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 | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 20 | #include <linux/gfs2_ondisk.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 21 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | |
| 23 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 24 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 25 | #include "glock.h" |
| 26 | #include "glops.h" |
| 27 | #include "inode.h" |
| 28 | #include "log.h" |
| 29 | #include "lops.h" |
| 30 | #include "meta_io.h" |
| 31 | #include "rgrp.h" |
| 32 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 33 | #include "util.h" |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 34 | #include "ops_address.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 35 | |
| 36 | #define buffer_busy(bh) \ |
| 37 | ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned))) |
| 38 | #define buffer_in_io(bh) \ |
| 39 | ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock))) |
| 40 | |
| 41 | static int aspace_get_block(struct inode *inode, sector_t lblock, |
| 42 | struct buffer_head *bh_result, int create) |
| 43 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 44 | gfs2_assert_warn(inode->i_sb->s_fs_info, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 45 | return -EOPNOTSUPP; |
| 46 | } |
| 47 | |
| 48 | static int gfs2_aspace_writepage(struct page *page, |
| 49 | struct writeback_control *wbc) |
| 50 | { |
| 51 | return block_write_full_page(page, aspace_get_block, wbc); |
| 52 | } |
| 53 | |
Steven Whitehouse | 66de045 | 2006-07-03 13:37:30 -0400 | [diff] [blame] | 54 | static const struct address_space_operations aspace_aops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 55 | .writepage = gfs2_aspace_writepage, |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 56 | .releasepage = gfs2_releasepage, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * gfs2_aspace_get - Create and initialize a struct inode structure |
| 61 | * @sdp: the filesystem the aspace is in |
| 62 | * |
| 63 | * Right now a struct inode is just a struct inode. Maybe Linux |
| 64 | * will supply a more lightweight address space construct (that works) |
| 65 | * in the future. |
| 66 | * |
| 67 | * Make sure pages/buffers in this aspace aren't in high memory. |
| 68 | * |
| 69 | * Returns: the aspace |
| 70 | */ |
| 71 | |
| 72 | struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp) |
| 73 | { |
| 74 | struct inode *aspace; |
| 75 | |
| 76 | aspace = new_inode(sdp->sd_vfs); |
| 77 | if (aspace) { |
Steven Whitehouse | f3bba03 | 2006-07-11 09:50:54 -0400 | [diff] [blame] | 78 | mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 79 | aspace->i_mapping->a_ops = &aspace_aops; |
| 80 | aspace->i_size = ~0ULL; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 81 | aspace->u.generic_ip = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 82 | insert_inode_hash(aspace); |
| 83 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 84 | return aspace; |
| 85 | } |
| 86 | |
| 87 | void gfs2_aspace_put(struct inode *aspace) |
| 88 | { |
| 89 | remove_inode_hash(aspace); |
| 90 | iput(aspace); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * gfs2_ail1_start_one - Start I/O on a part of the AIL |
| 95 | * @sdp: the filesystem |
| 96 | * @tr: the part of the AIL |
| 97 | * |
| 98 | */ |
| 99 | |
| 100 | void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 101 | { |
| 102 | struct gfs2_bufdata *bd, *s; |
| 103 | struct buffer_head *bh; |
| 104 | int retry; |
| 105 | |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 106 | BUG_ON(!spin_is_locked(&sdp->sd_log_lock)); |
| 107 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 108 | do { |
| 109 | retry = 0; |
| 110 | |
| 111 | list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, |
| 112 | bd_ail_st_list) { |
| 113 | bh = bd->bd_bh; |
| 114 | |
| 115 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 116 | |
| 117 | if (!buffer_busy(bh)) { |
Steven Whitehouse | 1b50259 | 2006-05-18 14:10:52 -0400 | [diff] [blame] | 118 | if (!buffer_uptodate(bh)) { |
| 119 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 120 | gfs2_io_error_bh(sdp, bh); |
Steven Whitehouse | 1b50259 | 2006-05-18 14:10:52 -0400 | [diff] [blame] | 121 | gfs2_log_lock(sdp); |
| 122 | } |
Steven Whitehouse | 5696553 | 2006-09-20 15:48:09 -0400 | [diff] [blame] | 123 | list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | |
| 127 | if (!buffer_dirty(bh)) |
| 128 | continue; |
| 129 | |
| 130 | list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list); |
| 131 | |
| 132 | gfs2_log_unlock(sdp); |
| 133 | wait_on_buffer(bh); |
| 134 | ll_rw_block(WRITE, 1, &bh); |
| 135 | gfs2_log_lock(sdp); |
| 136 | |
| 137 | retry = 1; |
| 138 | break; |
| 139 | } |
| 140 | } while (retry); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced |
| 145 | * @sdp: the filesystem |
| 146 | * @ai: the AIL entry |
| 147 | * |
| 148 | */ |
| 149 | |
| 150 | int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags) |
| 151 | { |
| 152 | struct gfs2_bufdata *bd, *s; |
| 153 | struct buffer_head *bh; |
| 154 | |
| 155 | list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, |
| 156 | bd_ail_st_list) { |
| 157 | bh = bd->bd_bh; |
| 158 | |
| 159 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 160 | |
| 161 | if (buffer_busy(bh)) { |
| 162 | if (flags & DIO_ALL) |
| 163 | continue; |
| 164 | else |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (!buffer_uptodate(bh)) |
| 169 | gfs2_io_error_bh(sdp, bh); |
| 170 | |
| 171 | list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); |
| 172 | } |
| 173 | |
| 174 | return list_empty(&ai->ai_ail1_list); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced |
| 179 | * @sdp: the filesystem |
| 180 | * @ai: the AIL entry |
| 181 | * |
| 182 | */ |
| 183 | |
| 184 | void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 185 | { |
| 186 | struct list_head *head = &ai->ai_ail2_list; |
| 187 | struct gfs2_bufdata *bd; |
| 188 | |
| 189 | while (!list_empty(head)) { |
| 190 | bd = list_entry(head->prev, struct gfs2_bufdata, |
| 191 | bd_ail_st_list); |
| 192 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 193 | bd->bd_ail = NULL; |
| 194 | list_del(&bd->bd_ail_st_list); |
| 195 | list_del(&bd->bd_ail_gl_list); |
| 196 | atomic_dec(&bd->bd_gl->gl_ail_count); |
| 197 | brelse(bd->bd_bh); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * ail_empty_gl - remove all buffers for a given lock from the AIL |
| 203 | * @gl: the glock |
| 204 | * |
| 205 | * None of the buffers should be dirty, locked, or pinned. |
| 206 | */ |
| 207 | |
| 208 | void gfs2_ail_empty_gl(struct gfs2_glock *gl) |
| 209 | { |
| 210 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 211 | unsigned int blocks; |
| 212 | struct list_head *head = &gl->gl_ail_list; |
| 213 | struct gfs2_bufdata *bd; |
| 214 | struct buffer_head *bh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 215 | u64 blkno; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 216 | int error; |
| 217 | |
| 218 | blocks = atomic_read(&gl->gl_ail_count); |
| 219 | if (!blocks) |
| 220 | return; |
| 221 | |
| 222 | error = gfs2_trans_begin(sdp, 0, blocks); |
| 223 | if (gfs2_assert_withdraw(sdp, !error)) |
| 224 | return; |
| 225 | |
| 226 | gfs2_log_lock(sdp); |
| 227 | while (!list_empty(head)) { |
| 228 | bd = list_entry(head->next, struct gfs2_bufdata, |
| 229 | bd_ail_gl_list); |
| 230 | bh = bd->bd_bh; |
| 231 | blkno = bh->b_blocknr; |
| 232 | gfs2_assert_withdraw(sdp, !buffer_busy(bh)); |
| 233 | |
| 234 | bd->bd_ail = NULL; |
| 235 | list_del(&bd->bd_ail_st_list); |
| 236 | list_del(&bd->bd_ail_gl_list); |
| 237 | atomic_dec(&gl->gl_ail_count); |
| 238 | brelse(bh); |
| 239 | gfs2_log_unlock(sdp); |
| 240 | |
| 241 | gfs2_trans_add_revoke(sdp, blkno); |
| 242 | |
| 243 | gfs2_log_lock(sdp); |
| 244 | } |
| 245 | gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count)); |
| 246 | gfs2_log_unlock(sdp); |
| 247 | |
| 248 | gfs2_trans_end(sdp); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 249 | gfs2_log_flush(sdp, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * gfs2_meta_inval - Invalidate all buffers associated with a glock |
| 254 | * @gl: the glock |
| 255 | * |
| 256 | */ |
| 257 | |
| 258 | void gfs2_meta_inval(struct gfs2_glock *gl) |
| 259 | { |
| 260 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 261 | struct inode *aspace = gl->gl_aspace; |
| 262 | struct address_space *mapping = gl->gl_aspace->i_mapping; |
| 263 | |
| 264 | gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count)); |
| 265 | |
| 266 | atomic_inc(&aspace->i_writecount); |
| 267 | truncate_inode_pages(mapping, 0); |
| 268 | atomic_dec(&aspace->i_writecount); |
| 269 | |
| 270 | gfs2_assert_withdraw(sdp, !mapping->nrpages); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * gfs2_meta_sync - Sync all buffers associated with a glock |
| 275 | * @gl: The glock |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 276 | * |
| 277 | */ |
| 278 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 279 | void gfs2_meta_sync(struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 280 | { |
| 281 | struct address_space *mapping = gl->gl_aspace->i_mapping; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 282 | int error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 283 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 284 | filemap_fdatawrite(mapping); |
| 285 | error = filemap_fdatawait(mapping); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 286 | |
| 287 | if (error) |
| 288 | gfs2_io_error(gl->gl_sbd); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * getbuf - Get a buffer with a given address space |
| 293 | * @sdp: the filesystem |
| 294 | * @aspace: the address space |
| 295 | * @blkno: the block number (filesystem scope) |
| 296 | * @create: 1 if the buffer should be created |
| 297 | * |
| 298 | * Returns: the buffer |
| 299 | */ |
| 300 | |
| 301 | static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 302 | u64 blkno, int create) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 303 | { |
| 304 | struct page *page; |
| 305 | struct buffer_head *bh; |
| 306 | unsigned int shift; |
| 307 | unsigned long index; |
| 308 | unsigned int bufnum; |
| 309 | |
| 310 | shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift; |
| 311 | index = blkno >> shift; /* convert block to page */ |
| 312 | bufnum = blkno - (index << shift); /* block buf index within page */ |
| 313 | |
| 314 | if (create) { |
| 315 | for (;;) { |
| 316 | page = grab_cache_page(aspace->i_mapping, index); |
| 317 | if (page) |
| 318 | break; |
| 319 | yield(); |
| 320 | } |
| 321 | } else { |
| 322 | page = find_lock_page(aspace->i_mapping, index); |
| 323 | if (!page) |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | if (!page_has_buffers(page)) |
| 328 | create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0); |
| 329 | |
| 330 | /* Locate header for our buffer within our page */ |
| 331 | for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page) |
| 332 | /* Do nothing */; |
| 333 | get_bh(bh); |
| 334 | |
| 335 | if (!buffer_mapped(bh)) |
| 336 | map_bh(bh, sdp->sd_vfs, blkno); |
| 337 | |
| 338 | unlock_page(page); |
| 339 | mark_page_accessed(page); |
| 340 | page_cache_release(page); |
| 341 | |
| 342 | return bh; |
| 343 | } |
| 344 | |
| 345 | static void meta_prep_new(struct buffer_head *bh) |
| 346 | { |
| 347 | struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data; |
| 348 | |
| 349 | lock_buffer(bh); |
| 350 | clear_buffer_dirty(bh); |
| 351 | set_buffer_uptodate(bh); |
| 352 | unlock_buffer(bh); |
| 353 | |
| 354 | mh->mh_magic = cpu_to_be32(GFS2_MAGIC); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * gfs2_meta_new - Get a block |
| 359 | * @gl: The glock associated with this block |
| 360 | * @blkno: The block number |
| 361 | * |
| 362 | * Returns: The buffer |
| 363 | */ |
| 364 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 365 | struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 366 | { |
| 367 | struct buffer_head *bh; |
| 368 | bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE); |
| 369 | meta_prep_new(bh); |
| 370 | return bh; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * gfs2_meta_read - Read a block from disk |
| 375 | * @gl: The glock covering the block |
| 376 | * @blkno: The block number |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 377 | * @flags: flags |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 378 | * @bhp: the place where the buffer is returned (NULL on failure) |
| 379 | * |
| 380 | * Returns: errno |
| 381 | */ |
| 382 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 383 | int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 384 | struct buffer_head **bhp) |
| 385 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 386 | *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 387 | if (!buffer_uptodate(*bhp)) |
| 388 | ll_rw_block(READ, 1, bhp); |
| 389 | if (flags & DIO_WAIT) { |
| 390 | int error = gfs2_meta_wait(gl->gl_sbd, *bhp); |
| 391 | if (error) { |
| 392 | brelse(*bhp); |
| 393 | return error; |
| 394 | } |
| 395 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 396 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 397 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 401 | * gfs2_meta_wait - Reread a block from disk |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 402 | * @sdp: the filesystem |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 403 | * @bh: The block to wait for |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 404 | * |
| 405 | * Returns: errno |
| 406 | */ |
| 407 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 408 | int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 409 | { |
| 410 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 411 | return -EIO; |
| 412 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 413 | wait_on_buffer(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 414 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 415 | if (!buffer_uptodate(bh)) { |
| 416 | struct gfs2_trans *tr = current->journal_info; |
| 417 | if (tr && tr->tr_touched) |
| 418 | gfs2_io_error_bh(sdp, bh); |
| 419 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 420 | } |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 421 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 422 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /** |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 428 | * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 429 | * @gl: the glock the buffer belongs to |
| 430 | * @bh: The buffer to be attached to |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 431 | * @meta: Flag to indicate whether its metadata or not |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 432 | */ |
| 433 | |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 434 | void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh, |
| 435 | int meta) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 436 | { |
| 437 | struct gfs2_bufdata *bd; |
| 438 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 439 | if (meta) |
| 440 | lock_page(bh->b_page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 441 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 442 | if (bh->b_private) { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 443 | if (meta) |
| 444 | unlock_page(bh->b_page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 445 | return; |
| 446 | } |
| 447 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 448 | bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 449 | memset(bd, 0, sizeof(struct gfs2_bufdata)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 450 | bd->bd_bh = bh; |
| 451 | bd->bd_gl = gl; |
| 452 | |
| 453 | INIT_LIST_HEAD(&bd->bd_list_tr); |
Steven Whitehouse | 82ffa51 | 2006-09-04 14:47:06 -0400 | [diff] [blame] | 454 | if (meta) |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 455 | lops_init_le(&bd->bd_le, &gfs2_buf_lops); |
Steven Whitehouse | 82ffa51 | 2006-09-04 14:47:06 -0400 | [diff] [blame] | 456 | else |
Steven Whitehouse | 586dfda | 2006-01-18 11:32:00 +0000 | [diff] [blame] | 457 | lops_init_le(&bd->bd_le, &gfs2_databuf_lops); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 458 | bh->b_private = bd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 459 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 460 | if (meta) |
| 461 | unlock_page(bh->b_page); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /** |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 465 | * gfs2_pin - Pin a buffer in memory |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 466 | * @sdp: the filesystem the buffer belongs to |
| 467 | * @bh: The buffer to be pinned |
| 468 | * |
| 469 | */ |
| 470 | |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 471 | void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 472 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 473 | struct gfs2_bufdata *bd = bh->b_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 474 | |
| 475 | gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)); |
| 476 | |
| 477 | if (test_set_buffer_pinned(bh)) |
| 478 | gfs2_assert_withdraw(sdp, 0); |
| 479 | |
| 480 | wait_on_buffer(bh); |
| 481 | |
| 482 | /* If this buffer is in the AIL and it has already been written |
| 483 | to in-place disk block, remove it from the AIL. */ |
| 484 | |
| 485 | gfs2_log_lock(sdp); |
| 486 | if (bd->bd_ail && !buffer_in_io(bh)) |
| 487 | list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list); |
| 488 | gfs2_log_unlock(sdp); |
| 489 | |
| 490 | clear_buffer_dirty(bh); |
| 491 | wait_on_buffer(bh); |
| 492 | |
| 493 | if (!buffer_uptodate(bh)) |
| 494 | gfs2_io_error_bh(sdp, bh); |
| 495 | |
| 496 | get_bh(bh); |
| 497 | } |
| 498 | |
| 499 | /** |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 500 | * gfs2_unpin - Unpin a buffer |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 501 | * @sdp: the filesystem the buffer belongs to |
| 502 | * @bh: The buffer to unpin |
| 503 | * @ai: |
| 504 | * |
| 505 | */ |
| 506 | |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 507 | void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, |
| 508 | struct gfs2_ail *ai) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 509 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 510 | struct gfs2_bufdata *bd = bh->b_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 511 | |
| 512 | gfs2_assert_withdraw(sdp, buffer_uptodate(bh)); |
| 513 | |
| 514 | if (!buffer_pinned(bh)) |
| 515 | gfs2_assert_withdraw(sdp, 0); |
| 516 | |
| 517 | mark_buffer_dirty(bh); |
| 518 | clear_buffer_pinned(bh); |
| 519 | |
| 520 | gfs2_log_lock(sdp); |
| 521 | if (bd->bd_ail) { |
| 522 | list_del(&bd->bd_ail_st_list); |
| 523 | brelse(bh); |
| 524 | } else { |
| 525 | struct gfs2_glock *gl = bd->bd_gl; |
| 526 | list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list); |
| 527 | atomic_inc(&gl->gl_ail_count); |
| 528 | } |
| 529 | bd->bd_ail = ai; |
| 530 | list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list); |
| 531 | gfs2_log_unlock(sdp); |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore |
| 536 | * @ip: the inode who owns the buffers |
| 537 | * @bstart: the first buffer in the run |
| 538 | * @blen: the number of buffers in the run |
| 539 | * |
| 540 | */ |
| 541 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 542 | void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 543 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 544 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 545 | struct inode *aspace = ip->i_gl->gl_aspace; |
| 546 | struct buffer_head *bh; |
| 547 | |
| 548 | while (blen) { |
| 549 | bh = getbuf(sdp, aspace, bstart, NO_CREATE); |
| 550 | if (bh) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 551 | struct gfs2_bufdata *bd = bh->b_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 552 | |
| 553 | if (test_clear_buffer_pinned(bh)) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 554 | struct gfs2_trans *tr = current->journal_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 555 | gfs2_log_lock(sdp); |
| 556 | list_del_init(&bd->bd_le.le_list); |
| 557 | gfs2_assert_warn(sdp, sdp->sd_log_num_buf); |
| 558 | sdp->sd_log_num_buf--; |
| 559 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 560 | tr->tr_num_buf_rm++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 561 | brelse(bh); |
| 562 | } |
| 563 | if (bd) { |
| 564 | gfs2_log_lock(sdp); |
| 565 | if (bd->bd_ail) { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 566 | u64 blkno = bh->b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 567 | bd->bd_ail = NULL; |
| 568 | list_del(&bd->bd_ail_st_list); |
| 569 | list_del(&bd->bd_ail_gl_list); |
| 570 | atomic_dec(&bd->bd_gl->gl_ail_count); |
| 571 | brelse(bh); |
| 572 | gfs2_log_unlock(sdp); |
| 573 | gfs2_trans_add_revoke(sdp, blkno); |
| 574 | } else |
| 575 | gfs2_log_unlock(sdp); |
| 576 | } |
| 577 | |
| 578 | lock_buffer(bh); |
| 579 | clear_buffer_dirty(bh); |
| 580 | clear_buffer_uptodate(bh); |
| 581 | unlock_buffer(bh); |
| 582 | |
| 583 | brelse(bh); |
| 584 | } |
| 585 | |
| 586 | bstart++; |
| 587 | blen--; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * gfs2_meta_cache_flush - get rid of any references on buffers for this inode |
| 593 | * @ip: The GFS2 inode |
| 594 | * |
| 595 | * This releases buffers that are in the most-recently-used array of |
| 596 | * blocks used for indirect block addressing for this inode. |
| 597 | */ |
| 598 | |
| 599 | void gfs2_meta_cache_flush(struct gfs2_inode *ip) |
| 600 | { |
| 601 | struct buffer_head **bh_slot; |
| 602 | unsigned int x; |
| 603 | |
| 604 | spin_lock(&ip->i_spin); |
| 605 | |
| 606 | for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) { |
| 607 | bh_slot = &ip->i_cache[x]; |
| 608 | if (!*bh_slot) |
| 609 | break; |
| 610 | brelse(*bh_slot); |
| 611 | *bh_slot = NULL; |
| 612 | } |
| 613 | |
| 614 | spin_unlock(&ip->i_spin); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * gfs2_meta_indirect_buffer - Get a metadata buffer |
| 619 | * @ip: The GFS2 inode |
| 620 | * @height: The level of this buf in the metadata (indir addr) tree (if any) |
| 621 | * @num: The block number (device relative) of the buffer |
| 622 | * @new: Non-zero if we may create a new buffer |
| 623 | * @bhp: the buffer is returned here |
| 624 | * |
| 625 | * Try to use the gfs2_inode's MRU metadata tree cache. |
| 626 | * |
| 627 | * Returns: errno |
| 628 | */ |
| 629 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 630 | 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] | 631 | int new, struct buffer_head **bhp) |
| 632 | { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 633 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 634 | struct gfs2_glock *gl = ip->i_gl; |
| 635 | struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height; |
| 636 | int in_cache = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 637 | |
| 638 | spin_lock(&ip->i_spin); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 639 | if (*bh_slot && (*bh_slot)->b_blocknr == num) { |
| 640 | bh = *bh_slot; |
| 641 | get_bh(bh); |
| 642 | in_cache = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 643 | } |
| 644 | spin_unlock(&ip->i_spin); |
| 645 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 646 | if (!bh) |
| 647 | bh = getbuf(gl->gl_sbd, gl->gl_aspace, num, CREATE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 648 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 649 | if (!bh) |
| 650 | return -ENOBUFS; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 651 | |
| 652 | if (new) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 653 | if (gfs2_assert_warn(sdp, height)) |
| 654 | goto err; |
| 655 | meta_prep_new(bh); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 656 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 657 | gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN); |
| 658 | gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 659 | } else { |
| 660 | u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI; |
| 661 | if (!buffer_uptodate(bh)) { |
| 662 | ll_rw_block(READ, 1, &bh); |
| 663 | if (gfs2_meta_wait(sdp, bh)) |
| 664 | goto err; |
| 665 | } |
| 666 | if (gfs2_metatype_check(sdp, bh, mtype)) |
| 667 | goto err; |
| 668 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 669 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 670 | if (!in_cache) { |
| 671 | spin_lock(&ip->i_spin); |
| 672 | if (*bh_slot) |
| 673 | brelse(*bh_slot); |
| 674 | *bh_slot = bh; |
| 675 | get_bh(bh); |
| 676 | spin_unlock(&ip->i_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | *bhp = bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 680 | return 0; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 681 | err: |
| 682 | brelse(bh); |
| 683 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /** |
| 687 | * gfs2_meta_ra - start readahead on an extent of a file |
| 688 | * @gl: the glock the blocks belong to |
| 689 | * @dblock: the starting disk block |
| 690 | * @extlen: the number of blocks in the extent |
| 691 | * |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 692 | * returns: the first buffer in the extent |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 693 | */ |
| 694 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 695 | 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] | 696 | { |
| 697 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 698 | struct inode *aspace = gl->gl_aspace; |
| 699 | struct buffer_head *first_bh, *bh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 700 | u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 701 | sdp->sd_sb.sb_bsize_shift; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 702 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 703 | BUG_ON(!extlen); |
| 704 | |
| 705 | if (max_ra < 1) |
| 706 | max_ra = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 707 | if (extlen > max_ra) |
| 708 | extlen = max_ra; |
| 709 | |
| 710 | first_bh = getbuf(sdp, aspace, dblock, CREATE); |
| 711 | |
| 712 | if (buffer_uptodate(first_bh)) |
| 713 | goto out; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 714 | if (!buffer_locked(first_bh)) |
| 715 | ll_rw_block(READ, 1, &first_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 716 | |
| 717 | dblock++; |
| 718 | extlen--; |
| 719 | |
| 720 | while (extlen) { |
| 721 | bh = getbuf(sdp, aspace, dblock, CREATE); |
| 722 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 723 | if (!buffer_uptodate(bh) && !buffer_locked(bh)) |
| 724 | ll_rw_block(READA, 1, &bh); |
| 725 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 726 | dblock++; |
| 727 | extlen--; |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 728 | if (!buffer_locked(first_bh) && buffer_uptodate(first_bh)) |
| 729 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 732 | wait_on_buffer(first_bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 733 | out: |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame^] | 734 | return first_bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /** |
| 738 | * gfs2_meta_syncfs - sync all the buffers in a filesystem |
| 739 | * @sdp: the filesystem |
| 740 | * |
| 741 | */ |
| 742 | |
| 743 | void gfs2_meta_syncfs(struct gfs2_sbd *sdp) |
| 744 | { |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 745 | gfs2_log_flush(sdp, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 746 | for (;;) { |
| 747 | gfs2_ail1_start(sdp, DIO_ALL); |
| 748 | if (gfs2_ail1_empty(sdp, DIO_ALL)) |
| 749 | break; |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 750 | msleep(10); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |