xfs_super.c 54.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
3
 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4
 * All Rights Reserved.
L
Linus Torvalds 已提交
5
 */
6

L
Linus Torvalds 已提交
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"
L
Linus Torvalds 已提交
12 13 14
#include "xfs_sb.h"
#include "xfs_mount.h"
#include "xfs_inode.h"
15
#include "xfs_btree.h"
L
Linus Torvalds 已提交
16
#include "xfs_bmap.h"
17
#include "xfs_alloc.h"
18
#include "xfs_fsops.h"
19
#include "xfs_trans.h"
L
Linus Torvalds 已提交
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"
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"
L
Linus Torvalds 已提交
38

39
#include <linux/magic.h>
40 41
#include <linux/fs_context.h>
#include <linux/fs_parser.h>
L
Linus Torvalds 已提交
42

43
static const struct super_operations xfs_super_operations;
44

45
static struct kset *xfs_kset;		/* top-level xfs sysfs dir */
46 47 48
#ifdef DEBUG
static struct xfs_kobj xfs_dbg_kobj;	/* global debug sysfs attrs */
#endif
L
Linus Torvalds 已提交
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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:
		mp->m_flags &= ~(XFS_MOUNT_DAX_ALWAYS | XFS_MOUNT_DAX_NEVER);
		break;
	case XFS_DAX_ALWAYS:
		mp->m_flags |= XFS_MOUNT_DAX_ALWAYS;
		mp->m_flags &= ~XFS_MOUNT_DAX_NEVER;
		break;
	case XFS_DAX_NEVER:
		mp->m_flags |= XFS_MOUNT_DAX_NEVER;
		mp->m_flags &= ~XFS_MOUNT_DAX_ALWAYS;
		break;
	}
}

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

83 84 85 86
/*
 * Table driven mount option parser.
 */
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 91 92 93
	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,
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	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),
139
	fsparam_enum("dax",		Opt_dax_enum, dax_param_enums),
140
	{}
141 142
};

143
struct proc_xfs_info {
144 145
	uint64_t	flag;
	char		*str;
146 147
};

148 149 150 151
static int
xfs_fs_show_options(
	struct seq_file		*m,
	struct dentry		*root)
152 153 154
{
	static struct proc_xfs_info xfs_info_set[] = {
		/* the few simple ones we can get from the mount struct */
155 156 157 158 159 160 161 162 163 164
		{ XFS_MOUNT_IKEEP,		",ikeep" },
		{ XFS_MOUNT_WSYNC,		",wsync" },
		{ XFS_MOUNT_NOALIGN,		",noalign" },
		{ XFS_MOUNT_SWALLOC,		",swalloc" },
		{ XFS_MOUNT_NOUUID,		",nouuid" },
		{ XFS_MOUNT_NORECOVERY,		",norecovery" },
		{ XFS_MOUNT_ATTR2,		",attr2" },
		{ XFS_MOUNT_FILESTREAMS,	",filestreams" },
		{ XFS_MOUNT_GRPID,		",grpid" },
		{ XFS_MOUNT_DISCARD,		",discard" },
165
		{ XFS_MOUNT_LARGEIO,		",largeio" },
166 167
		{ XFS_MOUNT_DAX_ALWAYS,		",dax=always" },
		{ XFS_MOUNT_DAX_NEVER,		",dax=never" },
168 169
		{ 0, NULL }
	};
170
	struct xfs_mount	*mp = XFS_M(root->d_sb);
171 172 173 174 175 176
	struct proc_xfs_info	*xfs_infop;

	for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
		if (mp->m_flags & xfs_infop->flag)
			seq_puts(m, xfs_infop->str);
	}
177 178 179

	seq_printf(m, ",inode%d",
		(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 198
				(int)XFS_FSB_TO_BB(mp, mp->m_dalign));
	if (mp->m_swidth > 0)
199
		seq_printf(m, ",swidth=%d",
200 201 202
				(int)XFS_FSB_TO_BB(mp, mp->m_swidth));

	if (mp->m_qflags & (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD))
203
		seq_puts(m, ",usrquota");
204
	else if (mp->m_qflags & XFS_UQUOTA_ACCT)
205
		seq_puts(m, ",uqnoenforce");
206

207
	if (mp->m_qflags & XFS_PQUOTA_ACCT) {
208
		if (mp->m_qflags & XFS_PQUOTA_ENFD)
209
			seq_puts(m, ",prjquota");
210
		else
211
			seq_puts(m, ",pqnoenforce");
212 213
	}
	if (mp->m_qflags & XFS_GQUOTA_ACCT) {
214
		if (mp->m_qflags & XFS_GQUOTA_ENFD)
215
			seq_puts(m, ",grpquota");
216
		else
217
			seq_puts(m, ",gqnoenforce");
218
	}
219 220

	if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
221
		seq_puts(m, ",noquota");
222 223

	return 0;
224
}
225

226
/*
227 228 229 230 231 232 233 234 235 236 237 238 239
 * Set parameters for inode allocation heuristics, taking into account
 * filesystem size and inode32/inode64 mount options; i.e. specifically
 * whether or not XFS_MOUNT_SMALL_INUMS is set.
 *
 * Inode allocation patterns are altered only if inode32 is requested
 * (XFS_MOUNT_SMALL_INUMS), and the filesystem is sufficiently large.
 * If altered, XFS_MOUNT_32BITINODES is set as well.
 *
 * 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.
240
 */
241
xfs_agnumber_t
242 243 244
xfs_set_inode_alloc(
	struct xfs_mount *mp,
	xfs_agnumber_t	agcount)
245
{
246
	xfs_agnumber_t	index;
247
	xfs_agnumber_t	maxagi = 0;
248 249
	xfs_sb_t	*sbp = &mp->m_sb;
	xfs_agnumber_t	max_metadata;
250 251
	xfs_agino_t	agino;
	xfs_ino_t	ino;
252

253 254 255
	/*
	 * Calculate how much should be reserved for inodes to meet
	 * the max inode percentage.  Used only for inode32.
256
	 */
257
	if (M_IGEO(mp)->maxicount) {
258
		uint64_t	icount;
259 260 261 262 263 264 265

		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 {
266
		max_metadata = agcount;
267 268
	}

269
	/* Get the last possible inode in the filesystem */
270
	agino =	XFS_AGB_TO_AGINO(mp, sbp->sb_agblocks - 1);
271 272 273 274 275 276 277 278 279 280 281
	ino = XFS_AGINO_TO_INO(mp, agcount - 1, agino);

	/*
	 * If user asked for no more than 32-bit inodes, and the fs is
	 * sufficiently large, set XFS_MOUNT_32BITINODES if we must alter
	 * the allocator to accommodate the request.
	 */
	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) && ino > XFS_MAXINUMBER_32)
		mp->m_flags |= XFS_MOUNT_32BITINODES;
	else
		mp->m_flags &= ~XFS_MOUNT_32BITINODES;
282

283
	for (index = 0; index < agcount; index++) {
284
		struct xfs_perag	*pag;
285

286
		ino = XFS_AGINO_TO_INO(mp, index, agino);
287 288 289

		pag = xfs_perag_get(mp, index);

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		if (mp->m_flags & XFS_MOUNT_32BITINODES) {
			if (ino > XFS_MAXINUMBER_32) {
				pag->pagi_inodeok = 0;
				pag->pagf_metadata = 0;
			} else {
				pag->pagi_inodeok = 1;
				maxagi++;
				if (index < max_metadata)
					pag->pagf_metadata = 1;
				else
					pag->pagf_metadata = 0;
			}
		} else {
			pag->pagi_inodeok = 1;
			pag->pagf_metadata = 0;
		}
306 307 308 309

		xfs_perag_put(pag);
	}

310
	return (mp->m_flags & XFS_MOUNT_32BITINODES) ? maxagi : agcount;
