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