xref: /linux/fs/fuse/ext_map.c (revision dfc68b6c99f094e06f720b57cf4ee3d0ae5dbe8f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "fuse_i.h"
4 #include <linux/rbtree.h>
5 #include <linux/pagemap.h>
6 #include <linux/iomap.h>
7 #include <linux/dax.h>
8 
9 struct fuse_iext {
10 	struct rb_node rb;
11 	u64 start;
12 	u64 end; /* exclusive */
13 	struct fuse_backing *backing;
14 	u64 backing_offset;
15 };
16 
17 void fuse_ext_map_destroy(struct rb_root *extents)
18 {
19 	struct fuse_iext *fie, *tmp;
20 
21 	rbtree_postorder_for_each_entry_safe(fie, tmp, extents, rb) {
22 		fuse_backing_put(fie->backing);
23 		kfree(fie);
24 	}
25 }
26 
27 static struct fuse_iext *fuse_find_extent(struct rb_root *extents, u64 offset)
28 {
29 	struct rb_node *node = extents->rb_node;
30 
31 	while (node) {
32 		struct fuse_iext *fie = rb_entry(node, typeof(*fie), rb);
33 
34 		if (offset < fie->start)
35 			node = node->rb_left;
36 		else if (offset >= fie->end)
37 			node = node->rb_right;
38 		else
39 			return fie;
40 	}
41 
42 	return NULL;
43 }
44 
45 static int fuse_add_extent(struct fuse_conn *fc, struct rb_root *extents,
46 			   struct fuse_extent *ext)
47 {
48 	struct fuse_iext *new_fie __free(kfree) = kzalloc_obj(*new_fie);
49 	struct rb_node *parent = NULL, **link = &extents->rb_node;
50 	loff_t end;
51 	u64 tmp;
52 
53 	if (!new_fie)
54 		return -ENOMEM;
55 
56 	if (ext->reserved[0] || ext->reserved[1])
57 		return fuse_EIO("reserved fields set");
58 
59 	if (!PAGE_ALIGNED(ext->offset) || !PAGE_ALIGNED(ext->length) || !PAGE_ALIGNED(ext->addr))
60 		return fuse_EIO("not page aligned");
61 
62 	if (!ext->length)
63 		return fuse_EIO("zero sized extent");
64 
65 	if (overflows_type(ext->offset, loff_t) ||
66 	    check_add_overflow(ext->offset, ext->length, &end) ||
67 	    check_add_overflow(ext->addr, ext->length, &tmp))
68 		return fuse_EIO("offset overflow");
69 
70 	new_fie->start = ext->offset;
71 	new_fie->end = end;
72 	new_fie->backing_offset = ext->addr;
73 
74 	while (*link) {
75 		struct fuse_iext *fie = rb_entry(*link, typeof(*fie), rb);
76 
77 		parent = *link;
78 		if (new_fie->end <= fie->start)
79 			link = &parent->rb_left;
80 		else if (new_fie->start >= fie->end)
81 			link = &parent->rb_right;
82 		else
83 			return fuse_EIO("overlap");
84 	}
85 
86 	new_fie->backing = fuse_backing_lookup(fc, ext->backing_id);
87 	if (!new_fie->backing)
88 		return fuse_EIO("backing not found");
89 
90 	if (new_fie->backing->type != FUSE_BACKING_DAXDEV) {
91 		fuse_backing_put(new_fie->backing);
92 		return fuse_EIO("backing is not dax device");
93 	}
94 
95 	rb_link_node(&new_fie->rb, parent, link);
96 	rb_insert_color(&no_free_ptr(new_fie)->rb, extents);
97 
98 	return 0;
99 }
100 
101 static int fuse_ext_map_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
102 				    unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
103 {
104 	struct fuse_backing *fb = fuse_inode_backing(get_fuse_inode(inode));
105 	struct fuse_iext *fie;
106 	u64 ncycle = 0, seq_off = 0, addr, tmp;
107 	loff_t ext_len;
108 
109 	if (!fb || fb->type != FUSE_BACKING_EXTMAP)
110 		return fuse_EIO("missing or wrong type backing");
111 
112 	if (fb->cycle_length) {
113 		ncycle = div64_u64(offset, fb->cycle_length);
114 		seq_off = ncycle * fb->cycle_length;
115 	}
116 
117 	fie = fuse_find_extent(&fb->extents, offset - seq_off);
118 	if (!fie)
119 		return fuse_EIO("missing mapping");
120 
121 	if (WARN_ON(fie->backing->type != FUSE_BACKING_DAXDEV))
122 		return fuse_EIO("wrong type backing for extent");
123 
124 	if (fie->backing->dax_error) {
125 		fuse_make_bad(inode);
126 		return fuse_EIO("dax error");
127 	}
128 
129 	ext_len = fie->end - fie->start;
130 	addr = fie->backing_offset;
131 	if (check_mul_overflow(ncycle, ext_len, &tmp) || check_add_overflow(addr, tmp, &addr))
132 		return fuse_EIO("extent address overflow");
133 
134 	if (check_add_overflow(addr, ext_len, &tmp))
135 		return fuse_EIO("extent end overflow");
136 
137 	iomap->offset = fie->start + seq_off;
138 	iomap->addr = addr;
139 	iomap->length = ext_len;
140 	iomap->dax_dev = fie->backing->dax_dev;
141 	iomap->type = IOMAP_MAPPED;
142 	iomap->flags = 0;
143 
144 	return 0;
145 }
146 
147 static const struct iomap_ops fuse_ext_map_iomap_ops = {
148 	.iomap_begin		= fuse_ext_map_iomap_begin,
149 };
150 
151 static vm_fault_t fuse_ext_map_huge_fault(struct vm_fault *vmf, unsigned int order)
152 {
153 	struct inode *inode = file_inode(vmf->vma->vm_file);
154 	bool write_fault = (vmf->flags & FAULT_FLAG_WRITE) && (vmf->vma->vm_flags & VM_SHARED);
155 	vm_fault_t ret;
156 	unsigned long pfn;
157 
158 	if (!IS_ENABLED(CONFIG_FS_DAX))
159 		return VM_FAULT_SIGBUS;
160 
161 	if (WARN_ON_ONCE(!IS_DAX(inode)))
162 		return VM_FAULT_SIGBUS;
163 
164 	if (write_fault) {
165 		sb_start_pagefault(inode->i_sb);
166 		file_update_time(vmf->vma->vm_file);
167 	}
168 
169 	filemap_invalidate_lock_shared(inode->i_mapping);
170 
171 	ret = dax_iomap_fault(vmf, order, &pfn, NULL, &fuse_ext_map_iomap_ops);
172 	if (ret & VM_FAULT_NEEDDSYNC)
173 		ret = dax_finish_sync_fault(vmf, order, pfn);
174 
175 	filemap_invalidate_unlock_shared(inode->i_mapping);
176 
177 	if (write_fault)
178 		sb_end_pagefault(inode->i_sb);
179 
180 	return ret;
181 }
182 
183 static vm_fault_t fuse_ext_map_fault(struct vm_fault *vmf)
184 {
185 	return fuse_ext_map_huge_fault(vmf, 0);
186 }
187 
188 static const struct vm_operations_struct fuse_ext_map_vm_ops = {
189 	.fault		= fuse_ext_map_fault,
190 	.huge_fault	= fuse_ext_map_huge_fault,
191 	.page_mkwrite	= fuse_ext_map_fault,
192 	.pfn_mkwrite	= fuse_ext_map_fault,
193 };
194 
195 static void fuse_rw_clamp(struct kiocb *iocb, struct iov_iter *ubuf)
196 {
197 	struct inode *inode = iocb->ki_filp->f_mapping->host;
198 	loff_t i_size = i_size_read(inode);
199 	loff_t max_count = iocb->ki_pos >= i_size ? 0 : i_size - iocb->ki_pos;
200 
201 	if (iov_iter_count(ubuf) > max_count)
202 		iov_iter_truncate(ubuf, max_count);
203 }
204 
205 ssize_t fuse_ext_map_read_iter(struct kiocb *iocb, struct iov_iter *to)
206 {
207 	struct inode *inode = iocb->ki_filp->f_mapping->host;
208 	ssize_t res;
209 
210 	guard(rwsem_read)(&inode->i_rwsem);
211 
212 	fuse_rw_clamp(iocb, to);
213 
214 	if (!iov_iter_count(to))
215 		return 0;
216 
217 	if (!IS_ENABLED(CONFIG_FS_DAX))
218 		return -EIO;
219 
220 	res = dax_iomap_rw(iocb, to, &fuse_ext_map_iomap_ops);
221 
222 	file_accessed(iocb->ki_filp);
223 	return res;
224 }
225 
226 ssize_t fuse_ext_map_write_iter(struct kiocb *iocb, struct iov_iter *from)
227 {
228 	ssize_t res;
229 
230 	res = generic_write_checks(iocb, from);
231 	if (res <= 0)
232 		return res;
233 
234 	fuse_rw_clamp(iocb, from);
235 
236 	if (!iov_iter_count(from))
237 		return -ENOSPC;
238 
239 	res = kiocb_modified(iocb);
240 	if (res)
241 		return res;
242 
243 	if (!IS_ENABLED(CONFIG_FS_DAX))
244 		return -EIO;
245 
246 	return dax_iomap_rw(iocb, from, &fuse_ext_map_iomap_ops);
247 }
248 
249 int fuse_ext_map_mmap(struct file *file, struct vm_area_struct *vma)
250 {
251 	file_accessed(file);
252 	vma->vm_ops = &fuse_ext_map_vm_ops;
253 	vm_flags_set(vma, VM_HUGEPAGE);
254 	return 0;
255 }
256 
257 bool fuse_ext_map_is_dax(struct fuse_backing *fb)
258 {
259 	struct fuse_iext *fie;
260 
261 	if (WARN_ON(fb->type != FUSE_BACKING_EXTMAP || RB_EMPTY_ROOT(&fb->extents)))
262 		return false;
263 
264 	fie = rb_entry(fb->extents.rb_node, typeof(*fie), rb);
265 	return fie->backing->type == FUSE_BACKING_DAXDEV;
266 }
267 
268 int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg,
269 			  struct fuse_extent *ext)
270 {
271 	struct fuse_backing *fb;
272 	unsigned int i;
273 	int err;
274 	u64 chunk_size = 0;
275 
276 	if (!arg->num_extents)
277 		return fuse_EIO("no extents");
278 
279 	if (arg->flags & FUSE_BACKING_MAP_CREATE) {
280 		fb = kzalloc_obj(*fb);
281 		if (!fb)
282 			return -ENOMEM;
283 
284 		refcount_set(&fb->count, 1);
285 		fb->type = FUSE_BACKING_EXTMAP;
286 		fb->backing_id = arg->backing_id;
287 	} else {
288 		/* Adding extents to an existing extmap backing is not yet supported */
289 		return -EINVAL;
290 	}
291 
292 	if (arg->flags & FUSE_BACKING_MAP_CYCLIC) {
293 		chunk_size = ext[0].length;
294 		err = -EINVAL;
295 		if (check_mul_overflow(chunk_size, arg->num_extents, &fb->cycle_length))
296 			goto err_put;
297 	}
298 
299 	for (i = 0; i < arg->num_extents; i++) {
300 		err = -EINVAL;
301 		if (chunk_size && (ext[i].offset != chunk_size * i || ext[i].length != chunk_size))
302 			goto err_put;
303 
304 		err = fuse_add_extent(fc, &fb->extents, &ext[i]);
305 		if (err)
306 			goto err_put;
307 	}
308 
309 	err = 0;
310 	if (arg->flags & FUSE_BACKING_MAP_CREATE) {
311 		err = fuse_backing_add_64(fc, fb);
312 		if (!err)
313 			return 0;
314 	}
315 
316 err_put:
317 	fuse_backing_put(fb);
318 	return err;
319 }
320