2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_types.h"
24 #include "xfs_trans.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"
46 #include "xfs_buf_item.h"
47 #include "xfs_log_priv.h"
48 #include "xfs_dir2_trace.h"
49 #include "xfs_extfree_item.h"
53 #include "xfs_mru_cache.h"
54 #include "xfs_filestream.h"
55 #include "xfs_fsops.h"
56 #include "xfs_vnodeops.h"
57 #include "xfs_vfsops.h"
58 #include "xfs_utils.h"
64 #ifdef XFS_DABUF_DEBUG
65 extern spinlock_t xfs_dabuf_global_lock;
66 spin_lock_init(&xfs_dabuf_global_lock);
70 * Initialize all of the zone allocators we use.
72 xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
73 "xfs_bmap_free_item");
74 xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
76 xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
78 kmem_zone_init(sizeof(xfs_da_state_t), "xfs_da_state");
79 xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
80 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
81 xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
83 xfs_filestream_init();
86 * The size of the zone allocated buf log item is the maximum
87 * size possible under XFS. This wastes a little bit of memory,
88 * but it is much faster.
91 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
92 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
93 NBWORD) * sizeof(int))),
96 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
97 ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
98 sizeof(xfs_extent_t))),
101 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
102 ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
103 sizeof(xfs_extent_t))),
107 * These zones warrant special memory allocator hints
110 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
111 KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
112 KM_ZONE_SPREAD, NULL);
114 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
115 KM_ZONE_SPREAD, NULL);
118 * Allocate global trace buffers.
120 #ifdef XFS_ALLOC_TRACE
121 xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
123 #ifdef XFS_BMAP_TRACE
124 xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
126 #ifdef XFS_BMBT_TRACE
127 xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
129 #ifdef XFS_ATTR_TRACE
130 xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
132 #ifdef XFS_DIR2_TRACE
133 xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
138 #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
139 xfs_error_test_init();
140 #endif /* DEBUG || INDUCE_IO_ERROR */
143 xfs_sysctl_register();
150 extern kmem_zone_t *xfs_inode_zone;
151 extern kmem_zone_t *xfs_efd_zone;
152 extern kmem_zone_t *xfs_efi_zone;
154 xfs_cleanup_procfs();
155 xfs_sysctl_unregister();
156 xfs_filestream_uninit();
157 xfs_mru_cache_uninit();
158 xfs_acl_zone_destroy(xfs_acl_zone);
160 #ifdef XFS_DIR2_TRACE
161 ktrace_free(xfs_dir2_trace_buf);
163 #ifdef XFS_ATTR_TRACE
164 ktrace_free(xfs_attr_trace_buf);
166 #ifdef XFS_BMBT_TRACE
167 ktrace_free(xfs_bmbt_trace_buf);
169 #ifdef XFS_BMAP_TRACE
170 ktrace_free(xfs_bmap_trace_buf);
172 #ifdef XFS_ALLOC_TRACE
173 ktrace_free(xfs_alloc_trace_buf);
176 kmem_zone_destroy(xfs_bmap_free_item_zone);
177 kmem_zone_destroy(xfs_btree_cur_zone);
178 kmem_zone_destroy(xfs_inode_zone);
179 kmem_zone_destroy(xfs_trans_zone);
180 kmem_zone_destroy(xfs_da_state_zone);
181 kmem_zone_destroy(xfs_dabuf_zone);
182 kmem_zone_destroy(xfs_buf_item_zone);
183 kmem_zone_destroy(xfs_efd_zone);
184 kmem_zone_destroy(xfs_efi_zone);
185 kmem_zone_destroy(xfs_ifork_zone);
186 kmem_zone_destroy(xfs_ili_zone);
192 * This function fills in xfs_mount_t fields based on mount args.
193 * Note: the superblock has _not_ yet been read in.
197 struct xfs_mount_args *ap,
198 struct xfs_mount *mp)
200 /* Values are in BBs */
201 if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
203 * At this point the superblock has not been read
204 * in, therefore we do not know the block size.
205 * Before the mount call ends we will convert
208 mp->m_dalign = ap->sunit;
209 mp->m_swidth = ap->swidth;
212 if (ap->logbufs != -1 &&
214 (ap->logbufs < XLOG_MIN_ICLOGS ||
215 ap->logbufs > XLOG_MAX_ICLOGS)) {
217 "XFS: invalid logbufs value: %d [not %d-%d]",
218 ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
219 return XFS_ERROR(EINVAL);
221 mp->m_logbufs = ap->logbufs;
222 if (ap->logbufsize != -1 &&
223 ap->logbufsize != 0 &&
224 (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
225 ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
226 !is_power_of_2(ap->logbufsize))) {
228 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
230 return XFS_ERROR(EINVAL);
232 mp->m_logbsize = ap->logbufsize;
233 mp->m_fsname_len = strlen(ap->fsname) + 1;
234 mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
235 strcpy(mp->m_fsname, ap->fsname);
237 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
238 strcpy(mp->m_rtname, ap->rtname);
240 if (ap->logname[0]) {
241 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
242 strcpy(mp->m_logname, ap->logname);
245 if (ap->flags & XFSMNT_WSYNC)
246 mp->m_flags |= XFS_MOUNT_WSYNC;
248 if (ap->flags & XFSMNT_INO64) {
249 mp->m_flags |= XFS_MOUNT_INO64;
250 mp->m_inoadd = XFS_INO64_OFFSET;
253 if (ap->flags & XFSMNT_RETERR)
254 mp->m_flags |= XFS_MOUNT_RETERR;
255 if (ap->flags & XFSMNT_NOALIGN)
256 mp->m_flags |= XFS_MOUNT_NOALIGN;
257 if (ap->flags & XFSMNT_SWALLOC)
258 mp->m_flags |= XFS_MOUNT_SWALLOC;
259 if (ap->flags & XFSMNT_OSYNCISOSYNC)
260 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
261 if (ap->flags & XFSMNT_32BITINODES)
262 mp->m_flags |= XFS_MOUNT_32BITINODES;
264 if (ap->flags & XFSMNT_IOSIZE) {
265 if (ap->iosizelog > XFS_MAX_IO_LOG ||
266 ap->iosizelog < XFS_MIN_IO_LOG) {
268 "XFS: invalid log iosize: %d [not %d-%d]",
269 ap->iosizelog, XFS_MIN_IO_LOG,
271 return XFS_ERROR(EINVAL);
274 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
275 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
278 if (ap->flags & XFSMNT_IKEEP)
279 mp->m_flags |= XFS_MOUNT_IKEEP;
280 if (ap->flags & XFSMNT_DIRSYNC)
281 mp->m_flags |= XFS_MOUNT_DIRSYNC;
282 if (ap->flags & XFSMNT_ATTR2)
283 mp->m_flags |= XFS_MOUNT_ATTR2;
285 if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
286 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
289 * no recovery flag requires a read-only mount
291 if (ap->flags & XFSMNT_NORECOVERY) {
292 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
294 "XFS: tried to mount a FS read-write without recovery!");
295 return XFS_ERROR(EINVAL);
297 mp->m_flags |= XFS_MOUNT_NORECOVERY;
300 if (ap->flags & XFSMNT_NOUUID)
301 mp->m_flags |= XFS_MOUNT_NOUUID;
302 if (ap->flags & XFSMNT_BARRIER)
303 mp->m_flags |= XFS_MOUNT_BARRIER;
305 mp->m_flags &= ~XFS_MOUNT_BARRIER;
307 if (ap->flags2 & XFSMNT2_FILESTREAMS)
308 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
310 if (ap->flags & XFSMNT_DMAPI)
311 mp->m_flags |= XFS_MOUNT_DMAPI;
316 * This function fills in xfs_mount_t fields based on mount args.
317 * Note: the superblock _has_ now been read in.
321 struct xfs_mount_args *ap,
322 struct xfs_mount *mp)
324 int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
326 /* Fail a mount where the logbuf is smaller then the log stripe */
327 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
328 if ((ap->logbufsize <= 0) &&
329 (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
330 mp->m_logbsize = mp->m_sb.sb_logsunit;
331 } else if (ap->logbufsize > 0 &&
332 ap->logbufsize < mp->m_sb.sb_logsunit) {
334 "XFS: logbuf size must be greater than or equal to log stripe size");
335 return XFS_ERROR(EINVAL);
338 /* Fail a mount if the logbuf is larger than 32K */
339 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
341 "XFS: logbuf size for version 1 logs must be 16K or 32K");
342 return XFS_ERROR(EINVAL);
346 if (xfs_sb_version_hasattr2(&mp->m_sb))
347 mp->m_flags |= XFS_MOUNT_ATTR2;
350 * prohibit r/w mounts of read-only filesystems
352 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
354 "XFS: cannot mount a read-only filesystem as read-write");
355 return XFS_ERROR(EROFS);
359 * check for shared mount.
361 if (ap->flags & XFSMNT_SHARED) {
362 if (!xfs_sb_version_hasshared(&mp->m_sb))
363 return XFS_ERROR(EINVAL);
366 * For IRIX 6.5, shared mounts must have the shared
367 * version bit set, have the persistent readonly
368 * field set, must be version 0 and can only be mounted
371 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
372 (mp->m_sb.sb_shared_vn != 0))
373 return XFS_ERROR(EINVAL);
375 mp->m_flags |= XFS_MOUNT_SHARED;
378 * Shared XFS V0 can't deal with DMI. Return EINVAL.
380 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
381 return XFS_ERROR(EINVAL);
384 if (ap->flags & XFSMNT_UQUOTA) {
385 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
386 if (ap->flags & XFSMNT_UQUOTAENF)
387 mp->m_qflags |= XFS_UQUOTA_ENFD;
390 if (ap->flags & XFSMNT_GQUOTA) {
391 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
392 if (ap->flags & XFSMNT_GQUOTAENF)
393 mp->m_qflags |= XFS_OQUOTA_ENFD;
394 } else if (ap->flags & XFSMNT_PQUOTA) {
395 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
396 if (ap->flags & XFSMNT_PQUOTAENF)
397 mp->m_qflags |= XFS_OQUOTA_ENFD;
406 * The file system configurations are:
407 * (1) device (partition) with data and internal log
408 * (2) logical volume with data and log subvolumes.
409 * (3) logical volume with data, log, and realtime subvolumes.
411 * We only have to handle opening the log and realtime volumes here if
412 * they are present. The data subvolume has already been opened by
413 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
417 struct xfs_mount *mp,
418 struct xfs_mount_args *args,
421 struct block_device *ddev, *logdev, *rtdev;
422 int flags = 0, error;
424 ddev = mp->m_super->s_bdev;
425 logdev = rtdev = NULL;
427 error = xfs_dmops_get(mp, args);
430 error = xfs_qmops_get(mp, args);
434 if (args->flags & XFSMNT_QUIET)
435 flags |= XFS_MFSI_QUIET;
438 * Open real time and log devices - order is important.
440 if (args->logname[0]) {
441 error = xfs_blkdev_get(mp, args->logname, &logdev);
445 if (args->rtname[0]) {
446 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
448 xfs_blkdev_put(logdev);
452 if (rtdev == ddev || rtdev == logdev) {
454 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
455 xfs_blkdev_put(logdev);
456 xfs_blkdev_put(rtdev);
462 * Setup xfs_mount buffer target pointers
465 mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
466 if (!mp->m_ddev_targp) {
467 xfs_blkdev_put(logdev);
468 xfs_blkdev_put(rtdev);
472 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
473 if (!mp->m_rtdev_targp) {
474 xfs_blkdev_put(logdev);
475 xfs_blkdev_put(rtdev);
479 mp->m_logdev_targp = (logdev && logdev != ddev) ?
480 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
481 if (!mp->m_logdev_targp) {
482 xfs_blkdev_put(logdev);
483 xfs_blkdev_put(rtdev);
488 * Setup flags based on mount(2) options and then the superblock
490 error = xfs_start_flags(args, mp);
493 error = xfs_readsb(mp, flags);
496 error = xfs_finish_flags(args, mp);
501 * Setup xfs_mount buffer target pointers based on superblock
503 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
504 mp->m_sb.sb_sectsize);
505 if (!error && logdev && logdev != ddev) {
506 unsigned int log_sector_size = BBSIZE;
508 if (xfs_sb_version_hassector(&mp->m_sb))
509 log_sector_size = mp->m_sb.sb_logsectsize;
510 error = xfs_setsize_buftarg(mp->m_logdev_targp,
511 mp->m_sb.sb_blocksize,
515 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
516 mp->m_sb.sb_blocksize,
517 mp->m_sb.sb_sectsize);
521 if (mp->m_flags & XFS_MOUNT_BARRIER)
522 xfs_mountfs_check_barriers(mp);
524 if ((error = xfs_filestream_mount(mp)))
527 error = xfs_mountfs(mp, flags);
531 XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
539 xfs_binval(mp->m_ddev_targp);
540 if (logdev && logdev != ddev)
541 xfs_binval(mp->m_logdev_targp);
543 xfs_binval(mp->m_rtdev_targp);
545 xfs_unmountfs_close(mp, credp);
559 int unmount_event_wanted = 0;
560 int unmount_event_flags = 0;
561 int xfs_unmountfs_needed = 0;
568 if (mp->m_flags & XFS_MOUNT_DMAPI) {
569 error = XFS_SEND_PREUNMOUNT(mp,
570 rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
572 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
573 0:DM_FLAGS_UNWANTED);
575 return XFS_ERROR(error);
576 unmount_event_wanted = 1;
577 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
578 0 : DM_FLAGS_UNWANTED;
583 * Blow away any referenced inode in the filestreams cache.
584 * This can and will cause log traffic as inodes go inactive
587 xfs_filestream_unmount(mp);
589 XFS_bflush(mp->m_ddev_targp);
590 error = xfs_unmount_flush(mp, 0);
594 ASSERT(vn_count(rvp) == 1);
597 * Drop the reference count
602 * If we're forcing a shutdown, typically because of a media error,
603 * we want to make sure we invalidate dirty pages that belong to
604 * referenced vnodes as well.
606 if (XFS_FORCED_SHUTDOWN(mp)) {
607 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
608 ASSERT(error != EFSCORRUPTED);
610 xfs_unmountfs_needed = 1;
613 /* Send DMAPI event, if required.
614 * Then do xfs_unmountfs() if needed.
615 * Then return error (or zero).
617 if (unmount_event_wanted) {
618 /* Note: mp structure must still exist for
619 * XFS_SEND_UNMOUNT() call.
621 XFS_SEND_UNMOUNT(mp, error == 0 ? rip : NULL,
622 DM_RIGHT_NULL, 0, error, unmount_event_flags);
624 if (xfs_unmountfs_needed) {
626 * Call common unmount function to flush to disk
627 * and free the super block buffer & mount structures.
629 xfs_unmountfs(mp, credp);
632 kmem_free(mp, sizeof(xfs_mount_t));
635 return XFS_ERROR(error);
642 int count = 0, pincount;
644 xfs_flush_buftarg(mp->m_ddev_targp, 0);
645 xfs_finish_reclaim_all(mp, 0);
647 /* This loop must run at least twice.
648 * The first instance of the loop will flush
649 * most meta data but that will generate more
650 * meta data (typically directory updates).
651 * Which then must be flushed and logged before
652 * we can write the unmount record.
655 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
656 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
667 * Second stage of a quiesce. The data is already synced, now we have to take
668 * care of the metadata. New transactions are already blocked, so we need to
669 * wait for any remaining transactions to drain out before proceding.
675 /* wait for all modifications to complete */
676 while (atomic_read(&mp->m_active_trans) > 0)
679 /* flush inodes and push all remaining buffers out to disk */
682 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
684 /* Push the superblock and write an unmount record */
685 xfs_log_sbcount(mp, 1);
686 xfs_log_unmount_write(mp);
687 xfs_unmountfs_writesb(mp);
692 struct xfs_mount *mp,
694 struct xfs_mount_args *args)
696 if (!(*flags & MS_RDONLY)) { /* rw/ro -> rw */
697 if (mp->m_flags & XFS_MOUNT_RDONLY)
698 mp->m_flags &= ~XFS_MOUNT_RDONLY;
699 if (args->flags & XFSMNT_BARRIER) {
700 mp->m_flags |= XFS_MOUNT_BARRIER;
701 xfs_mountfs_check_barriers(mp);
703 mp->m_flags &= ~XFS_MOUNT_BARRIER;
705 } else if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { /* rw -> ro */
706 xfs_filestream_flush(mp);
707 xfs_sync(mp, SYNC_DATA_QUIESCE);
708 xfs_attr_quiesce(mp);
709 mp->m_flags |= XFS_MOUNT_RDONLY;
715 * xfs_unmount_flush implements a set of flush operation on special
716 * inodes, which are needed as a separate set of operations so that
717 * they can be called as part of relocation process.
721 xfs_mount_t *mp, /* Mount structure we are getting
723 int relocation) /* Called from vfs relocation. */
725 xfs_inode_t *rip = mp->m_rootip;
727 xfs_inode_t *rsumip = NULL;
728 bhv_vnode_t *rvp = XFS_ITOV(rip);
731 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
735 * Flush out the real time inodes.
737 if ((rbmip = mp->m_rbmip) != NULL) {
738 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
740 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
741 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
743 if (error == EFSCORRUPTED)
746 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
748 rsumip = mp->m_rsumip;
749 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
751 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
752 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
754 if (error == EFSCORRUPTED)
757 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
761 * Synchronously flush root inode to disk
763 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
764 if (error == EFSCORRUPTED)
767 if (vn_count(rvp) != 1 && !relocation) {
768 xfs_iunlock(rip, XFS_ILOCK_EXCL);
769 return XFS_ERROR(EBUSY);
773 * Release dquot that rootinode, rbmino and rsumino might be holding,
774 * flush and purge the quota inodes.
776 error = XFS_QM_UNMOUNT(mp);
777 if (error == EFSCORRUPTED)
785 xfs_iunlock(rip, XFS_ILOCK_EXCL);
792 xfs_iunlock(rip, XFS_ILOCK_EXCL);
794 return XFS_ERROR(EFSCORRUPTED);
798 * xfs_sync flushes any pending I/O to file system vfsp.
800 * This routine is called by vfs_sync() to make sure that things make it
801 * out to disk eventually, on sync() system calls to flush out everything,
802 * and when the file system is unmounted. For the vfs_sync() case, all
803 * we really need to do is sync out the log to make all of our meta-data
804 * updates permanent (except for timestamps). For calls from pflushd(),
805 * dirty pages are kept moving by calling pdflush() on the inodes
806 * containing them. We also flush the inodes that we can lock without
807 * sleeping and the superblock if we can lock it without sleeping from
808 * vfs_sync() so that items at the tail of the log are always moving out.
811 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
812 * to sleep if we can help it. All we really need
813 * to do is ensure that the log is synced at least
814 * periodically. We also push the inodes and
815 * superblock if we can lock them without sleeping
816 * and they are not pinned.
817 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
818 * set, then we really want to lock each inode and flush
820 * SYNC_WAIT - All the flushes that take place in this call should
822 * SYNC_DELWRI - This tells us to push dirty pages associated with
823 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
824 * determine if they should be flushed sync, async, or
826 * SYNC_CLOSE - This flag is passed when the system is being
827 * unmounted. We should sync and invalidate everything.
828 * SYNC_FSDATA - This indicates that the caller would like to make
829 * sure the superblock is safe on disk. We can ensure
830 * this by simply making sure the log gets flushed
831 * if SYNC_BDFLUSH is set, and by actually writing it
833 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
834 * before we return (including direct I/O). Forms the drain
835 * side of the write barrier needed to safely quiesce the
847 * Get the Quota Manager to flush the dquots.
849 * If XFS quota support is not enabled or this filesystem
850 * instance does not use quotas XFS_QM_DQSYNC will always
853 error = XFS_QM_DQSYNC(mp, flags);
856 * If we got an IO error, we will be shutting down.
857 * So, there's nothing more for us to do here.
859 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
860 if (XFS_FORCED_SHUTDOWN(mp))
861 return XFS_ERROR(error);
864 if (flags & SYNC_IOWAIT)
865 xfs_filestream_flush(mp);
867 return xfs_syncsub(mp, flags, NULL);
871 * xfs sync routine for internal use
873 * This routine supports all of the flags defined for the generic vfs_sync
874 * interface as explained above under xfs_sync.
883 xfs_inode_t *ip = NULL;
884 bhv_vnode_t *vp = NULL;
889 uint base_lock_flags;
890 boolean_t mount_locked;
891 boolean_t vnode_refed;
893 xfs_iptr_t *ipointer;
895 boolean_t ipointer_in = B_FALSE;
897 #define IPOINTER_SET ipointer_in = B_TRUE
898 #define IPOINTER_CLR ipointer_in = B_FALSE
905 /* Insert a marker record into the inode list after inode ip. The list
906 * must be locked when this is called. After the call the list will no
909 #define IPOINTER_INSERT(ip, mp) { \
910 ASSERT(ipointer_in == B_FALSE); \
911 ipointer->ip_mnext = ip->i_mnext; \
912 ipointer->ip_mprev = ip; \
913 ip->i_mnext = (xfs_inode_t *)ipointer; \
914 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
916 XFS_MOUNT_IUNLOCK(mp); \
917 mount_locked = B_FALSE; \
921 /* Remove the marker from the inode list. If the marker was the only item
922 * in the list then there are no remaining inodes and we should zero out
923 * the whole list. If we are the current head of the list then move the head
926 #define IPOINTER_REMOVE(ip, mp) { \
927 ASSERT(ipointer_in == B_TRUE); \
928 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
929 ip = ipointer->ip_mnext; \
930 ip->i_mprev = ipointer->ip_mprev; \
931 ipointer->ip_mprev->i_mnext = ip; \
932 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
936 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
937 mp->m_inodes = NULL; \
943 #define XFS_PREEMPT_MASK 0x7f
945 ASSERT(!(flags & SYNC_BDFLUSH));
949 if (mp->m_flags & XFS_MOUNT_RDONLY)
955 /* Allocate a reference marker */
956 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
958 fflag = XFS_B_ASYNC; /* default is don't wait */
959 if (flags & SYNC_DELWRI)
960 fflag = XFS_B_DELWRI;
961 if (flags & SYNC_WAIT)
962 fflag = 0; /* synchronous overrides all */
964 base_lock_flags = XFS_ILOCK_SHARED;
965 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
967 * We need the I/O lock if we're going to call any of
968 * the flush/inval routines.
970 base_lock_flags |= XFS_IOLOCK_SHARED;
977 mount_locked = B_TRUE;
978 vnode_refed = B_FALSE;
983 ASSERT(ipointer_in == B_FALSE);
984 ASSERT(vnode_refed == B_FALSE);
986 lock_flags = base_lock_flags;
989 * There were no inodes in the list, just break out
997 * We found another sync thread marker - skip it
999 if (ip->i_mount == NULL) {
1004 vp = XFS_ITOV_NULL(ip);
1007 * If the vnode is gone then this is being torn down,
1008 * call reclaim if it is flushed, else let regular flush
1009 * code deal with it later in the loop.
1013 /* Skip ones already in reclaim */
1014 if (ip->i_flags & XFS_IRECLAIM) {
1018 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1020 } else if ((xfs_ipincount(ip) == 0) &&
1021 xfs_iflock_nowait(ip)) {
1022 IPOINTER_INSERT(ip, mp);
1024 xfs_finish_reclaim(ip, 1,
1025 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1027 XFS_MOUNT_ILOCK(mp);
1028 mount_locked = B_TRUE;
1029 IPOINTER_REMOVE(ip, mp);
1031 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1042 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1043 XFS_MOUNT_IUNLOCK(mp);
1044 kmem_free(ipointer, sizeof(xfs_iptr_t));
1049 * Try to lock without sleeping. We're out of order with
1050 * the inode list lock here, so if we fail we need to drop
1051 * the mount lock and try again. If we're called from
1052 * bdflush() here, then don't bother.
1054 * The inode lock here actually coordinates with the
1055 * almost spurious inode lock in xfs_ireclaim() to prevent
1056 * the vnode we handle here without a reference from
1057 * being freed while we reference it. If we lock the inode
1058 * while it's on the mount list here, then the spurious inode
1059 * lock in xfs_ireclaim() after the inode is pulled from
1060 * the mount list will sleep until we release it here.
1061 * This keeps the vnode from being freed while we reference
1064 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
1076 IPOINTER_INSERT(ip, mp);
1077 xfs_ilock(ip, lock_flags);
1079 ASSERT(vp == XFS_ITOV(ip));
1080 ASSERT(ip->i_mount == mp);
1082 vnode_refed = B_TRUE;
1085 /* From here on in the loop we may have a marker record
1086 * in the inode list.
1090 * If we have to flush data or wait for I/O completion
1091 * we need to drop the ilock that we currently hold.
1092 * If we need to drop the lock, insert a marker if we
1093 * have not already done so.
1095 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1096 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1098 IPOINTER_INSERT(ip, mp);
1100 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1102 if (flags & SYNC_CLOSE) {
1103 /* Shutdown case. Flush and invalidate. */
1104 if (XFS_FORCED_SHUTDOWN(mp))
1105 xfs_tosspages(ip, 0, -1,
1108 error = xfs_flushinval_pages(ip,
1110 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
1111 error = xfs_flush_pages(ip, 0,
1112 -1, fflag, FI_NONE);
1116 * When freezing, we need to wait ensure all I/O (including direct
1117 * I/O) is complete to ensure no further data modification can take
1118 * place after this point
1120 if (flags & SYNC_IOWAIT)
1123 xfs_ilock(ip, XFS_ILOCK_SHARED);
1126 if ((flags & SYNC_ATTR) &&
1127 (ip->i_update_core ||
1128 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1130 IPOINTER_INSERT(ip, mp);
1132 if (flags & SYNC_WAIT) {
1134 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
1137 * If we can't acquire the flush lock, then the inode
1138 * is already being flushed so don't bother waiting.
1140 * If we can lock it then do a delwri flush so we can
1141 * combine multiple inode flushes in each disk write.
1143 } else if (xfs_iflock_nowait(ip)) {
1144 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1145 } else if (bypassed) {
1150 if (lock_flags != 0) {
1151 xfs_iunlock(ip, lock_flags);
1156 * If we had to take a reference on the vnode
1157 * above, then wait until after we've unlocked
1158 * the inode to release the reference. This is
1159 * because we can be already holding the inode
1160 * lock when IRELE() calls xfs_inactive().
1162 * Make sure to drop the mount lock before calling
1163 * IRELE() so that we don't trip over ourselves if
1164 * we have to go for the mount lock again in the
1168 IPOINTER_INSERT(ip, mp);
1173 vnode_refed = B_FALSE;
1181 * bail out if the filesystem is corrupted.
1183 if (error == EFSCORRUPTED) {
1184 if (!mount_locked) {
1185 XFS_MOUNT_ILOCK(mp);
1186 IPOINTER_REMOVE(ip, mp);
1188 XFS_MOUNT_IUNLOCK(mp);
1189 ASSERT(ipointer_in == B_FALSE);
1190 kmem_free(ipointer, sizeof(xfs_iptr_t));
1191 return XFS_ERROR(error);
1194 /* Let other threads have a chance at the mount lock
1195 * if we have looped many times without dropping the
1198 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1200 IPOINTER_INSERT(ip, mp);
1204 if (mount_locked == B_FALSE) {
1205 XFS_MOUNT_ILOCK(mp);
1206 mount_locked = B_TRUE;
1207 IPOINTER_REMOVE(ip, mp);
1211 ASSERT(ipointer_in == B_FALSE);
1214 } while (ip != mp->m_inodes);
1216 XFS_MOUNT_IUNLOCK(mp);
1218 ASSERT(ipointer_in == B_FALSE);
1220 kmem_free(ipointer, sizeof(xfs_iptr_t));
1221 return XFS_ERROR(last_error);
1225 * xfs sync routine for internal use
1227 * This routine supports all of the flags defined for the generic vfs_sync
1228 * interface as explained above under xfs_sync.
1239 uint log_flags = XFS_LOG_FORCE;
1241 xfs_buf_log_item_t *bip;
1244 * Sync out the log. This ensures that the log is periodically
1245 * flushed even if there is not enough activity to fill it up.
1247 if (flags & SYNC_WAIT)
1248 log_flags |= XFS_LOG_SYNC;
1250 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1252 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1253 if (flags & SYNC_BDFLUSH)
1254 xfs_finish_reclaim_all(mp, 1);
1256 error = xfs_sync_inodes(mp, flags, bypassed);
1260 * Flushing out dirty data above probably generated more
1261 * log activity, so if this isn't vfs_sync() then flush
1264 if (flags & SYNC_DELWRI) {
1265 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1268 if (flags & SYNC_FSDATA) {
1270 * If this is vfs_sync() then only sync the superblock
1271 * if we can lock it without sleeping and it is not pinned.
1273 if (flags & SYNC_BDFLUSH) {
1274 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1276 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1277 if ((bip != NULL) &&
1278 xfs_buf_item_dirty(bip)) {
1279 if (!(XFS_BUF_ISPINNED(bp))) {
1281 error = xfs_bwrite(mp, bp);
1290 bp = xfs_getsb(mp, 0);
1292 * If the buffer is pinned then push on the log so
1293 * we won't get stuck waiting in the write for
1294 * someone, maybe ourselves, to flush the log.
1295 * Even though we just pushed the log above, we
1296 * did not have the superblock buffer locked at
1297 * that point so it can become pinned in between
1300 if (XFS_BUF_ISPINNED(bp))
1301 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1302 if (flags & SYNC_WAIT)
1303 XFS_BUF_UNASYNC(bp);
1306 error = xfs_bwrite(mp, bp);
1314 * If asked, update the disk superblock with incore counter values if we
1315 * are using non-persistent counters so that they don't get too far out
1316 * of sync if we crash or get a forced shutdown. We don't want to force
1317 * this to disk, just get a transaction into the iclogs....
1319 if (flags & SYNC_SUPER)
1320 xfs_log_sbcount(mp, 0);
1323 * Now check to see if the log needs a "dummy" transaction.
1326 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1331 * Put a dummy transaction in the log to tell
1332 * recovery that all others are OK.
1334 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1335 if ((error = xfs_trans_reserve(tp, 0,
1336 XFS_ICHANGE_LOG_RES(mp),
1338 xfs_trans_cancel(tp, 0);
1343 xfs_ilock(ip, XFS_ILOCK_EXCL);
1345 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1346 xfs_trans_ihold(tp, ip);
1347 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1348 error = xfs_trans_commit(tp, 0);
1349 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1350 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1354 * When shutting down, we need to insure that the AIL is pushed
1355 * to disk or the filesystem can appear corrupt from the PROM.
1357 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1358 XFS_bflush(mp->m_ddev_targp);
1359 if (mp->m_rtdev_targp) {
1360 XFS_bflush(mp->m_rtdev_targp);
1364 return XFS_ERROR(last_error);