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