-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.c
1425 lines (1205 loc) · 37.4 KB
/
log.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
*
* Log methods
*
* 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 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 "journal.h"
#include "inode.h"
#include "log.h"
static int nova_execute_invalidate_reassign_logentry(struct super_block *sb,
void *entry, enum nova_entry_type type, int reassign,
unsigned int num_free)
{
struct nova_file_write_entry *fw_entry;
int invalid = 0;
switch (type) {
case FILE_WRITE:
fw_entry = (struct nova_file_write_entry *)entry;
if (reassign)
fw_entry->reassigned = 1;
if (num_free)
fw_entry->invalid_pages += num_free;
if (fw_entry->invalid_pages == fw_entry->num_pages)
invalid = 1;
break;
case DIR_LOG:
if (reassign) {
((struct nova_dentry *)entry)->reassigned = 1;
} else {
((struct nova_dentry *)entry)->invalid = 1;
invalid = 1;
}
break;
case SET_ATTR:
((struct nova_setattr_logentry *)entry)->invalid = 1;
invalid = 1;
break;
case LINK_CHANGE:
((struct nova_link_change_entry *)entry)->invalid = 1;
invalid = 1;
break;
case MMAP_WRITE:
((struct nova_mmap_entry *)entry)->invalid = 1;
invalid = 1;
break;
case SNAPSHOT_INFO:
((struct nova_snapshot_info_entry *)entry)->deleted = 1;
invalid = 1;
break;
default:
break;
}
if (invalid) {
u64 addr = nova_get_addr_off(NOVA_SB(sb), entry);
nova_inc_page_invalid_entries(sb, addr);
}
nova_update_entry_csum(entry);
return 0;
}
static int nova_invalidate_reassign_logentry(struct super_block *sb,
void *entry, enum nova_entry_type type, int reassign,
unsigned int num_free)
{
nova_memunlock_range(sb, entry, CACHELINE_SIZE);
nova_execute_invalidate_reassign_logentry(sb, entry, type,
reassign, num_free);
nova_update_alter_entry(sb, entry);
nova_memlock_range(sb, entry, CACHELINE_SIZE);
return 0;
}
int nova_invalidate_logentry(struct super_block *sb, void *entry,
enum nova_entry_type type, unsigned int num_free)
{
return nova_invalidate_reassign_logentry(sb, entry, type, 0, num_free);
}
int nova_reassign_logentry(struct super_block *sb, void *entry,
enum nova_entry_type type)
{
return nova_invalidate_reassign_logentry(sb, entry, type, 1, 0);
}
static inline int nova_invalidate_write_entry(struct super_block *sb,
struct nova_file_write_entry *entry, int reassign,
unsigned int num_free)
{
struct nova_file_write_entry *entryc, entry_copy;
if (!entry)
return 0;
if (metadata_csum == 0)
entryc = entry;
else {
entryc = &entry_copy;
if (!nova_verify_entry_csum(sb, entry, entryc))
return -EIO;
}
if (num_free == 0 && entryc->reassigned == 1)
return 0;
return nova_invalidate_reassign_logentry(sb, entry, FILE_WRITE,
reassign, num_free);
}
unsigned int nova_free_old_entry(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_file_write_entry *entry,
unsigned long pgoff, unsigned int num_free,
bool delete_dead, u64 epoch_id)
{
struct nova_file_write_entry *entryc, entry_copy;
unsigned long old_nvmm;
int ret;
timing_t free_time;
if (!entry)
return 0;
NOVA_START_TIMING(free_old_t, free_time);
if (metadata_csum == 0)
entryc = entry;
else {
entryc = &entry_copy;
if (!nova_verify_entry_csum(sb, entry, entryc))
return -EIO;
}
old_nvmm = get_nvmm(sb, sih, entryc, pgoff);
if (!delete_dead) {
ret = nova_append_data_to_snapshot(sb, entryc, old_nvmm,
num_free, epoch_id);
if (ret == 0) {
nova_invalidate_write_entry(sb, entry, 1, 0);
goto out;
}
nova_invalidate_write_entry(sb, entry, 1, num_free);
}
nova_dbgv("%s: pgoff %lu, free %u blocks\n",
__func__, pgoff, num_free);
nova_free_data_blocks(sb, sih, old_nvmm, num_free);
out:
sih->i_blocks -= num_free;
NOVA_END_TIMING(free_old_t, free_time);
return num_free;
}
struct nova_file_write_entry *nova_find_next_entry(struct super_block *sb,
struct nova_inode_info_header *sih, pgoff_t pgoff)
{
struct nova_file_write_entry *entry = NULL;
struct nova_file_write_entry *entries[1];
int nr_entries;
nr_entries = radix_tree_gang_lookup(&sih->tree,
(void **)entries, pgoff, 1);
if (nr_entries == 1)
entry = entries[0];
return entry;
}
/*
* Zero the tail page. Used in resize request
* to avoid to keep data in case the file grows again.
*/
void nova_clear_last_page_tail(struct super_block *sb,
struct inode *inode, loff_t newsize)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
unsigned long offset = newsize & (sb->s_blocksize - 1);
unsigned long pgoff, length;
u64 nvmm;
char *nvmm_addr;
if (offset == 0 || newsize > inode->i_size)
return;
length = sb->s_blocksize - offset;
pgoff = newsize >> sb->s_blocksize_bits;
nvmm = nova_find_nvmm_block(sb, sih, NULL, pgoff);
if (nvmm == 0)
return;
nvmm_addr = (char *)nova_get_block(sb, nvmm);
nova_memunlock_range(sb, nvmm_addr + offset, length);
memcpy_to_pmem_nocache(nvmm_addr + offset, sbi->zeroed_page, length);
nova_memlock_range(sb, nvmm_addr + offset, length);
if (data_csum > 0)
nova_update_truncated_block_csum(sb, inode, newsize);
if (data_parity > 0)
nova_update_truncated_block_parity(sb, inode, newsize);
}
static void nova_update_setattr_entry(struct inode *inode,
struct nova_setattr_logentry *entry,
struct nova_log_entry_info *entry_info)
{
struct iattr *attr = entry_info->attr;
unsigned int ia_valid = attr->ia_valid, attr_mask;
/* These files are in the lowest byte */
attr_mask = ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_SIZE |
ATTR_ATIME | ATTR_MTIME | ATTR_CTIME;
entry->entry_type = SET_ATTR;
entry->attr = ia_valid & attr_mask;
entry->mode = cpu_to_le16(inode->i_mode);
entry->uid = cpu_to_le32(i_uid_read(inode));
entry->gid = cpu_to_le32(i_gid_read(inode));
entry->atime = cpu_to_le32(inode->i_atime.tv_sec);
entry->ctime = cpu_to_le32(inode->i_ctime.tv_sec);
entry->mtime = cpu_to_le32(inode->i_mtime.tv_sec);
entry->epoch_id = cpu_to_le64(entry_info->epoch_id);
entry->trans_id = cpu_to_le64(entry_info->trans_id);
entry->invalid = 0;
if (ia_valid & ATTR_SIZE)
entry->size = cpu_to_le64(attr->ia_size);
else
entry->size = cpu_to_le64(inode->i_size);
nova_update_entry_csum(entry);
}
static void nova_update_link_change_entry(struct inode *inode,
struct nova_link_change_entry *entry,
struct nova_log_entry_info *entry_info)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
entry->entry_type = LINK_CHANGE;
entry->epoch_id = cpu_to_le64(entry_info->epoch_id);
entry->trans_id = cpu_to_le64(entry_info->trans_id);
entry->invalid = 0;
entry->links = cpu_to_le16(inode->i_nlink);
entry->ctime = cpu_to_le32(inode->i_ctime.tv_sec);
entry->flags = cpu_to_le32(sih->i_flags);
entry->generation = cpu_to_le32(inode->i_generation);
nova_update_entry_csum(entry);
}
static int nova_update_write_entry(struct super_block *sb,
struct nova_file_write_entry *entry,
struct nova_log_entry_info *entry_info)
{
entry->epoch_id = cpu_to_le64(entry_info->epoch_id);
entry->trans_id = cpu_to_le64(entry_info->trans_id);
entry->mtime = cpu_to_le32(entry_info->time);
entry->size = cpu_to_le64(entry_info->file_size);
entry->updating = 0;
nova_update_entry_csum(entry);
return 0;
}
static int nova_update_old_dentry(struct super_block *sb,
struct inode *dir, struct nova_dentry *dentry,
struct nova_log_entry_info *entry_info)
{
unsigned short links_count;
int link_change = entry_info->link_change;
u64 addr;
dentry->epoch_id = entry_info->epoch_id;
dentry->trans_id = entry_info->trans_id;
/* Remove_dentry */
dentry->ino = cpu_to_le64(0);
dentry->invalid = 1;
dentry->mtime = cpu_to_le32(dir->i_mtime.tv_sec);
links_count = cpu_to_le16(dir->i_nlink);
if (links_count == 0 && link_change == -1)
links_count = 0;
else
links_count += link_change;
dentry->links_count = cpu_to_le16(links_count);
addr = nova_get_addr_off(NOVA_SB(sb), dentry);
nova_inc_page_invalid_entries(sb, addr);
/* Update checksum */
nova_update_entry_csum(dentry);
return 0;
}
static int nova_update_new_dentry(struct super_block *sb,
struct inode *dir, struct nova_dentry *entry,
struct nova_log_entry_info *entry_info)
{
struct dentry *dentry = entry_info->data;
unsigned short links_count;
int link_change = entry_info->link_change;
entry->entry_type = DIR_LOG;
entry->epoch_id = entry_info->epoch_id;
entry->trans_id = entry_info->trans_id;
entry->ino = entry_info->ino;
entry->name_len = dentry->d_name.len;
memcpy_to_pmem_nocache(entry->name, dentry->d_name.name,
dentry->d_name.len);
entry->name[dentry->d_name.len] = '\0';
entry->mtime = cpu_to_le32(dir->i_mtime.tv_sec);
//entry->size = cpu_to_le64(dir->i_size);
links_count = cpu_to_le16(dir->i_nlink);
if (links_count == 0 && link_change == -1)
links_count = 0;
else
links_count += link_change;
entry->links_count = cpu_to_le16(links_count);
/* Update actual de_len */
entry->de_len = cpu_to_le16(entry_info->file_size);
/* Update checksum */
nova_update_entry_csum(entry);
return 0;
}
static int nova_update_log_entry(struct super_block *sb, struct inode *inode,
void *entry, struct nova_log_entry_info *entry_info)
{
enum nova_entry_type type = entry_info->type;
switch (type) {
case FILE_WRITE:
if (entry_info->inplace)
nova_update_write_entry(sb, entry, entry_info);
else
memcpy_to_pmem_nocache(entry, entry_info->data,
sizeof(struct nova_file_write_entry));
break;
case DIR_LOG:
if (entry_info->inplace)
nova_update_old_dentry(sb, inode, entry, entry_info);
else
nova_update_new_dentry(sb, inode, entry, entry_info);
break;
case SET_ATTR:
nova_update_setattr_entry(inode, entry, entry_info);
break;
case LINK_CHANGE:
nova_update_link_change_entry(inode, entry, entry_info);
break;
case MMAP_WRITE:
memcpy_to_pmem_nocache(entry, entry_info->data,
sizeof(struct nova_mmap_entry));
break;
case SNAPSHOT_INFO:
memcpy_to_pmem_nocache(entry, entry_info->data,
sizeof(struct nova_snapshot_info_entry));
break;
default:
break;
}
return 0;
}
static int nova_append_log_entry(struct super_block *sb,
struct nova_inode *pi, struct inode *inode,
struct nova_inode_info_header *sih,
struct nova_log_entry_info *entry_info)
{
void *entry, *alter_entry;
enum nova_entry_type type = entry_info->type;
struct nova_inode_update *update = entry_info->update;
u64 tail, alter_tail;
u64 curr_p, alter_curr_p;
size_t size;
int extended = 0;
if (type == DIR_LOG)
size = entry_info->file_size;
else
size = nova_get_log_entry_size(sb, type);
tail = update->tail;
alter_tail = update->alter_tail;
curr_p = nova_get_append_head(sb, pi, sih, tail, size,
MAIN_LOG, 0, &extended);
if (curr_p == 0)
return -ENOSPC;
nova_dbg_verbose("%s: inode %lu attr change entry @ 0x%llx\n",
__func__, sih->ino, curr_p);
entry = nova_get_block(sb, curr_p);
/* inode is already updated with attr */
nova_memunlock_range(sb, entry, size);
memset(entry, 0, size);
nova_update_log_entry(sb, inode, entry, entry_info);
nova_inc_page_num_entries(sb, curr_p);
nova_memlock_range(sb, entry, size);
update->curr_entry = curr_p;
update->tail = curr_p + size;
if (metadata_csum) {
alter_curr_p = nova_get_append_head(sb, pi, sih, alter_tail,
size, ALTER_LOG, 0, &extended);
if (alter_curr_p == 0)
return -ENOSPC;
alter_entry = nova_get_block(sb, alter_curr_p);
nova_memunlock_range(sb, alter_entry, size);
memset(alter_entry, 0, size);
nova_update_log_entry(sb, inode, alter_entry, entry_info);
nova_memlock_range(sb, alter_entry, size);
update->alter_entry = alter_curr_p;
update->alter_tail = alter_curr_p + size;
}
entry_info->curr_p = curr_p;
return 0;
}
int nova_inplace_update_log_entry(struct super_block *sb,
struct inode *inode, void *entry,
struct nova_log_entry_info *entry_info)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
enum nova_entry_type type = entry_info->type;
u64 journal_tail;
size_t size;
int cpu;
timing_t update_time;
NOVA_START_TIMING(update_entry_t, update_time);
size = nova_get_log_entry_size(sb, type);
if (metadata_csum) {
nova_memunlock_range(sb, entry, size);
nova_update_log_entry(sb, inode, entry, entry_info);
// Also update the alter inode log entry.
nova_update_alter_entry(sb, entry);
nova_memlock_range(sb, entry, size);
goto out;
}
cpu = nova_get_cpuid(sb);
spin_lock(&sbi->journal_locks[cpu]);
nova_memunlock_journal(sb);
journal_tail = nova_create_logentry_transaction(sb, entry, type, cpu);
nova_update_log_entry(sb, inode, entry, entry_info);
PERSISTENT_BARRIER();
nova_commit_lite_transaction(sb, journal_tail, cpu);
nova_memlock_journal(sb);
spin_unlock(&sbi->journal_locks[cpu]);
out:
NOVA_END_TIMING(update_entry_t, update_time);
return 0;
}
/* Returns new tail after append */
static int nova_append_setattr_entry(struct super_block *sb,
struct nova_inode *pi, struct inode *inode, struct iattr *attr,
struct nova_inode_update *update, u64 *last_setattr, u64 epoch_id)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_inode inode_copy;
struct nova_log_entry_info entry_info;
timing_t append_time;
int ret;
NOVA_START_TIMING(append_setattr_t, append_time);
entry_info.type = SET_ATTR;
entry_info.attr = attr;
entry_info.update = update;
entry_info.epoch_id = epoch_id;
entry_info.trans_id = sih->trans_id;
if (nova_check_inode_integrity(sb, sih->ino, sih->pi_addr,
sih->alter_pi_addr, &inode_copy, 0) < 0) {
ret = -EIO;
goto out;
}
ret = nova_append_log_entry(sb, pi, inode, sih, &entry_info);
if (ret) {
nova_err(sb, "%s failed\n", __func__);
goto out;
}
*last_setattr = sih->last_setattr;
sih->last_setattr = entry_info.curr_p;
out:
NOVA_END_TIMING(append_setattr_t, append_time);
return ret;
}
/* Invalidate old link change entry */
static int nova_invalidate_setattr_entry(struct super_block *sb,
u64 last_setattr)
{
struct nova_setattr_logentry *old_entry;
struct nova_setattr_logentry *old_entryc, old_entry_copy;
void *addr;
int ret;
addr = (void *)nova_get_block(sb, last_setattr);
old_entry = (struct nova_setattr_logentry *)addr;
if (metadata_csum == 0)
old_entryc = old_entry;
else {
old_entryc = &old_entry_copy;
if (!nova_verify_entry_csum(sb, old_entry, old_entryc))
return -EIO;
}
/* Do not invalidate setsize entries */
if (!old_entry_freeable(sb, old_entryc->epoch_id) ||
(old_entryc->attr & ATTR_SIZE))
return 0;
ret = nova_invalidate_logentry(sb, old_entry, SET_ATTR, 0);
return ret;
}
#if 0
static void setattr_copy_to_nova_inode(struct super_block *sb,
struct inode *inode, struct nova_inode *pi, u64 epoch_id)
{
pi->i_mode = cpu_to_le16(inode->i_mode);
pi->i_uid = cpu_to_le32(i_uid_read(inode));
pi->i_gid = cpu_to_le32(i_gid_read(inode));
pi->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
pi->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
pi->create_epoch_id = epoch_id;
nova_update_alter_inode(sb, inode, pi);
}
#endif
static int nova_can_inplace_update_setattr(struct super_block *sb,
struct nova_inode_info_header *sih, u64 epoch_id)
{
u64 last_log = 0;
struct nova_setattr_logentry *entry = NULL;
last_log = sih->last_setattr;
if (last_log) {
entry = (struct nova_setattr_logentry *)nova_get_block(sb,
last_log);
/* Do not overwrite setsize entry */
if (entry->attr & ATTR_SIZE)
return 0;
if (entry->epoch_id == epoch_id)
return 1;
}
return 0;
}
static int nova_inplace_update_setattr_entry(struct super_block *sb,
struct inode *inode, struct nova_inode_info_header *sih,
struct iattr *attr, u64 epoch_id)
{
struct nova_setattr_logentry *entry = NULL;
struct nova_log_entry_info entry_info;
u64 last_log = 0;
nova_dbgv("%s : Modifying last log entry for inode %lu\n",
__func__, inode->i_ino);
last_log = sih->last_setattr;
entry = (struct nova_setattr_logentry *)nova_get_block(sb,
last_log);
entry_info.type = SET_ATTR;
entry_info.attr = attr;
entry_info.epoch_id = epoch_id;
entry_info.trans_id = sih->trans_id;
return nova_inplace_update_log_entry(sb, inode, entry,
&entry_info);
}
int nova_handle_setattr_operation(struct super_block *sb, struct inode *inode,
struct nova_inode *pi, unsigned int ia_valid, struct iattr *attr,
u64 epoch_id)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_inode_update update;
u64 last_setattr = 0;
int ret;
if (ia_valid & ATTR_MODE)
sih->i_mode = inode->i_mode;
/*
* Let's try to do inplace update.
* If there are currently no snapshots holding this inode,
* we can update the inode in place. If a snapshot creation
* is in progress, we will use the create_snapshot_epoch_id
* as the latest snapshot id.
*/
if (!(ia_valid & ATTR_SIZE) &&
nova_can_inplace_update_setattr(sb, sih, epoch_id)) {
nova_inplace_update_setattr_entry(sb, inode, sih,
attr, epoch_id);
} else {
/* We are holding inode lock so OK to append the log */
nova_dbgv("%s : Appending last log entry for inode ino = %lu\n",
__func__, inode->i_ino);
update.tail = update.alter_tail = 0;
ret = nova_append_setattr_entry(sb, pi, inode, attr, &update,
&last_setattr, epoch_id);
if (ret) {
nova_dbg("%s: append setattr entry failure\n",
__func__);
return ret;
}
nova_memunlock_inode(sb, pi);
nova_update_inode(sb, inode, pi, &update, 1);
nova_memlock_inode(sb, pi);
}
/* Invalidate old setattr entry */
if (last_setattr)
nova_invalidate_setattr_entry(sb, last_setattr);
return 0;
}
/* Invalidate old link change entry */
int nova_invalidate_link_change_entry(struct super_block *sb,
u64 old_link_change)
{
struct nova_link_change_entry *old_entry;
struct nova_link_change_entry *old_entryc, old_entry_copy;
void *addr;
int ret;
if (old_link_change == 0)
return 0;
addr = (void *)nova_get_block(sb, old_link_change);
old_entry = (struct nova_link_change_entry *)addr;
if (metadata_csum == 0)
old_entryc = old_entry;
else {
old_entryc = &old_entry_copy;
if (!nova_verify_entry_csum(sb, old_entry, old_entryc))
return -EIO;
}
if (!old_entry_freeable(sb, old_entryc->epoch_id))
return 0;
ret = nova_invalidate_logentry(sb, old_entry, LINK_CHANGE, 0);
return ret;
}
static int nova_can_inplace_update_lcentry(struct super_block *sb,
struct nova_inode_info_header *sih, u64 epoch_id)
{
u64 last_log = 0;
struct nova_link_change_entry *entry = NULL;
last_log = sih->last_link_change;
if (last_log) {
entry = (struct nova_link_change_entry *)nova_get_block(sb,
last_log);
if (entry->epoch_id == epoch_id)
return 1;
}
return 0;
}
static int nova_inplace_update_lcentry(struct super_block *sb,
struct inode *inode, struct nova_inode_info_header *sih,
u64 epoch_id)
{
struct nova_link_change_entry *entry = NULL;
struct nova_log_entry_info entry_info;
u64 last_log = 0;
last_log = sih->last_link_change;
entry = (struct nova_link_change_entry *)nova_get_block(sb,
last_log);
entry_info.type = LINK_CHANGE;
entry_info.epoch_id = epoch_id;
entry_info.trans_id = sih->trans_id;
return nova_inplace_update_log_entry(sb, inode, entry,
&entry_info);
}
/* Returns new tail after append */
int nova_append_link_change_entry(struct super_block *sb,
struct nova_inode *pi, struct inode *inode,
struct nova_inode_update *update, u64 *old_linkc, u64 epoch_id)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_inode inode_copy;
struct nova_log_entry_info entry_info;
int ret = 0;
timing_t append_time;
NOVA_START_TIMING(append_link_change_t, append_time);
if (nova_check_inode_integrity(sb, sih->ino, sih->pi_addr,
sih->alter_pi_addr, &inode_copy, 0) < 0) {
ret = -EIO;
goto out;
}
if (nova_can_inplace_update_lcentry(sb, sih, epoch_id)) {
nova_inplace_update_lcentry(sb, inode, sih, epoch_id);
update->tail = sih->log_tail;
update->alter_tail = sih->alter_log_tail;
*old_linkc = 0;
sih->trans_id++;
goto out;
}
entry_info.type = LINK_CHANGE;
entry_info.update = update;
entry_info.epoch_id = epoch_id;
entry_info.trans_id = sih->trans_id;
ret = nova_append_log_entry(sb, pi, inode, sih, &entry_info);
if (ret) {
nova_err(sb, "%s failed\n", __func__);
goto out;
}
*old_linkc = sih->last_link_change;
sih->last_link_change = entry_info.curr_p;
sih->trans_id++;
out:
NOVA_END_TIMING(append_link_change_t, append_time);
return ret;
}
int nova_assign_write_entry(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_file_write_entry *entry,
struct nova_file_write_entry *entryc,
bool free)
{
struct nova_file_write_entry *old_entry;
struct nova_file_write_entry *start_old_entry = NULL;
void **pentry;
unsigned long start_pgoff = entryc->pgoff;
unsigned long start_old_pgoff = 0;
unsigned int num = entryc->num_pages;
unsigned int num_free = 0;
unsigned long curr_pgoff;
int i;
int ret = 0;
timing_t assign_time;
NOVA_START_TIMING(assign_t, assign_time);
for (i = 0; i < num; i++) {
curr_pgoff = start_pgoff + i;
pentry = radix_tree_lookup_slot(&sih->tree, curr_pgoff);
if (pentry) {
old_entry = radix_tree_deref_slot(pentry);
if (old_entry != start_old_entry) {
if (start_old_entry && free)
nova_free_old_entry(sb, sih,
start_old_entry,
start_old_pgoff,
num_free, false,
entryc->epoch_id);
nova_invalidate_write_entry(sb,
start_old_entry, 1, 0);
start_old_entry = old_entry;
start_old_pgoff = curr_pgoff;
num_free = 1;
} else {
num_free++;
}
radix_tree_replace_slot(&sih->tree, pentry, entry);
} else {
ret = radix_tree_insert(&sih->tree, curr_pgoff, entry);
if (ret) {
nova_dbg("%s: ERROR %d\n", __func__, ret);
goto out;
}
}
}
if (start_old_entry && free)
nova_free_old_entry(sb, sih, start_old_entry,
start_old_pgoff, num_free, false,
entryc->epoch_id);
nova_invalidate_write_entry(sb, start_old_entry, 1, 0);
out:
NOVA_END_TIMING(assign_t, assign_time);
return ret;
}
int nova_inplace_update_write_entry(struct super_block *sb,
struct inode *inode, struct nova_file_write_entry *entry,
struct nova_log_entry_info *entry_info)
{
return nova_inplace_update_log_entry(sb, inode, entry,
entry_info);
}
int nova_set_write_entry_updating(struct super_block *sb,
struct nova_file_write_entry *entry, int set)
{
nova_memunlock_range(sb, entry, sizeof(*entry));
entry->updating = set ? 1 : 0;
nova_update_entry_csum(entry);
nova_update_alter_entry(sb, entry);
nova_memlock_range(sb, entry, sizeof(*entry));
return 0;
}
/*
* Append a nova_file_write_entry to the current nova_inode_log_page.
* blocknr and start_blk are pgoff.
* We cannot update pi->log_tail here because a transaction may contain
* multiple entries.
*/
int nova_append_file_write_entry(struct super_block *sb, struct nova_inode *pi,
struct inode *inode, struct nova_file_write_entry *data,
struct nova_inode_update *update)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_log_entry_info entry_info;
timing_t append_time;
int ret;
NOVA_START_TIMING(append_file_entry_t, append_time);
nova_update_entry_csum(data);
entry_info.type = FILE_WRITE;
entry_info.update = update;
entry_info.data = data;
entry_info.epoch_id = data->epoch_id;
entry_info.trans_id = data->trans_id;
entry_info.inplace = 0;
ret = nova_append_log_entry(sb, pi, inode, sih, &entry_info);
if (ret)
nova_err(sb, "%s failed\n", __func__);
NOVA_END_TIMING(append_file_entry_t, append_time);
return ret;
}
int nova_append_mmap_entry(struct super_block *sb, struct nova_inode *pi,
struct inode *inode, struct nova_mmap_entry *data,
struct nova_inode_update *update, struct vma_item *item)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_inode inode_copy;
struct nova_log_entry_info entry_info;
timing_t append_time;
int ret;
NOVA_START_TIMING(append_mmap_entry_t, append_time);
nova_update_entry_csum(data);
entry_info.type = MMAP_WRITE;
entry_info.update = update;
entry_info.data = data;
entry_info.epoch_id = data->epoch_id;
if (nova_check_inode_integrity(sb, sih->ino, sih->pi_addr,
sih->alter_pi_addr, &inode_copy, 0) < 0) {
ret = -EIO;
goto out;
}
ret = nova_append_log_entry(sb, pi, inode, sih, &entry_info);
if (ret)
nova_err(sb, "%s failed\n", __func__);
item->mmap_entry = entry_info.curr_p;
out:
NOVA_END_TIMING(append_mmap_entry_t, append_time);
return ret;
}
int nova_append_snapshot_info_entry(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info *si,
struct snapshot_info *info, struct nova_snapshot_info_entry *data,
struct nova_inode_update *update)
{
struct nova_inode_info_header *sih = &si->header;
struct nova_inode inode_copy;
struct nova_log_entry_info entry_info;
timing_t append_time;
int ret;
NOVA_START_TIMING(append_snapshot_info_t, append_time);
nova_update_entry_csum(data);
entry_info.type = SNAPSHOT_INFO;
entry_info.update = update;
entry_info.data = data;
entry_info.epoch_id = data->epoch_id;
entry_info.inplace = 0;
if (nova_check_inode_integrity(sb, sih->ino, sih->pi_addr,
sih->alter_pi_addr, &inode_copy, 0) < 0) {
ret = -EIO;
goto out;
}
ret = nova_append_log_entry(sb, pi, NULL, sih, &entry_info);
if (ret)
nova_err(sb, "%s failed\n", __func__);
info->snapshot_entry = entry_info.curr_p;
out:
NOVA_END_TIMING(append_snapshot_info_t, append_time);
return ret;
}
int nova_append_dentry(struct super_block *sb, struct nova_inode *pi,
struct inode *dir, struct dentry *dentry, u64 ino,
unsigned short de_len, struct nova_inode_update *update,
int link_change, u64 epoch_id)
{
struct nova_inode_info *si = NOVA_I(dir);
struct nova_inode_info_header *sih = &si->header;
struct nova_inode inode_copy;
struct nova_log_entry_info entry_info;
timing_t append_time;
int ret;
NOVA_START_TIMING(append_dir_entry_t, append_time);
entry_info.type = DIR_LOG;
entry_info.update = update;
entry_info.data = dentry;
entry_info.ino = ino;