David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 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 |
| 7 | * of the GNU General Public License v.2. |
| 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 | 9b124fb | 2006-01-30 11:55:32 +0000 | [diff] [blame] | 16 | #include <linux/mpage.h> |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 17 | #include <linux/fs.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 18 | #include <asm/semaphore.h> |
| 19 | |
| 20 | #include "gfs2.h" |
| 21 | #include "bmap.h" |
| 22 | #include "glock.h" |
| 23 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "log.h" |
| 25 | #include "meta_io.h" |
| 26 | #include "ops_address.h" |
| 27 | #include "page.h" |
| 28 | #include "quota.h" |
| 29 | #include "trans.h" |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 30 | #include "rgrp.h" |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 31 | #include "ops_file.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 34 | * 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] | 35 | * @inode: The inode |
| 36 | * @lblock: The block number to look up |
| 37 | * @bh_result: The buffer head to return the result in |
| 38 | * @create: Non-zero if we may add block to the file |
| 39 | * |
| 40 | * Returns: errno |
| 41 | */ |
| 42 | |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 43 | int gfs2_get_block(struct inode *inode, sector_t lblock, |
| 44 | struct buffer_head *bh_result, int create) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 45 | { |
| 46 | struct gfs2_inode *ip = get_v2ip(inode); |
| 47 | int new = create; |
| 48 | uint64_t dblock; |
| 49 | int error; |
| 50 | |
| 51 | error = gfs2_block_map(ip, lblock, &new, &dblock, NULL); |
| 52 | if (error) |
| 53 | return error; |
| 54 | |
| 55 | if (!dblock) |
| 56 | return 0; |
| 57 | |
| 58 | map_bh(bh_result, inode->i_sb, dblock); |
| 59 | if (new) |
| 60 | set_buffer_new(bh_result); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * get_block_noalloc - Fills in a buffer head with details about a block |
| 67 | * @inode: The inode |
| 68 | * @lblock: The block number to look up |
| 69 | * @bh_result: The buffer head to return the result in |
| 70 | * @create: Non-zero if we may add block to the file |
| 71 | * |
| 72 | * Returns: errno |
| 73 | */ |
| 74 | |
| 75 | static int get_block_noalloc(struct inode *inode, sector_t lblock, |
| 76 | struct buffer_head *bh_result, int create) |
| 77 | { |
| 78 | struct gfs2_inode *ip = get_v2ip(inode); |
| 79 | int new = 0; |
| 80 | uint64_t dblock; |
| 81 | int error; |
| 82 | |
| 83 | error = gfs2_block_map(ip, lblock, &new, &dblock, NULL); |
| 84 | if (error) |
| 85 | return error; |
| 86 | |
| 87 | if (dblock) |
| 88 | map_bh(bh_result, inode->i_sb, dblock); |
| 89 | else if (gfs2_assert_withdraw(ip->i_sbd, !create)) |
| 90 | error = -EIO; |
| 91 | |
| 92 | return error; |
| 93 | } |
| 94 | |
| 95 | static int get_blocks(struct inode *inode, sector_t lblock, |
| 96 | unsigned long max_blocks, struct buffer_head *bh_result, |
| 97 | int create) |
| 98 | { |
| 99 | struct gfs2_inode *ip = get_v2ip(inode); |
| 100 | int new = create; |
| 101 | uint64_t dblock; |
| 102 | uint32_t extlen; |
| 103 | int error; |
| 104 | |
| 105 | error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen); |
| 106 | if (error) |
| 107 | return error; |
| 108 | |
| 109 | if (!dblock) |
| 110 | return 0; |
| 111 | |
| 112 | map_bh(bh_result, inode->i_sb, dblock); |
| 113 | if (new) |
| 114 | set_buffer_new(bh_result); |
| 115 | |
| 116 | if (extlen > max_blocks) |
| 117 | extlen = max_blocks; |
| 118 | bh_result->b_size = extlen << inode->i_blkbits; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int get_blocks_noalloc(struct inode *inode, sector_t lblock, |
| 124 | unsigned long max_blocks, |
| 125 | struct buffer_head *bh_result, int create) |
| 126 | { |
| 127 | struct gfs2_inode *ip = get_v2ip(inode); |
| 128 | int new = 0; |
| 129 | uint64_t dblock; |
| 130 | uint32_t extlen; |
| 131 | int error; |
| 132 | |
| 133 | error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen); |
| 134 | if (error) |
| 135 | return error; |
| 136 | |
| 137 | if (dblock) { |
| 138 | map_bh(bh_result, inode->i_sb, dblock); |
| 139 | if (extlen > max_blocks) |
| 140 | extlen = max_blocks; |
| 141 | bh_result->b_size = extlen << inode->i_blkbits; |
| 142 | } else if (gfs2_assert_withdraw(ip->i_sbd, !create)) |
| 143 | error = -EIO; |
| 144 | |
| 145 | return error; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * gfs2_writepage - Write complete page |
| 150 | * @page: Page to write |
| 151 | * |
| 152 | * Returns: errno |
| 153 | * |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 154 | * Some of this is copied from block_write_full_page() although we still |
| 155 | * call it to do most of the work. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 156 | */ |
| 157 | |
| 158 | static int gfs2_writepage(struct page *page, struct writeback_control *wbc) |
| 159 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 160 | struct inode *inode = page->mapping->host; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 161 | struct gfs2_inode *ip = get_v2ip(page->mapping->host); |
| 162 | struct gfs2_sbd *sdp = ip->i_sbd; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 163 | loff_t i_size = i_size_read(inode); |
| 164 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 165 | unsigned offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 166 | int error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 167 | int done_trans = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 168 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 169 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) { |
| 170 | unlock_page(page); |
| 171 | return -EIO; |
| 172 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 173 | if (get_transaction) |
| 174 | goto out_ignore; |
| 175 | |
| 176 | /* Is the page fully outside i_size? (truncate in progress) */ |
| 177 | offset = i_size & (PAGE_CACHE_SIZE-1); |
| 178 | if (page->index >= end_index+1 || !offset) { |
| 179 | page->mapping->a_ops->invalidatepage(page, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 180 | unlock_page(page); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 181 | return 0; /* don't care */ |
| 182 | } |
| 183 | |
| 184 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) { |
| 185 | error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0); |
| 186 | if (error) |
| 187 | goto out_ignore; |
| 188 | gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1); |
| 189 | done_trans = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 191 | error = block_write_full_page(page, get_block_noalloc, wbc); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 192 | if (done_trans) |
| 193 | gfs2_trans_end(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 194 | gfs2_meta_cache_flush(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 195 | return error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 196 | |
| 197 | out_ignore: |
| 198 | redirty_page_for_writepage(wbc, page); |
| 199 | unlock_page(page); |
| 200 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * stuffed_readpage - Fill in a Linux page with stuffed file data |
| 205 | * @ip: the inode |
| 206 | * @page: the page |
| 207 | * |
| 208 | * Returns: errno |
| 209 | */ |
| 210 | |
| 211 | static int stuffed_readpage(struct gfs2_inode *ip, struct page *page) |
| 212 | { |
| 213 | struct buffer_head *dibh; |
| 214 | void *kaddr; |
| 215 | int error; |
| 216 | |
| 217 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 218 | if (error) |
| 219 | return error; |
| 220 | |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 221 | kaddr = kmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 222 | memcpy((char *)kaddr, |
| 223 | dibh->b_data + sizeof(struct gfs2_dinode), |
| 224 | ip->i_di.di_size); |
| 225 | memset((char *)kaddr + ip->i_di.di_size, |
| 226 | 0, |
| 227 | PAGE_CACHE_SIZE - ip->i_di.di_size); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 228 | kunmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 229 | |
| 230 | brelse(dibh); |
| 231 | |
| 232 | SetPageUptodate(page); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int zero_readpage(struct page *page) |
| 238 | { |
| 239 | void *kaddr; |
| 240 | |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 241 | kaddr = kmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 242 | memset(kaddr, 0, PAGE_CACHE_SIZE); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 243 | kunmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 244 | |
| 245 | SetPageUptodate(page); |
| 246 | unlock_page(page); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 252 | * gfs2_readpage - readpage with locking |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 253 | * @file: The file to read a page for. N.B. This may be NULL if we are |
| 254 | * reading an internal file. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 255 | * @page: The page to read |
| 256 | * |
| 257 | * Returns: errno |
| 258 | */ |
| 259 | |
| 260 | static int gfs2_readpage(struct file *file, struct page *page) |
| 261 | { |
| 262 | struct gfs2_inode *ip = get_v2ip(page->mapping->host); |
| 263 | struct gfs2_sbd *sdp = ip->i_sbd; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 264 | struct gfs2_holder gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 265 | int error; |
| 266 | |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 267 | if (file != &gfs2_internal_file_sentinal) { |
| 268 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 269 | error = gfs2_glock_nq_m_atime(1, &gh); |
| 270 | if (error) |
| 271 | goto out_unlock; |
| 272 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 273 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 274 | if (gfs2_is_stuffed(ip)) { |
| 275 | if (!page->index) { |
| 276 | error = stuffed_readpage(ip, page); |
| 277 | unlock_page(page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 278 | } else |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 279 | error = zero_readpage(page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 280 | } else |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 281 | error = mpage_readpage(page, gfs2_get_block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 282 | |
| 283 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 284 | error = -EIO; |
| 285 | |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 286 | if (file != &gfs2_internal_file_sentinal) { |
| 287 | gfs2_glock_dq_m(1, &gh); |
| 288 | gfs2_holder_uninit(&gh); |
| 289 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 290 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 291 | return error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 292 | out_unlock: |
| 293 | unlock_page(page); |
| 294 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
| 298 | * gfs2_prepare_write - Prepare to write a page to a file |
| 299 | * @file: The file to write to |
| 300 | * @page: The page which is to be prepared for writing |
| 301 | * @from: From (byte range within page) |
| 302 | * @to: To (byte range within page) |
| 303 | * |
| 304 | * Returns: errno |
| 305 | */ |
| 306 | |
| 307 | static int gfs2_prepare_write(struct file *file, struct page *page, |
| 308 | unsigned from, unsigned to) |
| 309 | { |
| 310 | struct gfs2_inode *ip = get_v2ip(page->mapping->host); |
| 311 | struct gfs2_sbd *sdp = ip->i_sbd; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 312 | unsigned int data_blocks, ind_blocks, rblocks; |
| 313 | int alloc_required; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 314 | int error = 0; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 315 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from; |
| 316 | loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 317 | struct gfs2_alloc *al; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 318 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 319 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh); |
| 320 | error = gfs2_glock_nq_m_atime(1, &ip->i_gh); |
| 321 | if (error) |
| 322 | goto out_uninit; |
| 323 | |
| 324 | gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks); |
| 325 | |
| 326 | error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required); |
| 327 | if (error) |
| 328 | goto out_unlock; |
| 329 | |
| 330 | |
| 331 | if (alloc_required) { |
| 332 | al = gfs2_alloc_get(ip); |
| 333 | |
| 334 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 335 | if (error) |
| 336 | goto out_alloc_put; |
| 337 | |
| 338 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); |
| 339 | if (error) |
| 340 | goto out_qunlock; |
| 341 | |
| 342 | al->al_requested = data_blocks + ind_blocks; |
| 343 | error = gfs2_inplace_reserve(ip); |
| 344 | if (error) |
| 345 | goto out_qunlock; |
| 346 | } |
| 347 | |
| 348 | rblocks = RES_DINODE + ind_blocks; |
| 349 | if (gfs2_is_jdata(ip)) |
| 350 | rblocks += data_blocks ? data_blocks : 1; |
| 351 | if (ind_blocks || data_blocks) |
| 352 | rblocks += RES_STATFS + RES_QUOTA; |
| 353 | |
| 354 | error = gfs2_trans_begin(sdp, rblocks, 0); |
| 355 | if (error) |
| 356 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 357 | |
| 358 | if (gfs2_is_stuffed(ip)) { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 359 | if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame^] | 360 | error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page, |
| 361 | page); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 362 | if (error == 0) |
| 363 | goto prepare_write; |
| 364 | } else if (!PageUptodate(page)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 365 | error = stuffed_readpage(ip, page); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 366 | goto out; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 369 | prepare_write: |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 370 | error = block_prepare_write(page, from, to, gfs2_get_block); |
| 371 | |
| 372 | out: |
| 373 | if (error) { |
| 374 | gfs2_trans_end(sdp); |
| 375 | if (alloc_required) { |
| 376 | gfs2_inplace_release(ip); |
| 377 | out_qunlock: |
| 378 | gfs2_quota_unlock(ip); |
| 379 | out_alloc_put: |
| 380 | gfs2_alloc_put(ip); |
| 381 | } |
| 382 | out_unlock: |
| 383 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 384 | out_uninit: |
| 385 | gfs2_holder_uninit(&ip->i_gh); |
| 386 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 387 | |
| 388 | return error; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * gfs2_commit_write - Commit write to a file |
| 393 | * @file: The file to write to |
| 394 | * @page: The page containing the data |
| 395 | * @from: From (byte range within page) |
| 396 | * @to: To (byte range within page) |
| 397 | * |
| 398 | * Returns: errno |
| 399 | */ |
| 400 | |
| 401 | static int gfs2_commit_write(struct file *file, struct page *page, |
| 402 | unsigned from, unsigned to) |
| 403 | { |
| 404 | struct inode *inode = page->mapping->host; |
| 405 | struct gfs2_inode *ip = get_v2ip(inode); |
| 406 | struct gfs2_sbd *sdp = ip->i_sbd; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 407 | int error = -EOPNOTSUPP; |
| 408 | struct buffer_head *dibh; |
| 409 | struct gfs2_alloc *al = &ip->i_alloc;; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 410 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 411 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl))) |
| 412 | goto fail_nounlock; |
| 413 | |
| 414 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 415 | if (error) |
| 416 | goto fail_endtrans; |
| 417 | |
| 418 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 419 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 420 | if (gfs2_is_stuffed(ip)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 421 | uint64_t file_size; |
| 422 | void *kaddr; |
| 423 | |
| 424 | file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 425 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 426 | kaddr = kmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 427 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from, |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 428 | (char *)kaddr + from, to - from); |
| 429 | kunmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | |
| 431 | SetPageUptodate(page); |
| 432 | |
| 433 | if (inode->i_size < file_size) |
| 434 | i_size_write(inode, file_size); |
| 435 | } else { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame^] | 436 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || |
| 437 | gfs2_is_jdata(ip)) |
Steven Whitehouse | 257f9b4 | 2006-01-31 10:00:25 +0000 | [diff] [blame] | 438 | gfs2_page_add_databufs(ip, page, from, to); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 439 | error = generic_commit_write(file, page, from, to); |
| 440 | if (error) |
| 441 | goto fail; |
| 442 | } |
| 443 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 444 | if (ip->i_di.di_size < inode->i_size) |
| 445 | ip->i_di.di_size = inode->i_size; |
| 446 | |
| 447 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 448 | brelse(dibh); |
| 449 | gfs2_trans_end(sdp); |
| 450 | if (al->al_requested) { |
| 451 | gfs2_inplace_release(ip); |
| 452 | gfs2_quota_unlock(ip); |
| 453 | gfs2_alloc_put(ip); |
| 454 | } |
| 455 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 456 | gfs2_holder_uninit(&ip->i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 457 | return 0; |
| 458 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 459 | fail: |
| 460 | brelse(dibh); |
| 461 | fail_endtrans: |
| 462 | gfs2_trans_end(sdp); |
| 463 | if (al->al_requested) { |
| 464 | gfs2_inplace_release(ip); |
| 465 | gfs2_quota_unlock(ip); |
| 466 | gfs2_alloc_put(ip); |
| 467 | } |
| 468 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 469 | gfs2_holder_uninit(&ip->i_gh); |
| 470 | fail_nounlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 471 | ClearPageUptodate(page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 472 | return error; |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * gfs2_bmap - Block map function |
| 477 | * @mapping: Address space info |
| 478 | * @lblock: The block to map |
| 479 | * |
| 480 | * Returns: The disk address for the block or 0 on hole or error |
| 481 | */ |
| 482 | |
| 483 | static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock) |
| 484 | { |
| 485 | struct gfs2_inode *ip = get_v2ip(mapping->host); |
| 486 | struct gfs2_holder i_gh; |
| 487 | sector_t dblock = 0; |
| 488 | int error; |
| 489 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 490 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
| 491 | if (error) |
| 492 | return 0; |
| 493 | |
| 494 | if (!gfs2_is_stuffed(ip)) |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 495 | dblock = generic_block_bmap(mapping, lblock, gfs2_get_block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 496 | |
| 497 | gfs2_glock_dq_uninit(&i_gh); |
| 498 | |
| 499 | return dblock; |
| 500 | } |
| 501 | |
| 502 | static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh) |
| 503 | { |
Steven Whitehouse | 64fb4eb | 2006-01-18 13:14:40 +0000 | [diff] [blame] | 504 | struct gfs2_bufdata *bd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 505 | |
| 506 | gfs2_log_lock(sdp); |
Steven Whitehouse | 64fb4eb | 2006-01-18 13:14:40 +0000 | [diff] [blame] | 507 | bd = get_v2bd(bh); |
| 508 | if (bd) { |
| 509 | bd->bd_bh = NULL; |
| 510 | set_v2bd(bh, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 511 | gfs2_log_unlock(sdp); |
| 512 | brelse(bh); |
| 513 | } else |
| 514 | gfs2_log_unlock(sdp); |
| 515 | |
| 516 | lock_buffer(bh); |
| 517 | clear_buffer_dirty(bh); |
| 518 | bh->b_bdev = NULL; |
| 519 | clear_buffer_mapped(bh); |
| 520 | clear_buffer_req(bh); |
| 521 | clear_buffer_new(bh); |
| 522 | clear_buffer_delay(bh); |
| 523 | unlock_buffer(bh); |
| 524 | } |
| 525 | |
| 526 | static int gfs2_invalidatepage(struct page *page, unsigned long offset) |
| 527 | { |
| 528 | struct gfs2_sbd *sdp = get_v2sdp(page->mapping->host->i_sb); |
| 529 | struct buffer_head *head, *bh, *next; |
| 530 | unsigned int curr_off = 0; |
| 531 | int ret = 1; |
| 532 | |
| 533 | BUG_ON(!PageLocked(page)); |
| 534 | if (!page_has_buffers(page)) |
| 535 | return 1; |
| 536 | |
| 537 | bh = head = page_buffers(page); |
| 538 | do { |
| 539 | unsigned int next_off = curr_off + bh->b_size; |
| 540 | next = bh->b_this_page; |
| 541 | |
| 542 | if (offset <= curr_off) |
| 543 | discard_buffer(sdp, bh); |
| 544 | |
| 545 | curr_off = next_off; |
| 546 | bh = next; |
| 547 | } while (bh != head); |
| 548 | |
| 549 | if (!offset) |
| 550 | ret = try_to_release_page(page, 0); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 555 | static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov, |
| 556 | loff_t offset, unsigned long nr_segs) |
| 557 | { |
| 558 | struct file *file = iocb->ki_filp; |
| 559 | struct inode *inode = file->f_mapping->host; |
| 560 | struct gfs2_inode *ip = get_v2ip(inode); |
| 561 | struct gfs2_holder gh; |
| 562 | int rv; |
| 563 | |
| 564 | /* |
| 565 | * Shared lock, even though its write, since we do no allocation |
| 566 | * on this path. All we need change is atime. |
| 567 | */ |
| 568 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 569 | rv = gfs2_glock_nq_m_atime(1, &gh); |
| 570 | if (rv) |
| 571 | goto out; |
| 572 | |
| 573 | /* |
| 574 | * Should we return an error here? I can't see that O_DIRECT for |
| 575 | * a journaled file makes any sense. For now we'll silently fall |
| 576 | * back to buffered I/O, likewise we do the same for stuffed |
| 577 | * files since they are (a) small and (b) unaligned. |
| 578 | */ |
| 579 | if (gfs2_is_jdata(ip)) |
| 580 | goto out; |
| 581 | |
| 582 | if (gfs2_is_stuffed(ip)) |
| 583 | goto out; |
| 584 | |
| 585 | rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev, |
| 586 | iov, offset, nr_segs, get_blocks_noalloc, |
| 587 | NULL, DIO_OWN_LOCKING); |
| 588 | out: |
| 589 | gfs2_glock_dq_m(1, &gh); |
| 590 | gfs2_holder_uninit(&gh); |
| 591 | |
| 592 | return rv; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * gfs2_direct_IO |
| 597 | * |
| 598 | * This is called with a shared lock already held for the read path. |
| 599 | * Currently, no locks are held when the write path is called. |
| 600 | */ |
| 601 | static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb, |
| 602 | const struct iovec *iov, loff_t offset, |
| 603 | unsigned long nr_segs) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 604 | { |
| 605 | struct file *file = iocb->ki_filp; |
| 606 | struct inode *inode = file->f_mapping->host; |
| 607 | struct gfs2_inode *ip = get_v2ip(inode); |
| 608 | struct gfs2_sbd *sdp = ip->i_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 609 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 610 | if (rw == WRITE) |
| 611 | return gfs2_direct_IO_write(iocb, iov, offset, nr_segs); |
| 612 | |
| 613 | if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) || |
| 614 | gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip))) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 615 | return -EINVAL; |
| 616 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 617 | return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov, |
| 618 | offset, nr_segs, get_blocks, NULL, |
| 619 | DIO_OWN_LOCKING); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | struct address_space_operations gfs2_file_aops = { |
| 623 | .writepage = gfs2_writepage, |
| 624 | .readpage = gfs2_readpage, |
| 625 | .sync_page = block_sync_page, |
| 626 | .prepare_write = gfs2_prepare_write, |
| 627 | .commit_write = gfs2_commit_write, |
| 628 | .bmap = gfs2_bmap, |
| 629 | .invalidatepage = gfs2_invalidatepage, |
| 630 | .direct_IO = gfs2_direct_IO, |
| 631 | }; |
| 632 | |