xfs_super.c 61 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2
/*
3
 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4
 * All Rights Reserved.
Linus Torvalds's avatar
Linus Torvalds committed
5
 */
6

Linus Torvalds's avatar
Linus Torvalds committed
7
#include "xfs.h"
8
#include "xfs_shared.h"
9
#include "xfs_format.h"
10 11
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
Linus Torvalds's avatar
Linus Torvalds committed
12 13 14
#include "xfs_sb.h"
#include "xfs_mount.h"
#include "xfs_inode.h"
15
#include "xfs_btree.h"
Linus Torvalds's avatar
Linus Torvalds committed
16
#include "xfs_bmap.h"
17
#include "xfs_alloc.h"
Christoph Hellwig's avatar
Christoph Hellwig committed
18
#include "xfs_fsops.h"
19
#include "xfs_trans.h"
Linus Torvalds's avatar
Linus Torvalds committed
20
#include "xfs_buf_item.h"
21
#include "xfs_log.h"
22
#include "xfs_log_priv.h"
23
#include "xfs_dir2.h"
24 25 26
#include "xfs_extfree_item.h"
#include "xfs_mru_cache.h"
#include "xfs_inode_item.h"
27
#include "xfs_icache.h"
28
#include "xfs_trace.h"
Dave Chinner's avatar
Dave Chinner committed
29
#include "xfs_icreate_item.h"
30 31
#include "xfs_filestream.h"
#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"
Linus Torvalds's avatar
Linus Torvalds committed
45

46
#include <linux/magic.h>
47 48
#include <linux/fs_context.h>
#include <linux/fs_parser.h>
Linus Torvalds's avatar
Linus Torvalds committed
49

50
static const struct super_operations xfs_super_operations;
51

52
static struct kset *xfs_kset;		/* top-level xfs sysfs dir */
53 54 55
#ifdef DEBUG
static struct xfs_kobj xfs_dbg_kobj;	/* global debug sysfs attrs */
#endif
Linus Torvalds's avatar
Linus Torvalds committed
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#ifdef CONFIG_HOTPLUG_CPU
static LIST_HEAD(xfs_mount_list);
static DEFINE_SPINLOCK(xfs_mount_list_lock);

static inline void xfs_mount_list_add(struct xfs_mount *mp)
{
	spin_lock(&xfs_mount_list_lock);
	list_add(&mp->m_mount_list, &xfs_mount_list);
	spin_unlock(&xfs_mount_list_lock);
}

static inline void xfs_mount_list_del(struct xfs_mount *mp)
{
	spin_lock(&xfs_mount_list_lock);
	list_del(&mp->m_mount_list);
	spin_unlock(&xfs_mount_list_lock);
}
#else /* !CONFIG_HOTPLUG_CPU */
static inline void xfs_mount_list_add(struct xfs_mount *mp) {}
static inline void xfs_mount_list_del(struct xfs_mount *mp) {}
#endif

79 80 81 82 83 84 85 86 87 88 89 90 91
enum xfs_dax_mode {
	XFS_DAX_INODE = 0,
	XFS_DAX_ALWAYS = 1,
	XFS_DAX_NEVER = 2,
};

static void
xfs_mount_set_dax_mode(
	struct xfs_mount	*mp,
	enum xfs_dax_mode	mode)
{
	switch (mode) {
	case XFS_DAX_INODE:
92
		mp->m_features &= ~(XFS_FEAT_DAX_ALWAYS | XFS_FEAT_DAX_NEVER);
93 94
		break;
	case XFS_DAX_ALWAYS:
95 96
		mp->m_features |= XFS_FEAT_DAX_ALWAYS;
		mp->m_features &= ~XFS_FEAT_DAX_NEVER;
97 98
		break;
	case XFS_DAX_NEVER:
99 100
		mp->m_features |= XFS_FEAT_DAX_NEVER;
		mp->m_features &= ~XFS_FEAT_DAX_ALWAYS;
101 102 103 104 105 106 107 108 109 110 111
		break;
	}
}

static const struct constant_table dax_param_enums[] = {
	{"inode",	XFS_DAX_INODE },
	{"always",	XFS_DAX_ALWAYS },
	{"never",	XFS_DAX_NEVER },
	{}
};

112 113 114 115
/*
 * Table driven mount option parser.
 */
enum {
116
	Opt_logbufs, Opt_logbsize, Opt_logdev, Opt_rtdev,
117
	Opt_wsync, Opt_noalign, Opt_swalloc, Opt_sunit, Opt_swidth, Opt_nouuid,
118
	Opt_grpid, Opt_nogrpid, Opt_bsdgroups, Opt_sysvgroups,
119 120 121 122
	Opt_allocsize, Opt_norecovery, Opt_inode64, Opt_inode32, Opt_ikeep,
	Opt_noikeep, Opt_largeio, Opt_nolargeio, Opt_attr2, Opt_noattr2,
	Opt_filestreams, Opt_quota, Opt_noquota, Opt_usrquota, Opt_grpquota,
	Opt_prjquota, Opt_uquota, Opt_gquota, Opt_pquota,
123
	Opt_uqnoenforce, Opt_gqnoenforce, Opt_pqnoenforce, Opt_qnoenforce,
124
	Opt_discard, Opt_nodiscard, Opt_dax, Opt_dax_enum,
125 126
};

127
static const struct fs_parameter_spec xfs_fs_parameters[] = {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	fsparam_u32("logbufs",		Opt_logbufs),
	fsparam_string("logbsize",	Opt_logbsize),
	fsparam_string("logdev",	Opt_logdev),
	fsparam_string("rtdev",		Opt_rtdev),
	fsparam_flag("wsync",		Opt_wsync),
	fsparam_flag("noalign",		Opt_noalign),
	fsparam_flag("swalloc",		Opt_swalloc),
	fsparam_u32("sunit",		Opt_sunit),
	fsparam_u32("swidth",		Opt_swidth),
	fsparam_flag("nouuid",		Opt_nouuid),
	fsparam_flag("grpid",		Opt_grpid),
	fsparam_flag("nogrpid",		Opt_nogrpid),
	fsparam_flag("bsdgroups",	Opt_bsdgroups),
	fsparam_flag("sysvgroups",	Opt_sysvgroups),
	fsparam_string("allocsize",	Opt_allocsize),
	fsparam_flag("norecovery",	Opt_norecovery),
	fsparam_flag("inode64",		Opt_inode64),
	fsparam_flag("inode32",		Opt_inode32),
	fsparam_flag("ikeep",		Opt_ikeep),
	fsparam_flag("noikeep",		Opt_noikeep),
	fsparam_flag("largeio",		Opt_largeio),
	fsparam_flag("nolargeio",	Opt_nolargeio),
	fsparam_flag("attr2",		Opt_attr2),
	fsparam_flag("noattr2",		Opt_noattr2),
	fsparam_flag("filestreams",	Opt_filestreams),
	fsparam_flag("quota",		Opt_quota),
	fsparam_flag("noquota",		Opt_noquota),
	fsparam_flag("usrquota",	Opt_usrquota),
	fsparam_flag("grpquota",	Opt_grpquota),
	fsparam_flag("prjquota",	Opt_prjquota),
	fsparam_flag("uquota",		Opt_uquota),
	fsparam_flag("gquota",		Opt_gquota),
	fsparam_flag("pquota",		Opt_pquota),
	fsparam_flag("uqnoenforce",	Opt_uqnoenforce),
	fsparam_flag("gqnoenforce",	Opt_gqnoenforce),
	fsparam_flag("pqnoenforce",	Opt_pqnoenforce),
	fsparam_flag("qnoenforce",	Opt_qnoenforce),
	fsparam_flag("discard",		Opt_discard),
	fsparam_flag("nodiscard",	Opt_nodiscard),
	fsparam_flag("dax",		Opt_dax),
168
	fsparam_enum("dax",		Opt_dax_enum, dax_param_enums),
169
	{}
170 171
};

172
struct proc_xfs_info {
173 174
	uint64_t	flag;
	char		*str;
175 176
};

177 178 179 180
static int
xfs_fs_show_options(
	struct seq_file		*m,
	struct dentry		*root)
181 182 183
{
	static struct proc_xfs_info xfs_info_set[] = {
		/* the few simple ones we can get from the mount struct */
184 185 186 187 188 189 190 191 192 193 194 195 196
		{ XFS_FEAT_IKEEP,		",ikeep" },
		{ XFS_FEAT_WSYNC,		",wsync" },
		{ XFS_FEAT_NOALIGN,		",noalign" },
		{ XFS_FEAT_SWALLOC,		",swalloc" },
		{ XFS_FEAT_NOUUID,		",nouuid" },
		{ XFS_FEAT_NORECOVERY,		",norecovery" },
		{ XFS_FEAT_ATTR2,		",attr2" },
		{ XFS_FEAT_FILESTREAMS,		",filestreams" },
		{ XFS_FEAT_GRPID,		",grpid" },
		{ XFS_FEAT_DISCARD,		",discard" },
		{ XFS_FEAT_LARGE_IOSIZE,	",largeio" },
		{ XFS_FEAT_DAX_ALWAYS,		",dax=always" },
		{ XFS_FEAT_DAX_NEVER,		",dax=never" },
197 198
		{ 0, NULL }
	};
199
	struct xfs_mount	*mp = XFS_M(root->d_sb);
200 201 202
	struct proc_xfs_info	*xfs_infop;

	for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
203
		if (mp->m_features & xfs_infop->flag)
204 205
			seq_puts(m, xfs_infop->str);
	}
206

207
	seq_printf(m, ",inode%d", xfs_has_small_inums(mp) ? 32 : 64);
208

209
	if (xfs_has_allocsize(mp))
210
		seq_printf(m, ",allocsize=%dk",
211
			   (1 << mp->m_allocsize_log) >> 10);
212 213

	if (mp->m_logbufs > 0)
214
		seq_printf(m, ",logbufs=%d", mp->m_logbufs);
215
	if (mp->m_logbsize > 0)
216
		seq_printf(m, ",logbsize=%dk", mp->m_logbsize >> 10);
217 218

	if (mp->m_logname)
219
		seq_show_option(m, "logdev", mp->m_logname);
220
	if (mp->m_rtname)
221
		seq_show_option(m, "rtdev", mp->m_rtname);
222 223

	if (mp->m_dalign > 0)
224
		seq_printf(m, ",sunit=%d",
225 226
				(int)XFS_FSB_TO_BB(mp, mp->m_dalign));
	if (mp->m_swidth > 0)
227
		seq_printf(m, ",swidth=%d",
228 229
				(int)XFS_FSB_TO_BB(mp, mp->m_swidth));

230 231 232 233
	if (mp->m_qflags & XFS_UQUOTA_ENFD)
		seq_puts(m, ",usrquota");
	else if (mp->m_qflags & XFS_UQUOTA_ACCT)
		seq_puts(m, ",uqnoenforce");
234

235 236 237 238 239 240 241 242 243
	if (mp->m_qflags & XFS_PQUOTA_ENFD)
		seq_puts(m, ",prjquota");
	else if (mp->m_qflags & XFS_PQUOTA_ACCT)
		seq_puts(m, ",pqnoenforce");

	if (mp->m_qflags & XFS_GQUOTA_ENFD)
		seq_puts(m, ",grpquota");
	else if (mp->m_qflags & XFS_GQUOTA_ACCT)
		seq_puts(m, ",gqnoenforce");
244 245

	if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
246
		seq_puts(m, ",noquota");
247 248

	return 0;
249
}
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
static bool
xfs_set_inode_alloc_perag(
	struct xfs_perag	*pag,
	xfs_ino_t		ino,
	xfs_agnumber_t		max_metadata)
{
	if (!xfs_is_inode32(pag->pag_mount)) {
		set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
		return false;
	}

	if (ino > XFS_MAXINUMBER_32) {
		clear_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
		return false;
	}

	set_bit(XFS_AGSTATE_ALLOWS_INODES, &pag->pag_opstate);
	if (pag->pag_agno < max_metadata)
		set_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
	else
		clear_bit(XFS_AGSTATE_PREFERS_METADATA, &pag->pag_opstate);
	return true;
}

