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