]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/dax.c
dax: guarantee page aligned results from bdev_direct_access()
[mirror_ubuntu-artful-kernel.git] / fs / dax.c
CommitLineData
d475c634
MW
1/*
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>
d77e92e2 20#include <linux/dax.h>
d475c634
MW
21#include <linux/fs.h>
22#include <linux/genhd.h>
f7ca90b1
MW
23#include <linux/highmem.h>
24#include <linux/memcontrol.h>
25#include <linux/mm.h>
d475c634 26#include <linux/mutex.h>
2765cfbb 27#include <linux/pmem.h>
289c6aed 28#include <linux/sched.h>
d475c634 29#include <linux/uio.h>
f7ca90b1 30#include <linux/vmstat.h>
0e749e54 31#include <linux/sizes.h>
d475c634 32
1ca19157
DC
33/*
34 * dax_clear_blocks() is called from within transaction context from XFS,
35 * and hence this means the stack from this point must follow GFP_NOFS
36 * semantics for all operations.
37 */
289c6aed
MW
38int dax_clear_blocks(struct inode *inode, sector_t block, long size)
39{
40 struct block_device *bdev = inode->i_sb->s_bdev;
41 sector_t sector = block << (inode->i_blkbits - 9);
42
43 might_sleep();
44 do {
e2e05394 45 void __pmem *addr;
289c6aed 46 unsigned long pfn;
0e749e54 47 long count, sz;
289c6aed
MW
48
49 count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
50 if (count < 0)
51 return count;
0e749e54
DW
52 sz = min_t(long, count, SZ_128K);
53 clear_pmem(addr, sz);
54 size -= sz;
0e749e54
DW
55 sector += sz / 512;
56 cond_resched();
289c6aed
MW
57 } while (size);
58
2765cfbb 59 wmb_pmem();
289c6aed
MW
60 return 0;
61}
62EXPORT_SYMBOL_GPL(dax_clear_blocks);
63
e2e05394
RZ
64static long dax_get_addr(struct buffer_head *bh, void __pmem **addr,
65 unsigned blkbits)
d475c634
MW
66{
67 unsigned long pfn;
68 sector_t sector = bh->b_blocknr << (blkbits - 9);
69 return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
70}
71
2765cfbb 72/* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
e2e05394
RZ
73static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
74 loff_t pos, loff_t end)
d475c634
MW
75{
76 loff_t final = end - pos + first; /* The final byte of the buffer */
77
78 if (first > 0)
e2e05394 79 clear_pmem(addr, first);
d475c634 80 if (final < size)
e2e05394 81 clear_pmem(addr + final, size - final);
d475c634
MW
82}
83
84static bool buffer_written(struct buffer_head *bh)
85{
86 return buffer_mapped(bh) && !buffer_unwritten(bh);
87}
88
89/*
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.
95 */
96static bool buffer_size_valid(struct buffer_head *bh)
97{
98 return bh->b_state != 0;
99}
100
a95cd631
OS
101static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
102 loff_t start, loff_t end, get_block_t get_block,
103 struct buffer_head *bh)
d475c634
MW
104{
105 ssize_t retval = 0;
106 loff_t pos = start;
107 loff_t max = start;
108 loff_t bh_max = start;
e2e05394 109 void __pmem *addr;
d475c634 110 bool hole = false;
2765cfbb 111 bool need_wmb = false;
d475c634 112
a95cd631 113 if (iov_iter_rw(iter) != WRITE)
d475c634
MW
114 end = min(end, i_size_read(inode));
115
116 while (pos < end) {
2765cfbb 117 size_t len;
d475c634
MW
118 if (pos == max) {
119 unsigned blkbits = inode->i_blkbits;
e94f5a22
JM
120 long page = pos >> PAGE_SHIFT;
121 sector_t block = page << (PAGE_SHIFT - blkbits);
d475c634
MW
122 unsigned first = pos - (block << blkbits);
123 long size;
124
125 if (pos == bh_max) {
126 bh->b_size = PAGE_ALIGN(end - pos);
127 bh->b_state = 0;
128 retval = get_block(inode, block, bh,
a95cd631 129 iov_iter_rw(iter) == WRITE);
d475c634
MW
130 if (retval)
131 break;
132 if (!buffer_size_valid(bh))
133 bh->b_size = 1 << blkbits;
134 bh_max = pos - first + bh->b_size;
135 } else {
136 unsigned done = bh->b_size -
137 (bh_max - (pos - first));
138 bh->b_blocknr += done >> blkbits;
139 bh->b_size -= done;
140 }
141
a95cd631 142 hole = iov_iter_rw(iter) != WRITE && !buffer_written(bh);
d475c634
MW
143 if (hole) {
144 addr = NULL;
145 size = bh->b_size - first;
146 } else {
147 retval = dax_get_addr(bh, &addr, blkbits);
148 if (retval < 0)
149 break;
2765cfbb 150 if (buffer_unwritten(bh) || buffer_new(bh)) {
d475c634
MW
151 dax_new_buf(addr, retval, first, pos,
152 end);
2765cfbb
RZ
153 need_wmb = true;
154 }
d475c634
MW
155 addr += first;
156 size = retval - first;
157 }
158 max = min(pos + size, end);
159 }
160
2765cfbb 161 if (iov_iter_rw(iter) == WRITE) {
e2e05394 162 len = copy_from_iter_pmem(addr, max - pos, iter);
2765cfbb
RZ
163 need_wmb = true;
164 } else if (!hole)
e2e05394
RZ
165 len = copy_to_iter((void __force *)addr, max - pos,
166 iter);
d475c634
MW
167 else
168 len = iov_iter_zero(max - pos, iter);
169
cadfbb6e
AV
170 if (!len) {
171 retval = -EFAULT;
d475c634 172 break;
cadfbb6e 173 }
d475c634
MW
174
175 pos += len;
176 addr += len;
177 }
178
2765cfbb
RZ
179 if (need_wmb)
180 wmb_pmem();
181
d475c634
MW
182 return (pos == start) ? retval : pos - start;
183}
184
185/**
186 * dax_do_io - Perform I/O to a DAX file
d475c634
MW
187 * @iocb: The control block for this I/O
188 * @inode: The file which the I/O is directed at
189 * @iter: The addresses to do I/O from or to
190 * @pos: The file offset where the I/O starts
191 * @get_block: The filesystem method used to translate file offsets to blocks
192 * @end_io: A filesystem callback for I/O completion
193 * @flags: See below
194 *
195 * This function uses the same locking scheme as do_blockdev_direct_IO:
196 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
197 * caller for writes. For reads, we take and release the i_mutex ourselves.
198 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
199 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
200 * is in progress.
201 */
a95cd631
OS
202ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
203 struct iov_iter *iter, loff_t pos, get_block_t get_block,
204 dio_iodone_t end_io, int flags)
d475c634
MW
205{
206 struct buffer_head bh;
207 ssize_t retval = -EINVAL;
208 loff_t end = pos + iov_iter_count(iter);
209
210 memset(&bh, 0, sizeof(bh));
211
a95cd631 212 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
d475c634
MW
213 struct address_space *mapping = inode->i_mapping;
214 mutex_lock(&inode->i_mutex);
215 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
216 if (retval) {
217 mutex_unlock(&inode->i_mutex);
218 goto out;
219 }
220 }
221
222 /* Protects against truncate */
bbab37dd
MW
223 if (!(flags & DIO_SKIP_DIO_COUNT))
224 inode_dio_begin(inode);
d475c634 225
a95cd631 226 retval = dax_io(inode, iter, pos, end, get_block, &bh);
d475c634 227
a95cd631 228 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
d475c634
MW
229 mutex_unlock(&inode->i_mutex);
230
231 if ((retval > 0) && end_io)
232 end_io(iocb, pos, retval, bh.b_private);
233
bbab37dd
MW
234 if (!(flags & DIO_SKIP_DIO_COUNT))
235 inode_dio_end(inode);
d475c634
MW
236 out:
237 return retval;
238}
239EXPORT_SYMBOL_GPL(dax_do_io);
f7ca90b1
MW
240
241/*
242 * The user has performed a load from a hole in the file. Allocating
243 * a new page in the file would cause excessive storage usage for
244 * workloads with sparse files. We allocate a page cache page instead.
245 * We'll kick it out of the page cache if it's ever written to,
246 * otherwise it will simply fall out of the page cache under memory
247 * pressure without ever having been dirtied.
248 */
249static int dax_load_hole(struct address_space *mapping, struct page *page,
250 struct vm_fault *vmf)
251{
252 unsigned long size;
253 struct inode *inode = mapping->host;
254 if (!page)
255 page = find_or_create_page(mapping, vmf->pgoff,
256 GFP_KERNEL | __GFP_ZERO);
257 if (!page)
258 return VM_FAULT_OOM;
259 /* Recheck i_size under page lock to avoid truncate race */
260 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
261 if (vmf->pgoff >= size) {
262 unlock_page(page);
263 page_cache_release(page);
264 return VM_FAULT_SIGBUS;
265 }
266
267 vmf->page = page;
268 return VM_FAULT_LOCKED;
269}
270
271static int copy_user_bh(struct page *to, struct buffer_head *bh,
272 unsigned blkbits, unsigned long vaddr)
273{
e2e05394
RZ
274 void __pmem *vfrom;
275 void *vto;
276
f7ca90b1
MW
277 if (dax_get_addr(bh, &vfrom, blkbits) < 0)
278 return -EIO;
279 vto = kmap_atomic(to);
e2e05394 280 copy_user_page(vto, (void __force *)vfrom, vaddr, to);
f7ca90b1
MW
281 kunmap_atomic(vto);
282 return 0;
283}
284
285static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
286 struct vm_area_struct *vma, struct vm_fault *vmf)
287{
0f90cc66 288 struct address_space *mapping = inode->i_mapping;
f7ca90b1
MW
289 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
290 unsigned long vaddr = (unsigned long)vmf->virtual_address;
e2e05394 291 void __pmem *addr;
f7ca90b1
MW
292 unsigned long pfn;
293 pgoff_t size;
294 int error;
295
0f90cc66
RZ
296 i_mmap_lock_read(mapping);
297
f7ca90b1
MW
298 /*
299 * Check truncate didn't happen while we were allocating a block.
300 * If it did, this block may or may not be still allocated to the
301 * file. We can't tell the filesystem to free it because we can't
302 * take i_mutex here. In the worst case, the file still has blocks
303 * allocated past the end of the file.
304 */
305 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
306 if (unlikely(vmf->pgoff >= size)) {
307 error = -EIO;
308 goto out;
309 }
310
311 error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
312 if (error < 0)
313 goto out;
314 if (error < PAGE_SIZE) {
315 error = -EIO;
316 goto out;
317 }
318
2765cfbb 319 if (buffer_unwritten(bh) || buffer_new(bh)) {
e2e05394 320 clear_pmem(addr, PAGE_SIZE);
2765cfbb
RZ
321 wmb_pmem();
322 }
f7ca90b1
MW
323
324 error = vm_insert_mixed(vma, vaddr, pfn);
325
326 out:
0f90cc66
RZ
327 i_mmap_unlock_read(mapping);
328
f7ca90b1
MW
329 return error;
330}
331
ce5c5d55
DC
332/**
333 * __dax_fault - handle a page fault on a DAX file
334 * @vma: The virtual memory area where the fault occurred
335 * @vmf: The description of the fault
336 * @get_block: The filesystem method used to translate file offsets to blocks
b2442c5a
DC
337 * @complete_unwritten: The filesystem method used to convert unwritten blocks
338 * to written so the data written to them is exposed. This is required for
339 * required by write faults for filesystems that will return unwritten
340 * extent mappings from @get_block, but it is optional for reads as
341 * dax_insert_mapping() will always zero unwritten blocks. If the fs does
342 * not support unwritten extents, the it should pass NULL.
ce5c5d55
DC
343 *
344 * When a page fault occurs, filesystems may call this helper in their
345 * fault handler for DAX files. __dax_fault() assumes the caller has done all
346 * the necessary locking for the page fault to proceed successfully.
347 */
348int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
e842f290 349 get_block_t get_block, dax_iodone_t complete_unwritten)
f7ca90b1
MW
350{
351 struct file *file = vma->vm_file;
352 struct address_space *mapping = file->f_mapping;
353 struct inode *inode = mapping->host;
354 struct page *page;
355 struct buffer_head bh;
356 unsigned long vaddr = (unsigned long)vmf->virtual_address;
357 unsigned blkbits = inode->i_blkbits;
358 sector_t block;
359 pgoff_t size;
360 int error;
361 int major = 0;
362
363 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
364 if (vmf->pgoff >= size)
365 return VM_FAULT_SIGBUS;
366
367 memset(&bh, 0, sizeof(bh));
368 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
369 bh.b_size = PAGE_SIZE;
370
371 repeat:
372 page = find_get_page(mapping, vmf->pgoff);
373 if (page) {
374 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
375 page_cache_release(page);
376 return VM_FAULT_RETRY;
377 }
378 if (unlikely(page->mapping != mapping)) {
379 unlock_page(page);
380 page_cache_release(page);
381 goto repeat;
382 }
383 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
384 if (unlikely(vmf->pgoff >= size)) {
385 /*
386 * We have a struct page covering a hole in the file
387 * from a read fault and we've raced with a truncate
388 */
389 error = -EIO;
0f90cc66 390 goto unlock_page;
f7ca90b1
MW
391 }
392 }
393
394 error = get_block(inode, block, &bh, 0);
395 if (!error && (bh.b_size < PAGE_SIZE))
396 error = -EIO; /* fs corruption? */
397 if (error)
0f90cc66 398 goto unlock_page;
f7ca90b1
MW
399
400 if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
401 if (vmf->flags & FAULT_FLAG_WRITE) {
402 error = get_block(inode, block, &bh, 1);
403 count_vm_event(PGMAJFAULT);
404 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
405 major = VM_FAULT_MAJOR;
406 if (!error && (bh.b_size < PAGE_SIZE))
407 error = -EIO;
408 if (error)
0f90cc66 409 goto unlock_page;
f7ca90b1
MW
410 } else {
411 return dax_load_hole(mapping, page, vmf);
412 }
413 }
414
415 if (vmf->cow_page) {
416 struct page *new_page = vmf->cow_page;
417 if (buffer_written(&bh))
418 error = copy_user_bh(new_page, &bh, blkbits, vaddr);
419 else
420 clear_user_highpage(new_page, vaddr);
421 if (error)
0f90cc66 422 goto unlock_page;
f7ca90b1
MW
423 vmf->page = page;
424 if (!page) {
0f90cc66 425 i_mmap_lock_read(mapping);
f7ca90b1
MW
426 /* Check we didn't race with truncate */
427 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
428 PAGE_SHIFT;
429 if (vmf->pgoff >= size) {
0f90cc66 430 i_mmap_unlock_read(mapping);
f7ca90b1 431 error = -EIO;
0f90cc66 432 goto out;
f7ca90b1
MW
433 }
434 }
435 return VM_FAULT_LOCKED;
436 }
437
438 /* Check we didn't race with a read fault installing a new page */
439 if (!page && major)
440 page = find_lock_page(mapping, vmf->pgoff);
441
442 if (page) {
443 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
444 PAGE_CACHE_SIZE, 0);
445 delete_from_page_cache(page);
446 unlock_page(page);
447 page_cache_release(page);
448 }
449
e842f290
DC
450 /*
451 * If we successfully insert the new mapping over an unwritten extent,
452 * we need to ensure we convert the unwritten extent. If there is an
453 * error inserting the mapping, the filesystem needs to leave it as
454 * unwritten to prevent exposure of the stale underlying data to
455 * userspace, but we still need to call the completion function so
456 * the private resources on the mapping buffer can be released. We
457 * indicate what the callback should do via the uptodate variable, same
458 * as for normal BH based IO completions.
459 */
f7ca90b1 460 error = dax_insert_mapping(inode, &bh, vma, vmf);
b2442c5a
DC
461 if (buffer_unwritten(&bh)) {
462 if (complete_unwritten)
463 complete_unwritten(&bh, !error);
464 else
465 WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
466 }
f7ca90b1
MW
467
468 out:
469 if (error == -ENOMEM)
470 return VM_FAULT_OOM | major;
471 /* -EBUSY is fine, somebody else faulted on the same PTE */
472 if ((error < 0) && (error != -EBUSY))
473 return VM_FAULT_SIGBUS | major;
474 return VM_FAULT_NOPAGE | major;
475
0f90cc66 476 unlock_page:
f7ca90b1
MW
477 if (page) {
478 unlock_page(page);
479 page_cache_release(page);
480 }
481 goto out;
482}
ce5c5d55 483EXPORT_SYMBOL(__dax_fault);
f7ca90b1
MW
484
485/**
486 * dax_fault - handle a page fault on a DAX file
487 * @vma: The virtual memory area where the fault occurred
488 * @vmf: The description of the fault
489 * @get_block: The filesystem method used to translate file offsets to blocks
490 *
491 * When a page fault occurs, filesystems may call this helper in their
492 * fault handler for DAX files.
493 */
494int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
e842f290 495 get_block_t get_block, dax_iodone_t complete_unwritten)
f7ca90b1
MW
496{
497 int result;
498 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
499
500 if (vmf->flags & FAULT_FLAG_WRITE) {
501 sb_start_pagefault(sb);
502 file_update_time(vma->vm_file);
503 }
ce5c5d55 504 result = __dax_fault(vma, vmf, get_block, complete_unwritten);
f7ca90b1
MW
505 if (vmf->flags & FAULT_FLAG_WRITE)
506 sb_end_pagefault(sb);
507
508 return result;
509}
510EXPORT_SYMBOL_GPL(dax_fault);
4c0ccfef 511
844f35db
MW
512#ifdef CONFIG_TRANSPARENT_HUGEPAGE
513/*
514 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
515 * more often than one might expect in the below function.
516 */
517#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
518
519int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
520 pmd_t *pmd, unsigned int flags, get_block_t get_block,
521 dax_iodone_t complete_unwritten)
522{
523 struct file *file = vma->vm_file;
524 struct address_space *mapping = file->f_mapping;
525 struct inode *inode = mapping->host;
526 struct buffer_head bh;
527 unsigned blkbits = inode->i_blkbits;
528 unsigned long pmd_addr = address & PMD_MASK;
529 bool write = flags & FAULT_FLAG_WRITE;
530 long length;
d77e92e2 531 void __pmem *kaddr;
844f35db
MW
532 pgoff_t size, pgoff;
533 sector_t block, sector;
534 unsigned long pfn;
535 int result = 0;
536
ee82c9ed
DW
537 /* dax pmd mappings are broken wrt gup and fork */
538 if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
539 return VM_FAULT_FALLBACK;
540
844f35db
MW
541 /* Fall back to PTEs if we're going to COW */
542 if (write && !(vma->vm_flags & VM_SHARED))
543 return VM_FAULT_FALLBACK;
544 /* If the PMD would extend outside the VMA */
545 if (pmd_addr < vma->vm_start)
546 return VM_FAULT_FALLBACK;
547 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
548 return VM_FAULT_FALLBACK;
549
3fdd1b47 550 pgoff = linear_page_index(vma, pmd_addr);
844f35db
MW
551 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
552 if (pgoff >= size)
553 return VM_FAULT_SIGBUS;
554 /* If the PMD would cover blocks out of the file */
555 if ((pgoff | PG_PMD_COLOUR) >= size)
556 return VM_FAULT_FALLBACK;
557
558 memset(&bh, 0, sizeof(bh));
559 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
560
561 bh.b_size = PMD_SIZE;
562 length = get_block(inode, block, &bh, write);
563 if (length)
564 return VM_FAULT_SIGBUS;
0f90cc66 565 i_mmap_lock_read(mapping);
844f35db
MW
566
567 /*
568 * If the filesystem isn't willing to tell us the length of a hole,
569 * just fall back to PTEs. Calling get_block 512 times in a loop
570 * would be silly.
571 */
572 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE)
573 goto fallback;
574
46c043ed
KS
575 /*
576 * If we allocated new storage, make sure no process has any
577 * zero pages covering this hole
578 */
579 if (buffer_new(&bh)) {
0f90cc66 580 i_mmap_unlock_read(mapping);
46c043ed 581 unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0);
0f90cc66 582 i_mmap_lock_read(mapping);
46c043ed
KS
583 }
584
84c4e5e6
MW
585 /*
586 * If a truncate happened while we were allocating blocks, we may
587 * leave blocks allocated to the file that are beyond EOF. We can't
588 * take i_mutex here, so just leave them hanging; they'll be freed
589 * when the file is deleted.
590 */
844f35db
MW
591 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
592 if (pgoff >= size) {
593 result = VM_FAULT_SIGBUS;
594 goto out;
595 }
596 if ((pgoff | PG_PMD_COLOUR) >= size)
597 goto fallback;
598
844f35db 599 if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
844f35db 600 spinlock_t *ptl;
d295e341 601 pmd_t entry;
844f35db 602 struct page *zero_page = get_huge_zero_page();
d295e341 603
844f35db
MW
604 if (unlikely(!zero_page))
605 goto fallback;
606
d295e341
KS
607 ptl = pmd_lock(vma->vm_mm, pmd);
608 if (!pmd_none(*pmd)) {
609 spin_unlock(ptl);
610 goto fallback;
611 }
612
613 entry = mk_pmd(zero_page, vma->vm_page_prot);
614 entry = pmd_mkhuge(entry);
615 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
844f35db 616 result = VM_FAULT_NOPAGE;
d295e341 617 spin_unlock(ptl);
844f35db 618 } else {
0f90cc66 619 sector = bh.b_blocknr << (blkbits - 9);
844f35db
MW
620 length = bdev_direct_access(bh.b_bdev, sector, &kaddr, &pfn,
621 bh.b_size);
622 if (length < 0) {
623 result = VM_FAULT_SIGBUS;
624 goto out;
625 }
626 if ((length < PMD_SIZE) || (pfn & PG_PMD_COLOUR))
627 goto fallback;
628
152d7bd8
DW
629 /*
630 * TODO: teach vmf_insert_pfn_pmd() to support
631 * 'pte_special' for pmds
632 */
633 if (pfn_valid(pfn))
634 goto fallback;
635
0f90cc66 636 if (buffer_unwritten(&bh) || buffer_new(&bh)) {
52db400f 637 clear_pmem(kaddr, PMD_SIZE);
0f90cc66
RZ
638 wmb_pmem();
639 count_vm_event(PGMAJFAULT);
640 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
641 result |= VM_FAULT_MAJOR;
642 }
643
844f35db
MW
644 result |= vmf_insert_pfn_pmd(vma, address, pmd, pfn, write);
645 }
646
647 out:
0f90cc66
RZ
648 i_mmap_unlock_read(mapping);
649
844f35db
MW
650 if (buffer_unwritten(&bh))
651 complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
652
653 return result;
654
655 fallback:
656 count_vm_event(THP_FAULT_FALLBACK);
657 result = VM_FAULT_FALLBACK;
658 goto out;
659}
660EXPORT_SYMBOL_GPL(__dax_pmd_fault);
661
662/**
663 * dax_pmd_fault - handle a PMD fault on a DAX file
664 * @vma: The virtual memory area where the fault occurred
665 * @vmf: The description of the fault
666 * @get_block: The filesystem method used to translate file offsets to blocks
667 *
668 * When a page fault occurs, filesystems may call this helper in their
669 * pmd_fault handler for DAX files.
670 */
671int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
672 pmd_t *pmd, unsigned int flags, get_block_t get_block,
673 dax_iodone_t complete_unwritten)
674{
675 int result;
676 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
677
678 if (flags & FAULT_FLAG_WRITE) {
679 sb_start_pagefault(sb);
680 file_update_time(vma->vm_file);
681 }
682 result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
683 complete_unwritten);
684 if (flags & FAULT_FLAG_WRITE)
685 sb_end_pagefault(sb);
686
687 return result;
688}
689EXPORT_SYMBOL_GPL(dax_pmd_fault);
dd8a2b6c 690#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
844f35db 691
0e3b210c
BH
692/**
693 * dax_pfn_mkwrite - handle first write to DAX page
694 * @vma: The virtual memory area where the fault occurred
695 * @vmf: The description of the fault
696 *
697 */
698int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
699{
700 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
701
702 sb_start_pagefault(sb);
703 file_update_time(vma->vm_file);
704 sb_end_pagefault(sb);
705 return VM_FAULT_NOPAGE;
706}
707EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
708
4c0ccfef 709/**
25726bc1 710 * dax_zero_page_range - zero a range within a page of a DAX file
4c0ccfef
MW
711 * @inode: The file being truncated
712 * @from: The file offset that is being truncated to
25726bc1 713 * @length: The number of bytes to zero
4c0ccfef
MW
714 * @get_block: The filesystem method used to translate file offsets to blocks
715 *
25726bc1
MW
716 * This function can be called by a filesystem when it is zeroing part of a
717 * page in a DAX file. This is intended for hole-punch operations. If
718 * you are truncating a file, the helper function dax_truncate_page() may be
719 * more convenient.
4c0ccfef
MW
720 *
721 * We work in terms of PAGE_CACHE_SIZE here for commonality with
722 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
723 * took care of disposing of the unnecessary blocks. Even if the filesystem
724 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
25726bc1 725 * since the file might be mmapped.
4c0ccfef 726 */
25726bc1
MW
727int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
728 get_block_t get_block)
4c0ccfef
MW
729{
730 struct buffer_head bh;
731 pgoff_t index = from >> PAGE_CACHE_SHIFT;
732 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4c0ccfef
MW
733 int err;
734
735 /* Block boundary? Nothing to do */
736 if (!length)
737 return 0;
25726bc1 738 BUG_ON((offset + length) > PAGE_CACHE_SIZE);
4c0ccfef
MW
739
740 memset(&bh, 0, sizeof(bh));
741 bh.b_size = PAGE_CACHE_SIZE;
742 err = get_block(inode, index, &bh, 0);
743 if (err < 0)
744 return err;
745 if (buffer_written(&bh)) {
e2e05394 746 void __pmem *addr;
4c0ccfef
MW
747 err = dax_get_addr(&bh, &addr, inode->i_blkbits);
748 if (err < 0)
749 return err;
e2e05394 750 clear_pmem(addr + offset, length);
2765cfbb 751 wmb_pmem();
4c0ccfef
MW
752 }
753
754 return 0;
755}
25726bc1
MW
756EXPORT_SYMBOL_GPL(dax_zero_page_range);
757
758/**
759 * dax_truncate_page - handle a partial page being truncated in a DAX file
760 * @inode: The file being truncated
761 * @from: The file offset that is being truncated to
762 * @get_block: The filesystem method used to translate file offsets to blocks
763 *
764 * Similar to block_truncate_page(), this function can be called by a
765 * filesystem when it is truncating a DAX file to handle the partial page.
766 *
767 * We work in terms of PAGE_CACHE_SIZE here for commonality with
768 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
769 * took care of disposing of the unnecessary blocks. Even if the filesystem
770 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
771 * since the file might be mmapped.
772 */
773int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
774{
775 unsigned length = PAGE_CACHE_ALIGN(from) - from;
776 return dax_zero_page_range(inode, from, length, get_block);
777}
4c0ccfef 778EXPORT_SYMBOL_GPL(dax_truncate_page);