[XFS] make SYNC_ATTR no longer use xfs_sync
[safe/jmp/linux-2.6] / fs / xfs / linux-2.6 / 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 #include "xfs.h"
19 #include "xfs_bit.h"
20 #include "xfs_log.h"
21 #include "xfs_clnt.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_alloc.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_quota.h"
30 #include "xfs_mount.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_alloc_btree.h"
33 #include "xfs_ialloc_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_btree.h"
39 #include "xfs_btree_trace.h"
40 #include "xfs_ialloc.h"
41 #include "xfs_bmap.h"
42 #include "xfs_rtalloc.h"
43 #include "xfs_error.h"
44 #include "xfs_itable.h"
45 #include "xfs_fsops.h"
46 #include "xfs_rw.h"
47 #include "xfs_acl.h"
48 #include "xfs_attr.h"
49 #include "xfs_buf_item.h"
50 #include "xfs_utils.h"
51 #include "xfs_vnodeops.h"
52 #include "xfs_vfsops.h"
53 #include "xfs_version.h"
54 #include "xfs_log_priv.h"
55 #include "xfs_trans_priv.h"
56 #include "xfs_filestream.h"
57 #include "xfs_da_btree.h"
58 #include "xfs_dir2_trace.h"
59 #include "xfs_extfree_item.h"
60 #include "xfs_mru_cache.h"
61 #include "xfs_inode_item.h"
62 #include "xfs_sync.h"
63
64 #include <linux/namei.h>
65 #include <linux/init.h>
66 #include <linux/mount.h>
67 #include <linux/mempool.h>
68 #include <linux/writeback.h>
69 #include <linux/kthread.h>
70 #include <linux/freezer.h>
71 #include <linux/parser.h>
72
73 static struct quotactl_ops xfs_quotactl_operations;
74 static struct super_operations xfs_super_operations;
75 static kmem_zone_t *xfs_vnode_zone;
76 static kmem_zone_t *xfs_ioend_zone;
77 mempool_t *xfs_ioend_pool;
78
79 STATIC struct xfs_mount_args *
80 xfs_args_allocate(
81         struct super_block      *sb,
82         int                     silent)
83 {
84         struct xfs_mount_args   *args;
85
86         args = kzalloc(sizeof(struct xfs_mount_args), GFP_KERNEL);
87         if (!args)
88                 return NULL;
89
90         args->logbufs = args->logbufsize = -1;
91         strncpy(args->fsname, sb->s_id, MAXNAMELEN);
92
93         /* Copy the already-parsed mount(2) flags we're interested in */
94         if (sb->s_flags & MS_DIRSYNC)
95                 args->flags |= XFSMNT_DIRSYNC;
96         if (sb->s_flags & MS_SYNCHRONOUS)
97                 args->flags |= XFSMNT_WSYNC;
98         if (silent)
99                 args->flags |= XFSMNT_QUIET;
100         args->flags |= XFSMNT_32BITINODES;
101
102         return args;
103 }
104
105 #define MNTOPT_LOGBUFS  "logbufs"       /* number of XFS log buffers */
106 #define MNTOPT_LOGBSIZE "logbsize"      /* size of XFS log buffers */
107 #define MNTOPT_LOGDEV   "logdev"        /* log device */
108 #define MNTOPT_RTDEV    "rtdev"         /* realtime I/O device */
109 #define MNTOPT_BIOSIZE  "biosize"       /* log2 of preferred buffered io size */
110 #define MNTOPT_WSYNC    "wsync"         /* safe-mode nfs compatible mount */
111 #define MNTOPT_INO64    "ino64"         /* force inodes into 64-bit range */
112 #define MNTOPT_NOALIGN  "noalign"       /* turn off stripe alignment */
113 #define MNTOPT_SWALLOC  "swalloc"       /* turn on stripe width allocation */
114 #define MNTOPT_SUNIT    "sunit"         /* data volume stripe unit */
115 #define MNTOPT_SWIDTH   "swidth"        /* data volume stripe width */
116 #define MNTOPT_NOUUID   "nouuid"        /* ignore filesystem UUID */
117 #define MNTOPT_MTPT     "mtpt"          /* filesystem mount point */
118 #define MNTOPT_GRPID    "grpid"         /* group-ID from parent directory */
119 #define MNTOPT_NOGRPID  "nogrpid"       /* group-ID from current process */
120 #define MNTOPT_BSDGROUPS    "bsdgroups"    /* group-ID from parent directory */
121 #define MNTOPT_SYSVGROUPS   "sysvgroups"   /* group-ID from current process */
122 #define MNTOPT_ALLOCSIZE    "allocsize"    /* preferred allocation size */
123 #define MNTOPT_NORECOVERY   "norecovery"   /* don't run XFS recovery */
124 #define MNTOPT_BARRIER  "barrier"       /* use writer barriers for log write and
125                                          * unwritten extent conversion */
126 #define MNTOPT_NOBARRIER "nobarrier"    /* .. disable */
127 #define MNTOPT_OSYNCISOSYNC "osyncisosync" /* o_sync is REALLY o_sync */
128 #define MNTOPT_64BITINODE   "inode64"   /* inodes can be allocated anywhere */
129 #define MNTOPT_IKEEP    "ikeep"         /* do not free empty inode clusters */
130 #define MNTOPT_NOIKEEP  "noikeep"       /* free empty inode clusters */
131 #define MNTOPT_LARGEIO     "largeio"    /* report large I/O sizes in stat() */
132 #define MNTOPT_NOLARGEIO   "nolargeio"  /* do not report large I/O sizes
133                                          * in stat(). */
134 #define MNTOPT_ATTR2    "attr2"         /* do use attr2 attribute format */
135 #define MNTOPT_NOATTR2  "noattr2"       /* do not use attr2 attribute format */
136 #define MNTOPT_FILESTREAM  "filestreams" /* use filestreams allocator */
137 #define MNTOPT_QUOTA    "quota"         /* disk quotas (user) */
138 #define MNTOPT_NOQUOTA  "noquota"       /* no quotas */
139 #define MNTOPT_USRQUOTA "usrquota"      /* user quota enabled */
140 #define MNTOPT_GRPQUOTA "grpquota"      /* group quota enabled */
141 #define MNTOPT_PRJQUOTA "prjquota"      /* project quota enabled */
142 #define MNTOPT_UQUOTA   "uquota"        /* user quota (IRIX variant) */
143 #define MNTOPT_GQUOTA   "gquota"        /* group quota (IRIX variant) */
144 #define MNTOPT_PQUOTA   "pquota"        /* project quota (IRIX variant) */
145 #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
146 #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
147 #define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
148 #define MNTOPT_QUOTANOENF  "qnoenforce" /* same as uqnoenforce */
149 #define MNTOPT_DMAPI    "dmapi"         /* DMI enabled (DMAPI / XDSM) */
150 #define MNTOPT_XDSM     "xdsm"          /* DMI enabled (DMAPI / XDSM) */
151 #define MNTOPT_DMI      "dmi"           /* DMI enabled (DMAPI / XDSM) */
152
153 /*
154  * Table driven mount option parser.
155  *
156  * Currently only used for remount, but it will be used for mount
157  * in the future, too.
158  */
159 enum {
160         Opt_barrier, Opt_nobarrier, Opt_err
161 };
162
163 static const match_table_t tokens = {
164         {Opt_barrier, "barrier"},
165         {Opt_nobarrier, "nobarrier"},
166         {Opt_err, NULL}
167 };
168
169
170 STATIC unsigned long
171 suffix_strtoul(char *s, char **endp, unsigned int base)
172 {
173         int     last, shift_left_factor = 0;
174         char    *value = s;
175
176         last = strlen(value) - 1;
177         if (value[last] == 'K' || value[last] == 'k') {
178                 shift_left_factor = 10;
179                 value[last] = '\0';
180         }
181         if (value[last] == 'M' || value[last] == 'm') {
182                 shift_left_factor = 20;
183                 value[last] = '\0';
184         }
185         if (value[last] == 'G' || value[last] == 'g') {
186                 shift_left_factor = 30;
187                 value[last] = '\0';
188         }
189
190         return simple_strtoul((const char *)s, endp, base) << shift_left_factor;
191 }
192
193 STATIC int
194 xfs_parseargs(
195         struct xfs_mount        *mp,
196         char                    *options,
197         struct xfs_mount_args   *args,
198         int                     update)
199 {
200         char                    *this_char, *value, *eov;
201         int                     dsunit, dswidth, vol_dsunit, vol_dswidth;
202         int                     iosize;
203         int                     dmapi_implies_ikeep = 1;
204
205         args->flags |= XFSMNT_BARRIER;
206         args->flags2 |= XFSMNT2_COMPAT_IOSIZE;
207
208         if (!options)
209                 goto done;
210
211         iosize = dsunit = dswidth = vol_dsunit = vol_dswidth = 0;
212
213         while ((this_char = strsep(&options, ",")) != NULL) {
214                 if (!*this_char)
215                         continue;
216                 if ((value = strchr(this_char, '=')) != NULL)
217                         *value++ = 0;
218
219                 if (!strcmp(this_char, MNTOPT_LOGBUFS)) {
220                         if (!value || !*value) {
221                                 cmn_err(CE_WARN,
222                                         "XFS: %s option requires an argument",
223                                         this_char);
224                                 return EINVAL;
225                         }
226                         args->logbufs = simple_strtoul(value, &eov, 10);
227                 } else if (!strcmp(this_char, MNTOPT_LOGBSIZE)) {
228                         if (!value || !*value) {
229                                 cmn_err(CE_WARN,
230                                         "XFS: %s option requires an argument",
231                                         this_char);
232                                 return EINVAL;
233                         }
234                         args->logbufsize = suffix_strtoul(value, &eov, 10);
235                 } else if (!strcmp(this_char, MNTOPT_LOGDEV)) {
236                         if (!value || !*value) {
237                                 cmn_err(CE_WARN,
238                                         "XFS: %s option requires an argument",
239                                         this_char);
240                                 return EINVAL;
241                         }
242                         strncpy(args->logname, value, MAXNAMELEN);
243                 } else if (!strcmp(this_char, MNTOPT_MTPT)) {
244                         if (!value || !*value) {
245                                 cmn_err(CE_WARN,
246                                         "XFS: %s option requires an argument",
247                                         this_char);
248                                 return EINVAL;
249                         }
250                         strncpy(args->mtpt, value, MAXNAMELEN);
251                 } else if (!strcmp(this_char, MNTOPT_RTDEV)) {
252                         if (!value || !*value) {
253                                 cmn_err(CE_WARN,
254                                         "XFS: %s option requires an argument",
255                                         this_char);
256                                 return EINVAL;
257                         }
258                         strncpy(args->rtname, value, MAXNAMELEN);
259                 } else if (!strcmp(this_char, MNTOPT_BIOSIZE)) {
260                         if (!value || !*value) {
261                                 cmn_err(CE_WARN,
262                                         "XFS: %s option requires an argument",
263                                         this_char);
264                                 return EINVAL;
265                         }
266                         iosize = simple_strtoul(value, &eov, 10);
267                         args->flags |= XFSMNT_IOSIZE;
268                         args->iosizelog = (uint8_t) iosize;
269                 } else if (!strcmp(this_char, MNTOPT_ALLOCSIZE)) {
270                         if (!value || !*value) {
271                                 cmn_err(CE_WARN,
272                                         "XFS: %s option requires an argument",
273                                         this_char);
274                                 return EINVAL;
275                         }
276                         iosize = suffix_strtoul(value, &eov, 10);
277                         args->flags |= XFSMNT_IOSIZE;
278                         args->iosizelog = ffs(iosize) - 1;
279                 } else if (!strcmp(this_char, MNTOPT_GRPID) ||
280                            !strcmp(this_char, MNTOPT_BSDGROUPS)) {
281                         mp->m_flags |= XFS_MOUNT_GRPID;
282                 } else if (!strcmp(this_char, MNTOPT_NOGRPID) ||
283                            !strcmp(this_char, MNTOPT_SYSVGROUPS)) {
284                         mp->m_flags &= ~XFS_MOUNT_GRPID;
285                 } else if (!strcmp(this_char, MNTOPT_WSYNC)) {
286                         args->flags |= XFSMNT_WSYNC;
287                 } else if (!strcmp(this_char, MNTOPT_OSYNCISOSYNC)) {
288                         args->flags |= XFSMNT_OSYNCISOSYNC;
289                 } else if (!strcmp(this_char, MNTOPT_NORECOVERY)) {
290                         args->flags |= XFSMNT_NORECOVERY;
291                 } else if (!strcmp(this_char, MNTOPT_INO64)) {
292                         args->flags |= XFSMNT_INO64;
293 #if !XFS_BIG_INUMS
294                         cmn_err(CE_WARN,
295                                 "XFS: %s option not allowed on this system",
296                                 this_char);
297                         return EINVAL;
298 #endif
299                 } else if (!strcmp(this_char, MNTOPT_NOALIGN)) {
300                         args->flags |= XFSMNT_NOALIGN;
301                 } else if (!strcmp(this_char, MNTOPT_SWALLOC)) {
302                         args->flags |= XFSMNT_SWALLOC;
303                 } else if (!strcmp(this_char, MNTOPT_SUNIT)) {
304                         if (!value || !*value) {
305                                 cmn_err(CE_WARN,
306                                         "XFS: %s option requires an argument",
307                                         this_char);
308                                 return EINVAL;
309                         }
310                         dsunit = simple_strtoul(value, &eov, 10);
311                 } else if (!strcmp(this_char, MNTOPT_SWIDTH)) {
312                         if (!value || !*value) {
313                                 cmn_err(CE_WARN,
314                                         "XFS: %s option requires an argument",
315                                         this_char);
316                                 return EINVAL;
317                         }
318                         dswidth = simple_strtoul(value, &eov, 10);
319                 } else if (!strcmp(this_char, MNTOPT_64BITINODE)) {
320                         args->flags &= ~XFSMNT_32BITINODES;
321 #if !XFS_BIG_INUMS
322                         cmn_err(CE_WARN,
323                                 "XFS: %s option not allowed on this system",
324                                 this_char);
325                         return EINVAL;
326 #endif
327                 } else if (!strcmp(this_char, MNTOPT_NOUUID)) {
328                         args->flags |= XFSMNT_NOUUID;
329                 } else if (!strcmp(this_char, MNTOPT_BARRIER)) {
330                         args->flags |= XFSMNT_BARRIER;
331                 } else if (!strcmp(this_char, MNTOPT_NOBARRIER)) {
332                         args->flags &= ~XFSMNT_BARRIER;
333                 } else if (!strcmp(this_char, MNTOPT_IKEEP)) {
334                         args->flags |= XFSMNT_IKEEP;
335                 } else if (!strcmp(this_char, MNTOPT_NOIKEEP)) {
336                         dmapi_implies_ikeep = 0;
337                         args->flags &= ~XFSMNT_IKEEP;
338                 } else if (!strcmp(this_char, MNTOPT_LARGEIO)) {
339                         args->flags2 &= ~XFSMNT2_COMPAT_IOSIZE;
340                 } else if (!strcmp(this_char, MNTOPT_NOLARGEIO)) {
341                         args->flags2 |= XFSMNT2_COMPAT_IOSIZE;
342                 } else if (!strcmp(this_char, MNTOPT_ATTR2)) {
343                         args->flags |= XFSMNT_ATTR2;
344                 } else if (!strcmp(this_char, MNTOPT_NOATTR2)) {
345                         args->flags &= ~XFSMNT_ATTR2;
346                         args->flags |= XFSMNT_NOATTR2;
347                 } else if (!strcmp(this_char, MNTOPT_FILESTREAM)) {
348                         args->flags2 |= XFSMNT2_FILESTREAMS;
349                 } else if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
350                         args->flags &= ~(XFSMNT_UQUOTAENF|XFSMNT_UQUOTA);
351                         args->flags &= ~(XFSMNT_GQUOTAENF|XFSMNT_GQUOTA);
352                 } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
353                            !strcmp(this_char, MNTOPT_UQUOTA) ||
354                            !strcmp(this_char, MNTOPT_USRQUOTA)) {
355                         args->flags |= XFSMNT_UQUOTA | XFSMNT_UQUOTAENF;
356                 } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
357                            !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
358                         args->flags |= XFSMNT_UQUOTA;
359                         args->flags &= ~XFSMNT_UQUOTAENF;
360                 } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
361                            !strcmp(this_char, MNTOPT_PRJQUOTA)) {
362                         args->flags |= XFSMNT_PQUOTA | XFSMNT_PQUOTAENF;
363                 } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
364                         args->flags |= XFSMNT_PQUOTA;
365                         args->flags &= ~XFSMNT_PQUOTAENF;
366                 } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
367                            !strcmp(this_char, MNTOPT_GRPQUOTA)) {
368                         args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
369                 } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
370                         args->flags |= XFSMNT_GQUOTA;
371                         args->flags &= ~XFSMNT_GQUOTAENF;
372                 } else if (!strcmp(this_char, MNTOPT_DMAPI)) {
373                         args->flags |= XFSMNT_DMAPI;
374                 } else if (!strcmp(this_char, MNTOPT_XDSM)) {
375                         args->flags |= XFSMNT_DMAPI;
376                 } else if (!strcmp(this_char, MNTOPT_DMI)) {
377                         args->flags |= XFSMNT_DMAPI;
378                 } else if (!strcmp(this_char, "ihashsize")) {
379                         cmn_err(CE_WARN,
380         "XFS: ihashsize no longer used, option is deprecated.");
381                 } else if (!strcmp(this_char, "osyncisdsync")) {
382                         /* no-op, this is now the default */
383                         cmn_err(CE_WARN,
384         "XFS: osyncisdsync is now the default, option is deprecated.");
385                 } else if (!strcmp(this_char, "irixsgid")) {
386                         cmn_err(CE_WARN,
387         "XFS: irixsgid is now a sysctl(2) variable, option is deprecated.");
388                 } else {
389                         cmn_err(CE_WARN,
390                                 "XFS: unknown mount option [%s].", this_char);
391                         return EINVAL;
392                 }
393         }
394
395         if (args->flags & XFSMNT_NORECOVERY) {
396                 if ((mp->m_flags & XFS_MOUNT_RDONLY) == 0) {
397                         cmn_err(CE_WARN,
398                                 "XFS: no-recovery mounts must be read-only.");
399                         return EINVAL;
400                 }
401         }
402
403         if ((args->flags & XFSMNT_NOALIGN) && (dsunit || dswidth)) {
404                 cmn_err(CE_WARN,
405         "XFS: sunit and swidth options incompatible with the noalign option");
406                 return EINVAL;
407         }
408
409         if ((args->flags & XFSMNT_GQUOTA) && (args->flags & XFSMNT_PQUOTA)) {
410                 cmn_err(CE_WARN,
411                         "XFS: cannot mount with both project and group quota");
412                 return EINVAL;
413         }
414
415         if ((args->flags & XFSMNT_DMAPI) && *args->mtpt == '\0') {
416                 printk("XFS: %s option needs the mount point option as well\n",
417                         MNTOPT_DMAPI);
418                 return EINVAL;
419         }
420
421         if ((dsunit && !dswidth) || (!dsunit && dswidth)) {
422                 cmn_err(CE_WARN,
423                         "XFS: sunit and swidth must be specified together");
424                 return EINVAL;
425         }
426
427         if (dsunit && (dswidth % dsunit != 0)) {
428                 cmn_err(CE_WARN,
429         "XFS: stripe width (%d) must be a multiple of the stripe unit (%d)",
430                         dswidth, dsunit);
431                 return EINVAL;
432         }
433
434         /*
435          * Applications using DMI filesystems often expect the
436          * inode generation number to be monotonically increasing.
437          * If we delete inode chunks we break this assumption, so
438          * keep unused inode chunks on disk for DMI filesystems
439          * until we come up with a better solution.
440          * Note that if "ikeep" or "noikeep" mount options are
441          * supplied, then they are honored.
442          */
443         if ((args->flags & XFSMNT_DMAPI) && dmapi_implies_ikeep)
444                 args->flags |= XFSMNT_IKEEP;
445
446         if ((args->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
447                 if (dsunit) {
448                         args->sunit = dsunit;
449                         args->flags |= XFSMNT_RETERR;
450                 } else {
451                         args->sunit = vol_dsunit;
452                 }
453                 dswidth ? (args->swidth = dswidth) :
454                           (args->swidth = vol_dswidth);
455         } else {
456                 args->sunit = args->swidth = 0;
457         }
458
459 done:
460         if (args->flags & XFSMNT_32BITINODES)
461                 mp->m_flags |= XFS_MOUNT_SMALL_INUMS;
462         if (args->flags2)
463                 args->flags |= XFSMNT_FLAGS2;
464         return 0;
465 }
466
467 struct proc_xfs_info {
468         int     flag;
469         char    *str;
470 };
471
472 STATIC int
473 xfs_showargs(
474         struct xfs_mount        *mp,
475         struct seq_file         *m)
476 {
477         static struct proc_xfs_info xfs_info_set[] = {
478                 /* the few simple ones we can get from the mount struct */
479                 { XFS_MOUNT_IKEEP,              "," MNTOPT_IKEEP },
480                 { XFS_MOUNT_WSYNC,              "," MNTOPT_WSYNC },
481                 { XFS_MOUNT_INO64,              "," MNTOPT_INO64 },
482                 { XFS_MOUNT_NOALIGN,            "," MNTOPT_NOALIGN },
483                 { XFS_MOUNT_SWALLOC,            "," MNTOPT_SWALLOC },
484                 { XFS_MOUNT_NOUUID,             "," MNTOPT_NOUUID },
485                 { XFS_MOUNT_NORECOVERY,         "," MNTOPT_NORECOVERY },
486                 { XFS_MOUNT_OSYNCISOSYNC,       "," MNTOPT_OSYNCISOSYNC },
487                 { XFS_MOUNT_ATTR2,              "," MNTOPT_ATTR2 },
488                 { XFS_MOUNT_FILESTREAMS,        "," MNTOPT_FILESTREAM },
489                 { XFS_MOUNT_DMAPI,              "," MNTOPT_DMAPI },
490                 { XFS_MOUNT_GRPID,              "," MNTOPT_GRPID },
491                 { 0, NULL }
492         };
493         static struct proc_xfs_info xfs_info_unset[] = {
494                 /* the few simple ones we can get from the mount struct */
495                 { XFS_MOUNT_COMPAT_IOSIZE,      "," MNTOPT_LARGEIO },
496                 { XFS_MOUNT_BARRIER,            "," MNTOPT_NOBARRIER },
497                 { XFS_MOUNT_SMALL_INUMS,        "," MNTOPT_64BITINODE },
498                 { 0, NULL }
499         };
500         struct proc_xfs_info    *xfs_infop;
501
502         for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
503                 if (mp->m_flags & xfs_infop->flag)
504                         seq_puts(m, xfs_infop->str);
505         }
506         for (xfs_infop = xfs_info_unset; xfs_infop->flag; xfs_infop++) {
507                 if (!(mp->m_flags & xfs_infop->flag))
508                         seq_puts(m, xfs_infop->str);
509         }
510
511         if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
512                 seq_printf(m, "," MNTOPT_ALLOCSIZE "=%dk",
513                                 (int)(1 << mp->m_writeio_log) >> 10);
514
515         if (mp->m_logbufs > 0)
516                 seq_printf(m, "," MNTOPT_LOGBUFS "=%d", mp->m_logbufs);
517         if (mp->m_logbsize > 0)
518                 seq_printf(m, "," MNTOPT_LOGBSIZE "=%dk", mp->m_logbsize >> 10);
519
520         if (mp->m_logname)
521                 seq_printf(m, "," MNTOPT_LOGDEV "=%s", mp->m_logname);
522         if (mp->m_rtname)
523                 seq_printf(m, "," MNTOPT_RTDEV "=%s", mp->m_rtname);
524
525         if (mp->m_dalign > 0)
526                 seq_printf(m, "," MNTOPT_SUNIT "=%d",
527                                 (int)XFS_FSB_TO_BB(mp, mp->m_dalign));
528         if (mp->m_swidth > 0)
529                 seq_printf(m, "," MNTOPT_SWIDTH "=%d",
530                                 (int)XFS_FSB_TO_BB(mp, mp->m_swidth));
531
532         if (mp->m_qflags & (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD))
533                 seq_puts(m, "," MNTOPT_USRQUOTA);
534         else if (mp->m_qflags & XFS_UQUOTA_ACCT)
535                 seq_puts(m, "," MNTOPT_UQUOTANOENF);
536
537         if (mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_OQUOTA_ENFD))
538                 seq_puts(m, "," MNTOPT_PRJQUOTA);
539         else if (mp->m_qflags & XFS_PQUOTA_ACCT)
540                 seq_puts(m, "," MNTOPT_PQUOTANOENF);
541
542         if (mp->m_qflags & (XFS_GQUOTA_ACCT|XFS_OQUOTA_ENFD))
543                 seq_puts(m, "," MNTOPT_GRPQUOTA);
544         else if (mp->m_qflags & XFS_GQUOTA_ACCT)
545                 seq_puts(m, "," MNTOPT_GQUOTANOENF);
546
547         if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
548                 seq_puts(m, "," MNTOPT_NOQUOTA);
549
550         return 0;
551 }
552 __uint64_t
553 xfs_max_file_offset(
554         unsigned int            blockshift)
555 {
556         unsigned int            pagefactor = 1;
557         unsigned int            bitshift = BITS_PER_LONG - 1;
558
559         /* Figure out maximum filesize, on Linux this can depend on
560          * the filesystem blocksize (on 32 bit platforms).
561          * __block_prepare_write does this in an [unsigned] long...
562          *      page->index << (PAGE_CACHE_SHIFT - bbits)
563          * So, for page sized blocks (4K on 32 bit platforms),
564          * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
565          *      (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
566          * but for smaller blocksizes it is less (bbits = log2 bsize).
567          * Note1: get_block_t takes a long (implicit cast from above)
568          * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
569          * can optionally convert the [unsigned] long from above into
570          * an [unsigned] long long.
571          */
572
573 #if BITS_PER_LONG == 32
574 # if defined(CONFIG_LBD)
575         ASSERT(sizeof(sector_t) == 8);
576         pagefactor = PAGE_CACHE_SIZE;
577         bitshift = BITS_PER_LONG;
578 # else
579         pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
580 # endif
581 #endif
582
583         return (((__uint64_t)pagefactor) << bitshift) - 1;
584 }
585
586 int
587 xfs_blkdev_get(
588         xfs_mount_t             *mp,
589         const char              *name,
590         struct block_device     **bdevp)
591 {
592         int                     error = 0;
593
594         *bdevp = open_bdev_exclusive(name, FMODE_READ|FMODE_WRITE, mp);
595         if (IS_ERR(*bdevp)) {
596                 error = PTR_ERR(*bdevp);
597                 printk("XFS: Invalid device [%s], error=%d\n", name, error);
598         }
599
600         return -error;
601 }
602
603 void
604 xfs_blkdev_put(
605         struct block_device     *bdev)
606 {
607         if (bdev)
608                 close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
609 }
610
611 /*
612  * Try to write out the superblock using barriers.
613  */
614 STATIC int
615 xfs_barrier_test(
616         xfs_mount_t     *mp)
617 {
618         xfs_buf_t       *sbp = xfs_getsb(mp, 0);
619         int             error;
620
621         XFS_BUF_UNDONE(sbp);
622         XFS_BUF_UNREAD(sbp);
623         XFS_BUF_UNDELAYWRITE(sbp);
624         XFS_BUF_WRITE(sbp);
625         XFS_BUF_UNASYNC(sbp);
626         XFS_BUF_ORDERED(sbp);
627
628         xfsbdstrat(mp, sbp);
629         error = xfs_iowait(sbp);
630
631         /*
632          * Clear all the flags we set and possible error state in the
633          * buffer.  We only did the write to try out whether barriers
634          * worked and shouldn't leave any traces in the superblock
635          * buffer.
636          */
637         XFS_BUF_DONE(sbp);
638         XFS_BUF_ERROR(sbp, 0);
639         XFS_BUF_UNORDERED(sbp);
640
641         xfs_buf_relse(sbp);
642         return error;
643 }
644
645 void
646 xfs_mountfs_check_barriers(xfs_mount_t *mp)
647 {
648         int error;
649
650         if (mp->m_logdev_targp != mp->m_ddev_targp) {
651                 xfs_fs_cmn_err(CE_NOTE, mp,
652                   "Disabling barriers, not supported with external log device");
653                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
654                 return;
655         }
656
657         if (xfs_readonly_buftarg(mp->m_ddev_targp)) {
658                 xfs_fs_cmn_err(CE_NOTE, mp,
659                   "Disabling barriers, underlying device is readonly");
660                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
661                 return;
662         }
663
664         error = xfs_barrier_test(mp);
665         if (error) {
666                 xfs_fs_cmn_err(CE_NOTE, mp,
667                   "Disabling barriers, trial barrier write failed");
668                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
669                 return;
670         }
671 }
672
673 void
674 xfs_blkdev_issue_flush(
675         xfs_buftarg_t           *buftarg)
676 {
677         blkdev_issue_flush(buftarg->bt_bdev, NULL);
678 }
679
680 STATIC void
681 xfs_close_devices(
682         struct xfs_mount        *mp)
683 {
684         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
685                 struct block_device *logdev = mp->m_logdev_targp->bt_bdev;
686                 xfs_free_buftarg(mp->m_logdev_targp);
687                 xfs_blkdev_put(logdev);
688         }
689         if (mp->m_rtdev_targp) {
690                 struct block_device *rtdev = mp->m_rtdev_targp->bt_bdev;
691                 xfs_free_buftarg(mp->m_rtdev_targp);
692                 xfs_blkdev_put(rtdev);
693         }
694         xfs_free_buftarg(mp->m_ddev_targp);
695 }
696
697 /*
698  * The file system configurations are:
699  *      (1) device (partition) with data and internal log
700  *      (2) logical volume with data and log subvolumes.
701  *      (3) logical volume with data, log, and realtime subvolumes.
702  *
703  * We only have to handle opening the log and realtime volumes here if
704  * they are present.  The data subvolume has already been opened by
705  * get_sb_bdev() and is stored in sb->s_bdev.
706  */
707 STATIC int
708 xfs_open_devices(
709         struct xfs_mount        *mp,
710         struct xfs_mount_args   *args)
711 {
712         struct block_device     *ddev = mp->m_super->s_bdev;
713         struct block_device     *logdev = NULL, *rtdev = NULL;
714         int                     error;
715
716         /*
717          * Open real time and log devices - order is important.
718          */
719         if (args->logname[0]) {
720                 error = xfs_blkdev_get(mp, args->logname, &logdev);
721                 if (error)
722                         goto out;
723         }
724
725         if (args->rtname[0]) {
726                 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
727                 if (error)
728                         goto out_close_logdev;
729
730                 if (rtdev == ddev || rtdev == logdev) {
731                         cmn_err(CE_WARN,
732         "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
733                         error = EINVAL;
734                         goto out_close_rtdev;
735                 }
736         }
737
738         /*
739          * Setup xfs_mount buffer target pointers
740          */
741         error = ENOMEM;
742         mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
743         if (!mp->m_ddev_targp)
744                 goto out_close_rtdev;
745
746         if (rtdev) {
747                 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
748                 if (!mp->m_rtdev_targp)
749                         goto out_free_ddev_targ;
750         }
751
752         if (logdev && logdev != ddev) {
753                 mp->m_logdev_targp = xfs_alloc_buftarg(logdev, 1);
754                 if (!mp->m_logdev_targp)
755                         goto out_free_rtdev_targ;
756         } else {
757                 mp->m_logdev_targp = mp->m_ddev_targp;
758         }
759
760         return 0;
761
762  out_free_rtdev_targ:
763         if (mp->m_rtdev_targp)
764                 xfs_free_buftarg(mp->m_rtdev_targp);
765  out_free_ddev_targ:
766         xfs_free_buftarg(mp->m_ddev_targp);
767  out_close_rtdev:
768         if (rtdev)
769                 xfs_blkdev_put(rtdev);
770  out_close_logdev:
771         if (logdev && logdev != ddev)
772                 xfs_blkdev_put(logdev);
773  out:
774         return error;
775 }
776
777 /*
778  * Setup xfs_mount buffer target pointers based on superblock
779  */
780 STATIC int
781 xfs_setup_devices(
782         struct xfs_mount        *mp)
783 {
784         int                     error;
785
786         error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
787                                     mp->m_sb.sb_sectsize);
788         if (error)
789                 return error;
790
791         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
792                 unsigned int    log_sector_size = BBSIZE;
793
794                 if (xfs_sb_version_hassector(&mp->m_sb))
795                         log_sector_size = mp->m_sb.sb_logsectsize;
796                 error = xfs_setsize_buftarg(mp->m_logdev_targp,
797                                             mp->m_sb.sb_blocksize,
798                                             log_sector_size);
799                 if (error)
800                         return error;
801         }
802         if (mp->m_rtdev_targp) {
803                 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
804                                             mp->m_sb.sb_blocksize,
805                                             mp->m_sb.sb_sectsize);
806                 if (error)
807                         return error;
808         }
809
810         return 0;
811 }
812
813 /*
814  * XFS AIL push thread support
815  */
816 void
817 xfsaild_wakeup(
818         xfs_mount_t             *mp,
819         xfs_lsn_t               threshold_lsn)
820 {
821         mp->m_ail.xa_target = threshold_lsn;
822         wake_up_process(mp->m_ail.xa_task);
823 }
824
825 int
826 xfsaild(
827         void    *data)
828 {
829         xfs_mount_t     *mp = (xfs_mount_t *)data;
830         xfs_lsn_t       last_pushed_lsn = 0;
831         long            tout = 0;
832
833         while (!kthread_should_stop()) {
834                 if (tout)
835                         schedule_timeout_interruptible(msecs_to_jiffies(tout));
836                 tout = 1000;
837
838                 /* swsusp */
839                 try_to_freeze();
840
841                 ASSERT(mp->m_log);
842                 if (XFS_FORCED_SHUTDOWN(mp))
843                         continue;
844
845                 tout = xfsaild_push(mp, &last_pushed_lsn);
846         }
847
848         return 0;
849 }       /* xfsaild */
850
851 int
852 xfsaild_start(
853         xfs_mount_t     *mp)
854 {
855         mp->m_ail.xa_target = 0;
856         mp->m_ail.xa_task = kthread_run(xfsaild, mp, "xfsaild");
857         if (IS_ERR(mp->m_ail.xa_task))
858                 return -PTR_ERR(mp->m_ail.xa_task);
859         return 0;
860 }
861
862 void
863 xfsaild_stop(
864         xfs_mount_t     *mp)
865 {
866         kthread_stop(mp->m_ail.xa_task);
867 }
868
869
870
871 STATIC struct inode *
872 xfs_fs_alloc_inode(
873         struct super_block      *sb)
874 {
875         return kmem_zone_alloc(xfs_vnode_zone, KM_SLEEP);
876 }
877
878 STATIC void
879 xfs_fs_destroy_inode(
880         struct inode            *inode)
881 {
882         kmem_zone_free(xfs_vnode_zone, inode);
883 }
884
885 STATIC void
886 xfs_fs_inode_init_once(
887         void                    *vnode)
888 {
889         inode_init_once((struct inode *)vnode);
890 }
891
892
893 /*
894  * Slab object creation initialisation for the XFS inode.
895  * This covers only the idempotent fields in the XFS inode;
896  * all other fields need to be initialised on allocation
897  * from the slab. This avoids the need to repeatedly intialise
898  * fields in the xfs inode that left in the initialise state
899  * when freeing the inode.
900  */
901 void
902 xfs_inode_init_once(
903         void                    *inode)
904 {
905         struct xfs_inode        *ip = inode;
906
907         memset(ip, 0, sizeof(struct xfs_inode));
908         atomic_set(&ip->i_iocount, 0);
909         atomic_set(&ip->i_pincount, 0);
910         spin_lock_init(&ip->i_flags_lock);
911         INIT_LIST_HEAD(&ip->i_reclaim);
912         init_waitqueue_head(&ip->i_ipin_wait);
913         /*
914          * Because we want to use a counting completion, complete
915          * the flush completion once to allow a single access to
916          * the flush completion without blocking.
917          */
918         init_completion(&ip->i_flush);
919         complete(&ip->i_flush);
920
921         mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
922                      "xfsino", ip->i_ino);
923         mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
924 }
925
926 /*
927  * Attempt to flush the inode, this will actually fail
928  * if the inode is pinned, but we dirty the inode again
929  * at the point when it is unpinned after a log write,
930  * since this is when the inode itself becomes flushable.
931  */
932 STATIC int
933 xfs_fs_write_inode(
934         struct inode            *inode,
935         int                     sync)
936 {
937         int                     error = 0;
938         int                     flags = 0;
939
940         xfs_itrace_entry(XFS_I(inode));
941         if (sync) {
942                 filemap_fdatawait(inode->i_mapping);
943                 flags |= FLUSH_SYNC;
944         }
945         error = xfs_inode_flush(XFS_I(inode), flags);
946         /*
947          * if we failed to write out the inode then mark
948          * it dirty again so we'll try again later.
949          */
950         if (error)
951                 mark_inode_dirty_sync(inode);
952
953         return -error;
954 }
955
956 STATIC void
957 xfs_fs_clear_inode(
958         struct inode            *inode)
959 {
960         xfs_inode_t             *ip = XFS_I(inode);
961
962         /*
963          * ip can be null when xfs_iget_core calls xfs_idestroy if we
964          * find an inode with di_mode == 0 but without IGET_CREATE set.
965          */
966         if (ip) {
967                 xfs_itrace_entry(ip);
968                 XFS_STATS_INC(vn_rele);
969                 XFS_STATS_INC(vn_remove);
970                 XFS_STATS_INC(vn_reclaim);
971                 XFS_STATS_DEC(vn_active);
972
973                 xfs_inactive(ip);
974                 xfs_iflags_clear(ip, XFS_IMODIFIED);
975                 if (xfs_reclaim(ip))
976                         panic("%s: cannot reclaim 0x%p\n", __func__, inode);
977         }
978
979         ASSERT(XFS_I(inode) == NULL);
980 }
981
982 STATIC void
983 xfs_free_fsname(
984         struct xfs_mount        *mp)
985 {
986         kfree(mp->m_fsname);
987         kfree(mp->m_rtname);
988         kfree(mp->m_logname);
989 }
990
991 STATIC void
992 xfs_fs_put_super(
993         struct super_block      *sb)
994 {
995         struct xfs_mount        *mp = XFS_M(sb);
996         struct xfs_inode        *rip = mp->m_rootip;
997         int                     unmount_event_flags = 0;
998         int                     error;
999
1000         xfs_syncd_stop(mp);
1001         xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
1002         xfs_sync_inodes(mp, SYNC_ATTR|SYNC_DELWRI);
1003
1004 #ifdef HAVE_DMAPI
1005         if (mp->m_flags & XFS_MOUNT_DMAPI) {
1006                 unmount_event_flags =
1007                         (mp->m_dmevmask & (1 << DM_EVENT_UNMOUNT)) ?
1008                                 0 : DM_FLAGS_UNWANTED;
1009                 /*
1010                  * Ignore error from dmapi here, first unmount is not allowed
1011                  * to fail anyway, and second we wouldn't want to fail a
1012                  * unmount because of dmapi.
1013                  */
1014                 XFS_SEND_PREUNMOUNT(mp, rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
1015                                 NULL, NULL, 0, 0, unmount_event_flags);
1016         }
1017 #endif
1018
1019         /*
1020          * Blow away any referenced inode in the filestreams cache.
1021          * This can and will cause log traffic as inodes go inactive
1022          * here.
1023          */
1024         xfs_filestream_unmount(mp);
1025
1026         XFS_bflush(mp->m_ddev_targp);
1027         error = xfs_unmount_flush(mp, 0);
1028         WARN_ON(error);
1029
1030         /*
1031          * If we're forcing a shutdown, typically because of a media error,
1032          * we want to make sure we invalidate dirty pages that belong to
1033          * referenced vnodes as well.
1034          */
1035         if (XFS_FORCED_SHUTDOWN(mp)) {
1036                 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
1037                 ASSERT(error != EFSCORRUPTED);
1038         }
1039
1040         if (mp->m_flags & XFS_MOUNT_DMAPI) {
1041                 XFS_SEND_UNMOUNT(mp, rip, DM_RIGHT_NULL, 0, 0,
1042                                 unmount_event_flags);
1043         }
1044
1045         xfs_unmountfs(mp);
1046         xfs_freesb(mp);
1047         xfs_icsb_destroy_counters(mp);
1048         xfs_close_devices(mp);
1049         xfs_qmops_put(mp);
1050         xfs_dmops_put(mp);
1051         xfs_free_fsname(mp);
1052         kfree(mp);
1053 }
1054
1055 STATIC void
1056 xfs_fs_write_super(
1057         struct super_block      *sb)
1058 {
1059         if (!(sb->s_flags & MS_RDONLY))
1060                 xfs_sync(XFS_M(sb), SYNC_FSDATA);
1061         sb->s_dirt = 0;
1062 }
1063
1064 STATIC int
1065 xfs_fs_sync_super(
1066         struct super_block      *sb,
1067         int                     wait)
1068 {
1069         struct xfs_mount        *mp = XFS_M(sb);
1070         int                     error;
1071         int                     flags;
1072
1073         /*
1074          * Treat a sync operation like a freeze.  This is to work
1075          * around a race in sync_inodes() which works in two phases
1076          * - an asynchronous flush, which can write out an inode
1077          * without waiting for file size updates to complete, and a
1078          * synchronous flush, which wont do anything because the
1079          * async flush removed the inode's dirty flag.  Also
1080          * sync_inodes() will not see any files that just have
1081          * outstanding transactions to be flushed because we don't
1082          * dirty the Linux inode until after the transaction I/O
1083          * completes.
1084          */
1085         if (wait || unlikely(sb->s_frozen == SB_FREEZE_WRITE)) {
1086                 /*
1087                  * First stage of freeze - no more writers will make progress
1088                  * now we are here, so we flush delwri and delalloc buffers
1089                  * here, then wait for all I/O to complete.  Data is frozen at
1090                  * that point. Metadata is not frozen, transactions can still
1091                  * occur here so don't bother flushing the buftarg (i.e
1092                  * SYNC_QUIESCE) because it'll just get dirty again.
1093                  */
1094                 flags = SYNC_DATA_QUIESCE;
1095         } else
1096                 flags = SYNC_FSDATA;
1097
1098         error = xfs_sync(mp, flags);
1099         sb->s_dirt = 0;
1100
1101         if (unlikely(laptop_mode)) {
1102                 int     prev_sync_seq = mp->m_sync_seq;
1103
1104                 /*
1105                  * The disk must be active because we're syncing.
1106                  * We schedule xfssyncd now (now that the disk is
1107                  * active) instead of later (when it might not be).
1108                  */
1109                 wake_up_process(mp->m_sync_task);
1110                 /*
1111                  * We have to wait for the sync iteration to complete.
1112                  * If we don't, the disk activity caused by the sync
1113                  * will come after the sync is completed, and that
1114                  * triggers another sync from laptop mode.
1115                  */
1116                 wait_event(mp->m_wait_single_sync_task,
1117                                 mp->m_sync_seq != prev_sync_seq);
1118         }
1119
1120         return -error;
1121 }
1122
1123 STATIC int
1124 xfs_fs_statfs(
1125         struct dentry           *dentry,
1126         struct kstatfs          *statp)
1127 {
1128         struct xfs_mount        *mp = XFS_M(dentry->d_sb);
1129         xfs_sb_t                *sbp = &mp->m_sb;
1130         __uint64_t              fakeinos, id;
1131         xfs_extlen_t            lsize;
1132
1133         statp->f_type = XFS_SB_MAGIC;
1134         statp->f_namelen = MAXNAMELEN - 1;
1135
1136         id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
1137         statp->f_fsid.val[0] = (u32)id;
1138         statp->f_fsid.val[1] = (u32)(id >> 32);
1139
1140         xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
1141
1142         spin_lock(&mp->m_sb_lock);
1143         statp->f_bsize = sbp->sb_blocksize;
1144         lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
1145         statp->f_blocks = sbp->sb_dblocks - lsize;
1146         statp->f_bfree = statp->f_bavail =
1147                                 sbp->sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1148         fakeinos = statp->f_bfree << sbp->sb_inopblog;
1149 #if XFS_BIG_INUMS
1150         fakeinos += mp->m_inoadd;
1151 #endif
1152         statp->f_files =
1153             MIN(sbp->sb_icount + fakeinos, (__uint64_t)XFS_MAXINUMBER);
1154         if (mp->m_maxicount)
1155 #if XFS_BIG_INUMS
1156                 if (!mp->m_inoadd)
1157 #endif
1158                         statp->f_files = min_t(typeof(statp->f_files),
1159                                                 statp->f_files,
1160                                                 mp->m_maxicount);
1161         statp->f_ffree = statp->f_files - (sbp->sb_icount - sbp->sb_ifree);
1162         spin_unlock(&mp->m_sb_lock);
1163
1164         XFS_QM_DQSTATVFS(XFS_I(dentry->d_inode), statp);
1165         return 0;
1166 }
1167
1168 STATIC int
1169 xfs_fs_remount(
1170         struct super_block      *sb,
1171         int                     *flags,
1172         char                    *options)
1173 {
1174         struct xfs_mount        *mp = XFS_M(sb);
1175         substring_t             args[MAX_OPT_ARGS];
1176         char                    *p;
1177
1178         while ((p = strsep(&options, ",")) != NULL) {
1179                 int token;
1180
1181                 if (!*p)
1182                         continue;
1183
1184                 token = match_token(p, tokens, args);
1185                 switch (token) {
1186                 case Opt_barrier:
1187                         mp->m_flags |= XFS_MOUNT_BARRIER;
1188
1189                         /*
1190                          * Test if barriers are actually working if we can,
1191                          * else delay this check until the filesystem is
1192                          * marked writeable.
1193                          */
1194                         if (!(mp->m_flags & XFS_MOUNT_RDONLY))
1195                                 xfs_mountfs_check_barriers(mp);
1196                         break;
1197                 case Opt_nobarrier:
1198                         mp->m_flags &= ~XFS_MOUNT_BARRIER;
1199                         break;
1200                 default:
1201                         /*
1202                          * Logically we would return an error here to prevent
1203                          * users from believing they might have changed
1204                          * mount options using remount which can't be changed.
1205                          *
1206                          * But unfortunately mount(8) adds all options from
1207                          * mtab and fstab to the mount arguments in some cases
1208                          * so we can't blindly reject options, but have to
1209                          * check for each specified option if it actually
1210                          * differs from the currently set option and only
1211                          * reject it if that's the case.
1212                          *
1213                          * Until that is implemented we return success for
1214                          * every remount request, and silently ignore all
1215                          * options that we can't actually change.
1216                          */
1217 #if 0
1218                         printk(KERN_INFO
1219         "XFS: mount option \"%s\" not supported for remount\n", p);
1220                         return -EINVAL;
1221 #else
1222                         break;
1223 #endif
1224                 }
1225         }
1226
1227         /* rw/ro -> rw */
1228         if ((mp->m_flags & XFS_MOUNT_RDONLY) && !(*flags & MS_RDONLY)) {
1229                 mp->m_flags &= ~XFS_MOUNT_RDONLY;
1230                 if (mp->m_flags & XFS_MOUNT_BARRIER)
1231                         xfs_mountfs_check_barriers(mp);
1232         }
1233
1234         /* rw -> ro */
1235         if (!(mp->m_flags & XFS_MOUNT_RDONLY) && (*flags & MS_RDONLY)) {
1236                 xfs_filestream_flush(mp);
1237                 xfs_sync(mp, SYNC_DATA_QUIESCE);
1238                 xfs_attr_quiesce(mp);
1239                 mp->m_flags |= XFS_MOUNT_RDONLY;
1240         }
1241
1242         return 0;
1243 }
1244
1245 /*
1246  * Second stage of a freeze. The data is already frozen so we only
1247  * need to take care of themetadata. Once that's done write a dummy
1248  * record to dirty the log in case of a crash while frozen.
1249  */
1250 STATIC void
1251 xfs_fs_lockfs(
1252         struct super_block      *sb)
1253 {
1254         struct xfs_mount        *mp = XFS_M(sb);
1255
1256         xfs_attr_quiesce(mp);
1257         xfs_fs_log_dummy(mp);
1258 }
1259
1260 STATIC int
1261 xfs_fs_show_options(
1262         struct seq_file         *m,
1263         struct vfsmount         *mnt)
1264 {
1265         return -xfs_showargs(XFS_M(mnt->mnt_sb), m);
1266 }
1267
1268 STATIC int
1269 xfs_fs_quotasync(
1270         struct super_block      *sb,
1271         int                     type)
1272 {
1273         return -XFS_QM_QUOTACTL(XFS_M(sb), Q_XQUOTASYNC, 0, NULL);
1274 }
1275
1276 STATIC int
1277 xfs_fs_getxstate(
1278         struct super_block      *sb,
1279         struct fs_quota_stat    *fqs)
1280 {
1281         return -XFS_QM_QUOTACTL(XFS_M(sb), Q_XGETQSTAT, 0, (caddr_t)fqs);
1282 }
1283
1284 STATIC int
1285 xfs_fs_setxstate(
1286         struct super_block      *sb,
1287         unsigned int            flags,
1288         int                     op)
1289 {
1290         return -XFS_QM_QUOTACTL(XFS_M(sb), op, 0, (caddr_t)&flags);
1291 }
1292
1293 STATIC int
1294 xfs_fs_getxquota(
1295         struct super_block      *sb,
1296         int                     type,
1297         qid_t                   id,
1298         struct fs_disk_quota    *fdq)
1299 {
1300         return -XFS_QM_QUOTACTL(XFS_M(sb),
1301                                  (type == USRQUOTA) ? Q_XGETQUOTA :
1302                                   ((type == GRPQUOTA) ? Q_XGETGQUOTA :
1303                                    Q_XGETPQUOTA), id, (caddr_t)fdq);
1304 }
1305
1306 STATIC int
1307 xfs_fs_setxquota(
1308         struct super_block      *sb,
1309         int                     type,
1310         qid_t                   id,
1311         struct fs_disk_quota    *fdq)
1312 {
1313         return -XFS_QM_QUOTACTL(XFS_M(sb),
1314                                  (type == USRQUOTA) ? Q_XSETQLIM :
1315                                   ((type == GRPQUOTA) ? Q_XSETGQLIM :
1316                                    Q_XSETPQLIM), id, (caddr_t)fdq);
1317 }
1318
1319 /*
1320  * This function fills in xfs_mount_t fields based on mount args.
1321  * Note: the superblock has _not_ yet been read in.
1322  */
1323 STATIC int
1324 xfs_start_flags(
1325         struct xfs_mount_args   *ap,
1326         struct xfs_mount        *mp)
1327 {
1328         int                     error;
1329
1330         /* Values are in BBs */
1331         if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
1332                 /*
1333                  * At this point the superblock has not been read
1334                  * in, therefore we do not know the block size.
1335                  * Before the mount call ends we will convert
1336                  * these to FSBs.
1337                  */
1338                 mp->m_dalign = ap->sunit;
1339                 mp->m_swidth = ap->swidth;
1340         }
1341
1342         if (ap->logbufs != -1 &&
1343             ap->logbufs != 0 &&
1344             (ap->logbufs < XLOG_MIN_ICLOGS ||
1345              ap->logbufs > XLOG_MAX_ICLOGS)) {
1346                 cmn_err(CE_WARN,
1347                         "XFS: invalid logbufs value: %d [not %d-%d]",
1348                         ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
1349                 return XFS_ERROR(EINVAL);
1350         }
1351         mp->m_logbufs = ap->logbufs;
1352         if (ap->logbufsize != -1 &&
1353             ap->logbufsize !=  0 &&
1354             (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
1355              ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
1356              !is_power_of_2(ap->logbufsize))) {
1357                 cmn_err(CE_WARN,
1358         "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
1359                         ap->logbufsize);
1360                 return XFS_ERROR(EINVAL);
1361         }
1362
1363         error = ENOMEM;
1364
1365         mp->m_logbsize = ap->logbufsize;
1366         mp->m_fsname_len = strlen(ap->fsname) + 1;
1367
1368         mp->m_fsname = kstrdup(ap->fsname, GFP_KERNEL);
1369         if (!mp->m_fsname)
1370                 goto out;
1371
1372         if (ap->rtname[0]) {
1373                 mp->m_rtname = kstrdup(ap->rtname, GFP_KERNEL);
1374                 if (!mp->m_rtname)
1375                         goto out_free_fsname;
1376
1377         }
1378
1379         if (ap->logname[0]) {
1380                 mp->m_logname = kstrdup(ap->logname, GFP_KERNEL);
1381                 if (!mp->m_logname)
1382                         goto out_free_rtname;
1383         }
1384
1385         if (ap->flags & XFSMNT_WSYNC)
1386                 mp->m_flags |= XFS_MOUNT_WSYNC;
1387 #if XFS_BIG_INUMS
1388         if (ap->flags & XFSMNT_INO64) {
1389                 mp->m_flags |= XFS_MOUNT_INO64;
1390                 mp->m_inoadd = XFS_INO64_OFFSET;
1391         }
1392 #endif
1393         if (ap->flags & XFSMNT_RETERR)
1394                 mp->m_flags |= XFS_MOUNT_RETERR;
1395         if (ap->flags & XFSMNT_NOALIGN)
1396                 mp->m_flags |= XFS_MOUNT_NOALIGN;
1397         if (ap->flags & XFSMNT_SWALLOC)
1398                 mp->m_flags |= XFS_MOUNT_SWALLOC;
1399         if (ap->flags & XFSMNT_OSYNCISOSYNC)
1400                 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
1401         if (ap->flags & XFSMNT_32BITINODES)
1402                 mp->m_flags |= XFS_MOUNT_32BITINODES;
1403
1404         if (ap->flags & XFSMNT_IOSIZE) {
1405                 if (ap->iosizelog > XFS_MAX_IO_LOG ||
1406                     ap->iosizelog < XFS_MIN_IO_LOG) {
1407                         cmn_err(CE_WARN,
1408                 "XFS: invalid log iosize: %d [not %d-%d]",
1409                                 ap->iosizelog, XFS_MIN_IO_LOG,
1410                                 XFS_MAX_IO_LOG);
1411                         return XFS_ERROR(EINVAL);
1412                 }
1413
1414                 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
1415                 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
1416         }
1417
1418         if (ap->flags & XFSMNT_IKEEP)
1419                 mp->m_flags |= XFS_MOUNT_IKEEP;
1420         if (ap->flags & XFSMNT_DIRSYNC)
1421                 mp->m_flags |= XFS_MOUNT_DIRSYNC;
1422         if (ap->flags & XFSMNT_ATTR2)
1423                 mp->m_flags |= XFS_MOUNT_ATTR2;
1424         if (ap->flags & XFSMNT_NOATTR2)
1425                 mp->m_flags |= XFS_MOUNT_NOATTR2;
1426
1427         if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
1428                 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
1429
1430         /*
1431          * no recovery flag requires a read-only mount
1432          */
1433         if (ap->flags & XFSMNT_NORECOVERY) {
1434                 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
1435                         cmn_err(CE_WARN,
1436         "XFS: tried to mount a FS read-write without recovery!");
1437                         return XFS_ERROR(EINVAL);
1438                 }
1439                 mp->m_flags |= XFS_MOUNT_NORECOVERY;
1440         }
1441
1442         if (ap->flags & XFSMNT_NOUUID)
1443                 mp->m_flags |= XFS_MOUNT_NOUUID;
1444         if (ap->flags & XFSMNT_BARRIER)
1445                 mp->m_flags |= XFS_MOUNT_BARRIER;
1446         else
1447                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
1448
1449         if (ap->flags2 & XFSMNT2_FILESTREAMS)
1450                 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
1451
1452         if (ap->flags & XFSMNT_DMAPI)
1453                 mp->m_flags |= XFS_MOUNT_DMAPI;
1454         return 0;
1455
1456
1457  out_free_rtname:
1458         kfree(mp->m_rtname);
1459  out_free_fsname:
1460         kfree(mp->m_fsname);
1461  out:
1462         return error;
1463 }
1464
1465 /*
1466  * This function fills in xfs_mount_t fields based on mount args.
1467  * Note: the superblock _has_ now been read in.
1468  */
1469 STATIC int
1470 xfs_finish_flags(
1471         struct xfs_mount_args   *ap,
1472         struct xfs_mount        *mp)
1473 {
1474         int                     ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
1475
1476         /* Fail a mount where the logbuf is smaller then the log stripe */
1477         if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1478                 if ((ap->logbufsize <= 0) &&
1479                     (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
1480                         mp->m_logbsize = mp->m_sb.sb_logsunit;
1481                 } else if (ap->logbufsize > 0 &&
1482                            ap->logbufsize < mp->m_sb.sb_logsunit) {
1483                         cmn_err(CE_WARN,
1484         "XFS: logbuf size must be greater than or equal to log stripe size");
1485                         return XFS_ERROR(EINVAL);
1486                 }
1487         } else {
1488                 /* Fail a mount if the logbuf is larger than 32K */
1489                 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
1490                         cmn_err(CE_WARN,
1491         "XFS: logbuf size for version 1 logs must be 16K or 32K");
1492                         return XFS_ERROR(EINVAL);
1493                 }
1494         }
1495
1496         /*
1497          * mkfs'ed attr2 will turn on attr2 mount unless explicitly
1498          * told by noattr2 to turn it off
1499          */
1500         if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1501             !(ap->flags & XFSMNT_NOATTR2))
1502                 mp->m_flags |= XFS_MOUNT_ATTR2;
1503
1504         /*
1505          * prohibit r/w mounts of read-only filesystems
1506          */
1507         if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
1508                 cmn_err(CE_WARN,
1509         "XFS: cannot mount a read-only filesystem as read-write");
1510                 return XFS_ERROR(EROFS);
1511         }
1512
1513         /*
1514          * check for shared mount.
1515          */
1516         if (ap->flags & XFSMNT_SHARED) {
1517                 if (!xfs_sb_version_hasshared(&mp->m_sb))
1518                         return XFS_ERROR(EINVAL);
1519
1520                 /*
1521                  * For IRIX 6.5, shared mounts must have the shared
1522                  * version bit set, have the persistent readonly
1523                  * field set, must be version 0 and can only be mounted
1524                  * read-only.
1525                  */
1526                 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
1527                      (mp->m_sb.sb_shared_vn != 0))
1528                         return XFS_ERROR(EINVAL);
1529
1530                 mp->m_flags |= XFS_MOUNT_SHARED;
1531
1532                 /*
1533                  * Shared XFS V0 can't deal with DMI.  Return EINVAL.
1534                  */
1535                 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
1536                         return XFS_ERROR(EINVAL);
1537         }
1538
1539         if (ap->flags & XFSMNT_UQUOTA) {
1540                 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
1541                 if (ap->flags & XFSMNT_UQUOTAENF)
1542                         mp->m_qflags |= XFS_UQUOTA_ENFD;
1543         }
1544
1545         if (ap->flags & XFSMNT_GQUOTA) {
1546                 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
1547                 if (ap->flags & XFSMNT_GQUOTAENF)
1548                         mp->m_qflags |= XFS_OQUOTA_ENFD;
1549         } else if (ap->flags & XFSMNT_PQUOTA) {
1550                 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
1551                 if (ap->flags & XFSMNT_PQUOTAENF)
1552                         mp->m_qflags |= XFS_OQUOTA_ENFD;
1553         }
1554
1555         return 0;
1556 }
1557
1558 STATIC int
1559 xfs_fs_fill_super(
1560         struct super_block      *sb,
1561         void                    *data,
1562         int                     silent)
1563 {
1564         struct inode            *root;
1565         struct xfs_mount        *mp = NULL;
1566         struct xfs_mount_args   *args;
1567         int                     flags = 0, error = ENOMEM;
1568
1569         args = xfs_args_allocate(sb, silent);
1570         if (!args)
1571                 return -ENOMEM;
1572
1573         mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL);
1574         if (!mp)
1575                 goto out_free_args;
1576
1577         spin_lock_init(&mp->m_sb_lock);
1578         mutex_init(&mp->m_ilock);
1579         mutex_init(&mp->m_growlock);
1580         atomic_set(&mp->m_active_trans, 0);
1581         INIT_LIST_HEAD(&mp->m_sync_list);
1582         spin_lock_init(&mp->m_sync_lock);
1583         init_waitqueue_head(&mp->m_wait_single_sync_task);
1584
1585         mp->m_super = sb;
1586         sb->s_fs_info = mp;
1587
1588         if (sb->s_flags & MS_RDONLY)
1589                 mp->m_flags |= XFS_MOUNT_RDONLY;
1590
1591         error = xfs_parseargs(mp, (char *)data, args, 0);
1592         if (error)
1593                 goto out_free_mp;
1594
1595         sb_min_blocksize(sb, BBSIZE);
1596         sb->s_xattr = xfs_xattr_handlers;
1597         sb->s_export_op = &xfs_export_operations;
1598         sb->s_qcop = &xfs_quotactl_operations;
1599         sb->s_op = &xfs_super_operations;
1600
1601         error = xfs_dmops_get(mp, args);
1602         if (error)
1603                 goto out_free_mp;
1604         error = xfs_qmops_get(mp, args);
1605         if (error)
1606                 goto out_put_dmops;
1607
1608         if (args->flags & XFSMNT_QUIET)
1609                 flags |= XFS_MFSI_QUIET;
1610
1611         error = xfs_open_devices(mp, args);
1612         if (error)
1613                 goto out_put_qmops;
1614
1615         if (xfs_icsb_init_counters(mp))
1616                 mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
1617
1618         /*
1619          * Setup flags based on mount(2) options and then the superblock
1620          */
1621         error = xfs_start_flags(args, mp);
1622         if (error)
1623                 goto out_free_fsname;
1624         error = xfs_readsb(mp, flags);
1625         if (error)
1626                 goto out_free_fsname;
1627         error = xfs_finish_flags(args, mp);
1628         if (error)
1629                 goto out_free_sb;
1630
1631         error = xfs_setup_devices(mp);
1632         if (error)
1633                 goto out_free_sb;
1634
1635         if (mp->m_flags & XFS_MOUNT_BARRIER)
1636                 xfs_mountfs_check_barriers(mp);
1637
1638         error = xfs_filestream_mount(mp);
1639         if (error)
1640                 goto out_free_sb;
1641
1642         error = xfs_mountfs(mp);
1643         if (error)
1644                 goto out_filestream_unmount;
1645
1646         XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
1647
1648         sb->s_dirt = 1;
1649         sb->s_magic = XFS_SB_MAGIC;
1650         sb->s_blocksize = mp->m_sb.sb_blocksize;
1651         sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1652         sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
1653         sb->s_time_gran = 1;
1654         set_posix_acl_flag(sb);
1655
1656         root = igrab(VFS_I(mp->m_rootip));
1657         if (!root) {
1658                 error = ENOENT;
1659                 goto fail_unmount;
1660         }
1661         if (is_bad_inode(root)) {
1662                 error = EINVAL;
1663                 goto fail_vnrele;
1664         }
1665         sb->s_root = d_alloc_root(root);
1666         if (!sb->s_root) {
1667                 error = ENOMEM;
1668                 goto fail_vnrele;
1669         }
1670
1671         error = xfs_syncd_init(mp);
1672         if (error)
1673                 goto fail_vnrele;
1674
1675         xfs_itrace_exit(XFS_I(sb->s_root->d_inode));
1676
1677         kfree(args);
1678         return 0;
1679
1680  out_filestream_unmount:
1681         xfs_filestream_unmount(mp);
1682  out_free_sb:
1683         xfs_freesb(mp);
1684  out_free_fsname:
1685         xfs_free_fsname(mp);
1686         xfs_icsb_destroy_counters(mp);
1687         xfs_close_devices(mp);
1688  out_put_qmops:
1689         xfs_qmops_put(mp);
1690  out_put_dmops:
1691         xfs_dmops_put(mp);
1692  out_free_mp:
1693         kfree(mp);
1694  out_free_args:
1695         kfree(args);
1696         return -error;
1697
1698  fail_vnrele:
1699         if (sb->s_root) {
1700                 dput(sb->s_root);
1701                 sb->s_root = NULL;
1702         } else {
1703                 iput(root);
1704         }
1705
1706  fail_unmount:
1707         /*
1708          * Blow away any referenced inode in the filestreams cache.
1709          * This can and will cause log traffic as inodes go inactive
1710          * here.
1711          */
1712         xfs_filestream_unmount(mp);
1713
1714         XFS_bflush(mp->m_ddev_targp);
1715         error = xfs_unmount_flush(mp, 0);
1716         WARN_ON(error);
1717
1718         xfs_unmountfs(mp);
1719         goto out_free_sb;
1720 }
1721
1722 STATIC int
1723 xfs_fs_get_sb(
1724         struct file_system_type *fs_type,
1725         int                     flags,
1726         const char              *dev_name,
1727         void                    *data,
1728         struct vfsmount         *mnt)
1729 {
1730         return get_sb_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super,
1731                            mnt);
1732 }
1733
1734 static struct super_operations xfs_super_operations = {
1735         .alloc_inode            = xfs_fs_alloc_inode,
1736         .destroy_inode          = xfs_fs_destroy_inode,
1737         .write_inode            = xfs_fs_write_inode,
1738         .clear_inode            = xfs_fs_clear_inode,
1739         .put_super              = xfs_fs_put_super,
1740         .write_super            = xfs_fs_write_super,
1741         .sync_fs                = xfs_fs_sync_super,
1742         .write_super_lockfs     = xfs_fs_lockfs,
1743         .statfs                 = xfs_fs_statfs,
1744         .remount_fs             = xfs_fs_remount,
1745         .show_options           = xfs_fs_show_options,
1746 };
1747
1748 static struct quotactl_ops xfs_quotactl_operations = {
1749         .quota_sync             = xfs_fs_quotasync,
1750         .get_xstate             = xfs_fs_getxstate,
1751         .set_xstate             = xfs_fs_setxstate,
1752         .get_xquota             = xfs_fs_getxquota,
1753         .set_xquota             = xfs_fs_setxquota,
1754 };
1755
1756 static struct file_system_type xfs_fs_type = {
1757         .owner                  = THIS_MODULE,
1758         .name                   = "xfs",
1759         .get_sb                 = xfs_fs_get_sb,
1760         .kill_sb                = kill_block_super,
1761         .fs_flags               = FS_REQUIRES_DEV,
1762 };
1763
1764 STATIC int __init
1765 xfs_alloc_trace_bufs(void)
1766 {
1767 #ifdef XFS_ALLOC_TRACE
1768         xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_MAYFAIL);
1769         if (!xfs_alloc_trace_buf)
1770                 goto out;
1771 #endif
1772 #ifdef XFS_BMAP_TRACE
1773         xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_MAYFAIL);
1774         if (!xfs_bmap_trace_buf)
1775                 goto out_free_alloc_trace;
1776 #endif
1777 #ifdef XFS_BTREE_TRACE
1778         xfs_allocbt_trace_buf = ktrace_alloc(XFS_ALLOCBT_TRACE_SIZE,
1779                                              KM_MAYFAIL);
1780         if (!xfs_allocbt_trace_buf)
1781                 goto out_free_bmap_trace;
1782
1783         xfs_inobt_trace_buf = ktrace_alloc(XFS_INOBT_TRACE_SIZE, KM_MAYFAIL);
1784         if (!xfs_inobt_trace_buf)
1785                 goto out_free_allocbt_trace;
1786
1787         xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_MAYFAIL);
1788         if (!xfs_bmbt_trace_buf)
1789                 goto out_free_inobt_trace;
1790 #endif
1791 #ifdef XFS_ATTR_TRACE
1792         xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_MAYFAIL);
1793         if (!xfs_attr_trace_buf)
1794                 goto out_free_bmbt_trace;
1795 #endif
1796 #ifdef XFS_DIR2_TRACE
1797         xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_MAYFAIL);
1798         if (!xfs_dir2_trace_buf)
1799                 goto out_free_attr_trace;
1800 #endif
1801
1802         return 0;
1803
1804 #ifdef XFS_DIR2_TRACE
1805  out_free_attr_trace:
1806 #endif
1807 #ifdef XFS_ATTR_TRACE
1808         ktrace_free(xfs_attr_trace_buf);
1809  out_free_bmbt_trace:
1810 #endif
1811 #ifdef XFS_BTREE_TRACE
1812         ktrace_free(xfs_bmbt_trace_buf);
1813  out_free_inobt_trace:
1814         ktrace_free(xfs_inobt_trace_buf);
1815  out_free_allocbt_trace:
1816         ktrace_free(xfs_allocbt_trace_buf);
1817  out_free_bmap_trace:
1818 #endif
1819 #ifdef XFS_BMAP_TRACE
1820         ktrace_free(xfs_bmap_trace_buf);
1821  out_free_alloc_trace:
1822 #endif
1823 #ifdef XFS_ALLOC_TRACE
1824         ktrace_free(xfs_alloc_trace_buf);
1825  out:
1826 #endif
1827         return -ENOMEM;
1828 }
1829
1830 STATIC void
1831 xfs_free_trace_bufs(void)
1832 {
1833 #ifdef XFS_DIR2_TRACE
1834         ktrace_free(xfs_dir2_trace_buf);
1835 #endif
1836 #ifdef XFS_ATTR_TRACE
1837         ktrace_free(xfs_attr_trace_buf);
1838 #endif
1839 #ifdef XFS_BTREE_TRACE
1840         ktrace_free(xfs_bmbt_trace_buf);
1841         ktrace_free(xfs_inobt_trace_buf);
1842         ktrace_free(xfs_allocbt_trace_buf);
1843 #endif
1844 #ifdef XFS_BMAP_TRACE
1845         ktrace_free(xfs_bmap_trace_buf);
1846 #endif
1847 #ifdef XFS_ALLOC_TRACE
1848         ktrace_free(xfs_alloc_trace_buf);
1849 #endif
1850 }
1851
1852 STATIC int __init
1853 xfs_init_zones(void)
1854 {
1855         xfs_vnode_zone = kmem_zone_init_flags(sizeof(struct inode), "xfs_vnode",
1856                                         KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
1857                                         KM_ZONE_SPREAD,
1858                                         xfs_fs_inode_init_once);
1859         if (!xfs_vnode_zone)
1860                 goto out;
1861
1862         xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
1863         if (!xfs_ioend_zone)
1864                 goto out_destroy_vnode_zone;
1865
1866         xfs_ioend_pool = mempool_create_slab_pool(4 * MAX_BUF_PER_PAGE,
1867                                                   xfs_ioend_zone);
1868         if (!xfs_ioend_pool)
1869                 goto out_destroy_ioend_zone;
1870
1871         xfs_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t),
1872                                                 "xfs_log_ticket");
1873         if (!xfs_log_ticket_zone)
1874                 goto out_destroy_ioend_pool;
1875
1876         xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
1877                                                 "xfs_bmap_free_item");
1878         if (!xfs_bmap_free_item_zone)
1879                 goto out_destroy_log_ticket_zone;
1880         xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
1881                                                 "xfs_btree_cur");
1882         if (!xfs_btree_cur_zone)
1883                 goto out_destroy_bmap_free_item_zone;
1884
1885         xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t),
1886                                                 "xfs_da_state");
1887         if (!xfs_da_state_zone)
1888                 goto out_destroy_btree_cur_zone;
1889
1890         xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
1891         if (!xfs_dabuf_zone)
1892                 goto out_destroy_da_state_zone;
1893
1894         xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
1895         if (!xfs_ifork_zone)
1896                 goto out_destroy_dabuf_zone;
1897
1898         xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
1899         if (!xfs_trans_zone)
1900                 goto out_destroy_ifork_zone;
1901
1902         /*
1903          * The size of the zone allocated buf log item is the maximum
1904          * size possible under XFS.  This wastes a little bit of memory,
1905          * but it is much faster.
1906          */
1907         xfs_buf_item_zone = kmem_zone_init((sizeof(xfs_buf_log_item_t) +
1908                                 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
1909                                   NBWORD) * sizeof(int))), "xfs_buf_item");
1910         if (!xfs_buf_item_zone)
1911                 goto out_destroy_trans_zone;
1912
1913         xfs_efd_zone = kmem_zone_init((sizeof(xfs_efd_log_item_t) +
1914                         ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
1915                                  sizeof(xfs_extent_t))), "xfs_efd_item");
1916         if (!xfs_efd_zone)
1917                 goto out_destroy_buf_item_zone;
1918
1919         xfs_efi_zone = kmem_zone_init((sizeof(xfs_efi_log_item_t) +
1920                         ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
1921                                 sizeof(xfs_extent_t))), "xfs_efi_item");
1922         if (!xfs_efi_zone)
1923                 goto out_destroy_efd_zone;
1924
1925         xfs_inode_zone =
1926                 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
1927                                         KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
1928                                         KM_ZONE_SPREAD, xfs_inode_init_once);
1929         if (!xfs_inode_zone)
1930                 goto out_destroy_efi_zone;
1931
1932         xfs_ili_zone =
1933                 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
1934                                         KM_ZONE_SPREAD, NULL);
1935         if (!xfs_ili_zone)
1936                 goto out_destroy_inode_zone;
1937
1938 #ifdef CONFIG_XFS_POSIX_ACL
1939         xfs_acl_zone = kmem_zone_init(sizeof(xfs_acl_t), "xfs_acl");
1940         if (!xfs_acl_zone)
1941                 goto out_destroy_ili_zone;
1942 #endif
1943
1944         return 0;
1945
1946 #ifdef CONFIG_XFS_POSIX_ACL
1947  out_destroy_ili_zone:
1948 #endif
1949         kmem_zone_destroy(xfs_ili_zone);
1950  out_destroy_inode_zone:
1951         kmem_zone_destroy(xfs_inode_zone);
1952  out_destroy_efi_zone:
1953         kmem_zone_destroy(xfs_efi_zone);
1954  out_destroy_efd_zone:
1955         kmem_zone_destroy(xfs_efd_zone);
1956  out_destroy_buf_item_zone:
1957         kmem_zone_destroy(xfs_buf_item_zone);
1958  out_destroy_trans_zone:
1959         kmem_zone_destroy(xfs_trans_zone);
1960  out_destroy_ifork_zone:
1961         kmem_zone_destroy(xfs_ifork_zone);
1962  out_destroy_dabuf_zone:
1963         kmem_zone_destroy(xfs_dabuf_zone);
1964  out_destroy_da_state_zone:
1965         kmem_zone_destroy(xfs_da_state_zone);
1966  out_destroy_btree_cur_zone:
1967         kmem_zone_destroy(xfs_btree_cur_zone);
1968  out_destroy_bmap_free_item_zone:
1969         kmem_zone_destroy(xfs_bmap_free_item_zone);
1970  out_destroy_log_ticket_zone:
1971         kmem_zone_destroy(xfs_log_ticket_zone);
1972  out_destroy_ioend_pool:
1973         mempool_destroy(xfs_ioend_pool);
1974  out_destroy_ioend_zone:
1975         kmem_zone_destroy(xfs_ioend_zone);
1976  out_destroy_vnode_zone:
1977         kmem_zone_destroy(xfs_vnode_zone);
1978  out:
1979         return -ENOMEM;
1980 }
1981
1982 STATIC void
1983 xfs_destroy_zones(void)
1984 {
1985 #ifdef CONFIG_XFS_POSIX_ACL
1986         kmem_zone_destroy(xfs_acl_zone);
1987 #endif
1988         kmem_zone_destroy(xfs_ili_zone);
1989         kmem_zone_destroy(xfs_inode_zone);
1990         kmem_zone_destroy(xfs_efi_zone);
1991         kmem_zone_destroy(xfs_efd_zone);
1992         kmem_zone_destroy(xfs_buf_item_zone);
1993         kmem_zone_destroy(xfs_trans_zone);
1994         kmem_zone_destroy(xfs_ifork_zone);
1995         kmem_zone_destroy(xfs_dabuf_zone);
1996         kmem_zone_destroy(xfs_da_state_zone);
1997         kmem_zone_destroy(xfs_btree_cur_zone);
1998         kmem_zone_destroy(xfs_bmap_free_item_zone);
1999         kmem_zone_destroy(xfs_log_ticket_zone);
2000         mempool_destroy(xfs_ioend_pool);
2001         kmem_zone_destroy(xfs_ioend_zone);
2002         kmem_zone_destroy(xfs_vnode_zone);
2003
2004 }
2005
2006 STATIC int __init
2007 init_xfs_fs(void)
2008 {
2009         int                     error;
2010         static char             message[] __initdata = KERN_INFO \
2011                 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
2012
2013         printk(message);
2014
2015         ktrace_init(64);
2016         vn_init();
2017         xfs_dir_startup();
2018
2019         error = xfs_init_zones();
2020         if (error)
2021                 goto out;
2022
2023         error = xfs_alloc_trace_bufs();
2024         if (error)
2025                 goto out_destroy_zones;
2026
2027         error = xfs_mru_cache_init();
2028         if (error)
2029                 goto out_free_trace_buffers;
2030
2031         error = xfs_filestream_init();
2032         if (error)
2033                 goto out_mru_cache_uninit;
2034
2035         error = xfs_buf_init();
2036         if (error)
2037                 goto out_filestream_uninit;
2038
2039         error = xfs_init_procfs();
2040         if (error)
2041                 goto out_buf_terminate;
2042
2043         error = xfs_sysctl_register();
2044         if (error)
2045                 goto out_cleanup_procfs;
2046
2047         vfs_initquota();
2048
2049         error = register_filesystem(&xfs_fs_type);
2050         if (error)
2051                 goto out_sysctl_unregister;
2052         return 0;
2053
2054  out_sysctl_unregister:
2055         xfs_sysctl_unregister();
2056  out_cleanup_procfs:
2057         xfs_cleanup_procfs();
2058  out_buf_terminate:
2059         xfs_buf_terminate();
2060  out_filestream_uninit:
2061         xfs_filestream_uninit();
2062  out_mru_cache_uninit:
2063         xfs_mru_cache_uninit();
2064  out_free_trace_buffers:
2065         xfs_free_trace_bufs();
2066  out_destroy_zones:
2067         xfs_destroy_zones();
2068  out:
2069         return error;
2070 }
2071
2072 STATIC void __exit
2073 exit_xfs_fs(void)
2074 {
2075         vfs_exitquota();
2076         unregister_filesystem(&xfs_fs_type);
2077         xfs_sysctl_unregister();
2078         xfs_cleanup_procfs();
2079         xfs_buf_terminate();
2080         xfs_filestream_uninit();
2081         xfs_mru_cache_uninit();
2082         xfs_free_trace_bufs();
2083         xfs_destroy_zones();
2084         ktrace_uninit();
2085 }
2086
2087 module_init(init_xfs_fs);
2088 module_exit(exit_xfs_fs);
2089
2090 MODULE_AUTHOR("Silicon Graphics, Inc.");
2091 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
2092 MODULE_LICENSE("GPL");