[XFS] remove dependency of the quota module on behaviors
[powerpc.git] / fs / xfs / xfs_vfsops.c
1 /*
2  * Copyright (c) 2000-2005 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_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_bit.h"
22 #include "xfs_log.h"
23 #include "xfs_inum.h"
24 #include "xfs_trans.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dir2.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_alloc_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_inode_item.h"
39 #include "xfs_btree.h"
40 #include "xfs_alloc.h"
41 #include "xfs_ialloc.h"
42 #include "xfs_quota.h"
43 #include "xfs_error.h"
44 #include "xfs_bmap.h"
45 #include "xfs_rw.h"
46 #include "xfs_refcache.h"
47 #include "xfs_buf_item.h"
48 #include "xfs_log_priv.h"
49 #include "xfs_dir2_trace.h"
50 #include "xfs_extfree_item.h"
51 #include "xfs_acl.h"
52 #include "xfs_attr.h"
53 #include "xfs_clnt.h"
54 #include "xfs_mru_cache.h"
55 #include "xfs_filestream.h"
56 #include "xfs_fsops.h"
57 #include "xfs_vnodeops.h"
58
59
60 STATIC int      xfs_sync(bhv_desc_t *, int, cred_t *);
61
62 int
63 xfs_init(void)
64 {
65         extern kmem_zone_t      *xfs_bmap_free_item_zone;
66         extern kmem_zone_t      *xfs_btree_cur_zone;
67         extern kmem_zone_t      *xfs_trans_zone;
68         extern kmem_zone_t      *xfs_buf_item_zone;
69         extern kmem_zone_t      *xfs_dabuf_zone;
70 #ifdef XFS_DABUF_DEBUG
71         extern lock_t           xfs_dabuf_global_lock;
72         spinlock_init(&xfs_dabuf_global_lock, "xfsda");
73 #endif
74
75         /*
76          * Initialize all of the zone allocators we use.
77          */
78         xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
79                                                  "xfs_bmap_free_item");
80         xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
81                                             "xfs_btree_cur");
82         xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
83         xfs_da_state_zone =
84                 kmem_zone_init(sizeof(xfs_da_state_t), "xfs_da_state");
85         xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
86         xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
87         xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
88         xfs_mru_cache_init();
89         xfs_filestream_init();
90
91         /*
92          * The size of the zone allocated buf log item is the maximum
93          * size possible under XFS.  This wastes a little bit of memory,
94          * but it is much faster.
95          */
96         xfs_buf_item_zone =
97                 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
98                                 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
99                                   NBWORD) * sizeof(int))),
100                                "xfs_buf_item");
101         xfs_efd_zone =
102                 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
103                                ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
104                                  sizeof(xfs_extent_t))),
105                                       "xfs_efd_item");
106         xfs_efi_zone =
107                 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
108                                ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
109                                  sizeof(xfs_extent_t))),
110                                       "xfs_efi_item");
111
112         /*
113          * These zones warrant special memory allocator hints
114          */
115         xfs_inode_zone =
116                 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
117                                         KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
118                                         KM_ZONE_SPREAD, NULL);
119         xfs_ili_zone =
120                 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
121                                         KM_ZONE_SPREAD, NULL);
122         xfs_icluster_zone =
123                 kmem_zone_init_flags(sizeof(xfs_icluster_t), "xfs_icluster",
124                                         KM_ZONE_SPREAD, NULL);
125
126         /*
127          * Allocate global trace buffers.
128          */
129 #ifdef XFS_ALLOC_TRACE
130         xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
131 #endif
132 #ifdef XFS_BMAP_TRACE
133         xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
134 #endif
135 #ifdef XFS_BMBT_TRACE
136         xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
137 #endif
138 #ifdef XFS_ATTR_TRACE
139         xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
140 #endif
141 #ifdef XFS_DIR2_TRACE
142         xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
143 #endif
144
145         xfs_dir_startup();
146
147 #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
148         xfs_error_test_init();
149 #endif /* DEBUG || INDUCE_IO_ERROR */
150
151         xfs_init_procfs();
152         xfs_sysctl_register();
153         return 0;
154 }
155
156 void
157 xfs_cleanup(void)
158 {
159         extern kmem_zone_t      *xfs_bmap_free_item_zone;
160         extern kmem_zone_t      *xfs_btree_cur_zone;
161         extern kmem_zone_t      *xfs_inode_zone;
162         extern kmem_zone_t      *xfs_trans_zone;
163         extern kmem_zone_t      *xfs_da_state_zone;
164         extern kmem_zone_t      *xfs_dabuf_zone;
165         extern kmem_zone_t      *xfs_efd_zone;
166         extern kmem_zone_t      *xfs_efi_zone;
167         extern kmem_zone_t      *xfs_buf_item_zone;
168         extern kmem_zone_t      *xfs_icluster_zone;
169
170         xfs_cleanup_procfs();
171         xfs_sysctl_unregister();
172         xfs_refcache_destroy();
173         xfs_filestream_uninit();
174         xfs_mru_cache_uninit();
175         xfs_acl_zone_destroy(xfs_acl_zone);
176
177 #ifdef XFS_DIR2_TRACE
178         ktrace_free(xfs_dir2_trace_buf);
179 #endif
180 #ifdef XFS_ATTR_TRACE
181         ktrace_free(xfs_attr_trace_buf);
182 #endif
183 #ifdef XFS_BMBT_TRACE
184         ktrace_free(xfs_bmbt_trace_buf);
185 #endif
186 #ifdef XFS_BMAP_TRACE
187         ktrace_free(xfs_bmap_trace_buf);
188 #endif
189 #ifdef XFS_ALLOC_TRACE
190         ktrace_free(xfs_alloc_trace_buf);
191 #endif
192
193         kmem_zone_destroy(xfs_bmap_free_item_zone);
194         kmem_zone_destroy(xfs_btree_cur_zone);
195         kmem_zone_destroy(xfs_inode_zone);
196         kmem_zone_destroy(xfs_trans_zone);
197         kmem_zone_destroy(xfs_da_state_zone);
198         kmem_zone_destroy(xfs_dabuf_zone);
199         kmem_zone_destroy(xfs_buf_item_zone);
200         kmem_zone_destroy(xfs_efd_zone);
201         kmem_zone_destroy(xfs_efi_zone);
202         kmem_zone_destroy(xfs_ifork_zone);
203         kmem_zone_destroy(xfs_ili_zone);
204         kmem_zone_destroy(xfs_icluster_zone);
205 }
206
207 /*
208  * xfs_start_flags
209  *
210  * This function fills in xfs_mount_t fields based on mount args.
211  * Note: the superblock has _not_ yet been read in.
212  */
213 STATIC int
214 xfs_start_flags(
215         struct bhv_vfs          *vfs,
216         struct xfs_mount_args   *ap,
217         struct xfs_mount        *mp)
218 {
219         /* Values are in BBs */
220         if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
221                 /*
222                  * At this point the superblock has not been read
223                  * in, therefore we do not know the block size.
224                  * Before the mount call ends we will convert
225                  * these to FSBs.
226                  */
227                 mp->m_dalign = ap->sunit;
228                 mp->m_swidth = ap->swidth;
229         }
230
231         if (ap->logbufs != -1 &&
232             ap->logbufs != 0 &&
233             (ap->logbufs < XLOG_MIN_ICLOGS ||
234              ap->logbufs > XLOG_MAX_ICLOGS)) {
235                 cmn_err(CE_WARN,
236                         "XFS: invalid logbufs value: %d [not %d-%d]",
237                         ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
238                 return XFS_ERROR(EINVAL);
239         }
240         mp->m_logbufs = ap->logbufs;
241         if (ap->logbufsize != -1 &&
242             ap->logbufsize !=  0 &&
243             (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
244              ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
245              !is_power_of_2(ap->logbufsize))) {
246                 cmn_err(CE_WARN,
247         "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
248                         ap->logbufsize);
249                 return XFS_ERROR(EINVAL);
250         }
251         mp->m_logbsize = ap->logbufsize;
252         mp->m_fsname_len = strlen(ap->fsname) + 1;
253         mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
254         strcpy(mp->m_fsname, ap->fsname);
255         if (ap->rtname[0]) {
256                 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
257                 strcpy(mp->m_rtname, ap->rtname);
258         }
259         if (ap->logname[0]) {
260                 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
261                 strcpy(mp->m_logname, ap->logname);
262         }
263
264         if (ap->flags & XFSMNT_WSYNC)
265                 mp->m_flags |= XFS_MOUNT_WSYNC;
266 #if XFS_BIG_INUMS
267         if (ap->flags & XFSMNT_INO64) {
268                 mp->m_flags |= XFS_MOUNT_INO64;
269                 mp->m_inoadd = XFS_INO64_OFFSET;
270         }
271 #endif
272         if (ap->flags & XFSMNT_RETERR)
273                 mp->m_flags |= XFS_MOUNT_RETERR;
274         if (ap->flags & XFSMNT_NOALIGN)
275                 mp->m_flags |= XFS_MOUNT_NOALIGN;
276         if (ap->flags & XFSMNT_SWALLOC)
277                 mp->m_flags |= XFS_MOUNT_SWALLOC;
278         if (ap->flags & XFSMNT_OSYNCISOSYNC)
279                 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
280         if (ap->flags & XFSMNT_32BITINODES)
281                 mp->m_flags |= XFS_MOUNT_32BITINODES;
282
283         if (ap->flags & XFSMNT_IOSIZE) {
284                 if (ap->iosizelog > XFS_MAX_IO_LOG ||
285                     ap->iosizelog < XFS_MIN_IO_LOG) {
286                         cmn_err(CE_WARN,
287                 "XFS: invalid log iosize: %d [not %d-%d]",
288                                 ap->iosizelog, XFS_MIN_IO_LOG,
289                                 XFS_MAX_IO_LOG);
290                         return XFS_ERROR(EINVAL);
291                 }
292
293                 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
294                 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
295         }
296
297         if (ap->flags & XFSMNT_IDELETE)
298                 mp->m_flags |= XFS_MOUNT_IDELETE;
299         if (ap->flags & XFSMNT_DIRSYNC)
300                 mp->m_flags |= XFS_MOUNT_DIRSYNC;
301         if (ap->flags & XFSMNT_ATTR2)
302                 mp->m_flags |= XFS_MOUNT_ATTR2;
303
304         if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
305                 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
306
307         /*
308          * no recovery flag requires a read-only mount
309          */
310         if (ap->flags & XFSMNT_NORECOVERY) {
311                 if (!(vfs->vfs_flag & VFS_RDONLY)) {
312                         cmn_err(CE_WARN,
313         "XFS: tried to mount a FS read-write without recovery!");
314                         return XFS_ERROR(EINVAL);
315                 }
316                 mp->m_flags |= XFS_MOUNT_NORECOVERY;
317         }
318
319         if (ap->flags & XFSMNT_NOUUID)
320                 mp->m_flags |= XFS_MOUNT_NOUUID;
321         if (ap->flags & XFSMNT_BARRIER)
322                 mp->m_flags |= XFS_MOUNT_BARRIER;
323         else
324                 mp->m_flags &= ~XFS_MOUNT_BARRIER;
325
326         if (ap->flags2 & XFSMNT2_FILESTREAMS)
327                 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
328
329         if (ap->flags & XFSMNT_DMAPI)
330                 vfs->vfs_flag |= VFS_DMI;
331         return 0;
332 }
333
334 /*
335  * This function fills in xfs_mount_t fields based on mount args.
336  * Note: the superblock _has_ now been read in.
337  */
338 STATIC int
339 xfs_finish_flags(
340         struct bhv_vfs          *vfs,
341         struct xfs_mount_args   *ap,
342         struct xfs_mount        *mp)
343 {
344         int                     ronly = (vfs->vfs_flag & VFS_RDONLY);
345
346         /* Fail a mount where the logbuf is smaller then the log stripe */
347         if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
348                 if ((ap->logbufsize <= 0) &&
349                     (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
350                         mp->m_logbsize = mp->m_sb.sb_logsunit;
351                 } else if (ap->logbufsize > 0 &&
352                            ap->logbufsize < mp->m_sb.sb_logsunit) {
353                         cmn_err(CE_WARN,
354         "XFS: logbuf size must be greater than or equal to log stripe size");
355                         return XFS_ERROR(EINVAL);
356                 }
357         } else {
358                 /* Fail a mount if the logbuf is larger than 32K */
359                 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
360                         cmn_err(CE_WARN,
361         "XFS: logbuf size for version 1 logs must be 16K or 32K");
362                         return XFS_ERROR(EINVAL);
363                 }
364         }
365
366         if (XFS_SB_VERSION_HASATTR2(&mp->m_sb)) {
367                 mp->m_flags |= XFS_MOUNT_ATTR2;
368         }
369
370         /*
371          * prohibit r/w mounts of read-only filesystems
372          */
373         if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
374                 cmn_err(CE_WARN,
375         "XFS: cannot mount a read-only filesystem as read-write");
376                 return XFS_ERROR(EROFS);
377         }
378
379         /*
380          * check for shared mount.
381          */
382         if (ap->flags & XFSMNT_SHARED) {
383                 if (!XFS_SB_VERSION_HASSHARED(&mp->m_sb))
384                         return XFS_ERROR(EINVAL);
385
386                 /*
387                  * For IRIX 6.5, shared mounts must have the shared
388                  * version bit set, have the persistent readonly
389                  * field set, must be version 0 and can only be mounted
390                  * read-only.
391                  */
392                 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
393                      (mp->m_sb.sb_shared_vn != 0))
394                         return XFS_ERROR(EINVAL);
395
396                 mp->m_flags |= XFS_MOUNT_SHARED;
397
398                 /*
399                  * Shared XFS V0 can't deal with DMI.  Return EINVAL.
400                  */
401                 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
402                         return XFS_ERROR(EINVAL);
403         }
404
405         if (ap->flags & XFSMNT_UQUOTA) {
406                 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
407                 if (ap->flags & XFSMNT_UQUOTAENF)
408                         mp->m_qflags |= XFS_UQUOTA_ENFD;
409         }
410
411         if (ap->flags & XFSMNT_GQUOTA) {
412                 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
413                 if (ap->flags & XFSMNT_GQUOTAENF)
414                         mp->m_qflags |= XFS_OQUOTA_ENFD;
415         } else if (ap->flags & XFSMNT_PQUOTA) {
416                 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
417                 if (ap->flags & XFSMNT_PQUOTAENF)
418                         mp->m_qflags |= XFS_OQUOTA_ENFD;
419         }
420
421         return 0;
422 }
423
424 /*
425  * xfs_mount
426  *
427  * The file system configurations are:
428  *      (1) device (partition) with data and internal log
429  *      (2) logical volume with data and log subvolumes.
430  *      (3) logical volume with data, log, and realtime subvolumes.
431  *
432  * We only have to handle opening the log and realtime volumes here if
433  * they are present.  The data subvolume has already been opened by
434  * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
435  */
436 STATIC int
437 xfs_mount(
438         struct bhv_desc         *bhvp,
439         struct xfs_mount_args   *args,
440         cred_t                  *credp)
441 {
442         struct bhv_vfs          *vfsp = bhvtovfs(bhvp);
443         struct bhv_desc         *p;
444         struct xfs_mount        *mp = XFS_BHVTOM(bhvp);
445         struct block_device     *ddev, *logdev, *rtdev;
446         int                     flags = 0, error;
447
448         ddev = vfsp->vfs_super->s_bdev;
449         logdev = rtdev = NULL;
450
451         error = xfs_dmops_get(mp, args);
452         if (error)
453                 return error;
454         error = xfs_qmops_get(mp, args);
455         if (error)
456                 return error;
457
458         /*
459          * Setup xfs_mount function vectors from available behaviors
460          */
461         p = vfs_bhv_lookup(vfsp, VFS_POSITION_IO);
462         mp->m_io_ops = p ? *(xfs_ioops_t *) vfs_bhv_custom(p) : xfs_iocore_xfs;
463
464         if (args->flags & XFSMNT_QUIET)
465                 flags |= XFS_MFSI_QUIET;
466
467         /*
468          * Open real time and log devices - order is important.
469          */
470         if (args->logname[0]) {
471                 error = xfs_blkdev_get(mp, args->logname, &logdev);
472                 if (error)
473                         return error;
474         }
475         if (args->rtname[0]) {
476                 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
477                 if (error) {
478                         xfs_blkdev_put(logdev);
479                         return error;
480                 }
481
482                 if (rtdev == ddev || rtdev == logdev) {
483                         cmn_err(CE_WARN,
484         "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
485                         xfs_blkdev_put(logdev);
486                         xfs_blkdev_put(rtdev);
487                         return EINVAL;
488                 }
489         }
490
491         /*
492          * Setup xfs_mount buffer target pointers
493          */
494         error = ENOMEM;
495         mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
496         if (!mp->m_ddev_targp) {
497                 xfs_blkdev_put(logdev);
498                 xfs_blkdev_put(rtdev);
499                 return error;
500         }
501         if (rtdev) {
502                 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
503                 if (!mp->m_rtdev_targp) {
504                         xfs_blkdev_put(logdev);
505                         xfs_blkdev_put(rtdev);
506                         goto error0;
507                 }
508         }
509         mp->m_logdev_targp = (logdev && logdev != ddev) ?
510                                 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
511         if (!mp->m_logdev_targp) {
512                 xfs_blkdev_put(logdev);
513                 xfs_blkdev_put(rtdev);
514                 goto error0;
515         }
516
517         /*
518          * Setup flags based on mount(2) options and then the superblock
519          */
520         error = xfs_start_flags(vfsp, args, mp);
521         if (error)
522                 goto error1;
523         error = xfs_readsb(mp, flags);
524         if (error)
525                 goto error1;
526         error = xfs_finish_flags(vfsp, args, mp);
527         if (error)
528                 goto error2;
529
530         /*
531          * Setup xfs_mount buffer target pointers based on superblock
532          */
533         error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
534                                     mp->m_sb.sb_sectsize);
535         if (!error && logdev && logdev != ddev) {
536                 unsigned int    log_sector_size = BBSIZE;
537
538                 if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb))
539                         log_sector_size = mp->m_sb.sb_logsectsize;
540                 error = xfs_setsize_buftarg(mp->m_logdev_targp,
541                                             mp->m_sb.sb_blocksize,
542                                             log_sector_size);
543         }
544         if (!error && rtdev)
545                 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
546                                             mp->m_sb.sb_blocksize,
547                                             mp->m_sb.sb_sectsize);
548         if (error)
549                 goto error2;
550
551         if (mp->m_flags & XFS_MOUNT_BARRIER)
552                 xfs_mountfs_check_barriers(mp);
553
554         if ((error = xfs_filestream_mount(mp)))
555                 goto error2;
556
557         error = XFS_IOINIT(vfsp, args, flags);
558         if (error)
559                 goto error2;
560
561         XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
562
563         return 0;
564
565 error2:
566         if (mp->m_sb_bp)
567                 xfs_freesb(mp);
568 error1:
569         xfs_binval(mp->m_ddev_targp);
570         if (logdev && logdev != ddev)
571                 xfs_binval(mp->m_logdev_targp);
572         if (rtdev)
573                 xfs_binval(mp->m_rtdev_targp);
574 error0:
575         xfs_unmountfs_close(mp, credp);
576         xfs_qmops_put(mp);
577         xfs_dmops_put(mp);
578         return error;
579 }
580
581 STATIC int
582 xfs_unmount(
583         bhv_desc_t      *bdp,
584         int             flags,
585         cred_t          *credp)
586 {
587         bhv_vfs_t       *vfsp = bhvtovfs(bdp);
588         xfs_mount_t     *mp = XFS_BHVTOM(bdp);
589         xfs_inode_t     *rip;
590         bhv_vnode_t     *rvp;
591         int             unmount_event_wanted = 0;
592         int             unmount_event_flags = 0;
593         int             xfs_unmountfs_needed = 0;
594         int             error;
595
596         rip = mp->m_rootip;
597         rvp = XFS_ITOV(rip);
598
599 #ifdef HAVE_DMAPI
600         if (vfsp->vfs_flag & VFS_DMI) {
601                 error = XFS_SEND_PREUNMOUNT(mp, vfsp,
602                                 rvp, DM_RIGHT_NULL, rvp, DM_RIGHT_NULL,
603                                 NULL, NULL, 0, 0,
604                                 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
605                                         0:DM_FLAGS_UNWANTED);
606                         if (error)
607                                 return XFS_ERROR(error);
608                 unmount_event_wanted = 1;
609                 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
610                                         0 : DM_FLAGS_UNWANTED;
611         }
612 #endif
613         /*
614          * First blow any referenced inode from this file system
615          * out of the reference cache, and delete the timer.
616          */
617         xfs_refcache_purge_mp(mp);
618
619         /*
620          * Blow away any referenced inode in the filestreams cache.
621          * This can and will cause log traffic as inodes go inactive
622          * here.
623          */
624         xfs_filestream_unmount(mp);
625
626         XFS_bflush(mp->m_ddev_targp);
627         error = xfs_unmount_flush(mp, 0);
628         if (error)
629                 goto out;
630
631         ASSERT(vn_count(rvp) == 1);
632
633         /*
634          * Drop the reference count
635          */
636         VN_RELE(rvp);
637
638         /*
639          * If we're forcing a shutdown, typically because of a media error,
640          * we want to make sure we invalidate dirty pages that belong to
641          * referenced vnodes as well.
642          */
643         if (XFS_FORCED_SHUTDOWN(mp)) {
644                 error = xfs_sync(&mp->m_bhv,
645                          (SYNC_WAIT | SYNC_CLOSE), credp);
646                 ASSERT(error != EFSCORRUPTED);
647         }
648         xfs_unmountfs_needed = 1;
649
650 out:
651         /*      Send DMAPI event, if required.
652          *      Then do xfs_unmountfs() if needed.
653          *      Then return error (or zero).
654          */
655         if (unmount_event_wanted) {
656                 /* Note: mp structure must still exist for
657                  * XFS_SEND_UNMOUNT() call.
658                  */
659                 XFS_SEND_UNMOUNT(mp, vfsp, error == 0 ? rvp : NULL,
660                         DM_RIGHT_NULL, 0, error, unmount_event_flags);
661         }
662         if (xfs_unmountfs_needed) {
663                 /*
664                  * Call common unmount function to flush to disk
665                  * and free the super block buffer & mount structures.
666                  */
667                 xfs_unmountfs(mp, credp);
668                 xfs_qmops_put(mp);
669                 xfs_dmops_put(mp);
670                 kmem_free(mp, sizeof(xfs_mount_t));
671         }
672
673         return XFS_ERROR(error);
674 }
675
676 STATIC int
677 xfs_quiesce_fs(
678         xfs_mount_t             *mp)
679 {
680         int                     count = 0, pincount;
681
682         xfs_refcache_purge_mp(mp);
683         xfs_flush_buftarg(mp->m_ddev_targp, 0);
684         xfs_finish_reclaim_all(mp, 0);
685
686         /* This loop must run at least twice.
687          * The first instance of the loop will flush
688          * most meta data but that will generate more
689          * meta data (typically directory updates).
690          * Which then must be flushed and logged before
691          * we can write the unmount record.
692          */
693         do {
694                 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
695                 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
696                 if (!pincount) {
697                         delay(50);
698                         count++;
699                 }
700         } while (count < 2);
701
702         return 0;
703 }
704
705 /*
706  * Second stage of a quiesce. The data is already synced, now we have to take
707  * care of the metadata. New transactions are already blocked, so we need to
708  * wait for any remaining transactions to drain out before proceding.
709  */
710 STATIC void
711 xfs_attr_quiesce(
712         xfs_mount_t     *mp)
713 {
714         /* wait for all modifications to complete */
715         while (atomic_read(&mp->m_active_trans) > 0)
716                 delay(100);
717
718         /* flush inodes and push all remaining buffers out to disk */
719         xfs_quiesce_fs(mp);
720
721         ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
722
723         /* Push the superblock and write an unmount record */
724         xfs_log_sbcount(mp, 1);
725         xfs_log_unmount_write(mp);
726         xfs_unmountfs_writesb(mp);
727 }
728
729 STATIC int
730 xfs_mntupdate(
731         bhv_desc_t                      *bdp,
732         int                             *flags,
733         struct xfs_mount_args           *args)
734 {
735         bhv_vfs_t       *vfsp = bhvtovfs(bdp);
736         xfs_mount_t     *mp = XFS_BHVTOM(bdp);
737
738         if (!(*flags & MS_RDONLY)) {                    /* rw/ro -> rw */
739                 if (vfsp->vfs_flag & VFS_RDONLY)
740                         vfsp->vfs_flag &= ~VFS_RDONLY;
741                 if (args->flags & XFSMNT_BARRIER) {
742                         mp->m_flags |= XFS_MOUNT_BARRIER;
743                         xfs_mountfs_check_barriers(mp);
744                 } else {
745                         mp->m_flags &= ~XFS_MOUNT_BARRIER;
746                 }
747         } else if (!(vfsp->vfs_flag & VFS_RDONLY)) {    /* rw -> ro */
748                 xfs_filestream_flush(mp);
749                 bhv_vfs_sync(vfsp, SYNC_DATA_QUIESCE, NULL);
750                 xfs_attr_quiesce(mp);
751                 vfsp->vfs_flag |= VFS_RDONLY;
752         }
753         return 0;
754 }
755
756 /*
757  * xfs_unmount_flush implements a set of flush operation on special
758  * inodes, which are needed as a separate set of operations so that
759  * they can be called as part of relocation process.
760  */
761 int
762 xfs_unmount_flush(
763         xfs_mount_t     *mp,            /* Mount structure we are getting
764                                            rid of. */
765         int             relocation)     /* Called from vfs relocation. */
766 {
767         xfs_inode_t     *rip = mp->m_rootip;
768         xfs_inode_t     *rbmip;
769         xfs_inode_t     *rsumip = NULL;
770         bhv_vnode_t     *rvp = XFS_ITOV(rip);
771         int             error;
772
773         xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
774         xfs_iflock(rip);
775
776         /*
777          * Flush out the real time inodes.
778          */
779         if ((rbmip = mp->m_rbmip) != NULL) {
780                 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
781                 xfs_iflock(rbmip);
782                 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
783                 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
784
785                 if (error == EFSCORRUPTED)
786                         goto fscorrupt_out;
787
788                 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
789
790                 rsumip = mp->m_rsumip;
791                 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
792                 xfs_iflock(rsumip);
793                 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
794                 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
795
796                 if (error == EFSCORRUPTED)
797                         goto fscorrupt_out;
798
799                 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
800         }
801
802         /*
803          * Synchronously flush root inode to disk
804          */
805         error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
806         if (error == EFSCORRUPTED)
807                 goto fscorrupt_out2;
808
809         if (vn_count(rvp) != 1 && !relocation) {
810                 xfs_iunlock(rip, XFS_ILOCK_EXCL);
811                 return XFS_ERROR(EBUSY);
812         }
813
814         /*
815          * Release dquot that rootinode, rbmino and rsumino might be holding,
816          * flush and purge the quota inodes.
817          */
818         error = XFS_QM_UNMOUNT(mp);
819         if (error == EFSCORRUPTED)
820                 goto fscorrupt_out2;
821
822         if (rbmip) {
823                 VN_RELE(XFS_ITOV(rbmip));
824                 VN_RELE(XFS_ITOV(rsumip));
825         }
826
827         xfs_iunlock(rip, XFS_ILOCK_EXCL);
828         return 0;
829
830 fscorrupt_out:
831         xfs_ifunlock(rip);
832
833 fscorrupt_out2:
834         xfs_iunlock(rip, XFS_ILOCK_EXCL);
835
836         return XFS_ERROR(EFSCORRUPTED);
837 }
838
839 /*
840  * xfs_root extracts the root vnode from a vfs.
841  *
842  * vfsp -- the vfs struct for the desired file system
843  * vpp  -- address of the caller's vnode pointer which should be
844  *         set to the desired fs root vnode
845  */
846 STATIC int
847 xfs_root(
848         bhv_desc_t      *bdp,
849         bhv_vnode_t     **vpp)
850 {
851         bhv_vnode_t     *vp;
852
853         vp = XFS_ITOV((XFS_BHVTOM(bdp))->m_rootip);
854         VN_HOLD(vp);
855         *vpp = vp;
856         return 0;
857 }
858
859 /*
860  * xfs_statvfs
861  *
862  * Fill in the statvfs structure for the given file system.  We use
863  * the superblock lock in the mount structure to ensure a consistent
864  * snapshot of the counters returned.
865  */
866 STATIC int
867 xfs_statvfs(
868         bhv_desc_t      *bdp,
869         bhv_statvfs_t   *statp,
870         bhv_vnode_t     *vp)
871 {
872         __uint64_t      fakeinos;
873         xfs_extlen_t    lsize;
874         xfs_mount_t     *mp;
875         xfs_sb_t        *sbp;
876         unsigned long   s;
877
878         mp = XFS_BHVTOM(bdp);
879         sbp = &(mp->m_sb);
880
881         statp->f_type = XFS_SB_MAGIC;
882
883         xfs_icsb_sync_counters_flags(mp, XFS_ICSB_LAZY_COUNT);
884         s = XFS_SB_LOCK(mp);
885         statp->f_bsize = sbp->sb_blocksize;
886         lsize = sbp->sb_logstart ? sbp->sb_logblocks : 0;
887         statp->f_blocks = sbp->sb_dblocks - lsize;
888         statp->f_bfree = statp->f_bavail =
889                                 sbp->sb_fdblocks - XFS_ALLOC_SET_ASIDE(mp);
890         fakeinos = statp->f_bfree << sbp->sb_inopblog;
891 #if XFS_BIG_INUMS
892         fakeinos += mp->m_inoadd;
893 #endif
894         statp->f_files =
895             MIN(sbp->sb_icount + fakeinos, (__uint64_t)XFS_MAXINUMBER);
896         if (mp->m_maxicount)
897 #if XFS_BIG_INUMS
898                 if (!mp->m_inoadd)
899 #endif
900                         statp->f_files = min_t(typeof(statp->f_files),
901                                                 statp->f_files,
902                                                 mp->m_maxicount);
903         statp->f_ffree = statp->f_files - (sbp->sb_icount - sbp->sb_ifree);
904         XFS_SB_UNLOCK(mp, s);
905
906         xfs_statvfs_fsid(statp, mp);
907         statp->f_namelen = MAXNAMELEN - 1;
908
909         if (vp)
910                 XFS_QM_DQSTATVFS(xfs_vtoi(vp), statp);
911         return 0;
912 }
913
914
915 /*
916  * xfs_sync flushes any pending I/O to file system vfsp.
917  *
918  * This routine is called by vfs_sync() to make sure that things make it
919  * out to disk eventually, on sync() system calls to flush out everything,
920  * and when the file system is unmounted.  For the vfs_sync() case, all
921  * we really need to do is sync out the log to make all of our meta-data
922  * updates permanent (except for timestamps).  For calls from pflushd(),
923  * dirty pages are kept moving by calling pdflush() on the inodes
924  * containing them.  We also flush the inodes that we can lock without
925  * sleeping and the superblock if we can lock it without sleeping from
926  * vfs_sync() so that items at the tail of the log are always moving out.
927  *
928  * Flags:
929  *      SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
930  *                     to sleep if we can help it.  All we really need
931  *                     to do is ensure that the log is synced at least
932  *                     periodically.  We also push the inodes and
933  *                     superblock if we can lock them without sleeping
934  *                      and they are not pinned.
935  *      SYNC_ATTR    - We need to flush the inodes.  If SYNC_BDFLUSH is not
936  *                     set, then we really want to lock each inode and flush
937  *                     it.
938  *      SYNC_WAIT    - All the flushes that take place in this call should
939  *                     be synchronous.
940  *      SYNC_DELWRI  - This tells us to push dirty pages associated with
941  *                     inodes.  SYNC_WAIT and SYNC_BDFLUSH are used to
942  *                     determine if they should be flushed sync, async, or
943  *                     delwri.
944  *      SYNC_CLOSE   - This flag is passed when the system is being
945  *                     unmounted.  We should sync and invalidate everything.
946  *      SYNC_FSDATA  - This indicates that the caller would like to make
947  *                     sure the superblock is safe on disk.  We can ensure
948  *                     this by simply making sure the log gets flushed
949  *                     if SYNC_BDFLUSH is set, and by actually writing it
950  *                     out otherwise.
951  *      SYNC_IOWAIT  - The caller wants us to wait for all data I/O to complete
952  *                     before we return (including direct I/O). Forms the drain
953  *                     side of the write barrier needed to safely quiesce the
954  *                     filesystem.
955  *
956  */
957 /*ARGSUSED*/
958 STATIC int
959 xfs_sync(
960         bhv_desc_t      *bdp,
961         int             flags,
962         cred_t          *credp)
963 {
964         xfs_mount_t     *mp = XFS_BHVTOM(bdp);
965         int             error;
966
967         /*
968          * Get the Quota Manager to flush the dquots.
969          *
970          * If XFS quota support is not enabled or this filesystem
971          * instance does not use quotas XFS_QM_DQSYNC will always
972          * return zero.
973          */
974         error = XFS_QM_DQSYNC(mp, flags);
975         if (error) {
976                 /*
977                  * If we got an IO error, we will be shutting down.
978                  * So, there's nothing more for us to do here.
979                  */
980                 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
981                 if (XFS_FORCED_SHUTDOWN(mp))
982                         return XFS_ERROR(error);
983         }
984
985         if (flags & SYNC_IOWAIT)
986                 xfs_filestream_flush(mp);
987
988         return xfs_syncsub(mp, flags, NULL);
989 }
990
991 /*
992  * xfs sync routine for internal use
993  *
994  * This routine supports all of the flags defined for the generic vfs_sync
995  * interface as explained above under xfs_sync.
996  *
997  */
998 int
999 xfs_sync_inodes(
1000         xfs_mount_t     *mp,
1001         int             flags,
1002         int             *bypassed)
1003 {
1004         xfs_inode_t     *ip = NULL;
1005         xfs_inode_t     *ip_next;
1006         xfs_buf_t       *bp;
1007         bhv_vnode_t     *vp = NULL;
1008         int             error;
1009         int             last_error;
1010         uint64_t        fflag;
1011         uint            lock_flags;
1012         uint            base_lock_flags;
1013         boolean_t       mount_locked;
1014         boolean_t       vnode_refed;
1015         int             preempt;
1016         xfs_dinode_t    *dip;
1017         xfs_iptr_t      *ipointer;
1018 #ifdef DEBUG
1019         boolean_t       ipointer_in = B_FALSE;
1020
1021 #define IPOINTER_SET    ipointer_in = B_TRUE
1022 #define IPOINTER_CLR    ipointer_in = B_FALSE
1023 #else
1024 #define IPOINTER_SET
1025 #define IPOINTER_CLR
1026 #endif
1027
1028
1029 /* Insert a marker record into the inode list after inode ip. The list
1030  * must be locked when this is called. After the call the list will no
1031  * longer be locked.
1032  */
1033 #define IPOINTER_INSERT(ip, mp) { \
1034                 ASSERT(ipointer_in == B_FALSE); \
1035                 ipointer->ip_mnext = ip->i_mnext; \
1036                 ipointer->ip_mprev = ip; \
1037                 ip->i_mnext = (xfs_inode_t *)ipointer; \
1038                 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
1039                 preempt = 0; \
1040                 XFS_MOUNT_IUNLOCK(mp); \
1041                 mount_locked = B_FALSE; \
1042                 IPOINTER_SET; \
1043         }
1044
1045 /* Remove the marker from the inode list. If the marker was the only item
1046  * in the list then there are no remaining inodes and we should zero out
1047  * the whole list. If we are the current head of the list then move the head
1048  * past us.
1049  */
1050 #define IPOINTER_REMOVE(ip, mp) { \
1051                 ASSERT(ipointer_in == B_TRUE); \
1052                 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
1053                         ip = ipointer->ip_mnext; \
1054                         ip->i_mprev = ipointer->ip_mprev; \
1055                         ipointer->ip_mprev->i_mnext = ip; \
1056                         if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
1057                                 mp->m_inodes = ip; \
1058                         } \
1059                 } else { \
1060                         ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
1061                         mp->m_inodes = NULL; \
1062                         ip = NULL; \
1063                 } \
1064                 IPOINTER_CLR; \
1065         }
1066
1067 #define XFS_PREEMPT_MASK        0x7f
1068
1069         if (bypassed)
1070                 *bypassed = 0;
1071         if (XFS_MTOVFS(mp)->vfs_flag & VFS_RDONLY)
1072                 return 0;
1073         error = 0;
1074         last_error = 0;
1075         preempt = 0;
1076
1077         /* Allocate a reference marker */
1078         ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
1079
1080         fflag = XFS_B_ASYNC;            /* default is don't wait */
1081         if (flags & (SYNC_BDFLUSH | SYNC_DELWRI))
1082                 fflag = XFS_B_DELWRI;
1083         if (flags & SYNC_WAIT)
1084                 fflag = 0;              /* synchronous overrides all */
1085
1086         base_lock_flags = XFS_ILOCK_SHARED;
1087         if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
1088                 /*
1089                  * We need the I/O lock if we're going to call any of
1090                  * the flush/inval routines.
1091                  */
1092                 base_lock_flags |= XFS_IOLOCK_SHARED;
1093         }
1094
1095         XFS_MOUNT_ILOCK(mp);
1096
1097         ip = mp->m_inodes;
1098
1099         mount_locked = B_TRUE;
1100         vnode_refed  = B_FALSE;
1101
1102         IPOINTER_CLR;
1103
1104         do {
1105                 ASSERT(ipointer_in == B_FALSE);
1106                 ASSERT(vnode_refed == B_FALSE);
1107
1108                 lock_flags = base_lock_flags;
1109
1110                 /*
1111                  * There were no inodes in the list, just break out
1112                  * of the loop.
1113                  */
1114                 if (ip == NULL) {
1115                         break;
1116                 }
1117
1118                 /*
1119                  * We found another sync thread marker - skip it
1120                  */
1121                 if (ip->i_mount == NULL) {
1122                         ip = ip->i_mnext;
1123                         continue;
1124                 }
1125
1126                 vp = XFS_ITOV_NULL(ip);
1127
1128                 /*
1129                  * If the vnode is gone then this is being torn down,
1130                  * call reclaim if it is flushed, else let regular flush
1131                  * code deal with it later in the loop.
1132                  */
1133
1134                 if (vp == NULL) {
1135                         /* Skip ones already in reclaim */
1136                         if (ip->i_flags & XFS_IRECLAIM) {
1137                                 ip = ip->i_mnext;
1138                                 continue;
1139                         }
1140                         if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1141                                 ip = ip->i_mnext;
1142                         } else if ((xfs_ipincount(ip) == 0) &&
1143                                     xfs_iflock_nowait(ip)) {
1144                                 IPOINTER_INSERT(ip, mp);
1145
1146                                 xfs_finish_reclaim(ip, 1,
1147                                                 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1148
1149                                 XFS_MOUNT_ILOCK(mp);
1150                                 mount_locked = B_TRUE;
1151                                 IPOINTER_REMOVE(ip, mp);
1152                         } else {
1153                                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1154                                 ip = ip->i_mnext;
1155                         }
1156                         continue;
1157                 }
1158
1159                 if (VN_BAD(vp)) {
1160                         ip = ip->i_mnext;
1161                         continue;
1162                 }
1163
1164                 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1165                         XFS_MOUNT_IUNLOCK(mp);
1166                         kmem_free(ipointer, sizeof(xfs_iptr_t));
1167                         return 0;
1168                 }
1169
1170                 /*
1171                  * If this is just vfs_sync() or pflushd() calling
1172                  * then we can skip inodes for which it looks like
1173                  * there is nothing to do.  Since we don't have the
1174                  * inode locked this is racy, but these are periodic
1175                  * calls so it doesn't matter.  For the others we want
1176                  * to know for sure, so we at least try to lock them.
1177                  */
1178                 if (flags & SYNC_BDFLUSH) {
1179                         if (((ip->i_itemp == NULL) ||
1180                              !(ip->i_itemp->ili_format.ilf_fields &
1181                                XFS_ILOG_ALL)) &&
1182                             (ip->i_update_core == 0)) {
1183                                 ip = ip->i_mnext;
1184                                 continue;
1185                         }
1186                 }
1187
1188                 /*
1189                  * Try to lock without sleeping.  We're out of order with
1190                  * the inode list lock here, so if we fail we need to drop
1191                  * the mount lock and try again.  If we're called from
1192                  * bdflush() here, then don't bother.
1193                  *
1194                  * The inode lock here actually coordinates with the
1195                  * almost spurious inode lock in xfs_ireclaim() to prevent
1196                  * the vnode we handle here without a reference from
1197                  * being freed while we reference it.  If we lock the inode
1198                  * while it's on the mount list here, then the spurious inode
1199                  * lock in xfs_ireclaim() after the inode is pulled from
1200                  * the mount list will sleep until we release it here.
1201                  * This keeps the vnode from being freed while we reference
1202                  * it.
1203                  */
1204                 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
1205                         if ((flags & SYNC_BDFLUSH) || (vp == NULL)) {
1206                                 ip = ip->i_mnext;
1207                                 continue;
1208                         }
1209
1210                         vp = vn_grab(vp);
1211                         if (vp == NULL) {
1212                                 ip = ip->i_mnext;
1213                                 continue;
1214                         }
1215
1216                         IPOINTER_INSERT(ip, mp);
1217                         xfs_ilock(ip, lock_flags);
1218
1219                         ASSERT(vp == XFS_ITOV(ip));
1220                         ASSERT(ip->i_mount == mp);
1221
1222                         vnode_refed = B_TRUE;
1223                 }
1224
1225                 /* From here on in the loop we may have a marker record
1226                  * in the inode list.
1227                  */
1228
1229                 /*
1230                  * If we have to flush data or wait for I/O completion
1231                  * we need to drop the ilock that we currently hold.
1232                  * If we need to drop the lock, insert a marker if we
1233                  * have not already done so.
1234                  */
1235                 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1236                     ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1237                         if (mount_locked) {
1238                                 IPOINTER_INSERT(ip, mp);
1239                         }
1240                         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1241
1242                         if (flags & SYNC_CLOSE) {
1243                                 /* Shutdown case. Flush and invalidate. */
1244                                 if (XFS_FORCED_SHUTDOWN(mp))
1245                                         xfs_tosspages(ip, 0, -1,
1246                                                              FI_REMAPF);
1247                                 else
1248                                         error = xfs_flushinval_pages(ip,
1249                                                         0, -1, FI_REMAPF);
1250                         } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
1251                                 error = xfs_flush_pages(ip, 0,
1252                                                         -1, fflag, FI_NONE);
1253                         }
1254
1255                         /*
1256                          * When freezing, we need to wait ensure all I/O (including direct
1257                          * I/O) is complete to ensure no further data modification can take
1258                          * place after this point
1259                          */
1260                         if (flags & SYNC_IOWAIT)
1261                                 vn_iowait(ip);
1262
1263                         xfs_ilock(ip, XFS_ILOCK_SHARED);
1264                 }
1265
1266                 if (flags & SYNC_BDFLUSH) {
1267                         if ((flags & SYNC_ATTR) &&
1268                             ((ip->i_update_core) ||
1269                              ((ip->i_itemp != NULL) &&
1270                               (ip->i_itemp->ili_format.ilf_fields != 0)))) {
1271
1272                                 /* Insert marker and drop lock if not already
1273                                  * done.
1274                                  */
1275                                 if (mount_locked) {
1276                                         IPOINTER_INSERT(ip, mp);
1277                                 }
1278
1279                                 /*
1280                                  * We don't want the periodic flushing of the
1281                                  * inodes by vfs_sync() to interfere with
1282                                  * I/O to the file, especially read I/O
1283                                  * where it is only the access time stamp
1284                                  * that is being flushed out.  To prevent
1285                                  * long periods where we have both inode
1286                                  * locks held shared here while reading the
1287                                  * inode's buffer in from disk, we drop the
1288                                  * inode lock while reading in the inode
1289                                  * buffer.  We have to release the buffer
1290                                  * and reacquire the inode lock so that they
1291                                  * are acquired in the proper order (inode
1292                                  * locks first).  The buffer will go at the
1293                                  * end of the lru chain, though, so we can
1294                                  * expect it to still be there when we go
1295                                  * for it again in xfs_iflush().
1296                                  */
1297                                 if ((xfs_ipincount(ip) == 0) &&
1298                                     xfs_iflock_nowait(ip)) {
1299
1300                                         xfs_ifunlock(ip);
1301                                         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1302
1303                                         error = xfs_itobp(mp, NULL, ip,
1304                                                           &dip, &bp, 0, 0);
1305                                         if (!error) {
1306                                                 xfs_buf_relse(bp);
1307                                         } else {
1308                                                 /* Bailing out, remove the
1309                                                  * marker and free it.
1310                                                  */
1311                                                 XFS_MOUNT_ILOCK(mp);
1312                                                 IPOINTER_REMOVE(ip, mp);
1313                                                 XFS_MOUNT_IUNLOCK(mp);
1314
1315                                                 ASSERT(!(lock_flags &
1316                                                         XFS_IOLOCK_SHARED));
1317
1318                                                 kmem_free(ipointer,
1319                                                         sizeof(xfs_iptr_t));
1320                                                 return (0);
1321                                         }
1322
1323                                         /*
1324                                          * Since we dropped the inode lock,
1325                                          * the inode may have been reclaimed.
1326                                          * Therefore, we reacquire the mount
1327                                          * lock and check to see if we were the
1328                                          * inode reclaimed. If this happened
1329                                          * then the ipointer marker will no
1330                                          * longer point back at us. In this
1331                                          * case, move ip along to the inode
1332                                          * after the marker, remove the marker
1333                                          * and continue.
1334                                          */
1335                                         XFS_MOUNT_ILOCK(mp);
1336                                         mount_locked = B_TRUE;
1337
1338                                         if (ip != ipointer->ip_mprev) {
1339                                                 IPOINTER_REMOVE(ip, mp);
1340
1341                                                 ASSERT(!vnode_refed);
1342                                                 ASSERT(!(lock_flags &
1343                                                         XFS_IOLOCK_SHARED));
1344                                                 continue;
1345                                         }
1346
1347                                         ASSERT(ip->i_mount == mp);
1348
1349                                         if (xfs_ilock_nowait(ip,
1350                                                     XFS_ILOCK_SHARED) == 0) {
1351                                                 ASSERT(ip->i_mount == mp);
1352                                                 /*
1353                                                  * We failed to reacquire
1354                                                  * the inode lock without
1355                                                  * sleeping, so just skip
1356                                                  * the inode for now.  We
1357                                                  * clear the ILOCK bit from
1358                                                  * the lock_flags so that we
1359                                                  * won't try to drop a lock
1360                                                  * we don't hold below.
1361                                                  */
1362                                                 lock_flags &= ~XFS_ILOCK_SHARED;
1363                                                 IPOINTER_REMOVE(ip_next, mp);
1364                                         } else if ((xfs_ipincount(ip) == 0) &&
1365                                                    xfs_iflock_nowait(ip)) {
1366                                                 ASSERT(ip->i_mount == mp);
1367                                                 /*
1368                                                  * Since this is vfs_sync()
1369                                                  * calling we only flush the
1370                                                  * inode out if we can lock
1371                                                  * it without sleeping and
1372                                                  * it is not pinned.  Drop
1373                                                  * the mount lock here so
1374                                                  * that we don't hold it for
1375                                                  * too long. We already have
1376                                                  * a marker in the list here.
1377                                                  */
1378                                                 XFS_MOUNT_IUNLOCK(mp);
1379                                                 mount_locked = B_FALSE;
1380                                                 error = xfs_iflush(ip,
1381                                                            XFS_IFLUSH_DELWRI);
1382                                         } else {
1383                                                 ASSERT(ip->i_mount == mp);
1384                                                 IPOINTER_REMOVE(ip_next, mp);
1385                                         }
1386                                 }
1387
1388                         }
1389
1390                 } else {
1391                         if ((flags & SYNC_ATTR) &&
1392                             ((ip->i_update_core) ||
1393                              ((ip->i_itemp != NULL) &&
1394                               (ip->i_itemp->ili_format.ilf_fields != 0)))) {
1395                                 if (mount_locked) {
1396                                         IPOINTER_INSERT(ip, mp);
1397                                 }
1398
1399                                 if (flags & SYNC_WAIT) {
1400                                         xfs_iflock(ip);
1401                                         error = xfs_iflush(ip,
1402                                                            XFS_IFLUSH_SYNC);
1403                                 } else {
1404                                         /*
1405                                          * If we can't acquire the flush
1406                                          * lock, then the inode is already
1407                                          * being flushed so don't bother
1408                                          * waiting.  If we can lock it then
1409                                          * do a delwri flush so we can
1410                                          * combine multiple inode flushes
1411                                          * in each disk write.
1412                                          */
1413                                         if (xfs_iflock_nowait(ip)) {
1414                                                 error = xfs_iflush(ip,
1415                                                            XFS_IFLUSH_DELWRI);
1416                                         }
1417                                         else if (bypassed)
1418                                                 (*bypassed)++;
1419                                 }
1420                         }
1421                 }
1422
1423                 if (lock_flags != 0) {
1424                         xfs_iunlock(ip, lock_flags);
1425                 }
1426
1427                 if (vnode_refed) {
1428                         /*
1429                          * If we had to take a reference on the vnode
1430                          * above, then wait until after we've unlocked
1431                          * the inode to release the reference.  This is
1432                          * because we can be already holding the inode
1433                          * lock when VN_RELE() calls xfs_inactive().
1434                          *
1435                          * Make sure to drop the mount lock before calling
1436                          * VN_RELE() so that we don't trip over ourselves if
1437                          * we have to go for the mount lock again in the
1438                          * inactive code.
1439                          */
1440                         if (mount_locked) {
1441                                 IPOINTER_INSERT(ip, mp);
1442                         }
1443
1444                         VN_RELE(vp);
1445
1446                         vnode_refed = B_FALSE;
1447                 }
1448
1449                 if (error) {
1450                         last_error = error;
1451                 }
1452
1453                 /*
1454                  * bail out if the filesystem is corrupted.
1455                  */
1456                 if (error == EFSCORRUPTED)  {
1457                         if (!mount_locked) {
1458                                 XFS_MOUNT_ILOCK(mp);
1459                                 IPOINTER_REMOVE(ip, mp);
1460                         }
1461                         XFS_MOUNT_IUNLOCK(mp);
1462                         ASSERT(ipointer_in == B_FALSE);
1463                         kmem_free(ipointer, sizeof(xfs_iptr_t));
1464                         return XFS_ERROR(error);
1465                 }
1466
1467                 /* Let other threads have a chance at the mount lock
1468                  * if we have looped many times without dropping the
1469                  * lock.
1470                  */
1471                 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1472                         if (mount_locked) {
1473                                 IPOINTER_INSERT(ip, mp);
1474                         }
1475                 }
1476
1477                 if (mount_locked == B_FALSE) {
1478                         XFS_MOUNT_ILOCK(mp);
1479                         mount_locked = B_TRUE;
1480                         IPOINTER_REMOVE(ip, mp);
1481                         continue;
1482                 }
1483
1484                 ASSERT(ipointer_in == B_FALSE);
1485                 ip = ip->i_mnext;
1486
1487         } while (ip != mp->m_inodes);
1488
1489         XFS_MOUNT_IUNLOCK(mp);
1490
1491         ASSERT(ipointer_in == B_FALSE);
1492
1493         kmem_free(ipointer, sizeof(xfs_iptr_t));
1494         return XFS_ERROR(last_error);
1495 }
1496
1497 /*
1498  * xfs sync routine for internal use
1499  *
1500  * This routine supports all of the flags defined for the generic vfs_sync
1501  * interface as explained above under xfs_sync.
1502  *
1503  */
1504 int
1505 xfs_syncsub(
1506         xfs_mount_t     *mp,
1507         int             flags,
1508         int             *bypassed)
1509 {
1510         int             error = 0;
1511         int             last_error = 0;
1512         uint            log_flags = XFS_LOG_FORCE;
1513         xfs_buf_t       *bp;
1514         xfs_buf_log_item_t      *bip;
1515
1516         /*
1517          * Sync out the log.  This ensures that the log is periodically
1518          * flushed even if there is not enough activity to fill it up.
1519          */
1520         if (flags & SYNC_WAIT)
1521                 log_flags |= XFS_LOG_SYNC;
1522
1523         xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1524
1525         if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1526                 if (flags & SYNC_BDFLUSH)
1527                         xfs_finish_reclaim_all(mp, 1);
1528                 else
1529                         error = xfs_sync_inodes(mp, flags, bypassed);
1530         }
1531
1532         /*
1533          * Flushing out dirty data above probably generated more
1534          * log activity, so if this isn't vfs_sync() then flush
1535          * the log again.
1536          */
1537         if (flags & SYNC_DELWRI) {
1538                 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1539         }
1540
1541         if (flags & SYNC_FSDATA) {
1542                 /*
1543                  * If this is vfs_sync() then only sync the superblock
1544                  * if we can lock it without sleeping and it is not pinned.
1545                  */
1546                 if (flags & SYNC_BDFLUSH) {
1547                         bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1548                         if (bp != NULL) {
1549                                 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1550                                 if ((bip != NULL) &&
1551                                     xfs_buf_item_dirty(bip)) {
1552                                         if (!(XFS_BUF_ISPINNED(bp))) {
1553                                                 XFS_BUF_ASYNC(bp);
1554                                                 error = xfs_bwrite(mp, bp);
1555                                         } else {
1556                                                 xfs_buf_relse(bp);
1557                                         }
1558                                 } else {
1559                                         xfs_buf_relse(bp);
1560                                 }
1561                         }
1562                 } else {
1563                         bp = xfs_getsb(mp, 0);
1564                         /*
1565                          * If the buffer is pinned then push on the log so
1566                          * we won't get stuck waiting in the write for
1567                          * someone, maybe ourselves, to flush the log.
1568                          * Even though we just pushed the log above, we
1569                          * did not have the superblock buffer locked at
1570                          * that point so it can become pinned in between
1571                          * there and here.
1572                          */
1573                         if (XFS_BUF_ISPINNED(bp))
1574                                 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1575                         if (flags & SYNC_WAIT)
1576                                 XFS_BUF_UNASYNC(bp);
1577                         else
1578                                 XFS_BUF_ASYNC(bp);
1579                         error = xfs_bwrite(mp, bp);
1580                 }
1581                 if (error) {
1582                         last_error = error;
1583                 }
1584         }
1585
1586         /*
1587          * If this is the periodic sync, then kick some entries out of
1588          * the reference cache.  This ensures that idle entries are
1589          * eventually kicked out of the cache.
1590          */
1591         if (flags & SYNC_REFCACHE) {
1592                 if (flags & SYNC_WAIT)
1593                         xfs_refcache_purge_mp(mp);
1594                 else
1595                         xfs_refcache_purge_some(mp);
1596         }
1597
1598         /*
1599          * If asked, update the disk superblock with incore counter values if we
1600          * are using non-persistent counters so that they don't get too far out
1601          * of sync if we crash or get a forced shutdown. We don't want to force
1602          * this to disk, just get a transaction into the iclogs....
1603          */
1604         if (flags & SYNC_SUPER)
1605                 xfs_log_sbcount(mp, 0);
1606
1607         /*
1608          * Now check to see if the log needs a "dummy" transaction.
1609          */
1610
1611         if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1612                 xfs_trans_t *tp;
1613                 xfs_inode_t *ip;
1614
1615                 /*
1616                  * Put a dummy transaction in the log to tell
1617                  * recovery that all others are OK.
1618                  */
1619                 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1620                 if ((error = xfs_trans_reserve(tp, 0,
1621                                 XFS_ICHANGE_LOG_RES(mp),
1622                                 0, 0, 0)))  {
1623                         xfs_trans_cancel(tp, 0);
1624                         return error;
1625                 }
1626
1627                 ip = mp->m_rootip;
1628                 xfs_ilock(ip, XFS_ILOCK_EXCL);
1629
1630                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1631                 xfs_trans_ihold(tp, ip);
1632                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1633                 error = xfs_trans_commit(tp, 0);
1634                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1635                 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1636         }
1637
1638         /*
1639          * When shutting down, we need to insure that the AIL is pushed
1640          * to disk or the filesystem can appear corrupt from the PROM.
1641          */
1642         if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1643                 XFS_bflush(mp->m_ddev_targp);
1644                 if (mp->m_rtdev_targp) {
1645                         XFS_bflush(mp->m_rtdev_targp);
1646                 }
1647         }
1648
1649         return XFS_ERROR(last_error);
1650 }
1651
1652 /*
1653  * xfs_vget - called by DMAPI and NFSD to get vnode from file handle
1654  */
1655 STATIC int
1656 xfs_vget(
1657         bhv_desc_t      *bdp,
1658         bhv_vnode_t     **vpp,
1659         fid_t           *fidp)
1660 {
1661         xfs_mount_t     *mp = XFS_BHVTOM(bdp);
1662         xfs_fid_t       *xfid = (struct xfs_fid *)fidp;
1663         xfs_inode_t     *ip;
1664         int             error;
1665         xfs_ino_t       ino;
1666         unsigned int    igen;
1667
1668         /*
1669          * Invalid.  Since handles can be created in user space and passed in
1670          * via gethandle(), this is not cause for a panic.
1671          */
1672         if (xfid->xfs_fid_len != sizeof(*xfid) - sizeof(xfid->xfs_fid_len))
1673                 return XFS_ERROR(EINVAL);
1674
1675         ino  = xfid->xfs_fid_ino;
1676         igen = xfid->xfs_fid_gen;
1677
1678         /*
1679          * NFS can sometimes send requests for ino 0.  Fail them gracefully.
1680          */
1681         if (ino == 0)
1682                 return XFS_ERROR(ESTALE);
1683
1684         error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
1685         if (error) {
1686                 *vpp = NULL;
1687                 return error;
1688         }
1689
1690         if (ip == NULL) {
1691                 *vpp = NULL;
1692                 return XFS_ERROR(EIO);
1693         }
1694
1695         if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
1696                 xfs_iput_new(ip, XFS_ILOCK_SHARED);
1697                 *vpp = NULL;
1698                 return XFS_ERROR(ENOENT);
1699         }
1700
1701         *vpp = XFS_ITOV(ip);
1702         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1703         return 0;
1704 }
1705
1706
1707 #define MNTOPT_LOGBUFS  "logbufs"       /* number of XFS log buffers */
1708 #define MNTOPT_LOGBSIZE "logbsize"      /* size of XFS log buffers */
1709 #define MNTOPT_LOGDEV   "logdev"        /* log device */
1710 #define MNTOPT_RTDEV    "rtdev"         /* realtime I/O device */
1711 #define MNTOPT_BIOSIZE  "biosize"       /* log2 of preferred buffered io size */
1712 #define MNTOPT_WSYNC    "wsync"         /* safe-mode nfs compatible mount */
1713 #define MNTOPT_INO64    "ino64"         /* force inodes into 64-bit range */
1714 #define MNTOPT_NOALIGN  "noalign"       /* turn off stripe alignment */
1715 #define MNTOPT_SWALLOC  "swalloc"       /* turn on stripe width allocation */
1716 #define MNTOPT_SUNIT    "sunit"         /* data volume stripe unit */
1717 #define MNTOPT_SWIDTH   "swidth"        /* data volume stripe width */
1718 #define MNTOPT_NOUUID   "nouuid"        /* ignore filesystem UUID */
1719 #define MNTOPT_MTPT     "mtpt"          /* filesystem mount point */
1720 #define MNTOPT_GRPID    "grpid"         /* group-ID from parent directory */
1721 #define MNTOPT_NOGRPID  "nogrpid"       /* group-ID from current process */
1722 #define MNTOPT_BSDGROUPS    "bsdgroups"    /* group-ID from parent directory */
1723 #define MNTOPT_SYSVGROUPS   "sysvgroups"   /* group-ID from current process */
1724 #define MNTOPT_ALLOCSIZE    "allocsize"    /* preferred allocation size */
1725 #define MNTOPT_NORECOVERY   "norecovery"   /* don't run XFS recovery */
1726 #define MNTOPT_BARRIER  "barrier"       /* use writer barriers for log write and
1727                                          * unwritten extent conversion */
1728 #define MNTOPT_NOBARRIER "nobarrier"    /* .. disable */
1729 #define MNTOPT_OSYNCISOSYNC "osyncisosync" /* o_sync is REALLY o_sync */
1730 #define MNTOPT_64BITINODE   "inode64"   /* inodes can be allocated anywhere */
1731 #define MNTOPT_IKEEP    "ikeep"         /* do not free empty inode clusters */
1732 #define MNTOPT_NOIKEEP  "noikeep"       /* free empty inode clusters */
1733 #define MNTOPT_LARGEIO     "largeio"    /* report large I/O sizes in stat() */
1734 #define MNTOPT_NOLARGEIO   "nolargeio"  /* do not report large I/O sizes
1735                                          * in stat(). */
1736 #define MNTOPT_ATTR2    "attr2"         /* do use attr2 attribute format */
1737 #define MNTOPT_NOATTR2  "noattr2"       /* do not use attr2 attribute format */
1738 #define MNTOPT_FILESTREAM  "filestreams" /* use filestreams allocator */
1739 #define MNTOPT_QUOTA    "quota"         /* disk quotas (user) */
1740 #define MNTOPT_NOQUOTA  "noquota"       /* no quotas */
1741 #define MNTOPT_USRQUOTA "usrquota"      /* user quota enabled */
1742 #define MNTOPT_GRPQUOTA "grpquota"      /* group quota enabled */
1743 #define MNTOPT_PRJQUOTA "prjquota"      /* project quota enabled */
1744 #define MNTOPT_UQUOTA   "uquota"        /* user quota (IRIX variant) */
1745 #define MNTOPT_GQUOTA   "gquota"        /* group quota (IRIX variant) */
1746 #define MNTOPT_PQUOTA   "pquota"        /* project quota (IRIX variant) */
1747 #define MNTOPT_UQUOTANOENF "uqnoenforce"/* user quota limit enforcement */
1748 #define MNTOPT_GQUOTANOENF "gqnoenforce"/* group quota limit enforcement */
1749 #define MNTOPT_PQUOTANOENF "pqnoenforce"/* project quota limit enforcement */
1750 #define MNTOPT_QUOTANOENF  "qnoenforce" /* same as uqnoenforce */
1751 #define MNTOPT_DMAPI    "dmapi"         /* DMI enabled (DMAPI / XDSM) */
1752 #define MNTOPT_XDSM     "xdsm"          /* DMI enabled (DMAPI / XDSM) */
1753 #define MNTOPT_DMI      "dmi"           /* DMI enabled (DMAPI / XDSM) */
1754
1755 STATIC unsigned long
1756 suffix_strtoul(char *s, char **endp, unsigned int base)
1757 {
1758         int     last, shift_left_factor = 0;
1759         char    *value = s;
1760
1761         last = strlen(value) - 1;
1762         if (value[last] == 'K' || value[last] == 'k') {
1763                 shift_left_factor = 10;
1764                 value[last] = '\0';
1765         }
1766         if (value[last] == 'M' || value[last] == 'm') {
1767                 shift_left_factor = 20;
1768                 value[last] = '\0';
1769         }
1770         if (value[last] == 'G' || value[last] == 'g') {
1771                 shift_left_factor = 30;
1772                 value[last] = '\0';
1773         }
1774
1775         return simple_strtoul((const char *)s, endp, base) << shift_left_factor;
1776 }
1777
1778 STATIC int
1779 xfs_parseargs(
1780         struct bhv_desc         *bhv,
1781         char                    *options,
1782         struct xfs_mount_args   *args,
1783         int                     update)
1784 {
1785         bhv_vfs_t               *vfsp = bhvtovfs(bhv);
1786         char                    *this_char, *value, *eov;
1787         int                     dsunit, dswidth, vol_dsunit, vol_dswidth;
1788         int                     iosize;
1789
1790         /*
1791          * Applications using DMI filesystems often expect the
1792          * inode generation number to be monotonically increasing.
1793          * If we delete inode chunks we break this assumption, so
1794          * keep unused inode chunks on disk for DMI filesystems
1795          * until we come up with a better solution.
1796          * Note that if "ikeep" or "noikeep" mount options are
1797          * supplied, then they are honored.
1798          */
1799         if (!(args->flags & XFSMNT_DMAPI))
1800                 args->flags |= XFSMNT_IDELETE;
1801
1802         args->flags |= XFSMNT_BARRIER;
1803         args->flags2 |= XFSMNT2_COMPAT_IOSIZE;
1804
1805         if (!options)
1806                 goto done;
1807
1808         iosize = dsunit = dswidth = vol_dsunit = vol_dswidth = 0;
1809
1810         while ((this_char = strsep(&options, ",")) != NULL) {
1811                 if (!*this_char)
1812                         continue;
1813                 if ((value = strchr(this_char, '=')) != NULL)
1814                         *value++ = 0;
1815
1816                 if (!strcmp(this_char, MNTOPT_LOGBUFS)) {
1817                         if (!value || !*value) {
1818                                 cmn_err(CE_WARN,
1819                                         "XFS: %s option requires an argument",
1820                                         this_char);
1821                                 return EINVAL;
1822                         }
1823                         args->logbufs = simple_strtoul(value, &eov, 10);
1824                 } else if (!strcmp(this_char, MNTOPT_LOGBSIZE)) {
1825                         if (!value || !*value) {
1826                                 cmn_err(CE_WARN,
1827                                         "XFS: %s option requires an argument",
1828                                         this_char);
1829                                 return EINVAL;
1830                         }
1831                         args->logbufsize = suffix_strtoul(value, &eov, 10);
1832                 } else if (!strcmp(this_char, MNTOPT_LOGDEV)) {
1833                         if (!value || !*value) {
1834                                 cmn_err(CE_WARN,
1835                                         "XFS: %s option requires an argument",
1836                                         this_char);
1837                                 return EINVAL;
1838                         }
1839                         strncpy(args->logname, value, MAXNAMELEN);
1840                 } else if (!strcmp(this_char, MNTOPT_MTPT)) {
1841                         if (!value || !*value) {
1842                                 cmn_err(CE_WARN,
1843                                         "XFS: %s option requires an argument",
1844                                         this_char);
1845                                 return EINVAL;
1846                         }
1847                         strncpy(args->mtpt, value, MAXNAMELEN);
1848                 } else if (!strcmp(this_char, MNTOPT_RTDEV)) {
1849                         if (!value || !*value) {
1850                                 cmn_err(CE_WARN,
1851                                         "XFS: %s option requires an argument",
1852                                         this_char);
1853                                 return EINVAL;
1854                         }
1855                         strncpy(args->rtname, value, MAXNAMELEN);
1856                 } else if (!strcmp(this_char, MNTOPT_BIOSIZE)) {
1857                         if (!value || !*value) {
1858                                 cmn_err(CE_WARN,
1859                                         "XFS: %s option requires an argument",
1860                                         this_char);
1861                                 return EINVAL;
1862                         }
1863                         iosize = simple_strtoul(value, &eov, 10);
1864                         args->flags |= XFSMNT_IOSIZE;
1865                         args->iosizelog = (uint8_t) iosize;
1866                 } else if (!strcmp(this_char, MNTOPT_ALLOCSIZE)) {
1867                         if (!value || !*value) {
1868                                 cmn_err(CE_WARN,
1869                                         "XFS: %s option requires an argument",
1870                                         this_char);
1871                                 return EINVAL;
1872                         }
1873                         iosize = suffix_strtoul(value, &eov, 10);
1874                         args->flags |= XFSMNT_IOSIZE;
1875                         args->iosizelog = ffs(iosize) - 1;
1876                 } else if (!strcmp(this_char, MNTOPT_GRPID) ||
1877                            !strcmp(this_char, MNTOPT_BSDGROUPS)) {
1878                         vfsp->vfs_flag |= VFS_GRPID;
1879                 } else if (!strcmp(this_char, MNTOPT_NOGRPID) ||
1880                            !strcmp(this_char, MNTOPT_SYSVGROUPS)) {
1881                         vfsp->vfs_flag &= ~VFS_GRPID;
1882                 } else if (!strcmp(this_char, MNTOPT_WSYNC)) {
1883                         args->flags |= XFSMNT_WSYNC;
1884                 } else if (!strcmp(this_char, MNTOPT_OSYNCISOSYNC)) {
1885                         args->flags |= XFSMNT_OSYNCISOSYNC;
1886                 } else if (!strcmp(this_char, MNTOPT_NORECOVERY)) {
1887                         args->flags |= XFSMNT_NORECOVERY;
1888                 } else if (!strcmp(this_char, MNTOPT_INO64)) {
1889                         args->flags |= XFSMNT_INO64;
1890 #if !XFS_BIG_INUMS
1891                         cmn_err(CE_WARN,
1892                                 "XFS: %s option not allowed on this system",
1893                                 this_char);
1894                         return EINVAL;
1895 #endif
1896                 } else if (!strcmp(this_char, MNTOPT_NOALIGN)) {
1897                         args->flags |= XFSMNT_NOALIGN;
1898                 } else if (!strcmp(this_char, MNTOPT_SWALLOC)) {
1899                         args->flags |= XFSMNT_SWALLOC;
1900                 } else if (!strcmp(this_char, MNTOPT_SUNIT)) {
1901                         if (!value || !*value) {
1902                                 cmn_err(CE_WARN,
1903                                         "XFS: %s option requires an argument",
1904                                         this_char);
1905                                 return EINVAL;
1906                         }
1907                         dsunit = simple_strtoul(value, &eov, 10);
1908                 } else if (!strcmp(this_char, MNTOPT_SWIDTH)) {
1909                         if (!value || !*value) {
1910                                 cmn_err(CE_WARN,
1911                                         "XFS: %s option requires an argument",
1912                                         this_char);
1913                                 return EINVAL;
1914                         }
1915                         dswidth = simple_strtoul(value, &eov, 10);
1916                 } else if (!strcmp(this_char, MNTOPT_64BITINODE)) {
1917                         args->flags &= ~XFSMNT_32BITINODES;
1918 #if !XFS_BIG_INUMS
1919                         cmn_err(CE_WARN,
1920                                 "XFS: %s option not allowed on this system",
1921                                 this_char);
1922                         return EINVAL;
1923 #endif
1924                 } else if (!strcmp(this_char, MNTOPT_NOUUID)) {
1925                         args->flags |= XFSMNT_NOUUID;
1926                 } else if (!strcmp(this_char, MNTOPT_BARRIER)) {
1927                         args->flags |= XFSMNT_BARRIER;
1928                 } else if (!strcmp(this_char, MNTOPT_NOBARRIER)) {
1929                         args->flags &= ~XFSMNT_BARRIER;
1930                 } else if (!strcmp(this_char, MNTOPT_IKEEP)) {
1931                         args->flags &= ~XFSMNT_IDELETE;
1932                 } else if (!strcmp(this_char, MNTOPT_NOIKEEP)) {
1933                         args->flags |= XFSMNT_IDELETE;
1934                 } else if (!strcmp(this_char, MNTOPT_LARGEIO)) {
1935                         args->flags2 &= ~XFSMNT2_COMPAT_IOSIZE;
1936                 } else if (!strcmp(this_char, MNTOPT_NOLARGEIO)) {
1937                         args->flags2 |= XFSMNT2_COMPAT_IOSIZE;
1938                 } else if (!strcmp(this_char, MNTOPT_ATTR2)) {
1939                         args->flags |= XFSMNT_ATTR2;
1940                 } else if (!strcmp(this_char, MNTOPT_NOATTR2)) {
1941                         args->flags &= ~XFSMNT_ATTR2;
1942                 } else if (!strcmp(this_char, MNTOPT_FILESTREAM)) {
1943                         args->flags2 |= XFSMNT2_FILESTREAMS;
1944                 } else if (!strcmp(this_char, MNTOPT_NOQUOTA)) {
1945                         args->flags &= ~(XFSMNT_UQUOTAENF|XFSMNT_UQUOTA);
1946                         args->flags &= ~(XFSMNT_GQUOTAENF|XFSMNT_GQUOTA);
1947                 } else if (!strcmp(this_char, MNTOPT_QUOTA) ||
1948                            !strcmp(this_char, MNTOPT_UQUOTA) ||
1949                            !strcmp(this_char, MNTOPT_USRQUOTA)) {
1950                         args->flags |= XFSMNT_UQUOTA | XFSMNT_UQUOTAENF;
1951                 } else if (!strcmp(this_char, MNTOPT_QUOTANOENF) ||
1952                            !strcmp(this_char, MNTOPT_UQUOTANOENF)) {
1953                         args->flags |= XFSMNT_UQUOTA;
1954                         args->flags &= ~XFSMNT_UQUOTAENF;
1955                 } else if (!strcmp(this_char, MNTOPT_PQUOTA) ||
1956                            !strcmp(this_char, MNTOPT_PRJQUOTA)) {
1957                         args->flags |= XFSMNT_PQUOTA | XFSMNT_PQUOTAENF;
1958                 } else if (!strcmp(this_char, MNTOPT_PQUOTANOENF)) {
1959                         args->flags |= XFSMNT_PQUOTA;
1960                         args->flags &= ~XFSMNT_PQUOTAENF;
1961                 } else if (!strcmp(this_char, MNTOPT_GQUOTA) ||
1962                            !strcmp(this_char, MNTOPT_GRPQUOTA)) {
1963                         args->flags |= XFSMNT_GQUOTA | XFSMNT_GQUOTAENF;
1964                 } else if (!strcmp(this_char, MNTOPT_GQUOTANOENF)) {
1965                         args->flags |= XFSMNT_GQUOTA;
1966                         args->flags &= ~XFSMNT_GQUOTAENF;
1967                 } else if (!strcmp(this_char, MNTOPT_DMAPI)) {
1968                         args->flags |= XFSMNT_DMAPI;
1969                 } else if (!strcmp(this_char, MNTOPT_XDSM)) {
1970                         args->flags |= XFSMNT_DMAPI;
1971                 } else if (!strcmp(this_char, MNTOPT_DMI)) {
1972                         args->flags |= XFSMNT_DMAPI;
1973                 } else if (!strcmp(this_char, "ihashsize")) {
1974                         cmn_err(CE_WARN,
1975         "XFS: ihashsize no longer used, option is deprecated.");
1976                 } else if (!strcmp(this_char, "osyncisdsync")) {
1977                         /* no-op, this is now the default */
1978                         cmn_err(CE_WARN,
1979         "XFS: osyncisdsync is now the default, option is deprecated.");
1980                 } else if (!strcmp(this_char, "irixsgid")) {
1981                         cmn_err(CE_WARN,
1982         "XFS: irixsgid is now a sysctl(2) variable, option is deprecated.");
1983                 } else {
1984                         cmn_err(CE_WARN,
1985                                 "XFS: unknown mount option [%s].", this_char);
1986                         return EINVAL;
1987                 }
1988         }
1989
1990         if (args->flags & XFSMNT_NORECOVERY) {
1991                 if ((vfsp->vfs_flag & VFS_RDONLY) == 0) {
1992                         cmn_err(CE_WARN,
1993                                 "XFS: no-recovery mounts must be read-only.");
1994                         return EINVAL;
1995                 }
1996         }
1997
1998         if ((args->flags & XFSMNT_NOALIGN) && (dsunit || dswidth)) {
1999                 cmn_err(CE_WARN,
2000         "XFS: sunit and swidth options incompatible with the noalign option");
2001                 return EINVAL;
2002         }
2003
2004         if ((args->flags & XFSMNT_GQUOTA) && (args->flags & XFSMNT_PQUOTA)) {
2005                 cmn_err(CE_WARN,
2006                         "XFS: cannot mount with both project and group quota");
2007                 return EINVAL;
2008         }
2009
2010         if ((args->flags & XFSMNT_DMAPI) && *args->mtpt == '\0') {
2011                 printk("XFS: %s option needs the mount point option as well\n",
2012                         MNTOPT_DMAPI);
2013                 return EINVAL;
2014         }
2015
2016         if ((dsunit && !dswidth) || (!dsunit && dswidth)) {
2017                 cmn_err(CE_WARN,
2018                         "XFS: sunit and swidth must be specified together");
2019                 return EINVAL;
2020         }
2021
2022         if (dsunit && (dswidth % dsunit != 0)) {
2023                 cmn_err(CE_WARN,
2024         "XFS: stripe width (%d) must be a multiple of the stripe unit (%d)",
2025                         dswidth, dsunit);
2026                 return EINVAL;
2027         }
2028
2029         if ((args->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
2030                 if (dsunit) {
2031                         args->sunit = dsunit;
2032                         args->flags |= XFSMNT_RETERR;
2033                 } else {
2034                         args->sunit = vol_dsunit;
2035                 }
2036                 dswidth ? (args->swidth = dswidth) :
2037                           (args->swidth = vol_dswidth);
2038         } else {
2039                 args->sunit = args->swidth = 0;
2040         }
2041
2042 done:
2043         if (args->flags & XFSMNT_32BITINODES)
2044                 vfsp->vfs_flag |= VFS_32BITINODES;
2045         if (args->flags2)
2046                 args->flags |= XFSMNT_FLAGS2;
2047         return 0;
2048 }
2049
2050 STATIC int
2051 xfs_showargs(
2052         struct bhv_desc         *bhv,
2053         struct seq_file         *m)
2054 {
2055         static struct proc_xfs_info {
2056                 int     flag;
2057                 char    *str;
2058         } xfs_info[] = {
2059                 /* the few simple ones we can get from the mount struct */
2060                 { XFS_MOUNT_WSYNC,              "," MNTOPT_WSYNC },
2061                 { XFS_MOUNT_INO64,              "," MNTOPT_INO64 },
2062                 { XFS_MOUNT_NOALIGN,            "," MNTOPT_NOALIGN },
2063                 { XFS_MOUNT_SWALLOC,            "," MNTOPT_SWALLOC },
2064                 { XFS_MOUNT_NOUUID,             "," MNTOPT_NOUUID },
2065                 { XFS_MOUNT_NORECOVERY,         "," MNTOPT_NORECOVERY },
2066                 { XFS_MOUNT_OSYNCISOSYNC,       "," MNTOPT_OSYNCISOSYNC },
2067                 { 0, NULL }
2068         };
2069         struct proc_xfs_info    *xfs_infop;
2070         struct xfs_mount        *mp = XFS_BHVTOM(bhv);
2071         struct bhv_vfs          *vfsp = XFS_MTOVFS(mp);
2072
2073         for (xfs_infop = xfs_info; xfs_infop->flag; xfs_infop++) {
2074                 if (mp->m_flags & xfs_infop->flag)
2075                         seq_puts(m, xfs_infop->str);
2076         }
2077
2078         if (mp->m_flags & XFS_MOUNT_DFLT_IOSIZE)
2079                 seq_printf(m, "," MNTOPT_ALLOCSIZE "=%dk",
2080                                 (int)(1 << mp->m_writeio_log) >> 10);
2081
2082         if (mp->m_logbufs > 0)
2083                 seq_printf(m, "," MNTOPT_LOGBUFS "=%d", mp->m_logbufs);
2084         if (mp->m_logbsize > 0)
2085                 seq_printf(m, "," MNTOPT_LOGBSIZE "=%dk", mp->m_logbsize >> 10);
2086
2087         if (mp->m_logname)
2088                 seq_printf(m, "," MNTOPT_LOGDEV "=%s", mp->m_logname);
2089         if (mp->m_rtname)
2090                 seq_printf(m, "," MNTOPT_RTDEV "=%s", mp->m_rtname);
2091
2092         if (mp->m_dalign > 0)
2093                 seq_printf(m, "," MNTOPT_SUNIT "=%d",
2094                                 (int)XFS_FSB_TO_BB(mp, mp->m_dalign));
2095         if (mp->m_swidth > 0)
2096                 seq_printf(m, "," MNTOPT_SWIDTH "=%d",
2097                                 (int)XFS_FSB_TO_BB(mp, mp->m_swidth));
2098
2099         if (!(mp->m_flags & XFS_MOUNT_IDELETE))
2100                 seq_printf(m, "," MNTOPT_IKEEP);
2101         if (!(mp->m_flags & XFS_MOUNT_COMPAT_IOSIZE))
2102                 seq_printf(m, "," MNTOPT_LARGEIO);
2103
2104         if (!(vfsp->vfs_flag & VFS_32BITINODES))
2105                 seq_printf(m, "," MNTOPT_64BITINODE);
2106         if (vfsp->vfs_flag & VFS_GRPID)
2107                 seq_printf(m, "," MNTOPT_GRPID);
2108
2109         if (mp->m_qflags & XFS_UQUOTA_ACCT) {
2110                 if (mp->m_qflags & XFS_UQUOTA_ENFD)
2111                         seq_puts(m, "," MNTOPT_USRQUOTA);
2112                 else
2113                         seq_puts(m, "," MNTOPT_UQUOTANOENF);
2114         }
2115
2116         if (mp->m_qflags & XFS_PQUOTA_ACCT) {
2117                 if (mp->m_qflags & XFS_OQUOTA_ENFD)
2118                         seq_puts(m, "," MNTOPT_PRJQUOTA);
2119                 else
2120                         seq_puts(m, "," MNTOPT_PQUOTANOENF);
2121         }
2122
2123         if (mp->m_qflags & XFS_GQUOTA_ACCT) {
2124                 if (mp->m_qflags & XFS_OQUOTA_ENFD)
2125                         seq_puts(m, "," MNTOPT_GRPQUOTA);
2126                 else
2127                         seq_puts(m, "," MNTOPT_GQUOTANOENF);
2128         }
2129
2130         if (!(mp->m_qflags & XFS_ALL_QUOTA_ACCT))
2131                 seq_puts(m, "," MNTOPT_NOQUOTA);
2132
2133         if (vfsp->vfs_flag & VFS_DMI)
2134                 seq_puts(m, "," MNTOPT_DMAPI);
2135         return 0;
2136 }
2137
2138 /*
2139  * Second stage of a freeze. The data is already frozen so we only
2140  * need to take care of themetadata. Once that's done write a dummy
2141  * record to dirty the log in case of a crash while frozen.
2142  */
2143 STATIC void
2144 xfs_freeze(
2145         bhv_desc_t      *bdp)
2146 {
2147         xfs_mount_t     *mp = XFS_BHVTOM(bdp);
2148
2149         xfs_attr_quiesce(mp);
2150         xfs_fs_log_dummy(mp);
2151 }
2152
2153
2154 bhv_vfsops_t xfs_vfsops = {
2155         BHV_IDENTITY_INIT(VFS_BHV_XFS,VFS_POSITION_XFS),
2156         .vfs_parseargs          = xfs_parseargs,
2157         .vfs_showargs           = xfs_showargs,
2158         .vfs_mount              = xfs_mount,
2159         .vfs_unmount            = xfs_unmount,
2160         .vfs_mntupdate          = xfs_mntupdate,
2161         .vfs_root               = xfs_root,
2162         .vfs_statvfs            = xfs_statvfs,
2163         .vfs_sync               = xfs_sync,
2164         .vfs_vget               = xfs_vget,
2165         .vfs_quotactl           = (vfs_quotactl_t)fs_nosys,
2166         .vfs_init_vnode         = xfs_initialize_vnode,
2167         .vfs_force_shutdown     = xfs_do_force_shutdown,
2168         .vfs_freeze             = xfs_freeze,
2169 };