]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/dax.c
Merge branch 'fscrypt' into dev
[mirror_ubuntu-zesty-kernel.git] / fs / dax.c
1 /*
2 * fs/dax.c - Direct Access filesystem code
3 * Copyright (c) 2013-2014 Intel Corporation
4 * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
5 * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 */
16
17 #include <linux/atomic.h>
18 #include <linux/blkdev.h>
19 #include <linux/buffer_head.h>
20 #include <linux/dax.h>
21 #include <linux/fs.h>
22 #include <linux/genhd.h>
23 #include <linux/highmem.h>
24 #include <linux/memcontrol.h>
25 #include <linux/mm.h>
26 #include <linux/mutex.h>
27 #include <linux/pagevec.h>
28 #include <linux/pmem.h>
29 #include <linux/sched.h>
30 #include <linux/uio.h>
31 #include <linux/vmstat.h>
32 #include <linux/pfn_t.h>
33 #include <linux/sizes.h>
34 #include <linux/iomap.h>
35 #include "internal.h"
36
37 /* We choose 4096 entries - same as per-zone page wait tables */
38 #define DAX_WAIT_TABLE_BITS 12
39 #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
40
41 static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
42
43 static int __init init_dax_wait_table(void)
44 {
45 int i;
46
47 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
48 init_waitqueue_head(wait_table + i);
49 return 0;
50 }
51 fs_initcall(init_dax_wait_table);
52
53 static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
54 {
55 struct request_queue *q = bdev->bd_queue;
56 long rc = -EIO;
57
58 dax->addr = ERR_PTR(-EIO);
59 if (blk_queue_enter(q, true) != 0)
60 return rc;
61
62 rc = bdev_direct_access(bdev, dax);
63 if (rc < 0) {
64 dax->addr = ERR_PTR(rc);
65 blk_queue_exit(q);
66 return rc;
67 }
68 return rc;
69 }
70
71 static void dax_unmap_atomic(struct block_device *bdev,
72 const struct blk_dax_ctl *dax)
73 {
74 if (IS_ERR(dax->addr))
75 return;
76 blk_queue_exit(bdev->bd_queue);
77 }
78
79 static int dax_is_pmd_entry(void *entry)
80 {
81 return (unsigned long)entry & RADIX_DAX_PMD;
82 }
83
84 static int dax_is_pte_entry(void *entry)
85 {
86 return !((unsigned long)entry & RADIX_DAX_PMD);
87 }
88
89 static int dax_is_zero_entry(void *entry)
90 {
91 return (unsigned long)entry & RADIX_DAX_HZP;
92 }
93
94 static int dax_is_empty_entry(void *entry)
95 {
96 return (unsigned long)entry & RADIX_DAX_EMPTY;
97 }
98
99 struct page *read_dax_sector(struct block_device *bdev, sector_t n)
100 {
101 struct page *page = alloc_pages(GFP_KERNEL, 0);
102 struct blk_dax_ctl dax = {
103 .size = PAGE_SIZE,
104 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
105 };
106 long rc;
107
108 if (!page)
109 return ERR_PTR(-ENOMEM);
110
111 rc = dax_map_atomic(bdev, &dax);
112 if (rc < 0)
113 return ERR_PTR(rc);
114 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
115 dax_unmap_atomic(bdev, &dax);
116 return page;
117 }
118
119 /*
120 * DAX radix tree locking
121 */
122 struct exceptional_entry_key {
123 struct address_space *mapping;
124 pgoff_t entry_start;
125 };
126
127 struct wait_exceptional_entry_queue {
128 wait_queue_t wait;
129 struct exceptional_entry_key key;
130 };
131
132 static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
133 pgoff_t index, void *entry, struct exceptional_entry_key *key)
134 {
135 unsigned long hash;
136
137 /*
138 * If 'entry' is a PMD, align the 'index' that we use for the wait
139 * queue to the start of that PMD. This ensures that all offsets in
140 * the range covered by the PMD map to the same bit lock.
141 */
142 if (dax_is_pmd_entry(entry))
143 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
144
145 key->mapping = mapping;
146 key->entry_start = index;
147
148 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
149 return wait_table + hash;
150 }
151
152 static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
153 int sync, void *keyp)
154 {
155 struct exceptional_entry_key *key = keyp;
156 struct wait_exceptional_entry_queue *ewait =
157 container_of(wait, struct wait_exceptional_entry_queue, wait);
158
159 if (key->mapping != ewait->key.mapping ||
160 key->entry_start != ewait->key.entry_start)
161 return 0;
162 return autoremove_wake_function(wait, mode, sync, NULL);
163 }
164
165 /*
166 * Check whether the given slot is locked. The function must be called with
167 * mapping->tree_lock held
168 */
169 static inline int slot_locked(struct address_space *mapping, void **slot)
170 {
171 unsigned long entry = (unsigned long)
172 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
173 return entry & RADIX_DAX_ENTRY_LOCK;
174 }
175
176 /*
177 * Mark the given slot is locked. The function must be called with
178 * mapping->tree_lock held
179 */
180 static inline void *lock_slot(struct address_space *mapping, void **slot)
181 {
182 unsigned long entry = (unsigned long)
183 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
184
185 entry |= RADIX_DAX_ENTRY_LOCK;
186 radix_tree_replace_slot(slot, (void *)entry);
187 return (void *)entry;
188 }
189
190 /*
191 * Mark the given slot is unlocked. The function must be called with
192 * mapping->tree_lock held
193 */
194 static inline void *unlock_slot(struct address_space *mapping, void **slot)
195 {
196 unsigned long entry = (unsigned long)
197 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
198
199 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
200 radix_tree_replace_slot(slot, (void *)entry);
201 return (void *)entry;
202 }
203
204 /*
205 * Lookup entry in radix tree, wait for it to become unlocked if it is
206 * exceptional entry and return it. The caller must call
207 * put_unlocked_mapping_entry() when he decided not to lock the entry or
208 * put_locked_mapping_entry() when he locked the entry and now wants to
209 * unlock it.
210 *
211 * The function must be called with mapping->tree_lock held.
212 */
213 static void *get_unlocked_mapping_entry(struct address_space *mapping,
214 pgoff_t index, void ***slotp)
215 {
216 void *entry, **slot;
217 struct wait_exceptional_entry_queue ewait;
218 wait_queue_head_t *wq;
219
220 init_wait(&ewait.wait);
221 ewait.wait.func = wake_exceptional_entry_func;
222
223 for (;;) {
224 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
225 &slot);
226 if (!entry || !radix_tree_exceptional_entry(entry) ||
227 !slot_locked(mapping, slot)) {
228 if (slotp)
229 *slotp = slot;
230 return entry;
231 }
232
233 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
234 prepare_to_wait_exclusive(wq, &ewait.wait,
235 TASK_UNINTERRUPTIBLE);
236 spin_unlock_irq(&mapping->tree_lock);
237 schedule();
238 finish_wait(wq, &ewait.wait);
239 spin_lock_irq(&mapping->tree_lock);
240 }
241 }
242
243 static void put_locked_mapping_entry(struct address_space *mapping,
244 pgoff_t index, void *entry)
245 {
246 if (!radix_tree_exceptional_entry(entry)) {
247 unlock_page(entry);
248 put_page(entry);
249 } else {
250 dax_unlock_mapping_entry(mapping, index);
251 }
252 }
253
254 /*
255 * Called when we are done with radix tree entry we looked up via
256 * get_unlocked_mapping_entry() and which we didn't lock in the end.
257 */
258 static void put_unlocked_mapping_entry(struct address_space *mapping,
259 pgoff_t index, void *entry)
260 {
261 if (!radix_tree_exceptional_entry(entry))
262 return;
263
264 /* We have to wake up next waiter for the radix tree entry lock */
265 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
266 }
267
268 /*
269 * Find radix tree entry at given index. If it points to a page, return with
270 * the page locked. If it points to the exceptional entry, return with the
271 * radix tree entry locked. If the radix tree doesn't contain given index,
272 * create empty exceptional entry for the index and return with it locked.
273 *
274 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
275 * either return that locked entry or will return an error. This error will
276 * happen if there are any 4k entries (either zero pages or DAX entries)
277 * within the 2MiB range that we are requesting.
278 *
279 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
280 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
281 * insertion will fail if it finds any 4k entries already in the tree, and a
282 * 4k insertion will cause an existing 2MiB entry to be unmapped and
283 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
284 * well as 2MiB empty entries.
285 *
286 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
287 * real storage backing them. We will leave these real 2MiB DAX entries in
288 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
289 *
290 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
291 * persistent memory the benefit is doubtful. We can add that later if we can
292 * show it helps.
293 */
294 static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
295 unsigned long size_flag)
296 {
297 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
298 void *entry, **slot;
299
300 restart:
301 spin_lock_irq(&mapping->tree_lock);
302 entry = get_unlocked_mapping_entry(mapping, index, &slot);
303
304 if (entry) {
305 if (size_flag & RADIX_DAX_PMD) {
306 if (!radix_tree_exceptional_entry(entry) ||
307 dax_is_pte_entry(entry)) {
308 put_unlocked_mapping_entry(mapping, index,
309 entry);
310 entry = ERR_PTR(-EEXIST);
311 goto out_unlock;
312 }
313 } else { /* trying to grab a PTE entry */
314 if (radix_tree_exceptional_entry(entry) &&
315 dax_is_pmd_entry(entry) &&
316 (dax_is_zero_entry(entry) ||
317 dax_is_empty_entry(entry))) {
318 pmd_downgrade = true;
319 }
320 }
321 }
322
323 /* No entry for given index? Make sure radix tree is big enough. */
324 if (!entry || pmd_downgrade) {
325 int err;
326
327 if (pmd_downgrade) {
328 /*
329 * Make sure 'entry' remains valid while we drop
330 * mapping->tree_lock.
331 */
332 entry = lock_slot(mapping, slot);
333 }
334
335 spin_unlock_irq(&mapping->tree_lock);
336 /*
337 * Besides huge zero pages the only other thing that gets
338 * downgraded are empty entries which don't need to be
339 * unmapped.
340 */
341 if (pmd_downgrade && dax_is_zero_entry(entry))
342 unmap_mapping_range(mapping,
343 (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
344
345 err = radix_tree_preload(
346 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
347 if (err) {
348 if (pmd_downgrade)
349 put_locked_mapping_entry(mapping, index, entry);
350 return ERR_PTR(err);
351 }
352 spin_lock_irq(&mapping->tree_lock);
353
354 if (pmd_downgrade) {
355 radix_tree_delete(&mapping->page_tree, index);
356 mapping->nrexceptional--;
357 dax_wake_mapping_entry_waiter(mapping, index, entry,
358 true);
359 }
360
361 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
362
363 err = __radix_tree_insert(&mapping->page_tree, index,
364 dax_radix_order(entry), entry);
365 radix_tree_preload_end();
366 if (err) {
367 spin_unlock_irq(&mapping->tree_lock);
368 /*
369 * Someone already created the entry? This is a
370 * normal failure when inserting PMDs in a range
371 * that already contains PTEs. In that case we want
372 * to return -EEXIST immediately.
373 */
374 if (err == -EEXIST && !(size_flag & RADIX_DAX_PMD))
375 goto restart;
376 /*
377 * Our insertion of a DAX PMD entry failed, most
378 * likely because it collided with a PTE sized entry
379 * at a different index in the PMD range. We haven't
380 * inserted anything into the radix tree and have no
381 * waiters to wake.
382 */
383 return ERR_PTR(err);
384 }
385 /* Good, we have inserted empty locked entry into the tree. */
386 mapping->nrexceptional++;
387 spin_unlock_irq(&mapping->tree_lock);
388 return entry;
389 }
390 /* Normal page in radix tree? */
391 if (!radix_tree_exceptional_entry(entry)) {
392 struct page *page = entry;
393
394 get_page(page);
395 spin_unlock_irq(&mapping->tree_lock);
396 lock_page(page);
397 /* Page got truncated? Retry... */
398 if (unlikely(page->mapping != mapping)) {
399 unlock_page(page);
400 put_page(page);
401 goto restart;
402 }
403 return page;
404 }
405 entry = lock_slot(mapping, slot);
406 out_unlock:
407 spin_unlock_irq(&mapping->tree_lock);
408 return entry;
409 }
410
411 /*
412 * We do not necessarily hold the mapping->tree_lock when we call this
413 * function so it is possible that 'entry' is no longer a valid item in the
414 * radix tree. This is okay because all we really need to do is to find the
415 * correct waitqueue where tasks might be waiting for that old 'entry' and
416 * wake them.
417 */
418 void dax_wake_mapping_entry_waiter(struct address_space *mapping,
419 pgoff_t index, void *entry, bool wake_all)
420 {
421 struct exceptional_entry_key key;
422 wait_queue_head_t *wq;
423
424 wq = dax_entry_waitqueue(mapping, index, entry, &key);
425
426 /*
427 * Checking for locked entry and prepare_to_wait_exclusive() happens
428 * under mapping->tree_lock, ditto for entry handling in our callers.
429 * So at this point all tasks that could have seen our entry locked
430 * must be in the waitqueue and the following check will see them.
431 */
432 if (waitqueue_active(wq))
433 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
434 }
435
436 void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
437 {
438 void *entry, **slot;
439
440 spin_lock_irq(&mapping->tree_lock);
441 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
442 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
443 !slot_locked(mapping, slot))) {
444 spin_unlock_irq(&mapping->tree_lock);
445 return;
446 }
447 unlock_slot(mapping, slot);
448 spin_unlock_irq(&mapping->tree_lock);
449 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
450 }
451
452 /*
453 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
454 * entry to get unlocked before deleting it.
455 */
456 int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
457 {
458 void *entry;
459
460 spin_lock_irq(&mapping->tree_lock);
461 entry = get_unlocked_mapping_entry(mapping, index, NULL);
462 /*
463 * This gets called from truncate / punch_hole path. As such, the caller
464 * must hold locks protecting against concurrent modifications of the
465 * radix tree (usually fs-private i_mmap_sem for writing). Since the
466 * caller has seen exceptional entry for this index, we better find it
467 * at that index as well...
468 */
469 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
470 spin_unlock_irq(&mapping->tree_lock);
471 return 0;
472 }
473 radix_tree_delete(&mapping->page_tree, index);
474 mapping->nrexceptional--;
475 spin_unlock_irq(&mapping->tree_lock);
476 dax_wake_mapping_entry_waiter(mapping, index, entry, true);
477
478 return 1;
479 }
480
481 /*
482 * The user has performed a load from a hole in the file. Allocating
483 * a new page in the file would cause excessive storage usage for
484 * workloads with sparse files. We allocate a page cache page instead.
485 * We'll kick it out of the page cache if it's ever written to,
486 * otherwise it will simply fall out of the page cache under memory
487 * pressure without ever having been dirtied.
488 */
489 static int dax_load_hole(struct address_space *mapping, void *entry,
490 struct vm_fault *vmf)
491 {
492 struct page *page;
493
494 /* Hole page already exists? Return it... */
495 if (!radix_tree_exceptional_entry(entry)) {
496 vmf->page = entry;
497 return VM_FAULT_LOCKED;
498 }
499
500 /* This will replace locked radix tree entry with a hole page */
501 page = find_or_create_page(mapping, vmf->pgoff,
502 vmf->gfp_mask | __GFP_ZERO);
503 if (!page) {
504 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
505 return VM_FAULT_OOM;
506 }
507 vmf->page = page;
508 return VM_FAULT_LOCKED;
509 }
510
511 static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
512 struct page *to, unsigned long vaddr)
513 {
514 struct blk_dax_ctl dax = {
515 .sector = sector,
516 .size = size,
517 };
518 void *vto;
519
520 if (dax_map_atomic(bdev, &dax) < 0)
521 return PTR_ERR(dax.addr);
522 vto = kmap_atomic(to);
523 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
524 kunmap_atomic(vto);
525 dax_unmap_atomic(bdev, &dax);
526 return 0;
527 }
528
529 /*
530 * By this point grab_mapping_entry() has ensured that we have a locked entry
531 * of the appropriate size so we don't have to worry about downgrading PMDs to
532 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
533 * already in the tree, we will skip the insertion and just dirty the PMD as
534 * appropriate.
535 */
536 static void *dax_insert_mapping_entry(struct address_space *mapping,
537 struct vm_fault *vmf,
538 void *entry, sector_t sector,
539 unsigned long flags)
540 {
541 struct radix_tree_root *page_tree = &mapping->page_tree;
542 int error = 0;
543 bool hole_fill = false;
544 void *new_entry;
545 pgoff_t index = vmf->pgoff;
546
547 if (vmf->flags & FAULT_FLAG_WRITE)
548 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
549
550 /* Replacing hole page with block mapping? */
551 if (!radix_tree_exceptional_entry(entry)) {
552 hole_fill = true;
553 /*
554 * Unmap the page now before we remove it from page cache below.
555 * The page is locked so it cannot be faulted in again.
556 */
557 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
558 PAGE_SIZE, 0);
559 error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
560 if (error)
561 return ERR_PTR(error);
562 } else if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_HZP)) {
563 /* replacing huge zero page with PMD block mapping */
564 unmap_mapping_range(mapping,
565 (vmf->pgoff << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
566 }
567
568 spin_lock_irq(&mapping->tree_lock);
569 new_entry = dax_radix_locked_entry(sector, flags);
570
571 if (hole_fill) {
572 __delete_from_page_cache(entry, NULL);
573 /* Drop pagecache reference */
574 put_page(entry);
575 error = __radix_tree_insert(page_tree, index,
576 dax_radix_order(new_entry), new_entry);
577 if (error) {
578 new_entry = ERR_PTR(error);
579 goto unlock;
580 }
581 mapping->nrexceptional++;
582 } else if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
583 /*
584 * Only swap our new entry into the radix tree if the current
585 * entry is a zero page or an empty entry. If a normal PTE or
586 * PMD entry is already in the tree, we leave it alone. This
587 * means that if we are trying to insert a PTE and the
588 * existing entry is a PMD, we will just leave the PMD in the
589 * tree and dirty it if necessary.
590 */
591 void **slot;
592 void *ret;
593
594 ret = __radix_tree_lookup(page_tree, index, NULL, &slot);
595 WARN_ON_ONCE(ret != entry);
596 radix_tree_replace_slot(slot, new_entry);
597 }
598 if (vmf->flags & FAULT_FLAG_WRITE)
599 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
600 unlock:
601 spin_unlock_irq(&mapping->tree_lock);
602 if (hole_fill) {
603 radix_tree_preload_end();
604 /*
605 * We don't need hole page anymore, it has been replaced with
606 * locked radix tree entry now.
607 */
608 if (mapping->a_ops->freepage)
609 mapping->a_ops->freepage(entry);
610 unlock_page(entry);
611 put_page(entry);
612 }
613 return new_entry;
614 }
615
616 static int dax_writeback_one(struct block_device *bdev,
617 struct address_space *mapping, pgoff_t index, void *entry)
618 {
619 struct radix_tree_root *page_tree = &mapping->page_tree;
620 struct radix_tree_node *node;
621 struct blk_dax_ctl dax;
622 void **slot;
623 int ret = 0;
624
625 spin_lock_irq(&mapping->tree_lock);
626 /*
627 * Regular page slots are stabilized by the page lock even
628 * without the tree itself locked. These unlocked entries
629 * need verification under the tree lock.
630 */
631 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
632 goto unlock;
633 if (*slot != entry)
634 goto unlock;
635
636 /* another fsync thread may have already written back this entry */
637 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
638 goto unlock;
639
640 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
641 dax_is_zero_entry(entry))) {
642 ret = -EIO;
643 goto unlock;
644 }
645
646 /*
647 * Even if dax_writeback_mapping_range() was given a wbc->range_start
648 * in the middle of a PMD, the 'index' we are given will be aligned to
649 * the start index of the PMD, as will the sector we pull from
650 * 'entry'. This allows us to flush for PMD_SIZE and not have to
651 * worry about partial PMD writebacks.
652 */
653 dax.sector = dax_radix_sector(entry);
654 dax.size = PAGE_SIZE << dax_radix_order(entry);
655 spin_unlock_irq(&mapping->tree_lock);
656
657 /*
658 * We cannot hold tree_lock while calling dax_map_atomic() because it
659 * eventually calls cond_resched().
660 */
661 ret = dax_map_atomic(bdev, &dax);
662 if (ret < 0)
663 return ret;
664
665 if (WARN_ON_ONCE(ret < dax.size)) {
666 ret = -EIO;
667 goto unmap;
668 }
669
670 wb_cache_pmem(dax.addr, dax.size);
671
672 spin_lock_irq(&mapping->tree_lock);
673 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
674 spin_unlock_irq(&mapping->tree_lock);
675 unmap:
676 dax_unmap_atomic(bdev, &dax);
677 return ret;
678
679 unlock:
680 spin_unlock_irq(&mapping->tree_lock);
681 return ret;
682 }
683
684 /*
685 * Flush the mapping to the persistent domain within the byte range of [start,
686 * end]. This is required by data integrity operations to ensure file data is
687 * on persistent storage prior to completion of the operation.
688 */
689 int dax_writeback_mapping_range(struct address_space *mapping,
690 struct block_device *bdev, struct writeback_control *wbc)
691 {
692 struct inode *inode = mapping->host;
693 pgoff_t start_index, end_index;
694 pgoff_t indices[PAGEVEC_SIZE];
695 struct pagevec pvec;
696 bool done = false;
697 int i, ret = 0;
698
699 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
700 return -EIO;
701
702 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
703 return 0;
704
705 start_index = wbc->range_start >> PAGE_SHIFT;
706 end_index = wbc->range_end >> PAGE_SHIFT;
707
708 tag_pages_for_writeback(mapping, start_index, end_index);
709
710 pagevec_init(&pvec, 0);
711 while (!done) {
712 pvec.nr = find_get_entries_tag(mapping, start_index,
713 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
714 pvec.pages, indices);
715
716 if (pvec.nr == 0)
717 break;
718
719 for (i = 0; i < pvec.nr; i++) {
720 if (indices[i] > end_index) {
721 done = true;
722 break;
723 }
724
725 ret = dax_writeback_one(bdev, mapping, indices[i],
726 pvec.pages[i]);
727 if (ret < 0)
728 return ret;
729 }
730 }
731 return 0;
732 }
733 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
734
735 static int dax_insert_mapping(struct address_space *mapping,
736 struct block_device *bdev, sector_t sector, size_t size,
737 void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
738 {
739 unsigned long vaddr = (unsigned long)vmf->virtual_address;
740 struct blk_dax_ctl dax = {
741 .sector = sector,
742 .size = size,
743 };
744 void *ret;
745 void *entry = *entryp;
746
747 if (dax_map_atomic(bdev, &dax) < 0)
748 return PTR_ERR(dax.addr);
749 dax_unmap_atomic(bdev, &dax);
750
751 ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector, 0);
752 if (IS_ERR(ret))
753 return PTR_ERR(ret);
754 *entryp = ret;
755
756 return vm_insert_mixed(vma, vaddr, dax.pfn);
757 }
758
759 /**
760 * dax_pfn_mkwrite - handle first write to DAX page
761 * @vma: The virtual memory area where the fault occurred
762 * @vmf: The description of the fault
763 */
764 int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
765 {
766 struct file *file = vma->vm_file;
767 struct address_space *mapping = file->f_mapping;
768 void *entry;
769 pgoff_t index = vmf->pgoff;
770
771 spin_lock_irq(&mapping->tree_lock);
772 entry = get_unlocked_mapping_entry(mapping, index, NULL);
773 if (!entry || !radix_tree_exceptional_entry(entry))
774 goto out;
775 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
776 put_unlocked_mapping_entry(mapping, index, entry);
777 out:
778 spin_unlock_irq(&mapping->tree_lock);
779 return VM_FAULT_NOPAGE;
780 }
781 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
782
783 static bool dax_range_is_aligned(struct block_device *bdev,
784 unsigned int offset, unsigned int length)
785 {
786 unsigned short sector_size = bdev_logical_block_size(bdev);
787
788 if (!IS_ALIGNED(offset, sector_size))
789 return false;
790 if (!IS_ALIGNED(length, sector_size))
791 return false;
792
793 return true;
794 }
795
796 int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
797 unsigned int offset, unsigned int length)
798 {
799 struct blk_dax_ctl dax = {
800 .sector = sector,
801 .size = PAGE_SIZE,
802 };
803
804 if (dax_range_is_aligned(bdev, offset, length)) {
805 sector_t start_sector = dax.sector + (offset >> 9);
806
807 return blkdev_issue_zeroout(bdev, start_sector,
808 length >> 9, GFP_NOFS, true);
809 } else {
810 if (dax_map_atomic(bdev, &dax) < 0)
811 return PTR_ERR(dax.addr);
812 clear_pmem(dax.addr + offset, length);
813 dax_unmap_atomic(bdev, &dax);
814 }
815 return 0;
816 }
817 EXPORT_SYMBOL_GPL(__dax_zero_page_range);
818
819 #ifdef CONFIG_FS_IOMAP
820 static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
821 {
822 return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
823 }
824
825 static loff_t
826 dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
827 struct iomap *iomap)
828 {
829 struct iov_iter *iter = data;
830 loff_t end = pos + length, done = 0;
831 ssize_t ret = 0;
832
833 if (iov_iter_rw(iter) == READ) {
834 end = min(end, i_size_read(inode));
835 if (pos >= end)
836 return 0;
837
838 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
839 return iov_iter_zero(min(length, end - pos), iter);
840 }
841
842 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
843 return -EIO;
844
845 while (pos < end) {
846 unsigned offset = pos & (PAGE_SIZE - 1);
847 struct blk_dax_ctl dax = { 0 };
848 ssize_t map_len;
849
850 dax.sector = dax_iomap_sector(iomap, pos);
851 dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
852 map_len = dax_map_atomic(iomap->bdev, &dax);
853 if (map_len < 0) {
854 ret = map_len;
855 break;
856 }
857
858 dax.addr += offset;
859 map_len -= offset;
860 if (map_len > end - pos)
861 map_len = end - pos;
862
863 if (iov_iter_rw(iter) == WRITE)
864 map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
865 else
866 map_len = copy_to_iter(dax.addr, map_len, iter);
867 dax_unmap_atomic(iomap->bdev, &dax);
868 if (map_len <= 0) {
869 ret = map_len ? map_len : -EFAULT;
870 break;
871 }
872
873 pos += map_len;
874 length -= map_len;
875 done += map_len;
876 }
877
878 return done ? done : ret;
879 }
880
881 /**
882 * dax_iomap_rw - Perform I/O to a DAX file
883 * @iocb: The control block for this I/O
884 * @iter: The addresses to do I/O from or to
885 * @ops: iomap ops passed from the file system
886 *
887 * This function performs read and write operations to directly mapped
888 * persistent memory. The callers needs to take care of read/write exclusion
889 * and evicting any page cache pages in the region under I/O.
890 */
891 ssize_t
892 dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
893 struct iomap_ops *ops)
894 {
895 struct address_space *mapping = iocb->ki_filp->f_mapping;
896 struct inode *inode = mapping->host;
897 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
898 unsigned flags = 0;
899
900 if (iov_iter_rw(iter) == WRITE)
901 flags |= IOMAP_WRITE;
902
903 /*
904 * Yes, even DAX files can have page cache attached to them: A zeroed
905 * page is inserted into the pagecache when we have to serve a write
906 * fault on a hole. It should never be dirtied and can simply be
907 * dropped from the pagecache once we get real data for the page.
908 *
909 * XXX: This is racy against mmap, and there's nothing we can do about
910 * it. We'll eventually need to shift this down even further so that
911 * we can check if we allocated blocks over a hole first.
912 */
913 if (mapping->nrpages) {
914 ret = invalidate_inode_pages2_range(mapping,
915 pos >> PAGE_SHIFT,
916 (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
917 WARN_ON_ONCE(ret);
918 }
919
920 while (iov_iter_count(iter)) {
921 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
922 iter, dax_iomap_actor);
923 if (ret <= 0)
924 break;
925 pos += ret;
926 done += ret;
927 }
928
929 iocb->ki_pos += done;
930 return done ? done : ret;
931 }
932 EXPORT_SYMBOL_GPL(dax_iomap_rw);
933
934 /**
935 * dax_iomap_fault - handle a page fault on a DAX file
936 * @vma: The virtual memory area where the fault occurred
937 * @vmf: The description of the fault
938 * @ops: iomap ops passed from the file system
939 *
940 * When a page fault occurs, filesystems may call this helper in their fault
941 * or mkwrite handler for DAX files. Assumes the caller has done all the
942 * necessary locking for the page fault to proceed successfully.
943 */
944 int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
945 struct iomap_ops *ops)
946 {
947 struct address_space *mapping = vma->vm_file->f_mapping;
948 struct inode *inode = mapping->host;
949 unsigned long vaddr = (unsigned long)vmf->virtual_address;
950 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
951 sector_t sector;
952 struct iomap iomap = { 0 };
953 unsigned flags = IOMAP_FAULT;
954 int error, major = 0;
955 int locked_status = 0;
956 void *entry;
957
958 /*
959 * Check whether offset isn't beyond end of file now. Caller is supposed
960 * to hold locks serializing us with truncate / punch hole so this is
961 * a reliable test.
962 */
963 if (pos >= i_size_read(inode))
964 return VM_FAULT_SIGBUS;
965
966 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
967 if (IS_ERR(entry)) {
968 error = PTR_ERR(entry);
969 goto out;
970 }
971
972 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
973 flags |= IOMAP_WRITE;
974
975 /*
976 * Note that we don't bother to use iomap_apply here: DAX required
977 * the file system block size to be equal the page size, which means
978 * that we never have to deal with more than a single extent here.
979 */
980 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
981 if (error)
982 goto unlock_entry;
983 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
984 error = -EIO; /* fs corruption? */
985 goto finish_iomap;
986 }
987
988 sector = dax_iomap_sector(&iomap, pos);
989
990 if (vmf->cow_page) {
991 switch (iomap.type) {
992 case IOMAP_HOLE:
993 case IOMAP_UNWRITTEN:
994 clear_user_highpage(vmf->cow_page, vaddr);
995 break;
996 case IOMAP_MAPPED:
997 error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
998 vmf->cow_page, vaddr);
999 break;
1000 default:
1001 WARN_ON_ONCE(1);
1002 error = -EIO;
1003 break;
1004 }
1005
1006 if (error)
1007 goto finish_iomap;
1008 if (!radix_tree_exceptional_entry(entry)) {
1009 vmf->page = entry;
1010 locked_status = VM_FAULT_LOCKED;
1011 } else {
1012 vmf->entry = entry;
1013 locked_status = VM_FAULT_DAX_LOCKED;
1014 }
1015 goto finish_iomap;
1016 }
1017
1018 switch (iomap.type) {
1019 case IOMAP_MAPPED:
1020 if (iomap.flags & IOMAP_F_NEW) {
1021 count_vm_event(PGMAJFAULT);
1022 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1023 major = VM_FAULT_MAJOR;
1024 }
1025 error = dax_insert_mapping(mapping, iomap.bdev, sector,
1026 PAGE_SIZE, &entry, vma, vmf);
1027 break;
1028 case IOMAP_UNWRITTEN:
1029 case IOMAP_HOLE:
1030 if (!(vmf->flags & FAULT_FLAG_WRITE)) {
1031 locked_status = dax_load_hole(mapping, entry, vmf);
1032 break;
1033 }
1034 /*FALLTHRU*/
1035 default:
1036 WARN_ON_ONCE(1);
1037 error = -EIO;
1038 break;
1039 }
1040
1041 finish_iomap:
1042 if (ops->iomap_end) {
1043 if (error) {
1044 /* keep previous error */
1045 ops->iomap_end(inode, pos, PAGE_SIZE, 0, flags,
1046 &iomap);
1047 } else {
1048 error = ops->iomap_end(inode, pos, PAGE_SIZE,
1049 PAGE_SIZE, flags, &iomap);
1050 }
1051 }
1052 unlock_entry:
1053 if (!locked_status || error)
1054 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
1055 out:
1056 if (error == -ENOMEM)
1057 return VM_FAULT_OOM | major;
1058 /* -EBUSY is fine, somebody else faulted on the same PTE */
1059 if (error < 0 && error != -EBUSY)
1060 return VM_FAULT_SIGBUS | major;
1061 if (locked_status) {
1062 WARN_ON_ONCE(error); /* -EBUSY from ops->iomap_end? */
1063 return locked_status;
1064 }
1065 return VM_FAULT_NOPAGE | major;
1066 }
1067 EXPORT_SYMBOL_GPL(dax_iomap_fault);
1068
1069 #ifdef CONFIG_FS_DAX_PMD
1070 /*
1071 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1072 * more often than one might expect in the below functions.
1073 */
1074 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1075
1076 static int dax_pmd_insert_mapping(struct vm_area_struct *vma, pmd_t *pmd,
1077 struct vm_fault *vmf, unsigned long address,
1078 struct iomap *iomap, loff_t pos, bool write, void **entryp)
1079 {
1080 struct address_space *mapping = vma->vm_file->f_mapping;
1081 struct block_device *bdev = iomap->bdev;
1082 struct blk_dax_ctl dax = {
1083 .sector = dax_iomap_sector(iomap, pos),
1084 .size = PMD_SIZE,
1085 };
1086 long length = dax_map_atomic(bdev, &dax);
1087 void *ret;
1088
1089 if (length < 0) /* dax_map_atomic() failed */
1090 return VM_FAULT_FALLBACK;
1091 if (length < PMD_SIZE)
1092 goto unmap_fallback;
1093 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR)
1094 goto unmap_fallback;
1095 if (!pfn_t_devmap(dax.pfn))
1096 goto unmap_fallback;
1097
1098 dax_unmap_atomic(bdev, &dax);
1099
1100 ret = dax_insert_mapping_entry(mapping, vmf, *entryp, dax.sector,
1101 RADIX_DAX_PMD);
1102 if (IS_ERR(ret))
1103 return VM_FAULT_FALLBACK;
1104 *entryp = ret;
1105
1106 return vmf_insert_pfn_pmd(vma, address, pmd, dax.pfn, write);
1107
1108 unmap_fallback:
1109 dax_unmap_atomic(bdev, &dax);
1110 return VM_FAULT_FALLBACK;
1111 }
1112
1113 static int dax_pmd_load_hole(struct vm_area_struct *vma, pmd_t *pmd,
1114 struct vm_fault *vmf, unsigned long address,
1115 struct iomap *iomap, void **entryp)
1116 {
1117 struct address_space *mapping = vma->vm_file->f_mapping;
1118 unsigned long pmd_addr = address & PMD_MASK;
1119 struct page *zero_page;
1120 spinlock_t *ptl;
1121 pmd_t pmd_entry;
1122 void *ret;
1123
1124 zero_page = mm_get_huge_zero_page(vma->vm_mm);
1125
1126 if (unlikely(!zero_page))
1127 return VM_FAULT_FALLBACK;
1128
1129 ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0,
1130 RADIX_DAX_PMD | RADIX_DAX_HZP);
1131 if (IS_ERR(ret))
1132 return VM_FAULT_FALLBACK;
1133 *entryp = ret;
1134
1135 ptl = pmd_lock(vma->vm_mm, pmd);
1136 if (!pmd_none(*pmd)) {
1137 spin_unlock(ptl);
1138 return VM_FAULT_FALLBACK;
1139 }
1140
1141 pmd_entry = mk_pmd(zero_page, vma->vm_page_prot);
1142 pmd_entry = pmd_mkhuge(pmd_entry);
1143 set_pmd_at(vma->vm_mm, pmd_addr, pmd, pmd_entry);
1144 spin_unlock(ptl);
1145 return VM_FAULT_NOPAGE;
1146 }
1147
1148 int dax_iomap_pmd_fault(struct vm_area_struct *vma, unsigned long address,
1149 pmd_t *pmd, unsigned int flags, struct iomap_ops *ops)
1150 {
1151 struct address_space *mapping = vma->vm_file->f_mapping;
1152 unsigned long pmd_addr = address & PMD_MASK;
1153 bool write = flags & FAULT_FLAG_WRITE;
1154 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
1155 struct inode *inode = mapping->host;
1156 int result = VM_FAULT_FALLBACK;
1157 struct iomap iomap = { 0 };
1158 pgoff_t max_pgoff, pgoff;
1159 struct vm_fault vmf;
1160 void *entry;
1161 loff_t pos;
1162 int error;
1163
1164 /* Fall back to PTEs if we're going to COW */
1165 if (write && !(vma->vm_flags & VM_SHARED))
1166 goto fallback;
1167
1168 /* If the PMD would extend outside the VMA */
1169 if (pmd_addr < vma->vm_start)
1170 goto fallback;
1171 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1172 goto fallback;
1173
1174 /*
1175 * Check whether offset isn't beyond end of file now. Caller is
1176 * supposed to hold locks serializing us with truncate / punch hole so
1177 * this is a reliable test.
1178 */
1179 pgoff = linear_page_index(vma, pmd_addr);
1180 max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
1181
1182 if (pgoff > max_pgoff)
1183 return VM_FAULT_SIGBUS;
1184
1185 /* If the PMD would extend beyond the file size */
1186 if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
1187 goto fallback;
1188
1189 /*
1190 * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
1191 * PMD or a HZP entry. If it can't (because a 4k page is already in
1192 * the tree, for instance), it will return -EEXIST and we just fall
1193 * back to 4k entries.
1194 */
1195 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1196 if (IS_ERR(entry))
1197 goto fallback;
1198
1199 /*
1200 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1201 * setting up a mapping, so really we're using iomap_begin() as a way
1202 * to look up our filesystem block.
1203 */
1204 pos = (loff_t)pgoff << PAGE_SHIFT;
1205 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1206 if (error)
1207 goto unlock_entry;
1208 if (iomap.offset + iomap.length < pos + PMD_SIZE)
1209 goto finish_iomap;
1210
1211 vmf.pgoff = pgoff;
1212 vmf.flags = flags;
1213 vmf.gfp_mask = mapping_gfp_mask(mapping) | __GFP_IO;
1214
1215 switch (iomap.type) {
1216 case IOMAP_MAPPED:
1217 result = dax_pmd_insert_mapping(vma, pmd, &vmf, address,
1218 &iomap, pos, write, &entry);
1219 break;
1220 case IOMAP_UNWRITTEN:
1221 case IOMAP_HOLE:
1222 if (WARN_ON_ONCE(write))
1223 goto finish_iomap;
1224 result = dax_pmd_load_hole(vma, pmd, &vmf, address, &iomap,
1225 &entry);
1226 break;
1227 default:
1228 WARN_ON_ONCE(1);
1229 break;
1230 }
1231
1232 finish_iomap:
1233 if (ops->iomap_end) {
1234 if (result == VM_FAULT_FALLBACK) {
1235 ops->iomap_end(inode, pos, PMD_SIZE, 0, iomap_flags,
1236 &iomap);
1237 } else {
1238 error = ops->iomap_end(inode, pos, PMD_SIZE, PMD_SIZE,
1239 iomap_flags, &iomap);
1240 if (error)
1241 result = VM_FAULT_FALLBACK;
1242 }
1243 }
1244 unlock_entry:
1245 put_locked_mapping_entry(mapping, pgoff, entry);
1246 fallback:
1247 if (result == VM_FAULT_FALLBACK) {
1248 split_huge_pmd(vma, pmd, address);
1249 count_vm_event(THP_FAULT_FALLBACK);
1250 }
1251 return result;
1252 }
1253 EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault);
1254 #endif /* CONFIG_FS_DAX_PMD */
1255 #endif /* CONFIG_FS_IOMAP */