summaryrefslogtreecommitdiff
path: root/fs/fuse/ext_map.c
blob: 7f04edc33fe38c5970d4ae3afb57af3b7e9f8ab3 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0-only

#include "fuse_i.h"
#include <linux/pagemap.h>
#include <linux/iomap.h>
#include <linux/dax.h>
#include <linux/sort.h>
#include <linux/log2.h>
#include <linux/math64.h>

struct fuse_iext {
	u64 start;
	u64 end; /* exclusive */
	struct fuse_backing *backing;
	u64 backing_offset;
};

void fuse_ext_map_destroy(struct fuse_backing *fb)
{
	unsigned int i;

	for (i = 0; i < fb->nr_extents; i++)
		fuse_backing_put(fb->extents[i].backing);

	kvfree(fb->extents);
	fb->extents = NULL;
	fb->nr_extents = 0;
}

/*
 * Resolve @offset to an extent.
 *
 * A uniform map (all extents the same length, contiguous from offset zero) is
 * resolved with a single array index.  A cyclic map is uniform by construction;
 * the index wraps and *ncycle is the number of complete cycles before @offset.
 * Only an irregular map needs a search: a binary search of the sorted array.
 */
static struct fuse_iext *fuse_find_extent(struct fuse_backing *fb, u64 offset,
					  u64 *ncycle)
{
	struct fuse_iext *ext = fb->extents;
	unsigned int lo, hi;
	u64 idx;

	*ncycle = 0;

	/* If the extents are uniform... */
	if (fb->ext_len) {
		idx = div64_u64(offset, fb->ext_len);

		if (fb->cycle_length) {
			u32 rem;

			*ncycle = div_u64_rem(idx, fb->nr_extents, &rem);
			idx = rem;
		} else if (idx >= fb->nr_extents) {
			return NULL;
		}
		return &ext[idx];
	}

	/* Extents are not uniform: binary search the extents array */
	lo = 0;
	hi = fb->nr_extents;
	while (lo < hi) {
		unsigned int mid = lo + (hi - lo) / 2;

		if (offset < ext[mid].start)
			hi = mid;
		else if (offset >= ext[mid].end)
			lo = mid + 1;
		else
			return &ext[mid];
	}

	return NULL;
}

static int fuse_add_extent(struct fuse_conn *fc, struct fuse_iext *new_fie,
			   struct fuse_extent *ext)
{
	loff_t end;
	u64 tmp;

	if (ext->reserved[0] || ext->reserved[1])
		return fuse_EIO("reserved fields set");

	if (!PAGE_ALIGNED(ext->offset) || !PAGE_ALIGNED(ext->length) || !PAGE_ALIGNED(ext->addr))
		return fuse_EIO("not page aligned");

	if (!ext->length)
		return fuse_EIO("zero sized extent");

	if (overflows_type(ext->offset, loff_t) ||
	    check_add_overflow(ext->offset, ext->length, &end) ||
	    check_add_overflow(ext->addr, ext->length, &tmp))
		return fuse_EIO("offset overflow");

	new_fie->backing = fuse_backing_lookup(fc, ext->backing_id);
	if (!new_fie->backing)
		return fuse_EIO("backing not found");

	if (new_fie->backing->type != FUSE_BACKING_DAXDEV) {
		fuse_backing_put(new_fie->backing);
		new_fie->backing = NULL;
		return fuse_EIO("backing is not dax device");
	}

	new_fie->start = ext->offset;
	new_fie->end = end;
	new_fie->backing_offset = ext->addr;

	return 0;
}

static int fuse_iext_cmp(const void *a, const void *b)
{
	const struct fuse_iext *x = a, *y = b;

	if (x->start < y->start)
		return -1;
	return x->start > y->start;
}

