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_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"
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"
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
);
117 kmem_zone_init_flags(sizeof(xfs_icluster_t
), "xfs_icluster",
118 KM_ZONE_SPREAD
, NULL
);
121 * Allocate global trace buffers.
123 #ifdef XFS_ALLOC_TRACE
124 xfs_alloc_trace_buf
= ktrace_alloc(XFS_ALLOC_TRACE_SIZE
, KM_SLEEP
);
126 #ifdef XFS_BMAP_TRACE
127 xfs_bmap_trace_buf
= ktrace_alloc(XFS_BMAP_TRACE_SIZE
, KM_SLEEP
);
129 #ifdef XFS_BMBT_TRACE
130 xfs_bmbt_trace_buf
= ktrace_alloc(XFS_BMBT_TRACE_SIZE
, KM_SLEEP
);
132 #ifdef XFS_ATTR_TRACE
133 xfs_attr_trace_buf
= ktrace_alloc(XFS_ATTR_TRACE_SIZE
, KM_SLEEP
);
135 #ifdef XFS_DIR2_TRACE
136 xfs_dir2_trace_buf
= ktrace_alloc(XFS_DIR2_GTRACE_SIZE
, KM_SLEEP
);
141 #if (defined(DEBUG) || defined(INDUCE_IO_ERROR))
142 xfs_error_test_init();
143 #endif /* DEBUG || INDUCE_IO_ERROR */
146 xfs_sysctl_register();
153 extern kmem_zone_t
*xfs_inode_zone
;
154 extern kmem_zone_t
*xfs_efd_zone
;
155 extern kmem_zone_t
*xfs_efi_zone
;
156 extern kmem_zone_t
*xfs_icluster_zone
;
158 xfs_cleanup_procfs();
159 xfs_sysctl_unregister();
160 xfs_refcache_destroy();
161 xfs_filestream_uninit();
162 xfs_mru_cache_uninit();
163 xfs_acl_zone_destroy(xfs_acl_zone
);
165 #ifdef XFS_DIR2_TRACE
166 ktrace_free(xfs_dir2_trace_buf
);
168 #ifdef XFS_ATTR_TRACE
169 ktrace_free(xfs_attr_trace_buf
);
171 #ifdef XFS_BMBT_TRACE
172 ktrace_free(xfs_bmbt_trace_buf
);
174 #ifdef XFS_BMAP_TRACE
175 ktrace_free(xfs_bmap_trace_buf
);
177 #ifdef XFS_ALLOC_TRACE
178 ktrace_free(xfs_alloc_trace_buf
);
181 kmem_zone_destroy(xfs_bmap_free_item_zone
);
182 kmem_zone_destroy(xfs_btree_cur_zone
);
183 kmem_zone_destroy(xfs_inode_zone
);
184 kmem_zone_destroy(xfs_trans_zone
);
185 kmem_zone_destroy(xfs_da_state_zone
);
186 kmem_zone_destroy(xfs_dabuf_zone
);
187 kmem_zone_destroy(xfs_buf_item_zone
);
188 kmem_zone_destroy(xfs_efd_zone
);
189 kmem_zone_destroy(xfs_efi_zone
);
190 kmem_zone_destroy(xfs_ifork_zone
);
191 kmem_zone_destroy(xfs_ili_zone
);
192 kmem_zone_destroy(xfs_icluster_zone
);
198 * This function fills in xfs_mount_t fields based on mount args.
199 * Note: the superblock has _not_ yet been read in.
203 struct xfs_mount_args
*ap
,
204 struct xfs_mount
*mp
)
206 /* Values are in BBs */
207 if ((ap
->flags
& XFSMNT_NOALIGN
) != XFSMNT_NOALIGN
) {
209 * At this point the superblock has not been read
210 * in, therefore we do not know the block size.
211 * Before the mount call ends we will convert
214 mp
->m_dalign
= ap
->sunit
;
215 mp
->m_swidth
= ap
->swidth
;
218 if (ap
->logbufs
!= -1 &&
220 (ap
->logbufs
< XLOG_MIN_ICLOGS
||
221 ap
->logbufs
> XLOG_MAX_ICLOGS
)) {
223 "XFS: invalid logbufs value: %d [not %d-%d]",
224 ap
->logbufs
, XLOG_MIN_ICLOGS
, XLOG_MAX_ICLOGS
);
225 return XFS_ERROR(EINVAL
);
227 mp
->m_logbufs
= ap
->logbufs
;
228 if (ap
->logbufsize
!= -1 &&
229 ap
->logbufsize
!= 0 &&
230 (ap
->logbufsize
< XLOG_MIN_RECORD_BSIZE
||
231 ap
->logbufsize
> XLOG_MAX_RECORD_BSIZE
||
232 !is_power_of_2(ap
->logbufsize
))) {
234 "XFS: invalid logbufsize: %d [not 16k,32k,64k,128k or 256k]",
236 return XFS_ERROR(EINVAL
);
238 mp
->m_logbsize
= ap
->logbufsize
;
239 mp
->m_fsname_len
= strlen(ap
->fsname
) + 1;
240 mp
->m_fsname
= kmem_alloc(mp
->m_fsname_len
, KM_SLEEP
);
241 strcpy(mp
->m_fsname
, ap
->fsname
);
243 mp
->m_rtname
= kmem_alloc(strlen(ap
->rtname
) + 1, KM_SLEEP
);
244 strcpy(mp
->m_rtname
, ap
->rtname
);
246 if (ap
->logname
[0]) {
247 mp
->m_logname
= kmem_alloc(strlen(ap
->logname
) + 1, KM_SLEEP
);
248 strcpy(mp
->m_logname
, ap
->logname
);
251 if (ap
->flags
& XFSMNT_WSYNC
)
252 mp
->m_flags
|= XFS_MOUNT_WSYNC
;
254 if (ap
->flags
& XFSMNT_INO64
) {
255 mp
->m_flags
|= XFS_MOUNT_INO64
;
256 mp
->m_inoadd
= XFS_INO64_OFFSET
;
259 if (ap
->flags
& XFSMNT_RETERR
)
260 mp
->m_flags
|= XFS_MOUNT_RETERR
;
261 if (ap
->flags
& XFSMNT_NOALIGN
)
262 mp
->m_flags
|= XFS_MOUNT_NOALIGN
;
263 if (ap
->flags
& XFSMNT_SWALLOC
)
264 mp
->m_flags
|= XFS_MOUNT_SWALLOC
;
265 if (ap
->flags
& XFSMNT_OSYNCISOSYNC
)
266 mp
->m_flags
|= XFS_MOUNT_OSYNCISOSYNC
;
267 if (ap
->flags
& XFSMNT_32BITINODES
)
268 mp
->m_flags
|= XFS_MOUNT_32BITINODES
;
270 if (ap
->flags
& XFSMNT_IOSIZE
) {
271 if (ap
->iosizelog
> XFS_MAX_IO_LOG
||
272 ap
->iosizelog
< XFS_MIN_IO_LOG
) {
274 "XFS: invalid log iosize: %d [not %d-%d]",
275 ap
->iosizelog
, XFS_MIN_IO_LOG
,
277 return XFS_ERROR(EINVAL
);
280 mp
->m_flags
|= XFS_MOUNT_DFLT_IOSIZE
;
281 mp
->m_readio_log
= mp
->m_writeio_log
= ap
->iosizelog
;
284 if (ap
->flags
& XFSMNT_IKEEP
)
285 mp
->m_flags
|= XFS_MOUNT_IKEEP
;
286 if (ap
->flags
& XFSMNT_DIRSYNC
)
287 mp
->m_flags
|= XFS_MOUNT_DIRSYNC
;
288 if (ap
->flags
& XFSMNT_ATTR2
)
289 mp
->m_flags
|= XFS_MOUNT_ATTR2
;
291 if (ap
->flags2
& XFSMNT2_COMPAT_IOSIZE
)
292 mp
->m_flags
|= XFS_MOUNT_COMPAT_IOSIZE
;
295 * no recovery flag requires a read-only mount
297 if (ap
->flags
& XFSMNT_NORECOVERY
) {
298 if (!(mp
->m_flags
& XFS_MOUNT_RDONLY
)) {
300 "XFS: tried to mount a FS read-write without recovery!");
301 return XFS_ERROR(EINVAL
);
303 mp
->m_flags
|= XFS_MOUNT_NORECOVERY
;
306 if (ap
->flags
& XFSMNT_NOUUID
)
307 mp
->m_flags
|= XFS_MOUNT_NOUUID
;
308 if (ap
->flags
& XFSMNT_BARRIER
)
309 mp
->m_flags
|= XFS_MOUNT_BARRIER
;
311 mp
->m_flags
&= ~XFS_MOUNT_BARRIER
;
313 if (ap
->flags2
& XFSMNT2_FILESTREAMS
)
314 mp
->m_flags
|= XFS_MOUNT_FILESTREAMS
;
316 if (ap
->flags
& XFSMNT_DMAPI
)
317 mp
->m_flags
|= XFS_MOUNT_DMAPI
;
322 * This function fills in xfs_mount_t fields based on mount args.
323 * Note: the superblock _has_ now been read in.
327 struct xfs_mount_args
*ap
,
328 struct xfs_mount
*mp
)
330 int ronly
= (mp
->m_flags
& XFS_MOUNT_RDONLY
);
332 /* Fail a mount where the logbuf is smaller then the log stripe */
333 if (XFS_SB_VERSION_HASLOGV2(&mp
->m_sb
)) {
334 if ((ap
->logbufsize
<= 0) &&
335 (mp
->m_sb
.sb_logsunit
> XLOG_BIG_RECORD_BSIZE
)) {
336 mp
->m_logbsize
= mp
->m_sb
.sb_logsunit
;
337 } else if (ap
->logbufsize
> 0 &&
338 ap
->logbufsize
< mp
->m_sb
.sb_logsunit
) {
340 "XFS: logbuf size must be greater than or equal to log stripe size");
341 return XFS_ERROR(EINVAL
);
344 /* Fail a mount if the logbuf is larger than 32K */
345 if (ap
->logbufsize
> XLOG_BIG_RECORD_BSIZE
) {
347 "XFS: logbuf size for version 1 logs must be 16K or 32K");
348 return XFS_ERROR(EINVAL
);
352 if (XFS_SB_VERSION_HASATTR2(&mp
->m_sb
)) {
353 mp
->m_flags
|= XFS_MOUNT_ATTR2
;
357 * prohibit r/w mounts of read-only filesystems
359 if ((mp
->m_sb
.sb_flags
& XFS_SBF_READONLY
) && !ronly
) {
361 "XFS: cannot mount a read-only filesystem as read-write");
362 return XFS_ERROR(EROFS
);
366 * check for shared mount.
368 if (ap
->flags
& XFSMNT_SHARED
) {
369 if (!XFS_SB_VERSION_HASSHARED(&mp
->m_sb
))
370 return XFS_ERROR(EINVAL
);
373 * For IRIX 6.5, shared mounts must have the shared
374 * version bit set, have the persistent readonly
375 * field set, must be version 0 and can only be mounted
378 if (!ronly
|| !(mp
->m_sb
.sb_flags
& XFS_SBF_READONLY
) ||
379 (mp
->m_sb
.sb_shared_vn
!= 0))
380 return XFS_ERROR(EINVAL
);
382 mp
->m_flags
|= XFS_MOUNT_SHARED
;
385 * Shared XFS V0 can't deal with DMI. Return EINVAL.
387 if (mp
->m_sb
.sb_shared_vn
== 0 && (ap
->flags
& XFSMNT_DMAPI
))
388 return XFS_ERROR(EINVAL
);
391 if (ap
->flags
& XFSMNT_UQUOTA
) {
392 mp
->m_qflags
|= (XFS_UQUOTA_ACCT
| XFS_UQUOTA_ACTIVE
);
393 if (ap
->flags
& XFSMNT_UQUOTAENF
)
394 mp
->m_qflags
|= XFS_UQUOTA_ENFD
;
397 if (ap
->flags
& XFSMNT_GQUOTA
) {
398 mp
->m_qflags
|= (XFS_GQUOTA_ACCT
| XFS_GQUOTA_ACTIVE
);
399 if (ap
->flags
& XFSMNT_GQUOTAENF
)
400 mp
->m_qflags
|= XFS_OQUOTA_ENFD
;
401 } else if (ap
->flags
& XFSMNT_PQUOTA
) {
402 mp
->m_qflags
|= (XFS_PQUOTA_ACCT
| XFS_PQUOTA_ACTIVE
);
403 if (ap
->flags
& XFSMNT_PQUOTAENF
)
404 mp
->m_qflags
|= XFS_OQUOTA_ENFD
;
413 * The file system configurations are:
414 * (1) device (partition) with data and internal log
415 * (2) logical volume with data and log subvolumes.
416 * (3) logical volume with data, log, and realtime subvolumes.
418 * We only have to handle opening the log and realtime volumes here if
419 * they are present. The data subvolume has already been opened by
420 * get_sb_bdev() and is stored in vfsp->vfs_super->s_bdev.
424 struct xfs_mount
*mp
,
425 struct xfs_mount_args
*args
,
428 struct block_device
*ddev
, *logdev
, *rtdev
;
429 int flags
= 0, error
;
431 ddev
= mp
->m_super
->s_bdev
;
432 logdev
= rtdev
= NULL
;
434 error
= xfs_dmops_get(mp
, args
);
437 error
= xfs_qmops_get(mp
, args
);
441 if (args
->flags
& XFSMNT_QUIET
)
442 flags
|= XFS_MFSI_QUIET
;
445 * Open real time and log devices - order is important.
447 if (args
->logname
[0]) {
448 error
= xfs_blkdev_get(mp
, args
->logname
, &logdev
);
452 if (args
->rtname
[0]) {
453 error
= xfs_blkdev_get(mp
, args
->rtname
, &rtdev
);
455 xfs_blkdev_put(logdev
);
459 if (rtdev
== ddev
|| rtdev
== logdev
) {
461 "XFS: Cannot mount filesystem with identical rtdev and ddev/logdev.");
462 xfs_blkdev_put(logdev
);
463 xfs_blkdev_put(rtdev
);
469 * Setup xfs_mount buffer target pointers
472 mp
->m_ddev_targp
= xfs_alloc_buftarg(ddev
, 0);
473 if (!mp
->m_ddev_targp
) {
474 xfs_blkdev_put(logdev
);
475 xfs_blkdev_put(rtdev
);
479 mp
->m_rtdev_targp
= xfs_alloc_buftarg(rtdev
, 1);
480 if (!mp
->m_rtdev_targp
) {
481 xfs_blkdev_put(logdev
);
482 xfs_blkdev_put(rtdev
);
486 mp
->m_logdev_targp
= (logdev
&& logdev
!= ddev
) ?
487 xfs_alloc_buftarg(logdev
, 1) : mp
->m_ddev_targp
;
488 if (!mp
->m_logdev_targp
) {
489 xfs_blkdev_put(logdev
);
490 xfs_blkdev_put(rtdev
);
495 * Setup flags based on mount(2) options and then the superblock
497 error
= xfs_start_flags(args
, mp
);
500 error
= xfs_readsb(mp
, flags
);
503 error
= xfs_finish_flags(args
, mp
);
508 * Setup xfs_mount buffer target pointers based on superblock
510 error
= xfs_setsize_buftarg(mp
->m_ddev_targp
, mp
->m_sb
.sb_blocksize
,
511 mp
->m_sb
.sb_sectsize
);
512 if (!error
&& logdev
&& logdev
!= ddev
) {
513 unsigned int log_sector_size
= BBSIZE
;
515 if (XFS_SB_VERSION_HASSECTOR(&mp
->m_sb
))
516 log_sector_size
= mp
->m_sb
.sb_logsectsize
;
517 error
= xfs_setsize_buftarg(mp
->m_logdev_targp
,
518 mp
->m_sb
.sb_blocksize
,
522 error
= xfs_setsize_buftarg(mp
->m_rtdev_targp
,
523 mp
->m_sb
.sb_blocksize
,
524 mp
->m_sb
.sb_sectsize
);
528 if (mp
->m_flags
& XFS_MOUNT_BARRIER
)
529 xfs_mountfs_check_barriers(mp
);
531 if ((error
= xfs_filestream_mount(mp
)))
534 error
= xfs_mountfs(mp
, flags
);
538 XFS_SEND_MOUNT(mp
, DM_RIGHT_NULL
, args
->mtpt
, args
->fsname
);
546 xfs_binval(mp
->m_ddev_targp
);
547 if (logdev
&& logdev
!= ddev
)
548 xfs_binval(mp
->m_logdev_targp
);
550 xfs_binval(mp
->m_rtdev_targp
);
552 xfs_unmountfs_close(mp
, credp
);
566 int unmount_event_wanted
= 0;
567 int unmount_event_flags
= 0;
568 int xfs_unmountfs_needed
= 0;
575 if (mp
->m_flags
& XFS_MOUNT_DMAPI
) {
576 error
= XFS_SEND_PREUNMOUNT(mp
,
577 rvp
, DM_RIGHT_NULL
, rvp
, DM_RIGHT_NULL
,
579 (mp
->m_dmevmask
& (1<<DM_EVENT_PREUNMOUNT
))?
580 0:DM_FLAGS_UNWANTED
);
582 return XFS_ERROR(error
);
583 unmount_event_wanted
= 1;
584 unmount_event_flags
= (mp
->m_dmevmask
& (1<<DM_EVENT_UNMOUNT
))?
585 0 : DM_FLAGS_UNWANTED
;
589 * First blow any referenced inode from this file system
590 * out of the reference cache, and delete the timer.
592 xfs_refcache_purge_mp(mp
);
595 * Blow away any referenced inode in the filestreams cache.
596 * This can and will cause log traffic as inodes go inactive
599 xfs_filestream_unmount(mp
);
601 XFS_bflush(mp
->m_ddev_targp
);
602 error
= xfs_unmount_flush(mp
, 0);
606 ASSERT(vn_count(rvp
) == 1);
609 * Drop the reference count
614 * If we're forcing a shutdown, typically because of a media error,
615 * we want to make sure we invalidate dirty pages that belong to
616 * referenced vnodes as well.
618 if (XFS_FORCED_SHUTDOWN(mp
)) {
619 error
= xfs_sync(mp
, SYNC_WAIT
| SYNC_CLOSE
);
620 ASSERT(error
!= EFSCORRUPTED
);
622 xfs_unmountfs_needed
= 1;
625 /* Send DMAPI event, if required.
626 * Then do xfs_unmountfs() if needed.
627 * Then return error (or zero).
629 if (unmount_event_wanted
) {
630 /* Note: mp structure must still exist for
631 * XFS_SEND_UNMOUNT() call.
633 XFS_SEND_UNMOUNT(mp
, error
== 0 ? rvp
: NULL
,
634 DM_RIGHT_NULL
, 0, error
, unmount_event_flags
);
636 if (xfs_unmountfs_needed
) {
638 * Call common unmount function to flush to disk
639 * and free the super block buffer & mount structures.
641 xfs_unmountfs(mp
, credp
);
644 kmem_free(mp
, sizeof(xfs_mount_t
));
647 return XFS_ERROR(error
);
654 int count
= 0, pincount
;
656 xfs_refcache_purge_mp(mp
);
657 xfs_flush_buftarg(mp
->m_ddev_targp
, 0);
658 xfs_finish_reclaim_all(mp
, 0);
660 /* This loop must run at least twice.
661 * The first instance of the loop will flush
662 * most meta data but that will generate more
663 * meta data (typically directory updates).
664 * Which then must be flushed and logged before
665 * we can write the unmount record.
668 xfs_syncsub(mp
, SYNC_INODE_QUIESCE
, NULL
);
669 pincount
= xfs_flush_buftarg(mp
->m_ddev_targp
, 1);
680 * Second stage of a quiesce. The data is already synced, now we have to take
681 * care of the metadata. New transactions are already blocked, so we need to
682 * wait for any remaining transactions to drain out before proceding.
688 /* wait for all modifications to complete */
689 while (atomic_read(&mp
->m_active_trans
) > 0)
692 /* flush inodes and push all remaining buffers out to disk */
695 ASSERT_ALWAYS(atomic_read(&mp
->m_active_trans
) == 0);
697 /* Push the superblock and write an unmount record */
698 xfs_log_sbcount(mp
, 1);
699 xfs_log_unmount_write(mp
);
700 xfs_unmountfs_writesb(mp
);
705 struct xfs_mount
*mp
,
707 struct xfs_mount_args
*args
)
709 if (!(*flags
& MS_RDONLY
)) { /* rw/ro -> rw */
710 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
711 mp
->m_flags
&= ~XFS_MOUNT_RDONLY
;
712 if (args
->flags
& XFSMNT_BARRIER
) {
713 mp
->m_flags
|= XFS_MOUNT_BARRIER
;
714 xfs_mountfs_check_barriers(mp
);
716 mp
->m_flags
&= ~XFS_MOUNT_BARRIER
;
718 } else if (!(mp
->m_flags
& XFS_MOUNT_RDONLY
)) { /* rw -> ro */
719 xfs_filestream_flush(mp
);
720 xfs_sync(mp
, SYNC_DATA_QUIESCE
);
721 xfs_attr_quiesce(mp
);
722 mp
->m_flags
|= XFS_MOUNT_RDONLY
;
728 * xfs_unmount_flush implements a set of flush operation on special
729 * inodes, which are needed as a separate set of operations so that
730 * they can be called as part of relocation process.
734 xfs_mount_t
*mp
, /* Mount structure we are getting
736 int relocation
) /* Called from vfs relocation. */
738 xfs_inode_t
*rip
= mp
->m_rootip
;
740 xfs_inode_t
*rsumip
= NULL
;
741 bhv_vnode_t
*rvp
= XFS_ITOV(rip
);
744 xfs_ilock(rip
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
748 * Flush out the real time inodes.
750 if ((rbmip
= mp
->m_rbmip
) != NULL
) {
751 xfs_ilock(rbmip
, XFS_ILOCK_EXCL
);
753 error
= xfs_iflush(rbmip
, XFS_IFLUSH_SYNC
);
754 xfs_iunlock(rbmip
, XFS_ILOCK_EXCL
);
756 if (error
== EFSCORRUPTED
)
759 ASSERT(vn_count(XFS_ITOV(rbmip
)) == 1);
761 rsumip
= mp
->m_rsumip
;
762 xfs_ilock(rsumip
, XFS_ILOCK_EXCL
);
764 error
= xfs_iflush(rsumip
, XFS_IFLUSH_SYNC
);
765 xfs_iunlock(rsumip
, XFS_ILOCK_EXCL
);
767 if (error
== EFSCORRUPTED
)
770 ASSERT(vn_count(XFS_ITOV(rsumip
)) == 1);
774 * Synchronously flush root inode to disk
776 error
= xfs_iflush(rip
, XFS_IFLUSH_SYNC
);
777 if (error
== EFSCORRUPTED
)
780 if (vn_count(rvp
) != 1 && !relocation
) {
781 xfs_iunlock(rip
, XFS_ILOCK_EXCL
);
782 return XFS_ERROR(EBUSY
);
786 * Release dquot that rootinode, rbmino and rsumino might be holding,
787 * flush and purge the quota inodes.
789 error
= XFS_QM_UNMOUNT(mp
);
790 if (error
== EFSCORRUPTED
)
794 VN_RELE(XFS_ITOV(rbmip
));
795 VN_RELE(XFS_ITOV(rsumip
));
798 xfs_iunlock(rip
, XFS_ILOCK_EXCL
);
805 xfs_iunlock(rip
, XFS_ILOCK_EXCL
);
807 return XFS_ERROR(EFSCORRUPTED
);
811 * xfs_sync flushes any pending I/O to file system vfsp.
813 * This routine is called by vfs_sync() to make sure that things make it
814 * out to disk eventually, on sync() system calls to flush out everything,
815 * and when the file system is unmounted. For the vfs_sync() case, all
816 * we really need to do is sync out the log to make all of our meta-data
817 * updates permanent (except for timestamps). For calls from pflushd(),
818 * dirty pages are kept moving by calling pdflush() on the inodes
819 * containing them. We also flush the inodes that we can lock without
820 * sleeping and the superblock if we can lock it without sleeping from
821 * vfs_sync() so that items at the tail of the log are always moving out.
824 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
825 * to sleep if we can help it. All we really need
826 * to do is ensure that the log is synced at least
827 * periodically. We also push the inodes and
828 * superblock if we can lock them without sleeping
829 * and they are not pinned.
830 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
831 * set, then we really want to lock each inode and flush
833 * SYNC_WAIT - All the flushes that take place in this call should
835 * SYNC_DELWRI - This tells us to push dirty pages associated with
836 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
837 * determine if they should be flushed sync, async, or
839 * SYNC_CLOSE - This flag is passed when the system is being
840 * unmounted. We should sync and invalidate everything.
841 * SYNC_FSDATA - This indicates that the caller would like to make
842 * sure the superblock is safe on disk. We can ensure
843 * this by simply making sure the log gets flushed
844 * if SYNC_BDFLUSH is set, and by actually writing it
846 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
847 * before we return (including direct I/O). Forms the drain
848 * side of the write barrier needed to safely quiesce the
860 * Get the Quota Manager to flush the dquots.
862 * If XFS quota support is not enabled or this filesystem
863 * instance does not use quotas XFS_QM_DQSYNC will always
866 error
= XFS_QM_DQSYNC(mp
, flags
);
869 * If we got an IO error, we will be shutting down.
870 * So, there's nothing more for us to do here.
872 ASSERT(error
!= EIO
|| XFS_FORCED_SHUTDOWN(mp
));
873 if (XFS_FORCED_SHUTDOWN(mp
))
874 return XFS_ERROR(error
);
877 if (flags
& SYNC_IOWAIT
)
878 xfs_filestream_flush(mp
);
880 return xfs_syncsub(mp
, flags
, NULL
);
884 * xfs sync routine for internal use
886 * This routine supports all of the flags defined for the generic vfs_sync
887 * interface as explained above under xfs_sync.
896 xfs_inode_t
*ip
= NULL
;
897 bhv_vnode_t
*vp
= NULL
;
902 uint base_lock_flags
;
903 boolean_t mount_locked
;
904 boolean_t vnode_refed
;
906 xfs_iptr_t
*ipointer
;
908 boolean_t ipointer_in
= B_FALSE
;
910 #define IPOINTER_SET ipointer_in = B_TRUE
911 #define IPOINTER_CLR ipointer_in = B_FALSE
918 /* Insert a marker record into the inode list after inode ip. The list
919 * must be locked when this is called. After the call the list will no
922 #define IPOINTER_INSERT(ip, mp) { \
923 ASSERT(ipointer_in == B_FALSE); \
924 ipointer->ip_mnext = ip->i_mnext; \
925 ipointer->ip_mprev = ip; \
926 ip->i_mnext = (xfs_inode_t *)ipointer; \
927 ipointer->ip_mnext->i_mprev = (xfs_inode_t *)ipointer; \
929 XFS_MOUNT_IUNLOCK(mp); \
930 mount_locked = B_FALSE; \
934 /* Remove the marker from the inode list. If the marker was the only item
935 * in the list then there are no remaining inodes and we should zero out
936 * the whole list. If we are the current head of the list then move the head
939 #define IPOINTER_REMOVE(ip, mp) { \
940 ASSERT(ipointer_in == B_TRUE); \
941 if (ipointer->ip_mnext != (xfs_inode_t *)ipointer) { \
942 ip = ipointer->ip_mnext; \
943 ip->i_mprev = ipointer->ip_mprev; \
944 ipointer->ip_mprev->i_mnext = ip; \
945 if (mp->m_inodes == (xfs_inode_t *)ipointer) { \
949 ASSERT(mp->m_inodes == (xfs_inode_t *)ipointer); \
950 mp->m_inodes = NULL; \
956 #define XFS_PREEMPT_MASK 0x7f
958 ASSERT(!(flags
& SYNC_BDFLUSH
));
962 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
968 /* Allocate a reference marker */
969 ipointer
= (xfs_iptr_t
*)kmem_zalloc(sizeof(xfs_iptr_t
), KM_SLEEP
);
971 fflag
= XFS_B_ASYNC
; /* default is don't wait */
972 if (flags
& SYNC_DELWRI
)
973 fflag
= XFS_B_DELWRI
;
974 if (flags
& SYNC_WAIT
)
975 fflag
= 0; /* synchronous overrides all */
977 base_lock_flags
= XFS_ILOCK_SHARED
;
978 if (flags
& (SYNC_DELWRI
| SYNC_CLOSE
)) {
980 * We need the I/O lock if we're going to call any of
981 * the flush/inval routines.
983 base_lock_flags
|= XFS_IOLOCK_SHARED
;
990 mount_locked
= B_TRUE
;
991 vnode_refed
= B_FALSE
;
996 ASSERT(ipointer_in
== B_FALSE
);
997 ASSERT(vnode_refed
== B_FALSE
);
999 lock_flags
= base_lock_flags
;
1002 * There were no inodes in the list, just break out
1010 * We found another sync thread marker - skip it
1012 if (ip
->i_mount
== NULL
) {
1017 vp
= XFS_ITOV_NULL(ip
);
1020 * If the vnode is gone then this is being torn down,
1021 * call reclaim if it is flushed, else let regular flush
1022 * code deal with it later in the loop.
1026 /* Skip ones already in reclaim */
1027 if (ip
->i_flags
& XFS_IRECLAIM
) {
1031 if (xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
) == 0) {
1033 } else if ((xfs_ipincount(ip
) == 0) &&
1034 xfs_iflock_nowait(ip
)) {
1035 IPOINTER_INSERT(ip
, mp
);
1037 xfs_finish_reclaim(ip
, 1,
1038 XFS_IFLUSH_DELWRI_ELSE_ASYNC
);
1040 XFS_MOUNT_ILOCK(mp
);
1041 mount_locked
= B_TRUE
;
1042 IPOINTER_REMOVE(ip
, mp
);
1044 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1055 if (XFS_FORCED_SHUTDOWN(mp
) && !(flags
& SYNC_CLOSE
)) {
1056 XFS_MOUNT_IUNLOCK(mp
);
1057 kmem_free(ipointer
, sizeof(xfs_iptr_t
));
1062 * Try to lock without sleeping. We're out of order with
1063 * the inode list lock here, so if we fail we need to drop
1064 * the mount lock and try again. If we're called from
1065 * bdflush() here, then don't bother.
1067 * The inode lock here actually coordinates with the
1068 * almost spurious inode lock in xfs_ireclaim() to prevent
1069 * the vnode we handle here without a reference from
1070 * being freed while we reference it. If we lock the inode
1071 * while it's on the mount list here, then the spurious inode
1072 * lock in xfs_ireclaim() after the inode is pulled from
1073 * the mount list will sleep until we release it here.
1074 * This keeps the vnode from being freed while we reference
1077 if (xfs_ilock_nowait(ip
, lock_flags
) == 0) {
1089 IPOINTER_INSERT(ip
, mp
);
1090 xfs_ilock(ip
, lock_flags
);
1092 ASSERT(vp
== XFS_ITOV(ip
));
1093 ASSERT(ip
->i_mount
== mp
);
1095 vnode_refed
= B_TRUE
;
1098 /* From here on in the loop we may have a marker record
1099 * in the inode list.
1103 * If we have to flush data or wait for I/O completion
1104 * we need to drop the ilock that we currently hold.
1105 * If we need to drop the lock, insert a marker if we
1106 * have not already done so.
1108 if ((flags
& (SYNC_CLOSE
|SYNC_IOWAIT
)) ||
1109 ((flags
& SYNC_DELWRI
) && VN_DIRTY(vp
))) {
1111 IPOINTER_INSERT(ip
, mp
);
1113 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
1115 if (flags
& SYNC_CLOSE
) {
1116 /* Shutdown case. Flush and invalidate. */
1117 if (XFS_FORCED_SHUTDOWN(mp
))
1118 xfs_tosspages(ip
, 0, -1,
1121 error
= xfs_flushinval_pages(ip
,
1123 } else if ((flags
& SYNC_DELWRI
) && VN_DIRTY(vp
)) {
1124 error
= xfs_flush_pages(ip
, 0,
1125 -1, fflag
, FI_NONE
);
1129 * When freezing, we need to wait ensure all I/O (including direct
1130 * I/O) is complete to ensure no further data modification can take
1131 * place after this point
1133 if (flags
& SYNC_IOWAIT
)
1136 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
1139 if ((flags
& SYNC_ATTR
) &&
1140 (ip
->i_update_core
||
1141 (ip
->i_itemp
&& ip
->i_itemp
->ili_format
.ilf_fields
))) {
1143 IPOINTER_INSERT(ip
, mp
);
1145 if (flags
& SYNC_WAIT
) {
1147 error
= xfs_iflush(ip
, XFS_IFLUSH_SYNC
);
1150 * If we can't acquire the flush lock, then the inode
1151 * is already being flushed so don't bother waiting.
1153 * If we can lock it then do a delwri flush so we can
1154 * combine multiple inode flushes in each disk write.
1156 } else if (xfs_iflock_nowait(ip
)) {
1157 error
= xfs_iflush(ip
, XFS_IFLUSH_DELWRI
);
1158 } else if (bypassed
) {
1163 if (lock_flags
!= 0) {
1164 xfs_iunlock(ip
, lock_flags
);
1169 * If we had to take a reference on the vnode
1170 * above, then wait until after we've unlocked
1171 * the inode to release the reference. This is
1172 * because we can be already holding the inode
1173 * lock when VN_RELE() calls xfs_inactive().
1175 * Make sure to drop the mount lock before calling
1176 * VN_RELE() so that we don't trip over ourselves if
1177 * we have to go for the mount lock again in the
1181 IPOINTER_INSERT(ip
, mp
);
1186 vnode_refed
= B_FALSE
;
1194 * bail out if the filesystem is corrupted.
1196 if (error
== EFSCORRUPTED
) {
1197 if (!mount_locked
) {
1198 XFS_MOUNT_ILOCK(mp
);
1199 IPOINTER_REMOVE(ip
, mp
);
1201 XFS_MOUNT_IUNLOCK(mp
);
1202 ASSERT(ipointer_in
== B_FALSE
);
1203 kmem_free(ipointer
, sizeof(xfs_iptr_t
));
1204 return XFS_ERROR(error
);
1207 /* Let other threads have a chance at the mount lock
1208 * if we have looped many times without dropping the
1211 if ((++preempt
& XFS_PREEMPT_MASK
) == 0) {
1213 IPOINTER_INSERT(ip
, mp
);
1217 if (mount_locked
== B_FALSE
) {
1218 XFS_MOUNT_ILOCK(mp
);
1219 mount_locked
= B_TRUE
;
1220 IPOINTER_REMOVE(ip
, mp
);
1224 ASSERT(ipointer_in
== B_FALSE
);
1227 } while (ip
!= mp
->m_inodes
);
1229 XFS_MOUNT_IUNLOCK(mp
);
1231 ASSERT(ipointer_in
== B_FALSE
);
1233 kmem_free(ipointer
, sizeof(xfs_iptr_t
));
1234 return XFS_ERROR(last_error
);
1238 * xfs sync routine for internal use
1240 * This routine supports all of the flags defined for the generic vfs_sync
1241 * interface as explained above under xfs_sync.
1252 uint log_flags
= XFS_LOG_FORCE
;
1254 xfs_buf_log_item_t
*bip
;
1257 * Sync out the log. This ensures that the log is periodically
1258 * flushed even if there is not enough activity to fill it up.
1260 if (flags
& SYNC_WAIT
)
1261 log_flags
|= XFS_LOG_SYNC
;
1263 xfs_log_force(mp
, (xfs_lsn_t
)0, log_flags
);
1265 if (flags
& (SYNC_ATTR
|SYNC_DELWRI
)) {
1266 if (flags
& SYNC_BDFLUSH
)
1267 xfs_finish_reclaim_all(mp
, 1);
1269 error
= xfs_sync_inodes(mp
, flags
, bypassed
);
1273 * Flushing out dirty data above probably generated more
1274 * log activity, so if this isn't vfs_sync() then flush
1277 if (flags
& SYNC_DELWRI
) {
1278 xfs_log_force(mp
, (xfs_lsn_t
)0, log_flags
);
1281 if (flags
& SYNC_FSDATA
) {
1283 * If this is vfs_sync() then only sync the superblock
1284 * if we can lock it without sleeping and it is not pinned.
1286 if (flags
& SYNC_BDFLUSH
) {
1287 bp
= xfs_getsb(mp
, XFS_BUF_TRYLOCK
);
1289 bip
= XFS_BUF_FSPRIVATE(bp
,xfs_buf_log_item_t
*);
1290 if ((bip
!= NULL
) &&
1291 xfs_buf_item_dirty(bip
)) {
1292 if (!(XFS_BUF_ISPINNED(bp
))) {
1294 error
= xfs_bwrite(mp
, bp
);
1303 bp
= xfs_getsb(mp
, 0);
1305 * If the buffer is pinned then push on the log so
1306 * we won't get stuck waiting in the write for
1307 * someone, maybe ourselves, to flush the log.
1308 * Even though we just pushed the log above, we
1309 * did not have the superblock buffer locked at
1310 * that point so it can become pinned in between
1313 if (XFS_BUF_ISPINNED(bp
))
1314 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
);
1315 if (flags
& SYNC_WAIT
)
1316 XFS_BUF_UNASYNC(bp
);
1319 error
= xfs_bwrite(mp
, bp
);
1327 * If this is the periodic sync, then kick some entries out of
1328 * the reference cache. This ensures that idle entries are
1329 * eventually kicked out of the cache.
1331 if (flags
& SYNC_REFCACHE
) {
1332 if (flags
& SYNC_WAIT
)
1333 xfs_refcache_purge_mp(mp
);
1335 xfs_refcache_purge_some(mp
);
1339 * If asked, update the disk superblock with incore counter values if we
1340 * are using non-persistent counters so that they don't get too far out
1341 * of sync if we crash or get a forced shutdown. We don't want to force
1342 * this to disk, just get a transaction into the iclogs....
1344 if (flags
& SYNC_SUPER
)
1345 xfs_log_sbcount(mp
, 0);
1348 * Now check to see if the log needs a "dummy" transaction.
1351 if (!(flags
& SYNC_REMOUNT
) && xfs_log_need_covered(mp
)) {
1356 * Put a dummy transaction in the log to tell
1357 * recovery that all others are OK.
1359 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DUMMY1
);
1360 if ((error
= xfs_trans_reserve(tp
, 0,
1361 XFS_ICHANGE_LOG_RES(mp
),
1363 xfs_trans_cancel(tp
, 0);
1368 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1370 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1371 xfs_trans_ihold(tp
, ip
);
1372 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1373 error
= xfs_trans_commit(tp
, 0);
1374 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1375 xfs_log_force(mp
, (xfs_lsn_t
)0, log_flags
);
1379 * When shutting down, we need to insure that the AIL is pushed
1380 * to disk or the filesystem can appear corrupt from the PROM.
1382 if ((flags
& (SYNC_CLOSE
|SYNC_WAIT
)) == (SYNC_CLOSE
|SYNC_WAIT
)) {
1383 XFS_bflush(mp
->m_ddev_targp
);
1384 if (mp
->m_rtdev_targp
) {
1385 XFS_bflush(mp
->m_rtdev_targp
);
1389 return XFS_ERROR(last_error
);