xref: /linux/fs/fuse/dax.c (revision 0686082dbf7a204ca0fab326a820779e31666639)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dax: direct host memory access
4  * Copyright (C) 2020 Red Hat, Inc.
5  */
6 
7 #include "fuse_i.h"
8 
9 #include <linux/delay.h>
10 #include <linux/dax.h>
11 #include <linux/uio.h>
12 #include <linux/pagemap.h>
13 #include <linux/pfn_t.h>
14 #include <linux/iomap.h>
15 #include <linux/interval_tree.h>
16 
17 /*
18  * Default memory range size.  A power of 2 so it agrees with common FUSE_INIT
19  * map_alignment values 4KB and 64KB.
20  */
21 #define FUSE_DAX_SHIFT	21
22 #define FUSE_DAX_SZ	(1 << FUSE_DAX_SHIFT)
23 #define FUSE_DAX_PAGES	(FUSE_DAX_SZ / PAGE_SIZE)
24 
25 /* Number of ranges reclaimer will try to free in one invocation */
26 #define FUSE_DAX_RECLAIM_CHUNK		(10)
27 
28 /*
29  * Dax memory reclaim threshold in percetage of total ranges. When free
30  * number of free ranges drops below this threshold, reclaim can trigger
31  * Default is 20%
32  */
33 #define FUSE_DAX_RECLAIM_THRESHOLD	(20)
34 
35 /** Translation information for file offsets to DAX window offsets */
36 struct fuse_dax_mapping {
37 	/* Pointer to inode where this memory range is mapped */
38 	struct inode *inode;
39 
40 	/* Will connect in fcd->free_ranges to keep track of free memory */
41 	struct list_head list;
42 
43 	/* For interval tree in file/inode */
44 	struct interval_tree_node itn;
45 
46 	/* Will connect in fc->busy_ranges to keep track busy memory */
47 	struct list_head busy_list;
48 
49 	/** Position in DAX window */
50 	u64 window_offset;
51 
52 	/** Length of mapping, in bytes */
53 	loff_t length;
54 
55 	/* Is this mapping read-only or read-write */
56 	bool writable;
57 
58 	/* reference count when the mapping is used by dax iomap. */
59 	refcount_t refcnt;
60 };
61 
62 /* Per-inode dax map */
63 struct fuse_inode_dax {
64 	/* Semaphore to protect modifications to the dmap tree */
65 	struct rw_semaphore sem;
66 
67 	/* Sorted rb tree of struct fuse_dax_mapping elements */
68 	struct rb_root_cached tree;
69 	unsigned long nr;
70 };
71 
72 struct fuse_conn_dax {
73 	/* DAX device */
74 	struct dax_device *dev;
75 
76 	/* Lock protecting accessess to  members of this structure */
77 	spinlock_t lock;
78 
79 	/* List of memory ranges which are busy */
80 	unsigned long nr_busy_ranges;
81 	struct list_head busy_ranges;
82 
83 	/* Worker to free up memory ranges */
84 	struct delayed_work free_work;
85 
86 	/* Wait queue for a dax range to become free */
87 	wait_queue_head_t range_waitq;
88 
89 	/* DAX Window Free Ranges */
90 	long nr_free_ranges;
91 	struct list_head free_ranges;
92 
93 	unsigned long nr_ranges;
94 };
95 
96 static inline struct fuse_dax_mapping *
97 node_to_dmap(struct interval_tree_node *node)
98 {
99 	if (!node)
100 		return NULL;
101 
102 	return container_of(node, struct fuse_dax_mapping, itn);
103 }
104 
105 static struct fuse_dax_mapping *
106 alloc_dax_mapping_reclaim(struct fuse_conn_dax *fcd, struct inode *inode);
107 
108 static void
109 __kick_dmap_free_worker(struct fuse_conn_dax *fcd, unsigned long delay_ms)
110 {
111 	unsigned long free_threshold;
112 
113 	/* If number of free ranges are below threshold, start reclaim */
114 	free_threshold = max_t(unsigned long, fcd->nr_ranges * FUSE_DAX_RECLAIM_THRESHOLD / 100,
115 			     1);
116 	if (fcd->nr_free_ranges < free_threshold)
117 		queue_delayed_work(system_long_wq, &fcd->free_work,
118 				   msecs_to_jiffies(delay_ms));
119 }
120 
121 static void kick_dmap_free_worker(struct fuse_conn_dax *fcd,
122 				  unsigned long delay_ms)
123 {
124 	spin_lock(&fcd->lock);
125 	__kick_dmap_free_worker(fcd, delay_ms);
126 	spin_unlock(&fcd->lock);
127 }
128 
129 static struct fuse_dax_mapping *alloc_dax_mapping(struct fuse_conn_dax *fcd)
130 {
131 	struct fuse_dax_mapping *dmap;
132 
133 	spin_lock(&fcd->lock);
134 	dmap = list_first_entry_or_null(&fcd->free_ranges,
135 					struct fuse_dax_mapping, list);
136 	if (dmap) {
137 		list_del_init(&dmap->list);
138 		WARN_ON(fcd->nr_free_ranges <= 0);
139 		fcd->nr_free_ranges--;
140 	}
141 	spin_unlock(&fcd->lock);
142 
143 	kick_dmap_free_worker(fcd, 0);
144 	return dmap;
145 }
146 
147 /* This assumes fcd->lock is held */
148 static void __dmap_remove_busy_list(struct fuse_conn_dax *fcd,
149 				    struct fuse_dax_mapping *dmap)
150 {
151 	list_del_init(&dmap->busy_list);
152 	WARN_ON(fcd->nr_busy_ranges == 0);
153 	fcd->nr_busy_ranges--;
154 }
155 
156 static void dmap_remove_busy_list(struct fuse_conn_dax *fcd,
157 				  struct fuse_dax_mapping *dmap)
158 {
159 	spin_lock(&fcd->lock);
160 	__dmap_remove_busy_list(fcd, dmap);
161 	spin_unlock(&fcd->lock);
162 }
163 
164 /* This assumes fcd->lock is held */
165 static void __dmap_add_to_free_pool(struct fuse_conn_dax *fcd,
166 				struct fuse_dax_mapping *dmap)
167 {
168 	list_add_tail(&dmap->list, &fcd->free_ranges);
169 	fcd->nr_free_ranges++;
170 	wake_up(&fcd->range_waitq);
171 }
172 
173 static void dmap_add_to_free_pool(struct fuse_conn_dax *fcd,
174 				struct fuse_dax_mapping *dmap)
175 {
176 	/* Return fuse_dax_mapping to free list */
177 	spin_lock(&fcd->lock);
178 	__dmap_add_to_free_pool(fcd, dmap);
179 	spin_unlock(&fcd->lock);
180 }
181 
182 static int fuse_setup_one_mapping(struct inode *inode, unsigned long start_idx,
183 				  struct fuse_dax_mapping *dmap, bool writable,
184 				  bool upgrade)
185 {
186 	struct fuse_mount *fm = get_fuse_mount(inode);
187 	struct fuse_conn_dax *fcd = fm->fc->dax;
188 	struct fuse_inode *fi = get_fuse_inode(inode);
189 	struct fuse_setupmapping_in inarg;
190 	loff_t offset = start_idx << FUSE_DAX_SHIFT;
191 	FUSE_ARGS(args);
192 	ssize_t err;
193 
194 	WARN_ON(fcd->nr_free_ranges < 0);
195 
196 	/* Ask fuse daemon to setup mapping */
197 	memset(&inarg, 0, sizeof(inarg));
198 	inarg.foffset = offset;
199 	inarg.fh = -1;
200 	inarg.moffset = dmap->window_offset;
201 	inarg.len = FUSE_DAX_SZ;
202 	inarg.flags |= FUSE_SETUPMAPPING_FLAG_READ;
203 	if (writable)
204 		inarg.flags |= FUSE_SETUPMAPPING_FLAG_WRITE;
205 	args.opcode = FUSE_SETUPMAPPING;
206 	args.nodeid = fi->nodeid;
207 	args.in_numargs = 1;
208 	args.in_args[0].size = sizeof(inarg);
209 	args.in_args[0].value = &inarg;
210 	err = fuse_simple_request(fm, &args);
211 	if (err < 0)
212 		return err;
213 	dmap->writable = writable;
214 	if (!upgrade) {
215 		/*
216 		 * We don't take a reference on inode. inode is valid right now
217 		 * and when inode is going away, cleanup logic should first
218 		 * cleanup dmap entries.
219 		 */
220 		dmap->inode = inode;
221 		dmap->itn.start = dmap->itn.last = start_idx;
222 		/* Protected by fi->dax->sem */
223 		interval_tree_insert(&dmap->itn, &fi->dax->tree);
224 		fi->dax->nr++;
225 		spin_lock(&fcd->lock);
226 		list_add_tail(&dmap->busy_list, &fcd->busy_ranges);
227 		fcd->nr_busy_ranges++;
228 		spin_unlock(&fcd->lock);
229 	}
230 	return 0;
231 }
232 
233 static int fuse_send_removemapping(struct inode *inode,
234 				   struct fuse_removemapping_in *inargp,
235 				   struct fuse_removemapping_one *remove_one)
236 {
237 	struct fuse_inode *fi = get_fuse_inode(inode);
238 	struct fuse_mount *fm = get_fuse_mount(inode);
239 	FUSE_ARGS(args);
240 
241 	args.opcode = FUSE_REMOVEMAPPING;
242 	args.nodeid = fi->nodeid;
243 	args.in_numargs = 2;
244 	args.in_args[0].size = sizeof(*inargp);
245 	args.in_args[0].value = inargp;
246 	args.in_args[1].size = inargp->count * sizeof(*remove_one);
247 	args.in_args[1].value = remove_one;
248 	return fuse_simple_request(fm, &args);
249 }
250 
251 static int dmap_removemapping_list(struct inode *inode, unsigned int num,
252 				   struct list_head *to_remove)
253 {
254 	struct fuse_removemapping_one *remove_one, *ptr;
255 	struct fuse_removemapping_in inarg;
256 	struct fuse_dax_mapping *dmap;
257 	int ret, i = 0, nr_alloc;
258 
259 	nr_alloc = min_t(unsigned int, num, FUSE_REMOVEMAPPING_MAX_ENTRY);
260 	remove_one = kmalloc_array(nr_alloc, sizeof(*remove_one), GFP_NOFS);
261 	if (!remove_one)
262 		return -ENOMEM;
263 
264 	ptr = remove_one;
265 	list_for_each_entry(dmap, to_remove, list) {
266 		ptr->moffset = dmap->window_offset;
267 		ptr->len = dmap->length;
268 		ptr++;
269 		i++;
270 		num--;
271 		if (i >= nr_alloc || num == 0) {
272 			memset(&inarg, 0, sizeof(inarg));
273 			inarg.count = i;
274 			ret = fuse_send_removemapping(inode, &inarg,
275 						      remove_one);
276 			if (ret)
277 				goto out;
278 			ptr = remove_one;
279 			i = 0;
280 		}
281 	}
282 out:
283 	kfree(remove_one);
284 	return ret;
285 }
286 
287 /*
288  * Cleanup dmap entry and add back to free list. This should be called with
289  * fcd->lock held.
290  */
291 static void dmap_reinit_add_to_free_pool(struct fuse_conn_dax *fcd,
292 					    struct fuse_dax_mapping *dmap)
293 {
294 	pr_debug("fuse: freeing memory range start_idx=0x%lx end_idx=0x%lx window_offset=0x%llx length=0x%llx\n",
295 		 dmap->itn.start, dmap->itn.last, dmap->window_offset,
296 		 dmap->length);
297 	__dmap_remove_busy_list(fcd, dmap);
298 	dmap->inode = NULL;
299 	dmap->itn.start = dmap->itn.last = 0;
300 	__dmap_add_to_free_pool(fcd, dmap);
301 }
302 
303 /*
304  * Free inode dmap entries whose range falls inside [start, end].
305  * Does not take any locks. At this point of time it should only be
306  * called from evict_inode() path where we know all dmap entries can be
307  * reclaimed.
308  */
309 static void inode_reclaim_dmap_range(struct fuse_conn_dax *fcd,
310 				     struct inode *inode,
311 				     loff_t start, loff_t end)
312 {
313 	struct fuse_inode *fi = get_fuse_inode(inode);
314 	struct fuse_dax_mapping *dmap, *n;
315 	int err, num = 0;
316 	LIST_HEAD(to_remove);
317 	unsigned long start_idx = start >> FUSE_DAX_SHIFT;
318 	unsigned long end_idx = end >> FUSE_DAX_SHIFT;
319 	struct interval_tree_node *node;
320 
321 	while (1) {
322 		node = interval_tree_iter_first(&fi->dax->tree, start_idx,
323 						end_idx);
324 		if (!node)
325 			break;
326 		dmap = node_to_dmap(node);
327 		/* inode is going away. There should not be any users of dmap */
328 		WARN_ON(refcount_read(&dmap->refcnt) > 1);
329 		interval_tree_remove(&dmap->itn, &fi->dax->tree);
330 		num++;
331 		list_add(&dmap->list, &to_remove);
332 	}
333 
334 	/* Nothing to remove */
335 	if (list_empty(&to_remove))
336 		return;
337 
338 	WARN_ON(fi->dax->nr < num);
339 	fi->dax->nr -= num;
340 	err = dmap_removemapping_list(inode, num, &to_remove);
341 	if (err && err != -ENOTCONN) {
342 		pr_warn("Failed to removemappings. start=0x%llx end=0x%llx\n",
343 			start, end);
344 	}
345 	spin_lock(&fcd->lock);
346 	list_for_each_entry_safe(dmap, n, &to_remove, list) {
347 		list_del_init(&dmap->list);
348 		dmap_reinit_add_to_free_pool(fcd, dmap);
349 	}
350 	spin_unlock(&fcd->lock);
351 }
352 
353 static int dmap_removemapping_one(struct inode *inode,
354 				  struct fuse_dax_mapping *dmap)
355 {
356 	struct fuse_removemapping_one forget_one;
357 	struct fuse_removemapping_in inarg;
358 
359 	memset(&inarg, 0, sizeof(inarg));
360 	inarg.count = 1;
361 	memset(&forget_one, 0, sizeof(forget_one));
362 	forget_one.moffset = dmap->window_offset;
363 	forget_one.len = dmap->length;
364 
365 	return fuse_send_removemapping(inode, &inarg, &forget_one);
366 }
367 
368 /*
369  * It is called from evict_inode() and by that time inode is going away. So
370  * this function does not take any locks like fi->dax->sem for traversing
371  * that fuse inode interval tree. If that lock is taken then lock validator
372  * complains of deadlock situation w.r.t fs_reclaim lock.
373  */
374 void fuse_dax_inode_cleanup(struct inode *inode)
375 {
376 	struct fuse_conn *fc = get_fuse_conn(inode);
377 	struct fuse_inode *fi = get_fuse_inode(inode);
378 
379 	/*
380 	 * fuse_evict_inode() has already called truncate_inode_pages_final()
381 	 * before we arrive here. So we should not have to worry about any
382 	 * pages/exception entries still associated with inode.
383 	 */
384 	inode_reclaim_dmap_range(fc->dax, inode, 0, -1);
385 	WARN_ON(fi->dax->nr);
386 }
387 
388 static void fuse_fill_iomap_hole(struct iomap *iomap, loff_t length)
389 {
390 	iomap->addr = IOMAP_NULL_ADDR;
391 	iomap->length = length;
392 	iomap->type = IOMAP_HOLE;
393 }
394 
395 static void fuse_fill_iomap(struct inode *inode, loff_t pos, loff_t length,
396 			    struct iomap *iomap, struct fuse_dax_mapping *dmap,
397 			    unsigned int flags)
398 {
399 	loff_t offset, len;
400 	loff_t i_size = i_size_read(inode);
401 
402 	offset = pos - (dmap->itn.start << FUSE_DAX_SHIFT);
403 	len = min(length, dmap->length - offset);
404 
405 	/* If length is beyond end of file, truncate further */
406 	if (pos + len > i_size)
407 		len = i_size - pos;
408 
409 	if (len > 0) {
410 		iomap->addr = dmap->window_offset + offset;
411 		iomap->length = len;
412 		if (flags & IOMAP_FAULT)
413 			iomap->length = ALIGN(len, PAGE_SIZE);
414 		iomap->type = IOMAP_MAPPED;
415 		/*
416 		 * increace refcnt so that reclaim code knows this dmap is in
417 		 * use. This assumes fi->dax->sem mutex is held either
418 		 * shared/exclusive.
419 		 */
420 		refcount_inc(&dmap->refcnt);
421 
422 		/* iomap->private should be NULL */
423 		WARN_ON_ONCE(iomap->private);
424 		iomap->private = dmap;
425 	} else {
426 		/* Mapping beyond end of file is hole */
427 		fuse_fill_iomap_hole(iomap, length);
428 	}
429 }
430 
431 static int fuse_setup_new_dax_mapping(struct inode *inode, loff_t pos,
432 				      loff_t length, unsigned int flags,
433 				      struct iomap *iomap)
434 {
435 	struct fuse_inode *fi = get_fuse_inode(inode);
436 	struct fuse_conn *fc = get_fuse_conn(inode);
437 	struct fuse_conn_dax *fcd = fc->dax;
438 	struct fuse_dax_mapping *dmap, *alloc_dmap = NULL;
439 	int ret;
440 	bool writable = flags & IOMAP_WRITE;
441 	unsigned long start_idx = pos >> FUSE_DAX_SHIFT;
442 	struct interval_tree_node *node;
443 
444 	/*
445 	 * Can't do inline reclaim in fault path. We call
446 	 * dax_layout_busy_page() before we free a range. And
447 	 * fuse_wait_dax_page() drops fi->i_mmap_sem lock and requires it.
448 	 * In fault path we enter with fi->i_mmap_sem held and can't drop
449 	 * it. Also in fault path we hold fi->i_mmap_sem shared and not
450 	 * exclusive, so that creates further issues with fuse_wait_dax_page().
451 	 * Hence return -EAGAIN and fuse_dax_fault() will wait for a memory
452 	 * range to become free and retry.
453 	 */
454 	if (flags & IOMAP_FAULT) {
455 		alloc_dmap = alloc_dax_mapping(fcd);
456 		if (!alloc_dmap)
457 			return -EAGAIN;
458 	} else {
459 		alloc_dmap = alloc_dax_mapping_reclaim(fcd, inode);
460 		if (IS_ERR(alloc_dmap))
461 			return PTR_ERR(alloc_dmap);
462 	}
463 
464 	/* If we are here, we should have memory allocated */
465 	if (WARN_ON(!alloc_dmap))
466 		return -EIO;
467 
468 	/*
469 	 * Take write lock so that only one caller can try to setup mapping
470 	 * and other waits.
471 	 */
472 	down_write(&fi->dax->sem);
473 	/*
474 	 * We dropped lock. Check again if somebody else setup
475 	 * mapping already.
476 	 */
477 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
478 	if (node) {
479 		dmap = node_to_dmap(node);
480 		fuse_fill_iomap(inode, pos, length, iomap, dmap, flags);
481 		dmap_add_to_free_pool(fcd, alloc_dmap);
482 		up_write(&fi->dax->sem);
483 		return 0;
484 	}
485 
486 	/* Setup one mapping */
487 	ret = fuse_setup_one_mapping(inode, pos >> FUSE_DAX_SHIFT, alloc_dmap,
488 				     writable, false);
489 	if (ret < 0) {
490 		dmap_add_to_free_pool(fcd, alloc_dmap);
491 		up_write(&fi->dax->sem);
492 		return ret;
493 	}
494 	fuse_fill_iomap(inode, pos, length, iomap, alloc_dmap, flags);
495 	up_write(&fi->dax->sem);
496 	return 0;
497 }
498 
499 static int fuse_upgrade_dax_mapping(struct inode *inode, loff_t pos,
500 				    loff_t length, unsigned int flags,
501 				    struct iomap *iomap)
502 {
503 	struct fuse_inode *fi = get_fuse_inode(inode);
504 	struct fuse_dax_mapping *dmap;
505 	int ret;
506 	unsigned long idx = pos >> FUSE_DAX_SHIFT;
507 	struct interval_tree_node *node;
508 
509 	/*
510 	 * Take exclusive lock so that only one caller can try to setup
511 	 * mapping and others wait.
512 	 */
513 	down_write(&fi->dax->sem);
514 	node = interval_tree_iter_first(&fi->dax->tree, idx, idx);
515 
516 	/* We are holding either inode lock or i_mmap_sem, and that should
517 	 * ensure that dmap can't be truncated. We are holding a reference
518 	 * on dmap and that should make sure it can't be reclaimed. So dmap
519 	 * should still be there in tree despite the fact we dropped and
520 	 * re-acquired the fi->dax->sem lock.
521 	 */
522 	ret = -EIO;
523 	if (WARN_ON(!node))
524 		goto out_err;
525 
526 	dmap = node_to_dmap(node);
527 
528 	/* We took an extra reference on dmap to make sure its not reclaimd.
529 	 * Now we hold fi->dax->sem lock and that reference is not needed
530 	 * anymore. Drop it.
531 	 */
532 	if (refcount_dec_and_test(&dmap->refcnt)) {
533 		/* refcount should not hit 0. This object only goes
534 		 * away when fuse connection goes away
535 		 */
536 		WARN_ON_ONCE(1);
537 	}
538 
539 	/* Maybe another thread already upgraded mapping while we were not
540 	 * holding lock.
541 	 */
542 	if (dmap->writable) {
543 		ret = 0;
544 		goto out_fill_iomap;
545 	}
546 
547 	ret = fuse_setup_one_mapping(inode, pos >> FUSE_DAX_SHIFT, dmap, true,
548 				     true);
549 	if (ret < 0)
550 		goto out_err;
551 out_fill_iomap:
552 	fuse_fill_iomap(inode, pos, length, iomap, dmap, flags);
553 out_err:
554 	up_write(&fi->dax->sem);
555 	return ret;
556 }
557 
558 /* This is just for DAX and the mapping is ephemeral, do not use it for other
559  * purposes since there is no block device with a permanent mapping.
560  */
561 static int fuse_iomap_begin(struct inode *inode, loff_t pos, loff_t length,
562 			    unsigned int flags, struct iomap *iomap,
563 			    struct iomap *srcmap)
564 {
565 	struct fuse_inode *fi = get_fuse_inode(inode);
566 	struct fuse_conn *fc = get_fuse_conn(inode);
567 	struct fuse_dax_mapping *dmap;
568 	bool writable = flags & IOMAP_WRITE;
569 	unsigned long start_idx = pos >> FUSE_DAX_SHIFT;
570 	struct interval_tree_node *node;
571 
572 	/* We don't support FIEMAP */
573 	if (WARN_ON(flags & IOMAP_REPORT))
574 		return -EIO;
575 
576 	iomap->offset = pos;
577 	iomap->flags = 0;
578 	iomap->bdev = NULL;
579 	iomap->dax_dev = fc->dax->dev;
580 
581 	/*
582 	 * Both read/write and mmap path can race here. So we need something
583 	 * to make sure if we are setting up mapping, then other path waits
584 	 *
585 	 * For now, use a semaphore for this. It probably needs to be
586 	 * optimized later.
587 	 */
588 	down_read(&fi->dax->sem);
589 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
590 	if (node) {
591 		dmap = node_to_dmap(node);
592 		if (writable && !dmap->writable) {
593 			/* Upgrade read-only mapping to read-write. This will
594 			 * require exclusive fi->dax->sem lock as we don't want
595 			 * two threads to be trying to this simultaneously
596 			 * for same dmap. So drop shared lock and acquire
597 			 * exclusive lock.
598 			 *
599 			 * Before dropping fi->dax->sem lock, take reference
600 			 * on dmap so that its not freed by range reclaim.
601 			 */
602 			refcount_inc(&dmap->refcnt);
603 			up_read(&fi->dax->sem);
604 			pr_debug("%s: Upgrading mapping at offset 0x%llx length 0x%llx\n",
605 				 __func__, pos, length);
606 			return fuse_upgrade_dax_mapping(inode, pos, length,
607 							flags, iomap);
608 		} else {
609 			fuse_fill_iomap(inode, pos, length, iomap, dmap, flags);
610 			up_read(&fi->dax->sem);
611 			return 0;
612 		}
613 	} else {
614 		up_read(&fi->dax->sem);
615 		pr_debug("%s: no mapping at offset 0x%llx length 0x%llx\n",
616 				__func__, pos, length);
617 		if (pos >= i_size_read(inode))
618 			goto iomap_hole;
619 
620 		return fuse_setup_new_dax_mapping(inode, pos, length, flags,
621 						  iomap);
622 	}
623 
624 	/*
625 	 * If read beyond end of file happens, fs code seems to return
626 	 * it as hole
627 	 */
628 iomap_hole:
629 	fuse_fill_iomap_hole(iomap, length);
630 	pr_debug("%s returning hole mapping. pos=0x%llx length_asked=0x%llx length_returned=0x%llx\n",
631 		 __func__, pos, length, iomap->length);
632 	return 0;
633 }
634 
635 static int fuse_iomap_end(struct inode *inode, loff_t pos, loff_t length,
636 			  ssize_t written, unsigned int flags,
637 			  struct iomap *iomap)
638 {
639 	struct fuse_dax_mapping *dmap = iomap->private;
640 
641 	if (dmap) {
642 		if (refcount_dec_and_test(&dmap->refcnt)) {
643 			/* refcount should not hit 0. This object only goes
644 			 * away when fuse connection goes away
645 			 */
646 			WARN_ON_ONCE(1);
647 		}
648 	}
649 
650 	/* DAX writes beyond end-of-file aren't handled using iomap, so the
651 	 * file size is unchanged and there is nothing to do here.
652 	 */
653 	return 0;
654 }
655 
656 static const struct iomap_ops fuse_iomap_ops = {
657 	.iomap_begin = fuse_iomap_begin,
658 	.iomap_end = fuse_iomap_end,
659 };
660 
661 static void fuse_wait_dax_page(struct inode *inode)
662 {
663 	struct fuse_inode *fi = get_fuse_inode(inode);
664 
665 	up_write(&fi->i_mmap_sem);
666 	schedule();
667 	down_write(&fi->i_mmap_sem);
668 }
669 
670 /* Should be called with fi->i_mmap_sem lock held exclusively */
671 static int __fuse_dax_break_layouts(struct inode *inode, bool *retry,
672 				    loff_t start, loff_t end)
673 {
674 	struct page *page;
675 
676 	page = dax_layout_busy_page_range(inode->i_mapping, start, end);
677 	if (!page)
678 		return 0;
679 
680 	*retry = true;
681 	return ___wait_var_event(&page->_refcount,
682 			atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
683 			0, 0, fuse_wait_dax_page(inode));
684 }
685 
686 /* dmap_end == 0 leads to unmapping of whole file */
687 int fuse_dax_break_layouts(struct inode *inode, u64 dmap_start,
688 				  u64 dmap_end)
689 {
690 	bool	retry;
691 	int	ret;
692 
693 	do {
694 		retry = false;
695 		ret = __fuse_dax_break_layouts(inode, &retry, dmap_start,
696 					       dmap_end);
697 	} while (ret == 0 && retry);
698 
699 	return ret;
700 }
701 
702 ssize_t fuse_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
703 {
704 	struct inode *inode = file_inode(iocb->ki_filp);
705 	ssize_t ret;
706 
707 	if (iocb->ki_flags & IOCB_NOWAIT) {
708 		if (!inode_trylock_shared(inode))
709 			return -EAGAIN;
710 	} else {
711 		inode_lock_shared(inode);
712 	}
713 
714 	ret = dax_iomap_rw(iocb, to, &fuse_iomap_ops);
715 	inode_unlock_shared(inode);
716 
717 	/* TODO file_accessed(iocb->f_filp) */
718 	return ret;
719 }
720 
721 static bool file_extending_write(struct kiocb *iocb, struct iov_iter *from)
722 {
723 	struct inode *inode = file_inode(iocb->ki_filp);
724 
725 	return (iov_iter_rw(from) == WRITE &&
726 		((iocb->ki_pos) >= i_size_read(inode) ||
727 		  (iocb->ki_pos + iov_iter_count(from) > i_size_read(inode))));
728 }
729 
730 static ssize_t fuse_dax_direct_write(struct kiocb *iocb, struct iov_iter *from)
731 {
732 	struct inode *inode = file_inode(iocb->ki_filp);
733 	struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
734 	ssize_t ret;
735 
736 	ret = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
737 	if (ret < 0)
738 		return ret;
739 
740 	fuse_invalidate_attr(inode);
741 	fuse_write_update_size(inode, iocb->ki_pos);
742 	return ret;
743 }
744 
745 ssize_t fuse_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
746 {
747 	struct inode *inode = file_inode(iocb->ki_filp);
748 	ssize_t ret;
749 
750 	if (iocb->ki_flags & IOCB_NOWAIT) {
751 		if (!inode_trylock(inode))
752 			return -EAGAIN;
753 	} else {
754 		inode_lock(inode);
755 	}
756 
757 	ret = generic_write_checks(iocb, from);
758 	if (ret <= 0)
759 		goto out;
760 
761 	ret = file_remove_privs(iocb->ki_filp);
762 	if (ret)
763 		goto out;
764 	/* TODO file_update_time() but we don't want metadata I/O */
765 
766 	/* Do not use dax for file extending writes as write and on
767 	 * disk i_size increase are not atomic otherwise.
768 	 */
769 	if (file_extending_write(iocb, from))
770 		ret = fuse_dax_direct_write(iocb, from);
771 	else
772 		ret = dax_iomap_rw(iocb, from, &fuse_iomap_ops);
773 
774 out:
775 	inode_unlock(inode);
776 
777 	if (ret > 0)
778 		ret = generic_write_sync(iocb, ret);
779 	return ret;
780 }
781 
782 static int fuse_dax_writepages(struct address_space *mapping,
783 			       struct writeback_control *wbc)
784 {
785 
786 	struct inode *inode = mapping->host;
787 	struct fuse_conn *fc = get_fuse_conn(inode);
788 
789 	return dax_writeback_mapping_range(mapping, fc->dax->dev, wbc);
790 }
791 
792 static vm_fault_t __fuse_dax_fault(struct vm_fault *vmf,
793 				   enum page_entry_size pe_size, bool write)
794 {
795 	vm_fault_t ret;
796 	struct inode *inode = file_inode(vmf->vma->vm_file);
797 	struct super_block *sb = inode->i_sb;
798 	pfn_t pfn;
799 	int error = 0;
800 	struct fuse_conn *fc = get_fuse_conn(inode);
801 	struct fuse_conn_dax *fcd = fc->dax;
802 	bool retry = false;
803 
804 	if (write)
805 		sb_start_pagefault(sb);
806 retry:
807 	if (retry && !(fcd->nr_free_ranges > 0))
808 		wait_event(fcd->range_waitq, (fcd->nr_free_ranges > 0));
809 
810 	/*
811 	 * We need to serialize against not only truncate but also against
812 	 * fuse dax memory range reclaim. While a range is being reclaimed,
813 	 * we do not want any read/write/mmap to make progress and try
814 	 * to populate page cache or access memory we are trying to free.
815 	 */
816 	down_read(&get_fuse_inode(inode)->i_mmap_sem);
817 	ret = dax_iomap_fault(vmf, pe_size, &pfn, &error, &fuse_iomap_ops);
818 	if ((ret & VM_FAULT_ERROR) && error == -EAGAIN) {
819 		error = 0;
820 		retry = true;
821 		up_read(&get_fuse_inode(inode)->i_mmap_sem);
822 		goto retry;
823 	}
824 
825 	if (ret & VM_FAULT_NEEDDSYNC)
826 		ret = dax_finish_sync_fault(vmf, pe_size, pfn);
827 	up_read(&get_fuse_inode(inode)->i_mmap_sem);
828 
829 	if (write)
830 		sb_end_pagefault(sb);
831 
832 	return ret;
833 }
834 
835 static vm_fault_t fuse_dax_fault(struct vm_fault *vmf)
836 {
837 	return __fuse_dax_fault(vmf, PE_SIZE_PTE,
838 				vmf->flags & FAULT_FLAG_WRITE);
839 }
840 
841 static vm_fault_t fuse_dax_huge_fault(struct vm_fault *vmf,
842 			       enum page_entry_size pe_size)
843 {
844 	return __fuse_dax_fault(vmf, pe_size, vmf->flags & FAULT_FLAG_WRITE);
845 }
846 
847 static vm_fault_t fuse_dax_page_mkwrite(struct vm_fault *vmf)
848 {
849 	return __fuse_dax_fault(vmf, PE_SIZE_PTE, true);
850 }
851 
852 static vm_fault_t fuse_dax_pfn_mkwrite(struct vm_fault *vmf)
853 {
854 	return __fuse_dax_fault(vmf, PE_SIZE_PTE, true);
855 }
856 
857 static const struct vm_operations_struct fuse_dax_vm_ops = {
858 	.fault		= fuse_dax_fault,
859 	.huge_fault	= fuse_dax_huge_fault,
860 	.page_mkwrite	= fuse_dax_page_mkwrite,
861 	.pfn_mkwrite	= fuse_dax_pfn_mkwrite,
862 };
863 
864 int fuse_dax_mmap(struct file *file, struct vm_area_struct *vma)
865 {
866 	file_accessed(file);
867 	vma->vm_ops = &fuse_dax_vm_ops;
868 	vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
869 	return 0;
870 }
871 
872 static int dmap_writeback_invalidate(struct inode *inode,
873 				     struct fuse_dax_mapping *dmap)
874 {
875 	int ret;
876 	loff_t start_pos = dmap->itn.start << FUSE_DAX_SHIFT;
877 	loff_t end_pos = (start_pos + FUSE_DAX_SZ - 1);
878 
879 	ret = filemap_fdatawrite_range(inode->i_mapping, start_pos, end_pos);
880 	if (ret) {
881 		pr_debug("fuse: filemap_fdatawrite_range() failed. err=%d start_pos=0x%llx, end_pos=0x%llx\n",
882 			 ret, start_pos, end_pos);
883 		return ret;
884 	}
885 
886 	ret = invalidate_inode_pages2_range(inode->i_mapping,
887 					    start_pos >> PAGE_SHIFT,
888 					    end_pos >> PAGE_SHIFT);
889 	if (ret)
890 		pr_debug("fuse: invalidate_inode_pages2_range() failed err=%d\n",
891 			 ret);
892 
893 	return ret;
894 }
895 
896 static int reclaim_one_dmap_locked(struct inode *inode,
897 				   struct fuse_dax_mapping *dmap)
898 {
899 	int ret;
900 	struct fuse_inode *fi = get_fuse_inode(inode);
901 
902 	/*
903 	 * igrab() was done to make sure inode won't go under us, and this
904 	 * further avoids the race with evict().
905 	 */
906 	ret = dmap_writeback_invalidate(inode, dmap);
907 	if (ret)
908 		return ret;
909 
910 	/* Remove dax mapping from inode interval tree now */
911 	interval_tree_remove(&dmap->itn, &fi->dax->tree);
912 	fi->dax->nr--;
913 
914 	/* It is possible that umount/shutdown has killed the fuse connection
915 	 * and worker thread is trying to reclaim memory in parallel.  Don't
916 	 * warn in that case.
917 	 */
918 	ret = dmap_removemapping_one(inode, dmap);
919 	if (ret && ret != -ENOTCONN) {
920 		pr_warn("Failed to remove mapping. offset=0x%llx len=0x%llx ret=%d\n",
921 			dmap->window_offset, dmap->length, ret);
922 	}
923 	return 0;
924 }
925 
926 /* Find first mapped dmap for an inode and return file offset. Caller needs
927  * to hold fi->dax->sem lock either shared or exclusive.
928  */
929 static struct fuse_dax_mapping *inode_lookup_first_dmap(struct inode *inode)
930 {
931 	struct fuse_inode *fi = get_fuse_inode(inode);
932 	struct fuse_dax_mapping *dmap;
933 	struct interval_tree_node *node;
934 
935 	for (node = interval_tree_iter_first(&fi->dax->tree, 0, -1); node;
936 	     node = interval_tree_iter_next(node, 0, -1)) {
937 		dmap = node_to_dmap(node);
938 		/* still in use. */
939 		if (refcount_read(&dmap->refcnt) > 1)
940 			continue;
941 
942 		return dmap;
943 	}
944 
945 	return NULL;
946 }
947 
948 /*
949  * Find first mapping in the tree and free it and return it. Do not add
950  * it back to free pool.
951  */
952 static struct fuse_dax_mapping *
953 inode_inline_reclaim_one_dmap(struct fuse_conn_dax *fcd, struct inode *inode,
954 			      bool *retry)
955 {
956 	struct fuse_inode *fi = get_fuse_inode(inode);
957 	struct fuse_dax_mapping *dmap;
958 	u64 dmap_start, dmap_end;
959 	unsigned long start_idx;
960 	int ret;
961 	struct interval_tree_node *node;
962 
963 	down_write(&fi->i_mmap_sem);
964 
965 	/* Lookup a dmap and corresponding file offset to reclaim. */
966 	down_read(&fi->dax->sem);
967 	dmap = inode_lookup_first_dmap(inode);
968 	if (dmap) {
969 		start_idx = dmap->itn.start;
970 		dmap_start = start_idx << FUSE_DAX_SHIFT;
971 		dmap_end = dmap_start + FUSE_DAX_SZ - 1;
972 	}
973 	up_read(&fi->dax->sem);
974 
975 	if (!dmap)
976 		goto out_mmap_sem;
977 	/*
978 	 * Make sure there are no references to inode pages using
979 	 * get_user_pages()
980 	 */
981 	ret = fuse_dax_break_layouts(inode, dmap_start, dmap_end);
982 	if (ret) {
983 		pr_debug("fuse: fuse_dax_break_layouts() failed. err=%d\n",
984 			 ret);
985 		dmap = ERR_PTR(ret);
986 		goto out_mmap_sem;
987 	}
988 
989 	down_write(&fi->dax->sem);
990 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
991 	/* Range already got reclaimed by somebody else */
992 	if (!node) {
993 		if (retry)
994 			*retry = true;
995 		goto out_write_dmap_sem;
996 	}
997 
998 	dmap = node_to_dmap(node);
999 	/* still in use. */
1000 	if (refcount_read(&dmap->refcnt) > 1) {
1001 		dmap = NULL;
1002 		if (retry)
1003 			*retry = true;
1004 		goto out_write_dmap_sem;
1005 	}
1006 
1007 	ret = reclaim_one_dmap_locked(inode, dmap);
1008 	if (ret < 0) {
1009 		dmap = ERR_PTR(ret);
1010 		goto out_write_dmap_sem;
1011 	}
1012 
1013 	/* Clean up dmap. Do not add back to free list */
1014 	dmap_remove_busy_list(fcd, dmap);
1015 	dmap->inode = NULL;
1016 	dmap->itn.start = dmap->itn.last = 0;
1017 
1018 	pr_debug("fuse: %s: inline reclaimed memory range. inode=%p, window_offset=0x%llx, length=0x%llx\n",
1019 		 __func__, inode, dmap->window_offset, dmap->length);
1020 
1021 out_write_dmap_sem:
1022 	up_write(&fi->dax->sem);
1023 out_mmap_sem:
1024 	up_write(&fi->i_mmap_sem);
1025 	return dmap;
1026 }
1027 
1028 static struct fuse_dax_mapping *
1029 alloc_dax_mapping_reclaim(struct fuse_conn_dax *fcd, struct inode *inode)
1030 {
1031 	struct fuse_dax_mapping *dmap;
1032 	struct fuse_inode *fi = get_fuse_inode(inode);
1033 
1034 	while (1) {
1035 		bool retry = false;
1036 
1037 		dmap = alloc_dax_mapping(fcd);
1038 		if (dmap)
1039 			return dmap;
1040 
1041 		dmap = inode_inline_reclaim_one_dmap(fcd, inode, &retry);
1042 		/*
1043 		 * Either we got a mapping or it is an error, return in both
1044 		 * the cases.
1045 		 */
1046 		if (dmap)
1047 			return dmap;
1048 
1049 		/* If we could not reclaim a mapping because it
1050 		 * had a reference or some other temporary failure,
1051 		 * Try again. We want to give up inline reclaim only
1052 		 * if there is no range assigned to this node. Otherwise
1053 		 * if a deadlock is possible if we sleep with fi->i_mmap_sem
1054 		 * held and worker to free memory can't make progress due
1055 		 * to unavailability of fi->i_mmap_sem lock. So sleep
1056 		 * only if fi->dax->nr=0
1057 		 */
1058 		if (retry)
1059 			continue;
1060 		/*
1061 		 * There are no mappings which can be reclaimed. Wait for one.
1062 		 * We are not holding fi->dax->sem. So it is possible
1063 		 * that range gets added now. But as we are not holding
1064 		 * fi->i_mmap_sem, worker should still be able to free up
1065 		 * a range and wake us up.
1066 		 */
1067 		if (!fi->dax->nr && !(fcd->nr_free_ranges > 0)) {
1068 			if (wait_event_killable_exclusive(fcd->range_waitq,
1069 					(fcd->nr_free_ranges > 0))) {
1070 				return ERR_PTR(-EINTR);
1071 			}
1072 		}
1073 	}
1074 }
1075 
1076 static int lookup_and_reclaim_dmap_locked(struct fuse_conn_dax *fcd,
1077 					  struct inode *inode,
1078 					  unsigned long start_idx)
1079 {
1080 	int ret;
1081 	struct fuse_inode *fi = get_fuse_inode(inode);
1082 	struct fuse_dax_mapping *dmap;
1083 	struct interval_tree_node *node;
1084 
1085 	/* Find fuse dax mapping at file offset inode. */
1086 	node = interval_tree_iter_first(&fi->dax->tree, start_idx, start_idx);
1087 
1088 	/* Range already got cleaned up by somebody else */
1089 	if (!node)
1090 		return 0;
1091 	dmap = node_to_dmap(node);
1092 
1093 	/* still in use. */
1094 	if (refcount_read(&dmap->refcnt) > 1)
1095 		return 0;
1096 
1097 	ret = reclaim_one_dmap_locked(inode, dmap);
1098 	if (ret < 0)
1099 		return ret;
1100 
1101 	/* Cleanup dmap entry and add back to free list */
1102 	spin_lock(&fcd->lock);
1103 	dmap_reinit_add_to_free_pool(fcd, dmap);
1104 	spin_unlock(&fcd->lock);
1105 	return ret;
1106 }
1107 
1108 /*
1109  * Free a range of memory.
1110  * Locking:
1111  * 1. Take fi->i_mmap_sem to block dax faults.
1112  * 2. Take fi->dax->sem to protect interval tree and also to make sure
1113  *    read/write can not reuse a dmap which we might be freeing.
1114  */
1115 static int lookup_and_reclaim_dmap(struct fuse_conn_dax *fcd,
1116 				   struct inode *inode,
1117 				   unsigned long start_idx,
1118 				   unsigned long end_idx)
1119 {
1120 	int ret;
1121 	struct fuse_inode *fi = get_fuse_inode(inode);
1122 	loff_t dmap_start = start_idx << FUSE_DAX_SHIFT;
1123 	loff_t dmap_end = (dmap_start + FUSE_DAX_SZ) - 1;
1124 
1125 	down_write(&fi->i_mmap_sem);
1126 	ret = fuse_dax_break_layouts(inode, dmap_start, dmap_end);
1127 	if (ret) {
1128 		pr_debug("virtio_fs: fuse_dax_break_layouts() failed. err=%d\n",
1129 			 ret);
1130 		goto out_mmap_sem;
1131 	}
1132 
1133 	down_write(&fi->dax->sem);
1134 	ret = lookup_and_reclaim_dmap_locked(fcd, inode, start_idx);
1135 	up_write(&fi->dax->sem);
1136 out_mmap_sem:
1137 	up_write(&fi->i_mmap_sem);
1138 	return ret;
1139 }
1140 
1141 static int try_to_free_dmap_chunks(struct fuse_conn_dax *fcd,
1142 				   unsigned long nr_to_free)
1143 {
1144 	struct fuse_dax_mapping *dmap, *pos, *temp;
1145 	int ret, nr_freed = 0;
1146 	unsigned long start_idx = 0, end_idx = 0;
1147 	struct inode *inode = NULL;
1148 
1149 	/* Pick first busy range and free it for now*/
1150 	while (1) {
1151 		if (nr_freed >= nr_to_free)
1152 			break;
1153 
1154 		dmap = NULL;
1155 		spin_lock(&fcd->lock);
1156 
1157 		if (!fcd->nr_busy_ranges) {
1158 			spin_unlock(&fcd->lock);
1159 			return 0;
1160 		}
1161 
1162 		list_for_each_entry_safe(pos, temp, &fcd->busy_ranges,
1163 						busy_list) {
1164 			/* skip this range if it's in use. */
1165 			if (refcount_read(&pos->refcnt) > 1)
1166 				continue;
1167 
1168 			inode = igrab(pos->inode);
1169 			/*
1170 			 * This inode is going away. That will free
1171 			 * up all the ranges anyway, continue to
1172 			 * next range.
1173 			 */
1174 			if (!inode)
1175 				continue;
1176 			/*
1177 			 * Take this element off list and add it tail. If
1178 			 * this element can't be freed, it will help with
1179 			 * selecting new element in next iteration of loop.
1180 			 */
1181 			dmap = pos;
1182 			list_move_tail(&dmap->busy_list, &fcd->busy_ranges);
1183 			start_idx = end_idx = dmap->itn.start;
1184 			break;
1185 		}
1186 		spin_unlock(&fcd->lock);
1187 		if (!dmap)
1188 			return 0;
1189 
1190 		ret = lookup_and_reclaim_dmap(fcd, inode, start_idx, end_idx);
1191 		iput(inode);
1192 		if (ret)
1193 			return ret;
1194 		nr_freed++;
1195 	}
1196 	return 0;
1197 }
1198 
1199 static void fuse_dax_free_mem_worker(struct work_struct *work)
1200 {
1201 	int ret;
1202 	struct fuse_conn_dax *fcd = container_of(work, struct fuse_conn_dax,
1203 						 free_work.work);
1204 	ret = try_to_free_dmap_chunks(fcd, FUSE_DAX_RECLAIM_CHUNK);
1205 	if (ret) {
1206 		pr_debug("fuse: try_to_free_dmap_chunks() failed with err=%d\n",
1207 			 ret);
1208 	}
1209 
1210 	/* If number of free ranges are still below threshold, requeue */
1211 	kick_dmap_free_worker(fcd, 1);
1212 }
1213 
1214 static void fuse_free_dax_mem_ranges(struct list_head *mem_list)
1215 {
1216 	struct fuse_dax_mapping *range, *temp;
1217 
1218 	/* Free All allocated elements */
1219 	list_for_each_entry_safe(range, temp, mem_list, list) {
1220 		list_del(&range->list);
1221 		if (!list_empty(&range->busy_list))
1222 			list_del(&range->busy_list);
1223 		kfree(range);
1224 	}
1225 }
1226 
1227 void fuse_dax_conn_free(struct fuse_conn *fc)
1228 {
1229 	if (fc->dax) {
1230 		fuse_free_dax_mem_ranges(&fc->dax->free_ranges);
1231 		kfree(fc->dax);
1232 	}
1233 }
1234 
1235 static int fuse_dax_mem_range_init(struct fuse_conn_dax *fcd)
1236 {
1237 	long nr_pages, nr_ranges;
1238 	struct fuse_dax_mapping *range;
1239 	int ret, id;
1240 	size_t dax_size = -1;
1241 	unsigned long i;
1242 
1243 	init_waitqueue_head(&fcd->range_waitq);
1244 	INIT_LIST_HEAD(&fcd->free_ranges);
1245 	INIT_LIST_HEAD(&fcd->busy_ranges);
1246 	INIT_DELAYED_WORK(&fcd->free_work, fuse_dax_free_mem_worker);
1247 
1248 	id = dax_read_lock();
1249 	nr_pages = dax_direct_access(fcd->dev, 0, PHYS_PFN(dax_size), NULL,
1250 				     NULL);
1251 	dax_read_unlock(id);
1252 	if (nr_pages < 0) {
1253 		pr_debug("dax_direct_access() returned %ld\n", nr_pages);
1254 		return nr_pages;
1255 	}
1256 
1257 	nr_ranges = nr_pages/FUSE_DAX_PAGES;
1258 	pr_debug("%s: dax mapped %ld pages. nr_ranges=%ld\n",
1259 		__func__, nr_pages, nr_ranges);
1260 
1261 	for (i = 0; i < nr_ranges; i++) {
1262 		range = kzalloc(sizeof(struct fuse_dax_mapping), GFP_KERNEL);
1263 		ret = -ENOMEM;
1264 		if (!range)
1265 			goto out_err;
1266 
1267 		/* TODO: This offset only works if virtio-fs driver is not
1268 		 * having some memory hidden at the beginning. This needs
1269 		 * better handling
1270 		 */
1271 		range->window_offset = i * FUSE_DAX_SZ;
1272 		range->length = FUSE_DAX_SZ;
1273 		INIT_LIST_HEAD(&range->busy_list);
1274 		refcount_set(&range->refcnt, 1);
1275 		list_add_tail(&range->list, &fcd->free_ranges);
1276 	}
1277 
1278 	fcd->nr_free_ranges = nr_ranges;
1279 	fcd->nr_ranges = nr_ranges;
1280 	return 0;
1281 out_err:
1282 	/* Free All allocated elements */
1283 	fuse_free_dax_mem_ranges(&fcd->free_ranges);
1284 	return ret;
1285 }
1286 
1287 int fuse_dax_conn_alloc(struct fuse_conn *fc, struct dax_device *dax_dev)
1288 {
1289 	struct fuse_conn_dax *fcd;
1290 	int err;
1291 
1292 	if (!dax_dev)
1293 		return 0;
1294 
1295 	fcd = kzalloc(sizeof(*fcd), GFP_KERNEL);
1296 	if (!fcd)
1297 		return -ENOMEM;
1298 
1299 	spin_lock_init(&fcd->lock);
1300 	fcd->dev = dax_dev;
1301 	err = fuse_dax_mem_range_init(fcd);
1302 	if (err) {
1303 		kfree(fcd);
1304 		return err;
1305 	}
1306 
1307 	fc->dax = fcd;
1308 	return 0;
1309 }
1310 
1311 bool fuse_dax_inode_alloc(struct super_block *sb, struct fuse_inode *fi)
1312 {
1313 	struct fuse_conn *fc = get_fuse_conn_super(sb);
1314 
1315 	fi->dax = NULL;
1316 	if (fc->dax) {
1317 		fi->dax = kzalloc(sizeof(*fi->dax), GFP_KERNEL_ACCOUNT);
1318 		if (!fi->dax)
1319 			return false;
1320 
1321 		init_rwsem(&fi->dax->sem);
1322 		fi->dax->tree = RB_ROOT_CACHED;
1323 	}
1324 
1325 	return true;
1326 }
1327 
1328 static const struct address_space_operations fuse_dax_file_aops  = {
1329 	.writepages	= fuse_dax_writepages,
1330 	.direct_IO	= noop_direct_IO,
1331 	.set_page_dirty	= __set_page_dirty_no_writeback,
1332 	.invalidatepage	= noop_invalidatepage,
1333 };
1334 
1335 void fuse_dax_inode_init(struct inode *inode)
1336 {
1337 	struct fuse_conn *fc = get_fuse_conn(inode);
1338 
1339 	if (!fc->dax)
1340 		return;
1341 
1342 	inode->i_flags |= S_DAX;
1343 	inode->i_data.a_ops = &fuse_dax_file_aops;
1344 }
1345 
1346 bool fuse_dax_check_alignment(struct fuse_conn *fc, unsigned int map_alignment)
1347 {
1348 	if (fc->dax && (map_alignment > FUSE_DAX_SHIFT)) {
1349 		pr_warn("FUSE: map_alignment %u incompatible with dax mem range size %u\n",
1350 			map_alignment, FUSE_DAX_SZ);
1351 		return false;
1352 	}
1353 	return true;
1354 }
1355 
1356 void fuse_dax_cancel_work(struct fuse_conn *fc)
1357 {
1358 	struct fuse_conn_dax *fcd = fc->dax;
1359 
1360 	if (fcd)
1361 		cancel_delayed_work_sync(&fcd->free_work);
1362 
1363 }
1364 EXPORT_SYMBOL_GPL(fuse_dax_cancel_work);
1365