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 if (tag
== XFS_ICI_NO_TAG
) {
68 nr_found
= radix_tree_gang_lookup(&pag
->pag_ici_root
,
69 (void **)&ip
, *first_index
, 1);
71 nr_found
= radix_tree_gang_lookup_tag(&pag
->pag_ici_root
,
72 (void **)&ip
, *first_index
, 1, tag
);
78 * Update the index for the next lookup. Catch overflows
79 * into the next AG range which can occur if we have inodes
80 * in the last block of the AG and we are currently
81 * pointing to the last inode.
83 *first_index
= XFS_INO_TO_AGINO(mp
, ip
->i_ino
+ 1);
84 if (*first_index
< XFS_INO_TO_AGINO(mp
, ip
->i_ino
))
93 int (*execute
)(struct xfs_inode
*ip
,
94 struct xfs_perag
*pag
, int flags
),
100 struct xfs_perag
*pag
= &mp
->m_perag
[ag
];
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
)--);
145 xfs_put_perag(mp
, pag
);
150 xfs_inode_ag_iterator(
151 struct xfs_mount
*mp
,
152 int (*execute
)(struct xfs_inode
*ip
,
153 struct xfs_perag
*pag
, int flags
),
164 nr
= nr_to_scan
? *nr_to_scan
: INT_MAX
;
165 for (ag
= 0; ag
< mp
->m_sb
.sb_agcount
; ag
++) {
166 if (!mp
->m_perag
[ag
].pag_ici_init
)
168 error
= xfs_inode_ag_walk(mp
, ag
, execute
, flags
, tag
,
172 if (error
== EFSCORRUPTED
)
180 return XFS_ERROR(last_error
);
183 /* must be called with pag_ici_lock held and releases it */
185 xfs_sync_inode_valid(
186 struct xfs_inode
*ip
,
187 struct xfs_perag
*pag
)
189 struct inode
*inode
= VFS_I(ip
);
190 int error
= EFSCORRUPTED
;
192 /* nothing to sync during shutdown */
193 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
196 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
198 if (xfs_iflags_test(ip
, XFS_INEW
| XFS_IRECLAIMABLE
| XFS_IRECLAIM
))
201 /* If we can't grab the inode, it must on it's way to reclaim. */
205 if (is_bad_inode(inode
)) {
213 read_unlock(&pag
->pag_ici_lock
);
219 struct xfs_inode
*ip
,
220 struct xfs_perag
*pag
,
223 struct inode
*inode
= VFS_I(ip
);
224 struct address_space
*mapping
= inode
->i_mapping
;
227 error
= xfs_sync_inode_valid(ip
, pag
);
231 if (!mapping_tagged(mapping
, PAGECACHE_TAG_DIRTY
))
234 if (!xfs_ilock_nowait(ip
, XFS_IOLOCK_SHARED
)) {
235 if (flags
& SYNC_TRYLOCK
)
237 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
240 error
= xfs_flush_pages(ip
, 0, -1, (flags
& SYNC_WAIT
) ?
241 0 : XFS_B_ASYNC
, FI_NONE
);
242 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
245 if (flags
& SYNC_WAIT
)
253 struct xfs_inode
*ip
,
254 struct xfs_perag
*pag
,
259 error
= xfs_sync_inode_valid(ip
, pag
);
263 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
264 if (xfs_inode_clean(ip
))
266 if (!xfs_iflock_nowait(ip
)) {
267 if (!(flags
& SYNC_WAIT
))
272 if (xfs_inode_clean(ip
)) {
277 error
= xfs_iflush(ip
, (flags
& SYNC_WAIT
) ?
278 XFS_IFLUSH_SYNC
: XFS_IFLUSH_DELWRI
);
281 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
287 * Write out pagecache data for the whole filesystem.
291 struct xfs_mount
*mp
,
296 ASSERT((flags
& ~(SYNC_TRYLOCK
|SYNC_WAIT
)) == 0);
298 error
= xfs_inode_ag_iterator(mp
, xfs_sync_inode_data
, flags
,
299 XFS_ICI_NO_TAG
, 0, NULL
);
301 return XFS_ERROR(error
);
304 (flags
& SYNC_WAIT
) ?
305 XFS_LOG_FORCE
| XFS_LOG_SYNC
:
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
;
332 int log_flags
= XFS_LOG_FORCE
;
334 if (flags
& SYNC_WAIT
)
335 log_flags
|= XFS_LOG_SYNC
;
338 * Put a dummy transaction in the log to tell recovery
339 * that all others are OK.
341 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DUMMY1
);
342 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES(mp
), 0, 0, 0);
344 xfs_trans_cancel(tp
, 0);
348 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
350 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
351 xfs_trans_ihold(tp
, ip
);
352 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
353 error
= xfs_trans_commit(tp
, 0);
354 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
356 /* the log force ensures this transaction is pushed to disk */
357 xfs_log_force(mp
, 0, log_flags
);
363 struct xfs_mount
*mp
,
367 struct xfs_buf_log_item
*bip
;
371 * If this is xfssyncd() then only sync the superblock if we can
372 * lock it without sleeping and it is not pinned.
374 if (flags
& SYNC_TRYLOCK
) {
375 ASSERT(!(flags
& SYNC_WAIT
));
377 bp
= xfs_getsb(mp
, XFS_BUF_TRYLOCK
);
381 bip
= XFS_BUF_FSPRIVATE(bp
, struct xfs_buf_log_item
*);
382 if (!bip
|| !xfs_buf_item_dirty(bip
) || XFS_BUF_ISPINNED(bp
))
385 bp
= xfs_getsb(mp
, 0);
388 * If the buffer is pinned then push on the log so we won't
389 * get stuck waiting in the write for someone, maybe
390 * ourselves, to flush the log.
392 * Even though we just pushed the log above, we did not have
393 * the superblock buffer locked at that point so it can
394 * become pinned in between there and here.
396 if (XFS_BUF_ISPINNED(bp
))
397 xfs_log_force(mp
, 0, XFS_LOG_FORCE
);
401 if (flags
& SYNC_WAIT
)
406 error
= xfs_bwrite(mp
, bp
);
411 * If this is a data integrity sync make sure all pending buffers
412 * are flushed out for the log coverage check below.
414 if (flags
& SYNC_WAIT
)
415 xfs_flush_buftarg(mp
->m_ddev_targp
, 1);
417 if (xfs_log_need_covered(mp
))
418 error
= xfs_commit_dummy_trans(mp
, flags
);
428 * When remounting a filesystem read-only or freezing the filesystem, we have
429 * two phases to execute. This first phase is syncing the data before we
430 * quiesce the filesystem, and the second is flushing all the inodes out after
431 * we've waited for all the transactions created by the first phase to
432 * complete. The second phase ensures that the inodes are written to their
433 * location on disk rather than just existing in transactions in the log. This
434 * means after a quiesce there is no log replay required to write the inodes to
435 * disk (this is the main difference between a sync and a quiesce).
438 * First stage of freeze - no writers will make progress now we are here,
439 * so we flush delwri and delalloc buffers here, then wait for all I/O to
440 * complete. Data is frozen at that point. Metadata is not frozen,
441 * transactions can still occur here so don't bother flushing the buftarg
442 * because it'll just get dirty again.
446 struct xfs_mount
*mp
)
450 /* push non-blocking */
451 xfs_sync_data(mp
, 0);
452 xfs_qm_sync(mp
, SYNC_TRYLOCK
);
454 /* push and block till complete */
455 xfs_sync_data(mp
, SYNC_WAIT
);
456 xfs_qm_sync(mp
, SYNC_WAIT
);
458 /* drop inode references pinned by filestreams */
459 xfs_filestream_flush(mp
);
461 /* write superblock and hoover up shutdown errors */
462 error
= xfs_sync_fsdata(mp
, SYNC_WAIT
);
464 /* flush data-only devices */
465 if (mp
->m_rtdev_targp
)
466 XFS_bflush(mp
->m_rtdev_targp
);
473 struct xfs_mount
*mp
)
475 int count
= 0, pincount
;
477 xfs_flush_buftarg(mp
->m_ddev_targp
, 0);
478 xfs_reclaim_inodes(mp
, XFS_IFLUSH_DELWRI_ELSE_ASYNC
);
481 * This loop must run at least twice. The first instance of the loop
482 * will flush most meta data but that will generate more meta data
483 * (typically directory updates). Which then must be flushed and
484 * logged before we can write the unmount record.
487 xfs_sync_attr(mp
, SYNC_WAIT
);
488 pincount
= xfs_flush_buftarg(mp
->m_ddev_targp
, 1);
497 * Second stage of a quiesce. The data is already synced, now we have to take
498 * care of the metadata. New transactions are already blocked, so we need to
499 * wait for any remaining transactions to drain out before proceding.
503 struct xfs_mount
*mp
)
507 /* wait for all modifications to complete */
508 while (atomic_read(&mp
->m_active_trans
) > 0)
511 /* flush inodes and push all remaining buffers out to disk */
515 * Just warn here till VFS can correctly support
516 * read-only remount without racing.
518 WARN_ON(atomic_read(&mp
->m_active_trans
) != 0);
520 /* Push the superblock and write an unmount record */
521 error
= xfs_log_sbcount(mp
, 1);
523 xfs_fs_cmn_err(CE_WARN
, mp
,
524 "xfs_attr_quiesce: failed to log sb changes. "
525 "Frozen image may not be consistent.");
526 xfs_log_unmount_write(mp
);
527 xfs_unmountfs_writesb(mp
);
531 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
532 * Doing this has two advantages:
533 * - It saves on stack space, which is tight in certain situations
534 * - It can be used (with care) as a mechanism to avoid deadlocks.
535 * Flushing while allocating in a full filesystem requires both.
538 xfs_syncd_queue_work(
539 struct xfs_mount
*mp
,
541 void (*syncer
)(struct xfs_mount
*, void *),
542 struct completion
*completion
)
544 struct xfs_sync_work
*work
;
546 work
= kmem_alloc(sizeof(struct xfs_sync_work
), KM_SLEEP
);
547 INIT_LIST_HEAD(&work
->w_list
);
548 work
->w_syncer
= syncer
;
551 work
->w_completion
= completion
;
552 spin_lock(&mp
->m_sync_lock
);
553 list_add_tail(&work
->w_list
, &mp
->m_sync_list
);
554 spin_unlock(&mp
->m_sync_lock
);
555 wake_up_process(mp
->m_sync_task
);
559 * Flush delayed allocate data, attempting to free up reserved space
560 * from existing allocations. At this point a new allocation attempt
561 * has failed with ENOSPC and we are in the process of scratching our
562 * heads, looking about for more room...
565 xfs_flush_inodes_work(
566 struct xfs_mount
*mp
,
569 struct inode
*inode
= arg
;
570 xfs_sync_data(mp
, SYNC_TRYLOCK
);
571 xfs_sync_data(mp
, SYNC_TRYLOCK
| SYNC_WAIT
);
579 struct inode
*inode
= VFS_I(ip
);
580 DECLARE_COMPLETION_ONSTACK(completion
);
583 xfs_syncd_queue_work(ip
->i_mount
, inode
, xfs_flush_inodes_work
, &completion
);
584 wait_for_completion(&completion
);
585 xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0, XFS_LOG_FORCE
|XFS_LOG_SYNC
);
589 * Every sync period we need to unpin all items, reclaim inodes, sync
590 * quota and write out the superblock. We might need to cover the log
591 * to indicate it is idle.
595 struct xfs_mount
*mp
,
600 if (!(mp
->m_flags
& XFS_MOUNT_RDONLY
)) {
601 xfs_log_force(mp
, (xfs_lsn_t
)0, XFS_LOG_FORCE
);
602 xfs_reclaim_inodes(mp
, XFS_IFLUSH_DELWRI_ELSE_ASYNC
);
603 /* dgc: errors ignored here */
604 error
= xfs_qm_sync(mp
, SYNC_TRYLOCK
);
605 error
= xfs_sync_fsdata(mp
, SYNC_TRYLOCK
);
608 wake_up(&mp
->m_wait_single_sync_task
);
615 struct xfs_mount
*mp
= arg
;
617 xfs_sync_work_t
*work
, *n
;
621 timeleft
= xfs_syncd_centisecs
* msecs_to_jiffies(10);
623 timeleft
= schedule_timeout_interruptible(timeleft
);
626 if (kthread_should_stop() && list_empty(&mp
->m_sync_list
))
629 spin_lock(&mp
->m_sync_lock
);
631 * We can get woken by laptop mode, to do a sync -
632 * that's the (only!) case where the list would be
633 * empty with time remaining.
635 if (!timeleft
|| list_empty(&mp
->m_sync_list
)) {
637 timeleft
= xfs_syncd_centisecs
*
638 msecs_to_jiffies(10);
639 INIT_LIST_HEAD(&mp
->m_sync_work
.w_list
);
640 list_add_tail(&mp
->m_sync_work
.w_list
,
643 list_for_each_entry_safe(work
, n
, &mp
->m_sync_list
, w_list
)
644 list_move(&work
->w_list
, &tmp
);
645 spin_unlock(&mp
->m_sync_lock
);
647 list_for_each_entry_safe(work
, n
, &tmp
, w_list
) {
648 (*work
->w_syncer
)(mp
, work
->w_data
);
649 list_del(&work
->w_list
);
650 if (work
== &mp
->m_sync_work
)
652 if (work
->w_completion
)
653 complete(work
->w_completion
);
663 struct xfs_mount
*mp
)
665 mp
->m_sync_work
.w_syncer
= xfs_sync_worker
;
666 mp
->m_sync_work
.w_mount
= mp
;
667 mp
->m_sync_work
.w_completion
= NULL
;
668 mp
->m_sync_task
= kthread_run(xfssyncd
, mp
, "xfssyncd");
669 if (IS_ERR(mp
->m_sync_task
))
670 return -PTR_ERR(mp
->m_sync_task
);
676 struct xfs_mount
*mp
)
678 kthread_stop(mp
->m_sync_task
);
682 __xfs_inode_set_reclaim_tag(
683 struct xfs_perag
*pag
,
684 struct xfs_inode
*ip
)
686 radix_tree_tag_set(&pag
->pag_ici_root
,
687 XFS_INO_TO_AGINO(ip
->i_mount
, ip
->i_ino
),
688 XFS_ICI_RECLAIM_TAG
);
689 pag
->pag_ici_reclaimable
++;
693 * We set the inode flag atomically with the radix tree tag.
694 * Once we get tag lookups on the radix tree, this inode flag
698 xfs_inode_set_reclaim_tag(
701 xfs_mount_t
*mp
= ip
->i_mount
;
702 xfs_perag_t
*pag
= xfs_get_perag(mp
, ip
->i_ino
);
704 write_lock(&pag
->pag_ici_lock
);
705 spin_lock(&ip
->i_flags_lock
);
706 __xfs_inode_set_reclaim_tag(pag
, ip
);
707 __xfs_iflags_set(ip
, XFS_IRECLAIMABLE
);
708 spin_unlock(&ip
->i_flags_lock
);
709 write_unlock(&pag
->pag_ici_lock
);
710 xfs_put_perag(mp
, pag
);
714 __xfs_inode_clear_reclaim(
718 pag
->pag_ici_reclaimable
--;
722 __xfs_inode_clear_reclaim_tag(
727 radix_tree_tag_clear(&pag
->pag_ici_root
,
728 XFS_INO_TO_AGINO(mp
, ip
->i_ino
), XFS_ICI_RECLAIM_TAG
);
729 __xfs_inode_clear_reclaim(pag
, ip
);
734 struct xfs_inode
*ip
,
735 struct xfs_perag
*pag
,
739 * The radix tree lock here protects a thread in xfs_iget from racing
740 * with us starting reclaim on the inode. Once we have the
741 * XFS_IRECLAIM flag set it will not touch us.
743 spin_lock(&ip
->i_flags_lock
);
744 ASSERT_ALWAYS(__xfs_iflags_test(ip
, XFS_IRECLAIMABLE
));
745 if (__xfs_iflags_test(ip
, XFS_IRECLAIM
)) {
746 /* ignore as it is already under reclaim */
747 spin_unlock(&ip
->i_flags_lock
);
748 write_unlock(&pag
->pag_ici_lock
);
751 __xfs_iflags_set(ip
, XFS_IRECLAIM
);
752 spin_unlock(&ip
->i_flags_lock
);
753 write_unlock(&pag
->pag_ici_lock
);
756 * If the inode is still dirty, then flush it out. If the inode
757 * is not in the AIL, then it will be OK to flush it delwri as
758 * long as xfs_iflush() does not keep any references to the inode.
759 * We leave that decision up to xfs_iflush() since it has the
760 * knowledge of whether it's OK to simply do a delwri flush of
761 * the inode or whether we need to wait until the inode is
762 * pulled from the AIL.
763 * We get the flush lock regardless, though, just to make sure
764 * we don't free it while it is being flushed.
766 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
770 * In the case of a forced shutdown we rely on xfs_iflush() to
771 * wait for the inode to be unpinned before returning an error.
773 if (!is_bad_inode(VFS_I(ip
)) && xfs_iflush(ip
, sync_mode
) == 0) {
774 /* synchronize with xfs_iflush_done */
779 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
789 return xfs_inode_ag_iterator(mp
, xfs_reclaim_inode
, mode
,
790 XFS_ICI_RECLAIM_TAG
, 1, NULL
);
794 * Shrinker infrastructure.
796 * This is all far more complex than it needs to be. It adds a global list of
797 * mounts because the shrinkers can only call a global context. We need to make
798 * the shrinkers pass a context to avoid the need for global state.
800 static LIST_HEAD(xfs_mount_list
);
801 static struct rw_semaphore xfs_mount_list_lock
;
804 xfs_reclaim_inode_shrink(
808 struct xfs_mount
*mp
;
813 if (!(gfp_mask
& __GFP_FS
))
816 down_read(&xfs_mount_list_lock
);
817 list_for_each_entry(mp
, &xfs_mount_list
, m_mplist
) {
818 xfs_inode_ag_iterator(mp
, xfs_reclaim_inode
, 0,
819 XFS_ICI_RECLAIM_TAG
, 1, &nr_to_scan
);
823 up_read(&xfs_mount_list_lock
);
826 down_read(&xfs_mount_list_lock
);
827 list_for_each_entry(mp
, &xfs_mount_list
, m_mplist
) {
828 for (ag
= 0; ag
< mp
->m_sb
.sb_agcount
; ag
++) {
830 if (!mp
->m_perag
[ag
].pag_ici_init
)
832 reclaimable
+= mp
->m_perag
[ag
].pag_ici_reclaimable
;
835 up_read(&xfs_mount_list_lock
);
839 static struct shrinker xfs_inode_shrinker
= {
840 .shrink
= xfs_reclaim_inode_shrink
,
841 .seeks
= DEFAULT_SEEKS
,
845 xfs_inode_shrinker_init(void)
847 init_rwsem(&xfs_mount_list_lock
);
848 register_shrinker(&xfs_inode_shrinker
);
852 xfs_inode_shrinker_destroy(void)
854 ASSERT(list_empty(&xfs_mount_list
));
855 unregister_shrinker(&xfs_inode_shrinker
);
859 xfs_inode_shrinker_register(
860 struct xfs_mount
*mp
)
862 down_write(&xfs_mount_list_lock
);
863 list_add_tail(&mp
->m_mplist
, &xfs_mount_list
);
864 up_write(&xfs_mount_list_lock
);
868 xfs_inode_shrinker_unregister(
869 struct xfs_mount
*mp
)
871 down_write(&xfs_mount_list_lock
);
872 list_del(&mp
->m_mplist
);
873 up_write(&xfs_mount_list_lock
);