Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
fe4fa4b8 DC |
2 | /* |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. | |
4 | * All Rights Reserved. | |
fe4fa4b8 DC |
5 | */ |
6 | #include "xfs.h" | |
7 | #include "xfs_fs.h" | |
5467b34b | 8 | #include "xfs_shared.h" |
6ca1c906 | 9 | #include "xfs_format.h" |
239880ef DC |
10 | #include "xfs_log_format.h" |
11 | #include "xfs_trans_resv.h" | |
fe4fa4b8 | 12 | #include "xfs_sb.h" |
fe4fa4b8 | 13 | #include "xfs_mount.h" |
fe4fa4b8 | 14 | #include "xfs_inode.h" |
239880ef DC |
15 | #include "xfs_trans.h" |
16 | #include "xfs_trans_priv.h" | |
fe4fa4b8 | 17 | #include "xfs_inode_item.h" |
7d095257 | 18 | #include "xfs_quota.h" |
0b1b213f | 19 | #include "xfs_trace.h" |
6d8b79cf | 20 | #include "xfs_icache.h" |
c24b5dfa | 21 | #include "xfs_bmap_util.h" |
dc06f398 BF |
22 | #include "xfs_dquot_item.h" |
23 | #include "xfs_dquot.h" | |
83104d44 | 24 | #include "xfs_reflink.h" |
bb8a66af | 25 | #include "xfs_ialloc.h" |
fe4fa4b8 | 26 | |
f0e28280 | 27 | #include <linux/iversion.h> |
a167b17e | 28 | |
33479e05 DC |
29 | /* |
30 | * Allocate and initialise an xfs_inode. | |
31 | */ | |
638f4416 | 32 | struct xfs_inode * |
33479e05 DC |
33 | xfs_inode_alloc( |
34 | struct xfs_mount *mp, | |
35 | xfs_ino_t ino) | |
36 | { | |
37 | struct xfs_inode *ip; | |
38 | ||
39 | /* | |
3050bd0b CM |
40 | * XXX: If this didn't occur in transactions, we could drop GFP_NOFAIL |
41 | * and return NULL here on ENOMEM. | |
33479e05 | 42 | */ |
3050bd0b CM |
43 | ip = kmem_cache_alloc(xfs_inode_zone, GFP_KERNEL | __GFP_NOFAIL); |
44 | ||
33479e05 | 45 | if (inode_init_always(mp->m_super, VFS_I(ip))) { |
377bcd5f | 46 | kmem_cache_free(xfs_inode_zone, ip); |
33479e05 DC |
47 | return NULL; |
48 | } | |
49 | ||
c19b3b05 DC |
50 | /* VFS doesn't initialise i_mode! */ |
51 | VFS_I(ip)->i_mode = 0; | |
52 | ||
ff6d6af2 | 53 | XFS_STATS_INC(mp, vn_active); |
33479e05 | 54 | ASSERT(atomic_read(&ip->i_pincount) == 0); |
33479e05 DC |
55 | ASSERT(ip->i_ino == 0); |
56 | ||
33479e05 DC |
57 | /* initialise the xfs inode */ |
58 | ip->i_ino = ino; | |
59 | ip->i_mount = mp; | |
60 | memset(&ip->i_imap, 0, sizeof(struct xfs_imap)); | |
61 | ip->i_afp = NULL; | |
3993baeb | 62 | ip->i_cowfp = NULL; |
3ba738df | 63 | memset(&ip->i_df, 0, sizeof(ip->i_df)); |
33479e05 DC |
64 | ip->i_flags = 0; |
65 | ip->i_delayed_blks = 0; | |
4cb6f2e8 | 66 | ip->i_d.di_flags2 = mp->m_ino_geo.new_diflags2; |
6e73a545 | 67 | ip->i_nblocks = 0; |
7821ea30 | 68 | ip->i_forkoff = 0; |
6772c1f1 DW |
69 | ip->i_sick = 0; |
70 | ip->i_checked = 0; | |
cb357bf3 DW |
71 | INIT_WORK(&ip->i_ioend_work, xfs_end_io); |
72 | INIT_LIST_HEAD(&ip->i_ioend_list); | |
73 | spin_lock_init(&ip->i_ioend_lock); | |
33479e05 DC |
74 | |
75 | return ip; | |
76 | } | |
77 | ||
78 | STATIC void | |
79 | xfs_inode_free_callback( | |
80 | struct rcu_head *head) | |
81 | { | |
82 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
83 | struct xfs_inode *ip = XFS_I(inode); | |
84 | ||
c19b3b05 | 85 | switch (VFS_I(ip)->i_mode & S_IFMT) { |
33479e05 DC |
86 | case S_IFREG: |
87 | case S_IFDIR: | |
88 | case S_IFLNK: | |
ef838512 | 89 | xfs_idestroy_fork(&ip->i_df); |
33479e05 DC |
90 | break; |
91 | } | |
92 | ||
ef838512 CH |
93 | if (ip->i_afp) { |
94 | xfs_idestroy_fork(ip->i_afp); | |
95 | kmem_cache_free(xfs_ifork_zone, ip->i_afp); | |
96 | } | |
97 | if (ip->i_cowfp) { | |
98 | xfs_idestroy_fork(ip->i_cowfp); | |
99 | kmem_cache_free(xfs_ifork_zone, ip->i_cowfp); | |
100 | } | |
33479e05 | 101 | if (ip->i_itemp) { |
22525c17 DC |
102 | ASSERT(!test_bit(XFS_LI_IN_AIL, |
103 | &ip->i_itemp->ili_item.li_flags)); | |
33479e05 DC |
104 | xfs_inode_item_destroy(ip); |
105 | ip->i_itemp = NULL; | |
106 | } | |
107 | ||
377bcd5f | 108 | kmem_cache_free(xfs_inode_zone, ip); |
1f2dcfe8 DC |
109 | } |
110 | ||
8a17d7dd DC |
111 | static void |
112 | __xfs_inode_free( | |
113 | struct xfs_inode *ip) | |
114 | { | |
115 | /* asserts to verify all state is correct here */ | |
116 | ASSERT(atomic_read(&ip->i_pincount) == 0); | |
48d55e2a | 117 | ASSERT(!ip->i_itemp || list_empty(&ip->i_itemp->ili_item.li_bio_list)); |
8a17d7dd DC |
118 | XFS_STATS_DEC(ip->i_mount, vn_active); |
119 | ||
120 | call_rcu(&VFS_I(ip)->i_rcu, xfs_inode_free_callback); | |
121 | } | |
122 | ||
1f2dcfe8 DC |
123 | void |
124 | xfs_inode_free( | |
125 | struct xfs_inode *ip) | |
126 | { | |
718ecc50 | 127 | ASSERT(!xfs_iflags_test(ip, XFS_IFLUSHING)); |
98efe8af | 128 | |
33479e05 DC |
129 | /* |
130 | * Because we use RCU freeing we need to ensure the inode always | |
131 | * appears to be reclaimed with an invalid inode number when in the | |
132 | * free state. The ip->i_flags_lock provides the barrier against lookup | |
133 | * races. | |
134 | */ | |
135 | spin_lock(&ip->i_flags_lock); | |
136 | ip->i_flags = XFS_IRECLAIM; | |
137 | ip->i_ino = 0; | |
138 | spin_unlock(&ip->i_flags_lock); | |
139 | ||
8a17d7dd | 140 | __xfs_inode_free(ip); |
33479e05 DC |
141 | } |
142 | ||
ad438c40 | 143 | /* |
02511a5a DC |
144 | * Queue background inode reclaim work if there are reclaimable inodes and there |
145 | * isn't reclaim work already scheduled or in progress. | |
ad438c40 DC |
146 | */ |
147 | static void | |
148 | xfs_reclaim_work_queue( | |
149 | struct xfs_mount *mp) | |
150 | { | |
151 | ||
152 | rcu_read_lock(); | |
153 | if (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) { | |
154 | queue_delayed_work(mp->m_reclaim_workqueue, &mp->m_reclaim_work, | |
155 | msecs_to_jiffies(xfs_syncd_centisecs / 6 * 10)); | |
156 | } | |
157 | rcu_read_unlock(); | |
158 | } | |
159 | ||
ad438c40 DC |
160 | static void |
161 | xfs_perag_set_reclaim_tag( | |
162 | struct xfs_perag *pag) | |
163 | { | |
164 | struct xfs_mount *mp = pag->pag_mount; | |
165 | ||
95989c46 | 166 | lockdep_assert_held(&pag->pag_ici_lock); |
ad438c40 DC |
167 | if (pag->pag_ici_reclaimable++) |
168 | return; | |
169 | ||
170 | /* propagate the reclaim tag up into the perag radix tree */ | |
171 | spin_lock(&mp->m_perag_lock); | |
172 | radix_tree_tag_set(&mp->m_perag_tree, pag->pag_agno, | |
173 | XFS_ICI_RECLAIM_TAG); | |
174 | spin_unlock(&mp->m_perag_lock); | |
175 | ||
176 | /* schedule periodic background inode reclaim */ | |
177 | xfs_reclaim_work_queue(mp); | |
178 | ||
179 | trace_xfs_perag_set_reclaim(mp, pag->pag_agno, -1, _RET_IP_); | |
180 | } | |
181 | ||
182 | static void | |
183 | xfs_perag_clear_reclaim_tag( | |
184 | struct xfs_perag *pag) | |
185 | { | |
186 | struct xfs_mount *mp = pag->pag_mount; | |
187 | ||
95989c46 | 188 | lockdep_assert_held(&pag->pag_ici_lock); |
ad438c40 DC |
189 | if (--pag->pag_ici_reclaimable) |
190 | return; | |
191 | ||
192 | /* clear the reclaim tag from the perag radix tree */ | |
193 | spin_lock(&mp->m_perag_lock); | |
194 | radix_tree_tag_clear(&mp->m_perag_tree, pag->pag_agno, | |
195 | XFS_ICI_RECLAIM_TAG); | |
196 | spin_unlock(&mp->m_perag_lock); | |
197 | trace_xfs_perag_clear_reclaim(mp, pag->pag_agno, -1, _RET_IP_); | |
198 | } | |
199 | ||
200 | ||
201 | /* | |
202 | * We set the inode flag atomically with the radix tree tag. | |
203 | * Once we get tag lookups on the radix tree, this inode flag | |
204 | * can go away. | |
205 | */ | |
206 | void | |
207 | xfs_inode_set_reclaim_tag( | |
208 | struct xfs_inode *ip) | |
209 | { | |
210 | struct xfs_mount *mp = ip->i_mount; | |
211 | struct xfs_perag *pag; | |
212 | ||
213 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); | |
214 | spin_lock(&pag->pag_ici_lock); | |
215 | spin_lock(&ip->i_flags_lock); | |
216 | ||
217 | radix_tree_tag_set(&pag->pag_ici_root, XFS_INO_TO_AGINO(mp, ip->i_ino), | |
218 | XFS_ICI_RECLAIM_TAG); | |
219 | xfs_perag_set_reclaim_tag(pag); | |
220 | __xfs_iflags_set(ip, XFS_IRECLAIMABLE); | |
221 | ||
222 | spin_unlock(&ip->i_flags_lock); | |
223 | spin_unlock(&pag->pag_ici_lock); | |
224 | xfs_perag_put(pag); | |
225 | } | |
226 | ||
227 | STATIC void | |
228 | xfs_inode_clear_reclaim_tag( | |
229 | struct xfs_perag *pag, | |
230 | xfs_ino_t ino) | |
231 | { | |
232 | radix_tree_tag_clear(&pag->pag_ici_root, | |
233 | XFS_INO_TO_AGINO(pag->pag_mount, ino), | |
234 | XFS_ICI_RECLAIM_TAG); | |
235 | xfs_perag_clear_reclaim_tag(pag); | |
236 | } | |
237 | ||
ae2c4ac2 BF |
238 | static void |
239 | xfs_inew_wait( | |
240 | struct xfs_inode *ip) | |
241 | { | |
242 | wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_INEW_BIT); | |
243 | DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_INEW_BIT); | |
244 | ||
245 | do { | |
21417136 | 246 | prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE); |
ae2c4ac2 BF |
247 | if (!xfs_iflags_test(ip, XFS_INEW)) |
248 | break; | |
249 | schedule(); | |
250 | } while (true); | |
21417136 | 251 | finish_wait(wq, &wait.wq_entry); |
ae2c4ac2 BF |
252 | } |
253 | ||
50997470 DC |
254 | /* |
255 | * When we recycle a reclaimable inode, we need to re-initialise the VFS inode | |
256 | * part of the structure. This is made more complex by the fact we store | |
257 | * information about the on-disk values in the VFS inode and so we can't just | |
83e06f21 | 258 | * overwrite the values unconditionally. Hence we save the parameters we |
50997470 | 259 | * need to retain across reinitialisation, and rewrite them into the VFS inode |
83e06f21 | 260 | * after reinitialisation even if it fails. |
50997470 DC |
261 | */ |
262 | static int | |
263 | xfs_reinit_inode( | |
264 | struct xfs_mount *mp, | |
265 | struct inode *inode) | |
266 | { | |
267 | int error; | |
54d7b5c1 | 268 | uint32_t nlink = inode->i_nlink; |
9e9a2674 | 269 | uint32_t generation = inode->i_generation; |
f0e28280 | 270 | uint64_t version = inode_peek_iversion(inode); |
c19b3b05 | 271 | umode_t mode = inode->i_mode; |
acd1d715 | 272 | dev_t dev = inode->i_rdev; |
3d8f2821 CH |
273 | kuid_t uid = inode->i_uid; |
274 | kgid_t gid = inode->i_gid; | |
50997470 DC |
275 | |
276 | error = inode_init_always(mp->m_super, inode); | |
277 | ||
54d7b5c1 | 278 | set_nlink(inode, nlink); |
9e9a2674 | 279 | inode->i_generation = generation; |
f0e28280 | 280 | inode_set_iversion_queried(inode, version); |
c19b3b05 | 281 | inode->i_mode = mode; |
acd1d715 | 282 | inode->i_rdev = dev; |
3d8f2821 CH |
283 | inode->i_uid = uid; |
284 | inode->i_gid = gid; | |
50997470 DC |
285 | return error; |
286 | } | |
287 | ||
afca6c5b DC |
288 | /* |
289 | * If we are allocating a new inode, then check what was returned is | |
290 | * actually a free, empty inode. If we are not allocating an inode, | |
291 | * then check we didn't find a free inode. | |
292 | * | |
293 | * Returns: | |
294 | * 0 if the inode free state matches the lookup context | |
295 | * -ENOENT if the inode is free and we are not allocating | |
296 | * -EFSCORRUPTED if there is any state mismatch at all | |
297 | */ | |
298 | static int | |
299 | xfs_iget_check_free_state( | |
300 | struct xfs_inode *ip, | |
301 | int flags) | |
302 | { | |
303 | if (flags & XFS_IGET_CREATE) { | |
304 | /* should be a free inode */ | |
305 | if (VFS_I(ip)->i_mode != 0) { | |
306 | xfs_warn(ip->i_mount, | |
307 | "Corruption detected! Free inode 0x%llx not marked free! (mode 0x%x)", | |
308 | ip->i_ino, VFS_I(ip)->i_mode); | |
309 | return -EFSCORRUPTED; | |
310 | } | |
311 | ||
6e73a545 | 312 | if (ip->i_nblocks != 0) { |
afca6c5b DC |
313 | xfs_warn(ip->i_mount, |
314 | "Corruption detected! Free inode 0x%llx has blocks allocated!", | |
315 | ip->i_ino); | |
316 | return -EFSCORRUPTED; | |
317 | } | |
318 | return 0; | |
319 | } | |
320 | ||
321 | /* should be an allocated inode */ | |
322 | if (VFS_I(ip)->i_mode == 0) | |
323 | return -ENOENT; | |
324 | ||
325 | return 0; | |
326 | } | |
327 | ||
33479e05 DC |
328 | /* |
329 | * Check the validity of the inode we just found it the cache | |
330 | */ | |
331 | static int | |
332 | xfs_iget_cache_hit( | |
333 | struct xfs_perag *pag, | |
334 | struct xfs_inode *ip, | |
335 | xfs_ino_t ino, | |
336 | int flags, | |
337 | int lock_flags) __releases(RCU) | |
338 | { | |
339 | struct inode *inode = VFS_I(ip); | |
340 | struct xfs_mount *mp = ip->i_mount; | |
341 | int error; | |
342 | ||
343 | /* | |
344 | * check for re-use of an inode within an RCU grace period due to the | |
345 | * radix tree nodes not being updated yet. We monitor for this by | |
346 | * setting the inode number to zero before freeing the inode structure. | |
347 | * If the inode has been reallocated and set up, then the inode number | |
348 | * will not match, so check for that, too. | |
349 | */ | |
350 | spin_lock(&ip->i_flags_lock); | |
351 | if (ip->i_ino != ino) { | |
352 | trace_xfs_iget_skip(ip); | |
ff6d6af2 | 353 | XFS_STATS_INC(mp, xs_ig_frecycle); |
2451337d | 354 | error = -EAGAIN; |
33479e05 DC |
355 | goto out_error; |
356 | } | |
357 | ||
358 | ||
359 | /* | |
360 | * If we are racing with another cache hit that is currently | |
361 | * instantiating this inode or currently recycling it out of | |
362 | * reclaimabe state, wait for the initialisation to complete | |
363 | * before continuing. | |
364 | * | |
365 | * XXX(hch): eventually we should do something equivalent to | |
366 | * wait_on_inode to wait for these flags to be cleared | |
367 | * instead of polling for it. | |
368 | */ | |
369 | if (ip->i_flags & (XFS_INEW|XFS_IRECLAIM)) { | |
370 | trace_xfs_iget_skip(ip); | |
ff6d6af2 | 371 | XFS_STATS_INC(mp, xs_ig_frecycle); |
2451337d | 372 | error = -EAGAIN; |
33479e05 DC |
373 | goto out_error; |
374 | } | |
375 | ||
376 | /* | |
afca6c5b DC |
377 | * Check the inode free state is valid. This also detects lookup |
378 | * racing with unlinks. | |
33479e05 | 379 | */ |
afca6c5b DC |
380 | error = xfs_iget_check_free_state(ip, flags); |
381 | if (error) | |
33479e05 | 382 | goto out_error; |
33479e05 DC |
383 | |
384 | /* | |
385 | * If IRECLAIMABLE is set, we've torn down the VFS inode already. | |
386 | * Need to carefully get it back into useable state. | |
387 | */ | |
388 | if (ip->i_flags & XFS_IRECLAIMABLE) { | |
389 | trace_xfs_iget_reclaim(ip); | |
390 | ||
378f681c DW |
391 | if (flags & XFS_IGET_INCORE) { |
392 | error = -EAGAIN; | |
393 | goto out_error; | |
394 | } | |
395 | ||
33479e05 DC |
396 | /* |
397 | * We need to set XFS_IRECLAIM to prevent xfs_reclaim_inode | |
398 | * from stomping over us while we recycle the inode. We can't | |
399 | * clear the radix tree reclaimable tag yet as it requires | |
400 | * pag_ici_lock to be held exclusive. | |
401 | */ | |
402 | ip->i_flags |= XFS_IRECLAIM; | |
403 | ||
404 | spin_unlock(&ip->i_flags_lock); | |
405 | rcu_read_unlock(); | |
406 | ||
d45344d6 | 407 | ASSERT(!rwsem_is_locked(&inode->i_rwsem)); |
50997470 | 408 | error = xfs_reinit_inode(mp, inode); |
33479e05 | 409 | if (error) { |
756baca2 | 410 | bool wake; |
33479e05 DC |
411 | /* |
412 | * Re-initializing the inode failed, and we are in deep | |
413 | * trouble. Try to re-add it to the reclaim list. | |
414 | */ | |
415 | rcu_read_lock(); | |
416 | spin_lock(&ip->i_flags_lock); | |
756baca2 | 417 | wake = !!__xfs_iflags_test(ip, XFS_INEW); |
33479e05 | 418 | ip->i_flags &= ~(XFS_INEW | XFS_IRECLAIM); |
756baca2 BF |
419 | if (wake) |
420 | wake_up_bit(&ip->i_flags, __XFS_INEW_BIT); | |
33479e05 DC |
421 | ASSERT(ip->i_flags & XFS_IRECLAIMABLE); |
422 | trace_xfs_iget_reclaim_fail(ip); | |
423 | goto out_error; | |
424 | } | |
425 | ||
426 | spin_lock(&pag->pag_ici_lock); | |
427 | spin_lock(&ip->i_flags_lock); | |
428 | ||
429 | /* | |
430 | * Clear the per-lifetime state in the inode as we are now | |
431 | * effectively a new inode and need to return to the initial | |
432 | * state before reuse occurs. | |
433 | */ | |
434 | ip->i_flags &= ~XFS_IRECLAIM_RESET_FLAGS; | |
435 | ip->i_flags |= XFS_INEW; | |
545c0889 | 436 | xfs_inode_clear_reclaim_tag(pag, ip->i_ino); |
33479e05 | 437 | inode->i_state = I_NEW; |
6772c1f1 DW |
438 | ip->i_sick = 0; |
439 | ip->i_checked = 0; | |
33479e05 | 440 | |
33479e05 DC |
441 | spin_unlock(&ip->i_flags_lock); |
442 | spin_unlock(&pag->pag_ici_lock); | |
443 | } else { | |
444 | /* If the VFS inode is being torn down, pause and try again. */ | |
445 | if (!igrab(inode)) { | |
446 | trace_xfs_iget_skip(ip); | |
2451337d | 447 | error = -EAGAIN; |
33479e05 DC |
448 | goto out_error; |
449 | } | |
450 | ||
451 | /* We've got a live one. */ | |
452 | spin_unlock(&ip->i_flags_lock); | |
453 | rcu_read_unlock(); | |
454 | trace_xfs_iget_hit(ip); | |
455 | } | |
456 | ||
457 | if (lock_flags != 0) | |
458 | xfs_ilock(ip, lock_flags); | |
459 | ||
378f681c | 460 | if (!(flags & XFS_IGET_INCORE)) |
dae2f8ed | 461 | xfs_iflags_clear(ip, XFS_ISTALE); |
ff6d6af2 | 462 | XFS_STATS_INC(mp, xs_ig_found); |
33479e05 DC |
463 | |
464 | return 0; | |
465 | ||
466 | out_error: | |
467 | spin_unlock(&ip->i_flags_lock); | |
468 | rcu_read_unlock(); | |
469 | return error; | |
470 | } | |
471 | ||
472 | ||
473 | static int | |
474 | xfs_iget_cache_miss( | |
475 | struct xfs_mount *mp, | |
476 | struct xfs_perag *pag, | |
477 | xfs_trans_t *tp, | |
478 | xfs_ino_t ino, | |
479 | struct xfs_inode **ipp, | |
480 | int flags, | |
481 | int lock_flags) | |
482 | { | |
483 | struct xfs_inode *ip; | |
484 | int error; | |
485 | xfs_agino_t agino = XFS_INO_TO_AGINO(mp, ino); | |
486 | int iflags; | |
487 | ||
488 | ip = xfs_inode_alloc(mp, ino); | |
489 | if (!ip) | |
2451337d | 490 | return -ENOMEM; |
33479e05 | 491 | |
bb8a66af | 492 | error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, flags); |
33479e05 DC |
493 | if (error) |
494 | goto out_destroy; | |
495 | ||
bb8a66af CH |
496 | /* |
497 | * For version 5 superblocks, if we are initialising a new inode and we | |
498 | * are not utilising the XFS_MOUNT_IKEEP inode cluster mode, we can | |
499 | * simply build the new inode core with a random generation number. | |
500 | * | |
501 | * For version 4 (and older) superblocks, log recovery is dependent on | |
965e0a1a | 502 | * the i_flushiter field being initialised from the current on-disk |
bb8a66af CH |
503 | * value and hence we must also read the inode off disk even when |
504 | * initializing new inodes. | |
505 | */ | |
506 | if (xfs_sb_version_has_v3inode(&mp->m_sb) && | |
507 | (flags & XFS_IGET_CREATE) && !(mp->m_flags & XFS_MOUNT_IKEEP)) { | |
508 | VFS_I(ip)->i_generation = prandom_u32(); | |
509 | } else { | |
bb8a66af CH |
510 | struct xfs_buf *bp; |
511 | ||
af9dcdde | 512 | error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp); |
bb8a66af CH |
513 | if (error) |
514 | goto out_destroy; | |
515 | ||
af9dcdde CH |
516 | error = xfs_inode_from_disk(ip, |
517 | xfs_buf_offset(bp, ip->i_imap.im_boffset)); | |
bb8a66af CH |
518 | if (!error) |
519 | xfs_buf_set_ref(bp, XFS_INO_REF); | |
520 | xfs_trans_brelse(tp, bp); | |
521 | ||
522 | if (error) | |
523 | goto out_destroy; | |
524 | } | |
525 | ||
33479e05 DC |
526 | trace_xfs_iget_miss(ip); |
527 | ||
ee457001 | 528 | /* |
afca6c5b DC |
529 | * Check the inode free state is valid. This also detects lookup |
530 | * racing with unlinks. | |
ee457001 | 531 | */ |
afca6c5b DC |
532 | error = xfs_iget_check_free_state(ip, flags); |
533 | if (error) | |
33479e05 | 534 | goto out_destroy; |
33479e05 DC |
535 | |
536 | /* | |
537 | * Preload the radix tree so we can insert safely under the | |
538 | * write spinlock. Note that we cannot sleep inside the preload | |
539 | * region. Since we can be called from transaction context, don't | |
540 | * recurse into the file system. | |
541 | */ | |
542 | if (radix_tree_preload(GFP_NOFS)) { | |
2451337d | 543 | error = -EAGAIN; |
33479e05 DC |
544 | goto out_destroy; |
545 | } | |
546 | ||
547 | /* | |
548 | * Because the inode hasn't been added to the radix-tree yet it can't | |
549 | * be found by another thread, so we can do the non-sleeping lock here. | |
550 | */ | |
551 | if (lock_flags) { | |
552 | if (!xfs_ilock_nowait(ip, lock_flags)) | |
553 | BUG(); | |
554 | } | |
555 | ||
556 | /* | |
557 | * These values must be set before inserting the inode into the radix | |
558 | * tree as the moment it is inserted a concurrent lookup (allowed by the | |
559 | * RCU locking mechanism) can find it and that lookup must see that this | |
560 | * is an inode currently under construction (i.e. that XFS_INEW is set). | |
561 | * The ip->i_flags_lock that protects the XFS_INEW flag forms the | |
562 | * memory barrier that ensures this detection works correctly at lookup | |
563 | * time. | |
564 | */ | |
565 | iflags = XFS_INEW; | |
566 | if (flags & XFS_IGET_DONTCACHE) | |
2c567af4 | 567 | d_mark_dontcache(VFS_I(ip)); |
113a5683 CS |
568 | ip->i_udquot = NULL; |
569 | ip->i_gdquot = NULL; | |
92f8ff73 | 570 | ip->i_pdquot = NULL; |
33479e05 DC |
571 | xfs_iflags_set(ip, iflags); |
572 | ||
573 | /* insert the new inode */ | |
574 | spin_lock(&pag->pag_ici_lock); | |
575 | error = radix_tree_insert(&pag->pag_ici_root, agino, ip); | |
576 | if (unlikely(error)) { | |
577 | WARN_ON(error != -EEXIST); | |
ff6d6af2 | 578 | XFS_STATS_INC(mp, xs_ig_dup); |
2451337d | 579 | error = -EAGAIN; |
33479e05 DC |
580 | goto out_preload_end; |
581 | } | |
582 | spin_unlock(&pag->pag_ici_lock); | |
583 | radix_tree_preload_end(); | |
584 | ||
585 | *ipp = ip; | |
586 | return 0; | |
587 | ||
588 | out_preload_end: | |
589 | spin_unlock(&pag->pag_ici_lock); | |
590 | radix_tree_preload_end(); | |
591 | if (lock_flags) | |
592 | xfs_iunlock(ip, lock_flags); | |
593 | out_destroy: | |
594 | __destroy_inode(VFS_I(ip)); | |
595 | xfs_inode_free(ip); | |
596 | return error; | |
597 | } | |
598 | ||
599 | /* | |
02511a5a DC |
600 | * Look up an inode by number in the given file system. The inode is looked up |
601 | * in the cache held in each AG. If the inode is found in the cache, initialise | |
602 | * the vfs inode if necessary. | |
33479e05 | 603 | * |
02511a5a DC |
604 | * If it is not in core, read it in from the file system's device, add it to the |
605 | * cache and initialise the vfs inode. | |
33479e05 DC |
606 | * |
607 | * The inode is locked according to the value of the lock_flags parameter. | |
02511a5a DC |
608 | * Inode lookup is only done during metadata operations and not as part of the |
609 | * data IO path. Hence we only allow locking of the XFS_ILOCK during lookup. | |
33479e05 DC |
610 | */ |
611 | int | |
612 | xfs_iget( | |
02511a5a DC |
613 | struct xfs_mount *mp, |
614 | struct xfs_trans *tp, | |
615 | xfs_ino_t ino, | |
616 | uint flags, | |
617 | uint lock_flags, | |
618 | struct xfs_inode **ipp) | |
33479e05 | 619 | { |
02511a5a DC |
620 | struct xfs_inode *ip; |
621 | struct xfs_perag *pag; | |
622 | xfs_agino_t agino; | |
623 | int error; | |
33479e05 | 624 | |
33479e05 DC |
625 | ASSERT((lock_flags & (XFS_IOLOCK_EXCL | XFS_IOLOCK_SHARED)) == 0); |
626 | ||
627 | /* reject inode numbers outside existing AGs */ | |
628 | if (!ino || XFS_INO_TO_AGNO(mp, ino) >= mp->m_sb.sb_agcount) | |
2451337d | 629 | return -EINVAL; |
33479e05 | 630 | |
ff6d6af2 | 631 | XFS_STATS_INC(mp, xs_ig_attempts); |
8774cf8b | 632 | |
33479e05 DC |
633 | /* get the perag structure and ensure that it's inode capable */ |
634 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ino)); | |
635 | agino = XFS_INO_TO_AGINO(mp, ino); | |
636 | ||
637 | again: | |
638 | error = 0; | |
639 | rcu_read_lock(); | |
640 | ip = radix_tree_lookup(&pag->pag_ici_root, agino); | |
641 | ||
642 | if (ip) { | |
643 | error = xfs_iget_cache_hit(pag, ip, ino, flags, lock_flags); | |
644 | if (error) | |
645 | goto out_error_or_again; | |
646 | } else { | |
647 | rcu_read_unlock(); | |
378f681c | 648 | if (flags & XFS_IGET_INCORE) { |
ed438b47 | 649 | error = -ENODATA; |
378f681c DW |
650 | goto out_error_or_again; |
651 | } | |
ff6d6af2 | 652 | XFS_STATS_INC(mp, xs_ig_missed); |
33479e05 DC |
653 | |
654 | error = xfs_iget_cache_miss(mp, pag, tp, ino, &ip, | |
655 | flags, lock_flags); | |
656 | if (error) | |
657 | goto out_error_or_again; | |
658 | } | |
659 | xfs_perag_put(pag); | |
660 | ||
661 | *ipp = ip; | |
662 | ||
663 | /* | |
58c90473 | 664 | * If we have a real type for an on-disk inode, we can setup the inode |
33479e05 DC |
665 | * now. If it's a new inode being created, xfs_ialloc will handle it. |
666 | */ | |
c19b3b05 | 667 | if (xfs_iflags_test(ip, XFS_INEW) && VFS_I(ip)->i_mode != 0) |
58c90473 | 668 | xfs_setup_existing_inode(ip); |
33479e05 DC |
669 | return 0; |
670 | ||
671 | out_error_or_again: | |
378f681c | 672 | if (!(flags & XFS_IGET_INCORE) && error == -EAGAIN) { |
33479e05 DC |
673 | delay(1); |
674 | goto again; | |
675 | } | |
676 | xfs_perag_put(pag); | |
677 | return error; | |
678 | } | |
679 | ||
378f681c DW |
680 | /* |
681 | * "Is this a cached inode that's also allocated?" | |
682 | * | |
683 | * Look up an inode by number in the given file system. If the inode is | |
684 | * in cache and isn't in purgatory, return 1 if the inode is allocated | |
685 | * and 0 if it is not. For all other cases (not in cache, being torn | |
686 | * down, etc.), return a negative error code. | |
687 | * | |
688 | * The caller has to prevent inode allocation and freeing activity, | |
689 | * presumably by locking the AGI buffer. This is to ensure that an | |
690 | * inode cannot transition from allocated to freed until the caller is | |
691 | * ready to allow that. If the inode is in an intermediate state (new, | |
692 | * reclaimable, or being reclaimed), -EAGAIN will be returned; if the | |
693 | * inode is not in the cache, -ENOENT will be returned. The caller must | |
694 | * deal with these scenarios appropriately. | |
695 | * | |
696 | * This is a specialized use case for the online scrubber; if you're | |
697 | * reading this, you probably want xfs_iget. | |
698 | */ | |
699 | int | |
700 | xfs_icache_inode_is_allocated( | |
701 | struct xfs_mount *mp, | |
702 | struct xfs_trans *tp, | |
703 | xfs_ino_t ino, | |
704 | bool *inuse) | |
705 | { | |
706 | struct xfs_inode *ip; | |
707 | int error; | |
708 | ||
709 | error = xfs_iget(mp, tp, ino, XFS_IGET_INCORE, 0, &ip); | |
710 | if (error) | |
711 | return error; | |
712 | ||
713 | *inuse = !!(VFS_I(ip)->i_mode); | |
44a8736b | 714 | xfs_irele(ip); |
378f681c DW |
715 | return 0; |
716 | } | |
717 | ||
78ae5256 DC |
718 | /* |
719 | * The inode lookup is done in batches to keep the amount of lock traffic and | |
720 | * radix tree lookups to a minimum. The batch size is a trade off between | |
721 | * lookup reduction and stack usage. This is in the reclaim path, so we can't | |
722 | * be too greedy. | |
723 | */ | |
724 | #define XFS_LOOKUP_BATCH 32 | |
725 | ||
39b1cfd7 DW |
726 | /* |
727 | * Decide if the given @ip is eligible to be a part of the inode walk, and | |
728 | * grab it if so. Returns true if it's ready to go or false if we should just | |
729 | * ignore it. | |
730 | */ | |
731 | STATIC bool | |
042f65f4 | 732 | xfs_inode_walk_ag_grab( |
ae2c4ac2 BF |
733 | struct xfs_inode *ip, |
734 | int flags) | |
e13de955 DC |
735 | { |
736 | struct inode *inode = VFS_I(ip); | |
042f65f4 | 737 | bool newinos = !!(flags & XFS_INODE_WALK_INEW_WAIT); |
e13de955 | 738 | |
1a3e8f3d DC |
739 | ASSERT(rcu_read_lock_held()); |
740 | ||
02511a5a | 741 | /* Check for stale RCU freed inode */ |
1a3e8f3d DC |
742 | spin_lock(&ip->i_flags_lock); |
743 | if (!ip->i_ino) | |
744 | goto out_unlock_noent; | |
745 | ||
746 | /* avoid new or reclaimable inodes. Leave for reclaim code to flush */ | |
ae2c4ac2 BF |
747 | if ((!newinos && __xfs_iflags_test(ip, XFS_INEW)) || |
748 | __xfs_iflags_test(ip, XFS_IRECLAIMABLE | XFS_IRECLAIM)) | |
1a3e8f3d DC |
749 | goto out_unlock_noent; |
750 | spin_unlock(&ip->i_flags_lock); | |
751 | ||
e13de955 DC |
752 | /* nothing to sync during shutdown */ |
753 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | |
39b1cfd7 | 754 | return false; |
e13de955 | 755 | |
e13de955 DC |
756 | /* If we can't grab the inode, it must on it's way to reclaim. */ |
757 | if (!igrab(inode)) | |
39b1cfd7 | 758 | return false; |
e13de955 | 759 | |
e13de955 | 760 | /* inode is valid */ |
39b1cfd7 | 761 | return true; |
1a3e8f3d DC |
762 | |
763 | out_unlock_noent: | |
764 | spin_unlock(&ip->i_flags_lock); | |
39b1cfd7 | 765 | return false; |
e13de955 DC |
766 | } |
767 | ||
5662d38c DW |
768 | /* |
769 | * For a given per-AG structure @pag, grab, @execute, and rele all incore | |
770 | * inodes with the given radix tree @tag. | |
771 | */ | |
75f3cb13 | 772 | STATIC int |
042f65f4 | 773 | xfs_inode_walk_ag( |
5017e97d | 774 | struct xfs_perag *pag, |
964176bd | 775 | int iter_flags, |
390600f8 | 776 | int (*execute)(struct xfs_inode *ip, void *args), |
a454f742 | 777 | void *args, |
964176bd | 778 | int tag) |
75f3cb13 | 779 | { |
964176bd | 780 | struct xfs_mount *mp = pag->pag_mount; |
75f3cb13 DC |
781 | uint32_t first_index; |
782 | int last_error = 0; | |
783 | int skipped; | |
7e88d314 | 784 | bool done; |
78ae5256 | 785 | int nr_found; |
75f3cb13 DC |
786 | |
787 | restart: | |
7e88d314 | 788 | done = false; |
75f3cb13 DC |
789 | skipped = 0; |
790 | first_index = 0; | |
78ae5256 | 791 | nr_found = 0; |
75f3cb13 | 792 | do { |
78ae5256 | 793 | struct xfs_inode *batch[XFS_LOOKUP_BATCH]; |
75f3cb13 | 794 | int error = 0; |
78ae5256 | 795 | int i; |
75f3cb13 | 796 | |
1a3e8f3d | 797 | rcu_read_lock(); |
a454f742 | 798 | |
fc96be95 | 799 | if (tag == XFS_ICI_NO_TAG) |
a454f742 | 800 | nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, |
78ae5256 DC |
801 | (void **)batch, first_index, |
802 | XFS_LOOKUP_BATCH); | |
a454f742 BF |
803 | else |
804 | nr_found = radix_tree_gang_lookup_tag( | |
805 | &pag->pag_ici_root, | |
806 | (void **) batch, first_index, | |
807 | XFS_LOOKUP_BATCH, tag); | |
808 | ||
65d0f205 | 809 | if (!nr_found) { |
1a3e8f3d | 810 | rcu_read_unlock(); |
75f3cb13 | 811 | break; |
c8e20be0 | 812 | } |
75f3cb13 | 813 | |
65d0f205 | 814 | /* |
78ae5256 DC |
815 | * Grab the inodes before we drop the lock. if we found |
816 | * nothing, nr == 0 and the loop will be skipped. | |
65d0f205 | 817 | */ |
78ae5256 DC |
818 | for (i = 0; i < nr_found; i++) { |
819 | struct xfs_inode *ip = batch[i]; | |
820 | ||
042f65f4 | 821 | if (done || !xfs_inode_walk_ag_grab(ip, iter_flags)) |
78ae5256 DC |
822 | batch[i] = NULL; |
823 | ||
824 | /* | |
1a3e8f3d DC |
825 | * Update the index for the next lookup. Catch |
826 | * overflows into the next AG range which can occur if | |
827 | * we have inodes in the last block of the AG and we | |
828 | * are currently pointing to the last inode. | |
829 | * | |
830 | * Because we may see inodes that are from the wrong AG | |
831 | * due to RCU freeing and reallocation, only update the | |
832 | * index if it lies in this AG. It was a race that lead | |
833 | * us to see this inode, so another lookup from the | |
834 | * same index will not find it again. | |
78ae5256 | 835 | */ |
1a3e8f3d DC |
836 | if (XFS_INO_TO_AGNO(mp, ip->i_ino) != pag->pag_agno) |
837 | continue; | |
78ae5256 DC |
838 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); |
839 | if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) | |
7e88d314 | 840 | done = true; |
e13de955 | 841 | } |
78ae5256 DC |
842 | |
843 | /* unlock now we've grabbed the inodes. */ | |
1a3e8f3d | 844 | rcu_read_unlock(); |
e13de955 | 845 | |
78ae5256 DC |
846 | for (i = 0; i < nr_found; i++) { |
847 | if (!batch[i]) | |
848 | continue; | |
042f65f4 | 849 | if ((iter_flags & XFS_INODE_WALK_INEW_WAIT) && |
ae2c4ac2 BF |
850 | xfs_iflags_test(batch[i], XFS_INEW)) |
851 | xfs_inew_wait(batch[i]); | |
390600f8 | 852 | error = execute(batch[i], args); |
44a8736b | 853 | xfs_irele(batch[i]); |
2451337d | 854 | if (error == -EAGAIN) { |
78ae5256 DC |
855 | skipped++; |
856 | continue; | |
857 | } | |
2451337d | 858 | if (error && last_error != -EFSCORRUPTED) |
78ae5256 | 859 | last_error = error; |
75f3cb13 | 860 | } |
c8e20be0 DC |
861 | |
862 | /* bail out if the filesystem is corrupted. */ | |
2451337d | 863 | if (error == -EFSCORRUPTED) |
75f3cb13 DC |
864 | break; |
865 | ||
8daaa831 DC |
866 | cond_resched(); |
867 | ||
78ae5256 | 868 | } while (nr_found && !done); |
75f3cb13 DC |
869 | |
870 | if (skipped) { | |
871 | delay(1); | |
872 | goto restart; | |
873 | } | |
75f3cb13 DC |
874 | return last_error; |
875 | } | |
876 | ||
5662d38c DW |
877 | /* Fetch the next (possibly tagged) per-AG structure. */ |
878 | static inline struct xfs_perag * | |
879 | xfs_inode_walk_get_perag( | |
880 | struct xfs_mount *mp, | |
881 | xfs_agnumber_t agno, | |
882 | int tag) | |
883 | { | |
884 | if (tag == XFS_ICI_NO_TAG) | |
885 | return xfs_perag_get(mp, agno); | |
886 | return xfs_perag_get_tag(mp, agno, tag); | |
887 | } | |
888 | ||
889 | /* | |
890 | * Call the @execute function on all incore inodes matching the radix tree | |
891 | * @tag. | |
892 | */ | |
893 | int | |
042f65f4 | 894 | xfs_inode_walk( |
5662d38c DW |
895 | struct xfs_mount *mp, |
896 | int iter_flags, | |
897 | int (*execute)(struct xfs_inode *ip, void *args), | |
898 | void *args, | |
899 | int tag) | |
900 | { | |
901 | struct xfs_perag *pag; | |
902 | int error = 0; | |
903 | int last_error = 0; | |
904 | xfs_agnumber_t ag; | |
905 | ||
906 | ag = 0; | |
907 | while ((pag = xfs_inode_walk_get_perag(mp, ag, tag))) { | |
908 | ag = pag->pag_agno + 1; | |
964176bd | 909 | error = xfs_inode_walk_ag(pag, iter_flags, execute, args, tag); |
5662d38c DW |
910 | xfs_perag_put(pag); |
911 | if (error) { | |
912 | last_error = error; | |
913 | if (error == -EFSCORRUPTED) | |
914 | break; | |
915 | } | |
916 | } | |
917 | return last_error; | |
918 | } | |
919 | ||
e3a20c0b DC |
920 | /* |
921 | * Grab the inode for reclaim exclusively. | |
50718b8d DC |
922 | * |
923 | * We have found this inode via a lookup under RCU, so the inode may have | |
924 | * already been freed, or it may be in the process of being recycled by | |
925 | * xfs_iget(). In both cases, the inode will have XFS_IRECLAIM set. If the inode | |
926 | * has been fully recycled by the time we get the i_flags_lock, XFS_IRECLAIMABLE | |
927 | * will not be set. Hence we need to check for both these flag conditions to | |
928 | * avoid inodes that are no longer reclaim candidates. | |
929 | * | |
930 | * Note: checking for other state flags here, under the i_flags_lock or not, is | |
931 | * racy and should be avoided. Those races should be resolved only after we have | |
932 | * ensured that we are able to reclaim this inode and the world can see that we | |
933 | * are going to reclaim it. | |
934 | * | |
935 | * Return true if we grabbed it, false otherwise. | |
e3a20c0b | 936 | */ |
50718b8d | 937 | static bool |
e3a20c0b | 938 | xfs_reclaim_inode_grab( |
50718b8d | 939 | struct xfs_inode *ip) |
e3a20c0b | 940 | { |
1a3e8f3d DC |
941 | ASSERT(rcu_read_lock_held()); |
942 | ||
e3a20c0b | 943 | spin_lock(&ip->i_flags_lock); |
1a3e8f3d DC |
944 | if (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) || |
945 | __xfs_iflags_test(ip, XFS_IRECLAIM)) { | |
946 | /* not a reclaim candidate. */ | |
e3a20c0b | 947 | spin_unlock(&ip->i_flags_lock); |
50718b8d | 948 | return false; |
e3a20c0b DC |
949 | } |
950 | __xfs_iflags_set(ip, XFS_IRECLAIM); | |
951 | spin_unlock(&ip->i_flags_lock); | |
50718b8d | 952 | return true; |
e3a20c0b DC |
953 | } |
954 | ||
777df5af | 955 | /* |
02511a5a DC |
956 | * Inode reclaim is non-blocking, so the default action if progress cannot be |
957 | * made is to "requeue" the inode for reclaim by unlocking it and clearing the | |
958 | * XFS_IRECLAIM flag. If we are in a shutdown state, we don't care about | |
959 | * blocking anymore and hence we can wait for the inode to be able to reclaim | |
960 | * it. | |
777df5af | 961 | * |
02511a5a DC |
962 | * We do no IO here - if callers require inodes to be cleaned they must push the |
963 | * AIL first to trigger writeback of dirty inodes. This enables writeback to be | |
964 | * done in the background in a non-blocking manner, and enables memory reclaim | |
965 | * to make progress without blocking. | |
777df5af | 966 | */ |
4d0bab3a | 967 | static void |
c8e20be0 | 968 | xfs_reclaim_inode( |
75f3cb13 | 969 | struct xfs_inode *ip, |
50718b8d | 970 | struct xfs_perag *pag) |
fce08f2f | 971 | { |
8a17d7dd | 972 | xfs_ino_t ino = ip->i_ino; /* for radix_tree_delete */ |
777df5af | 973 | |
9552e14d | 974 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) |
617825fe | 975 | goto out; |
718ecc50 | 976 | if (xfs_iflags_test_and_set(ip, XFS_IFLUSHING)) |
9552e14d | 977 | goto out_iunlock; |
7a3be02b | 978 | |
777df5af DC |
979 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
980 | xfs_iunpin_wait(ip); | |
88fc1879 | 981 | xfs_iflush_abort(ip); |
777df5af DC |
982 | goto reclaim; |
983 | } | |
617825fe | 984 | if (xfs_ipincount(ip)) |
718ecc50 | 985 | goto out_clear_flush; |
617825fe | 986 | if (!xfs_inode_clean(ip)) |
718ecc50 | 987 | goto out_clear_flush; |
8a48088f | 988 | |
718ecc50 | 989 | xfs_iflags_clear(ip, XFS_IFLUSHING); |
777df5af | 990 | reclaim: |
98efe8af | 991 | |
8a17d7dd DC |
992 | /* |
993 | * Because we use RCU freeing we need to ensure the inode always appears | |
994 | * to be reclaimed with an invalid inode number when in the free state. | |
98efe8af | 995 | * We do this as early as possible under the ILOCK so that |
f2e9ad21 OS |
996 | * xfs_iflush_cluster() and xfs_ifree_cluster() can be guaranteed to |
997 | * detect races with us here. By doing this, we guarantee that once | |
998 | * xfs_iflush_cluster() or xfs_ifree_cluster() has locked XFS_ILOCK that | |
999 | * it will see either a valid inode that will serialise correctly, or it | |
1000 | * will see an invalid inode that it can skip. | |
8a17d7dd DC |
1001 | */ |
1002 | spin_lock(&ip->i_flags_lock); | |
1003 | ip->i_flags = XFS_IRECLAIM; | |
1004 | ip->i_ino = 0; | |
1005 | spin_unlock(&ip->i_flags_lock); | |
1006 | ||
c8e20be0 | 1007 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
2f11feab | 1008 | |
ff6d6af2 | 1009 | XFS_STATS_INC(ip->i_mount, xs_ig_reclaims); |
2f11feab DC |
1010 | /* |
1011 | * Remove the inode from the per-AG radix tree. | |
1012 | * | |
1013 | * Because radix_tree_delete won't complain even if the item was never | |
1014 | * added to the tree assert that it's been there before to catch | |
1015 | * problems with the inode life time early on. | |
1016 | */ | |
1a427ab0 | 1017 | spin_lock(&pag->pag_ici_lock); |
2f11feab | 1018 | if (!radix_tree_delete(&pag->pag_ici_root, |
8a17d7dd | 1019 | XFS_INO_TO_AGINO(ip->i_mount, ino))) |
2f11feab | 1020 | ASSERT(0); |
545c0889 | 1021 | xfs_perag_clear_reclaim_tag(pag); |
1a427ab0 | 1022 | spin_unlock(&pag->pag_ici_lock); |
2f11feab DC |
1023 | |
1024 | /* | |
1025 | * Here we do an (almost) spurious inode lock in order to coordinate | |
1026 | * with inode cache radix tree lookups. This is because the lookup | |
1027 | * can reference the inodes in the cache without taking references. | |
1028 | * | |
1029 | * We make that OK here by ensuring that we wait until the inode is | |
ad637a10 | 1030 | * unlocked after the lookup before we go ahead and free it. |
2f11feab | 1031 | */ |
ad637a10 | 1032 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
2f11feab | 1033 | xfs_qm_dqdetach(ip); |
ad637a10 | 1034 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
96355d5a | 1035 | ASSERT(xfs_inode_clean(ip)); |
2f11feab | 1036 | |
8a17d7dd | 1037 | __xfs_inode_free(ip); |
4d0bab3a | 1038 | return; |
8a48088f | 1039 | |
718ecc50 DC |
1040 | out_clear_flush: |
1041 | xfs_iflags_clear(ip, XFS_IFLUSHING); | |
9552e14d | 1042 | out_iunlock: |
8a48088f | 1043 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
9552e14d | 1044 | out: |
617825fe | 1045 | xfs_iflags_clear(ip, XFS_IRECLAIM); |
7a3be02b DC |
1046 | } |
1047 | ||
65d0f205 DC |
1048 | /* |
1049 | * Walk the AGs and reclaim the inodes in them. Even if the filesystem is | |
1050 | * corrupted, we still want to try to reclaim all the inodes. If we don't, | |
1051 | * then a shut down during filesystem unmount reclaim walk leak all the | |
1052 | * unreclaimed inodes. | |
617825fe DC |
1053 | * |
1054 | * Returns non-zero if any AGs or inodes were skipped in the reclaim pass | |
1055 | * so that callers that want to block until all dirty inodes are written back | |
1056 | * and reclaimed can sanely loop. | |
65d0f205 | 1057 | */ |
4d0bab3a | 1058 | static void |
65d0f205 DC |
1059 | xfs_reclaim_inodes_ag( |
1060 | struct xfs_mount *mp, | |
65d0f205 DC |
1061 | int *nr_to_scan) |
1062 | { | |
1063 | struct xfs_perag *pag; | |
0e8e2c63 | 1064 | xfs_agnumber_t ag = 0; |
65d0f205 | 1065 | |
65d0f205 DC |
1066 | while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { |
1067 | unsigned long first_index = 0; | |
1068 | int done = 0; | |
e3a20c0b | 1069 | int nr_found = 0; |
65d0f205 DC |
1070 | |
1071 | ag = pag->pag_agno + 1; | |
1072 | ||
0e8e2c63 | 1073 | first_index = READ_ONCE(pag->pag_ici_reclaim_cursor); |
65d0f205 | 1074 | do { |
e3a20c0b DC |
1075 | struct xfs_inode *batch[XFS_LOOKUP_BATCH]; |
1076 | int i; | |
65d0f205 | 1077 | |
1a3e8f3d | 1078 | rcu_read_lock(); |
e3a20c0b DC |
1079 | nr_found = radix_tree_gang_lookup_tag( |
1080 | &pag->pag_ici_root, | |
1081 | (void **)batch, first_index, | |
1082 | XFS_LOOKUP_BATCH, | |
65d0f205 DC |
1083 | XFS_ICI_RECLAIM_TAG); |
1084 | if (!nr_found) { | |
b2232219 | 1085 | done = 1; |
1a3e8f3d | 1086 | rcu_read_unlock(); |
65d0f205 DC |
1087 | break; |
1088 | } | |
1089 | ||
1090 | /* | |
e3a20c0b DC |
1091 | * Grab the inodes before we drop the lock. if we found |
1092 | * nothing, nr == 0 and the loop will be skipped. | |
65d0f205 | 1093 | */ |
e3a20c0b DC |
1094 | for (i = 0; i < nr_found; i++) { |
1095 | struct xfs_inode *ip = batch[i]; | |
1096 | ||
50718b8d | 1097 | if (done || !xfs_reclaim_inode_grab(ip)) |
e3a20c0b DC |
1098 | batch[i] = NULL; |
1099 | ||
1100 | /* | |
1101 | * Update the index for the next lookup. Catch | |
1102 | * overflows into the next AG range which can | |
1103 | * occur if we have inodes in the last block of | |
1104 | * the AG and we are currently pointing to the | |
1105 | * last inode. | |
1a3e8f3d DC |
1106 | * |
1107 | * Because we may see inodes that are from the | |
1108 | * wrong AG due to RCU freeing and | |
1109 | * reallocation, only update the index if it | |
1110 | * lies in this AG. It was a race that lead us | |
1111 | * to see this inode, so another lookup from | |
1112 | * the same index will not find it again. | |
e3a20c0b | 1113 | */ |
1a3e8f3d DC |
1114 | if (XFS_INO_TO_AGNO(mp, ip->i_ino) != |
1115 | pag->pag_agno) | |
1116 | continue; | |
e3a20c0b DC |
1117 | first_index = XFS_INO_TO_AGINO(mp, ip->i_ino + 1); |
1118 | if (first_index < XFS_INO_TO_AGINO(mp, ip->i_ino)) | |
1119 | done = 1; | |
1120 | } | |
65d0f205 | 1121 | |
e3a20c0b | 1122 | /* unlock now we've grabbed the inodes. */ |
1a3e8f3d | 1123 | rcu_read_unlock(); |
e3a20c0b DC |
1124 | |
1125 | for (i = 0; i < nr_found; i++) { | |
4d0bab3a DC |
1126 | if (batch[i]) |
1127 | xfs_reclaim_inode(batch[i], pag); | |
e3a20c0b DC |
1128 | } |
1129 | ||
1130 | *nr_to_scan -= XFS_LOOKUP_BATCH; | |
8daaa831 | 1131 | cond_resched(); |
e3a20c0b | 1132 | } while (nr_found && !done && *nr_to_scan > 0); |
65d0f205 | 1133 | |
0e8e2c63 DC |
1134 | if (done) |
1135 | first_index = 0; | |
1136 | WRITE_ONCE(pag->pag_ici_reclaim_cursor, first_index); | |
65d0f205 DC |
1137 | xfs_perag_put(pag); |
1138 | } | |
65d0f205 DC |
1139 | } |
1140 | ||
4d0bab3a | 1141 | void |
7a3be02b | 1142 | xfs_reclaim_inodes( |
4d0bab3a | 1143 | struct xfs_mount *mp) |
7a3be02b | 1144 | { |
65d0f205 DC |
1145 | int nr_to_scan = INT_MAX; |
1146 | ||
4d0bab3a | 1147 | while (radix_tree_tagged(&mp->m_perag_tree, XFS_ICI_RECLAIM_TAG)) { |
617825fe | 1148 | xfs_ail_push_all_sync(mp->m_ail); |
4d0bab3a | 1149 | xfs_reclaim_inodes_ag(mp, &nr_to_scan); |
0f4ec0f1 | 1150 | } |
9bf729c0 DC |
1151 | } |
1152 | ||
1153 | /* | |
02511a5a DC |
1154 | * The shrinker infrastructure determines how many inodes we should scan for |
1155 | * reclaim. We want as many clean inodes ready to reclaim as possible, so we | |
1156 | * push the AIL here. We also want to proactively free up memory if we can to | |
1157 | * minimise the amount of work memory reclaim has to do so we kick the | |
1158 | * background reclaim if it isn't already scheduled. | |
9bf729c0 | 1159 | */ |
0a234c6d | 1160 | long |
8daaa831 DC |
1161 | xfs_reclaim_inodes_nr( |
1162 | struct xfs_mount *mp, | |
1163 | int nr_to_scan) | |
9bf729c0 | 1164 | { |
8daaa831 | 1165 | /* kick background reclaimer and push the AIL */ |
5889608d | 1166 | xfs_reclaim_work_queue(mp); |
8daaa831 | 1167 | xfs_ail_push_all(mp->m_ail); |
a7b339f1 | 1168 | |
50718b8d | 1169 | xfs_reclaim_inodes_ag(mp, &nr_to_scan); |
617825fe | 1170 | return 0; |
8daaa831 | 1171 | } |
9bf729c0 | 1172 | |
8daaa831 DC |
1173 | /* |
1174 | * Return the number of reclaimable inodes in the filesystem for | |
1175 | * the shrinker to determine how much to reclaim. | |
1176 | */ | |
1177 | int | |
1178 | xfs_reclaim_inodes_count( | |
1179 | struct xfs_mount *mp) | |
1180 | { | |
1181 | struct xfs_perag *pag; | |
1182 | xfs_agnumber_t ag = 0; | |
1183 | int reclaimable = 0; | |
9bf729c0 | 1184 | |
65d0f205 DC |
1185 | while ((pag = xfs_perag_get_tag(mp, ag, XFS_ICI_RECLAIM_TAG))) { |
1186 | ag = pag->pag_agno + 1; | |
70e60ce7 DC |
1187 | reclaimable += pag->pag_ici_reclaimable; |
1188 | xfs_perag_put(pag); | |
9bf729c0 | 1189 | } |
9bf729c0 DC |
1190 | return reclaimable; |
1191 | } | |
1192 | ||
39b1cfd7 | 1193 | STATIC bool |
3e3f9f58 BF |
1194 | xfs_inode_match_id( |
1195 | struct xfs_inode *ip, | |
1196 | struct xfs_eofblocks *eofb) | |
1197 | { | |
b9fe5052 DE |
1198 | if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) && |
1199 | !uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid)) | |
39b1cfd7 | 1200 | return false; |
3e3f9f58 | 1201 | |
b9fe5052 DE |
1202 | if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) && |
1203 | !gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid)) | |
39b1cfd7 | 1204 | return false; |
1b556048 | 1205 | |
b9fe5052 | 1206 | if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) && |
ceaf603c | 1207 | ip->i_projid != eofb->eof_prid) |
39b1cfd7 | 1208 | return false; |
1b556048 | 1209 | |
39b1cfd7 | 1210 | return true; |
3e3f9f58 BF |
1211 | } |
1212 | ||
f4526397 BF |
1213 | /* |
1214 | * A union-based inode filtering algorithm. Process the inode if any of the | |
1215 | * criteria match. This is for global/internal scans only. | |
1216 | */ | |
39b1cfd7 | 1217 | STATIC bool |
f4526397 BF |
1218 | xfs_inode_match_id_union( |
1219 | struct xfs_inode *ip, | |
1220 | struct xfs_eofblocks *eofb) | |
1221 | { | |
1222 | if ((eofb->eof_flags & XFS_EOF_FLAGS_UID) && | |
1223 | uid_eq(VFS_I(ip)->i_uid, eofb->eof_uid)) | |
39b1cfd7 | 1224 | return true; |
f4526397 BF |
1225 | |
1226 | if ((eofb->eof_flags & XFS_EOF_FLAGS_GID) && | |
1227 | gid_eq(VFS_I(ip)->i_gid, eofb->eof_gid)) | |
39b1cfd7 | 1228 | return true; |
f4526397 BF |
1229 | |
1230 | if ((eofb->eof_flags & XFS_EOF_FLAGS_PRID) && | |
ceaf603c | 1231 | ip->i_projid == eofb->eof_prid) |
39b1cfd7 | 1232 | return true; |
f4526397 | 1233 | |
39b1cfd7 | 1234 | return false; |
f4526397 BF |
1235 | } |
1236 | ||
a91bf992 DW |
1237 | /* |
1238 | * Is this inode @ip eligible for eof/cow block reclamation, given some | |
1239 | * filtering parameters @eofb? The inode is eligible if @eofb is null or | |
1240 | * if the predicate functions match. | |
1241 | */ | |
1242 | static bool | |
1243 | xfs_inode_matches_eofb( | |
1244 | struct xfs_inode *ip, | |
1245 | struct xfs_eofblocks *eofb) | |
1246 | { | |
39b1cfd7 | 1247 | bool match; |
a91bf992 DW |
1248 | |
1249 | if (!eofb) | |
1250 | return true; | |
1251 | ||
1252 | if (eofb->eof_flags & XFS_EOF_FLAGS_UNION) | |
1253 | match = xfs_inode_match_id_union(ip, eofb); | |
1254 | else | |
1255 | match = xfs_inode_match_id(ip, eofb); | |
1256 | if (!match) | |
1257 | return false; | |
1258 | ||
1259 | /* skip the inode if the file size is too small */ | |
1260 | if ((eofb->eof_flags & XFS_EOF_FLAGS_MINFILESIZE) && | |
1261 | XFS_ISIZE(ip) < eofb->eof_min_file_size) | |
1262 | return false; | |
1263 | ||
1264 | return true; | |
1265 | } | |
1266 | ||
4d0bab3a DC |
1267 | /* |
1268 | * This is a fast pass over the inode cache to try to get reclaim moving on as | |
1269 | * many inodes as possible in a short period of time. It kicks itself every few | |
1270 | * seconds, as well as being kicked by the inode cache shrinker when memory | |
02511a5a | 1271 | * goes low. |
4d0bab3a DC |
1272 | */ |
1273 | void | |
1274 | xfs_reclaim_worker( | |
1275 | struct work_struct *work) | |
1276 | { | |
1277 | struct xfs_mount *mp = container_of(to_delayed_work(work), | |
1278 | struct xfs_mount, m_reclaim_work); | |
1279 | int nr_to_scan = INT_MAX; | |
1280 | ||
1281 | xfs_reclaim_inodes_ag(mp, &nr_to_scan); | |
1282 | xfs_reclaim_work_queue(mp); | |
1283 | } | |
1284 | ||
41176a68 BF |
1285 | STATIC int |
1286 | xfs_inode_free_eofblocks( | |
1287 | struct xfs_inode *ip, | |
0fa4a10a DW |
1288 | void *args, |
1289 | unsigned int *lockflags) | |
41176a68 | 1290 | { |
390600f8 DW |
1291 | struct xfs_eofblocks *eofb = args; |
1292 | bool wait; | |
390600f8 DW |
1293 | |
1294 | wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC); | |
5400da7d | 1295 | |
ce2d3bbe DW |
1296 | if (!xfs_iflags_test(ip, XFS_IEOFBLOCKS)) |
1297 | return 0; | |
1298 | ||
41176a68 BF |
1299 | if (!xfs_can_free_eofblocks(ip, false)) { |
1300 | /* inode could be preallocated or append-only */ | |
1301 | trace_xfs_inode_free_eofblocks_invalid(ip); | |
1302 | xfs_inode_clear_eofblocks_tag(ip); | |
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | /* | |
1307 | * If the mapping is dirty the operation can block and wait for some | |
1308 | * time. Unless we are waiting, skip it. | |
1309 | */ | |
390600f8 | 1310 | if (!wait && mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY)) |
41176a68 BF |
1311 | return 0; |
1312 | ||
a91bf992 DW |
1313 | if (!xfs_inode_matches_eofb(ip, eofb)) |
1314 | return 0; | |
3e3f9f58 | 1315 | |
a36b9261 BF |
1316 | /* |
1317 | * If the caller is waiting, return -EAGAIN to keep the background | |
1318 | * scanner moving and revisit the inode in a subsequent pass. | |
1319 | */ | |
c3155097 | 1320 | if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { |
390600f8 DW |
1321 | if (wait) |
1322 | return -EAGAIN; | |
1323 | return 0; | |
a36b9261 | 1324 | } |
0fa4a10a | 1325 | *lockflags |= XFS_IOLOCK_EXCL; |
390600f8 | 1326 | |
0fa4a10a | 1327 | return xfs_free_eofblocks(ip); |
41176a68 BF |
1328 | } |
1329 | ||
f9296569 | 1330 | /* |
9669f51d DW |
1331 | * Background scanning to trim preallocated space. This is queued based on the |
1332 | * 'speculative_prealloc_lifetime' tunable (5m by default). | |
f9296569 | 1333 | */ |
9669f51d DW |
1334 | static inline void |
1335 | xfs_blockgc_queue( | |
894ecacf | 1336 | struct xfs_perag *pag) |
f9296569 DW |
1337 | { |
1338 | rcu_read_lock(); | |
894ecacf | 1339 | if (radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG)) |
3fef46fc | 1340 | queue_delayed_work(pag->pag_mount->m_gc_workqueue, |
894ecacf | 1341 | &pag->pag_blockgc_work, |
9669f51d | 1342 | msecs_to_jiffies(xfs_blockgc_secs * 1000)); |
f9296569 DW |
1343 | rcu_read_unlock(); |
1344 | } | |
1345 | ||
83104d44 | 1346 | static void |
ce2d3bbe DW |
1347 | xfs_blockgc_set_iflag( |
1348 | struct xfs_inode *ip, | |
ce2d3bbe | 1349 | unsigned long iflag) |
27b52867 | 1350 | { |
ce2d3bbe DW |
1351 | struct xfs_mount *mp = ip->i_mount; |
1352 | struct xfs_perag *pag; | |
1353 | int tagged; | |
1354 | ||
1355 | ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0); | |
27b52867 | 1356 | |
85a6e764 CH |
1357 | /* |
1358 | * Don't bother locking the AG and looking up in the radix trees | |
1359 | * if we already know that we have the tag set. | |
1360 | */ | |
ce2d3bbe | 1361 | if (ip->i_flags & iflag) |
85a6e764 CH |
1362 | return; |
1363 | spin_lock(&ip->i_flags_lock); | |
ce2d3bbe | 1364 | ip->i_flags |= iflag; |
85a6e764 CH |
1365 | spin_unlock(&ip->i_flags_lock); |
1366 | ||
27b52867 BF |
1367 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
1368 | spin_lock(&pag->pag_ici_lock); | |
27b52867 | 1369 | |
ce2d3bbe | 1370 | tagged = radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG); |
27b52867 | 1371 | radix_tree_tag_set(&pag->pag_ici_root, |
ce2d3bbe DW |
1372 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), |
1373 | XFS_ICI_BLOCKGC_TAG); | |
27b52867 | 1374 | if (!tagged) { |
ce2d3bbe | 1375 | /* propagate the blockgc tag up into the perag radix tree */ |
27b52867 BF |
1376 | spin_lock(&ip->i_mount->m_perag_lock); |
1377 | radix_tree_tag_set(&ip->i_mount->m_perag_tree, | |
1378 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | |
ce2d3bbe | 1379 | XFS_ICI_BLOCKGC_TAG); |
27b52867 | 1380 | spin_unlock(&ip->i_mount->m_perag_lock); |
579b62fa BF |
1381 | |
1382 | /* kick off background trimming */ | |
894ecacf | 1383 | xfs_blockgc_queue(pag); |
27b52867 | 1384 | |
ce2d3bbe DW |
1385 | trace_xfs_perag_set_blockgc(ip->i_mount, pag->pag_agno, -1, |
1386 | _RET_IP_); | |
27b52867 BF |
1387 | } |
1388 | ||
1389 | spin_unlock(&pag->pag_ici_lock); | |
1390 | xfs_perag_put(pag); | |
1391 | } | |
1392 | ||
1393 | void | |
83104d44 | 1394 | xfs_inode_set_eofblocks_tag( |
27b52867 | 1395 | xfs_inode_t *ip) |
83104d44 DW |
1396 | { |
1397 | trace_xfs_inode_set_eofblocks_tag(ip); | |
9669f51d | 1398 | return xfs_blockgc_set_iflag(ip, XFS_IEOFBLOCKS); |
83104d44 DW |
1399 | } |
1400 | ||
1401 | static void | |
ce2d3bbe DW |
1402 | xfs_blockgc_clear_iflag( |
1403 | struct xfs_inode *ip, | |
1404 | unsigned long iflag) | |
27b52867 | 1405 | { |
ce2d3bbe DW |
1406 | struct xfs_mount *mp = ip->i_mount; |
1407 | struct xfs_perag *pag; | |
1408 | bool clear_tag; | |
1409 | ||
1410 | ASSERT((iflag & ~(XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0); | |
27b52867 | 1411 | |
85a6e764 | 1412 | spin_lock(&ip->i_flags_lock); |
ce2d3bbe DW |
1413 | ip->i_flags &= ~iflag; |
1414 | clear_tag = (ip->i_flags & (XFS_IEOFBLOCKS | XFS_ICOWBLOCKS)) == 0; | |
85a6e764 CH |
1415 | spin_unlock(&ip->i_flags_lock); |
1416 | ||
ce2d3bbe DW |
1417 | if (!clear_tag) |
1418 | return; | |
1419 | ||
27b52867 BF |
1420 | pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino)); |
1421 | spin_lock(&pag->pag_ici_lock); | |
27b52867 BF |
1422 | |
1423 | radix_tree_tag_clear(&pag->pag_ici_root, | |
ce2d3bbe DW |
1424 | XFS_INO_TO_AGINO(ip->i_mount, ip->i_ino), |
1425 | XFS_ICI_BLOCKGC_TAG); | |
1426 | if (!radix_tree_tagged(&pag->pag_ici_root, XFS_ICI_BLOCKGC_TAG)) { | |
1427 | /* clear the blockgc tag from the perag radix tree */ | |
27b52867 BF |
1428 | spin_lock(&ip->i_mount->m_perag_lock); |
1429 | radix_tree_tag_clear(&ip->i_mount->m_perag_tree, | |
1430 | XFS_INO_TO_AGNO(ip->i_mount, ip->i_ino), | |
ce2d3bbe | 1431 | XFS_ICI_BLOCKGC_TAG); |
27b52867 | 1432 | spin_unlock(&ip->i_mount->m_perag_lock); |
ce2d3bbe DW |
1433 | trace_xfs_perag_clear_blockgc(ip->i_mount, pag->pag_agno, -1, |
1434 | _RET_IP_); | |
27b52867 BF |
1435 | } |
1436 | ||
1437 | spin_unlock(&pag->pag_ici_lock); | |
1438 | xfs_perag_put(pag); | |
1439 | } | |
1440 | ||
83104d44 DW |
1441 | void |
1442 | xfs_inode_clear_eofblocks_tag( | |
1443 | xfs_inode_t *ip) | |
1444 | { | |
1445 | trace_xfs_inode_clear_eofblocks_tag(ip); | |
ce2d3bbe | 1446 | return xfs_blockgc_clear_iflag(ip, XFS_IEOFBLOCKS); |
83104d44 DW |
1447 | } |
1448 | ||
1449 | /* | |
be78ff0e DW |
1450 | * Set ourselves up to free CoW blocks from this file. If it's already clean |
1451 | * then we can bail out quickly, but otherwise we must back off if the file | |
1452 | * is undergoing some kind of write. | |
83104d44 | 1453 | */ |
be78ff0e DW |
1454 | static bool |
1455 | xfs_prep_free_cowblocks( | |
51d62690 | 1456 | struct xfs_inode *ip) |
83104d44 | 1457 | { |
39937234 BF |
1458 | /* |
1459 | * Just clear the tag if we have an empty cow fork or none at all. It's | |
1460 | * possible the inode was fully unshared since it was originally tagged. | |
1461 | */ | |
51d62690 | 1462 | if (!xfs_inode_has_cow_data(ip)) { |
83104d44 DW |
1463 | trace_xfs_inode_free_cowblocks_invalid(ip); |
1464 | xfs_inode_clear_cowblocks_tag(ip); | |
be78ff0e | 1465 | return false; |
83104d44 DW |
1466 | } |
1467 | ||
1468 | /* | |
1469 | * If the mapping is dirty or under writeback we cannot touch the | |
1470 | * CoW fork. Leave it alone if we're in the midst of a directio. | |
1471 | */ | |
a1b7a4de CH |
1472 | if ((VFS_I(ip)->i_state & I_DIRTY_PAGES) || |
1473 | mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_DIRTY) || | |
83104d44 DW |
1474 | mapping_tagged(VFS_I(ip)->i_mapping, PAGECACHE_TAG_WRITEBACK) || |
1475 | atomic_read(&VFS_I(ip)->i_dio_count)) | |
be78ff0e DW |
1476 | return false; |
1477 | ||
1478 | return true; | |
1479 | } | |
1480 | ||
1481 | /* | |
1482 | * Automatic CoW Reservation Freeing | |
1483 | * | |
1484 | * These functions automatically garbage collect leftover CoW reservations | |
1485 | * that were made on behalf of a cowextsize hint when we start to run out | |
1486 | * of quota or when the reservations sit around for too long. If the file | |
1487 | * has dirty pages or is undergoing writeback, its CoW reservations will | |
1488 | * be retained. | |
1489 | * | |
1490 | * The actual garbage collection piggybacks off the same code that runs | |
1491 | * the speculative EOF preallocation garbage collector. | |
1492 | */ | |
1493 | STATIC int | |
1494 | xfs_inode_free_cowblocks( | |
1495 | struct xfs_inode *ip, | |
0fa4a10a DW |
1496 | void *args, |
1497 | unsigned int *lockflags) | |
be78ff0e DW |
1498 | { |
1499 | struct xfs_eofblocks *eofb = args; | |
f41a0716 | 1500 | bool wait; |
be78ff0e DW |
1501 | int ret = 0; |
1502 | ||
f41a0716 DW |
1503 | wait = eofb && (eofb->eof_flags & XFS_EOF_FLAGS_SYNC); |
1504 | ||
ce2d3bbe DW |
1505 | if (!xfs_iflags_test(ip, XFS_ICOWBLOCKS)) |
1506 | return 0; | |
1507 | ||
51d62690 | 1508 | if (!xfs_prep_free_cowblocks(ip)) |
83104d44 DW |
1509 | return 0; |
1510 | ||
a91bf992 DW |
1511 | if (!xfs_inode_matches_eofb(ip, eofb)) |
1512 | return 0; | |
83104d44 | 1513 | |
f41a0716 DW |
1514 | /* |
1515 | * If the caller is waiting, return -EAGAIN to keep the background | |
1516 | * scanner moving and revisit the inode in a subsequent pass. | |
1517 | */ | |
0fa4a10a DW |
1518 | if (!(*lockflags & XFS_IOLOCK_EXCL) && |
1519 | !xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) { | |
f41a0716 DW |
1520 | if (wait) |
1521 | return -EAGAIN; | |
1522 | return 0; | |
1523 | } | |
0fa4a10a DW |
1524 | *lockflags |= XFS_IOLOCK_EXCL; |
1525 | ||
f41a0716 DW |
1526 | if (!xfs_ilock_nowait(ip, XFS_MMAPLOCK_EXCL)) { |
1527 | if (wait) | |
0fa4a10a DW |
1528 | return -EAGAIN; |
1529 | return 0; | |
f41a0716 | 1530 | } |
0fa4a10a | 1531 | *lockflags |= XFS_MMAPLOCK_EXCL; |
83104d44 | 1532 | |
be78ff0e DW |
1533 | /* |
1534 | * Check again, nobody else should be able to dirty blocks or change | |
1535 | * the reflink iflag now that we have the first two locks held. | |
1536 | */ | |
51d62690 | 1537 | if (xfs_prep_free_cowblocks(ip)) |
be78ff0e | 1538 | ret = xfs_reflink_cancel_cow_range(ip, 0, NULLFILEOFF, false); |
83104d44 DW |
1539 | return ret; |
1540 | } | |
1541 | ||
83104d44 DW |
1542 | void |
1543 | xfs_inode_set_cowblocks_tag( | |
1544 | xfs_inode_t *ip) | |
1545 | { | |
7b7381f0 | 1546 | trace_xfs_inode_set_cowblocks_tag(ip); |
9669f51d | 1547 | return xfs_blockgc_set_iflag(ip, XFS_ICOWBLOCKS); |
83104d44 DW |
1548 | } |
1549 | ||
1550 | void | |
1551 | xfs_inode_clear_cowblocks_tag( | |
1552 | xfs_inode_t *ip) | |
1553 | { | |
7b7381f0 | 1554 | trace_xfs_inode_clear_cowblocks_tag(ip); |
ce2d3bbe | 1555 | return xfs_blockgc_clear_iflag(ip, XFS_ICOWBLOCKS); |
83104d44 | 1556 | } |
d6b636eb | 1557 | |
894ecacf DW |
1558 | #define for_each_perag_tag(mp, next_agno, pag, tag) \ |
1559 | for ((next_agno) = 0, (pag) = xfs_perag_get_tag((mp), 0, (tag)); \ | |
1560 | (pag) != NULL; \ | |
1561 | (next_agno) = (pag)->pag_agno + 1, \ | |
1562 | xfs_perag_put(pag), \ | |
1563 | (pag) = xfs_perag_get_tag((mp), (next_agno), (tag))) | |
1564 | ||
1565 | ||
d6b636eb DW |
1566 | /* Disable post-EOF and CoW block auto-reclamation. */ |
1567 | void | |
c9a6526f | 1568 | xfs_blockgc_stop( |
d6b636eb DW |
1569 | struct xfs_mount *mp) |
1570 | { | |
894ecacf DW |
1571 | struct xfs_perag *pag; |
1572 | xfs_agnumber_t agno; | |
1573 | ||
1574 | for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG) | |
1575 | cancel_delayed_work_sync(&pag->pag_blockgc_work); | |
d6b636eb DW |
1576 | } |
1577 | ||
1578 | /* Enable post-EOF and CoW block auto-reclamation. */ | |
1579 | void | |
c9a6526f | 1580 | xfs_blockgc_start( |
d6b636eb DW |
1581 | struct xfs_mount *mp) |
1582 | { | |
894ecacf DW |
1583 | struct xfs_perag *pag; |
1584 | xfs_agnumber_t agno; | |
1585 | ||
1586 | for_each_perag_tag(mp, agno, pag, XFS_ICI_BLOCKGC_TAG) | |
1587 | xfs_blockgc_queue(pag); | |
d6b636eb | 1588 | } |
3d4feec0 | 1589 | |
41956753 DW |
1590 | /* Scan one incore inode for block preallocations that we can remove. */ |
1591 | static int | |
1592 | xfs_blockgc_scan_inode( | |
1593 | struct xfs_inode *ip, | |
1594 | void *args) | |
85c5b270 | 1595 | { |
0fa4a10a | 1596 | unsigned int lockflags = 0; |
85c5b270 DW |
1597 | int error; |
1598 | ||
0fa4a10a | 1599 | error = xfs_inode_free_eofblocks(ip, args, &lockflags); |
85c5b270 | 1600 | if (error) |
0fa4a10a | 1601 | goto unlock; |
85c5b270 | 1602 | |
0fa4a10a DW |
1603 | error = xfs_inode_free_cowblocks(ip, args, &lockflags); |
1604 | unlock: | |
1605 | if (lockflags) | |
1606 | xfs_iunlock(ip, lockflags); | |
1607 | return error; | |
85c5b270 DW |
1608 | } |
1609 | ||
9669f51d DW |
1610 | /* Background worker that trims preallocated space. */ |
1611 | void | |
1612 | xfs_blockgc_worker( | |
1613 | struct work_struct *work) | |
1614 | { | |
894ecacf DW |
1615 | struct xfs_perag *pag = container_of(to_delayed_work(work), |
1616 | struct xfs_perag, pag_blockgc_work); | |
1617 | struct xfs_mount *mp = pag->pag_mount; | |
9669f51d DW |
1618 | int error; |
1619 | ||
1620 | if (!sb_start_write_trylock(mp->m_super)) | |
1621 | return; | |
894ecacf | 1622 | error = xfs_inode_walk_ag(pag, 0, xfs_blockgc_scan_inode, NULL, |
41956753 | 1623 | XFS_ICI_BLOCKGC_TAG); |
9669f51d | 1624 | if (error) |
894ecacf DW |
1625 | xfs_info(mp, "AG %u preallocation gc worker failed, err=%d", |
1626 | pag->pag_agno, error); | |
9669f51d | 1627 | sb_end_write(mp->m_super); |
894ecacf | 1628 | xfs_blockgc_queue(pag); |
9669f51d DW |
1629 | } |
1630 | ||
85c5b270 DW |
1631 | /* |
1632 | * Try to free space in the filesystem by purging eofblocks and cowblocks. | |
1633 | */ | |
1634 | int | |
1635 | xfs_blockgc_free_space( | |
1636 | struct xfs_mount *mp, | |
1637 | struct xfs_eofblocks *eofb) | |
1638 | { | |
1639 | trace_xfs_blockgc_free_space(mp, eofb, _RET_IP_); | |
1640 | ||
41956753 DW |
1641 | return xfs_inode_walk(mp, 0, xfs_blockgc_scan_inode, eofb, |
1642 | XFS_ICI_BLOCKGC_TAG); | |
85c5b270 DW |
1643 | } |
1644 | ||
3d4feec0 | 1645 | /* |
c237dd7c DW |
1646 | * Run cow/eofblocks scans on the supplied dquots. We don't know exactly which |
1647 | * quota caused an allocation failure, so we make a best effort by including | |
1648 | * each quota under low free space conditions (less than 1% free space) in the | |
1649 | * scan. | |
111068f8 DW |
1650 | * |
1651 | * Callers must not hold any inode's ILOCK. If requesting a synchronous scan | |
1652 | * (XFS_EOF_FLAGS_SYNC), the caller also must not hold any inode's IOLOCK or | |
1653 | * MMAPLOCK. | |
3d4feec0 | 1654 | */ |
111068f8 | 1655 | int |
c237dd7c DW |
1656 | xfs_blockgc_free_dquots( |
1657 | struct xfs_mount *mp, | |
1658 | struct xfs_dquot *udqp, | |
1659 | struct xfs_dquot *gdqp, | |
1660 | struct xfs_dquot *pdqp, | |
111068f8 | 1661 | unsigned int eof_flags) |
3d4feec0 DW |
1662 | { |
1663 | struct xfs_eofblocks eofb = {0}; | |
3d4feec0 DW |
1664 | bool do_work = false; |
1665 | ||
c237dd7c DW |
1666 | if (!udqp && !gdqp && !pdqp) |
1667 | return 0; | |
1668 | ||
3d4feec0 | 1669 | /* |
111068f8 DW |
1670 | * Run a scan to free blocks using the union filter to cover all |
1671 | * applicable quotas in a single scan. | |
3d4feec0 | 1672 | */ |
111068f8 | 1673 | eofb.eof_flags = XFS_EOF_FLAGS_UNION | eof_flags; |
3d4feec0 | 1674 | |
c237dd7c DW |
1675 | if (XFS_IS_UQUOTA_ENFORCED(mp) && udqp && xfs_dquot_lowsp(udqp)) { |
1676 | eofb.eof_uid = make_kuid(mp->m_super->s_user_ns, udqp->q_id); | |
1677 | eofb.eof_flags |= XFS_EOF_FLAGS_UID; | |
1678 | do_work = true; | |
3d4feec0 DW |
1679 | } |
1680 | ||
c237dd7c DW |
1681 | if (XFS_IS_UQUOTA_ENFORCED(mp) && gdqp && xfs_dquot_lowsp(gdqp)) { |
1682 | eofb.eof_gid = make_kgid(mp->m_super->s_user_ns, gdqp->q_id); | |
1683 | eofb.eof_flags |= XFS_EOF_FLAGS_GID; | |
1684 | do_work = true; | |
3d4feec0 DW |
1685 | } |
1686 | ||
c237dd7c DW |
1687 | if (XFS_IS_PQUOTA_ENFORCED(mp) && pdqp && xfs_dquot_lowsp(pdqp)) { |
1688 | eofb.eof_prid = pdqp->q_id; | |
1689 | eofb.eof_flags |= XFS_EOF_FLAGS_PRID; | |
1690 | do_work = true; | |
3d4feec0 DW |
1691 | } |
1692 | ||
1693 | if (!do_work) | |
111068f8 | 1694 | return 0; |
3d4feec0 | 1695 | |
85c5b270 | 1696 | return xfs_blockgc_free_space(mp, &eofb); |
c237dd7c DW |
1697 | } |
1698 | ||
1699 | /* Run cow/eofblocks scans on the quotas attached to the inode. */ | |
1700 | int | |
1701 | xfs_blockgc_free_quota( | |
1702 | struct xfs_inode *ip, | |
1703 | unsigned int eof_flags) | |
1704 | { | |
1705 | return xfs_blockgc_free_dquots(ip->i_mount, | |
1706 | xfs_inode_dquot(ip, XFS_DQTYPE_USER), | |
1707 | xfs_inode_dquot(ip, XFS_DQTYPE_GROUP), | |
1708 | xfs_inode_dquot(ip, XFS_DQTYPE_PROJ), eof_flags); | |
3d4feec0 | 1709 | } |