xref: /linux/fs/xfs/xfs_super.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 
7 #include "xfs_platform.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_sb.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_btree.h"
16 #include "xfs_bmap.h"
17 #include "xfs_alloc.h"
18 #include "xfs_fsops.h"
19 #include "xfs_trans.h"
20 #include "xfs_buf_item.h"
21 #include "xfs_log.h"
22 #include "xfs_log_priv.h"
23 #include "xfs_dir2.h"
24 #include "xfs_extfree_item.h"
25 #include "xfs_mru_cache.h"
26 #include "xfs_inode_item.h"
27 #include "xfs_icache.h"
28 #include "xfs_trace.h"
29 #include "xfs_icreate_item.h"
30 #include "xfs_filestream.h"
31 #include "xfs_quota.h"
32 #include "xfs_sysfs.h"
33 #include "xfs_ondisk.h"
34 #include "xfs_rmap_item.h"
35 #include "xfs_refcount_item.h"
36 #include "xfs_bmap_item.h"
37 #include "xfs_reflink.h"
38 #include "xfs_pwork.h"
39 #include "xfs_ag.h"
40 #include "xfs_defer.h"
41 #include "xfs_attr_item.h"
42 #include "xfs_xattr.h"
43 #include "xfs_error.h"
44 #include "xfs_errortag.h"
45 #include "xfs_iunlink_item.h"
46 #include "xfs_dahash_test.h"
47 #include "xfs_rtbitmap.h"
48 #include "xfs_exchmaps_item.h"
49 #include "xfs_parent.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_zone_alloc.h"
52 #include "xfs_healthmon.h"
53 #include "scrub/stats.h"
54 #include "scrub/rcbag_btree.h"
55 
56 #include <linux/magic.h>
57 #include <linux/fs_context.h>
58 #include <linux/fs_parser.h>
59 #include <linux/fserror.h>
60 
61 static const struct super_operations xfs_super_operations;
62 
63 static struct dentry *xfs_debugfs;	/* top-level xfs debugfs dir */
64 static struct kset *xfs_kset;		/* top-level xfs sysfs dir */
65 #ifdef DEBUG
66 static struct xfs_kobj xfs_dbg_kobj;	/* global debug sysfs attrs */
67 #endif
68 
69 enum xfs_dax_mode {
70 	XFS_DAX_INODE = 0,
71 	XFS_DAX_ALWAYS = 1,
72 	XFS_DAX_NEVER = 2,
73 };
74 
75 /* Were quota mount options provided?  Must use the upper 16 bits of qflags. */
76 #define XFS_QFLAGS_MNTOPTS	(1U << 31)
77 
78 static void
xfs_mount_set_dax_mode(struct xfs_mount * mp,enum xfs_dax_mode mode)79 xfs_mount_set_dax_mode(
80 	struct xfs_mount	*mp,
81 	enum xfs_dax_mode	mode)
82 {
83 	switch (mode) {
84 	case XFS_DAX_INODE:
85 		mp->m_features &= ~(XFS_FEAT_DAX_ALWAYS | XFS_FEAT_DAX_NEVER);
86 		break;
87 	case XFS_DAX_ALWAYS:
88 		mp->m_features |= XFS_FEAT_DAX_ALWAYS;
89 		mp->m_features &= ~XFS_FEAT_DAX_NEVER;
90 		break;
91 	case XFS_DAX_NEVER:
92 		mp->m_features |= XFS_FEAT_DAX_NEVER;
93 		mp->m_features &= ~XFS_FEAT_DAX_ALWAYS;
94 		break;
95 	}
96 }
97 
98 static const struct constant_table dax_param_enums[] = {
99 	{"inode",	XFS_DAX_INODE },
100 	{"always",	XFS_DAX_ALWAYS },
101 	{"never",	XFS_DAX_NEVER },
102 	{}
103 };
104 
105 /*
106  * Table driven mount option parser.
107  */
108 enum {
109 	Op_deprecated, Opt_logbufs, Opt_logbsize, Opt_logdev, Opt_rtdev,
110 	Opt_wsync, Opt_noalign, Opt_swalloc, Opt_sunit, Opt_swidth, Opt_nouuid,
111 	Opt_grpid, Opt_nogrpid, Opt_bsdgroups, Opt_sysvgroups,
112 	Opt_allocsize, Opt_norecovery, Opt_inode64, Opt_inode32,
113 	Opt_largeio, Opt_nolargeio,
114 	Opt_filestreams, Opt_quota, Opt_noquota, Opt_usrquota, Opt_grpquota,
115 	Opt_prjquota, Opt_uquota, Opt_gquota, Opt_pquota,
116 	Opt_uqnoenforce, Opt_gqnoenforce, Opt_pqnoenforce, Opt_qnoenforce,
117 	Opt_discard, Opt_nodiscard, Opt_dax, Opt_dax_enum, Opt_max_open_zones,
118 	Opt_lifetime, Opt_nolifetime, Opt_max_atomic_write, Opt_errortag,
119 };
120 
121 #define fsparam_dead(NAME) \
122 	__fsparam(NULL, (NAME), Op_deprecated, fs_param_deprecated, NULL)
123 
124 static const struct fs_parameter_spec xfs_fs_parameters[] = {
125 	/*
126 	 * These mount options were supposed to be deprecated in September 2025
127 	 * but the deprecation warning was buggy, so not all users were
128 	 * notified.  The deprecation is now obnoxiously loud and postponed to
129 	 * September 2030.
130 	 */
131 	fsparam_dead("attr2"),
132 	fsparam_dead("noattr2"),
133 	fsparam_dead("ikeep"),
134 	fsparam_dead("noikeep"),
135 
136 	fsparam_u32("logbufs",		Opt_logbufs),
137 	fsparam_string("logbsize",	Opt_logbsize),
138 	fsparam_string("logdev",	Opt_logdev),
139 	fsparam_string("rtdev",		Opt_rtdev),
140 	fsparam_flag("wsync",		Opt_wsync),
141 	fsparam_flag("noalign",		Opt_noalign),
142 	fsparam_flag("swalloc",		Opt_swalloc),
143 	fsparam_u32("sunit",		Opt_sunit),
144 	fsparam_u32("swidth",		Opt_swidth),
145 	fsparam_flag("nouuid",		Opt_nouuid),
146 	fsparam_flag("grpid",		Opt_grpid),
147 	fsparam_flag("nogrpid",		Opt_nogrpid),
148 	fsparam_flag("bsdgroups",	Opt_bsdgroups),
149 	fsparam_flag("sysvgroups",	Opt_sysvgroups),
150 	fsparam_string("allocsize",	Opt_allocsize),
151 	fsparam_flag("norecovery",	Opt_norecovery),
152 	fsparam_flag("inode64",		Opt_inode64),
153 	fsparam_flag("inode32",		Opt_inode32),
154 	fsparam_flag("largeio",		Opt_largeio),
155 	fsparam_flag("nolargeio",	Opt_nolargeio),
156 	fsparam_flag("filestreams",	Opt_filestreams),
157 	fsparam_flag("quota",		Opt_quota),
158 	fsparam_flag("noquota",		Opt_noquota),
159 	fsparam_flag("usrquota",	Opt_usrquota),
160 	fsparam_flag("grpquota",	Opt_grpquota),
161 	fsparam_flag("prjquota",	Opt_prjquota),
162 	fsparam_flag("uquota",		Opt_uquota),
163 	fsparam_flag("gquota",		Opt_gquota),
164 	fsparam_flag("pquota",		Opt_pquota),
165 	fsparam_flag("uqnoenforce",	Opt_uqnoenforce),
166 	fsparam_flag("gqnoenforce",	Opt_gqnoenforce),
167 	fsparam_flag("pqnoenforce",	Opt_pqnoenforce),
168 	fsparam_flag("qnoenforce",	Opt_qnoenforce),
169 	fsparam_flag("discard",		Opt_discard),
170 	fsparam_flag("nodiscard",	Opt_nodiscard),
171 	fsparam_flag("dax",		Opt_dax),
172 	fsparam_enum("dax",		Opt_dax_enum, dax_param_enums),
173 	fsparam_u32("max_open_zones",	Opt_max_open_zones),
174 	fsparam_flag("lifetime",	Opt_lifetime),
175 	fsparam_flag("nolifetime",	Opt_nolifetime),
176 	fsparam_string("max_atomic_write",	Opt_max_atomic_write),
177 	fsparam_string("errortag",	Opt_errortag),
178 	{}
179 };
180 
181 struct proc_xfs_info {
182 	uint64_t	flag;
183 	char		*str;
184 };
185 
186 static int
xfs_fs_show_options(struct seq_file * m,struct dentry * root)187 xfs_fs_show_options(
188 	struct seq_file		*m,
189 	struct dentry		*root)
190 {
191 	static struct proc_xfs_info xfs_info_set[] = {
192 		/* the few simple ones we can get from the mount struct */
193 		{ XFS_FEAT_WSYNC,		",wsync" },
194 		{ XFS_FEAT_NOALIGN,		",noalign" },
195 		{ XFS_FEAT_SWALLOC,		",swalloc" },
196 		{ XFS_FEAT_NOUUID,		",nouuid" },
197 		{ XFS_FEAT_NORECOVERY,		",norecovery" },
198 		{ XFS_FEAT_FILESTREAMS,		",filestreams" },
199 		{ XFS_FEAT_GRPID,		",grpid" },
200 		{ XFS_FEAT_DISCARD,		",discard" },
201 		{ XFS_FEAT_LARGE_IOSIZE,	",largeio" },
202 		{ XFS_FEAT_DAX_ALWAYS,		",dax=always" },
203 		{ XFS_FEAT_DAX_NEVER,		",dax=never" },
204 		{ XFS_FEAT_NOLIFETIME,		",nolifetime" },
205 		{ 0, NULL }
206 	};
207 	struct xfs_mount	*mp = XFS_M(root->d_sb);
208 	struct proc_xfs_info	*xfs_infop;
209 
210 	for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
211 		if (mp->m_features & xfs_infop->flag)
212 			seq_puts(m, xfs_infop->str);
213 	}
214 
215 	seq_printf(m, ",inode%d", xfs_has_small_inums(mp) ? 32 : 64);
216 
217 	if (xfs_has_allocsize(mp))
218 		seq_printf(m, ",allocsize=%dk",
219 			   (1 << mp->m_allocsize_log) >> 10);
220 
221 	if (mp->m_logbufs > 0)
222 		seq_printf(m, ",logbufs=%d", mp->m_logbufs);
223 	if (mp->m_logbsize > 0)
224 		seq_printf(m, ",logbsize=%dk", mp->m_logbsize >> 10);
225 
226 	if (mp->m_logname)
227 		seq_show_option(m, "logdev", mp->m_logname);
228 	if (mp->m_rtname)
229 		seq_show_option(m, "rtdev", mp->m_rtname);
230 
231 	if (mp->m_dalign > 0)
232 		seq_printf(m, ",sunit=%d",
233 				(int)XFS_FSB_TO_BB(mp, mp->m_dalign));
234 	if (mp->m_swidth > 0)
235 		seq_printf(m, ",swidth=%d",
236 				(int)XFS_FSB_TO_BB(mp, mp->m_swidth));
237 
238 	if (mp->m_qflags & XFS_UQUOTA_ENFD)
239 		seq_puts(m, ",usrquota");
240 	else if (mp->m_qflags & XFS_UQUOTA_ACCT)
241 		seq_puts(m, ",uqnoenforce");
242 
243 	if (mp->m_qflags & XFS_PQUOTA_ENFD)
244 		seq_puts(m, ",prjquota");
245 	else if (mp->m_qflags & XFS_PQUOTA_ACCT)
246 		seq_puts(m, ",pqnoenforce");
247 
248 	if (mp->m_qflags & XFS_GQUOTA_ENFD)
249 		seq_puts(m, ",grpquota");
250 	else if (mp->m_qflags & XFS_GQUOTA_ACCT)
251 		seq_puts(m, ",gqnoenforce");
252 
253 	if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
254 		seq_puts(m, ",noquota");
255 
256 	if (mp->m_max_open_zones)
257 		seq_printf(m, ",max_open_zones=%u", mp->m_max_open_zones);
258 	if (mp->m_awu_max_bytes)
259 		seq_printf(m, ",max_atomic_write=%lluk",
260 				mp->m_awu_max_bytes >> 10);
261 
262 	return 0;
263 }
264 
265 static bool
xfs_set_inode_alloc_perag(struct xfs_perag * pag,xfs_ino_t ino,xfs_agnumber_t max_metadata)266 xfs_set_inode_alloc_perag(
267 	struct xfs_perag	*pag,
268 	xfs_ino_t		ino,
269 	xfs_agnumber_t		max_metadata)
270 {
271 	if (!xfs_is_inode32(pag_mount(pag))) {
272 		set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
273 		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
274 		return false;
275 	}
276 
277 	if (ino > XFS_MAXINUMBER_32) {
278 		clear_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
279 		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
280 		return false;
281 	}
282 
283 	set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
284 	if (pag_agno(pag) < max_metadata)
285 		set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
286 	else
287 		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
288 	return true;
289 }
290 
291 /*
292  * Set parameters for inode allocation heuristics, taking into account
293  * filesystem size and inode32/inode64 mount options; i.e. specifically
294  * whether or not XFS_FEAT_SMALL_INUMS is set.
295  *
296  * Inode allocation patterns are altered only if inode32 is requested
297  * (XFS_FEAT_SMALL_INUMS), and the filesystem is sufficiently large.
298  * If altered, XFS_OPSTATE_INODE32 is set as well.
299  *
300  * An agcount independent of that in the mount structure is provided
301  * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
302  * to the potentially higher ag count.
303  *
304  * Returns the maximum AG index which may contain inodes.
305  */
306 xfs_agnumber_t
xfs_set_inode_alloc(struct xfs_mount * mp,xfs_agnumber_t agcount)307 xfs_set_inode_alloc(
308 	struct xfs_mount *mp,
309 	xfs_agnumber_t	agcount)
310 {
311 	xfs_agnumber_t	index;
312 	xfs_agnumber_t	maxagi = 0;
313 	xfs_sb_t	*sbp = &mp->m_sb;
314 	xfs_agnumber_t	max_metadata;
315 	xfs_agino_t	agino;
316 	xfs_ino_t	ino;
317 
318 	/*
319 	 * Calculate how much should be reserved for inodes to meet
320 	 * the max inode percentage.  Used only for inode32.
321 	 */
322 	if (M_IGEO(mp)->maxicount) {
323 		uint64_t	icount;
324 
325 		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
326 		do_div(icount, 100);
327 		icount += sbp->sb_agblocks - 1;
328 		do_div(icount, sbp->sb_agblocks);
329 		max_metadata = icount;
330 	} else {
331 		max_metadata = agcount;
332 	}
333 
334 	/* Get the last possible inode in the filesystem */
335 	agino =	XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
336 	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);
337 
338 	/*
339 	 * If user asked for no more than 32-bit inodes, and the fs is
340 	 * sufficiently large, set XFS_OPSTATE_INODE32 if we must alter
341 	 * the allocator to accommodate the request.
342 	 */
343 	if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
344 		xfs_set_inode32(mp);
345 	else
346 		xfs_clear_inode32(mp);
347 
348 	for (index = 0; index < agcount; index++) {
349 		struct xfs_perag	*pag;
350 
351 		ino = XFS_AGINO_TO_INO(mp, index, agino);
352 
353 		pag = xfs_perag_get(mp, index);
354 		if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
355 			maxagi++;
356 		xfs_perag_put(pag);
357 	}
358 
359 	return xfs_is_inode32(mp) ? maxagi : agcount;
360 }
361 
362 static int
xfs_setup_dax_always(struct xfs_mount * mp)363 xfs_setup_dax_always(
364 	struct xfs_mount	*mp)
365 {
366 	if (!mp->m_ddev_targp->bt_daxdev &&
367 	    (!mp->m_rtdev_targp || !mp->m_rtdev_targp->bt_daxdev)) {
368 		xfs_alert(mp,
369 			"DAX unsupported by block device. Turning off DAX.");
370 		goto disable_dax;
371 	}
372 
373 	if (mp->m_super->s_blocksize != PAGE_SIZE) {
374 		xfs_alert(mp,
375 			"DAX not supported for blocksize. Turning off DAX.");
376 		goto disable_dax;
377 	}
378 
379 	if (xfs_has_reflink(mp) &&
380 	    bdev_is_partition(mp->m_ddev_targp->bt_bdev)) {
381 		xfs_alert(mp,
382 			"DAX and reflink cannot work with multi-partitions!");
383 		return -EINVAL;
384 	}
385 
386 	return 0;
387 
388 disable_dax:
389 	xfs_mount_set_dax_mode(mp, XFS_DAX_NEVER);
390 	return 0;
391 }
392 
393 STATIC int
xfs_blkdev_get(xfs_mount_t * mp,const char * name,struct file ** bdev_filep)394 xfs_blkdev_get(
395 	xfs_mount_t		*mp,
396 	const char		*name,
397 	struct file		**bdev_filep)
398 {
399 	int			error = 0;
400 	blk_mode_t		mode;
401 
402 	mode = sb_open_mode(mp->m_super->s_flags);
403 	*bdev_filep = fs_bdev_file_open_by_path(name, mode,
404 			mp->m_super, mp->m_super);
405 	if (IS_ERR(*bdev_filep)) {
406 		error = PTR_ERR(*bdev_filep);
407 		*bdev_filep = NULL;
408 		xfs_warn(mp, "Invalid device [%s], error=%d", name, error);
409 	}
410 
411 	return error;
412 }
413 
414 STATIC void
xfs_shutdown_devices(struct xfs_mount * mp)415 xfs_shutdown_devices(
416 	struct xfs_mount	*mp)
417 {
418 	/*
419 	 * Udev is triggered whenever anyone closes a block device or unmounts
420 	 * a file systemm on a block device.
421 	 * The default udev rules invoke blkid to read the fs super and create
422 	 * symlinks to the bdev under /dev/disk.  For this, it uses buffered
423 	 * reads through the page cache.
424 	 *
425 	 * xfs_db also uses buffered reads to examine metadata.  There is no
426 	 * coordination between xfs_db and udev, which means that they can run
427 	 * concurrently.  Note there is no coordination between the kernel and
428 	 * blkid either.
429 	 *
430 	 * On a system with 64k pages, the page cache can cache the superblock
431 	 * and the root inode (and hence the root directory) with the same 64k
432 	 * page.  If udev spawns blkid after the mkfs and the system is busy
433 	 * enough that it is still running when xfs_db starts up, they'll both
434 	 * read from the same page in the pagecache.
435 	 *
436 	 * The unmount writes updated inode metadata to disk directly.  The XFS
437 	 * buffer cache does not use the bdev pagecache, so it needs to
438 	 * invalidate that pagecache on unmount.  If the above scenario occurs,
439 	 * the pagecache no longer reflects what's on disk, xfs_db reads the
440 	 * stale metadata, and fails to find /a.  Most of the time this succeeds
441 	 * because closing a bdev invalidates the page cache, but when processes
442 	 * race, everyone loses.
443 	 */
444 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
445 		blkdev_issue_flush(mp->m_logdev_targp->bt_bdev);
446 		invalidate_bdev(mp->m_logdev_targp->bt_bdev);
447 	}
448 	if (mp->m_rtdev_targp) {
449 		blkdev_issue_flush(mp->m_rtdev_targp->bt_bdev);
450 		invalidate_bdev(mp->m_rtdev_targp->bt_bdev);
451 	}
452 	blkdev_issue_flush(mp->m_ddev_targp->bt_bdev);
453 	invalidate_bdev(mp->m_ddev_targp->bt_bdev);
454 }
455 
456 /*
457  * The file system configurations are:
458  *	(1) device (partition) with data and internal log
459  *	(2) logical volume with data and log subvolumes.
460  *	(3) logical volume with data, log, and realtime subvolumes.
461  *
462  * We only have to handle opening the log and realtime volumes here if
463  * they are present.  The data subvolume has already been opened by
464  * get_sb_bdev() and is stored in sb->s_bdev.
465  */
466 STATIC int
xfs_open_devices(struct xfs_mount * mp)467 xfs_open_devices(
468 	struct xfs_mount	*mp)
469 {
470 	struct super_block	*sb = mp->m_super;
471 	struct block_device	*ddev = sb->s_bdev;
472 	struct file		*logdev_file = NULL, *rtdev_file = NULL;
473 	int			error;
474 
475 	/*
476 	 * Open real time and log devices - order is important.
477 	 */
478 	if (mp->m_logname) {
479 		error = xfs_blkdev_get(mp, mp->m_logname, &logdev_file);
480 		if (error)
481 			return error;
482 	}
483 
484 	if (mp->m_rtname) {
485 		error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev_file);
486 		if (error)
487 			goto out_close_logdev;
488 
489 		if (file_bdev(rtdev_file) == ddev ||
490 		    (logdev_file &&
491 		     file_bdev(rtdev_file) == file_bdev(logdev_file))) {
492 			xfs_warn(mp,
493 	"Cannot mount filesystem with identical rtdev and ddev/logdev.");
494 			error = -EINVAL;
495 			goto out_close_rtdev;
496 		}
497 	}
498 
499 	/*
500 	 * Setup xfs_mount buffer target pointers
501 	 */
502 	mp->m_ddev_targp = xfs_alloc_buftarg(mp, sb->s_bdev_file);
503 	if (IS_ERR(mp->m_ddev_targp)) {
504 		error = PTR_ERR(mp->m_ddev_targp);
505 		mp->m_ddev_targp = NULL;
506 		goto out_close_rtdev;
507 	}
508 
509 	if (rtdev_file) {
510 		mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev_file);
511 		if (IS_ERR(mp->m_rtdev_targp)) {
512 			error = PTR_ERR(mp->m_rtdev_targp);
513 			mp->m_rtdev_targp = NULL;
514 			goto out_free_ddev_targ;
515 		}
516 	}
517 
518 	if (logdev_file && file_bdev(logdev_file) != ddev) {
519 		mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev_file);
520 		if (IS_ERR(mp->m_logdev_targp)) {
521 			error = PTR_ERR(mp->m_logdev_targp);
522 			mp->m_logdev_targp = NULL;
523 			goto out_free_rtdev_targ;
524 		}
525 	} else {
526 		mp->m_logdev_targp = mp->m_ddev_targp;
527 		/* Handle won't be used, drop it */
528 		if (logdev_file)
529 			fs_bdev_file_release(logdev_file, mp->m_super);
530 	}
531 
532 	return 0;
533 
534  out_free_rtdev_targ:
535 	if (mp->m_rtdev_targp)
536 		xfs_free_buftarg(mp->m_rtdev_targp);
537 	mp->m_rtdev_targp = NULL;
538 	rtdev_file = NULL;	/* released by xfs_free_buftarg() */
539  out_free_ddev_targ:
540 	xfs_free_buftarg(mp->m_ddev_targp);
541 	mp->m_ddev_targp = NULL;
542  out_close_rtdev:
543 	 if (rtdev_file)
544 		fs_bdev_file_release(rtdev_file, mp->m_super);
545  out_close_logdev:
546 	if (logdev_file)
547 		fs_bdev_file_release(logdev_file, mp->m_super);
548 	return error;
549 }
550 
551 /*
552  * When using a RT device some or all data I/O is using the RT device, but
553  * the BDI is inherited from the main data device.  When the underlying block
554  * device for the RT device has larger I/O sizes, the BDI settings might be
555  * incorrect, which is especially bad if the main device is a SSD and the
556  * RT device is a HDD, as the io_opt fixup in blk_apply_bdi_limits is missing
557  * for this case.
558  *
559  * Update the BDI values to the max of the data and RT device to cover our
560  * bases.
561  */
562 static void
xfs_update_bdi_rahead(struct xfs_mount * mp)563 xfs_update_bdi_rahead(
564 	struct xfs_mount	*mp)
565 {
566 	struct backing_dev_info	*rt_bdi =
567 		mp->m_rtdev_targp->bt_bdev->bd_disk->bdi;
568 	struct backing_dev_info	*sb_bdi = mp->m_super->s_bdi;
569 
570 	mp->m_old_io_pages = sb_bdi->io_pages;
571 	mp->m_old_ra_pages = sb_bdi->ra_pages;
572 
573 	sb_bdi->io_pages = mp->m_initial_io_pages =
574 		max(sb_bdi->io_pages, rt_bdi->io_pages);
575 	sb_bdi->ra_pages = mp->m_initial_ra_pages =
576 		max(sb_bdi->ra_pages, rt_bdi->ra_pages);
577 }
578 
579 static void
xfs_restore_bdi_rahead(struct xfs_mount * mp)580 xfs_restore_bdi_rahead(
581 	struct xfs_mount	*mp)
582 {
583 	struct backing_dev_info	*sb_bdi = mp->m_super->s_bdi;
584 
585 	if (sb_bdi->io_pages == mp->m_initial_io_pages)
586 		sb_bdi->io_pages = mp->m_old_io_pages;
587 	else
588 		xfs_info(mp, "io_pages changed from %lu to %lu, not restoring.",
589 				mp->m_initial_io_pages, sb_bdi->io_pages);
590 	if (sb_bdi->ra_pages == mp->m_initial_ra_pages)
591 		sb_bdi->ra_pages = mp->m_old_ra_pages;
592 	else
593 		xfs_info(mp, "ra_pages changed from %lu to %lu, not restoring.",
594 				mp->m_initial_ra_pages, sb_bdi->ra_pages);
595 }
596 
597 /*
598  * Setup xfs_mount buffer target pointers based on superblock
599  */
600 STATIC int
xfs_setup_devices(struct xfs_mount * mp)601 xfs_setup_devices(
602 	struct xfs_mount	*mp)
603 {
604 	int			error;
605 
606 	error = xfs_configure_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize,
607 			mp->m_sb.sb_dblocks);
608 	if (error)
609 		return error;
610 
611 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
612 		unsigned int	log_sector_size = BBSIZE;
613 
614 		if (xfs_has_sector(mp))
615 			log_sector_size = mp->m_sb.sb_logsectsize;
616 		error = xfs_configure_buftarg(mp->m_logdev_targp,
617 				log_sector_size, mp->m_sb.sb_logblocks);
618 		if (error)
619 			return error;
620 	}
621 
622 	if (mp->m_sb.sb_rtstart) {
623 		if (mp->m_rtdev_targp) {
624 			xfs_warn(mp,
625 		"can't use internal and external rtdev at the same time");
626 			return -EINVAL;
627 		}
628 		mp->m_rtdev_targp = mp->m_ddev_targp;
629 	} else if (mp->m_rtname) {
630 		error = xfs_configure_buftarg(mp->m_rtdev_targp,
631 				mp->m_sb.sb_sectsize, mp->m_sb.sb_rblocks);
632 		if (error)
633 			return error;
634 		xfs_update_bdi_rahead(mp);
635 	}
636 
637 	return 0;
638 }
639 
640 STATIC int
xfs_init_mount_workqueues(struct xfs_mount * mp)641 xfs_init_mount_workqueues(
642 	struct xfs_mount	*mp)
643 {
644 	mp->m_buf_workqueue = alloc_workqueue("xfs-buf/%s",
645 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU),
646 			1, mp->m_super->s_id);
647 	if (!mp->m_buf_workqueue)
648 		goto out;
649 
650 	mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
651 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU),
652 			0, mp->m_super->s_id);
653 	if (!mp->m_unwritten_workqueue)
654 		goto out_destroy_buf;
655 
656 	mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
657 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU),
658 			0, mp->m_super->s_id);
659 	if (!mp->m_reclaim_workqueue)
660 		goto out_destroy_unwritten;
661 
662 	mp->m_blockgc_wq = alloc_workqueue("xfs-blockgc/%s",
663 			XFS_WQFLAGS(WQ_UNBOUND | WQ_FREEZABLE | WQ_MEM_RECLAIM),
664 			0, mp->m_super->s_id);
665 	if (!mp->m_blockgc_wq)
666 		goto out_destroy_reclaim;
667 
668 	mp->m_inodegc_wq = alloc_workqueue("xfs-inodegc/%s",
669 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | WQ_PERCPU),
670 			1, mp->m_super->s_id);
671 	if (!mp->m_inodegc_wq)
672 		goto out_destroy_blockgc;
673 
674 	mp->m_sync_workqueue = alloc_workqueue("xfs-sync/%s",
675 			XFS_WQFLAGS(WQ_FREEZABLE | WQ_PERCPU), 0,
676 			mp->m_super->s_id);
677 	if (!mp->m_sync_workqueue)
678 		goto out_destroy_inodegc;
679 
680 	return 0;
681 
682 out_destroy_inodegc:
683 	destroy_workqueue(mp->m_inodegc_wq);
684 out_destroy_blockgc:
685 	destroy_workqueue(mp->m_blockgc_wq);
686 out_destroy_reclaim:
687 	destroy_workqueue(mp->m_reclaim_workqueue);
688 out_destroy_unwritten:
689 	destroy_workqueue(mp->m_unwritten_workqueue);
690 out_destroy_buf:
691 	destroy_workqueue(mp->m_buf_workqueue);
692 out:
693 	return -ENOMEM;
694 }
695 
696 STATIC void
xfs_destroy_mount_workqueues(struct xfs_mount * mp)697 xfs_destroy_mount_workqueues(
698 	struct xfs_mount	*mp)
699 {
700 	destroy_workqueue(mp->m_sync_workqueue);
701 	destroy_workqueue(mp->m_blockgc_wq);
702 	destroy_workqueue(mp->m_inodegc_wq);
703 	destroy_workqueue(mp->m_reclaim_workqueue);
704 	destroy_workqueue(mp->m_unwritten_workqueue);
705 	destroy_workqueue(mp->m_buf_workqueue);
706 }
707 
708 static void
xfs_flush_inodes_worker(struct work_struct * work)709 xfs_flush_inodes_worker(
710 	struct work_struct	*work)
711 {
712 	struct xfs_mount	*mp = container_of(work, struct xfs_mount,
713 						   m_flush_inodes_work);
714 	struct super_block	*sb = mp->m_super;
715 
716 	if (down_read_trylock(&sb->s_umount)) {
717 		sync_inodes_sb(sb);
718 		up_read(&sb->s_umount);
719 	}
720 }
721 
722 /*
723  * Flush all dirty data to disk. Must not be called while holding an XFS_ILOCK
724  * or a page lock. We use sync_inodes_sb() here to ensure we block while waiting
725  * for IO to complete so that we effectively throttle multiple callers to the
726  * rate at which IO is completing.
727  */
728 void
xfs_flush_inodes(struct xfs_mount * mp)729 xfs_flush_inodes(
730 	struct xfs_mount	*mp)
731 {
732 	/*
733 	 * If flush_work() returns true then that means we waited for a flush
734 	 * which was already in progress.  Don't bother running another scan.
735 	 */
736 	if (flush_work(&mp->m_flush_inodes_work))
737 		return;
738 
739 	queue_work(mp->m_sync_workqueue, &mp->m_flush_inodes_work);
740 	flush_work(&mp->m_flush_inodes_work);
741 }
742 
743 /* Catch misguided souls that try to use this interface on XFS */
744 STATIC struct inode *
xfs_fs_alloc_inode(struct super_block * sb)745 xfs_fs_alloc_inode(
746 	struct super_block	*sb)
747 {
748 	BUG();
749 	return NULL;
750 }
751 
752 /*
753  * Now that the generic code is guaranteed not to be accessing
754  * the linux inode, we can inactivate and reclaim the inode.
755  */
756 STATIC void
xfs_fs_destroy_inode(struct inode * inode)757 xfs_fs_destroy_inode(
758 	struct inode		*inode)
759 {
760 	struct xfs_inode	*ip = XFS_I(inode);
761 
762 	trace_xfs_destroy_inode(ip);
763 
764 	ASSERT(!rwsem_is_locked(&inode->i_rwsem));
765 	XFS_STATS_INC(ip->i_mount, xs_inode_destroy);
766 	XFS_STATS_INC(ip->i_mount, xs_inode_destroy2);
767 	xfs_inode_mark_reclaimable(ip);
768 }
769 
770 /*
771  * Slab object creation initialisation for the XFS inode.
772  * This covers only the idempotent fields in the XFS inode;
773  * all other fields need to be initialised on allocation
774  * from the slab. This avoids the need to repeatedly initialise
775  * fields in the xfs inode that left in the initialise state
776  * when freeing the inode.
777  */
778 STATIC void
xfs_fs_inode_init_once(void * inode)779 xfs_fs_inode_init_once(
780 	void			*inode)
781 {
782 	struct xfs_inode	*ip = inode;
783 
784 	memset(ip, 0, sizeof(struct xfs_inode));
785 
786 	/* vfs inode */
787 	inode_init_once(VFS_I(ip));
788 
789 	/* xfs inode */
790 	atomic_set(&ip->i_pincount, 0);
791 	spin_lock_init(&ip->i_flags_lock);
792 	init_rwsem(&ip->i_lock);
793 }
794 
795 /*
796  * We do an unlocked check for XFS_IDONTCACHE here because we are already
797  * serialised against cache hits here via the inode->i_lock and igrab() in
798  * xfs_iget_cache_hit(). Hence a lookup that might clear this flag will not be
799  * racing with us, and it avoids needing to grab a spinlock here for every inode
800  * we drop the final reference on.
801  */
802 STATIC int
xfs_fs_drop_inode(struct inode * inode)803 xfs_fs_drop_inode(
804 	struct inode		*inode)
805 {
806 	struct xfs_inode	*ip = XFS_I(inode);
807 
808 	/*
809 	 * If this unlinked inode is in the middle of recovery, don't
810 	 * drop the inode just yet; log recovery will take care of
811 	 * that.  See the comment for this inode flag.
812 	 */
813 	if (ip->i_flags & XFS_IRECOVERY) {
814 		ASSERT(xlog_recovery_needed(ip->i_mount->m_log));
815 		return 0;
816 	}
817 
818 	return inode_generic_drop(inode);
819 }
820 
821 STATIC void
xfs_fs_evict_inode(struct inode * inode)822 xfs_fs_evict_inode(
823 	struct inode		*inode)
824 {
825 	if (IS_DAX(inode))
826 		dax_break_layout_final(inode);
827 
828 	truncate_inode_pages_final(&inode->i_data);
829 	clear_inode(inode);
830 
831 	if (IS_ENABLED(CONFIG_XFS_RT) &&
832 	    S_ISREG(inode->i_mode) && inode->i_private) {
833 		xfs_open_zone_put(inode->i_private);
834 		inode->i_private = NULL;
835 	}
836 }
837 
838 static void
xfs_mount_free(struct xfs_mount * mp)839 xfs_mount_free(
840 	struct xfs_mount	*mp)
841 {
842 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)
843 		xfs_free_buftarg(mp->m_logdev_targp);
844 	if (mp->m_rtdev_targp && mp->m_rtdev_targp != mp->m_ddev_targp)
845 		xfs_free_buftarg(mp->m_rtdev_targp);
846 	if (mp->m_ddev_targp)
847 		xfs_free_buftarg(mp->m_ddev_targp);
848 
849 	debugfs_remove(mp->m_debugfs);
850 	kfree(mp->m_rtname);
851 	kfree(mp->m_logname);
852 #ifdef DEBUG
853 	kfree(mp->m_errortag);
854 #endif
855 	kfree(mp);
856 }
857 
858 STATIC int
xfs_fs_sync_fs(struct super_block * sb,int wait)859 xfs_fs_sync_fs(
860 	struct super_block	*sb,
861 	int			wait)
862 {
863 	struct xfs_mount	*mp = XFS_M(sb);
864 	int			error;
865 
866 	trace_xfs_fs_sync_fs(mp, __return_address);
867 
868 	/*
869 	 * Doing anything during the async pass would be counterproductive.
870 	 */
871 	if (!wait)
872 		return 0;
873 
874 	error = xfs_log_force(mp, XFS_LOG_SYNC);
875 	if (error)
876 		return error;
877 
878 	/*
879 	 * If we are called with page faults frozen out, it means we are about
880 	 * to freeze the transaction subsystem. Take the opportunity to shut
881 	 * down inodegc because once SB_FREEZE_FS is set it's too late to
882 	 * prevent inactivation races with freeze. The fs doesn't get called
883 	 * again by the freezing process until after SB_FREEZE_FS has been set,
884 	 * so it's now or never.  Same logic applies to speculative allocation
885 	 * garbage collection.
886 	 *
887 	 * We don't care if this is a normal syncfs call that does this or
888 	 * freeze that does this - we can run this multiple times without issue
889 	 * and we won't race with a restart because a restart can only occur
890 	 * when the state is either SB_FREEZE_FS or SB_FREEZE_COMPLETE.
891 	 */
892 	if (sb->s_writers.frozen == SB_FREEZE_PAGEFAULT) {
893 		xfs_inodegc_stop(mp);
894 		xfs_blockgc_stop(mp);
895 		xfs_zone_gc_stop(mp);
896 	}
897 
898 	return 0;
899 }
900 
901 static xfs_extlen_t
xfs_internal_log_size(struct xfs_mount * mp)902 xfs_internal_log_size(
903 	struct xfs_mount	*mp)
904 {
905 	if (!mp->m_sb.sb_logstart)
906 		return 0;
907 	return mp->m_sb.sb_logblocks;
908 }
909 
910 static void
xfs_statfs_data(struct xfs_mount * mp,struct kstatfs * st)911 xfs_statfs_data(
912 	struct xfs_mount	*mp,
913 	struct kstatfs		*st)
914 {
915 	int64_t			fdblocks =
916 		xfs_sum_freecounter(mp, XC_FREE_BLOCKS);
917 
918 	/* make sure st->f_bfree does not underflow */
919 	st->f_bfree = max(0LL,
920 		fdblocks - xfs_freecounter_unavailable(mp, XC_FREE_BLOCKS));
921 
922 	/*
923 	 * sb_dblocks can change during growfs, but nothing cares about reporting
924 	 * the old or new value during growfs.
925 	 */
926 	st->f_blocks = mp->m_sb.sb_dblocks - xfs_internal_log_size(mp);
927 }
928 
929 /*
930  * When stat(v)fs is called on a file with the realtime bit set or a directory
931  * with the rtinherit bit, report freespace information for the RT device
932  * instead of the main data device.
933  */
934 static void
xfs_statfs_rt(struct xfs_mount * mp,struct kstatfs * st)935 xfs_statfs_rt(
936 	struct xfs_mount	*mp,
937 	struct kstatfs		*st)
938 {
939 	st->f_bfree = xfs_rtbxlen_to_blen(mp,
940 			xfs_sum_freecounter(mp, XC_FREE_RTEXTENTS));
941 	st->f_blocks = mp->m_sb.sb_rblocks - xfs_rtbxlen_to_blen(mp,
942 			mp->m_free[XC_FREE_RTEXTENTS].res_total);
943 }
944 
945 static void
xfs_statfs_inodes(struct xfs_mount * mp,struct kstatfs * st)946 xfs_statfs_inodes(
947 	struct xfs_mount	*mp,
948 	struct kstatfs		*st)
949 {
950 	uint64_t		icount = percpu_counter_sum(&mp->m_icount);
951 	uint64_t		ifree = percpu_counter_sum(&mp->m_ifree);
952 	uint64_t		fakeinos = XFS_FSB_TO_INO(mp, st->f_bfree);
953 
954 	st->f_files = min(icount + fakeinos, (uint64_t)XFS_MAXINUMBER);
955 	if (M_IGEO(mp)->maxicount)
956 		st->f_files = min_t(typeof(st->f_files), st->f_files,
957 					M_IGEO(mp)->maxicount);
958 
959 	/* If sb_icount overshot maxicount, report actual allocation */
960 	st->f_files = max_t(typeof(st->f_files), st->f_files,
961 			mp->m_sb.sb_icount);
962 
963 	/* Make sure st->f_ffree does not underflow */
964 	st->f_ffree = max_t(int64_t, 0, st->f_files - (icount - ifree));
965 }
966 
967 STATIC int
xfs_fs_statfs(struct dentry * dentry,struct kstatfs * st)968 xfs_fs_statfs(
969 	struct dentry		*dentry,
970 	struct kstatfs		*st)
971 {
972 	struct xfs_mount	*mp = XFS_M(dentry->d_sb);
973 	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
974 
975 	/*
976 	 * Expedite background inodegc but don't wait. We do not want to block
977 	 * here waiting hours for a billion extent file to be truncated.
978 	 */
979 	xfs_inodegc_push(mp);
980 
981 	st->f_type = XFS_SUPER_MAGIC;
982 	st->f_namelen = MAXNAMELEN - 1;
983 	st->f_bsize = mp->m_sb.sb_blocksize;
984 	st->f_fsid = u64_to_fsid(huge_encode_dev(mp->m_ddev_targp->bt_dev));
985 
986 	xfs_statfs_data(mp, st);
987 	xfs_statfs_inodes(mp, st);
988 
989 	if (XFS_IS_REALTIME_MOUNT(mp) &&
990 	    (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME)))
991 		xfs_statfs_rt(mp, st);
992 
993 	if ((ip->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
994 	    ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
995 			      (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
996 		xfs_qm_statvfs(ip, st);
997 
998 	/*
999 	 * XFS does not distinguish between blocks available to privileged and
1000 	 * unprivileged users.
1001 	 */
1002 	st->f_bavail = st->f_bfree;
1003 	return 0;
1004 }
1005 
1006 STATIC void
xfs_save_resvblks(struct xfs_mount * mp)1007 xfs_save_resvblks(
1008 	struct xfs_mount	*mp)
1009 {
1010 	enum xfs_free_counter	i;
1011 
1012 	for (i = 0; i < XC_FREE_NR; i++) {
1013 		mp->m_free[i].res_saved = mp->m_free[i].res_total;
1014 		xfs_reserve_blocks(mp, i, 0);
1015 	}
1016 }
1017 
1018 STATIC void
xfs_restore_resvblks(struct xfs_mount * mp)1019 xfs_restore_resvblks(
1020 	struct xfs_mount	*mp)
1021 {
1022 	uint64_t		resblks;
1023 	enum xfs_free_counter	i;
1024 
1025 	for (i = 0; i < XC_FREE_NR; i++) {
1026 		if (mp->m_free[i].res_saved) {
1027 			resblks = mp->m_free[i].res_saved;
1028 			mp->m_free[i].res_saved = 0;
1029 		} else
1030 			resblks = xfs_default_resblks(mp, i);
1031 		xfs_reserve_blocks(mp, i, resblks);
1032 	}
1033 }
1034 
1035 /*
1036  * Second stage of a freeze. The data is already frozen so we only
1037  * need to take care of the metadata. Once that's done sync the superblock
1038  * to the log to dirty it in case of a crash while frozen. This ensures that we
1039  * will recover the unlinked inode lists on the next mount.
1040  */
1041 STATIC int
xfs_fs_freeze(struct super_block * sb)1042 xfs_fs_freeze(
1043 	struct super_block	*sb)
1044 {
1045 	struct xfs_mount	*mp = XFS_M(sb);
1046 	unsigned int		flags;
1047 	int			ret;
1048 
1049 	/*
1050 	 * The filesystem is now frozen far enough that memory reclaim
1051 	 * cannot safely operate on the filesystem. Hence we need to
1052 	 * set a GFP_NOFS context here to avoid recursion deadlocks.
1053 	 */
1054 	flags = memalloc_nofs_save();
1055 	xfs_save_resvblks(mp);
1056 	ret = xfs_log_quiesce(mp);
1057 	memalloc_nofs_restore(flags);
1058 
1059 	/*
1060 	 * For read-write filesystems, we need to restart the inodegc on error
1061 	 * because we stopped it at SB_FREEZE_PAGEFAULT level and a thaw is not
1062 	 * going to be run to restart it now.  We are at SB_FREEZE_FS level
1063 	 * here, so we can restart safely without racing with a stop in
1064 	 * xfs_fs_sync_fs().
1065 	 */
1066 	if (ret && !xfs_is_readonly(mp)) {
1067 		xfs_blockgc_start(mp);
1068 		xfs_inodegc_start(mp);
1069 		xfs_zone_gc_start(mp);
1070 	}
1071 
1072 	return ret;
1073 }
1074 
1075 STATIC int
xfs_fs_unfreeze(struct super_block * sb)1076 xfs_fs_unfreeze(
1077 	struct super_block	*sb)
1078 {
1079 	struct xfs_mount	*mp = XFS_M(sb);
1080 
1081 	xfs_restore_resvblks(mp);
1082 	xfs_log_work_queue(mp);
1083 
1084 	/*
1085 	 * Don't reactivate the inodegc worker on a readonly filesystem because
1086 	 * inodes are sent directly to reclaim.  Don't reactivate the blockgc
1087 	 * worker because there are no speculative preallocations on a readonly
1088 	 * filesystem.
1089 	 */
1090 	if (!xfs_is_readonly(mp)) {
1091 		xfs_zone_gc_start(mp);
1092 		xfs_blockgc_start(mp);
1093 		xfs_inodegc_start(mp);
1094 	}
1095 
1096 	return 0;
1097 }
1098 
1099 /*
1100  * This function fills in xfs_mount_t fields based on mount args.
1101  * Note: the superblock _has_ now been read in.
1102  */
1103 STATIC int
xfs_finish_flags(struct xfs_mount * mp)1104 xfs_finish_flags(
1105 	struct xfs_mount	*mp)
1106 {
1107 	/* Fail a mount where the logbuf is smaller than the log stripe */
1108 	if (xfs_has_logv2(mp)) {
1109 		if (mp->m_logbsize <= 0 &&
1110 		    mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
1111 			mp->m_logbsize = mp->m_sb.sb_logsunit;
1112 		} else if (mp->m_logbsize > 0 &&
1113 			   mp->m_logbsize < mp->m_sb.sb_logsunit) {
1114 			xfs_warn(mp,
1115 		"logbuf size must be greater than or equal to log stripe size");
1116 			return -EINVAL;
1117 		}
1118 	} else {
1119 		/* Fail a mount if the logbuf is larger than 32K */
1120 		if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
1121 			xfs_warn(mp,
1122 		"logbuf size for version 1 logs must be 16K or 32K");
1123 			return -EINVAL;
1124 		}
1125 	}
1126 
1127 	/*
1128 	 * prohibit r/w mounts of read-only filesystems
1129 	 */
1130 	if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !xfs_is_readonly(mp)) {
1131 		xfs_warn(mp,
1132 			"cannot mount a read-only filesystem as read-write");
1133 		return -EROFS;
1134 	}
1135 
1136 	if ((mp->m_qflags & XFS_GQUOTA_ACCT) &&
1137 	    (mp->m_qflags & XFS_PQUOTA_ACCT) &&
1138 	    !xfs_has_pquotino(mp)) {
1139 		xfs_warn(mp,
1140 		  "Super block does not support project and group quota together");
1141 		return -EINVAL;
1142 	}
1143 
1144 	if (!xfs_has_zoned(mp)) {
1145 		if (mp->m_max_open_zones) {
1146 			xfs_warn(mp,
1147 "max_open_zones mount option only supported on zoned file systems.");
1148 			return -EINVAL;
1149 		}
1150 		if (mp->m_features & XFS_FEAT_NOLIFETIME) {
1151 			xfs_warn(mp,
1152 "nolifetime mount option only supported on zoned file systems.");
1153 			return -EINVAL;
1154 		}
1155 	}
1156 
1157 	return 0;
1158 }
1159 
1160 static int
xfs_init_percpu_counters(struct xfs_mount * mp)1161 xfs_init_percpu_counters(
1162 	struct xfs_mount	*mp)
1163 {
1164 	int			error;
1165 	int			i;
1166 
1167 	error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
1168 	if (error)
1169 		return -ENOMEM;
1170 
1171 	error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
1172 	if (error)
1173 		goto free_icount;
1174 
1175 	error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
1176 	if (error)
1177 		goto free_ifree;
1178 
1179 	error = percpu_counter_init(&mp->m_delalloc_rtextents, 0, GFP_KERNEL);
1180 	if (error)
1181 		goto free_delalloc;
1182 
1183 	for (i = 0; i < XC_FREE_NR; i++) {
1184 		error = percpu_counter_init(&mp->m_free[i].count, 0,
1185 				GFP_KERNEL);
1186 		if (error)
1187 			goto free_freecounters;
1188 	}
1189 
1190 	return 0;
1191 
1192 free_freecounters:
1193 	while (--i >= 0)
1194 		percpu_counter_destroy(&mp->m_free[i].count);
1195 	percpu_counter_destroy(&mp->m_delalloc_rtextents);
1196 free_delalloc:
1197 	percpu_counter_destroy(&mp->m_delalloc_blks);
1198 free_ifree:
1199 	percpu_counter_destroy(&mp->m_ifree);
1200 free_icount:
1201 	percpu_counter_destroy(&mp->m_icount);
1202 	return -ENOMEM;
1203 }
1204 
1205 void
xfs_reinit_percpu_counters(struct xfs_mount * mp)1206 xfs_reinit_percpu_counters(
1207 	struct xfs_mount	*mp)
1208 {
1209 	percpu_counter_set(&mp->m_icount, mp->m_sb.sb_icount);
1210 	percpu_counter_set(&mp->m_ifree, mp->m_sb.sb_ifree);
1211 	xfs_set_freecounter(mp, XC_FREE_BLOCKS, mp->m_sb.sb_fdblocks);
1212 	if (!xfs_has_zoned(mp))
1213 		xfs_set_freecounter(mp, XC_FREE_RTEXTENTS,
1214 				mp->m_sb.sb_frextents);
1215 }
1216 
1217 static void
xfs_destroy_percpu_counters(struct xfs_mount * mp)1218 xfs_destroy_percpu_counters(
1219 	struct xfs_mount	*mp)
1220 {
1221 	enum xfs_free_counter	i;
1222 
1223 	for (i = 0; i < XC_FREE_NR; i++)
1224 		percpu_counter_destroy(&mp->m_free[i].count);
1225 	percpu_counter_destroy(&mp->m_icount);
1226 	percpu_counter_destroy(&mp->m_ifree);
1227 	ASSERT(xfs_is_shutdown(mp) ||
1228 	       percpu_counter_sum(&mp->m_delalloc_rtextents) == 0);
1229 	percpu_counter_destroy(&mp->m_delalloc_rtextents);
1230 	ASSERT(xfs_is_shutdown(mp) ||
1231 	       percpu_counter_sum(&mp->m_delalloc_blks) == 0);
1232 	percpu_counter_destroy(&mp->m_delalloc_blks);
1233 }
1234 
1235 static int
xfs_inodegc_init_percpu(struct xfs_mount * mp)1236 xfs_inodegc_init_percpu(
1237 	struct xfs_mount	*mp)
1238 {
1239 	struct xfs_inodegc	*gc;
1240 	int			cpu;
1241 
1242 	mp->m_inodegc = alloc_percpu(struct xfs_inodegc);
1243 	if (!mp->m_inodegc)
1244 		return -ENOMEM;
1245 
1246 	for_each_possible_cpu(cpu) {
1247 		gc = per_cpu_ptr(mp->m_inodegc, cpu);
1248 		gc->cpu = cpu;
1249 		gc->mp = mp;
1250 		init_llist_head(&gc->list);
1251 		gc->items = 0;
1252 		gc->error = 0;
1253 		INIT_DELAYED_WORK(&gc->work, xfs_inodegc_worker);
1254 	}
1255 	return 0;
1256 }
1257 
1258 static void
xfs_inodegc_free_percpu(struct xfs_mount * mp)1259 xfs_inodegc_free_percpu(
1260 	struct xfs_mount	*mp)
1261 {
1262 	if (!mp->m_inodegc)
1263 		return;
1264 	free_percpu(mp->m_inodegc);
1265 }
1266 
1267 static void
xfs_fs_put_super(struct super_block * sb)1268 xfs_fs_put_super(
1269 	struct super_block	*sb)
1270 {
1271 	struct xfs_mount	*mp = XFS_M(sb);
1272 
1273 	xfs_notice(mp, "Unmounting Filesystem %pU", &mp->m_sb.sb_uuid);
1274 	xfs_filestream_unmount(mp);
1275 	xfs_unmountfs(mp);
1276 
1277 	xfs_rtmount_freesb(mp);
1278 	xfs_freesb(mp);
1279 	xchk_mount_stats_free(mp);
1280 	free_percpu(mp->m_stats.xs_stats);
1281 	xfs_inodegc_free_percpu(mp);
1282 	xfs_destroy_percpu_counters(mp);
1283 	xfs_destroy_mount_workqueues(mp);
1284 	xfs_shutdown_devices(mp);
1285 }
1286 
1287 static long
xfs_fs_nr_cached_objects(struct super_block * sb,struct shrink_control * sc)1288 xfs_fs_nr_cached_objects(
1289 	struct super_block	*sb,
1290 	struct shrink_control	*sc)
1291 {
1292 	/* Paranoia: catch incorrect calls during mount setup or teardown */
1293 	if (WARN_ON_ONCE(!sb->s_fs_info))
1294 		return 0;
1295 	return xfs_reclaim_inodes_count(XFS_M(sb));
1296 }
1297 
1298 static long
xfs_fs_free_cached_objects(struct super_block * sb,struct shrink_control * sc)1299 xfs_fs_free_cached_objects(
1300 	struct super_block	*sb,
1301 	struct shrink_control	*sc)
1302 {
1303 	return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
1304 }
1305 
1306 static void
xfs_fs_shutdown(struct super_block * sb)1307 xfs_fs_shutdown(
1308 	struct super_block	*sb)
1309 {
1310 	xfs_force_shutdown(XFS_M(sb), SHUTDOWN_DEVICE_REMOVED);
1311 }
1312 
1313 static int
xfs_fs_show_stats(struct seq_file * m,struct dentry * root)1314 xfs_fs_show_stats(
1315 	struct seq_file		*m,
1316 	struct dentry		*root)
1317 {
1318 	struct xfs_mount	*mp = XFS_M(root->d_sb);
1319 
1320 	if (xfs_has_zoned(mp) && IS_ENABLED(CONFIG_XFS_RT))
1321 		xfs_zoned_show_stats(m, mp);
1322 	return 0;
1323 }
1324 
1325 static void
xfs_fs_report_error(const struct fserror_event * event)1326 xfs_fs_report_error(
1327 	const struct fserror_event	*event)
1328 {
1329 	/* healthmon already knows about non-inode and metadata errors */
1330 	if (event->inode && event->type != FSERR_METADATA)
1331 		xfs_healthmon_report_file_ioerror(XFS_I(event->inode), event);
1332 }
1333 
1334 static const struct super_operations xfs_super_operations = {
1335 	.alloc_inode		= xfs_fs_alloc_inode,
1336 	.destroy_inode		= xfs_fs_destroy_inode,
1337 	.drop_inode		= xfs_fs_drop_inode,
1338 	.evict_inode		= xfs_fs_evict_inode,
1339 	.put_super		= xfs_fs_put_super,
1340 	.sync_fs		= xfs_fs_sync_fs,
1341 	.freeze_fs		= xfs_fs_freeze,
1342 	.unfreeze_fs		= xfs_fs_unfreeze,
1343 	.statfs			= xfs_fs_statfs,
1344 	.show_options		= xfs_fs_show_options,
1345 	.nr_cached_objects	= xfs_fs_nr_cached_objects,
1346 	.free_cached_objects	= xfs_fs_free_cached_objects,
1347 	.shutdown		= xfs_fs_shutdown,
1348 	.show_stats		= xfs_fs_show_stats,
1349 	.report_error		= xfs_fs_report_error,
1350 };
1351 
1352 static int
suffix_kstrtoint(const char * s,unsigned int base,int * res)1353 suffix_kstrtoint(
1354 	const char	*s,
1355 	unsigned int	base,
1356 	int		*res)
1357 {
1358 	int		last, shift_left_factor = 0, _res;
1359 	char		*value;
1360 	int		ret = 0;
1361 
1362 	value = kstrdup(s, GFP_KERNEL);
1363 	if (!value)
1364 		return -ENOMEM;
1365 
1366 	last = strlen(value) - 1;
1367 	if (value[last] == 'K' || value[last] == 'k') {
1368 		shift_left_factor = 10;
1369 		value[last] = '\0';
1370 	}
1371 	if (value[last] == 'M' || value[last] == 'm') {
1372 		shift_left_factor = 20;
1373 		value[last] = '\0';
1374 	}
1375 	if (value[last] == 'G' || value[last] == 'g') {
1376 		shift_left_factor = 30;
1377 		value[last] = '\0';
1378 	}
1379 
1380 	if (kstrtoint(value, base, &_res))
1381 		ret = -EINVAL;
1382 	kfree(value);
1383 	*res = _res << shift_left_factor;
1384 	return ret;
1385 }
1386 
1387 static int
suffix_kstrtoull(const char * s,unsigned int base,unsigned long long * res)1388 suffix_kstrtoull(
1389 	const char		*s,
1390 	unsigned int		base,
1391 	unsigned long long	*res)
1392 {
1393 	int			last, shift_left_factor = 0;
1394 	unsigned long long	_res;
1395 	char			*value;
1396 	int			ret = 0;
1397 
1398 	value = kstrdup(s, GFP_KERNEL);
1399 	if (!value)
1400 		return -ENOMEM;
1401 
1402 	last = strlen(value) - 1;
1403 	if (value[last] == 'K' || value[last] == 'k') {
1404 		shift_left_factor = 10;
1405 		value[last] = '\0';
1406 	}
1407 	if (value[last] == 'M' || value[last] == 'm') {
1408 		shift_left_factor = 20;
1409 		value[last] = '\0';
1410 	}
1411 	if (value[last] == 'G' || value[last] == 'g') {
1412 		shift_left_factor = 30;
1413 		value[last] = '\0';
1414 	}
1415 
1416 	if (kstrtoull(value, base, &_res))
1417 		ret = -EINVAL;
1418 	kfree(value);
1419 	*res = _res << shift_left_factor;
1420 	return ret;
1421 }
1422 
1423 static inline void
xfs_fs_warn_deprecated(struct fs_context * fc,struct fs_parameter * param)1424 xfs_fs_warn_deprecated(
1425 	struct fs_context	*fc,
1426 	struct fs_parameter	*param)
1427 {
1428 	/*
1429 	 * Always warn about someone passing in a deprecated mount option.
1430 	 * Previously we wouldn't print the warning if we were reconfiguring
1431 	 * and current mount point already had the flag set, but that was not
1432 	 * the right thing to do.
1433 	 *
1434 	 * Many distributions mount the root filesystem with no options in the
1435 	 * initramfs and rely on mount -a to remount the root fs with the
1436 	 * options in fstab.  However, the old behavior meant that there would
1437 	 * never be a warning about deprecated mount options for the root fs in
1438 	 * /etc/fstab.  On a single-fs system, that means no warning at all.
1439 	 *
1440 	 * Compounding this problem are distribution scripts that copy
1441 	 * /proc/mounts to fstab, which means that we can't remove mount
1442 	 * options unless we're 100% sure they have only ever been advertised
1443 	 * in /proc/mounts in response to explicitly provided mount options.
1444 	 */
1445 	xfs_warn(fc->s_fs_info, "%s mount option is deprecated.", param->key);
1446 }
1447 
1448 /*
1449  * Set mount state from a mount option.
1450  *
1451  * NOTE: mp->m_super is NULL here!
1452  */
1453 static int
xfs_fs_parse_param(struct fs_context * fc,struct fs_parameter * param)1454 xfs_fs_parse_param(
1455 	struct fs_context	*fc,
1456 	struct fs_parameter	*param)
1457 {
1458 	struct xfs_mount	*parsing_mp = fc->s_fs_info;
1459 	struct fs_parse_result	result;
1460 	int			size = 0;
1461 	int			opt;
1462 
1463 	BUILD_BUG_ON(XFS_QFLAGS_MNTOPTS & XFS_MOUNT_QUOTA_ALL);
1464 
1465 	opt = fs_parse(fc, xfs_fs_parameters, param, &result);
1466 	if (opt < 0)
1467 		return opt;
1468 
1469 	switch (opt) {
1470 	case Op_deprecated:
1471 		xfs_fs_warn_deprecated(fc, param);
1472 		return 0;
1473 	case Opt_logbufs:
1474 		parsing_mp->m_logbufs = result.uint_32;
1475 		return 0;
1476 	case Opt_logbsize:
1477 		if (suffix_kstrtoint(param->string, 10, &parsing_mp->m_logbsize))
1478 			return -EINVAL;
1479 		return 0;
1480 	case Opt_logdev:
1481 		kfree(parsing_mp->m_logname);
1482 		parsing_mp->m_logname = kstrdup(param->string, GFP_KERNEL);
1483 		if (!parsing_mp->m_logname)
1484 			return -ENOMEM;
1485 		return 0;
1486 	case Opt_rtdev:
1487 		kfree(parsing_mp->m_rtname);
1488 		parsing_mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
1489 		if (!parsing_mp->m_rtname)
1490 			return -ENOMEM;
1491 		return 0;
1492 	case Opt_allocsize:
1493 		if (suffix_kstrtoint(param->string, 10, &size))
1494 			return -EINVAL;
1495 		parsing_mp->m_allocsize_log = ffs(size) - 1;
1496 		parsing_mp->m_features |= XFS_FEAT_ALLOCSIZE;
1497 		return 0;
1498 	case Opt_grpid:
1499 	case Opt_bsdgroups:
1500 		parsing_mp->m_features |= XFS_FEAT_GRPID;
1501 		return 0;
1502 	case Opt_nogrpid:
1503 	case Opt_sysvgroups:
1504 		parsing_mp->m_features &= ~XFS_FEAT_GRPID;
1505 		return 0;
1506 	case Opt_wsync:
1507 		parsing_mp->m_features |= XFS_FEAT_WSYNC;
1508 		return 0;
1509 	case Opt_norecovery:
1510 		parsing_mp->m_features |= XFS_FEAT_NORECOVERY;
1511 		return 0;
1512 	case Opt_noalign:
1513 		parsing_mp->m_features |= XFS_FEAT_NOALIGN;
1514 		return 0;
1515 	case Opt_swalloc:
1516 		parsing_mp->m_features |= XFS_FEAT_SWALLOC;
1517 		return 0;
1518 	case Opt_sunit:
1519 		parsing_mp->m_dalign = result.uint_32;
1520 		return 0;
1521 	case Opt_swidth:
1522 		parsing_mp->m_swidth = result.uint_32;
1523 		return 0;
1524 	case Opt_inode32:
1525 		parsing_mp->m_features |= XFS_FEAT_SMALL_INUMS;
1526 		return 0;
1527 	case Opt_inode64:
1528 		parsing_mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1529 		return 0;
1530 	case Opt_nouuid:
1531 		parsing_mp->m_features |= XFS_FEAT_NOUUID;
1532 		return 0;
1533 	case Opt_largeio:
1534 		parsing_mp->m_features |= XFS_FEAT_LARGE_IOSIZE;
1535 		return 0;
1536 	case Opt_nolargeio:
1537 		parsing_mp->m_features &= ~XFS_FEAT_LARGE_IOSIZE;
1538 		return 0;
1539 	case Opt_filestreams:
1540 		parsing_mp->m_features |= XFS_FEAT_FILESTREAMS;
1541 		return 0;
1542 	case Opt_noquota:
1543 		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
1544 		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
1545 		parsing_mp->m_qflags |= XFS_QFLAGS_MNTOPTS;
1546 		return 0;
1547 	case Opt_quota:
1548 	case Opt_uquota:
1549 	case Opt_usrquota:
1550 		parsing_mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ENFD);
1551 		parsing_mp->m_qflags |= XFS_QFLAGS_MNTOPTS;
1552 		return 0;
1553 	case Opt_qnoenforce:
1554 	case Opt_uqnoenforce:
1555 		parsing_mp->m_qflags |= XFS_UQUOTA_ACCT;
1556 		parsing_mp->m_qflags &= ~XFS_UQUOTA_ENFD;
1557 		parsing_mp->m_qflags |= XFS_QFLAGS_MNTOPTS;
1558 		return 0;
1559 	case Opt_pquota:
1560 	case Opt_prjquota:
1561 		parsing_mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ENFD);
1562 		parsing_mp->m_qflags |= XFS_QFLAGS_MNTOPTS;
1563 		return 0;
1564 	case Opt_pqnoenforce:
1565 		parsing_mp->m_qflags |= XFS_PQUOTA_ACCT;
1566 		parsing_mp->m_qflags &= ~XFS_PQUOTA_ENFD;
1567 		parsing_mp->m_qflags |= XFS_QFLAGS_MNTOPTS;
1568 		return 0;
1569 	case Opt_gquota:
1570 	case Opt_grpquota:
1571 		parsing_mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ENFD);
1572 		parsing_mp->m_qflags |= XFS_QFLAGS_MNTOPTS;
1573 		return 0;
1574 	case Opt_gqnoenforce:
1575 		parsing_mp->m_qflags |= XFS_GQUOTA_ACCT;
1576 		parsing_mp->m_qflags &= ~XFS_GQUOTA_ENFD;
1577 		parsing_mp->m_qflags |= XFS_QFLAGS_MNTOPTS;
1578 		return 0;
1579 	case Opt_discard:
1580 		parsing_mp->m_features |= XFS_FEAT_DISCARD;
1581 		return 0;
1582 	case Opt_nodiscard:
1583 		parsing_mp->m_features &= ~XFS_FEAT_DISCARD;
1584 		return 0;
1585 #ifdef CONFIG_FS_DAX
1586 	case Opt_dax:
1587 		xfs_mount_set_dax_mode(parsing_mp, XFS_DAX_ALWAYS);
1588 		return 0;
1589 	case Opt_dax_enum:
1590 		xfs_mount_set_dax_mode(parsing_mp, result.uint_32);
1591 		return 0;
1592 #endif
1593 	case Opt_max_open_zones:
1594 		parsing_mp->m_max_open_zones = result.uint_32;
1595 		return 0;
1596 	case Opt_lifetime:
1597 		parsing_mp->m_features &= ~XFS_FEAT_NOLIFETIME;
1598 		return 0;
1599 	case Opt_nolifetime:
1600 		parsing_mp->m_features |= XFS_FEAT_NOLIFETIME;
1601 		return 0;
1602 	case Opt_max_atomic_write:
1603 		if (suffix_kstrtoull(param->string, 10,
1604 				     &parsing_mp->m_awu_max_bytes)) {
1605 			xfs_warn(parsing_mp,
1606  "max atomic write size must be positive integer");
1607 			return -EINVAL;
1608 		}
1609 		return 0;
1610 	case Opt_errortag:
1611 		return xfs_errortag_add_name(parsing_mp, param->string);
1612 	default:
1613 		xfs_warn(parsing_mp, "unknown mount option [%s].", param->key);
1614 		return -EINVAL;
1615 	}
1616 
1617 	return 0;
1618 }
1619 
1620 static int
xfs_fs_validate_params(struct xfs_mount * mp)1621 xfs_fs_validate_params(
1622 	struct xfs_mount	*mp)
1623 {
1624 	/* No recovery flag requires a read-only mount */
1625 	if (xfs_has_norecovery(mp) && !xfs_is_readonly(mp)) {
1626 		xfs_warn(mp, "no-recovery mounts must be read-only.");
1627 		return -EINVAL;
1628 	}
1629 
1630 	if (xfs_has_noalign(mp) && (mp->m_dalign || mp->m_swidth)) {
1631 		xfs_warn(mp,
1632 	"sunit and swidth options incompatible with the noalign option");
1633 		return -EINVAL;
1634 	}
1635 
1636 	if (!IS_ENABLED(CONFIG_XFS_QUOTA) &&
1637 	    (mp->m_qflags & ~XFS_QFLAGS_MNTOPTS)) {
1638 		xfs_warn(mp, "quota support not available in this kernel.");
1639 		return -EINVAL;
1640 	}
1641 
1642 	if ((mp->m_dalign && !mp->m_swidth) ||
1643 	    (!mp->m_dalign && mp->m_swidth)) {
1644 		xfs_warn(mp, "sunit and swidth must be specified together");
1645 		return -EINVAL;
1646 	}
1647 
1648 	if (mp->m_dalign && (mp->m_swidth % mp->m_dalign != 0)) {
1649 		xfs_warn(mp,
1650 	"stripe width (%d) must be a multiple of the stripe unit (%d)",
1651 			mp->m_swidth, mp->m_dalign);
1652 		return -EINVAL;
1653 	}
1654 
1655 	if (mp->m_logbufs != -1 &&
1656 	    mp->m_logbufs != 0 &&
1657 	    (mp->m_logbufs < XLOG_MIN_ICLOGS ||
1658 	     mp->m_logbufs > XLOG_MAX_ICLOGS)) {
1659 		xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
1660 			mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
1661 		return -EINVAL;
1662 	}
1663 
1664 	if (mp->m_logbsize != -1 &&
1665 	    mp->m_logbsize !=  0 &&
1666 	    (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
1667 	     mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
1668 	     !is_power_of_2(mp->m_logbsize))) {
1669 		xfs_warn(mp,
1670 			"invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
1671 			mp->m_logbsize);
1672 		return -EINVAL;
1673 	}
1674 
1675 	if (xfs_has_allocsize(mp) &&
1676 	    (mp->m_allocsize_log > XFS_MAX_IO_LOG ||
1677 	     mp->m_allocsize_log < XFS_MIN_IO_LOG)) {
1678 		xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
1679 			mp->m_allocsize_log, XFS_MIN_IO_LOG, XFS_MAX_IO_LOG);
1680 		return -EINVAL;
1681 	}
1682 
1683 	return 0;
1684 }
1685 
1686 struct dentry *
xfs_debugfs_mkdir(const char * name,struct dentry * parent)1687 xfs_debugfs_mkdir(
1688 	const char	*name,
1689 	struct dentry	*parent)
1690 {
1691 	struct dentry	*child;
1692 
1693 	/* Apparently we're expected to ignore error returns?? */
1694 	child = debugfs_create_dir(name, parent);
1695 	if (IS_ERR(child))
1696 		return NULL;
1697 
1698 	return child;
1699 }
1700 
1701 static int
xfs_fs_fill_super(struct super_block * sb,struct fs_context * fc)1702 xfs_fs_fill_super(
1703 	struct super_block	*sb,
1704 	struct fs_context	*fc)
1705 {
1706 	struct xfs_mount	*mp = sb->s_fs_info;
1707 	struct inode		*root;
1708 	int			flags = 0, error;
1709 
1710 	mp->m_super = sb;
1711 
1712 	/*
1713 	 * Copy VFS mount flags from the context now that all parameter parsing
1714 	 * is guaranteed to have been completed by either the old mount API or
1715 	 * the newer fsopen/fsconfig API.
1716 	 */
1717 	if (fc->sb_flags & SB_RDONLY)
1718 		xfs_set_readonly(mp);
1719 	if (fc->sb_flags & SB_DIRSYNC)
1720 		mp->m_features |= XFS_FEAT_DIRSYNC;
1721 	if (fc->sb_flags & SB_SYNCHRONOUS)
1722 		mp->m_features |= XFS_FEAT_WSYNC;
1723 
1724 	error = xfs_fs_validate_params(mp);
1725 	if (error)
1726 		return error;
1727 
1728 	if (!sb_min_blocksize(sb, BBSIZE)) {
1729 		xfs_err(mp, "unable to set blocksize");
1730 		return -EINVAL;
1731 	}
1732 	sb->s_xattr = xfs_xattr_handlers;
1733 	sb->s_export_op = &xfs_export_operations;
1734 #ifdef CONFIG_XFS_QUOTA
1735 	sb->s_qcop = &xfs_quotactl_operations;
1736 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1737 #endif
1738 	sb->s_op = &xfs_super_operations;
1739 
1740 	/*
1741 	 * Delay mount work if the debug hook is set. This is debug
1742 	 * instrumention to coordinate simulation of xfs mount failures with
1743 	 * VFS superblock operations
1744 	 */
1745 	if (xfs_globals.mount_delay) {
1746 		xfs_notice(mp, "Delaying mount for %d seconds.",
1747 			xfs_globals.mount_delay);
1748 		msleep(xfs_globals.mount_delay * 1000);
1749 	}
1750 
1751 	if (fc->sb_flags & SB_SILENT)
1752 		flags |= XFS_MFSI_QUIET;
1753 
1754 	error = xfs_open_devices(mp);
1755 	if (error)
1756 		return error;
1757 
1758 	if (xfs_debugfs) {
1759 		mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
1760 						  xfs_debugfs);
1761 	} else {
1762 		mp->m_debugfs = NULL;
1763 	}
1764 
1765 	error = xfs_init_mount_workqueues(mp);
1766 	if (error)
1767 		goto out_shutdown_devices;
1768 
1769 	error = xfs_init_percpu_counters(mp);
1770 	if (error)
1771 		goto out_destroy_workqueues;
1772 
1773 	error = xfs_inodegc_init_percpu(mp);
1774 	if (error)
1775 		goto out_destroy_counters;
1776 
1777 	/* Allocate stats memory before we do operations that might use it */
1778 	mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
1779 	if (!mp->m_stats.xs_stats) {
1780 		error = -ENOMEM;
1781 		goto out_destroy_inodegc;
1782 	}
1783 
1784 	error = xchk_mount_stats_alloc(mp);
1785 	if (error)
1786 		goto out_free_stats;
1787 
1788 	error = xfs_readsb(mp, flags);
1789 	if (error)
1790 		goto out_free_scrub_stats;
1791 
1792 	error = xfs_finish_flags(mp);
1793 	if (error)
1794 		goto out_free_sb;
1795 
1796 	error = xfs_setup_devices(mp);
1797 	if (error)
1798 		goto out_free_sb;
1799 
1800 	/*
1801 	 * V4 support is undergoing deprecation.
1802 	 *
1803 	 * Note: this has to use an open coded m_features check as xfs_has_crc
1804 	 * always returns false for !CONFIG_XFS_SUPPORT_V4.
1805 	 */
1806 	if (!(mp->m_features & XFS_FEAT_CRC)) {
1807 		if (!IS_ENABLED(CONFIG_XFS_SUPPORT_V4)) {
1808 			xfs_warn(mp,
1809 	"Deprecated V4 format (crc=0) not supported by kernel.");
1810 			error = -EINVAL;
1811 			goto out_free_sb;
1812 		}
1813 		xfs_warn_once(mp,
1814 	"Deprecated V4 format (crc=0) will not be supported after September 2030.");
1815 	}
1816 
1817 	/* ASCII case insensitivity is undergoing deprecation. */
1818 	if (xfs_has_asciici(mp)) {
1819 #ifdef CONFIG_XFS_SUPPORT_ASCII_CI
1820 		xfs_warn_once(mp,
1821 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) will not be supported after September 2030.");
1822 #else
1823 		xfs_warn(mp,
1824 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) not supported by kernel.");
1825 		error = -EINVAL;
1826 		goto out_free_sb;
1827 #endif
1828 	}
1829 
1830 	/*
1831 	 * Filesystem claims it needs repair, so refuse the mount unless
1832 	 * norecovery is also specified, in which case the filesystem can
1833 	 * be mounted with no risk of further damage.
1834 	 */
1835 	if (xfs_has_needsrepair(mp) && !xfs_has_norecovery(mp)) {
1836 		xfs_warn(mp, "Filesystem needs repair.  Please run xfs_repair.");
1837 		error = -EFSCORRUPTED;
1838 		goto out_free_sb;
1839 	}
1840 
1841 	/*
1842 	 * Don't touch the filesystem if a user tool thinks it owns the primary
1843 	 * superblock.  mkfs doesn't clear the flag from secondary supers, so
1844 	 * we don't check them at all.
1845 	 */
1846 	if (mp->m_sb.sb_inprogress) {
1847 		xfs_warn(mp, "Offline file system operation in progress!");
1848 		error = -EFSCORRUPTED;
1849 		goto out_free_sb;
1850 	}
1851 
1852 	if (mp->m_sb.sb_blocksize > PAGE_SIZE) {
1853 		size_t max_folio_size = mapping_max_folio_size_supported();
1854 
1855 		if (!xfs_has_crc(mp)) {
1856 			xfs_warn(mp,
1857 "V4 Filesystem with blocksize %d bytes. Only pagesize (%ld) or less is supported.",
1858 				mp->m_sb.sb_blocksize, PAGE_SIZE);
1859 			error = -ENOSYS;
1860 			goto out_free_sb;
1861 		}
1862 
1863 		if (mp->m_sb.sb_blocksize > max_folio_size) {
1864 			xfs_warn(mp,
1865 "block size (%u bytes) not supported; Only block size (%zu) or less is supported",
1866 				mp->m_sb.sb_blocksize, max_folio_size);
1867 			error = -ENOSYS;
1868 			goto out_free_sb;
1869 		}
1870 	}
1871 
1872 	/* Ensure this filesystem fits in the page cache limits */
1873 	if (xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_dblocks) ||
1874 	    xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_rblocks)) {
1875 		xfs_warn(mp,
1876 		"file system too large to be mounted on this system.");
1877 		error = -EFBIG;
1878 		goto out_free_sb;
1879 	}
1880 
1881 	/*
1882 	 * XFS block mappings use 54 bits to store the logical block offset.
1883 	 * This should suffice to handle the maximum file size that the VFS
1884 	 * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT
1885 	 * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes
1886 	 * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON
1887 	 * to check this assertion.
1888 	 *
1889 	 * Avoid integer overflow by comparing the maximum bmbt offset to the
1890 	 * maximum pagecache offset in units of fs blocks.
1891 	 */
1892 	if (!xfs_verify_fileoff(mp, XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE))) {
1893 		xfs_warn(mp,
1894 "MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!",
1895 			 XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE),
1896 			 XFS_MAX_FILEOFF);
1897 		error = -EINVAL;
1898 		goto out_free_sb;
1899 	}
1900 
1901 	error = xfs_rtmount_readsb(mp);
1902 	if (error)
1903 		goto out_free_sb;
1904 
1905 	error = xfs_filestream_mount(mp);
1906 	if (error)
1907 		goto out_free_rtsb;
1908 
1909 	/*
1910 	 * we must configure the block size in the superblock before we run the
1911 	 * full mount process as the mount process can lookup and cache inodes.
1912 	 */
1913 	sb->s_magic = XFS_SUPER_MAGIC;
1914 	sb->s_blocksize = mp->m_sb.sb_blocksize;
1915 	sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1916 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1917 	sb->s_max_links = XFS_MAXLINK;
1918 	sb->s_time_gran = 1;
1919 	if (xfs_has_bigtime(mp)) {
1920 		sb->s_time_min = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MIN);
1921 		sb->s_time_max = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MAX);
1922 	} else {
1923 		sb->s_time_min = XFS_LEGACY_TIME_MIN;
1924 		sb->s_time_max = XFS_LEGACY_TIME_MAX;
1925 	}
1926 	trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
1927 	sb->s_iflags |= SB_I_CGROUPWB | SB_I_ALLOW_HSM;
1928 
1929 	set_posix_acl_flag(sb);
1930 
1931 	/* version 5 superblocks support inode version counters. */
1932 	if (xfs_has_crc(mp))
1933 		sb->s_flags |= SB_I_VERSION;
1934 
1935 	if (xfs_has_dax_always(mp)) {
1936 		error = xfs_setup_dax_always(mp);
1937 		if (error)
1938 			goto out_filestream_unmount;
1939 	}
1940 
1941 	if (xfs_has_discard(mp) && !bdev_max_discard_sectors(sb->s_bdev)) {
1942 		xfs_warn(mp,
1943 	"mounting with \"discard\" option, but the device does not support discard");
1944 		mp->m_features &= ~XFS_FEAT_DISCARD;
1945 	}
1946 
1947 	if (xfs_has_zoned(mp)) {
1948 		if (!xfs_has_metadir(mp)) {
1949 			xfs_alert(mp,
1950 		"metadir feature required for zoned realtime devices.");
1951 			error = -EINVAL;
1952 			goto out_filestream_unmount;
1953 		}
1954 	}
1955 
1956 	if (xfs_has_reflink(mp)) {
1957 		if (xfs_has_realtime(mp) &&
1958 		    !xfs_reflink_supports_rextsize(mp, mp->m_sb.sb_rextsize)) {
1959 			xfs_alert(mp,
1960 	"reflink not compatible with realtime extent size %u!",
1961 					mp->m_sb.sb_rextsize);
1962 			error = -EINVAL;
1963 			goto out_filestream_unmount;
1964 		}
1965 
1966 		if (xfs_has_zoned(mp)) {
1967 			xfs_alert(mp,
1968 	"reflink not compatible with zoned RT device!");
1969 			error = -EINVAL;
1970 			goto out_filestream_unmount;
1971 		}
1972 
1973 		if (xfs_globals.always_cow) {
1974 			xfs_info(mp, "using DEBUG-only always_cow mode.");
1975 			mp->m_always_cow = true;
1976 		}
1977 	}
1978 
1979 	/*
1980 	 * If no quota mount options were provided, maybe we'll try to pick
1981 	 * up the quota accounting and enforcement flags from the ondisk sb.
1982 	 */
1983 	if (!(mp->m_qflags & XFS_QFLAGS_MNTOPTS))
1984 		xfs_set_resuming_quotaon(mp);
1985 	mp->m_qflags &= ~XFS_QFLAGS_MNTOPTS;
1986 
1987 	error = xfs_mountfs(mp);
1988 	if (error)
1989 		goto out_filestream_unmount;
1990 
1991 	root = igrab(VFS_I(mp->m_rootip));
1992 	if (!root) {
1993 		error = -ENOENT;
1994 		goto out_unmount;
1995 	}
1996 	sb->s_root = d_make_root(root);
1997 	if (!sb->s_root) {
1998 		error = -ENOMEM;
1999 		goto out_unmount;
2000 	}
2001 
2002 	return 0;
2003 
2004  out_filestream_unmount:
2005 	xfs_filestream_unmount(mp);
2006  out_free_rtsb:
2007 	xfs_rtmount_freesb(mp);
2008  out_free_sb:
2009 	xfs_freesb(mp);
2010  out_free_scrub_stats:
2011 	xchk_mount_stats_free(mp);
2012  out_free_stats:
2013 	free_percpu(mp->m_stats.xs_stats);
2014  out_destroy_inodegc:
2015 	xfs_inodegc_free_percpu(mp);
2016  out_destroy_counters:
2017 	xfs_destroy_percpu_counters(mp);
2018  out_destroy_workqueues:
2019 	xfs_destroy_mount_workqueues(mp);
2020  out_shutdown_devices:
2021 	xfs_shutdown_devices(mp);
2022 	return error;
2023 
2024  out_unmount:
2025 	xfs_filestream_unmount(mp);
2026 	xfs_unmountfs(mp);
2027 	goto out_free_rtsb;
2028 }
2029 
2030 static int
xfs_fs_get_tree(struct fs_context * fc)2031 xfs_fs_get_tree(
2032 	struct fs_context	*fc)
2033 {
2034 	return get_tree_bdev(fc, xfs_fs_fill_super);
2035 }
2036 
2037 static int
xfs_remount_rw(struct xfs_mount * mp)2038 xfs_remount_rw(
2039 	struct xfs_mount	*mp)
2040 {
2041 	struct xfs_sb		*sbp = &mp->m_sb;
2042 	int error;
2043 
2044 	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp &&
2045 	    xfs_readonly_buftarg(mp->m_logdev_targp)) {
2046 		xfs_warn(mp,
2047 			"ro->rw transition prohibited by read-only logdev");
2048 		return -EACCES;
2049 	}
2050 
2051 	if (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp)) {
2052 		xfs_warn(mp,
2053 			"ro->rw transition prohibited by read-only rtdev");
2054 		return -EACCES;
2055 	}
2056 
2057 	if (xfs_has_norecovery(mp)) {
2058 		xfs_warn(mp,
2059 			"ro->rw transition prohibited on norecovery mount");
2060 		return -EINVAL;
2061 	}
2062 
2063 	if (xfs_sb_is_v5(sbp) &&
2064 	    xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
2065 		xfs_warn(mp,
2066 	"ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
2067 			(sbp->sb_features_ro_compat &
2068 				XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
2069 		return -EINVAL;
2070 	}
2071 
2072 	xfs_clear_readonly(mp);
2073 
2074 	/*
2075 	 * If this is the first remount to writeable state we might have some
2076 	 * superblock changes to update.
2077 	 */
2078 	if (mp->m_update_sb) {
2079 		error = xfs_sync_sb(mp, false);
2080 		if (error) {
2081 			xfs_warn(mp, "failed to write sb changes");
2082 			return error;
2083 		}
2084 		mp->m_update_sb = false;
2085 	}
2086 
2087 	/*
2088 	 * Fill out the reserve pool if it is empty. Use the stashed value if
2089 	 * it is non-zero, otherwise go with the default.
2090 	 */
2091 	xfs_restore_resvblks(mp);
2092 	xfs_log_work_queue(mp);
2093 	xfs_blockgc_start(mp);
2094 
2095 	/* Create the per-AG metadata reservation pool .*/
2096 	error = xfs_fs_reserve_ag_blocks(mp);
2097 	if (error && error != -ENOSPC)
2098 		return error;
2099 
2100 	/* Re-enable the background inode inactivation worker. */
2101 	xfs_inodegc_start(mp);
2102 
2103 	/* Restart zone reclaim */
2104 	xfs_zone_gc_start(mp);
2105 
2106 	return 0;
2107 }
2108 
2109 static int
xfs_remount_ro(struct xfs_mount * mp)2110 xfs_remount_ro(
2111 	struct xfs_mount	*mp)
2112 {
2113 	struct xfs_icwalk	icw = {
2114 		.icw_flags	= XFS_ICWALK_FLAG_SYNC,
2115 	};
2116 	int			error;
2117 
2118 	/* Flush all the dirty data to disk. */
2119 	error = sync_filesystem(mp->m_super);
2120 	if (error)
2121 		return error;
2122 
2123 	/*
2124 	 * Cancel background eofb scanning so it cannot race with the final
2125 	 * log force+buftarg wait and deadlock the remount.
2126 	 */
2127 	xfs_blockgc_stop(mp);
2128 
2129 	/*
2130 	 * Clear out all remaining COW staging extents and speculative post-EOF
2131 	 * preallocations so that we don't leave inodes requiring inactivation
2132 	 * cleanups during reclaim on a read-only mount.  We must process every
2133 	 * cached inode, so this requires a synchronous cache scan.
2134 	 */
2135 	error = xfs_blockgc_free_space(mp, &icw);
2136 	if (error) {
2137 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
2138 		return error;
2139 	}
2140 
2141 	/*
2142 	 * Stop the inodegc background worker.  xfs_fs_reconfigure already
2143 	 * flushed all pending inodegc work when it sync'd the filesystem.
2144 	 * The VFS holds s_umount, so we know that inodes cannot enter
2145 	 * xfs_fs_destroy_inode during a remount operation.  In readonly mode
2146 	 * we send inodes straight to reclaim, so no inodes will be queued.
2147 	 */
2148 	xfs_inodegc_stop(mp);
2149 
2150 	/* Stop zone reclaim */
2151 	xfs_zone_gc_stop(mp);
2152 
2153 	/* Free the per-AG metadata reservation pool. */
2154 	xfs_fs_unreserve_ag_blocks(mp);
2155 
2156 	/*
2157 	 * Before we sync the metadata, we need to free up the reserve block
2158 	 * pool so that the used block count in the superblock on disk is
2159 	 * correct at the end of the remount. Stash the current* reserve pool
2160 	 * size so that if we get remounted rw, we can return it to the same
2161 	 * size.
2162 	 */
2163 	xfs_save_resvblks(mp);
2164 
2165 	xfs_log_clean(mp);
2166 	xfs_set_readonly(mp);
2167 
2168 	return 0;
2169 }
2170 
2171 /*
2172  * Logically we would return an error here to prevent users from believing
2173  * they might have changed mount options using remount which can't be changed.
2174  *
2175  * But unfortunately mount(8) adds all options from mtab and fstab to the mount
2176  * arguments in some cases so we can't blindly reject options, but have to
2177  * check for each specified option if it actually differs from the currently
2178  * set option and only reject it if that's the case.
2179  *
2180  * Until that is implemented we return success for every remount request, and
2181  * silently ignore all options that we can't actually change.
2182  */
2183 static int
xfs_fs_reconfigure(struct fs_context * fc)2184 xfs_fs_reconfigure(
2185 	struct fs_context *fc)
2186 {
2187 	struct xfs_mount	*mp = XFS_M(fc->root->d_sb);
2188 	struct xfs_mount        *new_mp = fc->s_fs_info;
2189 	int			flags = fc->sb_flags;
2190 	int			error;
2191 
2192 	new_mp->m_qflags &= ~XFS_QFLAGS_MNTOPTS;
2193 
2194 	/* version 5 superblocks always support version counters. */
2195 	if (xfs_has_crc(mp))
2196 		fc->sb_flags |= SB_I_VERSION;
2197 
2198 	error = xfs_fs_validate_params(new_mp);
2199 	if (error)
2200 		return error;
2201 
2202 	xfs_errortag_copy(mp, new_mp);
2203 
2204 	/* Validate new max_atomic_write option before making other changes */
2205 	if (mp->m_awu_max_bytes != new_mp->m_awu_max_bytes) {
2206 		error = xfs_set_max_atomic_write_opt(mp,
2207 				new_mp->m_awu_max_bytes);
2208 		if (error)
2209 			return error;
2210 	}
2211 
2212 	/* inode32 -> inode64 */
2213 	if (xfs_has_small_inums(mp) && !xfs_has_small_inums(new_mp)) {
2214 		mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
2215 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
2216 	}
2217 
2218 	/* inode64 -> inode32 */
2219 	if (!xfs_has_small_inums(mp) && xfs_has_small_inums(new_mp)) {
2220 		mp->m_features |= XFS_FEAT_SMALL_INUMS;
2221 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
2222 	}
2223 
2224 	/*
2225 	 * Now that mp has been modified according to the remount options, we
2226 	 * do a final option validation with xfs_finish_flags() just like it is
2227 	 * just like it is done during mount. We cannot use
2228 	 * done during mount. We cannot use xfs_finish_flags() on new_mp as it
2229 	 * contains only the user given options.
2230 	 */
2231 	error = xfs_finish_flags(mp);
2232 	if (error)
2233 		return error;
2234 
2235 	/* ro -> rw */
2236 	if (xfs_is_readonly(mp) && !(flags & SB_RDONLY)) {
2237 		error = xfs_remount_rw(mp);
2238 		if (error)
2239 			return error;
2240 	}
2241 
2242 	/* rw -> ro */
2243 	if (!xfs_is_readonly(mp) && (flags & SB_RDONLY)) {
2244 		error = xfs_remount_ro(mp);
2245 		if (error)
2246 			return error;
2247 	}
2248 
2249 	return 0;
2250 }
2251 
2252 static void
xfs_fs_free(struct fs_context * fc)2253 xfs_fs_free(
2254 	struct fs_context	*fc)
2255 {
2256 	struct xfs_mount	*mp = fc->s_fs_info;
2257 
2258 	/*
2259 	 * mp is stored in the fs_context when it is initialized.
2260 	 * mp is transferred to the superblock on a successful mount,
2261 	 * but if an error occurs before the transfer we have to free
2262 	 * it here.
2263 	 */
2264 	if (mp)
2265 		xfs_mount_free(mp);
2266 }
2267 
2268 static const struct fs_context_operations xfs_context_ops = {
2269 	.parse_param = xfs_fs_parse_param,
2270 	.get_tree    = xfs_fs_get_tree,
2271 	.reconfigure = xfs_fs_reconfigure,
2272 	.free        = xfs_fs_free,
2273 };
2274 
2275 /*
2276  * WARNING: do not initialise any parameters in this function that depend on
2277  * mount option parsing having already been performed as this can be called from
2278  * fsopen() before any parameters have been set.
2279  */
2280 static int
xfs_init_fs_context(struct fs_context * fc)2281 xfs_init_fs_context(
2282 	struct fs_context	*fc)
2283 {
2284 	struct xfs_mount	*mp;
2285 	int			i;
2286 
2287 	mp = kzalloc_obj(struct xfs_mount);
2288 	if (!mp)
2289 		return -ENOMEM;
2290 #ifdef DEBUG
2291 	mp->m_errortag = kzalloc_objs(*mp->m_errortag, XFS_ERRTAG_MAX);
2292 	if (!mp->m_errortag) {
2293 		kfree(mp);
2294 		return -ENOMEM;
2295 	}
2296 #endif
2297 
2298 	spin_lock_init(&mp->m_sb_lock);
2299 	for (i = 0; i < XG_TYPE_MAX; i++)
2300 		xa_init(&mp->m_groups[i].xa);
2301 	mutex_init(&mp->m_growlock);
2302 	mutex_init(&mp->m_metafile_resv_lock);
2303 	INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
2304 	INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
2305 	mp->m_kobj.kobject.kset = xfs_kset;
2306 	/*
2307 	 * We don't create the finobt per-ag space reservation until after log
2308 	 * recovery, so we must set this to true so that an ifree transaction
2309 	 * started during log recovery will not depend on space reservations
2310 	 * for finobt expansion.
2311 	 */
2312 	mp->m_finobt_nores = true;
2313 
2314 	/*
2315 	 * These can be overridden by the mount option parsing.
2316 	 */
2317 	mp->m_logbufs = -1;
2318 	mp->m_logbsize = -1;
2319 	mp->m_allocsize_log = 16; /* 64k */
2320 
2321 	xfs_hooks_init(&mp->m_dir_update_hooks);
2322 
2323 	fc->s_fs_info = mp;
2324 	fc->ops = &xfs_context_ops;
2325 
2326 	return 0;
2327 }
2328 
2329 static void
xfs_kill_sb(struct super_block * sb)2330 xfs_kill_sb(
2331 	struct super_block		*sb)
2332 {
2333 	struct xfs_mount		*mp = XFS_M(sb);
2334 
2335 	if (mp->m_rtdev_targp && mp->m_rtdev_targp != mp->m_ddev_targp)
2336 		xfs_restore_bdi_rahead(mp);
2337 	kill_block_super(sb);
2338 	xfs_mount_free(mp);
2339 }
2340 
2341 static struct file_system_type xfs_fs_type = {
2342 	.owner			= THIS_MODULE,
2343 	.name			= "xfs",
2344 	.init_fs_context	= xfs_init_fs_context,
2345 	.parameters		= xfs_fs_parameters,
2346 	.kill_sb		= xfs_kill_sb,
2347 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_MGTIME |
2348 				  FS_LBS,
2349 };
2350 MODULE_ALIAS_FS("xfs");
2351 
2352 STATIC int __init
xfs_init_caches(void)2353 xfs_init_caches(void)
2354 {
2355 	int		error;
2356 
2357 	xfs_buf_cache = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2358 					 SLAB_HWCACHE_ALIGN |
2359 					 SLAB_RECLAIM_ACCOUNT,
2360 					 NULL);
2361 	if (!xfs_buf_cache)
2362 		goto out;
2363 
2364 	xfs_log_ticket_cache = kmem_cache_create("xfs_log_ticket",
2365 						sizeof(struct xlog_ticket),
2366 						0, 0, NULL);
2367 	if (!xfs_log_ticket_cache)
2368 		goto out_destroy_buf_cache;
2369 
2370 	error = xfs_btree_init_cur_caches();
2371 	if (error)
2372 		goto out_destroy_log_ticket_cache;
2373 
2374 	error = rcbagbt_init_cur_cache();
2375 	if (error)
2376 		goto out_destroy_btree_cur_cache;
2377 
2378 	error = xfs_defer_init_item_caches();
2379 	if (error)
2380 		goto out_destroy_rcbagbt_cur_cache;
2381 
2382 	xfs_da_state_cache = kmem_cache_create("xfs_da_state",
2383 					      sizeof(struct xfs_da_state),
2384 					      0, 0, NULL);
2385 	if (!xfs_da_state_cache)
2386 		goto out_destroy_defer_item_cache;
2387 
2388 	xfs_ifork_cache = kmem_cache_create("xfs_ifork",
2389 					   sizeof(struct xfs_ifork),
2390 					   0, 0, NULL);
2391 	if (!xfs_ifork_cache)
2392 		goto out_destroy_da_state_cache;
2393 
2394 	xfs_trans_cache = kmem_cache_create("xfs_trans",
2395 					   sizeof(struct xfs_trans),
2396 					   0, 0, NULL);
2397 	if (!xfs_trans_cache)
2398 		goto out_destroy_ifork_cache;
2399 
2400 
2401 	/*
2402 	 * The size of the cache-allocated buf log item is the maximum
2403 	 * size possible under XFS.  This wastes a little bit of memory,
2404 	 * but it is much faster.
2405 	 */
2406 	xfs_buf_item_cache = kmem_cache_create("xfs_buf_item",
2407 					      sizeof(struct xfs_buf_log_item),
2408 					      0, 0, NULL);
2409 	if (!xfs_buf_item_cache)
2410 		goto out_destroy_trans_cache;
2411 
2412 	xfs_efd_cache = kmem_cache_create("xfs_efd_item",
2413 			xfs_efd_log_item_sizeof(XFS_EFD_MAX_FAST_EXTENTS),
2414 			0, 0, NULL);
2415 	if (!xfs_efd_cache)
2416 		goto out_destroy_buf_item_cache;
2417 
2418 	xfs_efi_cache = kmem_cache_create("xfs_efi_item",
2419 			xfs_efi_log_item_sizeof(XFS_EFI_MAX_FAST_EXTENTS),
2420 			0, 0, NULL);
2421 	if (!xfs_efi_cache)
2422 		goto out_destroy_efd_cache;
2423 
2424 	xfs_inode_cache = kmem_cache_create("xfs_inode",
2425 					   sizeof(struct xfs_inode), 0,
2426 					   (SLAB_HWCACHE_ALIGN |
2427 					    SLAB_RECLAIM_ACCOUNT |
2428 					    SLAB_ACCOUNT),
2429 					   xfs_fs_inode_init_once);
2430 	if (!xfs_inode_cache)
2431 		goto out_destroy_efi_cache;
2432 
2433 	xfs_ili_cache = kmem_cache_create("xfs_ili",
2434 					 sizeof(struct xfs_inode_log_item), 0,
2435 					 SLAB_RECLAIM_ACCOUNT,
2436 					 NULL);
2437 	if (!xfs_ili_cache)
2438 		goto out_destroy_inode_cache;
2439 
2440 	xfs_icreate_cache = kmem_cache_create("xfs_icr",
2441 					     sizeof(struct xfs_icreate_item),
2442 					     0, 0, NULL);
2443 	if (!xfs_icreate_cache)
2444 		goto out_destroy_ili_cache;
2445 
2446 	xfs_rud_cache = kmem_cache_create("xfs_rud_item",
2447 					 sizeof(struct xfs_rud_log_item),
2448 					 0, 0, NULL);
2449 	if (!xfs_rud_cache)
2450 		goto out_destroy_icreate_cache;
2451 
2452 	xfs_rui_cache = kmem_cache_create("xfs_rui_item",
2453 			xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
2454 			0, 0, NULL);
2455 	if (!xfs_rui_cache)
2456 		goto out_destroy_rud_cache;
2457 
2458 	xfs_cud_cache = kmem_cache_create("xfs_cud_item",
2459 					 sizeof(struct xfs_cud_log_item),
2460 					 0, 0, NULL);
2461 	if (!xfs_cud_cache)
2462 		goto out_destroy_rui_cache;
2463 
2464 	xfs_cui_cache = kmem_cache_create("xfs_cui_item",
2465 			xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
2466 			0, 0, NULL);
2467 	if (!xfs_cui_cache)
2468 		goto out_destroy_cud_cache;
2469 
2470 	xfs_bud_cache = kmem_cache_create("xfs_bud_item",
2471 					 sizeof(struct xfs_bud_log_item),
2472 					 0, 0, NULL);
2473 	if (!xfs_bud_cache)
2474 		goto out_destroy_cui_cache;
2475 
2476 	xfs_bui_cache = kmem_cache_create("xfs_bui_item",
2477 			xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
2478 			0, 0, NULL);
2479 	if (!xfs_bui_cache)
2480 		goto out_destroy_bud_cache;
2481 
2482 	xfs_attrd_cache = kmem_cache_create("xfs_attrd_item",
2483 					    sizeof(struct xfs_attrd_log_item),
2484 					    0, 0, NULL);
2485 	if (!xfs_attrd_cache)
2486 		goto out_destroy_bui_cache;
2487 
2488 	xfs_attri_cache = kmem_cache_create("xfs_attri_item",
2489 					    sizeof(struct xfs_attri_log_item),
2490 					    0, 0, NULL);
2491 	if (!xfs_attri_cache)
2492 		goto out_destroy_attrd_cache;
2493 
2494 	xfs_iunlink_cache = kmem_cache_create("xfs_iul_item",
2495 					     sizeof(struct xfs_iunlink_item),
2496 					     0, 0, NULL);
2497 	if (!xfs_iunlink_cache)
2498 		goto out_destroy_attri_cache;
2499 
2500 	xfs_xmd_cache = kmem_cache_create("xfs_xmd_item",
2501 					 sizeof(struct xfs_xmd_log_item),
2502 					 0, 0, NULL);
2503 	if (!xfs_xmd_cache)
2504 		goto out_destroy_iul_cache;
2505 
2506 	xfs_xmi_cache = kmem_cache_create("xfs_xmi_item",
2507 					 sizeof(struct xfs_xmi_log_item),
2508 					 0, 0, NULL);
2509 	if (!xfs_xmi_cache)
2510 		goto out_destroy_xmd_cache;
2511 
2512 	xfs_parent_args_cache = kmem_cache_create("xfs_parent_args",
2513 					     sizeof(struct xfs_parent_args),
2514 					     0, 0, NULL);
2515 	if (!xfs_parent_args_cache)
2516 		goto out_destroy_xmi_cache;
2517 
2518 	return 0;
2519 
2520  out_destroy_xmi_cache:
2521 	kmem_cache_destroy(xfs_xmi_cache);
2522  out_destroy_xmd_cache:
2523 	kmem_cache_destroy(xfs_xmd_cache);
2524  out_destroy_iul_cache:
2525 	kmem_cache_destroy(xfs_iunlink_cache);
2526  out_destroy_attri_cache:
2527 	kmem_cache_destroy(xfs_attri_cache);
2528  out_destroy_attrd_cache:
2529 	kmem_cache_destroy(xfs_attrd_cache);
2530  out_destroy_bui_cache:
2531 	kmem_cache_destroy(xfs_bui_cache);
2532  out_destroy_bud_cache:
2533 	kmem_cache_destroy(xfs_bud_cache);
2534  out_destroy_cui_cache:
2535 	kmem_cache_destroy(xfs_cui_cache);
2536  out_destroy_cud_cache:
2537 	kmem_cache_destroy(xfs_cud_cache);
2538  out_destroy_rui_cache:
2539 	kmem_cache_destroy(xfs_rui_cache);
2540  out_destroy_rud_cache:
2541 	kmem_cache_destroy(xfs_rud_cache);
2542  out_destroy_icreate_cache:
2543 	kmem_cache_destroy(xfs_icreate_cache);
2544  out_destroy_ili_cache:
2545 	kmem_cache_destroy(xfs_ili_cache);
2546  out_destroy_inode_cache:
2547 	kmem_cache_destroy(xfs_inode_cache);
2548  out_destroy_efi_cache:
2549 	kmem_cache_destroy(xfs_efi_cache);
2550  out_destroy_efd_cache:
2551 	kmem_cache_destroy(xfs_efd_cache);
2552  out_destroy_buf_item_cache:
2553 	kmem_cache_destroy(xfs_buf_item_cache);
2554  out_destroy_trans_cache:
2555 	kmem_cache_destroy(xfs_trans_cache);
2556  out_destroy_ifork_cache:
2557 	kmem_cache_destroy(xfs_ifork_cache);
2558  out_destroy_da_state_cache:
2559 	kmem_cache_destroy(xfs_da_state_cache);
2560  out_destroy_defer_item_cache:
2561 	xfs_defer_destroy_item_caches();
2562  out_destroy_rcbagbt_cur_cache:
2563 	rcbagbt_destroy_cur_cache();
2564  out_destroy_btree_cur_cache:
2565 	xfs_btree_destroy_cur_caches();
2566  out_destroy_log_ticket_cache:
2567 	kmem_cache_destroy(xfs_log_ticket_cache);
2568  out_destroy_buf_cache:
2569 	kmem_cache_destroy(xfs_buf_cache);
2570  out:
2571 	return -ENOMEM;
2572 }
2573 
2574 STATIC void
xfs_destroy_caches(void)2575 xfs_destroy_caches(void)
2576 {
2577 	/*
2578 	 * Make sure all delayed rcu free are flushed before we
2579 	 * destroy caches.
2580 	 */
2581 	rcu_barrier();
2582 	kmem_cache_destroy(xfs_parent_args_cache);
2583 	kmem_cache_destroy(xfs_xmd_cache);
2584 	kmem_cache_destroy(xfs_xmi_cache);
2585 	kmem_cache_destroy(xfs_iunlink_cache);
2586 	kmem_cache_destroy(xfs_attri_cache);
2587 	kmem_cache_destroy(xfs_attrd_cache);
2588 	kmem_cache_destroy(xfs_bui_cache);
2589 	kmem_cache_destroy(xfs_bud_cache);
2590 	kmem_cache_destroy(xfs_cui_cache);
2591 	kmem_cache_destroy(xfs_cud_cache);
2592 	kmem_cache_destroy(xfs_rui_cache);
2593 	kmem_cache_destroy(xfs_rud_cache);
2594 	kmem_cache_destroy(xfs_icreate_cache);
2595 	kmem_cache_destroy(xfs_ili_cache);
2596 	kmem_cache_destroy(xfs_inode_cache);
2597 	kmem_cache_destroy(xfs_efi_cache);
2598 	kmem_cache_destroy(xfs_efd_cache);
2599 	kmem_cache_destroy(xfs_buf_item_cache);
2600 	kmem_cache_destroy(xfs_trans_cache);
2601 	kmem_cache_destroy(xfs_ifork_cache);
2602 	kmem_cache_destroy(xfs_da_state_cache);
2603 	xfs_defer_destroy_item_caches();
2604 	rcbagbt_destroy_cur_cache();
2605 	xfs_btree_destroy_cur_caches();
2606 	kmem_cache_destroy(xfs_log_ticket_cache);
2607 	kmem_cache_destroy(xfs_buf_cache);
2608 }
2609 
2610 STATIC int __init
xfs_init_workqueues(void)2611 xfs_init_workqueues(void)
2612 {
2613 	/*
2614 	 * The allocation workqueue can be used in memory reclaim situations
2615 	 * (writepage path), and parallelism is only limited by the number of
2616 	 * AGs in all the filesystems mounted. Hence use the default large
2617 	 * max_active value for this workqueue.
2618 	 */
2619 	xfs_alloc_wq = alloc_workqueue("xfsalloc", XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_PERCPU),
2620 			0);
2621 	if (!xfs_alloc_wq)
2622 		return -ENOMEM;
2623 
2624 	xfs_discard_wq = alloc_workqueue("xfsdiscard", XFS_WQFLAGS(WQ_UNBOUND),
2625 			0);
2626 	if (!xfs_discard_wq)
2627 		goto out_free_alloc_wq;
2628 
2629 	return 0;
2630 out_free_alloc_wq:
2631 	destroy_workqueue(xfs_alloc_wq);
2632 	return -ENOMEM;
2633 }
2634 
2635 STATIC void
xfs_destroy_workqueues(void)2636 xfs_destroy_workqueues(void)
2637 {
2638 	destroy_workqueue(xfs_discard_wq);
2639 	destroy_workqueue(xfs_alloc_wq);
2640 }
2641 
2642 STATIC int __init
init_xfs_fs(void)2643 init_xfs_fs(void)
2644 {
2645 	int			error;
2646 
2647 	xfs_check_ondisk_structs();
2648 
2649 	error = xfs_dahash_test();
2650 	if (error)
2651 		return error;
2652 
2653 	printk(KERN_INFO XFS_VERSION_STRING " with "
2654 			 XFS_BUILD_OPTIONS " enabled\n");
2655 
2656 	xfs_dir_startup();
2657 
2658 	error = xfs_init_caches();
2659 	if (error)
2660 		goto out;
2661 
2662 	error = xfs_init_workqueues();
2663 	if (error)
2664 		goto out_destroy_caches;
2665 
2666 	error = xfs_mru_cache_init();
2667 	if (error)
2668 		goto out_destroy_wq;
2669 
2670 	error = xfs_init_procfs();
2671 	if (error)
2672 		goto out_mru_cache_uninit;
2673 
2674 	error = xfs_sysctl_register();
2675 	if (error)
2676 		goto out_cleanup_procfs;
2677 
2678 	xfs_debugfs = xfs_debugfs_mkdir("xfs", NULL);
2679 
2680 	xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
2681 	if (!xfs_kset) {
2682 		error = -ENOMEM;
2683 		goto out_debugfs_unregister;
2684 	}
2685 
2686 	xfsstats.xs_kobj.kobject.kset = xfs_kset;
2687 
2688 	xfsstats.xs_stats = alloc_percpu(struct xfsstats);
2689 	if (!xfsstats.xs_stats) {
2690 		error = -ENOMEM;
2691 		goto out_kset_unregister;
2692 	}
2693 
2694 	error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
2695 			       "stats");
2696 	if (error)
2697 		goto out_free_stats;
2698 
2699 	error = xchk_global_stats_setup(xfs_debugfs);
2700 	if (error)
2701 		goto out_remove_stats_kobj;
2702 
2703 #ifdef DEBUG
2704 	xfs_dbg_kobj.kobject.kset = xfs_kset;
2705 	error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2706 	if (error)
2707 		goto out_remove_scrub_stats;
2708 #endif
2709 
2710 	error = xfs_qm_init();
2711 	if (error)
2712 		goto out_remove_dbg_kobj;
2713 
2714 	error = register_filesystem(&xfs_fs_type);
2715 	if (error)
2716 		goto out_qm_exit;
2717 	return 0;
2718 
2719  out_qm_exit:
2720 	xfs_qm_exit();
2721  out_remove_dbg_kobj:
2722 #ifdef DEBUG
2723 	xfs_sysfs_del(&xfs_dbg_kobj);
2724  out_remove_scrub_stats:
2725 #endif
2726 	xchk_global_stats_teardown();
2727  out_remove_stats_kobj:
2728 	xfs_sysfs_del(&xfsstats.xs_kobj);
2729  out_free_stats:
2730 	free_percpu(xfsstats.xs_stats);
2731  out_kset_unregister:
2732 	kset_unregister(xfs_kset);
2733  out_debugfs_unregister:
2734 	debugfs_remove(xfs_debugfs);
2735 	xfs_sysctl_unregister();
2736  out_cleanup_procfs:
2737 	xfs_cleanup_procfs();
2738  out_mru_cache_uninit:
2739 	xfs_mru_cache_uninit();
2740  out_destroy_wq:
2741 	xfs_destroy_workqueues();
2742  out_destroy_caches:
2743 	xfs_destroy_caches();
2744  out:
2745 	return error;
2746 }
2747 
2748 STATIC void __exit
exit_xfs_fs(void)2749 exit_xfs_fs(void)
2750 {
2751 	xfs_qm_exit();
2752 	unregister_filesystem(&xfs_fs_type);
2753 #ifdef DEBUG
2754 	xfs_sysfs_del(&xfs_dbg_kobj);
2755 #endif
2756 	xchk_global_stats_teardown();
2757 	xfs_sysfs_del(&xfsstats.xs_kobj);
2758 	free_percpu(xfsstats.xs_stats);
2759 	kset_unregister(xfs_kset);
2760 	debugfs_remove(xfs_debugfs);
2761 	xfs_sysctl_unregister();
2762 	xfs_cleanup_procfs();
2763 	xfs_mru_cache_uninit();
2764 	xfs_destroy_workqueues();
2765 	xfs_destroy_caches();
2766 	xfs_uuid_table_free();
2767 }
2768 
2769 module_init(init_xfs_fs);
2770 module_exit(exit_xfs_fs);
2771 
2772 MODULE_AUTHOR("Silicon Graphics, Inc.");
2773 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2774 MODULE_LICENSE("GPL");
2775