cd93644c7d705279d7c2d3798c78423387bca38f
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / gfs2 / page.c
1 /*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
8 */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/pagemap.h>
16 #include <linux/mm.h>
17 #include <linux/gfs2_ondisk.h>
18
19 #include "gfs2.h"
20 #include "lm_interface.h"
21 #include "incore.h"
22 #include "bmap.h"
23 #include "inode.h"
24 #include "page.h"
25 #include "trans.h"
26 #include "ops_address.h"
27 #include "util.h"
28
29 /**
30 * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
31 * @gl: the glock
32 *
33 */
34
35 void gfs2_pte_inval(struct gfs2_glock *gl)
36 {
37 struct gfs2_inode *ip;
38 struct inode *inode;
39
40 ip = gl->gl_object;
41 if (!ip || !S_ISREG(ip->i_di.di_mode))
42 return;
43
44 if (!test_bit(GIF_PAGED, &ip->i_flags))
45 return;
46
47 inode = gfs2_ip2v_lookup(ip);
48 if (inode) {
49 unmap_shared_mapping_range(inode->i_mapping, 0, 0);
50 iput(inode);
51
52 if (test_bit(GIF_SW_PAGED, &ip->i_flags))
53 set_bit(GLF_DIRTY, &gl->gl_flags);
54 }
55
56 clear_bit(GIF_SW_PAGED, &ip->i_flags);
57 }
58
59 /**
60 * gfs2_page_inval - Invalidate all pages associated with a glock
61 * @gl: the glock
62 *
63 */
64
65 void gfs2_page_inval(struct gfs2_glock *gl)
66 {
67 struct gfs2_inode *ip;
68 struct inode *inode;
69
70 ip = gl->gl_object;
71 if (!ip || !S_ISREG(ip->i_di.di_mode))
72 return;
73
74 inode = gfs2_ip2v_lookup(ip);
75 if (inode) {
76 struct address_space *mapping = inode->i_mapping;
77
78 truncate_inode_pages(mapping, 0);
79 gfs2_assert_withdraw(ip->i_sbd, !mapping->nrpages);
80
81 iput(inode);
82 }
83
84 clear_bit(GIF_PAGED, &ip->i_flags);
85 }
86
87 /**
88 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
89 * @gl: the glock
90 * @flags: DIO_START | DIO_WAIT
91 *
92 * Syncs data (not metadata) for a regular file.
93 * No-op for all other types.
94 */
95
96 void gfs2_page_sync(struct gfs2_glock *gl, int flags)
97 {
98 struct gfs2_inode *ip;
99 struct inode *inode;
100
101 ip = gl->gl_object;
102 if (!ip || !S_ISREG(ip->i_di.di_mode))
103 return;
104
105 inode = gfs2_ip2v_lookup(ip);
106 if (inode) {
107 struct address_space *mapping = inode->i_mapping;
108 int error = 0;
109
110 if (flags & DIO_START)
111 filemap_fdatawrite(mapping);
112 if (!error && (flags & DIO_WAIT))
113 error = filemap_fdatawait(mapping);
114
115 /* Put back any errors cleared by filemap_fdatawait()
116 so they can be caught by someone who can pass them
117 up to user space. */
118
119 if (error == -ENOSPC)
120 set_bit(AS_ENOSPC, &mapping->flags);
121 else if (error)
122 set_bit(AS_EIO, &mapping->flags);
123
124 iput(inode);
125 }
126 }
127
128 /**
129 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
130 * @ip: the inode
131 * @dibh: the dinode buffer
132 * @block: the block number that was allocated
133 * @private: any locked page held by the caller process
134 *
135 * Returns: errno
136 */
137
138 int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
139 uint64_t block, void *private)
140 {
141 struct gfs2_sbd *sdp = ip->i_sbd;
142 struct inode *inode = ip->i_vnode;
143 struct page *page = (struct page *)private;
144 struct buffer_head *bh;
145 int release = 0;
146
147 if (!page || page->index) {
148 page = grab_cache_page(inode->i_mapping, 0);
149 if (!page)
150 return -ENOMEM;
151 release = 1;
152 }
153
154 if (!PageUptodate(page)) {
155 void *kaddr = kmap(page);
156
157 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
158 ip->i_di.di_size);
159 memset(kaddr + ip->i_di.di_size, 0,
160 PAGE_CACHE_SIZE - ip->i_di.di_size);
161 kunmap(page);
162
163 SetPageUptodate(page);
164 }
165
166 if (!page_has_buffers(page))
167 create_empty_buffers(page, 1 << inode->i_blkbits,
168 (1 << BH_Uptodate));
169
170 bh = page_buffers(page);
171
172 if (!buffer_mapped(bh))
173 map_bh(bh, inode->i_sb, block);
174
175 set_buffer_uptodate(bh);
176 if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED) || gfs2_is_jdata(ip))
177 gfs2_trans_add_bh(ip->i_gl, bh, 0);
178 mark_buffer_dirty(bh);
179
180 if (release) {
181 unlock_page(page);
182 page_cache_release(page);
183 }
184
185 return 0;
186 }
187
188 /**
189 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
190 *
191 * This is partly borrowed from ext3.
192 */
193 int gfs2_block_truncate_page(struct address_space *mapping)
194 {
195 struct inode *inode = mapping->host;
196 struct gfs2_inode *ip = inode->u.generic_ip;
197 struct gfs2_sbd *sdp = ip->i_sbd;
198 loff_t from = inode->i_size;
199 unsigned long index = from >> PAGE_CACHE_SHIFT;
200 unsigned offset = from & (PAGE_CACHE_SIZE-1);
201 unsigned blocksize, iblock, length, pos;
202 struct buffer_head *bh;
203 struct page *page;
204 void *kaddr;
205 int err;
206
207 page = grab_cache_page(mapping, index);
208 if (!page)
209 return 0;
210
211 blocksize = inode->i_sb->s_blocksize;
212 length = blocksize - (offset & (blocksize - 1));
213 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
214
215 if (!page_has_buffers(page))
216 create_empty_buffers(page, blocksize, 0);
217
218 /* Find the buffer that contains "offset" */
219 bh = page_buffers(page);
220 pos = blocksize;
221 while (offset >= pos) {
222 bh = bh->b_this_page;
223 iblock++;
224 pos += blocksize;
225 }
226
227 err = 0;
228
229 if (!buffer_mapped(bh)) {
230 gfs2_get_block(inode, iblock, bh, 0);
231 /* unmapped? It's a hole - nothing to do */
232 if (!buffer_mapped(bh))
233 goto unlock;
234 }
235
236 /* Ok, it's mapped. Make sure it's up-to-date */
237 if (PageUptodate(page))
238 set_buffer_uptodate(bh);
239
240 if (!buffer_uptodate(bh)) {
241 err = -EIO;
242 ll_rw_block(READ, 1, &bh);
243 wait_on_buffer(bh);
244 /* Uhhuh. Read error. Complain and punt. */
245 if (!buffer_uptodate(bh))
246 goto unlock;
247 }
248
249 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
250 gfs2_trans_add_bh(ip->i_gl, bh, 0);
251
252 kaddr = kmap_atomic(page, KM_USER0);
253 memset(kaddr + offset, 0, length);
254 flush_dcache_page(page);
255 kunmap_atomic(kaddr, KM_USER0);
256
257 unlock:
258 unlock_page(page);
259 page_cache_release(page);
260 return err;
261 }
262
263 void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
264 unsigned int from, unsigned int to)
265 {
266 struct buffer_head *head = page_buffers(page);
267 unsigned int bsize = head->b_size;
268 struct buffer_head *bh;
269 unsigned int start, end;
270
271 for (bh = head, start = 0;
272 bh != head || !start;
273 bh = bh->b_this_page, start = end) {
274 end = start + bsize;
275 if (end <= from || start >= to)
276 continue;
277 gfs2_trans_add_bh(ip->i_gl, bh, 0);
278 }
279 }
280