bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 1) // SPDX-License-Identifier: GPL-2.0
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 2) #include <linux/fs.h>
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 3) #include <linux/export.h>
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 4)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 5) /*
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 6) * fs on-disk file type to dirent file type conversion
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 7) */
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 8) static const unsigned char fs_dtype_by_ftype[FT_MAX] = {
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 9) [FT_UNKNOWN] = DT_UNKNOWN,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 10) [FT_REG_FILE] = DT_REG,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 11) [FT_DIR] = DT_DIR,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 12) [FT_CHRDEV] = DT_CHR,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 13) [FT_BLKDEV] = DT_BLK,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 14) [FT_FIFO] = DT_FIFO,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 15) [FT_SOCK] = DT_SOCK,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 16) [FT_SYMLINK] = DT_LNK
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 17) };
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 18)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 19) /**
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 20) * fs_ftype_to_dtype() - fs on-disk file type to dirent type.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 21) * @filetype: The on-disk file type to convert.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 22) *
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 23) * This function converts the on-disk file type value (FT_*) to the directory
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 24) * entry type (DT_*).
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 25) *
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 26) * Context: Any context.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 27) * Return:
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 28) * * DT_UNKNOWN - Unknown type
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 29) * * DT_FIFO - FIFO
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 30) * * DT_CHR - Character device
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 31) * * DT_DIR - Directory
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 32) * * DT_BLK - Block device
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 33) * * DT_REG - Regular file
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 34) * * DT_LNK - Symbolic link
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 35) * * DT_SOCK - Local-domain socket
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 36) */
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 37) unsigned char fs_ftype_to_dtype(unsigned int filetype)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 38) {
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 39) if (filetype >= FT_MAX)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 40) return DT_UNKNOWN;
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 41)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 42) return fs_dtype_by_ftype[filetype];
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 43) }
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 44) EXPORT_SYMBOL_GPL(fs_ftype_to_dtype);
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 45)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 46) /*
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 47) * dirent file type to fs on-disk file type conversion
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 48) * Values not initialized explicitly are FT_UNKNOWN (0).
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 49) */
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 50) static const unsigned char fs_ftype_by_dtype[DT_MAX] = {
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 51) [DT_REG] = FT_REG_FILE,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 52) [DT_DIR] = FT_DIR,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 53) [DT_LNK] = FT_SYMLINK,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 54) [DT_CHR] = FT_CHRDEV,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 55) [DT_BLK] = FT_BLKDEV,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 56) [DT_FIFO] = FT_FIFO,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 57) [DT_SOCK] = FT_SOCK,
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 58) };
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 59)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 60) /**
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 61) * fs_umode_to_ftype() - file mode to on-disk file type.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 62) * @mode: The file mode to convert.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 63) *
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 64) * This function converts the file mode value to the on-disk file type (FT_*).
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 65) *
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 66) * Context: Any context.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 67) * Return:
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 68) * * FT_UNKNOWN - Unknown type
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 69) * * FT_REG_FILE - Regular file
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 70) * * FT_DIR - Directory
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 71) * * FT_CHRDEV - Character device
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 72) * * FT_BLKDEV - Block device
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 73) * * FT_FIFO - FIFO
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 74) * * FT_SOCK - Local-domain socket
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 75) * * FT_SYMLINK - Symbolic link
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 76) */
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 77) unsigned char fs_umode_to_ftype(umode_t mode)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 78) {
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 79) return fs_ftype_by_dtype[S_DT(mode)];
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 80) }
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 81) EXPORT_SYMBOL_GPL(fs_umode_to_ftype);
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 82)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 83) /**
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 84) * fs_umode_to_dtype() - file mode to dirent file type.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 85) * @mode: The file mode to convert.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 86) *
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 87) * This function converts the file mode value to the directory
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 88) * entry type (DT_*).
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 89) *
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 90) * Context: Any context.
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 91) * Return:
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 92) * * DT_UNKNOWN - Unknown type
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 93) * * DT_FIFO - FIFO
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 94) * * DT_CHR - Character device
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 95) * * DT_DIR - Directory
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 96) * * DT_BLK - Block device
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 97) * * DT_REG - Regular file
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 98) * * DT_LNK - Symbolic link
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 99) * * DT_SOCK - Local-domain socket
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 100) */
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 101) unsigned char fs_umode_to_dtype(umode_t mode)
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 102) {
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 103) return fs_ftype_to_dtype(fs_umode_to_ftype(mode));
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 104) }
bbe7449e2599b (Phillip Potter 2019-01-21 00:54:27 +0000 105) EXPORT_SYMBOL_GPL(fs_umode_to_dtype);