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