-
Notifications
You must be signed in to change notification settings - Fork 1
/
dax.c
1390 lines (1153 loc) · 35.2 KB
/
dax.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
*
* DAX file operations.
*
* 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]>
*
* 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/buffer_head.h>
#include <linux/cpufeature.h>
#include <asm/pgtable.h>
#include <linux/version.h>
#include "nova.h"
#include "inode.h"
static inline int nova_copy_partial_block(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_file_write_entry *entry, unsigned long index,
size_t offset, size_t length, void *kmem)
{
void *ptr;
int rc = 0;
unsigned long nvmm;
nvmm = get_nvmm(sb, sih, entry, index);
ptr = nova_get_block(sb, (nvmm << PAGE_SHIFT));
if (ptr != NULL) {
if (support_clwb)
rc = memcpy_mcsafe(kmem + offset, ptr + offset,
length);
else
memcpy_to_pmem_nocache(kmem + offset, ptr + offset,
length);
}
/* TODO: If rc < 0, go to MCE data recovery. */
return rc;
}
static inline int nova_handle_partial_block(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_file_write_entry *entry, unsigned long index,
size_t offset, size_t length, void *kmem)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_file_write_entry *entryc, entry_copy;
nova_memunlock_block(sb, kmem);
if (entry == NULL) {
/* Fill zero */
if (support_clwb)
memset(kmem + offset, 0, length);
else
memcpy_to_pmem_nocache(kmem + offset,
sbi->zeroed_page, length);
} else {
/* Copy from original block */
if (metadata_csum == 0)
entryc = entry;
else {
entryc = &entry_copy;
if (!nova_verify_entry_csum(sb, entry, entryc))
return -EIO;
}
nova_copy_partial_block(sb, sih, entryc, index,
offset, length, kmem);
}
nova_memlock_block(sb, kmem);
if (support_clwb)
nova_flush_buffer(kmem + offset, length, 0);
return 0;
}
/*
* Fill the new start/end block from original blocks.
* Do nothing if fully covered; copy if original blocks present;
* Fill zero otherwise.
*/
int nova_handle_head_tail_blocks(struct super_block *sb,
struct inode *inode, loff_t pos, size_t count, void *kmem)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
size_t offset, eblk_offset;
unsigned long start_blk, end_blk, num_blocks;
struct nova_file_write_entry *entry;
timing_t partial_time;
int ret = 0;
NOVA_START_TIMING(partial_block_t, partial_time);
offset = pos & (sb->s_blocksize - 1);
num_blocks = ((count + offset - 1) >> sb->s_blocksize_bits) + 1;
/* offset in the actual block size block */
offset = pos & (nova_inode_blk_size(sih) - 1);
start_blk = pos >> sb->s_blocksize_bits;
end_blk = start_blk + num_blocks - 1;
nova_dbg_verbose("%s: %lu blocks\n", __func__, num_blocks);
/* We avoid zeroing the alloc'd range, which is going to be overwritten
* by this system call anyway
*/
nova_dbg_verbose("%s: start offset %lu start blk %lu %p\n", __func__,
offset, start_blk, kmem);
if (offset != 0) {
entry = nova_get_write_entry(sb, sih, start_blk);
ret = nova_handle_partial_block(sb, sih, entry,
start_blk, 0, offset, kmem);
if (ret < 0)
return ret;
}
kmem = (void *)((char *)kmem +
((num_blocks - 1) << sb->s_blocksize_bits));
eblk_offset = (pos + count) & (nova_inode_blk_size(sih) - 1);
nova_dbg_verbose("%s: end offset %lu, end blk %lu %p\n", __func__,
eblk_offset, end_blk, kmem);
if (eblk_offset != 0) {
entry = nova_get_write_entry(sb, sih, end_blk);
ret = nova_handle_partial_block(sb, sih, entry, end_blk,
eblk_offset,
sb->s_blocksize - eblk_offset,
kmem);
if (ret < 0)
return ret;
}
NOVA_END_TIMING(partial_block_t, partial_time);
return ret;
}
int nova_reassign_file_tree(struct super_block *sb,
struct nova_inode_info_header *sih, u64 begin_tail)
{
void *addr;
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc, entry_copy;
u64 curr_p = begin_tail;
size_t entry_size = sizeof(struct nova_file_write_entry);
entryc = (metadata_csum == 0) ? entry : &entry_copy;
while (curr_p && curr_p != sih->log_tail) {
if (is_last_entry(curr_p, entry_size))
curr_p = next_log_page(sb, curr_p);
if (curr_p == 0) {
nova_err(sb, "%s: File inode %lu log is NULL!\n",
__func__, sih->ino);
return -EINVAL;
}
addr = (void *) nova_get_block(sb, curr_p);
entry = (struct nova_file_write_entry *) addr;
if (metadata_csum == 0)
entryc = entry;
else if (!nova_verify_entry_csum(sb, entry, entryc))
return -EIO;
if (nova_get_entry_type(entryc) != FILE_WRITE) {
nova_dbg("%s: entry type is not write? %d\n",
__func__, nova_get_entry_type(entry));
curr_p += entry_size;
continue;
}
nova_assign_write_entry(sb, sih, entry, entryc, true);
curr_p += entry_size;
}
return 0;
}
int nova_cleanup_incomplete_write(struct super_block *sb,
struct nova_inode_info_header *sih, unsigned long blocknr,
int allocated, u64 begin_tail, u64 end_tail)
{
void *addr;
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc, entry_copy;
u64 curr_p = begin_tail;
size_t entry_size = sizeof(struct nova_file_write_entry);
if (blocknr > 0 && allocated > 0)
nova_free_data_blocks(sb, sih, blocknr, allocated);
if (begin_tail == 0 || end_tail == 0)
return 0;
entryc = (metadata_csum == 0) ? entry : &entry_copy;
while (curr_p != end_tail) {
if (is_last_entry(curr_p, entry_size))
curr_p = next_log_page(sb, curr_p);
if (curr_p == 0) {
nova_err(sb, "%s: File inode %lu log is NULL!\n",
__func__, sih->ino);
return -EINVAL;
}
addr = (void *) nova_get_block(sb, curr_p);
entry = (struct nova_file_write_entry *) addr;
if (metadata_csum == 0)
entryc = entry;
else {
/* skip entry check here as the entry checksum may not
* be updated when this is called
*/
if (memcpy_mcsafe(entryc, entry,
sizeof(struct nova_file_write_entry)))
return -EIO;
}
if (nova_get_entry_type(entryc) != FILE_WRITE) {
nova_dbg("%s: entry type is not write? %d\n",
__func__, nova_get_entry_type(entry));
curr_p += entry_size;
continue;
}
blocknr = entryc->block >> PAGE_SHIFT;
nova_free_data_blocks(sb, sih, blocknr, entryc->num_pages);
curr_p += entry_size;
}
return 0;
}
void nova_init_file_write_entry(struct super_block *sb,
struct nova_inode_info_header *sih, struct nova_file_write_entry *entry,
u64 epoch_id, u64 pgoff, int num_pages, u64 blocknr, u32 time,
u64 file_size)
{
memset(entry, 0, sizeof(struct nova_file_write_entry));
entry->entry_type = FILE_WRITE;
entry->reassigned = 0;
entry->updating = 0;
entry->epoch_id = epoch_id;
entry->trans_id = sih->trans_id;
entry->pgoff = cpu_to_le64(pgoff);
entry->num_pages = cpu_to_le32(num_pages);
entry->invalid_pages = 0;
entry->block = cpu_to_le64(nova_get_block_off(sb, blocknr,
sih->i_blk_type));
entry->mtime = cpu_to_le32(time);
entry->size = file_size;
}
int nova_protect_file_data(struct super_block *sb, struct inode *inode,
loff_t pos, size_t count, const char __user *buf, unsigned long blocknr,
bool inplace)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
size_t offset, eblk_offset, bytes, left;
unsigned long start_blk, end_blk, num_blocks, nvmm, nvmmoff;
unsigned long blocksize = sb->s_blocksize;
unsigned int blocksize_bits = sb->s_blocksize_bits;
u8 *blockbuf, *blockptr;
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc, entry_copy;
bool mapped, nvmm_ok;
int ret = 0;
timing_t protect_file_data_time, memcpy_time;
NOVA_START_TIMING(protect_file_data_t, protect_file_data_time);
offset = pos & (blocksize - 1);
num_blocks = ((offset + count - 1) >> blocksize_bits) + 1;
start_blk = pos >> blocksize_bits;
end_blk = start_blk + num_blocks - 1;
NOVA_START_TIMING(protect_memcpy_t, memcpy_time);
blockbuf = kmalloc(blocksize, GFP_KERNEL);
if (blockbuf == NULL) {
nova_err(sb, "%s: block buffer allocation error\n", __func__);
return -ENOMEM;
}
bytes = blocksize - offset;
if (bytes > count)
bytes = count;
left = copy_from_user(blockbuf + offset, buf, bytes);
NOVA_END_TIMING(protect_memcpy_t, memcpy_time);
if (unlikely(left != 0)) {
nova_err(sb, "%s: not all data is copied from user! expect to copy %zu bytes, actually copied %zu bytes\n",
__func__, bytes, bytes - left);
ret = -EFAULT;
goto out;
}
entryc = (metadata_csum == 0) ? entry : &entry_copy;
if (offset != 0) {
NOVA_STATS_ADD(protect_head, 1);
entry = nova_get_write_entry(sb, sih, start_blk);
if (entry != NULL) {
if (metadata_csum == 0)
entryc = entry;
else if (!nova_verify_entry_csum(sb, entry, entryc))
return -EIO;
/* make sure data in the partial block head is good */
nvmm = get_nvmm(sb, sih, entryc, start_blk);
nvmmoff = nova_get_block_off(sb, nvmm, sih->i_blk_type);
blockptr = (u8 *) nova_get_block(sb, nvmmoff);
mapped = nova_find_pgoff_in_vma(inode, start_blk);
if (data_csum > 0 && !mapped && !inplace) {
nvmm_ok = nova_verify_data_csum(sb, sih, nvmm,
0, offset);
if (!nvmm_ok) {
ret = -EIO;
goto out;
}
}
ret = memcpy_mcsafe(blockbuf, blockptr, offset);
if (ret < 0)
goto out;
} else {
memset(blockbuf, 0, offset);
}
/* copying existing checksums from nvmm can be even slower than
* re-computing checksums of a whole block.
if (data_csum > 0)
nova_copy_partial_block_csum(sb, sih, entry, start_blk,
offset, blocknr, false);
*/
}
if (num_blocks == 1)
goto eblk;
do {
if (inplace)
nova_update_block_csum_parity(sb, sih, blockbuf,
blocknr, offset, bytes);
else
nova_update_block_csum_parity(sb, sih, blockbuf,
blocknr, 0, blocksize);
blocknr++;
pos += bytes;
buf += bytes;
count -= bytes;
offset = pos & (blocksize - 1);
bytes = count < blocksize ? count : blocksize;
left = copy_from_user(blockbuf, buf, bytes);
if (unlikely(left != 0)) {
nova_err(sb, "%s: not all data is copied from user! expect to copy %zu bytes, actually copied %zu bytes\n",
__func__, bytes, bytes - left);
ret = -EFAULT;
goto out;
}
} while (count > blocksize);
eblk:
eblk_offset = (pos + count) & (blocksize - 1);
if (eblk_offset != 0) {
NOVA_STATS_ADD(protect_tail, 1);
entry = nova_get_write_entry(sb, sih, end_blk);
if (entry != NULL) {
if (metadata_csum == 0)
entryc = entry;
else if (!nova_verify_entry_csum(sb, entry, entryc))
return -EIO;
/* make sure data in the partial block tail is good */
nvmm = get_nvmm(sb, sih, entryc, end_blk);
nvmmoff = nova_get_block_off(sb, nvmm, sih->i_blk_type);
blockptr = (u8 *) nova_get_block(sb, nvmmoff);
mapped = nova_find_pgoff_in_vma(inode, end_blk);
if (data_csum > 0 && !mapped && !inplace) {
nvmm_ok = nova_verify_data_csum(sb, sih, nvmm,
eblk_offset, blocksize - eblk_offset);
if (!nvmm_ok) {
ret = -EIO;
goto out;
}
}
ret = memcpy_mcsafe(blockbuf + eblk_offset,
blockptr + eblk_offset,
blocksize - eblk_offset);
if (ret < 0)
goto out;
} else {
memset(blockbuf + eblk_offset, 0,
blocksize - eblk_offset);
}
/* copying existing checksums from nvmm can be even slower than
* re-computing checksums of a whole block.
if (data_csum > 0)
nova_copy_partial_block_csum(sb, sih, entry, end_blk,
eblk_offset, blocknr, true);
*/
}
if (inplace)
nova_update_block_csum_parity(sb, sih, blockbuf, blocknr,
offset, bytes);
else
nova_update_block_csum_parity(sb, sih, blockbuf, blocknr,
0, blocksize);
out:
if (blockbuf != NULL)
kfree(blockbuf);
NOVA_END_TIMING(protect_file_data_t, protect_file_data_time);
return ret;
}
static bool nova_get_verify_entry(struct super_block *sb,
struct nova_file_write_entry *entry,
struct nova_file_write_entry *entryc,
int locked)
{
int ret = 0;
if (metadata_csum == 0)
return true;
if (locked == 0) {
/* Someone else may be updating the entry. Skip check */
ret = memcpy_mcsafe(entryc, entry,
sizeof(struct nova_file_write_entry));
if (ret < 0)
return false;
return true;
}
return nova_verify_entry_csum(sb, entry, entryc);
}
/*
* Check if there is an existing entry for target page offset.
* Used for inplace write, direct IO, DAX-mmap and fallocate.
*/
unsigned long nova_check_existing_entry(struct super_block *sb,
struct inode *inode, unsigned long num_blocks, unsigned long start_blk,
struct nova_file_write_entry **ret_entry,
struct nova_file_write_entry *ret_entryc, int check_next, u64 epoch_id,
int *inplace, int locked)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc;
unsigned long next_pgoff;
unsigned long ent_blks = 0;
timing_t check_time;
NOVA_START_TIMING(check_entry_t, check_time);
*ret_entry = NULL;
*inplace = 0;
entry = nova_get_write_entry(sb, sih, start_blk);
entryc = (metadata_csum == 0) ? entry : ret_entryc;
if (entry) {
if (metadata_csum == 0)
entryc = entry;
else if (!nova_get_verify_entry(sb, entry, entryc, locked))
goto out;
*ret_entry = entry;
/* We can do inplace write. Find contiguous blocks */
if (entryc->reassigned == 0)
ent_blks = entryc->num_pages -
(start_blk - entryc->pgoff);
else
ent_blks = 1;
if (ent_blks > num_blocks)
ent_blks = num_blocks;
if (entryc->epoch_id == epoch_id)
*inplace = 1;
} else if (check_next) {
/* Possible Hole */
entry = nova_find_next_entry(sb, sih, start_blk);
if (entry) {
if (metadata_csum == 0)
entryc = entry;
else if (!nova_get_verify_entry(sb, entry, entryc,
locked))
goto out;
next_pgoff = entryc->pgoff;
if (next_pgoff <= start_blk) {
nova_err(sb, "iblock %lu, entry pgoff %lu, num pages %lu\n",
start_blk, next_pgoff, entry->num_pages);
nova_print_inode_log(sb, inode);
BUG();
ent_blks = num_blocks;
goto out;
}
ent_blks = next_pgoff - start_blk;
if (ent_blks > num_blocks)
ent_blks = num_blocks;
} else {
/* File grow */
ent_blks = num_blocks;
}
}
if (entry && ent_blks == 0) {
nova_dbg("%s: %d\n", __func__, check_next);
dump_stack();
}
out:
NOVA_END_TIMING(check_entry_t, check_time);
return ent_blks;
}
/*
* Do an inplace write. This function assumes that the lock on the inode is
* already held.
*/
ssize_t do_nova_inplace_file_write(struct file *filp,
const char __user *buf, size_t len, loff_t *ppos)
{
struct address_space *mapping = filp->f_mapping;
struct inode *inode = mapping->host;
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct super_block *sb = inode->i_sb;
struct nova_inode *pi, inode_copy;
struct nova_file_write_entry *entry;
struct nova_file_write_entry *entryc, entry_copy;
struct nova_file_write_entry entry_data;
struct nova_inode_update update;
ssize_t written = 0;
loff_t pos;
size_t count, offset, copied;
unsigned long start_blk, num_blocks, ent_blks = 0;
unsigned long total_blocks;
unsigned long new_blocks = 0;
unsigned long blocknr = 0;
unsigned int data_bits;
int allocated = 0;
int inplace = 0;
bool hole_fill = false;
bool update_log = false;
void *kmem;
u64 blk_off;
size_t bytes;
long status = 0;
timing_t inplace_write_time, memcpy_time;
unsigned long step = 0;
u64 begin_tail = 0;
u64 epoch_id;
u64 file_size;
u32 time;
ssize_t ret;
if (len == 0)
return 0;
NOVA_START_TIMING(inplace_write_t, inplace_write_time);
if (!access_ok(VERIFY_READ, buf, len)) {
ret = -EFAULT;
goto out;
}
pos = *ppos;
if (filp->f_flags & O_APPEND)
pos = i_size_read(inode);
count = len;
pi = nova_get_block(sb, sih->pi_addr);
/* nova_inode tail pointer will be updated and we make sure all other
* inode fields are good before checksumming the whole structure
*/
if (nova_check_inode_integrity(sb, sih->ino, sih->pi_addr,
sih->alter_pi_addr, &inode_copy, 0) < 0) {
ret = -EIO;
goto out;
}
offset = pos & (sb->s_blocksize - 1);
num_blocks = ((count + offset - 1) >> sb->s_blocksize_bits) + 1;
total_blocks = num_blocks;
/* offset in the actual block size block */
ret = file_remove_privs(filp);
if (ret)
goto out;
inode->i_ctime = inode->i_mtime = current_time(inode);
time = current_time(inode).tv_sec;
epoch_id = nova_get_epoch_id(sb);
nova_dbgv("%s: epoch_id %llu, inode %lu, offset %lld, count %lu\n",
__func__, epoch_id, inode->i_ino, pos, count);
update.tail = sih->log_tail;
update.alter_tail = sih->alter_log_tail;
while (num_blocks > 0) {
hole_fill = false;
offset = pos & (nova_inode_blk_size(sih) - 1);
start_blk = pos >> sb->s_blocksize_bits;
ent_blks = nova_check_existing_entry(sb, inode, num_blocks,
start_blk, &entry, &entry_copy,
1, epoch_id, &inplace, 1);
entryc = (metadata_csum == 0) ? entry : &entry_copy;
if (entry && inplace) {
/* We can do inplace write. Find contiguous blocks */
blocknr = get_nvmm(sb, sih, entryc, start_blk);
blk_off = blocknr << PAGE_SHIFT;
allocated = ent_blks;
if (data_csum || data_parity)
nova_set_write_entry_updating(sb, entry, 1);
} else {
/* Allocate blocks to fill hole */
allocated = nova_new_data_blocks(sb, sih, &blocknr,
start_blk, ent_blks, ALLOC_NO_INIT,
ANY_CPU, ALLOC_FROM_HEAD);
nova_dbg_verbose("%s: alloc %d blocks @ %lu\n",
__func__, allocated, blocknr);
if (allocated <= 0) {
nova_dbg("%s alloc blocks failed!, %d\n",
__func__, allocated);
ret = allocated;
goto out;
}
hole_fill = true;
new_blocks += allocated;
blk_off = nova_get_block_off(sb, blocknr,
sih->i_blk_type);
}
step++;
bytes = sb->s_blocksize * allocated - offset;
if (bytes > count)
bytes = count;
kmem = nova_get_block(inode->i_sb, blk_off);
if (hole_fill &&
(offset || ((offset + bytes) & (PAGE_SIZE - 1)) != 0)) {
ret = nova_handle_head_tail_blocks(sb, inode,
pos, bytes, kmem);
if (ret)
goto out;
}
/* Now copy from user buf */
// nova_dbg("Write: %p\n", kmem);
NOVA_START_TIMING(memcpy_w_nvmm_t, memcpy_time);
nova_memunlock_range(sb, kmem + offset, bytes);
copied = bytes - memcpy_to_pmem_nocache(kmem + offset,
buf, bytes);
nova_memlock_range(sb, kmem + offset, bytes);
NOVA_END_TIMING(memcpy_w_nvmm_t, memcpy_time);
if (data_csum > 0 || data_parity > 0) {
ret = nova_protect_file_data(sb, inode, pos, bytes,
buf, blocknr, !hole_fill);
if (ret)
goto out;
}
if (pos + copied > inode->i_size)
file_size = cpu_to_le64(pos + copied);
else
file_size = cpu_to_le64(inode->i_size);
/* Handle hole fill write */
if (hole_fill) {
nova_init_file_write_entry(sb, sih, &entry_data,
epoch_id, start_blk, allocated,
blocknr, time, file_size);
ret = nova_append_file_write_entry(sb, pi, inode,
&entry_data, &update);
if (ret) {
nova_dbg("%s: append inode entry failed\n",
__func__);
ret = -ENOSPC;
goto out;
}
} else {
/* Update existing entry */
struct nova_log_entry_info entry_info;
entry_info.type = FILE_WRITE;
entry_info.epoch_id = epoch_id;
entry_info.trans_id = sih->trans_id;
entry_info.time = time;
entry_info.file_size = file_size;
entry_info.inplace = 1;
nova_inplace_update_write_entry(sb, inode, entry,
&entry_info);
}
nova_dbgv("Write: %p, %lu\n", kmem, copied);
if (copied > 0) {
status = copied;
written += copied;
pos += copied;
buf += copied;
count -= copied;
num_blocks -= allocated;
}
if (unlikely(copied != bytes)) {
nova_dbg("%s ERROR!: %p, bytes %lu, copied %lu\n",
__func__, kmem, bytes, copied);
if (status >= 0)
status = -EFAULT;
}
if (status < 0)
break;
if (hole_fill) {
update_log = true;
if (begin_tail == 0)
begin_tail = update.curr_entry;
}
}
data_bits = blk_type_to_shift[sih->i_blk_type];
sih->i_blocks += (new_blocks << (data_bits - sb->s_blocksize_bits));
inode->i_blocks = sih->i_blocks;
if (update_log) {
nova_memunlock_inode(sb, pi);
nova_update_inode(sb, inode, pi, &update, 1);
nova_memlock_inode(sb, pi);
NOVA_STATS_ADD(inplace_new_blocks, 1);
/* Update file tree */
ret = nova_reassign_file_tree(sb, sih, begin_tail);
if (ret)
goto out;
}
ret = written;
NOVA_STATS_ADD(inplace_write_breaks, step);
nova_dbgv("blocks: %lu, %lu\n", inode->i_blocks, sih->i_blocks);
*ppos = pos;
if (pos > inode->i_size) {
i_size_write(inode, pos);
sih->i_size = pos;
}
sih->trans_id++;
out:
if (ret < 0)
nova_cleanup_incomplete_write(sb, sih, blocknr, allocated,
begin_tail, update.tail);
NOVA_END_TIMING(inplace_write_t, inplace_write_time);
NOVA_STATS_ADD(inplace_write_bytes, written);
return ret;
}
/*
* Acquire locks and perform an inplace update.
*/
ssize_t nova_inplace_file_write(struct file *filp,
const char __user *buf, size_t len, loff_t *ppos)
{
struct address_space *mapping = filp->f_mapping;
struct inode *inode = mapping->host;
int ret;
if (len == 0)
return 0;
sb_start_write(inode->i_sb);
inode_lock(inode);
ret = do_nova_inplace_file_write(filp, buf, len, ppos);
inode_unlock(inode);
sb_end_write(inode->i_sb);
return ret;
}
/* Check if existing entry overlap with vma regions */
int nova_check_overlap_vmas(struct super_block *sb,
struct nova_inode_info_header *sih,
unsigned long pgoff, unsigned long num_pages)
{
unsigned long start_pgoff = 0;
unsigned long num = 0;
unsigned long i;
struct vma_item *item;
struct rb_node *temp;
int ret = 0;
if (sih->num_vmas == 0)
return 0;
temp = rb_first(&sih->vma_tree);
while (temp) {
item = container_of(temp, struct vma_item, node);
temp = rb_next(temp);
ret = nova_get_vma_overlap_range(sb, sih, item->vma, pgoff,
num_pages, &start_pgoff, &num);
if (ret) {
for (i = 0; i < num; i++) {
if (nova_get_write_entry(sb, sih,
start_pgoff + i))
return 1;
}
}
}
return 0;
}
/*
* return > 0, # of blocks mapped or allocated.
* return = 0, if plain lookup failed.
* return < 0, error case.
*/
static int nova_dax_get_blocks(struct inode *inode, sector_t iblock,
unsigned long max_blocks, u32 *bno, bool *new, bool *boundary,
int create, bool taking_lock)
{
struct super_block *sb = inode->i_sb;
struct nova_inode *pi;
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_file_write_entry *entry = NULL;
struct nova_file_write_entry *entryc, entry_copy;
struct nova_file_write_entry entry_data;
struct nova_inode_update update;
u32 time;
unsigned int data_bits;
unsigned long nvmm = 0;
unsigned long blocknr = 0;
u64 epoch_id;
int num_blocks = 0;
int inplace = 0;
int allocated = 0;
int locked = 0;
int check_next = 1;
int ret = 0;
timing_t get_block_time;
if (max_blocks == 0)
return 0;
NOVA_START_TIMING(dax_get_block_t, get_block_time);
nova_dbgv("%s: pgoff %lu, num %lu, create %d\n",
__func__, iblock, max_blocks, create);
epoch_id = nova_get_epoch_id(sb);
if (taking_lock)
check_next = 0;
again:
num_blocks = nova_check_existing_entry(sb, inode, max_blocks,
iblock, &entry, &entry_copy, check_next,
epoch_id, &inplace, locked);
entryc = (metadata_csum == 0) ? entry : &entry_copy;
if (entry) {
if (create == 0 || inplace) {
nvmm = get_nvmm(sb, sih, entryc, iblock);
nova_dbgv("%s: found pgoff %lu, block %lu\n",
__func__, iblock, nvmm);
goto out;
}
}
if (create == 0) {
num_blocks = 0;
goto out1;
}
if (taking_lock && locked == 0) {
inode_lock(inode);
locked = 1;
/* Check again incase someone has done it for us */
check_next = 1;
goto again;
}
pi = nova_get_inode(sb, inode);
inode->i_ctime = inode->i_mtime = current_time(inode);
time = current_time(inode).tv_sec;
update.tail = sih->log_tail;
update.alter_tail = sih->alter_log_tail;
/* Return initialized blocks to the user */
allocated = nova_new_data_blocks(sb, sih, &blocknr, iblock,
num_blocks, ALLOC_INIT_ZERO, ANY_CPU,
ALLOC_FROM_HEAD);
if (allocated <= 0) {
nova_dbgv("%s alloc blocks failed %d\n", __func__,
allocated);
ret = allocated;
goto out;
}
num_blocks = allocated;
/* Do not extend file size */
nova_init_file_write_entry(sb, sih, &entry_data,
epoch_id, iblock, num_blocks,
blocknr, time, inode->i_size);
ret = nova_append_file_write_entry(sb, pi, inode,
&entry_data, &update);
if (ret) {
nova_dbgv("%s: append inode entry failed\n", __func__);
ret = -ENOSPC;
goto out;
}
nvmm = blocknr;
data_bits = blk_type_to_shift[sih->i_blk_type];
sih->i_blocks += (num_blocks << (data_bits - sb->s_blocksize_bits));
nova_memunlock_inode(sb, pi);
nova_update_inode(sb, inode, pi, &update, 1);
nova_memlock_inode(sb, pi);
ret = nova_reassign_file_tree(sb, sih, update.curr_entry);
if (ret) {
nova_dbgv("%s: nova_reassign_file_tree failed: %d\n",
__func__, ret);
goto out;
}
inode->i_blocks = sih->i_blocks;
sih->trans_id++;
NOVA_STATS_ADD(dax_new_blocks, 1);
// set_buffer_new(bh);
out:
if (ret < 0) {
nova_cleanup_incomplete_write(sb, sih, blocknr, allocated,
0, update.tail);
num_blocks = ret;
goto out1;
}
*bno = nvmm;
// if (num_blocks > 1)
// bh->b_size = sb->s_blocksize * num_blocks;
out1:
if (taking_lock && locked)