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