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