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