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>
21 #include <linux/genhd.h>
22 #include <linux/highmem.h>
23 #include <linux/memcontrol.h>
25 #include <linux/mutex.h>
26 #include <linux/pmem.h>
27 #include <linux/sched.h>
28 #include <linux/uio.h>
29 #include <linux/vmstat.h>
31 int dax_clear_blocks(struct inode *inode, sector_t block, long size)
33 struct block_device *bdev = inode->i_sb->s_bdev;
34 sector_t sector = block << (inode->i_blkbits - 9);
42 count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
47 unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
50 clear_pmem(addr, pgsz);
63 EXPORT_SYMBOL_GPL(dax_clear_blocks);
65 static long dax_get_addr(struct buffer_head *bh, void __pmem **addr,
69 sector_t sector = bh->b_blocknr << (blkbits - 9);
70 return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
73 /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
74 static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
75 loff_t pos, loff_t end)
77 loff_t final = end - pos + first; /* The final byte of the buffer */
80 clear_pmem(addr, first);
82 clear_pmem(addr + final, size - final);
85 static bool buffer_written(struct buffer_head *bh)
87 return buffer_mapped(bh) && !buffer_unwritten(bh);
91 * When ext4 encounters a hole, it returns without modifying the buffer_head
92 * which means that we can't trust b_size. To cope with this, we set b_state
93 * to 0 before calling get_block and, if any bit is set, we know we can trust
94 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
95 * and would save us time calling get_block repeatedly.
97 static bool buffer_size_valid(struct buffer_head *bh)
99 return bh->b_state != 0;
102 static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
103 loff_t start, loff_t end, get_block_t get_block,
104 struct buffer_head *bh)
109 loff_t bh_max = start;
112 bool need_wmb = false;
114 if (iov_iter_rw(iter) != WRITE)
115 end = min(end, i_size_read(inode));
120 unsigned blkbits = inode->i_blkbits;
121 sector_t block = pos >> blkbits;
122 unsigned first = pos - (block << blkbits);
126 bh->b_size = PAGE_ALIGN(end - pos);
128 retval = get_block(inode, block, bh,
129 iov_iter_rw(iter) == WRITE);
132 if (!buffer_size_valid(bh))
133 bh->b_size = 1 << blkbits;
134 bh_max = pos - first + bh->b_size;
136 unsigned done = bh->b_size -
137 (bh_max - (pos - first));
138 bh->b_blocknr += done >> blkbits;
142 hole = iov_iter_rw(iter) != WRITE && !buffer_written(bh);
145 size = bh->b_size - first;
147 retval = dax_get_addr(bh, &addr, blkbits);
150 if (buffer_unwritten(bh) || buffer_new(bh)) {
151 dax_new_buf(addr, retval, first, pos,
156 size = retval - first;
158 max = min(pos + size, end);
161 if (iov_iter_rw(iter) == WRITE) {
162 len = copy_from_iter_pmem(addr, max - pos, iter);
165 len = copy_to_iter((void __force *)addr, max - pos,
168 len = iov_iter_zero(max - pos, iter);
180 return (pos == start) ? retval : pos - start;
184 * dax_do_io - Perform I/O to a DAX file
185 * @iocb: The control block for this I/O
186 * @inode: The file which the I/O is directed at
187 * @iter: The addresses to do I/O from or to
188 * @pos: The file offset where the I/O starts
189 * @get_block: The filesystem method used to translate file offsets to blocks
190 * @end_io: A filesystem callback for I/O completion
193 * This function uses the same locking scheme as do_blockdev_direct_IO:
194 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
195 * caller for writes. For reads, we take and release the i_mutex ourselves.
196 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
197 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
200 ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
201 struct iov_iter *iter, loff_t pos, get_block_t get_block,
202 dio_iodone_t end_io, int flags)
204 struct buffer_head bh;
205 ssize_t retval = -EINVAL;
206 loff_t end = pos + iov_iter_count(iter);
208 memset(&bh, 0, sizeof(bh));
210 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
211 struct address_space *mapping = inode->i_mapping;
212 mutex_lock(&inode->i_mutex);
213 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
215 mutex_unlock(&inode->i_mutex);
220 /* Protects against truncate */
221 if (!(flags & DIO_SKIP_DIO_COUNT))
222 inode_dio_begin(inode);
224 retval = dax_io(inode, iter, pos, end, get_block, &bh);
226 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
227 mutex_unlock(&inode->i_mutex);
229 if ((retval > 0) && end_io)
230 end_io(iocb, pos, retval, bh.b_private);
232 if (!(flags & DIO_SKIP_DIO_COUNT))
233 inode_dio_end(inode);
237 EXPORT_SYMBOL_GPL(dax_do_io);
240 * The user has performed a load from a hole in the file. Allocating
241 * a new page in the file would cause excessive storage usage for
242 * workloads with sparse files. We allocate a page cache page instead.
243 * We'll kick it out of the page cache if it's ever written to,
244 * otherwise it will simply fall out of the page cache under memory
245 * pressure without ever having been dirtied.
247 static int dax_load_hole(struct address_space *mapping, struct page *page,
248 struct vm_fault *vmf)
251 struct inode *inode = mapping->host;
253 page = find_or_create_page(mapping, vmf->pgoff,
254 GFP_KERNEL | __GFP_ZERO);
257 /* Recheck i_size under page lock to avoid truncate race */
258 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
259 if (vmf->pgoff >= size) {
261 page_cache_release(page);
262 return VM_FAULT_SIGBUS;
266 return VM_FAULT_LOCKED;
269 static int copy_user_bh(struct page *to, struct buffer_head *bh,
270 unsigned blkbits, unsigned long vaddr)
275 if (dax_get_addr(bh, &vfrom, blkbits) < 0)
277 vto = kmap_atomic(to);
278 copy_user_page(vto, (void __force *)vfrom, vaddr, to);
283 static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
284 struct vm_area_struct *vma, struct vm_fault *vmf)
286 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
287 unsigned long vaddr = (unsigned long)vmf->virtual_address;
294 * Check truncate didn't happen while we were allocating a block.
295 * If it did, this block may or may not be still allocated to the
296 * file. We can't tell the filesystem to free it because we can't
297 * take i_mutex here. In the worst case, the file still has blocks
298 * allocated past the end of the file.
300 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
301 if (unlikely(vmf->pgoff >= size)) {
306 error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
309 if (error < PAGE_SIZE) {
314 if (buffer_unwritten(bh) || buffer_new(bh)) {
315 clear_pmem(addr, PAGE_SIZE);
319 error = vm_insert_mixed(vma, vaddr, pfn);
326 * __dax_fault - handle a page fault on a DAX file
327 * @vma: The virtual memory area where the fault occurred
328 * @vmf: The description of the fault
329 * @get_block: The filesystem method used to translate file offsets to blocks
330 * @complete_unwritten: The filesystem method used to convert unwritten blocks
331 * to written so the data written to them is exposed. This is required for
332 * required by write faults for filesystems that will return unwritten
333 * extent mappings from @get_block, but it is optional for reads as
334 * dax_insert_mapping() will always zero unwritten blocks. If the fs does
335 * not support unwritten extents, the it should pass NULL.
337 * When a page fault occurs, filesystems may call this helper in their
338 * fault handler for DAX files. __dax_fault() assumes the caller has done all
339 * the necessary locking for the page fault to proceed successfully.
341 int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
342 get_block_t get_block, dax_iodone_t complete_unwritten)
344 struct file *file = vma->vm_file;
345 struct address_space *mapping = file->f_mapping;
346 struct inode *inode = mapping->host;
348 struct buffer_head bh;
349 unsigned long vaddr = (unsigned long)vmf->virtual_address;
350 unsigned blkbits = inode->i_blkbits;
356 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
357 if (vmf->pgoff >= size)
358 return VM_FAULT_SIGBUS;
360 memset(&bh, 0, sizeof(bh));
361 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
362 bh.b_size = PAGE_SIZE;
365 page = find_get_page(mapping, vmf->pgoff);
367 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
368 page_cache_release(page);
369 return VM_FAULT_RETRY;
371 if (unlikely(page->mapping != mapping)) {
373 page_cache_release(page);
376 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
377 if (unlikely(vmf->pgoff >= size)) {
379 * We have a struct page covering a hole in the file
380 * from a read fault and we've raced with a truncate
386 i_mmap_lock_write(mapping);
389 error = get_block(inode, block, &bh, 0);
390 if (!error && (bh.b_size < PAGE_SIZE))
391 error = -EIO; /* fs corruption? */
395 if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
396 if (vmf->flags & FAULT_FLAG_WRITE) {
397 error = get_block(inode, block, &bh, 1);
398 count_vm_event(PGMAJFAULT);
399 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
400 major = VM_FAULT_MAJOR;
401 if (!error && (bh.b_size < PAGE_SIZE))
406 i_mmap_unlock_write(mapping);
407 return dax_load_hole(mapping, page, vmf);
412 struct page *new_page = vmf->cow_page;
413 if (buffer_written(&bh))
414 error = copy_user_bh(new_page, &bh, blkbits, vaddr);
416 clear_user_highpage(new_page, vaddr);
421 /* Check we didn't race with truncate */
422 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
424 if (vmf->pgoff >= size) {
429 return VM_FAULT_LOCKED;
432 /* Check we didn't race with a read fault installing a new page */
434 page = find_lock_page(mapping, vmf->pgoff);
437 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
439 delete_from_page_cache(page);
441 page_cache_release(page);
445 * If we successfully insert the new mapping over an unwritten extent,
446 * we need to ensure we convert the unwritten extent. If there is an
447 * error inserting the mapping, the filesystem needs to leave it as
448 * unwritten to prevent exposure of the stale underlying data to
449 * userspace, but we still need to call the completion function so
450 * the private resources on the mapping buffer can be released. We
451 * indicate what the callback should do via the uptodate variable, same
452 * as for normal BH based IO completions.
454 error = dax_insert_mapping(inode, &bh, vma, vmf);
455 if (buffer_unwritten(&bh)) {
456 if (complete_unwritten)
457 complete_unwritten(&bh, !error);
459 WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
463 i_mmap_unlock_write(mapping);
465 if (error == -ENOMEM)
466 return VM_FAULT_OOM | major;
467 /* -EBUSY is fine, somebody else faulted on the same PTE */
468 if ((error < 0) && (error != -EBUSY))
469 return VM_FAULT_SIGBUS | major;
470 return VM_FAULT_NOPAGE | major;
475 page_cache_release(page);
477 i_mmap_unlock_write(mapping);
482 EXPORT_SYMBOL(__dax_fault);
485 * dax_fault - handle a page fault on a DAX file
486 * @vma: The virtual memory area where the fault occurred
487 * @vmf: The description of the fault
488 * @get_block: The filesystem method used to translate file offsets to blocks
490 * When a page fault occurs, filesystems may call this helper in their
491 * fault handler for DAX files.
493 int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
494 get_block_t get_block, dax_iodone_t complete_unwritten)
497 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
499 if (vmf->flags & FAULT_FLAG_WRITE) {
500 sb_start_pagefault(sb);
501 file_update_time(vma->vm_file);
503 result = __dax_fault(vma, vmf, get_block, complete_unwritten);
504 if (vmf->flags & FAULT_FLAG_WRITE)
505 sb_end_pagefault(sb);
509 EXPORT_SYMBOL_GPL(dax_fault);
511 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
513 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
514 * more often than one might expect in the below function.
516 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
518 int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
519 pmd_t *pmd, unsigned int flags, get_block_t get_block,
520 dax_iodone_t complete_unwritten)
522 struct file *file = vma->vm_file;
523 struct address_space *mapping = file->f_mapping;
524 struct inode *inode = mapping->host;
525 struct buffer_head bh;
526 unsigned blkbits = inode->i_blkbits;
527 unsigned long pmd_addr = address & PMD_MASK;
528 bool write = flags & FAULT_FLAG_WRITE;
532 sector_t block, sector;
536 /* Fall back to PTEs if we're going to COW */
537 if (write && !(vma->vm_flags & VM_SHARED))
538 return VM_FAULT_FALLBACK;
539 /* If the PMD would extend outside the VMA */
540 if (pmd_addr < vma->vm_start)
541 return VM_FAULT_FALLBACK;
542 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
543 return VM_FAULT_FALLBACK;
545 pgoff = linear_page_index(vma, pmd_addr);
546 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
548 return VM_FAULT_SIGBUS;
549 /* If the PMD would cover blocks out of the file */
550 if ((pgoff | PG_PMD_COLOUR) >= size)
551 return VM_FAULT_FALLBACK;
553 memset(&bh, 0, sizeof(bh));
554 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
556 bh.b_size = PMD_SIZE;
557 i_mmap_lock_write(mapping);
558 length = get_block(inode, block, &bh, write);
560 return VM_FAULT_SIGBUS;
563 * If the filesystem isn't willing to tell us the length of a hole,
564 * just fall back to PTEs. Calling get_block 512 times in a loop
567 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE)
570 if (buffer_unwritten(&bh) || buffer_new(&bh)) {
572 for (i = 0; i < PTRS_PER_PMD; i++)
573 clear_page(kaddr + i * PAGE_SIZE);
574 count_vm_event(PGMAJFAULT);
575 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
576 result |= VM_FAULT_MAJOR;
580 * If we allocated new storage, make sure no process has any
581 * zero pages covering this hole
583 if (buffer_new(&bh)) {
584 i_mmap_unlock_write(mapping);
585 unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0);
586 i_mmap_lock_write(mapping);
590 * If a truncate happened while we were allocating blocks, we may
591 * leave blocks allocated to the file that are beyond EOF. We can't
592 * take i_mutex here, so just leave them hanging; they'll be freed
593 * when the file is deleted.
595 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
597 result = VM_FAULT_SIGBUS;
600 if ((pgoff | PG_PMD_COLOUR) >= size)
603 if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
606 struct page *zero_page = get_huge_zero_page();
608 if (unlikely(!zero_page))
611 ptl = pmd_lock(vma->vm_mm, pmd);
612 if (!pmd_none(*pmd)) {
617 entry = mk_pmd(zero_page, vma->vm_page_prot);
618 entry = pmd_mkhuge(entry);
619 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
620 result = VM_FAULT_NOPAGE;
623 sector = bh.b_blocknr << (blkbits - 9);
624 length = bdev_direct_access(bh.b_bdev, sector, &kaddr, &pfn,
627 result = VM_FAULT_SIGBUS;
630 if ((length < PMD_SIZE) || (pfn & PG_PMD_COLOUR))
633 result |= vmf_insert_pfn_pmd(vma, address, pmd, pfn, write);
637 if (buffer_unwritten(&bh))
638 complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
640 i_mmap_unlock_write(mapping);
645 count_vm_event(THP_FAULT_FALLBACK);
646 result = VM_FAULT_FALLBACK;
649 EXPORT_SYMBOL_GPL(__dax_pmd_fault);
652 * dax_pmd_fault - handle a PMD fault on a DAX file
653 * @vma: The virtual memory area where the fault occurred
654 * @vmf: The description of the fault
655 * @get_block: The filesystem method used to translate file offsets to blocks
657 * When a page fault occurs, filesystems may call this helper in their
658 * pmd_fault handler for DAX files.
660 int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
661 pmd_t *pmd, unsigned int flags, get_block_t get_block,
662 dax_iodone_t complete_unwritten)
665 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
667 if (flags & FAULT_FLAG_WRITE) {
668 sb_start_pagefault(sb);
669 file_update_time(vma->vm_file);
671 result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
673 if (flags & FAULT_FLAG_WRITE)
674 sb_end_pagefault(sb);
678 EXPORT_SYMBOL_GPL(dax_pmd_fault);
679 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
682 * dax_pfn_mkwrite - handle first write to DAX page
683 * @vma: The virtual memory area where the fault occurred
684 * @vmf: The description of the fault
687 int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
689 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
691 sb_start_pagefault(sb);
692 file_update_time(vma->vm_file);
693 sb_end_pagefault(sb);
694 return VM_FAULT_NOPAGE;
696 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
699 * dax_zero_page_range - zero a range within a page of a DAX file
700 * @inode: The file being truncated
701 * @from: The file offset that is being truncated to
702 * @length: The number of bytes to zero
703 * @get_block: The filesystem method used to translate file offsets to blocks
705 * This function can be called by a filesystem when it is zeroing part of a
706 * page in a DAX file. This is intended for hole-punch operations. If
707 * you are truncating a file, the helper function dax_truncate_page() may be
710 * We work in terms of PAGE_CACHE_SIZE here for commonality with
711 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
712 * took care of disposing of the unnecessary blocks. Even if the filesystem
713 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
714 * since the file might be mmapped.
716 int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
717 get_block_t get_block)
719 struct buffer_head bh;
720 pgoff_t index = from >> PAGE_CACHE_SHIFT;
721 unsigned offset = from & (PAGE_CACHE_SIZE-1);
724 /* Block boundary? Nothing to do */
727 BUG_ON((offset + length) > PAGE_CACHE_SIZE);
729 memset(&bh, 0, sizeof(bh));
730 bh.b_size = PAGE_CACHE_SIZE;
731 err = get_block(inode, index, &bh, 0);
734 if (buffer_written(&bh)) {
736 err = dax_get_addr(&bh, &addr, inode->i_blkbits);
739 clear_pmem(addr + offset, length);
745 EXPORT_SYMBOL_GPL(dax_zero_page_range);
748 * dax_truncate_page - handle a partial page being truncated in a DAX file
749 * @inode: The file being truncated
750 * @from: The file offset that is being truncated to
751 * @get_block: The filesystem method used to translate file offsets to blocks
753 * Similar to block_truncate_page(), this function can be called by a
754 * filesystem when it is truncating a DAX file to handle the partial page.
756 * We work in terms of PAGE_CACHE_SIZE here for commonality with
757 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
758 * took care of disposing of the unnecessary blocks. Even if the filesystem
759 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
760 * since the file might be mmapped.
762 int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
764 unsigned length = PAGE_CACHE_ALIGN(from) - from;
765 return dax_zero_page_range(inode, from, length, get_block);
767 EXPORT_SYMBOL_GPL(dax_truncate_page);