-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathinode.c
2597 lines (2249 loc) · 70.5 KB
/
inode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <linux/mount.h>
#include <linux/mpage.h>
#include <linux/blk_types.h>
#include "apfs.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
#include <linux/sched/mm.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 13, 0)
#include <linux/fileattr.h>
#endif
#define MAX_PFK_LEN 512
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
static int apfs_read_folio(struct file *file, struct folio *folio)
{
return mpage_read_folio(folio, apfs_get_block);
}
#else
static int apfs_readpage(struct file *file, struct page *page)
{
return mpage_readpage(page, apfs_get_block);
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) /* Misses mpage_readpages() */
static void apfs_readahead(struct readahead_control *rac)
{
mpage_readahead(rac, apfs_get_block);
}
#else /* LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) */
static int apfs_readpages(struct file *file, struct address_space *mapping,
struct list_head *pages, unsigned int nr_pages)
{
return mpage_readpages(mapping, pages, nr_pages, apfs_get_block);
}
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0) */
/**
* apfs_create_dstream_rec - Create a data stream record
* @dstream: data stream info
*
* Does nothing if the record already exists. TODO: support cloned files.
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_create_dstream_rec(struct apfs_dstream_info *dstream)
{
struct super_block *sb = dstream->ds_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
struct apfs_dstream_id_key raw_key;
struct apfs_dstream_id_val raw_val;
int ret;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_dstream_id_key(dstream->ds_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret != -ENODATA) /* Either an error, or the record already exists */
goto out;
apfs_key_set_hdr(APFS_TYPE_DSTREAM_ID, dstream->ds_id, &raw_key);
raw_val.refcnt = cpu_to_le32(1);
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val));
if (ret) {
apfs_err(sb, "insertion failed for id 0x%llx", dstream->ds_id);
goto out;
}
out:
apfs_free_query(query);
return ret;
}
#define APFS_CREATE_DSTREAM_REC_MAXOPS 1
static int apfs_check_dstream_refcnt(struct inode *inode);
static int apfs_put_dstream_rec(struct apfs_dstream_info *dstream);
/**
* apfs_inode_create_exclusive_dstream - Make an inode's dstream not shared
* @inode: the vfs inode
*
* Returns 0 on success, or a negative error code in case of failure.
*/
int apfs_inode_create_exclusive_dstream(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct apfs_superblock *vsb_raw = APFS_SB(sb)->s_vsb_raw;
struct apfs_inode_info *ai = APFS_I(inode);
struct apfs_dstream_info *dstream = &ai->i_dstream;
u64 new_id;
int err;
if (!ai->i_has_dstream || !dstream->ds_shared)
return 0;
/*
* The ds_shared field is not updated when the other user of the
* dstream puts it, so it could be a false positive. Check it again
* before actually putting the dstream. The double query is wasteful,
* but I don't know if it makes sense to optimize this (TODO).
*/
err = apfs_check_dstream_refcnt(inode);
if (err) {
apfs_err(sb, "failed to check refcnt for ino 0x%llx", apfs_ino(inode));
return err;
}
if (!dstream->ds_shared)
return 0;
err = apfs_put_dstream_rec(dstream);
if (err) {
apfs_err(sb, "failed to put dstream for ino 0x%llx", apfs_ino(inode));
return err;
}
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
new_id = le64_to_cpu(vsb_raw->apfs_next_obj_id);
le64_add_cpu(&vsb_raw->apfs_next_obj_id, 1);
err = apfs_clone_extents(dstream, new_id);
if (err) {
apfs_err(sb, "failed clone extents for ino 0x%llx", apfs_ino(inode));
return err;
}
dstream->ds_id = new_id;
err = apfs_create_dstream_rec(dstream);
if (err) {
apfs_err(sb, "failed to create dstream for ino 0x%llx", apfs_ino(inode));
return err;
}
dstream->ds_shared = false;
return 0;
}
/**
* apfs_inode_create_dstream_rec - Create the data stream record for an inode
* @inode: the vfs inode
*
* Does nothing if the record already exists. TODO: support cloned files.
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_inode_create_dstream_rec(struct inode *inode)
{
struct apfs_inode_info *ai = APFS_I(inode);
int err;
if (ai->i_has_dstream)
return apfs_inode_create_exclusive_dstream(inode);
err = apfs_create_dstream_rec(&ai->i_dstream);
if (err)
return err;
ai->i_has_dstream = true;
return 0;
}
/**
* apfs_dstream_adj_refcnt - Adjust dstream record refcount
* @dstream: data stream info
* @delta: desired change in reference count
*
* Deletes the record if the reference count goes to zero. Returns 0 on success
* or a negative error code in case of failure.
*/
int apfs_dstream_adj_refcnt(struct apfs_dstream_info *dstream, u32 delta)
{
struct super_block *sb = dstream->ds_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
struct apfs_dstream_id_val raw_val;
void *raw = NULL;
u32 refcnt;
int ret;
ASSERT(APFS_I(dstream->ds_inode)->i_has_dstream);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_dstream_id_key(dstream->ds_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret) {
apfs_err(sb, "query failed for id 0x%llx", dstream->ds_id);
if (ret == -ENODATA)
ret = -EFSCORRUPTED;
goto out;
}
if (query->len != sizeof(raw_val)) {
apfs_err(sb, "bad value length (%d)", query->len);
ret = -EFSCORRUPTED;
goto out;
}
raw = query->node->object.data;
raw_val = *(struct apfs_dstream_id_val *)(raw + query->off);
refcnt = le32_to_cpu(raw_val.refcnt);
refcnt += delta;
if (refcnt == 0) {
ret = apfs_btree_remove(query);
if (ret)
apfs_err(sb, "removal failed for id 0x%llx", dstream->ds_id);
goto out;
}
raw_val.refcnt = cpu_to_le32(refcnt);
ret = apfs_btree_replace(query, NULL /* key */, 0 /* key_len */, &raw_val, sizeof(raw_val));
if (ret)
apfs_err(sb, "update failed for id 0x%llx", dstream->ds_id);
out:
apfs_free_query(query);
return ret;
}
/**
* apfs_put_dstream_rec - Put a reference for a data stream record
* @dstream: data stream info
*
* Deletes the record if the reference count goes to zero. Returns 0 on success
* or a negative error code in case of failure.
*/
static int apfs_put_dstream_rec(struct apfs_dstream_info *dstream)
{
struct apfs_inode_info *ai = APFS_I(dstream->ds_inode);
if (!ai->i_has_dstream)
return 0;
return apfs_dstream_adj_refcnt(dstream, -1);
}
/**
* apfs_create_crypto_rec - Create the crypto state record for an inode
* @inode: the vfs inode
*
* Does nothing if the record already exists. TODO: support cloned files.
* Returns 0 on success or a negative error code in case of failure.
*/
static int apfs_create_crypto_rec(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
struct apfs_query *query;
struct apfs_crypto_state_key raw_key;
int ret;
if (inode->i_size || inode->i_blocks) /* Already has a dstream */
return 0;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_crypto_state_key(dstream->ds_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret != -ENODATA) /* Either an error, or the record already exists */
goto out;
apfs_key_set_hdr(APFS_TYPE_CRYPTO_STATE, dstream->ds_id, &raw_key);
if (sbi->s_dflt_pfk) {
struct apfs_crypto_state_val *raw_val = sbi->s_dflt_pfk;
unsigned int key_len = le16_to_cpu(raw_val->state.key_len);
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), raw_val, sizeof(*raw_val) + key_len);
if (ret)
apfs_err(sb, "insertion failed for id 0x%llx", dstream->ds_id);
} else {
struct apfs_crypto_state_val raw_val;
raw_val.refcnt = cpu_to_le32(1);
raw_val.state.major_version = cpu_to_le16(APFS_WMCS_MAJOR_VERSION);
raw_val.state.minor_version = cpu_to_le16(APFS_WMCS_MINOR_VERSION);
raw_val.state.cpflags = 0;
raw_val.state.persistent_class = cpu_to_le32(APFS_PROTECTION_CLASS_F);
raw_val.state.key_os_version = 0;
raw_val.state.key_revision = cpu_to_le16(1);
raw_val.state.key_len = cpu_to_le16(0);
ret = apfs_btree_insert(query, &raw_key, sizeof(raw_key), &raw_val, sizeof(raw_val));
if (ret)
apfs_err(sb, "insertion failed for id 0x%llx", dstream->ds_id);
}
out:
apfs_free_query(query);
return ret;
}
#define APFS_CREATE_CRYPTO_REC_MAXOPS 1
/**
* apfs_dflt_key_class - Returns default key class for files in volume
* @sb: volume superblock
*/
static unsigned int apfs_dflt_key_class(struct super_block *sb)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
if (!sbi->s_dflt_pfk)
return APFS_PROTECTION_CLASS_F;
return le32_to_cpu(sbi->s_dflt_pfk->state.persistent_class);
}
/**
* apfs_create_crypto_rec - Adjust crypto state record refcount
* @sb: volume superblock
* @crypto_id: crypto_id to adjust
* @delta: desired change in reference count
*
* This function is used when adding or removing extents, as each extent holds
* a reference to the crypto ID. It should also be used when removing inodes,
* and in that case it should also remove the crypto record (TODO).
*/
int apfs_crypto_adj_refcnt(struct super_block *sb, u64 crypto_id, int delta)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
struct apfs_crypto_state_val *raw_val;
char *raw;
int ret;
if (!crypto_id)
return 0;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_crypto_state_key(crypto_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret) {
apfs_err(sb, "query failed for id 0x%llx", crypto_id);
goto out;
}
ret = apfs_query_join_transaction(query);
if (ret) {
apfs_err(sb, "query join failed");
return ret;
}
raw = query->node->object.data;
raw_val = (void *)raw + query->off;
le32_add_cpu(&raw_val->refcnt, delta);
out:
apfs_free_query(query);
return ret;
}
int APFS_CRYPTO_ADJ_REFCNT_MAXOPS(void)
{
return 1;
}
/**
* apfs_crypto_set_key - Modify content of crypto state record
* @sb: volume superblock
* @crypto_id: crypto_id to modify
* @new_val: new crypto state data; new_val->refcnt is overridden
*
* This function does not alter the inode's default protection class field.
* It needs to be done separately if the class changes.
*/
static int apfs_crypto_set_key(struct super_block *sb, u64 crypto_id, struct apfs_crypto_state_val *new_val)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
struct apfs_crypto_state_val *raw_val;
char *raw;
int ret;
unsigned int pfk_len;
if (!crypto_id)
return 0;
pfk_len = le16_to_cpu(new_val->state.key_len);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_crypto_state_key(crypto_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret) {
apfs_err(sb, "query failed for id 0x%llx", crypto_id);
goto out;
}
raw = query->node->object.data;
raw_val = (void *)raw + query->off;
new_val->refcnt = raw_val->refcnt;
ret = apfs_btree_replace(query, NULL /* key */, 0 /* key_len */, new_val, sizeof(*new_val) + pfk_len);
if (ret)
apfs_err(sb, "update failed for id 0x%llx", crypto_id);
out:
apfs_free_query(query);
return ret;
}
#define APFS_CRYPTO_SET_KEY_MAXOPS 1
/**
* apfs_crypto_get_key - Retrieve content of crypto state record
* @sb: volume superblock
* @crypto_id: crypto_id to modify
* @val: result crypto state data
* @max_len: maximum allowed value of val->state.key_len
*/
static int apfs_crypto_get_key(struct super_block *sb, u64 crypto_id, struct apfs_crypto_state_val *val,
unsigned int max_len)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
struct apfs_crypto_state_val *raw_val;
char *raw;
int ret;
unsigned int pfk_len;
if (!crypto_id)
return -ENOENT;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_crypto_state_key(crypto_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret)
goto out;
raw = query->node->object.data;
raw_val = (void *)raw + query->off;
pfk_len = le16_to_cpu(raw_val->state.key_len);
if (pfk_len > max_len) {
ret = -ENOSPC;
goto out;
}
memcpy(val, raw_val, sizeof(*val) + pfk_len);
out:
apfs_free_query(query);
return ret;
}
int __apfs_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int flags, struct page **pagep, void **fsdata)
{
struct inode *inode = mapping->host;
struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
struct super_block *sb = inode->i_sb;
struct page *page;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0)
struct folio *folio;
#endif
struct buffer_head *bh, *head;
unsigned int blocksize, block_start, block_end, from, to;
pgoff_t index = pos >> PAGE_SHIFT;
sector_t iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
loff_t i_blks_end;
int err;
apfs_inode_join_transaction(sb, inode);
err = apfs_inode_create_dstream_rec(inode);
if (err) {
apfs_err(sb, "failed to create dstream for ino 0x%llx", apfs_ino(inode));
return err;
}
if (apfs_vol_is_encrypted(sb)) {
err = apfs_create_crypto_rec(inode);
if (err) {
apfs_err(sb, "crypto creation failed for ino 0x%llx", apfs_ino(inode));
return err;
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
flags = memalloc_nofs_save();
page = grab_cache_page_write_begin(mapping, index);
memalloc_nofs_restore(flags);
#else
page = grab_cache_page_write_begin(mapping, index, flags | AOP_FLAG_NOFS);
#endif
if (!page)
return -ENOMEM;
if (!page_has_buffers(page)) {
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
create_empty_buffers(page, sb->s_blocksize, 0);
#else
folio = page_folio(page);
bh = folio_buffers(folio);
if (!bh)
bh = create_empty_buffers(folio, sb->s_blocksize, 0);
#endif
}
/* CoW moves existing blocks, so read them but mark them as unmapped */
head = page_buffers(page);
blocksize = head->b_size;
i_blks_end = (inode->i_size + sb->s_blocksize - 1) >> inode->i_blkbits;
i_blks_end <<= inode->i_blkbits;
if (i_blks_end >= pos) {
from = pos & (PAGE_SIZE - 1);
to = from + min(i_blks_end - pos, (loff_t)len);
} else {
/* TODO: deal with preallocated tail blocks */
from = UINT_MAX;
to = 0;
}
for (bh = head, block_start = 0; bh != head || !block_start;
block_start = block_end, bh = bh->b_this_page, ++iblock) {
block_end = block_start + blocksize;
if (to > block_start && from < block_end) {
if (buffer_trans(bh))
continue;
if (!buffer_mapped(bh)) {
err = __apfs_get_block(dstream, iblock, bh,
false /* create */);
if (err) {
apfs_err(sb, "failed to map block for ino 0x%llx", apfs_ino(inode));
goto out_put_page;
}
}
if (buffer_mapped(bh) && !buffer_uptodate(bh)) {
get_bh(bh);
lock_buffer(bh);
bh->b_end_io = end_buffer_read_sync;
apfs_submit_bh(REQ_OP_READ, 0, bh);
wait_on_buffer(bh);
if (!buffer_uptodate(bh)) {
apfs_err(sb, "failed to read block for ino 0x%llx", apfs_ino(inode));
err = -EIO;
goto out_put_page;
}
}
clear_buffer_mapped(bh);
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
err = __block_write_begin(page_folio(page), pos, len, apfs_get_new_block);
#else
err = __block_write_begin(page, pos, len, apfs_get_new_block);
#endif
if (err) {
apfs_err(sb, "CoW failed in inode 0x%llx", apfs_ino(inode));
goto out_put_page;
}
*pagep = page;
return 0;
out_put_page:
unlock_page(page);
put_page(page);
return err;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
static int apfs_write_begin(struct file *file, struct address_space *mapping,
loff_t pos, unsigned int len,
struct folio **foliop, void **fsdata)
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
static int apfs_write_begin(struct file *file, struct address_space *mapping,
loff_t pos, unsigned int len,
struct page **pagep, void **fsdata)
#else
static int apfs_write_begin(struct file *file, struct address_space *mapping,
loff_t pos, unsigned int len, unsigned int flags,
struct page **pagep, void **fsdata)
#endif
{
struct inode *inode = mapping->host;
struct super_block *sb = inode->i_sb;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
struct page *page = NULL;
struct page **pagep = &page;
#endif
int blkcount = (len + sb->s_blocksize - 1) >> inode->i_blkbits;
struct apfs_max_ops maxops;
int err;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
unsigned int flags = 0;
#endif
if (unlikely(pos >= APFS_MAX_FILE_SIZE))
return -EFBIG;
maxops.cat = APFS_CREATE_DSTREAM_REC_MAXOPS +
APFS_CREATE_CRYPTO_REC_MAXOPS +
APFS_UPDATE_INODE_MAXOPS() +
blkcount * APFS_GET_NEW_BLOCK_MAXOPS();
maxops.blks = blkcount;
err = apfs_transaction_start(sb, maxops);
if (err)
return err;
err = __apfs_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
if (err)
goto fail;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
*foliop = page_folio(page);
#endif
return 0;
fail:
apfs_transaction_abort(sb);
return err;
}
int __apfs_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned int len, unsigned int copied, struct page *page, void *fsdata)
{
struct inode *inode = mapping->host;
struct apfs_dstream_info *dstream = &APFS_I(inode)->i_dstream;
int ret, err;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
ret = generic_write_end(file, mapping, pos, len, copied, page_folio(page), fsdata);
#else
ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
#endif
dstream->ds_size = i_size_read(inode);
if (ret < len && pos + len > inode->i_size) {
truncate_pagecache(inode, inode->i_size);
err = apfs_truncate(dstream, inode->i_size);
if (err) {
apfs_err(inode->i_sb, "truncation failed for ino 0x%llx", apfs_ino(inode));
return err;
}
}
return ret;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
static int apfs_write_end(struct file *file, struct address_space *mapping,
loff_t pos, unsigned int len, unsigned int copied,
struct folio *folio, void *fsdata)
#else
static int apfs_write_end(struct file *file, struct address_space *mapping,
loff_t pos, unsigned int len, unsigned int copied,
struct page *page, void *fsdata)
#endif
{
struct inode *inode = mapping->host;
struct super_block *sb = inode->i_sb;
struct apfs_nx_transaction *trans = &APFS_NXI(sb)->nx_transaction;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 12, 0)
struct page *page = &folio->page;
#endif
int ret, err;
ret = __apfs_write_end(file, mapping, pos, len, copied, page, fsdata);
if (ret < 0) {
err = ret;
goto fail;
}
if ((pos + ret) & (sb->s_blocksize - 1))
trans->t_state |= APFS_NX_TRANS_INCOMPLETE_BLOCK;
else
trans->t_state &= ~APFS_NX_TRANS_INCOMPLETE_BLOCK;
err = apfs_transaction_commit(sb);
if (!err)
return ret;
fail:
apfs_transaction_abort(sb);
return err;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
static void apfs_noop_invalidatepage(struct page *page, unsigned int offset, unsigned int length)
#else
static void apfs_noop_invalidate_folio(struct folio *folio, size_t offset, size_t length)
#endif
{
}
/* bmap is not implemented to avoid issues with CoW on swapfiles */
static const struct address_space_operations apfs_aops = {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)
.dirty_folio = block_dirty_folio,
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 14, 0)
.set_page_dirty = __set_page_dirty_buffers,
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
.read_folio = apfs_read_folio,
#else
.readpage = apfs_readpage,
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 8, 0)
.readahead = apfs_readahead,
#else
.readpages = apfs_readpages,
#endif
.write_begin = apfs_write_begin,
.write_end = apfs_write_end,
/* The intention is to keep bhs around until the transaction is over */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
.invalidatepage = apfs_noop_invalidatepage,
#else
.invalidate_folio = apfs_noop_invalidate_folio,
#endif
};
/**
* apfs_inode_set_ops - Set up an inode's operations
* @inode: vfs inode to set up
* @rdev: device id (0 if not a device file)
* @compressed: is this a compressed inode?
*
* For device files, also sets the device id to @rdev.
*/
static void apfs_inode_set_ops(struct inode *inode, dev_t rdev, bool compressed)
{
/* A lot of operations still missing, of course */
switch (inode->i_mode & S_IFMT) {
case S_IFREG:
inode->i_op = &apfs_file_inode_operations;
if (compressed) {
inode->i_fop = &apfs_compress_file_operations;
inode->i_mapping->a_ops = &apfs_compress_aops;
} else {
inode->i_fop = &apfs_file_operations;
inode->i_mapping->a_ops = &apfs_aops;
}
break;
case S_IFDIR:
inode->i_op = &apfs_dir_inode_operations;
inode->i_fop = &apfs_dir_operations;
break;
case S_IFLNK:
inode->i_op = &apfs_symlink_inode_operations;
break;
default:
inode->i_op = &apfs_special_inode_operations;
init_special_inode(inode, inode->i_mode, rdev);
break;
}
}
/**
* apfs_inode_from_query - Read the inode found by a successful query
* @query: the query that found the record
* @inode: vfs inode to be filled with the read data
*
* Reads the inode record into @inode and performs some basic sanity checks,
* mostly as a protection against crafted filesystems. Returns 0 on success
* or a negative error code otherwise.
*/
static int apfs_inode_from_query(struct apfs_query *query, struct inode *inode)
{
struct apfs_inode_info *ai = APFS_I(inode);
struct apfs_dstream_info *dstream = &ai->i_dstream;
struct apfs_inode_val *inode_val;
char *raw = query->node->object.data;
char *xval = NULL;
int xlen;
u32 rdev = 0, bsd_flags;
bool compressed = false;
if (query->len < sizeof(*inode_val))
goto corrupted;
inode_val = (struct apfs_inode_val *)(raw + query->off);
ai->i_parent_id = le64_to_cpu(inode_val->parent_id);
dstream->ds_id = le64_to_cpu(inode_val->private_id);
inode->i_mode = le16_to_cpu(inode_val->mode);
ai->i_key_class = le32_to_cpu(inode_val->default_protection_class);
ai->i_int_flags = le64_to_cpu(inode_val->internal_flags);
ai->i_saved_uid = le32_to_cpu(inode_val->owner);
i_uid_write(inode, ai->i_saved_uid);
ai->i_saved_gid = le32_to_cpu(inode_val->group);
i_gid_write(inode, ai->i_saved_gid);
ai->i_bsd_flags = bsd_flags = le32_to_cpu(inode_val->bsd_flags);
if (bsd_flags & APFS_INOBSD_IMMUTABLE)
inode->i_flags |= S_IMMUTABLE;
if (bsd_flags & APFS_INOBSD_APPEND)
inode->i_flags |= S_APPEND;
if (!S_ISDIR(inode->i_mode)) {
/*
* Directory inodes don't store their link count, so to provide
* it we would have to actually count the subdirectories. The
* HFS/HFS+ modules just leave it at 1, and so do we, for now.
*/
set_nlink(inode, le32_to_cpu(inode_val->nlink));
} else {
ai->i_nchildren = le32_to_cpu(inode_val->nchildren);
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
inode->i_ctime = ns_to_timespec64(le64_to_cpu(inode_val->change_time));
#else
inode_set_ctime_to_ts(inode, ns_to_timespec64(le64_to_cpu(inode_val->change_time)));
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 7, 0)
inode->i_atime = ns_to_timespec64(le64_to_cpu(inode_val->access_time));
inode->i_mtime = ns_to_timespec64(le64_to_cpu(inode_val->mod_time));
#else
inode_set_atime_to_ts(inode, ns_to_timespec64(le64_to_cpu(inode_val->access_time)));
inode_set_mtime_to_ts(inode, ns_to_timespec64(le64_to_cpu(inode_val->mod_time)));
#endif
ai->i_crtime = ns_to_timespec64(le64_to_cpu(inode_val->create_time));
dstream->ds_size = inode->i_size = inode->i_blocks = 0;
ai->i_has_dstream = false;
if ((bsd_flags & APFS_INOBSD_COMPRESSED) && !S_ISDIR(inode->i_mode)) {
if (!apfs_compress_get_size(inode, &inode->i_size)) {
inode->i_blocks = (inode->i_size + 511) >> 9;
compressed = true;
}
} else {
xlen = apfs_find_xfield(inode_val->xfields,
query->len - sizeof(*inode_val),
APFS_INO_EXT_TYPE_DSTREAM, &xval);
if (xlen >= sizeof(struct apfs_dstream)) {
struct apfs_dstream *dstream_raw = (struct apfs_dstream *)xval;
dstream->ds_size = inode->i_size = le64_to_cpu(dstream_raw->size);
inode->i_blocks = le64_to_cpu(dstream_raw->alloced_size) >> 9;
ai->i_has_dstream = true;
}
}
xval = NULL;
/* TODO: move each xfield read to its own function */
dstream->ds_sparse_bytes = 0;
xlen = apfs_find_xfield(inode_val->xfields, query->len - sizeof(*inode_val), APFS_INO_EXT_TYPE_SPARSE_BYTES, &xval);
if (xlen >= sizeof(__le64)) {
__le64 *sparse_bytes_p = (__le64 *)xval;
dstream->ds_sparse_bytes = le64_to_cpup(sparse_bytes_p);
}
xval = NULL;
rdev = 0;
xlen = apfs_find_xfield(inode_val->xfields,
query->len - sizeof(*inode_val),
APFS_INO_EXT_TYPE_RDEV, &xval);
if (xlen >= sizeof(__le32)) {
__le32 *rdev_p = (__le32 *)xval;
rdev = le32_to_cpup(rdev_p);
}
apfs_inode_set_ops(inode, rdev, compressed);
return 0;
corrupted:
apfs_err(inode->i_sb, "bad inode record for inode 0x%llx", apfs_ino(inode));
return -EFSCORRUPTED;
}
/**
* apfs_inode_lookup - Lookup an inode record in the catalog b-tree
* @inode: vfs inode to lookup
*
* Runs a catalog query for the apfs_ino(@inode) inode record; returns a pointer
* to the query structure on success, or an error pointer in case of failure.
*/
static struct apfs_query *apfs_inode_lookup(const struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query;
int ret;
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return ERR_PTR(-ENOMEM);
apfs_init_inode_key(apfs_ino(inode), &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (!ret)
return query;
/* Don't complain if an orphan is already gone */
if (!current_work() || ret != -ENODATA)
apfs_err(sb, "query failed for id 0x%llx", apfs_ino(inode));
apfs_free_query(query);
return ERR_PTR(ret);
}
/**
* apfs_test_inode - Check if the inode matches a 64-bit inode number
* @inode: inode to test
* @cnid: pointer to the inode number
*/
static int apfs_test_inode(struct inode *inode, void *cnid)
{
u64 *ino = cnid;
return apfs_ino(inode) == *ino;
}
/**
* apfs_set_inode - Set a 64-bit inode number on the given inode
* @inode: inode to set
* @cnid: pointer to the inode number
*/
static int apfs_set_inode(struct inode *inode, void *cnid)
{
apfs_set_ino(inode, *(u64 *)cnid);
return 0;
}
/**
* apfs_iget_locked - Wrapper for iget5_locked()
* @sb: filesystem superblock
* @cnid: 64-bit inode number
*
* Works the same as iget_locked(), but can handle 64-bit inode numbers on
* 32-bit architectures.
*/
static struct inode *apfs_iget_locked(struct super_block *sb, u64 cnid)
{
return iget5_locked(sb, cnid, apfs_test_inode, apfs_set_inode, &cnid);
}
/**
* apfs_check_dstream_refcnt - Check if an inode's dstream is shared
* @inode: the inode to check
*
* Sets the value of ds_shared for the inode's dstream. Returns 0 on success,
* or a negative error code in case of failure.
*/
static int apfs_check_dstream_refcnt(struct inode *inode)
{
struct apfs_inode_info *ai = APFS_I(inode);
struct apfs_dstream_info *dstream = &ai->i_dstream;
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_query *query = NULL;
struct apfs_dstream_id_val raw_val;
void *raw = NULL;
u32 refcnt;
int ret;
if (!ai->i_has_dstream) {
dstream->ds_shared = false;
return 0;
}
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
apfs_init_dstream_id_key(dstream->ds_id, &query->key);
query->flags |= APFS_QUERY_CAT | APFS_QUERY_EXACT;
ret = apfs_btree_query(sb, &query);
if (ret) {
apfs_err(sb, "query failed for id 0x%llx", dstream->ds_id);
if (ret == -ENODATA)
ret = -EFSCORRUPTED;
goto fail;
}
if (query->len != sizeof(raw_val)) {
ret = -EFSCORRUPTED;
goto fail;