311 312
}

H
Hannes Eder 已提交
313
STATIC int
L
Linus Torvalds 已提交
314 315 316 317 318 319 320
xfs_blkdev_get(
	xfs_mount_t		*mp,
	const char		*name,
	struct block_device	**bdevp)
{
	int			error = 0;

321 322
	*bdevp = blkdev_get_by_path(name, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
				    mp);
L
Linus Torvalds 已提交
323 324
	if (IS_ERR(*bdevp)) {
		error = PTR_ERR(*bdevp);
325
		xfs_warn(mp, "Invalid device [%s], error=%d", name, error);
L
Linus Torvalds 已提交
326 327
	}

328
	return error;
L
Linus Torvalds 已提交
329 330
}

H
Hannes Eder 已提交
331
STATIC void
L
Linus Torvalds 已提交
332 333 334 335
xfs_blkdev_put(
	struct block_device	*bdev)
{
	if (bdev)
336
		blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
L
Linus Torvalds 已提交
337 338
}

339 340 341 342
void
xfs_blkdev_issue_flush(
	xfs_buftarg_t		*buftarg)
{
343
	blkdev_issue_flush(buftarg->bt_bdev, GFP_NOFS);
344
}
L
Linus Torvalds 已提交
345

346 347 348 349
STATIC void
xfs_close_devices(
	struct xfs_mount	*mp)
{
350 351
	struct dax_device *dax_ddev = mp->m_ddev_targp->bt_daxdev;

352
	if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
353
		struct block_device *logdev = mp->m_logdev_targp->bt_bdev;
354 355
		struct dax_device *dax_logdev = mp->m_logdev_targp->bt_daxdev;

356
		xfs_free_buftarg(mp->m_logdev_targp);
357
		xfs_blkdev_put(logdev);
358
		fs_put_dax(dax_logdev);
359 360
	}
	if (mp->m_rtdev_targp) {
361
		struct block_device *rtdev = mp->m_rtdev_targp->bt_bdev;
362 363
		struct dax_device *dax_rtdev = mp->m_rtdev_targp->bt_daxdev;

364
		xfs_free_buftarg(mp->m_rtdev_targp);
365
		xfs_blkdev_put(rtdev);
366
		fs_put_dax(dax_rtdev);
367
	}
368
	xfs_free_buftarg(mp->m_ddev_targp);
369
	fs_put_dax(dax_ddev);
370 371 372 373 374 375 376 377 378 379 380 381 382 383
}

/*
 * 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(
384
	struct xfs_mount	*mp)
385 386
{
	struct block_device	*ddev = mp->m_super->s_bdev;
387 388
	struct dax_device	*dax_ddev = fs_dax_get_by_bdev(ddev);
	struct dax_device	*dax_logdev = NULL, *dax_rtdev = NULL;
389 390 391 392 393 394
	struct block_device	*logdev = NULL, *rtdev = NULL;
	int			error;

	/*
	 * Open real time and log devices - order is important.
	 */
395 396
	if (mp->m_logname) {
		error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
397 398
		if (error)
			goto out;
399
		dax_logdev = fs_dax_get_by_bdev(logdev);
400 401
	}

402 403
	if (mp->m_rtname) {
		error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
404 405 406 407
		if (error)
			goto out_close_logdev;

		if (rtdev == ddev || rtdev == logdev) {
408 409
			xfs_warn(mp,
	"Cannot mount filesystem with identical rtdev and ddev/logdev.");
410
			error = -EINVAL;
411 412
			goto out_close_rtdev;
		}
413
		dax_rtdev = fs_dax_get_by_bdev(rtdev);
414 415 416 417 418
	}

	/*
	 * Setup xfs_mount buffer target pointers
	 */
419
	error = -ENOMEM;
420
	mp->m_ddev_targp = xfs_alloc_buftarg(mp, ddev, dax_ddev);
421 422 423 424
	if (!mp->m_ddev_targp)
		goto out_close_rtdev;

	if (rtdev) {
425
		mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev, dax_rtdev);
426 427 428 429 430
		if (!mp->m_rtdev_targp)
			goto out_free_ddev_targ;
	}

	if (logdev && logdev != ddev) {
431
		mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev, dax_logdev);
432 433 434 435 436 437 438 439 440 441
		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)
442
		xfs_free_buftarg(mp->m_rtdev_targp);
443
 out_free_ddev_targ:
444
	xfs_free_buftarg(mp->m_ddev_targp);
445
 out_close_rtdev:
446
	xfs_blkdev_put(rtdev);
447
	fs_put_dax(dax_rtdev);
448
 out_close_logdev:
449
	if (logdev && logdev != ddev) {
450
		xfs_blkdev_put(logdev);
451 452
		fs_put_dax(dax_logdev);
	}
453
 out:
454
	fs_put_dax(dax_ddev);
455 456 457
	return error;
}

458 459 460 461 462 463 464 465
/*
 * Setup xfs_mount buffer target pointers based on superblock
 */
STATIC int
xfs_setup_devices(
	struct xfs_mount	*mp)
{
	int			error;
466

467
	error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize);
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	if (error)
		return error;

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

		if (xfs_sb_version_hassector(&mp->m_sb))
			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;
}
490

491 492 493 494
STATIC int
xfs_init_mount_workqueues(
	struct xfs_mount	*mp)
{
495
	mp->m_buf_workqueue = alloc_workqueue("xfs-buf/%s",
496
			WQ_MEM_RECLAIM|WQ_FREEZABLE, 1, mp->m_super->s_id);
497 498 499
	if (!mp->m_buf_workqueue)
		goto out;

500
	mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
501
			WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_super->s_id);
502
	if (!mp->m_unwritten_workqueue)
503
		goto out_destroy_buf;
504

505
	mp->m_cil_workqueue = alloc_workqueue("xfs-cil/%s",
506
			WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND,
507
			0, mp->m_super->s_id);
508 509
	if (!mp->m_cil_workqueue)
		goto out_destroy_unwritten;
510 511

	mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
512
			WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_super->s_id);
513 514 515
	if (!mp->m_reclaim_workqueue)
		goto out_destroy_cil;

516
	mp->m_eofblocks_workqueue = alloc_workqueue("xfs-eofblocks/%s",
517
			WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_super->s_id);
518
	if (!mp->m_eofblocks_workqueue)
519
		goto out_destroy_reclaim;
520

521
	mp->m_sync_workqueue = alloc_workqueue("xfs-sync/%s", WQ_FREEZABLE, 0,
522
					       mp->m_super->s_id);
523 524 525
	if (!mp->m_sync_workqueue)
		goto out_destroy_eofb;

526 527
	return 0;

528 529
out_destroy_eofb:
	destroy_workqueue(mp->m_eofblocks_workqueue);
530 531 532 533
out_destroy_reclaim:
	destroy_workqueue(mp->m_reclaim_workqueue);
out_destroy_cil:
	destroy_workqueue(mp->m_cil_workqueue);
534 535
out_destroy_unwritten:
	destroy_workqueue(mp->m_unwritten_workqueue);
536 537
out_destroy_buf:
	destroy_workqueue(mp->m_buf_workqueue);
538 539 540 541 542 543 544 545
out:
	return -ENOMEM;
}

STATIC void
xfs_destroy_mount_workqueues(
	struct xfs_mount	*mp)
{
546
	destroy_workqueue(mp->m_sync_workqueue);
547
	destroy_workqueue(mp->m_eofblocks_workqueue);
548
	destroy_workqueue(mp->m_reclaim_workqueue);
549
	destroy_workqueue(mp->m_cil_workqueue);
550
	destroy_workqueue(mp->m_unwritten_workqueue);
551
	destroy_workqueue(mp->m_buf_workqueue);
552 553
}

554 555 556 557 558 559 560 561 562 563 564 565 566 567
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);
	}
}

