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"
47 #include <linux/kthread.h>
48 #include <linux/freezer.h>
51 * xfs_sync flushes any pending I/O to file system vfsp.
53 * This routine is called by vfs_sync() to make sure that things make it
54 * out to disk eventually, on sync() system calls to flush out everything,
55 * and when the file system is unmounted. For the vfs_sync() case, all
56 * we really need to do is sync out the log to make all of our meta-data
57 * updates permanent (except for timestamps). For calls from pflushd(),
58 * dirty pages are kept moving by calling pdflush() on the inodes
59 * containing them. We also flush the inodes that we can lock without
60 * sleeping and the superblock if we can lock it without sleeping from
61 * vfs_sync() so that items at the tail of the log are always moving out.
64 * SYNC_BDFLUSH - We're being called from vfs_sync() so we don't want
65 * to sleep if we can help it. All we really need
66 * to do is ensure that the log is synced at least
67 * periodically. We also push the inodes and
68 * superblock if we can lock them without sleeping
69 * and they are not pinned.
70 * SYNC_ATTR - We need to flush the inodes. If SYNC_BDFLUSH is not
71 * set, then we really want to lock each inode and flush
73 * SYNC_WAIT - All the flushes that take place in this call should
75 * SYNC_DELWRI - This tells us to push dirty pages associated with
76 * inodes. SYNC_WAIT and SYNC_BDFLUSH are used to
77 * determine if they should be flushed sync, async, or
79 * SYNC_CLOSE - This flag is passed when the system is being
80 * unmounted. We should sync and invalidate everything.
81 * SYNC_FSDATA - This indicates that the caller would like to make
82 * sure the superblock is safe on disk. We can ensure
83 * this by simply making sure the log gets flushed
84 * if SYNC_BDFLUSH is set, and by actually writing it
86 * SYNC_IOWAIT - The caller wants us to wait for all data I/O to complete
87 * before we return (including direct I/O). Forms the drain
88 * side of the write barrier needed to safely quiesce the
100 * Get the Quota Manager to flush the dquots.
102 * If XFS quota support is not enabled or this filesystem
103 * instance does not use quotas XFS_QM_DQSYNC will always
106 error = XFS_QM_DQSYNC(mp, flags);
109 * If we got an IO error, we will be shutting down.
110 * So, there's nothing more for us to do here.
112 ASSERT(error != EIO || XFS_FORCED_SHUTDOWN(mp));
113 if (XFS_FORCED_SHUTDOWN(mp))
114 return XFS_ERROR(error);
117 if (flags & SYNC_IOWAIT)
118 xfs_filestream_flush(mp);
120 return xfs_syncsub(mp, flags, NULL);
124 * Sync all the inodes in the given AG according to the
125 * direction given by the flags.
134 xfs_inode_t *ip = NULL;
135 struct inode *vp = NULL;
136 xfs_perag_t *pag = &mp->m_perag[ag];
137 boolean_t vnode_refed = B_FALSE;
142 int fflag = XFS_B_ASYNC;
143 int lock_flags = XFS_ILOCK_SHARED;
145 if (flags & SYNC_DELWRI)
146 fflag = XFS_B_DELWRI;
147 if (flags & SYNC_WAIT)
148 fflag = 0; /* synchronous overrides all */
150 if (flags & (SYNC_DELWRI | SYNC_CLOSE)) {
152 * We need the I/O lock if we're going to call any of
153 * the flush/inval routines.
155 lock_flags |= XFS_IOLOCK_SHARED;
160 * use a gang lookup to find the next inode in the tree
161 * as the tree is sparse and a gang lookup walks to find
162 * the number of objects requested.
164 read_lock(&pag->pag_ici_lock);
165 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
166 (void**)&ip, first_index, 1);
169 read_unlock(&pag->pag_ici_lock);
173 /* update the index for the next lookup */
174 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
177 * skip inodes in reclaim. Let xfs_syncsub do that for
178 * us so we don't need to worry.
182 read_unlock(&pag->pag_ici_lock);
186 /* bad inodes are dealt with elsewhere */
188 read_unlock(&pag->pag_ici_lock);
192 /* nothing to sync during shutdown */
193 if (XFS_FORCED_SHUTDOWN(mp) && !(flags & SYNC_CLOSE)) {
194 read_unlock(&pag->pag_ici_lock);
199 * The inode lock here actually coordinates with the almost
200 * spurious inode lock in xfs_ireclaim() to prevent the vnode
201 * we handle here without a reference from being freed while we
202 * reference it. If we lock the inode while it's on the mount
203 * list here, then the spurious inode lock in xfs_ireclaim()
204 * after the inode is pulled from the mount list will sleep
205 * until we release it here. This keeps the vnode from being
206 * freed while we reference it.
208 if (xfs_ilock_nowait(ip, lock_flags) == 0) {
210 read_unlock(&pag->pag_ici_lock);
213 xfs_ilock(ip, lock_flags);
215 ASSERT(vp == VFS_I(ip));
216 ASSERT(ip->i_mount == mp);
218 vnode_refed = B_TRUE;
220 /* safe to unlock here as we have a reference */
221 read_unlock(&pag->pag_ici_lock);
224 * If we have to flush data or wait for I/O completion
225 * we need to drop the ilock that we currently hold.
226 * If we need to drop the lock, insert a marker if we
227 * have not already done so.
229 if (flags & SYNC_CLOSE) {
230 xfs_iunlock(ip, XFS_ILOCK_SHARED);
231 if (XFS_FORCED_SHUTDOWN(mp))
232 xfs_tosspages(ip, 0, -1, FI_REMAPF);
234 error = xfs_flushinval_pages(ip, 0, -1,
236 /* wait for I/O on freeze */
237 if (flags & SYNC_IOWAIT)
240 xfs_ilock(ip, XFS_ILOCK_SHARED);
243 if ((flags & SYNC_DELWRI) && VN_DIRTY(vp)) {
244 xfs_iunlock(ip, XFS_ILOCK_SHARED);
245 error = xfs_flush_pages(ip, 0, -1, fflag, FI_NONE);
246 if (flags & SYNC_IOWAIT)
248 xfs_ilock(ip, XFS_ILOCK_SHARED);
251 if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
252 if (flags & SYNC_WAIT) {
254 if (!xfs_inode_clean(ip))
255 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
258 } else if (xfs_iflock_nowait(ip)) {
259 if (!xfs_inode_clean(ip))
260 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
263 } else if (bypassed) {
269 xfs_iunlock(ip, lock_flags);
273 vnode_refed = B_FALSE;
279 * bail out if the filesystem is corrupted.
281 if (error == EFSCORRUPTED)
282 return XFS_ERROR(error);
301 if (mp->m_flags & XFS_MOUNT_RDONLY)
306 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
307 if (!mp->m_perag[i].pag_ici_init)
309 error = xfs_sync_inodes_ag(mp, i, flags, bypassed);
312 if (error == EFSCORRUPTED)
315 return XFS_ERROR(last_error);
319 * xfs sync routine for internal use
321 * This routine supports all of the flags defined for the generic vfs_sync
322 * interface as explained above under xfs_sync.
333 uint log_flags = XFS_LOG_FORCE;
335 xfs_buf_log_item_t *bip;
338 * Sync out the log. This ensures that the log is periodically
339 * flushed even if there is not enough activity to fill it up.
341 if (flags & SYNC_WAIT)
342 log_flags |= XFS_LOG_SYNC;
344 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
346 if (flags & (SYNC_ATTR|SYNC_DELWRI)) {
347 if (flags & SYNC_BDFLUSH)
348 xfs_finish_reclaim_all(mp, 1, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
350 error = xfs_sync_inodes(mp, flags, bypassed);
354 * Flushing out dirty data above probably generated more
355 * log activity, so if this isn't vfs_sync() then flush
358 if (flags & SYNC_DELWRI) {
359 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
362 if (flags & SYNC_FSDATA) {
364 * If this is vfs_sync() then only sync the superblock
365 * if we can lock it without sleeping and it is not pinned.
367 if (flags & SYNC_BDFLUSH) {
368 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
370 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
372 xfs_buf_item_dirty(bip)) {
373 if (!(XFS_BUF_ISPINNED(bp))) {
375 error = xfs_bwrite(mp, bp);
384 bp = xfs_getsb(mp, 0);
386 * If the buffer is pinned then push on the log so
387 * we won't get stuck waiting in the write for
388 * someone, maybe ourselves, to flush the log.
389 * Even though we just pushed the log above, we
390 * did not have the superblock buffer locked at
391 * that point so it can become pinned in between
394 if (XFS_BUF_ISPINNED(bp))
395 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
396 if (flags & SYNC_WAIT)
400 error = xfs_bwrite(mp, bp);
408 * Now check to see if the log needs a "dummy" transaction.
410 if (!(flags & SYNC_REMOUNT) && xfs_log_need_covered(mp)) {
415 * Put a dummy transaction in the log to tell
416 * recovery that all others are OK.
418 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
419 if ((error = xfs_trans_reserve(tp, 0,
420 XFS_ICHANGE_LOG_RES(mp),
422 xfs_trans_cancel(tp, 0);
427 xfs_ilock(ip, XFS_ILOCK_EXCL);
429 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
430 xfs_trans_ihold(tp, ip);
431 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
432 error = xfs_trans_commit(tp, 0);
433 xfs_iunlock(ip, XFS_ILOCK_EXCL);
434 xfs_log_force(mp, (xfs_lsn_t)0, log_flags);
438 * When shutting down, we need to insure that the AIL is pushed
439 * to disk or the filesystem can appear corrupt from the PROM.
441 if ((flags & (SYNC_CLOSE|SYNC_WAIT)) == (SYNC_CLOSE|SYNC_WAIT)) {
442 XFS_bflush(mp->m_ddev_targp);
443 if (mp->m_rtdev_targp) {
444 XFS_bflush(mp->m_rtdev_targp);
448 return XFS_ERROR(last_error);
452 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
453 * Doing this has two advantages:
454 * - It saves on stack space, which is tight in certain situations
455 * - It can be used (with care) as a mechanism to avoid deadlocks.
456 * Flushing while allocating in a full filesystem requires both.
459 xfs_syncd_queue_work(
460 struct xfs_mount *mp,
462 void (*syncer)(struct xfs_mount *, void *))
464 struct bhv_vfs_sync_work *work;
466 work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
467 INIT_LIST_HEAD(&work->w_list);
468 work->w_syncer = syncer;
471 spin_lock(&mp->m_sync_lock);
472 list_add_tail(&work->w_list, &mp->m_sync_list);
473 spin_unlock(&mp->m_sync_lock);
474 wake_up_process(mp->m_sync_task);
478 * Flush delayed allocate data, attempting to free up reserved space
479 * from existing allocations. At this point a new allocation attempt
480 * has failed with ENOSPC and we are in the process of scratching our
481 * heads, looking about for more room...
484 xfs_flush_inode_work(
485 struct xfs_mount *mp,
488 struct inode *inode = arg;
489 filemap_flush(inode->i_mapping);
497 struct inode *inode = VFS_I(ip);
500 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
501 delay(msecs_to_jiffies(500));
505 * This is the "bigger hammer" version of xfs_flush_inode_work...
506 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
509 xfs_flush_device_work(
510 struct xfs_mount *mp,
513 struct inode *inode = arg;
514 sync_blockdev(mp->m_super->s_bdev);
522 struct inode *inode = VFS_I(ip);
525 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
526 delay(msecs_to_jiffies(500));
527 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
532 struct xfs_mount *mp,
537 if (!(mp->m_flags & XFS_MOUNT_RDONLY))
538 error = xfs_sync(mp, SYNC_FSDATA | SYNC_BDFLUSH | SYNC_ATTR);
540 wake_up(&mp->m_wait_single_sync_task);
547 struct xfs_mount *mp = arg;
549 bhv_vfs_sync_work_t *work, *n;
553 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
555 timeleft = schedule_timeout_interruptible(timeleft);
558 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
561 spin_lock(&mp->m_sync_lock);
563 * We can get woken by laptop mode, to do a sync -
564 * that's the (only!) case where the list would be
565 * empty with time remaining.
567 if (!timeleft || list_empty(&mp->m_sync_list)) {
569 timeleft = xfs_syncd_centisecs *
570 msecs_to_jiffies(10);
571 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
572 list_add_tail(&mp->m_sync_work.w_list,
575 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
576 list_move(&work->w_list, &tmp);
577 spin_unlock(&mp->m_sync_lock);
579 list_for_each_entry_safe(work, n, &tmp, w_list) {
580 (*work->w_syncer)(mp, work->w_data);
581 list_del(&work->w_list);
582 if (work == &mp->m_sync_work)
593 struct xfs_mount *mp)
595 mp->m_sync_work.w_syncer = xfs_sync_worker;
596 mp->m_sync_work.w_mount = mp;
597 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
598 if (IS_ERR(mp->m_sync_task))
599 return -PTR_ERR(mp->m_sync_task);
605 struct xfs_mount *mp)
607 kthread_stop(mp->m_sync_task);