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