568 569 570 571 572 573 574 575 576 577
/*
 * 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)
{
578 579 580 581 582
	/*
	 * 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))
583 584
		return;

585 586
	queue_work(mp->m_sync_workqueue, &mp->m_flush_inodes_work);
	flush_work(&mp->m_flush_inodes_work);
587 588
}

589
/* Catch misguided souls that try to use this interface on XFS */
L
Linus Torvalds 已提交
590
STATIC struct inode *
591
xfs_fs_alloc_inode(
L
Linus Torvalds 已提交
592 593
	struct super_block	*sb)
{
594
	BUG();
595
	return NULL;
L
Linus Torvalds 已提交
596 597
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
#ifdef DEBUG
static void
xfs_check_delalloc(
	struct xfs_inode	*ip,
	int			whichfork)
{
	struct xfs_ifork	*ifp = XFS_IFORK_PTR(ip, whichfork);
	struct xfs_bmbt_irec	got;
	struct xfs_iext_cursor	icur;

	if (!ifp || !xfs_iext_lookup_extent(ip, ifp, 0, &icur, &got))
		return;
	do {
		if (isnullstartblock(got.br_startblock)) {
			xfs_warn(ip->i_mount,
	"ino %llx %s fork has delalloc extent at [0x%llx:0x%llx]",
				ip->i_ino,
				whichfork == XFS_DATA_FORK ? "data" : "cow",
				got.br_startoff, got.br_blockcount);
		}
	} while (xfs_iext_next_extent(ifp, &icur, &got));
}
#else
#define xfs_check_delalloc(ip, whichfork)	do { } while (0)
#endif

624
/*
625
 * Now that the generic code is guaranteed not to be accessing
626
 * the linux inode, we can inactivate and reclaim the inode.
627
 */
L
Linus Torvalds 已提交
628
STATIC void
629
xfs_fs_destroy_inode(
630
	struct inode		*inode)
L
Linus Torvalds 已提交
631
{
632 633
	struct xfs_inode	*ip = XFS_I(inode);

634
	trace_xfs_destroy_inode(ip);
635

636
	ASSERT(!rwsem_is_locked(&inode->i_rwsem));
637 638 639 640
	XFS_STATS_INC(ip->i_mount, vn_rele);
	XFS_STATS_INC(ip->i_mount, vn_remove);

	xfs_inactive(ip);
641

642 643 644 645 646 647
	if (!XFS_FORCED_SHUTDOWN(ip->i_mount) && ip->i_delayed_blks) {
		xfs_check_delalloc(ip, XFS_DATA_FORK);
		xfs_check_delalloc(ip, XFS_COW_FORK);
		ASSERT(0);
	}

648
	XFS_STATS_INC(ip->i_mount, vn_reclaim);
649 650 651 652 653 654 655 656

	/*
	 * We should never get here with one of the reclaim flags already set.
	 */
	ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
	ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIM));

	/*
657 658 659 660 661
	 * We always use background reclaim here because even if the
	 * inode is clean, it still may be under IO and hence we have
	 * to take the flush lock. The background reclaim path handles
	 * this more efficiently than we can here, so simply let background
	 * reclaim tear down all inodes.
662
	 */
663
	xfs_inode_set_reclaim_tag(ip);
L
Linus Torvalds 已提交
664 665
}

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
static void
xfs_fs_dirty_inode(
	struct inode			*inode,
	int				flag)
{
	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;
	if (flag != I_DIRTY_SYNC || !(inode->i_state & I_DIRTY_TIME))
		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);
}

688 689 690 691
/*
 * 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
692
 * from the slab. This avoids the need to repeatedly initialise
693 694 695
 * fields in the xfs inode that left in the initialise state
 * when freeing the inode.
 */
696 697
STATIC void
xfs_fs_inode_init_once(
698 699 700 701 702
	void			*inode)
{
	struct xfs_inode	*ip = inode;

	memset(ip, 0, sizeof(struct xfs_inode));
703 704 705 706 707

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

	/* xfs inode */
708 709 710
	atomic_set(&ip->i_pincount, 0);
	spin_lock_init(&ip->i_flags_lock);

711 712
	mrlock_init(&ip->i_mmaplock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
		     "xfsino", ip->i_ino);
713 714 715 716
	mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
		     "xfsino", ip->i_ino);
}

717 718 719 720 721 722 723 724 725 726 727 728 729
/*
 * 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);

730 731 732 733 734 735 736 737 738 739
	/*
	 * 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) {
		ASSERT(ip->i_mount->m_log->l_flags & XLOG_RECOVERY_NEEDED);
		return 0;
	}

740
	return generic_drop_inode(inode);
741 742
}

743 744
static void
xfs_mount_free(
745 746 747 748
	struct xfs_mount	*mp)
{
	kfree(mp->m_rtname);
	kfree(mp->m_logname);
749
	kmem_free(mp);
750 751
}

L
Linus Torvalds 已提交
752
STATIC int
753
xfs_fs_sync_fs(
L
Linus Torvalds 已提交
754 755 756
	struct super_block	*sb,
	int			wait)
{
757
	struct xfs_mount	*mp = XFS_M(sb);
L
Linus Torvalds 已提交
758

759
	/*
760
	 * Doing anything during the async pass would be counterproductive.
761
	 */
762
	if (!wait)
763 764
		return 0;

765
	xfs_log_force(mp, XFS_LOG_SYNC);
766
	if (laptop_mode) {
L
Linus Torvalds 已提交
767 768
		/*
		 * The disk must be active because we're syncing.
769
		 * We schedule log work now (now that the disk is
L
Linus Torvalds 已提交
770 771
		 * active) instead of later (when it might not be).
		 */
772
		flush_delayed_work(&mp->m_log->l_work);
L
Linus Torvalds 已提交
773 774
	}

775
	return 0;
L
Linus Torvalds 已提交
776 777 778
}

STATIC int
779
xfs_fs_statfs(
780
	struct dentry		*dentry,
L
Linus Torvalds 已提交
781 782
	struct kstatfs		*statp)
{
783 784
	struct xfs_mount	*mp = XFS_M(dentry->d_sb);
	xfs_sb_t		*sbp = &mp->m_sb;
785
	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
786 787 788 789
	uint64_t		fakeinos, id;
	uint64_t		icount;
	uint64_t		ifree;
	uint64_t		fdblocks;
790
	xfs_extlen_t		lsize;
791
	int64_t			ffree;
792

793
	statp->f_type = XFS_SUPER_MAGIC;
794 795 796 797 798 799
	statp->f_namelen = MAXNAMELEN - 1;

