-
Notifications
You must be signed in to change notification settings - Fork 25
/
file.c
345 lines (297 loc) · 8.68 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
/*
* BRIEF DESCRIPTION
*
* File operations for files.
*
* Copyright 2012-2013 Intel Corporation
* Copyright 2009-2011 Marco Stornelli <marco.stornelli@gmail.com>
* 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/fs.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/uio.h>
#include <linux/mm.h>
#include <linux/uaccess.h>
#include <linux/falloc.h>
#include <asm/mman.h>
#include "pmfs.h"
#include "xip.h"
static inline int pmfs_can_set_blocksize_hint(struct pmfs_inode *pi,
loff_t new_size)
{
/* Currently, we don't deallocate data blocks till the file is deleted.
* So no changing blocksize hints once allocation is done. */
if (le64_to_cpu(pi->root))
return 0;
return 1;
}
int pmfs_set_blocksize_hint(struct super_block *sb, struct pmfs_inode *pi,
loff_t new_size)
{
unsigned short block_type;
if (!pmfs_can_set_blocksize_hint(pi, new_size))
return 0;
if (new_size >= 0x40000000) { /* 1G */
block_type = PMFS_BLOCK_TYPE_1G;
goto hint_set;
}
if (new_size >= 0x200000) { /* 2M */
block_type = PMFS_BLOCK_TYPE_2M;
goto hint_set;
}
/* defaulting to 4K */
block_type = PMFS_BLOCK_TYPE_4K;
hint_set:
pmfs_dbg_verbose(
"Hint: new_size 0x%llx, i_size 0x%llx, root 0x%llx\n",
new_size, pi->i_size, le64_to_cpu(pi->root));
pmfs_dbg_verbose("Setting the hint to 0x%x\n", block_type);
pmfs_memunlock_inode(sb, pi);
pi->i_blk_type = block_type;
pmfs_memlock_inode(sb, pi);
return 0;
}
static long pmfs_fallocate(struct file *file, int mode, loff_t offset,
loff_t len)
{
struct inode *inode = file->f_path.dentry->d_inode;
struct super_block *sb = inode->i_sb;
long ret = 0;
unsigned long blocknr, blockoff;
int num_blocks, blocksize_mask;
struct pmfs_inode *pi;
pmfs_transaction_t *trans;
loff_t new_size;
/* We only support the FALLOC_FL_KEEP_SIZE mode */
if (mode & ~FALLOC_FL_KEEP_SIZE)
return -EOPNOTSUPP;
if (S_ISDIR(inode->i_mode))
return -ENODEV;
inode_lock(inode);
new_size = len + offset;
if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size > inode->i_size) {
ret = inode_newsize_ok(inode, new_size);
if (ret)
goto out;
}
pi = pmfs_get_inode(sb, inode->i_ino);
if (!pi) {
ret = -EACCES;
goto out;
}
trans = pmfs_new_transaction(sb, MAX_INODE_LENTRIES +
MAX_METABLOCK_LENTRIES);
if (IS_ERR(trans)) {
ret = PTR_ERR(trans);
goto out;
}
pmfs_add_logentry(sb, trans, pi, MAX_DATA_PER_LENTRY, LE_DATA);
/* Set the block size hint */
pmfs_set_blocksize_hint(sb, pi, new_size);
blocksize_mask = sb->s_blocksize - 1;
blocknr = offset >> sb->s_blocksize_bits;
blockoff = offset & blocksize_mask;
num_blocks = (blockoff + len + blocksize_mask) >> sb->s_blocksize_bits;
ret = pmfs_alloc_blocks(trans, inode, blocknr, num_blocks, true);
inode->i_mtime = inode->i_ctime = current_time(inode);
pmfs_memunlock_inode(sb, pi);
if (ret || (mode & FALLOC_FL_KEEP_SIZE)) {
pi->i_flags |= cpu_to_le32(PMFS_EOFBLOCKS_FL);
}
if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size > inode->i_size) {
inode->i_size = new_size;
pi->i_size = cpu_to_le64(inode->i_size);
}
pi->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
pi->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
pmfs_memlock_inode(sb, pi);
pmfs_commit_transaction(sb, trans);
out:
inode_unlock(inode);
return ret;
}
static loff_t pmfs_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);
inode_lock(inode);
switch (origin) {
case SEEK_DATA:
retval = pmfs_find_region(inode, &offset, 0);
if (retval) {
inode_unlock(inode);
return retval;
}
break;
case SEEK_HOLE:
retval = pmfs_find_region(inode, &offset, 1);
if (retval) {
inode_unlock(inode);
return retval;
}
break;
}
if ((offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) ||
offset > inode->i_sb->s_maxbytes) {
inode_unlock(inode);
return -EINVAL;
}
if (offset != file->f_pos) {
file->f_pos = offset;
file->f_version = 0;
}
inode_unlock(inode);
return offset;
}
/* This function is called by both msync() and fsync().
* TODO: Check if we can avoid calling pmfs_flush_buffer() for fsync. We use
* movnti to write data to files, so we may want to avoid doing unnecessary
* pmfs_flush_buffer() on fsync() */
int pmfs_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;
loff_t isize;
timing_t fsync_time;
PMFS_START_TIMING(fsync_t, fsync_time);
/* if the file is not mmap'ed, there is no need to do clflushes */
if (mapping_mapped(mapping) == 0)
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))
{
pmfs_dbg_verbose("[%s:%d] : (ERR) isize(%llx), start(%llx),"
" end(%llx)\n", __func__, __LINE__, isize, start, end);
PMFS_END_TIMING(fsync_t, fsync_time);
return -ENODATA;
}
/* Align start and end to cacheline boundaries */
start = start & CACHELINE_MASK;
end = CACHELINE_ALIGN(end);
do {
sector_t block = 0;
void *xip_mem;
pgoff_t pgoff;
loff_t offset;
unsigned long nr_flush_bytes;
pgoff = start >> PAGE_SHIFT;
offset = start & ~PAGE_MASK;
nr_flush_bytes = PAGE_SIZE - offset;
if (nr_flush_bytes > (end - start))
nr_flush_bytes = end - start;
block = pmfs_find_data_block(inode, (sector_t)pgoff);
if (block) {
xip_mem = pmfs_get_block(inode->i_sb, block);
/* flush the range */
atomic64_inc(&fsync_pages);
pmfs_flush_buffer(xip_mem + offset, nr_flush_bytes, 0);
} else {
/* sparse files could have such holes */
pmfs_dbg_verbose("[%s:%d] : start(%llx), end(%llx),"
" pgoff(%lx)\n", __func__, __LINE__, start, end, pgoff);
break;
}
start += nr_flush_bytes;
} while (start < end);
persist:
PERSISTENT_MARK();
PERSISTENT_BARRIER();
PMFS_END_TIMING(fsync_t, fsync_time);
return 0;
}
/* This callback is called when a file is closed */
static int pmfs_flush(struct file *file, fl_owner_t id)
{
int ret = 0;
/* if the file was opened for writing, make it persistent.
* TODO: Should we be more smart to check if the file was modified? */
if (file->f_mode & FMODE_WRITE) {
PERSISTENT_MARK();
PERSISTENT_BARRIER();
}
return ret;
}
#if 0
static unsigned long
pmfs_get_unmapped_area(struct file *file, unsigned long addr,
unsigned long len, unsigned long pgoff,
unsigned long flags)
{
unsigned long align_size;
struct vm_area_struct *vma;
struct mm_struct *mm = current->mm;
struct inode *inode = file->f_mapping->host;
struct pmfs_inode *pi = pmfs_get_inode(inode->i_sb, inode->i_ino);
struct vm_unmapped_area_info info;
if (len > TASK_SIZE)
return -ENOMEM;
if (pi->i_blk_type == PMFS_BLOCK_TYPE_1G)
align_size = PUD_SIZE;
else if (pi->i_blk_type == PMFS_BLOCK_TYPE_2M)
align_size = PMD_SIZE;
else
align_size = PAGE_SIZE;
if (flags & MAP_FIXED) {
/* FIXME: We could use 4K mappings as fallback. */
if (len & (align_size - 1))
return -EINVAL;
if (addr & (align_size - 1))
return -EINVAL;
return addr;
}
if (addr) {
addr = ALIGN(addr, align_size);
vma = find_vma(mm, addr);
if (TASK_SIZE - len >= addr &&
(!vma || addr + len <= vma->vm_start))
return addr;
}
/*
* FIXME: Using the following values for low_limit and high_limit
* implicitly disables ASLR. Awaiting a better way to have this fixed.
*/
info.flags = 0;
info.length = len;
info.low_limit = TASK_UNMAPPED_BASE;
info.high_limit = TASK_SIZE;
info.align_mask = align_size - 1;
info.align_offset = 0;
return vm_unmapped_area(&info);
}
#endif
const struct file_operations pmfs_xip_file_operations = {
.llseek = pmfs_llseek,
.read = pmfs_xip_file_read,
.write = pmfs_xip_file_write,
// .aio_read = xip_file_aio_read,
// .aio_write = xip_file_aio_write,
// .read_iter = generic_file_read_iter,
// .write_iter = generic_file_write_iter,
.mmap = pmfs_xip_file_mmap,
.open = generic_file_open,
.fsync = pmfs_fsync,
.flush = pmfs_flush,
// .get_unmapped_area = pmfs_get_unmapped_area,
.unlocked_ioctl = pmfs_ioctl,
.fallocate = pmfs_fallocate,
#ifdef CONFIG_COMPAT
.compat_ioctl = pmfs_compat_ioctl,
#endif
};
const struct inode_operations pmfs_file_inode_operations = {
.setattr = pmfs_notify_change,
.getattr = pmfs_getattr,
.get_acl = NULL,
};