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