/*
 * fb has an array of extents;
 * - Sort it to guarantee they are in file offset order
 * - Check whether it is uniform (starts at ofs=0, all same size, no gaps)
 * - Validate if cyclic
 *
 * Array-based extent lists have better cache locality than a tree
 * (packed into cache lines). Uniform simple extent lists and all currently
 * valid cyclic extent lists can be indexed in O(1) at fault time. And
 * when search is needed, arrays can be bisected for balanced O(log2 n).
 */
static int fuse_ext_map_finish(struct fuse_backing *fb, bool cyclic)
{
	struct fuse_iext *ext = fb->extents;
	unsigned int n = fb->nr_extents;
	unsigned int i;
	bool uniform;
	u64 len;

	sort(ext, n, sizeof(*ext), fuse_iext_cmp, NULL);

	len = ext[0].end - ext[0].start;
	uniform = ext[0].start == 0;

	/* Check for uniformity */
	for (i = 1; i < n; i++) {
		if (ext[i].start < ext[i - 1].end)
			return fuse_EIO("overlap");

		if (ext[i].start != ext[i - 1].end ||
		    ext[i].end - ext[i].start != len)
			uniform = false;
	}

	if (cyclic) {
		if (!uniform ||
		    check_mul_overflow(len, (u64)n, &fb->cycle_length))
			return -EINVAL;
	}

	if (uniform)
		fb->ext_len = len;

	return 0;
}

static int fuse_ext_map_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
				    unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
{
	struct fuse_backing *fb = fuse_inode_backing(get_fuse_inode(inode));
	struct fuse_iext *fie;
	u64 ncycle, seq_off, addr, tmp;
	loff_t ext_len;

	if (!fb || fb->type != FUSE_BACKING_EXTMAP)
		return fuse_EIO("missing or wrong type backing");

	fie = fuse_find_extent(fb, offset, &ncycle);
	if (!fie)
		return fuse_EIO("missing mapping");

	if (WARN_ON(fie->backing->type != FUSE_BACKING_DAXDEV))
		return fuse_EIO("wrong type backing for extent");

	if (fie->backing->dax_error) {
		fuse_make_bad(inode);
		return fuse_EIO("dax error");
	}

	ext_len = fie->end - fie->start;
	seq_off = ncycle * fb->cycle_length;
	addr = fie->backing_offset;
	if (check_mul_overflow(ncycle, ext_len, &tmp) || check_add_overflow(addr, tmp, &addr))
		return fuse_EIO("extent address overflow");

	if (check_add_overflow(addr, ext_len, &tmp))
		return fuse_EIO("extent end overflow");

	iomap->offset = fie->start + seq_off;
	iomap->addr = addr;
	iomap->length = ext_len;
	iomap->dax_dev = fie->backing->dax_dev;
	iomap->type = IOMAP_MAPPED;
	iomap->flags = 0;

	return 0;
}

static const struct iomap_ops fuse_ext_map_iomap_ops = {
	.iomap_begin		= fuse_ext_map_iomap_begin,
};

static vm_fault_t fuse_ext_map_huge_fault(struct vm_fault *vmf, unsigned int order)
{
	struct inode *inode = file_inode(vmf->vma->vm_file);
	bool write_fault = (vmf->flags & FAULT_FLAG_WRITE) && (vmf->vma->vm_flags & VM_SHARED);
	vm_fault_t ret;
	unsigned long pfn;

	if (!IS_ENABLED(CONFIG_FS_DAX))
		return VM_FAULT_SIGBUS;

	if (WARN_ON_ONCE(!IS_DAX(inode)))
		return VM_FAULT_SIGBUS;

	if (write_fault) {
		sb_start_pagefault(inode->i_sb);
		file_update_time(vmf->vma->vm_file);
	}

	filemap_invalidate_lock_shared(inode->i_mapping);

	ret = dax_iomap_fault(vmf, order, &pfn, NULL, &fuse_ext_map_iomap_ops);
	if (ret & VM_FAULT_NEEDDSYNC)
		ret = dax_finish_sync_fault(vmf, order, pfn);

	filemap_invalidate_unlock_shared(inode->i_mapping);

	if (write_fault)
		sb_end_pagefault(inode->i_sb);

	return ret;
}