277
/*
278 279
 * Set parameters for inode allocation heuristics, taking into account
 * filesystem size and inode32/inode64 mount options; i.e. specifically
280
 * whether or not XFS_FEAT_SMALL_INUMS is set.
281 282
 *
 * Inode allocation patterns are altered only if inode32 is requested
283
 * (XFS_FEAT_SMALL_INUMS), and the filesystem is sufficiently large.
284
 * If altered, XFS_OPSTATE_INODE32 is set as well.
285 286 287 288 289 290
 *
 * An agcount independent of that in the mount structure is provided
 * because in the growfs case, mp->m_sb.sb_agcount is not yet updated
 * to the potentially higher ag count.
 *
 * Returns the maximum AG index which may contain inodes.
291
 */
292
xfs_agnumber_t
293 294 295
xfs_set_inode_alloc(
	struct xfs_mount *mp,
	xfs_agnumber_t	agcount)
296
{
297
	xfs_agnumber_t	index;
298
	xfs_agnumber_t	maxagi = 0;
299 300
	xfs_sb_t	*sbp = &mp->m_sb;
	xfs_agnumber_t	max_metadata;
Eric Sandeen's avatar
Eric Sandeen committed
301 302
	xfs_agino_t	agino;
	xfs_ino_t	ino;
303

304 305 306
	/*
	 * Calculate how much should be reserved for inodes to meet
	 * the max inode percentage.  Used only for inode32.
307
	 */
308
	if (M_IGEO(mp)->maxicount) {
309
		uint64_t	icount;
310 311 312 313 314 315 316

		icount = sbp->sb_dblocks * sbp->sb_imax_pct;
		do_div(icount, 100);
		icount += sbp->sb_agblocks - 1;
		do_div(icount, sbp->sb_agblocks);
		max_metadata = icount;
	} else {
317
		max_metadata = agcount;
318 319
	}

320
	/* Get the last possible inode in the filesystem */
321
	agino =	XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
322 323 324 325
	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);

	/*
	 * If user asked for no more than 32-bit inodes, and the fs is
326
	 * sufficiently large, set XFS_OPSTATE_INODE32 if we must alter
327 328
	 * the allocator to accommodate the request.
	 */
329
	if (xfs_has_small_inums(mp) && ino > XFS_MAXINUMBER_32)
330
		set_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
331
	else
332
		clear_bit(XFS_OPSTATE_INODE32, &mp->m_opstate);
Eric Sandeen's avatar
Eric Sandeen committed
333

334
	for (index = 0; index < agcount; index++) {
335
		struct xfs_perag	*pag;
336

337
		ino = XFS_AGINO_TO_INO(mp, index, agino);
338 339

		pag = xfs_perag_get(mp, index);
340 341
		if (xfs_set_inode_alloc_perag(pag, ino, max_metadata))
			maxagi++;
342 343 344
		xfs_perag_put(pag);
	}

345
	return xfs_is_inode32(mp) ? maxagi : agcount;
346 347
}

348 349 350
static int
xfs_setup_dax_always(
	struct xfs_mount	*mp)
351
{
352 353
	if (!mp->m_ddev_targp->bt_daxdev &&
	    (!mp->m_rtdev_targp || !mp->m_rtdev_targp->bt_daxdev)) {
354 355 356
		xfs_alert(mp,
			"DAX unsupported by block device. Turning off DAX.");
		goto disable_dax;
357 358 359 360 361 362
	}

	if (mp->m_super->s_blocksize != PAGE_SIZE) {
		xfs_alert(mp,
			"DAX not supported for blocksize. Turning off DAX.");
		goto disable_dax;
363 364
	}

365 366 367 368
	if (xfs_has_reflink(mp) &&
	    bdev_is_partition(mp->m_ddev_targp->bt_bdev)) {
		xfs_alert(mp,
			"DAX and reflink cannot work with multi-partitions!");
369 370 371 372 373 374 375 376 377
		return -EINVAL;
	}

	xfs_warn(mp, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
	return 0;

disable_dax:
	xfs_mount_set_dax_mode(mp, XFS_DAX_NEVER);
	return 0;
378 379
}

380 381 382 383 384 385 386 387 388 389 390
static void
xfs_bdev_mark_dead(
	struct block_device	*bdev)
{
	xfs_force_shutdown(bdev->bd_holder, SHUTDOWN_DEVICE_REMOVED);
}

static const struct blk_holder_ops xfs_holder_ops = {
	.mark_dead		= xfs_bdev_mark_dead,
};

Hannes Eder's avatar
Hannes Eder committed
391
STATIC int
Linus Torvalds's avatar
Linus Torvalds committed
392 393 394 395 396 397 398
xfs_blkdev_get(
	xfs_mount_t		*mp,
	const char		*name,
	struct block_device	**bdevp)
{
	int			error = 0;

399
	*bdevp = blkdev_get_by_path(name, BLK_OPEN_READ | BLK_OPEN_WRITE, mp,
400
				    &xfs_holder_ops);
Linus Torvalds's avatar
Linus Torvalds committed
401 402
	if (IS_ERR(*bdevp)) {
		error = PTR_ERR(*bdevp);
403
		xfs_warn(mp, "Invalid device [%s], error=%d", name, error);
Linus Torvalds's avatar
Linus Torvalds committed
404 405
	}

406
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
407 408
}

Hannes Eder's avatar
Hannes Eder committed
409
STATIC void
Linus Torvalds's avatar
Linus Torvalds committed
410
xfs_blkdev_put(
411
	struct xfs_mount	*mp,
Linus Torvalds's avatar
Linus Torvalds committed
412 413 414
	struct block_device	*bdev)
{
	if (bdev)
415
		blkdev_put(bdev, mp);
Linus Torvalds's avatar
Linus Torvalds committed
416 417
}

418 419 420 421 422
STATIC void
xfs_close_devices(
	struct xfs_mount	*mp)
{
	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
423
		struct block_device *logdev = mp->m_logdev_targp->bt_bdev;
424

425
		xfs_free_buftarg(mp->m_logdev_targp);
426
		xfs_blkdev_put(mp, logdev);
427 428
	}
	if (mp->m_rtdev_targp) {
429
		struct block_device *rtdev = mp->m_rtdev_targp->bt_bdev;
430

431
		xfs_free_buftarg(mp->m_rtdev_targp);
432
		xfs_blkdev_put(mp, rtdev);
433
	}
434
	xfs_free_buftarg(mp->m_ddev_targp);
435 436 437 438 439 440 441 442 443 444 445 446 447 448
}

/*
 * The file system configurations are:
 *	(1) device (partition) with data and internal log
 *	(2) logical volume with data and log subvolumes.
 *	(3) logical volume with data, log, and realtime subvolumes.
 *
 * We only have to handle opening the log and realtime volumes here if
 * they are present.  The data subvolume has already been opened by
 * get_sb_bdev() and is stored in sb->s_bdev.
 */
STATIC int
xfs_open_devices(
449
	struct xfs_mount	*mp)
450 451 452 453 454 455 456 457
{
	struct block_device	*ddev = mp->m_super->s_bdev;
	struct block_device	*logdev = NULL, *rtdev = NULL;
	int			error;

	/*
	 * Open real time and log devices - order is important.
	 */
458 459
	if (mp->m_logname) {
		error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
460
		if (error)
461
			return error;
462 463
	}

464 465
	if (mp->m_rtname) {
		error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
466 467 468 469
		if (error)
			goto out_close_logdev;

		if (rtdev == ddev || rtdev == logdev) {
470 471
			xfs_warn(mp,
	"Cannot mount filesystem with identical rtdev and ddev/logdev.");
472
			error = -EINVAL;
473 474 475 476 477 478 479
			goto out_close_rtdev;
		}
	}

	/*
	 * Setup xfs_mount buffer target pointers
	 */
480
	error = -ENOMEM;
481
	mp->m_ddev_targp = xfs_alloc_buftarg(mp, ddev);
482 483 484 485
	if (!mp->m_ddev_targp)
		goto out_close_rtdev;

	if (rtdev) {
486
		mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev);
487 488 489 490 491
		if (!mp->m_rtdev_targp)
			goto out_free_ddev_targ;
	}

	if (logdev && logdev != ddev) {
492
		mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev);
493 494 495 496 497 498 499 500 501 502
		if (!mp->m_logdev_targp)
			goto out_free_rtdev_targ;
	} else {
		mp->m_logdev_targp = mp->m_ddev_targp;
	}

	return 0;

 out_free_rtdev_targ:
	if (mp->m_rtdev_targp)
503
		xfs_free_buftarg(mp->m_rtdev_targp);
504
 out_free_ddev_targ:
505
	xfs_free_buftarg(mp->m_ddev_targp);
506
 out_close_rtdev:
507
	xfs_blkdev_put(mp, rtdev);
508
 out_close_logdev:
509
	if (logdev && logdev != ddev)
510
		xfs_blkdev_put(mp, logdev);
511 512 513
	return error;
}

514 515 516 517 518 519 520 521
/*
 * Setup xfs_mount buffer target pointers based on superblock
 */
STATIC int
xfs_setup_devices(
	struct xfs_mount	*mp)
{
	int			error;
522

523
	error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize);
524 525 526 527 528 529
	if (error)
		return error;

	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
		unsigned int	log_sector_size = BBSIZE;

530
		if (xfs_has_sector(mp))
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
			log_sector_size = mp->m_sb.sb_logsectsize;
		error = xfs_setsize_buftarg(mp->m_logdev_targp,
					    log_sector_size);
		if (error)
			return error;
	}
	if (mp->m_rtdev_targp) {
		error = xfs_setsize_buftarg(mp->m_rtdev_targp,
					    mp->m_sb.sb_sectsize);
		if (error)
			return error;
	}

	return 0;
}
546

547 548 549 550
STATIC int
xfs_init_mount_workqueues(
	struct xfs_mount	*mp)
{
551
	mp->m_buf_workqueue = alloc_workqueue("xfs-buf/%s",
552 553
			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
			1, mp->m_super->s_id);
554 555 556
	if (!mp->m_buf_workqueue)
		goto out;

557
	mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
558 559
			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
			0, mp->m_super->s_id);
560
	if (!mp->m_unwritten_workqueue)
561
		goto out_destroy_buf;
562

563
	mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
564 565
			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
			0, mp->m_super->s_id);
566
	if (!mp->m_reclaim_workqueue)
567
		goto out_destroy_unwritten;
568

569 570
	mp->m_blockgc_wq = alloc_workqueue("xfs-blockgc/%s",
			XFS_WQFLAGS(WQ_UNBOUND | WQ_FREEZABLE | WQ_MEM_RECLAIM),
571
			0, mp->m_super->s_id);
572
	if (!mp->m_blockgc_wq)
573
		goto out_destroy_reclaim;
574

575 576 577 578 579 580
	mp->m_inodegc_wq = alloc_workqueue("xfs-inodegc/%s",
			XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM),
			1, mp->m_super->s_id);
	if (!mp->m_inodegc_wq)
		goto out_destroy_blockgc;

581 582
	mp->m_sync_workqueue = alloc_workqueue("xfs-sync/%s",
			XFS_WQFLAGS(WQ_FREEZABLE), 0, mp->m_super->s_id);
583
	if (!mp->m_sync_workqueue)
584
		goto out_destroy_inodegc;
585

586 587
	return 0;

588 589 590 591
out_destroy_inodegc:
	destroy_workqueue(mp->m_inodegc_wq);
out_destroy_blockgc:
	destroy_workqueue(mp->m_blockgc_wq);
592 593
out_destroy_reclaim:
	destroy_workqueue(mp->m_reclaim_workqueue);
594 595
out_destroy_unwritten:
	destroy_workqueue(mp->m_unwritten_workqueue);
596 597
out_destroy_buf:
	destroy_workqueue(mp->m_buf_workqueue);
598 599 600 601 602 603 604 605
out:
	return -ENOMEM;
}

