]>
Commit | Line | Data |
---|---|---|
fe4fa4b8 DC |
1 | /* |
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" | |
fe4fa4b8 DC |
27 | #include "xfs_mount.h" |
28 | #include "xfs_bmap_btree.h" | |
fe4fa4b8 DC |
29 | #include "xfs_inode.h" |
30 | #include "xfs_dinode.h" | |
31 | #include "xfs_error.h" | |
fe4fa4b8 DC |
32 | #include "xfs_filestream.h" |
33 | #include "xfs_vnodeops.h" | |
fe4fa4b8 | 34 | #include "xfs_inode_item.h" |
7d095257 | 35 | #include "xfs_quota.h" |
0b1b213f | 36 | #include "xfs_trace.h" |
1a387d3b | 37 | #include "xfs_fsops.h" |
fe4fa4b8 | 38 | |
a167b17e DC |
39 | #include <linux/kthread.h> |
40 | #include <linux/freezer.h> | |
41 | ||
5a34d5cd | 42 | |
75f3cb13 DC |
43 | STATIC xfs_inode_t * |
44 | xfs_inode_ag_lookup( | |
45 | struct xfs_mount *mp, | |
46 | struct xfs_perag *pag, | |
47 | uint32_t *first_index, | |
48 | int tag) | |
49 | { | |
50 | int nr_found; | |
51 | struct xfs_inode *ip; | |
52 | ||
53 | /* | |
54 | * use a gang lookup to find the next inode in the tree | |
55 | * as the tree is sparse and a gang lookup walks to find | |
56 | * the number of objects requested. | |
57 | */ | |
75f3cb13 DC |
58 | if (tag == XFS_ICI_NO_TAG) { |
59 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, | |
60 | (void **)&ip, *first_index, 1); | |
61 | } else { | |
62 | nr_found = radix_tree_gang_lookup_tag(&pag->pag_ici_root, | |
63 | (void **)&ip, *first_index, 1, tag); | |
64 | } | |
65 | if (!nr_found) | |
c8e20be0 | 66 | return NULL; |
75f3cb13 DC |
67 | |
68 | /* | |
69 | * Update the index for the next lookup. Catch overflows | |
70 | * into the next AG range which can occur if we have inodes | |
71 | * in the last block of the AG and we are currently | |
72 | * pointing to the last inode. | |
73 | */ | |
74 | *first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); | |
75 | if (*first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) | |
c8e20be0 | 76 | return NULL; |
75f3cb13 | 77 | return ip; |
75f3cb13 DC |
78 | } |
79 | ||
80 | STATIC int | |
81 | xfs_inode_ag_walk( | |
82 | struct xfs_mount *mp, | |
5017e97d | 83 | struct xfs_perag *pag, |
75f3cb13 DC |
84 | int (*execute)(struct xfs_inode *ip, |
85 | struct xfs_perag *pag, int flags), | |
86 | int flags, | |
c8e20be0 | 87 | int tag, |
9bf729c0 DC |
88 | int exclusive, |
89 | int *nr_to_scan) | |
75f3cb13 | 90 | { |
75f3cb13 DC |
91 | uint32_t first_index; |
92 | int last_error = 0; | |
93 | int skipped; | |
94 | ||
95 | restart: | |
96 | skipped = 0; | |
97 | first_index = 0; | |
98 | do { | |
99 | int error = 0; | |
100 | xfs_inode_t *ip; | |
101 | ||
c8e20be0 DC |
102 | if (exclusive) |
103 | write_lock(&pag->pag_ici_lock); | |
104 | else | |
105 | read_lock(&pag->pag_ici_lock); | |
75f3cb13 | 106 | ip = xfs_inode_ag_lookup(mp, pag, &first_index, tag); |
c8e20be0 DC |
107 | if (!ip) { |
108 | if (exclusive) | |
109 | write_unlock(&pag->pag_ici_lock); | |
110 | else | |
111 | read_unlock(&pag->pag_ici_lock); | |
75f3cb13 | 112 | break; |
c8e20be0 | 113 | } |
75f3cb13 | 114 | |
c8e20be0 | 115 | /* execute releases pag->pag_ici_lock */ |
75f3cb13 DC |
116 | error = execute(ip, pag, flags); |
117 | if (error == EAGAIN) { | |
118 | skipped++; | |
119 | continue; | |
120 | } | |
121 | if (error) | |
122 | last_error = error; | |
c8e20be0 DC |
123 | |
124 | /* bail out if the filesystem is corrupted. */ | |
75f3cb13 DC |
125 | if (error == EFSCORRUPTED) |
126 | break; | |
127 | ||
9bf729c0 | 128 | } while ((*nr_to_scan)--); |
75f3cb13 DC |
129 | |
130 | if (skipped) { | |
131 | delay(1); | |
132 | goto restart; | |
133 | } | |
75f3cb13 DC |
134 | return last_error; |
135 | } | |
136 | ||
16fd5367 DC |
137 | /* |
138 | * Select the next per-ag structure to iterate during the walk. The reclaim | |
139 | * walk is optimised only to walk AGs with reclaimable inodes in them. | |
140 | */ | |
141 | static struct xfs_perag * | |
142 | xfs_inode_ag_iter_next_pag( | |
143 | struct xfs_mount *mp, | |
144 | xfs_agnumber_t *first, | |
145 | int tag) | |
146 | { | |
147 | struct xfs_perag *pag = NULL; | |
148 | ||
149 | if (tag == XFS_ICI_RECLAIM_TAG) { | |
150 | int found; | |
151 | int ref; | |
152 | ||
153 | spin_lock(&mp->m_perag_lock); | |
154 | found = radix_tree_gang_lookup_tag(&mp->m_perag_tree, | |
155 | (void **)&pag, *first, 1, tag); | |
156 | if (found <= 0) { | |
157 | spin_unlock(&mp->m_perag_lock); | |
158 | return NULL; | |
159 | } | |
160 | *first = pag->pag_agno + 1; | |
161 | /* open coded pag reference increment */ | |
162 | ref = atomic_inc_return(&pag->pag_ref); | |
163 | spin_unlock(&mp->m_perag_lock); | |
164 | trace_xfs_perag_get_reclaim(mp, pag->pag_agno, ref, _RET_IP_); | |
165 | } else { | |
166 | pag = xfs_perag_get(mp, *first); | |
167 | (*first)++; | |
168 | } | |
169 | return pag; | |
170 | } | |
171 | ||
fe588ed3 | 172 | int |
75f3cb13 DC |
173 | xfs_inode_ag_iterator( |
174 | struct xfs_mount *mp, | |
175 | int (*execute)(struct xfs_inode *ip, | |
176 | struct xfs_perag *pag, int flags), | |
177 | int flags, | |
c8e20be0 | 178 | int tag, |
9bf729c0 DC |
179 | int exclusive, |
180 | int *nr_to_scan) | |
75f3cb13 | 181 | { |
16fd5367 | 182 | struct xfs_perag *pag; |
75f3cb13 DC |
183 | int error = 0; |
184 | int last_error = 0; | |
185 | xfs_agnumber_t ag; | |
9bf729c0 | 186 | int nr; |
75f3cb13 | 187 | |
9bf729c0 | 188 | nr = nr_to_scan ? *nr_to_scan : INT_MAX; |
16fd5367 DC |
189 | ag = 0; |
190 | while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, tag))) { | |
5017e97d | 191 | error = xfs_inode_ag_walk(mp, pag, execute, flags, tag, |
9bf729c0 | 192 | exclusive, &nr); |
5017e97d | 193 | xfs_perag_put(pag); |
75f3cb13 DC |
194 | if (error) { |
195 | last_error = error; | |
196 | if (error == EFSCORRUPTED) | |
197 | break; | |
198 | } | |
9bf729c0 DC |
199 | if (nr <= 0) |
200 | break; | |
75f3cb13 | 201 | } |
9bf729c0 DC |
202 | if (nr_to_scan) |
203 | *nr_to_scan = nr; | |
75f3cb13 DC |
204 | return XFS_ERROR(last_error); |
205 | } | |
206 | ||
1da8eeca | 207 | /* must be called with pag_ici_lock held and releases it */ |
fe588ed3 | 208 | int |
1da8eeca DC |
209 | xfs_sync_inode_valid( |
210 | struct xfs_inode *ip, | |
211 | struct xfs_perag *pag) | |
212 | { | |
213 | struct inode *inode = VFS_I(ip); | |
018027be | 214 | int error = EFSCORRUPTED; |
1da8eeca DC |
215 | |
216 | /* nothing to sync during shutdown */ | |
018027be DC |
217 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
218 | goto out_unlock; | |
1da8eeca | 219 | |
018027be DC |
220 | /* avoid new or reclaimable inodes. Leave for reclaim code to flush */ |
221 | error = ENOENT; | |
222 | if (xfs_iflags_test(ip, XFS_INEW | XFS_IRECLAIMABLE | XFS_IRECLAIM)) | |
223 | goto out_unlock; | |
1da8eeca | 224 | |
018027be DC |
225 | /* If we can't grab the inode, it must on it's way to reclaim. */ |
226 | if (!igrab(inode)) | |
227 | goto out_unlock; | |
228 | ||
229 | if (is_bad_inode(inode)) { | |
1da8eeca | 230 | IRELE(ip); |
018027be | 231 | goto out_unlock; |
1da8eeca DC |
232 | } |
233 | ||
018027be DC |
234 | /* inode is valid */ |
235 | error = 0; | |
236 | out_unlock: | |
237 | read_unlock(&pag->pag_ici_lock); | |
238 | return error; | |
1da8eeca DC |
239 | } |
240 | ||
5a34d5cd DC |
241 | STATIC int |
242 | xfs_sync_inode_data( | |
243 | struct xfs_inode *ip, | |
75f3cb13 | 244 | struct xfs_perag *pag, |
5a34d5cd DC |
245 | int flags) |
246 | { | |
247 | struct inode *inode = VFS_I(ip); | |
248 | struct address_space *mapping = inode->i_mapping; | |
249 | int error = 0; | |
250 | ||
75f3cb13 DC |
251 | error = xfs_sync_inode_valid(ip, pag); |
252 | if (error) | |
253 | return error; | |
254 | ||
5a34d5cd DC |
255 | if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
256 | goto out_wait; | |
257 | ||
258 | if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED)) { | |
259 | if (flags & SYNC_TRYLOCK) | |
260 | goto out_wait; | |
261 | xfs_ilock(ip, XFS_IOLOCK_SHARED); | |
262 | } | |
263 | ||
264 | error = xfs_flush_pages(ip, 0, -1, (flags & SYNC_WAIT) ? | |
0cadda1c | 265 | 0 : XBF_ASYNC, FI_NONE); |
5a34d5cd DC |
266 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
267 | ||
268 | out_wait: | |
b0710ccc | 269 | if (flags & SYNC_WAIT) |
5a34d5cd | 270 | xfs_ioend_wait(ip); |
75f3cb13 | 271 | IRELE(ip); |
5a34d5cd DC |
272 | return error; |
273 | } | |
274 | ||
845b6d0c CH |
275 | STATIC int |
276 | xfs_sync_inode_attr( | |
277 | struct xfs_inode *ip, | |
75f3cb13 | 278 | struct xfs_perag *pag, |
845b6d0c CH |
279 | int flags) |
280 | { | |
281 | int error = 0; | |
282 | ||
75f3cb13 DC |
283 | error = xfs_sync_inode_valid(ip, pag); |
284 | if (error) | |
285 | return error; | |
286 | ||
845b6d0c CH |
287 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
288 | if (xfs_inode_clean(ip)) | |
289 | goto out_unlock; | |
290 | if (!xfs_iflock_nowait(ip)) { | |
291 | if (!(flags & SYNC_WAIT)) | |
292 | goto out_unlock; | |
293 | xfs_iflock(ip); | |
294 | } | |
295 | ||
296 | if (xfs_inode_clean(ip)) { | |
297 | xfs_ifunlock(ip); | |
298 | goto out_unlock; | |
299 | } | |
300 | ||
c854363e | 301 | error = xfs_iflush(ip, flags); |
845b6d0c CH |
302 | |
303 | out_unlock: | |
304 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | |
75f3cb13 | 305 | IRELE(ip); |
845b6d0c CH |
306 | return error; |
307 | } | |
308 | ||
075fe102 CH |
309 | /* |
310 | * Write out pagecache data for the whole filesystem. | |
311 | */ | |
64c86149 | 312 | STATIC int |
075fe102 CH |
313 | xfs_sync_data( |
314 | struct xfs_mount *mp, | |
315 | int flags) | |
683a8970 | 316 | { |
075fe102 | 317 | int error; |
fe4fa4b8 | 318 | |
b0710ccc | 319 | ASSERT((flags & ~(SYNC_TRYLOCK|SYNC_WAIT)) == 0); |
fe4fa4b8 | 320 | |
075fe102 | 321 | error = xfs_inode_ag_iterator(mp, xfs_sync_inode_data, flags, |
9bf729c0 | 322 | XFS_ICI_NO_TAG, 0, NULL); |
075fe102 CH |
323 | if (error) |
324 | return XFS_ERROR(error); | |
e9f1c6ee | 325 | |
a14a348b | 326 | xfs_log_force(mp, (flags & SYNC_WAIT) ? XFS_LOG_SYNC : 0); |
075fe102 CH |
327 | return 0; |
328 | } | |
e9f1c6ee | 329 | |
075fe102 CH |
330 | /* |
331 | * Write out inode metadata (attributes) for the whole filesystem. | |
332 | */ | |
64c86149 | 333 | STATIC int |
075fe102 CH |
334 | xfs_sync_attr( |
335 | struct xfs_mount *mp, | |
336 | int flags) | |
337 | { | |
338 | ASSERT((flags & ~SYNC_WAIT) == 0); | |
75f3cb13 | 339 | |
075fe102 | 340 | return xfs_inode_ag_iterator(mp, xfs_sync_inode_attr, flags, |
9bf729c0 | 341 | XFS_ICI_NO_TAG, 0, NULL); |
fe4fa4b8 DC |
342 | } |
343 | ||
5d77c0dc | 344 | STATIC int |
2af75df7 | 345 | xfs_sync_fsdata( |
df308bcf | 346 | struct xfs_mount *mp) |
2af75df7 CH |
347 | { |
348 | struct xfs_buf *bp; | |
2af75df7 CH |
349 | |
350 | /* | |
df308bcf CH |
351 | * If the buffer is pinned then push on the log so we won't get stuck |
352 | * waiting in the write for someone, maybe ourselves, to flush the log. | |
353 | * | |
354 | * Even though we just pushed the log above, we did not have the | |
355 | * superblock buffer locked at that point so it can become pinned in | |
356 | * between there and here. | |
2af75df7 | 357 | */ |
df308bcf CH |
358 | bp = xfs_getsb(mp, 0); |
359 | if (XFS_BUF_ISPINNED(bp)) | |
360 | xfs_log_force(mp, 0); | |
2af75df7 | 361 | |
df308bcf | 362 | return xfs_bwrite(mp, bp); |
e9f1c6ee DC |
363 | } |
364 | ||
365 | /* | |
a4e4c4f4 DC |
366 | * When remounting a filesystem read-only or freezing the filesystem, we have |
367 | * two phases to execute. This first phase is syncing the data before we | |
368 | * quiesce the filesystem, and the second is flushing all the inodes out after | |
369 | * we've waited for all the transactions created by the first phase to | |
370 | * complete. The second phase ensures that the inodes are written to their | |
371 | * location on disk rather than just existing in transactions in the log. This | |
372 | * means after a quiesce there is no log replay required to write the inodes to | |
373 | * disk (this is the main difference between a sync and a quiesce). | |
374 | */ | |
375 | /* | |
376 | * First stage of freeze - no writers will make progress now we are here, | |
e9f1c6ee DC |
377 | * so we flush delwri and delalloc buffers here, then wait for all I/O to |
378 | * complete. Data is frozen at that point. Metadata is not frozen, | |
a4e4c4f4 DC |
379 | * transactions can still occur here so don't bother flushing the buftarg |
380 | * because it'll just get dirty again. | |
e9f1c6ee DC |
381 | */ |
382 | int | |
383 | xfs_quiesce_data( | |
384 | struct xfs_mount *mp) | |
385 | { | |
df308bcf | 386 | int error, error2 = 0; |
e9f1c6ee DC |
387 | |
388 | /* push non-blocking */ | |
075fe102 | 389 | xfs_sync_data(mp, 0); |
8b5403a6 | 390 | xfs_qm_sync(mp, SYNC_TRYLOCK); |
e9f1c6ee | 391 | |
c90b07e8 | 392 | /* push and block till complete */ |
b0710ccc | 393 | xfs_sync_data(mp, SYNC_WAIT); |
7d095257 | 394 | xfs_qm_sync(mp, SYNC_WAIT); |
e9f1c6ee | 395 | |
a4e4c4f4 | 396 | /* write superblock and hoover up shutdown errors */ |
df308bcf CH |
397 | error = xfs_sync_fsdata(mp); |
398 | ||
399 | /* make sure all delwri buffers are written out */ | |
400 | xfs_flush_buftarg(mp->m_ddev_targp, 1); | |
401 | ||
402 | /* mark the log as covered if needed */ | |
403 | if (xfs_log_need_covered(mp)) | |
1a387d3b | 404 | error2 = xfs_fs_log_dummy(mp, SYNC_WAIT); |
e9f1c6ee | 405 | |
a4e4c4f4 | 406 | /* flush data-only devices */ |
e9f1c6ee DC |
407 | if (mp->m_rtdev_targp) |
408 | XFS_bflush(mp->m_rtdev_targp); | |
409 | ||
df308bcf | 410 | return error ? error : error2; |
2af75df7 CH |
411 | } |
412 | ||
76bf105c DC |
413 | STATIC void |
414 | xfs_quiesce_fs( | |
415 | struct xfs_mount *mp) | |
416 | { | |
417 | int count = 0, pincount; | |
418 | ||
c854363e | 419 | xfs_reclaim_inodes(mp, 0); |
76bf105c | 420 | xfs_flush_buftarg(mp->m_ddev_targp, 0); |
76bf105c DC |
421 | |
422 | /* | |
423 | * This loop must run at least twice. The first instance of the loop | |
424 | * will flush most meta data but that will generate more meta data | |
425 | * (typically directory updates). Which then must be flushed and | |
c854363e DC |
426 | * logged before we can write the unmount record. We also so sync |
427 | * reclaim of inodes to catch any that the above delwri flush skipped. | |
76bf105c DC |
428 | */ |
429 | do { | |
c854363e | 430 | xfs_reclaim_inodes(mp, SYNC_WAIT); |
075fe102 | 431 | xfs_sync_attr(mp, SYNC_WAIT); |
76bf105c DC |
432 | pincount = xfs_flush_buftarg(mp->m_ddev_targp, 1); |
433 | if (!pincount) { | |
434 | delay(50); | |
435 | count++; | |
436 | } | |
437 | } while (count < 2); | |
438 | } | |
439 | ||
440 | /* | |
441 | * Second stage of a quiesce. The data is already synced, now we have to take | |
442 | * care of the metadata. New transactions are already blocked, so we need to | |
443 | * wait for any remaining transactions to drain out before proceding. | |
444 | */ | |
445 | void | |
446 | xfs_quiesce_attr( | |
447 | struct xfs_mount *mp) | |
448 | { | |
449 | int error = 0; | |
450 | ||
451 | /* wait for all modifications to complete */ | |
452 | while (atomic_read(&mp->m_active_trans) > 0) | |
453 | delay(100); | |
454 | ||
455 | /* flush inodes and push all remaining buffers out to disk */ | |
456 | xfs_quiesce_fs(mp); | |
457 | ||
5e106572 FB |
458 | /* |
459 | * Just warn here till VFS can correctly support | |
460 | * read-only remount without racing. | |
461 | */ | |
462 | WARN_ON(atomic_read(&mp->m_active_trans) != 0); | |
76bf105c DC |
463 | |
464 | /* Push the superblock and write an unmount record */ | |
465 | error = xfs_log_sbcount(mp, 1); | |
466 | if (error) | |
467 | xfs_fs_cmn_err(CE_WARN, mp, | |
468 | "xfs_attr_quiesce: failed to log sb changes. " | |
469 | "Frozen image may not be consistent."); | |
470 | xfs_log_unmount_write(mp); | |
471 | xfs_unmountfs_writesb(mp); | |
472 | } | |
473 | ||
a167b17e DC |
474 | /* |
475 | * Enqueue a work item to be picked up by the vfs xfssyncd thread. | |
476 | * Doing this has two advantages: | |
477 | * - It saves on stack space, which is tight in certain situations | |
478 | * - It can be used (with care) as a mechanism to avoid deadlocks. | |
479 | * Flushing while allocating in a full filesystem requires both. | |
480 | */ | |
481 | STATIC void | |
482 | xfs_syncd_queue_work( | |
483 | struct xfs_mount *mp, | |
484 | void *data, | |
e43afd72 DC |
485 | void (*syncer)(struct xfs_mount *, void *), |
486 | struct completion *completion) | |
a167b17e | 487 | { |
a8d770d9 | 488 | struct xfs_sync_work *work; |
a167b17e | 489 | |
a8d770d9 | 490 | work = kmem_alloc(sizeof(struct xfs_sync_work), KM_SLEEP); |
a167b17e DC |
491 | INIT_LIST_HEAD(&work->w_list); |
492 | work->w_syncer = syncer; | |
493 | work->w_data = data; | |
494 | work->w_mount = mp; | |
e43afd72 | 495 | work->w_completion = completion; |
a167b17e DC |
496 | spin_lock(&mp->m_sync_lock); |
497 | list_add_tail(&work->w_list, &mp->m_sync_list); | |
498 | spin_unlock(&mp->m_sync_lock); | |
499 | wake_up_process(mp->m_sync_task); | |
500 | } | |
501 | ||
502 | /* | |
503 | * Flush delayed allocate data, attempting to free up reserved space | |
504 | * from existing allocations. At this point a new allocation attempt | |
505 | * has failed with ENOSPC and we are in the process of scratching our | |
506 | * heads, looking about for more room... | |
507 | */ | |
508 | STATIC void | |
a8d770d9 | 509 | xfs_flush_inodes_work( |
a167b17e DC |
510 | struct xfs_mount *mp, |
511 | void *arg) | |
512 | { | |
513 | struct inode *inode = arg; | |
075fe102 | 514 | xfs_sync_data(mp, SYNC_TRYLOCK); |
b0710ccc | 515 | xfs_sync_data(mp, SYNC_TRYLOCK | SYNC_WAIT); |
a167b17e DC |
516 | iput(inode); |
517 | } | |
518 | ||
519 | void | |
a8d770d9 | 520 | xfs_flush_inodes( |
a167b17e DC |
521 | xfs_inode_t *ip) |
522 | { | |
523 | struct inode *inode = VFS_I(ip); | |
e43afd72 | 524 | DECLARE_COMPLETION_ONSTACK(completion); |
a167b17e DC |
525 | |
526 | igrab(inode); | |
e43afd72 DC |
527 | xfs_syncd_queue_work(ip->i_mount, inode, xfs_flush_inodes_work, &completion); |
528 | wait_for_completion(&completion); | |
a14a348b | 529 | xfs_log_force(ip->i_mount, XFS_LOG_SYNC); |
a167b17e DC |
530 | } |
531 | ||
aacaa880 | 532 | /* |
df308bcf CH |
533 | * Every sync period we need to unpin all items, reclaim inodes and sync |
534 | * disk quotas. We might need to cover the log to indicate that the | |
1a387d3b | 535 | * filesystem is idle and not frozen. |
aacaa880 | 536 | */ |
a167b17e DC |
537 | STATIC void |
538 | xfs_sync_worker( | |
539 | struct xfs_mount *mp, | |
540 | void *unused) | |
541 | { | |
542 | int error; | |
543 | ||
aacaa880 | 544 | if (!(mp->m_flags & XFS_MOUNT_RDONLY)) { |
a14a348b | 545 | xfs_log_force(mp, 0); |
c854363e | 546 | xfs_reclaim_inodes(mp, 0); |
aacaa880 | 547 | /* dgc: errors ignored here */ |
8b5403a6 | 548 | error = xfs_qm_sync(mp, SYNC_TRYLOCK); |
1a387d3b DC |
549 | if (mp->m_super->s_frozen == SB_UNFROZEN && |
550 | xfs_log_need_covered(mp)) | |
551 | error = xfs_fs_log_dummy(mp, 0); | |
aacaa880 | 552 | } |
a167b17e DC |
553 | mp->m_sync_seq++; |
554 | wake_up(&mp->m_wait_single_sync_task); | |
555 | } | |
556 | ||
557 | STATIC int | |
558 | xfssyncd( | |
559 | void *arg) | |
560 | { | |
561 | struct xfs_mount *mp = arg; | |
562 | long timeleft; | |
a8d770d9 | 563 | xfs_sync_work_t *work, *n; |
a167b17e DC |
564 | LIST_HEAD (tmp); |
565 | ||
566 | set_freezable(); | |
567 | timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10); | |
568 | for (;;) { | |
20f6b2c7 DC |
569 | if (list_empty(&mp->m_sync_list)) |
570 | timeleft = schedule_timeout_interruptible(timeleft); | |
a167b17e DC |
571 | /* swsusp */ |
572 | try_to_freeze(); | |
573 | if (kthread_should_stop() && list_empty(&mp->m_sync_list)) | |
574 | break; | |
575 | ||
576 | spin_lock(&mp->m_sync_lock); | |
577 | /* | |
578 | * We can get woken by laptop mode, to do a sync - | |
579 | * that's the (only!) case where the list would be | |
580 | * empty with time remaining. | |
581 | */ | |
582 | if (!timeleft || list_empty(&mp->m_sync_list)) { | |
583 | if (!timeleft) | |
584 | timeleft = xfs_syncd_centisecs * | |
585 | msecs_to_jiffies(10); | |
586 | INIT_LIST_HEAD(&mp->m_sync_work.w_list); | |
587 | list_add_tail(&mp->m_sync_work.w_list, | |
588 | &mp->m_sync_list); | |
589 | } | |
20f6b2c7 | 590 | list_splice_init(&mp->m_sync_list, &tmp); |
a167b17e DC |
591 | spin_unlock(&mp->m_sync_lock); |
592 | ||
593 | list_for_each_entry_safe(work, n, &tmp, w_list) { | |
594 | (*work->w_syncer)(mp, work->w_data); | |
595 | list_del(&work->w_list); | |
596 | if (work == &mp->m_sync_work) | |
597 | continue; | |
e43afd72 DC |
598 | if (work->w_completion) |
599 | complete(work->w_completion); | |
a167b17e DC |
600 | kmem_free(work); |
601 | } | |
602 | } | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | int | |
608 | xfs_syncd_init( | |
609 | struct xfs_mount *mp) | |
610 | { | |
611 | mp->m_sync_work.w_syncer = xfs_sync_worker; | |
612 | mp->m_sync_work.w_mount = mp; | |
e43afd72 | 613 | mp->m_sync_work.w_completion = NULL; |
e2a07812 | 614 | mp->m_sync_task = kthread_run(xfssyncd, mp, "xfssyncd/%s", mp->m_fsname); |
a167b17e DC |
615 | if (IS_ERR(mp->m_sync_task)) |
616 | return -PTR_ERR(mp->m_sync_task); | |
617 | return 0; | |
618 | } | |
619 | ||
620 | void | |
621 | xfs_syncd_stop( | |
622 | struct xfs_mount *mp) | |
623 | { | |
624 | kthread_stop(mp->m_sync_task); | |
625 | } | |
626 | ||
bc990f5c CH |
627 | void |
628 | __xfs_inode_set_reclaim_tag( | |
629 | struct xfs_perag *pag, | |
630 | struct xfs_inode *ip) | |
631 | { | |
632 | radix_tree_tag_set(&pag->pag_ici_root, | |
633 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), | |
634 | XFS_ICI_RECLAIM_TAG); | |
16fd5367 DC |
635 | |
636 | if (!pag->pag_ici_reclaimable) { | |
637 | /* propagate the reclaim tag up into the perag radix tree */ | |
638 | spin_lock(&ip->i_mount->m_perag_lock); | |
639 | radix_tree_tag_set(&ip->i_mount->m_perag_tree, | |
640 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | |
641 | XFS_ICI_RECLAIM_TAG); | |
642 | spin_unlock(&ip->i_mount->m_perag_lock); | |
643 | trace_xfs_perag_set_reclaim(ip->i_mount, pag->pag_agno, | |
644 | -1, _RET_IP_); | |
645 | } | |
9bf729c0 | 646 | pag->pag_ici_reclaimable++; |
bc990f5c CH |
647 | } |
648 | ||
11654513 DC |
649 | /* |
650 | * We set the inode flag atomically with the radix tree tag. | |
651 | * Once we get tag lookups on the radix tree, this inode flag | |
652 | * can go away. | |
653 | */ | |
396beb85 DC |
654 | void |
655 | xfs_inode_set_reclaim_tag( | |
656 | xfs_inode_t *ip) | |
657 | { | |
5017e97d DC |
658 | struct xfs_mount *mp = ip->i_mount; |
659 | struct xfs_perag *pag; | |
396beb85 | 660 | |
5017e97d | 661 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
f1f724e4 | 662 | write_lock(&pag->pag_ici_lock); |
396beb85 | 663 | spin_lock(&ip->i_flags_lock); |
bc990f5c | 664 | __xfs_inode_set_reclaim_tag(pag, ip); |
11654513 | 665 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); |
396beb85 | 666 | spin_unlock(&ip->i_flags_lock); |
f1f724e4 | 667 | write_unlock(&pag->pag_ici_lock); |
5017e97d | 668 | xfs_perag_put(pag); |
396beb85 DC |
669 | } |
670 | ||
081003ff JW |
671 | STATIC void |
672 | __xfs_inode_clear_reclaim( | |
396beb85 DC |
673 | xfs_perag_t *pag, |
674 | xfs_inode_t *ip) | |
675 | { | |
9bf729c0 | 676 | pag->pag_ici_reclaimable--; |
16fd5367 DC |
677 | if (!pag->pag_ici_reclaimable) { |
678 | /* clear the reclaim tag from the perag radix tree */ | |
679 | spin_lock(&ip->i_mount->m_perag_lock); | |
680 | radix_tree_tag_clear(&ip->i_mount->m_perag_tree, | |
681 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | |
682 | XFS_ICI_RECLAIM_TAG); | |
683 | spin_unlock(&ip->i_mount->m_perag_lock); | |
684 | trace_xfs_perag_clear_reclaim(ip->i_mount, pag->pag_agno, | |
685 | -1, _RET_IP_); | |
686 | } | |
396beb85 DC |
687 | } |
688 | ||
081003ff JW |
689 | void |
690 | __xfs_inode_clear_reclaim_tag( | |
691 | xfs_mount_t *mp, | |
692 | xfs_perag_t *pag, | |
693 | xfs_inode_t *ip) | |
694 | { | |
695 | radix_tree_tag_clear(&pag->pag_ici_root, | |
696 | XFS_INO_TO_AGINO(mp, ip->i_ino), XFS_ICI_RECLAIM_TAG); | |
697 | __xfs_inode_clear_reclaim(pag, ip); | |
698 | } | |
699 | ||
777df5af DC |
700 | /* |
701 | * Inodes in different states need to be treated differently, and the return | |
702 | * value of xfs_iflush is not sufficient to get this right. The following table | |
703 | * lists the inode states and the reclaim actions necessary for non-blocking | |
704 | * reclaim: | |
705 | * | |
706 | * | |
707 | * inode state iflush ret required action | |
708 | * --------------- ---------- --------------- | |
709 | * bad - reclaim | |
710 | * shutdown EIO unpin and reclaim | |
711 | * clean, unpinned 0 reclaim | |
712 | * stale, unpinned 0 reclaim | |
c854363e DC |
713 | * clean, pinned(*) 0 requeue |
714 | * stale, pinned EAGAIN requeue | |
715 | * dirty, delwri ok 0 requeue | |
716 | * dirty, delwri blocked EAGAIN requeue | |
717 | * dirty, sync flush 0 reclaim | |
777df5af DC |
718 | * |
719 | * (*) dgc: I don't think the clean, pinned state is possible but it gets | |
720 | * handled anyway given the order of checks implemented. | |
721 | * | |
c854363e DC |
722 | * As can be seen from the table, the return value of xfs_iflush() is not |
723 | * sufficient to correctly decide the reclaim action here. The checks in | |
724 | * xfs_iflush() might look like duplicates, but they are not. | |
725 | * | |
726 | * Also, because we get the flush lock first, we know that any inode that has | |
727 | * been flushed delwri has had the flush completed by the time we check that | |
728 | * the inode is clean. The clean inode check needs to be done before flushing | |
729 | * the inode delwri otherwise we would loop forever requeuing clean inodes as | |
730 | * we cannot tell apart a successful delwri flush and a clean inode from the | |
731 | * return value of xfs_iflush(). | |
732 | * | |
733 | * Note that because the inode is flushed delayed write by background | |
734 | * writeback, the flush lock may already be held here and waiting on it can | |
735 | * result in very long latencies. Hence for sync reclaims, where we wait on the | |
736 | * flush lock, the caller should push out delayed write inodes first before | |
737 | * trying to reclaim them to minimise the amount of time spent waiting. For | |
738 | * background relaim, we just requeue the inode for the next pass. | |
739 | * | |
777df5af DC |
740 | * Hence the order of actions after gaining the locks should be: |
741 | * bad => reclaim | |
742 | * shutdown => unpin and reclaim | |
c854363e DC |
743 | * pinned, delwri => requeue |
744 | * pinned, sync => unpin | |
777df5af DC |
745 | * stale => reclaim |
746 | * clean => reclaim | |
c854363e DC |
747 | * dirty, delwri => flush and requeue |
748 | * dirty, sync => flush, wait and reclaim | |
777df5af | 749 | */ |
75f3cb13 | 750 | STATIC int |
c8e20be0 | 751 | xfs_reclaim_inode( |
75f3cb13 DC |
752 | struct xfs_inode *ip, |
753 | struct xfs_perag *pag, | |
c8e20be0 | 754 | int sync_mode) |
fce08f2f | 755 | { |
c854363e | 756 | int error = 0; |
777df5af | 757 | |
c8e20be0 DC |
758 | /* |
759 | * The radix tree lock here protects a thread in xfs_iget from racing | |
760 | * with us starting reclaim on the inode. Once we have the | |
761 | * XFS_IRECLAIM flag set it will not touch us. | |
762 | */ | |
763 | spin_lock(&ip->i_flags_lock); | |
764 | ASSERT_ALWAYS(__xfs_iflags_test(ip, XFS_IRECLAIMABLE)); | |
765 | if (__xfs_iflags_test(ip, XFS_IRECLAIM)) { | |
766 | /* ignore as it is already under reclaim */ | |
767 | spin_unlock(&ip->i_flags_lock); | |
768 | write_unlock(&pag->pag_ici_lock); | |
75f3cb13 | 769 | return 0; |
fce08f2f | 770 | } |
c8e20be0 DC |
771 | __xfs_iflags_set(ip, XFS_IRECLAIM); |
772 | spin_unlock(&ip->i_flags_lock); | |
773 | write_unlock(&pag->pag_ici_lock); | |
774 | ||
c8e20be0 | 775 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
c854363e DC |
776 | if (!xfs_iflock_nowait(ip)) { |
777 | if (!(sync_mode & SYNC_WAIT)) | |
778 | goto out; | |
779 | xfs_iflock(ip); | |
780 | } | |
7a3be02b | 781 | |
777df5af DC |
782 | if (is_bad_inode(VFS_I(ip))) |
783 | goto reclaim; | |
784 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { | |
785 | xfs_iunpin_wait(ip); | |
786 | goto reclaim; | |
787 | } | |
c854363e DC |
788 | if (xfs_ipincount(ip)) { |
789 | if (!(sync_mode & SYNC_WAIT)) { | |
790 | xfs_ifunlock(ip); | |
791 | goto out; | |
792 | } | |
777df5af | 793 | xfs_iunpin_wait(ip); |
c854363e | 794 | } |
777df5af DC |
795 | if (xfs_iflags_test(ip, XFS_ISTALE)) |
796 | goto reclaim; | |
797 | if (xfs_inode_clean(ip)) | |
798 | goto reclaim; | |
799 | ||
800 | /* Now we have an inode that needs flushing */ | |
801 | error = xfs_iflush(ip, sync_mode); | |
c854363e DC |
802 | if (sync_mode & SYNC_WAIT) { |
803 | xfs_iflock(ip); | |
804 | goto reclaim; | |
c8e20be0 DC |
805 | } |
806 | ||
c854363e DC |
807 | /* |
808 | * When we have to flush an inode but don't have SYNC_WAIT set, we | |
809 | * flush the inode out using a delwri buffer and wait for the next | |
810 | * call into reclaim to find it in a clean state instead of waiting for | |
811 | * it now. We also don't return errors here - if the error is transient | |
812 | * then the next reclaim pass will flush the inode, and if the error | |
f1d486a3 | 813 | * is permanent then the next sync reclaim will reclaim the inode and |
c854363e DC |
814 | * pass on the error. |
815 | */ | |
f1d486a3 | 816 | if (error && error != EAGAIN && !XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
c854363e DC |
817 | xfs_fs_cmn_err(CE_WARN, ip->i_mount, |
818 | "inode 0x%llx background reclaim flush failed with %d", | |
819 | (long long)ip->i_ino, error); | |
820 | } | |
821 | out: | |
822 | xfs_iflags_clear(ip, XFS_IRECLAIM); | |
823 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
824 | /* | |
825 | * We could return EAGAIN here to make reclaim rescan the inode tree in | |
826 | * a short while. However, this just burns CPU time scanning the tree | |
827 | * waiting for IO to complete and xfssyncd never goes back to the idle | |
828 | * state. Instead, return 0 to let the next scheduled background reclaim | |
829 | * attempt to reclaim the inode again. | |
830 | */ | |
831 | return 0; | |
832 | ||
777df5af DC |
833 | reclaim: |
834 | xfs_ifunlock(ip); | |
c8e20be0 | 835 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
2f11feab DC |
836 | |
837 | XFS_STATS_INC(xs_ig_reclaims); | |
838 | /* | |
839 | * Remove the inode from the per-AG radix tree. | |
840 | * | |
841 | * Because radix_tree_delete won't complain even if the item was never | |
842 | * added to the tree assert that it's been there before to catch | |
843 | * problems with the inode life time early on. | |
844 | */ | |
845 | write_lock(&pag->pag_ici_lock); | |
846 | if (!radix_tree_delete(&pag->pag_ici_root, | |
847 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino))) | |
848 | ASSERT(0); | |
081003ff | 849 | __xfs_inode_clear_reclaim(pag, ip); |
2f11feab DC |
850 | write_unlock(&pag->pag_ici_lock); |
851 | ||
852 | /* | |
853 | * Here we do an (almost) spurious inode lock in order to coordinate | |
854 | * with inode cache radix tree lookups. This is because the lookup | |
855 | * can reference the inodes in the cache without taking references. | |
856 | * | |
857 | * We make that OK here by ensuring that we wait until the inode is | |
858 | * unlocked after the lookup before we go ahead and free it. We get | |
859 | * both the ilock and the iolock because the code may need to drop the | |
860 | * ilock one but will still hold the iolock. | |
861 | */ | |
862 | xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | |
863 | xfs_qm_dqdetach(ip); | |
864 | xfs_iunlock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL); | |
865 | ||
866 | xfs_inode_free(ip); | |
c854363e DC |
867 | return error; |
868 | ||
7a3be02b DC |
869 | } |
870 | ||
871 | int | |
872 | xfs_reclaim_inodes( | |
873 | xfs_mount_t *mp, | |
7a3be02b DC |
874 | int mode) |
875 | { | |
c8e20be0 | 876 | return xfs_inode_ag_iterator(mp, xfs_reclaim_inode, mode, |
9bf729c0 DC |
877 | XFS_ICI_RECLAIM_TAG, 1, NULL); |
878 | } | |
879 | ||
880 | /* | |
881 | * Shrinker infrastructure. | |
9bf729c0 | 882 | */ |
9bf729c0 DC |
883 | static int |
884 | xfs_reclaim_inode_shrink( | |
7f8275d0 | 885 | struct shrinker *shrink, |
9bf729c0 DC |
886 | int nr_to_scan, |
887 | gfp_t gfp_mask) | |
888 | { | |
889 | struct xfs_mount *mp; | |
890 | struct xfs_perag *pag; | |
891 | xfs_agnumber_t ag; | |
16fd5367 | 892 | int reclaimable; |
9bf729c0 | 893 | |
70e60ce7 | 894 | mp = container_of(shrink, struct xfs_mount, m_inode_shrink); |
9bf729c0 DC |
895 | if (nr_to_scan) { |
896 | if (!(gfp_mask & __GFP_FS)) | |
897 | return -1; | |
898 | ||
70e60ce7 | 899 | xfs_inode_ag_iterator(mp, xfs_reclaim_inode, 0, |
9bf729c0 | 900 | XFS_ICI_RECLAIM_TAG, 1, &nr_to_scan); |
70e60ce7 DC |
901 | /* if we don't exhaust the scan, don't bother coming back */ |
902 | if (nr_to_scan > 0) | |
903 | return -1; | |
904 | } | |
9bf729c0 | 905 | |
16fd5367 DC |
906 | reclaimable = 0; |
907 | ag = 0; | |
908 | while ((pag = xfs_inode_ag_iter_next_pag(mp, &ag, | |
909 | XFS_ICI_RECLAIM_TAG))) { | |
70e60ce7 DC |
910 | reclaimable += pag->pag_ici_reclaimable; |
911 | xfs_perag_put(pag); | |
9bf729c0 | 912 | } |
9bf729c0 DC |
913 | return reclaimable; |
914 | } | |
915 | ||
9bf729c0 DC |
916 | void |
917 | xfs_inode_shrinker_register( | |
918 | struct xfs_mount *mp) | |
919 | { | |
70e60ce7 DC |
920 | mp->m_inode_shrink.shrink = xfs_reclaim_inode_shrink; |
921 | mp->m_inode_shrink.seeks = DEFAULT_SEEKS; | |
922 | register_shrinker(&mp->m_inode_shrink); | |
9bf729c0 DC |
923 | } |
924 | ||
925 | void | |
926 | xfs_inode_shrinker_unregister( | |
927 | struct xfs_mount *mp) | |
928 | { | |
70e60ce7 | 929 | unregister_shrinker(&mp->m_inode_shrink); |
fce08f2f | 930 | } |