-
Notifications
You must be signed in to change notification settings - Fork 0
/
free-space-cache.c
4159 lines (3570 loc) · 107 KB
/
free-space-cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2008 Red Hat. All rights reserved.
*/
#include <linux/pagemap.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/slab.h>
#include <linux/math64.h>
#include <linux/ratelimit.h>
#include <linux/error-injection.h>
#include <linux/sched/mm.h>
#include "misc.h"
#include "ctree.h"
#include "free-space-cache.h"
#include "transaction.h"
#include "disk-io.h"
#include "extent_io.h"
#include "volumes.h"
#include "space-info.h"
#include "delalloc-space.h"
#include "block-group.h"
#include "discard.h"
#include "apfs_trace.h"
#define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
#define MAX_CACHE_BYTES_PER_GIG SZ_64K
#define FORCE_EXTENT_THRESHOLD SZ_1M
struct apfs_trim_range {
u64 start;
u64 bytes;
struct list_head list;
};
static int link_free_space(struct apfs_free_space_ctl *ctl,
struct apfs_free_space *info);
static void unlink_free_space(struct apfs_free_space_ctl *ctl,
struct apfs_free_space *info);
static int search_bitmap(struct apfs_free_space_ctl *ctl,
struct apfs_free_space *bitmap_info, u64 *offset,
u64 *bytes, bool for_alloc);
static void free_bitmap(struct apfs_free_space_ctl *ctl,
struct apfs_free_space *bitmap_info);
static void bitmap_clear_bits(struct apfs_free_space_ctl *ctl,
struct apfs_free_space *info, u64 offset,
u64 bytes);
static struct inode *__lookup_free_space_inode(struct apfs_root *root,
struct apfs_path *path,
u64 offset)
{
struct apfs_fs_info *fs_info = root->fs_info;
struct apfs_key key = {};
struct apfs_key location = {};
struct apfs_disk_key disk_key;
struct apfs_free_space_header *header;
struct extent_buffer *leaf;
struct inode *inode = NULL;
unsigned nofs_flag;
int ret;
key.objectid = APFS_FREE_SPACE_OBJECTID;
key.offset = offset;
key.type = 0;
ret = apfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
return ERR_PTR(ret);
if (ret > 0) {
apfs_release_path(path);
return ERR_PTR(-ENOENT);
}
leaf = path->nodes[0];
header = apfs_item_ptr(leaf, path->slots[0],
struct apfs_free_space_header);
apfs_free_space_key(leaf, header, &disk_key);
apfs_disk_key_to_cpu(leaf, &location, &disk_key);
apfs_release_path(path);
/*
* We are often under a trans handle at this point, so we need to make
* sure NOFS is set to keep us from deadlocking.
*/
nofs_flag = memalloc_nofs_save();
inode = apfs_iget_path(fs_info->sb, location.objectid, root);
apfs_release_path(path);
memalloc_nofs_restore(nofs_flag);
if (IS_ERR(inode))
return inode;
mapping_set_gfp_mask(inode->i_mapping,
mapping_gfp_constraint(inode->i_mapping,
~(__GFP_FS | __GFP_HIGHMEM)));
return inode;
}
struct inode *lookup_free_space_inode(struct apfs_block_group *block_group,
struct apfs_path *path)
{
struct apfs_fs_info *fs_info = block_group->fs_info;
struct inode *inode = NULL;
u32 flags = APFS_INODE_NODATASUM | APFS_INODE_NODATACOW;
spin_lock(&block_group->lock);
if (block_group->inode)
inode = igrab(block_group->inode);
spin_unlock(&block_group->lock);
if (inode)
return inode;
inode = __lookup_free_space_inode(fs_info->tree_root, path,
block_group->start);
if (IS_ERR(inode))
return inode;
spin_lock(&block_group->lock);
if (!((APFS_I(inode)->flags & flags) == flags)) {
apfs_info(fs_info, "Old style space inode found, converting.");
APFS_I(inode)->flags |= APFS_INODE_NODATASUM |
APFS_INODE_NODATACOW;
block_group->disk_cache_state = APFS_DC_CLEAR;
}
if (!block_group->iref) {
block_group->inode = igrab(inode);
block_group->iref = 1;
}
spin_unlock(&block_group->lock);
return inode;
}
static int __create_free_space_inode(struct apfs_root *root,
struct apfs_trans_handle *trans,
struct apfs_path *path,
u64 ino, u64 offset)
{
struct apfs_key key = {};
struct apfs_disk_key disk_key;
struct apfs_free_space_header *header;
struct apfs_inode_item *inode_item;
struct extent_buffer *leaf;
/* We inline CRCs for the free disk space cache */
const u64 flags = APFS_INODE_NOCOMPRESS | APFS_INODE_PREALLOC |
APFS_INODE_NODATASUM | APFS_INODE_NODATACOW;
int ret;
ret = apfs_insert_empty_inode(trans, root, path, ino);
if (ret)
return ret;
leaf = path->nodes[0];
inode_item = apfs_item_ptr(leaf, path->slots[0],
struct apfs_inode_item);
apfs_item_key(leaf, &disk_key, path->slots[0]);
memzero_extent_buffer(leaf, (unsigned long)inode_item,
sizeof(*inode_item));
apfs_set_inode_generation(leaf, inode_item, trans->transid);
apfs_set_inode_size(leaf, inode_item, 0);
apfs_set_inode_nbytes(leaf, inode_item, 0);
apfs_set_inode_uid(leaf, inode_item, 0);
apfs_set_inode_gid(leaf, inode_item, 0);
apfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
apfs_set_inode_flags(leaf, inode_item, flags);
apfs_set_inode_nlink(leaf, inode_item, 1);
apfs_set_inode_transid(leaf, inode_item, trans->transid);
apfs_set_inode_block_group(leaf, inode_item, offset);
apfs_mark_buffer_dirty(leaf);
apfs_release_path(path);
key.objectid = APFS_FREE_SPACE_OBJECTID;
key.offset = offset;
key.type = 0;
ret = apfs_insert_empty_item(trans, root, path, &key,
sizeof(struct apfs_free_space_header));
if (ret < 0) {
apfs_release_path(path);
return ret;
}
leaf = path->nodes[0];
header = apfs_item_ptr(leaf, path->slots[0],
struct apfs_free_space_header);
memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
apfs_set_free_space_key(leaf, header, &disk_key);
apfs_mark_buffer_dirty(leaf);
apfs_release_path(path);
return 0;
}
int create_free_space_inode(struct apfs_trans_handle *trans,
struct apfs_block_group *block_group,
struct apfs_path *path)
{
int ret;
u64 ino;
ret = apfs_get_free_objectid(trans->fs_info->tree_root, &ino);
if (ret < 0)
return ret;
return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
ino, block_group->start);
}
/*
* inode is an optional sink: if it is NULL, apfs_remove_free_space_inode
* handles lookup, otherwise it takes ownership and iputs the inode.
* Don't reuse an inode pointer after passing it into this function.
*/
int apfs_remove_free_space_inode(struct apfs_trans_handle *trans,
struct inode *inode,
struct apfs_block_group *block_group)
{
struct apfs_path *path;
struct apfs_key key = {};
int ret = 0;
path = apfs_alloc_path();
if (!path)
return -ENOMEM;
if (!inode)
inode = lookup_free_space_inode(block_group, path);
if (IS_ERR(inode)) {
if (PTR_ERR(inode) != -ENOENT)
ret = PTR_ERR(inode);
goto out;
}
ret = apfs_orphan_add(trans, APFS_I(inode));
if (ret) {
apfs_add_delayed_iput(inode);
goto out;
}
clear_nlink(inode);
/* One for the block groups ref */
spin_lock(&block_group->lock);
if (block_group->iref) {
block_group->iref = 0;
block_group->inode = NULL;
spin_unlock(&block_group->lock);
iput(inode);
} else {
spin_unlock(&block_group->lock);
}
/* One for the lookup ref */
apfs_add_delayed_iput(inode);
key.objectid = APFS_FREE_SPACE_OBJECTID;
key.type = 0;
key.offset = block_group->start;
ret = apfs_search_slot(trans, trans->fs_info->tree_root, &key, path,
-1, 1);
if (ret) {
if (ret > 0)
ret = 0;
goto out;
}
ret = apfs_del_item(trans, trans->fs_info->tree_root, path);
out:
apfs_free_path(path);
return ret;
}
int apfs_check_trunc_cache_free_space(struct apfs_fs_info *fs_info,
struct apfs_block_rsv *rsv)
{
u64 needed_bytes;
int ret;
/* 1 for slack space, 1 for updating the inode */
needed_bytes = apfs_calc_insert_metadata_size(fs_info, 1) +
apfs_calc_metadata_size(fs_info, 1);
spin_lock(&rsv->lock);
if (rsv->reserved < needed_bytes)
ret = -ENOSPC;
else
ret = 0;
spin_unlock(&rsv->lock);
return ret;
}
int apfs_truncate_free_space_cache(struct apfs_trans_handle *trans,
struct apfs_block_group *block_group,
struct inode *inode)
{
struct apfs_root *root = APFS_I(inode)->root;
int ret = 0;
bool locked = false;
if (block_group) {
struct apfs_path *path = apfs_alloc_path();
if (!path) {
ret = -ENOMEM;
goto fail;
}
locked = true;
mutex_lock(&trans->transaction->cache_write_mutex);
if (!list_empty(&block_group->io_list)) {
list_del_init(&block_group->io_list);
apfs_wait_cache_io(trans, block_group, path);
apfs_put_block_group(block_group);
}
/*
* now that we've truncated the cache away, its no longer
* setup or written
*/
spin_lock(&block_group->lock);
block_group->disk_cache_state = APFS_DC_CLEAR;
spin_unlock(&block_group->lock);
apfs_free_path(path);
}
apfs_i_size_write(APFS_I(inode), 0);
truncate_pagecache(inode, 0);
/*
* We skip the throttling logic for free space cache inodes, so we don't
* need to check for -EAGAIN.
*/
ret = apfs_truncate_inode_items(trans, root, APFS_I(inode),
0, APFS_EXTENT_DATA_KEY, NULL);
if (ret)
goto fail;
ret = apfs_update_inode(trans, root, APFS_I(inode));
fail:
if (locked)
mutex_unlock(&trans->transaction->cache_write_mutex);
if (ret)
apfs_abort_transaction(trans, ret);
return ret;
}
static void readahead_cache(struct inode *inode)
{
struct file_ra_state *ra;
unsigned long last_index;
ra = kzalloc(sizeof(*ra), GFP_NOFS);
if (!ra)
return;
file_ra_state_init(ra, inode->i_mapping);
last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
kfree(ra);
}
static int io_ctl_init(struct apfs_io_ctl *io_ctl, struct inode *inode,
int write)
{
int num_pages;
num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
/* Make sure we can fit our crcs and generation into the first page */
if (write && (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
return -ENOSPC;
memset(io_ctl, 0, sizeof(struct apfs_io_ctl));
io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
if (!io_ctl->pages)
return -ENOMEM;
io_ctl->num_pages = num_pages;
io_ctl->fs_info = apfs_sb(inode->i_sb);
io_ctl->inode = inode;
return 0;
}
ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
static void io_ctl_free(struct apfs_io_ctl *io_ctl)
{
kfree(io_ctl->pages);
io_ctl->pages = NULL;
}
static void io_ctl_unmap_page(struct apfs_io_ctl *io_ctl)
{
if (io_ctl->cur) {
io_ctl->cur = NULL;
io_ctl->orig = NULL;
}
}
static void io_ctl_map_page(struct apfs_io_ctl *io_ctl, int clear)
{
ASSERT(io_ctl->index < io_ctl->num_pages);
io_ctl->page = io_ctl->pages[io_ctl->index++];
io_ctl->cur = page_address(io_ctl->page);
io_ctl->orig = io_ctl->cur;
io_ctl->size = PAGE_SIZE;
if (clear)
clear_page(io_ctl->cur);
}
static void io_ctl_drop_pages(struct apfs_io_ctl *io_ctl)
{
int i;
io_ctl_unmap_page(io_ctl);
for (i = 0; i < io_ctl->num_pages; i++) {
if (io_ctl->pages[i]) {
ClearPageChecked(io_ctl->pages[i]);
unlock_page(io_ctl->pages[i]);
put_page(io_ctl->pages[i]);
}
}
}
static int io_ctl_prepare_pages(struct apfs_io_ctl *io_ctl, bool uptodate)
{
struct page *page;
struct inode *inode = io_ctl->inode;
gfp_t mask = apfs_alloc_write_mask(inode->i_mapping);
int i;
for (i = 0; i < io_ctl->num_pages; i++) {
int ret;
page = find_or_create_page(inode->i_mapping, i, mask);
if (!page) {
io_ctl_drop_pages(io_ctl);
return -ENOMEM;
}
ret = set_page_extent_mapped(page);
if (ret < 0) {
unlock_page(page);
put_page(page);
io_ctl_drop_pages(io_ctl);
return ret;
}
io_ctl->pages[i] = page;
if (uptodate && !PageUptodate(page)) {
apfs_readpage(NULL, page);
lock_page(page);
if (page->mapping != inode->i_mapping) {
apfs_err(APFS_I(inode)->root->fs_info,
"free space cache page truncated");
io_ctl_drop_pages(io_ctl);
return -EIO;
}
if (!PageUptodate(page)) {
apfs_err(APFS_I(inode)->root->fs_info,
"error reading free space cache");
io_ctl_drop_pages(io_ctl);
return -EIO;
}
}
}
for (i = 0; i < io_ctl->num_pages; i++)
clear_page_dirty_for_io(io_ctl->pages[i]);
return 0;
}
static void io_ctl_set_generation(struct apfs_io_ctl *io_ctl, u64 generation)
{
io_ctl_map_page(io_ctl, 1);
/*
* Skip the csum areas. If we don't check crcs then we just have a
* 64bit chunk at the front of the first page.
*/
io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
put_unaligned_le64(generation, io_ctl->cur);
io_ctl->cur += sizeof(u64);
}
static int io_ctl_check_generation(struct apfs_io_ctl *io_ctl, u64 generation)
{
u64 cache_gen;
/*
* Skip the crc area. If we don't check crcs then we just have a 64bit
* chunk at the front of the first page.
*/
io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
cache_gen = get_unaligned_le64(io_ctl->cur);
if (cache_gen != generation) {
apfs_err_rl(io_ctl->fs_info,
"space cache generation (%llu) does not match inode (%llu)",
cache_gen, generation);
io_ctl_unmap_page(io_ctl);
return -EIO;
}
io_ctl->cur += sizeof(u64);
return 0;
}
static void io_ctl_set_crc(struct apfs_io_ctl *io_ctl, int index)
{
u32 *tmp;
u32 crc = ~(u32)0;
unsigned offset = 0;
if (index == 0)
offset = sizeof(u32) * io_ctl->num_pages;
crc = apfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
apfs_crc32c_final(crc, (u8 *)&crc);
io_ctl_unmap_page(io_ctl);
tmp = page_address(io_ctl->pages[0]);
tmp += index;
*tmp = crc;
}
static int io_ctl_check_crc(struct apfs_io_ctl *io_ctl, int index)
{
u32 *tmp, val;
u32 crc = ~(u32)0;
unsigned offset = 0;
if (index == 0)
offset = sizeof(u32) * io_ctl->num_pages;
tmp = page_address(io_ctl->pages[0]);
tmp += index;
val = *tmp;
io_ctl_map_page(io_ctl, 0);
crc = apfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
apfs_crc32c_final(crc, (u8 *)&crc);
if (val != crc) {
apfs_err_rl(io_ctl->fs_info,
"csum mismatch on free space cache");
io_ctl_unmap_page(io_ctl);
return -EIO;
}
return 0;
}
static int io_ctl_add_entry(struct apfs_io_ctl *io_ctl, u64 offset, u64 bytes,
void *bitmap)
{
struct apfs_free_space_entry *entry;
if (!io_ctl->cur)
return -ENOSPC;
entry = io_ctl->cur;
put_unaligned_le64(offset, &entry->offset);
put_unaligned_le64(bytes, &entry->bytes);
entry->type = (bitmap) ? APFS_FREE_SPACE_BITMAP :
APFS_FREE_SPACE_EXTENT;
io_ctl->cur += sizeof(struct apfs_free_space_entry);
io_ctl->size -= sizeof(struct apfs_free_space_entry);
if (io_ctl->size >= sizeof(struct apfs_free_space_entry))
return 0;
io_ctl_set_crc(io_ctl, io_ctl->index - 1);
/* No more pages to map */
if (io_ctl->index >= io_ctl->num_pages)
return 0;
/* map the next page */
io_ctl_map_page(io_ctl, 1);
return 0;
}
static int io_ctl_add_bitmap(struct apfs_io_ctl *io_ctl, void *bitmap)
{
if (!io_ctl->cur)
return -ENOSPC;
/*
* If we aren't at the start of the current page, unmap this one and
* map the next one if there is any left.
*/
if (io_ctl->cur != io_ctl->orig) {
io_ctl_set_crc(io_ctl, io_ctl->index - 1);
if (io_ctl->index >= io_ctl->num_pages)
return -ENOSPC;
io_ctl_map_page(io_ctl, 0);
}
copy_page(io_ctl->cur, bitmap);
io_ctl_set_crc(io_ctl, io_ctl->index - 1);
if (io_ctl->index < io_ctl->num_pages)
io_ctl_map_page(io_ctl, 0);
return 0;
}
static void io_ctl_zero_remaining_pages(struct apfs_io_ctl *io_ctl)
{
/*
* If we're not on the boundary we know we've modified the page and we
* need to crc the page.
*/
if (io_ctl->cur != io_ctl->orig)
io_ctl_set_crc(io_ctl, io_ctl->index - 1);
else
io_ctl_unmap_page(io_ctl);
while (io_ctl->index < io_ctl->num_pages) {
io_ctl_map_page(io_ctl, 1);
io_ctl_set_crc(io_ctl, io_ctl->index - 1);
}
}
static int io_ctl_read_entry(struct apfs_io_ctl *io_ctl,
struct apfs_free_space *entry, u8 *type)
{
struct apfs_free_space_entry *e;
int ret;
if (!io_ctl->cur) {
ret = io_ctl_check_crc(io_ctl, io_ctl->index);
if (ret)
return ret;
}
e = io_ctl->cur;
entry->offset = get_unaligned_le64(&e->offset);
entry->bytes = get_unaligned_le64(&e->bytes);
*type = e->type;
io_ctl->cur += sizeof(struct apfs_free_space_entry);
io_ctl->size -= sizeof(struct apfs_free_space_entry);
if (io_ctl->size >= sizeof(struct apfs_free_space_entry))
return 0;
io_ctl_unmap_page(io_ctl);
return 0;
}
static int io_ctl_read_bitmap(struct apfs_io_ctl *io_ctl,
struct apfs_free_space *entry)
{
int ret;
ret = io_ctl_check_crc(io_ctl, io_ctl->index);
if (ret)
return ret;
copy_page(entry->bitmap, io_ctl->cur);
io_ctl_unmap_page(io_ctl);
return 0;
}
static void recalculate_thresholds(struct apfs_free_space_ctl *ctl)
{
struct apfs_block_group *block_group = ctl->private;
u64 max_bytes;
u64 bitmap_bytes;
u64 extent_bytes;
u64 size = block_group->length;
u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
max_bitmaps = max_t(u64, max_bitmaps, 1);
ASSERT(ctl->total_bitmaps <= max_bitmaps);
/*
* We are trying to keep the total amount of memory used per 1GiB of
* space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation
* mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of
* bitmaps, we may end up using more memory than this.
*/
if (size < SZ_1G)
max_bytes = MAX_CACHE_BYTES_PER_GIG;
else
max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
bitmap_bytes = ctl->total_bitmaps * ctl->unit;
/*
* we want the extent entry threshold to always be at most 1/2 the max
* bytes we can have, or whatever is less than that.
*/
extent_bytes = max_bytes - bitmap_bytes;
extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
ctl->extents_thresh =
div_u64(extent_bytes, sizeof(struct apfs_free_space));
}
static int __load_free_space_cache(struct apfs_root *root, struct inode *inode,
struct apfs_free_space_ctl *ctl,
struct apfs_path *path, u64 offset)
{
struct apfs_fs_info *fs_info = root->fs_info;
struct apfs_free_space_header *header;
struct extent_buffer *leaf;
struct apfs_io_ctl io_ctl;
struct apfs_key key = {};
struct apfs_free_space *e, *n;
LIST_HEAD(bitmaps);
u64 num_entries;
u64 num_bitmaps;
u64 generation;
u8 type;
int ret = 0;
/* Nothing in the space cache, goodbye */
if (!i_size_read(inode))
return 0;
key.objectid = APFS_FREE_SPACE_OBJECTID;
key.offset = offset;
key.type = 0;
ret = apfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
return 0;
else if (ret > 0) {
apfs_release_path(path);
return 0;
}
ret = -1;
leaf = path->nodes[0];
header = apfs_item_ptr(leaf, path->slots[0],
struct apfs_free_space_header);
num_entries = apfs_free_space_entries(leaf, header);
num_bitmaps = apfs_free_space_bitmaps(leaf, header);
generation = apfs_free_space_generation(leaf, header);
apfs_release_path(path);
if (!APFS_I(inode)->generation) {
apfs_info(fs_info,
"the free space cache file (%llu) is invalid, skip it",
offset);
return 0;
}
if (APFS_I(inode)->generation != generation) {
apfs_err(fs_info,
"free space inode generation (%llu) did not match free space cache generation (%llu)",
APFS_I(inode)->generation, generation);
return 0;
}
if (!num_entries)
return 0;
ret = io_ctl_init(&io_ctl, inode, 0);
if (ret)
return ret;
readahead_cache(inode);
ret = io_ctl_prepare_pages(&io_ctl, true);
if (ret)
goto out;
ret = io_ctl_check_crc(&io_ctl, 0);
if (ret)
goto free_cache;
ret = io_ctl_check_generation(&io_ctl, generation);
if (ret)
goto free_cache;
while (num_entries) {
e = kmem_cache_zalloc(apfs_free_space_cachep,
GFP_NOFS);
if (!e) {
ret = -ENOMEM;
goto free_cache;
}
ret = io_ctl_read_entry(&io_ctl, e, &type);
if (ret) {
kmem_cache_free(apfs_free_space_cachep, e);
goto free_cache;
}
if (!e->bytes) {
ret = -1;
kmem_cache_free(apfs_free_space_cachep, e);
goto free_cache;
}
if (type == APFS_FREE_SPACE_EXTENT) {
spin_lock(&ctl->tree_lock);
ret = link_free_space(ctl, e);
spin_unlock(&ctl->tree_lock);
if (ret) {
apfs_err(fs_info,
"Duplicate entries in free space cache, dumping");
kmem_cache_free(apfs_free_space_cachep, e);
goto free_cache;
}
} else {
ASSERT(num_bitmaps);
num_bitmaps--;
e->bitmap = kmem_cache_zalloc(
apfs_free_space_bitmap_cachep, GFP_NOFS);
if (!e->bitmap) {
ret = -ENOMEM;
kmem_cache_free(
apfs_free_space_cachep, e);
goto free_cache;
}
spin_lock(&ctl->tree_lock);
ret = link_free_space(ctl, e);
ctl->total_bitmaps++;
recalculate_thresholds(ctl);
spin_unlock(&ctl->tree_lock);
if (ret) {
apfs_err(fs_info,
"Duplicate entries in free space cache, dumping");
kmem_cache_free(apfs_free_space_cachep, e);
goto free_cache;
}
list_add_tail(&e->list, &bitmaps);
}
num_entries--;
}
io_ctl_unmap_page(&io_ctl);
/*
* We add the bitmaps at the end of the entries in order that
* the bitmap entries are added to the cache.
*/
list_for_each_entry_safe(e, n, &bitmaps, list) {
list_del_init(&e->list);
ret = io_ctl_read_bitmap(&io_ctl, e);
if (ret)
goto free_cache;
}
io_ctl_drop_pages(&io_ctl);
ret = 1;
out:
io_ctl_free(&io_ctl);
return ret;
free_cache:
io_ctl_drop_pages(&io_ctl);
__apfs_remove_free_space_cache(ctl);
goto out;
}
static int copy_free_space_cache(struct apfs_block_group *block_group,
struct apfs_free_space_ctl *ctl)
{
struct apfs_free_space *info;
struct rb_node *n;
int ret = 0;
while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) {
info = rb_entry(n, struct apfs_free_space, offset_index);
if (!info->bitmap) {
unlink_free_space(ctl, info);
ret = apfs_add_free_space(block_group, info->offset,
info->bytes);
kmem_cache_free(apfs_free_space_cachep, info);
} else {
u64 offset = info->offset;
u64 bytes = ctl->unit;
while (search_bitmap(ctl, info, &offset, &bytes,
false) == 0) {
ret = apfs_add_free_space(block_group, offset,
bytes);
if (ret)
break;
bitmap_clear_bits(ctl, info, offset, bytes);
offset = info->offset;
bytes = ctl->unit;
}
free_bitmap(ctl, info);
}
cond_resched();
}
return ret;
}
int load_free_space_cache(struct apfs_block_group *block_group)
{
struct apfs_fs_info *fs_info = block_group->fs_info;
struct apfs_free_space_ctl *ctl = block_group->free_space_ctl;
struct apfs_free_space_ctl tmp_ctl = {};
struct inode *inode;
struct apfs_path *path;
int ret = 0;
bool matched;
u64 used = block_group->used;
/*
* Because we could potentially discard our loaded free space, we want
* to load everything into a temporary structure first, and then if it's
* valid copy it all into the actual free space ctl.
*/
apfs_init_free_space_ctl(block_group, &tmp_ctl);
/*
* If this block group has been marked to be cleared for one reason or
* another then we can't trust the on disk cache, so just return.
*/
spin_lock(&block_group->lock);
if (block_group->disk_cache_state != APFS_DC_WRITTEN) {
spin_unlock(&block_group->lock);
return 0;
}
spin_unlock(&block_group->lock);
path = apfs_alloc_path();
if (!path)
return 0;
path->search_commit_root = 1;
path->skip_locking = 1;
/*
* We must pass a path with search_commit_root set to apfs_iget in
* order to avoid a deadlock when allocating extents for the tree root.
*
* When we are COWing an extent buffer from the tree root, when looking
* for a free extent, at extent-tree.c:find_free_extent(), we can find
* block group without its free space cache loaded. When we find one
* we must load its space cache which requires reading its free space
* cache's inode item from the root tree. If this inode item is located
* in the same leaf that we started COWing before, then we end up in
* deadlock on the extent buffer (trying to read lock it when we
* previously write locked it).
*
* It's safe to read the inode item using the commit root because
* block groups, once loaded, stay in memory forever (until they are
* removed) as well as their space caches once loaded. New block groups
* once created get their ->cached field set to APFS_CACHE_FINISHED so
* we will never try to read their inode item while the fs is mounted.
*/
inode = lookup_free_space_inode(block_group, path);
if (IS_ERR(inode)) {
apfs_free_path(path);
return 0;
}
/* We may have converted the inode and made the cache invalid. */
spin_lock(&block_group->lock);
if (block_group->disk_cache_state != APFS_DC_WRITTEN) {
spin_unlock(&block_group->lock);
apfs_free_path(path);
goto out;
}
spin_unlock(&block_group->lock);
ret = __load_free_space_cache(fs_info->tree_root, inode, &tmp_ctl,
path, block_group->start);
apfs_free_path(path);
if (ret <= 0)
goto out;
matched = (tmp_ctl.free_space == (block_group->length - used -
block_group->bytes_super));
if (matched) {
ret = copy_free_space_cache(block_group, &tmp_ctl);
/*
* ret == 1 means we successfully loaded the free space cache,
* so we need to re-set it here.
*/
if (ret == 0)
ret = 1;
} else {
__apfs_remove_free_space_cache(&tmp_ctl);
apfs_warn(fs_info,
"block group %llu has wrong amount of free space",
block_group->start);
ret = -1;
}
out:
if (ret < 0) {
/* This cache is bogus, make sure it gets cleared */
spin_lock(&block_group->lock);
block_group->disk_cache_state = APFS_DC_CLEAR;