blob: c9b863eacab728efbe5c4252835d77c8c79ade99 [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 Hellwiga14a3482010-01-19 09:56:46 +0000299 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200300 return 0;
301}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100302
Christoph Hellwig075fe102009-06-08 15:35:48 +0200303/*
304 * Write out inode metadata (attributes) for the whole filesystem.
305 */
306int
307xfs_sync_attr(
308 struct xfs_mount *mp,
309 int flags)
310{
311 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200312
Christoph Hellwig075fe102009-06-08 15:35:48 +0200313 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000314 XFS_ICI_NO_TAG, 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100315}
316
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100317STATIC int
318xfs_commit_dummy_trans(
319 struct xfs_mount *mp,
Dave Chinnerdce50652009-10-06 20:29:30 +0000320 uint flags)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100321{
322 struct xfs_inode *ip = mp->m_rootip;
323 struct xfs_trans *tp;
324 int error;
325
326 /*
327 * Put a dummy transaction in the log to tell recovery
328 * that all others are OK.
329 */
330 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
331 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
332 if (error) {
333 xfs_trans_cancel(tp, 0);
334 return error;
335 }
336
337 xfs_ilock(ip, XFS_ILOCK_EXCL);
338
339 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
340 xfs_trans_ihold(tp, ip);
341 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100342 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100343 xfs_iunlock(ip, XFS_ILOCK_EXCL);
344
Dave Chinnerdce50652009-10-06 20:29:30 +0000345 /* the log force ensures this transaction is pushed to disk */
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000346 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Dave Chinnerdce50652009-10-06 20:29:30 +0000347 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100348}
349
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000350STATIC int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100351xfs_sync_fsdata(
352 struct xfs_mount *mp,
353 int flags)
354{
355 struct xfs_buf *bp;
356 struct xfs_buf_log_item *bip;
357 int error = 0;
358
359 /*
360 * If this is xfssyncd() then only sync the superblock if we can
361 * lock it without sleeping and it is not pinned.
362 */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200363 if (flags & SYNC_TRYLOCK) {
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100364 ASSERT(!(flags & SYNC_WAIT));
365
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000366 bp = xfs_getsb(mp, XBF_TRYLOCK);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100367 if (!bp)
368 goto out;
369
370 bip = XFS_BUF_FSPRIVATE(bp, struct xfs_buf_log_item *);
371 if (!bip || !xfs_buf_item_dirty(bip) || XFS_BUF_ISPINNED(bp))
372 goto out_brelse;
373 } else {
374 bp = xfs_getsb(mp, 0);
375
376 /*
377 * If the buffer is pinned then push on the log so we won't
378 * get stuck waiting in the write for someone, maybe
379 * ourselves, to flush the log.
380 *
381 * Even though we just pushed the log above, we did not have
382 * the superblock buffer locked at that point so it can
383 * become pinned in between there and here.
384 */
385 if (XFS_BUF_ISPINNED(bp))
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000386 xfs_log_force(mp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100387 }
388
389
390 if (flags & SYNC_WAIT)
391 XFS_BUF_UNASYNC(bp);
392 else
393 XFS_BUF_ASYNC(bp);
394
Dave Chinnerdce50652009-10-06 20:29:30 +0000395 error = xfs_bwrite(mp, bp);
396 if (error)
397 return error;
398
399 /*
400 * If this is a data integrity sync make sure all pending buffers
401 * are flushed out for the log coverage check below.
402 */
403 if (flags & SYNC_WAIT)
404 xfs_flush_buftarg(mp->m_ddev_targp, 1);
405
406 if (xfs_log_need_covered(mp))
407 error = xfs_commit_dummy_trans(mp, flags);
408 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100409
410 out_brelse:
411 xfs_buf_relse(bp);
412 out:
413 return error;
414}
415
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100416/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100417 * When remounting a filesystem read-only or freezing the filesystem, we have
418 * two phases to execute. This first phase is syncing the data before we
419 * quiesce the filesystem, and the second is flushing all the inodes out after
420 * we've waited for all the transactions created by the first phase to
421 * complete. The second phase ensures that the inodes are written to their
422 * location on disk rather than just existing in transactions in the log. This
423 * means after a quiesce there is no log replay required to write the inodes to
424 * disk (this is the main difference between a sync and a quiesce).
425 */
426/*
427 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100428 * so we flush delwri and delalloc buffers here, then wait for all I/O to
429 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100430 * transactions can still occur here so don't bother flushing the buftarg
431 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100432 */
433int
434xfs_quiesce_data(
435 struct xfs_mount *mp)
436{
437 int error;
438
439 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200440 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200441 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100442
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000443 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200444 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200445 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100446
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);
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000571 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
David Chinnera167b172008-10-30 17:06:18 +1100572}
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)) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000587 xfs_log_force(mp, 0);
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 Hellwigbc990f52009-08-16 20:36:34 -0400667void
668__xfs_inode_set_reclaim_tag(
669 struct xfs_perag *pag,
670 struct xfs_inode *ip)
671{
672 radix_tree_tag_set(&pag->pag_ici_root,
673 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
674 XFS_ICI_RECLAIM_TAG);
675}
676
David Chinner11654512008-10-30 17:37:49 +1100677/*
678 * We set the inode flag atomically with the radix tree tag.
679 * Once we get tag lookups on the radix tree, this inode flag
680 * can go away.
681 */
David Chinner396beb82008-10-30 17:37:26 +1100682void
683xfs_inode_set_reclaim_tag(
684 xfs_inode_t *ip)
685{
Dave Chinner5017e972010-01-11 11:47:40 +0000686 struct xfs_mount *mp = ip->i_mount;
687 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100688
Dave Chinner5017e972010-01-11 11:47:40 +0000689 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
David Chinner396beb82008-10-30 17:37:26 +1100690 read_lock(&pag->pag_ici_lock);
691 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400692 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100693 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100694 spin_unlock(&ip->i_flags_lock);
695 read_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000696 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100697}
698
699void
700__xfs_inode_clear_reclaim_tag(
701 xfs_mount_t *mp,
702 xfs_perag_t *pag,
703 xfs_inode_t *ip)
704{
705 radix_tree_tag_clear(&pag->pag_ici_root,
706 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
707}
708
Dave Chinner75f3cb12009-06-08 15:35:14 +0200709STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000710xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200711 struct xfs_inode *ip,
712 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000713 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100714{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000715 /*
716 * The radix tree lock here protects a thread in xfs_iget from racing
717 * with us starting reclaim on the inode. Once we have the
718 * XFS_IRECLAIM flag set it will not touch us.
719 */
720 spin_lock(&ip->i_flags_lock);
721 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
722 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
723 /* ignore as it is already under reclaim */
724 spin_unlock(&ip->i_flags_lock);
725 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200726 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100727 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000728 __xfs_iflags_set(ip, XFS_IRECLAIM);
729 spin_unlock(&ip->i_flags_lock);
730 write_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100731
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000732 /*
733 * If the inode is still dirty, then flush it out. If the inode
734 * is not in the AIL, then it will be OK to flush it delwri as
735 * long as xfs_iflush() does not keep any references to the inode.
736 * We leave that decision up to xfs_iflush() since it has the
737 * knowledge of whether it's OK to simply do a delwri flush of
738 * the inode or whether we need to wait until the inode is
739 * pulled from the AIL.
740 * We get the flush lock regardless, though, just to make sure
741 * we don't free it while it is being flushed.
742 */
743 xfs_ilock(ip, XFS_ILOCK_EXCL);
744 xfs_iflock(ip);
745
746 /*
747 * In the case of a forced shutdown we rely on xfs_iflush() to
748 * wait for the inode to be unpinned before returning an error.
749 */
750 if (!is_bad_inode(VFS_I(ip)) && xfs_iflush(ip, sync_mode) == 0) {
751 /* synchronize with xfs_iflush_done */
752 xfs_iflock(ip);
753 xfs_ifunlock(ip);
754 }
755
756 xfs_iunlock(ip, XFS_ILOCK_EXCL);
757 xfs_ireclaim(ip);
758 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100759}
760
David Chinnerfce08f22008-10-30 17:37:03 +1100761int
David Chinner1dc33182008-10-30 17:37:15 +1100762xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100763 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100764 int mode)
765{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000766 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
767 XFS_ICI_RECLAIM_TAG, 1);
David Chinnerfce08f22008-10-30 17:37:03 +1100768}