[XFS] Kill xfs_sync()
[deliverable/linux.git] / fs / xfs / linux-2.6 / xfs_sync.c
CommitLineData
fe4fa4b8
DC
1/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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.
8 *
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.
13 *
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
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir2.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"
45#include "xfs_rw.h"
46
a167b17e
DC
47#include <linux/kthread.h>
48#include <linux/freezer.h>
49
fe4fa4b8 50/*
683a8970
DC
51 * Sync all the inodes in the given AG according to the
52 * direction given by the flags.
fe4fa4b8 53 */
683a8970
DC
54STATIC int
55xfs_sync_inodes_ag(
fe4fa4b8 56 xfs_mount_t *mp,
683a8970 57 int ag,
2030b5ab 58 int flags)
fe4fa4b8 59{
683a8970 60 xfs_perag_t *pag = &mp->m_perag[ag];
683a8970
DC
61 int nr_found;
62 int first_index = 0;
63 int error = 0;
64 int last_error = 0;
65 int fflag = XFS_B_ASYNC;
66 int lock_flags = XFS_ILOCK_SHARED;
fe4fa4b8 67
fe4fa4b8
DC
68 if (flags & SYNC_DELWRI)
69 fflag = XFS_B_DELWRI;
70 if (flags & SYNC_WAIT)
71 fflag = 0; /* synchronous overrides all */
72
cb56a4b9 73 if (flags & SYNC_DELWRI) {
fe4fa4b8
DC
74 /*
75 * We need the I/O lock if we're going to call any of
76 * the flush/inval routines.
77 */
683a8970 78 lock_flags |= XFS_IOLOCK_SHARED;
fe4fa4b8
DC
79 }
80
fe4fa4b8 81 do {
bc60a993
DC
82 struct inode *inode;
83 boolean_t inode_refed;
84 xfs_inode_t *ip = NULL;
85
fe4fa4b8 86 /*
683a8970
DC
87 * use a gang lookup to find the next inode in the tree
88 * as the tree is sparse and a gang lookup walks to find
89 * the number of objects requested.
fe4fa4b8 90 */
683a8970
DC
91 read_lock(&pag->pag_ici_lock);
92 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
93 (void**)&ip, first_index, 1);
fe4fa4b8 94
683a8970
DC
95 if (!nr_found) {
96 read_unlock(&pag->pag_ici_lock);
97 break;
fe4fa4b8
DC
98 }
99
683a8970
DC
100 /* update the index for the next lookup */
101 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
fe4fa4b8
DC
102
103 /*
683a8970
DC
104 * skip inodes in reclaim. Let xfs_syncsub do that for
105 * us so we don't need to worry.
fe4fa4b8 106 */
bc60a993 107 if (xfs_iflags_test(ip, (XFS_IRECLAIM|XFS_IRECLAIMABLE))) {
683a8970 108 read_unlock(&pag->pag_ici_lock);
fe4fa4b8
DC
109 continue;
110 }
111
683a8970 112 /* bad inodes are dealt with elsewhere */
bc60a993
DC
113 inode = VFS_I(ip);
114 if (is_bad_inode(inode)) {
683a8970 115 read_unlock(&pag->pag_ici_lock);
fe4fa4b8
DC
116 continue;
117 }
118
683a8970 119 /* nothing to sync during shutdown */
cb56a4b9 120 if (XFS_FORCED_SHUTDOWN(mp)) {
683a8970 121 read_unlock(&pag->pag_ici_lock);
fe4fa4b8
DC
122 return 0;
123 }
124
125 /*
bc60a993
DC
126 * If we can't get a reference on the VFS_I, the inode must be
127 * in reclaim. If we can get the inode lock without blocking,
128 * it is safe to flush the inode because we hold the tree lock
129 * and xfs_iextract will block right now. Hence if we lock the
130 * inode while holding the tree lock, xfs_ireclaim() is
131 * guaranteed to block on the inode lock we now hold and hence
132 * it is safe to reference the inode until we drop the inode
133 * locks completely.
fe4fa4b8 134 */
bc60a993
DC
135 inode_refed = B_FALSE;
136 if (igrab(inode)) {
683a8970 137 read_unlock(&pag->pag_ici_lock);
fe4fa4b8 138 xfs_ilock(ip, lock_flags);
bc60a993 139 inode_refed = B_TRUE;
683a8970 140 } else {
bc60a993
DC
141 if (!xfs_ilock_nowait(ip, lock_flags)) {
142 /* leave it to reclaim */
143 read_unlock(&pag->pag_ici_lock);
144 continue;
145 }
683a8970 146 read_unlock(&pag->pag_ici_lock);
fe4fa4b8 147 }
bc60a993 148
fe4fa4b8
DC
149 /*
150 * If we have to flush data or wait for I/O completion
151 * we need to drop the ilock that we currently hold.
152 * If we need to drop the lock, insert a marker if we
153 * have not already done so.
154 */
bc60a993 155 if ((flags & SYNC_DELWRI) && VN_DIRTY(inode)) {
683a8970
DC
156 xfs_iunlock(ip, XFS_ILOCK_SHARED);
157 error = xfs_flush_pages(ip, 0, -1, fflag, FI_NONE);
158 if (flags & SYNC_IOWAIT)
159 vn_iowait(ip);
160 xfs_ilock(ip, XFS_ILOCK_SHARED);
161 }
fe4fa4b8 162
683a8970 163 if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
fe4fa4b8
DC
164 if (flags & SYNC_WAIT) {
165 xfs_iflock(ip);
683a8970
DC
166 if (!xfs_inode_clean(ip))
167 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
168 else
169 xfs_ifunlock(ip);
fe4fa4b8 170 } else if (xfs_iflock_nowait(ip)) {
683a8970
DC
171 if (!xfs_inode_clean(ip))
172 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
173 else
174 xfs_ifunlock(ip);
fe4fa4b8
DC
175 }
176 }
177
683a8970 178 if (lock_flags)
fe4fa4b8 179 xfs_iunlock(ip, lock_flags);
fe4fa4b8 180
bc60a993 181 if (inode_refed) {
fe4fa4b8 182 IRELE(ip);
fe4fa4b8
DC
183 }
184
683a8970 185 if (error)
fe4fa4b8 186 last_error = error;
fe4fa4b8
DC
187 /*
188 * bail out if the filesystem is corrupted.
189 */
683a8970 190 if (error == EFSCORRUPTED)
fe4fa4b8 191 return XFS_ERROR(error);
fe4fa4b8 192
683a8970 193 } while (nr_found);
fe4fa4b8 194
683a8970
DC
195 return last_error;
196}
fe4fa4b8 197
683a8970
DC
198int
199xfs_sync_inodes(
200 xfs_mount_t *mp,
2030b5ab 201 int flags)
683a8970
DC
202{
203 int error;
204 int last_error;
205 int i;
e9f1c6ee 206 int lflags = XFS_LOG_FORCE;
fe4fa4b8 207
683a8970
DC
208 if (mp->m_flags & XFS_MOUNT_RDONLY)
209 return 0;
210 error = 0;
211 last_error = 0;
fe4fa4b8 212
e9f1c6ee
DC
213 if (flags & SYNC_WAIT)
214 lflags |= XFS_LOG_SYNC;
215
683a8970
DC
216 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
217 if (!mp->m_perag[i].pag_ici_init)
218 continue;
2030b5ab 219 error = xfs_sync_inodes_ag(mp, i, flags);
683a8970
DC
220 if (error)
221 last_error = error;
222 if (error == EFSCORRUPTED)
223 break;
224 }
e9f1c6ee
DC
225 if (flags & SYNC_DELWRI)
226 xfs_log_force(mp, 0, lflags);
227
fe4fa4b8
DC
228 return XFS_ERROR(last_error);
229}
230
2af75df7
CH
231STATIC int
232xfs_commit_dummy_trans(
233 struct xfs_mount *mp,
234 uint log_flags)
235{
236 struct xfs_inode *ip = mp->m_rootip;
237 struct xfs_trans *tp;
238 int error;
239
240 /*
241 * Put a dummy transaction in the log to tell recovery
242 * that all others are OK.
243 */
244 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
245 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
246 if (error) {
247 xfs_trans_cancel(tp, 0);
248 return error;
249 }
250
251 xfs_ilock(ip, XFS_ILOCK_EXCL);
252
253 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
254 xfs_trans_ihold(tp, ip);
255 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
256 /* XXX(hch): ignoring the error here.. */
257 error = xfs_trans_commit(tp, 0);
258
259 xfs_iunlock(ip, XFS_ILOCK_EXCL);
260
261 xfs_log_force(mp, 0, log_flags);
262 return 0;
263}
264
e9f1c6ee 265int
2af75df7
CH
266xfs_sync_fsdata(
267 struct xfs_mount *mp,
268 int flags)
269{
270 struct xfs_buf *bp;
271 struct xfs_buf_log_item *bip;
272 int error = 0;
273
274 /*
275 * If this is xfssyncd() then only sync the superblock if we can
276 * lock it without sleeping and it is not pinned.
277 */
278 if (flags & SYNC_BDFLUSH) {
279 ASSERT(!(flags & SYNC_WAIT));
280
281 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
282 if (!bp)
283 goto out;
284
285 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
286 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
287 goto out_brelse;
288 } else {
289 bp = xfs_getsb(mp, 0);
290
291 /*
292 * If the buffer is pinned then push on the log so we won't
293 * get stuck waiting in the write for someone, maybe
294 * ourselves, to flush the log.
295 *
296 * Even though we just pushed the log above, we did not have
297 * the superblock buffer locked at that point so it can
298 * become pinned in between there and here.
299 */
300 if (XFS_BUF_ISPINNED(bp))
301 xfs_log_force(mp, 0, XFS_LOG_FORCE);
302 }
303
304
305 if (flags & SYNC_WAIT)
306 XFS_BUF_UNASYNC(bp);
307 else
308 XFS_BUF_ASYNC(bp);
309
310 return xfs_bwrite(mp, bp);
311
312 out_brelse:
313 xfs_buf_relse(bp);
314 out:
315 return error;
e9f1c6ee
DC
316}
317
318/*
a4e4c4f4
DC
319 * When remounting a filesystem read-only or freezing the filesystem, we have
320 * two phases to execute. This first phase is syncing the data before we
321 * quiesce the filesystem, and the second is flushing all the inodes out after
322 * we've waited for all the transactions created by the first phase to
323 * complete. The second phase ensures that the inodes are written to their
324 * location on disk rather than just existing in transactions in the log. This
325 * means after a quiesce there is no log replay required to write the inodes to
326 * disk (this is the main difference between a sync and a quiesce).
327 */
328/*
329 * First stage of freeze - no writers will make progress now we are here,
e9f1c6ee
DC
330 * so we flush delwri and delalloc buffers here, then wait for all I/O to
331 * complete. Data is frozen at that point. Metadata is not frozen,
a4e4c4f4
DC
332 * transactions can still occur here so don't bother flushing the buftarg
333 * because it'll just get dirty again.
e9f1c6ee
DC
334 */
335int
336xfs_quiesce_data(
337 struct xfs_mount *mp)
338{
339 int error;
340
341 /* push non-blocking */
342 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_BDFLUSH);
343 XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
344 xfs_filestream_flush(mp);
345
346 /* push and block */
347 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_WAIT|SYNC_IOWAIT);
348 XFS_QM_DQSYNC(mp, SYNC_WAIT);
349
a4e4c4f4 350 /* write superblock and hoover up shutdown errors */
e9f1c6ee
DC
351 error = xfs_sync_fsdata(mp, 0);
352
a4e4c4f4 353 /* flush data-only devices */
e9f1c6ee
DC
354 if (mp->m_rtdev_targp)
355 XFS_bflush(mp->m_rtdev_targp);
356
357 return error;
2af75df7
CH
358}
359
a167b17e
DC
360/*
361 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
362 * Doing this has two advantages:
363 * - It saves on stack space, which is tight in certain situations
364 * - It can be used (with care) as a mechanism to avoid deadlocks.
365 * Flushing while allocating in a full filesystem requires both.
366 */
367STATIC void
368xfs_syncd_queue_work(
369 struct xfs_mount *mp,
370 void *data,
371 void (*syncer)(struct xfs_mount *, void *))
372{
373 struct bhv_vfs_sync_work *work;
374
375 work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
376 INIT_LIST_HEAD(&work->w_list);
377 work->w_syncer = syncer;
378 work->w_data = data;
379 work->w_mount = mp;
380 spin_lock(&mp->m_sync_lock);
381 list_add_tail(&work->w_list, &mp->m_sync_list);
382 spin_unlock(&mp->m_sync_lock);
383 wake_up_process(mp->m_sync_task);
384}
385
386/*
387 * Flush delayed allocate data, attempting to free up reserved space
388 * from existing allocations. At this point a new allocation attempt
389 * has failed with ENOSPC and we are in the process of scratching our
390 * heads, looking about for more room...
391 */
392STATIC void
393xfs_flush_inode_work(
394 struct xfs_mount *mp,
395 void *arg)
396{
397 struct inode *inode = arg;
398 filemap_flush(inode->i_mapping);
399 iput(inode);
400}
401
402void
403xfs_flush_inode(
404 xfs_inode_t *ip)
405{
406 struct inode *inode = VFS_I(ip);
407
408 igrab(inode);
409 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
410 delay(msecs_to_jiffies(500));
411}
412
413/*
414 * This is the "bigger hammer" version of xfs_flush_inode_work...
415 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
416 */
417STATIC void
418xfs_flush_device_work(
419 struct xfs_mount *mp,
420 void *arg)
421{
422 struct inode *inode = arg;
423 sync_blockdev(mp->m_super->s_bdev);
424 iput(inode);
425}
426
427void
428xfs_flush_device(
429 xfs_inode_t *ip)
430{
431 struct inode *inode = VFS_I(ip);
432
433 igrab(inode);
434 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
435 delay(msecs_to_jiffies(500));
436 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
437}
438
aacaa880
DC
439/*
440 * Every sync period we need to unpin all items, reclaim inodes, sync
441 * quota and write out the superblock. We might need to cover the log
442 * to indicate it is idle.
443 */
a167b17e
DC
444STATIC void
445xfs_sync_worker(
446 struct xfs_mount *mp,
447 void *unused)
448{
449 int error;
450
aacaa880
DC
451 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
452 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
453 xfs_finish_reclaim_all(mp, 1, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
454 /* dgc: errors ignored here */
455 error = XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
456 error = xfs_sync_fsdata(mp, SYNC_BDFLUSH);
457 if (xfs_log_need_covered(mp))
458 error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
459 }
a167b17e
DC
460 mp->m_sync_seq++;
461 wake_up(&mp->m_wait_single_sync_task);
462}
463
464STATIC int
465xfssyncd(
466 void *arg)
467{
468 struct xfs_mount *mp = arg;
469 long timeleft;
470 bhv_vfs_sync_work_t *work, *n;
471 LIST_HEAD (tmp);
472
473 set_freezable();
474 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
475 for (;;) {
476 timeleft = schedule_timeout_interruptible(timeleft);
477 /* swsusp */
478 try_to_freeze();
479 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
480 break;
481
482 spin_lock(&mp->m_sync_lock);
483 /*
484 * We can get woken by laptop mode, to do a sync -
485 * that's the (only!) case where the list would be
486 * empty with time remaining.
487 */
488 if (!timeleft || list_empty(&mp->m_sync_list)) {
489 if (!timeleft)
490 timeleft = xfs_syncd_centisecs *
491 msecs_to_jiffies(10);
492 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
493 list_add_tail(&mp->m_sync_work.w_list,
494 &mp->m_sync_list);
495 }
496 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
497 list_move(&work->w_list, &tmp);
498 spin_unlock(&mp->m_sync_lock);
499
500 list_for_each_entry_safe(work, n, &tmp, w_list) {
501 (*work->w_syncer)(mp, work->w_data);
502 list_del(&work->w_list);
503 if (work == &mp->m_sync_work)
504 continue;
505 kmem_free(work);
506 }
507 }
508
509 return 0;
510}
511
512int
513xfs_syncd_init(
514 struct xfs_mount *mp)
515{
516 mp->m_sync_work.w_syncer = xfs_sync_worker;
517 mp->m_sync_work.w_mount = mp;
518 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
519 if (IS_ERR(mp->m_sync_task))
520 return -PTR_ERR(mp->m_sync_task);
521 return 0;
522}
523
524void
525xfs_syncd_stop(
526 struct xfs_mount *mp)
527{
528 kthread_stop(mp->m_sync_task);
529}
530
This page took 0.047261 seconds and 5 git commands to generate.