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