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 | |
Jan Kara | e804315 | 2016-05-12 18:29:16 +0200 | [diff] [blame] | 35 | /* |
| 36 | * We use lowest available bit in exceptional entry for locking, other two |
| 37 | * bits to determine entry type. In total 3 special bits. |
| 38 | */ |
| 39 | #define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 3) |
| 40 | #define RADIX_DAX_PTE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1)) |
| 41 | #define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2)) |
| 42 | #define RADIX_DAX_TYPE_MASK (RADIX_DAX_PTE | RADIX_DAX_PMD) |
| 43 | #define RADIX_DAX_TYPE(entry) ((unsigned long)entry & RADIX_DAX_TYPE_MASK) |
NeilBrown | e4b2749 | 2016-05-11 11:58:47 +0200 | [diff] [blame] | 44 | #define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT)) |
| 45 | #define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \ |
Jan Kara | e804315 | 2016-05-12 18:29:16 +0200 | [diff] [blame] | 46 | RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE) | \ |
| 47 | RADIX_TREE_EXCEPTIONAL_ENTRY)) |
NeilBrown | e4b2749 | 2016-05-11 11:58:47 +0200 | [diff] [blame] | 48 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 49 | /* We choose 4096 entries - same as per-zone page wait tables */ |
| 50 | #define DAX_WAIT_TABLE_BITS 12 |
| 51 | #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) |
| 52 | |
| 53 | wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; |
| 54 | |
| 55 | static int __init init_dax_wait_table(void) |
| 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) |
| 60 | init_waitqueue_head(wait_table + i); |
| 61 | return 0; |
| 62 | } |
| 63 | fs_initcall(init_dax_wait_table); |
| 64 | |
| 65 | static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping, |
| 66 | pgoff_t index) |
| 67 | { |
| 68 | unsigned long hash = hash_long((unsigned long)mapping ^ index, |
| 69 | DAX_WAIT_TABLE_BITS); |
| 70 | return wait_table + hash; |
| 71 | } |
| 72 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 73 | static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax) |
| 74 | { |
| 75 | struct request_queue *q = bdev->bd_queue; |
| 76 | long rc = -EIO; |
| 77 | |
| 78 | dax->addr = (void __pmem *) ERR_PTR(-EIO); |
| 79 | if (blk_queue_enter(q, true) != 0) |
| 80 | return rc; |
| 81 | |
| 82 | rc = bdev_direct_access(bdev, dax); |
| 83 | if (rc < 0) { |
| 84 | dax->addr = (void __pmem *) ERR_PTR(rc); |
| 85 | blk_queue_exit(q); |
| 86 | return rc; |
| 87 | } |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | static void dax_unmap_atomic(struct block_device *bdev, |
| 92 | const struct blk_dax_ctl *dax) |
| 93 | { |
| 94 | if (IS_ERR(dax->addr)) |
| 95 | return; |
| 96 | blk_queue_exit(bdev->bd_queue); |
| 97 | } |
| 98 | |
Dan Williams | d1a5f2b4 | 2016-01-28 20:25:31 -0800 | [diff] [blame] | 99 | struct page *read_dax_sector(struct block_device *bdev, sector_t n) |
| 100 | { |
| 101 | struct page *page = alloc_pages(GFP_KERNEL, 0); |
| 102 | struct blk_dax_ctl dax = { |
| 103 | .size = PAGE_SIZE, |
| 104 | .sector = n & ~((((int) PAGE_SIZE) / 512) - 1), |
| 105 | }; |
| 106 | long rc; |
| 107 | |
| 108 | if (!page) |
| 109 | return ERR_PTR(-ENOMEM); |
| 110 | |
| 111 | rc = dax_map_atomic(bdev, &dax); |
| 112 | if (rc < 0) |
| 113 | return ERR_PTR(rc); |
| 114 | memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE); |
| 115 | dax_unmap_atomic(bdev, &dax); |
| 116 | return page; |
| 117 | } |
| 118 | |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 119 | static bool buffer_written(struct buffer_head *bh) |
| 120 | { |
| 121 | return buffer_mapped(bh) && !buffer_unwritten(bh); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * When ext4 encounters a hole, it returns without modifying the buffer_head |
| 126 | * which means that we can't trust b_size. To cope with this, we set b_state |
| 127 | * to 0 before calling get_block and, if any bit is set, we know we can trust |
| 128 | * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is |
| 129 | * and would save us time calling get_block repeatedly. |
| 130 | */ |
| 131 | static bool buffer_size_valid(struct buffer_head *bh) |
| 132 | { |
| 133 | return bh->b_state != 0; |
| 134 | } |
| 135 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 136 | |
| 137 | static sector_t to_sector(const struct buffer_head *bh, |
| 138 | const struct inode *inode) |
| 139 | { |
| 140 | sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9); |
| 141 | |
| 142 | return sector; |
| 143 | } |
| 144 | |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 145 | static ssize_t dax_io(struct inode *inode, struct iov_iter *iter, |
| 146 | loff_t start, loff_t end, get_block_t get_block, |
| 147 | struct buffer_head *bh) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 148 | { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 149 | loff_t pos = start, max = start, bh_max = start; |
| 150 | bool hole = false, need_wmb = false; |
| 151 | struct block_device *bdev = NULL; |
| 152 | int rw = iov_iter_rw(iter), rc; |
| 153 | long map_len = 0; |
| 154 | struct blk_dax_ctl dax = { |
| 155 | .addr = (void __pmem *) ERR_PTR(-EIO), |
| 156 | }; |
Jan Kara | 069c77b | 2016-05-11 11:58:51 +0200 | [diff] [blame] | 157 | unsigned blkbits = inode->i_blkbits; |
| 158 | sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1) |
| 159 | >> blkbits; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 160 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 161 | if (rw == READ) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 162 | end = min(end, i_size_read(inode)); |
| 163 | |
| 164 | while (pos < end) { |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 165 | size_t len; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 166 | if (pos == max) { |
Jeff Moyer | e94f5a2 | 2015-08-14 16:15:31 -0400 | [diff] [blame] | 167 | long page = pos >> PAGE_SHIFT; |
| 168 | sector_t block = page << (PAGE_SHIFT - blkbits); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 169 | unsigned first = pos - (block << blkbits); |
| 170 | long size; |
| 171 | |
| 172 | if (pos == bh_max) { |
| 173 | bh->b_size = PAGE_ALIGN(end - pos); |
| 174 | bh->b_state = 0; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 175 | rc = get_block(inode, block, bh, rw == WRITE); |
| 176 | if (rc) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 177 | break; |
| 178 | if (!buffer_size_valid(bh)) |
| 179 | bh->b_size = 1 << blkbits; |
| 180 | bh_max = pos - first + bh->b_size; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 181 | bdev = bh->b_bdev; |
Jan Kara | 069c77b | 2016-05-11 11:58:51 +0200 | [diff] [blame] | 182 | /* |
| 183 | * We allow uninitialized buffers for writes |
| 184 | * beyond EOF as those cannot race with faults |
| 185 | */ |
| 186 | WARN_ON_ONCE( |
| 187 | (buffer_new(bh) && block < file_blks) || |
| 188 | (rw == WRITE && buffer_unwritten(bh))); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 189 | } else { |
| 190 | unsigned done = bh->b_size - |
| 191 | (bh_max - (pos - first)); |
| 192 | bh->b_blocknr += done >> blkbits; |
| 193 | bh->b_size -= done; |
| 194 | } |
| 195 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 196 | hole = rw == READ && !buffer_written(bh); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 197 | if (hole) { |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 198 | size = bh->b_size - first; |
| 199 | } else { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 200 | dax_unmap_atomic(bdev, &dax); |
| 201 | dax.sector = to_sector(bh, inode); |
| 202 | dax.size = bh->b_size; |
| 203 | map_len = dax_map_atomic(bdev, &dax); |
| 204 | if (map_len < 0) { |
| 205 | rc = map_len; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 206 | break; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 207 | } |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 208 | dax.addr += first; |
| 209 | size = map_len - first; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 210 | } |
| 211 | max = min(pos + size, end); |
| 212 | } |
| 213 | |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 214 | if (iov_iter_rw(iter) == WRITE) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 215 | len = copy_from_iter_pmem(dax.addr, max - pos, iter); |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 216 | need_wmb = true; |
| 217 | } else if (!hole) |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 218 | len = copy_to_iter((void __force *) dax.addr, max - pos, |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 219 | iter); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 220 | else |
| 221 | len = iov_iter_zero(max - pos, iter); |
| 222 | |
Al Viro | cadfbb6 | 2015-11-10 19:42:49 -0700 | [diff] [blame] | 223 | if (!len) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 224 | rc = -EFAULT; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 225 | break; |
Al Viro | cadfbb6 | 2015-11-10 19:42:49 -0700 | [diff] [blame] | 226 | } |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 227 | |
| 228 | pos += len; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 229 | if (!IS_ERR(dax.addr)) |
| 230 | dax.addr += len; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 233 | if (need_wmb) |
| 234 | wmb_pmem(); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 235 | dax_unmap_atomic(bdev, &dax); |
Ross Zwisler | 2765cfb | 2015-08-18 13:55:40 -0600 | [diff] [blame] | 236 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 237 | return (pos == start) ? rc : pos - start; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * dax_do_io - Perform I/O to a DAX file |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 242 | * @iocb: The control block for this I/O |
| 243 | * @inode: The file which the I/O is directed at |
| 244 | * @iter: The addresses to do I/O from or to |
| 245 | * @pos: The file offset where the I/O starts |
| 246 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 247 | * @end_io: A filesystem callback for I/O completion |
| 248 | * @flags: See below |
| 249 | * |
| 250 | * This function uses the same locking scheme as do_blockdev_direct_IO: |
| 251 | * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the |
| 252 | * caller for writes. For reads, we take and release the i_mutex ourselves. |
| 253 | * If DIO_LOCKING is not set, the filesystem takes care of its own locking. |
| 254 | * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O |
| 255 | * is in progress. |
| 256 | */ |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 257 | ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode, |
| 258 | struct iov_iter *iter, loff_t pos, get_block_t get_block, |
| 259 | dio_iodone_t end_io, int flags) |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 260 | { |
| 261 | struct buffer_head bh; |
| 262 | ssize_t retval = -EINVAL; |
| 263 | loff_t end = pos + iov_iter_count(iter); |
| 264 | |
| 265 | memset(&bh, 0, sizeof(bh)); |
Ross Zwisler | eab95db | 2016-01-22 15:10:59 -0800 | [diff] [blame] | 266 | bh.b_bdev = inode->i_sb->s_bdev; |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 267 | |
Jan Kara | c3d98e3 | 2016-05-11 11:58:52 +0200 | [diff] [blame] | 268 | if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 269 | inode_lock(inode); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 270 | |
| 271 | /* Protects against truncate */ |
Matthew Wilcox | bbab37d | 2015-07-03 10:40:42 -0400 | [diff] [blame] | 272 | if (!(flags & DIO_SKIP_DIO_COUNT)) |
| 273 | inode_dio_begin(inode); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 274 | |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 275 | retval = dax_io(inode, iter, pos, end, get_block, &bh); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 276 | |
Omar Sandoval | a95cd63 | 2015-03-16 04:33:51 -0700 | [diff] [blame] | 277 | if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 278 | inode_unlock(inode); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 279 | |
Christoph Hellwig | 187372a | 2016-02-08 14:40:51 +1100 | [diff] [blame] | 280 | if (end_io) { |
| 281 | int err; |
| 282 | |
| 283 | err = end_io(iocb, pos, retval, bh.b_private); |
| 284 | if (err) |
| 285 | retval = err; |
| 286 | } |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 287 | |
Matthew Wilcox | bbab37d | 2015-07-03 10:40:42 -0400 | [diff] [blame] | 288 | if (!(flags & DIO_SKIP_DIO_COUNT)) |
| 289 | inode_dio_end(inode); |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 290 | return retval; |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(dax_do_io); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 293 | |
| 294 | /* |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 295 | * DAX radix tree locking |
| 296 | */ |
| 297 | struct exceptional_entry_key { |
| 298 | struct address_space *mapping; |
| 299 | unsigned long index; |
| 300 | }; |
| 301 | |
| 302 | struct wait_exceptional_entry_queue { |
| 303 | wait_queue_t wait; |
| 304 | struct exceptional_entry_key key; |
| 305 | }; |
| 306 | |
| 307 | static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode, |
| 308 | int sync, void *keyp) |
| 309 | { |
| 310 | struct exceptional_entry_key *key = keyp; |
| 311 | struct wait_exceptional_entry_queue *ewait = |
| 312 | container_of(wait, struct wait_exceptional_entry_queue, wait); |
| 313 | |
| 314 | if (key->mapping != ewait->key.mapping || |
| 315 | key->index != ewait->key.index) |
| 316 | return 0; |
| 317 | return autoremove_wake_function(wait, mode, sync, NULL); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Check whether the given slot is locked. The function must be called with |
| 322 | * mapping->tree_lock held |
| 323 | */ |
| 324 | static inline int slot_locked(struct address_space *mapping, void **slot) |
| 325 | { |
| 326 | unsigned long entry = (unsigned long) |
| 327 | radix_tree_deref_slot_protected(slot, &mapping->tree_lock); |
| 328 | return entry & RADIX_DAX_ENTRY_LOCK; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Mark the given slot is locked. The function must be called with |
| 333 | * mapping->tree_lock held |
| 334 | */ |
| 335 | static inline void *lock_slot(struct address_space *mapping, void **slot) |
| 336 | { |
| 337 | unsigned long entry = (unsigned long) |
| 338 | radix_tree_deref_slot_protected(slot, &mapping->tree_lock); |
| 339 | |
| 340 | entry |= RADIX_DAX_ENTRY_LOCK; |
| 341 | radix_tree_replace_slot(slot, (void *)entry); |
| 342 | return (void *)entry; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Mark the given slot is unlocked. The function must be called with |
| 347 | * mapping->tree_lock held |
| 348 | */ |
| 349 | static inline void *unlock_slot(struct address_space *mapping, void **slot) |
| 350 | { |
| 351 | unsigned long entry = (unsigned long) |
| 352 | radix_tree_deref_slot_protected(slot, &mapping->tree_lock); |
| 353 | |
| 354 | entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK; |
| 355 | radix_tree_replace_slot(slot, (void *)entry); |
| 356 | return (void *)entry; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Lookup entry in radix tree, wait for it to become unlocked if it is |
| 361 | * exceptional entry and return it. The caller must call |
| 362 | * put_unlocked_mapping_entry() when he decided not to lock the entry or |
| 363 | * put_locked_mapping_entry() when he locked the entry and now wants to |
| 364 | * unlock it. |
| 365 | * |
| 366 | * The function must be called with mapping->tree_lock held. |
| 367 | */ |
| 368 | static void *get_unlocked_mapping_entry(struct address_space *mapping, |
| 369 | pgoff_t index, void ***slotp) |
| 370 | { |
| 371 | void *ret, **slot; |
| 372 | struct wait_exceptional_entry_queue ewait; |
| 373 | wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index); |
| 374 | |
| 375 | init_wait(&ewait.wait); |
| 376 | ewait.wait.func = wake_exceptional_entry_func; |
| 377 | ewait.key.mapping = mapping; |
| 378 | ewait.key.index = index; |
| 379 | |
| 380 | for (;;) { |
| 381 | ret = __radix_tree_lookup(&mapping->page_tree, index, NULL, |
| 382 | &slot); |
| 383 | if (!ret || !radix_tree_exceptional_entry(ret) || |
| 384 | !slot_locked(mapping, slot)) { |
| 385 | if (slotp) |
| 386 | *slotp = slot; |
| 387 | return ret; |
| 388 | } |
| 389 | prepare_to_wait_exclusive(wq, &ewait.wait, |
| 390 | TASK_UNINTERRUPTIBLE); |
| 391 | spin_unlock_irq(&mapping->tree_lock); |
| 392 | schedule(); |
| 393 | finish_wait(wq, &ewait.wait); |
| 394 | spin_lock_irq(&mapping->tree_lock); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Find radix tree entry at given index. If it points to a page, return with |
| 400 | * the page locked. If it points to the exceptional entry, return with the |
| 401 | * radix tree entry locked. If the radix tree doesn't contain given index, |
| 402 | * create empty exceptional entry for the index and return with it locked. |
| 403 | * |
| 404 | * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For |
| 405 | * persistent memory the benefit is doubtful. We can add that later if we can |
| 406 | * show it helps. |
| 407 | */ |
| 408 | static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index) |
| 409 | { |
| 410 | void *ret, **slot; |
| 411 | |
| 412 | restart: |
| 413 | spin_lock_irq(&mapping->tree_lock); |
| 414 | ret = get_unlocked_mapping_entry(mapping, index, &slot); |
| 415 | /* No entry for given index? Make sure radix tree is big enough. */ |
| 416 | if (!ret) { |
| 417 | int err; |
| 418 | |
| 419 | spin_unlock_irq(&mapping->tree_lock); |
| 420 | err = radix_tree_preload( |
| 421 | mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM); |
| 422 | if (err) |
| 423 | return ERR_PTR(err); |
| 424 | ret = (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY | |
| 425 | RADIX_DAX_ENTRY_LOCK); |
| 426 | spin_lock_irq(&mapping->tree_lock); |
| 427 | err = radix_tree_insert(&mapping->page_tree, index, ret); |
| 428 | radix_tree_preload_end(); |
| 429 | if (err) { |
| 430 | spin_unlock_irq(&mapping->tree_lock); |
| 431 | /* Someone already created the entry? */ |
| 432 | if (err == -EEXIST) |
| 433 | goto restart; |
| 434 | return ERR_PTR(err); |
| 435 | } |
| 436 | /* Good, we have inserted empty locked entry into the tree. */ |
| 437 | mapping->nrexceptional++; |
| 438 | spin_unlock_irq(&mapping->tree_lock); |
| 439 | return ret; |
| 440 | } |
| 441 | /* Normal page in radix tree? */ |
| 442 | if (!radix_tree_exceptional_entry(ret)) { |
| 443 | struct page *page = ret; |
| 444 | |
| 445 | get_page(page); |
| 446 | spin_unlock_irq(&mapping->tree_lock); |
| 447 | lock_page(page); |
| 448 | /* Page got truncated? Retry... */ |
| 449 | if (unlikely(page->mapping != mapping)) { |
| 450 | unlock_page(page); |
| 451 | put_page(page); |
| 452 | goto restart; |
| 453 | } |
| 454 | return page; |
| 455 | } |
| 456 | ret = lock_slot(mapping, slot); |
| 457 | spin_unlock_irq(&mapping->tree_lock); |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | void dax_wake_mapping_entry_waiter(struct address_space *mapping, |
| 462 | pgoff_t index, bool wake_all) |
| 463 | { |
| 464 | wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index); |
| 465 | |
| 466 | /* |
| 467 | * Checking for locked entry and prepare_to_wait_exclusive() happens |
| 468 | * under mapping->tree_lock, ditto for entry handling in our callers. |
| 469 | * So at this point all tasks that could have seen our entry locked |
| 470 | * must be in the waitqueue and the following check will see them. |
| 471 | */ |
| 472 | if (waitqueue_active(wq)) { |
| 473 | struct exceptional_entry_key key; |
| 474 | |
| 475 | key.mapping = mapping; |
| 476 | key.index = index; |
| 477 | __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key); |
| 478 | } |
| 479 | } |
| 480 | |
Jan Kara | bc2466e | 2016-05-12 18:29:19 +0200 | [diff] [blame^] | 481 | void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index) |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 482 | { |
| 483 | void *ret, **slot; |
| 484 | |
| 485 | spin_lock_irq(&mapping->tree_lock); |
| 486 | ret = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot); |
| 487 | if (WARN_ON_ONCE(!ret || !radix_tree_exceptional_entry(ret) || |
| 488 | !slot_locked(mapping, slot))) { |
| 489 | spin_unlock_irq(&mapping->tree_lock); |
| 490 | return; |
| 491 | } |
| 492 | unlock_slot(mapping, slot); |
| 493 | spin_unlock_irq(&mapping->tree_lock); |
| 494 | dax_wake_mapping_entry_waiter(mapping, index, false); |
| 495 | } |
| 496 | |
| 497 | static void put_locked_mapping_entry(struct address_space *mapping, |
| 498 | pgoff_t index, void *entry) |
| 499 | { |
| 500 | if (!radix_tree_exceptional_entry(entry)) { |
| 501 | unlock_page(entry); |
| 502 | put_page(entry); |
| 503 | } else { |
Jan Kara | bc2466e | 2016-05-12 18:29:19 +0200 | [diff] [blame^] | 504 | dax_unlock_mapping_entry(mapping, index); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Called when we are done with radix tree entry we looked up via |
| 510 | * get_unlocked_mapping_entry() and which we didn't lock in the end. |
| 511 | */ |
| 512 | static void put_unlocked_mapping_entry(struct address_space *mapping, |
| 513 | pgoff_t index, void *entry) |
| 514 | { |
| 515 | if (!radix_tree_exceptional_entry(entry)) |
| 516 | return; |
| 517 | |
| 518 | /* We have to wake up next waiter for the radix tree entry lock */ |
| 519 | dax_wake_mapping_entry_waiter(mapping, index, false); |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree |
| 524 | * entry to get unlocked before deleting it. |
| 525 | */ |
| 526 | int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) |
| 527 | { |
| 528 | void *entry; |
| 529 | |
| 530 | spin_lock_irq(&mapping->tree_lock); |
| 531 | entry = get_unlocked_mapping_entry(mapping, index, NULL); |
| 532 | /* |
| 533 | * This gets called from truncate / punch_hole path. As such, the caller |
| 534 | * must hold locks protecting against concurrent modifications of the |
| 535 | * radix tree (usually fs-private i_mmap_sem for writing). Since the |
| 536 | * caller has seen exceptional entry for this index, we better find it |
| 537 | * at that index as well... |
| 538 | */ |
| 539 | if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) { |
| 540 | spin_unlock_irq(&mapping->tree_lock); |
| 541 | return 0; |
| 542 | } |
| 543 | radix_tree_delete(&mapping->page_tree, index); |
| 544 | mapping->nrexceptional--; |
| 545 | spin_unlock_irq(&mapping->tree_lock); |
| 546 | dax_wake_mapping_entry_waiter(mapping, index, true); |
| 547 | |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | /* |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 552 | * The user has performed a load from a hole in the file. Allocating |
| 553 | * a new page in the file would cause excessive storage usage for |
| 554 | * workloads with sparse files. We allocate a page cache page instead. |
| 555 | * We'll kick it out of the page cache if it's ever written to, |
| 556 | * otherwise it will simply fall out of the page cache under memory |
| 557 | * pressure without ever having been dirtied. |
| 558 | */ |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 559 | static int dax_load_hole(struct address_space *mapping, void *entry, |
| 560 | struct vm_fault *vmf) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 561 | { |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 562 | struct page *page; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 563 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 564 | /* Hole page already exists? Return it... */ |
| 565 | if (!radix_tree_exceptional_entry(entry)) { |
| 566 | vmf->page = entry; |
| 567 | return VM_FAULT_LOCKED; |
| 568 | } |
| 569 | |
| 570 | /* This will replace locked radix tree entry with a hole page */ |
| 571 | page = find_or_create_page(mapping, vmf->pgoff, |
| 572 | vmf->gfp_mask | __GFP_ZERO); |
| 573 | if (!page) { |
| 574 | put_locked_mapping_entry(mapping, vmf->pgoff, entry); |
| 575 | return VM_FAULT_OOM; |
| 576 | } |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 577 | vmf->page = page; |
| 578 | return VM_FAULT_LOCKED; |
| 579 | } |
| 580 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 581 | static int copy_user_bh(struct page *to, struct inode *inode, |
| 582 | struct buffer_head *bh, unsigned long vaddr) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 583 | { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 584 | struct blk_dax_ctl dax = { |
| 585 | .sector = to_sector(bh, inode), |
| 586 | .size = bh->b_size, |
| 587 | }; |
| 588 | struct block_device *bdev = bh->b_bdev; |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 589 | void *vto; |
| 590 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 591 | if (dax_map_atomic(bdev, &dax) < 0) |
| 592 | return PTR_ERR(dax.addr); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 593 | vto = kmap_atomic(to); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 594 | copy_user_page(vto, (void __force *)dax.addr, vaddr, to); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 595 | kunmap_atomic(vto); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 596 | dax_unmap_atomic(bdev, &dax); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 600 | #define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT)) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 601 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 602 | static void *dax_insert_mapping_entry(struct address_space *mapping, |
| 603 | struct vm_fault *vmf, |
| 604 | void *entry, sector_t sector) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 605 | { |
| 606 | struct radix_tree_root *page_tree = &mapping->page_tree; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 607 | int error = 0; |
| 608 | bool hole_fill = false; |
| 609 | void *new_entry; |
| 610 | pgoff_t index = vmf->pgoff; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 611 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 612 | if (vmf->flags & FAULT_FLAG_WRITE) |
Dmitry Monakhov | d2b2a28 | 2016-02-05 15:36:55 -0800 | [diff] [blame] | 613 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 614 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 615 | /* Replacing hole page with block mapping? */ |
| 616 | if (!radix_tree_exceptional_entry(entry)) { |
| 617 | hole_fill = true; |
| 618 | /* |
| 619 | * Unmap the page now before we remove it from page cache below. |
| 620 | * The page is locked so it cannot be faulted in again. |
| 621 | */ |
| 622 | unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT, |
| 623 | PAGE_SIZE, 0); |
| 624 | error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM); |
| 625 | if (error) |
| 626 | return ERR_PTR(error); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 627 | } |
| 628 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 629 | spin_lock_irq(&mapping->tree_lock); |
| 630 | new_entry = (void *)((unsigned long)RADIX_DAX_ENTRY(sector, false) | |
| 631 | RADIX_DAX_ENTRY_LOCK); |
| 632 | if (hole_fill) { |
| 633 | __delete_from_page_cache(entry, NULL); |
| 634 | /* Drop pagecache reference */ |
| 635 | put_page(entry); |
| 636 | error = radix_tree_insert(page_tree, index, new_entry); |
| 637 | if (error) { |
| 638 | new_entry = ERR_PTR(error); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 639 | goto unlock; |
| 640 | } |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 641 | mapping->nrexceptional++; |
| 642 | } else { |
| 643 | void **slot; |
| 644 | void *ret; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 645 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 646 | ret = __radix_tree_lookup(page_tree, index, NULL, &slot); |
| 647 | WARN_ON_ONCE(ret != entry); |
| 648 | radix_tree_replace_slot(slot, new_entry); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 649 | } |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 650 | if (vmf->flags & FAULT_FLAG_WRITE) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 651 | radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY); |
| 652 | unlock: |
| 653 | spin_unlock_irq(&mapping->tree_lock); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 654 | if (hole_fill) { |
| 655 | radix_tree_preload_end(); |
| 656 | /* |
| 657 | * We don't need hole page anymore, it has been replaced with |
| 658 | * locked radix tree entry now. |
| 659 | */ |
| 660 | if (mapping->a_ops->freepage) |
| 661 | mapping->a_ops->freepage(entry); |
| 662 | unlock_page(entry); |
| 663 | put_page(entry); |
| 664 | } |
| 665 | return new_entry; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static int dax_writeback_one(struct block_device *bdev, |
| 669 | struct address_space *mapping, pgoff_t index, void *entry) |
| 670 | { |
| 671 | struct radix_tree_root *page_tree = &mapping->page_tree; |
| 672 | int type = RADIX_DAX_TYPE(entry); |
| 673 | struct radix_tree_node *node; |
| 674 | struct blk_dax_ctl dax; |
| 675 | void **slot; |
| 676 | int ret = 0; |
| 677 | |
| 678 | spin_lock_irq(&mapping->tree_lock); |
| 679 | /* |
| 680 | * Regular page slots are stabilized by the page lock even |
| 681 | * without the tree itself locked. These unlocked entries |
| 682 | * need verification under the tree lock. |
| 683 | */ |
| 684 | if (!__radix_tree_lookup(page_tree, index, &node, &slot)) |
| 685 | goto unlock; |
| 686 | if (*slot != entry) |
| 687 | goto unlock; |
| 688 | |
| 689 | /* another fsync thread may have already written back this entry */ |
| 690 | if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)) |
| 691 | goto unlock; |
| 692 | |
| 693 | if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) { |
| 694 | ret = -EIO; |
| 695 | goto unlock; |
| 696 | } |
| 697 | |
| 698 | dax.sector = RADIX_DAX_SECTOR(entry); |
| 699 | dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE); |
| 700 | spin_unlock_irq(&mapping->tree_lock); |
| 701 | |
| 702 | /* |
| 703 | * We cannot hold tree_lock while calling dax_map_atomic() because it |
| 704 | * eventually calls cond_resched(). |
| 705 | */ |
| 706 | ret = dax_map_atomic(bdev, &dax); |
| 707 | if (ret < 0) |
| 708 | return ret; |
| 709 | |
| 710 | if (WARN_ON_ONCE(ret < dax.size)) { |
| 711 | ret = -EIO; |
| 712 | goto unmap; |
| 713 | } |
| 714 | |
| 715 | wb_cache_pmem(dax.addr, dax.size); |
| 716 | |
| 717 | spin_lock_irq(&mapping->tree_lock); |
| 718 | radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE); |
| 719 | spin_unlock_irq(&mapping->tree_lock); |
| 720 | unmap: |
| 721 | dax_unmap_atomic(bdev, &dax); |
| 722 | return ret; |
| 723 | |
| 724 | unlock: |
| 725 | spin_unlock_irq(&mapping->tree_lock); |
| 726 | return ret; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * Flush the mapping to the persistent domain within the byte range of [start, |
| 731 | * end]. This is required by data integrity operations to ensure file data is |
| 732 | * on persistent storage prior to completion of the operation. |
| 733 | */ |
Ross Zwisler | 7f6d5b5 | 2016-02-26 15:19:55 -0800 | [diff] [blame] | 734 | int dax_writeback_mapping_range(struct address_space *mapping, |
| 735 | struct block_device *bdev, struct writeback_control *wbc) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 736 | { |
| 737 | struct inode *inode = mapping->host; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 738 | pgoff_t start_index, end_index, pmd_index; |
| 739 | pgoff_t indices[PAGEVEC_SIZE]; |
| 740 | struct pagevec pvec; |
| 741 | bool done = false; |
| 742 | int i, ret = 0; |
| 743 | void *entry; |
| 744 | |
| 745 | if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) |
| 746 | return -EIO; |
| 747 | |
Ross Zwisler | 7f6d5b5 | 2016-02-26 15:19:55 -0800 | [diff] [blame] | 748 | if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL) |
| 749 | return 0; |
| 750 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 751 | start_index = wbc->range_start >> PAGE_SHIFT; |
| 752 | end_index = wbc->range_end >> PAGE_SHIFT; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 753 | pmd_index = DAX_PMD_INDEX(start_index); |
| 754 | |
| 755 | rcu_read_lock(); |
| 756 | entry = radix_tree_lookup(&mapping->page_tree, pmd_index); |
| 757 | rcu_read_unlock(); |
| 758 | |
| 759 | /* see if the start of our range is covered by a PMD entry */ |
| 760 | if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) |
| 761 | start_index = pmd_index; |
| 762 | |
| 763 | tag_pages_for_writeback(mapping, start_index, end_index); |
| 764 | |
| 765 | pagevec_init(&pvec, 0); |
| 766 | while (!done) { |
| 767 | pvec.nr = find_get_entries_tag(mapping, start_index, |
| 768 | PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE, |
| 769 | pvec.pages, indices); |
| 770 | |
| 771 | if (pvec.nr == 0) |
| 772 | break; |
| 773 | |
| 774 | for (i = 0; i < pvec.nr; i++) { |
| 775 | if (indices[i] > end_index) { |
| 776 | done = true; |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | ret = dax_writeback_one(bdev, mapping, indices[i], |
| 781 | pvec.pages[i]); |
| 782 | if (ret < 0) |
| 783 | return ret; |
| 784 | } |
| 785 | } |
| 786 | wmb_pmem(); |
| 787 | return 0; |
| 788 | } |
| 789 | EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); |
| 790 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 791 | static int dax_insert_mapping(struct address_space *mapping, |
| 792 | struct buffer_head *bh, void **entryp, |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 793 | struct vm_area_struct *vma, struct vm_fault *vmf) |
| 794 | { |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 795 | unsigned long vaddr = (unsigned long)vmf->virtual_address; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 796 | struct block_device *bdev = bh->b_bdev; |
| 797 | struct blk_dax_ctl dax = { |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 798 | .sector = to_sector(bh, mapping->host), |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 799 | .size = bh->b_size, |
| 800 | }; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 801 | int error; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 802 | void *ret; |
| 803 | void *entry = *entryp; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 804 | |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 805 | i_mmap_lock_read(mapping); |
| 806 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 807 | if (dax_map_atomic(bdev, &dax) < 0) { |
| 808 | error = PTR_ERR(dax.addr); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 809 | goto out; |
| 810 | } |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 811 | dax_unmap_atomic(bdev, &dax); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 812 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 813 | ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector); |
| 814 | if (IS_ERR(ret)) { |
| 815 | error = PTR_ERR(ret); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 816 | goto out; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 817 | } |
| 818 | *entryp = ret; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 819 | |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 820 | error = vm_insert_mixed(vma, vaddr, dax.pfn); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 821 | out: |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 822 | i_mmap_unlock_read(mapping); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 823 | return error; |
| 824 | } |
| 825 | |
Dave Chinner | ce5c5d5 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 826 | /** |
| 827 | * __dax_fault - handle a page fault on a DAX file |
| 828 | * @vma: The virtual memory area where the fault occurred |
| 829 | * @vmf: The description of the fault |
| 830 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 831 | * |
| 832 | * When a page fault occurs, filesystems may call this helper in their |
| 833 | * fault handler for DAX files. __dax_fault() assumes the caller has done all |
| 834 | * the necessary locking for the page fault to proceed successfully. |
| 835 | */ |
| 836 | int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, |
Jan Kara | 02fbd13 | 2016-05-11 11:58:48 +0200 | [diff] [blame] | 837 | get_block_t get_block) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 838 | { |
| 839 | struct file *file = vma->vm_file; |
| 840 | struct address_space *mapping = file->f_mapping; |
| 841 | struct inode *inode = mapping->host; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 842 | void *entry; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 843 | struct buffer_head bh; |
| 844 | unsigned long vaddr = (unsigned long)vmf->virtual_address; |
| 845 | unsigned blkbits = inode->i_blkbits; |
| 846 | sector_t block; |
| 847 | pgoff_t size; |
| 848 | int error; |
| 849 | int major = 0; |
| 850 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 851 | /* |
| 852 | * Check whether offset isn't beyond end of file now. Caller is supposed |
| 853 | * to hold locks serializing us with truncate / punch hole so this is |
| 854 | * a reliable test. |
| 855 | */ |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 856 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 857 | if (vmf->pgoff >= size) |
| 858 | return VM_FAULT_SIGBUS; |
| 859 | |
| 860 | memset(&bh, 0, sizeof(bh)); |
| 861 | block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits); |
Ross Zwisler | eab95db | 2016-01-22 15:10:59 -0800 | [diff] [blame] | 862 | bh.b_bdev = inode->i_sb->s_bdev; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 863 | bh.b_size = PAGE_SIZE; |
| 864 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 865 | entry = grab_mapping_entry(mapping, vmf->pgoff); |
| 866 | if (IS_ERR(entry)) { |
| 867 | error = PTR_ERR(entry); |
| 868 | goto out; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | error = get_block(inode, block, &bh, 0); |
| 872 | if (!error && (bh.b_size < PAGE_SIZE)) |
| 873 | error = -EIO; /* fs corruption? */ |
| 874 | if (error) |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 875 | goto unlock_entry; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 876 | |
| 877 | if (vmf->cow_page) { |
| 878 | struct page *new_page = vmf->cow_page; |
| 879 | if (buffer_written(&bh)) |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 880 | error = copy_user_bh(new_page, inode, &bh, vaddr); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 881 | else |
| 882 | clear_user_highpage(new_page, vaddr); |
| 883 | if (error) |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 884 | goto unlock_entry; |
| 885 | if (!radix_tree_exceptional_entry(entry)) { |
| 886 | vmf->page = entry; |
Jan Kara | bc2466e | 2016-05-12 18:29:19 +0200 | [diff] [blame^] | 887 | return VM_FAULT_LOCKED; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 888 | } |
Jan Kara | bc2466e | 2016-05-12 18:29:19 +0200 | [diff] [blame^] | 889 | vmf->entry = entry; |
| 890 | return VM_FAULT_DAX_LOCKED; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 893 | if (!buffer_mapped(&bh)) { |
| 894 | if (vmf->flags & FAULT_FLAG_WRITE) { |
| 895 | error = get_block(inode, block, &bh, 1); |
| 896 | count_vm_event(PGMAJFAULT); |
| 897 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); |
| 898 | major = VM_FAULT_MAJOR; |
| 899 | if (!error && (bh.b_size < PAGE_SIZE)) |
| 900 | error = -EIO; |
| 901 | if (error) |
| 902 | goto unlock_entry; |
| 903 | } else { |
| 904 | return dax_load_hole(mapping, entry, vmf); |
| 905 | } |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Jan Kara | 02fbd13 | 2016-05-11 11:58:48 +0200 | [diff] [blame] | 908 | /* Filesystem should not return unwritten buffers to us! */ |
Jan Kara | 2b10945 | 2016-05-11 11:58:50 +0200 | [diff] [blame] | 909 | WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh)); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 910 | error = dax_insert_mapping(mapping, &bh, &entry, vma, vmf); |
| 911 | unlock_entry: |
| 912 | put_locked_mapping_entry(mapping, vmf->pgoff, entry); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 913 | out: |
| 914 | if (error == -ENOMEM) |
| 915 | return VM_FAULT_OOM | major; |
| 916 | /* -EBUSY is fine, somebody else faulted on the same PTE */ |
| 917 | if ((error < 0) && (error != -EBUSY)) |
| 918 | return VM_FAULT_SIGBUS | major; |
| 919 | return VM_FAULT_NOPAGE | major; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 920 | } |
Dave Chinner | ce5c5d5 | 2015-06-04 09:18:18 +1000 | [diff] [blame] | 921 | EXPORT_SYMBOL(__dax_fault); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 922 | |
| 923 | /** |
| 924 | * dax_fault - handle a page fault on a DAX file |
| 925 | * @vma: The virtual memory area where the fault occurred |
| 926 | * @vmf: The description of the fault |
| 927 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 928 | * |
| 929 | * When a page fault occurs, filesystems may call this helper in their |
| 930 | * fault handler for DAX files. |
| 931 | */ |
| 932 | int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf, |
Jan Kara | 02fbd13 | 2016-05-11 11:58:48 +0200 | [diff] [blame] | 933 | get_block_t get_block) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 934 | { |
| 935 | int result; |
| 936 | struct super_block *sb = file_inode(vma->vm_file)->i_sb; |
| 937 | |
| 938 | if (vmf->flags & FAULT_FLAG_WRITE) { |
| 939 | sb_start_pagefault(sb); |
| 940 | file_update_time(vma->vm_file); |
| 941 | } |
Jan Kara | 02fbd13 | 2016-05-11 11:58:48 +0200 | [diff] [blame] | 942 | result = __dax_fault(vma, vmf, get_block); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 943 | if (vmf->flags & FAULT_FLAG_WRITE) |
| 944 | sb_end_pagefault(sb); |
| 945 | |
| 946 | return result; |
| 947 | } |
| 948 | EXPORT_SYMBOL_GPL(dax_fault); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 949 | |
Jan Kara | 348e967 | 2016-05-12 18:29:15 +0200 | [diff] [blame] | 950 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 951 | /* |
| 952 | * The 'colour' (ie low bits) within a PMD of a page offset. This comes up |
| 953 | * more often than one might expect in the below function. |
| 954 | */ |
| 955 | #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) |
| 956 | |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 957 | static void __dax_dbg(struct buffer_head *bh, unsigned long address, |
| 958 | const char *reason, const char *fn) |
| 959 | { |
| 960 | if (bh) { |
| 961 | char bname[BDEVNAME_SIZE]; |
| 962 | bdevname(bh->b_bdev, bname); |
| 963 | pr_debug("%s: %s addr: %lx dev %s state %lx start %lld " |
| 964 | "length %zd fallback: %s\n", fn, current->comm, |
| 965 | address, bname, bh->b_state, (u64)bh->b_blocknr, |
| 966 | bh->b_size, reason); |
| 967 | } else { |
| 968 | pr_debug("%s: %s addr: %lx fallback: %s\n", fn, |
| 969 | current->comm, address, reason); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | #define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd") |
| 974 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 975 | int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, |
Jan Kara | 02fbd13 | 2016-05-11 11:58:48 +0200 | [diff] [blame] | 976 | pmd_t *pmd, unsigned int flags, get_block_t get_block) |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 977 | { |
| 978 | struct file *file = vma->vm_file; |
| 979 | struct address_space *mapping = file->f_mapping; |
| 980 | struct inode *inode = mapping->host; |
| 981 | struct buffer_head bh; |
| 982 | unsigned blkbits = inode->i_blkbits; |
| 983 | unsigned long pmd_addr = address & PMD_MASK; |
| 984 | bool write = flags & FAULT_FLAG_WRITE; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 985 | struct block_device *bdev; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 986 | pgoff_t size, pgoff; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 987 | sector_t block; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 988 | int result = 0; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 989 | bool alloc = false; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 990 | |
Dan Williams | c046c32 | 2016-01-15 16:57:01 -0800 | [diff] [blame] | 991 | /* dax pmd mappings require pfn_t_devmap() */ |
Dan Williams | ee82c9e | 2015-11-15 16:06:32 -0800 | [diff] [blame] | 992 | if (!IS_ENABLED(CONFIG_FS_DAX_PMD)) |
| 993 | return VM_FAULT_FALLBACK; |
| 994 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 995 | /* Fall back to PTEs if we're going to COW */ |
Toshi Kani | 59bf4fb | 2016-01-15 16:56:05 -0800 | [diff] [blame] | 996 | if (write && !(vma->vm_flags & VM_SHARED)) { |
| 997 | split_huge_pmd(vma, pmd, address); |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 998 | dax_pmd_dbg(NULL, address, "cow write"); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 999 | return VM_FAULT_FALLBACK; |
Toshi Kani | 59bf4fb | 2016-01-15 16:56:05 -0800 | [diff] [blame] | 1000 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1001 | /* If the PMD would extend outside the VMA */ |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1002 | if (pmd_addr < vma->vm_start) { |
| 1003 | dax_pmd_dbg(NULL, address, "vma start unaligned"); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1004 | return VM_FAULT_FALLBACK; |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1005 | } |
| 1006 | if ((pmd_addr + PMD_SIZE) > vma->vm_end) { |
| 1007 | dax_pmd_dbg(NULL, address, "vma end unaligned"); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1008 | return VM_FAULT_FALLBACK; |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1009 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1010 | |
Matthew Wilcox | 3fdd1b47 | 2015-09-08 14:59:39 -0700 | [diff] [blame] | 1011 | pgoff = linear_page_index(vma, pmd_addr); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1012 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1013 | if (pgoff >= size) |
| 1014 | return VM_FAULT_SIGBUS; |
| 1015 | /* If the PMD would cover blocks out of the file */ |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1016 | if ((pgoff | PG_PMD_COLOUR) >= size) { |
| 1017 | dax_pmd_dbg(NULL, address, |
| 1018 | "offset + huge page size > file size"); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1019 | return VM_FAULT_FALLBACK; |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1020 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1021 | |
| 1022 | memset(&bh, 0, sizeof(bh)); |
Ross Zwisler | d4bbe70 | 2016-01-22 15:10:31 -0800 | [diff] [blame] | 1023 | bh.b_bdev = inode->i_sb->s_bdev; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1024 | block = (sector_t)pgoff << (PAGE_SHIFT - blkbits); |
| 1025 | |
| 1026 | bh.b_size = PMD_SIZE; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1027 | |
| 1028 | if (get_block(inode, block, &bh, 0) != 0) |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1029 | return VM_FAULT_SIGBUS; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1030 | |
| 1031 | if (!buffer_mapped(&bh) && write) { |
| 1032 | if (get_block(inode, block, &bh, 1) != 0) |
| 1033 | return VM_FAULT_SIGBUS; |
| 1034 | alloc = true; |
Jan Kara | 2b10945 | 2016-05-11 11:58:50 +0200 | [diff] [blame] | 1035 | WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh)); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1038 | bdev = bh.b_bdev; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1039 | |
| 1040 | /* |
| 1041 | * If the filesystem isn't willing to tell us the length of a hole, |
| 1042 | * just fall back to PTEs. Calling get_block 512 times in a loop |
| 1043 | * would be silly. |
| 1044 | */ |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1045 | if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) { |
| 1046 | dax_pmd_dbg(&bh, address, "allocated block too small"); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1047 | return VM_FAULT_FALLBACK; |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1048 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1049 | |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1050 | /* |
| 1051 | * If we allocated new storage, make sure no process has any |
| 1052 | * zero pages covering this hole |
| 1053 | */ |
| 1054 | if (alloc) { |
| 1055 | loff_t lstart = pgoff << PAGE_SHIFT; |
| 1056 | loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */ |
| 1057 | |
| 1058 | truncate_pagecache_range(inode, lstart, lend); |
| 1059 | } |
| 1060 | |
Ross Zwisler | de14b9c | 2016-01-22 15:10:34 -0800 | [diff] [blame] | 1061 | i_mmap_lock_read(mapping); |
Kirill A. Shutemov | 46c043e | 2015-09-08 14:59:42 -0700 | [diff] [blame] | 1062 | |
Jan Kara | b995353 | 2016-05-12 18:29:14 +0200 | [diff] [blame] | 1063 | if (!write && !buffer_mapped(&bh)) { |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1064 | spinlock_t *ptl; |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 1065 | pmd_t entry; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1066 | struct page *zero_page = get_huge_zero_page(); |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 1067 | |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1068 | if (unlikely(!zero_page)) { |
| 1069 | dax_pmd_dbg(&bh, address, "no zero page"); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1070 | goto fallback; |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1071 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1072 | |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 1073 | ptl = pmd_lock(vma->vm_mm, pmd); |
| 1074 | if (!pmd_none(*pmd)) { |
| 1075 | spin_unlock(ptl); |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1076 | dax_pmd_dbg(&bh, address, "pmd already present"); |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 1077 | goto fallback; |
| 1078 | } |
| 1079 | |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1080 | dev_dbg(part_to_dev(bdev->bd_part), |
| 1081 | "%s: %s addr: %lx pfn: <zero> sect: %llx\n", |
| 1082 | __func__, current->comm, address, |
| 1083 | (unsigned long long) to_sector(&bh, inode)); |
| 1084 | |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 1085 | entry = mk_pmd(zero_page, vma->vm_page_prot); |
| 1086 | entry = pmd_mkhuge(entry); |
| 1087 | set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1088 | result = VM_FAULT_NOPAGE; |
Kirill A. Shutemov | d295e34 | 2015-09-08 14:59:34 -0700 | [diff] [blame] | 1089 | spin_unlock(ptl); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1090 | } else { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1091 | struct blk_dax_ctl dax = { |
| 1092 | .sector = to_sector(&bh, inode), |
| 1093 | .size = PMD_SIZE, |
| 1094 | }; |
| 1095 | long length = dax_map_atomic(bdev, &dax); |
| 1096 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1097 | if (length < 0) { |
Dan Williams | 8b3db979 | 2016-02-24 14:02:06 -0800 | [diff] [blame] | 1098 | dax_pmd_dbg(&bh, address, "dax-error fallback"); |
| 1099 | goto fallback; |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1100 | } |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1101 | if (length < PMD_SIZE) { |
| 1102 | dax_pmd_dbg(&bh, address, "dax-length too small"); |
| 1103 | dax_unmap_atomic(bdev, &dax); |
| 1104 | goto fallback; |
| 1105 | } |
| 1106 | if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) { |
| 1107 | dax_pmd_dbg(&bh, address, "pfn unaligned"); |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1108 | dax_unmap_atomic(bdev, &dax); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1109 | goto fallback; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1110 | } |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1111 | |
Dan Williams | c046c32 | 2016-01-15 16:57:01 -0800 | [diff] [blame] | 1112 | if (!pfn_t_devmap(dax.pfn)) { |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1113 | dax_unmap_atomic(bdev, &dax); |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1114 | dax_pmd_dbg(&bh, address, "pfn not in memmap"); |
Dan Williams | 152d7bd | 2015-11-12 18:33:54 -0800 | [diff] [blame] | 1115 | goto fallback; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1116 | } |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1117 | dax_unmap_atomic(bdev, &dax); |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 1118 | |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1119 | /* |
| 1120 | * For PTE faults we insert a radix tree entry for reads, and |
| 1121 | * leave it clean. Then on the first write we dirty the radix |
| 1122 | * tree entry via the dax_pfn_mkwrite() path. This sequence |
| 1123 | * allows the dax_pfn_mkwrite() call to be simpler and avoid a |
| 1124 | * call into get_block() to translate the pgoff to a sector in |
| 1125 | * order to be able to create a new radix tree entry. |
| 1126 | * |
| 1127 | * The PMD path doesn't have an equivalent to |
| 1128 | * dax_pfn_mkwrite(), though, so for a read followed by a |
| 1129 | * write we traverse all the way through __dax_pmd_fault() |
| 1130 | * twice. This means we can just skip inserting a radix tree |
| 1131 | * entry completely on the initial read and just wait until |
| 1132 | * the write to insert a dirty entry. |
| 1133 | */ |
| 1134 | if (write) { |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 1135 | /* |
| 1136 | * We should insert radix-tree entry and dirty it here. |
| 1137 | * For now this is broken... |
| 1138 | */ |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
Dan Williams | cbb38e4 | 2016-01-15 16:56:58 -0800 | [diff] [blame] | 1141 | dev_dbg(part_to_dev(bdev->bd_part), |
| 1142 | "%s: %s addr: %lx pfn: %lx sect: %llx\n", |
| 1143 | __func__, current->comm, address, |
| 1144 | pfn_t_to_pfn(dax.pfn), |
| 1145 | (unsigned long long) dax.sector); |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 1146 | result |= vmf_insert_pfn_pmd(vma, address, pmd, |
Dan Williams | f25748e3 | 2016-01-15 16:56:43 -0800 | [diff] [blame] | 1147 | dax.pfn, write); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | out: |
Ross Zwisler | 0f90cc6 | 2015-10-15 15:28:32 -0700 | [diff] [blame] | 1151 | i_mmap_unlock_read(mapping); |
| 1152 | |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1153 | return result; |
| 1154 | |
| 1155 | fallback: |
| 1156 | count_vm_event(THP_FAULT_FALLBACK); |
| 1157 | result = VM_FAULT_FALLBACK; |
| 1158 | goto out; |
| 1159 | } |
| 1160 | EXPORT_SYMBOL_GPL(__dax_pmd_fault); |
| 1161 | |
| 1162 | /** |
| 1163 | * dax_pmd_fault - handle a PMD fault on a DAX file |
| 1164 | * @vma: The virtual memory area where the fault occurred |
| 1165 | * @vmf: The description of the fault |
| 1166 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 1167 | * |
| 1168 | * When a page fault occurs, filesystems may call this helper in their |
| 1169 | * pmd_fault handler for DAX files. |
| 1170 | */ |
| 1171 | int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address, |
Jan Kara | 02fbd13 | 2016-05-11 11:58:48 +0200 | [diff] [blame] | 1172 | pmd_t *pmd, unsigned int flags, get_block_t get_block) |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1173 | { |
| 1174 | int result; |
| 1175 | struct super_block *sb = file_inode(vma->vm_file)->i_sb; |
| 1176 | |
| 1177 | if (flags & FAULT_FLAG_WRITE) { |
| 1178 | sb_start_pagefault(sb); |
| 1179 | file_update_time(vma->vm_file); |
| 1180 | } |
Jan Kara | 02fbd13 | 2016-05-11 11:58:48 +0200 | [diff] [blame] | 1181 | result = __dax_pmd_fault(vma, address, pmd, flags, get_block); |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1182 | if (flags & FAULT_FLAG_WRITE) |
| 1183 | sb_end_pagefault(sb); |
| 1184 | |
| 1185 | return result; |
| 1186 | } |
| 1187 | EXPORT_SYMBOL_GPL(dax_pmd_fault); |
Valentin Rothberg | dd8a2b6 | 2015-09-08 14:59:09 -0700 | [diff] [blame] | 1188 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Matthew Wilcox | 844f35d | 2015-09-08 14:58:57 -0700 | [diff] [blame] | 1189 | |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1190 | /** |
Boaz Harrosh | 0e3b210 | 2015-04-15 16:15:14 -0700 | [diff] [blame] | 1191 | * dax_pfn_mkwrite - handle first write to DAX page |
| 1192 | * @vma: The virtual memory area where the fault occurred |
| 1193 | * @vmf: The description of the fault |
Boaz Harrosh | 0e3b210 | 2015-04-15 16:15:14 -0700 | [diff] [blame] | 1194 | */ |
| 1195 | int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1196 | { |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 1197 | struct file *file = vma->vm_file; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 1198 | struct address_space *mapping = file->f_mapping; |
| 1199 | void *entry; |
| 1200 | pgoff_t index = vmf->pgoff; |
Boaz Harrosh | 0e3b210 | 2015-04-15 16:15:14 -0700 | [diff] [blame] | 1201 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 1202 | spin_lock_irq(&mapping->tree_lock); |
| 1203 | entry = get_unlocked_mapping_entry(mapping, index, NULL); |
| 1204 | if (!entry || !radix_tree_exceptional_entry(entry)) |
| 1205 | goto out; |
| 1206 | radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY); |
| 1207 | put_unlocked_mapping_entry(mapping, index, entry); |
| 1208 | out: |
| 1209 | spin_unlock_irq(&mapping->tree_lock); |
Boaz Harrosh | 0e3b210 | 2015-04-15 16:15:14 -0700 | [diff] [blame] | 1210 | return VM_FAULT_NOPAGE; |
| 1211 | } |
| 1212 | EXPORT_SYMBOL_GPL(dax_pfn_mkwrite); |
| 1213 | |
Vishal Verma | 4b0228f | 2016-04-21 15:13:46 -0400 | [diff] [blame] | 1214 | static bool dax_range_is_aligned(struct block_device *bdev, |
| 1215 | unsigned int offset, unsigned int length) |
| 1216 | { |
| 1217 | unsigned short sector_size = bdev_logical_block_size(bdev); |
| 1218 | |
| 1219 | if (!IS_ALIGNED(offset, sector_size)) |
| 1220 | return false; |
| 1221 | if (!IS_ALIGNED(length, sector_size)) |
| 1222 | return false; |
| 1223 | |
| 1224 | return true; |
| 1225 | } |
| 1226 | |
Christoph Hellwig | 679c8bd | 2016-05-09 10:47:04 +0200 | [diff] [blame] | 1227 | int __dax_zero_page_range(struct block_device *bdev, sector_t sector, |
| 1228 | unsigned int offset, unsigned int length) |
| 1229 | { |
| 1230 | struct blk_dax_ctl dax = { |
| 1231 | .sector = sector, |
| 1232 | .size = PAGE_SIZE, |
| 1233 | }; |
| 1234 | |
Vishal Verma | 4b0228f | 2016-04-21 15:13:46 -0400 | [diff] [blame] | 1235 | if (dax_range_is_aligned(bdev, offset, length)) { |
| 1236 | sector_t start_sector = dax.sector + (offset >> 9); |
| 1237 | |
| 1238 | return blkdev_issue_zeroout(bdev, start_sector, |
| 1239 | length >> 9, GFP_NOFS, true); |
| 1240 | } else { |
| 1241 | if (dax_map_atomic(bdev, &dax) < 0) |
| 1242 | return PTR_ERR(dax.addr); |
| 1243 | clear_pmem(dax.addr + offset, length); |
| 1244 | wmb_pmem(); |
| 1245 | dax_unmap_atomic(bdev, &dax); |
| 1246 | } |
Christoph Hellwig | 679c8bd | 2016-05-09 10:47:04 +0200 | [diff] [blame] | 1247 | return 0; |
| 1248 | } |
| 1249 | EXPORT_SYMBOL_GPL(__dax_zero_page_range); |
| 1250 | |
Boaz Harrosh | 0e3b210 | 2015-04-15 16:15:14 -0700 | [diff] [blame] | 1251 | /** |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 1252 | * 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] | 1253 | * @inode: The file being truncated |
| 1254 | * @from: The file offset that is being truncated to |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 1255 | * @length: The number of bytes to zero |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1256 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 1257 | * |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 1258 | * This function can be called by a filesystem when it is zeroing part of a |
| 1259 | * page in a DAX file. This is intended for hole-punch operations. If |
| 1260 | * you are truncating a file, the helper function dax_truncate_page() may be |
| 1261 | * more convenient. |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1262 | */ |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 1263 | int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length, |
| 1264 | get_block_t get_block) |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1265 | { |
| 1266 | struct buffer_head bh; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1267 | pgoff_t index = from >> PAGE_SHIFT; |
| 1268 | unsigned offset = from & (PAGE_SIZE-1); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1269 | int err; |
| 1270 | |
| 1271 | /* Block boundary? Nothing to do */ |
| 1272 | if (!length) |
| 1273 | return 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1274 | BUG_ON((offset + length) > PAGE_SIZE); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1275 | |
| 1276 | memset(&bh, 0, sizeof(bh)); |
Ross Zwisler | eab95db | 2016-01-22 15:10:59 -0800 | [diff] [blame] | 1277 | bh.b_bdev = inode->i_sb->s_bdev; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1278 | bh.b_size = PAGE_SIZE; |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1279 | err = get_block(inode, index, &bh, 0); |
Christoph Hellwig | 679c8bd | 2016-05-09 10:47:04 +0200 | [diff] [blame] | 1280 | if (err < 0 || !buffer_written(&bh)) |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1281 | return err; |
Dan Williams | b2e0d16 | 2016-01-15 16:55:59 -0800 | [diff] [blame] | 1282 | |
Christoph Hellwig | 679c8bd | 2016-05-09 10:47:04 +0200 | [diff] [blame] | 1283 | return __dax_zero_page_range(bh.b_bdev, to_sector(&bh, inode), |
| 1284 | offset, length); |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1285 | } |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 1286 | EXPORT_SYMBOL_GPL(dax_zero_page_range); |
| 1287 | |
| 1288 | /** |
| 1289 | * dax_truncate_page - handle a partial page being truncated in a DAX file |
| 1290 | * @inode: The file being truncated |
| 1291 | * @from: The file offset that is being truncated to |
| 1292 | * @get_block: The filesystem method used to translate file offsets to blocks |
| 1293 | * |
| 1294 | * Similar to block_truncate_page(), this function can be called by a |
| 1295 | * filesystem when it is truncating a DAX file to handle the partial page. |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 1296 | */ |
| 1297 | int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block) |
| 1298 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1299 | unsigned length = PAGE_ALIGN(from) - from; |
Matthew Wilcox | 25726bc | 2015-02-16 15:59:35 -0800 | [diff] [blame] | 1300 | return dax_zero_page_range(inode, from, length, get_block); |
| 1301 | } |
Matthew Wilcox | 4c0ccfe | 2015-02-16 15:59:06 -0800 | [diff] [blame] | 1302 | EXPORT_SYMBOL_GPL(dax_truncate_page); |