blob: fb5cca3df840e6811ee3d6713ed0b6cd38b5343c [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;
David Chinner683a8972008-10-30 17:07:29 +110066
67 if (flags & SYNC_DELWRI)
68 fflag = XFS_B_DELWRI;
69 if (flags & SYNC_WAIT)
70 fflag = 0; /* synchronous overrides all */
71
David Chinner683a8972008-10-30 17:07:29 +110072 do {
David Chinnerbc60a992008-10-30 17:15:03 +110073 struct inode *inode;
David Chinnerbc60a992008-10-30 17:15:03 +110074 xfs_inode_t *ip = NULL;
David Chinner455486b2008-10-30 18:03:14 +110075 int lock_flags = XFS_ILOCK_SHARED;
David Chinnerbc60a992008-10-30 17:15:03 +110076
David Chinner683a8972008-10-30 17:07:29 +110077 /*
78 * use a gang lookup to find the next inode in the tree
79 * as the tree is sparse and a gang lookup walks to find
80 * the number of objects requested.
81 */
82 read_lock(&pag->pag_ici_lock);
83 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
84 (void**)&ip, first_index, 1);
85
86 if (!nr_found) {
87 read_unlock(&pag->pag_ici_lock);
88 break;
89 }
90
David Chinner8c38ab02008-10-30 17:38:00 +110091 /*
92 * Update the index for the next lookup. Catch overflows
93 * into the next AG range which can occur if we have inodes
94 * in the last block of the AG and we are currently
95 * pointing to the last inode.
96 */
David Chinner683a8972008-10-30 17:07:29 +110097 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
David Chinner8c38ab02008-10-30 17:38:00 +110098 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
99 read_unlock(&pag->pag_ici_lock);
100 break;
101 }
David Chinner683a8972008-10-30 17:07:29 +1100102
David Chinner683a8972008-10-30 17:07:29 +1100103 /* nothing to sync during shutdown */
David Chinnercb56a4b2008-10-30 17:16:00 +1100104 if (XFS_FORCED_SHUTDOWN(mp)) {
David Chinner683a8972008-10-30 17:07:29 +1100105 read_unlock(&pag->pag_ici_lock);
106 return 0;
107 }
108
109 /*
David Chinner455486b2008-10-30 18:03:14 +1100110 * If we can't get a reference on the inode, it must be
111 * in reclaim. Leave it for the reclaim code to flush.
David Chinner683a8972008-10-30 17:07:29 +1100112 */
David Chinner455486b2008-10-30 18:03:14 +1100113 inode = VFS_I(ip);
114 if (!igrab(inode)) {
David Chinner683a8972008-10-30 17:07:29 +1100115 read_unlock(&pag->pag_ici_lock);
David Chinner455486b2008-10-30 18:03:14 +1100116 continue;
117 }
118 read_unlock(&pag->pag_ici_lock);
119
120 /* bad inodes are dealt with elsewhere */
121 if (is_bad_inode(inode)) {
122 IRELE(ip);
123 continue;
David Chinner683a8972008-10-30 17:07:29 +1100124 }
David Chinnerbc60a992008-10-30 17:15:03 +1100125
David Chinner683a8972008-10-30 17:07:29 +1100126 /*
127 * If we have to flush data or wait for I/O completion
David Chinner455486b2008-10-30 18:03:14 +1100128 * we need to hold the iolock.
David Chinner683a8972008-10-30 17:07:29 +1100129 */
David Chinnerbc60a992008-10-30 17:15:03 +1100130 if ((flags & SYNC_DELWRI) && VN_DIRTY(inode)) {
David Chinner455486b2008-10-30 18:03:14 +1100131 xfs_ilock(ip, XFS_IOLOCK_SHARED);
132 lock_flags |= XFS_IOLOCK_SHARED;
David Chinner683a8972008-10-30 17:07:29 +1100133 error = xfs_flush_pages(ip, 0, -1, fflag, FI_NONE);
134 if (flags & SYNC_IOWAIT)
135 vn_iowait(ip);
David Chinner683a8972008-10-30 17:07:29 +1100136 }
David Chinner455486b2008-10-30 18:03:14 +1100137 xfs_ilock(ip, XFS_ILOCK_SHARED);
David Chinner683a8972008-10-30 17:07:29 +1100138
139 if ((flags & SYNC_ATTR) && !xfs_inode_clean(ip)) {
140 if (flags & SYNC_WAIT) {
141 xfs_iflock(ip);
142 if (!xfs_inode_clean(ip))
143 error = xfs_iflush(ip, XFS_IFLUSH_SYNC);
144 else
145 xfs_ifunlock(ip);
146 } else if (xfs_iflock_nowait(ip)) {
147 if (!xfs_inode_clean(ip))
148 error = xfs_iflush(ip, XFS_IFLUSH_DELWRI);
149 else
150 xfs_ifunlock(ip);
David Chinner683a8972008-10-30 17:07:29 +1100151 }
152 }
David Chinner455486b2008-10-30 18:03:14 +1100153 xfs_iput(ip, lock_flags);
David Chinner683a8972008-10-30 17:07:29 +1100154
155 if (error)
156 last_error = error;
157 /*
158 * bail out if the filesystem is corrupted.
159 */
160 if (error == EFSCORRUPTED)
161 return XFS_ERROR(error);
162
163 } while (nr_found);
164
165 return last_error;
166}
167
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100168int
169xfs_sync_inodes(
170 xfs_mount_t *mp,
David Chinner2030b5a2008-10-30 17:15:12 +1100171 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100172{
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100173 int error;
174 int last_error;
David Chinner683a8972008-10-30 17:07:29 +1100175 int i;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100176 int lflags = XFS_LOG_FORCE;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100177
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100178 if (mp->m_flags & XFS_MOUNT_RDONLY)
179 return 0;
180 error = 0;
181 last_error = 0;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100182
David Chinnere9f1c6e2008-10-30 17:15:50 +1100183 if (flags & SYNC_WAIT)
184 lflags |= XFS_LOG_SYNC;
185
David Chinner683a8972008-10-30 17:07:29 +1100186 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
187 if (!mp->m_perag[i].pag_ici_init)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100188 continue;
David Chinner2030b5a2008-10-30 17:15:12 +1100189 error = xfs_sync_inodes_ag(mp, i, flags);
David Chinner683a8972008-10-30 17:07:29 +1100190 if (error)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100191 last_error = error;
David Chinner683a8972008-10-30 17:07:29 +1100192 if (error == EFSCORRUPTED)
193 break;
194 }
David Chinnere9f1c6e2008-10-30 17:15:50 +1100195 if (flags & SYNC_DELWRI)
196 xfs_log_force(mp, 0, lflags);
197
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100198 return XFS_ERROR(last_error);
199}
200
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100201STATIC int
202xfs_commit_dummy_trans(
203 struct xfs_mount *mp,
204 uint log_flags)
205{
206 struct xfs_inode *ip = mp->m_rootip;
207 struct xfs_trans *tp;
208 int error;
209
210 /*
211 * Put a dummy transaction in the log to tell recovery
212 * that all others are OK.
213 */
214 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
215 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
216 if (error) {
217 xfs_trans_cancel(tp, 0);
218 return error;
219 }
220
221 xfs_ilock(ip, XFS_ILOCK_EXCL);
222
223 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
224 xfs_trans_ihold(tp, ip);
225 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
226 /* XXX(hch): ignoring the error here.. */
227 error = xfs_trans_commit(tp, 0);
228
229 xfs_iunlock(ip, XFS_ILOCK_EXCL);
230
231 xfs_log_force(mp, 0, log_flags);
232 return 0;
233}
234
David Chinnere9f1c6e2008-10-30 17:15:50 +1100235int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100236xfs_sync_fsdata(
237 struct xfs_mount *mp,
238 int flags)
239{
240 struct xfs_buf *bp;
241 struct xfs_buf_log_item *bip;
242 int error = 0;
243
244 /*
245 * If this is xfssyncd() then only sync the superblock if we can
246 * lock it without sleeping and it is not pinned.
247 */
248 if (flags & SYNC_BDFLUSH) {
249 ASSERT(!(flags & SYNC_WAIT));
250
251 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
252 if (!bp)
253 goto out;
254
255 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
256 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
257 goto out_brelse;
258 } else {
259 bp = xfs_getsb(mp, 0);
260
261 /*
262 * If the buffer is pinned then push on the log so we won't
263 * get stuck waiting in the write for someone, maybe
264 * ourselves, to flush the log.
265 *
266 * Even though we just pushed the log above, we did not have
267 * the superblock buffer locked at that point so it can
268 * become pinned in between there and here.
269 */
270 if (XFS_BUF_ISPINNED(bp))
271 xfs_log_force(mp, 0, XFS_LOG_FORCE);
272 }
273
274
275 if (flags & SYNC_WAIT)
276 XFS_BUF_UNASYNC(bp);
277 else
278 XFS_BUF_ASYNC(bp);
279
280 return xfs_bwrite(mp, bp);
281
282 out_brelse:
283 xfs_buf_relse(bp);
284 out:
285 return error;
286}
287
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100288/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100289 * When remounting a filesystem read-only or freezing the filesystem, we have
290 * two phases to execute. This first phase is syncing the data before we
291 * quiesce the filesystem, and the second is flushing all the inodes out after
292 * we've waited for all the transactions created by the first phase to
293 * complete. The second phase ensures that the inodes are written to their
294 * location on disk rather than just existing in transactions in the log. This
295 * means after a quiesce there is no log replay required to write the inodes to
296 * disk (this is the main difference between a sync and a quiesce).
297 */
298/*
299 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100300 * so we flush delwri and delalloc buffers here, then wait for all I/O to
301 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100302 * transactions can still occur here so don't bother flushing the buftarg
303 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100304 */
305int
306xfs_quiesce_data(
307 struct xfs_mount *mp)
308{
309 int error;
310
311 /* push non-blocking */
312 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_BDFLUSH);
313 XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
314 xfs_filestream_flush(mp);
315
316 /* push and block */
317 xfs_sync_inodes(mp, SYNC_DELWRI|SYNC_WAIT|SYNC_IOWAIT);
318 XFS_QM_DQSYNC(mp, SYNC_WAIT);
319
David Chinnera4e4c4f2008-10-30 17:16:11 +1100320 /* write superblock and hoover up shutdown errors */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100321 error = xfs_sync_fsdata(mp, 0);
322
David Chinnera4e4c4f2008-10-30 17:16:11 +1100323 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100324 if (mp->m_rtdev_targp)
325 XFS_bflush(mp->m_rtdev_targp);
326
327 return error;
328}
329
David Chinner76bf1052008-10-30 17:16:21 +1100330STATIC void
331xfs_quiesce_fs(
332 struct xfs_mount *mp)
333{
334 int count = 0, pincount;
335
336 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner1dc33182008-10-30 17:37:15 +1100337 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinner76bf1052008-10-30 17:16:21 +1100338
339 /*
340 * This loop must run at least twice. The first instance of the loop
341 * will flush most meta data but that will generate more meta data
342 * (typically directory updates). Which then must be flushed and
343 * logged before we can write the unmount record.
344 */
345 do {
346 xfs_sync_inodes(mp, SYNC_ATTR|SYNC_WAIT);
347 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
348 if (!pincount) {
349 delay(50);
350 count++;
351 }
352 } while (count < 2);
353}
354
355/*
356 * Second stage of a quiesce. The data is already synced, now we have to take
357 * care of the metadata. New transactions are already blocked, so we need to
358 * wait for any remaining transactions to drain out before proceding.
359 */
360void
361xfs_quiesce_attr(
362 struct xfs_mount *mp)
363{
364 int error = 0;
365
366 /* wait for all modifications to complete */
367 while (atomic_read(&mp->m_active_trans) > 0)
368 delay(100);
369
370 /* flush inodes and push all remaining buffers out to disk */
371 xfs_quiesce_fs(mp);
372
373 ASSERT_ALWAYS(atomic_read(&mp->m_active_trans) == 0);
374
375 /* Push the superblock and write an unmount record */
376 error = xfs_log_sbcount(mp, 1);
377 if (error)
378 xfs_fs_cmn_err(CE_WARN, mp,
379 "xfs_attr_quiesce: failed to log sb changes. "
380 "Frozen image may not be consistent.");
381 xfs_log_unmount_write(mp);
382 xfs_unmountfs_writesb(mp);
383}
384
David Chinnere9f1c6e2008-10-30 17:15:50 +1100385/*
David Chinnera167b172008-10-30 17:06:18 +1100386 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
387 * Doing this has two advantages:
388 * - It saves on stack space, which is tight in certain situations
389 * - It can be used (with care) as a mechanism to avoid deadlocks.
390 * Flushing while allocating in a full filesystem requires both.
391 */
392STATIC void
393xfs_syncd_queue_work(
394 struct xfs_mount *mp,
395 void *data,
396 void (*syncer)(struct xfs_mount *, void *))
397{
398 struct bhv_vfs_sync_work *work;
399
400 work = kmem_alloc(sizeof(struct bhv_vfs_sync_work), KM_SLEEP);
401 INIT_LIST_HEAD(&work->w_list);
402 work->w_syncer = syncer;
403 work->w_data = data;
404 work->w_mount = mp;
405 spin_lock(&mp->m_sync_lock);
406 list_add_tail(&work->w_list, &mp->m_sync_list);
407 spin_unlock(&mp->m_sync_lock);
408 wake_up_process(mp->m_sync_task);
409}
410
411/*
412 * Flush delayed allocate data, attempting to free up reserved space
413 * from existing allocations. At this point a new allocation attempt
414 * has failed with ENOSPC and we are in the process of scratching our
415 * heads, looking about for more room...
416 */
417STATIC void
418xfs_flush_inode_work(
419 struct xfs_mount *mp,
420 void *arg)
421{
422 struct inode *inode = arg;
423 filemap_flush(inode->i_mapping);
424 iput(inode);
425}
426
427void
428xfs_flush_inode(
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_inode_work);
435 delay(msecs_to_jiffies(500));
436}
437
438/*
439 * This is the "bigger hammer" version of xfs_flush_inode_work...
440 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
441 */
442STATIC void
443xfs_flush_device_work(
444 struct xfs_mount *mp,
445 void *arg)
446{
447 struct inode *inode = arg;
448 sync_blockdev(mp->m_super->s_bdev);
449 iput(inode);
450}
451
452void
453xfs_flush_device(
454 xfs_inode_t *ip)
455{
456 struct inode *inode = VFS_I(ip);
457
458 igrab(inode);
459 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_device_work);
460 delay(msecs_to_jiffies(500));
461 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
462}
463
David Chinneraacaa882008-10-30 17:15:29 +1100464/*
465 * Every sync period we need to unpin all items, reclaim inodes, sync
466 * quota and write out the superblock. We might need to cover the log
467 * to indicate it is idle.
468 */
David Chinnera167b172008-10-30 17:06:18 +1100469STATIC void
470xfs_sync_worker(
471 struct xfs_mount *mp,
472 void *unused)
473{
474 int error;
475
David Chinneraacaa882008-10-30 17:15:29 +1100476 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
477 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
David Chinner1dc33182008-10-30 17:37:15 +1100478 xfs_reclaim_inodes(mp, 0, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinneraacaa882008-10-30 17:15:29 +1100479 /* dgc: errors ignored here */
480 error = XFS_QM_DQSYNC(mp, SYNC_BDFLUSH);
481 error = xfs_sync_fsdata(mp, SYNC_BDFLUSH);
482 if (xfs_log_need_covered(mp))
483 error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
484 }
David Chinnera167b172008-10-30 17:06:18 +1100485 mp->m_sync_seq++;
486 wake_up(&mp->m_wait_single_sync_task);
487}
488
489STATIC int
490xfssyncd(
491 void *arg)
492{
493 struct xfs_mount *mp = arg;
494 long timeleft;
495 bhv_vfs_sync_work_t *work, *n;
496 LIST_HEAD (tmp);
497
498 set_freezable();
499 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
500 for (;;) {
501 timeleft = schedule_timeout_interruptible(timeleft);
502 /* swsusp */
503 try_to_freeze();
504 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
505 break;
506
507 spin_lock(&mp->m_sync_lock);
508 /*
509 * We can get woken by laptop mode, to do a sync -
510 * that's the (only!) case where the list would be
511 * empty with time remaining.
512 */
513 if (!timeleft || list_empty(&mp->m_sync_list)) {
514 if (!timeleft)
515 timeleft = xfs_syncd_centisecs *
516 msecs_to_jiffies(10);
517 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
518 list_add_tail(&mp->m_sync_work.w_list,
519 &mp->m_sync_list);
520 }
521 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
522 list_move(&work->w_list, &tmp);
523 spin_unlock(&mp->m_sync_lock);
524
525 list_for_each_entry_safe(work, n, &tmp, w_list) {
526 (*work->w_syncer)(mp, work->w_data);
527 list_del(&work->w_list);
528 if (work == &mp->m_sync_work)
529 continue;
530 kmem_free(work);
531 }
532 }
533
534 return 0;
535}
536
537int
538xfs_syncd_init(
539 struct xfs_mount *mp)
540{
541 mp->m_sync_work.w_syncer = xfs_sync_worker;
542 mp->m_sync_work.w_mount = mp;
543 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
544 if (IS_ERR(mp->m_sync_task))
545 return -PTR_ERR(mp->m_sync_task);
546 return 0;
547}
548
549void
550xfs_syncd_stop(
551 struct xfs_mount *mp)
552{
553 kthread_stop(mp->m_sync_task);
554}
555
David Chinnerfce08f22008-10-30 17:37:03 +1100556int
David Chinner1dc33182008-10-30 17:37:15 +1100557xfs_reclaim_inode(
David Chinnerfce08f22008-10-30 17:37:03 +1100558 xfs_inode_t *ip,
559 int locked,
560 int sync_mode)
561{
562 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
563
564 /* The hash lock here protects a thread in xfs_iget_core from
565 * racing with us on linking the inode back with a vnode.
566 * Once we have the XFS_IRECLAIM flag set it will not touch
567 * us.
568 */
569 write_lock(&pag->pag_ici_lock);
570 spin_lock(&ip->i_flags_lock);
571 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
572 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
573 spin_unlock(&ip->i_flags_lock);
574 write_unlock(&pag->pag_ici_lock);
575 if (locked) {
576 xfs_ifunlock(ip);
577 xfs_iunlock(ip, XFS_ILOCK_EXCL);
578 }
579 return 1;
580 }
581 __xfs_iflags_set(ip, XFS_IRECLAIM);
582 spin_unlock(&ip->i_flags_lock);
583 write_unlock(&pag->pag_ici_lock);
584 xfs_put_perag(ip->i_mount, pag);
585
586 /*
587 * If the inode is still dirty, then flush it out. If the inode
588 * is not in the AIL, then it will be OK to flush it delwri as
589 * long as xfs_iflush() does not keep any references to the inode.
590 * We leave that decision up to xfs_iflush() since it has the
591 * knowledge of whether it's OK to simply do a delwri flush of
592 * the inode or whether we need to wait until the inode is
593 * pulled from the AIL.
594 * We get the flush lock regardless, though, just to make sure
595 * we don't free it while it is being flushed.
596 */
597 if (!locked) {
598 xfs_ilock(ip, XFS_ILOCK_EXCL);
599 xfs_iflock(ip);
600 }
601
602 /*
603 * In the case of a forced shutdown we rely on xfs_iflush() to
604 * wait for the inode to be unpinned before returning an error.
605 */
606 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
607 /* synchronize with xfs_iflush_done */
608 xfs_iflock(ip);
609 xfs_ifunlock(ip);
610 }
611
612 xfs_iunlock(ip, XFS_ILOCK_EXCL);
613 xfs_ireclaim(ip);
614 return 0;
615}
616
David Chinner11654512008-10-30 17:37:49 +1100617/*
618 * We set the inode flag atomically with the radix tree tag.
619 * Once we get tag lookups on the radix tree, this inode flag
620 * can go away.
621 */
David Chinner396beb82008-10-30 17:37:26 +1100622void
623xfs_inode_set_reclaim_tag(
624 xfs_inode_t *ip)
625{
626 xfs_mount_t *mp = ip->i_mount;
627 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
628
629 read_lock(&pag->pag_ici_lock);
630 spin_lock(&ip->i_flags_lock);
631 radix_tree_tag_set(&pag->pag_ici_root,
632 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
David Chinner11654512008-10-30 17:37:49 +1100633 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100634 spin_unlock(&ip->i_flags_lock);
635 read_unlock(&pag->pag_ici_lock);
636 xfs_put_perag(mp, pag);
637}
638
639void
640__xfs_inode_clear_reclaim_tag(
641 xfs_mount_t *mp,
642 xfs_perag_t *pag,
643 xfs_inode_t *ip)
644{
645 radix_tree_tag_clear(&pag->pag_ici_root,
646 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
647}
648
649void
650xfs_inode_clear_reclaim_tag(
651 xfs_inode_t *ip)
652{
653 xfs_mount_t *mp = ip->i_mount;
654 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
655
656 read_lock(&pag->pag_ici_lock);
657 spin_lock(&ip->i_flags_lock);
658 __xfs_inode_clear_reclaim_tag(mp, pag, ip);
659 spin_unlock(&ip->i_flags_lock);
660 read_unlock(&pag->pag_ici_lock);
661 xfs_put_perag(mp, pag);
662}
663
David Chinner7a3be022008-10-30 17:37:37 +1100664
665STATIC void
666xfs_reclaim_inodes_ag(
667 xfs_mount_t *mp,
668 int ag,
669 int noblock,
670 int mode)
671{
672 xfs_inode_t *ip = NULL;
673 xfs_perag_t *pag = &mp->m_perag[ag];
674 int nr_found;
David Chinner8c38ab02008-10-30 17:38:00 +1100675 uint32_t first_index;
David Chinner7a3be022008-10-30 17:37:37 +1100676 int skipped;
677
678restart:
679 first_index = 0;
680 skipped = 0;
681 do {
682 /*
683 * use a gang lookup to find the next inode in the tree
684 * as the tree is sparse and a gang lookup walks to find
685 * the number of objects requested.
686 */
687 read_lock(&pag->pag_ici_lock);
688 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
689 (void**)&ip, first_index, 1,
690 XFS_ICI_RECLAIM_TAG);
691
692 if (!nr_found) {
693 read_unlock(&pag->pag_ici_lock);
694 break;
695 }
696
David Chinner8c38ab02008-10-30 17:38:00 +1100697 /*
698 * Update the index for the next lookup. Catch overflows
699 * into the next AG range which can occur if we have inodes
700 * in the last block of the AG and we are currently
701 * pointing to the last inode.
702 */
David Chinner7a3be022008-10-30 17:37:37 +1100703 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
David Chinner8c38ab02008-10-30 17:38:00 +1100704 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) {
705 read_unlock(&pag->pag_ici_lock);
706 break;
707 }
David Chinner7a3be022008-10-30 17:37:37 +1100708
709 ASSERT(xfs_iflags_test(ip, (XFS_IRECLAIMABLE|XFS_IRECLAIM)));
710
711 /* ignore if already under reclaim */
712 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
713 read_unlock(&pag->pag_ici_lock);
714 continue;
715 }
716
717 if (noblock) {
718 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
719 read_unlock(&pag->pag_ici_lock);
720 continue;
721 }
722 if (xfs_ipincount(ip) ||
723 !xfs_iflock_nowait(ip)) {
724 xfs_iunlock(ip, XFS_ILOCK_EXCL);
725 read_unlock(&pag->pag_ici_lock);
726 continue;
727 }
728 }
729 read_unlock(&pag->pag_ici_lock);
730
731 /*
732 * hmmm - this is an inode already in reclaim. Do
733 * we even bother catching it here?
734 */
735 if (xfs_reclaim_inode(ip, noblock, mode))
736 skipped++;
737 } while (nr_found);
738
739 if (skipped) {
740 delay(1);
741 goto restart;
742 }
743 return;
744
745}
746
David Chinnerfce08f22008-10-30 17:37:03 +1100747int
David Chinner1dc33182008-10-30 17:37:15 +1100748xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100749 xfs_mount_t *mp,
750 int noblock,
751 int mode)
752{
David Chinner7a3be022008-10-30 17:37:37 +1100753 int i;
David Chinnerfce08f22008-10-30 17:37:03 +1100754
David Chinner7a3be022008-10-30 17:37:37 +1100755 for (i = 0; i < mp->m_sb.sb_agcount; i++) {
756 if (!mp->m_perag[i].pag_ici_init)
757 continue;
758 xfs_reclaim_inodes_ag(mp, i, noblock, mode);
David Chinnerfce08f22008-10-30 17:37:03 +1100759 }
David Chinnerfce08f22008-10-30 17:37:03 +1100760 return 0;
761}
762
763