blob: 8d292fbae659eff6a65bd7a8c255d741d998635e [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 /*
1304 * Maybe new delayed items have been inserted, so we need requeue
1305 * the work. Besides that, we must dequeue the empty delayed nodes
1306 * to avoid the race between delayed items balance and the worker.
1307 * The race like this:
1308 * Task1 Worker thread
1309 * count == 0, needn't requeue
1310 * also needn't insert the
1311 * delayed node into prepare
1312 * list again.
1313 * add lots of delayed items
1314 * queue the delayed node
1315 * already in the list,
1316 * and not in the prepare
1317 * list, it means the delayed
1318 * node is being dealt with
1319 * by the worker.
1320 * do delayed items balance
1321 * the delayed node is being
1322 * dealt with by the worker
1323 * now, just wait.
1324 * the worker goto idle.
1325 * Task1 will sleep until the transaction is commited.
1326 */
1327 mutex_lock(&delayed_node->mutex);
Chris Masonde3cb942013-03-04 17:13:31 -05001328 btrfs_dequeue_delayed_node(root->fs_info->delayed_root, delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001329 mutex_unlock(&delayed_node->mutex);
1330
Miao Xie19fd2942011-06-15 10:47:30 +00001331 trans->block_rsv = block_rsv;
Miao Xie16cdcec2011-04-22 18:12:22 +08001332 btrfs_end_transaction_dmeta(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00001333 btrfs_btree_balance_dirty_nodelay(root);
Chris Masonde3cb942013-03-04 17:13:31 -05001334
1335release_path:
1336 btrfs_release_path(path);
1337 total_done++;
1338
1339 btrfs_release_prepared_delayed_node(delayed_node);
1340 if (async_work->nr == 0 || total_done < async_work->nr)
1341 goto again;
1342
Miao Xie16cdcec2011-04-22 18:12:22 +08001343free_path:
1344 btrfs_free_path(path);
1345out:
Chris Masonde3cb942013-03-04 17:13:31 -05001346 wake_up(&delayed_root->wait);
1347 kfree(async_work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001348}
1349
Miao Xie16cdcec2011-04-22 18:12:22 +08001350
Chris Masonde3cb942013-03-04 17:13:31 -05001351static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
1352 struct btrfs_root *root, int nr)
1353{
1354 struct btrfs_async_delayed_work *async_work;
1355
1356 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
Miao Xie16cdcec2011-04-22 18:12:22 +08001357 return 0;
1358
Chris Masonde3cb942013-03-04 17:13:31 -05001359 async_work = kmalloc(sizeof(*async_work), GFP_NOFS);
1360 if (!async_work)
Miao Xie16cdcec2011-04-22 18:12:22 +08001361 return -ENOMEM;
Miao Xie16cdcec2011-04-22 18:12:22 +08001362
Chris Masonde3cb942013-03-04 17:13:31 -05001363 async_work->delayed_root = delayed_root;
1364 async_work->work.func = btrfs_async_run_delayed_root;
1365 async_work->work.flags = 0;
1366 async_work->nr = nr;
Miao Xie16cdcec2011-04-22 18:12:22 +08001367
Chris Masonde3cb942013-03-04 17:13:31 -05001368 btrfs_queue_worker(&root->fs_info->delayed_workers, &async_work->work);
Miao Xie16cdcec2011-04-22 18:12:22 +08001369 return 0;
1370}
1371
Chris Masone9993762011-06-17 16:14:09 -04001372void btrfs_assert_delayed_root_empty(struct btrfs_root *root)
1373{
1374 struct btrfs_delayed_root *delayed_root;
1375 delayed_root = btrfs_get_delayed_root(root);
1376 WARN_ON(btrfs_first_delayed_node(delayed_root));
1377}
1378
Chris Masonde3cb942013-03-04 17:13:31 -05001379static int refs_newer(struct btrfs_delayed_root *delayed_root,
1380 int seq, int count)
1381{
1382 int val = atomic_read(&delayed_root->items_seq);
1383
1384 if (val < seq || val >= seq + count)
1385 return 1;
1386 return 0;
1387}
1388
Miao Xie16cdcec2011-04-22 18:12:22 +08001389void btrfs_balance_delayed_items(struct btrfs_root *root)
1390{
1391 struct btrfs_delayed_root *delayed_root;
Chris Masonde3cb942013-03-04 17:13:31 -05001392 int seq;
Miao Xie16cdcec2011-04-22 18:12:22 +08001393
1394 delayed_root = btrfs_get_delayed_root(root);
1395
1396 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1397 return;
1398
Chris Masonde3cb942013-03-04 17:13:31 -05001399 seq = atomic_read(&delayed_root->items_seq);
1400
Miao Xie16cdcec2011-04-22 18:12:22 +08001401 if (atomic_read(&delayed_root->items) >= BTRFS_DELAYED_WRITEBACK) {
1402 int ret;
Chris Masonde3cb942013-03-04 17:13:31 -05001403 DEFINE_WAIT(__wait);
1404
1405 ret = btrfs_wq_run_delayed_node(delayed_root, root, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001406 if (ret)
1407 return;
1408
Chris Masonde3cb942013-03-04 17:13:31 -05001409 while (1) {
1410 prepare_to_wait(&delayed_root->wait, &__wait,
1411 TASK_INTERRUPTIBLE);
1412
1413 if (refs_newer(delayed_root, seq,
1414 BTRFS_DELAYED_BATCH) ||
1415 atomic_read(&delayed_root->items) <
1416 BTRFS_DELAYED_BACKGROUND) {
1417 break;
1418 }
1419 if (!signal_pending(current))
1420 schedule();
1421 else
1422 break;
1423 }
1424 finish_wait(&delayed_root->wait, &__wait);
Miao Xie16cdcec2011-04-22 18:12:22 +08001425 }
1426
Chris Masonde3cb942013-03-04 17:13:31 -05001427 btrfs_wq_run_delayed_node(delayed_root, root, BTRFS_DELAYED_BATCH);
Miao Xie16cdcec2011-04-22 18:12:22 +08001428}
1429
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001430/* Will return 0 or -ENOMEM */
Miao Xie16cdcec2011-04-22 18:12:22 +08001431int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1432 struct btrfs_root *root, const char *name,
1433 int name_len, struct inode *dir,
1434 struct btrfs_disk_key *disk_key, u8 type,
1435 u64 index)
1436{
1437 struct btrfs_delayed_node *delayed_node;
1438 struct btrfs_delayed_item *delayed_item;
1439 struct btrfs_dir_item *dir_item;
1440 int ret;
1441
1442 delayed_node = btrfs_get_or_create_delayed_node(dir);
1443 if (IS_ERR(delayed_node))
1444 return PTR_ERR(delayed_node);
1445
1446 delayed_item = btrfs_alloc_delayed_item(sizeof(*dir_item) + name_len);
1447 if (!delayed_item) {
1448 ret = -ENOMEM;
1449 goto release_node;
1450 }
1451
Chris Mason0d0ca302011-05-22 07:11:22 -04001452 delayed_item->key.objectid = btrfs_ino(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08001453 btrfs_set_key_type(&delayed_item->key, BTRFS_DIR_INDEX_KEY);
1454 delayed_item->key.offset = index;
1455
1456 dir_item = (struct btrfs_dir_item *)delayed_item->data;
1457 dir_item->location = *disk_key;
Qu Wenruo3cae2102013-07-16 11:19:18 +08001458 btrfs_set_stack_dir_transid(dir_item, trans->transid);
1459 btrfs_set_stack_dir_data_len(dir_item, 0);
1460 btrfs_set_stack_dir_name_len(dir_item, name_len);
1461 btrfs_set_stack_dir_type(dir_item, type);
Miao Xie16cdcec2011-04-22 18:12:22 +08001462 memcpy((char *)(dir_item + 1), name, name_len);
1463
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05001464 ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item);
1465 /*
1466 * we have reserved enough space when we start a new transaction,
1467 * so reserving metadata failure is impossible
1468 */
1469 BUG_ON(ret);
1470
1471
Miao Xie16cdcec2011-04-22 18:12:22 +08001472 mutex_lock(&delayed_node->mutex);
1473 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1474 if (unlikely(ret)) {
Filipe David Borba Mananabdab49d2013-08-20 17:51:48 +01001475 printk(KERN_ERR "err add delayed dir index item(name: %.*s) "
1476 "into the insertion tree of the delayed node"
Miao Xie16cdcec2011-04-22 18:12:22 +08001477 "(root id: %llu, inode id: %llu, errno: %d)\n",
Filipe David Borba Mananabdab49d2013-08-20 17:51:48 +01001478 name_len, name, delayed_node->root->objectid,
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001479 delayed_node->inode_id, ret);
Miao Xie16cdcec2011-04-22 18:12:22 +08001480 BUG();
1481 }
1482 mutex_unlock(&delayed_node->mutex);
1483
1484release_node:
1485 btrfs_release_delayed_node(delayed_node);
1486 return ret;
1487}
1488
1489static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root,
1490 struct btrfs_delayed_node *node,
1491 struct btrfs_key *key)
1492{
1493 struct btrfs_delayed_item *item;
1494
1495 mutex_lock(&node->mutex);
1496 item = __btrfs_lookup_delayed_insertion_item(node, key);
1497 if (!item) {
1498 mutex_unlock(&node->mutex);
1499 return 1;
1500 }
1501
1502 btrfs_delayed_item_release_metadata(root, item);
1503 btrfs_release_delayed_item(item);
1504 mutex_unlock(&node->mutex);
1505 return 0;
1506}
1507
1508int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1509 struct btrfs_root *root, struct inode *dir,
1510 u64 index)
1511{
1512 struct btrfs_delayed_node *node;
1513 struct btrfs_delayed_item *item;
1514 struct btrfs_key item_key;
1515 int ret;
1516
1517 node = btrfs_get_or_create_delayed_node(dir);
1518 if (IS_ERR(node))
1519 return PTR_ERR(node);
1520
Chris Mason0d0ca302011-05-22 07:11:22 -04001521 item_key.objectid = btrfs_ino(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08001522 btrfs_set_key_type(&item_key, BTRFS_DIR_INDEX_KEY);
1523 item_key.offset = index;
1524
1525 ret = btrfs_delete_delayed_insertion_item(root, node, &item_key);
1526 if (!ret)
1527 goto end;
1528
1529 item = btrfs_alloc_delayed_item(0);
1530 if (!item) {
1531 ret = -ENOMEM;
1532 goto end;
1533 }
1534
1535 item->key = item_key;
1536
1537 ret = btrfs_delayed_item_reserve_metadata(trans, root, item);
1538 /*
1539 * we have reserved enough space when we start a new transaction,
1540 * so reserving metadata failure is impossible.
1541 */
1542 BUG_ON(ret);
1543
1544 mutex_lock(&node->mutex);
1545 ret = __btrfs_add_delayed_deletion_item(node, item);
1546 if (unlikely(ret)) {
1547 printk(KERN_ERR "err add delayed dir index item(index: %llu) "
1548 "into the deletion tree of the delayed node"
1549 "(root id: %llu, inode id: %llu, errno: %d)\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001550 index, node->root->objectid, node->inode_id,
Miao Xie16cdcec2011-04-22 18:12:22 +08001551 ret);
1552 BUG();
1553 }
1554 mutex_unlock(&node->mutex);
1555end:
1556 btrfs_release_delayed_node(node);
1557 return ret;
1558}
1559
1560int btrfs_inode_delayed_dir_index_count(struct inode *inode)
1561{
Miao Xie2f7e33d2011-06-23 07:27:13 +00001562 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08001563
1564 if (!delayed_node)
1565 return -ENOENT;
1566
1567 /*
1568 * Since we have held i_mutex of this directory, it is impossible that
1569 * a new directory index is added into the delayed node and index_cnt
1570 * is updated now. So we needn't lock the delayed node.
1571 */
Miao Xie2f7e33d2011-06-23 07:27:13 +00001572 if (!delayed_node->index_cnt) {
1573 btrfs_release_delayed_node(delayed_node);
Miao Xie16cdcec2011-04-22 18:12:22 +08001574 return -EINVAL;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001575 }
Miao Xie16cdcec2011-04-22 18:12:22 +08001576
1577 BTRFS_I(inode)->index_cnt = delayed_node->index_cnt;
Miao Xie2f7e33d2011-06-23 07:27:13 +00001578 btrfs_release_delayed_node(delayed_node);
1579 return 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001580}
1581
1582void btrfs_get_delayed_items(struct inode *inode, struct list_head *ins_list,
1583 struct list_head *del_list)
1584{
1585 struct btrfs_delayed_node *delayed_node;
1586 struct btrfs_delayed_item *item;
1587
1588 delayed_node = btrfs_get_delayed_node(inode);
1589 if (!delayed_node)
1590 return;
1591
1592 mutex_lock(&delayed_node->mutex);
1593 item = __btrfs_first_delayed_insertion_item(delayed_node);
1594 while (item) {
1595 atomic_inc(&item->refs);
1596 list_add_tail(&item->readdir_list, ins_list);
1597 item = __btrfs_next_delayed_item(item);
1598 }
1599
1600 item = __btrfs_first_delayed_deletion_item(delayed_node);
1601 while (item) {
1602 atomic_inc(&item->refs);
1603 list_add_tail(&item->readdir_list, del_list);
1604 item = __btrfs_next_delayed_item(item);
1605 }
1606 mutex_unlock(&delayed_node->mutex);
1607 /*
1608 * This delayed node is still cached in the btrfs inode, so refs
1609 * must be > 1 now, and we needn't check it is going to be freed
1610 * or not.
1611 *
1612 * Besides that, this function is used to read dir, we do not
1613 * insert/delete delayed items in this period. So we also needn't
1614 * requeue or dequeue this delayed node.
1615 */
1616 atomic_dec(&delayed_node->refs);
1617}
1618
1619void btrfs_put_delayed_items(struct list_head *ins_list,
1620 struct list_head *del_list)
1621{
1622 struct btrfs_delayed_item *curr, *next;
1623
1624 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1625 list_del(&curr->readdir_list);
1626 if (atomic_dec_and_test(&curr->refs))
1627 kfree(curr);
1628 }
1629
1630 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1631 list_del(&curr->readdir_list);
1632 if (atomic_dec_and_test(&curr->refs))
1633 kfree(curr);
1634 }
1635}
1636
1637int btrfs_should_delete_dir_index(struct list_head *del_list,
1638 u64 index)
1639{
1640 struct btrfs_delayed_item *curr, *next;
1641 int ret;
1642
1643 if (list_empty(del_list))
1644 return 0;
1645
1646 list_for_each_entry_safe(curr, next, del_list, readdir_list) {
1647 if (curr->key.offset > index)
1648 break;
1649
1650 list_del(&curr->readdir_list);
1651 ret = (curr->key.offset == index);
1652
1653 if (atomic_dec_and_test(&curr->refs))
1654 kfree(curr);
1655
1656 if (ret)
1657 return 1;
1658 else
1659 continue;
1660 }
1661 return 0;
1662}
1663
1664/*
1665 * btrfs_readdir_delayed_dir_index - read dir info stored in the delayed tree
1666 *
1667 */
Al Viro9cdda8d2013-05-22 16:48:09 -04001668int btrfs_readdir_delayed_dir_index(struct dir_context *ctx,
Miao Xie16cdcec2011-04-22 18:12:22 +08001669 struct list_head *ins_list)
1670{
1671 struct btrfs_dir_item *di;
1672 struct btrfs_delayed_item *curr, *next;
1673 struct btrfs_key location;
1674 char *name;
1675 int name_len;
1676 int over = 0;
1677 unsigned char d_type;
1678
1679 if (list_empty(ins_list))
1680 return 0;
1681
1682 /*
1683 * Changing the data of the delayed item is impossible. So
1684 * we needn't lock them. And we have held i_mutex of the
1685 * directory, nobody can delete any directory indexes now.
1686 */
1687 list_for_each_entry_safe(curr, next, ins_list, readdir_list) {
1688 list_del(&curr->readdir_list);
1689
Al Viro9cdda8d2013-05-22 16:48:09 -04001690 if (curr->key.offset < ctx->pos) {
Miao Xie16cdcec2011-04-22 18:12:22 +08001691 if (atomic_dec_and_test(&curr->refs))
1692 kfree(curr);
1693 continue;
1694 }
1695
Al Viro9cdda8d2013-05-22 16:48:09 -04001696 ctx->pos = curr->key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08001697
1698 di = (struct btrfs_dir_item *)curr->data;
1699 name = (char *)(di + 1);
Qu Wenruo3cae2102013-07-16 11:19:18 +08001700 name_len = btrfs_stack_dir_name_len(di);
Miao Xie16cdcec2011-04-22 18:12:22 +08001701
1702 d_type = btrfs_filetype_table[di->type];
1703 btrfs_disk_key_to_cpu(&location, &di->location);
1704
Al Viro9cdda8d2013-05-22 16:48:09 -04001705 over = !dir_emit(ctx, name, name_len,
Miao Xie16cdcec2011-04-22 18:12:22 +08001706 location.objectid, d_type);
1707
1708 if (atomic_dec_and_test(&curr->refs))
1709 kfree(curr);
1710
1711 if (over)
1712 return 1;
1713 }
1714 return 0;
1715}
1716
Miao Xie16cdcec2011-04-22 18:12:22 +08001717static void fill_stack_inode_item(struct btrfs_trans_handle *trans,
1718 struct btrfs_inode_item *inode_item,
1719 struct inode *inode)
1720{
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08001721 btrfs_set_stack_inode_uid(inode_item, i_uid_read(inode));
1722 btrfs_set_stack_inode_gid(inode_item, i_gid_read(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08001723 btrfs_set_stack_inode_size(inode_item, BTRFS_I(inode)->disk_i_size);
1724 btrfs_set_stack_inode_mode(inode_item, inode->i_mode);
1725 btrfs_set_stack_inode_nlink(inode_item, inode->i_nlink);
1726 btrfs_set_stack_inode_nbytes(inode_item, inode_get_bytes(inode));
1727 btrfs_set_stack_inode_generation(inode_item,
1728 BTRFS_I(inode)->generation);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04001729 btrfs_set_stack_inode_sequence(inode_item, inode->i_version);
Miao Xie16cdcec2011-04-22 18:12:22 +08001730 btrfs_set_stack_inode_transid(inode_item, trans->transid);
1731 btrfs_set_stack_inode_rdev(inode_item, inode->i_rdev);
1732 btrfs_set_stack_inode_flags(inode_item, BTRFS_I(inode)->flags);
Chris Masonff5714c2011-05-28 07:00:39 -04001733 btrfs_set_stack_inode_block_group(inode_item, 0);
Miao Xie16cdcec2011-04-22 18:12:22 +08001734
1735 btrfs_set_stack_timespec_sec(btrfs_inode_atime(inode_item),
1736 inode->i_atime.tv_sec);
1737 btrfs_set_stack_timespec_nsec(btrfs_inode_atime(inode_item),
1738 inode->i_atime.tv_nsec);
1739
1740 btrfs_set_stack_timespec_sec(btrfs_inode_mtime(inode_item),
1741 inode->i_mtime.tv_sec);
1742 btrfs_set_stack_timespec_nsec(btrfs_inode_mtime(inode_item),
1743 inode->i_mtime.tv_nsec);
1744
1745 btrfs_set_stack_timespec_sec(btrfs_inode_ctime(inode_item),
1746 inode->i_ctime.tv_sec);
1747 btrfs_set_stack_timespec_nsec(btrfs_inode_ctime(inode_item),
1748 inode->i_ctime.tv_nsec);
1749}
1750
Miao Xie2f7e33d2011-06-23 07:27:13 +00001751int btrfs_fill_inode(struct inode *inode, u32 *rdev)
1752{
1753 struct btrfs_delayed_node *delayed_node;
1754 struct btrfs_inode_item *inode_item;
1755 struct btrfs_timespec *tspec;
1756
1757 delayed_node = btrfs_get_delayed_node(inode);
1758 if (!delayed_node)
1759 return -ENOENT;
1760
1761 mutex_lock(&delayed_node->mutex);
1762 if (!delayed_node->inode_dirty) {
1763 mutex_unlock(&delayed_node->mutex);
1764 btrfs_release_delayed_node(delayed_node);
1765 return -ENOENT;
1766 }
1767
1768 inode_item = &delayed_node->inode_item;
1769
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08001770 i_uid_write(inode, btrfs_stack_inode_uid(inode_item));
1771 i_gid_write(inode, btrfs_stack_inode_gid(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001772 btrfs_i_size_write(inode, btrfs_stack_inode_size(inode_item));
1773 inode->i_mode = btrfs_stack_inode_mode(inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02001774 set_nlink(inode, btrfs_stack_inode_nlink(inode_item));
Miao Xie2f7e33d2011-06-23 07:27:13 +00001775 inode_set_bytes(inode, btrfs_stack_inode_nbytes(inode_item));
1776 BTRFS_I(inode)->generation = btrfs_stack_inode_generation(inode_item);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04001777 inode->i_version = btrfs_stack_inode_sequence(inode_item);
Miao Xie2f7e33d2011-06-23 07:27:13 +00001778 inode->i_rdev = 0;
1779 *rdev = btrfs_stack_inode_rdev(inode_item);
1780 BTRFS_I(inode)->flags = btrfs_stack_inode_flags(inode_item);
1781
1782 tspec = btrfs_inode_atime(inode_item);
1783 inode->i_atime.tv_sec = btrfs_stack_timespec_sec(tspec);
1784 inode->i_atime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1785
1786 tspec = btrfs_inode_mtime(inode_item);
1787 inode->i_mtime.tv_sec = btrfs_stack_timespec_sec(tspec);
1788 inode->i_mtime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1789
1790 tspec = btrfs_inode_ctime(inode_item);
1791 inode->i_ctime.tv_sec = btrfs_stack_timespec_sec(tspec);
1792 inode->i_ctime.tv_nsec = btrfs_stack_timespec_nsec(tspec);
1793
1794 inode->i_generation = BTRFS_I(inode)->generation;
1795 BTRFS_I(inode)->index_cnt = (u64)-1;
1796
1797 mutex_unlock(&delayed_node->mutex);
1798 btrfs_release_delayed_node(delayed_node);
1799 return 0;
1800}
1801
Miao Xie16cdcec2011-04-22 18:12:22 +08001802int btrfs_delayed_update_inode(struct btrfs_trans_handle *trans,
1803 struct btrfs_root *root, struct inode *inode)
1804{
1805 struct btrfs_delayed_node *delayed_node;
David Sterbaaa0467d2011-06-03 16:29:08 +02001806 int ret = 0;
Miao Xie16cdcec2011-04-22 18:12:22 +08001807
1808 delayed_node = btrfs_get_or_create_delayed_node(inode);
1809 if (IS_ERR(delayed_node))
1810 return PTR_ERR(delayed_node);
1811
1812 mutex_lock(&delayed_node->mutex);
1813 if (delayed_node->inode_dirty) {
1814 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1815 goto release_node;
1816 }
1817
Josef Bacik7fd2ae22011-11-08 15:47:34 -05001818 ret = btrfs_delayed_inode_reserve_metadata(trans, root, inode,
1819 delayed_node);
Josef Bacikc06a0e12011-11-04 19:56:02 -04001820 if (ret)
1821 goto release_node;
Miao Xie16cdcec2011-04-22 18:12:22 +08001822
1823 fill_stack_inode_item(trans, &delayed_node->inode_item, inode);
1824 delayed_node->inode_dirty = 1;
1825 delayed_node->count++;
1826 atomic_inc(&root->fs_info->delayed_root->items);
1827release_node:
1828 mutex_unlock(&delayed_node->mutex);
1829 btrfs_release_delayed_node(delayed_node);
1830 return ret;
1831}
1832
1833static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1834{
1835 struct btrfs_root *root = delayed_node->root;
1836 struct btrfs_delayed_item *curr_item, *prev_item;
1837
1838 mutex_lock(&delayed_node->mutex);
1839 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1840 while (curr_item) {
1841 btrfs_delayed_item_release_metadata(root, curr_item);
1842 prev_item = curr_item;
1843 curr_item = __btrfs_next_delayed_item(prev_item);
1844 btrfs_release_delayed_item(prev_item);
1845 }
1846
1847 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1848 while (curr_item) {
1849 btrfs_delayed_item_release_metadata(root, curr_item);
1850 prev_item = curr_item;
1851 curr_item = __btrfs_next_delayed_item(prev_item);
1852 btrfs_release_delayed_item(prev_item);
1853 }
1854
1855 if (delayed_node->inode_dirty) {
1856 btrfs_delayed_inode_release_metadata(root, delayed_node);
1857 btrfs_release_delayed_inode(delayed_node);
1858 }
1859 mutex_unlock(&delayed_node->mutex);
1860}
1861
1862void btrfs_kill_delayed_inode_items(struct inode *inode)
1863{
1864 struct btrfs_delayed_node *delayed_node;
1865
1866 delayed_node = btrfs_get_delayed_node(inode);
1867 if (!delayed_node)
1868 return;
1869
1870 __btrfs_kill_delayed_node(delayed_node);
1871 btrfs_release_delayed_node(delayed_node);
1872}
1873
1874void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
1875{
1876 u64 inode_id = 0;
1877 struct btrfs_delayed_node *delayed_nodes[8];
1878 int i, n;
1879
1880 while (1) {
1881 spin_lock(&root->inode_lock);
1882 n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
1883 (void **)delayed_nodes, inode_id,
1884 ARRAY_SIZE(delayed_nodes));
1885 if (!n) {
1886 spin_unlock(&root->inode_lock);
1887 break;
1888 }
1889
1890 inode_id = delayed_nodes[n - 1]->inode_id + 1;
1891
1892 for (i = 0; i < n; i++)
1893 atomic_inc(&delayed_nodes[i]->refs);
1894 spin_unlock(&root->inode_lock);
1895
1896 for (i = 0; i < n; i++) {
1897 __btrfs_kill_delayed_node(delayed_nodes[i]);
1898 btrfs_release_delayed_node(delayed_nodes[i]);
1899 }
1900 }
1901}
Miao Xie67cde342012-06-14 02:23:22 -06001902
1903void btrfs_destroy_delayed_inodes(struct btrfs_root *root)
1904{
1905 struct btrfs_delayed_root *delayed_root;
1906 struct btrfs_delayed_node *curr_node, *prev_node;
1907
1908 delayed_root = btrfs_get_delayed_root(root);
1909
1910 curr_node = btrfs_first_delayed_node(delayed_root);
1911 while (curr_node) {
1912 __btrfs_kill_delayed_node(curr_node);
1913
1914 prev_node = curr_node;
1915 curr_node = btrfs_next_delayed_node(curr_node);
1916 btrfs_release_delayed_node(prev_node);
1917 }
1918}
1919