-
Notifications
You must be signed in to change notification settings - Fork 1
/
gc.c
739 lines (649 loc) · 20.5 KB
/
gc.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
/*
* BRIEF DESCRIPTION
*
* Garbage collection 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 "inode.h"
static bool curr_log_entry_invalid(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info_header *sih,
u64 curr_p, size_t *length)
{
struct nova_file_write_entry *entry;
struct nova_dentry *dentry;
struct nova_setattr_logentry *setattr_entry;
struct nova_link_change_entry *linkc_entry;
struct nova_mmap_entry *mmap_entry;
struct nova_snapshot_info_entry *sn_entry;
char entry_copy[NOVA_MAX_ENTRY_LEN];
void *addr, *entryc;
u8 type;
bool ret = true;
addr = (void *)nova_get_block(sb, curr_p);
/* FIXME: this check might hurt performance for workloads that
* frequently invokes gc
*/
if (metadata_csum == 0)
entryc = addr;
else {
entryc = entry_copy;
if (!nova_verify_entry_csum(sb, addr, entryc))
return true;
}
type = nova_get_entry_type(entryc);
switch (type) {
case SET_ATTR:
setattr_entry = (struct nova_setattr_logentry *) entryc;
if (setattr_entry->invalid == 0)
ret = false;
*length = sizeof(struct nova_setattr_logentry);
break;
case LINK_CHANGE:
linkc_entry = (struct nova_link_change_entry *) entryc;
if (linkc_entry->invalid == 0)
ret = false;
*length = sizeof(struct nova_link_change_entry);
break;
case FILE_WRITE:
entry = (struct nova_file_write_entry *) entryc;
if (entry->num_pages != entry->invalid_pages)
ret = false;
*length = sizeof(struct nova_file_write_entry);
break;
case DIR_LOG:
dentry = (struct nova_dentry *) entryc;
if (dentry->invalid == 0)
ret = false;
if (sih->last_dentry == curr_p)
ret = false;
*length = le16_to_cpu(dentry->de_len);
break;
case MMAP_WRITE:
mmap_entry = (struct nova_mmap_entry *) entryc;
if (mmap_entry->invalid == 0)
ret = false;
*length = sizeof(struct nova_mmap_entry);
break;
case SNAPSHOT_INFO:
sn_entry = (struct nova_snapshot_info_entry *) entryc;
if (sn_entry->deleted == 0)
ret = false;
*length = sizeof(struct nova_snapshot_info_entry);
break;
case NEXT_PAGE:
/* No more entries in this page */
*length = PAGE_SIZE - ENTRY_LOC(curr_p);
break;
default:
nova_dbg("%s: unknown type %d, 0x%llx\n",
__func__, type, curr_p);
NOVA_ASSERT(0);
*length = PAGE_SIZE - ENTRY_LOC(curr_p);
break;
}
return ret;
}
static bool curr_page_invalid(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info_header *sih,
u64 page_head)
{
struct nova_inode_log_page *curr_page;
struct nova_inode_page_tail page_tail;
unsigned int num_entries;
unsigned int invalid_entries;
bool ret;
timing_t check_time;
int rc;
NOVA_START_TIMING(check_invalid_t, check_time);
curr_page = (struct nova_inode_log_page *)
nova_get_block(sb, page_head);
rc = memcpy_mcsafe(&page_tail, &curr_page->page_tail,
sizeof(struct nova_inode_page_tail));
if (rc) {
/* FIXME: Recover use replica log */
nova_err(sb, "check page failed\n");
return false;
}
num_entries = le32_to_cpu(page_tail.num_entries);
invalid_entries = le32_to_cpu(page_tail.invalid_entries);
ret = (invalid_entries == num_entries);
if (!ret) {
sih->num_entries += num_entries;
sih->valid_entries += num_entries - invalid_entries;
}
NOVA_END_TIMING(check_invalid_t, check_time);
return ret;
}
static void free_curr_page(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_inode_log_page *curr_page,
struct nova_inode_log_page *last_page, u64 curr_head)
{
u8 btype = sih->i_blk_type;
nova_memunlock_block(sb, last_page);
nova_set_next_page_address(sb, last_page,
curr_page->page_tail.next_page, 1);
nova_memlock_block(sb, last_page);
nova_free_log_blocks(sb, sih,
nova_get_blocknr(sb, curr_head, btype), 1);
}
static int nova_gc_assign_file_entry(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_file_write_entry *old_entry,
struct nova_file_write_entry *new_entry)
{
struct nova_file_write_entry *temp;
void **pentry;
unsigned long start_pgoff = old_entry->pgoff;
unsigned int num = old_entry->num_pages;
unsigned long curr_pgoff;
int i;
int ret = 0;
for (i = 0; i < num; i++) {
curr_pgoff = start_pgoff + i;
pentry = radix_tree_lookup_slot(&sih->tree, curr_pgoff);
if (pentry) {
temp = radix_tree_deref_slot(pentry);
if (temp == old_entry)
radix_tree_replace_slot(&sih->tree, pentry,
new_entry);
}
}
return ret;
}
static int nova_gc_assign_dentry(struct super_block *sb,
struct nova_inode_info_header *sih, struct nova_dentry *old_dentry,
struct nova_dentry *new_dentry)
{
struct nova_range_node *ret_node = NULL;
unsigned long hash;
int found = 0;
int ret = 0;
hash = BKDRHash(old_dentry->name, old_dentry->name_len);
nova_dbgv("%s: assign %s hash %lu\n", __func__,
old_dentry->name, hash);
/* FIXME: hash collision ignored here */
found = nova_find_range_node(&sih->rb_tree, hash,
NODE_DIR, &ret_node);
if (found == 1 && hash == ret_node->hash) {
if (ret_node->direntry == old_dentry)
ret_node->direntry = new_dentry;
}
return ret;
}
static int nova_gc_assign_mmap_entry(struct super_block *sb,
struct nova_inode_info_header *sih, u64 curr_p, u64 new_curr)
{
struct vma_item *item;
struct rb_node *temp;
int ret = 0;
if (sih->num_vmas == 0)
return ret;
temp = rb_first(&sih->vma_tree);
while (temp) {
item = container_of(temp, struct vma_item, node);
temp = rb_next(temp);
if (item->mmap_entry == curr_p) {
item->mmap_entry = new_curr;
break;
}
}
return ret;
}
static int nova_gc_assign_snapshot_entry(struct super_block *sb,
struct nova_inode_info_header *sih,
struct nova_snapshot_info_entry *old_entry, u64 curr_p, u64 new_curr)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct snapshot_info *info;
int ret = 0;
info = radix_tree_lookup(&sbi->snapshot_info_tree,
old_entry->epoch_id);
if (info && info->snapshot_entry == curr_p)
info->snapshot_entry = new_curr;
return ret;
}
static int nova_gc_assign_new_entry(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info_header *sih,
u64 curr_p, u64 new_curr)
{
struct nova_file_write_entry *old_entry, *new_entry;
struct nova_dentry *old_dentry, *new_dentry;
void *addr, *new_addr;
u8 type;
int ret = 0;
addr = (void *)nova_get_block(sb, curr_p);
type = nova_get_entry_type(addr);
switch (type) {
case SET_ATTR:
sih->last_setattr = new_curr;
break;
case LINK_CHANGE:
sih->last_link_change = new_curr;
break;
case MMAP_WRITE:
ret = nova_gc_assign_mmap_entry(sb, sih, curr_p, new_curr);
break;
case SNAPSHOT_INFO:
ret = nova_gc_assign_snapshot_entry(sb, sih, addr,
curr_p, new_curr);
break;
case FILE_WRITE:
new_addr = (void *)nova_get_block(sb, new_curr);
old_entry = (struct nova_file_write_entry *)addr;
new_entry = (struct nova_file_write_entry *)new_addr;
ret = nova_gc_assign_file_entry(sb, sih, old_entry, new_entry);
break;
case DIR_LOG:
new_addr = (void *)nova_get_block(sb, new_curr);
old_dentry = (struct nova_dentry *)addr;
new_dentry = (struct nova_dentry *)new_addr;
if (sih->last_dentry == curr_p)
sih->last_dentry = new_curr;
ret = nova_gc_assign_dentry(sb, sih, old_dentry, new_dentry);
break;
default:
nova_dbg("%s: unknown type %d, 0x%llx\n",
__func__, type, curr_p);
NOVA_ASSERT(0);
break;
}
return ret;
}
/* Copy live log entries to the new log and atomically replace the old log */
static unsigned long nova_inode_log_thorough_gc(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info_header *sih,
unsigned long blocks, unsigned long checked_pages)
{
struct nova_inode_log_page *curr_page = NULL;
size_t length;
struct nova_inode *alter_pi;
u64 ino = pi->nova_ino;
u64 curr_p, new_curr;
u64 old_curr_p;
u64 tail_block;
u64 old_head;
u64 new_head = 0;
u64 next;
int allocated;
int extended = 0;
int ret;
timing_t gc_time;
NOVA_START_TIMING(thorough_gc_t, gc_time);
curr_p = sih->log_head;
old_curr_p = curr_p;
old_head = sih->log_head;
nova_dbg_verbose("Log head 0x%llx, tail 0x%llx\n",
curr_p, sih->log_tail);
if (curr_p == 0 && sih->log_tail == 0)
goto out;
if (curr_p >> PAGE_SHIFT == sih->log_tail >> PAGE_SHIFT)
goto out;
allocated = nova_allocate_inode_log_pages(sb, sih, blocks,
&new_head, ANY_CPU, 0);
if (allocated != blocks) {
nova_err(sb, "%s: ERROR: no inode log page available\n",
__func__);
goto out;
}
new_curr = new_head;
while (curr_p != sih->log_tail) {
old_curr_p = curr_p;
if (goto_next_page(sb, curr_p))
curr_p = next_log_page(sb, curr_p);
if (curr_p >> PAGE_SHIFT == sih->log_tail >> PAGE_SHIFT) {
/* Don't recycle tail page */
break;
}
if (curr_p == 0) {
nova_err(sb, "File inode %llu log is NULL!\n", ino);
BUG();
}
length = 0;
ret = curr_log_entry_invalid(sb, pi, sih, curr_p, &length);
if (!ret) {
extended = 0;
new_curr = nova_get_append_head(sb, pi, sih,
new_curr, length, MAIN_LOG,
1, &extended);
if (extended)
blocks++;
/* Copy entry to the new log */
nova_memunlock_block(sb, nova_get_block(sb, new_curr));
memcpy_to_pmem_nocache(nova_get_block(sb, new_curr),
nova_get_block(sb, curr_p), length);
nova_inc_page_num_entries(sb, new_curr);
nova_memlock_block(sb, nova_get_block(sb, new_curr));
nova_gc_assign_new_entry(sb, pi, sih, curr_p, new_curr);
new_curr += length;
}
curr_p += length;
}
/* Step 1: Link new log to the tail block */
tail_block = BLOCK_OFF(sih->log_tail);
curr_page = (struct nova_inode_log_page *)nova_get_block(sb,
BLOCK_OFF(new_curr));
next = next_log_page(sb, new_curr);
if (next > 0)
nova_free_contiguous_log_blocks(sb, sih, next);
nova_memunlock_block(sb, curr_page);
nova_set_next_page_flag(sb, new_curr);
nova_set_next_page_address(sb, curr_page, tail_block, 0);
nova_memlock_block(sb, curr_page);
/* Step 2: Atomically switch to the new log */
nova_memunlock_inode(sb, pi);
pi->log_head = new_head;
nova_update_inode_checksum(pi);
if (metadata_csum && sih->alter_pi_addr) {
alter_pi = (struct nova_inode *)nova_get_block(sb,
sih->alter_pi_addr);
memcpy_to_pmem_nocache(alter_pi, pi, sizeof(struct nova_inode));
}
nova_memlock_inode(sb, pi);
nova_flush_buffer(pi, sizeof(struct nova_inode), 1);
sih->log_head = new_head;
/* Step 3: Unlink the old log */
curr_page = (struct nova_inode_log_page *)nova_get_block(sb,
BLOCK_OFF(old_curr_p));
next = next_log_page(sb, old_curr_p);
if (next != tail_block) {
nova_err(sb, "Old log error: old curr_p 0x%lx, next 0x%lx ",
"curr_p 0x%lx, tail block 0x%lx\n", old_curr_p,
next, curr_p, tail_block);
BUG();
}
nova_memunlock_block(sb, curr_page);
nova_set_next_page_address(sb, curr_page, 0, 1);
nova_memlock_block(sb, curr_page);
/* Step 4: Free the old log */
nova_free_contiguous_log_blocks(sb, sih, old_head);
sih->log_pages = sih->log_pages + blocks - checked_pages;
NOVA_STATS_ADD(thorough_gc_pages, checked_pages - blocks);
NOVA_STATS_ADD(thorough_checked_pages, checked_pages);
out:
NOVA_END_TIMING(thorough_gc_t, gc_time);
return blocks;
}
/* Copy original log to alternate log */
static unsigned long nova_inode_alter_log_thorough_gc(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info_header *sih,
unsigned long blocks, unsigned long checked_pages)
{
struct nova_inode_log_page *alter_curr_page = NULL;
struct nova_inode *alter_pi;
u64 ino = pi->nova_ino;
u64 curr_p, new_curr;
u64 alter_curr_p;
u64 old_alter_curr_p;
u64 alter_tail_block;
u64 alter_old_head;
u64 new_head = 0;
u64 alter_next;
int allocated;
timing_t gc_time;
NOVA_START_TIMING(thorough_gc_t, gc_time);
curr_p = sih->log_head;
alter_old_head = sih->alter_log_head;
nova_dbg_verbose("Log head 0x%llx, tail 0x%llx\n",
curr_p, sih->log_tail);
if (curr_p == 0 && sih->log_tail == 0)
goto out;
if (curr_p >> PAGE_SHIFT == sih->log_tail >> PAGE_SHIFT)
goto out;
if (alter_old_head >> PAGE_SHIFT == sih->alter_log_tail >> PAGE_SHIFT)
goto out;
allocated = nova_allocate_inode_log_pages(sb, sih, blocks,
&new_head, ANY_CPU, 1);
if (allocated != blocks) {
nova_err(sb, "%s: ERROR: no inode log page available\n",
__func__);
goto out;
}
new_curr = new_head;
while (1) {
nova_memunlock_block(sb, nova_get_block(sb, new_curr));
memcpy_to_pmem_nocache(nova_get_block(sb, new_curr),
nova_get_block(sb, curr_p), LOG_BLOCK_TAIL);
nova_set_alter_page_address(sb, curr_p, new_curr);
nova_memlock_block(sb, nova_get_block(sb, new_curr));
curr_p = next_log_page(sb, curr_p);
if (curr_p >> PAGE_SHIFT == sih->log_tail >> PAGE_SHIFT) {
/* Don't recycle tail page */
break;
}
new_curr = next_log_page(sb, new_curr);
if (curr_p == 0) {
nova_err(sb, "File inode %llu log is NULL!\n", ino);
BUG();
}
}
/* Step 1: Link new log to the tail block */
alter_tail_block = BLOCK_OFF(sih->alter_log_tail);
alter_curr_page = (struct nova_inode_log_page *)nova_get_block(sb,
BLOCK_OFF(new_curr));
alter_next = next_log_page(sb, new_curr);
if (alter_next > 0)
nova_free_contiguous_log_blocks(sb, sih, alter_next);
nova_memunlock_block(sb, alter_curr_page);
nova_set_next_page_address(sb, alter_curr_page, alter_tail_block, 0);
nova_memlock_block(sb, alter_curr_page);
/* Step 2: Find the old log block before the tail block */
alter_curr_p = sih->alter_log_head;
while (1) {
old_alter_curr_p = alter_curr_p;
alter_curr_p = next_log_page(sb, alter_curr_p);
if (alter_curr_p >> PAGE_SHIFT ==
sih->alter_log_tail >> PAGE_SHIFT)
break;
if (alter_curr_p == 0) {
nova_err(sb, "File inode %llu log is NULL!\n", ino);
BUG();
}
}
/* Step 3: Atomically switch to the new log */
nova_memunlock_inode(sb, pi);
pi->alter_log_head = new_head;
nova_update_inode_checksum(pi);
if (metadata_csum && sih->alter_pi_addr) {
alter_pi = (struct nova_inode *)nova_get_block(sb,
sih->alter_pi_addr);
memcpy_to_pmem_nocache(alter_pi, pi, sizeof(struct nova_inode));
}
nova_memlock_inode(sb, pi);
nova_flush_buffer(pi, sizeof(struct nova_inode), 1);
sih->alter_log_head = new_head;
/* Step 4: Unlink the old log */
alter_curr_page = (struct nova_inode_log_page *)nova_get_block(sb,
BLOCK_OFF(old_alter_curr_p));
alter_next = next_log_page(sb, old_alter_curr_p);
if (alter_next != alter_tail_block) {
nova_err(sb, "Old log error: old curr_p 0x%lx, next 0x%lx ",
"curr_p 0x%lx, tail block 0x%lx\n", old_alter_curr_p,
alter_next, alter_curr_p, alter_tail_block);
BUG();
}
nova_memunlock_block(sb, alter_curr_page);
nova_set_next_page_address(sb, alter_curr_page, 0, 1);
nova_memlock_block(sb, alter_curr_page);
/* Step 5: Free the old log */
nova_free_contiguous_log_blocks(sb, sih, alter_old_head);
sih->log_pages = sih->log_pages + blocks - checked_pages;
NOVA_STATS_ADD(thorough_gc_pages, checked_pages - blocks);
NOVA_STATS_ADD(thorough_checked_pages, checked_pages);
out:
NOVA_END_TIMING(thorough_gc_t, gc_time);
return blocks;
}
/*
* Scan pages in the log and remove those with no valid log entries.
*/
int nova_inode_log_fast_gc(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info_header *sih,
u64 curr_tail, u64 new_block, u64 alter_new_block,
int num_pages, int force_thorough)
{
struct nova_inode *alter_pi;
u64 curr, next, possible_head = 0;
u64 alter_curr, alter_next = 0, alter_possible_head = 0;
int found_head = 0;
struct nova_inode_log_page *last_page = NULL;
struct nova_inode_log_page *curr_page = NULL;
struct nova_inode_log_page *alter_last_page = NULL;
struct nova_inode_log_page *alter_curr_page = NULL;
int first_need_free = 0;
int num_logs;
u8 btype = sih->i_blk_type;
unsigned long blocks;
unsigned long checked_pages = 0;
int freed_pages = 0;
timing_t gc_time;
NOVA_START_TIMING(fast_gc_t, gc_time);
curr = sih->log_head;
alter_curr = sih->alter_log_head;
sih->valid_entries = 0;
sih->num_entries = 0;
num_logs = 1;
if (metadata_csum)
num_logs = 2;
nova_dbgv("%s: log head 0x%llx, tail 0x%llx\n",
__func__, curr, curr_tail);
while (1) {
if (curr >> PAGE_SHIFT == sih->log_tail >> PAGE_SHIFT) {
/* Don't recycle tail page */
if (found_head == 0) {
possible_head = cpu_to_le64(curr);
alter_possible_head = cpu_to_le64(alter_curr);
}
break;
}
curr_page = (struct nova_inode_log_page *)
nova_get_block(sb, curr);
next = next_log_page(sb, curr);
if (next < 0)
break;
if (metadata_csum) {
alter_curr_page = (struct nova_inode_log_page *)
nova_get_block(sb, alter_curr);
alter_next = next_log_page(sb, alter_curr);
if (alter_next < 0)
break;
}
nova_dbg_verbose("curr 0x%llx, next 0x%llx\n", curr, next);
if (curr_page_invalid(sb, pi, sih, curr)) {
nova_dbg_verbose("curr page %p invalid\n", curr_page);
if (curr == sih->log_head) {
/* Free first page later */
first_need_free = 1;
last_page = curr_page;
alter_last_page = alter_curr_page;
} else {
nova_dbg_verbose("Free log block 0x%llx\n",
curr >> PAGE_SHIFT);
free_curr_page(sb, sih, curr_page, last_page,
curr);
if (metadata_csum)
free_curr_page(sb, sih, alter_curr_page,
alter_last_page, alter_curr);
}
NOVA_STATS_ADD(fast_gc_pages, 1);
freed_pages++;
} else {
if (found_head == 0) {
possible_head = cpu_to_le64(curr);
alter_possible_head = cpu_to_le64(alter_curr);
found_head = 1;
}
last_page = curr_page;
alter_last_page = alter_curr_page;
}
curr = next;
alter_curr = alter_next;
checked_pages++;
if (curr == 0 || (metadata_csum && alter_curr == 0))
break;
}
NOVA_STATS_ADD(fast_checked_pages, checked_pages);
nova_dbgv("checked pages %lu, freed %d\n", checked_pages, freed_pages);
checked_pages -= freed_pages;
// TODO: I think this belongs in nova_extend_inode_log.
if (num_pages > 0) {
curr = BLOCK_OFF(curr_tail);
curr_page = (struct nova_inode_log_page *)
nova_get_block(sb, curr);
nova_memunlock_block(sb, curr_page);
nova_set_next_page_address(sb, curr_page, new_block, 1);
nova_memlock_block(sb, curr_page);
if (metadata_csum) {
alter_curr = BLOCK_OFF(sih->alter_log_tail);
while (next_log_page(sb, alter_curr) > 0)
alter_curr = next_log_page(sb, alter_curr);
alter_curr_page = (struct nova_inode_log_page *)
nova_get_block(sb, alter_curr);
nova_memunlock_block(sb, curr_page);
nova_set_next_page_address(sb, alter_curr_page,
alter_new_block, 1);
nova_memlock_block(sb, curr_page);
}
}
curr = sih->log_head;
alter_curr = sih->alter_log_head;
nova_memunlock_inode(sb, pi);
pi->log_head = possible_head;
pi->alter_log_head = alter_possible_head;
nova_update_inode_checksum(pi);
if (metadata_csum && sih->alter_pi_addr) {
alter_pi = (struct nova_inode *)nova_get_block(sb,
sih->alter_pi_addr);
memcpy_to_pmem_nocache(alter_pi, pi, sizeof(struct nova_inode));
}
nova_memlock_inode(sb, pi);
sih->log_head = possible_head;
sih->alter_log_head = alter_possible_head;
nova_dbgv("%s: %d new head 0x%llx\n", __func__,
found_head, possible_head);
sih->log_pages += (num_pages - freed_pages) * num_logs;
/* Don't update log tail pointer here */
nova_flush_buffer(&pi->log_head, CACHELINE_SIZE, 1);
if (first_need_free) {
nova_dbg_verbose("Free log head block 0x%llx\n",
curr >> PAGE_SHIFT);
nova_free_log_blocks(sb, sih,
nova_get_blocknr(sb, curr, btype), 1);
if (metadata_csum)
nova_free_log_blocks(sb, sih,
nova_get_blocknr(sb, alter_curr, btype), 1);
}
NOVA_END_TIMING(fast_gc_t, gc_time);
if (sih->num_entries == 0)
return 0;
/* Estimate how many pages worth of valid entries the log contains.
*
* If it is less than half the number pages that remain in the log,
* compress them with thorough gc.
*/
blocks = (sih->valid_entries * checked_pages) / sih->num_entries;
if ((sih->valid_entries * checked_pages) % sih->num_entries)
blocks++;
if (force_thorough || (blocks && blocks * 2 < checked_pages)) {
nova_dbgv("Thorough GC for inode %lu: checked pages %lu, valid pages %lu\n",
sih->ino,
checked_pages, blocks);
blocks = nova_inode_log_thorough_gc(sb, pi, sih,
blocks, checked_pages);
if (metadata_csum)
nova_inode_alter_log_thorough_gc(sb, pi, sih,
blocks, checked_pages);
}
return 0;
}