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