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"
47 #include "xfs_trace.h"
49 #include <linux/kthread.h>
50 #include <linux/freezer.h>
56 struct xfs_perag *pag,
57 uint32_t *first_index,
64 * use a gang lookup to find the next inode in the tree
65 * as the tree is sparse and a gang lookup walks to find
66 * the number of objects requested.
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))
93 struct xfs_perag *pag,
94 int (*execute)(struct xfs_inode *ip,
95 struct xfs_perag *pag, int flags),
100 uint32_t first_index;
112 write_lock(&pag->pag_ici_lock);
114 read_lock(&pag->pag_ici_lock);
115 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
118 write_unlock(&pag->pag_ici_lock);
120 read_unlock(&pag->pag_ici_lock);
124 /* execute releases pag->pag_ici_lock */
125 error = execute(ip, pag, flags);
126 if (error == EAGAIN) {
133 /* bail out if the filesystem is corrupted. */
134 if (error == EFSCORRUPTED)
147 xfs_inode_ag_iterator(
148 struct xfs_mount *mp,
149 int (*execute)(struct xfs_inode *ip,
150 struct xfs_perag *pag, int flags),
159 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
160 struct xfs_perag *pag;
162 pag = xfs_perag_get(mp, ag);
163 if (!pag->pag_ici_init) {
167 error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
172 if (error == EFSCORRUPTED)
176 return XFS_ERROR(last_error);
179 /* must be called with pag_ici_lock held and releases it */
181 xfs_sync_inode_valid(
182 struct xfs_inode *ip,
183 struct xfs_perag *pag)
185 struct inode *inode = VFS_I(ip);
186 int error = EFSCORRUPTED;
188 /* nothing to sync during shutdown */
189 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
192 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
194 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
197 /* If we can't grab the inode, it must on it's way to reclaim. */
201 if (is_bad_inode(inode)) {
209 read_unlock(&pag->pag_ici_lock);
215 struct xfs_inode *ip,
216 struct xfs_perag *pag,
219 struct inode *inode = VFS_I(ip);
220 struct address_space *mapping = inode->i_mapping;
223 error = xfs_sync_inode_valid(ip, pag);
227 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
230 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
231 if (flags & SYNC_TRYLOCK)
233 xfs_ilock(ip, XFS_IOLOCK_SHARED);
236 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
237 0 : XBF_ASYNC, FI_NONE);
238 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
241 if (flags & SYNC_WAIT)
249 struct xfs_inode *ip,
250 struct xfs_perag *pag,
255 error = xfs_sync_inode_valid(ip, pag);
259 xfs_ilock(ip, XFS_ILOCK_SHARED);
260 if (xfs_inode_clean(ip))
262 if (!xfs_iflock_nowait(ip)) {
263 if (!(flags & SYNC_WAIT))
268 if (xfs_inode_clean(ip)) {
273 error = xfs_iflush(ip, flags);
276 xfs_iunlock(ip, XFS_ILOCK_SHARED);
282 * Write out pagecache data for the whole filesystem.
286 struct xfs_mount *mp,
291 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
293 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
296 return XFS_ERROR(error);
298 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
303 * Write out inode metadata (attributes) for the whole filesystem.
307 struct xfs_mount *mp,
310 ASSERT((flags & ~SYNC_WAIT) == 0);
312 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
317 xfs_commit_dummy_trans(
318 struct xfs_mount *mp,
321 struct xfs_inode *ip = mp->m_rootip;
322 struct xfs_trans *tp;
326 * Put a dummy transaction in the log to tell recovery
327 * that all others are OK.
329 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
330 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
332 xfs_trans_cancel(tp, 0);
336 xfs_ilock(ip, XFS_ILOCK_EXCL);
338 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
339 xfs_trans_ihold(tp, ip);
340 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
341 error = xfs_trans_commit(tp, 0);
342 xfs_iunlock(ip, XFS_ILOCK_EXCL);
344 /* the log force ensures this transaction is pushed to disk */
345 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
351 struct xfs_mount *mp,
355 struct xfs_buf_log_item *bip;
359 * If this is xfssyncd() then only sync the superblock if we can
360 * lock it without sleeping and it is not pinned.
362 if (flags & SYNC_TRYLOCK) {
363 ASSERT(!(flags & SYNC_WAIT));
365 bp = xfs_getsb(mp, XBF_TRYLOCK);
369 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
370 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
373 bp = xfs_getsb(mp, 0);
376 * If the buffer is pinned then push on the log so we won't
377 * get stuck waiting in the write for someone, maybe
378 * ourselves, to flush the log.
380 * Even though we just pushed the log above, we did not have
381 * the superblock buffer locked at that point so it can
382 * become pinned in between there and here.
384 if (XFS_BUF_ISPINNED(bp))
385 xfs_log_force(mp, 0);
389 if (flags & SYNC_WAIT)
394 error = xfs_bwrite(mp, bp);
399 * If this is a data integrity sync make sure all pending buffers
400 * are flushed out for the log coverage check below.
402 if (flags & SYNC_WAIT)
403 xfs_flush_buftarg(mp->m_ddev_targp, 1);
405 if (xfs_log_need_covered(mp))
406 error = xfs_commit_dummy_trans(mp, flags);
416 * When remounting a filesystem read-only or freezing the filesystem, we have
417 * two phases to execute. This first phase is syncing the data before we
418 * quiesce the filesystem, and the second is flushing all the inodes out after
419 * we've waited for all the transactions created by the first phase to
420 * complete. The second phase ensures that the inodes are written to their
421 * location on disk rather than just existing in transactions in the log. This
422 * means after a quiesce there is no log replay required to write the inodes to
423 * disk (this is the main difference between a sync and a quiesce).
426 * First stage of freeze - no writers will make progress now we are here,
427 * so we flush delwri and delalloc buffers here, then wait for all I/O to
428 * complete. Data is frozen at that point. Metadata is not frozen,
429 * transactions can still occur here so don't bother flushing the buftarg
430 * because it'll just get dirty again.
434 struct xfs_mount *mp)
438 /* push non-blocking */
439 xfs_sync_data(mp, 0);
440 xfs_qm_sync(mp, SYNC_TRYLOCK);
442 /* push and block till complete */
443 xfs_sync_data(mp, SYNC_WAIT);
444 xfs_qm_sync(mp, SYNC_WAIT);
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_reclaim_inodes(mp, 0);
463 xfs_flush_buftarg(mp->m_ddev_targp, 0);
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. We also so sync
470 * reclaim of inodes to catch any that the above delwri flush skipped.
473 xfs_reclaim_inodes(mp, SYNC_WAIT);
474 xfs_sync_attr(mp, SYNC_WAIT);
475 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
484 * Second stage of a quiesce. The data is already synced, now we have to take
485 * care of the metadata. New transactions are already blocked, so we need to
486 * wait for any remaining transactions to drain out before proceding.
490 struct xfs_mount *mp)
494 /* wait for all modifications to complete */
495 while (atomic_read(&mp->m_active_trans) > 0)
498 /* flush inodes and push all remaining buffers out to disk */
502 * Just warn here till VFS can correctly support
503 * read-only remount without racing.
505 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
507 /* Push the superblock and write an unmount record */
508 error = xfs_log_sbcount(mp, 1);
510 xfs_fs_cmn_err(CE_WARN, mp,
511 "xfs_attr_quiesce: failed to log sb changes. "
512 "Frozen image may not be consistent.");
513 xfs_log_unmount_write(mp);
514 xfs_unmountfs_writesb(mp);
518 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
519 * Doing this has two advantages:
520 * - It saves on stack space, which is tight in certain situations
521 * - It can be used (with care) as a mechanism to avoid deadlocks.
522 * Flushing while allocating in a full filesystem requires both.
525 xfs_syncd_queue_work(
526 struct xfs_mount *mp,
528 void (*syncer)(struct xfs_mount *, void *),
529 struct completion *completion)
531 struct xfs_sync_work *work;
533 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
534 INIT_LIST_HEAD(&work->w_list);
535 work->w_syncer = syncer;
538 work->w_completion = completion;
539 spin_lock(&mp->m_sync_lock);
540 list_add_tail(&work->w_list, &mp->m_sync_list);
541 spin_unlock(&mp->m_sync_lock);
542 wake_up_process(mp->m_sync_task);
546 * Flush delayed allocate data, attempting to free up reserved space
547 * from existing allocations. At this point a new allocation attempt
548 * has failed with ENOSPC and we are in the process of scratching our
549 * heads, looking about for more room...
552 xfs_flush_inodes_work(
553 struct xfs_mount *mp,
556 struct inode *inode = arg;
557 xfs_sync_data(mp, SYNC_TRYLOCK);
558 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
566 struct inode *inode = VFS_I(ip);
567 DECLARE_COMPLETION_ONSTACK(completion);
570 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
571 wait_for_completion(&completion);
572 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
576 * Every sync period we need to unpin all items, reclaim inodes, sync
577 * quota and write out the superblock. We might need to cover the log
578 * to indicate it is idle.
582 struct xfs_mount *mp,
587 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
588 xfs_log_force(mp, 0);
589 xfs_reclaim_inodes(mp, 0);
590 /* dgc: errors ignored here */
591 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
592 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
595 wake_up(&mp->m_wait_single_sync_task);
602 struct xfs_mount *mp = arg;
604 xfs_sync_work_t *work, *n;
608 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
610 timeleft = schedule_timeout_interruptible(timeleft);
613 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
616 spin_lock(&mp->m_sync_lock);
618 * We can get woken by laptop mode, to do a sync -
619 * that's the (only!) case where the list would be
620 * empty with time remaining.
622 if (!timeleft || list_empty(&mp->m_sync_list)) {
624 timeleft = xfs_syncd_centisecs *
625 msecs_to_jiffies(10);
626 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
627 list_add_tail(&mp->m_sync_work.w_list,
630 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
631 list_move(&work->w_list, &tmp);
632 spin_unlock(&mp->m_sync_lock);
634 list_for_each_entry_safe(work, n, &tmp, w_list) {
635 (*work->w_syncer)(mp, work->w_data);
636 list_del(&work->w_list);
637 if (work == &mp->m_sync_work)
639 if (work->w_completion)
640 complete(work->w_completion);
650 struct xfs_mount *mp)
652 mp->m_sync_work.w_syncer = xfs_sync_worker;
653 mp->m_sync_work.w_mount = mp;
654 mp->m_sync_work.w_completion = NULL;
655 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
656 if (IS_ERR(mp->m_sync_task))
657 return -PTR_ERR(mp->m_sync_task);
663 struct xfs_mount *mp)
665 kthread_stop(mp->m_sync_task);
669 __xfs_inode_set_reclaim_tag(
670 struct xfs_perag *pag,
671 struct xfs_inode *ip)
673 radix_tree_tag_set(&pag->pag_ici_root,
674 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
675 XFS_ICI_RECLAIM_TAG);
679 * We set the inode flag atomically with the radix tree tag.
680 * Once we get tag lookups on the radix tree, this inode flag
684 xfs_inode_set_reclaim_tag(
687 struct xfs_mount *mp = ip->i_mount;
688 struct xfs_perag *pag;
690 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
691 read_lock(&pag->pag_ici_lock);
692 spin_lock(&ip->i_flags_lock);
693 __xfs_inode_set_reclaim_tag(pag, ip);
694 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
695 spin_unlock(&ip->i_flags_lock);
696 read_unlock(&pag->pag_ici_lock);
701 __xfs_inode_clear_reclaim_tag(
706 radix_tree_tag_clear(&pag->pag_ici_root,
707 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
711 * Inodes in different states need to be treated differently, and the return
712 * value of xfs_iflush is not sufficient to get this right. The following table
713 * lists the inode states and the reclaim actions necessary for non-blocking
717 * inode state iflush ret required action
718 * --------------- ---------- ---------------
720 * shutdown EIO unpin and reclaim
721 * clean, unpinned 0 reclaim
722 * stale, unpinned 0 reclaim
723 * clean, pinned(*) 0 requeue
724 * stale, pinned EAGAIN requeue
725 * dirty, delwri ok 0 requeue
726 * dirty, delwri blocked EAGAIN requeue
727 * dirty, sync flush 0 reclaim
729 * (*) dgc: I don't think the clean, pinned state is possible but it gets
730 * handled anyway given the order of checks implemented.
732 * As can be seen from the table, the return value of xfs_iflush() is not
733 * sufficient to correctly decide the reclaim action here. The checks in
734 * xfs_iflush() might look like duplicates, but they are not.
736 * Also, because we get the flush lock first, we know that any inode that has
737 * been flushed delwri has had the flush completed by the time we check that
738 * the inode is clean. The clean inode check needs to be done before flushing
739 * the inode delwri otherwise we would loop forever requeuing clean inodes as
740 * we cannot tell apart a successful delwri flush and a clean inode from the
741 * return value of xfs_iflush().
743 * Note that because the inode is flushed delayed write by background
744 * writeback, the flush lock may already be held here and waiting on it can
745 * result in very long latencies. Hence for sync reclaims, where we wait on the
746 * flush lock, the caller should push out delayed write inodes first before
747 * trying to reclaim them to minimise the amount of time spent waiting. For
748 * background relaim, we just requeue the inode for the next pass.
750 * Hence the order of actions after gaining the locks should be:
752 * shutdown => unpin and reclaim
753 * pinned, delwri => requeue
754 * pinned, sync => unpin
757 * dirty, delwri => flush and requeue
758 * dirty, sync => flush, wait and reclaim
762 struct xfs_inode *ip,
763 struct xfs_perag *pag,
769 * The radix tree lock here protects a thread in xfs_iget from racing
770 * with us starting reclaim on the inode. Once we have the
771 * XFS_IRECLAIM flag set it will not touch us.
773 spin_lock(&ip->i_flags_lock);
774 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
775 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
776 /* ignore as it is already under reclaim */
777 spin_unlock(&ip->i_flags_lock);
778 write_unlock(&pag->pag_ici_lock);
781 __xfs_iflags_set(ip, XFS_IRECLAIM);
782 spin_unlock(&ip->i_flags_lock);
783 write_unlock(&pag->pag_ici_lock);
785 xfs_ilock(ip, XFS_ILOCK_EXCL);
786 if (!xfs_iflock_nowait(ip)) {
787 if (!(sync_mode & SYNC_WAIT))
792 if (is_bad_inode(VFS_I(ip)))
794 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
798 if (xfs_ipincount(ip)) {
799 if (!(sync_mode & SYNC_WAIT)) {
805 if (xfs_iflags_test(ip, XFS_ISTALE))
807 if (xfs_inode_clean(ip))
810 /* Now we have an inode that needs flushing */
811 error = xfs_iflush(ip, sync_mode);
812 if (sync_mode & SYNC_WAIT) {
818 * When we have to flush an inode but don't have SYNC_WAIT set, we
819 * flush the inode out using a delwri buffer and wait for the next
820 * call into reclaim to find it in a clean state instead of waiting for
821 * it now. We also don't return errors here - if the error is transient
822 * then the next reclaim pass will flush the inode, and if the error
823 * is permanent then the next sync reclaim will relcaim the inode and
826 if (error && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
827 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
828 "inode 0x%llx background reclaim flush failed with %d",
829 (long long)ip->i_ino, error);
832 xfs_iflags_clear(ip, XFS_IRECLAIM);
833 xfs_iunlock(ip, XFS_ILOCK_EXCL);
835 * We could return EAGAIN here to make reclaim rescan the inode tree in
836 * a short while. However, this just burns CPU time scanning the tree
837 * waiting for IO to complete and xfssyncd never goes back to the idle
838 * state. Instead, return 0 to let the next scheduled background reclaim
839 * attempt to reclaim the inode again.
845 xfs_iunlock(ip, XFS_ILOCK_EXCL);
856 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
857 XFS_ICI_RECLAIM_TAG, 1);