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