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