blob: ee1648b179f71886b7659f9f28dfaffdabdbfb2a [file] [log] [blame]
David Chinnerfe4fa4b2008-10-30 17:06:08 +11001/*
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
David Chinnera167b172008-10-30 17:06:18 +110047#include <linux/kthread.h>
48#include <linux/freezer.h>
49
David Chinnerfe4fa4b2008-10-30 17:06:08 +110050/*
David Chinner683a8972008-10-30 17:07:29 +110051 * Sync all the inodes in the given AG according to the
52 * direction given by the flags.
David Chinnerfe4fa4b2008-10-30 17:06:08 +110053 */
David Chinner683a8972008-10-30 17:07:29 +110054STATIC int
55xfs_sync_inodes_ag(
56 xfs_mount_t *mp,
57 int ag,
David Chinner2030b5a2008-10-30 17:15:12 +110058 int flags)
David Chinner683a8972008-10-30 17:07:29 +110059{
David Chinner683a8972008-10-30 17:07:29 +110060 xfs_perag_t *pag = &mp->m_perag[ag];
David Chinner683a8972008-10-30 17:07:29 +110061 int nr_found;
David Chinner8c38ab02008-10-30 17:38:00 +110062 uint32_t first_index = 0;
David Chinner683a8972008-10-30 17:07:29 +110063 int error = 0;
64 int last_error = 0;
65 int fflag = XFS_B_ASYNC;
66 int lock_flags = XFS_ILOCK_SHARED;
67
68 if (flags & SYNC_DELWRI)
69 fflag = XFS_B_DELWRI;
70 if (flags & SYNC_WAIT)
71 fflag = 0; /* synchronous overrides all */
72
David Chinnercb56a4b2008-10-30 17:16:00 +110073 if (flags & SYNC_DELWRI) {
David Chinner683a8972008-10-30 17:07:29 +110074 /*
75 * We need the I/O lock if we're going to call any of
76 * the flush/inval routines.
77 */
78 lock_flags |= XFS_IOLOCK_SHARED;
79 }
80
81 do {
David Chinnerbc60a992008-10-30 17:15:03 +110082 struct inode *inode;
83 boolean_t inode_refed;
84 xfs_inode_t *ip = NULL;
85
David Chinner683a8972008-10-30 17:07:29 +110086 /*
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.
90 */
91 read_lock(&pag->pag_ici_lock);
92 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
93 (void**)&ip, first_index, 1);
94
95 if (!nr_found) {
96 read_unlock(&pag->pag_ici_lock);
97 break;
98 }
99
David Chinner8c38ab02008-10-30 17:38:00 +1100100 /*
101 * Update the index for the next lookup. Catch overflows
102 * into the next AG range which can occur if we have inodes
103 * in the last block of the AG and we are currently
104 * pointing to the last inode.
105 */
David Chinner683a8972008-10-30 17:07:29 +1100106 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
David Chinner8c38ab02008-10-30 17:38:00 +1100107 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
108 read_unlock(&pag->pag_ici_lock);
109 break;
110 }
David Chinner683a8972008-10-30 17:07:29 +1100111
112 /*
113 * skip inodes in reclaim. Let xfs_syncsub do that for
114 * us so we don't need to worry.
115 */
David Chinnerbc60a992008-10-30 17:15:03 +1100116 if (xfs_iflags_test(ip, (XFS_IRECLAIM|XFS_IRECLAIMABLE))) {
David Chinner683a8972008-10-30 17:07:29 +1100117 read_unlock(&pag->pag_ici_lock);
118 continue;
119 }
120
121 /* bad inodes are dealt with elsewhere */
David Chinnerbc60a992008-10-30 17:15:03 +1100122 inode = VFS_I(ip);
123 if (is_bad_inode(inode)) {
David Chinner683a8972008-10-30 17:07:29 +1100124 read_unlock(&pag->pag_ici_lock);
125 continue;
126 }
127
128 /* nothing to sync during shutdown */
David Chinnercb56a4b2008-10-30 17:16:00 +1100129 if (XFS_FORCED_SHUTDOWN(mp)) {
David Chinner683a8972008-10-30 17:07:29 +1100130 read_unlock(&pag->pag_ici_lock);
131 return 0;
132 }
133
134 /*
David Chinnerbc60a992008-10-30 17:15:03 +1100135 * If we can't get a reference on the VFS_I, the inode must be
136 * in reclaim. If we can get the inode lock without blocking,
137 * it is safe to flush the inode because we hold the tree lock
138 * and xfs_iextract will block right now. Hence if we lock the
139 * inode while holding the tree lock, xfs_ireclaim() is
140 * guaranteed to block on the inode lock we now hold and hence
141 * it is safe to reference the inode until we drop the inode
142 * locks completely.
David Chinner683a8972008-10-30 17:07:29 +1100143 */
David Chinnerbc60a992008-10-30 17:15:03 +1100144 inode_refed = B_FALSE;
145 if (igrab(inode)) {
David Chinner683a8972008-10-30 17:07:29 +1100146 read_unlock(&pag->pag_ici_lock);
David Chinner683a8972008-10-30 17:07:29 +1100147 xfs_ilock(ip, lock_flags);
David Chinnerbc60a992008-10-30 17:15:03 +1100148 inode_refed = B_TRUE;
David Chinner683a8972008-10-30 17:07:29 +1100149 } else {
David Chinnerbc60a992008-10-30 17:15:03 +1100150 if (!xfs_ilock_nowait(ip, lock_flags)) {
151 /* leave it to reclaim */
152 read_unlock(&pag->pag_ici_lock);
153 continue;
154 }
David Chinner683a8972008-10-30 17:07:29 +1100155 read_unlock(&pag->pag_ici_lock);
156 }
David Chinnerbc60a992008-10-30 17:15:03 +1100157
David Chinner683a8972008-10-30 17:07:29 +1100158 /*
159 * If we have to flush data or wait for I/O completion
160 * we need to drop the ilock that we currently hold.
161 * If we need to drop the lock, insert a marker if we
162 * have not already done so.
163 */
David Chinnerbc60a992008-10-30 17:15:03 +1100164 if ((flags & SYNC_DELWRI) && VN_DIRTY(inode)) {
David Chinner683a8972008-10-30 17:07:29 +1100165 xfs_iunlock(ip, XFS_ILOCK_SHARED);
166 error = xfs_flush_pages(ip, 0, -1, fflag, FI_NONE);
167 if (flags & SYNC_IOWAIT)
168 vn_iowait(ip);
169 xfs_ilock(ip, XFS_ILOCK_SHARED);
170 }
171
172 if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
173 if (flags & SYNC_WAIT) {
174 xfs_iflock(ip);
175 if (!xfs_inode_clean(ip))
176 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
177 else
178 xfs_ifunlock(ip);
179 } else if (xfs_iflock_nowait(ip)) {
180 if (!xfs_inode_clean(ip))
181 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
182 else
183 xfs_ifunlock(ip);
David Chinner683a8972008-10-30 17:07:29 +1100184 }
185 }
186
187 if (lock_flags)
188 xfs_iunlock(ip, lock_flags);
189
David Chinnerbc60a992008-10-30 17:15:03 +1100190 if (inode_refed) {
David Chinner683a8972008-10-30 17:07:29 +1100191 IRELE(ip);
David Chinner683a8972008-10-30 17:07:29 +1100192 }
193
194 if (error)
195 last_error = error;
196 /*
197 * bail out if the filesystem is corrupted.
198 */
199 if (error == EFSCORRUPTED)
200 return XFS_ERROR(error);
201
202 } while (nr_found);
203
204 return last_error;
205}
206
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100207int
208xfs_sync_inodes(
209 xfs_mount_t *mp,
David Chinner2030b5a2008-10-30 17:15:12 +1100210 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100211{
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100212 int error;
213 int last_error;
David Chinner683a8972008-10-30 17:07:29 +1100214 int i;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100215 int lflags = XFS_LOG_FORCE;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100216
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100217 if (mp->m_flags & XFS_MOUNT_RDONLY)
218 return 0;
219 error = 0;
220 last_error = 0;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100221
David Chinnere9f1c6e2008-10-30 17:15:50 +1100222 if (flags & SYNC_WAIT)
223 lflags |= XFS_LOG_SYNC;
224
David Chinner683a8972008-10-30 17:07:29 +1100225 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
226 if (!mp->m_perag[i].pag_ici_init)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100227 continue;
David Chinner2030b5a2008-10-30 17:15:12 +1100228 error = xfs_sync_inodes_ag(mp, i, flags);
David Chinner683a8972008-10-30 17:07:29 +1100229 if (error)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100230 last_error = error;
David Chinner683a8972008-10-30 17:07:29 +1100231 if (error == EFSCORRUPTED)
232 break;
233 }
David Chinnere9f1c6e2008-10-30 17:15:50 +1100234 if (flags & SYNC_DELWRI)
235 xfs_log_force(mp, 0, lflags);
236
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100237 return XFS_ERROR(last_error);
238}
239
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100240STATIC int
241xfs_commit_dummy_trans(
242 struct xfs_mount *mp,
243 uint log_flags)
244{
245 struct xfs_inode *ip = mp->m_rootip;
246 struct xfs_trans *tp;
247 int error;
248
249 /*
250 * Put a dummy transaction in the log to tell recovery
251 * that all others are OK.
252 */
253 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
254 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
255 if (error) {
256 xfs_trans_cancel(tp, 0);
257 return error;
258 }
259
260 xfs_ilock(ip, XFS_ILOCK_EXCL);
261
262 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
263 xfs_trans_ihold(tp, ip);
264 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
265 /* XXX(hch): ignoring the error here.. */
266 error = xfs_trans_commit(tp, 0);
267
268 xfs_iunlock(ip, XFS_ILOCK_EXCL);
269
270 xfs_log_force(mp, 0, log_flags);
271 return 0;
272}
273
David Chinnere9f1c6e2008-10-30 17:15:50 +1100274int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100275xfs_sync_fsdata(
276 struct xfs_mount *mp,
277 int flags)
278{
279 struct xfs_buf *bp;
280 struct xfs_buf_log_item *bip;
281 int error = 0;
282
283 /*
284 * If this is xfssyncd() then only sync the superblock if we can
285 * lock it without sleeping and it is not pinned.
286 */
287 if (flags & SYNC_BDFLUSH) {
288 ASSERT(!(flags & SYNC_WAIT));
289
290 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
291 if (!bp)
292 goto out;
293
294 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
295 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
296 goto out_brelse;
297 } else {
298 bp = xfs_getsb(mp, 0);
299
300 /*
301 * If the buffer is pinned then push on the log so we won't
302 * get stuck waiting in the write for someone, maybe
303 * ourselves, to flush the log.
304 *
305 * Even though we just pushed the log above, we did not have
306 * the superblock buffer locked at that point so it can
307 * become pinned in between there and here.
308 */
309 if (XFS_BUF_ISPINNED(bp))
310 xfs_log_force(mp, 0, XFS_LOG_FORCE);
311 }
312
313
314 if (flags & SYNC_WAIT)
315 XFS_BUF_UNASYNC(bp);
316 else
317 XFS_BUF_ASYNC(bp);
318
319 return xfs_bwrite(mp, bp);
320
321 out_brelse:
322 xfs_buf_relse(bp);
323 out:
324 return error;
325}
326
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100327/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100328 * When remounting a filesystem read-only or freezing the filesystem, we have
329 * two phases to execute. This first phase is syncing the data before we
330 * quiesce the filesystem, and the second is flushing all the inodes out after
331 * we've waited for all the transactions created by the first phase to
332 * complete. The second phase ensures that the inodes are written to their
333 * location on disk rather than just existing in transactions in the log. This
334 * means after a quiesce there is no log replay required to write the inodes to
335 * disk (this is the main difference between a sync and a quiesce).
336 */
337/*
338 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100339 * so we flush delwri and delalloc buffers here, then wait for all I/O to
340 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100341 * transactions can still occur here so don't bother flushing the buftarg
342 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100343 */
344int
345xfs_quiesce_data(
346 struct xfs_mount *mp)
347{
348 int error;
349
350 /* push non-blocking */
351 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_BDFLUSH);
352 XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
353 xfs_filestream_flush(mp);
354
355 /* push and block */
356 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_WAIT|SYNC_IOWAIT);
357 XFS_QM_DQSYNC(mp, SYNC_WAIT);
358
David Chinnera4e4c4f2008-10-30 17:16:11 +1100359 /* write superblock and hoover up shutdown errors */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100360 error = xfs_sync_fsdata(mp, 0);
361
David Chinnera4e4c4f2008-10-30 17:16:11 +1100362 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100363 if (mp->m_rtdev_targp)
364 XFS_bflush(mp->m_rtdev_targp);
365
366 return error;
367}
368
David Chinner76bf1052008-10-30 17:16:21 +1100369STATIC void
370xfs_quiesce_fs(
371 struct xfs_mount *mp)
372{
373 int count = 0, pincount;
374
375 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner1dc33182008-10-30 17:37:15 +1100376 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinner76bf1052008-10-30 17:16:21 +1100377
378 /*
379 * This loop must run at least twice. The first instance of the loop
380 * will flush most meta data but that will generate more meta data
381 * (typically directory updates). Which then must be flushed and
382 * logged before we can write the unmount record.
383 */
384 do {
385 xfs_sync_inodes(mp, SYNC_ATTR|SYNC_WAIT);
386 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
387 if (!pincount) {
388 delay(50);
389 count++;
390 }
391 } while (count < 2);
392}
393
394/*
395 * Second stage of a quiesce. The data is already synced, now we have to take
396 * care of the metadata. New transactions are already blocked, so we need to
397 * wait for any remaining transactions to drain out before proceding.
398 */
399void
400xfs_quiesce_attr(
401 struct xfs_mount *mp)
402{
403 int error = 0;
404
405 /* wait for all modifications to complete */
406 while (atomic_read(&mp->m_active_trans) > 0)
407 delay(100);
408
409 /* flush inodes and push all remaining buffers out to disk */
410 xfs_quiesce_fs(mp);
411
412 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
413
414 /* Push the superblock and write an unmount record */
415 error = xfs_log_sbcount(mp, 1);
416 if (error)
417 xfs_fs_cmn_err(CE_WARN, mp,
418 "xfs_attr_quiesce: failed to log sb changes. "
419 "Frozen image may not be consistent.");
420 xfs_log_unmount_write(mp);
421 xfs_unmountfs_writesb(mp);
422}
423
David Chinnere9f1c6e2008-10-30 17:15:50 +1100424/*
David Chinnera167b172008-10-30 17:06:18 +1100425 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
426 * Doing this has two advantages:
427 * - It saves on stack space, which is tight in certain situations
428 * - It can be used (with care) as a mechanism to avoid deadlocks.
429 * Flushing while allocating in a full filesystem requires both.
430 */
431STATIC void
432xfs_syncd_queue_work(
433 struct xfs_mount *mp,
434 void *data,
435 void (*syncer)(struct xfs_mount *, void *))
436{
437 struct bhv_vfs_sync_work *work;
438
439 work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
440 INIT_LIST_HEAD(&work->w_list);
441 work->w_syncer = syncer;
442 work->w_data = data;
443 work->w_mount = mp;
444 spin_lock(&mp->m_sync_lock);
445 list_add_tail(&work->w_list, &mp->m_sync_list);
446 spin_unlock(&mp->m_sync_lock);
447 wake_up_process(mp->m_sync_task);
448}
449
450/*
451 * Flush delayed allocate data, attempting to free up reserved space
452 * from existing allocations. At this point a new allocation attempt
453 * has failed with ENOSPC and we are in the process of scratching our
454 * heads, looking about for more room...
455 */
456STATIC void
457xfs_flush_inode_work(
458 struct xfs_mount *mp,
459 void *arg)
460{
461 struct inode *inode = arg;
462 filemap_flush(inode->i_mapping);
463 iput(inode);
464}
465
466void
467xfs_flush_inode(
468 xfs_inode_t *ip)
469{
470 struct inode *inode = VFS_I(ip);
471
472 igrab(inode);
473 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
474 delay(msecs_to_jiffies(500));
475}
476
477/*
478 * This is the "bigger hammer" version of xfs_flush_inode_work...
479 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
480 */
481STATIC void
482xfs_flush_device_work(
483 struct xfs_mount *mp,
484 void *arg)
485{
486 struct inode *inode = arg;
487 sync_blockdev(mp->m_super->s_bdev);
488 iput(inode);
489}
490
491void
492xfs_flush_device(
493 xfs_inode_t *ip)
494{
495 struct inode *inode = VFS_I(ip);
496
497 igrab(inode);
498 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
499 delay(msecs_to_jiffies(500));
500 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
501}
502
David Chinneraacaa882008-10-30 17:15:29 +1100503/*
504 * Every sync period we need to unpin all items, reclaim inodes, sync
505 * quota and write out the superblock. We might need to cover the log
506 * to indicate it is idle.
507 */
David Chinnera167b172008-10-30 17:06:18 +1100508STATIC void
509xfs_sync_worker(
510 struct xfs_mount *mp,
511 void *unused)
512{
513 int error;
514
David Chinneraacaa882008-10-30 17:15:29 +1100515 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
516 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
David Chinner1dc33182008-10-30 17:37:15 +1100517 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinneraacaa882008-10-30 17:15:29 +1100518 /* dgc: errors ignored here */
519 error = XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
520 error = xfs_sync_fsdata(mp, SYNC_BDFLUSH);
521 if (xfs_log_need_covered(mp))
522 error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
523 }
David Chinnera167b172008-10-30 17:06:18 +1100524 mp->m_sync_seq++;
525 wake_up(&mp->m_wait_single_sync_task);
526}
527
528STATIC int
529xfssyncd(
530 void *arg)
531{
532 struct xfs_mount *mp = arg;
533 long timeleft;
534 bhv_vfs_sync_work_t *work, *n;
535 LIST_HEAD (tmp);
536
537 set_freezable();
538 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
539 for (;;) {
540 timeleft = schedule_timeout_interruptible(timeleft);
541 /* swsusp */
542 try_to_freeze();
543 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
544 break;
545
546 spin_lock(&mp->m_sync_lock);
547 /*
548 * We can get woken by laptop mode, to do a sync -
549 * that's the (only!) case where the list would be
550 * empty with time remaining.
551 */
552 if (!timeleft || list_empty(&mp->m_sync_list)) {
553 if (!timeleft)
554 timeleft = xfs_syncd_centisecs *
555 msecs_to_jiffies(10);
556 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
557 list_add_tail(&mp->m_sync_work.w_list,
558 &mp->m_sync_list);
559 }
560 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
561 list_move(&work->w_list, &tmp);
562 spin_unlock(&mp->m_sync_lock);
563
564 list_for_each_entry_safe(work, n, &tmp, w_list) {
565 (*work->w_syncer)(mp, work->w_data);
566 list_del(&work->w_list);
567 if (work == &mp->m_sync_work)
568 continue;
569 kmem_free(work);
570 }
571 }
572
573 return 0;
574}
575
576int
577xfs_syncd_init(
578 struct xfs_mount *mp)
579{
580 mp->m_sync_work.w_syncer = xfs_sync_worker;
581 mp->m_sync_work.w_mount = mp;
582 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
583 if (IS_ERR(mp->m_sync_task))
584 return -PTR_ERR(mp->m_sync_task);
585 return 0;
586}
587
588void
589xfs_syncd_stop(
590 struct xfs_mount *mp)
591{
592 kthread_stop(mp->m_sync_task);
593}
594
David Chinnerfce08f22008-10-30 17:37:03 +1100595int
David Chinner1dc33182008-10-30 17:37:15 +1100596xfs_reclaim_inode(
David Chinnerfce08f22008-10-30 17:37:03 +1100597 xfs_inode_t *ip,
598 int locked,
599 int sync_mode)
600{
601 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
602
603 /* The hash lock here protects a thread in xfs_iget_core from
604 * racing with us on linking the inode back with a vnode.
605 * Once we have the XFS_IRECLAIM flag set it will not touch
606 * us.
607 */
608 write_lock(&pag->pag_ici_lock);
609 spin_lock(&ip->i_flags_lock);
610 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
611 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
612 spin_unlock(&ip->i_flags_lock);
613 write_unlock(&pag->pag_ici_lock);
614 if (locked) {
615 xfs_ifunlock(ip);
616 xfs_iunlock(ip, XFS_ILOCK_EXCL);
617 }
618 return 1;
619 }
620 __xfs_iflags_set(ip, XFS_IRECLAIM);
621 spin_unlock(&ip->i_flags_lock);
622 write_unlock(&pag->pag_ici_lock);
623 xfs_put_perag(ip->i_mount, pag);
624
625 /*
626 * If the inode is still dirty, then flush it out. If the inode
627 * is not in the AIL, then it will be OK to flush it delwri as
628 * long as xfs_iflush() does not keep any references to the inode.
629 * We leave that decision up to xfs_iflush() since it has the
630 * knowledge of whether it's OK to simply do a delwri flush of
631 * the inode or whether we need to wait until the inode is
632 * pulled from the AIL.
633 * We get the flush lock regardless, though, just to make sure
634 * we don't free it while it is being flushed.
635 */
636 if (!locked) {
637 xfs_ilock(ip, XFS_ILOCK_EXCL);
638 xfs_iflock(ip);
639 }
640
641 /*
642 * In the case of a forced shutdown we rely on xfs_iflush() to
643 * wait for the inode to be unpinned before returning an error.
644 */
645 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
646 /* synchronize with xfs_iflush_done */
647 xfs_iflock(ip);
648 xfs_ifunlock(ip);
649 }
650
651 xfs_iunlock(ip, XFS_ILOCK_EXCL);
652 xfs_ireclaim(ip);
653 return 0;
654}
655
David Chinner11654512008-10-30 17:37:49 +1100656/*
657 * We set the inode flag atomically with the radix tree tag.
658 * Once we get tag lookups on the radix tree, this inode flag
659 * can go away.
660 */
David Chinner396beb82008-10-30 17:37:26 +1100661void
662xfs_inode_set_reclaim_tag(
663 xfs_inode_t *ip)
664{
665 xfs_mount_t *mp = ip->i_mount;
666 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
667
668 read_lock(&pag->pag_ici_lock);
669 spin_lock(&ip->i_flags_lock);
670 radix_tree_tag_set(&pag->pag_ici_root,
671 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
David Chinner11654512008-10-30 17:37:49 +1100672 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100673 spin_unlock(&ip->i_flags_lock);
674 read_unlock(&pag->pag_ici_lock);
675 xfs_put_perag(mp, pag);
676}
677
678void
679__xfs_inode_clear_reclaim_tag(
680 xfs_mount_t *mp,
681 xfs_perag_t *pag,
682 xfs_inode_t *ip)
683{
684 radix_tree_tag_clear(&pag->pag_ici_root,
685 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
686}
687
688void
689xfs_inode_clear_reclaim_tag(
690 xfs_inode_t *ip)
691{
692 xfs_mount_t *mp = ip->i_mount;
693 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
694
695 read_lock(&pag->pag_ici_lock);
696 spin_lock(&ip->i_flags_lock);
697 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
698 spin_unlock(&ip->i_flags_lock);
699 read_unlock(&pag->pag_ici_lock);
700 xfs_put_perag(mp, pag);
701}
702
David Chinner7a3be022008-10-30 17:37:37 +1100703
704STATIC void
705xfs_reclaim_inodes_ag(
706 xfs_mount_t *mp,
707 int ag,
708 int noblock,
709 int mode)
710{
711 xfs_inode_t *ip = NULL;
712 xfs_perag_t *pag = &mp->m_perag[ag];
713 int nr_found;
David Chinner8c38ab02008-10-30 17:38:00 +1100714 uint32_t first_index;
David Chinner7a3be022008-10-30 17:37:37 +1100715 int skipped;
716
717restart:
718 first_index = 0;
719 skipped = 0;
720 do {
721 /*
722 * use a gang lookup to find the next inode in the tree
723 * as the tree is sparse and a gang lookup walks to find
724 * the number of objects requested.
725 */
726 read_lock(&pag->pag_ici_lock);
727 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
728 (void**)&ip, first_index, 1,
729 XFS_ICI_RECLAIM_TAG);
730
731 if (!nr_found) {
732 read_unlock(&pag->pag_ici_lock);
733 break;
734 }
735
David Chinner8c38ab02008-10-30 17:38:00 +1100736 /*
737 * Update the index for the next lookup. Catch overflows
738 * into the next AG range which can occur if we have inodes
739 * in the last block of the AG and we are currently
740 * pointing to the last inode.
741 */
David Chinner7a3be022008-10-30 17:37:37 +1100742 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
David Chinner8c38ab02008-10-30 17:38:00 +1100743 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
744 read_unlock(&pag->pag_ici_lock);
745 break;
746 }
David Chinner7a3be022008-10-30 17:37:37 +1100747
748 ASSERT(xfs_iflags_test(ip, (XFS_IRECLAIMABLE|XFS_IRECLAIM)));
749
750 /* ignore if already under reclaim */
751 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
752 read_unlock(&pag->pag_ici_lock);
753 continue;
754 }
755
756 if (noblock) {
757 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
758 read_unlock(&pag->pag_ici_lock);
759 continue;
760 }
761 if (xfs_ipincount(ip) ||
762 !xfs_iflock_nowait(ip)) {
763 xfs_iunlock(ip, XFS_ILOCK_EXCL);
764 read_unlock(&pag->pag_ici_lock);
765 continue;
766 }
767 }
768 read_unlock(&pag->pag_ici_lock);
769
770 /*
771 * hmmm - this is an inode already in reclaim. Do
772 * we even bother catching it here?
773 */
774 if (xfs_reclaim_inode(ip, noblock, mode))
775 skipped++;
776 } while (nr_found);
777
778 if (skipped) {
779 delay(1);
780 goto restart;
781 }
782 return;
783
784}
785
David Chinnerfce08f22008-10-30 17:37:03 +1100786int
David Chinner1dc33182008-10-30 17:37:15 +1100787xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100788 xfs_mount_t *mp,
789 int noblock,
790 int mode)
791{
David Chinner7a3be022008-10-30 17:37:37 +1100792 int i;
David Chinnerfce08f22008-10-30 17:37:03 +1100793
David Chinner7a3be022008-10-30 17:37:37 +1100794 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
795 if (!mp->m_perag[i].pag_ici_init)
796 continue;
797 xfs_reclaim_inodes_ag(mp, i, noblock, mode);
David Chinnerfce08f22008-10-30 17:37:03 +1100798 }
David Chinnerfce08f22008-10-30 17:37:03 +1100799 return 0;
800}
801
802