-
Notifications
You must be signed in to change notification settings - Fork 1
/
journal.c
474 lines (386 loc) · 12.6 KB
/
journal.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
/*
* NOVA journaling facility.
*
* This file contains journaling code to guarantee the atomicity of directory
* operations that span multiple inodes (unlink, rename, etc).
*
* 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>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/vfs.h>
#include <linux/uaccess.h>
#include <linux/mm.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include "nova.h"
#include "journal.h"
/**************************** Lite journal ******************************/
static inline void
nova_print_lite_transaction(struct nova_lite_journal_entry *entry)
{
nova_dbg("Entry %p: Type %llu, data1 0x%llx, data2 0x%llx\n, checksum %u\n",
entry, entry->type,
entry->data1, entry->data2, entry->csum);
}
static inline int nova_update_journal_entry_csum(struct super_block *sb,
struct nova_lite_journal_entry *entry)
{
u32 crc = 0;
crc = nova_crc32c(~0, (__u8 *)entry,
(sizeof(struct nova_lite_journal_entry)
- sizeof(__le32)));
entry->csum = cpu_to_le32(crc);
nova_flush_buffer(entry, sizeof(struct nova_lite_journal_entry), 0);
return 0;
}
static inline int nova_check_entry_integrity(struct super_block *sb,
struct nova_lite_journal_entry *entry)
{
u32 crc = 0;
crc = nova_crc32c(~0, (__u8 *)entry,
(sizeof(struct nova_lite_journal_entry)
- sizeof(__le32)));
if (entry->csum == cpu_to_le32(crc))
return 0;
else
return 1;
}
// Get the next journal entry. Journal entries are stored in a circular
// buffer. They live a 1-page circular buffer.
//
// TODO: Add check to ensure that the journal doesn't grow too large.
static inline u64 next_lite_journal(u64 curr_p)
{
size_t size = sizeof(struct nova_lite_journal_entry);
if ((curr_p & (PAGE_SIZE - 1)) + size >= PAGE_SIZE)
return (curr_p & PAGE_MASK);
return curr_p + size;
}
// Walk the journal for one CPU, and verify the checksum on each entry.
static int nova_check_journal_entries(struct super_block *sb,
struct journal_ptr_pair *pair)
{
struct nova_lite_journal_entry *entry;
u64 temp;
int ret;
temp = pair->journal_head;
while (temp != pair->journal_tail) {
entry = (struct nova_lite_journal_entry *)nova_get_block(sb,
temp);
ret = nova_check_entry_integrity(sb, entry);
if (ret) {
nova_dbg("Entry %p checksum failure\n", entry);
nova_print_lite_transaction(entry);
return ret;
}
temp = next_lite_journal(temp);
}
return 0;
}
/**************************** Journal Recovery ******************************/
static void nova_undo_journal_inode(struct super_block *sb,
struct nova_lite_journal_entry *entry)
{
struct nova_inode *pi, *alter_pi;
u64 pi_addr, alter_pi_addr;
if (metadata_csum == 0)
return;
pi_addr = le64_to_cpu(entry->data1);
alter_pi_addr = le64_to_cpu(entry->data2);
pi = (struct nova_inode *)nova_get_block(sb, pi_addr);
alter_pi = (struct nova_inode *)nova_get_block(sb, alter_pi_addr);
memcpy_to_pmem_nocache(pi, alter_pi, sizeof(struct nova_inode));
}
static void nova_undo_journal_entry(struct super_block *sb,
struct nova_lite_journal_entry *entry)
{
u64 addr, value;
addr = le64_to_cpu(entry->data1);
value = le64_to_cpu(entry->data2);
*(u64 *)nova_get_block(sb, addr) = (u64)value;
nova_flush_buffer((void *)nova_get_block(sb, addr), CACHELINE_SIZE, 0);
}
static void nova_undo_lite_journal_entry(struct super_block *sb,
struct nova_lite_journal_entry *entry)
{
u64 type;
type = le64_to_cpu(entry->type);
switch (type) {
case JOURNAL_INODE:
nova_undo_journal_inode(sb, entry);
break;
case JOURNAL_ENTRY:
nova_undo_journal_entry(sb, entry);
break;
default:
nova_dbg("%s: unknown data type %llu\n", __func__, type);
break;
}
}
/* Roll back all journal enries */
static int nova_recover_lite_journal(struct super_block *sb,
struct journal_ptr_pair *pair)
{
struct nova_lite_journal_entry *entry;
u64 temp;
nova_memunlock_journal(sb);
temp = pair->journal_head;
while (temp != pair->journal_tail) {
entry = (struct nova_lite_journal_entry *)nova_get_block(sb,
temp);
nova_undo_lite_journal_entry(sb, entry);
temp = next_lite_journal(temp);
}
pair->journal_tail = pair->journal_head;
nova_memlock_journal(sb);
nova_flush_buffer(&pair->journal_head, CACHELINE_SIZE, 1);
return 0;
}
/**************************** Create/commit ******************************/
static u64 nova_append_replica_inode_journal(struct super_block *sb,
u64 curr_p, struct inode *inode)
{
struct nova_lite_journal_entry *entry;
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
entry = (struct nova_lite_journal_entry *)nova_get_block(sb,
curr_p);
entry->type = cpu_to_le64(JOURNAL_INODE);
entry->padding = 0;
entry->data1 = cpu_to_le64(sih->pi_addr);
entry->data2 = cpu_to_le64(sih->alter_pi_addr);
nova_update_journal_entry_csum(sb, entry);
curr_p = next_lite_journal(curr_p);
return curr_p;
}
/* Create and append an undo entry for a small update to PMEM. */
static u64 nova_append_entry_journal(struct super_block *sb,
u64 curr_p, void *field)
{
struct nova_lite_journal_entry *entry;
struct nova_sb_info *sbi = NOVA_SB(sb);
u64 *aligned_field;
u64 addr;
entry = (struct nova_lite_journal_entry *)nova_get_block(sb,
curr_p);
entry->type = cpu_to_le64(JOURNAL_ENTRY);
entry->padding = 0;
/* Align to 8 bytes */
aligned_field = (u64 *)((unsigned long)field & ~7UL);
/* Store the offset from the start of Nova instead of the pointer */
addr = (u64)nova_get_addr_off(sbi, aligned_field);
entry->data1 = cpu_to_le64(addr);
entry->data2 = cpu_to_le64(*aligned_field);
nova_update_journal_entry_csum(sb, entry);
curr_p = next_lite_journal(curr_p);
return curr_p;
}
static u64 nova_journal_inode_tail(struct super_block *sb,
u64 curr_p, struct nova_inode *pi)
{
curr_p = nova_append_entry_journal(sb, curr_p, &pi->log_tail);
if (metadata_csum)
curr_p = nova_append_entry_journal(sb, curr_p,
&pi->alter_log_tail);
return curr_p;
}
/* Create and append undo log entries for creating a new file or directory. */
static u64 nova_append_inode_journal(struct super_block *sb,
u64 curr_p, struct inode *inode, int new_inode,
int invalidate, int is_dir)
{
struct nova_inode *pi = nova_get_inode(sb, inode);
if (metadata_csum)
return nova_append_replica_inode_journal(sb, curr_p, inode);
if (!pi) {
nova_err(sb, "%s: get inode failed\n", __func__);
return curr_p;
}
if (is_dir)
return nova_journal_inode_tail(sb, curr_p, pi);
if (new_inode) {
curr_p = nova_append_entry_journal(sb, curr_p,
&pi->valid);
} else {
curr_p = nova_journal_inode_tail(sb, curr_p, pi);
if (invalidate) {
curr_p = nova_append_entry_journal(sb, curr_p,
&pi->valid);
curr_p = nova_append_entry_journal(sb, curr_p,
&pi->delete_epoch_id);
}
}
return curr_p;
}
static u64 nova_append_dentry_journal(struct super_block *sb,
u64 curr_p, struct nova_dentry *dentry)
{
curr_p = nova_append_entry_journal(sb, curr_p, &dentry->ino);
curr_p = nova_append_entry_journal(sb, curr_p, &dentry->csum);
return curr_p;
}
/* Journaled transactions for inode creation */
u64 nova_create_inode_transaction(struct super_block *sb,
struct inode *inode, struct inode *dir, int cpu,
int new_inode, int invalidate)
{
struct journal_ptr_pair *pair;
u64 temp;
pair = nova_get_journal_pointers(sb, cpu);
if (pair->journal_head == 0 ||
pair->journal_head != pair->journal_tail)
BUG();
temp = pair->journal_head;
temp = nova_append_inode_journal(sb, temp, inode,
new_inode, invalidate, 0);
temp = nova_append_inode_journal(sb, temp, dir,
new_inode, invalidate, 1);
pair->journal_tail = temp;
nova_flush_buffer(&pair->journal_head, CACHELINE_SIZE, 1);
nova_dbgv("%s: head 0x%llx, tail 0x%llx\n",
__func__, pair->journal_head, pair->journal_tail);
return temp;
}
/* Journaled transactions for rename operations */
u64 nova_create_rename_transaction(struct super_block *sb,
struct inode *old_inode, struct inode *old_dir, struct inode *new_inode,
struct inode *new_dir, struct nova_dentry *father_entry,
int invalidate_new_inode, int cpu)
{
struct journal_ptr_pair *pair;
u64 temp;
pair = nova_get_journal_pointers(sb, cpu);
if (pair->journal_head == 0 ||
pair->journal_head != pair->journal_tail)
BUG();
temp = pair->journal_head;
/* Journal tails for old inode */
temp = nova_append_inode_journal(sb, temp, old_inode, 0, 0, 0);
/* Journal tails for old dir */
temp = nova_append_inode_journal(sb, temp, old_dir, 0, 0, 1);
if (new_inode) {
/* New inode may be unlinked */
temp = nova_append_inode_journal(sb, temp, new_inode, 0,
invalidate_new_inode, 0);
}
if (new_dir)
temp = nova_append_inode_journal(sb, temp, new_dir, 0, 0, 1);
if (father_entry)
temp = nova_append_dentry_journal(sb, temp, father_entry);
pair->journal_tail = temp;
nova_flush_buffer(&pair->journal_head, CACHELINE_SIZE, 1);
nova_dbgv("%s: head 0x%llx, tail 0x%llx\n",
__func__, pair->journal_head, pair->journal_tail);
return temp;
}
/* For log entry inplace update */
u64 nova_create_logentry_transaction(struct super_block *sb,
void *entry, enum nova_entry_type type, int cpu)
{
struct journal_ptr_pair *pair;
size_t size = 0;
int i, count;
u64 temp;
pair = nova_get_journal_pointers(sb, cpu);
if (pair->journal_head == 0 ||
pair->journal_head != pair->journal_tail)
BUG();
size = nova_get_log_entry_size(sb, type);
temp = pair->journal_head;
count = size / 8;
for (i = 0; i < count; i++) {
temp = nova_append_entry_journal(sb, temp,
(char *)entry + i * 8);
}
pair->journal_tail = temp;
nova_flush_buffer(&pair->journal_head, CACHELINE_SIZE, 1);
nova_dbgv("%s: head 0x%llx, tail 0x%llx\n",
__func__, pair->journal_head, pair->journal_tail);
return temp;
}
/* Commit the transactions by dropping the journal entries */
void nova_commit_lite_transaction(struct super_block *sb, u64 tail, int cpu)
{
struct journal_ptr_pair *pair;
pair = nova_get_journal_pointers(sb, cpu);
if (pair->journal_tail != tail)
BUG();
pair->journal_head = tail;
nova_flush_buffer(&pair->journal_head, CACHELINE_SIZE, 1);
}
/**************************** Initialization ******************************/
// Initialized DRAM journal state, validate, and recover
int nova_lite_journal_soft_init(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct journal_ptr_pair *pair;
int i;
int ret = 0;
sbi->journal_locks = kcalloc(sbi->cpus, sizeof(spinlock_t),
GFP_KERNEL);
if (!sbi->journal_locks)
return -ENOMEM;
for (i = 0; i < sbi->cpus; i++)
spin_lock_init(&sbi->journal_locks[i]);
for (i = 0; i < sbi->cpus; i++) {
pair = nova_get_journal_pointers(sb, i);
if (pair->journal_head == pair->journal_tail)
continue;
/* Ensure all entries are genuine */
ret = nova_check_journal_entries(sb, pair);
if (ret) {
nova_err(sb, "Journal %d checksum failure\n", i);
ret = -EINVAL;
break;
}
ret = nova_recover_lite_journal(sb, pair);
}
return ret;
}
/* Initialized persistent journal state */
int nova_lite_journal_hard_init(struct super_block *sb)
{
struct nova_sb_info *sbi = NOVA_SB(sb);
struct nova_inode_info_header sih;
struct journal_ptr_pair *pair;
unsigned long blocknr = 0;
int allocated;
int i;
u64 block;
sih.ino = NOVA_LITEJOURNAL_INO;
sih.i_blk_type = NOVA_BLOCK_TYPE_4K;
for (i = 0; i < sbi->cpus; i++) {
pair = nova_get_journal_pointers(sb, i);
allocated = nova_new_log_blocks(sb, &sih, &blocknr, 1,
ALLOC_INIT_ZERO, ANY_CPU, ALLOC_FROM_HEAD);
nova_dbg_verbose("%s: allocate log @ 0x%lx\n", __func__,
blocknr);
if (allocated != 1 || blocknr == 0)
return -ENOSPC;
block = nova_get_block_off(sb, blocknr, NOVA_BLOCK_TYPE_4K);
nova_memunlock_range(sb, pair, CACHELINE_SIZE);
pair->journal_head = pair->journal_tail = block;
nova_flush_buffer(pair, CACHELINE_SIZE, 0);
nova_memlock_range(sb, pair, CACHELINE_SIZE);
}
PERSISTENT_BARRIER();
return nova_lite_journal_soft_init(sb);
}