	id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
	statp->f_fsid.val[0] = (u32)id;
	statp->f_fsid.val[1] = (u32)(id >> 32);

800
	icount = percpu_counter_sum(&mp->m_icount);
801
	ifree = percpu_counter_sum(&mp->m_ifree);
802
	fdblocks = percpu_counter_sum(&mp->m_fdblocks);
803 804 805 806 807

	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;
808 809
	spin_unlock(&mp->m_sb_lock);

810 811
	/* make sure statp->f_bfree does not underflow */
	statp->f_bfree = max_t(int64_t, fdblocks - mp->m_alloc_set_aside, 0);
812 813
	statp->f_bavail = statp->f_bfree;

814
	fakeinos = XFS_FSB_TO_INO(mp, statp->f_bfree);
D
Dave Chinner 已提交
815
	statp->f_files = min(icount + fakeinos, (uint64_t)XFS_MAXINUMBER);
816
	if (M_IGEO(mp)->maxicount)
817 818
		statp->f_files = min_t(typeof(statp->f_files),
					statp->f_files,
819
					M_IGEO(mp)->maxicount);
820

821 822 823 824 825
	/* If sb_icount overshot maxicount, report actual allocation */
	statp->f_files = max_t(typeof(statp->f_files),
					statp->f_files,
					sbp->sb_icount);

826
	/* make sure statp->f_ffree does not underflow */
827
	ffree = statp->f_files - (icount - ifree);
828
	statp->f_ffree = max_t(int64_t, ffree, 0);
829

830

831
	if ((ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
832 833
	    ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
			      (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
C
Christoph Hellwig 已提交
834
		xfs_qm_statvfs(ip, statp);
835 836 837 838 839 840 841 842

	if (XFS_IS_REALTIME_MOUNT(mp) &&
	    (ip->i_d.di_flags & (XFS_DIFLAG_RTINHERIT | XFS_DIFLAG_REALTIME))) {
		statp->f_blocks = sbp->sb_rblocks;
		statp->f_bavail = statp->f_bfree =
			sbp->sb_frextents * sbp->sb_rextsize;
	}

843
	return 0;
L
Linus Torvalds 已提交
844 845
}

846 847 848
STATIC void
xfs_save_resvblks(struct xfs_mount *mp)
{
849
	uint64_t resblks = 0;
850 851 852 853 854 855 856 857

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

STATIC void
xfs_restore_resvblks(struct xfs_mount *mp)
{
858
	uint64_t resblks;
859 860 861 862 863 864 865 866 867 868

	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);
}

869 870 871 872 873 874 875 876
/*
 * Trigger writeback of all the dirty metadata in the file system.
 *
 * This ensures that the metadata is written to their location on disk rather
 * than just existing in transactions in the log. This means after a quiesce
 * there is no log replay required to write the inodes to disk - this is the
 * primary difference between a sync and a quiesce.
 *
877 878 879 880
 * We cancel log work early here to ensure all transactions the log worker may
 * run have finished before we clean up and log the superblock and write an
 * unmount record. The unfreeze process is responsible for restarting the log
 * worker correctly.
881 882 883 884 885 886 887
 */
void
xfs_quiesce_attr(
	struct xfs_mount	*mp)
{
	int	error = 0;

888
	cancel_delayed_work_sync(&mp->m_log->l_work);
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 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 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

	/* force the log to unpin objects from the now complete transactions */
	xfs_log_force(mp, XFS_LOG_SYNC);

	/* reclaim inodes to do any IO before the freeze completes */
	xfs_reclaim_inodes(mp, 0);
	xfs_reclaim_inodes(mp, SYNC_WAIT);

	/* Push the superblock and write an unmount record */
	error = xfs_log_sbcount(mp);
	if (error)
		xfs_warn(mp, "xfs_attr_quiesce: failed to log sb changes. "
				"Frozen image may not be consistent.");
	xfs_log_quiesce(mp);
}

/*
 * 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);

	xfs_stop_block_reaping(mp);
	xfs_save_resvblks(mp);
	xfs_quiesce_attr(mp);
	return xfs_sync_sb(mp, true);
}

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);
	xfs_start_block_reaping(mp);
	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)
{
	int			ronly = (mp->m_flags & XFS_MOUNT_RDONLY);

	/* Fail a mount where the logbuf is smaller than the log stripe */
	if (xfs_sb_version_haslogv2(&mp->m_sb)) {
		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.
	 */
	if (xfs_sb_version_hascrc(&mp->m_sb) &&
	    (mp->m_flags & XFS_MOUNT_NOATTR2)) {
		xfs_warn(mp, "Cannot mount a V5 filesystem as noattr2. "
			     "attr2 is always enabled for V5 filesystems.");
		return -EINVAL;
	}

	/*
	 * mkfs'ed attr2 will turn on attr2 mount unless explicitly
	 * told by noattr2 to turn it off
	 */
	if (xfs_sb_version_hasattr2(&mp->m_sb) &&
	    !(mp->m_flags & XFS_MOUNT_NOATTR2))
		mp->m_flags |= XFS_MOUNT_ATTR2;

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

	if ((mp->m_qflags & (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE)) &&
	    (mp->m_qflags & (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE)) &&
	    !xfs_sb_version_has_pquotino(&mp->m_sb)) {
		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;

	return 0;

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);
}

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);
	ASSERT(XFS_FORCED_SHUTDOWN(mp) ||
	       percpu_counter_sum(&mp->m_delalloc_blks) == 0);
	percpu_counter_destroy(&mp->m_delalloc_blks);
}

static void
xfs_fs_put_super(
	struct super_block	*sb)
{
	struct xfs_mount	*mp = XFS_M(sb);

	/* if ->fill_super failed, we have no mount to tear down */
	if (!sb->s_fs_info)
		return;

	xfs_notice(mp, "Unmounting Filesystem");
	xfs_filestream_unmount(mp);
	xfs_unmountfs(mp);

	xfs_freesb(mp);
	free_percpu(mp->m_stats.xs_stats);
	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)
1096
{
1097 1098
	return xfs_reclaim_inodes_nr(XFS_M(sb), sc->nr_to_scan);
}
1099

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
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,
};
1114

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
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;
1148 1149
}

1150
/*
1151 1152 1153
 * Set mount state from a mount option.
 *
 * NOTE: mp->m_super is NULL here!
1154
 */
1155 1156 1157 1158
static int
xfs_fc_parse_param(
	struct fs_context	*fc,
	struct fs_parameter	*param)
L
Linus Torvalds 已提交
1159
{
1160 1161 1162 1163
	struct xfs_mount	*mp = fc->s_fs_info;
	struct fs_parse_result	result;
	int			size = 0;
	int			opt;
1164

1165
	opt = fs_parse(fc, xfs_fs_parameters, param, &result);
1166 1167
	if (opt < 0)
		return opt;
L
Linus Torvalds 已提交
1168

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 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 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	switch (opt) {
	case Opt_logbufs:
		mp->m_logbufs = result.uint_32;
		return 0;
	case Opt_logbsize:
		if (suffix_kstrtoint(param->string, 10, &mp->m_logbsize))
			return -EINVAL;
		return 0;
	case Opt_logdev:
		kfree(mp->m_logname);
		mp->m_logname = kstrdup(param->string, GFP_KERNEL);
		if (!mp->m_logname)
			return -ENOMEM;
		return 0;
	case Opt_rtdev:
		kfree(mp->m_rtname);
		mp->m_rtname = kstrdup(param->string, GFP_KERNEL);
		if (!mp->m_rtname)
			return -ENOMEM;
		return 0;
	case Opt_allocsize:
		if (suffix_kstrtoint(param->string, 10, &size))
			return -EINVAL;
		mp->m_allocsize_log = ffs(size) - 1;
		mp->m_flags |= XFS_MOUNT_ALLOCSIZE;
		return 0;
	case Opt_grpid:
	case Opt_bsdgroups:
		mp->m_flags |= XFS_MOUNT_GRPID;
		return 0;
	case Opt_nogrpid:
	case Opt_sysvgroups:
		mp->m_flags &= ~XFS_MOUNT_GRPID;
		return 0;
	case Opt_wsync:
		mp->m_flags |= XFS_MOUNT_WSYNC;
		return 0;
	case Opt_norecovery:
		mp->m_flags |= XFS_MOUNT_NORECOVERY;
		return 0;
	case Opt_noalign:
		mp->m_flags |= XFS_MOUNT_NOALIGN;
		return 0;
	case Opt_swalloc:
		mp->m_flags |= XFS_MOUNT_SWALLOC;
		return 0;
	case Opt_sunit:
		mp->m_dalign = result.uint_32;
		return 0;
	case Opt_swidth:
		mp->m_swidth = result.uint_32;
		return 0;
	case Opt_inode32:
		mp->m_flags |= XFS_MOUNT_SMALL_INUMS;
		return 0;
	case Opt_inode64:
		mp->m_flags &= ~XFS_MOUNT_SMALL_INUMS;
		return 0;
	case Opt_nouuid:
		mp->m_flags |= XFS_MOUNT_NOUUID;
		return 0;
	case Opt_ikeep:
		mp->m_flags |= XFS_MOUNT_IKEEP;
		return 0;
	case Opt_noikeep:
		mp->m_flags &= ~XFS_MOUNT_IKEEP;
		return 0;
	case Opt_largeio:
		mp->m_flags |= XFS_MOUNT_LARGEIO;
		return 0;
	case Opt_nolargeio:
		mp->m_flags &= ~XFS_MOUNT_LARGEIO;
		return 0;
	case Opt_attr2:
		mp->m_flags |= XFS_MOUNT_ATTR2;
		return 0;
	case Opt_noattr2:
		mp->m_flags &= ~XFS_MOUNT_ATTR2;
		mp->m_flags |= XFS_MOUNT_NOATTR2;
		return 0;
	case Opt_filestreams:
		mp->m_flags |= XFS_MOUNT_FILESTREAMS;
		return 0;
	case Opt_noquota:
		mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
		mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
		mp->m_qflags &= ~XFS_ALL_QUOTA_ACTIVE;
		return 0;
	case Opt_quota:
	case Opt_uquota:
	case Opt_usrquota:
		mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE |
				 XFS_UQUOTA_ENFD);
		return 0;
	case Opt_qnoenforce:
	case Opt_uqnoenforce:
		mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
		mp->m_qflags &= ~XFS_UQUOTA_ENFD;
		return 0;
	case Opt_pquota:
	case Opt_prjquota:
		mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE |
				 XFS_PQUOTA_ENFD);
		return 0;
	case Opt_pqnoenforce:
		mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
		mp->m_qflags &= ~XFS_PQUOTA_ENFD;
		return 0;
	case Opt_gquota:
	case Opt_grpquota:
		mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE |
				 XFS_GQUOTA_ENFD);
		return 0;
	case Opt_gqnoenforce:
		mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
		mp->m_qflags &= ~XFS_GQUOTA_ENFD;
		return 0;
	case Opt_discard:
		mp->m_flags |= XFS_MOUNT_DISCARD;
		return 0;
	case Opt_nodiscard:
		mp->m_flags &= ~XFS_MOUNT_DISCARD;
		return 0;
