-
Notifications
You must be signed in to change notification settings - Fork 6
/
file.c
413 lines (342 loc) · 10.2 KB
/
file.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
/*
* BRIEF DESCRIPTION
*
* File operations for files.
*
* 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 <linux/slab.h>
#include <linux/uio.h>
#include <linux/uaccess.h>
#include <linux/falloc.h>
#include <asm/mman.h>
#include "nova.h"
static inline int nova_can_set_blocksize_hint(struct inode *inode,
struct nova_inode *pi, loff_t new_size)
{
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
/* Currently, we don't deallocate data blocks till the file is deleted.
* So no changing blocksize hints once allocation is done. */
if (sih->i_size > 0)
return 0;
return 1;
}
int nova_set_blocksize_hint(struct super_block *sb, struct inode *inode,
struct nova_inode *pi, loff_t new_size)
{
unsigned short block_type;
if (!nova_can_set_blocksize_hint(inode, pi, new_size))
return 0;
if (new_size >= 0x40000000) { /* 1G */
block_type = NOVA_BLOCK_TYPE_1G;
goto hint_set;
}
if (new_size >= 0x200000) { /* 2M */
block_type = NOVA_BLOCK_TYPE_2M;
goto hint_set;
}
/* defaulting to 4K */
block_type = NOVA_BLOCK_TYPE_4K;
hint_set:
nova_dbg_verbose(
"Hint: new_size 0x%llx, i_size 0x%llx\n",
new_size, pi->i_size);
nova_dbg_verbose("Setting the hint to 0x%x\n", block_type);
nova_memunlock_inode(sb, pi);
pi->i_blk_type = block_type;
nova_memlock_inode(sb, pi);
return 0;
}
static loff_t nova_llseek(struct file *file, loff_t offset, int origin)
{
struct inode *inode = file->f_path.dentry->d_inode;
int retval;
if (origin != SEEK_DATA && origin != SEEK_HOLE)
return generic_file_llseek(file, offset, origin);
mutex_lock(&inode->i_mutex);
switch (origin) {
case SEEK_DATA:
retval = nova_find_region(inode, &offset, 0);
if (retval) {
mutex_unlock(&inode->i_mutex);
return retval;
}
break;
case SEEK_HOLE:
retval = nova_find_region(inode, &offset, 1);
if (retval) {
mutex_unlock(&inode->i_mutex);
return retval;
}
break;
}
if ((offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) ||
offset > inode->i_sb->s_maxbytes) {
mutex_unlock(&inode->i_mutex);
return -EINVAL;
}
if (offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
mutex_unlock(&inode->i_mutex);
return offset;
}
#if 0
static inline int nova_check_page_dirty(struct super_block *sb,
unsigned long addr)
{
return IS_MAP_WRITE(addr);
}
static unsigned long nova_get_dirty_range(struct super_block *sb,
struct nova_inode *pi, struct nova_inode_info *si, loff_t *start,
loff_t end)
{
unsigned long flush_bytes = 0;
unsigned long bytes;
unsigned long cache_addr = 0;
pgoff_t pgoff;
loff_t offset;
loff_t dirty_start;
loff_t temp = *start;
nova_dbgv("%s: inode %llu, start %llu, end %llu\n",
__func__, pi->nova_ino, *start, end);
dirty_start = temp;
while (temp < end) {
pgoff = temp >> PAGE_SHIFT;
offset = temp & ~PAGE_MASK;
bytes = sb->s_blocksize - offset;
if (bytes > (end - temp))
bytes = end - temp;
cache_addr = nova_get_cache_addr(sb, si, pgoff);
if (cache_addr && nova_check_page_dirty(sb, cache_addr)) {
if (flush_bytes == 0)
dirty_start = temp;
flush_bytes += bytes;
} else {
if (flush_bytes)
break;
}
temp += bytes;
}
if (flush_bytes == 0)
*start = end;
else
*start = dirty_start;
return flush_bytes;
}
static void nova_get_sync_range(struct nova_inode_info_header *sih,
loff_t *start, loff_t *end)
{
unsigned long start_blk, end_blk;
unsigned long low_blk, high_blk;
start_blk = *start >> PAGE_SHIFT;
end_blk = *end >> PAGE_SHIFT;
low_blk = sih->low_dirty;
high_blk = sih->high_dirty;
if (start_blk < low_blk)
*start = low_blk << PAGE_SHIFT;
if (end_blk > high_blk)
*end = (high_blk + 1) << PAGE_SHIFT;
}
/* This function is called by both msync() and fsync().
* TODO: Check if we can avoid calling nova_flush_buffer() for fsync. We use
* movnti to write data to files, so we may want to avoid doing unnecessary
* nova_flush_buffer() on fsync() */
int nova_fsync(struct file *file, loff_t start, loff_t end, int datasync)
{
/* Sync from start to end[inclusive] */
struct address_space *mapping = file->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;
unsigned long start_blk, end_blk;
u64 end_tail = 0, begin_tail = 0;
u64 begin_temp = 0, end_temp = 0;
int ret = 0;
loff_t sync_start, sync_end;
loff_t isize;
timing_t fsync_time;
NOVA_START_TIMING(fsync_t, fsync_time);
if (!mapping_mapped(mapping))
goto out;
mutex_lock(&inode->i_mutex);
/* Check the dirty range */
pi = nova_get_inode(sb, inode);
end += 1; /* end is inclusive. We like our indices normal please! */
isize = i_size_read(inode);
if ((unsigned long)end > (unsigned long)isize)
end = isize;
if (!isize || (start >= end))
{
nova_dbg_verbose("[%s:%d] : (ERR) isize(%llx), start(%llx),"
" end(%llx)\n", __func__, __LINE__, isize, start, end);
NOVA_END_TIMING(fsync_t, fsync_time);
mutex_unlock(&inode->i_mutex);
return 0;
}
nova_get_sync_range(sih, &start, &end);
start_blk = start >> PAGE_SHIFT;
end_blk = end >> PAGE_SHIFT;
nova_dbgv("%s: start %llu, end %llu, size %llu, "
" start_blk %lu, end_blk %lu\n",
__func__, start, end, isize, start_blk,
end_blk);
sync_start = start;
sync_end = end;
end_temp = pi->log_tail;
do {
unsigned long nr_flush_bytes = 0;
nr_flush_bytes = nova_get_dirty_range(sb, pi, si, &start, end);
nova_dbgv("start %llu, flush bytes %lu\n",
start, nr_flush_bytes);
if (nr_flush_bytes) {
nova_copy_to_nvmm(sb, inode, pi, start,
nr_flush_bytes, &begin_temp, &end_temp);
if (begin_tail == 0)
begin_tail = begin_temp;
}
start += nr_flush_bytes;
} while (start < end);
end_tail = end_temp;
if (begin_tail && end_tail && end_tail != pi->log_tail) {
nova_update_tail(pi, end_tail);
/* Free the overlap blocks after the write is committed */
ret = nova_reassign_file_tree(sb, pi, sih, begin_tail);
inode->i_blocks = le64_to_cpu(pi->i_blocks);
}
mutex_unlock(&inode->i_mutex);
out:
NOVA_END_TIMING(fsync_t, fsync_time);
return ret;
}
#endif
/* This function is called by both msync() and fsync().
* TODO: Check if we can avoid calling nova_flush_buffer() for fsync. We use
* movnti to write data to files, so we may want to avoid doing unnecessary
* nova_flush_buffer() on fsync() */
int nova_fsync(struct file *file, loff_t start, loff_t end, int datasync)
{
/* Sync from start to end[inclusive] */
struct address_space *mapping = file->f_mapping;
struct inode *inode = mapping->host;
struct super_block *sb = inode->i_sb;
struct nova_inode_info *si = NOVA_I(inode);
struct nova_inode_info_header *sih = &si->header;
struct nova_file_write_entry *entry;
int ret = 0;
loff_t isize;
timing_t fsync_time;
NOVA_START_TIMING(fsync_t, fsync_time);
/* No need to flush if the file is not mmaped */
if (!mapping_mapped(mapping))
goto persist;
end += 1; /* end is inclusive. We like our indices normal please! */
isize = i_size_read(inode);
if ((unsigned long)end > (unsigned long)isize)
end = isize;
if (!isize || (start >= end))
{
nova_dbgv("[%s:%d] : (ERR) isize(%llx), start(%llx),"
" end(%llx)\n", __func__, __LINE__, isize, start, end);
NOVA_END_TIMING(fsync_t, fsync_time);
return -ENODATA;
}
/* Align start and end to cacheline boundaries */
start = start & CACHELINE_MASK;
end = CACHELINE_ALIGN(end);
do {
unsigned long nvmm;
unsigned long nr_flush_bytes = 0;
unsigned long avail_bytes = 0;
void *dax_mem;
pgoff_t pgoff;
loff_t offset;
pgoff = start >> PAGE_SHIFT;
offset = start & ~PAGE_MASK;
entry = nova_get_write_entry(sb, si, pgoff);
if (unlikely(entry == NULL)) {
nova_dbgv("Found hole: pgoff %lu, inode size %lld\n",
pgoff, isize);
/* Jump the hole */
entry = nova_find_next_entry(sb, sih, pgoff);
if (!entry)
goto persist;
pgoff = entry->pgoff;
start = pgoff << PAGE_SHIFT;
offset = 0;
if (start >= end)
goto persist;
}
nr_flush_bytes = end - start;
if (pgoff < entry->pgoff ||
pgoff - entry->pgoff >= entry->num_pages) {
nova_err(sb, "%s ERROR: %lu, entry pgoff %llu, num %u, "
"blocknr %llu\n", __func__, pgoff, entry->pgoff,
entry->num_pages, entry->block >> PAGE_SHIFT);
NOVA_END_TIMING(fsync_t, fsync_time);
return -EINVAL;
}
/* Find contiguous blocks */
if (entry->invalid_pages == 0)
avail_bytes = (entry->num_pages - (pgoff - entry->pgoff))
* PAGE_SIZE - offset;
else
avail_bytes = PAGE_SIZE - offset;
if (nr_flush_bytes > avail_bytes)
nr_flush_bytes = avail_bytes;
nvmm = get_nvmm(sb, sih, entry, pgoff);
dax_mem = nova_get_block(sb, (nvmm << PAGE_SHIFT));
nova_dbgv("start %llu, flush bytes %lu\n",
start, nr_flush_bytes);
if (nr_flush_bytes)
nova_flush_buffer(dax_mem + offset, nr_flush_bytes, 0);
start += nr_flush_bytes;
} while (start < end);
persist:
PERSISTENT_BARRIER();
NOVA_END_TIMING(fsync_t, fsync_time);
return ret;
}
/* This callback is called when a file is closed */
static int nova_flush(struct file *file, fl_owner_t id)
{
PERSISTENT_BARRIER();
return 0;
}
static int nova_open(struct inode *inode, struct file *filp)
{
return generic_file_open(inode, filp);
}
const struct file_operations nova_dax_file_operations = {
.llseek = nova_llseek,
.read = nova_dax_file_read,
.write = nova_dax_file_write,
.read_iter = generic_file_read_iter,
.write_iter = generic_file_write_iter,
.mmap = nova_dax_file_mmap,
.open = nova_open,
.fsync = nova_fsync,
.flush = nova_flush,
.unlocked_ioctl = nova_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = nova_compat_ioctl,
#endif
};
const struct inode_operations nova_file_inode_operations = {
.setattr = nova_notify_change,
.getattr = nova_getattr,
.get_acl = NULL,
};