xref: /linux/fs/fuse/ext_map.c (revision 9e8fc16dc6b474b9a87843c3d3ee12fc83f2bdcc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "fuse_i.h"
4 #include <linux/pagemap.h>
5 #include <linux/iomap.h>
6 #include <linux/dax.h>
7 #include <linux/sort.h>
8 #include <linux/log2.h>
9 #include <linux/math64.h>
10 
11 struct fuse_iext {
12 	u64 start;
13 	u64 end; /* exclusive */
14 	struct fuse_backing *backing;
15 	u64 backing_offset;
16 };
17 
18 void fuse_ext_map_destroy(struct fuse_backing *fb)
19 {
20 	unsigned int i;
21 
22 	for (i = 0; i < fb->nr_extents; i++)
23 		fuse_backing_put(fb->extents[i].backing);
24 
25 	kvfree(fb->extents);
26 	fb->extents = NULL;
27 	fb->nr_extents = 0;
28 }
29 
30 /*
31  * Resolve @offset to an extent.
32  *
33  * A uniform map (all extents the same length, contiguous from offset zero) is
34  * resolved with a single array index.  A cyclic map is uniform by construction;
35  * the index wraps and *ncycle is the number of complete cycles before @offset.
36  * Only an irregular map needs a search: a binary search of the sorted array.
37  */
38 static struct fuse_iext *fuse_find_extent(struct fuse_backing *fb, u64 offset,
39 					  u64 *ncycle)
40 {
41 	struct fuse_iext *ext = fb->extents;
42 	unsigned int lo, hi;
43 	u64 idx;
44 
45 	*ncycle = 0;
46 
47 	/* If the extents are uniform... */
48 	if (fb->ext_len) {
49 		idx = div64_u64(offset, fb->ext_len);
50 
51 		if (fb->cycle_length) {
52 			u32 rem;
53 
54 			*ncycle = div_u64_rem(idx, fb->nr_extents, &rem);
55 			idx = rem;
56 		} else if (idx >= fb->nr_extents) {
57 			return NULL;
58 		}
59 		return &ext[idx];
60 	}
61 
62 	/* Extents are not uniform: binary search the extents array */
63 	lo = 0;
64 	hi = fb->nr_extents;
65 	while (lo < hi) {
66 		unsigned int mid = lo + (hi - lo) / 2;
67 
68 		if (offset < ext[mid].start)
69 			hi = mid;
70 		else if (offset >= ext[mid].end)
71 			lo = mid + 1;
72 		else
73 			return &ext[mid];
74 	}
75 
76 	return NULL;
77 }
78 
79 static int fuse_add_extent(struct fuse_conn *fc, struct fuse_iext *new_fie,
80 			   struct fuse_extent *ext)
81 {
82 	loff_t end;
83 	u64 tmp;
84 
85 	if (ext->reserved[0] || ext->reserved[1])
86 		return fuse_EIO("reserved fields set");
87 
88 	if (!PAGE_ALIGNED(ext->offset) || !PAGE_ALIGNED(ext->length) || !PAGE_ALIGNED(ext->addr))
89 		return fuse_EIO("not page aligned");
90 
91 	if (!ext->length)
92 		return fuse_EIO("zero sized extent");
93 
94 	if (overflows_type(ext->offset, loff_t) ||
95 	    check_add_overflow(ext->offset, ext->length, &end) ||
96 	    check_add_overflow(ext->addr, ext->length, &tmp))
97 		return fuse_EIO("offset overflow");
98 
99 	new_fie->backing = fuse_backing_lookup(fc, ext->backing_id);
100 	if (!new_fie->backing)
101 		return fuse_EIO("backing not found");
102 
103 	if (new_fie->backing->type != FUSE_BACKING_DAXDEV) {
104 		fuse_backing_put(new_fie->backing);
105 		new_fie->backing = NULL;
106 		return fuse_EIO("backing is not dax device");
107 	}
108 
109 	new_fie->start = ext->offset;
110 	new_fie->end = end;
111 	new_fie->backing_offset = ext->addr;
112 
113 	return 0;
114 }
115 
116 static int fuse_iext_cmp(const void *a, const void *b)
117 {
118 	const struct fuse_iext *x = a, *y = b;
119 
120 	if (x->start < y->start)
121 		return -1;
122 	return x->start > y->start;
123 }
124 
125 /*
126  * fb has an array of extents;
127  * - Sort it to guarantee they are in file offset order
128  * - Check whether it is uniform (starts at ofs=0, all same size, no gaps)
129  * - Validate if cyclic
130  *
131  * Array-based extent lists have better cache locality than a tree
132  * (packed into cache lines). Uniform simple extent lists and all currently
133  * valid cyclic extent lists can be indexed in O(1) at fault time. And
134  * when search is needed, arrays can be bisected for balanced O(log2 n).
135  */
136 static int fuse_ext_map_finish(struct fuse_backing *fb, bool cyclic)
137 {
138 	struct fuse_iext *ext = fb->extents;
139 	unsigned int n = fb->nr_extents;
140 	unsigned int i;
141 	bool uniform;
142 	u64 len;
143 
144 	sort(ext, n, sizeof(*ext), fuse_iext_cmp, NULL);
145 
146 	len = ext[0].end - ext[0].start;
147 	uniform = ext[0].start == 0;
148 
149 	/* Check for uniformity */
150 	for (i = 1; i < n; i++) {
151 		if (ext[i].start < ext[i - 1].end)
152 			return fuse_EIO("overlap");
153 
154 		if (ext[i].start != ext[i - 1].end ||
155 		    ext[i].end - ext[i].start != len)
156 			uniform = false;
157 	}
158 
159 	if (cyclic) {
160 		if (!uniform ||
161 		    check_mul_overflow(len, (u64)n, &fb->cycle_length))
162 			return -EINVAL;
163 	}
164 
165 	if (uniform)
166 		fb->ext_len = len;
167 
168 	return 0;
169 }
170 
171 static int fuse_ext_map_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
172 				    unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
173 {
174 	struct fuse_backing *fb = fuse_inode_backing(get_fuse_inode(inode));
175 	struct fuse_iext *fie;
176 	u64 ncycle, seq_off, addr, tmp;
177 	loff_t ext_len;
178 
179 	if (!fb || fb->type != FUSE_BACKING_EXTMAP)
180 		return fuse_EIO("missing or wrong type backing");
181 
182 	fie = fuse_find_extent(fb, offset, &ncycle);
183 	if (!fie)
184 		return fuse_EIO("missing mapping");
185 
186 	if (WARN_ON(fie->backing->type != FUSE_BACKING_DAXDEV))
187 		return fuse_EIO("wrong type backing for extent");
188 
189 	if (fie->backing->dax_error) {
190 		fuse_make_bad(inode);
191 		return fuse_EIO("dax error");
192 	}
193 
194 	ext_len = fie->end - fie->start;
195 	seq_off = ncycle * fb->cycle_length;
196 	addr = fie->backing_offset;
197 	if (check_mul_overflow(ncycle, ext_len, &tmp) || check_add_overflow(addr, tmp, &addr))
198 		return fuse_EIO("extent address overflow");
199 
200 	if (check_add_overflow(addr, ext_len, &tmp))
201 		return fuse_EIO("extent end overflow");
202 
203 	iomap->offset = fie->start + seq_off;
204 	iomap->addr = addr;
205 	iomap->length = ext_len;
206 	iomap->dax_dev = fie->backing->dax_dev;
207 	iomap->type = IOMAP_MAPPED;
208 	iomap->flags = 0;
209 
210 	return 0;
211 }
212 
213 static DEFINE_IOMAP_ITER_NEXT(fuse_ext_map_iomap_next,
214 			      fuse_ext_map_iomap_begin);
215 
216 static vm_fault_t fuse_ext_map_huge_fault(struct vm_fault *vmf, unsigned int order)
217 {
218 	struct inode *inode = file_inode(vmf->vma->vm_file);
219 	bool write_fault = (vmf->flags & FAULT_FLAG_WRITE) && (vmf->vma->vm_flags & VM_SHARED);
220 	vm_fault_t ret;
221 	unsigned long pfn;
222 
223 	if (!IS_ENABLED(CONFIG_FS_DAX))
224 		return VM_FAULT_SIGBUS;
225 
226 	if (WARN_ON_ONCE(!IS_DAX(inode)))
227 		return VM_FAULT_SIGBUS;
228 
229 	if (write_fault) {
230 		sb_start_pagefault(inode->i_sb);
231 		file_update_time(vmf->vma->vm_file);
232 	}
233 
234 	filemap_invalidate_lock_shared(inode->i_mapping);
235 
236 	ret = dax_iomap_fault(vmf, order, &pfn, NULL, fuse_ext_map_iomap_next);
237 	if (ret & VM_FAULT_NEEDDSYNC)
238 		ret = dax_finish_sync_fault(vmf, order, pfn);
239 
240 	filemap_invalidate_unlock_shared(inode->i_mapping);
241 
242 	if (write_fault)
243 		sb_end_pagefault(inode->i_sb);
244 
245 	return ret;
246 }
247 
248 static vm_fault_t fuse_ext_map_fault(struct vm_fault *vmf)
249 {
250 	return fuse_ext_map_huge_fault(vmf, 0);
251 }
252 
253 static const struct vm_operations_struct fuse_ext_map_vm_ops = {
254 	.fault		= fuse_ext_map_fault,
255 	.huge_fault	= fuse_ext_map_huge_fault,
256 	.page_mkwrite	= fuse_ext_map_fault,
257 	.pfn_mkwrite	= fuse_ext_map_fault,
258 };
259 
260 static void fuse_rw_clamp(struct kiocb *iocb, struct iov_iter *ubuf)
261 {
262 	struct inode *inode = iocb->ki_filp->f_mapping->host;
263 	loff_t i_size = i_size_read(inode);
264 	loff_t max_count = iocb->ki_pos >= i_size ? 0 : i_size - iocb->ki_pos;
265 
266 	if (iov_iter_count(ubuf) > max_count)
267 		iov_iter_truncate(ubuf, max_count);
268 }
269 
270 ssize_t fuse_ext_map_read_iter(struct kiocb *iocb, struct iov_iter *to)
271 {
272 	struct inode *inode = iocb->ki_filp->f_mapping->host;
273 	ssize_t res;
274 
275 	guard(rwsem_read)(&inode->i_rwsem);
276 
277 	fuse_rw_clamp(iocb, to);
278 
279 	if (!iov_iter_count(to))
280 		return 0;
281 
282 	if (!IS_ENABLED(CONFIG_FS_DAX))
283 		return -EIO;
284 
285 	res = dax_iomap_rw(iocb, to, fuse_ext_map_iomap_next);
286 
287 	file_accessed(iocb->ki_filp);
288 	return res;
289 }
290 
291 ssize_t fuse_ext_map_write_iter(struct kiocb *iocb, struct iov_iter *from)
292 {
293 	ssize_t res;
294 
295 	res = generic_write_checks(iocb, from);
296 	if (res <= 0)
297 		return res;
298 
299 	fuse_rw_clamp(iocb, from);
300 
301 	if (!iov_iter_count(from))
302 		return -ENOSPC;
303 
304 	res = kiocb_modified(iocb);
305 	if (res)
306 		return res;
307 
308 	if (!IS_ENABLED(CONFIG_FS_DAX))
309 		return -EIO;
310 
311 	return dax_iomap_rw(iocb, from, fuse_ext_map_iomap_next);
312 }
313 
314 int fuse_ext_map_mmap(struct file *file, struct vm_area_struct *vma)
315 {
316 	file_accessed(file);
317 	vma->vm_ops = &fuse_ext_map_vm_ops;
318 	vm_flags_set(vma, VM_HUGEPAGE);
319 	return 0;
320 }
321 
322 bool fuse_ext_map_is_dax(struct fuse_backing *fb)
323 {
324 	if (WARN_ON(fb->type != FUSE_BACKING_EXTMAP || !fb->nr_extents))
325 		return false;
326 
327 	return fb->extents[0].backing->type == FUSE_BACKING_DAXDEV;
328 }
329 
330 int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg,
331 			  struct fuse_extent *ext)
332 {
333 	struct fuse_backing *fb;
334 	unsigned int i;
335 	int err;
336 
337 	if (!arg->num_extents)
338 		return fuse_EIO("no extents");
339 
340 	/* Adding extents to an existing extmap backing is not yet supported */
341 	if (!(arg->flags & FUSE_BACKING_MAP_CREATE))
342 		return -EINVAL;
343 
344 	fb = kzalloc_obj(*fb);
345 	if (!fb)
346 		return -ENOMEM;
347 
348 	refcount_set(&fb->count, 1);
349 	fb->type = FUSE_BACKING_EXTMAP;
350 	fb->backing_id = arg->backing_id;
351 
352 	err = -ENOMEM;
353 	fb->extents = kvcalloc(arg->num_extents, sizeof(*fb->extents), GFP_KERNEL);
354 	if (!fb->extents)
355 		goto err_put;
356 
357 	for (i = 0; i < arg->num_extents; i++) {
358 		err = fuse_add_extent(fc, &fb->extents[i], &ext[i]);
359 		if (err)
360 			goto err_put;
361 		fb->nr_extents++;
362 	}
363 
364 	err = fuse_ext_map_finish(fb, arg->flags & FUSE_BACKING_MAP_CYCLIC);
365 	if (err)
366 		goto err_put;
367 
368 	err = fuse_backing_add_64(fc, fb);
369 	if (!err)
370 		return 0;
371 
372 err_put:
373 	fuse_backing_put(fb);
374 	return err;
375 }
376