#ifdef CONFIG_FS_DAX
	case Opt_dax:
1294 1295 1296 1297
		xfs_mount_set_dax_mode(mp, XFS_DAX_ALWAYS);
		return 0;
	case Opt_dax_enum:
		xfs_mount_set_dax_mode(mp, result.uint_32);
1298 1299 1300 1301 1302 1303
		return 0;
#endif
	default:
		xfs_warn(mp, "unknown mount option [%s].", param->key);
		return -EINVAL;
	}
1304 1305 1306 1307

	return 0;
}

1308 1309
static int
xfs_fc_validate_params(
1310 1311
	struct xfs_mount	*mp)
{
1312
	/*
1313
	 * no recovery flag requires a read-only mount
1314
	 */
1315 1316 1317
	if ((mp->m_flags & XFS_MOUNT_NORECOVERY) &&
	    !(mp->m_flags & XFS_MOUNT_RDONLY)) {
		xfs_warn(mp, "no-recovery mounts must be read-only.");
1318
		return -EINVAL;
1319 1320
	}

1321 1322
	if ((mp->m_flags & XFS_MOUNT_NOALIGN) &&
	    (mp->m_dalign || mp->m_swidth)) {
1323
		xfs_warn(mp,
1324 1325
	"sunit and swidth options incompatible with the noalign option");
		return -EINVAL;
1326 1327
	}

1328 1329
	if (!IS_ENABLED(CONFIG_XFS_QUOTA) && mp->m_qflags != 0) {
		xfs_warn(mp, "quota support not available in this kernel.");
1330
		return -EINVAL;
1331 1332
	}

1333 1334 1335 1336 1337
	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;
	}
1338

1339 1340 1341 1342 1343 1344
	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;
	}
1345

1346 1347 1348 1349 1350 1351 1352 1353
	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;
	}
1354

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
	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;
	}
1365

1366 1367 1368 1369 1370 1371 1372
	if ((mp->m_flags & XFS_MOUNT_ALLOCSIZE) &&
	    (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;
	}
1373

1374
	return 0;
1375
}
1376

1377 1378
static int
xfs_fc_fill_super(
1379
	struct super_block	*sb,
1380
	struct fs_context	*fc)
