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