2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/pagemap.h>
16 #include <linux/pagevec.h>
17 #include <linux/mpage.h>
19 #include <linux/writeback.h>
20 #include <linux/swap.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <linux/lm_interface.h>
23 #include <linux/swap.h>
32 #include "ops_address.h"
41 static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
42 unsigned int from, unsigned int to)
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;
49 for (bh = head, start = 0; bh != head || !start;
50 bh = bh->b_this_page, start = end) {
52 if (end <= from || start >= to)
54 if (gfs2_is_jdata(ip))
55 set_buffer_uptodate(bh);
56 gfs2_trans_add_bh(ip->i_gl, bh, 0);
61 * gfs2_get_block - Fills in a buffer head with details about a block
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
70 int gfs2_get_block(struct inode *inode, sector_t lblock,
71 struct buffer_head *bh_result, int create)
73 return gfs2_block_map(inode, lblock, create, bh_result);
77 * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
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
86 static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
87 struct buffer_head *bh_result, int create)
91 error = gfs2_block_map(inode, lblock, 0, bh_result);
94 if (!buffer_mapped(bh_result))
99 static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
100 struct buffer_head *bh_result, int create)
102 return gfs2_block_map(inode, lblock, 0, bh_result);
106 * gfs2_writepage - Write complete page
107 * @page: Page to write
111 * Some of this is copied from block_write_full_page() although we still
112 * call it to do most of the work.
115 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
117 struct inode *inode = page->mapping->host;
118 struct gfs2_inode *ip = GFS2_I(inode);
119 struct gfs2_sbd *sdp = GFS2_SB(inode);
120 loff_t i_size = i_size_read(inode);
121 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
126 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
130 if (current->journal_info)
133 /* Is the page fully outside i_size? (truncate in progress) */
134 offset = i_size & (PAGE_CACHE_SIZE-1);
135 if (page->index > end_index || (page->index == end_index && !offset)) {
136 page->mapping->a_ops->invalidatepage(page, 0);
138 return 0; /* don't care */
141 if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) &&
143 ClearPageChecked(page);
144 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
147 if (!page_has_buffers(page)) {
148 create_empty_buffers(page, inode->i_sb->s_blocksize,
149 (1 << BH_Dirty)|(1 << BH_Uptodate));
151 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
154 error = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
157 gfs2_meta_cache_flush(ip);
161 redirty_page_for_writepage(wbc, page);
167 * gfs2_writepages - Write a bunch of dirty pages back to disk
168 * @mapping: The mapping to write
169 * @wbc: Write-back control
171 * For journaled files and/or ordered writes this just falls back to the
172 * kernel's default writepages path for now. We will probably want to change
173 * that eventually (i.e. when we look at allocate on flush).
175 * For the data=writeback case though we can already ignore buffer heads
176 * and write whole extents at once. This is a big reduction in the
177 * number of I/O requests we send and the bmap calls we make in this case.
179 static int gfs2_writepages(struct address_space *mapping,
180 struct writeback_control *wbc)
182 struct inode *inode = mapping->host;
183 struct gfs2_inode *ip = GFS2_I(inode);
184 struct gfs2_sbd *sdp = GFS2_SB(inode);
186 if (sdp->sd_args.ar_data == GFS2_DATA_WRITEBACK && !gfs2_is_jdata(ip))
187 return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
189 return generic_writepages(mapping, wbc);
193 * stuffed_readpage - Fill in a Linux page with stuffed file data
200 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
202 struct buffer_head *dibh;
207 * Due to the order of unstuffing files and ->nopage(), we can be
208 * asked for a zero page in the case of a stuffed file being extended,
209 * so we need to supply one here. It doesn't happen often.
211 if (unlikely(page->index)) {
212 zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
216 error = gfs2_meta_inode_buffer(ip, &dibh);
220 kaddr = kmap_atomic(page, KM_USER0);
221 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
223 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
224 kunmap_atomic(kaddr, KM_USER0);
225 flush_dcache_page(page);
227 SetPageUptodate(page);
234 * __gfs2_readpage - readpage
235 * @file: The file to read a page for
236 * @page: The page to read
238 * This is the core of gfs2's readpage. Its used by the internal file
239 * reading code as in that case we already hold the glock. Also its
240 * called by gfs2_readpage() once the required lock has been granted.
244 static int __gfs2_readpage(void *file, struct page *page)
246 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
247 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
250 if (gfs2_is_stuffed(ip)) {
251 error = stuffed_readpage(ip, page);
254 error = mpage_readpage(page, gfs2_get_block);
257 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
264 * gfs2_readpage - read a page of a file
265 * @file: The file to read
266 * @page: The page of the file
268 * This deals with the locking required. We use a trylock in order to
269 * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
270 * in the event that we are unable to get the lock.
273 static int gfs2_readpage(struct file *file, struct page *page)
275 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
276 struct gfs2_holder gh;
279 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
280 error = gfs2_glock_nq_atime(&gh);
281 if (unlikely(error)) {
285 error = __gfs2_readpage(file, page);
288 gfs2_holder_uninit(&gh);
289 if (error == GLR_TRYFAILED) {
291 return AOP_TRUNCATED_PAGE;
297 * gfs2_internal_read - read an internal file
298 * @ip: The gfs2 inode
299 * @ra_state: The readahead state (or NULL for no readahead)
300 * @buf: The buffer to fill
301 * @pos: The file position
302 * @size: The amount to read
306 int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
307 char *buf, loff_t *pos, unsigned size)
309 struct address_space *mapping = ip->i_inode.i_mapping;
310 unsigned long index = *pos / PAGE_CACHE_SIZE;
311 unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
319 if (offset + size > PAGE_CACHE_SIZE)
320 amt = PAGE_CACHE_SIZE - offset;
321 page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
323 return PTR_ERR(page);
324 p = kmap_atomic(page, KM_USER0);
325 memcpy(buf + copied, p + offset, amt);
326 kunmap_atomic(p, KM_USER0);
327 mark_page_accessed(page);
328 page_cache_release(page);
332 } while(copied < size);
338 * gfs2_readpages - Read a bunch of pages at once
341 * 1. This is only for readahead, so we can simply ignore any things
342 * which are slightly inconvenient (such as locking conflicts between
343 * the page lock and the glock) and return having done no I/O. Its
344 * obviously not something we'd want to do on too regular a basis.
345 * Any I/O we ignore at this time will be done via readpage later.
346 * 2. We don't handle stuffed files here we let readpage do the honours.
347 * 3. mpage_readpages() does most of the heavy lifting in the common case.
348 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
351 static int gfs2_readpages(struct file *file, struct address_space *mapping,
352 struct list_head *pages, unsigned nr_pages)
354 struct inode *inode = mapping->host;
355 struct gfs2_inode *ip = GFS2_I(inode);
356 struct gfs2_sbd *sdp = GFS2_SB(inode);
357 struct gfs2_holder gh;
360 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
361 ret = gfs2_glock_nq_atime(&gh);
364 if (!gfs2_is_stuffed(ip))
365 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
368 gfs2_holder_uninit(&gh);
369 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
375 * gfs2_write_begin - Begin to write to a file
376 * @file: The file to write to
377 * @mapping: The mapping in which to write
378 * @pos: The file offset at which to start writing
379 * @len: Length of the write
380 * @flags: Various flags
381 * @pagep: Pointer to return the page
382 * @fsdata: Pointer to return fs data (unused by GFS2)
387 static int gfs2_write_begin(struct file *file, struct address_space *mapping,
388 loff_t pos, unsigned len, unsigned flags,
389 struct page **pagep, void **fsdata)
391 struct gfs2_inode *ip = GFS2_I(mapping->host);
392 struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
393 unsigned int data_blocks, ind_blocks, rblocks;
396 struct gfs2_alloc *al;
397 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
398 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
399 unsigned to = from + len;
402 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
403 error = gfs2_glock_nq_atime(&ip->i_gh);
408 page = __grab_cache_page(mapping, index);
413 gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
415 error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
420 ip->i_alloc.al_requested = 0;
421 if (alloc_required) {
422 al = gfs2_alloc_get(ip);
424 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
428 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
432 al->al_requested = data_blocks + ind_blocks;
433 error = gfs2_inplace_reserve(ip);
438 rblocks = RES_DINODE + ind_blocks;
439 if (gfs2_is_jdata(ip))
440 rblocks += data_blocks ? data_blocks : 1;
441 if (ind_blocks || data_blocks)
442 rblocks += RES_STATFS + RES_QUOTA;
444 error = gfs2_trans_begin(sdp, rblocks,
445 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
449 if (gfs2_is_stuffed(ip)) {
450 if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
451 error = gfs2_unstuff_dinode(ip, page);
454 } else if (!PageUptodate(page))
455 error = stuffed_readpage(ip, page);
460 error = block_prepare_write(page, from, to, gfs2_get_block);
466 if (alloc_required) {
467 gfs2_inplace_release(ip);
469 gfs2_quota_unlock(ip);
474 page_cache_release(page);
475 if (pos + len > ip->i_inode.i_size)
476 vmtruncate(&ip->i_inode, ip->i_inode.i_size);
478 gfs2_glock_dq_m(1, &ip->i_gh);
480 gfs2_holder_uninit(&ip->i_gh);
487 * adjust_fs_space - Adjusts the free space available due to gfs2_grow
488 * @inode: the rindex inode
490 static void adjust_fs_space(struct inode *inode)
492 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
493 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
494 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
495 u64 fs_total, new_free;
497 /* Total up the file system space, according to the latest rindex. */
498 fs_total = gfs2_ri_total(sdp);
500 spin_lock(&sdp->sd_statfs_spin);
501 if (fs_total > (m_sc->sc_total + l_sc->sc_total))
502 new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
505 spin_unlock(&sdp->sd_statfs_spin);
506 fs_warn(sdp, "File system extended by %llu blocks.\n",
507 (unsigned long long)new_free);
508 gfs2_statfs_change(sdp, new_free, new_free, 0);
512 * gfs2_stuffed_write_end - Write end for stuffed files
514 * @dibh: The buffer_head containing the on-disk inode
515 * @pos: The file position
516 * @len: The length of the write
517 * @copied: How much was actually copied by the VFS
520 * This copies the data from the page into the inode block after
521 * the inode data structure itself.
525 static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
526 loff_t pos, unsigned len, unsigned copied,
529 struct gfs2_inode *ip = GFS2_I(inode);
530 struct gfs2_sbd *sdp = GFS2_SB(inode);
531 u64 to = pos + copied;
533 unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
534 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
536 BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
537 kaddr = kmap_atomic(page, KM_USER0);
538 memcpy(buf + pos, kaddr + pos, copied);
539 memset(kaddr + pos + copied, 0, len - copied);
540 flush_dcache_page(page);
541 kunmap_atomic(kaddr, KM_USER0);
543 if (!PageUptodate(page))
544 SetPageUptodate(page);
546 page_cache_release(page);
548 if (inode->i_size < to) {
549 i_size_write(inode, to);
550 ip->i_di.di_size = inode->i_size;
551 di->di_size = cpu_to_be64(inode->i_size);
552 mark_inode_dirty(inode);
555 if (inode == sdp->sd_rindex)
556 adjust_fs_space(inode);
560 gfs2_glock_dq(&ip->i_gh);
561 gfs2_holder_uninit(&ip->i_gh);
567 * @file: The file to write to
568 * @mapping: The address space to write to
569 * @pos: The file position
570 * @len: The length of the data
572 * @page: The page that has been written
573 * @fsdata: The fsdata (unused in GFS2)
575 * The main write_end function for GFS2. We have a separate one for
576 * stuffed files as they are slightly different, otherwise we just
577 * put our locking around the VFS provided functions.
582 static int gfs2_write_end(struct file *file, struct address_space *mapping,
583 loff_t pos, unsigned len, unsigned copied,
584 struct page *page, void *fsdata)
586 struct inode *inode = page->mapping->host;
587 struct gfs2_inode *ip = GFS2_I(inode);
588 struct gfs2_sbd *sdp = GFS2_SB(inode);
589 struct buffer_head *dibh;
590 struct gfs2_alloc *al = &ip->i_alloc;
591 struct gfs2_dinode *di;
592 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
593 unsigned int to = from + len;
596 BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
598 ret = gfs2_meta_inode_buffer(ip, &dibh);
601 page_cache_release(page);
605 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
607 if (gfs2_is_stuffed(ip))
608 return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
610 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
611 gfs2_page_add_databufs(ip, page, from, to);
613 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
615 if (likely(ret >= 0)) {
617 if ((pos + copied) > inode->i_size) {
618 di = (struct gfs2_dinode *)dibh->b_data;
619 ip->i_di.di_size = inode->i_size;
620 di->di_size = cpu_to_be64(inode->i_size);
621 mark_inode_dirty(inode);
625 if (inode == sdp->sd_rindex)
626 adjust_fs_space(inode);
631 if (al->al_requested) {
632 gfs2_inplace_release(ip);
633 gfs2_quota_unlock(ip);
636 gfs2_glock_dq(&ip->i_gh);
637 gfs2_holder_uninit(&ip->i_gh);
642 * gfs2_set_page_dirty - Page dirtying function
643 * @page: The page to dirty
645 * Returns: 1 if it dirtyed the page, or 0 otherwise
648 static int gfs2_set_page_dirty(struct page *page)
650 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
651 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
653 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
654 SetPageChecked(page);
655 return __set_page_dirty_buffers(page);
659 * gfs2_bmap - Block map function
660 * @mapping: Address space info
661 * @lblock: The block to map
663 * Returns: The disk address for the block or 0 on hole or error
666 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
668 struct gfs2_inode *ip = GFS2_I(mapping->host);
669 struct gfs2_holder i_gh;
673 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
677 if (!gfs2_is_stuffed(ip))
678 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
680 gfs2_glock_dq_uninit(&i_gh);
685 static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
687 struct gfs2_bufdata *bd;
691 clear_buffer_dirty(bh);
694 if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
695 list_del_init(&bd->bd_le.le_list);
697 gfs2_remove_from_journal(bh, current->journal_info, 0);
700 clear_buffer_mapped(bh);
701 clear_buffer_req(bh);
702 clear_buffer_new(bh);
703 gfs2_log_unlock(sdp);
707 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
709 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
710 struct buffer_head *bh, *head;
711 unsigned long pos = 0;
713 BUG_ON(!PageLocked(page));
715 ClearPageChecked(page);
716 if (!page_has_buffers(page))
719 bh = head = page_buffers(page);
722 gfs2_discard(sdp, bh);
724 bh = bh->b_this_page;
725 } while (bh != head);
728 try_to_release_page(page, 0);
732 * gfs2_ok_for_dio - check that dio is valid on this file
735 * @offset: The offset at which we are reading or writing
737 * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
738 * 1 (to accept the i/o request)
740 static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
743 * Should we return an error here? I can't see that O_DIRECT for
744 * a journaled file makes any sense. For now we'll silently fall
745 * back to buffered I/O, likewise we do the same for stuffed
746 * files since they are (a) small and (b) unaligned.
748 if (gfs2_is_jdata(ip))
751 if (gfs2_is_stuffed(ip))
754 if (offset > i_size_read(&ip->i_inode))
761 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
762 const struct iovec *iov, loff_t offset,
763 unsigned long nr_segs)
765 struct file *file = iocb->ki_filp;
766 struct inode *inode = file->f_mapping->host;
767 struct gfs2_inode *ip = GFS2_I(inode);
768 struct gfs2_holder gh;
772 * Deferred lock, even if its a write, since we do no allocation
773 * on this path. All we need change is atime, and this lock mode
774 * ensures that other nodes have flushed their buffered read caches
775 * (i.e. their page cache entries for this inode). We do not,
776 * unfortunately have the option of only flushing a range like
779 gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
780 rv = gfs2_glock_nq_atime(&gh);
783 rv = gfs2_ok_for_dio(ip, rw, offset);
785 goto out; /* dio not valid, fall back to buffered i/o */
787 rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
788 iov, offset, nr_segs,
789 gfs2_get_block_direct, NULL);
791 gfs2_glock_dq_m(1, &gh);
792 gfs2_holder_uninit(&gh);
797 * gfs2_releasepage - free the metadata associated with a page
798 * @page: the page that's being released
799 * @gfp_mask: passed from Linux VFS, ignored by us
801 * Call try_to_free_buffers() if the buffers in this page can be
807 int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
809 struct inode *aspace = page->mapping->host;
810 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
811 struct buffer_head *bh, *head;
812 struct gfs2_bufdata *bd;
814 if (!page_has_buffers(page))
818 head = bh = page_buffers(page);
820 if (atomic_read(&bh->b_count))
823 if (bd && bd->bd_ail)
825 gfs2_assert_warn(sdp, !buffer_pinned(bh));
826 gfs2_assert_warn(sdp, !buffer_dirty(bh));
827 bh = bh->b_this_page;
829 gfs2_log_unlock(sdp);
831 head = bh = page_buffers(page);
836 gfs2_assert_warn(sdp, bd->bd_bh == bh);
837 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
838 if (!list_empty(&bd->bd_le.le_list)) {
839 if (!buffer_pinned(bh))
840 list_del_init(&bd->bd_le.le_list);
846 bh->b_private = NULL;
848 gfs2_log_unlock(sdp);
850 kmem_cache_free(gfs2_bufdata_cachep, bd);
852 bh = bh->b_this_page;
853 } while (bh != head);
855 return try_to_free_buffers(page);
857 gfs2_log_unlock(sdp);
861 const struct address_space_operations gfs2_file_aops = {
862 .writepage = gfs2_writepage,
863 .writepages = gfs2_writepages,
864 .readpage = gfs2_readpage,
865 .readpages = gfs2_readpages,
866 .sync_page = block_sync_page,
867 .write_begin = gfs2_write_begin,
868 .write_end = gfs2_write_end,
869 .set_page_dirty = gfs2_set_page_dirty,
871 .invalidatepage = gfs2_invalidatepage,
872 .releasepage = gfs2_releasepage,
873 .direct_IO = gfs2_direct_IO,