9bc6624251b4d878ff02d27634f77b2b1be5c52a
[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/dax.h>
21 #include <linux/fs.h>
22 #include <linux/genhd.h>
23 #include <linux/highmem.h>
24 #include <linux/memcontrol.h>
25 #include <linux/mm.h>
26 #include <linux/mutex.h>
27 #include <linux/pagevec.h>
28 #include <linux/pmem.h>
29 #include <linux/sched.h>
30 #include <linux/uio.h>
31 #include <linux/vmstat.h>
32 #include <linux/pfn_t.h>
33 #include <linux/sizes.h>
34
35 #define RADIX_DAX_MASK  0xf
36 #define RADIX_DAX_SHIFT 4
37 #define RADIX_DAX_PTE  (0x4 | RADIX_TREE_EXCEPTIONAL_ENTRY)
38 #define RADIX_DAX_PMD  (0x8 | RADIX_TREE_EXCEPTIONAL_ENTRY)
39 #define RADIX_DAX_TYPE(entry) ((unsigned long)entry & RADIX_DAX_MASK)
40 #define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT))
41 #define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \
42                 RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE)))
43
44 static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
45 {
46         struct request_queue *q = bdev->bd_queue;
47         long rc = -EIO;
48
49         dax->addr = (void __pmem *) ERR_PTR(-EIO);
50         if (blk_queue_enter(q, true) != 0)
51                 return rc;
52
53         rc = bdev_direct_access(bdev, dax);
54         if (rc < 0) {
55                 dax->addr = (void __pmem *) ERR_PTR(rc);
56                 blk_queue_exit(q);
57                 return rc;
58         }
59         return rc;
60 }
61
62 static void dax_unmap_atomic(struct block_device *bdev,
63                 const struct blk_dax_ctl *dax)
64 {
65         if (IS_ERR(dax->addr))
66                 return;
67         blk_queue_exit(bdev->bd_queue);
68 }
69
70 struct page *read_dax_sector(struct block_device *bdev, sector_t n)
71 {
72         struct page *page = alloc_pages(GFP_KERNEL, 0);
73         struct blk_dax_ctl dax = {
74                 .size = PAGE_SIZE,
75                 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
76         };
77         long rc;
78
79         if (!page)
80                 return ERR_PTR(-ENOMEM);
81
82         rc = dax_map_atomic(bdev, &dax);
83         if (rc < 0)
84                 return ERR_PTR(rc);
85         memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
86         dax_unmap_atomic(bdev, &dax);
87         return page;
88 }
89
90 /*
91  * dax_clear_sectors() is called from within transaction context from XFS,
92  * and hence this means the stack from this point must follow GFP_NOFS
93  * semantics for all operations.
94  */
95 int dax_clear_sectors(struct block_device *bdev, sector_t _sector, long _size)
96 {
97         struct blk_dax_ctl dax = {
98                 .sector = _sector,
99                 .size = _size,
100         };
101
102         might_sleep();
103         do {
104                 long count, sz;
105
106                 count = dax_map_atomic(bdev, &dax);
107                 if (count < 0)
108                         return count;
109                 sz = min_t(long, count, SZ_128K);
110                 clear_pmem(dax.addr, sz);
111                 dax.size -= sz;
112                 dax.sector += sz / 512;
113                 dax_unmap_atomic(bdev, &dax);
114                 cond_resched();
115         } while (dax.size);
116
117         wmb_pmem();
118         return 0;
119 }
120 EXPORT_SYMBOL_GPL(dax_clear_sectors);
121
122 static bool buffer_written(struct buffer_head *bh)
123 {
124         return buffer_mapped(bh) && !buffer_unwritten(bh);
125 }
126
127 /*
128  * When ext4 encounters a hole, it returns without modifying the buffer_head
129  * which means that we can't trust b_size.  To cope with this, we set b_state
130  * to 0 before calling get_block and, if any bit is set, we know we can trust
131  * b_size.  Unfortunate, really, since ext4 knows precisely how long a hole is
132  * and would save us time calling get_block repeatedly.
133  */
134 static bool buffer_size_valid(struct buffer_head *bh)
135 {
136         return bh->b_state != 0;
137 }
138
139
140 static sector_t to_sector(const struct buffer_head *bh,
141                 const struct inode *inode)
142 {
143         sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
144
145         return sector;
146 }
147
148 static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
149                       loff_t start, loff_t end, get_block_t get_block,
150                       struct buffer_head *bh)
151 {
152         loff_t pos = start, max = start, bh_max = start;
153         bool hole = false, need_wmb = false;
154         struct block_device *bdev = NULL;
155         int rw = iov_iter_rw(iter), rc;
156         long map_len = 0;
157         struct blk_dax_ctl dax = {
158                 .addr = (void __pmem *) ERR_PTR(-EIO),
159         };
160         unsigned blkbits = inode->i_blkbits;
161         sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1)
162                                                                 >> blkbits;
163
164         if (rw == READ)
165                 end = min(end, i_size_read(inode));
166
167         while (pos < end) {
168                 size_t len;
169                 if (pos == max) {
170                         long page = pos >> PAGE_SHIFT;
171                         sector_t block = page << (PAGE_SHIFT - blkbits);
172                         unsigned first = pos - (block << blkbits);
173                         long size;
174
175                         if (pos == bh_max) {
176                                 bh->b_size = PAGE_ALIGN(end - pos);
177                                 bh->b_state = 0;
178                                 rc = get_block(inode, block, bh, rw == WRITE);
179                                 if (rc)
180                                         break;
181                                 if (!buffer_size_valid(bh))
182                                         bh->b_size = 1 << blkbits;
183                                 bh_max = pos - first + bh->b_size;
184                                 bdev = bh->b_bdev;
185                                 /*
186                                  * We allow uninitialized buffers for writes
187                                  * beyond EOF as those cannot race with faults
188                                  */
189                                 WARN_ON_ONCE(
190                                         (buffer_new(bh) && block < file_blks) ||
191                                         (rw == WRITE && buffer_unwritten(bh)));
192                         } else {
193                                 unsigned done = bh->b_size -
194                                                 (bh_max - (pos - first));
195                                 bh->b_blocknr += done >> blkbits;
196                                 bh->b_size -= done;
197                         }
198
199                         hole = rw == READ && !buffer_written(bh);
200                         if (hole) {
201                                 size = bh->b_size - first;
202                         } else {
203                                 dax_unmap_atomic(bdev, &dax);
204                                 dax.sector = to_sector(bh, inode);
205                                 dax.size = bh->b_size;
206                                 map_len = dax_map_atomic(bdev, &dax);
207                                 if (map_len < 0) {
208                                         rc = map_len;
209                                         break;
210                                 }
211                                 dax.addr += first;
212                                 size = map_len - first;
213                         }
214                         max = min(pos + size, end);
215                 }
216
217                 if (iov_iter_rw(iter) == WRITE) {
218                         len = copy_from_iter_pmem(dax.addr, max - pos, iter);
219                         need_wmb = true;
220                 } else if (!hole)
221                         len = copy_to_iter((void __force *) dax.addr, max - pos,
222                                         iter);
223                 else
224                         len = iov_iter_zero(max - pos, iter);
225
226                 if (!len) {
227                         rc = -EFAULT;
228                         break;
229                 }
230
231                 pos += len;
232                 if (!IS_ERR(dax.addr))
233                         dax.addr += len;
234         }
235
236         if (need_wmb)
237                 wmb_pmem();
238         dax_unmap_atomic(bdev, &dax);
239
240         return (pos == start) ? rc : pos - start;
241 }
242
243 /**
244  * dax_do_io - Perform I/O to a DAX file
245  * @iocb: The control block for this I/O
246  * @inode: The file which the I/O is directed at
247  * @iter: The addresses to do I/O from or to
248  * @pos: The file offset where the I/O starts
249  * @get_block: The filesystem method used to translate file offsets to blocks
250  * @end_io: A filesystem callback for I/O completion
251  * @flags: See below
252  *
253  * This function uses the same locking scheme as do_blockdev_direct_IO:
254  * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
255  * caller for writes.  For reads, we take and release the i_mutex ourselves.
256  * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
257  * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
258  * is in progress.
259  */
260 ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
261                   struct iov_iter *iter, loff_t pos, get_block_t get_block,
262                   dio_iodone_t end_io, int flags)
263 {
264         struct buffer_head bh;
265         ssize_t retval = -EINVAL;
266         loff_t end = pos + iov_iter_count(iter);
267
268         memset(&bh, 0, sizeof(bh));
269         bh.b_bdev = inode->i_sb->s_bdev;
270
271         if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
272                 inode_lock(inode);
273
274         /* Protects against truncate */
275         if (!(flags & DIO_SKIP_DIO_COUNT))
276                 inode_dio_begin(inode);
277
278         retval = dax_io(inode, iter, pos, end, get_block, &bh);
279
280         if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
281                 inode_unlock(inode);
282
283         if (end_io) {
284                 int err;
285
286                 err = end_io(iocb, pos, retval, bh.b_private);
287                 if (err)
288                         retval = err;
289         }
290
291         if (!(flags & DIO_SKIP_DIO_COUNT))
292                 inode_dio_end(inode);
293         return retval;
294 }
295 EXPORT_SYMBOL_GPL(dax_do_io);
296
297 /*
298  * The user has performed a load from a hole in the file.  Allocating
299  * a new page in the file would cause excessive storage usage for
300  * workloads with sparse files.  We allocate a page cache page instead.
301  * We'll kick it out of the page cache if it's ever written to,
302  * otherwise it will simply fall out of the page cache under memory
303  * pressure without ever having been dirtied.
304  */
305 static int dax_load_hole(struct address_space *mapping, struct page *page,
306                                                         struct vm_fault *vmf)
307 {
308         if (!page)
309                 page = find_or_create_page(mapping, vmf->pgoff,
310                                                 GFP_KERNEL | __GFP_ZERO);
311         if (!page)
312                 return VM_FAULT_OOM;
313
314         vmf->page = page;
315         return VM_FAULT_LOCKED;
316 }
317
318 static int copy_user_bh(struct page *to, struct inode *inode,
319                 struct buffer_head *bh, unsigned long vaddr)
320 {
321         struct blk_dax_ctl dax = {
322                 .sector = to_sector(bh, inode),
323                 .size = bh->b_size,
324         };
325         struct block_device *bdev = bh->b_bdev;
326         void *vto;
327
328         if (dax_map_atomic(bdev, &dax) < 0)
329                 return PTR_ERR(dax.addr);
330         vto = kmap_atomic(to);
331         copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
332         kunmap_atomic(vto);
333         dax_unmap_atomic(bdev, &dax);
334         return 0;
335 }
336
337 #define NO_SECTOR -1
338 #define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
339
340 static int dax_radix_entry(struct address_space *mapping, pgoff_t index,
341                 sector_t sector, bool pmd_entry, bool dirty)
342 {
343         struct radix_tree_root *page_tree = &mapping->page_tree;
344         pgoff_t pmd_index = DAX_PMD_INDEX(index);
345         int type, error = 0;
346         void *entry;
347
348         WARN_ON_ONCE(pmd_entry && !dirty);
349         if (dirty)
350                 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
351
352         spin_lock_irq(&mapping->tree_lock);
353
354         entry = radix_tree_lookup(page_tree, pmd_index);
355         if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) {
356                 index = pmd_index;
357                 goto dirty;
358         }
359
360         entry = radix_tree_lookup(page_tree, index);
361         if (entry) {
362                 type = RADIX_DAX_TYPE(entry);
363                 if (WARN_ON_ONCE(type != RADIX_DAX_PTE &&
364                                         type != RADIX_DAX_PMD)) {
365                         error = -EIO;
366                         goto unlock;
367                 }
368
369                 if (!pmd_entry || type == RADIX_DAX_PMD)
370                         goto dirty;
371
372                 /*
373                  * We only insert dirty PMD entries into the radix tree.  This
374                  * means we don't need to worry about removing a dirty PTE
375                  * entry and inserting a clean PMD entry, thus reducing the
376                  * range we would flush with a follow-up fsync/msync call.
377                  */
378                 radix_tree_delete(&mapping->page_tree, index);
379                 mapping->nrexceptional--;
380         }
381
382         if (sector == NO_SECTOR) {
383                 /*
384                  * This can happen during correct operation if our pfn_mkwrite
385                  * fault raced against a hole punch operation.  If this
386                  * happens the pte that was hole punched will have been
387                  * unmapped and the radix tree entry will have been removed by
388                  * the time we are called, but the call will still happen.  We
389                  * will return all the way up to wp_pfn_shared(), where the
390                  * pte_same() check will fail, eventually causing page fault
391                  * to be retried by the CPU.
392                  */
393                 goto unlock;
394         }
395
396         error = radix_tree_insert(page_tree, index,
397                         RADIX_DAX_ENTRY(sector, pmd_entry));
398         if (error)
399                 goto unlock;
400
401         mapping->nrexceptional++;
402  dirty:
403         if (dirty)
404                 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
405  unlock:
406         spin_unlock_irq(&mapping->tree_lock);
407         return error;
408 }
409
410 static int dax_writeback_one(struct block_device *bdev,
411                 struct address_space *mapping, pgoff_t index, void *entry)
412 {
413         struct radix_tree_root *page_tree = &mapping->page_tree;
414         int type = RADIX_DAX_TYPE(entry);
415         struct radix_tree_node *node;
416         struct blk_dax_ctl dax;
417         void **slot;
418         int ret = 0;
419
420         spin_lock_irq(&mapping->tree_lock);
421         /*
422          * Regular page slots are stabilized by the page lock even
423          * without the tree itself locked.  These unlocked entries
424          * need verification under the tree lock.
425          */
426         if (!__radix_tree_lookup(page_tree, index, &node, &slot))
427                 goto unlock;
428         if (*slot != entry)
429                 goto unlock;
430
431         /* another fsync thread may have already written back this entry */
432         if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
433                 goto unlock;
434
435         if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
436                 ret = -EIO;
437                 goto unlock;
438         }
439
440         dax.sector = RADIX_DAX_SECTOR(entry);
441         dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
442         spin_unlock_irq(&mapping->tree_lock);
443
444         /*
445          * We cannot hold tree_lock while calling dax_map_atomic() because it
446          * eventually calls cond_resched().
447          */
448         ret = dax_map_atomic(bdev, &dax);
449         if (ret < 0)
450                 return ret;
451
452         if (WARN_ON_ONCE(ret < dax.size)) {
453                 ret = -EIO;
454                 goto unmap;
455         }
456
457         wb_cache_pmem(dax.addr, dax.size);
458
459         spin_lock_irq(&mapping->tree_lock);
460         radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
461         spin_unlock_irq(&mapping->tree_lock);
462  unmap:
463         dax_unmap_atomic(bdev, &dax);
464         return ret;
465
466  unlock:
467         spin_unlock_irq(&mapping->tree_lock);
468         return ret;
469 }
470
471 /*
472  * Flush the mapping to the persistent domain within the byte range of [start,
473  * end]. This is required by data integrity operations to ensure file data is
474  * on persistent storage prior to completion of the operation.
475  */
476 int dax_writeback_mapping_range(struct address_space *mapping,
477                 struct block_device *bdev, struct writeback_control *wbc)
478 {
479         struct inode *inode = mapping->host;
480         pgoff_t start_index, end_index, pmd_index;
481         pgoff_t indices[PAGEVEC_SIZE];
482         struct pagevec pvec;
483         bool done = false;
484         int i, ret = 0;
485         void *entry;
486
487         if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
488                 return -EIO;
489
490         if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
491                 return 0;
492
493         start_index = wbc->range_start >> PAGE_SHIFT;
494         end_index = wbc->range_end >> PAGE_SHIFT;
495         pmd_index = DAX_PMD_INDEX(start_index);
496
497         rcu_read_lock();
498         entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
499         rcu_read_unlock();
500
501         /* see if the start of our range is covered by a PMD entry */
502         if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
503                 start_index = pmd_index;
504
505         tag_pages_for_writeback(mapping, start_index, end_index);
506
507         pagevec_init(&pvec, 0);
508         while (!done) {
509                 pvec.nr = find_get_entries_tag(mapping, start_index,
510                                 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
511                                 pvec.pages, indices);
512
513                 if (pvec.nr == 0)
514                         break;
515
516                 for (i = 0; i < pvec.nr; i++) {
517                         if (indices[i] > end_index) {
518                                 done = true;
519                                 break;
520                         }
521
522                         ret = dax_writeback_one(bdev, mapping, indices[i],
523                                         pvec.pages[i]);
524                         if (ret < 0)
525                                 return ret;
526                 }
527         }
528         wmb_pmem();
529         return 0;
530 }
531 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
532
533 static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
534                         struct vm_area_struct *vma, struct vm_fault *vmf)
535 {
536         unsigned long vaddr = (unsigned long)vmf->virtual_address;
537         struct address_space *mapping = inode->i_mapping;
538         struct block_device *bdev = bh->b_bdev;
539         struct blk_dax_ctl dax = {
540                 .sector = to_sector(bh, inode),
541                 .size = bh->b_size,
542         };
543         int error;
544
545         i_mmap_lock_read(mapping);
546
547         if (dax_map_atomic(bdev, &dax) < 0) {
548                 error = PTR_ERR(dax.addr);
549                 goto out;
550         }
551         dax_unmap_atomic(bdev, &dax);
552
553         error = dax_radix_entry(mapping, vmf->pgoff, dax.sector, false,
554                         vmf->flags & FAULT_FLAG_WRITE);
555         if (error)
556                 goto out;
557
558         error = vm_insert_mixed(vma, vaddr, dax.pfn);
559
560  out:
561         i_mmap_unlock_read(mapping);
562
563         return error;
564 }
565
566 /**
567  * __dax_fault - handle a page fault on a DAX file
568  * @vma: The virtual memory area where the fault occurred
569  * @vmf: The description of the fault
570  * @get_block: The filesystem method used to translate file offsets to blocks
571  *
572  * When a page fault occurs, filesystems may call this helper in their
573  * fault handler for DAX files. __dax_fault() assumes the caller has done all
574  * the necessary locking for the page fault to proceed successfully.
575  */
576 int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
577                         get_block_t get_block)
578 {
579         struct file *file = vma->vm_file;
580         struct address_space *mapping = file->f_mapping;
581         struct inode *inode = mapping->host;
582         struct page *page;
583         struct buffer_head bh;
584         unsigned long vaddr = (unsigned long)vmf->virtual_address;
585         unsigned blkbits = inode->i_blkbits;
586         sector_t block;
587         pgoff_t size;
588         int error;
589         int major = 0;
590
591         size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
592         if (vmf->pgoff >= size)
593                 return VM_FAULT_SIGBUS;
594
595         memset(&bh, 0, sizeof(bh));
596         block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
597         bh.b_bdev = inode->i_sb->s_bdev;
598         bh.b_size = PAGE_SIZE;
599
600  repeat:
601         page = find_get_page(mapping, vmf->pgoff);
602         if (page) {
603                 if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
604                         put_page(page);
605                         return VM_FAULT_RETRY;
606                 }
607                 if (unlikely(page->mapping != mapping)) {
608                         unlock_page(page);
609                         put_page(page);
610                         goto repeat;
611                 }
612         }
613
614         error = get_block(inode, block, &bh, 0);
615         if (!error && (bh.b_size < PAGE_SIZE))
616                 error = -EIO;           /* fs corruption? */
617         if (error)
618                 goto unlock_page;
619
620         if (!buffer_mapped(&bh) && !vmf->cow_page) {
621                 if (vmf->flags & FAULT_FLAG_WRITE) {
622                         error = get_block(inode, block, &bh, 1);
623                         count_vm_event(PGMAJFAULT);
624                         mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
625                         major = VM_FAULT_MAJOR;
626                         if (!error && (bh.b_size < PAGE_SIZE))
627                                 error = -EIO;
628                         if (error)
629                                 goto unlock_page;
630                 } else {
631                         return dax_load_hole(mapping, page, vmf);
632                 }
633         }
634
635         if (vmf->cow_page) {
636                 struct page *new_page = vmf->cow_page;
637                 if (buffer_written(&bh))
638                         error = copy_user_bh(new_page, inode, &bh, vaddr);
639                 else
640                         clear_user_highpage(new_page, vaddr);
641                 if (error)
642                         goto unlock_page;
643                 vmf->page = page;
644                 if (!page)
645                         i_mmap_lock_read(mapping);
646                 return VM_FAULT_LOCKED;
647         }
648
649         /* Check we didn't race with a read fault installing a new page */
650         if (!page && major)
651                 page = find_lock_page(mapping, vmf->pgoff);
652
653         if (page) {
654                 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
655                                                         PAGE_SIZE, 0);
656                 delete_from_page_cache(page);
657                 unlock_page(page);
658                 put_page(page);
659                 page = NULL;
660         }
661
662         /* Filesystem should not return unwritten buffers to us! */
663         WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
664         error = dax_insert_mapping(inode, &bh, vma, vmf);
665
666  out:
667         if (error == -ENOMEM)
668                 return VM_FAULT_OOM | major;
669         /* -EBUSY is fine, somebody else faulted on the same PTE */
670         if ((error < 0) && (error != -EBUSY))
671                 return VM_FAULT_SIGBUS | major;
672         return VM_FAULT_NOPAGE | major;
673
674  unlock_page:
675         if (page) {
676                 unlock_page(page);
677                 put_page(page);
678         }
679         goto out;
680 }
681 EXPORT_SYMBOL(__dax_fault);
682
683 /**
684  * dax_fault - handle a page fault on a DAX file
685  * @vma: The virtual memory area where the fault occurred
686  * @vmf: The description of the fault
687  * @get_block: The filesystem method used to translate file offsets to blocks
688  *
689  * When a page fault occurs, filesystems may call this helper in their
690  * fault handler for DAX files.
691  */
692 int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
693               get_block_t get_block)
694 {
695         int result;
696         struct super_block *sb = file_inode(vma->vm_file)->i_sb;
697
698         if (vmf->flags & FAULT_FLAG_WRITE) {
699                 sb_start_pagefault(sb);
700                 file_update_time(vma->vm_file);
701         }
702         result = __dax_fault(vma, vmf, get_block);
703         if (vmf->flags & FAULT_FLAG_WRITE)
704                 sb_end_pagefault(sb);
705
706         return result;
707 }
708 EXPORT_SYMBOL_GPL(dax_fault);
709
710 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
711 /*
712  * The 'colour' (ie low bits) within a PMD of a page offset.  This comes up
713  * more often than one might expect in the below function.
714  */
715 #define PG_PMD_COLOUR   ((PMD_SIZE >> PAGE_SHIFT) - 1)
716
717 static void __dax_dbg(struct buffer_head *bh, unsigned long address,
718                 const char *reason, const char *fn)
719 {
720         if (bh) {
721                 char bname[BDEVNAME_SIZE];
722                 bdevname(bh->b_bdev, bname);
723                 pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
724                         "length %zd fallback: %s\n", fn, current->comm,
725                         address, bname, bh->b_state, (u64)bh->b_blocknr,
726                         bh->b_size, reason);
727         } else {
728                 pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
729                         current->comm, address, reason);
730         }
731 }
732
733 #define dax_pmd_dbg(bh, address, reason)        __dax_dbg(bh, address, reason, "dax_pmd")
734
735 int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
736                 pmd_t *pmd, unsigned int flags, get_block_t get_block)
737 {
738         struct file *file = vma->vm_file;
739         struct address_space *mapping = file->f_mapping;
740         struct inode *inode = mapping->host;
741         struct buffer_head bh;
742         unsigned blkbits = inode->i_blkbits;
743         unsigned long pmd_addr = address & PMD_MASK;
744         bool write = flags & FAULT_FLAG_WRITE;
745         struct block_device *bdev;
746         pgoff_t size, pgoff;
747         sector_t block;
748         int error, result = 0;
749         bool alloc = false;
750
751         /* dax pmd mappings require pfn_t_devmap() */
752         if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
753                 return VM_FAULT_FALLBACK;
754
755         /* Fall back to PTEs if we're going to COW */
756         if (write && !(vma->vm_flags & VM_SHARED)) {
757                 split_huge_pmd(vma, pmd, address);
758                 dax_pmd_dbg(NULL, address, "cow write");
759                 return VM_FAULT_FALLBACK;
760         }
761         /* If the PMD would extend outside the VMA */
762         if (pmd_addr < vma->vm_start) {
763                 dax_pmd_dbg(NULL, address, "vma start unaligned");
764                 return VM_FAULT_FALLBACK;
765         }
766         if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
767                 dax_pmd_dbg(NULL, address, "vma end unaligned");
768                 return VM_FAULT_FALLBACK;
769         }
770
771         pgoff = linear_page_index(vma, pmd_addr);
772         size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
773         if (pgoff >= size)
774                 return VM_FAULT_SIGBUS;
775         /* If the PMD would cover blocks out of the file */
776         if ((pgoff | PG_PMD_COLOUR) >= size) {
777                 dax_pmd_dbg(NULL, address,
778                                 "offset + huge page size > file size");
779                 return VM_FAULT_FALLBACK;
780         }
781
782         memset(&bh, 0, sizeof(bh));
783         bh.b_bdev = inode->i_sb->s_bdev;
784         block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
785
786         bh.b_size = PMD_SIZE;
787
788         if (get_block(inode, block, &bh, 0) != 0)
789                 return VM_FAULT_SIGBUS;
790
791         if (!buffer_mapped(&bh) && write) {
792                 if (get_block(inode, block, &bh, 1) != 0)
793                         return VM_FAULT_SIGBUS;
794                 alloc = true;
795                 WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
796         }
797
798         bdev = bh.b_bdev;
799
800         /*
801          * If the filesystem isn't willing to tell us the length of a hole,
802          * just fall back to PTEs.  Calling get_block 512 times in a loop
803          * would be silly.
804          */
805         if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
806                 dax_pmd_dbg(&bh, address, "allocated block too small");
807                 return VM_FAULT_FALLBACK;
808         }
809
810         /*
811          * If we allocated new storage, make sure no process has any
812          * zero pages covering this hole
813          */
814         if (alloc) {
815                 loff_t lstart = pgoff << PAGE_SHIFT;
816                 loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
817
818                 truncate_pagecache_range(inode, lstart, lend);
819         }
820
821         i_mmap_lock_read(mapping);
822
823         if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
824                 spinlock_t *ptl;
825                 pmd_t entry;
826                 struct page *zero_page = get_huge_zero_page();
827
828                 if (unlikely(!zero_page)) {
829                         dax_pmd_dbg(&bh, address, "no zero page");
830                         goto fallback;
831                 }
832
833                 ptl = pmd_lock(vma->vm_mm, pmd);
834                 if (!pmd_none(*pmd)) {
835                         spin_unlock(ptl);
836                         dax_pmd_dbg(&bh, address, "pmd already present");
837                         goto fallback;
838                 }
839
840                 dev_dbg(part_to_dev(bdev->bd_part),
841                                 "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
842                                 __func__, current->comm, address,
843                                 (unsigned long long) to_sector(&bh, inode));
844
845                 entry = mk_pmd(zero_page, vma->vm_page_prot);
846                 entry = pmd_mkhuge(entry);
847                 set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
848                 result = VM_FAULT_NOPAGE;
849                 spin_unlock(ptl);
850         } else {
851                 struct blk_dax_ctl dax = {
852                         .sector = to_sector(&bh, inode),
853                         .size = PMD_SIZE,
854                 };
855                 long length = dax_map_atomic(bdev, &dax);
856
857                 if (length < 0) {
858                         result = VM_FAULT_SIGBUS;
859                         goto out;
860                 }
861                 if (length < PMD_SIZE) {
862                         dax_pmd_dbg(&bh, address, "dax-length too small");
863                         dax_unmap_atomic(bdev, &dax);
864                         goto fallback;
865                 }
866                 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
867                         dax_pmd_dbg(&bh, address, "pfn unaligned");
868                         dax_unmap_atomic(bdev, &dax);
869                         goto fallback;
870                 }
871
872                 if (!pfn_t_devmap(dax.pfn)) {
873                         dax_unmap_atomic(bdev, &dax);
874                         dax_pmd_dbg(&bh, address, "pfn not in memmap");
875                         goto fallback;
876                 }
877                 dax_unmap_atomic(bdev, &dax);
878
879                 /*
880                  * For PTE faults we insert a radix tree entry for reads, and
881                  * leave it clean.  Then on the first write we dirty the radix
882                  * tree entry via the dax_pfn_mkwrite() path.  This sequence
883                  * allows the dax_pfn_mkwrite() call to be simpler and avoid a
884                  * call into get_block() to translate the pgoff to a sector in
885                  * order to be able to create a new radix tree entry.
886                  *
887                  * The PMD path doesn't have an equivalent to
888                  * dax_pfn_mkwrite(), though, so for a read followed by a
889                  * write we traverse all the way through __dax_pmd_fault()
890                  * twice.  This means we can just skip inserting a radix tree
891                  * entry completely on the initial read and just wait until
892                  * the write to insert a dirty entry.
893                  */
894                 if (write) {
895                         error = dax_radix_entry(mapping, pgoff, dax.sector,
896                                         true, true);
897                         if (error) {
898                                 dax_pmd_dbg(&bh, address,
899                                                 "PMD radix insertion failed");
900                                 goto fallback;
901                         }
902                 }
903
904                 dev_dbg(part_to_dev(bdev->bd_part),
905                                 "%s: %s addr: %lx pfn: %lx sect: %llx\n",
906                                 __func__, current->comm, address,
907                                 pfn_t_to_pfn(dax.pfn),
908                                 (unsigned long long) dax.sector);
909                 result |= vmf_insert_pfn_pmd(vma, address, pmd,
910                                 dax.pfn, write);
911         }
912
913  out:
914         i_mmap_unlock_read(mapping);
915
916         return result;
917
918  fallback:
919         count_vm_event(THP_FAULT_FALLBACK);
920         result = VM_FAULT_FALLBACK;
921         goto out;
922 }
923 EXPORT_SYMBOL_GPL(__dax_pmd_fault);
924
925 /**
926  * dax_pmd_fault - handle a PMD fault on a DAX file
927  * @vma: The virtual memory area where the fault occurred
928  * @vmf: The description of the fault
929  * @get_block: The filesystem method used to translate file offsets to blocks
930  *
931  * When a page fault occurs, filesystems may call this helper in their
932  * pmd_fault handler for DAX files.
933  */
934 int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
935                         pmd_t *pmd, unsigned int flags, get_block_t get_block)
936 {
937         int result;
938         struct super_block *sb = file_inode(vma->vm_file)->i_sb;
939
940         if (flags & FAULT_FLAG_WRITE) {
941                 sb_start_pagefault(sb);
942                 file_update_time(vma->vm_file);
943         }
944         result = __dax_pmd_fault(vma, address, pmd, flags, get_block);
945         if (flags & FAULT_FLAG_WRITE)
946                 sb_end_pagefault(sb);
947
948         return result;
949 }
950 EXPORT_SYMBOL_GPL(dax_pmd_fault);
951 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
952
953 /**
954  * dax_pfn_mkwrite - handle first write to DAX page
955  * @vma: The virtual memory area where the fault occurred
956  * @vmf: The description of the fault
957  */
958 int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
959 {
960         struct file *file = vma->vm_file;
961         int error;
962
963         /*
964          * We pass NO_SECTOR to dax_radix_entry() because we expect that a
965          * RADIX_DAX_PTE entry already exists in the radix tree from a
966          * previous call to __dax_fault().  We just want to look up that PTE
967          * entry using vmf->pgoff and make sure the dirty tag is set.  This
968          * saves us from having to make a call to get_block() here to look
969          * up the sector.
970          */
971         error = dax_radix_entry(file->f_mapping, vmf->pgoff, NO_SECTOR, false,
972                         true);
973
974         if (error == -ENOMEM)
975                 return VM_FAULT_OOM;
976         if (error)
977                 return VM_FAULT_SIGBUS;
978         return VM_FAULT_NOPAGE;
979 }
980 EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
981
982 /**
983  * dax_zero_page_range - zero a range within a page of a DAX file
984  * @inode: The file being truncated
985  * @from: The file offset that is being truncated to
986  * @length: The number of bytes to zero
987  * @get_block: The filesystem method used to translate file offsets to blocks
988  *
989  * This function can be called by a filesystem when it is zeroing part of a
990  * page in a DAX file.  This is intended for hole-punch operations.  If
991  * you are truncating a file, the helper function dax_truncate_page() may be
992  * more convenient.
993  *
994  * We work in terms of PAGE_SIZE here for commonality with
995  * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
996  * took care of disposing of the unnecessary blocks.  Even if the filesystem
997  * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
998  * since the file might be mmapped.
999  */
1000 int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
1001                                                         get_block_t get_block)
1002 {
1003         struct buffer_head bh;
1004         pgoff_t index = from >> PAGE_SHIFT;
1005         unsigned offset = from & (PAGE_SIZE-1);
1006         int err;
1007
1008         /* Block boundary? Nothing to do */
1009         if (!length)
1010                 return 0;
1011         BUG_ON((offset + length) > PAGE_SIZE);
1012
1013         memset(&bh, 0, sizeof(bh));
1014         bh.b_bdev = inode->i_sb->s_bdev;
1015         bh.b_size = PAGE_SIZE;
1016         err = get_block(inode, index, &bh, 0);
1017         if (err < 0)
1018                 return err;
1019         if (buffer_written(&bh)) {
1020                 struct block_device *bdev = bh.b_bdev;
1021                 struct blk_dax_ctl dax = {
1022                         .sector = to_sector(&bh, inode),
1023                         .size = PAGE_SIZE,
1024                 };
1025
1026                 if (dax_map_atomic(bdev, &dax) < 0)
1027                         return PTR_ERR(dax.addr);
1028                 clear_pmem(dax.addr + offset, length);
1029                 wmb_pmem();
1030                 dax_unmap_atomic(bdev, &dax);
1031         }
1032
1033         return 0;
1034 }
1035 EXPORT_SYMBOL_GPL(dax_zero_page_range);
1036
1037 /**
1038  * dax_truncate_page - handle a partial page being truncated in a DAX file
1039  * @inode: The file being truncated
1040  * @from: The file offset that is being truncated to
1041  * @get_block: The filesystem method used to translate file offsets to blocks
1042  *
1043  * Similar to block_truncate_page(), this function can be called by a
1044  * filesystem when it is truncating a DAX file to handle the partial page.
1045  *
1046  * We work in terms of PAGE_SIZE here for commonality with
1047  * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
1048  * took care of disposing of the unnecessary blocks.  Even if the filesystem
1049  * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
1050  * since the file might be mmapped.
1051  */
1052 int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
1053 {
1054         unsigned length = PAGE_ALIGN(from) - from;
1055         return dax_zero_page_range(inode, from, length, get_block);
1056 }
1057 EXPORT_SYMBOL_GPL(dax_truncate_page);