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