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