blob: 6edd89b3b69cfbb7904740f24beb659d283c1508 [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
Dan Williamsb2e0d162016-01-15 16:55:59 -080067static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
68{
69 struct request_queue *q = bdev->bd_queue;
70 long rc = -EIO;
71
Dan Williams7a9eb202016-06-03 18:06:47 -070072 dax->addr = ERR_PTR(-EIO);
Dan Williamsb2e0d162016-01-15 16:55:59 -080073 if (blk_queue_enter(q, true) != 0)
74 return rc;
75
76 rc = bdev_direct_access(bdev, dax);
77 if (rc < 0) {
Dan Williams7a9eb202016-06-03 18:06:47 -070078 dax->addr = ERR_PTR(rc);
Dan Williamsb2e0d162016-01-15 16:55:59 -080079 blk_queue_exit(q);
80 return rc;
81 }
82 return rc;
83}
84
85static void dax_unmap_atomic(struct block_device *bdev,
86 const struct blk_dax_ctl *dax)
87{
88 if (IS_ERR(dax->addr))
89 return;
90 blk_queue_exit(bdev->bd_queue);
91}
92
Dan Williamsd1a5f2b42016-01-28 20:25:31 -080093struct page *read_dax_sector(struct block_device *bdev, sector_t n)
94{
95 struct page *page = alloc_pages(GFP_KERNEL, 0);
96 struct blk_dax_ctl dax = {
97 .size = PAGE_SIZE,
98 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
99 };
100 long rc;
101
102 if (!page)
103 return ERR_PTR(-ENOMEM);
104
105 rc = dax_map_atomic(bdev, &dax);
106 if (rc < 0)
107 return ERR_PTR(rc);
108 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
109 dax_unmap_atomic(bdev, &dax);
110 return page;
111}
112
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800113static bool buffer_written(struct buffer_head *bh)
114{
115 return buffer_mapped(bh) && !buffer_unwritten(bh);
116}
117
Dan Williamsb2e0d162016-01-15 16:55:59 -0800118static sector_t to_sector(const struct buffer_head *bh,
119 const struct inode *inode)
120{
121 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
122
123 return sector;
124}
125
Omar Sandovala95cd632015-03-16 04:33:51 -0700126static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
127 loff_t start, loff_t end, get_block_t get_block,
128 struct buffer_head *bh)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800129{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800130 loff_t pos = start, max = start, bh_max = start;
Dan Williams14df6a42016-06-01 21:03:32 -0700131 bool hole = false;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800132 struct block_device *bdev = NULL;
133 int rw = iov_iter_rw(iter), rc;
134 long map_len = 0;
135 struct blk_dax_ctl dax = {
Dan Williams7a9eb202016-06-03 18:06:47 -0700136 .addr = ERR_PTR(-EIO),
Dan Williamsb2e0d162016-01-15 16:55:59 -0800137 };
Jan Kara069c77b2016-05-11 11:58:51 +0200138 unsigned blkbits = inode->i_blkbits;
139 sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1)
140 >> blkbits;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800141
Dan Williamsb2e0d162016-01-15 16:55:59 -0800142 if (rw == READ)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800143 end = min(end, i_size_read(inode));
144
145 while (pos < end) {
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600146 size_t len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800147 if (pos == max) {
Jeff Moyere94f5a22015-08-14 16:15:31 -0400148 long page = pos >> PAGE_SHIFT;
149 sector_t block = page << (PAGE_SHIFT - blkbits);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800150 unsigned first = pos - (block << blkbits);
151 long size;
152
153 if (pos == bh_max) {
154 bh->b_size = PAGE_ALIGN(end - pos);
155 bh->b_state = 0;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800156 rc = get_block(inode, block, bh, rw == WRITE);
157 if (rc)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800158 break;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800159 bh_max = pos - first + bh->b_size;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800160 bdev = bh->b_bdev;
Jan Kara069c77b2016-05-11 11:58:51 +0200161 /*
162 * We allow uninitialized buffers for writes
163 * beyond EOF as those cannot race with faults
164 */
165 WARN_ON_ONCE(
166 (buffer_new(bh) && block < file_blks) ||
167 (rw == WRITE && buffer_unwritten(bh)));
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800168 } else {
169 unsigned done = bh->b_size -
170 (bh_max - (pos - first));
171 bh->b_blocknr += done >> blkbits;
172 bh->b_size -= done;
173 }
174
Dan Williamsb2e0d162016-01-15 16:55:59 -0800175 hole = rw == READ && !buffer_written(bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800176 if (hole) {
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800177 size = bh->b_size - first;
178 } else {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800179 dax_unmap_atomic(bdev, &dax);
180 dax.sector = to_sector(bh, inode);
181 dax.size = bh->b_size;
182 map_len = dax_map_atomic(bdev, &dax);
183 if (map_len < 0) {
184 rc = map_len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800185 break;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800186 }
Dan Williamsb2e0d162016-01-15 16:55:59 -0800187 dax.addr += first;
188 size = map_len - first;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800189 }
Eric Sandeen02395432016-06-23 16:54:46 -0500190 /*
191 * pos + size is one past the last offset for IO,
192 * so pos + size can overflow loff_t at extreme offsets.
193 * Cast to u64 to catch this and get the true minimum.
194 */
195 max = min_t(u64, pos + size, end);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800196 }
197
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600198 if (iov_iter_rw(iter) == WRITE) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800199 len = copy_from_iter_pmem(dax.addr, max - pos, iter);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600200 } else if (!hole)
Dan Williamsb2e0d162016-01-15 16:55:59 -0800201 len = copy_to_iter((void __force *) dax.addr, max - pos,
Ross Zwislere2e05392015-08-18 13:55:41 -0600202 iter);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800203 else
204 len = iov_iter_zero(max - pos, iter);
205
Al Virocadfbb62015-11-10 19:42:49 -0700206 if (!len) {
Dan Williamsb2e0d162016-01-15 16:55:59 -0800207 rc = -EFAULT;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800208 break;
Al Virocadfbb62015-11-10 19:42:49 -0700209 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800210
211 pos += len;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800212 if (!IS_ERR(dax.addr))
213 dax.addr += len;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800214 }
215
Dan Williamsb2e0d162016-01-15 16:55:59 -0800216 dax_unmap_atomic(bdev, &dax);
Ross Zwisler2765cfb2015-08-18 13:55:40 -0600217
Dan Williamsb2e0d162016-01-15 16:55:59 -0800218 return (pos == start) ? rc : pos - start;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800219}
220
221/**
222 * dax_do_io - Perform I/O to a DAX file
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800223 * @iocb: The control block for this I/O
224 * @inode: The file which the I/O is directed at
225 * @iter: The addresses to do I/O from or to
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800226 * @get_block: The filesystem method used to translate file offsets to blocks
227 * @end_io: A filesystem callback for I/O completion
228 * @flags: See below
229 *
230 * This function uses the same locking scheme as do_blockdev_direct_IO:
231 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
232 * caller for writes. For reads, we take and release the i_mutex ourselves.
233 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
234 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
235 * is in progress.
236 */
Omar Sandovala95cd632015-03-16 04:33:51 -0700237ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700238 struct iov_iter *iter, get_block_t get_block,
Omar Sandovala95cd632015-03-16 04:33:51 -0700239 dio_iodone_t end_io, int flags)
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800240{
241 struct buffer_head bh;
242 ssize_t retval = -EINVAL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700243 loff_t pos = iocb->ki_pos;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800244 loff_t end = pos + iov_iter_count(iter);
245
246 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -0800247 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800248
Jan Karac3d98e32016-05-11 11:58:52 +0200249 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Al Viro59551022016-01-22 15:40:57 -0500250 inode_lock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800251
252 /* Protects against truncate */
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400253 if (!(flags & DIO_SKIP_DIO_COUNT))
254 inode_dio_begin(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800255
Omar Sandovala95cd632015-03-16 04:33:51 -0700256 retval = dax_io(inode, iter, pos, end, get_block, &bh);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800257
Omar Sandovala95cd632015-03-16 04:33:51 -0700258 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
Al Viro59551022016-01-22 15:40:57 -0500259 inode_unlock(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800260
Christoph Hellwig187372a2016-02-08 14:40:51 +1100261 if (end_io) {
262 int err;
263
264 err = end_io(iocb, pos, retval, bh.b_private);
265 if (err)
266 retval = err;
267 }
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800268
Matthew Wilcoxbbab37d2015-07-03 10:40:42 -0400269 if (!(flags & DIO_SKIP_DIO_COUNT))
270 inode_dio_end(inode);
Matthew Wilcoxd475c632015-02-16 15:58:56 -0800271 return retval;
272}
273EXPORT_SYMBOL_GPL(dax_do_io);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800274
275/*
Jan Karaac401cc2016-05-12 18:29:18 +0200276 * DAX radix tree locking
277 */
278struct exceptional_entry_key {
279 struct address_space *mapping;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100280 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200281};
282
283struct wait_exceptional_entry_queue {
284 wait_queue_t wait;
285 struct exceptional_entry_key key;
286};
287
Ross Zwisler63e95b52016-11-08 11:32:20 +1100288static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
289 pgoff_t index, void *entry, struct exceptional_entry_key *key)
290{
291 unsigned long hash;
292
293 /*
294 * If 'entry' is a PMD, align the 'index' that we use for the wait
295 * queue to the start of that PMD. This ensures that all offsets in
296 * the range covered by the PMD map to the same bit lock.
297 */
298 if (RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
299 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
300
301 key->mapping = mapping;
302 key->entry_start = index;
303
304 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
305 return wait_table + hash;
306}
307
Jan Karaac401cc2016-05-12 18:29:18 +0200308static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
309 int sync, void *keyp)
310{
311 struct exceptional_entry_key *key = keyp;
312 struct wait_exceptional_entry_queue *ewait =
313 container_of(wait, struct wait_exceptional_entry_queue, wait);
314
315 if (key->mapping != ewait->key.mapping ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100316 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200317 return 0;
318 return autoremove_wake_function(wait, mode, sync, NULL);
319}
320
321/*
322 * Check whether the given slot is locked. The function must be called with
323 * mapping->tree_lock held
324 */
325static inline int slot_locked(struct address_space *mapping, void **slot)
326{
327 unsigned long entry = (unsigned long)
328 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
329 return entry & RADIX_DAX_ENTRY_LOCK;
330}
331
332/*
333 * Mark the given slot is locked. The function must be called with
334 * mapping->tree_lock held
335 */
336static inline void *lock_slot(struct address_space *mapping, void **slot)
337{
338 unsigned long entry = (unsigned long)
339 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
340
341 entry |= RADIX_DAX_ENTRY_LOCK;
342 radix_tree_replace_slot(slot, (void *)entry);
343 return (void *)entry;
344}
345
346/*
347 * Mark the given slot is unlocked. The function must be called with
348 * mapping->tree_lock held
349 */
350static inline void *unlock_slot(struct address_space *mapping, void **slot)
351{
352 unsigned long entry = (unsigned long)
353 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
354
355 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
356 radix_tree_replace_slot(slot, (void *)entry);
357 return (void *)entry;
358}
359
360/*
361 * Lookup entry in radix tree, wait for it to become unlocked if it is
362 * exceptional entry and return it. The caller must call
363 * put_unlocked_mapping_entry() when he decided not to lock the entry or
364 * put_locked_mapping_entry() when he locked the entry and now wants to
365 * unlock it.
366 *
367 * The function must be called with mapping->tree_lock held.
368 */
369static void *get_unlocked_mapping_entry(struct address_space *mapping,
370 pgoff_t index, void ***slotp)
371{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100372 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200373 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100374 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200375
376 init_wait(&ewait.wait);
377 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200378
379 for (;;) {
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100380 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200381 &slot);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100382 if (!entry || !radix_tree_exceptional_entry(entry) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200383 !slot_locked(mapping, slot)) {
384 if (slotp)
385 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100386 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200387 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100388
389 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200390 prepare_to_wait_exclusive(wq, &ewait.wait,
391 TASK_UNINTERRUPTIBLE);
392 spin_unlock_irq(&mapping->tree_lock);
393 schedule();
394 finish_wait(wq, &ewait.wait);
395 spin_lock_irq(&mapping->tree_lock);
396 }
397}
398
399/*
400 * Find radix tree entry at given index. If it points to a page, return with
401 * the page locked. If it points to the exceptional entry, return with the
402 * radix tree entry locked. If the radix tree doesn't contain given index,
403 * create empty exceptional entry for the index and return with it locked.
404 *
405 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
406 * persistent memory the benefit is doubtful. We can add that later if we can
407 * show it helps.
408 */
409static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index)
410{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100411 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200412
413restart:
414 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100415 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200416 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100417 if (!entry) {
Jan Karaac401cc2016-05-12 18:29:18 +0200418 int err;
419
420 spin_unlock_irq(&mapping->tree_lock);
421 err = radix_tree_preload(
422 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
423 if (err)
424 return ERR_PTR(err);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100425 entry = (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY |
Jan Karaac401cc2016-05-12 18:29:18 +0200426 RADIX_DAX_ENTRY_LOCK);
427 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100428 err = radix_tree_insert(&mapping->page_tree, index, entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200429 radix_tree_preload_end();
430 if (err) {
431 spin_unlock_irq(&mapping->tree_lock);
432 /* Someone already created the entry? */
433 if (err == -EEXIST)
434 goto restart;
435 return ERR_PTR(err);
436 }
437 /* Good, we have inserted empty locked entry into the tree. */
438 mapping->nrexceptional++;
439 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100440 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200441 }
442 /* Normal page in radix tree? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100443 if (!radix_tree_exceptional_entry(entry)) {
444 struct page *page = entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200445
446 get_page(page);
447 spin_unlock_irq(&mapping->tree_lock);
448 lock_page(page);
449 /* Page got truncated? Retry... */
450 if (unlikely(page->mapping != mapping)) {
451 unlock_page(page);
452 put_page(page);
453 goto restart;
454 }
455 return page;
456 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100457 entry = lock_slot(mapping, slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200458 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100459 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200460}
461
Ross Zwisler63e95b52016-11-08 11:32:20 +1100462/*
463 * We do not necessarily hold the mapping->tree_lock when we call this
464 * function so it is possible that 'entry' is no longer a valid item in the
465 * radix tree. This is okay, though, because all we really need to do is to
466 * find the correct waitqueue where tasks might be sleeping waiting for that
467 * old 'entry' and wake them.
468 */
Jan Karaac401cc2016-05-12 18:29:18 +0200469void dax_wake_mapping_entry_waiter(struct address_space *mapping,
Ross Zwisler63e95b52016-11-08 11:32:20 +1100470 pgoff_t index, void *entry, bool wake_all)
Jan Karaac401cc2016-05-12 18:29:18 +0200471{
Ross Zwisler63e95b52016-11-08 11:32:20 +1100472 struct exceptional_entry_key key;
473 wait_queue_head_t *wq;
474
475 wq = dax_entry_waitqueue(mapping, index, entry, &key);
Jan Karaac401cc2016-05-12 18:29:18 +0200476
477 /*
478 * Checking for locked entry and prepare_to_wait_exclusive() happens
479 * under mapping->tree_lock, ditto for entry handling in our callers.
480 * So at this point all tasks that could have seen our entry locked
481 * must be in the waitqueue and the following check will see them.
482 */
Ross Zwisler63e95b52016-11-08 11:32:20 +1100483 if (waitqueue_active(wq))
Jan Karaac401cc2016-05-12 18:29:18 +0200484 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
Jan Karaac401cc2016-05-12 18:29:18 +0200485}
486
Jan Karabc2466e2016-05-12 18:29:19 +0200487void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
Jan Karaac401cc2016-05-12 18:29:18 +0200488{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100489 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200490
491 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100492 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
493 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200494 !slot_locked(mapping, slot))) {
495 spin_unlock_irq(&mapping->tree_lock);
496 return;
497 }
498 unlock_slot(mapping, slot);
499 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler63e95b52016-11-08 11:32:20 +1100500 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200501}
502
503static void put_locked_mapping_entry(struct address_space *mapping,
504 pgoff_t index, void *entry)
505{
506 if (!radix_tree_exceptional_entry(entry)) {
507 unlock_page(entry);
508 put_page(entry);
509 } else {
Jan Karabc2466e2016-05-12 18:29:19 +0200510 dax_unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200511 }
512}
513
514/*
515 * Called when we are done with radix tree entry we looked up via
516 * get_unlocked_mapping_entry() and which we didn't lock in the end.
517 */
518static void put_unlocked_mapping_entry(struct address_space *mapping,
519 pgoff_t index, void *entry)
520{
521 if (!radix_tree_exceptional_entry(entry))
522 return;
523
524 /* We have to wake up next waiter for the radix tree entry lock */
Ross Zwisler63e95b52016-11-08 11:32:20 +1100525 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200526}
527
528/*
529 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
530 * entry to get unlocked before deleting it.
531 */
532int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
533{
534 void *entry;
535
536 spin_lock_irq(&mapping->tree_lock);
537 entry = get_unlocked_mapping_entry(mapping, index, NULL);
538 /*
539 * This gets called from truncate / punch_hole path. As such, the caller
540 * must hold locks protecting against concurrent modifications of the
541 * radix tree (usually fs-private i_mmap_sem for writing). Since the
542 * caller has seen exceptional entry for this index, we better find it
543 * at that index as well...
544 */
545 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
546 spin_unlock_irq(&mapping->tree_lock);
547 return 0;
548 }
549 radix_tree_delete(&mapping->page_tree, index);
550 mapping->nrexceptional--;
551 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler63e95b52016-11-08 11:32:20 +1100552 dax_wake_mapping_entry_waiter(mapping, index, entry, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200553
554 return 1;
555}
556
557/*
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800558 * The user has performed a load from a hole in the file. Allocating
559 * a new page in the file would cause excessive storage usage for
560 * workloads with sparse files. We allocate a page cache page instead.
561 * We'll kick it out of the page cache if it's ever written to,
562 * otherwise it will simply fall out of the page cache under memory
563 * pressure without ever having been dirtied.
564 */
Jan Karaac401cc2016-05-12 18:29:18 +0200565static int dax_load_hole(struct address_space *mapping, void *entry,
566 struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800567{
Jan Karaac401cc2016-05-12 18:29:18 +0200568 struct page *page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800569
Jan Karaac401cc2016-05-12 18:29:18 +0200570 /* Hole page already exists? Return it... */
571 if (!radix_tree_exceptional_entry(entry)) {
572 vmf->page = entry;
573 return VM_FAULT_LOCKED;
574 }
575
576 /* This will replace locked radix tree entry with a hole page */
577 page = find_or_create_page(mapping, vmf->pgoff,
578 vmf->gfp_mask | __GFP_ZERO);
579 if (!page) {
580 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
581 return VM_FAULT_OOM;
582 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800583 vmf->page = page;
584 return VM_FAULT_LOCKED;
585}
586
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000587static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
588 struct page *to, unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800589{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800590 struct blk_dax_ctl dax = {
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000591 .sector = sector,
592 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800593 };
Ross Zwislere2e05392015-08-18 13:55:41 -0600594 void *vto;
595
Dan Williamsb2e0d162016-01-15 16:55:59 -0800596 if (dax_map_atomic(bdev, &dax) < 0)
597 return PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800598 vto = kmap_atomic(to);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800599 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800600 kunmap_atomic(vto);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800601 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800602 return 0;
603}
604
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300605#define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
Ross Zwisler9973c982016-01-22 15:10:47 -0800606
Jan Karaac401cc2016-05-12 18:29:18 +0200607static void *dax_insert_mapping_entry(struct address_space *mapping,
608 struct vm_fault *vmf,
609 void *entry, sector_t sector)
Ross Zwisler9973c982016-01-22 15:10:47 -0800610{
611 struct radix_tree_root *page_tree = &mapping->page_tree;
Jan Karaac401cc2016-05-12 18:29:18 +0200612 int error = 0;
613 bool hole_fill = false;
614 void *new_entry;
615 pgoff_t index = vmf->pgoff;
Ross Zwisler9973c982016-01-22 15:10:47 -0800616
Jan Karaac401cc2016-05-12 18:29:18 +0200617 if (vmf->flags & FAULT_FLAG_WRITE)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800618 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800619
Jan Karaac401cc2016-05-12 18:29:18 +0200620 /* Replacing hole page with block mapping? */
621 if (!radix_tree_exceptional_entry(entry)) {
622 hole_fill = true;
623 /*
624 * Unmap the page now before we remove it from page cache below.
625 * The page is locked so it cannot be faulted in again.
626 */
627 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
628 PAGE_SIZE, 0);
629 error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
630 if (error)
631 return ERR_PTR(error);
Ross Zwisler9973c982016-01-22 15:10:47 -0800632 }
633
Jan Karaac401cc2016-05-12 18:29:18 +0200634 spin_lock_irq(&mapping->tree_lock);
635 new_entry = (void *)((unsigned long)RADIX_DAX_ENTRY(sector, false) |
636 RADIX_DAX_ENTRY_LOCK);
637 if (hole_fill) {
638 __delete_from_page_cache(entry, NULL);
639 /* Drop pagecache reference */
640 put_page(entry);
641 error = radix_tree_insert(page_tree, index, new_entry);
642 if (error) {
643 new_entry = ERR_PTR(error);
Ross Zwisler9973c982016-01-22 15:10:47 -0800644 goto unlock;
645 }
Jan Karaac401cc2016-05-12 18:29:18 +0200646 mapping->nrexceptional++;
647 } else {
648 void **slot;
649 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800650
Jan Karaac401cc2016-05-12 18:29:18 +0200651 ret = __radix_tree_lookup(page_tree, index, NULL, &slot);
652 WARN_ON_ONCE(ret != entry);
653 radix_tree_replace_slot(slot, new_entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800654 }
Jan Karaac401cc2016-05-12 18:29:18 +0200655 if (vmf->flags & FAULT_FLAG_WRITE)
Ross Zwisler9973c982016-01-22 15:10:47 -0800656 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
657 unlock:
658 spin_unlock_irq(&mapping->tree_lock);
Jan Karaac401cc2016-05-12 18:29:18 +0200659 if (hole_fill) {
660 radix_tree_preload_end();
661 /*
662 * We don't need hole page anymore, it has been replaced with
663 * locked radix tree entry now.
664 */
665 if (mapping->a_ops->freepage)
666 mapping->a_ops->freepage(entry);
667 unlock_page(entry);
668 put_page(entry);
669 }
670 return new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800671}
672
673static int dax_writeback_one(struct block_device *bdev,
674 struct address_space *mapping, pgoff_t index, void *entry)
675{
676 struct radix_tree_root *page_tree = &mapping->page_tree;
677 int type = RADIX_DAX_TYPE(entry);
678 struct radix_tree_node *node;
679 struct blk_dax_ctl dax;
680 void **slot;
681 int ret = 0;
682
683 spin_lock_irq(&mapping->tree_lock);
684 /*
685 * Regular page slots are stabilized by the page lock even
686 * without the tree itself locked. These unlocked entries
687 * need verification under the tree lock.
688 */
689 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
690 goto unlock;
691 if (*slot != entry)
692 goto unlock;
693
694 /* another fsync thread may have already written back this entry */
695 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
696 goto unlock;
697
698 if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
699 ret = -EIO;
700 goto unlock;
701 }
702
703 dax.sector = RADIX_DAX_SECTOR(entry);
704 dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
705 spin_unlock_irq(&mapping->tree_lock);
706
707 /*
708 * We cannot hold tree_lock while calling dax_map_atomic() because it
709 * eventually calls cond_resched().
710 */
711 ret = dax_map_atomic(bdev, &dax);
712 if (ret < 0)
713 return ret;
714
715 if (WARN_ON_ONCE(ret < dax.size)) {
716 ret = -EIO;
717 goto unmap;
718 }
719
720 wb_cache_pmem(dax.addr, dax.size);
721
722 spin_lock_irq(&mapping->tree_lock);
723 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
724 spin_unlock_irq(&mapping->tree_lock);
725 unmap:
726 dax_unmap_atomic(bdev, &dax);
727 return ret;
728
729 unlock:
730 spin_unlock_irq(&mapping->tree_lock);
731 return ret;
732}
733
734/*
735 * Flush the mapping to the persistent domain within the byte range of [start,
736 * end]. This is required by data integrity operations to ensure file data is
737 * on persistent storage prior to completion of the operation.
738 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800739int dax_writeback_mapping_range(struct address_space *mapping,
740 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800741{
742 struct inode *inode = mapping->host;
Ross Zwisler9973c982016-01-22 15:10:47 -0800743 pgoff_t start_index, end_index, pmd_index;
744 pgoff_t indices[PAGEVEC_SIZE];
745 struct pagevec pvec;
746 bool done = false;
747 int i, ret = 0;
748 void *entry;
749
750 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
751 return -EIO;
752
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800753 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
754 return 0;
755
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300756 start_index = wbc->range_start >> PAGE_SHIFT;
757 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800758 pmd_index = DAX_PMD_INDEX(start_index);
759
760 rcu_read_lock();
761 entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
762 rcu_read_unlock();
763
764 /* see if the start of our range is covered by a PMD entry */
765 if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
766 start_index = pmd_index;
767
768 tag_pages_for_writeback(mapping, start_index, end_index);
769
770 pagevec_init(&pvec, 0);
771 while (!done) {
772 pvec.nr = find_get_entries_tag(mapping, start_index,
773 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
774 pvec.pages, indices);
775
776 if (pvec.nr == 0)
777 break;
778
779 for (i = 0; i < pvec.nr; i++) {
780 if (indices[i] > end_index) {
781 done = true;
782 break;
783 }
784
785 ret = dax_writeback_one(bdev, mapping, indices[i],
786 pvec.pages[i]);
787 if (ret < 0)
788 return ret;
789 }
790 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800791 return 0;
792}
793EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
794
Jan Karaac401cc2016-05-12 18:29:18 +0200795static int dax_insert_mapping(struct address_space *mapping,
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000796 struct block_device *bdev, sector_t sector, size_t size,
797 void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800798{
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800799 unsigned long vaddr = (unsigned long)vmf->virtual_address;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800800 struct blk_dax_ctl dax = {
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000801 .sector = sector,
802 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800803 };
Jan Karaac401cc2016-05-12 18:29:18 +0200804 void *ret;
805 void *entry = *entryp;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800806
Jan Kara4d9a2c82016-05-12 18:29:20 +0200807 if (dax_map_atomic(bdev, &dax) < 0)
808 return PTR_ERR(dax.addr);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800809 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800810
Jan Karaac401cc2016-05-12 18:29:18 +0200811 ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector);
Jan Kara4d9a2c82016-05-12 18:29:20 +0200812 if (IS_ERR(ret))
813 return PTR_ERR(ret);
Jan Karaac401cc2016-05-12 18:29:18 +0200814 *entryp = ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800815
Jan Kara4d9a2c82016-05-12 18:29:20 +0200816 return vm_insert_mixed(vma, vaddr, dax.pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800817}
818
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000819/**
Ross Zwisler6b524992016-07-26 15:21:05 -0700820 * dax_fault - handle a page fault on a DAX file
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000821 * @vma: The virtual memory area where the fault occurred
822 * @vmf: The description of the fault
823 * @get_block: The filesystem method used to translate file offsets to blocks
824 *
825 * When a page fault occurs, filesystems may call this helper in their
Ross Zwisler6b524992016-07-26 15:21:05 -0700826 * fault handler for DAX files. dax_fault() assumes the caller has done all
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000827 * the necessary locking for the page fault to proceed successfully.
828 */
Ross Zwisler6b524992016-07-26 15:21:05 -0700829int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Jan Kara02fbd132016-05-11 11:58:48 +0200830 get_block_t get_block)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800831{
832 struct file *file = vma->vm_file;
833 struct address_space *mapping = file->f_mapping;
834 struct inode *inode = mapping->host;
Jan Karaac401cc2016-05-12 18:29:18 +0200835 void *entry;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800836 struct buffer_head bh;
837 unsigned long vaddr = (unsigned long)vmf->virtual_address;
838 unsigned blkbits = inode->i_blkbits;
839 sector_t block;
840 pgoff_t size;
841 int error;
842 int major = 0;
843
Jan Karaac401cc2016-05-12 18:29:18 +0200844 /*
845 * Check whether offset isn't beyond end of file now. Caller is supposed
846 * to hold locks serializing us with truncate / punch hole so this is
847 * a reliable test.
848 */
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800849 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
850 if (vmf->pgoff >= size)
851 return VM_FAULT_SIGBUS;
852
853 memset(&bh, 0, sizeof(bh));
854 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
Ross Zwislereab95db2016-01-22 15:10:59 -0800855 bh.b_bdev = inode->i_sb->s_bdev;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800856 bh.b_size = PAGE_SIZE;
857
Jan Karaac401cc2016-05-12 18:29:18 +0200858 entry = grab_mapping_entry(mapping, vmf->pgoff);
859 if (IS_ERR(entry)) {
860 error = PTR_ERR(entry);
861 goto out;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800862 }
863
864 error = get_block(inode, block, &bh, 0);
865 if (!error && (bh.b_size < PAGE_SIZE))
866 error = -EIO; /* fs corruption? */
867 if (error)
Jan Karaac401cc2016-05-12 18:29:18 +0200868 goto unlock_entry;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800869
870 if (vmf->cow_page) {
871 struct page *new_page = vmf->cow_page;
872 if (buffer_written(&bh))
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000873 error = copy_user_dax(bh.b_bdev, to_sector(&bh, inode),
874 bh.b_size, new_page, vaddr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800875 else
876 clear_user_highpage(new_page, vaddr);
877 if (error)
Jan Karaac401cc2016-05-12 18:29:18 +0200878 goto unlock_entry;
879 if (!radix_tree_exceptional_entry(entry)) {
880 vmf->page = entry;
Jan Karabc2466e2016-05-12 18:29:19 +0200881 return VM_FAULT_LOCKED;
Jan Karaac401cc2016-05-12 18:29:18 +0200882 }
Jan Karabc2466e2016-05-12 18:29:19 +0200883 vmf->entry = entry;
884 return VM_FAULT_DAX_LOCKED;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800885 }
886
Jan Karaac401cc2016-05-12 18:29:18 +0200887 if (!buffer_mapped(&bh)) {
888 if (vmf->flags & FAULT_FLAG_WRITE) {
889 error = get_block(inode, block, &bh, 1);
890 count_vm_event(PGMAJFAULT);
891 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
892 major = VM_FAULT_MAJOR;
893 if (!error && (bh.b_size < PAGE_SIZE))
894 error = -EIO;
895 if (error)
896 goto unlock_entry;
897 } else {
898 return dax_load_hole(mapping, entry, vmf);
899 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800900 }
901
Jan Kara02fbd132016-05-11 11:58:48 +0200902 /* Filesystem should not return unwritten buffers to us! */
Jan Kara2b109452016-05-11 11:58:50 +0200903 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000904 error = dax_insert_mapping(mapping, bh.b_bdev, to_sector(&bh, inode),
905 bh.b_size, &entry, vma, vmf);
Jan Karaac401cc2016-05-12 18:29:18 +0200906 unlock_entry:
907 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800908 out:
909 if (error == -ENOMEM)
910 return VM_FAULT_OOM | major;
911 /* -EBUSY is fine, somebody else faulted on the same PTE */
912 if ((error < 0) && (error != -EBUSY))
913 return VM_FAULT_SIGBUS | major;
914 return VM_FAULT_NOPAGE | major;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800915}
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800916EXPORT_SYMBOL_GPL(dax_fault);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800917
918/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700919 * dax_pfn_mkwrite - handle first write to DAX page
920 * @vma: The virtual memory area where the fault occurred
921 * @vmf: The description of the fault
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700922 */
923int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
924{
Ross Zwisler9973c982016-01-22 15:10:47 -0800925 struct file *file = vma->vm_file;
Jan Karaac401cc2016-05-12 18:29:18 +0200926 struct address_space *mapping = file->f_mapping;
927 void *entry;
928 pgoff_t index = vmf->pgoff;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700929
Jan Karaac401cc2016-05-12 18:29:18 +0200930 spin_lock_irq(&mapping->tree_lock);
931 entry = get_unlocked_mapping_entry(mapping, index, NULL);
932 if (!entry || !radix_tree_exceptional_entry(entry))
933 goto out;
934 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
935 put_unlocked_mapping_entry(mapping, index, entry);
936out:
937 spin_unlock_irq(&mapping->tree_lock);
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700938 return VM_FAULT_NOPAGE;
939}
940EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
941
Vishal Verma4b0228f2016-04-21 15:13:46 -0400942static bool dax_range_is_aligned(struct block_device *bdev,
943 unsigned int offset, unsigned int length)
944{
945 unsigned short sector_size = bdev_logical_block_size(bdev);
946
947 if (!IS_ALIGNED(offset, sector_size))
948 return false;
949 if (!IS_ALIGNED(length, sector_size))
950 return false;
951
952 return true;
953}
954
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200955int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
956 unsigned int offset, unsigned int length)
957{
958 struct blk_dax_ctl dax = {
959 .sector = sector,
960 .size = PAGE_SIZE,
961 };
962
Vishal Verma4b0228f2016-04-21 15:13:46 -0400963 if (dax_range_is_aligned(bdev, offset, length)) {
964 sector_t start_sector = dax.sector + (offset >> 9);
965
966 return blkdev_issue_zeroout(bdev, start_sector,
967 length >> 9, GFP_NOFS, true);
968 } else {
969 if (dax_map_atomic(bdev, &dax) < 0)
970 return PTR_ERR(dax.addr);
971 clear_pmem(dax.addr + offset, length);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400972 dax_unmap_atomic(bdev, &dax);
973 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200974 return 0;
975}
976EXPORT_SYMBOL_GPL(__dax_zero_page_range);
977
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700978/**
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800979 * dax_zero_page_range - zero a range within a page of a DAX file
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800980 * @inode: The file being truncated
981 * @from: The file offset that is being truncated to
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800982 * @length: The number of bytes to zero
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800983 * @get_block: The filesystem method used to translate file offsets to blocks
984 *
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800985 * This function can be called by a filesystem when it is zeroing part of a
986 * page in a DAX file. This is intended for hole-punch operations. If
987 * you are truncating a file, the helper function dax_truncate_page() may be
988 * more convenient.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800989 */
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800990int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
991 get_block_t get_block)
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800992{
993 struct buffer_head bh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300994 pgoff_t index = from >> PAGE_SHIFT;
995 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800996 int err;
997
998 /* Block boundary? Nothing to do */
999 if (!length)
1000 return 0;
Ross Zwisleraada54f2016-11-08 11:32:00 +11001001 if (WARN_ON_ONCE((offset + length) > PAGE_SIZE))
1002 return -EINVAL;
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001003
1004 memset(&bh, 0, sizeof(bh));
Ross Zwislereab95db2016-01-22 15:10:59 -08001005 bh.b_bdev = inode->i_sb->s_bdev;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001006 bh.b_size = PAGE_SIZE;
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001007 err = get_block(inode, index, &bh, 0);
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001008 if (err < 0 || !buffer_written(&bh))
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001009 return err;
Dan Williamsb2e0d162016-01-15 16:55:59 -08001010
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001011 return __dax_zero_page_range(bh.b_bdev, to_sector(&bh, inode),
1012 offset, length);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001013}
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001014EXPORT_SYMBOL_GPL(dax_zero_page_range);
1015
1016/**
1017 * dax_truncate_page - handle a partial page being truncated in a DAX file
1018 * @inode: The file being truncated
1019 * @from: The file offset that is being truncated to
1020 * @get_block: The filesystem method used to translate file offsets to blocks
1021 *
1022 * Similar to block_truncate_page(), this function can be called by a
1023 * filesystem when it is truncating a DAX file to handle the partial page.
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001024 */
1025int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1026{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 unsigned length = PAGE_ALIGN(from) - from;
Matthew Wilcox25726bc2015-02-16 15:59:35 -08001028 return dax_zero_page_range(inode, from, length, get_block);
1029}
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -08001030EXPORT_SYMBOL_GPL(dax_truncate_page);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001031
1032#ifdef CONFIG_FS_IOMAP
Ross Zwisler333ccc92016-11-08 11:33:09 +11001033static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
1034{
1035 return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
1036}
1037
Christoph Hellwiga254e562016-09-19 11:24:49 +10001038static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001039dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +10001040 struct iomap *iomap)
1041{
1042 struct iov_iter *iter = data;
1043 loff_t end = pos + length, done = 0;
1044 ssize_t ret = 0;
1045
1046 if (iov_iter_rw(iter) == READ) {
1047 end = min(end, i_size_read(inode));
1048 if (pos >= end)
1049 return 0;
1050
1051 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1052 return iov_iter_zero(min(length, end - pos), iter);
1053 }
1054
1055 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1056 return -EIO;
1057
1058 while (pos < end) {
1059 unsigned offset = pos & (PAGE_SIZE - 1);
1060 struct blk_dax_ctl dax = { 0 };
1061 ssize_t map_len;
1062
Ross Zwisler333ccc92016-11-08 11:33:09 +11001063 dax.sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001064 dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
1065 map_len = dax_map_atomic(iomap->bdev, &dax);
1066 if (map_len < 0) {
1067 ret = map_len;
1068 break;
1069 }
1070
1071 dax.addr += offset;
1072 map_len -= offset;
1073 if (map_len > end - pos)
1074 map_len = end - pos;
1075
1076 if (iov_iter_rw(iter) == WRITE)
1077 map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
1078 else
1079 map_len = copy_to_iter(dax.addr, map_len, iter);
1080 dax_unmap_atomic(iomap->bdev, &dax);
1081 if (map_len <= 0) {
1082 ret = map_len ? map_len : -EFAULT;
1083 break;
1084 }
1085
1086 pos += map_len;
1087 length -= map_len;
1088 done += map_len;
1089 }
1090
1091 return done ? done : ret;
1092}
1093
1094/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001095 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001096 * @iocb: The control block for this I/O
1097 * @iter: The addresses to do I/O from or to
1098 * @ops: iomap ops passed from the file system
1099 *
1100 * This function performs read and write operations to directly mapped
1101 * persistent memory. The callers needs to take care of read/write exclusion
1102 * and evicting any page cache pages in the region under I/O.
1103 */
1104ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001105dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwiga254e562016-09-19 11:24:49 +10001106 struct iomap_ops *ops)
1107{
1108 struct address_space *mapping = iocb->ki_filp->f_mapping;
1109 struct inode *inode = mapping->host;
1110 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1111 unsigned flags = 0;
1112
1113 if (iov_iter_rw(iter) == WRITE)
1114 flags |= IOMAP_WRITE;
1115
1116 /*
1117 * Yes, even DAX files can have page cache attached to them: A zeroed
1118 * page is inserted into the pagecache when we have to serve a write
1119 * fault on a hole. It should never be dirtied and can simply be
1120 * dropped from the pagecache once we get real data for the page.
1121 *
1122 * XXX: This is racy against mmap, and there's nothing we can do about
1123 * it. We'll eventually need to shift this down even further so that
1124 * we can check if we allocated blocks over a hole first.
1125 */
1126 if (mapping->nrpages) {
1127 ret = invalidate_inode_pages2_range(mapping,
1128 pos >> PAGE_SHIFT,
1129 (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
1130 WARN_ON_ONCE(ret);
1131 }
1132
1133 while (iov_iter_count(iter)) {
1134 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001135 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001136 if (ret <= 0)
1137 break;
1138 pos += ret;
1139 done += ret;
1140 }
1141
1142 iocb->ki_pos += done;
1143 return done ? done : ret;
1144}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001145EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001146
1147/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001148 * dax_iomap_fault - handle a page fault on a DAX file
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001149 * @vma: The virtual memory area where the fault occurred
1150 * @vmf: The description of the fault
1151 * @ops: iomap ops passed from the file system
1152 *
1153 * When a page fault occurs, filesystems may call this helper in their fault
1154 * or mkwrite handler for DAX files. Assumes the caller has done all the
1155 * necessary locking for the page fault to proceed successfully.
1156 */
Ross Zwisler11c59c92016-11-08 11:32:46 +11001157int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001158 struct iomap_ops *ops)
1159{
1160 struct address_space *mapping = vma->vm_file->f_mapping;
1161 struct inode *inode = mapping->host;
1162 unsigned long vaddr = (unsigned long)vmf->virtual_address;
1163 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1164 sector_t sector;
1165 struct iomap iomap = { 0 };
1166 unsigned flags = 0;
1167 int error, major = 0;
Ross Zwisler15502902016-11-08 11:33:26 +11001168 int locked_status = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001169 void *entry;
1170
1171 /*
1172 * Check whether offset isn't beyond end of file now. Caller is supposed
1173 * to hold locks serializing us with truncate / punch hole so this is
1174 * a reliable test.
1175 */
1176 if (pos >= i_size_read(inode))
1177 return VM_FAULT_SIGBUS;
1178
1179 entry = grab_mapping_entry(mapping, vmf->pgoff);
1180 if (IS_ERR(entry)) {
1181 error = PTR_ERR(entry);
1182 goto out;
1183 }
1184
1185 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1186 flags |= IOMAP_WRITE;
1187
1188 /*
1189 * Note that we don't bother to use iomap_apply here: DAX required
1190 * the file system block size to be equal the page size, which means
1191 * that we never have to deal with more than a single extent here.
1192 */
1193 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1194 if (error)
1195 goto unlock_entry;
1196 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
1197 error = -EIO; /* fs corruption? */
Ross Zwisler15502902016-11-08 11:33:26 +11001198 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001199 }
1200
Ross Zwisler333ccc92016-11-08 11:33:09 +11001201 sector = dax_iomap_sector(&iomap, pos);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001202
1203 if (vmf->cow_page) {
1204 switch (iomap.type) {
1205 case IOMAP_HOLE:
1206 case IOMAP_UNWRITTEN:
1207 clear_user_highpage(vmf->cow_page, vaddr);
1208 break;
1209 case IOMAP_MAPPED:
1210 error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
1211 vmf->cow_page, vaddr);
1212 break;
1213 default:
1214 WARN_ON_ONCE(1);
1215 error = -EIO;
1216 break;
1217 }
1218
1219 if (error)
Ross Zwisler15502902016-11-08 11:33:26 +11001220 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001221 if (!radix_tree_exceptional_entry(entry)) {
1222 vmf->page = entry;
Ross Zwisler15502902016-11-08 11:33:26 +11001223 locked_status = VM_FAULT_LOCKED;
1224 } else {
1225 vmf->entry = entry;
1226 locked_status = VM_FAULT_DAX_LOCKED;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001227 }
Ross Zwisler15502902016-11-08 11:33:26 +11001228 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001229 }
1230
1231 switch (iomap.type) {
1232 case IOMAP_MAPPED:
1233 if (iomap.flags & IOMAP_F_NEW) {
1234 count_vm_event(PGMAJFAULT);
1235 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1236 major = VM_FAULT_MAJOR;
1237 }
1238 error = dax_insert_mapping(mapping, iomap.bdev, sector,
1239 PAGE_SIZE, &entry, vma, vmf);
1240 break;
1241 case IOMAP_UNWRITTEN:
1242 case IOMAP_HOLE:
Ross Zwisler15502902016-11-08 11:33:26 +11001243 if (!(vmf->flags & FAULT_FLAG_WRITE)) {
1244 locked_status = dax_load_hole(mapping, entry, vmf);
1245 break;
1246 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001247 /*FALLTHRU*/
1248 default:
1249 WARN_ON_ONCE(1);
1250 error = -EIO;
1251 break;
1252 }
1253
Ross Zwisler15502902016-11-08 11:33:26 +11001254 finish_iomap:
1255 if (ops->iomap_end) {
1256 if (error) {
1257 /* keep previous error */
1258 ops->iomap_end(inode, pos, PAGE_SIZE, 0, flags,
1259 &iomap);
1260 } else {
1261 error = ops->iomap_end(inode, pos, PAGE_SIZE,
1262 PAGE_SIZE, flags, &iomap);
1263 }
1264 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001265 unlock_entry:
Ross Zwisler15502902016-11-08 11:33:26 +11001266 if (!locked_status || error)
1267 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001268 out:
1269 if (error == -ENOMEM)
1270 return VM_FAULT_OOM | major;
1271 /* -EBUSY is fine, somebody else faulted on the same PTE */
1272 if (error < 0 && error != -EBUSY)
1273 return VM_FAULT_SIGBUS | major;
Ross Zwisler15502902016-11-08 11:33:26 +11001274 if (locked_status) {
1275 WARN_ON_ONCE(error); /* -EBUSY from ops->iomap_end? */
1276 return locked_status;
1277 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001278 return VM_FAULT_NOPAGE | major;
1279}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001280EXPORT_SYMBOL_GPL(dax_iomap_fault);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001281#endif /* CONFIG_FS_IOMAP */