Blame view

fs/ocfs2/mmap.c 5.04 KB
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
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   Mark Fasheh   [PATCH] OCFS2: Th...
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   Mark Fasheh   [PATCH] OCFS2: Th...
33
34
35
  #include <cluster/masklog.h>
  
  #include "ocfs2.h"
7307de805   Mark Fasheh   ocfs2: shared wri...
36
  #include "aops.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
37
38
39
40
  #include "dlmglue.h"
  #include "file.h"
  #include "inode.h"
  #include "mmap.h"
e4b963f10   Joel Becker   ocfs2: Wrap signa...
41
  #include "super.h"
614a9e849   Tao Ma   ocfs2: Remove FIL...
42
  #include "ocfs2_trace.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
43

7307de805   Mark Fasheh   ocfs2: shared wri...
44

11bac8000   Dave Jiang   mm, fs: reduce fa...
45
  static int ocfs2_fault(struct vm_fault *vmf)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
46
  {
11bac8000   Dave Jiang   mm, fs: reduce fa...
47
  	struct vm_area_struct *vma = vmf->vma;
e4b963f10   Joel Becker   ocfs2: Wrap signa...
48
49
  	sigset_t oldset;
  	int ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
50

e4b963f10   Joel Becker   ocfs2: Wrap signa...
51
  	ocfs2_block_signals(&oldset);
11bac8000   Dave Jiang   mm, fs: reduce fa...
52
  	ret = filemap_fault(vmf);
e4b963f10   Joel Becker   ocfs2: Wrap signa...
53
  	ocfs2_unblock_signals(&oldset);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
54

11bac8000   Dave Jiang   mm, fs: reduce fa...
55
56
  	trace_ocfs2_fault(OCFS2_I(vma->vm_file->f_mapping->host)->ip_blkno,
  			  vma, vmf->page, vmf->pgoff);
d0217ac04   Nick Piggin   mm: fault feedbac...
57
  	return ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
58
  }