STATIC void
xfs_destroy_mount_workqueues(
	struct xfs_mount	*mp)
{
606
	destroy_workqueue(mp->m_sync_workqueue);
607 608
	destroy_workqueue(mp->m_blockgc_wq);
	destroy_workqueue(mp->m_inodegc_wq);
609
	destroy_workqueue(mp->m_reclaim_workqueue);
610
	destroy_workqueue(mp->m_unwritten_workqueue);
611
	destroy_workqueue(mp->m_buf_workqueue);
612 613
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627
static void
xfs_flush_inodes_worker(
	struct work_struct	*work)
{
	struct xfs_mount	*mp = container_of(work, struct xfs_mount,
						   m_flush_inodes_work);
	struct super_block	*sb = mp->m_super;

	if (down_read_trylock(&sb->s_umount)) {
		sync_inodes_sb(sb);
		up_read(&sb->s_umount);
	}
}

628 629 630 631 632 633 634 635 636 637
/*
 * Flush all dirty data to disk. Must not be called while holding an XFS_ILOCK
 * or a page lock. We use sync_inodes_sb() here to ensure we block while waiting
 * for IO to complete so that we effectively throttle multiple callers to the
 * rate at which IO is completing.
 */
void
xfs_flush_inodes(
	struct xfs_mount	*mp)
{
638 639 640 641 642
	/*
	 * If flush_work() returns true then that means we waited for a flush
	 * which was already in progress.  Don't bother running another scan.
	 */
	if (flush_work(&mp->m_flush_inodes_work))
643 644
		return;

645 646
	queue_work(mp->m_sync_workqueue, &mp->m_flush_inodes_work);
	flush_work(&mp->m_flush_inodes_work);
647 648
}

649
/* Catch misguided souls that try to use this interface on XFS */
Linus Torvalds's avatar
Linus Torvalds committed
650
STATIC struct inode *
651
xfs_fs_alloc_inode(
Linus Torvalds's avatar
Linus Torvalds committed
652 653
	struct super_block	*sb)
{
654
	BUG();
655
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
656 657
}

658
/*
659
 * Now that the generic code is guaranteed not to be accessing
660
 * the linux inode, we can inactivate and reclaim the inode.
661
 */
Linus Torvalds's avatar
Linus Torvalds committed
662
STATIC void
663
xfs_fs_destroy_inode(
664
	struct inode		*inode)
Linus Torvalds's avatar
Linus Torvalds committed
665
{
666 667
	struct xfs_inode	*ip = XFS_I(inode);

668
	trace_xfs_destroy_inode(ip);
669

670
	ASSERT(!rwsem_is_locked(&inode->i_rwsem));
671 672
	XFS_STATS_INC(ip->i_mount, vn_rele);
	XFS_STATS_INC(ip->i_mount, vn_remove);
673
	xfs_inode_mark_reclaimable(ip);
Linus Torvalds's avatar
Linus Torvalds committed
674 675
}

676 677 678
static void
xfs_fs_dirty_inode(
	struct inode			*inode,
679
	int				flags)
680 681 682 683 684 685 686
{
	struct xfs_inode		*ip = XFS_I(inode);
	struct xfs_mount		*mp = ip->i_mount;
	struct xfs_trans		*tp;

	if (!(inode->i_sb->s_flags & SB_LAZYTIME))
		return;
687 688 689 690 691 692 693

	/*
	 * Only do the timestamp update if the inode is dirty (I_DIRTY_SYNC)
	 * and has dirty timestamp (I_DIRTY_TIME). I_DIRTY_TIME can be passed
	 * in flags possibly together with I_DIRTY_SYNC.
	 */
	if ((flags & ~I_DIRTY_TIME) != I_DIRTY_SYNC || !(flags & I_DIRTY_TIME))
694 695 696 697 698 699 700 701 702 703
		return;

	if (xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp))
		return;
	xfs_ilock(ip, XFS_ILOCK_EXCL);
	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
	xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
	xfs_trans_commit(tp);
}

704 705 706 707
/*
 * Slab object creation initialisation for the XFS inode.
 * This covers only the idempotent fields in the XFS inode;
 * all other fields need to be initialised on allocation
708
 * from the slab. This avoids the need to repeatedly initialise
709 710 711
 * fields in the xfs inode that left in the initialise state
 * when freeing the inode.
 */
712 713
STATIC void
xfs_fs_inode_init_once(
714 715 716 717 718
	void			*inode)
{
	struct xfs_inode	*ip = inode;

	memset(ip, 0, sizeof(struct xfs_inode));
719 720 721 722 723

	/* vfs inode */
	inode_init_once(VFS_I(ip));

	/* xfs inode */
724 725 726 727 728 729 730
	atomic_set(&ip->i_pincount, 0);
	spin_lock_init(&ip->i_flags_lock);

	mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
		     "xfsino", ip->i_ino);
}

731 732 733 734 735 736 737 738 739 740 741 742 743
/*
 * We do an unlocked check for XFS_IDONTCACHE here because we are already
 * serialised against cache hits here via the inode->i_lock and igrab() in
 * xfs_iget_cache_hit(). Hence a lookup that might clear this flag will not be
 * racing with us, and it avoids needing to grab a spinlock here for every inode
 * we drop the final reference on.
 */
STATIC int
xfs_fs_drop_inode(
	struct inode		*inode)
{
	struct xfs_inode	*ip = XFS_I(inode);

744 745 746 747 748 749
	/*
	 * If this unlinked inode is in the middle of recovery, don't
	 * drop the inode just yet; log recovery will take care of
	 * that.  See the comment for this inode flag.
	 */
	if (ip->i_flags & XFS_IRECOVERY) {
750
		ASSERT(xlog_recovery_needed(ip->i_mount->m_log));
751 752 753
		return 0;
	}

754
	return generic_drop_inode(inode);
755 756
}

757 758
static void
xfs_mount_free(
759 760 761 762
	struct xfs_mount	*mp)
{
	kfree(mp->m_rtname);
	kfree(mp->m_logname);
763
	kmem_free(mp);
764 765
}

Linus Torvalds's avatar
Linus Torvalds committed
766
STATIC int
Christoph Hellwig's avatar
Christoph Hellwig committed
767
xfs_fs_sync_fs(
Linus Torvalds's avatar
Linus Torvalds committed
768 769 770
	struct super_block	*sb,
	int			wait)
{
771
	struct xfs_mount	*mp = XFS_M(sb);
772
	int			error;
Linus Torvalds's avatar
Linus Torvalds committed
773

774 775
	trace_xfs_fs_sync_fs(mp, __return_address);

776
	/*
Christoph Hellwig's avatar
Christoph Hellwig committed
777
	 * Doing anything during the async pass would be counterproductive.
778
	 */
Christoph Hellwig's avatar
Christoph Hellwig committed
779
	if (!wait)
Christoph Hellwig's avatar
Christoph Hellwig committed
780 781
		return 0;

782 783 784 785
	error = xfs_log_force(mp, XFS_LOG_SYNC);
	if (error)
		return error;

Christoph Hellwig's avatar
Christoph Hellwig committed
786
	if (laptop_mode) {
Linus Torvalds's avatar
Linus Torvalds committed
787 788
		/*
		 * The disk must be active because we're syncing.
789
		 * We schedule log work now (now that the disk is
Linus Torvalds's avatar
Linus Torvalds committed
790 791
		 * active) instead of later (when it might not be).
		 */
792
		flush_delayed_work(&mp->m_log->l_work);
Linus Torvalds's avatar
Linus Torvalds committed
793 794
	}

795 796 797 798 799 800
	/*
	 * If we are called with page faults frozen out, it means we are about
	 * to freeze the transaction subsystem. Take the opportunity to shut
	 * down inodegc because once SB_FREEZE_FS is set it's too late to
	 * prevent inactivation races with freeze. The fs doesn't get called
	 * again by the freezing process until after SB_FREEZE_FS has been set,
801 802
	 * so it's now or never.  Same logic applies to speculative allocation
	 * garbage collection.
803 804 805 806 807 808
	 *
	 * We don't care if this is a normal syncfs call that does this or
	 * freeze that does this - we can run this multiple times without issue
	 * and we won't race with a restart because a restart can only occur
	 * when the state is either SB_FREEZE_FS or SB_FREEZE_COMPLETE.
	 */
809
	if (sb->s_writers.frozen == SB_FREEZE_PAGEFAULT) {
810
		xfs_inodegc_stop(mp);
811 812
		xfs_blockgc_stop(mp);
	}
813

Christoph Hellwig's avatar
Christoph Hellwig committed
814
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817
}

STATIC int
818
xfs_fs_statfs(
819
	struct dentry		*dentry,
Linus Torvalds's avatar
Linus Torvalds committed
820 821
	struct kstatfs		*statp)
{
822 823
	struct xfs_mount	*mp = XFS_M(dentry->d_sb);
	xfs_sb_t		*sbp = &mp->m_sb;
824
	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
825 826 827 828
	uint64_t		fakeinos, id;
	uint64_t		icount;
	uint64_t		ifree;
	uint64_t		fdblocks;
829
	xfs_extlen_t		lsize;
830
	int64_t			ffree;
831

832 833 834 835 836
	/*
	 * Expedite background inodegc but don't wait. We do not want to block
	 * here waiting hours for a billion extent file to be truncated.
	 */
	xfs_inodegc_push(mp);
837

838
	statp->f_type = XFS_SUPER_MAGIC;
839 840 841
	statp->f_namelen = MAXNAMELEN - 1;

	id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
842
	statp->f_fsid = u64_to_fsid(id);
843

844
	icount = percpu_counter_sum(&mp->m_icount);
845
	ifree = percpu_counter_sum(&mp->m_ifree);
846
	fdblocks = percpu_counter_sum(&mp->m_fdblocks);
847 848 849 850 851

