Blame view
fs/ocfs2/mmap.c
5.01 KB
ccd979bdb
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* -*- mode: c; c-basic-offset: 8; -*- * vim: noexpandtab sw=8 ts=8 sts=0: * * mmap.c * * Code to deal with the mess that is clustered mmap. * * Copyright (C) 2002, 2004 Oracle. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. */ #include <linux/fs.h> #include <linux/types.h> |
ccd979bdb
|
28 29 30 31 32 |
#include <linux/highmem.h> #include <linux/pagemap.h> #include <linux/uio.h> #include <linux/signal.h> #include <linux/rbtree.h> |
ccd979bdb
|
33 34 35 |
#include <cluster/masklog.h> #include "ocfs2.h" |
7307de805
|
36 |
#include "aops.h" |
ccd979bdb
|
37 38 39 40 |
#include "dlmglue.h" #include "file.h" #include "inode.h" #include "mmap.h" |
e4b963f10
|
41 |
#include "super.h" |
614a9e849
|
42 |
#include "ocfs2_trace.h" |
ccd979bdb
|
43 |
|
7307de805
|
44 |
|
d0217ac04
|
45 |
static int ocfs2_fault(struct vm_area_struct *area, struct vm_fault *vmf) |
ccd979bdb
|
46 |
{ |
e4b963f10
|
47 48 |
sigset_t oldset; int ret; |
ccd979bdb
|
49 |
|
e4b963f10
|
50 |
ocfs2_block_signals(&oldset); |
d0217ac04
|
51 |
ret = filemap_fault(area, vmf); |
e4b963f10
|
52 |
ocfs2_unblock_signals(&oldset); |
ccd979bdb
|
53 |
|
614a9e849
|
54 55 |
trace_ocfs2_fault(OCFS2_I(area->vm_file->f_mapping->host)->ip_blkno, area, vmf->page, vmf->pgoff); |
d0217ac04
|
56 |
return ret; |
ccd979bdb
|
57 |
} |
0378da0fd
|
58 |
static int __ocfs2_page_mkwrite(struct file *file, struct buffer_head *di_bh, |
7307de805
|
59 60 |
struct page *page) { |
5cffff9e2
|
61 |
int ret = VM_FAULT_NOPAGE; |
0378da0fd
|
62 |
struct inode *inode = file->f_path.dentry->d_inode; |
7307de805
|
63 |
struct address_space *mapping = inode->i_mapping; |
183363380
|
64 |
loff_t pos = page_offset(page); |
7307de805
|
65 66 67 68 69 |
unsigned int len = PAGE_CACHE_SIZE; pgoff_t last_index; struct page *locked_page = NULL; void *fsdata; loff_t size = i_size_read(inode); |
ccd979bdb
|
70 |
|
f63afdb2c
|
71 |
last_index = (size - 1) >> PAGE_CACHE_SHIFT; |
7307de805
|
72 73 |
/* |
5cffff9e2
|
74 75 76 77 78 79 80 81 82 83 84 |
* There are cases that lead to the page no longer bebongs to the * mapping. * 1) pagecache truncates locally due to memory pressure. * 2) pagecache truncates when another is taking EX lock against * inode lock. see ocfs2_data_convert_worker. * * The i_size check doesn't catch the case where nodes truncated and * then re-extended the file. We'll re-check the page mapping after * taking the page lock inside of ocfs2_write_begin_nolock(). * * Let VM retry with these cases. |
7307de805
|
85 |
*/ |
5cffff9e2
|
86 87 88 |
if ((page->mapping != inode->i_mapping) || (!PageUptodate(page)) || (page_offset(page) >= size)) |
7307de805
|
89 |
goto out; |
7307de805
|
90 91 92 93 94 95 96 97 98 99 100 101 |
/* * Call ocfs2_write_begin() and ocfs2_write_end() to take * advantage of the allocation code there. We pass a write * length of the whole page (chopped to i_size) to make sure * the whole thing is allocated. * * Since we know the page is up to date, we don't have to * worry about ocfs2_write_begin() skipping some buffer reads * because the "write" would invalidate their data. */ if (page->index == last_index) |
f63afdb2c
|
102 |
len = ((size - 1) & ~PAGE_CACHE_MASK) + 1; |
7307de805
|
103 |
|
0378da0fd
|
104 |
ret = ocfs2_write_begin_nolock(file, mapping, pos, len, 0, &locked_page, |
7307de805
|
105 106 107 108 |
&fsdata, di_bh, page); if (ret) { if (ret != -ENOSPC) mlog_errno(ret); |
5cffff9e2
|
109 110 111 112 |
if (ret == -ENOMEM) ret = VM_FAULT_OOM; else ret = VM_FAULT_SIGBUS; |
7307de805
|
113 114 |
goto out; } |
5cffff9e2
|
115 116 |
if (!locked_page) { ret = VM_FAULT_NOPAGE; |
7307de805
|
117 118 |
goto out; } |
5cffff9e2
|
119 120 |
ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page, fsdata); |
7307de805
|
121 |
BUG_ON(ret != len); |
5cffff9e2
|
122 |
ret = VM_FAULT_LOCKED; |
7307de805
|
123 124 125 |
out: return ret; } |
c2ec175c3
|
126 |
static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
ccd979bdb
|
127 |
{ |
c2ec175c3
|
128 |
struct page *page = vmf->page; |
7307de805
|
129 130 |
struct inode *inode = vma->vm_file->f_path.dentry->d_inode; struct buffer_head *di_bh = NULL; |
e4b963f10
|
131 132 |
sigset_t oldset; int ret; |
7307de805
|
133 |
|
e4b963f10
|
134 |
ocfs2_block_signals(&oldset); |
7307de805
|
135 136 137 138 139 140 |
/* * The cluster locks taken will block a truncate from another * node. Taking the data lock will also ensure that we don't * attempt page truncation as part of a downconvert. */ |
e63aecb65
|
141 |
ret = ocfs2_inode_lock(inode, &di_bh, 1); |
7307de805
|
142 143 144 145 |
if (ret < 0) { mlog_errno(ret); goto out; } |
25899deef
|
146 |
|
89488984a
|
147 |
/* |
7307de805
|
148 149 150 |
* The alloc sem should be enough to serialize with * ocfs2_truncate_file() changing i_size as well as any thread * modifying the inode btree. |
89488984a
|
151 |
*/ |
7307de805
|
152 |
down_write(&OCFS2_I(inode)->ip_alloc_sem); |
0378da0fd
|
153 |
ret = __ocfs2_page_mkwrite(vma->vm_file, di_bh, page); |
7307de805
|
154 |
|
7307de805
|
155 156 157 |
up_write(&OCFS2_I(inode)->ip_alloc_sem); brelse(di_bh); |
e63aecb65
|
158 |
ocfs2_inode_unlock(inode, 1); |
7307de805
|
159 160 |
out: |
e4b963f10
|
161 |
ocfs2_unblock_signals(&oldset); |
7307de805
|
162 163 |
return ret; } |
f0f37e2f7
|
164 |
static const struct vm_operations_struct ocfs2_file_vm_ops = { |
54cb8821d
|
165 |
.fault = ocfs2_fault, |
7307de805
|
166 167 168 169 170 171 |
.page_mkwrite = ocfs2_page_mkwrite, }; int ocfs2_mmap(struct file *file, struct vm_area_struct *vma) { int ret = 0, lock_level = 0; |
e63aecb65
|
172 |
ret = ocfs2_inode_lock_atime(file->f_dentry->d_inode, |
25899deef
|
173 174 175 176 177 |
file->f_vfsmnt, &lock_level); if (ret < 0) { mlog_errno(ret); goto out; } |
e63aecb65
|
178 |
ocfs2_inode_unlock(file->f_dentry->d_inode, lock_level); |
25899deef
|
179 |
out: |
ccd979bdb
|
180 |
vma->vm_ops = &ocfs2_file_vm_ops; |
d0217ac04
|
181 |
vma->vm_flags |= VM_CAN_NONLINEAR; |
ccd979bdb
|
182 183 |
return 0; } |