blob: bff20cc5613040342c898ff56792abab4bbc61ab [file] [log] [blame]
Matthew Wilcoxd475c632015-02-16 15:58:56 -08001/*
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>
6 *
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.
10 *
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
14 * more details.
15 */
16
17#include <linux/atomic.h>
18#include <linux/blkdev.h>
19#include <linux/buffer_head.h>
Ross Zwislerd77e92e2015-09-09 10:29:40 -060020#include <linux/dax.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080021#include <linux/fs.h>
22#include <linux/genhd.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080023#include <linux/highmem.h>
24#include <linux/memcontrol.h>
25#include <linux/mm.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080026#include <linux/mutex.h>
Ross Zwisler2765cfb2015-08-18 13:55:40 -060027#include <linux/pmem.h>
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080028#include <linux/sched.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080029#include <linux/uio.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080030#include <linux/vmstat.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080031
Dave Chinner1ca19152015-11-03 12:37:00 +110032/*
33 * dax_clear_blocks() is called from within transaction context from XFS,
34 * and hence this means the stack from this point must follow GFP_NOFS
35 * semantics for all operations.
36 */
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080037int dax_clear_blocks(struct inode *inode, sector_t block, long size)
38{
39 struct block_device *bdev = inode->i_sb->s_bdev;
40 sector_t sector = block << (inode->i_blkbits - 9);
41
42 might_sleep();
43 do {
Ross Zwislere2e05392015-08-18 13:55:41 -060044 void __pmem *addr;
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080045 unsigned long pfn;
46 long count;
47
48 count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
49 if (count < 0)
50 return count;
51 BUG_ON(size < count);
52 while (count > 0) {
53 unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
54 if (pgsz > count)
55 pgsz = count;
Ross Zwislere2e05392015-08-18 13:55:41 -060056 clear_pmem(addr, pgsz);
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080057 addr += pgsz;
58 size -= pgsz;
59 count -= pgsz;
60 BUG_ON(pgsz & 511);
61 sector += pgsz / 512;
62 cond_resched();
63 }
64 } while (size);
65
Ross Zwisler2765cfb2015-08-18 13:55:40 -060066 wmb_pmem();
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080067 return 0;
68}
69EXPORT_SYMBOL_GPL(dax_clear_blocks);
70
Ross Zwislere2e05392015-08-18 13:55:41 -060071static long dax_get_addr(struct buffer_head *bh, void __pmem **addr,
72 unsigned blkbits)
Matthew Wilcoxd475c632015-02-16 15:58:56 -080073{
74 unsigned long pfn;
75 sector_t sector = bh->b_blocknr << (blkbits - 9);
76 return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
77}
78
Ross Zwisler2765cfb2015-08-18 13:55:40 -060079/* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
Ross Zwislere2e05392015-08-18 13:55:41 -060080static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
81 loff_t pos, loff_t end)
Matthew Wilcoxd475c632015-02-16 15:58:56 -080082{
83 loff_t final = end - pos + first; /* The final byte of the buffer */
84
85 if (first > 0)
Ross Zwislere2e05392015-08-18 13:55:41 -060086 clear_pmem(addr, first);
Matthew Wilcoxd475c632015-02-16 15:58:56 -080087 if (final < size)
Ross Zwislere2e05392015-08-18 13:55:41 -060088 clear_pmem(addr + final, size - final);
Matthew Wilcoxd475c632015-02-16 15:58:56 -080089}
90
91static bool buffer_written(struct buffer_head *bh)
92{
93 return buffer_mapped(bh) && !buffer_unwritten(bh);
94}
95
96/*
97 * When ext4 encounters a hole, it returns without modifying the buffer_head
98 * which means that we can't trust b_size. To cope with this, we set b_state
99 * to 0 before calling get_block and, if any bit is set, we know we can trust
100 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
101 * and would save us time calling get_block repeatedly.
102 */
103static bool buffer_size_valid(struct buffer_head *bh)
104{
105 return bh->b_state != 0;
106}
107
Omar Sandovala95cd632015-03-16 04:33:51 -0700108static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
109 loff_t start, loff_t end, get_block_t get_block,
110 struct buffer_head *bh)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800111{
112 ssize_t retval = 0;
113 loff_t pos = start;
114 loff_t max = start;
115 loff_t bh_max = start;
Ross Zwislere2e05392015-08-18 13:55:41 -0600116 void __pmem *addr;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800117 bool hole = false;
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600118 bool need_wmb = false;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800119
Omar Sandovala95cd632015-03-16 04:33:51 -0700120 if (iov_iter_rw(iter) != WRITE)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800121 end = min(end, i_size_read(inode));
122
123 while (pos < end) {
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600124 size_t len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800125 if (pos == max) {
126 unsigned blkbits = inode->i_blkbits;
Jeff Moyere94f5a22015-08-14 16:15:31 -0400127 long page = pos >> PAGE_SHIFT;
128 sector_t block = page << (PAGE_SHIFT - blkbits);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800129 unsigned first = pos - (block << blkbits);
130 long size;
131
132 if (pos == bh_max) {
133 bh->b_size = PAGE_ALIGN(end - pos);
134 bh->b_state = 0;
135 retval = get_block(inode, block, bh,
Omar Sandovala95cd632015-03-16 04:33:51 -0700136 iov_iter_rw(iter) == WRITE);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800137 if (retval)
138 break;
139 if (!buffer_size_valid(bh))
140 bh->b_size = 1 << blkbits;
141 bh_max = pos - first + bh->b_size;
142 } else {
143 unsigned done = bh->b_size -
144 (bh_max - (pos - first));
145 bh->b_blocknr += done >> blkbits;
146 bh->b_size -= done;
147 }
148
Omar Sandovala95cd632015-03-16 04:33:51 -0700149 hole = iov_iter_rw(iter) != WRITE && !buffer_written(bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800150 if (hole) {
151 addr = NULL;
152 size = bh->b_size - first;
153 } else {
154 retval = dax_get_addr(bh, &addr, blkbits);
155 if (retval < 0)
156 break;
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600157 if (buffer_unwritten(bh) || buffer_new(bh)) {
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800158 dax_new_buf(addr, retval, first, pos,
159 end);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600160 need_wmb = true;
161 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800162 addr += first;
163 size = retval - first;
164 }
165 max = min(pos + size, end);
166 }
167
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600168 if (iov_iter_rw(iter) == WRITE) {
Ross Zwislere2e05392015-08-18 13:55:41 -0600169 len = copy_from_iter_pmem(addr, max - pos, iter);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600170 need_wmb = true;
171 } else if (!hole)
Ross Zwislere2e05392015-08-18 13:55:41 -0600172 len = copy_to_iter((void __force *)addr, max - pos,
173 iter);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800174 else
175 len = iov_iter_zero(max - pos, iter);
176
177 if (!len)
178 break;
179
180 pos += len;
181 addr += len;
182 }
183
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600184 if (need_wmb)
185 wmb_pmem();
186
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800187 return (pos == start) ? retval : pos - start;
188}
189
190/**
191 * dax_do_io - Perform I/O to a DAX file
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800192 * @iocb: The control block for this I/O
193 * @inode: The file which the I/O is directed at
194 * @iter: The addresses to do I/O from or to
195 * @pos: The file offset where the I/O starts
196 * @get_block: The filesystem method used to translate file offsets to blocks
197 * @end_io: A filesystem callback for I/O completion
198 * @flags: See below
199 *
200 * This function uses the same locking scheme as do_blockdev_direct_IO:
201 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
202 * caller for writes. For reads, we take and release the i_mutex ourselves.
203 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
204 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
205 * is in progress.
206 */
Omar Sandovala95cd632015-03-16 04:33:51 -0700207ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
208 struct iov_iter *iter, loff_t pos, get_block_t get_block,
209 dio_iodone_t end_io, int flags)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800210{
211 struct buffer_head bh;
212 ssize_t retval = -EINVAL;
213 loff_t end = pos + iov_iter_count(iter);
214
215 memset(&bh, 0, sizeof(bh));
216
Omar Sandovala95cd632015-03-16 04:33:51 -0700217 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800218 struct address_space *mapping = inode->i_mapping;
219 mutex_lock(&inode->i_mutex);
220 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
221 if (retval) {
222 mutex_unlock(&inode->i_mutex);
223 goto out;
224 }
225 }
226
227 /* Protects against truncate */
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400228 if (!(flags & DIO_SKIP_DIO_COUNT))
229 inode_dio_begin(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800230
Omar Sandovala95cd632015-03-16 04:33:51 -0700231 retval = dax_io(inode, iter, pos, end, get_block, &bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800232
Omar Sandovala95cd632015-03-16 04:33:51 -0700233 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800234 mutex_unlock(&inode->i_mutex);
235
236 if ((retval > 0) && end_io)
237 end_io(iocb, pos, retval, bh.b_private);
238
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400239 if (!(flags & DIO_SKIP_DIO_COUNT))
240 inode_dio_end(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800241 out:
242 return retval;
243}
244EXPORT_SYMBOL_GPL(dax_do_io);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800245
246/*
247 * The user has performed a load from a hole in the file. Allocating
248 * a new page in the file would cause excessive storage usage for
249 * workloads with sparse files. We allocate a page cache page instead.
250 * We'll kick it out of the page cache if it's ever written to,
251 * otherwise it will simply fall out of the page cache under memory
252 * pressure without ever having been dirtied.
253 */
254static int dax_load_hole(struct address_space *mapping, struct page *page,
255 struct vm_fault *vmf)
256{
257 unsigned long size;
258 struct inode *inode = mapping->host;
259 if (!page)
260 page = find_or_create_page(mapping, vmf->pgoff,
261 GFP_KERNEL | __GFP_ZERO);
262 if (!page)
263 return VM_FAULT_OOM;
264 /* Recheck i_size under page lock to avoid truncate race */
265 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
266 if (vmf->pgoff >= size) {
267 unlock_page(page);
268 page_cache_release(page);
269 return VM_FAULT_SIGBUS;
270 }
271
272 vmf->page = page;
273 return VM_FAULT_LOCKED;
274}
275
276static int copy_user_bh(struct page *to, struct buffer_head *bh,
277 unsigned blkbits, unsigned long vaddr)
278{
Ross Zwislere2e05392015-08-18 13:55:41 -0600279 void __pmem *vfrom;
280 void *vto;
281
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800282 if (dax_get_addr(bh, &vfrom, blkbits) < 0)
283 return -EIO;
284 vto = kmap_atomic(to);
Ross Zwislere2e05392015-08-18 13:55:41 -0600285 copy_user_page(vto, (void __force *)vfrom, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800286 kunmap_atomic(vto);
287 return 0;
288}
289
290static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
291 struct vm_area_struct *vma, struct vm_fault *vmf)
292{
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700293 struct address_space *mapping = inode->i_mapping;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800294 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
295 unsigned long vaddr = (unsigned long)vmf->virtual_address;
Ross Zwislere2e05392015-08-18 13:55:41 -0600296 void __pmem *addr;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800297 unsigned long pfn;
298 pgoff_t size;
299 int error;
300
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700301 i_mmap_lock_read(mapping);
302
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800303 /*
304 * Check truncate didn't happen while we were allocating a block.
305 * If it did, this block may or may not be still allocated to the
306 * file. We can't tell the filesystem to free it because we can't
307 * take i_mutex here. In the worst case, the file still has blocks
308 * allocated past the end of the file.
309 */
310 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
311 if (unlikely(vmf->pgoff >= size)) {
312 error = -EIO;
313 goto out;
314 }
315
316 error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
317 if (error < 0)
318 goto out;
319 if (error < PAGE_SIZE) {
320 error = -EIO;
321 goto out;
322 }
323
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600324 if (buffer_unwritten(bh) || buffer_new(bh)) {
Ross Zwislere2e05392015-08-18 13:55:41 -0600325 clear_pmem(addr, PAGE_SIZE);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600326 wmb_pmem();
327 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800328
329 error = vm_insert_mixed(vma, vaddr, pfn);
330
331 out:
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700332 i_mmap_unlock_read(mapping);
333
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800334 return error;
335}
336
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000337/**
338 * __dax_fault - handle a page fault on a DAX file
339 * @vma: The virtual memory area where the fault occurred
340 * @vmf: The description of the fault
341 * @get_block: The filesystem method used to translate file offsets to blocks
Dave Chinnerb2442c52015-07-29 11:48:00 +1000342 * @complete_unwritten: The filesystem method used to convert unwritten blocks
343 * to written so the data written to them is exposed. This is required for
344 * required by write faults for filesystems that will return unwritten
345 * extent mappings from @get_block, but it is optional for reads as
346 * dax_insert_mapping() will always zero unwritten blocks. If the fs does
347 * not support unwritten extents, the it should pass NULL.
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000348 *
349 * When a page fault occurs, filesystems may call this helper in their
350 * fault handler for DAX files. __dax_fault() assumes the caller has done all
351 * the necessary locking for the page fault to proceed successfully.
352 */
353int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Dave Chinnere842f292015-06-04 09:18:18 +1000354 get_block_t get_block, dax_iodone_t complete_unwritten)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800355{
356 struct file *file = vma->vm_file;
357 struct address_space *mapping = file->f_mapping;
358 struct inode *inode = mapping->host;
359 struct page *page;
360 struct buffer_head bh;
361 unsigned long vaddr = (unsigned long)vmf->virtual_address;
362 unsigned blkbits = inode->i_blkbits;
363 sector_t block;
364 pgoff_t size;
365 int error;
366 int major = 0;
367
368 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
369 if (vmf->pgoff >= size)
370 return VM_FAULT_SIGBUS;
371
372 memset(&bh, 0, sizeof(bh));
373 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
374 bh.b_size = PAGE_SIZE;
375
376 repeat:
377 page = find_get_page(mapping, vmf->pgoff);
378 if (page) {
379 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
380 page_cache_release(page);
381 return VM_FAULT_RETRY;
382 }
383 if (unlikely(page->mapping != mapping)) {
384 unlock_page(page);
385 page_cache_release(page);
386 goto repeat;
387 }
388 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
389 if (unlikely(vmf->pgoff >= size)) {
390 /*
391 * We have a struct page covering a hole in the file
392 * from a read fault and we've raced with a truncate
393 */
394 error = -EIO;
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700395 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800396 }
397 }
398
399 error = get_block(inode, block, &bh, 0);
400 if (!error && (bh.b_size < PAGE_SIZE))
401 error = -EIO; /* fs corruption? */
402 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700403 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800404
405 if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
406 if (vmf->flags & FAULT_FLAG_WRITE) {
407 error = get_block(inode, block, &bh, 1);
408 count_vm_event(PGMAJFAULT);
409 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
410 major = VM_FAULT_MAJOR;
411 if (!error && (bh.b_size < PAGE_SIZE))
412 error = -EIO;
413 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700414 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800415 } else {
416 return dax_load_hole(mapping, page, vmf);
417 }
418 }
419
420 if (vmf->cow_page) {
421 struct page *new_page = vmf->cow_page;
422 if (buffer_written(&bh))
423 error = copy_user_bh(new_page, &bh, blkbits, vaddr);
424 else
425 clear_user_highpage(new_page, vaddr);
426 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700427 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800428 vmf->page = page;
429 if (!page) {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700430 i_mmap_lock_read(mapping);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800431 /* Check we didn't race with truncate */
432 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
433 PAGE_SHIFT;
434 if (vmf->pgoff >= size) {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700435 i_mmap_unlock_read(mapping);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800436 error = -EIO;
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700437 goto out;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800438 }
439 }
440 return VM_FAULT_LOCKED;
441 }
442
443 /* Check we didn't race with a read fault installing a new page */
444 if (!page && major)
445 page = find_lock_page(mapping, vmf->pgoff);
446
447 if (page) {
448 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
449 PAGE_CACHE_SIZE, 0);
450 delete_from_page_cache(page);
451 unlock_page(page);
452 page_cache_release(page);
453 }
454
Dave Chinnere842f292015-06-04 09:18:18 +1000455 /*
456 * If we successfully insert the new mapping over an unwritten extent,
457 * we need to ensure we convert the unwritten extent. If there is an
458 * error inserting the mapping, the filesystem needs to leave it as
459 * unwritten to prevent exposure of the stale underlying data to
460 * userspace, but we still need to call the completion function so
461 * the private resources on the mapping buffer can be released. We
462 * indicate what the callback should do via the uptodate variable, same
463 * as for normal BH based IO completions.
464 */
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800465 error = dax_insert_mapping(inode, &bh, vma, vmf);
Dave Chinnerb2442c52015-07-29 11:48:00 +1000466 if (buffer_unwritten(&bh)) {
467 if (complete_unwritten)
468 complete_unwritten(&bh, !error);
469 else
470 WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
471 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800472
473 out:
474 if (error == -ENOMEM)
475 return VM_FAULT_OOM | major;
476 /* -EBUSY is fine, somebody else faulted on the same PTE */
477 if ((error < 0) && (error != -EBUSY))
478 return VM_FAULT_SIGBUS | major;
479 return VM_FAULT_NOPAGE | major;
480
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700481 unlock_page:
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800482 if (page) {
483 unlock_page(page);
484 page_cache_release(page);
485 }
486 goto out;
487}
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000488EXPORT_SYMBOL(__dax_fault);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800489
490/**
491 * dax_fault - handle a page fault on a DAX file
492 * @vma: The virtual memory area where the fault occurred
493 * @vmf: The description of the fault
494 * @get_block: The filesystem method used to translate file offsets to blocks
495 *
496 * When a page fault occurs, filesystems may call this helper in their
497 * fault handler for DAX files.
498 */
499int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Dave Chinnere842f292015-06-04 09:18:18 +1000500 get_block_t get_block, dax_iodone_t complete_unwritten)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800501{
502 int result;
503 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
504
505 if (vmf->flags & FAULT_FLAG_WRITE) {
506 sb_start_pagefault(sb);
507 file_update_time(vma->vm_file);
508 }
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000509 result = __dax_fault(vma, vmf, get_block, complete_unwritten);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800510 if (vmf->flags & FAULT_FLAG_WRITE)
511 sb_end_pagefault(sb);
512
513 return result;
514}
515EXPORT_SYMBOL_GPL(dax_fault);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800516
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700517#ifdef CONFIG_TRANSPARENT_HUGEPAGE
518/*
519 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
520 * more often than one might expect in the below function.
521 */
522#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
523
524int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
525 pmd_t *pmd, unsigned int flags, get_block_t get_block,
526 dax_iodone_t complete_unwritten)
527{
528 struct file *file = vma->vm_file;
529 struct address_space *mapping = file->f_mapping;
530 struct inode *inode = mapping->host;
531 struct buffer_head bh;
532 unsigned blkbits = inode->i_blkbits;
533 unsigned long pmd_addr = address & PMD_MASK;
534 bool write = flags & FAULT_FLAG_WRITE;
535 long length;
Ross Zwislerd77e92e2015-09-09 10:29:40 -0600536 void __pmem *kaddr;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700537 pgoff_t size, pgoff;
538 sector_t block, sector;
539 unsigned long pfn;
540 int result = 0;
541
542 /* Fall back to PTEs if we're going to COW */
543 if (write && !(vma->vm_flags & VM_SHARED))
544 return VM_FAULT_FALLBACK;
545 /* If the PMD would extend outside the VMA */
546 if (pmd_addr < vma->vm_start)
547 return VM_FAULT_FALLBACK;
548 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
549 return VM_FAULT_FALLBACK;
550
Matthew Wilcox3fdd1b472015-09-08 14:59:39 -0700551 pgoff = linear_page_index(vma, pmd_addr);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700552 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
553 if (pgoff >= size)
554 return VM_FAULT_SIGBUS;
555 /* If the PMD would cover blocks out of the file */
556 if ((pgoff | PG_PMD_COLOUR) >= size)
557 return VM_FAULT_FALLBACK;
558
559 memset(&bh, 0, sizeof(bh));
560 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
561
562 bh.b_size = PMD_SIZE;
563 length = get_block(inode, block, &bh, write);
564 if (length)
565 return VM_FAULT_SIGBUS;
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700566 i_mmap_lock_read(mapping);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700567
568 /*
569 * If the filesystem isn't willing to tell us the length of a hole,
570 * just fall back to PTEs. Calling get_block 512 times in a loop
571 * would be silly.
572 */
573 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE)
574 goto fallback;
575
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -0700576 /*
577 * If we allocated new storage, make sure no process has any
578 * zero pages covering this hole
579 */
580 if (buffer_new(&bh)) {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700581 i_mmap_unlock_read(mapping);
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -0700582 unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0);
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700583 i_mmap_lock_read(mapping);
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -0700584 }
585
Matthew Wilcox84c4e5e2015-09-08 14:59:17 -0700586 /*
587 * If a truncate happened while we were allocating blocks, we may
588 * leave blocks allocated to the file that are beyond EOF. We can't
589 * take i_mutex here, so just leave them hanging; they'll be freed
590 * when the file is deleted.
591 */
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700592 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
593 if (pgoff >= size) {
594 result = VM_FAULT_SIGBUS;
595 goto out;
596 }
597 if ((pgoff | PG_PMD_COLOUR) >= size)
598 goto fallback;
599
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700600 if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700601 spinlock_t *ptl;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700602 pmd_t entry;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700603 struct page *zero_page = get_huge_zero_page();
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700604
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700605 if (unlikely(!zero_page))
606 goto fallback;
607
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700608 ptl = pmd_lock(vma->vm_mm, pmd);
609 if (!pmd_none(*pmd)) {
610 spin_unlock(ptl);
611 goto fallback;
612 }
613
614 entry = mk_pmd(zero_page, vma->vm_page_prot);
615 entry = pmd_mkhuge(entry);
616 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700617 result = VM_FAULT_NOPAGE;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700618 spin_unlock(ptl);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700619 } else {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700620 sector = bh.b_blocknr << (blkbits - 9);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700621 length = bdev_direct_access(bh.b_bdev, sector, &kaddr, &pfn,
622 bh.b_size);
623 if (length < 0) {
624 result = VM_FAULT_SIGBUS;
625 goto out;
626 }
627 if ((length < PMD_SIZE) || (pfn & PG_PMD_COLOUR))
628 goto fallback;
629
Dan Williams152d7bd2015-11-12 18:33:54 -0800630 /*
631 * TODO: teach vmf_insert_pfn_pmd() to support
632 * 'pte_special' for pmds
633 */
634 if (pfn_valid(pfn))
635 goto fallback;
636
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700637 if (buffer_unwritten(&bh) || buffer_new(&bh)) {
638 int i;
639 for (i = 0; i < PTRS_PER_PMD; i++)
640 clear_pmem(kaddr + i * PAGE_SIZE, PAGE_SIZE);
641 wmb_pmem();
642 count_vm_event(PGMAJFAULT);
643 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
644 result |= VM_FAULT_MAJOR;
645 }
646
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700647 result |= vmf_insert_pfn_pmd(vma, address, pmd, pfn, write);
648 }
649
650 out:
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700651 i_mmap_unlock_read(mapping);
652
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700653 if (buffer_unwritten(&bh))
654 complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
655
656 return result;
657
658 fallback:
659 count_vm_event(THP_FAULT_FALLBACK);
660 result = VM_FAULT_FALLBACK;
661 goto out;
662}
663EXPORT_SYMBOL_GPL(__dax_pmd_fault);
664
665/**
666 * dax_pmd_fault - handle a PMD fault on a DAX file
667 * @vma: The virtual memory area where the fault occurred
668 * @vmf: The description of the fault
669 * @get_block: The filesystem method used to translate file offsets to blocks
670 *
671 * When a page fault occurs, filesystems may call this helper in their
672 * pmd_fault handler for DAX files.
673 */
674int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
675 pmd_t *pmd, unsigned int flags, get_block_t get_block,
676 dax_iodone_t complete_unwritten)
677{
678 int result;
679 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
680
681 if (flags & FAULT_FLAG_WRITE) {
682 sb_start_pagefault(sb);
683 file_update_time(vma->vm_file);
684 }
685 result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
686 complete_unwritten);
687 if (flags & FAULT_FLAG_WRITE)
688 sb_end_pagefault(sb);
689
690 return result;
691}
692EXPORT_SYMBOL_GPL(dax_pmd_fault);
Valentin Rothbergdd8a2b62015-09-08 14:59:09 -0700693#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700694
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800695/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700696 * dax_pfn_mkwrite - handle first write to DAX page
697 * @vma: The virtual memory area where the fault occurred
698 * @vmf: The description of the fault
699 *
700 */
701int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
702{
703 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
704
705 sb_start_pagefault(sb);
706 file_update_time(vma->vm_file);
707 sb_end_pagefault(sb);
708 return VM_FAULT_NOPAGE;
709}
710EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
711
712/**
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800713 * dax_zero_page_range - zero a range within a page of a DAX file
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800714 * @inode: The file being truncated
715 * @from: The file offset that is being truncated to
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800716 * @length: The number of bytes to zero
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800717 * @get_block: The filesystem method used to translate file offsets to blocks
718 *
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800719 * This function can be called by a filesystem when it is zeroing part of a
720 * page in a DAX file. This is intended for hole-punch operations. If
721 * you are truncating a file, the helper function dax_truncate_page() may be
722 * more convenient.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800723 *
724 * We work in terms of PAGE_CACHE_SIZE here for commonality with
725 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
726 * took care of disposing of the unnecessary blocks. Even if the filesystem
727 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800728 * since the file might be mmapped.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800729 */
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800730int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
731 get_block_t get_block)
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800732{
733 struct buffer_head bh;
734 pgoff_t index = from >> PAGE_CACHE_SHIFT;
735 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800736 int err;
737
738 /* Block boundary? Nothing to do */
739 if (!length)
740 return 0;
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800741 BUG_ON((offset + length) > PAGE_CACHE_SIZE);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800742
743 memset(&bh, 0, sizeof(bh));
744 bh.b_size = PAGE_CACHE_SIZE;
745 err = get_block(inode, index, &bh, 0);
746 if (err < 0)
747 return err;
748 if (buffer_written(&bh)) {
Ross Zwislere2e05392015-08-18 13:55:41 -0600749 void __pmem *addr;
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800750 err = dax_get_addr(&bh, &addr, inode->i_blkbits);
751 if (err < 0)
752 return err;
Ross Zwislere2e05392015-08-18 13:55:41 -0600753 clear_pmem(addr + offset, length);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600754 wmb_pmem();
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800755 }
756
757 return 0;
758}
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800759EXPORT_SYMBOL_GPL(dax_zero_page_range);
760
761/**
762 * dax_truncate_page - handle a partial page being truncated in a DAX file
763 * @inode: The file being truncated
764 * @from: The file offset that is being truncated to
765 * @get_block: The filesystem method used to translate file offsets to blocks
766 *
767 * Similar to block_truncate_page(), this function can be called by a
768 * filesystem when it is truncating a DAX file to handle the partial page.
769 *
770 * We work in terms of PAGE_CACHE_SIZE here for commonality with
771 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
772 * took care of disposing of the unnecessary blocks. Even if the filesystem
773 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
774 * since the file might be mmapped.
775 */
776int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
777{
778 unsigned length = PAGE_CACHE_ALIGN(from) - from;
779 return dax_zero_page_range(inode, from, length, get_block);
780}
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800781EXPORT_SYMBOL_GPL(dax_truncate_page);