57bb70b4af70f75f422dff8beff13ce7545e2b61
[cascardo/linux.git] / fs / dax.c
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>
22 #include <linux/highmem.h>
23 #include <linux/memcontrol.h>
24 #include <linux/mm.h>
25 #include <linux/mutex.h>
26 #include <linux/pmem.h>
27 #include <linux/sched.h>
28 #include <linux/uio.h>
29 #include <linux/vmstat.h>
30
31 int dax_clear_blocks(struct inode *inode, sector_t block, long size)
32 {
33         struct block_device *bdev = inode->i_sb->s_bdev;
34         sector_t sector = block << (inode->i_blkbits - 9);
35
36         might_sleep();
37         do {
38                 void __pmem *addr;
39                 unsigned long pfn;
40                 long count;
41
42                 count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
43                 if (count < 0)
44                         return count;
45                 BUG_ON(size < count);
46                 while (count > 0) {
47                         unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
48                         if (pgsz > count)
49                                 pgsz = count;
50                         clear_pmem(addr, pgsz);
51                         addr += pgsz;
52                         size -= pgsz;
53                         count -= pgsz;
54                         BUG_ON(pgsz & 511);
55                         sector += pgsz / 512;
56                         cond_resched();
57                 }
58         } while (size);
59
60         wmb_pmem();
61         return 0;
62 }
63 EXPORT_SYMBOL_GPL(dax_clear_blocks);
64
65 static long dax_get_addr(struct buffer_head *bh, void __pmem **addr,
66                 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 /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
74 static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
75                 loff_t pos, loff_t end)
76 {
77         loff_t final = end - pos + first; /* The final byte of the buffer */
78
79         if (first > 0)
80                 clear_pmem(addr, first);
81         if (final < size)
82                 clear_pmem(addr + final, size - final);
83 }
84
85 static bool buffer_written(struct buffer_head *bh)
86 {
87         return buffer_mapped(bh) && !buffer_unwritten(bh);
88 }
89
90 /*
91  * When ext4 encounters a hole, it returns without modifying the buffer_head
92  * which means that we can't trust b_size.  To cope with this, we set b_state
93  * to 0 before calling get_block and, if any bit is set, we know we can trust
94  * b_size.  Unfortunate, really, since ext4 knows precisely how long a hole is
95  * and would save us time calling get_block repeatedly.
96  */
97 static bool buffer_size_valid(struct buffer_head *bh)
98 {
99         return bh->b_state != 0;
100 }
101
102 static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
103                       loff_t start, loff_t end, get_block_t get_block,
104                       struct buffer_head *bh)
105 {
106         ssize_t retval = 0;
107         loff_t pos = start;
108         loff_t max = start;
109         loff_t bh_max = start;
110         void __pmem *addr;
111         bool hole = false;
112         bool need_wmb = false;
113
114         if (iov_iter_rw(iter) != WRITE)
115                 end = min(end, i_size_read(inode));
116
117         while (pos < end) {
118                 size_t len;
119                 if (pos == max) {
120                         unsigned blkbits = inode->i_blkbits;
121                         sector_t block = pos >> blkbits;
122                         unsigned first = pos - (block << blkbits);
123                         long size;
124
125                         if (pos == bh_max) {
126                                 bh->b_size = PAGE_ALIGN(end - pos);
127                                 bh->b_state = 0;
128                                 retval = get_block(inode, block, bh,
129                                                    iov_iter_rw(iter) == WRITE);
130                                 if (retval)
131                                         break;
132                                 if (!buffer_size_valid(bh))
133                                         bh->b_size = 1 << blkbits;
134                                 bh_max = pos - first + bh->b_size;
135                         } else {
136                                 unsigned done = bh->b_size -
137                                                 (bh_max - (pos - first));
138                                 bh->b_blocknr += done >> blkbits;
139                                 bh->b_size -= done;
140                         }
141
142                         hole = iov_iter_rw(iter) != WRITE && !buffer_written(bh);
143                         if (hole) {
144                                 addr = NULL;
145                                 size = bh->b_size - first;
146                         } else {
147                                 retval = dax_get_addr(bh, &addr, blkbits);
148                                 if (retval < 0)
149                                         break;
150                                 if (buffer_unwritten(bh) || buffer_new(bh)) {
151                                         dax_new_buf(addr, retval, first, pos,
152                                                                         end);
153                                         need_wmb = true;
154                                 }
155                                 addr += first;
156                                 size = retval - first;
157                         }
158                         max = min(pos + size, end);
159                 }
160
161                 if (iov_iter_rw(iter) == WRITE) {
162                         len = copy_from_iter_pmem(addr, max - pos, iter);
163                         need_wmb = true;
164                 } else if (!hole)
165                         len = copy_to_iter((void __force *)addr, max - pos,
166                                         iter);
167                 else
168                         len = iov_iter_zero(max - pos, iter);
169
170                 if (!len)
171                         break;
172
173                 pos += len;
174                 addr += len;
175         }
176
177         if (need_wmb)
178                 wmb_pmem();
179
180         return (pos == start) ? retval : pos - start;
181 }
182
183 /**
184  * dax_do_io - Perform I/O to a DAX file
185  * @iocb: The control block for this I/O
186  * @inode: The file which the I/O is directed at
187  * @iter: The addresses to do I/O from or to
188  * @pos: The file offset where the I/O starts
189  * @get_block: The filesystem method used to translate file offsets to blocks
190  * @end_io: A filesystem callback for I/O completion
191  * @flags: See below
192  *
193  * This function uses the same locking scheme as do_blockdev_direct_IO:
194  * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
195  * caller for writes.  For reads, we take and release the i_mutex ourselves.
196  * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
197  * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
198  * is in progress.
199  */
200 ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
201                   struct iov_iter *iter, loff_t pos, get_block_t get_block,
202                   dio_iodone_t end_io, int flags)
203 {
204         struct buffer_head bh;
205         ssize_t retval = -EINVAL;
206         loff_t end = pos + iov_iter_count(iter);
207
208         memset(&bh, 0, sizeof(bh));
209
210         if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
211                 struct address_space *mapping = inode->i_mapping;
212                 mutex_lock(&inode->i_mutex);
213                 retval = filemap_write_and_wait_range(mapping, pos, end - 1);
214                 if (retval) {
215                         mutex_unlock(&inode->i_mutex);
216                         goto out;
217                 }
218         }
219
220         /* Protects against truncate */
221         if (!(flags & DIO_SKIP_DIO_COUNT))
222                 inode_dio_begin(inode);
223
224         retval = dax_io(inode, iter, pos, end, get_block, &bh);
225
226         if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
227                 mutex_unlock(&inode->i_mutex);
228
229         if ((retval > 0) && end_io)
230                 end_io(iocb, pos, retval, bh.b_private);
231
232         if (!(flags & DIO_SKIP_DIO_COUNT))
233                 inode_dio_end(inode);
234  out:
235         return retval;
236 }
237 EXPORT_SYMBOL_GPL(dax_do_io);
238
239 /*
240  * The user has performed a load from a hole in the file.  Allocating
241  * a new page in the file would cause excessive storage usage for
242  * workloads with sparse files.  We allocate a page cache page instead.
243  * We'll kick it out of the page cache if it's ever written to,
244  * otherwise it will simply fall out of the page cache under memory
245  * pressure without ever having been dirtied.
246  */
247 static int dax_load_hole(struct address_space *mapping, struct page *page,
248                                                         struct vm_fault *vmf)
249 {
250         unsigned long size;
251         struct inode *inode = mapping->host;
252         if (!page)
253                 page = find_or_create_page(mapping, vmf->pgoff,
254                                                 GFP_KERNEL | __GFP_ZERO);
255         if (!page)
256                 return VM_FAULT_OOM;
257         /* Recheck i_size under page lock to avoid truncate race */
258         size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
259         if (vmf->pgoff >= size) {
260                 unlock_page(page);
261                 page_cache_release(page);
262                 return VM_FAULT_SIGBUS;
263         }
264
265         vmf->page = page;
266         return VM_FAULT_LOCKED;
267 }
268
269 static int copy_user_bh(struct page *to, struct buffer_head *bh,
270                         unsigned blkbits, unsigned long vaddr)
271 {
272         void __pmem *vfrom;
273         void *vto;
274
275         if (dax_get_addr(bh, &vfrom, blkbits) < 0)
276                 return -EIO;
277         vto = kmap_atomic(to);
278         copy_user_page(vto, (void __force *)vfrom, vaddr, to);
279         kunmap_atomic(vto);
280         return 0;
281 }
282
283 static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
284                         struct vm_area_struct *vma, struct vm_fault *vmf)
285 {
286         struct address_space *mapping = inode->i_mapping;
287         sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
288         unsigned long vaddr = (unsigned long)vmf->virtual_address;
289         void __pmem *addr;
290         unsigned long pfn;
291         pgoff_t size;
292         int error;
293
294         i_mmap_lock_read(mapping);
295
296         /*
297          * Check truncate didn't happen while we were allocating a block.
298          * If it did, this block may or may not be still allocated to the
299          * file.  We can't tell the filesystem to free it because we can't
300          * take i_mutex here.  In the worst case, the file still has blocks
301          * allocated past the end of the file.
302          */
303         size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
304         if (unlikely(vmf->pgoff >= size)) {
305                 error = -EIO;
306                 goto out;
307         }
308
309         error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
310         if (error < 0)
311                 goto out;
312         if (error < PAGE_SIZE) {
313                 error = -EIO;
314                 goto out;
315         }
316
317         if (buffer_unwritten(bh) || buffer_new(bh)) {
318                 clear_pmem(addr, PAGE_SIZE);
319                 wmb_pmem();
320         }
321
322         error = vm_insert_mixed(vma, vaddr, pfn);
323
324  out:
325         i_mmap_unlock_read(mapping);
326
327         return error;
328 }
329
330 /**
331  * __dax_fault - handle a page fault on a DAX file
332  * @vma: The virtual memory area where the fault occurred
333  * @vmf: The description of the fault
334  * @get_block: The filesystem method used to translate file offsets to blocks
335  * @complete_unwritten: The filesystem method used to convert unwritten blocks
336  *      to written so the data written to them is exposed. This is required for
337  *      required by write faults for filesystems that will return unwritten
338  *      extent mappings from @get_block, but it is optional for reads as
339  *      dax_insert_mapping() will always zero unwritten blocks. If the fs does
340  *      not support unwritten extents, the it should pass NULL.
341  *
342  * When a page fault occurs, filesystems may call this helper in their
343  * fault handler for DAX files. __dax_fault() assumes the caller has done all
344  * the necessary locking for the page fault to proceed successfully.
345  */
346 int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
347                         get_block_t get_block, dax_iodone_t complete_unwritten)
348 {
349         struct file *file = vma->vm_file;
350         struct address_space *mapping = file->f_mapping;
351         struct inode *inode = mapping->host;
352         struct page *page;
353         struct buffer_head bh;
354         unsigned long vaddr = (unsigned long)vmf->virtual_address;
355         unsigned blkbits = inode->i_blkbits;
356         sector_t block;
357         pgoff_t size;
358         int error;
359         int major = 0;
360
361         size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
362         if (vmf->pgoff >= size)
363                 return VM_FAULT_SIGBUS;
364
365         memset(&bh, 0, sizeof(bh));
366         block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
367         bh.b_size = PAGE_SIZE;
368
369  repeat:
370         page = find_get_page(mapping, vmf->pgoff);
371         if (page) {
372                 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
373                         page_cache_release(page);
374                         return VM_FAULT_RETRY;
375                 }
376                 if (unlikely(page->mapping != mapping)) {
377                         unlock_page(page);
378                         page_cache_release(page);
379                         goto repeat;
380                 }
381                 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
382                 if (unlikely(vmf->pgoff >= size)) {
383                         /*
384                          * We have a struct page covering a hole in the file
385                          * from a read fault and we've raced with a truncate
386                          */
387                         error = -EIO;
388                         goto unlock_page;
389                 }
390         }
391
392         error = get_block(inode, block, &bh, 0);
393         if (!error && (bh.b_size < PAGE_SIZE))
394                 error = -EIO;           /* fs corruption? */
395         if (error)
396                 goto unlock_page;
397
398         if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
399                 if (vmf->flags & FAULT_FLAG_WRITE) {
400                         error = get_block(inode, block, &bh, 1);
401                         count_vm_event(PGMAJFAULT);
402                         mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
403                         major = VM_FAULT_MAJOR;
404                         if (!error && (bh.b_size < PAGE_SIZE))
405                                 error = -EIO;
406                         if (error)
407                                 goto unlock_page;
408                 } else {
409                         return dax_load_hole(mapping, page, vmf);
410                 }
411         }
412
413         if (vmf->cow_page) {
414                 struct page *new_page = vmf->cow_page;
415                 if (buffer_written(&bh))
416                         error = copy_user_bh(new_page, &bh, blkbits, vaddr);
417                 else
418                         clear_user_highpage(new_page, vaddr);
419                 if (error)
420                         goto unlock_page;
421                 vmf->page = page;
422                 if (!page) {
423                         i_mmap_lock_read(mapping);
424                         /* Check we didn't race with truncate */
425                         size = (i_size_read(inode) + PAGE_SIZE - 1) >>
426                                                                 PAGE_SHIFT;
427                         if (vmf->pgoff >= size) {
428                                 i_mmap_unlock_read(mapping);
429                                 error = -EIO;
430                                 goto out;
431                         }
432                 }
433                 return VM_FAULT_LOCKED;
434         }
435
436         /* Check we didn't race with a read fault installing a new page */
437         if (!page && major)
438                 page = find_lock_page(mapping, vmf->pgoff);
439
440         if (page) {
441                 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
442                                                         PAGE_CACHE_SIZE, 0);
443                 delete_from_page_cache(page);
444                 unlock_page(page);
445                 page_cache_release(page);
446         }
447
448         /*
449          * If we successfully insert the new mapping over an unwritten extent,
450          * we need to ensure we convert the unwritten extent. If there is an
451          * error inserting the mapping, the filesystem needs to leave it as
452          * unwritten to prevent exposure of the stale underlying data to
453          * userspace, but we still need to call the completion function so
454          * the private resources on the mapping buffer can be released. We
455          * indicate what the callback should do via the uptodate variable, same
456          * as for normal BH based IO completions.
457          */
458         error = dax_insert_mapping(inode, &bh, vma, vmf);
459         if (buffer_unwritten(&bh)) {
460                 if (complete_unwritten)
461                         complete_unwritten(&bh, !error);
462                 else
463                         WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
464         }
465
466  out:
467         if (error == -ENOMEM)
468                 return VM_FAULT_OOM | major;
469         /* -EBUSY is fine, somebody else faulted on the same PTE */
470         if ((error < 0) && (error != -EBUSY))
471                 return VM_FAULT_SIGBUS | major;
472         return VM_FAULT_NOPAGE | major;
473
474  unlock_page:
475         if (page) {
476                 unlock_page(page);
477                 page_cache_release(page);
478         }
479         goto out;
480 }
481 EXPORT_SYMBOL(__dax_fault);
482
483 /**
484  * dax_fault - handle a page fault on a DAX file
485  * @vma: The virtual memory area where the fault occurred
486  * @vmf: The description of the fault
487  * @get_block: The filesystem method used to translate file offsets to blocks
488  *
489  * When a page fault occurs, filesystems may call this helper in their
490  * fault handler for DAX files.
491  */
492 int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
493               get_block_t get_block, dax_iodone_t complete_unwritten)
494 {
495         int result;
496         struct super_block *sb = file_inode(vma->vm_file)->i_sb;
497
498         if (vmf->flags & FAULT_FLAG_WRITE) {
499                 sb_start_pagefault(sb);
500                 file_update_time(vma->vm_file);
501         }
502         result = __dax_fault(vma, vmf, get_block, complete_unwritten);
503         if (vmf->flags & FAULT_FLAG_WRITE)
504                 sb_end_pagefault(sb);
505
506         return result;
507 }
508 EXPORT_SYMBOL_GPL(dax_fault);
509
510 /**
511  * dax_pfn_mkwrite - handle first write to DAX page
512  * @vma: The virtual memory area where the fault occurred
513  * @vmf: The description of the fault
514  *
515  */
516 int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
517 {
518         struct super_block *sb = file_inode(vma->vm_file)->i_sb;
519
520         sb_start_pagefault(sb);
521         file_update_time(vma->vm_file);
522         sb_end_pagefault(sb);
523         return VM_FAULT_NOPAGE;
524 }
525 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
526
527 /**
528  * dax_zero_page_range - zero a range within a page of a DAX file
529  * @inode: The file being truncated
530  * @from: The file offset that is being truncated to
531  * @length: The number of bytes to zero
532  * @get_block: The filesystem method used to translate file offsets to blocks
533  *
534  * This function can be called by a filesystem when it is zeroing part of a
535  * page in a DAX file.  This is intended for hole-punch operations.  If
536  * you are truncating a file, the helper function dax_truncate_page() may be
537  * more convenient.
538  *
539  * We work in terms of PAGE_CACHE_SIZE here for commonality with
540  * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
541  * took care of disposing of the unnecessary blocks.  Even if the filesystem
542  * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
543  * since the file might be mmapped.
544  */
545 int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
546                                                         get_block_t get_block)
547 {
548         struct buffer_head bh;
549         pgoff_t index = from >> PAGE_CACHE_SHIFT;
550         unsigned offset = from & (PAGE_CACHE_SIZE-1);
551         int err;
552
553         /* Block boundary? Nothing to do */
554         if (!length)
555                 return 0;
556         BUG_ON((offset + length) > PAGE_CACHE_SIZE);
557
558         memset(&bh, 0, sizeof(bh));
559         bh.b_size = PAGE_CACHE_SIZE;
560         err = get_block(inode, index, &bh, 0);
561         if (err < 0)
562                 return err;
563         if (buffer_written(&bh)) {
564                 void __pmem *addr;
565                 err = dax_get_addr(&bh, &addr, inode->i_blkbits);
566                 if (err < 0)
567                         return err;
568                 clear_pmem(addr + offset, length);
569                 wmb_pmem();
570         }
571
572         return 0;
573 }
574 EXPORT_SYMBOL_GPL(dax_zero_page_range);
575
576 /**
577  * dax_truncate_page - handle a partial page being truncated in a DAX file
578  * @inode: The file being truncated
579  * @from: The file offset that is being truncated to
580  * @get_block: The filesystem method used to translate file offsets to blocks
581  *
582  * Similar to block_truncate_page(), this function can be called by a
583  * filesystem when it is truncating a DAX file to handle the partial page.
584  *
585  * We work in terms of PAGE_CACHE_SIZE here for commonality with
586  * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
587  * took care of disposing of the unnecessary blocks.  Even if the filesystem
588  * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
589  * since the file might be mmapped.
590  */
591 int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
592 {
593         unsigned length = PAGE_CACHE_ALIGN(from) - from;
594         return dax_zero_page_range(inode, from, length, get_block);
595 }
596 EXPORT_SYMBOL_GPL(dax_truncate_page);