1 /* 2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 3 * Copyright (C) 2004-2006 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 version 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 <linux/bio.h> 21 #include <linux/gfs2_ondisk.h> 22 #include <linux/lm_interface.h> 23 24 #include "gfs2.h" 25 #include "incore.h" 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" 34 #include "util.h" 35 #include "ops_address.h" 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(inode->i_sb->s_fs_info, 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 static const struct address_space_operations aspace_aops = { 51 .writepage = gfs2_aspace_writepage, 52 .releasepage = gfs2_releasepage, 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) { 74 mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS); 75 aspace->i_mapping->a_ops = &aspace_aops; 76 aspace->i_size = ~0ULL; 77 aspace->i_private = NULL; 78 insert_inode_hash(aspace); 79 } 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 /** 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 113 * 114 */ 115 116 void gfs2_meta_sync(struct gfs2_glock *gl) 117 { 118 struct address_space *mapping = gl->gl_aspace->i_mapping; 119 int error; 120 121 filemap_fdatawrite(mapping); 122 error = filemap_fdatawait(mapping); 123 124 if (error) 125 gfs2_io_error(gl->gl_sbd); 126 } 127 128 /** 129 * getbuf - Get a buffer with a given address space 130 * @sdp: the filesystem 131 * @aspace: the address space 132 * @blkno: the block number (filesystem scope) 133 * @create: 1 if the buffer should be created 134 * 135 * Returns: the buffer 136 */ 137 138 static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace, 139 u64 blkno, int create) 140 { 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 (;;) { 153 page = grab_cache_page(aspace->i_mapping, index); 154 if (page) 155 break; 156 yield(); 157 } 158 } else { 159 page = find_lock_page(aspace->i_mapping, index); 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 202 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno) 203 { 204 struct buffer_head *bh; 205 bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE); 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 214 * @flags: flags 215 * @bhp: the place where the buffer is returned (NULL on failure) 216 * 217 * Returns: errno 218 */ 219 220 int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags, 221 struct buffer_head **bhp) 222 { 223 *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE); 224 if (!buffer_uptodate(*bhp)) 225 ll_rw_block(READ_META, 1, bhp); 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 } 233 234 return 0; 235 } 236 237 /** 238 * gfs2_meta_wait - Reread a block from disk 239 * @sdp: the filesystem 240 * @bh: The block to wait for 241 * 242 * Returns: errno 243 */ 244 245 int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh) 246 { 247 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) 248 return -EIO; 249 250 wait_on_buffer(bh); 251 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; 257 } 258 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) 259 return -EIO; 260 261 return 0; 262 } 263 264 /** 265 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer 266 * @gl: the glock the buffer belongs to 267 * @bh: The buffer to be attached to 268 * @meta: Flag to indicate whether its metadata or not 269 */ 270 271 void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh, 272 int meta) 273 { 274 struct gfs2_bufdata *bd; 275 276 if (meta) 277 lock_page(bh->b_page); 278 279 if (bh->b_private) { 280 if (meta) 281 unlock_page(bh->b_page); 282 return; 283 } 284 285 bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL), 286 memset(bd, 0, sizeof(struct gfs2_bufdata)); 287 bd->bd_bh = bh; 288 bd->bd_gl = gl; 289 290 INIT_LIST_HEAD(&bd->bd_list_tr); 291 if (meta) 292 lops_init_le(&bd->bd_le, &gfs2_buf_lops); 293 else 294 lops_init_le(&bd->bd_le, &gfs2_databuf_lops); 295 bh->b_private = bd; 296 297 if (meta) 298 unlock_page(bh->b_page); 299 } 300 301 /** 302 * gfs2_pin - Pin a buffer in memory 303 * @sdp: the filesystem the buffer belongs to 304 * @bh: The buffer to be pinned 305 * 306 */ 307 308 void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) 309 { 310 struct gfs2_bufdata *bd = bh->b_private; 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 /** 337 * gfs2_unpin - Unpin a buffer 338 * @sdp: the filesystem the buffer belongs to 339 * @bh: The buffer to unpin 340 * @ai: 341 * 342 */ 343 344 void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, 345 struct gfs2_ail *ai) 346 { 347 struct gfs2_bufdata *bd = bh->b_private; 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 379 void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen) 380 { 381 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 382 struct inode *aspace = ip->i_gl->gl_aspace; 383 struct buffer_head *bh; 384 385 while (blen) { 386 bh = getbuf(sdp, aspace, bstart, NO_CREATE); 387 if (bh) { 388 struct gfs2_bufdata *bd = bh->b_private; 389 390 if (test_clear_buffer_pinned(bh)) { 391 struct gfs2_trans *tr = current->journal_info; 392 gfs2_log_lock(sdp); 393 list_del_init(&bd->bd_le.le_list); 394 gfs2_assert_warn(sdp, sdp->sd_log_num_buf); 395 sdp->sd_log_num_buf--; 396 gfs2_log_unlock(sdp); 397 tr->tr_num_buf_rm++; 398 brelse(bh); 399 } 400 if (bd) { 401 gfs2_log_lock(sdp); 402 if (bd->bd_ail) { 403 u64 blkno = bh->b_blocknr; 404 bd->bd_ail = NULL; 405 list_del(&bd->bd_ail_st_list); 406 list_del(&bd->bd_ail_gl_list); 407 atomic_dec(&bd->bd_gl->gl_ail_count); 408 brelse(bh); 409 gfs2_log_unlock(sdp); 410 gfs2_trans_add_revoke(sdp, blkno); 411 } else 412 gfs2_log_unlock(sdp); 413 } 414 415 lock_buffer(bh); 416 clear_buffer_dirty(bh); 417 clear_buffer_uptodate(bh); 418 unlock_buffer(bh); 419 420 brelse(bh); 421 } 422 423 bstart++; 424 blen--; 425 } 426 } 427 428 /** 429 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode 430 * @ip: The GFS2 inode 431 * 432 * This releases buffers that are in the most-recently-used array of 433 * blocks used for indirect block addressing for this inode. 434 */ 435 436 void gfs2_meta_cache_flush(struct gfs2_inode *ip) 437 { 438 struct buffer_head **bh_slot; 439 unsigned int x; 440 441 spin_lock(&ip->i_spin); 442 443 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) { 444 bh_slot = &ip->i_cache[x]; 445 if (!*bh_slot) 446 break; 447 brelse(*bh_slot); 448 *bh_slot = NULL; 449 } 450 451 spin_unlock(&ip->i_spin); 452 } 453 454 /** 455 * gfs2_meta_indirect_buffer - Get a metadata buffer 456 * @ip: The GFS2 inode 457 * @height: The level of this buf in the metadata (indir addr) tree (if any) 458 * @num: The block number (device relative) of the buffer 459 * @new: Non-zero if we may create a new buffer 460 * @bhp: the buffer is returned here 461 * 462 * Try to use the gfs2_inode's MRU metadata tree cache. 463 * 464 * Returns: errno 465 */ 466 467 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num, 468 int new, struct buffer_head **bhp) 469 { 470 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 471 struct gfs2_glock *gl = ip->i_gl; 472 struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height; 473 int in_cache = 0; 474 475 spin_lock(&ip->i_spin); 476 if (*bh_slot && (*bh_slot)->b_blocknr == num) { 477 bh = *bh_slot; 478 get_bh(bh); 479 in_cache = 1; 480 } 481 spin_unlock(&ip->i_spin); 482 483 if (!bh) 484 bh = getbuf(gl->gl_sbd, gl->gl_aspace, num, CREATE); 485 486 if (!bh) 487 return -ENOBUFS; 488 489 if (new) { 490 if (gfs2_assert_warn(sdp, height)) 491 goto err; 492 meta_prep_new(bh); 493 gfs2_trans_add_bh(ip->i_gl, bh, 1); 494 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN); 495 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); 496 } else { 497 u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI; 498 if (!buffer_uptodate(bh)) { 499 ll_rw_block(READ_META, 1, &bh); 500 if (gfs2_meta_wait(sdp, bh)) 501 goto err; 502 } 503 if (gfs2_metatype_check(sdp, bh, mtype)) 504 goto err; 505 } 506 507 if (!in_cache) { 508 spin_lock(&ip->i_spin); 509 if (*bh_slot) 510 brelse(*bh_slot); 511 *bh_slot = bh; 512 get_bh(bh); 513 spin_unlock(&ip->i_spin); 514 } 515 516 *bhp = bh; 517 return 0; 518 err: 519 brelse(bh); 520 return -EIO; 521 } 522 523 /** 524 * gfs2_meta_ra - start readahead on an extent of a file 525 * @gl: the glock the blocks belong to 526 * @dblock: the starting disk block 527 * @extlen: the number of blocks in the extent 528 * 529 * returns: the first buffer in the extent 530 */ 531 532 struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen) 533 { 534 struct gfs2_sbd *sdp = gl->gl_sbd; 535 struct inode *aspace = gl->gl_aspace; 536 struct buffer_head *first_bh, *bh; 537 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> 538 sdp->sd_sb.sb_bsize_shift; 539 540 BUG_ON(!extlen); 541 542 if (max_ra < 1) 543 max_ra = 1; 544 if (extlen > max_ra) 545 extlen = max_ra; 546 547 first_bh = getbuf(sdp, aspace, dblock, CREATE); 548 549 if (buffer_uptodate(first_bh)) 550 goto out; 551 if (!buffer_locked(first_bh)) 552 ll_rw_block(READ_META, 1, &first_bh); 553 554 dblock++; 555 extlen--; 556 557 while (extlen) { 558 bh = getbuf(sdp, aspace, dblock, CREATE); 559 560 if (!buffer_uptodate(bh) && !buffer_locked(bh)) 561 ll_rw_block(READA, 1, &bh); 562 brelse(bh); 563 dblock++; 564 extlen--; 565 if (!buffer_locked(first_bh) && buffer_uptodate(first_bh)) 566 goto out; 567 } 568 569 wait_on_buffer(first_bh); 570 out: 571 return first_bh; 572 } 573 574 /** 575 * gfs2_meta_syncfs - sync all the buffers in a filesystem 576 * @sdp: the filesystem 577 * 578 */ 579 580 void gfs2_meta_syncfs(struct gfs2_sbd *sdp) 581 { 582 gfs2_log_flush(sdp, NULL); 583 for (;;) { 584 gfs2_ail1_start(sdp, DIO_ALL); 585 if (gfs2_ail1_empty(sdp, DIO_ALL)) 586 break; 587 msleep(10); 588 } 589 } 590 591