Blame view

fs/ocfs2/mmap.c 5.01 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

d0217ac04   Nick Piggin   mm: fault feedbac...
45
  static int ocfs2_fault(struct vm_area_struct *area, struct vm_fault *vmf)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
46
  {
e4b963f10   Joel Becker   ocfs2: Wrap signa...
47
48
  	sigset_t oldset;
  	int ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
49

e4b963f10   Joel Becker   ocfs2: Wrap signa...
50
  	ocfs2_block_signals(&oldset);
d0217ac04   Nick Piggin   mm: fault feedbac...
51
  	ret = filemap_fault(area, vmf);
e4b963f10   Joel Becker   ocfs2: Wrap signa...
52
  	ocfs2_unblock_signals(&oldset);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
53

614a9e849   Tao Ma   ocfs2: Remove FIL...
54
55
  	trace_ocfs2_fault(OCFS2_I(area->vm_file->f_mapping->host)->ip_blkno,
  			  area, vmf->page, vmf->pgoff);
d0217ac04   Nick Piggin   mm: fault feedbac...
56
  	return ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
57
  }
0378da0fd   Tao Ma   ocfs2: pass struc...
58
  static int __ocfs2_page_mkwrite(struct file *file, struct buffer_head *di_bh,
7307de805   Mark Fasheh   ocfs2: shared wri...
59
60
  				struct page *page)
  {
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
61
  	int ret = VM_FAULT_NOPAGE;
0378da0fd   Tao Ma   ocfs2: pass struc...
62
  	struct inode *inode = file->f_path.dentry->d_inode;
7307de805   Mark Fasheh   ocfs2: shared wri...
63
  	struct address_space *mapping = inode->i_mapping;
183363380   Nick Piggin   fix some conversi...
64
  	loff_t pos = page_offset(page);
7307de805   Mark Fasheh   ocfs2: shared wri...
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   Mark Fasheh   [PATCH] OCFS2: Th...
70

f63afdb2c   Tao Ma   ocfs2: make __ocf...
71
  	last_index = (size - 1) >> PAGE_CACHE_SHIFT;
7307de805   Mark Fasheh   ocfs2: shared wri...
72
73
  
  	/*
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
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   Mark Fasheh   ocfs2: shared wri...
85
  	 */
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
86
87
88
  	if ((page->mapping != inode->i_mapping) ||
  	    (!PageUptodate(page)) ||
  	    (page_offset(page) >= size))
7307de805   Mark Fasheh   ocfs2: shared wri...
89
  		goto out;
7307de805   Mark Fasheh   ocfs2: shared wri...
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   Tao Ma   ocfs2: make __ocf...
102
  		len = ((size - 1) & ~PAGE_CACHE_MASK) + 1;
7307de805   Mark Fasheh   ocfs2: shared wri...
103

0378da0fd   Tao Ma   ocfs2: pass struc...
104
  	ret = ocfs2_write_begin_nolock(file, mapping, pos, len, 0, &locked_page,
7307de805   Mark Fasheh   ocfs2: shared wri...
105
106
107
108
  				       &fsdata, di_bh, page);
  	if (ret) {
  		if (ret != -ENOSPC)
  			mlog_errno(ret);
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
109
110
111
112
  		if (ret == -ENOMEM)
  			ret = VM_FAULT_OOM;
  		else
  			ret = VM_FAULT_SIGBUS;
7307de805   Mark Fasheh   ocfs2: shared wri...
113
114
  		goto out;
  	}
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
115
116
  	if (!locked_page) {
  		ret = VM_FAULT_NOPAGE;
7307de805   Mark Fasheh   ocfs2: shared wri...
117
118
  		goto out;
  	}
5cffff9e2   Wengang Wang   ocfs2: Fix ocfs2_...
119
120
  	ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page,
  				     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;
  }
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
126
  static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
127
  {
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
128
  	struct page *page = vmf->page;
7307de805   Mark Fasheh   ocfs2: shared wri...
129
130
  	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  	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

e4b963f10   Joel Becker   ocfs2: Wrap signa...
134
  	ocfs2_block_signals(&oldset);
7307de805   Mark Fasheh   ocfs2: shared wri...
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   Mark Fasheh   ocfs2: Rename ocf...
141
  	ret = ocfs2_inode_lock(inode, &di_bh, 1);
7307de805   Mark Fasheh   ocfs2: shared wri...
142
143
144
145
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
25899deef   Tiger Yang   ocfs2: update fil...
146

89488984a   Mark Fasheh   ocfs2: Turn off s...
147
  	/*
7307de805   Mark Fasheh   ocfs2: shared wri...
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   Mark Fasheh   ocfs2: Turn off s...
151
  	 */
7307de805   Mark Fasheh   ocfs2: shared wri...
152
  	down_write(&OCFS2_I(inode)->ip_alloc_sem);
0378da0fd   Tao Ma   ocfs2: pass struc...
153
  	ret = __ocfs2_page_mkwrite(vma->vm_file, di_bh, page);
7307de805   Mark Fasheh   ocfs2: shared wri...
154

7307de805   Mark Fasheh   ocfs2: shared wri...
155
156
157
  	up_write(&OCFS2_I(inode)->ip_alloc_sem);
  
  	brelse(di_bh);
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
158
  	ocfs2_inode_unlock(inode, 1);
7307de805   Mark Fasheh   ocfs2: shared wri...
159
160
  
  out:
e4b963f10   Joel Becker   ocfs2: Wrap signa...
161
  	ocfs2_unblock_signals(&oldset);
7307de805   Mark Fasheh   ocfs2: shared wri...
162
163
  	return ret;
  }
f0f37e2f7   Alexey Dobriyan   const: mark struc...
164
  static const struct vm_operations_struct ocfs2_file_vm_ops = {
54cb8821d   Nick Piggin   mm: merge populat...
165
  	.fault		= ocfs2_fault,
7307de805   Mark Fasheh   ocfs2: shared wri...
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   Mark Fasheh   ocfs2: Rename ocf...
172
  	ret = ocfs2_inode_lock_atime(file->f_dentry->d_inode,
25899deef   Tiger Yang   ocfs2: update fil...
173
174
175
176
177
  				    file->f_vfsmnt, &lock_level);
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
178
  	ocfs2_inode_unlock(file->f_dentry->d_inode, lock_level);
25899deef   Tiger Yang   ocfs2: update fil...
179
  out:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
180
  	vma->vm_ops = &ocfs2_file_vm_ops;
d0217ac04   Nick Piggin   mm: fault feedbac...
181
  	vma->vm_flags |= VM_CAN_NONLINEAR;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
182
183
  	return 0;
  }