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),
101 uint32_t first_index;
113 write_lock(&pag->pag_ici_lock);
115 read_lock(&pag->pag_ici_lock);
116 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
119 write_unlock(&pag->pag_ici_lock);
121 read_unlock(&pag->pag_ici_lock);
125 /* execute releases pag->pag_ici_lock */
126 error = execute(ip, pag, flags);
127 if (error == EAGAIN) {
134 /* bail out if the filesystem is corrupted. */
135 if (error == EFSCORRUPTED)
138 } while ((*nr_to_scan)--);
148 xfs_inode_ag_iterator(
149 struct xfs_mount *mp,
150 int (*execute)(struct xfs_inode *ip,
151 struct xfs_perag *pag, int flags),
162 nr = nr_to_scan ? *nr_to_scan : INT_MAX;
163 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
164 struct xfs_perag *pag;
166 pag = xfs_perag_get(mp, ag);
167 if (!pag->pag_ici_init) {
171 error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
176 if (error == EFSCORRUPTED)
184 return XFS_ERROR(last_error);
187 /* must be called with pag_ici_lock held and releases it */
189 xfs_sync_inode_valid(
190 struct xfs_inode *ip,
191 struct xfs_perag *pag)
193 struct inode *inode = VFS_I(ip);
194 int error = EFSCORRUPTED;
196 /* nothing to sync during shutdown */
197 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
200 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
202 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
205 /* If we can't grab the inode, it must on it's way to reclaim. */
209 if (is_bad_inode(inode)) {
217 read_unlock(&pag->pag_ici_lock);
223 struct xfs_inode *ip,
224 struct xfs_perag *pag,
227 struct inode *inode = VFS_I(ip);
228 struct address_space *mapping = inode->i_mapping;
231 error = xfs_sync_inode_valid(ip, pag);
235 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
238 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
239 if (flags & SYNC_TRYLOCK)
241 xfs_ilock(ip, XFS_IOLOCK_SHARED);
244 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
245 0 : XBF_ASYNC, FI_NONE);
246 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
249 if (flags & SYNC_WAIT)
257 struct xfs_inode *ip,
258 struct xfs_perag *pag,
263 error = xfs_sync_inode_valid(ip, pag);
267 xfs_ilock(ip, XFS_ILOCK_SHARED);
268 if (xfs_inode_clean(ip))
270 if (!xfs_iflock_nowait(ip)) {
271 if (!(flags & SYNC_WAIT))
276 if (xfs_inode_clean(ip)) {
281 error = xfs_iflush(ip, flags);
284 xfs_iunlock(ip, XFS_ILOCK_SHARED);
290 * Write out pagecache data for the whole filesystem.
294 struct xfs_mount *mp,
299 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
301 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
302 XFS_ICI_NO_TAG, 0, NULL);
304 return XFS_ERROR(error);
306 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
311 * Write out inode metadata (attributes) for the whole filesystem.
315 struct xfs_mount *mp,
318 ASSERT((flags & ~SYNC_WAIT) == 0);
320 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
321 XFS_ICI_NO_TAG, 0, NULL);
325 xfs_commit_dummy_trans(
326 struct xfs_mount *mp,
329 struct xfs_inode *ip = mp->m_rootip;
330 struct xfs_trans *tp;
334 * Put a dummy transaction in the log to tell recovery
335 * that all others are OK.
337 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
338 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
340 xfs_trans_cancel(tp, 0);
344 xfs_ilock(ip, XFS_ILOCK_EXCL);
346 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
347 xfs_trans_ihold(tp, ip);
348 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
349 error = xfs_trans_commit(tp, 0);
350 xfs_iunlock(ip, XFS_ILOCK_EXCL);
352 /* the log force ensures this transaction is pushed to disk */
353 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
359 struct xfs_mount *mp)
364 * If the buffer is pinned then push on the log so we won't get stuck
365 * waiting in the write for someone, maybe ourselves, to flush the log.
367 * Even though we just pushed the log above, we did not have the
368 * superblock buffer locked at that point so it can become pinned in
369 * between there and here.
371 bp = xfs_getsb(mp, 0);
372 if (XFS_BUF_ISPINNED(bp))
373 xfs_log_force(mp, 0);
376 return xfs_bwrite(mp, bp);
380 * When remounting a filesystem read-only or freezing the filesystem, we have
381 * two phases to execute. This first phase is syncing the data before we
382 * quiesce the filesystem, and the second is flushing all the inodes out after
383 * we've waited for all the transactions created by the first phase to
384 * complete. The second phase ensures that the inodes are written to their
385 * location on disk rather than just existing in transactions in the log. This
386 * means after a quiesce there is no log replay required to write the inodes to
387 * disk (this is the main difference between a sync and a quiesce).
390 * First stage of freeze - no writers will make progress now we are here,
391 * so we flush delwri and delalloc buffers here, then wait for all I/O to
392 * complete. Data is frozen at that point. Metadata is not frozen,
393 * transactions can still occur here so don't bother flushing the buftarg
394 * because it'll just get dirty again.
398 struct xfs_mount *mp)
400 int error, error2 = 0;
402 /* push non-blocking */
403 xfs_sync_data(mp, 0);
404 xfs_qm_sync(mp, SYNC_TRYLOCK);
406 /* push and block till complete */
407 xfs_sync_data(mp, SYNC_WAIT);
408 xfs_qm_sync(mp, SYNC_WAIT);
410 /* write superblock and hoover up shutdown errors */
411 error = xfs_sync_fsdata(mp);
413 /* make sure all delwri buffers are written out */
414 xfs_flush_buftarg(mp->m_ddev_targp, 1);
416 /* mark the log as covered if needed */
417 if (xfs_log_need_covered(mp))
418 error2 = xfs_commit_dummy_trans(mp, SYNC_WAIT);
420 /* flush data-only devices */
421 if (mp->m_rtdev_targp)
422 XFS_bflush(mp->m_rtdev_targp);
424 return error ? error : error2;
429 struct xfs_mount *mp)
431 int count = 0, pincount;
433 xfs_reclaim_inodes(mp, 0);
434 xfs_flush_buftarg(mp->m_ddev_targp, 0);
437 * This loop must run at least twice. The first instance of the loop
438 * will flush most meta data but that will generate more meta data
439 * (typically directory updates). Which then must be flushed and
440 * logged before we can write the unmount record. We also so sync
441 * reclaim of inodes to catch any that the above delwri flush skipped.
444 xfs_reclaim_inodes(mp, SYNC_WAIT);
445 xfs_sync_attr(mp, SYNC_WAIT);
446 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
455 * Second stage of a quiesce. The data is already synced, now we have to take
456 * care of the metadata. New transactions are already blocked, so we need to
457 * wait for any remaining transactions to drain out before proceding.
461 struct xfs_mount *mp)
465 /* wait for all modifications to complete */
466 while (atomic_read(&mp->m_active_trans) > 0)
469 /* flush inodes and push all remaining buffers out to disk */
473 * Just warn here till VFS can correctly support
474 * read-only remount without racing.
476 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
478 /* Push the superblock and write an unmount record */
479 error = xfs_log_sbcount(mp, 1);
481 xfs_fs_cmn_err(CE_WARN, mp,
482 "xfs_attr_quiesce: failed to log sb changes. "
483 "Frozen image may not be consistent.");
484 xfs_log_unmount_write(mp);
485 xfs_unmountfs_writesb(mp);
489 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
490 * Doing this has two advantages:
491 * - It saves on stack space, which is tight in certain situations
492 * - It can be used (with care) as a mechanism to avoid deadlocks.
493 * Flushing while allocating in a full filesystem requires both.
496 xfs_syncd_queue_work(
497 struct xfs_mount *mp,
499 void (*syncer)(struct xfs_mount *, void *),
500 struct completion *completion)
502 struct xfs_sync_work *work;
504 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
505 INIT_LIST_HEAD(&work->w_list);
506 work->w_syncer = syncer;
509 work->w_completion = completion;
510 spin_lock(&mp->m_sync_lock);
511 list_add_tail(&work->w_list, &mp->m_sync_list);
512 spin_unlock(&mp->m_sync_lock);
513 wake_up_process(mp->m_sync_task);
517 * Flush delayed allocate data, attempting to free up reserved space
518 * from existing allocations. At this point a new allocation attempt
519 * has failed with ENOSPC and we are in the process of scratching our
520 * heads, looking about for more room...
523 xfs_flush_inodes_work(
524 struct xfs_mount *mp,
527 struct inode *inode = arg;
528 xfs_sync_data(mp, SYNC_TRYLOCK);
529 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
537 struct inode *inode = VFS_I(ip);
538 DECLARE_COMPLETION_ONSTACK(completion);
541 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
542 wait_for_completion(&completion);
543 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
547 * Every sync period we need to unpin all items, reclaim inodes and sync
548 * disk quotas. We might need to cover the log to indicate that the
549 * filesystem is idle.
553 struct xfs_mount *mp,
558 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
559 xfs_log_force(mp, 0);
560 xfs_reclaim_inodes(mp, 0);
561 /* dgc: errors ignored here */
562 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
563 if (xfs_log_need_covered(mp))
564 error = xfs_commit_dummy_trans(mp, 0);
567 wake_up(&mp->m_wait_single_sync_task);
574 struct xfs_mount *mp = arg;
576 xfs_sync_work_t *work, *n;
580 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
582 if (list_empty(&mp->m_sync_list))
583 timeleft = schedule_timeout_interruptible(timeleft);
586 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
589 spin_lock(&mp->m_sync_lock);
591 * We can get woken by laptop mode, to do a sync -
592 * that's the (only!) case where the list would be
593 * empty with time remaining.
595 if (!timeleft || list_empty(&mp->m_sync_list)) {
597 timeleft = xfs_syncd_centisecs *
598 msecs_to_jiffies(10);
599 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
600 list_add_tail(&mp->m_sync_work.w_list,
603 list_splice_init(&mp->m_sync_list, &tmp);
604 spin_unlock(&mp->m_sync_lock);
606 list_for_each_entry_safe(work, n, &tmp, w_list) {
607 (*work->w_syncer)(mp, work->w_data);
608 list_del(&work->w_list);
609 if (work == &mp->m_sync_work)
611 if (work->w_completion)
612 complete(work->w_completion);
622 struct xfs_mount *mp)
624 mp->m_sync_work.w_syncer = xfs_sync_worker;
625 mp->m_sync_work.w_mount = mp;
626 mp->m_sync_work.w_completion = NULL;
627 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
628 if (IS_ERR(mp->m_sync_task))
629 return -PTR_ERR(mp->m_sync_task);
635 struct xfs_mount *mp)
637 kthread_stop(mp->m_sync_task);
641 __xfs_inode_set_reclaim_tag(
642 struct xfs_perag *pag,
643 struct xfs_inode *ip)
645 radix_tree_tag_set(&pag->pag_ici_root,
646 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
647 XFS_ICI_RECLAIM_TAG);
648 pag->pag_ici_reclaimable++;
652 * We set the inode flag atomically with the radix tree tag.
653 * Once we get tag lookups on the radix tree, this inode flag
657 xfs_inode_set_reclaim_tag(
660 struct xfs_mount *mp = ip->i_mount;
661 struct xfs_perag *pag;
663 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
664 write_lock(&pag->pag_ici_lock);
665 spin_lock(&ip->i_flags_lock);
666 __xfs_inode_set_reclaim_tag(pag, ip);
667 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
668 spin_unlock(&ip->i_flags_lock);
669 write_unlock(&pag->pag_ici_lock);
674 __xfs_inode_clear_reclaim_tag(
679 radix_tree_tag_clear(&pag->pag_ici_root,
680 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
681 pag->pag_ici_reclaimable--;
685 * Inodes in different states need to be treated differently, and the return
686 * value of xfs_iflush is not sufficient to get this right. The following table
687 * lists the inode states and the reclaim actions necessary for non-blocking
691 * inode state iflush ret required action
692 * --------------- ---------- ---------------
694 * shutdown EIO unpin and reclaim
695 * clean, unpinned 0 reclaim
696 * stale, unpinned 0 reclaim
697 * clean, pinned(*) 0 requeue
698 * stale, pinned EAGAIN requeue
699 * dirty, delwri ok 0 requeue
700 * dirty, delwri blocked EAGAIN requeue
701 * dirty, sync flush 0 reclaim
703 * (*) dgc: I don't think the clean, pinned state is possible but it gets
704 * handled anyway given the order of checks implemented.
706 * As can be seen from the table, the return value of xfs_iflush() is not
707 * sufficient to correctly decide the reclaim action here. The checks in
708 * xfs_iflush() might look like duplicates, but they are not.
710 * Also, because we get the flush lock first, we know that any inode that has
711 * been flushed delwri has had the flush completed by the time we check that
712 * the inode is clean. The clean inode check needs to be done before flushing
713 * the inode delwri otherwise we would loop forever requeuing clean inodes as
714 * we cannot tell apart a successful delwri flush and a clean inode from the
715 * return value of xfs_iflush().
717 * Note that because the inode is flushed delayed write by background
718 * writeback, the flush lock may already be held here and waiting on it can
719 * result in very long latencies. Hence for sync reclaims, where we wait on the
720 * flush lock, the caller should push out delayed write inodes first before
721 * trying to reclaim them to minimise the amount of time spent waiting. For
722 * background relaim, we just requeue the inode for the next pass.
724 * Hence the order of actions after gaining the locks should be:
726 * shutdown => unpin and reclaim
727 * pinned, delwri => requeue
728 * pinned, sync => unpin
731 * dirty, delwri => flush and requeue
732 * dirty, sync => flush, wait and reclaim
736 struct xfs_inode *ip,
737 struct xfs_perag *pag,
743 * The radix tree lock here protects a thread in xfs_iget from racing
744 * with us starting reclaim on the inode. Once we have the
745 * XFS_IRECLAIM flag set it will not touch us.
747 spin_lock(&ip->i_flags_lock);
748 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
749 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
750 /* ignore as it is already under reclaim */
751 spin_unlock(&ip->i_flags_lock);
752 write_unlock(&pag->pag_ici_lock);
755 __xfs_iflags_set(ip, XFS_IRECLAIM);
756 spin_unlock(&ip->i_flags_lock);
757 write_unlock(&pag->pag_ici_lock);
759 xfs_ilock(ip, XFS_ILOCK_EXCL);
760 if (!xfs_iflock_nowait(ip)) {
761 if (!(sync_mode & SYNC_WAIT))
766 if (is_bad_inode(VFS_I(ip)))
768 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
772 if (xfs_ipincount(ip)) {
773 if (!(sync_mode & SYNC_WAIT)) {
779 if (xfs_iflags_test(ip, XFS_ISTALE))
781 if (xfs_inode_clean(ip))
784 /* Now we have an inode that needs flushing */
785 error = xfs_iflush(ip, sync_mode);
786 if (sync_mode & SYNC_WAIT) {
792 * When we have to flush an inode but don't have SYNC_WAIT set, we
793 * flush the inode out using a delwri buffer and wait for the next
794 * call into reclaim to find it in a clean state instead of waiting for
795 * it now. We also don't return errors here - if the error is transient
796 * then the next reclaim pass will flush the inode, and if the error
797 * is permanent then the next sync reclaim will reclaim the inode and
800 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
801 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
802 "inode 0x%llx background reclaim flush failed with %d",
803 (long long)ip->i_ino, error);
806 xfs_iflags_clear(ip, XFS_IRECLAIM);
807 xfs_iunlock(ip, XFS_ILOCK_EXCL);
809 * We could return EAGAIN here to make reclaim rescan the inode tree in
810 * a short while. However, this just burns CPU time scanning the tree
811 * waiting for IO to complete and xfssyncd never goes back to the idle
812 * state. Instead, return 0 to let the next scheduled background reclaim
813 * attempt to reclaim the inode again.
819 xfs_iunlock(ip, XFS_ILOCK_EXCL);
830 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
831 XFS_ICI_RECLAIM_TAG, 1, NULL);
835 * Shrinker infrastructure.
837 * This is all far more complex than it needs to be. It adds a global list of
838 * mounts because the shrinkers can only call a global context. We need to make
839 * the shrinkers pass a context to avoid the need for global state.
841 static LIST_HEAD(xfs_mount_list);
842 static struct rw_semaphore xfs_mount_list_lock;
845 xfs_reclaim_inode_shrink(
849 struct xfs_mount *mp;
850 struct xfs_perag *pag;
855 if (!(gfp_mask & __GFP_FS))
858 down_read(&xfs_mount_list_lock);
859 list_for_each_entry(mp, &xfs_mount_list, m_mplist) {
860 xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
861 XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
865 up_read(&xfs_mount_list_lock);
868 down_read(&xfs_mount_list_lock);
869 list_for_each_entry(mp, &xfs_mount_list, m_mplist) {
870 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
872 pag = xfs_perag_get(mp, ag);
873 if (!pag->pag_ici_init) {
877 reclaimable += pag->pag_ici_reclaimable;
881 up_read(&xfs_mount_list_lock);
885 static struct shrinker xfs_inode_shrinker = {
886 .shrink = xfs_reclaim_inode_shrink,
887 .seeks = DEFAULT_SEEKS,
891 xfs_inode_shrinker_init(void)
893 init_rwsem(&xfs_mount_list_lock);
894 register_shrinker(&xfs_inode_shrinker);
898 xfs_inode_shrinker_destroy(void)
900 ASSERT(list_empty(&xfs_mount_list));
901 unregister_shrinker(&xfs_inode_shrinker);
905 xfs_inode_shrinker_register(
906 struct xfs_mount *mp)
908 down_write(&xfs_mount_list_lock);
909 list_add_tail(&mp->m_mplist, &xfs_mount_list);
910 up_write(&xfs_mount_list_lock);
914 xfs_inode_shrinker_unregister(
915 struct xfs_mount *mp)
917 down_write(&xfs_mount_list_lock);
918 list_del(&mp->m_mplist);
919 up_write(&xfs_mount_list_lock);