blob: 237581441bc1d3e48f5713a91606b1d3628b4026 [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
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800122static bool buffer_written(struct buffer_head *bh)
123{
124 return buffer_mapped(bh) && !buffer_unwritten(bh);
125}
126
127/*
128 * When ext4 encounters a hole, it returns without modifying the buffer_head
129 * which means that we can't trust b_size. To cope with this, we set b_state
130 * to 0 before calling get_block and, if any bit is set, we know we can trust
131 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
132 * and would save us time calling get_block repeatedly.
133 */
134static bool buffer_size_valid(struct buffer_head *bh)
135{
136 return bh->b_state != 0;
137}
138
Dan Williamsb2e0d162016-01-15 16:55:59 -0800139
140static sector_t to_sector(const struct buffer_head *bh,
141 const struct inode *inode)
142{
143 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
144
145 return sector;
146}
147
Omar Sandovala95cd632015-03-16 04:33:51 -0700148static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
149 loff_t start, loff_t end, get_block_t get_block,
150 struct buffer_head *bh)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800151{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800152 loff_t pos = start, max = start, bh_max = start;
153 bool hole = false, need_wmb = false;
154 struct block_device *bdev = NULL;
155 int rw = iov_iter_rw(iter), rc;
156 long map_len = 0;
157 struct blk_dax_ctl dax = {
158 .addr = (void __pmem *) ERR_PTR(-EIO),
159 };
Jan Kara069c77b2016-05-11 11:58:51 +0200160 unsigned blkbits = inode->i_blkbits;
161 sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1)
162 >> blkbits;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800163
Dan Williamsb2e0d162016-01-15 16:55:59 -0800164 if (rw == READ)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800165 end = min(end, i_size_read(inode));
166
167 while (pos < end) {
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600168 size_t len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800169 if (pos == max) {
Jeff Moyere94f5a22015-08-14 16:15:31 -0400170 long page = pos >> PAGE_SHIFT;
171 sector_t block = page << (PAGE_SHIFT - blkbits);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800172 unsigned first = pos - (block << blkbits);
173 long size;
174
175 if (pos == bh_max) {
176 bh->b_size = PAGE_ALIGN(end - pos);
177 bh->b_state = 0;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800178 rc = get_block(inode, block, bh, rw == WRITE);
179 if (rc)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800180 break;
181 if (!buffer_size_valid(bh))
182 bh->b_size = 1 << blkbits;
183 bh_max = pos - first + bh->b_size;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800184 bdev = bh->b_bdev;
Jan Kara069c77b2016-05-11 11:58:51 +0200185 /*
186 * We allow uninitialized buffers for writes
187 * beyond EOF as those cannot race with faults
188 */
189 WARN_ON_ONCE(
190 (buffer_new(bh) && block < file_blks) ||
191 (rw == WRITE && buffer_unwritten(bh)));
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800192 } else {
193 unsigned done = bh->b_size -
194 (bh_max - (pos - first));
195 bh->b_blocknr += done >> blkbits;
196 bh->b_size -= done;
197 }
198
Dan Williamsb2e0d162016-01-15 16:55:59 -0800199 hole = rw == READ && !buffer_written(bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800200 if (hole) {
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800201 size = bh->b_size - first;
202 } else {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800203 dax_unmap_atomic(bdev, &dax);
204 dax.sector = to_sector(bh, inode);
205 dax.size = bh->b_size;
206 map_len = dax_map_atomic(bdev, &dax);
207 if (map_len < 0) {
208 rc = map_len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800209 break;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800210 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800211 dax.addr += first;
212 size = map_len - first;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800213 }
214 max = min(pos + size, end);
215 }
216
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600217 if (iov_iter_rw(iter) == WRITE) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800218 len = copy_from_iter_pmem(dax.addr, max - pos, iter);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600219 need_wmb = true;
220 } else if (!hole)
Dan Williamsb2e0d162016-01-15 16:55:59 -0800221 len = copy_to_iter((void __force *) dax.addr, max - pos,
Ross Zwislere2e05392015-08-18 13:55:41 -0600222 iter);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800223 else
224 len = iov_iter_zero(max - pos, iter);
225
Al Virocadfbb62015-11-10 19:42:49 -0700226 if (!len) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800227 rc = -EFAULT;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800228 break;
Al Virocadfbb62015-11-10 19:42:49 -0700229 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800230
231 pos += len;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800232 if (!IS_ERR(dax.addr))
233 dax.addr += len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800234 }
235
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600236 if (need_wmb)
237 wmb_pmem();
Dan Williamsb2e0d162016-01-15 16:55:59 -0800238 dax_unmap_atomic(bdev, &dax);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600239
Dan Williamsb2e0d162016-01-15 16:55:59 -0800240 return (pos == start) ? rc : pos - start;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800241}
242
243/**
244 * dax_do_io - Perform I/O to a DAX file
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800245 * @iocb: The control block for this I/O
246 * @inode: The file which the I/O is directed at
247 * @iter: The addresses to do I/O from or to
248 * @pos: The file offset where the I/O starts
249 * @get_block: The filesystem method used to translate file offsets to blocks
250 * @end_io: A filesystem callback for I/O completion
251 * @flags: See below
252 *
253 * This function uses the same locking scheme as do_blockdev_direct_IO:
254 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
255 * caller for writes. For reads, we take and release the i_mutex ourselves.
256 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
257 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
258 * is in progress.
259 */
Omar Sandovala95cd632015-03-16 04:33:51 -0700260ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
261 struct iov_iter *iter, loff_t pos, get_block_t get_block,
262 dio_iodone_t end_io, int flags)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800263{
264 struct buffer_head bh;
265 ssize_t retval = -EINVAL;
266 loff_t end = pos + iov_iter_count(iter);
267
268 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -0800269 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800270
Jan Karac3d98e32016-05-11 11:58:52 +0200271 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Al Viro59551022016-01-22 15:40:57 -0500272 inode_lock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800273
274 /* Protects against truncate */
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400275 if (!(flags & DIO_SKIP_DIO_COUNT))
276 inode_dio_begin(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800277
Omar Sandovala95cd632015-03-16 04:33:51 -0700278 retval = dax_io(inode, iter, pos, end, get_block, &bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800279
Omar Sandovala95cd632015-03-16 04:33:51 -0700280 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Al Viro59551022016-01-22 15:40:57 -0500281 inode_unlock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800282
Christoph Hellwig187372a2016-02-08 14:40:51 +1100283 if (end_io) {
284 int err;
285
286 err = end_io(iocb, pos, retval, bh.b_private);
287 if (err)
288 retval = err;
289 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800290
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400291 if (!(flags & DIO_SKIP_DIO_COUNT))
292 inode_dio_end(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800293 return retval;
294}
295EXPORT_SYMBOL_GPL(dax_do_io);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800296
297/*
298 * The user has performed a load from a hole in the file. Allocating
299 * a new page in the file would cause excessive storage usage for
300 * workloads with sparse files. We allocate a page cache page instead.
301 * We'll kick it out of the page cache if it's ever written to,
302 * otherwise it will simply fall out of the page cache under memory
303 * pressure without ever having been dirtied.
304 */
305static int dax_load_hole(struct address_space *mapping, struct page *page,
306 struct vm_fault *vmf)
307{
308 unsigned long size;
309 struct inode *inode = mapping->host;
310 if (!page)
311 page = find_or_create_page(mapping, vmf->pgoff,
312 GFP_KERNEL | __GFP_ZERO);
313 if (!page)
314 return VM_FAULT_OOM;
315 /* Recheck i_size under page lock to avoid truncate race */
316 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
317 if (vmf->pgoff >= size) {
318 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300319 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800320 return VM_FAULT_SIGBUS;
321 }
322
323 vmf->page = page;
324 return VM_FAULT_LOCKED;
325}
326
Dan Williamsb2e0d162016-01-15 16:55:59 -0800327static int copy_user_bh(struct page *to, struct inode *inode,
328 struct buffer_head *bh, unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800329{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800330 struct blk_dax_ctl dax = {
331 .sector = to_sector(bh, inode),
332 .size = bh->b_size,
333 };
334 struct block_device *bdev = bh->b_bdev;
Ross Zwislere2e05392015-08-18 13:55:41 -0600335 void *vto;
336
Dan Williamsb2e0d162016-01-15 16:55:59 -0800337 if (dax_map_atomic(bdev, &dax) < 0)
338 return PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800339 vto = kmap_atomic(to);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800340 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800341 kunmap_atomic(vto);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800342 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800343 return 0;
344}
345
Ross Zwisler9973c982016-01-22 15:10:47 -0800346#define NO_SECTOR -1
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300347#define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
Ross Zwisler9973c982016-01-22 15:10:47 -0800348
349static int dax_radix_entry(struct address_space *mapping, pgoff_t index,
350 sector_t sector, bool pmd_entry, bool dirty)
351{
352 struct radix_tree_root *page_tree = &mapping->page_tree;
353 pgoff_t pmd_index = DAX_PMD_INDEX(index);
354 int type, error = 0;
355 void *entry;
356
357 WARN_ON_ONCE(pmd_entry && !dirty);
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800358 if (dirty)
359 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800360
361 spin_lock_irq(&mapping->tree_lock);
362
363 entry = radix_tree_lookup(page_tree, pmd_index);
364 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) {
365 index = pmd_index;
366 goto dirty;
367 }
368
369 entry = radix_tree_lookup(page_tree, index);
370 if (entry) {
371 type = RADIX_DAX_TYPE(entry);
372 if (WARN_ON_ONCE(type != RADIX_DAX_PTE &&
373 type != RADIX_DAX_PMD)) {
374 error = -EIO;
375 goto unlock;
376 }
377
378 if (!pmd_entry || type == RADIX_DAX_PMD)
379 goto dirty;
380
381 /*
382 * We only insert dirty PMD entries into the radix tree. This
383 * means we don't need to worry about removing a dirty PTE
384 * entry and inserting a clean PMD entry, thus reducing the
385 * range we would flush with a follow-up fsync/msync call.
386 */
387 radix_tree_delete(&mapping->page_tree, index);
388 mapping->nrexceptional--;
389 }
390
391 if (sector == NO_SECTOR) {
392 /*
393 * This can happen during correct operation if our pfn_mkwrite
394 * fault raced against a hole punch operation. If this
395 * happens the pte that was hole punched will have been
396 * unmapped and the radix tree entry will have been removed by
397 * the time we are called, but the call will still happen. We
398 * will return all the way up to wp_pfn_shared(), where the
399 * pte_same() check will fail, eventually causing page fault
400 * to be retried by the CPU.
401 */
402 goto unlock;
403 }
404
405 error = radix_tree_insert(page_tree, index,
406 RADIX_DAX_ENTRY(sector, pmd_entry));
407 if (error)
408 goto unlock;
409
410 mapping->nrexceptional++;
411 dirty:
412 if (dirty)
413 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
414 unlock:
415 spin_unlock_irq(&mapping->tree_lock);
416 return error;
417}
418
419static int dax_writeback_one(struct block_device *bdev,
420 struct address_space *mapping, pgoff_t index, void *entry)
421{
422 struct radix_tree_root *page_tree = &mapping->page_tree;
423 int type = RADIX_DAX_TYPE(entry);
424 struct radix_tree_node *node;
425 struct blk_dax_ctl dax;
426 void **slot;
427 int ret = 0;
428
429 spin_lock_irq(&mapping->tree_lock);
430 /*
431 * Regular page slots are stabilized by the page lock even
432 * without the tree itself locked. These unlocked entries
433 * need verification under the tree lock.
434 */
435 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
436 goto unlock;
437 if (*slot != entry)
438 goto unlock;
439
440 /* another fsync thread may have already written back this entry */
441 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
442 goto unlock;
443
444 if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
445 ret = -EIO;
446 goto unlock;
447 }
448
449 dax.sector = RADIX_DAX_SECTOR(entry);
450 dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
451 spin_unlock_irq(&mapping->tree_lock);
452
453 /*
454 * We cannot hold tree_lock while calling dax_map_atomic() because it
455 * eventually calls cond_resched().
456 */
457 ret = dax_map_atomic(bdev, &dax);
458 if (ret < 0)
459 return ret;
460
461 if (WARN_ON_ONCE(ret < dax.size)) {
462 ret = -EIO;
463 goto unmap;
464 }
465
466 wb_cache_pmem(dax.addr, dax.size);
467
468 spin_lock_irq(&mapping->tree_lock);
469 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
470 spin_unlock_irq(&mapping->tree_lock);
471 unmap:
472 dax_unmap_atomic(bdev, &dax);
473 return ret;
474
475 unlock:
476 spin_unlock_irq(&mapping->tree_lock);
477 return ret;
478}
479
480/*
481 * Flush the mapping to the persistent domain within the byte range of [start,
482 * end]. This is required by data integrity operations to ensure file data is
483 * on persistent storage prior to completion of the operation.
484 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800485int dax_writeback_mapping_range(struct address_space *mapping,
486 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800487{
488 struct inode *inode = mapping->host;
Ross Zwisler9973c982016-01-22 15:10:47 -0800489 pgoff_t start_index, end_index, pmd_index;
490 pgoff_t indices[PAGEVEC_SIZE];
491 struct pagevec pvec;
492 bool done = false;
493 int i, ret = 0;
494 void *entry;
495
496 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
497 return -EIO;
498
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800499 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
500 return 0;
501
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300502 start_index = wbc->range_start >> PAGE_SHIFT;
503 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800504 pmd_index = DAX_PMD_INDEX(start_index);
505
506 rcu_read_lock();
507 entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
508 rcu_read_unlock();
509
510 /* see if the start of our range is covered by a PMD entry */
511 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
512 start_index = pmd_index;
513
514 tag_pages_for_writeback(mapping, start_index, end_index);
515
516 pagevec_init(&pvec, 0);
517 while (!done) {
518 pvec.nr = find_get_entries_tag(mapping, start_index,
519 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
520 pvec.pages, indices);
521
522 if (pvec.nr == 0)
523 break;
524
525 for (i = 0; i < pvec.nr; i++) {
526 if (indices[i] > end_index) {
527 done = true;
528 break;
529 }
530
531 ret = dax_writeback_one(bdev, mapping, indices[i],
532 pvec.pages[i]);
533 if (ret < 0)
534 return ret;
535 }
536 }
537 wmb_pmem();
538 return 0;
539}
540EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
541
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800542static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
543 struct vm_area_struct *vma, struct vm_fault *vmf)
544{
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800545 unsigned long vaddr = (unsigned long)vmf->virtual_address;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800546 struct address_space *mapping = inode->i_mapping;
547 struct block_device *bdev = bh->b_bdev;
548 struct blk_dax_ctl dax = {
549 .sector = to_sector(bh, inode),
550 .size = bh->b_size,
551 };
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800552 pgoff_t size;
553 int error;
554
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700555 i_mmap_lock_read(mapping);
556
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800557 /*
558 * Check truncate didn't happen while we were allocating a block.
559 * If it did, this block may or may not be still allocated to the
560 * file. We can't tell the filesystem to free it because we can't
561 * take i_mutex here. In the worst case, the file still has blocks
562 * allocated past the end of the file.
563 */
564 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
565 if (unlikely(vmf->pgoff >= size)) {
566 error = -EIO;
567 goto out;
568 }
569
Dan Williamsb2e0d162016-01-15 16:55:59 -0800570 if (dax_map_atomic(bdev, &dax) < 0) {
571 error = PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800572 goto out;
573 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800574 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800575
Ross Zwisler9973c982016-01-22 15:10:47 -0800576 error = dax_radix_entry(mapping, vmf->pgoff, dax.sector, false,
577 vmf->flags & FAULT_FLAG_WRITE);
578 if (error)
579 goto out;
580
Dan Williams01c8f1c2016-01-15 16:56:40 -0800581 error = vm_insert_mixed(vma, vaddr, dax.pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800582
583 out:
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700584 i_mmap_unlock_read(mapping);
585
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800586 return error;
587}
588
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000589/**
590 * __dax_fault - handle a page fault on a DAX file
591 * @vma: The virtual memory area where the fault occurred
592 * @vmf: The description of the fault
593 * @get_block: The filesystem method used to translate file offsets to blocks
594 *
595 * When a page fault occurs, filesystems may call this helper in their
596 * fault handler for DAX files. __dax_fault() assumes the caller has done all
597 * the necessary locking for the page fault to proceed successfully.
598 */
599int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Jan Kara02fbd132016-05-11 11:58:48 +0200600 get_block_t get_block)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800601{
602 struct file *file = vma->vm_file;
603 struct address_space *mapping = file->f_mapping;
604 struct inode *inode = mapping->host;
605 struct page *page;
606 struct buffer_head bh;
607 unsigned long vaddr = (unsigned long)vmf->virtual_address;
608 unsigned blkbits = inode->i_blkbits;
609 sector_t block;
610 pgoff_t size;
611 int error;
612 int major = 0;
613
614 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
615 if (vmf->pgoff >= size)
616 return VM_FAULT_SIGBUS;
617
618 memset(&bh, 0, sizeof(bh));
619 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
Ross Zwislereab95db2016-01-22 15:10:59 -0800620 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800621 bh.b_size = PAGE_SIZE;
622
623 repeat:
624 page = find_get_page(mapping, vmf->pgoff);
625 if (page) {
626 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800628 return VM_FAULT_RETRY;
629 }
630 if (unlikely(page->mapping != mapping)) {
631 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300632 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800633 goto repeat;
634 }
635 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
636 if (unlikely(vmf->pgoff >= size)) {
637 /*
638 * We have a struct page covering a hole in the file
639 * from a read fault and we've raced with a truncate
640 */
641 error = -EIO;
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700642 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800643 }
644 }
645
646 error = get_block(inode, block, &bh, 0);
647 if (!error && (bh.b_size < PAGE_SIZE))
648 error = -EIO; /* fs corruption? */
649 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700650 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800651
Jan Karaaef39ab2016-05-13 00:38:15 -0400652 if (!buffer_mapped(&bh) && !vmf->cow_page) {
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800653 if (vmf->flags & FAULT_FLAG_WRITE) {
654 error = get_block(inode, block, &bh, 1);
655 count_vm_event(PGMAJFAULT);
656 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
657 major = VM_FAULT_MAJOR;
658 if (!error && (bh.b_size < PAGE_SIZE))
659 error = -EIO;
660 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700661 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800662 } else {
663 return dax_load_hole(mapping, page, vmf);
664 }
665 }
666
667 if (vmf->cow_page) {
668 struct page *new_page = vmf->cow_page;
669 if (buffer_written(&bh))
Dan Williamsb2e0d162016-01-15 16:55:59 -0800670 error = copy_user_bh(new_page, inode, &bh, vaddr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800671 else
672 clear_user_highpage(new_page, vaddr);
673 if (error)
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700674 goto unlock_page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800675 vmf->page = page;
676 if (!page) {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700677 i_mmap_lock_read(mapping);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800678 /* Check we didn't race with truncate */
679 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
680 PAGE_SHIFT;
681 if (vmf->pgoff >= size) {
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700682 i_mmap_unlock_read(mapping);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800683 error = -EIO;
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700684 goto out;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800685 }
686 }
687 return VM_FAULT_LOCKED;
688 }
689
690 /* Check we didn't race with a read fault installing a new page */
691 if (!page && major)
692 page = find_lock_page(mapping, vmf->pgoff);
693
694 if (page) {
695 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300696 PAGE_SIZE, 0);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800697 delete_from_page_cache(page);
698 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300699 put_page(page);
Ross Zwisler9973c982016-01-22 15:10:47 -0800700 page = NULL;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800701 }
702
Jan Kara02fbd132016-05-11 11:58:48 +0200703 /* Filesystem should not return unwritten buffers to us! */
Jan Kara2b109452016-05-11 11:58:50 +0200704 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800705 error = dax_insert_mapping(inode, &bh, vma, vmf);
706
707 out:
708 if (error == -ENOMEM)
709 return VM_FAULT_OOM | major;
710 /* -EBUSY is fine, somebody else faulted on the same PTE */
711 if ((error < 0) && (error != -EBUSY))
712 return VM_FAULT_SIGBUS | major;
713 return VM_FAULT_NOPAGE | major;
714
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700715 unlock_page:
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800716 if (page) {
717 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300718 put_page(page);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800719 }
720 goto out;
721}
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000722EXPORT_SYMBOL(__dax_fault);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800723
724/**
725 * dax_fault - handle a page fault on a DAX file
726 * @vma: The virtual memory area where the fault occurred
727 * @vmf: The description of the fault
728 * @get_block: The filesystem method used to translate file offsets to blocks
729 *
730 * When a page fault occurs, filesystems may call this helper in their
731 * fault handler for DAX files.
732 */
733int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Jan Kara02fbd132016-05-11 11:58:48 +0200734 get_block_t get_block)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800735{
736 int result;
737 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
738
739 if (vmf->flags & FAULT_FLAG_WRITE) {
740 sb_start_pagefault(sb);
741 file_update_time(vma->vm_file);
742 }
Jan Kara02fbd132016-05-11 11:58:48 +0200743 result = __dax_fault(vma, vmf, get_block);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800744 if (vmf->flags & FAULT_FLAG_WRITE)
745 sb_end_pagefault(sb);
746
747 return result;
748}
749EXPORT_SYMBOL_GPL(dax_fault);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800750
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700751#ifdef CONFIG_TRANSPARENT_HUGEPAGE
752/*
753 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
754 * more often than one might expect in the below function.
755 */
756#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
757
Dan Williamscbb38e42016-01-15 16:56:58 -0800758static void __dax_dbg(struct buffer_head *bh, unsigned long address,
759 const char *reason, const char *fn)
760{
761 if (bh) {
762 char bname[BDEVNAME_SIZE];
763 bdevname(bh->b_bdev, bname);
764 pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
765 "length %zd fallback: %s\n", fn, current->comm,
766 address, bname, bh->b_state, (u64)bh->b_blocknr,
767 bh->b_size, reason);
768 } else {
769 pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
770 current->comm, address, reason);
771 }
772}
773
774#define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
775
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700776int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
Jan Kara02fbd132016-05-11 11:58:48 +0200777 pmd_t *pmd, unsigned int flags, get_block_t get_block)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700778{
779 struct file *file = vma->vm_file;
780 struct address_space *mapping = file->f_mapping;
781 struct inode *inode = mapping->host;
782 struct buffer_head bh;
783 unsigned blkbits = inode->i_blkbits;
784 unsigned long pmd_addr = address & PMD_MASK;
785 bool write = flags & FAULT_FLAG_WRITE;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800786 struct block_device *bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700787 pgoff_t size, pgoff;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800788 sector_t block;
Ross Zwisler9973c982016-01-22 15:10:47 -0800789 int error, result = 0;
790 bool alloc = false;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700791
Dan Williamsc046c322016-01-15 16:57:01 -0800792 /* dax pmd mappings require pfn_t_devmap() */
Dan Williamsee82c9e2015-11-15 16:06:32 -0800793 if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
794 return VM_FAULT_FALLBACK;
795
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700796 /* Fall back to PTEs if we're going to COW */
Toshi Kani59bf4fb2016-01-15 16:56:05 -0800797 if (write && !(vma->vm_flags & VM_SHARED)) {
798 split_huge_pmd(vma, pmd, address);
Dan Williamscbb38e42016-01-15 16:56:58 -0800799 dax_pmd_dbg(NULL, address, "cow write");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700800 return VM_FAULT_FALLBACK;
Toshi Kani59bf4fb2016-01-15 16:56:05 -0800801 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700802 /* If the PMD would extend outside the VMA */
Dan Williamscbb38e42016-01-15 16:56:58 -0800803 if (pmd_addr < vma->vm_start) {
804 dax_pmd_dbg(NULL, address, "vma start unaligned");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700805 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800806 }
807 if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
808 dax_pmd_dbg(NULL, address, "vma end unaligned");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700809 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800810 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700811
Matthew Wilcox3fdd1b472015-09-08 14:59:39 -0700812 pgoff = linear_page_index(vma, pmd_addr);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700813 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
814 if (pgoff >= size)
815 return VM_FAULT_SIGBUS;
816 /* If the PMD would cover blocks out of the file */
Dan Williamscbb38e42016-01-15 16:56:58 -0800817 if ((pgoff | PG_PMD_COLOUR) >= size) {
818 dax_pmd_dbg(NULL, address,
819 "offset + huge page size > file size");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700820 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800821 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700822
823 memset(&bh, 0, sizeof(bh));
Ross Zwislerd4bbe702016-01-22 15:10:31 -0800824 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700825 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
826
827 bh.b_size = PMD_SIZE;
Ross Zwisler9973c982016-01-22 15:10:47 -0800828
829 if (get_block(inode, block, &bh, 0) != 0)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700830 return VM_FAULT_SIGBUS;
Ross Zwisler9973c982016-01-22 15:10:47 -0800831
832 if (!buffer_mapped(&bh) && write) {
833 if (get_block(inode, block, &bh, 1) != 0)
834 return VM_FAULT_SIGBUS;
835 alloc = true;
Jan Kara2b109452016-05-11 11:58:50 +0200836 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
Ross Zwisler9973c982016-01-22 15:10:47 -0800837 }
838
Dan Williamsb2e0d162016-01-15 16:55:59 -0800839 bdev = bh.b_bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700840
841 /*
842 * If the filesystem isn't willing to tell us the length of a hole,
843 * just fall back to PTEs. Calling get_block 512 times in a loop
844 * would be silly.
845 */
Dan Williamscbb38e42016-01-15 16:56:58 -0800846 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
847 dax_pmd_dbg(&bh, address, "allocated block too small");
Ross Zwisler9973c982016-01-22 15:10:47 -0800848 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800849 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700850
Ross Zwisler9973c982016-01-22 15:10:47 -0800851 /*
852 * If we allocated new storage, make sure no process has any
853 * zero pages covering this hole
854 */
855 if (alloc) {
856 loff_t lstart = pgoff << PAGE_SHIFT;
857 loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
858
859 truncate_pagecache_range(inode, lstart, lend);
860 }
861
Ross Zwislerde14b9c2016-01-22 15:10:34 -0800862 i_mmap_lock_read(mapping);
Kirill A. Shutemov46c043e2015-09-08 14:59:42 -0700863
Matthew Wilcox84c4e5e2015-09-08 14:59:17 -0700864 /*
865 * If a truncate happened while we were allocating blocks, we may
866 * leave blocks allocated to the file that are beyond EOF. We can't
867 * take i_mutex here, so just leave them hanging; they'll be freed
868 * when the file is deleted.
869 */
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700870 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
871 if (pgoff >= size) {
872 result = VM_FAULT_SIGBUS;
873 goto out;
874 }
Dan Williamscbb38e42016-01-15 16:56:58 -0800875 if ((pgoff | PG_PMD_COLOUR) >= size) {
Ross Zwislerde14b9c2016-01-22 15:10:34 -0800876 dax_pmd_dbg(&bh, address,
877 "offset + huge page size > file size");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700878 goto fallback;
Dan Williamscbb38e42016-01-15 16:56:58 -0800879 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700880
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700881 if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700882 spinlock_t *ptl;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700883 pmd_t entry;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700884 struct page *zero_page = get_huge_zero_page();
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700885
Dan Williamscbb38e42016-01-15 16:56:58 -0800886 if (unlikely(!zero_page)) {
887 dax_pmd_dbg(&bh, address, "no zero page");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700888 goto fallback;
Dan Williamscbb38e42016-01-15 16:56:58 -0800889 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700890
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700891 ptl = pmd_lock(vma->vm_mm, pmd);
892 if (!pmd_none(*pmd)) {
893 spin_unlock(ptl);
Dan Williamscbb38e42016-01-15 16:56:58 -0800894 dax_pmd_dbg(&bh, address, "pmd already present");
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700895 goto fallback;
896 }
897
Dan Williamscbb38e42016-01-15 16:56:58 -0800898 dev_dbg(part_to_dev(bdev->bd_part),
899 "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
900 __func__, current->comm, address,
901 (unsigned long long) to_sector(&bh, inode));
902
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700903 entry = mk_pmd(zero_page, vma->vm_page_prot);
904 entry = pmd_mkhuge(entry);
905 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700906 result = VM_FAULT_NOPAGE;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700907 spin_unlock(ptl);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700908 } else {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800909 struct blk_dax_ctl dax = {
910 .sector = to_sector(&bh, inode),
911 .size = PMD_SIZE,
912 };
913 long length = dax_map_atomic(bdev, &dax);
914
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700915 if (length < 0) {
916 result = VM_FAULT_SIGBUS;
917 goto out;
918 }
Dan Williamscbb38e42016-01-15 16:56:58 -0800919 if (length < PMD_SIZE) {
920 dax_pmd_dbg(&bh, address, "dax-length too small");
921 dax_unmap_atomic(bdev, &dax);
922 goto fallback;
923 }
924 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
925 dax_pmd_dbg(&bh, address, "pfn unaligned");
Dan Williamsb2e0d162016-01-15 16:55:59 -0800926 dax_unmap_atomic(bdev, &dax);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700927 goto fallback;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800928 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700929
Dan Williamsc046c322016-01-15 16:57:01 -0800930 if (!pfn_t_devmap(dax.pfn)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800931 dax_unmap_atomic(bdev, &dax);
Dan Williamscbb38e42016-01-15 16:56:58 -0800932 dax_pmd_dbg(&bh, address, "pfn not in memmap");
Dan Williams152d7bd2015-11-12 18:33:54 -0800933 goto fallback;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800934 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800935 dax_unmap_atomic(bdev, &dax);
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700936
Ross Zwisler9973c982016-01-22 15:10:47 -0800937 /*
938 * For PTE faults we insert a radix tree entry for reads, and
939 * leave it clean. Then on the first write we dirty the radix
940 * tree entry via the dax_pfn_mkwrite() path. This sequence
941 * allows the dax_pfn_mkwrite() call to be simpler and avoid a
942 * call into get_block() to translate the pgoff to a sector in
943 * order to be able to create a new radix tree entry.
944 *
945 * The PMD path doesn't have an equivalent to
946 * dax_pfn_mkwrite(), though, so for a read followed by a
947 * write we traverse all the way through __dax_pmd_fault()
948 * twice. This means we can just skip inserting a radix tree
949 * entry completely on the initial read and just wait until
950 * the write to insert a dirty entry.
951 */
952 if (write) {
953 error = dax_radix_entry(mapping, pgoff, dax.sector,
954 true, true);
955 if (error) {
956 dax_pmd_dbg(&bh, address,
957 "PMD radix insertion failed");
958 goto fallback;
959 }
960 }
961
Dan Williamscbb38e42016-01-15 16:56:58 -0800962 dev_dbg(part_to_dev(bdev->bd_part),
963 "%s: %s addr: %lx pfn: %lx sect: %llx\n",
964 __func__, current->comm, address,
965 pfn_t_to_pfn(dax.pfn),
966 (unsigned long long) dax.sector);
Dan Williams34c0fd52016-01-15 16:56:14 -0800967 result |= vmf_insert_pfn_pmd(vma, address, pmd,
Dan Williamsf25748e32016-01-15 16:56:43 -0800968 dax.pfn, write);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700969 }
970
971 out:
Ross Zwisler0f90cc62015-10-15 15:28:32 -0700972 i_mmap_unlock_read(mapping);
973
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700974 return result;
975
976 fallback:
977 count_vm_event(THP_FAULT_FALLBACK);
978 result = VM_FAULT_FALLBACK;
979 goto out;
980}
981EXPORT_SYMBOL_GPL(__dax_pmd_fault);
982
983/**
984 * dax_pmd_fault - handle a PMD fault on a DAX file
985 * @vma: The virtual memory area where the fault occurred
986 * @vmf: The description of the fault
987 * @get_block: The filesystem method used to translate file offsets to blocks
988 *
989 * When a page fault occurs, filesystems may call this helper in their
990 * pmd_fault handler for DAX files.
991 */
992int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
Jan Kara02fbd132016-05-11 11:58:48 +0200993 pmd_t *pmd, unsigned int flags, get_block_t get_block)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700994{
995 int result;
996 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
997
998 if (flags & FAULT_FLAG_WRITE) {
999 sb_start_pagefault(sb);
1000 file_update_time(vma->vm_file);
1001 }
Jan Kara02fbd132016-05-11 11:58:48 +02001002 result = __dax_pmd_fault(vma, address, pmd, flags, get_block);
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001003 if (flags & FAULT_FLAG_WRITE)
1004 sb_end_pagefault(sb);
1005
1006 return result;
1007}
1008EXPORT_SYMBOL_GPL(dax_pmd_fault);
Valentin Rothbergdd8a2b62015-09-08 14:59:09 -07001009#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001010
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001011/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001012 * dax_pfn_mkwrite - handle first write to DAX page
1013 * @vma: The virtual memory area where the fault occurred
1014 * @vmf: The description of the fault
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001015 */
1016int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1017{
Ross Zwisler9973c982016-01-22 15:10:47 -08001018 struct file *file = vma->vm_file;
Ross Zwisler30f471f2016-03-09 14:08:27 -08001019 int error;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001020
Ross Zwisler9973c982016-01-22 15:10:47 -08001021 /*
1022 * We pass NO_SECTOR to dax_radix_entry() because we expect that a
1023 * RADIX_DAX_PTE entry already exists in the radix tree from a
1024 * previous call to __dax_fault(). We just want to look up that PTE
1025 * entry using vmf->pgoff and make sure the dirty tag is set. This
1026 * saves us from having to make a call to get_block() here to look
1027 * up the sector.
1028 */
Ross Zwisler30f471f2016-03-09 14:08:27 -08001029 error = dax_radix_entry(file->f_mapping, vmf->pgoff, NO_SECTOR, false,
1030 true);
1031
1032 if (error == -ENOMEM)
1033 return VM_FAULT_OOM;
1034 if (error)
1035 return VM_FAULT_SIGBUS;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001036 return VM_FAULT_NOPAGE;
1037}
1038EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
1039
1040/**
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001041 * dax_zero_page_range - zero a range within a page of a DAX file
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001042 * @inode: The file being truncated
1043 * @from: The file offset that is being truncated to
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001044 * @length: The number of bytes to zero
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001045 * @get_block: The filesystem method used to translate file offsets to blocks
1046 *
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001047 * This function can be called by a filesystem when it is zeroing part of a
1048 * page in a DAX file. This is intended for hole-punch operations. If
1049 * you are truncating a file, the helper function dax_truncate_page() may be
1050 * more convenient.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001051 *
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03001052 * We work in terms of PAGE_SIZE here for commonality with
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001053 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
1054 * took care of disposing of the unnecessary blocks. Even if the filesystem
1055 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001056 * since the file might be mmapped.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001057 */
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001058int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
1059 get_block_t get_block)
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001060{
1061 struct buffer_head bh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001062 pgoff_t index = from >> PAGE_SHIFT;
1063 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001064 int err;
1065
1066 /* Block boundary? Nothing to do */
1067 if (!length)
1068 return 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001069 BUG_ON((offset + length) > PAGE_SIZE);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001070
1071 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -08001072 bh.b_bdev = inode->i_sb->s_bdev;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001073 bh.b_size = PAGE_SIZE;
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001074 err = get_block(inode, index, &bh, 0);
1075 if (err < 0)
1076 return err;
1077 if (buffer_written(&bh)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -08001078 struct block_device *bdev = bh.b_bdev;
1079 struct blk_dax_ctl dax = {
1080 .sector = to_sector(&bh, inode),
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001081 .size = PAGE_SIZE,
Dan Williamsb2e0d162016-01-15 16:55:59 -08001082 };
1083
1084 if (dax_map_atomic(bdev, &dax) < 0)
1085 return PTR_ERR(dax.addr);
1086 clear_pmem(dax.addr + offset, length);
Ross Zwisler2765cfb2015-08-18 13:55:40 -06001087 wmb_pmem();
Dan Williamsb2e0d162016-01-15 16:55:59 -08001088 dax_unmap_atomic(bdev, &dax);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001089 }
1090
1091 return 0;
1092}
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001093EXPORT_SYMBOL_GPL(dax_zero_page_range);
1094
1095/**
1096 * dax_truncate_page - handle a partial page being truncated in a DAX file
1097 * @inode: The file being truncated
1098 * @from: The file offset that is being truncated to
1099 * @get_block: The filesystem method used to translate file offsets to blocks
1100 *
1101 * Similar to block_truncate_page(), this function can be called by a
1102 * filesystem when it is truncating a DAX file to handle the partial page.
1103 *
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03001104 * We work in terms of PAGE_SIZE here for commonality with
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001105 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
1106 * took care of disposing of the unnecessary blocks. Even if the filesystem
1107 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
1108 * since the file might be mmapped.
1109 */
1110int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1111{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001112 unsigned length = PAGE_ALIGN(from) - from;
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001113 return dax_zero_page_range(inode, from, length, get_block);
1114}
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001115EXPORT_SYMBOL_GPL(dax_truncate_page);