1381
{
1382
	struct xfs_mount	*mp = sb->s_fs_info;
1383
	struct inode		*root;
1384
	int			flags = 0, error;
1385

1386
	mp->m_super = sb;
L
Linus Torvalds 已提交
1387

1388
	error = xfs_fc_validate_params(mp);
1389
	if (error)
1390
		goto out_free_names;
L
Linus Torvalds 已提交
1391 1392

	sb_min_blocksize(sb, BBSIZE);
1393
	sb->s_xattr = xfs_xattr_handlers;
1394
	sb->s_export_op = &xfs_export_operations;
1395
#ifdef CONFIG_XFS_QUOTA
1396
	sb->s_qcop = &xfs_quotactl_operations;
J
Jan Kara 已提交
1397
	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
1398
#endif
1399
	sb->s_op = &xfs_super_operations;
L
Linus Torvalds 已提交
1400

1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	/*
	 * 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);
	}

1412
	if (fc->sb_flags & SB_SILENT)
1413 1414
		flags |= XFS_MFSI_QUIET;

1415
	error = xfs_open_devices(mp);
1416
	if (error)
1417
		goto out_free_names;
1418

1419
	error = xfs_init_mount_workqueues(mp);
1420 1421
	if (error)
		goto out_close_devices;
1422

1423
	error = xfs_init_percpu_counters(mp);
1424 1425 1426
	if (error)
		goto out_destroy_workqueues;

1427 1428 1429
	/* 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) {
1430
		error = -ENOMEM;
1431 1432 1433
		goto out_destroy_counters;
	}

1434 1435
	error = xfs_readsb(mp, flags);
	if (error)
1436
		goto out_free_stats;
1437 1438

	error = xfs_finish_flags(mp);
1439
	if (error)
1440
		goto out_free_sb;
1441

1442
	error = xfs_setup_devices(mp);
1443
	if (error)
1444
		goto out_free_sb;
1445

1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
	/*
	 * 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.
	 */
	if (XFS_B_TO_FSBT(mp, MAX_LFS_FILESIZE) > XFS_MAX_FILEOFF) {
		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;
	}

1466 1467
	error = xfs_filestream_mount(mp);
	if (error)
1468
		goto out_free_sb;
1469

1470 1471 1472 1473
	/*
	 * 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.
	 */
1474
	sb->s_magic = XFS_SUPER_MAGIC;
1475 1476
	sb->s_blocksize = mp->m_sb.sb_blocksize;
	sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1477
	sb->s_maxbytes = MAX_LFS_FILESIZE;
1478
	sb->s_max_links = XFS_MAXLINK;
L
Linus Torvalds 已提交
1479
	sb->s_time_gran = 1;
1480 1481
	sb->s_time_min = S32_MIN;
	sb->s_time_max = S32_MAX;
1482 1483
	sb->s_iflags |= SB_I_CGROUPWB;

L
Linus Torvalds 已提交
1484 1485
	set_posix_acl_flag(sb);

1486 1487
	/* version 5 superblocks support inode version counters. */
	if (XFS_SB_VERSION_NUM(&mp->m_sb) == XFS_SB_VERSION_5)
1488
		sb->s_flags |= SB_I_VERSION;
1489

1490
	if (mp->m_flags & XFS_MOUNT_DAX_ALWAYS) {
1491
		bool rtdev_is_dax = false, datadev_is_dax;
1492

1493
		xfs_warn(mp,
1494 1495
		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");

1496 1497
		datadev_is_dax = bdev_dax_supported(mp->m_ddev_targp->bt_bdev,
			sb->s_blocksize);
1498
		if (mp->m_rtdev_targp)
1499 1500 1501
			rtdev_is_dax = bdev_dax_supported(
				mp->m_rtdev_targp->bt_bdev, sb->s_blocksize);
		if (!rtdev_is_dax && !datadev_is_dax) {
1502
			xfs_alert(mp,
1503
			"DAX unsupported by block device. Turning off DAX.");
1504
			xfs_mount_set_dax_mode(mp, XFS_DAX_NEVER);
1505
		}
1506
		if (xfs_sb_version_hasreflink(&mp->m_sb)) {
1507
			xfs_alert(mp,
1508
		"DAX and reflink cannot be used together!");
1509 1510 1511
			error = -EINVAL;
			goto out_filestream_unmount;
		}
1512 1513
	}

1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	if (mp->m_flags & XFS_MOUNT_DISCARD) {
		struct request_queue *q = bdev_get_queue(sb->s_bdev);

		if (!blk_queue_discard(q)) {
			xfs_warn(mp, "mounting with \"discard\" option, but "
					"the device does not support discard");
			mp->m_flags &= ~XFS_MOUNT_DISCARD;
		}
	}

1524 1525 1526
	if (xfs_sb_version_hasreflink(&mp->m_sb)) {
		if (mp->m_sb.sb_rblocks) {
			xfs_alert(mp,
1527
	"reflink not compatible with realtime device!");
1528 1529 1530 1531 1532 1533 1534 1535
			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;
		}
1536 1537
	}

1538
	if (xfs_sb_version_hasrmapbt(&mp->m_sb) && mp->m_sb.sb_rblocks) {
1539
		xfs_alert(mp,
1540 1541 1542
	"reverse mapping btree not compatible with realtime device!");
		error = -EINVAL;
		goto out_filestream_unmount;
1543
	}
1544

1545
	error = xfs_mountfs(mp);
1546
	if (error)
1547
		goto out_filestream_unmount;
1548

1549
	root = igrab(VFS_I(mp->m_rootip));
1550
	if (!root) {
1551
		error = -ENOENT;
1552
		goto out_unmount;
C
Christoph Hellwig 已提交
1553
	}
1554
	sb->s_root = d_make_root(root);
1555
	if (!sb->s_root) {
1556
		error = -ENOMEM;
1557
		goto out_unmount;
L
Linus Torvalds 已提交
1558
	}
1559

L
Linus Torvalds 已提交
1560
	return 0;
1561

1562
 out_filestream_unmount:
1563
	xfs_filestream_unmount(mp);
1564 1565
 out_free_sb:
	xfs_freesb(mp);
1566 1567
 out_free_stats:
	free_percpu(mp->m_stats.xs_stats);
1568
 out_destroy_counters:
1569
	xfs_destroy_percpu_counters(mp);
1570
 out_destroy_workqueues:
1571
	xfs_destroy_mount_workqueues(mp);
1572
 out_close_devices:
1573
	xfs_close_devices(mp);
1574
 out_free_names:
1575
	sb->s_fs_info = NULL;
1576
	xfs_mount_free(mp);
1577
	return error;
1578

1579
 out_unmount:
1580
	xfs_filestream_unmount(mp);
1581
	xfs_unmountfs(mp);
1582
	goto out_free_sb;
L
Linus Torvalds 已提交
1583 1584
}

1585 1586 1587 1588 1589 1590 1591
static int
xfs_fc_get_tree(
	struct fs_context	*fc)
{
	return get_tree_bdev(fc, xfs_fc_fill_super);
}

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 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
static int
xfs_remount_rw(
	struct xfs_mount	*mp)
{
	struct xfs_sb		*sbp = &mp->m_sb;
	int error;

	if (mp->m_flags & XFS_MOUNT_NORECOVERY) {
		xfs_warn(mp,
			"ro->rw transition prohibited on norecovery mount");
		return -EINVAL;
	}

	if (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 &&
	    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;
	}

	mp->m_flags &= ~XFS_MOUNT_RDONLY;

	/*
	 * 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);

	/* Recover any CoW blocks that never got remapped. */
	error = xfs_reflink_recover_cow(mp);
	if (error) {
		xfs_err(mp,
			"Error %d recovering leftover CoW allocations.", error);
1641
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753
		return error;
	}
	xfs_start_block_reaping(mp);

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

	return 0;
}

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

	/*
	 * Cancel background eofb scanning so it cannot race with the final
	 * log force+buftarg wait and deadlock the remount.
	 */
	xfs_stop_block_reaping(mp);

	/* Get rid of any leftover CoW reservations... */
	error = xfs_icache_free_cowblocks(mp, NULL);
	if (error) {
		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
		return error;
	}

	/* 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);

	xfs_quiesce_attr(mp);
	mp->m_flags |= XFS_MOUNT_RDONLY;

	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
xfs_fc_reconfigure(
	struct fs_context *fc)
{
	struct xfs_mount	*mp = XFS_M(fc->root->d_sb);
	struct xfs_mount        *new_mp = fc->s_fs_info;
	xfs_sb_t		*sbp = &mp->m_sb;
	int			flags = fc->sb_flags;
	int			error;

	error = xfs_fc_validate_params(new_mp);
	if (error)
		return error;

	sync_filesystem(mp->m_super);

	/* inode32 -> inode64 */
	if ((mp->m_flags & XFS_MOUNT_SMALL_INUMS) &&
	    !(new_mp->m_flags & XFS_MOUNT_SMALL_INUMS)) {
		mp->m_flags &= ~XFS_MOUNT_SMALL_INUMS;
		mp->m_maxagi = xfs_set_inode_alloc(mp, sbp->sb_agcount);
	}

	/* inode64 -> inode32 */
	if (!(mp->m_flags & XFS_MOUNT_SMALL_INUMS) &&
	    (new_mp->m_flags & XFS_MOUNT_SMALL_INUMS)) {
		mp->m_flags |= XFS_MOUNT_SMALL_INUMS;
		mp->m_maxagi = xfs_set_inode_alloc(mp, sbp->sb_agcount);
	}

	/* ro -> rw */
	if ((mp->m_flags & XFS_MOUNT_RDONLY) && !(flags & SB_RDONLY)) {
		error = xfs_remount_rw(mp);
		if (error)
			return error;
	}

	/* rw -> ro */
	if (!(mp->m_flags & XFS_MOUNT_RDONLY) && (flags & SB_RDONLY)) {
		error = xfs_remount_ro(mp);
		if (error)
			return error;
	}

	return 0;
}

