blob: 83e7894d86d853574e9863a3239bae7344fdf6e1 [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 Zwisler9973c982016-01-22 15:10:47 -080027#include <linux/pagevec.h>
Ross Zwisler2765cfb2015-08-18 13:55:40 -060028#include <linux/pmem.h>
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080029#include <linux/sched.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080030#include <linux/uio.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080031#include <linux/vmstat.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080032#include <linux/pfn_t.h>
Dan Williams0e749e52016-01-15 16:55:53 -080033#include <linux/sizes.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080034
NeilBrowne4b27492016-05-11 11:58:47 +020035#define RADIX_DAX_MASK 0xf
36#define RADIX_DAX_SHIFT 4
37#define RADIX_DAX_PTE (0x4 | RADIX_TREE_EXCEPTIONAL_ENTRY)
38#define RADIX_DAX_PMD (0x8 | RADIX_TREE_EXCEPTIONAL_ENTRY)
39#define RADIX_DAX_TYPE(entry) ((unsigned long)entry & RADIX_DAX_MASK)
40#define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT))
41#define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \
42 RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE)))
43
Dan Williamsb2e0d162016-01-15 16:55:59 -080044static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
45{
46 struct request_queue *q = bdev->bd_queue;
47 long rc = -EIO;
48
49 dax->addr = (void __pmem *) ERR_PTR(-EIO);
50 if (blk_queue_enter(q, true) != 0)
51 return rc;
52
53 rc = bdev_direct_access(bdev, dax);
54 if (rc < 0) {
55 dax->addr = (void __pmem *) ERR_PTR(rc);
56 blk_queue_exit(q);
57 return rc;
58 }
59 return rc;
60}
61
62static void dax_unmap_atomic(struct block_device *bdev,
63 const struct blk_dax_ctl *dax)
64{
65 if (IS_ERR(dax->addr))
66 return;
67 blk_queue_exit(bdev->bd_queue);
68}
69
Dan Williamsd1a5f2b42016-01-28 20:25:31 -080070struct page *read_dax_sector(struct block_device *bdev, sector_t n)
71{
72 struct page *page = alloc_pages(GFP_KERNEL, 0);
73 struct blk_dax_ctl dax = {
74 .size = PAGE_SIZE,
75 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
76 };
77 long rc;
78
79 if (!page)
80 return ERR_PTR(-ENOMEM);
81
82 rc = dax_map_atomic(bdev, &dax);
83 if (rc < 0)
84 return ERR_PTR(rc);
85 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
86 dax_unmap_atomic(bdev, &dax);
87 return page;
88}
89
Dave Chinner1ca19152015-11-03 12:37:00 +110090/*
Ross Zwisler20a90f52016-02-26 15:19:52 -080091 * dax_clear_sectors() is called from within transaction context from XFS,
Dave Chinner1ca19152015-11-03 12:37:00 +110092 * and hence this means the stack from this point must follow GFP_NOFS
93 * semantics for all operations.
94 */
Ross Zwisler20a90f52016-02-26 15:19:52 -080095int dax_clear_sectors(struct block_device *bdev, sector_t _sector, long _size)
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080096{
Dan Williamsb2e0d162016-01-15 16:55:59 -080097 struct blk_dax_ctl dax = {
Ross Zwisler20a90f52016-02-26 15:19:52 -080098 .sector = _sector,
Dan Williamsb2e0d162016-01-15 16:55:59 -080099 .size = _size,
100 };
Matthew Wilcox289c6ae2015-02-16 15:58:59 -0800101
102 might_sleep();
103 do {
Dan Williams0e749e52016-01-15 16:55:53 -0800104 long count, sz;
Matthew Wilcox289c6ae2015-02-16 15:58:59 -0800105
Dan Williamsb2e0d162016-01-15 16:55:59 -0800106 count = dax_map_atomic(bdev, &dax);
Matthew Wilcox289c6ae2015-02-16 15:58:59 -0800107 if (count < 0)
108 return count;
Dan Williams0e749e52016-01-15 16:55:53 -0800109 sz = min_t(long, count, SZ_128K);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800110 clear_pmem(dax.addr, sz);
111 dax.size -= sz;
112 dax.sector += sz / 512;
113 dax_unmap_atomic(bdev, &dax);
Dan Williams0e749e52016-01-15 16:55:53 -0800114 cond_resched();
Dan Williamsb2e0d162016-01-15 16:55:59 -0800115 } while (dax.size);
Matthew Wilcox289c6ae2015-02-16 15:58:59 -0800116
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600117 wmb_pmem();
Matthew Wilcox289c6ae2015-02-16 15:58:59 -0800118 return 0;
119}
Ross Zwisler20a90f52016-02-26 15:19:52 -0800120EXPORT_SYMBOL_GPL(dax_clear_sectors);
Matthew Wilcox289c6ae2015-02-16 15:58:59 -0800121
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600122/* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
Ross Zwislere2e05392015-08-18 13:55:41 -0600123static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
124 loff_t pos, loff_t end)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800125{
126 loff_t final = end - pos + first; /* The final byte of the buffer */
127
128 if (first > 0)
Ross Zwislere2e05392015-08-18 13:55:41 -0600129 clear_pmem(addr, first);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800130 if (final < size)
Ross Zwislere2e05392015-08-18 13:55:41 -0600131 clear_pmem(addr + final, size - final);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800132}
133
134static bool buffer_written(struct buffer_head *bh)
135{
136 return buffer_mapped(bh) && !buffer_unwritten(bh);
137}
138
139/*
140 * When ext4 encounters a hole, it returns without modifying the buffer_head
141 * which means that we can't trust b_size. To cope with this, we set b_state
142 * to 0 before calling get_block and, if any bit is set, we know we can trust
143 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
144 * and would save us time calling get_block repeatedly.
145 */
146static bool buffer_size_valid(struct buffer_head *bh)
147{
148 return bh->b_state != 0;
149}
150
Dan Williamsb2e0d162016-01-15 16:55:59 -0800151
152static sector_t to_sector(const struct buffer_head *bh,
153 const struct inode *inode)
154{
155 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
156
157 return sector;
158}
159
Omar Sandovala95cd632015-03-16 04:33:51 -0700160static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
161 loff_t start, loff_t end, get_block_t get_block,
162 struct buffer_head *bh)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800163{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800164 loff_t pos = start, max = start, bh_max = start;
165 bool hole = false, need_wmb = false;
166 struct block_device *bdev = NULL;
167 int rw = iov_iter_rw(iter), rc;
168 long map_len = 0;
169 struct blk_dax_ctl dax = {
170 .addr = (void __pmem *) ERR_PTR(-EIO),
171 };
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800172
Dan Williamsb2e0d162016-01-15 16:55:59 -0800173 if (rw == READ)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800174 end = min(end, i_size_read(inode));
175
176 while (pos < end) {
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600177 size_t len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800178 if (pos == max) {
179 unsigned blkbits = inode->i_blkbits;
Jeff Moyere94f5a22015-08-14 16:15:31 -0400180 long page = pos >> PAGE_SHIFT;
181 sector_t block = page << (PAGE_SHIFT - blkbits);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800182 unsigned first = pos - (block << blkbits);
183 long size;
184
185 if (pos == bh_max) {
186 bh->b_size = PAGE_ALIGN(end - pos);
187 bh->b_state = 0;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800188 rc = get_block(inode, block, bh, rw == WRITE);
189 if (rc)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800190 break;
191 if (!buffer_size_valid(bh))
192 bh->b_size = 1 << blkbits;
193 bh_max = pos - first + bh->b_size;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800194 bdev = bh->b_bdev;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800195 } else {
196 unsigned done = bh->b_size -
197 (bh_max - (pos - first));
198 bh->b_blocknr += done >> blkbits;
199 bh->b_size -= done;
200 }
201
Dan Williamsb2e0d162016-01-15 16:55:59 -0800202 hole = rw == READ && !buffer_written(bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800203 if (hole) {
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800204 size = bh->b_size - first;
205 } else {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800206 dax_unmap_atomic(bdev, &dax);
207 dax.sector = to_sector(bh, inode);
208 dax.size = bh->b_size;
209 map_len = dax_map_atomic(bdev, &dax);
210 if (map_len < 0) {
211 rc = map_len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800212 break;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800213 }
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600214 if (buffer_unwritten(bh) || buffer_new(bh)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800215 dax_new_buf(dax.addr, map_len, first,
216 pos, end);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600217 need_wmb = true;
218 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800219 dax.addr += first;
220 size = map_len - first;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800221 }
222 max = min(pos + size, end);
223 }
224
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600225 if (iov_iter_rw(iter) == WRITE) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800226 len = copy_from_iter_pmem(dax.addr, max - pos, iter);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600227 need_wmb = true;
228 } else if (!hole)
Dan Williamsb2e0d162016-01-15 16:55:59 -0800229 len = copy_to_iter((void __force *) dax.addr, max - pos,
Ross Zwislere2e05392015-08-18 13:55:41 -0600230 iter);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800231 else
232 len = iov_iter_zero(max - pos, iter);
233
Al Virocadfbb62015-11-10 19:42:49 -0700234 if (!len) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800235 rc = -EFAULT;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800236 break;
Al Virocadfbb62015-11-10 19:42:49 -0700237 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800238
239 pos += len;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800240 if (!IS_ERR(dax.addr))
241 dax.addr += len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800242 }
243
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600244 if (need_wmb)
245 wmb_pmem();
Dan Williamsb2e0d162016-01-15 16:55:59 -0800246 dax_unmap_atomic(bdev, &dax);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600247
Dan Williamsb2e0d162016-01-15 16:55:59 -0800248 return (pos == start) ? rc : pos - start;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800249}
250
251/**
252 * dax_do_io - Perform I/O to a DAX file
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800253 * @iocb: The control block for this I/O
254 * @inode: The file which the I/O is directed at
255 * @iter: The addresses to do I/O from or to
256 * @pos: The file offset where the I/O starts
257 * @get_block: The filesystem method used to translate file offsets to blocks
258 * @end_io: A filesystem callback for I/O completion
259 * @flags: See below
260 *
261 * This function uses the same locking scheme as do_blockdev_direct_IO:
262 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
263 * caller for writes. For reads, we take and release the i_mutex ourselves.
264 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
265 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
266 * is in progress.
267 */
Omar Sandovala95cd632015-03-16 04:33:51 -0700268ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
269 struct iov_iter *iter, loff_t pos, get_block_t get_block,
270 dio_iodone_t end_io, int flags)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800271{
272 struct buffer_head bh;
273 ssize_t retval = -EINVAL;
274 loff_t end = pos + iov_iter_count(iter);
275
276 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -0800277 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800278
Omar Sandovala95cd632015-03-16 04:33:51 -0700279 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800280 struct address_space *mapping = inode->i_mapping;
Al Viro59551022016-01-22 15:40:57 -0500281 inode_lock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800282 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
283 if (retval) {
Al Viro59551022016-01-22 15:40:57 -0500284 inode_unlock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800285 goto out;
286 }
287 }
288
289 /* Protects against truncate */
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400290 if (!(flags & DIO_SKIP_DIO_COUNT))
291 inode_dio_begin(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800292
Omar Sandovala95cd632015-03-16 04:33:51 -0700293 retval = dax_io(inode, iter, pos, end, get_block, &bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800294
Omar Sandovala95cd632015-03-16 04:33:51 -0700295 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Al Viro59551022016-01-22 15:40:57 -0500296 inode_unlock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800297
Christoph Hellwig187372a2016-02-08 14:40:51 +1100298 if (end_io) {
299 int err;
300
301 err = end_io(iocb, pos, retval, bh.b_private);
302 if (err)
303 retval = err;
304 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800305
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400306 if (!(flags & DIO_SKIP_DIO_COUNT))
307 inode_dio_end(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800308 out:
309 return retval;
310}
311EXPORT_SYMBOL_GPL(dax_do_io);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800312
313/*
314 * The user has performed a load from a hole in the file. Allocating
315 * a new page in the file would cause excessive storage usage for
316 * workloads with sparse files. We allocate a page cache page instead.
317 * We'll kick it out of the page cache if it's ever written to,
318 * otherwise it will simply fall out of the page cache under memory
319 * pressure without ever having been dirtied.
320 */
321static int dax_load_hole(struct address_space *mapping, struct page *page,
322 struct vm_fault *vmf)
323{
324 unsigned long size;
325 struct inode *inode = mapping->host;
326 if (!page)
327 page = find_or_create_page(mapping, vmf->pgoff,
328 GFP_KERNEL | __GFP_ZERO);
329 if (!page)
330 return VM_FAULT_OOM;
331 /* Recheck i_size under page lock to avoid truncate race */
332 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
333 if (vmf->pgoff >= size) {
334 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300335 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800336 return VM_FAULT_SIGBUS;
337 }
338
339 vmf->page = page;
340 return VM_FAULT_LOCKED;
341}
342
Dan Williamsb2e0d162016-01-15 16:55:59 -0800343static int copy_user_bh(struct page *to, struct inode *inode,
344 struct buffer_head *bh, unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800345{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800346 struct blk_dax_ctl dax = {
347 .sector = to_sector(bh, inode),
348 .size = bh->b_size,
349 };
350 struct block_device *bdev = bh->b_bdev;
Ross Zwislere2e05392015-08-18 13:55:41 -0600351 void *vto;
352
Dan Williamsb2e0d162016-01-15 16:55:59 -0800353 if (dax_map_atomic(bdev, &dax) < 0)
354 return PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800355 vto = kmap_atomic(to);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800356 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800357 kunmap_atomic(vto);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800358 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800359 return 0;
360}
361
Ross Zwisler9973c982016-01-22 15:10:47 -0800362#define NO_SECTOR -1
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300363#define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
Ross Zwisler9973c982016-01-22 15:10:47 -0800364
365static int dax_radix_entry(struct address_space *mapping, pgoff_t index,
366 sector_t sector, bool pmd_entry, bool dirty)
367{
368 struct radix_tree_root *page_tree = &mapping->page_tree;
369 pgoff_t pmd_index = DAX_PMD_INDEX(index);
370 int type, error = 0;
371 void *entry;
372
373 WARN_ON_ONCE(pmd_entry && !dirty);
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800374 if (dirty)
375 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800376
377 spin_lock_irq(&mapping->tree_lock);
378
379 entry = radix_tree_lookup(page_tree, pmd_index);
380 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) {
381 index = pmd_index;
382 goto dirty;
383 }
384
385 entry = radix_tree_lookup(page_tree, index);
386 if (entry) {
387 type = RADIX_DAX_TYPE(entry);
388 if (WARN_ON_ONCE(type != RADIX_DAX_PTE &&
389 type != RADIX_DAX_PMD)) {
390 error = -EIO;
391 goto unlock;
392 }
393
394 if (!pmd_entry || type == RADIX_DAX_PMD)
395 goto dirty;
396
397 /*
398 * We only insert dirty PMD entries into the radix tree. This
399 * means we don't need to worry about removing a dirty PTE
400 * entry and inserting a clean PMD entry, thus reducing the
401 * range we would flush with a follow-up fsync/msync call.
402 */
403 radix_tree_delete(&mapping->page_tree, index);
404 mapping->nrexceptional--;
405 }
406
407 if (sector == NO_SECTOR) {
408 /*
409 * This can happen during correct operation if our pfn_mkwrite
410 * fault raced against a hole punch operation. If this
411 * happens the pte that was hole punched will have been
412 * unmapped and the radix tree entry will have been removed by
413 * the time we are called, but the call will still happen. We
414 * will return all the way up to wp_pfn_shared(), where the
415 * pte_same() check will fail, eventually causing page fault
416 * to be retried by the CPU.
417 */
418 goto unlock;
419 }
420
421 error = radix_tree_insert(page_tree, index,
422 RADIX_DAX_ENTRY(sector, pmd_entry));
423 if (error)
424 goto unlock;
425
426 mapping->nrexceptional++;
427 dirty:
428 if (dirty)
429 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
430 unlock:
431 spin_unlock_irq(&mapping->tree_lock);
432 return error;
433}
434
435static int dax_writeback_one(struct block_device *bdev,
436 struct address_space *mapping, pgoff_t index, void *entry)
437{
438 struct radix_tree_root *page_tree = &mapping->page_tree;
439 int type = RADIX_DAX_TYPE(entry);
440 struct radix_tree_node *node;
441 struct blk_dax_ctl dax;
442 void **slot;
443 int ret = 0;
444
445 spin_lock_irq(&mapping->tree_lock);
446 /*
447 * Regular page slots are stabilized by the page lock even
448 * without the tree itself locked. These unlocked entries
449 * need verification under the tree lock.
450 */
451 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
452 goto unlock;
453 if (*slot != entry)
454 goto unlock;
455
456 /* another fsync thread may have already written back this entry */
457 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
458 goto unlock;
459
460 if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
461 ret = -EIO;
462 goto unlock;
463 }
464
465 dax.sector = RADIX_DAX_SECTOR(entry);
466 dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
467 spin_unlock_irq(&mapping->tree_lock);
468
469 /*
470 * We cannot hold tree_lock while calling dax_map_atomic() because it
471 * eventually calls cond_resched().
472 */
473 ret = dax_map_atomic(bdev, &dax);
474 if (ret < 0)
475 return ret;
476
477 if (WARN_ON_ONCE(ret < dax.size)) {
478 ret = -EIO;
479 goto unmap;
480 }
481
482 wb_cache_pmem(dax.addr, dax.size);
483
484 spin_lock_irq(&mapping->tree_lock);
485 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
486 spin_unlock_irq(&mapping->tree_lock);
487 unmap:
488 dax_unmap_atomic(bdev, &dax);
489 return ret;
490
491 unlock:
492 spin_unlock_irq(&mapping->tree_lock);
493 return ret;
494}
495
496/*
497 * Flush the mapping to the persistent domain within the byte range of [start,
498 * end]. This is required by data integrity operations to ensure file data is
499 * on persistent storage prior to completion of the operation.
500 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800501int dax_writeback_mapping_range(struct address_space *mapping,
502 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800503{
504 struct inode *inode = mapping->host;
Ross Zwisler9973c982016-01-22 15:10:47 -0800505 pgoff_t start_index, end_index, pmd_index;
506 pgoff_t indices[PAGEVEC_SIZE];
507 struct pagevec pvec;
508 bool done = false;
509 int i, ret = 0;
510 void *entry;
511
512 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
513 return -EIO;
514
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800515 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
516 return 0;
517
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 start_index = wbc->range_start >> PAGE_SHIFT;
519 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800520 pmd_index = DAX_PMD_INDEX(start_index);
521
522 rcu_read_lock();
523 entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
524 rcu_read_unlock();
525
526 /* see if the start of our range is covered by a PMD entry */
527 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
528 start_index = pmd_index;
529
530 tag_pages_for_writeback(mapping, start_index, end_index);
531
532 pagevec_init(&pvec, 0);
533 while (!done) {
534 pvec.nr = find_get_entries_tag(mapping, start_index,
535 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
536 pvec.pages, indices);
537
538 if (pvec.nr == 0)
539 break;
540
541 for (i = 0; i < pvec.nr; i++) {
542 if (indices[i] > end_index) {
543 done = true;
544 break;
545 }
546
547 ret = dax_writeback_one(bdev, mapping, indices[i],
548 pvec.pages[i]);
549 if (ret < 0)
550 return ret;
551 }
552 }
553 wmb_pmem();
554 return 0;
555}
556EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
557
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800558static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
559 struct vm_area_struct *vma, struct vm_fault *vmf)
560{
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800561 unsigned long vaddr = (unsigned long)vmf->virtual_address;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800562 struct address_space *mapping = inode->i_mapping;
563 struct block_device *bdev = bh->b_bdev;
564 struct blk_dax_ctl dax = {
565 .sector = to_sector(bh, inode),
566 .size = bh->b_size,
567 };
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800568 pgoff_t size;
569 int error;
570
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700571 i_mmap_lock_read(mapping);
572
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800573 /*
574 * Check truncate didn't happen while we were allocating a block.
575 * If it did, this block may or may not be still allocated to the
576 * file. We can't tell the filesystem to free it because we can't
577 * take i_mutex here. In the worst case, the file still has blocks
578 * allocated past the end of the file.
579 */
580 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
581 if (unlikely(vmf->pgoff >= size)) {
582 error = -EIO;
583 goto out;
584 }
585
Dan Williamsb2e0d162016-01-15 16:55:59 -0800586 if (dax_map_atomic(bdev, &dax) < 0) {
587 error = PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800588 goto out;
589 }
590
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600591 if (buffer_unwritten(bh) || buffer_new(bh)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800592 clear_pmem(dax.addr, PAGE_SIZE);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600593 wmb_pmem();
594 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800595 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800596
Ross Zwisler9973c982016-01-22 15:10:47 -0800597 error = dax_radix_entry(mapping, vmf->pgoff, dax.sector, false,
598 vmf->flags & FAULT_FLAG_WRITE);
599 if (error)
600 goto out;
601
Dan Williams01c8f1c2016-01-15 16:56:40 -0800602 error = vm_insert_mixed(vma, vaddr, dax.pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800603
604 out:
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700605 i_mmap_unlock_read(mapping);
606
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800607 return error;
608}
609
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000610/**
611 * __dax_fault - handle a page fault on a DAX file
612 * @vma: The virtual memory area where the fault occurred
613 * @vmf: The description of the fault
614 * @get_block: The filesystem method used to translate file offsets to blocks
615 *
616 * When a page fault occurs, filesystems may call this helper in their
617 * fault handler for DAX files. __dax_fault() assumes the caller has done all
618 * the necessary locking for the page fault to proceed successfully.
619 */
620int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Jan Kara02fbd132016-05-11 11:58:48 +0200621 get_block_t get_block)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800622{
623 struct file *file = vma->vm_file;
624 struct address_space *mapping = file->f_mapping;
625 struct inode *inode = mapping->host;
626 struct page *page;
627 struct buffer_head bh;
628 unsigned long vaddr = (unsigned long)vmf->virtual_address;
629 unsigned blkbits = inode->i_blkbits;
630 sector_t block;
631 pgoff_t size;
632 int error;
633 int major = 0;
634
635 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
636 if (vmf->pgoff >= size)
637 return VM_FAULT_SIGBUS;
638
639 memset(&bh, 0, sizeof(bh));
640 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
Ross Zwislereab95db2016-01-22 15:10:59 -0800641 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800642 bh.b_size = PAGE_SIZE;
643
644 repeat:
645 page = find_get_page(mapping, vmf->pgoff);
646 if (page) {
647 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300648 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800649 return VM_FAULT_RETRY;
650 }
651 if (unlikely(page->mapping != mapping)) {
652 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800654 goto repeat;
655 }
656 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
657 if (unlikely(vmf->pgoff >= size)) {
658 /*
659 * We have a struct page covering a hole in the file
660 * from a read fault and we've raced with a truncate
661 */
662 error = -EIO;
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700663 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800664 }
665 }
666
667 error = get_block(inode, block, &bh, 0);
668 if (!error && (bh.b_size < PAGE_SIZE))
669 error = -EIO; /* fs corruption? */
670 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700671 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800672
Jan Karaaef39ab2016-05-13 00:38:15 -0400673 if (!buffer_mapped(&bh) && !vmf->cow_page) {
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800674 if (vmf->flags & FAULT_FLAG_WRITE) {
675 error = get_block(inode, block, &bh, 1);
676 count_vm_event(PGMAJFAULT);
677 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
678 major = VM_FAULT_MAJOR;
679 if (!error && (bh.b_size < PAGE_SIZE))
680 error = -EIO;
681 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700682 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800683 } else {
684 return dax_load_hole(mapping, page, vmf);
685 }
686 }
687
688 if (vmf->cow_page) {
689 struct page *new_page = vmf->cow_page;
690 if (buffer_written(&bh))
Dan Williamsb2e0d162016-01-15 16:55:59 -0800691 error = copy_user_bh(new_page, inode, &bh, vaddr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800692 else
693 clear_user_highpage(new_page, vaddr);
694 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700695 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800696 vmf->page = page;
697 if (!page) {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700698 i_mmap_lock_read(mapping);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800699 /* Check we didn't race with truncate */
700 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
701 PAGE_SHIFT;
702 if (vmf->pgoff >= size) {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700703 i_mmap_unlock_read(mapping);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800704 error = -EIO;
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700705 goto out;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800706 }
707 }
708 return VM_FAULT_LOCKED;
709 }
710
711 /* Check we didn't race with a read fault installing a new page */
712 if (!page && major)
713 page = find_lock_page(mapping, vmf->pgoff);
714
715 if (page) {
716 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300717 PAGE_SIZE, 0);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800718 delete_from_page_cache(page);
719 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300720 put_page(page);
Ross Zwisler9973c982016-01-22 15:10:47 -0800721 page = NULL;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800722 }
723
Jan Kara02fbd132016-05-11 11:58:48 +0200724 /* Filesystem should not return unwritten buffers to us! */
725 WARN_ON_ONCE(buffer_unwritten(&bh));
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800726 error = dax_insert_mapping(inode, &bh, vma, vmf);
727
728 out:
729 if (error == -ENOMEM)
730 return VM_FAULT_OOM | major;
731 /* -EBUSY is fine, somebody else faulted on the same PTE */
732 if ((error < 0) && (error != -EBUSY))
733 return VM_FAULT_SIGBUS | major;
734 return VM_FAULT_NOPAGE | major;
735
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700736 unlock_page:
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800737 if (page) {
738 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300739 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800740 }
741 goto out;
742}
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000743EXPORT_SYMBOL(__dax_fault);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800744
745/**
746 * dax_fault - handle a page fault on a DAX file
747 * @vma: The virtual memory area where the fault occurred
748 * @vmf: The description of the fault
749 * @get_block: The filesystem method used to translate file offsets to blocks
750 *
751 * When a page fault occurs, filesystems may call this helper in their
752 * fault handler for DAX files.
753 */
754int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Jan Kara02fbd132016-05-11 11:58:48 +0200755 get_block_t get_block)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800756{
757 int result;
758 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
759
760 if (vmf->flags & FAULT_FLAG_WRITE) {
761 sb_start_pagefault(sb);
762 file_update_time(vma->vm_file);
763 }
Jan Kara02fbd132016-05-11 11:58:48 +0200764 result = __dax_fault(vma, vmf, get_block);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800765 if (vmf->flags & FAULT_FLAG_WRITE)
766 sb_end_pagefault(sb);
767
768 return result;
769}
770EXPORT_SYMBOL_GPL(dax_fault);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800771
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700772#ifdef CONFIG_TRANSPARENT_HUGEPAGE
773/*
774 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
775 * more often than one might expect in the below function.
776 */
777#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
778
Dan Williamscbb38e42016-01-15 16:56:58 -0800779static void __dax_dbg(struct buffer_head *bh, unsigned long address,
780 const char *reason, const char *fn)
781{
782 if (bh) {
783 char bname[BDEVNAME_SIZE];
784 bdevname(bh->b_bdev, bname);
785 pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
786 "length %zd fallback: %s\n", fn, current->comm,
787 address, bname, bh->b_state, (u64)bh->b_blocknr,
788 bh->b_size, reason);
789 } else {
790 pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
791 current->comm, address, reason);
792 }
793}
794
795#define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
796
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700797int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
Jan Kara02fbd132016-05-11 11:58:48 +0200798 pmd_t *pmd, unsigned int flags, get_block_t get_block)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700799{
800 struct file *file = vma->vm_file;
801 struct address_space *mapping = file->f_mapping;
802 struct inode *inode = mapping->host;
803 struct buffer_head bh;
804 unsigned blkbits = inode->i_blkbits;
805 unsigned long pmd_addr = address & PMD_MASK;
806 bool write = flags & FAULT_FLAG_WRITE;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800807 struct block_device *bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700808 pgoff_t size, pgoff;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800809 sector_t block;
Ross Zwisler9973c982016-01-22 15:10:47 -0800810 int error, result = 0;
811 bool alloc = false;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700812
Dan Williamsc046c322016-01-15 16:57:01 -0800813 /* dax pmd mappings require pfn_t_devmap() */
Dan Williamsee82c9e2015-11-15 16:06:32 -0800814 if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
815 return VM_FAULT_FALLBACK;
816
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700817 /* Fall back to PTEs if we're going to COW */
Toshi Kani59bf4fb2016-01-15 16:56:05 -0800818 if (write && !(vma->vm_flags & VM_SHARED)) {
819 split_huge_pmd(vma, pmd, address);
Dan Williamscbb38e42016-01-15 16:56:58 -0800820 dax_pmd_dbg(NULL, address, "cow write");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700821 return VM_FAULT_FALLBACK;
Toshi Kani59bf4fb2016-01-15 16:56:05 -0800822 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700823 /* If the PMD would extend outside the VMA */
Dan Williamscbb38e42016-01-15 16:56:58 -0800824 if (pmd_addr < vma->vm_start) {
825 dax_pmd_dbg(NULL, address, "vma start unaligned");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700826 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800827 }
828 if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
829 dax_pmd_dbg(NULL, address, "vma end unaligned");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700830 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800831 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700832
Matthew Wilcox3fdd1b472015-09-08 14:59:39 -0700833 pgoff = linear_page_index(vma, pmd_addr);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700834 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
835 if (pgoff >= size)
836 return VM_FAULT_SIGBUS;
837 /* If the PMD would cover blocks out of the file */
Dan Williamscbb38e42016-01-15 16:56:58 -0800838 if ((pgoff | PG_PMD_COLOUR) >= size) {
839 dax_pmd_dbg(NULL, address,
840 "offset + huge page size > file size");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700841 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800842 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700843
844 memset(&bh, 0, sizeof(bh));
Ross Zwislerd4bbe702016-01-22 15:10:31 -0800845 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700846 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
847
848 bh.b_size = PMD_SIZE;
Ross Zwisler9973c982016-01-22 15:10:47 -0800849
850 if (get_block(inode, block, &bh, 0) != 0)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700851 return VM_FAULT_SIGBUS;
Ross Zwisler9973c982016-01-22 15:10:47 -0800852
853 if (!buffer_mapped(&bh) && write) {
854 if (get_block(inode, block, &bh, 1) != 0)
855 return VM_FAULT_SIGBUS;
856 alloc = true;
Jan Kara02fbd132016-05-11 11:58:48 +0200857 WARN_ON_ONCE(buffer_unwritten(&bh));
Ross Zwisler9973c982016-01-22 15:10:47 -0800858 }
859
Dan Williamsb2e0d162016-01-15 16:55:59 -0800860 bdev = bh.b_bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700861
862 /*
863 * If the filesystem isn't willing to tell us the length of a hole,
864 * just fall back to PTEs. Calling get_block 512 times in a loop
865 * would be silly.
866 */
Dan Williamscbb38e42016-01-15 16:56:58 -0800867 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
868 dax_pmd_dbg(&bh, address, "allocated block too small");
Ross Zwisler9973c982016-01-22 15:10:47 -0800869 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800870 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700871
Ross Zwisler9973c982016-01-22 15:10:47 -0800872 /*
873 * If we allocated new storage, make sure no process has any
874 * zero pages covering this hole
875 */
876 if (alloc) {
877 loff_t lstart = pgoff << PAGE_SHIFT;
878 loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
879
880 truncate_pagecache_range(inode, lstart, lend);
881 }
882
Ross Zwislerde14b9c2016-01-22 15:10:34 -0800883 i_mmap_lock_read(mapping);
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -0700884
Matthew Wilcox84c4e5e2015-09-08 14:59:17 -0700885 /*
886 * If a truncate happened while we were allocating blocks, we may
887 * leave blocks allocated to the file that are beyond EOF. We can't
888 * take i_mutex here, so just leave them hanging; they'll be freed
889 * when the file is deleted.
890 */
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700891 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
892 if (pgoff >= size) {
893 result = VM_FAULT_SIGBUS;
894 goto out;
895 }
Dan Williamscbb38e42016-01-15 16:56:58 -0800896 if ((pgoff | PG_PMD_COLOUR) >= size) {
Ross Zwislerde14b9c2016-01-22 15:10:34 -0800897 dax_pmd_dbg(&bh, address,
898 "offset + huge page size > file size");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700899 goto fallback;
Dan Williamscbb38e42016-01-15 16:56:58 -0800900 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700901
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700902 if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700903 spinlock_t *ptl;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700904 pmd_t entry;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700905 struct page *zero_page = get_huge_zero_page();
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700906
Dan Williamscbb38e42016-01-15 16:56:58 -0800907 if (unlikely(!zero_page)) {
908 dax_pmd_dbg(&bh, address, "no zero page");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700909 goto fallback;
Dan Williamscbb38e42016-01-15 16:56:58 -0800910 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700911
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700912 ptl = pmd_lock(vma->vm_mm, pmd);
913 if (!pmd_none(*pmd)) {
914 spin_unlock(ptl);
Dan Williamscbb38e42016-01-15 16:56:58 -0800915 dax_pmd_dbg(&bh, address, "pmd already present");
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700916 goto fallback;
917 }
918
Dan Williamscbb38e42016-01-15 16:56:58 -0800919 dev_dbg(part_to_dev(bdev->bd_part),
920 "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
921 __func__, current->comm, address,
922 (unsigned long long) to_sector(&bh, inode));
923
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700924 entry = mk_pmd(zero_page, vma->vm_page_prot);
925 entry = pmd_mkhuge(entry);
926 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700927 result = VM_FAULT_NOPAGE;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700928 spin_unlock(ptl);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700929 } else {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800930 struct blk_dax_ctl dax = {
931 .sector = to_sector(&bh, inode),
932 .size = PMD_SIZE,
933 };
934 long length = dax_map_atomic(bdev, &dax);
935
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700936 if (length < 0) {
937 result = VM_FAULT_SIGBUS;
938 goto out;
939 }
Dan Williamscbb38e42016-01-15 16:56:58 -0800940 if (length < PMD_SIZE) {
941 dax_pmd_dbg(&bh, address, "dax-length too small");
942 dax_unmap_atomic(bdev, &dax);
943 goto fallback;
944 }
945 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
946 dax_pmd_dbg(&bh, address, "pfn unaligned");
Dan Williamsb2e0d162016-01-15 16:55:59 -0800947 dax_unmap_atomic(bdev, &dax);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700948 goto fallback;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800949 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700950
Dan Williamsc046c322016-01-15 16:57:01 -0800951 if (!pfn_t_devmap(dax.pfn)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800952 dax_unmap_atomic(bdev, &dax);
Dan Williamscbb38e42016-01-15 16:56:58 -0800953 dax_pmd_dbg(&bh, address, "pfn not in memmap");
Dan Williams152d7bd2015-11-12 18:33:54 -0800954 goto fallback;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800955 }
Dan Williams152d7bd2015-11-12 18:33:54 -0800956
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700957 if (buffer_unwritten(&bh) || buffer_new(&bh)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800958 clear_pmem(dax.addr, PMD_SIZE);
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700959 wmb_pmem();
960 count_vm_event(PGMAJFAULT);
961 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
962 result |= VM_FAULT_MAJOR;
963 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800964 dax_unmap_atomic(bdev, &dax);
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700965
Ross Zwisler9973c982016-01-22 15:10:47 -0800966 /*
967 * For PTE faults we insert a radix tree entry for reads, and
968 * leave it clean. Then on the first write we dirty the radix
969 * tree entry via the dax_pfn_mkwrite() path. This sequence
970 * allows the dax_pfn_mkwrite() call to be simpler and avoid a
971 * call into get_block() to translate the pgoff to a sector in
972 * order to be able to create a new radix tree entry.
973 *
974 * The PMD path doesn't have an equivalent to
975 * dax_pfn_mkwrite(), though, so for a read followed by a
976 * write we traverse all the way through __dax_pmd_fault()
977 * twice. This means we can just skip inserting a radix tree
978 * entry completely on the initial read and just wait until
979 * the write to insert a dirty entry.
980 */
981 if (write) {
982 error = dax_radix_entry(mapping, pgoff, dax.sector,
983 true, true);
984 if (error) {
985 dax_pmd_dbg(&bh, address,
986 "PMD radix insertion failed");
987 goto fallback;
988 }
989 }
990
Dan Williamscbb38e42016-01-15 16:56:58 -0800991 dev_dbg(part_to_dev(bdev->bd_part),
992 "%s: %s addr: %lx pfn: %lx sect: %llx\n",
993 __func__, current->comm, address,
994 pfn_t_to_pfn(dax.pfn),
995 (unsigned long long) dax.sector);
Dan Williams34c0fd52016-01-15 16:56:14 -0800996 result |= vmf_insert_pfn_pmd(vma, address, pmd,
Dan Williamsf25748e32016-01-15 16:56:43 -0800997 dax.pfn, write);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700998 }
999
1000 out:
Ross Zwisler0f90cc62015-10-15 15:28:32 -07001001 i_mmap_unlock_read(mapping);
1002
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001003 return result;
1004
1005 fallback:
1006 count_vm_event(THP_FAULT_FALLBACK);
1007 result = VM_FAULT_FALLBACK;
1008 goto out;
1009}
1010EXPORT_SYMBOL_GPL(__dax_pmd_fault);
1011
1012/**
1013 * dax_pmd_fault - handle a PMD fault on a DAX file
1014 * @vma: The virtual memory area where the fault occurred
1015 * @vmf: The description of the fault
1016 * @get_block: The filesystem method used to translate file offsets to blocks
1017 *
1018 * When a page fault occurs, filesystems may call this helper in their
1019 * pmd_fault handler for DAX files.
1020 */
1021int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
Jan Kara02fbd132016-05-11 11:58:48 +02001022 pmd_t *pmd, unsigned int flags, get_block_t get_block)
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001023{
1024 int result;
1025 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
1026
1027 if (flags & FAULT_FLAG_WRITE) {
1028 sb_start_pagefault(sb);
1029 file_update_time(vma->vm_file);
1030 }
Jan Kara02fbd132016-05-11 11:58:48 +02001031 result = __dax_pmd_fault(vma, address, pmd, flags, get_block);
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001032 if (flags & FAULT_FLAG_WRITE)
1033 sb_end_pagefault(sb);
1034
1035 return result;
1036}
1037EXPORT_SYMBOL_GPL(dax_pmd_fault);
Valentin Rothbergdd8a2b62015-09-08 14:59:09 -07001038#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001039
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001040/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001041 * dax_pfn_mkwrite - handle first write to DAX page
1042 * @vma: The virtual memory area where the fault occurred
1043 * @vmf: The description of the fault
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001044 */
1045int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1046{
Ross Zwisler9973c982016-01-22 15:10:47 -08001047 struct file *file = vma->vm_file;
Ross Zwisler30f471f2016-03-09 14:08:27 -08001048 int error;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001049
Ross Zwisler9973c982016-01-22 15:10:47 -08001050 /*
1051 * We pass NO_SECTOR to dax_radix_entry() because we expect that a
1052 * RADIX_DAX_PTE entry already exists in the radix tree from a
1053 * previous call to __dax_fault(). We just want to look up that PTE
1054 * entry using vmf->pgoff and make sure the dirty tag is set. This
1055 * saves us from having to make a call to get_block() here to look
1056 * up the sector.
1057 */
Ross Zwisler30f471f2016-03-09 14:08:27 -08001058 error = dax_radix_entry(file->f_mapping, vmf->pgoff, NO_SECTOR, false,
1059 true);
1060
1061 if (error == -ENOMEM)
1062 return VM_FAULT_OOM;
1063 if (error)
1064 return VM_FAULT_SIGBUS;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001065 return VM_FAULT_NOPAGE;
1066}
1067EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
1068
1069/**
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001070 * dax_zero_page_range - zero a range within a page of a DAX file
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001071 * @inode: The file being truncated
1072 * @from: The file offset that is being truncated to
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001073 * @length: The number of bytes to zero
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001074 * @get_block: The filesystem method used to translate file offsets to blocks
1075 *
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001076 * This function can be called by a filesystem when it is zeroing part of a
1077 * page in a DAX file. This is intended for hole-punch operations. If
1078 * you are truncating a file, the helper function dax_truncate_page() may be
1079 * more convenient.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001080 *
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03001081 * We work in terms of PAGE_SIZE here for commonality with
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001082 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
1083 * took care of disposing of the unnecessary blocks. Even if the filesystem
1084 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001085 * since the file might be mmapped.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001086 */
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001087int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
1088 get_block_t get_block)
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001089{
1090 struct buffer_head bh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001091 pgoff_t index = from >> PAGE_SHIFT;
1092 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001093 int err;
1094
1095 /* Block boundary? Nothing to do */
1096 if (!length)
1097 return 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001098 BUG_ON((offset + length) > PAGE_SIZE);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001099
1100 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -08001101 bh.b_bdev = inode->i_sb->s_bdev;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001102 bh.b_size = PAGE_SIZE;
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001103 err = get_block(inode, index, &bh, 0);
1104 if (err < 0)
1105 return err;
1106 if (buffer_written(&bh)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -08001107 struct block_device *bdev = bh.b_bdev;
1108 struct blk_dax_ctl dax = {
1109 .sector = to_sector(&bh, inode),
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001110 .size = PAGE_SIZE,
Dan Williamsb2e0d162016-01-15 16:55:59 -08001111 };
1112
1113 if (dax_map_atomic(bdev, &dax) < 0)
1114 return PTR_ERR(dax.addr);
1115 clear_pmem(dax.addr + offset, length);
Ross Zwisler2765cfb2015-08-18 13:55:40 -06001116 wmb_pmem();
Dan Williamsb2e0d162016-01-15 16:55:59 -08001117 dax_unmap_atomic(bdev, &dax);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001118 }
1119
1120 return 0;
1121}
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001122EXPORT_SYMBOL_GPL(dax_zero_page_range);
1123
1124/**
1125 * dax_truncate_page - handle a partial page being truncated in a DAX file
1126 * @inode: The file being truncated
1127 * @from: The file offset that is being truncated to
1128 * @get_block: The filesystem method used to translate file offsets to blocks
1129 *
1130 * Similar to block_truncate_page(), this function can be called by a
1131 * filesystem when it is truncating a DAX file to handle the partial page.
1132 *
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03001133 * We work in terms of PAGE_SIZE here for commonality with
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001134 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
1135 * took care of disposing of the unnecessary blocks. Even if the filesystem
1136 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
1137 * since the file might be mmapped.
1138 */
1139int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1140{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001141 unsigned length = PAGE_ALIGN(from) - from;
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001142 return dax_zero_page_range(inode, from, length, get_block);
1143}
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001144EXPORT_SYMBOL_GPL(dax_truncate_page);