blob: 320be6aea4924ed6fdcf4ac1e581c0c616d04c83 [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"
Christoph Hellwig7d095252009-06-08 15:33:32 +020046#include "xfs_quota.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110047
David Chinnera167b172008-10-30 17:06:18 +110048#include <linux/kthread.h>
49#include <linux/freezer.h>
50
Dave Chinner5a34d5c2009-06-08 15:35:03 +020051
Dave Chinner75f3cb12009-06-08 15:35:14 +020052STATIC 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
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200144int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200145xfs_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
Dave Chinner1da8eec2009-06-08 15:35:07 +0200169/* must be called with pag_ici_lock held and releases it */
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200170int
Dave Chinner1da8eec2009-06-08 15:35:07 +0200171xfs_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
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200202STATIC int
203xfs_sync_inode_data(
204 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200205 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200206 int flags)
207{
208 struct inode *inode = VFS_I(ip);
209 struct address_space *mapping = inode->i_mapping;
210 int error = 0;
211
Dave Chinner75f3cb12009-06-08 15:35:14 +0200212 error = xfs_sync_inode_valid(ip, pag);
213 if (error)
214 return error;
215
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200216 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:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200230 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200231 xfs_ioend_wait(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200232 IRELE(ip);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200233 return error;
234}
235
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200236STATIC int
237xfs_sync_inode_attr(
238 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200239 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200240 int flags)
241{
242 int error = 0;
243
Dave Chinner75f3cb12009-06-08 15:35:14 +0200244 error = xfs_sync_inode_valid(ip, pag);
245 if (error)
246 return error;
247
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200248 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);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200267 IRELE(ip);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200268 return error;
269}
270
Christoph Hellwig075fe102009-06-08 15:35:48 +0200271/*
272 * Write out pagecache data for the whole filesystem.
273 */
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100274int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200275xfs_sync_data(
276 struct xfs_mount *mp,
277 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100278{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200279 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100280
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200281 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100282
Christoph Hellwig075fe102009-06-08 15:35:48 +0200283 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
284 XFS_ICI_NO_TAG);
285 if (error)
286 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100287
Christoph Hellwig075fe102009-06-08 15:35:48 +0200288 xfs_log_force(mp, 0,
289 (flags & SYNC_WAIT) ?
290 XFS_LOG_FORCE | XFS_LOG_SYNC :
291 XFS_LOG_FORCE);
292 return 0;
293}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100294
Christoph Hellwig075fe102009-06-08 15:35:48 +0200295/*
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);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200304
Christoph Hellwig075fe102009-06-08 15:35:48 +0200305 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
306 XFS_ICI_NO_TAG);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100307}
308
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100309STATIC 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
David Chinnere9f1c6e2008-10-30 17:15:50 +1100343int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100344xfs_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 */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200356 if (flags & SYNC_TRYLOCK) {
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100357 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;
394}
395
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100396/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100397 * 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,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100408 * 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,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100410 * transactions can still occur here so don't bother flushing the buftarg
411 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100412 */
413int
414xfs_quiesce_data(
415 struct xfs_mount *mp)
416{
417 int error;
418
419 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200420 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200421 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100422 xfs_filestream_flush(mp);
423
424 /* push and block */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200425 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200426 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100427
David Chinnera4e4c4f2008-10-30 17:16:11 +1100428 /* write superblock and hoover up shutdown errors */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100429 error = xfs_sync_fsdata(mp, 0);
430
David Chinnera4e4c4f2008-10-30 17:16:11 +1100431 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100432 if (mp->m_rtdev_targp)
433 XFS_bflush(mp->m_rtdev_targp);
434
435 return error;
436}
437
David Chinner76bf1052008-10-30 17:16:21 +1100438STATIC void
439xfs_quiesce_fs(
440 struct xfs_mount *mp)
441{
442 int count = 0, pincount;
443
444 xfs_flush_buftarg(mp->m_ddev_targp, 0);
Dave Chinnerabc10642009-06-08 15:35:12 +0200445 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinner76bf1052008-10-30 17:16:21 +1100446
447 /*
448 * This loop must run at least twice. The first instance of the loop
449 * will flush most meta data but that will generate more meta data
450 * (typically directory updates). Which then must be flushed and
451 * logged before we can write the unmount record.
452 */
453 do {
Christoph Hellwig075fe102009-06-08 15:35:48 +0200454 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100455 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
456 if (!pincount) {
457 delay(50);
458 count++;
459 }
460 } while (count < 2);
461}
462
463/*
464 * Second stage of a quiesce. The data is already synced, now we have to take
465 * care of the metadata. New transactions are already blocked, so we need to
466 * wait for any remaining transactions to drain out before proceding.
467 */
468void
469xfs_quiesce_attr(
470 struct xfs_mount *mp)
471{
472 int error = 0;
473
474 /* wait for all modifications to complete */
475 while (atomic_read(&mp->m_active_trans) > 0)
476 delay(100);
477
478 /* flush inodes and push all remaining buffers out to disk */
479 xfs_quiesce_fs(mp);
480
Felix Blyakher5e106572009-01-22 21:34:05 -0600481 /*
482 * Just warn here till VFS can correctly support
483 * read-only remount without racing.
484 */
485 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100486
487 /* Push the superblock and write an unmount record */
488 error = xfs_log_sbcount(mp, 1);
489 if (error)
490 xfs_fs_cmn_err(CE_WARN, mp,
491 "xfs_attr_quiesce: failed to log sb changes. "
492 "Frozen image may not be consistent.");
493 xfs_log_unmount_write(mp);
494 xfs_unmountfs_writesb(mp);
495}
496
David Chinnere9f1c6e2008-10-30 17:15:50 +1100497/*
David Chinnera167b172008-10-30 17:06:18 +1100498 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
499 * Doing this has two advantages:
500 * - It saves on stack space, which is tight in certain situations
501 * - It can be used (with care) as a mechanism to avoid deadlocks.
502 * Flushing while allocating in a full filesystem requires both.
503 */
504STATIC void
505xfs_syncd_queue_work(
506 struct xfs_mount *mp,
507 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200508 void (*syncer)(struct xfs_mount *, void *),
509 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100510{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200511 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100512
Dave Chinnera8d770d2009-04-06 18:44:54 +0200513 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100514 INIT_LIST_HEAD(&work->w_list);
515 work->w_syncer = syncer;
516 work->w_data = data;
517 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200518 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100519 spin_lock(&mp->m_sync_lock);
520 list_add_tail(&work->w_list, &mp->m_sync_list);
521 spin_unlock(&mp->m_sync_lock);
522 wake_up_process(mp->m_sync_task);
523}
524
525/*
526 * Flush delayed allocate data, attempting to free up reserved space
527 * from existing allocations. At this point a new allocation attempt
528 * has failed with ENOSPC and we are in the process of scratching our
529 * heads, looking about for more room...
530 */
531STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200532xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100533 struct xfs_mount *mp,
534 void *arg)
535{
536 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200537 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200538 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100539 iput(inode);
540}
541
542void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200543xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100544 xfs_inode_t *ip)
545{
546 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200547 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100548
549 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200550 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
551 wait_for_completion(&completion);
David Chinnera167b172008-10-30 17:06:18 +1100552 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
553}
554
David Chinneraacaa882008-10-30 17:15:29 +1100555/*
556 * Every sync period we need to unpin all items, reclaim inodes, sync
557 * quota and write out the superblock. We might need to cover the log
558 * to indicate it is idle.
559 */
David Chinnera167b172008-10-30 17:06:18 +1100560STATIC void
561xfs_sync_worker(
562 struct xfs_mount *mp,
563 void *unused)
564{
565 int error;
566
David Chinneraacaa882008-10-30 17:15:29 +1100567 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
568 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
Dave Chinnerabc10642009-06-08 15:35:12 +0200569 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinneraacaa882008-10-30 17:15:29 +1100570 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200571 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
572 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
David Chinneraacaa882008-10-30 17:15:29 +1100573 if (xfs_log_need_covered(mp))
574 error = xfs_commit_dummy_trans(mp, XFS_LOG_FORCE);
575 }
David Chinnera167b172008-10-30 17:06:18 +1100576 mp->m_sync_seq++;
577 wake_up(&mp->m_wait_single_sync_task);
578}
579
580STATIC int
581xfssyncd(
582 void *arg)
583{
584 struct xfs_mount *mp = arg;
585 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200586 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100587 LIST_HEAD (tmp);
588
589 set_freezable();
590 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
591 for (;;) {
592 timeleft = schedule_timeout_interruptible(timeleft);
593 /* swsusp */
594 try_to_freeze();
595 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
596 break;
597
598 spin_lock(&mp->m_sync_lock);
599 /*
600 * We can get woken by laptop mode, to do a sync -
601 * that's the (only!) case where the list would be
602 * empty with time remaining.
603 */
604 if (!timeleft || list_empty(&mp->m_sync_list)) {
605 if (!timeleft)
606 timeleft = xfs_syncd_centisecs *
607 msecs_to_jiffies(10);
608 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
609 list_add_tail(&mp->m_sync_work.w_list,
610 &mp->m_sync_list);
611 }
612 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
613 list_move(&work->w_list, &tmp);
614 spin_unlock(&mp->m_sync_lock);
615
616 list_for_each_entry_safe(work, n, &tmp, w_list) {
617 (*work->w_syncer)(mp, work->w_data);
618 list_del(&work->w_list);
619 if (work == &mp->m_sync_work)
620 continue;
Dave Chinnere43afd72009-04-06 18:47:27 +0200621 if (work->w_completion)
622 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100623 kmem_free(work);
624 }
625 }
626
627 return 0;
628}
629
630int
631xfs_syncd_init(
632 struct xfs_mount *mp)
633{
634 mp->m_sync_work.w_syncer = xfs_sync_worker;
635 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200636 mp->m_sync_work.w_completion = NULL;
David Chinnera167b172008-10-30 17:06:18 +1100637 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
638 if (IS_ERR(mp->m_sync_task))
639 return -PTR_ERR(mp->m_sync_task);
640 return 0;
641}
642
643void
644xfs_syncd_stop(
645 struct xfs_mount *mp)
646{
647 kthread_stop(mp->m_sync_task);
648}
649
David Chinnerfce08f22008-10-30 17:37:03 +1100650int
David Chinner1dc33182008-10-30 17:37:15 +1100651xfs_reclaim_inode(
David Chinnerfce08f22008-10-30 17:37:03 +1100652 xfs_inode_t *ip,
653 int locked,
654 int sync_mode)
655{
656 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
657
658 /* The hash lock here protects a thread in xfs_iget_core from
659 * racing with us on linking the inode back with a vnode.
660 * Once we have the XFS_IRECLAIM flag set it will not touch
661 * us.
662 */
663 write_lock(&pag->pag_ici_lock);
664 spin_lock(&ip->i_flags_lock);
665 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
666 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
667 spin_unlock(&ip->i_flags_lock);
668 write_unlock(&pag->pag_ici_lock);
669 if (locked) {
670 xfs_ifunlock(ip);
671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
672 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200673 return -EAGAIN;
David Chinnerfce08f22008-10-30 17:37:03 +1100674 }
675 __xfs_iflags_set(ip, XFS_IRECLAIM);
676 spin_unlock(&ip->i_flags_lock);
677 write_unlock(&pag->pag_ici_lock);
678 xfs_put_perag(ip->i_mount, pag);
679
680 /*
681 * If the inode is still dirty, then flush it out. If the inode
682 * is not in the AIL, then it will be OK to flush it delwri as
683 * long as xfs_iflush() does not keep any references to the inode.
684 * We leave that decision up to xfs_iflush() since it has the
685 * knowledge of whether it's OK to simply do a delwri flush of
686 * the inode or whether we need to wait until the inode is
687 * pulled from the AIL.
688 * We get the flush lock regardless, though, just to make sure
689 * we don't free it while it is being flushed.
690 */
691 if (!locked) {
692 xfs_ilock(ip, XFS_ILOCK_EXCL);
693 xfs_iflock(ip);
694 }
695
696 /*
697 * In the case of a forced shutdown we rely on xfs_iflush() to
698 * wait for the inode to be unpinned before returning an error.
699 */
700 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
701 /* synchronize with xfs_iflush_done */
702 xfs_iflock(ip);
703 xfs_ifunlock(ip);
704 }
705
706 xfs_iunlock(ip, XFS_ILOCK_EXCL);
707 xfs_ireclaim(ip);
708 return 0;
709}
710
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400711void
712__xfs_inode_set_reclaim_tag(
713 struct xfs_perag *pag,
714 struct xfs_inode *ip)
715{
716 radix_tree_tag_set(&pag->pag_ici_root,
717 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
718 XFS_ICI_RECLAIM_TAG);
719}
720
David Chinner11654512008-10-30 17:37:49 +1100721/*
722 * We set the inode flag atomically with the radix tree tag.
723 * Once we get tag lookups on the radix tree, this inode flag
724 * can go away.
725 */
David Chinner396beb82008-10-30 17:37:26 +1100726void
727xfs_inode_set_reclaim_tag(
728 xfs_inode_t *ip)
729{
730 xfs_mount_t *mp = ip->i_mount;
731 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
732
733 read_lock(&pag->pag_ici_lock);
734 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400735 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100736 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100737 spin_unlock(&ip->i_flags_lock);
738 read_unlock(&pag->pag_ici_lock);
739 xfs_put_perag(mp, pag);
740}
741
742void
743__xfs_inode_clear_reclaim_tag(
744 xfs_mount_t *mp,
745 xfs_perag_t *pag,
746 xfs_inode_t *ip)
747{
748 radix_tree_tag_clear(&pag->pag_ici_root,
749 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
750}
751
Dave Chinner75f3cb12009-06-08 15:35:14 +0200752STATIC int
753xfs_reclaim_inode_now(
754 struct xfs_inode *ip,
755 struct xfs_perag *pag,
756 int flags)
David Chinner7a3be022008-10-30 17:37:37 +1100757{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200758 /* ignore if already under reclaim */
759 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
David Chinner7a3be022008-10-30 17:37:37 +1100760 read_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200761 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100762 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200763 read_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100764
Dave Chinner75f3cb12009-06-08 15:35:14 +0200765 return xfs_reclaim_inode(ip, 0, flags);
David Chinner7a3be022008-10-30 17:37:37 +1100766}
767
David Chinnerfce08f22008-10-30 17:37:03 +1100768int
David Chinner1dc33182008-10-30 17:37:15 +1100769xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100770 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100771 int mode)
772{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200773 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode_now, mode,
774 XFS_ICI_RECLAIM_TAG);
David Chinnerfce08f22008-10-30 17:37:03 +1100775}