1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
static void xfs_fc_free(
	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 = {
	.parse_param = xfs_fc_parse_param,
	.get_tree    = xfs_fc_get_tree,
	.reconfigure = xfs_fc_reconfigure,
	.free        = xfs_fc_free,
};

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

1781
	mp = kmem_alloc(sizeof(struct xfs_mount), KM_ZERO);
1782 1783 1784
	if (!mp)
		return -ENOMEM;

1785 1786 1787 1788 1789
	spin_lock_init(&mp->m_sb_lock);
	spin_lock_init(&mp->m_agirotor_lock);
	INIT_RADIX_TREE(&mp->m_perag_tree, GFP_ATOMIC);
	spin_lock_init(&mp->m_perag_lock);
	mutex_init(&mp->m_growlock);
1790
	INIT_WORK(&mp->m_flush_inodes_work, xfs_flush_inodes_worker);
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
	INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
	INIT_DELAYED_WORK(&mp->m_eofblocks_work, xfs_eofblocks_worker);
	INIT_DELAYED_WORK(&mp->m_cowblocks_work, xfs_cowblocks_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;

1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
	/*
	 * 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)
		mp->m_flags |= XFS_MOUNT_RDONLY;
	if (fc->sb_flags & SB_DIRSYNC)
		mp->m_flags |= XFS_MOUNT_DIRSYNC;
	if (fc->sb_flags & SB_SYNCHRONOUS)
		mp->m_flags |= XFS_MOUNT_WSYNC;

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

	return 0;
}

A
Andrew Morton 已提交
1826
static struct file_system_type xfs_fs_type = {
L
Linus Torvalds 已提交
1827 1828
	.owner			= THIS_MODULE,
	.name			= "xfs",
1829
	.init_fs_context	= xfs_init_fs_context,
1830
	.parameters		= xfs_fs_parameters,
L
Linus Torvalds 已提交
1831 1832 1833
	.kill_sb		= kill_block_super,
	.fs_flags		= FS_REQUIRES_DEV,
};
1834
MODULE_ALIAS_FS("xfs");
L
Linus Torvalds 已提交
1835

1836 1837 1838
STATIC int __init
xfs_init_zones(void)
{
1839 1840 1841
	xfs_log_ticket_zone = kmem_cache_create("xfs_log_ticket",
						sizeof(struct xlog_ticket),
						0, 0, NULL);
1842
	if (!xfs_log_ticket_zone)
1843
		goto out;
1844

1845 1846 1847
	xfs_bmap_free_item_zone = kmem_cache_create("xfs_bmap_free_item",
					sizeof(struct xfs_extent_free_item),
					0, 0, NULL);
1848 1849
	if (!xfs_bmap_free_item_zone)
		goto out_destroy_log_ticket_zone;
1850

1851 1852 1853
	xfs_btree_cur_zone = kmem_cache_create("xfs_btree_cur",
					       sizeof(struct xfs_btree_cur),
					       0, 0, NULL);
1854 1855 1856
	if (!xfs_btree_cur_zone)
		goto out_destroy_bmap_free_item_zone;

1857 1858 1859
	xfs_da_state_zone = kmem_cache_create("xfs_da_state",
					      sizeof(struct xfs_da_state),
					      0, 0, NULL);
1860 1861 1862
	if (!xfs_da_state_zone)
		goto out_destroy_btree_cur_zone;

1863 1864 1865
	xfs_ifork_zone = kmem_cache_create("xfs_ifork",
					   sizeof(struct xfs_ifork),
					   0, 0, NULL);
1866
	if (!xfs_ifork_zone)
1867
		goto out_destroy_da_state_zone;
1868

1869 1870 1871
	xfs_trans_zone = kmem_cache_create("xf_trans",
					   sizeof(struct xfs_trans),
					   0, 0, NULL);
1872 1873 1874
	if (!xfs_trans_zone)
		goto out_destroy_ifork_zone;

1875

1876 1877 1878 1879 1880
	/*
	 * The size of the zone allocated buf log item is the maximum
	 * size possible under XFS.  This wastes a little bit of memory,
	 * but it is much faster.
	 */
1881 1882 1883
	xfs_buf_item_zone = kmem_cache_create("xfs_buf_item",
					      sizeof(struct xfs_buf_log_item),
					      0, 0, NULL);
1884
	if (!xfs_buf_item_zone)
1885
		goto out_destroy_trans_zone;
1886

1887 1888 1889 1890 1891
	xfs_efd_zone = kmem_cache_create("xfs_efd_item",
					(sizeof(struct xfs_efd_log_item) +
					(XFS_EFD_MAX_FAST_EXTENTS - 1) *
					sizeof(struct xfs_extent)),
					0, 0, NULL);
1892 1893 1894
	if (!xfs_efd_zone)
		goto out_destroy_buf_item_zone;

1895 1896 1897 1898 1899
	xfs_efi_zone = kmem_cache_create("xfs_efi_item",
					 (sizeof(struct xfs_efi_log_item) +
					 (XFS_EFI_MAX_FAST_EXTENTS - 1) *
					 sizeof(struct xfs_extent)),
					 0, 0, NULL);
1900 1901 1902
	if (!xfs_efi_zone)
		goto out_destroy_efd_zone;

1903 1904 1905 1906 1907 1908
	xfs_inode_zone = kmem_cache_create("xfs_inode",
					   sizeof(struct xfs_inode), 0,
					   (SLAB_HWCACHE_ALIGN |
					    SLAB_RECLAIM_ACCOUNT |
					    SLAB_MEM_SPREAD | SLAB_ACCOUNT),
					   xfs_fs_inode_init_once);
1909 1910 1911
	if (!xfs_inode_zone)
		goto out_destroy_efi_zone;

1912 1913
	xfs_ili_zone = kmem_cache_create("xfs_ili",
					 sizeof(struct xfs_inode_log_item), 0,
1914 1915
					 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
					 NULL);
1916 1917
	if (!xfs_ili_zone)
		goto out_destroy_inode_zone;
1918 1919 1920 1921

	xfs_icreate_zone = kmem_cache_create("xfs_icr",
					     sizeof(struct xfs_icreate_item),
					     0, 0, NULL);
1922 1923
	if (!xfs_icreate_zone)
		goto out_destroy_ili_zone;
1924

1925 1926 1927
	xfs_rud_zone = kmem_cache_create("xfs_rud_item",
					 sizeof(struct xfs_rud_log_item),
					 0, 0, NULL);
1928 1929 1930
	if (!xfs_rud_zone)
		goto out_destroy_icreate_zone;

1931
	xfs_rui_zone = kmem_cache_create("xfs_rui_item",
1932
			xfs_rui_log_item_sizeof(XFS_RUI_MAX_FAST_EXTENTS),
1933
			0, 0, NULL);
1934 1935 1936
	if (!xfs_rui_zone)
		goto out_destroy_rud_zone;

1937 1938 1939
	xfs_cud_zone = kmem_cache_create("xfs_cud_item",
					 sizeof(struct xfs_cud_log_item),
					 0, 0, NULL);
1940 1941 1942
	if (!xfs_cud_zone)
		goto out_destroy_rui_zone;

1943
	xfs_cui_zone = kmem_cache_create("xfs_cui_item",
1944
			xfs_cui_log_item_sizeof(XFS_CUI_MAX_FAST_EXTENTS),
1945
			0, 0, NULL);
1946 1947 1948
	if (!xfs_cui_zone)
		goto out_destroy_cud_zone;

1949 1950 1951
	xfs_bud_zone = kmem_cache_create("xfs_bud_item",
					 sizeof(struct xfs_bud_log_item),
					 0, 0, NULL);
1952 1953 1954
	if (!xfs_bud_zone)
		goto out_destroy_cui_zone;

1955
	xfs_bui_zone = kmem_cache_create("xfs_bui_item",
1956
			xfs_bui_log_item_sizeof(XFS_BUI_MAX_FAST_EXTENTS),
1957
			0, 0, NULL);
1958 1959 1960
	if (!xfs_bui_zone)
		goto out_destroy_bud_zone;

1961 1962
	return 0;

1963
 out_destroy_bud_zone:
1964
	kmem_cache_destroy(xfs_bud_zone);
1965
 out_destroy_cui_zone:
1966
	kmem_cache_destroy(xfs_cui_zone);
1967
 out_destroy_cud_zone:
1968
	kmem_cache_destroy(xfs_cud_zone);
1969
 out_destroy_rui_zone:
1970
	kmem_cache_destroy(xfs_rui_zone);
1971
 out_destroy_rud_zone:
1972
	kmem_cache_destroy(xfs_rud_zone);
1973
 out_destroy_icreate_zone:
1974
	kmem_cache_destroy(xfs_icreate_zone);
1975
 out_destroy_ili_zone:
1976
	kmem_cache_destroy(xfs_ili_zone);
1977
 out_destroy_inode_zone:
1978
	kmem_cache_destroy(xfs_inode_zone);
1979
 out_destroy_efi_zone:
1980
	kmem_cache_destroy(xfs_efi_zone);
1981
 out_destroy_efd_zone:
1982
	kmem_cache_destroy(xfs_efd_zone);
1983
 out_destroy_buf_item_zone:
1984
	kmem_cache_destroy(xfs_buf_item_zone);
1985
 out_destroy_trans_zone:
1986
	kmem_cache_destroy(xfs_trans_zone);
1987
 out_destroy_ifork_zone:
1988
	kmem_cache_destroy(xfs_ifork_zone);
1989
 out_destroy_da_state_zone:
1990
	kmem_cache_destroy(xfs_da_state_zone);
1991
 out_destroy_btree_cur_zone:
1992
	kmem_cache_destroy(xfs_btree_cur_zone);
1993
 out_destroy_bmap_free_item_zone:
1994
	kmem_cache_destroy(xfs_bmap_free_item_zone);
1995
 out_destroy_log_ticket_zone:
1996
	kmem_cache_destroy(xfs_log_ticket_zone);
1997 1998 1999 2000 2001 2002 2003
 out:
	return -ENOMEM;
}

STATIC void
xfs_destroy_zones(void)
{
2004 2005 2006 2007 2008
	/*
	 * Make sure all delayed rcu free are flushed before we
	 * destroy caches.
	 */
	rcu_barrier();
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
	kmem_cache_destroy(xfs_bui_zone);
	kmem_cache_destroy(xfs_bud_zone);
	kmem_cache_destroy(xfs_cui_zone);
	kmem_cache_destroy(xfs_cud_zone);
	kmem_cache_destroy(xfs_rui_zone);
	kmem_cache_destroy(xfs_rud_zone);
	kmem_cache_destroy(xfs_icreate_zone);
	kmem_cache_destroy(xfs_ili_zone);
	kmem_cache_destroy(xfs_inode_zone);
	kmem_cache_destroy(xfs_efi_zone);
	kmem_cache_destroy(xfs_efd_zone);
	kmem_cache_destroy(xfs_buf_item_zone);
	kmem_cache_destroy(xfs_trans_zone);
	kmem_cache_destroy(xfs_ifork_zone);
	kmem_cache_destroy(xfs_da_state_zone);
	kmem_cache_destroy(xfs_btree_cur_zone);
	kmem_cache_destroy(xfs_bmap_free_item_zone);
	kmem_cache_destroy(xfs_log_ticket_zone);
2027
}
L
Linus Torvalds 已提交
2028

2029 2030 2031
STATIC int __init
xfs_init_workqueues(void)
{
2032 2033 2034 2035 2036 2037
	/*
	 * 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.
	 */
2038 2039
	xfs_alloc_wq = alloc_workqueue("xfsalloc",
			WQ_MEM_RECLAIM|WQ_FREEZABLE, 0);
2040
	if (!xfs_alloc_wq)
2041
		return -ENOMEM;
2042

2043 2044 2045 2046
	xfs_discard_wq = alloc_workqueue("xfsdiscard", WQ_UNBOUND, 0);
	if (!xfs_discard_wq)
		goto out_free_alloc_wq;

2047
	return 0;
2048 2049 2050
out_free_alloc_wq:
	destroy_workqueue(xfs_alloc_wq);
	return -ENOMEM;
2051 2052
}

2053
STATIC void
2054 2055
xfs_destroy_workqueues(void)
{
2056
	destroy_workqueue(xfs_discard_wq);
2057
	destroy_workqueue(xfs_alloc_wq);
2058 2059
}

L
Linus Torvalds 已提交
2060
STATIC int __init
2061
init_xfs_fs(void)
L
Linus Torvalds 已提交
2062 2063 2064
{
	int			error;

2065 2066
	xfs_check_ondisk_structs();

2067 2068
	printk(KERN_INFO XFS_VERSION_STRING " with "
			 XFS_BUILD_OPTIONS " enabled\n");
L
Linus Torvalds 已提交
2069

2070
	xfs_dir_startup();
L
Linus Torvalds 已提交
2071

2072
	error = xfs_init_zones();
2073 2074 2075
	if (error)
		goto out;

2076
	error = xfs_init_workqueues();
2077
	if (error)
2078
		goto out_destroy_zones;
2079

2080 2081 2082 2083
	error = xfs_mru_cache_init();
	if (error)
		goto out_destroy_wq;

2084
	error = xfs_buf_init();
2085
	if (error)
2086
		goto out_mru_cache_uninit;
2087 2088 2089 2090 2091 2092 2093 2094

	error = xfs_init_procfs();
	if (error)
		goto out_buf_terminate;

	error = xfs_sysctl_register();
	if (error)
		goto out_cleanup_procfs;
L
Linus Torvalds 已提交
2095

B
Brian Foster 已提交
2096 2097 2098
	xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
	if (!xfs_kset) {
		error = -ENOMEM;
2099
		goto out_sysctl_unregister;
B
Brian Foster 已提交
2100 2101
	}

2102 2103 2104 2105 2106 2107 2108 2109 2110
	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,
2111 2112
			       "stats");
	if (error)
2113
		goto out_free_stats;
2114

2115 2116 2117
#ifdef DEBUG
	xfs_dbg_kobj.kobject.kset = xfs_kset;
	error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
2118
	if (error)
2119
		goto out_remove_stats_kobj;
2120 2121 2122 2123
#endif

	error = xfs_qm_init();
	if (error)
2124
		goto out_remove_dbg_kobj;
L
Linus Torvalds 已提交
2125 2126 2127

	error = register_filesystem(&xfs_fs_type);
	if (error)
2128
		goto out_qm_exit;
L
Linus Torvalds 已提交
2129 2130
	return 0;

2131 2132
 out_qm_exit:
	xfs_qm_exit();
2133
 out_remove_dbg_kobj:
2134 2135
#ifdef DEBUG
	xfs_sysfs_del(&xfs_dbg_kobj);
2136
 out_remove_stats_kobj:
2137
#endif
2138 2139 2140
	xfs_sysfs_del(&xfsstats.xs_kobj);
 out_free_stats:
	free_percpu(xfsstats.xs_stats);
2141
 out_kset_unregister:
B
Brian Foster 已提交
2142
	kset_unregister(xfs_kset);
2143 2144 2145 2146 2147
 out_sysctl_unregister:
	xfs_sysctl_unregister();
 out_cleanup_procfs:
	xfs_cleanup_procfs();
 out_buf_terminate:
2148
	xfs_buf_terminate();
2149 2150
 out_mru_cache_uninit:
	xfs_mru_cache_uninit();
2151 2152
 out_destroy_wq:
	xfs_destroy_workqueues();
2153
 out_destroy_zones:
2154
	xfs_destroy_zones();
2155
 out:
L
Linus Torvalds 已提交
2156 2157 2158 2159
	return error;
}

STATIC void __exit
2160
exit_xfs_fs(void)
L
Linus Torvalds 已提交
2161
{
2162
	xfs_qm_exit();
L
Linus Torvalds 已提交
2163
	unregister_filesystem(&xfs_fs_type);
2164 2165 2166
#ifdef DEBUG
	xfs_sysfs_del(&xfs_dbg_kobj);
#endif
2167 2168
	xfs_sysfs_del(&xfsstats.xs_kobj);
	free_percpu(xfsstats.xs_stats);
B
Brian Foster 已提交
2169
	kset_unregister(xfs_kset);
2170 2171
	xfs_sysctl_unregister();
	xfs_cleanup_procfs();
2172
	xfs_buf_terminate();
2173
	xfs_mru_cache_uninit();
2174
	xfs_destroy_workqueues();
2175
	xfs_destroy_zones();
2176
	xfs_uuid_table_free();
L
Linus Torvalds 已提交
2177 2178 2179 2180 2181 2182 2183 2184
}

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");
新手
引导
客服 返回
顶部