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 | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 27 | #include <linux/pmem.h> |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 28 | #include <linux/sched.h> |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 29 | #include <linux/uio.h> |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 30 | #include <linux/vmstat.h> |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 31 | #include <linux/pfn_t.h> |
Dan Williams | 0e749e5 | 2016-01-15 16:55:53 -0800 | [diff] [blame] | 32 | #include <linux/sizes.h> |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 33 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 34 | static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax) |
| 35 | { |
| 36 | struct request_queue *q = bdev->bd_queue; |
| 37 | long rc = -EIO; |
| 38 | |
| 39 | dax->addr = (void __pmem *) ERR_PTR(-EIO); |
| 40 | if (blk_queue_enter(q, true) != 0) |
| 41 | return rc; |
| 42 | |
| 43 | rc = bdev_direct_access(bdev, dax); |
| 44 | if (rc < 0) { |
| 45 | dax->addr = (void __pmem *) ERR_PTR(rc); |
| 46 | blk_queue_exit(q); |
| 47 | return rc; |
| 48 | } |
| 49 | return rc; |
| 50 | } |
| 51 | |
| 52 | static void dax_unmap_atomic(struct block_device *bdev, |
| 53 | const struct blk_dax_ctl *dax) |
| 54 | { |
| 55 | if (IS_ERR(dax->addr)) |
| 56 | return; |
| 57 | blk_queue_exit(bdev->bd_queue); |
| 58 | } |
| 59 | |
Dave Chinner | 1ca1915 | 2015-11-03 12:37:00 +1100 | [diff] [blame] | 60 | /* |
| 61 | * dax_clear_blocks() is called from within transaction context from XFS, |
| 62 | * and hence this means the stack from this point must follow GFP_NOFS |
| 63 | * semantics for all operations. |
| 64 | */ |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 65 | int dax_clear_blocks(struct inode *inode, sector_t block, long _size) |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 66 | { |
| 67 | struct block_device *bdev = inode->i_sb->s_bdev; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 68 | struct blk_dax_ctl dax = { |
| 69 | .sector = block << (inode->i_blkbits - 9), |
| 70 | .size = _size, |
| 71 | }; |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 72 | |
| 73 | might_sleep(); |
| 74 | do { |
Dan Williams | 0e749e5 | 2016-01-15 16:55:53 -0800 | [diff] [blame] | 75 | long count, sz; |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 76 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 77 | count = dax_map_atomic(bdev, &dax); |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 78 | if (count < 0) |
| 79 | return count; |
Dan Williams | 0e749e5 | 2016-01-15 16:55:53 -0800 | [diff] [blame] | 80 | sz = min_t(long, count, SZ_128K); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 81 | clear_pmem(dax.addr, sz); |
| 82 | dax.size -= sz; |
| 83 | dax.sector += sz / 512; |
| 84 | dax_unmap_atomic(bdev, &dax); |
Dan Williams | 0e749e5 | 2016-01-15 16:55:53 -0800 | [diff] [blame] | 85 | cond_resched(); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 86 | } while (dax.size); |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 87 | |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 88 | wmb_pmem(); |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | EXPORT_SYMBOL_GPL(dax_clear_blocks); |
| 92 | |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 93 | /* 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] | 94 | static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first, |
| 95 | loff_t pos, loff_t end) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 96 | { |
| 97 | loff_t final = end - pos + first; /* The final byte of the buffer */ |
| 98 | |
| 99 | if (first > 0) |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 100 | clear_pmem(addr, first); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 101 | if (final < size) |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 102 | clear_pmem(addr + final, size - final); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static bool buffer_written(struct buffer_head *bh) |
| 106 | { |
| 107 | return buffer_mapped(bh) && !buffer_unwritten(bh); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * When ext4 encounters a hole, it returns without modifying the buffer_head |
| 112 | * which means that we can't trust b_size. To cope with this, we set b_state |
| 113 | * to 0 before calling get_block and, if any bit is set, we know we can trust |
| 114 | * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is |
| 115 | * and would save us time calling get_block repeatedly. |
| 116 | */ |
| 117 | static bool buffer_size_valid(struct buffer_head *bh) |
| 118 | { |
| 119 | return bh->b_state != 0; |
| 120 | } |
| 121 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 122 | |
| 123 | static sector_t to_sector(const struct buffer_head *bh, |
| 124 | const struct inode *inode) |
| 125 | { |
| 126 | sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9); |
| 127 | |
| 128 | return sector; |
| 129 | } |
| 130 | |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 131 | static ssize_t dax_io(struct inode *inode, struct iov_iter *iter, |
| 132 | loff_t start, loff_t end, get_block_t get_block, |
| 133 | struct buffer_head *bh) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 134 | { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 135 | loff_t pos = start, max = start, bh_max = start; |
| 136 | bool hole = false, need_wmb = false; |
| 137 | struct block_device *bdev = NULL; |
| 138 | int rw = iov_iter_rw(iter), rc; |
| 139 | long map_len = 0; |
| 140 | struct blk_dax_ctl dax = { |
| 141 | .addr = (void __pmem *) ERR_PTR(-EIO), |
| 142 | }; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 143 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 144 | if (rw == READ) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 145 | end = min(end, i_size_read(inode)); |
| 146 | |
| 147 | while (pos < end) { |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 148 | size_t len; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 149 | if (pos == max) { |
| 150 | unsigned blkbits = inode->i_blkbits; |
Jeff Moyer | e94f5a2 | 2015-08-14 16:15:31 -0400 | [diff] [blame] | 151 | long page = pos >> PAGE_SHIFT; |
| 152 | sector_t block = page << (PAGE_SHIFT - blkbits); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 153 | unsigned first = pos - (block << blkbits); |
| 154 | long size; |
| 155 | |
| 156 | if (pos == bh_max) { |
| 157 | bh->b_size = PAGE_ALIGN(end - pos); |
| 158 | bh->b_state = 0; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 159 | rc = get_block(inode, block, bh, rw == WRITE); |
| 160 | if (rc) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 161 | break; |
| 162 | if (!buffer_size_valid(bh)) |
| 163 | bh->b_size = 1 << blkbits; |
| 164 | bh_max = pos - first + bh->b_size; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 165 | bdev = bh->b_bdev; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 166 | } else { |
| 167 | unsigned done = bh->b_size - |
| 168 | (bh_max - (pos - first)); |
| 169 | bh->b_blocknr += done >> blkbits; |
| 170 | bh->b_size -= done; |
| 171 | } |
| 172 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 173 | hole = rw == READ && !buffer_written(bh); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 174 | if (hole) { |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 175 | size = bh->b_size - first; |
| 176 | } else { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 177 | dax_unmap_atomic(bdev, &dax); |
| 178 | dax.sector = to_sector(bh, inode); |
| 179 | dax.size = bh->b_size; |
| 180 | map_len = dax_map_atomic(bdev, &dax); |
| 181 | if (map_len < 0) { |
| 182 | rc = map_len; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 183 | break; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 184 | } |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 185 | if (buffer_unwritten(bh) || buffer_new(bh)) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 186 | dax_new_buf(dax.addr, map_len, first, |
| 187 | pos, end); |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 188 | need_wmb = true; |
| 189 | } |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 190 | dax.addr += first; |
| 191 | size = map_len - first; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 192 | } |
| 193 | max = min(pos + size, end); |
| 194 | } |
| 195 | |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 196 | if (iov_iter_rw(iter) == WRITE) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 197 | len = copy_from_iter_pmem(dax.addr, max - pos, iter); |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 198 | need_wmb = true; |
| 199 | } else if (!hole) |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 200 | len = copy_to_iter((void __force *) dax.addr, max - pos, |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 201 | iter); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 202 | else |
| 203 | len = iov_iter_zero(max - pos, iter); |
| 204 | |
Al Viro | cadfbb6 | 2015-11-10 19:42:49 -0700 | [diff] [blame] | 205 | if (!len) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 206 | rc = -EFAULT; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 207 | break; |
Al Viro | cadfbb6 | 2015-11-10 19:42:49 -0700 | [diff] [blame] | 208 | } |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 209 | |
| 210 | pos += len; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 211 | if (!IS_ERR(dax.addr)) |
| 212 | dax.addr += len; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 215 | if (need_wmb) |
| 216 | wmb_pmem(); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 217 | dax_unmap_atomic(bdev, &dax); |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 218 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 219 | return (pos == start) ? rc : pos - start; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * dax_do_io - Perform I/O to a DAX file |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 224 | * @iocb: The control block for this I/O |
| 225 | * @inode: The file which the I/O is directed at |
| 226 | * @iter: The addresses to do I/O from or to |
| 227 | * @pos: The file offset where the I/O starts |
| 228 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 229 | * @end_io: A filesystem callback for I/O completion |
| 230 | * @flags: See below |
| 231 | * |
| 232 | * This function uses the same locking scheme as do_blockdev_direct_IO: |
| 233 | * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the |
| 234 | * caller for writes. For reads, we take and release the i_mutex ourselves. |
| 235 | * If DIO_LOCKING is not set, the filesystem takes care of its own locking. |
| 236 | * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O |
| 237 | * is in progress. |
| 238 | */ |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 239 | ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode, |
| 240 | struct iov_iter *iter, loff_t pos, get_block_t get_block, |
| 241 | dio_iodone_t end_io, int flags) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 242 | { |
| 243 | struct buffer_head bh; |
| 244 | ssize_t retval = -EINVAL; |
| 245 | loff_t end = pos + iov_iter_count(iter); |
| 246 | |
| 247 | memset(&bh, 0, sizeof(bh)); |
| 248 | |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 249 | if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) { |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 250 | struct address_space *mapping = inode->i_mapping; |
| 251 | mutex_lock(&inode->i_mutex); |
| 252 | retval = filemap_write_and_wait_range(mapping, pos, end - 1); |
| 253 | if (retval) { |
| 254 | mutex_unlock(&inode->i_mutex); |
| 255 | goto out; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* Protects against truncate */ |
Matthew Wilcox | bbab37d | 2015-07-03 10:40:42 -0400 | [diff] [blame] | 260 | if (!(flags & DIO_SKIP_DIO_COUNT)) |
| 261 | inode_dio_begin(inode); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 262 | |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 263 | retval = dax_io(inode, iter, pos, end, get_block, &bh); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 264 | |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 265 | if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 266 | mutex_unlock(&inode->i_mutex); |
| 267 | |
| 268 | if ((retval > 0) && end_io) |
| 269 | end_io(iocb, pos, retval, bh.b_private); |
| 270 | |
Matthew Wilcox | bbab37d | 2015-07-03 10:40:42 -0400 | [diff] [blame] | 271 | if (!(flags & DIO_SKIP_DIO_COUNT)) |
| 272 | inode_dio_end(inode); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 273 | out: |
| 274 | return retval; |
| 275 | } |
| 276 | EXPORT_SYMBOL_GPL(dax_do_io); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * The user has performed a load from a hole in the file. Allocating |
| 280 | * a new page in the file would cause excessive storage usage for |
| 281 | * workloads with sparse files. We allocate a page cache page instead. |
| 282 | * We'll kick it out of the page cache if it's ever written to, |
| 283 | * otherwise it will simply fall out of the page cache under memory |
| 284 | * pressure without ever having been dirtied. |
| 285 | */ |
| 286 | static int dax_load_hole(struct address_space *mapping, struct page *page, |
| 287 | struct vm_fault *vmf) |
| 288 | { |
| 289 | unsigned long size; |
| 290 | struct inode *inode = mapping->host; |
| 291 | if (!page) |
| 292 | page = find_or_create_page(mapping, vmf->pgoff, |
| 293 | GFP_KERNEL | __GFP_ZERO); |
| 294 | if (!page) |
| 295 | return VM_FAULT_OOM; |
| 296 | /* Recheck i_size under page lock to avoid truncate race */ |
| 297 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 298 | if (vmf->pgoff >= size) { |
| 299 | unlock_page(page); |
| 300 | page_cache_release(page); |
| 301 | return VM_FAULT_SIGBUS; |
| 302 | } |
| 303 | |
| 304 | vmf->page = page; |
| 305 | return VM_FAULT_LOCKED; |
| 306 | } |
| 307 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 308 | static int copy_user_bh(struct page *to, struct inode *inode, |
| 309 | struct buffer_head *bh, unsigned long vaddr) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 310 | { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 311 | struct blk_dax_ctl dax = { |
| 312 | .sector = to_sector(bh, inode), |
| 313 | .size = bh->b_size, |
| 314 | }; |
| 315 | struct block_device *bdev = bh->b_bdev; |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 316 | void *vto; |
| 317 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 318 | if (dax_map_atomic(bdev, &dax) < 0) |
| 319 | return PTR_ERR(dax.addr); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 320 | vto = kmap_atomic(to); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 321 | copy_user_page(vto, (void __force *)dax.addr, vaddr, to); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 322 | kunmap_atomic(vto); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 323 | dax_unmap_atomic(bdev, &dax); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh, |
| 328 | struct vm_area_struct *vma, struct vm_fault *vmf) |
| 329 | { |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 330 | unsigned long vaddr = (unsigned long)vmf->virtual_address; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 331 | struct address_space *mapping = inode->i_mapping; |
| 332 | struct block_device *bdev = bh->b_bdev; |
| 333 | struct blk_dax_ctl dax = { |
| 334 | .sector = to_sector(bh, inode), |
| 335 | .size = bh->b_size, |
| 336 | }; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 337 | pgoff_t size; |
| 338 | int error; |
| 339 | |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 340 | i_mmap_lock_read(mapping); |
| 341 | |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 342 | /* |
| 343 | * Check truncate didn't happen while we were allocating a block. |
| 344 | * If it did, this block may or may not be still allocated to the |
| 345 | * file. We can't tell the filesystem to free it because we can't |
| 346 | * take i_mutex here. In the worst case, the file still has blocks |
| 347 | * allocated past the end of the file. |
| 348 | */ |
| 349 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 350 | if (unlikely(vmf->pgoff >= size)) { |
| 351 | error = -EIO; |
| 352 | goto out; |
| 353 | } |
| 354 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 355 | if (dax_map_atomic(bdev, &dax) < 0) { |
| 356 | error = PTR_ERR(dax.addr); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 357 | goto out; |
| 358 | } |
| 359 | |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 360 | if (buffer_unwritten(bh) || buffer_new(bh)) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 361 | clear_pmem(dax.addr, PAGE_SIZE); |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 362 | wmb_pmem(); |
| 363 | } |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 364 | dax_unmap_atomic(bdev, &dax); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 365 | |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame^] | 366 | error = vm_insert_mixed(vma, vaddr, dax.pfn); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 367 | |
| 368 | out: |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 369 | i_mmap_unlock_read(mapping); |
| 370 | |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 371 | return error; |
| 372 | } |
| 373 | |
Dave Chinner | ce5c5d5 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 374 | /** |
| 375 | * __dax_fault - handle a page fault on a DAX file |
| 376 | * @vma: The virtual memory area where the fault occurred |
| 377 | * @vmf: The description of the fault |
| 378 | * @get_block: The filesystem method used to translate file offsets to blocks |
Dave Chinner | b2442c5 | 2015-07-29 11:48:00 +1000 | [diff] [blame] | 379 | * @complete_unwritten: The filesystem method used to convert unwritten blocks |
| 380 | * to written so the data written to them is exposed. This is required for |
| 381 | * required by write faults for filesystems that will return unwritten |
| 382 | * extent mappings from @get_block, but it is optional for reads as |
| 383 | * dax_insert_mapping() will always zero unwritten blocks. If the fs does |
| 384 | * not support unwritten extents, the it should pass NULL. |
Dave Chinner | ce5c5d5 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 385 | * |
| 386 | * When a page fault occurs, filesystems may call this helper in their |
| 387 | * fault handler for DAX files. __dax_fault() assumes the caller has done all |
| 388 | * the necessary locking for the page fault to proceed successfully. |
| 389 | */ |
| 390 | int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, |
Dave Chinner | e842f29 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 391 | get_block_t get_block, dax_iodone_t complete_unwritten) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 392 | { |
| 393 | struct file *file = vma->vm_file; |
| 394 | struct address_space *mapping = file->f_mapping; |
| 395 | struct inode *inode = mapping->host; |
| 396 | struct page *page; |
| 397 | struct buffer_head bh; |
| 398 | unsigned long vaddr = (unsigned long)vmf->virtual_address; |
| 399 | unsigned blkbits = inode->i_blkbits; |
| 400 | sector_t block; |
| 401 | pgoff_t size; |
| 402 | int error; |
| 403 | int major = 0; |
| 404 | |
| 405 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 406 | if (vmf->pgoff >= size) |
| 407 | return VM_FAULT_SIGBUS; |
| 408 | |
| 409 | memset(&bh, 0, sizeof(bh)); |
| 410 | block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits); |
| 411 | bh.b_size = PAGE_SIZE; |
| 412 | |
| 413 | repeat: |
| 414 | page = find_get_page(mapping, vmf->pgoff); |
| 415 | if (page) { |
| 416 | if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) { |
| 417 | page_cache_release(page); |
| 418 | return VM_FAULT_RETRY; |
| 419 | } |
| 420 | if (unlikely(page->mapping != mapping)) { |
| 421 | unlock_page(page); |
| 422 | page_cache_release(page); |
| 423 | goto repeat; |
| 424 | } |
| 425 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 426 | if (unlikely(vmf->pgoff >= size)) { |
| 427 | /* |
| 428 | * We have a struct page covering a hole in the file |
| 429 | * from a read fault and we've raced with a truncate |
| 430 | */ |
| 431 | error = -EIO; |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 432 | goto unlock_page; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
| 436 | error = get_block(inode, block, &bh, 0); |
| 437 | if (!error && (bh.b_size < PAGE_SIZE)) |
| 438 | error = -EIO; /* fs corruption? */ |
| 439 | if (error) |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 440 | goto unlock_page; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 441 | |
| 442 | if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) { |
| 443 | if (vmf->flags & FAULT_FLAG_WRITE) { |
| 444 | error = get_block(inode, block, &bh, 1); |
| 445 | count_vm_event(PGMAJFAULT); |
| 446 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); |
| 447 | major = VM_FAULT_MAJOR; |
| 448 | if (!error && (bh.b_size < PAGE_SIZE)) |
| 449 | error = -EIO; |
| 450 | if (error) |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 451 | goto unlock_page; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 452 | } else { |
| 453 | return dax_load_hole(mapping, page, vmf); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (vmf->cow_page) { |
| 458 | struct page *new_page = vmf->cow_page; |
| 459 | if (buffer_written(&bh)) |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 460 | error = copy_user_bh(new_page, inode, &bh, vaddr); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 461 | else |
| 462 | clear_user_highpage(new_page, vaddr); |
| 463 | if (error) |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 464 | goto unlock_page; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 465 | vmf->page = page; |
| 466 | if (!page) { |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 467 | i_mmap_lock_read(mapping); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 468 | /* Check we didn't race with truncate */ |
| 469 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> |
| 470 | PAGE_SHIFT; |
| 471 | if (vmf->pgoff >= size) { |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 472 | i_mmap_unlock_read(mapping); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 473 | error = -EIO; |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 474 | goto out; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | return VM_FAULT_LOCKED; |
| 478 | } |
| 479 | |
| 480 | /* Check we didn't race with a read fault installing a new page */ |
| 481 | if (!page && major) |
| 482 | page = find_lock_page(mapping, vmf->pgoff); |
| 483 | |
| 484 | if (page) { |
| 485 | unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT, |
| 486 | PAGE_CACHE_SIZE, 0); |
| 487 | delete_from_page_cache(page); |
| 488 | unlock_page(page); |
| 489 | page_cache_release(page); |
| 490 | } |
| 491 | |
Dave Chinner | e842f29 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 492 | /* |
| 493 | * If we successfully insert the new mapping over an unwritten extent, |
| 494 | * we need to ensure we convert the unwritten extent. If there is an |
| 495 | * error inserting the mapping, the filesystem needs to leave it as |
| 496 | * unwritten to prevent exposure of the stale underlying data to |
| 497 | * userspace, but we still need to call the completion function so |
| 498 | * the private resources on the mapping buffer can be released. We |
| 499 | * indicate what the callback should do via the uptodate variable, same |
| 500 | * as for normal BH based IO completions. |
| 501 | */ |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 502 | error = dax_insert_mapping(inode, &bh, vma, vmf); |
Dave Chinner | b2442c5 | 2015-07-29 11:48:00 +1000 | [diff] [blame] | 503 | if (buffer_unwritten(&bh)) { |
| 504 | if (complete_unwritten) |
| 505 | complete_unwritten(&bh, !error); |
| 506 | else |
| 507 | WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE)); |
| 508 | } |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 509 | |
| 510 | out: |
| 511 | if (error == -ENOMEM) |
| 512 | return VM_FAULT_OOM | major; |
| 513 | /* -EBUSY is fine, somebody else faulted on the same PTE */ |
| 514 | if ((error < 0) && (error != -EBUSY)) |
| 515 | return VM_FAULT_SIGBUS | major; |
| 516 | return VM_FAULT_NOPAGE | major; |
| 517 | |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 518 | unlock_page: |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 519 | if (page) { |
| 520 | unlock_page(page); |
| 521 | page_cache_release(page); |
| 522 | } |
| 523 | goto out; |
| 524 | } |
Dave Chinner | ce5c5d5 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 525 | EXPORT_SYMBOL(__dax_fault); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 526 | |
| 527 | /** |
| 528 | * dax_fault - handle a page fault on a DAX file |
| 529 | * @vma: The virtual memory area where the fault occurred |
| 530 | * @vmf: The description of the fault |
| 531 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 532 | * |
| 533 | * When a page fault occurs, filesystems may call this helper in their |
| 534 | * fault handler for DAX files. |
| 535 | */ |
| 536 | int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, |
Dave Chinner | e842f29 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 537 | get_block_t get_block, dax_iodone_t complete_unwritten) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 538 | { |
| 539 | int result; |
| 540 | struct super_block *sb = file_inode(vma->vm_file)->i_sb; |
| 541 | |
| 542 | if (vmf->flags & FAULT_FLAG_WRITE) { |
| 543 | sb_start_pagefault(sb); |
| 544 | file_update_time(vma->vm_file); |
| 545 | } |
Dave Chinner | ce5c5d5 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 546 | result = __dax_fault(vma, vmf, get_block, complete_unwritten); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 547 | if (vmf->flags & FAULT_FLAG_WRITE) |
| 548 | sb_end_pagefault(sb); |
| 549 | |
| 550 | return result; |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(dax_fault); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 553 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 554 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 555 | /* |
| 556 | * The 'colour' (ie low bits) within a PMD of a page offset. This comes up |
| 557 | * more often than one might expect in the below function. |
| 558 | */ |
| 559 | #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) |
| 560 | |
| 561 | int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, |
| 562 | pmd_t *pmd, unsigned int flags, get_block_t get_block, |
| 563 | dax_iodone_t complete_unwritten) |
| 564 | { |
| 565 | struct file *file = vma->vm_file; |
| 566 | struct address_space *mapping = file->f_mapping; |
| 567 | struct inode *inode = mapping->host; |
| 568 | struct buffer_head bh; |
| 569 | unsigned blkbits = inode->i_blkbits; |
| 570 | unsigned long pmd_addr = address & PMD_MASK; |
| 571 | bool write = flags & FAULT_FLAG_WRITE; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 572 | struct block_device *bdev; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 573 | pgoff_t size, pgoff; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 574 | sector_t block; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 575 | int result = 0; |
| 576 | |
Dan Williams | ee82c9e | 2015-11-15 16:06:32 -0800 | [diff] [blame] | 577 | /* dax pmd mappings are broken wrt gup and fork */ |
| 578 | if (!IS_ENABLED(CONFIG_FS_DAX_PMD)) |
| 579 | return VM_FAULT_FALLBACK; |
| 580 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 581 | /* Fall back to PTEs if we're going to COW */ |
Toshi Kani | 59bf4fb | 2016-01-15 16:56:05 -0800 | [diff] [blame] | 582 | if (write && !(vma->vm_flags & VM_SHARED)) { |
| 583 | split_huge_pmd(vma, pmd, address); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 584 | return VM_FAULT_FALLBACK; |
Toshi Kani | 59bf4fb | 2016-01-15 16:56:05 -0800 | [diff] [blame] | 585 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 586 | /* If the PMD would extend outside the VMA */ |
| 587 | if (pmd_addr < vma->vm_start) |
| 588 | return VM_FAULT_FALLBACK; |
| 589 | if ((pmd_addr + PMD_SIZE) > vma->vm_end) |
| 590 | return VM_FAULT_FALLBACK; |
| 591 | |
Matthew Wilcox | 3fdd1b47 | 2015-09-08 14:59:39 -0700 | [diff] [blame] | 592 | pgoff = linear_page_index(vma, pmd_addr); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 593 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 594 | if (pgoff >= size) |
| 595 | return VM_FAULT_SIGBUS; |
| 596 | /* If the PMD would cover blocks out of the file */ |
| 597 | if ((pgoff | PG_PMD_COLOUR) >= size) |
| 598 | return VM_FAULT_FALLBACK; |
| 599 | |
| 600 | memset(&bh, 0, sizeof(bh)); |
| 601 | block = (sector_t)pgoff << (PAGE_SHIFT - blkbits); |
| 602 | |
| 603 | bh.b_size = PMD_SIZE; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 604 | if (get_block(inode, block, &bh, write) != 0) |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 605 | return VM_FAULT_SIGBUS; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 606 | bdev = bh.b_bdev; |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 607 | i_mmap_lock_read(mapping); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * If the filesystem isn't willing to tell us the length of a hole, |
| 611 | * just fall back to PTEs. Calling get_block 512 times in a loop |
| 612 | * would be silly. |
| 613 | */ |
| 614 | if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) |
| 615 | goto fallback; |
| 616 | |
Kirill A. Shutemov | 46c043e | 2015-09-08 14:59:42 -0700 | [diff] [blame] | 617 | /* |
| 618 | * If we allocated new storage, make sure no process has any |
| 619 | * zero pages covering this hole |
| 620 | */ |
| 621 | if (buffer_new(&bh)) { |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 622 | i_mmap_unlock_read(mapping); |
Kirill A. Shutemov | 46c043e | 2015-09-08 14:59:42 -0700 | [diff] [blame] | 623 | unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0); |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 624 | i_mmap_lock_read(mapping); |
Kirill A. Shutemov | 46c043e | 2015-09-08 14:59:42 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Matthew Wilcox | 84c4e5e | 2015-09-08 14:59:17 -0700 | [diff] [blame] | 627 | /* |
| 628 | * If a truncate happened while we were allocating blocks, we may |
| 629 | * leave blocks allocated to the file that are beyond EOF. We can't |
| 630 | * take i_mutex here, so just leave them hanging; they'll be freed |
| 631 | * when the file is deleted. |
| 632 | */ |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 633 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 634 | if (pgoff >= size) { |
| 635 | result = VM_FAULT_SIGBUS; |
| 636 | goto out; |
| 637 | } |
| 638 | if ((pgoff | PG_PMD_COLOUR) >= size) |
| 639 | goto fallback; |
| 640 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 641 | if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) { |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 642 | spinlock_t *ptl; |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 643 | pmd_t entry; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 644 | struct page *zero_page = get_huge_zero_page(); |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 645 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 646 | if (unlikely(!zero_page)) |
| 647 | goto fallback; |
| 648 | |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 649 | ptl = pmd_lock(vma->vm_mm, pmd); |
| 650 | if (!pmd_none(*pmd)) { |
| 651 | spin_unlock(ptl); |
| 652 | goto fallback; |
| 653 | } |
| 654 | |
| 655 | entry = mk_pmd(zero_page, vma->vm_page_prot); |
| 656 | entry = pmd_mkhuge(entry); |
| 657 | set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 658 | result = VM_FAULT_NOPAGE; |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 659 | spin_unlock(ptl); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 660 | } else { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 661 | struct blk_dax_ctl dax = { |
| 662 | .sector = to_sector(&bh, inode), |
| 663 | .size = PMD_SIZE, |
| 664 | }; |
| 665 | long length = dax_map_atomic(bdev, &dax); |
| 666 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 667 | if (length < 0) { |
| 668 | result = VM_FAULT_SIGBUS; |
| 669 | goto out; |
| 670 | } |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 671 | if (length < PMD_SIZE |
| 672 | || (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR)) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 673 | dax_unmap_atomic(bdev, &dax); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 674 | goto fallback; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 675 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 676 | |
Dan Williams | 152d7bd | 2015-11-12 18:33:54 -0800 | [diff] [blame] | 677 | /* |
| 678 | * TODO: teach vmf_insert_pfn_pmd() to support |
| 679 | * 'pte_special' for pmds |
| 680 | */ |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 681 | if (pfn_t_has_page(dax.pfn)) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 682 | dax_unmap_atomic(bdev, &dax); |
Dan Williams | 152d7bd | 2015-11-12 18:33:54 -0800 | [diff] [blame] | 683 | goto fallback; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 684 | } |
Dan Williams | 152d7bd | 2015-11-12 18:33:54 -0800 | [diff] [blame] | 685 | |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 686 | if (buffer_unwritten(&bh) || buffer_new(&bh)) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 687 | clear_pmem(dax.addr, PMD_SIZE); |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 688 | wmb_pmem(); |
| 689 | count_vm_event(PGMAJFAULT); |
| 690 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); |
| 691 | result |= VM_FAULT_MAJOR; |
| 692 | } |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 693 | dax_unmap_atomic(bdev, &dax); |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 694 | |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 695 | result |= vmf_insert_pfn_pmd(vma, address, pmd, |
| 696 | pfn_t_to_pfn(dax.pfn), write); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | out: |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 700 | i_mmap_unlock_read(mapping); |
| 701 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 702 | if (buffer_unwritten(&bh)) |
| 703 | complete_unwritten(&bh, !(result & VM_FAULT_ERROR)); |
| 704 | |
| 705 | return result; |
| 706 | |
| 707 | fallback: |
| 708 | count_vm_event(THP_FAULT_FALLBACK); |
| 709 | result = VM_FAULT_FALLBACK; |
| 710 | goto out; |
| 711 | } |
| 712 | EXPORT_SYMBOL_GPL(__dax_pmd_fault); |
| 713 | |
| 714 | /** |
| 715 | * dax_pmd_fault - handle a PMD fault on a DAX file |
| 716 | * @vma: The virtual memory area where the fault occurred |
| 717 | * @vmf: The description of the fault |
| 718 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 719 | * |
| 720 | * When a page fault occurs, filesystems may call this helper in their |
| 721 | * pmd_fault handler for DAX files. |
| 722 | */ |
| 723 | int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, |
| 724 | pmd_t *pmd, unsigned int flags, get_block_t get_block, |
| 725 | dax_iodone_t complete_unwritten) |
| 726 | { |
| 727 | int result; |
| 728 | struct super_block *sb = file_inode(vma->vm_file)->i_sb; |
| 729 | |
| 730 | if (flags & FAULT_FLAG_WRITE) { |
| 731 | sb_start_pagefault(sb); |
| 732 | file_update_time(vma->vm_file); |
| 733 | } |
| 734 | result = __dax_pmd_fault(vma, address, pmd, flags, get_block, |
| 735 | complete_unwritten); |
| 736 | if (flags & FAULT_FLAG_WRITE) |
| 737 | sb_end_pagefault(sb); |
| 738 | |
| 739 | return result; |
| 740 | } |
| 741 | EXPORT_SYMBOL_GPL(dax_pmd_fault); |
Valentin Rothberg | dd8a2b6 | 2015-09-08 14:59:09 -0700 | [diff] [blame] | 742 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 743 | |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 744 | /** |
Boaz Harrosh | 0e3b210 | 2015-04-15 16:15:14 -0700 | [diff] [blame] | 745 | * dax_pfn_mkwrite - handle first write to DAX page |
| 746 | * @vma: The virtual memory area where the fault occurred |
| 747 | * @vmf: The description of the fault |
| 748 | * |
| 749 | */ |
| 750 | int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 751 | { |
| 752 | struct super_block *sb = file_inode(vma->vm_file)->i_sb; |
| 753 | |
| 754 | sb_start_pagefault(sb); |
| 755 | file_update_time(vma->vm_file); |
| 756 | sb_end_pagefault(sb); |
| 757 | return VM_FAULT_NOPAGE; |
| 758 | } |
| 759 | EXPORT_SYMBOL_GPL(dax_pfn_mkwrite); |
| 760 | |
| 761 | /** |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 762 | * 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] | 763 | * @inode: The file being truncated |
| 764 | * @from: The file offset that is being truncated to |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 765 | * @length: The number of bytes to zero |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 766 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 767 | * |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 768 | * This function can be called by a filesystem when it is zeroing part of a |
| 769 | * page in a DAX file. This is intended for hole-punch operations. If |
| 770 | * you are truncating a file, the helper function dax_truncate_page() may be |
| 771 | * more convenient. |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 772 | * |
| 773 | * We work in terms of PAGE_CACHE_SIZE here for commonality with |
| 774 | * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem |
| 775 | * took care of disposing of the unnecessary blocks. Even if the filesystem |
| 776 | * 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] | 777 | * since the file might be mmapped. |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 778 | */ |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 779 | int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length, |
| 780 | get_block_t get_block) |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 781 | { |
| 782 | struct buffer_head bh; |
| 783 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 784 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 785 | int err; |
| 786 | |
| 787 | /* Block boundary? Nothing to do */ |
| 788 | if (!length) |
| 789 | return 0; |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 790 | BUG_ON((offset + length) > PAGE_CACHE_SIZE); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 791 | |
| 792 | memset(&bh, 0, sizeof(bh)); |
| 793 | bh.b_size = PAGE_CACHE_SIZE; |
| 794 | err = get_block(inode, index, &bh, 0); |
| 795 | if (err < 0) |
| 796 | return err; |
| 797 | if (buffer_written(&bh)) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 798 | struct block_device *bdev = bh.b_bdev; |
| 799 | struct blk_dax_ctl dax = { |
| 800 | .sector = to_sector(&bh, inode), |
| 801 | .size = PAGE_CACHE_SIZE, |
| 802 | }; |
| 803 | |
| 804 | if (dax_map_atomic(bdev, &dax) < 0) |
| 805 | return PTR_ERR(dax.addr); |
| 806 | clear_pmem(dax.addr + offset, length); |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 807 | wmb_pmem(); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 808 | dax_unmap_atomic(bdev, &dax); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | return 0; |
| 812 | } |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 813 | EXPORT_SYMBOL_GPL(dax_zero_page_range); |
| 814 | |
| 815 | /** |
| 816 | * dax_truncate_page - handle a partial page being truncated in a DAX file |
| 817 | * @inode: The file being truncated |
| 818 | * @from: The file offset that is being truncated to |
| 819 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 820 | * |
| 821 | * Similar to block_truncate_page(), this function can be called by a |
| 822 | * filesystem when it is truncating a DAX file to handle the partial page. |
| 823 | * |
| 824 | * We work in terms of PAGE_CACHE_SIZE here for commonality with |
| 825 | * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem |
| 826 | * took care of disposing of the unnecessary blocks. Even if the filesystem |
| 827 | * block size is smaller than PAGE_SIZE, we have to zero the rest of the page |
| 828 | * since the file might be mmapped. |
| 829 | */ |
| 830 | int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block) |
| 831 | { |
| 832 | unsigned length = PAGE_CACHE_ALIGN(from) - from; |
| 833 | return dax_zero_page_range(inode, from, length, get_block); |
| 834 | } |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 835 | EXPORT_SYMBOL_GPL(dax_truncate_page); |