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