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