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_log_ticket_zone = kmem_zone_init(sizeof(xlog_ticket_t),
74 xfs_bmap_free_item_zone = kmem_zone_init(sizeof(xfs_bmap_free_item_t),
75 "xfs_bmap_free_item");
76 xfs_btree_cur_zone = kmem_zone_init(sizeof(xfs_btree_cur_t),
78 xfs_da_state_zone = kmem_zone_init(sizeof(xfs_da_state_t),
80 xfs_dabuf_zone = kmem_zone_init(sizeof(xfs_dabuf_t), "xfs_dabuf");
81 xfs_ifork_zone = kmem_zone_init(sizeof(xfs_ifork_t), "xfs_ifork");
82 xfs_trans_zone = kmem_zone_init(sizeof(xfs_trans_t), "xfs_trans");
83 xfs_acl_zone_init(xfs_acl_zone, "xfs_acl");
85 xfs_filestream_init();
88 * The size of the zone allocated buf log item is the maximum
89 * size possible under XFS. This wastes a little bit of memory,
90 * but it is much faster.
93 kmem_zone_init((sizeof(xfs_buf_log_item_t) +
94 (((XFS_MAX_BLOCKSIZE / XFS_BLI_CHUNK) /
95 NBWORD) * sizeof(int))),
98 kmem_zone_init((sizeof(xfs_efd_log_item_t) +
99 ((XFS_EFD_MAX_FAST_EXTENTS - 1) *
100 sizeof(xfs_extent_t))),
103 kmem_zone_init((sizeof(xfs_efi_log_item_t) +
104 ((XFS_EFI_MAX_FAST_EXTENTS - 1) *
105 sizeof(xfs_extent_t))),
109 * These zones warrant special memory allocator hints
112 kmem_zone_init_flags(sizeof(xfs_inode_t), "xfs_inode",
113 KM_ZONE_HWALIGN | KM_ZONE_RECLAIM |
114 KM_ZONE_SPREAD, NULL);
116 kmem_zone_init_flags(sizeof(xfs_inode_log_item_t), "xfs_ili",
117 KM_ZONE_SPREAD, NULL);
120 * Allocate global trace buffers.
122 #ifdef XFS_ALLOC_TRACE
123 xfs_alloc_trace_buf = ktrace_alloc(XFS_ALLOC_TRACE_SIZE, KM_SLEEP);
125 #ifdef XFS_BMAP_TRACE
126 xfs_bmap_trace_buf = ktrace_alloc(XFS_BMAP_TRACE_SIZE, KM_SLEEP);
128 #ifdef XFS_BMBT_TRACE
129 xfs_bmbt_trace_buf = ktrace_alloc(XFS_BMBT_TRACE_SIZE, KM_SLEEP);
131 #ifdef XFS_ATTR_TRACE
132 xfs_attr_trace_buf = ktrace_alloc(XFS_ATTR_TRACE_SIZE, KM_SLEEP);
134 #ifdef XFS_DIR2_TRACE
135 xfs_dir2_trace_buf = ktrace_alloc(XFS_DIR2_GTRACE_SIZE, KM_SLEEP);
140 #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
141 xfs_error_test_init();
142 #endif /* DEBUG || INDUCE_IO_ERROR */
145 xfs_sysctl_register();
152 extern kmem_zone_t *xfs_inode_zone;
153 extern kmem_zone_t *xfs_efd_zone;
154 extern kmem_zone_t *xfs_efi_zone;
156 xfs_cleanup_procfs();
157 xfs_sysctl_unregister();
158 xfs_filestream_uninit();
159 xfs_mru_cache_uninit();
160 xfs_acl_zone_destroy(xfs_acl_zone);
162 #ifdef XFS_DIR2_TRACE
163 ktrace_free(xfs_dir2_trace_buf);
165 #ifdef XFS_ATTR_TRACE
166 ktrace_free(xfs_attr_trace_buf);
168 #ifdef XFS_BMBT_TRACE
169 ktrace_free(xfs_bmbt_trace_buf);
171 #ifdef XFS_BMAP_TRACE
172 ktrace_free(xfs_bmap_trace_buf);
174 #ifdef XFS_ALLOC_TRACE
175 ktrace_free(xfs_alloc_trace_buf);
178 kmem_zone_destroy(xfs_bmap_free_item_zone);
179 kmem_zone_destroy(xfs_btree_cur_zone);
180 kmem_zone_destroy(xfs_inode_zone);
181 kmem_zone_destroy(xfs_trans_zone);
182 kmem_zone_destroy(xfs_da_state_zone);
183 kmem_zone_destroy(xfs_dabuf_zone);
184 kmem_zone_destroy(xfs_buf_item_zone);
185 kmem_zone_destroy(xfs_efd_zone);
186 kmem_zone_destroy(xfs_efi_zone);
187 kmem_zone_destroy(xfs_ifork_zone);
188 kmem_zone_destroy(xfs_ili_zone);
189 kmem_zone_destroy(xfs_log_ticket_zone);
195 * This function fills in xfs_mount_t fields based on mount args.
196 * Note: the superblock has _not_ yet been read in.
200 struct xfs_mount_args *ap,
201 struct xfs_mount *mp)
203 /* Values are in BBs */
204 if ((ap->flags & XFSMNT_NOALIGN) != XFSMNT_NOALIGN) {
206 * At this point the superblock has not been read
207 * in, therefore we do not know the block size.
208 * Before the mount call ends we will convert
211 mp->m_dalign = ap->sunit;
212 mp->m_swidth = ap->swidth;
215 if (ap->logbufs != -1 &&
217 (ap->logbufs < XLOG_MIN_ICLOGS ||
218 ap->logbufs > XLOG_MAX_ICLOGS)) {
220 "XFS: invalid logbufs value: %d [not %d-%d]",
221 ap->logbufs, XLOG_MIN_ICLOGS, XLOG_MAX_ICLOGS);
222 return XFS_ERROR(EINVAL);
224 mp->m_logbufs = ap->logbufs;
225 if (ap->logbufsize != -1 &&
226 ap->logbufsize != 0 &&
227 (ap->logbufsize < XLOG_MIN_RECORD_BSIZE ||
228 ap->logbufsize > XLOG_MAX_RECORD_BSIZE ||
229 !is_power_of_2(ap->logbufsize))) {
231 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
233 return XFS_ERROR(EINVAL);
235 mp->m_logbsize = ap->logbufsize;
236 mp->m_fsname_len = strlen(ap->fsname) + 1;
237 mp->m_fsname = kmem_alloc(mp->m_fsname_len, KM_SLEEP);
238 strcpy(mp->m_fsname, ap->fsname);
240 mp->m_rtname = kmem_alloc(strlen(ap->rtname) + 1, KM_SLEEP);
241 strcpy(mp->m_rtname, ap->rtname);
243 if (ap->logname[0]) {
244 mp->m_logname = kmem_alloc(strlen(ap->logname) + 1, KM_SLEEP);
245 strcpy(mp->m_logname, ap->logname);
248 if (ap->flags & XFSMNT_WSYNC)
249 mp->m_flags |= XFS_MOUNT_WSYNC;
251 if (ap->flags & XFSMNT_INO64) {
252 mp->m_flags |= XFS_MOUNT_INO64;
253 mp->m_inoadd = XFS_INO64_OFFSET;
256 if (ap->flags & XFSMNT_RETERR)
257 mp->m_flags |= XFS_MOUNT_RETERR;
258 if (ap->flags & XFSMNT_NOALIGN)
259 mp->m_flags |= XFS_MOUNT_NOALIGN;
260 if (ap->flags & XFSMNT_SWALLOC)
261 mp->m_flags |= XFS_MOUNT_SWALLOC;
262 if (ap->flags & XFSMNT_OSYNCISOSYNC)
263 mp->m_flags |= XFS_MOUNT_OSYNCISOSYNC;
264 if (ap->flags & XFSMNT_32BITINODES)
265 mp->m_flags |= XFS_MOUNT_32BITINODES;
267 if (ap->flags & XFSMNT_IOSIZE) {
268 if (ap->iosizelog > XFS_MAX_IO_LOG ||
269 ap->iosizelog < XFS_MIN_IO_LOG) {
271 "XFS: invalid log iosize: %d [not %d-%d]",
272 ap->iosizelog, XFS_MIN_IO_LOG,
274 return XFS_ERROR(EINVAL);
277 mp->m_flags |= XFS_MOUNT_DFLT_IOSIZE;
278 mp->m_readio_log = mp->m_writeio_log = ap->iosizelog;
281 if (ap->flags & XFSMNT_IKEEP)
282 mp->m_flags |= XFS_MOUNT_IKEEP;
283 if (ap->flags & XFSMNT_DIRSYNC)
284 mp->m_flags |= XFS_MOUNT_DIRSYNC;
285 if (ap->flags & XFSMNT_ATTR2)
286 mp->m_flags |= XFS_MOUNT_ATTR2;
287 if (ap->flags & XFSMNT_NOATTR2)
288 mp->m_flags |= XFS_MOUNT_NOATTR2;
290 if (ap->flags2 & XFSMNT2_COMPAT_IOSIZE)
291 mp->m_flags |= XFS_MOUNT_COMPAT_IOSIZE;
294 * no recovery flag requires a read-only mount
296 if (ap->flags & XFSMNT_NORECOVERY) {
297 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
299 "XFS: tried to mount a FS read-write without recovery!");
300 return XFS_ERROR(EINVAL);
302 mp->m_flags |= XFS_MOUNT_NORECOVERY;
305 if (ap->flags & XFSMNT_NOUUID)
306 mp->m_flags |= XFS_MOUNT_NOUUID;
307 if (ap->flags & XFSMNT_BARRIER)
308 mp->m_flags |= XFS_MOUNT_BARRIER;
310 mp->m_flags &= ~XFS_MOUNT_BARRIER;
312 if (ap->flags2 & XFSMNT2_FILESTREAMS)
313 mp->m_flags |= XFS_MOUNT_FILESTREAMS;
315 if (ap->flags & XFSMNT_DMAPI)
316 mp->m_flags |= XFS_MOUNT_DMAPI;
321 * This function fills in xfs_mount_t fields based on mount args.
322 * Note: the superblock _has_ now been read in.
326 struct xfs_mount_args *ap,
327 struct xfs_mount *mp)
329 int ronly = (mp->m_flags & XFS_MOUNT_RDONLY);
331 /* Fail a mount where the logbuf is smaller then the log stripe */
332 if (xfs_sb_version_haslogv2(&mp->m_sb)) {
333 if ((ap->logbufsize <= 0) &&
334 (mp->m_sb.sb_logsunit > XLOG_BIG_RECORD_BSIZE)) {
335 mp->m_logbsize = mp->m_sb.sb_logsunit;
336 } else if (ap->logbufsize > 0 &&
337 ap->logbufsize < mp->m_sb.sb_logsunit) {
339 "XFS: logbuf size must be greater than or equal to log stripe size");
340 return XFS_ERROR(EINVAL);
343 /* Fail a mount if the logbuf is larger than 32K */
344 if (ap->logbufsize > XLOG_BIG_RECORD_BSIZE) {
346 "XFS: logbuf size for version 1 logs must be 16K or 32K");
347 return XFS_ERROR(EINVAL);
352 * mkfs'ed attr2 will turn on attr2 mount unless explicitly
353 * told by noattr2 to turn it off
355 if (xfs_sb_version_hasattr2(&mp->m_sb) &&
356 !(ap->flags & XFSMNT_NOATTR2))
357 mp->m_flags |= XFS_MOUNT_ATTR2;
360 * prohibit r/w mounts of read-only filesystems
362 if ((mp->m_sb.sb_flags & XFS_SBF_READONLY) && !ronly) {
364 "XFS: cannot mount a read-only filesystem as read-write");
365 return XFS_ERROR(EROFS);
369 * check for shared mount.
371 if (ap->flags & XFSMNT_SHARED) {
372 if (!xfs_sb_version_hasshared(&mp->m_sb))
373 return XFS_ERROR(EINVAL);
376 * For IRIX 6.5, shared mounts must have the shared
377 * version bit set, have the persistent readonly
378 * field set, must be version 0 and can only be mounted
381 if (!ronly || !(mp->m_sb.sb_flags & XFS_SBF_READONLY) ||
382 (mp->m_sb.sb_shared_vn != 0))
383 return XFS_ERROR(EINVAL);
385 mp->m_flags |= XFS_MOUNT_SHARED;
388 * Shared XFS V0 can't deal with DMI. Return EINVAL.
390 if (mp->m_sb.sb_shared_vn == 0 && (ap->flags & XFSMNT_DMAPI))
391 return XFS_ERROR(EINVAL);
394 if (ap->flags & XFSMNT_UQUOTA) {
395 mp->m_qflags |= (XFS_UQUOTA_ACCT | XFS_UQUOTA_ACTIVE);
396 if (ap->flags & XFSMNT_UQUOTAENF)
397 mp->m_qflags |= XFS_UQUOTA_ENFD;
400 if (ap->flags & XFSMNT_GQUOTA) {
401 mp->m_qflags |= (XFS_GQUOTA_ACCT | XFS_GQUOTA_ACTIVE);
402 if (ap->flags & XFSMNT_GQUOTAENF)
403 mp->m_qflags |= XFS_OQUOTA_ENFD;
404 } else if (ap->flags & XFSMNT_PQUOTA) {
405 mp->m_qflags |= (XFS_PQUOTA_ACCT | XFS_PQUOTA_ACTIVE);
406 if (ap->flags & XFSMNT_PQUOTAENF)
407 mp->m_qflags |= XFS_OQUOTA_ENFD;
416 * The file system configurations are:
417 * (1) device (partition) with data and internal log
418 * (2) logical volume with data and log subvolumes.
419 * (3) logical volume with data, log, and realtime subvolumes.
421 * We only have to handle opening the log and realtime volumes here if
422 * they are present. The data subvolume has already been opened by
423 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
427 struct xfs_mount *mp,
428 struct xfs_mount_args *args,
431 struct block_device *ddev, *logdev, *rtdev;
432 int flags = 0, error;
434 ddev = mp->m_super->s_bdev;
435 logdev = rtdev = NULL;
437 error = xfs_dmops_get(mp, args);
440 error = xfs_qmops_get(mp, args);
444 if (args->flags & XFSMNT_QUIET)
445 flags |= XFS_MFSI_QUIET;
448 * Open real time and log devices - order is important.
450 if (args->logname[0]) {
451 error = xfs_blkdev_get(mp, args->logname, &logdev);
455 if (args->rtname[0]) {
456 error = xfs_blkdev_get(mp, args->rtname, &rtdev);
458 xfs_blkdev_put(logdev);
462 if (rtdev == ddev || rtdev == logdev) {
464 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
465 xfs_blkdev_put(logdev);
466 xfs_blkdev_put(rtdev);
472 * Setup xfs_mount buffer target pointers
475 mp->m_ddev_targp = xfs_alloc_buftarg(ddev, 0);
476 if (!mp->m_ddev_targp) {
477 xfs_blkdev_put(logdev);
478 xfs_blkdev_put(rtdev);
482 mp->m_rtdev_targp = xfs_alloc_buftarg(rtdev, 1);
483 if (!mp->m_rtdev_targp) {
484 xfs_blkdev_put(logdev);
485 xfs_blkdev_put(rtdev);
489 mp->m_logdev_targp = (logdev && logdev != ddev) ?
490 xfs_alloc_buftarg(logdev, 1) : mp->m_ddev_targp;
491 if (!mp->m_logdev_targp) {
492 xfs_blkdev_put(logdev);
493 xfs_blkdev_put(rtdev);
498 * Setup flags based on mount(2) options and then the superblock
500 error = xfs_start_flags(args, mp);
503 error = xfs_readsb(mp, flags);
506 error = xfs_finish_flags(args, mp);
511 * Setup xfs_mount buffer target pointers based on superblock
513 error = xfs_setsize_buftarg(mp->m_ddev_targp, mp->m_sb.sb_blocksize,
514 mp->m_sb.sb_sectsize);
515 if (!error && logdev && logdev != ddev) {
516 unsigned int log_sector_size = BBSIZE;
518 if (xfs_sb_version_hassector(&mp->m_sb))
519 log_sector_size = mp->m_sb.sb_logsectsize;
520 error = xfs_setsize_buftarg(mp->m_logdev_targp,
521 mp->m_sb.sb_blocksize,
525 error = xfs_setsize_buftarg(mp->m_rtdev_targp,
526 mp->m_sb.sb_blocksize,
527 mp->m_sb.sb_sectsize);
531 if (mp->m_flags & XFS_MOUNT_BARRIER)
532 xfs_mountfs_check_barriers(mp);
534 if ((error = xfs_filestream_mount(mp)))
537 error = xfs_mountfs(mp, flags);
541 XFS_SEND_MOUNT(mp, DM_RIGHT_NULL, args->mtpt, args->fsname);
549 xfs_binval(mp->m_ddev_targp);
550 if (logdev && logdev != ddev)
551 xfs_binval(mp->m_logdev_targp);
553 xfs_binval(mp->m_rtdev_targp);
555 xfs_unmountfs_close(mp, credp);
569 int unmount_event_wanted = 0;
570 int unmount_event_flags = 0;
571 int xfs_unmountfs_needed = 0;
578 if (mp->m_flags & XFS_MOUNT_DMAPI) {
579 error = XFS_SEND_PREUNMOUNT(mp,
580 rip, DM_RIGHT_NULL, rip, DM_RIGHT_NULL,
582 (mp->m_dmevmask & (1<<DM_EVENT_PREUNMOUNT))?
583 0:DM_FLAGS_UNWANTED);
585 return XFS_ERROR(error);
586 unmount_event_wanted = 1;
587 unmount_event_flags = (mp->m_dmevmask & (1<<DM_EVENT_UNMOUNT))?
588 0 : DM_FLAGS_UNWANTED;
593 * Blow away any referenced inode in the filestreams cache.
594 * This can and will cause log traffic as inodes go inactive
597 xfs_filestream_unmount(mp);
599 XFS_bflush(mp->m_ddev_targp);
600 error = xfs_unmount_flush(mp, 0);
604 ASSERT(vn_count(rvp) == 1);
607 * Drop the reference count
612 * If we're forcing a shutdown, typically because of a media error,
613 * we want to make sure we invalidate dirty pages that belong to
614 * referenced vnodes as well.
616 if (XFS_FORCED_SHUTDOWN(mp)) {
617 error = xfs_sync(mp, SYNC_WAIT | SYNC_CLOSE);
618 ASSERT(error != EFSCORRUPTED);
620 xfs_unmountfs_needed = 1;
623 /* Send DMAPI event, if required.
624 * Then do xfs_unmountfs() if needed.
625 * Then return error (or zero).
627 if (unmount_event_wanted) {
628 /* Note: mp structure must still exist for
629 * XFS_SEND_UNMOUNT() call.
631 XFS_SEND_UNMOUNT(mp, error == 0 ? rip : NULL,
632 DM_RIGHT_NULL, 0, error, unmount_event_flags);
634 if (xfs_unmountfs_needed) {
636 * Call common unmount function to flush to disk
637 * and free the super block buffer & mount structures.
639 xfs_unmountfs(mp, credp);
645 return XFS_ERROR(error);
652 int count = 0, pincount;
654 xfs_flush_buftarg(mp->m_ddev_targp, 0);
655 xfs_finish_reclaim_all(mp, 0);
657 /* This loop must run at least twice.
658 * The first instance of the loop will flush
659 * most meta data but that will generate more
660 * meta data (typically directory updates).
661 * Which then must be flushed and logged before
662 * we can write the unmount record.
665 xfs_syncsub(mp, SYNC_INODE_QUIESCE, NULL);
666 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
675 * Second stage of a quiesce. The data is already synced, now we have to take
676 * care of the metadata. New transactions are already blocked, so we need to
677 * wait for any remaining transactions to drain out before proceding.
685 /* wait for all modifications to complete */
686 while (atomic_read(&mp->m_active_trans) > 0)
689 /* flush inodes and push all remaining buffers out to disk */
692 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
694 /* Push the superblock and write an unmount record */
695 error = xfs_log_sbcount(mp, 1);
697 xfs_fs_cmn_err(CE_WARN, mp,
698 "xfs_attr_quiesce: failed to log sb changes. "
699 "Frozen image may not be consistent.");
700 xfs_log_unmount_write(mp);
701 xfs_unmountfs_writesb(mp);
705 * xfs_unmount_flush implements a set of flush operation on special
706 * inodes, which are needed as a separate set of operations so that
707 * they can be called as part of relocation process.
711 xfs_mount_t *mp, /* Mount structure we are getting
713 int relocation) /* Called from vfs relocation. */
715 xfs_inode_t *rip = mp->m_rootip;
717 xfs_inode_t *rsumip = NULL;
718 bhv_vnode_t *rvp = XFS_ITOV(rip);
721 xfs_ilock(rip, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
725 * Flush out the real time inodes.
727 if ((rbmip = mp->m_rbmip) != NULL) {
728 xfs_ilock(rbmip, XFS_ILOCK_EXCL);
730 error = xfs_iflush(rbmip, XFS_IFLUSH_SYNC);
731 xfs_iunlock(rbmip, XFS_ILOCK_EXCL);
733 if (error == EFSCORRUPTED)
736 ASSERT(vn_count(XFS_ITOV(rbmip)) == 1);
738 rsumip = mp->m_rsumip;
739 xfs_ilock(rsumip, XFS_ILOCK_EXCL);
741 error = xfs_iflush(rsumip, XFS_IFLUSH_SYNC);
742 xfs_iunlock(rsumip, XFS_ILOCK_EXCL);
744 if (error == EFSCORRUPTED)
747 ASSERT(vn_count(XFS_ITOV(rsumip)) == 1);
751 * Synchronously flush root inode to disk
753 error = xfs_iflush(rip, XFS_IFLUSH_SYNC);
754 if (error == EFSCORRUPTED)
757 if (vn_count(rvp) != 1 && !relocation) {
758 xfs_iunlock(rip, XFS_ILOCK_EXCL);
759 return XFS_ERROR(EBUSY);
763 * Release dquot that rootinode, rbmino and rsumino might be holding,
764 * flush and purge the quota inodes.
766 error = XFS_QM_UNMOUNT(mp);
767 if (error == EFSCORRUPTED)
775 xfs_iunlock(rip, XFS_ILOCK_EXCL);
782 xfs_iunlock(rip, XFS_ILOCK_EXCL);
784 return XFS_ERROR(EFSCORRUPTED);
788 * xfs_sync flushes any pending I/O to file system vfsp.
790 * This routine is called by vfs_sync() to make sure that things make it
791 * out to disk eventually, on sync() system calls to flush out everything,
792 * and when the file system is unmounted. For the vfs_sync() case, all
793 * we really need to do is sync out the log to make all of our meta-data
794 * updates permanent (except for timestamps). For calls from pflushd(),
795 * dirty pages are kept moving by calling pdflush() on the inodes
796 * containing them. We also flush the inodes that we can lock without
797 * sleeping and the superblock if we can lock it without sleeping from
798 * vfs_sync() so that items at the tail of the log are always moving out.
801 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
802 * to sleep if we can help it. All we really need
803 * to do is ensure that the log is synced at least
804 * periodically. We also push the inodes and
805 * superblock if we can lock them without sleeping
806 * and they are not pinned.
807 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
808 * set, then we really want to lock each inode and flush
810 * SYNC_WAIT - All the flushes that take place in this call should
812 * SYNC_DELWRI - This tells us to push dirty pages associated with
813 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
814 * determine if they should be flushed sync, async, or
816 * SYNC_CLOSE - This flag is passed when the system is being
817 * unmounted. We should sync and invalidate everything.
818 * SYNC_FSDATA - This indicates that the caller would like to make
819 * sure the superblock is safe on disk. We can ensure
820 * this by simply making sure the log gets flushed
821 * if SYNC_BDFLUSH is set, and by actually writing it
823 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
824 * before we return (including direct I/O). Forms the drain
825 * side of the write barrier needed to safely quiesce the
837 * Get the Quota Manager to flush the dquots.
839 * If XFS quota support is not enabled or this filesystem
840 * instance does not use quotas XFS_QM_DQSYNC will always
843 error = XFS_QM_DQSYNC(mp, flags);
846 * If we got an IO error, we will be shutting down.
847 * So, there's nothing more for us to do here.
849 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
850 if (XFS_FORCED_SHUTDOWN(mp))
851 return XFS_ERROR(error);
854 if (flags & SYNC_IOWAIT)
855 xfs_filestream_flush(mp);
857 return xfs_syncsub(mp, flags, NULL);
861 * xfs sync routine for internal use
863 * This routine supports all of the flags defined for the generic vfs_sync
864 * interface as explained above under xfs_sync.
873 xfs_inode_t *ip = NULL;
874 bhv_vnode_t *vp = NULL;
879 uint base_lock_flags;
880 boolean_t mount_locked;
881 boolean_t vnode_refed;
883 xfs_iptr_t *ipointer;
885 boolean_t ipointer_in = B_FALSE;
887 #define IPOINTER_SET ipointer_in = B_TRUE
888 #define IPOINTER_CLR ipointer_in = B_FALSE
895 /* Insert a marker record into the inode list after inode ip. The list
896 * must be locked when this is called. After the call the list will no
899 #define IPOINTER_INSERT(ip, mp) { \
900 ASSERT(ipointer_in == B_FALSE); \
901 ipointer->ip_mnext = ip->i_mnext; \
902 ipointer->ip_mprev = ip; \
903 ip->i_mnext = (xfs_inode_t *)ipointer; \
904 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
906 XFS_MOUNT_IUNLOCK(mp); \
907 mount_locked = B_FALSE; \
911 /* Remove the marker from the inode list. If the marker was the only item
912 * in the list then there are no remaining inodes and we should zero out
913 * the whole list. If we are the current head of the list then move the head
916 #define IPOINTER_REMOVE(ip, mp) { \
917 ASSERT(ipointer_in == B_TRUE); \
918 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
919 ip = ipointer->ip_mnext; \
920 ip->i_mprev = ipointer->ip_mprev; \
921 ipointer->ip_mprev->i_mnext = ip; \
922 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
926 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
927 mp->m_inodes = NULL; \
933 #define XFS_PREEMPT_MASK 0x7f
935 ASSERT(!(flags & SYNC_BDFLUSH));
939 if (mp->m_flags & XFS_MOUNT_RDONLY)
945 /* Allocate a reference marker */
946 ipointer = (xfs_iptr_t *)kmem_zalloc(sizeof(xfs_iptr_t), KM_SLEEP);
948 fflag = XFS_B_ASYNC; /* default is don't wait */
949 if (flags & SYNC_DELWRI)
950 fflag = XFS_B_DELWRI;
951 if (flags & SYNC_WAIT)
952 fflag = 0; /* synchronous overrides all */
954 base_lock_flags = XFS_ILOCK_SHARED;
955 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
957 * We need the I/O lock if we're going to call any of
958 * the flush/inval routines.
960 base_lock_flags |= XFS_IOLOCK_SHARED;
967 mount_locked = B_TRUE;
968 vnode_refed = B_FALSE;
973 ASSERT(ipointer_in == B_FALSE);
974 ASSERT(vnode_refed == B_FALSE);
976 lock_flags = base_lock_flags;
979 * There were no inodes in the list, just break out
987 * We found another sync thread marker - skip it
989 if (ip->i_mount == NULL) {
994 vp = XFS_ITOV_NULL(ip);
997 * If the vnode is gone then this is being torn down,
998 * call reclaim if it is flushed, else let regular flush
999 * code deal with it later in the loop.
1003 /* Skip ones already in reclaim */
1004 if (ip->i_flags & XFS_IRECLAIM) {
1008 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0) {
1010 } else if ((xfs_ipincount(ip) == 0) &&
1011 xfs_iflock_nowait(ip)) {
1012 IPOINTER_INSERT(ip, mp);
1014 xfs_finish_reclaim(ip, 1,
1015 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
1017 XFS_MOUNT_ILOCK(mp);
1018 mount_locked = B_TRUE;
1019 IPOINTER_REMOVE(ip, mp);
1021 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1032 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
1033 XFS_MOUNT_IUNLOCK(mp);
1034 kmem_free(ipointer);
1039 * Try to lock without sleeping. We're out of order with
1040 * the inode list lock here, so if we fail we need to drop
1041 * the mount lock and try again. If we're called from
1042 * bdflush() here, then don't bother.
1044 * The inode lock here actually coordinates with the
1045 * almost spurious inode lock in xfs_ireclaim() to prevent
1046 * the vnode we handle here without a reference from
1047 * being freed while we reference it. If we lock the inode
1048 * while it's on the mount list here, then the spurious inode
1049 * lock in xfs_ireclaim() after the inode is pulled from
1050 * the mount list will sleep until we release it here.
1051 * This keeps the vnode from being freed while we reference
1054 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
1066 IPOINTER_INSERT(ip, mp);
1067 xfs_ilock(ip, lock_flags);
1069 ASSERT(vp == XFS_ITOV(ip));
1070 ASSERT(ip->i_mount == mp);
1072 vnode_refed = B_TRUE;
1075 /* From here on in the loop we may have a marker record
1076 * in the inode list.
1080 * If we have to flush data or wait for I/O completion
1081 * we need to drop the ilock that we currently hold.
1082 * If we need to drop the lock, insert a marker if we
1083 * have not already done so.
1085 if ((flags & (SYNC_CLOSE|SYNC_IOWAIT)) ||
1086 ((flags & SYNC_DELWRI) && VN_DIRTY(vp))) {
1088 IPOINTER_INSERT(ip, mp);
1090 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1092 if (flags & SYNC_CLOSE) {
1093 /* Shutdown case. Flush and invalidate. */
1094 if (XFS_FORCED_SHUTDOWN(mp))
1095 xfs_tosspages(ip, 0, -1,
1098 error = xfs_flushinval_pages(ip,
1100 } else if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
1101 error = xfs_flush_pages(ip, 0,
1102 -1, fflag, FI_NONE);
1106 * When freezing, we need to wait ensure all I/O (including direct
1107 * I/O) is complete to ensure no further data modification can take
1108 * place after this point
1110 if (flags & SYNC_IOWAIT)
1113 xfs_ilock(ip, XFS_ILOCK_SHARED);
1116 if ((flags & SYNC_ATTR) &&
1117 (ip->i_update_core ||
1118 (ip->i_itemp && ip->i_itemp->ili_format.ilf_fields))) {
1120 IPOINTER_INSERT(ip, mp);
1122 if (flags & SYNC_WAIT) {
1124 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
1127 * If we can't acquire the flush lock, then the inode
1128 * is already being flushed so don't bother waiting.
1130 * If we can lock it then do a delwri flush so we can
1131 * combine multiple inode flushes in each disk write.
1133 } else if (xfs_iflock_nowait(ip)) {
1134 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
1135 } else if (bypassed) {
1140 if (lock_flags != 0) {
1141 xfs_iunlock(ip, lock_flags);
1146 * If we had to take a reference on the vnode
1147 * above, then wait until after we've unlocked
1148 * the inode to release the reference. This is
1149 * because we can be already holding the inode
1150 * lock when IRELE() calls xfs_inactive().
1152 * Make sure to drop the mount lock before calling
1153 * IRELE() so that we don't trip over ourselves if
1154 * we have to go for the mount lock again in the
1158 IPOINTER_INSERT(ip, mp);
1163 vnode_refed = B_FALSE;
1171 * bail out if the filesystem is corrupted.
1173 if (error == EFSCORRUPTED) {
1174 if (!mount_locked) {
1175 XFS_MOUNT_ILOCK(mp);
1176 IPOINTER_REMOVE(ip, mp);
1178 XFS_MOUNT_IUNLOCK(mp);
1179 ASSERT(ipointer_in == B_FALSE);
1180 kmem_free(ipointer);
1181 return XFS_ERROR(error);
1184 /* Let other threads have a chance at the mount lock
1185 * if we have looped many times without dropping the
1188 if ((++preempt & XFS_PREEMPT_MASK) == 0) {
1190 IPOINTER_INSERT(ip, mp);
1194 if (mount_locked == B_FALSE) {
1195 XFS_MOUNT_ILOCK(mp);
1196 mount_locked = B_TRUE;
1197 IPOINTER_REMOVE(ip, mp);
1201 ASSERT(ipointer_in == B_FALSE);
1204 } while (ip != mp->m_inodes);
1206 XFS_MOUNT_IUNLOCK(mp);
1208 ASSERT(ipointer_in == B_FALSE);
1210 kmem_free(ipointer);
1211 return XFS_ERROR(last_error);
1215 * xfs sync routine for internal use
1217 * This routine supports all of the flags defined for the generic vfs_sync
1218 * interface as explained above under xfs_sync.
1229 uint log_flags = XFS_LOG_FORCE;
1231 xfs_buf_log_item_t *bip;
1234 * Sync out the log. This ensures that the log is periodically
1235 * flushed even if there is not enough activity to fill it up.
1237 if (flags & SYNC_WAIT)
1238 log_flags |= XFS_LOG_SYNC;
1240 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1242 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
1243 if (flags & SYNC_BDFLUSH)
1244 xfs_finish_reclaim_all(mp, 1);
1246 error = xfs_sync_inodes(mp, flags, bypassed);
1250 * Flushing out dirty data above probably generated more
1251 * log activity, so if this isn't vfs_sync() then flush
1254 if (flags & SYNC_DELWRI) {
1255 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1258 if (flags & SYNC_FSDATA) {
1260 * If this is vfs_sync() then only sync the superblock
1261 * if we can lock it without sleeping and it is not pinned.
1263 if (flags & SYNC_BDFLUSH) {
1264 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
1266 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
1267 if ((bip != NULL) &&
1268 xfs_buf_item_dirty(bip)) {
1269 if (!(XFS_BUF_ISPINNED(bp))) {
1271 error = xfs_bwrite(mp, bp);
1280 bp = xfs_getsb(mp, 0);
1282 * If the buffer is pinned then push on the log so
1283 * we won't get stuck waiting in the write for
1284 * someone, maybe ourselves, to flush the log.
1285 * Even though we just pushed the log above, we
1286 * did not have the superblock buffer locked at
1287 * that point so it can become pinned in between
1290 if (XFS_BUF_ISPINNED(bp))
1291 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1292 if (flags & SYNC_WAIT)
1293 XFS_BUF_UNASYNC(bp);
1296 error = xfs_bwrite(mp, bp);
1304 * Now check to see if the log needs a "dummy" transaction.
1306 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
1311 * Put a dummy transaction in the log to tell
1312 * recovery that all others are OK.
1314 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
1315 if ((error = xfs_trans_reserve(tp, 0,
1316 XFS_ICHANGE_LOG_RES(mp),
1318 xfs_trans_cancel(tp, 0);
1323 xfs_ilock(ip, XFS_ILOCK_EXCL);
1325 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1326 xfs_trans_ihold(tp, ip);
1327 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1328 error = xfs_trans_commit(tp, 0);
1329 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1330 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
1334 * When shutting down, we need to insure that the AIL is pushed
1335 * to disk or the filesystem can appear corrupt from the PROM.
1337 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
1338 XFS_bflush(mp->m_ddev_targp);
1339 if (mp->m_rtdev_targp) {
1340 XFS_bflush(mp->m_rtdev_targp);
1344 return XFS_ERROR(last_error);