dmaengine: imx-dma: fix slow path issue in prep_dma_cyclic
[linux/fpc-iii.git] / fs / btrfs / delayed-inode.c
blob375510913fe744784f8f56966ed29693ee8e3612
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"
25 #define BTRFS_DELAYED_WRITEBACK 512
26 #define BTRFS_DELAYED_BACKGROUND 128
27 #define BTRFS_DELAYED_BATCH 16
29 static struct kmem_cache *delayed_node_cache;
31 int __init btrfs_delayed_inode_init(void)
33 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
34 sizeof(struct btrfs_delayed_node),
36 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
37 NULL);
38 if (!delayed_node_cache)
39 return -ENOMEM;
40 return 0;
43 void btrfs_delayed_inode_exit(void)
45 if (delayed_node_cache)
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 atomic_set(&delayed_node->refs, 0);
56 delayed_node->count = 0;
57 delayed_node->in_list = 0;
58 delayed_node->inode_dirty = 0;
59 delayed_node->ins_root = RB_ROOT;
60 delayed_node->del_root = RB_ROOT;
61 mutex_init(&delayed_node->mutex);
62 delayed_node->index_cnt = 0;
63 INIT_LIST_HEAD(&delayed_node->n_list);
64 INIT_LIST_HEAD(&delayed_node->p_list);
65 delayed_node->bytes_reserved = 0;
66 memset(&delayed_node->inode_item, 0, sizeof(delayed_node->inode_item));
69 static inline int btrfs_is_continuous_delayed_item(
70 struct btrfs_delayed_item *item1,
71 struct btrfs_delayed_item *item2)
73 if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
74 item1->key.objectid == item2->key.objectid &&
75 item1->key.type == item2->key.type &&
76 item1->key.offset + 1 == item2->key.offset)
77 return 1;
78 return 0;
81 static inline struct btrfs_delayed_root *btrfs_get_delayed_root(
82 struct btrfs_root *root)
84 return root->fs_info->delayed_root;
87 static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode)
89 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
90 struct btrfs_root *root = btrfs_inode->root;
91 u64 ino = btrfs_ino(inode);
92 struct btrfs_delayed_node *node;
94 node = ACCESS_ONCE(btrfs_inode->delayed_node);
95 if (node) {
96 atomic_inc(&node->refs);
97 return node;
100 spin_lock(&root->inode_lock);
101 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
102 if (node) {
103 if (btrfs_inode->delayed_node) {
104 atomic_inc(&node->refs); /* can be accessed */
105 BUG_ON(btrfs_inode->delayed_node != node);
106 spin_unlock(&root->inode_lock);
107 return node;
109 btrfs_inode->delayed_node = node;
110 atomic_inc(&node->refs); /* can be accessed */
111 atomic_inc(&node->refs); /* cached in the inode */
112 spin_unlock(&root->inode_lock);
113 return node;
115 spin_unlock(&root->inode_lock);
117 return NULL;
120 /* Will return either the node or PTR_ERR(-ENOMEM) */
121 static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
122 struct inode *inode)
124 struct btrfs_delayed_node *node;
125 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
126 struct btrfs_root *root = btrfs_inode->root;
127 u64 ino = btrfs_ino(inode);
128 int ret;
130 again:
131 node = btrfs_get_delayed_node(inode);
132 if (node)
133 return node;
135 node = kmem_cache_alloc(delayed_node_cache, GFP_NOFS);
136 if (!node)
137 return ERR_PTR(-ENOMEM);
138 btrfs_init_delayed_node(node, root, ino);
140 atomic_inc(&node->refs); /* cached in the btrfs inode */
141 atomic_inc(&node->refs); /* can be accessed */
143 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
144 if (ret) {
145 kmem_cache_free(delayed_node_cache, node);
146 return ERR_PTR(ret);
149 spin_lock(&root->inode_lock);
150 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
151 if (ret == -EEXIST) {
152 kmem_cache_free(delayed_node_cache, node);
153 spin_unlock(&root->inode_lock);
154 radix_tree_preload_end();
155 goto again;
157 btrfs_inode->delayed_node = node;
158 spin_unlock(&root->inode_lock);
159 radix_tree_preload_end();
161 return node;
165 * Call it when holding delayed_node->mutex
167 * If mod = 1, add this node into the prepared list.
169 static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
170 struct btrfs_delayed_node *node,
171 int mod)
173 spin_lock(&root->lock);
174 if (node->in_list) {
175 if (!list_empty(&node->p_list))
176 list_move_tail(&node->p_list, &root->prepare_list);
177 else if (mod)
178 list_add_tail(&node->p_list, &root->prepare_list);
179 } else {
180 list_add_tail(&node->n_list, &root->node_list);
181 list_add_tail(&node->p_list, &root->prepare_list);
182 atomic_inc(&node->refs); /* inserted into list */
183 root->nodes++;
184 node->in_list = 1;
186 spin_unlock(&root->lock);
189 /* Call it when holding delayed_node->mutex */
190 static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
191 struct btrfs_delayed_node *node)
193 spin_lock(&root->lock);
194 if (node->in_list) {
195 root->nodes--;
196 atomic_dec(&node->refs); /* not in the list */
197 list_del_init(&node->n_list);
198 if (!list_empty(&node->p_list))
199 list_del_init(&node->p_list);
200 node->in_list = 0;
202 spin_unlock(&root->lock);
205 static struct btrfs_delayed_node *btrfs_first_delayed_node(
206 struct btrfs_delayed_root *delayed_root)
208 struct list_head *p;
209 struct btrfs_delayed_node *node = NULL;
211 spin_lock(&delayed_root->lock);
212 if (list_empty(&delayed_root->node_list))
213 goto out;
215 p = delayed_root->node_list.next;
216 node = list_entry(p, struct btrfs_delayed_node, n_list);
217 atomic_inc(&node->refs);
218 out:
219 spin_unlock(&delayed_root->lock);
221 return node;
224 static struct btrfs_delayed_node *btrfs_next_delayed_node(
225 struct btrfs_delayed_node *node)
227 struct btrfs_delayed_root *delayed_root;
228 struct list_head *p;
229 struct btrfs_delayed_node *next = NULL;
231 delayed_root = node->root->fs_info->delayed_root;
232 spin_lock(&delayed_root->lock);
233 if (!node->in_list) { /* not in the list */
234 if (list_empty(&delayed_root->node_list))
235 goto out;
236 p = delayed_root->node_list.next;
237 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
238 goto out;
239 else
240 p = node->n_list.next;
242 next = list_entry(p, struct btrfs_delayed_node, n_list);
243 atomic_inc(&next->refs);
244 out:
245 spin_unlock(&delayed_root->lock);
247 return next;
250 static void __btrfs_release_delayed_node(
251 struct btrfs_delayed_node *delayed_node,
252 int mod)
254 struct btrfs_delayed_root *delayed_root;
256 if (!delayed_node)
257 return;
259 delayed_root = delayed_node->root->fs_info->delayed_root;
261 mutex_lock(&delayed_node->mutex);
262 if (delayed_node->count)
263 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
264 else
265 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
266 mutex_unlock(&delayed_node->mutex);
268 if (atomic_dec_and_test(&delayed_node->refs)) {
269 struct btrfs_root *root = delayed_node->root;
270 spin_lock(&root->inode_lock);
271 if (atomic_read(&delayed_node->refs) == 0) {
272 radix_tree_delete(&root->delayed_nodes_tree,
273 delayed_node->inode_id);
274 kmem_cache_free(delayed_node_cache, delayed_node);
276 spin_unlock(&root->inode_lock);
280 static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
282 __btrfs_release_delayed_node(node, 0);
285 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
286 struct btrfs_delayed_root *delayed_root)
288 struct list_head *p;
289 struct btrfs_delayed_node *node = NULL;
291 spin_lock(&delayed_root->lock);
292 if (list_empty(&delayed_root->prepare_list))
293 goto out;
295 p = delayed_root->prepare_list.next;
296 list_del_init(p);
297 node = list_entry(p, struct btrfs_delayed_node, p_list);
298 atomic_inc(&node->refs);
299 out:
300 spin_unlock(&delayed_root->lock);
302 return node;
305 static inline void btrfs_release_prepared_delayed_node(
306 struct btrfs_delayed_node *node)
308 __btrfs_release_delayed_node(node, 1);
311 static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
313 struct btrfs_delayed_item *item;
314 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
315 if (item) {
316 item->data_len = data_len;
317 item->ins_or_del = 0;
318 item->bytes_reserved = 0;
319 item->delayed_node = NULL;
320 atomic_set(&item->refs, 1);
322 return item;
326 * __btrfs_lookup_delayed_item - look up the delayed item by key
327 * @delayed_node: pointer to the delayed node
328 * @key: the key to look up
329 * @prev: used to store the prev item if the right item isn't found
330 * @next: used to store the next item if the right item isn't found
332 * Note: if we don't find the right item, we will return the prev item and
333 * the next item.
335 static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
336 struct rb_root *root,
337 struct btrfs_key *key,
338 struct btrfs_delayed_item **prev,
339 struct btrfs_delayed_item **next)
341 struct rb_node *node, *prev_node = NULL;
342 struct btrfs_delayed_item *delayed_item = NULL;
343 int ret = 0;
345 node = root->rb_node;
347 while (node) {
348 delayed_item = rb_entry(node, struct btrfs_delayed_item,
349 rb_node);
350 prev_node = node;
351 ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
352 if (ret < 0)
353 node = node->rb_right;
354 else if (ret > 0)
355 node = node->rb_left;
356 else
357 return delayed_item;
360 if (prev) {
361 if (!prev_node)
362 *prev = NULL;
363 else if (ret < 0)
364 *prev = delayed_item;
365 else if ((node = rb_prev(prev_node)) != NULL) {
366 *prev = rb_entry(node, struct btrfs_delayed_item,
367 rb_node);
368 } else
369 *prev = NULL;
372 if (next) {
373 if (!prev_node)
374 *next = NULL;
375 else if (ret > 0)
376 *next = delayed_item;
377 else if ((node = rb_next(prev_node)) != NULL) {
378 *next = rb_entry(node, struct btrfs_delayed_item,
379 rb_node);
380 } else
381 *next = NULL;
383 return NULL;
386 static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
387 struct btrfs_delayed_node *delayed_node,
388 struct btrfs_key *key)
390 struct btrfs_delayed_item *item;
392 item = __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
393 NULL, NULL);
394 return item;
397 static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
398 struct btrfs_delayed_item *ins,
399 int action)
401 struct rb_node **p, *node;
402 struct rb_node *parent_node = NULL;
403 struct rb_root *root;
404 struct btrfs_delayed_item *item;
405 int cmp;
407 if (action == BTRFS_DELAYED_INSERTION_ITEM)
408 root = &delayed_node->ins_root;
409 else if (action == BTRFS_DELAYED_DELETION_ITEM)
410 root = &delayed_node->del_root;
411 else
412 BUG();
413 p = &root->rb_node;
414 node = &ins->rb_node;
416 while (*p) {
417 parent_node = *p;
418 item = rb_entry(parent_node, struct btrfs_delayed_item,
419 rb_node);
421 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
422 if (cmp < 0)
423 p = &(*p)->rb_right;
424 else if (cmp > 0)
425 p = &(*p)->rb_left;
426 else
427 return -EEXIST;
430 rb_link_node(node, parent_node, p);
431 rb_insert_color(node, root);
432 ins->delayed_node = delayed_node;
433 ins->ins_or_del = action;
435 if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
436 action == BTRFS_DELAYED_INSERTION_ITEM &&
437 ins->key.offset >= delayed_node->index_cnt)
438 delayed_node->index_cnt = ins->key.offset + 1;
440 delayed_node->count++;
441 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
442 return 0;
445 static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
446 struct btrfs_delayed_item *item)
448 return __btrfs_add_delayed_item(node, item,
449 BTRFS_DELAYED_INSERTION_ITEM);
452 static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
453 struct btrfs_delayed_item *item)
455 return __btrfs_add_delayed_item(node, item,
456 BTRFS_DELAYED_DELETION_ITEM);
459 static void finish_one_item(struct btrfs_delayed_root *delayed_root)
461 int seq = atomic_inc_return(&delayed_root->items_seq);
462 if ((atomic_dec_return(&delayed_root->items) <
463 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
464 waitqueue_active(&delayed_root->wait))
465 wake_up(&delayed_root->wait);
468 static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
470 struct rb_root *root;
471 struct btrfs_delayed_root *delayed_root;
473 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
475 BUG_ON(!delayed_root);
476 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
477 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
479 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
480 root = &delayed_item->delayed_node->ins_root;
481 else
482 root = &delayed_item->delayed_node->del_root;
484 rb_erase(&delayed_item->rb_node, root);
485 delayed_item->delayed_node->count--;
487 finish_one_item(delayed_root);
490 static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
492 if (item) {
493 __btrfs_remove_delayed_item(item);
494 if (atomic_dec_and_test(&item->refs))
495 kfree(item);
499 static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
500 struct btrfs_delayed_node *delayed_node)
502 struct rb_node *p;
503 struct btrfs_delayed_item *item = NULL;
505 p = rb_first(&delayed_node->ins_root);
506 if (p)
507 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
509 return item;
512 static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
513 struct btrfs_delayed_node *delayed_node)
515 struct rb_node *p;
516 struct btrfs_delayed_item *item = NULL;
518 p = rb_first(&delayed_node->del_root);
519 if (p)
520 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
522 return item;
525 static struct btrfs_delayed_item *__btrfs_next_delayed_item(
526 struct btrfs_delayed_item *item)
528 struct rb_node *p;
529 struct btrfs_delayed_item *next = NULL;
531 p = rb_next(&item->rb_node);
532 if (p)
533 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
535 return next;
538 static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct btrfs_delayed_item *item)
542 struct btrfs_block_rsv *src_rsv;
543 struct btrfs_block_rsv *dst_rsv;
544 u64 num_bytes;
545 int ret;
547 if (!trans->bytes_reserved)
548 return 0;
550 src_rsv = trans->block_rsv;
551 dst_rsv = &root->fs_info->delayed_block_rsv;
553 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
554 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
555 if (!ret) {
556 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
557 item->key.objectid,
558 num_bytes, 1);
559 item->bytes_reserved = num_bytes;
562 return ret;
565 static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
566 struct btrfs_delayed_item *item)
568 struct btrfs_block_rsv *rsv;
570 if (!item->bytes_reserved)
571 return;
573 rsv = &root->fs_info->delayed_block_rsv;
574 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
575 item->key.objectid, item->bytes_reserved,
577 btrfs_block_rsv_release(root, rsv,
578 item->bytes_reserved);
581 static int btrfs_delayed_inode_reserve_metadata(
582 struct btrfs_trans_handle *trans,
583 struct btrfs_root *root,
584 struct inode *inode,
585 struct btrfs_delayed_node *node)
587 struct btrfs_block_rsv *src_rsv;
588 struct btrfs_block_rsv *dst_rsv;
589 u64 num_bytes;
590 int ret;
591 bool release = false;
593 src_rsv = trans->block_rsv;
594 dst_rsv = &root->fs_info->delayed_block_rsv;
596 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
599 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
600 * which doesn't reserve space for speed. This is a problem since we
601 * still need to reserve space for this update, so try to reserve the
602 * space.
604 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
605 * we're accounted for.
607 if (!src_rsv || (!trans->bytes_reserved &&
608 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
609 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
610 BTRFS_RESERVE_NO_FLUSH);
612 * Since we're under a transaction reserve_metadata_bytes could
613 * try to commit the transaction which will make it return
614 * EAGAIN to make us stop the transaction we have, so return
615 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
617 if (ret == -EAGAIN)
618 ret = -ENOSPC;
619 if (!ret) {
620 node->bytes_reserved = num_bytes;
621 trace_btrfs_space_reservation(root->fs_info,
622 "delayed_inode",
623 btrfs_ino(inode),
624 num_bytes, 1);
626 return ret;
627 } else if (src_rsv->type == BTRFS_BLOCK_RSV_DELALLOC) {
628 spin_lock(&BTRFS_I(inode)->lock);
629 if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
630 &BTRFS_I(inode)->runtime_flags)) {
631 spin_unlock(&BTRFS_I(inode)->lock);
632 release = true;
633 goto migrate;
635 spin_unlock(&BTRFS_I(inode)->lock);
637 /* Ok we didn't have space pre-reserved. This shouldn't happen
638 * too often but it can happen if we do delalloc to an existing
639 * inode which gets dirtied because of the time update, and then
640 * isn't touched again until after the transaction commits and
641 * then we try to write out the data. First try to be nice and
642 * reserve something strictly for us. If not be a pain and try
643 * to steal from the delalloc block rsv.
645 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
646 BTRFS_RESERVE_NO_FLUSH);
647 if (!ret)
648 goto out;
650 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
651 if (!ret)
652 goto out;
655 * Ok this is a problem, let's just steal from the global rsv
656 * since this really shouldn't happen that often.
658 WARN_ON(1);
659 ret = btrfs_block_rsv_migrate(&root->fs_info->global_block_rsv,
660 dst_rsv, num_bytes);
661 goto out;
664 migrate:
665 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
667 out:
669 * Migrate only takes a reservation, it doesn't touch the size of the
670 * block_rsv. This is to simplify people who don't normally have things
671 * migrated from their block rsv. If they go to release their
672 * reservation, that will decrease the size as well, so if migrate
673 * reduced size we'd end up with a negative size. But for the
674 * delalloc_meta_reserved stuff we will only know to drop 1 reservation,
675 * but we could in fact do this reserve/migrate dance several times
676 * between the time we did the original reservation and we'd clean it
677 * up. So to take care of this, release the space for the meta
678 * reservation here. I think it may be time for a documentation page on
679 * how block rsvs. work.
681 if (!ret) {
682 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
683 btrfs_ino(inode), num_bytes, 1);
684 node->bytes_reserved = num_bytes;
687 if (release) {
688 trace_btrfs_space_reservation(root->fs_info, "delalloc",
689 btrfs_ino(inode), num_bytes, 0);
690 btrfs_block_rsv_release(root, src_rsv, num_bytes);
693 return ret;
696 static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
697 struct btrfs_delayed_node *node)
699 struct btrfs_block_rsv *rsv;
701 if (!node->bytes_reserved)
702 return;
704 rsv = &root->fs_info->delayed_block_rsv;
705 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
706 node->inode_id, node->bytes_reserved, 0);
707 btrfs_block_rsv_release(root, rsv,
708 node->bytes_reserved);
709 node->bytes_reserved = 0;
713 * This helper will insert some continuous items into the same leaf according
714 * to the free space of the leaf.
716 static int btrfs_batch_insert_items(struct btrfs_root *root,
717 struct btrfs_path *path,
718 struct btrfs_delayed_item *item)
720 struct btrfs_delayed_item *curr, *next;
721 int free_space;
722 int total_data_size = 0, total_size = 0;
723 struct extent_buffer *leaf;
724 char *data_ptr;
725 struct btrfs_key *keys;
726 u32 *data_size;
727 struct list_head head;
728 int slot;
729 int nitems;
730 int i;
731 int ret = 0;
733 BUG_ON(!path->nodes[0]);
735 leaf = path->nodes[0];
736 free_space = btrfs_leaf_free_space(root, leaf);
737 INIT_LIST_HEAD(&head);
739 next = item;
740 nitems = 0;
743 * count the number of the continuous items that we can insert in batch
745 while (total_size + next->data_len + sizeof(struct btrfs_item) <=
746 free_space) {
747 total_data_size += next->data_len;
748 total_size += next->data_len + sizeof(struct btrfs_item);
749 list_add_tail(&next->tree_list, &head);
750 nitems++;
752 curr = next;
753 next = __btrfs_next_delayed_item(curr);
754 if (!next)
755 break;
757 if (!btrfs_is_continuous_delayed_item(curr, next))
758 break;
761 if (!nitems) {
762 ret = 0;
763 goto out;
767 * we need allocate some memory space, but it might cause the task
768 * to sleep, so we set all locked nodes in the path to blocking locks
769 * first.
771 btrfs_set_path_blocking(path);
773 keys = kmalloc(sizeof(struct btrfs_key) * nitems, GFP_NOFS);
774 if (!keys) {
775 ret = -ENOMEM;
776 goto out;
779 data_size = kmalloc(sizeof(u32) * nitems, GFP_NOFS);
780 if (!data_size) {
781 ret = -ENOMEM;
782 goto error;
785 /* get keys of all the delayed items */
786 i = 0;
787 list_for_each_entry(next, &head, tree_list) {
788 keys[i] = next->key;
789 data_size[i] = next->data_len;
790 i++;
793 /* reset all the locked nodes in the patch to spinning locks. */
794 btrfs_clear_path_blocking(path, NULL, 0);
796 /* insert the keys of the items */
797 setup_items_for_insert(root, path, keys, data_size,
798 total_data_size, total_size, nitems);
800 /* insert the dir index items */
801 slot = path->slots[0];
802 list_for_each_entry_safe(curr, next, &head, tree_list) {
803 data_ptr = btrfs_item_ptr(leaf, slot, char);
804 write_extent_buffer(leaf, &curr->data,
805 (unsigned long)data_ptr,
806 curr->data_len);
807 slot++;
809 btrfs_delayed_item_release_metadata(root, curr);
811 list_del(&curr->tree_list);
812 btrfs_release_delayed_item(curr);
815 error:
816 kfree(data_size);
817 kfree(keys);
818 out:
819 return ret;
823 * This helper can just do simple insertion that needn't extend item for new
824 * data, such as directory name index insertion, inode insertion.
826 static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_path *path,
829 struct btrfs_delayed_item *delayed_item)
831 struct extent_buffer *leaf;
832 char *ptr;
833 int ret;
835 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
836 delayed_item->data_len);
837 if (ret < 0 && ret != -EEXIST)
838 return ret;
840 leaf = path->nodes[0];
842 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
844 write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
845 delayed_item->data_len);
846 btrfs_mark_buffer_dirty(leaf);
848 btrfs_delayed_item_release_metadata(root, delayed_item);
849 return 0;
853 * we insert an item first, then if there are some continuous items, we try
854 * to insert those items into the same leaf.
856 static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
857 struct btrfs_path *path,
858 struct btrfs_root *root,
859 struct btrfs_delayed_node *node)
861 struct btrfs_delayed_item *curr, *prev;
862 int ret = 0;
864 do_again:
865 mutex_lock(&node->mutex);
866 curr = __btrfs_first_delayed_insertion_item(node);
867 if (!curr)
868 goto insert_end;
870 ret = btrfs_insert_delayed_item(trans, root, path, curr);
871 if (ret < 0) {
872 btrfs_release_path(path);
873 goto insert_end;
876 prev = curr;
877 curr = __btrfs_next_delayed_item(prev);
878 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
879 /* insert the continuous items into the same leaf */
880 path->slots[0]++;
881 btrfs_batch_insert_items(root, path, curr);
883 btrfs_release_delayed_item(prev);
884 btrfs_mark_buffer_dirty(path->nodes[0]);
886 btrfs_release_path(path);
887 mutex_unlock(&node->mutex);
888 goto do_again;
890 insert_end:
891 mutex_unlock(&node->mutex);
892 return ret;
895 static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root,
897 struct btrfs_path *path,
898 struct btrfs_delayed_item *item)
900 struct btrfs_delayed_item *curr, *next;
901 struct extent_buffer *leaf;
902 struct btrfs_key key;
903 struct list_head head;
904 int nitems, i, last_item;
905 int ret = 0;
907 BUG_ON(!path->nodes[0]);
909 leaf = path->nodes[0];
911 i = path->slots[0];
912 last_item = btrfs_header_nritems(leaf) - 1;
913 if (i > last_item)
914 return -ENOENT; /* FIXME: Is errno suitable? */
916 next = item;
917 INIT_LIST_HEAD(&head);
918 btrfs_item_key_to_cpu(leaf, &key, i);
919 nitems = 0;
921 * count the number of the dir index items that we can delete in batch
923 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
924 list_add_tail(&next->tree_list, &head);
925 nitems++;
927 curr = next;
928 next = __btrfs_next_delayed_item(curr);
929 if (!next)
930 break;
932 if (!btrfs_is_continuous_delayed_item(curr, next))
933 break;
935 i++;
936 if (i > last_item)
937 break;
938 btrfs_item_key_to_cpu(leaf, &key, i);
941 if (!nitems)
942 return 0;
944 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
945 if (ret)
946 goto out;
948 list_for_each_entry_safe(curr, next, &head, tree_list) {
949 btrfs_delayed_item_release_metadata(root, curr);
950 list_del(&curr->tree_list);
951 btrfs_release_delayed_item(curr);
954 out:
955 return ret;
958 static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
959 struct btrfs_path *path,
960 struct btrfs_root *root,
961 struct btrfs_delayed_node *node)
963 struct btrfs_delayed_item *curr, *prev;
964 int ret = 0;
966 do_again:
967 mutex_lock(&node->mutex);
968 curr = __btrfs_first_delayed_deletion_item(node);
969 if (!curr)
970 goto delete_fail;
972 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
973 if (ret < 0)
974 goto delete_fail;
975 else if (ret > 0) {
977 * can't find the item which the node points to, so this node
978 * is invalid, just drop it.
980 prev = curr;
981 curr = __btrfs_next_delayed_item(prev);
982 btrfs_release_delayed_item(prev);
983 ret = 0;
984 btrfs_release_path(path);
985 if (curr) {
986 mutex_unlock(&node->mutex);
987 goto do_again;
988 } else
989 goto delete_fail;
992 btrfs_batch_delete_items(trans, root, path, curr);
993 btrfs_release_path(path);
994 mutex_unlock(&node->mutex);
995 goto do_again;
997 delete_fail:
998 btrfs_release_path(path);
999 mutex_unlock(&node->mutex);
1000 return ret;
1003 static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
1005 struct btrfs_delayed_root *delayed_root;
1007 if (delayed_node && delayed_node->inode_dirty) {
1008 BUG_ON(!delayed_node->root);
1009 delayed_node->inode_dirty = 0;
1010 delayed_node->count--;
1012 delayed_root = delayed_node->root->fs_info->delayed_root;
1013 finish_one_item(delayed_root);
1017 static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1018 struct btrfs_root *root,
1019 struct btrfs_path *path,
1020 struct btrfs_delayed_node *node)
1022 struct btrfs_key key;
1023 struct btrfs_inode_item *inode_item;
1024 struct extent_buffer *leaf;
1025 int ret;
1027 key.objectid = node->inode_id;
1028 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1029 key.offset = 0;
1031 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1032 if (ret > 0) {
1033 btrfs_release_path(path);
1034 return -ENOENT;
1035 } else if (ret < 0) {
1036 return ret;
1039 btrfs_unlock_up_safe(path, 1);
1040 leaf = path->nodes[0];
1041 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1042 struct btrfs_inode_item);
1043 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1044 sizeof(struct btrfs_inode_item));
1045 btrfs_mark_buffer_dirty(leaf);
1046 btrfs_release_path(path);
1048 btrfs_delayed_inode_release_metadata(root, node);
1049 btrfs_release_delayed_inode(node);
1051 return 0;
1054 static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1055 struct btrfs_root *root,
1056 struct btrfs_path *path,
1057 struct btrfs_delayed_node *node)
1059 int ret;
1061 mutex_lock(&node->mutex);
1062 if (!node->inode_dirty) {
1063 mutex_unlock(&node->mutex);
1064 return 0;
1067 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1068 mutex_unlock(&node->mutex);
1069 return ret;
1072 static inline int
1073 __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1074 struct btrfs_path *path,
1075 struct btrfs_delayed_node *node)
1077 int ret;
1079 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1080 if (ret)
1081 return ret;
1083 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1084 if (ret)
1085 return ret;
1087 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1088 return ret;
1092 * Called when committing the transaction.
1093 * Returns 0 on success.
1094 * Returns < 0 on error and returns with an aborted transaction with any
1095 * outstanding delayed items cleaned up.
1097 static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1098 struct btrfs_root *root, int nr)
1100 struct btrfs_delayed_root *delayed_root;
1101 struct btrfs_delayed_node *curr_node, *prev_node;
1102 struct btrfs_path *path;
1103 struct btrfs_block_rsv *block_rsv;
1104 int ret = 0;
1105 bool count = (nr > 0);
1107 if (trans->aborted)
1108 return -EIO;
1110 path = btrfs_alloc_path();
1111 if (!path)
1112 return -ENOMEM;
1113 path->leave_spinning = 1;
1115 block_rsv = trans->block_rsv;
1116 trans->block_rsv = &root->fs_info->delayed_block_rsv;
1118 delayed_root = btrfs_get_delayed_root(root);
1120 curr_node = btrfs_first_delayed_node(delayed_root);
1121 while (curr_node && (!count || (count && nr--))) {
1122 ret = __btrfs_commit_inode_delayed_items(trans, path,
1123 curr_node);
1124 if (ret) {
1125 btrfs_release_delayed_node(curr_node);
1126 curr_node = NULL;
1127 btrfs_abort_transaction(trans, root, ret);
1128 break;
1131 prev_node = curr_node;
1132 curr_node = btrfs_next_delayed_node(curr_node);
1133 btrfs_release_delayed_node(prev_node);
1136 if (curr_node)
1137 btrfs_release_delayed_node(curr_node);
1138 btrfs_free_path(path);
1139 trans->block_rsv = block_rsv;
1141 return ret;
1144 int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1145 struct btrfs_root *root)
1147 return __btrfs_run_delayed_items(trans, root, -1);
1150 int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
1151 struct btrfs_root *root, int nr)
1153 return __btrfs_run_delayed_items(trans, root, nr);
1156 int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1157 struct inode *inode)
1159 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1160 struct btrfs_path *path;
1161 struct btrfs_block_rsv *block_rsv;
1162 int ret;
1164 if (!delayed_node)
1165 return 0;
1167 mutex_lock(&delayed_node->mutex);
1168 if (!delayed_node->count) {
1169 mutex_unlock(&delayed_node->mutex);
1170 btrfs_release_delayed_node(delayed_node);
1171 return 0;
1173 mutex_unlock(&delayed_node->mutex);
1175 path = btrfs_alloc_path();
1176 if (!path)
1177 return -ENOMEM;
1178 path->leave_spinning = 1;
1180 block_rsv = trans->block_rsv;
1181 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1183 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1185 btrfs_release_delayed_node(delayed_node);
1186 btrfs_free_path(path);
1187 trans->block_rsv = block_rsv;
1189 return ret;
1192 int btrfs_commit_inode_delayed_inode(struct inode *inode)
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 (!delayed_node->inode_dirty) {
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 = &delayed_node->root->fs_info->delayed_block_rsv;
1227 mutex_lock(&delayed_node->mutex);
1228 if (delayed_node->inode_dirty)
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, delayed_node->root);
1239 btrfs_btree_balance_dirty(delayed_node->root);
1240 out:
1241 btrfs_release_delayed_node(delayed_node);
1243 return ret;
1246 void btrfs_remove_delayed_node(struct inode *inode)
1248 struct btrfs_delayed_node *delayed_node;
1250 delayed_node = ACCESS_ONCE(BTRFS_I(inode)->delayed_node);
1251 if (!delayed_node)
1252 return;
1254 BTRFS_I(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 * Maybe new delayed items have been inserted, so we need requeue
1303 * the work. Besides that, we must dequeue the empty delayed nodes
1304 * to avoid the race between delayed items balance and the worker.
1305 * The race like this:
1306 * Task1 Worker thread
1307 * count == 0, needn't requeue
1308 * also needn't insert the
1309 * delayed node into prepare
1310 * list again.
1311 * add lots of delayed items
1312 * queue the delayed node
1313 * already in the list,
1314 * and not in the prepare
1315 * list, it means the delayed
1316 * node is being dealt with
1317 * by the worker.
1318 * do delayed items balance
1319 * the delayed node is being
1320 * dealt with by the worker
1321 * now, just wait.
1322 * the worker goto idle.
1323 * Task1 will sleep until the transaction is commited.
1325 mutex_lock(&delayed_node->mutex);
1326 btrfs_dequeue_delayed_node(root->fs_info->delayed_root, delayed_node);
1327 mutex_unlock(&delayed_node->mutex);
1329 trans->block_rsv = block_rsv;
1330 btrfs_end_transaction_dmeta(trans, root);
1331 btrfs_btree_balance_dirty_nodelay(root);
1333 release_path:
1334 btrfs_release_path(path);
1335 total_done++;
1337 btrfs_release_prepared_delayed_node(delayed_node);
1338 if (async_work->nr == 0 || total_done < async_work->nr)
1339 goto again;
1341 free_path:
1342 btrfs_free_path(path);
1343 out:
1344 wake_up(&delayed_root->wait);
1345 kfree(async_work);
1349 static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1350 struct btrfs_root *root, int nr)
1352 struct btrfs_async_delayed_work *async_work;
1354 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1355 return 0;
1357 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1358 if (!async_work)
1359 return -ENOMEM;
1361 async_work->delayed_root = delayed_root;
1362 async_work->work.func = btrfs_async_run_delayed_root;
1363 async_work->work.flags = 0;
1364 async_work->nr = nr;
1366 btrfs_queue_worker(&root->fs_info->delayed_workers, &async_work->work);
1367 return 0;
1370 void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
1372 struct btrfs_delayed_root *delayed_root;
1373 delayed_root = btrfs_get_delayed_root(root);
1374 WARN_ON(btrfs_first_delayed_node(delayed_root));
1377 static int refs_newer(struct btrfs_delayed_root *delayed_root,
1378 int seq, int count)
1380 int val = atomic_read(&delayed_root->items_seq);
1382 if (val < seq || val >= seq + count)
1383 return 1;
1384 return 0;
1387 void btrfs_balance_delayed_items(struct btrfs_root *root)
1389 struct btrfs_delayed_root *delayed_root;
1390 int seq;
1392 delayed_root = btrfs_get_delayed_root(root);
1394 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1395 return;
1397 seq = atomic_read(&delayed_root->items_seq);
1399 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1400 int ret;
1401 DEFINE_WAIT(__wait);
1403 ret = btrfs_wq_run_delayed_node(delayed_root, root, 0);
1404 if (ret)
1405 return;
1407 while (1) {
1408 prepare_to_wait(&delayed_root->wait, &__wait,
1409 TASK_INTERRUPTIBLE);
1411 if (refs_newer(delayed_root, seq,
1412 BTRFS_DELAYED_BATCH) ||
1413 atomic_read(&delayed_root->items) <
1414 BTRFS_DELAYED_BACKGROUND) {
1415 break;
1417 if (!signal_pending(current))
1418 schedule();
1419 else
1420 break;
1422 finish_wait(&delayed_root->wait, &__wait);
1425 btrfs_wq_run_delayed_node(delayed_root, root, BTRFS_DELAYED_BATCH);
1428 /* Will return 0 or -ENOMEM */
1429 int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1430 struct btrfs_root *root, const char *name,
1431 int name_len, struct inode *dir,
1432 struct btrfs_disk_key *disk_key, u8 type,
1433 u64 index)
1435 struct btrfs_delayed_node *delayed_node;
1436 struct btrfs_delayed_item *delayed_item;
1437 struct btrfs_dir_item *dir_item;
1438 int ret;
1440 delayed_node = btrfs_get_or_create_delayed_node(dir);
1441 if (IS_ERR(delayed_node))
1442 return PTR_ERR(delayed_node);
1444 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1445 if (!delayed_item) {
1446 ret = -ENOMEM;
1447 goto release_node;
1450 delayed_item->key.objectid = btrfs_ino(dir);
1451 btrfs_set_key_type(&delayed_item->key, BTRFS_DIR_INDEX_KEY);
1452 delayed_item->key.offset = index;
1454 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1455 dir_item->location = *disk_key;
1456 dir_item->transid = cpu_to_le64(trans->transid);
1457 dir_item->data_len = 0;
1458 dir_item->name_len = cpu_to_le16(name_len);
1459 dir_item->type = type;
1460 memcpy((char *)(dir_item + 1), name, name_len);
1462 ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item);
1464 * we have reserved enough space when we start a new transaction,
1465 * so reserving metadata failure is impossible
1467 BUG_ON(ret);
1470 mutex_lock(&delayed_node->mutex);
1471 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1472 if (unlikely(ret)) {
1473 printk(KERN_ERR "err add delayed dir index item(name: %s) into "
1474 "the insertion tree of the delayed node"
1475 "(root id: %llu, inode id: %llu, errno: %d)\n",
1476 name,
1477 (unsigned long long)delayed_node->root->objectid,
1478 (unsigned long long)delayed_node->inode_id,
1479 ret);
1480 BUG();
1482 mutex_unlock(&delayed_node->mutex);
1484 release_node:
1485 btrfs_release_delayed_node(delayed_node);
1486 return ret;
1489 static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root,
1490 struct btrfs_delayed_node *node,
1491 struct btrfs_key *key)
1493 struct btrfs_delayed_item *item;
1495 mutex_lock(&node->mutex);
1496 item = __btrfs_lookup_delayed_insertion_item(node, key);
1497 if (!item) {
1498 mutex_unlock(&node->mutex);
1499 return 1;
1502 btrfs_delayed_item_release_metadata(root, item);
1503 btrfs_release_delayed_item(item);
1504 mutex_unlock(&node->mutex);
1505 return 0;
1508 int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1509 struct btrfs_root *root, struct inode *dir,
1510 u64 index)
1512 struct btrfs_delayed_node *node;
1513 struct btrfs_delayed_item *item;
1514 struct btrfs_key item_key;
1515 int ret;
1517 node = btrfs_get_or_create_delayed_node(dir);
1518 if (IS_ERR(node))
1519 return PTR_ERR(node);
1521 item_key.objectid = btrfs_ino(dir);
1522 btrfs_set_key_type(&item_key, BTRFS_DIR_INDEX_KEY);
1523 item_key.offset = index;
1525 ret = btrfs_delete_delayed_insertion_item(root, node, &item_key);
1526 if (!ret)
1527 goto end;
1529 item = btrfs_alloc_delayed_item(0);
1530 if (!item) {
1531 ret = -ENOMEM;
1532 goto end;
1535 item->key = item_key;
1537 ret = btrfs_delayed_item_reserve_metadata(trans, root, item);
1539 * we have reserved enough space when we start a new transaction,
1540 * so reserving metadata failure is impossible.
1542 BUG_ON(ret);
1544 mutex_lock(&node->mutex);
1545 ret = __btrfs_add_delayed_deletion_item(node, item);
1546 if (unlikely(ret)) {
1547 printk(KERN_ERR "err add delayed dir index item(index: %llu) "
1548 "into the deletion tree of the delayed node"
1549 "(root id: %llu, inode id: %llu, errno: %d)\n",
1550 (unsigned long long)index,
1551 (unsigned long long)node->root->objectid,
1552 (unsigned long long)node->inode_id,
1553 ret);
1554 BUG();
1556 mutex_unlock(&node->mutex);
1557 end:
1558 btrfs_release_delayed_node(node);
1559 return ret;
1562 int btrfs_inode_delayed_dir_index_count(struct inode *inode)
1564 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1566 if (!delayed_node)
1567 return -ENOENT;
1570 * Since we have held i_mutex of this directory, it is impossible that
1571 * a new directory index is added into the delayed node and index_cnt
1572 * is updated now. So we needn't lock the delayed node.
1574 if (!delayed_node->index_cnt) {
1575 btrfs_release_delayed_node(delayed_node);
1576 return -EINVAL;
1579 BTRFS_I(inode)->index_cnt = delayed_node->index_cnt;
1580 btrfs_release_delayed_node(delayed_node);
1581 return 0;
1584 void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list,
1585 struct list_head *del_list)
1587 struct btrfs_delayed_node *delayed_node;
1588 struct btrfs_delayed_item *item;
1590 delayed_node = btrfs_get_delayed_node(inode);
1591 if (!delayed_node)
1592 return;
1594 mutex_lock(&delayed_node->mutex);
1595 item = __btrfs_first_delayed_insertion_item(delayed_node);
1596 while (item) {
1597 atomic_inc(&item->refs);
1598 list_add_tail(&item->readdir_list, ins_list);
1599 item = __btrfs_next_delayed_item(item);
1602 item = __btrfs_first_delayed_deletion_item(delayed_node);
1603 while (item) {
1604 atomic_inc(&item->refs);
1605 list_add_tail(&item->readdir_list, del_list);
1606 item = __btrfs_next_delayed_item(item);
1608 mutex_unlock(&delayed_node->mutex);
1610 * This delayed node is still cached in the btrfs inode, so refs
1611 * must be > 1 now, and we needn't check it is going to be freed
1612 * or not.
1614 * Besides that, this function is used to read dir, we do not
1615 * insert/delete delayed items in this period. So we also needn't
1616 * requeue or dequeue this delayed node.
1618 atomic_dec(&delayed_node->refs);
1621 void btrfs_put_delayed_items(struct list_head *ins_list,
1622 struct list_head *del_list)
1624 struct btrfs_delayed_item *curr, *next;
1626 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1627 list_del(&curr->readdir_list);
1628 if (atomic_dec_and_test(&curr->refs))
1629 kfree(curr);
1632 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1633 list_del(&curr->readdir_list);
1634 if (atomic_dec_and_test(&curr->refs))
1635 kfree(curr);
1639 int btrfs_should_delete_dir_index(struct list_head *del_list,
1640 u64 index)
1642 struct btrfs_delayed_item *curr, *next;
1643 int ret;
1645 if (list_empty(del_list))
1646 return 0;
1648 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1649 if (curr->key.offset > index)
1650 break;
1652 list_del(&curr->readdir_list);
1653 ret = (curr->key.offset == index);
1655 if (atomic_dec_and_test(&curr->refs))
1656 kfree(curr);
1658 if (ret)
1659 return 1;
1660 else
1661 continue;
1663 return 0;
1667 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1670 int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
1671 struct list_head *ins_list)
1673 struct btrfs_dir_item *di;
1674 struct btrfs_delayed_item *curr, *next;
1675 struct btrfs_key location;
1676 char *name;
1677 int name_len;
1678 int over = 0;
1679 unsigned char d_type;
1681 if (list_empty(ins_list))
1682 return 0;
1685 * Changing the data of the delayed item is impossible. So
1686 * we needn't lock them. And we have held i_mutex of the
1687 * directory, nobody can delete any directory indexes now.
1689 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1690 list_del(&curr->readdir_list);
1692 if (curr->key.offset < ctx->pos) {
1693 if (atomic_dec_and_test(&curr->refs))
1694 kfree(curr);
1695 continue;
1698 ctx->pos = curr->key.offset;
1700 di = (struct btrfs_dir_item *)curr->data;
1701 name = (char *)(di + 1);
1702 name_len = le16_to_cpu(di->name_len);
1704 d_type = btrfs_filetype_table[di->type];
1705 btrfs_disk_key_to_cpu(&location, &di->location);
1707 over = !dir_emit(ctx, name, name_len,
1708 location.objectid, d_type);
1710 if (atomic_dec_and_test(&curr->refs))
1711 kfree(curr);
1713 if (over)
1714 return 1;
1716 return 0;
1719 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
1720 generation, 64);
1721 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
1722 sequence, 64);
1723 BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
1724 transid, 64);
1725 BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
1726 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
1727 nbytes, 64);
1728 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
1729 block_group, 64);
1730 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
1731 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
1732 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
1733 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
1734 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
1735 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
1737 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
1738 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
1740 static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1741 struct btrfs_inode_item *inode_item,
1742 struct inode *inode)
1744 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1745 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
1746 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1747 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1748 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1749 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1750 btrfs_set_stack_inode_generation(inode_item,
1751 BTRFS_I(inode)->generation);
1752 btrfs_set_stack_inode_sequence(inode_item, inode->i_version);
1753 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1754 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1755 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags);
1756 btrfs_set_stack_inode_block_group(inode_item, 0);
1758 btrfs_set_stack_timespec_sec(btrfs_inode_atime(inode_item),
1759 inode->i_atime.tv_sec);
1760 btrfs_set_stack_timespec_nsec(btrfs_inode_atime(inode_item),
1761 inode->i_atime.tv_nsec);
1763 btrfs_set_stack_timespec_sec(btrfs_inode_mtime(inode_item),
1764 inode->i_mtime.tv_sec);
1765 btrfs_set_stack_timespec_nsec(btrfs_inode_mtime(inode_item),
1766 inode->i_mtime.tv_nsec);
1768 btrfs_set_stack_timespec_sec(btrfs_inode_ctime(inode_item),
1769 inode->i_ctime.tv_sec);
1770 btrfs_set_stack_timespec_nsec(btrfs_inode_ctime(inode_item),
1771 inode->i_ctime.tv_nsec);
1774 int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1776 struct btrfs_delayed_node *delayed_node;
1777 struct btrfs_inode_item *inode_item;
1778 struct btrfs_timespec *tspec;
1780 delayed_node = btrfs_get_delayed_node(inode);
1781 if (!delayed_node)
1782 return -ENOENT;
1784 mutex_lock(&delayed_node->mutex);
1785 if (!delayed_node->inode_dirty) {
1786 mutex_unlock(&delayed_node->mutex);
1787 btrfs_release_delayed_node(delayed_node);
1788 return -ENOENT;
1791 inode_item = &delayed_node->inode_item;
1793 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1794 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
1795 btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
1796 inode->i_mode = btrfs_stack_inode_mode(inode_item);
1797 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
1798 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1799 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
1800 inode->i_version = btrfs_stack_inode_sequence(inode_item);
1801 inode->i_rdev = 0;
1802 *rdev = btrfs_stack_inode_rdev(inode_item);
1803 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1805 tspec = btrfs_inode_atime(inode_item);
1806 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(tspec);
1807 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1809 tspec = btrfs_inode_mtime(inode_item);
1810 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(tspec);
1811 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1813 tspec = btrfs_inode_ctime(inode_item);
1814 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(tspec);
1815 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1817 inode->i_generation = BTRFS_I(inode)->generation;
1818 BTRFS_I(inode)->index_cnt = (u64)-1;
1820 mutex_unlock(&delayed_node->mutex);
1821 btrfs_release_delayed_node(delayed_node);
1822 return 0;
1825 int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1826 struct btrfs_root *root, struct inode *inode)
1828 struct btrfs_delayed_node *delayed_node;
1829 int ret = 0;
1831 delayed_node = btrfs_get_or_create_delayed_node(inode);
1832 if (IS_ERR(delayed_node))
1833 return PTR_ERR(delayed_node);
1835 mutex_lock(&delayed_node->mutex);
1836 if (delayed_node->inode_dirty) {
1837 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1838 goto release_node;
1841 ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode,
1842 delayed_node);
1843 if (ret)
1844 goto release_node;
1846 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1847 delayed_node->inode_dirty = 1;
1848 delayed_node->count++;
1849 atomic_inc(&root->fs_info->delayed_root->items);
1850 release_node:
1851 mutex_unlock(&delayed_node->mutex);
1852 btrfs_release_delayed_node(delayed_node);
1853 return ret;
1856 static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1858 struct btrfs_root *root = delayed_node->root;
1859 struct btrfs_delayed_item *curr_item, *prev_item;
1861 mutex_lock(&delayed_node->mutex);
1862 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1863 while (curr_item) {
1864 btrfs_delayed_item_release_metadata(root, curr_item);
1865 prev_item = curr_item;
1866 curr_item = __btrfs_next_delayed_item(prev_item);
1867 btrfs_release_delayed_item(prev_item);
1870 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1871 while (curr_item) {
1872 btrfs_delayed_item_release_metadata(root, curr_item);
1873 prev_item = curr_item;
1874 curr_item = __btrfs_next_delayed_item(prev_item);
1875 btrfs_release_delayed_item(prev_item);
1878 if (delayed_node->inode_dirty) {
1879 btrfs_delayed_inode_release_metadata(root, delayed_node);
1880 btrfs_release_delayed_inode(delayed_node);
1882 mutex_unlock(&delayed_node->mutex);
1885 void btrfs_kill_delayed_inode_items(struct inode *inode)
1887 struct btrfs_delayed_node *delayed_node;
1889 delayed_node = btrfs_get_delayed_node(inode);
1890 if (!delayed_node)
1891 return;
1893 __btrfs_kill_delayed_node(delayed_node);
1894 btrfs_release_delayed_node(delayed_node);
1897 void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1899 u64 inode_id = 0;
1900 struct btrfs_delayed_node *delayed_nodes[8];
1901 int i, n;
1903 while (1) {
1904 spin_lock(&root->inode_lock);
1905 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1906 (void **)delayed_nodes, inode_id,
1907 ARRAY_SIZE(delayed_nodes));
1908 if (!n) {
1909 spin_unlock(&root->inode_lock);
1910 break;
1913 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1915 for (i = 0; i < n; i++)
1916 atomic_inc(&delayed_nodes[i]->refs);
1917 spin_unlock(&root->inode_lock);
1919 for (i = 0; i < n; i++) {
1920 __btrfs_kill_delayed_node(delayed_nodes[i]);
1921 btrfs_release_delayed_node(delayed_nodes[i]);
1926 void btrfs_destroy_delayed_inodes(struct btrfs_root *root)
1928 struct btrfs_delayed_root *delayed_root;
1929 struct btrfs_delayed_node *curr_node, *prev_node;
1931 delayed_root = btrfs_get_delayed_root(root);
1933 curr_node = btrfs_first_delayed_node(delayed_root);
1934 while (curr_node) {
1935 __btrfs_kill_delayed_node(curr_node);
1937 prev_node = curr_node;
1938 curr_node = btrfs_next_delayed_node(curr_node);
1939 btrfs_release_delayed_node(prev_node);