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>
35 #define RADIX_DAX_MASK 0xf
36 #define RADIX_DAX_SHIFT 4
37 #define RADIX_DAX_PTE (0x4 | RADIX_TREE_EXCEPTIONAL_ENTRY)
38 #define RADIX_DAX_PMD (0x8 | RADIX_TREE_EXCEPTIONAL_ENTRY)
39 #define RADIX_DAX_TYPE(entry) ((unsigned long)entry & RADIX_DAX_MASK)
40 #define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT))
41 #define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \
42 RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE)))
44 static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
46 struct request_queue *q = bdev->bd_queue;
49 dax->addr = (void __pmem *) ERR_PTR(-EIO);
50 if (blk_queue_enter(q, true) != 0)
53 rc = bdev_direct_access(bdev, dax);
55 dax->addr = (void __pmem *) ERR_PTR(rc);
62 static void dax_unmap_atomic(struct block_device *bdev,
63 const struct blk_dax_ctl *dax)
65 if (IS_ERR(dax->addr))
67 blk_queue_exit(bdev->bd_queue);
70 struct page *read_dax_sector(struct block_device *bdev, sector_t n)
72 struct page *page = alloc_pages(GFP_KERNEL, 0);
73 struct blk_dax_ctl dax = {
75 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
80 return ERR_PTR(-ENOMEM);
82 rc = dax_map_atomic(bdev, &dax);
85 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
86 dax_unmap_atomic(bdev, &dax);
91 * dax_clear_sectors() is called from within transaction context from XFS,
92 * and hence this means the stack from this point must follow GFP_NOFS
93 * semantics for all operations.
95 int dax_clear_sectors(struct block_device *bdev, sector_t _sector, long _size)
97 struct blk_dax_ctl dax = {
106 count = dax_map_atomic(bdev, &dax);
109 sz = min_t(long, count, SZ_128K);
110 clear_pmem(dax.addr, sz);
112 dax.sector += sz / 512;
113 dax_unmap_atomic(bdev, &dax);
120 EXPORT_SYMBOL_GPL(dax_clear_sectors);
122 /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
123 static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
124 loff_t pos, loff_t end)
126 loff_t final = end - pos + first; /* The final byte of the buffer */
129 clear_pmem(addr, first);
131 clear_pmem(addr + final, size - final);
134 static bool buffer_written(struct buffer_head *bh)
136 return buffer_mapped(bh) && !buffer_unwritten(bh);
140 * When ext4 encounters a hole, it returns without modifying the buffer_head
141 * which means that we can't trust b_size. To cope with this, we set b_state
142 * to 0 before calling get_block and, if any bit is set, we know we can trust
143 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
144 * and would save us time calling get_block repeatedly.
146 static bool buffer_size_valid(struct buffer_head *bh)
148 return bh->b_state != 0;
152 static sector_t to_sector(const struct buffer_head *bh,
153 const struct inode *inode)
155 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
160 static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
161 loff_t start, loff_t end, get_block_t get_block,
162 struct buffer_head *bh)
164 loff_t pos = start, max = start, bh_max = start;
165 bool hole = false, need_wmb = false;
166 struct block_device *bdev = NULL;
167 int rw = iov_iter_rw(iter), rc;
169 struct blk_dax_ctl dax = {
170 .addr = (void __pmem *) ERR_PTR(-EIO),
174 end = min(end, i_size_read(inode));
179 unsigned blkbits = inode->i_blkbits;
180 long page = pos >> PAGE_SHIFT;
181 sector_t block = page << (PAGE_SHIFT - blkbits);
182 unsigned first = pos - (block << blkbits);
186 bh->b_size = PAGE_ALIGN(end - pos);
188 rc = get_block(inode, block, bh, rw == WRITE);
191 if (!buffer_size_valid(bh))
192 bh->b_size = 1 << blkbits;
193 bh_max = pos - first + bh->b_size;
196 unsigned done = bh->b_size -
197 (bh_max - (pos - first));
198 bh->b_blocknr += done >> blkbits;
202 hole = rw == READ && !buffer_written(bh);
204 size = bh->b_size - first;
206 dax_unmap_atomic(bdev, &dax);
207 dax.sector = to_sector(bh, inode);
208 dax.size = bh->b_size;
209 map_len = dax_map_atomic(bdev, &dax);
214 if (buffer_unwritten(bh) || buffer_new(bh)) {
215 dax_new_buf(dax.addr, map_len, first,
220 size = map_len - first;
222 max = min(pos + size, end);
225 if (iov_iter_rw(iter) == WRITE) {
226 len = copy_from_iter_pmem(dax.addr, max - pos, iter);
229 len = copy_to_iter((void __force *) dax.addr, max - pos,
232 len = iov_iter_zero(max - pos, iter);
240 if (!IS_ERR(dax.addr))
246 dax_unmap_atomic(bdev, &dax);
248 return (pos == start) ? rc : pos - start;
252 * dax_do_io - Perform I/O to a DAX file
253 * @iocb: The control block for this I/O
254 * @inode: The file which the I/O is directed at
255 * @iter: The addresses to do I/O from or to
256 * @pos: The file offset where the I/O starts
257 * @get_block: The filesystem method used to translate file offsets to blocks
258 * @end_io: A filesystem callback for I/O completion
261 * This function uses the same locking scheme as do_blockdev_direct_IO:
262 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
263 * caller for writes. For reads, we take and release the i_mutex ourselves.
264 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
265 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
268 ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
269 struct iov_iter *iter, loff_t pos, get_block_t get_block,
270 dio_iodone_t end_io, int flags)
272 struct buffer_head bh;
273 ssize_t retval = -EINVAL;
274 loff_t end = pos + iov_iter_count(iter);
276 memset(&bh, 0, sizeof(bh));
277 bh.b_bdev = inode->i_sb->s_bdev;
279 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
280 struct address_space *mapping = inode->i_mapping;
282 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
289 /* Protects against truncate */
290 if (!(flags & DIO_SKIP_DIO_COUNT))
291 inode_dio_begin(inode);
293 retval = dax_io(inode, iter, pos, end, get_block, &bh);
295 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
301 err = end_io(iocb, pos, retval, bh.b_private);
306 if (!(flags & DIO_SKIP_DIO_COUNT))
307 inode_dio_end(inode);
311 EXPORT_SYMBOL_GPL(dax_do_io);
314 * The user has performed a load from a hole in the file. Allocating
315 * a new page in the file would cause excessive storage usage for
316 * workloads with sparse files. We allocate a page cache page instead.
317 * We'll kick it out of the page cache if it's ever written to,
318 * otherwise it will simply fall out of the page cache under memory
319 * pressure without ever having been dirtied.
321 static int dax_load_hole(struct address_space *mapping, struct page *page,
322 struct vm_fault *vmf)
325 struct inode *inode = mapping->host;
327 page = find_or_create_page(mapping, vmf->pgoff,
328 GFP_KERNEL | __GFP_ZERO);
331 /* Recheck i_size under page lock to avoid truncate race */
332 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
333 if (vmf->pgoff >= size) {
336 return VM_FAULT_SIGBUS;
340 return VM_FAULT_LOCKED;
343 static int copy_user_bh(struct page *to, struct inode *inode,
344 struct buffer_head *bh, unsigned long vaddr)
346 struct blk_dax_ctl dax = {
347 .sector = to_sector(bh, inode),
350 struct block_device *bdev = bh->b_bdev;
353 if (dax_map_atomic(bdev, &dax) < 0)
354 return PTR_ERR(dax.addr);
355 vto = kmap_atomic(to);
356 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
358 dax_unmap_atomic(bdev, &dax);
363 #define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
365 static int dax_radix_entry(struct address_space *mapping, pgoff_t index,
366 sector_t sector, bool pmd_entry, bool dirty)
368 struct radix_tree_root *page_tree = &mapping->page_tree;
369 pgoff_t pmd_index = DAX_PMD_INDEX(index);
373 WARN_ON_ONCE(pmd_entry && !dirty);
375 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
377 spin_lock_irq(&mapping->tree_lock);
379 entry = radix_tree_lookup(page_tree, pmd_index);
380 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) {
385 entry = radix_tree_lookup(page_tree, index);
387 type = RADIX_DAX_TYPE(entry);
388 if (WARN_ON_ONCE(type != RADIX_DAX_PTE &&
389 type != RADIX_DAX_PMD)) {
394 if (!pmd_entry || type == RADIX_DAX_PMD)
398 * We only insert dirty PMD entries into the radix tree. This
399 * means we don't need to worry about removing a dirty PTE
400 * entry and inserting a clean PMD entry, thus reducing the
401 * range we would flush with a follow-up fsync/msync call.
403 radix_tree_delete(&mapping->page_tree, index);
404 mapping->nrexceptional--;
407 if (sector == NO_SECTOR) {
409 * This can happen during correct operation if our pfn_mkwrite
410 * fault raced against a hole punch operation. If this
411 * happens the pte that was hole punched will have been
412 * unmapped and the radix tree entry will have been removed by
413 * the time we are called, but the call will still happen. We
414 * will return all the way up to wp_pfn_shared(), where the
415 * pte_same() check will fail, eventually causing page fault
416 * to be retried by the CPU.
421 error = radix_tree_insert(page_tree, index,
422 RADIX_DAX_ENTRY(sector, pmd_entry));
426 mapping->nrexceptional++;
429 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
431 spin_unlock_irq(&mapping->tree_lock);
435 static int dax_writeback_one(struct block_device *bdev,
436 struct address_space *mapping, pgoff_t index, void *entry)
438 struct radix_tree_root *page_tree = &mapping->page_tree;
439 int type = RADIX_DAX_TYPE(entry);
440 struct radix_tree_node *node;
441 struct blk_dax_ctl dax;
445 spin_lock_irq(&mapping->tree_lock);
447 * Regular page slots are stabilized by the page lock even
448 * without the tree itself locked. These unlocked entries
449 * need verification under the tree lock.
451 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
456 /* another fsync thread may have already written back this entry */
457 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
460 if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
465 dax.sector = RADIX_DAX_SECTOR(entry);
466 dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
467 spin_unlock_irq(&mapping->tree_lock);
470 * We cannot hold tree_lock while calling dax_map_atomic() because it
471 * eventually calls cond_resched().
473 ret = dax_map_atomic(bdev, &dax);
477 if (WARN_ON_ONCE(ret < dax.size)) {
482 wb_cache_pmem(dax.addr, dax.size);
484 spin_lock_irq(&mapping->tree_lock);
485 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
486 spin_unlock_irq(&mapping->tree_lock);
488 dax_unmap_atomic(bdev, &dax);
492 spin_unlock_irq(&mapping->tree_lock);
497 * Flush the mapping to the persistent domain within the byte range of [start,
498 * end]. This is required by data integrity operations to ensure file data is
499 * on persistent storage prior to completion of the operation.
501 int dax_writeback_mapping_range(struct address_space *mapping,
502 struct block_device *bdev, struct writeback_control *wbc)
504 struct inode *inode = mapping->host;
505 pgoff_t start_index, end_index, pmd_index;
506 pgoff_t indices[PAGEVEC_SIZE];
512 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
515 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
518 start_index = wbc->range_start >> PAGE_SHIFT;
519 end_index = wbc->range_end >> PAGE_SHIFT;
520 pmd_index = DAX_PMD_INDEX(start_index);
523 entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
526 /* see if the start of our range is covered by a PMD entry */
527 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
528 start_index = pmd_index;
530 tag_pages_for_writeback(mapping, start_index, end_index);
532 pagevec_init(&pvec, 0);
534 pvec.nr = find_get_entries_tag(mapping, start_index,
535 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
536 pvec.pages, indices);
541 for (i = 0; i < pvec.nr; i++) {
542 if (indices[i] > end_index) {
547 ret = dax_writeback_one(bdev, mapping, indices[i],
556 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
558 static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
559 struct vm_area_struct *vma, struct vm_fault *vmf)
561 unsigned long vaddr = (unsigned long)vmf->virtual_address;
562 struct address_space *mapping = inode->i_mapping;
563 struct block_device *bdev = bh->b_bdev;
564 struct blk_dax_ctl dax = {
565 .sector = to_sector(bh, inode),
571 i_mmap_lock_read(mapping);
574 * Check truncate didn't happen while we were allocating a block.
575 * If it did, this block may or may not be still allocated to the
576 * file. We can't tell the filesystem to free it because we can't
577 * take i_mutex here. In the worst case, the file still has blocks
578 * allocated past the end of the file.
580 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
581 if (unlikely(vmf->pgoff >= size)) {
586 if (dax_map_atomic(bdev, &dax) < 0) {
587 error = PTR_ERR(dax.addr);
590 dax_unmap_atomic(bdev, &dax);
592 error = dax_radix_entry(mapping, vmf->pgoff, dax.sector, false,
593 vmf->flags & FAULT_FLAG_WRITE);
597 error = vm_insert_mixed(vma, vaddr, dax.pfn);
600 i_mmap_unlock_read(mapping);
606 * __dax_fault - handle a page fault on a DAX file
607 * @vma: The virtual memory area where the fault occurred
608 * @vmf: The description of the fault
609 * @get_block: The filesystem method used to translate file offsets to blocks
611 * When a page fault occurs, filesystems may call this helper in their
612 * fault handler for DAX files. __dax_fault() assumes the caller has done all
613 * the necessary locking for the page fault to proceed successfully.
615 int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
616 get_block_t get_block)
618 struct file *file = vma->vm_file;
619 struct address_space *mapping = file->f_mapping;
620 struct inode *inode = mapping->host;
622 struct buffer_head bh;
623 unsigned long vaddr = (unsigned long)vmf->virtual_address;
624 unsigned blkbits = inode->i_blkbits;
630 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
631 if (vmf->pgoff >= size)
632 return VM_FAULT_SIGBUS;
634 memset(&bh, 0, sizeof(bh));
635 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
636 bh.b_bdev = inode->i_sb->s_bdev;
637 bh.b_size = PAGE_SIZE;
640 page = find_get_page(mapping, vmf->pgoff);
642 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
644 return VM_FAULT_RETRY;
646 if (unlikely(page->mapping != mapping)) {
651 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
652 if (unlikely(vmf->pgoff >= size)) {
654 * We have a struct page covering a hole in the file
655 * from a read fault and we've raced with a truncate
662 error = get_block(inode, block, &bh, 0);
663 if (!error && (bh.b_size < PAGE_SIZE))
664 error = -EIO; /* fs corruption? */
668 if (!buffer_mapped(&bh) && !vmf->cow_page) {
669 if (vmf->flags & FAULT_FLAG_WRITE) {
670 error = get_block(inode, block, &bh, 1);
671 count_vm_event(PGMAJFAULT);
672 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
673 major = VM_FAULT_MAJOR;
674 if (!error && (bh.b_size < PAGE_SIZE))
679 return dax_load_hole(mapping, page, vmf);
684 struct page *new_page = vmf->cow_page;
685 if (buffer_written(&bh))
686 error = copy_user_bh(new_page, inode, &bh, vaddr);
688 clear_user_highpage(new_page, vaddr);
693 i_mmap_lock_read(mapping);
694 /* Check we didn't race with truncate */
695 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
697 if (vmf->pgoff >= size) {
698 i_mmap_unlock_read(mapping);
703 return VM_FAULT_LOCKED;
706 /* Check we didn't race with a read fault installing a new page */
708 page = find_lock_page(mapping, vmf->pgoff);
711 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
713 delete_from_page_cache(page);
719 /* Filesystem should not return unwritten buffers to us! */
720 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
721 error = dax_insert_mapping(inode, &bh, vma, vmf);
724 if (error == -ENOMEM)
725 return VM_FAULT_OOM | major;
726 /* -EBUSY is fine, somebody else faulted on the same PTE */
727 if ((error < 0) && (error != -EBUSY))
728 return VM_FAULT_SIGBUS | major;
729 return VM_FAULT_NOPAGE | major;
738 EXPORT_SYMBOL(__dax_fault);
741 * dax_fault - handle a page fault on a DAX file
742 * @vma: The virtual memory area where the fault occurred
743 * @vmf: The description of the fault
744 * @get_block: The filesystem method used to translate file offsets to blocks
746 * When a page fault occurs, filesystems may call this helper in their
747 * fault handler for DAX files.
749 int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
750 get_block_t get_block)
753 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
755 if (vmf->flags & FAULT_FLAG_WRITE) {
756 sb_start_pagefault(sb);
757 file_update_time(vma->vm_file);
759 result = __dax_fault(vma, vmf, get_block);
760 if (vmf->flags & FAULT_FLAG_WRITE)
761 sb_end_pagefault(sb);
765 EXPORT_SYMBOL_GPL(dax_fault);
767 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
769 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
770 * more often than one might expect in the below function.
772 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
774 static void __dax_dbg(struct buffer_head *bh, unsigned long address,
775 const char *reason, const char *fn)
778 char bname[BDEVNAME_SIZE];
779 bdevname(bh->b_bdev, bname);
780 pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
781 "length %zd fallback: %s\n", fn, current->comm,
782 address, bname, bh->b_state, (u64)bh->b_blocknr,
785 pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
786 current->comm, address, reason);
790 #define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
792 int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
793 pmd_t *pmd, unsigned int flags, get_block_t get_block)
795 struct file *file = vma->vm_file;
796 struct address_space *mapping = file->f_mapping;
797 struct inode *inode = mapping->host;
798 struct buffer_head bh;
799 unsigned blkbits = inode->i_blkbits;
800 unsigned long pmd_addr = address & PMD_MASK;
801 bool write = flags & FAULT_FLAG_WRITE;
802 struct block_device *bdev;
805 int error, result = 0;
808 /* dax pmd mappings require pfn_t_devmap() */
809 if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
810 return VM_FAULT_FALLBACK;
812 /* Fall back to PTEs if we're going to COW */
813 if (write && !(vma->vm_flags & VM_SHARED)) {
814 split_huge_pmd(vma, pmd, address);
815 dax_pmd_dbg(NULL, address, "cow write");
816 return VM_FAULT_FALLBACK;
818 /* If the PMD would extend outside the VMA */
819 if (pmd_addr < vma->vm_start) {
820 dax_pmd_dbg(NULL, address, "vma start unaligned");
821 return VM_FAULT_FALLBACK;
823 if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
824 dax_pmd_dbg(NULL, address, "vma end unaligned");
825 return VM_FAULT_FALLBACK;
828 pgoff = linear_page_index(vma, pmd_addr);
829 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
831 return VM_FAULT_SIGBUS;
832 /* If the PMD would cover blocks out of the file */
833 if ((pgoff | PG_PMD_COLOUR) >= size) {
834 dax_pmd_dbg(NULL, address,
835 "offset + huge page size > file size");
836 return VM_FAULT_FALLBACK;
839 memset(&bh, 0, sizeof(bh));
840 bh.b_bdev = inode->i_sb->s_bdev;
841 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
843 bh.b_size = PMD_SIZE;
845 if (get_block(inode, block, &bh, 0) != 0)
846 return VM_FAULT_SIGBUS;
848 if (!buffer_mapped(&bh) && write) {
849 if (get_block(inode, block, &bh, 1) != 0)
850 return VM_FAULT_SIGBUS;
852 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
858 * If the filesystem isn't willing to tell us the length of a hole,
859 * just fall back to PTEs. Calling get_block 512 times in a loop
862 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
863 dax_pmd_dbg(&bh, address, "allocated block too small");
864 return VM_FAULT_FALLBACK;
868 * If we allocated new storage, make sure no process has any
869 * zero pages covering this hole
872 loff_t lstart = pgoff << PAGE_SHIFT;
873 loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
875 truncate_pagecache_range(inode, lstart, lend);
878 i_mmap_lock_read(mapping);
881 * If a truncate happened while we were allocating blocks, we may
882 * leave blocks allocated to the file that are beyond EOF. We can't
883 * take i_mutex here, so just leave them hanging; they'll be freed
884 * when the file is deleted.
886 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
888 result = VM_FAULT_SIGBUS;
891 if ((pgoff | PG_PMD_COLOUR) >= size) {
892 dax_pmd_dbg(&bh, address,
893 "offset + huge page size > file size");
897 if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
900 struct page *zero_page = get_huge_zero_page();
902 if (unlikely(!zero_page)) {
903 dax_pmd_dbg(&bh, address, "no zero page");
907 ptl = pmd_lock(vma->vm_mm, pmd);
908 if (!pmd_none(*pmd)) {
910 dax_pmd_dbg(&bh, address, "pmd already present");
914 dev_dbg(part_to_dev(bdev->bd_part),
915 "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
916 __func__, current->comm, address,
917 (unsigned long long) to_sector(&bh, inode));
919 entry = mk_pmd(zero_page, vma->vm_page_prot);
920 entry = pmd_mkhuge(entry);
921 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
922 result = VM_FAULT_NOPAGE;
925 struct blk_dax_ctl dax = {
926 .sector = to_sector(&bh, inode),
929 long length = dax_map_atomic(bdev, &dax);
932 result = VM_FAULT_SIGBUS;
935 if (length < PMD_SIZE) {
936 dax_pmd_dbg(&bh, address, "dax-length too small");
937 dax_unmap_atomic(bdev, &dax);
940 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
941 dax_pmd_dbg(&bh, address, "pfn unaligned");
942 dax_unmap_atomic(bdev, &dax);
946 if (!pfn_t_devmap(dax.pfn)) {
947 dax_unmap_atomic(bdev, &dax);
948 dax_pmd_dbg(&bh, address, "pfn not in memmap");
951 dax_unmap_atomic(bdev, &dax);
954 * For PTE faults we insert a radix tree entry for reads, and
955 * leave it clean. Then on the first write we dirty the radix
956 * tree entry via the dax_pfn_mkwrite() path. This sequence
957 * allows the dax_pfn_mkwrite() call to be simpler and avoid a
958 * call into get_block() to translate the pgoff to a sector in
959 * order to be able to create a new radix tree entry.
961 * The PMD path doesn't have an equivalent to
962 * dax_pfn_mkwrite(), though, so for a read followed by a
963 * write we traverse all the way through __dax_pmd_fault()
964 * twice. This means we can just skip inserting a radix tree
965 * entry completely on the initial read and just wait until
966 * the write to insert a dirty entry.
969 error = dax_radix_entry(mapping, pgoff, dax.sector,
972 dax_pmd_dbg(&bh, address,
973 "PMD radix insertion failed");
978 dev_dbg(part_to_dev(bdev->bd_part),
979 "%s: %s addr: %lx pfn: %lx sect: %llx\n",
980 __func__, current->comm, address,
981 pfn_t_to_pfn(dax.pfn),
982 (unsigned long long) dax.sector);
983 result |= vmf_insert_pfn_pmd(vma, address, pmd,
988 i_mmap_unlock_read(mapping);
993 count_vm_event(THP_FAULT_FALLBACK);
994 result = VM_FAULT_FALLBACK;
997 EXPORT_SYMBOL_GPL(__dax_pmd_fault);
1000 * dax_pmd_fault - handle a PMD fault on a DAX file
1001 * @vma: The virtual memory area where the fault occurred
1002 * @vmf: The description of the fault
1003 * @get_block: The filesystem method used to translate file offsets to blocks
1005 * When a page fault occurs, filesystems may call this helper in their
1006 * pmd_fault handler for DAX files.
1008 int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
1009 pmd_t *pmd, unsigned int flags, get_block_t get_block)
1012 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
1014 if (flags & FAULT_FLAG_WRITE) {
1015 sb_start_pagefault(sb);
1016 file_update_time(vma->vm_file);
1018 result = __dax_pmd_fault(vma, address, pmd, flags, get_block);
1019 if (flags & FAULT_FLAG_WRITE)
1020 sb_end_pagefault(sb);
1024 EXPORT_SYMBOL_GPL(dax_pmd_fault);
1025 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1028 * dax_pfn_mkwrite - handle first write to DAX page
1029 * @vma: The virtual memory area where the fault occurred
1030 * @vmf: The description of the fault
1032 int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1034 struct file *file = vma->vm_file;
1038 * We pass NO_SECTOR to dax_radix_entry() because we expect that a
1039 * RADIX_DAX_PTE entry already exists in the radix tree from a
1040 * previous call to __dax_fault(). We just want to look up that PTE
1041 * entry using vmf->pgoff and make sure the dirty tag is set. This
1042 * saves us from having to make a call to get_block() here to look
1045 error = dax_radix_entry(file->f_mapping, vmf->pgoff, NO_SECTOR, false,
1048 if (error == -ENOMEM)
1049 return VM_FAULT_OOM;
1051 return VM_FAULT_SIGBUS;
1052 return VM_FAULT_NOPAGE;
1054 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
1057 * dax_zero_page_range - zero a range within a page of a DAX file
1058 * @inode: The file being truncated
1059 * @from: The file offset that is being truncated to
1060 * @length: The number of bytes to zero
1061 * @get_block: The filesystem method used to translate file offsets to blocks
1063 * This function can be called by a filesystem when it is zeroing part of a
1064 * page in a DAX file. This is intended for hole-punch operations. If
1065 * you are truncating a file, the helper function dax_truncate_page() may be
1068 * We work in terms of PAGE_SIZE here for commonality with
1069 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
1070 * took care of disposing of the unnecessary blocks. Even if the filesystem
1071 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
1072 * since the file might be mmapped.
1074 int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
1075 get_block_t get_block)
1077 struct buffer_head bh;
1078 pgoff_t index = from >> PAGE_SHIFT;
1079 unsigned offset = from & (PAGE_SIZE-1);
1082 /* Block boundary? Nothing to do */
1085 BUG_ON((offset + length) > PAGE_SIZE);
1087 memset(&bh, 0, sizeof(bh));
1088 bh.b_bdev = inode->i_sb->s_bdev;
1089 bh.b_size = PAGE_SIZE;
1090 err = get_block(inode, index, &bh, 0);
1093 if (buffer_written(&bh)) {
1094 struct block_device *bdev = bh.b_bdev;
1095 struct blk_dax_ctl dax = {
1096 .sector = to_sector(&bh, inode),
1100 if (dax_map_atomic(bdev, &dax) < 0)
1101 return PTR_ERR(dax.addr);
1102 clear_pmem(dax.addr + offset, length);
1104 dax_unmap_atomic(bdev, &dax);
1109 EXPORT_SYMBOL_GPL(dax_zero_page_range);
1112 * dax_truncate_page - handle a partial page being truncated in a DAX file
1113 * @inode: The file being truncated
1114 * @from: The file offset that is being truncated to
1115 * @get_block: The filesystem method used to translate file offsets to blocks
1117 * Similar to block_truncate_page(), this function can be called by a
1118 * filesystem when it is truncating a DAX file to handle the partial page.
1120 * We work in terms of PAGE_SIZE here for commonality with
1121 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
1122 * took care of disposing of the unnecessary blocks. Even if the filesystem
1123 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
1124 * since the file might be mmapped.
1126 int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1128 unsigned length = PAGE_ALIGN(from) - from;
1129 return dax_zero_page_range(inode, from, length, get_block);
1131 EXPORT_SYMBOL_GPL(dax_truncate_page);