xfs: merge xfs_ag.h into xfs_format.h
[firefly-linux-kernel-4.4.55.git] / fs / xfs / xfs_super.c
1 /*
2  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "xfs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_inum.h"
25 #include "xfs_sb.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_inode.h"
29 #include "xfs_btree.h"
30 #include "xfs_bmap.h"
31 #include "xfs_alloc.h"
32 #include "xfs_error.h"
33 #include "xfs_fsops.h"
34 #include "xfs_trans.h"
35 #include "xfs_buf_item.h"
36 #include "xfs_log.h"
37 #include "xfs_log_priv.h"
38 #include "xfs_da_btree.h"
39 #include "xfs_dir2.h"
40 #include "xfs_extfree_item.h"
41 #include "xfs_mru_cache.h"
42 #include "xfs_inode_item.h"
43 #include "xfs_icache.h"
44 #include "xfs_trace.h"
45 #include "xfs_icreate_item.h"
46 #include "xfs_filestream.h"
47 #include "xfs_quota.h"
48 #include "xfs_sysfs.h"
49
50 #include <linux/namei.h>
51 #include <linux/init.h>
52 #include <linux/slab.h>
53 #include <linux/mount.h>
54 #include <linux/mempool.h>
55 #include <linux/writeback.h>
56 #include <linux/kthread.h>
57 #include <linux/freezer.h>
58 #include <linux/parser.h>
59
60 static const struct super_operations xfs_super_operations;
61 static kmem_zone_t *xfs_ioend_zone;
62 mempool_t *xfs_ioend_pool;
63
64 static struct kset *xfs_kset;           /* top-level xfs sysfs dir */
65 #ifdef DEBUG
66 static struct xfs_kobj xfs_dbg_kobj;    /* global debug sysfs attrs */
67 #endif
68
69 #define MNTOPT_LOGBUFS  "logbufs"       /* number of XFS log buffers */
70 #define MNTOPT_LOGBSIZE "logbsize"      /* size of XFS log buffers */
71 #define MNTOPT_LOGDEV   "logdev"        /* log device */
72 #define MNTOPT_RTDEV    "rtdev"         /* realtime I/O device */
73 #define MNTOPT_BIOSIZE  "biosize"       /* log2 of preferred buffered io size */
74 #define MNTOPT_WSYNC    "wsync"         /* safe-mode nfs compatible mount */
75 #define MNTOPT_NOALIGN  "noalign"       /* turn off stripe alignment */
76 #define MNTOPT_SWALLOC  "swalloc"       /* turn on stripe width allocation */
77 #define MNTOPT_SUNIT    "sunit"         /* data volume stripe unit */
78 #define MNTOPT_SWIDTH   "swidth"        /* data volume stripe width */
79 #define MNTOPT_NOUUID   "nouuid"        /* ignore filesystem UUID */
80 #define MNTOPT_MTPT     "mtpt"          /* filesystem mount point */
81 #define MNTOPT_GRPID    "grpid"         /* group-ID from parent directory */
82 #define MNTOPT_NOGRPID  "nogrpid"       /* group-ID from current process */
83 #define MNTOPT_BSDGROUPS    "bsdgroups"    /* group-ID from parent directory */
84 #define MNTOPT_SYSVGROUPS   "sysvgroups"   /* group-ID from current process */
85 #define MNTOPT_ALLOCSIZE    "allocsize"    /* preferred allocation size */
86 #define MNTOPT_NORECOVERY   "norecovery"   /* don't run XFS recovery */
87 #define MNTOPT_BARRIER  "barrier"       /* use writer barriers for log write and
88                                          * unwritten extent conversion */
89 #define MNTOPT_NOBARRIER "nobarrier"    /* .. disable */
90 #define MNTOPT_64BITINODE   "inode64"   /* inodes can be allocated anywhere */
91 #define MNTOPT_32BITINODE   "inode32"   /* inode allocation limited to
92                                          * XFS_MAXINUMBER_32 */
93 #define MNTOPT_IKEEP    "ikeep"         /* do not free empty inode clusters */
94 #define MNTOPT_NOIKEEP  "noikeep"       /* free empty inode clusters */
95 #define MNTOPT_LARGEIO     "largeio"    /* report large I/O sizes in stat() */
96 #define MNTOPT_NOLARGEIO   "nolargeio"  /* do not report large I/O sizes
97                                          * in stat(). */
98 #define MNTOPT_ATTR2    "attr2"         /* do use attr2 attribute format */
99 #define MNTOPT_NOATTR2  "noattr2"       /* do not use attr2 attribute format */
100 #define MNTOPT_FILESTREAM  "filestreams" /* use filestreams allocator */
101 #define MNTOPT_QUOTA    "quota"         /* disk quotas (user) */
102 #define MNTOPT_NOQUOTA  "noquota"       /* no quotas */
103 #define MNTOPT_USRQUOTA "usrquota"      /* user quota enabled */
104 #define MNTOPT_GRPQUOTA "grpquota"      /* group quota enabled */
105 #define MNTOPT_PRJQUOTA "prjquota"      /* project quota enabled */
106 #define MNTOPT_UQUOTA   "uquota"        /* user quota (IRIX variant) */
107 #define MNTOPT_GQUOTA   "gquota"        /* group quota (IRIX variant) */
108 #define MNTOPT_PQUOTA   "pquota"        /* project quota (IRIX variant) */
109 #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
110 #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
111 #define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
112 #define MNTOPT_QUOTANOENF  "qnoenforce" /* same as uqnoenforce */
113 #define MNTOPT_DELAYLOG    "delaylog"   /* Delayed logging enabled */
114 #define MNTOPT_NODELAYLOG  "nodelaylog" /* Delayed logging disabled */
115 #define MNTOPT_DISCARD     "discard"    /* Discard unused blocks */
116 #define MNTOPT_NODISCARD   "nodiscard"  /* Do not discard unused blocks */
117
118 /*
119  * Table driven mount option parser.
120  *
121  * Currently only used for remount, but it will be used for mount
122  * in the future, too.
123  */
124 enum {
125         Opt_barrier,
126         Opt_nobarrier,
127         Opt_inode64,
128         Opt_inode32,
129         Opt_err
130 };
131
132 static const match_table_t tokens = {
133         {Opt_barrier, "barrier"},
134         {Opt_nobarrier, "nobarrier"},
135         {Opt_inode64, "inode64"},
136         {Opt_inode32, "inode32"},
137         {Opt_err, NULL}
138 };
139
140
141 STATIC unsigned long
142 suffix_kstrtoint(char *s, unsigned int base, int *res)
143 {
144         int     last, shift_left_factor = 0, _res;
145         char    *value = s;
146
147         last = strlen(value) - 1;
148         if (value[last] == 'K' || value[last] == 'k') {
149                 shift_left_factor = 10;
150                 value[last] = '\0';
151         }
152         if (value[last] == 'M' || value[last] == 'm') {
153                 shift_left_factor = 20;
154                 value[last] = '\0';
155         }
156         if (value[last] == 'G' || value[last] == 'g') {
157                 shift_left_factor = 30;
158                 value[last] = '\0';
159         }
160
161         if (kstrtoint(s, base, &_res))
162                 return -EINVAL;
163         *res = _res << shift_left_factor;
164         return 0;
165 }
166
167 /*
168  * This function fills in xfs_mount_t fields based on mount args.
169  * Note: the superblock has _not_ yet been read in.
170  *
171  * Note that this function leaks the various device name allocations on
172  * failure.  The caller takes care of them.
173  */
174 STATIC int
175 xfs_parseargs(
176         struct xfs_mount        *mp,
177         char                    *options)
178 {
179         struct super_block      *sb = mp->m_super;
180         char                    *this_char, *value;
181         int                     dsunit = 0;
182         int                     dswidth = 0;
183         int                     iosize = 0;
184         __uint8_t               iosizelog = 0;
185
186         /*
187          * set up the mount name first so all the errors will refer to the
188          * correct device.
189          */
190         mp->m_fsname = kstrndup(sb->s_id, MAXNAMELEN, GFP_KERNEL);
191         if (!mp->m_fsname)
192                 return -ENOMEM;
193         mp->m_fsname_len = strlen(mp->m_fsname) + 1;
194
195         /*
196          * Copy binary VFS mount flags we are interested in.
197          */
198         if (sb->s_flags & MS_RDONLY)
199                 mp->m_flags |= XFS_MOUNT_RDONLY;
200         if (sb->s_flags & MS_DIRSYNC)
201                 mp->m_flags |= XFS_MOUNT_DIRSYNC;
202         if (sb->s_flags & MS_SYNCHRONOUS)
203                 mp->m_flags |= XFS_MOUNT_WSYNC;
204
205         /*
206          * Set some default flags that could be cleared by the mount option
207          * parsing.
208          */
209         mp->m_flags |= XFS_MOUNT_BARRIER;
210         mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
211
212         /*
213          * These can be overridden by the mount option parsing.
214          */
215         mp->m_logbufs = -1;
216         mp->m_logbsize = -1;
217
218         if (!options)
219                 goto done;
220
221         while ((this_char = strsep(&options, ",")) != NULL) {
222                 if (!*this_char)
223                         continue;
224                 if ((value = strchr(this_char, '=')) != NULL)
225                         *value++ = 0;
226
227                 if (!strcmp(this_char, MNTOPT_LOGBUFS)) {
228                         if (!value || !*value) {
229                                 xfs_warn(mp, "%s option requires an argument",
230                                         this_char);
231                                 return -EINVAL;
232                         }
233                         if (kstrtoint(value, 10, &mp->m_logbufs))
234                                 return -EINVAL;
235                 } else if (!strcmp(this_char, MNTOPT_LOGBSIZE)) {
236                         if (!value || !*value) {
237                                 xfs_warn(mp, "%s option requires an argument",
238                                         this_char);
239                                 return -EINVAL;
240                         }
241                         if (suffix_kstrtoint(value, 10, &mp->m_logbsize))
242                                 return -EINVAL;
243                 } else if (!strcmp(this_char, MNTOPT_LOGDEV)) {
244                         if (!value || !*value) {
245                                 xfs_warn(mp, "%s option requires an argument",
246                                         this_char);
247                                 return -EINVAL;
248                         }
249                         mp->m_logname = kstrndup(value, MAXNAMELEN, GFP_KERNEL);
250                         if (!mp->m_logname)
251                                 return -ENOMEM;
252                 } else if (!strcmp(this_char, MNTOPT_MTPT)) {
253                         xfs_warn(mp, "%s option not allowed on this system",
254                                 this_char);
255                         return -EINVAL;
256                 } else if (!strcmp(this_char, MNTOPT_RTDEV)) {
257                         if (!value || !*value) {
258                                 xfs_warn(mp, "%s option requires an argument",
259                                         this_char);
260                                 return -EINVAL;
261                         }
262                         mp->m_rtname = kstrndup(value, MAXNAMELEN, GFP_KERNEL);
263                         if (!mp->m_rtname)
264                                 return -ENOMEM;
265                 } else if (!strcmp(this_char, MNTOPT_BIOSIZE)) {
266                         if (!value || !*value) {
267                                 xfs_warn(mp, "%s option requires an argument",
268                                         this_char);
269                                 return -EINVAL;
270                         }
271                         if (kstrtoint(value, 10, &iosize))
272                                 return -EINVAL;
273                         iosizelog = ffs(iosize) - 1;
274                 } else if (!strcmp(this_char, MNTOPT_ALLOCSIZE)) {
275                         if (!value || !*value) {
276                                 xfs_warn(mp, "%s option requires an argument",
277                                         this_char);
278                                 return -EINVAL;
279                         }
280                         if (suffix_kstrtoint(value, 10, &iosize))
281                                 return -EINVAL;
282                         iosizelog = ffs(iosize) - 1;
283                 } else if (!strcmp(this_char, MNTOPT_GRPID) ||
284                            !strcmp(this_char, MNTOPT_BSDGROUPS)) {
285                         mp->m_flags |= XFS_MOUNT_GRPID;
286                 } else if (!strcmp(this_char, MNTOPT_NOGRPID) ||
287                            !strcmp(this_char, MNTOPT_SYSVGROUPS)) {
288                         mp->m_flags &= ~XFS_MOUNT_GRPID;
289                 } else if (!strcmp(this_char, MNTOPT_WSYNC)) {
290                         mp->m_flags |= XFS_MOUNT_WSYNC;
291                 } else if (!strcmp(this_char, MNTOPT_NORECOVERY)) {
292                         mp->m_flags |= XFS_MOUNT_NORECOVERY;
293                 } else if (!strcmp(this_char, MNTOPT_NOALIGN)) {
294                         mp->m_flags |= XFS_MOUNT_NOALIGN;
295                 } else if (!strcmp(this_char, MNTOPT_SWALLOC)) {
296                         mp->m_flags |= XFS_MOUNT_SWALLOC;
297                 } else if (!strcmp(this_char, MNTOPT_SUNIT)) {
298                         if (!value || !*value) {
299                                 xfs_warn(mp, "%s option requires an argument",
300                                         this_char);
301                                 return -EINVAL;
302                         }
303                         if (kstrtoint(value, 10, &dsunit))
304                                 return -EINVAL;
305                 } else if (!strcmp(this_char, MNTOPT_SWIDTH)) {
306                         if (!value || !*value) {
307                                 xfs_warn(mp, "%s option requires an argument",
308                                         this_char);
309                                 return -EINVAL;
310                         }
311                         if (kstrtoint(value, 10, &dswidth))
312                                 return -EINVAL;
313                 } else if (!strcmp(this_char, MNTOPT_32BITINODE)) {
314                         mp->m_flags |= XFS_MOUNT_SMALL_INUMS;
315                 } else if (!strcmp(this_char, MNTOPT_64BITINODE)) {
316                         mp->m_flags &= ~XFS_MOUNT_SMALL_INUMS;
317                 } else if (!strcmp(this_char, MNTOPT_NOUUID)) {
318                         mp->m_flags |= XFS_MOUNT_NOUUID;
319                 } else if (!strcmp(this_char, MNTOPT_BARRIER)) {
320                         mp->m_flags |= XFS_MOUNT_BARRIER;
321                 } else if (!strcmp(this_char, MNTOPT_NOBARRIER)) {
322                         mp->m_flags &= ~XFS_MOUNT_BARRIER;
323                 } else if (!strcmp(this_char, MNTOPT_IKEEP)) {
324                         mp->m_flags |= XFS_MOUNT_IKEEP;
325                 } else if (!strcmp(this_char, MNTOPT_NOIKEEP)) {
326                         mp->m_flags &= ~XFS_MOUNT_IKEEP;
327                 } else if (!strcmp(this_char, MNTOPT_LARGEIO)) {
328                         mp->m_flags &= ~XFS_MOUNT_COMPAT_IOSIZE;
329                 } else if (!strcmp(this_char, MNTOPT_NOLARGEIO)) {
330                         mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
331                 } else if (!strcmp(this_char, MNTOPT_ATTR2)) {
332                         mp->m_flags |= XFS_MOUNT_ATTR2;
333                 } else if (!strcmp(this_char, MNTOPT_NOATTR2)) {
334                         mp->m_flags &= ~XFS_MOUNT_ATTR2;
335                         mp->m_flags |= XFS_MOUNT_NOATTR2;
336                 } else if (!strcmp(this_char, MNTOPT_FILESTREAM)) {
337                         mp->m_flags |= XFS_MOUNT_FILESTREAMS;
338                 } else if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
339                         mp->m_qflags &= ~XFS_ALL_QUOTA_ACCT;
340                         mp->m_qflags &= ~XFS_ALL_QUOTA_ENFD;
341                         mp->m_qflags &= ~XFS_ALL_QUOTA_ACTIVE;
342                 } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
343                            !strcmp(this_char, MNTOPT_UQUOTA) ||
344                            !strcmp(this_char, MNTOPT_USRQUOTA)) {
345                         mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE |
346                                          XFS_UQUOTA_ENFD);
347                 } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
348                            !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
349                         mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
350                         mp->m_qflags &= ~XFS_UQUOTA_ENFD;
351                 } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
352                            !strcmp(this_char, MNTOPT_PRJQUOTA)) {
353                         mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE |
354                                          XFS_PQUOTA_ENFD);
355                 } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
356                         mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
357                         mp->m_qflags &= ~XFS_PQUOTA_ENFD;
358                 } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
359                            !strcmp(this_char, MNTOPT_GRPQUOTA)) {
360                         mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE |
361                                          XFS_GQUOTA_ENFD);
362                 } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
363                         mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
364                         mp->m_qflags &= ~XFS_GQUOTA_ENFD;
365                 } else if (!strcmp(this_char, MNTOPT_DELAYLOG)) {
366                         xfs_warn(mp,
367         "delaylog is the default now, option is deprecated.");
368                 } else if (!strcmp(this_char, MNTOPT_NODELAYLOG)) {
369                         xfs_warn(mp,
370         "nodelaylog support has been removed, option is deprecated.");
371                 } else if (!strcmp(this_char, MNTOPT_DISCARD)) {
372                         mp->m_flags |= XFS_MOUNT_DISCARD;
373                 } else if (!strcmp(this_char, MNTOPT_NODISCARD)) {
374                         mp->m_flags &= ~XFS_MOUNT_DISCARD;
375                 } else if (!strcmp(this_char, "ihashsize")) {
376                         xfs_warn(mp,
377         "ihashsize no longer used, option is deprecated.");
378                 } else if (!strcmp(this_char, "osyncisdsync")) {
379                         xfs_warn(mp,
380         "osyncisdsync has no effect, option is deprecated.");
381                 } else if (!strcmp(this_char, "osyncisosync")) {
382                         xfs_warn(mp,
383         "osyncisosync has no effect, option is deprecated.");
384                 } else if (!strcmp(this_char, "irixsgid")) {
385                         xfs_warn(mp,
386         "irixsgid is now a sysctl(2) variable, option is deprecated.");
387                 } else {
388                         xfs_warn(mp, "unknown mount option [%s].", this_char);
389                         return -EINVAL;
390                 }
391         }
392
393         /*
394          * no recovery flag requires a read-only mount
395          */
396         if ((mp->m_flags & XFS_MOUNT_NORECOVERY) &&
397             !(mp->m_flags & XFS_MOUNT_RDONLY)) {
398                 xfs_warn(mp, "no-recovery mounts must be read-only.");
399                 return -EINVAL;
400         }
401
402         if ((mp->m_flags & XFS_MOUNT_NOALIGN) && (dsunit || dswidth)) {
403                 xfs_warn(mp,
404         "sunit and swidth options incompatible with the noalign option");
405                 return -EINVAL;
406         }
407
408 #ifndef CONFIG_XFS_QUOTA
409         if (XFS_IS_QUOTA_RUNNING(mp)) {
410                 xfs_warn(mp, "quota support not available in this kernel.");
411                 return -EINVAL;
412         }
413 #endif
414
415         if ((dsunit && !dswidth) || (!dsunit && dswidth)) {
416                 xfs_warn(mp, "sunit and swidth must be specified together");
417                 return -EINVAL;
418         }
419
420         if (dsunit && (dswidth % dsunit != 0)) {
421                 xfs_warn(mp,
422         "stripe width (%d) must be a multiple of the stripe unit (%d)",
423                         dswidth, dsunit);
424                 return -EINVAL;
425         }
426
427 done:
428         if (dsunit && !(mp->m_flags & XFS_MOUNT_NOALIGN)) {
429                 /*
430                  * At this point the superblock has not been read
431                  * in, therefore we do not know the block size.
432                  * Before the mount call ends we will convert
433                  * these to FSBs.
434                  */
435                 mp->m_dalign = dsunit;
436                 mp->m_swidth = dswidth;
437         }
438
439         if (mp->m_logbufs != -1 &&
440             mp->m_logbufs != 0 &&
441             (mp->m_logbufs < XLOG_MIN_ICLOGS ||
442              mp->m_logbufs > XLOG_MAX_ICLOGS)) {
443                 xfs_warn(mp, "invalid logbufs value: %d [not %d-%d]",
444                         mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
445                 return -EINVAL;
446         }
447         if (mp->m_logbsize != -1 &&
448             mp->m_logbsize !=  0 &&
449             (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
450              mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
451              !is_power_of_2(mp->m_logbsize))) {
452                 xfs_warn(mp,
453                         "invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
454                         mp->m_logbsize);
455                 return -EINVAL;
456         }
457
458         if (iosizelog) {
459                 if (iosizelog > XFS_MAX_IO_LOG ||
460                     iosizelog < XFS_MIN_IO_LOG) {
461                         xfs_warn(mp, "invalid log iosize: %d [not %d-%d]",
462                                 iosizelog, XFS_MIN_IO_LOG,
463                                 XFS_MAX_IO_LOG);
464                         return -EINVAL;
465                 }
466
467                 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
468                 mp->m_readio_log = iosizelog;
469                 mp->m_writeio_log = iosizelog;
470         }
471
472         return 0;
473 }
474
475 struct proc_xfs_info {
476         int     flag;
477         char    *str;
478 };
479
480 STATIC int
481 xfs_showargs(
482         struct xfs_mount        *mp,
483         struct seq_file         *m)
484 {
485         static struct proc_xfs_info xfs_info_set[] = {
486                 /* the few simple ones we can get from the mount struct */
487                 { XFS_MOUNT_IKEEP,              "," MNTOPT_IKEEP },
488                 { XFS_MOUNT_WSYNC,              "," MNTOPT_WSYNC },
489                 { XFS_MOUNT_NOALIGN,            "," MNTOPT_NOALIGN },
490                 { XFS_MOUNT_SWALLOC,            "," MNTOPT_SWALLOC },
491                 { XFS_MOUNT_NOUUID,             "," MNTOPT_NOUUID },
492                 { XFS_MOUNT_NORECOVERY,         "," MNTOPT_NORECOVERY },
493                 { XFS_MOUNT_ATTR2,              "," MNTOPT_ATTR2 },
494                 { XFS_MOUNT_FILESTREAMS,        "," MNTOPT_FILESTREAM },
495                 { XFS_MOUNT_GRPID,              "," MNTOPT_GRPID },
496                 { XFS_MOUNT_DISCARD,            "," MNTOPT_DISCARD },
497                 { XFS_MOUNT_SMALL_INUMS,        "," MNTOPT_32BITINODE },
498                 { 0, NULL }
499         };
500         static struct proc_xfs_info xfs_info_unset[] = {
501                 /* the few simple ones we can get from the mount struct */
502                 { XFS_MOUNT_COMPAT_IOSIZE,      "," MNTOPT_LARGEIO },
503                 { XFS_MOUNT_BARRIER,            "," MNTOPT_NOBARRIER },
504                 { XFS_MOUNT_SMALL_INUMS,        "," MNTOPT_64BITINODE },
505                 { 0, NULL }
506         };
507         struct proc_xfs_info    *xfs_infop;
508
509         for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
510                 if (mp->m_flags & xfs_infop->flag)
511                         seq_puts(m, xfs_infop->str);
512         }
513         for (xfs_infop = xfs_info_unset; xfs_infop->flag; xfs_infop++) {
514                 if (!(mp->m_flags & xfs_infop->flag))
515                         seq_puts(m, xfs_infop->str);
516         }
517
518         if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
519                 seq_printf(m, "," MNTOPT_ALLOCSIZE "=%dk",
520                                 (int)(1 << mp->m_writeio_log) >> 10);
521
522         if (mp->m_logbufs > 0)
523                 seq_printf(m, "," MNTOPT_LOGBUFS "=%d", mp->m_logbufs);
524         if (mp->m_logbsize > 0)
525                 seq_printf(m, "," MNTOPT_LOGBSIZE "=%dk", mp->m_logbsize >> 10);
526
527         if (mp->m_logname)
528                 seq_printf(m, "," MNTOPT_LOGDEV "=%s", mp->m_logname);
529         if (mp->m_rtname)
530                 seq_printf(m, "," MNTOPT_RTDEV "=%s", mp->m_rtname);
531
532         if (mp->m_dalign > 0)
533                 seq_printf(m, "," MNTOPT_SUNIT "=%d",
534                                 (int)XFS_FSB_TO_BB(mp, mp->m_dalign));
535         if (mp->m_swidth > 0)
536                 seq_printf(m, "," MNTOPT_SWIDTH "=%d",
537                                 (int)XFS_FSB_TO_BB(mp, mp->m_swidth));
538
539         if (mp->m_qflags & (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD))
540                 seq_puts(m, "," MNTOPT_USRQUOTA);
541         else if (mp->m_qflags & XFS_UQUOTA_ACCT)
542                 seq_puts(m, "," MNTOPT_UQUOTANOENF);
543
544         if (mp->m_qflags & XFS_PQUOTA_ACCT) {
545                 if (mp->m_qflags & XFS_PQUOTA_ENFD)
546                         seq_puts(m, "," MNTOPT_PRJQUOTA);
547                 else
548                         seq_puts(m, "," MNTOPT_PQUOTANOENF);
549         }
550         if (mp->m_qflags & XFS_GQUOTA_ACCT) {
551                 if (mp->m_qflags & XFS_GQUOTA_ENFD)
552                         seq_puts(m, "," MNTOPT_GRPQUOTA);
553                 else
554                         seq_puts(m, "," MNTOPT_GQUOTANOENF);
555         }
556
557         if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
558                 seq_puts(m, "," MNTOPT_NOQUOTA);
559
560         return 0;
561 }
562 __uint64_t
563 xfs_max_file_offset(
564         unsigned int            blockshift)
565 {
566         unsigned int            pagefactor = 1;
567         unsigned int            bitshift = BITS_PER_LONG - 1;
568
569         /* Figure out maximum filesize, on Linux this can depend on
570          * the filesystem blocksize (on 32 bit platforms).
571          * __block_write_begin does this in an [unsigned] long...
572          *      page->index << (PAGE_CACHE_SHIFT - bbits)
573          * So, for page sized blocks (4K on 32 bit platforms),
574          * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
575          *      (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
576          * but for smaller blocksizes it is less (bbits = log2 bsize).
577          * Note1: get_block_t takes a long (implicit cast from above)
578          * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
579          * can optionally convert the [unsigned] long from above into
580          * an [unsigned] long long.
581          */
582
583 #if BITS_PER_LONG == 32
584 # if defined(CONFIG_LBDAF)
585         ASSERT(sizeof(sector_t) == 8);
586         pagefactor = PAGE_CACHE_SIZE;
587         bitshift = BITS_PER_LONG;
588 # else
589         pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
590 # endif
591 #endif
592
593         return (((__uint64_t)pagefactor) << bitshift) - 1;
594 }
595
596 /*
597  * xfs_set_inode32() and xfs_set_inode64() are passed an agcount
598  * because in the growfs case, mp->m_sb.sb_agcount is not updated
599  * yet to the potentially higher ag count.
600  */
601 xfs_agnumber_t
602 xfs_set_inode32(struct xfs_mount *mp, xfs_agnumber_t agcount)
603 {
604         xfs_agnumber_t  index = 0;
605         xfs_agnumber_t  maxagi = 0;
606         xfs_sb_t        *sbp = &mp->m_sb;
607         xfs_agnumber_t  max_metadata;
608         xfs_agino_t     agino;
609         xfs_ino_t       ino;
610         xfs_perag_t     *pag;
611
612         /* Calculate how much should be reserved for inodes to meet
613          * the max inode percentage.
614          */
615         if (mp->m_maxicount) {
616                 __uint64_t      icount;
617
618                 icount = sbp->sb_dblocks * sbp->sb_imax_pct;
619                 do_div(icount, 100);
620                 icount += sbp->sb_agblocks - 1;
621                 do_div(icount, sbp->sb_agblocks);
622                 max_metadata = icount;
623         } else {
624                 max_metadata = agcount;
625         }
626
627         agino = XFS_OFFBNO_TO_AGINO(mp, sbp->sb_agblocks - 1, 0);
628
629         for (index = 0; index < agcount; index++) {
630                 ino = XFS_AGINO_TO_INO(mp, index, agino);
631
632                 if (ino > XFS_MAXINUMBER_32) {
633                         pag = xfs_perag_get(mp, index);
634                         pag->pagi_inodeok = 0;
635                         pag->pagf_metadata = 0;
636                         xfs_perag_put(pag);
637                         continue;
638                 }
639
640                 pag = xfs_perag_get(mp, index);
641                 pag->pagi_inodeok = 1;
642                 maxagi++;
643                 if (index < max_metadata)
644                         pag->pagf_metadata = 1;
645                 xfs_perag_put(pag);
646         }
647         mp->m_flags |= (XFS_MOUNT_32BITINODES |
648                         XFS_MOUNT_SMALL_INUMS);
649
650         return maxagi;
651 }
652
653 xfs_agnumber_t
654 xfs_set_inode64(struct xfs_mount *mp, xfs_agnumber_t agcount)
655 {
656         xfs_agnumber_t index = 0;
657
658         for (index = 0; index < agcount; index++) {
659                 struct xfs_perag        *pag;
660
661                 pag = xfs_perag_get(mp, index);
662                 pag->pagi_inodeok = 1;
663                 pag->pagf_metadata = 0;
664                 xfs_perag_put(pag);
665         }
666
667         /* There is no need for lock protection on m_flags,
668          * the rw_semaphore of the VFS superblock is locked
669          * during mount/umount/remount operations, so this is
670          * enough to avoid concurency on the m_flags field
671          */
672         mp->m_flags &= ~(XFS_MOUNT_32BITINODES |
673                          XFS_MOUNT_SMALL_INUMS);
674         return index;
675 }
676
677 STATIC int
678 xfs_blkdev_get(
679         xfs_mount_t             *mp,
680         const char              *name,
681         struct block_device     **bdevp)
682 {
683         int                     error = 0;
684
685         *bdevp = blkdev_get_by_path(name, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
686                                     mp);
687         if (IS_ERR(*bdevp)) {
688                 error = PTR_ERR(*bdevp);
689                 xfs_warn(mp, "Invalid device [%s], error=%d\n", name, error);
690         }
691
692         return error;
693 }
694
695 STATIC void
696 xfs_blkdev_put(
697         struct block_device     *bdev)
698 {
699         if (bdev)
700                 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
701 }
702
703 void
704 xfs_blkdev_issue_flush(
705         xfs_buftarg_t           *buftarg)
706 {
707         blkdev_issue_flush(buftarg->bt_bdev, GFP_NOFS, NULL);
708 }
709
710 STATIC void
711 xfs_close_devices(
712         struct xfs_mount        *mp)
713 {
714         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
715                 struct block_device *logdev = mp->m_logdev_targp->bt_bdev;
716                 xfs_free_buftarg(mp, mp->m_logdev_targp);
717                 xfs_blkdev_put(logdev);
718         }
719         if (mp->m_rtdev_targp) {
720                 struct block_device *rtdev = mp->m_rtdev_targp->bt_bdev;
721                 xfs_free_buftarg(mp, mp->m_rtdev_targp);
722                 xfs_blkdev_put(rtdev);
723         }
724         xfs_free_buftarg(mp, mp->m_ddev_targp);
725 }
726
727 /*
728  * The file system configurations are:
729  *      (1) device (partition) with data and internal log
730  *      (2) logical volume with data and log subvolumes.
731  *      (3) logical volume with data, log, and realtime subvolumes.
732  *
733  * We only have to handle opening the log and realtime volumes here if
734  * they are present.  The data subvolume has already been opened by
735  * get_sb_bdev() and is stored in sb->s_bdev.
736  */
737 STATIC int
738 xfs_open_devices(
739         struct xfs_mount        *mp)
740 {
741         struct block_device     *ddev = mp->m_super->s_bdev;
742         struct block_device     *logdev = NULL, *rtdev = NULL;
743         int                     error;
744
745         /*
746          * Open real time and log devices - order is important.
747          */
748         if (mp->m_logname) {
749                 error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
750                 if (error)
751                         goto out;
752         }
753
754         if (mp->m_rtname) {
755                 error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
756                 if (error)
757                         goto out_close_logdev;
758
759                 if (rtdev == ddev || rtdev == logdev) {
760                         xfs_warn(mp,
761         "Cannot mount filesystem with identical rtdev and ddev/logdev.");
762                         error = -EINVAL;
763                         goto out_close_rtdev;
764                 }
765         }
766
767         /*
768          * Setup xfs_mount buffer target pointers
769          */
770         error = -ENOMEM;
771         mp->m_ddev_targp = xfs_alloc_buftarg(mp, ddev);
772         if (!mp->m_ddev_targp)
773                 goto out_close_rtdev;
774
775         if (rtdev) {
776                 mp->m_rtdev_targp = xfs_alloc_buftarg(mp, rtdev);
777                 if (!mp->m_rtdev_targp)
778                         goto out_free_ddev_targ;
779         }
780
781         if (logdev && logdev != ddev) {
782                 mp->m_logdev_targp = xfs_alloc_buftarg(mp, logdev);
783                 if (!mp->m_logdev_targp)
784                         goto out_free_rtdev_targ;
785         } else {
786                 mp->m_logdev_targp = mp->m_ddev_targp;
787         }
788
789         return 0;
790
791  out_free_rtdev_targ:
792         if (mp->m_rtdev_targp)
793                 xfs_free_buftarg(mp, mp->m_rtdev_targp);
794  out_free_ddev_targ:
795         xfs_free_buftarg(mp, mp->m_ddev_targp);
796  out_close_rtdev:
797         if (rtdev)
798                 xfs_blkdev_put(rtdev);
799  out_close_logdev:
800         if (logdev && logdev != ddev)
801                 xfs_blkdev_put(logdev);
802  out:
803         return error;
804 }
805
806 /*
807  * Setup xfs_mount buffer target pointers based on superblock
808  */
809 STATIC int
810 xfs_setup_devices(
811         struct xfs_mount        *mp)
812 {
813         int                     error;
814
815         error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_sectsize);
816         if (error)
817                 return error;
818
819         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
820                 unsigned int    log_sector_size = BBSIZE;
821
822                 if (xfs_sb_version_hassector(&mp->m_sb))
823                         log_sector_size = mp->m_sb.sb_logsectsize;
824                 error = xfs_setsize_buftarg(mp->m_logdev_targp,
825                                             log_sector_size);
826                 if (error)
827                         return error;
828         }
829         if (mp->m_rtdev_targp) {
830                 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
831                                             mp->m_sb.sb_sectsize);
832                 if (error)
833                         return error;
834         }
835
836         return 0;
837 }
838
839 STATIC int
840 xfs_init_mount_workqueues(
841         struct xfs_mount        *mp)
842 {
843         mp->m_data_workqueue = alloc_workqueue("xfs-data/%s",
844                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_fsname);
845         if (!mp->m_data_workqueue)
846                 goto out;
847
848         mp->m_unwritten_workqueue = alloc_workqueue("xfs-conv/%s",
849                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_fsname);
850         if (!mp->m_unwritten_workqueue)
851                 goto out_destroy_data_iodone_queue;
852
853         mp->m_cil_workqueue = alloc_workqueue("xfs-cil/%s",
854                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0, mp->m_fsname);
855         if (!mp->m_cil_workqueue)
856                 goto out_destroy_unwritten;
857
858         mp->m_reclaim_workqueue = alloc_workqueue("xfs-reclaim/%s",
859                         WQ_FREEZABLE, 0, mp->m_fsname);
860         if (!mp->m_reclaim_workqueue)
861                 goto out_destroy_cil;
862
863         mp->m_log_workqueue = alloc_workqueue("xfs-log/%s",
864                         WQ_FREEZABLE, 0, mp->m_fsname);
865         if (!mp->m_log_workqueue)
866                 goto out_destroy_reclaim;
867
868         mp->m_eofblocks_workqueue = alloc_workqueue("xfs-eofblocks/%s",
869                         WQ_FREEZABLE, 0, mp->m_fsname);
870         if (!mp->m_eofblocks_workqueue)
871                 goto out_destroy_log;
872
873         return 0;
874
875 out_destroy_log:
876         destroy_workqueue(mp->m_log_workqueue);
877 out_destroy_reclaim:
878         destroy_workqueue(mp->m_reclaim_workqueue);
879 out_destroy_cil:
880         destroy_workqueue(mp->m_cil_workqueue);
881 out_destroy_unwritten:
882         destroy_workqueue(mp->m_unwritten_workqueue);
883 out_destroy_data_iodone_queue:
884         destroy_workqueue(mp->m_data_workqueue);
885 out:
886         return -ENOMEM;
887 }
888
889 STATIC void
890 xfs_destroy_mount_workqueues(
891         struct xfs_mount        *mp)
892 {
893         destroy_workqueue(mp->m_eofblocks_workqueue);
894         destroy_workqueue(mp->m_log_workqueue);
895         destroy_workqueue(mp->m_reclaim_workqueue);
896         destroy_workqueue(mp->m_cil_workqueue);
897         destroy_workqueue(mp->m_data_workqueue);
898         destroy_workqueue(mp->m_unwritten_workqueue);
899 }
900
901 /*
902  * Flush all dirty data to disk. Must not be called while holding an XFS_ILOCK
903  * or a page lock. We use sync_inodes_sb() here to ensure we block while waiting
904  * for IO to complete so that we effectively throttle multiple callers to the
905  * rate at which IO is completing.
906  */
907 void
908 xfs_flush_inodes(
909         struct xfs_mount        *mp)
910 {
911         struct super_block      *sb = mp->m_super;
912
913         if (down_read_trylock(&sb->s_umount)) {
914                 sync_inodes_sb(sb);
915                 up_read(&sb->s_umount);
916         }
917 }
918
919 /* Catch misguided souls that try to use this interface on XFS */
920 STATIC struct inode *
921 xfs_fs_alloc_inode(
922         struct super_block      *sb)
923 {
924         BUG();
925         return NULL;
926 }
927
928 /*
929  * Now that the generic code is guaranteed not to be accessing
930  * the linux inode, we can reclaim the inode.
931  */
932 STATIC void
933 xfs_fs_destroy_inode(
934         struct inode            *inode)
935 {
936         struct xfs_inode        *ip = XFS_I(inode);
937
938         trace_xfs_destroy_inode(ip);
939
940         XFS_STATS_INC(vn_reclaim);
941
942         ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
943
944         /*
945          * We should never get here with one of the reclaim flags already set.
946          */
947         ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
948         ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIM));
949
950         /*
951          * We always use background reclaim here because even if the
952          * inode is clean, it still may be under IO and hence we have
953          * to take the flush lock. The background reclaim path handles
954          * this more efficiently than we can here, so simply let background
955          * reclaim tear down all inodes.
956          */
957         xfs_inode_set_reclaim_tag(ip);
958 }
959
960 /*
961  * Slab object creation initialisation for the XFS inode.
962  * This covers only the idempotent fields in the XFS inode;
963  * all other fields need to be initialised on allocation
964  * from the slab. This avoids the need to repeatedly initialise
965  * fields in the xfs inode that left in the initialise state
966  * when freeing the inode.
967  */
968 STATIC void
969 xfs_fs_inode_init_once(
970         void                    *inode)
971 {
972         struct xfs_inode        *ip = inode;
973
974         memset(ip, 0, sizeof(struct xfs_inode));
975
976         /* vfs inode */
977         inode_init_once(VFS_I(ip));
978
979         /* xfs inode */
980         atomic_set(&ip->i_pincount, 0);
981         spin_lock_init(&ip->i_flags_lock);
982
983         mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
984                      "xfsino", ip->i_ino);
985 }
986
987 STATIC void
988 xfs_fs_evict_inode(
989         struct inode            *inode)
990 {
991         xfs_inode_t             *ip = XFS_I(inode);
992
993         ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
994
995         trace_xfs_evict_inode(ip);
996
997         truncate_inode_pages_final(&inode->i_data);
998         clear_inode(inode);
999         XFS_STATS_INC(vn_rele);
1000         XFS_STATS_INC(vn_remove);
1001         XFS_STATS_DEC(vn_active);
1002
1003         xfs_inactive(ip);
1004 }
1005
1006 /*
1007  * We do an unlocked check for XFS_IDONTCACHE here because we are already
1008  * serialised against cache hits here via the inode->i_lock and igrab() in
1009  * xfs_iget_cache_hit(). Hence a lookup that might clear this flag will not be
1010  * racing with us, and it avoids needing to grab a spinlock here for every inode
1011  * we drop the final reference on.
1012  */
1013 STATIC int
1014 xfs_fs_drop_inode(
1015         struct inode            *inode)
1016 {
1017         struct xfs_inode        *ip = XFS_I(inode);
1018
1019         return generic_drop_inode(inode) || (ip->i_flags & XFS_IDONTCACHE);
1020 }
1021
1022 STATIC void
1023 xfs_free_fsname(
1024         struct xfs_mount        *mp)
1025 {
1026         kfree(mp->m_fsname);
1027         kfree(mp->m_rtname);
1028         kfree(mp->m_logname);
1029 }
1030
1031 STATIC void
1032 xfs_fs_put_super(
1033         struct super_block      *sb)
1034 {
1035         struct xfs_mount        *mp = XFS_M(sb);
1036
1037         xfs_filestream_unmount(mp);
1038         xfs_unmountfs(mp);
1039
1040         xfs_freesb(mp);
1041         xfs_icsb_destroy_counters(mp);
1042         xfs_destroy_mount_workqueues(mp);
1043         xfs_close_devices(mp);
1044         xfs_free_fsname(mp);
1045         kfree(mp);
1046 }
1047
1048 STATIC int
1049 xfs_fs_sync_fs(
1050         struct super_block      *sb,
1051         int                     wait)
1052 {
1053         struct xfs_mount        *mp = XFS_M(sb);
1054
1055         /*
1056          * Doing anything during the async pass would be counterproductive.
1057          */
1058         if (!wait)
1059                 return 0;
1060
1061         xfs_log_force(mp, XFS_LOG_SYNC);
1062         if (laptop_mode) {
1063                 /*
1064                  * The disk must be active because we're syncing.
1065                  * We schedule log work now (now that the disk is
1066                  * active) instead of later (when it might not be).
1067                  */
1068                 flush_delayed_work(&mp->m_log->l_work);
1069         }
1070
1071         return 0;
1072 }
1073
1074 STATIC int
1075 xfs_fs_statfs(
1076         struct dentry           *dentry,
1077         struct kstatfs          *statp)
1078 {
1079         struct xfs_mount        *mp = XFS_M(dentry->d_sb);
1080         xfs_sb_t                *sbp = &mp->m_sb;
1081         struct xfs_inode        *ip = XFS_I(dentry->d_inode);
1082         __uint64_t              fakeinos, id;
1083         xfs_extlen_t            lsize;
1084         __int64_t               ffree;
1085
1086         statp->f_type = XFS_SB_MAGIC;
1087         statp->f_namelen = MAXNAMELEN - 1;
1088
1089         id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
1090         statp->f_fsid.val[0] = (u32)id;
1091         statp->f_fsid.val[1] = (u32)(id >> 32);
1092
1093         xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
1094
1095         spin_lock(&mp->m_sb_lock);
1096         statp->f_bsize = sbp->sb_blocksize;
1097         lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
1098         statp->f_blocks = sbp->sb_dblocks - lsize;
1099         statp->f_bfree = statp->f_bavail =
1100                                 sbp->sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1101         fakeinos = statp->f_bfree << sbp->sb_inopblog;
1102         statp->f_files =
1103             MIN(sbp->sb_icount + fakeinos, (__uint64_t)XFS_MAXINUMBER);
1104         if (mp->m_maxicount)
1105                 statp->f_files = min_t(typeof(statp->f_files),
1106                                         statp->f_files,
1107                                         mp->m_maxicount);
1108
1109         /* make sure statp->f_ffree does not underflow */
1110         ffree = statp->f_files - (sbp->sb_icount - sbp->sb_ifree);
1111         statp->f_ffree = max_t(__int64_t, ffree, 0);
1112
1113         spin_unlock(&mp->m_sb_lock);
1114
1115         if ((ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1116             ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))) ==
1117                               (XFS_PQUOTA_ACCT|XFS_PQUOTA_ENFD))
1118                 xfs_qm_statvfs(ip, statp);
1119         return 0;
1120 }
1121
1122 STATIC void
1123 xfs_save_resvblks(struct xfs_mount *mp)
1124 {
1125         __uint64_t resblks = 0;
1126
1127         mp->m_resblks_save = mp->m_resblks;
1128         xfs_reserve_blocks(mp, &resblks, NULL);
1129 }
1130
1131 STATIC void
1132 xfs_restore_resvblks(struct xfs_mount *mp)
1133 {
1134         __uint64_t resblks;
1135
1136         if (mp->m_resblks_save) {
1137                 resblks = mp->m_resblks_save;
1138                 mp->m_resblks_save = 0;
1139         } else
1140                 resblks = xfs_default_resblks(mp);
1141
1142         xfs_reserve_blocks(mp, &resblks, NULL);
1143 }
1144
1145 /*
1146  * Trigger writeback of all the dirty metadata in the file system.
1147  *
1148  * This ensures that the metadata is written to their location on disk rather
1149  * than just existing in transactions in the log. This means after a quiesce
1150  * there is no log replay required to write the inodes to disk - this is the
1151  * primary difference between a sync and a quiesce.
1152  *
1153  * Note: xfs_log_quiesce() stops background log work - the callers must ensure
1154  * it is started again when appropriate.
1155  */
1156 static void
1157 xfs_quiesce_attr(
1158         struct xfs_mount        *mp)
1159 {
1160         int     error = 0;
1161
1162         /* wait for all modifications to complete */
1163         while (atomic_read(&mp->m_active_trans) > 0)
1164                 delay(100);
1165
1166         /* force the log to unpin objects from the now complete transactions */
1167         xfs_log_force(mp, XFS_LOG_SYNC);
1168
1169         /* reclaim inodes to do any IO before the freeze completes */
1170         xfs_reclaim_inodes(mp, 0);
1171         xfs_reclaim_inodes(mp, SYNC_WAIT);
1172
1173         /* Push the superblock and write an unmount record */
1174         error = xfs_log_sbcount(mp);
1175         if (error)
1176                 xfs_warn(mp, "xfs_attr_quiesce: failed to log sb changes. "
1177                                 "Frozen image may not be consistent.");
1178         /*
1179          * Just warn here till VFS can correctly support
1180          * read-only remount without racing.
1181          */
1182         WARN_ON(atomic_read(&mp->m_active_trans) != 0);
1183
1184         xfs_log_quiesce(mp);
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         sync_filesystem(sb);
1200         while ((p = strsep(&options, ",")) != NULL) {
1201                 int token;
1202
1203                 if (!*p)
1204                         continue;
1205
1206                 token = match_token(p, tokens, args);
1207                 switch (token) {
1208                 case Opt_barrier:
1209                         mp->m_flags |= XFS_MOUNT_BARRIER;
1210                         break;
1211                 case Opt_nobarrier:
1212                         mp->m_flags &= ~XFS_MOUNT_BARRIER;
1213                         break;
1214                 case Opt_inode64:
1215                         mp->m_maxagi = xfs_set_inode64(mp, sbp->sb_agcount);
1216                         break;
1217                 case Opt_inode32:
1218                         mp->m_maxagi = xfs_set_inode32(mp, sbp->sb_agcount);
1219                         break;
1220                 default:
1221                         /*
1222                          * Logically we would return an error here to prevent
1223                          * users from believing they might have changed
1224                          * mount options using remount which can't be changed.
1225                          *
1226                          * But unfortunately mount(8) adds all options from
1227                          * mtab and fstab to the mount arguments in some cases
1228                          * so we can't blindly reject options, but have to
1229                          * check for each specified option if it actually
1230                          * differs from the currently set option and only
1231                          * reject it if that's the case.
1232                          *
1233                          * Until that is implemented we return success for
1234                          * every remount request, and silently ignore all
1235                          * options that we can't actually change.
1236                          */
1237 #if 0
1238                         xfs_info(mp,
1239                 "mount option \"%s\" not supported for remount", p);
1240                         return -EINVAL;
1241 #else
1242                         break;
1243 #endif
1244                 }
1245         }
1246
1247         /* ro -> rw */
1248         if ((mp->m_flags & XFS_MOUNT_RDONLY) && !(*flags & MS_RDONLY)) {
1249                 mp->m_flags &= ~XFS_MOUNT_RDONLY;
1250
1251                 /*
1252                  * If this is the first remount to writeable state we
1253                  * might have some superblock changes to update.
1254                  */
1255                 if (mp->m_update_flags) {
1256                         error = xfs_mount_log_sb(mp, mp->m_update_flags);
1257                         if (error) {
1258                                 xfs_warn(mp, "failed to write sb changes");
1259                                 return error;
1260                         }
1261                         mp->m_update_flags = 0;
1262                 }
1263
1264                 /*
1265                  * Fill out the reserve pool if it is empty. Use the stashed
1266                  * value if it is non-zero, otherwise go with the default.
1267                  */
1268                 xfs_restore_resvblks(mp);
1269                 xfs_log_work_queue(mp);
1270         }
1271
1272         /* rw -> ro */
1273         if (!(mp->m_flags & XFS_MOUNT_RDONLY) && (*flags & MS_RDONLY)) {
1274                 /*
1275                  * Before we sync the metadata, we need to free up the reserve
1276                  * block pool so that the used block count in the superblock on
1277                  * disk is correct at the end of the remount. Stash the current
1278                  * reserve pool size so that if we get remounted rw, we can
1279                  * return it to the same size.
1280                  */
1281                 xfs_save_resvblks(mp);
1282                 xfs_quiesce_attr(mp);
1283                 mp->m_flags |= XFS_MOUNT_RDONLY;
1284         }
1285
1286         return 0;
1287 }
1288
1289 /*
1290  * Second stage of a freeze. The data is already frozen so we only
1291  * need to take care of the metadata. Once that's done write a dummy
1292  * record to dirty the log in case of a crash while frozen.
1293  */
1294 STATIC int
1295 xfs_fs_freeze(
1296         struct super_block      *sb)
1297 {
1298         struct xfs_mount        *mp = XFS_M(sb);
1299
1300         xfs_save_resvblks(mp);
1301         xfs_quiesce_attr(mp);
1302         return xfs_fs_log_dummy(mp);
1303 }
1304
1305 STATIC int
1306 xfs_fs_unfreeze(
1307         struct super_block      *sb)
1308 {
1309         struct xfs_mount        *mp = XFS_M(sb);
1310
1311         xfs_restore_resvblks(mp);
1312         xfs_log_work_queue(mp);
1313         return 0;
1314 }
1315
1316 STATIC int
1317 xfs_fs_show_options(
1318         struct seq_file         *m,
1319         struct dentry           *root)
1320 {
1321         return xfs_showargs(XFS_M(root->d_sb), m);
1322 }
1323
1324 /*
1325  * This function fills in xfs_mount_t fields based on mount args.
1326  * Note: the superblock _has_ now been read in.
1327  */
1328 STATIC int
1329 xfs_finish_flags(
1330         struct xfs_mount        *mp)
1331 {
1332         int                     ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
1333
1334         /* Fail a mount where the logbuf is smaller than the log stripe */
1335         if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1336                 if (mp->m_logbsize <= 0 &&
1337                     mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
1338                         mp->m_logbsize = mp->m_sb.sb_logsunit;
1339                 } else if (mp->m_logbsize > 0 &&
1340                            mp->m_logbsize < mp->m_sb.sb_logsunit) {
1341                         xfs_warn(mp,
1342                 "logbuf size must be greater than or equal to log stripe size");
1343                         return -EINVAL;
1344                 }
1345         } else {
1346                 /* Fail a mount if the logbuf is larger than 32K */
1347                 if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
1348                         xfs_warn(mp,
1349                 "logbuf size for version 1 logs must be 16K or 32K");
1350                         return -EINVAL;
1351                 }
1352         }
1353
1354         /*
1355          * V5 filesystems always use attr2 format for attributes.
1356          */
1357         if (xfs_sb_version_hascrc(&mp->m_sb) &&
1358             (mp->m_flags & XFS_MOUNT_NOATTR2)) {
1359                 xfs_warn(mp,
1360 "Cannot mount a V5 filesystem as %s. %s is always enabled for V5 filesystems.",
1361                         MNTOPT_NOATTR2, MNTOPT_ATTR2);
1362                 return -EINVAL;
1363         }
1364
1365         /*
1366          * mkfs'ed attr2 will turn on attr2 mount unless explicitly
1367          * told by noattr2 to turn it off
1368          */
1369         if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1370             !(mp->m_flags & XFS_MOUNT_NOATTR2))
1371                 mp->m_flags |= XFS_MOUNT_ATTR2;
1372
1373         /*
1374          * prohibit r/w mounts of read-only filesystems
1375          */
1376         if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
1377                 xfs_warn(mp,
1378                         "cannot mount a read-only filesystem as read-write");
1379                 return -EROFS;
1380         }
1381
1382         if ((mp->m_qflags & (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE)) &&
1383             (mp->m_qflags & (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE)) &&
1384             !xfs_sb_version_has_pquotino(&mp->m_sb)) {
1385                 xfs_warn(mp,
1386                   "Super block does not support project and group quota together");
1387                 return -EINVAL;
1388         }
1389
1390         return 0;
1391 }
1392
1393 STATIC int
1394 xfs_fs_fill_super(
1395         struct super_block      *sb,
1396         void                    *data,
1397         int                     silent)
1398 {
1399         struct inode            *root;
1400         struct xfs_mount        *mp = NULL;
1401         int                     flags = 0, error = -ENOMEM;
1402
1403         mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL);
1404         if (!mp)
1405                 goto out;
1406
1407         spin_lock_init(&mp->m_sb_lock);
1408         mutex_init(&mp->m_growlock);
1409         atomic_set(&mp->m_active_trans, 0);
1410         INIT_DELAYED_WORK(&mp->m_reclaim_work, xfs_reclaim_worker);
1411         INIT_DELAYED_WORK(&mp->m_eofblocks_work, xfs_eofblocks_worker);
1412         mp->m_kobj.kobject.kset = xfs_kset;
1413
1414         mp->m_super = sb;
1415         sb->s_fs_info = mp;
1416
1417         error = xfs_parseargs(mp, (char *)data);
1418         if (error)
1419                 goto out_free_fsname;
1420
1421         sb_min_blocksize(sb, BBSIZE);
1422         sb->s_xattr = xfs_xattr_handlers;
1423         sb->s_export_op = &xfs_export_operations;
1424 #ifdef CONFIG_XFS_QUOTA
1425         sb->s_qcop = &xfs_quotactl_operations;
1426 #endif
1427         sb->s_op = &xfs_super_operations;
1428
1429         if (silent)
1430                 flags |= XFS_MFSI_QUIET;
1431
1432         error = xfs_open_devices(mp);
1433         if (error)
1434                 goto out_free_fsname;
1435
1436         error = xfs_init_mount_workqueues(mp);
1437         if (error)
1438                 goto out_close_devices;
1439
1440         error = xfs_icsb_init_counters(mp);
1441         if (error)
1442                 goto out_destroy_workqueues;
1443
1444         error = xfs_readsb(mp, flags);
1445         if (error)
1446                 goto out_destroy_counters;
1447
1448         error = xfs_finish_flags(mp);
1449         if (error)
1450                 goto out_free_sb;
1451
1452         error = xfs_setup_devices(mp);
1453         if (error)
1454                 goto out_free_sb;
1455
1456         error = xfs_filestream_mount(mp);
1457         if (error)
1458                 goto out_free_sb;
1459
1460         /*
1461          * we must configure the block size in the superblock before we run the
1462          * full mount process as the mount process can lookup and cache inodes.
1463          */
1464         sb->s_magic = XFS_SB_MAGIC;
1465         sb->s_blocksize = mp->m_sb.sb_blocksize;
1466         sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1467         sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
1468         sb->s_max_links = XFS_MAXLINK;
1469         sb->s_time_gran = 1;
1470         set_posix_acl_flag(sb);
1471
1472         /* version 5 superblocks support inode version counters. */
1473         if (XFS_SB_VERSION_NUM(&mp->m_sb) == XFS_SB_VERSION_5)
1474                 sb->s_flags |= MS_I_VERSION;
1475
1476         error = xfs_mountfs(mp);
1477         if (error)
1478                 goto out_filestream_unmount;
1479
1480         root = igrab(VFS_I(mp->m_rootip));
1481         if (!root) {
1482                 error = -ENOENT;
1483                 goto out_unmount;
1484         }
1485         sb->s_root = d_make_root(root);
1486         if (!sb->s_root) {
1487                 error = -ENOMEM;
1488                 goto out_unmount;
1489         }
1490
1491         return 0;
1492
1493  out_filestream_unmount:
1494         xfs_filestream_unmount(mp);
1495  out_free_sb:
1496         xfs_freesb(mp);
1497  out_destroy_counters:
1498         xfs_icsb_destroy_counters(mp);
1499 out_destroy_workqueues:
1500         xfs_destroy_mount_workqueues(mp);
1501  out_close_devices:
1502         xfs_close_devices(mp);
1503  out_free_fsname:
1504         xfs_free_fsname(mp);
1505         kfree(mp);
1506  out:
1507         return error;
1508
1509  out_unmount:
1510         xfs_filestream_unmount(mp);
1511         xfs_unmountfs(mp);
1512         goto out_free_sb;
1513 }
1514
1515 STATIC struct dentry *
1516 xfs_fs_mount(
1517         struct file_system_type *fs_type,
1518         int                     flags,
1519         const char              *dev_name,
1520         void                    *data)
1521 {
1522         return mount_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super);
1523 }
1524
1525 static long
1526 xfs_fs_nr_cached_objects(
1527         struct super_block      *sb,
1528         int                     nid)
1529 {
1530         return xfs_reclaim_inodes_count(XFS_M(sb));
1531 }
1532
1533 static long
1534 xfs_fs_free_cached_objects(
1535         struct super_block      *sb,
1536         long                    nr_to_scan,
1537         int                     nid)
1538 {
1539         return xfs_reclaim_inodes_nr(XFS_M(sb), nr_to_scan);
1540 }
1541
1542 static const struct super_operations xfs_super_operations = {
1543         .alloc_inode            = xfs_fs_alloc_inode,
1544         .destroy_inode          = xfs_fs_destroy_inode,
1545         .evict_inode            = xfs_fs_evict_inode,
1546         .drop_inode             = xfs_fs_drop_inode,
1547         .put_super              = xfs_fs_put_super,
1548         .sync_fs                = xfs_fs_sync_fs,
1549         .freeze_fs              = xfs_fs_freeze,
1550         .unfreeze_fs            = xfs_fs_unfreeze,
1551         .statfs                 = xfs_fs_statfs,
1552         .remount_fs             = xfs_fs_remount,
1553         .show_options           = xfs_fs_show_options,
1554         .nr_cached_objects      = xfs_fs_nr_cached_objects,
1555         .free_cached_objects    = xfs_fs_free_cached_objects,
1556 };
1557
1558 static struct file_system_type xfs_fs_type = {
1559         .owner                  = THIS_MODULE,
1560         .name                   = "xfs",
1561         .mount                  = xfs_fs_mount,
1562         .kill_sb                = kill_block_super,
1563         .fs_flags               = FS_REQUIRES_DEV,
1564 };
1565 MODULE_ALIAS_FS("xfs");
1566
1567 STATIC int __init
1568 xfs_init_zones(void)
1569 {
1570
1571         xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
1572         if (!xfs_ioend_zone)
1573                 goto out;
1574
1575         xfs_ioend_pool = mempool_create_slab_pool(4 * MAX_BUF_PER_PAGE,
1576                                                   xfs_ioend_zone);
1577         if (!xfs_ioend_pool)
1578                 goto out_destroy_ioend_zone;
1579
1580         xfs_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t),
1581                                                 "xfs_log_ticket");
1582         if (!xfs_log_ticket_zone)
1583                 goto out_destroy_ioend_pool;
1584
1585         xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
1586                                                 "xfs_bmap_free_item");
1587         if (!xfs_bmap_free_item_zone)
1588                 goto out_destroy_log_ticket_zone;
1589
1590         xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
1591                                                 "xfs_btree_cur");
1592         if (!xfs_btree_cur_zone)
1593                 goto out_destroy_bmap_free_item_zone;
1594
1595         xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t),
1596                                                 "xfs_da_state");
1597         if (!xfs_da_state_zone)
1598                 goto out_destroy_btree_cur_zone;
1599
1600         xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
1601         if (!xfs_ifork_zone)
1602                 goto out_destroy_da_state_zone;
1603
1604         xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
1605         if (!xfs_trans_zone)
1606                 goto out_destroy_ifork_zone;
1607
1608         xfs_log_item_desc_zone =
1609                 kmem_zone_init(sizeof(struct xfs_log_item_desc),
1610                                "xfs_log_item_desc");
1611         if (!xfs_log_item_desc_zone)
1612                 goto out_destroy_trans_zone;
1613
1614         /*
1615          * The size of the zone allocated buf log item is the maximum
1616          * size possible under XFS.  This wastes a little bit of memory,
1617          * but it is much faster.
1618          */
1619         xfs_buf_item_zone = kmem_zone_init(sizeof(struct xfs_buf_log_item),
1620                                            "xfs_buf_item");
1621         if (!xfs_buf_item_zone)
1622                 goto out_destroy_log_item_desc_zone;
1623
1624         xfs_efd_zone = kmem_zone_init((sizeof(xfs_efd_log_item_t) +
1625                         ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
1626                                  sizeof(xfs_extent_t))), "xfs_efd_item");
1627         if (!xfs_efd_zone)
1628                 goto out_destroy_buf_item_zone;
1629
1630         xfs_efi_zone = kmem_zone_init((sizeof(xfs_efi_log_item_t) +
1631                         ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
1632                                 sizeof(xfs_extent_t))), "xfs_efi_item");
1633         if (!xfs_efi_zone)
1634                 goto out_destroy_efd_zone;
1635
1636         xfs_inode_zone =
1637                 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
1638                         KM_ZONE_HWALIGN | KM_ZONE_RECLAIM | KM_ZONE_SPREAD,
1639                         xfs_fs_inode_init_once);
1640         if (!xfs_inode_zone)
1641                 goto out_destroy_efi_zone;
1642
1643         xfs_ili_zone =
1644                 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
1645                                         KM_ZONE_SPREAD, NULL);
1646         if (!xfs_ili_zone)
1647                 goto out_destroy_inode_zone;
1648         xfs_icreate_zone = kmem_zone_init(sizeof(struct xfs_icreate_item),
1649                                         "xfs_icr");
1650         if (!xfs_icreate_zone)
1651                 goto out_destroy_ili_zone;
1652
1653         return 0;
1654
1655  out_destroy_ili_zone:
1656         kmem_zone_destroy(xfs_ili_zone);
1657  out_destroy_inode_zone:
1658         kmem_zone_destroy(xfs_inode_zone);
1659  out_destroy_efi_zone:
1660         kmem_zone_destroy(xfs_efi_zone);
1661  out_destroy_efd_zone:
1662         kmem_zone_destroy(xfs_efd_zone);
1663  out_destroy_buf_item_zone:
1664         kmem_zone_destroy(xfs_buf_item_zone);
1665  out_destroy_log_item_desc_zone:
1666         kmem_zone_destroy(xfs_log_item_desc_zone);
1667  out_destroy_trans_zone:
1668         kmem_zone_destroy(xfs_trans_zone);
1669  out_destroy_ifork_zone:
1670         kmem_zone_destroy(xfs_ifork_zone);
1671  out_destroy_da_state_zone:
1672         kmem_zone_destroy(xfs_da_state_zone);
1673  out_destroy_btree_cur_zone:
1674         kmem_zone_destroy(xfs_btree_cur_zone);
1675  out_destroy_bmap_free_item_zone:
1676         kmem_zone_destroy(xfs_bmap_free_item_zone);
1677  out_destroy_log_ticket_zone:
1678         kmem_zone_destroy(xfs_log_ticket_zone);
1679  out_destroy_ioend_pool:
1680         mempool_destroy(xfs_ioend_pool);
1681  out_destroy_ioend_zone:
1682         kmem_zone_destroy(xfs_ioend_zone);
1683  out:
1684         return -ENOMEM;
1685 }
1686
1687 STATIC void
1688 xfs_destroy_zones(void)
1689 {
1690         /*
1691          * Make sure all delayed rcu free are flushed before we
1692          * destroy caches.
1693          */
1694         rcu_barrier();
1695         kmem_zone_destroy(xfs_icreate_zone);
1696         kmem_zone_destroy(xfs_ili_zone);
1697         kmem_zone_destroy(xfs_inode_zone);
1698         kmem_zone_destroy(xfs_efi_zone);
1699         kmem_zone_destroy(xfs_efd_zone);
1700         kmem_zone_destroy(xfs_buf_item_zone);
1701         kmem_zone_destroy(xfs_log_item_desc_zone);
1702         kmem_zone_destroy(xfs_trans_zone);
1703         kmem_zone_destroy(xfs_ifork_zone);
1704         kmem_zone_destroy(xfs_da_state_zone);
1705         kmem_zone_destroy(xfs_btree_cur_zone);
1706         kmem_zone_destroy(xfs_bmap_free_item_zone);
1707         kmem_zone_destroy(xfs_log_ticket_zone);
1708         mempool_destroy(xfs_ioend_pool);
1709         kmem_zone_destroy(xfs_ioend_zone);
1710
1711 }
1712
1713 STATIC int __init
1714 xfs_init_workqueues(void)
1715 {
1716         /*
1717          * The allocation workqueue can be used in memory reclaim situations
1718          * (writepage path), and parallelism is only limited by the number of
1719          * AGs in all the filesystems mounted. Hence use the default large
1720          * max_active value for this workqueue.
1721          */
1722         xfs_alloc_wq = alloc_workqueue("xfsalloc",
1723                         WQ_MEM_RECLAIM|WQ_FREEZABLE, 0);
1724         if (!xfs_alloc_wq)
1725                 return -ENOMEM;
1726
1727         return 0;
1728 }
1729
1730 STATIC void
1731 xfs_destroy_workqueues(void)
1732 {
1733         destroy_workqueue(xfs_alloc_wq);
1734 }
1735
1736 STATIC int __init
1737 init_xfs_fs(void)
1738 {
1739         int                     error;
1740
1741         printk(KERN_INFO XFS_VERSION_STRING " with "
1742                          XFS_BUILD_OPTIONS " enabled\n");
1743
1744         xfs_dir_startup();
1745
1746         error = xfs_init_zones();
1747         if (error)
1748                 goto out;
1749
1750         error = xfs_init_workqueues();
1751         if (error)
1752                 goto out_destroy_zones;
1753
1754         error = xfs_mru_cache_init();
1755         if (error)
1756                 goto out_destroy_wq;
1757
1758         error = xfs_buf_init();
1759         if (error)
1760                 goto out_mru_cache_uninit;
1761
1762         error = xfs_init_procfs();
1763         if (error)
1764                 goto out_buf_terminate;
1765
1766         error = xfs_sysctl_register();
1767         if (error)
1768                 goto out_cleanup_procfs;
1769
1770         xfs_kset = kset_create_and_add("xfs", NULL, fs_kobj);
1771         if (!xfs_kset) {
1772                 error = -ENOMEM;
1773                 goto out_sysctl_unregister;;
1774         }
1775
1776 #ifdef DEBUG
1777         xfs_dbg_kobj.kobject.kset = xfs_kset;
1778         error = xfs_sysfs_init(&xfs_dbg_kobj, &xfs_dbg_ktype, NULL, "debug");
1779         if (error)
1780                 goto out_kset_unregister;
1781 #endif
1782
1783         error = xfs_qm_init();
1784         if (error)
1785                 goto out_remove_kobj;
1786
1787         error = register_filesystem(&xfs_fs_type);
1788         if (error)
1789                 goto out_qm_exit;
1790         return 0;
1791
1792  out_qm_exit:
1793         xfs_qm_exit();
1794  out_remove_kobj:
1795 #ifdef DEBUG
1796         xfs_sysfs_del(&xfs_dbg_kobj);
1797  out_kset_unregister:
1798 #endif
1799         kset_unregister(xfs_kset);
1800  out_sysctl_unregister:
1801         xfs_sysctl_unregister();
1802  out_cleanup_procfs:
1803         xfs_cleanup_procfs();
1804  out_buf_terminate:
1805         xfs_buf_terminate();
1806  out_mru_cache_uninit:
1807         xfs_mru_cache_uninit();
1808  out_destroy_wq:
1809         xfs_destroy_workqueues();
1810  out_destroy_zones:
1811         xfs_destroy_zones();
1812  out:
1813         return error;
1814 }
1815
1816 STATIC void __exit
1817 exit_xfs_fs(void)
1818 {
1819         xfs_qm_exit();
1820         unregister_filesystem(&xfs_fs_type);
1821 #ifdef DEBUG
1822         xfs_sysfs_del(&xfs_dbg_kobj);
1823 #endif
1824         kset_unregister(xfs_kset);
1825         xfs_sysctl_unregister();
1826         xfs_cleanup_procfs();
1827         xfs_buf_terminate();
1828         xfs_mru_cache_uninit();
1829         xfs_destroy_workqueues();
1830         xfs_destroy_zones();
1831 }
1832
1833 module_init(init_xfs_fs);
1834 module_exit(exit_xfs_fs);
1835
1836 MODULE_AUTHOR("Silicon Graphics, Inc.");
1837 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
1838 MODULE_LICENSE("GPL");