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