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 7 struct fuse_iext { 8 struct rb_node rb; 9 u64 start; 10 u64 end; /* exclusive */ 11 struct fuse_backing *backing; 12 u64 backing_offset; 13 }; 14 15 void fuse_ext_map_destroy(struct rb_root *extents) 16 { 17 struct fuse_iext *fie, *tmp; 18 19 rbtree_postorder_for_each_entry_safe(fie, tmp, extents, rb) { 20 fuse_backing_put(fie->backing); 21 kfree(fie); 22 } 23 } 24 25 static int fuse_add_extent(struct fuse_conn *fc, struct rb_root *extents, 26 struct fuse_extent *ext) 27 { 28 struct fuse_iext *new_fie __free(kfree) = kzalloc_obj(*new_fie); 29 struct rb_node *parent = NULL, **link = &extents->rb_node; 30 loff_t end; 31 u64 tmp; 32 33 if (!new_fie) 34 return -ENOMEM; 35 36 if (ext->reserved[0] || ext->reserved[1]) 37 return fuse_EIO("reserved fields set"); 38 39 if (!PAGE_ALIGNED(ext->offset) || !PAGE_ALIGNED(ext->length) || !PAGE_ALIGNED(ext->addr)) 40 return fuse_EIO("not page aligned"); 41 42 if (!ext->length) 43 return fuse_EIO("zero sized extent"); 44 45 if (overflows_type(ext->offset, loff_t) || 46 check_add_overflow(ext->offset, ext->length, &end) || 47 check_add_overflow(ext->addr, ext->length, &tmp)) 48 return fuse_EIO("offset overflow"); 49 50 new_fie->start = ext->offset; 51 new_fie->end = end; 52 new_fie->backing_offset = ext->addr; 53 54 while (*link) { 55 struct fuse_iext *fie = rb_entry(*link, typeof(*fie), rb); 56 57 parent = *link; 58 if (new_fie->end <= fie->start) 59 link = &parent->rb_left; 60 else if (new_fie->start >= fie->end) 61 link = &parent->rb_right; 62 else 63 return fuse_EIO("overlap"); 64 } 65 66 new_fie->backing = fuse_backing_lookup(fc, ext->backing_id); 67 if (!new_fie->backing) 68 return fuse_EIO("backing not found"); 69 70 if (new_fie->backing->type != FUSE_BACKING_DAXDEV) { 71 fuse_backing_put(new_fie->backing); 72 return fuse_EIO("backing is not dax device"); 73 } 74 75 rb_link_node(&new_fie->rb, parent, link); 76 rb_insert_color(&no_free_ptr(new_fie)->rb, extents); 77 78 return 0; 79 } 80 81 bool fuse_ext_map_is_dax(struct fuse_backing *fb) 82 { 83 struct fuse_iext *fie; 84 85 if (WARN_ON(fb->type != FUSE_BACKING_EXTMAP || RB_EMPTY_ROOT(&fb->extents))) 86 return false; 87 88 fie = rb_entry(fb->extents.rb_node, typeof(*fie), rb); 89 return fie->backing->type == FUSE_BACKING_DAXDEV; 90 } 91 92 int fuse_ext_map_populate(struct fuse_conn *fc, struct fuse_notify_backing_map_out *arg, 93 struct fuse_extent *ext) 94 { 95 struct fuse_backing *fb; 96 unsigned int i; 97 int err; 98 99 if (!arg->num_extents) 100 return fuse_EIO("no extents"); 101 102 if (arg->flags & FUSE_BACKING_MAP_CREATE) { 103 fb = kzalloc_obj(*fb); 104 if (!fb) 105 return -ENOMEM; 106 107 refcount_set(&fb->count, 1); 108 fb->type = FUSE_BACKING_EXTMAP; 109 fb->backing_id = arg->backing_id; 110 } else { 111 /* Adding extents to an existing extmap backing is not yet supported */ 112 return -EINVAL; 113 } 114 115 for (i = 0; i < arg->num_extents; i++) { 116 err = fuse_add_extent(fc, &fb->extents, &ext[i]); 117 if (err) 118 goto err_put; 119 } 120 121 err = 0; 122 if (arg->flags & FUSE_BACKING_MAP_CREATE) { 123 err = fuse_backing_add_64(fc, fb); 124 if (!err) 125 return 0; 126 } 127 128 err_put: 129 fuse_backing_put(fb); 130 return err; 131 } 132