blob: 0be47e4b813672996b490515ef88af357c40a276 [file] [log] [blame]
Miao Xie16cdcec2011-04-22 18:12:22 +08001/*
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 "delayed-inode.h"
22#include "disk-io.h"
23#include "transaction.h"
Qu Wenruo3cae2102013-07-16 11:19:18 +080024#include "ctree.h"
Miao Xie16cdcec2011-04-22 18:12:22 +080025
Chris Masonde3cb942013-03-04 17:13:31 -050026#define BTRFS_DELAYED_WRITEBACK 512
27#define BTRFS_DELAYED_BACKGROUND 128
28#define BTRFS_DELAYED_BATCH 16
Miao Xie16cdcec2011-04-22 18:12:22 +080029
30static struct kmem_cache *delayed_node_cache;
31
32int __init btrfs_delayed_inode_init(void)
33{
David Sterba837e1972012-09-07 03:00:48 -060034 delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
Miao Xie16cdcec2011-04-22 18:12:22 +080035 sizeof(struct btrfs_delayed_node),
36 0,
37 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
38 NULL);
39 if (!delayed_node_cache)
40 return -ENOMEM;
41 return 0;
42}
43
44void btrfs_delayed_inode_exit(void)
45{
46 if (delayed_node_cache)
47 kmem_cache_destroy(delayed_node_cache);
48}
49
50static 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 atomic_set(&delayed_node->refs, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +080057 delayed_node->ins_root = RB_ROOT;
58 delayed_node->del_root = RB_ROOT;
59 mutex_init(&delayed_node->mutex);
Miao Xie16cdcec2011-04-22 18:12:22 +080060 INIT_LIST_HEAD(&delayed_node->n_list);
61 INIT_LIST_HEAD(&delayed_node->p_list);
Miao Xie16cdcec2011-04-22 18:12:22 +080062}
63
64static 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
76static inline struct btrfs_delayed_root *btrfs_get_delayed_root(
77 struct btrfs_root *root)
78{
79 return root->fs_info->delayed_root;
80}
81
Miao Xie2f7e33d2011-06-23 07:27:13 +000082static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode)
83{
84 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
85 struct btrfs_root *root = btrfs_inode->root;
86 u64 ino = btrfs_ino(inode);
87 struct btrfs_delayed_node *node;
88
89 node = ACCESS_ONCE(btrfs_inode->delayed_node);
90 if (node) {
91 atomic_inc(&node->refs);
92 return node;
93 }
94
95 spin_lock(&root->inode_lock);
96 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
97 if (node) {
98 if (btrfs_inode->delayed_node) {
99 atomic_inc(&node->refs); /* can be accessed */
100 BUG_ON(btrfs_inode->delayed_node != node);
101 spin_unlock(&root->inode_lock);
102 return node;
103 }
104 btrfs_inode->delayed_node = node;
Rashika95e94d12013-10-31 03:12:42 +0530105 /* can be accessed and cached in the inode */
106 atomic_add(2, &node->refs);
Miao Xie2f7e33d2011-06-23 07:27:13 +0000107 spin_unlock(&root->inode_lock);
108 return node;
109 }
110 spin_unlock(&root->inode_lock);
111
112 return NULL;
113}
114
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100115/* Will return either the node or PTR_ERR(-ENOMEM) */
Miao Xie16cdcec2011-04-22 18:12:22 +0800116static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
117 struct inode *inode)
118{
119 struct btrfs_delayed_node *node;
120 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
121 struct btrfs_root *root = btrfs_inode->root;
Chris Mason0d0ca302011-05-22 07:11:22 -0400122 u64 ino = btrfs_ino(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +0800123 int ret;
124
125again:
Miao Xie2f7e33d2011-06-23 07:27:13 +0000126 node = btrfs_get_delayed_node(inode);
127 if (node)
Miao Xie16cdcec2011-04-22 18:12:22 +0800128 return node;
Miao Xie16cdcec2011-04-22 18:12:22 +0800129
Alexandru Moise352dd9c2015-10-25 20:15:06 +0000130 node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800131 if (!node)
132 return ERR_PTR(-ENOMEM);
Chris Mason0d0ca302011-05-22 07:11:22 -0400133 btrfs_init_delayed_node(node, root, ino);
Miao Xie16cdcec2011-04-22 18:12:22 +0800134
Rashika95e94d12013-10-31 03:12:42 +0530135 /* cached in the btrfs inode and can be accessed */
136 atomic_add(2, &node->refs);
Miao Xie16cdcec2011-04-22 18:12:22 +0800137
138 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
139 if (ret) {
140 kmem_cache_free(delayed_node_cache, node);
141 return ERR_PTR(ret);
142 }
143
144 spin_lock(&root->inode_lock);
Chris Mason0d0ca302011-05-22 07:11:22 -0400145 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
Miao Xie16cdcec2011-04-22 18:12:22 +0800146 if (ret == -EEXIST) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800147 spin_unlock(&root->inode_lock);
Jeff Mahoney96493032014-05-27 13:53:20 -0400148 kmem_cache_free(delayed_node_cache, node);
Miao Xie16cdcec2011-04-22 18:12:22 +0800149 radix_tree_preload_end();
150 goto again;
151 }
152 btrfs_inode->delayed_node = node;
153 spin_unlock(&root->inode_lock);
154 radix_tree_preload_end();
155
156 return node;
157}
158
159/*
160 * Call it when holding delayed_node->mutex
161 *
162 * If mod = 1, add this node into the prepared list.
163 */
164static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
165 struct btrfs_delayed_node *node,
166 int mod)
167{
168 spin_lock(&root->lock);
Miao Xie7cf35d92013-12-26 13:07:05 +0800169 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800170 if (!list_empty(&node->p_list))
171 list_move_tail(&node->p_list, &root->prepare_list);
172 else if (mod)
173 list_add_tail(&node->p_list, &root->prepare_list);
174 } else {
175 list_add_tail(&node->n_list, &root->node_list);
176 list_add_tail(&node->p_list, &root->prepare_list);
177 atomic_inc(&node->refs); /* inserted into list */
178 root->nodes++;
Miao Xie7cf35d92013-12-26 13:07:05 +0800179 set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +0800180 }
181 spin_unlock(&root->lock);
182}
183
184/* Call it when holding delayed_node->mutex */
185static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
186 struct btrfs_delayed_node *node)
187{
188 spin_lock(&root->lock);
Miao Xie7cf35d92013-12-26 13:07:05 +0800189 if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +0800190 root->nodes--;
191 atomic_dec(&node->refs); /* not in the list */
192 list_del_init(&node->n_list);
193 if (!list_empty(&node->p_list))
194 list_del_init(&node->p_list);
Miao Xie7cf35d92013-12-26 13:07:05 +0800195 clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +0800196 }
197 spin_unlock(&root->lock);
198}
199
Eric Sandeen48a3b632013-04-25 20:41:01 +0000200static struct btrfs_delayed_node *btrfs_first_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800201 struct btrfs_delayed_root *delayed_root)
202{
203 struct list_head *p;
204 struct btrfs_delayed_node *node = NULL;
205
206 spin_lock(&delayed_root->lock);
207 if (list_empty(&delayed_root->node_list))
208 goto out;
209
210 p = delayed_root->node_list.next;
211 node = list_entry(p, struct btrfs_delayed_node, n_list);
212 atomic_inc(&node->refs);
213out:
214 spin_unlock(&delayed_root->lock);
215
216 return node;
217}
218
Eric Sandeen48a3b632013-04-25 20:41:01 +0000219static struct btrfs_delayed_node *btrfs_next_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800220 struct btrfs_delayed_node *node)
221{
222 struct btrfs_delayed_root *delayed_root;
223 struct list_head *p;
224 struct btrfs_delayed_node *next = NULL;
225
226 delayed_root = node->root->fs_info->delayed_root;
227 spin_lock(&delayed_root->lock);
Miao Xie7cf35d92013-12-26 13:07:05 +0800228 if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
229 /* not in the list */
Miao Xie16cdcec2011-04-22 18:12:22 +0800230 if (list_empty(&delayed_root->node_list))
231 goto out;
232 p = delayed_root->node_list.next;
233 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
234 goto out;
235 else
236 p = node->n_list.next;
237
238 next = list_entry(p, struct btrfs_delayed_node, n_list);
239 atomic_inc(&next->refs);
240out:
241 spin_unlock(&delayed_root->lock);
242
243 return next;
244}
245
246static void __btrfs_release_delayed_node(
247 struct btrfs_delayed_node *delayed_node,
248 int mod)
249{
250 struct btrfs_delayed_root *delayed_root;
251
252 if (!delayed_node)
253 return;
254
255 delayed_root = delayed_node->root->fs_info->delayed_root;
256
257 mutex_lock(&delayed_node->mutex);
258 if (delayed_node->count)
259 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
260 else
261 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
262 mutex_unlock(&delayed_node->mutex);
263
264 if (atomic_dec_and_test(&delayed_node->refs)) {
Jeff Mahoney96493032014-05-27 13:53:20 -0400265 bool free = false;
Miao Xie16cdcec2011-04-22 18:12:22 +0800266 struct btrfs_root *root = delayed_node->root;
267 spin_lock(&root->inode_lock);
268 if (atomic_read(&delayed_node->refs) == 0) {
269 radix_tree_delete(&root->delayed_nodes_tree,
270 delayed_node->inode_id);
Jeff Mahoney96493032014-05-27 13:53:20 -0400271 free = true;
Miao Xie16cdcec2011-04-22 18:12:22 +0800272 }
273 spin_unlock(&root->inode_lock);
Jeff Mahoney96493032014-05-27 13:53:20 -0400274 if (free)
275 kmem_cache_free(delayed_node_cache, delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +0800276 }
277}
278
279static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
280{
281 __btrfs_release_delayed_node(node, 0);
282}
283
Eric Sandeen48a3b632013-04-25 20:41:01 +0000284static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800285 struct btrfs_delayed_root *delayed_root)
286{
287 struct list_head *p;
288 struct btrfs_delayed_node *node = NULL;
289
290 spin_lock(&delayed_root->lock);
291 if (list_empty(&delayed_root->prepare_list))
292 goto out;
293
294 p = delayed_root->prepare_list.next;
295 list_del_init(p);
296 node = list_entry(p, struct btrfs_delayed_node, p_list);
297 atomic_inc(&node->refs);
298out:
299 spin_unlock(&delayed_root->lock);
300
301 return node;
302}
303
304static inline void btrfs_release_prepared_delayed_node(
305 struct btrfs_delayed_node *node)
306{
307 __btrfs_release_delayed_node(node, 1);
308}
309
Eric Sandeen48a3b632013-04-25 20:41:01 +0000310static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
Miao Xie16cdcec2011-04-22 18:12:22 +0800311{
312 struct btrfs_delayed_item *item;
313 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
314 if (item) {
315 item->data_len = data_len;
316 item->ins_or_del = 0;
317 item->bytes_reserved = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +0800318 item->delayed_node = NULL;
319 atomic_set(&item->refs, 1);
320 }
321 return item;
322}
323
324/*
325 * __btrfs_lookup_delayed_item - look up the delayed item by key
326 * @delayed_node: pointer to the delayed node
327 * @key: the key to look up
328 * @prev: used to store the prev item if the right item isn't found
329 * @next: used to store the next item if the right item isn't found
330 *
331 * Note: if we don't find the right item, we will return the prev item and
332 * the next item.
333 */
334static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
335 struct rb_root *root,
336 struct btrfs_key *key,
337 struct btrfs_delayed_item **prev,
338 struct btrfs_delayed_item **next)
339{
340 struct rb_node *node, *prev_node = NULL;
341 struct btrfs_delayed_item *delayed_item = NULL;
342 int ret = 0;
343
344 node = root->rb_node;
345
346 while (node) {
347 delayed_item = rb_entry(node, struct btrfs_delayed_item,
348 rb_node);
349 prev_node = node;
350 ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
351 if (ret < 0)
352 node = node->rb_right;
353 else if (ret > 0)
354 node = node->rb_left;
355 else
356 return delayed_item;
357 }
358
359 if (prev) {
360 if (!prev_node)
361 *prev = NULL;
362 else if (ret < 0)
363 *prev = delayed_item;
364 else if ((node = rb_prev(prev_node)) != NULL) {
365 *prev = rb_entry(node, struct btrfs_delayed_item,
366 rb_node);
367 } else
368 *prev = NULL;
369 }
370
371 if (next) {
372 if (!prev_node)
373 *next = NULL;
374 else if (ret > 0)
375 *next = delayed_item;
376 else if ((node = rb_next(prev_node)) != NULL) {
377 *next = rb_entry(node, struct btrfs_delayed_item,
378 rb_node);
379 } else
380 *next = NULL;
381 }
382 return NULL;
383}
384
Eric Sandeen48a3b632013-04-25 20:41:01 +0000385static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800386 struct btrfs_delayed_node *delayed_node,
387 struct btrfs_key *key)
388{
389 struct btrfs_delayed_item *item;
390
391 item = __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
392 NULL, NULL);
393 return item;
394}
395
Miao Xie16cdcec2011-04-22 18:12:22 +0800396static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
397 struct btrfs_delayed_item *ins,
398 int action)
399{
400 struct rb_node **p, *node;
401 struct rb_node *parent_node = NULL;
402 struct rb_root *root;
403 struct btrfs_delayed_item *item;
404 int cmp;
405
406 if (action == BTRFS_DELAYED_INSERTION_ITEM)
407 root = &delayed_node->ins_root;
408 else if (action == BTRFS_DELAYED_DELETION_ITEM)
409 root = &delayed_node->del_root;
410 else
411 BUG();
412 p = &root->rb_node;
413 node = &ins->rb_node;
414
415 while (*p) {
416 parent_node = *p;
417 item = rb_entry(parent_node, struct btrfs_delayed_item,
418 rb_node);
419
420 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
421 if (cmp < 0)
422 p = &(*p)->rb_right;
423 else if (cmp > 0)
424 p = &(*p)->rb_left;
425 else
426 return -EEXIST;
427 }
428
429 rb_link_node(node, parent_node, p);
430 rb_insert_color(node, root);
431 ins->delayed_node = delayed_node;
432 ins->ins_or_del = action;
433
434 if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
435 action == BTRFS_DELAYED_INSERTION_ITEM &&
436 ins->key.offset >= delayed_node->index_cnt)
437 delayed_node->index_cnt = ins->key.offset + 1;
438
439 delayed_node->count++;
440 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
441 return 0;
442}
443
444static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
445 struct btrfs_delayed_item *item)
446{
447 return __btrfs_add_delayed_item(node, item,
448 BTRFS_DELAYED_INSERTION_ITEM);
449}
450
451static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
452 struct btrfs_delayed_item *item)
453{
454 return __btrfs_add_delayed_item(node, item,
455 BTRFS_DELAYED_DELETION_ITEM);
456}
457
Chris Masonde3cb942013-03-04 17:13:31 -0500458static void finish_one_item(struct btrfs_delayed_root *delayed_root)
459{
460 int seq = atomic_inc_return(&delayed_root->items_seq);
David Sterbaee863952015-02-16 19:41:40 +0100461
462 /*
463 * atomic_dec_return implies a barrier for waitqueue_active
464 */
Chris Masonde3cb942013-03-04 17:13:31 -0500465 if ((atomic_dec_return(&delayed_root->items) <
466 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
467 waitqueue_active(&delayed_root->wait))
468 wake_up(&delayed_root->wait);
469}
470
Miao Xie16cdcec2011-04-22 18:12:22 +0800471static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
472{
473 struct rb_root *root;
474 struct btrfs_delayed_root *delayed_root;
475
476 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
477
478 BUG_ON(!delayed_root);
479 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
480 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
481
482 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
483 root = &delayed_item->delayed_node->ins_root;
484 else
485 root = &delayed_item->delayed_node->del_root;
486
487 rb_erase(&delayed_item->rb_node, root);
488 delayed_item->delayed_node->count--;
Chris Masonde3cb942013-03-04 17:13:31 -0500489
490 finish_one_item(delayed_root);
Miao Xie16cdcec2011-04-22 18:12:22 +0800491}
492
493static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
494{
495 if (item) {
496 __btrfs_remove_delayed_item(item);
497 if (atomic_dec_and_test(&item->refs))
498 kfree(item);
499 }
500}
501
Eric Sandeen48a3b632013-04-25 20:41:01 +0000502static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800503 struct btrfs_delayed_node *delayed_node)
504{
505 struct rb_node *p;
506 struct btrfs_delayed_item *item = NULL;
507
508 p = rb_first(&delayed_node->ins_root);
509 if (p)
510 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
511
512 return item;
513}
514
Eric Sandeen48a3b632013-04-25 20:41:01 +0000515static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800516 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->del_root);
522 if (p)
523 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
524
525 return item;
526}
527
Eric Sandeen48a3b632013-04-25 20:41:01 +0000528static struct btrfs_delayed_item *__btrfs_next_delayed_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800529 struct btrfs_delayed_item *item)
530{
531 struct rb_node *p;
532 struct btrfs_delayed_item *next = NULL;
533
534 p = rb_next(&item->rb_node);
535 if (p)
536 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
537
538 return next;
539}
540
Miao Xie16cdcec2011-04-22 18:12:22 +0800541static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
542 struct btrfs_root *root,
543 struct btrfs_delayed_item *item)
544{
545 struct btrfs_block_rsv *src_rsv;
546 struct btrfs_block_rsv *dst_rsv;
547 u64 num_bytes;
548 int ret;
549
550 if (!trans->bytes_reserved)
551 return 0;
552
553 src_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -0400554 dst_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +0800555
556 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
557 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500558 if (!ret) {
559 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
560 item->key.objectid,
561 num_bytes, 1);
Miao Xie16cdcec2011-04-22 18:12:22 +0800562 item->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500563 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800564
565 return ret;
566}
567
568static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
569 struct btrfs_delayed_item *item)
570{
Miao Xie19fd2942011-06-15 10:47:30 +0000571 struct btrfs_block_rsv *rsv;
572
Miao Xie16cdcec2011-04-22 18:12:22 +0800573 if (!item->bytes_reserved)
574 return;
575
Josef Bacik6d668dd2011-11-03 22:54:25 -0400576 rsv = &root->fs_info->delayed_block_rsv;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500577 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
578 item->key.objectid, item->bytes_reserved,
579 0);
Miao Xie19fd2942011-06-15 10:47:30 +0000580 btrfs_block_rsv_release(root, rsv,
Miao Xie16cdcec2011-04-22 18:12:22 +0800581 item->bytes_reserved);
582}
583
584static int btrfs_delayed_inode_reserve_metadata(
585 struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500587 struct inode *inode,
Miao Xie16cdcec2011-04-22 18:12:22 +0800588 struct btrfs_delayed_node *node)
589{
590 struct btrfs_block_rsv *src_rsv;
591 struct btrfs_block_rsv *dst_rsv;
592 u64 num_bytes;
593 int ret;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500594 bool release = false;
Miao Xie16cdcec2011-04-22 18:12:22 +0800595
Miao Xie16cdcec2011-04-22 18:12:22 +0800596 src_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -0400597 dst_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +0800598
599 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacikc06a0e12011-11-04 19:56:02 -0400600
601 /*
602 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
603 * which doesn't reserve space for speed. This is a problem since we
604 * still need to reserve space for this update, so try to reserve the
605 * space.
606 *
607 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
608 * we're accounted for.
609 */
Chris Masone755d9a2011-12-15 13:36:29 -0500610 if (!src_rsv || (!trans->bytes_reserved &&
Miao Xie66d8f3d2012-09-06 04:02:28 -0600611 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
Miao Xie08e007d2012-10-16 11:33:38 +0000612 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
613 BTRFS_RESERVE_NO_FLUSH);
Josef Bacikc06a0e12011-11-04 19:56:02 -0400614 /*
615 * Since we're under a transaction reserve_metadata_bytes could
616 * try to commit the transaction which will make it return
617 * EAGAIN to make us stop the transaction we have, so return
618 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
619 */
620 if (ret == -EAGAIN)
621 ret = -ENOSPC;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500622 if (!ret) {
Josef Bacikc06a0e12011-11-04 19:56:02 -0400623 node->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500624 trace_btrfs_space_reservation(root->fs_info,
625 "delayed_inode",
626 btrfs_ino(inode),
627 num_bytes, 1);
628 }
Josef Bacikc06a0e12011-11-04 19:56:02 -0400629 return ret;
Miao Xie66d8f3d2012-09-06 04:02:28 -0600630 } else if (src_rsv->type == BTRFS_BLOCK_RSV_DELALLOC) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500631 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik72ac3c02012-05-23 14:13:11 -0400632 if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
633 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500634 spin_unlock(&BTRFS_I(inode)->lock);
635 release = true;
636 goto migrate;
637 }
638 spin_unlock(&BTRFS_I(inode)->lock);
639
640 /* Ok we didn't have space pre-reserved. This shouldn't happen
641 * too often but it can happen if we do delalloc to an existing
642 * inode which gets dirtied because of the time update, and then
643 * isn't touched again until after the transaction commits and
644 * then we try to write out the data. First try to be nice and
645 * reserve something strictly for us. If not be a pain and try
646 * to steal from the delalloc block rsv.
647 */
Miao Xie08e007d2012-10-16 11:33:38 +0000648 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
649 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500650 if (!ret)
651 goto out;
652
653 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530654 if (!WARN_ON(ret))
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500655 goto out;
656
657 /*
658 * Ok this is a problem, let's just steal from the global rsv
659 * since this really shouldn't happen that often.
660 */
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500661 ret = btrfs_block_rsv_migrate(&root->fs_info->global_block_rsv,
662 dst_rsv, num_bytes);
663 goto out;
Josef Bacikc06a0e12011-11-04 19:56:02 -0400664 }
665
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500666migrate:
Miao Xie16cdcec2011-04-22 18:12:22 +0800667 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500668
669out:
670 /*
671 * Migrate only takes a reservation, it doesn't touch the size of the
672 * block_rsv. This is to simplify people who don't normally have things
673 * migrated from their block rsv. If they go to release their
674 * reservation, that will decrease the size as well, so if migrate
675 * reduced size we'd end up with a negative size. But for the
676 * delalloc_meta_reserved stuff we will only know to drop 1 reservation,
677 * but we could in fact do this reserve/migrate dance several times
678 * between the time we did the original reservation and we'd clean it
679 * up. So to take care of this, release the space for the meta
680 * reservation here. I think it may be time for a documentation page on
681 * how block rsvs. work.
682 */
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500683 if (!ret) {
684 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
685 btrfs_ino(inode), num_bytes, 1);
Miao Xie16cdcec2011-04-22 18:12:22 +0800686 node->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500687 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800688
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500689 if (release) {
690 trace_btrfs_space_reservation(root->fs_info, "delalloc",
691 btrfs_ino(inode), num_bytes, 0);
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500692 btrfs_block_rsv_release(root, src_rsv, num_bytes);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500693 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800694
695 return ret;
696}
697
698static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
699 struct btrfs_delayed_node *node)
700{
701 struct btrfs_block_rsv *rsv;
702
703 if (!node->bytes_reserved)
704 return;
705
Josef Bacik6d668dd2011-11-03 22:54:25 -0400706 rsv = &root->fs_info->delayed_block_rsv;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500707 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
708 node->inode_id, node->bytes_reserved, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +0800709 btrfs_block_rsv_release(root, rsv,
710 node->bytes_reserved);
711 node->bytes_reserved = 0;
712}
713
714/*
715 * This helper will insert some continuous items into the same leaf according
716 * to the free space of the leaf.
717 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000718static int btrfs_batch_insert_items(struct btrfs_root *root,
719 struct btrfs_path *path,
720 struct btrfs_delayed_item *item)
Miao Xie16cdcec2011-04-22 18:12:22 +0800721{
722 struct btrfs_delayed_item *curr, *next;
723 int free_space;
724 int total_data_size = 0, total_size = 0;
725 struct extent_buffer *leaf;
726 char *data_ptr;
727 struct btrfs_key *keys;
728 u32 *data_size;
729 struct list_head head;
730 int slot;
731 int nitems;
732 int i;
733 int ret = 0;
734
735 BUG_ON(!path->nodes[0]);
736
737 leaf = path->nodes[0];
738 free_space = btrfs_leaf_free_space(root, leaf);
739 INIT_LIST_HEAD(&head);
740
741 next = item;
Chris Mason17aca1c2011-06-03 01:13:45 -0400742 nitems = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +0800743
744 /*
745 * count the number of the continuous items that we can insert in batch
746 */
747 while (total_size + next->data_len + sizeof(struct btrfs_item) <=
748 free_space) {
749 total_data_size += next->data_len;
750 total_size += next->data_len + sizeof(struct btrfs_item);
751 list_add_tail(&next->tree_list, &head);
752 nitems++;
753
754 curr = next;
755 next = __btrfs_next_delayed_item(curr);
756 if (!next)
757 break;
758
759 if (!btrfs_is_continuous_delayed_item(curr, next))
760 break;
761 }
762
763 if (!nitems) {
764 ret = 0;
765 goto out;
766 }
767
768 /*
769 * we need allocate some memory space, but it might cause the task
770 * to sleep, so we set all locked nodes in the path to blocking locks
771 * first.
772 */
773 btrfs_set_path_blocking(path);
774
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +0530775 keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800776 if (!keys) {
777 ret = -ENOMEM;
778 goto out;
779 }
780
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +0530781 data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800782 if (!data_size) {
783 ret = -ENOMEM;
784 goto error;
785 }
786
787 /* get keys of all the delayed items */
788 i = 0;
789 list_for_each_entry(next, &head, tree_list) {
790 keys[i] = next->key;
791 data_size[i] = next->data_len;
792 i++;
793 }
794
795 /* reset all the locked nodes in the patch to spinning locks. */
Chris Masonbd681512011-07-16 15:23:14 -0400796 btrfs_clear_path_blocking(path, NULL, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +0800797
798 /* insert the keys of the items */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000799 setup_items_for_insert(root, path, keys, data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100800 total_data_size, total_size, nitems);
Miao Xie16cdcec2011-04-22 18:12:22 +0800801
802 /* insert the dir index items */
803 slot = path->slots[0];
804 list_for_each_entry_safe(curr, next, &head, tree_list) {
805 data_ptr = btrfs_item_ptr(leaf, slot, char);
806 write_extent_buffer(leaf, &curr->data,
807 (unsigned long)data_ptr,
808 curr->data_len);
809 slot++;
810
811 btrfs_delayed_item_release_metadata(root, curr);
812
813 list_del(&curr->tree_list);
814 btrfs_release_delayed_item(curr);
815 }
816
817error:
818 kfree(data_size);
819 kfree(keys);
820out:
821 return ret;
822}
823
824/*
825 * This helper can just do simple insertion that needn't extend item for new
826 * data, such as directory name index insertion, inode insertion.
827 */
828static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
829 struct btrfs_root *root,
830 struct btrfs_path *path,
831 struct btrfs_delayed_item *delayed_item)
832{
833 struct extent_buffer *leaf;
Miao Xie16cdcec2011-04-22 18:12:22 +0800834 char *ptr;
835 int ret;
836
837 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
838 delayed_item->data_len);
839 if (ret < 0 && ret != -EEXIST)
840 return ret;
841
842 leaf = path->nodes[0];
843
Miao Xie16cdcec2011-04-22 18:12:22 +0800844 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
845
846 write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
847 delayed_item->data_len);
848 btrfs_mark_buffer_dirty(leaf);
849
850 btrfs_delayed_item_release_metadata(root, delayed_item);
851 return 0;
852}
853
854/*
855 * we insert an item first, then if there are some continuous items, we try
856 * to insert those items into the same leaf.
857 */
858static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
859 struct btrfs_path *path,
860 struct btrfs_root *root,
861 struct btrfs_delayed_node *node)
862{
863 struct btrfs_delayed_item *curr, *prev;
864 int ret = 0;
865
866do_again:
867 mutex_lock(&node->mutex);
868 curr = __btrfs_first_delayed_insertion_item(node);
869 if (!curr)
870 goto insert_end;
871
872 ret = btrfs_insert_delayed_item(trans, root, path, curr);
873 if (ret < 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400874 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800875 goto insert_end;
876 }
877
878 prev = curr;
879 curr = __btrfs_next_delayed_item(prev);
880 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
881 /* insert the continuous items into the same leaf */
882 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000883 btrfs_batch_insert_items(root, path, curr);
Miao Xie16cdcec2011-04-22 18:12:22 +0800884 }
885 btrfs_release_delayed_item(prev);
886 btrfs_mark_buffer_dirty(path->nodes[0]);
887
Chris Mason945d8962011-05-22 12:33:42 -0400888 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800889 mutex_unlock(&node->mutex);
890 goto do_again;
891
892insert_end:
893 mutex_unlock(&node->mutex);
894 return ret;
895}
896
897static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
898 struct btrfs_root *root,
899 struct btrfs_path *path,
900 struct btrfs_delayed_item *item)
901{
902 struct btrfs_delayed_item *curr, *next;
903 struct extent_buffer *leaf;
904 struct btrfs_key key;
905 struct list_head head;
906 int nitems, i, last_item;
907 int ret = 0;
908
909 BUG_ON(!path->nodes[0]);
910
911 leaf = path->nodes[0];
912
913 i = path->slots[0];
914 last_item = btrfs_header_nritems(leaf) - 1;
915 if (i > last_item)
916 return -ENOENT; /* FIXME: Is errno suitable? */
917
918 next = item;
919 INIT_LIST_HEAD(&head);
920 btrfs_item_key_to_cpu(leaf, &key, i);
921 nitems = 0;
922 /*
923 * count the number of the dir index items that we can delete in batch
924 */
925 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
926 list_add_tail(&next->tree_list, &head);
927 nitems++;
928
929 curr = next;
930 next = __btrfs_next_delayed_item(curr);
931 if (!next)
932 break;
933
934 if (!btrfs_is_continuous_delayed_item(curr, next))
935 break;
936
937 i++;
938 if (i > last_item)
939 break;
940 btrfs_item_key_to_cpu(leaf, &key, i);
941 }
942
943 if (!nitems)
944 return 0;
945
946 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
947 if (ret)
948 goto out;
949
950 list_for_each_entry_safe(curr, next, &head, tree_list) {
951 btrfs_delayed_item_release_metadata(root, curr);
952 list_del(&curr->tree_list);
953 btrfs_release_delayed_item(curr);
954 }
955
956out:
957 return ret;
958}
959
960static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
961 struct btrfs_path *path,
962 struct btrfs_root *root,
963 struct btrfs_delayed_node *node)
964{
965 struct btrfs_delayed_item *curr, *prev;
966 int ret = 0;
967
968do_again:
969 mutex_lock(&node->mutex);
970 curr = __btrfs_first_delayed_deletion_item(node);
971 if (!curr)
972 goto delete_fail;
973
974 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
975 if (ret < 0)
976 goto delete_fail;
977 else if (ret > 0) {
978 /*
979 * can't find the item which the node points to, so this node
980 * is invalid, just drop it.
981 */
982 prev = curr;
983 curr = __btrfs_next_delayed_item(prev);
984 btrfs_release_delayed_item(prev);
985 ret = 0;
Chris Mason945d8962011-05-22 12:33:42 -0400986 btrfs_release_path(path);
Fengguang Wu62095262012-08-04 01:45:02 -0600987 if (curr) {
988 mutex_unlock(&node->mutex);
Miao Xie16cdcec2011-04-22 18:12:22 +0800989 goto do_again;
Fengguang Wu62095262012-08-04 01:45:02 -0600990 } else
Miao Xie16cdcec2011-04-22 18:12:22 +0800991 goto delete_fail;
992 }
993
994 btrfs_batch_delete_items(trans, root, path, curr);
Chris Mason945d8962011-05-22 12:33:42 -0400995 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800996 mutex_unlock(&node->mutex);
997 goto do_again;
998
999delete_fail:
Chris Mason945d8962011-05-22 12:33:42 -04001000 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +08001001 mutex_unlock(&node->mutex);
1002 return ret;
1003}
1004
1005static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
1006{
1007 struct btrfs_delayed_root *delayed_root;
1008
Miao Xie7cf35d92013-12-26 13:07:05 +08001009 if (delayed_node &&
1010 test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001011 BUG_ON(!delayed_node->root);
Miao Xie7cf35d92013-12-26 13:07:05 +08001012 clear_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +08001013 delayed_node->count--;
1014
1015 delayed_root = delayed_node->root->fs_info->delayed_root;
Chris Masonde3cb942013-03-04 17:13:31 -05001016 finish_one_item(delayed_root);
Miao Xie16cdcec2011-04-22 18:12:22 +08001017 }
1018}
1019
Miao Xie67de1172013-12-26 13:07:06 +08001020static void btrfs_release_delayed_iref(struct btrfs_delayed_node *delayed_node)
1021{
1022 struct btrfs_delayed_root *delayed_root;
1023
1024 ASSERT(delayed_node->root);
1025 clear_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1026 delayed_node->count--;
1027
1028 delayed_root = delayed_node->root->fs_info->delayed_root;
1029 finish_one_item(delayed_root);
1030}
1031
Miao Xie0e8c36a2012-12-19 06:59:51 +00001032static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1033 struct btrfs_root *root,
1034 struct btrfs_path *path,
1035 struct btrfs_delayed_node *node)
Miao Xie16cdcec2011-04-22 18:12:22 +08001036{
1037 struct btrfs_key key;
1038 struct btrfs_inode_item *inode_item;
1039 struct extent_buffer *leaf;
Miao Xie67de1172013-12-26 13:07:06 +08001040 int mod;
Miao Xie16cdcec2011-04-22 18:12:22 +08001041 int ret;
1042
Miao Xie16cdcec2011-04-22 18:12:22 +08001043 key.objectid = node->inode_id;
David Sterba962a2982014-06-04 18:41:45 +02001044 key.type = BTRFS_INODE_ITEM_KEY;
Miao Xie16cdcec2011-04-22 18:12:22 +08001045 key.offset = 0;
Miao Xie0e8c36a2012-12-19 06:59:51 +00001046
Miao Xie67de1172013-12-26 13:07:06 +08001047 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1048 mod = -1;
1049 else
1050 mod = 1;
1051
1052 ret = btrfs_lookup_inode(trans, root, path, &key, mod);
Miao Xie16cdcec2011-04-22 18:12:22 +08001053 if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -04001054 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +08001055 return -ENOENT;
1056 } else if (ret < 0) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001057 return ret;
1058 }
1059
Miao Xie16cdcec2011-04-22 18:12:22 +08001060 leaf = path->nodes[0];
1061 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1062 struct btrfs_inode_item);
1063 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1064 sizeof(struct btrfs_inode_item));
1065 btrfs_mark_buffer_dirty(leaf);
Miao Xie16cdcec2011-04-22 18:12:22 +08001066
Miao Xie67de1172013-12-26 13:07:06 +08001067 if (!test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &node->flags))
1068 goto no_iref;
1069
1070 path->slots[0]++;
1071 if (path->slots[0] >= btrfs_header_nritems(leaf))
1072 goto search;
1073again:
1074 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1075 if (key.objectid != node->inode_id)
1076 goto out;
1077
1078 if (key.type != BTRFS_INODE_REF_KEY &&
1079 key.type != BTRFS_INODE_EXTREF_KEY)
1080 goto out;
1081
1082 /*
1083 * Delayed iref deletion is for the inode who has only one link,
1084 * so there is only one iref. The case that several irefs are
1085 * in the same item doesn't exist.
1086 */
1087 btrfs_del_item(trans, root, path);
1088out:
1089 btrfs_release_delayed_iref(node);
1090no_iref:
1091 btrfs_release_path(path);
1092err_out:
Miao Xie16cdcec2011-04-22 18:12:22 +08001093 btrfs_delayed_inode_release_metadata(root, node);
1094 btrfs_release_delayed_inode(node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001095
Miao Xie67de1172013-12-26 13:07:06 +08001096 return ret;
1097
1098search:
1099 btrfs_release_path(path);
1100
David Sterba962a2982014-06-04 18:41:45 +02001101 key.type = BTRFS_INODE_EXTREF_KEY;
Miao Xie67de1172013-12-26 13:07:06 +08001102 key.offset = -1;
1103 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1104 if (ret < 0)
1105 goto err_out;
1106 ASSERT(ret);
1107
1108 ret = 0;
1109 leaf = path->nodes[0];
1110 path->slots[0]--;
1111 goto again;
Miao Xie16cdcec2011-04-22 18:12:22 +08001112}
1113
Miao Xie0e8c36a2012-12-19 06:59:51 +00001114static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1115 struct btrfs_root *root,
1116 struct btrfs_path *path,
1117 struct btrfs_delayed_node *node)
1118{
1119 int ret;
1120
1121 mutex_lock(&node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001122 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &node->flags)) {
Miao Xie0e8c36a2012-12-19 06:59:51 +00001123 mutex_unlock(&node->mutex);
1124 return 0;
1125 }
1126
1127 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1128 mutex_unlock(&node->mutex);
1129 return ret;
1130}
1131
Miao Xie4ea41ce2012-12-19 06:59:03 +00001132static inline int
1133__btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1134 struct btrfs_path *path,
1135 struct btrfs_delayed_node *node)
1136{
1137 int ret;
1138
1139 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1140 if (ret)
1141 return ret;
1142
1143 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1144 if (ret)
1145 return ret;
1146
1147 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1148 return ret;
1149}
1150
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001151/*
1152 * Called when committing the transaction.
1153 * Returns 0 on success.
1154 * Returns < 0 on error and returns with an aborted transaction with any
1155 * outstanding delayed items cleaned up.
1156 */
Josef Bacik96c3f432012-06-21 14:05:49 -04001157static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root, int nr)
Miao Xie16cdcec2011-04-22 18:12:22 +08001159{
1160 struct btrfs_delayed_root *delayed_root;
1161 struct btrfs_delayed_node *curr_node, *prev_node;
1162 struct btrfs_path *path;
Miao Xie19fd2942011-06-15 10:47:30 +00001163 struct btrfs_block_rsv *block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001164 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04001165 bool count = (nr > 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001166
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001167 if (trans->aborted)
1168 return -EIO;
1169
Miao Xie16cdcec2011-04-22 18:12:22 +08001170 path = btrfs_alloc_path();
1171 if (!path)
1172 return -ENOMEM;
1173 path->leave_spinning = 1;
1174
Miao Xie19fd2942011-06-15 10:47:30 +00001175 block_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -04001176 trans->block_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie19fd2942011-06-15 10:47:30 +00001177
Miao Xie16cdcec2011-04-22 18:12:22 +08001178 delayed_root = btrfs_get_delayed_root(root);
1179
1180 curr_node = btrfs_first_delayed_node(delayed_root);
Josef Bacik96c3f432012-06-21 14:05:49 -04001181 while (curr_node && (!count || (count && nr--))) {
Miao Xie4ea41ce2012-12-19 06:59:03 +00001182 ret = __btrfs_commit_inode_delayed_items(trans, path,
1183 curr_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001184 if (ret) {
1185 btrfs_release_delayed_node(curr_node);
Josef Bacik96c3f432012-06-21 14:05:49 -04001186 curr_node = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001187 btrfs_abort_transaction(trans, root, ret);
Miao Xie16cdcec2011-04-22 18:12:22 +08001188 break;
1189 }
1190
1191 prev_node = curr_node;
1192 curr_node = btrfs_next_delayed_node(curr_node);
1193 btrfs_release_delayed_node(prev_node);
1194 }
1195
Josef Bacik96c3f432012-06-21 14:05:49 -04001196 if (curr_node)
1197 btrfs_release_delayed_node(curr_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001198 btrfs_free_path(path);
Miao Xie19fd2942011-06-15 10:47:30 +00001199 trans->block_rsv = block_rsv;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001200
Miao Xie16cdcec2011-04-22 18:12:22 +08001201 return ret;
1202}
1203
Josef Bacik96c3f432012-06-21 14:05:49 -04001204int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1205 struct btrfs_root *root)
1206{
1207 return __btrfs_run_delayed_items(trans, root, -1);
1208}
1209
1210int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
1211 struct btrfs_root *root, int nr)
1212{
1213 return __btrfs_run_delayed_items(trans, root, nr);
1214}
1215
Miao Xie16cdcec2011-04-22 18:12:22 +08001216int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1217 struct inode *inode)
1218{
1219 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001220 struct btrfs_path *path;
1221 struct btrfs_block_rsv *block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001222 int ret;
1223
1224 if (!delayed_node)
1225 return 0;
1226
1227 mutex_lock(&delayed_node->mutex);
1228 if (!delayed_node->count) {
1229 mutex_unlock(&delayed_node->mutex);
1230 btrfs_release_delayed_node(delayed_node);
1231 return 0;
1232 }
1233 mutex_unlock(&delayed_node->mutex);
1234
Miao Xie4ea41ce2012-12-19 06:59:03 +00001235 path = btrfs_alloc_path();
Filipe David Borba Manana3c77bd92013-10-12 20:32:59 +01001236 if (!path) {
1237 btrfs_release_delayed_node(delayed_node);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001238 return -ENOMEM;
Filipe David Borba Manana3c77bd92013-10-12 20:32:59 +01001239 }
Miao Xie4ea41ce2012-12-19 06:59:03 +00001240 path->leave_spinning = 1;
1241
1242 block_rsv = trans->block_rsv;
1243 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1244
1245 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1246
Miao Xie16cdcec2011-04-22 18:12:22 +08001247 btrfs_release_delayed_node(delayed_node);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001248 btrfs_free_path(path);
1249 trans->block_rsv = block_rsv;
1250
Miao Xie16cdcec2011-04-22 18:12:22 +08001251 return ret;
1252}
1253
Miao Xie0e8c36a2012-12-19 06:59:51 +00001254int btrfs_commit_inode_delayed_inode(struct inode *inode)
1255{
1256 struct btrfs_trans_handle *trans;
1257 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1258 struct btrfs_path *path;
1259 struct btrfs_block_rsv *block_rsv;
1260 int ret;
1261
1262 if (!delayed_node)
1263 return 0;
1264
1265 mutex_lock(&delayed_node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001266 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie0e8c36a2012-12-19 06:59:51 +00001267 mutex_unlock(&delayed_node->mutex);
1268 btrfs_release_delayed_node(delayed_node);
1269 return 0;
1270 }
1271 mutex_unlock(&delayed_node->mutex);
1272
1273 trans = btrfs_join_transaction(delayed_node->root);
1274 if (IS_ERR(trans)) {
1275 ret = PTR_ERR(trans);
1276 goto out;
1277 }
1278
1279 path = btrfs_alloc_path();
1280 if (!path) {
1281 ret = -ENOMEM;
1282 goto trans_out;
1283 }
1284 path->leave_spinning = 1;
1285
1286 block_rsv = trans->block_rsv;
1287 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1288
1289 mutex_lock(&delayed_node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001290 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
Miao Xie0e8c36a2012-12-19 06:59:51 +00001291 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1292 path, delayed_node);
1293 else
1294 ret = 0;
1295 mutex_unlock(&delayed_node->mutex);
1296
1297 btrfs_free_path(path);
1298 trans->block_rsv = block_rsv;
1299trans_out:
1300 btrfs_end_transaction(trans, delayed_node->root);
1301 btrfs_btree_balance_dirty(delayed_node->root);
1302out:
1303 btrfs_release_delayed_node(delayed_node);
1304
1305 return ret;
1306}
1307
Miao Xie16cdcec2011-04-22 18:12:22 +08001308void btrfs_remove_delayed_node(struct inode *inode)
1309{
1310 struct btrfs_delayed_node *delayed_node;
1311
1312 delayed_node = ACCESS_ONCE(BTRFS_I(inode)->delayed_node);
1313 if (!delayed_node)
1314 return;
1315
1316 BTRFS_I(inode)->delayed_node = NULL;
1317 btrfs_release_delayed_node(delayed_node);
1318}
1319
Chris Masonde3cb942013-03-04 17:13:31 -05001320struct btrfs_async_delayed_work {
1321 struct btrfs_delayed_root *delayed_root;
1322 int nr;
Qu Wenruod458b052014-02-28 10:46:19 +08001323 struct btrfs_work work;
Miao Xie16cdcec2011-04-22 18:12:22 +08001324};
1325
Qu Wenruod458b052014-02-28 10:46:19 +08001326static void btrfs_async_run_delayed_root(struct btrfs_work *work)
Miao Xie16cdcec2011-04-22 18:12:22 +08001327{
Chris Masonde3cb942013-03-04 17:13:31 -05001328 struct btrfs_async_delayed_work *async_work;
1329 struct btrfs_delayed_root *delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001330 struct btrfs_trans_handle *trans;
1331 struct btrfs_path *path;
1332 struct btrfs_delayed_node *delayed_node = NULL;
1333 struct btrfs_root *root;
Miao Xie19fd2942011-06-15 10:47:30 +00001334 struct btrfs_block_rsv *block_rsv;
Chris Masonde3cb942013-03-04 17:13:31 -05001335 int total_done = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001336
Chris Masonde3cb942013-03-04 17:13:31 -05001337 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1338 delayed_root = async_work->delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001339
1340 path = btrfs_alloc_path();
1341 if (!path)
1342 goto out;
Miao Xie16cdcec2011-04-22 18:12:22 +08001343
Chris Masonde3cb942013-03-04 17:13:31 -05001344again:
1345 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2)
1346 goto free_path;
1347
1348 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1349 if (!delayed_node)
1350 goto free_path;
1351
1352 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08001353 root = delayed_node->root;
1354
Chris Masonff5714c2011-05-28 07:00:39 -04001355 trans = btrfs_join_transaction(root);
Miao Xie16cdcec2011-04-22 18:12:22 +08001356 if (IS_ERR(trans))
Chris Masonde3cb942013-03-04 17:13:31 -05001357 goto release_path;
Miao Xie16cdcec2011-04-22 18:12:22 +08001358
Miao Xie19fd2942011-06-15 10:47:30 +00001359 block_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -04001360 trans->block_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie19fd2942011-06-15 10:47:30 +00001361
Miao Xie4ea41ce2012-12-19 06:59:03 +00001362 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001363
Miao Xie19fd2942011-06-15 10:47:30 +00001364 trans->block_rsv = block_rsv;
Miao Xiea56dbd82013-12-26 13:07:04 +08001365 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00001366 btrfs_btree_balance_dirty_nodelay(root);
Chris Masonde3cb942013-03-04 17:13:31 -05001367
1368release_path:
1369 btrfs_release_path(path);
1370 total_done++;
1371
1372 btrfs_release_prepared_delayed_node(delayed_node);
1373 if (async_work->nr == 0 || total_done < async_work->nr)
1374 goto again;
1375
Miao Xie16cdcec2011-04-22 18:12:22 +08001376free_path:
1377 btrfs_free_path(path);
1378out:
Chris Masonde3cb942013-03-04 17:13:31 -05001379 wake_up(&delayed_root->wait);
1380 kfree(async_work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001381}
1382
Miao Xie16cdcec2011-04-22 18:12:22 +08001383
Chris Masonde3cb942013-03-04 17:13:31 -05001384static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
Daniel Dresslera585e942014-11-17 22:05:02 +09001385 struct btrfs_fs_info *fs_info, int nr)
Chris Masonde3cb942013-03-04 17:13:31 -05001386{
1387 struct btrfs_async_delayed_work *async_work;
1388
1389 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
Miao Xie16cdcec2011-04-22 18:12:22 +08001390 return 0;
1391
Chris Masonde3cb942013-03-04 17:13:31 -05001392 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1393 if (!async_work)
Miao Xie16cdcec2011-04-22 18:12:22 +08001394 return -ENOMEM;
Miao Xie16cdcec2011-04-22 18:12:22 +08001395
Chris Masonde3cb942013-03-04 17:13:31 -05001396 async_work->delayed_root = delayed_root;
Liu Bo9e0af232014-08-15 23:36:53 +08001397 btrfs_init_work(&async_work->work, btrfs_delayed_meta_helper,
1398 btrfs_async_run_delayed_root, NULL, NULL);
Chris Masonde3cb942013-03-04 17:13:31 -05001399 async_work->nr = nr;
Miao Xie16cdcec2011-04-22 18:12:22 +08001400
Daniel Dresslera585e942014-11-17 22:05:02 +09001401 btrfs_queue_work(fs_info->delayed_workers, &async_work->work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001402 return 0;
1403}
1404
Chris Masone9993762011-06-17 16:14:09 -04001405void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
1406{
1407 struct btrfs_delayed_root *delayed_root;
1408 delayed_root = btrfs_get_delayed_root(root);
1409 WARN_ON(btrfs_first_delayed_node(delayed_root));
1410}
1411
Miao Xie03538082013-12-26 13:07:03 +08001412static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
Chris Masonde3cb942013-03-04 17:13:31 -05001413{
1414 int val = atomic_read(&delayed_root->items_seq);
1415
Miao Xie03538082013-12-26 13:07:03 +08001416 if (val < seq || val >= seq + BTRFS_DELAYED_BATCH)
Chris Masonde3cb942013-03-04 17:13:31 -05001417 return 1;
Miao Xie03538082013-12-26 13:07:03 +08001418
1419 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1420 return 1;
1421
Chris Masonde3cb942013-03-04 17:13:31 -05001422 return 0;
1423}
1424
Miao Xie16cdcec2011-04-22 18:12:22 +08001425void btrfs_balance_delayed_items(struct btrfs_root *root)
1426{
1427 struct btrfs_delayed_root *delayed_root;
Daniel Dresslera585e942014-11-17 22:05:02 +09001428 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xie16cdcec2011-04-22 18:12:22 +08001429
1430 delayed_root = btrfs_get_delayed_root(root);
1431
1432 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1433 return;
1434
1435 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
Miao Xie03538082013-12-26 13:07:03 +08001436 int seq;
Miao Xie16cdcec2011-04-22 18:12:22 +08001437 int ret;
Miao Xie03538082013-12-26 13:07:03 +08001438
1439 seq = atomic_read(&delayed_root->items_seq);
Chris Masonde3cb942013-03-04 17:13:31 -05001440
Daniel Dresslera585e942014-11-17 22:05:02 +09001441 ret = btrfs_wq_run_delayed_node(delayed_root, fs_info, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001442 if (ret)
1443 return;
1444
Miao Xie03538082013-12-26 13:07:03 +08001445 wait_event_interruptible(delayed_root->wait,
1446 could_end_wait(delayed_root, seq));
Miao Xie4dd466d2013-12-26 13:07:02 +08001447 return;
Miao Xie16cdcec2011-04-22 18:12:22 +08001448 }
1449
Daniel Dresslera585e942014-11-17 22:05:02 +09001450 btrfs_wq_run_delayed_node(delayed_root, fs_info, BTRFS_DELAYED_BATCH);
Miao Xie16cdcec2011-04-22 18:12:22 +08001451}
1452
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001453/* Will return 0 or -ENOMEM */
Miao Xie16cdcec2011-04-22 18:12:22 +08001454int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1455 struct btrfs_root *root, const char *name,
1456 int name_len, struct inode *dir,
1457 struct btrfs_disk_key *disk_key, u8 type,
1458 u64 index)
1459{
1460 struct btrfs_delayed_node *delayed_node;
1461 struct btrfs_delayed_item *delayed_item;
1462 struct btrfs_dir_item *dir_item;
1463 int ret;
1464
1465 delayed_node = btrfs_get_or_create_delayed_node(dir);
1466 if (IS_ERR(delayed_node))
1467 return PTR_ERR(delayed_node);
1468
1469 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1470 if (!delayed_item) {
1471 ret = -ENOMEM;
1472 goto release_node;
1473 }
1474
Chris Mason0d0ca302011-05-22 07:11:22 -04001475 delayed_item->key.objectid = btrfs_ino(dir);
David Sterba962a2982014-06-04 18:41:45 +02001476 delayed_item->key.type = BTRFS_DIR_INDEX_KEY;
Miao Xie16cdcec2011-04-22 18:12:22 +08001477 delayed_item->key.offset = index;
1478
1479 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1480 dir_item->location = *disk_key;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001481 btrfs_set_stack_dir_transid(dir_item, trans->transid);
1482 btrfs_set_stack_dir_data_len(dir_item, 0);
1483 btrfs_set_stack_dir_name_len(dir_item, name_len);
1484 btrfs_set_stack_dir_type(dir_item, type);
Miao Xie16cdcec2011-04-22 18:12:22 +08001485 memcpy((char *)(dir_item + 1), name, name_len);
1486
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05001487 ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item);
1488 /*
1489 * we have reserved enough space when we start a new transaction,
1490 * so reserving metadata failure is impossible
1491 */
1492 BUG_ON(ret);
1493
1494
Miao Xie16cdcec2011-04-22 18:12:22 +08001495 mutex_lock(&delayed_node->mutex);
1496 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1497 if (unlikely(ret)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001498 btrfs_err(root->fs_info, "err add delayed dir index item(name: %.*s) "
Filipe David Borba Mananabdab49d2013-08-20 17:51:48 +01001499 "into the insertion tree of the delayed node"
Frank Holtonefe120a2013-12-20 11:37:06 -05001500 "(root id: %llu, inode id: %llu, errno: %d)",
Filipe David Borba Mananabdab49d2013-08-20 17:51:48 +01001501 name_len, name, delayed_node->root->objectid,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001502 delayed_node->inode_id, ret);
Miao Xie16cdcec2011-04-22 18:12:22 +08001503 BUG();
1504 }
1505 mutex_unlock(&delayed_node->mutex);
1506
1507release_node:
1508 btrfs_release_delayed_node(delayed_node);
1509 return ret;
1510}
1511
1512static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root,
1513 struct btrfs_delayed_node *node,
1514 struct btrfs_key *key)
1515{
1516 struct btrfs_delayed_item *item;
1517
1518 mutex_lock(&node->mutex);
1519 item = __btrfs_lookup_delayed_insertion_item(node, key);
1520 if (!item) {
1521 mutex_unlock(&node->mutex);
1522 return 1;
1523 }
1524
1525 btrfs_delayed_item_release_metadata(root, item);
1526 btrfs_release_delayed_item(item);
1527 mutex_unlock(&node->mutex);
1528 return 0;
1529}
1530
1531int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1532 struct btrfs_root *root, struct inode *dir,
1533 u64 index)
1534{
1535 struct btrfs_delayed_node *node;
1536 struct btrfs_delayed_item *item;
1537 struct btrfs_key item_key;
1538 int ret;
1539
1540 node = btrfs_get_or_create_delayed_node(dir);
1541 if (IS_ERR(node))
1542 return PTR_ERR(node);
1543
Chris Mason0d0ca302011-05-22 07:11:22 -04001544 item_key.objectid = btrfs_ino(dir);
David Sterba962a2982014-06-04 18:41:45 +02001545 item_key.type = BTRFS_DIR_INDEX_KEY;
Miao Xie16cdcec2011-04-22 18:12:22 +08001546 item_key.offset = index;
1547
1548 ret = btrfs_delete_delayed_insertion_item(root, node, &item_key);
1549 if (!ret)
1550 goto end;
1551
1552 item = btrfs_alloc_delayed_item(0);
1553 if (!item) {
1554 ret = -ENOMEM;
1555 goto end;
1556 }
1557
1558 item->key = item_key;
1559
1560 ret = btrfs_delayed_item_reserve_metadata(trans, root, item);
1561 /*
1562 * we have reserved enough space when we start a new transaction,
1563 * so reserving metadata failure is impossible.
1564 */
1565 BUG_ON(ret);
1566
1567 mutex_lock(&node->mutex);
1568 ret = __btrfs_add_delayed_deletion_item(node, item);
1569 if (unlikely(ret)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001570 btrfs_err(root->fs_info, "err add delayed dir index item(index: %llu) "
Miao Xie16cdcec2011-04-22 18:12:22 +08001571 "into the deletion tree of the delayed node"
Frank Holtonefe120a2013-12-20 11:37:06 -05001572 "(root id: %llu, inode id: %llu, errno: %d)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001573 index, node->root->objectid, node->inode_id,
Miao Xie16cdcec2011-04-22 18:12:22 +08001574 ret);
1575 BUG();
1576 }
1577 mutex_unlock(&node->mutex);
1578end:
1579 btrfs_release_delayed_node(node);
1580 return ret;
1581}
1582
1583int btrfs_inode_delayed_dir_index_count(struct inode *inode)
1584{
Miao Xie2f7e33d2011-06-23 07:27:13 +00001585 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08001586
1587 if (!delayed_node)
1588 return -ENOENT;
1589
1590 /*
1591 * Since we have held i_mutex of this directory, it is impossible that
1592 * a new directory index is added into the delayed node and index_cnt
1593 * is updated now. So we needn't lock the delayed node.
1594 */
Miao Xie2f7e33d2011-06-23 07:27:13 +00001595 if (!delayed_node->index_cnt) {
1596 btrfs_release_delayed_node(delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001597 return -EINVAL;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001598 }
Miao Xie16cdcec2011-04-22 18:12:22 +08001599
1600 BTRFS_I(inode)->index_cnt = delayed_node->index_cnt;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001601 btrfs_release_delayed_node(delayed_node);
1602 return 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001603}
1604
1605void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list,
1606 struct list_head *del_list)
1607{
1608 struct btrfs_delayed_node *delayed_node;
1609 struct btrfs_delayed_item *item;
1610
1611 delayed_node = btrfs_get_delayed_node(inode);
1612 if (!delayed_node)
1613 return;
1614
1615 mutex_lock(&delayed_node->mutex);
1616 item = __btrfs_first_delayed_insertion_item(delayed_node);
1617 while (item) {
1618 atomic_inc(&item->refs);
1619 list_add_tail(&item->readdir_list, ins_list);
1620 item = __btrfs_next_delayed_item(item);
1621 }
1622
1623 item = __btrfs_first_delayed_deletion_item(delayed_node);
1624 while (item) {
1625 atomic_inc(&item->refs);
1626 list_add_tail(&item->readdir_list, del_list);
1627 item = __btrfs_next_delayed_item(item);
1628 }
1629 mutex_unlock(&delayed_node->mutex);
1630 /*
1631 * This delayed node is still cached in the btrfs inode, so refs
1632 * must be > 1 now, and we needn't check it is going to be freed
1633 * or not.
1634 *
1635 * Besides that, this function is used to read dir, we do not
1636 * insert/delete delayed items in this period. So we also needn't
1637 * requeue or dequeue this delayed node.
1638 */
1639 atomic_dec(&delayed_node->refs);
1640}
1641
1642void btrfs_put_delayed_items(struct list_head *ins_list,
1643 struct list_head *del_list)
1644{
1645 struct btrfs_delayed_item *curr, *next;
1646
1647 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1648 list_del(&curr->readdir_list);
1649 if (atomic_dec_and_test(&curr->refs))
1650 kfree(curr);
1651 }
1652
1653 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1654 list_del(&curr->readdir_list);
1655 if (atomic_dec_and_test(&curr->refs))
1656 kfree(curr);
1657 }
1658}
1659
1660int btrfs_should_delete_dir_index(struct list_head *del_list,
1661 u64 index)
1662{
1663 struct btrfs_delayed_item *curr, *next;
1664 int ret;
1665
1666 if (list_empty(del_list))
1667 return 0;
1668
1669 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1670 if (curr->key.offset > index)
1671 break;
1672
1673 list_del(&curr->readdir_list);
1674 ret = (curr->key.offset == index);
1675
1676 if (atomic_dec_and_test(&curr->refs))
1677 kfree(curr);
1678
1679 if (ret)
1680 return 1;
1681 else
1682 continue;
1683 }
1684 return 0;
1685}
1686
1687/*
1688 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1689 *
1690 */
Al Viro9cdda8d2013-05-22 16:48:09 -04001691int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
Miao Xie16cdcec2011-04-22 18:12:22 +08001692 struct list_head *ins_list)
1693{
1694 struct btrfs_dir_item *di;
1695 struct btrfs_delayed_item *curr, *next;
1696 struct btrfs_key location;
1697 char *name;
1698 int name_len;
1699 int over = 0;
1700 unsigned char d_type;
1701
1702 if (list_empty(ins_list))
1703 return 0;
1704
1705 /*
1706 * Changing the data of the delayed item is impossible. So
1707 * we needn't lock them. And we have held i_mutex of the
1708 * directory, nobody can delete any directory indexes now.
1709 */
1710 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1711 list_del(&curr->readdir_list);
1712
Al Viro9cdda8d2013-05-22 16:48:09 -04001713 if (curr->key.offset < ctx->pos) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001714 if (atomic_dec_and_test(&curr->refs))
1715 kfree(curr);
1716 continue;
1717 }
1718
Al Viro9cdda8d2013-05-22 16:48:09 -04001719 ctx->pos = curr->key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08001720
1721 di = (struct btrfs_dir_item *)curr->data;
1722 name = (char *)(di + 1);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001723 name_len = btrfs_stack_dir_name_len(di);
Miao Xie16cdcec2011-04-22 18:12:22 +08001724
1725 d_type = btrfs_filetype_table[di->type];
1726 btrfs_disk_key_to_cpu(&location, &di->location);
1727
Al Viro9cdda8d2013-05-22 16:48:09 -04001728 over = !dir_emit(ctx, name, name_len,
Miao Xie16cdcec2011-04-22 18:12:22 +08001729 location.objectid, d_type);
1730
1731 if (atomic_dec_and_test(&curr->refs))
1732 kfree(curr);
1733
1734 if (over)
1735 return 1;
1736 }
1737 return 0;
1738}
1739
Miao Xie16cdcec2011-04-22 18:12:22 +08001740static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1741 struct btrfs_inode_item *inode_item,
1742 struct inode *inode)
1743{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08001744 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1745 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08001746 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);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04001752 btrfs_set_stack_inode_sequence(inode_item, inode->i_version);
Miao Xie16cdcec2011-04-22 18:12:22 +08001753 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);
Chris Masonff5714c2011-05-28 07:00:39 -04001756 btrfs_set_stack_inode_block_group(inode_item, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001757
David Sterbaa937b972014-12-12 17:39:12 +01001758 btrfs_set_stack_timespec_sec(&inode_item->atime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001759 inode->i_atime.tv_sec);
David Sterbaa937b972014-12-12 17:39:12 +01001760 btrfs_set_stack_timespec_nsec(&inode_item->atime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001761 inode->i_atime.tv_nsec);
1762
David Sterbaa937b972014-12-12 17:39:12 +01001763 btrfs_set_stack_timespec_sec(&inode_item->mtime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001764 inode->i_mtime.tv_sec);
David Sterbaa937b972014-12-12 17:39:12 +01001765 btrfs_set_stack_timespec_nsec(&inode_item->mtime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001766 inode->i_mtime.tv_nsec);
1767
David Sterbaa937b972014-12-12 17:39:12 +01001768 btrfs_set_stack_timespec_sec(&inode_item->ctime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001769 inode->i_ctime.tv_sec);
David Sterbaa937b972014-12-12 17:39:12 +01001770 btrfs_set_stack_timespec_nsec(&inode_item->ctime,
Miao Xie16cdcec2011-04-22 18:12:22 +08001771 inode->i_ctime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05301772
1773 btrfs_set_stack_timespec_sec(&inode_item->otime,
1774 BTRFS_I(inode)->i_otime.tv_sec);
1775 btrfs_set_stack_timespec_nsec(&inode_item->otime,
1776 BTRFS_I(inode)->i_otime.tv_nsec);
Miao Xie16cdcec2011-04-22 18:12:22 +08001777}
1778
Miao Xie2f7e33d2011-06-23 07:27:13 +00001779int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1780{
1781 struct btrfs_delayed_node *delayed_node;
1782 struct btrfs_inode_item *inode_item;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001783
1784 delayed_node = btrfs_get_delayed_node(inode);
1785 if (!delayed_node)
1786 return -ENOENT;
1787
1788 mutex_lock(&delayed_node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001789 if (!test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie2f7e33d2011-06-23 07:27:13 +00001790 mutex_unlock(&delayed_node->mutex);
1791 btrfs_release_delayed_node(delayed_node);
1792 return -ENOENT;
1793 }
1794
1795 inode_item = &delayed_node->inode_item;
1796
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08001797 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1798 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001799 btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
1800 inode->i_mode = btrfs_stack_inode_mode(inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001801 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001802 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1803 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
Yang Dongsheng6e17d302015-04-09 12:08:43 +08001804 BTRFS_I(inode)->last_trans = btrfs_stack_inode_transid(inode_item);
1805
Josef Bacik0c4d2d92012-04-05 15:03:02 -04001806 inode->i_version = btrfs_stack_inode_sequence(inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00001807 inode->i_rdev = 0;
1808 *rdev = btrfs_stack_inode_rdev(inode_item);
1809 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1810
David Sterbaa937b972014-12-12 17:39:12 +01001811 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(&inode_item->atime);
1812 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->atime);
Miao Xie2f7e33d2011-06-23 07:27:13 +00001813
David Sterbaa937b972014-12-12 17:39:12 +01001814 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(&inode_item->mtime);
1815 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->mtime);
Miao Xie2f7e33d2011-06-23 07:27:13 +00001816
David Sterbaa937b972014-12-12 17:39:12 +01001817 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(&inode_item->ctime);
1818 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(&inode_item->ctime);
Miao Xie2f7e33d2011-06-23 07:27:13 +00001819
chandan r9cc97d62012-07-04 12:48:07 +05301820 BTRFS_I(inode)->i_otime.tv_sec =
1821 btrfs_stack_timespec_sec(&inode_item->otime);
1822 BTRFS_I(inode)->i_otime.tv_nsec =
1823 btrfs_stack_timespec_nsec(&inode_item->otime);
1824
Miao Xie2f7e33d2011-06-23 07:27:13 +00001825 inode->i_generation = BTRFS_I(inode)->generation;
1826 BTRFS_I(inode)->index_cnt = (u64)-1;
1827
1828 mutex_unlock(&delayed_node->mutex);
1829 btrfs_release_delayed_node(delayed_node);
1830 return 0;
1831}
1832
Miao Xie16cdcec2011-04-22 18:12:22 +08001833int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1834 struct btrfs_root *root, struct inode *inode)
1835{
1836 struct btrfs_delayed_node *delayed_node;
David Sterbaaa0467d2011-06-03 16:29:08 +02001837 int ret = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001838
1839 delayed_node = btrfs_get_or_create_delayed_node(inode);
1840 if (IS_ERR(delayed_node))
1841 return PTR_ERR(delayed_node);
1842
1843 mutex_lock(&delayed_node->mutex);
Miao Xie7cf35d92013-12-26 13:07:05 +08001844 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001845 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1846 goto release_node;
1847 }
1848
Josef Bacik7fd2ae22011-11-08 15:47:34 -05001849 ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode,
1850 delayed_node);
Josef Bacikc06a0e12011-11-04 19:56:02 -04001851 if (ret)
1852 goto release_node;
Miao Xie16cdcec2011-04-22 18:12:22 +08001853
1854 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
Miao Xie7cf35d92013-12-26 13:07:05 +08001855 set_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags);
Miao Xie16cdcec2011-04-22 18:12:22 +08001856 delayed_node->count++;
1857 atomic_inc(&root->fs_info->delayed_root->items);
1858release_node:
1859 mutex_unlock(&delayed_node->mutex);
1860 btrfs_release_delayed_node(delayed_node);
1861 return ret;
1862}
1863
Miao Xie67de1172013-12-26 13:07:06 +08001864int btrfs_delayed_delete_inode_ref(struct inode *inode)
1865{
1866 struct btrfs_delayed_node *delayed_node;
1867
Chris Mason6f896052014-12-31 12:18:29 -05001868 /*
1869 * we don't do delayed inode updates during log recovery because it
1870 * leads to enospc problems. This means we also can't do
1871 * delayed inode refs
1872 */
1873 if (BTRFS_I(inode)->root->fs_info->log_root_recovering)
1874 return -EAGAIN;
1875
Miao Xie67de1172013-12-26 13:07:06 +08001876 delayed_node = btrfs_get_or_create_delayed_node(inode);
1877 if (IS_ERR(delayed_node))
1878 return PTR_ERR(delayed_node);
1879
1880 /*
1881 * We don't reserve space for inode ref deletion is because:
1882 * - We ONLY do async inode ref deletion for the inode who has only
1883 * one link(i_nlink == 1), it means there is only one inode ref.
1884 * And in most case, the inode ref and the inode item are in the
1885 * same leaf, and we will deal with them at the same time.
1886 * Since we are sure we will reserve the space for the inode item,
1887 * it is unnecessary to reserve space for inode ref deletion.
1888 * - If the inode ref and the inode item are not in the same leaf,
1889 * We also needn't worry about enospc problem, because we reserve
1890 * much more space for the inode update than it needs.
1891 * - At the worst, we can steal some space from the global reservation.
1892 * It is very rare.
1893 */
1894 mutex_lock(&delayed_node->mutex);
1895 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1896 goto release_node;
1897
1898 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1899 delayed_node->count++;
1900 atomic_inc(&BTRFS_I(inode)->root->fs_info->delayed_root->items);
1901release_node:
1902 mutex_unlock(&delayed_node->mutex);
1903 btrfs_release_delayed_node(delayed_node);
1904 return 0;
1905}
1906
Miao Xie16cdcec2011-04-22 18:12:22 +08001907static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1908{
1909 struct btrfs_root *root = delayed_node->root;
1910 struct btrfs_delayed_item *curr_item, *prev_item;
1911
1912 mutex_lock(&delayed_node->mutex);
1913 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1914 while (curr_item) {
1915 btrfs_delayed_item_release_metadata(root, curr_item);
1916 prev_item = curr_item;
1917 curr_item = __btrfs_next_delayed_item(prev_item);
1918 btrfs_release_delayed_item(prev_item);
1919 }
1920
1921 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1922 while (curr_item) {
1923 btrfs_delayed_item_release_metadata(root, curr_item);
1924 prev_item = curr_item;
1925 curr_item = __btrfs_next_delayed_item(prev_item);
1926 btrfs_release_delayed_item(prev_item);
1927 }
1928
Miao Xie67de1172013-12-26 13:07:06 +08001929 if (test_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags))
1930 btrfs_release_delayed_iref(delayed_node);
1931
Miao Xie7cf35d92013-12-26 13:07:05 +08001932 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001933 btrfs_delayed_inode_release_metadata(root, delayed_node);
1934 btrfs_release_delayed_inode(delayed_node);
1935 }
1936 mutex_unlock(&delayed_node->mutex);
1937}
1938
1939void btrfs_kill_delayed_inode_items(struct inode *inode)
1940{
1941 struct btrfs_delayed_node *delayed_node;
1942
1943 delayed_node = btrfs_get_delayed_node(inode);
1944 if (!delayed_node)
1945 return;
1946
1947 __btrfs_kill_delayed_node(delayed_node);
1948 btrfs_release_delayed_node(delayed_node);
1949}
1950
1951void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1952{
1953 u64 inode_id = 0;
1954 struct btrfs_delayed_node *delayed_nodes[8];
1955 int i, n;
1956
1957 while (1) {
1958 spin_lock(&root->inode_lock);
1959 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1960 (void **)delayed_nodes, inode_id,
1961 ARRAY_SIZE(delayed_nodes));
1962 if (!n) {
1963 spin_unlock(&root->inode_lock);
1964 break;
1965 }
1966
1967 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1968
1969 for (i = 0; i < n; i++)
1970 atomic_inc(&delayed_nodes[i]->refs);
1971 spin_unlock(&root->inode_lock);
1972
1973 for (i = 0; i < n; i++) {
1974 __btrfs_kill_delayed_node(delayed_nodes[i]);
1975 btrfs_release_delayed_node(delayed_nodes[i]);
1976 }
1977 }
1978}
Miao Xie67cde342012-06-14 02:23:22 -06001979
1980void btrfs_destroy_delayed_inodes(struct btrfs_root *root)
1981{
1982 struct btrfs_delayed_root *delayed_root;
1983 struct btrfs_delayed_node *curr_node, *prev_node;
1984
1985 delayed_root = btrfs_get_delayed_root(root);
1986
1987 curr_node = btrfs_first_delayed_node(delayed_root);
1988 while (curr_node) {
1989 __btrfs_kill_delayed_node(curr_node);
1990
1991 prev_node = curr_node;
1992 curr_node = btrfs_next_delayed_node(curr_node);
1993 btrfs_release_delayed_node(prev_node);
1994 }
1995}
1996