blob: d0bd1f4f81b3528a1482d41a7b1f4b99499f3a23 [file] [log] [blame]
Matthew Wilcoxd475c632015-02-16 15:58:56 -08001/*
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 Wilcoxf7ca90b2015-02-16 15:59:02 -080022#include <linux/highmem.h>
23#include <linux/memcontrol.h>
24#include <linux/mm.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080025#include <linux/mutex.h>
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080026#include <linux/sched.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080027#include <linux/uio.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080028#include <linux/vmstat.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080029
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080030int 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}
64EXPORT_SYMBOL_GPL(dax_clear_blocks);
65
Matthew Wilcoxd475c632015-02-16 15:58:56 -080066static 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
73static 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
84static 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 */
96static bool buffer_size_valid(struct buffer_head *bh)
97{
98 return bh->b_state != 0;
99}
100
101static ssize_t dax_io(int rw, struct inode *inode, struct iov_iter *iter,
102 loff_t start, loff_t end, get_block_t get_block,
103 struct buffer_head *bh)
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
112 if (rw != WRITE)
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,
127 rw == WRITE);
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
140 hole = (rw != WRITE) && !buffer_written(bh);
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
157 if (rw == WRITE)
158 len = copy_from_iter(addr, max - pos, iter);
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
176 * @rw: READ to read or WRITE to write
177 * @iocb: The control block for this I/O
178 * @inode: The file which the I/O is directed at
179 * @iter: The addresses to do I/O from or to
180 * @pos: The file offset where the I/O starts
181 * @get_block: The filesystem method used to translate file offsets to blocks
182 * @end_io: A filesystem callback for I/O completion
183 * @flags: See below
184 *
185 * This function uses the same locking scheme as do_blockdev_direct_IO:
186 * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
187 * caller for writes. For reads, we take and release the i_mutex ourselves.
188 * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
189 * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
190 * is in progress.
191 */
192ssize_t dax_do_io(int rw, struct kiocb *iocb, struct inode *inode,
193 struct iov_iter *iter, loff_t pos,
194 get_block_t get_block, dio_iodone_t end_io, int flags)
195{
196 struct buffer_head bh;
197 ssize_t retval = -EINVAL;
198 loff_t end = pos + iov_iter_count(iter);
199
200 memset(&bh, 0, sizeof(bh));
201
202 if ((flags & DIO_LOCKING) && (rw == READ)) {
203 struct address_space *mapping = inode->i_mapping;
204 mutex_lock(&inode->i_mutex);
205 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
206 if (retval) {
207 mutex_unlock(&inode->i_mutex);
208 goto out;
209 }
210 }
211
212 /* Protects against truncate */
213 atomic_inc(&inode->i_dio_count);
214
215 retval = dax_io(rw, inode, iter, pos, end, get_block, &bh);
216
217 if ((flags & DIO_LOCKING) && (rw == READ))
218 mutex_unlock(&inode->i_mutex);
219
220 if ((retval > 0) && end_io)
221 end_io(iocb, pos, retval, bh.b_private);
222
223 inode_dio_done(inode);
224 out:
225 return retval;
226}
227EXPORT_SYMBOL_GPL(dax_do_io);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800228
229/*
230 * The user has performed a load from a hole in the file. Allocating
231 * a new page in the file would cause excessive storage usage for
232 * workloads with sparse files. We allocate a page cache page instead.
233 * We'll kick it out of the page cache if it's ever written to,
234 * otherwise it will simply fall out of the page cache under memory
235 * pressure without ever having been dirtied.
236 */
237static int dax_load_hole(struct address_space *mapping, struct page *page,
238 struct vm_fault *vmf)
239{
240 unsigned long size;
241 struct inode *inode = mapping->host;
242 if (!page)
243 page = find_or_create_page(mapping, vmf->pgoff,
244 GFP_KERNEL | __GFP_ZERO);
245 if (!page)
246 return VM_FAULT_OOM;
247 /* Recheck i_size under page lock to avoid truncate race */
248 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
249 if (vmf->pgoff >= size) {
250 unlock_page(page);
251 page_cache_release(page);
252 return VM_FAULT_SIGBUS;
253 }
254
255 vmf->page = page;
256 return VM_FAULT_LOCKED;
257}
258
259static int copy_user_bh(struct page *to, struct buffer_head *bh,
260 unsigned blkbits, unsigned long vaddr)
261{
262 void *vfrom, *vto;
263 if (dax_get_addr(bh, &vfrom, blkbits) < 0)
264 return -EIO;
265 vto = kmap_atomic(to);
266 copy_user_page(vto, vfrom, vaddr, to);
267 kunmap_atomic(vto);
268 return 0;
269}
270
271static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
272 struct vm_area_struct *vma, struct vm_fault *vmf)
273{
274 struct address_space *mapping = inode->i_mapping;
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
282 i_mmap_lock_read(mapping);
283
284 /*
285 * Check truncate didn't happen while we were allocating a block.
286 * If it did, this block may or may not be still allocated to the
287 * file. We can't tell the filesystem to free it because we can't
288 * take i_mutex here. In the worst case, the file still has blocks
289 * allocated past the end of the file.
290 */
291 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
292 if (unlikely(vmf->pgoff >= size)) {
293 error = -EIO;
294 goto out;
295 }
296
297 error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
298 if (error < 0)
299 goto out;
300 if (error < PAGE_SIZE) {
301 error = -EIO;
302 goto out;
303 }
304
305 if (buffer_unwritten(bh) || buffer_new(bh))
306 clear_page(addr);
307
308 error = vm_insert_mixed(vma, vaddr, pfn);
309
310 out:
311 i_mmap_unlock_read(mapping);
312
313 if (bh->b_end_io)
314 bh->b_end_io(bh, 1);
315
316 return error;
317}
318
319static int do_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
320 get_block_t get_block)
321{
322 struct file *file = vma->vm_file;
323 struct address_space *mapping = file->f_mapping;
324 struct inode *inode = mapping->host;
325 struct page *page;
326 struct buffer_head bh;
327 unsigned long vaddr = (unsigned long)vmf->virtual_address;
328 unsigned blkbits = inode->i_blkbits;
329 sector_t block;
330 pgoff_t size;
331 int error;
332 int major = 0;
333
334 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
335 if (vmf->pgoff >= size)
336 return VM_FAULT_SIGBUS;
337
338 memset(&bh, 0, sizeof(bh));
339 block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
340 bh.b_size = PAGE_SIZE;
341
342 repeat:
343 page = find_get_page(mapping, vmf->pgoff);
344 if (page) {
345 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
346 page_cache_release(page);
347 return VM_FAULT_RETRY;
348 }
349 if (unlikely(page->mapping != mapping)) {
350 unlock_page(page);
351 page_cache_release(page);
352 goto repeat;
353 }
354 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
355 if (unlikely(vmf->pgoff >= size)) {
356 /*
357 * We have a struct page covering a hole in the file
358 * from a read fault and we've raced with a truncate
359 */
360 error = -EIO;
361 goto unlock_page;
362 }
363 }
364
365 error = get_block(inode, block, &bh, 0);
366 if (!error && (bh.b_size < PAGE_SIZE))
367 error = -EIO; /* fs corruption? */
368 if (error)
369 goto unlock_page;
370
371 if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
372 if (vmf->flags & FAULT_FLAG_WRITE) {
373 error = get_block(inode, block, &bh, 1);
374 count_vm_event(PGMAJFAULT);
375 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
376 major = VM_FAULT_MAJOR;
377 if (!error && (bh.b_size < PAGE_SIZE))
378 error = -EIO;
379 if (error)
380 goto unlock_page;
381 } else {
382 return dax_load_hole(mapping, page, vmf);
383 }
384 }
385
386 if (vmf->cow_page) {
387 struct page *new_page = vmf->cow_page;
388 if (buffer_written(&bh))
389 error = copy_user_bh(new_page, &bh, blkbits, vaddr);
390 else
391 clear_user_highpage(new_page, vaddr);
392 if (error)
393 goto unlock_page;
394 vmf->page = page;
395 if (!page) {
396 i_mmap_lock_read(mapping);
397 /* Check we didn't race with truncate */
398 size = (i_size_read(inode) + PAGE_SIZE - 1) >>
399 PAGE_SHIFT;
400 if (vmf->pgoff >= size) {
401 i_mmap_unlock_read(mapping);
402 error = -EIO;
403 goto out;
404 }
405 }
406 return VM_FAULT_LOCKED;
407 }
408
409 /* Check we didn't race with a read fault installing a new page */
410 if (!page && major)
411 page = find_lock_page(mapping, vmf->pgoff);
412
413 if (page) {
414 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
415 PAGE_CACHE_SIZE, 0);
416 delete_from_page_cache(page);
417 unlock_page(page);
418 page_cache_release(page);
419 }
420
421 error = dax_insert_mapping(inode, &bh, vma, vmf);
422
423 out:
424 if (error == -ENOMEM)
425 return VM_FAULT_OOM | major;
426 /* -EBUSY is fine, somebody else faulted on the same PTE */
427 if ((error < 0) && (error != -EBUSY))
428 return VM_FAULT_SIGBUS | major;
429 return VM_FAULT_NOPAGE | major;
430
431 unlock_page:
432 if (page) {
433 unlock_page(page);
434 page_cache_release(page);
435 }
436 goto out;
437}
438
439/**
440 * dax_fault - handle a page fault on a DAX file
441 * @vma: The virtual memory area where the fault occurred
442 * @vmf: The description of the fault
443 * @get_block: The filesystem method used to translate file offsets to blocks
444 *
445 * When a page fault occurs, filesystems may call this helper in their
446 * fault handler for DAX files.
447 */
448int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
449 get_block_t get_block)
450{
451 int result;
452 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
453
454 if (vmf->flags & FAULT_FLAG_WRITE) {
455 sb_start_pagefault(sb);
456 file_update_time(vma->vm_file);
457 }
458 result = do_dax_fault(vma, vmf, get_block);
459 if (vmf->flags & FAULT_FLAG_WRITE)
460 sb_end_pagefault(sb);
461
462 return result;
463}
464EXPORT_SYMBOL_GPL(dax_fault);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800465
466/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700467 * dax_pfn_mkwrite - handle first write to DAX page
468 * @vma: The virtual memory area where the fault occurred
469 * @vmf: The description of the fault
470 *
471 */
472int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
473{
474 struct super_block *sb = file_inode(vma->vm_file)->i_sb;
475
476 sb_start_pagefault(sb);
477 file_update_time(vma->vm_file);
478 sb_end_pagefault(sb);
479 return VM_FAULT_NOPAGE;
480}
481EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
482
483/**
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800484 * dax_zero_page_range - zero a range within a page of a DAX file
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800485 * @inode: The file being truncated
486 * @from: The file offset that is being truncated to
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800487 * @length: The number of bytes to zero
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800488 * @get_block: The filesystem method used to translate file offsets to blocks
489 *
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800490 * This function can be called by a filesystem when it is zeroing part of a
491 * page in a DAX file. This is intended for hole-punch operations. If
492 * you are truncating a file, the helper function dax_truncate_page() may be
493 * more convenient.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800494 *
495 * We work in terms of PAGE_CACHE_SIZE here for commonality with
496 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
497 * took care of disposing of the unnecessary blocks. Even if the filesystem
498 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800499 * since the file might be mmapped.
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800500 */
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800501int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
502 get_block_t get_block)
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800503{
504 struct buffer_head bh;
505 pgoff_t index = from >> PAGE_CACHE_SHIFT;
506 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800507 int err;
508
509 /* Block boundary? Nothing to do */
510 if (!length)
511 return 0;
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800512 BUG_ON((offset + length) > PAGE_CACHE_SIZE);
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800513
514 memset(&bh, 0, sizeof(bh));
515 bh.b_size = PAGE_CACHE_SIZE;
516 err = get_block(inode, index, &bh, 0);
517 if (err < 0)
518 return err;
519 if (buffer_written(&bh)) {
520 void *addr;
521 err = dax_get_addr(&bh, &addr, inode->i_blkbits);
522 if (err < 0)
523 return err;
524 memset(addr + offset, 0, length);
525 }
526
527 return 0;
528}
Matthew Wilcox25726bc2015-02-16 15:59:35 -0800529EXPORT_SYMBOL_GPL(dax_zero_page_range);
530
531/**
532 * dax_truncate_page - handle a partial page being truncated in a DAX file
533 * @inode: The file being truncated
534 * @from: The file offset that is being truncated to
535 * @get_block: The filesystem method used to translate file offsets to blocks
536 *
537 * Similar to block_truncate_page(), this function can be called by a
538 * filesystem when it is truncating a DAX file to handle the partial page.
539 *
540 * We work in terms of PAGE_CACHE_SIZE here for commonality with
541 * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
542 * took care of disposing of the unnecessary blocks. Even if the filesystem
543 * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
544 * since the file might be mmapped.
545 */
546int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
547{
548 unsigned length = PAGE_CACHE_ALIGN(from) - from;
549 return dax_zero_page_range(inode, from, length, get_block);
550}
Matthew Wilcox4c0ccfe2015-02-16 15:59:06 -0800551EXPORT_SYMBOL_GPL(dax_truncate_page);