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