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>
38 * We use lowest available bit in exceptional entry for locking, other two
39 * bits to determine entry type. In total 3 special bits.
41 #define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 3)
42 #define RADIX_DAX_PTE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1))
43 #define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2))
44 #define RADIX_DAX_TYPE_MASK (RADIX_DAX_PTE | RADIX_DAX_PMD)
45 #define RADIX_DAX_TYPE(entry) ((unsigned long)entry & RADIX_DAX_TYPE_MASK)
46 #define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT))
47 #define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \
48 RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE) | \
49 RADIX_TREE_EXCEPTIONAL_ENTRY))
51 /* We choose 4096 entries - same as per-zone page wait tables */
52 #define DAX_WAIT_TABLE_BITS 12
53 #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
55 static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
57 static int __init init_dax_wait_table(void)
61 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
62 init_waitqueue_head(wait_table + i);
65 fs_initcall(init_dax_wait_table);
67 static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
69 struct request_queue *q = bdev->bd_queue;
72 dax->addr = ERR_PTR(-EIO);
73 if (blk_queue_enter(q, true) != 0)
76 rc = bdev_direct_access(bdev, dax);
78 dax->addr = ERR_PTR(rc);
85 static void dax_unmap_atomic(struct block_device *bdev,
86 const struct blk_dax_ctl *dax)
88 if (IS_ERR(dax->addr))
90 blk_queue_exit(bdev->bd_queue);
93 struct page *read_dax_sector(struct block_device *bdev, sector_t n)
95 struct page *page = alloc_pages(GFP_KERNEL, 0);
96 struct blk_dax_ctl dax = {
98 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
103 return ERR_PTR(-ENOMEM);
105 rc = dax_map_atomic(bdev, &dax);
108 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
109 dax_unmap_atomic(bdev, &dax);
113 static bool buffer_written(struct buffer_head *bh)
115 return buffer_mapped(bh) && !buffer_unwritten(bh);
118 static sector_t to_sector(const struct buffer_head *bh,
119 const struct inode *inode)
121 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
126 static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
127 loff_t start, loff_t end, get_block_t get_block,
128 struct buffer_head *bh)
130 loff_t pos = start, max = start, bh_max = start;
132 struct block_device *bdev = NULL;
133 int rw = iov_iter_rw(iter), rc;
135 struct blk_dax_ctl dax = {
136 .addr = ERR_PTR(-EIO),
138 unsigned blkbits = inode->i_blkbits;
139 sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1)
143 end = min(end, i_size_read(inode));
148 long page = pos >> PAGE_SHIFT;
149 sector_t block = page << (PAGE_SHIFT - blkbits);
150 unsigned first = pos - (block << blkbits);
154 bh->b_size = PAGE_ALIGN(end - pos);
156 rc = get_block(inode, block, bh, rw == WRITE);
159 bh_max = pos - first + bh->b_size;
162 * We allow uninitialized buffers for writes
163 * beyond EOF as those cannot race with faults
166 (buffer_new(bh) && block < file_blks) ||
167 (rw == WRITE && buffer_unwritten(bh)));
169 unsigned done = bh->b_size -
170 (bh_max - (pos - first));
171 bh->b_blocknr += done >> blkbits;
175 hole = rw == READ && !buffer_written(bh);
177 size = bh->b_size - first;
179 dax_unmap_atomic(bdev, &dax);
180 dax.sector = to_sector(bh, inode);
181 dax.size = bh->b_size;
182 map_len = dax_map_atomic(bdev, &dax);
188 size = map_len - first;
191 * pos + size is one past the last offset for IO,
192 * so pos + size can overflow loff_t at extreme offsets.
193 * Cast to u64 to catch this and get the true minimum.
195 max = min_t(u64, pos + size, end);
198 if (iov_iter_rw(iter) == WRITE) {
199 len = copy_from_iter_pmem(dax.addr, max - pos, iter);
201 len = copy_to_iter((void __force *) dax.addr, max - pos,
204 len = iov_iter_zero(max - pos, iter);
212 if (!IS_ERR(dax.addr))
216 dax_unmap_atomic(bdev, &dax);
218 return (pos == start) ? rc : pos - start;
222 * dax_do_io - Perform I/O to a DAX file
223 * @iocb: The control block for this I/O
224 * @inode: The file which the I/O is directed at
225 * @iter: The addresses to do I/O from or to
226 * @get_block: The filesystem method used to translate file offsets to blocks
227 * @end_io: A filesystem callback for I/O completion
230 * This function uses the same locking scheme as do_blockdev_direct_IO:
231 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
232 * caller for writes. For reads, we take and release the i_mutex ourselves.
233 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
234 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
237 ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
238 struct iov_iter *iter, get_block_t get_block,
239 dio_iodone_t end_io, int flags)
241 struct buffer_head bh;
242 ssize_t retval = -EINVAL;
243 loff_t pos = iocb->ki_pos;
244 loff_t end = pos + iov_iter_count(iter);
246 memset(&bh, 0, sizeof(bh));
247 bh.b_bdev = inode->i_sb->s_bdev;
249 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
252 /* Protects against truncate */
253 if (!(flags & DIO_SKIP_DIO_COUNT))
254 inode_dio_begin(inode);
256 retval = dax_io(inode, iter, pos, end, get_block, &bh);
258 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
264 err = end_io(iocb, pos, retval, bh.b_private);
269 if (!(flags & DIO_SKIP_DIO_COUNT))
270 inode_dio_end(inode);
273 EXPORT_SYMBOL_GPL(dax_do_io);
276 * DAX radix tree locking
278 struct exceptional_entry_key {
279 struct address_space *mapping;
283 struct wait_exceptional_entry_queue {
285 struct exceptional_entry_key key;
288 static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
289 pgoff_t index, void *entry, struct exceptional_entry_key *key)
294 * If 'entry' is a PMD, align the 'index' that we use for the wait
295 * queue to the start of that PMD. This ensures that all offsets in
296 * the range covered by the PMD map to the same bit lock.
298 if (RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
299 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
301 key->mapping = mapping;
302 key->entry_start = index;
304 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
305 return wait_table + hash;
308 static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
309 int sync, void *keyp)
311 struct exceptional_entry_key *key = keyp;
312 struct wait_exceptional_entry_queue *ewait =
313 container_of(wait, struct wait_exceptional_entry_queue, wait);
315 if (key->mapping != ewait->key.mapping ||
316 key->entry_start != ewait->key.entry_start)
318 return autoremove_wake_function(wait, mode, sync, NULL);
322 * Check whether the given slot is locked. The function must be called with
323 * mapping->tree_lock held
325 static inline int slot_locked(struct address_space *mapping, void **slot)
327 unsigned long entry = (unsigned long)
328 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
329 return entry & RADIX_DAX_ENTRY_LOCK;
333 * Mark the given slot is locked. The function must be called with
334 * mapping->tree_lock held
336 static inline void *lock_slot(struct address_space *mapping, void **slot)
338 unsigned long entry = (unsigned long)
339 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
341 entry |= RADIX_DAX_ENTRY_LOCK;
342 radix_tree_replace_slot(slot, (void *)entry);
343 return (void *)entry;
347 * Mark the given slot is unlocked. The function must be called with
348 * mapping->tree_lock held
350 static inline void *unlock_slot(struct address_space *mapping, void **slot)
352 unsigned long entry = (unsigned long)
353 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
355 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
356 radix_tree_replace_slot(slot, (void *)entry);
357 return (void *)entry;
361 * Lookup entry in radix tree, wait for it to become unlocked if it is
362 * exceptional entry and return it. The caller must call
363 * put_unlocked_mapping_entry() when he decided not to lock the entry or
364 * put_locked_mapping_entry() when he locked the entry and now wants to
367 * The function must be called with mapping->tree_lock held.
369 static void *get_unlocked_mapping_entry(struct address_space *mapping,
370 pgoff_t index, void ***slotp)
373 struct wait_exceptional_entry_queue ewait;
374 wait_queue_head_t *wq;
376 init_wait(&ewait.wait);
377 ewait.wait.func = wake_exceptional_entry_func;
380 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
382 if (!entry || !radix_tree_exceptional_entry(entry) ||
383 !slot_locked(mapping, slot)) {
389 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
390 prepare_to_wait_exclusive(wq, &ewait.wait,
391 TASK_UNINTERRUPTIBLE);
392 spin_unlock_irq(&mapping->tree_lock);
394 finish_wait(wq, &ewait.wait);
395 spin_lock_irq(&mapping->tree_lock);
400 * Find radix tree entry at given index. If it points to a page, return with
401 * the page locked. If it points to the exceptional entry, return with the
402 * radix tree entry locked. If the radix tree doesn't contain given index,
403 * create empty exceptional entry for the index and return with it locked.
405 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
406 * persistent memory the benefit is doubtful. We can add that later if we can
409 static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index)
414 spin_lock_irq(&mapping->tree_lock);
415 entry = get_unlocked_mapping_entry(mapping, index, &slot);
416 /* No entry for given index? Make sure radix tree is big enough. */
420 spin_unlock_irq(&mapping->tree_lock);
421 err = radix_tree_preload(
422 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
425 entry = (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY |
426 RADIX_DAX_ENTRY_LOCK);
427 spin_lock_irq(&mapping->tree_lock);
428 err = radix_tree_insert(&mapping->page_tree, index, entry);
429 radix_tree_preload_end();
431 spin_unlock_irq(&mapping->tree_lock);
432 /* Someone already created the entry? */
437 /* Good, we have inserted empty locked entry into the tree. */
438 mapping->nrexceptional++;
439 spin_unlock_irq(&mapping->tree_lock);
442 /* Normal page in radix tree? */
443 if (!radix_tree_exceptional_entry(entry)) {
444 struct page *page = entry;
447 spin_unlock_irq(&mapping->tree_lock);
449 /* Page got truncated? Retry... */
450 if (unlikely(page->mapping != mapping)) {
457 entry = lock_slot(mapping, slot);
458 spin_unlock_irq(&mapping->tree_lock);
463 * We do not necessarily hold the mapping->tree_lock when we call this
464 * function so it is possible that 'entry' is no longer a valid item in the
465 * radix tree. This is okay, though, because all we really need to do is to
466 * find the correct waitqueue where tasks might be sleeping waiting for that
467 * old 'entry' and wake them.
469 void dax_wake_mapping_entry_waiter(struct address_space *mapping,
470 pgoff_t index, void *entry, bool wake_all)
472 struct exceptional_entry_key key;
473 wait_queue_head_t *wq;
475 wq = dax_entry_waitqueue(mapping, index, entry, &key);
478 * Checking for locked entry and prepare_to_wait_exclusive() happens
479 * under mapping->tree_lock, ditto for entry handling in our callers.
480 * So at this point all tasks that could have seen our entry locked
481 * must be in the waitqueue and the following check will see them.
483 if (waitqueue_active(wq))
484 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
487 void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
491 spin_lock_irq(&mapping->tree_lock);
492 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
493 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
494 !slot_locked(mapping, slot))) {
495 spin_unlock_irq(&mapping->tree_lock);
498 unlock_slot(mapping, slot);
499 spin_unlock_irq(&mapping->tree_lock);
500 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
503 static void put_locked_mapping_entry(struct address_space *mapping,
504 pgoff_t index, void *entry)
506 if (!radix_tree_exceptional_entry(entry)) {
510 dax_unlock_mapping_entry(mapping, index);
515 * Called when we are done with radix tree entry we looked up via
516 * get_unlocked_mapping_entry() and which we didn't lock in the end.
518 static void put_unlocked_mapping_entry(struct address_space *mapping,
519 pgoff_t index, void *entry)
521 if (!radix_tree_exceptional_entry(entry))
524 /* We have to wake up next waiter for the radix tree entry lock */
525 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
529 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
530 * entry to get unlocked before deleting it.
532 int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
536 spin_lock_irq(&mapping->tree_lock);
537 entry = get_unlocked_mapping_entry(mapping, index, NULL);
539 * This gets called from truncate / punch_hole path. As such, the caller
540 * must hold locks protecting against concurrent modifications of the
541 * radix tree (usually fs-private i_mmap_sem for writing). Since the
542 * caller has seen exceptional entry for this index, we better find it
543 * at that index as well...
545 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
546 spin_unlock_irq(&mapping->tree_lock);
549 radix_tree_delete(&mapping->page_tree, index);
550 mapping->nrexceptional--;
551 spin_unlock_irq(&mapping->tree_lock);
552 dax_wake_mapping_entry_waiter(mapping, index, entry, true);
558 * The user has performed a load from a hole in the file. Allocating
559 * a new page in the file would cause excessive storage usage for
560 * workloads with sparse files. We allocate a page cache page instead.
561 * We'll kick it out of the page cache if it's ever written to,
562 * otherwise it will simply fall out of the page cache under memory
563 * pressure without ever having been dirtied.
565 static int dax_load_hole(struct address_space *mapping, void *entry,
566 struct vm_fault *vmf)
570 /* Hole page already exists? Return it... */
571 if (!radix_tree_exceptional_entry(entry)) {
573 return VM_FAULT_LOCKED;
576 /* This will replace locked radix tree entry with a hole page */
577 page = find_or_create_page(mapping, vmf->pgoff,
578 vmf->gfp_mask | __GFP_ZERO);
580 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
584 return VM_FAULT_LOCKED;
587 static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
588 struct page *to, unsigned long vaddr)
590 struct blk_dax_ctl dax = {
596 if (dax_map_atomic(bdev, &dax) < 0)
597 return PTR_ERR(dax.addr);
598 vto = kmap_atomic(to);
599 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
601 dax_unmap_atomic(bdev, &dax);
605 #define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
607 static void *dax_insert_mapping_entry(struct address_space *mapping,
608 struct vm_fault *vmf,
609 void *entry, sector_t sector)
611 struct radix_tree_root *page_tree = &mapping->page_tree;
613 bool hole_fill = false;
615 pgoff_t index = vmf->pgoff;
617 if (vmf->flags & FAULT_FLAG_WRITE)
618 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
620 /* Replacing hole page with block mapping? */
621 if (!radix_tree_exceptional_entry(entry)) {
624 * Unmap the page now before we remove it from page cache below.
625 * The page is locked so it cannot be faulted in again.
627 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
629 error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
631 return ERR_PTR(error);
634 spin_lock_irq(&mapping->tree_lock);
635 new_entry = (void *)((unsigned long)RADIX_DAX_ENTRY(sector, false) |
636 RADIX_DAX_ENTRY_LOCK);
638 __delete_from_page_cache(entry, NULL);
639 /* Drop pagecache reference */
641 error = radix_tree_insert(page_tree, index, new_entry);
643 new_entry = ERR_PTR(error);
646 mapping->nrexceptional++;
651 ret = __radix_tree_lookup(page_tree, index, NULL, &slot);
652 WARN_ON_ONCE(ret != entry);
653 radix_tree_replace_slot(slot, new_entry);
655 if (vmf->flags & FAULT_FLAG_WRITE)
656 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
658 spin_unlock_irq(&mapping->tree_lock);
660 radix_tree_preload_end();
662 * We don't need hole page anymore, it has been replaced with
663 * locked radix tree entry now.
665 if (mapping->a_ops->freepage)
666 mapping->a_ops->freepage(entry);
673 static int dax_writeback_one(struct block_device *bdev,
674 struct address_space *mapping, pgoff_t index, void *entry)
676 struct radix_tree_root *page_tree = &mapping->page_tree;
677 int type = RADIX_DAX_TYPE(entry);
678 struct radix_tree_node *node;
679 struct blk_dax_ctl dax;
683 spin_lock_irq(&mapping->tree_lock);
685 * Regular page slots are stabilized by the page lock even
686 * without the tree itself locked. These unlocked entries
687 * need verification under the tree lock.
689 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
694 /* another fsync thread may have already written back this entry */
695 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
698 if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
703 dax.sector = RADIX_DAX_SECTOR(entry);
704 dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
705 spin_unlock_irq(&mapping->tree_lock);
708 * We cannot hold tree_lock while calling dax_map_atomic() because it
709 * eventually calls cond_resched().
711 ret = dax_map_atomic(bdev, &dax);
715 if (WARN_ON_ONCE(ret < dax.size)) {
720 wb_cache_pmem(dax.addr, dax.size);
722 spin_lock_irq(&mapping->tree_lock);
723 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
724 spin_unlock_irq(&mapping->tree_lock);
726 dax_unmap_atomic(bdev, &dax);
730 spin_unlock_irq(&mapping->tree_lock);
735 * Flush the mapping to the persistent domain within the byte range of [start,
736 * end]. This is required by data integrity operations to ensure file data is
737 * on persistent storage prior to completion of the operation.
739 int dax_writeback_mapping_range(struct address_space *mapping,
740 struct block_device *bdev, struct writeback_control *wbc)
742 struct inode *inode = mapping->host;
743 pgoff_t start_index, end_index, pmd_index;
744 pgoff_t indices[PAGEVEC_SIZE];
750 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
753 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
756 start_index = wbc->range_start >> PAGE_SHIFT;
757 end_index = wbc->range_end >> PAGE_SHIFT;
758 pmd_index = DAX_PMD_INDEX(start_index);
761 entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
764 /* see if the start of our range is covered by a PMD entry */
765 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
766 start_index = pmd_index;
768 tag_pages_for_writeback(mapping, start_index, end_index);
770 pagevec_init(&pvec, 0);
772 pvec.nr = find_get_entries_tag(mapping, start_index,
773 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
774 pvec.pages, indices);
779 for (i = 0; i < pvec.nr; i++) {
780 if (indices[i] > end_index) {
785 ret = dax_writeback_one(bdev, mapping, indices[i],
793 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
795 static int dax_insert_mapping(struct address_space *mapping,
796 struct block_device *bdev, sector_t sector, size_t size,
797 void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
799 unsigned long vaddr = (unsigned long)vmf->virtual_address;
800 struct blk_dax_ctl dax = {
805 void *entry = *entryp;
807 if (dax_map_atomic(bdev, &dax) < 0)
808 return PTR_ERR(dax.addr);
809 dax_unmap_atomic(bdev, &dax);
811 ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector);
816 return vm_insert_mixed(vma, vaddr, dax.pfn);
820 * dax_fault - handle a page fault on a DAX file
821 * @vma: The virtual memory area where the fault occurred
822 * @vmf: The description of the fault
823 * @get_block: The filesystem method used to translate file offsets to blocks
825 * When a page fault occurs, filesystems may call this helper in their
826 * fault handler for DAX files. dax_fault() assumes the caller has done all
827 * the necessary locking for the page fault to proceed successfully.
829 int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
830 get_block_t get_block)
832 struct file *file = vma->vm_file;
833 struct address_space *mapping = file->f_mapping;
834 struct inode *inode = mapping->host;
836 struct buffer_head bh;
837 unsigned long vaddr = (unsigned long)vmf->virtual_address;
838 unsigned blkbits = inode->i_blkbits;
845 * Check whether offset isn't beyond end of file now. Caller is supposed
846 * to hold locks serializing us with truncate / punch hole so this is
849 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
850 if (vmf->pgoff >= size)
851 return VM_FAULT_SIGBUS;
853 memset(&bh, 0, sizeof(bh));
854 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
855 bh.b_bdev = inode->i_sb->s_bdev;
856 bh.b_size = PAGE_SIZE;
858 entry = grab_mapping_entry(mapping, vmf->pgoff);
860 error = PTR_ERR(entry);
864 error = get_block(inode, block, &bh, 0);
865 if (!error && (bh.b_size < PAGE_SIZE))
866 error = -EIO; /* fs corruption? */
871 struct page *new_page = vmf->cow_page;
872 if (buffer_written(&bh))
873 error = copy_user_dax(bh.b_bdev, to_sector(&bh, inode),
874 bh.b_size, new_page, vaddr);
876 clear_user_highpage(new_page, vaddr);
879 if (!radix_tree_exceptional_entry(entry)) {
881 return VM_FAULT_LOCKED;
884 return VM_FAULT_DAX_LOCKED;
887 if (!buffer_mapped(&bh)) {
888 if (vmf->flags & FAULT_FLAG_WRITE) {
889 error = get_block(inode, block, &bh, 1);
890 count_vm_event(PGMAJFAULT);
891 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
892 major = VM_FAULT_MAJOR;
893 if (!error && (bh.b_size < PAGE_SIZE))
898 return dax_load_hole(mapping, entry, vmf);
902 /* Filesystem should not return unwritten buffers to us! */
903 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
904 error = dax_insert_mapping(mapping, bh.b_bdev, to_sector(&bh, inode),
905 bh.b_size, &entry, vma, vmf);
907 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
909 if (error == -ENOMEM)
910 return VM_FAULT_OOM | major;
911 /* -EBUSY is fine, somebody else faulted on the same PTE */
912 if ((error < 0) && (error != -EBUSY))
913 return VM_FAULT_SIGBUS | major;
914 return VM_FAULT_NOPAGE | major;
916 EXPORT_SYMBOL_GPL(dax_fault);
919 * dax_pfn_mkwrite - handle first write to DAX page
920 * @vma: The virtual memory area where the fault occurred
921 * @vmf: The description of the fault
923 int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
925 struct file *file = vma->vm_file;
926 struct address_space *mapping = file->f_mapping;
928 pgoff_t index = vmf->pgoff;
930 spin_lock_irq(&mapping->tree_lock);
931 entry = get_unlocked_mapping_entry(mapping, index, NULL);
932 if (!entry || !radix_tree_exceptional_entry(entry))
934 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
935 put_unlocked_mapping_entry(mapping, index, entry);
937 spin_unlock_irq(&mapping->tree_lock);
938 return VM_FAULT_NOPAGE;
940 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
942 static bool dax_range_is_aligned(struct block_device *bdev,
943 unsigned int offset, unsigned int length)
945 unsigned short sector_size = bdev_logical_block_size(bdev);
947 if (!IS_ALIGNED(offset, sector_size))
949 if (!IS_ALIGNED(length, sector_size))
955 int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
956 unsigned int offset, unsigned int length)
958 struct blk_dax_ctl dax = {
963 if (dax_range_is_aligned(bdev, offset, length)) {
964 sector_t start_sector = dax.sector + (offset >> 9);
966 return blkdev_issue_zeroout(bdev, start_sector,
967 length >> 9, GFP_NOFS, true);
969 if (dax_map_atomic(bdev, &dax) < 0)
970 return PTR_ERR(dax.addr);
971 clear_pmem(dax.addr + offset, length);
972 dax_unmap_atomic(bdev, &dax);
976 EXPORT_SYMBOL_GPL(__dax_zero_page_range);
979 * dax_zero_page_range - zero a range within a page of a DAX file
980 * @inode: The file being truncated
981 * @from: The file offset that is being truncated to
982 * @length: The number of bytes to zero
983 * @get_block: The filesystem method used to translate file offsets to blocks
985 * This function can be called by a filesystem when it is zeroing part of a
986 * page in a DAX file. This is intended for hole-punch operations. If
987 * you are truncating a file, the helper function dax_truncate_page() may be
990 int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
991 get_block_t get_block)
993 struct buffer_head bh;
994 pgoff_t index = from >> PAGE_SHIFT;
995 unsigned offset = from & (PAGE_SIZE-1);
998 /* Block boundary? Nothing to do */
1001 if (WARN_ON_ONCE((offset + length) > PAGE_SIZE))
1004 memset(&bh, 0, sizeof(bh));
1005 bh.b_bdev = inode->i_sb->s_bdev;
1006 bh.b_size = PAGE_SIZE;
1007 err = get_block(inode, index, &bh, 0);
1008 if (err < 0 || !buffer_written(&bh))
1011 return __dax_zero_page_range(bh.b_bdev, to_sector(&bh, inode),
1014 EXPORT_SYMBOL_GPL(dax_zero_page_range);
1017 * dax_truncate_page - handle a partial page being truncated in a DAX file
1018 * @inode: The file being truncated
1019 * @from: The file offset that is being truncated to
1020 * @get_block: The filesystem method used to translate file offsets to blocks
1022 * Similar to block_truncate_page(), this function can be called by a
1023 * filesystem when it is truncating a DAX file to handle the partial page.
1025 int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1027 unsigned length = PAGE_ALIGN(from) - from;
1028 return dax_zero_page_range(inode, from, length, get_block);
1030 EXPORT_SYMBOL_GPL(dax_truncate_page);
1032 #ifdef CONFIG_FS_IOMAP
1034 dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1035 struct iomap *iomap)
1037 struct iov_iter *iter = data;
1038 loff_t end = pos + length, done = 0;
1041 if (iov_iter_rw(iter) == READ) {
1042 end = min(end, i_size_read(inode));
1046 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1047 return iov_iter_zero(min(length, end - pos), iter);
1050 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1054 unsigned offset = pos & (PAGE_SIZE - 1);
1055 struct blk_dax_ctl dax = { 0 };
1058 dax.sector = iomap->blkno +
1059 (((pos & PAGE_MASK) - iomap->offset) >> 9);
1060 dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
1061 map_len = dax_map_atomic(iomap->bdev, &dax);
1069 if (map_len > end - pos)
1070 map_len = end - pos;
1072 if (iov_iter_rw(iter) == WRITE)
1073 map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
1075 map_len = copy_to_iter(dax.addr, map_len, iter);
1076 dax_unmap_atomic(iomap->bdev, &dax);
1078 ret = map_len ? map_len : -EFAULT;
1087 return done ? done : ret;
1091 * dax_iomap_rw - Perform I/O to a DAX file
1092 * @iocb: The control block for this I/O
1093 * @iter: The addresses to do I/O from or to
1094 * @ops: iomap ops passed from the file system
1096 * This function performs read and write operations to directly mapped
1097 * persistent memory. The callers needs to take care of read/write exclusion
1098 * and evicting any page cache pages in the region under I/O.
1101 dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
1102 struct iomap_ops *ops)
1104 struct address_space *mapping = iocb->ki_filp->f_mapping;
1105 struct inode *inode = mapping->host;
1106 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1109 if (iov_iter_rw(iter) == WRITE)
1110 flags |= IOMAP_WRITE;
1113 * Yes, even DAX files can have page cache attached to them: A zeroed
1114 * page is inserted into the pagecache when we have to serve a write
1115 * fault on a hole. It should never be dirtied and can simply be
1116 * dropped from the pagecache once we get real data for the page.
1118 * XXX: This is racy against mmap, and there's nothing we can do about
1119 * it. We'll eventually need to shift this down even further so that
1120 * we can check if we allocated blocks over a hole first.
1122 if (mapping->nrpages) {
1123 ret = invalidate_inode_pages2_range(mapping,
1125 (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
1129 while (iov_iter_count(iter)) {
1130 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
1131 iter, dax_iomap_actor);
1138 iocb->ki_pos += done;
1139 return done ? done : ret;
1141 EXPORT_SYMBOL_GPL(dax_iomap_rw);
1144 * dax_iomap_fault - handle a page fault on a DAX file
1145 * @vma: The virtual memory area where the fault occurred
1146 * @vmf: The description of the fault
1147 * @ops: iomap ops passed from the file system
1149 * When a page fault occurs, filesystems may call this helper in their fault
1150 * or mkwrite handler for DAX files. Assumes the caller has done all the
1151 * necessary locking for the page fault to proceed successfully.
1153 int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
1154 struct iomap_ops *ops)
1156 struct address_space *mapping = vma->vm_file->f_mapping;
1157 struct inode *inode = mapping->host;
1158 unsigned long vaddr = (unsigned long)vmf->virtual_address;
1159 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1161 struct iomap iomap = { 0 };
1163 int error, major = 0;
1167 * Check whether offset isn't beyond end of file now. Caller is supposed
1168 * to hold locks serializing us with truncate / punch hole so this is
1171 if (pos >= i_size_read(inode))
1172 return VM_FAULT_SIGBUS;
1174 entry = grab_mapping_entry(mapping, vmf->pgoff);
1175 if (IS_ERR(entry)) {
1176 error = PTR_ERR(entry);
1180 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1181 flags |= IOMAP_WRITE;
1184 * Note that we don't bother to use iomap_apply here: DAX required
1185 * the file system block size to be equal the page size, which means
1186 * that we never have to deal with more than a single extent here.
1188 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1191 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
1192 error = -EIO; /* fs corruption? */
1196 sector = iomap.blkno + (((pos & PAGE_MASK) - iomap.offset) >> 9);
1198 if (vmf->cow_page) {
1199 switch (iomap.type) {
1201 case IOMAP_UNWRITTEN:
1202 clear_user_highpage(vmf->cow_page, vaddr);
1205 error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
1206 vmf->cow_page, vaddr);
1216 if (!radix_tree_exceptional_entry(entry)) {
1218 return VM_FAULT_LOCKED;
1221 return VM_FAULT_DAX_LOCKED;
1224 switch (iomap.type) {
1226 if (iomap.flags & IOMAP_F_NEW) {
1227 count_vm_event(PGMAJFAULT);
1228 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1229 major = VM_FAULT_MAJOR;
1231 error = dax_insert_mapping(mapping, iomap.bdev, sector,
1232 PAGE_SIZE, &entry, vma, vmf);
1234 case IOMAP_UNWRITTEN:
1236 if (!(vmf->flags & FAULT_FLAG_WRITE))
1237 return dax_load_hole(mapping, entry, vmf);
1246 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
1248 if (error == -ENOMEM)
1249 return VM_FAULT_OOM | major;
1250 /* -EBUSY is fine, somebody else faulted on the same PTE */
1251 if (error < 0 && error != -EBUSY)
1252 return VM_FAULT_SIGBUS | major;
1253 return VM_FAULT_NOPAGE | major;
1255 EXPORT_SYMBOL_GPL(dax_iomap_fault);
1256 #endif /* CONFIG_FS_IOMAP */