Thomas Gleixner | 328970d | 2019-05-24 12:04:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 3 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 4 | * |
| 5 | * mmap.c |
| 6 | * |
| 7 | * Code to deal with the mess that is clustered mmap. |
| 8 | * |
| 9 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/types.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 14 | #include <linux/highmem.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/uio.h> |
| 17 | #include <linux/signal.h> |
| 18 | #include <linux/rbtree.h> |
| 19 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 20 | #include <cluster/masklog.h> |
| 21 | |
| 22 | #include "ocfs2.h" |
| 23 | |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 24 | #include "aops.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 25 | #include "dlmglue.h" |
| 26 | #include "file.h" |
| 27 | #include "inode.h" |
| 28 | #include "mmap.h" |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 29 | #include "super.h" |
Tao Ma | 614a9e8 | 2011-02-22 21:59:46 +0800 | [diff] [blame] | 30 | #include "ocfs2_trace.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 31 | |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 32 | |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 33 | static vm_fault_t ocfs2_fault(struct vm_fault *vmf) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 34 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 35 | struct vm_area_struct *vma = vmf->vma; |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 36 | sigset_t oldset; |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 37 | vm_fault_t ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 38 | |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 39 | ocfs2_block_signals(&oldset); |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 40 | ret = filemap_fault(vmf); |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 41 | ocfs2_unblock_signals(&oldset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 42 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 43 | trace_ocfs2_fault(OCFS2_I(vma->vm_file->f_mapping->host)->ip_blkno, |
| 44 | vma, vmf->page, vmf->pgoff); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 45 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 48 | static vm_fault_t __ocfs2_page_mkwrite(struct file *file, |
| 49 | struct buffer_head *di_bh, struct page *page) |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 50 | { |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 51 | int err; |
| 52 | vm_fault_t ret = VM_FAULT_NOPAGE; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 53 | struct inode *inode = file_inode(file); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 54 | struct address_space *mapping = inode->i_mapping; |
Nick Piggin | 1833633 | 2007-07-20 00:31:45 -0700 | [diff] [blame] | 55 | loff_t pos = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 56 | unsigned int len = PAGE_SIZE; |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 57 | pgoff_t last_index; |
| 58 | struct page *locked_page = NULL; |
| 59 | void *fsdata; |
| 60 | loff_t size = i_size_read(inode); |
| 61 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 62 | last_index = (size - 1) >> PAGE_SHIFT; |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 63 | |
| 64 | /* |
Wengang Wang | 5cffff9 | 2011-07-24 10:36:54 -0700 | [diff] [blame] | 65 | * There are cases that lead to the page no longer bebongs to the |
| 66 | * mapping. |
| 67 | * 1) pagecache truncates locally due to memory pressure. |
| 68 | * 2) pagecache truncates when another is taking EX lock against |
| 69 | * inode lock. see ocfs2_data_convert_worker. |
| 70 | * |
| 71 | * The i_size check doesn't catch the case where nodes truncated and |
| 72 | * then re-extended the file. We'll re-check the page mapping after |
| 73 | * taking the page lock inside of ocfs2_write_begin_nolock(). |
| 74 | * |
| 75 | * Let VM retry with these cases. |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 76 | */ |
Wengang Wang | 5cffff9 | 2011-07-24 10:36:54 -0700 | [diff] [blame] | 77 | if ((page->mapping != inode->i_mapping) || |
| 78 | (!PageUptodate(page)) || |
| 79 | (page_offset(page) >= size)) |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 80 | goto out; |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Call ocfs2_write_begin() and ocfs2_write_end() to take |
| 84 | * advantage of the allocation code there. We pass a write |
| 85 | * length of the whole page (chopped to i_size) to make sure |
| 86 | * the whole thing is allocated. |
| 87 | * |
| 88 | * Since we know the page is up to date, we don't have to |
| 89 | * worry about ocfs2_write_begin() skipping some buffer reads |
| 90 | * because the "write" would invalidate their data. |
| 91 | */ |
| 92 | if (page->index == last_index) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 93 | len = ((size - 1) & ~PAGE_MASK) + 1; |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 94 | |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 95 | err = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_MMAP, |
Ryan Ding | c1ad1e3 | 2016-03-25 14:20:52 -0700 | [diff] [blame] | 96 | &locked_page, &fsdata, di_bh, page); |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 97 | if (err) { |
| 98 | if (err != -ENOSPC) |
| 99 | mlog_errno(err); |
| 100 | ret = vmf_error(err); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 101 | goto out; |
| 102 | } |
| 103 | |
Wengang Wang | 5cffff9 | 2011-07-24 10:36:54 -0700 | [diff] [blame] | 104 | if (!locked_page) { |
| 105 | ret = VM_FAULT_NOPAGE; |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 106 | goto out; |
| 107 | } |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 108 | err = ocfs2_write_end_nolock(mapping, pos, len, len, fsdata); |
| 109 | BUG_ON(err != len); |
Wengang Wang | 5cffff9 | 2011-07-24 10:36:54 -0700 | [diff] [blame] | 110 | ret = VM_FAULT_LOCKED; |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 111 | out: |
| 112 | return ret; |
| 113 | } |
| 114 | |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 115 | static vm_fault_t ocfs2_page_mkwrite(struct vm_fault *vmf) |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 116 | { |
Nick Piggin | c2ec175 | 2009-03-31 15:23:21 -0700 | [diff] [blame] | 117 | struct page *page = vmf->page; |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 118 | struct inode *inode = file_inode(vmf->vma->vm_file); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 119 | struct buffer_head *di_bh = NULL; |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 120 | sigset_t oldset; |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 121 | int err; |
| 122 | vm_fault_t ret; |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 123 | |
Jan Kara | fef6925 | 2012-06-12 16:20:40 +0200 | [diff] [blame] | 124 | sb_start_pagefault(inode->i_sb); |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 125 | ocfs2_block_signals(&oldset); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * The cluster locks taken will block a truncate from another |
| 129 | * node. Taking the data lock will also ensure that we don't |
| 130 | * attempt page truncation as part of a downconvert. |
| 131 | */ |
Souptick Joarder | c6137fe | 2018-06-07 17:04:59 -0700 | [diff] [blame] | 132 | err = ocfs2_inode_lock(inode, &di_bh, 1); |
| 133 | if (err < 0) { |
| 134 | mlog_errno(err); |
| 135 | ret = vmf_error(err); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * The alloc sem should be enough to serialize with |
| 141 | * ocfs2_truncate_file() changing i_size as well as any thread |
| 142 | * modifying the inode btree. |
| 143 | */ |
| 144 | down_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 145 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 146 | ret = __ocfs2_page_mkwrite(vmf->vma->vm_file, di_bh, page); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 147 | |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 148 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 149 | |
| 150 | brelse(di_bh); |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 151 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 152 | |
| 153 | out: |
Joel Becker | e4b963f | 2009-09-02 17:17:36 -0700 | [diff] [blame] | 154 | ocfs2_unblock_signals(&oldset); |
Jan Kara | fef6925 | 2012-06-12 16:20:40 +0200 | [diff] [blame] | 155 | sb_end_pagefault(inode->i_sb); |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 156 | return ret; |
| 157 | } |
| 158 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 159 | static const struct vm_operations_struct ocfs2_file_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 160 | .fault = ocfs2_fault, |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 161 | .page_mkwrite = ocfs2_page_mkwrite, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 162 | }; |
| 163 | |
Christoph Hellwig | 870f481 | 2006-01-09 20:52:01 -0800 | [diff] [blame] | 164 | int ocfs2_mmap(struct file *file, struct vm_area_struct *vma) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 165 | { |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 166 | int ret = 0, lock_level = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 167 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 168 | ret = ocfs2_inode_lock_atime(file_inode(file), |
Gang He | c4c2416 | 2018-01-31 16:15:25 -0800 | [diff] [blame] | 169 | file->f_path.mnt, &lock_level, 1); |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 170 | if (ret < 0) { |
| 171 | mlog_errno(ret); |
| 172 | goto out; |
| 173 | } |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 174 | ocfs2_inode_unlock(file_inode(file), lock_level); |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 175 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 176 | vma->vm_ops = &ocfs2_file_vm_ops; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |