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>
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.
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
17 #include <linux/atomic.h>
18 #include <linux/blkdev.h>
19 #include <linux/buffer_head.h>
20 #include <linux/dax.h>
22 #include <linux/genhd.h>
23 #include <linux/highmem.h>
24 #include <linux/memcontrol.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>
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)
41 static wait_queue_head_t wait_table
[DAX_WAIT_TABLE_ENTRIES
];
43 static int __init
init_dax_wait_table(void)
47 for (i
= 0; i
< DAX_WAIT_TABLE_ENTRIES
; i
++)
48 init_waitqueue_head(wait_table
+ i
);
51 fs_initcall(init_dax_wait_table
);
53 static long dax_map_atomic(struct block_device
*bdev
, struct blk_dax_ctl
*dax
)
55 struct request_queue
*q
= bdev
->bd_queue
;
58 dax
->addr
= ERR_PTR(-EIO
);
59 if (blk_queue_enter(q
, true) != 0)
62 rc
= bdev_direct_access(bdev
, dax
);
64 dax
->addr
= ERR_PTR(rc
);
71 static void dax_unmap_atomic(struct block_device
*bdev
,
72 const struct blk_dax_ctl
*dax
)
74 if (IS_ERR(dax
->addr
))
76 blk_queue_exit(bdev
->bd_queue
);
79 static int dax_is_pmd_entry(void *entry
)
81 return (unsigned long)entry
& RADIX_DAX_PMD
;
84 static int dax_is_pte_entry(void *entry
)
86 return !((unsigned long)entry
& RADIX_DAX_PMD
);
89 static int dax_is_zero_entry(void *entry
)
91 return (unsigned long)entry
& RADIX_DAX_HZP
;
94 static int dax_is_empty_entry(void *entry
)
96 return (unsigned long)entry
& RADIX_DAX_EMPTY
;
99 struct page
*read_dax_sector(struct block_device
*bdev
, sector_t n
)
101 struct page
*page
= alloc_pages(GFP_KERNEL
, 0);
102 struct blk_dax_ctl dax
= {
104 .sector
= n
& ~((((int) PAGE_SIZE
) / 512) - 1),
109 return ERR_PTR(-ENOMEM
);
111 rc
= dax_map_atomic(bdev
, &dax
);
114 memcpy_from_pmem(page_address(page
), dax
.addr
, PAGE_SIZE
);
115 dax_unmap_atomic(bdev
, &dax
);
119 static bool buffer_written(struct buffer_head
*bh
)
121 return buffer_mapped(bh
) && !buffer_unwritten(bh
);
124 static sector_t
to_sector(const struct buffer_head
*bh
,
125 const struct inode
*inode
)
127 sector_t sector
= bh
->b_blocknr
<< (inode
->i_blkbits
- 9);
132 static ssize_t
dax_io(struct inode
*inode
, struct iov_iter
*iter
,
133 loff_t start
, loff_t end
, get_block_t get_block
,
134 struct buffer_head
*bh
)
136 loff_t pos
= start
, max
= start
, bh_max
= start
;
138 struct block_device
*bdev
= NULL
;
139 int rw
= iov_iter_rw(iter
), rc
;
141 struct blk_dax_ctl dax
= {
142 .addr
= ERR_PTR(-EIO
),
144 unsigned blkbits
= inode
->i_blkbits
;
145 sector_t file_blks
= (i_size_read(inode
) + (1 << blkbits
) - 1)
149 end
= min(end
, i_size_read(inode
));
154 long page
= pos
>> PAGE_SHIFT
;
155 sector_t block
= page
<< (PAGE_SHIFT
- blkbits
);
156 unsigned first
= pos
- (block
<< blkbits
);
160 bh
->b_size
= PAGE_ALIGN(end
- pos
);
162 rc
= get_block(inode
, block
, bh
, rw
== WRITE
);
165 bh_max
= pos
- first
+ bh
->b_size
;
168 * We allow uninitialized buffers for writes
169 * beyond EOF as those cannot race with faults
172 (buffer_new(bh
) && block
< file_blks
) ||
173 (rw
== WRITE
&& buffer_unwritten(bh
)));
175 unsigned done
= bh
->b_size
-
176 (bh_max
- (pos
- first
));
177 bh
->b_blocknr
+= done
>> blkbits
;
181 hole
= rw
== READ
&& !buffer_written(bh
);
183 size
= bh
->b_size
- first
;
185 dax_unmap_atomic(bdev
, &dax
);
186 dax
.sector
= to_sector(bh
, inode
);
187 dax
.size
= bh
->b_size
;
188 map_len
= dax_map_atomic(bdev
, &dax
);
194 size
= map_len
- first
;
197 * pos + size is one past the last offset for IO,
198 * so pos + size can overflow loff_t at extreme offsets.
199 * Cast to u64 to catch this and get the true minimum.
201 max
= min_t(u64
, pos
+ size
, end
);
204 if (iov_iter_rw(iter
) == WRITE
) {
205 len
= copy_from_iter_pmem(dax
.addr
, max
- pos
, iter
);
207 len
= copy_to_iter((void __force
*) dax
.addr
, max
- pos
,
210 len
= iov_iter_zero(max
- pos
, iter
);
218 if (!IS_ERR(dax
.addr
))
222 dax_unmap_atomic(bdev
, &dax
);
224 return (pos
== start
) ? rc
: pos
- start
;
228 * dax_do_io - Perform I/O to a DAX file
229 * @iocb: The control block for this I/O
230 * @inode: The file which the I/O is directed at
231 * @iter: The addresses to do I/O from or to
232 * @get_block: The filesystem method used to translate file offsets to blocks
233 * @end_io: A filesystem callback for I/O completion
236 * This function uses the same locking scheme as do_blockdev_direct_IO:
237 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
238 * caller for writes. For reads, we take and release the i_mutex ourselves.
239 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
240 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
243 ssize_t
dax_do_io(struct kiocb
*iocb
, struct inode
*inode
,
244 struct iov_iter
*iter
, get_block_t get_block
,
245 dio_iodone_t end_io
, int flags
)
247 struct buffer_head bh
;
248 ssize_t retval
= -EINVAL
;
249 loff_t pos
= iocb
->ki_pos
;
250 loff_t end
= pos
+ iov_iter_count(iter
);
252 memset(&bh
, 0, sizeof(bh
));
253 bh
.b_bdev
= inode
->i_sb
->s_bdev
;
255 if ((flags
& DIO_LOCKING
) && iov_iter_rw(iter
) == READ
)
258 /* Protects against truncate */
259 if (!(flags
& DIO_SKIP_DIO_COUNT
))
260 inode_dio_begin(inode
);
262 retval
= dax_io(inode
, iter
, pos
, end
, get_block
, &bh
);
264 if ((flags
& DIO_LOCKING
) && iov_iter_rw(iter
) == READ
)
270 err
= end_io(iocb
, pos
, retval
, bh
.b_private
);
275 if (!(flags
& DIO_SKIP_DIO_COUNT
))
276 inode_dio_end(inode
);
279 EXPORT_SYMBOL_GPL(dax_do_io
);
282 * DAX radix tree locking
284 struct exceptional_entry_key
{
285 struct address_space
*mapping
;
289 struct wait_exceptional_entry_queue
{
291 struct exceptional_entry_key key
;
294 static wait_queue_head_t
*dax_entry_waitqueue(struct address_space
*mapping
,
295 pgoff_t index
, void *entry
, struct exceptional_entry_key
*key
)
300 * If 'entry' is a PMD, align the 'index' that we use for the wait
301 * queue to the start of that PMD. This ensures that all offsets in
302 * the range covered by the PMD map to the same bit lock.
304 if (dax_is_pmd_entry(entry
))
305 index
&= ~((1UL << (PMD_SHIFT
- PAGE_SHIFT
)) - 1);
307 key
->mapping
= mapping
;
308 key
->entry_start
= index
;
310 hash
= hash_long((unsigned long)mapping
^ index
, DAX_WAIT_TABLE_BITS
);
311 return wait_table
+ hash
;
314 static int wake_exceptional_entry_func(wait_queue_t
*wait
, unsigned int mode
,
315 int sync
, void *keyp
)
317 struct exceptional_entry_key
*key
= keyp
;
318 struct wait_exceptional_entry_queue
*ewait
=
319 container_of(wait
, struct wait_exceptional_entry_queue
, wait
);
321 if (key
->mapping
!= ewait
->key
.mapping
||
322 key
->entry_start
!= ewait
->key
.entry_start
)
324 return autoremove_wake_function(wait
, mode
, sync
, NULL
);
328 * Check whether the given slot is locked. The function must be called with
329 * mapping->tree_lock held
331 static inline int slot_locked(struct address_space
*mapping
, void **slot
)
333 unsigned long entry
= (unsigned long)
334 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
335 return entry
& RADIX_DAX_ENTRY_LOCK
;
339 * Mark the given slot is locked. The function must be called with
340 * mapping->tree_lock held
342 static inline void *lock_slot(struct address_space
*mapping
, void **slot
)
344 unsigned long entry
= (unsigned long)
345 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
347 entry
|= RADIX_DAX_ENTRY_LOCK
;
348 radix_tree_replace_slot(slot
, (void *)entry
);
349 return (void *)entry
;
353 * Mark the given slot is unlocked. The function must be called with
354 * mapping->tree_lock held
356 static inline void *unlock_slot(struct address_space
*mapping
, void **slot
)
358 unsigned long entry
= (unsigned long)
359 radix_tree_deref_slot_protected(slot
, &mapping
->tree_lock
);
361 entry
&= ~(unsigned long)RADIX_DAX_ENTRY_LOCK
;
362 radix_tree_replace_slot(slot
, (void *)entry
);
363 return (void *)entry
;
367 * Lookup entry in radix tree, wait for it to become unlocked if it is
368 * exceptional entry and return it. The caller must call
369 * put_unlocked_mapping_entry() when he decided not to lock the entry or
370 * put_locked_mapping_entry() when he locked the entry and now wants to
373 * The function must be called with mapping->tree_lock held.
375 static void *get_unlocked_mapping_entry(struct address_space
*mapping
,
376 pgoff_t index
, void ***slotp
)
379 struct wait_exceptional_entry_queue ewait
;
380 wait_queue_head_t
*wq
;
382 init_wait(&ewait
.wait
);
383 ewait
.wait
.func
= wake_exceptional_entry_func
;
386 entry
= __radix_tree_lookup(&mapping
->page_tree
, index
, NULL
,
388 if (!entry
|| !radix_tree_exceptional_entry(entry
) ||
389 !slot_locked(mapping
, slot
)) {
395 wq
= dax_entry_waitqueue(mapping
, index
, entry
, &ewait
.key
);
396 prepare_to_wait_exclusive(wq
, &ewait
.wait
,
397 TASK_UNINTERRUPTIBLE
);
398 spin_unlock_irq(&mapping
->tree_lock
);
400 finish_wait(wq
, &ewait
.wait
);
401 spin_lock_irq(&mapping
->tree_lock
);
405 static void put_locked_mapping_entry(struct address_space
*mapping
,
406 pgoff_t index
, void *entry
)
408 if (!radix_tree_exceptional_entry(entry
)) {
412 dax_unlock_mapping_entry(mapping
, index
);
417 * Called when we are done with radix tree entry we looked up via
418 * get_unlocked_mapping_entry() and which we didn't lock in the end.
420 static void put_unlocked_mapping_entry(struct address_space
*mapping
,
421 pgoff_t index
, void *entry
)
423 if (!radix_tree_exceptional_entry(entry
))
426 /* We have to wake up next waiter for the radix tree entry lock */
427 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, false);
431 * Find radix tree entry at given index. If it points to a page, return with
432 * the page locked. If it points to the exceptional entry, return with the
433 * radix tree entry locked. If the radix tree doesn't contain given index,
434 * create empty exceptional entry for the index and return with it locked.
436 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
437 * either return that locked entry or will return an error. This error will
438 * happen if there are any 4k entries (either zero pages or DAX entries)
439 * within the 2MiB range that we are requesting.
441 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
442 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
443 * insertion will fail if it finds any 4k entries already in the tree, and a
444 * 4k insertion will cause an existing 2MiB entry to be unmapped and
445 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
446 * well as 2MiB empty entries.
448 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
449 * real storage backing them. We will leave these real 2MiB DAX entries in
450 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
452 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
453 * persistent memory the benefit is doubtful. We can add that later if we can
456 static void *grab_mapping_entry(struct address_space
*mapping
, pgoff_t index
,
457 unsigned long size_flag
)
459 bool pmd_downgrade
= false; /* splitting 2MiB entry into 4k entries? */
463 spin_lock_irq(&mapping
->tree_lock
);
464 entry
= get_unlocked_mapping_entry(mapping
, index
, &slot
);
467 if (size_flag
& RADIX_DAX_PMD
) {
468 if (!radix_tree_exceptional_entry(entry
) ||
469 dax_is_pte_entry(entry
)) {
470 put_unlocked_mapping_entry(mapping
, index
,
472 entry
= ERR_PTR(-EEXIST
);
475 } else { /* trying to grab a PTE entry */
476 if (radix_tree_exceptional_entry(entry
) &&
477 dax_is_pmd_entry(entry
) &&
478 (dax_is_zero_entry(entry
) ||
479 dax_is_empty_entry(entry
))) {
480 pmd_downgrade
= true;
485 /* No entry for given index? Make sure radix tree is big enough. */
486 if (!entry
|| pmd_downgrade
) {
491 * Make sure 'entry' remains valid while we drop
492 * mapping->tree_lock.
494 entry
= lock_slot(mapping
, slot
);
497 spin_unlock_irq(&mapping
->tree_lock
);
498 err
= radix_tree_preload(
499 mapping_gfp_mask(mapping
) & ~__GFP_HIGHMEM
);
502 put_locked_mapping_entry(mapping
, index
, entry
);
507 * Besides huge zero pages the only other thing that gets
508 * downgraded are empty entries which don't need to be
511 if (pmd_downgrade
&& dax_is_zero_entry(entry
))
512 unmap_mapping_range(mapping
,
513 (index
<< PAGE_SHIFT
) & PMD_MASK
, PMD_SIZE
, 0);
515 spin_lock_irq(&mapping
->tree_lock
);
518 radix_tree_delete(&mapping
->page_tree
, index
);
519 mapping
->nrexceptional
--;
520 dax_wake_mapping_entry_waiter(mapping
, index
, entry
,
524 entry
= dax_radix_locked_entry(0, size_flag
| RADIX_DAX_EMPTY
);
526 err
= __radix_tree_insert(&mapping
->page_tree
, index
,
527 dax_radix_order(entry
), entry
);
528 radix_tree_preload_end();
530 spin_unlock_irq(&mapping
->tree_lock
);
532 * Someone already created the entry? This is a
533 * normal failure when inserting PMDs in a range
534 * that already contains PTEs. In that case we want
535 * to return -EEXIST immediately.
537 if (err
== -EEXIST
&& !(size_flag
& RADIX_DAX_PMD
))
540 * Our insertion of a DAX PMD entry failed, most
541 * likely because it collided with a PTE sized entry
542 * at a different index in the PMD range. We haven't
543 * inserted anything into the radix tree and have no
548 /* Good, we have inserted empty locked entry into the tree. */
549 mapping
->nrexceptional
++;
550 spin_unlock_irq(&mapping
->tree_lock
);
553 /* Normal page in radix tree? */
554 if (!radix_tree_exceptional_entry(entry
)) {
555 struct page
*page
= entry
;
558 spin_unlock_irq(&mapping
->tree_lock
);
560 /* Page got truncated? Retry... */
561 if (unlikely(page
->mapping
!= mapping
)) {
568 entry
= lock_slot(mapping
, slot
);
570 spin_unlock_irq(&mapping
->tree_lock
);
575 * We do not necessarily hold the mapping->tree_lock when we call this
576 * function so it is possible that 'entry' is no longer a valid item in the
577 * radix tree. This is okay because all we really need to do is to find the
578 * correct waitqueue where tasks might be waiting for that old 'entry' and
581 void dax_wake_mapping_entry_waiter(struct address_space
*mapping
,
582 pgoff_t index
, void *entry
, bool wake_all
)
584 struct exceptional_entry_key key
;
585 wait_queue_head_t
*wq
;
587 wq
= dax_entry_waitqueue(mapping
, index
, entry
, &key
);
590 * Checking for locked entry and prepare_to_wait_exclusive() happens
591 * under mapping->tree_lock, ditto for entry handling in our callers.
592 * So at this point all tasks that could have seen our entry locked
593 * must be in the waitqueue and the following check will see them.
595 if (waitqueue_active(wq
))
596 __wake_up(wq
, TASK_NORMAL
, wake_all
? 0 : 1, &key
);
599 void dax_unlock_mapping_entry(struct address_space
*mapping
, pgoff_t index
)
603 spin_lock_irq(&mapping
->tree_lock
);
604 entry
= __radix_tree_lookup(&mapping
->page_tree
, index
, NULL
, &slot
);
605 if (WARN_ON_ONCE(!entry
|| !radix_tree_exceptional_entry(entry
) ||
606 !slot_locked(mapping
, slot
))) {
607 spin_unlock_irq(&mapping
->tree_lock
);
610 unlock_slot(mapping
, slot
);
611 spin_unlock_irq(&mapping
->tree_lock
);
612 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, false);
616 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
617 * entry to get unlocked before deleting it.
619 int dax_delete_mapping_entry(struct address_space
*mapping
, pgoff_t index
)
623 spin_lock_irq(&mapping
->tree_lock
);
624 entry
= get_unlocked_mapping_entry(mapping
, index
, NULL
);
626 * This gets called from truncate / punch_hole path. As such, the caller
627 * must hold locks protecting against concurrent modifications of the
628 * radix tree (usually fs-private i_mmap_sem for writing). Since the
629 * caller has seen exceptional entry for this index, we better find it
630 * at that index as well...
632 if (WARN_ON_ONCE(!entry
|| !radix_tree_exceptional_entry(entry
))) {
633 spin_unlock_irq(&mapping
->tree_lock
);
636 radix_tree_delete(&mapping
->page_tree
, index
);
637 mapping
->nrexceptional
--;
638 spin_unlock_irq(&mapping
->tree_lock
);
639 dax_wake_mapping_entry_waiter(mapping
, index
, entry
, true);
645 * The user has performed a load from a hole in the file. Allocating
646 * a new page in the file would cause excessive storage usage for
647 * workloads with sparse files. We allocate a page cache page instead.
648 * We'll kick it out of the page cache if it's ever written to,
649 * otherwise it will simply fall out of the page cache under memory
650 * pressure without ever having been dirtied.
652 static int dax_load_hole(struct address_space
*mapping
, void *entry
,
653 struct vm_fault
*vmf
)
657 /* Hole page already exists? Return it... */
658 if (!radix_tree_exceptional_entry(entry
)) {
660 return VM_FAULT_LOCKED
;
663 /* This will replace locked radix tree entry with a hole page */
664 page
= find_or_create_page(mapping
, vmf
->pgoff
,
665 vmf
->gfp_mask
| __GFP_ZERO
);
667 put_locked_mapping_entry(mapping
, vmf
->pgoff
, entry
);
671 return VM_FAULT_LOCKED
;
674 static int copy_user_dax(struct block_device
*bdev
, sector_t sector
, size_t size
,
675 struct page
*to
, unsigned long vaddr
)
677 struct blk_dax_ctl dax
= {
683 if (dax_map_atomic(bdev
, &dax
) < 0)
684 return PTR_ERR(dax
.addr
);
685 vto
= kmap_atomic(to
);
686 copy_user_page(vto
, (void __force
*)dax
.addr
, vaddr
, to
);
688 dax_unmap_atomic(bdev
, &dax
);
693 * By this point grab_mapping_entry() has ensured that we have a locked entry
694 * of the appropriate size so we don't have to worry about downgrading PMDs to
695 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
696 * already in the tree, we will skip the insertion and just dirty the PMD as
699 static void *dax_insert_mapping_entry(struct address_space
*mapping
,
700 struct vm_fault
*vmf
,
701 void *entry
, sector_t sector
,
704 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
706 bool hole_fill
= false;
708 pgoff_t index
= vmf
->pgoff
;
710 if (vmf
->flags
& FAULT_FLAG_WRITE
)
711 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
713 /* Replacing hole page with block mapping? */
714 if (!radix_tree_exceptional_entry(entry
)) {
717 * Unmap the page now before we remove it from page cache below.
718 * The page is locked so it cannot be faulted in again.
720 unmap_mapping_range(mapping
, vmf
->pgoff
<< PAGE_SHIFT
,
722 error
= radix_tree_preload(vmf
->gfp_mask
& ~__GFP_HIGHMEM
);
724 return ERR_PTR(error
);
725 } else if (dax_is_zero_entry(entry
) && !(flags
& RADIX_DAX_HZP
)) {
726 /* replacing huge zero page with PMD block mapping */
727 unmap_mapping_range(mapping
,
728 (vmf
->pgoff
<< PAGE_SHIFT
) & PMD_MASK
, PMD_SIZE
, 0);
731 spin_lock_irq(&mapping
->tree_lock
);
732 new_entry
= dax_radix_locked_entry(sector
, flags
);
735 __delete_from_page_cache(entry
, NULL
);
736 /* Drop pagecache reference */
738 error
= __radix_tree_insert(page_tree
, index
,
739 dax_radix_order(new_entry
), new_entry
);
741 new_entry
= ERR_PTR(error
);
744 mapping
->nrexceptional
++;
745 } else if (dax_is_zero_entry(entry
) || dax_is_empty_entry(entry
)) {
747 * Only swap our new entry into the radix tree if the current
748 * entry is a zero page or an empty entry. If a normal PTE or
749 * PMD entry is already in the tree, we leave it alone. This
750 * means that if we are trying to insert a PTE and the
751 * existing entry is a PMD, we will just leave the PMD in the
752 * tree and dirty it if necessary.
757 ret
= __radix_tree_lookup(page_tree
, index
, NULL
, &slot
);
758 WARN_ON_ONCE(ret
!= entry
);
759 radix_tree_replace_slot(slot
, new_entry
);
761 if (vmf
->flags
& FAULT_FLAG_WRITE
)
762 radix_tree_tag_set(page_tree
, index
, PAGECACHE_TAG_DIRTY
);
764 spin_unlock_irq(&mapping
->tree_lock
);
766 radix_tree_preload_end();
768 * We don't need hole page anymore, it has been replaced with
769 * locked radix tree entry now.
771 if (mapping
->a_ops
->freepage
)
772 mapping
->a_ops
->freepage(entry
);
779 static int dax_writeback_one(struct block_device
*bdev
,
780 struct address_space
*mapping
, pgoff_t index
, void *entry
)
782 struct radix_tree_root
*page_tree
= &mapping
->page_tree
;
783 struct radix_tree_node
*node
;
784 struct blk_dax_ctl dax
;
788 spin_lock_irq(&mapping
->tree_lock
);
790 * Regular page slots are stabilized by the page lock even
791 * without the tree itself locked. These unlocked entries
792 * need verification under the tree lock.
794 if (!__radix_tree_lookup(page_tree
, index
, &node
, &slot
))
799 /* another fsync thread may have already written back this entry */
800 if (!radix_tree_tag_get(page_tree
, index
, PAGECACHE_TAG_TOWRITE
))
803 if (WARN_ON_ONCE(dax_is_empty_entry(entry
) ||
804 dax_is_zero_entry(entry
))) {
810 * Even if dax_writeback_mapping_range() was given a wbc->range_start
811 * in the middle of a PMD, the 'index' we are given will be aligned to
812 * the start index of the PMD, as will the sector we pull from
813 * 'entry'. This allows us to flush for PMD_SIZE and not have to
814 * worry about partial PMD writebacks.
816 dax
.sector
= dax_radix_sector(entry
);
817 dax
.size
= PAGE_SIZE
<< dax_radix_order(entry
);
818 spin_unlock_irq(&mapping
->tree_lock
);
821 * We cannot hold tree_lock while calling dax_map_atomic() because it
822 * eventually calls cond_resched().
824 ret
= dax_map_atomic(bdev
, &dax
);
828 if (WARN_ON_ONCE(ret
< dax
.size
)) {
833 wb_cache_pmem(dax
.addr
, dax
.size
);
835 spin_lock_irq(&mapping
->tree_lock
);
836 radix_tree_tag_clear(page_tree
, index
, PAGECACHE_TAG_TOWRITE
);
837 spin_unlock_irq(&mapping
->tree_lock
);
839 dax_unmap_atomic(bdev
, &dax
);
843 spin_unlock_irq(&mapping
->tree_lock
);
848 * Flush the mapping to the persistent domain within the byte range of [start,
849 * end]. This is required by data integrity operations to ensure file data is
850 * on persistent storage prior to completion of the operation.
852 int dax_writeback_mapping_range(struct address_space
*mapping
,
853 struct block_device
*bdev
, struct writeback_control
*wbc
)
855 struct inode
*inode
= mapping
->host
;
856 pgoff_t start_index
, end_index
;
857 pgoff_t indices
[PAGEVEC_SIZE
];
862 if (WARN_ON_ONCE(inode
->i_blkbits
!= PAGE_SHIFT
))
865 if (!mapping
->nrexceptional
|| wbc
->sync_mode
!= WB_SYNC_ALL
)
868 start_index
= wbc
->range_start
>> PAGE_SHIFT
;
869 end_index
= wbc
->range_end
>> PAGE_SHIFT
;
871 tag_pages_for_writeback(mapping
, start_index
, end_index
);
873 pagevec_init(&pvec
, 0);
875 pvec
.nr
= find_get_entries_tag(mapping
, start_index
,
876 PAGECACHE_TAG_TOWRITE
, PAGEVEC_SIZE
,
877 pvec
.pages
, indices
);
882 for (i
= 0; i
< pvec
.nr
; i
++) {
883 if (indices
[i
] > end_index
) {
888 ret
= dax_writeback_one(bdev
, mapping
, indices
[i
],
896 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range
);
898 static int dax_insert_mapping(struct address_space
*mapping
,
899 struct block_device
*bdev
, sector_t sector
, size_t size
,
900 void **entryp
, struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
902 unsigned long vaddr
= (unsigned long)vmf
->virtual_address
;
903 struct blk_dax_ctl dax
= {
908 void *entry
= *entryp
;
910 if (dax_map_atomic(bdev
, &dax
) < 0)
911 return PTR_ERR(dax
.addr
);
912 dax_unmap_atomic(bdev
, &dax
);
914 ret
= dax_insert_mapping_entry(mapping
, vmf
, entry
, dax
.sector
, 0);
919 return vm_insert_mixed(vma
, vaddr
, dax
.pfn
);
923 * dax_fault - handle a page fault on a DAX file
924 * @vma: The virtual memory area where the fault occurred
925 * @vmf: The description of the fault
926 * @get_block: The filesystem method used to translate file offsets to blocks
928 * When a page fault occurs, filesystems may call this helper in their
929 * fault handler for DAX files. dax_fault() assumes the caller has done all
930 * the necessary locking for the page fault to proceed successfully.
932 int dax_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
,
933 get_block_t get_block
)
935 struct file
*file
= vma
->vm_file
;
936 struct address_space
*mapping
= file
->f_mapping
;
937 struct inode
*inode
= mapping
->host
;
939 struct buffer_head bh
;
940 unsigned long vaddr
= (unsigned long)vmf
->virtual_address
;
941 unsigned blkbits
= inode
->i_blkbits
;
948 * Check whether offset isn't beyond end of file now. Caller is supposed
949 * to hold locks serializing us with truncate / punch hole so this is
952 size
= (i_size_read(inode
) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
953 if (vmf
->pgoff
>= size
)
954 return VM_FAULT_SIGBUS
;
956 memset(&bh
, 0, sizeof(bh
));
957 block
= (sector_t
)vmf
->pgoff
<< (PAGE_SHIFT
- blkbits
);
958 bh
.b_bdev
= inode
->i_sb
->s_bdev
;
959 bh
.b_size
= PAGE_SIZE
;
961 entry
= grab_mapping_entry(mapping
, vmf
->pgoff
, 0);
963 error
= PTR_ERR(entry
);
967 error
= get_block(inode
, block
, &bh
, 0);
968 if (!error
&& (bh
.b_size
< PAGE_SIZE
))
969 error
= -EIO
; /* fs corruption? */
974 struct page
*new_page
= vmf
->cow_page
;
975 if (buffer_written(&bh
))
976 error
= copy_user_dax(bh
.b_bdev
, to_sector(&bh
, inode
),
977 bh
.b_size
, new_page
, vaddr
);
979 clear_user_highpage(new_page
, vaddr
);
982 if (!radix_tree_exceptional_entry(entry
)) {
984 return VM_FAULT_LOCKED
;
987 return VM_FAULT_DAX_LOCKED
;
990 if (!buffer_mapped(&bh
)) {
991 if (vmf
->flags
& FAULT_FLAG_WRITE
) {
992 error
= get_block(inode
, block
, &bh
, 1);
993 count_vm_event(PGMAJFAULT
);
994 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
995 major
= VM_FAULT_MAJOR
;
996 if (!error
&& (bh
.b_size
< PAGE_SIZE
))
1001 return dax_load_hole(mapping
, entry
, vmf
);
1005 /* Filesystem should not return unwritten buffers to us! */
1006 WARN_ON_ONCE(buffer_unwritten(&bh
) || buffer_new(&bh
));
1007 error
= dax_insert_mapping(mapping
, bh
.b_bdev
, to_sector(&bh
, inode
),
1008 bh
.b_size
, &entry
, vma
, vmf
);
1010 put_locked_mapping_entry(mapping
, vmf
->pgoff
, entry
);
1012 if (error
== -ENOMEM
)
1013 return VM_FAULT_OOM
| major
;
1014 /* -EBUSY is fine, somebody else faulted on the same PTE */
1015 if ((error
< 0) && (error
!= -EBUSY
))
1016 return VM_FAULT_SIGBUS
| major
;
1017 return VM_FAULT_NOPAGE
| major
;
1019 EXPORT_SYMBOL_GPL(dax_fault
);
1022 * dax_pfn_mkwrite - handle first write to DAX page
1023 * @vma: The virtual memory area where the fault occurred
1024 * @vmf: The description of the fault
1026 int dax_pfn_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1028 struct file
*file
= vma
->vm_file
;
1029 struct address_space
*mapping
= file
->f_mapping
;
1031 pgoff_t index
= vmf
->pgoff
;
1033 spin_lock_irq(&mapping
->tree_lock
);
1034 entry
= get_unlocked_mapping_entry(mapping
, index
, NULL
);
1035 if (!entry
|| !radix_tree_exceptional_entry(entry
))
1037 radix_tree_tag_set(&mapping
->page_tree
, index
, PAGECACHE_TAG_DIRTY
);
1038 put_unlocked_mapping_entry(mapping
, index
, entry
);
1040 spin_unlock_irq(&mapping
->tree_lock
);
1041 return VM_FAULT_NOPAGE
;
1043 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite
);
1045 static bool dax_range_is_aligned(struct block_device
*bdev
,
1046 unsigned int offset
, unsigned int length
)
1048 unsigned short sector_size
= bdev_logical_block_size(bdev
);
1050 if (!IS_ALIGNED(offset
, sector_size
))
1052 if (!IS_ALIGNED(length
, sector_size
))
1058 int __dax_zero_page_range(struct block_device
*bdev
, sector_t sector
,
1059 unsigned int offset
, unsigned int length
)
1061 struct blk_dax_ctl dax
= {
1066 if (dax_range_is_aligned(bdev
, offset
, length
)) {
1067 sector_t start_sector
= dax
.sector
+ (offset
>> 9);
1069 return blkdev_issue_zeroout(bdev
, start_sector
,
1070 length
>> 9, GFP_NOFS
, true);
1072 if (dax_map_atomic(bdev
, &dax
) < 0)
1073 return PTR_ERR(dax
.addr
);
1074 clear_pmem(dax
.addr
+ offset
, length
);
1075 dax_unmap_atomic(bdev
, &dax
);
1079 EXPORT_SYMBOL_GPL(__dax_zero_page_range
);
1082 * dax_zero_page_range - zero a range within a page of a DAX file
1083 * @inode: The file being truncated
1084 * @from: The file offset that is being truncated to
1085 * @length: The number of bytes to zero
1086 * @get_block: The filesystem method used to translate file offsets to blocks
1088 * This function can be called by a filesystem when it is zeroing part of a
1089 * page in a DAX file. This is intended for hole-punch operations. If
1090 * you are truncating a file, the helper function dax_truncate_page() may be
1093 int dax_zero_page_range(struct inode
*inode
, loff_t from
, unsigned length
,
1094 get_block_t get_block
)
1096 struct buffer_head bh
;
1097 pgoff_t index
= from
>> PAGE_SHIFT
;
1098 unsigned offset
= from
& (PAGE_SIZE
-1);
1101 /* Block boundary? Nothing to do */
1104 if (WARN_ON_ONCE((offset
+ length
) > PAGE_SIZE
))
1107 memset(&bh
, 0, sizeof(bh
));
1108 bh
.b_bdev
= inode
->i_sb
->s_bdev
;
1109 bh
.b_size
= PAGE_SIZE
;
1110 err
= get_block(inode
, index
, &bh
, 0);
1111 if (err
< 0 || !buffer_written(&bh
))
1114 return __dax_zero_page_range(bh
.b_bdev
, to_sector(&bh
, inode
),
1117 EXPORT_SYMBOL_GPL(dax_zero_page_range
);
1120 * dax_truncate_page - handle a partial page being truncated in a DAX file
1121 * @inode: The file being truncated
1122 * @from: The file offset that is being truncated to
1123 * @get_block: The filesystem method used to translate file offsets to blocks
1125 * Similar to block_truncate_page(), this function can be called by a
1126 * filesystem when it is truncating a DAX file to handle the partial page.
1128 int dax_truncate_page(struct inode
*inode
, loff_t from
, get_block_t get_block
)
1130 unsigned length
= PAGE_ALIGN(from
) - from
;
1131 return dax_zero_page_range(inode
, from
, length
, get_block
);
1133 EXPORT_SYMBOL_GPL(dax_truncate_page
);
1135 #ifdef CONFIG_FS_IOMAP
1136 static sector_t
dax_iomap_sector(struct iomap
*iomap
, loff_t pos
)
1138 return iomap
->blkno
+ (((pos
& PAGE_MASK
) - iomap
->offset
) >> 9);
1142 dax_iomap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
1143 struct iomap
*iomap
)
1145 struct iov_iter
*iter
= data
;
1146 loff_t end
= pos
+ length
, done
= 0;
1149 if (iov_iter_rw(iter
) == READ
) {
1150 end
= min(end
, i_size_read(inode
));
1154 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
1155 return iov_iter_zero(min(length
, end
- pos
), iter
);
1158 if (WARN_ON_ONCE(iomap
->type
!= IOMAP_MAPPED
))
1162 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1163 struct blk_dax_ctl dax
= { 0 };
1166 dax
.sector
= dax_iomap_sector(iomap
, pos
);
1167 dax
.size
= (length
+ offset
+ PAGE_SIZE
- 1) & PAGE_MASK
;
1168 map_len
= dax_map_atomic(iomap
->bdev
, &dax
);
1176 if (map_len
> end
- pos
)
1177 map_len
= end
- pos
;
1179 if (iov_iter_rw(iter
) == WRITE
)
1180 map_len
= copy_from_iter_pmem(dax
.addr
, map_len
, iter
);
1182 map_len
= copy_to_iter(dax
.addr
, map_len
, iter
);
1183 dax_unmap_atomic(iomap
->bdev
, &dax
);
1185 ret
= map_len
? map_len
: -EFAULT
;
1194 return done
? done
: ret
;
1198 * dax_iomap_rw - Perform I/O to a DAX file
1199 * @iocb: The control block for this I/O
1200 * @iter: The addresses to do I/O from or to
1201 * @ops: iomap ops passed from the file system
1203 * This function performs read and write operations to directly mapped
1204 * persistent memory. The callers needs to take care of read/write exclusion
1205 * and evicting any page cache pages in the region under I/O.
1208 dax_iomap_rw(struct kiocb
*iocb
, struct iov_iter
*iter
,
1209 struct iomap_ops
*ops
)
1211 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1212 struct inode
*inode
= mapping
->host
;
1213 loff_t pos
= iocb
->ki_pos
, ret
= 0, done
= 0;
1216 if (iov_iter_rw(iter
) == WRITE
)
1217 flags
|= IOMAP_WRITE
;
1220 * Yes, even DAX files can have page cache attached to them: A zeroed
1221 * page is inserted into the pagecache when we have to serve a write
1222 * fault on a hole. It should never be dirtied and can simply be
1223 * dropped from the pagecache once we get real data for the page.
1225 * XXX: This is racy against mmap, and there's nothing we can do about
1226 * it. We'll eventually need to shift this down even further so that
1227 * we can check if we allocated blocks over a hole first.
1229 if (mapping
->nrpages
) {
1230 ret
= invalidate_inode_pages2_range(mapping
,
1232 (pos
+ iov_iter_count(iter
) - 1) >> PAGE_SHIFT
);
1236 while (iov_iter_count(iter
)) {
1237 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
), flags
, ops
,
1238 iter
, dax_iomap_actor
);
1245 iocb
->ki_pos
+= done
;
1246 return done
? done
: ret
;
1248 EXPORT_SYMBOL_GPL(dax_iomap_rw
);
1251 * dax_iomap_fault - handle a page fault on a DAX file
1252 * @vma: The virtual memory area where the fault occurred
1253 * @vmf: The description of the fault
1254 * @ops: iomap ops passed from the file system
1256 * When a page fault occurs, filesystems may call this helper in their fault
1257 * or mkwrite handler for DAX files. Assumes the caller has done all the
1258 * necessary locking for the page fault to proceed successfully.
1260 int dax_iomap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
,
1261 struct iomap_ops
*ops
)
1263 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1264 struct inode
*inode
= mapping
->host
;
1265 unsigned long vaddr
= (unsigned long)vmf
->virtual_address
;
1266 loff_t pos
= (loff_t
)vmf
->pgoff
<< PAGE_SHIFT
;
1268 struct iomap iomap
= { 0 };
1270 int error
, major
= 0;
1271 int locked_status
= 0;
1275 * Check whether offset isn't beyond end of file now. Caller is supposed
1276 * to hold locks serializing us with truncate / punch hole so this is
1279 if (pos
>= i_size_read(inode
))
1280 return VM_FAULT_SIGBUS
;
1282 entry
= grab_mapping_entry(mapping
, vmf
->pgoff
, 0);
1283 if (IS_ERR(entry
)) {
1284 error
= PTR_ERR(entry
);
1288 if ((vmf
->flags
& FAULT_FLAG_WRITE
) && !vmf
->cow_page
)
1289 flags
|= IOMAP_WRITE
;
1292 * Note that we don't bother to use iomap_apply here: DAX required
1293 * the file system block size to be equal the page size, which means
1294 * that we never have to deal with more than a single extent here.
1296 error
= ops
->iomap_begin(inode
, pos
, PAGE_SIZE
, flags
, &iomap
);
1299 if (WARN_ON_ONCE(iomap
.offset
+ iomap
.length
< pos
+ PAGE_SIZE
)) {
1300 error
= -EIO
; /* fs corruption? */
1304 sector
= dax_iomap_sector(&iomap
, pos
);
1306 if (vmf
->cow_page
) {
1307 switch (iomap
.type
) {
1309 case IOMAP_UNWRITTEN
:
1310 clear_user_highpage(vmf
->cow_page
, vaddr
);
1313 error
= copy_user_dax(iomap
.bdev
, sector
, PAGE_SIZE
,
1314 vmf
->cow_page
, vaddr
);
1324 if (!radix_tree_exceptional_entry(entry
)) {
1326 locked_status
= VM_FAULT_LOCKED
;
1329 locked_status
= VM_FAULT_DAX_LOCKED
;
1334 switch (iomap
.type
) {
1336 if (iomap
.flags
& IOMAP_F_NEW
) {
1337 count_vm_event(PGMAJFAULT
);
1338 mem_cgroup_count_vm_event(vma
->vm_mm
, PGMAJFAULT
);
1339 major
= VM_FAULT_MAJOR
;
1341 error
= dax_insert_mapping(mapping
, iomap
.bdev
, sector
,
1342 PAGE_SIZE
, &entry
, vma
, vmf
);
1344 case IOMAP_UNWRITTEN
:
1346 if (!(vmf
->flags
& FAULT_FLAG_WRITE
)) {
1347 locked_status
= dax_load_hole(mapping
, entry
, vmf
);
1358 if (ops
->iomap_end
) {
1360 /* keep previous error */
1361 ops
->iomap_end(inode
, pos
, PAGE_SIZE
, 0, flags
,
1364 error
= ops
->iomap_end(inode
, pos
, PAGE_SIZE
,
1365 PAGE_SIZE
, flags
, &iomap
);
1369 if (!locked_status
|| error
)
1370 put_locked_mapping_entry(mapping
, vmf
->pgoff
, entry
);
1372 if (error
== -ENOMEM
)
1373 return VM_FAULT_OOM
| major
;
1374 /* -EBUSY is fine, somebody else faulted on the same PTE */
1375 if (error
< 0 && error
!= -EBUSY
)
1376 return VM_FAULT_SIGBUS
| major
;
1377 if (locked_status
) {
1378 WARN_ON_ONCE(error
); /* -EBUSY from ops->iomap_end? */
1379 return locked_status
;
1381 return VM_FAULT_NOPAGE
| major
;
1383 EXPORT_SYMBOL_GPL(dax_iomap_fault
);
1385 #ifdef CONFIG_FS_DAX_PMD
1387 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1388 * more often than one might expect in the below functions.
1390 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1392 static int dax_pmd_insert_mapping(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1393 struct vm_fault
*vmf
, unsigned long address
,
1394 struct iomap
*iomap
, loff_t pos
, bool write
, void **entryp
)
1396 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1397 struct block_device
*bdev
= iomap
->bdev
;
1398 struct blk_dax_ctl dax
= {
1399 .sector
= dax_iomap_sector(iomap
, pos
),
1402 long length
= dax_map_atomic(bdev
, &dax
);
1405 if (length
< 0) /* dax_map_atomic() failed */
1406 return VM_FAULT_FALLBACK
;
1407 if (length
< PMD_SIZE
)
1408 goto unmap_fallback
;
1409 if (pfn_t_to_pfn(dax
.pfn
) & PG_PMD_COLOUR
)
1410 goto unmap_fallback
;
1411 if (!pfn_t_devmap(dax
.pfn
))
1412 goto unmap_fallback
;
1414 dax_unmap_atomic(bdev
, &dax
);
1416 ret
= dax_insert_mapping_entry(mapping
, vmf
, *entryp
, dax
.sector
,
1419 return VM_FAULT_FALLBACK
;
1422 return vmf_insert_pfn_pmd(vma
, address
, pmd
, dax
.pfn
, write
);
1425 dax_unmap_atomic(bdev
, &dax
);
1426 return VM_FAULT_FALLBACK
;
1429 static int dax_pmd_load_hole(struct vm_area_struct
*vma
, pmd_t
*pmd
,
1430 struct vm_fault
*vmf
, unsigned long address
,
1431 struct iomap
*iomap
, void **entryp
)
1433 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1434 unsigned long pmd_addr
= address
& PMD_MASK
;
1435 struct page
*zero_page
;
1440 zero_page
= mm_get_huge_zero_page(vma
->vm_mm
);
1442 if (unlikely(!zero_page
))
1443 return VM_FAULT_FALLBACK
;
1445 ret
= dax_insert_mapping_entry(mapping
, vmf
, *entryp
, 0,
1446 RADIX_DAX_PMD
| RADIX_DAX_HZP
);
1448 return VM_FAULT_FALLBACK
;
1451 ptl
= pmd_lock(vma
->vm_mm
, pmd
);
1452 if (!pmd_none(*pmd
)) {
1454 return VM_FAULT_FALLBACK
;
1457 pmd_entry
= mk_pmd(zero_page
, vma
->vm_page_prot
);
1458 pmd_entry
= pmd_mkhuge(pmd_entry
);
1459 set_pmd_at(vma
->vm_mm
, pmd_addr
, pmd
, pmd_entry
);
1461 return VM_FAULT_NOPAGE
;
1464 int dax_iomap_pmd_fault(struct vm_area_struct
*vma
, unsigned long address
,
1465 pmd_t
*pmd
, unsigned int flags
, struct iomap_ops
*ops
)
1467 struct address_space
*mapping
= vma
->vm_file
->f_mapping
;
1468 unsigned long pmd_addr
= address
& PMD_MASK
;
1469 bool write
= flags
& FAULT_FLAG_WRITE
;
1470 unsigned int iomap_flags
= write
? IOMAP_WRITE
: 0;
1471 struct inode
*inode
= mapping
->host
;
1472 int result
= VM_FAULT_FALLBACK
;
1473 struct iomap iomap
= { 0 };
1474 pgoff_t max_pgoff
, pgoff
;
1475 struct vm_fault vmf
;
1480 /* Fall back to PTEs if we're going to COW */
1481 if (write
&& !(vma
->vm_flags
& VM_SHARED
))
1484 /* If the PMD would extend outside the VMA */
1485 if (pmd_addr
< vma
->vm_start
)
1487 if ((pmd_addr
+ PMD_SIZE
) > vma
->vm_end
)
1491 * Check whether offset isn't beyond end of file now. Caller is
1492 * supposed to hold locks serializing us with truncate / punch hole so
1493 * this is a reliable test.
1495 pgoff
= linear_page_index(vma
, pmd_addr
);
1496 max_pgoff
= (i_size_read(inode
) - 1) >> PAGE_SHIFT
;
1498 if (pgoff
> max_pgoff
)
1499 return VM_FAULT_SIGBUS
;
1501 /* If the PMD would extend beyond the file size */
1502 if ((pgoff
| PG_PMD_COLOUR
) > max_pgoff
)
1506 * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
1507 * PMD or a HZP entry. If it can't (because a 4k page is already in
1508 * the tree, for instance), it will return -EEXIST and we just fall
1509 * back to 4k entries.
1511 entry
= grab_mapping_entry(mapping
, pgoff
, RADIX_DAX_PMD
);
1516 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1517 * setting up a mapping, so really we're using iomap_begin() as a way
1518 * to look up our filesystem block.
1520 pos
= (loff_t
)pgoff
<< PAGE_SHIFT
;
1521 error
= ops
->iomap_begin(inode
, pos
, PMD_SIZE
, iomap_flags
, &iomap
);
1524 if (iomap
.offset
+ iomap
.length
< pos
+ PMD_SIZE
)
1529 vmf
.gfp_mask
= mapping_gfp_mask(mapping
) | __GFP_IO
;
1531 switch (iomap
.type
) {
1533 result
= dax_pmd_insert_mapping(vma
, pmd
, &vmf
, address
,
1534 &iomap
, pos
, write
, &entry
);
1536 case IOMAP_UNWRITTEN
:
1538 if (WARN_ON_ONCE(write
))
1540 result
= dax_pmd_load_hole(vma
, pmd
, &vmf
, address
, &iomap
,
1549 if (ops
->iomap_end
) {
1550 if (result
== VM_FAULT_FALLBACK
) {
1551 ops
->iomap_end(inode
, pos
, PMD_SIZE
, 0, iomap_flags
,
1554 error
= ops
->iomap_end(inode
, pos
, PMD_SIZE
, PMD_SIZE
,
1555 iomap_flags
, &iomap
);
1557 result
= VM_FAULT_FALLBACK
;
1561 put_locked_mapping_entry(mapping
, pgoff
, entry
);
1563 if (result
== VM_FAULT_FALLBACK
) {
1564 split_huge_pmd(vma
, pmd
, address
);
1565 count_vm_event(THP_FAULT_FALLBACK
);
1569 EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault
);
1570 #endif /* CONFIG_FS_DAX_PMD */
1571 #endif /* CONFIG_FS_IOMAP */