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