blob: ef7f0218bccb45779157128139ab552e8b423832 [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,
Dave Chinner9bf729c2010-04-29 09:55:50 +100098 int exclusive,
99 int *nr_to_scan)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200100{
Dave Chinner75f3cb12009-06-08 15:35:14 +0200101 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
Dave Chinner9bf729c2010-04-29 09:55:50 +1000138 } while ((*nr_to_scan)--);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200139
140 if (skipped) {
141 delay(1);
142 goto restart;
143 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200144 return last_error;
145}
146
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200147int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200148xfs_inode_ag_iterator(
149 struct xfs_mount *mp,
150 int (*execute)(struct xfs_inode *ip,
151 struct xfs_perag *pag, int flags),
152 int flags,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000153 int tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000154 int exclusive,
155 int *nr_to_scan)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200156{
157 int error = 0;
158 int last_error = 0;
159 xfs_agnumber_t ag;
Dave Chinner9bf729c2010-04-29 09:55:50 +1000160 int nr;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200161
Dave Chinner9bf729c2010-04-29 09:55:50 +1000162 nr = nr_to_scan ? *nr_to_scan : INT_MAX;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200163 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
Dave Chinner5017e972010-01-11 11:47:40 +0000164 struct xfs_perag *pag;
165
166 pag = xfs_perag_get(mp, ag);
Dave Chinner5017e972010-01-11 11:47:40 +0000167 error = xfs_inode_ag_walk(mp, pag, execute, flags, tag,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000168 exclusive, &nr);
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 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000175 if (nr <= 0)
176 break;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200177 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000178 if (nr_to_scan)
179 *nr_to_scan = nr;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200180 return XFS_ERROR(last_error);
181}
182
Dave Chinner1da8eec2009-06-08 15:35:07 +0200183/* must be called with pag_ici_lock held and releases it */
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200184int
Dave Chinner1da8eec2009-06-08 15:35:07 +0200185xfs_sync_inode_valid(
186 struct xfs_inode *ip,
187 struct xfs_perag *pag)
188{
189 struct inode *inode = VFS_I(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000190 int error = EFSCORRUPTED;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200191
192 /* nothing to sync during shutdown */
Dave Chinner018027b2010-01-10 23:51:46 +0000193 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
194 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200195
Dave Chinner018027b2010-01-10 23:51:46 +0000196 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
197 error = ENOENT;
198 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
199 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200200
Dave Chinner018027b2010-01-10 23:51:46 +0000201 /* If we can't grab the inode, it must on it's way to reclaim. */
202 if (!igrab(inode))
203 goto out_unlock;
204
205 if (is_bad_inode(inode)) {
Dave Chinner1da8eec2009-06-08 15:35:07 +0200206 IRELE(ip);
Dave Chinner018027b2010-01-10 23:51:46 +0000207 goto out_unlock;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200208 }
209
Dave Chinner018027b2010-01-10 23:51:46 +0000210 /* inode is valid */
211 error = 0;
212out_unlock:
213 read_unlock(&pag->pag_ici_lock);
214 return error;
Dave Chinner1da8eec2009-06-08 15:35:07 +0200215}
216
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200217STATIC int
218xfs_sync_inode_data(
219 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200220 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200221 int flags)
222{
223 struct inode *inode = VFS_I(ip);
224 struct address_space *mapping = inode->i_mapping;
225 int error = 0;
226
Dave Chinner75f3cb12009-06-08 15:35:14 +0200227 error = xfs_sync_inode_valid(ip, pag);
228 if (error)
229 return error;
230
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200231 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
232 goto out_wait;
233
234 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
235 if (flags & SYNC_TRYLOCK)
236 goto out_wait;
237 xfs_ilock(ip, XFS_IOLOCK_SHARED);
238 }
239
240 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000241 0 : XBF_ASYNC, FI_NONE);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200242 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
243
244 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200245 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200246 xfs_ioend_wait(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200247 IRELE(ip);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200248 return error;
249}
250
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200251STATIC int
252xfs_sync_inode_attr(
253 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200254 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200255 int flags)
256{
257 int error = 0;
258
Dave Chinner75f3cb12009-06-08 15:35:14 +0200259 error = xfs_sync_inode_valid(ip, pag);
260 if (error)
261 return error;
262
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200263 xfs_ilock(ip, XFS_ILOCK_SHARED);
264 if (xfs_inode_clean(ip))
265 goto out_unlock;
266 if (!xfs_iflock_nowait(ip)) {
267 if (!(flags & SYNC_WAIT))
268 goto out_unlock;
269 xfs_iflock(ip);
270 }
271
272 if (xfs_inode_clean(ip)) {
273 xfs_ifunlock(ip);
274 goto out_unlock;
275 }
276
Dave Chinnerc8543632010-02-06 12:39:36 +1100277 error = xfs_iflush(ip, flags);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200278
279 out_unlock:
280 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200281 IRELE(ip);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200282 return error;
283}
284
Christoph Hellwig075fe102009-06-08 15:35:48 +0200285/*
286 * Write out pagecache data for the whole filesystem.
287 */
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100288int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200289xfs_sync_data(
290 struct xfs_mount *mp,
291 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100292{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200293 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100294
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200295 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100296
Christoph Hellwig075fe102009-06-08 15:35:48 +0200297 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000298 XFS_ICI_NO_TAG, 0, NULL);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200299 if (error)
300 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100301
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000302 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200303 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 Chinner9bf729c2010-04-29 09:55:50 +1000317 XFS_ICI_NO_TAG, 0, NULL);
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;
328
329 /*
330 * Put a dummy transaction in the log to tell recovery
331 * that all others are OK.
332 */
333 tp = xfs_trans_alloc(mp, XFS_TRANS_DUMMY1);
334 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
335 if (error) {
336 xfs_trans_cancel(tp, 0);
337 return error;
338 }
339
340 xfs_ilock(ip, XFS_ILOCK_EXCL);
341
342 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
343 xfs_trans_ihold(tp, ip);
344 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100345 error = xfs_trans_commit(tp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100346 xfs_iunlock(ip, XFS_ILOCK_EXCL);
347
Dave Chinnerdce50652009-10-06 20:29:30 +0000348 /* the log force ensures this transaction is pushed to disk */
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000349 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Dave Chinnerdce50652009-10-06 20:29:30 +0000350 return error;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100351}
352
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000353STATIC int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100354xfs_sync_fsdata(
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000355 struct xfs_mount *mp)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100356{
357 struct xfs_buf *bp;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100358
359 /*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000360 * If the buffer is pinned then push on the log so we won't get stuck
361 * waiting in the write for someone, maybe ourselves, to flush the log.
362 *
363 * Even though we just pushed the log above, we did not have the
364 * superblock buffer locked at that point so it can become pinned in
365 * between there and here.
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100366 */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000367 bp = xfs_getsb(mp, 0);
368 if (XFS_BUF_ISPINNED(bp))
369 xfs_log_force(mp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100370
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000371 return xfs_bwrite(mp, bp);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100372}
373
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100374/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100375 * When remounting a filesystem read-only or freezing the filesystem, we have
376 * two phases to execute. This first phase is syncing the data before we
377 * quiesce the filesystem, and the second is flushing all the inodes out after
378 * we've waited for all the transactions created by the first phase to
379 * complete. The second phase ensures that the inodes are written to their
380 * location on disk rather than just existing in transactions in the log. This
381 * means after a quiesce there is no log replay required to write the inodes to
382 * disk (this is the main difference between a sync and a quiesce).
383 */
384/*
385 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100386 * so we flush delwri and delalloc buffers here, then wait for all I/O to
387 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100388 * transactions can still occur here so don't bother flushing the buftarg
389 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100390 */
391int
392xfs_quiesce_data(
393 struct xfs_mount *mp)
394{
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000395 int error, error2 = 0;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100396
397 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200398 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200399 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100400
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000401 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200402 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200403 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100404
David Chinnera4e4c4f2008-10-30 17:16:11 +1100405 /* write superblock and hoover up shutdown errors */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000406 error = xfs_sync_fsdata(mp);
407
408 /* make sure all delwri buffers are written out */
409 xfs_flush_buftarg(mp->m_ddev_targp, 1);
410
411 /* mark the log as covered if needed */
412 if (xfs_log_need_covered(mp))
413 error2 = xfs_commit_dummy_trans(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100414
David Chinnera4e4c4f2008-10-30 17:16:11 +1100415 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100416 if (mp->m_rtdev_targp)
417 XFS_bflush(mp->m_rtdev_targp);
418
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000419 return error ? error : error2;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100420}
421
David Chinner76bf1052008-10-30 17:16:21 +1100422STATIC void
423xfs_quiesce_fs(
424 struct xfs_mount *mp)
425{
426 int count = 0, pincount;
427
Dave Chinnerc8543632010-02-06 12:39:36 +1100428 xfs_reclaim_inodes(mp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100429 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100430
431 /*
432 * This loop must run at least twice. The first instance of the loop
433 * will flush most meta data but that will generate more meta data
434 * (typically directory updates). Which then must be flushed and
Dave Chinnerc8543632010-02-06 12:39:36 +1100435 * logged before we can write the unmount record. We also so sync
436 * reclaim of inodes to catch any that the above delwri flush skipped.
David Chinner76bf1052008-10-30 17:16:21 +1100437 */
438 do {
Dave Chinnerc8543632010-02-06 12:39:36 +1100439 xfs_reclaim_inodes(mp, SYNC_WAIT);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200440 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100441 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
442 if (!pincount) {
443 delay(50);
444 count++;
445 }
446 } while (count < 2);
447}
448
449/*
450 * Second stage of a quiesce. The data is already synced, now we have to take
451 * care of the metadata. New transactions are already blocked, so we need to
452 * wait for any remaining transactions to drain out before proceding.
453 */
454void
455xfs_quiesce_attr(
456 struct xfs_mount *mp)
457{
458 int error = 0;
459
460 /* wait for all modifications to complete */
461 while (atomic_read(&mp->m_active_trans) > 0)
462 delay(100);
463
464 /* flush inodes and push all remaining buffers out to disk */
465 xfs_quiesce_fs(mp);
466
Felix Blyakher5e106572009-01-22 21:34:05 -0600467 /*
468 * Just warn here till VFS can correctly support
469 * read-only remount without racing.
470 */
471 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100472
473 /* Push the superblock and write an unmount record */
474 error = xfs_log_sbcount(mp, 1);
475 if (error)
476 xfs_fs_cmn_err(CE_WARN, mp,
477 "xfs_attr_quiesce: failed to log sb changes. "
478 "Frozen image may not be consistent.");
479 xfs_log_unmount_write(mp);
480 xfs_unmountfs_writesb(mp);
481}
482
David Chinnere9f1c6e2008-10-30 17:15:50 +1100483/*
David Chinnera167b172008-10-30 17:06:18 +1100484 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
485 * Doing this has two advantages:
486 * - It saves on stack space, which is tight in certain situations
487 * - It can be used (with care) as a mechanism to avoid deadlocks.
488 * Flushing while allocating in a full filesystem requires both.
489 */
490STATIC void
491xfs_syncd_queue_work(
492 struct xfs_mount *mp,
493 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200494 void (*syncer)(struct xfs_mount *, void *),
495 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100496{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200497 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100498
Dave Chinnera8d770d2009-04-06 18:44:54 +0200499 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100500 INIT_LIST_HEAD(&work->w_list);
501 work->w_syncer = syncer;
502 work->w_data = data;
503 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200504 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100505 spin_lock(&mp->m_sync_lock);
506 list_add_tail(&work->w_list, &mp->m_sync_list);
507 spin_unlock(&mp->m_sync_lock);
508 wake_up_process(mp->m_sync_task);
509}
510
511/*
512 * Flush delayed allocate data, attempting to free up reserved space
513 * from existing allocations. At this point a new allocation attempt
514 * has failed with ENOSPC and we are in the process of scratching our
515 * heads, looking about for more room...
516 */
517STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200518xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100519 struct xfs_mount *mp,
520 void *arg)
521{
522 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200523 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200524 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100525 iput(inode);
526}
527
528void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200529xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100530 xfs_inode_t *ip)
531{
532 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200533 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100534
535 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200536 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
537 wait_for_completion(&completion);
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000538 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
David Chinnera167b172008-10-30 17:06:18 +1100539}
540
David Chinneraacaa882008-10-30 17:15:29 +1100541/*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000542 * Every sync period we need to unpin all items, reclaim inodes and sync
543 * disk quotas. We might need to cover the log to indicate that the
544 * filesystem is idle.
David Chinneraacaa882008-10-30 17:15:29 +1100545 */
David Chinnera167b172008-10-30 17:06:18 +1100546STATIC void
547xfs_sync_worker(
548 struct xfs_mount *mp,
549 void *unused)
550{
551 int error;
552
David Chinneraacaa882008-10-30 17:15:29 +1100553 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000554 xfs_log_force(mp, 0);
Dave Chinnerc8543632010-02-06 12:39:36 +1100555 xfs_reclaim_inodes(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100556 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200557 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000558 if (xfs_log_need_covered(mp))
559 error = xfs_commit_dummy_trans(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100560 }
David Chinnera167b172008-10-30 17:06:18 +1100561 mp->m_sync_seq++;
562 wake_up(&mp->m_wait_single_sync_task);
563}
564
565STATIC int
566xfssyncd(
567 void *arg)
568{
569 struct xfs_mount *mp = arg;
570 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200571 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100572 LIST_HEAD (tmp);
573
574 set_freezable();
575 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
576 for (;;) {
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000577 if (list_empty(&mp->m_sync_list))
578 timeleft = schedule_timeout_interruptible(timeleft);
David Chinnera167b172008-10-30 17:06:18 +1100579 /* swsusp */
580 try_to_freeze();
581 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
582 break;
583
584 spin_lock(&mp->m_sync_lock);
585 /*
586 * We can get woken by laptop mode, to do a sync -
587 * that's the (only!) case where the list would be
588 * empty with time remaining.
589 */
590 if (!timeleft || list_empty(&mp->m_sync_list)) {
591 if (!timeleft)
592 timeleft = xfs_syncd_centisecs *
593 msecs_to_jiffies(10);
594 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
595 list_add_tail(&mp->m_sync_work.w_list,
596 &mp->m_sync_list);
597 }
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000598 list_splice_init(&mp->m_sync_list, &tmp);
David Chinnera167b172008-10-30 17:06:18 +1100599 spin_unlock(&mp->m_sync_lock);
600
601 list_for_each_entry_safe(work, n, &tmp, w_list) {
602 (*work->w_syncer)(mp, work->w_data);
603 list_del(&work->w_list);
604 if (work == &mp->m_sync_work)
605 continue;
Dave Chinnere43afd72009-04-06 18:47:27 +0200606 if (work->w_completion)
607 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100608 kmem_free(work);
609 }
610 }
611
612 return 0;
613}
614
615int
616xfs_syncd_init(
617 struct xfs_mount *mp)
618{
619 mp->m_sync_work.w_syncer = xfs_sync_worker;
620 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200621 mp->m_sync_work.w_completion = NULL;
Jan Engelhardte2a07812010-03-23 09:52:55 +1100622 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
David Chinnera167b172008-10-30 17:06:18 +1100623 if (IS_ERR(mp->m_sync_task))
624 return -PTR_ERR(mp->m_sync_task);
625 return 0;
626}
627
628void
629xfs_syncd_stop(
630 struct xfs_mount *mp)
631{
632 kthread_stop(mp->m_sync_task);
633}
634
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400635void
636__xfs_inode_set_reclaim_tag(
637 struct xfs_perag *pag,
638 struct xfs_inode *ip)
639{
640 radix_tree_tag_set(&pag->pag_ici_root,
641 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
642 XFS_ICI_RECLAIM_TAG);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000643 pag->pag_ici_reclaimable++;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400644}
645
David Chinner11654512008-10-30 17:37:49 +1100646/*
647 * We set the inode flag atomically with the radix tree tag.
648 * Once we get tag lookups on the radix tree, this inode flag
649 * can go away.
650 */
David Chinner396beb82008-10-30 17:37:26 +1100651void
652xfs_inode_set_reclaim_tag(
653 xfs_inode_t *ip)
654{
Dave Chinner5017e972010-01-11 11:47:40 +0000655 struct xfs_mount *mp = ip->i_mount;
656 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100657
Dave Chinner5017e972010-01-11 11:47:40 +0000658 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000659 write_lock(&pag->pag_ici_lock);
David Chinner396beb82008-10-30 17:37:26 +1100660 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400661 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100662 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100663 spin_unlock(&ip->i_flags_lock);
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000664 write_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000665 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100666}
667
668void
669__xfs_inode_clear_reclaim_tag(
670 xfs_mount_t *mp,
671 xfs_perag_t *pag,
672 xfs_inode_t *ip)
673{
674 radix_tree_tag_clear(&pag->pag_ici_root,
675 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000676 pag->pag_ici_reclaimable--;
David Chinner396beb82008-10-30 17:37:26 +1100677}
678
Dave Chinner777df5a2010-02-06 12:37:26 +1100679/*
680 * Inodes in different states need to be treated differently, and the return
681 * value of xfs_iflush is not sufficient to get this right. The following table
682 * lists the inode states and the reclaim actions necessary for non-blocking
683 * reclaim:
684 *
685 *
686 * inode state iflush ret required action
687 * --------------- ---------- ---------------
688 * bad - reclaim
689 * shutdown EIO unpin and reclaim
690 * clean, unpinned 0 reclaim
691 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100692 * clean, pinned(*) 0 requeue
693 * stale, pinned EAGAIN requeue
694 * dirty, delwri ok 0 requeue
695 * dirty, delwri blocked EAGAIN requeue
696 * dirty, sync flush 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100697 *
698 * (*) dgc: I don't think the clean, pinned state is possible but it gets
699 * handled anyway given the order of checks implemented.
700 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100701 * As can be seen from the table, the return value of xfs_iflush() is not
702 * sufficient to correctly decide the reclaim action here. The checks in
703 * xfs_iflush() might look like duplicates, but they are not.
704 *
705 * Also, because we get the flush lock first, we know that any inode that has
706 * been flushed delwri has had the flush completed by the time we check that
707 * the inode is clean. The clean inode check needs to be done before flushing
708 * the inode delwri otherwise we would loop forever requeuing clean inodes as
709 * we cannot tell apart a successful delwri flush and a clean inode from the
710 * return value of xfs_iflush().
711 *
712 * Note that because the inode is flushed delayed write by background
713 * writeback, the flush lock may already be held here and waiting on it can
714 * result in very long latencies. Hence for sync reclaims, where we wait on the
715 * flush lock, the caller should push out delayed write inodes first before
716 * trying to reclaim them to minimise the amount of time spent waiting. For
717 * background relaim, we just requeue the inode for the next pass.
718 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100719 * Hence the order of actions after gaining the locks should be:
720 * bad => reclaim
721 * shutdown => unpin and reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100722 * pinned, delwri => requeue
723 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100724 * stale => reclaim
725 * clean => reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100726 * dirty, delwri => flush and requeue
727 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100728 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200729STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000730xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200731 struct xfs_inode *ip,
732 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000733 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100734{
Dave Chinnerc8543632010-02-06 12:39:36 +1100735 int error = 0;
Dave Chinner777df5a2010-02-06 12:37:26 +1100736
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000737 /*
738 * The radix tree lock here protects a thread in xfs_iget from racing
739 * with us starting reclaim on the inode. Once we have the
740 * XFS_IRECLAIM flag set it will not touch us.
741 */
742 spin_lock(&ip->i_flags_lock);
743 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
744 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
745 /* ignore as it is already under reclaim */
746 spin_unlock(&ip->i_flags_lock);
747 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200748 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100749 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000750 __xfs_iflags_set(ip, XFS_IRECLAIM);
751 spin_unlock(&ip->i_flags_lock);
752 write_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100753
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000754 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100755 if (!xfs_iflock_nowait(ip)) {
756 if (!(sync_mode & SYNC_WAIT))
757 goto out;
758 xfs_iflock(ip);
759 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000760
Dave Chinner777df5a2010-02-06 12:37:26 +1100761 if (is_bad_inode(VFS_I(ip)))
762 goto reclaim;
763 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
764 xfs_iunpin_wait(ip);
765 goto reclaim;
766 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100767 if (xfs_ipincount(ip)) {
768 if (!(sync_mode & SYNC_WAIT)) {
769 xfs_ifunlock(ip);
770 goto out;
771 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100772 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100773 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100774 if (xfs_iflags_test(ip, XFS_ISTALE))
775 goto reclaim;
776 if (xfs_inode_clean(ip))
777 goto reclaim;
778
779 /* Now we have an inode that needs flushing */
780 error = xfs_iflush(ip, sync_mode);
Dave Chinnerc8543632010-02-06 12:39:36 +1100781 if (sync_mode & SYNC_WAIT) {
782 xfs_iflock(ip);
783 goto reclaim;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000784 }
785
Dave Chinnerc8543632010-02-06 12:39:36 +1100786 /*
787 * When we have to flush an inode but don't have SYNC_WAIT set, we
788 * flush the inode out using a delwri buffer and wait for the next
789 * call into reclaim to find it in a clean state instead of waiting for
790 * it now. We also don't return errors here - if the error is transient
791 * then the next reclaim pass will flush the inode, and if the error
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000792 * is permanent then the next sync reclaim will reclaim the inode and
Dave Chinnerc8543632010-02-06 12:39:36 +1100793 * pass on the error.
794 */
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000795 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinnerc8543632010-02-06 12:39:36 +1100796 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
797 "inode 0x%llx background reclaim flush failed with %d",
798 (long long)ip->i_ino, error);
799 }
800out:
801 xfs_iflags_clear(ip, XFS_IRECLAIM);
802 xfs_iunlock(ip, XFS_ILOCK_EXCL);
803 /*
804 * We could return EAGAIN here to make reclaim rescan the inode tree in
805 * a short while. However, this just burns CPU time scanning the tree
806 * waiting for IO to complete and xfssyncd never goes back to the idle
807 * state. Instead, return 0 to let the next scheduled background reclaim
808 * attempt to reclaim the inode again.
809 */
810 return 0;
811
Dave Chinner777df5a2010-02-06 12:37:26 +1100812reclaim:
813 xfs_ifunlock(ip);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000814 xfs_iunlock(ip, XFS_ILOCK_EXCL);
815 xfs_ireclaim(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100816 return error;
817
David Chinner7a3be022008-10-30 17:37:37 +1100818}
819
David Chinnerfce08f22008-10-30 17:37:03 +1100820int
David Chinner1dc33182008-10-30 17:37:15 +1100821xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100822 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100823 int mode)
824{
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000825 return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000826 XFS_ICI_RECLAIM_TAG, 1, NULL);
827}
828
829/*
830 * Shrinker infrastructure.
831 *
832 * This is all far more complex than it needs to be. It adds a global list of
833 * mounts because the shrinkers can only call a global context. We need to make
834 * the shrinkers pass a context to avoid the need for global state.
835 */
836static LIST_HEAD(xfs_mount_list);
837static struct rw_semaphore xfs_mount_list_lock;
838
839static int
840xfs_reclaim_inode_shrink(
841 int nr_to_scan,
842 gfp_t gfp_mask)
843{
844 struct xfs_mount *mp;
845 struct xfs_perag *pag;
846 xfs_agnumber_t ag;
847 int reclaimable = 0;
848
849 if (nr_to_scan) {
850 if (!(gfp_mask & __GFP_FS))
851 return -1;
852
853 down_read(&xfs_mount_list_lock);
854 list_for_each_entry(mp, &xfs_mount_list, m_mplist) {
855 xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0,
856 XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan);
857 if (nr_to_scan <= 0)
858 break;
859 }
860 up_read(&xfs_mount_list_lock);
861 }
862
863 down_read(&xfs_mount_list_lock);
864 list_for_each_entry(mp, &xfs_mount_list, m_mplist) {
865 for (ag = 0; ag < mp->m_sb.sb_agcount; ag++) {
Dave Chinner9bf729c2010-04-29 09:55:50 +1000866 pag = xfs_perag_get(mp, ag);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000867 reclaimable += pag->pag_ici_reclaimable;
868 xfs_perag_put(pag);
869 }
870 }
871 up_read(&xfs_mount_list_lock);
872 return reclaimable;
873}
874
875static struct shrinker xfs_inode_shrinker = {
876 .shrink = xfs_reclaim_inode_shrink,
877 .seeks = DEFAULT_SEEKS,
878};
879
880void __init
881xfs_inode_shrinker_init(void)
882{
883 init_rwsem(&xfs_mount_list_lock);
884 register_shrinker(&xfs_inode_shrinker);
885}
886
887void
888xfs_inode_shrinker_destroy(void)
889{
890 ASSERT(list_empty(&xfs_mount_list));
891 unregister_shrinker(&xfs_inode_shrinker);
892}
893
894void
895xfs_inode_shrinker_register(
896 struct xfs_mount *mp)
897{
898 down_write(&xfs_mount_list_lock);
899 list_add_tail(&mp->m_mplist, &xfs_mount_list);
900 up_write(&xfs_mount_list_lock);
901}
902
903void
904xfs_inode_shrinker_unregister(
905 struct xfs_mount *mp)
906{
907 down_write(&xfs_mount_list_lock);
908 list_del(&mp->m_mplist);
909 up_write(&xfs_mount_list_lock);
David Chinnerfce08f22008-10-30 17:37:03 +1100910}