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