xref: /linux/include/linux/fs/super_types.h (revision afdf0fb340948a8c0f581ed1dc42828af89b80b6)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FS_SUPER_TYPES_H
3 #define _LINUX_FS_SUPER_TYPES_H
4 
5 #include <linux/fs_dirent.h>
6 #include <linux/errseq.h>
7 #include <linux/list_lru.h>
8 #include <linux/list.h>
9 #include <linux/list_bl.h>
10 #include <linux/llist.h>
11 #include <linux/uidgid.h>
12 #include <linux/uuid.h>
13 #include <linux/percpu-rwsem.h>
14 #include <linux/workqueue_types.h>
15 #include <linux/quota.h>
16 
17 struct backing_dev_info;
18 struct block_device;
19 struct dentry;
20 struct dentry_operations;
21 struct dquot_operations;
22 struct export_operations;
23 struct file;
24 struct file_system_type;
25 struct fscrypt_operations;
26 struct fsnotify_sb_info;
27 struct fsverity_operations;
28 struct kstatfs;
29 struct mount;
30 struct mtd_info;
31 struct quotactl_ops;
32 struct shrinker;
33 struct unicode_map;
34 struct user_namespace;
35 struct workqueue_struct;
36 struct writeback_control;
37 struct xattr_handler;
38 
39 extern struct super_block *blockdev_superblock;
40 
41 /* Possible states of 'frozen' field */
42 enum {
43 	SB_UNFROZEN		= 0,	/* FS is unfrozen */
44 	SB_FREEZE_WRITE		= 1,	/* Writes, dir ops, ioctls frozen */
45 	SB_FREEZE_PAGEFAULT	= 2,	/* Page faults stopped as well */
46 	SB_FREEZE_FS		= 3,	/* For internal FS use (e.g. to stop internal threads if needed) */
47 	SB_FREEZE_COMPLETE	= 4,	/* ->freeze_fs finished successfully */
48 };
49 
50 #define SB_FREEZE_LEVELS (SB_FREEZE_COMPLETE - 1)
51 
52 struct sb_writers {
53 	unsigned short			frozen;		/* Is sb frozen? */
54 	int				freeze_kcount;	/* How many kernel freeze requests? */
55 	int				freeze_ucount;	/* How many userspace freeze requests? */
56 	const void			*freeze_owner;	/* Owner of the freeze */
57 	struct percpu_rw_semaphore	rw_sem[SB_FREEZE_LEVELS];
58 };
59 
60 /**
61  * enum freeze_holder - holder of the freeze
62  * @FREEZE_HOLDER_KERNEL: kernel wants to freeze or thaw filesystem
63  * @FREEZE_HOLDER_USERSPACE: userspace wants to freeze or thaw filesystem
64  * @FREEZE_MAY_NEST: whether nesting freeze and thaw requests is allowed
65  * @FREEZE_EXCL: a freeze that can only be undone by the owner
66  *
67  * Indicate who the owner of the freeze or thaw request is and whether
68  * the freeze needs to be exclusive or can nest.
69  * Without @FREEZE_MAY_NEST, multiple freeze and thaw requests from the
70  * same holder aren't allowed. It is however allowed to hold a single
71  * @FREEZE_HOLDER_USERSPACE and a single @FREEZE_HOLDER_KERNEL freeze at
72  * the same time. This is relied upon by some filesystems during online
73  * repair or similar.
74  */
75 enum freeze_holder {
76 	FREEZE_HOLDER_KERNEL	= (1U << 0),
77 	FREEZE_HOLDER_USERSPACE	= (1U << 1),
78 	FREEZE_MAY_NEST		= (1U << 2),
79 	FREEZE_EXCL		= (1U << 3),
80 };
81 
82 struct super_operations {
83 	struct inode *(*alloc_inode)(struct super_block *sb);
84 	void (*destroy_inode)(struct inode *inode);
85 	void (*free_inode)(struct inode *inode);
86 	void (*dirty_inode)(struct inode *inode, int flags);
87 	int (*write_inode)(struct inode *inode, struct writeback_control *wbc);
88 	int (*drop_inode)(struct inode *inode);
89 	void (*evict_inode)(struct inode *inode);
90 	void (*put_super)(struct super_block *sb);
91 	int (*sync_fs)(struct super_block *sb, int wait);
92 	int (*freeze_super)(struct super_block *sb, enum freeze_holder who,
93 			    const void *owner);
94 	int (*freeze_fs)(struct super_block *sb);
95 	int (*thaw_super)(struct super_block *sb, enum freeze_holder who,
96 			  const void *owner);
97 	int (*unfreeze_fs)(struct super_block *sb);
98 	int (*statfs)(struct dentry *dentry, struct kstatfs *kstatfs);
99 	int (*remount_fs) (struct super_block *, int *, char *);
100 	void (*umount_begin)(struct super_block *sb);
101 
102 	int (*show_options)(struct seq_file *seq, struct dentry *dentry);
103 	int (*show_devname)(struct seq_file *seq, struct dentry *dentry);
104 	int (*show_path)(struct seq_file *seq, struct dentry *dentry);
105 	int (*show_stats)(struct seq_file *seq, struct dentry *dentry);
106 #ifdef CONFIG_QUOTA
107 	ssize_t (*quota_read)(struct super_block *sb, int type, char *data,
108 			      size_t len, loff_t off);
109 	ssize_t (*quota_write)(struct super_block *sb, int type,
110 			       const char *data, size_t len, loff_t off);
111 	struct dquot __rcu **(*get_dquots)(struct inode *inode);
112 #endif
113 	long (*nr_cached_objects)(struct super_block *sb,
114 				  struct shrink_control *sc);
115 	long (*free_cached_objects)(struct super_block *sb,
116 				    struct shrink_control *sc);
117 	/*
118 	 * If a filesystem can support graceful removal of a device and
119 	 * continue read-write operations, implement this callback.
120 	 *
121 	 * Return 0 if the filesystem can continue read-write.
122 	 * Non-zero return value or no such callback means the fs will be shutdown
123 	 * as usual.
124 	 */
125 	int (*remove_bdev)(struct super_block *sb, struct block_device *bdev);
126 	void (*shutdown)(struct super_block *sb);
127 };
128 
129 struct super_block {
130 	struct list_head			s_list;		/* Keep this first */
131 	dev_t					s_dev;		/* search index; _not_ kdev_t */
132 	unsigned char				s_blocksize_bits;
133 	unsigned long				s_blocksize;
134 	loff_t					s_maxbytes;	/* Max file size */
135 	struct file_system_type			*s_type;
136 	const struct super_operations		*s_op;
137 	const struct dquot_operations		*dq_op;
138 	const struct quotactl_ops		*s_qcop;
139 	const struct export_operations		*s_export_op;
140 	unsigned long				s_flags;
141 	unsigned long				s_iflags;	/* internal SB_I_* flags */
142 	unsigned long				s_magic;
143 	struct dentry				*s_root;
144 	struct rw_semaphore			s_umount;
145 	int					s_count;
146 	atomic_t				s_active;
147 #ifdef CONFIG_SECURITY
148 	void					*s_security;
149 #endif
150 	const struct xattr_handler		*const *s_xattr;
151 #ifdef CONFIG_FS_ENCRYPTION
152 	const struct fscrypt_operations		*s_cop;
153 	struct fscrypt_keyring			*s_master_keys; /* master crypto keys in use */
154 #endif
155 #ifdef CONFIG_FS_VERITY
156 	const struct fsverity_operations	*s_vop;
157 #endif
158 #if IS_ENABLED(CONFIG_UNICODE)
159 	struct unicode_map			*s_encoding;
160 	__u16					s_encoding_flags;
161 #endif
162 	struct hlist_bl_head			s_roots;	/* alternate root dentries for NFS */
163 	struct mount				*s_mounts;	/* list of mounts; _not_ for fs use */
164 	struct block_device			*s_bdev;	/* can go away once we use an accessor for @s_bdev_file */
165 	struct file				*s_bdev_file;
166 	struct backing_dev_info 		*s_bdi;
167 	struct mtd_info				*s_mtd;
168 	struct hlist_node			s_instances;
169 	unsigned int				s_quota_types;	/* Bitmask of supported quota types */
170 	struct quota_info			s_dquot;	/* Diskquota specific options */
171 
172 	struct sb_writers			s_writers;
173 
174 	/*
175 	 * Keep s_fs_info, s_time_gran, s_fsnotify_mask, and
176 	 * s_fsnotify_info together for cache efficiency. They are frequently
177 	 * accessed and rarely modified.
178 	 */
179 	void					*s_fs_info;	/* Filesystem private info */
180 
181 	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
182 	u32					s_time_gran;
183 	/* Time limits for c/m/atime in seconds */
184 	time64_t				s_time_min;
185 	time64_t		   		s_time_max;
186 #ifdef CONFIG_FSNOTIFY
187 	u32					s_fsnotify_mask;
188 	struct fsnotify_sb_info			*s_fsnotify_info;
189 #endif
190 
191 	/*
192 	 * q: why are s_id and s_sysfs_name not the same? both are human
193 	 * readable strings that identify the filesystem
194 	 * a: s_id is allowed to change at runtime; it's used in log messages,
195 	 * and we want to when a device starts out as single device (s_id is dev
196 	 * name) but then a device is hot added and we have to switch to
197 	 * identifying it by UUID
198 	 * but s_sysfs_name is a handle for programmatic access, and can't
199 	 * change at runtime
200 	 */
201 	char					s_id[32];	/* Informational name */
202 	uuid_t					s_uuid;		/* UUID */
203 	u8					s_uuid_len;	/* Default 16, possibly smaller for weird filesystems */
204 
205 	/* if set, fs shows up under sysfs at /sys/fs/$FSTYP/s_sysfs_name */
206 	char					s_sysfs_name[UUID_STRING_LEN + 1];
207 
208 	unsigned int				s_max_links;
209 	unsigned int				s_d_flags;	/* default d_flags for dentries */
210 
211 	/*
212 	 * The next field is for VFS *only*. No filesystems have any business
213 	 * even looking at it. You had been warned.
214 	 */
215 	struct mutex				s_vfs_rename_mutex;	/* Kludge */
216 
217 	/*
218 	 * Filesystem subtype.  If non-empty the filesystem type field
219 	 * in /proc/mounts will be "type.subtype"
220 	 */
221 	const char				*s_subtype;
222 
223 	const struct dentry_operations		*__s_d_op; /* default d_op for dentries */
224 
225 	struct shrinker				*s_shrink;	/* per-sb shrinker handle */
226 
227 	/* Number of inodes with nlink == 0 but still referenced */
228 	atomic_long_t				s_remove_count;
229 
230 	/* Read-only state of the superblock is being changed */
231 	int					s_readonly_remount;
232 
233 	/* per-sb errseq_t for reporting writeback errors via syncfs */
234 	errseq_t s_wb_err;
235 
236 	/* AIO completions deferred from interrupt context */
237 	struct workqueue_struct			*s_dio_done_wq;
238 	struct hlist_head			s_pins;
239 
240 	/*
241 	 * Owning user namespace and default context in which to
242 	 * interpret filesystem uids, gids, quotas, device nodes,
243 	 * xattrs and security labels.
244 	 */
245 	struct user_namespace			*s_user_ns;
246 
247 	/*
248 	 * The list_lru structure is essentially just a pointer to a table
249 	 * of per-node lru lists, each of which has its own spinlock.
250 	 * There is no need to put them into separate cachelines.
251 	 */
252 	struct list_lru				s_dentry_lru;
253 	struct list_lru				s_inode_lru;
254 	struct rcu_head				rcu;
255 	struct work_struct			destroy_work;
256 
257 	struct mutex				s_sync_lock;	/* sync serialisation lock */
258 
259 	/*
260 	 * Indicates how deep in a filesystem stack this SB is
261 	 */
262 	int s_stack_depth;
263 
264 	/* s_inode_list_lock protects s_inodes */
265 	spinlock_t				s_inode_list_lock ____cacheline_aligned_in_smp;
266 	struct list_head			s_inodes;	/* all inodes */
267 
268 	spinlock_t				s_inode_wblist_lock;
269 	struct list_head			s_inodes_wb;	/* writeback inodes */
270 	long					s_min_writeback_pages;
271 } __randomize_layout;
272 
273 /*
274  * sb->s_flags.  Note that these mirror the equivalent MS_* flags where
275  * represented in both.
276  */
277 #define SB_RDONLY       BIT(0)	/* Mount read-only */
278 #define SB_NOSUID       BIT(1)	/* Ignore suid and sgid bits */
279 #define SB_NODEV        BIT(2)	/* Disallow access to device special files */
280 #define SB_NOEXEC       BIT(3)	/* Disallow program execution */
281 #define SB_SYNCHRONOUS  BIT(4)	/* Writes are synced at once */
282 #define SB_MANDLOCK     BIT(6)	/* Allow mandatory locks on an FS */
283 #define SB_DIRSYNC      BIT(7)	/* Directory modifications are synchronous */
284 #define SB_NOATIME      BIT(10)	/* Do not update access times. */
285 #define SB_NODIRATIME   BIT(11)	/* Do not update directory access times */
286 #define SB_SILENT       BIT(15)
287 #define SB_POSIXACL     BIT(16)	/* Supports POSIX ACLs */
288 #define SB_INLINECRYPT  BIT(17)	/* Use blk-crypto for encrypted files */
289 #define SB_KERNMOUNT    BIT(22)	/* this is a kern_mount call */
290 #define SB_I_VERSION    BIT(23)	/* Update inode I_version field */
291 #define SB_LAZYTIME     BIT(25)	/* Update the on-disk [acm]times lazily */
292 
293 /* These sb flags are internal to the kernel */
294 #define SB_DEAD         BIT(21)
295 #define SB_DYING        BIT(24)
296 #define SB_FORCE        BIT(27)
297 #define SB_NOSEC        BIT(28)
298 #define SB_BORN         BIT(29)
299 #define SB_ACTIVE       BIT(30)
300 #define SB_NOUSER       BIT(31)
301 
302 /* These flags relate to encoding and casefolding */
303 #define SB_ENC_STRICT_MODE_FL		(1 << 0)
304 #define SB_ENC_NO_COMPAT_FALLBACK_FL	(1 << 1)
305 
306 #define sb_has_strict_encoding(sb) \
307 	(sb->s_encoding_flags & SB_ENC_STRICT_MODE_FL)
308 
309 #if IS_ENABLED(CONFIG_UNICODE)
310 #define sb_no_casefold_compat_fallback(sb) \
311 	(sb->s_encoding_flags & SB_ENC_NO_COMPAT_FALLBACK_FL)
312 #else
313 #define sb_no_casefold_compat_fallback(sb) (1)
314 #endif
315 
316 /* sb->s_iflags */
317 #define SB_I_CGROUPWB	0x00000001	/* cgroup-aware writeback enabled */
318 #define SB_I_NOEXEC	0x00000002	/* Ignore executables on this fs */
319 #define SB_I_NODEV	0x00000004	/* Ignore devices on this fs */
320 #define SB_I_STABLE_WRITES 0x00000008	/* don't modify blks until WB is done */
321 
322 /* sb->s_iflags to limit user namespace mounts */
323 #define SB_I_USERNS_VISIBLE		0x00000010 /* fstype already mounted */
324 #define SB_I_IMA_UNVERIFIABLE_SIGNATURE	0x00000020
325 #define SB_I_UNTRUSTED_MOUNTER		0x00000040
326 #define SB_I_EVM_HMAC_UNSUPPORTED	0x00000080
327 
328 #define SB_I_SKIP_SYNC	0x00000100	/* Skip superblock at global sync */
329 #define SB_I_PERSB_BDI	0x00000200	/* has a per-sb bdi */
330 #define SB_I_TS_EXPIRY_WARNED 0x00000400 /* warned about timestamp range expiry */
331 #define SB_I_RETIRED	0x00000800	/* superblock shouldn't be reused */
332 #define SB_I_NOUMASK	0x00001000	/* VFS does not apply umask */
333 #define SB_I_NOIDMAP	0x00002000	/* No idmapped mounts on this superblock */
334 #define SB_I_ALLOW_HSM	0x00004000	/* Allow HSM events on this superblock */
335 
336 #endif /* _LINUX_FS_SUPER_TYPES_H */
337