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/pmem.h>
28 #include <linux/sched.h>
29 #include <linux/uio.h>
30 #include <linux/vmstat.h>
32 int dax_clear_blocks(struct inode *inode, sector_t block, long size)
34 struct block_device *bdev = inode->i_sb->s_bdev;
35 sector_t sector = block << (inode->i_blkbits - 9);
43 count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
48 unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
51 clear_pmem(addr, pgsz);
64 EXPORT_SYMBOL_GPL(dax_clear_blocks);
66 static long dax_get_addr(struct buffer_head *bh, void __pmem **addr,
70 sector_t sector = bh->b_blocknr << (blkbits - 9);
71 return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
74 /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
75 static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
76 loff_t pos, loff_t end)
78 loff_t final = end - pos + first; /* The final byte of the buffer */
81 clear_pmem(addr, first);
83 clear_pmem(addr + final, size - final);
86 static bool buffer_written(struct buffer_head *bh)
88 return buffer_mapped(bh) && !buffer_unwritten(bh);
92 * When ext4 encounters a hole, it returns without modifying the buffer_head
93 * which means that we can't trust b_size. To cope with this, we set b_state
94 * to 0 before calling get_block and, if any bit is set, we know we can trust
95 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
96 * and would save us time calling get_block repeatedly.
98 static bool buffer_size_valid(struct buffer_head *bh)
100 return bh->b_state != 0;
103 static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
104 loff_t start, loff_t end, get_block_t get_block,
105 struct buffer_head *bh)
110 loff_t bh_max = start;
113 bool need_wmb = false;
115 if (iov_iter_rw(iter) != WRITE)
116 end = min(end, i_size_read(inode));
121 unsigned blkbits = inode->i_blkbits;
122 long page = pos >> PAGE_SHIFT;
123 sector_t block = page << (PAGE_SHIFT - blkbits);
124 unsigned first = pos - (block << blkbits);
128 bh->b_size = PAGE_ALIGN(end - pos);
130 retval = get_block(inode, block, bh,
131 iov_iter_rw(iter) == WRITE);
134 if (!buffer_size_valid(bh))
135 bh->b_size = 1 << blkbits;
136 bh_max = pos - first + bh->b_size;
138 unsigned done = bh->b_size -
139 (bh_max - (pos - first));
140 bh->b_blocknr += done >> blkbits;
144 hole = iov_iter_rw(iter) != WRITE && !buffer_written(bh);
147 size = bh->b_size - first;
149 retval = dax_get_addr(bh, &addr, blkbits);
152 if (buffer_unwritten(bh) || buffer_new(bh)) {
153 dax_new_buf(addr, retval, first, pos,
158 size = retval - first;
160 max = min(pos + size, end);
163 if (iov_iter_rw(iter) == WRITE) {
164 len = copy_from_iter_pmem(addr, max - pos, iter);
167 len = copy_to_iter((void __force *)addr, max - pos,
170 len = iov_iter_zero(max - pos, iter);
184 return (pos == start) ? retval : pos - start;
188 * dax_do_io - Perform I/O to a DAX file
189 * @iocb: The control block for this I/O
190 * @inode: The file which the I/O is directed at
191 * @iter: The addresses to do I/O from or to
192 * @pos: The file offset where the I/O starts
193 * @get_block: The filesystem method used to translate file offsets to blocks
194 * @end_io: A filesystem callback for I/O completion
197 * This function uses the same locking scheme as do_blockdev_direct_IO:
198 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
199 * caller for writes. For reads, we take and release the i_mutex ourselves.
200 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
201 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
204 ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
205 struct iov_iter *iter, loff_t pos, get_block_t get_block,
206 dio_iodone_t end_io, int flags)
208 struct buffer_head bh;
209 ssize_t retval = -EINVAL;
210 loff_t end = pos + iov_iter_count(iter);
212 memset(&bh, 0, sizeof(bh));
214 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
215 struct address_space *mapping = inode->i_mapping;
216 mutex_lock(&inode->i_mutex);
217 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
219 mutex_unlock(&inode->i_mutex);
224 /* Protects against truncate */
225 if (!(flags & DIO_SKIP_DIO_COUNT))
226 inode_dio_begin(inode);
228 retval = dax_io(inode, iter, pos, end, get_block, &bh);
230 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
231 mutex_unlock(&inode->i_mutex);
233 if ((retval > 0) && end_io)
234 end_io(iocb, pos, retval, bh.b_private);
236 if (!(flags & DIO_SKIP_DIO_COUNT))
237 inode_dio_end(inode);
241 EXPORT_SYMBOL_GPL(dax_do_io);
244 * The user has performed a load from a hole in the file. Allocating
245 * a new page in the file would cause excessive storage usage for
246 * workloads with sparse files. We allocate a page cache page instead.
247 * We'll kick it out of the page cache if it's ever written to,
248 * otherwise it will simply fall out of the page cache under memory
249 * pressure without ever having been dirtied.
251 static int dax_load_hole(struct address_space *mapping, struct page *page,
252 struct vm_fault *vmf)
255 struct inode *inode = mapping->host;
257 page = find_or_create_page(mapping, vmf->pgoff,
258 GFP_KERNEL | __GFP_ZERO);
261 /* Recheck i_size under page lock to avoid truncate race */
262 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
263 if (vmf->pgoff >= size) {
265 page_cache_release(page);
266 return VM_FAULT_SIGBUS;
270 return VM_FAULT_LOCKED;
273 static int copy_user_bh(struct page *to, struct buffer_head *bh,
274 unsigned blkbits, unsigned long vaddr)
279 if (dax_get_addr(bh, &vfrom, blkbits) < 0)
281 vto = kmap_atomic(to);
282 copy_user_page(vto, (void __force *)vfrom, vaddr, to);
287 static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
288 struct vm_area_struct *vma, struct vm_fault *vmf)
290 struct address_space *mapping = inode->i_mapping;
291 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
292 unsigned long vaddr = (unsigned long)vmf->virtual_address;
298 i_mmap_lock_read(mapping);
301 * Check truncate didn't happen while we were allocating a block.
302 * If it did, this block may or may not be still allocated to the
303 * file. We can't tell the filesystem to free it because we can't
304 * take i_mutex here. In the worst case, the file still has blocks
305 * allocated past the end of the file.
307 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
308 if (unlikely(vmf->pgoff >= size)) {
313 error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
316 if (error < PAGE_SIZE) {
321 if (buffer_unwritten(bh) || buffer_new(bh)) {
322 clear_pmem(addr, PAGE_SIZE);
326 error = vm_insert_mixed(vma, vaddr, pfn);
329 i_mmap_unlock_read(mapping);
335 * __dax_fault - handle a page fault on a DAX file
336 * @vma: The virtual memory area where the fault occurred
337 * @vmf: The description of the fault
338 * @get_block: The filesystem method used to translate file offsets to blocks
339 * @complete_unwritten: The filesystem method used to convert unwritten blocks
340 * to written so the data written to them is exposed. This is required for
341 * required by write faults for filesystems that will return unwritten
342 * extent mappings from @get_block, but it is optional for reads as
343 * dax_insert_mapping() will always zero unwritten blocks. If the fs does
344 * not support unwritten extents, the it should pass NULL.
346 * When a page fault occurs, filesystems may call this helper in their
347 * fault handler for DAX files. __dax_fault() assumes the caller has done all
348 * the necessary locking for the page fault to proceed successfully.
350 int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
351 get_block_t get_block, dax_iodone_t complete_unwritten)
353 struct file *file = vma->vm_file;
354 struct address_space *mapping = file->f_mapping;
355 struct inode *inode = mapping->host;
357 struct buffer_head bh;
358 unsigned long vaddr = (unsigned long)vmf->virtual_address;
359 unsigned blkbits = inode->i_blkbits;
365 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
366 if (vmf->pgoff >= size)
367 return VM_FAULT_SIGBUS;
369 memset(&bh, 0, sizeof(bh));
370 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
371 bh.b_size = PAGE_SIZE;
374 page = find_get_page(mapping, vmf->pgoff);
376 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
377 page_cache_release(page);
378 return VM_FAULT_RETRY;
380 if (unlikely(page->mapping != mapping)) {
382 page_cache_release(page);
385 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
386 if (unlikely(vmf->pgoff >= size)) {
388 * We have a struct page covering a hole in the file
389 * from a read fault and we've raced with a truncate
396 error = get_block(inode, block, &bh, 0);
397 if (!error && (bh.b_size < PAGE_SIZE))
398 error = -EIO; /* fs corruption? */
402 if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
403 if (vmf->flags & FAULT_FLAG_WRITE) {
404 error = get_block(inode, block, &bh, 1);
405 count_vm_event(PGMAJFAULT);
406 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
407 major = VM_FAULT_MAJOR;
408 if (!error && (bh.b_size < PAGE_SIZE))
413 return dax_load_hole(mapping, page, vmf);
418 struct page *new_page = vmf->cow_page;
419 if (buffer_written(&bh))
420 error = copy_user_bh(new_page, &bh, blkbits, vaddr);
422 clear_user_highpage(new_page, vaddr);
427 i_mmap_lock_read(mapping);
428 /* Check we didn't race with truncate */
429 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
431 if (vmf->pgoff >= size) {
432 i_mmap_unlock_read(mapping);
437 return VM_FAULT_LOCKED;
440 /* Check we didn't race with a read fault installing a new page */
442 page = find_lock_page(mapping, vmf->pgoff);
445 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
447 delete_from_page_cache(page);
449 page_cache_release(page);
453 * If we successfully insert the new mapping over an unwritten extent,
454 * we need to ensure we convert the unwritten extent. If there is an
455 * error inserting the mapping, the filesystem needs to leave it as
456 * unwritten to prevent exposure of the stale underlying data to
457 * userspace, but we still need to call the completion function so
458 * the private resources on the mapping buffer can be released. We
459 * indicate what the callback should do via the uptodate variable, same
460 * as for normal BH based IO completions.
462 error = dax_insert_mapping(inode, &bh, vma, vmf);
463 if (buffer_unwritten(&bh)) {
464 if (complete_unwritten)
465 complete_unwritten(&bh, !error);
467 WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
471 if (error == -ENOMEM)
472 return VM_FAULT_OOM | major;
473 /* -EBUSY is fine, somebody else faulted on the same PTE */
474 if ((error < 0) && (error != -EBUSY))
475 return VM_FAULT_SIGBUS | major;
476 return VM_FAULT_NOPAGE | major;
481 page_cache_release(page);
485 EXPORT_SYMBOL(__dax_fault);
488 * dax_fault - handle a page fault on a DAX file
489 * @vma: The virtual memory area where the fault occurred
490 * @vmf: The description of the fault
491 * @get_block: The filesystem method used to translate file offsets to blocks
493 * When a page fault occurs, filesystems may call this helper in their
494 * fault handler for DAX files.
496 int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
497 get_block_t get_block, dax_iodone_t complete_unwritten)
500 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
502 if (vmf->flags & FAULT_FLAG_WRITE) {
503 sb_start_pagefault(sb);
504 file_update_time(vma->vm_file);
506 result = __dax_fault(vma, vmf, get_block, complete_unwritten);
507 if (vmf->flags & FAULT_FLAG_WRITE)
508 sb_end_pagefault(sb);
512 EXPORT_SYMBOL_GPL(dax_fault);
514 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
516 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
517 * more often than one might expect in the below function.
519 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
521 int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
522 pmd_t *pmd, unsigned int flags, get_block_t get_block,
523 dax_iodone_t complete_unwritten)
525 struct file *file = vma->vm_file;
526 struct address_space *mapping = file->f_mapping;
527 struct inode *inode = mapping->host;
528 struct buffer_head bh;
529 unsigned blkbits = inode->i_blkbits;
530 unsigned long pmd_addr = address & PMD_MASK;
531 bool write = flags & FAULT_FLAG_WRITE;
535 sector_t block, sector;
539 /* Fall back to PTEs if we're going to COW */
540 if (write && !(vma->vm_flags & VM_SHARED))
541 return VM_FAULT_FALLBACK;
542 /* If the PMD would extend outside the VMA */
543 if (pmd_addr < vma->vm_start)
544 return VM_FAULT_FALLBACK;
545 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
546 return VM_FAULT_FALLBACK;
548 pgoff = linear_page_index(vma, pmd_addr);
549 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
551 return VM_FAULT_SIGBUS;
552 /* If the PMD would cover blocks out of the file */
553 if ((pgoff | PG_PMD_COLOUR) >= size)
554 return VM_FAULT_FALLBACK;
556 memset(&bh, 0, sizeof(bh));
557 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
559 bh.b_size = PMD_SIZE;
560 length = get_block(inode, block, &bh, write);
562 return VM_FAULT_SIGBUS;
563 i_mmap_lock_read(mapping);
566 * If the filesystem isn't willing to tell us the length of a hole,
567 * just fall back to PTEs. Calling get_block 512 times in a loop
570 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE)
574 * If we allocated new storage, make sure no process has any
575 * zero pages covering this hole
577 if (buffer_new(&bh)) {
578 i_mmap_unlock_read(mapping);
579 unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0);
580 i_mmap_lock_read(mapping);
584 * If a truncate happened while we were allocating blocks, we may
585 * leave blocks allocated to the file that are beyond EOF. We can't
586 * take i_mutex here, so just leave them hanging; they'll be freed
587 * when the file is deleted.
589 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
591 result = VM_FAULT_SIGBUS;
594 if ((pgoff | PG_PMD_COLOUR) >= size)
597 if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
600 struct page *zero_page = get_huge_zero_page();
602 if (unlikely(!zero_page))
605 ptl = pmd_lock(vma->vm_mm, pmd);
606 if (!pmd_none(*pmd)) {
611 entry = mk_pmd(zero_page, vma->vm_page_prot);
612 entry = pmd_mkhuge(entry);
613 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
614 result = VM_FAULT_NOPAGE;
617 sector = bh.b_blocknr << (blkbits - 9);
618 length = bdev_direct_access(bh.b_bdev, sector, &kaddr, &pfn,
621 result = VM_FAULT_SIGBUS;
624 if ((length < PMD_SIZE) || (pfn & PG_PMD_COLOUR))
627 if (buffer_unwritten(&bh) || buffer_new(&bh)) {
629 for (i = 0; i < PTRS_PER_PMD; i++)
630 clear_pmem(kaddr + i * PAGE_SIZE, PAGE_SIZE);
632 count_vm_event(PGMAJFAULT);
633 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
634 result |= VM_FAULT_MAJOR;
637 result |= vmf_insert_pfn_pmd(vma, address, pmd, pfn, write);
641 i_mmap_unlock_read(mapping);
643 if (buffer_unwritten(&bh))
644 complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
649 count_vm_event(THP_FAULT_FALLBACK);
650 result = VM_FAULT_FALLBACK;
653 EXPORT_SYMBOL_GPL(__dax_pmd_fault);
656 * dax_pmd_fault - handle a PMD fault on a DAX file
657 * @vma: The virtual memory area where the fault occurred
658 * @vmf: The description of the fault
659 * @get_block: The filesystem method used to translate file offsets to blocks
661 * When a page fault occurs, filesystems may call this helper in their
662 * pmd_fault handler for DAX files.
664 int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
665 pmd_t *pmd, unsigned int flags, get_block_t get_block,
666 dax_iodone_t complete_unwritten)
669 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
671 if (flags & FAULT_FLAG_WRITE) {
672 sb_start_pagefault(sb);
673 file_update_time(vma->vm_file);
675 result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
677 if (flags & FAULT_FLAG_WRITE)
678 sb_end_pagefault(sb);
682 EXPORT_SYMBOL_GPL(dax_pmd_fault);
683 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
686 * dax_pfn_mkwrite - handle first write to DAX page
687 * @vma: The virtual memory area where the fault occurred
688 * @vmf: The description of the fault
691 int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
693 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
695 sb_start_pagefault(sb);
696 file_update_time(vma->vm_file);
697 sb_end_pagefault(sb);
698 return VM_FAULT_NOPAGE;
700 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
703 * dax_zero_page_range - zero a range within a page of a DAX file
704 * @inode: The file being truncated
705 * @from: The file offset that is being truncated to
706 * @length: The number of bytes to zero
707 * @get_block: The filesystem method used to translate file offsets to blocks
709 * This function can be called by a filesystem when it is zeroing part of a
710 * page in a DAX file. This is intended for hole-punch operations. If
711 * you are truncating a file, the helper function dax_truncate_page() may be
714 * We work in terms of PAGE_CACHE_SIZE here for commonality with
715 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
716 * took care of disposing of the unnecessary blocks. Even if the filesystem
717 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
718 * since the file might be mmapped.
720 int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
721 get_block_t get_block)
723 struct buffer_head bh;
724 pgoff_t index = from >> PAGE_CACHE_SHIFT;
725 unsigned offset = from & (PAGE_CACHE_SIZE-1);
728 /* Block boundary? Nothing to do */
731 BUG_ON((offset + length) > PAGE_CACHE_SIZE);
733 memset(&bh, 0, sizeof(bh));
734 bh.b_size = PAGE_CACHE_SIZE;
735 err = get_block(inode, index, &bh, 0);
738 if (buffer_written(&bh)) {
740 err = dax_get_addr(&bh, &addr, inode->i_blkbits);
743 clear_pmem(addr + offset, length);
749 EXPORT_SYMBOL_GPL(dax_zero_page_range);
752 * dax_truncate_page - handle a partial page being truncated in a DAX file
753 * @inode: The file being truncated
754 * @from: The file offset that is being truncated to
755 * @get_block: The filesystem method used to translate file offsets to blocks
757 * Similar to block_truncate_page(), this function can be called by a
758 * filesystem when it is truncating a DAX file to handle the partial page.
760 * We work in terms of PAGE_CACHE_SIZE here for commonality with
761 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
762 * took care of disposing of the unnecessary blocks. Even if the filesystem
763 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
764 * since the file might be mmapped.
766 int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
768 unsigned length = PAGE_CACHE_ALIGN(from) - from;
769 return dax_zero_page_range(inode, from, length, get_block);
771 EXPORT_SYMBOL_GPL(dax_truncate_page);