	spin_lock(&mp->m_sb_lock);
	statp->f_bsize = sbp->sb_blocksize;
	lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
	statp->f_blocks = sbp->sb_dblocks - lsize;
852 853
	spin_unlock(&mp->m_sb_lock);

854
	/* make sure statp->f_bfree does not underflow */
855 856
	statp->f_bfree = max_t(int64_t, 0,
				fdblocks - xfs_fdblocks_unavailable(mp));
857 858
	statp->f_bavail = statp->f_bfree;

859
	fakeinos = XFS_FSB_TO_INO(mp, statp->f_bfree);
Dave Chinner's avatar
Dave Chinner committed
860
	statp->f_files = min(icount + fakeinos, (uint64_t)XFS_MAXINUMBER);
861
	if (M_IGEO(mp)->maxicount)
862 863
		statp->f_files = min_t(typeof(statp->f_files),
					statp->f_files,
864
					M_IGEO(mp)->maxicount);
865

866 867 868 869 870
	/* If sb_icount overshot maxicount, report actual allocation */
	statp->f_files = max_t(typeof(statp->f_files),
					statp->f_files,
					sbp->sb_icount);

871
	/* make sure statp->f_ffree does not underflow */
872
	ffree = statp->f_files - (icount - ifree);
873
	statp->f_ffree = max_t(int64_t, ffree, 0);
874

875

876
	if ((ip->i_diflags & XFS_DIFLAG_PROJINHERIT) &&
877 878
	    ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
			      (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
Christoph Hellwig's avatar
Christoph Hellwig committed
879
		xfs_qm_statvfs(ip, statp);
880 881

	if (XFS_IS_REALTIME_MOUNT(mp) &&
882
	    (ip->i_diflags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) {
883 884
		s64	freertx;

885
		statp->f_blocks = sbp->sb_rblocks;
886 887
		freertx = percpu_counter_sum_positive(&mp->m_frextents);
		statp->f_bavail = statp->f_bfree = freertx * sbp->sb_rextsize;
888 889
	}

890
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
891 892
}

893 894 895
STATIC void
xfs_save_resvblks(struct xfs_mount *mp)
{
896
	uint64_t resblks = 0;
897 898 899 900 901 902 903 904

	mp->m_resblks_save = mp->m_resblks;
	xfs_reserve_blocks(mp, &resblks, NULL);
}

STATIC void
xfs_restore_resvblks(struct xfs_mount *mp)
{
905
	uint64_t resblks;
906 907 908 909 910 911 912 913 914 915

	if (mp->m_resblks_save) {
		resblks = mp->m_resblks_save;
		mp->m_resblks_save = 0;
	} else
		resblks = xfs_default_resblks(mp);

	xfs_reserve_blocks(mp, &resblks, NULL);
}

916 917 918 919 920 921 922 923 924 925 926
/*
 * Second stage of a freeze. The data is already frozen so we only
 * need to take care of the metadata. Once that's done sync the superblock
 * to the log to dirty it in case of a crash while frozen. This ensures that we
 * will recover the unlinked inode lists on the next mount.
 */
STATIC int
xfs_fs_freeze(
	struct super_block	*sb)
{
	struct xfs_mount	*mp = XFS_M(sb);
927 928
	unsigned int		flags;
	int			ret;
929

930 931 932 933 934 935
	/*
	 * The filesystem is now frozen far enough that memory reclaim
	 * cannot safely operate on the filesystem. Hence we need to
	 * set a GFP_NOFS context here to avoid recursion deadlocks.
	 */
	flags = memalloc_nofs_save();
936
	xfs_save_resvblks(mp);
937
	ret = xfs_log_quiesce(mp);
938
	memalloc_nofs_restore(flags);
939 940 941 942 943 944 945 946

	/*
	 * For read-write filesystems, we need to restart the inodegc on error
	 * because we stopped it at SB_FREEZE_PAGEFAULT level and a thaw is not
	 * going to be run to restart it now.  We are at SB_FREEZE_FS level
	 * here, so we can restart safely without racing with a stop in
	 * xfs_fs_sync_fs().
	 */
947
	if (ret && !xfs_is_readonly(mp)) {
948
		xfs_blockgc_start(mp);
949
		xfs_inodegc_start(mp);
950
	}
951

952
	return ret;
953 954 955 956 957 958 959 960 961 962
}

STATIC int
xfs_fs_unfreeze(
	struct super_block	*sb)
{
	struct xfs_mount	*mp = XFS_M(sb);

	xfs_restore_resvblks(mp);
	xfs_log_work_queue(mp);
963 964 965

	/*
	 * Don't reactivate the inodegc worker on a readonly filesystem because
966 967 968
	 * inodes are sent directly to reclaim.  Don't reactivate the blockgc
	 * worker because there are no speculative preallocations on a readonly
	 * filesystem.
969
	 */
970
	if (!xfs_is_readonly(mp)) {
971
		xfs_blockgc_start(mp);
972
		xfs_inodegc_start(mp);
973
	}
974

975 976 977 978 979 980 981 982 983 984 985 986
	return 0;
}

/*
 * This function fills in xfs_mount_t fields based on mount args.
 * Note: the superblock _has_ now been read in.
 */
STATIC int
xfs_finish_flags(
	struct xfs_mount	*mp)
{
	/* Fail a mount where the logbuf is smaller than the log stripe */
987
	if (xfs_has_logv2(mp)) {
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
		if (mp->m_logbsize <= 0 &&
		    mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
			mp->m_logbsize = mp->m_sb.sb_logsunit;
		} else if (mp->m_logbsize > 0 &&
			   mp->m_logbsize < mp->m_sb.sb_logsunit) {
			xfs_warn(mp,
		"logbuf size must be greater than or equal to log stripe size");
			return -EINVAL;
		}
	} else {
		/* Fail a mount if the logbuf is larger than 32K */
		if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
			xfs_warn(mp,
		"logbuf size for version 1 logs must be 16K or 32K");
			return -EINVAL;
		}
	}

	/*
	 * V5 filesystems always use attr2 format for attributes.
	 */
1009
	if (xfs_has_crc(mp) && xfs_has_noattr2(mp)) {
1010 1011 1012 1013 1014 1015 1016 1017
		xfs_warn(mp, "Cannot mount a V5 filesystem as noattr2. "
			     "attr2 is always enabled for V5 filesystems.");
		return -EINVAL;
	}

	/*
	 * prohibit r/w mounts of read-only filesystems
	 */
1018
	if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !xfs_is_readonly(mp)) {
1019 1020 1021 1022 1023
		xfs_warn(mp,
			"cannot mount a read-only filesystem as read-write");
		return -EROFS;
	}

1024 1025
	if ((mp->m_qflags & XFS_GQUOTA_ACCT) &&
	    (mp->m_qflags & XFS_PQUOTA_ACCT) &&
1026
	    !xfs_has_pquotino(mp)) {
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
		xfs_warn(mp,
		  "Super block does not support project and group quota together");
		return -EINVAL;
	}

	return 0;
}

static int
xfs_init_percpu_counters(
	struct xfs_mount	*mp)
{
	int		error;

	error = percpu_counter_init(&mp->m_icount, 0, GFP_KERNEL);
	if (error)
		return -ENOMEM;

	error = percpu_counter_init(&mp->m_ifree, 0, GFP_KERNEL);
	if (error)
		goto free_icount;

	error = percpu_counter_init(&mp->m_fdblocks, 0, GFP_KERNEL);
	if (error)
		goto free_ifree;

	error = percpu_counter_init(&mp->m_delalloc_blks, 0, GFP_KERNEL);
	if (error)
		goto free_fdblocks;

1057 1058 1059 1060
	error = percpu_counter_init(&mp->m_frextents, 0, GFP_KERNEL);
	if (error)
		goto free_delalloc;

1061 1062
	return 0;

1063 1064
free_delalloc:
	percpu_counter_destroy(&mp->m_delalloc_blks);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
free_fdblocks:
	percpu_counter_destroy(&mp->m_fdblocks);
free_ifree:
	percpu_counter_destroy(&mp->m_ifree);
free_icount:
	percpu_counter_destroy(&mp->m_icount);
	return -ENOMEM;
}

void
xfs_reinit_percpu_counters(
	struct xfs_mount	*mp)
{
	percpu_counter_set(&mp->m_icount, mp->m_sb.sb_icount);
	percpu_counter_set(&mp->m_ifree, mp->m_sb.sb_ifree);
	percpu_counter_set(&mp->m_fdblocks, mp->m_sb.sb_fdblocks);
1081
	percpu_counter_set(&mp->m_frextents, mp->m_sb.sb_frextents);
1082 1083 1084 1085 1086 1087 1088 1089 1090
}

static void
xfs_destroy_percpu_counters(
	struct xfs_mount	*mp)
{
	percpu_counter_destroy(&mp->m_icount);
	percpu_counter_destroy(&mp->m_ifree);
	percpu_counter_destroy(&mp->m_fdblocks);
1091
	ASSERT(xfs_is_shutdown(mp) ||
1092 1093
	       percpu_counter_sum(&mp->m_delalloc_blks) == 0);
	percpu_counter_destroy(&mp->m_delalloc_blks);
1094
	percpu_counter_destroy(&mp->m_frextents);
1095 1096
}

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
static int
xfs_inodegc_init_percpu(
	struct xfs_mount	*mp)
{
	struct xfs_inodegc	*gc;
	int			cpu;

	mp->m_inodegc = alloc_percpu(struct xfs_inodegc);
	if (!mp->m_inodegc)
		return -ENOMEM;

	for_each_possible_cpu(cpu) {
		gc = per_cpu_ptr(mp->m_inodegc, cpu);
1110 1111 1112
#if defined(DEBUG) || defined(XFS_WARN)
		gc->cpu = cpu;
#endif
1113 1114
		init_llist_head(&gc->list);
		gc->items = 0;
1115
		gc->error = 0;
1116
		INIT_DELAYED_WORK(&gc->work, xfs_inodegc_worker);
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
	}
	return 0;
}

static void
xfs_inodegc_free_percpu(
	struct xfs_mount	*mp)
{
	if (!mp->m_inodegc)
		return;
	free_percpu(mp->m_inodegc);
}

1130 1131 1132 1133 1134 1135
static void
xfs_fs_put_super(
	struct super_block	*sb)
{
	struct xfs_mount	*mp = XFS_M(sb);

1136
	xfs_notice(mp, "Unmounting Filesystem %pU", &mp->m_sb.sb_uuid);
1137 1138 1139 1140 1141
	xfs_filestream_unmount(mp);
	xfs_unmountfs(mp);

	xfs_freesb(mp);
	free_percpu(mp->m_stats.xs_stats);
1142
	xfs_mount_list_del(mp);
1143
	xfs_inodegc_free_percpu(mp);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	xfs_destroy_percpu_counters(mp);
	xfs_destroy_mount_workqueues(mp);
	xfs_close_devices(mp);

