]>
Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
7ae8fa84 | 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
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 | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
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> | |
fd88de56 | 16 | #include <linux/pagevec.h> |
9b124fbb | 17 | #include <linux/mpage.h> |
d1665e41 | 18 | #include <linux/fs.h> |
a8d638e3 | 19 | #include <linux/writeback.h> |
7765ec26 | 20 | #include <linux/swap.h> |
5c676f6d | 21 | #include <linux/gfs2_ondisk.h> |
7d308590 | 22 | #include <linux/lm_interface.h> |
51ff87bd | 23 | #include <linux/swap.h> |
b3b94faa DT |
24 | |
25 | #include "gfs2.h" | |
5c676f6d | 26 | #include "incore.h" |
b3b94faa DT |
27 | #include "bmap.h" |
28 | #include "glock.h" | |
29 | #include "inode.h" | |
b3b94faa DT |
30 | #include "log.h" |
31 | #include "meta_io.h" | |
32 | #include "ops_address.h" | |
b3b94faa DT |
33 | #include "quota.h" |
34 | #include "trans.h" | |
18ec7d5c | 35 | #include "rgrp.h" |
cd81a4ba | 36 | #include "super.h" |
5c676f6d | 37 | #include "util.h" |
4340fe62 | 38 | #include "glops.h" |
b3b94faa | 39 | |
ba7f7290 SW |
40 | |
41 | static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page, | |
42 | unsigned int from, unsigned int to) | |
43 | { | |
44 | struct buffer_head *head = page_buffers(page); | |
45 | unsigned int bsize = head->b_size; | |
46 | struct buffer_head *bh; | |
47 | unsigned int start, end; | |
48 | ||
49 | for (bh = head, start = 0; bh != head || !start; | |
50 | bh = bh->b_this_page, start = end) { | |
51 | end = start + bsize; | |
52 | if (end <= from || start >= to) | |
53 | continue; | |
ddf4b426 BM |
54 | if (gfs2_is_jdata(ip)) |
55 | set_buffer_uptodate(bh); | |
ba7f7290 SW |
56 | gfs2_trans_add_bh(ip->i_gl, bh, 0); |
57 | } | |
58 | } | |
59 | ||
b3b94faa | 60 | /** |
4ff14670 | 61 | * gfs2_get_block - Fills in a buffer head with details about a block |
b3b94faa DT |
62 | * @inode: The inode |
63 | * @lblock: The block number to look up | |
64 | * @bh_result: The buffer head to return the result in | |
65 | * @create: Non-zero if we may add block to the file | |
66 | * | |
67 | * Returns: errno | |
68 | */ | |
69 | ||
4ff14670 SW |
70 | int gfs2_get_block(struct inode *inode, sector_t lblock, |
71 | struct buffer_head *bh_result, int create) | |
b3b94faa | 72 | { |
23591256 | 73 | return gfs2_block_map(inode, lblock, create, bh_result); |
b3b94faa DT |
74 | } |
75 | ||
76 | /** | |
7a6bbacb | 77 | * gfs2_get_block_noalloc - Fills in a buffer head with details about a block |
b3b94faa DT |
78 | * @inode: The inode |
79 | * @lblock: The block number to look up | |
80 | * @bh_result: The buffer head to return the result in | |
81 | * @create: Non-zero if we may add block to the file | |
82 | * | |
83 | * Returns: errno | |
84 | */ | |
85 | ||
7a6bbacb SW |
86 | static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock, |
87 | struct buffer_head *bh_result, int create) | |
b3b94faa | 88 | { |
b3b94faa DT |
89 | int error; |
90 | ||
23591256 | 91 | error = gfs2_block_map(inode, lblock, 0, bh_result); |
b3b94faa DT |
92 | if (error) |
93 | return error; | |
de986e85 | 94 | if (!buffer_mapped(bh_result)) |
7a6bbacb SW |
95 | return -EIO; |
96 | return 0; | |
b3b94faa DT |
97 | } |
98 | ||
7a6bbacb SW |
99 | static int gfs2_get_block_direct(struct inode *inode, sector_t lblock, |
100 | struct buffer_head *bh_result, int create) | |
623d9355 | 101 | { |
23591256 | 102 | return gfs2_block_map(inode, lblock, 0, bh_result); |
623d9355 | 103 | } |
7a6bbacb | 104 | |
b3b94faa DT |
105 | /** |
106 | * gfs2_writepage - Write complete page | |
107 | * @page: Page to write | |
108 | * | |
109 | * Returns: errno | |
110 | * | |
18ec7d5c SW |
111 | * Some of this is copied from block_write_full_page() although we still |
112 | * call it to do most of the work. | |
b3b94faa DT |
113 | */ |
114 | ||
115 | static int gfs2_writepage(struct page *page, struct writeback_control *wbc) | |
116 | { | |
18ec7d5c | 117 | struct inode *inode = page->mapping->host; |
f4387149 SW |
118 | struct gfs2_inode *ip = GFS2_I(inode); |
119 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
18ec7d5c SW |
120 | loff_t i_size = i_size_read(inode); |
121 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; | |
122 | unsigned offset; | |
b3b94faa | 123 | int error; |
18ec7d5c | 124 | int done_trans = 0; |
b3b94faa | 125 | |
b3b94faa DT |
126 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) { |
127 | unlock_page(page); | |
128 | return -EIO; | |
129 | } | |
5c676f6d | 130 | if (current->journal_info) |
18ec7d5c SW |
131 | goto out_ignore; |
132 | ||
133 | /* Is the page fully outside i_size? (truncate in progress) */ | |
134 | offset = i_size & (PAGE_CACHE_SIZE-1); | |
d2d7b8a2 | 135 | if (page->index > end_index || (page->index == end_index && !offset)) { |
18ec7d5c | 136 | page->mapping->a_ops->invalidatepage(page, 0); |
b3b94faa | 137 | unlock_page(page); |
18ec7d5c | 138 | return 0; /* don't care */ |
b3b94faa DT |
139 | } |
140 | ||
8fb68595 RP |
141 | if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) && |
142 | PageChecked(page)) { | |
143 | ClearPageChecked(page); | |
18ec7d5c SW |
144 | error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0); |
145 | if (error) | |
146 | goto out_ignore; | |
f4387149 SW |
147 | if (!page_has_buffers(page)) { |
148 | create_empty_buffers(page, inode->i_sb->s_blocksize, | |
149 | (1 << BH_Dirty)|(1 << BH_Uptodate)); | |
150 | } | |
18ec7d5c SW |
151 | gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1); |
152 | done_trans = 1; | |
153 | } | |
7a6bbacb | 154 | error = block_write_full_page(page, gfs2_get_block_noalloc, wbc); |
18ec7d5c SW |
155 | if (done_trans) |
156 | gfs2_trans_end(sdp); | |
b3b94faa | 157 | return error; |
18ec7d5c SW |
158 | |
159 | out_ignore: | |
160 | redirty_page_for_writepage(wbc, page); | |
161 | unlock_page(page); | |
162 | return 0; | |
b3b94faa DT |
163 | } |
164 | ||
a8d638e3 SW |
165 | /** |
166 | * gfs2_writepages - Write a bunch of dirty pages back to disk | |
167 | * @mapping: The mapping to write | |
168 | * @wbc: Write-back control | |
169 | * | |
170 | * For journaled files and/or ordered writes this just falls back to the | |
171 | * kernel's default writepages path for now. We will probably want to change | |
172 | * that eventually (i.e. when we look at allocate on flush). | |
173 | * | |
174 | * For the data=writeback case though we can already ignore buffer heads | |
175 | * and write whole extents at once. This is a big reduction in the | |
176 | * number of I/O requests we send and the bmap calls we make in this case. | |
177 | */ | |
a2cf8222 AB |
178 | static int gfs2_writepages(struct address_space *mapping, |
179 | struct writeback_control *wbc) | |
a8d638e3 SW |
180 | { |
181 | struct inode *inode = mapping->host; | |
182 | struct gfs2_inode *ip = GFS2_I(inode); | |
183 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
184 | ||
185 | if (sdp->sd_args.ar_data == GFS2_DATA_WRITEBACK && !gfs2_is_jdata(ip)) | |
186 | return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc); | |
187 | ||
188 | return generic_writepages(mapping, wbc); | |
189 | } | |
190 | ||
b3b94faa DT |
191 | /** |
192 | * stuffed_readpage - Fill in a Linux page with stuffed file data | |
193 | * @ip: the inode | |
194 | * @page: the page | |
195 | * | |
196 | * Returns: errno | |
197 | */ | |
198 | ||
199 | static int stuffed_readpage(struct gfs2_inode *ip, struct page *page) | |
200 | { | |
201 | struct buffer_head *dibh; | |
202 | void *kaddr; | |
203 | int error; | |
204 | ||
bf126aee SW |
205 | /* |
206 | * Due to the order of unstuffing files and ->nopage(), we can be | |
207 | * asked for a zero page in the case of a stuffed file being extended, | |
208 | * so we need to supply one here. It doesn't happen often. | |
209 | */ | |
210 | if (unlikely(page->index)) { | |
2840501a | 211 | zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0); |
bf126aee SW |
212 | return 0; |
213 | } | |
fd88de56 | 214 | |
b3b94faa DT |
215 | error = gfs2_meta_inode_buffer(ip, &dibh); |
216 | if (error) | |
217 | return error; | |
218 | ||
5c4e9e03 | 219 | kaddr = kmap_atomic(page, KM_USER0); |
fd88de56 | 220 | memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), |
b3b94faa | 221 | ip->i_di.di_size); |
fd88de56 | 222 | memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size); |
c312c4fd | 223 | kunmap_atomic(kaddr, KM_USER0); |
bf126aee | 224 | flush_dcache_page(page); |
b3b94faa | 225 | brelse(dibh); |
b3b94faa DT |
226 | SetPageUptodate(page); |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
b3b94faa | 231 | |
b3b94faa | 232 | /** |
51ff87bd SW |
233 | * __gfs2_readpage - readpage |
234 | * @file: The file to read a page for | |
b3b94faa DT |
235 | * @page: The page to read |
236 | * | |
51ff87bd SW |
237 | * This is the core of gfs2's readpage. Its used by the internal file |
238 | * reading code as in that case we already hold the glock. Also its | |
239 | * called by gfs2_readpage() once the required lock has been granted. | |
240 | * | |
b3b94faa DT |
241 | */ |
242 | ||
51ff87bd | 243 | static int __gfs2_readpage(void *file, struct page *page) |
b3b94faa | 244 | { |
feaa7bba SW |
245 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
246 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); | |
b3b94faa DT |
247 | int error; |
248 | ||
18ec7d5c | 249 | if (gfs2_is_stuffed(ip)) { |
fd88de56 SW |
250 | error = stuffed_readpage(ip, page); |
251 | unlock_page(page); | |
51ff87bd | 252 | } else { |
18ec7d5c | 253 | error = mpage_readpage(page, gfs2_get_block); |
51ff87bd | 254 | } |
b3b94faa DT |
255 | |
256 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) | |
51ff87bd | 257 | return -EIO; |
b3b94faa | 258 | |
51ff87bd SW |
259 | return error; |
260 | } | |
261 | ||
262 | /** | |
263 | * gfs2_readpage - read a page of a file | |
264 | * @file: The file to read | |
265 | * @page: The page of the file | |
266 | * | |
3cc3f710 | 267 | * This deals with the locking required. We use a trylock in order to |
51ff87bd SW |
268 | * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE |
269 | * in the event that we are unable to get the lock. | |
270 | */ | |
271 | ||
272 | static int gfs2_readpage(struct file *file, struct page *page) | |
273 | { | |
274 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); | |
275 | struct gfs2_holder gh; | |
276 | int error; | |
277 | ||
51ff87bd SW |
278 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh); |
279 | error = gfs2_glock_nq_atime(&gh); | |
280 | if (unlikely(error)) { | |
281 | unlock_page(page); | |
282 | goto out; | |
61a30dcb | 283 | } |
51ff87bd SW |
284 | error = __gfs2_readpage(file, page); |
285 | gfs2_glock_dq(&gh); | |
18ec7d5c | 286 | out: |
51ff87bd | 287 | gfs2_holder_uninit(&gh); |
a13cbe37 | 288 | if (error == GLR_TRYFAILED) { |
a13cbe37 | 289 | yield(); |
51ff87bd | 290 | return AOP_TRUNCATED_PAGE; |
a13cbe37 | 291 | } |
51ff87bd SW |
292 | return error; |
293 | } | |
294 | ||
295 | /** | |
296 | * gfs2_internal_read - read an internal file | |
297 | * @ip: The gfs2 inode | |
298 | * @ra_state: The readahead state (or NULL for no readahead) | |
299 | * @buf: The buffer to fill | |
300 | * @pos: The file position | |
301 | * @size: The amount to read | |
302 | * | |
303 | */ | |
304 | ||
305 | int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state, | |
306 | char *buf, loff_t *pos, unsigned size) | |
307 | { | |
308 | struct address_space *mapping = ip->i_inode.i_mapping; | |
309 | unsigned long index = *pos / PAGE_CACHE_SIZE; | |
310 | unsigned offset = *pos & (PAGE_CACHE_SIZE - 1); | |
311 | unsigned copied = 0; | |
312 | unsigned amt; | |
313 | struct page *page; | |
314 | void *p; | |
315 | ||
316 | do { | |
317 | amt = size - copied; | |
318 | if (offset + size > PAGE_CACHE_SIZE) | |
319 | amt = PAGE_CACHE_SIZE - offset; | |
320 | page = read_cache_page(mapping, index, __gfs2_readpage, NULL); | |
321 | if (IS_ERR(page)) | |
322 | return PTR_ERR(page); | |
323 | p = kmap_atomic(page, KM_USER0); | |
324 | memcpy(buf + copied, p + offset, amt); | |
325 | kunmap_atomic(p, KM_USER0); | |
326 | mark_page_accessed(page); | |
327 | page_cache_release(page); | |
328 | copied += amt; | |
329 | index++; | |
330 | offset = 0; | |
331 | } while(copied < size); | |
332 | (*pos) += size; | |
333 | return size; | |
fd88de56 SW |
334 | } |
335 | ||
fd88de56 SW |
336 | /** |
337 | * gfs2_readpages - Read a bunch of pages at once | |
338 | * | |
339 | * Some notes: | |
340 | * 1. This is only for readahead, so we can simply ignore any things | |
341 | * which are slightly inconvenient (such as locking conflicts between | |
342 | * the page lock and the glock) and return having done no I/O. Its | |
343 | * obviously not something we'd want to do on too regular a basis. | |
344 | * Any I/O we ignore at this time will be done via readpage later. | |
e1d5b18a | 345 | * 2. We don't handle stuffed files here we let readpage do the honours. |
fd88de56 SW |
346 | * 3. mpage_readpages() does most of the heavy lifting in the common case. |
347 | * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places. | |
fd88de56 | 348 | */ |
3cc3f710 | 349 | |
fd88de56 SW |
350 | static int gfs2_readpages(struct file *file, struct address_space *mapping, |
351 | struct list_head *pages, unsigned nr_pages) | |
352 | { | |
353 | struct inode *inode = mapping->host; | |
feaa7bba SW |
354 | struct gfs2_inode *ip = GFS2_I(inode); |
355 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
fd88de56 | 356 | struct gfs2_holder gh; |
3cc3f710 | 357 | int ret; |
fd88de56 | 358 | |
3cc3f710 | 359 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
51ff87bd | 360 | ret = gfs2_glock_nq_atime(&gh); |
51ff87bd | 361 | if (unlikely(ret)) |
3cc3f710 | 362 | goto out_uninit; |
e1d5b18a | 363 | if (!gfs2_is_stuffed(ip)) |
fd88de56 | 364 | ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block); |
3cc3f710 SW |
365 | gfs2_glock_dq(&gh); |
366 | out_uninit: | |
367 | gfs2_holder_uninit(&gh); | |
fd88de56 SW |
368 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
369 | ret = -EIO; | |
370 | return ret; | |
b3b94faa DT |
371 | } |
372 | ||
373 | /** | |
7765ec26 | 374 | * gfs2_write_begin - Begin to write to a file |
b3b94faa | 375 | * @file: The file to write to |
7765ec26 SW |
376 | * @mapping: The mapping in which to write |
377 | * @pos: The file offset at which to start writing | |
378 | * @len: Length of the write | |
379 | * @flags: Various flags | |
380 | * @pagep: Pointer to return the page | |
381 | * @fsdata: Pointer to return fs data (unused by GFS2) | |
b3b94faa DT |
382 | * |
383 | * Returns: errno | |
384 | */ | |
385 | ||
7765ec26 SW |
386 | static int gfs2_write_begin(struct file *file, struct address_space *mapping, |
387 | loff_t pos, unsigned len, unsigned flags, | |
388 | struct page **pagep, void **fsdata) | |
b3b94faa | 389 | { |
7765ec26 SW |
390 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
391 | struct gfs2_sbd *sdp = GFS2_SB(mapping->host); | |
18ec7d5c SW |
392 | unsigned int data_blocks, ind_blocks, rblocks; |
393 | int alloc_required; | |
b3b94faa | 394 | int error = 0; |
18ec7d5c | 395 | struct gfs2_alloc *al; |
7765ec26 SW |
396 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
397 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); | |
398 | unsigned to = from + len; | |
399 | struct page *page; | |
52ae7b79 | 400 | |
7765ec26 | 401 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh); |
dcd24799 | 402 | error = gfs2_glock_nq_atime(&ip->i_gh); |
7765ec26 | 403 | if (unlikely(error)) |
18ec7d5c | 404 | goto out_uninit; |
b3b94faa | 405 | |
7765ec26 SW |
406 | error = -ENOMEM; |
407 | page = __grab_cache_page(mapping, index); | |
408 | *pagep = page; | |
409 | if (!page) | |
410 | goto out_unlock; | |
411 | ||
412 | gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks); | |
18ec7d5c | 413 | |
7765ec26 | 414 | error = gfs2_write_alloc_required(ip, pos, len, &alloc_required); |
18ec7d5c | 415 | if (error) |
7765ec26 | 416 | goto out_putpage; |
b3b94faa | 417 | |
18ec7d5c | 418 | |
f5c54804 | 419 | ip->i_alloc.al_requested = 0; |
18ec7d5c SW |
420 | if (alloc_required) { |
421 | al = gfs2_alloc_get(ip); | |
422 | ||
423 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
424 | if (error) | |
425 | goto out_alloc_put; | |
426 | ||
2933f925 | 427 | error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid); |
18ec7d5c SW |
428 | if (error) |
429 | goto out_qunlock; | |
430 | ||
431 | al->al_requested = data_blocks + ind_blocks; | |
432 | error = gfs2_inplace_reserve(ip); | |
433 | if (error) | |
434 | goto out_qunlock; | |
435 | } | |
436 | ||
437 | rblocks = RES_DINODE + ind_blocks; | |
438 | if (gfs2_is_jdata(ip)) | |
439 | rblocks += data_blocks ? data_blocks : 1; | |
440 | if (ind_blocks || data_blocks) | |
441 | rblocks += RES_STATFS + RES_QUOTA; | |
442 | ||
16615be1 SW |
443 | error = gfs2_trans_begin(sdp, rblocks, |
444 | PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize); | |
18ec7d5c | 445 | if (error) |
a867bb28 | 446 | goto out_trans_fail; |
18ec7d5c SW |
447 | |
448 | if (gfs2_is_stuffed(ip)) { | |
7765ec26 | 449 | if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { |
f25ef0c1 | 450 | error = gfs2_unstuff_dinode(ip, page); |
5c4e9e03 SW |
451 | if (error == 0) |
452 | goto prepare_write; | |
453 | } else if (!PageUptodate(page)) | |
b3b94faa | 454 | error = stuffed_readpage(ip, page); |
5c4e9e03 | 455 | goto out; |
18ec7d5c SW |
456 | } |
457 | ||
5c4e9e03 | 458 | prepare_write: |
18ec7d5c SW |
459 | error = block_prepare_write(page, from, to, gfs2_get_block); |
460 | ||
461 | out: | |
462 | if (error) { | |
463 | gfs2_trans_end(sdp); | |
a867bb28 | 464 | out_trans_fail: |
18ec7d5c SW |
465 | if (alloc_required) { |
466 | gfs2_inplace_release(ip); | |
467 | out_qunlock: | |
468 | gfs2_quota_unlock(ip); | |
469 | out_alloc_put: | |
470 | gfs2_alloc_put(ip); | |
471 | } | |
7765ec26 SW |
472 | out_putpage: |
473 | page_cache_release(page); | |
474 | if (pos + len > ip->i_inode.i_size) | |
475 | vmtruncate(&ip->i_inode, ip->i_inode.i_size); | |
18ec7d5c SW |
476 | out_unlock: |
477 | gfs2_glock_dq_m(1, &ip->i_gh); | |
478 | out_uninit: | |
479 | gfs2_holder_uninit(&ip->i_gh); | |
480 | } | |
b3b94faa DT |
481 | |
482 | return error; | |
483 | } | |
484 | ||
7ae8fa84 RP |
485 | /** |
486 | * adjust_fs_space - Adjusts the free space available due to gfs2_grow | |
487 | * @inode: the rindex inode | |
488 | */ | |
489 | static void adjust_fs_space(struct inode *inode) | |
490 | { | |
491 | struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; | |
492 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; | |
493 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; | |
494 | u64 fs_total, new_free; | |
495 | ||
496 | /* Total up the file system space, according to the latest rindex. */ | |
497 | fs_total = gfs2_ri_total(sdp); | |
498 | ||
499 | spin_lock(&sdp->sd_statfs_spin); | |
500 | if (fs_total > (m_sc->sc_total + l_sc->sc_total)) | |
501 | new_free = fs_total - (m_sc->sc_total + l_sc->sc_total); | |
502 | else | |
503 | new_free = 0; | |
504 | spin_unlock(&sdp->sd_statfs_spin); | |
6c53267f RP |
505 | fs_warn(sdp, "File system extended by %llu blocks.\n", |
506 | (unsigned long long)new_free); | |
7ae8fa84 RP |
507 | gfs2_statfs_change(sdp, new_free, new_free, 0); |
508 | } | |
509 | ||
b3b94faa | 510 | /** |
7765ec26 SW |
511 | * gfs2_stuffed_write_end - Write end for stuffed files |
512 | * @inode: The inode | |
513 | * @dibh: The buffer_head containing the on-disk inode | |
514 | * @pos: The file position | |
515 | * @len: The length of the write | |
516 | * @copied: How much was actually copied by the VFS | |
517 | * @page: The page | |
518 | * | |
519 | * This copies the data from the page into the inode block after | |
520 | * the inode data structure itself. | |
521 | * | |
522 | * Returns: errno | |
523 | */ | |
524 | static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh, | |
525 | loff_t pos, unsigned len, unsigned copied, | |
526 | struct page *page) | |
527 | { | |
528 | struct gfs2_inode *ip = GFS2_I(inode); | |
529 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
530 | u64 to = pos + copied; | |
531 | void *kaddr; | |
532 | unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode); | |
533 | struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; | |
534 | ||
535 | BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode))); | |
536 | kaddr = kmap_atomic(page, KM_USER0); | |
537 | memcpy(buf + pos, kaddr + pos, copied); | |
538 | memset(kaddr + pos + copied, 0, len - copied); | |
539 | flush_dcache_page(page); | |
540 | kunmap_atomic(kaddr, KM_USER0); | |
541 | ||
542 | if (!PageUptodate(page)) | |
543 | SetPageUptodate(page); | |
544 | unlock_page(page); | |
545 | page_cache_release(page); | |
546 | ||
547 | if (inode->i_size < to) { | |
548 | i_size_write(inode, to); | |
549 | ip->i_di.di_size = inode->i_size; | |
550 | di->di_size = cpu_to_be64(inode->i_size); | |
551 | mark_inode_dirty(inode); | |
552 | } | |
553 | ||
554 | if (inode == sdp->sd_rindex) | |
555 | adjust_fs_space(inode); | |
556 | ||
557 | brelse(dibh); | |
558 | gfs2_trans_end(sdp); | |
559 | gfs2_glock_dq(&ip->i_gh); | |
560 | gfs2_holder_uninit(&ip->i_gh); | |
561 | return copied; | |
562 | } | |
563 | ||
564 | /** | |
565 | * gfs2_write_end | |
b3b94faa | 566 | * @file: The file to write to |
7765ec26 SW |
567 | * @mapping: The address space to write to |
568 | * @pos: The file position | |
569 | * @len: The length of the data | |
570 | * @copied: | |
571 | * @page: The page that has been written | |
572 | * @fsdata: The fsdata (unused in GFS2) | |
573 | * | |
574 | * The main write_end function for GFS2. We have a separate one for | |
575 | * stuffed files as they are slightly different, otherwise we just | |
576 | * put our locking around the VFS provided functions. | |
b3b94faa DT |
577 | * |
578 | * Returns: errno | |
579 | */ | |
580 | ||
7765ec26 SW |
581 | static int gfs2_write_end(struct file *file, struct address_space *mapping, |
582 | loff_t pos, unsigned len, unsigned copied, | |
583 | struct page *page, void *fsdata) | |
b3b94faa DT |
584 | { |
585 | struct inode *inode = page->mapping->host; | |
feaa7bba SW |
586 | struct gfs2_inode *ip = GFS2_I(inode); |
587 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
18ec7d5c | 588 | struct buffer_head *dibh; |
48516ced SW |
589 | struct gfs2_alloc *al = &ip->i_alloc; |
590 | struct gfs2_dinode *di; | |
7765ec26 SW |
591 | unsigned int from = pos & (PAGE_CACHE_SIZE - 1); |
592 | unsigned int to = from + len; | |
593 | int ret; | |
b3b94faa | 594 | |
7765ec26 | 595 | BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0); |
18ec7d5c | 596 | |
7765ec26 SW |
597 | ret = gfs2_meta_inode_buffer(ip, &dibh); |
598 | if (unlikely(ret)) { | |
599 | unlock_page(page); | |
600 | page_cache_release(page); | |
601 | goto failed; | |
602 | } | |
18ec7d5c SW |
603 | |
604 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
b3b94faa | 605 | |
7765ec26 SW |
606 | if (gfs2_is_stuffed(ip)) |
607 | return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page); | |
b3b94faa | 608 | |
7765ec26 SW |
609 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) |
610 | gfs2_page_add_databufs(ip, page, from, to); | |
b3b94faa | 611 | |
7765ec26 | 612 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); |
b3b94faa | 613 | |
7765ec26 SW |
614 | if (likely(ret >= 0)) { |
615 | copied = ret; | |
616 | if ((pos + copied) > inode->i_size) { | |
617 | di = (struct gfs2_dinode *)dibh->b_data; | |
618 | ip->i_di.di_size = inode->i_size; | |
619 | di->di_size = cpu_to_be64(inode->i_size); | |
ae619320 SW |
620 | mark_inode_dirty(inode); |
621 | } | |
48516ced SW |
622 | } |
623 | ||
7ae8fa84 RP |
624 | if (inode == sdp->sd_rindex) |
625 | adjust_fs_space(inode); | |
626 | ||
18ec7d5c SW |
627 | brelse(dibh); |
628 | gfs2_trans_end(sdp); | |
7765ec26 | 629 | failed: |
18ec7d5c SW |
630 | if (al->al_requested) { |
631 | gfs2_inplace_release(ip); | |
632 | gfs2_quota_unlock(ip); | |
633 | gfs2_alloc_put(ip); | |
634 | } | |
7765ec26 | 635 | gfs2_glock_dq(&ip->i_gh); |
18ec7d5c | 636 | gfs2_holder_uninit(&ip->i_gh); |
7765ec26 | 637 | return ret; |
b3b94faa DT |
638 | } |
639 | ||
8fb68595 RP |
640 | /** |
641 | * gfs2_set_page_dirty - Page dirtying function | |
642 | * @page: The page to dirty | |
643 | * | |
644 | * Returns: 1 if it dirtyed the page, or 0 otherwise | |
645 | */ | |
646 | ||
647 | static int gfs2_set_page_dirty(struct page *page) | |
648 | { | |
649 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); | |
650 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); | |
651 | ||
652 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) | |
653 | SetPageChecked(page); | |
654 | return __set_page_dirty_buffers(page); | |
655 | } | |
656 | ||
b3b94faa DT |
657 | /** |
658 | * gfs2_bmap - Block map function | |
659 | * @mapping: Address space info | |
660 | * @lblock: The block to map | |
661 | * | |
662 | * Returns: The disk address for the block or 0 on hole or error | |
663 | */ | |
664 | ||
665 | static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock) | |
666 | { | |
feaa7bba | 667 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
b3b94faa DT |
668 | struct gfs2_holder i_gh; |
669 | sector_t dblock = 0; | |
670 | int error; | |
671 | ||
b3b94faa DT |
672 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
673 | if (error) | |
674 | return 0; | |
675 | ||
676 | if (!gfs2_is_stuffed(ip)) | |
4ff14670 | 677 | dblock = generic_block_bmap(mapping, lblock, gfs2_get_block); |
b3b94faa DT |
678 | |
679 | gfs2_glock_dq_uninit(&i_gh); | |
680 | ||
681 | return dblock; | |
682 | } | |
683 | ||
d7b616e2 SW |
684 | static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh) |
685 | { | |
686 | struct gfs2_bufdata *bd; | |
687 | ||
688 | lock_buffer(bh); | |
689 | gfs2_log_lock(sdp); | |
690 | clear_buffer_dirty(bh); | |
691 | bd = bh->b_private; | |
692 | if (bd) { | |
16615be1 SW |
693 | if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh)) |
694 | list_del_init(&bd->bd_le.le_list); | |
695 | else | |
696 | gfs2_remove_from_journal(bh, current->journal_info, 0); | |
d7b616e2 SW |
697 | } |
698 | bh->b_bdev = NULL; | |
699 | clear_buffer_mapped(bh); | |
700 | clear_buffer_req(bh); | |
701 | clear_buffer_new(bh); | |
702 | gfs2_log_unlock(sdp); | |
703 | unlock_buffer(bh); | |
704 | } | |
705 | ||
8628de05 | 706 | static void gfs2_invalidatepage(struct page *page, unsigned long offset) |
b3b94faa | 707 | { |
d7b616e2 SW |
708 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
709 | struct buffer_head *bh, *head; | |
710 | unsigned long pos = 0; | |
711 | ||
b3b94faa | 712 | BUG_ON(!PageLocked(page)); |
8fb68595 RP |
713 | if (offset == 0) |
714 | ClearPageChecked(page); | |
d7b616e2 SW |
715 | if (!page_has_buffers(page)) |
716 | goto out; | |
b3b94faa | 717 | |
d7b616e2 SW |
718 | bh = head = page_buffers(page); |
719 | do { | |
720 | if (offset <= pos) | |
721 | gfs2_discard(sdp, bh); | |
722 | pos += bh->b_size; | |
723 | bh = bh->b_this_page; | |
724 | } while (bh != head); | |
725 | out: | |
726 | if (offset == 0) | |
727 | try_to_release_page(page, 0); | |
b3b94faa DT |
728 | } |
729 | ||
c7b33834 SW |
730 | /** |
731 | * gfs2_ok_for_dio - check that dio is valid on this file | |
732 | * @ip: The inode | |
733 | * @rw: READ or WRITE | |
734 | * @offset: The offset at which we are reading or writing | |
735 | * | |
736 | * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o) | |
737 | * 1 (to accept the i/o request) | |
738 | */ | |
739 | static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset) | |
740 | { | |
741 | /* | |
742 | * Should we return an error here? I can't see that O_DIRECT for | |
743 | * a journaled file makes any sense. For now we'll silently fall | |
744 | * back to buffered I/O, likewise we do the same for stuffed | |
745 | * files since they are (a) small and (b) unaligned. | |
746 | */ | |
747 | if (gfs2_is_jdata(ip)) | |
748 | return 0; | |
749 | ||
750 | if (gfs2_is_stuffed(ip)) | |
751 | return 0; | |
752 | ||
753 | if (offset > i_size_read(&ip->i_inode)) | |
754 | return 0; | |
755 | return 1; | |
756 | } | |
757 | ||
758 | ||
759 | ||
a9e5f4d0 SW |
760 | static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb, |
761 | const struct iovec *iov, loff_t offset, | |
762 | unsigned long nr_segs) | |
d1665e41 SW |
763 | { |
764 | struct file *file = iocb->ki_filp; | |
765 | struct inode *inode = file->f_mapping->host; | |
feaa7bba | 766 | struct gfs2_inode *ip = GFS2_I(inode); |
d1665e41 SW |
767 | struct gfs2_holder gh; |
768 | int rv; | |
769 | ||
770 | /* | |
c7b33834 SW |
771 | * Deferred lock, even if its a write, since we do no allocation |
772 | * on this path. All we need change is atime, and this lock mode | |
773 | * ensures that other nodes have flushed their buffered read caches | |
774 | * (i.e. their page cache entries for this inode). We do not, | |
775 | * unfortunately have the option of only flushing a range like | |
776 | * the VFS does. | |
d1665e41 | 777 | */ |
c7b33834 | 778 | gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh); |
dcd24799 | 779 | rv = gfs2_glock_nq_atime(&gh); |
d1665e41 | 780 | if (rv) |
c7b33834 SW |
781 | return rv; |
782 | rv = gfs2_ok_for_dio(ip, rw, offset); | |
783 | if (rv != 1) | |
784 | goto out; /* dio not valid, fall back to buffered i/o */ | |
785 | ||
786 | rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev, | |
787 | iov, offset, nr_segs, | |
788 | gfs2_get_block_direct, NULL); | |
d1665e41 SW |
789 | out: |
790 | gfs2_glock_dq_m(1, &gh); | |
791 | gfs2_holder_uninit(&gh); | |
d1665e41 SW |
792 | return rv; |
793 | } | |
794 | ||
4340fe62 | 795 | /** |
623d9355 | 796 | * gfs2_releasepage - free the metadata associated with a page |
4340fe62 SW |
797 | * @page: the page that's being released |
798 | * @gfp_mask: passed from Linux VFS, ignored by us | |
799 | * | |
800 | * Call try_to_free_buffers() if the buffers in this page can be | |
801 | * released. | |
802 | * | |
803 | * Returns: 0 | |
804 | */ | |
805 | ||
806 | int gfs2_releasepage(struct page *page, gfp_t gfp_mask) | |
807 | { | |
808 | struct inode *aspace = page->mapping->host; | |
809 | struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info; | |
810 | struct buffer_head *bh, *head; | |
811 | struct gfs2_bufdata *bd; | |
4340fe62 SW |
812 | |
813 | if (!page_has_buffers(page)) | |
891ba6d4 | 814 | return 0; |
4340fe62 | 815 | |
bb3b0e3d | 816 | gfs2_log_lock(sdp); |
4340fe62 SW |
817 | head = bh = page_buffers(page); |
818 | do { | |
bb3b0e3d SW |
819 | if (atomic_read(&bh->b_count)) |
820 | goto cannot_release; | |
821 | bd = bh->b_private; | |
822 | if (bd && bd->bd_ail) | |
823 | goto cannot_release; | |
4340fe62 | 824 | gfs2_assert_warn(sdp, !buffer_pinned(bh)); |
623d9355 | 825 | gfs2_assert_warn(sdp, !buffer_dirty(bh)); |
bb3b0e3d SW |
826 | bh = bh->b_this_page; |
827 | } while(bh != head); | |
828 | gfs2_log_unlock(sdp); | |
4340fe62 | 829 | |
bb3b0e3d SW |
830 | head = bh = page_buffers(page); |
831 | do { | |
623d9355 | 832 | gfs2_log_lock(sdp); |
4340fe62 SW |
833 | bd = bh->b_private; |
834 | if (bd) { | |
835 | gfs2_assert_warn(sdp, bd->bd_bh == bh); | |
836 | gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr)); | |
d7b616e2 SW |
837 | if (!list_empty(&bd->bd_le.le_list)) { |
838 | if (!buffer_pinned(bh)) | |
839 | list_del_init(&bd->bd_le.le_list); | |
840 | else | |
841 | bd = NULL; | |
842 | } | |
843 | if (bd) | |
844 | bd->bd_bh = NULL; | |
4340fe62 SW |
845 | bh->b_private = NULL; |
846 | } | |
623d9355 SW |
847 | gfs2_log_unlock(sdp); |
848 | if (bd) | |
849 | kmem_cache_free(gfs2_bufdata_cachep, bd); | |
4340fe62 SW |
850 | |
851 | bh = bh->b_this_page; | |
166afccd | 852 | } while (bh != head); |
4340fe62 | 853 | |
4340fe62 | 854 | return try_to_free_buffers(page); |
bb3b0e3d SW |
855 | cannot_release: |
856 | gfs2_log_unlock(sdp); | |
857 | return 0; | |
4340fe62 SW |
858 | } |
859 | ||
66de045d | 860 | const struct address_space_operations gfs2_file_aops = { |
b3b94faa | 861 | .writepage = gfs2_writepage, |
a8d638e3 | 862 | .writepages = gfs2_writepages, |
b3b94faa | 863 | .readpage = gfs2_readpage, |
fd88de56 | 864 | .readpages = gfs2_readpages, |
b3b94faa | 865 | .sync_page = block_sync_page, |
7765ec26 SW |
866 | .write_begin = gfs2_write_begin, |
867 | .write_end = gfs2_write_end, | |
8fb68595 | 868 | .set_page_dirty = gfs2_set_page_dirty, |
b3b94faa DT |
869 | .bmap = gfs2_bmap, |
870 | .invalidatepage = gfs2_invalidatepage, | |
4340fe62 | 871 | .releasepage = gfs2_releasepage, |
b3b94faa DT |
872 | .direct_IO = gfs2_direct_IO, |
873 | }; | |
874 |