blob: e19d25555c3fe32e414d91293b5562bfe877947d [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 */
Dave Chinner75f3cb12009-06-08 15:35:14 +020068 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)
Dave Chinnerc8e20be2010-01-10 23:51:45 +000076 return NULL;
Dave Chinner75f3cb12009-06-08 15:35:14 +020077
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))
Dave Chinnerc8e20be2010-01-10 23:51:45 +000086 return NULL;
Dave Chinner75f3cb12009-06-08 15:35:14 +020087 return ip;
Dave Chinner75f3cb12009-06-08 15:35:14 +020088}
89
90STATIC int
91xfs_inode_ag_walk(
92 struct xfs_mount *mp,
93 xfs_agnumber_t ag,
94 int (*execute)(struct xfs_inode *ip,
95 struct xfs_perag *pag, int flags),
96 int flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +000097 int tag,
98 int exclusive)
Dave Chinner75f3cb12009-06-08 15:35:14 +020099{
100 struct xfs_perag *pag = &mp->m_perag[ag];
101 uint32_t first_index;
102 int last_error = 0;
103 int skipped;
104
105restart:
106 skipped = 0;
107 first_index = 0;
108 do {
109 int error = 0;
110 xfs_inode_t *ip;
111
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000112 if (exclusive)
113 write_lock(&pag->pag_ici_lock);
114 else
115 read_lock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200116 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000117 if (!ip) {
118 if (exclusive)
119 write_unlock(&pag->pag_ici_lock);
120 else
121 read_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200122 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000123 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200124
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000125 /* execute releases pag->pag_ici_lock */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200126 error = execute(ip, pag, flags);
127 if (error == EAGAIN) {
128 skipped++;
129 continue;
130 }
131 if (error)
132 last_error = error;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000133
134 /* bail out if the filesystem is corrupted. */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200135 if (error == EFSCORRUPTED)
136 break;
137
138 } while (1);
139
140 if (skipped) {
141 delay(1);
142 goto restart;
143 }
144
145 xfs_put_perag(mp, pag);
146 return last_error;
147}
148
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200149int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200150xfs_inode_ag_iterator(
151 struct xfs_mount *mp,
152 int (*execute)(struct xfs_inode *ip,
153 struct xfs_perag *pag, int flags),
154 int flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000155 int tag,
156 int exclusive)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200157{
158 int error = 0;
159 int last_error = 0;
160 xfs_agnumber_t ag;
161
162 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
163 if (!mp->m_perag[ag].pag_ici_init)
164 continue;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000165 error = xfs_inode_ag_walk(mp, ag, execute, flags, tag,
166 exclusive);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200167 if (error) {
168 last_error = error;
169 if (error == EFSCORRUPTED)
170 break;
171 }
172 }
173 return XFS_ERROR(last_error);
174}
175
Dave Chinner1da8eec2009-06-08 15:35:07 +0200176/* must be called with pag_ici_lock held and releases it */
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200177int
Dave Chinner1da8eec2009-06-08 15:35:07 +0200178xfs_sync_inode_valid(
179 struct xfs_inode *ip,
180 struct xfs_perag *pag)
181{
182 struct inode *inode = VFS_I(ip);
183
184 /* nothing to sync during shutdown */
185 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
186 read_unlock(&pag->pag_ici_lock);
187 return EFSCORRUPTED;
188 }
189
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000190 /* If we can't get a reference on the inode, it must be in reclaim. */
Dave Chinner1da8eec2009-06-08 15:35:07 +0200191 if (!igrab(inode)) {
192 read_unlock(&pag->pag_ici_lock);
193 return ENOENT;
194 }
195 read_unlock(&pag->pag_ici_lock);
196
197 if (is_bad_inode(inode) || xfs_iflags_test(ip, XFS_INEW)) {
198 IRELE(ip);
199 return ENOENT;
200 }
201
202 return 0;
203}
204
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200205STATIC int
206xfs_sync_inode_data(
207 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200208 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200209 int flags)
210{
211 struct inode *inode = VFS_I(ip);
212 struct address_space *mapping = inode->i_mapping;
213 int error = 0;
214
Dave Chinner75f3cb12009-06-08 15:35:14 +0200215 error = xfs_sync_inode_valid(ip, pag);
216 if (error)
217 return error;
218
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200219 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
220 goto out_wait;
221
222 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
223 if (flags & SYNC_TRYLOCK)
224 goto out_wait;
225 xfs_ilock(ip, XFS_IOLOCK_SHARED);
226 }
227
228 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
229 0 : XFS_B_ASYNC, FI_NONE);
230 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
231
232 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200233 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200234 xfs_ioend_wait(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200235 IRELE(ip);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200236 return error;
237}
238
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200239STATIC int
240xfs_sync_inode_attr(
241 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200242 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200243 int flags)
244{
245 int error = 0;
246
Dave Chinner75f3cb12009-06-08 15:35:14 +0200247 error = xfs_sync_inode_valid(ip, pag);
248 if (error)
249 return error;
250
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200251 xfs_ilock(ip, XFS_ILOCK_SHARED);
252 if (xfs_inode_clean(ip))
253 goto out_unlock;
254 if (!xfs_iflock_nowait(ip)) {
255 if (!(flags & SYNC_WAIT))
256 goto out_unlock;
257 xfs_iflock(ip);
258 }
259
260 if (xfs_inode_clean(ip)) {
261 xfs_ifunlock(ip);
262 goto out_unlock;
263 }
264
265 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
266 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
267
268 out_unlock:
269 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200270 IRELE(ip);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200271 return error;
272}
273
Christoph Hellwig075fe102009-06-08 15:35:48 +0200274/*
275 * Write out pagecache data for the whole filesystem.
276 */
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100277int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200278xfs_sync_data(
279 struct xfs_mount *mp,
280 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100281{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200282 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100283
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200284 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100285
Christoph Hellwig075fe102009-06-08 15:35:48 +0200286 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000287 XFS_ICI_NO_TAG, 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200288 if (error)
289 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100290
Christoph Hellwig075fe102009-06-08 15:35:48 +0200291 xfs_log_force(mp, 0,
292 (flags & SYNC_WAIT) ?
293 XFS_LOG_FORCE | XFS_LOG_SYNC :
294 XFS_LOG_FORCE);
295 return 0;
296}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100297
Christoph Hellwig075fe102009-06-08 15:35:48 +0200298/*
299 * Write out inode metadata (attributes) for the whole filesystem.
300 */
301int
302xfs_sync_attr(
303 struct xfs_mount *mp,
304 int flags)
305{
306 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200307
Christoph Hellwig075fe102009-06-08 15:35:48 +0200308 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000309 XFS_ICI_NO_TAG, 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100310}
311
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100312STATIC int
313xfs_commit_dummy_trans(
314 struct xfs_mount *mp,
Dave Chinnerdce50652009-10-06 20:29:30 +0000315 uint flags)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100316{
317 struct xfs_inode *ip = mp->m_rootip;
318 struct xfs_trans *tp;
319 int error;
Dave Chinnerdce50652009-10-06 20:29:30 +0000320 int log_flags = XFS_LOG_FORCE;
321
322 if (flags & SYNC_WAIT)
323 log_flags |= XFS_LOG_SYNC;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100324
325 /*
326 * Put a dummy transaction in the log to tell recovery
327 * that all others are OK.
328 */
329 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
330 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
331 if (error) {
332 xfs_trans_cancel(tp, 0);
333 return error;
334 }
335
336 xfs_ilock(ip, XFS_ILOCK_EXCL);
337
338 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
339 xfs_trans_ihold(tp, ip);
340 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100341 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100342 xfs_iunlock(ip, XFS_ILOCK_EXCL);
343
Dave Chinnerdce50652009-10-06 20:29:30 +0000344 /* the log force ensures this transaction is pushed to disk */
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100345 xfs_log_force(mp, 0, log_flags);
Dave Chinnerdce50652009-10-06 20:29:30 +0000346 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100347}
348
David Chinnere9f1c6e2008-10-30 17:15:50 +1100349int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100350xfs_sync_fsdata(
351 struct xfs_mount *mp,
352 int flags)
353{
354 struct xfs_buf *bp;
355 struct xfs_buf_log_item *bip;
356 int error = 0;
357
358 /*
359 * If this is xfssyncd() then only sync the superblock if we can
360 * lock it without sleeping and it is not pinned.
361 */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200362 if (flags & SYNC_TRYLOCK) {
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100363 ASSERT(!(flags & SYNC_WAIT));
364
365 bp = xfs_getsb(mp, XFS_BUF_TRYLOCK);
366 if (!bp)
367 goto out;
368
369 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
370 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
371 goto out_brelse;
372 } else {
373 bp = xfs_getsb(mp, 0);
374
375 /*
376 * If the buffer is pinned then push on the log so we won't
377 * get stuck waiting in the write for someone, maybe
378 * ourselves, to flush the log.
379 *
380 * Even though we just pushed the log above, we did not have
381 * the superblock buffer locked at that point so it can
382 * become pinned in between there and here.
383 */
384 if (XFS_BUF_ISPINNED(bp))
385 xfs_log_force(mp, 0, XFS_LOG_FORCE);
386 }
387
388
389 if (flags & SYNC_WAIT)
390 XFS_BUF_UNASYNC(bp);
391 else
392 XFS_BUF_ASYNC(bp);
393
Dave Chinnerdce50652009-10-06 20:29:30 +0000394 error = xfs_bwrite(mp, bp);
395 if (error)
396 return error;
397
398 /*
399 * If this is a data integrity sync make sure all pending buffers
400 * are flushed out for the log coverage check below.
401 */
402 if (flags & SYNC_WAIT)
403 xfs_flush_buftarg(mp->m_ddev_targp, 1);
404
405 if (xfs_log_need_covered(mp))
406 error = xfs_commit_dummy_trans(mp, flags);
407 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100408
409 out_brelse:
410 xfs_buf_relse(bp);
411 out:
412 return error;
413}
414
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100415/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100416 * When remounting a filesystem read-only or freezing the filesystem, we have
417 * two phases to execute. This first phase is syncing the data before we
418 * quiesce the filesystem, and the second is flushing all the inodes out after
419 * we've waited for all the transactions created by the first phase to
420 * complete. The second phase ensures that the inodes are written to their
421 * location on disk rather than just existing in transactions in the log. This
422 * means after a quiesce there is no log replay required to write the inodes to
423 * disk (this is the main difference between a sync and a quiesce).
424 */
425/*
426 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100427 * so we flush delwri and delalloc buffers here, then wait for all I/O to
428 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100429 * transactions can still occur here so don't bother flushing the buftarg
430 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100431 */
432int
433xfs_quiesce_data(
434 struct xfs_mount *mp)
435{
436 int error;
437
438 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200439 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200440 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100441
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000442 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200443 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200444 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100445
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000446 /* drop inode references pinned by filestreams */
447 xfs_filestream_flush(mp);
448
David Chinnera4e4c4f2008-10-30 17:16:11 +1100449 /* write superblock and hoover up shutdown errors */
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000450 error = xfs_sync_fsdata(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100451
David Chinnera4e4c4f2008-10-30 17:16:11 +1100452 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100453 if (mp->m_rtdev_targp)
454 XFS_bflush(mp->m_rtdev_targp);
455
456 return error;
457}
458
David Chinner76bf1052008-10-30 17:16:21 +1100459STATIC void
460xfs_quiesce_fs(
461 struct xfs_mount *mp)
462{
463 int count = 0, pincount;
464
465 xfs_flush_buftarg(mp->m_ddev_targp, 0);
Dave Chinnerabc10642009-06-08 15:35:12 +0200466 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinner76bf1052008-10-30 17:16:21 +1100467
468 /*
469 * This loop must run at least twice. The first instance of the loop
470 * will flush most meta data but that will generate more meta data
471 * (typically directory updates). Which then must be flushed and
472 * logged before we can write the unmount record.
473 */
474 do {
Christoph Hellwig075fe102009-06-08 15:35:48 +0200475 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100476 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
477 if (!pincount) {
478 delay(50);
479 count++;
480 }
481 } while (count < 2);
482}
483
484/*
485 * Second stage of a quiesce. The data is already synced, now we have to take
486 * care of the metadata. New transactions are already blocked, so we need to
487 * wait for any remaining transactions to drain out before proceding.
488 */
489void
490xfs_quiesce_attr(
491 struct xfs_mount *mp)
492{
493 int error = 0;
494
495 /* wait for all modifications to complete */
496 while (atomic_read(&mp->m_active_trans) > 0)
497 delay(100);
498
499 /* flush inodes and push all remaining buffers out to disk */
500 xfs_quiesce_fs(mp);
501
Felix Blyakher5e106572009-01-22 21:34:05 -0600502 /*
503 * Just warn here till VFS can correctly support
504 * read-only remount without racing.
505 */
506 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100507
508 /* Push the superblock and write an unmount record */
509 error = xfs_log_sbcount(mp, 1);
510 if (error)
511 xfs_fs_cmn_err(CE_WARN, mp,
512 "xfs_attr_quiesce: failed to log sb changes. "
513 "Frozen image may not be consistent.");
514 xfs_log_unmount_write(mp);
515 xfs_unmountfs_writesb(mp);
516}
517
David Chinnere9f1c6e2008-10-30 17:15:50 +1100518/*
David Chinnera167b172008-10-30 17:06:18 +1100519 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
520 * Doing this has two advantages:
521 * - It saves on stack space, which is tight in certain situations
522 * - It can be used (with care) as a mechanism to avoid deadlocks.
523 * Flushing while allocating in a full filesystem requires both.
524 */
525STATIC void
526xfs_syncd_queue_work(
527 struct xfs_mount *mp,
528 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200529 void (*syncer)(struct xfs_mount *, void *),
530 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100531{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200532 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100533
Dave Chinnera8d770d2009-04-06 18:44:54 +0200534 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100535 INIT_LIST_HEAD(&work->w_list);
536 work->w_syncer = syncer;
537 work->w_data = data;
538 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200539 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100540 spin_lock(&mp->m_sync_lock);
541 list_add_tail(&work->w_list, &mp->m_sync_list);
542 spin_unlock(&mp->m_sync_lock);
543 wake_up_process(mp->m_sync_task);
544}
545
546/*
547 * Flush delayed allocate data, attempting to free up reserved space
548 * from existing allocations. At this point a new allocation attempt
549 * has failed with ENOSPC and we are in the process of scratching our
550 * heads, looking about for more room...
551 */
552STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200553xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100554 struct xfs_mount *mp,
555 void *arg)
556{
557 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200558 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200559 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100560 iput(inode);
561}
562
563void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200564xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100565 xfs_inode_t *ip)
566{
567 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200568 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100569
570 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200571 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
572 wait_for_completion(&completion);
David Chinnera167b172008-10-30 17:06:18 +1100573 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
574}
575
David Chinneraacaa882008-10-30 17:15:29 +1100576/*
577 * Every sync period we need to unpin all items, reclaim inodes, sync
578 * quota and write out the superblock. We might need to cover the log
579 * to indicate it is idle.
580 */
David Chinnera167b172008-10-30 17:06:18 +1100581STATIC void
582xfs_sync_worker(
583 struct xfs_mount *mp,
584 void *unused)
585{
586 int error;
587
David Chinneraacaa882008-10-30 17:15:29 +1100588 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
589 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
Dave Chinnerabc10642009-06-08 15:35:12 +0200590 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinneraacaa882008-10-30 17:15:29 +1100591 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200592 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
593 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
David Chinneraacaa882008-10-30 17:15:29 +1100594 }
David Chinnera167b172008-10-30 17:06:18 +1100595 mp->m_sync_seq++;
596 wake_up(&mp->m_wait_single_sync_task);
597}
598
599STATIC int
600xfssyncd(
601 void *arg)
602{
603 struct xfs_mount *mp = arg;
604 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200605 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100606 LIST_HEAD (tmp);
607
608 set_freezable();
609 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
610 for (;;) {
611 timeleft = schedule_timeout_interruptible(timeleft);
612 /* swsusp */
613 try_to_freeze();
614 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
615 break;
616
617 spin_lock(&mp->m_sync_lock);
618 /*
619 * We can get woken by laptop mode, to do a sync -
620 * that's the (only!) case where the list would be
621 * empty with time remaining.
622 */
623 if (!timeleft || list_empty(&mp->m_sync_list)) {
624 if (!timeleft)
625 timeleft = xfs_syncd_centisecs *
626 msecs_to_jiffies(10);
627 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
628 list_add_tail(&mp->m_sync_work.w_list,
629 &mp->m_sync_list);
630 }
631 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
632 list_move(&work->w_list, &tmp);
633 spin_unlock(&mp->m_sync_lock);
634
635 list_for_each_entry_safe(work, n, &tmp, w_list) {
636 (*work->w_syncer)(mp, work->w_data);
637 list_del(&work->w_list);
638 if (work == &mp->m_sync_work)
639 continue;
Dave Chinnere43afd72009-04-06 18:47:27 +0200640 if (work->w_completion)
641 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100642 kmem_free(work);
643 }
644 }
645
646 return 0;
647}
648
649int
650xfs_syncd_init(
651 struct xfs_mount *mp)
652{
653 mp->m_sync_work.w_syncer = xfs_sync_worker;
654 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200655 mp->m_sync_work.w_completion = NULL;
David Chinnera167b172008-10-30 17:06:18 +1100656 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
657 if (IS_ERR(mp->m_sync_task))
658 return -PTR_ERR(mp->m_sync_task);
659 return 0;
660}
661
662void
663xfs_syncd_stop(
664 struct xfs_mount *mp)
665{
666 kthread_stop(mp->m_sync_task);
667}
668
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400669void
670__xfs_inode_set_reclaim_tag(
671 struct xfs_perag *pag,
672 struct xfs_inode *ip)
673{
674 radix_tree_tag_set(&pag->pag_ici_root,
675 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
676 XFS_ICI_RECLAIM_TAG);
677}
678
David Chinner11654512008-10-30 17:37:49 +1100679/*
680 * We set the inode flag atomically with the radix tree tag.
681 * Once we get tag lookups on the radix tree, this inode flag
682 * can go away.
683 */
David Chinner396beb82008-10-30 17:37:26 +1100684void
685xfs_inode_set_reclaim_tag(
686 xfs_inode_t *ip)
687{
688 xfs_mount_t *mp = ip->i_mount;
689 xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
690
691 read_lock(&pag->pag_ici_lock);
692 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400693 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100694 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100695 spin_unlock(&ip->i_flags_lock);
696 read_unlock(&pag->pag_ici_lock);
697 xfs_put_perag(mp, pag);
698}
699
700void
701__xfs_inode_clear_reclaim_tag(
702 xfs_mount_t *mp,
703 xfs_perag_t *pag,
704 xfs_inode_t *ip)
705{
706 radix_tree_tag_clear(&pag->pag_ici_root,
707 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
708}
709
Dave Chinner75f3cb12009-06-08 15:35:14 +0200710STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000711xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200712 struct xfs_inode *ip,
713 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000714 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100715{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000716 /*
717 * The radix tree lock here protects a thread in xfs_iget from racing
718 * with us starting reclaim on the inode. Once we have the
719 * XFS_IRECLAIM flag set it will not touch us.
720 */
721 spin_lock(&ip->i_flags_lock);
722 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
723 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
724 /* ignore as it is already under reclaim */
725 spin_unlock(&ip->i_flags_lock);
726 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200727 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100728 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000729 __xfs_iflags_set(ip, XFS_IRECLAIM);
730 spin_unlock(&ip->i_flags_lock);
731 write_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100732
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000733 /*
734 * If the inode is still dirty, then flush it out. If the inode
735 * is not in the AIL, then it will be OK to flush it delwri as
736 * long as xfs_iflush() does not keep any references to the inode.
737 * We leave that decision up to xfs_iflush() since it has the
738 * knowledge of whether it's OK to simply do a delwri flush of
739 * the inode or whether we need to wait until the inode is
740 * pulled from the AIL.
741 * We get the flush lock regardless, though, just to make sure
742 * we don't free it while it is being flushed.
743 */
744 xfs_ilock(ip, XFS_ILOCK_EXCL);
745 xfs_iflock(ip);
746
747 /*
748 * In the case of a forced shutdown we rely on xfs_iflush() to
749 * wait for the inode to be unpinned before returning an error.
750 */
751 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
752 /* synchronize with xfs_iflush_done */
753 xfs_iflock(ip);
754 xfs_ifunlock(ip);
755 }
756
757 xfs_iunlock(ip, XFS_ILOCK_EXCL);
758 xfs_ireclaim(ip);
759 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100760}
761
David Chinnerfce08f22008-10-30 17:37:03 +1100762int
David Chinner1dc33182008-10-30 17:37:15 +1100763xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100764 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100765 int mode)
766{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000767 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
768 XFS_ICI_RECLAIM_TAG, 1);
David Chinnerfce08f22008-10-30 17:37:03 +1100769}