xref: /linux/fs/fuse/ext_map.c (revision 0af298e62e5cff4ebf9bec47b8711177a7e2f115)
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 const struct iomap_ops fuse_ext_map_iomap_ops = {
214 	.iomap_begin		= fuse_ext_map_iomap_begin,
215 };
216 
217 static vm_fault_t fuse_ext_map_huge_fault(struct vm_fault *vmf, unsigned int order)
218 {
219 	struct inode *inode = file_inode(vmf->vma->vm_file);
220 	bool write_fault = (vmf->flags & FAULT_FLAG_WRITE) && (vmf->vma->vm_flags & VM_SHARED);
221 	vm_fault_t ret;
222 	unsigned long pfn;
223 
224 	if (!IS_ENABLED(CONFIG_FS_DAX))
225 		return VM_FAULT_SIGBUS;
226 
227 	if (WARN_ON_ONCE(!IS_DAX(inode)))
228 		return VM_FAULT_SIGBUS;
229 
230 	if (write_fault) {
231 		sb_start_pagefault(inode->i_sb);
232 		file_update_time(vmf->vma->vm_file);
233 	}
234 
235 	filemap_invalidate_lock_shared(inode->i_mapping);
236 
237 	ret = dax_iomap_fault(vmf, order, &pfn, NULL, &fuse_ext_map_iomap_ops);
238 	if (ret & VM_FAULT_NEEDDSYNC)
239 		ret = dax_finish_sync_fault(vmf, order, pfn);
240 
241 	filemap_invalidate_unlock_shared(inode->i_mapping);
242 
243 	if (write_fault)
244 		sb_end_pagefault(inode->i_sb);
245 
246 	return ret;
247 }
248 
249 static vm_fault_t fuse_ext_map_fault(struct vm_fault *vmf)
250 {
251 	return fuse_ext_map_huge_fault(vmf, 0);
252 }
253 
254 static const struct vm_operations_struct fuse_ext_map_vm_ops = {
255 	.fault		= fuse_ext_map_fault,
256 	.huge_fault	= fuse_ext_map_huge_fault,
257 	.page_mkwrite	= fuse_ext_map_fault,
258 	.pfn_mkwrite	= fuse_ext_map_fault,
259 };
260 
261 static void fuse_rw_clamp(struct kiocb *iocb, struct iov_iter *ubuf)
262 {
263 	struct inode *inode = iocb->ki_filp->f_mapping->host;
264 	loff_t i_size = i_size_read(inode);
265 	loff_t max_count = iocb->ki_pos >= i_size ? 0 : i_size - iocb->ki_pos;
266 
267 	if (iov_iter_count(ubuf) > max_count)
268 		iov_iter_truncate(ubuf, max_count);
269 }
270 
271 ssize_t fuse_ext_map_read_iter(struct kiocb *iocb, struct iov_iter *to)
272 {
273 	struct inode *inode = iocb->ki_filp->f_mapping->host;
274 	ssize_t res;
275 
276 	guard(rwsem_read)(&inode->i_rwsem);
277 
278 	fuse_rw_clamp(iocb, to);
279 
280 	if (!iov_iter_count(to))
281 		return 0;
282 
283 	if (!IS_ENABLED(CONFIG_FS_DAX))
284 		return -EIO;
285 
286 	res = dax_iomap_rw(iocb, to, &fuse_ext_map_iomap_ops);
287 
288 	file_accessed(iocb->ki_filp);
289 	return res;
290 }
291 
292 ssize_t fuse_ext_map_write_iter(struct kiocb *iocb, struct iov_iter *from)
293 {
294 	ssize_t res;
295 
296 	res = generic_write_checks(iocb, from);
297 	if (res <= 0)
298 		return res;
299 
300 	fuse_rw_clamp(iocb, from);
301 
302 	if (!iov_iter_count(from))
303 		return -ENOSPC;
304 
305 	res = kiocb_modified(iocb);
306 	if (res)
307 		return res;
308 
309 	if (!IS_ENABLED(CONFIG_FS_DAX))
310 		return -EIO;
311 
312 	return dax_iomap_rw(iocb, from, &fuse_ext_map_iomap_ops);
313 }
314 
315 int fuse_ext_map_mmap(struct file *file, struct vm_area_struct *vma)
316 {
317 	file_accessed(file);
318 	vma->vm_ops = &fuse_ext_map_vm_ops;
319 	vm_flags_set(vma, VM_HUGEPAGE);
320 	return 0;
321 }
322 
323 bool fuse_ext_map_is_dax(struct fuse_backing *fb)
324 {
325 	if (WARN_ON(fb->type != FUSE_BACKING_EXTMAP || !fb->nr_extents))
326 		return false;
327 
328 	return fb->extents[0].backing->type == FUSE_BACKING_DAXDEV;
329 }
330 
331 int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg,
332 			  struct fuse_extent *ext)
333 {
334 	struct fuse_backing *fb;
335 	unsigned int i;
336 	int err;
337 
338 	if (!arg->num_extents)
339 		return fuse_EIO("no extents");
340 
341 	/* Adding extents to an existing extmap backing is not yet supported */
342 	if (!(arg->flags & FUSE_BACKING_MAP_CREATE))
343 		return -EINVAL;
344 
345 	fb = kzalloc_obj(*fb);
346 	if (!fb)
347 		return -ENOMEM;
348 
349 	refcount_set(&fb->count, 1);
350 	fb->type = FUSE_BACKING_EXTMAP;
351 	fb->backing_id = arg->backing_id;
352 
353 	err = -ENOMEM;
354 	fb->extents = kvzalloc_objs(*fb->extents, arg->num_extents);
355 	if (!fb->extents)
356 		goto err_put;
357 
358 	for (i = 0; i < arg->num_extents; i++) {
359 		err = fuse_add_extent(fc, &fb->extents[i], &ext[i]);
360 		if (err)
361 			goto err_put;
362 		fb->nr_extents++;
363 	}
364 
365 	err = fuse_ext_map_finish(fb, arg->flags & FUSE_BACKING_MAP_CYCLIC);
366 	if (err)
367 		goto err_put;
368 
369 	err = fuse_backing_add_64(fc, fb);
370 	if (!err)
371 		return 0;
372 
373 err_put:
374 	fuse_backing_put(fb);
375 	return err;
376 }
377