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 |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/writeback.h> |
| 18 | #include <linux/swap.h> |
| 19 | #include <linux/delay.h> |
Steven Whitehouse | 2e565bb | 2006-10-02 11:38:25 -0400 | [diff] [blame] | 20 | #include <linux/bio.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 21 | #include <linux/gfs2_ondisk.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 22 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 23 | |
| 24 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 25 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 26 | #include "glock.h" |
| 27 | #include "glops.h" |
| 28 | #include "inode.h" |
| 29 | #include "log.h" |
| 30 | #include "lops.h" |
| 31 | #include "meta_io.h" |
| 32 | #include "rgrp.h" |
| 33 | #include "trans.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 "ops_address.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 37 | static int aspace_get_block(struct inode *inode, sector_t lblock, |
| 38 | struct buffer_head *bh_result, int create) |
| 39 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 40 | gfs2_assert_warn(inode->i_sb->s_fs_info, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 41 | return -EOPNOTSUPP; |
| 42 | } |
| 43 | |
| 44 | static int gfs2_aspace_writepage(struct page *page, |
| 45 | struct writeback_control *wbc) |
| 46 | { |
| 47 | return block_write_full_page(page, aspace_get_block, wbc); |
| 48 | } |
| 49 | |
Steven Whitehouse | 66de045 | 2006-07-03 13:37:30 -0400 | [diff] [blame] | 50 | static const struct address_space_operations aspace_aops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 51 | .writepage = gfs2_aspace_writepage, |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 52 | .releasepage = gfs2_releasepage, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * gfs2_aspace_get - Create and initialize a struct inode structure |
| 57 | * @sdp: the filesystem the aspace is in |
| 58 | * |
| 59 | * Right now a struct inode is just a struct inode. Maybe Linux |
| 60 | * will supply a more lightweight address space construct (that works) |
| 61 | * in the future. |
| 62 | * |
| 63 | * Make sure pages/buffers in this aspace aren't in high memory. |
| 64 | * |
| 65 | * Returns: the aspace |
| 66 | */ |
| 67 | |
| 68 | struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp) |
| 69 | { |
| 70 | struct inode *aspace; |
| 71 | |
| 72 | aspace = new_inode(sdp->sd_vfs); |
| 73 | if (aspace) { |
Steven Whitehouse | f3bba03 | 2006-07-11 09:50:54 -0400 | [diff] [blame] | 74 | mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 75 | aspace->i_mapping->a_ops = &aspace_aops; |
| 76 | aspace->i_size = ~0ULL; |
Theodore Ts'o | bba9dfd | 2006-09-27 01:50:31 -0700 | [diff] [blame] | 77 | aspace->i_private = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 78 | insert_inode_hash(aspace); |
| 79 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 80 | return aspace; |
| 81 | } |
| 82 | |
| 83 | void gfs2_aspace_put(struct inode *aspace) |
| 84 | { |
| 85 | remove_inode_hash(aspace); |
| 86 | iput(aspace); |
| 87 | } |
| 88 | |
| 89 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 90 | * gfs2_meta_inval - Invalidate all buffers associated with a glock |
| 91 | * @gl: the glock |
| 92 | * |
| 93 | */ |
| 94 | |
| 95 | void gfs2_meta_inval(struct gfs2_glock *gl) |
| 96 | { |
| 97 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 98 | struct inode *aspace = gl->gl_aspace; |
| 99 | struct address_space *mapping = gl->gl_aspace->i_mapping; |
| 100 | |
| 101 | gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count)); |
| 102 | |
| 103 | atomic_inc(&aspace->i_writecount); |
| 104 | truncate_inode_pages(mapping, 0); |
| 105 | atomic_dec(&aspace->i_writecount); |
| 106 | |
| 107 | gfs2_assert_withdraw(sdp, !mapping->nrpages); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * gfs2_meta_sync - Sync all buffers associated with a glock |
| 112 | * @gl: The glock |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 113 | * |
| 114 | */ |
| 115 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 116 | void gfs2_meta_sync(struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 117 | { |
| 118 | struct address_space *mapping = gl->gl_aspace->i_mapping; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 119 | int error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 120 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 121 | filemap_fdatawrite(mapping); |
| 122 | error = filemap_fdatawait(mapping); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 123 | |
| 124 | if (error) |
| 125 | gfs2_io_error(gl->gl_sbd); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * getbuf - Get a buffer with a given address space |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 130 | * @gl: the glock |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 131 | * @blkno: the block number (filesystem scope) |
| 132 | * @create: 1 if the buffer should be created |
| 133 | * |
| 134 | * Returns: the buffer |
| 135 | */ |
| 136 | |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 137 | static struct buffer_head *getbuf(struct gfs2_glock *gl, u64 blkno, int create) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 138 | { |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 139 | struct address_space *mapping = gl->gl_aspace->i_mapping; |
| 140 | struct gfs2_sbd *sdp = gl->gl_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 141 | struct page *page; |
| 142 | struct buffer_head *bh; |
| 143 | unsigned int shift; |
| 144 | unsigned long index; |
| 145 | unsigned int bufnum; |
| 146 | |
| 147 | shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift; |
| 148 | index = blkno >> shift; /* convert block to page */ |
| 149 | bufnum = blkno - (index << shift); /* block buf index within page */ |
| 150 | |
| 151 | if (create) { |
| 152 | for (;;) { |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 153 | page = grab_cache_page(mapping, index); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 154 | if (page) |
| 155 | break; |
| 156 | yield(); |
| 157 | } |
| 158 | } else { |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 159 | page = find_lock_page(mapping, index); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 160 | if (!page) |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | if (!page_has_buffers(page)) |
| 165 | create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0); |
| 166 | |
| 167 | /* Locate header for our buffer within our page */ |
| 168 | for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page) |
| 169 | /* Do nothing */; |
| 170 | get_bh(bh); |
| 171 | |
| 172 | if (!buffer_mapped(bh)) |
| 173 | map_bh(bh, sdp->sd_vfs, blkno); |
| 174 | |
| 175 | unlock_page(page); |
| 176 | mark_page_accessed(page); |
| 177 | page_cache_release(page); |
| 178 | |
| 179 | return bh; |
| 180 | } |
| 181 | |
| 182 | static void meta_prep_new(struct buffer_head *bh) |
| 183 | { |
| 184 | struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data; |
| 185 | |
| 186 | lock_buffer(bh); |
| 187 | clear_buffer_dirty(bh); |
| 188 | set_buffer_uptodate(bh); |
| 189 | unlock_buffer(bh); |
| 190 | |
| 191 | mh->mh_magic = cpu_to_be32(GFS2_MAGIC); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * gfs2_meta_new - Get a block |
| 196 | * @gl: The glock associated with this block |
| 197 | * @blkno: The block number |
| 198 | * |
| 199 | * Returns: The buffer |
| 200 | */ |
| 201 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 202 | struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 203 | { |
| 204 | struct buffer_head *bh; |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 205 | bh = getbuf(gl, blkno, CREATE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 206 | meta_prep_new(bh); |
| 207 | return bh; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * gfs2_meta_read - Read a block from disk |
| 212 | * @gl: The glock covering the block |
| 213 | * @blkno: The block number |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 214 | * @flags: flags |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 215 | * @bhp: the place where the buffer is returned (NULL on failure) |
| 216 | * |
| 217 | * Returns: errno |
| 218 | */ |
| 219 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 220 | int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 221 | struct buffer_head **bhp) |
| 222 | { |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 223 | *bhp = getbuf(gl, blkno, CREATE); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 224 | if (!buffer_uptodate(*bhp)) |
Steven Whitehouse | 2e565bb | 2006-10-02 11:38:25 -0400 | [diff] [blame] | 225 | ll_rw_block(READ_META, 1, bhp); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 226 | if (flags & DIO_WAIT) { |
| 227 | int error = gfs2_meta_wait(gl->gl_sbd, *bhp); |
| 228 | if (error) { |
| 229 | brelse(*bhp); |
| 230 | return error; |
| 231 | } |
| 232 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 233 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 234 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 238 | * gfs2_meta_wait - Reread a block from disk |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 239 | * @sdp: the filesystem |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 240 | * @bh: The block to wait for |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 241 | * |
| 242 | * Returns: errno |
| 243 | */ |
| 244 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 245 | int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 246 | { |
| 247 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 248 | return -EIO; |
| 249 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 250 | wait_on_buffer(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 251 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 252 | if (!buffer_uptodate(bh)) { |
| 253 | struct gfs2_trans *tr = current->journal_info; |
| 254 | if (tr && tr->tr_touched) |
| 255 | gfs2_io_error_bh(sdp, bh); |
| 256 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 257 | } |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 258 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 259 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /** |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 265 | * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 266 | * @gl: the glock the buffer belongs to |
| 267 | * @bh: The buffer to be attached to |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 268 | * @meta: Flag to indicate whether its metadata or not |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 269 | */ |
| 270 | |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 271 | void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh, |
| 272 | int meta) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 273 | { |
| 274 | struct gfs2_bufdata *bd; |
| 275 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 276 | if (meta) |
| 277 | lock_page(bh->b_page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 278 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 279 | if (bh->b_private) { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 280 | if (meta) |
| 281 | unlock_page(bh->b_page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 285 | bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 286 | memset(bd, 0, sizeof(struct gfs2_bufdata)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 287 | bd->bd_bh = bh; |
| 288 | bd->bd_gl = gl; |
| 289 | |
| 290 | INIT_LIST_HEAD(&bd->bd_list_tr); |
Steven Whitehouse | 82ffa51 | 2006-09-04 14:47:06 -0400 | [diff] [blame] | 291 | if (meta) |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 292 | lops_init_le(&bd->bd_le, &gfs2_buf_lops); |
Steven Whitehouse | 82ffa51 | 2006-09-04 14:47:06 -0400 | [diff] [blame] | 293 | else |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 294 | lops_init_le(&bd->bd_le, &gfs2_databuf_lops); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 295 | bh->b_private = bd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 296 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 297 | if (meta) |
| 298 | unlock_page(bh->b_page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 302 | * gfs2_pin - Pin a buffer in memory |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 303 | * @sdp: the filesystem the buffer belongs to |
| 304 | * @bh: The buffer to be pinned |
| 305 | * |
| 306 | */ |
| 307 | |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 308 | void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 309 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 310 | struct gfs2_bufdata *bd = bh->b_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 311 | |
| 312 | gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)); |
| 313 | |
| 314 | if (test_set_buffer_pinned(bh)) |
| 315 | gfs2_assert_withdraw(sdp, 0); |
| 316 | |
| 317 | wait_on_buffer(bh); |
| 318 | |
| 319 | /* If this buffer is in the AIL and it has already been written |
| 320 | to in-place disk block, remove it from the AIL. */ |
| 321 | |
| 322 | gfs2_log_lock(sdp); |
| 323 | if (bd->bd_ail && !buffer_in_io(bh)) |
| 324 | list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list); |
| 325 | gfs2_log_unlock(sdp); |
| 326 | |
| 327 | clear_buffer_dirty(bh); |
| 328 | wait_on_buffer(bh); |
| 329 | |
| 330 | if (!buffer_uptodate(bh)) |
| 331 | gfs2_io_error_bh(sdp, bh); |
| 332 | |
| 333 | get_bh(bh); |
| 334 | } |
| 335 | |
| 336 | /** |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 337 | * gfs2_unpin - Unpin a buffer |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 338 | * @sdp: the filesystem the buffer belongs to |
| 339 | * @bh: The buffer to unpin |
| 340 | * @ai: |
| 341 | * |
| 342 | */ |
| 343 | |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 344 | void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, |
| 345 | struct gfs2_ail *ai) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 346 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 347 | struct gfs2_bufdata *bd = bh->b_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 348 | |
| 349 | gfs2_assert_withdraw(sdp, buffer_uptodate(bh)); |
| 350 | |
| 351 | if (!buffer_pinned(bh)) |
| 352 | gfs2_assert_withdraw(sdp, 0); |
| 353 | |
| 354 | mark_buffer_dirty(bh); |
| 355 | clear_buffer_pinned(bh); |
| 356 | |
| 357 | gfs2_log_lock(sdp); |
| 358 | if (bd->bd_ail) { |
| 359 | list_del(&bd->bd_ail_st_list); |
| 360 | brelse(bh); |
| 361 | } else { |
| 362 | struct gfs2_glock *gl = bd->bd_gl; |
| 363 | list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list); |
| 364 | atomic_inc(&gl->gl_ail_count); |
| 365 | } |
| 366 | bd->bd_ail = ai; |
| 367 | list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list); |
| 368 | gfs2_log_unlock(sdp); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore |
| 373 | * @ip: the inode who owns the buffers |
| 374 | * @bstart: the first buffer in the run |
| 375 | * @blen: the number of buffers in the run |
| 376 | * |
| 377 | */ |
| 378 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 379 | void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 380 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 381 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 382 | struct buffer_head *bh; |
| 383 | |
| 384 | while (blen) { |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 385 | bh = getbuf(ip->i_gl, bstart, NO_CREATE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 386 | if (bh) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 387 | struct gfs2_bufdata *bd = bh->b_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 388 | |
| 389 | if (test_clear_buffer_pinned(bh)) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 390 | struct gfs2_trans *tr = current->journal_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 391 | gfs2_log_lock(sdp); |
| 392 | list_del_init(&bd->bd_le.le_list); |
| 393 | gfs2_assert_warn(sdp, sdp->sd_log_num_buf); |
| 394 | sdp->sd_log_num_buf--; |
| 395 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 396 | tr->tr_num_buf_rm++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 397 | brelse(bh); |
| 398 | } |
| 399 | if (bd) { |
| 400 | gfs2_log_lock(sdp); |
| 401 | if (bd->bd_ail) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 402 | u64 blkno = bh->b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 403 | bd->bd_ail = NULL; |
| 404 | list_del(&bd->bd_ail_st_list); |
| 405 | list_del(&bd->bd_ail_gl_list); |
| 406 | atomic_dec(&bd->bd_gl->gl_ail_count); |
| 407 | brelse(bh); |
| 408 | gfs2_log_unlock(sdp); |
| 409 | gfs2_trans_add_revoke(sdp, blkno); |
| 410 | } else |
| 411 | gfs2_log_unlock(sdp); |
| 412 | } |
| 413 | |
| 414 | lock_buffer(bh); |
| 415 | clear_buffer_dirty(bh); |
| 416 | clear_buffer_uptodate(bh); |
| 417 | unlock_buffer(bh); |
| 418 | |
| 419 | brelse(bh); |
| 420 | } |
| 421 | |
| 422 | bstart++; |
| 423 | blen--; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * gfs2_meta_cache_flush - get rid of any references on buffers for this inode |
| 429 | * @ip: The GFS2 inode |
| 430 | * |
| 431 | * This releases buffers that are in the most-recently-used array of |
| 432 | * blocks used for indirect block addressing for this inode. |
| 433 | */ |
| 434 | |
| 435 | void gfs2_meta_cache_flush(struct gfs2_inode *ip) |
| 436 | { |
| 437 | struct buffer_head **bh_slot; |
| 438 | unsigned int x; |
| 439 | |
| 440 | spin_lock(&ip->i_spin); |
| 441 | |
| 442 | for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) { |
| 443 | bh_slot = &ip->i_cache[x]; |
| 444 | if (!*bh_slot) |
| 445 | break; |
| 446 | brelse(*bh_slot); |
| 447 | *bh_slot = NULL; |
| 448 | } |
| 449 | |
| 450 | spin_unlock(&ip->i_spin); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * gfs2_meta_indirect_buffer - Get a metadata buffer |
| 455 | * @ip: The GFS2 inode |
| 456 | * @height: The level of this buf in the metadata (indir addr) tree (if any) |
| 457 | * @num: The block number (device relative) of the buffer |
| 458 | * @new: Non-zero if we may create a new buffer |
| 459 | * @bhp: the buffer is returned here |
| 460 | * |
| 461 | * Try to use the gfs2_inode's MRU metadata tree cache. |
| 462 | * |
| 463 | * Returns: errno |
| 464 | */ |
| 465 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 466 | int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 467 | int new, struct buffer_head **bhp) |
| 468 | { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 469 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 470 | struct gfs2_glock *gl = ip->i_gl; |
| 471 | struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height; |
| 472 | int in_cache = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 473 | |
Steven Whitehouse | b004157 | 2006-11-23 10:51:34 -0500 | [diff] [blame] | 474 | BUG_ON(!gl); |
| 475 | BUG_ON(!sdp); |
| 476 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 477 | spin_lock(&ip->i_spin); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 478 | if (*bh_slot && (*bh_slot)->b_blocknr == num) { |
| 479 | bh = *bh_slot; |
| 480 | get_bh(bh); |
| 481 | in_cache = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 482 | } |
| 483 | spin_unlock(&ip->i_spin); |
| 484 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 485 | if (!bh) |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 486 | bh = getbuf(gl, num, CREATE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 487 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 488 | if (!bh) |
| 489 | return -ENOBUFS; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 490 | |
| 491 | if (new) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 492 | if (gfs2_assert_warn(sdp, height)) |
| 493 | goto err; |
| 494 | meta_prep_new(bh); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 495 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 496 | gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN); |
| 497 | gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 498 | } else { |
| 499 | u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI; |
| 500 | if (!buffer_uptodate(bh)) { |
Steven Whitehouse | 2e565bb | 2006-10-02 11:38:25 -0400 | [diff] [blame] | 501 | ll_rw_block(READ_META, 1, &bh); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 502 | if (gfs2_meta_wait(sdp, bh)) |
| 503 | goto err; |
| 504 | } |
| 505 | if (gfs2_metatype_check(sdp, bh, mtype)) |
| 506 | goto err; |
| 507 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 508 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 509 | if (!in_cache) { |
| 510 | spin_lock(&ip->i_spin); |
| 511 | if (*bh_slot) |
| 512 | brelse(*bh_slot); |
| 513 | *bh_slot = bh; |
| 514 | get_bh(bh); |
| 515 | spin_unlock(&ip->i_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | *bhp = bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 519 | return 0; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 520 | err: |
| 521 | brelse(bh); |
| 522 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
| 526 | * gfs2_meta_ra - start readahead on an extent of a file |
| 527 | * @gl: the glock the blocks belong to |
| 528 | * @dblock: the starting disk block |
| 529 | * @extlen: the number of blocks in the extent |
| 530 | * |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 531 | * returns: the first buffer in the extent |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 532 | */ |
| 533 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 534 | struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 535 | { |
| 536 | struct gfs2_sbd *sdp = gl->gl_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 537 | struct buffer_head *first_bh, *bh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 538 | u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 539 | sdp->sd_sb.sb_bsize_shift; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 540 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 541 | BUG_ON(!extlen); |
| 542 | |
| 543 | if (max_ra < 1) |
| 544 | max_ra = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 545 | if (extlen > max_ra) |
| 546 | extlen = max_ra; |
| 547 | |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 548 | first_bh = getbuf(gl, dblock, CREATE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 549 | |
| 550 | if (buffer_uptodate(first_bh)) |
| 551 | goto out; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 552 | if (!buffer_locked(first_bh)) |
Steven Whitehouse | 2e565bb | 2006-10-02 11:38:25 -0400 | [diff] [blame] | 553 | ll_rw_block(READ_META, 1, &first_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 554 | |
| 555 | dblock++; |
| 556 | extlen--; |
| 557 | |
| 558 | while (extlen) { |
Steven Whitehouse | cb4c031 | 2006-11-23 11:16:32 -0500 | [diff] [blame] | 559 | bh = getbuf(gl, dblock, CREATE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 560 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 561 | if (!buffer_uptodate(bh) && !buffer_locked(bh)) |
| 562 | ll_rw_block(READA, 1, &bh); |
| 563 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 564 | dblock++; |
| 565 | extlen--; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 566 | if (!buffer_locked(first_bh) && buffer_uptodate(first_bh)) |
| 567 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 570 | wait_on_buffer(first_bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 571 | out: |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 572 | return first_bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 573 | } |
| 574 | |