blob: 37fc2c0a4d255c38ddbc528479518b26f1f610d4 [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"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110027#include "xfs_mount.h"
28#include "xfs_bmap_btree.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110029#include "xfs_inode.h"
30#include "xfs_dinode.h"
31#include "xfs_error.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110032#include "xfs_filestream.h"
33#include "xfs_vnodeops.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110034#include "xfs_inode_item.h"
Christoph Hellwig7d095252009-06-08 15:33:32 +020035#include "xfs_quota.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinner1a387d32010-08-24 11:46:31 +100037#include "xfs_fsops.h"
David Chinnerfe4fa4b2008-10-30 17:06:08 +110038
David Chinnera167b172008-10-30 17:06:18 +110039#include <linux/kthread.h>
40#include <linux/freezer.h>
41
Dave Chinnere13de952010-09-28 12:28:06 +100042STATIC int
43xfs_inode_ag_walk_grab(
44 struct xfs_inode *ip)
45{
46 struct inode *inode = VFS_I(ip);
47
48 /* nothing to sync during shutdown */
49 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
50 return EFSCORRUPTED;
51
52 /* avoid new or reclaimable inodes. Leave for reclaim code to flush */
53 if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM))
54 return ENOENT;
55
56 /* If we can't grab the inode, it must on it's way to reclaim. */
57 if (!igrab(inode))
58 return ENOENT;
59
60 if (is_bad_inode(inode)) {
61 IRELE(ip);
62 return ENOENT;
63 }
64
65 /* inode is valid */
66 return 0;
67}
68
Dave Chinner5a34d5c2009-06-08 15:35:03 +020069
Dave Chinner75f3cb12009-06-08 15:35:14 +020070STATIC int
71xfs_inode_ag_walk(
72 struct xfs_mount *mp,
Dave Chinner5017e972010-01-11 11:47:40 +000073 struct xfs_perag *pag,
Dave Chinner75f3cb12009-06-08 15:35:14 +020074 int (*execute)(struct xfs_inode *ip,
75 struct xfs_perag *pag, int flags),
Dave Chinner65d0f202010-09-24 18:40:15 +100076 int flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +020077{
Dave Chinner75f3cb12009-06-08 15:35:14 +020078 uint32_t first_index;
79 int last_error = 0;
80 int skipped;
Dave Chinner65d0f202010-09-24 18:40:15 +100081 int done;
Dave Chinner75f3cb12009-06-08 15:35:14 +020082
83restart:
Dave Chinner65d0f202010-09-24 18:40:15 +100084 done = 0;
Dave Chinner75f3cb12009-06-08 15:35:14 +020085 skipped = 0;
86 first_index = 0;
87 do {
88 int error = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +100089 int nr_found;
Dave Chinner75f3cb12009-06-08 15:35:14 +020090 xfs_inode_t *ip;
91
Dave Chinner65d0f202010-09-24 18:40:15 +100092 read_lock(&pag->pag_ici_lock);
93 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root,
94 (void **)&ip, first_index, 1);
95 if (!nr_found) {
96 read_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +020097 break;
Dave Chinnerc8e20be2010-01-10 23:51:45 +000098 }
Dave Chinner75f3cb12009-06-08 15:35:14 +020099
Dave Chinner65d0f202010-09-24 18:40:15 +1000100 /*
101 * Update the index for the next lookup. Catch overflows
102 * into the next AG range which can occur if we have inodes
103 * in the last block of the AG and we are currently
104 * pointing to the last inode.
105 */
106 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
107 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
108 done = 1;
109
Dave Chinnere13de952010-09-28 12:28:06 +1000110 if (xfs_inode_ag_walk_grab(ip)) {
111 read_unlock(&pag->pag_ici_lock);
112 continue;
113 }
114 read_unlock(&pag->pag_ici_lock);
115
Dave Chinner75f3cb12009-06-08 15:35:14 +0200116 error = execute(ip, pag, flags);
Dave Chinnere13de952010-09-28 12:28:06 +1000117 IRELE(ip);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200118 if (error == EAGAIN) {
119 skipped++;
120 continue;
121 }
122 if (error)
123 last_error = error;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000124
125 /* bail out if the filesystem is corrupted. */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200126 if (error == EFSCORRUPTED)
127 break;
128
Dave Chinner65d0f202010-09-24 18:40:15 +1000129 } while (!done);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200130
131 if (skipped) {
132 delay(1);
133 goto restart;
134 }
Dave Chinner75f3cb12009-06-08 15:35:14 +0200135 return last_error;
136}
137
Christoph Hellwigfe588ed2009-06-08 15:35:27 +0200138int
Dave Chinner75f3cb12009-06-08 15:35:14 +0200139xfs_inode_ag_iterator(
140 struct xfs_mount *mp,
141 int (*execute)(struct xfs_inode *ip,
142 struct xfs_perag *pag, int flags),
Dave Chinner65d0f202010-09-24 18:40:15 +1000143 int flags)
Dave Chinner75f3cb12009-06-08 15:35:14 +0200144{
Dave Chinner16fd5362010-07-20 09:43:39 +1000145 struct xfs_perag *pag;
Dave Chinner75f3cb12009-06-08 15:35:14 +0200146 int error = 0;
147 int last_error = 0;
148 xfs_agnumber_t ag;
149
Dave Chinner16fd5362010-07-20 09:43:39 +1000150 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000151 while ((pag = xfs_perag_get(mp, ag))) {
152 ag = pag->pag_agno + 1;
153 error = xfs_inode_ag_walk(mp, pag, execute, flags);
Dave Chinner5017e972010-01-11 11:47:40 +0000154 xfs_perag_put(pag);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200155 if (error) {
156 last_error = error;
157 if (error == EFSCORRUPTED)
158 break;
159 }
160 }
161 return XFS_ERROR(last_error);
162}
163
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200164STATIC int
165xfs_sync_inode_data(
166 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200167 struct xfs_perag *pag,
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200168 int flags)
169{
170 struct inode *inode = VFS_I(ip);
171 struct address_space *mapping = inode->i_mapping;
172 int error = 0;
173
174 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
175 goto out_wait;
176
177 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) {
178 if (flags & SYNC_TRYLOCK)
179 goto out_wait;
180 xfs_ilock(ip, XFS_IOLOCK_SHARED);
181 }
182
183 error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ?
Christoph Hellwig0cadda12010-01-19 09:56:44 +0000184 0 : XBF_ASYNC, FI_NONE);
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200185 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
186
187 out_wait:
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200188 if (flags & SYNC_WAIT)
Dave Chinner5a34d5c2009-06-08 15:35:03 +0200189 xfs_ioend_wait(ip);
190 return error;
191}
192
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200193STATIC int
194xfs_sync_inode_attr(
195 struct xfs_inode *ip,
Dave Chinner75f3cb12009-06-08 15:35:14 +0200196 struct xfs_perag *pag,
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200197 int flags)
198{
199 int error = 0;
200
201 xfs_ilock(ip, XFS_ILOCK_SHARED);
202 if (xfs_inode_clean(ip))
203 goto out_unlock;
204 if (!xfs_iflock_nowait(ip)) {
205 if (!(flags & SYNC_WAIT))
206 goto out_unlock;
207 xfs_iflock(ip);
208 }
209
210 if (xfs_inode_clean(ip)) {
211 xfs_ifunlock(ip);
212 goto out_unlock;
213 }
214
Dave Chinnerc8543632010-02-06 12:39:36 +1100215 error = xfs_iflush(ip, flags);
Christoph Hellwig845b6d02009-06-08 15:35:05 +0200216
217 out_unlock:
218 xfs_iunlock(ip, XFS_ILOCK_SHARED);
219 return error;
220}
221
Christoph Hellwig075fe102009-06-08 15:35:48 +0200222/*
223 * Write out pagecache data for the whole filesystem.
224 */
Christoph Hellwig64c86142010-06-24 11:45:34 +1000225STATIC int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200226xfs_sync_data(
227 struct xfs_mount *mp,
228 int flags)
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100229{
Christoph Hellwig075fe102009-06-08 15:35:48 +0200230 int error;
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100231
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200232 ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100233
Dave Chinner65d0f202010-09-24 18:40:15 +1000234 error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200235 if (error)
236 return XFS_ERROR(error);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100237
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000238 xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200239 return 0;
240}
David Chinnere9f1c6e2008-10-30 17:15:50 +1100241
Christoph Hellwig075fe102009-06-08 15:35:48 +0200242/*
243 * Write out inode metadata (attributes) for the whole filesystem.
244 */
Christoph Hellwig64c86142010-06-24 11:45:34 +1000245STATIC int
Christoph Hellwig075fe102009-06-08 15:35:48 +0200246xfs_sync_attr(
247 struct xfs_mount *mp,
248 int flags)
249{
250 ASSERT((flags & ~SYNC_WAIT) == 0);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200251
Dave Chinner65d0f202010-09-24 18:40:15 +1000252 return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags);
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100253}
254
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100255STATIC int
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100256xfs_sync_fsdata(
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000257 struct xfs_mount *mp)
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100258{
259 struct xfs_buf *bp;
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100260
261 /*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000262 * If the buffer is pinned then push on the log so we won't get stuck
263 * waiting in the write for someone, maybe ourselves, to flush the log.
264 *
265 * Even though we just pushed the log above, we did not have the
266 * superblock buffer locked at that point so it can become pinned in
267 * between there and here.
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100268 */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000269 bp = xfs_getsb(mp, 0);
270 if (XFS_BUF_ISPINNED(bp))
271 xfs_log_force(mp, 0);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100272
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000273 return xfs_bwrite(mp, bp);
Christoph Hellwig2af75df2008-10-30 17:14:53 +1100274}
275
David Chinnerfe4fa4b2008-10-30 17:06:08 +1100276/*
David Chinnera4e4c4f2008-10-30 17:16:11 +1100277 * When remounting a filesystem read-only or freezing the filesystem, we have
278 * two phases to execute. This first phase is syncing the data before we
279 * quiesce the filesystem, and the second is flushing all the inodes out after
280 * we've waited for all the transactions created by the first phase to
281 * complete. The second phase ensures that the inodes are written to their
282 * location on disk rather than just existing in transactions in the log. This
283 * means after a quiesce there is no log replay required to write the inodes to
284 * disk (this is the main difference between a sync and a quiesce).
285 */
286/*
287 * First stage of freeze - no writers will make progress now we are here,
David Chinnere9f1c6e2008-10-30 17:15:50 +1100288 * so we flush delwri and delalloc buffers here, then wait for all I/O to
289 * complete. Data is frozen at that point. Metadata is not frozen,
David Chinnera4e4c4f2008-10-30 17:16:11 +1100290 * transactions can still occur here so don't bother flushing the buftarg
291 * because it'll just get dirty again.
David Chinnere9f1c6e2008-10-30 17:15:50 +1100292 */
293int
294xfs_quiesce_data(
295 struct xfs_mount *mp)
296{
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000297 int error, error2 = 0;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100298
299 /* push non-blocking */
Christoph Hellwig075fe102009-06-08 15:35:48 +0200300 xfs_sync_data(mp, 0);
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200301 xfs_qm_sync(mp, SYNC_TRYLOCK);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100302
Dave Chinnerc90b07e2009-10-06 20:29:27 +0000303 /* push and block till complete */
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200304 xfs_sync_data(mp, SYNC_WAIT);
Christoph Hellwig7d095252009-06-08 15:33:32 +0200305 xfs_qm_sync(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100306
David Chinnera4e4c4f2008-10-30 17:16:11 +1100307 /* write superblock and hoover up shutdown errors */
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000308 error = xfs_sync_fsdata(mp);
309
310 /* make sure all delwri buffers are written out */
311 xfs_flush_buftarg(mp->m_ddev_targp, 1);
312
313 /* mark the log as covered if needed */
314 if (xfs_log_need_covered(mp))
Dave Chinner1a387d32010-08-24 11:46:31 +1000315 error2 = xfs_fs_log_dummy(mp, SYNC_WAIT);
David Chinnere9f1c6e2008-10-30 17:15:50 +1100316
David Chinnera4e4c4f2008-10-30 17:16:11 +1100317 /* flush data-only devices */
David Chinnere9f1c6e2008-10-30 17:15:50 +1100318 if (mp->m_rtdev_targp)
319 XFS_bflush(mp->m_rtdev_targp);
320
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000321 return error ? error : error2;
David Chinnere9f1c6e2008-10-30 17:15:50 +1100322}
323
David Chinner76bf1052008-10-30 17:16:21 +1100324STATIC void
325xfs_quiesce_fs(
326 struct xfs_mount *mp)
327{
328 int count = 0, pincount;
329
Dave Chinnerc8543632010-02-06 12:39:36 +1100330 xfs_reclaim_inodes(mp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100331 xfs_flush_buftarg(mp->m_ddev_targp, 0);
David Chinner76bf1052008-10-30 17:16:21 +1100332
333 /*
334 * This loop must run at least twice. The first instance of the loop
335 * will flush most meta data but that will generate more meta data
336 * (typically directory updates). Which then must be flushed and
Dave Chinnerc8543632010-02-06 12:39:36 +1100337 * logged before we can write the unmount record. We also so sync
338 * reclaim of inodes to catch any that the above delwri flush skipped.
David Chinner76bf1052008-10-30 17:16:21 +1100339 */
340 do {
Dave Chinnerc8543632010-02-06 12:39:36 +1100341 xfs_reclaim_inodes(mp, SYNC_WAIT);
Christoph Hellwig075fe102009-06-08 15:35:48 +0200342 xfs_sync_attr(mp, SYNC_WAIT);
David Chinner76bf1052008-10-30 17:16:21 +1100343 pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1);
344 if (!pincount) {
345 delay(50);
346 count++;
347 }
348 } while (count < 2);
349}
350
351/*
352 * Second stage of a quiesce. The data is already synced, now we have to take
353 * care of the metadata. New transactions are already blocked, so we need to
354 * wait for any remaining transactions to drain out before proceding.
355 */
356void
357xfs_quiesce_attr(
358 struct xfs_mount *mp)
359{
360 int error = 0;
361
362 /* wait for all modifications to complete */
363 while (atomic_read(&mp->m_active_trans) > 0)
364 delay(100);
365
366 /* flush inodes and push all remaining buffers out to disk */
367 xfs_quiesce_fs(mp);
368
Felix Blyakher5e106572009-01-22 21:34:05 -0600369 /*
370 * Just warn here till VFS can correctly support
371 * read-only remount without racing.
372 */
373 WARN_ON(atomic_read(&mp->m_active_trans) != 0);
David Chinner76bf1052008-10-30 17:16:21 +1100374
375 /* Push the superblock and write an unmount record */
376 error = xfs_log_sbcount(mp, 1);
377 if (error)
378 xfs_fs_cmn_err(CE_WARN, mp,
379 "xfs_attr_quiesce: failed to log sb changes. "
380 "Frozen image may not be consistent.");
381 xfs_log_unmount_write(mp);
382 xfs_unmountfs_writesb(mp);
383}
384
David Chinnere9f1c6e2008-10-30 17:15:50 +1100385/*
David Chinnera167b172008-10-30 17:06:18 +1100386 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
387 * Doing this has two advantages:
388 * - It saves on stack space, which is tight in certain situations
389 * - It can be used (with care) as a mechanism to avoid deadlocks.
390 * Flushing while allocating in a full filesystem requires both.
391 */
392STATIC void
393xfs_syncd_queue_work(
394 struct xfs_mount *mp,
395 void *data,
Dave Chinnere43afd72009-04-06 18:47:27 +0200396 void (*syncer)(struct xfs_mount *, void *),
397 struct completion *completion)
David Chinnera167b172008-10-30 17:06:18 +1100398{
Dave Chinnera8d770d2009-04-06 18:44:54 +0200399 struct xfs_sync_work *work;
David Chinnera167b172008-10-30 17:06:18 +1100400
Dave Chinnera8d770d2009-04-06 18:44:54 +0200401 work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP);
David Chinnera167b172008-10-30 17:06:18 +1100402 INIT_LIST_HEAD(&work->w_list);
403 work->w_syncer = syncer;
404 work->w_data = data;
405 work->w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200406 work->w_completion = completion;
David Chinnera167b172008-10-30 17:06:18 +1100407 spin_lock(&mp->m_sync_lock);
408 list_add_tail(&work->w_list, &mp->m_sync_list);
409 spin_unlock(&mp->m_sync_lock);
410 wake_up_process(mp->m_sync_task);
411}
412
413/*
414 * Flush delayed allocate data, attempting to free up reserved space
415 * from existing allocations. At this point a new allocation attempt
416 * has failed with ENOSPC and we are in the process of scratching our
417 * heads, looking about for more room...
418 */
419STATIC void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200420xfs_flush_inodes_work(
David Chinnera167b172008-10-30 17:06:18 +1100421 struct xfs_mount *mp,
422 void *arg)
423{
424 struct inode *inode = arg;
Christoph Hellwig075fe102009-06-08 15:35:48 +0200425 xfs_sync_data(mp, SYNC_TRYLOCK);
Christoph Hellwigb0710cc2009-06-08 15:37:11 +0200426 xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT);
David Chinnera167b172008-10-30 17:06:18 +1100427 iput(inode);
428}
429
430void
Dave Chinnera8d770d2009-04-06 18:44:54 +0200431xfs_flush_inodes(
David Chinnera167b172008-10-30 17:06:18 +1100432 xfs_inode_t *ip)
433{
434 struct inode *inode = VFS_I(ip);
Dave Chinnere43afd72009-04-06 18:47:27 +0200435 DECLARE_COMPLETION_ONSTACK(completion);
David Chinnera167b172008-10-30 17:06:18 +1100436
437 igrab(inode);
Dave Chinnere43afd72009-04-06 18:47:27 +0200438 xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion);
439 wait_for_completion(&completion);
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000440 xfs_log_force(ip->i_mount, XFS_LOG_SYNC);
David Chinnera167b172008-10-30 17:06:18 +1100441}
442
David Chinneraacaa882008-10-30 17:15:29 +1100443/*
Christoph Hellwigdf308bc2010-03-12 10:59:16 +0000444 * Every sync period we need to unpin all items, reclaim inodes and sync
445 * disk quotas. We might need to cover the log to indicate that the
Dave Chinner1a387d32010-08-24 11:46:31 +1000446 * filesystem is idle and not frozen.
David Chinneraacaa882008-10-30 17:15:29 +1100447 */
David Chinnera167b172008-10-30 17:06:18 +1100448STATIC void
449xfs_sync_worker(
450 struct xfs_mount *mp,
451 void *unused)
452{
453 int error;
454
David Chinneraacaa882008-10-30 17:15:29 +1100455 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +0000456 xfs_log_force(mp, 0);
Dave Chinnerc8543632010-02-06 12:39:36 +1100457 xfs_reclaim_inodes(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100458 /* dgc: errors ignored here */
Christoph Hellwig8b5403a2009-06-08 15:37:16 +0200459 error = xfs_qm_sync(mp, SYNC_TRYLOCK);
Dave Chinner1a387d32010-08-24 11:46:31 +1000460 if (mp->m_super->s_frozen == SB_UNFROZEN &&
461 xfs_log_need_covered(mp))
462 error = xfs_fs_log_dummy(mp, 0);
David Chinneraacaa882008-10-30 17:15:29 +1100463 }
David Chinnera167b172008-10-30 17:06:18 +1100464 mp->m_sync_seq++;
465 wake_up(&mp->m_wait_single_sync_task);
466}
467
468STATIC int
469xfssyncd(
470 void *arg)
471{
472 struct xfs_mount *mp = arg;
473 long timeleft;
Dave Chinnera8d770d2009-04-06 18:44:54 +0200474 xfs_sync_work_t *work, *n;
David Chinnera167b172008-10-30 17:06:18 +1100475 LIST_HEAD (tmp);
476
477 set_freezable();
478 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
479 for (;;) {
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000480 if (list_empty(&mp->m_sync_list))
481 timeleft = schedule_timeout_interruptible(timeleft);
David Chinnera167b172008-10-30 17:06:18 +1100482 /* swsusp */
483 try_to_freeze();
484 if (kthread_should_stop() && list_empty(&mp->m_sync_list))
485 break;
486
487 spin_lock(&mp->m_sync_lock);
488 /*
489 * We can get woken by laptop mode, to do a sync -
490 * that's the (only!) case where the list would be
491 * empty with time remaining.
492 */
493 if (!timeleft || list_empty(&mp->m_sync_list)) {
494 if (!timeleft)
495 timeleft = xfs_syncd_centisecs *
496 msecs_to_jiffies(10);
497 INIT_LIST_HEAD(&mp->m_sync_work.w_list);
498 list_add_tail(&mp->m_sync_work.w_list,
499 &mp->m_sync_list);
500 }
Dave Chinner20f6b2c2010-03-04 01:46:23 +0000501 list_splice_init(&mp->m_sync_list, &tmp);
David Chinnera167b172008-10-30 17:06:18 +1100502 spin_unlock(&mp->m_sync_lock);
503
504 list_for_each_entry_safe(work, n, &tmp, w_list) {
505 (*work->w_syncer)(mp, work->w_data);
506 list_del(&work->w_list);
507 if (work == &mp->m_sync_work)
508 continue;
Dave Chinnere43afd72009-04-06 18:47:27 +0200509 if (work->w_completion)
510 complete(work->w_completion);
David Chinnera167b172008-10-30 17:06:18 +1100511 kmem_free(work);
512 }
513 }
514
515 return 0;
516}
517
518int
519xfs_syncd_init(
520 struct xfs_mount *mp)
521{
522 mp->m_sync_work.w_syncer = xfs_sync_worker;
523 mp->m_sync_work.w_mount = mp;
Dave Chinnere43afd72009-04-06 18:47:27 +0200524 mp->m_sync_work.w_completion = NULL;
Jan Engelhardte2a07812010-03-23 09:52:55 +1100525 mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname);
David Chinnera167b172008-10-30 17:06:18 +1100526 if (IS_ERR(mp->m_sync_task))
527 return -PTR_ERR(mp->m_sync_task);
528 return 0;
529}
530
531void
532xfs_syncd_stop(
533 struct xfs_mount *mp)
534{
535 kthread_stop(mp->m_sync_task);
536}
537
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400538void
539__xfs_inode_set_reclaim_tag(
540 struct xfs_perag *pag,
541 struct xfs_inode *ip)
542{
543 radix_tree_tag_set(&pag->pag_ici_root,
544 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino),
545 XFS_ICI_RECLAIM_TAG);
Dave Chinner16fd5362010-07-20 09:43:39 +1000546
547 if (!pag->pag_ici_reclaimable) {
548 /* propagate the reclaim tag up into the perag radix tree */
549 spin_lock(&ip->i_mount->m_perag_lock);
550 radix_tree_tag_set(&ip->i_mount->m_perag_tree,
551 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
552 XFS_ICI_RECLAIM_TAG);
553 spin_unlock(&ip->i_mount->m_perag_lock);
554 trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno,
555 -1, _RET_IP_);
556 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000557 pag->pag_ici_reclaimable++;
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400558}
559
David Chinner11654512008-10-30 17:37:49 +1100560/*
561 * We set the inode flag atomically with the radix tree tag.
562 * Once we get tag lookups on the radix tree, this inode flag
563 * can go away.
564 */
David Chinner396beb82008-10-30 17:37:26 +1100565void
566xfs_inode_set_reclaim_tag(
567 xfs_inode_t *ip)
568{
Dave Chinner5017e972010-01-11 11:47:40 +0000569 struct xfs_mount *mp = ip->i_mount;
570 struct xfs_perag *pag;
David Chinner396beb82008-10-30 17:37:26 +1100571
Dave Chinner5017e972010-01-11 11:47:40 +0000572 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000573 write_lock(&pag->pag_ici_lock);
David Chinner396beb82008-10-30 17:37:26 +1100574 spin_lock(&ip->i_flags_lock);
Christoph Hellwigbc990f52009-08-16 20:36:34 -0400575 __xfs_inode_set_reclaim_tag(pag, ip);
David Chinner11654512008-10-30 17:37:49 +1100576 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
David Chinner396beb82008-10-30 17:37:26 +1100577 spin_unlock(&ip->i_flags_lock);
Christoph Hellwigf1f724e2010-03-01 11:30:31 +0000578 write_unlock(&pag->pag_ici_lock);
Dave Chinner5017e972010-01-11 11:47:40 +0000579 xfs_perag_put(pag);
David Chinner396beb82008-10-30 17:37:26 +1100580}
581
Johannes Weiner081003f2010-10-01 07:43:54 +0000582STATIC void
583__xfs_inode_clear_reclaim(
David Chinner396beb82008-10-30 17:37:26 +1100584 xfs_perag_t *pag,
585 xfs_inode_t *ip)
586{
Dave Chinner9bf729c2010-04-29 09:55:50 +1000587 pag->pag_ici_reclaimable--;
Dave Chinner16fd5362010-07-20 09:43:39 +1000588 if (!pag->pag_ici_reclaimable) {
589 /* clear the reclaim tag from the perag radix tree */
590 spin_lock(&ip->i_mount->m_perag_lock);
591 radix_tree_tag_clear(&ip->i_mount->m_perag_tree,
592 XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino),
593 XFS_ICI_RECLAIM_TAG);
594 spin_unlock(&ip->i_mount->m_perag_lock);
595 trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno,
596 -1, _RET_IP_);
597 }
David Chinner396beb82008-10-30 17:37:26 +1100598}
599
Johannes Weiner081003f2010-10-01 07:43:54 +0000600void
601__xfs_inode_clear_reclaim_tag(
602 xfs_mount_t *mp,
603 xfs_perag_t *pag,
604 xfs_inode_t *ip)
605{
606 radix_tree_tag_clear(&pag->pag_ici_root,
607 XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG);
608 __xfs_inode_clear_reclaim(pag, ip);
609}
610
Dave Chinner777df5a2010-02-06 12:37:26 +1100611/*
612 * Inodes in different states need to be treated differently, and the return
613 * value of xfs_iflush is not sufficient to get this right. The following table
614 * lists the inode states and the reclaim actions necessary for non-blocking
615 * reclaim:
616 *
617 *
618 * inode state iflush ret required action
619 * --------------- ---------- ---------------
620 * bad - reclaim
621 * shutdown EIO unpin and reclaim
622 * clean, unpinned 0 reclaim
623 * stale, unpinned 0 reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100624 * clean, pinned(*) 0 requeue
625 * stale, pinned EAGAIN requeue
626 * dirty, delwri ok 0 requeue
627 * dirty, delwri blocked EAGAIN requeue
628 * dirty, sync flush 0 reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100629 *
630 * (*) dgc: I don't think the clean, pinned state is possible but it gets
631 * handled anyway given the order of checks implemented.
632 *
Dave Chinnerc8543632010-02-06 12:39:36 +1100633 * As can be seen from the table, the return value of xfs_iflush() is not
634 * sufficient to correctly decide the reclaim action here. The checks in
635 * xfs_iflush() might look like duplicates, but they are not.
636 *
637 * Also, because we get the flush lock first, we know that any inode that has
638 * been flushed delwri has had the flush completed by the time we check that
639 * the inode is clean. The clean inode check needs to be done before flushing
640 * the inode delwri otherwise we would loop forever requeuing clean inodes as
641 * we cannot tell apart a successful delwri flush and a clean inode from the
642 * return value of xfs_iflush().
643 *
644 * Note that because the inode is flushed delayed write by background
645 * writeback, the flush lock may already be held here and waiting on it can
646 * result in very long latencies. Hence for sync reclaims, where we wait on the
647 * flush lock, the caller should push out delayed write inodes first before
648 * trying to reclaim them to minimise the amount of time spent waiting. For
649 * background relaim, we just requeue the inode for the next pass.
650 *
Dave Chinner777df5a2010-02-06 12:37:26 +1100651 * Hence the order of actions after gaining the locks should be:
652 * bad => reclaim
653 * shutdown => unpin and reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100654 * pinned, delwri => requeue
655 * pinned, sync => unpin
Dave Chinner777df5a2010-02-06 12:37:26 +1100656 * stale => reclaim
657 * clean => reclaim
Dave Chinnerc8543632010-02-06 12:39:36 +1100658 * dirty, delwri => flush and requeue
659 * dirty, sync => flush, wait and reclaim
Dave Chinner777df5a2010-02-06 12:37:26 +1100660 */
Dave Chinner75f3cb12009-06-08 15:35:14 +0200661STATIC int
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000662xfs_reclaim_inode(
Dave Chinner75f3cb12009-06-08 15:35:14 +0200663 struct xfs_inode *ip,
664 struct xfs_perag *pag,
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000665 int sync_mode)
David Chinner7a3be022008-10-30 17:37:37 +1100666{
Dave Chinnerc8543632010-02-06 12:39:36 +1100667 int error = 0;
Dave Chinner777df5a2010-02-06 12:37:26 +1100668
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000669 /*
670 * The radix tree lock here protects a thread in xfs_iget from racing
671 * with us starting reclaim on the inode. Once we have the
672 * XFS_IRECLAIM flag set it will not touch us.
673 */
674 spin_lock(&ip->i_flags_lock);
675 ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE));
676 if (__xfs_iflags_test(ip, XFS_IRECLAIM)) {
677 /* ignore as it is already under reclaim */
678 spin_unlock(&ip->i_flags_lock);
679 write_unlock(&pag->pag_ici_lock);
Dave Chinner75f3cb12009-06-08 15:35:14 +0200680 return 0;
David Chinner7a3be022008-10-30 17:37:37 +1100681 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000682 __xfs_iflags_set(ip, XFS_IRECLAIM);
683 spin_unlock(&ip->i_flags_lock);
684 write_unlock(&pag->pag_ici_lock);
David Chinner7a3be022008-10-30 17:37:37 +1100685
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000686 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc8543632010-02-06 12:39:36 +1100687 if (!xfs_iflock_nowait(ip)) {
688 if (!(sync_mode & SYNC_WAIT))
689 goto out;
690 xfs_iflock(ip);
691 }
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000692
Dave Chinner777df5a2010-02-06 12:37:26 +1100693 if (is_bad_inode(VFS_I(ip)))
694 goto reclaim;
695 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
696 xfs_iunpin_wait(ip);
697 goto reclaim;
698 }
Dave Chinnerc8543632010-02-06 12:39:36 +1100699 if (xfs_ipincount(ip)) {
700 if (!(sync_mode & SYNC_WAIT)) {
701 xfs_ifunlock(ip);
702 goto out;
703 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100704 xfs_iunpin_wait(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100705 }
Dave Chinner777df5a2010-02-06 12:37:26 +1100706 if (xfs_iflags_test(ip, XFS_ISTALE))
707 goto reclaim;
708 if (xfs_inode_clean(ip))
709 goto reclaim;
710
711 /* Now we have an inode that needs flushing */
712 error = xfs_iflush(ip, sync_mode);
Dave Chinnerc8543632010-02-06 12:39:36 +1100713 if (sync_mode & SYNC_WAIT) {
714 xfs_iflock(ip);
715 goto reclaim;
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000716 }
717
Dave Chinnerc8543632010-02-06 12:39:36 +1100718 /*
719 * When we have to flush an inode but don't have SYNC_WAIT set, we
720 * flush the inode out using a delwri buffer and wait for the next
721 * call into reclaim to find it in a clean state instead of waiting for
722 * it now. We also don't return errors here - if the error is transient
723 * then the next reclaim pass will flush the inode, and if the error
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000724 * is permanent then the next sync reclaim will reclaim the inode and
Dave Chinnerc8543632010-02-06 12:39:36 +1100725 * pass on the error.
726 */
Dave Chinnerf1d486a2010-04-13 15:06:45 +1000727 if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinnerc8543632010-02-06 12:39:36 +1100728 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
729 "inode 0x%llx background reclaim flush failed with %d",
730 (long long)ip->i_ino, error);
731 }
732out:
733 xfs_iflags_clear(ip, XFS_IRECLAIM);
734 xfs_iunlock(ip, XFS_ILOCK_EXCL);
735 /*
736 * We could return EAGAIN here to make reclaim rescan the inode tree in
737 * a short while. However, this just burns CPU time scanning the tree
738 * waiting for IO to complete and xfssyncd never goes back to the idle
739 * state. Instead, return 0 to let the next scheduled background reclaim
740 * attempt to reclaim the inode again.
741 */
742 return 0;
743
Dave Chinner777df5a2010-02-06 12:37:26 +1100744reclaim:
745 xfs_ifunlock(ip);
Dave Chinnerc8e20be2010-01-10 23:51:45 +0000746 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000747
748 XFS_STATS_INC(xs_ig_reclaims);
749 /*
750 * Remove the inode from the per-AG radix tree.
751 *
752 * Because radix_tree_delete won't complain even if the item was never
753 * added to the tree assert that it's been there before to catch
754 * problems with the inode life time early on.
755 */
756 write_lock(&pag->pag_ici_lock);
757 if (!radix_tree_delete(&pag->pag_ici_root,
758 XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino)))
759 ASSERT(0);
Johannes Weiner081003f2010-10-01 07:43:54 +0000760 __xfs_inode_clear_reclaim(pag, ip);
Dave Chinner2f11fea2010-07-20 17:53:25 +1000761 write_unlock(&pag->pag_ici_lock);
762
763 /*
764 * Here we do an (almost) spurious inode lock in order to coordinate
765 * with inode cache radix tree lookups. This is because the lookup
766 * can reference the inodes in the cache without taking references.
767 *
768 * We make that OK here by ensuring that we wait until the inode is
769 * unlocked after the lookup before we go ahead and free it. We get
770 * both the ilock and the iolock because the code may need to drop the
771 * ilock one but will still hold the iolock.
772 */
773 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
774 xfs_qm_dqdetach(ip);
775 xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
776
777 xfs_inode_free(ip);
Dave Chinnerc8543632010-02-06 12:39:36 +1100778 return error;
779
David Chinner7a3be022008-10-30 17:37:37 +1100780}
781
Dave Chinner65d0f202010-09-24 18:40:15 +1000782/*
783 * Walk the AGs and reclaim the inodes in them. Even if the filesystem is
784 * corrupted, we still want to try to reclaim all the inodes. If we don't,
785 * then a shut down during filesystem unmount reclaim walk leak all the
786 * unreclaimed inodes.
787 */
788int
789xfs_reclaim_inodes_ag(
790 struct xfs_mount *mp,
791 int flags,
792 int *nr_to_scan)
793{
794 struct xfs_perag *pag;
795 int error = 0;
796 int last_error = 0;
797 xfs_agnumber_t ag;
798
799 ag = 0;
800 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
801 unsigned long first_index = 0;
802 int done = 0;
803
804 ag = pag->pag_agno + 1;
805
806 do {
807 struct xfs_inode *ip;
808 int nr_found;
809
810 write_lock(&pag->pag_ici_lock);
811 nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root,
812 (void **)&ip, first_index, 1,
813 XFS_ICI_RECLAIM_TAG);
814 if (!nr_found) {
815 write_unlock(&pag->pag_ici_lock);
816 break;
817 }
818
819 /*
820 * Update the index for the next lookup. Catch overflows
821 * into the next AG range which can occur if we have inodes
822 * in the last block of the AG and we are currently
823 * pointing to the last inode.
824 */
825 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1);
826 if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino))
827 done = 1;
828
829 error = xfs_reclaim_inode(ip, pag, flags);
830 if (error && last_error != EFSCORRUPTED)
831 last_error = error;
832
833 } while (!done && (*nr_to_scan)--);
834
835 xfs_perag_put(pag);
836 }
837 return XFS_ERROR(last_error);
838}
839
David Chinnerfce08f22008-10-30 17:37:03 +1100840int
David Chinner1dc33182008-10-30 17:37:15 +1100841xfs_reclaim_inodes(
David Chinnerfce08f22008-10-30 17:37:03 +1100842 xfs_mount_t *mp,
David Chinnerfce08f22008-10-30 17:37:03 +1100843 int mode)
844{
Dave Chinner65d0f202010-09-24 18:40:15 +1000845 int nr_to_scan = INT_MAX;
846
847 return xfs_reclaim_inodes_ag(mp, mode, &nr_to_scan);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000848}
849
850/*
851 * Shrinker infrastructure.
Dave Chinner9bf729c2010-04-29 09:55:50 +1000852 */
Dave Chinner9bf729c2010-04-29 09:55:50 +1000853static int
854xfs_reclaim_inode_shrink(
Dave Chinner7f8275d2010-07-19 14:56:17 +1000855 struct shrinker *shrink,
Dave Chinner9bf729c2010-04-29 09:55:50 +1000856 int nr_to_scan,
857 gfp_t gfp_mask)
858{
859 struct xfs_mount *mp;
860 struct xfs_perag *pag;
861 xfs_agnumber_t ag;
Dave Chinner16fd5362010-07-20 09:43:39 +1000862 int reclaimable;
Dave Chinner9bf729c2010-04-29 09:55:50 +1000863
Dave Chinner70e60ce2010-07-20 08:07:02 +1000864 mp = container_of(shrink, struct xfs_mount, m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000865 if (nr_to_scan) {
866 if (!(gfp_mask & __GFP_FS))
867 return -1;
868
Dave Chinner65d0f202010-09-24 18:40:15 +1000869 xfs_reclaim_inodes_ag(mp, 0, &nr_to_scan);
870 /* terminate if we don't exhaust the scan */
Dave Chinner70e60ce2010-07-20 08:07:02 +1000871 if (nr_to_scan > 0)
872 return -1;
873 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000874
Dave Chinner16fd5362010-07-20 09:43:39 +1000875 reclaimable = 0;
876 ag = 0;
Dave Chinner65d0f202010-09-24 18:40:15 +1000877 while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) {
878 ag = pag->pag_agno + 1;
Dave Chinner70e60ce2010-07-20 08:07:02 +1000879 reclaimable += pag->pag_ici_reclaimable;
880 xfs_perag_put(pag);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000881 }
Dave Chinner9bf729c2010-04-29 09:55:50 +1000882 return reclaimable;
883}
884
Dave Chinner9bf729c2010-04-29 09:55:50 +1000885void
886xfs_inode_shrinker_register(
887 struct xfs_mount *mp)
888{
Dave Chinner70e60ce2010-07-20 08:07:02 +1000889 mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink;
890 mp->m_inode_shrink.seeks = DEFAULT_SEEKS;
891 register_shrinker(&mp->m_inode_shrink);
Dave Chinner9bf729c2010-04-29 09:55:50 +1000892}
893
894void
895xfs_inode_shrinker_unregister(
896 struct xfs_mount *mp)
897{
Dave Chinner70e60ce2010-07-20 08:07:02 +1000898 unregister_shrinker(&mp->m_inode_shrink);
David Chinnerfce08f22008-10-30 17:37:03 +1100899}