David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 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 |
| 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 | 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 | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 | #include "quota.h" |
| 31 | #include "trans.h" |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 32 | #include "rgrp.h" |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 33 | #include "ops_file.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 34 | #include "util.h" |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 35 | #include "glops.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | |
Steven Whitehouse | ba7f729 | 2006-07-26 11:27:10 -0400 | [diff] [blame] | 37 | |
| 38 | static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page, |
| 39 | unsigned int from, unsigned int to) |
| 40 | { |
| 41 | struct buffer_head *head = page_buffers(page); |
| 42 | unsigned int bsize = head->b_size; |
| 43 | struct buffer_head *bh; |
| 44 | unsigned int start, end; |
| 45 | |
| 46 | for (bh = head, start = 0; bh != head || !start; |
| 47 | bh = bh->b_this_page, start = end) { |
| 48 | end = start + bsize; |
| 49 | if (end <= from || start >= to) |
| 50 | continue; |
| 51 | gfs2_trans_add_bh(ip->i_gl, bh, 0); |
| 52 | } |
| 53 | } |
| 54 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 55 | /** |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 56 | * 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] | 57 | * @inode: The inode |
| 58 | * @lblock: The block number to look up |
| 59 | * @bh_result: The buffer head to return the result in |
| 60 | * @create: Non-zero if we may add block to the file |
| 61 | * |
| 62 | * Returns: errno |
| 63 | */ |
| 64 | |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 65 | int gfs2_get_block(struct inode *inode, sector_t lblock, |
| 66 | struct buffer_head *bh_result, int create) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 67 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 68 | int new = create; |
| 69 | uint64_t dblock; |
| 70 | int error; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 71 | int boundary; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 72 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 73 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 74 | if (error) |
| 75 | return error; |
| 76 | |
| 77 | if (!dblock) |
| 78 | return 0; |
| 79 | |
| 80 | map_bh(bh_result, inode->i_sb, dblock); |
| 81 | if (new) |
| 82 | set_buffer_new(bh_result); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 83 | if (boundary) |
| 84 | set_buffer_boundary(bh_result); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * get_block_noalloc - Fills in a buffer head with details about a block |
| 91 | * @inode: The inode |
| 92 | * @lblock: The block number to look up |
| 93 | * @bh_result: The buffer head to return the result in |
| 94 | * @create: Non-zero if we may add block to the file |
| 95 | * |
| 96 | * Returns: errno |
| 97 | */ |
| 98 | |
| 99 | static int get_block_noalloc(struct inode *inode, sector_t lblock, |
| 100 | struct buffer_head *bh_result, int create) |
| 101 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 102 | int new = 0; |
| 103 | uint64_t dblock; |
| 104 | int error; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 105 | int boundary; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 106 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 107 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 108 | if (error) |
| 109 | return error; |
| 110 | |
| 111 | if (dblock) |
| 112 | map_bh(bh_result, inode->i_sb, dblock); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 113 | else if (gfs2_assert_withdraw(GFS2_SB(inode), !create)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 114 | error = -EIO; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 115 | if (boundary) |
| 116 | set_buffer_boundary(bh_result); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 117 | |
| 118 | return error; |
| 119 | } |
| 120 | |
Steven Whitehouse | 623d935 | 2006-08-31 12:14:44 -0400 | [diff] [blame^] | 121 | static int get_block_direct(struct inode *inode, sector_t lblock, |
| 122 | struct buffer_head *bh_result, int create) |
| 123 | { |
| 124 | int new = 0; |
| 125 | u64 dblock; |
| 126 | int error, boundary; |
| 127 | |
| 128 | error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary); |
| 129 | if (error) |
| 130 | return error; |
| 131 | |
| 132 | if (dblock) { |
| 133 | map_bh(bh_result, inode->i_sb, dblock); |
| 134 | if (boundary) |
| 135 | set_buffer_boundary(bh_result); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 140 | /** |
| 141 | * gfs2_writepage - Write complete page |
| 142 | * @page: Page to write |
| 143 | * |
| 144 | * Returns: errno |
| 145 | * |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 146 | * Some of this is copied from block_write_full_page() although we still |
| 147 | * call it to do most of the work. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 148 | */ |
| 149 | |
| 150 | static int gfs2_writepage(struct page *page, struct writeback_control *wbc) |
| 151 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 152 | struct inode *inode = page->mapping->host; |
Steven Whitehouse | f438714 | 2006-08-08 13:23:19 -0400 | [diff] [blame] | 153 | struct gfs2_inode *ip = GFS2_I(inode); |
| 154 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 155 | loff_t i_size = i_size_read(inode); |
| 156 | pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 157 | unsigned offset; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 158 | int error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 159 | int done_trans = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 160 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 161 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) { |
| 162 | unlock_page(page); |
| 163 | return -EIO; |
| 164 | } |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 165 | if (current->journal_info) |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 166 | goto out_ignore; |
| 167 | |
| 168 | /* Is the page fully outside i_size? (truncate in progress) */ |
| 169 | offset = i_size & (PAGE_CACHE_SIZE-1); |
Steven Whitehouse | d2d7b8a | 2006-05-02 12:09:42 -0400 | [diff] [blame] | 170 | if (page->index > end_index || (page->index == end_index && !offset)) { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 171 | page->mapping->a_ops->invalidatepage(page, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 172 | unlock_page(page); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 173 | return 0; /* don't care */ |
| 174 | } |
| 175 | |
| 176 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) { |
| 177 | error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0); |
| 178 | if (error) |
| 179 | goto out_ignore; |
Steven Whitehouse | f438714 | 2006-08-08 13:23:19 -0400 | [diff] [blame] | 180 | if (!page_has_buffers(page)) { |
| 181 | create_empty_buffers(page, inode->i_sb->s_blocksize, |
| 182 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 183 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 184 | gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1); |
| 185 | done_trans = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 186 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 187 | error = block_write_full_page(page, get_block_noalloc, wbc); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 188 | if (done_trans) |
| 189 | gfs2_trans_end(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | gfs2_meta_cache_flush(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 191 | return error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 192 | |
| 193 | out_ignore: |
| 194 | redirty_page_for_writepage(wbc, page); |
| 195 | unlock_page(page); |
| 196 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 199 | static int zero_readpage(struct page *page) |
| 200 | { |
| 201 | void *kaddr; |
| 202 | |
| 203 | kaddr = kmap_atomic(page, KM_USER0); |
| 204 | memset(kaddr, 0, PAGE_CACHE_SIZE); |
| 205 | kunmap_atomic(page, KM_USER0); |
| 206 | |
| 207 | SetPageUptodate(page); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 212 | /** |
| 213 | * stuffed_readpage - Fill in a Linux page with stuffed file data |
| 214 | * @ip: the inode |
| 215 | * @page: the page |
| 216 | * |
| 217 | * Returns: errno |
| 218 | */ |
| 219 | |
| 220 | static int stuffed_readpage(struct gfs2_inode *ip, struct page *page) |
| 221 | { |
| 222 | struct buffer_head *dibh; |
| 223 | void *kaddr; |
| 224 | int error; |
| 225 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 226 | /* Only the first page of a stuffed file might contain data */ |
| 227 | if (unlikely(page->index)) |
| 228 | return zero_readpage(page); |
| 229 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 230 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 231 | if (error) |
| 232 | return error; |
| 233 | |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 234 | kaddr = kmap_atomic(page, KM_USER0); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 235 | memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 236 | ip->i_di.di_size); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 237 | memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 238 | kunmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 239 | |
| 240 | brelse(dibh); |
| 241 | |
| 242 | SetPageUptodate(page); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 247 | |
| 248 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 249 | * gfs2_readpage - readpage with locking |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 250 | * @file: The file to read a page for. N.B. This may be NULL if we are |
| 251 | * reading an internal file. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 252 | * @page: The page to read |
| 253 | * |
| 254 | * Returns: errno |
| 255 | */ |
| 256 | |
| 257 | static int gfs2_readpage(struct file *file, struct page *page) |
| 258 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 259 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
| 260 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 261 | struct gfs2_holder gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 262 | int error; |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 263 | int do_unlock = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 264 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 265 | if (likely(file != &gfs2_internal_file_sentinal)) { |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 266 | if (file) { |
| 267 | struct gfs2_file *gf = file->private_data; |
| 268 | if (test_bit(GFF_EXLOCK, &gf->f_flags)) |
| 269 | goto skip_lock; |
| 270 | } |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 271 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh); |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 272 | do_unlock = 1; |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 273 | error = gfs2_glock_nq_m_atime(1, &gh); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 274 | if (unlikely(error)) |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 275 | goto out_unlock; |
| 276 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 277 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 278 | skip_lock: |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 279 | if (gfs2_is_stuffed(ip)) { |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 280 | error = stuffed_readpage(ip, page); |
| 281 | unlock_page(page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 282 | } else |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 283 | error = mpage_readpage(page, gfs2_get_block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 284 | |
| 285 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 286 | error = -EIO; |
| 287 | |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 288 | if (file != &gfs2_internal_file_sentinal) { |
| 289 | gfs2_glock_dq_m(1, &gh); |
| 290 | gfs2_holder_uninit(&gh); |
| 291 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 292 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 293 | return error; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 294 | out_unlock: |
| 295 | unlock_page(page); |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 296 | if (do_unlock) |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 297 | gfs2_holder_uninit(&gh); |
| 298 | goto out; |
| 299 | } |
| 300 | |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 301 | /** |
| 302 | * gfs2_readpages - Read a bunch of pages at once |
| 303 | * |
| 304 | * Some notes: |
| 305 | * 1. This is only for readahead, so we can simply ignore any things |
| 306 | * which are slightly inconvenient (such as locking conflicts between |
| 307 | * the page lock and the glock) and return having done no I/O. Its |
| 308 | * obviously not something we'd want to do on too regular a basis. |
| 309 | * Any I/O we ignore at this time will be done via readpage later. |
| 310 | * 2. We have to handle stuffed files here too. |
| 311 | * 3. mpage_readpages() does most of the heavy lifting in the common case. |
| 312 | * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places. |
| 313 | * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as |
| 314 | * well as read-ahead. |
| 315 | */ |
| 316 | static int gfs2_readpages(struct file *file, struct address_space *mapping, |
| 317 | struct list_head *pages, unsigned nr_pages) |
| 318 | { |
| 319 | struct inode *inode = mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 320 | struct gfs2_inode *ip = GFS2_I(inode); |
| 321 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 322 | struct gfs2_holder gh; |
| 323 | unsigned page_idx; |
| 324 | int ret; |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 325 | int do_unlock = 0; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 326 | |
| 327 | if (likely(file != &gfs2_internal_file_sentinal)) { |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 328 | if (file) { |
| 329 | struct gfs2_file *gf = file->private_data; |
| 330 | if (test_bit(GFF_EXLOCK, &gf->f_flags)) |
| 331 | goto skip_lock; |
| 332 | } |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 333 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, |
| 334 | LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh); |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 335 | do_unlock = 1; |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 336 | ret = gfs2_glock_nq_m_atime(1, &gh); |
| 337 | if (ret == GLR_TRYFAILED) |
| 338 | goto out_noerror; |
| 339 | if (unlikely(ret)) |
| 340 | goto out_unlock; |
| 341 | } |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 342 | skip_lock: |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 343 | if (gfs2_is_stuffed(ip)) { |
| 344 | struct pagevec lru_pvec; |
| 345 | pagevec_init(&lru_pvec, 0); |
| 346 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Steven Whitehouse | ffeb874 | 2006-07-10 15:47:01 -0400 | [diff] [blame] | 347 | struct page *page = list_entry(pages->prev, struct page, lru); |
| 348 | prefetchw(&page->flags); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 349 | list_del(&page->lru); |
| 350 | if (!add_to_page_cache(page, mapping, |
| 351 | page->index, GFP_KERNEL)) { |
| 352 | ret = stuffed_readpage(ip, page); |
| 353 | unlock_page(page); |
| 354 | if (!pagevec_add(&lru_pvec, page)) |
| 355 | __pagevec_lru_add(&lru_pvec); |
Steven Whitehouse | ffeb874 | 2006-07-10 15:47:01 -0400 | [diff] [blame] | 356 | } else { |
| 357 | page_cache_release(page); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 358 | } |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 359 | } |
| 360 | pagevec_lru_add(&lru_pvec); |
| 361 | ret = 0; |
| 362 | } else { |
| 363 | /* What we really want to do .... */ |
| 364 | ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block); |
| 365 | } |
| 366 | |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 367 | if (do_unlock) { |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 368 | gfs2_glock_dq_m(1, &gh); |
| 369 | gfs2_holder_uninit(&gh); |
| 370 | } |
| 371 | out: |
| 372 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 373 | ret = -EIO; |
| 374 | return ret; |
| 375 | out_noerror: |
| 376 | ret = 0; |
| 377 | out_unlock: |
| 378 | /* unlock all pages, we can't do any I/O right now */ |
| 379 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Steven Whitehouse | ffeb874 | 2006-07-10 15:47:01 -0400 | [diff] [blame] | 380 | struct page *page = list_entry(pages->prev, struct page, lru); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 381 | list_del(&page->lru); |
| 382 | unlock_page(page); |
| 383 | page_cache_release(page); |
| 384 | } |
Steven Whitehouse | 59a1cc6 | 2006-08-04 15:41:22 -0400 | [diff] [blame] | 385 | if (do_unlock) |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 386 | gfs2_holder_uninit(&gh); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 387 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * gfs2_prepare_write - Prepare to write a page to a file |
| 392 | * @file: The file to write to |
| 393 | * @page: The page which is to be prepared for writing |
| 394 | * @from: From (byte range within page) |
| 395 | * @to: To (byte range within page) |
| 396 | * |
| 397 | * Returns: errno |
| 398 | */ |
| 399 | |
| 400 | static int gfs2_prepare_write(struct file *file, struct page *page, |
| 401 | unsigned from, unsigned to) |
| 402 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 403 | struct gfs2_inode *ip = GFS2_I(page->mapping->host); |
| 404 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 405 | unsigned int data_blocks, ind_blocks, rblocks; |
| 406 | int alloc_required; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 407 | int error = 0; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 408 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from; |
| 409 | loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 410 | struct gfs2_alloc *al; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 411 | |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 412 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 413 | error = gfs2_glock_nq_m_atime(1, &ip->i_gh); |
| 414 | if (error) |
| 415 | goto out_uninit; |
| 416 | |
| 417 | gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks); |
| 418 | |
| 419 | error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required); |
| 420 | if (error) |
| 421 | goto out_unlock; |
| 422 | |
| 423 | |
| 424 | if (alloc_required) { |
| 425 | al = gfs2_alloc_get(ip); |
| 426 | |
| 427 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 428 | if (error) |
| 429 | goto out_alloc_put; |
| 430 | |
| 431 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); |
| 432 | if (error) |
| 433 | goto out_qunlock; |
| 434 | |
| 435 | al->al_requested = data_blocks + ind_blocks; |
| 436 | error = gfs2_inplace_reserve(ip); |
| 437 | if (error) |
| 438 | goto out_qunlock; |
| 439 | } |
| 440 | |
| 441 | rblocks = RES_DINODE + ind_blocks; |
| 442 | if (gfs2_is_jdata(ip)) |
| 443 | rblocks += data_blocks ? data_blocks : 1; |
| 444 | if (ind_blocks || data_blocks) |
| 445 | rblocks += RES_STATFS + RES_QUOTA; |
| 446 | |
| 447 | error = gfs2_trans_begin(sdp, rblocks, 0); |
| 448 | if (error) |
| 449 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 450 | |
| 451 | if (gfs2_is_stuffed(ip)) { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 452 | if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) { |
Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 453 | error = gfs2_unstuff_dinode(ip, page); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 454 | if (error == 0) |
| 455 | goto prepare_write; |
| 456 | } else if (!PageUptodate(page)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 457 | error = stuffed_readpage(ip, page); |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 458 | goto out; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Steven Whitehouse | 5c4e9e0 | 2006-02-15 12:26:19 +0000 | [diff] [blame] | 461 | prepare_write: |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 462 | error = block_prepare_write(page, from, to, gfs2_get_block); |
| 463 | |
| 464 | out: |
| 465 | if (error) { |
| 466 | gfs2_trans_end(sdp); |
| 467 | if (alloc_required) { |
| 468 | gfs2_inplace_release(ip); |
| 469 | out_qunlock: |
| 470 | gfs2_quota_unlock(ip); |
| 471 | out_alloc_put: |
| 472 | gfs2_alloc_put(ip); |
| 473 | } |
| 474 | out_unlock: |
| 475 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 476 | out_uninit: |
| 477 | gfs2_holder_uninit(&ip->i_gh); |
| 478 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 479 | |
| 480 | return error; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * gfs2_commit_write - Commit write to a file |
| 485 | * @file: The file to write to |
| 486 | * @page: The page containing the data |
| 487 | * @from: From (byte range within page) |
| 488 | * @to: To (byte range within page) |
| 489 | * |
| 490 | * Returns: errno |
| 491 | */ |
| 492 | |
| 493 | static int gfs2_commit_write(struct file *file, struct page *page, |
| 494 | unsigned from, unsigned to) |
| 495 | { |
| 496 | struct inode *inode = page->mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 497 | struct gfs2_inode *ip = GFS2_I(inode); |
| 498 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 499 | int error = -EOPNOTSUPP; |
| 500 | struct buffer_head *dibh; |
| 501 | struct gfs2_alloc *al = &ip->i_alloc;; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 502 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 503 | if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl))) |
| 504 | goto fail_nounlock; |
| 505 | |
| 506 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 507 | if (error) |
| 508 | goto fail_endtrans; |
| 509 | |
| 510 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
| 511 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 512 | if (gfs2_is_stuffed(ip)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 513 | uint64_t file_size; |
| 514 | void *kaddr; |
| 515 | |
| 516 | file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 517 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 518 | kaddr = kmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 519 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from, |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 520 | (char *)kaddr + from, to - from); |
| 521 | kunmap_atomic(page, KM_USER0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 522 | |
| 523 | SetPageUptodate(page); |
| 524 | |
| 525 | if (inode->i_size < file_size) |
| 526 | i_size_write(inode, file_size); |
| 527 | } else { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 528 | if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || |
| 529 | gfs2_is_jdata(ip)) |
Steven Whitehouse | 257f9b4 | 2006-01-31 10:00:25 +0000 | [diff] [blame] | 530 | gfs2_page_add_databufs(ip, page, from, to); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 531 | error = generic_commit_write(file, page, from, to); |
| 532 | if (error) |
| 533 | goto fail; |
| 534 | } |
| 535 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 536 | if (ip->i_di.di_size < inode->i_size) |
| 537 | ip->i_di.di_size = inode->i_size; |
| 538 | |
| 539 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 540 | brelse(dibh); |
| 541 | gfs2_trans_end(sdp); |
| 542 | if (al->al_requested) { |
| 543 | gfs2_inplace_release(ip); |
| 544 | gfs2_quota_unlock(ip); |
| 545 | gfs2_alloc_put(ip); |
| 546 | } |
| 547 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 548 | gfs2_holder_uninit(&ip->i_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 549 | return 0; |
| 550 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 551 | fail: |
| 552 | brelse(dibh); |
| 553 | fail_endtrans: |
| 554 | gfs2_trans_end(sdp); |
| 555 | if (al->al_requested) { |
| 556 | gfs2_inplace_release(ip); |
| 557 | gfs2_quota_unlock(ip); |
| 558 | gfs2_alloc_put(ip); |
| 559 | } |
| 560 | gfs2_glock_dq_m(1, &ip->i_gh); |
| 561 | gfs2_holder_uninit(&ip->i_gh); |
| 562 | fail_nounlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 563 | ClearPageUptodate(page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 564 | return error; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * gfs2_bmap - Block map function |
| 569 | * @mapping: Address space info |
| 570 | * @lblock: The block to map |
| 571 | * |
| 572 | * Returns: The disk address for the block or 0 on hole or error |
| 573 | */ |
| 574 | |
| 575 | static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock) |
| 576 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 577 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 578 | struct gfs2_holder i_gh; |
| 579 | sector_t dblock = 0; |
| 580 | int error; |
| 581 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 582 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
| 583 | if (error) |
| 584 | return 0; |
| 585 | |
| 586 | if (!gfs2_is_stuffed(ip)) |
Steven Whitehouse | 4ff1467 | 2006-01-30 09:39:10 +0000 | [diff] [blame] | 587 | dblock = generic_block_bmap(mapping, lblock, gfs2_get_block); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 588 | |
| 589 | gfs2_glock_dq_uninit(&i_gh); |
| 590 | |
| 591 | return dblock; |
| 592 | } |
| 593 | |
| 594 | static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh) |
| 595 | { |
Steven Whitehouse | 64fb4eb | 2006-01-18 13:14:40 +0000 | [diff] [blame] | 596 | struct gfs2_bufdata *bd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 597 | |
| 598 | gfs2_log_lock(sdp); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 599 | bd = bh->b_private; |
Steven Whitehouse | 64fb4eb | 2006-01-18 13:14:40 +0000 | [diff] [blame] | 600 | if (bd) { |
| 601 | bd->bd_bh = NULL; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 602 | bh->b_private = NULL; |
Steven Whitehouse | 15d00c0 | 2006-08-18 15:51:09 -0400 | [diff] [blame] | 603 | } |
| 604 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 605 | |
| 606 | lock_buffer(bh); |
| 607 | clear_buffer_dirty(bh); |
| 608 | bh->b_bdev = NULL; |
| 609 | clear_buffer_mapped(bh); |
| 610 | clear_buffer_req(bh); |
| 611 | clear_buffer_new(bh); |
| 612 | clear_buffer_delay(bh); |
| 613 | unlock_buffer(bh); |
| 614 | } |
| 615 | |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 616 | static void gfs2_invalidatepage(struct page *page, unsigned long offset) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 617 | { |
Steven Whitehouse | 15d00c0 | 2006-08-18 15:51:09 -0400 | [diff] [blame] | 618 | struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 619 | struct buffer_head *head, *bh, *next; |
| 620 | unsigned int curr_off = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 621 | |
| 622 | BUG_ON(!PageLocked(page)); |
| 623 | if (!page_has_buffers(page)) |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 624 | return; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 625 | |
| 626 | bh = head = page_buffers(page); |
| 627 | do { |
| 628 | unsigned int next_off = curr_off + bh->b_size; |
| 629 | next = bh->b_this_page; |
| 630 | |
| 631 | if (offset <= curr_off) |
| 632 | discard_buffer(sdp, bh); |
| 633 | |
| 634 | curr_off = next_off; |
| 635 | bh = next; |
| 636 | } while (bh != head); |
| 637 | |
| 638 | if (!offset) |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 639 | try_to_release_page(page, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 640 | |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 641 | return; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 644 | static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb, |
| 645 | const struct iovec *iov, loff_t offset, |
| 646 | unsigned long nr_segs) |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 647 | { |
| 648 | struct file *file = iocb->ki_filp; |
| 649 | struct inode *inode = file->f_mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 650 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 651 | struct gfs2_holder gh; |
| 652 | int rv; |
| 653 | |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 654 | if (rw == READ) |
| 655 | mutex_lock(&inode->i_mutex); |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 656 | /* |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 657 | * Shared lock, even if its a write, since we do no allocation |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 658 | * on this path. All we need change is atime. |
| 659 | */ |
| 660 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 661 | rv = gfs2_glock_nq_m_atime(1, &gh); |
| 662 | if (rv) |
| 663 | goto out; |
| 664 | |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 665 | if (offset > i_size_read(inode)) |
| 666 | goto out; |
| 667 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 668 | /* |
| 669 | * Should we return an error here? I can't see that O_DIRECT for |
| 670 | * a journaled file makes any sense. For now we'll silently fall |
| 671 | * back to buffered I/O, likewise we do the same for stuffed |
| 672 | * files since they are (a) small and (b) unaligned. |
| 673 | */ |
| 674 | if (gfs2_is_jdata(ip)) |
| 675 | goto out; |
| 676 | |
| 677 | if (gfs2_is_stuffed(ip)) |
| 678 | goto out; |
| 679 | |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 680 | rv = blockdev_direct_IO_own_locking(rw, iocb, inode, |
| 681 | inode->i_sb->s_bdev, |
| 682 | iov, offset, nr_segs, |
Steven Whitehouse | 623d935 | 2006-08-31 12:14:44 -0400 | [diff] [blame^] | 683 | get_block_direct, NULL); |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 684 | out: |
| 685 | gfs2_glock_dq_m(1, &gh); |
| 686 | gfs2_holder_uninit(&gh); |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 687 | if (rw == READ) |
| 688 | mutex_unlock(&inode->i_mutex); |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 689 | |
| 690 | return rv; |
| 691 | } |
| 692 | |
| 693 | /** |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 694 | * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out. |
| 695 | * @bh: the buffer we're stuck on |
| 696 | * |
| 697 | */ |
| 698 | |
| 699 | static void stuck_releasepage(struct buffer_head *bh) |
| 700 | { |
| 701 | struct inode *inode = bh->b_page->mapping->host; |
| 702 | struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; |
| 703 | struct gfs2_bufdata *bd = bh->b_private; |
| 704 | struct gfs2_glock *gl; |
Steven Whitehouse | 166afcc | 2006-08-24 15:59:40 -0400 | [diff] [blame] | 705 | static unsigned limit = 0; |
| 706 | |
| 707 | if (limit++ > 3) |
| 708 | return; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 709 | |
| 710 | fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode); |
| 711 | fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n", |
| 712 | (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count)); |
| 713 | fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh)); |
| 714 | fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL"); |
| 715 | |
| 716 | if (!bd) |
| 717 | return; |
| 718 | |
| 719 | gl = bd->bd_gl; |
| 720 | |
| 721 | fs_warn(sdp, "gl = (%u, %llu)\n", |
| 722 | gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number); |
| 723 | |
| 724 | fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n", |
| 725 | (list_empty(&bd->bd_list_tr)) ? "no" : "yes", |
| 726 | (list_empty(&bd->bd_le.le_list)) ? "no" : "yes"); |
| 727 | |
| 728 | if (gl->gl_ops == &gfs2_inode_glops) { |
| 729 | struct gfs2_inode *ip = gl->gl_object; |
| 730 | unsigned int x; |
| 731 | |
| 732 | if (!ip) |
| 733 | return; |
| 734 | |
| 735 | fs_warn(sdp, "ip = %llu %llu\n", |
| 736 | (unsigned long long)ip->i_num.no_formal_ino, |
| 737 | (unsigned long long)ip->i_num.no_addr); |
| 738 | |
| 739 | for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) |
| 740 | fs_warn(sdp, "ip->i_cache[%u] = %s\n", |
| 741 | x, (ip->i_cache[x]) ? "!NULL" : "NULL"); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | /** |
Steven Whitehouse | 623d935 | 2006-08-31 12:14:44 -0400 | [diff] [blame^] | 746 | * gfs2_releasepage - free the metadata associated with a page |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 747 | * @page: the page that's being released |
| 748 | * @gfp_mask: passed from Linux VFS, ignored by us |
| 749 | * |
| 750 | * Call try_to_free_buffers() if the buffers in this page can be |
| 751 | * released. |
| 752 | * |
| 753 | * Returns: 0 |
| 754 | */ |
| 755 | |
| 756 | int gfs2_releasepage(struct page *page, gfp_t gfp_mask) |
| 757 | { |
| 758 | struct inode *aspace = page->mapping->host; |
| 759 | struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info; |
| 760 | struct buffer_head *bh, *head; |
| 761 | struct gfs2_bufdata *bd; |
Steven Whitehouse | 166afcc | 2006-08-24 15:59:40 -0400 | [diff] [blame] | 762 | unsigned long t = jiffies + gfs2_tune_get(sdp, gt_stall_secs) * HZ; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 763 | |
| 764 | if (!page_has_buffers(page)) |
| 765 | goto out; |
| 766 | |
| 767 | head = bh = page_buffers(page); |
| 768 | do { |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 769 | while (atomic_read(&bh->b_count)) { |
Steven Whitehouse | 166afcc | 2006-08-24 15:59:40 -0400 | [diff] [blame] | 770 | if (!atomic_read(&aspace->i_writecount)) |
| 771 | return 0; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 772 | |
Steven Whitehouse | 166afcc | 2006-08-24 15:59:40 -0400 | [diff] [blame] | 773 | if (time_after_eq(jiffies, t)) { |
| 774 | stuck_releasepage(bh); |
| 775 | /* should we withdraw here? */ |
| 776 | return 0; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 777 | } |
| 778 | |
Steven Whitehouse | 166afcc | 2006-08-24 15:59:40 -0400 | [diff] [blame] | 779 | yield(); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | gfs2_assert_warn(sdp, !buffer_pinned(bh)); |
Steven Whitehouse | 623d935 | 2006-08-31 12:14:44 -0400 | [diff] [blame^] | 783 | gfs2_assert_warn(sdp, !buffer_dirty(bh)); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 784 | |
Steven Whitehouse | 623d935 | 2006-08-31 12:14:44 -0400 | [diff] [blame^] | 785 | gfs2_log_lock(sdp); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 786 | bd = bh->b_private; |
| 787 | if (bd) { |
| 788 | gfs2_assert_warn(sdp, bd->bd_bh == bh); |
| 789 | gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr)); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 790 | gfs2_assert_warn(sdp, !bd->bd_ail); |
Steven Whitehouse | 623d935 | 2006-08-31 12:14:44 -0400 | [diff] [blame^] | 791 | bd->bd_bh = NULL; |
| 792 | if (!list_empty(&bd->bd_le.le_list)) |
| 793 | bd = NULL; |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 794 | bh->b_private = NULL; |
| 795 | } |
Steven Whitehouse | 623d935 | 2006-08-31 12:14:44 -0400 | [diff] [blame^] | 796 | gfs2_log_unlock(sdp); |
| 797 | if (bd) |
| 798 | kmem_cache_free(gfs2_bufdata_cachep, bd); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 799 | |
| 800 | bh = bh->b_this_page; |
Steven Whitehouse | 166afcc | 2006-08-24 15:59:40 -0400 | [diff] [blame] | 801 | } while (bh != head); |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 802 | |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 803 | out: |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 804 | return try_to_free_buffers(page); |
| 805 | } |
| 806 | |
Steven Whitehouse | 66de045 | 2006-07-03 13:37:30 -0400 | [diff] [blame] | 807 | const struct address_space_operations gfs2_file_aops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 808 | .writepage = gfs2_writepage, |
| 809 | .readpage = gfs2_readpage, |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 810 | .readpages = gfs2_readpages, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 811 | .sync_page = block_sync_page, |
| 812 | .prepare_write = gfs2_prepare_write, |
| 813 | .commit_write = gfs2_commit_write, |
| 814 | .bmap = gfs2_bmap, |
| 815 | .invalidatepage = gfs2_invalidatepage, |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 816 | .releasepage = gfs2_releasepage, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 817 | .direct_IO = gfs2_direct_IO, |
| 818 | }; |
| 819 | |