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