blob: 10d149c1fde186f7d859622ef98bbf6bf4feef9a [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);
57 delayed_node->count = 0;
58 delayed_node->in_list = 0;
59 delayed_node->inode_dirty = 0;
60 delayed_node->ins_root = RB_ROOT;
61 delayed_node->del_root = RB_ROOT;
62 mutex_init(&delayed_node->mutex);
63 delayed_node->index_cnt = 0;
64 INIT_LIST_HEAD(&delayed_node->n_list);
65 INIT_LIST_HEAD(&delayed_node->p_list);
66 delayed_node->bytes_reserved = 0;
Li Zefan293f7e02012-07-10 00:58:58 -060067 memset(&delayed_node->inode_item, 0, sizeof(delayed_node->inode_item));
Miao Xie16cdcec2011-04-22 18:12:22 +080068}
69
70static inline int btrfs_is_continuous_delayed_item(
71 struct btrfs_delayed_item *item1,
72 struct btrfs_delayed_item *item2)
73{
74 if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
75 item1->key.objectid == item2->key.objectid &&
76 item1->key.type == item2->key.type &&
77 item1->key.offset + 1 == item2->key.offset)
78 return 1;
79 return 0;
80}
81
82static inline struct btrfs_delayed_root *btrfs_get_delayed_root(
83 struct btrfs_root *root)
84{
85 return root->fs_info->delayed_root;
86}
87
Miao Xie2f7e33d2011-06-23 07:27:13 +000088static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode)
89{
90 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
91 struct btrfs_root *root = btrfs_inode->root;
92 u64 ino = btrfs_ino(inode);
93 struct btrfs_delayed_node *node;
94
95 node = ACCESS_ONCE(btrfs_inode->delayed_node);
96 if (node) {
97 atomic_inc(&node->refs);
98 return node;
99 }
100
101 spin_lock(&root->inode_lock);
102 node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
103 if (node) {
104 if (btrfs_inode->delayed_node) {
105 atomic_inc(&node->refs); /* can be accessed */
106 BUG_ON(btrfs_inode->delayed_node != node);
107 spin_unlock(&root->inode_lock);
108 return node;
109 }
110 btrfs_inode->delayed_node = node;
Rashika95e94d12013-10-31 03:12:42 +0530111 /* can be accessed and cached in the inode */
112 atomic_add(2, &node->refs);
Miao Xie2f7e33d2011-06-23 07:27:13 +0000113 spin_unlock(&root->inode_lock);
114 return node;
115 }
116 spin_unlock(&root->inode_lock);
117
118 return NULL;
119}
120
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100121/* Will return either the node or PTR_ERR(-ENOMEM) */
Miao Xie16cdcec2011-04-22 18:12:22 +0800122static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
123 struct inode *inode)
124{
125 struct btrfs_delayed_node *node;
126 struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
127 struct btrfs_root *root = btrfs_inode->root;
Chris Mason0d0ca302011-05-22 07:11:22 -0400128 u64 ino = btrfs_ino(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +0800129 int ret;
130
131again:
Miao Xie2f7e33d2011-06-23 07:27:13 +0000132 node = btrfs_get_delayed_node(inode);
133 if (node)
Miao Xie16cdcec2011-04-22 18:12:22 +0800134 return node;
Miao Xie16cdcec2011-04-22 18:12:22 +0800135
136 node = kmem_cache_alloc(delayed_node_cache, GFP_NOFS);
137 if (!node)
138 return ERR_PTR(-ENOMEM);
Chris Mason0d0ca302011-05-22 07:11:22 -0400139 btrfs_init_delayed_node(node, root, ino);
Miao Xie16cdcec2011-04-22 18:12:22 +0800140
Rashika95e94d12013-10-31 03:12:42 +0530141 /* cached in the btrfs inode and can be accessed */
142 atomic_add(2, &node->refs);
Miao Xie16cdcec2011-04-22 18:12:22 +0800143
144 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
145 if (ret) {
146 kmem_cache_free(delayed_node_cache, node);
147 return ERR_PTR(ret);
148 }
149
150 spin_lock(&root->inode_lock);
Chris Mason0d0ca302011-05-22 07:11:22 -0400151 ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
Miao Xie16cdcec2011-04-22 18:12:22 +0800152 if (ret == -EEXIST) {
153 kmem_cache_free(delayed_node_cache, node);
154 spin_unlock(&root->inode_lock);
155 radix_tree_preload_end();
156 goto again;
157 }
158 btrfs_inode->delayed_node = node;
159 spin_unlock(&root->inode_lock);
160 radix_tree_preload_end();
161
162 return node;
163}
164
165/*
166 * Call it when holding delayed_node->mutex
167 *
168 * If mod = 1, add this node into the prepared list.
169 */
170static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
171 struct btrfs_delayed_node *node,
172 int mod)
173{
174 spin_lock(&root->lock);
175 if (node->in_list) {
176 if (!list_empty(&node->p_list))
177 list_move_tail(&node->p_list, &root->prepare_list);
178 else if (mod)
179 list_add_tail(&node->p_list, &root->prepare_list);
180 } else {
181 list_add_tail(&node->n_list, &root->node_list);
182 list_add_tail(&node->p_list, &root->prepare_list);
183 atomic_inc(&node->refs); /* inserted into list */
184 root->nodes++;
185 node->in_list = 1;
186 }
187 spin_unlock(&root->lock);
188}
189
190/* Call it when holding delayed_node->mutex */
191static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
192 struct btrfs_delayed_node *node)
193{
194 spin_lock(&root->lock);
195 if (node->in_list) {
196 root->nodes--;
197 atomic_dec(&node->refs); /* not in the list */
198 list_del_init(&node->n_list);
199 if (!list_empty(&node->p_list))
200 list_del_init(&node->p_list);
201 node->in_list = 0;
202 }
203 spin_unlock(&root->lock);
204}
205
Eric Sandeen48a3b632013-04-25 20:41:01 +0000206static struct btrfs_delayed_node *btrfs_first_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800207 struct btrfs_delayed_root *delayed_root)
208{
209 struct list_head *p;
210 struct btrfs_delayed_node *node = NULL;
211
212 spin_lock(&delayed_root->lock);
213 if (list_empty(&delayed_root->node_list))
214 goto out;
215
216 p = delayed_root->node_list.next;
217 node = list_entry(p, struct btrfs_delayed_node, n_list);
218 atomic_inc(&node->refs);
219out:
220 spin_unlock(&delayed_root->lock);
221
222 return node;
223}
224
Eric Sandeen48a3b632013-04-25 20:41:01 +0000225static struct btrfs_delayed_node *btrfs_next_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800226 struct btrfs_delayed_node *node)
227{
228 struct btrfs_delayed_root *delayed_root;
229 struct list_head *p;
230 struct btrfs_delayed_node *next = NULL;
231
232 delayed_root = node->root->fs_info->delayed_root;
233 spin_lock(&delayed_root->lock);
234 if (!node->in_list) { /* not in the list */
235 if (list_empty(&delayed_root->node_list))
236 goto out;
237 p = delayed_root->node_list.next;
238 } else if (list_is_last(&node->n_list, &delayed_root->node_list))
239 goto out;
240 else
241 p = node->n_list.next;
242
243 next = list_entry(p, struct btrfs_delayed_node, n_list);
244 atomic_inc(&next->refs);
245out:
246 spin_unlock(&delayed_root->lock);
247
248 return next;
249}
250
251static void __btrfs_release_delayed_node(
252 struct btrfs_delayed_node *delayed_node,
253 int mod)
254{
255 struct btrfs_delayed_root *delayed_root;
256
257 if (!delayed_node)
258 return;
259
260 delayed_root = delayed_node->root->fs_info->delayed_root;
261
262 mutex_lock(&delayed_node->mutex);
263 if (delayed_node->count)
264 btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
265 else
266 btrfs_dequeue_delayed_node(delayed_root, delayed_node);
267 mutex_unlock(&delayed_node->mutex);
268
269 if (atomic_dec_and_test(&delayed_node->refs)) {
270 struct btrfs_root *root = delayed_node->root;
271 spin_lock(&root->inode_lock);
272 if (atomic_read(&delayed_node->refs) == 0) {
273 radix_tree_delete(&root->delayed_nodes_tree,
274 delayed_node->inode_id);
275 kmem_cache_free(delayed_node_cache, delayed_node);
276 }
277 spin_unlock(&root->inode_lock);
278 }
279}
280
281static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
282{
283 __btrfs_release_delayed_node(node, 0);
284}
285
Eric Sandeen48a3b632013-04-25 20:41:01 +0000286static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
Miao Xie16cdcec2011-04-22 18:12:22 +0800287 struct btrfs_delayed_root *delayed_root)
288{
289 struct list_head *p;
290 struct btrfs_delayed_node *node = NULL;
291
292 spin_lock(&delayed_root->lock);
293 if (list_empty(&delayed_root->prepare_list))
294 goto out;
295
296 p = delayed_root->prepare_list.next;
297 list_del_init(p);
298 node = list_entry(p, struct btrfs_delayed_node, p_list);
299 atomic_inc(&node->refs);
300out:
301 spin_unlock(&delayed_root->lock);
302
303 return node;
304}
305
306static inline void btrfs_release_prepared_delayed_node(
307 struct btrfs_delayed_node *node)
308{
309 __btrfs_release_delayed_node(node, 1);
310}
311
Eric Sandeen48a3b632013-04-25 20:41:01 +0000312static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
Miao Xie16cdcec2011-04-22 18:12:22 +0800313{
314 struct btrfs_delayed_item *item;
315 item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
316 if (item) {
317 item->data_len = data_len;
318 item->ins_or_del = 0;
319 item->bytes_reserved = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +0800320 item->delayed_node = NULL;
321 atomic_set(&item->refs, 1);
322 }
323 return item;
324}
325
326/*
327 * __btrfs_lookup_delayed_item - look up the delayed item by key
328 * @delayed_node: pointer to the delayed node
329 * @key: the key to look up
330 * @prev: used to store the prev item if the right item isn't found
331 * @next: used to store the next item if the right item isn't found
332 *
333 * Note: if we don't find the right item, we will return the prev item and
334 * the next item.
335 */
336static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
337 struct rb_root *root,
338 struct btrfs_key *key,
339 struct btrfs_delayed_item **prev,
340 struct btrfs_delayed_item **next)
341{
342 struct rb_node *node, *prev_node = NULL;
343 struct btrfs_delayed_item *delayed_item = NULL;
344 int ret = 0;
345
346 node = root->rb_node;
347
348 while (node) {
349 delayed_item = rb_entry(node, struct btrfs_delayed_item,
350 rb_node);
351 prev_node = node;
352 ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
353 if (ret < 0)
354 node = node->rb_right;
355 else if (ret > 0)
356 node = node->rb_left;
357 else
358 return delayed_item;
359 }
360
361 if (prev) {
362 if (!prev_node)
363 *prev = NULL;
364 else if (ret < 0)
365 *prev = delayed_item;
366 else if ((node = rb_prev(prev_node)) != NULL) {
367 *prev = rb_entry(node, struct btrfs_delayed_item,
368 rb_node);
369 } else
370 *prev = NULL;
371 }
372
373 if (next) {
374 if (!prev_node)
375 *next = NULL;
376 else if (ret > 0)
377 *next = delayed_item;
378 else if ((node = rb_next(prev_node)) != NULL) {
379 *next = rb_entry(node, struct btrfs_delayed_item,
380 rb_node);
381 } else
382 *next = NULL;
383 }
384 return NULL;
385}
386
Eric Sandeen48a3b632013-04-25 20:41:01 +0000387static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800388 struct btrfs_delayed_node *delayed_node,
389 struct btrfs_key *key)
390{
391 struct btrfs_delayed_item *item;
392
393 item = __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
394 NULL, NULL);
395 return item;
396}
397
Miao Xie16cdcec2011-04-22 18:12:22 +0800398static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
399 struct btrfs_delayed_item *ins,
400 int action)
401{
402 struct rb_node **p, *node;
403 struct rb_node *parent_node = NULL;
404 struct rb_root *root;
405 struct btrfs_delayed_item *item;
406 int cmp;
407
408 if (action == BTRFS_DELAYED_INSERTION_ITEM)
409 root = &delayed_node->ins_root;
410 else if (action == BTRFS_DELAYED_DELETION_ITEM)
411 root = &delayed_node->del_root;
412 else
413 BUG();
414 p = &root->rb_node;
415 node = &ins->rb_node;
416
417 while (*p) {
418 parent_node = *p;
419 item = rb_entry(parent_node, struct btrfs_delayed_item,
420 rb_node);
421
422 cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
423 if (cmp < 0)
424 p = &(*p)->rb_right;
425 else if (cmp > 0)
426 p = &(*p)->rb_left;
427 else
428 return -EEXIST;
429 }
430
431 rb_link_node(node, parent_node, p);
432 rb_insert_color(node, root);
433 ins->delayed_node = delayed_node;
434 ins->ins_or_del = action;
435
436 if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
437 action == BTRFS_DELAYED_INSERTION_ITEM &&
438 ins->key.offset >= delayed_node->index_cnt)
439 delayed_node->index_cnt = ins->key.offset + 1;
440
441 delayed_node->count++;
442 atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
443 return 0;
444}
445
446static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
447 struct btrfs_delayed_item *item)
448{
449 return __btrfs_add_delayed_item(node, item,
450 BTRFS_DELAYED_INSERTION_ITEM);
451}
452
453static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
454 struct btrfs_delayed_item *item)
455{
456 return __btrfs_add_delayed_item(node, item,
457 BTRFS_DELAYED_DELETION_ITEM);
458}
459
Chris Masonde3cb942013-03-04 17:13:31 -0500460static void finish_one_item(struct btrfs_delayed_root *delayed_root)
461{
462 int seq = atomic_inc_return(&delayed_root->items_seq);
463 if ((atomic_dec_return(&delayed_root->items) <
464 BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
465 waitqueue_active(&delayed_root->wait))
466 wake_up(&delayed_root->wait);
467}
468
Miao Xie16cdcec2011-04-22 18:12:22 +0800469static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
470{
471 struct rb_root *root;
472 struct btrfs_delayed_root *delayed_root;
473
474 delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
475
476 BUG_ON(!delayed_root);
477 BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
478 delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
479
480 if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
481 root = &delayed_item->delayed_node->ins_root;
482 else
483 root = &delayed_item->delayed_node->del_root;
484
485 rb_erase(&delayed_item->rb_node, root);
486 delayed_item->delayed_node->count--;
Chris Masonde3cb942013-03-04 17:13:31 -0500487
488 finish_one_item(delayed_root);
Miao Xie16cdcec2011-04-22 18:12:22 +0800489}
490
491static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
492{
493 if (item) {
494 __btrfs_remove_delayed_item(item);
495 if (atomic_dec_and_test(&item->refs))
496 kfree(item);
497 }
498}
499
Eric Sandeen48a3b632013-04-25 20:41:01 +0000500static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800501 struct btrfs_delayed_node *delayed_node)
502{
503 struct rb_node *p;
504 struct btrfs_delayed_item *item = NULL;
505
506 p = rb_first(&delayed_node->ins_root);
507 if (p)
508 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
509
510 return item;
511}
512
Eric Sandeen48a3b632013-04-25 20:41:01 +0000513static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800514 struct btrfs_delayed_node *delayed_node)
515{
516 struct rb_node *p;
517 struct btrfs_delayed_item *item = NULL;
518
519 p = rb_first(&delayed_node->del_root);
520 if (p)
521 item = rb_entry(p, struct btrfs_delayed_item, rb_node);
522
523 return item;
524}
525
Eric Sandeen48a3b632013-04-25 20:41:01 +0000526static struct btrfs_delayed_item *__btrfs_next_delayed_item(
Miao Xie16cdcec2011-04-22 18:12:22 +0800527 struct btrfs_delayed_item *item)
528{
529 struct rb_node *p;
530 struct btrfs_delayed_item *next = NULL;
531
532 p = rb_next(&item->rb_node);
533 if (p)
534 next = rb_entry(p, struct btrfs_delayed_item, rb_node);
535
536 return next;
537}
538
Miao Xie16cdcec2011-04-22 18:12:22 +0800539static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
540 struct btrfs_root *root,
541 struct btrfs_delayed_item *item)
542{
543 struct btrfs_block_rsv *src_rsv;
544 struct btrfs_block_rsv *dst_rsv;
545 u64 num_bytes;
546 int ret;
547
548 if (!trans->bytes_reserved)
549 return 0;
550
551 src_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -0400552 dst_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +0800553
554 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
555 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500556 if (!ret) {
557 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
558 item->key.objectid,
559 num_bytes, 1);
Miao Xie16cdcec2011-04-22 18:12:22 +0800560 item->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500561 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800562
563 return ret;
564}
565
566static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
567 struct btrfs_delayed_item *item)
568{
Miao Xie19fd2942011-06-15 10:47:30 +0000569 struct btrfs_block_rsv *rsv;
570
Miao Xie16cdcec2011-04-22 18:12:22 +0800571 if (!item->bytes_reserved)
572 return;
573
Josef Bacik6d668dd2011-11-03 22:54:25 -0400574 rsv = &root->fs_info->delayed_block_rsv;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500575 trace_btrfs_space_reservation(root->fs_info, "delayed_item",
576 item->key.objectid, item->bytes_reserved,
577 0);
Miao Xie19fd2942011-06-15 10:47:30 +0000578 btrfs_block_rsv_release(root, rsv,
Miao Xie16cdcec2011-04-22 18:12:22 +0800579 item->bytes_reserved);
580}
581
582static int btrfs_delayed_inode_reserve_metadata(
583 struct btrfs_trans_handle *trans,
584 struct btrfs_root *root,
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500585 struct inode *inode,
Miao Xie16cdcec2011-04-22 18:12:22 +0800586 struct btrfs_delayed_node *node)
587{
588 struct btrfs_block_rsv *src_rsv;
589 struct btrfs_block_rsv *dst_rsv;
590 u64 num_bytes;
591 int ret;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500592 bool release = false;
Miao Xie16cdcec2011-04-22 18:12:22 +0800593
Miao Xie16cdcec2011-04-22 18:12:22 +0800594 src_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -0400595 dst_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +0800596
597 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacikc06a0e12011-11-04 19:56:02 -0400598
599 /*
600 * btrfs_dirty_inode will update the inode under btrfs_join_transaction
601 * which doesn't reserve space for speed. This is a problem since we
602 * still need to reserve space for this update, so try to reserve the
603 * space.
604 *
605 * Now if src_rsv == delalloc_block_rsv we'll let it just steal since
606 * we're accounted for.
607 */
Chris Masone755d9a2011-12-15 13:36:29 -0500608 if (!src_rsv || (!trans->bytes_reserved &&
Miao Xie66d8f3d2012-09-06 04:02:28 -0600609 src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
Miao Xie08e007d2012-10-16 11:33:38 +0000610 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
611 BTRFS_RESERVE_NO_FLUSH);
Josef Bacikc06a0e12011-11-04 19:56:02 -0400612 /*
613 * Since we're under a transaction reserve_metadata_bytes could
614 * try to commit the transaction which will make it return
615 * EAGAIN to make us stop the transaction we have, so return
616 * ENOSPC instead so that btrfs_dirty_inode knows what to do.
617 */
618 if (ret == -EAGAIN)
619 ret = -ENOSPC;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500620 if (!ret) {
Josef Bacikc06a0e12011-11-04 19:56:02 -0400621 node->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500622 trace_btrfs_space_reservation(root->fs_info,
623 "delayed_inode",
624 btrfs_ino(inode),
625 num_bytes, 1);
626 }
Josef Bacikc06a0e12011-11-04 19:56:02 -0400627 return ret;
Miao Xie66d8f3d2012-09-06 04:02:28 -0600628 } else if (src_rsv->type == BTRFS_BLOCK_RSV_DELALLOC) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500629 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik72ac3c02012-05-23 14:13:11 -0400630 if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
631 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500632 spin_unlock(&BTRFS_I(inode)->lock);
633 release = true;
634 goto migrate;
635 }
636 spin_unlock(&BTRFS_I(inode)->lock);
637
638 /* Ok we didn't have space pre-reserved. This shouldn't happen
639 * too often but it can happen if we do delalloc to an existing
640 * inode which gets dirtied because of the time update, and then
641 * isn't touched again until after the transaction commits and
642 * then we try to write out the data. First try to be nice and
643 * reserve something strictly for us. If not be a pain and try
644 * to steal from the delalloc block rsv.
645 */
Miao Xie08e007d2012-10-16 11:33:38 +0000646 ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
647 BTRFS_RESERVE_NO_FLUSH);
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500648 if (!ret)
649 goto out;
650
651 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +0530652 if (!WARN_ON(ret))
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500653 goto out;
654
655 /*
656 * Ok this is a problem, let's just steal from the global rsv
657 * since this really shouldn't happen that often.
658 */
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500659 ret = btrfs_block_rsv_migrate(&root->fs_info->global_block_rsv,
660 dst_rsv, num_bytes);
661 goto out;
Josef Bacikc06a0e12011-11-04 19:56:02 -0400662 }
663
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500664migrate:
Miao Xie16cdcec2011-04-22 18:12:22 +0800665 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500666
667out:
668 /*
669 * Migrate only takes a reservation, it doesn't touch the size of the
670 * block_rsv. This is to simplify people who don't normally have things
671 * migrated from their block rsv. If they go to release their
672 * reservation, that will decrease the size as well, so if migrate
673 * reduced size we'd end up with a negative size. But for the
674 * delalloc_meta_reserved stuff we will only know to drop 1 reservation,
675 * but we could in fact do this reserve/migrate dance several times
676 * between the time we did the original reservation and we'd clean it
677 * up. So to take care of this, release the space for the meta
678 * reservation here. I think it may be time for a documentation page on
679 * how block rsvs. work.
680 */
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500681 if (!ret) {
682 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
683 btrfs_ino(inode), num_bytes, 1);
Miao Xie16cdcec2011-04-22 18:12:22 +0800684 node->bytes_reserved = num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500685 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800686
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500687 if (release) {
688 trace_btrfs_space_reservation(root->fs_info, "delalloc",
689 btrfs_ino(inode), num_bytes, 0);
Josef Bacik7fd2ae22011-11-08 15:47:34 -0500690 btrfs_block_rsv_release(root, src_rsv, num_bytes);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500691 }
Miao Xie16cdcec2011-04-22 18:12:22 +0800692
693 return ret;
694}
695
696static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
697 struct btrfs_delayed_node *node)
698{
699 struct btrfs_block_rsv *rsv;
700
701 if (!node->bytes_reserved)
702 return;
703
Josef Bacik6d668dd2011-11-03 22:54:25 -0400704 rsv = &root->fs_info->delayed_block_rsv;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -0500705 trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
706 node->inode_id, node->bytes_reserved, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +0800707 btrfs_block_rsv_release(root, rsv,
708 node->bytes_reserved);
709 node->bytes_reserved = 0;
710}
711
712/*
713 * This helper will insert some continuous items into the same leaf according
714 * to the free space of the leaf.
715 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000716static int btrfs_batch_insert_items(struct btrfs_root *root,
717 struct btrfs_path *path,
718 struct btrfs_delayed_item *item)
Miao Xie16cdcec2011-04-22 18:12:22 +0800719{
720 struct btrfs_delayed_item *curr, *next;
721 int free_space;
722 int total_data_size = 0, total_size = 0;
723 struct extent_buffer *leaf;
724 char *data_ptr;
725 struct btrfs_key *keys;
726 u32 *data_size;
727 struct list_head head;
728 int slot;
729 int nitems;
730 int i;
731 int ret = 0;
732
733 BUG_ON(!path->nodes[0]);
734
735 leaf = path->nodes[0];
736 free_space = btrfs_leaf_free_space(root, leaf);
737 INIT_LIST_HEAD(&head);
738
739 next = item;
Chris Mason17aca1c2011-06-03 01:13:45 -0400740 nitems = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +0800741
742 /*
743 * count the number of the continuous items that we can insert in batch
744 */
745 while (total_size + next->data_len + sizeof(struct btrfs_item) <=
746 free_space) {
747 total_data_size += next->data_len;
748 total_size += next->data_len + sizeof(struct btrfs_item);
749 list_add_tail(&next->tree_list, &head);
750 nitems++;
751
752 curr = next;
753 next = __btrfs_next_delayed_item(curr);
754 if (!next)
755 break;
756
757 if (!btrfs_is_continuous_delayed_item(curr, next))
758 break;
759 }
760
761 if (!nitems) {
762 ret = 0;
763 goto out;
764 }
765
766 /*
767 * we need allocate some memory space, but it might cause the task
768 * to sleep, so we set all locked nodes in the path to blocking locks
769 * first.
770 */
771 btrfs_set_path_blocking(path);
772
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +0530773 keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800774 if (!keys) {
775 ret = -ENOMEM;
776 goto out;
777 }
778
Dulshani Gunawardhanad9b0d9b2013-10-31 10:32:18 +0530779 data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
Miao Xie16cdcec2011-04-22 18:12:22 +0800780 if (!data_size) {
781 ret = -ENOMEM;
782 goto error;
783 }
784
785 /* get keys of all the delayed items */
786 i = 0;
787 list_for_each_entry(next, &head, tree_list) {
788 keys[i] = next->key;
789 data_size[i] = next->data_len;
790 i++;
791 }
792
793 /* reset all the locked nodes in the patch to spinning locks. */
Chris Masonbd681512011-07-16 15:23:14 -0400794 btrfs_clear_path_blocking(path, NULL, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +0800795
796 /* insert the keys of the items */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000797 setup_items_for_insert(root, path, keys, data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +0100798 total_data_size, total_size, nitems);
Miao Xie16cdcec2011-04-22 18:12:22 +0800799
800 /* insert the dir index items */
801 slot = path->slots[0];
802 list_for_each_entry_safe(curr, next, &head, tree_list) {
803 data_ptr = btrfs_item_ptr(leaf, slot, char);
804 write_extent_buffer(leaf, &curr->data,
805 (unsigned long)data_ptr,
806 curr->data_len);
807 slot++;
808
809 btrfs_delayed_item_release_metadata(root, curr);
810
811 list_del(&curr->tree_list);
812 btrfs_release_delayed_item(curr);
813 }
814
815error:
816 kfree(data_size);
817 kfree(keys);
818out:
819 return ret;
820}
821
822/*
823 * This helper can just do simple insertion that needn't extend item for new
824 * data, such as directory name index insertion, inode insertion.
825 */
826static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_path *path,
829 struct btrfs_delayed_item *delayed_item)
830{
831 struct extent_buffer *leaf;
Miao Xie16cdcec2011-04-22 18:12:22 +0800832 char *ptr;
833 int ret;
834
835 ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
836 delayed_item->data_len);
837 if (ret < 0 && ret != -EEXIST)
838 return ret;
839
840 leaf = path->nodes[0];
841
Miao Xie16cdcec2011-04-22 18:12:22 +0800842 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
843
844 write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
845 delayed_item->data_len);
846 btrfs_mark_buffer_dirty(leaf);
847
848 btrfs_delayed_item_release_metadata(root, delayed_item);
849 return 0;
850}
851
852/*
853 * we insert an item first, then if there are some continuous items, we try
854 * to insert those items into the same leaf.
855 */
856static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
857 struct btrfs_path *path,
858 struct btrfs_root *root,
859 struct btrfs_delayed_node *node)
860{
861 struct btrfs_delayed_item *curr, *prev;
862 int ret = 0;
863
864do_again:
865 mutex_lock(&node->mutex);
866 curr = __btrfs_first_delayed_insertion_item(node);
867 if (!curr)
868 goto insert_end;
869
870 ret = btrfs_insert_delayed_item(trans, root, path, curr);
871 if (ret < 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400872 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800873 goto insert_end;
874 }
875
876 prev = curr;
877 curr = __btrfs_next_delayed_item(prev);
878 if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
879 /* insert the continuous items into the same leaf */
880 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +0000881 btrfs_batch_insert_items(root, path, curr);
Miao Xie16cdcec2011-04-22 18:12:22 +0800882 }
883 btrfs_release_delayed_item(prev);
884 btrfs_mark_buffer_dirty(path->nodes[0]);
885
Chris Mason945d8962011-05-22 12:33:42 -0400886 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800887 mutex_unlock(&node->mutex);
888 goto do_again;
889
890insert_end:
891 mutex_unlock(&node->mutex);
892 return ret;
893}
894
895static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root,
897 struct btrfs_path *path,
898 struct btrfs_delayed_item *item)
899{
900 struct btrfs_delayed_item *curr, *next;
901 struct extent_buffer *leaf;
902 struct btrfs_key key;
903 struct list_head head;
904 int nitems, i, last_item;
905 int ret = 0;
906
907 BUG_ON(!path->nodes[0]);
908
909 leaf = path->nodes[0];
910
911 i = path->slots[0];
912 last_item = btrfs_header_nritems(leaf) - 1;
913 if (i > last_item)
914 return -ENOENT; /* FIXME: Is errno suitable? */
915
916 next = item;
917 INIT_LIST_HEAD(&head);
918 btrfs_item_key_to_cpu(leaf, &key, i);
919 nitems = 0;
920 /*
921 * count the number of the dir index items that we can delete in batch
922 */
923 while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
924 list_add_tail(&next->tree_list, &head);
925 nitems++;
926
927 curr = next;
928 next = __btrfs_next_delayed_item(curr);
929 if (!next)
930 break;
931
932 if (!btrfs_is_continuous_delayed_item(curr, next))
933 break;
934
935 i++;
936 if (i > last_item)
937 break;
938 btrfs_item_key_to_cpu(leaf, &key, i);
939 }
940
941 if (!nitems)
942 return 0;
943
944 ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
945 if (ret)
946 goto out;
947
948 list_for_each_entry_safe(curr, next, &head, tree_list) {
949 btrfs_delayed_item_release_metadata(root, curr);
950 list_del(&curr->tree_list);
951 btrfs_release_delayed_item(curr);
952 }
953
954out:
955 return ret;
956}
957
958static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
959 struct btrfs_path *path,
960 struct btrfs_root *root,
961 struct btrfs_delayed_node *node)
962{
963 struct btrfs_delayed_item *curr, *prev;
964 int ret = 0;
965
966do_again:
967 mutex_lock(&node->mutex);
968 curr = __btrfs_first_delayed_deletion_item(node);
969 if (!curr)
970 goto delete_fail;
971
972 ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
973 if (ret < 0)
974 goto delete_fail;
975 else if (ret > 0) {
976 /*
977 * can't find the item which the node points to, so this node
978 * is invalid, just drop it.
979 */
980 prev = curr;
981 curr = __btrfs_next_delayed_item(prev);
982 btrfs_release_delayed_item(prev);
983 ret = 0;
Chris Mason945d8962011-05-22 12:33:42 -0400984 btrfs_release_path(path);
Fengguang Wu62095262012-08-04 01:45:02 -0600985 if (curr) {
986 mutex_unlock(&node->mutex);
Miao Xie16cdcec2011-04-22 18:12:22 +0800987 goto do_again;
Fengguang Wu62095262012-08-04 01:45:02 -0600988 } else
Miao Xie16cdcec2011-04-22 18:12:22 +0800989 goto delete_fail;
990 }
991
992 btrfs_batch_delete_items(trans, root, path, curr);
Chris Mason945d8962011-05-22 12:33:42 -0400993 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800994 mutex_unlock(&node->mutex);
995 goto do_again;
996
997delete_fail:
Chris Mason945d8962011-05-22 12:33:42 -0400998 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +0800999 mutex_unlock(&node->mutex);
1000 return ret;
1001}
1002
1003static void btrfs_release_delayed_inode(struct btrfs_delayed_node *delayed_node)
1004{
1005 struct btrfs_delayed_root *delayed_root;
1006
1007 if (delayed_node && delayed_node->inode_dirty) {
1008 BUG_ON(!delayed_node->root);
1009 delayed_node->inode_dirty = 0;
1010 delayed_node->count--;
1011
1012 delayed_root = delayed_node->root->fs_info->delayed_root;
Chris Masonde3cb942013-03-04 17:13:31 -05001013 finish_one_item(delayed_root);
Miao Xie16cdcec2011-04-22 18:12:22 +08001014 }
1015}
1016
Miao Xie0e8c36a2012-12-19 06:59:51 +00001017static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1018 struct btrfs_root *root,
1019 struct btrfs_path *path,
1020 struct btrfs_delayed_node *node)
Miao Xie16cdcec2011-04-22 18:12:22 +08001021{
1022 struct btrfs_key key;
1023 struct btrfs_inode_item *inode_item;
1024 struct extent_buffer *leaf;
1025 int ret;
1026
Miao Xie16cdcec2011-04-22 18:12:22 +08001027 key.objectid = node->inode_id;
1028 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1029 key.offset = 0;
Miao Xie0e8c36a2012-12-19 06:59:51 +00001030
Miao Xie16cdcec2011-04-22 18:12:22 +08001031 ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1032 if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -04001033 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +08001034 return -ENOENT;
1035 } else if (ret < 0) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001036 return ret;
1037 }
1038
1039 btrfs_unlock_up_safe(path, 1);
1040 leaf = path->nodes[0];
1041 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1042 struct btrfs_inode_item);
1043 write_extent_buffer(leaf, &node->inode_item, (unsigned long)inode_item,
1044 sizeof(struct btrfs_inode_item));
1045 btrfs_mark_buffer_dirty(leaf);
Chris Mason945d8962011-05-22 12:33:42 -04001046 btrfs_release_path(path);
Miao Xie16cdcec2011-04-22 18:12:22 +08001047
1048 btrfs_delayed_inode_release_metadata(root, node);
1049 btrfs_release_delayed_inode(node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001050
1051 return 0;
1052}
1053
Miao Xie0e8c36a2012-12-19 06:59:51 +00001054static inline int btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1055 struct btrfs_root *root,
1056 struct btrfs_path *path,
1057 struct btrfs_delayed_node *node)
1058{
1059 int ret;
1060
1061 mutex_lock(&node->mutex);
1062 if (!node->inode_dirty) {
1063 mutex_unlock(&node->mutex);
1064 return 0;
1065 }
1066
1067 ret = __btrfs_update_delayed_inode(trans, root, path, node);
1068 mutex_unlock(&node->mutex);
1069 return ret;
1070}
1071
Miao Xie4ea41ce2012-12-19 06:59:03 +00001072static inline int
1073__btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1074 struct btrfs_path *path,
1075 struct btrfs_delayed_node *node)
1076{
1077 int ret;
1078
1079 ret = btrfs_insert_delayed_items(trans, path, node->root, node);
1080 if (ret)
1081 return ret;
1082
1083 ret = btrfs_delete_delayed_items(trans, path, node->root, node);
1084 if (ret)
1085 return ret;
1086
1087 ret = btrfs_update_delayed_inode(trans, node->root, path, node);
1088 return ret;
1089}
1090
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001091/*
1092 * Called when committing the transaction.
1093 * Returns 0 on success.
1094 * Returns < 0 on error and returns with an aborted transaction with any
1095 * outstanding delayed items cleaned up.
1096 */
Josef Bacik96c3f432012-06-21 14:05:49 -04001097static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1098 struct btrfs_root *root, int nr)
Miao Xie16cdcec2011-04-22 18:12:22 +08001099{
1100 struct btrfs_delayed_root *delayed_root;
1101 struct btrfs_delayed_node *curr_node, *prev_node;
1102 struct btrfs_path *path;
Miao Xie19fd2942011-06-15 10:47:30 +00001103 struct btrfs_block_rsv *block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001104 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04001105 bool count = (nr > 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001106
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001107 if (trans->aborted)
1108 return -EIO;
1109
Miao Xie16cdcec2011-04-22 18:12:22 +08001110 path = btrfs_alloc_path();
1111 if (!path)
1112 return -ENOMEM;
1113 path->leave_spinning = 1;
1114
Miao Xie19fd2942011-06-15 10:47:30 +00001115 block_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -04001116 trans->block_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie19fd2942011-06-15 10:47:30 +00001117
Miao Xie16cdcec2011-04-22 18:12:22 +08001118 delayed_root = btrfs_get_delayed_root(root);
1119
1120 curr_node = btrfs_first_delayed_node(delayed_root);
Josef Bacik96c3f432012-06-21 14:05:49 -04001121 while (curr_node && (!count || (count && nr--))) {
Miao Xie4ea41ce2012-12-19 06:59:03 +00001122 ret = __btrfs_commit_inode_delayed_items(trans, path,
1123 curr_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001124 if (ret) {
1125 btrfs_release_delayed_node(curr_node);
Josef Bacik96c3f432012-06-21 14:05:49 -04001126 curr_node = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001127 btrfs_abort_transaction(trans, root, ret);
Miao Xie16cdcec2011-04-22 18:12:22 +08001128 break;
1129 }
1130
1131 prev_node = curr_node;
1132 curr_node = btrfs_next_delayed_node(curr_node);
1133 btrfs_release_delayed_node(prev_node);
1134 }
1135
Josef Bacik96c3f432012-06-21 14:05:49 -04001136 if (curr_node)
1137 btrfs_release_delayed_node(curr_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001138 btrfs_free_path(path);
Miao Xie19fd2942011-06-15 10:47:30 +00001139 trans->block_rsv = block_rsv;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001140
Miao Xie16cdcec2011-04-22 18:12:22 +08001141 return ret;
1142}
1143
Josef Bacik96c3f432012-06-21 14:05:49 -04001144int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1145 struct btrfs_root *root)
1146{
1147 return __btrfs_run_delayed_items(trans, root, -1);
1148}
1149
1150int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
1151 struct btrfs_root *root, int nr)
1152{
1153 return __btrfs_run_delayed_items(trans, root, nr);
1154}
1155
Miao Xie16cdcec2011-04-22 18:12:22 +08001156int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1157 struct inode *inode)
1158{
1159 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001160 struct btrfs_path *path;
1161 struct btrfs_block_rsv *block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001162 int ret;
1163
1164 if (!delayed_node)
1165 return 0;
1166
1167 mutex_lock(&delayed_node->mutex);
1168 if (!delayed_node->count) {
1169 mutex_unlock(&delayed_node->mutex);
1170 btrfs_release_delayed_node(delayed_node);
1171 return 0;
1172 }
1173 mutex_unlock(&delayed_node->mutex);
1174
Miao Xie4ea41ce2012-12-19 06:59:03 +00001175 path = btrfs_alloc_path();
Filipe David Borba Manana3c77bd92013-10-12 20:32:59 +01001176 if (!path) {
1177 btrfs_release_delayed_node(delayed_node);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001178 return -ENOMEM;
Filipe David Borba Manana3c77bd92013-10-12 20:32:59 +01001179 }
Miao Xie4ea41ce2012-12-19 06:59:03 +00001180 path->leave_spinning = 1;
1181
1182 block_rsv = trans->block_rsv;
1183 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1184
1185 ret = __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
1186
Miao Xie16cdcec2011-04-22 18:12:22 +08001187 btrfs_release_delayed_node(delayed_node);
Miao Xie4ea41ce2012-12-19 06:59:03 +00001188 btrfs_free_path(path);
1189 trans->block_rsv = block_rsv;
1190
Miao Xie16cdcec2011-04-22 18:12:22 +08001191 return ret;
1192}
1193
Miao Xie0e8c36a2012-12-19 06:59:51 +00001194int btrfs_commit_inode_delayed_inode(struct inode *inode)
1195{
1196 struct btrfs_trans_handle *trans;
1197 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1198 struct btrfs_path *path;
1199 struct btrfs_block_rsv *block_rsv;
1200 int ret;
1201
1202 if (!delayed_node)
1203 return 0;
1204
1205 mutex_lock(&delayed_node->mutex);
1206 if (!delayed_node->inode_dirty) {
1207 mutex_unlock(&delayed_node->mutex);
1208 btrfs_release_delayed_node(delayed_node);
1209 return 0;
1210 }
1211 mutex_unlock(&delayed_node->mutex);
1212
1213 trans = btrfs_join_transaction(delayed_node->root);
1214 if (IS_ERR(trans)) {
1215 ret = PTR_ERR(trans);
1216 goto out;
1217 }
1218
1219 path = btrfs_alloc_path();
1220 if (!path) {
1221 ret = -ENOMEM;
1222 goto trans_out;
1223 }
1224 path->leave_spinning = 1;
1225
1226 block_rsv = trans->block_rsv;
1227 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv;
1228
1229 mutex_lock(&delayed_node->mutex);
1230 if (delayed_node->inode_dirty)
1231 ret = __btrfs_update_delayed_inode(trans, delayed_node->root,
1232 path, delayed_node);
1233 else
1234 ret = 0;
1235 mutex_unlock(&delayed_node->mutex);
1236
1237 btrfs_free_path(path);
1238 trans->block_rsv = block_rsv;
1239trans_out:
1240 btrfs_end_transaction(trans, delayed_node->root);
1241 btrfs_btree_balance_dirty(delayed_node->root);
1242out:
1243 btrfs_release_delayed_node(delayed_node);
1244
1245 return ret;
1246}
1247
Miao Xie16cdcec2011-04-22 18:12:22 +08001248void btrfs_remove_delayed_node(struct inode *inode)
1249{
1250 struct btrfs_delayed_node *delayed_node;
1251
1252 delayed_node = ACCESS_ONCE(BTRFS_I(inode)->delayed_node);
1253 if (!delayed_node)
1254 return;
1255
1256 BTRFS_I(inode)->delayed_node = NULL;
1257 btrfs_release_delayed_node(delayed_node);
1258}
1259
Chris Masonde3cb942013-03-04 17:13:31 -05001260struct btrfs_async_delayed_work {
1261 struct btrfs_delayed_root *delayed_root;
1262 int nr;
Miao Xie16cdcec2011-04-22 18:12:22 +08001263 struct btrfs_work work;
1264};
1265
Chris Masonde3cb942013-03-04 17:13:31 -05001266static void btrfs_async_run_delayed_root(struct btrfs_work *work)
Miao Xie16cdcec2011-04-22 18:12:22 +08001267{
Chris Masonde3cb942013-03-04 17:13:31 -05001268 struct btrfs_async_delayed_work *async_work;
1269 struct btrfs_delayed_root *delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001270 struct btrfs_trans_handle *trans;
1271 struct btrfs_path *path;
1272 struct btrfs_delayed_node *delayed_node = NULL;
1273 struct btrfs_root *root;
Miao Xie19fd2942011-06-15 10:47:30 +00001274 struct btrfs_block_rsv *block_rsv;
Chris Masonde3cb942013-03-04 17:13:31 -05001275 int total_done = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001276
Chris Masonde3cb942013-03-04 17:13:31 -05001277 async_work = container_of(work, struct btrfs_async_delayed_work, work);
1278 delayed_root = async_work->delayed_root;
Miao Xie16cdcec2011-04-22 18:12:22 +08001279
1280 path = btrfs_alloc_path();
1281 if (!path)
1282 goto out;
Miao Xie16cdcec2011-04-22 18:12:22 +08001283
Chris Masonde3cb942013-03-04 17:13:31 -05001284again:
1285 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND / 2)
1286 goto free_path;
1287
1288 delayed_node = btrfs_first_prepared_delayed_node(delayed_root);
1289 if (!delayed_node)
1290 goto free_path;
1291
1292 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08001293 root = delayed_node->root;
1294
Chris Masonff5714c2011-05-28 07:00:39 -04001295 trans = btrfs_join_transaction(root);
Miao Xie16cdcec2011-04-22 18:12:22 +08001296 if (IS_ERR(trans))
Chris Masonde3cb942013-03-04 17:13:31 -05001297 goto release_path;
Miao Xie16cdcec2011-04-22 18:12:22 +08001298
Miao Xie19fd2942011-06-15 10:47:30 +00001299 block_rsv = trans->block_rsv;
Josef Bacik6d668dd2011-11-03 22:54:25 -04001300 trans->block_rsv = &root->fs_info->delayed_block_rsv;
Miao Xie19fd2942011-06-15 10:47:30 +00001301
Miao Xie4ea41ce2012-12-19 06:59:03 +00001302 __btrfs_commit_inode_delayed_items(trans, path, delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001303
Miao Xie19fd2942011-06-15 10:47:30 +00001304 trans->block_rsv = block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001305 btrfs_end_transaction_dmeta(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00001306 btrfs_btree_balance_dirty_nodelay(root);
Chris Masonde3cb942013-03-04 17:13:31 -05001307
1308release_path:
1309 btrfs_release_path(path);
1310 total_done++;
1311
1312 btrfs_release_prepared_delayed_node(delayed_node);
1313 if (async_work->nr == 0 || total_done < async_work->nr)
1314 goto again;
1315
Miao Xie16cdcec2011-04-22 18:12:22 +08001316free_path:
1317 btrfs_free_path(path);
1318out:
Chris Masonde3cb942013-03-04 17:13:31 -05001319 wake_up(&delayed_root->wait);
1320 kfree(async_work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001321}
1322
Miao Xie16cdcec2011-04-22 18:12:22 +08001323
Chris Masonde3cb942013-03-04 17:13:31 -05001324static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1325 struct btrfs_root *root, int nr)
1326{
1327 struct btrfs_async_delayed_work *async_work;
1328
1329 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
Miao Xie16cdcec2011-04-22 18:12:22 +08001330 return 0;
1331
Chris Masonde3cb942013-03-04 17:13:31 -05001332 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1333 if (!async_work)
Miao Xie16cdcec2011-04-22 18:12:22 +08001334 return -ENOMEM;
Miao Xie16cdcec2011-04-22 18:12:22 +08001335
Chris Masonde3cb942013-03-04 17:13:31 -05001336 async_work->delayed_root = delayed_root;
1337 async_work->work.func = btrfs_async_run_delayed_root;
1338 async_work->work.flags = 0;
1339 async_work->nr = nr;
Miao Xie16cdcec2011-04-22 18:12:22 +08001340
Chris Masonde3cb942013-03-04 17:13:31 -05001341 btrfs_queue_worker(&root->fs_info->delayed_workers, &async_work->work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001342 return 0;
1343}
1344
Chris Masone9993762011-06-17 16:14:09 -04001345void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
1346{
1347 struct btrfs_delayed_root *delayed_root;
1348 delayed_root = btrfs_get_delayed_root(root);
1349 WARN_ON(btrfs_first_delayed_node(delayed_root));
1350}
1351
Chris Masonde3cb942013-03-04 17:13:31 -05001352static int refs_newer(struct btrfs_delayed_root *delayed_root,
1353 int seq, int count)
1354{
1355 int val = atomic_read(&delayed_root->items_seq);
1356
1357 if (val < seq || val >= seq + count)
1358 return 1;
1359 return 0;
1360}
1361
Miao Xie16cdcec2011-04-22 18:12:22 +08001362void btrfs_balance_delayed_items(struct btrfs_root *root)
1363{
1364 struct btrfs_delayed_root *delayed_root;
Chris Masonde3cb942013-03-04 17:13:31 -05001365 int seq;
Miao Xie16cdcec2011-04-22 18:12:22 +08001366
1367 delayed_root = btrfs_get_delayed_root(root);
1368
1369 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1370 return;
1371
Chris Masonde3cb942013-03-04 17:13:31 -05001372 seq = atomic_read(&delayed_root->items_seq);
1373
Miao Xie16cdcec2011-04-22 18:12:22 +08001374 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1375 int ret;
Chris Masonde3cb942013-03-04 17:13:31 -05001376 DEFINE_WAIT(__wait);
1377
1378 ret = btrfs_wq_run_delayed_node(delayed_root, root, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001379 if (ret)
1380 return;
1381
Chris Masonde3cb942013-03-04 17:13:31 -05001382 while (1) {
1383 prepare_to_wait(&delayed_root->wait, &__wait,
1384 TASK_INTERRUPTIBLE);
1385
1386 if (refs_newer(delayed_root, seq,
1387 BTRFS_DELAYED_BATCH) ||
1388 atomic_read(&delayed_root->items) <
1389 BTRFS_DELAYED_BACKGROUND) {
1390 break;
1391 }
1392 if (!signal_pending(current))
1393 schedule();
1394 else
1395 break;
1396 }
1397 finish_wait(&delayed_root->wait, &__wait);
Miao Xie4dd466d2013-12-26 13:07:02 +08001398 return;
Miao Xie16cdcec2011-04-22 18:12:22 +08001399 }
1400
Chris Masonde3cb942013-03-04 17:13:31 -05001401 btrfs_wq_run_delayed_node(delayed_root, root, BTRFS_DELAYED_BATCH);
Miao Xie16cdcec2011-04-22 18:12:22 +08001402}
1403
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001404/* Will return 0 or -ENOMEM */
Miao Xie16cdcec2011-04-22 18:12:22 +08001405int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1406 struct btrfs_root *root, const char *name,
1407 int name_len, struct inode *dir,
1408 struct btrfs_disk_key *disk_key, u8 type,
1409 u64 index)
1410{
1411 struct btrfs_delayed_node *delayed_node;
1412 struct btrfs_delayed_item *delayed_item;
1413 struct btrfs_dir_item *dir_item;
1414 int ret;
1415
1416 delayed_node = btrfs_get_or_create_delayed_node(dir);
1417 if (IS_ERR(delayed_node))
1418 return PTR_ERR(delayed_node);
1419
1420 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1421 if (!delayed_item) {
1422 ret = -ENOMEM;
1423 goto release_node;
1424 }
1425
Chris Mason0d0ca302011-05-22 07:11:22 -04001426 delayed_item->key.objectid = btrfs_ino(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08001427 btrfs_set_key_type(&delayed_item->key, BTRFS_DIR_INDEX_KEY);
1428 delayed_item->key.offset = index;
1429
1430 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1431 dir_item->location = *disk_key;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001432 btrfs_set_stack_dir_transid(dir_item, trans->transid);
1433 btrfs_set_stack_dir_data_len(dir_item, 0);
1434 btrfs_set_stack_dir_name_len(dir_item, name_len);
1435 btrfs_set_stack_dir_type(dir_item, type);
Miao Xie16cdcec2011-04-22 18:12:22 +08001436 memcpy((char *)(dir_item + 1), name, name_len);
1437
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05001438 ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item);
1439 /*
1440 * we have reserved enough space when we start a new transaction,
1441 * so reserving metadata failure is impossible
1442 */
1443 BUG_ON(ret);
1444
1445
Miao Xie16cdcec2011-04-22 18:12:22 +08001446 mutex_lock(&delayed_node->mutex);
1447 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1448 if (unlikely(ret)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001449 btrfs_err(root->fs_info, "err add delayed dir index item(name: %.*s) "
Filipe David Borba Mananabdab49d2013-08-20 17:51:48 +01001450 "into the insertion tree of the delayed node"
Frank Holtonefe120a2013-12-20 11:37:06 -05001451 "(root id: %llu, inode id: %llu, errno: %d)",
Filipe David Borba Mananabdab49d2013-08-20 17:51:48 +01001452 name_len, name, delayed_node->root->objectid,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001453 delayed_node->inode_id, ret);
Miao Xie16cdcec2011-04-22 18:12:22 +08001454 BUG();
1455 }
1456 mutex_unlock(&delayed_node->mutex);
1457
1458release_node:
1459 btrfs_release_delayed_node(delayed_node);
1460 return ret;
1461}
1462
1463static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root,
1464 struct btrfs_delayed_node *node,
1465 struct btrfs_key *key)
1466{
1467 struct btrfs_delayed_item *item;
1468
1469 mutex_lock(&node->mutex);
1470 item = __btrfs_lookup_delayed_insertion_item(node, key);
1471 if (!item) {
1472 mutex_unlock(&node->mutex);
1473 return 1;
1474 }
1475
1476 btrfs_delayed_item_release_metadata(root, item);
1477 btrfs_release_delayed_item(item);
1478 mutex_unlock(&node->mutex);
1479 return 0;
1480}
1481
1482int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1483 struct btrfs_root *root, struct inode *dir,
1484 u64 index)
1485{
1486 struct btrfs_delayed_node *node;
1487 struct btrfs_delayed_item *item;
1488 struct btrfs_key item_key;
1489 int ret;
1490
1491 node = btrfs_get_or_create_delayed_node(dir);
1492 if (IS_ERR(node))
1493 return PTR_ERR(node);
1494
Chris Mason0d0ca302011-05-22 07:11:22 -04001495 item_key.objectid = btrfs_ino(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08001496 btrfs_set_key_type(&item_key, BTRFS_DIR_INDEX_KEY);
1497 item_key.offset = index;
1498
1499 ret = btrfs_delete_delayed_insertion_item(root, node, &item_key);
1500 if (!ret)
1501 goto end;
1502
1503 item = btrfs_alloc_delayed_item(0);
1504 if (!item) {
1505 ret = -ENOMEM;
1506 goto end;
1507 }
1508
1509 item->key = item_key;
1510
1511 ret = btrfs_delayed_item_reserve_metadata(trans, root, item);
1512 /*
1513 * we have reserved enough space when we start a new transaction,
1514 * so reserving metadata failure is impossible.
1515 */
1516 BUG_ON(ret);
1517
1518 mutex_lock(&node->mutex);
1519 ret = __btrfs_add_delayed_deletion_item(node, item);
1520 if (unlikely(ret)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001521 btrfs_err(root->fs_info, "err add delayed dir index item(index: %llu) "
Miao Xie16cdcec2011-04-22 18:12:22 +08001522 "into the deletion tree of the delayed node"
Frank Holtonefe120a2013-12-20 11:37:06 -05001523 "(root id: %llu, inode id: %llu, errno: %d)",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001524 index, node->root->objectid, node->inode_id,
Miao Xie16cdcec2011-04-22 18:12:22 +08001525 ret);
1526 BUG();
1527 }
1528 mutex_unlock(&node->mutex);
1529end:
1530 btrfs_release_delayed_node(node);
1531 return ret;
1532}
1533
1534int btrfs_inode_delayed_dir_index_count(struct inode *inode)
1535{
Miao Xie2f7e33d2011-06-23 07:27:13 +00001536 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08001537
1538 if (!delayed_node)
1539 return -ENOENT;
1540
1541 /*
1542 * Since we have held i_mutex of this directory, it is impossible that
1543 * a new directory index is added into the delayed node and index_cnt
1544 * is updated now. So we needn't lock the delayed node.
1545 */
Miao Xie2f7e33d2011-06-23 07:27:13 +00001546 if (!delayed_node->index_cnt) {
1547 btrfs_release_delayed_node(delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001548 return -EINVAL;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001549 }
Miao Xie16cdcec2011-04-22 18:12:22 +08001550
1551 BTRFS_I(inode)->index_cnt = delayed_node->index_cnt;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001552 btrfs_release_delayed_node(delayed_node);
1553 return 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001554}
1555
1556void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list,
1557 struct list_head *del_list)
1558{
1559 struct btrfs_delayed_node *delayed_node;
1560 struct btrfs_delayed_item *item;
1561
1562 delayed_node = btrfs_get_delayed_node(inode);
1563 if (!delayed_node)
1564 return;
1565
1566 mutex_lock(&delayed_node->mutex);
1567 item = __btrfs_first_delayed_insertion_item(delayed_node);
1568 while (item) {
1569 atomic_inc(&item->refs);
1570 list_add_tail(&item->readdir_list, ins_list);
1571 item = __btrfs_next_delayed_item(item);
1572 }
1573
1574 item = __btrfs_first_delayed_deletion_item(delayed_node);
1575 while (item) {
1576 atomic_inc(&item->refs);
1577 list_add_tail(&item->readdir_list, del_list);
1578 item = __btrfs_next_delayed_item(item);
1579 }
1580 mutex_unlock(&delayed_node->mutex);
1581 /*
1582 * This delayed node is still cached in the btrfs inode, so refs
1583 * must be > 1 now, and we needn't check it is going to be freed
1584 * or not.
1585 *
1586 * Besides that, this function is used to read dir, we do not
1587 * insert/delete delayed items in this period. So we also needn't
1588 * requeue or dequeue this delayed node.
1589 */
1590 atomic_dec(&delayed_node->refs);
1591}
1592
1593void btrfs_put_delayed_items(struct list_head *ins_list,
1594 struct list_head *del_list)
1595{
1596 struct btrfs_delayed_item *curr, *next;
1597
1598 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1599 list_del(&curr->readdir_list);
1600 if (atomic_dec_and_test(&curr->refs))
1601 kfree(curr);
1602 }
1603
1604 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1605 list_del(&curr->readdir_list);
1606 if (atomic_dec_and_test(&curr->refs))
1607 kfree(curr);
1608 }
1609}
1610
1611int btrfs_should_delete_dir_index(struct list_head *del_list,
1612 u64 index)
1613{
1614 struct btrfs_delayed_item *curr, *next;
1615 int ret;
1616
1617 if (list_empty(del_list))
1618 return 0;
1619
1620 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1621 if (curr->key.offset > index)
1622 break;
1623
1624 list_del(&curr->readdir_list);
1625 ret = (curr->key.offset == index);
1626
1627 if (atomic_dec_and_test(&curr->refs))
1628 kfree(curr);
1629
1630 if (ret)
1631 return 1;
1632 else
1633 continue;
1634 }
1635 return 0;
1636}
1637
1638/*
1639 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1640 *
1641 */
Al Viro9cdda8d2013-05-22 16:48:09 -04001642int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
Miao Xie16cdcec2011-04-22 18:12:22 +08001643 struct list_head *ins_list)
1644{
1645 struct btrfs_dir_item *di;
1646 struct btrfs_delayed_item *curr, *next;
1647 struct btrfs_key location;
1648 char *name;
1649 int name_len;
1650 int over = 0;
1651 unsigned char d_type;
1652
1653 if (list_empty(ins_list))
1654 return 0;
1655
1656 /*
1657 * Changing the data of the delayed item is impossible. So
1658 * we needn't lock them. And we have held i_mutex of the
1659 * directory, nobody can delete any directory indexes now.
1660 */
1661 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1662 list_del(&curr->readdir_list);
1663
Al Viro9cdda8d2013-05-22 16:48:09 -04001664 if (curr->key.offset < ctx->pos) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001665 if (atomic_dec_and_test(&curr->refs))
1666 kfree(curr);
1667 continue;
1668 }
1669
Al Viro9cdda8d2013-05-22 16:48:09 -04001670 ctx->pos = curr->key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08001671
1672 di = (struct btrfs_dir_item *)curr->data;
1673 name = (char *)(di + 1);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001674 name_len = btrfs_stack_dir_name_len(di);
Miao Xie16cdcec2011-04-22 18:12:22 +08001675
1676 d_type = btrfs_filetype_table[di->type];
1677 btrfs_disk_key_to_cpu(&location, &di->location);
1678
Al Viro9cdda8d2013-05-22 16:48:09 -04001679 over = !dir_emit(ctx, name, name_len,
Miao Xie16cdcec2011-04-22 18:12:22 +08001680 location.objectid, d_type);
1681
1682 if (atomic_dec_and_test(&curr->refs))
1683 kfree(curr);
1684
1685 if (over)
1686 return 1;
1687 }
1688 return 0;
1689}
1690
Miao Xie16cdcec2011-04-22 18:12:22 +08001691static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1692 struct btrfs_inode_item *inode_item,
1693 struct inode *inode)
1694{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08001695 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1696 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08001697 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1698 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1699 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1700 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1701 btrfs_set_stack_inode_generation(inode_item,
1702 BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04001703 btrfs_set_stack_inode_sequence(inode_item, inode->i_version);
Miao Xie16cdcec2011-04-22 18:12:22 +08001704 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1705 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1706 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags);
Chris Masonff5714c2011-05-28 07:00:39 -04001707 btrfs_set_stack_inode_block_group(inode_item, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001708
1709 btrfs_set_stack_timespec_sec(btrfs_inode_atime(inode_item),
1710 inode->i_atime.tv_sec);
1711 btrfs_set_stack_timespec_nsec(btrfs_inode_atime(inode_item),
1712 inode->i_atime.tv_nsec);
1713
1714 btrfs_set_stack_timespec_sec(btrfs_inode_mtime(inode_item),
1715 inode->i_mtime.tv_sec);
1716 btrfs_set_stack_timespec_nsec(btrfs_inode_mtime(inode_item),
1717 inode->i_mtime.tv_nsec);
1718
1719 btrfs_set_stack_timespec_sec(btrfs_inode_ctime(inode_item),
1720 inode->i_ctime.tv_sec);
1721 btrfs_set_stack_timespec_nsec(btrfs_inode_ctime(inode_item),
1722 inode->i_ctime.tv_nsec);
1723}
1724
Miao Xie2f7e33d2011-06-23 07:27:13 +00001725int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1726{
1727 struct btrfs_delayed_node *delayed_node;
1728 struct btrfs_inode_item *inode_item;
1729 struct btrfs_timespec *tspec;
1730
1731 delayed_node = btrfs_get_delayed_node(inode);
1732 if (!delayed_node)
1733 return -ENOENT;
1734
1735 mutex_lock(&delayed_node->mutex);
1736 if (!delayed_node->inode_dirty) {
1737 mutex_unlock(&delayed_node->mutex);
1738 btrfs_release_delayed_node(delayed_node);
1739 return -ENOENT;
1740 }
1741
1742 inode_item = &delayed_node->inode_item;
1743
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08001744 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1745 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001746 btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
1747 inode->i_mode = btrfs_stack_inode_mode(inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001748 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001749 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1750 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04001751 inode->i_version = btrfs_stack_inode_sequence(inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00001752 inode->i_rdev = 0;
1753 *rdev = btrfs_stack_inode_rdev(inode_item);
1754 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1755
1756 tspec = btrfs_inode_atime(inode_item);
1757 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(tspec);
1758 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1759
1760 tspec = btrfs_inode_mtime(inode_item);
1761 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(tspec);
1762 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1763
1764 tspec = btrfs_inode_ctime(inode_item);
1765 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(tspec);
1766 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1767
1768 inode->i_generation = BTRFS_I(inode)->generation;
1769 BTRFS_I(inode)->index_cnt = (u64)-1;
1770
1771 mutex_unlock(&delayed_node->mutex);
1772 btrfs_release_delayed_node(delayed_node);
1773 return 0;
1774}
1775
Miao Xie16cdcec2011-04-22 18:12:22 +08001776int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1777 struct btrfs_root *root, struct inode *inode)
1778{
1779 struct btrfs_delayed_node *delayed_node;
David Sterbaaa0467d2011-06-03 16:29:08 +02001780 int ret = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001781
1782 delayed_node = btrfs_get_or_create_delayed_node(inode);
1783 if (IS_ERR(delayed_node))
1784 return PTR_ERR(delayed_node);
1785
1786 mutex_lock(&delayed_node->mutex);
1787 if (delayed_node->inode_dirty) {
1788 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1789 goto release_node;
1790 }
1791
Josef Bacik7fd2ae22011-11-08 15:47:34 -05001792 ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode,
1793 delayed_node);
Josef Bacikc06a0e12011-11-04 19:56:02 -04001794 if (ret)
1795 goto release_node;
Miao Xie16cdcec2011-04-22 18:12:22 +08001796
1797 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1798 delayed_node->inode_dirty = 1;
1799 delayed_node->count++;
1800 atomic_inc(&root->fs_info->delayed_root->items);
1801release_node:
1802 mutex_unlock(&delayed_node->mutex);
1803 btrfs_release_delayed_node(delayed_node);
1804 return ret;
1805}
1806
1807static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1808{
1809 struct btrfs_root *root = delayed_node->root;
1810 struct btrfs_delayed_item *curr_item, *prev_item;
1811
1812 mutex_lock(&delayed_node->mutex);
1813 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1814 while (curr_item) {
1815 btrfs_delayed_item_release_metadata(root, curr_item);
1816 prev_item = curr_item;
1817 curr_item = __btrfs_next_delayed_item(prev_item);
1818 btrfs_release_delayed_item(prev_item);
1819 }
1820
1821 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1822 while (curr_item) {
1823 btrfs_delayed_item_release_metadata(root, curr_item);
1824 prev_item = curr_item;
1825 curr_item = __btrfs_next_delayed_item(prev_item);
1826 btrfs_release_delayed_item(prev_item);
1827 }
1828
1829 if (delayed_node->inode_dirty) {
1830 btrfs_delayed_inode_release_metadata(root, delayed_node);
1831 btrfs_release_delayed_inode(delayed_node);
1832 }
1833 mutex_unlock(&delayed_node->mutex);
1834}
1835
1836void btrfs_kill_delayed_inode_items(struct inode *inode)
1837{
1838 struct btrfs_delayed_node *delayed_node;
1839
1840 delayed_node = btrfs_get_delayed_node(inode);
1841 if (!delayed_node)
1842 return;
1843
1844 __btrfs_kill_delayed_node(delayed_node);
1845 btrfs_release_delayed_node(delayed_node);
1846}
1847
1848void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1849{
1850 u64 inode_id = 0;
1851 struct btrfs_delayed_node *delayed_nodes[8];
1852 int i, n;
1853
1854 while (1) {
1855 spin_lock(&root->inode_lock);
1856 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1857 (void **)delayed_nodes, inode_id,
1858 ARRAY_SIZE(delayed_nodes));
1859 if (!n) {
1860 spin_unlock(&root->inode_lock);
1861 break;
1862 }
1863
1864 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1865
1866 for (i = 0; i < n; i++)
1867 atomic_inc(&delayed_nodes[i]->refs);
1868 spin_unlock(&root->inode_lock);
1869
1870 for (i = 0; i < n; i++) {
1871 __btrfs_kill_delayed_node(delayed_nodes[i]);
1872 btrfs_release_delayed_node(delayed_nodes[i]);
1873 }
1874 }
1875}
Miao Xie67cde342012-06-14 02:23:22 -06001876
1877void btrfs_destroy_delayed_inodes(struct btrfs_root *root)
1878{
1879 struct btrfs_delayed_root *delayed_root;
1880 struct btrfs_delayed_node *curr_node, *prev_node;
1881
1882 delayed_root = btrfs_get_delayed_root(root);
1883
1884 curr_node = btrfs_first_delayed_node(delayed_root);
1885 while (curr_node) {
1886 __btrfs_kill_delayed_node(curr_node);
1887
1888 prev_node = curr_node;
1889 curr_node = btrfs_next_delayed_node(curr_node);
1890 btrfs_release_delayed_node(prev_node);
1891 }
1892}
1893