0378da0fd   Tao Ma   ocfs2: pass struc...
59
  static int __ocfs2_page_mkwrite(struct file *file, struct buffer_head *di_bh,
7307de805   Mark Fasheh   ocfs2: shared wri...
60
61
  				struct page *page)
  {
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
62
  	int ret = VM_FAULT_NOPAGE;
496ad9aa8   Al Viro   new helper: file_...
63
  	struct inode *inode = file_inode(file);
7307de805   Mark Fasheh   ocfs2: shared wri...
64
  	struct address_space *mapping = inode->i_mapping;
183363380   Nick Piggin   fix some conversi...
65
  	loff_t pos = page_offset(page);
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
66
  	unsigned int len = PAGE_SIZE;
7307de805   Mark Fasheh   ocfs2: shared wri...
67
68
69
70
  	pgoff_t last_index;
  	struct page *locked_page = NULL;
  	void *fsdata;
  	loff_t size = i_size_read(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
71

09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
72
  	last_index = (size - 1) >> PAGE_SHIFT;
7307de805   Mark Fasheh   ocfs2: shared wri...
73
74
  
  	/*
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
75
76
77
78
79
80
81
82
83
84
85
  	 * 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   Mark Fasheh   ocfs2: shared wri...
86
  	 */
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
87
88
89
  	if ((page->mapping != inode->i_mapping) ||
  	    (!PageUptodate(page)) ||
  	    (page_offset(page) >= size))
7307de805   Mark Fasheh   ocfs2: shared wri...
90
  		goto out;
7307de805   Mark Fasheh   ocfs2: shared wri...
91
92
93
94
95
96
97
98
99
100
101
102
  
  	/*
  	 * 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)
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
103
  		len = ((size - 1) & ~PAGE_MASK) + 1;
7307de805   Mark Fasheh   ocfs2: shared wri...
104

c1ad1e3ca   Ryan Ding   ocfs2: add ocfs2_...
105
106
  	ret = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_MMAP,
  				       &locked_page, &fsdata, di_bh, page);
7307de805   Mark Fasheh   ocfs2: shared wri...
107
108
109
  	if (ret) {
  		if (ret != -ENOSPC)
  			mlog_errno(ret);
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
110
111
112
113
  		if (ret == -ENOMEM)
  			ret = VM_FAULT_OOM;
  		else
  			ret = VM_FAULT_SIGBUS;
7307de805   Mark Fasheh   ocfs2: shared wri...
114
115
  		goto out;
  	}
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
116
117
  	if (!locked_page) {
  		ret = VM_FAULT_NOPAGE;
7307de805   Mark Fasheh   ocfs2: shared wri...
118
119
  		goto out;
  	}
07f38d971   piaojun   ocfs2: clean up u...
120
  	ret = ocfs2_write_end_nolock(mapping, pos, len, len, fsdata);
7307de805   Mark Fasheh   ocfs2: shared wri...
121
  	BUG_ON(ret != len);
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
122
  	ret = VM_FAULT_LOCKED;
7307de805   Mark Fasheh   ocfs2: shared wri...
123
124
125
  out:
  	return ret;
  }
11bac8000   Dave Jiang   mm, fs: reduce fa...
126
  static int ocfs2_page_mkwrite(struct vm_fault *vmf)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
127
  {
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
128
  	struct page *page = vmf->page;
11bac8000   Dave Jiang   mm, fs: reduce fa...
129
  	struct inode *inode = file_inode(vmf->vma->vm_file);
7307de805   Mark Fasheh   ocfs2: shared wri...
130
  	struct buffer_head *di_bh = NULL;
e4b963f10   Joel Becker   ocfs2: Wrap signa...
131
132
  	sigset_t oldset;
  	int ret;
7307de805   Mark Fasheh   ocfs2: shared wri...
133

fef6925cd   Jan Kara   ocfs2: Convert to...
134
  	sb_start_pagefault(inode->i_sb);
e4b963f10   Joel Becker   ocfs2: Wrap signa...
135
  	ocfs2_block_signals(&oldset);
7307de805   Mark Fasheh   ocfs2: shared wri...
136
137
138
139
140
141
  
  	/*
  	 * 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   Mark Fasheh   ocfs2: Rename ocf...
142
  	ret = ocfs2_inode_lock(inode, &di_bh, 1);
7307de805   Mark Fasheh   ocfs2: shared wri...
143
144
  	if (ret < 0) {
  		mlog_errno(ret);
566e8dfd8   Jan Kara   ocfs2: fix return...
145
146
147
148
  		if (ret == -ENOMEM)
  			ret = VM_FAULT_OOM;
  		else
  			ret = VM_FAULT_SIGBUS;
7307de805   Mark Fasheh   ocfs2: shared wri...
149
150
  		goto out;
  	}
25899deef   Tiger Yang   ocfs2: update fil...
151

89488984a   Mark Fasheh   ocfs2: Turn off s...
152
  	/*
7307de805   Mark Fasheh   ocfs2: shared wri...
153
154
155
  	 * 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   Mark Fasheh   ocfs2: Turn off s...
156
  	 */
7307de805   Mark Fasheh   ocfs2: shared wri...
157
  	down_write(&OCFS2_I(inode)->ip_alloc_sem);
11bac8000   Dave Jiang   mm, fs: reduce fa...
158
  	ret = __ocfs2_page_mkwrite(vmf->vma->vm_file, di_bh, page);
7307de805   Mark Fasheh   ocfs2: shared wri...
159

7307de805   Mark Fasheh   ocfs2: shared wri...
160
161
162
  	up_write(&OCFS2_I(inode)->ip_alloc_sem);
  
  	brelse(di_bh);
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
163
  	ocfs2_inode_unlock(inode, 1);
7307de805   Mark Fasheh   ocfs2: shared wri...
164
165
  
  out:
e4b963f10   Joel Becker   ocfs2: Wrap signa...
166
  	ocfs2_unblock_signals(&oldset);
fef6925cd   Jan Kara   ocfs2: Convert to...
167
  	sb_end_pagefault(inode->i_sb);
7307de805   Mark Fasheh   ocfs2: shared wri...
168
169
  	return ret;
  }
f0f37e2f7   Alexey Dobriyan   const: mark struc...
170
  static const struct vm_operations_struct ocfs2_file_vm_ops = {
54cb8821d   Nick Piggin   mm: merge populat...
171
  	.fault		= ocfs2_fault,
7307de805   Mark Fasheh   ocfs2: shared wri...
172
173
174
175
176
177
  	.page_mkwrite	= ocfs2_page_mkwrite,
  };
  
  int ocfs2_mmap(struct file *file, struct vm_area_struct *vma)
  {
  	int ret = 0, lock_level = 0;
496ad9aa8   Al Viro   new helper: file_...
178
  	ret = ocfs2_inode_lock_atime(file_inode(file),
182be6847   Al Viro   kill f_vfsmnt
179
  				    file->f_path.mnt, &lock_level);
25899deef   Tiger Yang   ocfs2: update fil...
180
181
182
183
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
496ad9aa8   Al Viro   new helper: file_...
184
  	ocfs2_inode_unlock(file_inode(file), lock_level);
25899deef   Tiger Yang   ocfs2: update fil...
185
  out:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
186
187
188
  	vma->vm_ops = &ocfs2_file_vm_ops;
  	return 0;
  }