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/sched.h>
27 #include <linux/uio.h>
28 #include <linux/vmstat.h>
30 int dax_clear_blocks(struct inode *inode, sector_t block, long size)
32 struct block_device *bdev = inode->i_sb->s_bdev;
33 sector_t sector = block << (inode->i_blkbits - 9);
41 count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
46 unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
50 memset(addr, 0, pgsz);
64 EXPORT_SYMBOL_GPL(dax_clear_blocks);
66 static long dax_get_addr(struct buffer_head *bh, void **addr, unsigned blkbits)
69 sector_t sector = bh->b_blocknr << (blkbits - 9);
70 return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
73 static void dax_new_buf(void *addr, unsigned size, unsigned first, loff_t pos,
76 loff_t final = end - pos + first; /* The final byte of the buffer */
79 memset(addr, 0, first);
81 memset(addr + final, 0, size - final);
84 static bool buffer_written(struct buffer_head *bh)
86 return buffer_mapped(bh) && !buffer_unwritten(bh);
90 * When ext4 encounters a hole, it returns without modifying the buffer_head
91 * which means that we can't trust b_size. To cope with this, we set b_state
92 * to 0 before calling get_block and, if any bit is set, we know we can trust
93 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
94 * and would save us time calling get_block repeatedly.
96 static bool buffer_size_valid(struct buffer_head *bh)
98 return bh->b_state != 0;
101 static ssize_t dax_io(int rw, struct inode *inode, struct iov_iter *iter,
102 loff_t start, loff_t end, get_block_t get_block,
103 struct buffer_head *bh)
108 loff_t bh_max = start;
113 end = min(end, i_size_read(inode));
118 unsigned blkbits = inode->i_blkbits;
119 sector_t block = pos >> blkbits;
120 unsigned first = pos - (block << blkbits);
124 bh->b_size = PAGE_ALIGN(end - pos);
126 retval = get_block(inode, block, bh,
130 if (!buffer_size_valid(bh))
131 bh->b_size = 1 << blkbits;
132 bh_max = pos - first + bh->b_size;
134 unsigned done = bh->b_size -
135 (bh_max - (pos - first));
136 bh->b_blocknr += done >> blkbits;
140 hole = (rw != WRITE) && !buffer_written(bh);
143 size = bh->b_size - first;
145 retval = dax_get_addr(bh, &addr, blkbits);
148 if (buffer_unwritten(bh) || buffer_new(bh))
149 dax_new_buf(addr, retval, first, pos,
152 size = retval - first;
154 max = min(pos + size, end);
158 len = copy_from_iter(addr, max - pos, iter);
160 len = copy_to_iter(addr, max - pos, iter);
162 len = iov_iter_zero(max - pos, iter);
171 return (pos == start) ? retval : pos - start;
175 * dax_do_io - Perform I/O to a DAX file
176 * @rw: READ to read or WRITE to write
177 * @iocb: The control block for this I/O
178 * @inode: The file which the I/O is directed at
179 * @iter: The addresses to do I/O from or to
180 * @pos: The file offset where the I/O starts
181 * @get_block: The filesystem method used to translate file offsets to blocks
182 * @end_io: A filesystem callback for I/O completion
185 * This function uses the same locking scheme as do_blockdev_direct_IO:
186 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
187 * caller for writes. For reads, we take and release the i_mutex ourselves.
188 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
189 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
192 ssize_t dax_do_io(int rw, struct kiocb *iocb, struct inode *inode,
193 struct iov_iter *iter, loff_t pos,
194 get_block_t get_block, dio_iodone_t end_io, int flags)
196 struct buffer_head bh;
197 ssize_t retval = -EINVAL;
198 loff_t end = pos + iov_iter_count(iter);
200 memset(&bh, 0, sizeof(bh));
202 if ((flags & DIO_LOCKING) && (rw == READ)) {
203 struct address_space *mapping = inode->i_mapping;
204 mutex_lock(&inode->i_mutex);
205 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
207 mutex_unlock(&inode->i_mutex);
212 /* Protects against truncate */
213 atomic_inc(&inode->i_dio_count);
215 retval = dax_io(rw, inode, iter, pos, end, get_block, &bh);
217 if ((flags & DIO_LOCKING) && (rw == READ))
218 mutex_unlock(&inode->i_mutex);
220 if ((retval > 0) && end_io)
221 end_io(iocb, pos, retval, bh.b_private);
223 inode_dio_done(inode);
227 EXPORT_SYMBOL_GPL(dax_do_io);
230 * The user has performed a load from a hole in the file. Allocating
231 * a new page in the file would cause excessive storage usage for
232 * workloads with sparse files. We allocate a page cache page instead.
233 * We'll kick it out of the page cache if it's ever written to,
234 * otherwise it will simply fall out of the page cache under memory
235 * pressure without ever having been dirtied.
237 static int dax_load_hole(struct address_space *mapping, struct page *page,
238 struct vm_fault *vmf)
241 struct inode *inode = mapping->host;
243 page = find_or_create_page(mapping, vmf->pgoff,
244 GFP_KERNEL | __GFP_ZERO);
247 /* Recheck i_size under page lock to avoid truncate race */
248 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
249 if (vmf->pgoff >= size) {
251 page_cache_release(page);
252 return VM_FAULT_SIGBUS;
256 return VM_FAULT_LOCKED;
259 static int copy_user_bh(struct page *to, struct buffer_head *bh,
260 unsigned blkbits, unsigned long vaddr)
263 if (dax_get_addr(bh, &vfrom, blkbits) < 0)
265 vto = kmap_atomic(to);
266 copy_user_page(vto, vfrom, vaddr, to);
271 static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
272 struct vm_area_struct *vma, struct vm_fault *vmf)
274 struct address_space *mapping = inode->i_mapping;
275 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
276 unsigned long vaddr = (unsigned long)vmf->virtual_address;
282 i_mmap_lock_read(mapping);
285 * Check truncate didn't happen while we were allocating a block.
286 * If it did, this block may or may not be still allocated to the
287 * file. We can't tell the filesystem to free it because we can't
288 * take i_mutex here. In the worst case, the file still has blocks
289 * allocated past the end of the file.
291 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
292 if (unlikely(vmf->pgoff >= size)) {
297 error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
300 if (error < PAGE_SIZE) {
305 if (buffer_unwritten(bh) || buffer_new(bh))
308 error = vm_insert_mixed(vma, vaddr, pfn);
311 i_mmap_unlock_read(mapping);
319 static int do_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
320 get_block_t get_block)
322 struct file *file = vma->vm_file;
323 struct address_space *mapping = file->f_mapping;
324 struct inode *inode = mapping->host;
326 struct buffer_head bh;
327 unsigned long vaddr = (unsigned long)vmf->virtual_address;
328 unsigned blkbits = inode->i_blkbits;
334 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
335 if (vmf->pgoff >= size)
336 return VM_FAULT_SIGBUS;
338 memset(&bh, 0, sizeof(bh));
339 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
340 bh.b_size = PAGE_SIZE;
343 page = find_get_page(mapping, vmf->pgoff);
345 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
346 page_cache_release(page);
347 return VM_FAULT_RETRY;
349 if (unlikely(page->mapping != mapping)) {
351 page_cache_release(page);
354 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
355 if (unlikely(vmf->pgoff >= size)) {
357 * We have a struct page covering a hole in the file
358 * from a read fault and we've raced with a truncate
365 error = get_block(inode, block, &bh, 0);
366 if (!error && (bh.b_size < PAGE_SIZE))
367 error = -EIO; /* fs corruption? */
371 if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
372 if (vmf->flags & FAULT_FLAG_WRITE) {
373 error = get_block(inode, block, &bh, 1);
374 count_vm_event(PGMAJFAULT);
375 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
376 major = VM_FAULT_MAJOR;
377 if (!error && (bh.b_size < PAGE_SIZE))
382 return dax_load_hole(mapping, page, vmf);
387 struct page *new_page = vmf->cow_page;
388 if (buffer_written(&bh))
389 error = copy_user_bh(new_page, &bh, blkbits, vaddr);
391 clear_user_highpage(new_page, vaddr);
396 i_mmap_lock_read(mapping);
397 /* Check we didn't race with truncate */
398 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
400 if (vmf->pgoff >= size) {
401 i_mmap_unlock_read(mapping);
406 return VM_FAULT_LOCKED;
409 /* Check we didn't race with a read fault installing a new page */
411 page = find_lock_page(mapping, vmf->pgoff);
414 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
416 delete_from_page_cache(page);
418 page_cache_release(page);
421 error = dax_insert_mapping(inode, &bh, vma, vmf);
424 if (error == -ENOMEM)
425 return VM_FAULT_OOM | major;
426 /* -EBUSY is fine, somebody else faulted on the same PTE */
427 if ((error < 0) && (error != -EBUSY))
428 return VM_FAULT_SIGBUS | major;
429 return VM_FAULT_NOPAGE | major;
434 page_cache_release(page);
440 * dax_fault - handle a page fault on a DAX file
441 * @vma: The virtual memory area where the fault occurred
442 * @vmf: The description of the fault
443 * @get_block: The filesystem method used to translate file offsets to blocks
445 * When a page fault occurs, filesystems may call this helper in their
446 * fault handler for DAX files.
448 int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
449 get_block_t get_block)
452 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
454 if (vmf->flags & FAULT_FLAG_WRITE) {
455 sb_start_pagefault(sb);
456 file_update_time(vma->vm_file);
458 result = do_dax_fault(vma, vmf, get_block);
459 if (vmf->flags & FAULT_FLAG_WRITE)
460 sb_end_pagefault(sb);
464 EXPORT_SYMBOL_GPL(dax_fault);
467 * dax_zero_page_range - zero a range within a page of a DAX file
468 * @inode: The file being truncated
469 * @from: The file offset that is being truncated to
470 * @length: The number of bytes to zero
471 * @get_block: The filesystem method used to translate file offsets to blocks
473 * This function can be called by a filesystem when it is zeroing part of a
474 * page in a DAX file. This is intended for hole-punch operations. If
475 * you are truncating a file, the helper function dax_truncate_page() may be
478 * We work in terms of PAGE_CACHE_SIZE here for commonality with
479 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
480 * took care of disposing of the unnecessary blocks. Even if the filesystem
481 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
482 * since the file might be mmapped.
484 int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
485 get_block_t get_block)
487 struct buffer_head bh;
488 pgoff_t index = from >> PAGE_CACHE_SHIFT;
489 unsigned offset = from & (PAGE_CACHE_SIZE-1);
492 /* Block boundary? Nothing to do */
495 BUG_ON((offset + length) > PAGE_CACHE_SIZE);
497 memset(&bh, 0, sizeof(bh));
498 bh.b_size = PAGE_CACHE_SIZE;
499 err = get_block(inode, index, &bh, 0);
502 if (buffer_written(&bh)) {
504 err = dax_get_addr(&bh, &addr, inode->i_blkbits);
507 memset(addr + offset, 0, length);
512 EXPORT_SYMBOL_GPL(dax_zero_page_range);
515 * dax_truncate_page - handle a partial page being truncated in a DAX file
516 * @inode: The file being truncated
517 * @from: The file offset that is being truncated to
518 * @get_block: The filesystem method used to translate file offsets to blocks
520 * Similar to block_truncate_page(), this function can be called by a
521 * filesystem when it is truncating a DAX file to handle the partial page.
523 * We work in terms of PAGE_CACHE_SIZE here for commonality with
524 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
525 * took care of disposing of the unnecessary blocks. Even if the filesystem
526 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
527 * since the file might be mmapped.
529 int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
531 unsigned length = PAGE_CACHE_ALIGN(from) - from;
532 return dax_zero_page_range(inode, from, length, get_block);
534 EXPORT_SYMBOL_GPL(dax_truncate_page);