-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
node.c
2069 lines (1833 loc) · 60.5 KB
/
node.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-only
/*
* Copyright (C) 2018 Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
*/
#include <linux/slab.h>
#include <linux/buffer_head.h>
#include "apfs.h"
/**
* apfs_node_is_valid - Check basic sanity of the node index
* @sb: filesystem superblock
* @node: node to check
*
* Verifies that the node index fits in a single block, and that the number
* of records fits in the index. Without this check a crafted filesystem could
* pretend to have too many records, and calls to apfs_node_locate_key() and
* apfs_node_locate_data() would read beyond the limits of the node.
*/
static bool apfs_node_is_valid(struct super_block *sb,
struct apfs_node *node)
{
u32 records = node->records;
int index_size = node->key - sizeof(struct apfs_btree_node_phys);
int entry_size;
if (node->key > sb->s_blocksize)
return false;
entry_size = (apfs_node_has_fixed_kv_size(node)) ?
sizeof(struct apfs_kvoff) : sizeof(struct apfs_kvloc);
/* Coarse bound to prevent multiplication overflow in final check */
if (records > 1 << 16)
return false;
return records * entry_size <= index_size;
}
void apfs_node_free(struct apfs_node *node)
{
struct apfs_object *obj = NULL;
if (!node)
return;
obj = &node->object;
if (obj->o_bh) {
brelse(obj->o_bh);
obj->o_bh = NULL;
} else if (!obj->ephemeral) {
/* Ephemeral data always remains in memory */
kfree(obj->data);
}
obj->data = NULL;
kfree(node);
}
/**
* apfs_read_node - Read a node header from disk
* @sb: filesystem superblock
* @oid: object id for the node
* @storage: storage type for the node object
* @write: request write access?
*
* Returns ERR_PTR in case of failure, otherwise return a pointer to the
* resulting apfs_node structure with the initial reference taken.
*
* For now we assume the node has not been read before.
*/
struct apfs_node *apfs_read_node(struct super_block *sb, u64 oid, u32 storage,
bool write)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct buffer_head *bh = NULL;
struct apfs_ephemeral_object_info *eph_info = NULL;
struct apfs_btree_node_phys *raw = NULL;
struct apfs_node *node = NULL;
struct apfs_nloc *free_head = NULL;
u64 bno;
int err;
switch (storage) {
case APFS_OBJ_VIRTUAL:
/* All virtual nodes are inside a volume, at least for now */
err = apfs_omap_lookup_block(sb, sbi->s_omap, oid, &bno, write);
if (err) {
apfs_err(sb, "omap lookup failed for oid 0x%llx", oid);
return ERR_PTR(err);
}
/* CoW has already been done, don't worry about snapshots */
bh = apfs_read_object_block(sb, bno, write, false /* preserve */);
if (IS_ERR(bh)) {
apfs_err(sb, "object read failed for bno 0x%llx", bno);
return (void *)bh;
}
bno = bh->b_blocknr;
raw = (struct apfs_btree_node_phys *)bh->b_data;
break;
case APFS_OBJ_PHYSICAL:
bh = apfs_read_object_block(sb, oid, write, false /* preserve */);
if (IS_ERR(bh)) {
apfs_err(sb, "object read failed for bno 0x%llx", oid);
return (void *)bh;
}
bno = oid = bh->b_blocknr;
raw = (struct apfs_btree_node_phys *)bh->b_data;
break;
case APFS_OBJ_EPHEMERAL:
/* Ephemeral objects are already in memory */
eph_info = apfs_ephemeral_object_lookup(sb, oid);
if (IS_ERR(eph_info)) {
apfs_err(sb, "no ephemeral node for oid 0x%llx", oid);
return (void *)eph_info;
}
if (eph_info->size != sb->s_blocksize) {
apfs_err(sb, "unsupported size for ephemeral node (%u)", eph_info->size);
return ERR_PTR(-EOPNOTSUPP);
}
bno = 0; /* In memory, so meaningless */
raw = eph_info->object;
/* Only for consistency, will happen again on commit */
if (write)
raw->btn_o.o_xid = cpu_to_le64(nxi->nx_xid);
break;
default:
apfs_alert(sb, "invalid storage type %u - bug!", storage);
return ERR_PTR(-EINVAL);
}
node = kmalloc(sizeof(*node), GFP_KERNEL);
if (!node) {
brelse(bh);
return ERR_PTR(-ENOMEM);
}
node->tree_type = le32_to_cpu(raw->btn_o.o_subtype);
node->flags = le16_to_cpu(raw->btn_flags);
node->records = le32_to_cpu(raw->btn_nkeys);
node->key = sizeof(*raw) + le16_to_cpu(raw->btn_table_space.off)
+ le16_to_cpu(raw->btn_table_space.len);
node->free = node->key + le16_to_cpu(raw->btn_free_space.off);
node->data = node->free + le16_to_cpu(raw->btn_free_space.len);
free_head = &raw->btn_key_free_list;
node->key_free_list_len = le16_to_cpu(free_head->len);
free_head = &raw->btn_val_free_list;
node->val_free_list_len = le16_to_cpu(free_head->len);
node->object.sb = sb;
node->object.block_nr = bno;
node->object.oid = oid;
node->object.o_bh = bh;
node->object.data = (char *)raw;
node->object.ephemeral = !bh;
/* Ephemeral objects already got checked on mount */
if (!node->object.ephemeral && nxi->nx_flags & APFS_CHECK_NODES && !apfs_obj_verify_csum(sb, bh)) {
/* TODO: don't check this twice for virtual/physical objects */
apfs_err(sb, "bad checksum for node in block 0x%llx", (unsigned long long)bno);
apfs_node_free(node);
return ERR_PTR(-EFSBADCRC);
}
if (!apfs_node_is_valid(sb, node)) {
apfs_err(sb, "bad node in block 0x%llx", (unsigned long long)bno);
apfs_node_free(node);
return ERR_PTR(-EFSCORRUPTED);
}
return node;
}
/**
* apfs_node_min_table_size - Return the minimum size for a node's toc
* @sb: superblock structure
* @type: tree type for the node
* @flags: flags for the node
*/
static int apfs_node_min_table_size(struct super_block *sb, u32 type, u16 flags)
{
bool leaf = flags & APFS_BTNODE_LEAF;
int key_size, val_size, toc_size;
int space, count;
/* Preallocate the whole table for trees with fixed key/value sizes */
switch (type) {
case APFS_OBJECT_TYPE_OMAP:
key_size = sizeof(struct apfs_omap_key);
val_size = leaf ? sizeof(struct apfs_omap_val) : sizeof(__le64);
toc_size = sizeof(struct apfs_kvoff);
break;
case APFS_OBJECT_TYPE_SPACEMAN_FREE_QUEUE:
key_size = sizeof(struct apfs_spaceman_free_queue_key);
val_size = sizeof(__le64); /* We assume no ghosts here */
toc_size = sizeof(struct apfs_kvoff);
break;
case APFS_OBJECT_TYPE_OMAP_SNAPSHOT:
key_size = sizeof(__le64);
val_size = leaf ? sizeof(struct apfs_omap_snapshot) : sizeof(__le64);
toc_size = sizeof(struct apfs_kvoff);
break;
case APFS_OBJECT_TYPE_FEXT_TREE:
key_size = sizeof(struct apfs_fext_tree_key);
val_size = leaf ? sizeof(struct apfs_fext_tree_val) : sizeof(__le64);
toc_size = sizeof(struct apfs_kvoff);
break;
default:
/* Make room for one record at least */
toc_size = sizeof(struct apfs_kvloc);
return APFS_BTREE_TOC_ENTRY_INCREMENT * toc_size;
}
/* The footer of root nodes is ignored for some reason */
space = sb->s_blocksize - sizeof(struct apfs_btree_node_phys);
count = space / (key_size + val_size + toc_size);
return count * toc_size;
}
/**
* apfs_set_empty_btree_info - Set the info footer for an empty b-tree node
* @sb: filesystem superblock
* @info: pointer to the on-disk info footer
* @subtype: subtype of the root node, i.e., tree type
*
* For now only supports the extent reference tree.
*/
static void apfs_set_empty_btree_info(struct super_block *sb, struct apfs_btree_info *info, u32 subtype)
{
u32 flags;
ASSERT(subtype == APFS_OBJECT_TYPE_BLOCKREFTREE || subtype == APFS_OBJECT_TYPE_OMAP_SNAPSHOT);
memset(info, 0, sizeof(*info));
flags = APFS_BTREE_PHYSICAL;
if (subtype == APFS_OBJECT_TYPE_BLOCKREFTREE)
flags |= APFS_BTREE_KV_NONALIGNED;
info->bt_fixed.bt_flags = cpu_to_le32(flags);
info->bt_fixed.bt_node_size = cpu_to_le32(sb->s_blocksize);
info->bt_key_count = 0;
info->bt_node_count = cpu_to_le64(1); /* Only one node: the root */
if (subtype == APFS_OBJECT_TYPE_BLOCKREFTREE)
return;
info->bt_fixed.bt_key_size = cpu_to_le32(8);
info->bt_longest_key = info->bt_fixed.bt_key_size;
info->bt_fixed.bt_val_size = cpu_to_le32(sizeof(struct apfs_omap_snapshot));
info->bt_longest_val = info->bt_fixed.bt_val_size;
}
/**
* apfs_make_empty_btree_root - Make an empty root for a b-tree
* @sb: filesystem superblock
* @subtype: subtype of the root node, i.e., tree type
* @oid: on return, the root's object id
*
* For now only supports the extent reference tree and an omap's snapshot tree.
* Returns 0 on success or a negative error code in case of failure.
*/
int apfs_make_empty_btree_root(struct super_block *sb, u32 subtype, u64 *oid)
{
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
struct apfs_btree_node_phys *root = NULL;
struct buffer_head *bh = NULL;
u64 bno;
u16 flags;
int toc_len, free_len, head_len, info_len;
int err;
ASSERT(subtype == APFS_OBJECT_TYPE_BLOCKREFTREE || subtype == APFS_OBJECT_TYPE_OMAP_SNAPSHOT);
err = apfs_spaceman_allocate_block(sb, &bno, true /* backwards */);
if (err) {
apfs_err(sb, "block allocation failed");
return err;
}
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, 1);
le64_add_cpu(&vsb_raw->apfs_total_blocks_alloced, 1);
bh = apfs_getblk(sb, bno);
if (!bh)
return -EIO;
root = (void *)bh->b_data;
err = apfs_transaction_join(sb, bh);
if (err)
goto fail;
set_buffer_csum(bh);
flags = APFS_BTNODE_ROOT | APFS_BTNODE_LEAF;
if (subtype == APFS_OBJECT_TYPE_OMAP_SNAPSHOT)
flags |= APFS_BTNODE_FIXED_KV_SIZE;
root->btn_flags = cpu_to_le16(flags);
toc_len = apfs_node_min_table_size(sb, subtype, flags);
head_len = sizeof(*root);
info_len = sizeof(struct apfs_btree_info);
free_len = sb->s_blocksize - head_len - toc_len - info_len;
root->btn_level = 0; /* Root */
/* No keys and no values, so this is straightforward */
root->btn_nkeys = 0;
root->btn_table_space.off = 0;
root->btn_table_space.len = cpu_to_le16(toc_len);
root->btn_free_space.off = 0;
root->btn_free_space.len = cpu_to_le16(free_len);
/* No fragmentation */
root->btn_key_free_list.off = cpu_to_le16(APFS_BTOFF_INVALID);
root->btn_key_free_list.len = 0;
root->btn_val_free_list.off = cpu_to_le16(APFS_BTOFF_INVALID);
root->btn_val_free_list.len = 0;
apfs_set_empty_btree_info(sb, (void *)root + sb->s_blocksize - info_len, subtype);
root->btn_o.o_oid = cpu_to_le64(bno);
root->btn_o.o_xid = cpu_to_le64(APFS_NXI(sb)->nx_xid);
root->btn_o.o_type = cpu_to_le32(APFS_OBJECT_TYPE_BTREE | APFS_OBJ_PHYSICAL);
root->btn_o.o_subtype = cpu_to_le32(subtype);
*oid = bno;
err = 0;
fail:
root = NULL;
brelse(bh);
bh = NULL;
return err;
}
/**
* apfs_create_node - Allocates a new nonroot b-tree node on disk
* @sb: filesystem superblock
* @storage: storage type for the node object
*
* On success returns a pointer to the new in-memory node structure; the object
* header is initialized, and the node fields are given reasonable defaults.
* On failure, returns an error pointer.
*/
static struct apfs_node *apfs_create_node(struct super_block *sb, u32 storage)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct apfs_nx_superblock *msb_raw = nxi->nx_raw;
struct apfs_superblock *vsb_raw = sbi->s_vsb_raw;
struct apfs_ephemeral_object_info *eph_info = NULL;
struct apfs_node *node = NULL;
struct buffer_head *bh = NULL;
struct apfs_btree_node_phys *raw = NULL;
u64 bno, oid;
int err;
switch (storage) {
case APFS_OBJ_VIRTUAL:
err = apfs_spaceman_allocate_block(sb, &bno, true /* backwards */);
if (err) {
apfs_err(sb, "block allocation failed");
return ERR_PTR(err);
}
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, 1);
le64_add_cpu(&vsb_raw->apfs_total_blocks_alloced, 1);
oid = le64_to_cpu(msb_raw->nx_next_oid);
le64_add_cpu(&msb_raw->nx_next_oid, 1);
err = apfs_create_omap_rec(sb, oid, bno);
if (err) {
apfs_err(sb, "omap rec creation failed (0x%llx-0x%llx)", oid, bno);
return ERR_PTR(err);
}
break;
case APFS_OBJ_PHYSICAL:
err = apfs_spaceman_allocate_block(sb, &bno, true /* backwards */);
if (err) {
apfs_err(sb, "block allocation failed");
return ERR_PTR(err);
}
/* We don't write to the container's omap */
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, 1);
le64_add_cpu(&vsb_raw->apfs_total_blocks_alloced, 1);
oid = bno;
break;
case APFS_OBJ_EPHEMERAL:
if (nxi->nx_eph_count >= APFS_EPHEMERAL_LIST_LIMIT) {
apfs_err(sb, "creating too many ephemeral objects?");
return ERR_PTR(-EOPNOTSUPP);
}
eph_info = &nxi->nx_eph_list[nxi->nx_eph_count++];
eph_info->object = kzalloc(sb->s_blocksize, GFP_KERNEL);
if (!eph_info->object)
return ERR_PTR(-ENOMEM);
eph_info->size = sb->s_blocksize;
oid = eph_info->oid = le64_to_cpu(msb_raw->nx_next_oid);
le64_add_cpu(&msb_raw->nx_next_oid, 1);
break;
default:
apfs_alert(sb, "invalid storage type %u - bug!", storage);
return ERR_PTR(-EINVAL);
}
if (storage == APFS_OBJ_EPHEMERAL) {
bh = NULL;
bno = 0;
raw = eph_info->object;
} else {
bh = apfs_getblk(sb, bno);
if (!bh)
return ERR_PTR(-EIO);
bno = bh->b_blocknr;
raw = (void *)bh->b_data;
err = apfs_transaction_join(sb, bh);
if (err)
goto fail;
set_buffer_csum(bh);
}
/* Set most of the object header, but the subtype is up to the caller */
raw->btn_o.o_oid = cpu_to_le64(oid);
raw->btn_o.o_xid = cpu_to_le64(nxi->nx_xid);
raw->btn_o.o_type = cpu_to_le32(storage | APFS_OBJECT_TYPE_BTREE_NODE);
raw->btn_o.o_subtype = 0;
/* The caller is expected to change most node fields */
raw->btn_flags = 0;
raw->btn_level = 0;
raw->btn_nkeys = 0;
raw->btn_table_space.off = 0; /* Put the toc right after the header */
raw->btn_table_space.len = 0;
raw->btn_free_space.off = 0;
raw->btn_free_space.len = cpu_to_le16(sb->s_blocksize - sizeof(*raw));
raw->btn_key_free_list.off = cpu_to_le16(APFS_BTOFF_INVALID);
raw->btn_key_free_list.len = 0;
raw->btn_val_free_list.off = cpu_to_le16(APFS_BTOFF_INVALID);
raw->btn_val_free_list.len = 0;
node = kmalloc(sizeof(*node), GFP_KERNEL);
if (!node) {
err = -ENOMEM;
goto fail;
}
node->object.sb = sb;
node->object.block_nr = bno;
node->object.oid = oid;
node->object.o_bh = bh;
node->object.data = (char *)raw;
node->object.ephemeral = !bh;
return node;
fail:
if (storage == APFS_OBJ_EPHEMERAL)
kfree(raw);
else
brelse(bh);
raw = NULL;
bh = NULL;
return ERR_PTR(err);
}
/**
* apfs_delete_node - Deletes a nonroot node from disk
* @node: node to delete
* @type: tree type for the query that found the node
*
* Does nothing to the in-memory node structure. Returns 0 on success, or a
* negative error code in case of failure.
*/
int apfs_delete_node(struct apfs_node *node, int type)
{
struct super_block *sb = node->object.sb;
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct apfs_superblock *vsb_raw;
u64 oid = node->object.oid;
u64 bno = node->object.block_nr;
struct apfs_ephemeral_object_info *eph_info = NULL, *eph_info_end = NULL;
int err;
switch (type) {
case APFS_QUERY_CAT:
err = apfs_free_queue_insert(sb, bno, 1);
if (err) {
apfs_err(sb, "free queue insertion failed for 0x%llx", bno);
return err;
}
err = apfs_delete_omap_rec(sb, oid);
if (err) {
apfs_err(sb, "omap rec deletion failed (0x%llx)", oid);
return err;
}
vsb_raw = APFS_SB(sb)->s_vsb_raw;
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, -1);
le64_add_cpu(&vsb_raw->apfs_total_blocks_freed, 1);
return 0;
case APFS_QUERY_OMAP:
case APFS_QUERY_EXTENTREF:
case APFS_QUERY_SNAP_META:
err = apfs_free_queue_insert(sb, bno, 1);
if (err) {
apfs_err(sb, "free queue insertion failed for 0x%llx", bno);
return err;
}
/* We don't write to the container's omap */
vsb_raw = APFS_SB(sb)->s_vsb_raw;
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, -1);
le64_add_cpu(&vsb_raw->apfs_total_blocks_freed, 1);
return 0;
case APFS_QUERY_FREE_QUEUE:
eph_info_end = &nxi->nx_eph_list[nxi->nx_eph_count];
eph_info = apfs_ephemeral_object_lookup(sb, node->object.oid);
if (IS_ERR(eph_info)) {
apfs_alert(sb, "can't find ephemeral object to delete");
return PTR_ERR(eph_info);
}
kfree(eph_info->object);
eph_info->object = NULL;
memmove(eph_info, eph_info + 1, (char *)eph_info_end - (char *)(eph_info + 1));
eph_info_end->object = NULL;
--nxi->nx_eph_count;
return 0;
default:
apfs_alert(sb, "new query type must implement node deletion (%d)", type);
return -EOPNOTSUPP;
}
}
/**
* apfs_update_node - Update an existing node header
* @node: the modified in-memory node
*/
void apfs_update_node(struct apfs_node *node)
{
struct super_block *sb = node->object.sb;
struct buffer_head *bh = node->object.o_bh;
struct apfs_btree_node_phys *raw = (void *)node->object.data;
struct apfs_nloc *free_head;
u32 tflags, type;
int toc_off;
apfs_assert_in_transaction(sb, &raw->btn_o);
raw->btn_o.o_oid = cpu_to_le64(node->object.oid);
/* The node may no longer be a root, so update the object type */
tflags = le32_to_cpu(raw->btn_o.o_type) & APFS_OBJECT_TYPE_FLAGS_MASK;
type = (node->flags & APFS_BTNODE_ROOT) ? APFS_OBJECT_TYPE_BTREE :
APFS_OBJECT_TYPE_BTREE_NODE;
raw->btn_o.o_type = cpu_to_le32(type | tflags);
raw->btn_o.o_subtype = cpu_to_le32(node->tree_type);
raw->btn_flags = cpu_to_le16(node->flags);
raw->btn_nkeys = cpu_to_le32(node->records);
toc_off = sizeof(*raw) + le16_to_cpu(raw->btn_table_space.off);
raw->btn_table_space.len = cpu_to_le16(node->key - toc_off);
raw->btn_free_space.off = cpu_to_le16(node->free - node->key);
raw->btn_free_space.len = cpu_to_le16(node->data - node->free);
/* Reset the lists on zero length, a defragmentation is taking place */
free_head = &raw->btn_key_free_list;
free_head->len = cpu_to_le16(node->key_free_list_len);
if (!free_head->len)
free_head->off = cpu_to_le16(APFS_BTOFF_INVALID);
free_head = &raw->btn_val_free_list;
free_head->len = cpu_to_le16(node->val_free_list_len);
if (!free_head->len)
free_head->off = cpu_to_le16(APFS_BTOFF_INVALID);
if (bh) {
ASSERT(buffer_trans(bh));
ASSERT(buffer_csum(bh));
}
}
/**
* apfs_node_locate_key - Locate the key of a node record
* @node: node to be searched
* @index: number of the entry to locate
* @off: on return will hold the offset in the block
*
* Returns the length of the key, or 0 in case of failure. The function checks
* that this length fits within the block; callers must use the returned value
* to make sure they never operate outside its bounds.
*/
int apfs_node_locate_key(struct apfs_node *node, int index, int *off)
{
struct super_block *sb = node->object.sb;
struct apfs_btree_node_phys *raw;
int len;
if (index >= node->records) {
apfs_err(sb, "index out of bounds (%d of %d)", index, node->records);
return 0;
}
raw = (struct apfs_btree_node_phys *)node->object.data;
if (apfs_node_has_fixed_kv_size(node)) {
struct apfs_kvoff *entry;
entry = (struct apfs_kvoff *)raw->btn_data + index;
/* TODO: it would be cleaner to read this stuff from disk */
if (node->tree_type == APFS_OBJECT_TYPE_OMAP_SNAPSHOT)
len = 8;
else
len = 16;
/* Translate offset in key area to offset in block */
*off = node->key + le16_to_cpu(entry->k);
} else {
/* These node types have variable length keys and data */
struct apfs_kvloc *entry;
entry = (struct apfs_kvloc *)raw->btn_data + index;
len = le16_to_cpu(entry->k.len);
/* Translate offset in key area to offset in block */
*off = node->key + le16_to_cpu(entry->k.off);
}
if (*off + len > sb->s_blocksize) {
apfs_err(sb, "key out of bounds (%d-%d)", *off, len);
return 0;
}
return len;
}
/**
* apfs_node_locate_data - Locate the data of a node record
* @node: node to be searched
* @index: number of the entry to locate
* @off: on return will hold the offset in the block
*
* Returns the length of the data, which may be 0 in case of corruption or if
* the record is a ghost. The function checks that this length fits within the
* block; callers must use the returned value to make sure they never operate
* outside its bounds.
*/
static int apfs_node_locate_data(struct apfs_node *node, int index, int *off)
{
struct super_block *sb = node->object.sb;
struct apfs_btree_node_phys *raw;
int len;
if (index >= node->records) {
apfs_err(sb, "index out of bounds (%d of %d)", index, node->records);
return 0;
}
raw = (struct apfs_btree_node_phys *)node->object.data;
if (apfs_node_has_fixed_kv_size(node)) {
/* These node types have fixed length keys and data */
struct apfs_kvoff *entry;
entry = (struct apfs_kvoff *)raw->btn_data + index;
if (node->tree_type == APFS_OBJECT_TYPE_SPACEMAN_FREE_QUEUE) {
/* A free-space queue record may have no value */
if (le16_to_cpu(entry->v) == APFS_BTOFF_INVALID) {
*off = 0;
return 0;
}
len = 8;
} else {
/* This is an omap or omap snapshots node */
len = apfs_node_is_leaf(node) ? 16 : 8;
}
/*
* Data offsets are counted backwards from the end of the
* block, or from the beginning of the footer when it exists
*/
if (apfs_node_is_root(node)) /* has footer */
*off = sb->s_blocksize - sizeof(struct apfs_btree_info)
- le16_to_cpu(entry->v);
else
*off = sb->s_blocksize - le16_to_cpu(entry->v);
} else {
/* These node types have variable length keys and data */
struct apfs_kvloc *entry;
entry = (struct apfs_kvloc *)raw->btn_data + index;
len = le16_to_cpu(entry->v.len);
/*
* Data offsets are counted backwards from the end of the
* block, or from the beginning of the footer when it exists
*/
if (apfs_node_is_root(node)) /* has footer */
*off = sb->s_blocksize - sizeof(struct apfs_btree_info)
- le16_to_cpu(entry->v.off);
else
*off = sb->s_blocksize - le16_to_cpu(entry->v.off);
}
if (*off < 0 || *off + len > sb->s_blocksize) {
apfs_err(sb, "value out of bounds (%d-%d)", *off, len);
return 0;
}
return len;
}
/**
* apfs_create_toc_entry - Create the table-of-contents entry for a record
* @query: query pointing to the record
*
* Creates a toc entry for the record at index @query->index and increases
* @node->records. The caller must ensure enough space in the table.
*/
static void apfs_create_toc_entry(struct apfs_query *query)
{
struct apfs_node *node = query->node;
struct super_block *sb = node->object.sb;
struct apfs_btree_node_phys *raw = (void *)node->object.data;
int value_end;
int recs = node->records;
int index = query->index;
value_end = sb->s_blocksize;
if (apfs_node_is_root(node))
value_end -= sizeof(struct apfs_btree_info);
if (apfs_node_has_fixed_kv_size(node)) {
struct apfs_kvoff *kvoff;
kvoff = (struct apfs_kvoff *)raw->btn_data + query->index;
memmove(kvoff + 1, kvoff, (recs - index) * sizeof(*kvoff));
if (!query->len) /* Ghost record */
kvoff->v = cpu_to_le16(APFS_BTOFF_INVALID);
else
kvoff->v = cpu_to_le16(value_end - query->off);
kvoff->k = cpu_to_le16(query->key_off - node->key);
} else {
struct apfs_kvloc *kvloc;
kvloc = (struct apfs_kvloc *)raw->btn_data + query->index;
memmove(kvloc + 1, kvloc, (recs - index) * sizeof(*kvloc));
kvloc->v.off = cpu_to_le16(value_end - query->off);
kvloc->v.len = cpu_to_le16(query->len);
kvloc->k.off = cpu_to_le16(query->key_off - node->key);
kvloc->k.len = cpu_to_le16(query->key_len);
}
node->records++;
}
/**
* apfs_key_from_query - Read the current key from a query structure
* @query: the query, with @query->key_off and @query->key_len already set
* @key: return parameter for the key
*
* Reads the key into @key and performs some basic sanity checks as a
* protection against crafted filesystems. Returns 0 on success or a
* negative error code otherwise.
*/
static int apfs_key_from_query(struct apfs_query *query, struct apfs_key *key)
{
struct super_block *sb = query->node->object.sb;
char *raw = query->node->object.data;
void *raw_key = (void *)(raw + query->key_off);
bool hashed;
int err = 0;
switch (query->flags & APFS_QUERY_TREE_MASK) {
case APFS_QUERY_CAT:
hashed = apfs_is_normalization_insensitive(sb);
err = apfs_read_cat_key(raw_key, query->key_len, key, hashed);
break;
case APFS_QUERY_OMAP:
err = apfs_read_omap_key(raw_key, query->key_len, key);
break;
case APFS_QUERY_FREE_QUEUE:
err = apfs_read_free_queue_key(raw_key, query->key_len, key);
break;
case APFS_QUERY_EXTENTREF:
err = apfs_read_extentref_key(raw_key, query->key_len, key);
break;
case APFS_QUERY_FEXT:
err = apfs_read_fext_key(raw_key, query->key_len, key);
break;
case APFS_QUERY_SNAP_META:
err = apfs_read_snap_meta_key(raw_key, query->key_len, key);
break;
case APFS_QUERY_OMAP_SNAP:
err = apfs_read_omap_snap_key(raw_key, query->key_len, key);
break;
default:
apfs_alert(sb, "new query type must implement key reads (%d)", query->flags & APFS_QUERY_TREE_MASK);
err = -EOPNOTSUPP;
break;
}
if (err)
apfs_err(sb, "bad node key in block 0x%llx", query->node->object.block_nr);
/* A multiple query must ignore some of these fields */
if (query->flags & APFS_QUERY_ANY_NAME)
key->name = NULL;
if (query->flags & APFS_QUERY_ANY_NUMBER)
key->number = 0;
return err;
}
/**
* apfs_node_prev - Find the previous record in the current node
* @sb: filesystem superblock
* @query: query in execution
*
* Returns 0 on success, -EAGAIN if the previous record is in another node,
* -ENODATA if no more records exist, or another negative error code in case
* of failure.
*
* The meaning of "next" and "previous" is reverted here, because regular
* multiple always start with the final record, and then they go backwards.
* TODO: consider renaming this for clarity.
*/
static int apfs_node_prev(struct super_block *sb, struct apfs_query *query)
{
struct apfs_node *node = query->node;
if (query->index + 1 == node->records) {
/* The next record may be in another node */
return -EAGAIN;
}
++query->index;
query->key_len = apfs_node_locate_key(node, query->index, &query->key_off);
if (query->key_len == 0) {
apfs_err(sb, "bad key for index %d", query->index);
return -EFSCORRUPTED;
}
query->len = apfs_node_locate_data(node, query->index, &query->off);
if (query->len == 0) {
apfs_err(sb, "bad value for index %d", query->index);
return -EFSCORRUPTED;
}
return 0;
}
/**
* apfs_node_next - Find the next matching record in the current node
* @sb: filesystem superblock
* @query: multiple query in execution
*
* Returns 0 on success, -EAGAIN if the next record is in another node,
* -ENODATA if no more matching records exist, or another negative error
* code in case of failure.
*/
static int apfs_node_next(struct super_block *sb, struct apfs_query *query)
{
struct apfs_node *node = query->node;
struct apfs_key curr_key;
int cmp, err;
if (query->flags & APFS_QUERY_DONE)
/* Nothing left to search; the query failed */
return -ENODATA;
if (!query->index) /* The next record may be in another node */
return -EAGAIN;
--query->index;
query->key_len = apfs_node_locate_key(node, query->index,
&query->key_off);
err = apfs_key_from_query(query, &curr_key);
if (err) {
apfs_err(sb, "bad key for index %d", query->index);
return err;
}
cmp = apfs_keycmp(&curr_key, &query->key);
if (cmp > 0) {
apfs_err(sb, "records are out of order");
return -EFSCORRUPTED;
}
if (cmp != 0 && apfs_node_is_leaf(node) &&
query->flags & APFS_QUERY_EXACT)
return -ENODATA;
query->len = apfs_node_locate_data(node, query->index, &query->off);
if (query->len == 0) {
apfs_err(sb, "bad value for index %d", query->index);
return -EFSCORRUPTED;
}
if (cmp != 0) {
/*
* This is the last entry that can be relevant in this node.
* Keep searching the children, but don't return to this level.
*/
query->flags |= APFS_QUERY_DONE;
}
return 0;
}
/**
* apfs_node_query - Execute a query on a single node
* @sb: filesystem superblock
* @query: the query to execute
*
* The search will start at index @query->index, looking for the key that comes
* right before @query->key, according to the order given by apfs_keycmp().
*
* The @query->index will be updated to the last index checked. This is
* important when searching for multiple entries, since the query may need
* to remember where it was on this level. If we are done with this node, the
* query will be flagged as APFS_QUERY_DONE, and the search will end in failure
* as soon as we return to this level. The function may also return -EAGAIN,
* to signal that the search should go on in a different branch.
*
* On success returns 0; the offset of the data within the block will be saved
* in @query->off, and its length in @query->len. The function checks that this
* length fits within the block; callers must use the returned value to make
* sure they never operate outside its bounds.
*
* -ENODATA will be returned if no appropriate entry was found, -EFSCORRUPTED
* in case of corruption.
*/
int apfs_node_query(struct super_block *sb, struct apfs_query *query)
{
struct apfs_node *node = query->node;
int left, right;
int cmp;
int err;
if (query->flags & APFS_QUERY_PREV)
return apfs_node_prev(sb, query);
if (query->flags & APFS_QUERY_NEXT)
return apfs_node_next(sb, query);
/* Search by bisection */
cmp = 1;
left = 0;
do {
struct apfs_key curr_key;
if (cmp > 0) {
right = query->index - 1;
if (right < left) {
query->index = -1;
return -ENODATA;
}
query->index = (left + right) / 2;
} else {
left = query->index;
query->index = DIV_ROUND_UP(left + right, 2);
}
query->key_len = apfs_node_locate_key(node, query->index,
&query->key_off);
err = apfs_key_from_query(query, &curr_key);
if (err) {
apfs_err(sb, "bad key for index %d", query->index);
return err;
}
cmp = apfs_keycmp(&curr_key, &query->key);
if (cmp == 0 && !(query->flags & APFS_QUERY_MULTIPLE))
break;
} while (left != right);
if (cmp > 0) {
query->index = -1;
return -ENODATA;
}
if (cmp != 0 && apfs_node_is_leaf(query->node) &&
query->flags & APFS_QUERY_EXACT)
return -ENODATA;
if (query->flags & APFS_QUERY_MULTIPLE) {
if (cmp != 0) /* Last relevant entry in level */
query->flags |= APFS_QUERY_DONE;
query->flags |= APFS_QUERY_NEXT;
}
query->len = apfs_node_locate_data(node, query->index, &query->off);
return 0;
}
/**
* apfs_node_query_first - Find the first record in a node
* @query: on return this query points to the record
*/
void apfs_node_query_first(struct apfs_query *query)
{
struct apfs_node *node = query->node;
query->index = 0;
query->key_len = apfs_node_locate_key(node, query->index, &query->key_off);
query->len = apfs_node_locate_data(node, query->index, &query->off);
}
/**
* apfs_omap_map_from_query - Read the mapping found by a successful omap query