xfs: mark inodes dirty before issuing I/O
[deliverable/linux.git] / fs / xfs / linux-2.6 / xfs_sync.c
CommitLineData
fe4fa4b8
DC
1/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_bmap_btree.h"
31#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
33#include "xfs_btree.h"
34#include "xfs_dir2_sf.h"
35#include "xfs_attr_sf.h"
36#include "xfs_inode.h"
37#include "xfs_dinode.h"
38#include "xfs_error.h"
39#include "xfs_mru_cache.h"
40#include "xfs_filestream.h"
41#include "xfs_vnodeops.h"
42#include "xfs_utils.h"
43#include "xfs_buf_item.h"
44#include "xfs_inode_item.h"
45#include "xfs_rw.h"
7d095257 46#include "xfs_quota.h"
fe4fa4b8 47
a167b17e
DC
48#include <linux/kthread.h>
49#include <linux/freezer.h>
50
5a34d5cd 51
75f3cb13
DC
52STATIC xfs_inode_t *
53xfs_inode_ag_lookup(
54 struct xfs_mount *mp,
55 struct xfs_perag *pag,
56 uint32_t *first_index,
57 int tag)
58{
59 int nr_found;
60 struct xfs_inode *ip;
61
62 /*
63 * use a gang lookup to find the next inode in the tree
64 * as the tree is sparse and a gang lookup walks to find
65 * the number of objects requested.
66 */
67 read_lock(&pag->pag_ici_lock);
68 if (tag == XFS_ICI_NO_TAG) {
69 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
70 (void **)&ip, *first_index, 1);
71 } else {
72 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
73 (void **)&ip, *first_index, 1, tag);
74 }
75 if (!nr_found)
76 goto unlock;
77
78 /*
79 * Update the index for the next lookup. Catch overflows
80 * into the next AG range which can occur if we have inodes
81 * in the last block of the AG and we are currently
82 * pointing to the last inode.
83 */
84 *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
85 if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
86 goto unlock;
87
88 return ip;
89
90unlock:
91 read_unlock(&pag->pag_ici_lock);
92 return NULL;
93}
94
95STATIC int
96xfs_inode_ag_walk(
97 struct xfs_mount *mp,
98 xfs_agnumber_t ag,
99 int (*execute)(struct xfs_inode *ip,
100 struct xfs_perag *pag, int flags),
101 int flags,
102 int tag)
103{
104 struct xfs_perag *pag = &mp->m_perag[ag];
105 uint32_t first_index;
106 int last_error = 0;
107 int skipped;
108
109restart:
110 skipped = 0;
111 first_index = 0;
112 do {
113 int error = 0;
114 xfs_inode_t *ip;
115
116 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
117 if (!ip)
118 break;
119
120 error = execute(ip, pag, flags);
121 if (error == EAGAIN) {
122 skipped++;
123 continue;
124 }
125 if (error)
126 last_error = error;
127 /*
128 * bail out if the filesystem is corrupted.
129 */
130 if (error == EFSCORRUPTED)
131 break;
132
133 } while (1);
134
135 if (skipped) {
136 delay(1);
137 goto restart;
138 }
139
140 xfs_put_perag(mp, pag);
141 return last_error;
142}
143
fe588ed3 144int
75f3cb13
DC
145xfs_inode_ag_iterator(
146 struct xfs_mount *mp,
147 int (*execute)(struct xfs_inode *ip,
148 struct xfs_perag *pag, int flags),
149 int flags,
150 int tag)
151{
152 int error = 0;
153 int last_error = 0;
154 xfs_agnumber_t ag;
155
156 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
157 if (!mp->m_perag[ag].pag_ici_init)
158 continue;
159 error = xfs_inode_ag_walk(mp, ag, execute, flags, tag);
160 if (error) {
161 last_error = error;
162 if (error == EFSCORRUPTED)
163 break;
164 }
165 }
166 return XFS_ERROR(last_error);
167}
168
1da8eeca 169/* must be called with pag_ici_lock held and releases it */
fe588ed3 170int
1da8eeca
DC
171xfs_sync_inode_valid(
172 struct xfs_inode *ip,
173 struct xfs_perag *pag)
174{
175 struct inode *inode = VFS_I(ip);
176
177 /* nothing to sync during shutdown */
178 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
179 read_unlock(&pag->pag_ici_lock);
180 return EFSCORRUPTED;
181 }
182
183 /*
184 * If we can't get a reference on the inode, it must be in reclaim.
185 * Leave it for the reclaim code to flush. Also avoid inodes that
186 * haven't been fully initialised.
187 */
188 if (!igrab(inode)) {
189 read_unlock(&pag->pag_ici_lock);
190 return ENOENT;
191 }
192 read_unlock(&pag->pag_ici_lock);
193
194 if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) {
195 IRELE(ip);
196 return ENOENT;
197 }
198
199 return 0;
200}
201
5a34d5cd
DC
202STATIC int
203xfs_sync_inode_data(
204 struct xfs_inode *ip,
75f3cb13 205 struct xfs_perag *pag,
5a34d5cd
DC
206 int flags)
207{
208 struct inode *inode = VFS_I(ip);
209 struct address_space *mapping = inode->i_mapping;
210 int error = 0;
211
75f3cb13
DC
212 error = xfs_sync_inode_valid(ip, pag);
213 if (error)
214 return error;
215
5a34d5cd
DC
216 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
217 goto out_wait;
218
219 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
220 if (flags & SYNC_TRYLOCK)
221 goto out_wait;
222 xfs_ilock(ip, XFS_IOLOCK_SHARED);
223 }
224
225 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
226 0 : XFS_B_ASYNC, FI_NONE);
227 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
228
229 out_wait:
b0710ccc 230 if (flags & SYNC_WAIT)
5a34d5cd 231 xfs_ioend_wait(ip);
75f3cb13 232 IRELE(ip);
5a34d5cd
DC
233 return error;
234}
235
845b6d0c
CH
236STATIC int
237xfs_sync_inode_attr(
238 struct xfs_inode *ip,
75f3cb13 239 struct xfs_perag *pag,
845b6d0c
CH
240 int flags)
241{
242 int error = 0;
243
75f3cb13
DC
244 error = xfs_sync_inode_valid(ip, pag);
245 if (error)
246 return error;
247
845b6d0c
CH
248 xfs_ilock(ip, XFS_ILOCK_SHARED);
249 if (xfs_inode_clean(ip))
250 goto out_unlock;
251 if (!xfs_iflock_nowait(ip)) {
252 if (!(flags & SYNC_WAIT))
253 goto out_unlock;
254 xfs_iflock(ip);
255 }
256
257 if (xfs_inode_clean(ip)) {
258 xfs_ifunlock(ip);
259 goto out_unlock;
260 }
261
262 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
263 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
264
265 out_unlock:
266 xfs_iunlock(ip, XFS_ILOCK_SHARED);
75f3cb13 267 IRELE(ip);
845b6d0c
CH
268 return error;
269}
270
075fe102
CH
271/*
272 * Write out pagecache data for the whole filesystem.
273 */
683a8970 274int
075fe102
CH
275xfs_sync_data(
276 struct xfs_mount *mp,
277 int flags)
683a8970 278{
075fe102 279 int error;
fe4fa4b8 280
b0710ccc 281 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
fe4fa4b8 282
075fe102
CH
283 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
284 XFS_ICI_NO_TAG);
285 if (error)
286 return XFS_ERROR(error);
e9f1c6ee 287
075fe102
CH
288 xfs_log_force(mp, 0,
289 (flags & SYNC_WAIT) ?
290 XFS_LOG_FORCE | XFS_LOG_SYNC :
291 XFS_LOG_FORCE);
292 return 0;
293}
e9f1c6ee 294
075fe102
CH
295/*
296 * Write out inode metadata (attributes) for the whole filesystem.
297 */
298int
299xfs_sync_attr(
300 struct xfs_mount *mp,
301 int flags)
302{
303 ASSERT((flags & ~SYNC_WAIT) == 0);
75f3cb13 304
075fe102
CH
305 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
306 XFS_ICI_NO_TAG);
fe4fa4b8
DC
307}
308
2af75df7
CH
309STATIC int
310xfs_commit_dummy_trans(
311 struct xfs_mount *mp,
312 uint log_flags)
313{
314 struct xfs_inode *ip = mp->m_rootip;
315 struct xfs_trans *tp;
316 int error;
317
318 /*
319 * Put a dummy transaction in the log to tell recovery
320 * that all others are OK.
321 */
322 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
323 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
324 if (error) {
325 xfs_trans_cancel(tp, 0);
326 return error;
327 }
328
329 xfs_ilock(ip, XFS_ILOCK_EXCL);
330
331 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
332 xfs_trans_ihold(tp, ip);
333 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
334 /* XXX(hch): ignoring the error here.. */
335 error = xfs_trans_commit(tp, 0);
336
337 xfs_iunlock(ip, XFS_ILOCK_EXCL);
338
339 xfs_log_force(mp, 0, log_flags);
340 return 0;
341}
342
e9f1c6ee 343int
2af75df7
CH
344xfs_sync_fsdata(
345 struct xfs_mount *mp,
346 int flags)
347{
348 struct xfs_buf *bp;
349 struct xfs_buf_log_item *bip;
350 int error = 0;
351
352 /*
353 * If this is xfssyncd() then only sync the superblock if we can
354 * lock it without sleeping and it is not pinned.
355 */
8b5403a6 356 if (flags & SYNC_TRYLOCK) {
2af75df7
CH
357 ASSERT(!(flags & SYNC_WAIT));
358
359 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
360 if (!bp)
361 goto out;
362
363 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
364 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
365 goto out_brelse;
366 } else {
367 bp = xfs_getsb(mp, 0);
368
369 /*
370 * If the buffer is pinned then push on the log so we won't
371 * get stuck waiting in the write for someone, maybe
372 * ourselves, to flush the log.
373 *
374 * Even though we just pushed the log above, we did not have
375 * the superblock buffer locked at that point so it can
376 * become pinned in between there and here.
377 */
378 if (XFS_BUF_ISPINNED(bp))
379 xfs_log_force(mp, 0, XFS_LOG_FORCE);
380 }
381
382
383 if (flags & SYNC_WAIT)
384 XFS_BUF_UNASYNC(bp);
385 else
386 XFS_BUF_ASYNC(bp);
387
388 return xfs_bwrite(mp, bp);
389
390 out_brelse:
391 xfs_buf_relse(bp);
392 out:
393 return error;
e9f1c6ee
DC
394}
395
396/*
a4e4c4f4
DC
397 * When remounting a filesystem read-only or freezing the filesystem, we have
398 * two phases to execute. This first phase is syncing the data before we
399 * quiesce the filesystem, and the second is flushing all the inodes out after
400 * we've waited for all the transactions created by the first phase to
401 * complete. The second phase ensures that the inodes are written to their
402 * location on disk rather than just existing in transactions in the log. This
403 * means after a quiesce there is no log replay required to write the inodes to
404 * disk (this is the main difference between a sync and a quiesce).
405 */
406/*
407 * First stage of freeze - no writers will make progress now we are here,
e9f1c6ee
DC
408 * so we flush delwri and delalloc buffers here, then wait for all I/O to
409 * complete. Data is frozen at that point. Metadata is not frozen,
a4e4c4f4
DC
410 * transactions can still occur here so don't bother flushing the buftarg
411 * because it'll just get dirty again.
e9f1c6ee
DC
412 */
413int
414xfs_quiesce_data(
415 struct xfs_mount *mp)
416{
417 int error;
418
419 /* push non-blocking */
075fe102 420 xfs_sync_data(mp, 0);
8b5403a6 421 xfs_qm_sync(mp, SYNC_TRYLOCK);
e9f1c6ee 422
c90b07e8 423 /* push and block till complete */
b0710ccc 424 xfs_sync_data(mp, SYNC_WAIT);
7d095257 425 xfs_qm_sync(mp, SYNC_WAIT);
e9f1c6ee 426
c90b07e8
DC
427 /* drop inode references pinned by filestreams */
428 xfs_filestream_flush(mp);
429
a4e4c4f4 430 /* write superblock and hoover up shutdown errors */
c90b07e8 431 error = xfs_sync_fsdata(mp, SYNC_WAIT);
e9f1c6ee 432
a4e4c4f4 433 /* flush data-only devices */
e9f1c6ee
DC
434 if (mp->m_rtdev_targp)
435 XFS_bflush(mp->m_rtdev_targp);
436
437 return error;
2af75df7
CH
438}
439
76bf105c
DC
440STATIC void
441xfs_quiesce_fs(
442 struct xfs_mount *mp)
443{
444 int count = 0, pincount;
445
446 xfs_flush_buftarg(mp->m_ddev_targp, 0);
abc10647 447 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
76bf105c
DC
448
449 /*
450 * This loop must run at least twice. The first instance of the loop
451 * will flush most meta data but that will generate more meta data
452 * (typically directory updates). Which then must be flushed and
453 * logged before we can write the unmount record.
454 */
455 do {
075fe102 456 xfs_sync_attr(mp, SYNC_WAIT);
76bf105c
DC
457 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
458 if (!pincount) {
459 delay(50);
460 count++;
461 }
462 } while (count < 2);
463}
464
465/*
466 * Second stage of a quiesce. The data is already synced, now we have to take
467 * care of the metadata. New transactions are already blocked, so we need to
468 * wait for any remaining transactions to drain out before proceding.
469 */
470void
471xfs_quiesce_attr(
472 struct xfs_mount *mp)
473{
474 int error = 0;
475
476 /* wait for all modifications to complete */
477 while (atomic_read(&mp->m_active_trans) > 0)
478 delay(100);
479
480 /* flush inodes and push all remaining buffers out to disk */
481 xfs_quiesce_fs(mp);
482
5e106572
FB
483 /*
484 * Just warn here till VFS can correctly support
485 * read-only remount without racing.
486 */
487 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
76bf105c
DC
488
489 /* Push the superblock and write an unmount record */
490 error = xfs_log_sbcount(mp, 1);
491 if (error)
492 xfs_fs_cmn_err(CE_WARN, mp,
493 "xfs_attr_quiesce: failed to log sb changes. "
494 "Frozen image may not be consistent.");
495 xfs_log_unmount_write(mp);
496 xfs_unmountfs_writesb(mp);
497}
498
a167b17e
DC
499/*
500 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
501 * Doing this has two advantages:
502 * - It saves on stack space, which is tight in certain situations
503 * - It can be used (with care) as a mechanism to avoid deadlocks.
504 * Flushing while allocating in a full filesystem requires both.
505 */
506STATIC void
507xfs_syncd_queue_work(
508 struct xfs_mount *mp,
509 void *data,
e43afd72
DC
510 void (*syncer)(struct xfs_mount *, void *),
511 struct completion *completion)
a167b17e 512{
a8d770d9 513 struct xfs_sync_work *work;
a167b17e 514
a8d770d9 515 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
a167b17e
DC
516 INIT_LIST_HEAD(&work->w_list);
517 work->w_syncer = syncer;
518 work->w_data = data;
519 work->w_mount = mp;
e43afd72 520 work->w_completion = completion;
a167b17e
DC
521 spin_lock(&mp->m_sync_lock);
522 list_add_tail(&work->w_list, &mp->m_sync_list);
523 spin_unlock(&mp->m_sync_lock);
524 wake_up_process(mp->m_sync_task);
525}
526
527/*
528 * Flush delayed allocate data, attempting to free up reserved space
529 * from existing allocations. At this point a new allocation attempt
530 * has failed with ENOSPC and we are in the process of scratching our
531 * heads, looking about for more room...
532 */
533STATIC void
a8d770d9 534xfs_flush_inodes_work(
a167b17e
DC
535 struct xfs_mount *mp,
536 void *arg)
537{
538 struct inode *inode = arg;
075fe102 539 xfs_sync_data(mp, SYNC_TRYLOCK);
b0710ccc 540 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
a167b17e
DC
541 iput(inode);
542}
543
544void
a8d770d9 545xfs_flush_inodes(
a167b17e
DC
546 xfs_inode_t *ip)
547{
548 struct inode *inode = VFS_I(ip);
e43afd72 549 DECLARE_COMPLETION_ONSTACK(completion);
a167b17e
DC
550
551 igrab(inode);
e43afd72
DC
552 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
553 wait_for_completion(&completion);
a167b17e
DC
554 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
555}
556
aacaa880
DC
557/*
558 * Every sync period we need to unpin all items, reclaim inodes, sync
559 * quota and write out the superblock. We might need to cover the log
560 * to indicate it is idle.
561 */
a167b17e
DC
562STATIC void
563xfs_sync_worker(
564 struct xfs_mount *mp,
565 void *unused)
566{
567 int error;
568
aacaa880
DC
569 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
570 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
abc10647 571 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
aacaa880 572 /* dgc: errors ignored here */
8b5403a6
CH
573 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
574 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
aacaa880
DC
575 if (xfs_log_need_covered(mp))
576 error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
577 }
a167b17e
DC
578 mp->m_sync_seq++;
579 wake_up(&mp->m_wait_single_sync_task);
580}
581
582STATIC int
583xfssyncd(
584 void *arg)
585{
586 struct xfs_mount *mp = arg;
587 long timeleft;
a8d770d9 588 xfs_sync_work_t *work, *n;
a167b17e
DC
589 LIST_HEAD (tmp);
590
591 set_freezable();
592 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
593 for (;;) {
594 timeleft = schedule_timeout_interruptible(timeleft);
595 /* swsusp */
596 try_to_freeze();
597 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
598 break;
599
600 spin_lock(&mp->m_sync_lock);
601 /*
602 * We can get woken by laptop mode, to do a sync -
603 * that's the (only!) case where the list would be
604 * empty with time remaining.
605 */
606 if (!timeleft || list_empty(&mp->m_sync_list)) {
607 if (!timeleft)
608 timeleft = xfs_syncd_centisecs *
609 msecs_to_jiffies(10);
610 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
611 list_add_tail(&mp->m_sync_work.w_list,
612 &mp->m_sync_list);
613 }
614 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
615 list_move(&work->w_list, &tmp);
616 spin_unlock(&mp->m_sync_lock);
617
618 list_for_each_entry_safe(work, n, &tmp, w_list) {
619 (*work->w_syncer)(mp, work->w_data);
620 list_del(&work->w_list);
621 if (work == &mp->m_sync_work)
622 continue;
e43afd72
DC
623 if (work->w_completion)
624 complete(work->w_completion);
a167b17e
DC
625 kmem_free(work);
626 }
627 }
628
629 return 0;
630}
631
632int
633xfs_syncd_init(
634 struct xfs_mount *mp)
635{
636 mp->m_sync_work.w_syncer = xfs_sync_worker;
637 mp->m_sync_work.w_mount = mp;
e43afd72 638 mp->m_sync_work.w_completion = NULL;
a167b17e
DC
639 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
640 if (IS_ERR(mp->m_sync_task))
641 return -PTR_ERR(mp->m_sync_task);
642 return 0;
643}
644
645void
646xfs_syncd_stop(
647 struct xfs_mount *mp)
648{
649 kthread_stop(mp->m_sync_task);
650}
651
fce08f2f 652int
1dc3318a 653xfs_reclaim_inode(
fce08f2f
DC
654 xfs_inode_t *ip,
655 int locked,
656 int sync_mode)
657{
658 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
659
660 /* The hash lock here protects a thread in xfs_iget_core from
661 * racing with us on linking the inode back with a vnode.
662 * Once we have the XFS_IRECLAIM flag set it will not touch
663 * us.
664 */
665 write_lock(&pag->pag_ici_lock);
666 spin_lock(&ip->i_flags_lock);
667 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
668 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
669 spin_unlock(&ip->i_flags_lock);
670 write_unlock(&pag->pag_ici_lock);
671 if (locked) {
672 xfs_ifunlock(ip);
673 xfs_iunlock(ip, XFS_ILOCK_EXCL);
674 }
75f3cb13 675 return -EAGAIN;
fce08f2f
DC
676 }
677 __xfs_iflags_set(ip, XFS_IRECLAIM);
678 spin_unlock(&ip->i_flags_lock);
679 write_unlock(&pag->pag_ici_lock);
680 xfs_put_perag(ip->i_mount, pag);
681
682 /*
683 * If the inode is still dirty, then flush it out. If the inode
684 * is not in the AIL, then it will be OK to flush it delwri as
685 * long as xfs_iflush() does not keep any references to the inode.
686 * We leave that decision up to xfs_iflush() since it has the
687 * knowledge of whether it's OK to simply do a delwri flush of
688 * the inode or whether we need to wait until the inode is
689 * pulled from the AIL.
690 * We get the flush lock regardless, though, just to make sure
691 * we don't free it while it is being flushed.
692 */
693 if (!locked) {
694 xfs_ilock(ip, XFS_ILOCK_EXCL);
695 xfs_iflock(ip);
696 }
697
698 /*
699 * In the case of a forced shutdown we rely on xfs_iflush() to
700 * wait for the inode to be unpinned before returning an error.
701 */
702 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
703 /* synchronize with xfs_iflush_done */
704 xfs_iflock(ip);
705 xfs_ifunlock(ip);
706 }
707
708 xfs_iunlock(ip, XFS_ILOCK_EXCL);
709 xfs_ireclaim(ip);
710 return 0;
711}
712
bc990f5c
CH
713void
714__xfs_inode_set_reclaim_tag(
715 struct xfs_perag *pag,
716 struct xfs_inode *ip)
717{
718 radix_tree_tag_set(&pag->pag_ici_root,
719 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
720 XFS_ICI_RECLAIM_TAG);
721}
722
11654513
DC
723/*
724 * We set the inode flag atomically with the radix tree tag.
725 * Once we get tag lookups on the radix tree, this inode flag
726 * can go away.
727 */
396beb85
DC
728void
729xfs_inode_set_reclaim_tag(
730 xfs_inode_t *ip)
731{
732 xfs_mount_t *mp = ip->i_mount;
733 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
734
735 read_lock(&pag->pag_ici_lock);
736 spin_lock(&ip->i_flags_lock);
bc990f5c 737 __xfs_inode_set_reclaim_tag(pag, ip);
11654513 738 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
396beb85
DC
739 spin_unlock(&ip->i_flags_lock);
740 read_unlock(&pag->pag_ici_lock);
741 xfs_put_perag(mp, pag);
742}
743
744void
745__xfs_inode_clear_reclaim_tag(
746 xfs_mount_t *mp,
747 xfs_perag_t *pag,
748 xfs_inode_t *ip)
749{
750 radix_tree_tag_clear(&pag->pag_ici_root,
751 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
752}
753
75f3cb13
DC
754STATIC int
755xfs_reclaim_inode_now(
756 struct xfs_inode *ip,
757 struct xfs_perag *pag,
758 int flags)
fce08f2f 759{
75f3cb13
DC
760 /* ignore if already under reclaim */
761 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
7a3be02b 762 read_unlock(&pag->pag_ici_lock);
75f3cb13 763 return 0;
fce08f2f 764 }
75f3cb13 765 read_unlock(&pag->pag_ici_lock);
7a3be02b 766
75f3cb13 767 return xfs_reclaim_inode(ip, 0, flags);
7a3be02b
DC
768}
769
770int
771xfs_reclaim_inodes(
772 xfs_mount_t *mp,
7a3be02b
DC
773 int mode)
774{
75f3cb13
DC
775 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode_now, mode,
776 XFS_ICI_RECLAIM_TAG);
fce08f2f 777}
This page took 0.094383 seconds and 5 git commands to generate.