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