Blame view

fs/ocfs2/mmap.c 4.93 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
33
34
35
36
37
  #include <linux/highmem.h>
  #include <linux/pagemap.h>
  #include <linux/uio.h>
  #include <linux/signal.h>
  #include <linux/rbtree.h>
  
  #define MLOG_MASK_PREFIX ML_FILE_IO
  #include <cluster/masklog.h>
  
  #include "ocfs2.h"
7307de805   Mark Fasheh   ocfs2: shared wri...
38
  #include "aops.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
39
40
41
42
  #include "dlmglue.h"
  #include "file.h"
  #include "inode.h"
  #include "mmap.h"
e4b963f10   Joel Becker   ocfs2: Wrap signa...
43
  #include "super.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
44

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

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

d0217ac04   Nick Piggin   mm: fault feedbac...
51
52
  	mlog_entry("(area=%p, page offset=%lu)
  ", area, vmf->pgoff);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
53

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

d0217ac04   Nick Piggin   mm: fault feedbac...
58
59
  	mlog_exit_ptr(vmf->page);
  	return ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
60
  }
7307de805   Mark Fasheh   ocfs2: shared wri...
61
62
63
64
65
  static int __ocfs2_page_mkwrite(struct inode *inode, struct buffer_head *di_bh,
  				struct page *page)
  {
  	int ret;
  	struct address_space *mapping = inode->i_mapping;
183363380   Nick Piggin   fix some conversi...
66
  	loff_t pos = page_offset(page);
7307de805   Mark Fasheh   ocfs2: shared wri...
67
68
69
70
71
  	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...
72

7307de805   Mark Fasheh   ocfs2: shared wri...
73
74
75
  	/*
  	 * Another node might have truncated while we were waiting on
  	 * cluster locks.
f63afdb2c   Tao Ma   ocfs2: make __ocf...
76
77
  	 * We don't check size == 0 before the shift. This is borrowed
  	 * from do_generic_file_read.
7307de805   Mark Fasheh   ocfs2: shared wri...
78
  	 */
f63afdb2c   Tao Ma   ocfs2: make __ocf...
79
80
  	last_index = (size - 1) >> PAGE_CACHE_SHIFT;
  	if (unlikely(!size || page->index > last_index)) {
7307de805   Mark Fasheh   ocfs2: shared wri...
81
82
83
84
85
86
87
88
89
90
91
  		ret = -EINVAL;
  		goto out;
  	}
  
  	/*
  	 * The i_size check above 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().
  	 */
  	if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
4c1bbf1ba   Tao Ma   ocfs2: return 0 i...
92
93
94
95
96
  		/*
  		 * the page has been umapped in ocfs2_data_downconvert_worker.
  		 * So return 0 here and let VFS retry.
  		 */
  		ret = 0;
7307de805   Mark Fasheh   ocfs2: shared wri...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  		goto out;
  	}
  
  	/*
  	 * 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...
111
  		len = ((size - 1) & ~PAGE_CACHE_MASK) + 1;
7307de805   Mark Fasheh   ocfs2: shared wri...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  
  	ret = ocfs2_write_begin_nolock(mapping, pos, len, 0, &locked_page,
  				       &fsdata, di_bh, page);
  	if (ret) {
  		if (ret != -ENOSPC)
  			mlog_errno(ret);
  		goto out;
  	}
  
  	ret = ocfs2_write_end_nolock(mapping, pos, len, len, locked_page,
  				     fsdata);
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
  	BUG_ON(ret != len);
  	ret = 0;
  out:
  	return ret;
  }
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
132
  static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
133
  {
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
134
  	struct page *page = vmf->page;
7307de805   Mark Fasheh   ocfs2: shared wri...
135
136
  	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  	struct buffer_head *di_bh = NULL;
e4b963f10   Joel Becker   ocfs2: Wrap signa...
137
138
  	sigset_t oldset;
  	int ret;
7307de805   Mark Fasheh   ocfs2: shared wri...
139

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

89488984a   Mark Fasheh   ocfs2: Turn off s...
153
  	/*
7307de805   Mark Fasheh   ocfs2: shared wri...
154
155
156
  	 * 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...
157
  	 */
7307de805   Mark Fasheh   ocfs2: shared wri...
158
  	down_write(&OCFS2_I(inode)->ip_alloc_sem);
7307de805   Mark Fasheh   ocfs2: shared wri...
159
  	ret = __ocfs2_page_mkwrite(inode, di_bh, page);
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);
c2ec175c3   Nick Piggin   mm: page_mkwrite ...
167
168
  	if (ret)
  		ret = VM_FAULT_SIGBUS;
7307de805   Mark Fasheh   ocfs2: shared wri...
169
170
  	return ret;
  }
f0f37e2f7   Alexey Dobriyan   const: mark struc...
171
  static const struct vm_operations_struct ocfs2_file_vm_ops = {
54cb8821d   Nick Piggin   mm: merge populat...
172
  	.fault		= ocfs2_fault,
7307de805   Mark Fasheh   ocfs2: shared wri...
173
174
175
176
177
178
  	.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...
179
  	ret = ocfs2_inode_lock_atime(file->f_dentry->d_inode,
25899deef   Tiger Yang   ocfs2: update fil...
180
181
182
183
184
  				    file->f_vfsmnt, &lock_level);
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
185
  	ocfs2_inode_unlock(file->f_dentry->d_inode, lock_level);
25899deef   Tiger Yang   ocfs2: update fil...
186
  out:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
187
  	vma->vm_ops = &ocfs2_file_vm_ops;
d0217ac04   Nick Piggin   mm: fault feedbac...
188
  	vma->vm_flags |= VM_CAN_NONLINEAR;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
189
190
  	return 0;
  }