forked from linux-apfs/linux-apfs-rw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.c
1544 lines (1353 loc) · 42.1 KB
/
dir.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 <[email protected]>
*/
#include <linux/slab.h>
#include <linux/buffer_head.h>
#include "apfs.h"
/**
* apfs_drec_from_query - Read the directory record found by a successful query
* @query: the query that found the record
* @drec: Return parameter. The directory record found.
* @hashed: is this record hashed?
*
* Reads the directory record into @drec and performs some basic sanity checks
* as a protection against crafted filesystems. Returns 0 on success or
* -EFSCORRUPTED otherwise.
*
* The caller must not free @query while @drec is in use, because @drec->name
* points to data on disk.
*/
static int apfs_drec_from_query(struct apfs_query *query, struct apfs_drec *drec, bool hashed)
{
struct super_block *sb = query->node->object.sb;
char *raw = query->node->object.data;
struct apfs_drec_hashed_key *de_hkey = NULL;
struct apfs_drec_key *de_ukey = NULL;
struct apfs_drec_val *de;
int namelen, xlen;
char *xval = NULL, *name;
namelen = query->key_len - (hashed ? sizeof(*de_hkey) : sizeof(*de_ukey));
if (namelen < 1) {
apfs_err(sb, "key is too small (%d)", query->key_len);
return -EFSCORRUPTED;
}
if (query->len < sizeof(*de)) {
apfs_err(sb, "value is too small (%d)", query->len);
return -EFSCORRUPTED;
}
de = (struct apfs_drec_val *)(raw + query->off);
if (hashed) {
de_hkey = (struct apfs_drec_hashed_key *)(raw + query->key_off);
if (namelen != (le32_to_cpu(de_hkey->name_len_and_hash) & APFS_DREC_LEN_MASK)) {
apfs_err(sb, "inconsistent name length");
return -EFSCORRUPTED;
}
name = de_hkey->name;
} else {
de_ukey = (struct apfs_drec_key *)(raw + query->key_off);
if (namelen != le16_to_cpu(de_ukey->name_len)) {
apfs_err(sb, "inconsistent name length");
return -EFSCORRUPTED;
}
name = de_ukey->name;
}
/* Filename must be NULL-terminated */
if (name[namelen - 1] != 0) {
apfs_err(sb, "null termination missing");
return -EFSCORRUPTED;
}
/* The dentry may have at most one xfield: the sibling id */
drec->sibling_id = 0;
xlen = apfs_find_xfield(de->xfields, query->len - sizeof(*de),
APFS_DREC_EXT_TYPE_SIBLING_ID, &xval);
if (xlen >= sizeof(__le64)) {
__le64 *sib_id = (__le64 *)xval;
drec->sibling_id = le64_to_cpup(sib_id);
}
drec->name = name;
drec->name_len = namelen - 1; /* Don't count the NULL termination */
drec->ino = le64_to_cpu(de->file_id);
drec->type = le16_to_cpu(de->flags) & APFS_DREC_TYPE_MASK;
if (drec->type != DT_FIFO && drec->type & 1) /* Invalid file type */
drec->type = DT_UNKNOWN;
return 0;
}
/**
* apfs_dentry_lookup - Lookup a dentry record in the catalog b-tree
* @dir: parent directory
* @child: filename
* @drec: on return, the directory record found
*
* Runs a catalog query for @name in the @dir directory. On success, sets
* @drec and returns a pointer to the query structure. On failure, returns
* an appropriate error pointer.
*/
static struct apfs_query *apfs_dentry_lookup(struct inode *dir,
const struct qstr *child,
struct apfs_drec *drec)
{
struct super_block *sb = dir->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
u64 cnid = apfs_ino(dir);
bool hashed = apfs_is_normalization_insensitive(sb);
int err;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return ERR_PTR(-ENOMEM);
apfs_init_drec_key(sb, cnid, child->name, child->len, &query->key);
/*
* Distinct filenames in the same directory may (rarely) share the same
* hash. The query code cannot handle that because their order in the
* b-tree would depend on their unnormalized original names. Just get
* all the candidates and check them one by one.
*
* This is very wasteful for normalization-sensitive filesystems: there
* are no hashes so we just check every single file in the directory for
* no reason. This would be easy to avoid but does it matter? (TODO)
*/
query->flags |= APFS_QUERY_CAT | APFS_QUERY_ANY_NAME | APFS_QUERY_EXACT;
do {
err = apfs_btree_query(sb, &query);
if (err)
goto fail;
err = apfs_drec_from_query(query, drec, hashed);
if (err)
goto fail;
} while (unlikely(apfs_filename_cmp(sb, child->name, child->len, drec->name, drec->name_len)));
/*
* We may need to refresh the query later, but the refresh code doesn't
* know how to deal with hash collisions. Instead set the key to the
* unnormalized name and pretend that this was never a multiple query
* in the first place.
*/
query->key.name = drec->name;
query->flags &= ~(APFS_QUERY_MULTIPLE | APFS_QUERY_DONE | APFS_QUERY_NEXT);
return query;
fail:
if (err != -ENODATA)
apfs_err(sb, "query failed in dir 0x%llx", cnid);
apfs_free_query(query);
return ERR_PTR(err);
}
/**
* apfs_inode_by_name - Find the cnid for a given filename
* @dir: parent directory
* @child: filename
* @ino: on return, the inode number found
*
* Returns 0 and the inode number (which is the cnid of the file
* record); otherwise, return the appropriate error code.
*/
int apfs_inode_by_name(struct inode *dir, const struct qstr *child, u64 *ino)
{
struct super_block *sb = dir->i_sb;
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct apfs_query *query;
struct apfs_drec drec;
int err = 0;
down_read(&nxi->nx_big_sem);
query = apfs_dentry_lookup(dir, child, &drec);
if (IS_ERR(query)) {
err = PTR_ERR(query);
goto out;
}
*ino = drec.ino;
apfs_free_query(query);
out:
up_read(&nxi->nx_big_sem);
return err;
}
static int apfs_readdir(struct file *file, struct dir_context *ctx)
{
struct inode *inode = file_inode(file);
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct apfs_query *query;
u64 cnid = apfs_ino(inode);
loff_t pos;
bool hashed = apfs_is_normalization_insensitive(sb);
int err = 0;
down_read(&nxi->nx_big_sem);
/* Inode numbers might overflow here; follow btrfs in ignoring that */
if (!dir_emit_dots(file, ctx))
goto out;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query) {
err = -ENOMEM;
goto out;
}
/* We want all the children for the cnid, regardless of the name */
apfs_init_drec_key(sb, cnid, NULL /* name */, 0 /* name_len */, &query->key);
query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT;
pos = ctx->pos - 2;
while (1) {
struct apfs_drec drec;
/*
* We query for the matching records, one by one. After we
* pass ctx->pos we begin to emit them.
*
* TODO: Faster approach for large directories?
*/
err = apfs_btree_query(sb, &query);
if (err == -ENODATA) { /* Got all the records */
err = 0;
break;
}
if (err)
break;
err = apfs_drec_from_query(query, &drec, hashed);
if (err) {
apfs_alert(sb, "bad dentry record in directory 0x%llx",
cnid);
break;
}
err = 0;
if (pos <= 0) {
if (!dir_emit(ctx, drec.name, drec.name_len,
drec.ino, drec.type))
break;
++ctx->pos;
}
pos--;
}
apfs_free_query(query);
out:
up_read(&nxi->nx_big_sem);
return err;
}
const struct file_operations apfs_dir_operations = {
.llseek = generic_file_llseek,
.read = generic_read_dir,
.iterate_shared = apfs_readdir,
.fsync = apfs_fsync,
.unlocked_ioctl = apfs_dir_ioctl,
};
/**
* apfs_build_dentry_unhashed_key - Allocate and initialize the key for an unhashed dentry record
* @qname: filename
* @parent_id: inode number for the parent of the dentry
* @key_p: on return, a pointer to the new on-disk key structure
*
* Returns the length of the key, or a negative error code in case of failure.
*/
static int apfs_build_dentry_unhashed_key(struct qstr *qname, u64 parent_id,
struct apfs_drec_key **key_p)
{
struct apfs_drec_key *key;
u16 namelen = qname->len + 1; /* We count the null-termination */
int key_len;
key_len = sizeof(*key) + namelen;
key = kmalloc(key_len, GFP_KERNEL);
if (!key)
return -ENOMEM;
apfs_key_set_hdr(APFS_TYPE_DIR_REC, parent_id, key);
key->name_len = cpu_to_le16(namelen);
strscpy(key->name, qname->name, namelen);
*key_p = key;
return key_len;
}
/**
* apfs_build_dentry_hashed_key - Allocate and initialize the key for a hashed dentry record
* @qname: filename
* @hash: filename hash
* @parent_id: inode number for the parent of the dentry
* @key_p: on return, a pointer to the new on-disk key structure
*
* Returns the length of the key, or a negative error code in case of failure.
*/
static int apfs_build_dentry_hashed_key(struct qstr *qname, u64 hash, u64 parent_id,
struct apfs_drec_hashed_key **key_p)
{
struct apfs_drec_hashed_key *key;
u16 namelen = qname->len + 1; /* We count the null-termination */
int key_len;
key_len = sizeof(*key) + namelen;
key = kmalloc(key_len, GFP_KERNEL);
if (!key)
return -ENOMEM;
apfs_key_set_hdr(APFS_TYPE_DIR_REC, parent_id, key);
key->name_len_and_hash = cpu_to_le32(namelen | hash);
strscpy(key->name, qname->name, namelen);
*key_p = key;
return key_len;
}
/**
* apfs_build_dentry_val - Allocate and initialize the value for a dentry record
* @inode: vfs inode for the dentry
* @sibling_id: sibling id for this hardlink (0 for none)
* @val_p: on return, a pointer to the new on-disk value structure
*
* Returns the length of the value, or a negative error code in case of failure.
*/
static int apfs_build_dentry_val(struct inode *inode, u64 sibling_id,
struct apfs_drec_val **val_p)
{
struct apfs_drec_val *val;
struct apfs_x_field xkey;
int total_xlen = 0, val_len;
__le64 raw_sibling_id = cpu_to_le64(sibling_id);
struct timespec64 now = current_time(inode);
/* The dentry record may have one xfield: the sibling id */
if (sibling_id)
total_xlen += sizeof(struct apfs_xf_blob) +
sizeof(xkey) + sizeof(raw_sibling_id);
val_len = sizeof(*val) + total_xlen;
val = kmalloc(val_len, GFP_KERNEL);
if (!val)
return -ENOMEM;
*val_p = val;
val->file_id = cpu_to_le64(apfs_ino(inode));
val->date_added = cpu_to_le64(timespec64_to_ns(&now));
val->flags = cpu_to_le16((inode->i_mode >> 12) & 15); /* File type */
if (!sibling_id)
return val_len;
/* The buffer was just allocated: none of these functions should fail */
apfs_init_xfields(val->xfields, val_len - sizeof(*val));
xkey.x_type = APFS_DREC_EXT_TYPE_SIBLING_ID;
xkey.x_flags = 0; /* TODO: proper flags here? */
xkey.x_size = cpu_to_le16(sizeof(raw_sibling_id));
apfs_insert_xfield(val->xfields, total_xlen, &xkey, &raw_sibling_id);
return val_len;
}
/**
* apfs_create_dentry_rec - Create a dentry record in the catalog b-tree
* @inode: vfs inode for the dentry
* @qname: filename
* @parent_id: inode number for the parent of the dentry
* @sibling_id: sibling id for this hardlink (0 for none)
*
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_create_dentry_rec(struct inode *inode, struct qstr *qname,
u64 parent_id, u64 sibling_id)
{
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
void *raw_key = NULL;
struct apfs_drec_val *raw_val = NULL;
int key_len, val_len;
bool hashed = apfs_is_normalization_insensitive(sb);
int ret;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_drec_key(sb, parent_id, qname->name, qname->len, &query->key);
query->flags |= APFS_QUERY_CAT;
ret = apfs_btree_query(sb, &query);
if (ret && ret != -ENODATA) {
apfs_err(sb, "query failed in dir 0x%llx (hash 0x%llx)", parent_id, query->key.number);
goto fail;
}
if (hashed)
key_len = apfs_build_dentry_hashed_key(qname, query->key.number, parent_id,
(struct apfs_drec_hashed_key **)&raw_key);
else
key_len = apfs_build_dentry_unhashed_key(qname, parent_id,
(struct apfs_drec_key **)&raw_key);
if (key_len < 0) {
ret = key_len;
goto fail;
}
val_len = apfs_build_dentry_val(inode, sibling_id, &raw_val);
if (val_len < 0) {
ret = val_len;
goto fail;
}
/* TODO: deal with hash collisions */
ret = apfs_btree_insert(query, raw_key, key_len, raw_val, val_len);
if (ret)
apfs_err(sb, "insertion failed in dir 0x%llx (hash 0x%llx)", parent_id, query->key.number);
fail:
kfree(raw_val);
kfree(raw_key);
apfs_free_query(query);
return ret;
}
#define APFS_CREATE_DENTRY_REC_MAXOPS 1
/**
* apfs_build_sibling_val - Allocate and initialize a sibling link's value
* @dentry: in-memory dentry for this hardlink
* @val_p: on return, a pointer to the new on-disk value structure
*
* Returns the length of the value, or a negative error code in case of failure.
*/
static int apfs_build_sibling_val(struct dentry *dentry,
struct apfs_sibling_val **val_p)
{
struct apfs_sibling_val *val;
struct qstr *qname = &dentry->d_name;
u16 namelen = qname->len + 1; /* We count the null-termination */
struct inode *parent = d_inode(dentry->d_parent);
int val_len;
val_len = sizeof(*val) + namelen;
val = kmalloc(val_len, GFP_KERNEL);
if (!val)
return -ENOMEM;
val->parent_id = cpu_to_le64(apfs_ino(parent));
val->name_len = cpu_to_le16(namelen);
strscpy(val->name, qname->name, namelen);
*val_p = val;
return val_len;
}
/**
* apfs_create_sibling_link_rec - Create a sibling link record for a dentry
* @dentry: the in-memory dentry
* @inode: vfs inode for the dentry
* @sibling_id: sibling id for this hardlink
*
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_create_sibling_link_rec(struct dentry *dentry,
struct inode *inode, u64 sibling_id)
{
struct super_block *sb = dentry->d_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query = NULL;
struct apfs_sibling_link_key raw_key;
struct apfs_sibling_val *raw_val;
int val_len;
int ret;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_sibling_link_key(apfs_ino(inode), sibling_id, &query->key);
query->flags |= APFS_QUERY_CAT;
ret = apfs_btree_query(sb, &query);
if (ret && ret != -ENODATA) {
apfs_err(sb, "query failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
goto fail;
}
apfs_key_set_hdr(APFS_TYPE_SIBLING_LINK, apfs_ino(inode), &raw_key);
raw_key.sibling_id = cpu_to_le64(sibling_id);
val_len = apfs_build_sibling_val(dentry, &raw_val);
if (val_len < 0)
goto fail;
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), raw_val, val_len);
if (ret)
apfs_err(sb, "insertion failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
kfree(raw_val);
fail:
apfs_free_query(query);
return ret;
}
#define APFS_CREATE_SIBLING_LINK_REC_MAXOPS 1
/**
* apfs_create_sibling_map_rec - Create a sibling map record for a dentry
* @dentry: the in-memory dentry
* @inode: vfs inode for the dentry
* @sibling_id: sibling id for this hardlink
*
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_create_sibling_map_rec(struct dentry *dentry,
struct inode *inode, u64 sibling_id)
{
struct super_block *sb = dentry->d_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query = NULL;
struct apfs_sibling_map_key raw_key;
struct apfs_sibling_map_val raw_val;
int ret;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_sibling_map_key(sibling_id, &query->key);
query->flags |= APFS_QUERY_CAT;
ret = apfs_btree_query(sb, &query);
if (ret && ret != -ENODATA) {
apfs_err(sb, "query failed for sibling 0x%llx", sibling_id);
goto fail;
}
apfs_key_set_hdr(APFS_TYPE_SIBLING_MAP, sibling_id, &raw_key);
raw_val.file_id = cpu_to_le64(apfs_ino(inode));
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val));
if (ret)
apfs_err(sb, "insertion failed for sibling 0x%llx", sibling_id);
fail:
apfs_free_query(query);
return ret;
}
#define APFS_CREATE_SIBLING_MAP_REC_MAXOPS 1
/**
* apfs_create_sibling_recs - Create sibling link and map records for a dentry
* @dentry: the in-memory dentry
* @inode: vfs inode for the dentry
* @sibling_id: on return, the sibling id for this hardlink
*
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_create_sibling_recs(struct dentry *dentry,
struct inode *inode, u64 *sibling_id)
{
struct super_block *sb = dentry->d_sb;
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
u64 cnid;
int ret;
/* Sibling ids come from the same pool as the inode numbers */
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
cnid = le64_to_cpu(vsb_raw->apfs_next_obj_id);
le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1);
ret = apfs_create_sibling_link_rec(dentry, inode, cnid);
if (ret)
return ret;
ret = apfs_create_sibling_map_rec(dentry, inode, cnid);
if (ret)
return ret;
*sibling_id = cnid;
return 0;
}
#define APFS_CREATE_SIBLING_RECS_MAXOPS (APFS_CREATE_SIBLING_LINK_REC_MAXOPS + \
APFS_CREATE_SIBLING_MAP_REC_MAXOPS)
/**
* apfs_create_dentry - Create all records for a new dentry
* @dentry: the in-memory dentry
* @inode: vfs inode for the dentry
*
* Creates the dentry record itself, as well as the sibling records if needed;
* also updates the child count for the parent inode. Returns 0 on success or
* a negative error code in case of failure.
*/
static int apfs_create_dentry(struct dentry *dentry, struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct inode *parent = d_inode(dentry->d_parent);
u64 sibling_id = 0;
int err;
if (inode->i_nlink > 1) {
/* This is optional for a single link, so don't waste space */
err = apfs_create_sibling_recs(dentry, inode, &sibling_id);
if (err) {
apfs_err(sb, "failed to create sibling recs for ino 0x%llx", apfs_ino(inode));
return err;
}
}
err = apfs_create_dentry_rec(inode, &dentry->d_name, apfs_ino(parent), sibling_id);
if (err) {
apfs_err(sb, "failed to create drec for ino 0x%llx", apfs_ino(inode));
return err;
}
/* Now update the parent inode */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
parent->i_mtime = parent->i_ctime = current_time(inode);
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
parent->i_mtime = inode_set_ctime_current(parent);
#else
inode_set_mtime_to_ts(parent, inode_set_ctime_current(parent));
#endif
++APFS_I(parent)->i_nchildren;
apfs_inode_join_transaction(parent->i_sb, parent);
return 0;
}
#define APFS_CREATE_DENTRY_MAXOPS (APFS_CREATE_SIBLING_RECS_MAXOPS + \
APFS_CREATE_DENTRY_REC_MAXOPS + \
APFS_UPDATE_INODE_MAXOPS())
/**
* apfs_undo_create_dentry - Clean up apfs_create_dentry()
* @dentry: the in-memory dentry
*/
static void apfs_undo_create_dentry(struct dentry *dentry)
{
struct inode *parent = d_inode(dentry->d_parent);
--APFS_I(parent)->i_nchildren;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 19, 0)
static inline void discard_new_inode(struct inode *inode)
{
unlock_new_inode(inode);
iput(inode);
}
#endif
int apfs_mkany(struct inode *dir, struct dentry *dentry, umode_t mode,
dev_t rdev, const char *symname)
{
struct super_block *sb = dir->i_sb;
struct inode *inode;
struct apfs_max_ops maxops;
int err;
maxops.cat = APFS_CREATE_INODE_REC_MAXOPS() + APFS_CREATE_DENTRY_MAXOPS;
if (symname)
maxops.cat += APFS_XATTR_SET_MAXOPS();
maxops.blks = 0;
err = apfs_transaction_start(sb, maxops);
if (err)
return err;
inode = apfs_new_inode(dir, mode, rdev);
if (IS_ERR(inode)) {
err = PTR_ERR(inode);
goto out_abort;
}
err = apfs_create_inode_rec(sb, inode, dentry);
if (err) {
apfs_err(sb, "failed to create inode rec for ino 0x%llx", apfs_ino(inode));
goto out_discard_inode;
}
err = apfs_create_dentry(dentry, inode);
if (err) {
apfs_err(sb, "failed to create dentry recs for ino 0x%llx", apfs_ino(inode));
goto out_discard_inode;
}
if (symname) {
err = apfs_xattr_set(inode, APFS_XATTR_NAME_SYMLINK, symname,
strlen(symname) + 1, 0 /* flags */);
if (err == -ERANGE) {
err = -ENAMETOOLONG;
goto out_undo_create;
}
if (err) {
apfs_err(sb, "failed to set symlink xattr for ino 0x%llx", apfs_ino(inode));
goto out_undo_create;
}
}
err = apfs_transaction_commit(sb);
if (err)
goto out_undo_create;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
/* Apparently there is a lockdep bug here, but it doesn't matter */
unlock_new_inode(inode);
d_instantiate(dentry, inode);
#else
d_instantiate_new(dentry, inode);
#endif
return 0;
out_undo_create:
apfs_undo_create_dentry(dentry);
out_discard_inode:
/* Don't reset nlink: on-disk cleanup is unneeded and would deadlock */
discard_new_inode(inode);
out_abort:
apfs_transaction_abort(sb);
return err;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
int apfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
dev_t rdev)
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0)
int apfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, umode_t mode, dev_t rdev)
#else
int apfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode, dev_t rdev)
#endif
{
return apfs_mkany(dir, dentry, mode, rdev, NULL /* symname */);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
int apfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
{
return apfs_mknod(dir, dentry, mode | S_IFDIR, 0 /* rdev */);
}
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0)
int apfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, umode_t mode)
{
return apfs_mknod(mnt_userns, dir, dentry, mode | S_IFDIR, 0 /* rdev */);
}
#else
int apfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode)
{
return apfs_mknod(idmap, dir, dentry, mode | S_IFDIR, 0 /* rdev */);
}
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 12, 0)
int apfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
bool excl)
{
return apfs_mknod(dir, dentry, mode, 0 /* rdev */);
}
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 3, 0)
int apfs_create(struct user_namespace *mnt_userns, struct inode *dir,
struct dentry *dentry, umode_t mode, bool excl)
{
return apfs_mknod(mnt_userns, dir, dentry, mode, 0 /* rdev */);
}
#else
int apfs_create(struct mnt_idmap *idmap, struct inode *dir,
struct dentry *dentry, umode_t mode, bool excl)
{
return apfs_mknod(idmap, dir, dentry, mode, 0 /* rdev */);
}
#endif
/**
* apfs_prepare_dentry_for_link - Assign a sibling id and records to a dentry
* @dentry: the in-memory dentry (should be for a primary link)
*
* Returns 0 on success, or a negative error code in case of failure.
*/
static int apfs_prepare_dentry_for_link(struct dentry *dentry)
{
struct inode *parent = d_inode(dentry->d_parent);
struct super_block *sb = parent->i_sb;
struct apfs_query *query;
struct apfs_drec drec;
u64 sibling_id;
int ret;
query = apfs_dentry_lookup(parent, &dentry->d_name, &drec);
if (IS_ERR(query)) {
apfs_err(sb, "lookup failed in dir 0x%llx", apfs_ino(parent));
return PTR_ERR(query);
}
if (drec.sibling_id) {
/* This dentry already has a sibling id xfield */
apfs_free_query(query);
return 0;
}
/* Don't modify the dentry record, just delete it to make a new one */
ret = apfs_btree_remove(query);
apfs_free_query(query);
if (ret) {
apfs_err(sb, "removal failed in dir 0x%llx", apfs_ino(parent));
return ret;
}
ret = apfs_create_sibling_recs(dentry, d_inode(dentry), &sibling_id);
if (ret) {
apfs_err(sb, "failed to create sibling recs in dir 0x%llx", apfs_ino(parent));
return ret;
}
return apfs_create_dentry_rec(d_inode(dentry), &dentry->d_name,
apfs_ino(parent), sibling_id);
}
#define APFS_PREPARE_DENTRY_FOR_LINK_MAXOPS (1 + APFS_CREATE_SIBLING_RECS_MAXOPS + \
APFS_CREATE_DENTRY_REC_MAXOPS)
/**
* __apfs_undo_link - Clean up __apfs_link()
* @dentry: the in-memory dentry
* @inode: target inode
*/
static void __apfs_undo_link(struct dentry *dentry, struct inode *inode)
{
apfs_undo_create_dentry(dentry);
drop_nlink(inode);
}
/**
* __apfs_link - Link a dentry
* @old_dentry: dentry for the old link
* @dentry: new dentry to link
*
* Does the same as apfs_link(), but without starting a transaction, taking a
* new reference to @old_dentry->d_inode, or instantiating @dentry.
*/
static int __apfs_link(struct dentry *old_dentry, struct dentry *dentry)
{
struct inode *inode = d_inode(old_dentry);
struct super_block *sb = inode->i_sb;
int err;
/* First update the inode's link count */
inc_nlink(inode);
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
inode->i_ctime = current_time(inode);
#else
inode_set_ctime_current(inode);
#endif
apfs_inode_join_transaction(inode->i_sb, inode);
if (inode->i_nlink == 2) {
/* A single link may lack sibling records, so create them now */
err = apfs_prepare_dentry_for_link(old_dentry);
if (err) {
apfs_err(sb, "failed to prepare original dentry");
goto fail;
}
}
err = apfs_create_dentry(dentry, inode);
if (err) {
apfs_err(sb, "failed to create new dentry");
goto fail;
}
return 0;
fail:
drop_nlink(inode);
return err;
}
#define __APFS_LINK_MAXOPS (APFS_UPDATE_INODE_MAXOPS() + \
APFS_PREPARE_DENTRY_FOR_LINK_MAXOPS + \
APFS_CREATE_DENTRY_MAXOPS)
int apfs_link(struct dentry *old_dentry, struct inode *dir,
struct dentry *dentry)
{
struct super_block *sb = dir->i_sb;
struct inode *inode = d_inode(old_dentry);
struct apfs_max_ops maxops;
int err;
maxops.cat = __APFS_LINK_MAXOPS;
maxops.blks = 0;
err = apfs_transaction_start(sb, maxops);
if (err)
return err;
err = __apfs_link(old_dentry, dentry);
if (err)
goto out_abort;
ihold(inode);
err = apfs_transaction_commit(sb);
if (err)
goto out_undo_link;
d_instantiate(dentry, inode);
return 0;
out_undo_link:
iput(inode);
__apfs_undo_link(dentry, inode);
out_abort:
apfs_transaction_abort(sb);
return err;
}
/**
* apfs_delete_sibling_link_rec - Delete the sibling link record for a dentry
* @dentry: the in-memory dentry
* @sibling_id: sibling id for this hardlink
*
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_delete_sibling_link_rec(struct dentry *dentry, u64 sibling_id)
{
struct super_block *sb = dentry->d_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct inode *inode = d_inode(dentry);
struct apfs_query *query = NULL;
int ret;
ASSERT(sibling_id);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_sibling_link_key(apfs_ino(inode), sibling_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret == -ENODATA) {
/* A dentry with a sibling id must have sibling records */
ret = -EFSCORRUPTED;
}
if (ret) {
apfs_err(sb, "query failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
goto fail;
}
ret = apfs_btree_remove(query);
if (ret)
apfs_err(sb, "removal failed for ino 0x%llx, sibling 0x%llx", apfs_ino(inode), sibling_id);
fail:
apfs_free_query(query);
return ret;
}
#define APFS_DELETE_SIBLING_LINK_REC_MAXOPS 1
/**
* apfs_delete_sibling_map_rec - Delete the sibling map record for a dentry
* @dentry: the in-memory dentry
* @sibling_id: sibling id for this hardlink
*
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_delete_sibling_map_rec(struct dentry *dentry, u64 sibling_id)
{
struct super_block *sb = dentry->d_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query = NULL;
int ret;
ASSERT(sibling_id);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_sibling_map_key(sibling_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret == -ENODATA) {
/* A dentry with a sibling id must have sibling records */
ret = -EFSCORRUPTED;
}
if (ret) {
apfs_err(sb, "query failed for sibling 0x%llx", sibling_id);
goto fail;
}
ret = apfs_btree_remove(query);
if (ret)
apfs_err(sb, "removal failed for sibling 0x%llx", sibling_id);
fail:
apfs_free_query(query);
return ret;
}
#define APFS_DELETE_SIBLING_MAP_REC_MAXOPS 1
/**