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