Merge git://git.infradead.org/mtd-2.6
[pandora-kernel.git] / 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
19 #include "xfs.h"
20 #include "xfs_bit.h"
21 #include "xfs_log.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_attr.h"
48 #include "xfs_buf_item.h"
49 #include "xfs_utils.h"
50 #include "xfs_vnodeops.h"
51 #include "xfs_version.h"
52 #include "xfs_log_priv.h"
53 #include "xfs_trans_priv.h"
54 #include "xfs_filestream.h"
55 #include "xfs_da_btree.h"
56 #include "xfs_extfree_item.h"
57 #include "xfs_mru_cache.h"
58 #include "xfs_inode_item.h"
59 #include "xfs_sync.h"
60 #include "xfs_trace.h"
61
62 #include <linux/namei.h>
63 #include <linux/init.h>
64 #include <linux/slab.h>
65 #include <linux/mount.h>
66 #include <linux/mempool.h>
67 #include <linux/writeback.h>
68 #include <linux/kthread.h>
69 #include <linux/freezer.h>
70 #include <linux/parser.h>
71
72 static const struct super_operations xfs_super_operations;
73 static kmem_zone_t *xfs_ioend_zone;
74 mempool_t *xfs_ioend_pool;
75
76 #define MNTOPT_LOGBUFS  "logbufs"       /* number of XFS log buffers */
77 #define MNTOPT_LOGBSIZE "logbsize"      /* size of XFS log buffers */
78 #define MNTOPT_LOGDEV   "logdev"        /* log device */
79 #define MNTOPT_RTDEV    "rtdev"         /* realtime I/O device */
80 #define MNTOPT_BIOSIZE  "biosize"       /* log2 of preferred buffered io size */
81 #define MNTOPT_WSYNC    "wsync"         /* safe-mode nfs compatible mount */
82 #define MNTOPT_NOALIGN  "noalign"       /* turn off stripe alignment */
83 #define MNTOPT_SWALLOC  "swalloc"       /* turn on stripe width allocation */
84 #define MNTOPT_SUNIT    "sunit"         /* data volume stripe unit */
85 #define MNTOPT_SWIDTH   "swidth"        /* data volume stripe width */
86 #define MNTOPT_NOUUID   "nouuid"        /* ignore filesystem UUID */
87 #define MNTOPT_MTPT     "mtpt"          /* filesystem mount point */
88 #define MNTOPT_GRPID    "grpid"         /* group-ID from parent directory */
89 #define MNTOPT_NOGRPID  "nogrpid"       /* group-ID from current process */
90 #define MNTOPT_BSDGROUPS    "bsdgroups"    /* group-ID from parent directory */
91 #define MNTOPT_SYSVGROUPS   "sysvgroups"   /* group-ID from current process */
92 #define MNTOPT_ALLOCSIZE    "allocsize"    /* preferred allocation size */
93 #define MNTOPT_NORECOVERY   "norecovery"   /* don't run XFS recovery */
94 #define MNTOPT_BARRIER  "barrier"       /* use writer barriers for log write and
95                                          * unwritten extent conversion */
96 #define MNTOPT_NOBARRIER "nobarrier"    /* .. disable */
97 #define MNTOPT_OSYNCISOSYNC "osyncisosync" /* o_sync is REALLY o_sync */
98 #define MNTOPT_64BITINODE   "inode64"   /* inodes can be allocated anywhere */
99 #define MNTOPT_IKEEP    "ikeep"         /* do not free empty inode clusters */
100 #define MNTOPT_NOIKEEP  "noikeep"       /* free empty inode clusters */
101 #define MNTOPT_LARGEIO     "largeio"    /* report large I/O sizes in stat() */
102 #define MNTOPT_NOLARGEIO   "nolargeio"  /* do not report large I/O sizes
103                                          * in stat(). */
104 #define MNTOPT_ATTR2    "attr2"         /* do use attr2 attribute format */
105 #define MNTOPT_NOATTR2  "noattr2"       /* do not use attr2 attribute format */
106 #define MNTOPT_FILESTREAM  "filestreams" /* use filestreams allocator */
107 #define MNTOPT_QUOTA    "quota"         /* disk quotas (user) */
108 #define MNTOPT_NOQUOTA  "noquota"       /* no quotas */
109 #define MNTOPT_USRQUOTA "usrquota"      /* user quota enabled */
110 #define MNTOPT_GRPQUOTA "grpquota"      /* group quota enabled */
111 #define MNTOPT_PRJQUOTA "prjquota"      /* project quota enabled */
112 #define MNTOPT_UQUOTA   "uquota"        /* user quota (IRIX variant) */
113 #define MNTOPT_GQUOTA   "gquota"        /* group quota (IRIX variant) */
114 #define MNTOPT_PQUOTA   "pquota"        /* project quota (IRIX variant) */
115 #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
116 #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
117 #define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
118 #define MNTOPT_QUOTANOENF  "qnoenforce" /* same as uqnoenforce */
119 #define MNTOPT_DMAPI    "dmapi"         /* DMI enabled (DMAPI / XDSM) */
120 #define MNTOPT_XDSM     "xdsm"          /* DMI enabled (DMAPI / XDSM) */
121 #define MNTOPT_DMI      "dmi"           /* DMI enabled (DMAPI / XDSM) */
122
123 /*
124  * Table driven mount option parser.
125  *
126  * Currently only used for remount, but it will be used for mount
127  * in the future, too.
128  */
129 enum {
130         Opt_barrier, Opt_nobarrier, Opt_err
131 };
132
133 static const match_table_t tokens = {
134         {Opt_barrier, "barrier"},
135         {Opt_nobarrier, "nobarrier"},
136         {Opt_err, NULL}
137 };
138
139
140 STATIC unsigned long
141 suffix_strtoul(char *s, char **endp, unsigned int base)
142 {
143         int     last, shift_left_factor = 0;
144         char    *value = s;
145
146         last = strlen(value) - 1;
147         if (value[last] == 'K' || value[last] == 'k') {
148                 shift_left_factor = 10;
149                 value[last] = '\0';
150         }
151         if (value[last] == 'M' || value[last] == 'm') {
152                 shift_left_factor = 20;
153                 value[last] = '\0';
154         }
155         if (value[last] == 'G' || value[last] == 'g') {
156                 shift_left_factor = 30;
157                 value[last] = '\0';
158         }
159
160         return simple_strtoul((const char *)s, endp, base) << shift_left_factor;
161 }
162
163 /*
164  * This function fills in xfs_mount_t fields based on mount args.
165  * Note: the superblock has _not_ yet been read in.
166  *
167  * Note that this function leaks the various device name allocations on
168  * failure.  The caller takes care of them.
169  */
170 STATIC int
171 xfs_parseargs(
172         struct xfs_mount        *mp,
173         char                    *options,
174         char                    **mtpt)
175 {
176         struct super_block      *sb = mp->m_super;
177         char                    *this_char, *value, *eov;
178         int                     dsunit = 0;
179         int                     dswidth = 0;
180         int                     iosize = 0;
181         int                     dmapi_implies_ikeep = 1;
182         __uint8_t               iosizelog = 0;
183
184         /*
185          * Copy binary VFS mount flags we are interested in.
186          */
187         if (sb->s_flags & MS_RDONLY)
188                 mp->m_flags |= XFS_MOUNT_RDONLY;
189         if (sb->s_flags & MS_DIRSYNC)
190                 mp->m_flags |= XFS_MOUNT_DIRSYNC;
191         if (sb->s_flags & MS_SYNCHRONOUS)
192                 mp->m_flags |= XFS_MOUNT_WSYNC;
193
194         /*
195          * Set some default flags that could be cleared by the mount option
196          * parsing.
197          */
198         mp->m_flags |= XFS_MOUNT_BARRIER;
199         mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
200         mp->m_flags |= XFS_MOUNT_SMALL_INUMS;
201
202         /*
203          * These can be overridden by the mount option parsing.
204          */
205         mp->m_logbufs = -1;
206         mp->m_logbsize = -1;
207
208         if (!options)
209                 goto done;
210
211         while ((this_char = strsep(&options, ",")) != NULL) {
212                 if (!*this_char)
213                         continue;
214                 if ((value = strchr(this_char, '=')) != NULL)
215                         *value++ = 0;
216
217                 if (!strcmp(this_char, MNTOPT_LOGBUFS)) {
218                         if (!value || !*value) {
219                                 cmn_err(CE_WARN,
220                                         "XFS: %s option requires an argument",
221                                         this_char);
222                                 return EINVAL;
223                         }
224                         mp->m_logbufs = simple_strtoul(value, &eov, 10);
225                 } else if (!strcmp(this_char, MNTOPT_LOGBSIZE)) {
226                         if (!value || !*value) {
227                                 cmn_err(CE_WARN,
228                                         "XFS: %s option requires an argument",
229                                         this_char);
230                                 return EINVAL;
231                         }
232                         mp->m_logbsize = suffix_strtoul(value, &eov, 10);
233                 } else if (!strcmp(this_char, MNTOPT_LOGDEV)) {
234                         if (!value || !*value) {
235                                 cmn_err(CE_WARN,
236                                         "XFS: %s option requires an argument",
237                                         this_char);
238                                 return EINVAL;
239                         }
240                         mp->m_logname = kstrndup(value, MAXNAMELEN, GFP_KERNEL);
241                         if (!mp->m_logname)
242                                 return ENOMEM;
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                         *mtpt = kstrndup(value, MAXNAMELEN, GFP_KERNEL);
251                         if (!*mtpt)
252                                 return ENOMEM;
253                 } else if (!strcmp(this_char, MNTOPT_RTDEV)) {
254                         if (!value || !*value) {
255                                 cmn_err(CE_WARN,
256                                         "XFS: %s option requires an argument",
257                                         this_char);
258                                 return EINVAL;
259                         }
260                         mp->m_rtname = kstrndup(value, MAXNAMELEN, GFP_KERNEL);
261                         if (!mp->m_rtname)
262                                 return ENOMEM;
263                 } else if (!strcmp(this_char, MNTOPT_BIOSIZE)) {
264                         if (!value || !*value) {
265                                 cmn_err(CE_WARN,
266                                         "XFS: %s option requires an argument",
267                                         this_char);
268                                 return EINVAL;
269                         }
270                         iosize = simple_strtoul(value, &eov, 10);
271                         iosizelog = ffs(iosize) - 1;
272                 } else if (!strcmp(this_char, MNTOPT_ALLOCSIZE)) {
273                         if (!value || !*value) {
274                                 cmn_err(CE_WARN,
275                                         "XFS: %s option requires an argument",
276                                         this_char);
277                                 return EINVAL;
278                         }
279                         iosize = suffix_strtoul(value, &eov, 10);
280                         iosizelog = ffs(iosize) - 1;
281                 } else if (!strcmp(this_char, MNTOPT_GRPID) ||
282                            !strcmp(this_char, MNTOPT_BSDGROUPS)) {
283                         mp->m_flags |= XFS_MOUNT_GRPID;
284                 } else if (!strcmp(this_char, MNTOPT_NOGRPID) ||
285                            !strcmp(this_char, MNTOPT_SYSVGROUPS)) {
286                         mp->m_flags &= ~XFS_MOUNT_GRPID;
287                 } else if (!strcmp(this_char, MNTOPT_WSYNC)) {
288                         mp->m_flags |= XFS_MOUNT_WSYNC;
289                 } else if (!strcmp(this_char, MNTOPT_OSYNCISOSYNC)) {
290                         mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
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                                 cmn_err(CE_WARN,
300                                         "XFS: %s option requires an argument",
301                                         this_char);
302                                 return EINVAL;
303                         }
304                         dsunit = simple_strtoul(value, &eov, 10);
305                 } else if (!strcmp(this_char, MNTOPT_SWIDTH)) {
306                         if (!value || !*value) {
307                                 cmn_err(CE_WARN,
308                                         "XFS: %s option requires an argument",
309                                         this_char);
310                                 return EINVAL;
311                         }
312                         dswidth = simple_strtoul(value, &eov, 10);
313                 } else if (!strcmp(this_char, MNTOPT_64BITINODE)) {
314                         mp->m_flags &= ~XFS_MOUNT_SMALL_INUMS;
315 #if !XFS_BIG_INUMS
316                         cmn_err(CE_WARN,
317                                 "XFS: %s option not allowed on this system",
318                                 this_char);
319                         return EINVAL;
320 #endif
321                 } else if (!strcmp(this_char, MNTOPT_NOUUID)) {
322                         mp->m_flags |= XFS_MOUNT_NOUUID;
323                 } else if (!strcmp(this_char, MNTOPT_BARRIER)) {
324                         mp->m_flags |= XFS_MOUNT_BARRIER;
325                 } else if (!strcmp(this_char, MNTOPT_NOBARRIER)) {
326                         mp->m_flags &= ~XFS_MOUNT_BARRIER;
327                 } else if (!strcmp(this_char, MNTOPT_IKEEP)) {
328                         mp->m_flags |= XFS_MOUNT_IKEEP;
329                 } else if (!strcmp(this_char, MNTOPT_NOIKEEP)) {
330                         dmapi_implies_ikeep = 0;
331                         mp->m_flags &= ~XFS_MOUNT_IKEEP;
332                 } else if (!strcmp(this_char, MNTOPT_LARGEIO)) {
333                         mp->m_flags &= ~XFS_MOUNT_COMPAT_IOSIZE;
334                 } else if (!strcmp(this_char, MNTOPT_NOLARGEIO)) {
335                         mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
336                 } else if (!strcmp(this_char, MNTOPT_ATTR2)) {
337                         mp->m_flags |= XFS_MOUNT_ATTR2;
338                 } else if (!strcmp(this_char, MNTOPT_NOATTR2)) {
339                         mp->m_flags &= ~XFS_MOUNT_ATTR2;
340                         mp->m_flags |= XFS_MOUNT_NOATTR2;
341                 } else if (!strcmp(this_char, MNTOPT_FILESTREAM)) {
342                         mp->m_flags |= XFS_MOUNT_FILESTREAMS;
343                 } else if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
344                         mp->m_qflags &= ~(XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE |
345                                           XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE |
346                                           XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE |
347                                           XFS_UQUOTA_ENFD | XFS_OQUOTA_ENFD);
348                 } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
349                            !strcmp(this_char, MNTOPT_UQUOTA) ||
350                            !strcmp(this_char, MNTOPT_USRQUOTA)) {
351                         mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE |
352                                          XFS_UQUOTA_ENFD);
353                 } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
354                            !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
355                         mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
356                         mp->m_qflags &= ~XFS_UQUOTA_ENFD;
357                 } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
358                            !strcmp(this_char, MNTOPT_PRJQUOTA)) {
359                         mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE |
360                                          XFS_OQUOTA_ENFD);
361                 } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
362                         mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
363                         mp->m_qflags &= ~XFS_OQUOTA_ENFD;
364                 } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
365                            !strcmp(this_char, MNTOPT_GRPQUOTA)) {
366                         mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE |
367                                          XFS_OQUOTA_ENFD);
368                 } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
369                         mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
370                         mp->m_qflags &= ~XFS_OQUOTA_ENFD;
371                 } else if (!strcmp(this_char, MNTOPT_DMAPI)) {
372                         mp->m_flags |= XFS_MOUNT_DMAPI;
373                 } else if (!strcmp(this_char, MNTOPT_XDSM)) {
374                         mp->m_flags |= XFS_MOUNT_DMAPI;
375                 } else if (!strcmp(this_char, MNTOPT_DMI)) {
376                         mp->m_flags |= XFS_MOUNT_DMAPI;
377                 } else if (!strcmp(this_char, "ihashsize")) {
378                         cmn_err(CE_WARN,
379         "XFS: ihashsize no longer used, option is deprecated.");
380                 } else if (!strcmp(this_char, "osyncisdsync")) {
381                         /* no-op, this is now the default */
382                         cmn_err(CE_WARN,
383         "XFS: osyncisdsync is now the default, option is deprecated.");
384                 } else if (!strcmp(this_char, "irixsgid")) {
385                         cmn_err(CE_WARN,
386         "XFS: irixsgid is now a sysctl(2) variable, option is deprecated.");
387                 } else {
388                         cmn_err(CE_WARN,
389                                 "XFS: unknown mount option [%s].", this_char);
390                         return EINVAL;
391                 }
392         }
393
394         /*
395          * no recovery flag requires a read-only mount
396          */
397         if ((mp->m_flags & XFS_MOUNT_NORECOVERY) &&
398             !(mp->m_flags & XFS_MOUNT_RDONLY)) {
399                 cmn_err(CE_WARN, "XFS: no-recovery mounts must be read-only.");
400                 return EINVAL;
401         }
402
403         if ((mp->m_flags & XFS_MOUNT_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 #ifndef CONFIG_XFS_QUOTA
410         if (XFS_IS_QUOTA_RUNNING(mp)) {
411                 cmn_err(CE_WARN,
412                         "XFS: quota support not available in this kernel.");
413                 return EINVAL;
414         }
415 #endif
416
417         if ((mp->m_qflags & (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE)) &&
418             (mp->m_qflags & (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE))) {
419                 cmn_err(CE_WARN,
420                         "XFS: cannot mount with both project and group quota");
421                 return EINVAL;
422         }
423
424         if ((mp->m_flags & XFS_MOUNT_DMAPI) && (!*mtpt || *mtpt[0] == '\0')) {
425                 printk("XFS: %s option needs the mount point option as well\n",
426                         MNTOPT_DMAPI);
427                 return EINVAL;
428         }
429
430         if ((dsunit && !dswidth) || (!dsunit && dswidth)) {
431                 cmn_err(CE_WARN,
432                         "XFS: sunit and swidth must be specified together");
433                 return EINVAL;
434         }
435
436         if (dsunit && (dswidth % dsunit != 0)) {
437                 cmn_err(CE_WARN,
438         "XFS: stripe width (%d) must be a multiple of the stripe unit (%d)",
439                         dswidth, dsunit);
440                 return EINVAL;
441         }
442
443         /*
444          * Applications using DMI filesystems often expect the
445          * inode generation number to be monotonically increasing.
446          * If we delete inode chunks we break this assumption, so
447          * keep unused inode chunks on disk for DMI filesystems
448          * until we come up with a better solution.
449          * Note that if "ikeep" or "noikeep" mount options are
450          * supplied, then they are honored.
451          */
452         if ((mp->m_flags & XFS_MOUNT_DMAPI) && dmapi_implies_ikeep)
453                 mp->m_flags |= XFS_MOUNT_IKEEP;
454
455 done:
456         if (!(mp->m_flags & XFS_MOUNT_NOALIGN)) {
457                 /*
458                  * At this point the superblock has not been read
459                  * in, therefore we do not know the block size.
460                  * Before the mount call ends we will convert
461                  * these to FSBs.
462                  */
463                 if (dsunit) {
464                         mp->m_dalign = dsunit;
465                         mp->m_flags |= XFS_MOUNT_RETERR;
466                 }
467
468                 if (dswidth)
469                         mp->m_swidth = dswidth;
470         }
471
472         if (mp->m_logbufs != -1 &&
473             mp->m_logbufs != 0 &&
474             (mp->m_logbufs < XLOG_MIN_ICLOGS ||
475              mp->m_logbufs > XLOG_MAX_ICLOGS)) {
476                 cmn_err(CE_WARN,
477                         "XFS: invalid logbufs value: %d [not %d-%d]",
478                         mp->m_logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
479                 return XFS_ERROR(EINVAL);
480         }
481         if (mp->m_logbsize != -1 &&
482             mp->m_logbsize !=  0 &&
483             (mp->m_logbsize < XLOG_MIN_RECORD_BSIZE ||
484              mp->m_logbsize > XLOG_MAX_RECORD_BSIZE ||
485              !is_power_of_2(mp->m_logbsize))) {
486                 cmn_err(CE_WARN,
487         "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
488                         mp->m_logbsize);
489                 return XFS_ERROR(EINVAL);
490         }
491
492         mp->m_fsname = kstrndup(sb->s_id, MAXNAMELEN, GFP_KERNEL);
493         if (!mp->m_fsname)
494                 return ENOMEM;
495         mp->m_fsname_len = strlen(mp->m_fsname) + 1;
496
497         if (iosizelog) {
498                 if (iosizelog > XFS_MAX_IO_LOG ||
499                     iosizelog < XFS_MIN_IO_LOG) {
500                         cmn_err(CE_WARN,
501                 "XFS: invalid log iosize: %d [not %d-%d]",
502                                 iosizelog, XFS_MIN_IO_LOG,
503                                 XFS_MAX_IO_LOG);
504                         return XFS_ERROR(EINVAL);
505                 }
506
507                 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
508                 mp->m_readio_log = iosizelog;
509                 mp->m_writeio_log = iosizelog;
510         }
511
512         return 0;
513 }
514
515 struct proc_xfs_info {
516         int     flag;
517         char    *str;
518 };
519
520 STATIC int
521 xfs_showargs(
522         struct xfs_mount        *mp,
523         struct seq_file         *m)
524 {
525         static struct proc_xfs_info xfs_info_set[] = {
526                 /* the few simple ones we can get from the mount struct */
527                 { XFS_MOUNT_IKEEP,              "," MNTOPT_IKEEP },
528                 { XFS_MOUNT_WSYNC,              "," MNTOPT_WSYNC },
529                 { XFS_MOUNT_NOALIGN,            "," MNTOPT_NOALIGN },
530                 { XFS_MOUNT_SWALLOC,            "," MNTOPT_SWALLOC },
531                 { XFS_MOUNT_NOUUID,             "," MNTOPT_NOUUID },
532                 { XFS_MOUNT_NORECOVERY,         "," MNTOPT_NORECOVERY },
533                 { XFS_MOUNT_OSYNCISOSYNC,       "," MNTOPT_OSYNCISOSYNC },
534                 { XFS_MOUNT_ATTR2,              "," MNTOPT_ATTR2 },
535                 { XFS_MOUNT_FILESTREAMS,        "," MNTOPT_FILESTREAM },
536                 { XFS_MOUNT_DMAPI,              "," MNTOPT_DMAPI },
537                 { XFS_MOUNT_GRPID,              "," MNTOPT_GRPID },
538                 { 0, NULL }
539         };
540         static struct proc_xfs_info xfs_info_unset[] = {
541                 /* the few simple ones we can get from the mount struct */
542                 { XFS_MOUNT_COMPAT_IOSIZE,      "," MNTOPT_LARGEIO },
543                 { XFS_MOUNT_BARRIER,            "," MNTOPT_NOBARRIER },
544                 { XFS_MOUNT_SMALL_INUMS,        "," MNTOPT_64BITINODE },
545                 { 0, NULL }
546         };
547         struct proc_xfs_info    *xfs_infop;
548
549         for (xfs_infop = xfs_info_set; xfs_infop->flag; xfs_infop++) {
550                 if (mp->m_flags & xfs_infop->flag)
551                         seq_puts(m, xfs_infop->str);
552         }
553         for (xfs_infop = xfs_info_unset; xfs_infop->flag; xfs_infop++) {
554                 if (!(mp->m_flags & xfs_infop->flag))
555                         seq_puts(m, xfs_infop->str);
556         }
557
558         if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
559                 seq_printf(m, "," MNTOPT_ALLOCSIZE "=%dk",
560                                 (int)(1 << mp->m_writeio_log) >> 10);
561
562         if (mp->m_logbufs > 0)
563                 seq_printf(m, "," MNTOPT_LOGBUFS "=%d", mp->m_logbufs);
564         if (mp->m_logbsize > 0)
565                 seq_printf(m, "," MNTOPT_LOGBSIZE "=%dk", mp->m_logbsize >> 10);
566
567         if (mp->m_logname)
568                 seq_printf(m, "," MNTOPT_LOGDEV "=%s", mp->m_logname);
569         if (mp->m_rtname)
570                 seq_printf(m, "," MNTOPT_RTDEV "=%s", mp->m_rtname);
571
572         if (mp->m_dalign > 0)
573                 seq_printf(m, "," MNTOPT_SUNIT "=%d",
574                                 (int)XFS_FSB_TO_BB(mp, mp->m_dalign));
575         if (mp->m_swidth > 0)
576                 seq_printf(m, "," MNTOPT_SWIDTH "=%d",
577                                 (int)XFS_FSB_TO_BB(mp, mp->m_swidth));
578
579         if (mp->m_qflags & (XFS_UQUOTA_ACCT|XFS_UQUOTA_ENFD))
580                 seq_puts(m, "," MNTOPT_USRQUOTA);
581         else if (mp->m_qflags & XFS_UQUOTA_ACCT)
582                 seq_puts(m, "," MNTOPT_UQUOTANOENF);
583
584         /* Either project or group quotas can be active, not both */
585
586         if (mp->m_qflags & XFS_PQUOTA_ACCT) {
587                 if (mp->m_qflags & XFS_OQUOTA_ENFD)
588                         seq_puts(m, "," MNTOPT_PRJQUOTA);
589                 else
590                         seq_puts(m, "," MNTOPT_PQUOTANOENF);
591         } else if (mp->m_qflags & XFS_GQUOTA_ACCT) {
592                 if (mp->m_qflags & XFS_OQUOTA_ENFD)
593                         seq_puts(m, "," MNTOPT_GRPQUOTA);
594                 else
595                         seq_puts(m, "," MNTOPT_GQUOTANOENF);
596         }
597
598         if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
599                 seq_puts(m, "," MNTOPT_NOQUOTA);
600
601         return 0;
602 }
603 __uint64_t
604 xfs_max_file_offset(
605         unsigned int            blockshift)
606 {
607         unsigned int            pagefactor = 1;
608         unsigned int            bitshift = BITS_PER_LONG - 1;
609
610         /* Figure out maximum filesize, on Linux this can depend on
611          * the filesystem blocksize (on 32 bit platforms).
612          * __block_prepare_write does this in an [unsigned] long...
613          *      page->index << (PAGE_CACHE_SHIFT - bbits)
614          * So, for page sized blocks (4K on 32 bit platforms),
615          * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
616          *      (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
617          * but for smaller blocksizes it is less (bbits = log2 bsize).
618          * Note1: get_block_t takes a long (implicit cast from above)
619          * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
620          * can optionally convert the [unsigned] long from above into
621          * an [unsigned] long long.
622          */
623
624 #if BITS_PER_LONG == 32
625 # if defined(CONFIG_LBDAF)
626         ASSERT(sizeof(sector_t) == 8);
627         pagefactor = PAGE_CACHE_SIZE;
628         bitshift = BITS_PER_LONG;
629 # else
630         pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
631 # endif
632 #endif
633
634         return (((__uint64_t)pagefactor) << bitshift) - 1;
635 }
636
637 STATIC int
638 xfs_blkdev_get(
639         xfs_mount_t             *mp,
640         const char              *name,
641         struct block_device     **bdevp)
642 {
643         int                     error = 0;
644
645         *bdevp = open_bdev_exclusive(name, FMODE_READ|FMODE_WRITE, mp);
646         if (IS_ERR(*bdevp)) {
647                 error = PTR_ERR(*bdevp);
648                 printk("XFS: Invalid device [%s], error=%d\n", name, error);
649         }
650
651         return -error;
652 }
653
654 STATIC void
655 xfs_blkdev_put(
656         struct block_device     *bdev)
657 {
658         if (bdev)
659                 close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
660 }
661
662 /*
663  * Try to write out the superblock using barriers.
664  */
665 STATIC int
666 xfs_barrier_test(
667         xfs_mount_t     *mp)
668 {
669         xfs_buf_t       *sbp = xfs_getsb(mp, 0);
670         int             error;
671
672         XFS_BUF_UNDONE(sbp);
673         XFS_BUF_UNREAD(sbp);
674         XFS_BUF_UNDELAYWRITE(sbp);
675         XFS_BUF_WRITE(sbp);
676         XFS_BUF_UNASYNC(sbp);
677         XFS_BUF_ORDERED(sbp);
678
679         xfsbdstrat(mp, sbp);
680         error = xfs_iowait(sbp);
681
682         /*
683          * Clear all the flags we set and possible error state in the
684          * buffer.  We only did the write to try out whether barriers
685          * worked and shouldn't leave any traces in the superblock
686          * buffer.
687          */
688         XFS_BUF_DONE(sbp);
689         XFS_BUF_ERROR(sbp, 0);
690         XFS_BUF_UNORDERED(sbp);
691
692         xfs_buf_relse(sbp);
693         return error;
694 }
695
696 STATIC void
697 xfs_mountfs_check_barriers(xfs_mount_t *mp)
698 {
699         int error;
700
701         if (mp->m_logdev_targp != mp->m_ddev_targp) {
702                 xfs_fs_cmn_err(CE_NOTE, mp,
703                   "Disabling barriers, not supported with external log device");
704                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
705                 return;
706         }
707
708         if (xfs_readonly_buftarg(mp->m_ddev_targp)) {
709                 xfs_fs_cmn_err(CE_NOTE, mp,
710                   "Disabling barriers, underlying device is readonly");
711                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
712                 return;
713         }
714
715         error = xfs_barrier_test(mp);
716         if (error) {
717                 xfs_fs_cmn_err(CE_NOTE, mp,
718                   "Disabling barriers, trial barrier write failed");
719                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
720                 return;
721         }
722 }
723
724 void
725 xfs_blkdev_issue_flush(
726         xfs_buftarg_t           *buftarg)
727 {
728         blkdev_issue_flush(buftarg->bt_bdev, NULL);
729 }
730
731 STATIC void
732 xfs_close_devices(
733         struct xfs_mount        *mp)
734 {
735         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
736                 struct block_device *logdev = mp->m_logdev_targp->bt_bdev;
737                 xfs_free_buftarg(mp, mp->m_logdev_targp);
738                 xfs_blkdev_put(logdev);
739         }
740         if (mp->m_rtdev_targp) {
741                 struct block_device *rtdev = mp->m_rtdev_targp->bt_bdev;
742                 xfs_free_buftarg(mp, mp->m_rtdev_targp);
743                 xfs_blkdev_put(rtdev);
744         }
745         xfs_free_buftarg(mp, mp->m_ddev_targp);
746 }
747
748 /*
749  * The file system configurations are:
750  *      (1) device (partition) with data and internal log
751  *      (2) logical volume with data and log subvolumes.
752  *      (3) logical volume with data, log, and realtime subvolumes.
753  *
754  * We only have to handle opening the log and realtime volumes here if
755  * they are present.  The data subvolume has already been opened by
756  * get_sb_bdev() and is stored in sb->s_bdev.
757  */
758 STATIC int
759 xfs_open_devices(
760         struct xfs_mount        *mp)
761 {
762         struct block_device     *ddev = mp->m_super->s_bdev;
763         struct block_device     *logdev = NULL, *rtdev = NULL;
764         int                     error;
765
766         /*
767          * Open real time and log devices - order is important.
768          */
769         if (mp->m_logname) {
770                 error = xfs_blkdev_get(mp, mp->m_logname, &logdev);
771                 if (error)
772                         goto out;
773         }
774
775         if (mp->m_rtname) {
776                 error = xfs_blkdev_get(mp, mp->m_rtname, &rtdev);
777                 if (error)
778                         goto out_close_logdev;
779
780                 if (rtdev == ddev || rtdev == logdev) {
781                         cmn_err(CE_WARN,
782         "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
783                         error = EINVAL;
784                         goto out_close_rtdev;
785                 }
786         }
787
788         /*
789          * Setup xfs_mount buffer target pointers
790          */
791         error = ENOMEM;
792         mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0, mp->m_fsname);
793         if (!mp->m_ddev_targp)
794                 goto out_close_rtdev;
795
796         if (rtdev) {
797                 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1, mp->m_fsname);
798                 if (!mp->m_rtdev_targp)
799                         goto out_free_ddev_targ;
800         }
801
802         if (logdev && logdev != ddev) {
803                 mp->m_logdev_targp = xfs_alloc_buftarg(logdev, 1, mp->m_fsname);
804                 if (!mp->m_logdev_targp)
805                         goto out_free_rtdev_targ;
806         } else {
807                 mp->m_logdev_targp = mp->m_ddev_targp;
808         }
809
810         return 0;
811
812  out_free_rtdev_targ:
813         if (mp->m_rtdev_targp)
814                 xfs_free_buftarg(mp, mp->m_rtdev_targp);
815  out_free_ddev_targ:
816         xfs_free_buftarg(mp, mp->m_ddev_targp);
817  out_close_rtdev:
818         if (rtdev)
819                 xfs_blkdev_put(rtdev);
820  out_close_logdev:
821         if (logdev && logdev != ddev)
822                 xfs_blkdev_put(logdev);
823  out:
824         return error;
825 }
826
827 /*
828  * Setup xfs_mount buffer target pointers based on superblock
829  */
830 STATIC int
831 xfs_setup_devices(
832         struct xfs_mount        *mp)
833 {
834         int                     error;
835
836         error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
837                                     mp->m_sb.sb_sectsize);
838         if (error)
839                 return error;
840
841         if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) {
842                 unsigned int    log_sector_size = BBSIZE;
843
844                 if (xfs_sb_version_hassector(&mp->m_sb))
845                         log_sector_size = mp->m_sb.sb_logsectsize;
846                 error = xfs_setsize_buftarg(mp->m_logdev_targp,
847                                             mp->m_sb.sb_blocksize,
848                                             log_sector_size);
849                 if (error)
850                         return error;
851         }
852         if (mp->m_rtdev_targp) {
853                 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
854                                             mp->m_sb.sb_blocksize,
855                                             mp->m_sb.sb_sectsize);
856                 if (error)
857                         return error;
858         }
859
860         return 0;
861 }
862
863 /*
864  * XFS AIL push thread support
865  */
866 void
867 xfsaild_wakeup(
868         struct xfs_ail          *ailp,
869         xfs_lsn_t               threshold_lsn)
870 {
871         ailp->xa_target = threshold_lsn;
872         wake_up_process(ailp->xa_task);
873 }
874
875 STATIC int
876 xfsaild(
877         void    *data)
878 {
879         struct xfs_ail  *ailp = data;
880         xfs_lsn_t       last_pushed_lsn = 0;
881         long            tout = 0; /* milliseconds */
882
883         while (!kthread_should_stop()) {
884                 schedule_timeout_interruptible(tout ?
885                                 msecs_to_jiffies(tout) : MAX_SCHEDULE_TIMEOUT);
886
887                 /* swsusp */
888                 try_to_freeze();
889
890                 ASSERT(ailp->xa_mount->m_log);
891                 if (XFS_FORCED_SHUTDOWN(ailp->xa_mount))
892                         continue;
893
894                 tout = xfsaild_push(ailp, &last_pushed_lsn);
895         }
896
897         return 0;
898 }       /* xfsaild */
899
900 int
901 xfsaild_start(
902         struct xfs_ail  *ailp)
903 {
904         ailp->xa_target = 0;
905         ailp->xa_task = kthread_run(xfsaild, ailp, "xfsaild/%s",
906                                     ailp->xa_mount->m_fsname);
907         if (IS_ERR(ailp->xa_task))
908                 return -PTR_ERR(ailp->xa_task);
909         return 0;
910 }
911
912 void
913 xfsaild_stop(
914         struct xfs_ail  *ailp)
915 {
916         kthread_stop(ailp->xa_task);
917 }
918
919
920 /* Catch misguided souls that try to use this interface on XFS */
921 STATIC struct inode *
922 xfs_fs_alloc_inode(
923         struct super_block      *sb)
924 {
925         BUG();
926         return NULL;
927 }
928
929 /*
930  * Now that the generic code is guaranteed not to be accessing
931  * the linux inode, we can reclaim the inode.
932  */
933 STATIC void
934 xfs_fs_destroy_inode(
935         struct inode            *inode)
936 {
937         struct xfs_inode        *ip = XFS_I(inode);
938
939         xfs_itrace_entry(ip);
940
941         XFS_STATS_INC(vn_reclaim);
942
943         /* bad inode, get out here ASAP */
944         if (is_bad_inode(inode))
945                 goto out_reclaim;
946
947         xfs_ioend_wait(ip);
948
949         ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
950
951         /*
952          * We should never get here with one of the reclaim flags already set.
953          */
954         ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIMABLE));
955         ASSERT_ALWAYS(!xfs_iflags_test(ip, XFS_IRECLAIM));
956
957         /*
958          * We always use background reclaim here because even if the
959          * inode is clean, it still may be under IO and hence we have
960          * to take the flush lock. The background reclaim path handles
961          * this more efficiently than we can here, so simply let background
962          * reclaim tear down all inodes.
963          */
964 out_reclaim:
965         xfs_inode_set_reclaim_tag(ip);
966 }
967
968 /*
969  * Slab object creation initialisation for the XFS inode.
970  * This covers only the idempotent fields in the XFS inode;
971  * all other fields need to be initialised on allocation
972  * from the slab. This avoids the need to repeatedly intialise
973  * fields in the xfs inode that left in the initialise state
974  * when freeing the inode.
975  */
976 STATIC void
977 xfs_fs_inode_init_once(
978         void                    *inode)
979 {
980         struct xfs_inode        *ip = inode;
981
982         memset(ip, 0, sizeof(struct xfs_inode));
983
984         /* vfs inode */
985         inode_init_once(VFS_I(ip));
986
987         /* xfs inode */
988         atomic_set(&ip->i_iocount, 0);
989         atomic_set(&ip->i_pincount, 0);
990         spin_lock_init(&ip->i_flags_lock);
991         init_waitqueue_head(&ip->i_ipin_wait);
992         /*
993          * Because we want to use a counting completion, complete
994          * the flush completion once to allow a single access to
995          * the flush completion without blocking.
996          */
997         init_completion(&ip->i_flush);
998         complete(&ip->i_flush);
999
1000         mrlock_init(&ip->i_lock, MRLOCK_ALLOW_EQUAL_PRI|MRLOCK_BARRIER,
1001                      "xfsino", ip->i_ino);
1002 }
1003
1004 /*
1005  * Dirty the XFS inode when mark_inode_dirty_sync() is called so that
1006  * we catch unlogged VFS level updates to the inode. Care must be taken
1007  * here - the transaction code calls mark_inode_dirty_sync() to mark the
1008  * VFS inode dirty in a transaction and clears the i_update_core field;
1009  * it must clear the field after calling mark_inode_dirty_sync() to
1010  * correctly indicate that the dirty state has been propagated into the
1011  * inode log item.
1012  *
1013  * We need the barrier() to maintain correct ordering between unlogged
1014  * updates and the transaction commit code that clears the i_update_core
1015  * field. This requires all updates to be completed before marking the
1016  * inode dirty.
1017  */
1018 STATIC void
1019 xfs_fs_dirty_inode(
1020         struct inode    *inode)
1021 {
1022         barrier();
1023         XFS_I(inode)->i_update_core = 1;
1024 }
1025
1026 STATIC int
1027 xfs_log_inode(
1028         struct xfs_inode        *ip)
1029 {
1030         struct xfs_mount        *mp = ip->i_mount;
1031         struct xfs_trans        *tp;
1032         int                     error;
1033
1034         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1035         tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
1036         error = xfs_trans_reserve(tp, 0, XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
1037
1038         if (error) {
1039                 xfs_trans_cancel(tp, 0);
1040                 /* we need to return with the lock hold shared */
1041                 xfs_ilock(ip, XFS_ILOCK_SHARED);
1042                 return error;
1043         }
1044
1045         xfs_ilock(ip, XFS_ILOCK_EXCL);
1046
1047         /*
1048          * Note - it's possible that we might have pushed ourselves out of the
1049          * way during trans_reserve which would flush the inode.  But there's
1050          * no guarantee that the inode buffer has actually gone out yet (it's
1051          * delwri).  Plus the buffer could be pinned anyway if it's part of
1052          * an inode in another recent transaction.  So we play it safe and
1053          * fire off the transaction anyway.
1054          */
1055         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1056         xfs_trans_ihold(tp, ip);
1057         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1058         xfs_trans_set_sync(tp);
1059         error = xfs_trans_commit(tp, 0);
1060         xfs_ilock_demote(ip, XFS_ILOCK_EXCL);
1061
1062         return error;
1063 }
1064
1065 STATIC int
1066 xfs_fs_write_inode(
1067         struct inode            *inode,
1068         struct writeback_control *wbc)
1069 {
1070         struct xfs_inode        *ip = XFS_I(inode);
1071         struct xfs_mount        *mp = ip->i_mount;
1072         int                     error = EAGAIN;
1073
1074         xfs_itrace_entry(ip);
1075
1076         if (XFS_FORCED_SHUTDOWN(mp))
1077                 return XFS_ERROR(EIO);
1078
1079         if (wbc->sync_mode == WB_SYNC_ALL) {
1080                 /*
1081                  * Make sure the inode has hit stable storage.  By using the
1082                  * log and the fsync transactions we reduce the IOs we have
1083                  * to do here from two (log and inode) to just the log.
1084                  *
1085                  * Note: We still need to do a delwri write of the inode after
1086                  * this to flush it to the backing buffer so that bulkstat
1087                  * works properly if this is the first time the inode has been
1088                  * written.  Because we hold the ilock atomically over the
1089                  * transaction commit and the inode flush we are guaranteed
1090                  * that the inode is not pinned when it returns. If the flush
1091                  * lock is already held, then the inode has already been
1092                  * flushed once and we don't need to flush it again.  Hence
1093                  * the code will only flush the inode if it isn't already
1094                  * being flushed.
1095                  */
1096                 xfs_ioend_wait(ip);
1097                 xfs_ilock(ip, XFS_ILOCK_SHARED);
1098                 if (ip->i_update_core) {
1099                         error = xfs_log_inode(ip);
1100                         if (error)
1101                                 goto out_unlock;
1102                 }
1103         } else {
1104                 /*
1105                  * We make this non-blocking if the inode is contended, return
1106                  * EAGAIN to indicate to the caller that they did not succeed.
1107                  * This prevents the flush path from blocking on inodes inside
1108                  * another operation right now, they get caught later by xfs_sync.
1109                  */
1110                 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED))
1111                         goto out;
1112         }
1113
1114         if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip))
1115                 goto out_unlock;
1116
1117         /*
1118          * Now we have the flush lock and the inode is not pinned, we can check
1119          * if the inode is really clean as we know that there are no pending
1120          * transaction completions, it is not waiting on the delayed write
1121          * queue and there is no IO in progress.
1122          */
1123         if (xfs_inode_clean(ip)) {
1124                 xfs_ifunlock(ip);
1125                 error = 0;
1126                 goto out_unlock;
1127         }
1128         error = xfs_iflush(ip, 0);
1129
1130  out_unlock:
1131         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1132  out:
1133         /*
1134          * if we failed to write out the inode then mark
1135          * it dirty again so we'll try again later.
1136          */
1137         if (error)
1138                 xfs_mark_inode_dirty_sync(ip);
1139         return -error;
1140 }
1141
1142 STATIC void
1143 xfs_fs_clear_inode(
1144         struct inode            *inode)
1145 {
1146         xfs_inode_t             *ip = XFS_I(inode);
1147
1148         xfs_itrace_entry(ip);
1149         XFS_STATS_INC(vn_rele);
1150         XFS_STATS_INC(vn_remove);
1151         XFS_STATS_DEC(vn_active);
1152
1153         /*
1154          * The iolock is used by the file system to coordinate reads,
1155          * writes, and block truncates.  Up to this point the lock
1156          * protected concurrent accesses by users of the inode.  But
1157          * from here forward we're doing some final processing of the
1158          * inode because we're done with it, and although we reuse the
1159          * iolock for protection it is really a distinct lock class
1160          * (in the lockdep sense) from before.  To keep lockdep happy
1161          * (and basically indicate what we are doing), we explicitly
1162          * re-init the iolock here.
1163          */
1164         ASSERT(!rwsem_is_locked(&ip->i_iolock.mr_lock));
1165         mrlock_init(&ip->i_iolock, MRLOCK_BARRIER, "xfsio", ip->i_ino);
1166
1167         xfs_inactive(ip);
1168 }
1169
1170 STATIC void
1171 xfs_free_fsname(
1172         struct xfs_mount        *mp)
1173 {
1174         kfree(mp->m_fsname);
1175         kfree(mp->m_rtname);
1176         kfree(mp->m_logname);
1177 }
1178
1179 STATIC void
1180 xfs_fs_put_super(
1181         struct super_block      *sb)
1182 {
1183         struct xfs_mount        *mp = XFS_M(sb);
1184
1185         xfs_syncd_stop(mp);
1186
1187         if (!(sb->s_flags & MS_RDONLY)) {
1188                 /*
1189                  * XXX(hch): this should be SYNC_WAIT.
1190                  *
1191                  * Or more likely not needed at all because the VFS is already
1192                  * calling ->sync_fs after shutting down all filestem
1193                  * operations and just before calling ->put_super.
1194                  */
1195                 xfs_sync_data(mp, 0);
1196                 xfs_sync_attr(mp, 0);
1197         }
1198
1199         XFS_SEND_PREUNMOUNT(mp);
1200
1201         /*
1202          * Blow away any referenced inode in the filestreams cache.
1203          * This can and will cause log traffic as inodes go inactive
1204          * here.
1205          */
1206         xfs_filestream_unmount(mp);
1207
1208         XFS_bflush(mp->m_ddev_targp);
1209
1210         XFS_SEND_UNMOUNT(mp);
1211
1212         xfs_unmountfs(mp);
1213         xfs_freesb(mp);
1214         xfs_inode_shrinker_unregister(mp);
1215         xfs_icsb_destroy_counters(mp);
1216         xfs_close_devices(mp);
1217         xfs_dmops_put(mp);
1218         xfs_free_fsname(mp);
1219         kfree(mp);
1220 }
1221
1222 STATIC int
1223 xfs_fs_sync_fs(
1224         struct super_block      *sb,
1225         int                     wait)
1226 {
1227         struct xfs_mount        *mp = XFS_M(sb);
1228         int                     error;
1229
1230         /*
1231          * Not much we can do for the first async pass.  Writing out the
1232          * superblock would be counter-productive as we are going to redirty
1233          * when writing out other data and metadata (and writing out a single
1234          * block is quite fast anyway).
1235          *
1236          * Try to asynchronously kick off quota syncing at least.
1237          */
1238         if (!wait) {
1239                 xfs_qm_sync(mp, SYNC_TRYLOCK);
1240                 return 0;
1241         }
1242
1243         error = xfs_quiesce_data(mp);
1244         if (error)
1245                 return -error;
1246
1247         if (laptop_mode) {
1248                 int     prev_sync_seq = mp->m_sync_seq;
1249
1250                 /*
1251                  * The disk must be active because we're syncing.
1252                  * We schedule xfssyncd now (now that the disk is
1253                  * active) instead of later (when it might not be).
1254                  */
1255                 wake_up_process(mp->m_sync_task);
1256                 /*
1257                  * We have to wait for the sync iteration to complete.
1258                  * If we don't, the disk activity caused by the sync
1259                  * will come after the sync is completed, and that
1260                  * triggers another sync from laptop mode.
1261                  */
1262                 wait_event(mp->m_wait_single_sync_task,
1263                                 mp->m_sync_seq != prev_sync_seq);
1264         }
1265
1266         return 0;
1267 }
1268
1269 STATIC int
1270 xfs_fs_statfs(
1271         struct dentry           *dentry,
1272         struct kstatfs          *statp)
1273 {
1274         struct xfs_mount        *mp = XFS_M(dentry->d_sb);
1275         xfs_sb_t                *sbp = &mp->m_sb;
1276         struct xfs_inode        *ip = XFS_I(dentry->d_inode);
1277         __uint64_t              fakeinos, id;
1278         xfs_extlen_t            lsize;
1279
1280         statp->f_type = XFS_SB_MAGIC;
1281         statp->f_namelen = MAXNAMELEN - 1;
1282
1283         id = huge_encode_dev(mp->m_ddev_targp->bt_dev);
1284         statp->f_fsid.val[0] = (u32)id;
1285         statp->f_fsid.val[1] = (u32)(id >> 32);
1286
1287         xfs_icsb_sync_counters(mp, XFS_ICSB_LAZY_COUNT);
1288
1289         spin_lock(&mp->m_sb_lock);
1290         statp->f_bsize = sbp->sb_blocksize;
1291         lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
1292         statp->f_blocks = sbp->sb_dblocks - lsize;
1293         statp->f_bfree = statp->f_bavail =
1294                                 sbp->sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
1295         fakeinos = statp->f_bfree << sbp->sb_inopblog;
1296         statp->f_files =
1297             MIN(sbp->sb_icount + fakeinos, (__uint64_t)XFS_MAXINUMBER);
1298         if (mp->m_maxicount)
1299                 statp->f_files = min_t(typeof(statp->f_files),
1300                                         statp->f_files,
1301                                         mp->m_maxicount);
1302         statp->f_ffree = statp->f_files - (sbp->sb_icount - sbp->sb_ifree);
1303         spin_unlock(&mp->m_sb_lock);
1304
1305         if ((ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) ||
1306             ((mp->m_qflags & (XFS_PQUOTA_ACCT|XFS_OQUOTA_ENFD))) ==
1307                               (XFS_PQUOTA_ACCT|XFS_OQUOTA_ENFD))
1308                 xfs_qm_statvfs(ip, statp);
1309         return 0;
1310 }
1311
1312 STATIC void
1313 xfs_save_resvblks(struct xfs_mount *mp)
1314 {
1315         __uint64_t resblks = 0;
1316
1317         mp->m_resblks_save = mp->m_resblks;
1318         xfs_reserve_blocks(mp, &resblks, NULL);
1319 }
1320
1321 STATIC void
1322 xfs_restore_resvblks(struct xfs_mount *mp)
1323 {
1324         __uint64_t resblks;
1325
1326         if (mp->m_resblks_save) {
1327                 resblks = mp->m_resblks_save;
1328                 mp->m_resblks_save = 0;
1329         } else
1330                 resblks = xfs_default_resblks(mp);
1331
1332         xfs_reserve_blocks(mp, &resblks, NULL);
1333 }
1334
1335 STATIC int
1336 xfs_fs_remount(
1337         struct super_block      *sb,
1338         int                     *flags,
1339         char                    *options)
1340 {
1341         struct xfs_mount        *mp = XFS_M(sb);
1342         substring_t             args[MAX_OPT_ARGS];
1343         char                    *p;
1344         int                     error;
1345
1346         while ((p = strsep(&options, ",")) != NULL) {
1347                 int token;
1348
1349                 if (!*p)
1350                         continue;
1351
1352                 token = match_token(p, tokens, args);
1353                 switch (token) {
1354                 case Opt_barrier:
1355                         mp->m_flags |= XFS_MOUNT_BARRIER;
1356
1357                         /*
1358                          * Test if barriers are actually working if we can,
1359                          * else delay this check until the filesystem is
1360                          * marked writeable.
1361                          */
1362                         if (!(mp->m_flags & XFS_MOUNT_RDONLY))
1363                                 xfs_mountfs_check_barriers(mp);
1364                         break;
1365                 case Opt_nobarrier:
1366                         mp->m_flags &= ~XFS_MOUNT_BARRIER;
1367                         break;
1368                 default:
1369                         /*
1370                          * Logically we would return an error here to prevent
1371                          * users from believing they might have changed
1372                          * mount options using remount which can't be changed.
1373                          *
1374                          * But unfortunately mount(8) adds all options from
1375                          * mtab and fstab to the mount arguments in some cases
1376                          * so we can't blindly reject options, but have to
1377                          * check for each specified option if it actually
1378                          * differs from the currently set option and only
1379                          * reject it if that's the case.
1380                          *
1381                          * Until that is implemented we return success for
1382                          * every remount request, and silently ignore all
1383                          * options that we can't actually change.
1384                          */
1385 #if 0
1386                         printk(KERN_INFO
1387         "XFS: mount option \"%s\" not supported for remount\n", p);
1388                         return -EINVAL;
1389 #else
1390                         break;
1391 #endif
1392                 }
1393         }
1394
1395         /* ro -> rw */
1396         if ((mp->m_flags & XFS_MOUNT_RDONLY) && !(*flags & MS_RDONLY)) {
1397                 mp->m_flags &= ~XFS_MOUNT_RDONLY;
1398                 if (mp->m_flags & XFS_MOUNT_BARRIER)
1399                         xfs_mountfs_check_barriers(mp);
1400
1401                 /*
1402                  * If this is the first remount to writeable state we
1403                  * might have some superblock changes to update.
1404                  */
1405                 if (mp->m_update_flags) {
1406                         error = xfs_mount_log_sb(mp, mp->m_update_flags);
1407                         if (error) {
1408                                 cmn_err(CE_WARN,
1409                                         "XFS: failed to write sb changes");
1410                                 return error;
1411                         }
1412                         mp->m_update_flags = 0;
1413                 }
1414
1415                 /*
1416                  * Fill out the reserve pool if it is empty. Use the stashed
1417                  * value if it is non-zero, otherwise go with the default.
1418                  */
1419                 xfs_restore_resvblks(mp);
1420         }
1421
1422         /* rw -> ro */
1423         if (!(mp->m_flags & XFS_MOUNT_RDONLY) && (*flags & MS_RDONLY)) {
1424                 /*
1425                  * After we have synced the data but before we sync the
1426                  * metadata, we need to free up the reserve block pool so that
1427                  * the used block count in the superblock on disk is correct at
1428                  * the end of the remount. Stash the current reserve pool size
1429                  * so that if we get remounted rw, we can return it to the same
1430                  * size.
1431                  */
1432
1433                 xfs_quiesce_data(mp);
1434                 xfs_save_resvblks(mp);
1435                 xfs_quiesce_attr(mp);
1436                 mp->m_flags |= XFS_MOUNT_RDONLY;
1437         }
1438
1439         return 0;
1440 }
1441
1442 /*
1443  * Second stage of a freeze. The data is already frozen so we only
1444  * need to take care of the metadata. Once that's done write a dummy
1445  * record to dirty the log in case of a crash while frozen.
1446  */
1447 STATIC int
1448 xfs_fs_freeze(
1449         struct super_block      *sb)
1450 {
1451         struct xfs_mount        *mp = XFS_M(sb);
1452
1453         xfs_save_resvblks(mp);
1454         xfs_quiesce_attr(mp);
1455         return -xfs_fs_log_dummy(mp);
1456 }
1457
1458 STATIC int
1459 xfs_fs_unfreeze(
1460         struct super_block      *sb)
1461 {
1462         struct xfs_mount        *mp = XFS_M(sb);
1463
1464         xfs_restore_resvblks(mp);
1465         return 0;
1466 }
1467
1468 STATIC int
1469 xfs_fs_show_options(
1470         struct seq_file         *m,
1471         struct vfsmount         *mnt)
1472 {
1473         return -xfs_showargs(XFS_M(mnt->mnt_sb), m);
1474 }
1475
1476 /*
1477  * This function fills in xfs_mount_t fields based on mount args.
1478  * Note: the superblock _has_ now been read in.
1479  */
1480 STATIC int
1481 xfs_finish_flags(
1482         struct xfs_mount        *mp)
1483 {
1484         int                     ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
1485
1486         /* Fail a mount where the logbuf is smaller than the log stripe */
1487         if (xfs_sb_version_haslogv2(&mp->m_sb)) {
1488                 if (mp->m_logbsize <= 0 &&
1489                     mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE) {
1490                         mp->m_logbsize = mp->m_sb.sb_logsunit;
1491                 } else if (mp->m_logbsize > 0 &&
1492                            mp->m_logbsize < mp->m_sb.sb_logsunit) {
1493                         cmn_err(CE_WARN,
1494         "XFS: logbuf size must be greater than or equal to log stripe size");
1495                         return XFS_ERROR(EINVAL);
1496                 }
1497         } else {
1498                 /* Fail a mount if the logbuf is larger than 32K */
1499                 if (mp->m_logbsize > XLOG_BIG_RECORD_BSIZE) {
1500                         cmn_err(CE_WARN,
1501         "XFS: logbuf size for version 1 logs must be 16K or 32K");
1502                         return XFS_ERROR(EINVAL);
1503                 }
1504         }
1505
1506         /*
1507          * mkfs'ed attr2 will turn on attr2 mount unless explicitly
1508          * told by noattr2 to turn it off
1509          */
1510         if (xfs_sb_version_hasattr2(&mp->m_sb) &&
1511             !(mp->m_flags & XFS_MOUNT_NOATTR2))
1512                 mp->m_flags |= XFS_MOUNT_ATTR2;
1513
1514         /*
1515          * prohibit r/w mounts of read-only filesystems
1516          */
1517         if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
1518                 cmn_err(CE_WARN,
1519         "XFS: cannot mount a read-only filesystem as read-write");
1520                 return XFS_ERROR(EROFS);
1521         }
1522
1523         return 0;
1524 }
1525
1526 STATIC int
1527 xfs_fs_fill_super(
1528         struct super_block      *sb,
1529         void                    *data,
1530         int                     silent)
1531 {
1532         struct inode            *root;
1533         struct xfs_mount        *mp = NULL;
1534         int                     flags = 0, error = ENOMEM;
1535         char                    *mtpt = NULL;
1536
1537         mp = kzalloc(sizeof(struct xfs_mount), GFP_KERNEL);
1538         if (!mp)
1539                 goto out;
1540
1541         spin_lock_init(&mp->m_sb_lock);
1542         mutex_init(&mp->m_growlock);
1543         atomic_set(&mp->m_active_trans, 0);
1544         INIT_LIST_HEAD(&mp->m_sync_list);
1545         spin_lock_init(&mp->m_sync_lock);
1546         init_waitqueue_head(&mp->m_wait_single_sync_task);
1547
1548         mp->m_super = sb;
1549         sb->s_fs_info = mp;
1550
1551         error = xfs_parseargs(mp, (char *)data, &mtpt);
1552         if (error)
1553                 goto out_free_fsname;
1554
1555         sb_min_blocksize(sb, BBSIZE);
1556         sb->s_xattr = xfs_xattr_handlers;
1557         sb->s_export_op = &xfs_export_operations;
1558 #ifdef CONFIG_XFS_QUOTA
1559         sb->s_qcop = &xfs_quotactl_operations;
1560 #endif
1561         sb->s_op = &xfs_super_operations;
1562
1563         error = xfs_dmops_get(mp);
1564         if (error)
1565                 goto out_free_fsname;
1566
1567         if (silent)
1568                 flags |= XFS_MFSI_QUIET;
1569
1570         error = xfs_open_devices(mp);
1571         if (error)
1572                 goto out_put_dmops;
1573
1574         if (xfs_icsb_init_counters(mp))
1575                 mp->m_flags |= XFS_MOUNT_NO_PERCPU_SB;
1576
1577         error = xfs_readsb(mp, flags);
1578         if (error)
1579                 goto out_destroy_counters;
1580
1581         error = xfs_finish_flags(mp);
1582         if (error)
1583                 goto out_free_sb;
1584
1585         error = xfs_setup_devices(mp);
1586         if (error)
1587                 goto out_free_sb;
1588
1589         if (mp->m_flags & XFS_MOUNT_BARRIER)
1590                 xfs_mountfs_check_barriers(mp);
1591
1592         error = xfs_filestream_mount(mp);
1593         if (error)
1594                 goto out_free_sb;
1595
1596         error = xfs_mountfs(mp);
1597         if (error)
1598                 goto out_filestream_unmount;
1599
1600         XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, mtpt, mp->m_fsname);
1601
1602         sb->s_magic = XFS_SB_MAGIC;
1603         sb->s_blocksize = mp->m_sb.sb_blocksize;
1604         sb->s_blocksize_bits = ffs(sb->s_blocksize) - 1;
1605         sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
1606         sb->s_time_gran = 1;
1607         set_posix_acl_flag(sb);
1608
1609         root = igrab(VFS_I(mp->m_rootip));
1610         if (!root) {
1611                 error = ENOENT;
1612                 goto fail_unmount;
1613         }
1614         if (is_bad_inode(root)) {
1615                 error = EINVAL;
1616                 goto fail_vnrele;
1617         }
1618         sb->s_root = d_alloc_root(root);
1619         if (!sb->s_root) {
1620                 error = ENOMEM;
1621                 goto fail_vnrele;
1622         }
1623
1624         error = xfs_syncd_init(mp);
1625         if (error)
1626                 goto fail_vnrele;
1627
1628         xfs_inode_shrinker_register(mp);
1629
1630         kfree(mtpt);
1631         return 0;
1632
1633  out_filestream_unmount:
1634         xfs_filestream_unmount(mp);
1635  out_free_sb:
1636         xfs_freesb(mp);
1637  out_destroy_counters:
1638         xfs_icsb_destroy_counters(mp);
1639         xfs_close_devices(mp);
1640  out_put_dmops:
1641         xfs_dmops_put(mp);
1642  out_free_fsname:
1643         xfs_free_fsname(mp);
1644         kfree(mtpt);
1645         kfree(mp);
1646  out:
1647         return -error;
1648
1649  fail_vnrele:
1650         if (sb->s_root) {
1651                 dput(sb->s_root);
1652                 sb->s_root = NULL;
1653         } else {
1654                 iput(root);
1655         }
1656
1657  fail_unmount:
1658         /*
1659          * Blow away any referenced inode in the filestreams cache.
1660          * This can and will cause log traffic as inodes go inactive
1661          * here.
1662          */
1663         xfs_filestream_unmount(mp);
1664
1665         XFS_bflush(mp->m_ddev_targp);
1666
1667         xfs_unmountfs(mp);
1668         goto out_free_sb;
1669 }
1670
1671 STATIC int
1672 xfs_fs_get_sb(
1673         struct file_system_type *fs_type,
1674         int                     flags,
1675         const char              *dev_name,
1676         void                    *data,
1677         struct vfsmount         *mnt)
1678 {
1679         return get_sb_bdev(fs_type, flags, dev_name, data, xfs_fs_fill_super,
1680                            mnt);
1681 }
1682
1683 static const struct super_operations xfs_super_operations = {
1684         .alloc_inode            = xfs_fs_alloc_inode,
1685         .destroy_inode          = xfs_fs_destroy_inode,
1686         .dirty_inode            = xfs_fs_dirty_inode,
1687         .write_inode            = xfs_fs_write_inode,
1688         .clear_inode            = xfs_fs_clear_inode,
1689         .put_super              = xfs_fs_put_super,
1690         .sync_fs                = xfs_fs_sync_fs,
1691         .freeze_fs              = xfs_fs_freeze,
1692         .unfreeze_fs            = xfs_fs_unfreeze,
1693         .statfs                 = xfs_fs_statfs,
1694         .remount_fs             = xfs_fs_remount,
1695         .show_options           = xfs_fs_show_options,
1696 };
1697
1698 static struct file_system_type xfs_fs_type = {
1699         .owner                  = THIS_MODULE,
1700         .name                   = "xfs",
1701         .get_sb                 = xfs_fs_get_sb,
1702         .kill_sb                = kill_block_super,
1703         .fs_flags               = FS_REQUIRES_DEV,
1704 };
1705
1706 STATIC int __init
1707 xfs_init_zones(void)
1708 {
1709
1710         xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
1711         if (!xfs_ioend_zone)
1712                 goto out;
1713
1714         xfs_ioend_pool = mempool_create_slab_pool(4 * MAX_BUF_PER_PAGE,
1715                                                   xfs_ioend_zone);
1716         if (!xfs_ioend_pool)
1717                 goto out_destroy_ioend_zone;
1718
1719         xfs_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t),
1720                                                 "xfs_log_ticket");
1721         if (!xfs_log_ticket_zone)
1722                 goto out_destroy_ioend_pool;
1723
1724         xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
1725                                                 "xfs_bmap_free_item");
1726         if (!xfs_bmap_free_item_zone)
1727                 goto out_destroy_log_ticket_zone;
1728
1729         xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
1730                                                 "xfs_btree_cur");
1731         if (!xfs_btree_cur_zone)
1732                 goto out_destroy_bmap_free_item_zone;
1733
1734         xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t),
1735                                                 "xfs_da_state");
1736         if (!xfs_da_state_zone)
1737                 goto out_destroy_btree_cur_zone;
1738
1739         xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
1740         if (!xfs_dabuf_zone)
1741                 goto out_destroy_da_state_zone;
1742
1743         xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
1744         if (!xfs_ifork_zone)
1745                 goto out_destroy_dabuf_zone;
1746
1747         xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
1748         if (!xfs_trans_zone)
1749                 goto out_destroy_ifork_zone;
1750
1751         /*
1752          * The size of the zone allocated buf log item is the maximum
1753          * size possible under XFS.  This wastes a little bit of memory,
1754          * but it is much faster.
1755          */
1756         xfs_buf_item_zone = kmem_zone_init((sizeof(xfs_buf_log_item_t) +
1757                                 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
1758                                   NBWORD) * sizeof(int))), "xfs_buf_item");
1759         if (!xfs_buf_item_zone)
1760                 goto out_destroy_trans_zone;
1761
1762         xfs_efd_zone = kmem_zone_init((sizeof(xfs_efd_log_item_t) +
1763                         ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
1764                                  sizeof(xfs_extent_t))), "xfs_efd_item");
1765         if (!xfs_efd_zone)
1766                 goto out_destroy_buf_item_zone;
1767
1768         xfs_efi_zone = kmem_zone_init((sizeof(xfs_efi_log_item_t) +
1769                         ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
1770                                 sizeof(xfs_extent_t))), "xfs_efi_item");
1771         if (!xfs_efi_zone)
1772                 goto out_destroy_efd_zone;
1773
1774         xfs_inode_zone =
1775                 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
1776                         KM_ZONE_HWALIGN | KM_ZONE_RECLAIM | KM_ZONE_SPREAD,
1777                         xfs_fs_inode_init_once);
1778         if (!xfs_inode_zone)
1779                 goto out_destroy_efi_zone;
1780
1781         xfs_ili_zone =
1782                 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
1783                                         KM_ZONE_SPREAD, NULL);
1784         if (!xfs_ili_zone)
1785                 goto out_destroy_inode_zone;
1786
1787         return 0;
1788
1789  out_destroy_inode_zone:
1790         kmem_zone_destroy(xfs_inode_zone);
1791  out_destroy_efi_zone:
1792         kmem_zone_destroy(xfs_efi_zone);
1793  out_destroy_efd_zone:
1794         kmem_zone_destroy(xfs_efd_zone);
1795  out_destroy_buf_item_zone:
1796         kmem_zone_destroy(xfs_buf_item_zone);
1797  out_destroy_trans_zone:
1798         kmem_zone_destroy(xfs_trans_zone);
1799  out_destroy_ifork_zone:
1800         kmem_zone_destroy(xfs_ifork_zone);
1801  out_destroy_dabuf_zone:
1802         kmem_zone_destroy(xfs_dabuf_zone);
1803  out_destroy_da_state_zone:
1804         kmem_zone_destroy(xfs_da_state_zone);
1805  out_destroy_btree_cur_zone:
1806         kmem_zone_destroy(xfs_btree_cur_zone);
1807  out_destroy_bmap_free_item_zone:
1808         kmem_zone_destroy(xfs_bmap_free_item_zone);
1809  out_destroy_log_ticket_zone:
1810         kmem_zone_destroy(xfs_log_ticket_zone);
1811  out_destroy_ioend_pool:
1812         mempool_destroy(xfs_ioend_pool);
1813  out_destroy_ioend_zone:
1814         kmem_zone_destroy(xfs_ioend_zone);
1815  out:
1816         return -ENOMEM;
1817 }
1818
1819 STATIC void
1820 xfs_destroy_zones(void)
1821 {
1822         kmem_zone_destroy(xfs_ili_zone);
1823         kmem_zone_destroy(xfs_inode_zone);
1824         kmem_zone_destroy(xfs_efi_zone);
1825         kmem_zone_destroy(xfs_efd_zone);
1826         kmem_zone_destroy(xfs_buf_item_zone);
1827         kmem_zone_destroy(xfs_trans_zone);
1828         kmem_zone_destroy(xfs_ifork_zone);
1829         kmem_zone_destroy(xfs_dabuf_zone);
1830         kmem_zone_destroy(xfs_da_state_zone);
1831         kmem_zone_destroy(xfs_btree_cur_zone);
1832         kmem_zone_destroy(xfs_bmap_free_item_zone);
1833         kmem_zone_destroy(xfs_log_ticket_zone);
1834         mempool_destroy(xfs_ioend_pool);
1835         kmem_zone_destroy(xfs_ioend_zone);
1836
1837 }
1838
1839 STATIC int __init
1840 init_xfs_fs(void)
1841 {
1842         int                     error;
1843
1844         printk(KERN_INFO XFS_VERSION_STRING " with "
1845                          XFS_BUILD_OPTIONS " enabled\n");
1846
1847         xfs_ioend_init();
1848         xfs_dir_startup();
1849
1850         error = xfs_init_zones();
1851         if (error)
1852                 goto out;
1853
1854         error = xfs_mru_cache_init();
1855         if (error)
1856                 goto out_destroy_zones;
1857
1858         error = xfs_filestream_init();
1859         if (error)
1860                 goto out_mru_cache_uninit;
1861
1862         error = xfs_buf_init();
1863         if (error)
1864                 goto out_filestream_uninit;
1865
1866         error = xfs_init_procfs();
1867         if (error)
1868                 goto out_buf_terminate;
1869
1870         error = xfs_sysctl_register();
1871         if (error)
1872                 goto out_cleanup_procfs;
1873
1874         vfs_initquota();
1875         xfs_inode_shrinker_init();
1876
1877         error = register_filesystem(&xfs_fs_type);
1878         if (error)
1879                 goto out_sysctl_unregister;
1880         return 0;
1881
1882  out_sysctl_unregister:
1883         xfs_sysctl_unregister();
1884  out_cleanup_procfs:
1885         xfs_cleanup_procfs();
1886  out_buf_terminate:
1887         xfs_buf_terminate();
1888  out_filestream_uninit:
1889         xfs_filestream_uninit();
1890  out_mru_cache_uninit:
1891         xfs_mru_cache_uninit();
1892  out_destroy_zones:
1893         xfs_destroy_zones();
1894  out:
1895         return error;
1896 }
1897
1898 STATIC void __exit
1899 exit_xfs_fs(void)
1900 {
1901         vfs_exitquota();
1902         unregister_filesystem(&xfs_fs_type);
1903         xfs_inode_shrinker_destroy();
1904         xfs_sysctl_unregister();
1905         xfs_cleanup_procfs();
1906         xfs_buf_terminate();
1907         xfs_filestream_uninit();
1908         xfs_mru_cache_uninit();
1909         xfs_destroy_zones();
1910 }
1911
1912 module_init(init_xfs_fs);
1913 module_exit(exit_xfs_fs);
1914
1915 MODULE_AUTHOR("Silicon Graphics, Inc.");
1916 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
1917 MODULE_LICENSE("GPL");