blob: e52e7543c3519344c059468c455869bc50fcffea [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>
Christoph Hellwiga254e562016-09-19 11:24:49 +100034#include <linux/iomap.h>
35#include "internal.h"
Matthew Wilcoxd475c632015-02-16 15:58:56 -080036
Jan Karae8043152016-05-12 18:29:16 +020037/*
38 * We use lowest available bit in exceptional entry for locking, other two
39 * bits to determine entry type. In total 3 special bits.
40 */
41#define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 3)
42#define RADIX_DAX_PTE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1))
43#define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2))
44#define RADIX_DAX_TYPE_MASK (RADIX_DAX_PTE | RADIX_DAX_PMD)
45#define RADIX_DAX_TYPE(entry) ((unsigned long)entry & RADIX_DAX_TYPE_MASK)
NeilBrown78a9be02016-05-20 17:03:51 -070046#define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT))
47#define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \
Jan Karae8043152016-05-12 18:29:16 +020048 RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE) | \
49 RADIX_TREE_EXCEPTIONAL_ENTRY))
NeilBrowne4b27492016-05-11 11:58:47 +020050
Jan Karaac401cc2016-05-12 18:29:18 +020051/* We choose 4096 entries - same as per-zone page wait tables */
52#define DAX_WAIT_TABLE_BITS 12
53#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
54
Ross Zwislerce95ab02016-11-08 11:31:44 +110055static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020056
57static int __init init_dax_wait_table(void)
58{
59 int i;
60
61 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
62 init_waitqueue_head(wait_table + i);
63 return 0;
64}
65fs_initcall(init_dax_wait_table);
66
67static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
68 pgoff_t index)
69{
70 unsigned long hash = hash_long((unsigned long)mapping ^ index,
71 DAX_WAIT_TABLE_BITS);
72 return wait_table + hash;
73}
NeilBrown78a9be02016-05-20 17:03:51 -070074
Dan Williamsb2e0d162016-01-15 16:55:59 -080075static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
76{
77 struct request_queue *q = bdev->bd_queue;
78 long rc = -EIO;
79
Dan Williams7a9eb202016-06-03 18:06:47 -070080 dax->addr = ERR_PTR(-EIO);
Dan Williamsb2e0d162016-01-15 16:55:59 -080081 if (blk_queue_enter(q, true) != 0)
82 return rc;
83
84 rc = bdev_direct_access(bdev, dax);
85 if (rc < 0) {
Dan Williams7a9eb202016-06-03 18:06:47 -070086 dax->addr = ERR_PTR(rc);
Dan Williamsb2e0d162016-01-15 16:55:59 -080087 blk_queue_exit(q);
88 return rc;
89 }
90 return rc;
91}
92
93static void dax_unmap_atomic(struct block_device *bdev,
94 const struct blk_dax_ctl *dax)
95{
96 if (IS_ERR(dax->addr))
97 return;
98 blk_queue_exit(bdev->bd_queue);
99}
100
Dan Williamsd1a5f2b42016-01-28 20:25:31 -0800101struct page *read_dax_sector(struct block_device *bdev, sector_t n)
102{
103 struct page *page = alloc_pages(GFP_KERNEL, 0);
104 struct blk_dax_ctl dax = {
105 .size = PAGE_SIZE,
106 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
107 };
108 long rc;
109
110 if (!page)
111 return ERR_PTR(-ENOMEM);
112
113 rc = dax_map_atomic(bdev, &dax);
114 if (rc < 0)
115 return ERR_PTR(rc);
116 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
117 dax_unmap_atomic(bdev, &dax);
118 return page;
119}
120
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800121static bool buffer_written(struct buffer_head *bh)
122{
123 return buffer_mapped(bh) && !buffer_unwritten(bh);
124}
125
Dan Williamsb2e0d162016-01-15 16:55:59 -0800126static sector_t to_sector(const struct buffer_head *bh,
127 const struct inode *inode)
128{
129 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
130
131 return sector;
132}
133
Omar Sandovala95cd632015-03-16 04:33:51 -0700134static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
135 loff_t start, loff_t end, get_block_t get_block,
136 struct buffer_head *bh)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800137{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800138 loff_t pos = start, max = start, bh_max = start;
Dan Williams14df6a42016-06-01 21:03:32 -0700139 bool hole = false;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800140 struct block_device *bdev = NULL;
141 int rw = iov_iter_rw(iter), rc;
142 long map_len = 0;
143 struct blk_dax_ctl dax = {
Dan Williams7a9eb202016-06-03 18:06:47 -0700144 .addr = ERR_PTR(-EIO),
Dan Williamsb2e0d162016-01-15 16:55:59 -0800145 };
Jan Kara069c77b2016-05-11 11:58:51 +0200146 unsigned blkbits = inode->i_blkbits;
147 sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1)
148 >> blkbits;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800149
Dan Williamsb2e0d162016-01-15 16:55:59 -0800150 if (rw == READ)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800151 end = min(end, i_size_read(inode));
152
153 while (pos < end) {
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600154 size_t len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800155 if (pos == max) {
Jeff Moyere94f5a22015-08-14 16:15:31 -0400156 long page = pos >> PAGE_SHIFT;
157 sector_t block = page << (PAGE_SHIFT - blkbits);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800158 unsigned first = pos - (block << blkbits);
159 long size;
160
161 if (pos == bh_max) {
162 bh->b_size = PAGE_ALIGN(end - pos);
163 bh->b_state = 0;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800164 rc = get_block(inode, block, bh, rw == WRITE);
165 if (rc)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800166 break;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800167 bh_max = pos - first + bh->b_size;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800168 bdev = bh->b_bdev;
Jan Kara069c77b2016-05-11 11:58:51 +0200169 /*
170 * We allow uninitialized buffers for writes
171 * beyond EOF as those cannot race with faults
172 */
173 WARN_ON_ONCE(
174 (buffer_new(bh) && block < file_blks) ||
175 (rw == WRITE && buffer_unwritten(bh)));
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800176 } else {
177 unsigned done = bh->b_size -
178 (bh_max - (pos - first));
179 bh->b_blocknr += done >> blkbits;
180 bh->b_size -= done;
181 }
182
Dan Williamsb2e0d162016-01-15 16:55:59 -0800183 hole = rw == READ && !buffer_written(bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800184 if (hole) {
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800185 size = bh->b_size - first;
186 } else {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800187 dax_unmap_atomic(bdev, &dax);
188 dax.sector = to_sector(bh, inode);
189 dax.size = bh->b_size;
190 map_len = dax_map_atomic(bdev, &dax);
191 if (map_len < 0) {
192 rc = map_len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800193 break;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800194 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800195 dax.addr += first;
196 size = map_len - first;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800197 }
Eric Sandeen02395432016-06-23 16:54:46 -0500198 /*
199 * pos + size is one past the last offset for IO,
200 * so pos + size can overflow loff_t at extreme offsets.
201 * Cast to u64 to catch this and get the true minimum.
202 */
203 max = min_t(u64, pos + size, end);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800204 }
205
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600206 if (iov_iter_rw(iter) == WRITE) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800207 len = copy_from_iter_pmem(dax.addr, max - pos, iter);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600208 } else if (!hole)
Dan Williamsb2e0d162016-01-15 16:55:59 -0800209 len = copy_to_iter((void __force *) dax.addr, max - pos,
Ross Zwislere2e05392015-08-18 13:55:41 -0600210 iter);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800211 else
212 len = iov_iter_zero(max - pos, iter);
213
Al Virocadfbb62015-11-10 19:42:49 -0700214 if (!len) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800215 rc = -EFAULT;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800216 break;
Al Virocadfbb62015-11-10 19:42:49 -0700217 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800218
219 pos += len;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800220 if (!IS_ERR(dax.addr))
221 dax.addr += len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800222 }
223
Dan Williamsb2e0d162016-01-15 16:55:59 -0800224 dax_unmap_atomic(bdev, &dax);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600225
Dan Williamsb2e0d162016-01-15 16:55:59 -0800226 return (pos == start) ? rc : pos - start;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800227}
228
229/**
230 * dax_do_io - Perform I/O to a DAX file
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800231 * @iocb: The control block for this I/O
232 * @inode: The file which the I/O is directed at
233 * @iter: The addresses to do I/O from or to
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800234 * @get_block: The filesystem method used to translate file offsets to blocks
235 * @end_io: A filesystem callback for I/O completion
236 * @flags: See below
237 *
238 * This function uses the same locking scheme as do_blockdev_direct_IO:
239 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
240 * caller for writes. For reads, we take and release the i_mutex ourselves.
241 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
242 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
243 * is in progress.
244 */
Omar Sandovala95cd632015-03-16 04:33:51 -0700245ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700246 struct iov_iter *iter, get_block_t get_block,
Omar Sandovala95cd632015-03-16 04:33:51 -0700247 dio_iodone_t end_io, int flags)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800248{
249 struct buffer_head bh;
250 ssize_t retval = -EINVAL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700251 loff_t pos = iocb->ki_pos;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800252 loff_t end = pos + iov_iter_count(iter);
253
254 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -0800255 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800256
Jan Karac3d98e32016-05-11 11:58:52 +0200257 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Al Viro59551022016-01-22 15:40:57 -0500258 inode_lock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800259
260 /* Protects against truncate */
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400261 if (!(flags & DIO_SKIP_DIO_COUNT))
262 inode_dio_begin(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800263
Omar Sandovala95cd632015-03-16 04:33:51 -0700264 retval = dax_io(inode, iter, pos, end, get_block, &bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800265
Omar Sandovala95cd632015-03-16 04:33:51 -0700266 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Al Viro59551022016-01-22 15:40:57 -0500267 inode_unlock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800268
Christoph Hellwig187372a2016-02-08 14:40:51 +1100269 if (end_io) {
270 int err;
271
272 err = end_io(iocb, pos, retval, bh.b_private);
273 if (err)
274 retval = err;
275 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800276
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400277 if (!(flags & DIO_SKIP_DIO_COUNT))
278 inode_dio_end(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800279 return retval;
280}
281EXPORT_SYMBOL_GPL(dax_do_io);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800282
283/*
Jan Karaac401cc2016-05-12 18:29:18 +0200284 * DAX radix tree locking
285 */
286struct exceptional_entry_key {
287 struct address_space *mapping;
288 unsigned long index;
289};
290
291struct wait_exceptional_entry_queue {
292 wait_queue_t wait;
293 struct exceptional_entry_key key;
294};
295
296static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
297 int sync, void *keyp)
298{
299 struct exceptional_entry_key *key = keyp;
300 struct wait_exceptional_entry_queue *ewait =
301 container_of(wait, struct wait_exceptional_entry_queue, wait);
302
303 if (key->mapping != ewait->key.mapping ||
304 key->index != ewait->key.index)
305 return 0;
306 return autoremove_wake_function(wait, mode, sync, NULL);
307}
308
309/*
310 * Check whether the given slot is locked. The function must be called with
311 * mapping->tree_lock held
312 */
313static inline int slot_locked(struct address_space *mapping, void **slot)
314{
315 unsigned long entry = (unsigned long)
316 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
317 return entry & RADIX_DAX_ENTRY_LOCK;
318}
319
320/*
321 * Mark the given slot is locked. The function must be called with
322 * mapping->tree_lock held
323 */
324static inline void *lock_slot(struct address_space *mapping, void **slot)
325{
326 unsigned long entry = (unsigned long)
327 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
328
329 entry |= RADIX_DAX_ENTRY_LOCK;
330 radix_tree_replace_slot(slot, (void *)entry);
331 return (void *)entry;
332}
333
334/*
335 * Mark the given slot is unlocked. The function must be called with
336 * mapping->tree_lock held
337 */
338static inline void *unlock_slot(struct address_space *mapping, void **slot)
339{
340 unsigned long entry = (unsigned long)
341 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
342
343 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
344 radix_tree_replace_slot(slot, (void *)entry);
345 return (void *)entry;
346}
347
348/*
349 * Lookup entry in radix tree, wait for it to become unlocked if it is
350 * exceptional entry and return it. The caller must call
351 * put_unlocked_mapping_entry() when he decided not to lock the entry or
352 * put_locked_mapping_entry() when he locked the entry and now wants to
353 * unlock it.
354 *
355 * The function must be called with mapping->tree_lock held.
356 */
357static void *get_unlocked_mapping_entry(struct address_space *mapping,
358 pgoff_t index, void ***slotp)
359{
360 void *ret, **slot;
361 struct wait_exceptional_entry_queue ewait;
362 wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index);
363
364 init_wait(&ewait.wait);
365 ewait.wait.func = wake_exceptional_entry_func;
366 ewait.key.mapping = mapping;
367 ewait.key.index = index;
368
369 for (;;) {
370 ret = __radix_tree_lookup(&mapping->page_tree, index, NULL,
371 &slot);
372 if (!ret || !radix_tree_exceptional_entry(ret) ||
373 !slot_locked(mapping, slot)) {
374 if (slotp)
375 *slotp = slot;
376 return ret;
377 }
378 prepare_to_wait_exclusive(wq, &ewait.wait,
379 TASK_UNINTERRUPTIBLE);
380 spin_unlock_irq(&mapping->tree_lock);
381 schedule();
382 finish_wait(wq, &ewait.wait);
383 spin_lock_irq(&mapping->tree_lock);
384 }
385}
386
387/*
388 * Find radix tree entry at given index. If it points to a page, return with
389 * the page locked. If it points to the exceptional entry, return with the
390 * radix tree entry locked. If the radix tree doesn't contain given index,
391 * create empty exceptional entry for the index and return with it locked.
392 *
393 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
394 * persistent memory the benefit is doubtful. We can add that later if we can
395 * show it helps.
396 */
397static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index)
398{
399 void *ret, **slot;
400
401restart:
402 spin_lock_irq(&mapping->tree_lock);
403 ret = get_unlocked_mapping_entry(mapping, index, &slot);
404 /* No entry for given index? Make sure radix tree is big enough. */
405 if (!ret) {
406 int err;
407
408 spin_unlock_irq(&mapping->tree_lock);
409 err = radix_tree_preload(
410 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
411 if (err)
412 return ERR_PTR(err);
413 ret = (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY |
414 RADIX_DAX_ENTRY_LOCK);
415 spin_lock_irq(&mapping->tree_lock);
416 err = radix_tree_insert(&mapping->page_tree, index, ret);
417 radix_tree_preload_end();
418 if (err) {
419 spin_unlock_irq(&mapping->tree_lock);
420 /* Someone already created the entry? */
421 if (err == -EEXIST)
422 goto restart;
423 return ERR_PTR(err);
424 }
425 /* Good, we have inserted empty locked entry into the tree. */
426 mapping->nrexceptional++;
427 spin_unlock_irq(&mapping->tree_lock);
428 return ret;
429 }
430 /* Normal page in radix tree? */
431 if (!radix_tree_exceptional_entry(ret)) {
432 struct page *page = ret;
433
434 get_page(page);
435 spin_unlock_irq(&mapping->tree_lock);
436 lock_page(page);
437 /* Page got truncated? Retry... */
438 if (unlikely(page->mapping != mapping)) {
439 unlock_page(page);
440 put_page(page);
441 goto restart;
442 }
443 return page;
444 }
445 ret = lock_slot(mapping, slot);
446 spin_unlock_irq(&mapping->tree_lock);
447 return ret;
448}
449
450void dax_wake_mapping_entry_waiter(struct address_space *mapping,
451 pgoff_t index, bool wake_all)
452{
453 wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index);
454
455 /*
456 * Checking for locked entry and prepare_to_wait_exclusive() happens
457 * under mapping->tree_lock, ditto for entry handling in our callers.
458 * So at this point all tasks that could have seen our entry locked
459 * must be in the waitqueue and the following check will see them.
460 */
461 if (waitqueue_active(wq)) {
462 struct exceptional_entry_key key;
463
464 key.mapping = mapping;
465 key.index = index;
466 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
467 }
468}
469
Jan Karabc2466e2016-05-12 18:29:19 +0200470void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
Jan Karaac401cc2016-05-12 18:29:18 +0200471{
472 void *ret, **slot;
473
474 spin_lock_irq(&mapping->tree_lock);
475 ret = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
476 if (WARN_ON_ONCE(!ret || !radix_tree_exceptional_entry(ret) ||
477 !slot_locked(mapping, slot))) {
478 spin_unlock_irq(&mapping->tree_lock);
479 return;
480 }
481 unlock_slot(mapping, slot);
482 spin_unlock_irq(&mapping->tree_lock);
483 dax_wake_mapping_entry_waiter(mapping, index, false);
484}
485
486static void put_locked_mapping_entry(struct address_space *mapping,
487 pgoff_t index, void *entry)
488{
489 if (!radix_tree_exceptional_entry(entry)) {
490 unlock_page(entry);
491 put_page(entry);
492 } else {
Jan Karabc2466e2016-05-12 18:29:19 +0200493 dax_unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200494 }
495}
496
497/*
498 * Called when we are done with radix tree entry we looked up via
499 * get_unlocked_mapping_entry() and which we didn't lock in the end.
500 */
501static void put_unlocked_mapping_entry(struct address_space *mapping,
502 pgoff_t index, void *entry)
503{
504 if (!radix_tree_exceptional_entry(entry))
505 return;
506
507 /* We have to wake up next waiter for the radix tree entry lock */
508 dax_wake_mapping_entry_waiter(mapping, index, false);
509}
510
511/*
512 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
513 * entry to get unlocked before deleting it.
514 */
515int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
516{
517 void *entry;
518
519 spin_lock_irq(&mapping->tree_lock);
520 entry = get_unlocked_mapping_entry(mapping, index, NULL);
521 /*
522 * This gets called from truncate / punch_hole path. As such, the caller
523 * must hold locks protecting against concurrent modifications of the
524 * radix tree (usually fs-private i_mmap_sem for writing). Since the
525 * caller has seen exceptional entry for this index, we better find it
526 * at that index as well...
527 */
528 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
529 spin_unlock_irq(&mapping->tree_lock);
530 return 0;
531 }
532 radix_tree_delete(&mapping->page_tree, index);
533 mapping->nrexceptional--;
534 spin_unlock_irq(&mapping->tree_lock);
535 dax_wake_mapping_entry_waiter(mapping, index, true);
536
537 return 1;
538}
539
540/*
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800541 * The user has performed a load from a hole in the file. Allocating
542 * a new page in the file would cause excessive storage usage for
543 * workloads with sparse files. We allocate a page cache page instead.
544 * We'll kick it out of the page cache if it's ever written to,
545 * otherwise it will simply fall out of the page cache under memory
546 * pressure without ever having been dirtied.
547 */
Jan Karaac401cc2016-05-12 18:29:18 +0200548static int dax_load_hole(struct address_space *mapping, void *entry,
549 struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800550{
Jan Karaac401cc2016-05-12 18:29:18 +0200551 struct page *page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800552
Jan Karaac401cc2016-05-12 18:29:18 +0200553 /* Hole page already exists? Return it... */
554 if (!radix_tree_exceptional_entry(entry)) {
555 vmf->page = entry;
556 return VM_FAULT_LOCKED;
557 }
558
559 /* This will replace locked radix tree entry with a hole page */
560 page = find_or_create_page(mapping, vmf->pgoff,
561 vmf->gfp_mask | __GFP_ZERO);
562 if (!page) {
563 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
564 return VM_FAULT_OOM;
565 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800566 vmf->page = page;
567 return VM_FAULT_LOCKED;
568}
569
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000570static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
571 struct page *to, unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800572{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800573 struct blk_dax_ctl dax = {
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000574 .sector = sector,
575 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800576 };
Ross Zwislere2e05392015-08-18 13:55:41 -0600577 void *vto;
578
Dan Williamsb2e0d162016-01-15 16:55:59 -0800579 if (dax_map_atomic(bdev, &dax) < 0)
580 return PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800581 vto = kmap_atomic(to);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800582 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800583 kunmap_atomic(vto);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800584 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800585 return 0;
586}
587
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300588#define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
Ross Zwisler9973c982016-01-22 15:10:47 -0800589
Jan Karaac401cc2016-05-12 18:29:18 +0200590static void *dax_insert_mapping_entry(struct address_space *mapping,
591 struct vm_fault *vmf,
592 void *entry, sector_t sector)
Ross Zwisler9973c982016-01-22 15:10:47 -0800593{
594 struct radix_tree_root *page_tree = &mapping->page_tree;
Jan Karaac401cc2016-05-12 18:29:18 +0200595 int error = 0;
596 bool hole_fill = false;
597 void *new_entry;
598 pgoff_t index = vmf->pgoff;
Ross Zwisler9973c982016-01-22 15:10:47 -0800599
Jan Karaac401cc2016-05-12 18:29:18 +0200600 if (vmf->flags & FAULT_FLAG_WRITE)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800601 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800602
Jan Karaac401cc2016-05-12 18:29:18 +0200603 /* Replacing hole page with block mapping? */
604 if (!radix_tree_exceptional_entry(entry)) {
605 hole_fill = true;
606 /*
607 * Unmap the page now before we remove it from page cache below.
608 * The page is locked so it cannot be faulted in again.
609 */
610 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
611 PAGE_SIZE, 0);
612 error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
613 if (error)
614 return ERR_PTR(error);
Ross Zwisler9973c982016-01-22 15:10:47 -0800615 }
616
Jan Karaac401cc2016-05-12 18:29:18 +0200617 spin_lock_irq(&mapping->tree_lock);
618 new_entry = (void *)((unsigned long)RADIX_DAX_ENTRY(sector, false) |
619 RADIX_DAX_ENTRY_LOCK);
620 if (hole_fill) {
621 __delete_from_page_cache(entry, NULL);
622 /* Drop pagecache reference */
623 put_page(entry);
624 error = radix_tree_insert(page_tree, index, new_entry);
625 if (error) {
626 new_entry = ERR_PTR(error);
Ross Zwisler9973c982016-01-22 15:10:47 -0800627 goto unlock;
628 }
Jan Karaac401cc2016-05-12 18:29:18 +0200629 mapping->nrexceptional++;
630 } else {
631 void **slot;
632 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800633
Jan Karaac401cc2016-05-12 18:29:18 +0200634 ret = __radix_tree_lookup(page_tree, index, NULL, &slot);
635 WARN_ON_ONCE(ret != entry);
636 radix_tree_replace_slot(slot, new_entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800637 }
Jan Karaac401cc2016-05-12 18:29:18 +0200638 if (vmf->flags & FAULT_FLAG_WRITE)
Ross Zwisler9973c982016-01-22 15:10:47 -0800639 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
640 unlock:
641 spin_unlock_irq(&mapping->tree_lock);
Jan Karaac401cc2016-05-12 18:29:18 +0200642 if (hole_fill) {
643 radix_tree_preload_end();
644 /*
645 * We don't need hole page anymore, it has been replaced with
646 * locked radix tree entry now.
647 */
648 if (mapping->a_ops->freepage)
649 mapping->a_ops->freepage(entry);
650 unlock_page(entry);
651 put_page(entry);
652 }
653 return new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800654}
655
656static int dax_writeback_one(struct block_device *bdev,
657 struct address_space *mapping, pgoff_t index, void *entry)
658{
659 struct radix_tree_root *page_tree = &mapping->page_tree;
660 int type = RADIX_DAX_TYPE(entry);
661 struct radix_tree_node *node;
662 struct blk_dax_ctl dax;
663 void **slot;
664 int ret = 0;
665
666 spin_lock_irq(&mapping->tree_lock);
667 /*
668 * Regular page slots are stabilized by the page lock even
669 * without the tree itself locked. These unlocked entries
670 * need verification under the tree lock.
671 */
672 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
673 goto unlock;
674 if (*slot != entry)
675 goto unlock;
676
677 /* another fsync thread may have already written back this entry */
678 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
679 goto unlock;
680
681 if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
682 ret = -EIO;
683 goto unlock;
684 }
685
686 dax.sector = RADIX_DAX_SECTOR(entry);
687 dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
688 spin_unlock_irq(&mapping->tree_lock);
689
690 /*
691 * We cannot hold tree_lock while calling dax_map_atomic() because it
692 * eventually calls cond_resched().
693 */
694 ret = dax_map_atomic(bdev, &dax);
695 if (ret < 0)
696 return ret;
697
698 if (WARN_ON_ONCE(ret < dax.size)) {
699 ret = -EIO;
700 goto unmap;
701 }
702
703 wb_cache_pmem(dax.addr, dax.size);
704
705 spin_lock_irq(&mapping->tree_lock);
706 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
707 spin_unlock_irq(&mapping->tree_lock);
708 unmap:
709 dax_unmap_atomic(bdev, &dax);
710 return ret;
711
712 unlock:
713 spin_unlock_irq(&mapping->tree_lock);
714 return ret;
715}
716
717/*
718 * Flush the mapping to the persistent domain within the byte range of [start,
719 * end]. This is required by data integrity operations to ensure file data is
720 * on persistent storage prior to completion of the operation.
721 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800722int dax_writeback_mapping_range(struct address_space *mapping,
723 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800724{
725 struct inode *inode = mapping->host;
Ross Zwisler9973c982016-01-22 15:10:47 -0800726 pgoff_t start_index, end_index, pmd_index;
727 pgoff_t indices[PAGEVEC_SIZE];
728 struct pagevec pvec;
729 bool done = false;
730 int i, ret = 0;
731 void *entry;
732
733 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
734 return -EIO;
735
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800736 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
737 return 0;
738
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300739 start_index = wbc->range_start >> PAGE_SHIFT;
740 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800741 pmd_index = DAX_PMD_INDEX(start_index);
742
743 rcu_read_lock();
744 entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
745 rcu_read_unlock();
746
747 /* see if the start of our range is covered by a PMD entry */
748 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
749 start_index = pmd_index;
750
751 tag_pages_for_writeback(mapping, start_index, end_index);
752
753 pagevec_init(&pvec, 0);
754 while (!done) {
755 pvec.nr = find_get_entries_tag(mapping, start_index,
756 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
757 pvec.pages, indices);
758
759 if (pvec.nr == 0)
760 break;
761
762 for (i = 0; i < pvec.nr; i++) {
763 if (indices[i] > end_index) {
764 done = true;
765 break;
766 }
767
768 ret = dax_writeback_one(bdev, mapping, indices[i],
769 pvec.pages[i]);
770 if (ret < 0)
771 return ret;
772 }
773 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800774 return 0;
775}
776EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
777
Jan Karaac401cc2016-05-12 18:29:18 +0200778static int dax_insert_mapping(struct address_space *mapping,
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000779 struct block_device *bdev, sector_t sector, size_t size,
780 void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800781{
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800782 unsigned long vaddr = (unsigned long)vmf->virtual_address;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800783 struct blk_dax_ctl dax = {
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000784 .sector = sector,
785 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800786 };
Jan Karaac401cc2016-05-12 18:29:18 +0200787 void *ret;
788 void *entry = *entryp;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800789
Jan Kara4d9a2c82016-05-12 18:29:20 +0200790 if (dax_map_atomic(bdev, &dax) < 0)
791 return PTR_ERR(dax.addr);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800792 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800793
Jan Karaac401cc2016-05-12 18:29:18 +0200794 ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector);
Jan Kara4d9a2c82016-05-12 18:29:20 +0200795 if (IS_ERR(ret))
796 return PTR_ERR(ret);
Jan Karaac401cc2016-05-12 18:29:18 +0200797 *entryp = ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800798
Jan Kara4d9a2c82016-05-12 18:29:20 +0200799 return vm_insert_mixed(vma, vaddr, dax.pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800800}
801
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000802/**
Ross Zwisler6b524992016-07-26 15:21:05 -0700803 * dax_fault - handle a page fault on a DAX file
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000804 * @vma: The virtual memory area where the fault occurred
805 * @vmf: The description of the fault
806 * @get_block: The filesystem method used to translate file offsets to blocks
807 *
808 * When a page fault occurs, filesystems may call this helper in their
Ross Zwisler6b524992016-07-26 15:21:05 -0700809 * fault handler for DAX files. dax_fault() assumes the caller has done all
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000810 * the necessary locking for the page fault to proceed successfully.
811 */
Ross Zwisler6b524992016-07-26 15:21:05 -0700812int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Jan Kara02fbd132016-05-11 11:58:48 +0200813 get_block_t get_block)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800814{
815 struct file *file = vma->vm_file;
816 struct address_space *mapping = file->f_mapping;
817 struct inode *inode = mapping->host;
Jan Karaac401cc2016-05-12 18:29:18 +0200818 void *entry;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800819 struct buffer_head bh;
820 unsigned long vaddr = (unsigned long)vmf->virtual_address;
821 unsigned blkbits = inode->i_blkbits;
822 sector_t block;
823 pgoff_t size;
824 int error;
825 int major = 0;
826
Jan Karaac401cc2016-05-12 18:29:18 +0200827 /*
828 * Check whether offset isn't beyond end of file now. Caller is supposed
829 * to hold locks serializing us with truncate / punch hole so this is
830 * a reliable test.
831 */
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800832 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
833 if (vmf->pgoff >= size)
834 return VM_FAULT_SIGBUS;
835
836 memset(&bh, 0, sizeof(bh));
837 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
Ross Zwislereab95db2016-01-22 15:10:59 -0800838 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800839 bh.b_size = PAGE_SIZE;
840
Jan Karaac401cc2016-05-12 18:29:18 +0200841 entry = grab_mapping_entry(mapping, vmf->pgoff);
842 if (IS_ERR(entry)) {
843 error = PTR_ERR(entry);
844 goto out;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800845 }
846
847 error = get_block(inode, block, &bh, 0);
848 if (!error && (bh.b_size < PAGE_SIZE))
849 error = -EIO; /* fs corruption? */
850 if (error)
Jan Karaac401cc2016-05-12 18:29:18 +0200851 goto unlock_entry;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800852
853 if (vmf->cow_page) {
854 struct page *new_page = vmf->cow_page;
855 if (buffer_written(&bh))
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000856 error = copy_user_dax(bh.b_bdev, to_sector(&bh, inode),
857 bh.b_size, new_page, vaddr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800858 else
859 clear_user_highpage(new_page, vaddr);
860 if (error)
Jan Karaac401cc2016-05-12 18:29:18 +0200861 goto unlock_entry;
862 if (!radix_tree_exceptional_entry(entry)) {
863 vmf->page = entry;
Jan Karabc2466e2016-05-12 18:29:19 +0200864 return VM_FAULT_LOCKED;
Jan Karaac401cc2016-05-12 18:29:18 +0200865 }
Jan Karabc2466e2016-05-12 18:29:19 +0200866 vmf->entry = entry;
867 return VM_FAULT_DAX_LOCKED;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800868 }
869
Jan Karaac401cc2016-05-12 18:29:18 +0200870 if (!buffer_mapped(&bh)) {
871 if (vmf->flags & FAULT_FLAG_WRITE) {
872 error = get_block(inode, block, &bh, 1);
873 count_vm_event(PGMAJFAULT);
874 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
875 major = VM_FAULT_MAJOR;
876 if (!error && (bh.b_size < PAGE_SIZE))
877 error = -EIO;
878 if (error)
879 goto unlock_entry;
880 } else {
881 return dax_load_hole(mapping, entry, vmf);
882 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800883 }
884
Jan Kara02fbd132016-05-11 11:58:48 +0200885 /* Filesystem should not return unwritten buffers to us! */
Jan Kara2b109452016-05-11 11:58:50 +0200886 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000887 error = dax_insert_mapping(mapping, bh.b_bdev, to_sector(&bh, inode),
888 bh.b_size, &entry, vma, vmf);
Jan Karaac401cc2016-05-12 18:29:18 +0200889 unlock_entry:
890 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800891 out:
892 if (error == -ENOMEM)
893 return VM_FAULT_OOM | major;
894 /* -EBUSY is fine, somebody else faulted on the same PTE */
895 if ((error < 0) && (error != -EBUSY))
896 return VM_FAULT_SIGBUS | major;
897 return VM_FAULT_NOPAGE | major;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800898}
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800899EXPORT_SYMBOL_GPL(dax_fault);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800900
Jan Kara348e9672016-05-12 18:29:15 +0200901#if defined(CONFIG_TRANSPARENT_HUGEPAGE)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700902/*
903 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
904 * more often than one might expect in the below function.
905 */
906#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
907
Dan Williamscbb38e42016-01-15 16:56:58 -0800908static void __dax_dbg(struct buffer_head *bh, unsigned long address,
909 const char *reason, const char *fn)
910{
911 if (bh) {
912 char bname[BDEVNAME_SIZE];
913 bdevname(bh->b_bdev, bname);
914 pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
915 "length %zd fallback: %s\n", fn, current->comm,
916 address, bname, bh->b_state, (u64)bh->b_blocknr,
917 bh->b_size, reason);
918 } else {
919 pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
920 current->comm, address, reason);
921 }
922}
923
924#define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
925
Ross Zwisler6b524992016-07-26 15:21:05 -0700926/**
927 * dax_pmd_fault - handle a PMD fault on a DAX file
928 * @vma: The virtual memory area where the fault occurred
929 * @vmf: The description of the fault
930 * @get_block: The filesystem method used to translate file offsets to blocks
931 *
932 * When a page fault occurs, filesystems may call this helper in their
933 * pmd_fault handler for DAX files.
934 */
935int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
Jan Kara02fbd132016-05-11 11:58:48 +0200936 pmd_t *pmd, unsigned int flags, get_block_t get_block)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700937{
938 struct file *file = vma->vm_file;
939 struct address_space *mapping = file->f_mapping;
940 struct inode *inode = mapping->host;
941 struct buffer_head bh;
942 unsigned blkbits = inode->i_blkbits;
943 unsigned long pmd_addr = address & PMD_MASK;
944 bool write = flags & FAULT_FLAG_WRITE;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800945 struct block_device *bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700946 pgoff_t size, pgoff;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800947 sector_t block;
Jan Karaac401cc2016-05-12 18:29:18 +0200948 int result = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800949 bool alloc = false;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700950
Dan Williamsc046c322016-01-15 16:57:01 -0800951 /* dax pmd mappings require pfn_t_devmap() */
Dan Williamsee82c9e2015-11-15 16:06:32 -0800952 if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
953 return VM_FAULT_FALLBACK;
954
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700955 /* Fall back to PTEs if we're going to COW */
Toshi Kani59bf4fb2016-01-15 16:56:05 -0800956 if (write && !(vma->vm_flags & VM_SHARED)) {
957 split_huge_pmd(vma, pmd, address);
Dan Williamscbb38e42016-01-15 16:56:58 -0800958 dax_pmd_dbg(NULL, address, "cow write");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700959 return VM_FAULT_FALLBACK;
Toshi Kani59bf4fb2016-01-15 16:56:05 -0800960 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700961 /* If the PMD would extend outside the VMA */
Dan Williamscbb38e42016-01-15 16:56:58 -0800962 if (pmd_addr < vma->vm_start) {
963 dax_pmd_dbg(NULL, address, "vma start unaligned");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700964 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800965 }
966 if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
967 dax_pmd_dbg(NULL, address, "vma end unaligned");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700968 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800969 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700970
Matthew Wilcox3fdd1b472015-09-08 14:59:39 -0700971 pgoff = linear_page_index(vma, pmd_addr);
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700972 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
973 if (pgoff >= size)
974 return VM_FAULT_SIGBUS;
975 /* If the PMD would cover blocks out of the file */
Dan Williamscbb38e42016-01-15 16:56:58 -0800976 if ((pgoff | PG_PMD_COLOUR) >= size) {
977 dax_pmd_dbg(NULL, address,
978 "offset + huge page size > file size");
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700979 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -0800980 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700981
982 memset(&bh, 0, sizeof(bh));
Ross Zwislerd4bbe702016-01-22 15:10:31 -0800983 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700984 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
985
986 bh.b_size = PMD_SIZE;
Ross Zwisler9973c982016-01-22 15:10:47 -0800987
988 if (get_block(inode, block, &bh, 0) != 0)
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700989 return VM_FAULT_SIGBUS;
Ross Zwisler9973c982016-01-22 15:10:47 -0800990
991 if (!buffer_mapped(&bh) && write) {
992 if (get_block(inode, block, &bh, 1) != 0)
993 return VM_FAULT_SIGBUS;
994 alloc = true;
Jan Kara2b109452016-05-11 11:58:50 +0200995 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
Ross Zwisler9973c982016-01-22 15:10:47 -0800996 }
997
Dan Williamsb2e0d162016-01-15 16:55:59 -0800998 bdev = bh.b_bdev;
Matthew Wilcox844f35d2015-09-08 14:58:57 -0700999
Ross Zwislerfa0d3fc2016-11-08 11:31:14 +11001000 if (bh.b_size < PMD_SIZE) {
Dan Williamscbb38e42016-01-15 16:56:58 -08001001 dax_pmd_dbg(&bh, address, "allocated block too small");
Ross Zwisler9973c982016-01-22 15:10:47 -08001002 return VM_FAULT_FALLBACK;
Dan Williamscbb38e42016-01-15 16:56:58 -08001003 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001004
Ross Zwisler9973c982016-01-22 15:10:47 -08001005 /*
1006 * If we allocated new storage, make sure no process has any
1007 * zero pages covering this hole
1008 */
1009 if (alloc) {
1010 loff_t lstart = pgoff << PAGE_SHIFT;
1011 loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
1012
1013 truncate_pagecache_range(inode, lstart, lend);
1014 }
1015
Jan Karab9953532016-05-12 18:29:14 +02001016 if (!write && !buffer_mapped(&bh)) {
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001017 spinlock_t *ptl;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -07001018 pmd_t entry;
Aaron Lu6fcb52a2016-10-07 17:00:08 -07001019 struct page *zero_page = mm_get_huge_zero_page(vma->vm_mm);
Kirill A. Shutemovd295e342015-09-08 14:59:34 -07001020
Dan Williamscbb38e42016-01-15 16:56:58 -08001021 if (unlikely(!zero_page)) {
1022 dax_pmd_dbg(&bh, address, "no zero page");
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001023 goto fallback;
Dan Williamscbb38e42016-01-15 16:56:58 -08001024 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001025
Kirill A. Shutemovd295e342015-09-08 14:59:34 -07001026 ptl = pmd_lock(vma->vm_mm, pmd);
1027 if (!pmd_none(*pmd)) {
1028 spin_unlock(ptl);
Dan Williamscbb38e42016-01-15 16:56:58 -08001029 dax_pmd_dbg(&bh, address, "pmd already present");
Kirill A. Shutemovd295e342015-09-08 14:59:34 -07001030 goto fallback;
1031 }
1032
Dan Williamscbb38e42016-01-15 16:56:58 -08001033 dev_dbg(part_to_dev(bdev->bd_part),
1034 "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
1035 __func__, current->comm, address,
1036 (unsigned long long) to_sector(&bh, inode));
1037
Kirill A. Shutemovd295e342015-09-08 14:59:34 -07001038 entry = mk_pmd(zero_page, vma->vm_page_prot);
1039 entry = pmd_mkhuge(entry);
1040 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001041 result = VM_FAULT_NOPAGE;
Kirill A. Shutemovd295e342015-09-08 14:59:34 -07001042 spin_unlock(ptl);
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001043 } else {
Dan Williamsb2e0d162016-01-15 16:55:59 -08001044 struct blk_dax_ctl dax = {
1045 .sector = to_sector(&bh, inode),
1046 .size = PMD_SIZE,
1047 };
1048 long length = dax_map_atomic(bdev, &dax);
1049
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001050 if (length < 0) {
Dan Williams8b3db9792016-02-24 14:02:06 -08001051 dax_pmd_dbg(&bh, address, "dax-error fallback");
1052 goto fallback;
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001053 }
Dan Williamscbb38e42016-01-15 16:56:58 -08001054 if (length < PMD_SIZE) {
1055 dax_pmd_dbg(&bh, address, "dax-length too small");
1056 dax_unmap_atomic(bdev, &dax);
1057 goto fallback;
1058 }
1059 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
1060 dax_pmd_dbg(&bh, address, "pfn unaligned");
Dan Williamsb2e0d162016-01-15 16:55:59 -08001061 dax_unmap_atomic(bdev, &dax);
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001062 goto fallback;
Dan Williamsb2e0d162016-01-15 16:55:59 -08001063 }
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001064
Dan Williamsc046c322016-01-15 16:57:01 -08001065 if (!pfn_t_devmap(dax.pfn)) {
Dan Williamsb2e0d162016-01-15 16:55:59 -08001066 dax_unmap_atomic(bdev, &dax);
Dan Williamscbb38e42016-01-15 16:56:58 -08001067 dax_pmd_dbg(&bh, address, "pfn not in memmap");
Dan Williams152d7bd2015-11-12 18:33:54 -08001068 goto fallback;
Dan Williamsb2e0d162016-01-15 16:55:59 -08001069 }
Dan Williamsb2e0d162016-01-15 16:55:59 -08001070 dax_unmap_atomic(bdev, &dax);
Ross Zwisler0f90cc62015-10-15 15:28:32 -07001071
Ross Zwisler9973c982016-01-22 15:10:47 -08001072 /*
1073 * For PTE faults we insert a radix tree entry for reads, and
1074 * leave it clean. Then on the first write we dirty the radix
1075 * tree entry via the dax_pfn_mkwrite() path. This sequence
1076 * allows the dax_pfn_mkwrite() call to be simpler and avoid a
1077 * call into get_block() to translate the pgoff to a sector in
1078 * order to be able to create a new radix tree entry.
1079 *
1080 * The PMD path doesn't have an equivalent to
1081 * dax_pfn_mkwrite(), though, so for a read followed by a
Ross Zwisler6b524992016-07-26 15:21:05 -07001082 * write we traverse all the way through dax_pmd_fault()
Ross Zwisler9973c982016-01-22 15:10:47 -08001083 * twice. This means we can just skip inserting a radix tree
1084 * entry completely on the initial read and just wait until
1085 * the write to insert a dirty entry.
1086 */
1087 if (write) {
Jan Karaac401cc2016-05-12 18:29:18 +02001088 /*
1089 * We should insert radix-tree entry and dirty it here.
1090 * For now this is broken...
1091 */
Ross Zwisler9973c982016-01-22 15:10:47 -08001092 }
1093
Dan Williamscbb38e42016-01-15 16:56:58 -08001094 dev_dbg(part_to_dev(bdev->bd_part),
1095 "%s: %s addr: %lx pfn: %lx sect: %llx\n",
1096 __func__, current->comm, address,
1097 pfn_t_to_pfn(dax.pfn),
1098 (unsigned long long) dax.sector);
Dan Williams34c0fd52016-01-15 16:56:14 -08001099 result |= vmf_insert_pfn_pmd(vma, address, pmd,
Dan Williamsf25748e32016-01-15 16:56:43 -08001100 dax.pfn, write);
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001101 }
1102
1103 out:
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001104 return result;
1105
1106 fallback:
1107 count_vm_event(THP_FAULT_FALLBACK);
1108 result = VM_FAULT_FALLBACK;
1109 goto out;
1110}
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001111EXPORT_SYMBOL_GPL(dax_pmd_fault);
Valentin Rothbergdd8a2b62015-09-08 14:59:09 -07001112#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Matthew Wilcox844f35d2015-09-08 14:58:57 -07001113
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001114/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001115 * dax_pfn_mkwrite - handle first write to DAX page
1116 * @vma: The virtual memory area where the fault occurred
1117 * @vmf: The description of the fault
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001118 */
1119int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1120{
Ross Zwisler9973c982016-01-22 15:10:47 -08001121 struct file *file = vma->vm_file;
Jan Karaac401cc2016-05-12 18:29:18 +02001122 struct address_space *mapping = file->f_mapping;
1123 void *entry;
1124 pgoff_t index = vmf->pgoff;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001125
Jan Karaac401cc2016-05-12 18:29:18 +02001126 spin_lock_irq(&mapping->tree_lock);
1127 entry = get_unlocked_mapping_entry(mapping, index, NULL);
1128 if (!entry || !radix_tree_exceptional_entry(entry))
1129 goto out;
1130 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
1131 put_unlocked_mapping_entry(mapping, index, entry);
1132out:
1133 spin_unlock_irq(&mapping->tree_lock);
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001134 return VM_FAULT_NOPAGE;
1135}
1136EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
1137
Vishal Verma4b0228f2016-04-21 15:13:46 -04001138static bool dax_range_is_aligned(struct block_device *bdev,
1139 unsigned int offset, unsigned int length)
1140{
1141 unsigned short sector_size = bdev_logical_block_size(bdev);
1142
1143 if (!IS_ALIGNED(offset, sector_size))
1144 return false;
1145 if (!IS_ALIGNED(length, sector_size))
1146 return false;
1147
1148 return true;
1149}
1150
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001151int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
1152 unsigned int offset, unsigned int length)
1153{
1154 struct blk_dax_ctl dax = {
1155 .sector = sector,
1156 .size = PAGE_SIZE,
1157 };
1158
Vishal Verma4b0228f2016-04-21 15:13:46 -04001159 if (dax_range_is_aligned(bdev, offset, length)) {
1160 sector_t start_sector = dax.sector + (offset >> 9);
1161
1162 return blkdev_issue_zeroout(bdev, start_sector,
1163 length >> 9, GFP_NOFS, true);
1164 } else {
1165 if (dax_map_atomic(bdev, &dax) < 0)
1166 return PTR_ERR(dax.addr);
1167 clear_pmem(dax.addr + offset, length);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001168 dax_unmap_atomic(bdev, &dax);
1169 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001170 return 0;
1171}
1172EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1173
Boaz Harrosh0e3b2102015-04-15 16:15:14 -07001174/**
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001175 * dax_zero_page_range - zero a range within a page of a DAX file
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001176 * @inode: The file being truncated
1177 * @from: The file offset that is being truncated to
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001178 * @length: The number of bytes to zero
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001179 * @get_block: The filesystem method used to translate file offsets to blocks
1180 *
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001181 * This function can be called by a filesystem when it is zeroing part of a
1182 * page in a DAX file. This is intended for hole-punch operations. If
1183 * you are truncating a file, the helper function dax_truncate_page() may be
1184 * more convenient.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001185 */
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001186int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
1187 get_block_t get_block)
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001188{
1189 struct buffer_head bh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001190 pgoff_t index = from >> PAGE_SHIFT;
1191 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001192 int err;
1193
1194 /* Block boundary? Nothing to do */
1195 if (!length)
1196 return 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001197 BUG_ON((offset + length) > PAGE_SIZE);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001198
1199 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -08001200 bh.b_bdev = inode->i_sb->s_bdev;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001201 bh.b_size = PAGE_SIZE;
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001202 err = get_block(inode, index, &bh, 0);
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001203 if (err < 0 || !buffer_written(&bh))
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001204 return err;
Dan Williamsb2e0d162016-01-15 16:55:59 -08001205
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001206 return __dax_zero_page_range(bh.b_bdev, to_sector(&bh, inode),
1207 offset, length);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001208}
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001209EXPORT_SYMBOL_GPL(dax_zero_page_range);
1210
1211/**
1212 * dax_truncate_page - handle a partial page being truncated in a DAX file
1213 * @inode: The file being truncated
1214 * @from: The file offset that is being truncated to
1215 * @get_block: The filesystem method used to translate file offsets to blocks
1216 *
1217 * Similar to block_truncate_page(), this function can be called by a
1218 * filesystem when it is truncating a DAX file to handle the partial page.
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001219 */
1220int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1221{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001222 unsigned length = PAGE_ALIGN(from) - from;
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001223 return dax_zero_page_range(inode, from, length, get_block);
1224}
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001225EXPORT_SYMBOL_GPL(dax_truncate_page);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001226
1227#ifdef CONFIG_FS_IOMAP
1228static loff_t
1229iomap_dax_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1230 struct iomap *iomap)
1231{
1232 struct iov_iter *iter = data;
1233 loff_t end = pos + length, done = 0;
1234 ssize_t ret = 0;
1235
1236 if (iov_iter_rw(iter) == READ) {
1237 end = min(end, i_size_read(inode));
1238 if (pos >= end)
1239 return 0;
1240
1241 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1242 return iov_iter_zero(min(length, end - pos), iter);
1243 }
1244
1245 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1246 return -EIO;
1247
1248 while (pos < end) {
1249 unsigned offset = pos & (PAGE_SIZE - 1);
1250 struct blk_dax_ctl dax = { 0 };
1251 ssize_t map_len;
1252
1253 dax.sector = iomap->blkno +
1254 (((pos & PAGE_MASK) - iomap->offset) >> 9);
1255 dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
1256 map_len = dax_map_atomic(iomap->bdev, &dax);
1257 if (map_len < 0) {
1258 ret = map_len;
1259 break;
1260 }
1261
1262 dax.addr += offset;
1263 map_len -= offset;
1264 if (map_len > end - pos)
1265 map_len = end - pos;
1266
1267 if (iov_iter_rw(iter) == WRITE)
1268 map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
1269 else
1270 map_len = copy_to_iter(dax.addr, map_len, iter);
1271 dax_unmap_atomic(iomap->bdev, &dax);
1272 if (map_len <= 0) {
1273 ret = map_len ? map_len : -EFAULT;
1274 break;
1275 }
1276
1277 pos += map_len;
1278 length -= map_len;
1279 done += map_len;
1280 }
1281
1282 return done ? done : ret;
1283}
1284
1285/**
1286 * iomap_dax_rw - Perform I/O to a DAX file
1287 * @iocb: The control block for this I/O
1288 * @iter: The addresses to do I/O from or to
1289 * @ops: iomap ops passed from the file system
1290 *
1291 * This function performs read and write operations to directly mapped
1292 * persistent memory. The callers needs to take care of read/write exclusion
1293 * and evicting any page cache pages in the region under I/O.
1294 */
1295ssize_t
1296iomap_dax_rw(struct kiocb *iocb, struct iov_iter *iter,
1297 struct iomap_ops *ops)
1298{
1299 struct address_space *mapping = iocb->ki_filp->f_mapping;
1300 struct inode *inode = mapping->host;
1301 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1302 unsigned flags = 0;
1303
1304 if (iov_iter_rw(iter) == WRITE)
1305 flags |= IOMAP_WRITE;
1306
1307 /*
1308 * Yes, even DAX files can have page cache attached to them: A zeroed
1309 * page is inserted into the pagecache when we have to serve a write
1310 * fault on a hole. It should never be dirtied and can simply be
1311 * dropped from the pagecache once we get real data for the page.
1312 *
1313 * XXX: This is racy against mmap, and there's nothing we can do about
1314 * it. We'll eventually need to shift this down even further so that
1315 * we can check if we allocated blocks over a hole first.
1316 */
1317 if (mapping->nrpages) {
1318 ret = invalidate_inode_pages2_range(mapping,
1319 pos >> PAGE_SHIFT,
1320 (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
1321 WARN_ON_ONCE(ret);
1322 }
1323
1324 while (iov_iter_count(iter)) {
1325 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
1326 iter, iomap_dax_actor);
1327 if (ret <= 0)
1328 break;
1329 pos += ret;
1330 done += ret;
1331 }
1332
1333 iocb->ki_pos += done;
1334 return done ? done : ret;
1335}
1336EXPORT_SYMBOL_GPL(iomap_dax_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001337
1338/**
1339 * iomap_dax_fault - handle a page fault on a DAX file
1340 * @vma: The virtual memory area where the fault occurred
1341 * @vmf: The description of the fault
1342 * @ops: iomap ops passed from the file system
1343 *
1344 * When a page fault occurs, filesystems may call this helper in their fault
1345 * or mkwrite handler for DAX files. Assumes the caller has done all the
1346 * necessary locking for the page fault to proceed successfully.
1347 */
1348int iomap_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
1349 struct iomap_ops *ops)
1350{
1351 struct address_space *mapping = vma->vm_file->f_mapping;
1352 struct inode *inode = mapping->host;
1353 unsigned long vaddr = (unsigned long)vmf->virtual_address;
1354 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1355 sector_t sector;
1356 struct iomap iomap = { 0 };
1357 unsigned flags = 0;
1358 int error, major = 0;
1359 void *entry;
1360
1361 /*
1362 * Check whether offset isn't beyond end of file now. Caller is supposed
1363 * to hold locks serializing us with truncate / punch hole so this is
1364 * a reliable test.
1365 */
1366 if (pos >= i_size_read(inode))
1367 return VM_FAULT_SIGBUS;
1368
1369 entry = grab_mapping_entry(mapping, vmf->pgoff);
1370 if (IS_ERR(entry)) {
1371 error = PTR_ERR(entry);
1372 goto out;
1373 }
1374
1375 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1376 flags |= IOMAP_WRITE;
1377
1378 /*
1379 * Note that we don't bother to use iomap_apply here: DAX required
1380 * the file system block size to be equal the page size, which means
1381 * that we never have to deal with more than a single extent here.
1382 */
1383 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1384 if (error)
1385 goto unlock_entry;
1386 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
1387 error = -EIO; /* fs corruption? */
1388 goto unlock_entry;
1389 }
1390
1391 sector = iomap.blkno + (((pos & PAGE_MASK) - iomap.offset) >> 9);
1392
1393 if (vmf->cow_page) {
1394 switch (iomap.type) {
1395 case IOMAP_HOLE:
1396 case IOMAP_UNWRITTEN:
1397 clear_user_highpage(vmf->cow_page, vaddr);
1398 break;
1399 case IOMAP_MAPPED:
1400 error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
1401 vmf->cow_page, vaddr);
1402 break;
1403 default:
1404 WARN_ON_ONCE(1);
1405 error = -EIO;
1406 break;
1407 }
1408
1409 if (error)
1410 goto unlock_entry;
1411 if (!radix_tree_exceptional_entry(entry)) {
1412 vmf->page = entry;
1413 return VM_FAULT_LOCKED;
1414 }
1415 vmf->entry = entry;
1416 return VM_FAULT_DAX_LOCKED;
1417 }
1418
1419 switch (iomap.type) {
1420 case IOMAP_MAPPED:
1421 if (iomap.flags & IOMAP_F_NEW) {
1422 count_vm_event(PGMAJFAULT);
1423 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1424 major = VM_FAULT_MAJOR;
1425 }
1426 error = dax_insert_mapping(mapping, iomap.bdev, sector,
1427 PAGE_SIZE, &entry, vma, vmf);
1428 break;
1429 case IOMAP_UNWRITTEN:
1430 case IOMAP_HOLE:
1431 if (!(vmf->flags & FAULT_FLAG_WRITE))
1432 return dax_load_hole(mapping, entry, vmf);
1433 /*FALLTHRU*/
1434 default:
1435 WARN_ON_ONCE(1);
1436 error = -EIO;
1437 break;
1438 }
1439
1440 unlock_entry:
1441 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
1442 out:
1443 if (error == -ENOMEM)
1444 return VM_FAULT_OOM | major;
1445 /* -EBUSY is fine, somebody else faulted on the same PTE */
1446 if (error < 0 && error != -EBUSY)
1447 return VM_FAULT_SIGBUS | major;
1448 return VM_FAULT_NOPAGE | major;
1449}
1450EXPORT_SYMBOL_GPL(iomap_dax_fault);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001451#endif /* CONFIG_FS_IOMAP */