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_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_btree.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_inode.h"
37 #include "xfs_dinode.h"
38 #include "xfs_error.h"
39 #include "xfs_mru_cache.h"
40 #include "xfs_filestream.h"
41 #include "xfs_vnodeops.h"
42 #include "xfs_utils.h"
43 #include "xfs_buf_item.h"
44 #include "xfs_inode_item.h"
46 #include "xfs_quota.h"
48 #include <linux/kthread.h>
49 #include <linux/freezer.h>
55 struct xfs_perag
*pag
,
56 uint32_t *first_index
,
63 * use a gang lookup to find the next inode in the tree
64 * as the tree is sparse and a gang lookup walks to find
65 * the number of objects requested.
67 read_lock(&pag
->pag_ici_lock
);
68 if (tag
== XFS_ICI_NO_TAG
) {
69 nr_found
= radix_tree_gang_lookup(&pag
->pag_ici_root
,
70 (void **)&ip
, *first_index
, 1);
72 nr_found
= radix_tree_gang_lookup_tag(&pag
->pag_ici_root
,
73 (void **)&ip
, *first_index
, 1, tag
);
79 * Update the index for the next lookup. Catch overflows
80 * into the next AG range which can occur if we have inodes
81 * in the last block of the AG and we are currently
82 * pointing to the last inode.
84 *first_index
= XFS_INO_TO_AGINO(mp
, ip
->i_ino
+ 1);
85 if (*first_index
< XFS_INO_TO_AGINO(mp
, ip
->i_ino
))
91 read_unlock(&pag
->pag_ici_lock
);
99 int (*execute
)(struct xfs_inode
*ip
,
100 struct xfs_perag
*pag
, int flags
),
104 struct xfs_perag
*pag
= &mp
->m_perag
[ag
];
105 uint32_t first_index
;
116 ip
= xfs_inode_ag_lookup(mp
, pag
, &first_index
, tag
);
120 error
= execute(ip
, pag
, flags
);
121 if (error
== EAGAIN
) {
128 * bail out if the filesystem is corrupted.
130 if (error
== EFSCORRUPTED
)
140 xfs_put_perag(mp
, pag
);
145 xfs_inode_ag_iterator(
146 struct xfs_mount
*mp
,
147 int (*execute
)(struct xfs_inode
*ip
,
148 struct xfs_perag
*pag
, int flags
),
156 for (ag
= 0; ag
< mp
->m_sb
.sb_agcount
; ag
++) {
157 if (!mp
->m_perag
[ag
].pag_ici_init
)
159 error
= xfs_inode_ag_walk(mp
, ag
, execute
, flags
, tag
);
162 if (error
== EFSCORRUPTED
)
166 return XFS_ERROR(last_error
);
169 /* must be called with pag_ici_lock held and releases it */
171 xfs_sync_inode_valid(
172 struct xfs_inode
*ip
,
173 struct xfs_perag
*pag
)
175 struct inode
*inode
= VFS_I(ip
);
177 /* nothing to sync during shutdown */
178 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
179 read_unlock(&pag
->pag_ici_lock
);
184 * If we can't get a reference on the inode, it must be in reclaim.
185 * Leave it for the reclaim code to flush. Also avoid inodes that
186 * haven't been fully initialised.
189 read_unlock(&pag
->pag_ici_lock
);
192 read_unlock(&pag
->pag_ici_lock
);
194 if (is_bad_inode(inode
) || xfs_iflags_test(ip
, XFS_INEW
)) {
204 struct xfs_inode
*ip
,
205 struct xfs_perag
*pag
,
208 struct inode
*inode
= VFS_I(ip
);
209 struct address_space
*mapping
= inode
->i_mapping
;
212 error
= xfs_sync_inode_valid(ip
, pag
);
216 if (!mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
219 if (!xfs_ilock_nowait(ip
, XFS_IOLOCK_SHARED
)) {
220 if (flags
& SYNC_TRYLOCK
)
222 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
225 error
= xfs_flush_pages(ip
, 0, -1, (flags
& SYNC_WAIT
) ?
226 0 : XFS_B_ASYNC
, FI_NONE
);
227 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
230 if (flags
& SYNC_WAIT
)
238 struct xfs_inode
*ip
,
239 struct xfs_perag
*pag
,
244 error
= xfs_sync_inode_valid(ip
, pag
);
248 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
249 if (xfs_inode_clean(ip
))
251 if (!xfs_iflock_nowait(ip
)) {
252 if (!(flags
& SYNC_WAIT
))
257 if (xfs_inode_clean(ip
)) {
262 error
= xfs_iflush(ip
, (flags
& SYNC_WAIT
) ?
263 XFS_IFLUSH_SYNC
: XFS_IFLUSH_DELWRI
);
266 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
272 * Write out pagecache data for the whole filesystem.
276 struct xfs_mount
*mp
,
281 ASSERT((flags
& ~(SYNC_TRYLOCK
|SYNC_WAIT
)) == 0);
283 error
= xfs_inode_ag_iterator(mp
, xfs_sync_inode_data
, flags
,
286 return XFS_ERROR(error
);
289 (flags
& SYNC_WAIT
) ?
290 XFS_LOG_FORCE
| XFS_LOG_SYNC
:
296 * Write out inode metadata (attributes) for the whole filesystem.
300 struct xfs_mount
*mp
,
303 ASSERT((flags
& ~SYNC_WAIT
) == 0);
305 return xfs_inode_ag_iterator(mp
, xfs_sync_inode_attr
, flags
,
310 xfs_commit_dummy_trans(
311 struct xfs_mount
*mp
,
314 struct xfs_inode
*ip
= mp
->m_rootip
;
315 struct xfs_trans
*tp
;
317 int log_flags
= XFS_LOG_FORCE
;
319 if (flags
& SYNC_WAIT
)
320 log_flags
|= XFS_LOG_SYNC
;
323 * Put a dummy transaction in the log to tell recovery
324 * that all others are OK.
326 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DUMMY1
);
327 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES(mp
), 0, 0, 0);
329 xfs_trans_cancel(tp
, 0);
333 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
335 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
336 xfs_trans_ihold(tp
, ip
);
337 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
338 error
= xfs_trans_commit(tp
, 0);
339 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
341 /* the log force ensures this transaction is pushed to disk */
342 xfs_log_force(mp
, 0, log_flags
);
348 struct xfs_mount
*mp
,
352 struct xfs_buf_log_item
*bip
;
356 * If this is xfssyncd() then only sync the superblock if we can
357 * lock it without sleeping and it is not pinned.
359 if (flags
& SYNC_TRYLOCK
) {
360 ASSERT(!(flags
& SYNC_WAIT
));
362 bp
= xfs_getsb(mp
, XFS_BUF_TRYLOCK
);
366 bip
= XFS_BUF_FSPRIVATE(bp
, struct xfs_buf_log_item
*);
367 if (!bip
|| !xfs_buf_item_dirty(bip
) || XFS_BUF_ISPINNED(bp
))
370 bp
= xfs_getsb(mp
, 0);
373 * If the buffer is pinned then push on the log so we won't
374 * get stuck waiting in the write for someone, maybe
375 * ourselves, to flush the log.
377 * Even though we just pushed the log above, we did not have
378 * the superblock buffer locked at that point so it can
379 * become pinned in between there and here.
381 if (XFS_BUF_ISPINNED(bp
))
382 xfs_log_force(mp
, 0, XFS_LOG_FORCE
);
386 if (flags
& SYNC_WAIT
)
391 error
= xfs_bwrite(mp
, bp
);
396 * If this is a data integrity sync make sure all pending buffers
397 * are flushed out for the log coverage check below.
399 if (flags
& SYNC_WAIT
)
400 xfs_flush_buftarg(mp
->m_ddev_targp
, 1);
402 if (xfs_log_need_covered(mp
))
403 error
= xfs_commit_dummy_trans(mp
, flags
);
413 * When remounting a filesystem read-only or freezing the filesystem, we have
414 * two phases to execute. This first phase is syncing the data before we
415 * quiesce the filesystem, and the second is flushing all the inodes out after
416 * we've waited for all the transactions created by the first phase to
417 * complete. The second phase ensures that the inodes are written to their
418 * location on disk rather than just existing in transactions in the log. This
419 * means after a quiesce there is no log replay required to write the inodes to
420 * disk (this is the main difference between a sync and a quiesce).
423 * First stage of freeze - no writers will make progress now we are here,
424 * so we flush delwri and delalloc buffers here, then wait for all I/O to
425 * complete. Data is frozen at that point. Metadata is not frozen,
426 * transactions can still occur here so don't bother flushing the buftarg
427 * because it'll just get dirty again.
431 struct xfs_mount
*mp
)
435 /* push non-blocking */
436 xfs_sync_data(mp
, 0);
437 xfs_qm_sync(mp
, SYNC_TRYLOCK
);
439 /* push and block till complete */
440 xfs_sync_data(mp
, SYNC_WAIT
);
441 xfs_qm_sync(mp
, SYNC_WAIT
);
443 /* drop inode references pinned by filestreams */
444 xfs_filestream_flush(mp
);
446 /* write superblock and hoover up shutdown errors */
447 error
= xfs_sync_fsdata(mp
, SYNC_WAIT
);
449 /* flush data-only devices */
450 if (mp
->m_rtdev_targp
)
451 XFS_bflush(mp
->m_rtdev_targp
);
458 struct xfs_mount
*mp
)
460 int count
= 0, pincount
;
462 xfs_flush_buftarg(mp
->m_ddev_targp
, 0);
463 xfs_reclaim_inodes(mp
, XFS_IFLUSH_DELWRI_ELSE_ASYNC
);
466 * This loop must run at least twice. The first instance of the loop
467 * will flush most meta data but that will generate more meta data
468 * (typically directory updates). Which then must be flushed and
469 * logged before we can write the unmount record.
472 xfs_sync_attr(mp
, SYNC_WAIT
);
473 pincount
= xfs_flush_buftarg(mp
->m_ddev_targp
, 1);
482 * Second stage of a quiesce. The data is already synced, now we have to take
483 * care of the metadata. New transactions are already blocked, so we need to
484 * wait for any remaining transactions to drain out before proceding.
488 struct xfs_mount
*mp
)
492 /* wait for all modifications to complete */
493 while (atomic_read(&mp
->m_active_trans
) > 0)
496 /* flush inodes and push all remaining buffers out to disk */
500 * Just warn here till VFS can correctly support
501 * read-only remount without racing.
503 WARN_ON(atomic_read(&mp
->m_active_trans
) != 0);
505 /* Push the superblock and write an unmount record */
506 error
= xfs_log_sbcount(mp
, 1);
508 xfs_fs_cmn_err(CE_WARN
, mp
,
509 "xfs_attr_quiesce: failed to log sb changes. "
510 "Frozen image may not be consistent.");
511 xfs_log_unmount_write(mp
);
512 xfs_unmountfs_writesb(mp
);
516 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
517 * Doing this has two advantages:
518 * - It saves on stack space, which is tight in certain situations
519 * - It can be used (with care) as a mechanism to avoid deadlocks.
520 * Flushing while allocating in a full filesystem requires both.
523 xfs_syncd_queue_work(
524 struct xfs_mount
*mp
,
526 void (*syncer
)(struct xfs_mount
*, void *),
527 struct completion
*completion
)
529 struct xfs_sync_work
*work
;
531 work
= kmem_alloc(sizeof(struct xfs_sync_work
), KM_SLEEP
);
532 INIT_LIST_HEAD(&work
->w_list
);
533 work
->w_syncer
= syncer
;
536 work
->w_completion
= completion
;
537 spin_lock(&mp
->m_sync_lock
);
538 list_add_tail(&work
->w_list
, &mp
->m_sync_list
);
539 spin_unlock(&mp
->m_sync_lock
);
540 wake_up_process(mp
->m_sync_task
);
544 * Flush delayed allocate data, attempting to free up reserved space
545 * from existing allocations. At this point a new allocation attempt
546 * has failed with ENOSPC and we are in the process of scratching our
547 * heads, looking about for more room...
550 xfs_flush_inodes_work(
551 struct xfs_mount
*mp
,
554 struct inode
*inode
= arg
;
555 xfs_sync_data(mp
, SYNC_TRYLOCK
);
556 xfs_sync_data(mp
, SYNC_TRYLOCK
| SYNC_WAIT
);
564 struct inode
*inode
= VFS_I(ip
);
565 DECLARE_COMPLETION_ONSTACK(completion
);
568 xfs_syncd_queue_work(ip
->i_mount
, inode
, xfs_flush_inodes_work
, &completion
);
569 wait_for_completion(&completion
);
570 xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0, XFS_LOG_FORCE
|XFS_LOG_SYNC
);
574 * Every sync period we need to unpin all items, reclaim inodes, sync
575 * quota and write out the superblock. We might need to cover the log
576 * to indicate it is idle.
580 struct xfs_mount
*mp
,
585 if (!(mp
->m_flags
& XFS_MOUNT_RDONLY
)) {
586 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
);
587 xfs_reclaim_inodes(mp
, XFS_IFLUSH_DELWRI_ELSE_ASYNC
);
588 /* dgc: errors ignored here */
589 error
= xfs_qm_sync(mp
, SYNC_TRYLOCK
);
590 error
= xfs_sync_fsdata(mp
, SYNC_TRYLOCK
);
593 wake_up(&mp
->m_wait_single_sync_task
);
600 struct xfs_mount
*mp
= arg
;
602 xfs_sync_work_t
*work
, *n
;
606 timeleft
= xfs_syncd_centisecs
* msecs_to_jiffies(10);
608 timeleft
= schedule_timeout_interruptible(timeleft
);
611 if (kthread_should_stop() && list_empty(&mp
->m_sync_list
))
614 spin_lock(&mp
->m_sync_lock
);
616 * We can get woken by laptop mode, to do a sync -
617 * that's the (only!) case where the list would be
618 * empty with time remaining.
620 if (!timeleft
|| list_empty(&mp
->m_sync_list
)) {
622 timeleft
= xfs_syncd_centisecs
*
623 msecs_to_jiffies(10);
624 INIT_LIST_HEAD(&mp
->m_sync_work
.w_list
);
625 list_add_tail(&mp
->m_sync_work
.w_list
,
628 list_for_each_entry_safe(work
, n
, &mp
->m_sync_list
, w_list
)
629 list_move(&work
->w_list
, &tmp
);
630 spin_unlock(&mp
->m_sync_lock
);
632 list_for_each_entry_safe(work
, n
, &tmp
, w_list
) {
633 (*work
->w_syncer
)(mp
, work
->w_data
);
634 list_del(&work
->w_list
);
635 if (work
== &mp
->m_sync_work
)
637 if (work
->w_completion
)
638 complete(work
->w_completion
);
648 struct xfs_mount
*mp
)
650 mp
->m_sync_work
.w_syncer
= xfs_sync_worker
;
651 mp
->m_sync_work
.w_mount
= mp
;
652 mp
->m_sync_work
.w_completion
= NULL
;
653 mp
->m_sync_task
= kthread_run(xfssyncd
, mp
, "xfssyncd");
654 if (IS_ERR(mp
->m_sync_task
))
655 return -PTR_ERR(mp
->m_sync_task
);
661 struct xfs_mount
*mp
)
663 kthread_stop(mp
->m_sync_task
);
672 xfs_perag_t
*pag
= xfs_get_perag(ip
->i_mount
, ip
->i_ino
);
674 /* The hash lock here protects a thread in xfs_iget_core from
675 * racing with us on linking the inode back with a vnode.
676 * Once we have the XFS_IRECLAIM flag set it will not touch
679 write_lock(&pag
->pag_ici_lock
);
680 spin_lock(&ip
->i_flags_lock
);
681 if (__xfs_iflags_test(ip
, XFS_IRECLAIM
) ||
682 !__xfs_iflags_test(ip
, XFS_IRECLAIMABLE
)) {
683 spin_unlock(&ip
->i_flags_lock
);
684 write_unlock(&pag
->pag_ici_lock
);
687 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
691 __xfs_iflags_set(ip
, XFS_IRECLAIM
);
692 spin_unlock(&ip
->i_flags_lock
);
693 write_unlock(&pag
->pag_ici_lock
);
694 xfs_put_perag(ip
->i_mount
, pag
);
697 * If the inode is still dirty, then flush it out. If the inode
698 * is not in the AIL, then it will be OK to flush it delwri as
699 * long as xfs_iflush() does not keep any references to the inode.
700 * We leave that decision up to xfs_iflush() since it has the
701 * knowledge of whether it's OK to simply do a delwri flush of
702 * the inode or whether we need to wait until the inode is
703 * pulled from the AIL.
704 * We get the flush lock regardless, though, just to make sure
705 * we don't free it while it is being flushed.
708 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
713 * In the case of a forced shutdown we rely on xfs_iflush() to
714 * wait for the inode to be unpinned before returning an error.
716 if (!is_bad_inode(VFS_I(ip
)) && xfs_iflush(ip
, sync_mode
) == 0) {
717 /* synchronize with xfs_iflush_done */
722 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
728 __xfs_inode_set_reclaim_tag(
729 struct xfs_perag
*pag
,
730 struct xfs_inode
*ip
)
732 radix_tree_tag_set(&pag
->pag_ici_root
,
733 XFS_INO_TO_AGINO(ip
->i_mount
, ip
->i_ino
),
734 XFS_ICI_RECLAIM_TAG
);
738 * We set the inode flag atomically with the radix tree tag.
739 * Once we get tag lookups on the radix tree, this inode flag
743 xfs_inode_set_reclaim_tag(
746 xfs_mount_t
*mp
= ip
->i_mount
;
747 xfs_perag_t
*pag
= xfs_get_perag(mp
, ip
->i_ino
);
749 read_lock(&pag
->pag_ici_lock
);
750 spin_lock(&ip
->i_flags_lock
);
751 __xfs_inode_set_reclaim_tag(pag
, ip
);
752 __xfs_iflags_set(ip
, XFS_IRECLAIMABLE
);
753 spin_unlock(&ip
->i_flags_lock
);
754 read_unlock(&pag
->pag_ici_lock
);
755 xfs_put_perag(mp
, pag
);
759 __xfs_inode_clear_reclaim_tag(
764 radix_tree_tag_clear(&pag
->pag_ici_root
,
765 XFS_INO_TO_AGINO(mp
, ip
->i_ino
), XFS_ICI_RECLAIM_TAG
);
769 xfs_reclaim_inode_now(
770 struct xfs_inode
*ip
,
771 struct xfs_perag
*pag
,
774 /* ignore if already under reclaim */
775 if (xfs_iflags_test(ip
, XFS_IRECLAIM
)) {
776 read_unlock(&pag
->pag_ici_lock
);
779 read_unlock(&pag
->pag_ici_lock
);
781 return xfs_reclaim_inode(ip
, 0, flags
);
789 return xfs_inode_ag_iterator(mp
, xfs_reclaim_inode_now
, mode
,
790 XFS_ICI_RECLAIM_TAG
);