blob: 58c24be72c6509473b2823e1f4e84571b2d8cc37 [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,
Dave Chinner5017e972010-01-11 11:47:40 +000093 struct xfs_perag *pag,
Dave Chinner75f3cb12009-06-08 15:35:14 +020094 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{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200100 uint32_t first_index;
101 int last_error = 0;
102 int skipped;
103
104restart:
105 skipped = 0;
106 first_index = 0;
107 do {
108 int error = 0;
109 xfs_inode_t *ip;
110
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000111 if (exclusive)
112 write_lock(&pag->pag_ici_lock);
113 else
114 read_lock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200115 ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000116 if (!ip) {
117 if (exclusive)
118 write_unlock(&pag->pag_ici_lock);
119 else
120 read_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200121 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000122 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200123
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000124 /* execute releases pag->pag_ici_lock */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200125 error = execute(ip, pag, flags);
126 if (error == EAGAIN) {
127 skipped++;
128 continue;
129 }
130 if (error)
131 last_error = error;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000132
133 /* bail out if the filesystem is corrupted. */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200134 if (error == EFSCORRUPTED)
135 break;
136
137 } while (1);
138
139 if (skipped) {
140 delay(1);
141 goto restart;
142 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200143 return last_error;
144}
145
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200146int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200147xfs_inode_ag_iterator(
148 struct xfs_mount *mp,
149 int (*execute)(struct xfs_inode *ip,
150 struct xfs_perag *pag, int flags),
151 int flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000152 int tag,
153 int exclusive)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200154{
155 int error = 0;
156 int last_error = 0;
157 xfs_agnumber_t ag;
158
159 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
Dave Chinner5017e972010-01-11 11:47:40 +0000160 struct xfs_perag *pag;
161
162 pag = xfs_perag_get(mp, ag);
163 if (!pag->pag_ici_init) {
164 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200165 continue;
Dave Chinner5017e972010-01-11 11:47:40 +0000166 }
167 error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000168 exclusive);
Dave Chinner5017e972010-01-11 11:47:40 +0000169 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200170 if (error) {
171 last_error = error;
172 if (error == EFSCORRUPTED)
173 break;
174 }
175 }
176 return XFS_ERROR(last_error);
177}
178
Dave Chinner1da8eec2009-06-08 15:35:07 +0200179/* must be called with pag_ici_lock held and releases it */
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200180int
Dave Chinner1da8eec2009-06-08 15:35:07 +0200181xfs_sync_inode_valid(
182 struct xfs_inode *ip,
183 struct xfs_perag *pag)
184{
185 struct inode *inode = VFS_I(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000186 int error = EFSCORRUPTED;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200187
188 /* nothing to sync during shutdown */
Dave Chinner018027b2010-01-10 23:51:46 +0000189 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
190 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200191
Dave Chinner018027b2010-01-10 23:51:46 +0000192 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
193 error = ENOENT;
194 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
195 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200196
Dave Chinner018027b2010-01-10 23:51:46 +0000197 /* If we can't grab the inode, it must on it's way to reclaim. */
198 if (!igrab(inode))
199 goto out_unlock;
200
201 if (is_bad_inode(inode)) {
Dave Chinner1da8eec2009-06-08 15:35:07 +0200202 IRELE(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000203 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200204 }
205
Dave Chinner018027b2010-01-10 23:51:46 +0000206 /* inode is valid */
207 error = 0;
208out_unlock:
209 read_unlock(&pag->pag_ici_lock);
210 return error;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200211}
212
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200213STATIC int
214xfs_sync_inode_data(
215 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200216 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200217 int flags)
218{
219 struct inode *inode = VFS_I(ip);
220 struct address_space *mapping = inode->i_mapping;
221 int error = 0;
222
Dave Chinner75f3cb12009-06-08 15:35:14 +0200223 error = xfs_sync_inode_valid(ip, pag);
224 if (error)
225 return error;
226
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200227 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
228 goto out_wait;
229
230 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
231 if (flags & SYNC_TRYLOCK)
232 goto out_wait;
233 xfs_ilock(ip, XFS_IOLOCK_SHARED);
234 }
235
236 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000237 0 : XBF_ASYNC, FI_NONE);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200238 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
239
240 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200241 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200242 xfs_ioend_wait(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200243 IRELE(ip);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200244 return error;
245}
246
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200247STATIC int
248xfs_sync_inode_attr(
249 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200250 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200251 int flags)
252{
253 int error = 0;
254
Dave Chinner75f3cb12009-06-08 15:35:14 +0200255 error = xfs_sync_inode_valid(ip, pag);
256 if (error)
257 return error;
258
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200259 xfs_ilock(ip, XFS_ILOCK_SHARED);
260 if (xfs_inode_clean(ip))
261 goto out_unlock;
262 if (!xfs_iflock_nowait(ip)) {
263 if (!(flags & SYNC_WAIT))
264 goto out_unlock;
265 xfs_iflock(ip);
266 }
267
268 if (xfs_inode_clean(ip)) {
269 xfs_ifunlock(ip);
270 goto out_unlock;
271 }
272
273 error = xfs_iflush(ip, (flags & SYNC_WAIT) ?
274 XFS_IFLUSH_SYNC : XFS_IFLUSH_DELWRI);
275
276 out_unlock:
277 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200278 IRELE(ip);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200279 return error;
280}
281
Christoph Hellwig075fe102009-06-08 15:35:48 +0200282/*
283 * Write out pagecache data for the whole filesystem.
284 */
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100285int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200286xfs_sync_data(
287 struct xfs_mount *mp,
288 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100289{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200290 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100291
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200292 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100293
Christoph Hellwig075fe102009-06-08 15:35:48 +0200294 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000295 XFS_ICI_NO_TAG, 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200296 if (error)
297 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100298
Christoph Hellwig075fe102009-06-08 15:35:48 +0200299 xfs_log_force(mp, 0,
300 (flags & SYNC_WAIT) ?
301 XFS_LOG_FORCE | XFS_LOG_SYNC :
302 XFS_LOG_FORCE);
303 return 0;
304}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100305
Christoph Hellwig075fe102009-06-08 15:35:48 +0200306/*
307 * Write out inode metadata (attributes) for the whole filesystem.
308 */
309int
310xfs_sync_attr(
311 struct xfs_mount *mp,
312 int flags)
313{
314 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200315
Christoph Hellwig075fe102009-06-08 15:35:48 +0200316 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000317 XFS_ICI_NO_TAG, 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100318}
319
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100320STATIC int
321xfs_commit_dummy_trans(
322 struct xfs_mount *mp,
Dave Chinnerdce50652009-10-06 20:29:30 +0000323 uint flags)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100324{
325 struct xfs_inode *ip = mp->m_rootip;
326 struct xfs_trans *tp;
327 int error;
Dave Chinnerdce50652009-10-06 20:29:30 +0000328 int log_flags = XFS_LOG_FORCE;
329
330 if (flags & SYNC_WAIT)
331 log_flags |= XFS_LOG_SYNC;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100332
333 /*
334 * Put a dummy transaction in the log to tell recovery
335 * that all others are OK.
336 */
337 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
338 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
339 if (error) {
340 xfs_trans_cancel(tp, 0);
341 return error;
342 }
343
344 xfs_ilock(ip, XFS_ILOCK_EXCL);
345
346 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
347 xfs_trans_ihold(tp, ip);
348 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100349 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100350 xfs_iunlock(ip, XFS_ILOCK_EXCL);
351
Dave Chinnerdce50652009-10-06 20:29:30 +0000352 /* the log force ensures this transaction is pushed to disk */
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100353 xfs_log_force(mp, 0, log_flags);
Dave Chinnerdce50652009-10-06 20:29:30 +0000354 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100355}
356
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000357STATIC int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100358xfs_sync_fsdata(
359 struct xfs_mount *mp,
360 int flags)
361{
362 struct xfs_buf *bp;
363 struct xfs_buf_log_item *bip;
364 int error = 0;
365
366 /*
367 * If this is xfssyncd() then only sync the superblock if we can
368 * lock it without sleeping and it is not pinned.
369 */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200370 if (flags & SYNC_TRYLOCK) {
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100371 ASSERT(!(flags & SYNC_WAIT));
372
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000373 bp = xfs_getsb(mp, XBF_TRYLOCK);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100374 if (!bp)
375 goto out;
376
377 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
378 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
379 goto out_brelse;
380 } else {
381 bp = xfs_getsb(mp, 0);
382
383 /*
384 * If the buffer is pinned then push on the log so we won't
385 * get stuck waiting in the write for someone, maybe
386 * ourselves, to flush the log.
387 *
388 * Even though we just pushed the log above, we did not have
389 * the superblock buffer locked at that point so it can
390 * become pinned in between there and here.
391 */
392 if (XFS_BUF_ISPINNED(bp))
393 xfs_log_force(mp, 0, XFS_LOG_FORCE);
394 }
395
396
397 if (flags & SYNC_WAIT)
398 XFS_BUF_UNASYNC(bp);
399 else
400 XFS_BUF_ASYNC(bp);
401
Dave Chinnerdce50652009-10-06 20:29:30 +0000402 error = xfs_bwrite(mp, bp);
403 if (error)
404 return error;
405
406 /*
407 * If this is a data integrity sync make sure all pending buffers
408 * are flushed out for the log coverage check below.
409 */
410 if (flags & SYNC_WAIT)
411 xfs_flush_buftarg(mp->m_ddev_targp, 1);
412
413 if (xfs_log_need_covered(mp))
414 error = xfs_commit_dummy_trans(mp, flags);
415 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100416
417 out_brelse:
418 xfs_buf_relse(bp);
419 out:
420 return error;
421}
422
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100423/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100424 * When remounting a filesystem read-only or freezing the filesystem, we have
425 * two phases to execute. This first phase is syncing the data before we
426 * quiesce the filesystem, and the second is flushing all the inodes out after
427 * we've waited for all the transactions created by the first phase to
428 * complete. The second phase ensures that the inodes are written to their
429 * location on disk rather than just existing in transactions in the log. This
430 * means after a quiesce there is no log replay required to write the inodes to
431 * disk (this is the main difference between a sync and a quiesce).
432 */
433/*
434 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100435 * so we flush delwri and delalloc buffers here, then wait for all I/O to
436 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100437 * transactions can still occur here so don't bother flushing the buftarg
438 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100439 */
440int
441xfs_quiesce_data(
442 struct xfs_mount *mp)
443{
444 int error;
445
446 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200447 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200448 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100449
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000450 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200451 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200452 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100453
David Chinnera4e4c4f2008-10-30 17:16:11 +1100454 /* write superblock and hoover up shutdown errors */
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000455 error = xfs_sync_fsdata(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100456
David Chinnera4e4c4f2008-10-30 17:16:11 +1100457 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100458 if (mp->m_rtdev_targp)
459 XFS_bflush(mp->m_rtdev_targp);
460
461 return error;
462}
463
David Chinner76bf1052008-10-30 17:16:21 +1100464STATIC void
465xfs_quiesce_fs(
466 struct xfs_mount *mp)
467{
468 int count = 0, pincount;
469
470 xfs_flush_buftarg(mp->m_ddev_targp, 0);
Dave Chinnerabc10642009-06-08 15:35:12 +0200471 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinner76bf1052008-10-30 17:16:21 +1100472
473 /*
474 * This loop must run at least twice. The first instance of the loop
475 * will flush most meta data but that will generate more meta data
476 * (typically directory updates). Which then must be flushed and
477 * logged before we can write the unmount record.
478 */
479 do {
Christoph Hellwig075fe102009-06-08 15:35:48 +0200480 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100481 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
482 if (!pincount) {
483 delay(50);
484 count++;
485 }
486 } while (count < 2);
487}
488
489/*
490 * Second stage of a quiesce. The data is already synced, now we have to take
491 * care of the metadata. New transactions are already blocked, so we need to
492 * wait for any remaining transactions to drain out before proceding.
493 */
494void
495xfs_quiesce_attr(
496 struct xfs_mount *mp)
497{
498 int error = 0;
499
500 /* wait for all modifications to complete */
501 while (atomic_read(&mp->m_active_trans) > 0)
502 delay(100);
503
504 /* flush inodes and push all remaining buffers out to disk */
505 xfs_quiesce_fs(mp);
506
Felix Blyakher5e106572009-01-22 21:34:05 -0600507 /*
508 * Just warn here till VFS can correctly support
509 * read-only remount without racing.
510 */
511 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100512
513 /* Push the superblock and write an unmount record */
514 error = xfs_log_sbcount(mp, 1);
515 if (error)
516 xfs_fs_cmn_err(CE_WARN, mp,
517 "xfs_attr_quiesce: failed to log sb changes. "
518 "Frozen image may not be consistent.");
519 xfs_log_unmount_write(mp);
520 xfs_unmountfs_writesb(mp);
521}
522
David Chinnere9f1c6e2008-10-30 17:15:50 +1100523/*
David Chinnera167b172008-10-30 17:06:18 +1100524 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
525 * Doing this has two advantages:
526 * - It saves on stack space, which is tight in certain situations
527 * - It can be used (with care) as a mechanism to avoid deadlocks.
528 * Flushing while allocating in a full filesystem requires both.
529 */
530STATIC void
531xfs_syncd_queue_work(
532 struct xfs_mount *mp,
533 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200534 void (*syncer)(struct xfs_mount *, void *),
535 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100536{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200537 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100538
Dave Chinnera8d770d2009-04-06 18:44:54 +0200539 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100540 INIT_LIST_HEAD(&work->w_list);
541 work->w_syncer = syncer;
542 work->w_data = data;
543 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200544 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100545 spin_lock(&mp->m_sync_lock);
546 list_add_tail(&work->w_list, &mp->m_sync_list);
547 spin_unlock(&mp->m_sync_lock);
548 wake_up_process(mp->m_sync_task);
549}
550
551/*
552 * Flush delayed allocate data, attempting to free up reserved space
553 * from existing allocations. At this point a new allocation attempt
554 * has failed with ENOSPC and we are in the process of scratching our
555 * heads, looking about for more room...
556 */
557STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200558xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100559 struct xfs_mount *mp,
560 void *arg)
561{
562 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200563 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200564 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100565 iput(inode);
566}
567
568void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200569xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100570 xfs_inode_t *ip)
571{
572 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200573 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100574
575 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200576 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
577 wait_for_completion(&completion);
David Chinnera167b172008-10-30 17:06:18 +1100578 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
579}
580
David Chinneraacaa882008-10-30 17:15:29 +1100581/*
582 * Every sync period we need to unpin all items, reclaim inodes, sync
583 * quota and write out the superblock. We might need to cover the log
584 * to indicate it is idle.
585 */
David Chinnera167b172008-10-30 17:06:18 +1100586STATIC void
587xfs_sync_worker(
588 struct xfs_mount *mp,
589 void *unused)
590{
591 int error;
592
David Chinneraacaa882008-10-30 17:15:29 +1100593 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
594 xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
Dave Chinnerabc10642009-06-08 15:35:12 +0200595 xfs_reclaim_inodes(mp, XFS_IFLUSH_DELWRI_ELSE_ASYNC);
David Chinneraacaa882008-10-30 17:15:29 +1100596 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200597 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
598 error = xfs_sync_fsdata(mp, SYNC_TRYLOCK);
David Chinneraacaa882008-10-30 17:15:29 +1100599 }
David Chinnera167b172008-10-30 17:06:18 +1100600 mp->m_sync_seq++;
601 wake_up(&mp->m_wait_single_sync_task);
602}
603
604STATIC int
605xfssyncd(
606 void *arg)
607{
608 struct xfs_mount *mp = arg;
609 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200610 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100611 LIST_HEAD (tmp);
612
613 set_freezable();
614 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
615 for (;;) {
616 timeleft = schedule_timeout_interruptible(timeleft);
617 /* swsusp */
618 try_to_freeze();
619 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
620 break;
621
622 spin_lock(&mp->m_sync_lock);
623 /*
624 * We can get woken by laptop mode, to do a sync -
625 * that's the (only!) case where the list would be
626 * empty with time remaining.
627 */
628 if (!timeleft || list_empty(&mp->m_sync_list)) {
629 if (!timeleft)
630 timeleft = xfs_syncd_centisecs *
631 msecs_to_jiffies(10);
632 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
633 list_add_tail(&mp->m_sync_work.w_list,
634 &mp->m_sync_list);
635 }
636 list_for_each_entry_safe(work, n, &mp->m_sync_list, w_list)
637 list_move(&work->w_list, &tmp);
638 spin_unlock(&mp->m_sync_lock);
639
640 list_for_each_entry_safe(work, n, &tmp, w_list) {
641 (*work->w_syncer)(mp, work->w_data);
642 list_del(&work->w_list);
643 if (work == &mp->m_sync_work)
644 continue;
Dave Chinnere43afd72009-04-06 18:47:27 +0200645 if (work->w_completion)
646 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100647 kmem_free(work);
648 }
649 }
650
651 return 0;
652}
653
654int
655xfs_syncd_init(
656 struct xfs_mount *mp)
657{
658 mp->m_sync_work.w_syncer = xfs_sync_worker;
659 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200660 mp->m_sync_work.w_completion = NULL;
David Chinnera167b172008-10-30 17:06:18 +1100661 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd");
662 if (IS_ERR(mp->m_sync_task))
663 return -PTR_ERR(mp->m_sync_task);
664 return 0;
665}
666
667void
668xfs_syncd_stop(
669 struct xfs_mount *mp)
670{
671 kthread_stop(mp->m_sync_task);
672}
673
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400674void
675__xfs_inode_set_reclaim_tag(
676 struct xfs_perag *pag,
677 struct xfs_inode *ip)
678{
679 radix_tree_tag_set(&pag->pag_ici_root,
680 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
681 XFS_ICI_RECLAIM_TAG);
682}
683
David Chinner11654512008-10-30 17:37:49 +1100684/*
685 * We set the inode flag atomically with the radix tree tag.
686 * Once we get tag lookups on the radix tree, this inode flag
687 * can go away.
688 */
David Chinner396beb82008-10-30 17:37:26 +1100689void
690xfs_inode_set_reclaim_tag(
691 xfs_inode_t *ip)
692{
Dave Chinner5017e972010-01-11 11:47:40 +0000693 struct xfs_mount *mp = ip->i_mount;
694 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100695
Dave Chinner5017e972010-01-11 11:47:40 +0000696 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinner396beb82008-10-30 17:37:26 +1100697 read_lock(&pag->pag_ici_lock);
698 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400699 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100700 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100701 spin_unlock(&ip->i_flags_lock);
702 read_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000703 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100704}
705
706void
707__xfs_inode_clear_reclaim_tag(
708 xfs_mount_t *mp,
709 xfs_perag_t *pag,
710 xfs_inode_t *ip)
711{
712 radix_tree_tag_clear(&pag->pag_ici_root,
713 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
714}
715
Dave Chinner75f3cb12009-06-08 15:35:14 +0200716STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000717xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200718 struct xfs_inode *ip,
719 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000720 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100721{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000722 /*
723 * The radix tree lock here protects a thread in xfs_iget from racing
724 * with us starting reclaim on the inode. Once we have the
725 * XFS_IRECLAIM flag set it will not touch us.
726 */
727 spin_lock(&ip->i_flags_lock);
728 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
729 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
730 /* ignore as it is already under reclaim */
731 spin_unlock(&ip->i_flags_lock);
732 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200733 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100734 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000735 __xfs_iflags_set(ip, XFS_IRECLAIM);
736 spin_unlock(&ip->i_flags_lock);
737 write_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100738
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000739 /*
740 * If the inode is still dirty, then flush it out. If the inode
741 * is not in the AIL, then it will be OK to flush it delwri as
742 * long as xfs_iflush() does not keep any references to the inode.
743 * We leave that decision up to xfs_iflush() since it has the
744 * knowledge of whether it's OK to simply do a delwri flush of
745 * the inode or whether we need to wait until the inode is
746 * pulled from the AIL.
747 * We get the flush lock regardless, though, just to make sure
748 * we don't free it while it is being flushed.
749 */
750 xfs_ilock(ip, XFS_ILOCK_EXCL);
751 xfs_iflock(ip);
752
753 /*
754 * In the case of a forced shutdown we rely on xfs_iflush() to
755 * wait for the inode to be unpinned before returning an error.
756 */
757 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
758 /* synchronize with xfs_iflush_done */
759 xfs_iflock(ip);
760 xfs_ifunlock(ip);
761 }
762
763 xfs_iunlock(ip, XFS_ILOCK_EXCL);
764 xfs_ireclaim(ip);
765 return 0;
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 Chinnerc8e20be2010-01-10 23:51:45 +0000773 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
774 XFS_ICI_RECLAIM_TAG, 1);
David Chinnerfce08f22008-10-30 17:37:03 +1100775}