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