static vm_fault_t fuse_ext_map_fault(struct vm_fault *vmf)
{
	return fuse_ext_map_huge_fault(vmf, 0);
}

static const struct vm_operations_struct fuse_ext_map_vm_ops = {
	.fault		= fuse_ext_map_fault,
	.huge_fault	= fuse_ext_map_huge_fault,
	.page_mkwrite	= fuse_ext_map_fault,
	.pfn_mkwrite	= fuse_ext_map_fault,
};

static void fuse_rw_clamp(struct kiocb *iocb, struct iov_iter *ubuf)
{
	struct inode *inode = iocb->ki_filp->f_mapping->host;
	loff_t i_size = i_size_read(inode);
	loff_t max_count = iocb->ki_pos >= i_size ? 0 : i_size - iocb->ki_pos;

	if (iov_iter_count(ubuf) > max_count)
		iov_iter_truncate(ubuf, max_count);
}

ssize_t fuse_ext_map_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
	struct inode *inode = iocb->ki_filp->f_mapping->host;
	ssize_t res;

	guard(rwsem_read)(&inode->i_rwsem);

	fuse_rw_clamp(iocb, to);

	if (!iov_iter_count(to))
		return 0;

	if (!IS_ENABLED(CONFIG_FS_DAX))
		return -EIO;

	res = dax_iomap_rw(iocb, to, &fuse_ext_map_iomap_ops);

	file_accessed(iocb->ki_filp);
	return res;
}

ssize_t fuse_ext_map_write_iter(struct kiocb *iocb, struct iov_iter *from)
{
	ssize_t res;

	res = generic_write_checks(iocb, from);
	if (res <= 0)
		return res;

	fuse_rw_clamp(iocb, from);

	if (!iov_iter_count(from))
		return -ENOSPC;

	res = kiocb_modified(iocb);
	if (res)
		return res;

	if (!IS_ENABLED(CONFIG_FS_DAX))
		return -EIO;

	return dax_iomap_rw(iocb, from, &fuse_ext_map_iomap_ops);
}

int fuse_ext_map_mmap(struct file *file, struct vm_area_struct *vma)
{
	file_accessed(file);
	vma->vm_ops = &fuse_ext_map_vm_ops;
	vm_flags_set(vma, VM_HUGEPAGE);
	return 0;
}

bool fuse_ext_map_is_dax(struct fuse_backing *fb)
{
	if (WARN_ON(fb->type != FUSE_BACKING_EXTMAP || !fb->nr_extents))
		return false;

	return fb->extents[0].backing->type == FUSE_BACKING_DAXDEV;
}

int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg,
			  struct fuse_extent *ext)
{
	struct fuse_backing *fb;
	unsigned int i;
	int err;

	if (!arg->num_extents)
		return fuse_EIO("no extents");

	/* Adding extents to an existing extmap backing is not yet supported */
	if (!(arg->flags & FUSE_BACKING_MAP_CREATE))
		return -EINVAL;

	fb = kzalloc_obj(*fb);
	if (!fb)
		return -ENOMEM;

	refcount_set(&fb->count, 1);
	fb->type = FUSE_BACKING_EXTMAP;
	fb->backing_id = arg->backing_id;

	err = -ENOMEM;
	fb->extents = kvcalloc(arg->num_extents, sizeof(*fb->extents), GFP_KERNEL);
	if (!fb->extents)
		goto err_put;

	for (i = 0; i < arg->num_extents; i++) {
		err = fuse_add_extent(fc, &fb->extents[i], &ext[i]);
		if (err)
			goto err_put;
		fb->nr_extents++;
	}

	err = fuse_ext_map_finish(fb, arg->flags & FUSE_BACKING_MAP_CYCLIC);
	if (err)
		goto err_put;

	err = fuse_backing_add_64(fc, fb);
	if (!err)
		return 0;

err_put:
	fuse_backing_put(fb);
	return err;
}