forked from linux-apfs/linux-apfs-rw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper.c
1971 lines (1733 loc) · 50.8 KB
/
super.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/backing-dev.h>
#include <linux/blkdev.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/magic.h>
#include <linux/slab.h>
#include <linux/parser.h>
#include <linux/buffer_head.h>
#include <linux/statfs.h>
#include <linux/seq_file.h>
#include "apfs.h"
#include "version.h"
#define APFS_MODULE_ID_STRING "linux-apfs by eafer (" GIT_COMMIT ")"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0) /* iversion came in 4.16 */
#include <linux/iversion.h>
#endif
/* Keep a list of mounted containers, so that their volumes can share them */
static LIST_HEAD(nxs);
/*
* The main purpose of this mutex is to protect the list of containers and
* their reference counts, but it also has other uses during mounts/unmounts:
* - it prevents new mounts from starting while an unmount is updating the
* backup superblock (apfs_attach_nxi vs apfs_make_super_copy)
* - it prevents a new container superblock read from starting while another
* is taking place, which could cause leaks and other issues if both
* containers are the same (apfs_read_main_super vs itself)
* - it protects the list of volumes for each container, and keeps it
* consistent with the reference count
* - it prevents two different snapshots for a single volume from trying to
* do the first read of their shared omap at the same time
* (apfs_first_read_omap vs itself)
* - it protects the reference count for that shared omap, keeping it
* consistent with the number of volumes that are set with that omap
* - it protects the container mount flags, so that they can only be set by
* the first volume mount to attempt it (apfs_set_nx_flags vs itself)
*/
DEFINE_MUTEX(nxs_mutex);
/**
* apfs_nx_find_by_dev - Search for a device in the list of mounted containers
* @dev: device number of block device for the wanted container
*
* Returns a pointer to the container structure in the list, or NULL if the
* container isn't currently mounted.
*/
static struct apfs_nxsb_info *apfs_nx_find_by_dev(dev_t dev)
{
struct apfs_nxsb_info *curr;
lockdep_assert_held(&nxs_mutex);
list_for_each_entry(curr, &nxs, nx_list) {
struct block_device *curr_bdev = curr->nx_bdev;
if (curr_bdev->bd_dev == dev)
return curr;
}
return NULL;
}
/**
* apfs_sb_set_blocksize - Set the block size for the container's device
* @sb: superblock structure
* @size: size to set
*
* This is like sb_set_blocksize(), but it uses the container's device instead
* of the nonexistent volume device.
*/
static int apfs_sb_set_blocksize(struct super_block *sb, int size)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
if (set_blocksize(APFS_NXI(sb)->nx_bdev_file, size))
#else
if (set_blocksize(APFS_NXI(sb)->nx_bdev, size))
#endif
return 0;
sb->s_blocksize = size;
sb->s_blocksize_bits = blksize_bits(size);
return sb->s_blocksize;
}
/**
* apfs_read_super_copy - Read the copy of the container superblock in block 0
* @sb: superblock structure
*
* Returns a pointer to the buffer head, or an error pointer in case of failure.
*/
static struct buffer_head *apfs_read_super_copy(struct super_block *sb)
{
struct buffer_head *bh;
struct apfs_nx_superblock *msb_raw;
int blocksize;
int err = -EINVAL;
/*
* For now assume a small blocksize, we only need it so that we can
* read the actual blocksize from disk.
*/
if (!apfs_sb_set_blocksize(sb, APFS_NX_DEFAULT_BLOCK_SIZE)) {
apfs_err(sb, "unable to set blocksize");
return ERR_PTR(err);
}
bh = apfs_sb_bread(sb, APFS_NX_BLOCK_NUM);
if (!bh) {
apfs_err(sb, "unable to read superblock");
return ERR_PTR(err);
}
msb_raw = (struct apfs_nx_superblock *)bh->b_data;
blocksize = le32_to_cpu(msb_raw->nx_block_size);
sb->s_magic = le32_to_cpu(msb_raw->nx_magic);
if (sb->s_magic != APFS_NX_MAGIC) {
apfs_warn(sb, "not an apfs container - are you mounting the right partition?");
goto fail;
}
if (sb->s_blocksize != blocksize) {
brelse(bh);
if (!apfs_sb_set_blocksize(sb, blocksize)) {
apfs_err(sb, "bad blocksize %d", blocksize);
return ERR_PTR(err);
}
bh = apfs_sb_bread(sb, APFS_NX_BLOCK_NUM);
if (!bh) {
apfs_err(sb, "unable to read superblock 2nd time");
return ERR_PTR(err);
}
msb_raw = (struct apfs_nx_superblock *)bh->b_data;
}
if (!apfs_obj_verify_csum(sb, bh))
apfs_notice(sb, "backup superblock seems corrupted");
return bh;
fail:
brelse(bh);
return ERR_PTR(err);
}
/**
* apfs_make_super_copy - Write a copy of the checkpoint superblock to block 0
* @sb: superblock structure
*/
static void apfs_make_super_copy(struct super_block *sb)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_nxsb_info *nxi = sbi->s_nxi;
struct buffer_head *bh;
if (!(nxi->nx_flags & APFS_READWRITE))
return;
/*
* Only update the backup when the last volume is getting unmounted.
* Of course a new mounter could still come along before we actually
* release the nxi.
*/
mutex_lock(&nxs_mutex);
if (nxi->nx_refcnt > 1)
goto out_unlock;
bh = apfs_sb_bread(sb, APFS_NX_BLOCK_NUM);
if (!bh) {
apfs_err(sb, "failed to write block zero");
goto out_unlock;
}
memcpy(bh->b_data, nxi->nx_raw, sb->s_blocksize);
mark_buffer_dirty(bh);
brelse(bh);
out_unlock:
mutex_unlock(&nxs_mutex);
}
static int apfs_check_nx_features(struct super_block *sb);
static void apfs_set_trans_buffer_limit(struct super_block *sb);
/**
* apfs_read_main_super - Find the container superblock and read it into memory
* @sb: superblock structure
*
* Returns a negative error code in case of failure. On success, returns 0
* and sets the nx_raw and nx_xid fields of APFS_NXI(@sb).
*/
static int apfs_read_main_super(struct super_block *sb)
{
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct buffer_head *bh = NULL;
struct buffer_head *desc_bh = NULL;
struct apfs_nx_superblock *msb_raw;
u64 xid, bno = APFS_NX_BLOCK_NUM;
u64 desc_base;
u32 desc_blocks;
int err = -EINVAL;
int i;
mutex_lock(&nxs_mutex);
if (nxi->nx_blocksize) {
/* It's already mapped */
sb->s_blocksize = nxi->nx_blocksize;
sb->s_blocksize_bits = nxi->nx_blocksize_bits;
sb->s_magic = le32_to_cpu(nxi->nx_raw->nx_magic);
err = 0;
goto out;
}
/* Read the superblock from the last clean unmount */
bh = apfs_read_super_copy(sb);
if (IS_ERR(bh)) {
err = PTR_ERR(bh);
bh = NULL;
goto out;
}
msb_raw = (struct apfs_nx_superblock *)bh->b_data;
/* We want to mount the latest valid checkpoint among the descriptors */
desc_base = le64_to_cpu(msb_raw->nx_xp_desc_base);
if (desc_base >> 63 != 0) {
/* The highest bit is set when checkpoints are not contiguous */
apfs_err(sb, "checkpoint descriptor tree not yet supported");
goto out;
}
desc_blocks = le32_to_cpu(msb_raw->nx_xp_desc_blocks);
if (desc_blocks > 10000) { /* Arbitrary loop limit, is it enough? */
apfs_err(sb, "too many checkpoint descriptors?");
err = -EFSCORRUPTED;
goto out;
}
/* Now we go through the checkpoints one by one */
xid = le64_to_cpu(msb_raw->nx_o.o_xid);
for (i = 0; i < desc_blocks; ++i) {
struct apfs_nx_superblock *desc_raw;
brelse(desc_bh);
desc_bh = apfs_sb_bread(sb, desc_base + i);
if (!desc_bh) {
apfs_err(sb, "unable to read checkpoint descriptor");
goto out;
}
desc_raw = (struct apfs_nx_superblock *)desc_bh->b_data;
if (le32_to_cpu(desc_raw->nx_magic) != APFS_NX_MAGIC)
continue; /* Not a superblock */
if (le64_to_cpu(desc_raw->nx_o.o_xid) <= xid)
continue; /* Old */
if (!apfs_obj_verify_csum(sb, desc_bh))
continue; /* Corrupted */
xid = le64_to_cpu(desc_raw->nx_o.o_xid);
msb_raw = desc_raw;
bno = desc_base + i;
brelse(bh);
bh = desc_bh;
desc_bh = NULL;
}
nxi->nx_raw = kmalloc(sb->s_blocksize, GFP_KERNEL);
if (!nxi->nx_raw) {
err = -ENOMEM;
goto out;
}
memcpy(nxi->nx_raw, bh->b_data, sb->s_blocksize);
nxi->nx_bno = bno;
nxi->nx_xid = xid;
/* For now we only support blocksize < PAGE_SIZE */
nxi->nx_blocksize = sb->s_blocksize;
nxi->nx_blocksize_bits = sb->s_blocksize_bits;
apfs_set_trans_buffer_limit(sb);
err = apfs_check_nx_features(sb);
out:
brelse(bh);
mutex_unlock(&nxs_mutex);
return err;
}
/**
* apfs_update_software_info - Write the module info to a modified volume
* @sb: superblock structure
*
* Writes this module's information to index zero of the apfs_modified_by
* array, shifting the rest of the entries to the right.
*/
static void apfs_update_software_info(struct super_block *sb)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_superblock *raw = sbi->s_vsb_raw;
struct apfs_modified_by *mod_by;
ASSERT(sbi->s_vsb_raw);
apfs_assert_in_transaction(sb, &raw->apfs_o);
ASSERT(strlen(APFS_MODULE_ID_STRING) < APFS_MODIFIED_NAMELEN);
mod_by = raw->apfs_modified_by;
memmove(mod_by + 1, mod_by, (APFS_MAX_HIST - 1) * sizeof(*mod_by));
memset(mod_by->id, 0, sizeof(mod_by->id));
strscpy(mod_by->id, APFS_MODULE_ID_STRING, sizeof(mod_by->id));
mod_by->timestamp = cpu_to_le64(ktime_get_real_ns());
mod_by->last_xid = cpu_to_le64(APFS_NXI(sb)->nx_xid);
}
static struct file_system_type apfs_fs_type;
/**
* apfs_free_main_super - Clean up apfs_read_main_super()
* @sbi: in-memory superblock info
*
* It also cleans up after apfs_attach_nxi(), so the name is no longer accurate.
*/
static inline void apfs_free_main_super(struct apfs_sb_info *sbi)
{
struct apfs_nxsb_info *nxi = sbi->s_nxi;
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 5, 0)
fmode_t mode = FMODE_READ | FMODE_EXCL;
#endif
struct apfs_ephemeral_object_info *eph_list = NULL;
struct apfs_spaceman *sm = NULL;
u32 bmap_idx;
int i;
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 5, 0)
if (nxi->nx_flags & APFS_READWRITE)
mode |= FMODE_WRITE;
#endif
mutex_lock(&nxs_mutex);
list_del(&sbi->list);
if (--nxi->nx_refcnt)
goto out;
/* Clean up all the ephemeral objects in memory */
eph_list = nxi->nx_eph_list;
if (eph_list) {
for (i = 0; i < nxi->nx_eph_count; ++i) {
kfree(eph_list[i].object);
eph_list[i].object = NULL;
}
kfree(eph_list);
eph_list = nxi->nx_eph_list = NULL;
nxi->nx_eph_count = 0;
}
kfree(nxi->nx_raw);
nxi->nx_raw = NULL;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 9, 0)
fput(nxi->nx_bdev_file);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
bdev_release(nxi->nx_bdev_handle);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 5, 0)
blkdev_put(nxi->nx_bdev, &apfs_fs_type);
#else
blkdev_put(nxi->nx_bdev, mode);
#endif
list_del(&nxi->nx_list);
sm = nxi->nx_spaceman;
if (sm) {
for (bmap_idx = 0; bmap_idx < sm->sm_ip_bmaps_count; ++bmap_idx) {
kfree(sm->sm_ip_bmaps[bmap_idx].block);
sm->sm_ip_bmaps[bmap_idx].block = NULL;
}
kfree(sm);
nxi->nx_spaceman = sm = NULL;
}
kfree(nxi);
out:
sbi->s_nxi = NULL;
mutex_unlock(&nxs_mutex);
}
/**
* apfs_map_volume_super_bno - Map a block containing a volume superblock
* @sb: superblock structure
* @bno: block to map
* @check: verify the checksum?
*/
int apfs_map_volume_super_bno(struct super_block *sb, u64 bno, bool check)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_superblock *vsb_raw = NULL;
struct buffer_head *bh = NULL;
int err;
bh = apfs_sb_bread(sb, bno);
if (!bh) {
apfs_err(sb, "unable to read volume superblock");
return -EINVAL;
}
vsb_raw = (struct apfs_superblock *)bh->b_data;
if (le32_to_cpu(vsb_raw->apfs_magic) != APFS_MAGIC) {
apfs_err(sb, "wrong magic in volume superblock");
err = -EINVAL;
goto fail;
}
/*
* XXX: apfs_omap_lookup_block() only runs this check when write
* is true, but it should always do it.
*/
if (check && !apfs_obj_verify_csum(sb, bh)) {
apfs_err(sb, "inconsistent volume superblock");
err = -EFSBADCRC;
goto fail;
}
sbi->s_vsb_raw = vsb_raw;
sbi->s_vobject.sb = sb;
sbi->s_vobject.block_nr = bno;
sbi->s_vobject.oid = le64_to_cpu(vsb_raw->apfs_o.o_oid);
brelse(sbi->s_vobject.o_bh);
sbi->s_vobject.o_bh = bh;
sbi->s_vobject.data = bh->b_data;
return 0;
fail:
brelse(bh);
return err;
}
/**
* apfs_alloc_omap - Allocate and initialize an object map struct
*
* Returns the struct, or NULL in case of allocation failure.
*/
static struct apfs_omap *apfs_alloc_omap(void)
{
struct apfs_omap *omap = NULL;
struct apfs_omap_cache *cache = NULL;
omap = kzalloc(sizeof(*omap), GFP_KERNEL);
if (!omap)
return NULL;
cache = &omap->omap_cache;
spin_lock_init(&cache->lock);
return omap;
}
/**
* apfs_map_volume_super - Find the volume superblock and map it into memory
* @sb: superblock structure
* @write: request write access?
*
* Returns a negative error code in case of failure. On success, returns 0
* and sets APFS_SB(@sb)->s_vsb_raw and APFS_SB(@sb)->s_vobject.
*/
int apfs_map_volume_super(struct super_block *sb, bool write)
{
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_omap_phys *msb_omap_raw;
struct apfs_omap *omap = NULL;
struct apfs_node *vnode;
struct buffer_head *bh;
u64 vol_id;
u64 vsb;
int err;
ASSERT(msb_raw);
/* Get the id for the requested volume number */
if (sbi->s_vol_nr >= APFS_NX_MAX_FILE_SYSTEMS) {
apfs_err(sb, "volume number out of range");
return -EINVAL;
}
vol_id = le64_to_cpu(msb_raw->nx_fs_oid[sbi->s_vol_nr]);
if (vol_id == 0) {
apfs_err(sb, "requested volume does not exist");
return -EINVAL;
}
/* Get the container's object map */
bh = apfs_read_object_block(sb, le64_to_cpu(msb_raw->nx_omap_oid),
write, false /* preserve */);
if (IS_ERR(bh)) {
apfs_err(sb, "unable to read container object map");
return PTR_ERR(bh);
}
if (write) {
ASSERT(buffer_trans(bh));
msb_raw->nx_omap_oid = cpu_to_le64(bh->b_blocknr);
}
msb_omap_raw = (struct apfs_omap_phys *)bh->b_data;
/* Get the root node for the container's omap */
vnode = apfs_read_node(sb, le64_to_cpu(msb_omap_raw->om_tree_oid),
APFS_OBJ_PHYSICAL, write);
if (IS_ERR(vnode)) {
apfs_err(sb, "unable to read volume block");
err = PTR_ERR(vnode);
goto fail;
}
if (write) {
ASSERT(buffer_trans(bh));
msb_omap_raw->om_tree_oid = cpu_to_le64(vnode->object.block_nr);
}
msb_omap_raw = NULL;
brelse(bh);
bh = NULL;
omap = apfs_alloc_omap();
if (!omap) {
apfs_node_free(vnode);
return -ENOMEM;
}
omap->omap_root = vnode;
err = apfs_omap_lookup_block(sb, omap, vol_id, &vsb, write);
apfs_node_free(vnode);
vnode = NULL;
kfree(omap);
omap = NULL;
if (err) {
apfs_err(sb, "volume not found, likely corruption");
return err;
}
/*
* Snapshots could get mounted during a transaction, so the fletcher
* checksum doesn't have to be valid.
*/
return apfs_map_volume_super_bno(sb, vsb, !write && !sbi->s_snap_name);
fail:
brelse(bh);
return err;
}
/**
* apfs_unmap_volume_super - Clean up apfs_map_volume_super()
* @sb: filesystem superblock
*/
void apfs_unmap_volume_super(struct super_block *sb)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_object *obj = &sbi->s_vobject;
obj->data = NULL;
brelse(obj->o_bh);
obj->o_bh = NULL;
}
/**
* apfs_get_omap - Get a reference to the omap, if it's already read
* @sb: filesystem superblock
*
* Returns the omap struct, or NULL on failure.
*/
static struct apfs_omap *apfs_get_omap(struct super_block *sb)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_nxsb_info *nxi = APFS_NXI(sb);
struct apfs_sb_info *curr = NULL;
struct apfs_omap *omap = NULL;
struct apfs_omap_cache *cache = NULL;
lockdep_assert_held(&nxs_mutex);
list_for_each_entry(curr, &nxi->vol_list, list) {
if (curr == sbi)
continue;
if (curr->s_vol_nr == sbi->s_vol_nr) {
omap = curr->s_omap;
if (!omap) {
/*
* This volume has already gone through
* apfs_attach_nxi(), but its omap is either
* not yet read or already put.
*/
continue;
}
cache = &omap->omap_cache;
++omap->omap_refcnt;
/* Right now the cache can't be shared like this */
cache->disabled = true;
return omap;
}
}
return NULL;
}
/**
* apfs_read_omap - Find and read the omap root node
* @sb: superblock structure
* @write: request write access?
*
* On success, returns 0 and sets the fields of APFS_SB(@sb)->s_omap; on failure
* returns a negative error code.
*/
int apfs_read_omap(struct super_block *sb, bool write)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_superblock *vsb_raw = sbi->s_vsb_raw;
struct apfs_omap_phys *omap_raw;
struct apfs_node *omap_root;
struct apfs_omap *omap = NULL;
struct buffer_head *bh;
u64 omap_blk;
int err;
ASSERT(sbi->s_vsb_raw);
ASSERT(sbi->s_omap);
omap = sbi->s_omap;
/* Get the block holding the volume omap information */
omap_blk = le64_to_cpu(vsb_raw->apfs_omap_oid);
bh = apfs_read_object_block(sb, omap_blk, write, false /* preserve */);
if (IS_ERR(bh)) {
apfs_err(sb, "unable to read the volume object map");
return PTR_ERR(bh);
}
if (write) {
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
vsb_raw->apfs_omap_oid = cpu_to_le64(bh->b_blocknr);
}
omap_raw = (struct apfs_omap_phys *)bh->b_data;
/* Get the volume's object map */
omap_root = apfs_read_node(sb, le64_to_cpu(omap_raw->om_tree_oid),
APFS_OBJ_PHYSICAL, write);
if (IS_ERR(omap_root)) {
apfs_err(sb, "unable to read the omap root node");
err = PTR_ERR(omap_root);
goto fail;
}
if (write) {
apfs_assert_in_transaction(sb, &omap_raw->om_o);
ASSERT(buffer_trans(bh));
omap_raw->om_tree_oid = cpu_to_le64(omap_root->object.block_nr);
}
omap->omap_latest_snap = le64_to_cpu(omap_raw->om_most_recent_snap);
omap_raw = NULL;
brelse(bh);
if (omap->omap_root)
apfs_node_free(omap->omap_root);
omap->omap_root = omap_root;
return 0;
fail:
brelse(bh);
return err;
}
/**
* apfs_first_read_omap - Find and read the omap root node during mount
* @sb: superblock structure
*
* On success, returns 0 and sets APFS_SB(@sb)->s_omap; on failure returns a
* negative error code.
*/
static int apfs_first_read_omap(struct super_block *sb)
{
struct apfs_sb_info *sbi = NULL;
struct apfs_omap *omap = NULL;
int err;
/*
* For each volume, the first mount that gets here is responsible
* for reading the omap. Other mounts (for other snapshots) just
* go through the container's volume list to retrieve it. This results
* in coarse locking as usual: with some thought it would be possible
* to allow other volumes to read their own omaps at the same time,
* but I don't see the point.
*/
mutex_lock(&nxs_mutex);
sbi = APFS_SB(sb);
/* The current transaction and all snapshots share a single omap */
omap = apfs_get_omap(sb);
if (omap) {
sbi->s_omap = omap;
err = 0;
goto out;
}
omap = apfs_alloc_omap();
if (!omap) {
err = -ENOMEM;
goto out;
}
sbi->s_omap = omap;
err = apfs_read_omap(sb, false /* write */);
if (err) {
kfree(omap);
sbi->s_omap = NULL;
goto out;
}
++omap->omap_refcnt;
err = 0;
out:
mutex_unlock(&nxs_mutex);
return err;
}
/**
* apfs_unset_omap - Unset the object map in a superblock
* @sb: superblock structure
*
* Shrinks the omap reference, frees the omap if needed, and sets the field to
* NULL atomically in relation to apfs_first_read_omap(). So, no other mount
* can grab a new reference halfway through.
*/
static void apfs_unset_omap(struct super_block *sb)
{
struct apfs_omap **omap_p = NULL;
struct apfs_omap *omap = NULL;
omap_p = &APFS_SB(sb)->s_omap;
omap = *omap_p;
if (!omap)
return;
mutex_lock(&nxs_mutex);
if (--omap->omap_refcnt != 0)
goto out;
apfs_node_free(omap->omap_root);
kfree(omap);
out:
*omap_p = NULL;
mutex_unlock(&nxs_mutex);
}
/**
* apfs_read_catalog - Find and read the catalog root node
* @sb: superblock structure
* @write: request write access?
*
* On success, returns 0 and sets APFS_SB(@sb)->s_cat_root; on failure returns
* a negative error code.
*/
int apfs_read_catalog(struct super_block *sb, bool write)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_superblock *vsb_raw = sbi->s_vsb_raw;
struct apfs_node *root_node;
ASSERT(sbi->s_omap && sbi->s_omap->omap_root);
root_node = apfs_read_node(sb, le64_to_cpu(vsb_raw->apfs_root_tree_oid),
APFS_OBJ_VIRTUAL, write);
if (IS_ERR(root_node)) {
apfs_err(sb, "unable to read catalog root node");
return PTR_ERR(root_node);
}
if (sbi->s_cat_root)
apfs_node_free(sbi->s_cat_root);
sbi->s_cat_root = root_node;
return 0;
}
static void apfs_put_super(struct super_block *sb)
{
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_nx_transaction *trans = NULL;
/* Cleanups won't reschedule themselves during unmount */
flush_work(&sbi->s_orphan_cleanup_work);
/* We are about to commit anyway */
trans = &APFS_NXI(sb)->nx_transaction;
cancel_delayed_work_sync(&trans->t_work);
/* Stop flushing orphans and update the volume as needed */
if (!(sb->s_flags & SB_RDONLY)) {
struct apfs_superblock *vsb_raw;
struct buffer_head *vsb_bh;
struct apfs_max_ops maxops = {0};
int err;
err = apfs_transaction_start(sb, maxops);
if (err) {
apfs_err(sb, "unmount transaction start failed (err:%d)", err);
goto fail;
}
vsb_raw = sbi->s_vsb_raw;
vsb_bh = sbi->s_vobject.o_bh;
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o);
ASSERT(buffer_trans(vsb_bh));
apfs_update_software_info(sb);
vsb_raw->apfs_unmount_time = cpu_to_le64(ktime_get_real_ns());
set_buffer_csum(vsb_bh);
/* Guarantee commit */
sbi->s_nxi->nx_transaction.t_state |= APFS_NX_TRANS_FORCE_COMMIT;
err = apfs_transaction_commit(sb);
if (err) {
apfs_err(sb, "unmount transaction commit failed (err:%d)", err);
apfs_transaction_abort(sb);
goto fail;
}
}
/*
* Even if this particular volume/snapshot was read-only, the container
* may have changed and need an update here.
*/
apfs_make_super_copy(sb);
fail:
/*
* This is essentially the cleanup for apfs_fill_super(). It goes here
* because generic_shutdown_super() only calls ->put_super() when the
* root dentry has been set, that is, when apfs_fill_super() succeeded.
* The rest of the mount cleanup is done directly by ->kill_sb().
*/
iput(sbi->s_private_dir);
sbi->s_private_dir = NULL;
apfs_node_free(sbi->s_cat_root);
sbi->s_cat_root = NULL;
apfs_unset_omap(sb);
apfs_unmap_volume_super(sb);
}
static struct kmem_cache *apfs_inode_cachep;
static struct inode *apfs_alloc_inode(struct super_block *sb)
{
struct apfs_inode_info *ai;
struct apfs_dstream_info *dstream;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)
ai = alloc_inode_sb(sb, apfs_inode_cachep, GFP_KERNEL);
#else
ai = kmem_cache_alloc(apfs_inode_cachep, GFP_KERNEL);
#endif
if (!ai)
return NULL;
dstream = &ai->i_dstream;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0) /* iversion came in 4.16 */
inode_set_iversion(&ai->vfs_inode, 1);
#else
ai->vfs_inode.i_version = 1;
#endif
dstream->ds_sb = sb;
dstream->ds_inode = &ai->vfs_inode;
dstream->ds_cached_ext.len = 0;
dstream->ds_ext_dirty = false;
ai->i_nchildren = 0;
INIT_LIST_HEAD(&ai->i_list);
ai->i_cleaned = false;
return &ai->vfs_inode;
}
static void apfs_i_callback(struct rcu_head *head)
{
struct inode *inode = container_of(head, struct inode, i_rcu);
kmem_cache_free(apfs_inode_cachep, APFS_I(inode));
}
static void apfs_destroy_inode(struct inode *inode)
{
call_rcu(&inode->i_rcu, apfs_i_callback);
}
static void init_once(void *p)
{
struct apfs_inode_info *ai = (struct apfs_inode_info *)p;
struct apfs_dstream_info *dstream = &ai->i_dstream;
spin_lock_init(&dstream->ds_ext_lock);
inode_init_once(&ai->vfs_inode);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 9, 0)
#define SLAB_MEM_SPREAD 0
#endif
static int __init init_inodecache(void)
{
apfs_inode_cachep = kmem_cache_create("apfs_inode_cache",
sizeof(struct apfs_inode_info),
0, (SLAB_RECLAIM_ACCOUNT|
SLAB_MEM_SPREAD|SLAB_ACCOUNT),
init_once);
if (apfs_inode_cachep == NULL)
return -ENOMEM;
return 0;
}
static int apfs_write_inode(struct inode *inode, struct writeback_control *wbc)
{
struct super_block *sb = inode->i_sb;
struct apfs_nxsb_info *nxi = APFS_SB(sb)->s_nxi;
struct apfs_max_ops maxops;
int err;
maxops.cat = APFS_UPDATE_INODE_MAXOPS();
maxops.blks = 0;
err = apfs_transaction_start(sb, maxops);
if (err)
return err;
err = apfs_update_inode(inode, NULL /* new_name */);
if (err)
goto fail;
/* Don't commit yet, or the inode will get flushed again and lock up */
nxi->nx_transaction.t_state |= APFS_NX_TRANS_DEFER_COMMIT;
err = apfs_transaction_commit(sb);
if (err)
goto fail;
return 0;
fail:
apfs_transaction_abort(sb);
return err;
}
static void destroy_inodecache(void)
{
/*
* Make sure all delayed rcu free inodes are flushed before we
* destroy cache.
*/
rcu_barrier();
kmem_cache_destroy(apfs_inode_cachep);
}
/**
* apfs_count_used_blocks - Count the blocks in use across all volumes
* @sb: filesystem superblock
* @count: on return it will store the block count
*
* This function probably belongs in a separate file, but for now it is
* only called by statfs.
*/
static int apfs_count_used_blocks(struct super_block *sb, u64 *count)
{
struct apfs_nx_superblock *msb_raw = APFS_NXI(sb)->nx_raw;
struct apfs_node *vnode;
struct apfs_omap_phys *msb_omap_raw;
struct buffer_head *bh;
struct apfs_omap *omap = NULL;
u64 msb_omap, vb;
int i;
int err = 0;
/* Get the container's object map */
msb_omap = le64_to_cpu(msb_raw->nx_omap_oid);
bh = apfs_sb_bread(sb, msb_omap);
if (!bh) {
apfs_err(sb, "unable to read container object map");
return -EIO;
}
msb_omap_raw = (struct apfs_omap_phys *)bh->b_data;
/* Get the Volume Block */
vb = le64_to_cpu(msb_omap_raw->om_tree_oid);
msb_omap_raw = NULL;
brelse(bh);
bh = NULL;
vnode = apfs_read_node(sb, vb, APFS_OBJ_PHYSICAL, false /* write */);
if (IS_ERR(vnode)) {
apfs_err(sb, "unable to read volume block");
return PTR_ERR(vnode);
}
omap = apfs_alloc_omap();
if (!omap) {
err = -ENOMEM;
goto fail;
}
omap->omap_root = vnode;
/* Iterate through the checkpoint superblocks and add the used blocks */
*count = 0;
for (i = 0; i < APFS_NX_MAX_FILE_SYSTEMS; i++) {
struct apfs_superblock *vsb_raw;
u64 vol_id;
u64 vol_bno;
vol_id = le64_to_cpu(msb_raw->nx_fs_oid[i]);
if (vol_id == 0) /* All volumes have been checked */
break;
err = apfs_omap_lookup_newest_block(sb, omap, vol_id, &vol_bno, false /* write */);
if (err) {
apfs_err(sb, "omap lookup failed for vol id 0x%llx", vol_id);