blob: 6fed97a8cd3e29ef46791c24ebecf16b6ef5ba11 [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"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000047#include "xfs_trace.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110048
David Chinnera167b172008-10-30 17:06:18 +110049#include <linux/kthread.h>
50#include <linux/freezer.h>
51
Dave Chinner5a34d5c2009-06-08 15:35:03 +020052
Dave Chinner75f3cb12009-06-08 15:35:14 +020053STATIC xfs_inode_t *
54xfs_inode_ag_lookup(
55 struct xfs_mount *mp,
56 struct xfs_perag *pag,
57 uint32_t *first_index,
58 int tag)
59{
60 int nr_found;
61 struct xfs_inode *ip;
62
63 /*
64 * use a gang lookup to find the next inode in the tree
65 * as the tree is sparse and a gang lookup walks to find
66 * the number of objects requested.
67 */
68 read_lock(&pag->pag_ici_lock);
69 if (tag == XFS_ICI_NO_TAG) {
70 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
71 (void **)&ip, *first_index, 1);
72 } else {
73 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
74 (void **)&ip, *first_index, 1, tag);
75 }
76 if (!nr_found)
77 goto unlock;
78
79 /*
80 * Update the index for the next lookup. Catch overflows
81 * into the next AG range which can occur if we have inodes
82 * in the last block of the AG and we are currently
83 * pointing to the last inode.
84 */
85 *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
86 if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
87 goto unlock;
88
89 return ip;
90
91unlock:
92 read_unlock(&pag->pag_ici_lock);
93 return NULL;
94}
95
96STATIC int
97xfs_inode_ag_walk(
98 struct xfs_mount *mp,
99 xfs_agnumber_t ag,
100 int (*execute)(struct xfs_inode *ip,
101 struct xfs_perag *pag, int flags),
102 int flags,
103 int tag)
104{
105 struct xfs_perag *pag = &mp->m_perag[ag];
106 uint32_t first_index;
107 int last_error = 0;
108 int skipped;
109
110restart:
111 skipped = 0;
112 first_index = 0;
113 do {
114 int error = 0;
115 xfs_inode_t *ip;
116
117 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
118 if (!ip)
119 break;
120
121 error = execute(ip, pag, flags);
122 if (error == EAGAIN) {
123 skipped++;
124 continue;
125 }
126 if (error)
127 last_error = error;
128 /*
129 * bail out if the filesystem is corrupted.
130 */
131 if (error == EFSCORRUPTED)
132 break;
133
134 } while (1);
135
136 if (skipped) {
137 delay(1);
138 goto restart;
139 }
140
141 xfs_put_perag(mp, pag);
142 return last_error;
143}
144
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200145int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200146xfs_inode_ag_iterator(
147 struct xfs_mount *mp,
148 int (*execute)(struct xfs_inode *ip,
149 struct xfs_perag *pag, int flags),
150 int flags,
151 int tag)
152{
153 int error = 0;
154 int last_error = 0;
155 xfs_agnumber_t ag;
156
157 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
158 if (!mp->m_perag[ag].pag_ici_init)
159 continue;
160 error = xfs_inode_ag_walk(mp, ag, execute, flags, tag);
161 if (error) {
162 last_error = error;
163 if (error == EFSCORRUPTED)
164 break;
165 }
166 }
167 return XFS_ERROR(last_error);
168}
169
Dave Chinner1da8eec2009-06-08 15:35:07 +0200170/* must be called with pag_ici_lock held and releases it */
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200171int
Dave Chinner1da8eec2009-06-08 15:35:07 +0200172xfs_sync_inode_valid(
173 struct xfs_inode *ip,
174 struct xfs_perag *pag)
175{
176 struct inode *inode = VFS_I(ip);
177
178 /* nothing to sync during shutdown */
179 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
180 read_unlock(&pag->pag_ici_lock);
181 return EFSCORRUPTED;
182 }
183
184 /*
185 * If we can't get a reference on the inode, it must be in reclaim.
186 * Leave it for the reclaim code to flush. Also avoid inodes that
187 * haven't been fully initialised.
188 */
189 if (!igrab(inode)) {
190 read_unlock(&pag->pag_ici_lock);
191 return ENOENT;
192 }
193 read_unlock(&pag->pag_ici_lock);
194
195 if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) {
196 IRELE(ip);
197 return ENOENT;
198 }
199
200 return 0;
201}
202
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200203STATIC int
204xfs_sync_inode_data(
205 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200206 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200207 int flags)
208{
209 struct inode *inode = VFS_I(ip);
210 struct address_space *mapping = inode->i_mapping;
211 int error = 0;
212
Dave Chinner75f3cb12009-06-08 15:35:14 +0200213 error = xfs_sync_inode_valid(ip, pag);
214 if (error)
215 return error;
216
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200217 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
218 goto out_wait;
219
220 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
221 if (flags & SYNC_TRYLOCK)
222 goto out_wait;
223 xfs_ilock(ip, XFS_IOLOCK_SHARED);
224 }
225
226 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
227 0 : XFS_B_ASYNC, FI_NONE);
228 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
229
230 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200231 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200232 xfs_ioend_wait(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200233 IRELE(ip);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200234 return error;
235}
236
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200237STATIC int
238xfs_sync_inode_attr(
239 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200240 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200241 int flags)
242{
243 int error = 0;
244
Dave Chinner75f3cb12009-06-08 15:35:14 +0200245 error = xfs_sync_inode_valid(ip, pag);
246 if (error)
247 return error;
248
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200249 xfs_ilock(ip, XFS_ILOCK_SHARED);
250 if (xfs_inode_clean(ip))
251 goto out_unlock;
252 if (!xfs_iflock_nowait(ip)) {
253 if (!(flags & SYNC_WAIT))
254 goto out_unlock;
255 xfs_iflock(ip);
256 }
257
258 if (xfs_inode_clean(ip)) {
259 xfs_ifunlock(ip);
260 goto out_unlock;
261 }
262
263 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
264 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
265
266 out_unlock:
267 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200268 IRELE(ip);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200269 return error;
270}
271
Christoph Hellwig075fe102009-06-08 15:35:48 +0200272/*
273 * Write out pagecache data for the whole filesystem.
274 */
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100275int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200276xfs_sync_data(
277 struct xfs_mount *mp,
278 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100279{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200280 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100281
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200282 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100283
Christoph Hellwig075fe102009-06-08 15:35:48 +0200284 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
285 XFS_ICI_NO_TAG);
286 if (error)
287 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100288
Christoph Hellwig075fe102009-06-08 15:35:48 +0200289 xfs_log_force(mp, 0,
290 (flags & SYNC_WAIT) ?
291 XFS_LOG_FORCE | XFS_LOG_SYNC :
292 XFS_LOG_FORCE);
293 return 0;
294}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100295
Christoph Hellwig075fe102009-06-08 15:35:48 +0200296/*
297 * Write out inode metadata (attributes) for the whole filesystem.
298 */
299int
300xfs_sync_attr(
301 struct xfs_mount *mp,
302 int flags)
303{
304 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200305
Christoph Hellwig075fe102009-06-08 15:35:48 +0200306 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
307 XFS_ICI_NO_TAG);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100308}
309
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100310STATIC int
311xfs_commit_dummy_trans(
312 struct xfs_mount *mp,
Dave Chinnerdce50652009-10-06 20:29:30 +0000313 uint flags)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100314{
315 struct xfs_inode *ip = mp->m_rootip;
316 struct xfs_trans *tp;
317 int error;
Dave Chinnerdce50652009-10-06 20:29:30 +0000318 int log_flags = XFS_LOG_FORCE;
319
320 if (flags & SYNC_WAIT)
321 log_flags |= XFS_LOG_SYNC;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100322
323 /*
324 * Put a dummy transaction in the log to tell recovery
325 * that all others are OK.
326 */
327 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
328 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
329 if (error) {
330 xfs_trans_cancel(tp, 0);
331 return error;
332 }
333
334 xfs_ilock(ip, XFS_ILOCK_EXCL);
335
336 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
337 xfs_trans_ihold(tp, ip);
338 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100339 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100340 xfs_iunlock(ip, XFS_ILOCK_EXCL);
341
Dave Chinnerdce50652009-10-06 20:29:30 +0000342 /* the log force ensures this transaction is pushed to disk */
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100343 xfs_log_force(mp, 0, log_flags);
Dave Chinnerdce50652009-10-06 20:29:30 +0000344 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100345}
346
David Chinnere9f1c6e2008-10-30 17:15:50 +1100347int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100348xfs_sync_fsdata(
349 struct xfs_mount *mp,
350 int flags)
351{
352 struct xfs_buf *bp;
353 struct xfs_buf_log_item *bip;
354 int error = 0;
355
356 /*
357 * If this is xfssyncd() then only sync the superblock if we can
358 * lock it without sleeping and it is not pinned.
359 */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200360 if (flags & SYNC_TRYLOCK) {
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100361 ASSERT(!(flags & SYNC_WAIT));
362
363 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
364 if (!bp)
365 goto out;
366
367 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
368 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
369 goto out_brelse;
370 } else {
371 bp = xfs_getsb(mp, 0);
372
373 /*
374 * If the buffer is pinned then push on the log so we won't
375 * get stuck waiting in the write for someone, maybe
376 * ourselves, to flush the log.
377 *
378 * Even though we just pushed the log above, we did not have
379 * the superblock buffer locked at that point so it can
380 * become pinned in between there and here.
381 */
382 if (XFS_BUF_ISPINNED(bp))
383 xfs_log_force(mp, 0, XFS_LOG_FORCE);
384 }
385
386
387 if (flags & SYNC_WAIT)
388 XFS_BUF_UNASYNC(bp);
389 else
390 XFS_BUF_ASYNC(bp);
391
Dave Chinnerdce50652009-10-06 20:29:30 +0000392 error = xfs_bwrite(mp, bp);
393 if (error)
394 return error;
395
396 /*
397 * If this is a data integrity sync make sure all pending buffers
398 * are flushed out for the log coverage check below.
399 */
400 if (flags & SYNC_WAIT)
401 xfs_flush_buftarg(mp->m_ddev_targp, 1);
402
403 if (xfs_log_need_covered(mp))
404 error = xfs_commit_dummy_trans(mp, flags);
405 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100406
407 out_brelse:
408 xfs_buf_relse(bp);
409 out:
410 return error;
411}
412
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100413/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100414 * When remounting a filesystem read-only or freezing the filesystem, we have
415 * two phases to execute. This first phase is syncing the data before we
416 * quiesce the filesystem, and the second is flushing all the inodes out after
417 * we've waited for all the transactions created by the first phase to
418 * complete. The second phase ensures that the inodes are written to their
419 * location on disk rather than just existing in transactions in the log. This
420 * means after a quiesce there is no log replay required to write the inodes to
421 * disk (this is the main difference between a sync and a quiesce).
422 */
423/*
424 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100425 * so we flush delwri and delalloc buffers here, then wait for all I/O to
426 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100427 * transactions can still occur here so don't bother flushing the buftarg
428 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100429 */
430int
431xfs_quiesce_data(
432 struct xfs_mount *mp)
433{
434 int error;
435
436 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200437 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200438 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100439
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000440 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200441 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200442 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100443
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000444 /* drop inode references pinned by filestreams */
445 xfs_filestream_flush(mp);
446
David Chinnera4e4c4f2008-10-30 17:16:11 +1100447 /* write superblock and hoover up shutdown errors */
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000448 error = xfs_sync_fsdata(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100449
David Chinnera4e4c4f2008-10-30 17:16:11 +1100450 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100451 if (mp->m_rtdev_targp)
452 XFS_bflush(mp->m_rtdev_targp);
453
454 return error;
455}
456
David Chinner76bf1052008-10-30 17:16:21 +1100457STATIC void
458xfs_quiesce_fs(
459 struct xfs_mount *mp)
460{
461 int count = 0, pincount;
462
463 xfs_flush_buftarg(mp->m_ddev_targp, 0);
Dave Chinnerabc10642009-06-08 15:35:12 +0200464 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinner76bf1052008-10-30 17:16:21 +1100465
466 /*
467 * This loop must run at least twice. The first instance of the loop
468 * will flush most meta data but that will generate more meta data
469 * (typically directory updates). Which then must be flushed and
470 * logged before we can write the unmount record.
471 */
472 do {
Christoph Hellwig075fe102009-06-08 15:35:48 +0200473 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100474 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
475 if (!pincount) {
476 delay(50);
477 count++;
478 }
479 } while (count < 2);
480}
481
482/*
483 * Second stage of a quiesce. The data is already synced, now we have to take
484 * care of the metadata. New transactions are already blocked, so we need to
485 * wait for any remaining transactions to drain out before proceding.
486 */
487void
488xfs_quiesce_attr(
489 struct xfs_mount *mp)
490{
491 int error = 0;
492
493 /* wait for all modifications to complete */
494 while (atomic_read(&mp->m_active_trans) > 0)
495 delay(100);
496
497 /* flush inodes and push all remaining buffers out to disk */
498 xfs_quiesce_fs(mp);
499
Felix Blyakher5e106572009-01-22 21:34:05 -0600500 /*
501 * Just warn here till VFS can correctly support
502 * read-only remount without racing.
503 */
504 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100505
506 /* Push the superblock and write an unmount record */
507 error = xfs_log_sbcount(mp, 1);
508 if (error)
509 xfs_fs_cmn_err(CE_WARN, mp,
510 "xfs_attr_quiesce: failed to log sb changes. "
511 "Frozen image may not be consistent.");
512 xfs_log_unmount_write(mp);
513 xfs_unmountfs_writesb(mp);
514}
515
David Chinnere9f1c6e2008-10-30 17:15:50 +1100516/*
David Chinnera167b172008-10-30 17:06:18 +1100517 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
518 * Doing this has two advantages:
519 * - It saves on stack space, which is tight in certain situations
520 * - It can be used (with care) as a mechanism to avoid deadlocks.
521 * Flushing while allocating in a full filesystem requires both.
522 */
523STATIC void
524xfs_syncd_queue_work(
525 struct xfs_mount *mp,
526 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200527 void (*syncer)(struct xfs_mount *, void *),
528 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100529{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200530 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100531
Dave Chinnera8d770d2009-04-06 18:44:54 +0200532 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100533 INIT_LIST_HEAD(&work->w_list);
534 work->w_syncer = syncer;
535 work->w_data = data;
536 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200537 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100538 spin_lock(&mp->m_sync_lock);
539 list_add_tail(&work->w_list, &mp->m_sync_list);
540 spin_unlock(&mp->m_sync_lock);
541 wake_up_process(mp->m_sync_task);
542}
543
544/*
545 * Flush delayed allocate data, attempting to free up reserved space
546 * from existing allocations. At this point a new allocation attempt
547 * has failed with ENOSPC and we are in the process of scratching our
548 * heads, looking about for more room...
549 */
550STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200551xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100552 struct xfs_mount *mp,
553 void *arg)
554{
555 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200556 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200557 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100558 iput(inode);
559}
560
561void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200562xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100563 xfs_inode_t *ip)
564{
565 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200566 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100567
568 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200569 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
570 wait_for_completion(&completion);
David Chinnera167b172008-10-30 17:06:18 +1100571 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
572}
573
David Chinneraacaa882008-10-30 17:15:29 +1100574/*
575 * Every sync period we need to unpin all items, reclaim inodes, sync
576 * quota and write out the superblock. We might need to cover the log
577 * to indicate it is idle.
578 */
David Chinnera167b172008-10-30 17:06:18 +1100579STATIC void
580xfs_sync_worker(
581 struct xfs_mount *mp,
582 void *unused)
583{
584 int error;
585
David Chinneraacaa882008-10-30 17:15:29 +1100586 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
587 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
Dave Chinnerabc10642009-06-08 15:35:12 +0200588 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinneraacaa882008-10-30 17:15:29 +1100589 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200590 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
591 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
David Chinneraacaa882008-10-30 17:15:29 +1100592 }
David Chinnera167b172008-10-30 17:06:18 +1100593 mp->m_sync_seq++;
594 wake_up(&mp->m_wait_single_sync_task);
595}
596
597STATIC int
598xfssyncd(
599 void *arg)
600{
601 struct xfs_mount *mp = arg;
602 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200603 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100604 LIST_HEAD (tmp);
605
606 set_freezable();
607 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
608 for (;;) {
609 timeleft = schedule_timeout_interruptible(timeleft);
610 /* swsusp */
611 try_to_freeze();
612 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
613 break;
614
615 spin_lock(&mp->m_sync_lock);
616 /*
617 * We can get woken by laptop mode, to do a sync -
618 * that's the (only!) case where the list would be
619 * empty with time remaining.
620 */
621 if (!timeleft || list_empty(&mp->m_sync_list)) {
622 if (!timeleft)
623 timeleft = xfs_syncd_centisecs *
624 msecs_to_jiffies(10);
625 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
626 list_add_tail(&mp->m_sync_work.w_list,
627 &mp->m_sync_list);
628 }
629 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
630 list_move(&work->w_list, &tmp);
631 spin_unlock(&mp->m_sync_lock);
632
633 list_for_each_entry_safe(work, n, &tmp, w_list) {
634 (*work->w_syncer)(mp, work->w_data);
635 list_del(&work->w_list);
636 if (work == &mp->m_sync_work)
637 continue;
Dave Chinnere43afd72009-04-06 18:47:27 +0200638 if (work->w_completion)
639 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100640 kmem_free(work);
641 }
642 }
643
644 return 0;
645}
646
647int
648xfs_syncd_init(
649 struct xfs_mount *mp)
650{
651 mp->m_sync_work.w_syncer = xfs_sync_worker;
652 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200653 mp->m_sync_work.w_completion = NULL;
David Chinnera167b172008-10-30 17:06:18 +1100654 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
655 if (IS_ERR(mp->m_sync_task))
656 return -PTR_ERR(mp->m_sync_task);
657 return 0;
658}
659
660void
661xfs_syncd_stop(
662 struct xfs_mount *mp)
663{
664 kthread_stop(mp->m_sync_task);
665}
666
Christoph Hellwig848ce8f2009-09-29 13:48:56 +0000667STATIC int
David Chinner1dc33182008-10-30 17:37:15 +1100668xfs_reclaim_inode(
David Chinnerfce08f22008-10-30 17:37:03 +1100669 xfs_inode_t *ip,
David Chinnerfce08f22008-10-30 17:37:03 +1100670 int sync_mode)
671{
672 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
673
674 /* The hash lock here protects a thread in xfs_iget_core from
675 * racing with us on linking the inode back with a vnode.
676 * Once we have the XFS_IRECLAIM flag set it will not touch
677 * us.
678 */
679 write_lock(&pag->pag_ici_lock);
680 spin_lock(&ip->i_flags_lock);
681 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
682 !__xfs_iflags_test(ip, XFS_IRECLAIMABLE)) {
683 spin_unlock(&ip->i_flags_lock);
684 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200685 return -EAGAIN;
David Chinnerfce08f22008-10-30 17:37:03 +1100686 }
687 __xfs_iflags_set(ip, XFS_IRECLAIM);
688 spin_unlock(&ip->i_flags_lock);
689 write_unlock(&pag->pag_ici_lock);
690 xfs_put_perag(ip->i_mount, pag);
691
692 /*
693 * If the inode is still dirty, then flush it out. If the inode
694 * is not in the AIL, then it will be OK to flush it delwri as
695 * long as xfs_iflush() does not keep any references to the inode.
696 * We leave that decision up to xfs_iflush() since it has the
697 * knowledge of whether it's OK to simply do a delwri flush of
698 * the inode or whether we need to wait until the inode is
699 * pulled from the AIL.
700 * We get the flush lock regardless, though, just to make sure
701 * we don't free it while it is being flushed.
702 */
Christoph Hellwig848ce8f2009-09-29 13:48:56 +0000703 xfs_ilock(ip, XFS_ILOCK_EXCL);
704 xfs_iflock(ip);
David Chinnerfce08f22008-10-30 17:37:03 +1100705
706 /*
707 * In the case of a forced shutdown we rely on xfs_iflush() to
708 * wait for the inode to be unpinned before returning an error.
709 */
710 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
711 /* synchronize with xfs_iflush_done */
712 xfs_iflock(ip);
713 xfs_ifunlock(ip);
714 }
715
716 xfs_iunlock(ip, XFS_ILOCK_EXCL);
717 xfs_ireclaim(ip);
718 return 0;
719}
720
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400721void
722__xfs_inode_set_reclaim_tag(
723 struct xfs_perag *pag,
724 struct xfs_inode *ip)
725{
726 radix_tree_tag_set(&pag->pag_ici_root,
727 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
728 XFS_ICI_RECLAIM_TAG);
729}
730
David Chinner11654512008-10-30 17:37:49 +1100731/*
732 * We set the inode flag atomically with the radix tree tag.
733 * Once we get tag lookups on the radix tree, this inode flag
734 * can go away.
735 */
David Chinner396beb82008-10-30 17:37:26 +1100736void
737xfs_inode_set_reclaim_tag(
738 xfs_inode_t *ip)
739{
740 xfs_mount_t *mp = ip->i_mount;
741 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
742
743 read_lock(&pag->pag_ici_lock);
744 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400745 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100746 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100747 spin_unlock(&ip->i_flags_lock);
748 read_unlock(&pag->pag_ici_lock);
749 xfs_put_perag(mp, pag);
750}
751
752void
753__xfs_inode_clear_reclaim_tag(
754 xfs_mount_t *mp,
755 xfs_perag_t *pag,
756 xfs_inode_t *ip)
757{
758 radix_tree_tag_clear(&pag->pag_ici_root,
759 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
760}
761
Dave Chinner75f3cb12009-06-08 15:35:14 +0200762STATIC int
763xfs_reclaim_inode_now(
764 struct xfs_inode *ip,
765 struct xfs_perag *pag,
766 int flags)
David Chinner7a3be022008-10-30 17:37:37 +1100767{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200768 /* ignore if already under reclaim */
769 if (xfs_iflags_test(ip, XFS_IRECLAIM)) {
David Chinner7a3be022008-10-30 17:37:37 +1100770 read_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200771 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100772 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200773 read_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100774
Christoph Hellwig848ce8f2009-09-29 13:48:56 +0000775 return xfs_reclaim_inode(ip, flags);
David Chinner7a3be022008-10-30 17:37:37 +1100776}
777
David Chinnerfce08f22008-10-30 17:37:03 +1100778int
David Chinner1dc33182008-10-30 17:37:15 +1100779xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100780 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100781 int mode)
782{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200783 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode_now, mode,
784 XFS_ICI_RECLAIM_TAG);
David Chinnerfce08f22008-10-30 17:37:03 +1100785}