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