blob: 88caafc8ef1b35e9eb471387d3c263c4db12eaac [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;
David Chinner683a8972008-10-30 17:07:29 +110065
David Chinner683a8972008-10-30 17:07:29 +110066 do {
David Chinnerbc60a992008-10-30 17:15:03 +110067 struct inode *inode;
David Chinnerbc60a992008-10-30 17:15:03 +110068 xfs_inode_t *ip = NULL;
David Chinner455486b2008-10-30 18:03:14 +110069 int lock_flags = XFS_ILOCK_SHARED;
David Chinnerbc60a992008-10-30 17:15:03 +110070
David Chinner683a8972008-10-30 17:07:29 +110071 /*
72 * use a gang lookup to find the next inode in the tree
73 * as the tree is sparse and a gang lookup walks to find
74 * the number of objects requested.
75 */
76 read_lock(&pag->pag_ici_lock);
77 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
78 (void**)&ip, first_index, 1);
79
80 if (!nr_found) {
81 read_unlock(&pag->pag_ici_lock);
82 break;
83 }
84
David Chinner8c38ab02008-10-30 17:38:00 +110085 /*
86 * Update the index for the next lookup. Catch overflows
87 * into the next AG range which can occur if we have inodes
88 * in the last block of the AG and we are currently
89 * pointing to the last inode.
90 */
David Chinner683a8972008-10-30 17:07:29 +110091 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
David Chinner8c38ab02008-10-30 17:38:00 +110092 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
93 read_unlock(&pag->pag_ici_lock);
94 break;
95 }
David Chinner683a8972008-10-30 17:07:29 +110096
David Chinner683a8972008-10-30 17:07:29 +110097 /* nothing to sync during shutdown */
David Chinnercb56a4b2008-10-30 17:16:00 +110098 if (XFS_FORCED_SHUTDOWN(mp)) {
David Chinner683a8972008-10-30 17:07:29 +110099 read_unlock(&pag->pag_ici_lock);
100 return 0;
101 }
102
103 /*
David Chinner455486b2008-10-30 18:03:14 +1100104 * If we can't get a reference on the inode, it must be
105 * in reclaim. Leave it for the reclaim code to flush.
David Chinner683a8972008-10-30 17:07:29 +1100106 */
David Chinner455486b2008-10-30 18:03:14 +1100107 inode = VFS_I(ip);
108 if (!igrab(inode)) {
David Chinner683a8972008-10-30 17:07:29 +1100109 read_unlock(&pag->pag_ici_lock);
David Chinner455486b2008-10-30 18:03:14 +1100110 continue;
111 }
112 read_unlock(&pag->pag_ici_lock);
113
Dave Chinner63070912008-11-10 17:13:23 +1100114 /* avoid new or bad inodes */
115 if (is_bad_inode(inode) ||
116 xfs_iflags_test(ip, XFS_INEW)) {
David Chinner455486b2008-10-30 18:03:14 +1100117 IRELE(ip);
118 continue;
David Chinner683a8972008-10-30 17:07:29 +1100119 }
David Chinnerbc60a992008-10-30 17:15:03 +1100120
David Chinner683a8972008-10-30 17:07:29 +1100121 /*
122 * If we have to flush data or wait for I/O completion
David Chinner455486b2008-10-30 18:03:14 +1100123 * we need to hold the iolock.
David Chinner683a8972008-10-30 17:07:29 +1100124 */
Dave Chinnera8d770d2009-04-06 18:44:54 +0200125 if (flags & SYNC_DELWRI) {
126 if (VN_DIRTY(inode)) {
127 if (flags & SYNC_TRYLOCK) {
128 if (xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
129 lock_flags |= XFS_IOLOCK_SHARED;
130 } else {
131 xfs_ilock(ip, XFS_IOLOCK_SHARED);
132 lock_flags |= XFS_IOLOCK_SHARED;
133 }
134 if (lock_flags & XFS_IOLOCK_SHARED) {
135 error = xfs_flush_pages(ip, 0, -1,
136 (flags & SYNC_WAIT) ? 0
137 : XFS_B_ASYNC,
138 FI_NONE);
139 }
140 }
141 if (VN_CACHED(inode) && (flags & SYNC_IOWAIT))
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100142 xfs_ioend_wait(ip);
David Chinner683a8972008-10-30 17:07:29 +1100143 }
David Chinner455486b2008-10-30 18:03:14 +1100144 xfs_ilock(ip, XFS_ILOCK_SHARED);
David Chinner683a8972008-10-30 17:07:29 +1100145
146 if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
147 if (flags & SYNC_WAIT) {
148 xfs_iflock(ip);
149 if (!xfs_inode_clean(ip))
150 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
151 else
152 xfs_ifunlock(ip);
153 } else if (xfs_iflock_nowait(ip)) {
154 if (!xfs_inode_clean(ip))
155 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
156 else
157 xfs_ifunlock(ip);
David Chinner683a8972008-10-30 17:07:29 +1100158 }
159 }
David Chinner455486b2008-10-30 18:03:14 +1100160 xfs_iput(ip, lock_flags);
David Chinner683a8972008-10-30 17:07:29 +1100161
162 if (error)
163 last_error = error;
164 /*
165 * bail out if the filesystem is corrupted.
166 */
167 if (error == EFSCORRUPTED)
168 return XFS_ERROR(error);
169
170 } while (nr_found);
171
172 return last_error;
173}
174
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100175int
176xfs_sync_inodes(
177 xfs_mount_t *mp,
David Chinner2030b5a2008-10-30 17:15:12 +1100178 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100179{
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100180 int error;
181 int last_error;
David Chinner683a8972008-10-30 17:07:29 +1100182 int i;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100183 int lflags = XFS_LOG_FORCE;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100184
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100185 if (mp->m_flags & XFS_MOUNT_RDONLY)
186 return 0;
187 error = 0;
188 last_error = 0;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100189
David Chinnere9f1c6e2008-10-30 17:15:50 +1100190 if (flags & SYNC_WAIT)
191 lflags |= XFS_LOG_SYNC;
192
David Chinner683a8972008-10-30 17:07:29 +1100193 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
194 if (!mp->m_perag[i].pag_ici_init)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100195 continue;
David Chinner2030b5a2008-10-30 17:15:12 +1100196 error = xfs_sync_inodes_ag(mp, i, flags);
David Chinner683a8972008-10-30 17:07:29 +1100197 if (error)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100198 last_error = error;
David Chinner683a8972008-10-30 17:07:29 +1100199 if (error == EFSCORRUPTED)
200 break;
201 }
David Chinnere9f1c6e2008-10-30 17:15:50 +1100202 if (flags & SYNC_DELWRI)
203 xfs_log_force(mp, 0, lflags);
204
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100205 return XFS_ERROR(last_error);
206}
207
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100208STATIC int
209xfs_commit_dummy_trans(
210 struct xfs_mount *mp,
211 uint log_flags)
212{
213 struct xfs_inode *ip = mp->m_rootip;
214 struct xfs_trans *tp;
215 int error;
216
217 /*
218 * Put a dummy transaction in the log to tell recovery
219 * that all others are OK.
220 */
221 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
222 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
223 if (error) {
224 xfs_trans_cancel(tp, 0);
225 return error;
226 }
227
228 xfs_ilock(ip, XFS_ILOCK_EXCL);
229
230 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
231 xfs_trans_ihold(tp, ip);
232 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
233 /* XXX(hch): ignoring the error here.. */
234 error = xfs_trans_commit(tp, 0);
235
236 xfs_iunlock(ip, XFS_ILOCK_EXCL);
237
238 xfs_log_force(mp, 0, log_flags);
239 return 0;
240}
241
David Chinnere9f1c6e2008-10-30 17:15:50 +1100242int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100243xfs_sync_fsdata(
244 struct xfs_mount *mp,
245 int flags)
246{
247 struct xfs_buf *bp;
248 struct xfs_buf_log_item *bip;
249 int error = 0;
250
251 /*
252 * If this is xfssyncd() then only sync the superblock if we can
253 * lock it without sleeping and it is not pinned.
254 */
255 if (flags & SYNC_BDFLUSH) {
256 ASSERT(!(flags & SYNC_WAIT));
257
258 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
259 if (!bp)
260 goto out;
261
262 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
263 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
264 goto out_brelse;
265 } else {
266 bp = xfs_getsb(mp, 0);
267
268 /*
269 * If the buffer is pinned then push on the log so we won't
270 * get stuck waiting in the write for someone, maybe
271 * ourselves, to flush the log.
272 *
273 * Even though we just pushed the log above, we did not have
274 * the superblock buffer locked at that point so it can
275 * become pinned in between there and here.
276 */
277 if (XFS_BUF_ISPINNED(bp))
278 xfs_log_force(mp, 0, XFS_LOG_FORCE);
279 }
280
281
282 if (flags & SYNC_WAIT)
283 XFS_BUF_UNASYNC(bp);
284 else
285 XFS_BUF_ASYNC(bp);
286
287 return xfs_bwrite(mp, bp);
288
289 out_brelse:
290 xfs_buf_relse(bp);
291 out:
292 return error;
293}
294
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100295/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100296 * When remounting a filesystem read-only or freezing the filesystem, we have
297 * two phases to execute. This first phase is syncing the data before we
298 * quiesce the filesystem, and the second is flushing all the inodes out after
299 * we've waited for all the transactions created by the first phase to
300 * complete. The second phase ensures that the inodes are written to their
301 * location on disk rather than just existing in transactions in the log. This
302 * means after a quiesce there is no log replay required to write the inodes to
303 * disk (this is the main difference between a sync and a quiesce).
304 */
305/*
306 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100307 * so we flush delwri and delalloc buffers here, then wait for all I/O to
308 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100309 * transactions can still occur here so don't bother flushing the buftarg
310 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100311 */
312int
313xfs_quiesce_data(
314 struct xfs_mount *mp)
315{
316 int error;
317
318 /* push non-blocking */
319 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_BDFLUSH);
320 XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
321 xfs_filestream_flush(mp);
322
323 /* push and block */
324 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_WAIT|SYNC_IOWAIT);
325 XFS_QM_DQSYNC(mp, SYNC_WAIT);
326
David Chinnera4e4c4f2008-10-30 17:16:11 +1100327 /* write superblock and hoover up shutdown errors */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100328 error = xfs_sync_fsdata(mp, 0);
329
David Chinnera4e4c4f2008-10-30 17:16:11 +1100330 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100331 if (mp->m_rtdev_targp)
332 XFS_bflush(mp->m_rtdev_targp);
333
334 return error;
335}
336
David Chinner76bf1052008-10-30 17:16:21 +1100337STATIC void
338xfs_quiesce_fs(
339 struct xfs_mount *mp)
340{
341 int count = 0, pincount;
342
343 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner1dc33182008-10-30 17:37:15 +1100344 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinner76bf1052008-10-30 17:16:21 +1100345
346 /*
347 * This loop must run at least twice. The first instance of the loop
348 * will flush most meta data but that will generate more meta data
349 * (typically directory updates). Which then must be flushed and
350 * logged before we can write the unmount record.
351 */
352 do {
353 xfs_sync_inodes(mp, SYNC_ATTR|SYNC_WAIT);
354 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
355 if (!pincount) {
356 delay(50);
357 count++;
358 }
359 } while (count < 2);
360}
361
362/*
363 * Second stage of a quiesce. The data is already synced, now we have to take
364 * care of the metadata. New transactions are already blocked, so we need to
365 * wait for any remaining transactions to drain out before proceding.
366 */
367void
368xfs_quiesce_attr(
369 struct xfs_mount *mp)
370{
371 int error = 0;
372
373 /* wait for all modifications to complete */
374 while (atomic_read(&mp->m_active_trans) > 0)
375 delay(100);
376
377 /* flush inodes and push all remaining buffers out to disk */
378 xfs_quiesce_fs(mp);
379
Felix Blyakher5e106572009-01-22 21:34:05 -0600380 /*
381 * Just warn here till VFS can correctly support
382 * read-only remount without racing.
383 */
384 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100385
386 /* Push the superblock and write an unmount record */
387 error = xfs_log_sbcount(mp, 1);
388 if (error)
389 xfs_fs_cmn_err(CE_WARN, mp,
390 "xfs_attr_quiesce: failed to log sb changes. "
391 "Frozen image may not be consistent.");
392 xfs_log_unmount_write(mp);
393 xfs_unmountfs_writesb(mp);
394}
395
David Chinnere9f1c6e2008-10-30 17:15:50 +1100396/*
David Chinnera167b172008-10-30 17:06:18 +1100397 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
398 * Doing this has two advantages:
399 * - It saves on stack space, which is tight in certain situations
400 * - It can be used (with care) as a mechanism to avoid deadlocks.
401 * Flushing while allocating in a full filesystem requires both.
402 */
403STATIC void
404xfs_syncd_queue_work(
405 struct xfs_mount *mp,
406 void *data,
407 void (*syncer)(struct xfs_mount *, void *))
408{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200409 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100410
Dave Chinnera8d770d2009-04-06 18:44:54 +0200411 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100412 INIT_LIST_HEAD(&work->w_list);
413 work->w_syncer = syncer;
414 work->w_data = data;
415 work->w_mount = mp;
416 spin_lock(&mp->m_sync_lock);
417 list_add_tail(&work->w_list, &mp->m_sync_list);
418 spin_unlock(&mp->m_sync_lock);
419 wake_up_process(mp->m_sync_task);
420}
421
422/*
423 * Flush delayed allocate data, attempting to free up reserved space
424 * from existing allocations. At this point a new allocation attempt
425 * has failed with ENOSPC and we are in the process of scratching our
426 * heads, looking about for more room...
427 */
428STATIC void
429xfs_flush_inode_work(
430 struct xfs_mount *mp,
431 void *arg)
432{
433 struct inode *inode = arg;
434 filemap_flush(inode->i_mapping);
435 iput(inode);
436}
437
438void
439xfs_flush_inode(
440 xfs_inode_t *ip)
441{
442 struct inode *inode = VFS_I(ip);
443
444 igrab(inode);
445 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inode_work);
446 delay(msecs_to_jiffies(500));
447}
448
449/*
450 * This is the "bigger hammer" version of xfs_flush_inode_work...
451 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
452 */
453STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200454xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100455 struct xfs_mount *mp,
456 void *arg)
457{
458 struct inode *inode = arg;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200459 xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK);
460 xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK | SYNC_IOWAIT);
David Chinnera167b172008-10-30 17:06:18 +1100461 iput(inode);
462}
463
464void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200465xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100466 xfs_inode_t *ip)
467{
468 struct inode *inode = VFS_I(ip);
469
470 igrab(inode);
Dave Chinnera8d770d2009-04-06 18:44:54 +0200471 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work);
David Chinnera167b172008-10-30 17:06:18 +1100472 delay(msecs_to_jiffies(500));
473 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
474}
475
David Chinneraacaa882008-10-30 17:15:29 +1100476/*
477 * Every sync period we need to unpin all items, reclaim inodes, sync
478 * quota and write out the superblock. We might need to cover the log
479 * to indicate it is idle.
480 */
David Chinnera167b172008-10-30 17:06:18 +1100481STATIC void
482xfs_sync_worker(
483 struct xfs_mount *mp,
484 void *unused)
485{
486 int error;
487
David Chinneraacaa882008-10-30 17:15:29 +1100488 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
489 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
David Chinner1dc33182008-10-30 17:37:15 +1100490 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinneraacaa882008-10-30 17:15:29 +1100491 /* dgc: errors ignored here */
492 error = XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
493 error = xfs_sync_fsdata(mp, SYNC_BDFLUSH);
494 if (xfs_log_need_covered(mp))
495 error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
496 }
David Chinnera167b172008-10-30 17:06:18 +1100497 mp->m_sync_seq++;
498 wake_up(&mp->m_wait_single_sync_task);
499}
500
501STATIC int
502xfssyncd(
503 void *arg)
504{
505 struct xfs_mount *mp = arg;
506 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200507 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100508 LIST_HEAD (tmp);
509
510 set_freezable();
511 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
512 for (;;) {
513 timeleft = schedule_timeout_interruptible(timeleft);
514 /* swsusp */
515 try_to_freeze();
516 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
517 break;
518
519 spin_lock(&mp->m_sync_lock);
520 /*
521 * We can get woken by laptop mode, to do a sync -
522 * that's the (only!) case where the list would be
523 * empty with time remaining.
524 */
525 if (!timeleft || list_empty(&mp->m_sync_list)) {
526 if (!timeleft)
527 timeleft = xfs_syncd_centisecs *
528 msecs_to_jiffies(10);
529 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
530 list_add_tail(&mp->m_sync_work.w_list,
531 &mp->m_sync_list);
532 }
533 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
534 list_move(&work->w_list, &tmp);
535 spin_unlock(&mp->m_sync_lock);
536
537 list_for_each_entry_safe(work, n, &tmp, w_list) {
538 (*work->w_syncer)(mp, work->w_data);
539 list_del(&work->w_list);
540 if (work == &mp->m_sync_work)
541 continue;
542 kmem_free(work);
543 }
544 }
545
546 return 0;
547}
548
549int
550xfs_syncd_init(
551 struct xfs_mount *mp)
552{
553 mp->m_sync_work.w_syncer = xfs_sync_worker;
554 mp->m_sync_work.w_mount = mp;
555 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
556 if (IS_ERR(mp->m_sync_task))
557 return -PTR_ERR(mp->m_sync_task);
558 return 0;
559}
560
561void
562xfs_syncd_stop(
563 struct xfs_mount *mp)
564{
565 kthread_stop(mp->m_sync_task);
566}
567
David Chinnerfce08f22008-10-30 17:37:03 +1100568int
David Chinner1dc33182008-10-30 17:37:15 +1100569xfs_reclaim_inode(
David Chinnerfce08f22008-10-30 17:37:03 +1100570 xfs_inode_t *ip,
571 int locked,
572 int sync_mode)
573{
574 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
575
576 /* The hash lock here protects a thread in xfs_iget_core from
577 * racing with us on linking the inode back with a vnode.
578 * Once we have the XFS_IRECLAIM flag set it will not touch
579 * us.
580 */
581 write_lock(&pag->pag_ici_lock);
582 spin_lock(&ip->i_flags_lock);
583 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
584 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
585 spin_unlock(&ip->i_flags_lock);
586 write_unlock(&pag->pag_ici_lock);
587 if (locked) {
588 xfs_ifunlock(ip);
589 xfs_iunlock(ip, XFS_ILOCK_EXCL);
590 }
591 return 1;
592 }
593 __xfs_iflags_set(ip, XFS_IRECLAIM);
594 spin_unlock(&ip->i_flags_lock);
595 write_unlock(&pag->pag_ici_lock);
596 xfs_put_perag(ip->i_mount, pag);
597
598 /*
599 * If the inode is still dirty, then flush it out. If the inode
600 * is not in the AIL, then it will be OK to flush it delwri as
601 * long as xfs_iflush() does not keep any references to the inode.
602 * We leave that decision up to xfs_iflush() since it has the
603 * knowledge of whether it's OK to simply do a delwri flush of
604 * the inode or whether we need to wait until the inode is
605 * pulled from the AIL.
606 * We get the flush lock regardless, though, just to make sure
607 * we don't free it while it is being flushed.
608 */
609 if (!locked) {
610 xfs_ilock(ip, XFS_ILOCK_EXCL);
611 xfs_iflock(ip);
612 }
613
614 /*
615 * In the case of a forced shutdown we rely on xfs_iflush() to
616 * wait for the inode to be unpinned before returning an error.
617 */
618 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
619 /* synchronize with xfs_iflush_done */
620 xfs_iflock(ip);
621 xfs_ifunlock(ip);
622 }
623
624 xfs_iunlock(ip, XFS_ILOCK_EXCL);
625 xfs_ireclaim(ip);
626 return 0;
627}
628
David Chinner11654512008-10-30 17:37:49 +1100629/*
630 * We set the inode flag atomically with the radix tree tag.
631 * Once we get tag lookups on the radix tree, this inode flag
632 * can go away.
633 */
David Chinner396beb82008-10-30 17:37:26 +1100634void
635xfs_inode_set_reclaim_tag(
636 xfs_inode_t *ip)
637{
638 xfs_mount_t *mp = ip->i_mount;
639 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
640
641 read_lock(&pag->pag_ici_lock);
642 spin_lock(&ip->i_flags_lock);
643 radix_tree_tag_set(&pag->pag_ici_root,
644 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
David Chinner11654512008-10-30 17:37:49 +1100645 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100646 spin_unlock(&ip->i_flags_lock);
647 read_unlock(&pag->pag_ici_lock);
648 xfs_put_perag(mp, pag);
649}
650
651void
652__xfs_inode_clear_reclaim_tag(
653 xfs_mount_t *mp,
654 xfs_perag_t *pag,
655 xfs_inode_t *ip)
656{
657 radix_tree_tag_clear(&pag->pag_ici_root,
658 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
659}
660
661void
662xfs_inode_clear_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 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
671 spin_unlock(&ip->i_flags_lock);
672 read_unlock(&pag->pag_ici_lock);
673 xfs_put_perag(mp, pag);
674}
675
David Chinner7a3be022008-10-30 17:37:37 +1100676
677STATIC void
678xfs_reclaim_inodes_ag(
679 xfs_mount_t *mp,
680 int ag,
681 int noblock,
682 int mode)
683{
684 xfs_inode_t *ip = NULL;
685 xfs_perag_t *pag = &mp->m_perag[ag];
686 int nr_found;
David Chinner8c38ab02008-10-30 17:38:00 +1100687 uint32_t first_index;
David Chinner7a3be022008-10-30 17:37:37 +1100688 int skipped;
689
690restart:
691 first_index = 0;
692 skipped = 0;
693 do {
694 /*
695 * use a gang lookup to find the next inode in the tree
696 * as the tree is sparse and a gang lookup walks to find
697 * the number of objects requested.
698 */
699 read_lock(&pag->pag_ici_lock);
700 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
701 (void**)&ip, first_index, 1,
702 XFS_ICI_RECLAIM_TAG);
703
704 if (!nr_found) {
705 read_unlock(&pag->pag_ici_lock);
706 break;
707 }
708
David Chinner8c38ab02008-10-30 17:38:00 +1100709 /*
710 * Update the index for the next lookup. Catch overflows
711 * into the next AG range which can occur if we have inodes
712 * in the last block of the AG and we are currently
713 * pointing to the last inode.
714 */
David Chinner7a3be022008-10-30 17:37:37 +1100715 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
David Chinner8c38ab02008-10-30 17:38:00 +1100716 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
717 read_unlock(&pag->pag_ici_lock);
718 break;
719 }
David Chinner7a3be022008-10-30 17:37:37 +1100720
David Chinner7a3be022008-10-30 17:37:37 +1100721 /* ignore if already under reclaim */
722 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
723 read_unlock(&pag->pag_ici_lock);
724 continue;
725 }
726
727 if (noblock) {
728 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
729 read_unlock(&pag->pag_ici_lock);
730 continue;
731 }
732 if (xfs_ipincount(ip) ||
733 !xfs_iflock_nowait(ip)) {
734 xfs_iunlock(ip, XFS_ILOCK_EXCL);
735 read_unlock(&pag->pag_ici_lock);
736 continue;
737 }
738 }
739 read_unlock(&pag->pag_ici_lock);
740
741 /*
742 * hmmm - this is an inode already in reclaim. Do
743 * we even bother catching it here?
744 */
745 if (xfs_reclaim_inode(ip, noblock, mode))
746 skipped++;
747 } while (nr_found);
748
749 if (skipped) {
750 delay(1);
751 goto restart;
752 }
753 return;
754
755}
756
David Chinnerfce08f22008-10-30 17:37:03 +1100757int
David Chinner1dc33182008-10-30 17:37:15 +1100758xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100759 xfs_mount_t *mp,
760 int noblock,
761 int mode)
762{
David Chinner7a3be022008-10-30 17:37:37 +1100763 int i;
David Chinnerfce08f22008-10-30 17:37:03 +1100764
David Chinner7a3be022008-10-30 17:37:37 +1100765 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
766 if (!mp->m_perag[i].pag_ici_init)
767 continue;
768 xfs_reclaim_inodes_ag(mp, i, noblock, mode);
David Chinnerfce08f22008-10-30 17:37:03 +1100769 }
David Chinnerfce08f22008-10-30 17:37:03 +1100770 return 0;
771}
772
773