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> |
d475c634 | 34 | |
b2e0d162 DW |
35 | static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax) |
36 | { | |
37 | struct request_queue *q = bdev->bd_queue; | |
38 | long rc = -EIO; | |
39 | ||
40 | dax->addr = (void __pmem *) ERR_PTR(-EIO); | |
41 | if (blk_queue_enter(q, true) != 0) | |
42 | return rc; | |
43 | ||
44 | rc = bdev_direct_access(bdev, dax); | |
45 | if (rc < 0) { | |
46 | dax->addr = (void __pmem *) ERR_PTR(rc); | |
47 | blk_queue_exit(q); | |
48 | return rc; | |
49 | } | |
50 | return rc; | |
51 | } | |
52 | ||
53 | static void dax_unmap_atomic(struct block_device *bdev, | |
54 | const struct blk_dax_ctl *dax) | |
55 | { | |
56 | if (IS_ERR(dax->addr)) | |
57 | return; | |
58 | blk_queue_exit(bdev->bd_queue); | |
59 | } | |
60 | ||
1ca19157 DC |
61 | /* |
62 | * dax_clear_blocks() is called from within transaction context from XFS, | |
63 | * and hence this means the stack from this point must follow GFP_NOFS | |
64 | * semantics for all operations. | |
65 | */ | |
b2e0d162 | 66 | int dax_clear_blocks(struct inode *inode, sector_t block, long _size) |
289c6aed MW |
67 | { |
68 | struct block_device *bdev = inode->i_sb->s_bdev; | |
b2e0d162 DW |
69 | struct blk_dax_ctl dax = { |
70 | .sector = block << (inode->i_blkbits - 9), | |
71 | .size = _size, | |
72 | }; | |
289c6aed MW |
73 | |
74 | might_sleep(); | |
75 | do { | |
0e749e54 | 76 | long count, sz; |
289c6aed | 77 | |
b2e0d162 | 78 | count = dax_map_atomic(bdev, &dax); |
289c6aed MW |
79 | if (count < 0) |
80 | return count; | |
0e749e54 | 81 | sz = min_t(long, count, SZ_128K); |
b2e0d162 DW |
82 | clear_pmem(dax.addr, sz); |
83 | dax.size -= sz; | |
84 | dax.sector += sz / 512; | |
85 | dax_unmap_atomic(bdev, &dax); | |
0e749e54 | 86 | cond_resched(); |
b2e0d162 | 87 | } while (dax.size); |
289c6aed | 88 | |
2765cfbb | 89 | wmb_pmem(); |
289c6aed MW |
90 | return 0; |
91 | } | |
92 | EXPORT_SYMBOL_GPL(dax_clear_blocks); | |
93 | ||
2765cfbb | 94 | /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */ |
e2e05394 RZ |
95 | static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first, |
96 | loff_t pos, loff_t end) | |
d475c634 MW |
97 | { |
98 | loff_t final = end - pos + first; /* The final byte of the buffer */ | |
99 | ||
100 | if (first > 0) | |
e2e05394 | 101 | clear_pmem(addr, first); |
d475c634 | 102 | if (final < size) |
e2e05394 | 103 | clear_pmem(addr + final, size - final); |
d475c634 MW |
104 | } |
105 | ||
106 | static bool buffer_written(struct buffer_head *bh) | |
107 | { | |
108 | return buffer_mapped(bh) && !buffer_unwritten(bh); | |
109 | } | |
110 | ||
111 | /* | |
112 | * When ext4 encounters a hole, it returns without modifying the buffer_head | |
113 | * which means that we can't trust b_size. To cope with this, we set b_state | |
114 | * to 0 before calling get_block and, if any bit is set, we know we can trust | |
115 | * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is | |
116 | * and would save us time calling get_block repeatedly. | |
117 | */ | |
118 | static bool buffer_size_valid(struct buffer_head *bh) | |
119 | { | |
120 | return bh->b_state != 0; | |
121 | } | |
122 | ||
b2e0d162 DW |
123 | |
124 | static sector_t to_sector(const struct buffer_head *bh, | |
125 | const struct inode *inode) | |
126 | { | |
127 | sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9); | |
128 | ||
129 | return sector; | |
130 | } | |
131 | ||
a95cd631 OS |
132 | static ssize_t dax_io(struct inode *inode, struct iov_iter *iter, |
133 | loff_t start, loff_t end, get_block_t get_block, | |
134 | struct buffer_head *bh) | |
d475c634 | 135 | { |
b2e0d162 DW |
136 | loff_t pos = start, max = start, bh_max = start; |
137 | bool hole = false, need_wmb = false; | |
138 | struct block_device *bdev = NULL; | |
139 | int rw = iov_iter_rw(iter), rc; | |
140 | long map_len = 0; | |
141 | struct blk_dax_ctl dax = { | |
142 | .addr = (void __pmem *) ERR_PTR(-EIO), | |
143 | }; | |
144 | ||
145 | if (rw == READ) | |
d475c634 MW |
146 | end = min(end, i_size_read(inode)); |
147 | ||
148 | while (pos < end) { | |
2765cfbb | 149 | size_t len; |
d475c634 MW |
150 | if (pos == max) { |
151 | unsigned blkbits = inode->i_blkbits; | |
e94f5a22 JM |
152 | long page = pos >> PAGE_SHIFT; |
153 | sector_t block = page << (PAGE_SHIFT - blkbits); | |
d475c634 MW |
154 | unsigned first = pos - (block << blkbits); |
155 | long size; | |
156 | ||
157 | if (pos == bh_max) { | |
158 | bh->b_size = PAGE_ALIGN(end - pos); | |
159 | bh->b_state = 0; | |
b2e0d162 DW |
160 | rc = get_block(inode, block, bh, rw == WRITE); |
161 | if (rc) | |
d475c634 MW |
162 | break; |
163 | if (!buffer_size_valid(bh)) | |
164 | bh->b_size = 1 << blkbits; | |
165 | bh_max = pos - first + bh->b_size; | |
b2e0d162 | 166 | bdev = bh->b_bdev; |
d475c634 MW |
167 | } else { |
168 | unsigned done = bh->b_size - | |
169 | (bh_max - (pos - first)); | |
170 | bh->b_blocknr += done >> blkbits; | |
171 | bh->b_size -= done; | |
172 | } | |
173 | ||
b2e0d162 | 174 | hole = rw == READ && !buffer_written(bh); |
d475c634 | 175 | if (hole) { |
d475c634 MW |
176 | size = bh->b_size - first; |
177 | } else { | |
b2e0d162 DW |
178 | dax_unmap_atomic(bdev, &dax); |
179 | dax.sector = to_sector(bh, inode); | |
180 | dax.size = bh->b_size; | |
181 | map_len = dax_map_atomic(bdev, &dax); | |
182 | if (map_len < 0) { | |
183 | rc = map_len; | |
d475c634 | 184 | break; |
b2e0d162 | 185 | } |
2765cfbb | 186 | if (buffer_unwritten(bh) || buffer_new(bh)) { |
b2e0d162 DW |
187 | dax_new_buf(dax.addr, map_len, first, |
188 | pos, end); | |
2765cfbb RZ |
189 | need_wmb = true; |
190 | } | |
b2e0d162 DW |
191 | dax.addr += first; |
192 | size = map_len - first; | |
d475c634 MW |
193 | } |
194 | max = min(pos + size, end); | |
195 | } | |
196 | ||
2765cfbb | 197 | if (iov_iter_rw(iter) == WRITE) { |
b2e0d162 | 198 | len = copy_from_iter_pmem(dax.addr, max - pos, iter); |
2765cfbb RZ |
199 | need_wmb = true; |
200 | } else if (!hole) | |
b2e0d162 | 201 | len = copy_to_iter((void __force *) dax.addr, max - pos, |
e2e05394 | 202 | iter); |
d475c634 MW |
203 | else |
204 | len = iov_iter_zero(max - pos, iter); | |
205 | ||
cadfbb6e | 206 | if (!len) { |
b2e0d162 | 207 | rc = -EFAULT; |
d475c634 | 208 | break; |
cadfbb6e | 209 | } |
d475c634 MW |
210 | |
211 | pos += len; | |
b2e0d162 DW |
212 | if (!IS_ERR(dax.addr)) |
213 | dax.addr += len; | |
d475c634 MW |
214 | } |
215 | ||
2765cfbb RZ |
216 | if (need_wmb) |
217 | wmb_pmem(); | |
b2e0d162 | 218 | dax_unmap_atomic(bdev, &dax); |
2765cfbb | 219 | |
b2e0d162 | 220 | return (pos == start) ? rc : pos - start; |
d475c634 MW |
221 | } |
222 | ||
223 | /** | |
224 | * dax_do_io - Perform I/O to a DAX file | |
d475c634 MW |
225 | * @iocb: The control block for this I/O |
226 | * @inode: The file which the I/O is directed at | |
227 | * @iter: The addresses to do I/O from or to | |
228 | * @pos: The file offset where the I/O starts | |
229 | * @get_block: The filesystem method used to translate file offsets to blocks | |
230 | * @end_io: A filesystem callback for I/O completion | |
231 | * @flags: See below | |
232 | * | |
233 | * This function uses the same locking scheme as do_blockdev_direct_IO: | |
234 | * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the | |
235 | * caller for writes. For reads, we take and release the i_mutex ourselves. | |
236 | * If DIO_LOCKING is not set, the filesystem takes care of its own locking. | |
237 | * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O | |
238 | * is in progress. | |
239 | */ | |
a95cd631 OS |
240 | ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode, |
241 | struct iov_iter *iter, loff_t pos, get_block_t get_block, | |
242 | dio_iodone_t end_io, int flags) | |
d475c634 MW |
243 | { |
244 | struct buffer_head bh; | |
245 | ssize_t retval = -EINVAL; | |
246 | loff_t end = pos + iov_iter_count(iter); | |
247 | ||
248 | memset(&bh, 0, sizeof(bh)); | |
eab95db6 | 249 | bh.b_bdev = inode->i_sb->s_bdev; |
d475c634 | 250 | |
a95cd631 | 251 | if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) { |
d475c634 | 252 | struct address_space *mapping = inode->i_mapping; |
5955102c | 253 | inode_lock(inode); |
d475c634 MW |
254 | retval = filemap_write_and_wait_range(mapping, pos, end - 1); |
255 | if (retval) { | |
5955102c | 256 | inode_unlock(inode); |
d475c634 MW |
257 | goto out; |
258 | } | |
259 | } | |
260 | ||
261 | /* Protects against truncate */ | |
bbab37dd MW |
262 | if (!(flags & DIO_SKIP_DIO_COUNT)) |
263 | inode_dio_begin(inode); | |
d475c634 | 264 | |
a95cd631 | 265 | retval = dax_io(inode, iter, pos, end, get_block, &bh); |
d475c634 | 266 | |
a95cd631 | 267 | if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) |
5955102c | 268 | inode_unlock(inode); |
d475c634 | 269 | |
187372a3 CH |
270 | if (end_io) { |
271 | int err; | |
272 | ||
273 | err = end_io(iocb, pos, retval, bh.b_private); | |
274 | if (err) | |
275 | retval = err; | |
276 | } | |
d475c634 | 277 | |
bbab37dd MW |
278 | if (!(flags & DIO_SKIP_DIO_COUNT)) |
279 | inode_dio_end(inode); | |
d475c634 MW |
280 | out: |
281 | return retval; | |
282 | } | |
283 | EXPORT_SYMBOL_GPL(dax_do_io); | |
f7ca90b1 MW |
284 | |
285 | /* | |
286 | * The user has performed a load from a hole in the file. Allocating | |
287 | * a new page in the file would cause excessive storage usage for | |
288 | * workloads with sparse files. We allocate a page cache page instead. | |
289 | * We'll kick it out of the page cache if it's ever written to, | |
290 | * otherwise it will simply fall out of the page cache under memory | |
291 | * pressure without ever having been dirtied. | |
292 | */ | |
293 | static int dax_load_hole(struct address_space *mapping, struct page *page, | |
294 | struct vm_fault *vmf) | |
295 | { | |
296 | unsigned long size; | |
297 | struct inode *inode = mapping->host; | |
298 | if (!page) | |
299 | page = find_or_create_page(mapping, vmf->pgoff, | |
300 | GFP_KERNEL | __GFP_ZERO); | |
301 | if (!page) | |
302 | return VM_FAULT_OOM; | |
303 | /* Recheck i_size under page lock to avoid truncate race */ | |
304 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
305 | if (vmf->pgoff >= size) { | |
306 | unlock_page(page); | |
307 | page_cache_release(page); | |
308 | return VM_FAULT_SIGBUS; | |
309 | } | |
310 | ||
311 | vmf->page = page; | |
312 | return VM_FAULT_LOCKED; | |
313 | } | |
314 | ||
b2e0d162 DW |
315 | static int copy_user_bh(struct page *to, struct inode *inode, |
316 | struct buffer_head *bh, unsigned long vaddr) | |
f7ca90b1 | 317 | { |
b2e0d162 DW |
318 | struct blk_dax_ctl dax = { |
319 | .sector = to_sector(bh, inode), | |
320 | .size = bh->b_size, | |
321 | }; | |
322 | struct block_device *bdev = bh->b_bdev; | |
e2e05394 RZ |
323 | void *vto; |
324 | ||
b2e0d162 DW |
325 | if (dax_map_atomic(bdev, &dax) < 0) |
326 | return PTR_ERR(dax.addr); | |
f7ca90b1 | 327 | vto = kmap_atomic(to); |
b2e0d162 | 328 | copy_user_page(vto, (void __force *)dax.addr, vaddr, to); |
f7ca90b1 | 329 | kunmap_atomic(vto); |
b2e0d162 | 330 | dax_unmap_atomic(bdev, &dax); |
f7ca90b1 MW |
331 | return 0; |
332 | } | |
333 | ||
9973c98e RZ |
334 | #define NO_SECTOR -1 |
335 | #define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_CACHE_SHIFT)) | |
336 | ||
337 | static int dax_radix_entry(struct address_space *mapping, pgoff_t index, | |
338 | sector_t sector, bool pmd_entry, bool dirty) | |
339 | { | |
340 | struct radix_tree_root *page_tree = &mapping->page_tree; | |
341 | pgoff_t pmd_index = DAX_PMD_INDEX(index); | |
342 | int type, error = 0; | |
343 | void *entry; | |
344 | ||
345 | WARN_ON_ONCE(pmd_entry && !dirty); | |
346 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); | |
347 | ||
348 | spin_lock_irq(&mapping->tree_lock); | |
349 | ||
350 | entry = radix_tree_lookup(page_tree, pmd_index); | |
351 | if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) { | |
352 | index = pmd_index; | |
353 | goto dirty; | |
354 | } | |
355 | ||
356 | entry = radix_tree_lookup(page_tree, index); | |
357 | if (entry) { | |
358 | type = RADIX_DAX_TYPE(entry); | |
359 | if (WARN_ON_ONCE(type != RADIX_DAX_PTE && | |
360 | type != RADIX_DAX_PMD)) { | |
361 | error = -EIO; | |
362 | goto unlock; | |
363 | } | |
364 | ||
365 | if (!pmd_entry || type == RADIX_DAX_PMD) | |
366 | goto dirty; | |
367 | ||
368 | /* | |
369 | * We only insert dirty PMD entries into the radix tree. This | |
370 | * means we don't need to worry about removing a dirty PTE | |
371 | * entry and inserting a clean PMD entry, thus reducing the | |
372 | * range we would flush with a follow-up fsync/msync call. | |
373 | */ | |
374 | radix_tree_delete(&mapping->page_tree, index); | |
375 | mapping->nrexceptional--; | |
376 | } | |
377 | ||
378 | if (sector == NO_SECTOR) { | |
379 | /* | |
380 | * This can happen during correct operation if our pfn_mkwrite | |
381 | * fault raced against a hole punch operation. If this | |
382 | * happens the pte that was hole punched will have been | |
383 | * unmapped and the radix tree entry will have been removed by | |
384 | * the time we are called, but the call will still happen. We | |
385 | * will return all the way up to wp_pfn_shared(), where the | |
386 | * pte_same() check will fail, eventually causing page fault | |
387 | * to be retried by the CPU. | |
388 | */ | |
389 | goto unlock; | |
390 | } | |
391 | ||
392 | error = radix_tree_insert(page_tree, index, | |
393 | RADIX_DAX_ENTRY(sector, pmd_entry)); | |
394 | if (error) | |
395 | goto unlock; | |
396 | ||
397 | mapping->nrexceptional++; | |
398 | dirty: | |
399 | if (dirty) | |
400 | radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY); | |
401 | unlock: | |
402 | spin_unlock_irq(&mapping->tree_lock); | |
403 | return error; | |
404 | } | |
405 | ||
406 | static int dax_writeback_one(struct block_device *bdev, | |
407 | struct address_space *mapping, pgoff_t index, void *entry) | |
408 | { | |
409 | struct radix_tree_root *page_tree = &mapping->page_tree; | |
410 | int type = RADIX_DAX_TYPE(entry); | |
411 | struct radix_tree_node *node; | |
412 | struct blk_dax_ctl dax; | |
413 | void **slot; | |
414 | int ret = 0; | |
415 | ||
416 | spin_lock_irq(&mapping->tree_lock); | |
417 | /* | |
418 | * Regular page slots are stabilized by the page lock even | |
419 | * without the tree itself locked. These unlocked entries | |
420 | * need verification under the tree lock. | |
421 | */ | |
422 | if (!__radix_tree_lookup(page_tree, index, &node, &slot)) | |
423 | goto unlock; | |
424 | if (*slot != entry) | |
425 | goto unlock; | |
426 | ||
427 | /* another fsync thread may have already written back this entry */ | |
428 | if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)) | |
429 | goto unlock; | |
430 | ||
431 | if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) { | |
432 | ret = -EIO; | |
433 | goto unlock; | |
434 | } | |
435 | ||
436 | dax.sector = RADIX_DAX_SECTOR(entry); | |
437 | dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE); | |
438 | spin_unlock_irq(&mapping->tree_lock); | |
439 | ||
440 | /* | |
441 | * We cannot hold tree_lock while calling dax_map_atomic() because it | |
442 | * eventually calls cond_resched(). | |
443 | */ | |
444 | ret = dax_map_atomic(bdev, &dax); | |
445 | if (ret < 0) | |
446 | return ret; | |
447 | ||
448 | if (WARN_ON_ONCE(ret < dax.size)) { | |
449 | ret = -EIO; | |
450 | goto unmap; | |
451 | } | |
452 | ||
453 | wb_cache_pmem(dax.addr, dax.size); | |
454 | ||
455 | spin_lock_irq(&mapping->tree_lock); | |
456 | radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE); | |
457 | spin_unlock_irq(&mapping->tree_lock); | |
458 | unmap: | |
459 | dax_unmap_atomic(bdev, &dax); | |
460 | return ret; | |
461 | ||
462 | unlock: | |
463 | spin_unlock_irq(&mapping->tree_lock); | |
464 | return ret; | |
465 | } | |
466 | ||
467 | /* | |
468 | * Flush the mapping to the persistent domain within the byte range of [start, | |
469 | * end]. This is required by data integrity operations to ensure file data is | |
470 | * on persistent storage prior to completion of the operation. | |
471 | */ | |
472 | int dax_writeback_mapping_range(struct address_space *mapping, loff_t start, | |
473 | loff_t end) | |
474 | { | |
475 | struct inode *inode = mapping->host; | |
476 | struct block_device *bdev = inode->i_sb->s_bdev; | |
477 | pgoff_t start_index, end_index, pmd_index; | |
478 | pgoff_t indices[PAGEVEC_SIZE]; | |
479 | struct pagevec pvec; | |
480 | bool done = false; | |
481 | int i, ret = 0; | |
482 | void *entry; | |
483 | ||
484 | if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) | |
485 | return -EIO; | |
486 | ||
487 | start_index = start >> PAGE_CACHE_SHIFT; | |
488 | end_index = end >> PAGE_CACHE_SHIFT; | |
489 | pmd_index = DAX_PMD_INDEX(start_index); | |
490 | ||
491 | rcu_read_lock(); | |
492 | entry = radix_tree_lookup(&mapping->page_tree, pmd_index); | |
493 | rcu_read_unlock(); | |
494 | ||
495 | /* see if the start of our range is covered by a PMD entry */ | |
496 | if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) | |
497 | start_index = pmd_index; | |
498 | ||
499 | tag_pages_for_writeback(mapping, start_index, end_index); | |
500 | ||
501 | pagevec_init(&pvec, 0); | |
502 | while (!done) { | |
503 | pvec.nr = find_get_entries_tag(mapping, start_index, | |
504 | PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE, | |
505 | pvec.pages, indices); | |
506 | ||
507 | if (pvec.nr == 0) | |
508 | break; | |
509 | ||
510 | for (i = 0; i < pvec.nr; i++) { | |
511 | if (indices[i] > end_index) { | |
512 | done = true; | |
513 | break; | |
514 | } | |
515 | ||
516 | ret = dax_writeback_one(bdev, mapping, indices[i], | |
517 | pvec.pages[i]); | |
518 | if (ret < 0) | |
519 | return ret; | |
520 | } | |
521 | } | |
522 | wmb_pmem(); | |
523 | return 0; | |
524 | } | |
525 | EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); | |
526 | ||
f7ca90b1 MW |
527 | static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh, |
528 | struct vm_area_struct *vma, struct vm_fault *vmf) | |
529 | { | |
f7ca90b1 | 530 | unsigned long vaddr = (unsigned long)vmf->virtual_address; |
b2e0d162 DW |
531 | struct address_space *mapping = inode->i_mapping; |
532 | struct block_device *bdev = bh->b_bdev; | |
533 | struct blk_dax_ctl dax = { | |
534 | .sector = to_sector(bh, inode), | |
535 | .size = bh->b_size, | |
536 | }; | |
f7ca90b1 MW |
537 | pgoff_t size; |
538 | int error; | |
539 | ||
0f90cc66 RZ |
540 | i_mmap_lock_read(mapping); |
541 | ||
f7ca90b1 MW |
542 | /* |
543 | * Check truncate didn't happen while we were allocating a block. | |
544 | * If it did, this block may or may not be still allocated to the | |
545 | * file. We can't tell the filesystem to free it because we can't | |
546 | * take i_mutex here. In the worst case, the file still has blocks | |
547 | * allocated past the end of the file. | |
548 | */ | |
549 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
550 | if (unlikely(vmf->pgoff >= size)) { | |
551 | error = -EIO; | |
552 | goto out; | |
553 | } | |
554 | ||
b2e0d162 DW |
555 | if (dax_map_atomic(bdev, &dax) < 0) { |
556 | error = PTR_ERR(dax.addr); | |
f7ca90b1 MW |
557 | goto out; |
558 | } | |
559 | ||
2765cfbb | 560 | if (buffer_unwritten(bh) || buffer_new(bh)) { |
b2e0d162 | 561 | clear_pmem(dax.addr, PAGE_SIZE); |
2765cfbb RZ |
562 | wmb_pmem(); |
563 | } | |
b2e0d162 | 564 | dax_unmap_atomic(bdev, &dax); |
f7ca90b1 | 565 | |
9973c98e RZ |
566 | error = dax_radix_entry(mapping, vmf->pgoff, dax.sector, false, |
567 | vmf->flags & FAULT_FLAG_WRITE); | |
568 | if (error) | |
569 | goto out; | |
570 | ||
01c8f1c4 | 571 | error = vm_insert_mixed(vma, vaddr, dax.pfn); |
f7ca90b1 MW |
572 | |
573 | out: | |
0f90cc66 RZ |
574 | i_mmap_unlock_read(mapping); |
575 | ||
f7ca90b1 MW |
576 | return error; |
577 | } | |
578 | ||
ce5c5d55 DC |
579 | /** |
580 | * __dax_fault - handle a page fault on a DAX file | |
581 | * @vma: The virtual memory area where the fault occurred | |
582 | * @vmf: The description of the fault | |
583 | * @get_block: The filesystem method used to translate file offsets to blocks | |
b2442c5a DC |
584 | * @complete_unwritten: The filesystem method used to convert unwritten blocks |
585 | * to written so the data written to them is exposed. This is required for | |
586 | * required by write faults for filesystems that will return unwritten | |
587 | * extent mappings from @get_block, but it is optional for reads as | |
588 | * dax_insert_mapping() will always zero unwritten blocks. If the fs does | |
589 | * not support unwritten extents, the it should pass NULL. | |
ce5c5d55 DC |
590 | * |
591 | * When a page fault occurs, filesystems may call this helper in their | |
592 | * fault handler for DAX files. __dax_fault() assumes the caller has done all | |
593 | * the necessary locking for the page fault to proceed successfully. | |
594 | */ | |
595 | int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, | |
e842f290 | 596 | get_block_t get_block, dax_iodone_t complete_unwritten) |
f7ca90b1 MW |
597 | { |
598 | struct file *file = vma->vm_file; | |
599 | struct address_space *mapping = file->f_mapping; | |
600 | struct inode *inode = mapping->host; | |
601 | struct page *page; | |
602 | struct buffer_head bh; | |
603 | unsigned long vaddr = (unsigned long)vmf->virtual_address; | |
604 | unsigned blkbits = inode->i_blkbits; | |
605 | sector_t block; | |
606 | pgoff_t size; | |
607 | int error; | |
608 | int major = 0; | |
609 | ||
610 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
611 | if (vmf->pgoff >= size) | |
612 | return VM_FAULT_SIGBUS; | |
613 | ||
614 | memset(&bh, 0, sizeof(bh)); | |
615 | block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits); | |
eab95db6 | 616 | bh.b_bdev = inode->i_sb->s_bdev; |
f7ca90b1 MW |
617 | bh.b_size = PAGE_SIZE; |
618 | ||
619 | repeat: | |
620 | page = find_get_page(mapping, vmf->pgoff); | |
621 | if (page) { | |
622 | if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) { | |
623 | page_cache_release(page); | |
624 | return VM_FAULT_RETRY; | |
625 | } | |
626 | if (unlikely(page->mapping != mapping)) { | |
627 | unlock_page(page); | |
628 | page_cache_release(page); | |
629 | goto repeat; | |
630 | } | |
631 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
632 | if (unlikely(vmf->pgoff >= size)) { | |
633 | /* | |
634 | * We have a struct page covering a hole in the file | |
635 | * from a read fault and we've raced with a truncate | |
636 | */ | |
637 | error = -EIO; | |
0f90cc66 | 638 | goto unlock_page; |
f7ca90b1 MW |
639 | } |
640 | } | |
641 | ||
642 | error = get_block(inode, block, &bh, 0); | |
643 | if (!error && (bh.b_size < PAGE_SIZE)) | |
644 | error = -EIO; /* fs corruption? */ | |
645 | if (error) | |
0f90cc66 | 646 | goto unlock_page; |
f7ca90b1 MW |
647 | |
648 | if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) { | |
649 | if (vmf->flags & FAULT_FLAG_WRITE) { | |
650 | error = get_block(inode, block, &bh, 1); | |
651 | count_vm_event(PGMAJFAULT); | |
652 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); | |
653 | major = VM_FAULT_MAJOR; | |
654 | if (!error && (bh.b_size < PAGE_SIZE)) | |
655 | error = -EIO; | |
656 | if (error) | |
0f90cc66 | 657 | goto unlock_page; |
f7ca90b1 MW |
658 | } else { |
659 | return dax_load_hole(mapping, page, vmf); | |
660 | } | |
661 | } | |
662 | ||
663 | if (vmf->cow_page) { | |
664 | struct page *new_page = vmf->cow_page; | |
665 | if (buffer_written(&bh)) | |
b2e0d162 | 666 | error = copy_user_bh(new_page, inode, &bh, vaddr); |
f7ca90b1 MW |
667 | else |
668 | clear_user_highpage(new_page, vaddr); | |
669 | if (error) | |
0f90cc66 | 670 | goto unlock_page; |
f7ca90b1 MW |
671 | vmf->page = page; |
672 | if (!page) { | |
0f90cc66 | 673 | i_mmap_lock_read(mapping); |
f7ca90b1 MW |
674 | /* Check we didn't race with truncate */ |
675 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> | |
676 | PAGE_SHIFT; | |
677 | if (vmf->pgoff >= size) { | |
0f90cc66 | 678 | i_mmap_unlock_read(mapping); |
f7ca90b1 | 679 | error = -EIO; |
0f90cc66 | 680 | goto out; |
f7ca90b1 MW |
681 | } |
682 | } | |
683 | return VM_FAULT_LOCKED; | |
684 | } | |
685 | ||
686 | /* Check we didn't race with a read fault installing a new page */ | |
687 | if (!page && major) | |
688 | page = find_lock_page(mapping, vmf->pgoff); | |
689 | ||
690 | if (page) { | |
691 | unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT, | |
692 | PAGE_CACHE_SIZE, 0); | |
693 | delete_from_page_cache(page); | |
694 | unlock_page(page); | |
695 | page_cache_release(page); | |
9973c98e | 696 | page = NULL; |
f7ca90b1 MW |
697 | } |
698 | ||
e842f290 DC |
699 | /* |
700 | * If we successfully insert the new mapping over an unwritten extent, | |
701 | * we need to ensure we convert the unwritten extent. If there is an | |
702 | * error inserting the mapping, the filesystem needs to leave it as | |
703 | * unwritten to prevent exposure of the stale underlying data to | |
704 | * userspace, but we still need to call the completion function so | |
705 | * the private resources on the mapping buffer can be released. We | |
706 | * indicate what the callback should do via the uptodate variable, same | |
707 | * as for normal BH based IO completions. | |
708 | */ | |
f7ca90b1 | 709 | error = dax_insert_mapping(inode, &bh, vma, vmf); |
b2442c5a DC |
710 | if (buffer_unwritten(&bh)) { |
711 | if (complete_unwritten) | |
712 | complete_unwritten(&bh, !error); | |
713 | else | |
714 | WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE)); | |
715 | } | |
f7ca90b1 MW |
716 | |
717 | out: | |
718 | if (error == -ENOMEM) | |
719 | return VM_FAULT_OOM | major; | |
720 | /* -EBUSY is fine, somebody else faulted on the same PTE */ | |
721 | if ((error < 0) && (error != -EBUSY)) | |
722 | return VM_FAULT_SIGBUS | major; | |
723 | return VM_FAULT_NOPAGE | major; | |
724 | ||
0f90cc66 | 725 | unlock_page: |
f7ca90b1 MW |
726 | if (page) { |
727 | unlock_page(page); | |
728 | page_cache_release(page); | |
729 | } | |
730 | goto out; | |
731 | } | |
ce5c5d55 | 732 | EXPORT_SYMBOL(__dax_fault); |
f7ca90b1 MW |
733 | |
734 | /** | |
735 | * dax_fault - handle a page fault on a DAX file | |
736 | * @vma: The virtual memory area where the fault occurred | |
737 | * @vmf: The description of the fault | |
738 | * @get_block: The filesystem method used to translate file offsets to blocks | |
739 | * | |
740 | * When a page fault occurs, filesystems may call this helper in their | |
741 | * fault handler for DAX files. | |
742 | */ | |
743 | int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, | |
e842f290 | 744 | get_block_t get_block, dax_iodone_t complete_unwritten) |
f7ca90b1 MW |
745 | { |
746 | int result; | |
747 | struct super_block *sb = file_inode(vma->vm_file)->i_sb; | |
748 | ||
749 | if (vmf->flags & FAULT_FLAG_WRITE) { | |
750 | sb_start_pagefault(sb); | |
751 | file_update_time(vma->vm_file); | |
752 | } | |
ce5c5d55 | 753 | result = __dax_fault(vma, vmf, get_block, complete_unwritten); |
f7ca90b1 MW |
754 | if (vmf->flags & FAULT_FLAG_WRITE) |
755 | sb_end_pagefault(sb); | |
756 | ||
757 | return result; | |
758 | } | |
759 | EXPORT_SYMBOL_GPL(dax_fault); | |
4c0ccfef | 760 | |
844f35db MW |
761 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
762 | /* | |
763 | * The 'colour' (ie low bits) within a PMD of a page offset. This comes up | |
764 | * more often than one might expect in the below function. | |
765 | */ | |
766 | #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) | |
767 | ||
cbb38e41 DW |
768 | static void __dax_dbg(struct buffer_head *bh, unsigned long address, |
769 | const char *reason, const char *fn) | |
770 | { | |
771 | if (bh) { | |
772 | char bname[BDEVNAME_SIZE]; | |
773 | bdevname(bh->b_bdev, bname); | |
774 | pr_debug("%s: %s addr: %lx dev %s state %lx start %lld " | |
775 | "length %zd fallback: %s\n", fn, current->comm, | |
776 | address, bname, bh->b_state, (u64)bh->b_blocknr, | |
777 | bh->b_size, reason); | |
778 | } else { | |
779 | pr_debug("%s: %s addr: %lx fallback: %s\n", fn, | |
780 | current->comm, address, reason); | |
781 | } | |
782 | } | |
783 | ||
784 | #define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd") | |
785 | ||
844f35db MW |
786 | int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, |
787 | pmd_t *pmd, unsigned int flags, get_block_t get_block, | |
788 | dax_iodone_t complete_unwritten) | |
789 | { | |
790 | struct file *file = vma->vm_file; | |
791 | struct address_space *mapping = file->f_mapping; | |
792 | struct inode *inode = mapping->host; | |
793 | struct buffer_head bh; | |
794 | unsigned blkbits = inode->i_blkbits; | |
795 | unsigned long pmd_addr = address & PMD_MASK; | |
796 | bool write = flags & FAULT_FLAG_WRITE; | |
b2e0d162 | 797 | struct block_device *bdev; |
844f35db | 798 | pgoff_t size, pgoff; |
b2e0d162 | 799 | sector_t block; |
9973c98e RZ |
800 | int error, result = 0; |
801 | bool alloc = false; | |
844f35db | 802 | |
c046c321 | 803 | /* dax pmd mappings require pfn_t_devmap() */ |
ee82c9ed DW |
804 | if (!IS_ENABLED(CONFIG_FS_DAX_PMD)) |
805 | return VM_FAULT_FALLBACK; | |
806 | ||
844f35db | 807 | /* Fall back to PTEs if we're going to COW */ |
59bf4fb9 TK |
808 | if (write && !(vma->vm_flags & VM_SHARED)) { |
809 | split_huge_pmd(vma, pmd, address); | |
cbb38e41 | 810 | dax_pmd_dbg(NULL, address, "cow write"); |
844f35db | 811 | return VM_FAULT_FALLBACK; |
59bf4fb9 | 812 | } |
844f35db | 813 | /* If the PMD would extend outside the VMA */ |
cbb38e41 DW |
814 | if (pmd_addr < vma->vm_start) { |
815 | dax_pmd_dbg(NULL, address, "vma start unaligned"); | |
844f35db | 816 | return VM_FAULT_FALLBACK; |
cbb38e41 DW |
817 | } |
818 | if ((pmd_addr + PMD_SIZE) > vma->vm_end) { | |
819 | dax_pmd_dbg(NULL, address, "vma end unaligned"); | |
844f35db | 820 | return VM_FAULT_FALLBACK; |
cbb38e41 | 821 | } |
844f35db | 822 | |
3fdd1b47 | 823 | pgoff = linear_page_index(vma, pmd_addr); |
844f35db MW |
824 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
825 | if (pgoff >= size) | |
826 | return VM_FAULT_SIGBUS; | |
827 | /* If the PMD would cover blocks out of the file */ | |
cbb38e41 DW |
828 | if ((pgoff | PG_PMD_COLOUR) >= size) { |
829 | dax_pmd_dbg(NULL, address, | |
830 | "offset + huge page size > file size"); | |
844f35db | 831 | return VM_FAULT_FALLBACK; |
cbb38e41 | 832 | } |
844f35db MW |
833 | |
834 | memset(&bh, 0, sizeof(bh)); | |
d4bbe706 | 835 | bh.b_bdev = inode->i_sb->s_bdev; |
844f35db MW |
836 | block = (sector_t)pgoff << (PAGE_SHIFT - blkbits); |
837 | ||
838 | bh.b_size = PMD_SIZE; | |
9973c98e RZ |
839 | |
840 | if (get_block(inode, block, &bh, 0) != 0) | |
844f35db | 841 | return VM_FAULT_SIGBUS; |
9973c98e RZ |
842 | |
843 | if (!buffer_mapped(&bh) && write) { | |
844 | if (get_block(inode, block, &bh, 1) != 0) | |
845 | return VM_FAULT_SIGBUS; | |
846 | alloc = true; | |
847 | } | |
848 | ||
b2e0d162 | 849 | bdev = bh.b_bdev; |
844f35db MW |
850 | |
851 | /* | |
852 | * If the filesystem isn't willing to tell us the length of a hole, | |
853 | * just fall back to PTEs. Calling get_block 512 times in a loop | |
854 | * would be silly. | |
855 | */ | |
cbb38e41 DW |
856 | if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) { |
857 | dax_pmd_dbg(&bh, address, "allocated block too small"); | |
9973c98e RZ |
858 | return VM_FAULT_FALLBACK; |
859 | } | |
860 | ||
861 | /* | |
862 | * If we allocated new storage, make sure no process has any | |
863 | * zero pages covering this hole | |
864 | */ | |
865 | if (alloc) { | |
866 | loff_t lstart = pgoff << PAGE_SHIFT; | |
867 | loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */ | |
868 | ||
869 | truncate_pagecache_range(inode, lstart, lend); | |
cbb38e41 | 870 | } |
844f35db | 871 | |
de14b9cb | 872 | i_mmap_lock_read(mapping); |
46c043ed | 873 | |
84c4e5e6 MW |
874 | /* |
875 | * If a truncate happened while we were allocating blocks, we may | |
876 | * leave blocks allocated to the file that are beyond EOF. We can't | |
877 | * take i_mutex here, so just leave them hanging; they'll be freed | |
878 | * when the file is deleted. | |
879 | */ | |
844f35db MW |
880 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
881 | if (pgoff >= size) { | |
882 | result = VM_FAULT_SIGBUS; | |
883 | goto out; | |
884 | } | |
cbb38e41 | 885 | if ((pgoff | PG_PMD_COLOUR) >= size) { |
de14b9cb RZ |
886 | dax_pmd_dbg(&bh, address, |
887 | "offset + huge page size > file size"); | |
844f35db | 888 | goto fallback; |
cbb38e41 | 889 | } |
844f35db | 890 | |
844f35db | 891 | if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) { |
844f35db | 892 | spinlock_t *ptl; |
d295e341 | 893 | pmd_t entry; |
844f35db | 894 | struct page *zero_page = get_huge_zero_page(); |
d295e341 | 895 | |
cbb38e41 DW |
896 | if (unlikely(!zero_page)) { |
897 | dax_pmd_dbg(&bh, address, "no zero page"); | |
844f35db | 898 | goto fallback; |
cbb38e41 | 899 | } |
844f35db | 900 | |
d295e341 KS |
901 | ptl = pmd_lock(vma->vm_mm, pmd); |
902 | if (!pmd_none(*pmd)) { | |
903 | spin_unlock(ptl); | |
cbb38e41 | 904 | dax_pmd_dbg(&bh, address, "pmd already present"); |
d295e341 KS |
905 | goto fallback; |
906 | } | |
907 | ||
cbb38e41 DW |
908 | dev_dbg(part_to_dev(bdev->bd_part), |
909 | "%s: %s addr: %lx pfn: <zero> sect: %llx\n", | |
910 | __func__, current->comm, address, | |
911 | (unsigned long long) to_sector(&bh, inode)); | |
912 | ||
d295e341 KS |
913 | entry = mk_pmd(zero_page, vma->vm_page_prot); |
914 | entry = pmd_mkhuge(entry); | |
915 | set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry); | |
844f35db | 916 | result = VM_FAULT_NOPAGE; |
d295e341 | 917 | spin_unlock(ptl); |
844f35db | 918 | } else { |
b2e0d162 DW |
919 | struct blk_dax_ctl dax = { |
920 | .sector = to_sector(&bh, inode), | |
921 | .size = PMD_SIZE, | |
922 | }; | |
923 | long length = dax_map_atomic(bdev, &dax); | |
924 | ||
844f35db MW |
925 | if (length < 0) { |
926 | result = VM_FAULT_SIGBUS; | |
927 | goto out; | |
928 | } | |
cbb38e41 DW |
929 | if (length < PMD_SIZE) { |
930 | dax_pmd_dbg(&bh, address, "dax-length too small"); | |
931 | dax_unmap_atomic(bdev, &dax); | |
932 | goto fallback; | |
933 | } | |
934 | if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) { | |
935 | dax_pmd_dbg(&bh, address, "pfn unaligned"); | |
b2e0d162 | 936 | dax_unmap_atomic(bdev, &dax); |
844f35db | 937 | goto fallback; |
b2e0d162 | 938 | } |
844f35db | 939 | |
c046c321 | 940 | if (!pfn_t_devmap(dax.pfn)) { |
b2e0d162 | 941 | dax_unmap_atomic(bdev, &dax); |
cbb38e41 | 942 | dax_pmd_dbg(&bh, address, "pfn not in memmap"); |
152d7bd8 | 943 | goto fallback; |
b2e0d162 | 944 | } |
152d7bd8 | 945 | |
0f90cc66 | 946 | if (buffer_unwritten(&bh) || buffer_new(&bh)) { |
b2e0d162 | 947 | clear_pmem(dax.addr, PMD_SIZE); |
0f90cc66 RZ |
948 | wmb_pmem(); |
949 | count_vm_event(PGMAJFAULT); | |
950 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); | |
951 | result |= VM_FAULT_MAJOR; | |
952 | } | |
b2e0d162 | 953 | dax_unmap_atomic(bdev, &dax); |
0f90cc66 | 954 | |
9973c98e RZ |
955 | /* |
956 | * For PTE faults we insert a radix tree entry for reads, and | |
957 | * leave it clean. Then on the first write we dirty the radix | |
958 | * tree entry via the dax_pfn_mkwrite() path. This sequence | |
959 | * allows the dax_pfn_mkwrite() call to be simpler and avoid a | |
960 | * call into get_block() to translate the pgoff to a sector in | |
961 | * order to be able to create a new radix tree entry. | |
962 | * | |
963 | * The PMD path doesn't have an equivalent to | |
964 | * dax_pfn_mkwrite(), though, so for a read followed by a | |
965 | * write we traverse all the way through __dax_pmd_fault() | |
966 | * twice. This means we can just skip inserting a radix tree | |
967 | * entry completely on the initial read and just wait until | |
968 | * the write to insert a dirty entry. | |
969 | */ | |
970 | if (write) { | |
971 | error = dax_radix_entry(mapping, pgoff, dax.sector, | |
972 | true, true); | |
973 | if (error) { | |
974 | dax_pmd_dbg(&bh, address, | |
975 | "PMD radix insertion failed"); | |
976 | goto fallback; | |
977 | } | |
978 | } | |
979 | ||
cbb38e41 DW |
980 | dev_dbg(part_to_dev(bdev->bd_part), |
981 | "%s: %s addr: %lx pfn: %lx sect: %llx\n", | |
982 | __func__, current->comm, address, | |
983 | pfn_t_to_pfn(dax.pfn), | |
984 | (unsigned long long) dax.sector); | |
34c0fd54 | 985 | result |= vmf_insert_pfn_pmd(vma, address, pmd, |
f25748e3 | 986 | dax.pfn, write); |
844f35db MW |
987 | } |
988 | ||
989 | out: | |
0f90cc66 RZ |
990 | i_mmap_unlock_read(mapping); |
991 | ||
844f35db MW |
992 | if (buffer_unwritten(&bh)) |
993 | complete_unwritten(&bh, !(result & VM_FAULT_ERROR)); | |
994 | ||
995 | return result; | |
996 | ||
997 | fallback: | |
998 | count_vm_event(THP_FAULT_FALLBACK); | |
999 | result = VM_FAULT_FALLBACK; | |
1000 | goto out; | |
1001 | } | |
1002 | EXPORT_SYMBOL_GPL(__dax_pmd_fault); | |
1003 | ||
1004 | /** | |
1005 | * dax_pmd_fault - handle a PMD fault on a DAX file | |
1006 | * @vma: The virtual memory area where the fault occurred | |
1007 | * @vmf: The description of the fault | |
1008 | * @get_block: The filesystem method used to translate file offsets to blocks | |
1009 | * | |
1010 | * When a page fault occurs, filesystems may call this helper in their | |
1011 | * pmd_fault handler for DAX files. | |
1012 | */ | |
1013 | int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, | |
1014 | pmd_t *pmd, unsigned int flags, get_block_t get_block, | |
1015 | dax_iodone_t complete_unwritten) | |
1016 | { | |
1017 | int result; | |
1018 | struct super_block *sb = file_inode(vma->vm_file)->i_sb; | |
1019 | ||
1020 | if (flags & FAULT_FLAG_WRITE) { | |
1021 | sb_start_pagefault(sb); | |
1022 | file_update_time(vma->vm_file); | |
1023 | } | |
1024 | result = __dax_pmd_fault(vma, address, pmd, flags, get_block, | |
1025 | complete_unwritten); | |
1026 | if (flags & FAULT_FLAG_WRITE) | |
1027 | sb_end_pagefault(sb); | |
1028 | ||
1029 | return result; | |
1030 | } | |
1031 | EXPORT_SYMBOL_GPL(dax_pmd_fault); | |
dd8a2b6c | 1032 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
844f35db | 1033 | |
0e3b210c BH |
1034 | /** |
1035 | * dax_pfn_mkwrite - handle first write to DAX page | |
1036 | * @vma: The virtual memory area where the fault occurred | |
1037 | * @vmf: The description of the fault | |
0e3b210c BH |
1038 | */ |
1039 | int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1040 | { | |
9973c98e | 1041 | struct file *file = vma->vm_file; |
0e3b210c | 1042 | |
9973c98e RZ |
1043 | /* |
1044 | * We pass NO_SECTOR to dax_radix_entry() because we expect that a | |
1045 | * RADIX_DAX_PTE entry already exists in the radix tree from a | |
1046 | * previous call to __dax_fault(). We just want to look up that PTE | |
1047 | * entry using vmf->pgoff and make sure the dirty tag is set. This | |
1048 | * saves us from having to make a call to get_block() here to look | |
1049 | * up the sector. | |
1050 | */ | |
1051 | dax_radix_entry(file->f_mapping, vmf->pgoff, NO_SECTOR, false, true); | |
0e3b210c BH |
1052 | return VM_FAULT_NOPAGE; |
1053 | } | |
1054 | EXPORT_SYMBOL_GPL(dax_pfn_mkwrite); | |
1055 | ||
4c0ccfef | 1056 | /** |
25726bc1 | 1057 | * dax_zero_page_range - zero a range within a page of a DAX file |
4c0ccfef MW |
1058 | * @inode: The file being truncated |
1059 | * @from: The file offset that is being truncated to | |
25726bc1 | 1060 | * @length: The number of bytes to zero |
4c0ccfef MW |
1061 | * @get_block: The filesystem method used to translate file offsets to blocks |
1062 | * | |
25726bc1 MW |
1063 | * This function can be called by a filesystem when it is zeroing part of a |
1064 | * page in a DAX file. This is intended for hole-punch operations. If | |
1065 | * you are truncating a file, the helper function dax_truncate_page() may be | |
1066 | * more convenient. | |
4c0ccfef MW |
1067 | * |
1068 | * We work in terms of PAGE_CACHE_SIZE here for commonality with | |
1069 | * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem | |
1070 | * took care of disposing of the unnecessary blocks. Even if the filesystem | |
1071 | * block size is smaller than PAGE_SIZE, we have to zero the rest of the page | |
25726bc1 | 1072 | * since the file might be mmapped. |
4c0ccfef | 1073 | */ |
25726bc1 MW |
1074 | int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length, |
1075 | get_block_t get_block) | |
4c0ccfef MW |
1076 | { |
1077 | struct buffer_head bh; | |
1078 | pgoff_t index = from >> PAGE_CACHE_SHIFT; | |
1079 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
4c0ccfef MW |
1080 | int err; |
1081 | ||
1082 | /* Block boundary? Nothing to do */ | |
1083 | if (!length) | |
1084 | return 0; | |
25726bc1 | 1085 | BUG_ON((offset + length) > PAGE_CACHE_SIZE); |
4c0ccfef MW |
1086 | |
1087 | memset(&bh, 0, sizeof(bh)); | |
eab95db6 | 1088 | bh.b_bdev = inode->i_sb->s_bdev; |
4c0ccfef MW |
1089 | bh.b_size = PAGE_CACHE_SIZE; |
1090 | err = get_block(inode, index, &bh, 0); | |
1091 | if (err < 0) | |
1092 | return err; | |
1093 | if (buffer_written(&bh)) { | |
b2e0d162 DW |
1094 | struct block_device *bdev = bh.b_bdev; |
1095 | struct blk_dax_ctl dax = { | |
1096 | .sector = to_sector(&bh, inode), | |
1097 | .size = PAGE_CACHE_SIZE, | |
1098 | }; | |
1099 | ||
1100 | if (dax_map_atomic(bdev, &dax) < 0) | |
1101 | return PTR_ERR(dax.addr); | |
1102 | clear_pmem(dax.addr + offset, length); | |
2765cfbb | 1103 | wmb_pmem(); |
b2e0d162 | 1104 | dax_unmap_atomic(bdev, &dax); |
4c0ccfef MW |
1105 | } |
1106 | ||
1107 | return 0; | |
1108 | } | |
25726bc1 MW |
1109 | EXPORT_SYMBOL_GPL(dax_zero_page_range); |
1110 | ||
1111 | /** | |
1112 | * dax_truncate_page - handle a partial page being truncated in a DAX file | |
1113 | * @inode: The file being truncated | |
1114 | * @from: The file offset that is being truncated to | |
1115 | * @get_block: The filesystem method used to translate file offsets to blocks | |
1116 | * | |
1117 | * Similar to block_truncate_page(), this function can be called by a | |
1118 | * filesystem when it is truncating a DAX file to handle the partial page. | |
1119 | * | |
1120 | * We work in terms of PAGE_CACHE_SIZE here for commonality with | |
1121 | * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem | |
1122 | * took care of disposing of the unnecessary blocks. Even if the filesystem | |
1123 | * block size is smaller than PAGE_SIZE, we have to zero the rest of the page | |
1124 | * since the file might be mmapped. | |
1125 | */ | |
1126 | int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block) | |
1127 | { | |
1128 | unsigned length = PAGE_CACHE_ALIGN(from) - from; | |
1129 | return dax_zero_page_range(inode, from, length, get_block); | |
1130 | } | |
4c0ccfef | 1131 | EXPORT_SYMBOL_GPL(dax_truncate_page); |