Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / fs / btrfs / delayed-inode.c
blob5d73f79ded8bcbd967636b652d98433da64cceb8
1 /*
2 * Copyright (C) 2011 Fujitsu. All rights reserved.
3 * Written by Miao Xie <miaox@cn.fujitsu.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
20 #include <linux/slab.h>
21 #include "delayed-inode.h"
22 #include "disk-io.h"
23 #include "transaction.h"
24 #include "ctree.h"
26 #define BTRFS_DELAYED_WRITEBACK 512
27 #define BTRFS_DELAYED_BACKGROUND 128
28 #define BTRFS_DELAYED_BATCH 16
30 static struct kmem_cache *delayed_node_cache;
32 int __init btrfs_delayed_inode_init(void)
34 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
35 sizeof(struct btrfs_delayed_node),
37 SLAB_MEM_SPREAD,
38 NULL);
39 if (!delayed_node_cache)
40 return -ENOMEM;
41 return 0;
44 void btrfs_delayed_inode_exit(void)
46 kmem_cache_destroy(delayed_node_cache);
49 static inline void btrfs_init_delayed_node(
50 struct btrfs_delayed_node *delayed_node,
51 struct btrfs_root *root, u64 inode_id)
53 delayed_node->root = root;
54 delayed_node->inode_id = inode_id;
55 refcount_set(&delayed_node->refs, 0);
56 delayed_node->ins_root = RB_ROOT;
57 delayed_node->del_root = RB_ROOT;
58 mutex_init(&delayed_node->mutex);
59 INIT_LIST_HEAD(&delayed_node->n_list);
60 INIT_LIST_HEAD(&delayed_node->p_list);
63 static inline int btrfs_is_continuous_delayed_item(
64 struct btrfs_delayed_item *item1,
65 struct btrfs_delayed_item *item2)
67 if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
68 item1->key.objectid == item2->key.objectid &&
69 item1->key.type == item2->key.type &&
70 item1->key.offset + 1 == item2->key.offset)
71 return 1;
72 return 0;
75 static struct btrfs_delayed_node *btrfs_get_delayed_node(
76 struct btrfs_inode *btrfs_inode)
78 struct btrfs_root *root = btrfs_inode->root;
79 u64 ino = btrfs_ino(btrfs_inode);
80 struct btrfs_delayed_node *node;
82 node = READ_ONCE(btrfs_inode->delayed_node);
83 if (node) {
84 refcount_inc(&node->refs);
85 return node;
88 spin_lock(&root->inode_lock);
89 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
90 if (node) {
91 if (btrfs_inode->delayed_node) {
92 refcount_inc(&node->refs); /* can be accessed */
93 BUG_ON(btrfs_inode->delayed_node != node);
94 spin_unlock(&root->inode_lock);
95 return node;
97 btrfs_inode->delayed_node = node;
98 /* can be accessed and cached in the inode */
99 refcount_add(2, &node->refs);
100 spin_unlock(&root->inode_lock);
101 return node;
103 spin_unlock(&root->inode_lock);
105 return NULL;
108 /* Will return either the node or PTR_ERR(-ENOMEM) */
109 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
110 struct btrfs_inode *btrfs_inode)
112 struct btrfs_delayed_node *node;
113 struct btrfs_root *root = btrfs_inode->root;
114 u64 ino = btrfs_ino(btrfs_inode);
115 int ret;
117 again:
118 node = btrfs_get_delayed_node(btrfs_inode);
119 if (node)
120 return node;
122 node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
123 if (!node)
124 return ERR_PTR(-ENOMEM);
125 btrfs_init_delayed_node(node, root, ino);
127 /* cached in the btrfs inode and can be accessed */
128 refcount_set(&node->refs, 2);
130 ret = radix_tree_preload(GFP_NOFS);
131 if (ret) {
132 kmem_cache_free(delayed_node_cache, node);
133 return ERR_PTR(ret);
136 spin_lock(&root->inode_lock);
137 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
138 if (ret == -EEXIST) {
139 spin_unlock(&root->inode_lock);
140 kmem_cache_free(delayed_node_cache, node);
141 radix_tree_preload_end();
142 goto again;
144 btrfs_inode->delayed_node = node;
145 spin_unlock(&root->inode_lock);
146 radix_tree_preload_end();
148 return node;
152 * Call it when holding delayed_node->mutex
154 * If mod = 1, add this node into the prepared list.
156 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
157 struct btrfs_delayed_node *node,
158 int mod)
160 spin_lock(&root->lock);
161 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
162 if (!list_empty(&node->p_list))
163 list_move_tail(&node->p_list, &root->prepare_list);
164 else if (mod)
165 list_add_tail(&node->p_list, &root->prepare_list);
166 } else {
167 list_add_tail(&node->n_list, &root->node_list);
168 list_add_tail(&node->p_list, &root->prepare_list);
169 refcount_inc(&node->refs); /* inserted into list */
170 root->nodes++;
171 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
173 spin_unlock(&root->lock);
176 /* Call it when holding delayed_node->mutex */
177 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
178 struct btrfs_delayed_node *node)
180 spin_lock(&root->lock);
181 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
182 root->nodes--;
183 refcount_dec(&node->refs); /* not in the list */
184 list_del_init(&node->n_list);
185 if (!list_empty(&node->p_list))
186 list_del_init(&node->p_list);
187 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
189 spin_unlock(&root->lock);
192 static struct btrfs_delayed_node *btrfs_first_delayed_node(
193 struct btrfs_delayed_root *delayed_root)
195 struct list_head *p;
196 struct btrfs_delayed_node *node = NULL;
198 spin_lock(&delayed_root->lock);
199 if (list_empty(&delayed_root->node_list))
200 goto out;
202 p = delayed_root->node_list.next;
203 node = list_entry(p, struct btrfs_delayed_node, n_list);
204 refcount_inc(&node->refs);
205 out:
206 spin_unlock(&delayed_root->lock);
208 return node;
211 static struct btrfs_delayed_node *btrfs_next_delayed_node(
212 struct btrfs_delayed_node *node)
214 struct btrfs_delayed_root *delayed_root;
215 struct list_head *p;
216 struct btrfs_delayed_node *next = NULL;
218 delayed_root = node->root->fs_info->delayed_root;
219 spin_lock(&delayed_root->lock);
220 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
221 /* not in the list */
222 if (list_empty(&delayed_root->node_list))
223 goto out;
224 p = delayed_root->node_list.next;
225 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
226 goto out;
227 else
228 p = node->n_list.next;
230 next = list_entry(p, struct btrfs_delayed_node, n_list);
231 refcount_inc(&next->refs);
232 out:
233 spin_unlock(&delayed_root->lock);
235 return next;
238 static void __btrfs_release_delayed_node(
239 struct btrfs_delayed_node *delayed_node,
240 int mod)
242 struct btrfs_delayed_root *delayed_root;
244 if (!delayed_node)
245 return;
247 delayed_root = delayed_node->root->fs_info->delayed_root;
249 mutex_lock(&delayed_node->mutex);
250 if (delayed_node->count)
251 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
252 else
253 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
254 mutex_unlock(&delayed_node->mutex);
256 if (refcount_dec_and_test(&delayed_node->refs)) {
257 bool free = false;
258 struct btrfs_root *root = delayed_node->root;
259 spin_lock(&root->inode_lock);
260 if (refcount_read(&delayed_node->refs) == 0) {
261 radix_tree_delete(&root->delayed_nodes_tree,
262 delayed_node->inode_id);
263 free = true;
265 spin_unlock(&root->inode_lock);
266 if (free)
267 kmem_cache_free(delayed_node_cache, delayed_node);
271 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
273 __btrfs_release_delayed_node(node, 0);
276 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
277 struct btrfs_delayed_root *delayed_root)
279 struct list_head *p;
280 struct btrfs_delayed_node *node = NULL;
282 spin_lock(&delayed_root->lock);
283 if (list_empty(&delayed_root->prepare_list))
284 goto out;
286 p = delayed_root->prepare_list.next;
287 list_del_init(p);
288 node = list_entry(p, struct btrfs_delayed_node, p_list);
289 refcount_inc(&node->refs);
290 out:
291 spin_unlock(&delayed_root->lock);
293 return node;
296 static inline void btrfs_release_prepared_delayed_node(
297 struct btrfs_delayed_node *node)
299 __btrfs_release_delayed_node(node, 1);
302 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
304 struct btrfs_delayed_item *item;
305 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
306 if (item) {
307 item->data_len = data_len;
308 item->ins_or_del = 0;
309 item->bytes_reserved = 0;
310 item->delayed_node = NULL;
311 refcount_set(&item->refs, 1);
313 return item;
317 * __btrfs_lookup_delayed_item - look up the delayed item by key
318 * @delayed_node: pointer to the delayed node
319 * @key: the key to look up
320 * @prev: used to store the prev item if the right item isn't found
321 * @next: used to store the next item if the right item isn't found
323 * Note: if we don't find the right item, we will return the prev item and
324 * the next item.
326 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
327 struct rb_root *root,
328 struct btrfs_key *key,
329 struct btrfs_delayed_item **prev,
330 struct btrfs_delayed_item **next)
332 struct rb_node *node, *prev_node = NULL;
333 struct btrfs_delayed_item *delayed_item = NULL;
334 int ret = 0;
336 node = root->rb_node;
338 while (node) {
339 delayed_item = rb_entry(node, struct btrfs_delayed_item,
340 rb_node);
341 prev_node = node;
342 ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
343 if (ret < 0)
344 node = node->rb_right;
345 else if (ret > 0)
346 node = node->rb_left;
347 else
348 return delayed_item;
351 if (prev) {
352 if (!prev_node)
353 *prev = NULL;
354 else if (ret < 0)
355 *prev = delayed_item;
356 else if ((node = rb_prev(prev_node)) != NULL) {
357 *prev = rb_entry(node, struct btrfs_delayed_item,
358 rb_node);
359 } else
360 *prev = NULL;
363 if (next) {
364 if (!prev_node)
365 *next = NULL;
366 else if (ret > 0)
367 *next = delayed_item;
368 else if ((node = rb_next(prev_node)) != NULL) {
369 *next = rb_entry(node, struct btrfs_delayed_item,
370 rb_node);
371 } else
372 *next = NULL;
374 return NULL;
377 static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
378 struct btrfs_delayed_node *delayed_node,
379 struct btrfs_key *key)
381 return __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
382 NULL, NULL);
385 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
386 struct btrfs_delayed_item *ins,
387 int action)
389 struct rb_node **p, *node;
390 struct rb_node *parent_node = NULL;
391 struct rb_root *root;
392 struct btrfs_delayed_item *item;
393 int cmp;
395 if (action == BTRFS_DELAYED_INSERTION_ITEM)
396 root = &delayed_node->ins_root;
397 else if (action == BTRFS_DELAYED_DELETION_ITEM)
398 root = &delayed_node->del_root;
399 else
400 BUG();
401 p = &root->rb_node;
402 node = &ins->rb_node;
404 while (*p) {
405 parent_node = *p;
406 item = rb_entry(parent_node, struct btrfs_delayed_item,
407 rb_node);
409 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
410 if (cmp < 0)
411 p = &(*p)->rb_right;
412 else if (cmp > 0)
413 p = &(*p)->rb_left;
414 else
415 return -EEXIST;
418 rb_link_node(node, parent_node, p);
419 rb_insert_color(node, root);
420 ins->delayed_node = delayed_node;
421 ins->ins_or_del = action;
423 if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
424 action == BTRFS_DELAYED_INSERTION_ITEM &&
425 ins->key.offset >= delayed_node->index_cnt)
426 delayed_node->index_cnt = ins->key.offset + 1;
428 delayed_node->count++;
429 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
430 return 0;
433 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
434 struct btrfs_delayed_item *item)
436 return __btrfs_add_delayed_item(node, item,
437 BTRFS_DELAYED_INSERTION_ITEM);
440 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
441 struct btrfs_delayed_item *item)
443 return __btrfs_add_delayed_item(node, item,
444 BTRFS_DELAYED_DELETION_ITEM);
447 static void finish_one_item(struct btrfs_delayed_root *delayed_root)
449 int seq = atomic_inc_return(&delayed_root->items_seq);
452 * atomic_dec_return implies a barrier for waitqueue_active
454 if ((atomic_dec_return(&delayed_root->items) <
455 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
456 waitqueue_active(&delayed_root->wait))
457 wake_up(&delayed_root->wait);
460 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
462 struct rb_root *root;
463 struct btrfs_delayed_root *delayed_root;
465 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
467 BUG_ON(!delayed_root);
468 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
469 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
471 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
472 root = &delayed_item->delayed_node->ins_root;
473 else
474 root = &delayed_item->delayed_node->del_root;
476 rb_erase(&delayed_item->rb_node, root);
477 delayed_item->delayed_node->count--;
479 finish_one_item(delayed_root);
482 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
484 if (item) {
485 __btrfs_remove_delayed_item(item);
486 if (refcount_dec_and_test(&item->refs))
487 kfree(item);
491 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
492 struct btrfs_delayed_node *delayed_node)
494 struct rb_node *p;
495 struct btrfs_delayed_item *item = NULL;
497 p = rb_first(&delayed_node->ins_root);
498 if (p)
499 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
501 return item;
504 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
505 struct btrfs_delayed_node *delayed_node)
507 struct rb_node *p;
508 struct btrfs_delayed_item *item = NULL;
510 p = rb_first(&delayed_node->del_root);
511 if (p)
512 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
514 return item;
517 static struct btrfs_delayed_item *__btrfs_next_delayed_item(
518 struct btrfs_delayed_item *item)
520 struct rb_node *p;
521 struct btrfs_delayed_item *next = NULL;
523 p = rb_next(&item->rb_node);
524 if (p)
525 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
527 return next;
530 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
531 struct btrfs_fs_info *fs_info,
532 struct btrfs_delayed_item *item)
534 struct btrfs_block_rsv *src_rsv;
535 struct btrfs_block_rsv *dst_rsv;
536 u64 num_bytes;
537 int ret;
539 if (!trans->bytes_reserved)
540 return 0;
542 src_rsv = trans->block_rsv;
543 dst_rsv = &fs_info->delayed_block_rsv;
545 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
546 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
547 if (!ret) {
548 trace_btrfs_space_reservation(fs_info, "delayed_item",
549 item->key.objectid,
550 num_bytes, 1);
551 item->bytes_reserved = num_bytes;
554 return ret;
557 static void btrfs_delayed_item_release_metadata(struct btrfs_fs_info *fs_info,
558 struct btrfs_delayed_item *item)
560 struct btrfs_block_rsv *rsv;
562 if (!item->bytes_reserved)
563 return;
565 rsv = &fs_info->delayed_block_rsv;
566 trace_btrfs_space_reservation(fs_info, "delayed_item",
567 item->key.objectid, item->bytes_reserved,
569 btrfs_block_rsv_release(fs_info, rsv,
570 item->bytes_reserved);
573 static int btrfs_delayed_inode_reserve_metadata(
574 struct btrfs_trans_handle *trans,
575 struct btrfs_root *root,
576 struct btrfs_inode *inode,
577 struct btrfs_delayed_node *node)
579 struct btrfs_fs_info *fs_info = root->fs_info;
580 struct btrfs_block_rsv *src_rsv;
581 struct btrfs_block_rsv *dst_rsv;
582 u64 num_bytes;
583 int ret;
585 src_rsv = trans->block_rsv;
586 dst_rsv = &fs_info->delayed_block_rsv;
588 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
591 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
592 * which doesn't reserve space for speed. This is a problem since we
593 * still need to reserve space for this update, so try to reserve the
594 * space.
596 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
597 * we always reserve enough to update the inode item.
599 if (!src_rsv || (!trans->bytes_reserved &&
600 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
601 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
602 BTRFS_RESERVE_NO_FLUSH);
604 * Since we're under a transaction reserve_metadata_bytes could
605 * try to commit the transaction which will make it return
606 * EAGAIN to make us stop the transaction we have, so return
607 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
609 if (ret == -EAGAIN)
610 ret = -ENOSPC;
611 if (!ret) {
612 node->bytes_reserved = num_bytes;
613 trace_btrfs_space_reservation(fs_info,
614 "delayed_inode",
615 btrfs_ino(inode),
616 num_bytes, 1);
618 return ret;
621 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
622 if (!ret) {
623 trace_btrfs_space_reservation(fs_info, "delayed_inode",
624 btrfs_ino(inode), num_bytes, 1);
625 node->bytes_reserved = num_bytes;
628 return ret;
631 static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
632 struct btrfs_delayed_node *node)
634 struct btrfs_block_rsv *rsv;
636 if (!node->bytes_reserved)
637 return;
639 rsv = &fs_info->delayed_block_rsv;
640 trace_btrfs_space_reservation(fs_info, "delayed_inode",
641 node->inode_id, node->bytes_reserved, 0);
642 btrfs_block_rsv_release(fs_info, rsv,
643 node->bytes_reserved);
644 node->bytes_reserved = 0;
648 * This helper will insert some continuous items into the same leaf according
649 * to the free space of the leaf.
651 static int btrfs_batch_insert_items(struct btrfs_root *root,
652 struct btrfs_path *path,
653 struct btrfs_delayed_item *item)
655 struct btrfs_fs_info *fs_info = root->fs_info;
656 struct btrfs_delayed_item *curr, *next;
657 int free_space;
658 int total_data_size = 0, total_size = 0;
659 struct extent_buffer *leaf;
660 char *data_ptr;
661 struct btrfs_key *keys;
662 u32 *data_size;
663 struct list_head head;
664 int slot;
665 int nitems;
666 int i;
667 int ret = 0;
669 BUG_ON(!path->nodes[0]);
671 leaf = path->nodes[0];
672 free_space = btrfs_leaf_free_space(fs_info, leaf);
673 INIT_LIST_HEAD(&head);
675 next = item;
676 nitems = 0;
679 * count the number of the continuous items that we can insert in batch
681 while (total_size + next->data_len + sizeof(struct btrfs_item) <=
682 free_space) {
683 total_data_size += next->data_len;
684 total_size += next->data_len + sizeof(struct btrfs_item);
685 list_add_tail(&next->tree_list, &head);
686 nitems++;
688 curr = next;
689 next = __btrfs_next_delayed_item(curr);
690 if (!next)
691 break;
693 if (!btrfs_is_continuous_delayed_item(curr, next))
694 break;
697 if (!nitems) {
698 ret = 0;
699 goto out;
703 * we need allocate some memory space, but it might cause the task
704 * to sleep, so we set all locked nodes in the path to blocking locks
705 * first.
707 btrfs_set_path_blocking(path);
709 keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
710 if (!keys) {
711 ret = -ENOMEM;
712 goto out;
715 data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
716 if (!data_size) {
717 ret = -ENOMEM;
718 goto error;
721 /* get keys of all the delayed items */
722 i = 0;
723 list_for_each_entry(next, &head, tree_list) {
724 keys[i] = next->key;
725 data_size[i] = next->data_len;
726 i++;
729 /* reset all the locked nodes in the patch to spinning locks. */
730 btrfs_clear_path_blocking(path, NULL, 0);
732 /* insert the keys of the items */
733 setup_items_for_insert(root, path, keys, data_size,
734 total_data_size, total_size, nitems);
736 /* insert the dir index items */
737 slot = path->slots[0];
738 list_for_each_entry_safe(curr, next, &head, tree_list) {
739 data_ptr = btrfs_item_ptr(leaf, slot, char);
740 write_extent_buffer(leaf, &curr->data,
741 (unsigned long)data_ptr,
742 curr->data_len);
743 slot++;
745 btrfs_delayed_item_release_metadata(fs_info, curr);
747 list_del(&curr->tree_list);
748 btrfs_release_delayed_item(curr);
751 error:
752 kfree(data_size);
753 kfree(keys);
754 out:
755 return ret;
759 * This helper can just do simple insertion that needn't extend item for new
760 * data, such as directory name index insertion, inode insertion.
762 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
763 struct btrfs_root *root,
764 struct btrfs_path *path,
765 struct btrfs_delayed_item *delayed_item)
767 struct btrfs_fs_info *fs_info = root->fs_info;
768 struct extent_buffer *leaf;
769 char *ptr;
770 int ret;
772 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
773 delayed_item->data_len);
774 if (ret < 0 && ret != -EEXIST)
775 return ret;
777 leaf = path->nodes[0];
779 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
781 write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
782 delayed_item->data_len);
783 btrfs_mark_buffer_dirty(leaf);
785 btrfs_delayed_item_release_metadata(fs_info, delayed_item);
786 return 0;
790 * we insert an item first, then if there are some continuous items, we try
791 * to insert those items into the same leaf.
793 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
794 struct btrfs_path *path,
795 struct btrfs_root *root,
796 struct btrfs_delayed_node *node)
798 struct btrfs_delayed_item *curr, *prev;
799 int ret = 0;
801 do_again:
802 mutex_lock(&node->mutex);
803 curr = __btrfs_first_delayed_insertion_item(node);
804 if (!curr)
805 goto insert_end;
807 ret = btrfs_insert_delayed_item(trans, root, path, curr);
808 if (ret < 0) {
809 btrfs_release_path(path);
810 goto insert_end;
813 prev = curr;
814 curr = __btrfs_next_delayed_item(prev);
815 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
816 /* insert the continuous items into the same leaf */
817 path->slots[0]++;
818 btrfs_batch_insert_items(root, path, curr);
820 btrfs_release_delayed_item(prev);
821 btrfs_mark_buffer_dirty(path->nodes[0]);
823 btrfs_release_path(path);
824 mutex_unlock(&node->mutex);
825 goto do_again;
827 insert_end:
828 mutex_unlock(&node->mutex);
829 return ret;
832 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root,
834 struct btrfs_path *path,
835 struct btrfs_delayed_item *item)
837 struct btrfs_fs_info *fs_info = root->fs_info;
838 struct btrfs_delayed_item *curr, *next;
839 struct extent_buffer *leaf;
840 struct btrfs_key key;
841 struct list_head head;
842 int nitems, i, last_item;
843 int ret = 0;
845 BUG_ON(!path->nodes[0]);
847 leaf = path->nodes[0];
849 i = path->slots[0];
850 last_item = btrfs_header_nritems(leaf) - 1;
851 if (i > last_item)
852 return -ENOENT; /* FIXME: Is errno suitable? */
854 next = item;
855 INIT_LIST_HEAD(&head);
856 btrfs_item_key_to_cpu(leaf, &key, i);
857 nitems = 0;
859 * count the number of the dir index items that we can delete in batch
861 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
862 list_add_tail(&next->tree_list, &head);
863 nitems++;
865 curr = next;
866 next = __btrfs_next_delayed_item(curr);
867 if (!next)
868 break;
870 if (!btrfs_is_continuous_delayed_item(curr, next))
871 break;
873 i++;
874 if (i > last_item)
875 break;
876 btrfs_item_key_to_cpu(leaf, &key, i);
879 if (!nitems)
880 return 0;
882 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
883 if (ret)
884 goto out;
886 list_for_each_entry_safe(curr, next, &head, tree_list) {
887 btrfs_delayed_item_release_metadata(fs_info, curr);
888 list_del(&curr->tree_list);
889 btrfs_release_delayed_item(curr);
892 out:
893 return ret;
896 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
897 struct btrfs_path *path,
898 struct btrfs_root *root,
899 struct btrfs_delayed_node *node)
901 struct btrfs_delayed_item *curr, *prev;
902 int ret = 0;
904 do_again:
905 mutex_lock(&node->mutex);
906 curr = __btrfs_first_delayed_deletion_item(node);
907 if (!curr)
908 goto delete_fail;
910 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
911 if (ret < 0)
912 goto delete_fail;
913 else if (ret > 0) {
915 * can't find the item which the node points to, so this node
916 * is invalid, just drop it.
918 prev = curr;
919 curr = __btrfs_next_delayed_item(prev);
920 btrfs_release_delayed_item(prev);
921 ret = 0;
922 btrfs_release_path(path);
923 if (curr) {
924 mutex_unlock(&node->mutex);
925 goto do_again;
926 } else
927 goto delete_fail;
930 btrfs_batch_delete_items(trans, root, path, curr);
931 btrfs_release_path(path);
932 mutex_unlock(&node->mutex);
933 goto do_again;
935 delete_fail:
936 btrfs_release_path(path);
937 mutex_unlock(&node->mutex);
938 return ret;
941 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
943 struct btrfs_delayed_root *delayed_root;
945 if (delayed_node &&
946 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
947 BUG_ON(!delayed_node->root);
948 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
949 delayed_node->count--;
951 delayed_root = delayed_node->root->fs_info->delayed_root;
952 finish_one_item(delayed_root);
956 static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
958 struct btrfs_delayed_root *delayed_root;
960 ASSERT(delayed_node->root);
961 clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
962 delayed_node->count--;
964 delayed_root = delayed_node->root->fs_info->delayed_root;
965 finish_one_item(delayed_root);
968 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
969 struct btrfs_root *root,
970 struct btrfs_path *path,
971 struct btrfs_delayed_node *node)
973 struct btrfs_fs_info *fs_info = root->fs_info;
974 struct btrfs_key key;
975 struct btrfs_inode_item *inode_item;
976 struct extent_buffer *leaf;
977 int mod;
978 int ret;
980 key.objectid = node->inode_id;
981 key.type = BTRFS_INODE_ITEM_KEY;
982 key.offset = 0;
984 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
985 mod = -1;
986 else
987 mod = 1;
989 ret = btrfs_lookup_inode(trans, root, path, &key, mod);
990 if (ret > 0) {
991 btrfs_release_path(path);
992 return -ENOENT;
993 } else if (ret < 0) {
994 return ret;
997 leaf = path->nodes[0];
998 inode_item = btrfs_item_ptr(leaf, path->slots[0],
999 struct btrfs_inode_item);
1000 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1001 sizeof(struct btrfs_inode_item));
1002 btrfs_mark_buffer_dirty(leaf);
1004 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1005 goto no_iref;
1007 path->slots[0]++;
1008 if (path->slots[0] >= btrfs_header_nritems(leaf))
1009 goto search;
1010 again:
1011 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1012 if (key.objectid != node->inode_id)
1013 goto out;
1015 if (key.type != BTRFS_INODE_REF_KEY &&
1016 key.type != BTRFS_INODE_EXTREF_KEY)
1017 goto out;
1020 * Delayed iref deletion is for the inode who has only one link,
1021 * so there is only one iref. The case that several irefs are
1022 * in the same item doesn't exist.
1024 btrfs_del_item(trans, root, path);
1025 out:
1026 btrfs_release_delayed_iref(node);
1027 no_iref:
1028 btrfs_release_path(path);
1029 err_out:
1030 btrfs_delayed_inode_release_metadata(fs_info, node);
1031 btrfs_release_delayed_inode(node);
1033 return ret;
1035 search:
1036 btrfs_release_path(path);
1038 key.type = BTRFS_INODE_EXTREF_KEY;
1039 key.offset = -1;
1040 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1041 if (ret < 0)
1042 goto err_out;
1043 ASSERT(ret);
1045 ret = 0;
1046 leaf = path->nodes[0];
1047 path->slots[0]--;
1048 goto again;
1051 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1052 struct btrfs_root *root,
1053 struct btrfs_path *path,
1054 struct btrfs_delayed_node *node)
1056 int ret;
1058 mutex_lock(&node->mutex);
1059 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
1060 mutex_unlock(&node->mutex);
1061 return 0;
1064 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1065 mutex_unlock(&node->mutex);
1066 return ret;
1069 static inline int
1070 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1071 struct btrfs_path *path,
1072 struct btrfs_delayed_node *node)
1074 int ret;
1076 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1077 if (ret)
1078 return ret;
1080 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1081 if (ret)
1082 return ret;
1084 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1085 return ret;
1089 * Called when committing the transaction.
1090 * Returns 0 on success.
1091 * Returns < 0 on error and returns with an aborted transaction with any
1092 * outstanding delayed items cleaned up.
1094 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1095 struct btrfs_fs_info *fs_info, int nr)
1097 struct btrfs_delayed_root *delayed_root;
1098 struct btrfs_delayed_node *curr_node, *prev_node;
1099 struct btrfs_path *path;
1100 struct btrfs_block_rsv *block_rsv;
1101 int ret = 0;
1102 bool count = (nr > 0);
1104 if (trans->aborted)
1105 return -EIO;
1107 path = btrfs_alloc_path();
1108 if (!path)
1109 return -ENOMEM;
1110 path->leave_spinning = 1;
1112 block_rsv = trans->block_rsv;
1113 trans->block_rsv = &fs_info->delayed_block_rsv;
1115 delayed_root = fs_info->delayed_root;
1117 curr_node = btrfs_first_delayed_node(delayed_root);
1118 while (curr_node && (!count || (count && nr--))) {
1119 ret = __btrfs_commit_inode_delayed_items(trans, path,
1120 curr_node);
1121 if (ret) {
1122 btrfs_release_delayed_node(curr_node);
1123 curr_node = NULL;
1124 btrfs_abort_transaction(trans, ret);
1125 break;
1128 prev_node = curr_node;
1129 curr_node = btrfs_next_delayed_node(curr_node);
1130 btrfs_release_delayed_node(prev_node);
1133 if (curr_node)
1134 btrfs_release_delayed_node(curr_node);
1135 btrfs_free_path(path);
1136 trans->block_rsv = block_rsv;
1138 return ret;
1141 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1142 struct btrfs_fs_info *fs_info)
1144 return __btrfs_run_delayed_items(trans, fs_info, -1);
1147 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
1148 struct btrfs_fs_info *fs_info, int nr)
1150 return __btrfs_run_delayed_items(trans, fs_info, nr);
1153 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1154 struct btrfs_inode *inode)
1156 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1157 struct btrfs_path *path;
1158 struct btrfs_block_rsv *block_rsv;
1159 int ret;
1161 if (!delayed_node)
1162 return 0;
1164 mutex_lock(&delayed_node->mutex);
1165 if (!delayed_node->count) {
1166 mutex_unlock(&delayed_node->mutex);
1167 btrfs_release_delayed_node(delayed_node);
1168 return 0;
1170 mutex_unlock(&delayed_node->mutex);
1172 path = btrfs_alloc_path();
1173 if (!path) {
1174 btrfs_release_delayed_node(delayed_node);
1175 return -ENOMEM;
1177 path->leave_spinning = 1;
1179 block_rsv = trans->block_rsv;
1180 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1182 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1184 btrfs_release_delayed_node(delayed_node);
1185 btrfs_free_path(path);
1186 trans->block_rsv = block_rsv;
1188 return ret;
1191 int btrfs_commit_inode_delayed_inode(struct btrfs_inode *inode)
1193 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1194 struct btrfs_trans_handle *trans;
1195 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1196 struct btrfs_path *path;
1197 struct btrfs_block_rsv *block_rsv;
1198 int ret;
1200 if (!delayed_node)
1201 return 0;
1203 mutex_lock(&delayed_node->mutex);
1204 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1205 mutex_unlock(&delayed_node->mutex);
1206 btrfs_release_delayed_node(delayed_node);
1207 return 0;
1209 mutex_unlock(&delayed_node->mutex);
1211 trans = btrfs_join_transaction(delayed_node->root);
1212 if (IS_ERR(trans)) {
1213 ret = PTR_ERR(trans);
1214 goto out;
1217 path = btrfs_alloc_path();
1218 if (!path) {
1219 ret = -ENOMEM;
1220 goto trans_out;
1222 path->leave_spinning = 1;
1224 block_rsv = trans->block_rsv;
1225 trans->block_rsv = &fs_info->delayed_block_rsv;
1227 mutex_lock(&delayed_node->mutex);
1228 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
1229 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1230 path, delayed_node);
1231 else
1232 ret = 0;
1233 mutex_unlock(&delayed_node->mutex);
1235 btrfs_free_path(path);
1236 trans->block_rsv = block_rsv;
1237 trans_out:
1238 btrfs_end_transaction(trans);
1239 btrfs_btree_balance_dirty(fs_info);
1240 out:
1241 btrfs_release_delayed_node(delayed_node);
1243 return ret;
1246 void btrfs_remove_delayed_node(struct btrfs_inode *inode)
1248 struct btrfs_delayed_node *delayed_node;
1250 delayed_node = READ_ONCE(inode->delayed_node);
1251 if (!delayed_node)
1252 return;
1254 inode->delayed_node = NULL;
1255 btrfs_release_delayed_node(delayed_node);
1258 struct btrfs_async_delayed_work {
1259 struct btrfs_delayed_root *delayed_root;
1260 int nr;
1261 struct btrfs_work work;
1264 static void btrfs_async_run_delayed_root(struct btrfs_work *work)
1266 struct btrfs_async_delayed_work *async_work;
1267 struct btrfs_delayed_root *delayed_root;
1268 struct btrfs_trans_handle *trans;
1269 struct btrfs_path *path;
1270 struct btrfs_delayed_node *delayed_node = NULL;
1271 struct btrfs_root *root;
1272 struct btrfs_block_rsv *block_rsv;
1273 int total_done = 0;
1275 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1276 delayed_root = async_work->delayed_root;
1278 path = btrfs_alloc_path();
1279 if (!path)
1280 goto out;
1282 again:
1283 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2)
1284 goto free_path;
1286 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1287 if (!delayed_node)
1288 goto free_path;
1290 path->leave_spinning = 1;
1291 root = delayed_node->root;
1293 trans = btrfs_join_transaction(root);
1294 if (IS_ERR(trans))
1295 goto release_path;
1297 block_rsv = trans->block_rsv;
1298 trans->block_rsv = &root->fs_info->delayed_block_rsv;
1300 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1302 trans->block_rsv = block_rsv;
1303 btrfs_end_transaction(trans);
1304 btrfs_btree_balance_dirty_nodelay(root->fs_info);
1306 release_path:
1307 btrfs_release_path(path);
1308 total_done++;
1310 btrfs_release_prepared_delayed_node(delayed_node);
1311 if ((async_work->nr == 0 && total_done < BTRFS_DELAYED_WRITEBACK) ||
1312 total_done < async_work->nr)
1313 goto again;
1315 free_path:
1316 btrfs_free_path(path);
1317 out:
1318 wake_up(&delayed_root->wait);
1319 kfree(async_work);
1323 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1324 struct btrfs_fs_info *fs_info, int nr)
1326 struct btrfs_async_delayed_work *async_work;
1328 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND ||
1329 btrfs_workqueue_normal_congested(fs_info->delayed_workers))
1330 return 0;
1332 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1333 if (!async_work)
1334 return -ENOMEM;
1336 async_work->delayed_root = delayed_root;
1337 btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper,
1338 btrfs_async_run_delayed_root, NULL, NULL);
1339 async_work->nr = nr;
1341 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
1342 return 0;
1345 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
1347 WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
1350 static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
1352 int val = atomic_read(&delayed_root->items_seq);
1354 if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
1355 return 1;
1357 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1358 return 1;
1360 return 0;
1363 void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
1365 struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
1367 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1368 return;
1370 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1371 int seq;
1372 int ret;
1374 seq = atomic_read(&delayed_root->items_seq);
1376 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
1377 if (ret)
1378 return;
1380 wait_event_interruptible(delayed_root->wait,
1381 could_end_wait(delayed_root, seq));
1382 return;
1385 btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
1388 /* Will return 0 or -ENOMEM */
1389 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1390 struct btrfs_fs_info *fs_info,
1391 const char *name, int name_len,
1392 struct btrfs_inode *dir,
1393 struct btrfs_disk_key *disk_key, u8 type,
1394 u64 index)
1396 struct btrfs_delayed_node *delayed_node;
1397 struct btrfs_delayed_item *delayed_item;
1398 struct btrfs_dir_item *dir_item;
1399 int ret;
1401 delayed_node = btrfs_get_or_create_delayed_node(dir);
1402 if (IS_ERR(delayed_node))
1403 return PTR_ERR(delayed_node);
1405 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1406 if (!delayed_item) {
1407 ret = -ENOMEM;
1408 goto release_node;
1411 delayed_item->key.objectid = btrfs_ino(dir);
1412 delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
1413 delayed_item->key.offset = index;
1415 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1416 dir_item->location = *disk_key;
1417 btrfs_set_stack_dir_transid(dir_item, trans->transid);
1418 btrfs_set_stack_dir_data_len(dir_item, 0);
1419 btrfs_set_stack_dir_name_len(dir_item, name_len);
1420 btrfs_set_stack_dir_type(dir_item, type);
1421 memcpy((char *)(dir_item + 1), name, name_len);
1423 ret = btrfs_delayed_item_reserve_metadata(trans, fs_info, delayed_item);
1425 * we have reserved enough space when we start a new transaction,
1426 * so reserving metadata failure is impossible
1428 BUG_ON(ret);
1431 mutex_lock(&delayed_node->mutex);
1432 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1433 if (unlikely(ret)) {
1434 btrfs_err(fs_info,
1435 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1436 name_len, name, delayed_node->root->objectid,
1437 delayed_node->inode_id, ret);
1438 BUG();
1440 mutex_unlock(&delayed_node->mutex);
1442 release_node:
1443 btrfs_release_delayed_node(delayed_node);
1444 return ret;
1447 static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
1448 struct btrfs_delayed_node *node,
1449 struct btrfs_key *key)
1451 struct btrfs_delayed_item *item;
1453 mutex_lock(&node->mutex);
1454 item = __btrfs_lookup_delayed_insertion_item(node, key);
1455 if (!item) {
1456 mutex_unlock(&node->mutex);
1457 return 1;
1460 btrfs_delayed_item_release_metadata(fs_info, item);
1461 btrfs_release_delayed_item(item);
1462 mutex_unlock(&node->mutex);
1463 return 0;
1466 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1467 struct btrfs_fs_info *fs_info,
1468 struct btrfs_inode *dir, u64 index)
1470 struct btrfs_delayed_node *node;
1471 struct btrfs_delayed_item *item;
1472 struct btrfs_key item_key;
1473 int ret;
1475 node = btrfs_get_or_create_delayed_node(dir);
1476 if (IS_ERR(node))
1477 return PTR_ERR(node);
1479 item_key.objectid = btrfs_ino(dir);
1480 item_key.type = BTRFS_DIR_INDEX_KEY;
1481 item_key.offset = index;
1483 ret = btrfs_delete_delayed_insertion_item(fs_info, node, &item_key);
1484 if (!ret)
1485 goto end;
1487 item = btrfs_alloc_delayed_item(0);
1488 if (!item) {
1489 ret = -ENOMEM;
1490 goto end;
1493 item->key = item_key;
1495 ret = btrfs_delayed_item_reserve_metadata(trans, fs_info, item);
1497 * we have reserved enough space when we start a new transaction,
1498 * so reserving metadata failure is impossible.
1500 BUG_ON(ret);
1502 mutex_lock(&node->mutex);
1503 ret = __btrfs_add_delayed_deletion_item(node, item);
1504 if (unlikely(ret)) {
1505 btrfs_err(fs_info,
1506 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1507 index, node->root->objectid, node->inode_id, ret);
1508 BUG();
1510 mutex_unlock(&node->mutex);
1511 end:
1512 btrfs_release_delayed_node(node);
1513 return ret;
1516 int btrfs_inode_delayed_dir_index_count(struct btrfs_inode *inode)
1518 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1520 if (!delayed_node)
1521 return -ENOENT;
1524 * Since we have held i_mutex of this directory, it is impossible that
1525 * a new directory index is added into the delayed node and index_cnt
1526 * is updated now. So we needn't lock the delayed node.
1528 if (!delayed_node->index_cnt) {
1529 btrfs_release_delayed_node(delayed_node);
1530 return -EINVAL;
1533 inode->index_cnt = delayed_node->index_cnt;
1534 btrfs_release_delayed_node(delayed_node);
1535 return 0;
1538 bool btrfs_readdir_get_delayed_items(struct inode *inode,
1539 struct list_head *ins_list,
1540 struct list_head *del_list)
1542 struct btrfs_delayed_node *delayed_node;
1543 struct btrfs_delayed_item *item;
1545 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1546 if (!delayed_node)
1547 return false;
1550 * We can only do one readdir with delayed items at a time because of
1551 * item->readdir_list.
1553 inode_unlock_shared(inode);
1554 inode_lock(inode);
1556 mutex_lock(&delayed_node->mutex);
1557 item = __btrfs_first_delayed_insertion_item(delayed_node);
1558 while (item) {
1559 refcount_inc(&item->refs);
1560 list_add_tail(&item->readdir_list, ins_list);
1561 item = __btrfs_next_delayed_item(item);
1564 item = __btrfs_first_delayed_deletion_item(delayed_node);
1565 while (item) {
1566 refcount_inc(&item->refs);
1567 list_add_tail(&item->readdir_list, del_list);
1568 item = __btrfs_next_delayed_item(item);
1570 mutex_unlock(&delayed_node->mutex);
1572 * This delayed node is still cached in the btrfs inode, so refs
1573 * must be > 1 now, and we needn't check it is going to be freed
1574 * or not.
1576 * Besides that, this function is used to read dir, we do not
1577 * insert/delete delayed items in this period. So we also needn't
1578 * requeue or dequeue this delayed node.
1580 refcount_dec(&delayed_node->refs);
1582 return true;
1585 void btrfs_readdir_put_delayed_items(struct inode *inode,
1586 struct list_head *ins_list,
1587 struct list_head *del_list)
1589 struct btrfs_delayed_item *curr, *next;
1591 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1592 list_del(&curr->readdir_list);
1593 if (refcount_dec_and_test(&curr->refs))
1594 kfree(curr);
1597 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1598 list_del(&curr->readdir_list);
1599 if (refcount_dec_and_test(&curr->refs))
1600 kfree(curr);
1604 * The VFS is going to do up_read(), so we need to downgrade back to a
1605 * read lock.
1607 downgrade_write(&inode->i_rwsem);
1610 int btrfs_should_delete_dir_index(struct list_head *del_list,
1611 u64 index)
1613 struct btrfs_delayed_item *curr, *next;
1614 int ret;
1616 if (list_empty(del_list))
1617 return 0;
1619 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1620 if (curr->key.offset > index)
1621 break;
1623 list_del(&curr->readdir_list);
1624 ret = (curr->key.offset == index);
1626 if (refcount_dec_and_test(&curr->refs))
1627 kfree(curr);
1629 if (ret)
1630 return 1;
1631 else
1632 continue;
1634 return 0;
1638 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1641 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1642 struct list_head *ins_list)
1644 struct btrfs_dir_item *di;
1645 struct btrfs_delayed_item *curr, *next;
1646 struct btrfs_key location;
1647 char *name;
1648 int name_len;
1649 int over = 0;
1650 unsigned char d_type;
1652 if (list_empty(ins_list))
1653 return 0;
1656 * Changing the data of the delayed item is impossible. So
1657 * we needn't lock them. And we have held i_mutex of the
1658 * directory, nobody can delete any directory indexes now.
1660 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1661 list_del(&curr->readdir_list);
1663 if (curr->key.offset < ctx->pos) {
1664 if (refcount_dec_and_test(&curr->refs))
1665 kfree(curr);
1666 continue;
1669 ctx->pos = curr->key.offset;
1671 di = (struct btrfs_dir_item *)curr->data;
1672 name = (char *)(di + 1);
1673 name_len = btrfs_stack_dir_name_len(di);
1675 d_type = btrfs_filetype_table[di->type];
1676 btrfs_disk_key_to_cpu(&location, &di->location);
1678 over = !dir_emit(ctx, name, name_len,
1679 location.objectid, d_type);
1681 if (refcount_dec_and_test(&curr->refs))
1682 kfree(curr);
1684 if (over)
1685 return 1;
1686 ctx->pos++;
1688 return 0;
1691 static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1692 struct btrfs_inode_item *inode_item,
1693 struct inode *inode)
1695 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1696 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1697 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1698 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1699 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1700 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1701 btrfs_set_stack_inode_generation(inode_item,
1702 BTRFS_I(inode)->generation);
1703 btrfs_set_stack_inode_sequence(inode_item, inode->i_version);
1704 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1705 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1706 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags);
1707 btrfs_set_stack_inode_block_group(inode_item, 0);
1709 btrfs_set_stack_timespec_sec(&inode_item->atime,
1710 inode->i_atime.tv_sec);
1711 btrfs_set_stack_timespec_nsec(&inode_item->atime,
1712 inode->i_atime.tv_nsec);
1714 btrfs_set_stack_timespec_sec(&inode_item->mtime,
1715 inode->i_mtime.tv_sec);
1716 btrfs_set_stack_timespec_nsec(&inode_item->mtime,
1717 inode->i_mtime.tv_nsec);
1719 btrfs_set_stack_timespec_sec(&inode_item->ctime,
1720 inode->i_ctime.tv_sec);
1721 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
1722 inode->i_ctime.tv_nsec);
1724 btrfs_set_stack_timespec_sec(&inode_item->otime,
1725 BTRFS_I(inode)->i_otime.tv_sec);
1726 btrfs_set_stack_timespec_nsec(&inode_item->otime,
1727 BTRFS_I(inode)->i_otime.tv_nsec);
1730 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1732 struct btrfs_delayed_node *delayed_node;
1733 struct btrfs_inode_item *inode_item;
1735 delayed_node = btrfs_get_delayed_node(BTRFS_I(inode));
1736 if (!delayed_node)
1737 return -ENOENT;
1739 mutex_lock(&delayed_node->mutex);
1740 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1741 mutex_unlock(&delayed_node->mutex);
1742 btrfs_release_delayed_node(delayed_node);
1743 return -ENOENT;
1746 inode_item = &delayed_node->inode_item;
1748 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1749 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1750 btrfs_i_size_write(BTRFS_I(inode), btrfs_stack_inode_size(inode_item));
1751 inode->i_mode = btrfs_stack_inode_mode(inode_item);
1752 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1753 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1754 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1755 BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
1757 inode->i_version = btrfs_stack_inode_sequence(inode_item);
1758 inode->i_rdev = 0;
1759 *rdev = btrfs_stack_inode_rdev(inode_item);
1760 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1762 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
1763 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
1765 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
1766 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
1768 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
1769 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
1771 BTRFS_I(inode)->i_otime.tv_sec =
1772 btrfs_stack_timespec_sec(&inode_item->otime);
1773 BTRFS_I(inode)->i_otime.tv_nsec =
1774 btrfs_stack_timespec_nsec(&inode_item->otime);
1776 inode->i_generation = BTRFS_I(inode)->generation;
1777 BTRFS_I(inode)->index_cnt = (u64)-1;
1779 mutex_unlock(&delayed_node->mutex);
1780 btrfs_release_delayed_node(delayed_node);
1781 return 0;
1784 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1785 struct btrfs_root *root, struct inode *inode)
1787 struct btrfs_delayed_node *delayed_node;
1788 int ret = 0;
1790 delayed_node = btrfs_get_or_create_delayed_node(BTRFS_I(inode));
1791 if (IS_ERR(delayed_node))
1792 return PTR_ERR(delayed_node);
1794 mutex_lock(&delayed_node->mutex);
1795 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1796 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1797 goto release_node;
1800 ret = btrfs_delayed_inode_reserve_metadata(trans, root, BTRFS_I(inode),
1801 delayed_node);
1802 if (ret)
1803 goto release_node;
1805 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1806 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
1807 delayed_node->count++;
1808 atomic_inc(&root->fs_info->delayed_root->items);
1809 release_node:
1810 mutex_unlock(&delayed_node->mutex);
1811 btrfs_release_delayed_node(delayed_node);
1812 return ret;
1815 int btrfs_delayed_delete_inode_ref(struct btrfs_inode *inode)
1817 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1818 struct btrfs_delayed_node *delayed_node;
1821 * we don't do delayed inode updates during log recovery because it
1822 * leads to enospc problems. This means we also can't do
1823 * delayed inode refs
1825 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
1826 return -EAGAIN;
1828 delayed_node = btrfs_get_or_create_delayed_node(inode);
1829 if (IS_ERR(delayed_node))
1830 return PTR_ERR(delayed_node);
1833 * We don't reserve space for inode ref deletion is because:
1834 * - We ONLY do async inode ref deletion for the inode who has only
1835 * one link(i_nlink == 1), it means there is only one inode ref.
1836 * And in most case, the inode ref and the inode item are in the
1837 * same leaf, and we will deal with them at the same time.
1838 * Since we are sure we will reserve the space for the inode item,
1839 * it is unnecessary to reserve space for inode ref deletion.
1840 * - If the inode ref and the inode item are not in the same leaf,
1841 * We also needn't worry about enospc problem, because we reserve
1842 * much more space for the inode update than it needs.
1843 * - At the worst, we can steal some space from the global reservation.
1844 * It is very rare.
1846 mutex_lock(&delayed_node->mutex);
1847 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1848 goto release_node;
1850 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1851 delayed_node->count++;
1852 atomic_inc(&fs_info->delayed_root->items);
1853 release_node:
1854 mutex_unlock(&delayed_node->mutex);
1855 btrfs_release_delayed_node(delayed_node);
1856 return 0;
1859 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1861 struct btrfs_root *root = delayed_node->root;
1862 struct btrfs_fs_info *fs_info = root->fs_info;
1863 struct btrfs_delayed_item *curr_item, *prev_item;
1865 mutex_lock(&delayed_node->mutex);
1866 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1867 while (curr_item) {
1868 btrfs_delayed_item_release_metadata(fs_info, curr_item);
1869 prev_item = curr_item;
1870 curr_item = __btrfs_next_delayed_item(prev_item);
1871 btrfs_release_delayed_item(prev_item);
1874 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1875 while (curr_item) {
1876 btrfs_delayed_item_release_metadata(fs_info, curr_item);
1877 prev_item = curr_item;
1878 curr_item = __btrfs_next_delayed_item(prev_item);
1879 btrfs_release_delayed_item(prev_item);
1882 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1883 btrfs_release_delayed_iref(delayed_node);
1885 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1886 btrfs_delayed_inode_release_metadata(fs_info, delayed_node);
1887 btrfs_release_delayed_inode(delayed_node);
1889 mutex_unlock(&delayed_node->mutex);
1892 void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
1894 struct btrfs_delayed_node *delayed_node;
1896 delayed_node = btrfs_get_delayed_node(inode);
1897 if (!delayed_node)
1898 return;
1900 __btrfs_kill_delayed_node(delayed_node);
1901 btrfs_release_delayed_node(delayed_node);
1904 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1906 u64 inode_id = 0;
1907 struct btrfs_delayed_node *delayed_nodes[8];
1908 int i, n;
1910 while (1) {
1911 spin_lock(&root->inode_lock);
1912 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1913 (void **)delayed_nodes, inode_id,
1914 ARRAY_SIZE(delayed_nodes));
1915 if (!n) {
1916 spin_unlock(&root->inode_lock);
1917 break;
1920 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1922 for (i = 0; i < n; i++)
1923 refcount_inc(&delayed_nodes[i]->refs);
1924 spin_unlock(&root->inode_lock);
1926 for (i = 0; i < n; i++) {
1927 __btrfs_kill_delayed_node(delayed_nodes[i]);
1928 btrfs_release_delayed_node(delayed_nodes[i]);
1933 void btrfs_destroy_delayed_inodes(struct btrfs_fs_info *fs_info)
1935 struct btrfs_delayed_node *curr_node, *prev_node;
1937 curr_node = btrfs_first_delayed_node(fs_info->delayed_root);
1938 while (curr_node) {
1939 __btrfs_kill_delayed_node(curr_node);
1941 prev_node = curr_node;
1942 curr_node = btrfs_next_delayed_node(curr_node);
1943 btrfs_release_delayed_node(prev_node);