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