]> Git Repo - linux.git/blame - fs/dax.c
kasan: turn on -fsanitize-address-use-after-scope
[linux.git] / fs / dax.c
CommitLineData
d475c634
MW
1/*
2 * fs/dax.c - Direct Access filesystem code
3 * Copyright (c) 2013-2014 Intel Corporation
4 * Author: Matthew Wilcox <[email protected]>
5 * Author: Ross Zwisler <[email protected]>
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>
d77e92e2 20#include <linux/dax.h>
d475c634
MW
21#include <linux/fs.h>
22#include <linux/genhd.h>
f7ca90b1
MW
23#include <linux/highmem.h>
24#include <linux/memcontrol.h>
25#include <linux/mm.h>
d475c634 26#include <linux/mutex.h>
9973c98e 27#include <linux/pagevec.h>
2765cfbb 28#include <linux/pmem.h>
289c6aed 29#include <linux/sched.h>
d475c634 30#include <linux/uio.h>
f7ca90b1 31#include <linux/vmstat.h>
34c0fd54 32#include <linux/pfn_t.h>
0e749e54 33#include <linux/sizes.h>
a254e568
CH
34#include <linux/iomap.h>
35#include "internal.h"
d475c634 36
e804315d
JK
37/*
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)
78a9be0a
N
46#define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT))
47#define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \
e804315d
JK
48 RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE) | \
49 RADIX_TREE_EXCEPTIONAL_ENTRY))
e4b27491 50
ac401cc7
JK
51/* 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
55wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
56
57static int __init init_dax_wait_table(void)
58{
59 int i;
60
61 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
62 init_waitqueue_head(wait_table + i);
63 return 0;
64}
65fs_initcall(init_dax_wait_table);
66
67static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
68 pgoff_t index)
69{
70 unsigned long hash = hash_long((unsigned long)mapping ^ index,
71 DAX_WAIT_TABLE_BITS);
72 return wait_table + hash;
73}
78a9be0a 74
b2e0d162
DW
75static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
76{
77 struct request_queue *q = bdev->bd_queue;
78 long rc = -EIO;
79
7a9eb206 80 dax->addr = ERR_PTR(-EIO);
b2e0d162
DW
81 if (blk_queue_enter(q, true) != 0)
82 return rc;
83
84 rc = bdev_direct_access(bdev, dax);
85 if (rc < 0) {
7a9eb206 86 dax->addr = ERR_PTR(rc);
b2e0d162
DW
87 blk_queue_exit(q);
88 return rc;
89 }
90 return rc;
91}
92
93static void dax_unmap_atomic(struct block_device *bdev,
94 const struct blk_dax_ctl *dax)
95{
96 if (IS_ERR(dax->addr))
97 return;
98 blk_queue_exit(bdev->bd_queue);
99}
100
d1a5f2b4
DW
101struct page *read_dax_sector(struct block_device *bdev, sector_t n)
102{
103 struct page *page = alloc_pages(GFP_KERNEL, 0);
104 struct blk_dax_ctl dax = {
105 .size = PAGE_SIZE,
106 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
107 };
108 long rc;
109
110 if (!page)
111 return ERR_PTR(-ENOMEM);
112
113 rc = dax_map_atomic(bdev, &dax);
114 if (rc < 0)
115 return ERR_PTR(rc);
116 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
117 dax_unmap_atomic(bdev, &dax);
118 return page;
119}
120
d475c634
MW
121static bool buffer_written(struct buffer_head *bh)
122{
123 return buffer_mapped(bh) && !buffer_unwritten(bh);
124}
125
126/*
127 * When ext4 encounters a hole, it returns without modifying the buffer_head
128 * which means that we can't trust b_size. To cope with this, we set b_state
129 * to 0 before calling get_block and, if any bit is set, we know we can trust
130 * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
131 * and would save us time calling get_block repeatedly.
132 */
133static bool buffer_size_valid(struct buffer_head *bh)
134{
135 return bh->b_state != 0;
136}
137
b2e0d162
DW
138
139static sector_t to_sector(const struct buffer_head *bh,
140 const struct inode *inode)
141{
142 sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
143
144 return sector;
145}
146
a95cd631
OS
147static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
148 loff_t start, loff_t end, get_block_t get_block,
149 struct buffer_head *bh)
d475c634 150{
b2e0d162 151 loff_t pos = start, max = start, bh_max = start;
14df6a4e 152 bool hole = false;
b2e0d162
DW
153 struct block_device *bdev = NULL;
154 int rw = iov_iter_rw(iter), rc;
155 long map_len = 0;
156 struct blk_dax_ctl dax = {
7a9eb206 157 .addr = ERR_PTR(-EIO),
b2e0d162 158 };
069c77bc
JK
159 unsigned blkbits = inode->i_blkbits;
160 sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1)
161 >> blkbits;
b2e0d162
DW
162
163 if (rw == READ)
d475c634
MW
164 end = min(end, i_size_read(inode));
165
166 while (pos < end) {
2765cfbb 167 size_t len;
d475c634 168 if (pos == max) {
e94f5a22
JM
169 long page = pos >> PAGE_SHIFT;
170 sector_t block = page << (PAGE_SHIFT - blkbits);
d475c634
MW
171 unsigned first = pos - (block << blkbits);
172 long size;
173
174 if (pos == bh_max) {
175 bh->b_size = PAGE_ALIGN(end - pos);
176 bh->b_state = 0;
b2e0d162
DW
177 rc = get_block(inode, block, bh, rw == WRITE);
178 if (rc)
d475c634
MW
179 break;
180 if (!buffer_size_valid(bh))
181 bh->b_size = 1 << blkbits;
182 bh_max = pos - first + bh->b_size;
b2e0d162 183 bdev = bh->b_bdev;
069c77bc
JK
184 /*
185 * We allow uninitialized buffers for writes
186 * beyond EOF as those cannot race with faults
187 */
188 WARN_ON_ONCE(
189 (buffer_new(bh) && block < file_blks) ||
190 (rw == WRITE && buffer_unwritten(bh)));
d475c634
MW
191 } else {
192 unsigned done = bh->b_size -
193 (bh_max - (pos - first));
194 bh->b_blocknr += done >> blkbits;
195 bh->b_size -= done;
196 }
197
b2e0d162 198 hole = rw == READ && !buffer_written(bh);
d475c634 199 if (hole) {
d475c634
MW
200 size = bh->b_size - first;
201 } else {
b2e0d162
DW
202 dax_unmap_atomic(bdev, &dax);
203 dax.sector = to_sector(bh, inode);
204 dax.size = bh->b_size;
205 map_len = dax_map_atomic(bdev, &dax);
206 if (map_len < 0) {
207 rc = map_len;
d475c634 208 break;
b2e0d162 209 }
b2e0d162
DW
210 dax.addr += first;
211 size = map_len - first;
d475c634 212 }
02395435
ES
213 /*
214 * pos + size is one past the last offset for IO,
215 * so pos + size can overflow loff_t at extreme offsets.
216 * Cast to u64 to catch this and get the true minimum.
217 */
218 max = min_t(u64, pos + size, end);
d475c634
MW
219 }
220
2765cfbb 221 if (iov_iter_rw(iter) == WRITE) {
b2e0d162 222 len = copy_from_iter_pmem(dax.addr, max - pos, iter);
2765cfbb 223 } else if (!hole)
b2e0d162 224 len = copy_to_iter((void __force *) dax.addr, max - pos,
e2e05394 225 iter);
d475c634
MW
226 else
227 len = iov_iter_zero(max - pos, iter);
228
cadfbb6e 229 if (!len) {
b2e0d162 230 rc = -EFAULT;
d475c634 231 break;
cadfbb6e 232 }
d475c634
MW
233
234 pos += len;
b2e0d162
DW
235 if (!IS_ERR(dax.addr))
236 dax.addr += len;
d475c634
MW
237 }
238
b2e0d162 239 dax_unmap_atomic(bdev, &dax);
2765cfbb 240
b2e0d162 241 return (pos == start) ? rc : pos - start;
d475c634
MW
242}
243
244/**
245 * dax_do_io - Perform I/O to a DAX file
d475c634
MW
246 * @iocb: The control block for this I/O
247 * @inode: The file which the I/O is directed at
248 * @iter: The addresses to do I/O from or to
d475c634
MW
249 * @get_block: The filesystem method used to translate file offsets to blocks
250 * @end_io: A filesystem callback for I/O completion
251 * @flags: See below
252 *
253 * This function uses the same locking scheme as do_blockdev_direct_IO:
254 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
255 * caller for writes. For reads, we take and release the i_mutex ourselves.
256 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
257 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
258 * is in progress.
259 */
a95cd631 260ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
c8b8e32d 261 struct iov_iter *iter, get_block_t get_block,
a95cd631 262 dio_iodone_t end_io, int flags)
d475c634
MW
263{
264 struct buffer_head bh;
265 ssize_t retval = -EINVAL;
c8b8e32d 266 loff_t pos = iocb->ki_pos;
d475c634
MW
267 loff_t end = pos + iov_iter_count(iter);
268
269 memset(&bh, 0, sizeof(bh));
eab95db6 270 bh.b_bdev = inode->i_sb->s_bdev;
d475c634 271
c3d98e39 272 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
5955102c 273 inode_lock(inode);
d475c634
MW
274
275 /* Protects against truncate */
bbab37dd
MW
276 if (!(flags & DIO_SKIP_DIO_COUNT))
277 inode_dio_begin(inode);
d475c634 278
a95cd631 279 retval = dax_io(inode, iter, pos, end, get_block, &bh);
d475c634 280
a95cd631 281 if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
5955102c 282 inode_unlock(inode);
d475c634 283
187372a3
CH
284 if (end_io) {
285 int err;
286
287 err = end_io(iocb, pos, retval, bh.b_private);
288 if (err)
289 retval = err;
290 }
d475c634 291
bbab37dd
MW
292 if (!(flags & DIO_SKIP_DIO_COUNT))
293 inode_dio_end(inode);
d475c634
MW
294 return retval;
295}
296EXPORT_SYMBOL_GPL(dax_do_io);
f7ca90b1 297
ac401cc7
JK
298/*
299 * DAX radix tree locking
300 */
301struct exceptional_entry_key {
302 struct address_space *mapping;
303 unsigned long index;
304};
305
306struct wait_exceptional_entry_queue {
307 wait_queue_t wait;
308 struct exceptional_entry_key key;
309};
310
311static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
312 int sync, void *keyp)
313{
314 struct exceptional_entry_key *key = keyp;
315 struct wait_exceptional_entry_queue *ewait =
316 container_of(wait, struct wait_exceptional_entry_queue, wait);
317
318 if (key->mapping != ewait->key.mapping ||
319 key->index != ewait->key.index)
320 return 0;
321 return autoremove_wake_function(wait, mode, sync, NULL);
322}
323
324/*
325 * Check whether the given slot is locked. The function must be called with
326 * mapping->tree_lock held
327 */
328static inline int slot_locked(struct address_space *mapping, void **slot)
329{
330 unsigned long entry = (unsigned long)
331 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
332 return entry & RADIX_DAX_ENTRY_LOCK;
333}
334
335/*
336 * Mark the given slot is locked. The function must be called with
337 * mapping->tree_lock held
338 */
339static inline void *lock_slot(struct address_space *mapping, void **slot)
340{
341 unsigned long entry = (unsigned long)
342 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
343
344 entry |= RADIX_DAX_ENTRY_LOCK;
6d75f366 345 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
ac401cc7
JK
346 return (void *)entry;
347}
348
349/*
350 * Mark the given slot is unlocked. The function must be called with
351 * mapping->tree_lock held
352 */
353static inline void *unlock_slot(struct address_space *mapping, void **slot)
354{
355 unsigned long entry = (unsigned long)
356 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
357
358 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
6d75f366 359 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
ac401cc7
JK
360 return (void *)entry;
361}
362
363/*
364 * Lookup entry in radix tree, wait for it to become unlocked if it is
365 * exceptional entry and return it. The caller must call
366 * put_unlocked_mapping_entry() when he decided not to lock the entry or
367 * put_locked_mapping_entry() when he locked the entry and now wants to
368 * unlock it.
369 *
370 * The function must be called with mapping->tree_lock held.
371 */
372static void *get_unlocked_mapping_entry(struct address_space *mapping,
373 pgoff_t index, void ***slotp)
374{
375 void *ret, **slot;
376 struct wait_exceptional_entry_queue ewait;
377 wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index);
378
379 init_wait(&ewait.wait);
380 ewait.wait.func = wake_exceptional_entry_func;
381 ewait.key.mapping = mapping;
382 ewait.key.index = index;
383
384 for (;;) {
385 ret = __radix_tree_lookup(&mapping->page_tree, index, NULL,
386 &slot);
387 if (!ret || !radix_tree_exceptional_entry(ret) ||
388 !slot_locked(mapping, slot)) {
389 if (slotp)
390 *slotp = slot;
391 return ret;
392 }
393 prepare_to_wait_exclusive(wq, &ewait.wait,
394 TASK_UNINTERRUPTIBLE);
395 spin_unlock_irq(&mapping->tree_lock);
396 schedule();
397 finish_wait(wq, &ewait.wait);
398 spin_lock_irq(&mapping->tree_lock);
399 }
400}
401
402/*
403 * Find radix tree entry at given index. If it points to a page, return with
404 * the page locked. If it points to the exceptional entry, return with the
405 * radix tree entry locked. If the radix tree doesn't contain given index,
406 * create empty exceptional entry for the index and return with it locked.
407 *
408 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
409 * persistent memory the benefit is doubtful. We can add that later if we can
410 * show it helps.
411 */
412static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index)
413{
414 void *ret, **slot;
415
416restart:
417 spin_lock_irq(&mapping->tree_lock);
418 ret = get_unlocked_mapping_entry(mapping, index, &slot);
419 /* No entry for given index? Make sure radix tree is big enough. */
420 if (!ret) {
421 int err;
422
423 spin_unlock_irq(&mapping->tree_lock);
424 err = radix_tree_preload(
425 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
426 if (err)
427 return ERR_PTR(err);
428 ret = (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY |
429 RADIX_DAX_ENTRY_LOCK);
430 spin_lock_irq(&mapping->tree_lock);
431 err = radix_tree_insert(&mapping->page_tree, index, ret);
432 radix_tree_preload_end();
433 if (err) {
434 spin_unlock_irq(&mapping->tree_lock);
435 /* Someone already created the entry? */
436 if (err == -EEXIST)
437 goto restart;
438 return ERR_PTR(err);
439 }
440 /* Good, we have inserted empty locked entry into the tree. */
441 mapping->nrexceptional++;
442 spin_unlock_irq(&mapping->tree_lock);
443 return ret;
444 }
445 /* Normal page in radix tree? */
446 if (!radix_tree_exceptional_entry(ret)) {
447 struct page *page = ret;
448
449 get_page(page);
450 spin_unlock_irq(&mapping->tree_lock);
451 lock_page(page);
452 /* Page got truncated? Retry... */
453 if (unlikely(page->mapping != mapping)) {
454 unlock_page(page);
455 put_page(page);
456 goto restart;
457 }
458 return page;
459 }
460 ret = lock_slot(mapping, slot);
461 spin_unlock_irq(&mapping->tree_lock);
462 return ret;
463}
464
465void dax_wake_mapping_entry_waiter(struct address_space *mapping,
466 pgoff_t index, bool wake_all)
467{
468 wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index);
469
470 /*
471 * Checking for locked entry and prepare_to_wait_exclusive() happens
472 * under mapping->tree_lock, ditto for entry handling in our callers.
473 * So at this point all tasks that could have seen our entry locked
474 * must be in the waitqueue and the following check will see them.
475 */
476 if (waitqueue_active(wq)) {
477 struct exceptional_entry_key key;
478
479 key.mapping = mapping;
480 key.index = index;
481 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
482 }
483}
484
bc2466e4 485void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
ac401cc7
JK
486{
487 void *ret, **slot;
488
489 spin_lock_irq(&mapping->tree_lock);
490 ret = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
491 if (WARN_ON_ONCE(!ret || !radix_tree_exceptional_entry(ret) ||
492 !slot_locked(mapping, slot))) {
493 spin_unlock_irq(&mapping->tree_lock);
494 return;
495 }
496 unlock_slot(mapping, slot);
497 spin_unlock_irq(&mapping->tree_lock);
498 dax_wake_mapping_entry_waiter(mapping, index, false);
499}
500
501static void put_locked_mapping_entry(struct address_space *mapping,
502 pgoff_t index, void *entry)
503{
504 if (!radix_tree_exceptional_entry(entry)) {
505 unlock_page(entry);
506 put_page(entry);
507 } else {
bc2466e4 508 dax_unlock_mapping_entry(mapping, index);
ac401cc7
JK
509 }
510}
511
512/*
513 * Called when we are done with radix tree entry we looked up via
514 * get_unlocked_mapping_entry() and which we didn't lock in the end.
515 */
516static void put_unlocked_mapping_entry(struct address_space *mapping,
517 pgoff_t index, void *entry)
518{
519 if (!radix_tree_exceptional_entry(entry))
520 return;
521
522 /* We have to wake up next waiter for the radix tree entry lock */
523 dax_wake_mapping_entry_waiter(mapping, index, false);
524}
525
526/*
527 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
528 * entry to get unlocked before deleting it.
529 */
530int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
531{
532 void *entry;
533
534 spin_lock_irq(&mapping->tree_lock);
535 entry = get_unlocked_mapping_entry(mapping, index, NULL);
536 /*
537 * This gets called from truncate / punch_hole path. As such, the caller
538 * must hold locks protecting against concurrent modifications of the
539 * radix tree (usually fs-private i_mmap_sem for writing). Since the
540 * caller has seen exceptional entry for this index, we better find it
541 * at that index as well...
542 */
543 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
544 spin_unlock_irq(&mapping->tree_lock);
545 return 0;
546 }
547 radix_tree_delete(&mapping->page_tree, index);
548 mapping->nrexceptional--;
549 spin_unlock_irq(&mapping->tree_lock);
550 dax_wake_mapping_entry_waiter(mapping, index, true);
551
552 return 1;
553}
554
f7ca90b1
MW
555/*
556 * The user has performed a load from a hole in the file. Allocating
557 * a new page in the file would cause excessive storage usage for
558 * workloads with sparse files. We allocate a page cache page instead.
559 * We'll kick it out of the page cache if it's ever written to,
560 * otherwise it will simply fall out of the page cache under memory
561 * pressure without ever having been dirtied.
562 */
ac401cc7
JK
563static int dax_load_hole(struct address_space *mapping, void *entry,
564 struct vm_fault *vmf)
f7ca90b1 565{
ac401cc7 566 struct page *page;
f7ca90b1 567
ac401cc7
JK
568 /* Hole page already exists? Return it... */
569 if (!radix_tree_exceptional_entry(entry)) {
570 vmf->page = entry;
571 return VM_FAULT_LOCKED;
572 }
f7ca90b1 573
ac401cc7
JK
574 /* This will replace locked radix tree entry with a hole page */
575 page = find_or_create_page(mapping, vmf->pgoff,
576 vmf->gfp_mask | __GFP_ZERO);
577 if (!page) {
578 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
579 return VM_FAULT_OOM;
580 }
f7ca90b1
MW
581 vmf->page = page;
582 return VM_FAULT_LOCKED;
583}
584
b0d5e82f
CH
585static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
586 struct page *to, unsigned long vaddr)
f7ca90b1 587{
b2e0d162 588 struct blk_dax_ctl dax = {
b0d5e82f
CH
589 .sector = sector,
590 .size = size,
b2e0d162 591 };
e2e05394
RZ
592 void *vto;
593
b2e0d162
DW
594 if (dax_map_atomic(bdev, &dax) < 0)
595 return PTR_ERR(dax.addr);
f7ca90b1 596 vto = kmap_atomic(to);
b2e0d162 597 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
f7ca90b1 598 kunmap_atomic(vto);
b2e0d162 599 dax_unmap_atomic(bdev, &dax);
f7ca90b1
MW
600 return 0;
601}
602
09cbfeaf 603#define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
9973c98e 604
ac401cc7
JK
605static void *dax_insert_mapping_entry(struct address_space *mapping,
606 struct vm_fault *vmf,
607 void *entry, sector_t sector)
9973c98e
RZ
608{
609 struct radix_tree_root *page_tree = &mapping->page_tree;
ac401cc7
JK
610 int error = 0;
611 bool hole_fill = false;
612 void *new_entry;
613 pgoff_t index = vmf->pgoff;
9973c98e 614
ac401cc7 615 if (vmf->flags & FAULT_FLAG_WRITE)
d2b2a28e 616 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
9973c98e 617
ac401cc7
JK
618 /* Replacing hole page with block mapping? */
619 if (!radix_tree_exceptional_entry(entry)) {
620 hole_fill = true;
621 /*
622 * Unmap the page now before we remove it from page cache below.
623 * The page is locked so it cannot be faulted in again.
624 */
625 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
626 PAGE_SIZE, 0);
627 error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
628 if (error)
629 return ERR_PTR(error);
9973c98e
RZ
630 }
631
ac401cc7
JK
632 spin_lock_irq(&mapping->tree_lock);
633 new_entry = (void *)((unsigned long)RADIX_DAX_ENTRY(sector, false) |
634 RADIX_DAX_ENTRY_LOCK);
635 if (hole_fill) {
636 __delete_from_page_cache(entry, NULL);
637 /* Drop pagecache reference */
638 put_page(entry);
639 error = radix_tree_insert(page_tree, index, new_entry);
640 if (error) {
641 new_entry = ERR_PTR(error);
9973c98e
RZ
642 goto unlock;
643 }
ac401cc7
JK
644 mapping->nrexceptional++;
645 } else {
f7942430 646 struct radix_tree_node *node;
ac401cc7
JK
647 void **slot;
648 void *ret;
9973c98e 649
f7942430 650 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
ac401cc7 651 WARN_ON_ONCE(ret != entry);
4d693d08
JW
652 __radix_tree_replace(page_tree, node, slot,
653 new_entry, NULL, NULL);
9973c98e 654 }
ac401cc7 655 if (vmf->flags & FAULT_FLAG_WRITE)
9973c98e
RZ
656 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
657 unlock:
658 spin_unlock_irq(&mapping->tree_lock);
ac401cc7
JK
659 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;
9973c98e
RZ
671}
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 */
7f6d5b52
RZ
739int dax_writeback_mapping_range(struct address_space *mapping,
740 struct block_device *bdev, struct writeback_control *wbc)
9973c98e
RZ
741{
742 struct inode *inode = mapping->host;
9973c98e
RZ
743 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
7f6d5b52
RZ
753 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
754 return 0;
755
09cbfeaf
KS
756 start_index = wbc->range_start >> PAGE_SHIFT;
757 end_index = wbc->range_end >> PAGE_SHIFT;
9973c98e
RZ
758 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 }
9973c98e
RZ
791 return 0;
792}
793EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
794
ac401cc7 795static int dax_insert_mapping(struct address_space *mapping,
1aaba095
CH
796 struct block_device *bdev, sector_t sector, size_t size,
797 void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
f7ca90b1 798{
f7ca90b1 799 unsigned long vaddr = (unsigned long)vmf->virtual_address;
b2e0d162 800 struct blk_dax_ctl dax = {
1aaba095
CH
801 .sector = sector,
802 .size = size,
b2e0d162 803 };
ac401cc7
JK
804 void *ret;
805 void *entry = *entryp;
f7ca90b1 806
4d9a2c87
JK
807 if (dax_map_atomic(bdev, &dax) < 0)
808 return PTR_ERR(dax.addr);
b2e0d162 809 dax_unmap_atomic(bdev, &dax);
f7ca90b1 810
ac401cc7 811 ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector);
4d9a2c87
JK
812 if (IS_ERR(ret))
813 return PTR_ERR(ret);
ac401cc7 814 *entryp = ret;
9973c98e 815
4d9a2c87 816 return vm_insert_mixed(vma, vaddr, dax.pfn);
f7ca90b1
MW
817}
818
ce5c5d55 819/**
6b524995 820 * dax_fault - handle a page fault on a DAX file
ce5c5d55
DC
821 * @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
6b524995 826 * fault handler for DAX files. dax_fault() assumes the caller has done all
ce5c5d55
DC
827 * the necessary locking for the page fault to proceed successfully.
828 */
6b524995 829int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
02fbd139 830 get_block_t get_block)
f7ca90b1
MW
831{
832 struct file *file = vma->vm_file;
833 struct address_space *mapping = file->f_mapping;
834 struct inode *inode = mapping->host;
ac401cc7 835 void *entry;
f7ca90b1
MW
836 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
ac401cc7
JK
844 /*
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 */
f7ca90b1
MW
849 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);
eab95db6 855 bh.b_bdev = inode->i_sb->s_bdev;
f7ca90b1
MW
856 bh.b_size = PAGE_SIZE;
857
ac401cc7
JK
858 entry = grab_mapping_entry(mapping, vmf->pgoff);
859 if (IS_ERR(entry)) {
860 error = PTR_ERR(entry);
861 goto out;
f7ca90b1
MW
862 }
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)
ac401cc7 868 goto unlock_entry;
f7ca90b1
MW
869
870 if (vmf->cow_page) {
871 struct page *new_page = vmf->cow_page;
872 if (buffer_written(&bh))
b0d5e82f
CH
873 error = copy_user_dax(bh.b_bdev, to_sector(&bh, inode),
874 bh.b_size, new_page, vaddr);
f7ca90b1
MW
875 else
876 clear_user_highpage(new_page, vaddr);
877 if (error)
ac401cc7
JK
878 goto unlock_entry;
879 if (!radix_tree_exceptional_entry(entry)) {
880 vmf->page = entry;
bc2466e4 881 return VM_FAULT_LOCKED;
ac401cc7 882 }
bc2466e4
JK
883 vmf->entry = entry;
884 return VM_FAULT_DAX_LOCKED;
f7ca90b1 885 }
f7ca90b1 886
ac401cc7 887 if (!buffer_mapped(&bh)) {
f7ca90b1
MW
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)
ac401cc7 896 goto unlock_entry;
f7ca90b1 897 } else {
ac401cc7 898 return dax_load_hole(mapping, entry, vmf);
f7ca90b1
MW
899 }
900 }
901
02fbd139 902 /* Filesystem should not return unwritten buffers to us! */
2b10945c 903 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
1aaba095
CH
904 error = dax_insert_mapping(mapping, bh.b_bdev, to_sector(&bh, inode),
905 bh.b_size, &entry, vma, vmf);
ac401cc7
JK
906 unlock_entry:
907 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
f7ca90b1
MW
908 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;
f7ca90b1 915}
f7ca90b1 916EXPORT_SYMBOL_GPL(dax_fault);
4c0ccfef 917
348e967a 918#if defined(CONFIG_TRANSPARENT_HUGEPAGE)
844f35db
MW
919/*
920 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
921 * more often than one might expect in the below function.
922 */
923#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
924
cbb38e41
DW
925static void __dax_dbg(struct buffer_head *bh, unsigned long address,
926 const char *reason, const char *fn)
927{
928 if (bh) {
929 char bname[BDEVNAME_SIZE];
930 bdevname(bh->b_bdev, bname);
931 pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
932 "length %zd fallback: %s\n", fn, current->comm,
933 address, bname, bh->b_state, (u64)bh->b_blocknr,
934 bh->b_size, reason);
935 } else {
936 pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
937 current->comm, address, reason);
938 }
939}
940
941#define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
942
6b524995
RZ
943/**
944 * dax_pmd_fault - handle a PMD fault on a DAX file
945 * @vma: The virtual memory area where the fault occurred
946 * @vmf: The description of the fault
947 * @get_block: The filesystem method used to translate file offsets to blocks
948 *
949 * When a page fault occurs, filesystems may call this helper in their
950 * pmd_fault handler for DAX files.
951 */
952int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
02fbd139 953 pmd_t *pmd, unsigned int flags, get_block_t get_block)
844f35db
MW
954{
955 struct file *file = vma->vm_file;
956 struct address_space *mapping = file->f_mapping;
957 struct inode *inode = mapping->host;
958 struct buffer_head bh;
959 unsigned blkbits = inode->i_blkbits;
960 unsigned long pmd_addr = address & PMD_MASK;
961 bool write = flags & FAULT_FLAG_WRITE;
b2e0d162 962 struct block_device *bdev;
844f35db 963 pgoff_t size, pgoff;
b2e0d162 964 sector_t block;
ac401cc7 965 int result = 0;
9973c98e 966 bool alloc = false;
844f35db 967
c046c321 968 /* dax pmd mappings require pfn_t_devmap() */
ee82c9ed
DW
969 if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
970 return VM_FAULT_FALLBACK;
971
844f35db 972 /* Fall back to PTEs if we're going to COW */
59bf4fb9
TK
973 if (write && !(vma->vm_flags & VM_SHARED)) {
974 split_huge_pmd(vma, pmd, address);
cbb38e41 975 dax_pmd_dbg(NULL, address, "cow write");
844f35db 976 return VM_FAULT_FALLBACK;
59bf4fb9 977 }
844f35db 978 /* If the PMD would extend outside the VMA */
cbb38e41
DW
979 if (pmd_addr < vma->vm_start) {
980 dax_pmd_dbg(NULL, address, "vma start unaligned");
844f35db 981 return VM_FAULT_FALLBACK;
cbb38e41
DW
982 }
983 if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
984 dax_pmd_dbg(NULL, address, "vma end unaligned");
844f35db 985 return VM_FAULT_FALLBACK;
cbb38e41 986 }
844f35db 987
3fdd1b47 988 pgoff = linear_page_index(vma, pmd_addr);
844f35db
MW
989 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
990 if (pgoff >= size)
991 return VM_FAULT_SIGBUS;
992 /* If the PMD would cover blocks out of the file */
cbb38e41
DW
993 if ((pgoff | PG_PMD_COLOUR) >= size) {
994 dax_pmd_dbg(NULL, address,
995 "offset + huge page size > file size");
844f35db 996 return VM_FAULT_FALLBACK;
cbb38e41 997 }
844f35db
MW
998
999 memset(&bh, 0, sizeof(bh));
d4bbe706 1000 bh.b_bdev = inode->i_sb->s_bdev;
844f35db
MW
1001 block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
1002
1003 bh.b_size = PMD_SIZE;
9973c98e
RZ
1004
1005 if (get_block(inode, block, &bh, 0) != 0)
844f35db 1006 return VM_FAULT_SIGBUS;
9973c98e
RZ
1007
1008 if (!buffer_mapped(&bh) && write) {
1009 if (get_block(inode, block, &bh, 1) != 0)
1010 return VM_FAULT_SIGBUS;
1011 alloc = true;
2b10945c 1012 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
9973c98e
RZ
1013 }
1014
b2e0d162 1015 bdev = bh.b_bdev;
844f35db
MW
1016
1017 /*
1018 * If the filesystem isn't willing to tell us the length of a hole,
1019 * just fall back to PTEs. Calling get_block 512 times in a loop
1020 * would be silly.
1021 */
cbb38e41
DW
1022 if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
1023 dax_pmd_dbg(&bh, address, "allocated block too small");
9973c98e
RZ
1024 return VM_FAULT_FALLBACK;
1025 }
1026
1027 /*
1028 * If we allocated new storage, make sure no process has any
1029 * zero pages covering this hole
1030 */
1031 if (alloc) {
1032 loff_t lstart = pgoff << PAGE_SHIFT;
1033 loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
1034
1035 truncate_pagecache_range(inode, lstart, lend);
cbb38e41 1036 }
844f35db 1037
b9953536 1038 if (!write && !buffer_mapped(&bh)) {
844f35db 1039 spinlock_t *ptl;
d295e341 1040 pmd_t entry;
6fcb52a5 1041 struct page *zero_page = mm_get_huge_zero_page(vma->vm_mm);
d295e341 1042
cbb38e41
DW
1043 if (unlikely(!zero_page)) {
1044 dax_pmd_dbg(&bh, address, "no zero page");
844f35db 1045 goto fallback;
cbb38e41 1046 }
844f35db 1047
d295e341
KS
1048 ptl = pmd_lock(vma->vm_mm, pmd);
1049 if (!pmd_none(*pmd)) {
1050 spin_unlock(ptl);
cbb38e41 1051 dax_pmd_dbg(&bh, address, "pmd already present");
d295e341
KS
1052 goto fallback;
1053 }
1054
cbb38e41
DW
1055 dev_dbg(part_to_dev(bdev->bd_part),
1056 "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
1057 __func__, current->comm, address,
1058 (unsigned long long) to_sector(&bh, inode));
1059
d295e341
KS
1060 entry = mk_pmd(zero_page, vma->vm_page_prot);
1061 entry = pmd_mkhuge(entry);
1062 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
844f35db 1063 result = VM_FAULT_NOPAGE;
d295e341 1064 spin_unlock(ptl);
844f35db 1065 } else {
b2e0d162
DW
1066 struct blk_dax_ctl dax = {
1067 .sector = to_sector(&bh, inode),
1068 .size = PMD_SIZE,
1069 };
1070 long length = dax_map_atomic(bdev, &dax);
1071
844f35db 1072 if (length < 0) {
8b3db979
DW
1073 dax_pmd_dbg(&bh, address, "dax-error fallback");
1074 goto fallback;
844f35db 1075 }
cbb38e41
DW
1076 if (length < PMD_SIZE) {
1077 dax_pmd_dbg(&bh, address, "dax-length too small");
1078 dax_unmap_atomic(bdev, &dax);
1079 goto fallback;
1080 }
1081 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
1082 dax_pmd_dbg(&bh, address, "pfn unaligned");
b2e0d162 1083 dax_unmap_atomic(bdev, &dax);
844f35db 1084 goto fallback;
b2e0d162 1085 }
844f35db 1086
c046c321 1087 if (!pfn_t_devmap(dax.pfn)) {
b2e0d162 1088 dax_unmap_atomic(bdev, &dax);
cbb38e41 1089 dax_pmd_dbg(&bh, address, "pfn not in memmap");
152d7bd8 1090 goto fallback;
b2e0d162 1091 }
b2e0d162 1092 dax_unmap_atomic(bdev, &dax);
0f90cc66 1093
9973c98e
RZ
1094 /*
1095 * For PTE faults we insert a radix tree entry for reads, and
1096 * leave it clean. Then on the first write we dirty the radix
1097 * tree entry via the dax_pfn_mkwrite() path. This sequence
1098 * allows the dax_pfn_mkwrite() call to be simpler and avoid a
1099 * call into get_block() to translate the pgoff to a sector in
1100 * order to be able to create a new radix tree entry.
1101 *
1102 * The PMD path doesn't have an equivalent to
1103 * dax_pfn_mkwrite(), though, so for a read followed by a
6b524995 1104 * write we traverse all the way through dax_pmd_fault()
9973c98e
RZ
1105 * twice. This means we can just skip inserting a radix tree
1106 * entry completely on the initial read and just wait until
1107 * the write to insert a dirty entry.
1108 */
1109 if (write) {
ac401cc7
JK
1110 /*
1111 * We should insert radix-tree entry and dirty it here.
1112 * For now this is broken...
1113 */
9973c98e
RZ
1114 }
1115
cbb38e41
DW
1116 dev_dbg(part_to_dev(bdev->bd_part),
1117 "%s: %s addr: %lx pfn: %lx sect: %llx\n",
1118 __func__, current->comm, address,
1119 pfn_t_to_pfn(dax.pfn),
1120 (unsigned long long) dax.sector);
34c0fd54 1121 result |= vmf_insert_pfn_pmd(vma, address, pmd,
f25748e3 1122 dax.pfn, write);
844f35db
MW
1123 }
1124
1125 out:
844f35db
MW
1126 return result;
1127
1128 fallback:
1129 count_vm_event(THP_FAULT_FALLBACK);
1130 result = VM_FAULT_FALLBACK;
1131 goto out;
1132}
844f35db 1133EXPORT_SYMBOL_GPL(dax_pmd_fault);
dd8a2b6c 1134#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
844f35db 1135
0e3b210c
BH
1136/**
1137 * dax_pfn_mkwrite - handle first write to DAX page
1138 * @vma: The virtual memory area where the fault occurred
1139 * @vmf: The description of the fault
0e3b210c
BH
1140 */
1141int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1142{
9973c98e 1143 struct file *file = vma->vm_file;
ac401cc7
JK
1144 struct address_space *mapping = file->f_mapping;
1145 void *entry;
1146 pgoff_t index = vmf->pgoff;
30f471fd 1147
ac401cc7
JK
1148 spin_lock_irq(&mapping->tree_lock);
1149 entry = get_unlocked_mapping_entry(mapping, index, NULL);
1150 if (!entry || !radix_tree_exceptional_entry(entry))
1151 goto out;
1152 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
1153 put_unlocked_mapping_entry(mapping, index, entry);
1154out:
1155 spin_unlock_irq(&mapping->tree_lock);
0e3b210c
BH
1156 return VM_FAULT_NOPAGE;
1157}
1158EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
1159
4b0228fa
VV
1160static bool dax_range_is_aligned(struct block_device *bdev,
1161 unsigned int offset, unsigned int length)
1162{
1163 unsigned short sector_size = bdev_logical_block_size(bdev);
1164
1165 if (!IS_ALIGNED(offset, sector_size))
1166 return false;
1167 if (!IS_ALIGNED(length, sector_size))
1168 return false;
1169
1170 return true;
1171}
1172
679c8bd3
CH
1173int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
1174 unsigned int offset, unsigned int length)
1175{
1176 struct blk_dax_ctl dax = {
1177 .sector = sector,
1178 .size = PAGE_SIZE,
1179 };
1180
4b0228fa
VV
1181 if (dax_range_is_aligned(bdev, offset, length)) {
1182 sector_t start_sector = dax.sector + (offset >> 9);
1183
1184 return blkdev_issue_zeroout(bdev, start_sector,
1185 length >> 9, GFP_NOFS, true);
1186 } else {
1187 if (dax_map_atomic(bdev, &dax) < 0)
1188 return PTR_ERR(dax.addr);
1189 clear_pmem(dax.addr + offset, length);
4b0228fa
VV
1190 dax_unmap_atomic(bdev, &dax);
1191 }
679c8bd3
CH
1192 return 0;
1193}
1194EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1195
4c0ccfef 1196/**
25726bc1 1197 * dax_zero_page_range - zero a range within a page of a DAX file
4c0ccfef
MW
1198 * @inode: The file being truncated
1199 * @from: The file offset that is being truncated to
25726bc1 1200 * @length: The number of bytes to zero
4c0ccfef
MW
1201 * @get_block: The filesystem method used to translate file offsets to blocks
1202 *
25726bc1
MW
1203 * This function can be called by a filesystem when it is zeroing part of a
1204 * page in a DAX file. This is intended for hole-punch operations. If
1205 * you are truncating a file, the helper function dax_truncate_page() may be
1206 * more convenient.
4c0ccfef 1207 */
25726bc1
MW
1208int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
1209 get_block_t get_block)
4c0ccfef
MW
1210{
1211 struct buffer_head bh;
09cbfeaf
KS
1212 pgoff_t index = from >> PAGE_SHIFT;
1213 unsigned offset = from & (PAGE_SIZE-1);
4c0ccfef
MW
1214 int err;
1215
1216 /* Block boundary? Nothing to do */
1217 if (!length)
1218 return 0;
09cbfeaf 1219 BUG_ON((offset + length) > PAGE_SIZE);
4c0ccfef
MW
1220
1221 memset(&bh, 0, sizeof(bh));
eab95db6 1222 bh.b_bdev = inode->i_sb->s_bdev;
09cbfeaf 1223 bh.b_size = PAGE_SIZE;
4c0ccfef 1224 err = get_block(inode, index, &bh, 0);
679c8bd3 1225 if (err < 0 || !buffer_written(&bh))
4c0ccfef 1226 return err;
4c0ccfef 1227
679c8bd3
CH
1228 return __dax_zero_page_range(bh.b_bdev, to_sector(&bh, inode),
1229 offset, length);
4c0ccfef 1230}
25726bc1
MW
1231EXPORT_SYMBOL_GPL(dax_zero_page_range);
1232
1233/**
1234 * dax_truncate_page - handle a partial page being truncated in a DAX file
1235 * @inode: The file being truncated
1236 * @from: The file offset that is being truncated to
1237 * @get_block: The filesystem method used to translate file offsets to blocks
1238 *
1239 * Similar to block_truncate_page(), this function can be called by a
1240 * filesystem when it is truncating a DAX file to handle the partial page.
25726bc1
MW
1241 */
1242int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1243{
09cbfeaf 1244 unsigned length = PAGE_ALIGN(from) - from;
25726bc1
MW
1245 return dax_zero_page_range(inode, from, length, get_block);
1246}
4c0ccfef 1247EXPORT_SYMBOL_GPL(dax_truncate_page);
a254e568
CH
1248
1249#ifdef CONFIG_FS_IOMAP
1250static loff_t
1251iomap_dax_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1252 struct iomap *iomap)
1253{
1254 struct iov_iter *iter = data;
1255 loff_t end = pos + length, done = 0;
1256 ssize_t ret = 0;
1257
1258 if (iov_iter_rw(iter) == READ) {
1259 end = min(end, i_size_read(inode));
1260 if (pos >= end)
1261 return 0;
1262
1263 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1264 return iov_iter_zero(min(length, end - pos), iter);
1265 }
1266
1267 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1268 return -EIO;
1269
1270 while (pos < end) {
1271 unsigned offset = pos & (PAGE_SIZE - 1);
1272 struct blk_dax_ctl dax = { 0 };
1273 ssize_t map_len;
1274
1275 dax.sector = iomap->blkno +
1276 (((pos & PAGE_MASK) - iomap->offset) >> 9);
1277 dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
1278 map_len = dax_map_atomic(iomap->bdev, &dax);
1279 if (map_len < 0) {
1280 ret = map_len;
1281 break;
1282 }
1283
1284 dax.addr += offset;
1285 map_len -= offset;
1286 if (map_len > end - pos)
1287 map_len = end - pos;
1288
1289 if (iov_iter_rw(iter) == WRITE)
1290 map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
1291 else
1292 map_len = copy_to_iter(dax.addr, map_len, iter);
1293 dax_unmap_atomic(iomap->bdev, &dax);
1294 if (map_len <= 0) {
1295 ret = map_len ? map_len : -EFAULT;
1296 break;
1297 }
1298
1299 pos += map_len;
1300 length -= map_len;
1301 done += map_len;
1302 }
1303
1304 return done ? done : ret;
1305}
1306
1307/**
1308 * iomap_dax_rw - Perform I/O to a DAX file
1309 * @iocb: The control block for this I/O
1310 * @iter: The addresses to do I/O from or to
1311 * @ops: iomap ops passed from the file system
1312 *
1313 * This function performs read and write operations to directly mapped
1314 * persistent memory. The callers needs to take care of read/write exclusion
1315 * and evicting any page cache pages in the region under I/O.
1316 */
1317ssize_t
1318iomap_dax_rw(struct kiocb *iocb, struct iov_iter *iter,
1319 struct iomap_ops *ops)
1320{
1321 struct address_space *mapping = iocb->ki_filp->f_mapping;
1322 struct inode *inode = mapping->host;
1323 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1324 unsigned flags = 0;
1325
1326 if (iov_iter_rw(iter) == WRITE)
1327 flags |= IOMAP_WRITE;
1328
1329 /*
1330 * Yes, even DAX files can have page cache attached to them: A zeroed
1331 * page is inserted into the pagecache when we have to serve a write
1332 * fault on a hole. It should never be dirtied and can simply be
1333 * dropped from the pagecache once we get real data for the page.
1334 *
1335 * XXX: This is racy against mmap, and there's nothing we can do about
1336 * it. We'll eventually need to shift this down even further so that
1337 * we can check if we allocated blocks over a hole first.
1338 */
1339 if (mapping->nrpages) {
1340 ret = invalidate_inode_pages2_range(mapping,
1341 pos >> PAGE_SHIFT,
1342 (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
1343 WARN_ON_ONCE(ret);
1344 }
1345
1346 while (iov_iter_count(iter)) {
1347 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
1348 iter, iomap_dax_actor);
1349 if (ret <= 0)
1350 break;
1351 pos += ret;
1352 done += ret;
1353 }
1354
1355 iocb->ki_pos += done;
1356 return done ? done : ret;
1357}
1358EXPORT_SYMBOL_GPL(iomap_dax_rw);
a7d73fe6
CH
1359
1360/**
1361 * iomap_dax_fault - handle a page fault on a DAX file
1362 * @vma: The virtual memory area where the fault occurred
1363 * @vmf: The description of the fault
1364 * @ops: iomap ops passed from the file system
1365 *
1366 * When a page fault occurs, filesystems may call this helper in their fault
1367 * or mkwrite handler for DAX files. Assumes the caller has done all the
1368 * necessary locking for the page fault to proceed successfully.
1369 */
1370int iomap_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
1371 struct iomap_ops *ops)
1372{
1373 struct address_space *mapping = vma->vm_file->f_mapping;
1374 struct inode *inode = mapping->host;
1375 unsigned long vaddr = (unsigned long)vmf->virtual_address;
1376 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1377 sector_t sector;
1378 struct iomap iomap = { 0 };
1379 unsigned flags = 0;
1380 int error, major = 0;
1381 void *entry;
1382
1383 /*
1384 * Check whether offset isn't beyond end of file now. Caller is supposed
1385 * to hold locks serializing us with truncate / punch hole so this is
1386 * a reliable test.
1387 */
1388 if (pos >= i_size_read(inode))
1389 return VM_FAULT_SIGBUS;
1390
1391 entry = grab_mapping_entry(mapping, vmf->pgoff);
1392 if (IS_ERR(entry)) {
1393 error = PTR_ERR(entry);
1394 goto out;
1395 }
1396
1397 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1398 flags |= IOMAP_WRITE;
1399
1400 /*
1401 * Note that we don't bother to use iomap_apply here: DAX required
1402 * the file system block size to be equal the page size, which means
1403 * that we never have to deal with more than a single extent here.
1404 */
1405 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1406 if (error)
1407 goto unlock_entry;
1408 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
1409 error = -EIO; /* fs corruption? */
1410 goto unlock_entry;
1411 }
1412
1413 sector = iomap.blkno + (((pos & PAGE_MASK) - iomap.offset) >> 9);
1414
1415 if (vmf->cow_page) {
1416 switch (iomap.type) {
1417 case IOMAP_HOLE:
1418 case IOMAP_UNWRITTEN:
1419 clear_user_highpage(vmf->cow_page, vaddr);
1420 break;
1421 case IOMAP_MAPPED:
1422 error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
1423 vmf->cow_page, vaddr);
1424 break;
1425 default:
1426 WARN_ON_ONCE(1);
1427 error = -EIO;
1428 break;
1429 }
1430
1431 if (error)
1432 goto unlock_entry;
1433 if (!radix_tree_exceptional_entry(entry)) {
1434 vmf->page = entry;
1435 return VM_FAULT_LOCKED;
1436 }
1437 vmf->entry = entry;
1438 return VM_FAULT_DAX_LOCKED;
1439 }
1440
1441 switch (iomap.type) {
1442 case IOMAP_MAPPED:
1443 if (iomap.flags & IOMAP_F_NEW) {
1444 count_vm_event(PGMAJFAULT);
1445 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1446 major = VM_FAULT_MAJOR;
1447 }
1448 error = dax_insert_mapping(mapping, iomap.bdev, sector,
1449 PAGE_SIZE, &entry, vma, vmf);
1450 break;
1451 case IOMAP_UNWRITTEN:
1452 case IOMAP_HOLE:
1453 if (!(vmf->flags & FAULT_FLAG_WRITE))
1454 return dax_load_hole(mapping, entry, vmf);
1455 /*FALLTHRU*/
1456 default:
1457 WARN_ON_ONCE(1);
1458 error = -EIO;
1459 break;
1460 }
1461
1462 unlock_entry:
1463 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
1464 out:
1465 if (error == -ENOMEM)
1466 return VM_FAULT_OOM | major;
1467 /* -EBUSY is fine, somebody else faulted on the same PTE */
1468 if (error < 0 && error != -EBUSY)
1469 return VM_FAULT_SIGBUS | major;
1470 return VM_FAULT_NOPAGE | major;
1471}
1472EXPORT_SYMBOL_GPL(iomap_dax_fault);
a254e568 1473#endif /* CONFIG_FS_IOMAP */
This page took 0.389975 seconds and 4 git commands to generate.