xref: /linux/fs/xfs/xfs_super.c (revision 119ff04864a24470b1e531bb53e5c141aa8fefb0)
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 	/*
1500 	 * Copy VFS mount flags from the context now that all parameter parsing
1501 	 * is guaranteed to have been completed by either the old mount API or
1502 	 * the newer fsopen/fsconfig API.
1503 	 */
1504 	if (fc->sb_flags & SB_RDONLY)
1505 		set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1506 	if (fc->sb_flags & SB_DIRSYNC)
1507 		mp->m_features |= XFS_FEAT_DIRSYNC;
1508 	if (fc->sb_flags & SB_SYNCHRONOUS)
1509 		mp->m_features |= XFS_FEAT_WSYNC;
1510 
1511 	error = xfs_fs_validate_params(mp);
1512 	if (error)
1513 		return error;
1514 
1515 	sb_min_blocksize(sb, BBSIZE);
1516 	sb->s_xattr = xfs_xattr_handlers;
1517 	sb->s_export_op = &xfs_export_operations;
1518 #ifdef CONFIG_XFS_QUOTA
1519 	sb->s_qcop = &xfs_quotactl_operations;
1520 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1521 #endif
1522 	sb->s_op = &xfs_super_operations;
1523 
1524 	/*
1525 	 * Delay mount work if the debug hook is set. This is debug
1526 	 * instrumention to coordinate simulation of xfs mount failures with
1527 	 * VFS superblock operations
1528 	 */
1529 	if (xfs_globals.mount_delay) {
1530 		xfs_notice(mp, "Delaying mount for %d seconds.",
1531 			xfs_globals.mount_delay);
1532 		msleep(xfs_globals.mount_delay * 1000);
1533 	}
1534 
1535 	if (fc->sb_flags & SB_SILENT)
1536 		flags |= XFS_MFSI_QUIET;
1537 
1538 	error = xfs_open_devices(mp);
1539 	if (error)
1540 		return error;
1541 
1542 	if (xfs_debugfs) {
1543 		mp->m_debugfs = xfs_debugfs_mkdir(mp->m_super->s_id,
1544 						  xfs_debugfs);
1545 	} else {
1546 		mp->m_debugfs = NULL;
1547 	}
1548 
1549 	error = xfs_init_mount_workqueues(mp);
1550 	if (error)
1551 		goto out_shutdown_devices;
1552 
1553 	error = xfs_init_percpu_counters(mp);
1554 	if (error)
1555 		goto out_destroy_workqueues;
1556 
1557 	error = xfs_inodegc_init_percpu(mp);
1558 	if (error)
1559 		goto out_destroy_counters;
1560 
1561 	/* Allocate stats memory before we do operations that might use it */
1562 	mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
1563 	if (!mp->m_stats.xs_stats) {
1564 		error = -ENOMEM;
1565 		goto out_destroy_inodegc;
1566 	}
1567 
1568 	error = xchk_mount_stats_alloc(mp);
1569 	if (error)
1570 		goto out_free_stats;
1571 
1572 	error = xfs_readsb(mp, flags);
1573 	if (error)
1574 		goto out_free_scrub_stats;
1575 
1576 	error = xfs_finish_flags(mp);
1577 	if (error)
1578 		goto out_free_sb;
1579 
1580 	error = xfs_setup_devices(mp);
1581 	if (error)
1582 		goto out_free_sb;
1583 
1584 	/* V4 support is undergoing deprecation. */
1585 	if (!xfs_has_crc(mp)) {
1586 #ifdef CONFIG_XFS_SUPPORT_V4
1587 		xfs_warn_once(mp,
1588 	"Deprecated V4 format (crc=0) will not be supported after September 2030.");
1589 #else
1590 		xfs_warn(mp,
1591 	"Deprecated V4 format (crc=0) not supported by kernel.");
1592 		error = -EINVAL;
1593 		goto out_free_sb;
1594 #endif
1595 	}
1596 
1597 	/* ASCII case insensitivity is undergoing deprecation. */
1598 	if (xfs_has_asciici(mp)) {
1599 #ifdef CONFIG_XFS_SUPPORT_ASCII_CI
1600 		xfs_warn_once(mp,
1601 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) will not be supported after September 2030.");
1602 #else
1603 		xfs_warn(mp,
1604 	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) not supported by kernel.");
1605 		error = -EINVAL;
1606 		goto out_free_sb;
1607 #endif
1608 	}
1609 
1610 	/* Filesystem claims it needs repair, so refuse the mount. */
1611 	if (xfs_has_needsrepair(mp)) {
1612 		xfs_warn(mp, "Filesystem needs repair.  Please run xfs_repair.");
1613 		error = -EFSCORRUPTED;
1614 		goto out_free_sb;
1615 	}
1616 
1617 	/*
1618 	 * Don't touch the filesystem if a user tool thinks it owns the primary
1619 	 * superblock.  mkfs doesn't clear the flag from secondary supers, so
1620 	 * we don't check them at all.
1621 	 */
1622 	if (mp->m_sb.sb_inprogress) {
1623 		xfs_warn(mp, "Offline file system operation in progress!");
1624 		error = -EFSCORRUPTED;
1625 		goto out_free_sb;
1626 	}
1627 
1628 	/*
1629 	 * Until this is fixed only page-sized or smaller data blocks work.
1630 	 */
1631 	if (mp->m_sb.sb_blocksize > PAGE_SIZE) {
1632 		xfs_warn(mp,
1633 		"File system with blocksize %d bytes. "
1634 		"Only pagesize (%ld) or less will currently work.",
1635 				mp->m_sb.sb_blocksize, PAGE_SIZE);
1636 		error = -ENOSYS;
1637 		goto out_free_sb;
1638 	}
1639 
1640 	/* Ensure this filesystem fits in the page cache limits */
1641 	if (xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_dblocks) ||
1642 	    xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_rblocks)) {
1643 		xfs_warn(mp,
1644 		"file system too large to be mounted on this system.");
1645 		error = -EFBIG;
1646 		goto out_free_sb;
1647 	}
1648 
1649 	/*
1650 	 * XFS block mappings use 54 bits to store the logical block offset.
1651 	 * This should suffice to handle the maximum file size that the VFS
1652 	 * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT
1653 	 * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes
1654 	 * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON
1655 	 * to check this assertion.
1656 	 *
1657 	 * Avoid integer overflow by comparing the maximum bmbt offset to the
1658 	 * maximum pagecache offset in units of fs blocks.
1659 	 */
1660 	if (!xfs_verify_fileoff(mp, XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE))) {
1661 		xfs_warn(mp,
1662 "MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!",
1663 			 XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE),
1664 			 XFS_MAX_FILEOFF);
1665 		error = -EINVAL;
1666 		goto out_free_sb;
1667 	}
1668 
1669 	error = xfs_filestream_mount(mp);
1670 	if (error)
1671 		goto out_free_sb;
1672 
1673 	/*
1674 	 * we must configure the block size in the superblock before we run the
1675 	 * full mount process as the mount process can lookup and cache inodes.
1676 	 */
1677 	sb->s_magic = XFS_SUPER_MAGIC;
1678 	sb->s_blocksize = mp->m_sb.sb_blocksize;
1679 	sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1680 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1681 	sb->s_max_links = XFS_MAXLINK;
1682 	sb->s_time_gran = 1;
1683 	if (xfs_has_bigtime(mp)) {
1684 		sb->s_time_min = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MIN);
1685 		sb->s_time_max = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MAX);
1686 	} else {
1687 		sb->s_time_min = XFS_LEGACY_TIME_MIN;
1688 		sb->s_time_max = XFS_LEGACY_TIME_MAX;
1689 	}
1690 	trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
1691 	sb->s_iflags |= SB_I_CGROUPWB;
1692 
1693 	set_posix_acl_flag(sb);
1694 
1695 	/* version 5 superblocks support inode version counters. */
1696 	if (xfs_has_crc(mp))
1697 		sb->s_flags |= SB_I_VERSION;
1698 
1699 	if (xfs_has_dax_always(mp)) {
1700 		error = xfs_setup_dax_always(mp);
1701 		if (error)
1702 			goto out_filestream_unmount;
1703 	}
1704 
1705 	if (xfs_has_discard(mp) && !bdev_max_discard_sectors(sb->s_bdev)) {
1706 		xfs_warn(mp,
1707 	"mounting with \"discard\" option, but the device does not support discard");
1708 		mp->m_features &= ~XFS_FEAT_DISCARD;
1709 	}
1710 
1711 	if (xfs_has_reflink(mp)) {
1712 		if (mp->m_sb.sb_rblocks) {
1713 			xfs_alert(mp,
1714 	"reflink not compatible with realtime device!");
1715 			error = -EINVAL;
1716 			goto out_filestream_unmount;
1717 		}
1718 
1719 		if (xfs_globals.always_cow) {
1720 			xfs_info(mp, "using DEBUG-only always_cow mode.");
1721 			mp->m_always_cow = true;
1722 		}
1723 	}
1724 
1725 	if (xfs_has_rmapbt(mp) && mp->m_sb.sb_rblocks) {
1726 		xfs_alert(mp,
1727 	"reverse mapping btree not compatible with realtime device!");
1728 		error = -EINVAL;
1729 		goto out_filestream_unmount;
1730 	}
1731 
1732 	error = xfs_mountfs(mp);
1733 	if (error)
1734 		goto out_filestream_unmount;
1735 
1736 	root = igrab(VFS_I(mp->m_rootip));
1737 	if (!root) {
1738 		error = -ENOENT;
1739 		goto out_unmount;
1740 	}
1741 	sb->s_root = d_make_root(root);
1742 	if (!sb->s_root) {
1743 		error = -ENOMEM;
1744 		goto out_unmount;
1745 	}
1746 
1747 	return 0;
1748 
1749  out_filestream_unmount:
1750 	xfs_filestream_unmount(mp);
1751  out_free_sb:
1752 	xfs_freesb(mp);
1753  out_free_scrub_stats:
1754 	xchk_mount_stats_free(mp);
1755  out_free_stats:
1756 	free_percpu(mp->m_stats.xs_stats);
1757  out_destroy_inodegc:
1758 	xfs_inodegc_free_percpu(mp);
1759  out_destroy_counters:
1760 	xfs_destroy_percpu_counters(mp);
1761  out_destroy_workqueues:
1762 	xfs_destroy_mount_workqueues(mp);
1763  out_shutdown_devices:
1764 	xfs_shutdown_devices(mp);
1765 	return error;
1766 
1767  out_unmount:
1768 	xfs_filestream_unmount(mp);
1769 	xfs_unmountfs(mp);
1770 	goto out_free_sb;
1771 }
1772 
1773 static int
1774 xfs_fs_get_tree(
1775 	struct fs_context	*fc)
1776 {
1777 	return get_tree_bdev(fc, xfs_fs_fill_super);
1778 }
1779 
1780 static int
1781 xfs_remount_rw(
1782 	struct xfs_mount	*mp)
1783 {
1784 	struct xfs_sb		*sbp = &mp->m_sb;
1785 	int error;
1786 
1787 	if (xfs_has_norecovery(mp)) {
1788 		xfs_warn(mp,
1789 			"ro->rw transition prohibited on norecovery mount");
1790 		return -EINVAL;
1791 	}
1792 
1793 	if (xfs_sb_is_v5(sbp) &&
1794 	    xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
1795 		xfs_warn(mp,
1796 	"ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
1797 			(sbp->sb_features_ro_compat &
1798 				XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
1799 		return -EINVAL;
1800 	}
1801 
1802 	clear_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1803 
1804 	/*
1805 	 * If this is the first remount to writeable state we might have some
1806 	 * superblock changes to update.
1807 	 */
1808 	if (mp->m_update_sb) {
1809 		error = xfs_sync_sb(mp, false);
1810 		if (error) {
1811 			xfs_warn(mp, "failed to write sb changes");
1812 			return error;
1813 		}
1814 		mp->m_update_sb = false;
1815 	}
1816 
1817 	/*
1818 	 * Fill out the reserve pool if it is empty. Use the stashed value if
1819 	 * it is non-zero, otherwise go with the default.
1820 	 */
1821 	xfs_restore_resvblks(mp);
1822 	xfs_log_work_queue(mp);
1823 	xfs_blockgc_start(mp);
1824 
1825 	/* Create the per-AG metadata reservation pool .*/
1826 	error = xfs_fs_reserve_ag_blocks(mp);
1827 	if (error && error != -ENOSPC)
1828 		return error;
1829 
1830 	/* Re-enable the background inode inactivation worker. */
1831 	xfs_inodegc_start(mp);
1832 
1833 	return 0;
1834 }
1835 
1836 static int
1837 xfs_remount_ro(
1838 	struct xfs_mount	*mp)
1839 {
1840 	struct xfs_icwalk	icw = {
1841 		.icw_flags	= XFS_ICWALK_FLAG_SYNC,
1842 	};
1843 	int			error;
1844 
1845 	/* Flush all the dirty data to disk. */
1846 	error = sync_filesystem(mp->m_super);
1847 	if (error)
1848 		return error;
1849 
1850 	/*
1851 	 * Cancel background eofb scanning so it cannot race with the final
1852 	 * log force+buftarg wait and deadlock the remount.
1853 	 */
1854 	xfs_blockgc_stop(mp);
1855 
1856 	/*
1857 	 * Clear out all remaining COW staging extents and speculative post-EOF
1858 	 * preallocations so that we don't leave inodes requiring inactivation
1859 	 * cleanups during reclaim on a read-only mount.  We must process every
1860 	 * cached inode, so this requires a synchronous cache scan.
1861 	 */
1862 	error = xfs_blockgc_free_space(mp, &icw);
1863 	if (error) {
1864 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1865 		return error;
1866 	}
1867 
1868 	/*
1869 	 * Stop the inodegc background worker.  xfs_fs_reconfigure already
1870 	 * flushed all pending inodegc work when it sync'd the filesystem.
1871 	 * The VFS holds s_umount, so we know that inodes cannot enter
1872 	 * xfs_fs_destroy_inode during a remount operation.  In readonly mode
1873 	 * we send inodes straight to reclaim, so no inodes will be queued.
1874 	 */
1875 	xfs_inodegc_stop(mp);
1876 
1877 	/* Free the per-AG metadata reservation pool. */
1878 	error = xfs_fs_unreserve_ag_blocks(mp);
1879 	if (error) {
1880 		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1881 		return error;
1882 	}
1883 
1884 	/*
1885 	 * Before we sync the metadata, we need to free up the reserve block
1886 	 * pool so that the used block count in the superblock on disk is
1887 	 * correct at the end of the remount. Stash the current* reserve pool
1888 	 * size so that if we get remounted rw, we can return it to the same
1889 	 * size.
1890 	 */
1891 	xfs_save_resvblks(mp);
1892 
1893 	xfs_log_clean(mp);
1894 	set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1895 
1896 	return 0;
1897 }
1898 
1899 /*
1900  * Logically we would return an error here to prevent users from believing
1901  * they might have changed mount options using remount which can't be changed.
1902  *
1903  * But unfortunately mount(8) adds all options from mtab and fstab to the mount
1904  * arguments in some cases so we can't blindly reject options, but have to
1905  * check for each specified option if it actually differs from the currently
1906  * set option and only reject it if that's the case.
1907  *
1908  * Until that is implemented we return success for every remount request, and
1909  * silently ignore all options that we can't actually change.
1910  */
1911 static int
1912 xfs_fs_reconfigure(
1913 	struct fs_context *fc)
1914 {
1915 	struct xfs_mount	*mp = XFS_M(fc->root->d_sb);
1916 	struct xfs_mount        *new_mp = fc->s_fs_info;
1917 	int			flags = fc->sb_flags;
1918 	int			error;
1919 
1920 	/* version 5 superblocks always support version counters. */
1921 	if (xfs_has_crc(mp))
1922 		fc->sb_flags |= SB_I_VERSION;
1923 
1924 	error = xfs_fs_validate_params(new_mp);
1925 	if (error)
1926 		return error;
1927 
1928 	/* inode32 -> inode64 */
1929 	if (xfs_has_small_inums(mp) && !xfs_has_small_inums(new_mp)) {
1930 		mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1931 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
1932 	}
1933 
1934 	/* inode64 -> inode32 */
1935 	if (!xfs_has_small_inums(mp) && xfs_has_small_inums(new_mp)) {
1936 		mp->m_features |= XFS_FEAT_SMALL_INUMS;
1937 		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
1938 	}
1939 
1940 	/* ro -> rw */
1941 	if (xfs_is_readonly(mp) && !(flags & SB_RDONLY)) {
1942 		error = xfs_remount_rw(mp);
1943 		if (error)
1944 			return error;
1945 	}
1946 
1947 	/* rw -> ro */
1948 	if (!xfs_is_readonly(mp) && (flags & SB_RDONLY)) {
1949 		error = xfs_remount_ro(mp);
1950 		if (error)
1951 			return error;
1952 	}
1953 
1954 	return 0;
1955 }
1956 
1957 static void
1958 xfs_fs_free(
1959 	struct fs_context	*fc)
1960 {
1961 	struct xfs_mount	*mp = fc->s_fs_info;
1962 
1963 	/*
1964 	 * mp is stored in the fs_context when it is initialized.
1965 	 * mp is transferred to the superblock on a successful mount,
1966 	 * but if an error occurs before the transfer we have to free
1967 	 * it here.
1968 	 */
1969 	if (mp)
1970 		xfs_mount_free(mp);
1971 }
1972 
1973 static const struct fs_context_operations xfs_context_ops = {
1974 	.parse_param = xfs_fs_parse_param,
1975 	.get_tree    = xfs_fs_get_tree,
1976 	.reconfigure = xfs_fs_reconfigure,
1977 	.free        = xfs_fs_free,
1978 };
1979 
1980 /*
1981  * WARNING: do not initialise any parameters in this function that depend on
1982  * mount option parsing having already been performed as this can be called from
1983  * fsopen() before any parameters have been set.
1984  */
1985 static int xfs_init_fs_context(
1986 	struct fs_context	*fc)
1987 {
1988 	struct xfs_mount	*mp;
1989 
1990 	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
1991 	if (!mp)
1992 		return -ENOMEM;
1993 
1994 	spin_lock_init(&mp->m_sb_lock);
1995 	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
1996 	spin_lock_init(&mp->m_perag_lock);
1997 	mutex_init(&mp->m_growlock);
1998 	INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
1999 	INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
2000 	mp->m_kobj.kobject.kset = xfs_kset;
2001 	/*
2002 	 * We don't create the finobt per-ag space reservation until after log
2003 	 * recovery, so we must set this to true so that an ifree transaction
2004 	 * started during log recovery will not depend on space reservations
2005 	 * for finobt expansion.
2006 	 */
2007 	mp->m_finobt_nores = true;
2008 
2009 	/*
2010 	 * These can be overridden by the mount option parsing.
2011 	 */
2012 	mp->m_logbufs = -1;
2013 	mp->m_logbsize = -1;
2014 	mp->m_allocsize_log = 16; /* 64k */
2015 
2016 	fc->s_fs_info = mp;
2017 	fc->ops = &xfs_context_ops;
2018 
2019 	return 0;
2020 }
2021 
2022 static void
2023 xfs_kill_sb(
2024 	struct super_block		*sb)
2025 {
2026 	kill_block_super(sb);
2027 	xfs_mount_free(XFS_M(sb));
2028 }
2029 
2030 static struct file_system_type xfs_fs_type = {
2031 	.owner			= THIS_MODULE,
2032 	.name			= "xfs",
2033 	.init_fs_context	= xfs_init_fs_context,
2034 	.parameters		= xfs_fs_parameters,
2035 	.kill_sb		= xfs_kill_sb,
2036 	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
2037 };
2038 MODULE_ALIAS_FS("xfs");
2039 
2040 STATIC int __init
2041 xfs_init_caches(void)
2042 {
2043 	int		error;
2044 
2045 	xfs_buf_cache = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
2046 					 SLAB_HWCACHE_ALIGN |
2047 					 SLAB_RECLAIM_ACCOUNT |
2048 					 SLAB_MEM_SPREAD,
2049 					 NULL);
2050 	if (!xfs_buf_cache)
2051 		goto out;
2052 
2053 	xfs_log_ticket_cache = kmem_cache_create("xfs_log_ticket",
2054 						sizeof(struct xlog_ticket),
2055 						0, 0, NULL);
2056 	if (!xfs_log_ticket_cache)
2057 		goto out_destroy_buf_cache;
2058 
2059 	error = xfs_btree_init_cur_caches();
2060 	if (error)
2061 		goto out_destroy_log_ticket_cache;
2062 
2063 	error = xfs_defer_init_item_caches();
2064 	if (error)
2065 		goto out_destroy_btree_cur_cache;
2066 
2067 	xfs_da_state_cache = kmem_cache_create("xfs_da_state",
2068 					      sizeof(struct xfs_da_state),
2069 					      0, 0, NULL);
2070 	if (!xfs_da_state_cache)
2071 		goto out_destroy_defer_item_cache;
2072 
2073 	xfs_ifork_cache = kmem_cache_create("xfs_ifork",
2074 					   sizeof(struct xfs_ifork),
2075 					   0, 0, NULL);
2076 	if (!xfs_ifork_cache)
2077 		goto out_destroy_da_state_cache;
2078 
2079 	xfs_trans_cache = kmem_cache_create("xfs_trans",
2080 					   sizeof(struct xfs_trans),
2081 					   0, 0, NULL);
2082 	if (!xfs_trans_cache)
2083 		goto out_destroy_ifork_cache;
2084 
2085 
2086 	/*
2087 	 * The size of the cache-allocated buf log item is the maximum
2088 	 * size possible under XFS.  This wastes a little bit of memory,
2089 	 * but it is much faster.
2090 	 */
2091 	xfs_buf_item_cache = kmem_cache_create("xfs_buf_item",
2092 					      sizeof(struct xfs_buf_log_item),
2093 					      0, 0, NULL);
2094 	if (!xfs_buf_item_cache)
2095 		goto out_destroy_trans_cache;
2096 
2097 	xfs_efd_cache = kmem_cache_create("xfs_efd_item",
2098 			xfs_efd_log_item_sizeof(XFS_EFD_MAX_FAST_EXTENTS),
2099 			0, 0, NULL);
2100 	if (!xfs_efd_cache)
2101 		goto out_destroy_buf_item_cache;
2102 
2103 	xfs_efi_cache = kmem_cache_create("xfs_efi_item",
2104 			xfs_efi_log_item_sizeof(XFS_EFI_MAX_FAST_EXTENTS),
2105 			0, 0, NULL);
2106 	if (!xfs_efi_cache)
2107 		goto out_destroy_efd_cache;
2108 
2109 	xfs_inode_cache = kmem_cache_create("xfs_inode",
2110 					   sizeof(struct xfs_inode), 0,
2111 					   (SLAB_HWCACHE_ALIGN |
2112 					    SLAB_RECLAIM_ACCOUNT |
2113 					    SLAB_MEM_SPREAD | SLAB_ACCOUNT),
2114 					   xfs_fs_inode_init_once);
2115 	if (!xfs_inode_cache)
2116 		goto out_destroy_efi_cache;
2117 
2118 	xfs_ili_cache = kmem_cache_create("xfs_ili",
2119 					 sizeof(struct xfs_inode_log_item), 0,
2120 					 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
2121 					 NULL);
2122 	if (!xfs_ili_cache)
2123 		goto out_destroy_inode_cache;
2124 
2125 	xfs_icreate_cache = kmem_cache_create("xfs_icr",
2126 					     sizeof(struct xfs_icreate_item),
2127 					     0, 0, NULL);
2128 	if (!xfs_icreate_cache)
2129 		goto out_destroy_ili_cache;
2130 
2131 	xfs_rud_cache = kmem_cache_create("xfs_rud_item",
2132 					 sizeof(struct xfs_rud_log_item),
2133 					 0, 0, NULL);
2134 	if (!xfs_rud_cache)
2135 		goto out_destroy_icreate_cache;
2136 
2137 	xfs_rui_cache = kmem_cache_create("xfs_rui_item",
2138 			xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
2139 			0, 0, NULL);
2140 	if (!xfs_rui_cache)
2141 		goto out_destroy_rud_cache;
2142 
2143 	xfs_cud_cache = kmem_cache_create("xfs_cud_item",
2144 					 sizeof(struct xfs_cud_log_item),
2145 					 0, 0, NULL);
2146 	if (!xfs_cud_cache)
2147 		goto out_destroy_rui_cache;
2148 
2149 	xfs_cui_cache = kmem_cache_create("xfs_cui_item",
2150 			xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
2151 			0, 0, NULL);
2152 	if (!xfs_cui_cache)
2153 		goto out_destroy_cud_cache;
2154 
2155 	xfs_bud_cache = kmem_cache_create("xfs_bud_item",
2156 					 sizeof(struct xfs_bud_log_item),
2157 					 0, 0, NULL);
2158 	if (!xfs_bud_cache)
2159 		goto out_destroy_cui_cache;
2160 
2161 	xfs_bui_cache = kmem_cache_create("xfs_bui_item",
2162 			xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
2163 			0, 0, NULL);
2164 	if (!xfs_bui_cache)
2165 		goto out_destroy_bud_cache;
2166 
2167 	xfs_attrd_cache = kmem_cache_create("xfs_attrd_item",
2168 					    sizeof(struct xfs_attrd_log_item),
2169 					    0, 0, NULL);
2170 	if (!xfs_attrd_cache)
2171 		goto out_destroy_bui_cache;
2172 
2173 	xfs_attri_cache = kmem_cache_create("xfs_attri_item",
2174 					    sizeof(struct xfs_attri_log_item),
2175 					    0, 0, NULL);
2176 	if (!xfs_attri_cache)
2177 		goto out_destroy_attrd_cache;
2178 
2179 	xfs_iunlink_cache = kmem_cache_create("xfs_iul_item",
2180 					     sizeof(struct xfs_iunlink_item),
2181 					     0, 0, NULL);
2182 	if (!xfs_iunlink_cache)
2183 		goto out_destroy_attri_cache;
2184 
2185 	return 0;
2186 
2187  out_destroy_attri_cache:
2188 	kmem_cache_destroy(xfs_attri_cache);
2189  out_destroy_attrd_cache:
2190 	kmem_cache_destroy(xfs_attrd_cache);
2191  out_destroy_bui_cache:
2192 	kmem_cache_destroy(xfs_bui_cache);
2193  out_destroy_bud_cache:
2194 	kmem_cache_destroy(xfs_bud_cache);
2195  out_destroy_cui_cache:
2196 	kmem_cache_destroy(xfs_cui_cache);
2197  out_destroy_cud_cache:
2198 	kmem_cache_destroy(xfs_cud_cache);
2199  out_destroy_rui_cache:
2200 	kmem_cache_destroy(xfs_rui_cache);
2201  out_destroy_rud_cache:
2202 	kmem_cache_destroy(xfs_rud_cache);
2203  out_destroy_icreate_cache:
2204 	kmem_cache_destroy(xfs_icreate_cache);
2205  out_destroy_ili_cache:
2206 	kmem_cache_destroy(xfs_ili_cache);
2207  out_destroy_inode_cache:
2208 	kmem_cache_destroy(xfs_inode_cache);
2209  out_destroy_efi_cache:
2210 	kmem_cache_destroy(xfs_efi_cache);
2211  out_destroy_efd_cache:
2212 	kmem_cache_destroy(xfs_efd_cache);
2213  out_destroy_buf_item_cache:
2214 	kmem_cache_destroy(xfs_buf_item_cache);
2215  out_destroy_trans_cache:
2216 	kmem_cache_destroy(xfs_trans_cache);
2217  out_destroy_ifork_cache:
2218 	kmem_cache_destroy(xfs_ifork_cache);
2219  out_destroy_da_state_cache:
2220 	kmem_cache_destroy(xfs_da_state_cache);
2221  out_destroy_defer_item_cache:
2222 	xfs_defer_destroy_item_caches();
2223  out_destroy_btree_cur_cache:
2224 	xfs_btree_destroy_cur_caches();
2225  out_destroy_log_ticket_cache:
2226 	kmem_cache_destroy(xfs_log_ticket_cache);
2227  out_destroy_buf_cache:
2228 	kmem_cache_destroy(xfs_buf_cache);
2229  out:
2230 	return -ENOMEM;
2231 }
2232 
2233 STATIC void
2234 xfs_destroy_caches(void)
2235 {
2236 	/*
2237 	 * Make sure all delayed rcu free are flushed before we
2238 	 * destroy caches.
2239 	 */
2240 	rcu_barrier();
2241 	kmem_cache_destroy(xfs_iunlink_cache);
2242 	kmem_cache_destroy(xfs_attri_cache);
2243 	kmem_cache_destroy(xfs_attrd_cache);
2244 	kmem_cache_destroy(xfs_bui_cache);
2245 	kmem_cache_destroy(xfs_bud_cache);
2246 	kmem_cache_destroy(xfs_cui_cache);
2247 	kmem_cache_destroy(xfs_cud_cache);
2248 	kmem_cache_destroy(xfs_rui_cache);
2249 	kmem_cache_destroy(xfs_rud_cache);
2250 	kmem_cache_destroy(xfs_icreate_cache);
2251 	kmem_cache_destroy(xfs_ili_cache);
2252 	kmem_cache_destroy(xfs_inode_cache);
2253 	kmem_cache_destroy(xfs_efi_cache);
2254 	kmem_cache_destroy(xfs_efd_cache);
2255 	kmem_cache_destroy(xfs_buf_item_cache);
2256 	kmem_cache_destroy(xfs_trans_cache);
2257 	kmem_cache_destroy(xfs_ifork_cache);
2258 	kmem_cache_destroy(xfs_da_state_cache);
2259 	xfs_defer_destroy_item_caches();
2260 	xfs_btree_destroy_cur_caches();
2261 	kmem_cache_destroy(xfs_log_ticket_cache);
2262 	kmem_cache_destroy(xfs_buf_cache);
2263 }
2264 
2265 STATIC int __init
2266 xfs_init_workqueues(void)
2267 {
2268 	/*
2269 	 * The allocation workqueue can be used in memory reclaim situations
2270 	 * (writepage path), and parallelism is only limited by the number of
2271 	 * AGs in all the filesystems mounted. Hence use the default large
2272 	 * max_active value for this workqueue.
2273 	 */
2274 	xfs_alloc_wq = alloc_workqueue("xfsalloc",
2275 			XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE), 0);
2276 	if (!xfs_alloc_wq)
2277 		return -ENOMEM;
2278 
2279 	xfs_discard_wq = alloc_workqueue("xfsdiscard", XFS_WQFLAGS(WQ_UNBOUND),
2280 			0);
2281 	if (!xfs_discard_wq)
2282 		goto out_free_alloc_wq;
2283 
2284 	return 0;
2285 out_free_alloc_wq:
2286 	destroy_workqueue(xfs_alloc_wq);
2287 	return -ENOMEM;
2288 }
2289 
2290 STATIC void
2291 xfs_destroy_workqueues(void)
2292 {
2293 	destroy_workqueue(xfs_discard_wq);
2294 	destroy_workqueue(xfs_alloc_wq);
2295 }
2296 
2297 STATIC int __init
2298 init_xfs_fs(void)
2299 {
2300 	int			error;
2301 
2302 	xfs_check_ondisk_structs();
2303 
2304 	error = xfs_dahash_test();
2305 	if (error)
2306 		return error;
2307 
2308 	printk(KERN_INFO XFS_VERSION_STRING " with "
2309 			 XFS_BUILD_OPTIONS " enabled\n");
2310 
2311 	xfs_dir_startup();
2312 
2313 	error = xfs_init_caches();
2314 	if (error)
2315 		goto out;
2316 
2317 	error = xfs_init_workqueues();
2318 	if (error)
2319 		goto out_destroy_caches;
2320 
2321 	error = xfs_mru_cache_init();
2322 	if (error)
2323 		goto out_destroy_wq;
2324 
2325 	error = xfs_init_procfs();
2326 	if (error)
2327 		goto out_mru_cache_uninit;
2328 
2329 	error = xfs_sysctl_register();
2330 	if (error)
2331 		goto out_cleanup_procfs;
2332 
2333 	xfs_debugfs = xfs_debugfs_mkdir("xfs", NULL);
2334 
2335 	xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
2336 	if (!xfs_kset) {
2337 		error = -ENOMEM;
2338 		goto out_debugfs_unregister;
2339 	}
2340 
2341 	xfsstats.xs_kobj.kobject.kset = xfs_kset;
2342 
2343 	xfsstats.xs_stats = alloc_percpu(struct xfsstats);
2344 	if (!xfsstats.xs_stats) {
2345 		error = -ENOMEM;
2346 		goto out_kset_unregister;
2347 	}
2348 
2349 	error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
2350 			       "stats");
2351 	if (error)
2352 		goto out_free_stats;
2353 
2354 	error = xchk_global_stats_setup(xfs_debugfs);
2355 	if (error)
2356 		goto out_remove_stats_kobj;
2357 
2358 #ifdef DEBUG
2359 	xfs_dbg_kobj.kobject.kset = xfs_kset;
2360 	error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2361 	if (error)
2362 		goto out_remove_scrub_stats;
2363 #endif
2364 
2365 	error = xfs_qm_init();
2366 	if (error)
2367 		goto out_remove_dbg_kobj;
2368 
2369 	error = register_filesystem(&xfs_fs_type);
2370 	if (error)
2371 		goto out_qm_exit;
2372 	return 0;
2373 
2374  out_qm_exit:
2375 	xfs_qm_exit();
2376  out_remove_dbg_kobj:
2377 #ifdef DEBUG
2378 	xfs_sysfs_del(&xfs_dbg_kobj);
2379  out_remove_scrub_stats:
2380 #endif
2381 	xchk_global_stats_teardown();
2382  out_remove_stats_kobj:
2383 	xfs_sysfs_del(&xfsstats.xs_kobj);
2384  out_free_stats:
2385 	free_percpu(xfsstats.xs_stats);
2386  out_kset_unregister:
2387 	kset_unregister(xfs_kset);
2388  out_debugfs_unregister:
2389 	debugfs_remove(xfs_debugfs);
2390 	xfs_sysctl_unregister();
2391  out_cleanup_procfs:
2392 	xfs_cleanup_procfs();
2393  out_mru_cache_uninit:
2394 	xfs_mru_cache_uninit();
2395  out_destroy_wq:
2396 	xfs_destroy_workqueues();
2397  out_destroy_caches:
2398 	xfs_destroy_caches();
2399  out:
2400 	return error;
2401 }
2402 
2403 STATIC void __exit
2404 exit_xfs_fs(void)
2405 {
2406 	xfs_qm_exit();
2407 	unregister_filesystem(&xfs_fs_type);
2408 #ifdef DEBUG
2409 	xfs_sysfs_del(&xfs_dbg_kobj);
2410 #endif
2411 	xchk_global_stats_teardown();
2412 	xfs_sysfs_del(&xfsstats.xs_kobj);
2413 	free_percpu(xfsstats.xs_stats);
2414 	kset_unregister(xfs_kset);
2415 	debugfs_remove(xfs_debugfs);
2416 	xfs_sysctl_unregister();
2417 	xfs_cleanup_procfs();
2418 	xfs_mru_cache_uninit();
2419 	xfs_destroy_workqueues();
2420 	xfs_destroy_caches();
2421 	xfs_uuid_table_free();
2422 }
2423 
2424 module_init(init_xfs_fs);
2425 module_exit(exit_xfs_fs);
2426 
2427 MODULE_AUTHOR("Silicon Graphics, Inc.");
2428 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2429 MODULE_LICENSE("GPL");
2430