	sb->s_fs_info = NULL;
	xfs_mount_free(mp);
}

static long
xfs_fs_nr_cached_objects(
	struct super_block	*sb,
	struct shrink_control	*sc)
{
	/* Paranoia: catch incorrect calls during mount setup or teardown */
	if (WARN_ON_ONCE(!sb->s_fs_info))
		return 0;
	return xfs_reclaim_inodes_count(XFS_M(sb));
}

static long
xfs_fs_free_cached_objects(
	struct super_block	*sb,
	struct shrink_control	*sc)
1167
{
1168 1169
	return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
}
1170

1171 1172 1173 1174 1175 1176 1177
static void
xfs_fs_shutdown(
	struct super_block	*sb)
{
	xfs_force_shutdown(XFS_M(sb), SHUTDOWN_DEVICE_REMOVED);
}

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
static const struct super_operations xfs_super_operations = {
	.alloc_inode		= xfs_fs_alloc_inode,
	.destroy_inode		= xfs_fs_destroy_inode,
	.dirty_inode		= xfs_fs_dirty_inode,
	.drop_inode		= xfs_fs_drop_inode,
	.put_super		= xfs_fs_put_super,
	.sync_fs		= xfs_fs_sync_fs,
	.freeze_fs		= xfs_fs_freeze,
	.unfreeze_fs		= xfs_fs_unfreeze,
	.statfs			= xfs_fs_statfs,
	.show_options		= xfs_fs_show_options,
	.nr_cached_objects	= xfs_fs_nr_cached_objects,
	.free_cached_objects	= xfs_fs_free_cached_objects,
1191
	.shutdown		= xfs_fs_shutdown,
1192
};
1193

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
static int
suffix_kstrtoint(
	const char	*s,
	unsigned int	base,
	int		*res)
{
	int		last, shift_left_factor = 0, _res;
	char		*value;
	int		ret = 0;

	value = kstrdup(s, GFP_KERNEL);
	if (!value)
		return -ENOMEM;

	last = strlen(value) - 1;
	if (value[last] == 'K' || value[last] == 'k') {
		shift_left_factor = 10;
		value[last] = '\0';
	}
	if (value[last] == 'M' || value[last] == 'm') {
		shift_left_factor = 20;
		value[last] = '\0';
	}
	if (value[last] == 'G' || value[last] == 'g') {
		shift_left_factor = 30;
		value[last] = '\0';
	}

	if (kstrtoint(value, base, &_res))
		ret = -EINVAL;
	kfree(value);
	*res = _res << shift_left_factor;
	return ret;
1227 1228
}

1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
static inline void
xfs_fs_warn_deprecated(
	struct fs_context	*fc,
	struct fs_parameter	*param,
	uint64_t		flag,
	bool			value)
{
	/* Don't print the warning if reconfiguring and current mount point
	 * already had the flag set
	 */
	if ((fc->purpose & FS_CONTEXT_FOR_RECONFIGURE) &&
1240
            !!(XFS_M(fc->root->d_sb)->m_features & flag) == value)
1241 1242 1243 1244
		return;
	xfs_warn(fc->s_fs_info, "%s mount option is deprecated.", param->key);
}

Christoph Hellwig's avatar
Christoph Hellwig committed
1245
/*
1246 1247 1248
 * Set mount state from a mount option.
 *
 * NOTE: mp->m_super is NULL here!
Christoph Hellwig's avatar
Christoph Hellwig committed
1249
 */
1250
static int
1251
xfs_fs_parse_param(
1252 1253
	struct fs_context	*fc,
	struct fs_parameter	*param)
Linus Torvalds's avatar
Linus Torvalds committed
1254
{
1255
	struct xfs_mount	*parsing_mp = fc->s_fs_info;
1256 1257 1258
	struct fs_parse_result	result;
	int			size = 0;
	int			opt;
Christoph Hellwig's avatar
Christoph Hellwig committed
1259

1260
	opt = fs_parse(fc, xfs_fs_parameters, param, &result);
1261 1262
	if (opt < 0)
		return opt;
Linus Torvalds's avatar
Linus Torvalds committed
1263

1264 1265
	switch (opt) {
	case Opt_logbufs:
1266
		parsing_mp->m_logbufs = result.uint_32;
1267 1268
		return 0;
	case Opt_logbsize:
1269
		if (suffix_kstrtoint(param->string, 10, &parsing_mp->m_logbsize))
1270 1271 1272
			return -EINVAL;
		return 0;
	case Opt_logdev:
1273 1274 1275
		kfree(parsing_mp->m_logname);
		parsing_mp->m_logname = kstrdup(param->string, GFP_KERNEL);
		if (!parsing_mp->m_logname)
1276 1277 1278
			return -ENOMEM;
		return 0;
	case Opt_rtdev:
1279 1280 1281
		kfree(parsing_mp->m_rtname);
		parsing_mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
		if (!parsing_mp->m_rtname)
1282 1283 1284 1285 1286
			return -ENOMEM;
		return 0;
	case Opt_allocsize:
		if (suffix_kstrtoint(param->string, 10, &size))
			return -EINVAL;
1287
		parsing_mp->m_allocsize_log = ffs(size) - 1;
1288
		parsing_mp->m_features |= XFS_FEAT_ALLOCSIZE;
1289 1290 1291
		return 0;
	case Opt_grpid:
	case Opt_bsdgroups:
1292
		parsing_mp->m_features |= XFS_FEAT_GRPID;
1293 1294 1295
		return 0;
	case Opt_nogrpid:
	case Opt_sysvgroups:
1296
		parsing_mp->m_features &= ~XFS_FEAT_GRPID;
1297 1298
		return 0;
	case Opt_wsync:
1299
		parsing_mp->m_features |= XFS_FEAT_WSYNC;
1300 1301
		return 0;
	case Opt_norecovery:
1302
		parsing_mp->m_features |= XFS_FEAT_NORECOVERY;
1303 1304
		return 0;
	case Opt_noalign:
1305
		parsing_mp->m_features |= XFS_FEAT_NOALIGN;
1306 1307
		return 0;
	case Opt_swalloc:
1308
		parsing_mp->m_features |= XFS_FEAT_SWALLOC;
1309 1310
		return 0;
	case Opt_sunit:
1311
		parsing_mp->m_dalign = result.uint_32;
1312 1313
		return 0;
	case Opt_swidth:
1314
		parsing_mp->m_swidth = result.uint_32;
1315 1316
		return 0;
	case Opt_inode32:
1317
		parsing_mp->m_features |= XFS_FEAT_SMALL_INUMS;
1318 1319
		return 0;
	case Opt_inode64:
1320
		parsing_mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1321 1322
		return 0;
	case Opt_nouuid:
1323
		parsing_mp->m_features |= XFS_FEAT_NOUUID;
1324 1325
		return 0;
	case Opt_largeio:
1326
		parsing_mp->m_features |= XFS_FEAT_LARGE_IOSIZE;
1327 1328
		return 0;
	case Opt_nolargeio:
1329
		parsing_mp->m_features &= ~XFS_FEAT_LARGE_IOSIZE;
1330 1331
		return 0;
	case Opt_filestreams:
1332
		parsing_mp->m_features |= XFS_FEAT_FILESTREAMS;
1333 1334
		return 0;
	case Opt_noquota:
1335 1336
		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
		parsing_mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
1337 1338 1339 1340
		return 0;
	case Opt_quota:
	case Opt_uquota:
	case Opt_usrquota:
1341
		parsing_mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ENFD);
1342 1343 1344
		return 0;
	case Opt_qnoenforce:
	case Opt_uqnoenforce:
1345
		parsing_mp->m_qflags |= XFS_UQUOTA_ACCT;
1346
		parsing_mp->m_qflags &= ~XFS_UQUOTA_ENFD;
1347 1348 1349
		return 0;
	case Opt_pquota:
	case Opt_prjquota:
1350
		parsing_mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ENFD);
1351 1352
		return 0;
	case Opt_pqnoenforce:
1353
		parsing_mp->m_qflags |= XFS_PQUOTA_ACCT;
1354
		parsing_mp->m_qflags &= ~XFS_PQUOTA_ENFD;
1355 1356 1357
		return 0;
	case Opt_gquota:
	case Opt_grpquota:
1358
		parsing_mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ENFD);
1359 1360
		return 0;
	case Opt_gqnoenforce:
1361
		parsing_mp->m_qflags |= XFS_GQUOTA_ACCT;
1362
		parsing_mp->m_qflags &= ~XFS_GQUOTA_ENFD;
1363 1364
		return 0;
	case Opt_discard:
1365
		parsing_mp->m_features |= XFS_FEAT_DISCARD;
1366 1367
		return 0;
	case Opt_nodiscard:
1368
		parsing_mp->m_features &= ~XFS_FEAT_DISCARD;
1369 1370 1371
		return 0;
#ifdef CONFIG_FS_DAX
	case Opt_dax:
1372
		xfs_mount_set_dax_mode(parsing_mp, XFS_DAX_ALWAYS);
1373 1374
		return 0;
	case Opt_dax_enum:
1375
		xfs_mount_set_dax_mode(parsing_mp, result.uint_32);
1376 1377
		return 0;
#endif
1378 1379
	/* Following mount options will be removed in September 2025 */
	case Opt_ikeep:
1380 1381
		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, true);
		parsing_mp->m_features |= XFS_FEAT_IKEEP;
1382 1383
		return 0;
	case Opt_noikeep:
1384 1385
		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_IKEEP, false);
		parsing_mp->m_features &= ~XFS_FEAT_IKEEP;
1386 1387
		return 0;
	case Opt_attr2:
1388 1389
		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_ATTR2, true);
		parsing_mp->m_features |= XFS_FEAT_ATTR2;
1390 1391
		return 0;
	case Opt_noattr2:
1392 1393
		xfs_fs_warn_deprecated(fc, param, XFS_FEAT_NOATTR2, true);
		parsing_mp->m_features |= XFS_FEAT_NOATTR2;
1394
		return 0;
1395
	default:
1396
		xfs_warn(parsing_mp, "unknown mount option [%s].", param->key);
1397 1398
		return -EINVAL;
	}
1399 1400 1401 1402

	return 0;
}

1403
static int
1404
xfs_fs_validate_params(
1405 1406
	struct xfs_mount	*mp)
{
1407
	/* No recovery flag requires a read-only mount */
1408
	if (xfs_has_norecovery(mp) && !xfs_is_readonly(mp)) {
1409
		xfs_warn(mp, "no-recovery mounts must be read-only.");
1410
		return -EINVAL;
1411 1412 1413
	}

	/*
1414 1415
	 * We have not read the superblock at this point, so only the attr2
	 * mount option can set the attr2 feature by this stage.
1416
	 */
1417
	if (xfs_has_attr2(mp) && xfs_has_noattr2(mp)) {
1418
		xfs_warn(mp, "attr2 and noattr2 cannot both be specified.");
1419
		return -EINVAL;
1420 1421
	}

1422

1423
	if (xfs_has_noalign(mp) && (mp->m_dalign || mp->m_swidth)) {
1424
		xfs_warn(mp,
1425 1426
	"sunit and swidth options incompatible with the noalign option");
		return -EINVAL;
1427 1428
	}

1429 1430
	if (!IS_ENABLED(CONFIG_XFS_QUOTA) && mp->m_qflags != 0) {
		xfs_warn(mp, "quota support not available in this kernel.");
1431
		return -EINVAL;
1432 1433
	}

1434 1435 1436 1437 1438
	if ((mp->m_dalign && !mp->m_swidth) ||
	    (!mp->m_dalign && mp->m_swidth)) {
		xfs_warn(mp, "sunit and swidth must be specified together");
		return -EINVAL;
	}
1439

1440 1441 1442 1443 1444 1445
	if (mp->m_dalign && (mp->m_swidth % mp->m_dalign != 0)) {
		xfs_warn(mp,
	"stripe width (%d) must be a multiple of the stripe unit (%d)",
			mp->m_swidth, mp->m_dalign);
		return -EINVAL;
	}
1446

1447 1448 1449 1450 1451 1452 1453 1454
	if (mp->m_logbufs != -1 &&
	    mp->m_logbufs != 0 &&
	    (mp->m_logbufs < XLOG_MIN_ICLOGS ||
	     mp->m_logbufs > XLOG_MAX_ICLOGS)) {
		xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
			mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
		return -EINVAL;
	}
1455

1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
	if (mp->m_logbsize != -1 &&
	    mp->m_logbsize !=  0 &&
	    (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
	     mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
	     !is_power_of_2(mp->m_logbsize))) {
		xfs_warn(mp,
			"invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
			mp->m_logbsize);
		return -EINVAL;
	}
1466

1467
	if (xfs_has_allocsize(mp) &&
1468 1469 1470 1471 1472 1473
	    (mp->m_allocsize_log > XFS_MAX_IO_LOG ||
	     mp->m_allocsize_log < XFS_MIN_IO_LOG)) {
		xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
			mp->m_allocsize_log, XFS_MIN_IO_LOG, XFS_MAX_IO_LOG);
		return -EINVAL;
	}
1474

1475
	return 0;
1476
}
1477

1478
static int
1479
xfs_fs_fill_super(
1480
	struct super_block	*sb,
1481
	struct fs_context	*fc)
