-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.c
1413 lines (1167 loc) · 34.8 KB
/
snapshot.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
/*
* BRIEF DESCRIPTION
*
* Snapshot support
*
* Copyright 2015-2016 Regents of the University of California,
* UCSD Non-Volatile Systems Lab, Andiry Xu <[email protected]>
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <[email protected]>
* Copyright 2003 Sony Corporation
* Copyright 2003 Matsushita Electric Industrial Co., Ltd.
* 2003-2004 (c) MontaVista Software, Inc. , Steve Longerbeam
*
* This program is free software; you can redistribute it and/or modify it
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#include "nova.h"
#include "inode.h"
#include "super.h"
static inline u64 next_list_page(u64 curr_p)
{
void *curr_addr = (void *)curr_p;
unsigned long page_tail = ((unsigned long)curr_addr & ~PAGE_OFFSET_MASK)
+ LOG_BLOCK_TAIL;
return ((struct nova_inode_page_tail *)page_tail)->next_page;
}
static inline bool goto_next_list_page(struct super_block *sb, u64 curr_p)
{
void *addr;
u8 type;
/* Each kind of entry takes at least 32 bytes */
if (ENTRY_LOC(curr_p) + 32 > LOG_BLOCK_TAIL)
return true;
addr = (void *)curr_p;
type = nova_get_entry_type(addr);
if (type == NEXT_PAGE)
return true;
return false;
}
static int nova_find_target_snapshot_info(struct super_block *sb,
u64 epoch_id, struct snapshot_info **ret_info)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_info *infos[1];
int nr_infos;
int ret = 0;
nr_infos = radix_tree_gang_lookup(&sbi->snapshot_info_tree,
(void **)infos, epoch_id, 1);
if (nr_infos == 1) {
*ret_info = infos[0];
ret = 1;
}
return ret;
}
static struct snapshot_info *
nova_find_next_snapshot_info(struct super_block *sb, struct snapshot_info *info)
{
struct snapshot_info *ret_info = NULL;
int ret;
ret = nova_find_target_snapshot_info(sb, info->epoch_id + 1, &ret_info);
if (ret == 1 && ret_info->epoch_id <= info->epoch_id) {
nova_err(sb, "info epoch id %llu, next epoch id %llu\n",
info->epoch_id, ret_info->epoch_id);
ret_info = NULL;
}
return ret_info;
}
static int nova_insert_snapshot_info(struct super_block *sb,
struct snapshot_info *info)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
int ret;
ret = radix_tree_insert(&sbi->snapshot_info_tree, info->epoch_id, info);
if (ret)
nova_dbg("%s ERROR %d\n", __func__, ret);
return ret;
}
/* Reuse the inode log page structure */
static inline void nova_set_link_page_epoch_id(struct super_block *sb,
struct nova_inode_log_page *curr_page, u64 epoch_id)
{
curr_page->page_tail.epoch_id = epoch_id;
}
/* Reuse the inode log page structure */
static inline void nova_set_next_link_page_address(struct super_block *sb,
struct nova_inode_log_page *curr_page, u64 next_page)
{
curr_page->page_tail.next_page = next_page;
}
static int nova_delete_snapshot_list_entries(struct super_block *sb,
struct snapshot_list *list)
{
struct snapshot_file_write_entry *w_entry = NULL;
struct snapshot_inode_entry *i_entry = NULL;
struct nova_inode_info_header sih;
void *addr;
u64 curr_p;
u8 type;
sih.ino = NOVA_SNAPSHOT_INO;
sih.i_blk_type = NOVA_DEFAULT_BLOCK_TYPE;
sih.log_head = sih.log_tail = 0;
curr_p = list->head;
nova_dbg_verbose("Snapshot list head 0x%llx, tail 0x%lx\n",
curr_p, list->tail);
if (curr_p == 0 && list->tail == 0)
return 0;
while (curr_p != list->tail) {
if (goto_next_list_page(sb, curr_p)) {
curr_p = next_list_page(curr_p);
if (curr_p == list->tail)
break;
}
if (curr_p == 0) {
nova_err(sb, "Snapshot list is NULL!\n");
BUG();
}
addr = (void *)curr_p;
type = nova_get_entry_type(addr);
switch (type) {
case SS_INODE:
i_entry = (struct snapshot_inode_entry *)addr;
if (i_entry->deleted == 0)
nova_delete_dead_inode(sb, i_entry->nova_ino);
curr_p += sizeof(struct snapshot_inode_entry);
continue;
case SS_FILE_WRITE:
w_entry = (struct snapshot_file_write_entry *)addr;
if (w_entry->deleted == 0)
// TODO: Handle error.
nova_free_data_blocks(sb, &sih, w_entry->nvmm,
w_entry->num_pages);
curr_p += sizeof(struct snapshot_file_write_entry);
continue;
default:
nova_err(sb, "unknown type %d, 0x%llx, tail 0x%llx\n",
type, curr_p, list->tail);
NOVA_ASSERT(0);
curr_p += sizeof(struct snapshot_file_write_entry);
continue;
}
}
return 0;
}
static inline int nova_background_clean_inode_entry(struct super_block *sb,
struct snapshot_inode_entry *i_entry, u64 epoch_id)
{
if (i_entry->deleted == 0 && i_entry->delete_epoch_id <= epoch_id) {
nova_delete_dead_inode(sb, i_entry->nova_ino);
i_entry->deleted = 1;
}
return 0;
}
static inline int nova_background_clean_write_entry(struct super_block *sb,
struct snapshot_file_write_entry *w_entry,
struct nova_inode_info_header *sih, u64 epoch_id)
{
if (w_entry->deleted == 0 && w_entry->delete_epoch_id <= epoch_id) {
nova_deref_blocks(sb, w_entry->nvmm, w_entry->num_pages);
w_entry->deleted = 1;
}
return 0;
}
static int nova_background_clean_snapshot_list(struct super_block *sb,
struct snapshot_list *list, u64 epoch_id)
{
struct nova_inode_log_page *curr_page;
struct nova_inode_info_header sih;
void *addr;
u64 curr_p;
u8 type;
sih.ino = NOVA_SNAPSHOT_INO;
sih.i_blk_type = NOVA_DEFAULT_BLOCK_TYPE;
sih.log_head = sih.log_tail = 0;
curr_p = list->head;
nova_dbg_verbose("Snapshot list head 0x%llx, tail 0x%lx\n",
curr_p, list->tail);
if (curr_p == 0 && list->tail == 0)
return 0;
curr_page = (struct nova_inode_log_page *)curr_p;
while (curr_page->page_tail.epoch_id < epoch_id &&
curr_p != list->tail) {
if (goto_next_list_page(sb, curr_p)) {
curr_p = next_list_page(curr_p);
if (curr_p == list->tail)
break;
curr_page = (struct nova_inode_log_page *)curr_p;
if (curr_page->page_tail.epoch_id == epoch_id)
break;
}
if (curr_p == 0) {
nova_err(sb, "Snapshot list is NULL!\n");
BUG();
}
addr = (void *)curr_p;
type = nova_get_entry_type(addr);
switch (type) {
case SS_INODE:
nova_background_clean_inode_entry(sb, addr, epoch_id);
curr_p += sizeof(struct snapshot_inode_entry);
continue;
case SS_FILE_WRITE:
nova_background_clean_write_entry(sb, addr, &sih,
epoch_id);
curr_p += sizeof(struct snapshot_file_write_entry);
continue;
default:
nova_err(sb, "unknown type %d, 0x%llx, tail 0x%llx\n",
type, curr_p, list->tail);
NOVA_ASSERT(0);
curr_p += sizeof(struct snapshot_file_write_entry);
continue;
}
}
return 0;
}
static int nova_delete_snapshot_list_pages(struct super_block *sb,
struct snapshot_list *list)
{
struct nova_inode_log_page *curr_page;
u64 curr_block = list->head;
int freed = 0;
while (curr_block) {
if (ENTRY_LOC(curr_block)) {
nova_dbg("%s: ERROR: invalid block %llu\n",
__func__, curr_block);
break;
}
curr_page = (struct nova_inode_log_page *)curr_block;
curr_block = curr_page->page_tail.next_page;
kfree(curr_page);
freed++;
}
return freed;
}
static int nova_delete_snapshot_list(struct super_block *sb,
struct snapshot_list *list, int delete_entries)
{
if (delete_entries)
nova_delete_snapshot_list_entries(sb, list);
nova_delete_snapshot_list_pages(sb, list);
return 0;
}
static int nova_delete_snapshot_info(struct super_block *sb,
struct snapshot_info *info, int delete_entries)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_list *list;
int i;
for (i = 0; i < sbi->cpus; i++) {
list = &info->lists[i];
mutex_lock(&list->list_mutex);
nova_delete_snapshot_list(sb, list, delete_entries);
mutex_unlock(&list->list_mutex);
}
kfree(info->lists);
return 0;
}
static int nova_initialize_snapshot_info_pages(struct super_block *sb,
struct snapshot_info *info, u64 epoch_id)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_list *list;
unsigned long new_page = 0;
int i;
for (i = 0; i < sbi->cpus; i++) {
list = &info->lists[i];
new_page = (unsigned long)kmalloc(PAGE_SIZE,
GFP_KERNEL);
/* Aligned to PAGE_SIZE */
if (!new_page || ENTRY_LOC(new_page)) {
nova_dbg("%s: failed\n", __func__);
kfree((void *)new_page);
return -ENOMEM;
}
nova_set_link_page_epoch_id(sb, (void *)new_page, epoch_id);
nova_set_next_link_page_address(sb, (void *)new_page, 0);
list->tail = list->head = new_page;
list->num_pages = 1;
}
return 0;
}
static int nova_initialize_snapshot_info(struct super_block *sb,
struct snapshot_info **ret_info, int init_pages, u64 epoch_id)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_info *info;
struct snapshot_list *list;
int i;
int ret;
INIT_TIMING(init_snapshot_time);
NOVA_START_TIMING(init_snapshot_info_t, init_snapshot_time);
info = nova_alloc_snapshot_info(sb);
if (!info) {
ret = -ENOMEM;
goto out;
}
info->lists = kzalloc(sbi->cpus * sizeof(struct snapshot_list),
GFP_KERNEL);
if (!info->lists) {
nova_free_snapshot_info(info);
ret = -ENOMEM;
goto fail;
}
for (i = 0; i < sbi->cpus; i++) {
list = &info->lists[i];
mutex_init(&list->list_mutex);
}
if (init_pages) {
ret = nova_initialize_snapshot_info_pages(sb, info, epoch_id);
if (ret)
goto fail;
}
*ret_info = info;
out:
NOVA_END_TIMING(init_snapshot_info_t, init_snapshot_time);
return ret;
fail:
for (i = 0; i < sbi->cpus; i++) {
list = &info->lists[i];
if (list->head)
kfree((void *)list->head);
}
kfree(info->lists);
nova_free_snapshot_info(info);
*ret_info = NULL;
goto out;
}
static void nova_write_snapshot_list_entry(struct super_block *sb,
struct snapshot_list *list, u64 curr_p, void *entry, size_t size)
{
if (is_last_entry(curr_p, size)) {
nova_err(sb, "%s: write to page end? curr 0x%llx, size %lu\n",
__func__, curr_p, size);
return;
}
memcpy((void *)curr_p, entry, size);
list->tail = curr_p + size;
}
static int nova_append_snapshot_list_entry(struct super_block *sb,
struct snapshot_info *info, void *entry, size_t size)
{
struct snapshot_list *list;
struct nova_inode_log_page *curr_page;
u64 curr_block;
int cpuid;
u64 curr_p;
u64 new_page = 0;
cpuid = nova_get_cpuid(sb);
list = &info->lists[cpuid];
retry:
mutex_lock(&list->list_mutex);
curr_p = list->tail;
if (new_page) {
/* Link prev block and newly allocated page */
curr_block = BLOCK_OFF(curr_p);
curr_page = (struct nova_inode_log_page *)curr_block;
nova_set_next_link_page_address(sb, curr_page, new_page);
list->num_pages++;
}
if ((is_last_entry(curr_p, size) && next_list_page(curr_p) == 0)) {
nova_set_entry_type((void *)curr_p, NEXT_PAGE);
if (new_page == 0) {
mutex_unlock(&list->list_mutex);
new_page = (unsigned long)kmalloc(PAGE_SIZE,
GFP_KERNEL);
if (!new_page || ENTRY_LOC(new_page)) {
kfree((void *)new_page);
nova_err(sb, "%s: allocation failed\n",
__func__);
return -ENOMEM;
}
nova_set_link_page_epoch_id(sb, (void *)new_page,
info->epoch_id);
nova_set_next_link_page_address(sb,
(void *)new_page, 0);
goto retry;
}
}
if (is_last_entry(curr_p, size)) {
nova_set_entry_type((void *)curr_p, NEXT_PAGE);
curr_p = next_list_page(curr_p);
}
nova_write_snapshot_list_entry(sb, list, curr_p, entry, size);
mutex_unlock(&list->list_mutex);
return 0;
}
/*
* An entry is deleteable if
* 1) It is created after the last snapshot, or
* 2) It is created and deleted during the same snapshot period.
*/
static int nova_old_entry_deleteable(struct super_block *sb,
u64 create_epoch_id, u64 delete_epoch_id,
struct snapshot_info **ret_info)
{
struct snapshot_info *info = NULL;
int ret;
if (create_epoch_id == delete_epoch_id) {
/* Create and delete in the same epoch */
return 1;
}
ret = nova_find_target_snapshot_info(sb, create_epoch_id, &info);
if (ret == 0) {
/* Old entry does not belong to any snapshot */
return 1;
}
if (info->epoch_id >= delete_epoch_id) {
/* Create and delete in different epoch but same snapshot */
return 1;
}
*ret_info = info;
return 0;
}
static int nova_append_snapshot_file_write_entry(struct super_block *sb,
struct snapshot_info *info, u64 nvmm, u64 num_pages,
u64 delete_epoch_id)
{
struct snapshot_file_write_entry entry;
int ret;
INIT_TIMING(append_time);
if (!info) {
nova_dbg("%s: Snapshot info not found\n", __func__);
return -EINVAL;
}
NOVA_START_TIMING(append_snapshot_file_t, append_time);
nova_dbgv("Append file write entry: block %llu, %llu pages, delete epoch ID %llu to Snapshot epoch ID %llu\n",
nvmm, num_pages, delete_epoch_id,
info->epoch_id);
memset(&entry, 0, sizeof(struct snapshot_file_write_entry));
entry.type = SS_FILE_WRITE;
entry.deleted = 0;
entry.nvmm = nvmm;
entry.num_pages = num_pages;
entry.delete_epoch_id = delete_epoch_id;
ret = nova_append_snapshot_list_entry(sb, info, &entry,
sizeof(struct snapshot_file_write_entry));
NOVA_END_TIMING(append_snapshot_file_t, append_time);
return ret;
}
/* entry given to this function is a copy in dram */
int nova_append_data_to_snapshot(struct super_block *sb,
struct nova_file_write_entry *entry, u64 nvmm, u64 num_pages,
u64 delete_epoch_id)
{
struct snapshot_info *info = NULL;
int ret;
ret = nova_old_entry_deleteable(sb, entry->epoch_id,
delete_epoch_id, &info);
if (ret == 0)
nova_append_snapshot_file_write_entry(sb, info, nvmm,
num_pages, delete_epoch_id);
return ret;
}
static int nova_append_snapshot_inode_entry(struct super_block *sb,
struct nova_inode *pi, struct snapshot_info *info)
{
struct snapshot_inode_entry entry;
int ret;
INIT_TIMING(append_time);
if (!info) {
nova_dbg("%s: Snapshot info not found\n", __func__);
return -EINVAL;
}
NOVA_START_TIMING(append_snapshot_inode_t, append_time);
nova_dbgv("Append inode entry: inode %llu, delete epoch ID %llu to Snapshot epoch ID %llu\n",
pi->nova_ino, pi->delete_epoch_id,
info->epoch_id);
memset(&entry, 0, sizeof(struct snapshot_inode_entry));
entry.type = SS_INODE;
entry.deleted = 0;
entry.nova_ino = pi->nova_ino;
entry.delete_epoch_id = pi->delete_epoch_id;
ret = nova_append_snapshot_list_entry(sb, info, &entry,
sizeof(struct snapshot_inode_entry));
NOVA_END_TIMING(append_snapshot_inode_t, append_time);
return ret;
}
int nova_append_inode_to_snapshot(struct super_block *sb,
struct nova_inode *pi)
{
struct snapshot_info *info = NULL;
int ret;
ret = nova_old_entry_deleteable(sb, pi->create_epoch_id,
pi->delete_epoch_id, &info);
if (ret == 0)
nova_append_snapshot_inode_entry(sb, pi, info);
return ret;
}
int nova_encounter_mount_snapshot(struct super_block *sb, void *addr,
u8 type)
{
struct nova_dentry *dentry;
struct nova_setattr_logentry *attr_entry;
struct nova_link_change_entry *linkc_entry;
struct nova_file_write_entry *fw_entry;
struct nova_mmap_entry *mmap_entry;
int ret = 0;
switch (type) {
case SET_ATTR:
attr_entry = (struct nova_setattr_logentry *)addr;
if (pass_mount_snapshot(sb, attr_entry->epoch_id))
ret = 1;
break;
case LINK_CHANGE:
linkc_entry = (struct nova_link_change_entry *)addr;
if (pass_mount_snapshot(sb, linkc_entry->epoch_id))
ret = 1;
break;
case DIR_LOG:
dentry = (struct nova_dentry *)addr;
if (pass_mount_snapshot(sb, dentry->epoch_id))
ret = 1;
break;
case FILE_WRITE:
fw_entry = (struct nova_file_write_entry *)addr;
if (pass_mount_snapshot(sb, fw_entry->epoch_id))
ret = 1;
break;
case MMAP_WRITE:
mmap_entry = (struct nova_mmap_entry *)addr;
if (pass_mount_snapshot(sb, mmap_entry->epoch_id))
ret = 1;
break;
default:
break;
}
return ret;
}
static int nova_copy_snapshot_list_to_dram(struct super_block *sb,
struct snapshot_list *list, struct snapshot_nvmm_list *nvmm_list)
{
struct nova_inode_log_page *dram_page;
void *curr_nvmm_addr;
u64 curr_nvmm_block;
u64 prev_dram_addr;
u64 curr_dram_addr;
unsigned long i;
int ret;
curr_dram_addr = list->head;
prev_dram_addr = list->head;
curr_nvmm_block = nvmm_list->head;
curr_nvmm_addr = nova_get_block(sb, curr_nvmm_block);
for (i = 0; i < nvmm_list->num_pages; i++) {
/* Leave next_page field alone */
ret = memcpy_mcsafe((void *)curr_dram_addr, curr_nvmm_addr,
LOG_BLOCK_TAIL);
if (ret < 0) {
nova_dbg("%s: Copy nvmm page %lu failed\n",
__func__, i);
continue;
}
dram_page = (struct nova_inode_log_page *)curr_dram_addr;
prev_dram_addr = curr_dram_addr;
curr_nvmm_block = next_log_page(sb, curr_nvmm_block);
if (curr_nvmm_block < 0)
break;
curr_nvmm_addr = nova_get_block(sb, curr_nvmm_block);
curr_dram_addr = dram_page->page_tail.next_page;
}
list->num_pages = nvmm_list->num_pages;
list->tail = prev_dram_addr + ENTRY_LOC(nvmm_list->tail);
return 0;
}
static int nova_allocate_snapshot_list_pages(struct super_block *sb,
struct snapshot_list *list, struct snapshot_nvmm_list *nvmm_list,
u64 epoch_id)
{
unsigned long prev_page = 0;
unsigned long new_page = 0;
unsigned long i;
for (i = 0; i < nvmm_list->num_pages; i++) {
new_page = (unsigned long)kmalloc(PAGE_SIZE,
GFP_KERNEL);
if (!new_page) {
nova_dbg("%s ERROR: fail to allocate list pages\n",
__func__);
goto fail;
}
nova_set_link_page_epoch_id(sb, (void *)new_page, epoch_id);
nova_set_next_link_page_address(sb, (void *)new_page, 0);
if (i == 0)
list->head = new_page;
if (prev_page)
nova_set_next_link_page_address(sb, (void *)prev_page,
new_page);
prev_page = new_page;
}
return 0;
fail:
nova_delete_snapshot_list_pages(sb, list);
return -ENOMEM;
}
static int nova_restore_snapshot_info_lists(struct super_block *sb,
struct snapshot_info *info, struct nova_snapshot_info_entry *entry,
u64 epoch_id)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_nvmm_page *nvmm_page;
struct snapshot_list *list;
struct snapshot_nvmm_list *nvmm_list;
int i;
int ret;
nvmm_page = (struct snapshot_nvmm_page *)nova_get_block(sb,
entry->nvmm_page_addr);
for (i = 0; i < sbi->cpus; i++) {
list = &info->lists[i];
nvmm_list = &nvmm_page->lists[i];
if (!list || !nvmm_list) {
nova_dbg("%s: list NULL? list %p, nvmm list %p\n",
__func__, list, nvmm_list);
continue;
}
ret = nova_allocate_snapshot_list_pages(sb, list,
nvmm_list, info->epoch_id);
if (ret) {
nova_dbg("%s failure\n", __func__);
return ret;
}
nova_copy_snapshot_list_to_dram(sb, list, nvmm_list);
}
return 0;
}
static int nova_restore_snapshot_info(struct super_block *sb,
struct nova_snapshot_info_entry *entry, u64 epoch_id,
u64 timestamp, u64 curr_p, int just_init)
{
struct snapshot_info *info = NULL;
int ret = 0;
nova_dbg("Restore snapshot epoch ID %llu\n", epoch_id);
/* Allocate list pages on demand later */
ret = nova_initialize_snapshot_info(sb, &info, just_init, epoch_id);
if (ret) {
nova_dbg("%s: initialize snapshot info failed %d\n",
__func__, ret);
goto fail;
}
info->epoch_id = epoch_id;
info->timestamp = timestamp;
info->snapshot_entry = curr_p;
if (just_init == 0) {
ret = nova_restore_snapshot_info_lists(sb, info,
entry, epoch_id);
if (ret)
goto fail;
}
ret = nova_insert_snapshot_info(sb, info);
return ret;
fail:
nova_delete_snapshot_info(sb, info, 0);
return ret;
}
int nova_mount_snapshot(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
u64 epoch_id;
epoch_id = sbi->mount_snapshot_epoch_id;
nova_dbg("Mount snapshot %llu\n", epoch_id);
return 0;
}
static int nova_free_nvmm_page(struct super_block *sb,
u64 nvmm_page_addr)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_nvmm_page *nvmm_page;
struct snapshot_nvmm_list *nvmm_list;
struct nova_inode_info_header sih;
unsigned long nvmm_blocknr;
int i;
if (nvmm_page_addr == 0)
return 0;
sih.ino = NOVA_SNAPSHOT_INO;
sih.i_blk_type = NOVA_DEFAULT_BLOCK_TYPE;
nvmm_page = (struct snapshot_nvmm_page *)nova_get_block(sb,
nvmm_page_addr);
for (i = 0; i < sbi->cpus; i++) {
nvmm_list = &nvmm_page->lists[i];
sih.log_head = nvmm_list->head;
sih.log_tail = nvmm_list->tail;
sih.alter_log_head = sih.alter_log_tail = 0;
nova_free_inode_log(sb, NULL, &sih);
}
nvmm_blocknr = nova_get_blocknr(sb, nvmm_page_addr, 0);
nova_free_log_block(sb, nvmm_blocknr);
return 0;
}
static int nova_set_nvmm_page_addr(struct super_block *sb,
struct nova_snapshot_info_entry *entry, u64 nvmm_page_addr)
{
unsigned long irq_flags = 0;
nova_memunlock_range(sb, entry, CACHELINE_SIZE, &irq_flags);
entry->nvmm_page_addr = nvmm_page_addr;
nova_update_entry_csum(entry);
nova_update_alter_entry(sb, entry);
nova_memlock_range(sb, entry, CACHELINE_SIZE, &irq_flags);
return 0;
}
static int nova_clear_nvmm_page(struct super_block *sb,
struct nova_snapshot_info_entry *entry, int just_init)
{
if (just_init)
/* No need to free because we do not set the bitmap. */
goto out;
nova_free_nvmm_page(sb, entry->nvmm_page_addr);
out:
nova_set_nvmm_page_addr(sb, entry, 0);
return 0;
}
int nova_restore_snapshot_entry(struct super_block *sb,
struct nova_snapshot_info_entry *entry, u64 curr_p, int just_init)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
u64 epoch_id, timestamp;
int ret = 0;
if (entry->deleted == 1)
goto out;
epoch_id = entry->epoch_id;
timestamp = entry->timestamp;
ret = nova_restore_snapshot_info(sb, entry, epoch_id,
timestamp, curr_p, just_init);
if (ret) {
nova_dbg("%s: Restore snapshot epoch ID %llu failed\n",
__func__, epoch_id);
goto out;
}
if (epoch_id > sbi->s_epoch_id)
sbi->s_epoch_id = epoch_id;
out:
nova_clear_nvmm_page(sb, entry, just_init);
return ret;
}
static int nova_append_snapshot_info_log(struct super_block *sb,
struct snapshot_info *info, u64 epoch_id, u64 timestamp)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode_info *si = sbi->snapshot_si;
struct nova_inode *pi = nova_get_reserved_inode(sb, NOVA_SNAPSHOT_INO);
struct nova_inode_update update;
struct nova_snapshot_info_entry entry_info;
int ret;
unsigned long irq_flags = 0;
entry_info.type = SNAPSHOT_INFO;
entry_info.deleted = 0;
entry_info.nvmm_page_addr = 0;
entry_info.epoch_id = epoch_id;
entry_info.timestamp = timestamp;
update.tail = update.alter_tail = 0;
ret = nova_append_snapshot_info_entry(sb, pi, si, info,
&entry_info, &update);
if (ret) {
nova_dbg("%s: append snapshot info entry failure\n", __func__);
return ret;
}
nova_memunlock_inode(sb, pi, &irq_flags);
nova_update_inode(sb, &si->vfs_inode, pi, &update, 1);
nova_memlock_inode(sb, pi, &irq_flags);
return 0;
}
int nova_create_snapshot(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_info *info = NULL;
u64 timestamp = 0;
u64 epoch_id;
int ret;
INIT_TIMING(create_snapshot_time);
struct timespec64 now;
NOVA_START_TIMING(create_snapshot_t, create_snapshot_time);
mutex_lock(&sbi->s_lock);
sbi->snapshot_taking = 1;
/* Increase the epoch id, but use the old value as snapshot id */
epoch_id = sbi->s_epoch_id++;
/*
* Mark the create_snapshot_epoch_id before starting the snapshot
* creation. We will check this during in-place updates for metadata
* and data, to prevent overwriting logs that might belong to a
* snapshot that is being created.
*/
nova_info("%s: epoch id %llu\n", __func__, epoch_id);
ktime_get_coarse_real_ts64(&now);
timestamp = timespec64_trunc(now,
sb->s_time_gran).tv_sec;
ret = nova_initialize_snapshot_info(sb, &info, 1, epoch_id);
if (ret) {
nova_dbg("%s: initialize snapshot info failed %d\n",
__func__, ret);
NOVA_END_TIMING(create_snapshot_t, create_snapshot_time);
goto out;
}
info->epoch_id = epoch_id;
info->timestamp = timestamp;
ret = nova_append_snapshot_info_log(sb, info, epoch_id, timestamp);
if (ret) {
nova_free_snapshot_info(info);
NOVA_END_TIMING(create_snapshot_t, create_snapshot_time);
goto out;
}
sbi->num_snapshots++;
ret = nova_insert_snapshot_info(sb, info);
nova_set_vmas_readonly(sb);
sbi->nova_sb->s_wtime = cpu_to_le32(get_seconds());
sbi->nova_sb->s_epoch_id = cpu_to_le64(epoch_id);
nova_update_super_crc(sb);
nova_sync_super(sb);
out:
sbi->snapshot_taking = 0;
mutex_unlock(&sbi->s_lock);
wake_up_interruptible(&sbi->snapshot_mmap_wait);
NOVA_END_TIMING(create_snapshot_t, create_snapshot_time);
return ret;
}
static void wakeup_snapshot_cleaner(struct nova_sb_info *sbi)
{
if (!waitqueue_active(&sbi->snapshot_cleaner_wait))
return;
nova_dbg("Wakeup snapshot cleaner thread\n");
wake_up_interruptible(&sbi->snapshot_cleaner_wait);
}
static int nova_link_to_next_snapshot(struct super_block *sb,
struct snapshot_info *prev_info, struct snapshot_info *next_info)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_list *prev_list, *next_list;
struct nova_inode_log_page *curr_page;
u64 curr_block, curr_p;
int i;
nova_dbg("Link deleted snapshot %llu to next snapshot %llu\n",
prev_info->epoch_id, next_info->epoch_id);