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