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