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