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