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