xref: /linux/include/linux/fs/super_types.h (revision 157d3d6efd5a58466d90be3a134f9667486fe6f9)
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_bl_head			s_roots;	/* alternate root dentries for NFS */
166 	struct mount				*s_mounts;	/* list of mounts; _not_ for fs use */
167 	struct block_device			*s_bdev;	/* can go away once we use an accessor for @s_bdev_file */
168 	struct file				*s_bdev_file;
169 	struct backing_dev_info 		*s_bdi;
170 	struct mtd_info				*s_mtd;
171 	struct hlist_node			s_instances;
172 	unsigned int				s_quota_types;	/* Bitmask of supported quota types */
173 	struct quota_info			s_dquot;	/* Diskquota specific options */
174 
175 	struct sb_writers			s_writers;
176 
177 	/*
178 	 * Keep s_fs_info, s_time_gran, s_fsnotify_mask, and
179 	 * s_fsnotify_info together for cache efficiency. They are frequently
180 	 * accessed and rarely modified.
181 	 */
182 	void					*s_fs_info;	/* Filesystem private info */
183 
184 	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
185 	u32					s_time_gran;
186 	/* Time limits for c/m/atime in seconds */
187 	time64_t				s_time_min;
188 	time64_t		   		s_time_max;
189 #ifdef CONFIG_FSNOTIFY
190 	u32					s_fsnotify_mask;
191 	struct fsnotify_sb_info			*s_fsnotify_info;
192 #endif
193 
194 	/*
195 	 * q: why are s_id and s_sysfs_name not the same? both are human
196 	 * readable strings that identify the filesystem
197 	 * a: s_id is allowed to change at runtime; it's used in log messages,
198 	 * and we want to when a device starts out as single device (s_id is dev
199 	 * name) but then a device is hot added and we have to switch to
200 	 * identifying it by UUID
201 	 * but s_sysfs_name is a handle for programmatic access, and can't
202 	 * change at runtime
203 	 */
204 	char					s_id[32];	/* Informational name */
205 	uuid_t					s_uuid;		/* UUID */
206 	u8					s_uuid_len;	/* Default 16, possibly smaller for weird filesystems */
207 
208 	/* if set, fs shows up under sysfs at /sys/fs/$FSTYP/s_sysfs_name */
209 	char					s_sysfs_name[UUID_STRING_LEN + 1];
210 
211 	unsigned int				s_max_links;
212 	unsigned int				s_d_flags;	/* default d_flags for dentries */
213 
214 	/*
215 	 * The next field is for VFS *only*. No filesystems have any business
216 	 * even looking at it. You had been warned.
217 	 */
218 	struct mutex				s_vfs_rename_mutex;	/* Kludge */
219 
220 	/*
221 	 * Filesystem subtype.  If non-empty the filesystem type field
222 	 * in /proc/mounts will be "type.subtype"
223 	 */
224 	const char				*s_subtype;
225 
226 	const struct dentry_operations		*__s_d_op; /* default d_op for dentries */
227 
228 	struct shrinker				*s_shrink;	/* per-sb shrinker handle */
229 
230 	/* Number of inodes with nlink == 0 but still referenced */
231 	atomic_long_t				s_remove_count;
232 
233 	/* Read-only state of the superblock is being changed */
234 	int					s_readonly_remount;
235 
236 	/* per-sb errseq_t for reporting writeback errors via syncfs */
237 	errseq_t s_wb_err;
238 
239 	/* AIO completions deferred from interrupt context */
240 	struct workqueue_struct			*s_dio_done_wq;
241 	struct hlist_head			s_pins;
242 
243 	/*
244 	 * Owning user namespace and default context in which to
245 	 * interpret filesystem uids, gids, quotas, device nodes,
246 	 * xattrs and security labels.
247 	 */
248 	struct user_namespace			*s_user_ns;
249 
250 	/*
251 	 * The list_lru structure is essentially just a pointer to a table
252 	 * of per-node lru lists, each of which has its own spinlock.
253 	 * There is no need to put them into separate cachelines.
254 	 */
255 	struct list_lru				s_dentry_lru;
256 	struct list_lru				s_inode_lru;
257 	struct rcu_head				rcu;
258 	struct work_struct			destroy_work;
259 
260 	struct mutex				s_sync_lock;	/* sync serialisation lock */
261 
262 	/*
263 	 * Indicates how deep in a filesystem stack this SB is
264 	 */
265 	int s_stack_depth;
266 
267 	/* s_inode_list_lock protects s_inodes */
268 	spinlock_t				s_inode_list_lock ____cacheline_aligned_in_smp;
269 	struct list_head			s_inodes;	/* all inodes */
270 
271 	spinlock_t				s_inode_wblist_lock;
272 	struct list_head			s_inodes_wb;	/* writeback inodes */
273 	long					s_min_writeback_pages;
274 
275 	/* number of fserrors that are being sent to fsnotify/filesystems */
276 	refcount_t				s_pending_errors;
277 } __randomize_layout;
278 
279 /*
280  * sb->s_flags.  Note that these mirror the equivalent MS_* flags where
281  * represented in both.
282  */
283 #define SB_RDONLY       BIT(0)	/* Mount read-only */
284 #define SB_NOSUID       BIT(1)	/* Ignore suid and sgid bits */
285 #define SB_NODEV        BIT(2)	/* Disallow access to device special files */
286 #define SB_NOEXEC       BIT(3)	/* Disallow program execution */
287 #define SB_SYNCHRONOUS  BIT(4)	/* Writes are synced at once */
288 #define SB_MANDLOCK     BIT(6)	/* Allow mandatory locks on an FS */
289 #define SB_DIRSYNC      BIT(7)	/* Directory modifications are synchronous */
290 #define SB_NOATIME      BIT(10)	/* Do not update access times. */
291 #define SB_NODIRATIME   BIT(11)	/* Do not update directory access times */
292 #define SB_SILENT       BIT(15)
293 #define SB_POSIXACL     BIT(16)	/* Supports POSIX ACLs */
294 #define SB_INLINECRYPT  BIT(17)	/* Use blk-crypto for encrypted files */
295 #define SB_KERNMOUNT    BIT(22)	/* this is a kern_mount call */
296 #define SB_I_VERSION    BIT(23)	/* Update inode I_version field */
297 #define SB_LAZYTIME     BIT(25)	/* Update the on-disk [acm]times lazily */
298 
299 /* These sb flags are internal to the kernel */
300 #define SB_DEAD         BIT(21)
301 #define SB_DYING        BIT(24)
302 #define SB_FORCE        BIT(27)
303 #define SB_NOSEC        BIT(28)
304 #define SB_BORN         BIT(29)
305 #define SB_ACTIVE       BIT(30)
306 #define SB_NOUSER       BIT(31)
307 
308 /* These flags relate to encoding and casefolding */
309 #define SB_ENC_STRICT_MODE_FL		(1 << 0)
310 #define SB_ENC_NO_COMPAT_FALLBACK_FL	(1 << 1)
311 
312 #define sb_has_strict_encoding(sb) \
313 	(sb->s_encoding_flags & SB_ENC_STRICT_MODE_FL)
314 
315 #if IS_ENABLED(CONFIG_UNICODE)
316 #define sb_no_casefold_compat_fallback(sb) \
317 	(sb->s_encoding_flags & SB_ENC_NO_COMPAT_FALLBACK_FL)
318 #else
319 #define sb_no_casefold_compat_fallback(sb) (1)
320 #endif
321 
322 /* sb->s_iflags */
323 #define SB_I_CGROUPWB	0x00000001	/* cgroup-aware writeback enabled */
324 #define SB_I_NOEXEC	0x00000002	/* Ignore executables on this fs */
325 #define SB_I_NODEV	0x00000004	/* Ignore devices on this fs */
326 #define SB_I_STABLE_WRITES 0x00000008	/* don't modify blks until WB is done */
327 
328 /* sb->s_iflags to limit user namespace mounts */
329 #define SB_I_USERNS_VISIBLE		0x00000010 /* fstype already mounted */
330 #define SB_I_IMA_UNVERIFIABLE_SIGNATURE	0x00000020
331 #define SB_I_UNTRUSTED_MOUNTER		0x00000040
332 #define SB_I_EVM_HMAC_UNSUPPORTED	0x00000080
333 
334 #define SB_I_SKIP_SYNC	0x00000100	/* Skip superblock at global sync */
335 #define SB_I_PERSB_BDI	0x00000200	/* has a per-sb bdi */
336 #define SB_I_TS_EXPIRY_WARNED 0x00000400 /* warned about timestamp range expiry */
337 #define SB_I_RETIRED	0x00000800	/* superblock shouldn't be reused */
338 #define SB_I_NOUMASK	0x00001000	/* VFS does not apply umask */
339 #define SB_I_NOIDMAP	0x00002000	/* No idmapped mounts on this superblock */
340 #define SB_I_ALLOW_HSM	0x00004000	/* Allow HSM events on this superblock */
341 
342 #endif /* _LINUX_FS_SUPER_TYPES_H */
343