-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctree.c
4750 lines (4180 loc) · 121 KB
/
ctree.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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2007,2008 Oracle. All rights reserved.
*/
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/rbtree.h>
#include <linux/mm.h>
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
#include "print-tree.h"
#include "locking.h"
#include "volumes.h"
#include "qgroup.h"
#include "tree-mod-log.h"
#include "apfs_trace.h"
static int split_node(struct apfs_trans_handle *trans, struct apfs_root
*root, struct apfs_path *path, int level);
static int split_leaf(struct apfs_trans_handle *trans, struct apfs_root *root,
const struct apfs_key *ins_key, struct apfs_path *path,
int data_size, int extend);
static int push_node_left(struct apfs_trans_handle *trans,
struct extent_buffer *dst,
struct extent_buffer *src, int empty);
static int balance_node_right(struct apfs_trans_handle *trans,
struct extent_buffer *dst_buf,
struct extent_buffer *src_buf);
static void del_ptr(struct apfs_root *root, struct apfs_path *path,
int level, int slot);
static const struct apfs_csums {
u16 size;
const char name[10];
const char driver[12];
} apfs_csums[] = {
[APFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
[APFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
[APFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
[APFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
.driver = "blake2b-256" },
};
int apfs_super_csum_size(const struct apfs_super_block *s)
{
u16 t = apfs_super_csum_type(s);
/*
* csum type is validated at mount time
*/
return apfs_csums[t].size;
}
const char *apfs_super_csum_name(u16 csum_type)
{
/* csum type is validated at mount time */
return apfs_csums[csum_type].name;
}
/*
* Return driver name if defined, otherwise the name that's also a valid driver
* name
*/
const char *apfs_super_csum_driver(u16 csum_type)
{
/* csum type is validated at mount time */
return apfs_csums[csum_type].driver[0] ?
apfs_csums[csum_type].driver :
apfs_csums[csum_type].name;
}
size_t __attribute_const__ apfs_get_num_csums(void)
{
return ARRAY_SIZE(apfs_csums);
}
struct apfs_path *apfs_alloc_path(void)
{
return kmem_cache_zalloc(apfs_path_cachep, GFP_NOFS);
}
/* this also releases the path */
void apfs_free_path(struct apfs_path *p)
{
if (!p)
return;
apfs_release_path(p);
kmem_cache_free(apfs_path_cachep, p);
}
/*
* path release drops references on the extent buffers in the path
* and it drops any locks held by this path
*
* It is safe to call this on paths that no locks or extent buffers held.
*/
noinline void apfs_release_path(struct apfs_path *p)
{
int i;
for (i = 0; i < APFS_MAX_LEVEL; i++) {
p->slots[i] = 0;
if (!p->nodes[i])
continue;
if (p->locks[i]) {
apfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
p->locks[i] = 0;
}
free_extent_buffer(p->nodes[i]);
p->nodes[i] = NULL;
}
}
/*
* safely gets a reference on the root node of a tree. A lock
* is not taken, so a concurrent writer may put a different node
* at the root of the tree. See apfs_lock_root_node for the
* looping required.
*
* The extent buffer returned by this has a reference taken, so
* it won't disappear. It may stop being the root of the tree
* at any time because there are no locks held.
*/
struct extent_buffer *apfs_root_node(struct apfs_root *root)
{
struct extent_buffer *eb;
while (1) {
rcu_read_lock();
eb = rcu_dereference(root->node);
/*
* RCU really hurts here, we could free up the root node because
* it was COWed but we may not get the new root node yet so do
* the inc_not_zero dance and if it doesn't work then
* synchronize_rcu and try again.
*/
if (atomic_inc_not_zero(&eb->refs)) {
rcu_read_unlock();
break;
}
rcu_read_unlock();
synchronize_rcu();
}
return eb;
}
/*
* Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
* just get put onto a simple dirty list. Transaction walks this list to make
* sure they get properly updated on disk.
*/
static void add_root_to_dirty_list(struct apfs_root *root)
{
struct apfs_fs_info *fs_info = root->fs_info;
if (test_bit(APFS_ROOT_DIRTY, &root->state) ||
!test_bit(APFS_ROOT_TRACK_DIRTY, &root->state))
return;
spin_lock(&fs_info->trans_lock);
if (!test_and_set_bit(APFS_ROOT_DIRTY, &root->state)) {
/* Want the extent tree to be the last on the list */
if (root->root_key.objectid == APFS_EXTENT_TREE_OBJECTID)
list_move_tail(&root->dirty_list,
&fs_info->dirty_cowonly_roots);
else
list_move(&root->dirty_list,
&fs_info->dirty_cowonly_roots);
}
spin_unlock(&fs_info->trans_lock);
}
/*
* used by snapshot creation to make a copy of a root for a tree with
* a given objectid. The buffer with the new root node is returned in
* cow_ret, and this func returns zero on success or a negative error code.
*/
int apfs_copy_root(struct apfs_trans_handle *trans,
struct apfs_root *root,
struct extent_buffer *buf,
struct extent_buffer **cow_ret, u64 new_root_objectid)
{
struct apfs_fs_info *fs_info = root->fs_info;
struct extent_buffer *cow;
int ret = 0;
int level;
struct apfs_disk_key disk_key;
WARN_ON(test_bit(APFS_ROOT_SHAREABLE, &root->state) &&
trans->transid != fs_info->running_transaction->transid);
WARN_ON(test_bit(APFS_ROOT_SHAREABLE, &root->state) &&
trans->transid != root->last_trans);
level = apfs_header_level(buf);
if (level == 0)
apfs_item_key(buf, &disk_key, 0);
else
apfs_node_key(buf, &disk_key, 0);
cow = apfs_alloc_tree_block(trans, root, 0, new_root_objectid,
&disk_key, level, buf->start, 0,
APFS_NESTING_NEW_ROOT);
if (IS_ERR(cow))
return PTR_ERR(cow);
copy_extent_buffer_full(cow, buf);
apfs_set_header_bytenr(cow, cow->start);
apfs_set_header_generation(cow, trans->transid);
apfs_set_header_backref_rev(cow, APFS_MIXED_BACKREF_REV);
apfs_clear_header_flag(cow, APFS_HEADER_FLAG_WRITTEN |
APFS_HEADER_FLAG_RELOC);
if (new_root_objectid == APFS_TREE_RELOC_OBJECTID)
apfs_set_header_flag(cow, APFS_HEADER_FLAG_RELOC);
else
apfs_set_header_owner(cow, new_root_objectid);
write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
WARN_ON(apfs_header_generation(buf) > trans->transid);
if (new_root_objectid == APFS_TREE_RELOC_OBJECTID)
ret = apfs_inc_ref(trans, root, cow, 1);
else
ret = apfs_inc_ref(trans, root, cow, 0);
if (ret) {
apfs_tree_unlock(cow);
free_extent_buffer(cow);
apfs_abort_transaction(trans, ret);
return ret;
}
apfs_mark_buffer_dirty(cow);
*cow_ret = cow;
return 0;
}
/*
* check if the tree block can be shared by multiple trees
*/
int apfs_block_can_be_shared(struct apfs_root *root,
struct extent_buffer *buf)
{
/*
* Tree blocks not in shareable trees and tree roots are never shared.
* If a block was allocated after the last snapshot and the block was
* not allocated by tree relocation, we know the block is not shared.
*/
if (test_bit(APFS_ROOT_SHAREABLE, &root->state) &&
buf != root->node && buf != root->commit_root &&
(apfs_header_generation(buf) <=
apfs_root_last_snapshot(&root->root_item) ||
apfs_header_flag(buf, APFS_HEADER_FLAG_RELOC)))
return 1;
return 0;
}
static noinline int update_ref_for_cow(struct apfs_trans_handle *trans,
struct apfs_root *root,
struct extent_buffer *buf,
struct extent_buffer *cow,
int *last_ref)
{
struct apfs_fs_info *fs_info = root->fs_info;
u64 refs;
u64 owner;
u64 flags;
u64 new_flags = 0;
int ret;
/*
* Backrefs update rules:
*
* Always use full backrefs for extent pointers in tree block
* allocated by tree relocation.
*
* If a shared tree block is no longer referenced by its owner
* tree (apfs_header_owner(buf) == root->root_key.objectid),
* use full backrefs for extent pointers in tree block.
*
* If a tree block is been relocating
* (root->root_key.objectid == APFS_TREE_RELOC_OBJECTID),
* use full backrefs for extent pointers in tree block.
* The reason for this is some operations (such as drop tree)
* are only allowed for blocks use full backrefs.
*/
if (apfs_block_can_be_shared(root, buf)) {
ret = apfs_lookup_extent_info(trans, fs_info, buf->start,
apfs_header_level(buf), 1,
&refs, &flags);
if (ret)
return ret;
if (refs == 0) {
ret = -EROFS;
apfs_handle_fs_error(fs_info, ret, NULL);
return ret;
}
} else {
refs = 1;
if (root->root_key.objectid == APFS_TREE_RELOC_OBJECTID ||
apfs_header_backref_rev(buf) < APFS_MIXED_BACKREF_REV)
flags = APFS_BLOCK_FLAG_FULL_BACKREF;
else
flags = 0;
}
owner = apfs_header_owner(buf);
BUG_ON(owner == APFS_TREE_RELOC_OBJECTID &&
!(flags & APFS_BLOCK_FLAG_FULL_BACKREF));
if (refs > 1) {
if ((owner == root->root_key.objectid ||
root->root_key.objectid == APFS_TREE_RELOC_OBJECTID) &&
!(flags & APFS_BLOCK_FLAG_FULL_BACKREF)) {
ret = apfs_inc_ref(trans, root, buf, 1);
if (ret)
return ret;
if (root->root_key.objectid ==
APFS_TREE_RELOC_OBJECTID) {
ret = apfs_dec_ref(trans, root, buf, 0);
if (ret)
return ret;
ret = apfs_inc_ref(trans, root, cow, 1);
if (ret)
return ret;
}
new_flags |= APFS_BLOCK_FLAG_FULL_BACKREF;
} else {
if (root->root_key.objectid ==
APFS_TREE_RELOC_OBJECTID)
ret = apfs_inc_ref(trans, root, cow, 1);
else
ret = apfs_inc_ref(trans, root, cow, 0);
if (ret)
return ret;
}
if (new_flags != 0) {
int level = apfs_header_level(buf);
ret = apfs_set_disk_extent_flags(trans, buf,
new_flags, level, 0);
if (ret)
return ret;
}
} else {
if (flags & APFS_BLOCK_FLAG_FULL_BACKREF) {
if (root->root_key.objectid ==
APFS_TREE_RELOC_OBJECTID)
ret = apfs_inc_ref(trans, root, cow, 1);
else
ret = apfs_inc_ref(trans, root, cow, 0);
if (ret)
return ret;
ret = apfs_dec_ref(trans, root, buf, 1);
if (ret)
return ret;
}
apfs_clean_tree_block(buf);
*last_ref = 1;
}
return 0;
}
/*
* does the dirty work in cow of a single block. The parent block (if
* supplied) is updated to point to the new cow copy. The new buffer is marked
* dirty and returned locked. If you modify the block it needs to be marked
* dirty again.
*
* search_start -- an allocation hint for the new block
*
* empty_size -- a hint that you plan on doing more cow. This is the size in
* bytes the allocator should try to find free next to the block it returns.
* This is just a hint and may be ignored by the allocator.
*/
static noinline int __apfs_cow_block(struct apfs_trans_handle *trans,
struct apfs_root *root,
struct extent_buffer *buf,
struct extent_buffer *parent, int parent_slot,
struct extent_buffer **cow_ret,
u64 search_start, u64 empty_size,
enum apfs_lock_nesting nest)
{
struct apfs_fs_info *fs_info = root->fs_info;
struct apfs_disk_key disk_key;
struct extent_buffer *cow;
int level, ret;
int last_ref = 0;
int unlock_orig = 0;
u64 parent_start = 0;
if (*cow_ret == buf)
unlock_orig = 1;
apfs_assert_tree_locked(buf);
WARN_ON(test_bit(APFS_ROOT_SHAREABLE, &root->state) &&
trans->transid != fs_info->running_transaction->transid);
WARN_ON(test_bit(APFS_ROOT_SHAREABLE, &root->state) &&
trans->transid != root->last_trans);
level = apfs_header_level(buf);
if (level == 0)
apfs_item_key(buf, &disk_key, 0);
else
apfs_node_key(buf, &disk_key, 0);
if ((root->root_key.objectid == APFS_TREE_RELOC_OBJECTID) && parent)
parent_start = parent->start;
cow = apfs_alloc_tree_block(trans, root, parent_start,
root->root_key.objectid, &disk_key, level,
search_start, empty_size, nest);
if (IS_ERR(cow))
return PTR_ERR(cow);
/* cow is set to blocking by apfs_init_new_buffer */
copy_extent_buffer_full(cow, buf);
apfs_set_header_bytenr(cow, cow->start);
apfs_set_header_generation(cow, trans->transid);
apfs_set_header_backref_rev(cow, APFS_MIXED_BACKREF_REV);
apfs_clear_header_flag(cow, APFS_HEADER_FLAG_WRITTEN |
APFS_HEADER_FLAG_RELOC);
if (root->root_key.objectid == APFS_TREE_RELOC_OBJECTID)
apfs_set_header_flag(cow, APFS_HEADER_FLAG_RELOC);
else
apfs_set_header_owner(cow, root->root_key.objectid);
write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
if (ret) {
apfs_tree_unlock(cow);
free_extent_buffer(cow);
apfs_abort_transaction(trans, ret);
return ret;
}
if (test_bit(APFS_ROOT_SHAREABLE, &root->state)) {
ret = apfs_reloc_cow_block(trans, root, buf, cow);
if (ret) {
apfs_tree_unlock(cow);
free_extent_buffer(cow);
apfs_abort_transaction(trans, ret);
return ret;
}
}
if (buf == root->node) {
WARN_ON(parent && parent != buf);
if (root->root_key.objectid == APFS_TREE_RELOC_OBJECTID ||
apfs_header_backref_rev(buf) < APFS_MIXED_BACKREF_REV)
parent_start = buf->start;
atomic_inc(&cow->refs);
ret = apfs_tree_mod_log_insert_root(root->node, cow, true);
BUG_ON(ret < 0);
rcu_assign_pointer(root->node, cow);
apfs_free_tree_block(trans, root, buf, parent_start,
last_ref);
free_extent_buffer(buf);
add_root_to_dirty_list(root);
} else {
WARN_ON(trans->transid != apfs_header_generation(parent));
apfs_tree_mod_log_insert_key(parent, parent_slot,
APFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
apfs_set_node_blockptr(parent, parent_slot,
cow->start);
apfs_set_node_ptr_generation(parent, parent_slot,
trans->transid);
apfs_mark_buffer_dirty(parent);
if (last_ref) {
ret = apfs_tree_mod_log_free_eb(buf);
if (ret) {
apfs_tree_unlock(cow);
free_extent_buffer(cow);
apfs_abort_transaction(trans, ret);
return ret;
}
}
apfs_free_tree_block(trans, root, buf, parent_start,
last_ref);
}
if (unlock_orig)
apfs_tree_unlock(buf);
free_extent_buffer_stale(buf);
apfs_mark_buffer_dirty(cow);
*cow_ret = cow;
return 0;
}
static inline int should_cow_block(struct apfs_trans_handle *trans,
struct apfs_root *root,
struct extent_buffer *buf)
{
if (apfs_is_testing(root->fs_info))
return 0;
/* Ensure we can see the FORCE_COW bit */
smp_mb__before_atomic();
/*
* We do not need to cow a block if
* 1) this block is not created or changed in this transaction;
* 2) this block does not belong to TREE_RELOC tree;
* 3) the root is not forced COW.
*
* What is forced COW:
* when we create snapshot during committing the transaction,
* after we've finished copying src root, we must COW the shared
* block to ensure the metadata consistency.
*/
if (apfs_header_generation(buf) == trans->transid &&
!apfs_header_flag(buf, APFS_HEADER_FLAG_WRITTEN) &&
!(root->root_key.objectid != APFS_TREE_RELOC_OBJECTID &&
apfs_header_flag(buf, APFS_HEADER_FLAG_RELOC)) &&
!test_bit(APFS_ROOT_FORCE_COW, &root->state))
return 0;
return 1;
}
/*
* cows a single block, see __apfs_cow_block for the real work.
* This version of it has extra checks so that a block isn't COWed more than
* once per transaction, as long as it hasn't been written yet
*/
noinline int apfs_cow_block(struct apfs_trans_handle *trans,
struct apfs_root *root, struct extent_buffer *buf,
struct extent_buffer *parent, int parent_slot,
struct extent_buffer **cow_ret,
enum apfs_lock_nesting nest)
{
struct apfs_fs_info *fs_info = root->fs_info;
u64 search_start;
int ret;
if (test_bit(APFS_ROOT_DELETING, &root->state))
apfs_err(fs_info,
"COW'ing blocks on a fs root that's being dropped");
if (trans->transaction != fs_info->running_transaction)
WARN(1, KERN_CRIT "trans %llu running %llu\n",
trans->transid,
fs_info->running_transaction->transid);
if (trans->transid != fs_info->generation)
WARN(1, KERN_CRIT "trans %llu running %llu\n",
trans->transid, fs_info->generation);
if (!should_cow_block(trans, root, buf)) {
*cow_ret = buf;
return 0;
}
search_start = buf->start & ~((u64)SZ_1G - 1);
/*
* Before CoWing this block for later modification, check if it's
* the subtree root and do the delayed subtree trace if needed.
*
* Also We don't care about the error, as it's handled internally.
*/
apfs_qgroup_trace_subtree_after_cow(trans, root, buf);
ret = __apfs_cow_block(trans, root, buf, parent,
parent_slot, cow_ret, search_start, 0, nest);
trace_apfs_cow_block(root, buf, *cow_ret);
return ret;
}
ALLOW_ERROR_INJECTION(apfs_cow_block, ERRNO);
/*
* helper function for defrag to decide if two blocks pointed to by a
* node are actually close by
*/
static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
{
if (blocknr < other && other - (blocknr + blocksize) < 32768)
return 1;
if (blocknr > other && blocknr - (other + blocksize) < 32768)
return 1;
return 0;
}
/*
* Compare two keys, on little-endian the disk order is same as CPU order and
* we can avoid the conversion.
*/
static int comp_keys(const struct extent_buffer *eb,
const struct apfs_disk_key *disk_key,
const struct apfs_key *k2)
{
struct apfs_key k1;
apfs_disk_key_to_cpu(eb, &k1, disk_key);
return apfs_comp_cpu_keys(eb, &k1, k2);
}
static inline int apfs_comp_str(const char *s1, const char *s2)
{
trace_printk("s1 %s s2 %s res\n", s1 ? s1 : "NULL" ,
s2 ? s2 : "NULL");
if (!s1 && !s2)
return 0;
else if (!s1 && s2)
return -1;
else if (s1 && !s2)
return 1;
trace_printk("s1 %s s2 %s res %d\n", s1 ,s2, strcmp(s1, s2));
return strcmp(s1, s2);
}
static inline int apfs_comp_casestr(const char *s1, const char *s2)
{
if (!s1 && !s2)
return 0;
else if (!s1 && s2)
return -1;
else if (s1 && !s2)
return 1;
return strcmp(s1, s2);
}
static noinline int
apfs_comp_fs_keys(const struct apfs_key *k1, const struct apfs_key *k2,
bool hashed)
{
if (k1->oid != k2->oid)
return k1->oid < k2->oid ? -1 : 1;
if (k1->type != k2->type)
return k1->type < k2->type ? -1 : 1;
if (k1->type == APFS_TYPE_DIR_REC) {
if (!hashed)
return apfs_comp_str(k1->name, k2->name);
if (k1->hash == k2->hash)
return apfs_comp_str(k1->name, k2->name);
return k1->hash < k2->hash ? -1 : 1;
} else if (k1->type == APFS_TYPE_SNAP_NAME ||
k1->type == APFS_TYPE_XATTR) {
return apfs_comp_str(k1->name, k2->name);
}
if (k1->offset < k2->offset)
return -1;
if (k1->offset > k2->offset)
return 1;
return 0;
}
/*
* compare two keys in a memcmp fashion
* One of k1/k2 must be valid cause we reply on the key->type to decide how to
* compare.
*/
static noinline int
apfs_comp_normal_keys(const struct apfs_key *k1, const struct apfs_key *k2)
{
/* type is on high bits */
if (k1->type != k2->type)
return k1->type < k2->type ? -1 : 1;
if (k1->id != k2->id)
return k1->id < k2->id ? -1 : 1;
if (k1->offset != k2->offset)
return k1->offset < k2->offset ? -1 : 1;
return 0;
}
/*
* same as comp_keys only with two apfs_key's
*/
int __pure apfs_comp_cpu_keys(const struct extent_buffer *eb,
const struct apfs_key *k1,
const struct apfs_key *k2)
{
if (!apfs_is_fs_node(eb))
return apfs_comp_normal_keys(k1, k2);
return apfs_comp_fs_keys(k1, k2,
apfs_is_normalization_insensitive(eb->fs_info->__super_copy));
}
/*
* this is used by the defrag code to go through all the
* leaves pointed to by a node and reallocate them so that
* disk order is close to key order
*/
int apfs_realloc_node(struct apfs_trans_handle *trans,
struct apfs_root *root, struct extent_buffer *parent,
int start_slot, u64 *last_ret,
struct apfs_key *progress)
{
return 0;
}
/*
* search for key in the extent_buffer. The items start at offset p,
* and they are item_size apart. There are 'max' items in p.
*
* the slot in the array is returned via slot, and it points to
* the place where you would insert key if it is not found in
* the array.
*
* slot may point to max if the key is bigger than all of the keys
*/
static noinline int generic_bin_search(struct extent_buffer *eb,
const struct apfs_key *key,
int max, int *slot)
{
int low = 0;
int high = max;
int ret;
if (low > high) {
apfs_err(eb->fs_info,
"%s: low (%d) > high (%d) eb %llu owner %llu level %d",
__func__, low, high, eb->start,
apfs_header_owner(eb), apfs_header_level(eb));
return -EINVAL;
}
while (low < high) {
struct apfs_key tmp = {};
int mid;
mid = (low + high) / 2;
apfs_item_key_to_cpu(eb, &tmp, mid);
ret = apfs_comp_cpu_keys(eb, &tmp, key);
// trace_printk("low %d mid %d high %d ret: %d\n", low, mid, high, ret);
// apfs_print_key_raw(eb, &tmp);
// trace_printk(" comp ");
// apfs_print_key_raw(eb, key);
// trace_printk("\n");
if (ret < 0)
low = mid + 1;
else if (ret > 0)
high = mid;
else {
*slot = mid;
return 0;
}
}
*slot = low;
return 1;
}
/*
* simple bin_search frontend that does the right thing for
* leaves vs nodes
*/
int apfs_bin_search(struct extent_buffer *eb, const struct apfs_key *key,
int *slot)
{
return generic_bin_search(eb, key, apfs_header_nritems(eb), slot);
}
static void root_add_used(struct apfs_root *root, u32 size)
{
spin_lock(&root->accounting_lock);
apfs_set_root_used(&root->root_item,
apfs_root_used(&root->root_item) + size);
spin_unlock(&root->accounting_lock);
}
static void root_sub_used(struct apfs_root *root, u32 size)
{
spin_lock(&root->accounting_lock);
apfs_set_root_used(&root->root_item,
apfs_root_used(&root->root_item) - size);
spin_unlock(&root->accounting_lock);
}
/* given a node and slot number, this reads the blocks it points to. The
* extent buffer is returned with a reference taken (but unlocked).
*/
struct extent_buffer *apfs_read_node_slot(struct extent_buffer *parent,
int slot)
{
int level = apfs_header_level(parent);
struct extent_buffer *eb;
struct apfs_key first_key = {};
if (slot < 0 || slot >= apfs_header_nritems(parent))
return ERR_PTR(-ENOENT);
BUG_ON(level == 0);
apfs_node_key_to_cpu(parent, &first_key, slot);
eb = read_tree_block(parent->fs_info, apfs_node_blockptr(parent, slot),
apfs_header_owner(parent),
apfs_node_ptr_generation(parent, slot),
level - 1, &first_key);
if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
free_extent_buffer(eb);
eb = ERR_PTR(-EIO);
}
return eb;
}
/*
* node level balancing, used to make sure nodes are in proper order for
* item deletion. We balance from the top down, so we have to make sure
* that a deletion won't leave an node completely empty later on.
*/
static noinline int balance_level(struct apfs_trans_handle *trans,
struct apfs_root *root,
struct apfs_path *path, int level)
{
struct apfs_fs_info *fs_info = root->fs_info;
struct extent_buffer *right = NULL;
struct extent_buffer *mid;
struct extent_buffer *left = NULL;
struct extent_buffer *parent = NULL;
int ret = 0;
int wret;
int pslot;
int orig_slot = path->slots[level];
u64 orig_ptr;
ASSERT(level > 0);
mid = path->nodes[level];
WARN_ON(path->locks[level] != APFS_WRITE_LOCK);
WARN_ON(apfs_header_generation(mid) != trans->transid);
orig_ptr = apfs_node_blockptr(mid, orig_slot);
if (level < APFS_MAX_LEVEL - 1) {
parent = path->nodes[level + 1];
pslot = path->slots[level + 1];
}
/*
* deal with the case where there is only one pointer in the root
* by promoting the node below to a root
*/
if (!parent) {
struct extent_buffer *child;
if (apfs_header_nritems(mid) != 1)
return 0;
/* promote the child to a root */
child = apfs_read_node_slot(mid, 0);
if (IS_ERR(child)) {
ret = PTR_ERR(child);
apfs_handle_fs_error(fs_info, ret, NULL);
goto enospc;
}
apfs_tree_lock(child);
ret = apfs_cow_block(trans, root, child, mid, 0, &child,
APFS_NESTING_COW);
if (ret) {
apfs_tree_unlock(child);
free_extent_buffer(child);
goto enospc;
}
ret = apfs_tree_mod_log_insert_root(root->node, child, true);
BUG_ON(ret < 0);
rcu_assign_pointer(root->node, child);
add_root_to_dirty_list(root);
apfs_tree_unlock(child);
path->locks[level] = 0;
path->nodes[level] = NULL;
apfs_clean_tree_block(mid);
apfs_tree_unlock(mid);
/* once for the path */
free_extent_buffer(mid);
root_sub_used(root, mid->len);
apfs_free_tree_block(trans, root, mid, 0, 1);
/* once for the root ptr */
free_extent_buffer_stale(mid);
return 0;
}
if (apfs_header_nritems(mid) >
APFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
return 0;
left = apfs_read_node_slot(parent, pslot - 1);
if (IS_ERR(left))
left = NULL;
if (left) {
__apfs_tree_lock(left, APFS_NESTING_LEFT);
wret = apfs_cow_block(trans, root, left,
parent, pslot - 1, &left,
APFS_NESTING_LEFT_COW);
if (wret) {
ret = wret;
goto enospc;
}
}
right = apfs_read_node_slot(parent, pslot + 1);
if (IS_ERR(right))
right = NULL;
if (right) {
__apfs_tree_lock(right, APFS_NESTING_RIGHT);
wret = apfs_cow_block(trans, root, right,
parent, pslot + 1, &right,
APFS_NESTING_RIGHT_COW);
if (wret) {
ret = wret;
goto enospc;
}
}
/* first, try to make some room in the middle buffer */
if (left) {
orig_slot += apfs_header_nritems(left);
wret = push_node_left(trans, left, mid, 1);
if (wret < 0)
ret = wret;
}
/*
* then try to empty the right most buffer into the middle
*/
if (right) {
wret = push_node_left(trans, mid, right, 1);
if (wret < 0 && wret != -ENOSPC)
ret = wret;
if (apfs_header_nritems(right) == 0) {
apfs_clean_tree_block(right);
apfs_tree_unlock(right);
del_ptr(root, path, level + 1, pslot + 1);
root_sub_used(root, right->len);
apfs_free_tree_block(trans, root, right, 0, 1);
free_extent_buffer_stale(right);
right = NULL;
} else {
struct apfs_disk_key right_key;
apfs_node_key(right, &right_key, 0);
ret = apfs_tree_mod_log_insert_key(parent, pslot + 1,
APFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
BUG_ON(ret < 0);
apfs_set_node_key(parent, &right_key, pslot + 1);
apfs_mark_buffer_dirty(parent);
}
}
if (apfs_header_nritems(mid) == 1) {
/*
* we're not allowed to leave a node with one item in the
* tree during a delete. A deletion from lower in the tree
* could try to delete the only pointer in this node.
* So, pull some keys from the left.
* There has to be a left pointer at this point because
* otherwise we would have pulled some pointers from the
* right
*/
if (!left) {
ret = -EROFS;
apfs_handle_fs_error(fs_info, ret, NULL);
goto enospc;
}
wret = balance_node_right(trans, mid, left);
if (wret < 0) {
ret = wret;
goto enospc;
}
if (wret == 1) {
wret = push_node_left(trans, left, mid, 1);
if (wret < 0)
ret = wret;
}
BUG_ON(wret == 1);
}
if (apfs_header_nritems(mid) == 0) {
apfs_clean_tree_block(mid);
apfs_tree_unlock(mid);
del_ptr(root, path, level + 1, pslot);
root_sub_used(root, mid->len);
apfs_free_tree_block(trans, root, mid, 0, 1);
free_extent_buffer_stale(mid);
mid = NULL;
} else {
/* update the parent key to reflect our changes */