blob: f1dfae64b11af0fa0f1708155334a3da783c6859 [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 Karaac401cc2016-05-12 18:29:18 +020037/* We choose 4096 entries - same as per-zone page wait tables */
38#define DAX_WAIT_TABLE_BITS 12
39#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
40
Ross Zwislerce95ab02016-11-08 11:31:44 +110041static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020042
43static int __init init_dax_wait_table(void)
44{
45 int i;
46
47 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
48 init_waitqueue_head(wait_table + i);
49 return 0;
50}
51fs_initcall(init_dax_wait_table);
52
Dan Williamsb2e0d162016-01-15 16:55:59 -080053static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
54{
55 struct request_queue *q = bdev->bd_queue;
56 long rc = -EIO;
57
Dan Williams7a9eb202016-06-03 18:06:47 -070058 dax->addr = ERR_PTR(-EIO);
Dan Williamsb2e0d162016-01-15 16:55:59 -080059 if (blk_queue_enter(q, true) != 0)
60 return rc;
61
62 rc = bdev_direct_access(bdev, dax);
63 if (rc < 0) {
Dan Williams7a9eb202016-06-03 18:06:47 -070064 dax->addr = ERR_PTR(rc);
Dan Williamsb2e0d162016-01-15 16:55:59 -080065 blk_queue_exit(q);
66 return rc;
67 }
68 return rc;
69}
70
71static void dax_unmap_atomic(struct block_device *bdev,
72 const struct blk_dax_ctl *dax)
73{
74 if (IS_ERR(dax->addr))
75 return;
76 blk_queue_exit(bdev->bd_queue);
77}
78
Ross Zwisler642261a2016-11-08 11:34:45 +110079static int dax_is_pmd_entry(void *entry)
80{
81 return (unsigned long)entry & RADIX_DAX_PMD;
82}
83
84static int dax_is_pte_entry(void *entry)
85{
86 return !((unsigned long)entry & RADIX_DAX_PMD);
87}
88
89static int dax_is_zero_entry(void *entry)
90{
91 return (unsigned long)entry & RADIX_DAX_HZP;
92}
93
94static int dax_is_empty_entry(void *entry)
95{
96 return (unsigned long)entry & RADIX_DAX_EMPTY;
97}
98
Dan Williamsd1a5f2b42016-01-28 20:25:31 -080099struct page *read_dax_sector(struct block_device *bdev, sector_t n)
100{
101 struct page *page = alloc_pages(GFP_KERNEL, 0);
102 struct blk_dax_ctl dax = {
103 .size = PAGE_SIZE,
104 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
105 };
106 long rc;
107
108 if (!page)
109 return ERR_PTR(-ENOMEM);
110
111 rc = dax_map_atomic(bdev, &dax);
112 if (rc < 0)
113 return ERR_PTR(rc);
114 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
115 dax_unmap_atomic(bdev, &dax);
116 return page;
117}
118
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800119/*
Jan Karaac401cc2016-05-12 18:29:18 +0200120 * DAX radix tree locking
121 */
122struct exceptional_entry_key {
123 struct address_space *mapping;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100124 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200125};
126
127struct wait_exceptional_entry_queue {
128 wait_queue_t wait;
129 struct exceptional_entry_key key;
130};
131
Ross Zwisler63e95b52016-11-08 11:32:20 +1100132static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
133 pgoff_t index, void *entry, struct exceptional_entry_key *key)
134{
135 unsigned long hash;
136
137 /*
138 * If 'entry' is a PMD, align the 'index' that we use for the wait
139 * queue to the start of that PMD. This ensures that all offsets in
140 * the range covered by the PMD map to the same bit lock.
141 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100142 if (dax_is_pmd_entry(entry))
Ross Zwisler63e95b52016-11-08 11:32:20 +1100143 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
144
145 key->mapping = mapping;
146 key->entry_start = index;
147
148 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
149 return wait_table + hash;
150}
151
Jan Karaac401cc2016-05-12 18:29:18 +0200152static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
153 int sync, void *keyp)
154{
155 struct exceptional_entry_key *key = keyp;
156 struct wait_exceptional_entry_queue *ewait =
157 container_of(wait, struct wait_exceptional_entry_queue, wait);
158
159 if (key->mapping != ewait->key.mapping ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100160 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200161 return 0;
162 return autoremove_wake_function(wait, mode, sync, NULL);
163}
164
165/*
166 * Check whether the given slot is locked. The function must be called with
167 * mapping->tree_lock held
168 */
169static inline int slot_locked(struct address_space *mapping, void **slot)
170{
171 unsigned long entry = (unsigned long)
172 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
173 return entry & RADIX_DAX_ENTRY_LOCK;
174}
175
176/*
177 * Mark the given slot is locked. The function must be called with
178 * mapping->tree_lock held
179 */
180static inline void *lock_slot(struct address_space *mapping, void **slot)
181{
182 unsigned long entry = (unsigned long)
183 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
184
185 entry |= RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800186 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200187 return (void *)entry;
188}
189
190/*
191 * Mark the given slot is unlocked. The function must be called with
192 * mapping->tree_lock held
193 */
194static inline void *unlock_slot(struct address_space *mapping, void **slot)
195{
196 unsigned long entry = (unsigned long)
197 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
198
199 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800200 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200201 return (void *)entry;
202}
203
204/*
205 * Lookup entry in radix tree, wait for it to become unlocked if it is
206 * exceptional entry and return it. The caller must call
207 * put_unlocked_mapping_entry() when he decided not to lock the entry or
208 * put_locked_mapping_entry() when he locked the entry and now wants to
209 * unlock it.
210 *
211 * The function must be called with mapping->tree_lock held.
212 */
213static void *get_unlocked_mapping_entry(struct address_space *mapping,
214 pgoff_t index, void ***slotp)
215{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100216 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200217 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100218 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200219
220 init_wait(&ewait.wait);
221 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200222
223 for (;;) {
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100224 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200225 &slot);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100226 if (!entry || !radix_tree_exceptional_entry(entry) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200227 !slot_locked(mapping, slot)) {
228 if (slotp)
229 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100230 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200231 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100232
233 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200234 prepare_to_wait_exclusive(wq, &ewait.wait,
235 TASK_UNINTERRUPTIBLE);
236 spin_unlock_irq(&mapping->tree_lock);
237 schedule();
238 finish_wait(wq, &ewait.wait);
239 spin_lock_irq(&mapping->tree_lock);
240 }
241}
242
Jan Karaac401cc2016-05-12 18:29:18 +0200243static void put_locked_mapping_entry(struct address_space *mapping,
244 pgoff_t index, void *entry)
245{
246 if (!radix_tree_exceptional_entry(entry)) {
247 unlock_page(entry);
248 put_page(entry);
249 } else {
Jan Karabc2466e2016-05-12 18:29:19 +0200250 dax_unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200251 }
252}
253
254/*
255 * Called when we are done with radix tree entry we looked up via
256 * get_unlocked_mapping_entry() and which we didn't lock in the end.
257 */
258static void put_unlocked_mapping_entry(struct address_space *mapping,
259 pgoff_t index, void *entry)
260{
261 if (!radix_tree_exceptional_entry(entry))
262 return;
263
264 /* We have to wake up next waiter for the radix tree entry lock */
Ross Zwisler422476c2016-11-08 11:33:44 +1100265 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
266}
267
Jan Karaac401cc2016-05-12 18:29:18 +0200268/*
269 * Find radix tree entry at given index. If it points to a page, return with
270 * the page locked. If it points to the exceptional entry, return with the
271 * radix tree entry locked. If the radix tree doesn't contain given index,
272 * create empty exceptional entry for the index and return with it locked.
273 *
Ross Zwisler642261a2016-11-08 11:34:45 +1100274 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
275 * either return that locked entry or will return an error. This error will
276 * happen if there are any 4k entries (either zero pages or DAX entries)
277 * within the 2MiB range that we are requesting.
278 *
279 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
280 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
281 * insertion will fail if it finds any 4k entries already in the tree, and a
282 * 4k insertion will cause an existing 2MiB entry to be unmapped and
283 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
284 * well as 2MiB empty entries.
285 *
286 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
287 * real storage backing them. We will leave these real 2MiB DAX entries in
288 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
289 *
Jan Karaac401cc2016-05-12 18:29:18 +0200290 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
291 * persistent memory the benefit is doubtful. We can add that later if we can
292 * show it helps.
293 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100294static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
295 unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200296{
Ross Zwisler642261a2016-11-08 11:34:45 +1100297 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100298 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200299
300restart:
301 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100302 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100303
304 if (entry) {
305 if (size_flag & RADIX_DAX_PMD) {
306 if (!radix_tree_exceptional_entry(entry) ||
307 dax_is_pte_entry(entry)) {
308 put_unlocked_mapping_entry(mapping, index,
309 entry);
310 entry = ERR_PTR(-EEXIST);
311 goto out_unlock;
312 }
313 } else { /* trying to grab a PTE entry */
314 if (radix_tree_exceptional_entry(entry) &&
315 dax_is_pmd_entry(entry) &&
316 (dax_is_zero_entry(entry) ||
317 dax_is_empty_entry(entry))) {
318 pmd_downgrade = true;
319 }
320 }
321 }
322
Jan Karaac401cc2016-05-12 18:29:18 +0200323 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwisler642261a2016-11-08 11:34:45 +1100324 if (!entry || pmd_downgrade) {
Jan Karaac401cc2016-05-12 18:29:18 +0200325 int err;
326
Ross Zwisler642261a2016-11-08 11:34:45 +1100327 if (pmd_downgrade) {
328 /*
329 * Make sure 'entry' remains valid while we drop
330 * mapping->tree_lock.
331 */
332 entry = lock_slot(mapping, slot);
333 }
334
Jan Karaac401cc2016-05-12 18:29:18 +0200335 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100336 /*
337 * Besides huge zero pages the only other thing that gets
338 * downgraded are empty entries which don't need to be
339 * unmapped.
340 */
341 if (pmd_downgrade && dax_is_zero_entry(entry))
342 unmap_mapping_range(mapping,
343 (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
344
Jan Kara0cb80b42016-12-12 21:34:12 -0500345 err = radix_tree_preload(
346 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
347 if (err) {
348 if (pmd_downgrade)
349 put_locked_mapping_entry(mapping, index, entry);
350 return ERR_PTR(err);
351 }
Jan Karaac401cc2016-05-12 18:29:18 +0200352 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100353
354 if (pmd_downgrade) {
355 radix_tree_delete(&mapping->page_tree, index);
356 mapping->nrexceptional--;
357 dax_wake_mapping_entry_waiter(mapping, index, entry,
358 true);
359 }
360
361 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
362
363 err = __radix_tree_insert(&mapping->page_tree, index,
364 dax_radix_order(entry), entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200365 radix_tree_preload_end();
366 if (err) {
367 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100368 /*
369 * Someone already created the entry? This is a
370 * normal failure when inserting PMDs in a range
371 * that already contains PTEs. In that case we want
372 * to return -EEXIST immediately.
373 */
374 if (err == -EEXIST && !(size_flag & RADIX_DAX_PMD))
Jan Karaac401cc2016-05-12 18:29:18 +0200375 goto restart;
Ross Zwisler642261a2016-11-08 11:34:45 +1100376 /*
377 * Our insertion of a DAX PMD entry failed, most
378 * likely because it collided with a PTE sized entry
379 * at a different index in the PMD range. We haven't
380 * inserted anything into the radix tree and have no
381 * waiters to wake.
382 */
Jan Karaac401cc2016-05-12 18:29:18 +0200383 return ERR_PTR(err);
384 }
385 /* Good, we have inserted empty locked entry into the tree. */
386 mapping->nrexceptional++;
387 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100388 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200389 }
390 /* Normal page in radix tree? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100391 if (!radix_tree_exceptional_entry(entry)) {
392 struct page *page = entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200393
394 get_page(page);
395 spin_unlock_irq(&mapping->tree_lock);
396 lock_page(page);
397 /* Page got truncated? Retry... */
398 if (unlikely(page->mapping != mapping)) {
399 unlock_page(page);
400 put_page(page);
401 goto restart;
402 }
403 return page;
404 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100405 entry = lock_slot(mapping, slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100406 out_unlock:
Jan Karaac401cc2016-05-12 18:29:18 +0200407 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100408 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200409}
410
Ross Zwisler63e95b52016-11-08 11:32:20 +1100411/*
412 * We do not necessarily hold the mapping->tree_lock when we call this
413 * function so it is possible that 'entry' is no longer a valid item in the
Ross Zwisler642261a2016-11-08 11:34:45 +1100414 * radix tree. This is okay because all we really need to do is to find the
415 * correct waitqueue where tasks might be waiting for that old 'entry' and
416 * wake them.
Ross Zwisler63e95b52016-11-08 11:32:20 +1100417 */
Jan Karaac401cc2016-05-12 18:29:18 +0200418void dax_wake_mapping_entry_waiter(struct address_space *mapping,
Ross Zwisler63e95b52016-11-08 11:32:20 +1100419 pgoff_t index, void *entry, bool wake_all)
Jan Karaac401cc2016-05-12 18:29:18 +0200420{
Ross Zwisler63e95b52016-11-08 11:32:20 +1100421 struct exceptional_entry_key key;
422 wait_queue_head_t *wq;
423
424 wq = dax_entry_waitqueue(mapping, index, entry, &key);
Jan Karaac401cc2016-05-12 18:29:18 +0200425
426 /*
427 * Checking for locked entry and prepare_to_wait_exclusive() happens
428 * under mapping->tree_lock, ditto for entry handling in our callers.
429 * So at this point all tasks that could have seen our entry locked
430 * must be in the waitqueue and the following check will see them.
431 */
Ross Zwisler63e95b52016-11-08 11:32:20 +1100432 if (waitqueue_active(wq))
Jan Karaac401cc2016-05-12 18:29:18 +0200433 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
Jan Karaac401cc2016-05-12 18:29:18 +0200434}
435
436void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
437{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100438 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200439
440 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100441 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
442 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200443 !slot_locked(mapping, slot))) {
444 spin_unlock_irq(&mapping->tree_lock);
445 return;
446 }
447 unlock_slot(mapping, slot);
448 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler63e95b52016-11-08 11:32:20 +1100449 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200450}
451
452/*
453 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
454 * entry to get unlocked before deleting it.
455 */
456int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
457{
458 void *entry;
459
460 spin_lock_irq(&mapping->tree_lock);
461 entry = get_unlocked_mapping_entry(mapping, index, NULL);
462 /*
463 * This gets called from truncate / punch_hole path. As such, the caller
464 * must hold locks protecting against concurrent modifications of the
465 * radix tree (usually fs-private i_mmap_sem for writing). Since the
466 * caller has seen exceptional entry for this index, we better find it
467 * at that index as well...
468 */
469 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
470 spin_unlock_irq(&mapping->tree_lock);
471 return 0;
472 }
473 radix_tree_delete(&mapping->page_tree, index);
474 mapping->nrexceptional--;
475 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler63e95b52016-11-08 11:32:20 +1100476 dax_wake_mapping_entry_waiter(mapping, index, entry, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200477
478 return 1;
479}
480
481/*
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800482 * The user has performed a load from a hole in the file. Allocating
483 * a new page in the file would cause excessive storage usage for
484 * workloads with sparse files. We allocate a page cache page instead.
485 * We'll kick it out of the page cache if it's ever written to,
486 * otherwise it will simply fall out of the page cache under memory
487 * pressure without ever having been dirtied.
488 */
Jan Karaac401cc2016-05-12 18:29:18 +0200489static int dax_load_hole(struct address_space *mapping, void *entry,
490 struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800491{
Jan Karaac401cc2016-05-12 18:29:18 +0200492 struct page *page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800493
Jan Karaac401cc2016-05-12 18:29:18 +0200494 /* Hole page already exists? Return it... */
495 if (!radix_tree_exceptional_entry(entry)) {
496 vmf->page = entry;
497 return VM_FAULT_LOCKED;
498 }
499
500 /* This will replace locked radix tree entry with a hole page */
501 page = find_or_create_page(mapping, vmf->pgoff,
502 vmf->gfp_mask | __GFP_ZERO);
503 if (!page) {
504 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
505 return VM_FAULT_OOM;
506 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800507 vmf->page = page;
508 return VM_FAULT_LOCKED;
509}
510
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000511static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
512 struct page *to, unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800513{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800514 struct blk_dax_ctl dax = {
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000515 .sector = sector,
516 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800517 };
Ross Zwislere2e05392015-08-18 13:55:41 -0600518 void *vto;
519
Dan Williamsb2e0d162016-01-15 16:55:59 -0800520 if (dax_map_atomic(bdev, &dax) < 0)
521 return PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800522 vto = kmap_atomic(to);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800523 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800524 kunmap_atomic(vto);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800525 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800526 return 0;
527}
528
Ross Zwisler642261a2016-11-08 11:34:45 +1100529/*
530 * By this point grab_mapping_entry() has ensured that we have a locked entry
531 * of the appropriate size so we don't have to worry about downgrading PMDs to
532 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
533 * already in the tree, we will skip the insertion and just dirty the PMD as
534 * appropriate.
535 */
Jan Karaac401cc2016-05-12 18:29:18 +0200536static void *dax_insert_mapping_entry(struct address_space *mapping,
537 struct vm_fault *vmf,
Ross Zwisler642261a2016-11-08 11:34:45 +1100538 void *entry, sector_t sector,
539 unsigned long flags)
Ross Zwisler9973c982016-01-22 15:10:47 -0800540{
541 struct radix_tree_root *page_tree = &mapping->page_tree;
Jan Karaac401cc2016-05-12 18:29:18 +0200542 int error = 0;
543 bool hole_fill = false;
544 void *new_entry;
545 pgoff_t index = vmf->pgoff;
Ross Zwisler9973c982016-01-22 15:10:47 -0800546
Jan Karaac401cc2016-05-12 18:29:18 +0200547 if (vmf->flags & FAULT_FLAG_WRITE)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800548 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800549
Jan Karaac401cc2016-05-12 18:29:18 +0200550 /* Replacing hole page with block mapping? */
551 if (!radix_tree_exceptional_entry(entry)) {
552 hole_fill = true;
553 /*
554 * Unmap the page now before we remove it from page cache below.
555 * The page is locked so it cannot be faulted in again.
556 */
557 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
558 PAGE_SIZE, 0);
559 error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
560 if (error)
561 return ERR_PTR(error);
Ross Zwisler642261a2016-11-08 11:34:45 +1100562 } else if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_HZP)) {
563 /* replacing huge zero page with PMD block mapping */
564 unmap_mapping_range(mapping,
565 (vmf->pgoff << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
Ross Zwisler9973c982016-01-22 15:10:47 -0800566 }
567
Jan Karaac401cc2016-05-12 18:29:18 +0200568 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100569 new_entry = dax_radix_locked_entry(sector, flags);
570
Jan Karaac401cc2016-05-12 18:29:18 +0200571 if (hole_fill) {
572 __delete_from_page_cache(entry, NULL);
573 /* Drop pagecache reference */
574 put_page(entry);
Ross Zwisler642261a2016-11-08 11:34:45 +1100575 error = __radix_tree_insert(page_tree, index,
576 dax_radix_order(new_entry), new_entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200577 if (error) {
578 new_entry = ERR_PTR(error);
Ross Zwisler9973c982016-01-22 15:10:47 -0800579 goto unlock;
580 }
Jan Karaac401cc2016-05-12 18:29:18 +0200581 mapping->nrexceptional++;
Ross Zwisler642261a2016-11-08 11:34:45 +1100582 } else if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
583 /*
584 * Only swap our new entry into the radix tree if the current
585 * entry is a zero page or an empty entry. If a normal PTE or
586 * PMD entry is already in the tree, we leave it alone. This
587 * means that if we are trying to insert a PTE and the
588 * existing entry is a PMD, we will just leave the PMD in the
589 * tree and dirty it if necessary.
590 */
Johannes Weinerf7942432016-12-12 16:43:41 -0800591 struct radix_tree_node *node;
Jan Karaac401cc2016-05-12 18:29:18 +0200592 void **slot;
593 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800594
Johannes Weinerf7942432016-12-12 16:43:41 -0800595 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200596 WARN_ON_ONCE(ret != entry);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800597 __radix_tree_replace(page_tree, node, slot,
598 new_entry, NULL, NULL);
Ross Zwisler9973c982016-01-22 15:10:47 -0800599 }
Jan Karaac401cc2016-05-12 18:29:18 +0200600 if (vmf->flags & FAULT_FLAG_WRITE)
Ross Zwisler9973c982016-01-22 15:10:47 -0800601 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
602 unlock:
603 spin_unlock_irq(&mapping->tree_lock);
Jan Karaac401cc2016-05-12 18:29:18 +0200604 if (hole_fill) {
605 radix_tree_preload_end();
606 /*
607 * We don't need hole page anymore, it has been replaced with
608 * locked radix tree entry now.
609 */
610 if (mapping->a_ops->freepage)
611 mapping->a_ops->freepage(entry);
612 unlock_page(entry);
613 put_page(entry);
614 }
615 return new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800616}
617
618static int dax_writeback_one(struct block_device *bdev,
619 struct address_space *mapping, pgoff_t index, void *entry)
620{
621 struct radix_tree_root *page_tree = &mapping->page_tree;
Ross Zwisler9973c982016-01-22 15:10:47 -0800622 struct radix_tree_node *node;
623 struct blk_dax_ctl dax;
624 void **slot;
625 int ret = 0;
626
627 spin_lock_irq(&mapping->tree_lock);
628 /*
629 * Regular page slots are stabilized by the page lock even
630 * without the tree itself locked. These unlocked entries
631 * need verification under the tree lock.
632 */
633 if (!__radix_tree_lookup(page_tree, index, &node, &slot))
634 goto unlock;
635 if (*slot != entry)
636 goto unlock;
637
638 /* another fsync thread may have already written back this entry */
639 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
640 goto unlock;
641
Ross Zwisler642261a2016-11-08 11:34:45 +1100642 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
643 dax_is_zero_entry(entry))) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800644 ret = -EIO;
645 goto unlock;
646 }
647
Ross Zwisler642261a2016-11-08 11:34:45 +1100648 /*
649 * Even if dax_writeback_mapping_range() was given a wbc->range_start
650 * in the middle of a PMD, the 'index' we are given will be aligned to
651 * the start index of the PMD, as will the sector we pull from
652 * 'entry'. This allows us to flush for PMD_SIZE and not have to
653 * worry about partial PMD writebacks.
654 */
655 dax.sector = dax_radix_sector(entry);
656 dax.size = PAGE_SIZE << dax_radix_order(entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800657 spin_unlock_irq(&mapping->tree_lock);
658
659 /*
660 * We cannot hold tree_lock while calling dax_map_atomic() because it
661 * eventually calls cond_resched().
662 */
663 ret = dax_map_atomic(bdev, &dax);
664 if (ret < 0)
665 return ret;
666
667 if (WARN_ON_ONCE(ret < dax.size)) {
668 ret = -EIO;
669 goto unmap;
670 }
671
672 wb_cache_pmem(dax.addr, dax.size);
673
674 spin_lock_irq(&mapping->tree_lock);
675 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
676 spin_unlock_irq(&mapping->tree_lock);
677 unmap:
678 dax_unmap_atomic(bdev, &dax);
679 return ret;
680
681 unlock:
682 spin_unlock_irq(&mapping->tree_lock);
683 return ret;
684}
685
686/*
687 * Flush the mapping to the persistent domain within the byte range of [start,
688 * end]. This is required by data integrity operations to ensure file data is
689 * on persistent storage prior to completion of the operation.
690 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800691int dax_writeback_mapping_range(struct address_space *mapping,
692 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800693{
694 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +1100695 pgoff_t start_index, end_index;
Ross Zwisler9973c982016-01-22 15:10:47 -0800696 pgoff_t indices[PAGEVEC_SIZE];
697 struct pagevec pvec;
698 bool done = false;
699 int i, ret = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800700
701 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
702 return -EIO;
703
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800704 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
705 return 0;
706
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300707 start_index = wbc->range_start >> PAGE_SHIFT;
708 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800709
710 tag_pages_for_writeback(mapping, start_index, end_index);
711
712 pagevec_init(&pvec, 0);
713 while (!done) {
714 pvec.nr = find_get_entries_tag(mapping, start_index,
715 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
716 pvec.pages, indices);
717
718 if (pvec.nr == 0)
719 break;
720
721 for (i = 0; i < pvec.nr; i++) {
722 if (indices[i] > end_index) {
723 done = true;
724 break;
725 }
726
727 ret = dax_writeback_one(bdev, mapping, indices[i],
728 pvec.pages[i]);
729 if (ret < 0)
730 return ret;
731 }
732 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800733 return 0;
734}
735EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
736
Jan Karaac401cc2016-05-12 18:29:18 +0200737static int dax_insert_mapping(struct address_space *mapping,
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000738 struct block_device *bdev, sector_t sector, size_t size,
739 void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800740{
Jan Kara1a29d852016-12-14 15:07:01 -0800741 unsigned long vaddr = vmf->address;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800742 struct blk_dax_ctl dax = {
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000743 .sector = sector,
744 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800745 };
Jan Karaac401cc2016-05-12 18:29:18 +0200746 void *ret;
747 void *entry = *entryp;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800748
Jan Kara4d9a2c82016-05-12 18:29:20 +0200749 if (dax_map_atomic(bdev, &dax) < 0)
750 return PTR_ERR(dax.addr);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800751 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800752
Ross Zwisler642261a2016-11-08 11:34:45 +1100753 ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector, 0);
Jan Kara4d9a2c82016-05-12 18:29:20 +0200754 if (IS_ERR(ret))
755 return PTR_ERR(ret);
Jan Karaac401cc2016-05-12 18:29:18 +0200756 *entryp = ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800757
Jan Kara4d9a2c82016-05-12 18:29:20 +0200758 return vm_insert_mixed(vma, vaddr, dax.pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800759}
760
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000761/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700762 * dax_pfn_mkwrite - handle first write to DAX page
763 * @vma: The virtual memory area where the fault occurred
764 * @vmf: The description of the fault
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700765 */
766int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
767{
Ross Zwisler9973c982016-01-22 15:10:47 -0800768 struct file *file = vma->vm_file;
Jan Karaac401cc2016-05-12 18:29:18 +0200769 struct address_space *mapping = file->f_mapping;
770 void *entry;
771 pgoff_t index = vmf->pgoff;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700772
Jan Karaac401cc2016-05-12 18:29:18 +0200773 spin_lock_irq(&mapping->tree_lock);
774 entry = get_unlocked_mapping_entry(mapping, index, NULL);
775 if (!entry || !radix_tree_exceptional_entry(entry))
776 goto out;
777 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
778 put_unlocked_mapping_entry(mapping, index, entry);
779out:
780 spin_unlock_irq(&mapping->tree_lock);
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700781 return VM_FAULT_NOPAGE;
782}
783EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
784
Vishal Verma4b0228f2016-04-21 15:13:46 -0400785static bool dax_range_is_aligned(struct block_device *bdev,
786 unsigned int offset, unsigned int length)
787{
788 unsigned short sector_size = bdev_logical_block_size(bdev);
789
790 if (!IS_ALIGNED(offset, sector_size))
791 return false;
792 if (!IS_ALIGNED(length, sector_size))
793 return false;
794
795 return true;
796}
797
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200798int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
799 unsigned int offset, unsigned int length)
800{
801 struct blk_dax_ctl dax = {
802 .sector = sector,
803 .size = PAGE_SIZE,
804 };
805
Vishal Verma4b0228f2016-04-21 15:13:46 -0400806 if (dax_range_is_aligned(bdev, offset, length)) {
807 sector_t start_sector = dax.sector + (offset >> 9);
808
809 return blkdev_issue_zeroout(bdev, start_sector,
810 length >> 9, GFP_NOFS, true);
811 } else {
812 if (dax_map_atomic(bdev, &dax) < 0)
813 return PTR_ERR(dax.addr);
814 clear_pmem(dax.addr + offset, length);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400815 dax_unmap_atomic(bdev, &dax);
816 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200817 return 0;
818}
819EXPORT_SYMBOL_GPL(__dax_zero_page_range);
820
Christoph Hellwiga254e562016-09-19 11:24:49 +1000821#ifdef CONFIG_FS_IOMAP
Ross Zwisler333ccc92016-11-08 11:33:09 +1100822static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
823{
824 return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
825}
826
Christoph Hellwiga254e562016-09-19 11:24:49 +1000827static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +1100828dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +1000829 struct iomap *iomap)
830{
831 struct iov_iter *iter = data;
832 loff_t end = pos + length, done = 0;
833 ssize_t ret = 0;
834
835 if (iov_iter_rw(iter) == READ) {
836 end = min(end, i_size_read(inode));
837 if (pos >= end)
838 return 0;
839
840 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
841 return iov_iter_zero(min(length, end - pos), iter);
842 }
843
844 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
845 return -EIO;
846
847 while (pos < end) {
848 unsigned offset = pos & (PAGE_SIZE - 1);
849 struct blk_dax_ctl dax = { 0 };
850 ssize_t map_len;
851
Ross Zwisler333ccc92016-11-08 11:33:09 +1100852 dax.sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000853 dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
854 map_len = dax_map_atomic(iomap->bdev, &dax);
855 if (map_len < 0) {
856 ret = map_len;
857 break;
858 }
859
860 dax.addr += offset;
861 map_len -= offset;
862 if (map_len > end - pos)
863 map_len = end - pos;
864
865 if (iov_iter_rw(iter) == WRITE)
866 map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
867 else
868 map_len = copy_to_iter(dax.addr, map_len, iter);
869 dax_unmap_atomic(iomap->bdev, &dax);
870 if (map_len <= 0) {
871 ret = map_len ? map_len : -EFAULT;
872 break;
873 }
874
875 pos += map_len;
876 length -= map_len;
877 done += map_len;
878 }
879
880 return done ? done : ret;
881}
882
883/**
Ross Zwisler11c59c92016-11-08 11:32:46 +1100884 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +1000885 * @iocb: The control block for this I/O
886 * @iter: The addresses to do I/O from or to
887 * @ops: iomap ops passed from the file system
888 *
889 * This function performs read and write operations to directly mapped
890 * persistent memory. The callers needs to take care of read/write exclusion
891 * and evicting any page cache pages in the region under I/O.
892 */
893ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +1100894dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwiga254e562016-09-19 11:24:49 +1000895 struct iomap_ops *ops)
896{
897 struct address_space *mapping = iocb->ki_filp->f_mapping;
898 struct inode *inode = mapping->host;
899 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
900 unsigned flags = 0;
901
902 if (iov_iter_rw(iter) == WRITE)
903 flags |= IOMAP_WRITE;
904
905 /*
906 * Yes, even DAX files can have page cache attached to them: A zeroed
907 * page is inserted into the pagecache when we have to serve a write
908 * fault on a hole. It should never be dirtied and can simply be
909 * dropped from the pagecache once we get real data for the page.
910 *
911 * XXX: This is racy against mmap, and there's nothing we can do about
912 * it. We'll eventually need to shift this down even further so that
913 * we can check if we allocated blocks over a hole first.
914 */
915 if (mapping->nrpages) {
916 ret = invalidate_inode_pages2_range(mapping,
917 pos >> PAGE_SHIFT,
918 (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
919 WARN_ON_ONCE(ret);
920 }
921
922 while (iov_iter_count(iter)) {
923 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +1100924 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000925 if (ret <= 0)
926 break;
927 pos += ret;
928 done += ret;
929 }
930
931 iocb->ki_pos += done;
932 return done ? done : ret;
933}
Ross Zwisler11c59c92016-11-08 11:32:46 +1100934EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000935
936/**
Ross Zwisler11c59c92016-11-08 11:32:46 +1100937 * dax_iomap_fault - handle a page fault on a DAX file
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000938 * @vma: The virtual memory area where the fault occurred
939 * @vmf: The description of the fault
940 * @ops: iomap ops passed from the file system
941 *
942 * When a page fault occurs, filesystems may call this helper in their fault
943 * or mkwrite handler for DAX files. Assumes the caller has done all the
944 * necessary locking for the page fault to proceed successfully.
945 */
Ross Zwisler11c59c92016-11-08 11:32:46 +1100946int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000947 struct iomap_ops *ops)
948{
949 struct address_space *mapping = vma->vm_file->f_mapping;
950 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -0800951 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000952 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
953 sector_t sector;
954 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +1100955 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000956 int error, major = 0;
Ross Zwisler15502902016-11-08 11:33:26 +1100957 int locked_status = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000958 void *entry;
959
960 /*
961 * Check whether offset isn't beyond end of file now. Caller is supposed
962 * to hold locks serializing us with truncate / punch hole so this is
963 * a reliable test.
964 */
965 if (pos >= i_size_read(inode))
966 return VM_FAULT_SIGBUS;
967
Ross Zwisler642261a2016-11-08 11:34:45 +1100968 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000969 if (IS_ERR(entry)) {
970 error = PTR_ERR(entry);
971 goto out;
972 }
973
974 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
975 flags |= IOMAP_WRITE;
976
977 /*
978 * Note that we don't bother to use iomap_apply here: DAX required
979 * the file system block size to be equal the page size, which means
980 * that we never have to deal with more than a single extent here.
981 */
982 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
983 if (error)
984 goto unlock_entry;
985 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
986 error = -EIO; /* fs corruption? */
Ross Zwisler15502902016-11-08 11:33:26 +1100987 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000988 }
989
Ross Zwisler333ccc92016-11-08 11:33:09 +1100990 sector = dax_iomap_sector(&iomap, pos);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +1000991
992 if (vmf->cow_page) {
993 switch (iomap.type) {
994 case IOMAP_HOLE:
995 case IOMAP_UNWRITTEN:
996 clear_user_highpage(vmf->cow_page, vaddr);
997 break;
998 case IOMAP_MAPPED:
999 error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
1000 vmf->cow_page, vaddr);
1001 break;
1002 default:
1003 WARN_ON_ONCE(1);
1004 error = -EIO;
1005 break;
1006 }
1007
1008 if (error)
Ross Zwisler15502902016-11-08 11:33:26 +11001009 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001010 if (!radix_tree_exceptional_entry(entry)) {
1011 vmf->page = entry;
Ross Zwisler15502902016-11-08 11:33:26 +11001012 locked_status = VM_FAULT_LOCKED;
1013 } else {
1014 vmf->entry = entry;
1015 locked_status = VM_FAULT_DAX_LOCKED;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001016 }
Ross Zwisler15502902016-11-08 11:33:26 +11001017 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001018 }
1019
1020 switch (iomap.type) {
1021 case IOMAP_MAPPED:
1022 if (iomap.flags & IOMAP_F_NEW) {
1023 count_vm_event(PGMAJFAULT);
1024 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1025 major = VM_FAULT_MAJOR;
1026 }
1027 error = dax_insert_mapping(mapping, iomap.bdev, sector,
1028 PAGE_SIZE, &entry, vma, vmf);
1029 break;
1030 case IOMAP_UNWRITTEN:
1031 case IOMAP_HOLE:
Ross Zwisler15502902016-11-08 11:33:26 +11001032 if (!(vmf->flags & FAULT_FLAG_WRITE)) {
1033 locked_status = dax_load_hole(mapping, entry, vmf);
1034 break;
1035 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001036 /*FALLTHRU*/
1037 default:
1038 WARN_ON_ONCE(1);
1039 error = -EIO;
1040 break;
1041 }
1042
Ross Zwisler15502902016-11-08 11:33:26 +11001043 finish_iomap:
1044 if (ops->iomap_end) {
1045 if (error) {
1046 /* keep previous error */
1047 ops->iomap_end(inode, pos, PAGE_SIZE, 0, flags,
1048 &iomap);
1049 } else {
1050 error = ops->iomap_end(inode, pos, PAGE_SIZE,
1051 PAGE_SIZE, flags, &iomap);
1052 }
1053 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001054 unlock_entry:
Ross Zwisler15502902016-11-08 11:33:26 +11001055 if (!locked_status || error)
1056 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001057 out:
1058 if (error == -ENOMEM)
1059 return VM_FAULT_OOM | major;
1060 /* -EBUSY is fine, somebody else faulted on the same PTE */
1061 if (error < 0 && error != -EBUSY)
1062 return VM_FAULT_SIGBUS | major;
Ross Zwisler15502902016-11-08 11:33:26 +11001063 if (locked_status) {
1064 WARN_ON_ONCE(error); /* -EBUSY from ops->iomap_end? */
1065 return locked_status;
1066 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001067 return VM_FAULT_NOPAGE | major;
1068}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001069EXPORT_SYMBOL_GPL(dax_iomap_fault);
Ross Zwisler642261a2016-11-08 11:34:45 +11001070
1071#ifdef CONFIG_FS_DAX_PMD
1072/*
1073 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1074 * more often than one might expect in the below functions.
1075 */
1076#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1077
1078static int dax_pmd_insert_mapping(struct vm_area_struct *vma, pmd_t *pmd,
1079 struct vm_fault *vmf, unsigned long address,
1080 struct iomap *iomap, loff_t pos, bool write, void **entryp)
1081{
1082 struct address_space *mapping = vma->vm_file->f_mapping;
1083 struct block_device *bdev = iomap->bdev;
1084 struct blk_dax_ctl dax = {
1085 .sector = dax_iomap_sector(iomap, pos),
1086 .size = PMD_SIZE,
1087 };
1088 long length = dax_map_atomic(bdev, &dax);
1089 void *ret;
1090
1091 if (length < 0) /* dax_map_atomic() failed */
1092 return VM_FAULT_FALLBACK;
1093 if (length < PMD_SIZE)
1094 goto unmap_fallback;
1095 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR)
1096 goto unmap_fallback;
1097 if (!pfn_t_devmap(dax.pfn))
1098 goto unmap_fallback;
1099
1100 dax_unmap_atomic(bdev, &dax);
1101
1102 ret = dax_insert_mapping_entry(mapping, vmf, *entryp, dax.sector,
1103 RADIX_DAX_PMD);
1104 if (IS_ERR(ret))
1105 return VM_FAULT_FALLBACK;
1106 *entryp = ret;
1107
1108 return vmf_insert_pfn_pmd(vma, address, pmd, dax.pfn, write);
1109
1110 unmap_fallback:
1111 dax_unmap_atomic(bdev, &dax);
1112 return VM_FAULT_FALLBACK;
1113}
1114
1115static int dax_pmd_load_hole(struct vm_area_struct *vma, pmd_t *pmd,
1116 struct vm_fault *vmf, unsigned long address,
1117 struct iomap *iomap, void **entryp)
1118{
1119 struct address_space *mapping = vma->vm_file->f_mapping;
1120 unsigned long pmd_addr = address & PMD_MASK;
1121 struct page *zero_page;
1122 spinlock_t *ptl;
1123 pmd_t pmd_entry;
1124 void *ret;
1125
1126 zero_page = mm_get_huge_zero_page(vma->vm_mm);
1127
1128 if (unlikely(!zero_page))
1129 return VM_FAULT_FALLBACK;
1130
1131 ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0,
1132 RADIX_DAX_PMD | RADIX_DAX_HZP);
1133 if (IS_ERR(ret))
1134 return VM_FAULT_FALLBACK;
1135 *entryp = ret;
1136
1137 ptl = pmd_lock(vma->vm_mm, pmd);
1138 if (!pmd_none(*pmd)) {
1139 spin_unlock(ptl);
1140 return VM_FAULT_FALLBACK;
1141 }
1142
1143 pmd_entry = mk_pmd(zero_page, vma->vm_page_prot);
1144 pmd_entry = pmd_mkhuge(pmd_entry);
1145 set_pmd_at(vma->vm_mm, pmd_addr, pmd, pmd_entry);
1146 spin_unlock(ptl);
1147 return VM_FAULT_NOPAGE;
1148}
1149
1150int dax_iomap_pmd_fault(struct vm_area_struct *vma, unsigned long address,
1151 pmd_t *pmd, unsigned int flags, struct iomap_ops *ops)
1152{
1153 struct address_space *mapping = vma->vm_file->f_mapping;
1154 unsigned long pmd_addr = address & PMD_MASK;
1155 bool write = flags & FAULT_FLAG_WRITE;
Jan Kara9484ab12016-11-10 10:26:50 +11001156 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001157 struct inode *inode = mapping->host;
1158 int result = VM_FAULT_FALLBACK;
1159 struct iomap iomap = { 0 };
1160 pgoff_t max_pgoff, pgoff;
1161 struct vm_fault vmf;
1162 void *entry;
1163 loff_t pos;
1164 int error;
1165
1166 /* Fall back to PTEs if we're going to COW */
1167 if (write && !(vma->vm_flags & VM_SHARED))
1168 goto fallback;
1169
1170 /* If the PMD would extend outside the VMA */
1171 if (pmd_addr < vma->vm_start)
1172 goto fallback;
1173 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1174 goto fallback;
1175
1176 /*
1177 * Check whether offset isn't beyond end of file now. Caller is
1178 * supposed to hold locks serializing us with truncate / punch hole so
1179 * this is a reliable test.
1180 */
1181 pgoff = linear_page_index(vma, pmd_addr);
1182 max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
1183
1184 if (pgoff > max_pgoff)
1185 return VM_FAULT_SIGBUS;
1186
1187 /* If the PMD would extend beyond the file size */
1188 if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
1189 goto fallback;
1190
1191 /*
1192 * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
1193 * PMD or a HZP entry. If it can't (because a 4k page is already in
1194 * the tree, for instance), it will return -EEXIST and we just fall
1195 * back to 4k entries.
1196 */
1197 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1198 if (IS_ERR(entry))
1199 goto fallback;
1200
1201 /*
1202 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1203 * setting up a mapping, so really we're using iomap_begin() as a way
1204 * to look up our filesystem block.
1205 */
1206 pos = (loff_t)pgoff << PAGE_SHIFT;
1207 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1208 if (error)
1209 goto unlock_entry;
1210 if (iomap.offset + iomap.length < pos + PMD_SIZE)
1211 goto finish_iomap;
1212
1213 vmf.pgoff = pgoff;
1214 vmf.flags = flags;
1215 vmf.gfp_mask = mapping_gfp_mask(mapping) | __GFP_IO;
1216
1217 switch (iomap.type) {
1218 case IOMAP_MAPPED:
1219 result = dax_pmd_insert_mapping(vma, pmd, &vmf, address,
1220 &iomap, pos, write, &entry);
1221 break;
1222 case IOMAP_UNWRITTEN:
1223 case IOMAP_HOLE:
1224 if (WARN_ON_ONCE(write))
1225 goto finish_iomap;
1226 result = dax_pmd_load_hole(vma, pmd, &vmf, address, &iomap,
1227 &entry);
1228 break;
1229 default:
1230 WARN_ON_ONCE(1);
1231 break;
1232 }
1233
1234 finish_iomap:
1235 if (ops->iomap_end) {
1236 if (result == VM_FAULT_FALLBACK) {
1237 ops->iomap_end(inode, pos, PMD_SIZE, 0, iomap_flags,
1238 &iomap);
1239 } else {
1240 error = ops->iomap_end(inode, pos, PMD_SIZE, PMD_SIZE,
1241 iomap_flags, &iomap);
1242 if (error)
1243 result = VM_FAULT_FALLBACK;
1244 }
1245 }
1246 unlock_entry:
1247 put_locked_mapping_entry(mapping, pgoff, entry);
1248 fallback:
1249 if (result == VM_FAULT_FALLBACK) {
1250 split_huge_pmd(vma, pmd, address);
1251 count_vm_event(THP_FAULT_FALLBACK);
1252 }
1253 return result;
1254}
1255EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault);
1256#endif /* CONFIG_FS_DAX_PMD */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001257#endif /* CONFIG_FS_IOMAP */