David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
| 7 | * of the GNU General Public License v.2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/writeback.h> |
| 18 | #include <linux/swap.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <asm/semaphore.h> |
| 21 | |
| 22 | #include "gfs2.h" |
| 23 | #include "glock.h" |
| 24 | #include "glops.h" |
| 25 | #include "inode.h" |
| 26 | #include "log.h" |
| 27 | #include "lops.h" |
| 28 | #include "meta_io.h" |
| 29 | #include "rgrp.h" |
| 30 | #include "trans.h" |
| 31 | |
| 32 | #define buffer_busy(bh) \ |
| 33 | ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned))) |
| 34 | #define buffer_in_io(bh) \ |
| 35 | ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock))) |
| 36 | |
| 37 | static int aspace_get_block(struct inode *inode, sector_t lblock, |
| 38 | struct buffer_head *bh_result, int create) |
| 39 | { |
| 40 | gfs2_assert_warn(get_v2sdp(inode->i_sb), 0); |
| 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 | |
| 50 | /** |
| 51 | * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out. |
| 52 | * @bh: the buffer we're stuck on |
| 53 | * |
| 54 | */ |
| 55 | |
| 56 | static void stuck_releasepage(struct buffer_head *bh) |
| 57 | { |
| 58 | struct gfs2_sbd *sdp = get_v2sdp(bh->b_page->mapping->host->i_sb); |
| 59 | struct gfs2_bufdata *bd = get_v2bd(bh); |
| 60 | struct gfs2_glock *gl; |
| 61 | |
| 62 | fs_warn(sdp, "stuck in gfs2_releasepage()\n"); |
| 63 | fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n", |
| 64 | (uint64_t)bh->b_blocknr, atomic_read(&bh->b_count)); |
| 65 | fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh)); |
| 66 | fs_warn(sdp, "get_v2bd(bh) = %s\n", (bd) ? "!NULL" : "NULL"); |
| 67 | |
| 68 | if (!bd) |
| 69 | return; |
| 70 | |
| 71 | gl = bd->bd_gl; |
| 72 | |
| 73 | fs_warn(sdp, "gl = (%u, %llu)\n", |
| 74 | gl->gl_name.ln_type, gl->gl_name.ln_number); |
| 75 | |
| 76 | fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n", |
| 77 | (list_empty(&bd->bd_list_tr)) ? "no" : "yes", |
| 78 | (list_empty(&bd->bd_le.le_list)) ? "no" : "yes"); |
| 79 | |
| 80 | if (gl->gl_ops == &gfs2_inode_glops) { |
| 81 | struct gfs2_inode *ip = get_gl2ip(gl); |
| 82 | unsigned int x; |
| 83 | |
| 84 | if (!ip) |
| 85 | return; |
| 86 | |
| 87 | fs_warn(sdp, "ip = %llu %llu\n", |
| 88 | ip->i_num.no_formal_ino, ip->i_num.no_addr); |
| 89 | fs_warn(sdp, "ip->i_count = %d, ip->i_vnode = %s\n", |
| 90 | atomic_read(&ip->i_count), |
| 91 | (ip->i_vnode) ? "!NULL" : "NULL"); |
| 92 | |
| 93 | for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) |
| 94 | fs_warn(sdp, "ip->i_cache[%u] = %s\n", |
| 95 | x, (ip->i_cache[x]) ? "!NULL" : "NULL"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * gfs2_aspace_releasepage - free the metadata associated with a page |
| 101 | * @page: the page that's being released |
| 102 | * @gfp_mask: passed from Linux VFS, ignored by us |
| 103 | * |
| 104 | * Call try_to_free_buffers() if the buffers in this page can be |
| 105 | * released. |
| 106 | * |
| 107 | * Returns: 0 |
| 108 | */ |
| 109 | |
| 110 | static int gfs2_aspace_releasepage(struct page *page, gfp_t gfp_mask) |
| 111 | { |
| 112 | struct inode *aspace = page->mapping->host; |
| 113 | struct gfs2_sbd *sdp = get_v2sdp(aspace->i_sb); |
| 114 | struct buffer_head *bh, *head; |
| 115 | struct gfs2_bufdata *bd; |
| 116 | unsigned long t; |
| 117 | |
| 118 | if (!page_has_buffers(page)) |
| 119 | goto out; |
| 120 | |
| 121 | head = bh = page_buffers(page); |
| 122 | do { |
| 123 | t = jiffies; |
| 124 | |
| 125 | while (atomic_read(&bh->b_count)) { |
| 126 | if (atomic_read(&aspace->i_writecount)) { |
| 127 | if (time_after_eq(jiffies, t + |
| 128 | gfs2_tune_get(sdp, gt_stall_secs) * HZ)) { |
| 129 | stuck_releasepage(bh); |
| 130 | t = jiffies; |
| 131 | } |
| 132 | |
| 133 | yield(); |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | gfs2_assert_warn(sdp, !buffer_pinned(bh)); |
| 141 | |
| 142 | bd = get_v2bd(bh); |
| 143 | if (bd) { |
| 144 | gfs2_assert_warn(sdp, bd->bd_bh == bh); |
| 145 | gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr)); |
| 146 | gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list)); |
| 147 | gfs2_assert_warn(sdp, !bd->bd_ail); |
| 148 | kmem_cache_free(gfs2_bufdata_cachep, bd); |
| 149 | atomic_dec(&sdp->sd_bufdata_count); |
| 150 | set_v2bd(bh, NULL); |
| 151 | } |
| 152 | |
| 153 | bh = bh->b_this_page; |
| 154 | } |
| 155 | while (bh != head); |
| 156 | |
| 157 | out: |
| 158 | return try_to_free_buffers(page); |
| 159 | } |
| 160 | |
| 161 | static struct address_space_operations aspace_aops = { |
| 162 | .writepage = gfs2_aspace_writepage, |
| 163 | .releasepage = gfs2_aspace_releasepage, |
| 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * gfs2_aspace_get - Create and initialize a struct inode structure |
| 168 | * @sdp: the filesystem the aspace is in |
| 169 | * |
| 170 | * Right now a struct inode is just a struct inode. Maybe Linux |
| 171 | * will supply a more lightweight address space construct (that works) |
| 172 | * in the future. |
| 173 | * |
| 174 | * Make sure pages/buffers in this aspace aren't in high memory. |
| 175 | * |
| 176 | * Returns: the aspace |
| 177 | */ |
| 178 | |
| 179 | struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp) |
| 180 | { |
| 181 | struct inode *aspace; |
| 182 | |
| 183 | aspace = new_inode(sdp->sd_vfs); |
| 184 | if (aspace) { |
| 185 | mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL); |
| 186 | aspace->i_mapping->a_ops = &aspace_aops; |
| 187 | aspace->i_size = ~0ULL; |
| 188 | set_v2ip(aspace, NULL); |
| 189 | insert_inode_hash(aspace); |
| 190 | } |
| 191 | |
| 192 | return aspace; |
| 193 | } |
| 194 | |
| 195 | void gfs2_aspace_put(struct inode *aspace) |
| 196 | { |
| 197 | remove_inode_hash(aspace); |
| 198 | iput(aspace); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * gfs2_ail1_start_one - Start I/O on a part of the AIL |
| 203 | * @sdp: the filesystem |
| 204 | * @tr: the part of the AIL |
| 205 | * |
| 206 | */ |
| 207 | |
| 208 | void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 209 | { |
| 210 | struct gfs2_bufdata *bd, *s; |
| 211 | struct buffer_head *bh; |
| 212 | int retry; |
| 213 | |
| 214 | do { |
| 215 | retry = 0; |
| 216 | |
| 217 | list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, |
| 218 | bd_ail_st_list) { |
| 219 | bh = bd->bd_bh; |
| 220 | |
| 221 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 222 | |
| 223 | if (!buffer_busy(bh)) { |
| 224 | if (!buffer_uptodate(bh)) |
| 225 | gfs2_io_error_bh(sdp, bh); |
| 226 | list_move(&bd->bd_ail_st_list, |
| 227 | &ai->ai_ail2_list); |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | if (!buffer_dirty(bh)) |
| 232 | continue; |
| 233 | |
| 234 | list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list); |
| 235 | |
| 236 | gfs2_log_unlock(sdp); |
| 237 | wait_on_buffer(bh); |
| 238 | ll_rw_block(WRITE, 1, &bh); |
| 239 | gfs2_log_lock(sdp); |
| 240 | |
| 241 | retry = 1; |
| 242 | break; |
| 243 | } |
| 244 | } while (retry); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced |
| 249 | * @sdp: the filesystem |
| 250 | * @ai: the AIL entry |
| 251 | * |
| 252 | */ |
| 253 | |
| 254 | int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags) |
| 255 | { |
| 256 | struct gfs2_bufdata *bd, *s; |
| 257 | struct buffer_head *bh; |
| 258 | |
| 259 | list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, |
| 260 | bd_ail_st_list) { |
| 261 | bh = bd->bd_bh; |
| 262 | |
| 263 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 264 | |
| 265 | if (buffer_busy(bh)) { |
| 266 | if (flags & DIO_ALL) |
| 267 | continue; |
| 268 | else |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | if (!buffer_uptodate(bh)) |
| 273 | gfs2_io_error_bh(sdp, bh); |
| 274 | |
| 275 | list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); |
| 276 | } |
| 277 | |
| 278 | return list_empty(&ai->ai_ail1_list); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced |
| 283 | * @sdp: the filesystem |
| 284 | * @ai: the AIL entry |
| 285 | * |
| 286 | */ |
| 287 | |
| 288 | void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 289 | { |
| 290 | struct list_head *head = &ai->ai_ail2_list; |
| 291 | struct gfs2_bufdata *bd; |
| 292 | |
| 293 | while (!list_empty(head)) { |
| 294 | bd = list_entry(head->prev, struct gfs2_bufdata, |
| 295 | bd_ail_st_list); |
| 296 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 297 | bd->bd_ail = NULL; |
| 298 | list_del(&bd->bd_ail_st_list); |
| 299 | list_del(&bd->bd_ail_gl_list); |
| 300 | atomic_dec(&bd->bd_gl->gl_ail_count); |
| 301 | brelse(bd->bd_bh); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * ail_empty_gl - remove all buffers for a given lock from the AIL |
| 307 | * @gl: the glock |
| 308 | * |
| 309 | * None of the buffers should be dirty, locked, or pinned. |
| 310 | */ |
| 311 | |
| 312 | void gfs2_ail_empty_gl(struct gfs2_glock *gl) |
| 313 | { |
| 314 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 315 | unsigned int blocks; |
| 316 | struct list_head *head = &gl->gl_ail_list; |
| 317 | struct gfs2_bufdata *bd; |
| 318 | struct buffer_head *bh; |
| 319 | uint64_t blkno; |
| 320 | int error; |
| 321 | |
| 322 | blocks = atomic_read(&gl->gl_ail_count); |
| 323 | if (!blocks) |
| 324 | return; |
| 325 | |
| 326 | error = gfs2_trans_begin(sdp, 0, blocks); |
| 327 | if (gfs2_assert_withdraw(sdp, !error)) |
| 328 | return; |
| 329 | |
| 330 | gfs2_log_lock(sdp); |
| 331 | while (!list_empty(head)) { |
| 332 | bd = list_entry(head->next, struct gfs2_bufdata, |
| 333 | bd_ail_gl_list); |
| 334 | bh = bd->bd_bh; |
| 335 | blkno = bh->b_blocknr; |
| 336 | gfs2_assert_withdraw(sdp, !buffer_busy(bh)); |
| 337 | |
| 338 | bd->bd_ail = NULL; |
| 339 | list_del(&bd->bd_ail_st_list); |
| 340 | list_del(&bd->bd_ail_gl_list); |
| 341 | atomic_dec(&gl->gl_ail_count); |
| 342 | brelse(bh); |
| 343 | gfs2_log_unlock(sdp); |
| 344 | |
| 345 | gfs2_trans_add_revoke(sdp, blkno); |
| 346 | |
| 347 | gfs2_log_lock(sdp); |
| 348 | } |
| 349 | gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count)); |
| 350 | gfs2_log_unlock(sdp); |
| 351 | |
| 352 | gfs2_trans_end(sdp); |
| 353 | gfs2_log_flush(sdp); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * gfs2_meta_inval - Invalidate all buffers associated with a glock |
| 358 | * @gl: the glock |
| 359 | * |
| 360 | */ |
| 361 | |
| 362 | void gfs2_meta_inval(struct gfs2_glock *gl) |
| 363 | { |
| 364 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 365 | struct inode *aspace = gl->gl_aspace; |
| 366 | struct address_space *mapping = gl->gl_aspace->i_mapping; |
| 367 | |
| 368 | gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count)); |
| 369 | |
| 370 | atomic_inc(&aspace->i_writecount); |
| 371 | truncate_inode_pages(mapping, 0); |
| 372 | atomic_dec(&aspace->i_writecount); |
| 373 | |
| 374 | gfs2_assert_withdraw(sdp, !mapping->nrpages); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * gfs2_meta_sync - Sync all buffers associated with a glock |
| 379 | * @gl: The glock |
| 380 | * @flags: DIO_START | DIO_WAIT |
| 381 | * |
| 382 | */ |
| 383 | |
| 384 | void gfs2_meta_sync(struct gfs2_glock *gl, int flags) |
| 385 | { |
| 386 | struct address_space *mapping = gl->gl_aspace->i_mapping; |
| 387 | int error = 0; |
| 388 | |
| 389 | if (flags & DIO_START) |
| 390 | filemap_fdatawrite(mapping); |
| 391 | if (!error && (flags & DIO_WAIT)) |
| 392 | error = filemap_fdatawait(mapping); |
| 393 | |
| 394 | if (error) |
| 395 | gfs2_io_error(gl->gl_sbd); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * getbuf - Get a buffer with a given address space |
| 400 | * @sdp: the filesystem |
| 401 | * @aspace: the address space |
| 402 | * @blkno: the block number (filesystem scope) |
| 403 | * @create: 1 if the buffer should be created |
| 404 | * |
| 405 | * Returns: the buffer |
| 406 | */ |
| 407 | |
| 408 | static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace, |
| 409 | uint64_t blkno, int create) |
| 410 | { |
| 411 | struct page *page; |
| 412 | struct buffer_head *bh; |
| 413 | unsigned int shift; |
| 414 | unsigned long index; |
| 415 | unsigned int bufnum; |
| 416 | |
| 417 | shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift; |
| 418 | index = blkno >> shift; /* convert block to page */ |
| 419 | bufnum = blkno - (index << shift); /* block buf index within page */ |
| 420 | |
| 421 | if (create) { |
| 422 | for (;;) { |
| 423 | page = grab_cache_page(aspace->i_mapping, index); |
| 424 | if (page) |
| 425 | break; |
| 426 | yield(); |
| 427 | } |
| 428 | } else { |
| 429 | page = find_lock_page(aspace->i_mapping, index); |
| 430 | if (!page) |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | if (!page_has_buffers(page)) |
| 435 | create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0); |
| 436 | |
| 437 | /* Locate header for our buffer within our page */ |
| 438 | for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page) |
| 439 | /* Do nothing */; |
| 440 | get_bh(bh); |
| 441 | |
| 442 | if (!buffer_mapped(bh)) |
| 443 | map_bh(bh, sdp->sd_vfs, blkno); |
| 444 | |
| 445 | unlock_page(page); |
| 446 | mark_page_accessed(page); |
| 447 | page_cache_release(page); |
| 448 | |
| 449 | return bh; |
| 450 | } |
| 451 | |
| 452 | static void meta_prep_new(struct buffer_head *bh) |
| 453 | { |
| 454 | struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data; |
| 455 | |
| 456 | lock_buffer(bh); |
| 457 | clear_buffer_dirty(bh); |
| 458 | set_buffer_uptodate(bh); |
| 459 | unlock_buffer(bh); |
| 460 | |
| 461 | mh->mh_magic = cpu_to_be32(GFS2_MAGIC); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * gfs2_meta_new - Get a block |
| 466 | * @gl: The glock associated with this block |
| 467 | * @blkno: The block number |
| 468 | * |
| 469 | * Returns: The buffer |
| 470 | */ |
| 471 | |
| 472 | struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno) |
| 473 | { |
| 474 | struct buffer_head *bh; |
| 475 | bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE); |
| 476 | meta_prep_new(bh); |
| 477 | return bh; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * gfs2_meta_read - Read a block from disk |
| 482 | * @gl: The glock covering the block |
| 483 | * @blkno: The block number |
| 484 | * @flags: flags to gfs2_dreread() |
| 485 | * @bhp: the place where the buffer is returned (NULL on failure) |
| 486 | * |
| 487 | * Returns: errno |
| 488 | */ |
| 489 | |
| 490 | int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags, |
| 491 | struct buffer_head **bhp) |
| 492 | { |
| 493 | int error; |
| 494 | |
| 495 | *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE); |
| 496 | error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags); |
| 497 | if (error) |
| 498 | brelse(*bhp); |
| 499 | |
| 500 | return error; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * gfs2_meta_reread - Reread a block from disk |
| 505 | * @sdp: the filesystem |
| 506 | * @bh: The block to read |
| 507 | * @flags: Flags that control the read |
| 508 | * |
| 509 | * Returns: errno |
| 510 | */ |
| 511 | |
| 512 | int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags) |
| 513 | { |
| 514 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 515 | return -EIO; |
| 516 | |
| 517 | if (flags & DIO_FORCE) |
| 518 | clear_buffer_uptodate(bh); |
| 519 | |
| 520 | if ((flags & DIO_START) && !buffer_uptodate(bh)) |
| 521 | ll_rw_block(READ, 1, &bh); |
| 522 | |
| 523 | if (flags & DIO_WAIT) { |
| 524 | wait_on_buffer(bh); |
| 525 | |
| 526 | if (!buffer_uptodate(bh)) { |
| 527 | struct gfs2_trans *tr = get_transaction; |
| 528 | if (tr && tr->tr_touched) |
| 529 | gfs2_io_error_bh(sdp, bh); |
| 530 | return -EIO; |
| 531 | } |
| 532 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 533 | return -EIO; |
| 534 | } |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * gfs2_meta_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer |
| 541 | * @gl: the glock the buffer belongs to |
| 542 | * @bh: The buffer to be attached to |
| 543 | * |
| 544 | */ |
| 545 | |
| 546 | void gfs2_meta_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh) |
| 547 | { |
| 548 | struct gfs2_bufdata *bd; |
| 549 | |
| 550 | lock_page(bh->b_page); |
| 551 | |
| 552 | if (get_v2bd(bh)) { |
| 553 | unlock_page(bh->b_page); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_KERNEL | __GFP_NOFAIL), |
| 558 | atomic_inc(&gl->gl_sbd->sd_bufdata_count); |
| 559 | |
| 560 | memset(bd, 0, sizeof(struct gfs2_bufdata)); |
| 561 | |
| 562 | bd->bd_bh = bh; |
| 563 | bd->bd_gl = gl; |
| 564 | |
| 565 | INIT_LIST_HEAD(&bd->bd_list_tr); |
| 566 | lops_init_le(&bd->bd_le, &gfs2_buf_lops); |
| 567 | |
| 568 | set_v2bd(bh, bd); |
| 569 | |
| 570 | unlock_page(bh->b_page); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * gfs2_meta_pin - Pin a metadata buffer in memory |
| 575 | * @sdp: the filesystem the buffer belongs to |
| 576 | * @bh: The buffer to be pinned |
| 577 | * |
| 578 | */ |
| 579 | |
| 580 | void gfs2_meta_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) |
| 581 | { |
| 582 | struct gfs2_bufdata *bd = get_v2bd(bh); |
| 583 | |
| 584 | gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)); |
| 585 | |
| 586 | if (test_set_buffer_pinned(bh)) |
| 587 | gfs2_assert_withdraw(sdp, 0); |
| 588 | |
| 589 | wait_on_buffer(bh); |
| 590 | |
| 591 | /* If this buffer is in the AIL and it has already been written |
| 592 | to in-place disk block, remove it from the AIL. */ |
| 593 | |
| 594 | gfs2_log_lock(sdp); |
| 595 | if (bd->bd_ail && !buffer_in_io(bh)) |
| 596 | list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list); |
| 597 | gfs2_log_unlock(sdp); |
| 598 | |
| 599 | clear_buffer_dirty(bh); |
| 600 | wait_on_buffer(bh); |
| 601 | |
| 602 | if (!buffer_uptodate(bh)) |
| 603 | gfs2_io_error_bh(sdp, bh); |
| 604 | |
| 605 | get_bh(bh); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * gfs2_meta_unpin - Unpin a buffer |
| 610 | * @sdp: the filesystem the buffer belongs to |
| 611 | * @bh: The buffer to unpin |
| 612 | * @ai: |
| 613 | * |
| 614 | */ |
| 615 | |
| 616 | void gfs2_meta_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, |
| 617 | struct gfs2_ail *ai) |
| 618 | { |
| 619 | struct gfs2_bufdata *bd = get_v2bd(bh); |
| 620 | |
| 621 | gfs2_assert_withdraw(sdp, buffer_uptodate(bh)); |
| 622 | |
| 623 | if (!buffer_pinned(bh)) |
| 624 | gfs2_assert_withdraw(sdp, 0); |
| 625 | |
| 626 | mark_buffer_dirty(bh); |
| 627 | clear_buffer_pinned(bh); |
| 628 | |
| 629 | gfs2_log_lock(sdp); |
| 630 | if (bd->bd_ail) { |
| 631 | list_del(&bd->bd_ail_st_list); |
| 632 | brelse(bh); |
| 633 | } else { |
| 634 | struct gfs2_glock *gl = bd->bd_gl; |
| 635 | list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list); |
| 636 | atomic_inc(&gl->gl_ail_count); |
| 637 | } |
| 638 | bd->bd_ail = ai; |
| 639 | list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list); |
| 640 | gfs2_log_unlock(sdp); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore |
| 645 | * @ip: the inode who owns the buffers |
| 646 | * @bstart: the first buffer in the run |
| 647 | * @blen: the number of buffers in the run |
| 648 | * |
| 649 | */ |
| 650 | |
| 651 | void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen) |
| 652 | { |
| 653 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 654 | struct inode *aspace = ip->i_gl->gl_aspace; |
| 655 | struct buffer_head *bh; |
| 656 | |
| 657 | while (blen) { |
| 658 | bh = getbuf(sdp, aspace, bstart, NO_CREATE); |
| 659 | if (bh) { |
| 660 | struct gfs2_bufdata *bd = get_v2bd(bh); |
| 661 | |
| 662 | if (test_clear_buffer_pinned(bh)) { |
| 663 | gfs2_log_lock(sdp); |
| 664 | list_del_init(&bd->bd_le.le_list); |
| 665 | gfs2_assert_warn(sdp, sdp->sd_log_num_buf); |
| 666 | sdp->sd_log_num_buf--; |
| 667 | gfs2_log_unlock(sdp); |
| 668 | get_transaction->tr_num_buf_rm++; |
| 669 | brelse(bh); |
| 670 | } |
| 671 | if (bd) { |
| 672 | gfs2_log_lock(sdp); |
| 673 | if (bd->bd_ail) { |
| 674 | uint64_t blkno = bh->b_blocknr; |
| 675 | bd->bd_ail = NULL; |
| 676 | list_del(&bd->bd_ail_st_list); |
| 677 | list_del(&bd->bd_ail_gl_list); |
| 678 | atomic_dec(&bd->bd_gl->gl_ail_count); |
| 679 | brelse(bh); |
| 680 | gfs2_log_unlock(sdp); |
| 681 | gfs2_trans_add_revoke(sdp, blkno); |
| 682 | } else |
| 683 | gfs2_log_unlock(sdp); |
| 684 | } |
| 685 | |
| 686 | lock_buffer(bh); |
| 687 | clear_buffer_dirty(bh); |
| 688 | clear_buffer_uptodate(bh); |
| 689 | unlock_buffer(bh); |
| 690 | |
| 691 | brelse(bh); |
| 692 | } |
| 693 | |
| 694 | bstart++; |
| 695 | blen--; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * gfs2_meta_cache_flush - get rid of any references on buffers for this inode |
| 701 | * @ip: The GFS2 inode |
| 702 | * |
| 703 | * This releases buffers that are in the most-recently-used array of |
| 704 | * blocks used for indirect block addressing for this inode. |
| 705 | */ |
| 706 | |
| 707 | void gfs2_meta_cache_flush(struct gfs2_inode *ip) |
| 708 | { |
| 709 | struct buffer_head **bh_slot; |
| 710 | unsigned int x; |
| 711 | |
| 712 | spin_lock(&ip->i_spin); |
| 713 | |
| 714 | for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) { |
| 715 | bh_slot = &ip->i_cache[x]; |
| 716 | if (!*bh_slot) |
| 717 | break; |
| 718 | brelse(*bh_slot); |
| 719 | *bh_slot = NULL; |
| 720 | } |
| 721 | |
| 722 | spin_unlock(&ip->i_spin); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * gfs2_meta_indirect_buffer - Get a metadata buffer |
| 727 | * @ip: The GFS2 inode |
| 728 | * @height: The level of this buf in the metadata (indir addr) tree (if any) |
| 729 | * @num: The block number (device relative) of the buffer |
| 730 | * @new: Non-zero if we may create a new buffer |
| 731 | * @bhp: the buffer is returned here |
| 732 | * |
| 733 | * Try to use the gfs2_inode's MRU metadata tree cache. |
| 734 | * |
| 735 | * Returns: errno |
| 736 | */ |
| 737 | |
| 738 | int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num, |
| 739 | int new, struct buffer_head **bhp) |
| 740 | { |
| 741 | struct buffer_head *bh, **bh_slot = ip->i_cache + height; |
| 742 | int error; |
| 743 | |
| 744 | spin_lock(&ip->i_spin); |
| 745 | bh = *bh_slot; |
| 746 | if (bh) { |
| 747 | if (bh->b_blocknr == num) |
| 748 | get_bh(bh); |
| 749 | else |
| 750 | bh = NULL; |
| 751 | } |
| 752 | spin_unlock(&ip->i_spin); |
| 753 | |
| 754 | if (bh) { |
| 755 | if (new) |
| 756 | meta_prep_new(bh); |
| 757 | else { |
| 758 | error = gfs2_meta_reread(ip->i_sbd, bh, |
| 759 | DIO_START | DIO_WAIT); |
| 760 | if (error) { |
| 761 | brelse(bh); |
| 762 | return error; |
| 763 | } |
| 764 | } |
| 765 | } else { |
| 766 | if (new) |
| 767 | bh = gfs2_meta_new(ip->i_gl, num); |
| 768 | else { |
| 769 | error = gfs2_meta_read(ip->i_gl, num, |
| 770 | DIO_START | DIO_WAIT, &bh); |
| 771 | if (error) |
| 772 | return error; |
| 773 | } |
| 774 | |
| 775 | spin_lock(&ip->i_spin); |
| 776 | if (*bh_slot != bh) { |
| 777 | brelse(*bh_slot); |
| 778 | *bh_slot = bh; |
| 779 | get_bh(bh); |
| 780 | } |
| 781 | spin_unlock(&ip->i_spin); |
| 782 | } |
| 783 | |
| 784 | if (new) { |
| 785 | if (gfs2_assert_warn(ip->i_sbd, height)) { |
| 786 | brelse(bh); |
| 787 | return -EIO; |
| 788 | } |
| 789 | gfs2_trans_add_bh(ip->i_gl, bh); |
| 790 | gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN); |
| 791 | gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); |
| 792 | |
| 793 | } else if (gfs2_metatype_check(ip->i_sbd, bh, |
| 794 | (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) { |
| 795 | brelse(bh); |
| 796 | return -EIO; |
| 797 | } |
| 798 | |
| 799 | *bhp = bh; |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * gfs2_meta_ra - start readahead on an extent of a file |
| 806 | * @gl: the glock the blocks belong to |
| 807 | * @dblock: the starting disk block |
| 808 | * @extlen: the number of blocks in the extent |
| 809 | * |
| 810 | */ |
| 811 | |
| 812 | void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen) |
| 813 | { |
| 814 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 815 | struct inode *aspace = gl->gl_aspace; |
| 816 | struct buffer_head *first_bh, *bh; |
| 817 | uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift; |
| 818 | int error; |
| 819 | |
| 820 | if (!extlen || !max_ra) |
| 821 | return; |
| 822 | if (extlen > max_ra) |
| 823 | extlen = max_ra; |
| 824 | |
| 825 | first_bh = getbuf(sdp, aspace, dblock, CREATE); |
| 826 | |
| 827 | if (buffer_uptodate(first_bh)) |
| 828 | goto out; |
| 829 | if (!buffer_locked(first_bh)) { |
| 830 | error = gfs2_meta_reread(sdp, first_bh, DIO_START); |
| 831 | if (error) |
| 832 | goto out; |
| 833 | } |
| 834 | |
| 835 | dblock++; |
| 836 | extlen--; |
| 837 | |
| 838 | while (extlen) { |
| 839 | bh = getbuf(sdp, aspace, dblock, CREATE); |
| 840 | |
| 841 | if (!buffer_uptodate(bh) && !buffer_locked(bh)) { |
| 842 | error = gfs2_meta_reread(sdp, bh, DIO_START); |
| 843 | brelse(bh); |
| 844 | if (error) |
| 845 | goto out; |
| 846 | } else |
| 847 | brelse(bh); |
| 848 | |
| 849 | dblock++; |
| 850 | extlen--; |
| 851 | |
| 852 | if (buffer_uptodate(first_bh)) |
| 853 | break; |
| 854 | } |
| 855 | |
| 856 | out: |
| 857 | brelse(first_bh); |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * gfs2_meta_syncfs - sync all the buffers in a filesystem |
| 862 | * @sdp: the filesystem |
| 863 | * |
| 864 | */ |
| 865 | |
| 866 | void gfs2_meta_syncfs(struct gfs2_sbd *sdp) |
| 867 | { |
| 868 | gfs2_log_flush(sdp); |
| 869 | for (;;) { |
| 870 | gfs2_ail1_start(sdp, DIO_ALL); |
| 871 | if (gfs2_ail1_empty(sdp, DIO_ALL)) |
| 872 | break; |
| 873 | msleep(100); |
| 874 | } |
| 875 | } |
| 876 | |