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