]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/linux-2.6/xfs_sync.c
xfs: validate quota log items during log recovery
[net-next-2.6.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 61 int nr_found;
8c38ab03 62 uint32_t first_index = 0;
683a8970
DC
63 int error = 0;
64 int last_error = 0;
fe4fa4b8 65
fe4fa4b8 66 do {
bc60a993 67 struct inode *inode;
bc60a993 68 xfs_inode_t *ip = NULL;
455486b9 69 int lock_flags = XFS_ILOCK_SHARED;
bc60a993 70
fe4fa4b8 71 /*
683a8970
DC
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.
fe4fa4b8 75 */
683a8970
DC
76 read_lock(&pag->pag_ici_lock);
77 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
78 (void**)&ip, first_index, 1);
fe4fa4b8 79
683a8970
DC
80 if (!nr_found) {
81 read_unlock(&pag->pag_ici_lock);
82 break;
fe4fa4b8
DC
83 }
84
8c38ab03
DC
85 /*
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 */
683a8970 91 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
8c38ab03
DC
92 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
93 read_unlock(&pag->pag_ici_lock);
94 break;
95 }
fe4fa4b8 96
683a8970 97 /* nothing to sync during shutdown */
cb56a4b9 98 if (XFS_FORCED_SHUTDOWN(mp)) {
683a8970 99 read_unlock(&pag->pag_ici_lock);
fe4fa4b8
DC
100 return 0;
101 }
102
103 /*
455486b9
DC
104 * If we can't get a reference on the inode, it must be
105 * in reclaim. Leave it for the reclaim code to flush.
fe4fa4b8 106 */
455486b9
DC
107 inode = VFS_I(ip);
108 if (!igrab(inode)) {
683a8970 109 read_unlock(&pag->pag_ici_lock);
455486b9
DC
110 continue;
111 }
112 read_unlock(&pag->pag_ici_lock);
113
6307091f
DC
114 /* avoid new or bad inodes */
115 if (is_bad_inode(inode) ||
116 xfs_iflags_test(ip, XFS_INEW)) {
455486b9
DC
117 IRELE(ip);
118 continue;
fe4fa4b8 119 }
bc60a993 120
fe4fa4b8
DC
121 /*
122 * If we have to flush data or wait for I/O completion
455486b9 123 * we need to hold the iolock.
fe4fa4b8 124 */
a8d770d9
DC
125 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))
25e41b3d 142 xfs_ioend_wait(ip);
683a8970 143 }
455486b9 144 xfs_ilock(ip, XFS_ILOCK_SHARED);
fe4fa4b8 145
683a8970 146 if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
fe4fa4b8
DC
147 if (flags & SYNC_WAIT) {
148 xfs_iflock(ip);
683a8970
DC
149 if (!xfs_inode_clean(ip))
150 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
151 else
152 xfs_ifunlock(ip);
fe4fa4b8 153 } else if (xfs_iflock_nowait(ip)) {
683a8970
DC
154 if (!xfs_inode_clean(ip))
155 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
156 else
157 xfs_ifunlock(ip);
fe4fa4b8
DC
158 }
159 }
455486b9 160 xfs_iput(ip, lock_flags);
fe4fa4b8 161
683a8970 162 if (error)
fe4fa4b8 163 last_error = error;
fe4fa4b8
DC
164 /*
165 * bail out if the filesystem is corrupted.
166 */
683a8970 167 if (error == EFSCORRUPTED)
fe4fa4b8 168 return XFS_ERROR(error);
fe4fa4b8 169
683a8970 170 } while (nr_found);
fe4fa4b8 171
683a8970
DC
172 return last_error;
173}
fe4fa4b8 174
683a8970
DC
175int
176xfs_sync_inodes(
177 xfs_mount_t *mp,
2030b5ab 178 int flags)
683a8970
DC
179{
180 int error;
181 int last_error;
182 int i;
e9f1c6ee 183 int lflags = XFS_LOG_FORCE;
fe4fa4b8 184
683a8970
DC
185 if (mp->m_flags & XFS_MOUNT_RDONLY)
186 return 0;
187 error = 0;
188 last_error = 0;
fe4fa4b8 189
e9f1c6ee
DC
190 if (flags & SYNC_WAIT)
191 lflags |= XFS_LOG_SYNC;
192
683a8970
DC
193 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
194 if (!mp->m_perag[i].pag_ici_init)
195 continue;
2030b5ab 196 error = xfs_sync_inodes_ag(mp, i, flags);
683a8970
DC
197 if (error)
198 last_error = error;
199 if (error == EFSCORRUPTED)
200 break;
201 }
e9f1c6ee
DC
202 if (flags & SYNC_DELWRI)
203 xfs_log_force(mp, 0, lflags);
204
fe4fa4b8
DC
205 return XFS_ERROR(last_error);
206}
207
2af75df7
CH
208STATIC 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
e9f1c6ee 242int
2af75df7
CH
243xfs_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;
e9f1c6ee
DC
293}
294
295/*
a4e4c4f4
DC
296 * 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,
e9f1c6ee
DC
307 * 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,
a4e4c4f4
DC
309 * transactions can still occur here so don't bother flushing the buftarg
310 * because it'll just get dirty again.
e9f1c6ee
DC
311 */
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
a4e4c4f4 327 /* write superblock and hoover up shutdown errors */
e9f1c6ee
DC
328 error = xfs_sync_fsdata(mp, 0);
329
a4e4c4f4 330 /* flush data-only devices */
e9f1c6ee
DC
331 if (mp->m_rtdev_targp)
332 XFS_bflush(mp->m_rtdev_targp);
333
334 return error;
2af75df7
CH
335}
336
76bf105c
DC
337STATIC 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);
1dc3318a 344 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
76bf105c
DC
345
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
5e106572
FB
380 /*
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);
76bf105c
DC
385
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
a167b17e
DC
396/*
397 * 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,
e43afd72
DC
407 void (*syncer)(struct xfs_mount *, void *),
408 struct completion *completion)
a167b17e 409{
a8d770d9 410 struct xfs_sync_work *work;
a167b17e 411
a8d770d9 412 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
a167b17e
DC
413 INIT_LIST_HEAD(&work->w_list);
414 work->w_syncer = syncer;
415 work->w_data = data;
416 work->w_mount = mp;
e43afd72 417 work->w_completion = completion;
a167b17e
DC
418 spin_lock(&mp->m_sync_lock);
419 list_add_tail(&work->w_list, &mp->m_sync_list);
420 spin_unlock(&mp->m_sync_lock);
421 wake_up_process(mp->m_sync_task);
422}
423
424/*
425 * Flush delayed allocate data, attempting to free up reserved space
426 * from existing allocations. At this point a new allocation attempt
427 * has failed with ENOSPC and we are in the process of scratching our
428 * heads, looking about for more room...
429 */
430STATIC void
a8d770d9 431xfs_flush_inodes_work(
a167b17e
DC
432 struct xfs_mount *mp,
433 void *arg)
434{
435 struct inode *inode = arg;
a8d770d9
DC
436 xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK);
437 xfs_sync_inodes(mp, SYNC_DELWRI | SYNC_TRYLOCK | SYNC_IOWAIT);
a167b17e
DC
438 iput(inode);
439}
440
441void
a8d770d9 442xfs_flush_inodes(
a167b17e
DC
443 xfs_inode_t *ip)
444{
445 struct inode *inode = VFS_I(ip);
e43afd72 446 DECLARE_COMPLETION_ONSTACK(completion);
a167b17e
DC
447
448 igrab(inode);
e43afd72
DC
449 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
450 wait_for_completion(&completion);
a167b17e
DC
451 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
452}
453
aacaa880
DC
454/*
455 * Every sync period we need to unpin all items, reclaim inodes, sync
456 * quota and write out the superblock. We might need to cover the log
457 * to indicate it is idle.
458 */
a167b17e
DC
459STATIC void
460xfs_sync_worker(
461 struct xfs_mount *mp,
462 void *unused)
463{
464 int error;
465
aacaa880
DC
466 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
467 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
1dc3318a 468 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
aacaa880
DC
469 /* dgc: errors ignored here */
470 error = XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
471 error = xfs_sync_fsdata(mp, SYNC_BDFLUSH);
472 if (xfs_log_need_covered(mp))
473 error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
474 }
a167b17e
DC
475 mp->m_sync_seq++;
476 wake_up(&mp->m_wait_single_sync_task);
477}
478
479STATIC int
480xfssyncd(
481 void *arg)
482{
483 struct xfs_mount *mp = arg;
484 long timeleft;
a8d770d9 485 xfs_sync_work_t *work, *n;
a167b17e
DC
486 LIST_HEAD (tmp);
487
488 set_freezable();
489 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
490 for (;;) {
491 timeleft = schedule_timeout_interruptible(timeleft);
492 /* swsusp */
493 try_to_freeze();
494 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
495 break;
496
497 spin_lock(&mp->m_sync_lock);
498 /*
499 * We can get woken by laptop mode, to do a sync -
500 * that's the (only!) case where the list would be
501 * empty with time remaining.
502 */
503 if (!timeleft || list_empty(&mp->m_sync_list)) {
504 if (!timeleft)
505 timeleft = xfs_syncd_centisecs *
506 msecs_to_jiffies(10);
507 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
508 list_add_tail(&mp->m_sync_work.w_list,
509 &mp->m_sync_list);
510 }
511 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
512 list_move(&work->w_list, &tmp);
513 spin_unlock(&mp->m_sync_lock);
514
515 list_for_each_entry_safe(work, n, &tmp, w_list) {
516 (*work->w_syncer)(mp, work->w_data);
517 list_del(&work->w_list);
518 if (work == &mp->m_sync_work)
519 continue;
e43afd72
DC
520 if (work->w_completion)
521 complete(work->w_completion);
a167b17e
DC
522 kmem_free(work);
523 }
524 }
525
526 return 0;
527}
528
529int
530xfs_syncd_init(
531 struct xfs_mount *mp)
532{
533 mp->m_sync_work.w_syncer = xfs_sync_worker;
534 mp->m_sync_work.w_mount = mp;
e43afd72 535 mp->m_sync_work.w_completion = NULL;
a167b17e
DC
536 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
537 if (IS_ERR(mp->m_sync_task))
538 return -PTR_ERR(mp->m_sync_task);
539 return 0;
540}
541
542void
543xfs_syncd_stop(
544 struct xfs_mount *mp)
545{
546 kthread_stop(mp->m_sync_task);
547}
548
fce08f2f 549int
1dc3318a 550xfs_reclaim_inode(
fce08f2f
DC
551 xfs_inode_t *ip,
552 int locked,
553 int sync_mode)
554{
555 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
556
557 /* The hash lock here protects a thread in xfs_iget_core from
558 * racing with us on linking the inode back with a vnode.
559 * Once we have the XFS_IRECLAIM flag set it will not touch
560 * us.
561 */
562 write_lock(&pag->pag_ici_lock);
563 spin_lock(&ip->i_flags_lock);
564 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
565 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
566 spin_unlock(&ip->i_flags_lock);
567 write_unlock(&pag->pag_ici_lock);
568 if (locked) {
569 xfs_ifunlock(ip);
570 xfs_iunlock(ip, XFS_ILOCK_EXCL);
571 }
572 return 1;
573 }
574 __xfs_iflags_set(ip, XFS_IRECLAIM);
575 spin_unlock(&ip->i_flags_lock);
576 write_unlock(&pag->pag_ici_lock);
577 xfs_put_perag(ip->i_mount, pag);
578
579 /*
580 * If the inode is still dirty, then flush it out. If the inode
581 * is not in the AIL, then it will be OK to flush it delwri as
582 * long as xfs_iflush() does not keep any references to the inode.
583 * We leave that decision up to xfs_iflush() since it has the
584 * knowledge of whether it's OK to simply do a delwri flush of
585 * the inode or whether we need to wait until the inode is
586 * pulled from the AIL.
587 * We get the flush lock regardless, though, just to make sure
588 * we don't free it while it is being flushed.
589 */
590 if (!locked) {
591 xfs_ilock(ip, XFS_ILOCK_EXCL);
592 xfs_iflock(ip);
593 }
594
595 /*
596 * In the case of a forced shutdown we rely on xfs_iflush() to
597 * wait for the inode to be unpinned before returning an error.
598 */
599 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
600 /* synchronize with xfs_iflush_done */
601 xfs_iflock(ip);
602 xfs_ifunlock(ip);
603 }
604
605 xfs_iunlock(ip, XFS_ILOCK_EXCL);
606 xfs_ireclaim(ip);
607 return 0;
608}
609
11654513
DC
610/*
611 * We set the inode flag atomically with the radix tree tag.
612 * Once we get tag lookups on the radix tree, this inode flag
613 * can go away.
614 */
396beb85
DC
615void
616xfs_inode_set_reclaim_tag(
617 xfs_inode_t *ip)
618{
619 xfs_mount_t *mp = ip->i_mount;
620 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
621
622 read_lock(&pag->pag_ici_lock);
623 spin_lock(&ip->i_flags_lock);
624 radix_tree_tag_set(&pag->pag_ici_root,
625 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
11654513 626 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
396beb85
DC
627 spin_unlock(&ip->i_flags_lock);
628 read_unlock(&pag->pag_ici_lock);
629 xfs_put_perag(mp, pag);
630}
631
632void
633__xfs_inode_clear_reclaim_tag(
634 xfs_mount_t *mp,
635 xfs_perag_t *pag,
636 xfs_inode_t *ip)
637{
638 radix_tree_tag_clear(&pag->pag_ici_root,
639 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
640}
641
642void
643xfs_inode_clear_reclaim_tag(
644 xfs_inode_t *ip)
645{
646 xfs_mount_t *mp = ip->i_mount;
647 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
648
649 read_lock(&pag->pag_ici_lock);
650 spin_lock(&ip->i_flags_lock);
651 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
652 spin_unlock(&ip->i_flags_lock);
653 read_unlock(&pag->pag_ici_lock);
654 xfs_put_perag(mp, pag);
655}
656
7a3be02b
DC
657
658STATIC void
659xfs_reclaim_inodes_ag(
fce08f2f 660 xfs_mount_t *mp,
7a3be02b
DC
661 int ag,
662 int noblock,
fce08f2f
DC
663 int mode)
664{
7a3be02b
DC
665 xfs_inode_t *ip = NULL;
666 xfs_perag_t *pag = &mp->m_perag[ag];
667 int nr_found;
8c38ab03 668 uint32_t first_index;
7a3be02b 669 int skipped;
fce08f2f
DC
670
671restart:
7a3be02b
DC
672 first_index = 0;
673 skipped = 0;
674 do {
675 /*
676 * use a gang lookup to find the next inode in the tree
677 * as the tree is sparse and a gang lookup walks to find
678 * the number of objects requested.
679 */
680 read_lock(&pag->pag_ici_lock);
681 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
682 (void**)&ip, first_index, 1,
683 XFS_ICI_RECLAIM_TAG);
684
685 if (!nr_found) {
686 read_unlock(&pag->pag_ici_lock);
687 break;
688 }
689
8c38ab03
DC
690 /*
691 * Update the index for the next lookup. Catch overflows
692 * into the next AG range which can occur if we have inodes
693 * in the last block of the AG and we are currently
694 * pointing to the last inode.
695 */
7a3be02b 696 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
8c38ab03
DC
697 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
698 read_unlock(&pag->pag_ici_lock);
699 break;
700 }
7a3be02b 701
7a3be02b
DC
702 /* ignore if already under reclaim */
703 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
704 read_unlock(&pag->pag_ici_lock);
705 continue;
706 }
707
fce08f2f 708 if (noblock) {
7a3be02b
DC
709 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
710 read_unlock(&pag->pag_ici_lock);
fce08f2f 711 continue;
7a3be02b 712 }
fce08f2f
DC
713 if (xfs_ipincount(ip) ||
714 !xfs_iflock_nowait(ip)) {
715 xfs_iunlock(ip, XFS_ILOCK_EXCL);
7a3be02b 716 read_unlock(&pag->pag_ici_lock);
fce08f2f
DC
717 continue;
718 }
719 }
7a3be02b
DC
720 read_unlock(&pag->pag_ici_lock);
721
722 /*
723 * hmmm - this is an inode already in reclaim. Do
724 * we even bother catching it here?
725 */
1dc3318a 726 if (xfs_reclaim_inode(ip, noblock, mode))
7a3be02b
DC
727 skipped++;
728 } while (nr_found);
729
730 if (skipped) {
731 delay(1);
fce08f2f
DC
732 goto restart;
733 }
7a3be02b
DC
734 return;
735
736}
737
738int
739xfs_reclaim_inodes(
740 xfs_mount_t *mp,
741 int noblock,
742 int mode)
743{
744 int i;
745
746 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
747 if (!mp->m_perag[i].pag_ici_init)
748 continue;
749 xfs_reclaim_inodes_ag(mp, i, noblock, mode);
750 }
fce08f2f
DC
751 return 0;
752}
753
754