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