-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.c
612 lines (496 loc) · 14 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
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
#include <linux/fs.h>
#include <linux/dax.h>
#include <linux/iomap.h>
#include <linux/uio.h>
#include <linux/buffer_head.h>
#include <linux/blkdev.h>
#include "aeon.h"
#include "aeon_balloc.h"
#include "aeon_extents.h"
#include "aeon_compression.h"
int compression = 0;
static loff_t aeon_llseek(struct file *file, loff_t offset, int origin)
{
struct inode *inode = file->f_path.dentry->d_inode;
loff_t maxbytes = inode->i_sb->s_maxbytes;
if (origin != SEEK_DATA && origin != SEEK_HOLE)
return generic_file_llseek(file, offset, origin);
switch (origin) {
case SEEK_DATA:
aeon_dbgv("DATA\n");
inode_lock_shared(inode);
offset = iomap_seek_data(inode, offset, &aeon_iomap_ops);
inode_unlock_shared(inode);
break;
case SEEK_HOLE:
aeon_dbgv("SEEK\n");
inode_lock_shared(inode);
offset = iomap_seek_hole(inode, offset, &aeon_iomap_ops);
inode_unlock_shared(inode);
break;
}
if (offset < 0)
return offset;
return vfs_setpos(file, offset, maxbytes);
}
#ifdef CONFIG_AEON_FS_AEON_RW
static ssize_t do_dax_mapping_read(struct inode *inode, char __user *buf,
size_t len, loff_t *ppos)
{
struct super_block *sb = inode->i_sb;
struct aeon_inode_info_header *sih = &AEON_I(inode)->header;
ssize_t offset;
ssize_t index;
ssize_t end_index;
ssize_t copied = 0;
loff_t pos;
loff_t isize;
ssize_t err = 0;
aeon_dbgv("---READ---\n");
aeon_dbgv("INO: %lu\n", inode->i_ino);
pos = *ppos;
index = pos >> PAGE_SHIFT;
offset = pos & ~PAGE_MASK;
isize = i_size_read(inode);
if (isize == 0)
goto out;
aeon_dbgv("len %lu \n", len);
aeon_dbgv("isize %lld \n", isize);
aeon_dbgv("pos %lld \n", pos);
if (len > isize - pos)
len = isize - pos;
if (len <= 0)
goto out;
end_index = (isize - 1) >> PAGE_SHIFT;
aeon_dbgv("start %lu \n", index);
aeon_dbgv("end %lu \n", end_index);
do {
struct aeon_extent *ae;
unsigned long nr;
unsigned long left;
unsigned long blocknr;
unsigned long ex_offset;
ssize_t copying;
void *nvmm;
int pages;
if (index >= end_index) {
if (index > end_index)
goto out;
nr = ((isize - 1) & ~PAGE_MASK) + 1;
if (nr <= offset)
goto out;
}
aeon_dbgv("---PREPARE \n");
aeon_dbgv("isize %lu \n", index);
aeon_dbgv("end %lu \n", end_index);
ae = aeon_search_extent(sb, sih, index);
if (!ae) {
aeon_err(sb, "can't find an extent: %u\n", index);
goto out;
}
ex_offset = le32_to_cpu(ae->ex_offset);
blocknr = le64_to_cpu(ae->ex_block) + (index - ex_offset);
pages = le16_to_cpu(ae->ex_length) - (index-ex_offset);
nvmm = aeon_get_address(sb, blocknr<<AEON_SHIFT, 0);
aeon_dbgv("block %lu\n", blocknr);
aeon_dbgv("exoff %lu\n", ex_offset);
aeon_dbgv("lengt %d\n", le16_to_cpu(ae->ex_length));
copying = pages << PAGE_SHIFT;
if (len < copying + copied)
nr = len - copied;
else
nr = copying;
left = copy_to_user(buf+copied, nvmm+offset, nr);
copied += (nr - left);
offset += (nr - left);
index += (offset >> AEON_SHIFT);
offset &= ~PAGE_MASK;
aeon_dbgv("---READ \n");
aeon_dbgv("le %lu\n", len);
aeon_dbgv("left %lu\n", left);
aeon_dbgv("copied %lu\n", copied);
aeon_dbgv("offset %ld\n", offset);
} while (copied < len);
out:
*ppos = pos + copied;
aeon_dbgv("copied return %lu\n", copied);
return copied ? copied : err;
}
static ssize_t aeon_read(struct file *filp, char __user *buf,
size_t len, loff_t *ppos)
{
struct inode *inode = filp->f_mapping->host;
ssize_t ret;
inode_lock_shared(inode);
#ifdef CONFIG_AEON_FS_COMPRESSION
if (compression)
ret = do_dax_decompress_read(inode, buf, len, ppos);
else
ret = do_dax_mapping_read(inode, buf, len, ppos);
#else
ret = do_dax_mapping_read(inode, buf, len, ppos);
#endif
if (filp)
file_accessed(filp);
inode_unlock_shared(inode);
return ret;
}
static int do_dax_get_new_blocks(struct super_block *sb,
struct inode *inode,
struct aeon_inode_info_header *sih,
unsigned long iblock,
unsigned long num_blocks,
unsigned long *ret_blocknr)
{
struct aeon_inode *pi = aeon_get_inode(sb, sih);
struct aeon_extent_header *aeh;
unsigned long blocknr = 0;
int allocated;
int err;
aeh = aeon_get_extent_header(pi);
aeon_init_file(pi, aeh);
allocated = aeon_new_data_blocks(sb, sih, &blocknr,
iblock, num_blocks, ANY_CPU);
if (allocated <= 0) {
aeon_err(sb, "%s\n", __func__);
return -ENOSPC;
}
*ret_blocknr = blocknr;
err = aeon_update_extent(sb, inode, blocknr, iblock, allocated);
if (err) {
aeon_err(sb, "failed to update extent\n");
goto out;
}
clean_bdev_aliases(sb->s_bdev, blocknr, allocated);
err = sb_issue_zeroout(sb, blocknr, allocated, GFP_NOFS);
if (err) {
aeon_err(sb, "%s: sb_issue_zero_out\n", __func__);
goto out;
}
return allocated;
out:
return err;
}
static ssize_t aeon_dax_write(struct file *filp, const char __user *buf,
size_t len, loff_t *ppos)
{
struct inode *inode = filp->f_mapping->host;
struct super_block *sb = inode->i_sb;
struct aeon_inode_info_header *sih = &AEON_I(inode)->header;
struct aeon_inode *pi = aeon_get_inode(sb, sih);
struct aeon_extent *ae;
unsigned long blocknr;
unsigned long num_blocks; /* The num of blocks used for the file data */
unsigned long iblock; /* file offset in block */
unsigned long offset; /* file offset in bytes */
unsigned long copied;
unsigned long status;
ssize_t ret = -EFAULT;
ssize_t written = 0;
ssize_t count; /* entire written bytes to the file */
ssize_t space;
ssize_t bytes;
int allocated;
loff_t pos;
void *nvmm;
u64 addr_off;
if (!access_ok(VERIFY_READ, buf, len))
return -EFAULT;
ret = file_remove_privs(filp);
if (ret)
goto out;
pos = *ppos;
if (filp->f_flags & O_APPEND)
pos = i_size_read(inode);
inode->i_ctime = inode->i_mtime = current_time(inode);
count = len;
iblock = pos >> AEON_SHIFT;
offset = pos;
aeon_dbgv("---WRITE---\n");
aeon_dbgv("INO: %lu\n", inode->i_ino);
aeon_dbgv("len %lu\n", len);
aeon_dbgv("pos %llu\n", pos);
aeon_dbgv("iblock %lu\n", iblock);
aeon_dbgv("offset %lu\n", offset);
while (count > 0) {
aeon_dbgv("-START-\n");
num_blocks = ((count - 1) >> AEON_SHIFT) + 1;
ae = aeon_search_extent(sb, sih, iblock);
if (!ae) {
aeon_dbgv("Allocate new blocks\n");
allocated = do_dax_get_new_blocks(sb, inode, sih, iblock,
num_blocks, &blocknr);
if (allocated <= 0) {
ret = -ENOSPC;
goto out;
}
offset = pos & ~PAGE_MASK;
space = sb->s_blocksize * allocated - offset;
} else {
aeon_dbgv("Use remaining space\n");
blocknr = le64_to_cpu(ae->ex_block);
allocated = le32_to_cpu(ae->ex_length);
aeon_dbgv("eoffset %lu->\n", offset);
offset -= sb->s_blocksize * le32_to_cpu(ae->ex_offset);
aeon_dbgv("offset %u\n", le32_to_cpu(ae->ex_offset));
space = sb->s_blocksize * allocated - offset;
}
aeon_dbgv("allocate %d\n", allocated);
aeon_dbgv("blocknr %lu\n", blocknr);
aeon_dbgv("pos %llu\n", pos);
aeon_dbgv("count %lu\n", count);
aeon_dbgv("offset %lu\n", offset);
aeon_dbgv("space %lu\n", space);
bytes = sb->s_blocksize * allocated - offset;
aeon_dbgv("bytes %lu\n", bytes);
if (bytes > count)
bytes = count;
if (bytes > space)
bytes = space;
addr_off = blocknr << AEON_SHIFT;
nvmm = aeon_get_address(sb, addr_off, 0);
aeon_dbgv("nvmm 0x%llx\n", (u64)nvmm);
aeon_dbgv("offset %lu\n", offset);
aeon_dbgv("bytes %lu\n", bytes);
aeon_dbgv("COPY\n");
copied = bytes - copy_from_user(nvmm+offset, buf, bytes);
if (copied > 0) {
status = copied;
written += copied;
pos += copied;
buf += copied;
count -= copied;
iblock = pos >> AEON_SHIFT;
}
aeon_dbgv("copied %lu\n", copied);
aeon_dbgv("offset %lu\n", offset);
aeon_dbgv("bytes %lu\n", bytes);
aeon_dbgv("pos %llu\n", pos);
aeon_dbgv("count %lu\n", count);
if (unlikely(copied != bytes)) {
aeon_err(sb, "%s ERROR!: %p, bytes %lu, copied %lu\n",
__func__, nvmm, bytes, copied);
if (status >= 0)
status = -EFAULT;
}
if (status < 0)
break;
}
aeon_dbgv("NOW %llu\n", pos >> AEON_SHIFT);
*ppos = pos;
if (pos > inode->i_size) {
i_size_write(inode, pos);
pi->i_size = cpu_to_le64(pos);
}
ret = written;
out:
return ret;
}
static ssize_t do_aeon_write(struct file *filp, const char __user *buf,
size_t len, loff_t *ppos)
{
#ifdef CONFIG_AEON_FS_COMPRESSION
if (compression)
return aeon_compress_write(filp, buf, len, ppos);
#endif
return aeon_dax_write(filp, buf, len, ppos);
}
static ssize_t aeon_write(struct file *filp, const char __user *buf,
size_t len, loff_t *ppos)
{
struct inode *inode = filp->f_mapping->host;
ssize_t ret;
if (len == 0)
return 0;
sb_start_write(inode->i_sb);
inode_lock(inode);
ret = do_aeon_write(filp, buf, len, ppos);
inode_unlock(inode);
sb_end_write(inode->i_sb);
return ret;
}
#endif
static inline void wrap_file_accessed(struct file *fp)
{
if (!(fp->f_flags & O_NOATIME)) {
struct aeon_inode *pi;
struct inode *inode = fp->f_path.dentry->d_inode;
pi = aeon_get_inode(inode->i_sb, &AEON_I(inode)->header);
touch_atime(&fp->f_path);
pi->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
}
}
static ssize_t aeon_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
struct inode *inode = iocb->ki_filp->f_mapping->host;
ssize_t ret;
if(!iov_iter_count(to))
return 0;
inode_lock_shared(inode);
ret = dax_iomap_rw(iocb, to, &aeon_iomap_ops);
inode_unlock_shared(inode);
wrap_file_accessed(iocb->ki_filp);
return ret;
}
static inline void wrap_i_size_write(struct inode *inode, struct kiocb *iocb)
{
struct aeon_inode *pi;
pi = aeon_get_inode(inode->i_sb, &AEON_I(inode)->header);
i_size_write(inode, iocb->ki_pos);
pi->i_size = cpu_to_le64(inode->i_size);
}
static ssize_t aeon_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
struct file *file = iocb->ki_filp;
struct inode *inode = file->f_mapping->host;
ssize_t ret;
inode_lock(inode);
ret = generic_write_checks(iocb, from);
if (ret <= 0)
goto out_unlock;
ret = file_remove_privs(file);
if (ret)
goto out_unlock;
ret = file_update_time(file);
if (ret)
goto out_unlock;
ret = dax_iomap_rw(iocb, from, &aeon_iomap_ops);
if (ret > 0 && iocb->ki_pos > i_size_read(inode))
wrap_i_size_write(inode, iocb);
out_unlock:
inode_unlock(inode);
if (ret > 0)
ret = generic_write_sync(iocb, ret);
return ret;
}
static vm_fault_t aeon_dax_huge_fault(struct vm_fault *vmf,
enum page_entry_size pe_size)
{
struct inode *inode = file_inode(vmf->vma->vm_file);
struct super_block *sb = inode->i_sb;
struct aeon_inode_info *si = AEON_I(inode);
struct aeon_inode_info_header *sih = &si->header;
bool write;
vm_fault_t ret;
write = (vmf->flags & FAULT_FLAG_WRITE);
if (write) {
sb_start_pagefault(sb);
file_update_time(vmf->vma->vm_file);
}
down_read(&sih->dax_sem);
ret = dax_iomap_fault(vmf, pe_size, NULL, NULL, &aeon_iomap_ops);
up_read(&sih->dax_sem);
if (write)
sb_end_pagefault(sb);
return ret;
}
static vm_fault_t aeon_dax_fault(struct vm_fault *vmf)
{
return aeon_dax_huge_fault(vmf, PE_SIZE_PTE);
}
static const struct vm_operations_struct aeon_dax_vm_ops = {
.fault = aeon_dax_fault,
//.huge_fault = aeon_dax_huge_fault,
.page_mkwrite = aeon_dax_fault,
.pfn_mkwrite = aeon_dax_fault,
};
static int aeon_mmap(struct file *file, struct vm_area_struct *vma)
{
file_accessed(file);
vma->vm_ops = &aeon_dax_vm_ops;
vma->vm_flags |= VM_MIXEDMAP;
return 0;
}
static int aeon_fsync(struct file *file, loff_t start, loff_t end, int datasync)
{
return generic_file_fsync(file, start, end, datasync);
}
static int aeon_open(struct inode *inode, struct file *file)
{
return generic_file_open(inode, file);
}
const struct file_operations aeon_dax_file_operations = {
.llseek = aeon_llseek,
.read_iter = aeon_file_read_iter,
.write_iter = aeon_file_write_iter,
.mmap = aeon_mmap,
.fsync = aeon_fsync,
.open = aeon_open,
.unlocked_ioctl = aeon_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = aeon_compat_ioctl,
#endif
};
const struct file_operations aeon_compress_file_operations = {
.llseek = aeon_llseek,
#ifdef CONFIG_AEON_FS_AEON_RW
.read = aeon_read,
.write = aeon_write,
#endif
.read_iter = aeon_file_read_iter,
.write_iter = aeon_file_write_iter,
.mmap = aeon_mmap,
.fsync = aeon_fsync,
.open = aeon_open,
.unlocked_ioctl = aeon_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = aeon_compat_ioctl,
#endif
};
const struct inode_operations aeon_file_inode_operations = {
.setattr = aeon_setattr,
.update_time = aeon_update_time,
};
static int aeon_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
unsigned flags, struct iomap *iomap)
{
struct aeon_sb_info *sbi = AEON_SB(inode->i_sb);
unsigned int blkbits = inode->i_blkbits;
unsigned long first_block = offset >> blkbits;
unsigned long max_blocks = (length + (1 << blkbits) - 1) >> blkbits;
bool new = false, boundary = false;
u32 bno = 0;
int ret;
ret = aeon_dax_get_blocks(inode, first_block, max_blocks,
&bno, &new, &boundary, flags & IOMAP_WRITE);
if (ret < 0)
return ret;
iomap->flags = 0;
iomap->bdev = inode->i_sb->s_bdev;
iomap->offset = (u64)first_block << blkbits;
iomap->dax_dev = sbi->s_dax_dev;
if (ret == 0) {
iomap->type = IOMAP_HOLE;
iomap->addr = IOMAP_NULL_ADDR;
iomap->length = 1 << blkbits;
} else {
iomap->type = IOMAP_MAPPED;
iomap->addr = (u64)bno << blkbits;
iomap->length = (u64)ret << blkbits;
iomap->flags |= IOMAP_F_MERGED;
}
if (new)
iomap->flags |= IOMAP_F_NEW;
return 0;
}
static void aeon_write_failed(struct address_space *mapping, loff_t to)
{
struct inode *inode = mapping->host;
if (to > inode->i_size) {
truncate_pagecache(inode, inode->i_size);
aeon_truncate_blocks(inode, inode->i_size);
}
}
static int aeon_iomap_end(struct inode *inode, loff_t offset, loff_t length,
ssize_t written, unsigned flags, struct iomap *iomap)
{
if (iomap->type == IOMAP_MAPPED &&
written < length && (flags & IOMAP_WRITE)) {
aeon_write_failed(inode->i_mapping, offset + length);
}
return 0;
}
const struct iomap_ops aeon_iomap_ops = {
.iomap_begin = aeon_iomap_begin,
.iomap_end = aeon_iomap_end,
};