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