forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delayed-inode.c
2001 lines (1675 loc) · 52 KB
/
delayed-inode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 Fujitsu. All rights reserved.
* Written by Miao Xie <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include <linux/slab.h>
#include "delayed-inode.h"
#include "disk-io.h"
#include "transaction.h"
#include "ctree.h"
#define BTRFS_DELAYED_WRITEBACK 512
#define BTRFS_DELAYED_BACKGROUND 128
#define BTRFS_DELAYED_BATCH 16
static struct kmem_cache *delayed_node_cache;
int __init btrfs_delayed_inode_init(void)
{
delayed_node_cache = kmem_cache_create("btrfs_delayed_node",
sizeof(struct btrfs_delayed_node),
0,
SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
NULL);
if (!delayed_node_cache)
return -ENOMEM;
return 0;
}
void btrfs_delayed_inode_exit(void)
{
kmem_cache_destroy(delayed_node_cache);
}
static inline void btrfs_init_delayed_node(
struct btrfs_delayed_node *delayed_node,
struct btrfs_root *root, u64 inode_id)
{
delayed_node->root = root;
delayed_node->inode_id = inode_id;
atomic_set(&delayed_node->refs, 0);
delayed_node->ins_root = RB_ROOT;
delayed_node->del_root = RB_ROOT;
mutex_init(&delayed_node->mutex);
INIT_LIST_HEAD(&delayed_node->n_list);
INIT_LIST_HEAD(&delayed_node->p_list);
}
static inline int btrfs_is_continuous_delayed_item(
struct btrfs_delayed_item *item1,
struct btrfs_delayed_item *item2)
{
if (item1->key.type == BTRFS_DIR_INDEX_KEY &&
item1->key.objectid == item2->key.objectid &&
item1->key.type == item2->key.type &&
item1->key.offset + 1 == item2->key.offset)
return 1;
return 0;
}
static inline struct btrfs_delayed_root *btrfs_get_delayed_root(
struct btrfs_root *root)
{
return root->fs_info->delayed_root;
}
static struct btrfs_delayed_node *btrfs_get_delayed_node(struct inode *inode)
{
struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
struct btrfs_root *root = btrfs_inode->root;
u64 ino = btrfs_ino(inode);
struct btrfs_delayed_node *node;
node = ACCESS_ONCE(btrfs_inode->delayed_node);
if (node) {
atomic_inc(&node->refs);
return node;
}
spin_lock(&root->inode_lock);
node = radix_tree_lookup(&root->delayed_nodes_tree, ino);
if (node) {
if (btrfs_inode->delayed_node) {
atomic_inc(&node->refs); /* can be accessed */
BUG_ON(btrfs_inode->delayed_node != node);
spin_unlock(&root->inode_lock);
return node;
}
btrfs_inode->delayed_node = node;
/* can be accessed and cached in the inode */
atomic_add(2, &node->refs);
spin_unlock(&root->inode_lock);
return node;
}
spin_unlock(&root->inode_lock);
return NULL;
}
/* Will return either the node or PTR_ERR(-ENOMEM) */
static struct btrfs_delayed_node *btrfs_get_or_create_delayed_node(
struct inode *inode)
{
struct btrfs_delayed_node *node;
struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
struct btrfs_root *root = btrfs_inode->root;
u64 ino = btrfs_ino(inode);
int ret;
again:
node = btrfs_get_delayed_node(inode);
if (node)
return node;
node = kmem_cache_zalloc(delayed_node_cache, GFP_NOFS);
if (!node)
return ERR_PTR(-ENOMEM);
btrfs_init_delayed_node(node, root, ino);
/* cached in the btrfs inode and can be accessed */
atomic_add(2, &node->refs);
ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
if (ret) {
kmem_cache_free(delayed_node_cache, node);
return ERR_PTR(ret);
}
spin_lock(&root->inode_lock);
ret = radix_tree_insert(&root->delayed_nodes_tree, ino, node);
if (ret == -EEXIST) {
spin_unlock(&root->inode_lock);
kmem_cache_free(delayed_node_cache, node);
radix_tree_preload_end();
goto again;
}
btrfs_inode->delayed_node = node;
spin_unlock(&root->inode_lock);
radix_tree_preload_end();
return node;
}
/*
* Call it when holding delayed_node->mutex
*
* If mod = 1, add this node into the prepared list.
*/
static void btrfs_queue_delayed_node(struct btrfs_delayed_root *root,
struct btrfs_delayed_node *node,
int mod)
{
spin_lock(&root->lock);
if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
if (!list_empty(&node->p_list))
list_move_tail(&node->p_list, &root->prepare_list);
else if (mod)
list_add_tail(&node->p_list, &root->prepare_list);
} else {
list_add_tail(&node->n_list, &root->node_list);
list_add_tail(&node->p_list, &root->prepare_list);
atomic_inc(&node->refs); /* inserted into list */
root->nodes++;
set_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
}
spin_unlock(&root->lock);
}
/* Call it when holding delayed_node->mutex */
static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
struct btrfs_delayed_node *node)
{
spin_lock(&root->lock);
if (test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
root->nodes--;
atomic_dec(&node->refs); /* not in the list */
list_del_init(&node->n_list);
if (!list_empty(&node->p_list))
list_del_init(&node->p_list);
clear_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags);
}
spin_unlock(&root->lock);
}
static struct btrfs_delayed_node *btrfs_first_delayed_node(
struct btrfs_delayed_root *delayed_root)
{
struct list_head *p;
struct btrfs_delayed_node *node = NULL;
spin_lock(&delayed_root->lock);
if (list_empty(&delayed_root->node_list))
goto out;
p = delayed_root->node_list.next;
node = list_entry(p, struct btrfs_delayed_node, n_list);
atomic_inc(&node->refs);
out:
spin_unlock(&delayed_root->lock);
return node;
}
static struct btrfs_delayed_node *btrfs_next_delayed_node(
struct btrfs_delayed_node *node)
{
struct btrfs_delayed_root *delayed_root;
struct list_head *p;
struct btrfs_delayed_node *next = NULL;
delayed_root = node->root->fs_info->delayed_root;
spin_lock(&delayed_root->lock);
if (!test_bit(BTRFS_DELAYED_NODE_IN_LIST, &node->flags)) {
/* not in the list */
if (list_empty(&delayed_root->node_list))
goto out;
p = delayed_root->node_list.next;
} else if (list_is_last(&node->n_list, &delayed_root->node_list))
goto out;
else
p = node->n_list.next;
next = list_entry(p, struct btrfs_delayed_node, n_list);
atomic_inc(&next->refs);
out:
spin_unlock(&delayed_root->lock);
return next;
}
static void __btrfs_release_delayed_node(
struct btrfs_delayed_node *delayed_node,
int mod)
{
struct btrfs_delayed_root *delayed_root;
if (!delayed_node)
return;
delayed_root = delayed_node->root->fs_info->delayed_root;
mutex_lock(&delayed_node->mutex);
if (delayed_node->count)
btrfs_queue_delayed_node(delayed_root, delayed_node, mod);
else
btrfs_dequeue_delayed_node(delayed_root, delayed_node);
mutex_unlock(&delayed_node->mutex);
if (atomic_dec_and_test(&delayed_node->refs)) {
bool free = false;
struct btrfs_root *root = delayed_node->root;
spin_lock(&root->inode_lock);
if (atomic_read(&delayed_node->refs) == 0) {
radix_tree_delete(&root->delayed_nodes_tree,
delayed_node->inode_id);
free = true;
}
spin_unlock(&root->inode_lock);
if (free)
kmem_cache_free(delayed_node_cache, delayed_node);
}
}
static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
{
__btrfs_release_delayed_node(node, 0);
}
static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
struct btrfs_delayed_root *delayed_root)
{
struct list_head *p;
struct btrfs_delayed_node *node = NULL;
spin_lock(&delayed_root->lock);
if (list_empty(&delayed_root->prepare_list))
goto out;
p = delayed_root->prepare_list.next;
list_del_init(p);
node = list_entry(p, struct btrfs_delayed_node, p_list);
atomic_inc(&node->refs);
out:
spin_unlock(&delayed_root->lock);
return node;
}
static inline void btrfs_release_prepared_delayed_node(
struct btrfs_delayed_node *node)
{
__btrfs_release_delayed_node(node, 1);
}
static struct btrfs_delayed_item *btrfs_alloc_delayed_item(u32 data_len)
{
struct btrfs_delayed_item *item;
item = kmalloc(sizeof(*item) + data_len, GFP_NOFS);
if (item) {
item->data_len = data_len;
item->ins_or_del = 0;
item->bytes_reserved = 0;
item->delayed_node = NULL;
atomic_set(&item->refs, 1);
}
return item;
}
/*
* __btrfs_lookup_delayed_item - look up the delayed item by key
* @delayed_node: pointer to the delayed node
* @key: the key to look up
* @prev: used to store the prev item if the right item isn't found
* @next: used to store the next item if the right item isn't found
*
* Note: if we don't find the right item, we will return the prev item and
* the next item.
*/
static struct btrfs_delayed_item *__btrfs_lookup_delayed_item(
struct rb_root *root,
struct btrfs_key *key,
struct btrfs_delayed_item **prev,
struct btrfs_delayed_item **next)
{
struct rb_node *node, *prev_node = NULL;
struct btrfs_delayed_item *delayed_item = NULL;
int ret = 0;
node = root->rb_node;
while (node) {
delayed_item = rb_entry(node, struct btrfs_delayed_item,
rb_node);
prev_node = node;
ret = btrfs_comp_cpu_keys(&delayed_item->key, key);
if (ret < 0)
node = node->rb_right;
else if (ret > 0)
node = node->rb_left;
else
return delayed_item;
}
if (prev) {
if (!prev_node)
*prev = NULL;
else if (ret < 0)
*prev = delayed_item;
else if ((node = rb_prev(prev_node)) != NULL) {
*prev = rb_entry(node, struct btrfs_delayed_item,
rb_node);
} else
*prev = NULL;
}
if (next) {
if (!prev_node)
*next = NULL;
else if (ret > 0)
*next = delayed_item;
else if ((node = rb_next(prev_node)) != NULL) {
*next = rb_entry(node, struct btrfs_delayed_item,
rb_node);
} else
*next = NULL;
}
return NULL;
}
static struct btrfs_delayed_item *__btrfs_lookup_delayed_insertion_item(
struct btrfs_delayed_node *delayed_node,
struct btrfs_key *key)
{
struct btrfs_delayed_item *item;
item = __btrfs_lookup_delayed_item(&delayed_node->ins_root, key,
NULL, NULL);
return item;
}
static int __btrfs_add_delayed_item(struct btrfs_delayed_node *delayed_node,
struct btrfs_delayed_item *ins,
int action)
{
struct rb_node **p, *node;
struct rb_node *parent_node = NULL;
struct rb_root *root;
struct btrfs_delayed_item *item;
int cmp;
if (action == BTRFS_DELAYED_INSERTION_ITEM)
root = &delayed_node->ins_root;
else if (action == BTRFS_DELAYED_DELETION_ITEM)
root = &delayed_node->del_root;
else
BUG();
p = &root->rb_node;
node = &ins->rb_node;
while (*p) {
parent_node = *p;
item = rb_entry(parent_node, struct btrfs_delayed_item,
rb_node);
cmp = btrfs_comp_cpu_keys(&item->key, &ins->key);
if (cmp < 0)
p = &(*p)->rb_right;
else if (cmp > 0)
p = &(*p)->rb_left;
else
return -EEXIST;
}
rb_link_node(node, parent_node, p);
rb_insert_color(node, root);
ins->delayed_node = delayed_node;
ins->ins_or_del = action;
if (ins->key.type == BTRFS_DIR_INDEX_KEY &&
action == BTRFS_DELAYED_INSERTION_ITEM &&
ins->key.offset >= delayed_node->index_cnt)
delayed_node->index_cnt = ins->key.offset + 1;
delayed_node->count++;
atomic_inc(&delayed_node->root->fs_info->delayed_root->items);
return 0;
}
static int __btrfs_add_delayed_insertion_item(struct btrfs_delayed_node *node,
struct btrfs_delayed_item *item)
{
return __btrfs_add_delayed_item(node, item,
BTRFS_DELAYED_INSERTION_ITEM);
}
static int __btrfs_add_delayed_deletion_item(struct btrfs_delayed_node *node,
struct btrfs_delayed_item *item)
{
return __btrfs_add_delayed_item(node, item,
BTRFS_DELAYED_DELETION_ITEM);
}
static void finish_one_item(struct btrfs_delayed_root *delayed_root)
{
int seq = atomic_inc_return(&delayed_root->items_seq);
/*
* atomic_dec_return implies a barrier for waitqueue_active
*/
if ((atomic_dec_return(&delayed_root->items) <
BTRFS_DELAYED_BACKGROUND || seq % BTRFS_DELAYED_BATCH == 0) &&
waitqueue_active(&delayed_root->wait))
wake_up(&delayed_root->wait);
}
static void __btrfs_remove_delayed_item(struct btrfs_delayed_item *delayed_item)
{
struct rb_root *root;
struct btrfs_delayed_root *delayed_root;
delayed_root = delayed_item->delayed_node->root->fs_info->delayed_root;
BUG_ON(!delayed_root);
BUG_ON(delayed_item->ins_or_del != BTRFS_DELAYED_DELETION_ITEM &&
delayed_item->ins_or_del != BTRFS_DELAYED_INSERTION_ITEM);
if (delayed_item->ins_or_del == BTRFS_DELAYED_INSERTION_ITEM)
root = &delayed_item->delayed_node->ins_root;
else
root = &delayed_item->delayed_node->del_root;
rb_erase(&delayed_item->rb_node, root);
delayed_item->delayed_node->count--;
finish_one_item(delayed_root);
}
static void btrfs_release_delayed_item(struct btrfs_delayed_item *item)
{
if (item) {
__btrfs_remove_delayed_item(item);
if (atomic_dec_and_test(&item->refs))
kfree(item);
}
}
static struct btrfs_delayed_item *__btrfs_first_delayed_insertion_item(
struct btrfs_delayed_node *delayed_node)
{
struct rb_node *p;
struct btrfs_delayed_item *item = NULL;
p = rb_first(&delayed_node->ins_root);
if (p)
item = rb_entry(p, struct btrfs_delayed_item, rb_node);
return item;
}
static struct btrfs_delayed_item *__btrfs_first_delayed_deletion_item(
struct btrfs_delayed_node *delayed_node)
{
struct rb_node *p;
struct btrfs_delayed_item *item = NULL;
p = rb_first(&delayed_node->del_root);
if (p)
item = rb_entry(p, struct btrfs_delayed_item, rb_node);
return item;
}
static struct btrfs_delayed_item *__btrfs_next_delayed_item(
struct btrfs_delayed_item *item)
{
struct rb_node *p;
struct btrfs_delayed_item *next = NULL;
p = rb_next(&item->rb_node);
if (p)
next = rb_entry(p, struct btrfs_delayed_item, rb_node);
return next;
}
static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_delayed_item *item)
{
struct btrfs_block_rsv *src_rsv;
struct btrfs_block_rsv *dst_rsv;
u64 num_bytes;
int ret;
if (!trans->bytes_reserved)
return 0;
src_rsv = trans->block_rsv;
dst_rsv = &root->fs_info->delayed_block_rsv;
num_bytes = btrfs_calc_trans_metadata_size(root, 1);
ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
if (!ret) {
trace_btrfs_space_reservation(root->fs_info, "delayed_item",
item->key.objectid,
num_bytes, 1);
item->bytes_reserved = num_bytes;
}
return ret;
}
static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
struct btrfs_delayed_item *item)
{
struct btrfs_block_rsv *rsv;
if (!item->bytes_reserved)
return;
rsv = &root->fs_info->delayed_block_rsv;
trace_btrfs_space_reservation(root->fs_info, "delayed_item",
item->key.objectid, item->bytes_reserved,
0);
btrfs_block_rsv_release(root, rsv,
item->bytes_reserved);
}
static int btrfs_delayed_inode_reserve_metadata(
struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct inode *inode,
struct btrfs_delayed_node *node)
{
struct btrfs_block_rsv *src_rsv;
struct btrfs_block_rsv *dst_rsv;
u64 num_bytes;
int ret;
bool release = false;
src_rsv = trans->block_rsv;
dst_rsv = &root->fs_info->delayed_block_rsv;
num_bytes = btrfs_calc_trans_metadata_size(root, 1);
/*
* btrfs_dirty_inode will update the inode under btrfs_join_transaction
* which doesn't reserve space for speed. This is a problem since we
* still need to reserve space for this update, so try to reserve the
* space.
*
* Now if src_rsv == delalloc_block_rsv we'll let it just steal since
* we're accounted for.
*/
if (!src_rsv || (!trans->bytes_reserved &&
src_rsv->type != BTRFS_BLOCK_RSV_DELALLOC)) {
ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
BTRFS_RESERVE_NO_FLUSH);
/*
* Since we're under a transaction reserve_metadata_bytes could
* try to commit the transaction which will make it return
* EAGAIN to make us stop the transaction we have, so return
* ENOSPC instead so that btrfs_dirty_inode knows what to do.
*/
if (ret == -EAGAIN)
ret = -ENOSPC;
if (!ret) {
node->bytes_reserved = num_bytes;
trace_btrfs_space_reservation(root->fs_info,
"delayed_inode",
btrfs_ino(inode),
num_bytes, 1);
}
return ret;
} else if (src_rsv->type == BTRFS_BLOCK_RSV_DELALLOC) {
spin_lock(&BTRFS_I(inode)->lock);
if (test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
&BTRFS_I(inode)->runtime_flags)) {
spin_unlock(&BTRFS_I(inode)->lock);
release = true;
goto migrate;
}
spin_unlock(&BTRFS_I(inode)->lock);
/* Ok we didn't have space pre-reserved. This shouldn't happen
* too often but it can happen if we do delalloc to an existing
* inode which gets dirtied because of the time update, and then
* isn't touched again until after the transaction commits and
* then we try to write out the data. First try to be nice and
* reserve something strictly for us. If not be a pain and try
* to steal from the delalloc block rsv.
*/
ret = btrfs_block_rsv_add(root, dst_rsv, num_bytes,
BTRFS_RESERVE_NO_FLUSH);
if (!ret)
goto out;
ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
if (!ret)
goto out;
if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
btrfs_debug(root->fs_info,
"block rsv migrate returned %d", ret);
WARN_ON(1);
}
/*
* Ok this is a problem, let's just steal from the global rsv
* since this really shouldn't happen that often.
*/
ret = btrfs_block_rsv_migrate(&root->fs_info->global_block_rsv,
dst_rsv, num_bytes);
goto out;
}
migrate:
ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes);
out:
/*
* Migrate only takes a reservation, it doesn't touch the size of the
* block_rsv. This is to simplify people who don't normally have things
* migrated from their block rsv. If they go to release their
* reservation, that will decrease the size as well, so if migrate
* reduced size we'd end up with a negative size. But for the
* delalloc_meta_reserved stuff we will only know to drop 1 reservation,
* but we could in fact do this reserve/migrate dance several times
* between the time we did the original reservation and we'd clean it
* up. So to take care of this, release the space for the meta
* reservation here. I think it may be time for a documentation page on
* how block rsvs. work.
*/
if (!ret) {
trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
btrfs_ino(inode), num_bytes, 1);
node->bytes_reserved = num_bytes;
}
if (release) {
trace_btrfs_space_reservation(root->fs_info, "delalloc",
btrfs_ino(inode), num_bytes, 0);
btrfs_block_rsv_release(root, src_rsv, num_bytes);
}
return ret;
}
static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
struct btrfs_delayed_node *node)
{
struct btrfs_block_rsv *rsv;
if (!node->bytes_reserved)
return;
rsv = &root->fs_info->delayed_block_rsv;
trace_btrfs_space_reservation(root->fs_info, "delayed_inode",
node->inode_id, node->bytes_reserved, 0);
btrfs_block_rsv_release(root, rsv,
node->bytes_reserved);
node->bytes_reserved = 0;
}
/*
* This helper will insert some continuous items into the same leaf according
* to the free space of the leaf.
*/
static int btrfs_batch_insert_items(struct btrfs_root *root,
struct btrfs_path *path,
struct btrfs_delayed_item *item)
{
struct btrfs_delayed_item *curr, *next;
int free_space;
int total_data_size = 0, total_size = 0;
struct extent_buffer *leaf;
char *data_ptr;
struct btrfs_key *keys;
u32 *data_size;
struct list_head head;
int slot;
int nitems;
int i;
int ret = 0;
BUG_ON(!path->nodes[0]);
leaf = path->nodes[0];
free_space = btrfs_leaf_free_space(root, leaf);
INIT_LIST_HEAD(&head);
next = item;
nitems = 0;
/*
* count the number of the continuous items that we can insert in batch
*/
while (total_size + next->data_len + sizeof(struct btrfs_item) <=
free_space) {
total_data_size += next->data_len;
total_size += next->data_len + sizeof(struct btrfs_item);
list_add_tail(&next->tree_list, &head);
nitems++;
curr = next;
next = __btrfs_next_delayed_item(curr);
if (!next)
break;
if (!btrfs_is_continuous_delayed_item(curr, next))
break;
}
if (!nitems) {
ret = 0;
goto out;
}
/*
* we need allocate some memory space, but it might cause the task
* to sleep, so we set all locked nodes in the path to blocking locks
* first.
*/
btrfs_set_path_blocking(path);
keys = kmalloc_array(nitems, sizeof(struct btrfs_key), GFP_NOFS);
if (!keys) {
ret = -ENOMEM;
goto out;
}
data_size = kmalloc_array(nitems, sizeof(u32), GFP_NOFS);
if (!data_size) {
ret = -ENOMEM;
goto error;
}
/* get keys of all the delayed items */
i = 0;
list_for_each_entry(next, &head, tree_list) {
keys[i] = next->key;
data_size[i] = next->data_len;
i++;
}
/* reset all the locked nodes in the patch to spinning locks. */
btrfs_clear_path_blocking(path, NULL, 0);
/* insert the keys of the items */
setup_items_for_insert(root, path, keys, data_size,
total_data_size, total_size, nitems);
/* insert the dir index items */
slot = path->slots[0];
list_for_each_entry_safe(curr, next, &head, tree_list) {
data_ptr = btrfs_item_ptr(leaf, slot, char);
write_extent_buffer(leaf, &curr->data,
(unsigned long)data_ptr,
curr->data_len);
slot++;
btrfs_delayed_item_release_metadata(root, curr);
list_del(&curr->tree_list);
btrfs_release_delayed_item(curr);
}
error:
kfree(data_size);
kfree(keys);
out:
return ret;
}
/*
* This helper can just do simple insertion that needn't extend item for new
* data, such as directory name index insertion, inode insertion.
*/
static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path,
struct btrfs_delayed_item *delayed_item)
{
struct extent_buffer *leaf;
char *ptr;
int ret;
ret = btrfs_insert_empty_item(trans, root, path, &delayed_item->key,
delayed_item->data_len);
if (ret < 0 && ret != -EEXIST)
return ret;
leaf = path->nodes[0];
ptr = btrfs_item_ptr(leaf, path->slots[0], char);
write_extent_buffer(leaf, delayed_item->data, (unsigned long)ptr,
delayed_item->data_len);
btrfs_mark_buffer_dirty(leaf);
btrfs_delayed_item_release_metadata(root, delayed_item);
return 0;
}
/*
* we insert an item first, then if there are some continuous items, we try
* to insert those items into the same leaf.
*/
static int btrfs_insert_delayed_items(struct btrfs_trans_handle *trans,
struct btrfs_path *path,
struct btrfs_root *root,
struct btrfs_delayed_node *node)
{
struct btrfs_delayed_item *curr, *prev;
int ret = 0;
do_again:
mutex_lock(&node->mutex);
curr = __btrfs_first_delayed_insertion_item(node);
if (!curr)
goto insert_end;
ret = btrfs_insert_delayed_item(trans, root, path, curr);
if (ret < 0) {
btrfs_release_path(path);
goto insert_end;
}
prev = curr;
curr = __btrfs_next_delayed_item(prev);
if (curr && btrfs_is_continuous_delayed_item(prev, curr)) {
/* insert the continuous items into the same leaf */
path->slots[0]++;
btrfs_batch_insert_items(root, path, curr);
}
btrfs_release_delayed_item(prev);
btrfs_mark_buffer_dirty(path->nodes[0]);
btrfs_release_path(path);
mutex_unlock(&node->mutex);
goto do_again;
insert_end:
mutex_unlock(&node->mutex);
return ret;
}
static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path,
struct btrfs_delayed_item *item)
{
struct btrfs_delayed_item *curr, *next;
struct extent_buffer *leaf;
struct btrfs_key key;
struct list_head head;
int nitems, i, last_item;
int ret = 0;
BUG_ON(!path->nodes[0]);
leaf = path->nodes[0];
i = path->slots[0];
last_item = btrfs_header_nritems(leaf) - 1;
if (i > last_item)
return -ENOENT; /* FIXME: Is errno suitable? */
next = item;
INIT_LIST_HEAD(&head);
btrfs_item_key_to_cpu(leaf, &key, i);
nitems = 0;
/*
* count the number of the dir index items that we can delete in batch
*/
while (btrfs_comp_cpu_keys(&next->key, &key) == 0) {
list_add_tail(&next->tree_list, &head);
nitems++;
curr = next;
next = __btrfs_next_delayed_item(curr);
if (!next)
break;
if (!btrfs_is_continuous_delayed_item(curr, next))
break;
i++;
if (i > last_item)
break;
btrfs_item_key_to_cpu(leaf, &key, i);
}
if (!nitems)
return 0;
ret = btrfs_del_items(trans, root, path, path->slots[0], nitems);
if (ret)
goto out;
list_for_each_entry_safe(curr, next, &head, tree_list) {
btrfs_delayed_item_release_metadata(root, curr);
list_del(&curr->tree_list);
btrfs_release_delayed_item(curr);
}
out:
return ret;
}
static int btrfs_delete_delayed_items(struct btrfs_trans_handle *trans,
struct btrfs_path *path,
struct btrfs_root *root,
struct btrfs_delayed_node *node)
{
struct btrfs_delayed_item *curr, *prev;
int ret = 0;
do_again:
mutex_lock(&node->mutex);
curr = __btrfs_first_delayed_deletion_item(node);
if (!curr)
goto delete_fail;
ret = btrfs_search_slot(trans, root, &curr->key, path, -1, 1);
if (ret < 0)
goto delete_fail;
else if (ret > 0) {
/*
* can't find the item which the node points to, so this node
* is invalid, just drop it.
*/
prev = curr;
curr = __btrfs_next_delayed_item(prev);
btrfs_release_delayed_item(prev);
ret = 0;
btrfs_release_path(path);
if (curr) {
mutex_unlock(&node->mutex);
goto do_again;
} else
goto delete_fail;
}
btrfs_batch_delete_items(trans, root, path, curr);
btrfs_release_path(path);
mutex_unlock(&node->mutex);