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