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 read_lock(&pag->pag_ici_lock);
69 if (tag == XFS_ICI_NO_TAG) {
70 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
71 (void **)&ip, *first_index, 1);
73 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
74 (void **)&ip, *first_index, 1, tag);
80 * Update the index for the next lookup. Catch overflows
81 * into the next AG range which can occur if we have inodes
82 * in the last block of the AG and we are currently
83 * pointing to the last inode.
85 *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
86 if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
92 read_unlock(&pag->pag_ici_lock);
100 int (*execute)(struct xfs_inode *ip,
101 struct xfs_perag *pag, int flags),
105 struct xfs_perag *pag = &mp->m_perag[ag];
106 uint32_t first_index;
117 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
121 error = execute(ip, pag, flags);
122 if (error == EAGAIN) {
129 * bail out if the filesystem is corrupted.
131 if (error == EFSCORRUPTED)
141 xfs_put_perag(mp, pag);
146 xfs_inode_ag_iterator(
147 struct xfs_mount *mp,
148 int (*execute)(struct xfs_inode *ip,
149 struct xfs_perag *pag, int flags),
157 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
158 if (!mp->m_perag[ag].pag_ici_init)
160 error = xfs_inode_ag_walk(mp, ag, execute, flags, tag);
163 if (error == EFSCORRUPTED)
167 return XFS_ERROR(last_error);
170 /* must be called with pag_ici_lock held and releases it */
172 xfs_sync_inode_valid(
173 struct xfs_inode *ip,
174 struct xfs_perag *pag)
176 struct inode *inode = VFS_I(ip);
178 /* nothing to sync during shutdown */
179 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
180 read_unlock(&pag->pag_ici_lock);
185 * If we can't get a reference on the inode, it must be in reclaim.
186 * Leave it for the reclaim code to flush. Also avoid inodes that
187 * haven't been fully initialised.
190 read_unlock(&pag->pag_ici_lock);
193 read_unlock(&pag->pag_ici_lock);
195 if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) {
205 struct xfs_inode *ip,
206 struct xfs_perag *pag,
209 struct inode *inode = VFS_I(ip);
210 struct address_space *mapping = inode->i_mapping;
213 error = xfs_sync_inode_valid(ip, pag);
217 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
220 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
221 if (flags & SYNC_TRYLOCK)
223 xfs_ilock(ip, XFS_IOLOCK_SHARED);
226 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
227 0 : XFS_B_ASYNC, FI_NONE);
228 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
231 if (flags & SYNC_WAIT)
239 struct xfs_inode *ip,
240 struct xfs_perag *pag,
245 error = xfs_sync_inode_valid(ip, pag);
249 xfs_ilock(ip, XFS_ILOCK_SHARED);
250 if (xfs_inode_clean(ip))
252 if (!xfs_iflock_nowait(ip)) {
253 if (!(flags & SYNC_WAIT))
258 if (xfs_inode_clean(ip)) {
263 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
264 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
267 xfs_iunlock(ip, XFS_ILOCK_SHARED);
273 * Write out pagecache data for the whole filesystem.
277 struct xfs_mount *mp,
282 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
284 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
287 return XFS_ERROR(error);
290 (flags & SYNC_WAIT) ?
291 XFS_LOG_FORCE | XFS_LOG_SYNC :
297 * Write out inode metadata (attributes) for the whole filesystem.
301 struct xfs_mount *mp,
304 ASSERT((flags & ~SYNC_WAIT) == 0);
306 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
311 xfs_commit_dummy_trans(
312 struct xfs_mount *mp,
315 struct xfs_inode *ip = mp->m_rootip;
316 struct xfs_trans *tp;
318 int log_flags = XFS_LOG_FORCE;
320 if (flags & SYNC_WAIT)
321 log_flags |= XFS_LOG_SYNC;
324 * Put a dummy transaction in the log to tell recovery
325 * that all others are OK.
327 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
328 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
330 xfs_trans_cancel(tp, 0);
334 xfs_ilock(ip, XFS_ILOCK_EXCL);
336 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
337 xfs_trans_ihold(tp, ip);
338 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
339 error = xfs_trans_commit(tp, 0);
340 xfs_iunlock(ip, XFS_ILOCK_EXCL);
342 /* the log force ensures this transaction is pushed to disk */
343 xfs_log_force(mp, 0, log_flags);
349 struct xfs_mount *mp,
353 struct xfs_buf_log_item *bip;
357 * If this is xfssyncd() then only sync the superblock if we can
358 * lock it without sleeping and it is not pinned.
360 if (flags & SYNC_TRYLOCK) {
361 ASSERT(!(flags & SYNC_WAIT));
363 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
367 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
368 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
371 bp = xfs_getsb(mp, 0);
374 * If the buffer is pinned then push on the log so we won't
375 * get stuck waiting in the write for someone, maybe
376 * ourselves, to flush the log.
378 * Even though we just pushed the log above, we did not have
379 * the superblock buffer locked at that point so it can
380 * become pinned in between there and here.
382 if (XFS_BUF_ISPINNED(bp))
383 xfs_log_force(mp, 0, XFS_LOG_FORCE);
387 if (flags & SYNC_WAIT)
392 error = xfs_bwrite(mp, bp);
397 * If this is a data integrity sync make sure all pending buffers
398 * are flushed out for the log coverage check below.
400 if (flags & SYNC_WAIT)
401 xfs_flush_buftarg(mp->m_ddev_targp, 1);
403 if (xfs_log_need_covered(mp))
404 error = xfs_commit_dummy_trans(mp, flags);
414 * When remounting a filesystem read-only or freezing the filesystem, we have
415 * two phases to execute. This first phase is syncing the data before we
416 * quiesce the filesystem, and the second is flushing all the inodes out after
417 * we've waited for all the transactions created by the first phase to
418 * complete. The second phase ensures that the inodes are written to their
419 * location on disk rather than just existing in transactions in the log. This
420 * means after a quiesce there is no log replay required to write the inodes to
421 * disk (this is the main difference between a sync and a quiesce).
424 * First stage of freeze - no writers will make progress now we are here,
425 * so we flush delwri and delalloc buffers here, then wait for all I/O to
426 * complete. Data is frozen at that point. Metadata is not frozen,
427 * transactions can still occur here so don't bother flushing the buftarg
428 * because it'll just get dirty again.
432 struct xfs_mount *mp)
436 /* push non-blocking */
437 xfs_sync_data(mp, 0);
438 xfs_qm_sync(mp, SYNC_TRYLOCK);
440 /* push and block till complete */
441 xfs_sync_data(mp, SYNC_WAIT);
442 xfs_qm_sync(mp, SYNC_WAIT);
444 /* drop inode references pinned by filestreams */
445 xfs_filestream_flush(mp);
447 /* write superblock and hoover up shutdown errors */
448 error = xfs_sync_fsdata(mp, SYNC_WAIT);
450 /* flush data-only devices */
451 if (mp->m_rtdev_targp)
452 XFS_bflush(mp->m_rtdev_targp);
459 struct xfs_mount *mp)
461 int count = 0, pincount;
463 xfs_flush_buftarg(mp->m_ddev_targp, 0);
464 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
467 * This loop must run at least twice. The first instance of the loop
468 * will flush most meta data but that will generate more meta data
469 * (typically directory updates). Which then must be flushed and
470 * logged before we can write the unmount record.
473 xfs_sync_attr(mp, SYNC_WAIT);
474 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
483 * Second stage of a quiesce. The data is already synced, now we have to take
484 * care of the metadata. New transactions are already blocked, so we need to
485 * wait for any remaining transactions to drain out before proceding.
489 struct xfs_mount *mp)
493 /* wait for all modifications to complete */
494 while (atomic_read(&mp->m_active_trans) > 0)
497 /* flush inodes and push all remaining buffers out to disk */
501 * Just warn here till VFS can correctly support
502 * read-only remount without racing.
504 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
506 /* Push the superblock and write an unmount record */
507 error = xfs_log_sbcount(mp, 1);
509 xfs_fs_cmn_err(CE_WARN, mp,
510 "xfs_attr_quiesce: failed to log sb changes. "
511 "Frozen image may not be consistent.");
512 xfs_log_unmount_write(mp);
513 xfs_unmountfs_writesb(mp);
517 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
518 * Doing this has two advantages:
519 * - It saves on stack space, which is tight in certain situations
520 * - It can be used (with care) as a mechanism to avoid deadlocks.
521 * Flushing while allocating in a full filesystem requires both.
524 xfs_syncd_queue_work(
525 struct xfs_mount *mp,
527 void (*syncer)(struct xfs_mount *, void *),
528 struct completion *completion)
530 struct xfs_sync_work *work;
532 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
533 INIT_LIST_HEAD(&work->w_list);
534 work->w_syncer = syncer;
537 work->w_completion = completion;
538 spin_lock(&mp->m_sync_lock);
539 list_add_tail(&work->w_list, &mp->m_sync_list);
540 spin_unlock(&mp->m_sync_lock);
541 wake_up_process(mp->m_sync_task);
545 * Flush delayed allocate data, attempting to free up reserved space
546 * from existing allocations. At this point a new allocation attempt
547 * has failed with ENOSPC and we are in the process of scratching our
548 * heads, looking about for more room...
551 xfs_flush_inodes_work(
552 struct xfs_mount *mp,
555 struct inode *inode = arg;
556 xfs_sync_data(mp, SYNC_TRYLOCK);
557 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
565 struct inode *inode = VFS_I(ip);
566 DECLARE_COMPLETION_ONSTACK(completion);
569 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
570 wait_for_completion(&completion);
571 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
575 * Every sync period we need to unpin all items, reclaim inodes, sync
576 * quota and write out the superblock. We might need to cover the log
577 * to indicate it is idle.
581 struct xfs_mount *mp,
586 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
587 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
588 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
589 /* dgc: errors ignored here */
590 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
591 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
594 wake_up(&mp->m_wait_single_sync_task);
601 struct xfs_mount *mp = arg;
603 xfs_sync_work_t *work, *n;
607 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
609 timeleft = schedule_timeout_interruptible(timeleft);
612 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
615 spin_lock(&mp->m_sync_lock);
617 * We can get woken by laptop mode, to do a sync -
618 * that's the (only!) case where the list would be
619 * empty with time remaining.
621 if (!timeleft || list_empty(&mp->m_sync_list)) {
623 timeleft = xfs_syncd_centisecs *
624 msecs_to_jiffies(10);
625 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
626 list_add_tail(&mp->m_sync_work.w_list,
629 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
630 list_move(&work->w_list, &tmp);
631 spin_unlock(&mp->m_sync_lock);
633 list_for_each_entry_safe(work, n, &tmp, w_list) {
634 (*work->w_syncer)(mp, work->w_data);
635 list_del(&work->w_list);
636 if (work == &mp->m_sync_work)
638 if (work->w_completion)
639 complete(work->w_completion);
649 struct xfs_mount *mp)
651 mp->m_sync_work.w_syncer = xfs_sync_worker;
652 mp->m_sync_work.w_mount = mp;
653 mp->m_sync_work.w_completion = NULL;
654 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
655 if (IS_ERR(mp->m_sync_task))
656 return -PTR_ERR(mp->m_sync_task);
662 struct xfs_mount *mp)
664 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_iflags_set(ip, XFS_IRECLAIM);
688 spin_unlock(&ip->i_flags_lock);
689 write_unlock(&pag->pag_ici_lock);
690 xfs_put_perag(ip->i_mount, pag);
693 * If the inode is still dirty, then flush it out. If the inode
694 * is not in the AIL, then it will be OK to flush it delwri as
695 * long as xfs_iflush() does not keep any references to the inode.
696 * We leave that decision up to xfs_iflush() since it has the
697 * knowledge of whether it's OK to simply do a delwri flush of
698 * the inode or whether we need to wait until the inode is
699 * pulled from the AIL.
700 * We get the flush lock regardless, though, just to make sure
701 * we don't free it while it is being flushed.
703 xfs_ilock(ip, XFS_ILOCK_EXCL);
707 * In the case of a forced shutdown we rely on xfs_iflush() to
708 * wait for the inode to be unpinned before returning an error.
710 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
711 /* synchronize with xfs_iflush_done */
716 xfs_iunlock(ip, XFS_ILOCK_EXCL);
722 __xfs_inode_set_reclaim_tag(
723 struct xfs_perag *pag,
724 struct xfs_inode *ip)
726 radix_tree_tag_set(&pag->pag_ici_root,
727 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
728 XFS_ICI_RECLAIM_TAG);
732 * We set the inode flag atomically with the radix tree tag.
733 * Once we get tag lookups on the radix tree, this inode flag
737 xfs_inode_set_reclaim_tag(
740 xfs_mount_t *mp = ip->i_mount;
741 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
743 read_lock(&pag->pag_ici_lock);
744 spin_lock(&ip->i_flags_lock);
745 __xfs_inode_set_reclaim_tag(pag, ip);
746 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
747 spin_unlock(&ip->i_flags_lock);
748 read_unlock(&pag->pag_ici_lock);
749 xfs_put_perag(mp, pag);
753 __xfs_inode_clear_reclaim_tag(
758 radix_tree_tag_clear(&pag->pag_ici_root,
759 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
763 xfs_reclaim_inode_now(
764 struct xfs_inode *ip,
765 struct xfs_perag *pag,
768 /* ignore if already under reclaim */
769 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
770 read_unlock(&pag->pag_ici_lock);
773 read_unlock(&pag->pag_ici_lock);
775 return xfs_reclaim_inode(ip, flags);
783 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode_now, mode,
784 XFS_ICI_RECLAIM_TAG);