1482
{
1483
	struct xfs_mount	*mp = sb->s_fs_info;
1484
	struct inode		*root;
1485
	int			flags = 0, error;
1486

1487
	mp->m_super = sb;
Linus Torvalds's avatar
Linus Torvalds committed
1488

1489
	error = xfs_fs_validate_params(mp);
1490
	if (error)
1491
		goto out_free_names;
Linus Torvalds's avatar
Linus Torvalds committed
1492 1493

	sb_min_blocksize(sb, BBSIZE);
1494
	sb->s_xattr = xfs_xattr_handlers;
1495
	sb->s_export_op = &xfs_export_operations;
1496
#ifdef CONFIG_XFS_QUOTA
1497
	sb->s_qcop = &xfs_quotactl_operations;
Jan Kara's avatar
Jan Kara committed
1498
	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1499
#endif
1500
	sb->s_op = &xfs_super_operations;
Linus Torvalds's avatar
Linus Torvalds committed
1501

1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	/*
	 * Delay mount work if the debug hook is set. This is debug
	 * instrumention to coordinate simulation of xfs mount failures with
	 * VFS superblock operations
	 */
	if (xfs_globals.mount_delay) {
		xfs_notice(mp, "Delaying mount for %d seconds.",
			xfs_globals.mount_delay);
		msleep(xfs_globals.mount_delay * 1000);
	}

1513
	if (fc->sb_flags & SB_SILENT)
1514 1515
		flags |= XFS_MFSI_QUIET;

1516
	error = xfs_open_devices(mp);
1517
	if (error)
1518
		goto out_free_names;
1519

1520
	error = xfs_init_mount_workqueues(mp);
1521 1522
	if (error)
		goto out_close_devices;
1523

1524
	error = xfs_init_percpu_counters(mp);
1525 1526 1527
	if (error)
		goto out_destroy_workqueues;

1528 1529 1530 1531
	error = xfs_inodegc_init_percpu(mp);
	if (error)
		goto out_destroy_counters;

1532 1533 1534 1535 1536 1537 1538
	/*
	 * All percpu data structures requiring cleanup when a cpu goes offline
	 * must be allocated before adding this @mp to the cpu-dead handler's
	 * mount list.
	 */
	xfs_mount_list_add(mp);

1539 1540 1541
	/* Allocate stats memory before we do operations that might use it */
	mp->m_stats.xs_stats = alloc_percpu(struct xfsstats);
	if (!mp->m_stats.xs_stats) {
1542
		error = -ENOMEM;
1543
		goto out_destroy_inodegc;
1544 1545
	}

1546 1547
	error = xfs_readsb(mp, flags);
	if (error)
1548
		goto out_free_stats;
1549 1550

	error = xfs_finish_flags(mp);
1551
	if (error)
1552
		goto out_free_sb;
1553

1554
	error = xfs_setup_devices(mp);
1555
	if (error)
1556
		goto out_free_sb;
1557

1558
	/* V4 support is undergoing deprecation. */
1559
	if (!xfs_has_crc(mp)) {
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
#ifdef CONFIG_XFS_SUPPORT_V4
		xfs_warn_once(mp,
	"Deprecated V4 format (crc=0) will not be supported after September 2030.");
#else
		xfs_warn(mp,
	"Deprecated V4 format (crc=0) not supported by kernel.");
		error = -EINVAL;
		goto out_free_sb;
#endif
	}

1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
	/* ASCII case insensitivity is undergoing deprecation. */
	if (xfs_has_asciici(mp)) {
#ifdef CONFIG_XFS_SUPPORT_ASCII_CI
		xfs_warn_once(mp,
	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) will not be supported after September 2030.");
#else
		xfs_warn(mp,
	"Deprecated ASCII case-insensitivity feature (ascii-ci=1) not supported by kernel.");
		error = -EINVAL;
		goto out_free_sb;
#endif
	}

1584
	/* Filesystem claims it needs repair, so refuse the mount. */
1585
	if (xfs_has_needsrepair(mp)) {
1586 1587 1588 1589 1590
		xfs_warn(mp, "Filesystem needs repair.  Please run xfs_repair.");
		error = -EFSCORRUPTED;
		goto out_free_sb;
	}

1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
	/*
	 * Don't touch the filesystem if a user tool thinks it owns the primary
	 * superblock.  mkfs doesn't clear the flag from secondary supers, so
	 * we don't check them at all.
	 */
	if (mp->m_sb.sb_inprogress) {
		xfs_warn(mp, "Offline file system operation in progress!");
		error = -EFSCORRUPTED;
		goto out_free_sb;
	}

	/*
	 * Until this is fixed only page-sized or smaller data blocks work.
	 */
	if (mp->m_sb.sb_blocksize > PAGE_SIZE) {
		xfs_warn(mp,
		"File system with blocksize %d bytes. "
		"Only pagesize (%ld) or less will currently work.",
				mp->m_sb.sb_blocksize, PAGE_SIZE);
		error = -ENOSYS;
		goto out_free_sb;
	}

	/* Ensure this filesystem fits in the page cache limits */
	if (xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_dblocks) ||
	    xfs_sb_validate_fsb_count(&mp->m_sb, mp->m_sb.sb_rblocks)) {
		xfs_warn(mp,
		"file system too large to be mounted on this system.");
		error = -EFBIG;
		goto out_free_sb;
	}

1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
	/*
	 * XFS block mappings use 54 bits to store the logical block offset.
	 * This should suffice to handle the maximum file size that the VFS
	 * supports (currently 2^63 bytes on 64-bit and ULONG_MAX << PAGE_SHIFT
	 * bytes on 32-bit), but as XFS and VFS have gotten the s_maxbytes
	 * calculation wrong on 32-bit kernels in the past, we'll add a WARN_ON
	 * to check this assertion.
	 *
	 * Avoid integer overflow by comparing the maximum bmbt offset to the
	 * maximum pagecache offset in units of fs blocks.
	 */
1634
	if (!xfs_verify_fileoff(mp, XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE))) {
1635 1636 1637 1638 1639 1640 1641 1642
		xfs_warn(mp,
"MAX_LFS_FILESIZE block offset (%llu) exceeds extent map maximum (%llu)!",
			 XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE),
			 XFS_MAX_FILEOFF);
		error = -EINVAL;
		goto out_free_sb;
	}

1643 1644
	error = xfs_filestream_mount(mp);
	if (error)
1645
		goto out_free_sb;
1646

1647 1648 1649 1650
	/*
	 * we must configure the block size in the superblock before we run the
	 * full mount process as the mount process can lookup and cache inodes.
	 */
1651
	sb->s_magic = XFS_SUPER_MAGIC;
1652 1653
	sb->s_blocksize = mp->m_sb.sb_blocksize;
	sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1654
	sb->s_maxbytes = MAX_LFS_FILESIZE;
1655
	sb->s_max_links = XFS_MAXLINK;
Linus Torvalds's avatar
Linus Torvalds committed
1656
	sb->s_time_gran = 1;
1657
	if (xfs_has_bigtime(mp)) {
1658 1659 1660 1661 1662 1663
		sb->s_time_min = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MIN);
		sb->s_time_max = xfs_bigtime_to_unix(XFS_BIGTIME_TIME_MAX);
	} else {
		sb->s_time_min = XFS_LEGACY_TIME_MIN;
		sb->s_time_max = XFS_LEGACY_TIME_MAX;
	}
1664
	trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
1665 1666
	sb->s_iflags |= SB_I_CGROUPWB;

Linus Torvalds's avatar
Linus Torvalds committed
1667 1668
	set_posix_acl_flag(sb);

1669
	/* version 5 superblocks support inode version counters. */
1670
	if (xfs_has_crc(mp))
1671
		sb->s_flags |= SB_I_VERSION;
1672

1673
	if (xfs_has_dax_always(mp)) {
1674 1675
		error = xfs_setup_dax_always(mp);
		if (error)
1676
			goto out_filestream_unmount;
1677 1678
	}

1679 1680 1681 1682
	if (xfs_has_discard(mp) && !bdev_max_discard_sectors(sb->s_bdev)) {
		xfs_warn(mp,
	"mounting with \"discard\" option, but the device does not support discard");
		mp->m_features &= ~XFS_FEAT_DISCARD;
1683 1684
	}

1685
	if (xfs_has_reflink(mp)) {
1686 1687
		if (mp->m_sb.sb_rblocks) {
			xfs_alert(mp,
1688
	"reflink not compatible with realtime device!");
1689 1690 1691 1692 1693 1694 1695 1696
			error = -EINVAL;
			goto out_filestream_unmount;
		}

		if (xfs_globals.always_cow) {
			xfs_info(mp, "using DEBUG-only always_cow mode.");
			mp->m_always_cow = true;
		}
1697 1698
	}

1699
	if (xfs_has_rmapbt(mp) && mp->m_sb.sb_rblocks) {
1700
		xfs_alert(mp,
1701 1702 1703
	"reverse mapping btree not compatible with realtime device!");
		error = -EINVAL;
		goto out_filestream_unmount;
1704
	}
1705

1706
	error = xfs_mountfs(mp);
1707
	if (error)
1708
		goto out_filestream_unmount;
1709

1710
	root = igrab(VFS_I(mp->m_rootip));
1711
	if (!root) {
1712
		error = -ENOENT;
1713
		goto out_unmount;
Christoph Hellwig's avatar
Christoph Hellwig committed
1714
	}
1715
	sb->s_root = d_make_root(root);
1716
	if (!sb->s_root) {
1717
		error = -ENOMEM;
1718
		goto out_unmount;
Linus Torvalds's avatar
Linus Torvalds committed
1719
	}
1720

Linus Torvalds's avatar
Linus Torvalds committed
1721
	return 0;
Dave Chinner's avatar
Dave Chinner committed
1722

1723
 out_filestream_unmount:
1724
	xfs_filestream_unmount(mp);
1725 1726
 out_free_sb:
	xfs_freesb(mp);
1727 1728
 out_free_stats:
	free_percpu(mp->m_stats.xs_stats);
1729
 out_destroy_inodegc:
1730
	xfs_mount_list_del(mp);
1731
	xfs_inodegc_free_percpu(mp);
1732
 out_destroy_counters:
1733
	xfs_destroy_percpu_counters(mp);
1734
 out_destroy_workqueues:
1735
	xfs_destroy_mount_workqueues(mp);
1736
 out_close_devices:
1737
	xfs_close_devices(mp);
1738
 out_free_names:
1739
	sb->s_fs_info = NULL;
1740
	xfs_mount_free(mp);
1741
	return error;
1742

1743
 out_unmount:
1744
	xfs_filestream_unmount(mp);
1745
	xfs_unmountfs(mp);
1746
	goto out_free_sb;
Linus Torvalds's avatar
Linus Torvalds committed
1747 1748
}

1749
static int
1750
xfs_fs_get_tree(
1751 1752
	struct fs_context	*fc)
{
1753
	return get_tree_bdev(fc, xfs_fs_fill_super);
1754 1755
}

