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