xref: /linux/fs/fuse/dax.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
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>
123a6b2162SMatthew Wilcox (Oracle) #include <linux/pagemap.h>
1345f2348eSVivek Goyal #include <linux/pfn_t.h>
14c2d0ad00SVivek Goyal #include <linux/iomap.h>
15c2d0ad00SVivek Goyal #include <linux/interval_tree.h>
1645f2348eSVivek Goyal 
17fd1a1dc6SStefan Hajnoczi /*
18fd1a1dc6SStefan Hajnoczi  * Default memory range size.  A power of 2 so it agrees with common FUSE_INIT
19fd1a1dc6SStefan Hajnoczi  * map_alignment values 4KB and 64KB.
20fd1a1dc6SStefan Hajnoczi  */
2145f2348eSVivek Goyal #define FUSE_DAX_SHIFT	21
2245f2348eSVivek Goyal #define FUSE_DAX_SZ	(1 << FUSE_DAX_SHIFT)
2345f2348eSVivek Goyal #define FUSE_DAX_PAGES	(FUSE_DAX_SZ / PAGE_SIZE)
2445f2348eSVivek Goyal 
259a752d18SVivek Goyal /* Number of ranges reclaimer will try to free in one invocation */
269a752d18SVivek Goyal #define FUSE_DAX_RECLAIM_CHUNK		(10)
279a752d18SVivek Goyal 
289a752d18SVivek Goyal /*
299a752d18SVivek Goyal  * Dax memory reclaim threshold in percetage of total ranges. When free
309a752d18SVivek Goyal  * number of free ranges drops below this threshold, reclaim can trigger
319a752d18SVivek Goyal  * Default is 20%
329a752d18SVivek Goyal  */
339a752d18SVivek Goyal #define FUSE_DAX_RECLAIM_THRESHOLD	(20)
349a752d18SVivek Goyal 
3545f2348eSVivek Goyal /** Translation information for file offsets to DAX window offsets */
3645f2348eSVivek Goyal struct fuse_dax_mapping {
379a752d18SVivek Goyal 	/* Pointer to inode where this memory range is mapped */
389a752d18SVivek Goyal 	struct inode *inode;
399a752d18SVivek Goyal 
4045f2348eSVivek Goyal 	/* Will connect in fcd->free_ranges to keep track of free memory */
4145f2348eSVivek Goyal 	struct list_head list;
4245f2348eSVivek Goyal 
43c2d0ad00SVivek Goyal 	/* For interval tree in file/inode */
44c2d0ad00SVivek Goyal 	struct interval_tree_node itn;
45c2d0ad00SVivek Goyal 
46d0cfb9dcSVivek Goyal 	/* Will connect in fc->busy_ranges to keep track busy memory */
47d0cfb9dcSVivek Goyal 	struct list_head busy_list;
48d0cfb9dcSVivek Goyal 
4945f2348eSVivek Goyal 	/** Position in DAX window */
5045f2348eSVivek Goyal 	u64 window_offset;
5145f2348eSVivek Goyal 
5245f2348eSVivek Goyal 	/** Length of mapping, in bytes */
5345f2348eSVivek Goyal 	loff_t length;
54c2d0ad00SVivek Goyal 
55c2d0ad00SVivek Goyal 	/* Is this mapping read-only or read-write */
56c2d0ad00SVivek Goyal 	bool writable;
579a752d18SVivek Goyal 
589a752d18SVivek Goyal 	/* reference count when the mapping is used by dax iomap. */
599a752d18SVivek Goyal 	refcount_t refcnt;
60c2d0ad00SVivek Goyal };
61c2d0ad00SVivek Goyal 
62c2d0ad00SVivek Goyal /* Per-inode dax map */
63c2d0ad00SVivek Goyal struct fuse_inode_dax {
64c2d0ad00SVivek Goyal 	/* Semaphore to protect modifications to the dmap tree */
65c2d0ad00SVivek Goyal 	struct rw_semaphore sem;
66c2d0ad00SVivek Goyal 
67c2d0ad00SVivek Goyal 	/* Sorted rb tree of struct fuse_dax_mapping elements */
68c2d0ad00SVivek Goyal 	struct rb_root_cached tree;
69c2d0ad00SVivek Goyal 	unsigned long nr;
7045f2348eSVivek Goyal };
711dd53957SVivek Goyal 
721dd53957SVivek Goyal struct fuse_conn_dax {
731dd53957SVivek Goyal 	/* DAX device */
741dd53957SVivek Goyal 	struct dax_device *dev;
7545f2348eSVivek Goyal 
76c2d0ad00SVivek Goyal 	/* Lock protecting accessess to  members of this structure */
77c2d0ad00SVivek Goyal 	spinlock_t lock;
78c2d0ad00SVivek Goyal 
79d0cfb9dcSVivek Goyal 	/* List of memory ranges which are busy */
80d0cfb9dcSVivek Goyal 	unsigned long nr_busy_ranges;
81d0cfb9dcSVivek Goyal 	struct list_head busy_ranges;
82d0cfb9dcSVivek Goyal 
839a752d18SVivek Goyal 	/* Worker to free up memory ranges */
849a752d18SVivek Goyal 	struct delayed_work free_work;
859a752d18SVivek Goyal 
869a752d18SVivek Goyal 	/* Wait queue for a dax range to become free */
879a752d18SVivek Goyal 	wait_queue_head_t range_waitq;
889a752d18SVivek Goyal 
8945f2348eSVivek Goyal 	/* DAX Window Free Ranges */
9045f2348eSVivek Goyal 	long nr_free_ranges;
9145f2348eSVivek Goyal 	struct list_head free_ranges;
929a752d18SVivek Goyal 
939a752d18SVivek Goyal 	unsigned long nr_ranges;
941dd53957SVivek Goyal };
951dd53957SVivek Goyal 
96c2d0ad00SVivek Goyal static inline struct fuse_dax_mapping *
node_to_dmap(struct interval_tree_node * node)97c2d0ad00SVivek Goyal node_to_dmap(struct interval_tree_node *node)
98c2d0ad00SVivek Goyal {
99c2d0ad00SVivek Goyal 	if (!node)
100c2d0ad00SVivek Goyal 		return NULL;
101c2d0ad00SVivek Goyal 
102c2d0ad00SVivek Goyal 	return container_of(node, struct fuse_dax_mapping, itn);
103c2d0ad00SVivek Goyal }
104c2d0ad00SVivek Goyal 
1059a752d18SVivek Goyal static struct fuse_dax_mapping *
1069a752d18SVivek Goyal alloc_dax_mapping_reclaim(struct fuse_conn_dax *fcd, struct inode *inode);
1079a752d18SVivek Goyal 
1089a752d18SVivek Goyal static void
__kick_dmap_free_worker(struct fuse_conn_dax * fcd,unsigned long delay_ms)1099a752d18SVivek Goyal __kick_dmap_free_worker(struct fuse_conn_dax *fcd, unsigned long delay_ms)
1109a752d18SVivek Goyal {
1119a752d18SVivek Goyal 	unsigned long free_threshold;
1129a752d18SVivek Goyal 
1139a752d18SVivek Goyal 	/* If number of free ranges are below threshold, start reclaim */
1149a752d18SVivek Goyal 	free_threshold = max_t(unsigned long, fcd->nr_ranges * FUSE_DAX_RECLAIM_THRESHOLD / 100,
1159a752d18SVivek Goyal 			     1);
1169a752d18SVivek Goyal 	if (fcd->nr_free_ranges < free_threshold)
1179a752d18SVivek Goyal 		queue_delayed_work(system_long_wq, &fcd->free_work,
1189a752d18SVivek Goyal 				   msecs_to_jiffies(delay_ms));
1199a752d18SVivek Goyal }
1209a752d18SVivek Goyal 
kick_dmap_free_worker(struct fuse_conn_dax * fcd,unsigned long delay_ms)1219a752d18SVivek Goyal static void kick_dmap_free_worker(struct fuse_conn_dax *fcd,
1229a752d18SVivek Goyal 				  unsigned long delay_ms)
1239a752d18SVivek Goyal {
1249a752d18SVivek Goyal 	spin_lock(&fcd->lock);
1259a752d18SVivek Goyal 	__kick_dmap_free_worker(fcd, delay_ms);
1269a752d18SVivek Goyal 	spin_unlock(&fcd->lock);
1279a752d18SVivek Goyal }
1289a752d18SVivek Goyal 
alloc_dax_mapping(struct fuse_conn_dax * fcd)129c2d0ad00SVivek Goyal static struct fuse_dax_mapping *alloc_dax_mapping(struct fuse_conn_dax *fcd)
130c2d0ad00SVivek Goyal {
131c2d0ad00SVivek Goyal 	struct fuse_dax_mapping *dmap;
132c2d0ad00SVivek Goyal 
133c2d0ad00SVivek Goyal 	spin_lock(&fcd->lock);
134c2d0ad00SVivek Goyal 	dmap = list_first_entry_or_null(&fcd->free_ranges,
135c2d0ad00SVivek Goyal 					struct fuse_dax_mapping, list);
136c2d0ad00SVivek Goyal 	if (dmap) {
137c2d0ad00SVivek Goyal 		list_del_init(&dmap->list);
138c2d0ad00SVivek Goyal 		WARN_ON(fcd->nr_free_ranges <= 0);
139c2d0ad00SVivek Goyal 		fcd->nr_free_ranges--;
140c2d0ad00SVivek Goyal 	}
14147e30149SJeffle Xu 	__kick_dmap_free_worker(fcd, 0);
142c2d0ad00SVivek Goyal 	spin_unlock(&fcd->lock);
1439a752d18SVivek Goyal 
144c2d0ad00SVivek Goyal 	return dmap;
145c2d0ad00SVivek Goyal }
146c2d0ad00SVivek Goyal 
147c2d0ad00SVivek Goyal /* This assumes fcd->lock is held */
__dmap_remove_busy_list(struct fuse_conn_dax * fcd,struct fuse_dax_mapping * dmap)148d0cfb9dcSVivek Goyal static void __dmap_remove_busy_list(struct fuse_conn_dax *fcd,
149d0cfb9dcSVivek Goyal 				    struct fuse_dax_mapping *dmap)
150d0cfb9dcSVivek Goyal {
151d0cfb9dcSVivek Goyal 	list_del_init(&dmap->busy_list);
152d0cfb9dcSVivek Goyal 	WARN_ON(fcd->nr_busy_ranges == 0);
153d0cfb9dcSVivek Goyal 	fcd->nr_busy_ranges--;
154d0cfb9dcSVivek Goyal }
155d0cfb9dcSVivek Goyal 
dmap_remove_busy_list(struct fuse_conn_dax * fcd,struct fuse_dax_mapping * dmap)1569a752d18SVivek Goyal static void dmap_remove_busy_list(struct fuse_conn_dax *fcd,
1579a752d18SVivek Goyal 				  struct fuse_dax_mapping *dmap)
1589a752d18SVivek Goyal {
1599a752d18SVivek Goyal 	spin_lock(&fcd->lock);
1609a752d18SVivek Goyal 	__dmap_remove_busy_list(fcd, dmap);
1619a752d18SVivek Goyal 	spin_unlock(&fcd->lock);
1629a752d18SVivek Goyal }
1639a752d18SVivek Goyal 
164d0cfb9dcSVivek Goyal /* This assumes fcd->lock is held */
__dmap_add_to_free_pool(struct fuse_conn_dax * fcd,struct fuse_dax_mapping * dmap)165c2d0ad00SVivek Goyal static void __dmap_add_to_free_pool(struct fuse_conn_dax *fcd,
166c2d0ad00SVivek Goyal 				struct fuse_dax_mapping *dmap)
167c2d0ad00SVivek Goyal {
168c2d0ad00SVivek Goyal 	list_add_tail(&dmap->list, &fcd->free_ranges);
169c2d0ad00SVivek Goyal 	fcd->nr_free_ranges++;
1709a752d18SVivek Goyal 	wake_up(&fcd->range_waitq);
171c2d0ad00SVivek Goyal }
172c2d0ad00SVivek Goyal 
dmap_add_to_free_pool(struct fuse_conn_dax * fcd,struct fuse_dax_mapping * dmap)173c2d0ad00SVivek Goyal static void dmap_add_to_free_pool(struct fuse_conn_dax *fcd,
174c2d0ad00SVivek Goyal 				struct fuse_dax_mapping *dmap)
175c2d0ad00SVivek Goyal {
176c2d0ad00SVivek Goyal 	/* Return fuse_dax_mapping to free list */
177c2d0ad00SVivek Goyal 	spin_lock(&fcd->lock);
178c2d0ad00SVivek Goyal 	__dmap_add_to_free_pool(fcd, dmap);
179c2d0ad00SVivek Goyal 	spin_unlock(&fcd->lock);
180c2d0ad00SVivek Goyal }
181c2d0ad00SVivek Goyal 
fuse_setup_one_mapping(struct inode * inode,unsigned long start_idx,struct fuse_dax_mapping * dmap,bool writable,bool upgrade)182c2d0ad00SVivek Goyal static int fuse_setup_one_mapping(struct inode *inode, unsigned long start_idx,
183c2d0ad00SVivek Goyal 				  struct fuse_dax_mapping *dmap, bool writable,
184c2d0ad00SVivek Goyal 				  bool upgrade)
185c2d0ad00SVivek Goyal {
186fcee216bSMax Reitz 	struct fuse_mount *fm = get_fuse_mount(inode);
187fcee216bSMax Reitz 	struct fuse_conn_dax *fcd = fm->fc->dax;
188c2d0ad00SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
189c2d0ad00SVivek Goyal 	struct fuse_setupmapping_in inarg;
190c2d0ad00SVivek Goyal 	loff_t offset = start_idx << FUSE_DAX_SHIFT;
191c2d0ad00SVivek Goyal 	FUSE_ARGS(args);
192c2d0ad00SVivek Goyal 	ssize_t err;
193c2d0ad00SVivek Goyal 
194c2d0ad00SVivek Goyal 	WARN_ON(fcd->nr_free_ranges < 0);
195c2d0ad00SVivek Goyal 
196c2d0ad00SVivek Goyal 	/* Ask fuse daemon to setup mapping */
197c2d0ad00SVivek Goyal 	memset(&inarg, 0, sizeof(inarg));
198c2d0ad00SVivek Goyal 	inarg.foffset = offset;
199c2d0ad00SVivek Goyal 	inarg.fh = -1;
200c2d0ad00SVivek Goyal 	inarg.moffset = dmap->window_offset;
201c2d0ad00SVivek Goyal 	inarg.len = FUSE_DAX_SZ;
202c2d0ad00SVivek Goyal 	inarg.flags |= FUSE_SETUPMAPPING_FLAG_READ;
203c2d0ad00SVivek Goyal 	if (writable)
204c2d0ad00SVivek Goyal 		inarg.flags |= FUSE_SETUPMAPPING_FLAG_WRITE;
205c2d0ad00SVivek Goyal 	args.opcode = FUSE_SETUPMAPPING;
206c2d0ad00SVivek Goyal 	args.nodeid = fi->nodeid;
207c2d0ad00SVivek Goyal 	args.in_numargs = 1;
208c2d0ad00SVivek Goyal 	args.in_args[0].size = sizeof(inarg);
209c2d0ad00SVivek Goyal 	args.in_args[0].value = &inarg;
210fcee216bSMax Reitz 	err = fuse_simple_request(fm, &args);
211c2d0ad00SVivek Goyal 	if (err < 0)
212c2d0ad00SVivek Goyal 		return err;
213c2d0ad00SVivek Goyal 	dmap->writable = writable;
214c2d0ad00SVivek Goyal 	if (!upgrade) {
2159a752d18SVivek Goyal 		/*
216c4e0cd4eSZheng Yongjun 		 * We don't take a reference on inode. inode is valid right now
2179a752d18SVivek Goyal 		 * and when inode is going away, cleanup logic should first
2189a752d18SVivek Goyal 		 * cleanup dmap entries.
2199a752d18SVivek Goyal 		 */
2209a752d18SVivek Goyal 		dmap->inode = inode;
221c2d0ad00SVivek Goyal 		dmap->itn.start = dmap->itn.last = start_idx;
222c2d0ad00SVivek Goyal 		/* Protected by fi->dax->sem */
223c2d0ad00SVivek Goyal 		interval_tree_insert(&dmap->itn, &fi->dax->tree);
224c2d0ad00SVivek Goyal 		fi->dax->nr++;
225d0cfb9dcSVivek Goyal 		spin_lock(&fcd->lock);
226d0cfb9dcSVivek Goyal 		list_add_tail(&dmap->busy_list, &fcd->busy_ranges);
227d0cfb9dcSVivek Goyal 		fcd->nr_busy_ranges++;
228d0cfb9dcSVivek Goyal 		spin_unlock(&fcd->lock);
229c2d0ad00SVivek Goyal 	}
230c2d0ad00SVivek Goyal 	return 0;
231c2d0ad00SVivek Goyal }
232c2d0ad00SVivek Goyal 
fuse_send_removemapping(struct inode * inode,struct fuse_removemapping_in * inargp,struct fuse_removemapping_one * remove_one)233c2d0ad00SVivek Goyal static int fuse_send_removemapping(struct inode *inode,
234c2d0ad00SVivek Goyal 				   struct fuse_removemapping_in *inargp,
235c2d0ad00SVivek Goyal 				   struct fuse_removemapping_one *remove_one)
236c2d0ad00SVivek Goyal {
237c2d0ad00SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
238fcee216bSMax Reitz 	struct fuse_mount *fm = get_fuse_mount(inode);
239c2d0ad00SVivek Goyal 	FUSE_ARGS(args);
240c2d0ad00SVivek Goyal 
241c2d0ad00SVivek Goyal 	args.opcode = FUSE_REMOVEMAPPING;
242c2d0ad00SVivek Goyal 	args.nodeid = fi->nodeid;
243c2d0ad00SVivek Goyal 	args.in_numargs = 2;
244c2d0ad00SVivek Goyal 	args.in_args[0].size = sizeof(*inargp);
245c2d0ad00SVivek Goyal 	args.in_args[0].value = inargp;
246c2d0ad00SVivek Goyal 	args.in_args[1].size = inargp->count * sizeof(*remove_one);
247c2d0ad00SVivek Goyal 	args.in_args[1].value = remove_one;
248fcee216bSMax Reitz 	return fuse_simple_request(fm, &args);
249c2d0ad00SVivek Goyal }
250c2d0ad00SVivek Goyal 
dmap_removemapping_list(struct inode * inode,unsigned int num,struct list_head * to_remove)251c2d0ad00SVivek Goyal static int dmap_removemapping_list(struct inode *inode, unsigned int num,
252c2d0ad00SVivek Goyal 				   struct list_head *to_remove)
253c2d0ad00SVivek Goyal {
254c2d0ad00SVivek Goyal 	struct fuse_removemapping_one *remove_one, *ptr;
255c2d0ad00SVivek Goyal 	struct fuse_removemapping_in inarg;
256c2d0ad00SVivek Goyal 	struct fuse_dax_mapping *dmap;
257c2d0ad00SVivek Goyal 	int ret, i = 0, nr_alloc;
258c2d0ad00SVivek Goyal 
259c2d0ad00SVivek Goyal 	nr_alloc = min_t(unsigned int, num, FUSE_REMOVEMAPPING_MAX_ENTRY);
260c2d0ad00SVivek Goyal 	remove_one = kmalloc_array(nr_alloc, sizeof(*remove_one), GFP_NOFS);
261c2d0ad00SVivek Goyal 	if (!remove_one)
262c2d0ad00SVivek Goyal 		return -ENOMEM;
263c2d0ad00SVivek Goyal 
264c2d0ad00SVivek Goyal 	ptr = remove_one;
265c2d0ad00SVivek Goyal 	list_for_each_entry(dmap, to_remove, list) {
266c2d0ad00SVivek Goyal 		ptr->moffset = dmap->window_offset;
267c2d0ad00SVivek Goyal 		ptr->len = dmap->length;
268c2d0ad00SVivek Goyal 		ptr++;
269c2d0ad00SVivek Goyal 		i++;
270c2d0ad00SVivek Goyal 		num--;
271c2d0ad00SVivek Goyal 		if (i >= nr_alloc || num == 0) {
272c2d0ad00SVivek Goyal 			memset(&inarg, 0, sizeof(inarg));
273c2d0ad00SVivek Goyal 			inarg.count = i;
274c2d0ad00SVivek Goyal 			ret = fuse_send_removemapping(inode, &inarg,
275c2d0ad00SVivek Goyal 						      remove_one);
276c2d0ad00SVivek Goyal 			if (ret)
277c2d0ad00SVivek Goyal 				goto out;
278c2d0ad00SVivek Goyal 			ptr = remove_one;
279c2d0ad00SVivek Goyal 			i = 0;
280c2d0ad00SVivek Goyal 		}
281c2d0ad00SVivek Goyal 	}
282c2d0ad00SVivek Goyal out:
283c2d0ad00SVivek Goyal 	kfree(remove_one);
284c2d0ad00SVivek Goyal 	return ret;
285c2d0ad00SVivek Goyal }
286c2d0ad00SVivek Goyal 
287c2d0ad00SVivek Goyal /*
288c2d0ad00SVivek Goyal  * Cleanup dmap entry and add back to free list. This should be called with
289c2d0ad00SVivek Goyal  * fcd->lock held.
290c2d0ad00SVivek Goyal  */
dmap_reinit_add_to_free_pool(struct fuse_conn_dax * fcd,struct fuse_dax_mapping * dmap)291c2d0ad00SVivek Goyal static void dmap_reinit_add_to_free_pool(struct fuse_conn_dax *fcd,
292c2d0ad00SVivek Goyal 					    struct fuse_dax_mapping *dmap)
293c2d0ad00SVivek Goyal {
294c2d0ad00SVivek Goyal 	pr_debug("fuse: freeing memory range start_idx=0x%lx end_idx=0x%lx window_offset=0x%llx length=0x%llx\n",
295c2d0ad00SVivek Goyal 		 dmap->itn.start, dmap->itn.last, dmap->window_offset,
296c2d0ad00SVivek Goyal 		 dmap->length);
297d0cfb9dcSVivek Goyal 	__dmap_remove_busy_list(fcd, dmap);
2989a752d18SVivek Goyal 	dmap->inode = NULL;
299c2d0ad00SVivek Goyal 	dmap->itn.start = dmap->itn.last = 0;
300c2d0ad00SVivek Goyal 	__dmap_add_to_free_pool(fcd, dmap);
301c2d0ad00SVivek Goyal }
302c2d0ad00SVivek Goyal 
303c2d0ad00SVivek Goyal /*
304c2d0ad00SVivek Goyal  * Free inode dmap entries whose range falls inside [start, end].
305c2d0ad00SVivek Goyal  * Does not take any locks. At this point of time it should only be
306c2d0ad00SVivek Goyal  * called from evict_inode() path where we know all dmap entries can be
307c2d0ad00SVivek Goyal  * reclaimed.
308c2d0ad00SVivek Goyal  */
inode_reclaim_dmap_range(struct fuse_conn_dax * fcd,struct inode * inode,loff_t start,loff_t end)309c2d0ad00SVivek Goyal static void inode_reclaim_dmap_range(struct fuse_conn_dax *fcd,
310c2d0ad00SVivek Goyal 				     struct inode *inode,
311c2d0ad00SVivek Goyal 				     loff_t start, loff_t end)
312c2d0ad00SVivek Goyal {
313c2d0ad00SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
314c2d0ad00SVivek Goyal 	struct fuse_dax_mapping *dmap, *n;
315c2d0ad00SVivek Goyal 	int err, num = 0;
316c2d0ad00SVivek Goyal 	LIST_HEAD(to_remove);
317c2d0ad00SVivek Goyal 	unsigned long start_idx = start >> FUSE_DAX_SHIFT;
318c2d0ad00SVivek Goyal 	unsigned long end_idx = end >> FUSE_DAX_SHIFT;
319c2d0ad00SVivek Goyal 	struct interval_tree_node *node;
320c2d0ad00SVivek Goyal 
321c2d0ad00SVivek Goyal 	while (1) {
322c2d0ad00SVivek Goyal 		node = interval_tree_iter_first(&fi->dax->tree, start_idx,
323c2d0ad00SVivek Goyal 						end_idx);
324c2d0ad00SVivek Goyal 		if (!node)
325c2d0ad00SVivek Goyal 			break;
326c2d0ad00SVivek Goyal 		dmap = node_to_dmap(node);
3279a752d18SVivek Goyal 		/* inode is going away. There should not be any users of dmap */
3289a752d18SVivek Goyal 		WARN_ON(refcount_read(&dmap->refcnt) > 1);
329c2d0ad00SVivek Goyal 		interval_tree_remove(&dmap->itn, &fi->dax->tree);
330c2d0ad00SVivek Goyal 		num++;
331c2d0ad00SVivek Goyal 		list_add(&dmap->list, &to_remove);
332c2d0ad00SVivek Goyal 	}
333c2d0ad00SVivek Goyal 
334c2d0ad00SVivek Goyal 	/* Nothing to remove */
335c2d0ad00SVivek Goyal 	if (list_empty(&to_remove))
336c2d0ad00SVivek Goyal 		return;
337c2d0ad00SVivek Goyal 
338c2d0ad00SVivek Goyal 	WARN_ON(fi->dax->nr < num);
339c2d0ad00SVivek Goyal 	fi->dax->nr -= num;
340c2d0ad00SVivek Goyal 	err = dmap_removemapping_list(inode, num, &to_remove);
341c2d0ad00SVivek Goyal 	if (err && err != -ENOTCONN) {
342c2d0ad00SVivek Goyal 		pr_warn("Failed to removemappings. start=0x%llx end=0x%llx\n",
343c2d0ad00SVivek Goyal 			start, end);
344c2d0ad00SVivek Goyal 	}
345c2d0ad00SVivek Goyal 	spin_lock(&fcd->lock);
346c2d0ad00SVivek Goyal 	list_for_each_entry_safe(dmap, n, &to_remove, list) {
347c2d0ad00SVivek Goyal 		list_del_init(&dmap->list);
348c2d0ad00SVivek Goyal 		dmap_reinit_add_to_free_pool(fcd, dmap);
349c2d0ad00SVivek Goyal 	}
350c2d0ad00SVivek Goyal 	spin_unlock(&fcd->lock);
351c2d0ad00SVivek Goyal }
352c2d0ad00SVivek Goyal 
dmap_removemapping_one(struct inode * inode,struct fuse_dax_mapping * dmap)3539a752d18SVivek Goyal static int dmap_removemapping_one(struct inode *inode,
3549a752d18SVivek Goyal 				  struct fuse_dax_mapping *dmap)
3559a752d18SVivek Goyal {
3569a752d18SVivek Goyal 	struct fuse_removemapping_one forget_one;
3579a752d18SVivek Goyal 	struct fuse_removemapping_in inarg;
3589a752d18SVivek Goyal 
3599a752d18SVivek Goyal 	memset(&inarg, 0, sizeof(inarg));
3609a752d18SVivek Goyal 	inarg.count = 1;
3619a752d18SVivek Goyal 	memset(&forget_one, 0, sizeof(forget_one));
3629a752d18SVivek Goyal 	forget_one.moffset = dmap->window_offset;
3639a752d18SVivek Goyal 	forget_one.len = dmap->length;
3649a752d18SVivek Goyal 
3659a752d18SVivek Goyal 	return fuse_send_removemapping(inode, &inarg, &forget_one);
3669a752d18SVivek Goyal }
3679a752d18SVivek Goyal 
368c2d0ad00SVivek Goyal /*
369c2d0ad00SVivek Goyal  * It is called from evict_inode() and by that time inode is going away. So
370c2d0ad00SVivek Goyal  * this function does not take any locks like fi->dax->sem for traversing
371c2d0ad00SVivek Goyal  * that fuse inode interval tree. If that lock is taken then lock validator
372c2d0ad00SVivek Goyal  * complains of deadlock situation w.r.t fs_reclaim lock.
373c2d0ad00SVivek Goyal  */
fuse_dax_inode_cleanup(struct inode * inode)374c2d0ad00SVivek Goyal void fuse_dax_inode_cleanup(struct inode *inode)
375c2d0ad00SVivek Goyal {
376c2d0ad00SVivek Goyal 	struct fuse_conn *fc = get_fuse_conn(inode);
377c2d0ad00SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
378c2d0ad00SVivek Goyal 
379c2d0ad00SVivek Goyal 	/*
380c2d0ad00SVivek Goyal 	 * fuse_evict_inode() has already called truncate_inode_pages_final()
381c2d0ad00SVivek Goyal 	 * before we arrive here. So we should not have to worry about any
382c2d0ad00SVivek Goyal 	 * pages/exception entries still associated with inode.
383c2d0ad00SVivek Goyal 	 */
384c2d0ad00SVivek Goyal 	inode_reclaim_dmap_range(fc->dax, inode, 0, -1);
385c2d0ad00SVivek Goyal 	WARN_ON(fi->dax->nr);
386c2d0ad00SVivek Goyal }
387c2d0ad00SVivek Goyal 
fuse_fill_iomap_hole(struct iomap * iomap,loff_t length)388c2d0ad00SVivek Goyal static void fuse_fill_iomap_hole(struct iomap *iomap, loff_t length)
389c2d0ad00SVivek Goyal {
390c2d0ad00SVivek Goyal 	iomap->addr = IOMAP_NULL_ADDR;
391c2d0ad00SVivek Goyal 	iomap->length = length;
392c2d0ad00SVivek Goyal 	iomap->type = IOMAP_HOLE;
393c2d0ad00SVivek Goyal }
394c2d0ad00SVivek Goyal 
fuse_fill_iomap(struct inode * inode,loff_t pos,loff_t length,struct iomap * iomap,struct fuse_dax_mapping * dmap,unsigned int flags)395c2d0ad00SVivek Goyal static void fuse_fill_iomap(struct inode *inode, loff_t pos, loff_t length,
396c2d0ad00SVivek Goyal 			    struct iomap *iomap, struct fuse_dax_mapping *dmap,
397c2d0ad00SVivek Goyal 			    unsigned int flags)
398c2d0ad00SVivek Goyal {
399c2d0ad00SVivek Goyal 	loff_t offset, len;
400c2d0ad00SVivek Goyal 	loff_t i_size = i_size_read(inode);
401c2d0ad00SVivek Goyal 
402c2d0ad00SVivek Goyal 	offset = pos - (dmap->itn.start << FUSE_DAX_SHIFT);
403c2d0ad00SVivek Goyal 	len = min(length, dmap->length - offset);
404c2d0ad00SVivek Goyal 
405c2d0ad00SVivek Goyal 	/* If length is beyond end of file, truncate further */
406c2d0ad00SVivek Goyal 	if (pos + len > i_size)
407c2d0ad00SVivek Goyal 		len = i_size - pos;
408c2d0ad00SVivek Goyal 
409c2d0ad00SVivek Goyal 	if (len > 0) {
410c2d0ad00SVivek Goyal 		iomap->addr = dmap->window_offset + offset;
411c2d0ad00SVivek Goyal 		iomap->length = len;
412c2d0ad00SVivek Goyal 		if (flags & IOMAP_FAULT)
413c2d0ad00SVivek Goyal 			iomap->length = ALIGN(len, PAGE_SIZE);
414c2d0ad00SVivek Goyal 		iomap->type = IOMAP_MAPPED;
4159a752d18SVivek Goyal 		/*
4169a752d18SVivek Goyal 		 * increace refcnt so that reclaim code knows this dmap is in
4179a752d18SVivek Goyal 		 * use. This assumes fi->dax->sem mutex is held either
4189a752d18SVivek Goyal 		 * shared/exclusive.
4199a752d18SVivek Goyal 		 */
4209a752d18SVivek Goyal 		refcount_inc(&dmap->refcnt);
4219a752d18SVivek Goyal 
4229a752d18SVivek Goyal 		/* iomap->private should be NULL */
4239a752d18SVivek Goyal 		WARN_ON_ONCE(iomap->private);
4249a752d18SVivek Goyal 		iomap->private = dmap;
425c2d0ad00SVivek Goyal 	} else {
426c2d0ad00SVivek Goyal 		/* Mapping beyond end of file is hole */
427c2d0ad00SVivek Goyal 		fuse_fill_iomap_hole(iomap, length);
428c2d0ad00SVivek Goyal 	}
429c2d0ad00SVivek Goyal }
430c2d0ad00SVivek Goyal 
fuse_setup_new_dax_mapping(struct inode * inode,loff_t pos,loff_t length,unsigned int flags,struct iomap * iomap)431c2d0ad00SVivek Goyal static int fuse_setup_new_dax_mapping(struct inode *inode, loff_t pos,
432c2d0ad00SVivek Goyal 				      loff_t length, unsigned int flags,
433c2d0ad00SVivek Goyal 				      struct iomap *iomap)
434c2d0ad00SVivek Goyal {
435c2d0ad00SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
436c2d0ad00SVivek Goyal 	struct fuse_conn *fc = get_fuse_conn(inode);
437c2d0ad00SVivek Goyal 	struct fuse_conn_dax *fcd = fc->dax;
438c2d0ad00SVivek Goyal 	struct fuse_dax_mapping *dmap, *alloc_dmap = NULL;
439c2d0ad00SVivek Goyal 	int ret;
440c2d0ad00SVivek Goyal 	bool writable = flags & IOMAP_WRITE;
441c2d0ad00SVivek Goyal 	unsigned long start_idx = pos >> FUSE_DAX_SHIFT;
442c2d0ad00SVivek Goyal 	struct interval_tree_node *node;
443c2d0ad00SVivek Goyal 
4449a752d18SVivek Goyal 	/*
4459a752d18SVivek Goyal 	 * Can't do inline reclaim in fault path. We call
4469a752d18SVivek Goyal 	 * dax_layout_busy_page() before we free a range. And
4478bcbbe9cSJan Kara 	 * fuse_wait_dax_page() drops mapping->invalidate_lock and requires it.
4488bcbbe9cSJan Kara 	 * In fault path we enter with mapping->invalidate_lock held and can't
4498bcbbe9cSJan Kara 	 * drop it. Also in fault path we hold mapping->invalidate_lock shared
4508bcbbe9cSJan Kara 	 * and not exclusive, so that creates further issues with
4518bcbbe9cSJan Kara 	 * fuse_wait_dax_page().  Hence return -EAGAIN and fuse_dax_fault()
4528bcbbe9cSJan Kara 	 * will wait for a memory range to become free and retry.
4539a752d18SVivek Goyal 	 */
4549a752d18SVivek Goyal 	if (flags & IOMAP_FAULT) {
455c2d0ad00SVivek Goyal 		alloc_dmap = alloc_dax_mapping(fcd);
456c2d0ad00SVivek Goyal 		if (!alloc_dmap)
4579a752d18SVivek Goyal 			return -EAGAIN;
4589a752d18SVivek Goyal 	} else {
4599a752d18SVivek Goyal 		alloc_dmap = alloc_dax_mapping_reclaim(fcd, inode);
4609a752d18SVivek Goyal 		if (IS_ERR(alloc_dmap))
4619a752d18SVivek Goyal 			return PTR_ERR(alloc_dmap);
4629a752d18SVivek Goyal 	}
4639a752d18SVivek Goyal 
4649a752d18SVivek Goyal 	/* If we are here, we should have memory allocated */
4659a752d18SVivek Goyal 	if (WARN_ON(!alloc_dmap))
466c2d0ad00SVivek Goyal 		return -EIO;
467c2d0ad00SVivek Goyal 
468c2d0ad00SVivek Goyal 	/*
469c2d0ad00SVivek Goyal 	 * Take write lock so that only one caller can try to setup mapping
470c2d0ad00SVivek Goyal 	 * and other waits.
471c2d0ad00SVivek Goyal 	 */
472c2d0ad00SVivek Goyal 	down_write(&fi->dax->sem);
473c2d0ad00SVivek Goyal 	/*
474c2d0ad00SVivek Goyal 	 * We dropped lock. Check again if somebody else setup
475c2d0ad00SVivek Goyal 	 * mapping already.
476c2d0ad00SVivek Goyal 	 */
477c2d0ad00SVivek Goyal 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
478c2d0ad00SVivek Goyal 	if (node) {
479c2d0ad00SVivek Goyal 		dmap = node_to_dmap(node);
480c2d0ad00SVivek Goyal 		fuse_fill_iomap(inode, pos, length, iomap, dmap, flags);
481c2d0ad00SVivek Goyal 		dmap_add_to_free_pool(fcd, alloc_dmap);
482c2d0ad00SVivek Goyal 		up_write(&fi->dax->sem);
483c2d0ad00SVivek Goyal 		return 0;
484c2d0ad00SVivek Goyal 	}
485c2d0ad00SVivek Goyal 
486c2d0ad00SVivek Goyal 	/* Setup one mapping */
487c2d0ad00SVivek Goyal 	ret = fuse_setup_one_mapping(inode, pos >> FUSE_DAX_SHIFT, alloc_dmap,
488c2d0ad00SVivek Goyal 				     writable, false);
489c2d0ad00SVivek Goyal 	if (ret < 0) {
490c2d0ad00SVivek Goyal 		dmap_add_to_free_pool(fcd, alloc_dmap);
491c2d0ad00SVivek Goyal 		up_write(&fi->dax->sem);
492c2d0ad00SVivek Goyal 		return ret;
493c2d0ad00SVivek Goyal 	}
494c2d0ad00SVivek Goyal 	fuse_fill_iomap(inode, pos, length, iomap, alloc_dmap, flags);
495c2d0ad00SVivek Goyal 	up_write(&fi->dax->sem);
496c2d0ad00SVivek Goyal 	return 0;
497c2d0ad00SVivek Goyal }
498c2d0ad00SVivek Goyal 
fuse_upgrade_dax_mapping(struct inode * inode,loff_t pos,loff_t length,unsigned int flags,struct iomap * iomap)499c2d0ad00SVivek Goyal static int fuse_upgrade_dax_mapping(struct inode *inode, loff_t pos,
500c2d0ad00SVivek Goyal 				    loff_t length, unsigned int flags,
501c2d0ad00SVivek Goyal 				    struct iomap *iomap)
502c2d0ad00SVivek Goyal {
503c2d0ad00SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
504c2d0ad00SVivek Goyal 	struct fuse_dax_mapping *dmap;
505c2d0ad00SVivek Goyal 	int ret;
506c2d0ad00SVivek Goyal 	unsigned long idx = pos >> FUSE_DAX_SHIFT;
507c2d0ad00SVivek Goyal 	struct interval_tree_node *node;
508c2d0ad00SVivek Goyal 
509c2d0ad00SVivek Goyal 	/*
510c2d0ad00SVivek Goyal 	 * Take exclusive lock so that only one caller can try to setup
511c2d0ad00SVivek Goyal 	 * mapping and others wait.
512c2d0ad00SVivek Goyal 	 */
513c2d0ad00SVivek Goyal 	down_write(&fi->dax->sem);
514c2d0ad00SVivek Goyal 	node = interval_tree_iter_first(&fi->dax->tree, idx, idx);
515c2d0ad00SVivek Goyal 
5168bcbbe9cSJan Kara 	/* We are holding either inode lock or invalidate_lock, and that should
5179a752d18SVivek Goyal 	 * ensure that dmap can't be truncated. We are holding a reference
5189a752d18SVivek Goyal 	 * on dmap and that should make sure it can't be reclaimed. So dmap
5199a752d18SVivek Goyal 	 * should still be there in tree despite the fact we dropped and
5209a752d18SVivek Goyal 	 * re-acquired the fi->dax->sem lock.
521c2d0ad00SVivek Goyal 	 */
522c2d0ad00SVivek Goyal 	ret = -EIO;
523c2d0ad00SVivek Goyal 	if (WARN_ON(!node))
524c2d0ad00SVivek Goyal 		goto out_err;
525c2d0ad00SVivek Goyal 
526c2d0ad00SVivek Goyal 	dmap = node_to_dmap(node);
527c2d0ad00SVivek Goyal 
5289a752d18SVivek Goyal 	/* We took an extra reference on dmap to make sure its not reclaimd.
5299a752d18SVivek Goyal 	 * Now we hold fi->dax->sem lock and that reference is not needed
5309a752d18SVivek Goyal 	 * anymore. Drop it.
5319a752d18SVivek Goyal 	 */
5329a752d18SVivek Goyal 	if (refcount_dec_and_test(&dmap->refcnt)) {
5339a752d18SVivek Goyal 		/* refcount should not hit 0. This object only goes
5349a752d18SVivek Goyal 		 * away when fuse connection goes away
5359a752d18SVivek Goyal 		 */
5369a752d18SVivek Goyal 		WARN_ON_ONCE(1);
5379a752d18SVivek Goyal 	}
5389a752d18SVivek Goyal 
539c2d0ad00SVivek Goyal 	/* Maybe another thread already upgraded mapping while we were not
540c2d0ad00SVivek Goyal 	 * holding lock.
541c2d0ad00SVivek Goyal 	 */
542c2d0ad00SVivek Goyal 	if (dmap->writable) {
543c2d0ad00SVivek Goyal 		ret = 0;
544c2d0ad00SVivek Goyal 		goto out_fill_iomap;
545c2d0ad00SVivek Goyal 	}
546c2d0ad00SVivek Goyal 
547c2d0ad00SVivek Goyal 	ret = fuse_setup_one_mapping(inode, pos >> FUSE_DAX_SHIFT, dmap, true,
548c2d0ad00SVivek Goyal 				     true);
549c2d0ad00SVivek Goyal 	if (ret < 0)
550c2d0ad00SVivek Goyal 		goto out_err;
551c2d0ad00SVivek Goyal out_fill_iomap:
552c2d0ad00SVivek Goyal 	fuse_fill_iomap(inode, pos, length, iomap, dmap, flags);
553c2d0ad00SVivek Goyal out_err:
554c2d0ad00SVivek Goyal 	up_write(&fi->dax->sem);
555c2d0ad00SVivek Goyal 	return ret;
556c2d0ad00SVivek Goyal }
557c2d0ad00SVivek Goyal 
558c2d0ad00SVivek Goyal /* This is just for DAX and the mapping is ephemeral, do not use it for other
559c2d0ad00SVivek Goyal  * purposes since there is no block device with a permanent mapping.
560c2d0ad00SVivek Goyal  */
fuse_iomap_begin(struct inode * inode,loff_t pos,loff_t length,unsigned int flags,struct iomap * iomap,struct iomap * srcmap)561c2d0ad00SVivek Goyal static int fuse_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
562c2d0ad00SVivek Goyal 			    unsigned int flags, struct iomap *iomap,
563c2d0ad00SVivek Goyal 			    struct iomap *srcmap)
564c2d0ad00SVivek Goyal {
565c2d0ad00SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
566c2d0ad00SVivek Goyal 	struct fuse_conn *fc = get_fuse_conn(inode);
567c2d0ad00SVivek Goyal 	struct fuse_dax_mapping *dmap;
568c2d0ad00SVivek Goyal 	bool writable = flags & IOMAP_WRITE;
569c2d0ad00SVivek Goyal 	unsigned long start_idx = pos >> FUSE_DAX_SHIFT;
570c2d0ad00SVivek Goyal 	struct interval_tree_node *node;
571c2d0ad00SVivek Goyal 
572c2d0ad00SVivek Goyal 	/* We don't support FIEMAP */
573c2d0ad00SVivek Goyal 	if (WARN_ON(flags & IOMAP_REPORT))
574c2d0ad00SVivek Goyal 		return -EIO;
575c2d0ad00SVivek Goyal 
576c2d0ad00SVivek Goyal 	iomap->offset = pos;
577c2d0ad00SVivek Goyal 	iomap->flags = 0;
578c2d0ad00SVivek Goyal 	iomap->bdev = NULL;
579c2d0ad00SVivek Goyal 	iomap->dax_dev = fc->dax->dev;
580c2d0ad00SVivek Goyal 
581c2d0ad00SVivek Goyal 	/*
582c2d0ad00SVivek Goyal 	 * Both read/write and mmap path can race here. So we need something
583c2d0ad00SVivek Goyal 	 * to make sure if we are setting up mapping, then other path waits
584c2d0ad00SVivek Goyal 	 *
585c2d0ad00SVivek Goyal 	 * For now, use a semaphore for this. It probably needs to be
586c2d0ad00SVivek Goyal 	 * optimized later.
587c2d0ad00SVivek Goyal 	 */
588c2d0ad00SVivek Goyal 	down_read(&fi->dax->sem);
589c2d0ad00SVivek Goyal 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
590c2d0ad00SVivek Goyal 	if (node) {
591c2d0ad00SVivek Goyal 		dmap = node_to_dmap(node);
592c2d0ad00SVivek Goyal 		if (writable && !dmap->writable) {
593c2d0ad00SVivek Goyal 			/* Upgrade read-only mapping to read-write. This will
594c2d0ad00SVivek Goyal 			 * require exclusive fi->dax->sem lock as we don't want
595c2d0ad00SVivek Goyal 			 * two threads to be trying to this simultaneously
596c2d0ad00SVivek Goyal 			 * for same dmap. So drop shared lock and acquire
597c2d0ad00SVivek Goyal 			 * exclusive lock.
5989a752d18SVivek Goyal 			 *
5999a752d18SVivek Goyal 			 * Before dropping fi->dax->sem lock, take reference
6009a752d18SVivek Goyal 			 * on dmap so that its not freed by range reclaim.
601c2d0ad00SVivek Goyal 			 */
6029a752d18SVivek Goyal 			refcount_inc(&dmap->refcnt);
603c2d0ad00SVivek Goyal 			up_read(&fi->dax->sem);
604c2d0ad00SVivek Goyal 			pr_debug("%s: Upgrading mapping at offset 0x%llx length 0x%llx\n",
605c2d0ad00SVivek Goyal 				 __func__, pos, length);
606c2d0ad00SVivek Goyal 			return fuse_upgrade_dax_mapping(inode, pos, length,
607c2d0ad00SVivek Goyal 							flags, iomap);
608c2d0ad00SVivek Goyal 		} else {
609c2d0ad00SVivek Goyal 			fuse_fill_iomap(inode, pos, length, iomap, dmap, flags);
610c2d0ad00SVivek Goyal 			up_read(&fi->dax->sem);
611c2d0ad00SVivek Goyal 			return 0;
612c2d0ad00SVivek Goyal 		}
613c2d0ad00SVivek Goyal 	} else {
614c2d0ad00SVivek Goyal 		up_read(&fi->dax->sem);
615c2d0ad00SVivek Goyal 		pr_debug("%s: no mapping at offset 0x%llx length 0x%llx\n",
616c2d0ad00SVivek Goyal 				__func__, pos, length);
617c2d0ad00SVivek Goyal 		if (pos >= i_size_read(inode))
618c2d0ad00SVivek Goyal 			goto iomap_hole;
619c2d0ad00SVivek Goyal 
620c2d0ad00SVivek Goyal 		return fuse_setup_new_dax_mapping(inode, pos, length, flags,
621c2d0ad00SVivek Goyal 						  iomap);
622c2d0ad00SVivek Goyal 	}
623c2d0ad00SVivek Goyal 
624c2d0ad00SVivek Goyal 	/*
625c4e0cd4eSZheng Yongjun 	 * If read beyond end of file happens, fs code seems to return
626c2d0ad00SVivek Goyal 	 * it as hole
627c2d0ad00SVivek Goyal 	 */
628c2d0ad00SVivek Goyal iomap_hole:
629c2d0ad00SVivek Goyal 	fuse_fill_iomap_hole(iomap, length);
630c2d0ad00SVivek Goyal 	pr_debug("%s returning hole mapping. pos=0x%llx length_asked=0x%llx length_returned=0x%llx\n",
631c2d0ad00SVivek Goyal 		 __func__, pos, length, iomap->length);
632c2d0ad00SVivek Goyal 	return 0;
633c2d0ad00SVivek Goyal }
634c2d0ad00SVivek Goyal 
fuse_iomap_end(struct inode * inode,loff_t pos,loff_t length,ssize_t written,unsigned int flags,struct iomap * iomap)635c2d0ad00SVivek Goyal static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t length,
636c2d0ad00SVivek Goyal 			  ssize_t written, unsigned int flags,
637c2d0ad00SVivek Goyal 			  struct iomap *iomap)
638c2d0ad00SVivek Goyal {
6399a752d18SVivek Goyal 	struct fuse_dax_mapping *dmap = iomap->private;
6409a752d18SVivek Goyal 
6419a752d18SVivek Goyal 	if (dmap) {
6429a752d18SVivek Goyal 		if (refcount_dec_and_test(&dmap->refcnt)) {
6439a752d18SVivek Goyal 			/* refcount should not hit 0. This object only goes
6449a752d18SVivek Goyal 			 * away when fuse connection goes away
6459a752d18SVivek Goyal 			 */
6469a752d18SVivek Goyal 			WARN_ON_ONCE(1);
6479a752d18SVivek Goyal 		}
6489a752d18SVivek Goyal 	}
6499a752d18SVivek Goyal 
650c2d0ad00SVivek Goyal 	/* DAX writes beyond end-of-file aren't handled using iomap, so the
651c2d0ad00SVivek Goyal 	 * file size is unchanged and there is nothing to do here.
652c2d0ad00SVivek Goyal 	 */
653c2d0ad00SVivek Goyal 	return 0;
654c2d0ad00SVivek Goyal }
655c2d0ad00SVivek Goyal 
656c2d0ad00SVivek Goyal static const struct iomap_ops fuse_iomap_ops = {
657c2d0ad00SVivek Goyal 	.iomap_begin = fuse_iomap_begin,
658c2d0ad00SVivek Goyal 	.iomap_end = fuse_iomap_end,
659c2d0ad00SVivek Goyal };
660c2d0ad00SVivek Goyal 
fuse_wait_dax_page(struct inode * inode)6616ae330caSVivek Goyal static void fuse_wait_dax_page(struct inode *inode)
6626ae330caSVivek Goyal {
6638bcbbe9cSJan Kara 	filemap_invalidate_unlock(inode->i_mapping);
6646ae330caSVivek Goyal 	schedule();
6658bcbbe9cSJan Kara 	filemap_invalidate_lock(inode->i_mapping);
6666ae330caSVivek Goyal }
6676ae330caSVivek Goyal 
6688bcbbe9cSJan Kara /* Should be called with mapping->invalidate_lock held exclusively */
__fuse_dax_break_layouts(struct inode * inode,bool * retry,loff_t start,loff_t end)6696ae330caSVivek Goyal static int __fuse_dax_break_layouts(struct inode *inode, bool *retry,
6706ae330caSVivek Goyal 				    loff_t start, loff_t end)
6716ae330caSVivek Goyal {
6726ae330caSVivek Goyal 	struct page *page;
6736ae330caSVivek Goyal 
6746ae330caSVivek Goyal 	page = dax_layout_busy_page_range(inode->i_mapping, start, end);
6756ae330caSVivek Goyal 	if (!page)
6766ae330caSVivek Goyal 		return 0;
6776ae330caSVivek Goyal 
6786ae330caSVivek Goyal 	*retry = true;
6796ae330caSVivek Goyal 	return ___wait_var_event(&page->_refcount,
6806ae330caSVivek Goyal 			atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
6816ae330caSVivek Goyal 			0, 0, fuse_wait_dax_page(inode));
6826ae330caSVivek Goyal }
6836ae330caSVivek Goyal 
6846ae330caSVivek Goyal /* dmap_end == 0 leads to unmapping of whole file */
fuse_dax_break_layouts(struct inode * inode,u64 dmap_start,u64 dmap_end)6856ae330caSVivek Goyal int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start,
6866ae330caSVivek Goyal 				  u64 dmap_end)
6876ae330caSVivek Goyal {
6886ae330caSVivek Goyal 	bool	retry;
6896ae330caSVivek Goyal 	int	ret;
6906ae330caSVivek Goyal 
6916ae330caSVivek Goyal 	do {
6926ae330caSVivek Goyal 		retry = false;
6936ae330caSVivek Goyal 		ret = __fuse_dax_break_layouts(inode, &retry, dmap_start,
6946ae330caSVivek Goyal 					       dmap_end);
6956ae330caSVivek Goyal 	} while (ret == 0 && retry);
6966ae330caSVivek Goyal 
6976ae330caSVivek Goyal 	return ret;
6986ae330caSVivek Goyal }
6996ae330caSVivek Goyal 
fuse_dax_read_iter(struct kiocb * iocb,struct iov_iter * to)700c2d0ad00SVivek Goyal ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
701c2d0ad00SVivek Goyal {
702c2d0ad00SVivek Goyal 	struct inode *inode = file_inode(iocb->ki_filp);
703c2d0ad00SVivek Goyal 	ssize_t ret;
704c2d0ad00SVivek Goyal 
705c2d0ad00SVivek Goyal 	if (iocb->ki_flags & IOCB_NOWAIT) {
706c2d0ad00SVivek Goyal 		if (!inode_trylock_shared(inode))
707c2d0ad00SVivek Goyal 			return -EAGAIN;
708c2d0ad00SVivek Goyal 	} else {
709c2d0ad00SVivek Goyal 		inode_lock_shared(inode);
710c2d0ad00SVivek Goyal 	}
711c2d0ad00SVivek Goyal 
712c2d0ad00SVivek Goyal 	ret = dax_iomap_rw(iocb, to, &fuse_iomap_ops);
713c2d0ad00SVivek Goyal 	inode_unlock_shared(inode);
714c2d0ad00SVivek Goyal 
715c2d0ad00SVivek Goyal 	/* TODO file_accessed(iocb->f_filp) */
716c2d0ad00SVivek Goyal 	return ret;
717c2d0ad00SVivek Goyal }
718c2d0ad00SVivek Goyal 
file_extending_write(struct kiocb * iocb,struct iov_iter * from)719c2d0ad00SVivek Goyal static bool file_extending_write(struct kiocb *iocb, struct iov_iter *from)
720c2d0ad00SVivek Goyal {
721c2d0ad00SVivek Goyal 	struct inode *inode = file_inode(iocb->ki_filp);
722c2d0ad00SVivek Goyal 
723c2d0ad00SVivek Goyal 	return (iov_iter_rw(from) == WRITE &&
724c2d0ad00SVivek Goyal 		((iocb->ki_pos) >= i_size_read(inode) ||
725c2d0ad00SVivek Goyal 		  (iocb->ki_pos + iov_iter_count(from) > i_size_read(inode))));
726c2d0ad00SVivek Goyal }
727c2d0ad00SVivek Goyal 
fuse_dax_direct_write(struct kiocb * iocb,struct iov_iter * from)728c2d0ad00SVivek Goyal static ssize_t fuse_dax_direct_write(struct kiocb *iocb, struct iov_iter *from)
729c2d0ad00SVivek Goyal {
730c2d0ad00SVivek Goyal 	struct inode *inode = file_inode(iocb->ki_filp);
731c2d0ad00SVivek Goyal 	struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
732c2d0ad00SVivek Goyal 	ssize_t ret;
733c2d0ad00SVivek Goyal 
734c2d0ad00SVivek Goyal 	ret = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
735c2d0ad00SVivek Goyal 
736d347739aSMiklos Szeredi 	fuse_write_update_attr(inode, iocb->ki_pos, ret);
737c2d0ad00SVivek Goyal 	return ret;
738c2d0ad00SVivek Goyal }
739c2d0ad00SVivek Goyal 
fuse_dax_write_iter(struct kiocb * iocb,struct iov_iter * from)740c2d0ad00SVivek Goyal ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
741c2d0ad00SVivek Goyal {
742c2d0ad00SVivek Goyal 	struct inode *inode = file_inode(iocb->ki_filp);
743c2d0ad00SVivek Goyal 	ssize_t ret;
744c2d0ad00SVivek Goyal 
745c2d0ad00SVivek Goyal 	if (iocb->ki_flags & IOCB_NOWAIT) {
746c2d0ad00SVivek Goyal 		if (!inode_trylock(inode))
747c2d0ad00SVivek Goyal 			return -EAGAIN;
748c2d0ad00SVivek Goyal 	} else {
749c2d0ad00SVivek Goyal 		inode_lock(inode);
750c2d0ad00SVivek Goyal 	}
751c2d0ad00SVivek Goyal 
752c2d0ad00SVivek Goyal 	ret = generic_write_checks(iocb, from);
753c2d0ad00SVivek Goyal 	if (ret <= 0)
754c2d0ad00SVivek Goyal 		goto out;
755c2d0ad00SVivek Goyal 
756c2d0ad00SVivek Goyal 	ret = file_remove_privs(iocb->ki_filp);
757c2d0ad00SVivek Goyal 	if (ret)
758c2d0ad00SVivek Goyal 		goto out;
759c2d0ad00SVivek Goyal 	/* TODO file_update_time() but we don't want metadata I/O */
760c2d0ad00SVivek Goyal 
761c2d0ad00SVivek Goyal 	/* Do not use dax for file extending writes as write and on
762c2d0ad00SVivek Goyal 	 * disk i_size increase are not atomic otherwise.
763c2d0ad00SVivek Goyal 	 */
764c2d0ad00SVivek Goyal 	if (file_extending_write(iocb, from))
765c2d0ad00SVivek Goyal 		ret = fuse_dax_direct_write(iocb, from);
766c2d0ad00SVivek Goyal 	else
767c2d0ad00SVivek Goyal 		ret = dax_iomap_rw(iocb, from, &fuse_iomap_ops);
768c2d0ad00SVivek Goyal 
769c2d0ad00SVivek Goyal out:
770c2d0ad00SVivek Goyal 	inode_unlock(inode);
771c2d0ad00SVivek Goyal 
772c2d0ad00SVivek Goyal 	if (ret > 0)
773c2d0ad00SVivek Goyal 		ret = generic_write_sync(iocb, ret);
774c2d0ad00SVivek Goyal 	return ret;
775c2d0ad00SVivek Goyal }
776c2d0ad00SVivek Goyal 
fuse_dax_writepages(struct address_space * mapping,struct writeback_control * wbc)7779483e7d5SVivek Goyal static int fuse_dax_writepages(struct address_space *mapping,
7789483e7d5SVivek Goyal 			       struct writeback_control *wbc)
7799483e7d5SVivek Goyal {
7809483e7d5SVivek Goyal 
7819483e7d5SVivek Goyal 	struct inode *inode = mapping->host;
7829483e7d5SVivek Goyal 	struct fuse_conn *fc = get_fuse_conn(inode);
7839483e7d5SVivek Goyal 
7849483e7d5SVivek Goyal 	return dax_writeback_mapping_range(mapping, fc->dax->dev, wbc);
7859483e7d5SVivek Goyal }
7869483e7d5SVivek Goyal 
__fuse_dax_fault(struct vm_fault * vmf,unsigned int order,bool write)7871d024e7aSMatthew Wilcox (Oracle) static vm_fault_t __fuse_dax_fault(struct vm_fault *vmf, unsigned int order,
7881d024e7aSMatthew Wilcox (Oracle) 		bool write)
7892a9a609aSStefan Hajnoczi {
7902a9a609aSStefan Hajnoczi 	vm_fault_t ret;
7912a9a609aSStefan Hajnoczi 	struct inode *inode = file_inode(vmf->vma->vm_file);
7922a9a609aSStefan Hajnoczi 	struct super_block *sb = inode->i_sb;
7932a9a609aSStefan Hajnoczi 	pfn_t pfn;
7949a752d18SVivek Goyal 	int error = 0;
7959a752d18SVivek Goyal 	struct fuse_conn *fc = get_fuse_conn(inode);
7969a752d18SVivek Goyal 	struct fuse_conn_dax *fcd = fc->dax;
7979a752d18SVivek Goyal 	bool retry = false;
7982a9a609aSStefan Hajnoczi 
7992a9a609aSStefan Hajnoczi 	if (write)
8002a9a609aSStefan Hajnoczi 		sb_start_pagefault(sb);
8019a752d18SVivek Goyal retry:
8029a752d18SVivek Goyal 	if (retry && !(fcd->nr_free_ranges > 0))
8039a752d18SVivek Goyal 		wait_event(fcd->range_waitq, (fcd->nr_free_ranges > 0));
8042a9a609aSStefan Hajnoczi 
8056ae330caSVivek Goyal 	/*
8066ae330caSVivek Goyal 	 * We need to serialize against not only truncate but also against
8076ae330caSVivek Goyal 	 * fuse dax memory range reclaim. While a range is being reclaimed,
8086ae330caSVivek Goyal 	 * we do not want any read/write/mmap to make progress and try
8096ae330caSVivek Goyal 	 * to populate page cache or access memory we are trying to free.
8106ae330caSVivek Goyal 	 */
8118bcbbe9cSJan Kara 	filemap_invalidate_lock_shared(inode->i_mapping);
8121d024e7aSMatthew Wilcox (Oracle) 	ret = dax_iomap_fault(vmf, order, &pfn, &error, &fuse_iomap_ops);
8139a752d18SVivek Goyal 	if ((ret & VM_FAULT_ERROR) && error == -EAGAIN) {
8149a752d18SVivek Goyal 		error = 0;
8159a752d18SVivek Goyal 		retry = true;
8168bcbbe9cSJan Kara 		filemap_invalidate_unlock_shared(inode->i_mapping);
8179a752d18SVivek Goyal 		goto retry;
8189a752d18SVivek Goyal 	}
8192a9a609aSStefan Hajnoczi 
8202a9a609aSStefan Hajnoczi 	if (ret & VM_FAULT_NEEDDSYNC)
8211d024e7aSMatthew Wilcox (Oracle) 		ret = dax_finish_sync_fault(vmf, order, pfn);
8228bcbbe9cSJan Kara 	filemap_invalidate_unlock_shared(inode->i_mapping);
8232a9a609aSStefan Hajnoczi 
8242a9a609aSStefan Hajnoczi 	if (write)
8252a9a609aSStefan Hajnoczi 		sb_end_pagefault(sb);
8262a9a609aSStefan Hajnoczi 
8272a9a609aSStefan Hajnoczi 	return ret;
8282a9a609aSStefan Hajnoczi }
8292a9a609aSStefan Hajnoczi 
fuse_dax_fault(struct vm_fault * vmf)8302a9a609aSStefan Hajnoczi static vm_fault_t fuse_dax_fault(struct vm_fault *vmf)
8312a9a609aSStefan Hajnoczi {
8321d024e7aSMatthew Wilcox (Oracle) 	return __fuse_dax_fault(vmf, 0, vmf->flags & FAULT_FLAG_WRITE);
8332a9a609aSStefan Hajnoczi }
8342a9a609aSStefan Hajnoczi 
fuse_dax_huge_fault(struct vm_fault * vmf,unsigned int order)8351d024e7aSMatthew Wilcox (Oracle) static vm_fault_t fuse_dax_huge_fault(struct vm_fault *vmf, unsigned int order)
8362a9a609aSStefan Hajnoczi {
8371d024e7aSMatthew Wilcox (Oracle) 	return __fuse_dax_fault(vmf, order, vmf->flags & FAULT_FLAG_WRITE);
8382a9a609aSStefan Hajnoczi }
8392a9a609aSStefan Hajnoczi 
fuse_dax_page_mkwrite(struct vm_fault * vmf)8402a9a609aSStefan Hajnoczi static vm_fault_t fuse_dax_page_mkwrite(struct vm_fault *vmf)
8412a9a609aSStefan Hajnoczi {
8421d024e7aSMatthew Wilcox (Oracle) 	return __fuse_dax_fault(vmf, 0, true);
8432a9a609aSStefan Hajnoczi }
8442a9a609aSStefan Hajnoczi 
fuse_dax_pfn_mkwrite(struct vm_fault * vmf)8452a9a609aSStefan Hajnoczi static vm_fault_t fuse_dax_pfn_mkwrite(struct vm_fault *vmf)
8462a9a609aSStefan Hajnoczi {
8471d024e7aSMatthew Wilcox (Oracle) 	return __fuse_dax_fault(vmf, 0, true);
8482a9a609aSStefan Hajnoczi }
8492a9a609aSStefan Hajnoczi 
8502a9a609aSStefan Hajnoczi static const struct vm_operations_struct fuse_dax_vm_ops = {
8512a9a609aSStefan Hajnoczi 	.fault		= fuse_dax_fault,
8522a9a609aSStefan Hajnoczi 	.huge_fault	= fuse_dax_huge_fault,
8532a9a609aSStefan Hajnoczi 	.page_mkwrite	= fuse_dax_page_mkwrite,
8542a9a609aSStefan Hajnoczi 	.pfn_mkwrite	= fuse_dax_pfn_mkwrite,
8552a9a609aSStefan Hajnoczi };
8562a9a609aSStefan Hajnoczi 
fuse_dax_mmap(struct file * file,struct vm_area_struct * vma)8572a9a609aSStefan Hajnoczi int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma)
8582a9a609aSStefan Hajnoczi {
8592a9a609aSStefan Hajnoczi 	file_accessed(file);
8602a9a609aSStefan Hajnoczi 	vma->vm_ops = &fuse_dax_vm_ops;
8611c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_MIXEDMAP | VM_HUGEPAGE);
8622a9a609aSStefan Hajnoczi 	return 0;
8632a9a609aSStefan Hajnoczi }
8642a9a609aSStefan Hajnoczi 
dmap_writeback_invalidate(struct inode * inode,struct fuse_dax_mapping * dmap)8659a752d18SVivek Goyal static int dmap_writeback_invalidate(struct inode *inode,
8669a752d18SVivek Goyal 				     struct fuse_dax_mapping *dmap)
8679a752d18SVivek Goyal {
8689a752d18SVivek Goyal 	int ret;
8699a752d18SVivek Goyal 	loff_t start_pos = dmap->itn.start << FUSE_DAX_SHIFT;
8709a752d18SVivek Goyal 	loff_t end_pos = (start_pos + FUSE_DAX_SZ - 1);
8719a752d18SVivek Goyal 
8729a752d18SVivek Goyal 	ret = filemap_fdatawrite_range(inode->i_mapping, start_pos, end_pos);
8739a752d18SVivek Goyal 	if (ret) {
8749a752d18SVivek Goyal 		pr_debug("fuse: filemap_fdatawrite_range() failed. err=%d start_pos=0x%llx, end_pos=0x%llx\n",
8759a752d18SVivek Goyal 			 ret, start_pos, end_pos);
8769a752d18SVivek Goyal 		return ret;
8779a752d18SVivek Goyal 	}
8789a752d18SVivek Goyal 
8799a752d18SVivek Goyal 	ret = invalidate_inode_pages2_range(inode->i_mapping,
8809a752d18SVivek Goyal 					    start_pos >> PAGE_SHIFT,
8819a752d18SVivek Goyal 					    end_pos >> PAGE_SHIFT);
8829a752d18SVivek Goyal 	if (ret)
8839a752d18SVivek Goyal 		pr_debug("fuse: invalidate_inode_pages2_range() failed err=%d\n",
8849a752d18SVivek Goyal 			 ret);
8859a752d18SVivek Goyal 
8869a752d18SVivek Goyal 	return ret;
8879a752d18SVivek Goyal }
8889a752d18SVivek Goyal 
reclaim_one_dmap_locked(struct inode * inode,struct fuse_dax_mapping * dmap)8899a752d18SVivek Goyal static int reclaim_one_dmap_locked(struct inode *inode,
8909a752d18SVivek Goyal 				   struct fuse_dax_mapping *dmap)
8919a752d18SVivek Goyal {
8929a752d18SVivek Goyal 	int ret;
8939a752d18SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
8949a752d18SVivek Goyal 
8959a752d18SVivek Goyal 	/*
8969a752d18SVivek Goyal 	 * igrab() was done to make sure inode won't go under us, and this
8979a752d18SVivek Goyal 	 * further avoids the race with evict().
8989a752d18SVivek Goyal 	 */
8999a752d18SVivek Goyal 	ret = dmap_writeback_invalidate(inode, dmap);
9009a752d18SVivek Goyal 	if (ret)
9019a752d18SVivek Goyal 		return ret;
9029a752d18SVivek Goyal 
9039a752d18SVivek Goyal 	/* Remove dax mapping from inode interval tree now */
9049a752d18SVivek Goyal 	interval_tree_remove(&dmap->itn, &fi->dax->tree);
9059a752d18SVivek Goyal 	fi->dax->nr--;
9069a752d18SVivek Goyal 
9079a752d18SVivek Goyal 	/* It is possible that umount/shutdown has killed the fuse connection
9089a752d18SVivek Goyal 	 * and worker thread is trying to reclaim memory in parallel.  Don't
9099a752d18SVivek Goyal 	 * warn in that case.
9109a752d18SVivek Goyal 	 */
9119a752d18SVivek Goyal 	ret = dmap_removemapping_one(inode, dmap);
9129a752d18SVivek Goyal 	if (ret && ret != -ENOTCONN) {
9139a752d18SVivek Goyal 		pr_warn("Failed to remove mapping. offset=0x%llx len=0x%llx ret=%d\n",
9149a752d18SVivek Goyal 			dmap->window_offset, dmap->length, ret);
9159a752d18SVivek Goyal 	}
9169a752d18SVivek Goyal 	return 0;
9179a752d18SVivek Goyal }
9189a752d18SVivek Goyal 
9199a752d18SVivek Goyal /* Find first mapped dmap for an inode and return file offset. Caller needs
9209a752d18SVivek Goyal  * to hold fi->dax->sem lock either shared or exclusive.
9219a752d18SVivek Goyal  */
inode_lookup_first_dmap(struct inode * inode)9229a752d18SVivek Goyal static struct fuse_dax_mapping *inode_lookup_first_dmap(struct inode *inode)
9239a752d18SVivek Goyal {
9249a752d18SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
9259a752d18SVivek Goyal 	struct fuse_dax_mapping *dmap;
9269a752d18SVivek Goyal 	struct interval_tree_node *node;
9279a752d18SVivek Goyal 
9289a752d18SVivek Goyal 	for (node = interval_tree_iter_first(&fi->dax->tree, 0, -1); node;
9299a752d18SVivek Goyal 	     node = interval_tree_iter_next(node, 0, -1)) {
9309a752d18SVivek Goyal 		dmap = node_to_dmap(node);
9319a752d18SVivek Goyal 		/* still in use. */
9329a752d18SVivek Goyal 		if (refcount_read(&dmap->refcnt) > 1)
9339a752d18SVivek Goyal 			continue;
9349a752d18SVivek Goyal 
9359a752d18SVivek Goyal 		return dmap;
9369a752d18SVivek Goyal 	}
9379a752d18SVivek Goyal 
9389a752d18SVivek Goyal 	return NULL;
9399a752d18SVivek Goyal }
9409a752d18SVivek Goyal 
9419a752d18SVivek Goyal /*
9429a752d18SVivek Goyal  * Find first mapping in the tree and free it and return it. Do not add
9439a752d18SVivek Goyal  * it back to free pool.
9449a752d18SVivek Goyal  */
9459a752d18SVivek Goyal static struct fuse_dax_mapping *
inode_inline_reclaim_one_dmap(struct fuse_conn_dax * fcd,struct inode * inode,bool * retry)9469a752d18SVivek Goyal inode_inline_reclaim_one_dmap(struct fuse_conn_dax *fcd, struct inode *inode,
9479a752d18SVivek Goyal 			      bool *retry)
9489a752d18SVivek Goyal {
9499a752d18SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
9509a752d18SVivek Goyal 	struct fuse_dax_mapping *dmap;
9519a752d18SVivek Goyal 	u64 dmap_start, dmap_end;
9529a752d18SVivek Goyal 	unsigned long start_idx;
9539a752d18SVivek Goyal 	int ret;
9549a752d18SVivek Goyal 	struct interval_tree_node *node;
9559a752d18SVivek Goyal 
9568bcbbe9cSJan Kara 	filemap_invalidate_lock(inode->i_mapping);
9579a752d18SVivek Goyal 
9589a752d18SVivek Goyal 	/* Lookup a dmap and corresponding file offset to reclaim. */
9599a752d18SVivek Goyal 	down_read(&fi->dax->sem);
9609a752d18SVivek Goyal 	dmap = inode_lookup_first_dmap(inode);
9619a752d18SVivek Goyal 	if (dmap) {
9629a752d18SVivek Goyal 		start_idx = dmap->itn.start;
9639a752d18SVivek Goyal 		dmap_start = start_idx << FUSE_DAX_SHIFT;
9649a752d18SVivek Goyal 		dmap_end = dmap_start + FUSE_DAX_SZ - 1;
9659a752d18SVivek Goyal 	}
9669a752d18SVivek Goyal 	up_read(&fi->dax->sem);
9679a752d18SVivek Goyal 
9689a752d18SVivek Goyal 	if (!dmap)
9699a752d18SVivek Goyal 		goto out_mmap_sem;
9709a752d18SVivek Goyal 	/*
9719a752d18SVivek Goyal 	 * Make sure there are no references to inode pages using
9729a752d18SVivek Goyal 	 * get_user_pages()
9739a752d18SVivek Goyal 	 */
9749a752d18SVivek Goyal 	ret = fuse_dax_break_layouts(inode, dmap_start, dmap_end);
9759a752d18SVivek Goyal 	if (ret) {
9769a752d18SVivek Goyal 		pr_debug("fuse: fuse_dax_break_layouts() failed. err=%d\n",
9779a752d18SVivek Goyal 			 ret);
9789a752d18SVivek Goyal 		dmap = ERR_PTR(ret);
9799a752d18SVivek Goyal 		goto out_mmap_sem;
9809a752d18SVivek Goyal 	}
9819a752d18SVivek Goyal 
9829a752d18SVivek Goyal 	down_write(&fi->dax->sem);
9839a752d18SVivek Goyal 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
9849a752d18SVivek Goyal 	/* Range already got reclaimed by somebody else */
9859a752d18SVivek Goyal 	if (!node) {
9869a752d18SVivek Goyal 		if (retry)
9879a752d18SVivek Goyal 			*retry = true;
9889a752d18SVivek Goyal 		goto out_write_dmap_sem;
9899a752d18SVivek Goyal 	}
9909a752d18SVivek Goyal 
9919a752d18SVivek Goyal 	dmap = node_to_dmap(node);
9929a752d18SVivek Goyal 	/* still in use. */
9939a752d18SVivek Goyal 	if (refcount_read(&dmap->refcnt) > 1) {
9949a752d18SVivek Goyal 		dmap = NULL;
9959a752d18SVivek Goyal 		if (retry)
9969a752d18SVivek Goyal 			*retry = true;
9979a752d18SVivek Goyal 		goto out_write_dmap_sem;
9989a752d18SVivek Goyal 	}
9999a752d18SVivek Goyal 
10009a752d18SVivek Goyal 	ret = reclaim_one_dmap_locked(inode, dmap);
10019a752d18SVivek Goyal 	if (ret < 0) {
10029a752d18SVivek Goyal 		dmap = ERR_PTR(ret);
10039a752d18SVivek Goyal 		goto out_write_dmap_sem;
10049a752d18SVivek Goyal 	}
10059a752d18SVivek Goyal 
10069a752d18SVivek Goyal 	/* Clean up dmap. Do not add back to free list */
10079a752d18SVivek Goyal 	dmap_remove_busy_list(fcd, dmap);
10089a752d18SVivek Goyal 	dmap->inode = NULL;
10099a752d18SVivek Goyal 	dmap->itn.start = dmap->itn.last = 0;
10109a752d18SVivek Goyal 
10119a752d18SVivek Goyal 	pr_debug("fuse: %s: inline reclaimed memory range. inode=%p, window_offset=0x%llx, length=0x%llx\n",
10129a752d18SVivek Goyal 		 __func__, inode, dmap->window_offset, dmap->length);
10139a752d18SVivek Goyal 
10149a752d18SVivek Goyal out_write_dmap_sem:
10159a752d18SVivek Goyal 	up_write(&fi->dax->sem);
10169a752d18SVivek Goyal out_mmap_sem:
10178bcbbe9cSJan Kara 	filemap_invalidate_unlock(inode->i_mapping);
10189a752d18SVivek Goyal 	return dmap;
10199a752d18SVivek Goyal }
10209a752d18SVivek Goyal 
10219a752d18SVivek Goyal static struct fuse_dax_mapping *
alloc_dax_mapping_reclaim(struct fuse_conn_dax * fcd,struct inode * inode)10229a752d18SVivek Goyal alloc_dax_mapping_reclaim(struct fuse_conn_dax *fcd, struct inode *inode)
10239a752d18SVivek Goyal {
10249a752d18SVivek Goyal 	struct fuse_dax_mapping *dmap;
10259a752d18SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
10269a752d18SVivek Goyal 
10279a752d18SVivek Goyal 	while (1) {
10289a752d18SVivek Goyal 		bool retry = false;
10299a752d18SVivek Goyal 
10309a752d18SVivek Goyal 		dmap = alloc_dax_mapping(fcd);
10319a752d18SVivek Goyal 		if (dmap)
10329a752d18SVivek Goyal 			return dmap;
10339a752d18SVivek Goyal 
10349a752d18SVivek Goyal 		dmap = inode_inline_reclaim_one_dmap(fcd, inode, &retry);
10359a752d18SVivek Goyal 		/*
10369a752d18SVivek Goyal 		 * Either we got a mapping or it is an error, return in both
10379a752d18SVivek Goyal 		 * the cases.
10389a752d18SVivek Goyal 		 */
10399a752d18SVivek Goyal 		if (dmap)
10409a752d18SVivek Goyal 			return dmap;
10419a752d18SVivek Goyal 
10429a752d18SVivek Goyal 		/* If we could not reclaim a mapping because it
10439a752d18SVivek Goyal 		 * had a reference or some other temporary failure,
10449a752d18SVivek Goyal 		 * Try again. We want to give up inline reclaim only
10459a752d18SVivek Goyal 		 * if there is no range assigned to this node. Otherwise
10468bcbbe9cSJan Kara 		 * if a deadlock is possible if we sleep with
10478bcbbe9cSJan Kara 		 * mapping->invalidate_lock held and worker to free memory
10488bcbbe9cSJan Kara 		 * can't make progress due to unavailability of
10498bcbbe9cSJan Kara 		 * mapping->invalidate_lock.  So sleep only if fi->dax->nr=0
10509a752d18SVivek Goyal 		 */
10519a752d18SVivek Goyal 		if (retry)
10529a752d18SVivek Goyal 			continue;
10539a752d18SVivek Goyal 		/*
10549a752d18SVivek Goyal 		 * There are no mappings which can be reclaimed. Wait for one.
10559a752d18SVivek Goyal 		 * We are not holding fi->dax->sem. So it is possible
10569a752d18SVivek Goyal 		 * that range gets added now. But as we are not holding
10578bcbbe9cSJan Kara 		 * mapping->invalidate_lock, worker should still be able to
10588bcbbe9cSJan Kara 		 * free up a range and wake us up.
10599a752d18SVivek Goyal 		 */
10609a752d18SVivek Goyal 		if (!fi->dax->nr && !(fcd->nr_free_ranges > 0)) {
10619a752d18SVivek Goyal 			if (wait_event_killable_exclusive(fcd->range_waitq,
10629a752d18SVivek Goyal 					(fcd->nr_free_ranges > 0))) {
10639a752d18SVivek Goyal 				return ERR_PTR(-EINTR);
10649a752d18SVivek Goyal 			}
10659a752d18SVivek Goyal 		}
10669a752d18SVivek Goyal 	}
10679a752d18SVivek Goyal }
10689a752d18SVivek Goyal 
lookup_and_reclaim_dmap_locked(struct fuse_conn_dax * fcd,struct inode * inode,unsigned long start_idx)10699a752d18SVivek Goyal static int lookup_and_reclaim_dmap_locked(struct fuse_conn_dax *fcd,
10709a752d18SVivek Goyal 					  struct inode *inode,
10719a752d18SVivek Goyal 					  unsigned long start_idx)
10729a752d18SVivek Goyal {
10739a752d18SVivek Goyal 	int ret;
10749a752d18SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
10759a752d18SVivek Goyal 	struct fuse_dax_mapping *dmap;
10769a752d18SVivek Goyal 	struct interval_tree_node *node;
10779a752d18SVivek Goyal 
10789a752d18SVivek Goyal 	/* Find fuse dax mapping at file offset inode. */
10799a752d18SVivek Goyal 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
10809a752d18SVivek Goyal 
10819a752d18SVivek Goyal 	/* Range already got cleaned up by somebody else */
10829a752d18SVivek Goyal 	if (!node)
10839a752d18SVivek Goyal 		return 0;
10849a752d18SVivek Goyal 	dmap = node_to_dmap(node);
10859a752d18SVivek Goyal 
10869a752d18SVivek Goyal 	/* still in use. */
10879a752d18SVivek Goyal 	if (refcount_read(&dmap->refcnt) > 1)
10889a752d18SVivek Goyal 		return 0;
10899a752d18SVivek Goyal 
10909a752d18SVivek Goyal 	ret = reclaim_one_dmap_locked(inode, dmap);
10919a752d18SVivek Goyal 	if (ret < 0)
10929a752d18SVivek Goyal 		return ret;
10939a752d18SVivek Goyal 
10949a752d18SVivek Goyal 	/* Cleanup dmap entry and add back to free list */
10959a752d18SVivek Goyal 	spin_lock(&fcd->lock);
10969a752d18SVivek Goyal 	dmap_reinit_add_to_free_pool(fcd, dmap);
10979a752d18SVivek Goyal 	spin_unlock(&fcd->lock);
10989a752d18SVivek Goyal 	return ret;
10999a752d18SVivek Goyal }
11009a752d18SVivek Goyal 
11019a752d18SVivek Goyal /*
11029a752d18SVivek Goyal  * Free a range of memory.
11039a752d18SVivek Goyal  * Locking:
11048bcbbe9cSJan Kara  * 1. Take mapping->invalidate_lock to block dax faults.
11059a752d18SVivek Goyal  * 2. Take fi->dax->sem to protect interval tree and also to make sure
11069a752d18SVivek Goyal  *    read/write can not reuse a dmap which we might be freeing.
11079a752d18SVivek Goyal  */
lookup_and_reclaim_dmap(struct fuse_conn_dax * fcd,struct inode * inode,unsigned long start_idx,unsigned long end_idx)11089a752d18SVivek Goyal static int lookup_and_reclaim_dmap(struct fuse_conn_dax *fcd,
11099a752d18SVivek Goyal 				   struct inode *inode,
11109a752d18SVivek Goyal 				   unsigned long start_idx,
11119a752d18SVivek Goyal 				   unsigned long end_idx)
11129a752d18SVivek Goyal {
11139a752d18SVivek Goyal 	int ret;
11149a752d18SVivek Goyal 	struct fuse_inode *fi = get_fuse_inode(inode);
11159a752d18SVivek Goyal 	loff_t dmap_start = start_idx << FUSE_DAX_SHIFT;
11169a752d18SVivek Goyal 	loff_t dmap_end = (dmap_start + FUSE_DAX_SZ) - 1;
11179a752d18SVivek Goyal 
11188bcbbe9cSJan Kara 	filemap_invalidate_lock(inode->i_mapping);
11199a752d18SVivek Goyal 	ret = fuse_dax_break_layouts(inode, dmap_start, dmap_end);
11209a752d18SVivek Goyal 	if (ret) {
11219a752d18SVivek Goyal 		pr_debug("virtio_fs: fuse_dax_break_layouts() failed. err=%d\n",
11229a752d18SVivek Goyal 			 ret);
11239a752d18SVivek Goyal 		goto out_mmap_sem;
11249a752d18SVivek Goyal 	}
11259a752d18SVivek Goyal 
11269a752d18SVivek Goyal 	down_write(&fi->dax->sem);
11279a752d18SVivek Goyal 	ret = lookup_and_reclaim_dmap_locked(fcd, inode, start_idx);
11289a752d18SVivek Goyal 	up_write(&fi->dax->sem);
11299a752d18SVivek Goyal out_mmap_sem:
11308bcbbe9cSJan Kara 	filemap_invalidate_unlock(inode->i_mapping);
11319a752d18SVivek Goyal 	return ret;
11329a752d18SVivek Goyal }
11339a752d18SVivek Goyal 
try_to_free_dmap_chunks(struct fuse_conn_dax * fcd,unsigned long nr_to_free)11349a752d18SVivek Goyal static int try_to_free_dmap_chunks(struct fuse_conn_dax *fcd,
11359a752d18SVivek Goyal 				   unsigned long nr_to_free)
11369a752d18SVivek Goyal {
11379a752d18SVivek Goyal 	struct fuse_dax_mapping *dmap, *pos, *temp;
11389a752d18SVivek Goyal 	int ret, nr_freed = 0;
11399a752d18SVivek Goyal 	unsigned long start_idx = 0, end_idx = 0;
11409a752d18SVivek Goyal 	struct inode *inode = NULL;
11419a752d18SVivek Goyal 
11429a752d18SVivek Goyal 	/* Pick first busy range and free it for now*/
11439a752d18SVivek Goyal 	while (1) {
11449a752d18SVivek Goyal 		if (nr_freed >= nr_to_free)
11459a752d18SVivek Goyal 			break;
11469a752d18SVivek Goyal 
11479a752d18SVivek Goyal 		dmap = NULL;
11489a752d18SVivek Goyal 		spin_lock(&fcd->lock);
11499a752d18SVivek Goyal 
11509a752d18SVivek Goyal 		if (!fcd->nr_busy_ranges) {
11519a752d18SVivek Goyal 			spin_unlock(&fcd->lock);
11529a752d18SVivek Goyal 			return 0;
11539a752d18SVivek Goyal 		}
11549a752d18SVivek Goyal 
11559a752d18SVivek Goyal 		list_for_each_entry_safe(pos, temp, &fcd->busy_ranges,
11569a752d18SVivek Goyal 						busy_list) {
11579a752d18SVivek Goyal 			/* skip this range if it's in use. */
11589a752d18SVivek Goyal 			if (refcount_read(&pos->refcnt) > 1)
11599a752d18SVivek Goyal 				continue;
11609a752d18SVivek Goyal 
11619a752d18SVivek Goyal 			inode = igrab(pos->inode);
11629a752d18SVivek Goyal 			/*
11639a752d18SVivek Goyal 			 * This inode is going away. That will free
11649a752d18SVivek Goyal 			 * up all the ranges anyway, continue to
11659a752d18SVivek Goyal 			 * next range.
11669a752d18SVivek Goyal 			 */
11679a752d18SVivek Goyal 			if (!inode)
11689a752d18SVivek Goyal 				continue;
11699a752d18SVivek Goyal 			/*
11709a752d18SVivek Goyal 			 * Take this element off list and add it tail. If
11719a752d18SVivek Goyal 			 * this element can't be freed, it will help with
11729a752d18SVivek Goyal 			 * selecting new element in next iteration of loop.
11739a752d18SVivek Goyal 			 */
11749a752d18SVivek Goyal 			dmap = pos;
11759a752d18SVivek Goyal 			list_move_tail(&dmap->busy_list, &fcd->busy_ranges);
11769a752d18SVivek Goyal 			start_idx = end_idx = dmap->itn.start;
11779a752d18SVivek Goyal 			break;
11789a752d18SVivek Goyal 		}
11799a752d18SVivek Goyal 		spin_unlock(&fcd->lock);
11809a752d18SVivek Goyal 		if (!dmap)
11819a752d18SVivek Goyal 			return 0;
11829a752d18SVivek Goyal 
11839a752d18SVivek Goyal 		ret = lookup_and_reclaim_dmap(fcd, inode, start_idx, end_idx);
11849a752d18SVivek Goyal 		iput(inode);
11859a752d18SVivek Goyal 		if (ret)
11869a752d18SVivek Goyal 			return ret;
11879a752d18SVivek Goyal 		nr_freed++;
11889a752d18SVivek Goyal 	}
11899a752d18SVivek Goyal 	return 0;
11909a752d18SVivek Goyal }
11919a752d18SVivek Goyal 
fuse_dax_free_mem_worker(struct work_struct * work)11929a752d18SVivek Goyal static void fuse_dax_free_mem_worker(struct work_struct *work)
11939a752d18SVivek Goyal {
11949a752d18SVivek Goyal 	int ret;
11959a752d18SVivek Goyal 	struct fuse_conn_dax *fcd = container_of(work, struct fuse_conn_dax,
11969a752d18SVivek Goyal 						 free_work.work);
11979a752d18SVivek Goyal 	ret = try_to_free_dmap_chunks(fcd, FUSE_DAX_RECLAIM_CHUNK);
11989a752d18SVivek Goyal 	if (ret) {
11999a752d18SVivek Goyal 		pr_debug("fuse: try_to_free_dmap_chunks() failed with err=%d\n",
12009a752d18SVivek Goyal 			 ret);
12019a752d18SVivek Goyal 	}
12029a752d18SVivek Goyal 
1203c4e0cd4eSZheng Yongjun 	/* If number of free ranges are still below threshold, requeue */
12049a752d18SVivek Goyal 	kick_dmap_free_worker(fcd, 1);
12059a752d18SVivek Goyal }
12069a752d18SVivek Goyal 
fuse_free_dax_mem_ranges(struct list_head * mem_list)120745f2348eSVivek Goyal static void fuse_free_dax_mem_ranges(struct list_head *mem_list)
120845f2348eSVivek Goyal {
120945f2348eSVivek Goyal 	struct fuse_dax_mapping *range, *temp;
121045f2348eSVivek Goyal 
121145f2348eSVivek Goyal 	/* Free All allocated elements */
121245f2348eSVivek Goyal 	list_for_each_entry_safe(range, temp, mem_list, list) {
121345f2348eSVivek Goyal 		list_del(&range->list);
1214d0cfb9dcSVivek Goyal 		if (!list_empty(&range->busy_list))
1215d0cfb9dcSVivek Goyal 			list_del(&range->busy_list);
121645f2348eSVivek Goyal 		kfree(range);
121745f2348eSVivek Goyal 	}
121845f2348eSVivek Goyal }
121945f2348eSVivek Goyal 
fuse_dax_conn_free(struct fuse_conn * fc)12201dd53957SVivek Goyal void fuse_dax_conn_free(struct fuse_conn *fc)
12211dd53957SVivek Goyal {
122245f2348eSVivek Goyal 	if (fc->dax) {
122345f2348eSVivek Goyal 		fuse_free_dax_mem_ranges(&fc->dax->free_ranges);
12241dd53957SVivek Goyal 		kfree(fc->dax);
1225*7f8ed28dSHangyu Hua 		fc->dax = NULL;
12261dd53957SVivek Goyal 	}
122745f2348eSVivek Goyal }
122845f2348eSVivek Goyal 
fuse_dax_mem_range_init(struct fuse_conn_dax * fcd)122945f2348eSVivek Goyal static int fuse_dax_mem_range_init(struct fuse_conn_dax *fcd)
123045f2348eSVivek Goyal {
123145f2348eSVivek Goyal 	long nr_pages, nr_ranges;
123245f2348eSVivek Goyal 	struct fuse_dax_mapping *range;
123345f2348eSVivek Goyal 	int ret, id;
123445f2348eSVivek Goyal 	size_t dax_size = -1;
123545f2348eSVivek Goyal 	unsigned long i;
123645f2348eSVivek Goyal 
12379a752d18SVivek Goyal 	init_waitqueue_head(&fcd->range_waitq);
123845f2348eSVivek Goyal 	INIT_LIST_HEAD(&fcd->free_ranges);
1239d0cfb9dcSVivek Goyal 	INIT_LIST_HEAD(&fcd->busy_ranges);
12409a752d18SVivek Goyal 	INIT_DELAYED_WORK(&fcd->free_work, fuse_dax_free_mem_worker);
12419a752d18SVivek Goyal 
124245f2348eSVivek Goyal 	id = dax_read_lock();
1243e511c4a3SJane Chu 	nr_pages = dax_direct_access(fcd->dev, 0, PHYS_PFN(dax_size),
1244e511c4a3SJane Chu 			DAX_ACCESS, NULL, NULL);
124545f2348eSVivek Goyal 	dax_read_unlock(id);
124645f2348eSVivek Goyal 	if (nr_pages < 0) {
124745f2348eSVivek Goyal 		pr_debug("dax_direct_access() returned %ld\n", nr_pages);
124845f2348eSVivek Goyal 		return nr_pages;
124945f2348eSVivek Goyal 	}
125045f2348eSVivek Goyal 
125145f2348eSVivek Goyal 	nr_ranges = nr_pages/FUSE_DAX_PAGES;
125245f2348eSVivek Goyal 	pr_debug("%s: dax mapped %ld pages. nr_ranges=%ld\n",
125345f2348eSVivek Goyal 		__func__, nr_pages, nr_ranges);
125445f2348eSVivek Goyal 
125545f2348eSVivek Goyal 	for (i = 0; i < nr_ranges; i++) {
125645f2348eSVivek Goyal 		range = kzalloc(sizeof(struct fuse_dax_mapping), GFP_KERNEL);
125745f2348eSVivek Goyal 		ret = -ENOMEM;
125845f2348eSVivek Goyal 		if (!range)
125945f2348eSVivek Goyal 			goto out_err;
126045f2348eSVivek Goyal 
126145f2348eSVivek Goyal 		/* TODO: This offset only works if virtio-fs driver is not
126245f2348eSVivek Goyal 		 * having some memory hidden at the beginning. This needs
126345f2348eSVivek Goyal 		 * better handling
126445f2348eSVivek Goyal 		 */
126545f2348eSVivek Goyal 		range->window_offset = i * FUSE_DAX_SZ;
126645f2348eSVivek Goyal 		range->length = FUSE_DAX_SZ;
1267d0cfb9dcSVivek Goyal 		INIT_LIST_HEAD(&range->busy_list);
12689a752d18SVivek Goyal 		refcount_set(&range->refcnt, 1);
126945f2348eSVivek Goyal 		list_add_tail(&range->list, &fcd->free_ranges);
127045f2348eSVivek Goyal 	}
127145f2348eSVivek Goyal 
127245f2348eSVivek Goyal 	fcd->nr_free_ranges = nr_ranges;
12739a752d18SVivek Goyal 	fcd->nr_ranges = nr_ranges;
127445f2348eSVivek Goyal 	return 0;
127545f2348eSVivek Goyal out_err:
127645f2348eSVivek Goyal 	/* Free All allocated elements */
127745f2348eSVivek Goyal 	fuse_free_dax_mem_ranges(&fcd->free_ranges);
127845f2348eSVivek Goyal 	return ret;
127945f2348eSVivek Goyal }
12801dd53957SVivek Goyal 
fuse_dax_conn_alloc(struct fuse_conn * fc,enum fuse_dax_mode dax_mode,struct dax_device * dax_dev)1281780b1b95SJeffle Xu int fuse_dax_conn_alloc(struct fuse_conn *fc, enum fuse_dax_mode dax_mode,
1282780b1b95SJeffle Xu 			struct dax_device *dax_dev)
12831dd53957SVivek Goyal {
12841dd53957SVivek Goyal 	struct fuse_conn_dax *fcd;
128545f2348eSVivek Goyal 	int err;
12861dd53957SVivek Goyal 
1287780b1b95SJeffle Xu 	fc->dax_mode = dax_mode;
1288780b1b95SJeffle Xu 
12891dd53957SVivek Goyal 	if (!dax_dev)
12901dd53957SVivek Goyal 		return 0;
12911dd53957SVivek Goyal 
12921dd53957SVivek Goyal 	fcd = kzalloc(sizeof(*fcd), GFP_KERNEL);
12931dd53957SVivek Goyal 	if (!fcd)
12941dd53957SVivek Goyal 		return -ENOMEM;
12951dd53957SVivek Goyal 
1296c2d0ad00SVivek Goyal 	spin_lock_init(&fcd->lock);
12971dd53957SVivek Goyal 	fcd->dev = dax_dev;
129845f2348eSVivek Goyal 	err = fuse_dax_mem_range_init(fcd);
129945f2348eSVivek Goyal 	if (err) {
130045f2348eSVivek Goyal 		kfree(fcd);
130145f2348eSVivek Goyal 		return err;
130245f2348eSVivek Goyal 	}
13031dd53957SVivek Goyal 
13041dd53957SVivek Goyal 	fc->dax = fcd;
13051dd53957SVivek Goyal 	return 0;
13061dd53957SVivek Goyal }
1307fd1a1dc6SStefan Hajnoczi 
fuse_dax_inode_alloc(struct super_block * sb,struct fuse_inode * fi)1308c2d0ad00SVivek Goyal bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi)
1309c2d0ad00SVivek Goyal {
1310c2d0ad00SVivek Goyal 	struct fuse_conn *fc = get_fuse_conn_super(sb);
1311c2d0ad00SVivek Goyal 
1312c2d0ad00SVivek Goyal 	fi->dax = NULL;
1313c2d0ad00SVivek Goyal 	if (fc->dax) {
1314c2d0ad00SVivek Goyal 		fi->dax = kzalloc(sizeof(*fi->dax), GFP_KERNEL_ACCOUNT);
1315c2d0ad00SVivek Goyal 		if (!fi->dax)
1316c2d0ad00SVivek Goyal 			return false;
1317c2d0ad00SVivek Goyal 
1318c2d0ad00SVivek Goyal 		init_rwsem(&fi->dax->sem);
1319c2d0ad00SVivek Goyal 		fi->dax->tree = RB_ROOT_CACHED;
1320c2d0ad00SVivek Goyal 	}
1321c2d0ad00SVivek Goyal 
1322c2d0ad00SVivek Goyal 	return true;
1323c2d0ad00SVivek Goyal }
1324c2d0ad00SVivek Goyal 
13259483e7d5SVivek Goyal static const struct address_space_operations fuse_dax_file_aops  = {
13269483e7d5SVivek Goyal 	.writepages	= fuse_dax_writepages,
13279483e7d5SVivek Goyal 	.direct_IO	= noop_direct_IO,
132846de8b97SMatthew Wilcox (Oracle) 	.dirty_folio	= noop_dirty_folio,
13299483e7d5SVivek Goyal };
13309483e7d5SVivek Goyal 
fuse_should_enable_dax(struct inode * inode,unsigned int flags)133193a497b9SJeffle Xu static bool fuse_should_enable_dax(struct inode *inode, unsigned int flags)
1332c2d0ad00SVivek Goyal {
1333c2d0ad00SVivek Goyal 	struct fuse_conn *fc = get_fuse_conn(inode);
1334780b1b95SJeffle Xu 	enum fuse_dax_mode dax_mode = fc->dax_mode;
1335c2d0ad00SVivek Goyal 
1336780b1b95SJeffle Xu 	if (dax_mode == FUSE_DAX_NEVER)
1337780b1b95SJeffle Xu 		return false;
1338780b1b95SJeffle Xu 
1339780b1b95SJeffle Xu 	/*
1340780b1b95SJeffle Xu 	 * fc->dax may be NULL in 'inode' mode when filesystem device doesn't
1341780b1b95SJeffle Xu 	 * support DAX, in which case it will silently fallback to 'never' mode.
1342780b1b95SJeffle Xu 	 */
1343c2d0ad00SVivek Goyal 	if (!fc->dax)
1344cecd4916SJeffle Xu 		return false;
1345cecd4916SJeffle Xu 
134693a497b9SJeffle Xu 	if (dax_mode == FUSE_DAX_ALWAYS)
1347cecd4916SJeffle Xu 		return true;
134893a497b9SJeffle Xu 
134993a497b9SJeffle Xu 	/* dax_mode is FUSE_DAX_INODE* */
13502ee019faSJeffle Xu 	return fc->inode_dax && (flags & FUSE_ATTR_DAX);
1351cecd4916SJeffle Xu }
1352cecd4916SJeffle Xu 
fuse_dax_inode_init(struct inode * inode,unsigned int flags)135393a497b9SJeffle Xu void fuse_dax_inode_init(struct inode *inode, unsigned int flags)
1354cecd4916SJeffle Xu {
135593a497b9SJeffle Xu 	if (!fuse_should_enable_dax(inode, flags))
1356c2d0ad00SVivek Goyal 		return;
1357c2d0ad00SVivek Goyal 
1358c2d0ad00SVivek Goyal 	inode->i_flags |= S_DAX;
13599483e7d5SVivek Goyal 	inode->i_data.a_ops = &fuse_dax_file_aops;
1360c2d0ad00SVivek Goyal }
1361c2d0ad00SVivek Goyal 
fuse_dax_dontcache(struct inode * inode,unsigned int flags)1362c3cb6f93SJeffle Xu void fuse_dax_dontcache(struct inode *inode, unsigned int flags)
1363c3cb6f93SJeffle Xu {
1364c3cb6f93SJeffle Xu 	struct fuse_conn *fc = get_fuse_conn(inode);
1365c3cb6f93SJeffle Xu 
1366c3cb6f93SJeffle Xu 	if (fuse_is_inode_dax_mode(fc->dax_mode) &&
1367c3cb6f93SJeffle Xu 	    ((bool) IS_DAX(inode) != (bool) (flags & FUSE_ATTR_DAX)))
1368c3cb6f93SJeffle Xu 		d_mark_dontcache(inode);
1369c3cb6f93SJeffle Xu }
1370c3cb6f93SJeffle Xu 
fuse_dax_check_alignment(struct fuse_conn * fc,unsigned int map_alignment)1371fd1a1dc6SStefan Hajnoczi bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment)
1372fd1a1dc6SStefan Hajnoczi {
1373fd1a1dc6SStefan Hajnoczi 	if (fc->dax && (map_alignment > FUSE_DAX_SHIFT)) {
1374fd1a1dc6SStefan Hajnoczi 		pr_warn("FUSE: map_alignment %u incompatible with dax mem range size %u\n",
1375fd1a1dc6SStefan Hajnoczi 			map_alignment, FUSE_DAX_SZ);
1376fd1a1dc6SStefan Hajnoczi 		return false;
1377fd1a1dc6SStefan Hajnoczi 	}
1378fd1a1dc6SStefan Hajnoczi 	return true;
1379fd1a1dc6SStefan Hajnoczi }
13809a752d18SVivek Goyal 
fuse_dax_cancel_work(struct fuse_conn * fc)13819a752d18SVivek Goyal void fuse_dax_cancel_work(struct fuse_conn *fc)
13829a752d18SVivek Goyal {
13839a752d18SVivek Goyal 	struct fuse_conn_dax *fcd = fc->dax;
13849a752d18SVivek Goyal 
13859a752d18SVivek Goyal 	if (fcd)
13869a752d18SVivek Goyal 		cancel_delayed_work_sync(&fcd->free_work);
13879a752d18SVivek Goyal 
13889a752d18SVivek Goyal }
13899a752d18SVivek Goyal EXPORT_SYMBOL_GPL(fuse_dax_cancel_work);
1390