1756 1757 1758 1759 1760 1761 1762
static int
xfs_remount_rw(
	struct xfs_mount	*mp)
{
	struct xfs_sb		*sbp = &mp->m_sb;
	int error;

1763
	if (xfs_has_norecovery(mp)) {
1764 1765 1766 1767 1768
		xfs_warn(mp,
			"ro->rw transition prohibited on norecovery mount");
		return -EINVAL;
	}

1769
	if (xfs_sb_is_v5(sbp) &&
1770 1771 1772 1773 1774 1775 1776 1777
	    xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
		xfs_warn(mp,
	"ro->rw transition prohibited on unknown (0x%x) ro-compat filesystem",
			(sbp->sb_features_ro_compat &
				XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
		return -EINVAL;
	}

1778
	clear_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798

	/*
	 * If this is the first remount to writeable state we might have some
	 * superblock changes to update.
	 */
	if (mp->m_update_sb) {
		error = xfs_sync_sb(mp, false);
		if (error) {
			xfs_warn(mp, "failed to write sb changes");
			return error;
		}
		mp->m_update_sb = false;
	}

	/*
	 * Fill out the reserve pool if it is empty. Use the stashed value if
	 * it is non-zero, otherwise go with the default.
	 */
	xfs_restore_resvblks(mp);
	xfs_log_work_queue(mp);
1799
	xfs_blockgc_start(mp);
1800 1801 1802 1803 1804 1805

	/* Create the per-AG metadata reservation pool .*/
	error = xfs_fs_reserve_ag_blocks(mp);
	if (error && error != -ENOSPC)
		return error;

1806 1807 1808
	/* Re-enable the background inode inactivation worker. */
	xfs_inodegc_start(mp);

1809 1810 1811 1812 1813 1814 1815
	return 0;
}

static int
xfs_remount_ro(
	struct xfs_mount	*mp)
{
1816 1817 1818 1819
	struct xfs_icwalk	icw = {
		.icw_flags	= XFS_ICWALK_FLAG_SYNC,
	};
	int			error;
1820

1821 1822 1823 1824 1825
	/* Flush all the dirty data to disk. */
	error = sync_filesystem(mp->m_super);
	if (error)
		return error;

1826 1827 1828 1829
	/*
	 * Cancel background eofb scanning so it cannot race with the final
	 * log force+buftarg wait and deadlock the remount.
	 */
1830
	xfs_blockgc_stop(mp);
1831

1832 1833 1834 1835 1836 1837 1838
	/*
	 * Clear out all remaining COW staging extents and speculative post-EOF
	 * preallocations so that we don't leave inodes requiring inactivation
	 * cleanups during reclaim on a read-only mount.  We must process every
	 * cached inode, so this requires a synchronous cache scan.
	 */
	error = xfs_blockgc_free_space(mp, &icw);
1839 1840 1841 1842 1843
	if (error) {
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
		return error;
	}

1844 1845 1846 1847 1848 1849 1850 1851 1852
	/*
	 * Stop the inodegc background worker.  xfs_fs_reconfigure already
	 * flushed all pending inodegc work when it sync'd the filesystem.
	 * The VFS holds s_umount, so we know that inodes cannot enter
	 * xfs_fs_destroy_inode during a remount operation.  In readonly mode
	 * we send inodes straight to reclaim, so no inodes will be queued.
	 */
	xfs_inodegc_stop(mp);

1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
	/* Free the per-AG metadata reservation pool. */
	error = xfs_fs_unreserve_ag_blocks(mp);
	if (error) {
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
		return error;
	}

	/*
	 * Before we sync the metadata, we need to free up the reserve block
	 * pool so that the used block count in the superblock on disk is
	 * correct at the end of the remount. Stash the current* reserve pool
	 * size so that if we get remounted rw, we can return it to the same
	 * size.
	 */
	xfs_save_resvblks(mp);

1869
	xfs_log_clean(mp);
1870
	set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887

	return 0;
}

/*
 * Logically we would return an error here to prevent users from believing
 * they might have changed mount options using remount which can't be changed.
 *
 * But unfortunately mount(8) adds all options from mtab and fstab to the mount
 * arguments in some cases so we can't blindly reject options, but have to
 * check for each specified option if it actually differs from the currently
 * set option and only reject it if that's the case.
 *
 * Until that is implemented we return success for every remount request, and
 * silently ignore all options that we can't actually change.
 */
static int
1888
xfs_fs_reconfigure(
1889 1890 1891 1892 1893 1894 1895
	struct fs_context *fc)
{
	struct xfs_mount	*mp = XFS_M(fc->root->d_sb);
	struct xfs_mount        *new_mp = fc->s_fs_info;
	int			flags = fc->sb_flags;
	int			error;

1896
	/* version 5 superblocks always support version counters. */
1897
	if (xfs_has_crc(mp))
1898 1899
		fc->sb_flags |= SB_I_VERSION;

1900
	error = xfs_fs_validate_params(new_mp);
1901 1902 1903 1904
	if (error)
		return error;

	/* inode32 -> inode64 */
1905 1906
	if (xfs_has_small_inums(mp) && !xfs_has_small_inums(new_mp)) {
		mp->m_features &= ~XFS_FEAT_SMALL_INUMS;
1907
		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
1908 1909 1910
	}

	/* inode64 -> inode32 */
1911 1912
	if (!xfs_has_small_inums(mp) && xfs_has_small_inums(new_mp)) {
		mp->m_features |= XFS_FEAT_SMALL_INUMS;
1913
		mp->m_maxagi = xfs_set_inode_alloc(mp, mp->m_sb.sb_agcount);
1914 1915 1916
	}

	/* ro -> rw */
1917
	if (xfs_is_readonly(mp) && !(flags & SB_RDONLY)) {
1918 1919 1920 1921 1922 1923
		error = xfs_remount_rw(mp);
		if (error)
			return error;
	}

	/* rw -> ro */
1924
	if (!xfs_is_readonly(mp) && (flags & SB_RDONLY)) {
1925 1926 1927 1928 1929 1930 1931 1932
		error = xfs_remount_ro(mp);
		if (error)
			return error;
	}

	return 0;
}

1933 1934
static void
xfs_fs_free(
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
	struct fs_context	*fc)
{
	struct xfs_mount	*mp = fc->s_fs_info;

	/*
	 * mp is stored in the fs_context when it is initialized.
	 * mp is transferred to the superblock on a successful mount,
	 * but if an error occurs before the transfer we have to free
	 * it here.
	 */
	if (mp)
		xfs_mount_free(mp);
}

static const struct fs_context_operations xfs_context_ops = {
1950 1951 1952 1953
	.parse_param = xfs_fs_parse_param,
	.get_tree    = xfs_fs_get_tree,
	.reconfigure = xfs_fs_reconfigure,
	.free        = xfs_fs_free,
1954 1955 1956 1957 1958 1959 1960
};

static int xfs_init_fs_context(
	struct fs_context	*fc)
{
	struct xfs_mount	*mp;

1961
	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
1962 1963 1964
	if (!mp)
		return -ENOMEM;

1965 1966 1967 1968
	spin_lock_init(&mp->m_sb_lock);
	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
	spin_lock_init(&mp->m_perag_lock);
	mutex_init(&mp->m_growlock);
1969
	INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
	INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
	mp->m_kobj.kobject.kset = xfs_kset;
	/*
	 * We don't create the finobt per-ag space reservation until after log
	 * recovery, so we must set this to true so that an ifree transaction
	 * started during log recovery will not depend on space reservations
	 * for finobt expansion.
	 */
	mp->m_finobt_nores = true;

1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
	/*
	 * These can be overridden by the mount option parsing.
	 */
	mp->m_logbufs = -1;
	mp->m_logbsize = -1;
	mp->m_allocsize_log = 16; /* 64k */

	/*
	 * Copy binary VFS mount flags we are interested in.
	 */
	if (fc->sb_flags & SB_RDONLY)
1991
		set_bit(XFS_OPSTATE_READONLY, &mp->m_opstate);
1992
	if (fc->sb_flags & SB_DIRSYNC)
1993
		mp->m_features |= XFS_FEAT_DIRSYNC;
1994
	if (fc->sb_flags & SB_SYNCHRONOUS)
1995
		mp->m_features |= XFS_FEAT_WSYNC;
1996 1997 1998 1999 2000 2001 2002

	fc->s_fs_info = mp;
	fc->ops = &xfs_context_ops;

	return 0;
}

Andrew Morton's avatar
Andrew Morton committed
2003
static struct file_system_type xfs_fs_type = {
Linus Torvalds's avatar
Linus Torvalds committed
2004 2005
	.owner			= THIS_MODULE,
	.name			= "xfs",
2006
	.init_fs_context	= xfs_init_fs_context,
2007
	.parameters		= xfs_fs_parameters,
Linus Torvalds's avatar
Linus Torvalds committed
2008
	.kill_sb		= kill_block_super,
2009
	.fs_flags		= FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
Linus Torvalds's avatar
Linus Torvalds committed
2010
};
2011
MODULE_ALIAS_FS("xfs");
Linus Torvalds's avatar
Linus Torvalds committed
2012

2013
STATIC int __init
2014
xfs_init_caches(void)
2015
{
2016 2017
	int		error;

2018 2019 2020 2021 2022 2023 2024 2025
	xfs_buf_cache = kmem_cache_create("xfs_buf", sizeof(struct xfs_buf), 0,
					 SLAB_HWCACHE_ALIGN |
					 SLAB_RECLAIM_ACCOUNT |
					 SLAB_MEM_SPREAD,
					 NULL);
	if (!xfs_buf_cache)
		goto out;

2026
	xfs_log_ticket_cache = kmem_cache_create("xfs_log_ticket",
2027 2028
						sizeof(struct xlog_ticket),
						0, 0, NULL);
2029
	if (!xfs_log_ticket_cache)
2030
		goto out_destroy_buf_cache;
2031

2032 2033
	error = xfs_btree_init_cur_caches();
	if (error)
2034
		goto out_destroy_log_ticket_cache;
2035

2036 2037 2038 2039
	error = xfs_defer_init_item_caches();
	if (error)
		goto out_destroy_btree_cur_cache;

2040
	xfs_da_state_cache = kmem_cache_create("xfs_da_state",
2041 2042
					      sizeof(struct xfs_da_state),
					      0, 0, NULL);
2043
	if (!xfs_da_state_cache)
2044
		goto out_destroy_defer_item_cache;
2045

2046
	xfs_ifork_cache = kmem_cache_create("xfs_ifork",
2047 2048
					   sizeof(struct xfs_ifork),
					   0, 0, NULL);
2049 2050
	if (!xfs_ifork_cache)
		goto out_destroy_da_state_cache;
2051

2052
	xfs_trans_cache = kmem_cache_create("xfs_trans",
2053 2054
					   sizeof(struct xfs_trans),
					   0, 0, NULL);
2055 2056
	if (!xfs_trans_cache)
		goto out_destroy_ifork_cache;
2057

2058

2059
	/*
2060
	 * The size of the cache-allocated buf log item is the maximum
2061 2062 2063
	 * size possible under XFS.  This wastes a little bit of memory,
	 * but it is much faster.
	 */
2064
	xfs_buf_item_cache = kmem_cache_create("xfs_buf_item",
2065 2066
					      sizeof(struct xfs_buf_log_item),
					      0, 0, NULL);
2067 2068
	if (!xfs_buf_item_cache)
		goto out_destroy_trans_cache;
2069

2070
	xfs_efd_cache = kmem_cache_create("xfs_efd_item",
2071 2072
			xfs_efd_log_item_sizeof(XFS_EFD_MAX_FAST_EXTENTS),
			0, 0, NULL);
2073 2074
	if (!xfs_efd_cache)
		goto out_destroy_buf_item_cache;
2075

2076
	xfs_efi_cache = kmem_cache_create("xfs_efi_item",
2077 2078
			xfs_efi_log_item_sizeof(XFS_EFI_MAX_FAST_EXTENTS),
			0, 0, NULL);
2079 2080
	if (!xfs_efi_cache)
		goto out_destroy_efd_cache;
2081

2082
	xfs_inode_cache = kmem_cache_create("xfs_inode",
2083 2084 2085 2086 2087
					   sizeof(struct xfs_inode), 0,
					   (SLAB_HWCACHE_ALIGN |
					    SLAB_RECLAIM_ACCOUNT |
					    SLAB_MEM_SPREAD | SLAB_ACCOUNT),
					   xfs_fs_inode_init_once);
2088 2089
	if (!xfs_inode_cache)
		goto out_destroy_efi_cache;
2090

2091
	xfs_ili_cache = kmem_cache_create("xfs_ili",
2092
					 sizeof(struct xfs_inode_log_item), 0,
2093 2094
					 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
					 NULL);
2095 2096
	if (!xfs_ili_cache)
		goto out_destroy_inode_cache;
2097

2098
	xfs_icreate_cache = kmem_cache_create("xfs_icr",
2099 2100
					     sizeof(struct xfs_icreate_item),
					     0, 0, NULL);
2101 2102
	if (!xfs_icreate_cache)
		goto out_destroy_ili_cache;
2103

2104
	xfs_rud_cache = kmem_cache_create("xfs_rud_item",
2105 2106
					 sizeof(struct xfs_rud_log_item),
					 0, 0, NULL);
2107 2108
	if (!xfs_rud_cache)
		goto out_destroy_icreate_cache;
2109

2110
	xfs_rui_cache = kmem_cache_create("xfs_rui_item",
2111
			xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
2112
			0, 0, NULL);
2113 2114
	if (!xfs_rui_cache)
		goto out_destroy_rud_cache;
2115

2116
	xfs_cud_cache = kmem_cache_create("xfs_cud_item",
2117 2118
					 sizeof(struct xfs_cud_log_item),
					 0, 0, NULL);
2119 2120
	if (!xfs_cud_cache)
		goto out_destroy_rui_cache;
2121

2122
	xfs_cui_cache = kmem_cache_create("xfs_cui_item",
2123
			xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
2124
			0, 0, NULL);
2125 2126
	if (!xfs_cui_cache)
		goto out_destroy_cud_cache;
2127

2128
	xfs_bud_cache = kmem_cache_create("xfs_bud_item",
2129 2130
					 sizeof(struct xfs_bud_log_item),
					 0, 0, NULL);
2131 2132
	if (!xfs_bud_cache)
		goto out_destroy_cui_cache;
2133

2134
	xfs_bui_cache = kmem_cache_create("xfs_bui_item",
2135
			xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
2136
			0, 0, NULL);
2137 2138
	if (!xfs_bui_cache)
		goto out_destroy_bud_cache;
2139

2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151
	xfs_attrd_cache = kmem_cache_create("xfs_attrd_item",
					    sizeof(struct xfs_attrd_log_item),
					    0, 0, NULL);
	if (!xfs_attrd_cache)
		goto out_destroy_bui_cache;

	xfs_attri_cache = kmem_cache_create("xfs_attri_item",
					    sizeof(struct xfs_attri_log_item),
					    0, 0, NULL);
	if (!xfs_attri_cache)
		goto out_destroy_attrd_cache;

2152 2153 2154 2155 2156 2157
	xfs_iunlink_cache = kmem_cache_create("xfs_iul_item",
					     sizeof(struct xfs_iunlink_item),
					     0, 0, NULL);
	if (!xfs_iunlink_cache)
		goto out_destroy_attri_cache;

2158 2159
	return 0;

2160 2161
 out_destroy_attri_cache:
	kmem_cache_destroy(xfs_attri_cache);
2162 2163 2164 2165
 out_destroy_attrd_cache:
	kmem_cache_destroy(xfs_attrd_cache);
 out_destroy_bui_cache:
	kmem_cache_destroy(xfs_bui_cache);
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
 out_destroy_bud_cache:
	kmem_cache_destroy(xfs_bud_cache);
 out_destroy_cui_cache:
	kmem_cache_destroy(xfs_cui_cache);
 out_destroy_cud_cache:
	kmem_cache_destroy(xfs_cud_cache);
 out_destroy_rui_cache:
	kmem_cache_destroy(xfs_rui_cache);
 out_destroy_rud_cache:
	kmem_cache_destroy(xfs_rud_cache);
 out_destroy_icreate_cache:
	kmem_cache_destroy(xfs_icreate_cache);
 out_destroy_ili_cache:
	kmem_cache_destroy(xfs_ili_cache);
 out_destroy_inode_cache:
	kmem_cache_destroy(xfs_inode_cache);
 out_destroy_efi_cache:
	kmem_cache_destroy(xfs_efi_cache);
 out_destroy_efd_cache:
	kmem_cache_destroy(xfs_efd_cache);
 out_destroy_buf_item_cache:
	kmem_cache_destroy(xfs_buf_item_cache);
 out_destroy_trans_cache:
	kmem_cache_destroy(xfs_trans_cache);
 out_destroy_ifork_cache:
	kmem_cache_destroy(xfs_ifork_cache);
 out_destroy_da_state_cache:
	kmem_cache_destroy(xfs_da_state_cache);
2194 2195
 out_destroy_defer_item_cache:
	xfs_defer_destroy_item_caches();
2196
 out_destroy_btree_cur_cache:
2197
	xfs_btree_destroy_cur_caches();
2198 2199
 out_destroy_log_ticket_cache:
	kmem_cache_destroy(xfs_log_ticket_cache);
2200 2201
 out_destroy_buf_cache:
	kmem_cache_destroy(xfs_buf_cache);
2202 2203 2204 2205 2206
 out:
	return -ENOMEM;
}

STATIC void
2207
xfs_destroy_caches(void)
2208
{
2209 2210 2211 2212 2213
	/*
	 * Make sure all delayed rcu free are flushed before we
	 * destroy caches.
	 */
	rcu_barrier();
2214
	kmem_cache_destroy(xfs_iunlink_cache);
2215 2216
	kmem_cache_destroy(xfs_attri_cache);
	kmem_cache_destroy(xfs_attrd_cache);
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
	kmem_cache_destroy(xfs_bui_cache);
	kmem_cache_destroy(xfs_bud_cache);
	kmem_cache_destroy(xfs_cui_cache);
	kmem_cache_destroy(xfs_cud_cache);
	kmem_cache_destroy(xfs_rui_cache);
	kmem_cache_destroy(xfs_rud_cache);
	kmem_cache_destroy(xfs_icreate_cache);
	kmem_cache_destroy(xfs_ili_cache);
	kmem_cache_destroy(xfs_inode_cache);
	kmem_cache_destroy(xfs_efi_cache);
	kmem_cache_destroy(xfs_efd_cache);
	kmem_cache_destroy(xfs_buf_item_cache);
	kmem_cache_destroy(xfs_trans_cache);
	kmem_cache_destroy(xfs_ifork_cache);
	kmem_cache_destroy(xfs_da_state_cache);
2232
	xfs_defer_destroy_item_caches();
2233
	xfs_btree_destroy_cur_caches();
2234
	kmem_cache_destroy(xfs_log_ticket_cache);
2235
	kmem_cache_destroy(xfs_buf_cache);
2236
}
Linus Torvalds's avatar
Linus Torvalds committed
2237

2238 2239 2240
STATIC int __init
xfs_init_workqueues(void)
{
2241 2242 2243 2244 2245 2246
	/*
	 * The allocation workqueue can be used in memory reclaim situations
	 * (writepage path), and parallelism is only limited by the number of
	 * AGs in all the filesystems mounted. Hence use the default large
	 * max_active value for this workqueue.
	 */
2247
	xfs_alloc_wq = alloc_workqueue("xfsalloc",
2248
			XFS_WQFLAGS(WQ_MEM_RECLAIM | WQ_FREEZABLE), 0);
2249
	if (!xfs_alloc_wq)
2250
		return -ENOMEM;
2251

2252 2253
	xfs_discard_wq = alloc_workqueue("xfsdiscard", XFS_WQFLAGS(WQ_UNBOUND),
			0);
2254 2255 2256
	if (!xfs_discard_wq)
		goto out_free_alloc_wq;

2257
	return 0;
2258 2259 2260
out_free_alloc_wq:
	destroy_workqueue(xfs_alloc_wq);
	return -ENOMEM;
2261 2262
}

2263
STATIC void
2264 2265
xfs_destroy_workqueues(void)
{
2266
	destroy_workqueue(xfs_discard_wq);
2267
	destroy_workqueue(xfs_alloc_wq);
2268 2269
}

2270 2271 2272 2273 2274
#ifdef CONFIG_HOTPLUG_CPU
static int
xfs_cpu_dead(
	unsigned int		cpu)
{
2275 2276 2277 2278 2279
	struct xfs_mount	*mp, *n;

	spin_lock(&xfs_mount_list_lock);
	list_for_each_entry_safe(mp, n, &xfs_mount_list, m_mount_list) {
		spin_unlock(&xfs_mount_list_lock);
2280
		xfs_inodegc_cpu_dead(mp, cpu);
2281
		xlog_cil_pcp_dead(mp->m_log, cpu);
2282 2283 2284
		spin_lock(&xfs_mount_list_lock);
	}
	spin_unlock(&xfs_mount_list_lock);
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
	return 0;
}

static int __init
xfs_cpu_hotplug_init(void)
{
	int	error;

	error = cpuhp_setup_state_nocalls(CPUHP_XFS_DEAD, "xfs:dead", NULL,
			xfs_cpu_dead);
	if (error < 0)
		xfs_alert(NULL,
"Failed to initialise CPU hotplug, error %d. XFS is non-functional.",
			error);
	return error;
}

static void
xfs_cpu_hotplug_destroy(void)
{
	cpuhp_remove_state_nocalls(CPUHP_XFS_DEAD);
}

#else /* !CONFIG_HOTPLUG_CPU */
static inline int xfs_cpu_hotplug_init(void) { return 0; }
static inline void xfs_cpu_hotplug_destroy(void) {}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
2313
STATIC int __init
2314
init_xfs_fs(void)
Linus Torvalds's avatar
Linus Torvalds committed
2315 2316 2317
{
	int			error;

2318 2319
	xfs_check_ondisk_structs();

2320 2321 2322 2323
	error = xfs_dahash_test();
	if (error)
		return error;

2324 2325
	printk(KERN_INFO XFS_VERSION_STRING " with "
			 XFS_BUILD_OPTIONS " enabled\n");
Linus Torvalds's avatar
Linus Torvalds committed
2326

2327
	xfs_dir_startup();
Linus Torvalds's avatar
Linus Torvalds committed
2328

2329
	error = xfs_cpu_hotplug_init();
2330 2331 2332
	if (error)
		goto out;

2333
	error = xfs_init_caches();
2334 2335 2336
	if (error)
		goto out_destroy_hp;

2337
	error = xfs_init_workqueues();
2338
	if (error)
2339
		goto out_destroy_caches;
2340

2341 2342 2343 2344
	error = xfs_mru_cache_init();
	if (error)
		goto out_destroy_wq;

2345 2346
	error = xfs_init_procfs();
	if (error)
2347
		goto out_mru_cache_uninit;
2348 2349 2350 2351

	error = xfs_sysctl_register();
	if (error)
		goto out_cleanup_procfs;
Linus Torvalds's avatar
Linus Torvalds committed
2352

Brian Foster's avatar
Brian Foster committed
2353 2354 2355
	xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
	if (!xfs_kset) {
		error = -ENOMEM;
2356
		goto out_sysctl_unregister;
Brian Foster's avatar
Brian Foster committed
2357 2358
	}

2359 2360 2361 2362 2363 2364 2365 2366 2367
	xfsstats.xs_kobj.kobject.kset = xfs_kset;

	xfsstats.xs_stats = alloc_percpu(struct xfsstats);
	if (!xfsstats.xs_stats) {
		error = -ENOMEM;
		goto out_kset_unregister;
	}

	error = xfs_sysfs_init(&xfsstats.xs_kobj, &xfs_stats_ktype, NULL,
2368 2369
			       "stats");
	if (error)
2370
		goto out_free_stats;
2371

2372 2373 2374
#ifdef DEBUG
	xfs_dbg_kobj.kobject.kset = xfs_kset;
	error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2375
	if (error)
2376
		goto out_remove_stats_kobj;
2377 2378 2379 2380
#endif

	error = xfs_qm_init();
	if (error)
2381
		goto out_remove_dbg_kobj;
Linus Torvalds's avatar
Linus Torvalds committed
2382 2383 2384

	error = register_filesystem(&xfs_fs_type);
	if (error)
2385
		goto out_qm_exit;
Linus Torvalds's avatar
Linus Torvalds committed
2386 2387
	return 0;

2388 2389
 out_qm_exit:
	xfs_qm_exit();
2390
 out_remove_dbg_kobj:
2391 2392
#ifdef DEBUG
	xfs_sysfs_del(&xfs_dbg_kobj);
2393
 out_remove_stats_kobj:
2394
#endif
2395 2396 2397
	xfs_sysfs_del(&xfsstats.xs_kobj);
 out_free_stats:
	free_percpu(xfsstats.xs_stats);
2398
 out_kset_unregister:
Brian Foster's avatar
Brian Foster committed
2399
	kset_unregister(xfs_kset);
2400 2401 2402 2403 2404 2405
 out_sysctl_unregister:
	xfs_sysctl_unregister();
 out_cleanup_procfs:
	xfs_cleanup_procfs();
 out_mru_cache_uninit:
	xfs_mru_cache_uninit();
2406 2407
 out_destroy_wq:
	xfs_destroy_workqueues();
2408 2409
 out_destroy_caches:
	xfs_destroy_caches();
2410 2411
 out_destroy_hp:
	xfs_cpu_hotplug_destroy();
2412
 out:
Linus Torvalds's avatar
Linus Torvalds committed
2413 2414 2415 2416
	return error;
}

STATIC void __exit
2417
exit_xfs_fs(void)
Linus Torvalds's avatar
Linus Torvalds committed
2418
{
2419
	xfs_qm_exit();
Linus Torvalds's avatar
Linus Torvalds committed
2420
	unregister_filesystem(&xfs_fs_type);
2421 2422 2423
#ifdef DEBUG
	xfs_sysfs_del(&xfs_dbg_kobj);
#endif
2424 2425
	xfs_sysfs_del(&xfsstats.xs_kobj);
	free_percpu(xfsstats.xs_stats);
Brian Foster's avatar
Brian Foster committed
2426
	kset_unregister(xfs_kset);
2427 2428 2429
	xfs_sysctl_unregister();
	xfs_cleanup_procfs();
	xfs_mru_cache_uninit();
2430
	xfs_destroy_workqueues();
2431
	xfs_destroy_caches();
2432
	xfs_uuid_table_free();
2433
	xfs_cpu_hotplug_destroy();
Linus Torvalds's avatar
Linus Torvalds committed
2434 2435 2436 2437 2438 2439 2440 2441
}

module_init(init_xfs_fs);
module_exit(exit_xfs_fs);

MODULE_AUTHOR("Silicon Graphics, Inc.");
MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
MODULE_LICENSE("GPL");