]>
Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
16cdcec7 MX |
2 | /* |
3 | * Copyright (C) 2011 Fujitsu. All rights reserved. | |
4 | * Written by Miao Xie <miaox@cn.fujitsu.com> | |
16cdcec7 MX |
5 | */ |
6 | ||
7 | #include <linux/slab.h> | |
c7f88c4e | 8 | #include <linux/iversion.h> |
16cdcec7 MX |
9 | #include "delayed-inode.h" |
10 | #include "disk-io.h" | |
11 | #include "transaction.h" | |
3cae210f | 12 | #include "ctree.h" |
4f5427cc | 13 | #include "qgroup.h" |
16cdcec7 | 14 | |
de3cb945 CM |
15 | #define BTRFS_DELAYED_WRITEBACK 512 |
16 | #define BTRFS_DELAYED_BACKGROUND 128 | |
17 | #define BTRFS_DELAYED_BATCH 16 | |
16cdcec7 MX |
18 | |
19 | static struct kmem_cache *delayed_node_cache; | |
20 | ||
21 | int __init btrfs_delayed_inode_init(void) | |
22 | { | |
837e1972 | 23 | delayed_node_cache = kmem_cache_create("btrfs_delayed_node", |
16cdcec7 MX |
24 | sizeof(struct btrfs_delayed_node), |
25 | 0, | |
fba4b697 | 26 | SLAB_MEM_SPREAD, |
16cdcec7 MX |
27 | NULL); |
28 | if (!delayed_node_cache) | |
29 | return -ENOMEM; | |
30 | return 0; | |
31 | } | |
32 | ||
e67c718b | 33 | void __cold btrfs_delayed_inode_exit(void) |
16cdcec7 | 34 | { |
5598e900 | 35 | kmem_cache_destroy(delayed_node_cache); |
16cdcec7 MX |
36 | } |
37 | ||
38 | static inline void btrfs_init_delayed_node( | |
39 | struct btrfs_delayed_node *delayed_node, | |
40 | struct btrfs_root *root, u64 inode_id) | |
41 | { | |
42 | delayed_node->root = root; | |
43 | delayed_node->inode_id = inode_id; | |
6de5f18e | 44 | refcount_set(&delayed_node->refs, 0); |
03a1d4c8 LB |
45 | delayed_node->ins_root = RB_ROOT_CACHED; |
46 | delayed_node->del_root = RB_ROOT_CACHED; | |
16cdcec7 | 47 | mutex_init(&delayed_node->mutex); |
16cdcec7 MX |
48 | INIT_LIST_HEAD(&delayed_node->n_list); |
49 | INIT_LIST_HEAD(&delayed_node->p_list); | |
16cdcec7 MX |
50 | } |
51 | ||
52 | static inline int btrfs_is_continuous_delayed_item( | |
53 | struct btrfs_delayed_item *item1, | |
54 | struct btrfs_delayed_item *item2) | |
55 | { | |
56 | if (item1->key.type == BTRFS_DIR_INDEX_KEY && | |
57 | item1->key.objectid == item2->key.objectid && | |
58 | item1->key.type == item2->key.type && | |
59 | item1->key.offset + 1 == item2->key.offset) | |
60 | return 1; | |
61 | return 0; | |
62 | } | |
63 | ||
f85b7379 DS |
64 | static struct btrfs_delayed_node *btrfs_get_delayed_node( |
65 | struct btrfs_inode *btrfs_inode) | |
16cdcec7 | 66 | { |
16cdcec7 | 67 | struct btrfs_root *root = btrfs_inode->root; |
4a0cc7ca | 68 | u64 ino = btrfs_ino(btrfs_inode); |
2f7e33d4 | 69 | struct btrfs_delayed_node *node; |
16cdcec7 | 70 | |
20c7bcec | 71 | node = READ_ONCE(btrfs_inode->delayed_node); |
16cdcec7 | 72 | if (node) { |
6de5f18e | 73 | refcount_inc(&node->refs); |
16cdcec7 MX |
74 | return node; |
75 | } | |
76 | ||
77 | spin_lock(&root->inode_lock); | |
0d0ca30f | 78 | node = radix_tree_lookup(&root->delayed_nodes_tree, ino); |
ec35e48b | 79 | |
16cdcec7 MX |
80 | if (node) { |
81 | if (btrfs_inode->delayed_node) { | |
6de5f18e | 82 | refcount_inc(&node->refs); /* can be accessed */ |
2f7e33d4 | 83 | BUG_ON(btrfs_inode->delayed_node != node); |
16cdcec7 | 84 | spin_unlock(&root->inode_lock); |
2f7e33d4 | 85 | return node; |
16cdcec7 | 86 | } |
ec35e48b CM |
87 | |
88 | /* | |
89 | * It's possible that we're racing into the middle of removing | |
90 | * this node from the radix tree. In this case, the refcount | |
91 | * was zero and it should never go back to one. Just return | |
92 | * NULL like it was never in the radix at all; our release | |
93 | * function is in the process of removing it. | |
94 | * | |
95 | * Some implementations of refcount_inc refuse to bump the | |
96 | * refcount once it has hit zero. If we don't do this dance | |
97 | * here, refcount_inc() may decide to just WARN_ONCE() instead | |
98 | * of actually bumping the refcount. | |
99 | * | |
100 | * If this node is properly in the radix, we want to bump the | |
101 | * refcount twice, once for the inode and once for this get | |
102 | * operation. | |
103 | */ | |
104 | if (refcount_inc_not_zero(&node->refs)) { | |
105 | refcount_inc(&node->refs); | |
106 | btrfs_inode->delayed_node = node; | |
107 | } else { | |
108 | node = NULL; | |
109 | } | |
110 | ||
16cdcec7 MX |
111 | spin_unlock(&root->inode_lock); |
112 | return node; | |
113 | } | |
114 | spin_unlock(&root->inode_lock); | |
115 | ||
2f7e33d4 MX |
116 | return NULL; |
117 | } | |
118 | ||
79787eaa | 119 | /* Will return either the node or PTR_ERR(-ENOMEM) */ |
2f7e33d4 | 120 | static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node( |
f85b7379 | 121 | struct btrfs_inode *btrfs_inode) |
2f7e33d4 MX |
122 | { |
123 | struct btrfs_delayed_node *node; | |
2f7e33d4 | 124 | struct btrfs_root *root = btrfs_inode->root; |
4a0cc7ca | 125 | u64 ino = btrfs_ino(btrfs_inode); |
2f7e33d4 MX |
126 | int ret; |
127 | ||
128 | again: | |
340c6ca9 | 129 | node = btrfs_get_delayed_node(btrfs_inode); |
2f7e33d4 MX |
130 | if (node) |
131 | return node; | |
132 | ||
352dd9c8 | 133 | node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS); |
16cdcec7 MX |
134 | if (!node) |
135 | return ERR_PTR(-ENOMEM); | |
0d0ca30f | 136 | btrfs_init_delayed_node(node, root, ino); |
16cdcec7 | 137 | |
95e94d14 | 138 | /* cached in the btrfs inode and can be accessed */ |
6de5f18e | 139 | refcount_set(&node->refs, 2); |
16cdcec7 | 140 | |
e1860a77 | 141 | ret = radix_tree_preload(GFP_NOFS); |
16cdcec7 MX |
142 | if (ret) { |
143 | kmem_cache_free(delayed_node_cache, node); | |
144 | return ERR_PTR(ret); | |
145 | } | |
146 | ||
147 | spin_lock(&root->inode_lock); | |
0d0ca30f | 148 | ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node); |
16cdcec7 | 149 | if (ret == -EEXIST) { |
16cdcec7 | 150 | spin_unlock(&root->inode_lock); |
96493031 | 151 | kmem_cache_free(delayed_node_cache, node); |
16cdcec7 MX |
152 | radix_tree_preload_end(); |
153 | goto again; | |
154 | } | |
155 | btrfs_inode->delayed_node = node; | |
156 | spin_unlock(&root->inode_lock); | |
157 | radix_tree_preload_end(); | |
158 | ||
159 | return node; | |
160 | } | |
161 | ||
162 | /* | |
163 | * Call it when holding delayed_node->mutex | |
164 | * | |
165 | * If mod = 1, add this node into the prepared list. | |
166 | */ | |
167 | static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root, | |
168 | struct btrfs_delayed_node *node, | |
169 | int mod) | |
170 | { | |
171 | spin_lock(&root->lock); | |
7cf35d91 | 172 | if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { |
16cdcec7 MX |
173 | if (!list_empty(&node->p_list)) |
174 | list_move_tail(&node->p_list, &root->prepare_list); | |
175 | else if (mod) | |
176 | list_add_tail(&node->p_list, &root->prepare_list); | |
177 | } else { | |
178 | list_add_tail(&node->n_list, &root->node_list); | |
179 | list_add_tail(&node->p_list, &root->prepare_list); | |
6de5f18e | 180 | refcount_inc(&node->refs); /* inserted into list */ |
16cdcec7 | 181 | root->nodes++; |
7cf35d91 | 182 | set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags); |
16cdcec7 MX |
183 | } |
184 | spin_unlock(&root->lock); | |
185 | } | |
186 | ||
187 | /* Call it when holding delayed_node->mutex */ | |
188 | static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root, | |
189 | struct btrfs_delayed_node *node) | |
190 | { | |
191 | spin_lock(&root->lock); | |
7cf35d91 | 192 | if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { |
16cdcec7 | 193 | root->nodes--; |
6de5f18e | 194 | refcount_dec(&node->refs); /* not in the list */ |
16cdcec7 MX |
195 | list_del_init(&node->n_list); |
196 | if (!list_empty(&node->p_list)) | |
197 | list_del_init(&node->p_list); | |
7cf35d91 | 198 | clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags); |
16cdcec7 MX |
199 | } |
200 | spin_unlock(&root->lock); | |
201 | } | |
202 | ||
48a3b636 | 203 | static struct btrfs_delayed_node *btrfs_first_delayed_node( |
16cdcec7 MX |
204 | struct btrfs_delayed_root *delayed_root) |
205 | { | |
206 | struct list_head *p; | |
207 | struct btrfs_delayed_node *node = NULL; | |
208 | ||
209 | spin_lock(&delayed_root->lock); | |
210 | if (list_empty(&delayed_root->node_list)) | |
211 | goto out; | |
212 | ||
213 | p = delayed_root->node_list.next; | |
214 | node = list_entry(p, struct btrfs_delayed_node, n_list); | |
6de5f18e | 215 | refcount_inc(&node->refs); |
16cdcec7 MX |
216 | out: |
217 | spin_unlock(&delayed_root->lock); | |
218 | ||
219 | return node; | |
220 | } | |
221 | ||
48a3b636 | 222 | static struct btrfs_delayed_node *btrfs_next_delayed_node( |
16cdcec7 MX |
223 | struct btrfs_delayed_node *node) |
224 | { | |
225 | struct btrfs_delayed_root *delayed_root; | |
226 | struct list_head *p; | |
227 | struct btrfs_delayed_node *next = NULL; | |
228 | ||
229 | delayed_root = node->root->fs_info->delayed_root; | |
230 | spin_lock(&delayed_root->lock); | |
7cf35d91 MX |
231 | if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) { |
232 | /* not in the list */ | |
16cdcec7 MX |
233 | if (list_empty(&delayed_root->node_list)) |
234 | goto out; | |
235 | p = delayed_root->node_list.next; | |
236 | } else if (list_is_last(&node->n_list, &delayed_root->node_list)) | |
237 | goto out; | |
238 | else | |
239 | p = node->n_list.next; | |
240 | ||
241 | next = list_entry(p, struct btrfs_delayed_node, n_list); | |
6de5f18e | 242 | refcount_inc(&next->refs); |
16cdcec7 MX |
243 | out: |
244 | spin_unlock(&delayed_root->lock); | |
245 | ||
246 | return next; | |
247 | } | |
248 | ||
249 | static void __btrfs_release_delayed_node( | |
250 | struct btrfs_delayed_node *delayed_node, | |
251 | int mod) | |
252 | { | |
253 | struct btrfs_delayed_root *delayed_root; | |
254 | ||
255 | if (!delayed_node) | |
256 | return; | |
257 | ||
258 | delayed_root = delayed_node->root->fs_info->delayed_root; | |
259 | ||
260 | mutex_lock(&delayed_node->mutex); | |
261 | if (delayed_node->count) | |
262 | btrfs_queue_delayed_node(delayed_root, delayed_node, mod); | |
263 | else | |
264 | btrfs_dequeue_delayed_node(delayed_root, delayed_node); | |
265 | mutex_unlock(&delayed_node->mutex); | |
266 | ||
6de5f18e | 267 | if (refcount_dec_and_test(&delayed_node->refs)) { |
16cdcec7 | 268 | struct btrfs_root *root = delayed_node->root; |
ec35e48b | 269 | |
16cdcec7 | 270 | spin_lock(&root->inode_lock); |
ec35e48b CM |
271 | /* |
272 | * Once our refcount goes to zero, nobody is allowed to bump it | |
273 | * back up. We can delete it now. | |
274 | */ | |
275 | ASSERT(refcount_read(&delayed_node->refs) == 0); | |
276 | radix_tree_delete(&root->delayed_nodes_tree, | |
277 | delayed_node->inode_id); | |
16cdcec7 | 278 | spin_unlock(&root->inode_lock); |
ec35e48b | 279 | kmem_cache_free(delayed_node_cache, delayed_node); |
16cdcec7 MX |
280 | } |
281 | } | |
282 | ||
283 | static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node) | |
284 | { | |
285 | __btrfs_release_delayed_node(node, 0); | |
286 | } | |
287 | ||
48a3b636 | 288 | static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node( |
16cdcec7 MX |
289 | struct btrfs_delayed_root *delayed_root) |
290 | { | |
291 | struct list_head *p; | |
292 | struct btrfs_delayed_node *node = NULL; | |
293 | ||
294 | spin_lock(&delayed_root->lock); | |
295 | if (list_empty(&delayed_root->prepare_list)) | |
296 | goto out; | |
297 | ||
298 | p = delayed_root->prepare_list.next; | |
299 | list_del_init(p); | |
300 | node = list_entry(p, struct btrfs_delayed_node, p_list); | |
6de5f18e | 301 | refcount_inc(&node->refs); |
16cdcec7 MX |
302 | out: |
303 | spin_unlock(&delayed_root->lock); | |
304 | ||
305 | return node; | |
306 | } | |
307 | ||
308 | static inline void btrfs_release_prepared_delayed_node( | |
309 | struct btrfs_delayed_node *node) | |
310 | { | |
311 | __btrfs_release_delayed_node(node, 1); | |
312 | } | |
313 | ||
48a3b636 | 314 | static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len) |
16cdcec7 MX |
315 | { |
316 | struct btrfs_delayed_item *item; | |
317 | item = kmalloc(sizeof(*item) + data_len, GFP_NOFS); | |
318 | if (item) { | |
319 | item->data_len = data_len; | |
320 | item->ins_or_del = 0; | |
321 | item->bytes_reserved = 0; | |
16cdcec7 | 322 | item->delayed_node = NULL; |
089e77e1 | 323 | refcount_set(&item->refs, 1); |
16cdcec7 MX |
324 | } |
325 | return item; | |
326 | } | |
327 | ||
328 | /* | |
329 | * __btrfs_lookup_delayed_item - look up the delayed item by key | |
330 | * @delayed_node: pointer to the delayed node | |
331 | * @key: the key to look up | |
332 | * @prev: used to store the prev item if the right item isn't found | |
333 | * @next: used to store the next item if the right item isn't found | |
334 | * | |
335 | * Note: if we don't find the right item, we will return the prev item and | |
336 | * the next item. | |
337 | */ | |
338 | static struct btrfs_delayed_item *__btrfs_lookup_delayed_item( | |
339 | struct rb_root *root, | |
340 | struct btrfs_key *key, | |
341 | struct btrfs_delayed_item **prev, | |
342 | struct btrfs_delayed_item **next) | |
343 | { | |
344 | struct rb_node *node, *prev_node = NULL; | |
345 | struct btrfs_delayed_item *delayed_item = NULL; | |
346 | int ret = 0; | |
347 | ||
348 | node = root->rb_node; | |
349 | ||
350 | while (node) { | |
351 | delayed_item = rb_entry(node, struct btrfs_delayed_item, | |
352 | rb_node); | |
353 | prev_node = node; | |
354 | ret = btrfs_comp_cpu_keys(&delayed_item->key, key); | |
355 | if (ret < 0) | |
356 | node = node->rb_right; | |
357 | else if (ret > 0) | |
358 | node = node->rb_left; | |
359 | else | |
360 | return delayed_item; | |
361 | } | |
362 | ||
363 | if (prev) { | |
364 | if (!prev_node) | |
365 | *prev = NULL; | |
366 | else if (ret < 0) | |
367 | *prev = delayed_item; | |
368 | else if ((node = rb_prev(prev_node)) != NULL) { | |
369 | *prev = rb_entry(node, struct btrfs_delayed_item, | |
370 | rb_node); | |
371 | } else | |
372 | *prev = NULL; | |
373 | } | |
374 | ||
375 | if (next) { | |
376 | if (!prev_node) | |
377 | *next = NULL; | |
378 | else if (ret > 0) | |
379 | *next = delayed_item; | |
380 | else if ((node = rb_next(prev_node)) != NULL) { | |
381 | *next = rb_entry(node, struct btrfs_delayed_item, | |
382 | rb_node); | |
383 | } else | |
384 | *next = NULL; | |
385 | } | |
386 | return NULL; | |
387 | } | |
388 | ||
48a3b636 | 389 | static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item( |
16cdcec7 MX |
390 | struct btrfs_delayed_node *delayed_node, |
391 | struct btrfs_key *key) | |
392 | { | |
03a1d4c8 | 393 | return __btrfs_lookup_delayed_item(&delayed_node->ins_root.rb_root, key, |
16cdcec7 | 394 | NULL, NULL); |
16cdcec7 MX |
395 | } |
396 | ||
16cdcec7 MX |
397 | static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node, |
398 | struct btrfs_delayed_item *ins, | |
399 | int action) | |
400 | { | |
401 | struct rb_node **p, *node; | |
402 | struct rb_node *parent_node = NULL; | |
03a1d4c8 | 403 | struct rb_root_cached *root; |
16cdcec7 MX |
404 | struct btrfs_delayed_item *item; |
405 | int cmp; | |
03a1d4c8 | 406 | bool leftmost = true; |
16cdcec7 MX |
407 | |
408 | if (action == BTRFS_DELAYED_INSERTION_ITEM) | |
409 | root = &delayed_node->ins_root; | |
410 | else if (action == BTRFS_DELAYED_DELETION_ITEM) | |
411 | root = &delayed_node->del_root; | |
412 | else | |
413 | BUG(); | |
03a1d4c8 | 414 | p = &root->rb_root.rb_node; |
16cdcec7 MX |
415 | node = &ins->rb_node; |
416 | ||
417 | while (*p) { | |
418 | parent_node = *p; | |
419 | item = rb_entry(parent_node, struct btrfs_delayed_item, | |
420 | rb_node); | |
421 | ||
422 | cmp = btrfs_comp_cpu_keys(&item->key, &ins->key); | |
03a1d4c8 | 423 | if (cmp < 0) { |
16cdcec7 | 424 | p = &(*p)->rb_right; |
03a1d4c8 LB |
425 | leftmost = false; |
426 | } else if (cmp > 0) { | |
16cdcec7 | 427 | p = &(*p)->rb_left; |
03a1d4c8 | 428 | } else { |
16cdcec7 | 429 | return -EEXIST; |
03a1d4c8 | 430 | } |
16cdcec7 MX |
431 | } |
432 | ||
433 | rb_link_node(node, parent_node, p); | |
03a1d4c8 | 434 | rb_insert_color_cached(node, root, leftmost); |
16cdcec7 MX |
435 | ins->delayed_node = delayed_node; |
436 | ins->ins_or_del = action; | |
437 | ||
438 | if (ins->key.type == BTRFS_DIR_INDEX_KEY && | |
439 | action == BTRFS_DELAYED_INSERTION_ITEM && | |
440 | ins->key.offset >= delayed_node->index_cnt) | |
441 | delayed_node->index_cnt = ins->key.offset + 1; | |
442 | ||
443 | delayed_node->count++; | |
444 | atomic_inc(&delayed_node->root->fs_info->delayed_root->items); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node, | |
449 | struct btrfs_delayed_item *item) | |
450 | { | |
451 | return __btrfs_add_delayed_item(node, item, | |
452 | BTRFS_DELAYED_INSERTION_ITEM); | |
453 | } | |
454 | ||
455 | static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node, | |
456 | struct btrfs_delayed_item *item) | |
457 | { | |
458 | return __btrfs_add_delayed_item(node, item, | |
459 | BTRFS_DELAYED_DELETION_ITEM); | |
460 | } | |
461 | ||
de3cb945 CM |
462 | static void finish_one_item(struct btrfs_delayed_root *delayed_root) |
463 | { | |
464 | int seq = atomic_inc_return(&delayed_root->items_seq); | |
ee863954 | 465 | |
093258e6 | 466 | /* atomic_dec_return implies a barrier */ |
de3cb945 | 467 | if ((atomic_dec_return(&delayed_root->items) < |
093258e6 DS |
468 | BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0)) |
469 | cond_wake_up_nomb(&delayed_root->wait); | |
de3cb945 CM |
470 | } |
471 | ||
16cdcec7 MX |
472 | static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item) |
473 | { | |
03a1d4c8 | 474 | struct rb_root_cached *root; |
16cdcec7 MX |
475 | struct btrfs_delayed_root *delayed_root; |
476 | ||
477 | delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root; | |
478 | ||
479 | BUG_ON(!delayed_root); | |
480 | BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM && | |
481 | delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM); | |
482 | ||
483 | if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM) | |
484 | root = &delayed_item->delayed_node->ins_root; | |
485 | else | |
486 | root = &delayed_item->delayed_node->del_root; | |
487 | ||
03a1d4c8 | 488 | rb_erase_cached(&delayed_item->rb_node, root); |
16cdcec7 | 489 | delayed_item->delayed_node->count--; |
de3cb945 CM |
490 | |
491 | finish_one_item(delayed_root); | |
16cdcec7 MX |
492 | } |
493 | ||
494 | static void btrfs_release_delayed_item(struct btrfs_delayed_item *item) | |
495 | { | |
496 | if (item) { | |
497 | __btrfs_remove_delayed_item(item); | |
089e77e1 | 498 | if (refcount_dec_and_test(&item->refs)) |
16cdcec7 MX |
499 | kfree(item); |
500 | } | |
501 | } | |
502 | ||
48a3b636 | 503 | static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item( |
16cdcec7 MX |
504 | struct btrfs_delayed_node *delayed_node) |
505 | { | |
506 | struct rb_node *p; | |
507 | struct btrfs_delayed_item *item = NULL; | |
508 | ||
03a1d4c8 | 509 | p = rb_first_cached(&delayed_node->ins_root); |
16cdcec7 MX |
510 | if (p) |
511 | item = rb_entry(p, struct btrfs_delayed_item, rb_node); | |
512 | ||
513 | return item; | |
514 | } | |
515 | ||
48a3b636 | 516 | static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item( |
16cdcec7 MX |
517 | struct btrfs_delayed_node *delayed_node) |
518 | { | |
519 | struct rb_node *p; | |
520 | struct btrfs_delayed_item *item = NULL; | |
521 | ||
03a1d4c8 | 522 | p = rb_first_cached(&delayed_node->del_root); |
16cdcec7 MX |
523 | if (p) |
524 | item = rb_entry(p, struct btrfs_delayed_item, rb_node); | |
525 | ||
526 | return item; | |
527 | } | |
528 | ||
48a3b636 | 529 | static struct btrfs_delayed_item *__btrfs_next_delayed_item( |
16cdcec7 MX |
530 | struct btrfs_delayed_item *item) |
531 | { | |
532 | struct rb_node *p; | |
533 | struct btrfs_delayed_item *next = NULL; | |
534 | ||
535 | p = rb_next(&item->rb_node); | |
536 | if (p) | |
537 | next = rb_entry(p, struct btrfs_delayed_item, rb_node); | |
538 | ||
539 | return next; | |
540 | } | |
541 | ||
16cdcec7 | 542 | static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans, |
4f5427cc | 543 | struct btrfs_root *root, |
16cdcec7 MX |
544 | struct btrfs_delayed_item *item) |
545 | { | |
546 | struct btrfs_block_rsv *src_rsv; | |
547 | struct btrfs_block_rsv *dst_rsv; | |
4f5427cc | 548 | struct btrfs_fs_info *fs_info = root->fs_info; |
16cdcec7 MX |
549 | u64 num_bytes; |
550 | int ret; | |
551 | ||
552 | if (!trans->bytes_reserved) | |
553 | return 0; | |
554 | ||
555 | src_rsv = trans->block_rsv; | |
0b246afa | 556 | dst_rsv = &fs_info->delayed_block_rsv; |
16cdcec7 | 557 | |
0b246afa | 558 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
f218ea6c QW |
559 | |
560 | /* | |
561 | * Here we migrate space rsv from transaction rsv, since have already | |
562 | * reserved space when starting a transaction. So no need to reserve | |
563 | * qgroup space here. | |
564 | */ | |
3a584174 | 565 | ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true); |
8c2a3ca2 | 566 | if (!ret) { |
0b246afa | 567 | trace_btrfs_space_reservation(fs_info, "delayed_item", |
8c2a3ca2 JB |
568 | item->key.objectid, |
569 | num_bytes, 1); | |
16cdcec7 | 570 | item->bytes_reserved = num_bytes; |
8c2a3ca2 | 571 | } |
16cdcec7 MX |
572 | |
573 | return ret; | |
574 | } | |
575 | ||
4f5427cc | 576 | static void btrfs_delayed_item_release_metadata(struct btrfs_root *root, |
16cdcec7 MX |
577 | struct btrfs_delayed_item *item) |
578 | { | |
19fd2949 | 579 | struct btrfs_block_rsv *rsv; |
4f5427cc | 580 | struct btrfs_fs_info *fs_info = root->fs_info; |
19fd2949 | 581 | |
16cdcec7 MX |
582 | if (!item->bytes_reserved) |
583 | return; | |
584 | ||
0b246afa | 585 | rsv = &fs_info->delayed_block_rsv; |
f218ea6c QW |
586 | /* |
587 | * Check btrfs_delayed_item_reserve_metadata() to see why we don't need | |
588 | * to release/reserve qgroup space. | |
589 | */ | |
0b246afa | 590 | trace_btrfs_space_reservation(fs_info, "delayed_item", |
8c2a3ca2 JB |
591 | item->key.objectid, item->bytes_reserved, |
592 | 0); | |
2ff7e61e | 593 | btrfs_block_rsv_release(fs_info, rsv, |
16cdcec7 MX |
594 | item->bytes_reserved); |
595 | } | |
596 | ||
597 | static int btrfs_delayed_inode_reserve_metadata( | |
598 | struct btrfs_trans_handle *trans, | |
599 | struct btrfs_root *root, | |
fcabdd1c | 600 | struct btrfs_inode *inode, |
16cdcec7 MX |
601 | struct btrfs_delayed_node *node) |
602 | { | |
0b246afa | 603 | struct btrfs_fs_info *fs_info = root->fs_info; |
16cdcec7 MX |
604 | struct btrfs_block_rsv *src_rsv; |
605 | struct btrfs_block_rsv *dst_rsv; | |
606 | u64 num_bytes; | |
607 | int ret; | |
608 | ||
16cdcec7 | 609 | src_rsv = trans->block_rsv; |
0b246afa | 610 | dst_rsv = &fs_info->delayed_block_rsv; |
16cdcec7 | 611 | |
0b246afa | 612 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
c06a0e12 JB |
613 | |
614 | /* | |
615 | * btrfs_dirty_inode will update the inode under btrfs_join_transaction | |
616 | * which doesn't reserve space for speed. This is a problem since we | |
617 | * still need to reserve space for this update, so try to reserve the | |
618 | * space. | |
619 | * | |
620 | * Now if src_rsv == delalloc_block_rsv we'll let it just steal since | |
69fe2d75 | 621 | * we always reserve enough to update the inode item. |
c06a0e12 | 622 | */ |
e755d9ab | 623 | if (!src_rsv || (!trans->bytes_reserved && |
66d8f3dd | 624 | src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) { |
f218ea6c QW |
625 | ret = btrfs_qgroup_reserve_meta_prealloc(root, |
626 | fs_info->nodesize, true); | |
627 | if (ret < 0) | |
628 | return ret; | |
08e007d2 MX |
629 | ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes, |
630 | BTRFS_RESERVE_NO_FLUSH); | |
c06a0e12 JB |
631 | /* |
632 | * Since we're under a transaction reserve_metadata_bytes could | |
633 | * try to commit the transaction which will make it return | |
634 | * EAGAIN to make us stop the transaction we have, so return | |
635 | * ENOSPC instead so that btrfs_dirty_inode knows what to do. | |
636 | */ | |
4f5427cc | 637 | if (ret == -EAGAIN) { |
c06a0e12 | 638 | ret = -ENOSPC; |
4f5427cc QW |
639 | btrfs_qgroup_free_meta_prealloc(root, num_bytes); |
640 | } | |
8c2a3ca2 | 641 | if (!ret) { |
c06a0e12 | 642 | node->bytes_reserved = num_bytes; |
0b246afa | 643 | trace_btrfs_space_reservation(fs_info, |
8c2a3ca2 | 644 | "delayed_inode", |
fcabdd1c | 645 | btrfs_ino(inode), |
8c2a3ca2 | 646 | num_bytes, 1); |
f218ea6c QW |
647 | } else { |
648 | btrfs_qgroup_free_meta_prealloc(root, fs_info->nodesize); | |
8c2a3ca2 | 649 | } |
c06a0e12 JB |
650 | return ret; |
651 | } | |
652 | ||
3a584174 | 653 | ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, true); |
8c2a3ca2 | 654 | if (!ret) { |
0b246afa | 655 | trace_btrfs_space_reservation(fs_info, "delayed_inode", |
fcabdd1c | 656 | btrfs_ino(inode), num_bytes, 1); |
16cdcec7 | 657 | node->bytes_reserved = num_bytes; |
8c2a3ca2 | 658 | } |
16cdcec7 MX |
659 | |
660 | return ret; | |
661 | } | |
662 | ||
2ff7e61e | 663 | static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info, |
4f5427cc QW |
664 | struct btrfs_delayed_node *node, |
665 | bool qgroup_free) | |
16cdcec7 MX |
666 | { |
667 | struct btrfs_block_rsv *rsv; | |
668 | ||
669 | if (!node->bytes_reserved) | |
670 | return; | |
671 | ||
0b246afa JM |
672 | rsv = &fs_info->delayed_block_rsv; |
673 | trace_btrfs_space_reservation(fs_info, "delayed_inode", | |
8c2a3ca2 | 674 | node->inode_id, node->bytes_reserved, 0); |
2ff7e61e | 675 | btrfs_block_rsv_release(fs_info, rsv, |
16cdcec7 | 676 | node->bytes_reserved); |
4f5427cc QW |
677 | if (qgroup_free) |
678 | btrfs_qgroup_free_meta_prealloc(node->root, | |
679 | node->bytes_reserved); | |
680 | else | |
681 | btrfs_qgroup_convert_reserved_meta(node->root, | |
682 | node->bytes_reserved); | |
16cdcec7 MX |
683 | node->bytes_reserved = 0; |
684 | } | |
685 | ||
686 | /* | |
687 | * This helper will insert some continuous items into the same leaf according | |
688 | * to the free space of the leaf. | |
689 | */ | |
afe5fea7 TI |
690 | static int btrfs_batch_insert_items(struct btrfs_root *root, |
691 | struct btrfs_path *path, | |
692 | struct btrfs_delayed_item *item) | |
16cdcec7 | 693 | { |
2ff7e61e | 694 | struct btrfs_fs_info *fs_info = root->fs_info; |
16cdcec7 MX |
695 | struct btrfs_delayed_item *curr, *next; |
696 | int free_space; | |
697 | int total_data_size = 0, total_size = 0; | |
698 | struct extent_buffer *leaf; | |
699 | char *data_ptr; | |
700 | struct btrfs_key *keys; | |
701 | u32 *data_size; | |
702 | struct list_head head; | |
703 | int slot; | |
704 | int nitems; | |
705 | int i; | |
706 | int ret = 0; | |
707 | ||
708 | BUG_ON(!path->nodes[0]); | |
709 | ||
710 | leaf = path->nodes[0]; | |
2ff7e61e | 711 | free_space = btrfs_leaf_free_space(fs_info, leaf); |
16cdcec7 MX |
712 | INIT_LIST_HEAD(&head); |
713 | ||
714 | next = item; | |
17aca1c9 | 715 | nitems = 0; |
16cdcec7 MX |
716 | |
717 | /* | |
718 | * count the number of the continuous items that we can insert in batch | |
719 | */ | |
720 | while (total_size + next->data_len + sizeof(struct btrfs_item) <= | |
721 | free_space) { | |
722 | total_data_size += next->data_len; | |
723 | total_size += next->data_len + sizeof(struct btrfs_item); | |
724 | list_add_tail(&next->tree_list, &head); | |
725 | nitems++; | |
726 | ||
727 | curr = next; | |
728 | next = __btrfs_next_delayed_item(curr); | |
729 | if (!next) | |
730 | break; | |
731 | ||
732 | if (!btrfs_is_continuous_delayed_item(curr, next)) | |
733 | break; | |
734 | } | |
735 | ||
736 | if (!nitems) { | |
737 | ret = 0; | |
738 | goto out; | |
739 | } | |
740 | ||
741 | /* | |
742 | * we need allocate some memory space, but it might cause the task | |
743 | * to sleep, so we set all locked nodes in the path to blocking locks | |
744 | * first. | |
745 | */ | |
746 | btrfs_set_path_blocking(path); | |
747 | ||
d9b0d9ba | 748 | keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS); |
16cdcec7 MX |
749 | if (!keys) { |
750 | ret = -ENOMEM; | |
751 | goto out; | |
752 | } | |
753 | ||
d9b0d9ba | 754 | data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS); |
16cdcec7 MX |
755 | if (!data_size) { |
756 | ret = -ENOMEM; | |
757 | goto error; | |
758 | } | |
759 | ||
760 | /* get keys of all the delayed items */ | |
761 | i = 0; | |
762 | list_for_each_entry(next, &head, tree_list) { | |
763 | keys[i] = next->key; | |
764 | data_size[i] = next->data_len; | |
765 | i++; | |
766 | } | |
767 | ||
16cdcec7 | 768 | /* insert the keys of the items */ |
afe5fea7 | 769 | setup_items_for_insert(root, path, keys, data_size, |
143bede5 | 770 | total_data_size, total_size, nitems); |
16cdcec7 MX |
771 | |
772 | /* insert the dir index items */ | |
773 | slot = path->slots[0]; | |
774 | list_for_each_entry_safe(curr, next, &head, tree_list) { | |
775 | data_ptr = btrfs_item_ptr(leaf, slot, char); | |
776 | write_extent_buffer(leaf, &curr->data, | |
777 | (unsigned long)data_ptr, | |
778 | curr->data_len); | |
779 | slot++; | |
780 | ||
4f5427cc | 781 | btrfs_delayed_item_release_metadata(root, curr); |
16cdcec7 MX |
782 | |
783 | list_del(&curr->tree_list); | |
784 | btrfs_release_delayed_item(curr); | |
785 | } | |
786 | ||
787 | error: | |
788 | kfree(data_size); | |
789 | kfree(keys); | |
790 | out: | |
791 | return ret; | |
792 | } | |
793 | ||
794 | /* | |
795 | * This helper can just do simple insertion that needn't extend item for new | |
796 | * data, such as directory name index insertion, inode insertion. | |
797 | */ | |
798 | static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans, | |
799 | struct btrfs_root *root, | |
800 | struct btrfs_path *path, | |
801 | struct btrfs_delayed_item *delayed_item) | |
802 | { | |
803 | struct extent_buffer *leaf; | |
16cdcec7 MX |
804 | char *ptr; |
805 | int ret; | |
806 | ||
807 | ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key, | |
808 | delayed_item->data_len); | |
809 | if (ret < 0 && ret != -EEXIST) | |
810 | return ret; | |
811 | ||
812 | leaf = path->nodes[0]; | |
813 | ||
16cdcec7 MX |
814 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
815 | ||
816 | write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr, | |
817 | delayed_item->data_len); | |
818 | btrfs_mark_buffer_dirty(leaf); | |
819 | ||
4f5427cc | 820 | btrfs_delayed_item_release_metadata(root, delayed_item); |
16cdcec7 MX |
821 | return 0; |
822 | } | |
823 | ||
824 | /* | |
825 | * we insert an item first, then if there are some continuous items, we try | |
826 | * to insert those items into the same leaf. | |
827 | */ | |
828 | static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans, | |
829 | struct btrfs_path *path, | |
830 | struct btrfs_root *root, | |
831 | struct btrfs_delayed_node *node) | |
832 | { | |
833 | struct btrfs_delayed_item *curr, *prev; | |
834 | int ret = 0; | |
835 | ||
836 | do_again: | |
837 | mutex_lock(&node->mutex); | |
838 | curr = __btrfs_first_delayed_insertion_item(node); | |
839 | if (!curr) | |
840 | goto insert_end; | |
841 | ||
842 | ret = btrfs_insert_delayed_item(trans, root, path, curr); | |
843 | if (ret < 0) { | |
945d8962 | 844 | btrfs_release_path(path); |
16cdcec7 MX |
845 | goto insert_end; |
846 | } | |
847 | ||
848 | prev = curr; | |
849 | curr = __btrfs_next_delayed_item(prev); | |
850 | if (curr && btrfs_is_continuous_delayed_item(prev, curr)) { | |
851 | /* insert the continuous items into the same leaf */ | |
852 | path->slots[0]++; | |
afe5fea7 | 853 | btrfs_batch_insert_items(root, path, curr); |
16cdcec7 MX |
854 | } |
855 | btrfs_release_delayed_item(prev); | |
856 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
857 | ||
945d8962 | 858 | btrfs_release_path(path); |
16cdcec7 MX |
859 | mutex_unlock(&node->mutex); |
860 | goto do_again; | |
861 | ||
862 | insert_end: | |
863 | mutex_unlock(&node->mutex); | |
864 | return ret; | |
865 | } | |
866 | ||
867 | static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans, | |
868 | struct btrfs_root *root, | |
869 | struct btrfs_path *path, | |
870 | struct btrfs_delayed_item *item) | |
871 | { | |
872 | struct btrfs_delayed_item *curr, *next; | |
873 | struct extent_buffer *leaf; | |
874 | struct btrfs_key key; | |
875 | struct list_head head; | |
876 | int nitems, i, last_item; | |
877 | int ret = 0; | |
878 | ||
879 | BUG_ON(!path->nodes[0]); | |
880 | ||
881 | leaf = path->nodes[0]; | |
882 | ||
883 | i = path->slots[0]; | |
884 | last_item = btrfs_header_nritems(leaf) - 1; | |
885 | if (i > last_item) | |
886 | return -ENOENT; /* FIXME: Is errno suitable? */ | |
887 | ||
888 | next = item; | |
889 | INIT_LIST_HEAD(&head); | |
890 | btrfs_item_key_to_cpu(leaf, &key, i); | |
891 | nitems = 0; | |
892 | /* | |
893 | * count the number of the dir index items that we can delete in batch | |
894 | */ | |
895 | while (btrfs_comp_cpu_keys(&next->key, &key) == 0) { | |
896 | list_add_tail(&next->tree_list, &head); | |
897 | nitems++; | |
898 | ||
899 | curr = next; | |
900 | next = __btrfs_next_delayed_item(curr); | |
901 | if (!next) | |
902 | break; | |
903 | ||
904 | if (!btrfs_is_continuous_delayed_item(curr, next)) | |
905 | break; | |
906 | ||
907 | i++; | |
908 | if (i > last_item) | |
909 | break; | |
910 | btrfs_item_key_to_cpu(leaf, &key, i); | |
911 | } | |
912 | ||
913 | if (!nitems) | |
914 | return 0; | |
915 | ||
916 | ret = btrfs_del_items(trans, root, path, path->slots[0], nitems); | |
917 | if (ret) | |
918 | goto out; | |
919 | ||
920 | list_for_each_entry_safe(curr, next, &head, tree_list) { | |
4f5427cc | 921 | btrfs_delayed_item_release_metadata(root, curr); |
16cdcec7 MX |
922 | list_del(&curr->tree_list); |
923 | btrfs_release_delayed_item(curr); | |
924 | } | |
925 | ||
926 | out: | |
927 | return ret; | |
928 | } | |
929 | ||
930 | static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans, | |
931 | struct btrfs_path *path, | |
932 | struct btrfs_root *root, | |
933 | struct btrfs_delayed_node *node) | |
934 | { | |
935 | struct btrfs_delayed_item *curr, *prev; | |
936 | int ret = 0; | |
937 | ||
938 | do_again: | |
939 | mutex_lock(&node->mutex); | |
940 | curr = __btrfs_first_delayed_deletion_item(node); | |
941 | if (!curr) | |
942 | goto delete_fail; | |
943 | ||
944 | ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1); | |
945 | if (ret < 0) | |
946 | goto delete_fail; | |
947 | else if (ret > 0) { | |
948 | /* | |
949 | * can't find the item which the node points to, so this node | |
950 | * is invalid, just drop it. | |
951 | */ | |
952 | prev = curr; | |
953 | curr = __btrfs_next_delayed_item(prev); | |
954 | btrfs_release_delayed_item(prev); | |
955 | ret = 0; | |
945d8962 | 956 | btrfs_release_path(path); |
62095265 FW |
957 | if (curr) { |
958 | mutex_unlock(&node->mutex); | |
16cdcec7 | 959 | goto do_again; |
62095265 | 960 | } else |
16cdcec7 MX |
961 | goto delete_fail; |
962 | } | |
963 | ||
964 | btrfs_batch_delete_items(trans, root, path, curr); | |
945d8962 | 965 | btrfs_release_path(path); |
16cdcec7 MX |
966 | mutex_unlock(&node->mutex); |
967 | goto do_again; | |
968 | ||
969 | delete_fail: | |
945d8962 | 970 | btrfs_release_path(path); |
16cdcec7 MX |
971 | mutex_unlock(&node->mutex); |
972 | return ret; | |
973 | } | |
974 | ||
975 | static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node) | |
976 | { | |
977 | struct btrfs_delayed_root *delayed_root; | |
978 | ||
7cf35d91 MX |
979 | if (delayed_node && |
980 | test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { | |
16cdcec7 | 981 | BUG_ON(!delayed_node->root); |
7cf35d91 | 982 | clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags); |
16cdcec7 MX |
983 | delayed_node->count--; |
984 | ||
985 | delayed_root = delayed_node->root->fs_info->delayed_root; | |
de3cb945 | 986 | finish_one_item(delayed_root); |
16cdcec7 MX |
987 | } |
988 | } | |
989 | ||
67de1176 MX |
990 | static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node) |
991 | { | |
992 | struct btrfs_delayed_root *delayed_root; | |
993 | ||
994 | ASSERT(delayed_node->root); | |
995 | clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags); | |
996 | delayed_node->count--; | |
997 | ||
998 | delayed_root = delayed_node->root->fs_info->delayed_root; | |
999 | finish_one_item(delayed_root); | |
1000 | } | |
1001 | ||
0e8c36a9 MX |
1002 | static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, |
1003 | struct btrfs_root *root, | |
1004 | struct btrfs_path *path, | |
1005 | struct btrfs_delayed_node *node) | |
16cdcec7 | 1006 | { |
2ff7e61e | 1007 | struct btrfs_fs_info *fs_info = root->fs_info; |
16cdcec7 MX |
1008 | struct btrfs_key key; |
1009 | struct btrfs_inode_item *inode_item; | |
1010 | struct extent_buffer *leaf; | |
67de1176 | 1011 | int mod; |
16cdcec7 MX |
1012 | int ret; |
1013 | ||
16cdcec7 | 1014 | key.objectid = node->inode_id; |
962a298f | 1015 | key.type = BTRFS_INODE_ITEM_KEY; |
16cdcec7 | 1016 | key.offset = 0; |
0e8c36a9 | 1017 | |
67de1176 MX |
1018 | if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) |
1019 | mod = -1; | |
1020 | else | |
1021 | mod = 1; | |
1022 | ||
1023 | ret = btrfs_lookup_inode(trans, root, path, &key, mod); | |
16cdcec7 | 1024 | if (ret > 0) { |
945d8962 | 1025 | btrfs_release_path(path); |
16cdcec7 MX |
1026 | return -ENOENT; |
1027 | } else if (ret < 0) { | |
16cdcec7 MX |
1028 | return ret; |
1029 | } | |
1030 | ||
16cdcec7 MX |
1031 | leaf = path->nodes[0]; |
1032 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
1033 | struct btrfs_inode_item); | |
1034 | write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item, | |
1035 | sizeof(struct btrfs_inode_item)); | |
1036 | btrfs_mark_buffer_dirty(leaf); | |
16cdcec7 | 1037 | |
67de1176 MX |
1038 | if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags)) |
1039 | goto no_iref; | |
1040 | ||
1041 | path->slots[0]++; | |
1042 | if (path->slots[0] >= btrfs_header_nritems(leaf)) | |
1043 | goto search; | |
1044 | again: | |
1045 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1046 | if (key.objectid != node->inode_id) | |
1047 | goto out; | |
1048 | ||
1049 | if (key.type != BTRFS_INODE_REF_KEY && | |
1050 | key.type != BTRFS_INODE_EXTREF_KEY) | |
1051 | goto out; | |
1052 | ||
1053 | /* | |
1054 | * Delayed iref deletion is for the inode who has only one link, | |
1055 | * so there is only one iref. The case that several irefs are | |
1056 | * in the same item doesn't exist. | |
1057 | */ | |
1058 | btrfs_del_item(trans, root, path); | |
1059 | out: | |
1060 | btrfs_release_delayed_iref(node); | |
1061 | no_iref: | |
1062 | btrfs_release_path(path); | |
1063 | err_out: | |
4f5427cc | 1064 | btrfs_delayed_inode_release_metadata(fs_info, node, (ret < 0)); |
16cdcec7 | 1065 | btrfs_release_delayed_inode(node); |
16cdcec7 | 1066 | |
67de1176 MX |
1067 | return ret; |
1068 | ||
1069 | search: | |
1070 | btrfs_release_path(path); | |
1071 | ||
962a298f | 1072 | key.type = BTRFS_INODE_EXTREF_KEY; |
67de1176 MX |
1073 | key.offset = -1; |
1074 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1075 | if (ret < 0) | |
1076 | goto err_out; | |
1077 | ASSERT(ret); | |
1078 | ||
1079 | ret = 0; | |
1080 | leaf = path->nodes[0]; | |
1081 | path->slots[0]--; | |
1082 | goto again; | |
16cdcec7 MX |
1083 | } |
1084 | ||
0e8c36a9 MX |
1085 | static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans, |
1086 | struct btrfs_root *root, | |
1087 | struct btrfs_path *path, | |
1088 | struct btrfs_delayed_node *node) | |
1089 | { | |
1090 | int ret; | |
1091 | ||
1092 | mutex_lock(&node->mutex); | |
7cf35d91 | 1093 | if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) { |
0e8c36a9 MX |
1094 | mutex_unlock(&node->mutex); |
1095 | return 0; | |
1096 | } | |
1097 | ||
1098 | ret = __btrfs_update_delayed_inode(trans, root, path, node); | |
1099 | mutex_unlock(&node->mutex); | |
1100 | return ret; | |
1101 | } | |
1102 | ||
4ea41ce0 MX |
1103 | static inline int |
1104 | __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, | |
1105 | struct btrfs_path *path, | |
1106 | struct btrfs_delayed_node *node) | |
1107 | { | |
1108 | int ret; | |
1109 | ||
1110 | ret = btrfs_insert_delayed_items(trans, path, node->root, node); | |
1111 | if (ret) | |
1112 | return ret; | |
1113 | ||
1114 | ret = btrfs_delete_delayed_items(trans, path, node->root, node); | |
1115 | if (ret) | |
1116 | return ret; | |
1117 | ||
1118 | ret = btrfs_update_delayed_inode(trans, node->root, path, node); | |
1119 | return ret; | |
1120 | } | |
1121 | ||
79787eaa JM |
1122 | /* |
1123 | * Called when committing the transaction. | |
1124 | * Returns 0 on success. | |
1125 | * Returns < 0 on error and returns with an aborted transaction with any | |
1126 | * outstanding delayed items cleaned up. | |
1127 | */ | |
b84acab3 | 1128 | static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr) |
16cdcec7 | 1129 | { |
b84acab3 | 1130 | struct btrfs_fs_info *fs_info = trans->fs_info; |
16cdcec7 MX |
1131 | struct btrfs_delayed_root *delayed_root; |
1132 | struct btrfs_delayed_node *curr_node, *prev_node; | |
1133 | struct btrfs_path *path; | |
19fd2949 | 1134 | struct btrfs_block_rsv *block_rsv; |
16cdcec7 | 1135 | int ret = 0; |
96c3f433 | 1136 | bool count = (nr > 0); |
16cdcec7 | 1137 | |
79787eaa JM |
1138 | if (trans->aborted) |
1139 | return -EIO; | |
1140 | ||
16cdcec7 MX |
1141 | path = btrfs_alloc_path(); |
1142 | if (!path) | |
1143 | return -ENOMEM; | |
1144 | path->leave_spinning = 1; | |
1145 | ||
19fd2949 | 1146 | block_rsv = trans->block_rsv; |
0b246afa | 1147 | trans->block_rsv = &fs_info->delayed_block_rsv; |
19fd2949 | 1148 | |
ccdf9b30 | 1149 | delayed_root = fs_info->delayed_root; |
16cdcec7 MX |
1150 | |
1151 | curr_node = btrfs_first_delayed_node(delayed_root); | |
96c3f433 | 1152 | while (curr_node && (!count || (count && nr--))) { |
4ea41ce0 MX |
1153 | ret = __btrfs_commit_inode_delayed_items(trans, path, |
1154 | curr_node); | |
16cdcec7 MX |
1155 | if (ret) { |
1156 | btrfs_release_delayed_node(curr_node); | |
96c3f433 | 1157 | curr_node = NULL; |
66642832 | 1158 | btrfs_abort_transaction(trans, ret); |
16cdcec7 MX |
1159 | break; |
1160 | } | |
1161 | ||
1162 | prev_node = curr_node; | |
1163 | curr_node = btrfs_next_delayed_node(curr_node); | |
1164 | btrfs_release_delayed_node(prev_node); | |
1165 | } | |
1166 | ||
96c3f433 JB |
1167 | if (curr_node) |
1168 | btrfs_release_delayed_node(curr_node); | |
16cdcec7 | 1169 | btrfs_free_path(path); |
19fd2949 | 1170 | trans->block_rsv = block_rsv; |
79787eaa | 1171 | |
16cdcec7 MX |
1172 | return ret; |
1173 | } | |
1174 | ||
e5c304e6 | 1175 | int btrfs_run_delayed_items(struct btrfs_trans_handle *trans) |
96c3f433 | 1176 | { |
b84acab3 | 1177 | return __btrfs_run_delayed_items(trans, -1); |
96c3f433 JB |
1178 | } |
1179 | ||
e5c304e6 | 1180 | int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, int nr) |
96c3f433 | 1181 | { |
b84acab3 | 1182 | return __btrfs_run_delayed_items(trans, nr); |
96c3f433 JB |
1183 | } |
1184 | ||
16cdcec7 | 1185 | int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, |
5f4b32e9 | 1186 | struct btrfs_inode *inode) |
16cdcec7 | 1187 | { |
5f4b32e9 | 1188 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
4ea41ce0 MX |
1189 | struct btrfs_path *path; |
1190 | struct btrfs_block_rsv *block_rsv; | |
16cdcec7 MX |
1191 | int ret; |
1192 | ||
1193 | if (!delayed_node) | |
1194 | return 0; | |
1195 | ||
1196 | mutex_lock(&delayed_node->mutex); | |
1197 | if (!delayed_node->count) { | |
1198 | mutex_unlock(&delayed_node->mutex); | |
1199 | btrfs_release_delayed_node(delayed_node); | |
1200 | return 0; | |
1201 | } | |
1202 | mutex_unlock(&delayed_node->mutex); | |
1203 | ||
4ea41ce0 | 1204 | path = btrfs_alloc_path(); |
3c77bd94 FDBM |
1205 | if (!path) { |
1206 | btrfs_release_delayed_node(delayed_node); | |
4ea41ce0 | 1207 | return -ENOMEM; |
3c77bd94 | 1208 | } |
4ea41ce0 MX |
1209 | path->leave_spinning = 1; |
1210 | ||
1211 | block_rsv = trans->block_rsv; | |
1212 | trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv; | |
1213 | ||
1214 | ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node); | |
1215 | ||
16cdcec7 | 1216 | btrfs_release_delayed_node(delayed_node); |
4ea41ce0 MX |
1217 | btrfs_free_path(path); |
1218 | trans->block_rsv = block_rsv; | |
1219 | ||
16cdcec7 MX |
1220 | return ret; |
1221 | } | |
1222 | ||
aa79021f | 1223 | int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode) |
0e8c36a9 | 1224 | { |
3ffbd68c | 1225 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
0e8c36a9 | 1226 | struct btrfs_trans_handle *trans; |
aa79021f | 1227 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
0e8c36a9 MX |
1228 | struct btrfs_path *path; |
1229 | struct btrfs_block_rsv *block_rsv; | |
1230 | int ret; | |
1231 | ||
1232 | if (!delayed_node) | |
1233 | return 0; | |
1234 | ||
1235 | mutex_lock(&delayed_node->mutex); | |
7cf35d91 | 1236 | if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
0e8c36a9 MX |
1237 | mutex_unlock(&delayed_node->mutex); |
1238 | btrfs_release_delayed_node(delayed_node); | |
1239 | return 0; | |
1240 | } | |
1241 | mutex_unlock(&delayed_node->mutex); | |
1242 | ||
1243 | trans = btrfs_join_transaction(delayed_node->root); | |
1244 | if (IS_ERR(trans)) { | |
1245 | ret = PTR_ERR(trans); | |
1246 | goto out; | |
1247 | } | |
1248 | ||
1249 | path = btrfs_alloc_path(); | |
1250 | if (!path) { | |
1251 | ret = -ENOMEM; | |
1252 | goto trans_out; | |
1253 | } | |
1254 | path->leave_spinning = 1; | |
1255 | ||
1256 | block_rsv = trans->block_rsv; | |
2ff7e61e | 1257 | trans->block_rsv = &fs_info->delayed_block_rsv; |
0e8c36a9 MX |
1258 | |
1259 | mutex_lock(&delayed_node->mutex); | |
7cf35d91 | 1260 | if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) |
0e8c36a9 MX |
1261 | ret = __btrfs_update_delayed_inode(trans, delayed_node->root, |
1262 | path, delayed_node); | |
1263 | else | |
1264 | ret = 0; | |
1265 | mutex_unlock(&delayed_node->mutex); | |
1266 | ||
1267 | btrfs_free_path(path); | |
1268 | trans->block_rsv = block_rsv; | |
1269 | trans_out: | |
3a45bb20 | 1270 | btrfs_end_transaction(trans); |
2ff7e61e | 1271 | btrfs_btree_balance_dirty(fs_info); |
0e8c36a9 MX |
1272 | out: |
1273 | btrfs_release_delayed_node(delayed_node); | |
1274 | ||
1275 | return ret; | |
1276 | } | |
1277 | ||
f48d1cf5 | 1278 | void btrfs_remove_delayed_node(struct btrfs_inode *inode) |
16cdcec7 MX |
1279 | { |
1280 | struct btrfs_delayed_node *delayed_node; | |
1281 | ||
f48d1cf5 | 1282 | delayed_node = READ_ONCE(inode->delayed_node); |
16cdcec7 MX |
1283 | if (!delayed_node) |
1284 | return; | |
1285 | ||
f48d1cf5 | 1286 | inode->delayed_node = NULL; |
16cdcec7 MX |
1287 | btrfs_release_delayed_node(delayed_node); |
1288 | } | |
1289 | ||
de3cb945 CM |
1290 | struct btrfs_async_delayed_work { |
1291 | struct btrfs_delayed_root *delayed_root; | |
1292 | int nr; | |
d458b054 | 1293 | struct btrfs_work work; |
16cdcec7 MX |
1294 | }; |
1295 | ||
d458b054 | 1296 | static void btrfs_async_run_delayed_root(struct btrfs_work *work) |
16cdcec7 | 1297 | { |
de3cb945 CM |
1298 | struct btrfs_async_delayed_work *async_work; |
1299 | struct btrfs_delayed_root *delayed_root; | |
16cdcec7 MX |
1300 | struct btrfs_trans_handle *trans; |
1301 | struct btrfs_path *path; | |
1302 | struct btrfs_delayed_node *delayed_node = NULL; | |
1303 | struct btrfs_root *root; | |
19fd2949 | 1304 | struct btrfs_block_rsv *block_rsv; |
de3cb945 | 1305 | int total_done = 0; |
16cdcec7 | 1306 | |
de3cb945 CM |
1307 | async_work = container_of(work, struct btrfs_async_delayed_work, work); |
1308 | delayed_root = async_work->delayed_root; | |
16cdcec7 MX |
1309 | |
1310 | path = btrfs_alloc_path(); | |
1311 | if (!path) | |
1312 | goto out; | |
16cdcec7 | 1313 | |
617c54a8 NB |
1314 | do { |
1315 | if (atomic_read(&delayed_root->items) < | |
1316 | BTRFS_DELAYED_BACKGROUND / 2) | |
1317 | break; | |
de3cb945 | 1318 | |
617c54a8 NB |
1319 | delayed_node = btrfs_first_prepared_delayed_node(delayed_root); |
1320 | if (!delayed_node) | |
1321 | break; | |
de3cb945 | 1322 | |
617c54a8 NB |
1323 | path->leave_spinning = 1; |
1324 | root = delayed_node->root; | |
16cdcec7 | 1325 | |
617c54a8 NB |
1326 | trans = btrfs_join_transaction(root); |
1327 | if (IS_ERR(trans)) { | |
1328 | btrfs_release_path(path); | |
1329 | btrfs_release_prepared_delayed_node(delayed_node); | |
1330 | total_done++; | |
1331 | continue; | |
1332 | } | |
16cdcec7 | 1333 | |
617c54a8 NB |
1334 | block_rsv = trans->block_rsv; |
1335 | trans->block_rsv = &root->fs_info->delayed_block_rsv; | |
19fd2949 | 1336 | |
617c54a8 | 1337 | __btrfs_commit_inode_delayed_items(trans, path, delayed_node); |
16cdcec7 | 1338 | |
617c54a8 NB |
1339 | trans->block_rsv = block_rsv; |
1340 | btrfs_end_transaction(trans); | |
1341 | btrfs_btree_balance_dirty_nodelay(root->fs_info); | |
de3cb945 | 1342 | |
617c54a8 NB |
1343 | btrfs_release_path(path); |
1344 | btrfs_release_prepared_delayed_node(delayed_node); | |
1345 | total_done++; | |
de3cb945 | 1346 | |
617c54a8 NB |
1347 | } while ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK) |
1348 | || total_done < async_work->nr); | |
de3cb945 | 1349 | |
16cdcec7 MX |
1350 | btrfs_free_path(path); |
1351 | out: | |
de3cb945 CM |
1352 | wake_up(&delayed_root->wait); |
1353 | kfree(async_work); | |
16cdcec7 MX |
1354 | } |
1355 | ||
de3cb945 | 1356 | |
16cdcec7 | 1357 | static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, |
a585e948 | 1358 | struct btrfs_fs_info *fs_info, int nr) |
16cdcec7 | 1359 | { |
de3cb945 | 1360 | struct btrfs_async_delayed_work *async_work; |
16cdcec7 | 1361 | |
de3cb945 CM |
1362 | async_work = kmalloc(sizeof(*async_work), GFP_NOFS); |
1363 | if (!async_work) | |
16cdcec7 | 1364 | return -ENOMEM; |
16cdcec7 | 1365 | |
de3cb945 | 1366 | async_work->delayed_root = delayed_root; |
9e0af237 LB |
1367 | btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper, |
1368 | btrfs_async_run_delayed_root, NULL, NULL); | |
de3cb945 | 1369 | async_work->nr = nr; |
16cdcec7 | 1370 | |
a585e948 | 1371 | btrfs_queue_work(fs_info->delayed_workers, &async_work->work); |
16cdcec7 MX |
1372 | return 0; |
1373 | } | |
1374 | ||
ccdf9b30 | 1375 | void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info) |
e999376f | 1376 | { |
ccdf9b30 | 1377 | WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root)); |
e999376f CM |
1378 | } |
1379 | ||
0353808c | 1380 | static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq) |
de3cb945 CM |
1381 | { |
1382 | int val = atomic_read(&delayed_root->items_seq); | |
1383 | ||
0353808c | 1384 | if (val < seq || val >= seq + BTRFS_DELAYED_BATCH) |
de3cb945 | 1385 | return 1; |
0353808c MX |
1386 | |
1387 | if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) | |
1388 | return 1; | |
1389 | ||
de3cb945 CM |
1390 | return 0; |
1391 | } | |
1392 | ||
2ff7e61e | 1393 | void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info) |
16cdcec7 | 1394 | { |
2ff7e61e | 1395 | struct btrfs_delayed_root *delayed_root = fs_info->delayed_root; |
16cdcec7 | 1396 | |
8577787f NB |
1397 | if ((atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) || |
1398 | btrfs_workqueue_normal_congested(fs_info->delayed_workers)) | |
16cdcec7 MX |
1399 | return; |
1400 | ||
1401 | if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) { | |
0353808c | 1402 | int seq; |
16cdcec7 | 1403 | int ret; |
0353808c MX |
1404 | |
1405 | seq = atomic_read(&delayed_root->items_seq); | |
de3cb945 | 1406 | |
a585e948 | 1407 | ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0); |
16cdcec7 MX |
1408 | if (ret) |
1409 | return; | |
1410 | ||
0353808c MX |
1411 | wait_event_interruptible(delayed_root->wait, |
1412 | could_end_wait(delayed_root, seq)); | |
4dd466d3 | 1413 | return; |
16cdcec7 MX |
1414 | } |
1415 | ||
a585e948 | 1416 | btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH); |
16cdcec7 MX |
1417 | } |
1418 | ||
79787eaa | 1419 | /* Will return 0 or -ENOMEM */ |
16cdcec7 | 1420 | int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, |
2ff7e61e | 1421 | const char *name, int name_len, |
6f45d185 | 1422 | struct btrfs_inode *dir, |
16cdcec7 MX |
1423 | struct btrfs_disk_key *disk_key, u8 type, |
1424 | u64 index) | |
1425 | { | |
1426 | struct btrfs_delayed_node *delayed_node; | |
1427 | struct btrfs_delayed_item *delayed_item; | |
1428 | struct btrfs_dir_item *dir_item; | |
1429 | int ret; | |
1430 | ||
6f45d185 | 1431 | delayed_node = btrfs_get_or_create_delayed_node(dir); |
16cdcec7 MX |
1432 | if (IS_ERR(delayed_node)) |
1433 | return PTR_ERR(delayed_node); | |
1434 | ||
1435 | delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len); | |
1436 | if (!delayed_item) { | |
1437 | ret = -ENOMEM; | |
1438 | goto release_node; | |
1439 | } | |
1440 | ||
6f45d185 | 1441 | delayed_item->key.objectid = btrfs_ino(dir); |
962a298f | 1442 | delayed_item->key.type = BTRFS_DIR_INDEX_KEY; |
16cdcec7 MX |
1443 | delayed_item->key.offset = index; |
1444 | ||
1445 | dir_item = (struct btrfs_dir_item *)delayed_item->data; | |
1446 | dir_item->location = *disk_key; | |
3cae210f QW |
1447 | btrfs_set_stack_dir_transid(dir_item, trans->transid); |
1448 | btrfs_set_stack_dir_data_len(dir_item, 0); | |
1449 | btrfs_set_stack_dir_name_len(dir_item, name_len); | |
1450 | btrfs_set_stack_dir_type(dir_item, type); | |
16cdcec7 MX |
1451 | memcpy((char *)(dir_item + 1), name, name_len); |
1452 | ||
4f5427cc | 1453 | ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, delayed_item); |
8c2a3ca2 JB |
1454 | /* |
1455 | * we have reserved enough space when we start a new transaction, | |
1456 | * so reserving metadata failure is impossible | |
1457 | */ | |
1458 | BUG_ON(ret); | |
1459 | ||
16cdcec7 MX |
1460 | mutex_lock(&delayed_node->mutex); |
1461 | ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item); | |
1462 | if (unlikely(ret)) { | |
4465c8b4 | 1463 | btrfs_err(trans->fs_info, |
5d163e0e | 1464 | "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", |
4fd786e6 | 1465 | name_len, name, delayed_node->root->root_key.objectid, |
5d163e0e | 1466 | delayed_node->inode_id, ret); |
16cdcec7 MX |
1467 | BUG(); |
1468 | } | |
1469 | mutex_unlock(&delayed_node->mutex); | |
1470 | ||
1471 | release_node: | |
1472 | btrfs_release_delayed_node(delayed_node); | |
1473 | return ret; | |
1474 | } | |
1475 | ||
2ff7e61e | 1476 | static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info, |
16cdcec7 MX |
1477 | struct btrfs_delayed_node *node, |
1478 | struct btrfs_key *key) | |
1479 | { | |
1480 | struct btrfs_delayed_item *item; | |
1481 | ||
1482 | mutex_lock(&node->mutex); | |
1483 | item = __btrfs_lookup_delayed_insertion_item(node, key); | |
1484 | if (!item) { | |
1485 | mutex_unlock(&node->mutex); | |
1486 | return 1; | |
1487 | } | |
1488 | ||
4f5427cc | 1489 | btrfs_delayed_item_release_metadata(node->root, item); |
16cdcec7 MX |
1490 | btrfs_release_delayed_item(item); |
1491 | mutex_unlock(&node->mutex); | |
1492 | return 0; | |
1493 | } | |
1494 | ||
1495 | int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, | |
e67bbbb9 | 1496 | struct btrfs_inode *dir, u64 index) |
16cdcec7 MX |
1497 | { |
1498 | struct btrfs_delayed_node *node; | |
1499 | struct btrfs_delayed_item *item; | |
1500 | struct btrfs_key item_key; | |
1501 | int ret; | |
1502 | ||
e67bbbb9 | 1503 | node = btrfs_get_or_create_delayed_node(dir); |
16cdcec7 MX |
1504 | if (IS_ERR(node)) |
1505 | return PTR_ERR(node); | |
1506 | ||
e67bbbb9 | 1507 | item_key.objectid = btrfs_ino(dir); |
962a298f | 1508 | item_key.type = BTRFS_DIR_INDEX_KEY; |
16cdcec7 MX |
1509 | item_key.offset = index; |
1510 | ||
9add2945 LF |
1511 | ret = btrfs_delete_delayed_insertion_item(trans->fs_info, node, |
1512 | &item_key); | |
16cdcec7 MX |
1513 | if (!ret) |
1514 | goto end; | |
1515 | ||
1516 | item = btrfs_alloc_delayed_item(0); | |
1517 | if (!item) { | |
1518 | ret = -ENOMEM; | |
1519 | goto end; | |
1520 | } | |
1521 | ||
1522 | item->key = item_key; | |
1523 | ||
4f5427cc | 1524 | ret = btrfs_delayed_item_reserve_metadata(trans, dir->root, item); |
16cdcec7 MX |
1525 | /* |
1526 | * we have reserved enough space when we start a new transaction, | |
1527 | * so reserving metadata failure is impossible. | |
1528 | */ | |
1529 | BUG_ON(ret); | |
1530 | ||
1531 | mutex_lock(&node->mutex); | |
1532 | ret = __btrfs_add_delayed_deletion_item(node, item); | |
1533 | if (unlikely(ret)) { | |
9add2945 | 1534 | btrfs_err(trans->fs_info, |
5d163e0e | 1535 | "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", |
4fd786e6 MT |
1536 | index, node->root->root_key.objectid, |
1537 | node->inode_id, ret); | |
16cdcec7 MX |
1538 | BUG(); |
1539 | } | |
1540 | mutex_unlock(&node->mutex); | |
1541 | end: | |
1542 | btrfs_release_delayed_node(node); | |
1543 | return ret; | |
1544 | } | |
1545 | ||
f5cc7b80 | 1546 | int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode) |
16cdcec7 | 1547 | { |
f5cc7b80 | 1548 | struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); |
16cdcec7 MX |
1549 | |
1550 | if (!delayed_node) | |
1551 | return -ENOENT; | |
1552 | ||
1553 | /* | |
1554 | * Since we have held i_mutex of this directory, it is impossible that | |
1555 | * a new directory index is added into the delayed node and index_cnt | |
1556 | * is updated now. So we needn't lock the delayed node. | |
1557 | */ | |
2f7e33d4 MX |
1558 | if (!delayed_node->index_cnt) { |
1559 | btrfs_release_delayed_node(delayed_node); | |
16cdcec7 | 1560 | return -EINVAL; |
2f7e33d4 | 1561 | } |
16cdcec7 | 1562 | |
f5cc7b80 | 1563 | inode->index_cnt = delayed_node->index_cnt; |
2f7e33d4 MX |
1564 | btrfs_release_delayed_node(delayed_node); |
1565 | return 0; | |
16cdcec7 MX |
1566 | } |
1567 | ||
02dbfc99 OS |
1568 | bool btrfs_readdir_get_delayed_items(struct inode *inode, |
1569 | struct list_head *ins_list, | |
1570 | struct list_head *del_list) | |
16cdcec7 MX |
1571 | { |
1572 | struct btrfs_delayed_node *delayed_node; | |
1573 | struct btrfs_delayed_item *item; | |
1574 | ||
340c6ca9 | 1575 | delayed_node = btrfs_get_delayed_node(BTRFS_I(inode)); |
16cdcec7 | 1576 | if (!delayed_node) |
02dbfc99 OS |
1577 | return false; |
1578 | ||
1579 | /* | |
1580 | * We can only do one readdir with delayed items at a time because of | |
1581 | * item->readdir_list. | |
1582 | */ | |
1583 | inode_unlock_shared(inode); | |
1584 | inode_lock(inode); | |
16cdcec7 MX |
1585 | |
1586 | mutex_lock(&delayed_node->mutex); | |
1587 | item = __btrfs_first_delayed_insertion_item(delayed_node); | |
1588 | while (item) { | |
089e77e1 | 1589 | refcount_inc(&item->refs); |
16cdcec7 MX |
1590 | list_add_tail(&item->readdir_list, ins_list); |
1591 | item = __btrfs_next_delayed_item(item); | |
1592 | } | |
1593 | ||
1594 | item = __btrfs_first_delayed_deletion_item(delayed_node); | |
1595 | while (item) { | |
089e77e1 | 1596 | refcount_inc(&item->refs); |
16cdcec7 MX |
1597 | list_add_tail(&item->readdir_list, del_list); |
1598 | item = __btrfs_next_delayed_item(item); | |
1599 | } | |
1600 | mutex_unlock(&delayed_node->mutex); | |
1601 | /* | |
1602 | * This delayed node is still cached in the btrfs inode, so refs | |
1603 | * must be > 1 now, and we needn't check it is going to be freed | |
1604 | * or not. | |
1605 | * | |
1606 | * Besides that, this function is used to read dir, we do not | |
1607 | * insert/delete delayed items in this period. So we also needn't | |
1608 | * requeue or dequeue this delayed node. | |
1609 | */ | |
6de5f18e | 1610 | refcount_dec(&delayed_node->refs); |
02dbfc99 OS |
1611 | |
1612 | return true; | |
16cdcec7 MX |
1613 | } |
1614 | ||
02dbfc99 OS |
1615 | void btrfs_readdir_put_delayed_items(struct inode *inode, |
1616 | struct list_head *ins_list, | |
1617 | struct list_head *del_list) | |
16cdcec7 MX |
1618 | { |
1619 | struct btrfs_delayed_item *curr, *next; | |
1620 | ||
1621 | list_for_each_entry_safe(curr, next, ins_list, readdir_list) { | |
1622 | list_del(&curr->readdir_list); | |
089e77e1 | 1623 | if (refcount_dec_and_test(&curr->refs)) |
16cdcec7 MX |
1624 | kfree(curr); |
1625 | } | |
1626 | ||
1627 | list_for_each_entry_safe(curr, next, del_list, readdir_list) { | |
1628 | list_del(&curr->readdir_list); | |
089e77e1 | 1629 | if (refcount_dec_and_test(&curr->refs)) |
16cdcec7 MX |
1630 | kfree(curr); |
1631 | } | |
02dbfc99 OS |
1632 | |
1633 | /* | |
1634 | * The VFS is going to do up_read(), so we need to downgrade back to a | |
1635 | * read lock. | |
1636 | */ | |
1637 | downgrade_write(&inode->i_rwsem); | |
16cdcec7 MX |
1638 | } |
1639 | ||
1640 | int btrfs_should_delete_dir_index(struct list_head *del_list, | |
1641 | u64 index) | |
1642 | { | |
e4fd493c JB |
1643 | struct btrfs_delayed_item *curr; |
1644 | int ret = 0; | |
16cdcec7 | 1645 | |
e4fd493c | 1646 | list_for_each_entry(curr, del_list, readdir_list) { |
16cdcec7 MX |
1647 | if (curr->key.offset > index) |
1648 | break; | |
e4fd493c JB |
1649 | if (curr->key.offset == index) { |
1650 | ret = 1; | |
1651 | break; | |
1652 | } | |
16cdcec7 | 1653 | } |
e4fd493c | 1654 | return ret; |
16cdcec7 MX |
1655 | } |
1656 | ||
1657 | /* | |
1658 | * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree | |
1659 | * | |
1660 | */ | |
9cdda8d3 | 1661 | int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, |
d2fbb2b5 | 1662 | struct list_head *ins_list) |
16cdcec7 MX |
1663 | { |
1664 | struct btrfs_dir_item *di; | |
1665 | struct btrfs_delayed_item *curr, *next; | |
1666 | struct btrfs_key location; | |
1667 | char *name; | |
1668 | int name_len; | |
1669 | int over = 0; | |
1670 | unsigned char d_type; | |
1671 | ||
1672 | if (list_empty(ins_list)) | |
1673 | return 0; | |
1674 | ||
1675 | /* | |
1676 | * Changing the data of the delayed item is impossible. So | |
1677 | * we needn't lock them. And we have held i_mutex of the | |
1678 | * directory, nobody can delete any directory indexes now. | |
1679 | */ | |
1680 | list_for_each_entry_safe(curr, next, ins_list, readdir_list) { | |
1681 | list_del(&curr->readdir_list); | |
1682 | ||
9cdda8d3 | 1683 | if (curr->key.offset < ctx->pos) { |
089e77e1 | 1684 | if (refcount_dec_and_test(&curr->refs)) |
16cdcec7 MX |
1685 | kfree(curr); |
1686 | continue; | |
1687 | } | |
1688 | ||
9cdda8d3 | 1689 | ctx->pos = curr->key.offset; |
16cdcec7 MX |
1690 | |
1691 | di = (struct btrfs_dir_item *)curr->data; | |
1692 | name = (char *)(di + 1); | |
3cae210f | 1693 | name_len = btrfs_stack_dir_name_len(di); |
16cdcec7 MX |
1694 | |
1695 | d_type = btrfs_filetype_table[di->type]; | |
1696 | btrfs_disk_key_to_cpu(&location, &di->location); | |
1697 | ||
9cdda8d3 | 1698 | over = !dir_emit(ctx, name, name_len, |
16cdcec7 MX |
1699 | location.objectid, d_type); |
1700 | ||
089e77e1 | 1701 | if (refcount_dec_and_test(&curr->refs)) |
16cdcec7 MX |
1702 | kfree(curr); |
1703 | ||
1704 | if (over) | |
1705 | return 1; | |
42e9cc46 | 1706 | ctx->pos++; |
16cdcec7 MX |
1707 | } |
1708 | return 0; | |
1709 | } | |
1710 | ||
16cdcec7 MX |
1711 | static void fill_stack_inode_item(struct btrfs_trans_handle *trans, |
1712 | struct btrfs_inode_item *inode_item, | |
1713 | struct inode *inode) | |
1714 | { | |
2f2f43d3 EB |
1715 | btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode)); |
1716 | btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode)); | |
16cdcec7 MX |
1717 | btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size); |
1718 | btrfs_set_stack_inode_mode(inode_item, inode->i_mode); | |
1719 | btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink); | |
1720 | btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode)); | |
1721 | btrfs_set_stack_inode_generation(inode_item, | |
1722 | BTRFS_I(inode)->generation); | |
c7f88c4e JL |
1723 | btrfs_set_stack_inode_sequence(inode_item, |
1724 | inode_peek_iversion(inode)); | |
16cdcec7 MX |
1725 | btrfs_set_stack_inode_transid(inode_item, trans->transid); |
1726 | btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev); | |
1727 | btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags); | |
ff5714cc | 1728 | btrfs_set_stack_inode_block_group(inode_item, 0); |
16cdcec7 | 1729 | |
a937b979 | 1730 | btrfs_set_stack_timespec_sec(&inode_item->atime, |
16cdcec7 | 1731 | inode->i_atime.tv_sec); |
a937b979 | 1732 | btrfs_set_stack_timespec_nsec(&inode_item->atime, |
16cdcec7 MX |
1733 | inode->i_atime.tv_nsec); |
1734 | ||
a937b979 | 1735 | btrfs_set_stack_timespec_sec(&inode_item->mtime, |
16cdcec7 | 1736 | inode->i_mtime.tv_sec); |
a937b979 | 1737 | btrfs_set_stack_timespec_nsec(&inode_item->mtime, |
16cdcec7 MX |
1738 | inode->i_mtime.tv_nsec); |
1739 | ||
a937b979 | 1740 | btrfs_set_stack_timespec_sec(&inode_item->ctime, |
16cdcec7 | 1741 | inode->i_ctime.tv_sec); |
a937b979 | 1742 | btrfs_set_stack_timespec_nsec(&inode_item->ctime, |
16cdcec7 | 1743 | inode->i_ctime.tv_nsec); |
9cc97d64 | 1744 | |
1745 | btrfs_set_stack_timespec_sec(&inode_item->otime, | |
1746 | BTRFS_I(inode)->i_otime.tv_sec); | |
1747 | btrfs_set_stack_timespec_nsec(&inode_item->otime, | |
1748 | BTRFS_I(inode)->i_otime.tv_nsec); | |
16cdcec7 MX |
1749 | } |
1750 | ||
2f7e33d4 MX |
1751 | int btrfs_fill_inode(struct inode *inode, u32 *rdev) |
1752 | { | |
1753 | struct btrfs_delayed_node *delayed_node; | |
1754 | struct btrfs_inode_item *inode_item; | |
2f7e33d4 | 1755 | |
340c6ca9 | 1756 | delayed_node = btrfs_get_delayed_node(BTRFS_I(inode)); |
2f7e33d4 MX |
1757 | if (!delayed_node) |
1758 | return -ENOENT; | |
1759 | ||
1760 | mutex_lock(&delayed_node->mutex); | |
7cf35d91 | 1761 | if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
2f7e33d4 MX |
1762 | mutex_unlock(&delayed_node->mutex); |
1763 | btrfs_release_delayed_node(delayed_node); | |
1764 | return -ENOENT; | |
1765 | } | |
1766 | ||
1767 | inode_item = &delayed_node->inode_item; | |
1768 | ||
2f2f43d3 EB |
1769 | i_uid_write(inode, btrfs_stack_inode_uid(inode_item)); |
1770 | i_gid_write(inode, btrfs_stack_inode_gid(inode_item)); | |
6ef06d27 | 1771 | btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item)); |
2f7e33d4 | 1772 | inode->i_mode = btrfs_stack_inode_mode(inode_item); |
bfe86848 | 1773 | set_nlink(inode, btrfs_stack_inode_nlink(inode_item)); |
2f7e33d4 MX |
1774 | inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item)); |
1775 | BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item); | |
6e17d30b YD |
1776 | BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item); |
1777 | ||
c7f88c4e JL |
1778 | inode_set_iversion_queried(inode, |
1779 | btrfs_stack_inode_sequence(inode_item)); | |
2f7e33d4 MX |
1780 | inode->i_rdev = 0; |
1781 | *rdev = btrfs_stack_inode_rdev(inode_item); | |
1782 | BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item); | |
1783 | ||
a937b979 DS |
1784 | inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime); |
1785 | inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime); | |
2f7e33d4 | 1786 | |
a937b979 DS |
1787 | inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime); |
1788 | inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime); | |
2f7e33d4 | 1789 | |
a937b979 DS |
1790 | inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime); |
1791 | inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime); | |
2f7e33d4 | 1792 | |
9cc97d64 | 1793 | BTRFS_I(inode)->i_otime.tv_sec = |
1794 | btrfs_stack_timespec_sec(&inode_item->otime); | |
1795 | BTRFS_I(inode)->i_otime.tv_nsec = | |
1796 | btrfs_stack_timespec_nsec(&inode_item->otime); | |
1797 | ||
2f7e33d4 MX |
1798 | inode->i_generation = BTRFS_I(inode)->generation; |
1799 | BTRFS_I(inode)->index_cnt = (u64)-1; | |
1800 | ||
1801 | mutex_unlock(&delayed_node->mutex); | |
1802 | btrfs_release_delayed_node(delayed_node); | |
1803 | return 0; | |
1804 | } | |
1805 | ||
16cdcec7 MX |
1806 | int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans, |
1807 | struct btrfs_root *root, struct inode *inode) | |
1808 | { | |
1809 | struct btrfs_delayed_node *delayed_node; | |
aa0467d8 | 1810 | int ret = 0; |
16cdcec7 | 1811 | |
e5517a7b | 1812 | delayed_node = btrfs_get_or_create_delayed_node(BTRFS_I(inode)); |
16cdcec7 MX |
1813 | if (IS_ERR(delayed_node)) |
1814 | return PTR_ERR(delayed_node); | |
1815 | ||
1816 | mutex_lock(&delayed_node->mutex); | |
7cf35d91 | 1817 | if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
16cdcec7 MX |
1818 | fill_stack_inode_item(trans, &delayed_node->inode_item, inode); |
1819 | goto release_node; | |
1820 | } | |
1821 | ||
fcabdd1c | 1822 | ret = btrfs_delayed_inode_reserve_metadata(trans, root, BTRFS_I(inode), |
7fd2ae21 | 1823 | delayed_node); |
c06a0e12 JB |
1824 | if (ret) |
1825 | goto release_node; | |
16cdcec7 MX |
1826 | |
1827 | fill_stack_inode_item(trans, &delayed_node->inode_item, inode); | |
7cf35d91 | 1828 | set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags); |
16cdcec7 MX |
1829 | delayed_node->count++; |
1830 | atomic_inc(&root->fs_info->delayed_root->items); | |
1831 | release_node: | |
1832 | mutex_unlock(&delayed_node->mutex); | |
1833 | btrfs_release_delayed_node(delayed_node); | |
1834 | return ret; | |
1835 | } | |
1836 | ||
e07222c7 | 1837 | int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode) |
67de1176 | 1838 | { |
3ffbd68c | 1839 | struct btrfs_fs_info *fs_info = inode->root->fs_info; |
67de1176 MX |
1840 | struct btrfs_delayed_node *delayed_node; |
1841 | ||
6f896054 CM |
1842 | /* |
1843 | * we don't do delayed inode updates during log recovery because it | |
1844 | * leads to enospc problems. This means we also can't do | |
1845 | * delayed inode refs | |
1846 | */ | |
0b246afa | 1847 | if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) |
6f896054 CM |
1848 | return -EAGAIN; |
1849 | ||
e07222c7 | 1850 | delayed_node = btrfs_get_or_create_delayed_node(inode); |
67de1176 MX |
1851 | if (IS_ERR(delayed_node)) |
1852 | return PTR_ERR(delayed_node); | |
1853 | ||
1854 | /* | |
1855 | * We don't reserve space for inode ref deletion is because: | |
1856 | * - We ONLY do async inode ref deletion for the inode who has only | |
1857 | * one link(i_nlink == 1), it means there is only one inode ref. | |
1858 | * And in most case, the inode ref and the inode item are in the | |
1859 | * same leaf, and we will deal with them at the same time. | |
1860 | * Since we are sure we will reserve the space for the inode item, | |
1861 | * it is unnecessary to reserve space for inode ref deletion. | |
1862 | * - If the inode ref and the inode item are not in the same leaf, | |
1863 | * We also needn't worry about enospc problem, because we reserve | |
1864 | * much more space for the inode update than it needs. | |
1865 | * - At the worst, we can steal some space from the global reservation. | |
1866 | * It is very rare. | |
1867 | */ | |
1868 | mutex_lock(&delayed_node->mutex); | |
1869 | if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) | |
1870 | goto release_node; | |
1871 | ||
1872 | set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags); | |
1873 | delayed_node->count++; | |
0b246afa | 1874 | atomic_inc(&fs_info->delayed_root->items); |
67de1176 MX |
1875 | release_node: |
1876 | mutex_unlock(&delayed_node->mutex); | |
1877 | btrfs_release_delayed_node(delayed_node); | |
1878 | return 0; | |
1879 | } | |
1880 | ||
16cdcec7 MX |
1881 | static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node) |
1882 | { | |
1883 | struct btrfs_root *root = delayed_node->root; | |
2ff7e61e | 1884 | struct btrfs_fs_info *fs_info = root->fs_info; |
16cdcec7 MX |
1885 | struct btrfs_delayed_item *curr_item, *prev_item; |
1886 | ||
1887 | mutex_lock(&delayed_node->mutex); | |
1888 | curr_item = __btrfs_first_delayed_insertion_item(delayed_node); | |
1889 | while (curr_item) { | |
4f5427cc | 1890 | btrfs_delayed_item_release_metadata(root, curr_item); |
16cdcec7 MX |
1891 | prev_item = curr_item; |
1892 | curr_item = __btrfs_next_delayed_item(prev_item); | |
1893 | btrfs_release_delayed_item(prev_item); | |
1894 | } | |
1895 | ||
1896 | curr_item = __btrfs_first_delayed_deletion_item(delayed_node); | |
1897 | while (curr_item) { | |
4f5427cc | 1898 | btrfs_delayed_item_release_metadata(root, curr_item); |
16cdcec7 MX |
1899 | prev_item = curr_item; |
1900 | curr_item = __btrfs_next_delayed_item(prev_item); | |
1901 | btrfs_release_delayed_item(prev_item); | |
1902 | } | |
1903 | ||
67de1176 MX |
1904 | if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags)) |
1905 | btrfs_release_delayed_iref(delayed_node); | |
1906 | ||
7cf35d91 | 1907 | if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { |
4f5427cc | 1908 | btrfs_delayed_inode_release_metadata(fs_info, delayed_node, false); |
16cdcec7 MX |
1909 | btrfs_release_delayed_inode(delayed_node); |
1910 | } | |
1911 | mutex_unlock(&delayed_node->mutex); | |
1912 | } | |
1913 | ||
4ccb5c72 | 1914 | void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode) |
16cdcec7 MX |
1915 | { |
1916 | struct btrfs_delayed_node *delayed_node; | |
1917 | ||
4ccb5c72 | 1918 | delayed_node = btrfs_get_delayed_node(inode); |
16cdcec7 MX |
1919 | if (!delayed_node) |
1920 | return; | |
1921 | ||
1922 | __btrfs_kill_delayed_node(delayed_node); | |
1923 | btrfs_release_delayed_node(delayed_node); | |
1924 | } | |
1925 | ||
1926 | void btrfs_kill_all_delayed_nodes(struct btrfs_root *root) | |
1927 | { | |
1928 | u64 inode_id = 0; | |
1929 | struct btrfs_delayed_node *delayed_nodes[8]; | |
1930 | int i, n; | |
1931 | ||
1932 | while (1) { | |
1933 | spin_lock(&root->inode_lock); | |
1934 | n = radix_tree_gang_lookup(&root->delayed_nodes_tree, | |
1935 | (void **)delayed_nodes, inode_id, | |
1936 | ARRAY_SIZE(delayed_nodes)); | |
1937 | if (!n) { | |
1938 | spin_unlock(&root->inode_lock); | |
1939 | break; | |
1940 | } | |
1941 | ||
1942 | inode_id = delayed_nodes[n - 1]->inode_id + 1; | |
1943 | ||
1944 | for (i = 0; i < n; i++) | |
6de5f18e | 1945 | refcount_inc(&delayed_nodes[i]->refs); |
16cdcec7 MX |
1946 | spin_unlock(&root->inode_lock); |
1947 | ||
1948 | for (i = 0; i < n; i++) { | |
1949 | __btrfs_kill_delayed_node(delayed_nodes[i]); | |
1950 | btrfs_release_delayed_node(delayed_nodes[i]); | |
1951 | } | |
1952 | } | |
1953 | } | |
67cde344 | 1954 | |
ccdf9b30 | 1955 | void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info) |
67cde344 | 1956 | { |
67cde344 MX |
1957 | struct btrfs_delayed_node *curr_node, *prev_node; |
1958 | ||
ccdf9b30 | 1959 | curr_node = btrfs_first_delayed_node(fs_info->delayed_root); |
67cde344 MX |
1960 | while (curr_node) { |
1961 | __btrfs_kill_delayed_node(curr_node); | |
1962 | ||
1963 | prev_node = curr_node; | |
1964 | curr_node = btrfs_next_delayed_node(curr_node); | |
1965 | btrfs_release_delayed_node(prev_node); | |
1966 | } | |
1967 | } | |
1968 |