-
Notifications
You must be signed in to change notification settings - Fork 1
/
super.c
1279 lines (1044 loc) · 32.1 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
/*
* BRIEF DESCRIPTION
*
* Super block operations.
*
* Copyright 2015-2016 Regents of the University of California,
* UCSD Non-Volatile Systems Lab, Andiry Xu <jix024@cs.ucsd.edu>
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <marco.stornelli@gmail.com>
* 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 <linux/module.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/parser.h>
#include <linux/vfs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/seq_file.h>
#include <linux/mount.h>
#include <linux/mm.h>
#include <linux/ctype.h>
#include <linux/bitops.h>
#include <linux/magic.h>
#include <linux/exportfs.h>
#include <linux/random.h>
#include <linux/cred.h>
#include <linux/list.h>
#include <linux/dax.h>
#include "nova.h"
#include "journal.h"
#include "super.h"
#include "inode.h"
int measure_timing;
int metadata_csum;
int wprotect;
int data_csum;
int data_parity;
int dram_struct_csum;
int support_clwb;
int support_clflushopt;
int inplace_data_updates;
module_param(measure_timing, int, 0444);
MODULE_PARM_DESC(measure_timing, "Timing measurement");
module_param(metadata_csum, int, 0444);
MODULE_PARM_DESC(metadata_csum, "Protect metadata structures with replication and checksums");
module_param(wprotect, int, 0444);
MODULE_PARM_DESC(wprotect, "Write-protect pmem region and use CR0.WP to allow updates");
module_param(data_csum, int, 0444);
MODULE_PARM_DESC(data_csum, "Detect corruption of data pages using checksum");
module_param(data_parity, int, 0444);
MODULE_PARM_DESC(data_parity, "Protect file data using RAID-5 style parity.");
module_param(inplace_data_updates, int, 0444);
MODULE_PARM_DESC(inplace_data_updates, "Perform data updates in-place (i.e., not atomically)");
module_param(dram_struct_csum, int, 0444);
MODULE_PARM_DESC(dram_struct_csum, "Protect key DRAM data structures with checksums");
module_param(nova_dbgmask, int, 0444);
MODULE_PARM_DESC(nova_dbgmask, "Control debugging output");
static struct super_operations nova_sops;
static const struct export_operations nova_export_ops;
static struct kmem_cache *nova_inode_cachep;
static struct kmem_cache *nova_range_node_cachep;
static struct kmem_cache *nova_snapshot_info_cachep;
/* FIXME: should the following variable be one per NOVA instance? */
unsigned int nova_dbgmask;
void nova_error_mng(struct super_block *sb, const char *fmt, ...)
{
va_list args;
printk(KERN_CRIT "nova error: ");
va_start(args, fmt);
vprintk(fmt, args);
va_end(args);
if (test_opt(sb, ERRORS_PANIC))
panic("nova: panic from previous error\n");
if (test_opt(sb, ERRORS_RO)) {
printk(KERN_CRIT "nova err: remounting filesystem read-only");
sb->s_flags |= MS_RDONLY;
}
}
static void nova_set_blocksize(struct super_block *sb, unsigned long size)
{
int bits;
/*
* We've already validated the user input and the value here must be
* between NOVA_MAX_BLOCK_SIZE and NOVA_MIN_BLOCK_SIZE
* and it must be a power of 2.
*/
bits = fls(size) - 1;
sb->s_blocksize_bits = bits;
sb->s_blocksize = (1 << bits);
}
static int nova_get_nvmm_info(struct super_block *sb,
struct nova_sb_info *sbi)
{
void *virt_addr = NULL;
void *virt_addr2 = NULL;
pfn_t __pfn_t;
pfn_t __pfn_t_2;
long size;
long size2;
struct dax_device *dax_dev;
int ret;
ret = bdev_dax_supported(sb, PAGE_SIZE);
nova_dbg_verbose("%s: dax_supported = %d; bdev->super=0x%p",
__func__, ret, sb->s_bdev->bd_super);
if (ret) {
nova_err(sb, "device does not support DAX\n");
return ret;
}
sbi->s_bdev = sb->s_bdev;
dax_dev = fs_dax_get_by_host(sb->s_bdev->bd_disk->disk_name);
if (!dax_dev) {
nova_err(sb, "Couldn't retrieve DAX device.\n");
return -EINVAL;
}
sbi->s_dax_dev = dax_dev;
printk(KERN_INFO "%s: doing first direct access\n", __func__);
size = dax_direct_access(sbi->s_dax_dev, 0, LONG_MAX/PAGE_SIZE,
&virt_addr, &__pfn_t) * PAGE_SIZE;
if (size <= 0) {
nova_err(sb, "direct_access failed\n");
return -EINVAL;
}
printk(KERN_INFO "%s: first direct access returned size = %lu\n", __func__, size);
printk(KERN_INFO "%s: doing second direct access\n", __func__);
/*
size2 = dax_direct_access(sbi->s_dax_dev, (((512*PAGE_SIZE) + size) / PAGE_SIZE), LONG_MAX/PAGE_SIZE,
&virt_addr2, &__pfn_t_2) * PAGE_SIZE;
*/
size2 = dax_direct_access(sbi->s_dax_dev, ((size) / PAGE_SIZE), LONG_MAX/PAGE_SIZE,
&virt_addr2, &__pfn_t_2) * PAGE_SIZE;
if (size2 <= 0) {
nova_err(sb, "second direct access failed\n");
return -EINVAL;
}
printk(KERN_INFO "%s: second direct access returned size = %lu\n", __func__, size2);
sbi->virt_addr = virt_addr;
sbi->virt_addr_2 = virt_addr2;
if (!sbi->virt_addr) {
nova_err(sb, "ioremap of the nova image failed(1)\n");
return -EINVAL;
}
sbi->phys_addr = pfn_t_to_pfn(__pfn_t) << PAGE_SHIFT;
sbi->phys_addr_2 = pfn_t_to_pfn(__pfn_t_2) << PAGE_SHIFT;
sbi->initsize = size;
sbi->initsize_2 = size2;
sbi->replica_reserved_inodes_addr = virt_addr2 + size2 -
(sbi->tail_reserved_blocks << PAGE_SHIFT);
sbi->replica_sb_addr = virt_addr2 + size2 - PAGE_SIZE;
nova_dbg("%s: dev %s, phys_addr 0x%llx, virt_addr 0x%lx, size %ld, virt_addr_end 0x%lx virt_addr_2 0x%lx, size2 %ld virt_addr_2_end 0x%lx\n",
__func__, sbi->s_bdev->bd_disk->disk_name,
sbi->phys_addr, (unsigned long)sbi->virt_addr, sbi->initsize, (unsigned long) sbi->virt_addr + (unsigned long) sbi->initsize, (unsigned long)sbi->virt_addr_2, sbi->initsize_2, (unsigned long) sbi->virt_addr_2 + (unsigned long) sbi->initsize_2);
return 0;
}
static loff_t nova_max_size(int bits)
{
loff_t res;
res = (1ULL << 63) - 1;
if (res > MAX_LFS_FILESIZE)
res = MAX_LFS_FILESIZE;
nova_dbg_verbose("max file size %llu bytes\n", res);
return res;
}
enum {
Opt_bpi, Opt_init, Opt_snapshot, Opt_mode, Opt_uid,
Opt_gid, Opt_dax, Opt_wprotect,
Opt_err_cont, Opt_err_panic, Opt_err_ro,
Opt_dbgmask, Opt_err
};
static const match_table_t tokens = {
{ Opt_bpi, "bpi=%u" },
{ Opt_init, "init" },
{ Opt_snapshot, "snapshot=%u" },
{ Opt_mode, "mode=%o" },
{ Opt_uid, "uid=%u" },
{ Opt_gid, "gid=%u" },
{ Opt_dax, "dax" },
{ Opt_wprotect, "wprotect" },
{ Opt_err_cont, "errors=continue" },
{ Opt_err_panic, "errors=panic" },
{ Opt_err_ro, "errors=remount-ro" },
{ Opt_dbgmask, "dbgmask=%u" },
{ Opt_err, NULL },
};
static int nova_parse_options(char *options, struct nova_sb_info *sbi,
bool remount)
{
char *p;
substring_t args[MAX_OPT_ARGS];
int option;
kuid_t uid;
if (!options)
return 0;
while ((p = strsep(&options, ",")) != NULL) {
int token;
if (!*p)
continue;
token = match_token(p, tokens, args);
switch (token) {
case Opt_bpi:
if (match_int(&args[0], &option))
goto bad_val;
if (remount && sbi->bpi)
goto bad_opt;
sbi->bpi = option;
break;
case Opt_uid:
if (match_int(&args[0], &option))
goto bad_val;
uid = make_kuid(current_user_ns(), option);
if (remount && !uid_eq(sbi->uid, uid))
goto bad_opt;
sbi->uid = uid;
break;
case Opt_gid:
if (match_int(&args[0], &option))
goto bad_val;
sbi->gid = make_kgid(current_user_ns(), option);
break;
case Opt_mode:
if (match_octal(&args[0], &option))
goto bad_val;
sbi->mode = option & 01777U;
break;
case Opt_init:
if (remount)
goto bad_opt;
set_opt(sbi->s_mount_opt, FORMAT);
break;
case Opt_snapshot:
if (match_int(&args[0], &option))
goto bad_val;
sbi->mount_snapshot = 1;
sbi->mount_snapshot_epoch_id = option;
break;
case Opt_err_panic:
clear_opt(sbi->s_mount_opt, ERRORS_CONT);
clear_opt(sbi->s_mount_opt, ERRORS_RO);
set_opt(sbi->s_mount_opt, ERRORS_PANIC);
break;
case Opt_err_ro:
clear_opt(sbi->s_mount_opt, ERRORS_CONT);
clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
set_opt(sbi->s_mount_opt, ERRORS_RO);
break;
case Opt_err_cont:
clear_opt(sbi->s_mount_opt, ERRORS_RO);
clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
set_opt(sbi->s_mount_opt, ERRORS_CONT);
break;
case Opt_dax:
set_opt(sbi->s_mount_opt, DAX);
break;
case Opt_wprotect:
if (remount)
goto bad_opt;
set_opt(sbi->s_mount_opt, PROTECT);
nova_info("NOVA: Enabling new Write Protection (CR0.WP)\n");
break;
case Opt_dbgmask:
if (match_int(&args[0], &option))
goto bad_val;
nova_dbgmask = option;
break;
default: {
goto bad_opt;
}
}
}
return 0;
bad_val:
nova_info("Bad value '%s' for mount option '%s'\n", args[0].from,
p);
return -EINVAL;
bad_opt:
nova_info("Bad mount option: \"%s\"\n", p);
return -EINVAL;
}
/* Make sure we have enough space */
static bool nova_check_size(struct super_block *sb, unsigned long size)
{
unsigned long minimum_size;
/* space required for super block and root directory.*/
minimum_size = (HEAD_RESERVED_BLOCKS + TAIL_RESERVED_BLOCKS + 1)
<< sb->s_blocksize_bits;
if (size < minimum_size)
return false;
return true;
}
static inline int nova_check_super_checksum(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
u32 crc = 0;
// Check CRC but skip c_sum, which is the 4 bytes at the beginning
crc = nova_crc32c(~0, (__u8 *)sbi->nova_sb + sizeof(__le32),
sizeof(struct nova_super_block) - sizeof(__le32));
if (sbi->nova_sb->s_sum == cpu_to_le32(crc))
return 0;
else
return 1;
}
inline void nova_sync_super(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_super_block *super = nova_get_super(sb);
struct nova_super_block *super_redund;
nova_memunlock_super(sb);
super_redund = nova_get_redund_super(sb);
memcpy_to_pmem_nocache((void *)super, (void *)sbi->nova_sb,
sizeof(struct nova_super_block));
PERSISTENT_BARRIER();
memcpy_to_pmem_nocache((void *)super_redund, (void *)sbi->nova_sb,
sizeof(struct nova_super_block));
PERSISTENT_BARRIER();
nova_memlock_super(sb);
}
/* Update checksum for the DRAM copy */
inline void nova_update_super_crc(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
u32 crc = 0;
sbi->nova_sb->s_wtime = cpu_to_le32(get_seconds());
sbi->nova_sb->s_sum = 0;
crc = nova_crc32c(~0, (__u8 *)sbi->nova_sb + sizeof(__le32),
sizeof(struct nova_super_block) - sizeof(__le32));
sbi->nova_sb->s_sum = cpu_to_le32(crc);
}
static inline void nova_update_mount_time(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
u64 mnt_write_time;
mnt_write_time = (get_seconds() & 0xFFFFFFFF);
mnt_write_time = mnt_write_time | (mnt_write_time << 32);
sbi->nova_sb->s_mtime = cpu_to_le64(mnt_write_time);
nova_update_super_crc(sb);
nova_sync_super(sb);
}
static struct nova_inode *nova_init(struct super_block *sb,
unsigned long size, unsigned long size2)
{
unsigned long blocksize;
struct nova_inode *root_i, *pi;
struct nova_super_block *super;
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode_update update;
u64 epoch_id;
timing_t init_time;
NOVA_START_TIMING(new_init_t, init_time);
nova_info("creating an empty nova of size %lu\n", size + size2);
sbi->num_blocks = ((unsigned long)(size) >> PAGE_SHIFT);
sbi->num_blocks_2 = ((unsigned long)(size2) >> PAGE_SHIFT);
nova_dbgv("nova: Default block size set to 4K\n");
sbi->blocksize = blocksize = NOVA_DEF_BLOCK_SIZE_4K;
nova_set_blocksize(sb, sbi->blocksize);
if (!nova_check_size(sb, size)) {
nova_warn("Specified NOVA size too small 0x%lx.\n", size);
return ERR_PTR(-EINVAL);
}
nova_dbgv("max file name len %d\n", (unsigned int)NOVA_NAME_LEN);
super = nova_get_super(sb);
nova_memunlock_reserved(sb, super);
/* clear out super-block and inode table */
memset_nt(super, 0, sbi->head_reserved_blocks * sbi->blocksize);
pi = nova_get_inode_by_ino(sb, NOVA_BLOCKNODE_INO);
pi->nova_ino = NOVA_BLOCKNODE_INO;
nova_flush_buffer(pi, CACHELINE_SIZE, 1);
pi = nova_get_inode_by_ino(sb, NOVA_SNAPSHOT_INO);
pi->nova_ino = NOVA_SNAPSHOT_INO;
nova_flush_buffer(pi, CACHELINE_SIZE, 1);
memset(&update, 0, sizeof(struct nova_inode_update));
nova_update_inode(sb, &sbi->snapshot_si->vfs_inode, pi, &update, 1);
nova_memlock_reserved(sb, super);
nova_init_blockmap(sb, 0);
if (nova_lite_journal_hard_init(sb) < 0) {
nova_err(sb, "Lite journal hard initialization failed\n");
return ERR_PTR(-EINVAL);
}
if (nova_init_inode_inuse_list(sb) < 0)
return ERR_PTR(-EINVAL);
if (nova_init_inode_table(sb) < 0)
return ERR_PTR(-EINVAL);
sbi->nova_sb->s_size = cpu_to_le64(size);
sbi->nova_sb->s_size_2 = cpu_to_le64(size2);
sbi->nova_sb->s_blocksize = cpu_to_le32(blocksize);
sbi->nova_sb->s_magic = cpu_to_le32(NOVA_SUPER_MAGIC);
sbi->nova_sb->s_epoch_id = 0;
sbi->nova_sb->s_metadata_csum = metadata_csum;
sbi->nova_sb->s_data_csum = data_csum;
sbi->nova_sb->s_data_parity = data_parity;
nova_update_super_crc(sb);
nova_sync_super(sb);
root_i = nova_get_inode_by_ino(sb, NOVA_ROOT_INO);
nova_dbgv("%s: Allocate root inode @ 0x%p\n", __func__, root_i);
nova_memunlock_inode(sb, root_i);
root_i->i_mode = cpu_to_le16(sbi->mode | S_IFDIR);
root_i->i_uid = cpu_to_le32(from_kuid(&init_user_ns, sbi->uid));
root_i->i_gid = cpu_to_le32(from_kgid(&init_user_ns, sbi->gid));
root_i->i_links_count = cpu_to_le16(2);
root_i->i_blk_type = NOVA_BLOCK_TYPE_4K;
root_i->i_flags = 0;
root_i->i_size = cpu_to_le64(sb->s_blocksize);
root_i->i_atime = root_i->i_mtime = root_i->i_ctime =
cpu_to_le32(get_seconds());
root_i->nova_ino = cpu_to_le64(NOVA_ROOT_INO);
root_i->valid = 1;
/* nova_sync_inode(root_i); */
nova_flush_buffer(root_i, sizeof(*root_i), false);
nova_memlock_inode(sb, root_i);
epoch_id = nova_get_epoch_id(sb);
nova_append_dir_init_entries(sb, root_i, NOVA_ROOT_INO,
NOVA_ROOT_INO, epoch_id);
PERSISTENT_MARK();
PERSISTENT_BARRIER();
NOVA_END_TIMING(new_init_t, init_time);
nova_info("NOVA initialization finish\n");
return root_i;
}
static inline void set_default_opts(struct nova_sb_info *sbi)
{
set_opt(sbi->s_mount_opt, HUGEIOREMAP);
set_opt(sbi->s_mount_opt, ERRORS_CONT);
sbi->head_reserved_blocks = HEAD_RESERVED_BLOCKS;
sbi->tail_reserved_blocks = TAIL_RESERVED_BLOCKS;
sbi->cpus = num_online_cpus();
nova_info("%d cpus online\n", sbi->cpus);
sbi->map_id = 0;
sbi->snapshot_si = NULL;
}
static void nova_root_check(struct super_block *sb, struct nova_inode *root_pi)
{
if (!S_ISDIR(le16_to_cpu(root_pi->i_mode)))
nova_warn("root is not a directory!\n");
}
/* Check super block magic and checksum */
static int nova_check_super(struct super_block *sb,
struct nova_super_block *ps)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
int rc;
rc = memcpy_mcsafe(sbi->nova_sb, ps,
sizeof(struct nova_super_block));
if (rc < 0)
return rc;
if (le32_to_cpu(sbi->nova_sb->s_magic) != NOVA_SUPER_MAGIC)
return -EIO;
if (nova_check_super_checksum(sb))
return -EIO;
return 0;
}
/* Check if we disable protection previously and enable it now */
/* FIXME */
static int nova_check_module_params(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
if (sbi->nova_sb->s_metadata_csum != metadata_csum) {
nova_dbg("%s metadata checksum\n",
sbi->nova_sb->s_metadata_csum ? "Enable" : "Disable");
metadata_csum = sbi->nova_sb->s_metadata_csum;
}
if (sbi->nova_sb->s_data_csum != data_csum) {
nova_dbg("%s data checksum\n",
sbi->nova_sb->s_data_csum ? "Enable" : "Disable");
data_csum = sbi->nova_sb->s_data_csum;
}
if (sbi->nova_sb->s_data_parity != data_parity) {
nova_dbg("%s data parity\n",
sbi->nova_sb->s_data_parity ? "Enable" : "Disable");
data_parity = sbi->nova_sb->s_data_parity;
}
return 0;
}
static int nova_check_integrity(struct super_block *sb)
{
struct nova_super_block *super = nova_get_super(sb);
struct nova_super_block *super_redund;
int rc;
super_redund = nova_get_redund_super(sb);
/* Do sanity checks on the superblock */
rc = nova_check_super(sb, super);
if (rc < 0) {
rc = nova_check_super(sb, super_redund);
if (rc < 0) {
nova_err(sb, "Can't find a valid nova partition\n");
return rc;
} else
nova_warn("Error in super block: try to repair it with the other copy\n");
}
nova_sync_super(sb);
nova_check_module_params(sb);
return 0;
}
static int nova_fill_super(struct super_block *sb, void *data, int silent)
{
struct nova_inode *root_pi;
struct nova_sb_info *sbi = NULL;
struct inode *root_i = NULL;
struct inode_map *inode_map;
unsigned long blocksize;
size_t strp_size = NOVA_STRIPE_SIZE;
u32 random = 0;
int retval = -EINVAL;
int i;
timing_t mount_time;
NOVA_START_TIMING(mount_t, mount_time);
BUILD_BUG_ON(sizeof(struct nova_super_block) > NOVA_SB_SIZE);
BUILD_BUG_ON(sizeof(struct nova_inode) > NOVA_INODE_SIZE);
BUILD_BUG_ON(sizeof(struct nova_inode_log_page) != PAGE_SIZE);
BUILD_BUG_ON(sizeof(struct journal_ptr_pair) > CACHELINE_SIZE);
BUILD_BUG_ON(PAGE_SIZE/sizeof(struct nova_lite_journal_entry) <
NOVA_MAX_JOURNAL_LENGTH);
BUILD_BUG_ON(sizeof(struct nova_inode_page_tail) +
LOG_BLOCK_TAIL != PAGE_SIZE);
sbi = kzalloc(sizeof(struct nova_sb_info), GFP_KERNEL);
if (!sbi)
return -ENOMEM;
sbi->nova_sb = kzalloc(sizeof(struct nova_super_block), GFP_KERNEL);
if (!sbi->nova_sb) {
kfree(sbi);
return -ENOMEM;
}
sb->s_fs_info = sbi;
sbi->sb = sb;
set_default_opts(sbi);
/* Currently the log page supports 64 journal pointer pairs */
if (sbi->cpus > MAX_CPUS) {
nova_err(sb, "NOVA needs more log pointer pages to support more than "
__stringify(MAX_CPUS) " cpus.\n");
goto out;
}
retval = nova_get_nvmm_info(sb, sbi);
if (retval) {
nova_err(sb, "%s: Failed to get nvmm info.",
__func__);
goto out;
}
nova_dbg("measure timing %d, metadata checksum %d, inplace update %d, wprotect %d, data checksum %d, data parity %d, DRAM checksum %d\n",
measure_timing, metadata_csum,
inplace_data_updates, wprotect, data_csum,
data_parity, dram_struct_csum);
get_random_bytes(&random, sizeof(u32));
atomic_set(&sbi->next_generation, random);
/* Init with default values */
sbi->mode = (0755);
sbi->uid = current_fsuid();
sbi->gid = current_fsgid();
set_opt(sbi->s_mount_opt, HUGEIOREMAP);
mutex_init(&sbi->vma_mutex);
INIT_LIST_HEAD(&sbi->mmap_sih_list);
sbi->inode_maps = kcalloc(sbi->cpus, sizeof(struct inode_map),
GFP_KERNEL);
if (!sbi->inode_maps) {
retval = -ENOMEM;
nova_dbg("%s: Allocating inode maps failed.",
__func__);
goto out;
}
for (i = 0; i < sbi->cpus; i++) {
inode_map = &sbi->inode_maps[i];
mutex_init(&inode_map->inode_table_mutex);
inode_map->inode_inuse_tree = RB_ROOT;
}
mutex_init(&sbi->s_lock);
sbi->zeroed_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
if (!sbi->zeroed_page) {
retval = -ENOMEM;
nova_dbg("%s: sbi->zeroed_page failed.",
__func__);
goto out;
}
for (i = 0; i < 8; i++)
sbi->zero_csum[i] = nova_crc32c(NOVA_INIT_CSUM,
sbi->zeroed_page, strp_size);
sbi->zero_parity = kzalloc(strp_size, GFP_KERNEL);
if (!sbi->zero_parity) {
retval = -ENOMEM;
nova_err(sb, "%s: sbi->zero_parity failed.",
__func__);
goto out;
}
sbi->snapshot_si = kmem_cache_alloc(nova_inode_cachep, GFP_NOFS);
nova_snapshot_init(sb);
retval = nova_parse_options(data, sbi, 0);
if (retval) {
nova_err(sb, "%s: Failed to parse nova command line options.",
__func__);
goto out;
}
if (nova_alloc_block_free_lists(sb)) {
retval = -ENOMEM;
nova_err(sb, "%s: Failed to allocate block free lists.",
__func__);
goto out;
}
nova_sysfs_init(sb);
/* Init a new nova instance */
if (sbi->s_mount_opt & NOVA_MOUNT_FORMAT) {
root_pi = nova_init(sb, sbi->initsize, sbi->initsize_2);
retval = -ENOMEM;
if (IS_ERR(root_pi)) {
nova_err(sb, "%s: root_pi error.",
__func__);
goto out;
}
goto setup_sb;
}
nova_dbg_verbose("checking physical address 0x%016llx for nova image\n",
(u64)sbi->phys_addr);
if (nova_check_integrity(sb) < 0) {
retval = -EINVAL;
nova_dbg("Memory contains invalid nova %x:%x\n",
le32_to_cpu(sbi->nova_sb->s_magic), NOVA_SUPER_MAGIC);
goto out;
}
if (nova_lite_journal_soft_init(sb)) {
retval = -EINVAL;
nova_err(sb, "Lite journal initialization failed\n");
goto out;
}
if (sbi->mount_snapshot) {
retval = nova_mount_snapshot(sb);
if (retval) {
nova_err(sb, "Mount snapshot failed\n");
goto out;
}
}
blocksize = le32_to_cpu(sbi->nova_sb->s_blocksize);
nova_set_blocksize(sb, blocksize);
nova_dbg_verbose("blocksize %lu\n", blocksize);
/* Read the root inode */
root_pi = nova_get_inode_by_ino(sb, NOVA_ROOT_INO);
/* Check that the root inode is in a sane state */
nova_root_check(sb, root_pi);
/* Set it all up.. */
setup_sb:
sb->s_magic = le32_to_cpu(sbi->nova_sb->s_magic);
sb->s_op = &nova_sops;
sb->s_maxbytes = nova_max_size(sb->s_blocksize_bits);
sb->s_time_gran = 1000000000; // 1 second.
sb->s_export_op = &nova_export_ops;
sb->s_xattr = NULL;
sb->s_flags |= MS_NOSEC;
/* If the FS was not formatted on this mount, scan the meta-data after
* truncate list has been processed
*/
if ((sbi->s_mount_opt & NOVA_MOUNT_FORMAT) == 0)
nova_recovery(sb);
root_i = nova_iget(sb, NOVA_ROOT_INO);
if (IS_ERR(root_i)) {
retval = PTR_ERR(root_i);
nova_err(sb, "%s: failed to get root inode",
__func__);
goto out;
}
sb->s_root = d_make_root(root_i);
if (!sb->s_root) {
nova_err(sb, "get nova root inode failed\n");
retval = -ENOMEM;
goto out;
}
if (!(sb->s_flags & MS_RDONLY))
nova_update_mount_time(sb);
nova_print_curr_epoch_id(sb);
retval = 0;
NOVA_END_TIMING(mount_t, mount_time);
return retval;
out:
if (sbi->snapshot_si) {
kmem_cache_free(nova_inode_cachep, sbi->snapshot_si);
sbi->snapshot_si = NULL;
}
kfree(sbi->zeroed_page);
sbi->zeroed_page = NULL;
kfree(sbi->zero_parity);
sbi->zero_parity = NULL;
kfree(sbi->free_lists);
sbi->free_lists = NULL;
kfree(sbi->journal_locks);
sbi->journal_locks = NULL;
kfree(sbi->inode_maps);
sbi->inode_maps = NULL;
nova_sysfs_exit(sb);
kfree(sbi->nova_sb);
kfree(sbi);
nova_dbg("%s failed: return %d\n", __func__, retval);
return retval;
}
int nova_statfs(struct dentry *d, struct kstatfs *buf)
{
struct super_block *sb = d->d_sb;
struct nova_sb_info *sbi = (struct nova_sb_info *)sb->s_fs_info;
buf->f_type = NOVA_SUPER_MAGIC;
buf->f_bsize = sb->s_blocksize;
buf->f_blocks = sbi->num_blocks + sbi->num_blocks_2;
buf->f_bfree = buf->f_bavail = nova_count_free_blocks(sb);
buf->f_files = LONG_MAX;
buf->f_ffree = LONG_MAX - sbi->s_inodes_used_count;
buf->f_namelen = NOVA_NAME_LEN;
nova_dbg_verbose("nova_stats: total 4k free blocks 0x%llx\n",
buf->f_bfree);
return 0;
}
static int nova_show_options(struct seq_file *seq, struct dentry *root)
{
struct nova_sb_info *sbi = NOVA_SB(root->d_sb);
//seq_printf(seq, ",physaddr=0x%016llx", (u64)sbi->phys_addr);
//if (sbi->initsize)
// seq_printf(seq, ",init=%luk", sbi->initsize >> 10);
//if (sbi->blocksize)
// seq_printf(seq, ",bs=%lu", sbi->blocksize);
//if (sbi->bpi)
// seq_printf(seq, ",bpi=%lu", sbi->bpi);
if (sbi->mode != (0777 | S_ISVTX))
seq_printf(seq, ",mode=%03o", sbi->mode);
if (uid_valid(sbi->uid))
seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->uid));
if (gid_valid(sbi->gid))
seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->gid));
if (test_opt(root->d_sb, ERRORS_RO))
seq_puts(seq, ",errors=remount-ro");
if (test_opt(root->d_sb, ERRORS_PANIC))
seq_puts(seq, ",errors=panic");
/* memory protection disabled by default */
if (test_opt(root->d_sb, PROTECT))
seq_puts(seq, ",wprotect");
if (test_opt(root->d_sb, DAX))
seq_puts(seq, ",dax");
return 0;
}
int nova_remount(struct super_block *sb, int *mntflags, char *data)
{
unsigned long old_sb_flags;
unsigned long old_mount_opt;
struct nova_sb_info *sbi = NOVA_SB(sb);
int ret = -EINVAL;
/* Store the old options */
mutex_lock(&sbi->s_lock);
old_sb_flags = sb->s_flags;
old_mount_opt = sbi->s_mount_opt;
if (nova_parse_options(data, sbi, 1))
goto restore_opt;
sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
((sbi->s_mount_opt & NOVA_MOUNT_POSIX_ACL) ?
MS_POSIXACL : 0);
if ((*mntflags & MS_RDONLY) != (sb->s_flags & MS_RDONLY))
nova_update_mount_time(sb);
mutex_unlock(&sbi->s_lock);
ret = 0;
return ret;
restore_opt:
sb->s_flags = old_sb_flags;
sbi->s_mount_opt = old_mount_opt;
mutex_unlock(&sbi->s_lock);
return ret;
}
static void nova_put_super(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct inode_map *inode_map;
int i;
nova_print_curr_epoch_id(sb);
/* It's unmount time, so unmap the nova memory */
// nova_print_free_lists(sb);
if (sbi->virt_addr) {
nova_save_snapshots(sb);
kmem_cache_free(nova_inode_cachep, sbi->snapshot_si);
nova_save_inode_list_to_log(sb);
/* Save everything before blocknode mapping! */
nova_save_blocknode_mappings_to_log(sb);
sbi->virt_addr = NULL;
}
nova_delete_free_lists(sb);
kfree(sbi->zeroed_page);
kfree(sbi->zero_parity);
nova_dbgmask = 0;
kfree(sbi->free_lists);
kfree(sbi->journal_locks);
for (i = 0; i < sbi->cpus; i++) {
inode_map = &sbi->inode_maps[i];
nova_dbgv("CPU %d: inode allocated %d, freed %d\n",
i, inode_map->allocated, inode_map->freed);
}
kfree(sbi->inode_maps);
nova_sysfs_exit(sb);
kfree(sbi->nova_sb);
kfree(sbi);
sb->s_fs_info = NULL;
}
void nova_free_range_node(struct nova_range_node *node)
{
kmem_cache_free(nova_range_node_cachep, node);
}
void nova_free_inode_node(struct nova_range_node *node)
{
nova_free_range_node(node);
}
void nova_free_dir_node(struct nova_range_node *node)
{
nova_free_range_node(node);
}
void nova_free_vma_item(struct super_block *sb,
struct vma_item *item)
{
nova_free_range_node((struct nova_range_node *)item);
}
struct snapshot_info *nova_alloc_snapshot_info(struct super_block *sb)
{
struct snapshot_info *p;
p = (struct snapshot_info *)