History

Merge tag 'dtype_for_v5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs

Pull dtype handling cleanups from Jan Kara: "A reworked dtype cleanup patches based on your feedback to the previous version of these. Again the series includes only the generic code and ext2 cleanup as a sample. The plan is to push cleanups for other filesystems separately through respective trees once the generic code lands to reduce the number of conflicts" * tag 'dtype_for_v5.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs: ext2: use common file type conversion fs: common implementation of file type

Linus Torvalds committed 7 years ago

Commit b39a07a2 parents f90d644 e1089217 files · +190 −62Browse files

Unified · Split

7 files
  1. MAINTAINERS +1 −0
  2. fs/Makefile +2 −1
  3. fs/ext2/dir.c +6 −29
  4. fs/ext2/ext2.h +0 −16
  5. fs/fs_types.c +105 −0
  6. include/linux/fs.h +1 −16
  7. include/linux/fs_types.h +75 −0
MAINTAINERS+1−0
@@ -5947,6 +5947,7 @@ L: linux-fsdevel@vger.kernel.org · 5946 unmodified lines
5947S: Maintained5947S: Maintained
5948F: fs/*5948F: fs/*
5949F: include/linux/fs.h5949F: include/linux/fs.h
5950F: include/linux/fs_types.h
5950F: include/uapi/linux/fs.h5951F: include/uapi/linux/fs.h
5951 5952
5952FINTEK F75375S HARDWARE MONITOR AND FAN CONTROLLER DRIVER5953FINTEK F75375S HARDWARE MONITOR AND FAN CONTROLLER DRIVER
fs/Makefile+2−1
@@ -12,7 +12,8 @@ obj-y := open.o read_write.o file_table.o super.o \ · 11 unmodified lines
12 attr.o bad_inode.o file.o filesystems.o namespace.o \12 attr.o bad_inode.o file.o filesystems.o namespace.o \
13 seq_file.o xattr.o libfs.o fs-writeback.o \13 seq_file.o xattr.o libfs.o fs-writeback.o \
14 pnode.o splice.o sync.o utimes.o d_path.o \14 pnode.o splice.o sync.o utimes.o d_path.o \
15 stack.o fs_struct.o statfs.o fs_pin.o nsfs.o15 stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
16 fs_types.o
16 17
17ifeq ($(CONFIG_BLOCK),y)18ifeq ($(CONFIG_BLOCK),y)
18obj-y += buffer.o block_dev.o direct-io.o mpage.o19obj-y += buffer.o block_dev.o direct-io.o mpage.o
fs/ext2/dir.c+6−29
@@ -252,33 +252,10 @@ ext2_validate_entry(char *base, unsigned offset, unsigned mask) · 251 unmodified lines
252 return (char *)p - base;252 return (char *)p - base;
253}253}
254 254
255static unsigned char ext2_filetype_table[EXT2_FT_MAX] = {
256 [EXT2_FT_UNKNOWN] = DT_UNKNOWN,
257 [EXT2_FT_REG_FILE] = DT_REG,
258 [EXT2_FT_DIR] = DT_DIR,
259 [EXT2_FT_CHRDEV] = DT_CHR,
260 [EXT2_FT_BLKDEV] = DT_BLK,
261 [EXT2_FT_FIFO] = DT_FIFO,
262 [EXT2_FT_SOCK] = DT_SOCK,
263 [EXT2_FT_SYMLINK] = DT_LNK,
264};
265
266#define S_SHIFT 12
267static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = {
268 [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE,
269 [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR,
270 [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV,
271 [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV,
272 [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO,
273 [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK,
274 [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK,
275};
276
277static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)255static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode)
278{256{
279 umode_t mode = inode->i_mode;
280 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))257 if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE))
281 de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT];258 de->file_type = fs_umode_to_ftype(inode->i_mode);
282 else259 else
283 de->file_type = 0;260 de->file_type = 0;
284}261}
@@ -293,14 +270,14 @@ ext2_readdir(struct file *file, struct dir_context *ctx) · 8 unmodified lines
293 unsigned long n = pos >> PAGE_SHIFT;270 unsigned long n = pos >> PAGE_SHIFT;
294 unsigned long npages = dir_pages(inode);271 unsigned long npages = dir_pages(inode);
295 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);272 unsigned chunk_mask = ~(ext2_chunk_size(inode)-1);
296 unsigned char *types = NULL;
297 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);273 bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
274 bool has_filetype;
298 275
299 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))276 if (pos > inode->i_size - EXT2_DIR_REC_LEN(1))
300 return 0;277 return 0;
301 278
302 if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE))279 has_filetype =
303 types = ext2_filetype_table;280 EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE);
304 281
305 for ( ; n < npages; n++, offset = 0) {282 for ( ; n < npages; n++, offset = 0) {
306 char *kaddr, *limit;283 char *kaddr, *limit;
@@ -335,8 +312,8 @@ ext2_readdir(struct file *file, struct dir_context *ctx) · 28 unmodified lines
335 if (de->inode) {312 if (de->inode) {
336 unsigned char d_type = DT_UNKNOWN;313 unsigned char d_type = DT_UNKNOWN;
337 314
338 if (types && de->file_type < EXT2_FT_MAX)315 if (has_filetype)
339 d_type = types[de->file_type];316 d_type = fs_ftype_to_dtype(de->file_type);
340 317
341 if (!dir_emit(ctx, de->name, de->name_len,318 if (!dir_emit(ctx, de->name, de->name_len,
342 le32_to_cpu(de->inode),319 le32_to_cpu(de->inode),
fs/ext2/ext2.h+0−16
@@ -603,22 +603,6 @@ struct ext2_dir_entry_2 { · 602 unmodified lines
603 char name[]; /* File name, up to EXT2_NAME_LEN */603 char name[]; /* File name, up to EXT2_NAME_LEN */
604};604};
605 605
606/*
607 * Ext2 directory file types. Only the low 3 bits are used. The
608 * other bits are reserved for now.
609 */
610enum {
611 EXT2_FT_UNKNOWN = 0,
612 EXT2_FT_REG_FILE = 1,
613 EXT2_FT_DIR = 2,
614 EXT2_FT_CHRDEV = 3,
615 EXT2_FT_BLKDEV = 4,
616 EXT2_FT_FIFO = 5,
617 EXT2_FT_SOCK = 6,
618 EXT2_FT_SYMLINK = 7,
619 EXT2_FT_MAX
620};
621
622/*606/*
623 * EXT2_DIR_PAD defines the directory entries boundaries607 * EXT2_DIR_PAD defines the directory entries boundaries
624 *608 *
fs/fs_types.c+105−0
@@ -0,0 +1,105 @@
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 */
8static 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 */
37unsigned 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}
44EXPORT_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 */
50static 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 */
77unsigned char fs_umode_to_ftype(umode_t mode)
78{
79 return fs_ftype_by_dtype[S_DT(mode)];
80}
81EXPORT_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 */
101unsigned char fs_umode_to_dtype(umode_t mode)
102{
103 return fs_ftype_to_dtype(fs_umode_to_ftype(mode));
104}
105EXPORT_SYMBOL_GPL(fs_umode_to_dtype);
include/linux/fs.h+1−16
@@ -37,6 +37,7 @@ · 36 unmodified lines
37#include <linux/uuid.h>37#include <linux/uuid.h>
38#include <linux/errseq.h>38#include <linux/errseq.h>
39#include <linux/ioprio.h>39#include <linux/ioprio.h>
40#include <linux/fs_types.h>
40 41
41#include <asm/byteorder.h>42#include <asm/byteorder.h>
42#include <uapi/linux/fs.h>43#include <uapi/linux/fs.h>
@@ -1706,22 +1707,6 @@ int fiemap_fill_next_extent(struct fiemap_extent_info *info, u64 logical, · 1663 unmodified lines
1706 u64 phys, u64 len, u32 flags);1707 u64 phys, u64 len, u32 flags);
1707int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags);1708int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags);
1708 1709
1709/*
1710 * File types
1711 *
1712 * NOTE! These match bits 12..15 of stat.st_mode
1713 * (ie "(i_mode >> 12) & 15").
1714 */
1715#define DT_UNKNOWN 0
1716#define DT_FIFO 1
1717#define DT_CHR 2
1718#define DT_DIR 4
1719#define DT_BLK 6
1720#define DT_REG 8
1721#define DT_LNK 10
1722#define DT_SOCK 12
1723#define DT_WHT 14
1724
1725/*1710/*
1726 * This is the "filldir" function type, used by readdir() to let1711 * This is the "filldir" function type, used by readdir() to let
1727 * the kernel specify what kind of dirent layout it wants to have.1712 * the kernel specify what kind of dirent layout it wants to have.
include/linux/fs_types.h+75−0
@@ -0,0 +1,75 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_FS_TYPES_H
3#define _LINUX_FS_TYPES_H
4
5/*
6 * This is a header for the common implementation of dirent
7 * to fs on-disk file type conversion. Although the fs on-disk
8 * bits are specific to every file system, in practice, many
9 * file systems use the exact same on-disk format to describe
10 * the lower 3 file type bits that represent the 7 POSIX file
11 * types.
12 *
13 * It is important to note that the definitions in this
14 * header MUST NOT change. This would break both the
15 * userspace ABI and the on-disk format of filesystems
16 * using this code.
17 *
18 * All those file systems can use this generic code for the
19 * conversions.
20 */
21
22/*
23 * struct dirent file types
24 * exposed to user via getdents(2), readdir(3)
25 *
26 * These match bits 12..15 of stat.st_mode
27 * (ie "(i_mode >> 12) & 15").
28 */
29#define S_DT_SHIFT 12
30#define S_DT(mode) (((mode) & S_IFMT) >> S_DT_SHIFT)
31#define S_DT_MASK (S_IFMT >> S_DT_SHIFT)
32
33/* these are defined by POSIX and also present in glibc's dirent.h */
34#define DT_UNKNOWN 0
35#define DT_FIFO 1
36#define DT_CHR 2
37#define DT_DIR 4
38#define DT_BLK 6
39#define DT_REG 8
40#define DT_LNK 10
41#define DT_SOCK 12
42#define DT_WHT 14
43
44#define DT_MAX (S_DT_MASK + 1) /* 16 */
45
46/*
47 * fs on-disk file types.
48 * Only the low 3 bits are used for the POSIX file types.
49 * Other bits are reserved for fs private use.
50 * These definitions are shared and used by multiple filesystems,
51 * and MUST NOT change under any circumstances.
52 *
53 * Note that no fs currently stores the whiteout type on-disk,
54 * so whiteout dirents are exposed to user as DT_CHR.
55 */
56#define FT_UNKNOWN 0
57#define FT_REG_FILE 1
58#define FT_DIR 2
59#define FT_CHRDEV 3
60#define FT_BLKDEV 4
61#define FT_FIFO 5
62#define FT_SOCK 6
63#define FT_SYMLINK 7
64
65#define FT_MAX 8
66
67/*
68 * declarations for helper functions, accompanying implementation
69 * is in fs/fs_types.c
70 */
71extern unsigned char fs_ftype_to_dtype(unsigned int filetype);
72extern unsigned char fs_umode_to_ftype(umode_t mode);
73extern unsigned char fs_umode_to_dtype(umode_t mode);
74
75#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t