11dd53957SVivek Goyal // SPDX-License-Identifier: GPL-2.0 21dd53957SVivek Goyal /* 31dd53957SVivek Goyal * dax: direct host memory access 41dd53957SVivek Goyal * Copyright (C) 2020 Red Hat, Inc. 51dd53957SVivek Goyal */ 61dd53957SVivek Goyal 71dd53957SVivek Goyal #include "fuse_i.h" 81dd53957SVivek Goyal 99a752d18SVivek Goyal #include <linux/delay.h> 101dd53957SVivek Goyal #include <linux/dax.h> 11c2d0ad00SVivek Goyal #include <linux/uio.h> 1245f2348eSVivek Goyal #include <linux/pfn_t.h> 13c2d0ad00SVivek Goyal #include <linux/iomap.h> 14c2d0ad00SVivek Goyal #include <linux/interval_tree.h> 1545f2348eSVivek Goyal 16fd1a1dc6SStefan Hajnoczi /* 17fd1a1dc6SStefan Hajnoczi * Default memory range size. A power of 2 so it agrees with common FUSE_INIT 18fd1a1dc6SStefan Hajnoczi * map_alignment values 4KB and 64KB. 19fd1a1dc6SStefan Hajnoczi */ 2045f2348eSVivek Goyal #define FUSE_DAX_SHIFT 21 2145f2348eSVivek Goyal #define FUSE_DAX_SZ (1 << FUSE_DAX_SHIFT) 2245f2348eSVivek Goyal #define FUSE_DAX_PAGES (FUSE_DAX_SZ / PAGE_SIZE) 2345f2348eSVivek Goyal 249a752d18SVivek Goyal /* Number of ranges reclaimer will try to free in one invocation */ 259a752d18SVivek Goyal #define FUSE_DAX_RECLAIM_CHUNK (10) 269a752d18SVivek Goyal 279a752d18SVivek Goyal /* 289a752d18SVivek Goyal * Dax memory reclaim threshold in percetage of total ranges. When free 299a752d18SVivek Goyal * number of free ranges drops below this threshold, reclaim can trigger 309a752d18SVivek Goyal * Default is 20% 319a752d18SVivek Goyal */ 329a752d18SVivek Goyal #define FUSE_DAX_RECLAIM_THRESHOLD (20) 339a752d18SVivek Goyal 3445f2348eSVivek Goyal /** Translation information for file offsets to DAX window offsets */ 3545f2348eSVivek Goyal struct fuse_dax_mapping { 369a752d18SVivek Goyal /* Pointer to inode where this memory range is mapped */ 379a752d18SVivek Goyal struct inode *inode; 389a752d18SVivek Goyal 3945f2348eSVivek Goyal /* Will connect in fcd->free_ranges to keep track of free memory */ 4045f2348eSVivek Goyal struct list_head list; 4145f2348eSVivek Goyal 42c2d0ad00SVivek Goyal /* For interval tree in file/inode */ 43c2d0ad00SVivek Goyal struct interval_tree_node itn; 44c2d0ad00SVivek Goyal 45d0cfb9dcSVivek Goyal /* Will connect in fc->busy_ranges to keep track busy memory */ 46d0cfb9dcSVivek Goyal struct list_head busy_list; 47d0cfb9dcSVivek Goyal 4845f2348eSVivek Goyal /** Position in DAX window */ 4945f2348eSVivek Goyal u64 window_offset; 5045f2348eSVivek Goyal 5145f2348eSVivek Goyal /** Length of mapping, in bytes */ 5245f2348eSVivek Goyal loff_t length; 53c2d0ad00SVivek Goyal 54c2d0ad00SVivek Goyal /* Is this mapping read-only or read-write */ 55c2d0ad00SVivek Goyal bool writable; 569a752d18SVivek Goyal 579a752d18SVivek Goyal /* reference count when the mapping is used by dax iomap. */ 589a752d18SVivek Goyal refcount_t refcnt; 59c2d0ad00SVivek Goyal }; 60c2d0ad00SVivek Goyal 61c2d0ad00SVivek Goyal /* Per-inode dax map */ 62c2d0ad00SVivek Goyal struct fuse_inode_dax { 63c2d0ad00SVivek Goyal /* Semaphore to protect modifications to the dmap tree */ 64c2d0ad00SVivek Goyal struct rw_semaphore sem; 65c2d0ad00SVivek Goyal 66c2d0ad00SVivek Goyal /* Sorted rb tree of struct fuse_dax_mapping elements */ 67c2d0ad00SVivek Goyal struct rb_root_cached tree; 68c2d0ad00SVivek Goyal unsigned long nr; 6945f2348eSVivek Goyal }; 701dd53957SVivek Goyal 711dd53957SVivek Goyal struct fuse_conn_dax { 721dd53957SVivek Goyal /* DAX device */ 731dd53957SVivek Goyal struct dax_device *dev; 7445f2348eSVivek Goyal 75c2d0ad00SVivek Goyal /* Lock protecting accessess to members of this structure */ 76c2d0ad00SVivek Goyal spinlock_t lock; 77c2d0ad00SVivek Goyal 78d0cfb9dcSVivek Goyal /* List of memory ranges which are busy */ 79d0cfb9dcSVivek Goyal unsigned long nr_busy_ranges; 80d0cfb9dcSVivek Goyal struct list_head busy_ranges; 81d0cfb9dcSVivek Goyal 829a752d18SVivek Goyal /* Worker to free up memory ranges */ 839a752d18SVivek Goyal struct delayed_work free_work; 849a752d18SVivek Goyal 859a752d18SVivek Goyal /* Wait queue for a dax range to become free */ 869a752d18SVivek Goyal wait_queue_head_t range_waitq; 879a752d18SVivek Goyal 8845f2348eSVivek Goyal /* DAX Window Free Ranges */ 8945f2348eSVivek Goyal long nr_free_ranges; 9045f2348eSVivek Goyal struct list_head free_ranges; 919a752d18SVivek Goyal 929a752d18SVivek Goyal unsigned long nr_ranges; 931dd53957SVivek Goyal }; 941dd53957SVivek Goyal 95c2d0ad00SVivek Goyal static inline struct fuse_dax_mapping * 96c2d0ad00SVivek Goyal node_to_dmap(struct interval_tree_node *node) 97c2d0ad00SVivek Goyal { 98c2d0ad00SVivek Goyal if (!node) 99c2d0ad00SVivek Goyal return NULL; 100c2d0ad00SVivek Goyal 101c2d0ad00SVivek Goyal return container_of(node, struct fuse_dax_mapping, itn); 102c2d0ad00SVivek Goyal } 103c2d0ad00SVivek Goyal 1049a752d18SVivek Goyal static struct fuse_dax_mapping * 1059a752d18SVivek Goyal alloc_dax_mapping_reclaim(struct fuse_conn_dax *fcd, struct inode *inode); 1069a752d18SVivek Goyal 1079a752d18SVivek Goyal static void 1089a752d18SVivek Goyal __kick_dmap_free_worker(struct fuse_conn_dax *fcd, unsigned long delay_ms) 1099a752d18SVivek Goyal { 1109a752d18SVivek Goyal unsigned long free_threshold; 1119a752d18SVivek Goyal 1129a752d18SVivek Goyal /* If number of free ranges are below threshold, start reclaim */ 1139a752d18SVivek Goyal free_threshold = max_t(unsigned long, fcd->nr_ranges * FUSE_DAX_RECLAIM_THRESHOLD / 100, 1149a752d18SVivek Goyal 1); 1159a752d18SVivek Goyal if (fcd->nr_free_ranges < free_threshold) 1169a752d18SVivek Goyal queue_delayed_work(system_long_wq, &fcd->free_work, 1179a752d18SVivek Goyal msecs_to_jiffies(delay_ms)); 1189a752d18SVivek Goyal } 1199a752d18SVivek Goyal 1209a752d18SVivek Goyal static void kick_dmap_free_worker(struct fuse_conn_dax *fcd, 1219a752d18SVivek Goyal unsigned long delay_ms) 1229a752d18SVivek Goyal { 1239a752d18SVivek Goyal spin_lock(&fcd->lock); 1249a752d18SVivek Goyal __kick_dmap_free_worker(fcd, delay_ms); 1259a752d18SVivek Goyal spin_unlock(&fcd->lock); 1269a752d18SVivek Goyal } 1279a752d18SVivek Goyal 128c2d0ad00SVivek Goyal static struct fuse_dax_mapping *alloc_dax_mapping(struct fuse_conn_dax *fcd) 129c2d0ad00SVivek Goyal { 130c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap; 131c2d0ad00SVivek Goyal 132c2d0ad00SVivek Goyal spin_lock(&fcd->lock); 133c2d0ad00SVivek Goyal dmap = list_first_entry_or_null(&fcd->free_ranges, 134c2d0ad00SVivek Goyal struct fuse_dax_mapping, list); 135c2d0ad00SVivek Goyal if (dmap) { 136c2d0ad00SVivek Goyal list_del_init(&dmap->list); 137c2d0ad00SVivek Goyal WARN_ON(fcd->nr_free_ranges <= 0); 138c2d0ad00SVivek Goyal fcd->nr_free_ranges--; 139c2d0ad00SVivek Goyal } 140c2d0ad00SVivek Goyal spin_unlock(&fcd->lock); 1419a752d18SVivek Goyal 1429a752d18SVivek Goyal kick_dmap_free_worker(fcd, 0); 143c2d0ad00SVivek Goyal return dmap; 144c2d0ad00SVivek Goyal } 145c2d0ad00SVivek Goyal 146c2d0ad00SVivek Goyal /* This assumes fcd->lock is held */ 147d0cfb9dcSVivek Goyal static void __dmap_remove_busy_list(struct fuse_conn_dax *fcd, 148d0cfb9dcSVivek Goyal struct fuse_dax_mapping *dmap) 149d0cfb9dcSVivek Goyal { 150d0cfb9dcSVivek Goyal list_del_init(&dmap->busy_list); 151d0cfb9dcSVivek Goyal WARN_ON(fcd->nr_busy_ranges == 0); 152d0cfb9dcSVivek Goyal fcd->nr_busy_ranges--; 153d0cfb9dcSVivek Goyal } 154d0cfb9dcSVivek Goyal 1559a752d18SVivek Goyal static void dmap_remove_busy_list(struct fuse_conn_dax *fcd, 1569a752d18SVivek Goyal struct fuse_dax_mapping *dmap) 1579a752d18SVivek Goyal { 1589a752d18SVivek Goyal spin_lock(&fcd->lock); 1599a752d18SVivek Goyal __dmap_remove_busy_list(fcd, dmap); 1609a752d18SVivek Goyal spin_unlock(&fcd->lock); 1619a752d18SVivek Goyal } 1629a752d18SVivek Goyal 163d0cfb9dcSVivek Goyal /* This assumes fcd->lock is held */ 164c2d0ad00SVivek Goyal static void __dmap_add_to_free_pool(struct fuse_conn_dax *fcd, 165c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap) 166c2d0ad00SVivek Goyal { 167c2d0ad00SVivek Goyal list_add_tail(&dmap->list, &fcd->free_ranges); 168c2d0ad00SVivek Goyal fcd->nr_free_ranges++; 1699a752d18SVivek Goyal wake_up(&fcd->range_waitq); 170c2d0ad00SVivek Goyal } 171c2d0ad00SVivek Goyal 172c2d0ad00SVivek Goyal static void dmap_add_to_free_pool(struct fuse_conn_dax *fcd, 173c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap) 174c2d0ad00SVivek Goyal { 175c2d0ad00SVivek Goyal /* Return fuse_dax_mapping to free list */ 176c2d0ad00SVivek Goyal spin_lock(&fcd->lock); 177c2d0ad00SVivek Goyal __dmap_add_to_free_pool(fcd, dmap); 178c2d0ad00SVivek Goyal spin_unlock(&fcd->lock); 179c2d0ad00SVivek Goyal } 180c2d0ad00SVivek Goyal 181c2d0ad00SVivek Goyal static int fuse_setup_one_mapping(struct inode *inode, unsigned long start_idx, 182c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap, bool writable, 183c2d0ad00SVivek Goyal bool upgrade) 184c2d0ad00SVivek Goyal { 185*fcee216bSMax Reitz struct fuse_mount *fm = get_fuse_mount(inode); 186*fcee216bSMax Reitz struct fuse_conn_dax *fcd = fm->fc->dax; 187c2d0ad00SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 188c2d0ad00SVivek Goyal struct fuse_setupmapping_in inarg; 189c2d0ad00SVivek Goyal loff_t offset = start_idx << FUSE_DAX_SHIFT; 190c2d0ad00SVivek Goyal FUSE_ARGS(args); 191c2d0ad00SVivek Goyal ssize_t err; 192c2d0ad00SVivek Goyal 193c2d0ad00SVivek Goyal WARN_ON(fcd->nr_free_ranges < 0); 194c2d0ad00SVivek Goyal 195c2d0ad00SVivek Goyal /* Ask fuse daemon to setup mapping */ 196c2d0ad00SVivek Goyal memset(&inarg, 0, sizeof(inarg)); 197c2d0ad00SVivek Goyal inarg.foffset = offset; 198c2d0ad00SVivek Goyal inarg.fh = -1; 199c2d0ad00SVivek Goyal inarg.moffset = dmap->window_offset; 200c2d0ad00SVivek Goyal inarg.len = FUSE_DAX_SZ; 201c2d0ad00SVivek Goyal inarg.flags |= FUSE_SETUPMAPPING_FLAG_READ; 202c2d0ad00SVivek Goyal if (writable) 203c2d0ad00SVivek Goyal inarg.flags |= FUSE_SETUPMAPPING_FLAG_WRITE; 204c2d0ad00SVivek Goyal args.opcode = FUSE_SETUPMAPPING; 205c2d0ad00SVivek Goyal args.nodeid = fi->nodeid; 206c2d0ad00SVivek Goyal args.in_numargs = 1; 207c2d0ad00SVivek Goyal args.in_args[0].size = sizeof(inarg); 208c2d0ad00SVivek Goyal args.in_args[0].value = &inarg; 209*fcee216bSMax Reitz err = fuse_simple_request(fm, &args); 210c2d0ad00SVivek Goyal if (err < 0) 211c2d0ad00SVivek Goyal return err; 212c2d0ad00SVivek Goyal dmap->writable = writable; 213c2d0ad00SVivek Goyal if (!upgrade) { 2149a752d18SVivek Goyal /* 2159a752d18SVivek Goyal * We don't take a refernce on inode. inode is valid right now 2169a752d18SVivek Goyal * and when inode is going away, cleanup logic should first 2179a752d18SVivek Goyal * cleanup dmap entries. 2189a752d18SVivek Goyal */ 2199a752d18SVivek Goyal dmap->inode = inode; 220c2d0ad00SVivek Goyal dmap->itn.start = dmap->itn.last = start_idx; 221c2d0ad00SVivek Goyal /* Protected by fi->dax->sem */ 222c2d0ad00SVivek Goyal interval_tree_insert(&dmap->itn, &fi->dax->tree); 223c2d0ad00SVivek Goyal fi->dax->nr++; 224d0cfb9dcSVivek Goyal spin_lock(&fcd->lock); 225d0cfb9dcSVivek Goyal list_add_tail(&dmap->busy_list, &fcd->busy_ranges); 226d0cfb9dcSVivek Goyal fcd->nr_busy_ranges++; 227d0cfb9dcSVivek Goyal spin_unlock(&fcd->lock); 228c2d0ad00SVivek Goyal } 229c2d0ad00SVivek Goyal return 0; 230c2d0ad00SVivek Goyal } 231c2d0ad00SVivek Goyal 232c2d0ad00SVivek Goyal static int fuse_send_removemapping(struct inode *inode, 233c2d0ad00SVivek Goyal struct fuse_removemapping_in *inargp, 234c2d0ad00SVivek Goyal struct fuse_removemapping_one *remove_one) 235c2d0ad00SVivek Goyal { 236c2d0ad00SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 237*fcee216bSMax Reitz struct fuse_mount *fm = get_fuse_mount(inode); 238c2d0ad00SVivek Goyal FUSE_ARGS(args); 239c2d0ad00SVivek Goyal 240c2d0ad00SVivek Goyal args.opcode = FUSE_REMOVEMAPPING; 241c2d0ad00SVivek Goyal args.nodeid = fi->nodeid; 242c2d0ad00SVivek Goyal args.in_numargs = 2; 243c2d0ad00SVivek Goyal args.in_args[0].size = sizeof(*inargp); 244c2d0ad00SVivek Goyal args.in_args[0].value = inargp; 245c2d0ad00SVivek Goyal args.in_args[1].size = inargp->count * sizeof(*remove_one); 246c2d0ad00SVivek Goyal args.in_args[1].value = remove_one; 247*fcee216bSMax Reitz return fuse_simple_request(fm, &args); 248c2d0ad00SVivek Goyal } 249c2d0ad00SVivek Goyal 250c2d0ad00SVivek Goyal static int dmap_removemapping_list(struct inode *inode, unsigned int num, 251c2d0ad00SVivek Goyal struct list_head *to_remove) 252c2d0ad00SVivek Goyal { 253c2d0ad00SVivek Goyal struct fuse_removemapping_one *remove_one, *ptr; 254c2d0ad00SVivek Goyal struct fuse_removemapping_in inarg; 255c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap; 256c2d0ad00SVivek Goyal int ret, i = 0, nr_alloc; 257c2d0ad00SVivek Goyal 258c2d0ad00SVivek Goyal nr_alloc = min_t(unsigned int, num, FUSE_REMOVEMAPPING_MAX_ENTRY); 259c2d0ad00SVivek Goyal remove_one = kmalloc_array(nr_alloc, sizeof(*remove_one), GFP_NOFS); 260c2d0ad00SVivek Goyal if (!remove_one) 261c2d0ad00SVivek Goyal return -ENOMEM; 262c2d0ad00SVivek Goyal 263c2d0ad00SVivek Goyal ptr = remove_one; 264c2d0ad00SVivek Goyal list_for_each_entry(dmap, to_remove, list) { 265c2d0ad00SVivek Goyal ptr->moffset = dmap->window_offset; 266c2d0ad00SVivek Goyal ptr->len = dmap->length; 267c2d0ad00SVivek Goyal ptr++; 268c2d0ad00SVivek Goyal i++; 269c2d0ad00SVivek Goyal num--; 270c2d0ad00SVivek Goyal if (i >= nr_alloc || num == 0) { 271c2d0ad00SVivek Goyal memset(&inarg, 0, sizeof(inarg)); 272c2d0ad00SVivek Goyal inarg.count = i; 273c2d0ad00SVivek Goyal ret = fuse_send_removemapping(inode, &inarg, 274c2d0ad00SVivek Goyal remove_one); 275c2d0ad00SVivek Goyal if (ret) 276c2d0ad00SVivek Goyal goto out; 277c2d0ad00SVivek Goyal ptr = remove_one; 278c2d0ad00SVivek Goyal i = 0; 279c2d0ad00SVivek Goyal } 280c2d0ad00SVivek Goyal } 281c2d0ad00SVivek Goyal out: 282c2d0ad00SVivek Goyal kfree(remove_one); 283c2d0ad00SVivek Goyal return ret; 284c2d0ad00SVivek Goyal } 285c2d0ad00SVivek Goyal 286c2d0ad00SVivek Goyal /* 287c2d0ad00SVivek Goyal * Cleanup dmap entry and add back to free list. This should be called with 288c2d0ad00SVivek Goyal * fcd->lock held. 289c2d0ad00SVivek Goyal */ 290c2d0ad00SVivek Goyal static void dmap_reinit_add_to_free_pool(struct fuse_conn_dax *fcd, 291c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap) 292c2d0ad00SVivek Goyal { 293c2d0ad00SVivek Goyal pr_debug("fuse: freeing memory range start_idx=0x%lx end_idx=0x%lx window_offset=0x%llx length=0x%llx\n", 294c2d0ad00SVivek Goyal dmap->itn.start, dmap->itn.last, dmap->window_offset, 295c2d0ad00SVivek Goyal dmap->length); 296d0cfb9dcSVivek Goyal __dmap_remove_busy_list(fcd, dmap); 2979a752d18SVivek Goyal dmap->inode = NULL; 298c2d0ad00SVivek Goyal dmap->itn.start = dmap->itn.last = 0; 299c2d0ad00SVivek Goyal __dmap_add_to_free_pool(fcd, dmap); 300c2d0ad00SVivek Goyal } 301c2d0ad00SVivek Goyal 302c2d0ad00SVivek Goyal /* 303c2d0ad00SVivek Goyal * Free inode dmap entries whose range falls inside [start, end]. 304c2d0ad00SVivek Goyal * Does not take any locks. At this point of time it should only be 305c2d0ad00SVivek Goyal * called from evict_inode() path where we know all dmap entries can be 306c2d0ad00SVivek Goyal * reclaimed. 307c2d0ad00SVivek Goyal */ 308c2d0ad00SVivek Goyal static void inode_reclaim_dmap_range(struct fuse_conn_dax *fcd, 309c2d0ad00SVivek Goyal struct inode *inode, 310c2d0ad00SVivek Goyal loff_t start, loff_t end) 311c2d0ad00SVivek Goyal { 312c2d0ad00SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 313c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap, *n; 314c2d0ad00SVivek Goyal int err, num = 0; 315c2d0ad00SVivek Goyal LIST_HEAD(to_remove); 316c2d0ad00SVivek Goyal unsigned long start_idx = start >> FUSE_DAX_SHIFT; 317c2d0ad00SVivek Goyal unsigned long end_idx = end >> FUSE_DAX_SHIFT; 318c2d0ad00SVivek Goyal struct interval_tree_node *node; 319c2d0ad00SVivek Goyal 320c2d0ad00SVivek Goyal while (1) { 321c2d0ad00SVivek Goyal node = interval_tree_iter_first(&fi->dax->tree, start_idx, 322c2d0ad00SVivek Goyal end_idx); 323c2d0ad00SVivek Goyal if (!node) 324c2d0ad00SVivek Goyal break; 325c2d0ad00SVivek Goyal dmap = node_to_dmap(node); 3269a752d18SVivek Goyal /* inode is going away. There should not be any users of dmap */ 3279a752d18SVivek Goyal WARN_ON(refcount_read(&dmap->refcnt) > 1); 328c2d0ad00SVivek Goyal interval_tree_remove(&dmap->itn, &fi->dax->tree); 329c2d0ad00SVivek Goyal num++; 330c2d0ad00SVivek Goyal list_add(&dmap->list, &to_remove); 331c2d0ad00SVivek Goyal } 332c2d0ad00SVivek Goyal 333c2d0ad00SVivek Goyal /* Nothing to remove */ 334c2d0ad00SVivek Goyal if (list_empty(&to_remove)) 335c2d0ad00SVivek Goyal return; 336c2d0ad00SVivek Goyal 337c2d0ad00SVivek Goyal WARN_ON(fi->dax->nr < num); 338c2d0ad00SVivek Goyal fi->dax->nr -= num; 339c2d0ad00SVivek Goyal err = dmap_removemapping_list(inode, num, &to_remove); 340c2d0ad00SVivek Goyal if (err && err != -ENOTCONN) { 341c2d0ad00SVivek Goyal pr_warn("Failed to removemappings. start=0x%llx end=0x%llx\n", 342c2d0ad00SVivek Goyal start, end); 343c2d0ad00SVivek Goyal } 344c2d0ad00SVivek Goyal spin_lock(&fcd->lock); 345c2d0ad00SVivek Goyal list_for_each_entry_safe(dmap, n, &to_remove, list) { 346c2d0ad00SVivek Goyal list_del_init(&dmap->list); 347c2d0ad00SVivek Goyal dmap_reinit_add_to_free_pool(fcd, dmap); 348c2d0ad00SVivek Goyal } 349c2d0ad00SVivek Goyal spin_unlock(&fcd->lock); 350c2d0ad00SVivek Goyal } 351c2d0ad00SVivek Goyal 3529a752d18SVivek Goyal static int dmap_removemapping_one(struct inode *inode, 3539a752d18SVivek Goyal struct fuse_dax_mapping *dmap) 3549a752d18SVivek Goyal { 3559a752d18SVivek Goyal struct fuse_removemapping_one forget_one; 3569a752d18SVivek Goyal struct fuse_removemapping_in inarg; 3579a752d18SVivek Goyal 3589a752d18SVivek Goyal memset(&inarg, 0, sizeof(inarg)); 3599a752d18SVivek Goyal inarg.count = 1; 3609a752d18SVivek Goyal memset(&forget_one, 0, sizeof(forget_one)); 3619a752d18SVivek Goyal forget_one.moffset = dmap->window_offset; 3629a752d18SVivek Goyal forget_one.len = dmap->length; 3639a752d18SVivek Goyal 3649a752d18SVivek Goyal return fuse_send_removemapping(inode, &inarg, &forget_one); 3659a752d18SVivek Goyal } 3669a752d18SVivek Goyal 367c2d0ad00SVivek Goyal /* 368c2d0ad00SVivek Goyal * It is called from evict_inode() and by that time inode is going away. So 369c2d0ad00SVivek Goyal * this function does not take any locks like fi->dax->sem for traversing 370c2d0ad00SVivek Goyal * that fuse inode interval tree. If that lock is taken then lock validator 371c2d0ad00SVivek Goyal * complains of deadlock situation w.r.t fs_reclaim lock. 372c2d0ad00SVivek Goyal */ 373c2d0ad00SVivek Goyal void fuse_dax_inode_cleanup(struct inode *inode) 374c2d0ad00SVivek Goyal { 375c2d0ad00SVivek Goyal struct fuse_conn *fc = get_fuse_conn(inode); 376c2d0ad00SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 377c2d0ad00SVivek Goyal 378c2d0ad00SVivek Goyal /* 379c2d0ad00SVivek Goyal * fuse_evict_inode() has already called truncate_inode_pages_final() 380c2d0ad00SVivek Goyal * before we arrive here. So we should not have to worry about any 381c2d0ad00SVivek Goyal * pages/exception entries still associated with inode. 382c2d0ad00SVivek Goyal */ 383c2d0ad00SVivek Goyal inode_reclaim_dmap_range(fc->dax, inode, 0, -1); 384c2d0ad00SVivek Goyal WARN_ON(fi->dax->nr); 385c2d0ad00SVivek Goyal } 386c2d0ad00SVivek Goyal 387c2d0ad00SVivek Goyal static void fuse_fill_iomap_hole(struct iomap *iomap, loff_t length) 388c2d0ad00SVivek Goyal { 389c2d0ad00SVivek Goyal iomap->addr = IOMAP_NULL_ADDR; 390c2d0ad00SVivek Goyal iomap->length = length; 391c2d0ad00SVivek Goyal iomap->type = IOMAP_HOLE; 392c2d0ad00SVivek Goyal } 393c2d0ad00SVivek Goyal 394c2d0ad00SVivek Goyal static void fuse_fill_iomap(struct inode *inode, loff_t pos, loff_t length, 395c2d0ad00SVivek Goyal struct iomap *iomap, struct fuse_dax_mapping *dmap, 396c2d0ad00SVivek Goyal unsigned int flags) 397c2d0ad00SVivek Goyal { 398c2d0ad00SVivek Goyal loff_t offset, len; 399c2d0ad00SVivek Goyal loff_t i_size = i_size_read(inode); 400c2d0ad00SVivek Goyal 401c2d0ad00SVivek Goyal offset = pos - (dmap->itn.start << FUSE_DAX_SHIFT); 402c2d0ad00SVivek Goyal len = min(length, dmap->length - offset); 403c2d0ad00SVivek Goyal 404c2d0ad00SVivek Goyal /* If length is beyond end of file, truncate further */ 405c2d0ad00SVivek Goyal if (pos + len > i_size) 406c2d0ad00SVivek Goyal len = i_size - pos; 407c2d0ad00SVivek Goyal 408c2d0ad00SVivek Goyal if (len > 0) { 409c2d0ad00SVivek Goyal iomap->addr = dmap->window_offset + offset; 410c2d0ad00SVivek Goyal iomap->length = len; 411c2d0ad00SVivek Goyal if (flags & IOMAP_FAULT) 412c2d0ad00SVivek Goyal iomap->length = ALIGN(len, PAGE_SIZE); 413c2d0ad00SVivek Goyal iomap->type = IOMAP_MAPPED; 4149a752d18SVivek Goyal /* 4159a752d18SVivek Goyal * increace refcnt so that reclaim code knows this dmap is in 4169a752d18SVivek Goyal * use. This assumes fi->dax->sem mutex is held either 4179a752d18SVivek Goyal * shared/exclusive. 4189a752d18SVivek Goyal */ 4199a752d18SVivek Goyal refcount_inc(&dmap->refcnt); 4209a752d18SVivek Goyal 4219a752d18SVivek Goyal /* iomap->private should be NULL */ 4229a752d18SVivek Goyal WARN_ON_ONCE(iomap->private); 4239a752d18SVivek Goyal iomap->private = dmap; 424c2d0ad00SVivek Goyal } else { 425c2d0ad00SVivek Goyal /* Mapping beyond end of file is hole */ 426c2d0ad00SVivek Goyal fuse_fill_iomap_hole(iomap, length); 427c2d0ad00SVivek Goyal } 428c2d0ad00SVivek Goyal } 429c2d0ad00SVivek Goyal 430c2d0ad00SVivek Goyal static int fuse_setup_new_dax_mapping(struct inode *inode, loff_t pos, 431c2d0ad00SVivek Goyal loff_t length, unsigned int flags, 432c2d0ad00SVivek Goyal struct iomap *iomap) 433c2d0ad00SVivek Goyal { 434c2d0ad00SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 435c2d0ad00SVivek Goyal struct fuse_conn *fc = get_fuse_conn(inode); 436c2d0ad00SVivek Goyal struct fuse_conn_dax *fcd = fc->dax; 437c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap, *alloc_dmap = NULL; 438c2d0ad00SVivek Goyal int ret; 439c2d0ad00SVivek Goyal bool writable = flags & IOMAP_WRITE; 440c2d0ad00SVivek Goyal unsigned long start_idx = pos >> FUSE_DAX_SHIFT; 441c2d0ad00SVivek Goyal struct interval_tree_node *node; 442c2d0ad00SVivek Goyal 4439a752d18SVivek Goyal /* 4449a752d18SVivek Goyal * Can't do inline reclaim in fault path. We call 4459a752d18SVivek Goyal * dax_layout_busy_page() before we free a range. And 4469a752d18SVivek Goyal * fuse_wait_dax_page() drops fi->i_mmap_sem lock and requires it. 4479a752d18SVivek Goyal * In fault path we enter with fi->i_mmap_sem held and can't drop 4489a752d18SVivek Goyal * it. Also in fault path we hold fi->i_mmap_sem shared and not 4499a752d18SVivek Goyal * exclusive, so that creates further issues with fuse_wait_dax_page(). 4509a752d18SVivek Goyal * Hence return -EAGAIN and fuse_dax_fault() will wait for a memory 4519a752d18SVivek Goyal * range to become free and retry. 4529a752d18SVivek Goyal */ 4539a752d18SVivek Goyal if (flags & IOMAP_FAULT) { 454c2d0ad00SVivek Goyal alloc_dmap = alloc_dax_mapping(fcd); 455c2d0ad00SVivek Goyal if (!alloc_dmap) 4569a752d18SVivek Goyal return -EAGAIN; 4579a752d18SVivek Goyal } else { 4589a752d18SVivek Goyal alloc_dmap = alloc_dax_mapping_reclaim(fcd, inode); 4599a752d18SVivek Goyal if (IS_ERR(alloc_dmap)) 4609a752d18SVivek Goyal return PTR_ERR(alloc_dmap); 4619a752d18SVivek Goyal } 4629a752d18SVivek Goyal 4639a752d18SVivek Goyal /* If we are here, we should have memory allocated */ 4649a752d18SVivek Goyal if (WARN_ON(!alloc_dmap)) 465c2d0ad00SVivek Goyal return -EIO; 466c2d0ad00SVivek Goyal 467c2d0ad00SVivek Goyal /* 468c2d0ad00SVivek Goyal * Take write lock so that only one caller can try to setup mapping 469c2d0ad00SVivek Goyal * and other waits. 470c2d0ad00SVivek Goyal */ 471c2d0ad00SVivek Goyal down_write(&fi->dax->sem); 472c2d0ad00SVivek Goyal /* 473c2d0ad00SVivek Goyal * We dropped lock. Check again if somebody else setup 474c2d0ad00SVivek Goyal * mapping already. 475c2d0ad00SVivek Goyal */ 476c2d0ad00SVivek Goyal node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx); 477c2d0ad00SVivek Goyal if (node) { 478c2d0ad00SVivek Goyal dmap = node_to_dmap(node); 479c2d0ad00SVivek Goyal fuse_fill_iomap(inode, pos, length, iomap, dmap, flags); 480c2d0ad00SVivek Goyal dmap_add_to_free_pool(fcd, alloc_dmap); 481c2d0ad00SVivek Goyal up_write(&fi->dax->sem); 482c2d0ad00SVivek Goyal return 0; 483c2d0ad00SVivek Goyal } 484c2d0ad00SVivek Goyal 485c2d0ad00SVivek Goyal /* Setup one mapping */ 486c2d0ad00SVivek Goyal ret = fuse_setup_one_mapping(inode, pos >> FUSE_DAX_SHIFT, alloc_dmap, 487c2d0ad00SVivek Goyal writable, false); 488c2d0ad00SVivek Goyal if (ret < 0) { 489c2d0ad00SVivek Goyal dmap_add_to_free_pool(fcd, alloc_dmap); 490c2d0ad00SVivek Goyal up_write(&fi->dax->sem); 491c2d0ad00SVivek Goyal return ret; 492c2d0ad00SVivek Goyal } 493c2d0ad00SVivek Goyal fuse_fill_iomap(inode, pos, length, iomap, alloc_dmap, flags); 494c2d0ad00SVivek Goyal up_write(&fi->dax->sem); 495c2d0ad00SVivek Goyal return 0; 496c2d0ad00SVivek Goyal } 497c2d0ad00SVivek Goyal 498c2d0ad00SVivek Goyal static int fuse_upgrade_dax_mapping(struct inode *inode, loff_t pos, 499c2d0ad00SVivek Goyal loff_t length, unsigned int flags, 500c2d0ad00SVivek Goyal struct iomap *iomap) 501c2d0ad00SVivek Goyal { 502c2d0ad00SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 503c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap; 504c2d0ad00SVivek Goyal int ret; 505c2d0ad00SVivek Goyal unsigned long idx = pos >> FUSE_DAX_SHIFT; 506c2d0ad00SVivek Goyal struct interval_tree_node *node; 507c2d0ad00SVivek Goyal 508c2d0ad00SVivek Goyal /* 509c2d0ad00SVivek Goyal * Take exclusive lock so that only one caller can try to setup 510c2d0ad00SVivek Goyal * mapping and others wait. 511c2d0ad00SVivek Goyal */ 512c2d0ad00SVivek Goyal down_write(&fi->dax->sem); 513c2d0ad00SVivek Goyal node = interval_tree_iter_first(&fi->dax->tree, idx, idx); 514c2d0ad00SVivek Goyal 515c2d0ad00SVivek Goyal /* We are holding either inode lock or i_mmap_sem, and that should 5169a752d18SVivek Goyal * ensure that dmap can't be truncated. We are holding a reference 5179a752d18SVivek Goyal * on dmap and that should make sure it can't be reclaimed. So dmap 5189a752d18SVivek Goyal * should still be there in tree despite the fact we dropped and 5199a752d18SVivek Goyal * re-acquired the fi->dax->sem lock. 520c2d0ad00SVivek Goyal */ 521c2d0ad00SVivek Goyal ret = -EIO; 522c2d0ad00SVivek Goyal if (WARN_ON(!node)) 523c2d0ad00SVivek Goyal goto out_err; 524c2d0ad00SVivek Goyal 525c2d0ad00SVivek Goyal dmap = node_to_dmap(node); 526c2d0ad00SVivek Goyal 5279a752d18SVivek Goyal /* We took an extra reference on dmap to make sure its not reclaimd. 5289a752d18SVivek Goyal * Now we hold fi->dax->sem lock and that reference is not needed 5299a752d18SVivek Goyal * anymore. Drop it. 5309a752d18SVivek Goyal */ 5319a752d18SVivek Goyal if (refcount_dec_and_test(&dmap->refcnt)) { 5329a752d18SVivek Goyal /* refcount should not hit 0. This object only goes 5339a752d18SVivek Goyal * away when fuse connection goes away 5349a752d18SVivek Goyal */ 5359a752d18SVivek Goyal WARN_ON_ONCE(1); 5369a752d18SVivek Goyal } 5379a752d18SVivek Goyal 538c2d0ad00SVivek Goyal /* Maybe another thread already upgraded mapping while we were not 539c2d0ad00SVivek Goyal * holding lock. 540c2d0ad00SVivek Goyal */ 541c2d0ad00SVivek Goyal if (dmap->writable) { 542c2d0ad00SVivek Goyal ret = 0; 543c2d0ad00SVivek Goyal goto out_fill_iomap; 544c2d0ad00SVivek Goyal } 545c2d0ad00SVivek Goyal 546c2d0ad00SVivek Goyal ret = fuse_setup_one_mapping(inode, pos >> FUSE_DAX_SHIFT, dmap, true, 547c2d0ad00SVivek Goyal true); 548c2d0ad00SVivek Goyal if (ret < 0) 549c2d0ad00SVivek Goyal goto out_err; 550c2d0ad00SVivek Goyal out_fill_iomap: 551c2d0ad00SVivek Goyal fuse_fill_iomap(inode, pos, length, iomap, dmap, flags); 552c2d0ad00SVivek Goyal out_err: 553c2d0ad00SVivek Goyal up_write(&fi->dax->sem); 554c2d0ad00SVivek Goyal return ret; 555c2d0ad00SVivek Goyal } 556c2d0ad00SVivek Goyal 557c2d0ad00SVivek Goyal /* This is just for DAX and the mapping is ephemeral, do not use it for other 558c2d0ad00SVivek Goyal * purposes since there is no block device with a permanent mapping. 559c2d0ad00SVivek Goyal */ 560c2d0ad00SVivek Goyal static int fuse_iomap_begin(struct inode *inode, loff_t pos, loff_t length, 561c2d0ad00SVivek Goyal unsigned int flags, struct iomap *iomap, 562c2d0ad00SVivek Goyal struct iomap *srcmap) 563c2d0ad00SVivek Goyal { 564c2d0ad00SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 565c2d0ad00SVivek Goyal struct fuse_conn *fc = get_fuse_conn(inode); 566c2d0ad00SVivek Goyal struct fuse_dax_mapping *dmap; 567c2d0ad00SVivek Goyal bool writable = flags & IOMAP_WRITE; 568c2d0ad00SVivek Goyal unsigned long start_idx = pos >> FUSE_DAX_SHIFT; 569c2d0ad00SVivek Goyal struct interval_tree_node *node; 570c2d0ad00SVivek Goyal 571c2d0ad00SVivek Goyal /* We don't support FIEMAP */ 572c2d0ad00SVivek Goyal if (WARN_ON(flags & IOMAP_REPORT)) 573c2d0ad00SVivek Goyal return -EIO; 574c2d0ad00SVivek Goyal 575c2d0ad00SVivek Goyal iomap->offset = pos; 576c2d0ad00SVivek Goyal iomap->flags = 0; 577c2d0ad00SVivek Goyal iomap->bdev = NULL; 578c2d0ad00SVivek Goyal iomap->dax_dev = fc->dax->dev; 579c2d0ad00SVivek Goyal 580c2d0ad00SVivek Goyal /* 581c2d0ad00SVivek Goyal * Both read/write and mmap path can race here. So we need something 582c2d0ad00SVivek Goyal * to make sure if we are setting up mapping, then other path waits 583c2d0ad00SVivek Goyal * 584c2d0ad00SVivek Goyal * For now, use a semaphore for this. It probably needs to be 585c2d0ad00SVivek Goyal * optimized later. 586c2d0ad00SVivek Goyal */ 587c2d0ad00SVivek Goyal down_read(&fi->dax->sem); 588c2d0ad00SVivek Goyal node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx); 589c2d0ad00SVivek Goyal if (node) { 590c2d0ad00SVivek Goyal dmap = node_to_dmap(node); 591c2d0ad00SVivek Goyal if (writable && !dmap->writable) { 592c2d0ad00SVivek Goyal /* Upgrade read-only mapping to read-write. This will 593c2d0ad00SVivek Goyal * require exclusive fi->dax->sem lock as we don't want 594c2d0ad00SVivek Goyal * two threads to be trying to this simultaneously 595c2d0ad00SVivek Goyal * for same dmap. So drop shared lock and acquire 596c2d0ad00SVivek Goyal * exclusive lock. 5979a752d18SVivek Goyal * 5989a752d18SVivek Goyal * Before dropping fi->dax->sem lock, take reference 5999a752d18SVivek Goyal * on dmap so that its not freed by range reclaim. 600c2d0ad00SVivek Goyal */ 6019a752d18SVivek Goyal refcount_inc(&dmap->refcnt); 602c2d0ad00SVivek Goyal up_read(&fi->dax->sem); 603c2d0ad00SVivek Goyal pr_debug("%s: Upgrading mapping at offset 0x%llx length 0x%llx\n", 604c2d0ad00SVivek Goyal __func__, pos, length); 605c2d0ad00SVivek Goyal return fuse_upgrade_dax_mapping(inode, pos, length, 606c2d0ad00SVivek Goyal flags, iomap); 607c2d0ad00SVivek Goyal } else { 608c2d0ad00SVivek Goyal fuse_fill_iomap(inode, pos, length, iomap, dmap, flags); 609c2d0ad00SVivek Goyal up_read(&fi->dax->sem); 610c2d0ad00SVivek Goyal return 0; 611c2d0ad00SVivek Goyal } 612c2d0ad00SVivek Goyal } else { 613c2d0ad00SVivek Goyal up_read(&fi->dax->sem); 614c2d0ad00SVivek Goyal pr_debug("%s: no mapping at offset 0x%llx length 0x%llx\n", 615c2d0ad00SVivek Goyal __func__, pos, length); 616c2d0ad00SVivek Goyal if (pos >= i_size_read(inode)) 617c2d0ad00SVivek Goyal goto iomap_hole; 618c2d0ad00SVivek Goyal 619c2d0ad00SVivek Goyal return fuse_setup_new_dax_mapping(inode, pos, length, flags, 620c2d0ad00SVivek Goyal iomap); 621c2d0ad00SVivek Goyal } 622c2d0ad00SVivek Goyal 623c2d0ad00SVivek Goyal /* 624c2d0ad00SVivek Goyal * If read beyond end of file happnes, fs code seems to return 625c2d0ad00SVivek Goyal * it as hole 626c2d0ad00SVivek Goyal */ 627c2d0ad00SVivek Goyal iomap_hole: 628c2d0ad00SVivek Goyal fuse_fill_iomap_hole(iomap, length); 629c2d0ad00SVivek Goyal pr_debug("%s returning hole mapping. pos=0x%llx length_asked=0x%llx length_returned=0x%llx\n", 630c2d0ad00SVivek Goyal __func__, pos, length, iomap->length); 631c2d0ad00SVivek Goyal return 0; 632c2d0ad00SVivek Goyal } 633c2d0ad00SVivek Goyal 634c2d0ad00SVivek Goyal static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t length, 635c2d0ad00SVivek Goyal ssize_t written, unsigned int flags, 636c2d0ad00SVivek Goyal struct iomap *iomap) 637c2d0ad00SVivek Goyal { 6389a752d18SVivek Goyal struct fuse_dax_mapping *dmap = iomap->private; 6399a752d18SVivek Goyal 6409a752d18SVivek Goyal if (dmap) { 6419a752d18SVivek Goyal if (refcount_dec_and_test(&dmap->refcnt)) { 6429a752d18SVivek Goyal /* refcount should not hit 0. This object only goes 6439a752d18SVivek Goyal * away when fuse connection goes away 6449a752d18SVivek Goyal */ 6459a752d18SVivek Goyal WARN_ON_ONCE(1); 6469a752d18SVivek Goyal } 6479a752d18SVivek Goyal } 6489a752d18SVivek Goyal 649c2d0ad00SVivek Goyal /* DAX writes beyond end-of-file aren't handled using iomap, so the 650c2d0ad00SVivek Goyal * file size is unchanged and there is nothing to do here. 651c2d0ad00SVivek Goyal */ 652c2d0ad00SVivek Goyal return 0; 653c2d0ad00SVivek Goyal } 654c2d0ad00SVivek Goyal 655c2d0ad00SVivek Goyal static const struct iomap_ops fuse_iomap_ops = { 656c2d0ad00SVivek Goyal .iomap_begin = fuse_iomap_begin, 657c2d0ad00SVivek Goyal .iomap_end = fuse_iomap_end, 658c2d0ad00SVivek Goyal }; 659c2d0ad00SVivek Goyal 6606ae330caSVivek Goyal static void fuse_wait_dax_page(struct inode *inode) 6616ae330caSVivek Goyal { 6626ae330caSVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 6636ae330caSVivek Goyal 6646ae330caSVivek Goyal up_write(&fi->i_mmap_sem); 6656ae330caSVivek Goyal schedule(); 6666ae330caSVivek Goyal down_write(&fi->i_mmap_sem); 6676ae330caSVivek Goyal } 6686ae330caSVivek Goyal 6696ae330caSVivek Goyal /* Should be called with fi->i_mmap_sem lock held exclusively */ 6706ae330caSVivek Goyal static int __fuse_dax_break_layouts(struct inode *inode, bool *retry, 6716ae330caSVivek Goyal loff_t start, loff_t end) 6726ae330caSVivek Goyal { 6736ae330caSVivek Goyal struct page *page; 6746ae330caSVivek Goyal 6756ae330caSVivek Goyal page = dax_layout_busy_page_range(inode->i_mapping, start, end); 6766ae330caSVivek Goyal if (!page) 6776ae330caSVivek Goyal return 0; 6786ae330caSVivek Goyal 6796ae330caSVivek Goyal *retry = true; 6806ae330caSVivek Goyal return ___wait_var_event(&page->_refcount, 6816ae330caSVivek Goyal atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE, 6826ae330caSVivek Goyal 0, 0, fuse_wait_dax_page(inode)); 6836ae330caSVivek Goyal } 6846ae330caSVivek Goyal 6856ae330caSVivek Goyal /* dmap_end == 0 leads to unmapping of whole file */ 6866ae330caSVivek Goyal int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start, 6876ae330caSVivek Goyal u64 dmap_end) 6886ae330caSVivek Goyal { 6896ae330caSVivek Goyal bool retry; 6906ae330caSVivek Goyal int ret; 6916ae330caSVivek Goyal 6926ae330caSVivek Goyal do { 6936ae330caSVivek Goyal retry = false; 6946ae330caSVivek Goyal ret = __fuse_dax_break_layouts(inode, &retry, dmap_start, 6956ae330caSVivek Goyal dmap_end); 6966ae330caSVivek Goyal } while (ret == 0 && retry); 6976ae330caSVivek Goyal 6986ae330caSVivek Goyal return ret; 6996ae330caSVivek Goyal } 7006ae330caSVivek Goyal 701c2d0ad00SVivek Goyal ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to) 702c2d0ad00SVivek Goyal { 703c2d0ad00SVivek Goyal struct inode *inode = file_inode(iocb->ki_filp); 704c2d0ad00SVivek Goyal ssize_t ret; 705c2d0ad00SVivek Goyal 706c2d0ad00SVivek Goyal if (iocb->ki_flags & IOCB_NOWAIT) { 707c2d0ad00SVivek Goyal if (!inode_trylock_shared(inode)) 708c2d0ad00SVivek Goyal return -EAGAIN; 709c2d0ad00SVivek Goyal } else { 710c2d0ad00SVivek Goyal inode_lock_shared(inode); 711c2d0ad00SVivek Goyal } 712c2d0ad00SVivek Goyal 713c2d0ad00SVivek Goyal ret = dax_iomap_rw(iocb, to, &fuse_iomap_ops); 714c2d0ad00SVivek Goyal inode_unlock_shared(inode); 715c2d0ad00SVivek Goyal 716c2d0ad00SVivek Goyal /* TODO file_accessed(iocb->f_filp) */ 717c2d0ad00SVivek Goyal return ret; 718c2d0ad00SVivek Goyal } 719c2d0ad00SVivek Goyal 720c2d0ad00SVivek Goyal static bool file_extending_write(struct kiocb *iocb, struct iov_iter *from) 721c2d0ad00SVivek Goyal { 722c2d0ad00SVivek Goyal struct inode *inode = file_inode(iocb->ki_filp); 723c2d0ad00SVivek Goyal 724c2d0ad00SVivek Goyal return (iov_iter_rw(from) == WRITE && 725c2d0ad00SVivek Goyal ((iocb->ki_pos) >= i_size_read(inode) || 726c2d0ad00SVivek Goyal (iocb->ki_pos + iov_iter_count(from) > i_size_read(inode)))); 727c2d0ad00SVivek Goyal } 728c2d0ad00SVivek Goyal 729c2d0ad00SVivek Goyal static ssize_t fuse_dax_direct_write(struct kiocb *iocb, struct iov_iter *from) 730c2d0ad00SVivek Goyal { 731c2d0ad00SVivek Goyal struct inode *inode = file_inode(iocb->ki_filp); 732c2d0ad00SVivek Goyal struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb); 733c2d0ad00SVivek Goyal ssize_t ret; 734c2d0ad00SVivek Goyal 735c2d0ad00SVivek Goyal ret = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE); 736c2d0ad00SVivek Goyal if (ret < 0) 737c2d0ad00SVivek Goyal return ret; 738c2d0ad00SVivek Goyal 739c2d0ad00SVivek Goyal fuse_invalidate_attr(inode); 740c2d0ad00SVivek Goyal fuse_write_update_size(inode, iocb->ki_pos); 741c2d0ad00SVivek Goyal return ret; 742c2d0ad00SVivek Goyal } 743c2d0ad00SVivek Goyal 744c2d0ad00SVivek Goyal ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from) 745c2d0ad00SVivek Goyal { 746c2d0ad00SVivek Goyal struct inode *inode = file_inode(iocb->ki_filp); 747c2d0ad00SVivek Goyal ssize_t ret; 748c2d0ad00SVivek Goyal 749c2d0ad00SVivek Goyal if (iocb->ki_flags & IOCB_NOWAIT) { 750c2d0ad00SVivek Goyal if (!inode_trylock(inode)) 751c2d0ad00SVivek Goyal return -EAGAIN; 752c2d0ad00SVivek Goyal } else { 753c2d0ad00SVivek Goyal inode_lock(inode); 754c2d0ad00SVivek Goyal } 755c2d0ad00SVivek Goyal 756c2d0ad00SVivek Goyal ret = generic_write_checks(iocb, from); 757c2d0ad00SVivek Goyal if (ret <= 0) 758c2d0ad00SVivek Goyal goto out; 759c2d0ad00SVivek Goyal 760c2d0ad00SVivek Goyal ret = file_remove_privs(iocb->ki_filp); 761c2d0ad00SVivek Goyal if (ret) 762c2d0ad00SVivek Goyal goto out; 763c2d0ad00SVivek Goyal /* TODO file_update_time() but we don't want metadata I/O */ 764c2d0ad00SVivek Goyal 765c2d0ad00SVivek Goyal /* Do not use dax for file extending writes as write and on 766c2d0ad00SVivek Goyal * disk i_size increase are not atomic otherwise. 767c2d0ad00SVivek Goyal */ 768c2d0ad00SVivek Goyal if (file_extending_write(iocb, from)) 769c2d0ad00SVivek Goyal ret = fuse_dax_direct_write(iocb, from); 770c2d0ad00SVivek Goyal else 771c2d0ad00SVivek Goyal ret = dax_iomap_rw(iocb, from, &fuse_iomap_ops); 772c2d0ad00SVivek Goyal 773c2d0ad00SVivek Goyal out: 774c2d0ad00SVivek Goyal inode_unlock(inode); 775c2d0ad00SVivek Goyal 776c2d0ad00SVivek Goyal if (ret > 0) 777c2d0ad00SVivek Goyal ret = generic_write_sync(iocb, ret); 778c2d0ad00SVivek Goyal return ret; 779c2d0ad00SVivek Goyal } 780c2d0ad00SVivek Goyal 7819483e7d5SVivek Goyal static int fuse_dax_writepages(struct address_space *mapping, 7829483e7d5SVivek Goyal struct writeback_control *wbc) 7839483e7d5SVivek Goyal { 7849483e7d5SVivek Goyal 7859483e7d5SVivek Goyal struct inode *inode = mapping->host; 7869483e7d5SVivek Goyal struct fuse_conn *fc = get_fuse_conn(inode); 7879483e7d5SVivek Goyal 7889483e7d5SVivek Goyal return dax_writeback_mapping_range(mapping, fc->dax->dev, wbc); 7899483e7d5SVivek Goyal } 7909483e7d5SVivek Goyal 7912a9a609aSStefan Hajnoczi static vm_fault_t __fuse_dax_fault(struct vm_fault *vmf, 7922a9a609aSStefan Hajnoczi enum page_entry_size pe_size, bool write) 7932a9a609aSStefan Hajnoczi { 7942a9a609aSStefan Hajnoczi vm_fault_t ret; 7952a9a609aSStefan Hajnoczi struct inode *inode = file_inode(vmf->vma->vm_file); 7962a9a609aSStefan Hajnoczi struct super_block *sb = inode->i_sb; 7972a9a609aSStefan Hajnoczi pfn_t pfn; 7989a752d18SVivek Goyal int error = 0; 7999a752d18SVivek Goyal struct fuse_conn *fc = get_fuse_conn(inode); 8009a752d18SVivek Goyal struct fuse_conn_dax *fcd = fc->dax; 8019a752d18SVivek Goyal bool retry = false; 8022a9a609aSStefan Hajnoczi 8032a9a609aSStefan Hajnoczi if (write) 8042a9a609aSStefan Hajnoczi sb_start_pagefault(sb); 8059a752d18SVivek Goyal retry: 8069a752d18SVivek Goyal if (retry && !(fcd->nr_free_ranges > 0)) 8079a752d18SVivek Goyal wait_event(fcd->range_waitq, (fcd->nr_free_ranges > 0)); 8082a9a609aSStefan Hajnoczi 8096ae330caSVivek Goyal /* 8106ae330caSVivek Goyal * We need to serialize against not only truncate but also against 8116ae330caSVivek Goyal * fuse dax memory range reclaim. While a range is being reclaimed, 8126ae330caSVivek Goyal * we do not want any read/write/mmap to make progress and try 8136ae330caSVivek Goyal * to populate page cache or access memory we are trying to free. 8146ae330caSVivek Goyal */ 8156ae330caSVivek Goyal down_read(&get_fuse_inode(inode)->i_mmap_sem); 8169a752d18SVivek Goyal ret = dax_iomap_fault(vmf, pe_size, &pfn, &error, &fuse_iomap_ops); 8179a752d18SVivek Goyal if ((ret & VM_FAULT_ERROR) && error == -EAGAIN) { 8189a752d18SVivek Goyal error = 0; 8199a752d18SVivek Goyal retry = true; 8209a752d18SVivek Goyal up_read(&get_fuse_inode(inode)->i_mmap_sem); 8219a752d18SVivek Goyal goto retry; 8229a752d18SVivek Goyal } 8232a9a609aSStefan Hajnoczi 8242a9a609aSStefan Hajnoczi if (ret & VM_FAULT_NEEDDSYNC) 8252a9a609aSStefan Hajnoczi ret = dax_finish_sync_fault(vmf, pe_size, pfn); 8266ae330caSVivek Goyal up_read(&get_fuse_inode(inode)->i_mmap_sem); 8272a9a609aSStefan Hajnoczi 8282a9a609aSStefan Hajnoczi if (write) 8292a9a609aSStefan Hajnoczi sb_end_pagefault(sb); 8302a9a609aSStefan Hajnoczi 8312a9a609aSStefan Hajnoczi return ret; 8322a9a609aSStefan Hajnoczi } 8332a9a609aSStefan Hajnoczi 8342a9a609aSStefan Hajnoczi static vm_fault_t fuse_dax_fault(struct vm_fault *vmf) 8352a9a609aSStefan Hajnoczi { 8362a9a609aSStefan Hajnoczi return __fuse_dax_fault(vmf, PE_SIZE_PTE, 8372a9a609aSStefan Hajnoczi vmf->flags & FAULT_FLAG_WRITE); 8382a9a609aSStefan Hajnoczi } 8392a9a609aSStefan Hajnoczi 8402a9a609aSStefan Hajnoczi static vm_fault_t fuse_dax_huge_fault(struct vm_fault *vmf, 8412a9a609aSStefan Hajnoczi enum page_entry_size pe_size) 8422a9a609aSStefan Hajnoczi { 8432a9a609aSStefan Hajnoczi return __fuse_dax_fault(vmf, pe_size, vmf->flags & FAULT_FLAG_WRITE); 8442a9a609aSStefan Hajnoczi } 8452a9a609aSStefan Hajnoczi 8462a9a609aSStefan Hajnoczi static vm_fault_t fuse_dax_page_mkwrite(struct vm_fault *vmf) 8472a9a609aSStefan Hajnoczi { 8482a9a609aSStefan Hajnoczi return __fuse_dax_fault(vmf, PE_SIZE_PTE, true); 8492a9a609aSStefan Hajnoczi } 8502a9a609aSStefan Hajnoczi 8512a9a609aSStefan Hajnoczi static vm_fault_t fuse_dax_pfn_mkwrite(struct vm_fault *vmf) 8522a9a609aSStefan Hajnoczi { 8532a9a609aSStefan Hajnoczi return __fuse_dax_fault(vmf, PE_SIZE_PTE, true); 8542a9a609aSStefan Hajnoczi } 8552a9a609aSStefan Hajnoczi 8562a9a609aSStefan Hajnoczi static const struct vm_operations_struct fuse_dax_vm_ops = { 8572a9a609aSStefan Hajnoczi .fault = fuse_dax_fault, 8582a9a609aSStefan Hajnoczi .huge_fault = fuse_dax_huge_fault, 8592a9a609aSStefan Hajnoczi .page_mkwrite = fuse_dax_page_mkwrite, 8602a9a609aSStefan Hajnoczi .pfn_mkwrite = fuse_dax_pfn_mkwrite, 8612a9a609aSStefan Hajnoczi }; 8622a9a609aSStefan Hajnoczi 8632a9a609aSStefan Hajnoczi int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma) 8642a9a609aSStefan Hajnoczi { 8652a9a609aSStefan Hajnoczi file_accessed(file); 8662a9a609aSStefan Hajnoczi vma->vm_ops = &fuse_dax_vm_ops; 8672a9a609aSStefan Hajnoczi vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE; 8682a9a609aSStefan Hajnoczi return 0; 8692a9a609aSStefan Hajnoczi } 8702a9a609aSStefan Hajnoczi 8719a752d18SVivek Goyal static int dmap_writeback_invalidate(struct inode *inode, 8729a752d18SVivek Goyal struct fuse_dax_mapping *dmap) 8739a752d18SVivek Goyal { 8749a752d18SVivek Goyal int ret; 8759a752d18SVivek Goyal loff_t start_pos = dmap->itn.start << FUSE_DAX_SHIFT; 8769a752d18SVivek Goyal loff_t end_pos = (start_pos + FUSE_DAX_SZ - 1); 8779a752d18SVivek Goyal 8789a752d18SVivek Goyal ret = filemap_fdatawrite_range(inode->i_mapping, start_pos, end_pos); 8799a752d18SVivek Goyal if (ret) { 8809a752d18SVivek Goyal pr_debug("fuse: filemap_fdatawrite_range() failed. err=%d start_pos=0x%llx, end_pos=0x%llx\n", 8819a752d18SVivek Goyal ret, start_pos, end_pos); 8829a752d18SVivek Goyal return ret; 8839a752d18SVivek Goyal } 8849a752d18SVivek Goyal 8859a752d18SVivek Goyal ret = invalidate_inode_pages2_range(inode->i_mapping, 8869a752d18SVivek Goyal start_pos >> PAGE_SHIFT, 8879a752d18SVivek Goyal end_pos >> PAGE_SHIFT); 8889a752d18SVivek Goyal if (ret) 8899a752d18SVivek Goyal pr_debug("fuse: invalidate_inode_pages2_range() failed err=%d\n", 8909a752d18SVivek Goyal ret); 8919a752d18SVivek Goyal 8929a752d18SVivek Goyal return ret; 8939a752d18SVivek Goyal } 8949a752d18SVivek Goyal 8959a752d18SVivek Goyal static int reclaim_one_dmap_locked(struct inode *inode, 8969a752d18SVivek Goyal struct fuse_dax_mapping *dmap) 8979a752d18SVivek Goyal { 8989a752d18SVivek Goyal int ret; 8999a752d18SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 9009a752d18SVivek Goyal 9019a752d18SVivek Goyal /* 9029a752d18SVivek Goyal * igrab() was done to make sure inode won't go under us, and this 9039a752d18SVivek Goyal * further avoids the race with evict(). 9049a752d18SVivek Goyal */ 9059a752d18SVivek Goyal ret = dmap_writeback_invalidate(inode, dmap); 9069a752d18SVivek Goyal if (ret) 9079a752d18SVivek Goyal return ret; 9089a752d18SVivek Goyal 9099a752d18SVivek Goyal /* Remove dax mapping from inode interval tree now */ 9109a752d18SVivek Goyal interval_tree_remove(&dmap->itn, &fi->dax->tree); 9119a752d18SVivek Goyal fi->dax->nr--; 9129a752d18SVivek Goyal 9139a752d18SVivek Goyal /* It is possible that umount/shutdown has killed the fuse connection 9149a752d18SVivek Goyal * and worker thread is trying to reclaim memory in parallel. Don't 9159a752d18SVivek Goyal * warn in that case. 9169a752d18SVivek Goyal */ 9179a752d18SVivek Goyal ret = dmap_removemapping_one(inode, dmap); 9189a752d18SVivek Goyal if (ret && ret != -ENOTCONN) { 9199a752d18SVivek Goyal pr_warn("Failed to remove mapping. offset=0x%llx len=0x%llx ret=%d\n", 9209a752d18SVivek Goyal dmap->window_offset, dmap->length, ret); 9219a752d18SVivek Goyal } 9229a752d18SVivek Goyal return 0; 9239a752d18SVivek Goyal } 9249a752d18SVivek Goyal 9259a752d18SVivek Goyal /* Find first mapped dmap for an inode and return file offset. Caller needs 9269a752d18SVivek Goyal * to hold fi->dax->sem lock either shared or exclusive. 9279a752d18SVivek Goyal */ 9289a752d18SVivek Goyal static struct fuse_dax_mapping *inode_lookup_first_dmap(struct inode *inode) 9299a752d18SVivek Goyal { 9309a752d18SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 9319a752d18SVivek Goyal struct fuse_dax_mapping *dmap; 9329a752d18SVivek Goyal struct interval_tree_node *node; 9339a752d18SVivek Goyal 9349a752d18SVivek Goyal for (node = interval_tree_iter_first(&fi->dax->tree, 0, -1); node; 9359a752d18SVivek Goyal node = interval_tree_iter_next(node, 0, -1)) { 9369a752d18SVivek Goyal dmap = node_to_dmap(node); 9379a752d18SVivek Goyal /* still in use. */ 9389a752d18SVivek Goyal if (refcount_read(&dmap->refcnt) > 1) 9399a752d18SVivek Goyal continue; 9409a752d18SVivek Goyal 9419a752d18SVivek Goyal return dmap; 9429a752d18SVivek Goyal } 9439a752d18SVivek Goyal 9449a752d18SVivek Goyal return NULL; 9459a752d18SVivek Goyal } 9469a752d18SVivek Goyal 9479a752d18SVivek Goyal /* 9489a752d18SVivek Goyal * Find first mapping in the tree and free it and return it. Do not add 9499a752d18SVivek Goyal * it back to free pool. 9509a752d18SVivek Goyal */ 9519a752d18SVivek Goyal static struct fuse_dax_mapping * 9529a752d18SVivek Goyal inode_inline_reclaim_one_dmap(struct fuse_conn_dax *fcd, struct inode *inode, 9539a752d18SVivek Goyal bool *retry) 9549a752d18SVivek Goyal { 9559a752d18SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 9569a752d18SVivek Goyal struct fuse_dax_mapping *dmap; 9579a752d18SVivek Goyal u64 dmap_start, dmap_end; 9589a752d18SVivek Goyal unsigned long start_idx; 9599a752d18SVivek Goyal int ret; 9609a752d18SVivek Goyal struct interval_tree_node *node; 9619a752d18SVivek Goyal 9629a752d18SVivek Goyal down_write(&fi->i_mmap_sem); 9639a752d18SVivek Goyal 9649a752d18SVivek Goyal /* Lookup a dmap and corresponding file offset to reclaim. */ 9659a752d18SVivek Goyal down_read(&fi->dax->sem); 9669a752d18SVivek Goyal dmap = inode_lookup_first_dmap(inode); 9679a752d18SVivek Goyal if (dmap) { 9689a752d18SVivek Goyal start_idx = dmap->itn.start; 9699a752d18SVivek Goyal dmap_start = start_idx << FUSE_DAX_SHIFT; 9709a752d18SVivek Goyal dmap_end = dmap_start + FUSE_DAX_SZ - 1; 9719a752d18SVivek Goyal } 9729a752d18SVivek Goyal up_read(&fi->dax->sem); 9739a752d18SVivek Goyal 9749a752d18SVivek Goyal if (!dmap) 9759a752d18SVivek Goyal goto out_mmap_sem; 9769a752d18SVivek Goyal /* 9779a752d18SVivek Goyal * Make sure there are no references to inode pages using 9789a752d18SVivek Goyal * get_user_pages() 9799a752d18SVivek Goyal */ 9809a752d18SVivek Goyal ret = fuse_dax_break_layouts(inode, dmap_start, dmap_end); 9819a752d18SVivek Goyal if (ret) { 9829a752d18SVivek Goyal pr_debug("fuse: fuse_dax_break_layouts() failed. err=%d\n", 9839a752d18SVivek Goyal ret); 9849a752d18SVivek Goyal dmap = ERR_PTR(ret); 9859a752d18SVivek Goyal goto out_mmap_sem; 9869a752d18SVivek Goyal } 9879a752d18SVivek Goyal 9889a752d18SVivek Goyal down_write(&fi->dax->sem); 9899a752d18SVivek Goyal node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx); 9909a752d18SVivek Goyal /* Range already got reclaimed by somebody else */ 9919a752d18SVivek Goyal if (!node) { 9929a752d18SVivek Goyal if (retry) 9939a752d18SVivek Goyal *retry = true; 9949a752d18SVivek Goyal goto out_write_dmap_sem; 9959a752d18SVivek Goyal } 9969a752d18SVivek Goyal 9979a752d18SVivek Goyal dmap = node_to_dmap(node); 9989a752d18SVivek Goyal /* still in use. */ 9999a752d18SVivek Goyal if (refcount_read(&dmap->refcnt) > 1) { 10009a752d18SVivek Goyal dmap = NULL; 10019a752d18SVivek Goyal if (retry) 10029a752d18SVivek Goyal *retry = true; 10039a752d18SVivek Goyal goto out_write_dmap_sem; 10049a752d18SVivek Goyal } 10059a752d18SVivek Goyal 10069a752d18SVivek Goyal ret = reclaim_one_dmap_locked(inode, dmap); 10079a752d18SVivek Goyal if (ret < 0) { 10089a752d18SVivek Goyal dmap = ERR_PTR(ret); 10099a752d18SVivek Goyal goto out_write_dmap_sem; 10109a752d18SVivek Goyal } 10119a752d18SVivek Goyal 10129a752d18SVivek Goyal /* Clean up dmap. Do not add back to free list */ 10139a752d18SVivek Goyal dmap_remove_busy_list(fcd, dmap); 10149a752d18SVivek Goyal dmap->inode = NULL; 10159a752d18SVivek Goyal dmap->itn.start = dmap->itn.last = 0; 10169a752d18SVivek Goyal 10179a752d18SVivek Goyal pr_debug("fuse: %s: inline reclaimed memory range. inode=%p, window_offset=0x%llx, length=0x%llx\n", 10189a752d18SVivek Goyal __func__, inode, dmap->window_offset, dmap->length); 10199a752d18SVivek Goyal 10209a752d18SVivek Goyal out_write_dmap_sem: 10219a752d18SVivek Goyal up_write(&fi->dax->sem); 10229a752d18SVivek Goyal out_mmap_sem: 10239a752d18SVivek Goyal up_write(&fi->i_mmap_sem); 10249a752d18SVivek Goyal return dmap; 10259a752d18SVivek Goyal } 10269a752d18SVivek Goyal 10279a752d18SVivek Goyal static struct fuse_dax_mapping * 10289a752d18SVivek Goyal alloc_dax_mapping_reclaim(struct fuse_conn_dax *fcd, struct inode *inode) 10299a752d18SVivek Goyal { 10309a752d18SVivek Goyal struct fuse_dax_mapping *dmap; 10319a752d18SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 10329a752d18SVivek Goyal 10339a752d18SVivek Goyal while (1) { 10349a752d18SVivek Goyal bool retry = false; 10359a752d18SVivek Goyal 10369a752d18SVivek Goyal dmap = alloc_dax_mapping(fcd); 10379a752d18SVivek Goyal if (dmap) 10389a752d18SVivek Goyal return dmap; 10399a752d18SVivek Goyal 10409a752d18SVivek Goyal dmap = inode_inline_reclaim_one_dmap(fcd, inode, &retry); 10419a752d18SVivek Goyal /* 10429a752d18SVivek Goyal * Either we got a mapping or it is an error, return in both 10439a752d18SVivek Goyal * the cases. 10449a752d18SVivek Goyal */ 10459a752d18SVivek Goyal if (dmap) 10469a752d18SVivek Goyal return dmap; 10479a752d18SVivek Goyal 10489a752d18SVivek Goyal /* If we could not reclaim a mapping because it 10499a752d18SVivek Goyal * had a reference or some other temporary failure, 10509a752d18SVivek Goyal * Try again. We want to give up inline reclaim only 10519a752d18SVivek Goyal * if there is no range assigned to this node. Otherwise 10529a752d18SVivek Goyal * if a deadlock is possible if we sleep with fi->i_mmap_sem 10539a752d18SVivek Goyal * held and worker to free memory can't make progress due 10549a752d18SVivek Goyal * to unavailability of fi->i_mmap_sem lock. So sleep 10559a752d18SVivek Goyal * only if fi->dax->nr=0 10569a752d18SVivek Goyal */ 10579a752d18SVivek Goyal if (retry) 10589a752d18SVivek Goyal continue; 10599a752d18SVivek Goyal /* 10609a752d18SVivek Goyal * There are no mappings which can be reclaimed. Wait for one. 10619a752d18SVivek Goyal * We are not holding fi->dax->sem. So it is possible 10629a752d18SVivek Goyal * that range gets added now. But as we are not holding 10639a752d18SVivek Goyal * fi->i_mmap_sem, worker should still be able to free up 10649a752d18SVivek Goyal * a range and wake us up. 10659a752d18SVivek Goyal */ 10669a752d18SVivek Goyal if (!fi->dax->nr && !(fcd->nr_free_ranges > 0)) { 10679a752d18SVivek Goyal if (wait_event_killable_exclusive(fcd->range_waitq, 10689a752d18SVivek Goyal (fcd->nr_free_ranges > 0))) { 10699a752d18SVivek Goyal return ERR_PTR(-EINTR); 10709a752d18SVivek Goyal } 10719a752d18SVivek Goyal } 10729a752d18SVivek Goyal } 10739a752d18SVivek Goyal } 10749a752d18SVivek Goyal 10759a752d18SVivek Goyal static int lookup_and_reclaim_dmap_locked(struct fuse_conn_dax *fcd, 10769a752d18SVivek Goyal struct inode *inode, 10779a752d18SVivek Goyal unsigned long start_idx) 10789a752d18SVivek Goyal { 10799a752d18SVivek Goyal int ret; 10809a752d18SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 10819a752d18SVivek Goyal struct fuse_dax_mapping *dmap; 10829a752d18SVivek Goyal struct interval_tree_node *node; 10839a752d18SVivek Goyal 10849a752d18SVivek Goyal /* Find fuse dax mapping at file offset inode. */ 10859a752d18SVivek Goyal node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx); 10869a752d18SVivek Goyal 10879a752d18SVivek Goyal /* Range already got cleaned up by somebody else */ 10889a752d18SVivek Goyal if (!node) 10899a752d18SVivek Goyal return 0; 10909a752d18SVivek Goyal dmap = node_to_dmap(node); 10919a752d18SVivek Goyal 10929a752d18SVivek Goyal /* still in use. */ 10939a752d18SVivek Goyal if (refcount_read(&dmap->refcnt) > 1) 10949a752d18SVivek Goyal return 0; 10959a752d18SVivek Goyal 10969a752d18SVivek Goyal ret = reclaim_one_dmap_locked(inode, dmap); 10979a752d18SVivek Goyal if (ret < 0) 10989a752d18SVivek Goyal return ret; 10999a752d18SVivek Goyal 11009a752d18SVivek Goyal /* Cleanup dmap entry and add back to free list */ 11019a752d18SVivek Goyal spin_lock(&fcd->lock); 11029a752d18SVivek Goyal dmap_reinit_add_to_free_pool(fcd, dmap); 11039a752d18SVivek Goyal spin_unlock(&fcd->lock); 11049a752d18SVivek Goyal return ret; 11059a752d18SVivek Goyal } 11069a752d18SVivek Goyal 11079a752d18SVivek Goyal /* 11089a752d18SVivek Goyal * Free a range of memory. 11099a752d18SVivek Goyal * Locking: 11109a752d18SVivek Goyal * 1. Take fi->i_mmap_sem to block dax faults. 11119a752d18SVivek Goyal * 2. Take fi->dax->sem to protect interval tree and also to make sure 11129a752d18SVivek Goyal * read/write can not reuse a dmap which we might be freeing. 11139a752d18SVivek Goyal */ 11149a752d18SVivek Goyal static int lookup_and_reclaim_dmap(struct fuse_conn_dax *fcd, 11159a752d18SVivek Goyal struct inode *inode, 11169a752d18SVivek Goyal unsigned long start_idx, 11179a752d18SVivek Goyal unsigned long end_idx) 11189a752d18SVivek Goyal { 11199a752d18SVivek Goyal int ret; 11209a752d18SVivek Goyal struct fuse_inode *fi = get_fuse_inode(inode); 11219a752d18SVivek Goyal loff_t dmap_start = start_idx << FUSE_DAX_SHIFT; 11229a752d18SVivek Goyal loff_t dmap_end = (dmap_start + FUSE_DAX_SZ) - 1; 11239a752d18SVivek Goyal 11249a752d18SVivek Goyal down_write(&fi->i_mmap_sem); 11259a752d18SVivek Goyal ret = fuse_dax_break_layouts(inode, dmap_start, dmap_end); 11269a752d18SVivek Goyal if (ret) { 11279a752d18SVivek Goyal pr_debug("virtio_fs: fuse_dax_break_layouts() failed. err=%d\n", 11289a752d18SVivek Goyal ret); 11299a752d18SVivek Goyal goto out_mmap_sem; 11309a752d18SVivek Goyal } 11319a752d18SVivek Goyal 11329a752d18SVivek Goyal down_write(&fi->dax->sem); 11339a752d18SVivek Goyal ret = lookup_and_reclaim_dmap_locked(fcd, inode, start_idx); 11349a752d18SVivek Goyal up_write(&fi->dax->sem); 11359a752d18SVivek Goyal out_mmap_sem: 11369a752d18SVivek Goyal up_write(&fi->i_mmap_sem); 11379a752d18SVivek Goyal return ret; 11389a752d18SVivek Goyal } 11399a752d18SVivek Goyal 11409a752d18SVivek Goyal static int try_to_free_dmap_chunks(struct fuse_conn_dax *fcd, 11419a752d18SVivek Goyal unsigned long nr_to_free) 11429a752d18SVivek Goyal { 11439a752d18SVivek Goyal struct fuse_dax_mapping *dmap, *pos, *temp; 11449a752d18SVivek Goyal int ret, nr_freed = 0; 11459a752d18SVivek Goyal unsigned long start_idx = 0, end_idx = 0; 11469a752d18SVivek Goyal struct inode *inode = NULL; 11479a752d18SVivek Goyal 11489a752d18SVivek Goyal /* Pick first busy range and free it for now*/ 11499a752d18SVivek Goyal while (1) { 11509a752d18SVivek Goyal if (nr_freed >= nr_to_free) 11519a752d18SVivek Goyal break; 11529a752d18SVivek Goyal 11539a752d18SVivek Goyal dmap = NULL; 11549a752d18SVivek Goyal spin_lock(&fcd->lock); 11559a752d18SVivek Goyal 11569a752d18SVivek Goyal if (!fcd->nr_busy_ranges) { 11579a752d18SVivek Goyal spin_unlock(&fcd->lock); 11589a752d18SVivek Goyal return 0; 11599a752d18SVivek Goyal } 11609a752d18SVivek Goyal 11619a752d18SVivek Goyal list_for_each_entry_safe(pos, temp, &fcd->busy_ranges, 11629a752d18SVivek Goyal busy_list) { 11639a752d18SVivek Goyal /* skip this range if it's in use. */ 11649a752d18SVivek Goyal if (refcount_read(&pos->refcnt) > 1) 11659a752d18SVivek Goyal continue; 11669a752d18SVivek Goyal 11679a752d18SVivek Goyal inode = igrab(pos->inode); 11689a752d18SVivek Goyal /* 11699a752d18SVivek Goyal * This inode is going away. That will free 11709a752d18SVivek Goyal * up all the ranges anyway, continue to 11719a752d18SVivek Goyal * next range. 11729a752d18SVivek Goyal */ 11739a752d18SVivek Goyal if (!inode) 11749a752d18SVivek Goyal continue; 11759a752d18SVivek Goyal /* 11769a752d18SVivek Goyal * Take this element off list and add it tail. If 11779a752d18SVivek Goyal * this element can't be freed, it will help with 11789a752d18SVivek Goyal * selecting new element in next iteration of loop. 11799a752d18SVivek Goyal */ 11809a752d18SVivek Goyal dmap = pos; 11819a752d18SVivek Goyal list_move_tail(&dmap->busy_list, &fcd->busy_ranges); 11829a752d18SVivek Goyal start_idx = end_idx = dmap->itn.start; 11839a752d18SVivek Goyal break; 11849a752d18SVivek Goyal } 11859a752d18SVivek Goyal spin_unlock(&fcd->lock); 11869a752d18SVivek Goyal if (!dmap) 11879a752d18SVivek Goyal return 0; 11889a752d18SVivek Goyal 11899a752d18SVivek Goyal ret = lookup_and_reclaim_dmap(fcd, inode, start_idx, end_idx); 11909a752d18SVivek Goyal iput(inode); 11919a752d18SVivek Goyal if (ret) 11929a752d18SVivek Goyal return ret; 11939a752d18SVivek Goyal nr_freed++; 11949a752d18SVivek Goyal } 11959a752d18SVivek Goyal return 0; 11969a752d18SVivek Goyal } 11979a752d18SVivek Goyal 11989a752d18SVivek Goyal static void fuse_dax_free_mem_worker(struct work_struct *work) 11999a752d18SVivek Goyal { 12009a752d18SVivek Goyal int ret; 12019a752d18SVivek Goyal struct fuse_conn_dax *fcd = container_of(work, struct fuse_conn_dax, 12029a752d18SVivek Goyal free_work.work); 12039a752d18SVivek Goyal ret = try_to_free_dmap_chunks(fcd, FUSE_DAX_RECLAIM_CHUNK); 12049a752d18SVivek Goyal if (ret) { 12059a752d18SVivek Goyal pr_debug("fuse: try_to_free_dmap_chunks() failed with err=%d\n", 12069a752d18SVivek Goyal ret); 12079a752d18SVivek Goyal } 12089a752d18SVivek Goyal 12099a752d18SVivek Goyal /* If number of free ranges are still below threhold, requeue */ 12109a752d18SVivek Goyal kick_dmap_free_worker(fcd, 1); 12119a752d18SVivek Goyal } 12129a752d18SVivek Goyal 121345f2348eSVivek Goyal static void fuse_free_dax_mem_ranges(struct list_head *mem_list) 121445f2348eSVivek Goyal { 121545f2348eSVivek Goyal struct fuse_dax_mapping *range, *temp; 121645f2348eSVivek Goyal 121745f2348eSVivek Goyal /* Free All allocated elements */ 121845f2348eSVivek Goyal list_for_each_entry_safe(range, temp, mem_list, list) { 121945f2348eSVivek Goyal list_del(&range->list); 1220d0cfb9dcSVivek Goyal if (!list_empty(&range->busy_list)) 1221d0cfb9dcSVivek Goyal list_del(&range->busy_list); 122245f2348eSVivek Goyal kfree(range); 122345f2348eSVivek Goyal } 122445f2348eSVivek Goyal } 122545f2348eSVivek Goyal 12261dd53957SVivek Goyal void fuse_dax_conn_free(struct fuse_conn *fc) 12271dd53957SVivek Goyal { 122845f2348eSVivek Goyal if (fc->dax) { 122945f2348eSVivek Goyal fuse_free_dax_mem_ranges(&fc->dax->free_ranges); 12301dd53957SVivek Goyal kfree(fc->dax); 12311dd53957SVivek Goyal } 123245f2348eSVivek Goyal } 123345f2348eSVivek Goyal 123445f2348eSVivek Goyal static int fuse_dax_mem_range_init(struct fuse_conn_dax *fcd) 123545f2348eSVivek Goyal { 123645f2348eSVivek Goyal long nr_pages, nr_ranges; 123745f2348eSVivek Goyal void *kaddr; 123845f2348eSVivek Goyal pfn_t pfn; 123945f2348eSVivek Goyal struct fuse_dax_mapping *range; 124045f2348eSVivek Goyal int ret, id; 124145f2348eSVivek Goyal size_t dax_size = -1; 124245f2348eSVivek Goyal unsigned long i; 124345f2348eSVivek Goyal 12449a752d18SVivek Goyal init_waitqueue_head(&fcd->range_waitq); 124545f2348eSVivek Goyal INIT_LIST_HEAD(&fcd->free_ranges); 1246d0cfb9dcSVivek Goyal INIT_LIST_HEAD(&fcd->busy_ranges); 12479a752d18SVivek Goyal INIT_DELAYED_WORK(&fcd->free_work, fuse_dax_free_mem_worker); 12489a752d18SVivek Goyal 124945f2348eSVivek Goyal id = dax_read_lock(); 125045f2348eSVivek Goyal nr_pages = dax_direct_access(fcd->dev, 0, PHYS_PFN(dax_size), &kaddr, 125145f2348eSVivek Goyal &pfn); 125245f2348eSVivek Goyal dax_read_unlock(id); 125345f2348eSVivek Goyal if (nr_pages < 0) { 125445f2348eSVivek Goyal pr_debug("dax_direct_access() returned %ld\n", nr_pages); 125545f2348eSVivek Goyal return nr_pages; 125645f2348eSVivek Goyal } 125745f2348eSVivek Goyal 125845f2348eSVivek Goyal nr_ranges = nr_pages/FUSE_DAX_PAGES; 125945f2348eSVivek Goyal pr_debug("%s: dax mapped %ld pages. nr_ranges=%ld\n", 126045f2348eSVivek Goyal __func__, nr_pages, nr_ranges); 126145f2348eSVivek Goyal 126245f2348eSVivek Goyal for (i = 0; i < nr_ranges; i++) { 126345f2348eSVivek Goyal range = kzalloc(sizeof(struct fuse_dax_mapping), GFP_KERNEL); 126445f2348eSVivek Goyal ret = -ENOMEM; 126545f2348eSVivek Goyal if (!range) 126645f2348eSVivek Goyal goto out_err; 126745f2348eSVivek Goyal 126845f2348eSVivek Goyal /* TODO: This offset only works if virtio-fs driver is not 126945f2348eSVivek Goyal * having some memory hidden at the beginning. This needs 127045f2348eSVivek Goyal * better handling 127145f2348eSVivek Goyal */ 127245f2348eSVivek Goyal range->window_offset = i * FUSE_DAX_SZ; 127345f2348eSVivek Goyal range->length = FUSE_DAX_SZ; 1274d0cfb9dcSVivek Goyal INIT_LIST_HEAD(&range->busy_list); 12759a752d18SVivek Goyal refcount_set(&range->refcnt, 1); 127645f2348eSVivek Goyal list_add_tail(&range->list, &fcd->free_ranges); 127745f2348eSVivek Goyal } 127845f2348eSVivek Goyal 127945f2348eSVivek Goyal fcd->nr_free_ranges = nr_ranges; 12809a752d18SVivek Goyal fcd->nr_ranges = nr_ranges; 128145f2348eSVivek Goyal return 0; 128245f2348eSVivek Goyal out_err: 128345f2348eSVivek Goyal /* Free All allocated elements */ 128445f2348eSVivek Goyal fuse_free_dax_mem_ranges(&fcd->free_ranges); 128545f2348eSVivek Goyal return ret; 128645f2348eSVivek Goyal } 12871dd53957SVivek Goyal 12881dd53957SVivek Goyal int fuse_dax_conn_alloc(struct fuse_conn *fc, struct dax_device *dax_dev) 12891dd53957SVivek Goyal { 12901dd53957SVivek Goyal struct fuse_conn_dax *fcd; 129145f2348eSVivek Goyal int err; 12921dd53957SVivek Goyal 12931dd53957SVivek Goyal if (!dax_dev) 12941dd53957SVivek Goyal return 0; 12951dd53957SVivek Goyal 12961dd53957SVivek Goyal fcd = kzalloc(sizeof(*fcd), GFP_KERNEL); 12971dd53957SVivek Goyal if (!fcd) 12981dd53957SVivek Goyal return -ENOMEM; 12991dd53957SVivek Goyal 1300c2d0ad00SVivek Goyal spin_lock_init(&fcd->lock); 13011dd53957SVivek Goyal fcd->dev = dax_dev; 130245f2348eSVivek Goyal err = fuse_dax_mem_range_init(fcd); 130345f2348eSVivek Goyal if (err) { 130445f2348eSVivek Goyal kfree(fcd); 130545f2348eSVivek Goyal return err; 130645f2348eSVivek Goyal } 13071dd53957SVivek Goyal 13081dd53957SVivek Goyal fc->dax = fcd; 13091dd53957SVivek Goyal return 0; 13101dd53957SVivek Goyal } 1311fd1a1dc6SStefan Hajnoczi 1312c2d0ad00SVivek Goyal bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi) 1313c2d0ad00SVivek Goyal { 1314c2d0ad00SVivek Goyal struct fuse_conn *fc = get_fuse_conn_super(sb); 1315c2d0ad00SVivek Goyal 1316c2d0ad00SVivek Goyal fi->dax = NULL; 1317c2d0ad00SVivek Goyal if (fc->dax) { 1318c2d0ad00SVivek Goyal fi->dax = kzalloc(sizeof(*fi->dax), GFP_KERNEL_ACCOUNT); 1319c2d0ad00SVivek Goyal if (!fi->dax) 1320c2d0ad00SVivek Goyal return false; 1321c2d0ad00SVivek Goyal 1322c2d0ad00SVivek Goyal init_rwsem(&fi->dax->sem); 1323c2d0ad00SVivek Goyal fi->dax->tree = RB_ROOT_CACHED; 1324c2d0ad00SVivek Goyal } 1325c2d0ad00SVivek Goyal 1326c2d0ad00SVivek Goyal return true; 1327c2d0ad00SVivek Goyal } 1328c2d0ad00SVivek Goyal 13299483e7d5SVivek Goyal static const struct address_space_operations fuse_dax_file_aops = { 13309483e7d5SVivek Goyal .writepages = fuse_dax_writepages, 13319483e7d5SVivek Goyal .direct_IO = noop_direct_IO, 13329483e7d5SVivek Goyal .set_page_dirty = noop_set_page_dirty, 13339483e7d5SVivek Goyal .invalidatepage = noop_invalidatepage, 13349483e7d5SVivek Goyal }; 13359483e7d5SVivek Goyal 1336c2d0ad00SVivek Goyal void fuse_dax_inode_init(struct inode *inode) 1337c2d0ad00SVivek Goyal { 1338c2d0ad00SVivek Goyal struct fuse_conn *fc = get_fuse_conn(inode); 1339c2d0ad00SVivek Goyal 1340c2d0ad00SVivek Goyal if (!fc->dax) 1341c2d0ad00SVivek Goyal return; 1342c2d0ad00SVivek Goyal 1343c2d0ad00SVivek Goyal inode->i_flags |= S_DAX; 13449483e7d5SVivek Goyal inode->i_data.a_ops = &fuse_dax_file_aops; 1345c2d0ad00SVivek Goyal } 1346c2d0ad00SVivek Goyal 1347fd1a1dc6SStefan Hajnoczi bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment) 1348fd1a1dc6SStefan Hajnoczi { 1349fd1a1dc6SStefan Hajnoczi if (fc->dax && (map_alignment > FUSE_DAX_SHIFT)) { 1350fd1a1dc6SStefan Hajnoczi pr_warn("FUSE: map_alignment %u incompatible with dax mem range size %u\n", 1351fd1a1dc6SStefan Hajnoczi map_alignment, FUSE_DAX_SZ); 1352fd1a1dc6SStefan Hajnoczi return false; 1353fd1a1dc6SStefan Hajnoczi } 1354fd1a1dc6SStefan Hajnoczi return true; 1355fd1a1dc6SStefan Hajnoczi } 13569a752d18SVivek Goyal 13579a752d18SVivek Goyal void fuse_dax_cancel_work(struct fuse_conn *fc) 13589a752d18SVivek Goyal { 13599a752d18SVivek Goyal struct fuse_conn_dax *fcd = fc->dax; 13609a752d18SVivek Goyal 13619a752d18SVivek Goyal if (fcd) 13629a752d18SVivek Goyal cancel_delayed_work_sync(&fcd->free_work); 13639a752d18SVivek Goyal 13649a752d18SVivek Goyal } 13659a752d18SVivek Goyal EXPORT_SYMBOL_GPL(fuse_dax_cancel_work); 1366