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