1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 4 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. 5 */ 6 7 #include <linux/spinlock.h> 8 #include <linux/completion.h> 9 #include <linux/buffer_head.h> 10 #include <linux/blkdev.h> 11 #include <linux/gfs2_ondisk.h> 12 #include <linux/crc32.h> 13 #include <linux/iomap.h> 14 #include <linux/ktime.h> 15 16 #include "gfs2.h" 17 #include "incore.h" 18 #include "bmap.h" 19 #include "glock.h" 20 #include "inode.h" 21 #include "meta_io.h" 22 #include "quota.h" 23 #include "rgrp.h" 24 #include "log.h" 25 #include "super.h" 26 #include "trans.h" 27 #include "dir.h" 28 #include "util.h" 29 #include "aops.h" 30 #include "trace_gfs2.h" 31 32 /* This doesn't need to be that large as max 64 bit pointers in a 4k 33 * block is 512, so __u16 is fine for that. It saves stack space to 34 * keep it small. 35 */ 36 struct metapath { 37 struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT]; 38 __u16 mp_list[GFS2_MAX_META_HEIGHT]; 39 int mp_fheight; /* find_metapath height */ 40 int mp_aheight; /* actual height (lookup height) */ 41 }; 42 43 static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length); 44 45 /** 46 * gfs2_unstuffer_folio - unstuff a stuffed inode into a block cached by a folio 47 * @ip: the inode 48 * @dibh: the dinode buffer 49 * @block: the block number that was allocated 50 * @folio: The folio. 51 * 52 * Returns: errno 53 */ 54 static int gfs2_unstuffer_folio(struct gfs2_inode *ip, struct buffer_head *dibh, 55 u64 block, struct folio *folio) 56 { 57 struct inode *inode = &ip->i_inode; 58 struct gfs2_glock *gl = gfs2_inode_glock(inode); 59 60 if (!folio_test_uptodate(folio)) { 61 void *kaddr = kmap_local_folio(folio, 0); 62 u64 dsize = i_size_read(inode); 63 64 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize); 65 memset(kaddr + dsize, 0, folio_size(folio) - dsize); 66 kunmap_local(kaddr); 67 68 folio_mark_uptodate(folio); 69 } 70 71 if (gfs2_is_jdata(ip)) { 72 struct buffer_head *bh = folio_buffers(folio); 73 74 if (!bh) 75 bh = create_empty_buffers(folio, 76 BIT(inode->i_blkbits), BIT(BH_Uptodate)); 77 78 if (!buffer_mapped(bh)) 79 map_bh(bh, inode->i_sb, block); 80 81 set_buffer_uptodate(bh); 82 gfs2_trans_add_data(gl, bh); 83 } else { 84 folio_mark_dirty(folio); 85 gfs2_ordered_add_inode(ip); 86 } 87 88 return 0; 89 } 90 91 static int __gfs2_unstuff_inode(struct gfs2_inode *ip, struct folio *folio) 92 { 93 struct inode *inode = &ip->i_inode; 94 struct gfs2_glock *gl = gfs2_inode_glock(inode); 95 struct buffer_head *bh, *dibh; 96 struct gfs2_dinode *di; 97 u64 block = 0; 98 int isdir = gfs2_is_dir(ip); 99 int error; 100 101 error = gfs2_meta_inode_buffer(ip, &dibh); 102 if (error) 103 return error; 104 105 if (i_size_read(inode)) { 106 /* Get a free block, fill it with the stuffed data, 107 and write it out to disk */ 108 109 unsigned int n = 1; 110 error = gfs2_alloc_blocks(ip, &block, &n, 0); 111 if (error) 112 goto out_brelse; 113 if (isdir) { 114 gfs2_trans_remove_revoke(GFS2_SB(inode), block, 1); 115 error = gfs2_dir_get_new_buffer(ip, block, &bh); 116 if (error) 117 goto out_brelse; 118 gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header), 119 dibh, sizeof(struct gfs2_dinode)); 120 brelse(bh); 121 } else { 122 error = gfs2_unstuffer_folio(ip, dibh, block, folio); 123 if (error) 124 goto out_brelse; 125 } 126 } 127 128 /* Set up the pointer to the new block */ 129 130 gfs2_trans_add_meta(gl, dibh); 131 di = (struct gfs2_dinode *)dibh->b_data; 132 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); 133 134 if (i_size_read(inode)) { 135 *(__be64 *)(di + 1) = cpu_to_be64(block); 136 gfs2_add_inode_blocks(inode, 1); 137 di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(inode)); 138 } 139 140 ip->i_height = 1; 141 di->di_height = cpu_to_be16(1); 142 143 out_brelse: 144 brelse(dibh); 145 return error; 146 } 147 148 /** 149 * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big 150 * @ip: The GFS2 inode to unstuff 151 * 152 * This routine unstuffs a dinode and returns it to a "normal" state such 153 * that the height can be grown in the traditional way. 154 * 155 * Returns: errno 156 */ 157 158 int gfs2_unstuff_dinode(struct gfs2_inode *ip) 159 { 160 struct inode *inode = &ip->i_inode; 161 struct folio *folio; 162 int error; 163 164 down_write(&ip->i_rw_mutex); 165 folio = filemap_grab_folio(inode->i_mapping, 0); 166 error = PTR_ERR(folio); 167 if (IS_ERR(folio)) 168 goto out; 169 error = __gfs2_unstuff_inode(ip, folio); 170 folio_unlock(folio); 171 folio_put(folio); 172 out: 173 up_write(&ip->i_rw_mutex); 174 return error; 175 } 176 177 /** 178 * find_metapath - Find path through the metadata tree 179 * @sdp: The superblock 180 * @block: The disk block to look up 181 * @mp: The metapath to return the result in 182 * @height: The pre-calculated height of the metadata tree 183 * 184 * This routine returns a struct metapath structure that defines a path 185 * through the metadata of inode "ip" to get to block "block". 186 * 187 * Example: 188 * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a 189 * filesystem with a blocksize of 4096. 190 * 191 * find_metapath() would return a struct metapath structure set to: 192 * mp_fheight = 3, mp_list[0] = 0, mp_list[1] = 48, and mp_list[2] = 165. 193 * 194 * That means that in order to get to the block containing the byte at 195 * offset 101342453, we would load the indirect block pointed to by pointer 196 * 0 in the dinode. We would then load the indirect block pointed to by 197 * pointer 48 in that indirect block. We would then load the data block 198 * pointed to by pointer 165 in that indirect block. 199 * 200 * ---------------------------------------- 201 * | Dinode | | 202 * | | 4| 203 * | |0 1 2 3 4 5 9| 204 * | | 6| 205 * ---------------------------------------- 206 * | 207 * | 208 * V 209 * ---------------------------------------- 210 * | Indirect Block | 211 * | 5| 212 * | 4 4 4 4 4 5 5 1| 213 * |0 5 6 7 8 9 0 1 2| 214 * ---------------------------------------- 215 * | 216 * | 217 * V 218 * ---------------------------------------- 219 * | Indirect Block | 220 * | 1 1 1 1 1 5| 221 * | 6 6 6 6 6 1| 222 * |0 3 4 5 6 7 2| 223 * ---------------------------------------- 224 * | 225 * | 226 * V 227 * ---------------------------------------- 228 * | Data block containing offset | 229 * | 101342453 | 230 * | | 231 * | | 232 * ---------------------------------------- 233 * 234 */ 235 236 static void find_metapath(const struct gfs2_sbd *sdp, u64 block, 237 struct metapath *mp, unsigned int height) 238 { 239 unsigned int i; 240 241 mp->mp_fheight = height; 242 for (i = height; i--;) 243 mp->mp_list[i] = do_div(block, sdp->sd_inptrs); 244 } 245 246 static inline unsigned int metapath_branch_start(const struct metapath *mp) 247 { 248 if (mp->mp_list[0] == 0) 249 return 2; 250 return 1; 251 } 252 253 /** 254 * metaptr1 - Return the first possible metadata pointer in a metapath buffer 255 * @height: The metadata height (0 = dinode) 256 * @mp: The metapath 257 */ 258 static inline __be64 *metaptr1(unsigned int height, const struct metapath *mp) 259 { 260 struct buffer_head *bh = mp->mp_bh[height]; 261 if (height == 0) 262 return ((__be64 *)(bh->b_data + sizeof(struct gfs2_dinode))); 263 return ((__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header))); 264 } 265 266 /** 267 * metapointer - Return pointer to start of metadata in a buffer 268 * @height: The metadata height (0 = dinode) 269 * @mp: The metapath 270 * 271 * Return a pointer to the block number of the next height of the metadata 272 * tree given a buffer containing the pointer to the current height of the 273 * metadata tree. 274 */ 275 276 static inline __be64 *metapointer(unsigned int height, const struct metapath *mp) 277 { 278 __be64 *p = metaptr1(height, mp); 279 return p + mp->mp_list[height]; 280 } 281 282 static inline const __be64 *metaend(unsigned int height, const struct metapath *mp) 283 { 284 const struct buffer_head *bh = mp->mp_bh[height]; 285 return (const __be64 *)(bh->b_data + bh->b_size); 286 } 287 288 static void clone_metapath(struct metapath *clone, struct metapath *mp) 289 { 290 unsigned int hgt; 291 292 *clone = *mp; 293 for (hgt = 0; hgt < mp->mp_aheight; hgt++) 294 get_bh(clone->mp_bh[hgt]); 295 } 296 297 static void gfs2_metapath_ra(struct gfs2_glock *gl, __be64 *start, __be64 *end) 298 { 299 const __be64 *t; 300 301 for (t = start; t < end; t++) { 302 struct buffer_head *rabh; 303 304 if (!*t) 305 continue; 306 307 rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE); 308 if (trylock_buffer(rabh)) { 309 if (!buffer_uptodate(rabh)) { 310 bh_submit(rabh, 311 REQ_OP_READ | REQ_RAHEAD | REQ_META | 312 REQ_PRIO, 313 bh_end_read); 314 } else { 315 unlock_buffer(rabh); 316 } 317 } 318 put_bh(rabh); 319 } 320 } 321 322 static inline struct buffer_head * 323 metapath_dibh(struct metapath *mp) 324 { 325 return mp->mp_bh[0]; 326 } 327 328 static int __fillup_metapath(struct gfs2_inode *ip, struct metapath *mp, 329 unsigned int x, unsigned int h) 330 { 331 for (; x < h; x++) { 332 __be64 *ptr = metapointer(x, mp); 333 u64 dblock = be64_to_cpu(*ptr); 334 int ret; 335 336 if (!dblock) 337 break; 338 ret = gfs2_meta_buffer(ip, GFS2_METATYPE_IN, dblock, &mp->mp_bh[x + 1]); 339 if (ret) 340 return ret; 341 } 342 mp->mp_aheight = x + 1; 343 return 0; 344 } 345 346 /** 347 * lookup_metapath - Walk the metadata tree to a specific point 348 * @ip: The inode 349 * @mp: The metapath 350 * 351 * Assumes that the inode's buffer has already been looked up and 352 * hooked onto mp->mp_bh[0] and that the metapath has been initialised 353 * by find_metapath(). 354 * 355 * If this function encounters part of the tree which has not been 356 * allocated, it returns the current height of the tree at the point 357 * at which it found the unallocated block. Blocks which are found are 358 * added to the mp->mp_bh[] list. 359 * 360 * Returns: error 361 */ 362 363 static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp) 364 { 365 return __fillup_metapath(ip, mp, 0, ip->i_height - 1); 366 } 367 368 /** 369 * fillup_metapath - fill up buffers for the metadata path to a specific height 370 * @ip: The inode 371 * @mp: The metapath 372 * @h: The height to which it should be mapped 373 * 374 * Similar to lookup_metapath, but does lookups for a range of heights 375 * 376 * Returns: error or the number of buffers filled 377 */ 378 379 static int fillup_metapath(struct gfs2_inode *ip, struct metapath *mp, int h) 380 { 381 unsigned int x = 0; 382 int ret; 383 384 if (h) { 385 /* find the first buffer we need to look up. */ 386 for (x = h - 1; x > 0; x--) { 387 if (mp->mp_bh[x]) 388 break; 389 } 390 } 391 ret = __fillup_metapath(ip, mp, x, h); 392 if (ret) 393 return ret; 394 return mp->mp_aheight - x - 1; 395 } 396 397 static sector_t metapath_to_block(struct gfs2_sbd *sdp, struct metapath *mp) 398 { 399 sector_t factor = 1, block = 0; 400 int hgt; 401 402 for (hgt = mp->mp_fheight - 1; hgt >= 0; hgt--) { 403 if (hgt < mp->mp_aheight) 404 block += mp->mp_list[hgt] * factor; 405 factor *= sdp->sd_inptrs; 406 } 407 return block; 408 } 409 410 static void release_metapath(struct metapath *mp) 411 { 412 int i; 413 414 for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) { 415 if (mp->mp_bh[i] == NULL) 416 break; 417 brelse(mp->mp_bh[i]); 418 mp->mp_bh[i] = NULL; 419 } 420 } 421 422 /** 423 * gfs2_extent_length - Returns length of an extent of blocks 424 * @bh: The metadata block 425 * @ptr: Current position in @bh 426 * @eob: Set to 1 if we hit "end of block" 427 * 428 * Returns: The length of the extent (minimum of one block) 429 */ 430 431 static inline unsigned int gfs2_extent_length(struct buffer_head *bh, __be64 *ptr, int *eob) 432 { 433 const __be64 *end = (__be64 *)(bh->b_data + bh->b_size); 434 const __be64 *first = ptr; 435 u64 d = be64_to_cpu(*ptr); 436 437 *eob = 0; 438 do { 439 ptr++; 440 if (ptr >= end) 441 break; 442 d++; 443 } while(be64_to_cpu(*ptr) == d); 444 if (ptr >= end) 445 *eob = 1; 446 return ptr - first; 447 } 448 449 enum walker_status { WALK_STOP, WALK_FOLLOW, WALK_CONTINUE }; 450 451 /* 452 * gfs2_metadata_walker - walk an indirect block 453 * @mp: Metapath to indirect block 454 * @ptrs: Number of pointers to look at 455 * 456 * When returning WALK_FOLLOW, the walker must update @mp to point at the right 457 * indirect block to follow. 458 */ 459 typedef enum walker_status (*gfs2_metadata_walker)(struct metapath *mp, 460 unsigned int ptrs); 461 462 /* 463 * gfs2_walk_metadata - walk a tree of indirect blocks 464 * @inode: The inode 465 * @mp: Starting point of walk 466 * @max_len: Maximum number of blocks to walk 467 * @walker: Called during the walk 468 * 469 * Returns 1 if the walk was stopped by @walker, 0 if we went past @max_len or 470 * past the end of metadata, and a negative error code otherwise. 471 */ 472 473 static int gfs2_walk_metadata(struct inode *inode, struct metapath *mp, 474 u64 max_len, gfs2_metadata_walker walker) 475 { 476 struct gfs2_inode *ip = GFS2_I(inode); 477 struct gfs2_sbd *sdp = GFS2_SB(inode); 478 u64 factor = 1; 479 unsigned int hgt; 480 int ret; 481 482 /* 483 * The walk starts in the lowest allocated indirect block, which may be 484 * before the position indicated by @mp. Adjust @max_len accordingly 485 * to avoid a short walk. 486 */ 487 for (hgt = mp->mp_fheight - 1; hgt >= mp->mp_aheight; hgt--) { 488 max_len += mp->mp_list[hgt] * factor; 489 mp->mp_list[hgt] = 0; 490 factor *= sdp->sd_inptrs; 491 } 492 493 for (;;) { 494 u16 start = mp->mp_list[hgt]; 495 enum walker_status status; 496 unsigned int ptrs; 497 u64 len; 498 499 /* Walk indirect block. */ 500 ptrs = (hgt >= 1 ? sdp->sd_inptrs : sdp->sd_diptrs) - start; 501 len = ptrs * factor; 502 if (len > max_len) 503 ptrs = DIV_ROUND_UP_ULL(max_len, factor); 504 status = walker(mp, ptrs); 505 switch (status) { 506 case WALK_STOP: 507 return 1; 508 case WALK_FOLLOW: 509 BUG_ON(mp->mp_aheight == mp->mp_fheight); 510 ptrs = mp->mp_list[hgt] - start; 511 len = ptrs * factor; 512 break; 513 case WALK_CONTINUE: 514 break; 515 } 516 if (len >= max_len) 517 break; 518 max_len -= len; 519 if (status == WALK_FOLLOW) 520 goto fill_up_metapath; 521 522 lower_metapath: 523 /* Decrease height of metapath. */ 524 brelse(mp->mp_bh[hgt]); 525 mp->mp_bh[hgt] = NULL; 526 mp->mp_list[hgt] = 0; 527 if (!hgt) 528 break; 529 hgt--; 530 factor *= sdp->sd_inptrs; 531 532 /* Advance in metadata tree. */ 533 (mp->mp_list[hgt])++; 534 if (hgt) { 535 if (mp->mp_list[hgt] >= sdp->sd_inptrs) 536 goto lower_metapath; 537 } else { 538 if (mp->mp_list[hgt] >= sdp->sd_diptrs) 539 break; 540 } 541 542 fill_up_metapath: 543 /* Increase height of metapath. */ 544 ret = fillup_metapath(ip, mp, ip->i_height - 1); 545 if (ret < 0) 546 return ret; 547 hgt += ret; 548 for (; ret; ret--) 549 do_div(factor, sdp->sd_inptrs); 550 mp->mp_aheight = hgt + 1; 551 } 552 return 0; 553 } 554 555 static enum walker_status gfs2_hole_walker(struct metapath *mp, 556 unsigned int ptrs) 557 { 558 const __be64 *start, *ptr, *end; 559 unsigned int hgt; 560 561 hgt = mp->mp_aheight - 1; 562 start = metapointer(hgt, mp); 563 end = start + ptrs; 564 565 for (ptr = start; ptr < end; ptr++) { 566 if (*ptr) { 567 mp->mp_list[hgt] += ptr - start; 568 if (mp->mp_aheight == mp->mp_fheight) 569 return WALK_STOP; 570 return WALK_FOLLOW; 571 } 572 } 573 return WALK_CONTINUE; 574 } 575 576 /** 577 * gfs2_hole_size - figure out the size of a hole 578 * @inode: The inode 579 * @lblock: The logical starting block number 580 * @len: How far to look (in blocks) 581 * @mp: The metapath at lblock 582 * @iomap: The iomap to store the hole size in 583 * 584 * This function modifies @mp. 585 * 586 * Returns: errno on error 587 */ 588 static int gfs2_hole_size(struct inode *inode, sector_t lblock, u64 len, 589 struct metapath *mp, struct iomap *iomap) 590 { 591 struct metapath clone; 592 u64 hole_size; 593 int ret; 594 595 clone_metapath(&clone, mp); 596 ret = gfs2_walk_metadata(inode, &clone, len, gfs2_hole_walker); 597 if (ret < 0) 598 goto out; 599 600 if (ret == 1) 601 hole_size = metapath_to_block(GFS2_SB(inode), &clone) - lblock; 602 else 603 hole_size = len; 604 iomap->length = hole_size << inode->i_blkbits; 605 ret = 0; 606 607 out: 608 release_metapath(&clone); 609 return ret; 610 } 611 612 static inline void gfs2_indirect_init(struct metapath *mp, 613 struct gfs2_glock *gl, unsigned int i, 614 unsigned offset, u64 bn) 615 { 616 __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data + 617 ((i > 1) ? sizeof(struct gfs2_meta_header) : 618 sizeof(struct gfs2_dinode))); 619 BUG_ON(i < 1); 620 BUG_ON(mp->mp_bh[i] != NULL); 621 mp->mp_bh[i] = gfs2_meta_new(gl, bn); 622 gfs2_trans_add_meta(gl, mp->mp_bh[i]); 623 gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN); 624 gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header)); 625 ptr += offset; 626 *ptr = cpu_to_be64(bn); 627 } 628 629 enum alloc_state { 630 ALLOC_DATA = 0, 631 ALLOC_GROW_DEPTH = 1, 632 ALLOC_GROW_HEIGHT = 2, 633 /* ALLOC_UNSTUFF = 3, TBD and rather complicated */ 634 }; 635 636 /** 637 * __gfs2_iomap_alloc - Build a metadata tree of the requested height 638 * @inode: The GFS2 inode 639 * @iomap: The iomap structure 640 * @mp: The metapath, with proper height information calculated 641 * 642 * In this routine we may have to alloc: 643 * i) Indirect blocks to grow the metadata tree height 644 * ii) Indirect blocks to fill in lower part of the metadata tree 645 * iii) Data blocks 646 * 647 * This function is called after __gfs2_iomap_get, which works out the 648 * total number of blocks which we need via gfs2_alloc_size. 649 * 650 * We then do the actual allocation asking for an extent at a time (if 651 * enough contiguous free blocks are available, there will only be one 652 * allocation request per call) and uses the state machine to initialise 653 * the blocks in order. 654 * 655 * Right now, this function will allocate at most one indirect block 656 * worth of data -- with a default block size of 4K, that's slightly 657 * less than 2M. If this limitation is ever removed to allow huge 658 * allocations, we would probably still want to limit the iomap size we 659 * return to avoid stalling other tasks during huge writes; the next 660 * iomap iteration would then find the blocks already allocated. 661 * 662 * Returns: errno on error 663 */ 664 665 static int __gfs2_iomap_alloc(struct inode *inode, struct iomap *iomap, 666 struct metapath *mp) 667 { 668 struct gfs2_glock *gl = gfs2_inode_glock(inode); 669 struct gfs2_inode *ip = GFS2_I(inode); 670 struct gfs2_sbd *sdp = GFS2_SB(inode); 671 struct buffer_head *dibh = metapath_dibh(mp); 672 u64 bn; 673 unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0; 674 size_t dblks = iomap->length >> inode->i_blkbits; 675 const unsigned end_of_metadata = mp->mp_fheight - 1; 676 int ret; 677 enum alloc_state state; 678 __be64 *ptr; 679 __be64 zero_bn = 0; 680 681 BUG_ON(mp->mp_aheight < 1); 682 BUG_ON(dibh == NULL); 683 BUG_ON(dblks < 1); 684 685 gfs2_trans_add_meta(gl, dibh); 686 687 down_write(&ip->i_rw_mutex); 688 689 if (mp->mp_fheight == mp->mp_aheight) { 690 /* Bottom indirect block exists */ 691 state = ALLOC_DATA; 692 } else { 693 /* Need to allocate indirect blocks */ 694 if (mp->mp_fheight == ip->i_height) { 695 /* Writing into existing tree, extend tree down */ 696 iblks = mp->mp_fheight - mp->mp_aheight; 697 state = ALLOC_GROW_DEPTH; 698 } else { 699 /* Building up tree height */ 700 state = ALLOC_GROW_HEIGHT; 701 iblks = mp->mp_fheight - ip->i_height; 702 branch_start = metapath_branch_start(mp); 703 iblks += (mp->mp_fheight - branch_start); 704 } 705 } 706 707 /* start of the second part of the function (state machine) */ 708 709 blks = dblks + iblks; 710 i = mp->mp_aheight; 711 do { 712 n = blks - alloced; 713 ret = gfs2_alloc_blocks(ip, &bn, &n, 0); 714 if (ret) 715 goto out; 716 alloced += n; 717 if (state != ALLOC_DATA || gfs2_is_jdata(ip)) 718 gfs2_trans_remove_revoke(sdp, bn, n); 719 switch (state) { 720 /* Growing height of tree */ 721 case ALLOC_GROW_HEIGHT: 722 if (i == 1) { 723 ptr = (__be64 *)(dibh->b_data + 724 sizeof(struct gfs2_dinode)); 725 zero_bn = *ptr; 726 } 727 for (; i - 1 < mp->mp_fheight - ip->i_height && n > 0; 728 i++, n--) 729 gfs2_indirect_init(mp, gl, i, 0, bn++); 730 if (i - 1 == mp->mp_fheight - ip->i_height) { 731 i--; 732 gfs2_buffer_copy_tail(mp->mp_bh[i], 733 sizeof(struct gfs2_meta_header), 734 dibh, sizeof(struct gfs2_dinode)); 735 gfs2_buffer_clear_tail(dibh, 736 sizeof(struct gfs2_dinode) + 737 sizeof(__be64)); 738 ptr = (__be64 *)(mp->mp_bh[i]->b_data + 739 sizeof(struct gfs2_meta_header)); 740 *ptr = zero_bn; 741 state = ALLOC_GROW_DEPTH; 742 for(i = branch_start; i < mp->mp_fheight; i++) { 743 if (mp->mp_bh[i] == NULL) 744 break; 745 brelse(mp->mp_bh[i]); 746 mp->mp_bh[i] = NULL; 747 } 748 i = branch_start; 749 } 750 if (n == 0) 751 break; 752 fallthrough; /* To branching from existing tree */ 753 case ALLOC_GROW_DEPTH: 754 if (i > 1 && i < mp->mp_fheight) 755 gfs2_trans_add_meta(gl, mp->mp_bh[i-1]); 756 for (; i < mp->mp_fheight && n > 0; i++, n--) 757 gfs2_indirect_init(mp, gl, i, 758 mp->mp_list[i-1], bn++); 759 if (i == mp->mp_fheight) 760 state = ALLOC_DATA; 761 if (n == 0) 762 break; 763 fallthrough; /* To tree complete, adding data blocks */ 764 case ALLOC_DATA: 765 BUG_ON(n > dblks); 766 BUG_ON(mp->mp_bh[end_of_metadata] == NULL); 767 gfs2_trans_add_meta(gl, mp->mp_bh[end_of_metadata]); 768 dblks = n; 769 ptr = metapointer(end_of_metadata, mp); 770 iomap->addr = bn << inode->i_blkbits; 771 iomap->flags |= IOMAP_F_MERGED | IOMAP_F_NEW; 772 while (n-- > 0) 773 *ptr++ = cpu_to_be64(bn++); 774 break; 775 } 776 } while (iomap->addr == IOMAP_NULL_ADDR); 777 778 iomap->type = IOMAP_MAPPED; 779 iomap->length = (u64)dblks << inode->i_blkbits; 780 ip->i_height = mp->mp_fheight; 781 gfs2_add_inode_blocks(&ip->i_inode, alloced); 782 gfs2_dinode_out(ip, dibh->b_data); 783 out: 784 up_write(&ip->i_rw_mutex); 785 return ret; 786 } 787 788 #define IOMAP_F_GFS2_BOUNDARY IOMAP_F_PRIVATE 789 790 /** 791 * gfs2_alloc_size - Compute the maximum allocation size 792 * @inode: The inode 793 * @mp: The metapath 794 * @size: Requested size in blocks 795 * 796 * Compute the maximum size of the next allocation at @mp. 797 * 798 * Returns: size in blocks 799 */ 800 static u64 gfs2_alloc_size(struct inode *inode, struct metapath *mp, u64 size) 801 { 802 struct gfs2_inode *ip = GFS2_I(inode); 803 struct gfs2_sbd *sdp = GFS2_SB(inode); 804 const __be64 *first, *ptr, *end; 805 806 /* 807 * For writes to stuffed files, this function is called twice via 808 * __gfs2_iomap_get, before and after unstuffing. The size we return the 809 * first time needs to be large enough to get the reservation and 810 * allocation sizes right. The size we return the second time must 811 * be exact or else __gfs2_iomap_alloc won't do the right thing. 812 */ 813 814 if (gfs2_is_stuffed(ip) || mp->mp_fheight != mp->mp_aheight) { 815 unsigned int maxsize = mp->mp_fheight > 1 ? 816 sdp->sd_inptrs : sdp->sd_diptrs; 817 maxsize -= mp->mp_list[mp->mp_fheight - 1]; 818 if (size > maxsize) 819 size = maxsize; 820 return size; 821 } 822 823 first = metapointer(ip->i_height - 1, mp); 824 end = metaend(ip->i_height - 1, mp); 825 if (end - first > size) 826 end = first + size; 827 for (ptr = first; ptr < end; ptr++) { 828 if (*ptr) 829 break; 830 } 831 return ptr - first; 832 } 833 834 /** 835 * __gfs2_iomap_get - Map blocks from an inode to disk blocks 836 * @inode: The inode 837 * @pos: Starting position in bytes 838 * @length: Length to map, in bytes 839 * @flags: iomap flags 840 * @iomap: The iomap structure 841 * @mp: The metapath 842 * 843 * Returns: errno 844 */ 845 static int __gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length, 846 unsigned flags, struct iomap *iomap, 847 struct metapath *mp) 848 { 849 struct gfs2_inode *ip = GFS2_I(inode); 850 struct gfs2_sbd *sdp = GFS2_SB(inode); 851 loff_t size = i_size_read(inode); 852 __be64 *ptr; 853 sector_t lblock; 854 sector_t lblock_stop; 855 int ret; 856 int eob; 857 u64 len; 858 struct buffer_head *dibh = NULL, *bh; 859 u8 height; 860 861 if (!length) 862 return -EINVAL; 863 864 down_read(&ip->i_rw_mutex); 865 866 ret = gfs2_meta_inode_buffer(ip, &dibh); 867 if (ret) 868 goto unlock; 869 mp->mp_bh[0] = dibh; 870 871 if (gfs2_is_stuffed(ip)) { 872 if (flags & IOMAP_WRITE) { 873 loff_t max_size = gfs2_max_stuffed_size(ip); 874 875 if (pos + length > max_size) 876 goto unstuff; 877 iomap->length = max_size; 878 } else { 879 if (pos >= size) { 880 if (flags & IOMAP_REPORT) { 881 ret = -ENOENT; 882 goto unlock; 883 } else { 884 iomap->offset = pos; 885 iomap->length = length; 886 goto hole_found; 887 } 888 } 889 iomap->length = size; 890 } 891 iomap->addr = (ip->i_no_addr << inode->i_blkbits) + 892 sizeof(struct gfs2_dinode); 893 iomap->type = IOMAP_INLINE; 894 iomap->inline_data = dibh->b_data + sizeof(struct gfs2_dinode); 895 goto out; 896 } 897 898 unstuff: 899 lblock = pos >> inode->i_blkbits; 900 iomap->offset = lblock << inode->i_blkbits; 901 lblock_stop = (pos + length - 1) >> inode->i_blkbits; 902 len = lblock_stop - lblock + 1; 903 iomap->length = len << inode->i_blkbits; 904 905 height = ip->i_height; 906 while ((lblock + 1) * sdp->sd_sb.sb_bsize > sdp->sd_heightsize[height]) 907 height++; 908 find_metapath(sdp, lblock, mp, height); 909 if (height > ip->i_height || gfs2_is_stuffed(ip)) 910 goto do_alloc; 911 912 ret = lookup_metapath(ip, mp); 913 if (ret) 914 goto unlock; 915 916 if (mp->mp_aheight != ip->i_height) 917 goto do_alloc; 918 919 ptr = metapointer(ip->i_height - 1, mp); 920 if (*ptr == 0) 921 goto do_alloc; 922 923 bh = mp->mp_bh[ip->i_height - 1]; 924 len = gfs2_extent_length(bh, ptr, &eob); 925 926 iomap->addr = be64_to_cpu(*ptr) << inode->i_blkbits; 927 iomap->length = len << inode->i_blkbits; 928 iomap->type = IOMAP_MAPPED; 929 iomap->flags |= IOMAP_F_MERGED; 930 if (eob) 931 iomap->flags |= IOMAP_F_GFS2_BOUNDARY; 932 933 out: 934 iomap->bdev = inode->i_sb->s_bdev; 935 unlock: 936 up_read(&ip->i_rw_mutex); 937 return ret; 938 939 do_alloc: 940 if (flags & IOMAP_REPORT) { 941 if (pos >= size) 942 ret = -ENOENT; 943 else if (height == ip->i_height) 944 ret = gfs2_hole_size(inode, lblock, len, mp, iomap); 945 else 946 iomap->length = size - iomap->offset; 947 } else if (flags & IOMAP_WRITE) { 948 u64 alloc_size; 949 950 if (flags & IOMAP_DIRECT) 951 goto out; /* (see gfs2_file_direct_write) */ 952 953 len = gfs2_alloc_size(inode, mp, len); 954 alloc_size = len << inode->i_blkbits; 955 if (alloc_size < iomap->length) 956 iomap->length = alloc_size; 957 } else { 958 if (pos < size && height == ip->i_height) 959 ret = gfs2_hole_size(inode, lblock, len, mp, iomap); 960 } 961 hole_found: 962 iomap->addr = IOMAP_NULL_ADDR; 963 iomap->type = IOMAP_HOLE; 964 goto out; 965 } 966 967 static struct folio * 968 gfs2_iomap_get_folio(struct iomap_iter *iter, loff_t pos, unsigned len) 969 { 970 struct inode *inode = iter->inode; 971 struct gfs2_inode *ip = GFS2_I(inode); 972 unsigned int blockmask = i_blocksize(inode) - 1; 973 struct gfs2_sbd *sdp = GFS2_SB(inode); 974 unsigned int blocks; 975 struct folio *folio; 976 int status; 977 978 if (!gfs2_is_jdata(ip) && !gfs2_is_stuffed(ip)) 979 return iomap_get_folio(iter, pos, len); 980 981 blocks = ((pos & blockmask) + len + blockmask) >> inode->i_blkbits; 982 status = gfs2_trans_begin(sdp, RES_DINODE + blocks, 0); 983 if (status) 984 return ERR_PTR(status); 985 986 folio = iomap_get_folio(iter, pos, len); 987 if (IS_ERR(folio)) 988 gfs2_trans_end(sdp); 989 return folio; 990 } 991 992 static void gfs2_iomap_put_folio(struct inode *inode, loff_t pos, 993 unsigned copied, struct folio *folio) 994 { 995 struct gfs2_trans *tr = current->journal_info; 996 struct gfs2_glock *gl = gfs2_inode_glock(inode); 997 struct gfs2_inode *ip = GFS2_I(inode); 998 struct gfs2_sbd *sdp = GFS2_SB(inode); 999 1000 if (gfs2_is_jdata(ip) && !gfs2_is_stuffed(ip)) 1001 gfs2_trans_add_databufs(gl, folio, offset_in_folio(folio, pos), 1002 copied); 1003 1004 folio_unlock(folio); 1005 folio_put(folio); 1006 1007 if (gfs2_is_jdata(ip) || gfs2_is_stuffed(ip)) { 1008 if (tr->tr_num_buf_new) 1009 __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 1010 gfs2_trans_end(sdp); 1011 } 1012 } 1013 1014 const struct iomap_write_ops gfs2_iomap_write_ops = { 1015 .get_folio = gfs2_iomap_get_folio, 1016 .put_folio = gfs2_iomap_put_folio, 1017 }; 1018 1019 static int gfs2_iomap_begin_write(struct inode *inode, loff_t pos, 1020 loff_t length, unsigned flags, 1021 struct iomap *iomap, 1022 struct metapath *mp) 1023 { 1024 struct gfs2_inode *ip = GFS2_I(inode); 1025 struct gfs2_sbd *sdp = GFS2_SB(inode); 1026 bool unstuff; 1027 int ret; 1028 1029 unstuff = gfs2_is_stuffed(ip) && 1030 pos + length > gfs2_max_stuffed_size(ip); 1031 1032 if (unstuff || iomap->type == IOMAP_HOLE) { 1033 unsigned int data_blocks, ind_blocks; 1034 struct gfs2_alloc_parms ap = {}; 1035 unsigned int rblocks; 1036 struct gfs2_trans *tr; 1037 1038 gfs2_write_calc_reserv(ip, iomap->length, &data_blocks, 1039 &ind_blocks); 1040 ap.target = data_blocks + ind_blocks; 1041 ret = gfs2_quota_lock_check(ip, &ap); 1042 if (ret) 1043 return ret; 1044 1045 ret = gfs2_inplace_reserve(ip, &ap); 1046 if (ret) 1047 goto out_qunlock; 1048 1049 rblocks = RES_DINODE + ind_blocks; 1050 if (gfs2_is_jdata(ip)) 1051 rblocks += data_blocks; 1052 if (ind_blocks || data_blocks) 1053 rblocks += RES_STATFS + RES_QUOTA; 1054 if (inode == sdp->sd_rindex) 1055 rblocks += 2 * RES_STATFS; 1056 rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks); 1057 1058 ret = gfs2_trans_begin(sdp, rblocks, 1059 iomap->length >> inode->i_blkbits); 1060 if (ret) 1061 goto out_trans_fail; 1062 1063 if (unstuff) { 1064 ret = gfs2_unstuff_dinode(ip); 1065 if (ret) 1066 goto out_trans_end; 1067 release_metapath(mp); 1068 ret = __gfs2_iomap_get(inode, iomap->offset, 1069 iomap->length, flags, iomap, mp); 1070 if (ret) 1071 goto out_trans_end; 1072 } 1073 1074 if (iomap->type == IOMAP_HOLE) { 1075 ret = __gfs2_iomap_alloc(inode, iomap, mp); 1076 if (ret) { 1077 gfs2_trans_end(sdp); 1078 gfs2_inplace_release(ip); 1079 punch_hole(ip, iomap->offset, iomap->length); 1080 goto out_qunlock; 1081 } 1082 } 1083 1084 tr = current->journal_info; 1085 if (tr->tr_num_buf_new) 1086 __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 1087 1088 gfs2_trans_end(sdp); 1089 } 1090 1091 return 0; 1092 1093 out_trans_end: 1094 gfs2_trans_end(sdp); 1095 out_trans_fail: 1096 gfs2_inplace_release(ip); 1097 out_qunlock: 1098 gfs2_quota_unlock(ip); 1099 return ret; 1100 } 1101 1102 static int gfs2_iomap_begin(struct inode *inode, loff_t pos, loff_t length, 1103 unsigned flags, struct iomap *iomap, 1104 struct iomap *srcmap) 1105 { 1106 struct gfs2_inode *ip = GFS2_I(inode); 1107 struct metapath mp = { .mp_aheight = 1, }; 1108 int ret; 1109 1110 if (gfs2_is_jdata(ip)) 1111 iomap->flags |= IOMAP_F_BUFFER_HEAD; 1112 1113 trace_gfs2_iomap_start(ip, pos, length, flags); 1114 ret = __gfs2_iomap_get(inode, pos, length, flags, iomap, &mp); 1115 if (ret) 1116 goto out_unlock; 1117 1118 switch(flags & (IOMAP_WRITE | IOMAP_ZERO)) { 1119 case IOMAP_WRITE: 1120 if (flags & IOMAP_DIRECT) { 1121 /* 1122 * Silently fall back to buffered I/O for stuffed files 1123 * or if we've got a hole (see gfs2_file_direct_write). 1124 */ 1125 if (iomap->type != IOMAP_MAPPED) 1126 ret = -ENOTBLK; 1127 goto out_unlock; 1128 } 1129 break; 1130 case IOMAP_ZERO: 1131 if (iomap->type == IOMAP_HOLE) 1132 goto out_unlock; 1133 break; 1134 default: 1135 goto out; 1136 } 1137 1138 ret = gfs2_iomap_begin_write(inode, pos, length, flags, iomap, &mp); 1139 if (ret) 1140 goto out_unlock; 1141 1142 out: 1143 if (iomap->type == IOMAP_INLINE) { 1144 iomap->private = metapath_dibh(&mp); 1145 get_bh(iomap->private); 1146 } 1147 1148 out_unlock: 1149 release_metapath(&mp); 1150 trace_gfs2_iomap_end(ip, iomap, ret); 1151 return ret; 1152 } 1153 1154 static int gfs2_iomap_end(struct inode *inode, loff_t pos, loff_t length, 1155 ssize_t written, unsigned flags, struct iomap *iomap) 1156 { 1157 struct gfs2_glock *gl = gfs2_inode_glock(inode); 1158 struct gfs2_inode *ip = GFS2_I(inode); 1159 struct gfs2_sbd *sdp = GFS2_SB(inode); 1160 1161 if (iomap->private) 1162 brelse(iomap->private); 1163 1164 switch (flags & (IOMAP_WRITE | IOMAP_ZERO)) { 1165 case IOMAP_WRITE: 1166 if (flags & IOMAP_DIRECT) 1167 return 0; 1168 break; 1169 case IOMAP_ZERO: 1170 if (iomap->type == IOMAP_HOLE) 1171 return 0; 1172 break; 1173 default: 1174 return 0; 1175 } 1176 1177 if (!gfs2_is_stuffed(ip)) 1178 gfs2_ordered_add_inode(ip); 1179 1180 if (inode == sdp->sd_rindex) 1181 adjust_fs_space(inode); 1182 1183 gfs2_inplace_release(ip); 1184 1185 if (ip->i_qadata && ip->i_qadata->qa_qd_num) 1186 gfs2_quota_unlock(ip); 1187 1188 if (length != written && (iomap->flags & IOMAP_F_NEW)) { 1189 /* Deallocate blocks that were just allocated. */ 1190 loff_t hstart = round_up(pos + written, i_blocksize(inode)); 1191 loff_t hend = iomap->offset + iomap->length; 1192 1193 if (hstart < hend) { 1194 truncate_pagecache_range(inode, hstart, hend - 1); 1195 punch_hole(ip, hstart, hend - hstart); 1196 } 1197 } 1198 1199 if (unlikely(!written)) 1200 return 0; 1201 1202 if (iomap->flags & IOMAP_F_SIZE_CHANGED) 1203 mark_inode_dirty(inode); 1204 set_bit(GLF_DIRTY, &gl->gl_flags); 1205 return 0; 1206 } 1207 1208 static DEFINE_IOMAP_ITER_NEXT_END(gfs2_iomap_next, gfs2_iomap_begin, 1209 gfs2_iomap_end); 1210 1211 const struct iomap_ops gfs2_iomap_ops = { 1212 .iomap_next = gfs2_iomap_next, 1213 }; 1214 1215 /** 1216 * gfs2_block_map - Map one or more blocks of an inode to a disk block 1217 * @inode: The inode 1218 * @lblock: The logical block number 1219 * @bh_map: The bh to be mapped 1220 * @create: True if its ok to alloc blocks to satisfy the request 1221 * 1222 * The size of the requested mapping is defined in bh_map->b_size. 1223 * 1224 * Clears buffer_mapped(bh_map) and leaves bh_map->b_size unchanged 1225 * when @lblock is not mapped. Sets buffer_mapped(bh_map) and 1226 * bh_map->b_size to indicate the size of the mapping when @lblock and 1227 * successive blocks are mapped, up to the requested size. 1228 * 1229 * Sets buffer_boundary() if a read of metadata will be required 1230 * before the next block can be mapped. Sets buffer_new() if new 1231 * blocks were allocated. 1232 * 1233 * Returns: errno 1234 */ 1235 1236 int gfs2_block_map(struct inode *inode, sector_t lblock, 1237 struct buffer_head *bh_map, int create) 1238 { 1239 struct gfs2_inode *ip = GFS2_I(inode); 1240 loff_t pos = (loff_t)lblock << inode->i_blkbits; 1241 loff_t length = bh_map->b_size; 1242 struct iomap iomap = { }; 1243 int ret; 1244 1245 clear_buffer_mapped(bh_map); 1246 clear_buffer_new(bh_map); 1247 clear_buffer_boundary(bh_map); 1248 trace_gfs2_bmap(ip, bh_map, lblock, create, 1); 1249 1250 if (!create) 1251 ret = gfs2_iomap_get(inode, pos, length, &iomap); 1252 else 1253 ret = gfs2_iomap_alloc(inode, pos, length, &iomap); 1254 if (ret) 1255 goto out; 1256 1257 if (iomap.length > bh_map->b_size) { 1258 iomap.length = bh_map->b_size; 1259 iomap.flags &= ~IOMAP_F_GFS2_BOUNDARY; 1260 } 1261 if (iomap.addr != IOMAP_NULL_ADDR) 1262 map_bh(bh_map, inode->i_sb, iomap.addr >> inode->i_blkbits); 1263 bh_map->b_size = iomap.length; 1264 if (iomap.flags & IOMAP_F_GFS2_BOUNDARY) 1265 set_buffer_boundary(bh_map); 1266 if (iomap.flags & IOMAP_F_NEW) 1267 set_buffer_new(bh_map); 1268 1269 out: 1270 trace_gfs2_bmap(ip, bh_map, lblock, create, ret); 1271 return ret; 1272 } 1273 1274 int gfs2_get_extent(struct inode *inode, u64 lblock, u64 *dblock, 1275 unsigned int *extlen) 1276 { 1277 unsigned int blkbits = inode->i_blkbits; 1278 struct iomap iomap = { }; 1279 unsigned int len; 1280 int ret; 1281 1282 ret = gfs2_iomap_get(inode, lblock << blkbits, *extlen << blkbits, 1283 &iomap); 1284 if (ret) 1285 return ret; 1286 if (iomap.type != IOMAP_MAPPED) 1287 return -EIO; 1288 *dblock = iomap.addr >> blkbits; 1289 len = iomap.length >> blkbits; 1290 if (len < *extlen) 1291 *extlen = len; 1292 return 0; 1293 } 1294 1295 int gfs2_alloc_extent(struct inode *inode, u64 lblock, u64 *dblock, 1296 unsigned int *extlen, bool *new) 1297 { 1298 unsigned int blkbits = inode->i_blkbits; 1299 struct iomap iomap = { }; 1300 unsigned int len; 1301 int ret; 1302 1303 ret = gfs2_iomap_alloc(inode, lblock << blkbits, *extlen << blkbits, 1304 &iomap); 1305 if (ret) 1306 return ret; 1307 if (iomap.type != IOMAP_MAPPED) 1308 return -EIO; 1309 *dblock = iomap.addr >> blkbits; 1310 len = iomap.length >> blkbits; 1311 if (len < *extlen) 1312 *extlen = len; 1313 *new = iomap.flags & IOMAP_F_NEW; 1314 return 0; 1315 } 1316 1317 /* 1318 * NOTE: Never call gfs2_block_zero_range with an open transaction because it 1319 * uses iomap write to perform its actions, which begin their own transactions 1320 * (iomap_begin, get_folio, etc.) 1321 */ 1322 static int gfs2_block_zero_range(struct inode *inode, loff_t from, loff_t length) 1323 { 1324 BUG_ON(current->journal_info); 1325 if (from >= inode->i_size) 1326 return 0; 1327 length = min(length, inode->i_size - from); 1328 return iomap_zero_range(inode, from, length, NULL, &gfs2_iomap_ops, 1329 &gfs2_iomap_write_ops, NULL); 1330 } 1331 1332 int gfs2_clear_beyond_eof(struct inode *inode, loff_t end) 1333 { 1334 loff_t isize = i_size_read(inode); 1335 unsigned int len = isize & ~PAGE_MASK; 1336 1337 if (!len || isize >= end) 1338 return 0; 1339 len = PAGE_SIZE - len; 1340 if (end - isize < len) 1341 len = end - isize; 1342 return gfs2_block_zero_range(inode, isize, len); 1343 } 1344 1345 #define GFS2_JTRUNC_REVOKES 8192 1346 1347 /** 1348 * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files 1349 * @inode: The inode being truncated 1350 * @oldsize: The original (larger) size 1351 * @newsize: The new smaller size 1352 * 1353 * With jdata files, we have to journal a revoke for each block which is 1354 * truncated. As a result, we need to split this into separate transactions 1355 * if the number of pages being truncated gets too large. 1356 */ 1357 1358 static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize) 1359 { 1360 struct gfs2_sbd *sdp = GFS2_SB(inode); 1361 u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize; 1362 u64 chunk; 1363 int error; 1364 1365 while (oldsize != newsize) { 1366 struct gfs2_trans *tr; 1367 unsigned int offs; 1368 1369 chunk = oldsize - newsize; 1370 if (chunk > max_chunk) 1371 chunk = max_chunk; 1372 1373 offs = oldsize & ~PAGE_MASK; 1374 if (offs && chunk > PAGE_SIZE) 1375 chunk = offs + ((chunk - offs) & PAGE_MASK); 1376 1377 truncate_pagecache(inode, oldsize - chunk); 1378 oldsize -= chunk; 1379 1380 tr = current->journal_info; 1381 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) 1382 continue; 1383 1384 gfs2_trans_end(sdp); 1385 error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES); 1386 if (error) 1387 return error; 1388 } 1389 1390 return 0; 1391 } 1392 1393 static int trunc_start(struct inode *inode, u64 newsize) 1394 { 1395 struct gfs2_glock *gl = gfs2_inode_glock(inode); 1396 struct gfs2_inode *ip = GFS2_I(inode); 1397 struct gfs2_sbd *sdp = GFS2_SB(inode); 1398 struct buffer_head *dibh = NULL; 1399 int journaled = gfs2_is_jdata(ip); 1400 u64 oldsize = inode->i_size; 1401 int error; 1402 1403 if (!gfs2_is_stuffed(ip)) { 1404 unsigned int blocksize = i_blocksize(inode); 1405 unsigned int offs = newsize & (blocksize - 1); 1406 if (offs) { 1407 error = gfs2_block_zero_range(inode, newsize, 1408 blocksize - offs); 1409 if (error) 1410 return error; 1411 } 1412 } 1413 if (journaled) 1414 error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES); 1415 else 1416 error = gfs2_trans_begin(sdp, RES_DINODE, 0); 1417 if (error) 1418 return error; 1419 1420 error = gfs2_meta_inode_buffer(ip, &dibh); 1421 if (error) 1422 goto out; 1423 1424 gfs2_trans_add_meta(gl, dibh); 1425 1426 if (gfs2_is_stuffed(ip)) 1427 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize); 1428 else 1429 ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG; 1430 1431 i_size_write(inode, newsize); 1432 inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode)); 1433 gfs2_dinode_out(ip, dibh->b_data); 1434 1435 if (journaled) 1436 error = gfs2_journaled_truncate(inode, oldsize, newsize); 1437 else 1438 truncate_pagecache(inode, newsize); 1439 1440 out: 1441 brelse(dibh); 1442 if (current->journal_info) 1443 gfs2_trans_end(sdp); 1444 return error; 1445 } 1446 1447 int gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length, 1448 struct iomap *iomap) 1449 { 1450 struct metapath mp = { .mp_aheight = 1, }; 1451 int ret; 1452 1453 ret = __gfs2_iomap_get(inode, pos, length, 0, iomap, &mp); 1454 release_metapath(&mp); 1455 return ret; 1456 } 1457 1458 int gfs2_iomap_alloc(struct inode *inode, loff_t pos, loff_t length, 1459 struct iomap *iomap) 1460 { 1461 struct metapath mp = { .mp_aheight = 1, }; 1462 int ret; 1463 1464 ret = __gfs2_iomap_get(inode, pos, length, IOMAP_WRITE, iomap, &mp); 1465 if (!ret && iomap->type == IOMAP_HOLE) 1466 ret = __gfs2_iomap_alloc(inode, iomap, &mp); 1467 release_metapath(&mp); 1468 return ret; 1469 } 1470 1471 /** 1472 * sweep_bh_for_rgrps - find an rgrp in a meta buffer and free blocks therein 1473 * @ip: inode 1474 * @rd_gh: holder of resource group glock 1475 * @bh: buffer head to sweep 1476 * @start: starting point in bh 1477 * @end: end point in bh 1478 * @meta: true if bh points to metadata (rather than data) 1479 * @btotal: place to keep count of total blocks freed 1480 * 1481 * We sweep a metadata buffer (provided by the metapath) for blocks we need to 1482 * free, and free them all. However, we do it one rgrp at a time. If this 1483 * block has references to multiple rgrps, we break it into individual 1484 * transactions. This allows other processes to use the rgrps while we're 1485 * focused on a single one, for better concurrency / performance. 1486 * At every transaction boundary, we rewrite the inode into the journal. 1487 * That way the bitmaps are kept consistent with the inode and we can recover 1488 * if we're interrupted by power-outages. 1489 * 1490 * Returns: 0, or return code if an error occurred. 1491 * *btotal has the total number of blocks freed 1492 */ 1493 static int sweep_bh_for_rgrps(struct gfs2_inode *ip, struct gfs2_holder *rd_gh, 1494 struct buffer_head *bh, __be64 *start, __be64 *end, 1495 bool meta, u32 *btotal) 1496 { 1497 struct inode *inode = &ip->i_inode; 1498 struct gfs2_glock *gl = gfs2_inode_glock(inode); 1499 struct gfs2_sbd *sdp = GFS2_SB(inode); 1500 struct gfs2_rgrpd *rgd; 1501 struct gfs2_trans *tr; 1502 __be64 *p; 1503 int blks_outside_rgrp; 1504 u64 bn, bstart, isize_blks; 1505 s64 blen; /* needs to be s64 or gfs2_add_inode_blocks breaks */ 1506 int ret = 0; 1507 bool buf_in_tr = false; /* buffer was added to transaction */ 1508 1509 more_rgrps: 1510 rgd = NULL; 1511 if (gfs2_holder_initialized(rd_gh)) { 1512 rgd = gfs2_glock2rgrp(rd_gh->gh_gl); 1513 gfs2_assert_withdraw(sdp, 1514 gfs2_glock_is_locked_by_me(rd_gh->gh_gl)); 1515 } 1516 blks_outside_rgrp = 0; 1517 bstart = 0; 1518 blen = 0; 1519 1520 for (p = start; p < end; p++) { 1521 if (!*p) 1522 continue; 1523 bn = be64_to_cpu(*p); 1524 1525 if (rgd) { 1526 if (!rgrp_contains_block(rgd, bn)) { 1527 blks_outside_rgrp++; 1528 continue; 1529 } 1530 } else { 1531 rgd = gfs2_blk2rgrpd(sdp, bn, true); 1532 if (unlikely(!rgd)) { 1533 ret = -EIO; 1534 goto out; 1535 } 1536 ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 1537 LM_FLAG_NODE_SCOPE, rd_gh); 1538 if (ret) 1539 goto out; 1540 1541 /* Must be done with the rgrp glock held: */ 1542 if (gfs2_rs_active(&ip->i_res) && 1543 rgd == ip->i_res.rs_rgd) 1544 gfs2_rs_deltree(&ip->i_res); 1545 } 1546 1547 /* The size of our transactions will be unknown until we 1548 actually process all the metadata blocks that relate to 1549 the rgrp. So we estimate. We know it can't be more than 1550 the dinode's i_blocks and we don't want to exceed the 1551 journal flush threshold, sd_log_thresh2. */ 1552 if (current->journal_info == NULL) { 1553 unsigned int jblocks_rqsted, revokes; 1554 1555 jblocks_rqsted = rgd->rd_length + RES_DINODE + 1556 RES_INDIRECT; 1557 isize_blks = gfs2_get_inode_blocks(inode); 1558 if (isize_blks > atomic_read(&sdp->sd_log_thresh2)) 1559 jblocks_rqsted += 1560 atomic_read(&sdp->sd_log_thresh2); 1561 else 1562 jblocks_rqsted += isize_blks; 1563 revokes = jblocks_rqsted; 1564 if (meta) 1565 revokes += end - start; 1566 else if (ip->i_diskflags & GFS2_DIF_EXHASH) 1567 revokes += sdp->sd_inptrs; 1568 ret = gfs2_trans_begin(sdp, jblocks_rqsted, revokes); 1569 if (ret) 1570 goto out_unlock; 1571 down_write(&ip->i_rw_mutex); 1572 } 1573 /* check if we will exceed the transaction blocks requested */ 1574 tr = current->journal_info; 1575 if (tr->tr_num_buf_new + RES_STATFS + 1576 RES_QUOTA >= atomic_read(&sdp->sd_log_thresh2)) { 1577 /* We set blks_outside_rgrp to ensure the loop will 1578 be repeated for the same rgrp, but with a new 1579 transaction. */ 1580 blks_outside_rgrp++; 1581 /* This next part is tricky. If the buffer was added 1582 to the transaction, we've already set some block 1583 pointers to 0, so we better follow through and free 1584 them, or we will introduce corruption (so break). 1585 This may be impossible, or at least rare, but I 1586 decided to cover the case regardless. 1587 1588 If the buffer was not added to the transaction 1589 (this call), doing so would exceed our transaction 1590 size, so we need to end the transaction and start a 1591 new one (so goto). */ 1592 1593 if (buf_in_tr) 1594 break; 1595 goto out_unlock; 1596 } 1597 1598 gfs2_trans_add_meta(gl, bh); 1599 buf_in_tr = true; 1600 *p = 0; 1601 if (bstart + blen == bn) { 1602 blen++; 1603 continue; 1604 } 1605 if (bstart) { 1606 __gfs2_free_blocks(ip, rgd, bstart, (u32)blen, meta); 1607 (*btotal) += blen; 1608 gfs2_add_inode_blocks(inode, -blen); 1609 } 1610 bstart = bn; 1611 blen = 1; 1612 } 1613 if (bstart) { 1614 __gfs2_free_blocks(ip, rgd, bstart, (u32)blen, meta); 1615 (*btotal) += blen; 1616 gfs2_add_inode_blocks(inode, -blen); 1617 } 1618 out_unlock: 1619 if (!ret && blks_outside_rgrp) { /* If buffer still has non-zero blocks 1620 outside the rgrp we just processed, 1621 do it all over again. */ 1622 if (current->journal_info) { 1623 struct buffer_head *dibh; 1624 1625 ret = gfs2_meta_inode_buffer(ip, &dibh); 1626 if (ret) 1627 goto out; 1628 1629 /* Every transaction boundary, we rewrite the dinode 1630 to keep its di_blocks current in case of failure. */ 1631 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 1632 gfs2_trans_add_meta(gl, dibh); 1633 gfs2_dinode_out(ip, dibh->b_data); 1634 brelse(dibh); 1635 up_write(&ip->i_rw_mutex); 1636 gfs2_trans_end(sdp); 1637 buf_in_tr = false; 1638 } 1639 gfs2_glock_dq_uninit(rd_gh); 1640 cond_resched(); 1641 goto more_rgrps; 1642 } 1643 out: 1644 return ret; 1645 } 1646 1647 static bool mp_eq_to_hgt(struct metapath *mp, __u16 *list, unsigned int h) 1648 { 1649 if (memcmp(mp->mp_list, list, h * sizeof(mp->mp_list[0]))) 1650 return false; 1651 return true; 1652 } 1653 1654 /** 1655 * find_nonnull_ptr - find a non-null pointer given a metapath and height 1656 * @sdp: The superblock 1657 * @mp: starting metapath 1658 * @h: desired height to search 1659 * @end_list: See punch_hole(). 1660 * @end_aligned: See punch_hole(). 1661 * 1662 * Assumes the metapath is valid (with buffers) out to height h. 1663 * Returns: true if a non-null pointer was found in the metapath buffer 1664 * false if all remaining pointers are NULL in the buffer 1665 */ 1666 static bool find_nonnull_ptr(struct gfs2_sbd *sdp, struct metapath *mp, 1667 unsigned int h, 1668 __u16 *end_list, unsigned int end_aligned) 1669 { 1670 struct buffer_head *bh = mp->mp_bh[h]; 1671 __be64 *first, *ptr, *end; 1672 1673 first = metaptr1(h, mp); 1674 ptr = first + mp->mp_list[h]; 1675 end = (__be64 *)(bh->b_data + bh->b_size); 1676 if (end_list && mp_eq_to_hgt(mp, end_list, h)) { 1677 bool keep_end = h < end_aligned; 1678 end = first + end_list[h] + keep_end; 1679 } 1680 1681 while (ptr < end) { 1682 if (*ptr) { /* if we have a non-null pointer */ 1683 mp->mp_list[h] = ptr - first; 1684 h++; 1685 if (h < GFS2_MAX_META_HEIGHT) 1686 mp->mp_list[h] = 0; 1687 return true; 1688 } 1689 ptr++; 1690 } 1691 return false; 1692 } 1693 1694 enum dealloc_states { 1695 DEALLOC_MP_FULL = 0, /* Strip a metapath with all buffers read in */ 1696 DEALLOC_MP_LOWER = 1, /* lower the metapath strip height */ 1697 DEALLOC_FILL_MP = 2, /* Fill in the metapath to the given height. */ 1698 DEALLOC_DONE = 3, /* process complete */ 1699 }; 1700 1701 static inline void 1702 metapointer_range(struct metapath *mp, unsigned int height, 1703 __u16 *start_list, unsigned int start_aligned, 1704 __u16 *end_list, unsigned int end_aligned, 1705 __be64 **start, __be64 **end) 1706 { 1707 struct buffer_head *bh = mp->mp_bh[height]; 1708 __be64 *first; 1709 1710 first = metaptr1(height, mp); 1711 *start = first; 1712 if (mp_eq_to_hgt(mp, start_list, height)) { 1713 bool keep_start = height < start_aligned; 1714 *start = first + start_list[height] + keep_start; 1715 } 1716 *end = (__be64 *)(bh->b_data + bh->b_size); 1717 if (end_list && mp_eq_to_hgt(mp, end_list, height)) { 1718 bool keep_end = height < end_aligned; 1719 *end = first + end_list[height] + keep_end; 1720 } 1721 } 1722 1723 static inline bool walk_done(struct gfs2_sbd *sdp, 1724 struct metapath *mp, int height, 1725 __u16 *end_list, unsigned int end_aligned) 1726 { 1727 __u16 end; 1728 1729 if (end_list) { 1730 bool keep_end = height < end_aligned; 1731 if (!mp_eq_to_hgt(mp, end_list, height)) 1732 return false; 1733 end = end_list[height] + keep_end; 1734 } else 1735 end = (height > 0) ? sdp->sd_inptrs : sdp->sd_diptrs; 1736 return mp->mp_list[height] >= end; 1737 } 1738 1739 /** 1740 * punch_hole - deallocate blocks in a file 1741 * @ip: inode to truncate 1742 * @offset: the start of the hole 1743 * @length: the size of the hole (or 0 for truncate) 1744 * 1745 * Punch a hole into a file or truncate a file at a given position. This 1746 * function operates in whole blocks (@offset and @length are rounded 1747 * accordingly); partially filled blocks must be cleared otherwise. 1748 * 1749 * This function works from the bottom up, and from the right to the left. In 1750 * other words, it strips off the highest layer (data) before stripping any of 1751 * the metadata. Doing it this way is best in case the operation is interrupted 1752 * by power failure, etc. The dinode is rewritten in every transaction to 1753 * guarantee integrity. 1754 */ 1755 static int punch_hole(struct gfs2_inode *ip, u64 offset, u64 length) 1756 { 1757 struct inode *inode = &ip->i_inode; 1758 struct gfs2_glock *gl = gfs2_inode_glock(inode); 1759 struct gfs2_sbd *sdp = GFS2_SB(inode); 1760 u64 maxsize = sdp->sd_heightsize[ip->i_height]; 1761 struct metapath mp = {}; 1762 struct buffer_head *dibh, *bh; 1763 struct gfs2_holder rd_gh; 1764 unsigned int bsize_shift = sdp->sd_sb.sb_bsize_shift; 1765 unsigned int bsize = 1 << bsize_shift; 1766 u64 lblock = (offset + bsize - 1) >> bsize_shift; 1767 __u16 start_list[GFS2_MAX_META_HEIGHT]; 1768 __u16 __end_list[GFS2_MAX_META_HEIGHT], *end_list = NULL; 1769 unsigned int start_aligned, end_aligned; 1770 unsigned int strip_h = ip->i_height - 1; 1771 u32 btotal = 0; 1772 int ret, state; 1773 unsigned int mp_h; /* metapath buffers are read in to this height */ 1774 u64 prev_bnr = 0; 1775 __be64 *start, *end; 1776 1777 if (offset + bsize - 1 >= maxsize) { 1778 /* 1779 * The starting point lies beyond the allocated metadata; 1780 * there are no blocks to deallocate. 1781 */ 1782 return 0; 1783 } 1784 1785 /* 1786 * The start position of the hole is defined by lblock, start_list, and 1787 * start_aligned. The end position of the hole is defined by lend, 1788 * end_list, and end_aligned. 1789 * 1790 * start_aligned and end_aligned define down to which height the start 1791 * and end positions are aligned to the metadata tree (i.e., the 1792 * position is a multiple of the metadata granularity at the height 1793 * above). This determines at which heights additional meta pointers 1794 * needs to be preserved for the remaining data. 1795 */ 1796 1797 if (length) { 1798 u64 end_offset = offset + length; 1799 u64 lend; 1800 1801 /* 1802 * Clip the end at the maximum file size for the given height: 1803 * that's how far the metadata goes; files bigger than that 1804 * will have additional layers of indirection. 1805 */ 1806 if (end_offset > maxsize) 1807 end_offset = maxsize; 1808 lend = end_offset >> bsize_shift; 1809 1810 if (lblock >= lend) 1811 return 0; 1812 1813 find_metapath(sdp, lend, &mp, ip->i_height); 1814 end_list = __end_list; 1815 memcpy(end_list, mp.mp_list, sizeof(mp.mp_list)); 1816 1817 for (mp_h = ip->i_height - 1; mp_h > 0; mp_h--) { 1818 if (end_list[mp_h]) 1819 break; 1820 } 1821 end_aligned = mp_h; 1822 } 1823 1824 find_metapath(sdp, lblock, &mp, ip->i_height); 1825 memcpy(start_list, mp.mp_list, sizeof(start_list)); 1826 1827 for (mp_h = ip->i_height - 1; mp_h > 0; mp_h--) { 1828 if (start_list[mp_h]) 1829 break; 1830 } 1831 start_aligned = mp_h; 1832 1833 ret = gfs2_meta_inode_buffer(ip, &dibh); 1834 if (ret) 1835 return ret; 1836 1837 mp.mp_bh[0] = dibh; 1838 ret = lookup_metapath(ip, &mp); 1839 if (ret) 1840 goto out_metapath; 1841 1842 /* issue read-ahead on metadata */ 1843 for (mp_h = 0; mp_h < mp.mp_aheight - 1; mp_h++) { 1844 metapointer_range(&mp, mp_h, start_list, start_aligned, 1845 end_list, end_aligned, &start, &end); 1846 gfs2_metapath_ra(gl, start, end); 1847 } 1848 1849 if (mp.mp_aheight == ip->i_height) 1850 state = DEALLOC_MP_FULL; /* We have a complete metapath */ 1851 else 1852 state = DEALLOC_FILL_MP; /* deal with partial metapath */ 1853 1854 ret = gfs2_rindex_update(sdp); 1855 if (ret) 1856 goto out_metapath; 1857 1858 ret = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); 1859 if (ret) 1860 goto out_metapath; 1861 gfs2_holder_mark_uninitialized(&rd_gh); 1862 1863 mp_h = strip_h; 1864 1865 while (state != DEALLOC_DONE) { 1866 switch (state) { 1867 /* Truncate a full metapath at the given strip height. 1868 * Note that strip_h == mp_h in order to be in this state. */ 1869 case DEALLOC_MP_FULL: 1870 bh = mp.mp_bh[mp_h]; 1871 gfs2_assert_withdraw(sdp, bh); 1872 if (gfs2_assert_withdraw(sdp, 1873 prev_bnr != bh->b_blocknr)) { 1874 fs_emerg(sdp, "inode %llu, block:%llu, i_h:%u, " 1875 "s_h:%u, mp_h:%u\n", 1876 (unsigned long long)ip->i_no_addr, 1877 prev_bnr, ip->i_height, strip_h, mp_h); 1878 } 1879 prev_bnr = bh->b_blocknr; 1880 1881 if (gfs2_metatype_check(sdp, bh, 1882 (mp_h ? GFS2_METATYPE_IN : 1883 GFS2_METATYPE_DI))) { 1884 ret = -EIO; 1885 goto out; 1886 } 1887 1888 /* 1889 * Below, passing end_aligned as 0 gives us the 1890 * metapointer range excluding the end point: the end 1891 * point is the first metapath we must not deallocate! 1892 */ 1893 1894 metapointer_range(&mp, mp_h, start_list, start_aligned, 1895 end_list, 0 /* end_aligned */, 1896 &start, &end); 1897 ret = sweep_bh_for_rgrps(ip, &rd_gh, mp.mp_bh[mp_h], 1898 start, end, 1899 mp_h != ip->i_height - 1, 1900 &btotal); 1901 1902 /* If we hit an error or just swept dinode buffer, 1903 just exit. */ 1904 if (ret || !mp_h) { 1905 state = DEALLOC_DONE; 1906 break; 1907 } 1908 state = DEALLOC_MP_LOWER; 1909 break; 1910 1911 /* lower the metapath strip height */ 1912 case DEALLOC_MP_LOWER: 1913 /* We're done with the current buffer, so release it, 1914 unless it's the dinode buffer. Then back up to the 1915 previous pointer. */ 1916 if (mp_h) { 1917 brelse(mp.mp_bh[mp_h]); 1918 mp.mp_bh[mp_h] = NULL; 1919 } 1920 /* If we can't get any lower in height, we've stripped 1921 off all we can. Next step is to back up and start 1922 stripping the previous level of metadata. */ 1923 if (mp_h == 0) { 1924 strip_h--; 1925 memcpy(mp.mp_list, start_list, sizeof(start_list)); 1926 mp_h = strip_h; 1927 state = DEALLOC_FILL_MP; 1928 break; 1929 } 1930 mp.mp_list[mp_h] = 0; 1931 mp_h--; /* search one metadata height down */ 1932 mp.mp_list[mp_h]++; 1933 if (walk_done(sdp, &mp, mp_h, end_list, end_aligned)) 1934 break; 1935 /* Here we've found a part of the metapath that is not 1936 * allocated. We need to search at that height for the 1937 * next non-null pointer. */ 1938 if (find_nonnull_ptr(sdp, &mp, mp_h, end_list, end_aligned)) { 1939 state = DEALLOC_FILL_MP; 1940 mp_h++; 1941 } 1942 /* No more non-null pointers at this height. Back up 1943 to the previous height and try again. */ 1944 break; /* loop around in the same state */ 1945 1946 /* Fill the metapath with buffers to the given height. */ 1947 case DEALLOC_FILL_MP: 1948 /* Fill the buffers out to the current height. */ 1949 ret = fillup_metapath(ip, &mp, mp_h); 1950 if (ret < 0) 1951 goto out; 1952 1953 /* On the first pass, issue read-ahead on metadata. */ 1954 if (mp.mp_aheight > 1 && strip_h == ip->i_height - 1) { 1955 unsigned int height = mp.mp_aheight - 1; 1956 1957 /* No read-ahead for data blocks. */ 1958 if (mp.mp_aheight - 1 == strip_h) 1959 height--; 1960 1961 for (; height >= mp.mp_aheight - ret; height--) { 1962 metapointer_range(&mp, height, 1963 start_list, start_aligned, 1964 end_list, end_aligned, 1965 &start, &end); 1966 gfs2_metapath_ra(gl, start, end); 1967 } 1968 } 1969 1970 /* If buffers found for the entire strip height */ 1971 if (mp.mp_aheight - 1 == strip_h) { 1972 state = DEALLOC_MP_FULL; 1973 break; 1974 } 1975 if (mp.mp_aheight < ip->i_height) /* We have a partial height */ 1976 mp_h = mp.mp_aheight - 1; 1977 1978 /* If we find a non-null block pointer, crawl a bit 1979 higher up in the metapath and try again, otherwise 1980 we need to look lower for a new starting point. */ 1981 if (find_nonnull_ptr(sdp, &mp, mp_h, end_list, end_aligned)) 1982 mp_h++; 1983 else 1984 state = DEALLOC_MP_LOWER; 1985 break; 1986 } 1987 } 1988 1989 if (btotal) { 1990 if (current->journal_info == NULL) { 1991 ret = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + 1992 RES_QUOTA, 0); 1993 if (ret) 1994 goto out; 1995 down_write(&ip->i_rw_mutex); 1996 } 1997 gfs2_statfs_change(sdp, 0, +btotal, 0); 1998 gfs2_quota_change(ip, -(s64)btotal, inode->i_uid, inode->i_gid); 1999 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 2000 gfs2_trans_add_meta(gl, dibh); 2001 gfs2_dinode_out(ip, dibh->b_data); 2002 up_write(&ip->i_rw_mutex); 2003 gfs2_trans_end(sdp); 2004 } 2005 2006 out: 2007 if (gfs2_holder_initialized(&rd_gh)) 2008 gfs2_glock_dq_uninit(&rd_gh); 2009 if (current->journal_info) { 2010 up_write(&ip->i_rw_mutex); 2011 gfs2_trans_end(sdp); 2012 cond_resched(); 2013 } 2014 gfs2_quota_unhold(ip); 2015 out_metapath: 2016 release_metapath(&mp); 2017 return ret; 2018 } 2019 2020 static int trunc_end(struct gfs2_inode *ip) 2021 { 2022 struct inode *inode = &ip->i_inode; 2023 struct gfs2_glock *gl = gfs2_inode_glock(inode); 2024 struct gfs2_sbd *sdp = GFS2_SB(inode); 2025 struct buffer_head *dibh; 2026 int error; 2027 2028 error = gfs2_trans_begin(sdp, RES_DINODE, 0); 2029 if (error) 2030 return error; 2031 2032 down_write(&ip->i_rw_mutex); 2033 2034 error = gfs2_meta_inode_buffer(ip, &dibh); 2035 if (error) 2036 goto out; 2037 2038 if (!i_size_read(inode)) { 2039 ip->i_height = 0; 2040 ip->i_goal = ip->i_no_addr; 2041 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); 2042 gfs2_ordered_del_inode(ip); 2043 } 2044 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 2045 ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG; 2046 2047 gfs2_trans_add_meta(gl, dibh); 2048 gfs2_dinode_out(ip, dibh->b_data); 2049 brelse(dibh); 2050 2051 out: 2052 up_write(&ip->i_rw_mutex); 2053 gfs2_trans_end(sdp); 2054 return error; 2055 } 2056 2057 /** 2058 * do_shrink - make a file smaller 2059 * @inode: the inode 2060 * @newsize: the size to make the file 2061 * 2062 * Called with an exclusive lock on @inode. The @size must 2063 * be equal to or smaller than the current inode size. 2064 * 2065 * Returns: errno 2066 */ 2067 2068 static int do_shrink(struct inode *inode, u64 newsize) 2069 { 2070 struct gfs2_inode *ip = GFS2_I(inode); 2071 int error; 2072 2073 error = trunc_start(inode, newsize); 2074 if (error < 0) 2075 return error; 2076 if (gfs2_is_stuffed(ip)) 2077 return 0; 2078 2079 error = punch_hole(ip, newsize, 0); 2080 if (error == 0) 2081 error = trunc_end(ip); 2082 2083 return error; 2084 } 2085 2086 /** 2087 * do_grow - Touch and update inode size 2088 * @inode: The inode 2089 * @size: The new size 2090 * 2091 * This function updates the timestamps on the inode and 2092 * may also increase the size of the inode. This function 2093 * must not be called with @size any smaller than the current 2094 * inode size. 2095 * 2096 * Although it is not strictly required to unstuff files here, 2097 * earlier versions of GFS2 have a bug in the stuffed file reading 2098 * code which will result in a buffer overrun if the size is larger 2099 * than the max stuffed file size. In order to prevent this from 2100 * occurring, such files are unstuffed, but in other cases we can 2101 * just update the inode size directly. 2102 * 2103 * Returns: 0 on success, or -ve on error 2104 */ 2105 2106 static int do_grow(struct inode *inode, u64 size) 2107 { 2108 struct gfs2_glock *gl = gfs2_inode_glock(inode); 2109 struct gfs2_inode *ip = GFS2_I(inode); 2110 struct gfs2_sbd *sdp = GFS2_SB(inode); 2111 struct gfs2_alloc_parms ap = { .target = 1, }; 2112 struct buffer_head *dibh; 2113 int error; 2114 int unstuff = 0; 2115 2116 if (gfs2_is_stuffed(ip) && size > gfs2_max_stuffed_size(ip)) { 2117 error = gfs2_quota_lock_check(ip, &ap); 2118 if (error) 2119 return error; 2120 2121 error = gfs2_inplace_reserve(ip, &ap); 2122 if (error) 2123 goto do_grow_qunlock; 2124 unstuff = 1; 2125 } 2126 2127 if (!unstuff) { 2128 error = gfs2_clear_beyond_eof(inode, size); 2129 if (error) 2130 goto do_grow_qunlock; 2131 } 2132 2133 error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT + 2134 (unstuff && 2135 gfs2_is_jdata(ip) ? RES_JDATA : 0) + 2136 (sdp->sd_args.ar_quota == GFS2_QUOTA_OFF ? 2137 0 : RES_QUOTA), 0); 2138 if (error) 2139 goto do_grow_release; 2140 2141 if (unstuff) { 2142 error = gfs2_unstuff_dinode(ip); 2143 if (error) 2144 goto do_end_trans; 2145 } 2146 2147 error = gfs2_meta_inode_buffer(ip, &dibh); 2148 if (error) 2149 goto do_end_trans; 2150 2151 truncate_setsize(inode, size); 2152 inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode)); 2153 gfs2_trans_add_meta(gl, dibh); 2154 gfs2_dinode_out(ip, dibh->b_data); 2155 brelse(dibh); 2156 2157 do_end_trans: 2158 gfs2_trans_end(sdp); 2159 do_grow_release: 2160 if (unstuff) { 2161 gfs2_inplace_release(ip); 2162 do_grow_qunlock: 2163 gfs2_quota_unlock(ip); 2164 } 2165 return error; 2166 } 2167 2168 /** 2169 * gfs2_setattr_size - make a file a given size 2170 * @inode: the inode 2171 * @newsize: the size to make the file 2172 * 2173 * The file size can grow, shrink, or stay the same size. This 2174 * is called holding i_rwsem and an exclusive glock on the inode 2175 * in question. 2176 * 2177 * Returns: errno 2178 */ 2179 2180 int gfs2_setattr_size(struct inode *inode, u64 newsize) 2181 { 2182 struct gfs2_inode *ip = GFS2_I(inode); 2183 int ret; 2184 2185 BUG_ON(!S_ISREG(inode->i_mode)); 2186 2187 ret = inode_newsize_ok(inode, newsize); 2188 if (ret) 2189 return ret; 2190 2191 inode_dio_wait(inode); 2192 2193 ret = gfs2_qa_get(ip); 2194 if (ret) 2195 goto out; 2196 2197 if (newsize >= inode->i_size) { 2198 ret = do_grow(inode, newsize); 2199 goto out; 2200 } 2201 2202 ret = do_shrink(inode, newsize); 2203 out: 2204 gfs2_rs_delete(ip); 2205 gfs2_qa_put(ip); 2206 return ret; 2207 } 2208 2209 int gfs2_truncatei_resume(struct gfs2_inode *ip) 2210 { 2211 int error; 2212 error = punch_hole(ip, i_size_read(&ip->i_inode), 0); 2213 if (!error) 2214 error = trunc_end(ip); 2215 return error; 2216 } 2217 2218 int gfs2_file_dealloc(struct gfs2_inode *ip) 2219 { 2220 return punch_hole(ip, 0, 0); 2221 } 2222 2223 /** 2224 * gfs2_free_journal_extents - Free cached journal bmap info 2225 * @jd: The journal 2226 * 2227 */ 2228 2229 void gfs2_free_journal_extents(struct gfs2_jdesc *jd) 2230 { 2231 struct gfs2_journal_extent *jext; 2232 2233 while(!list_empty(&jd->extent_list)) { 2234 jext = list_first_entry(&jd->extent_list, struct gfs2_journal_extent, list); 2235 list_del(&jext->list); 2236 kfree(jext); 2237 } 2238 } 2239 2240 /** 2241 * gfs2_add_jextent - Add or merge a new extent to extent cache 2242 * @jd: The journal descriptor 2243 * @lblock: The logical block at start of new extent 2244 * @dblock: The physical block at start of new extent 2245 * @blocks: Size of extent in fs blocks 2246 * 2247 * Returns: 0 on success or -ENOMEM 2248 */ 2249 2250 static int gfs2_add_jextent(struct gfs2_jdesc *jd, u64 lblock, u64 dblock, u64 blocks) 2251 { 2252 struct gfs2_journal_extent *jext; 2253 2254 if (!list_empty(&jd->extent_list)) { 2255 jext = list_last_entry(&jd->extent_list, struct gfs2_journal_extent, list); 2256 if ((jext->dblock + jext->blocks) == dblock) { 2257 jext->blocks += blocks; 2258 return 0; 2259 } 2260 } 2261 2262 jext = kzalloc_obj(struct gfs2_journal_extent, GFP_NOFS); 2263 if (jext == NULL) 2264 return -ENOMEM; 2265 jext->dblock = dblock; 2266 jext->lblock = lblock; 2267 jext->blocks = blocks; 2268 list_add_tail(&jext->list, &jd->extent_list); 2269 jd->nr_extents++; 2270 return 0; 2271 } 2272 2273 /** 2274 * gfs2_map_journal_extents - Cache journal bmap info 2275 * @sdp: The super block 2276 * @jd: The journal to map 2277 * 2278 * Create a reusable "extent" mapping from all logical 2279 * blocks to all physical blocks for the given journal. This will save 2280 * us time when writing journal blocks. Most journals will have only one 2281 * extent that maps all their logical blocks. That's because gfs2.mkfs 2282 * arranges the journal blocks sequentially to maximize performance. 2283 * So the extent would map the first block for the entire file length. 2284 * However, gfs2_jadd can happen while file activity is happening, so 2285 * those journals may not be sequential. Less likely is the case where 2286 * the users created their own journals by mounting the metafs and 2287 * laying it out. But it's still possible. These journals might have 2288 * several extents. 2289 * 2290 * Returns: 0 on success, or error on failure 2291 */ 2292 2293 int gfs2_map_journal_extents(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd) 2294 { 2295 u64 lblock = 0; 2296 u64 lblock_stop; 2297 struct gfs2_inode *ip = GFS2_I(jd->jd_inode); 2298 struct buffer_head bh; 2299 unsigned int shift = sdp->sd_sb.sb_bsize_shift; 2300 u64 size; 2301 int rc; 2302 ktime_t start, end; 2303 2304 start = ktime_get(); 2305 lblock_stop = i_size_read(jd->jd_inode) >> shift; 2306 size = (lblock_stop - lblock) << shift; 2307 jd->nr_extents = 0; 2308 WARN_ON(!list_empty(&jd->extent_list)); 2309 2310 do { 2311 bh.b_state = 0; 2312 bh.b_blocknr = 0; 2313 bh.b_size = size; 2314 rc = gfs2_block_map(jd->jd_inode, lblock, &bh, 0); 2315 if (rc || !buffer_mapped(&bh)) 2316 goto fail; 2317 rc = gfs2_add_jextent(jd, lblock, bh.b_blocknr, bh.b_size >> shift); 2318 if (rc) 2319 goto fail; 2320 size -= bh.b_size; 2321 lblock += (bh.b_size >> ip->i_inode.i_blkbits); 2322 } while(size > 0); 2323 2324 end = ktime_get(); 2325 fs_info(sdp, "journal %d mapped with %u extents in %lldms\n", jd->jd_jid, 2326 jd->nr_extents, ktime_ms_delta(end, start)); 2327 return 0; 2328 2329 fail: 2330 fs_warn(sdp, "error %d mapping journal %u at offset %llu (extent %u)\n", 2331 rc, jd->jd_jid, 2332 (unsigned long long)(i_size_read(jd->jd_inode) - size), 2333 jd->nr_extents); 2334 fs_warn(sdp, "bmap=%d lblock=%llu block=%llu, state=0x%08lx, size=%llu\n", 2335 rc, (unsigned long long)lblock, (unsigned long long)bh.b_blocknr, 2336 bh.b_state, (unsigned long long)bh.b_size); 2337 gfs2_free_journal_extents(jd); 2338 return rc; 2339 } 2340 2341 /** 2342 * gfs2_write_alloc_required - figure out if a write will require an allocation 2343 * @ip: the file being written to 2344 * @offset: the offset to write to 2345 * @len: the number of bytes being written 2346 * 2347 * Returns: 1 if an alloc is required, 0 otherwise 2348 */ 2349 2350 int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset, 2351 unsigned int len) 2352 { 2353 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 2354 struct buffer_head bh; 2355 unsigned int shift; 2356 u64 lblock, lblock_stop, size; 2357 u64 end_of_file; 2358 2359 if (!len) 2360 return 0; 2361 2362 if (gfs2_is_stuffed(ip)) { 2363 if (offset + len > gfs2_max_stuffed_size(ip)) 2364 return 1; 2365 return 0; 2366 } 2367 2368 shift = sdp->sd_sb.sb_bsize_shift; 2369 BUG_ON(gfs2_is_dir(ip)); 2370 end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift; 2371 lblock = offset >> shift; 2372 lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift; 2373 if (lblock_stop > end_of_file && ip != GFS2_I(sdp->sd_rindex)) 2374 return 1; 2375 2376 size = (lblock_stop - lblock) << shift; 2377 do { 2378 bh.b_state = 0; 2379 bh.b_size = size; 2380 gfs2_block_map(&ip->i_inode, lblock, &bh, 0); 2381 if (!buffer_mapped(&bh)) 2382 return 1; 2383 size -= bh.b_size; 2384 lblock += (bh.b_size >> ip->i_inode.i_blkbits); 2385 } while(size > 0); 2386 2387 return 0; 2388 } 2389 2390 static int stuffed_zero_range(struct inode *inode, loff_t offset, loff_t length) 2391 { 2392 struct gfs2_glock *gl = gfs2_inode_glock(inode); 2393 struct gfs2_inode *ip = GFS2_I(inode); 2394 struct buffer_head *dibh; 2395 int error; 2396 2397 if (offset >= inode->i_size) 2398 return 0; 2399 if (offset + length > inode->i_size) 2400 length = inode->i_size - offset; 2401 2402 error = gfs2_meta_inode_buffer(ip, &dibh); 2403 if (error) 2404 return error; 2405 gfs2_trans_add_meta(gl, dibh); 2406 memset(dibh->b_data + sizeof(struct gfs2_dinode) + offset, 0, 2407 length); 2408 brelse(dibh); 2409 return 0; 2410 } 2411 2412 static int gfs2_journaled_truncate_range(struct inode *inode, loff_t offset, 2413 loff_t length) 2414 { 2415 struct gfs2_sbd *sdp = GFS2_SB(inode); 2416 loff_t max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize; 2417 int error; 2418 2419 while (length) { 2420 struct gfs2_trans *tr; 2421 loff_t chunk; 2422 unsigned int offs; 2423 2424 chunk = length; 2425 if (chunk > max_chunk) 2426 chunk = max_chunk; 2427 2428 offs = offset & ~PAGE_MASK; 2429 if (offs && chunk > PAGE_SIZE) 2430 chunk = offs + ((chunk - offs) & PAGE_MASK); 2431 2432 truncate_pagecache_range(inode, offset, offset + chunk - 1); 2433 offset += chunk; 2434 length -= chunk; 2435 2436 tr = current->journal_info; 2437 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) 2438 continue; 2439 2440 gfs2_trans_end(sdp); 2441 error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES); 2442 if (error) 2443 return error; 2444 } 2445 return 0; 2446 } 2447 2448 int __gfs2_punch_hole(struct file *file, loff_t offset, loff_t length) 2449 { 2450 struct inode *inode = file_inode(file); 2451 struct gfs2_inode *ip = GFS2_I(inode); 2452 struct gfs2_sbd *sdp = GFS2_SB(inode); 2453 unsigned int blocksize = i_blocksize(inode); 2454 loff_t start, end; 2455 int error; 2456 2457 if (!gfs2_is_stuffed(ip)) { 2458 unsigned int start_off, end_len; 2459 2460 start_off = offset & (blocksize - 1); 2461 end_len = (offset + length) & (blocksize - 1); 2462 if (start_off) { 2463 unsigned int len = length; 2464 if (length > blocksize - start_off) 2465 len = blocksize - start_off; 2466 error = gfs2_block_zero_range(inode, offset, len); 2467 if (error) 2468 goto out; 2469 if (start_off + length < blocksize) 2470 end_len = 0; 2471 } 2472 if (end_len) { 2473 error = gfs2_block_zero_range(inode, 2474 offset + length - end_len, end_len); 2475 if (error) 2476 goto out; 2477 } 2478 } 2479 2480 start = round_down(offset, blocksize); 2481 end = round_up(offset + length, blocksize) - 1; 2482 error = filemap_write_and_wait_range(inode->i_mapping, start, end); 2483 if (error) 2484 return error; 2485 2486 if (gfs2_is_jdata(ip)) 2487 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_JDATA, 2488 GFS2_JTRUNC_REVOKES); 2489 else 2490 error = gfs2_trans_begin(sdp, RES_DINODE, 0); 2491 if (error) 2492 return error; 2493 2494 if (gfs2_is_stuffed(ip)) { 2495 error = stuffed_zero_range(inode, offset, length); 2496 if (error) 2497 goto out; 2498 } 2499 2500 if (gfs2_is_jdata(ip)) { 2501 BUG_ON(!current->journal_info); 2502 gfs2_journaled_truncate_range(inode, offset, length); 2503 } else 2504 truncate_pagecache_range(inode, offset, offset + length - 1); 2505 2506 file_update_time(file); 2507 mark_inode_dirty(inode); 2508 2509 if (current->journal_info) 2510 gfs2_trans_end(sdp); 2511 2512 if (!gfs2_is_stuffed(ip)) 2513 error = punch_hole(ip, offset, length); 2514 2515 out: 2516 if (current->journal_info) 2517 gfs2_trans_end(sdp); 2518 return error; 2519 } 2520 2521 static ssize_t gfs2_writeback_range(struct iomap_writepage_ctx *wpc, 2522 struct folio *folio, u64 offset, unsigned int len, u64 end_pos) 2523 { 2524 if (WARN_ON_ONCE(gfs2_is_stuffed(GFS2_I(wpc->inode)))) 2525 return -EIO; 2526 2527 if (offset < wpc->iomap.offset || 2528 offset >= wpc->iomap.offset + wpc->iomap.length) { 2529 int ret; 2530 2531 memset(&wpc->iomap, 0, sizeof(wpc->iomap)); 2532 ret = gfs2_iomap_get(wpc->inode, offset, INT_MAX, &wpc->iomap); 2533 if (ret) 2534 return ret; 2535 } 2536 2537 return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); 2538 } 2539 2540 const struct iomap_writeback_ops gfs2_writeback_ops = { 2541 .writeback_range = gfs2_writeback_range, 2542 .writeback_submit = iomap_ioend_writeback_submit, 2543 }; 2544