1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 7 #include "xfs.h" 8 #include "xfs_fs.h" 9 #include "xfs_shared.h" 10 #include "xfs_format.h" 11 #include "xfs_log_format.h" 12 #include "xfs_trans_resv.h" 13 #include "xfs_mount.h" 14 #include "xfs_inode.h" 15 #include "xfs_trans.h" 16 #include "xfs_inode_item.h" 17 #include "xfs_btree.h" 18 #include "xfs_bmap_btree.h" 19 #include "xfs_bmap.h" 20 #include "xfs_error.h" 21 #include "xfs_trace.h" 22 #include "xfs_da_format.h" 23 #include "xfs_da_btree.h" 24 #include "xfs_dir2_priv.h" 25 #include "xfs_attr_leaf.h" 26 #include "xfs_types.h" 27 #include "xfs_errortag.h" 28 29 struct kmem_cache *xfs_ifork_cache; 30 31 void 32 xfs_init_local_fork( 33 struct xfs_inode *ip, 34 int whichfork, 35 const void *data, 36 int64_t size) 37 { 38 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 39 int mem_size = size; 40 bool zero_terminate; 41 42 /* 43 * If we are using the local fork to store a symlink body we need to 44 * zero-terminate it so that we can pass it back to the VFS directly. 45 * Overallocate the in-memory fork by one for that and add a zero 46 * to terminate it below. 47 */ 48 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode); 49 if (zero_terminate) 50 mem_size++; 51 52 if (size) { 53 ifp->if_u1.if_data = kmem_alloc(mem_size, KM_NOFS); 54 memcpy(ifp->if_u1.if_data, data, size); 55 if (zero_terminate) 56 ifp->if_u1.if_data[size] = '\0'; 57 } else { 58 ifp->if_u1.if_data = NULL; 59 } 60 61 ifp->if_bytes = size; 62 } 63 64 /* 65 * The file is in-lined in the on-disk inode. 66 */ 67 STATIC int 68 xfs_iformat_local( 69 struct xfs_inode *ip, 70 struct xfs_dinode *dip, 71 int whichfork, 72 int size) 73 { 74 /* 75 * If the size is unreasonable, then something 76 * is wrong and we just bail out rather than crash in 77 * kmem_alloc() or memcpy() below. 78 */ 79 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { 80 xfs_warn(ip->i_mount, 81 "corrupt inode %llu (bad size %d for local fork, size = %zd).", 82 (unsigned long long) ip->i_ino, size, 83 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)); 84 xfs_inode_verifier_error(ip, -EFSCORRUPTED, 85 "xfs_iformat_local", dip, sizeof(*dip), 86 __this_address); 87 return -EFSCORRUPTED; 88 } 89 90 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size); 91 return 0; 92 } 93 94 /* 95 * The file consists of a set of extents all of which fit into the on-disk 96 * inode. 97 */ 98 STATIC int 99 xfs_iformat_extents( 100 struct xfs_inode *ip, 101 struct xfs_dinode *dip, 102 int whichfork) 103 { 104 struct xfs_mount *mp = ip->i_mount; 105 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 106 int state = xfs_bmap_fork_to_state(whichfork); 107 xfs_extnum_t nex = xfs_dfork_nextents(dip, whichfork); 108 int size = nex * sizeof(xfs_bmbt_rec_t); 109 struct xfs_iext_cursor icur; 110 struct xfs_bmbt_rec *dp; 111 struct xfs_bmbt_irec new; 112 int i; 113 114 /* 115 * If the number of extents is unreasonable, then something is wrong and 116 * we just bail out rather than crash in kmem_alloc() or memcpy() below. 117 */ 118 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) { 119 xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).", 120 ip->i_ino, nex); 121 xfs_inode_verifier_error(ip, -EFSCORRUPTED, 122 "xfs_iformat_extents(1)", dip, sizeof(*dip), 123 __this_address); 124 return -EFSCORRUPTED; 125 } 126 127 ifp->if_bytes = 0; 128 ifp->if_u1.if_root = NULL; 129 ifp->if_height = 0; 130 if (size) { 131 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork); 132 133 xfs_iext_first(ifp, &icur); 134 for (i = 0; i < nex; i++, dp++) { 135 xfs_failaddr_t fa; 136 137 xfs_bmbt_disk_get_all(dp, &new); 138 fa = xfs_bmap_validate_extent(ip, whichfork, &new); 139 if (fa) { 140 xfs_inode_verifier_error(ip, -EFSCORRUPTED, 141 "xfs_iformat_extents(2)", 142 dp, sizeof(*dp), fa); 143 return xfs_bmap_complain_bad_rec(ip, whichfork, 144 fa, &new); 145 } 146 147 xfs_iext_insert(ip, &icur, &new, state); 148 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); 149 xfs_iext_next(ifp, &icur); 150 } 151 } 152 return 0; 153 } 154 155 /* 156 * The file has too many extents to fit into 157 * the inode, so they are in B-tree format. 158 * Allocate a buffer for the root of the B-tree 159 * and copy the root into it. The i_extents 160 * field will remain NULL until all of the 161 * extents are read in (when they are needed). 162 */ 163 STATIC int 164 xfs_iformat_btree( 165 struct xfs_inode *ip, 166 struct xfs_dinode *dip, 167 int whichfork) 168 { 169 struct xfs_mount *mp = ip->i_mount; 170 xfs_bmdr_block_t *dfp; 171 struct xfs_ifork *ifp; 172 /* REFERENCED */ 173 int nrecs; 174 int size; 175 int level; 176 177 ifp = xfs_ifork_ptr(ip, whichfork); 178 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork); 179 size = XFS_BMAP_BROOT_SPACE(mp, dfp); 180 nrecs = be16_to_cpu(dfp->bb_numrecs); 181 level = be16_to_cpu(dfp->bb_level); 182 183 /* 184 * blow out if -- fork has less extents than can fit in 185 * fork (fork shouldn't be a btree format), root btree 186 * block has more records than can fit into the fork, 187 * or the number of extents is greater than the number of 188 * blocks. 189 */ 190 if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) || 191 nrecs == 0 || 192 XFS_BMDR_SPACE_CALC(nrecs) > 193 XFS_DFORK_SIZE(dip, mp, whichfork) || 194 ifp->if_nextents > ip->i_nblocks) || 195 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) { 196 xfs_warn(mp, "corrupt inode %llu (btree).", 197 (unsigned long long) ip->i_ino); 198 xfs_inode_verifier_error(ip, -EFSCORRUPTED, 199 "xfs_iformat_btree", dfp, size, 200 __this_address); 201 return -EFSCORRUPTED; 202 } 203 204 ifp->if_broot_bytes = size; 205 ifp->if_broot = kmem_alloc(size, KM_NOFS); 206 ASSERT(ifp->if_broot != NULL); 207 /* 208 * Copy and convert from the on-disk structure 209 * to the in-memory structure. 210 */ 211 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork), 212 ifp->if_broot, size); 213 214 ifp->if_bytes = 0; 215 ifp->if_u1.if_root = NULL; 216 ifp->if_height = 0; 217 return 0; 218 } 219 220 int 221 xfs_iformat_data_fork( 222 struct xfs_inode *ip, 223 struct xfs_dinode *dip) 224 { 225 struct inode *inode = VFS_I(ip); 226 int error; 227 228 /* 229 * Initialize the extent count early, as the per-format routines may 230 * depend on it. 231 */ 232 ip->i_df.if_format = dip->di_format; 233 ip->i_df.if_nextents = xfs_dfork_data_extents(dip); 234 235 switch (inode->i_mode & S_IFMT) { 236 case S_IFIFO: 237 case S_IFCHR: 238 case S_IFBLK: 239 case S_IFSOCK: 240 ip->i_disk_size = 0; 241 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip)); 242 return 0; 243 case S_IFREG: 244 case S_IFLNK: 245 case S_IFDIR: 246 switch (ip->i_df.if_format) { 247 case XFS_DINODE_FMT_LOCAL: 248 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, 249 be64_to_cpu(dip->di_size)); 250 if (!error) 251 error = xfs_ifork_verify_local_data(ip); 252 return error; 253 case XFS_DINODE_FMT_EXTENTS: 254 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK); 255 case XFS_DINODE_FMT_BTREE: 256 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK); 257 default: 258 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, 259 dip, sizeof(*dip), __this_address); 260 return -EFSCORRUPTED; 261 } 262 break; 263 default: 264 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip, 265 sizeof(*dip), __this_address); 266 return -EFSCORRUPTED; 267 } 268 } 269 270 static uint16_t 271 xfs_dfork_attr_shortform_size( 272 struct xfs_dinode *dip) 273 { 274 struct xfs_attr_shortform *atp = 275 (struct xfs_attr_shortform *)XFS_DFORK_APTR(dip); 276 277 return be16_to_cpu(atp->hdr.totsize); 278 } 279 280 void 281 xfs_ifork_init_attr( 282 struct xfs_inode *ip, 283 enum xfs_dinode_fmt format, 284 xfs_extnum_t nextents) 285 { 286 ip->i_af.if_format = format; 287 ip->i_af.if_nextents = nextents; 288 } 289 290 void 291 xfs_ifork_zap_attr( 292 struct xfs_inode *ip) 293 { 294 xfs_idestroy_fork(&ip->i_af); 295 memset(&ip->i_af, 0, sizeof(struct xfs_ifork)); 296 ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS; 297 } 298 299 int 300 xfs_iformat_attr_fork( 301 struct xfs_inode *ip, 302 struct xfs_dinode *dip) 303 { 304 xfs_extnum_t naextents = xfs_dfork_attr_extents(dip); 305 int error = 0; 306 307 /* 308 * Initialize the extent count early, as the per-format routines may 309 * depend on it. 310 */ 311 xfs_ifork_init_attr(ip, dip->di_aformat, naextents); 312 313 switch (ip->i_af.if_format) { 314 case XFS_DINODE_FMT_LOCAL: 315 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, 316 xfs_dfork_attr_shortform_size(dip)); 317 if (!error) 318 error = xfs_ifork_verify_local_attr(ip); 319 break; 320 case XFS_DINODE_FMT_EXTENTS: 321 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); 322 break; 323 case XFS_DINODE_FMT_BTREE: 324 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); 325 break; 326 default: 327 xfs_inode_verifier_error(ip, error, __func__, dip, 328 sizeof(*dip), __this_address); 329 error = -EFSCORRUPTED; 330 break; 331 } 332 333 if (error) 334 xfs_ifork_zap_attr(ip); 335 return error; 336 } 337 338 /* 339 * Reallocate the space for if_broot based on the number of records 340 * being added or deleted as indicated in rec_diff. Move the records 341 * and pointers in if_broot to fit the new size. When shrinking this 342 * will eliminate holes between the records and pointers created by 343 * the caller. When growing this will create holes to be filled in 344 * by the caller. 345 * 346 * The caller must not request to add more records than would fit in 347 * the on-disk inode root. If the if_broot is currently NULL, then 348 * if we are adding records, one will be allocated. The caller must also 349 * not request that the number of records go below zero, although 350 * it can go to zero. 351 * 352 * ip -- the inode whose if_broot area is changing 353 * ext_diff -- the change in the number of records, positive or negative, 354 * requested for the if_broot array. 355 */ 356 void 357 xfs_iroot_realloc( 358 xfs_inode_t *ip, 359 int rec_diff, 360 int whichfork) 361 { 362 struct xfs_mount *mp = ip->i_mount; 363 int cur_max; 364 struct xfs_ifork *ifp; 365 struct xfs_btree_block *new_broot; 366 int new_max; 367 size_t new_size; 368 char *np; 369 char *op; 370 371 /* 372 * Handle the degenerate case quietly. 373 */ 374 if (rec_diff == 0) { 375 return; 376 } 377 378 ifp = xfs_ifork_ptr(ip, whichfork); 379 if (rec_diff > 0) { 380 /* 381 * If there wasn't any memory allocated before, just 382 * allocate it now and get out. 383 */ 384 if (ifp->if_broot_bytes == 0) { 385 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff); 386 ifp->if_broot = kmem_alloc(new_size, KM_NOFS); 387 ifp->if_broot_bytes = (int)new_size; 388 return; 389 } 390 391 /* 392 * If there is already an existing if_broot, then we need 393 * to realloc() it and shift the pointers to their new 394 * location. The records don't change location because 395 * they are kept butted up against the btree block header. 396 */ 397 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); 398 new_max = cur_max + rec_diff; 399 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); 400 ifp->if_broot = krealloc(ifp->if_broot, new_size, 401 GFP_NOFS | __GFP_NOFAIL); 402 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 403 ifp->if_broot_bytes); 404 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 405 (int)new_size); 406 ifp->if_broot_bytes = (int)new_size; 407 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 408 xfs_inode_fork_size(ip, whichfork)); 409 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t)); 410 return; 411 } 412 413 /* 414 * rec_diff is less than 0. In this case, we are shrinking the 415 * if_broot buffer. It must already exist. If we go to zero 416 * records, just get rid of the root and clear the status bit. 417 */ 418 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); 419 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); 420 new_max = cur_max + rec_diff; 421 ASSERT(new_max >= 0); 422 if (new_max > 0) 423 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); 424 else 425 new_size = 0; 426 if (new_size > 0) { 427 new_broot = kmem_alloc(new_size, KM_NOFS); 428 /* 429 * First copy over the btree block header. 430 */ 431 memcpy(new_broot, ifp->if_broot, 432 XFS_BMBT_BLOCK_LEN(ip->i_mount)); 433 } else { 434 new_broot = NULL; 435 } 436 437 /* 438 * Only copy the records and pointers if there are any. 439 */ 440 if (new_max > 0) { 441 /* 442 * First copy the records. 443 */ 444 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); 445 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); 446 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); 447 448 /* 449 * Then copy the pointers. 450 */ 451 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 452 ifp->if_broot_bytes); 453 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, 454 (int)new_size); 455 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t)); 456 } 457 kmem_free(ifp->if_broot); 458 ifp->if_broot = new_broot; 459 ifp->if_broot_bytes = (int)new_size; 460 if (ifp->if_broot) 461 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 462 xfs_inode_fork_size(ip, whichfork)); 463 return; 464 } 465 466 467 /* 468 * This is called when the amount of space needed for if_data 469 * is increased or decreased. The change in size is indicated by 470 * the number of bytes that need to be added or deleted in the 471 * byte_diff parameter. 472 * 473 * If the amount of space needed has decreased below the size of the 474 * inline buffer, then switch to using the inline buffer. Otherwise, 475 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer 476 * to what is needed. 477 * 478 * ip -- the inode whose if_data area is changing 479 * byte_diff -- the change in the number of bytes, positive or negative, 480 * requested for the if_data array. 481 */ 482 void 483 xfs_idata_realloc( 484 struct xfs_inode *ip, 485 int64_t byte_diff, 486 int whichfork) 487 { 488 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 489 int64_t new_size = ifp->if_bytes + byte_diff; 490 491 ASSERT(new_size >= 0); 492 ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork)); 493 494 if (byte_diff == 0) 495 return; 496 497 if (new_size == 0) { 498 kmem_free(ifp->if_u1.if_data); 499 ifp->if_u1.if_data = NULL; 500 ifp->if_bytes = 0; 501 return; 502 } 503 504 ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, new_size, 505 GFP_NOFS | __GFP_NOFAIL); 506 ifp->if_bytes = new_size; 507 } 508 509 void 510 xfs_idestroy_fork( 511 struct xfs_ifork *ifp) 512 { 513 if (ifp->if_broot != NULL) { 514 kmem_free(ifp->if_broot); 515 ifp->if_broot = NULL; 516 } 517 518 switch (ifp->if_format) { 519 case XFS_DINODE_FMT_LOCAL: 520 kmem_free(ifp->if_u1.if_data); 521 ifp->if_u1.if_data = NULL; 522 break; 523 case XFS_DINODE_FMT_EXTENTS: 524 case XFS_DINODE_FMT_BTREE: 525 if (ifp->if_height) 526 xfs_iext_destroy(ifp); 527 break; 528 } 529 } 530 531 /* 532 * Convert in-core extents to on-disk form 533 * 534 * In the case of the data fork, the in-core and on-disk fork sizes can be 535 * different due to delayed allocation extents. We only copy on-disk extents 536 * here, so callers must always use the physical fork size to determine the 537 * size of the buffer passed to this routine. We will return the size actually 538 * used. 539 */ 540 int 541 xfs_iextents_copy( 542 struct xfs_inode *ip, 543 struct xfs_bmbt_rec *dp, 544 int whichfork) 545 { 546 int state = xfs_bmap_fork_to_state(whichfork); 547 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 548 struct xfs_iext_cursor icur; 549 struct xfs_bmbt_irec rec; 550 int64_t copied = 0; 551 552 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED)); 553 ASSERT(ifp->if_bytes > 0); 554 555 for_each_xfs_iext(ifp, &icur, &rec) { 556 if (isnullstartblock(rec.br_startblock)) 557 continue; 558 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL); 559 xfs_bmbt_disk_set_all(dp, &rec); 560 trace_xfs_write_extent(ip, &icur, state, _RET_IP_); 561 copied += sizeof(struct xfs_bmbt_rec); 562 dp++; 563 } 564 565 ASSERT(copied > 0); 566 ASSERT(copied <= ifp->if_bytes); 567 return copied; 568 } 569 570 /* 571 * Each of the following cases stores data into the same region 572 * of the on-disk inode, so only one of them can be valid at 573 * any given time. While it is possible to have conflicting formats 574 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is 575 * in EXTENTS format, this can only happen when the fork has 576 * changed formats after being modified but before being flushed. 577 * In these cases, the format always takes precedence, because the 578 * format indicates the current state of the fork. 579 */ 580 void 581 xfs_iflush_fork( 582 struct xfs_inode *ip, 583 struct xfs_dinode *dip, 584 struct xfs_inode_log_item *iip, 585 int whichfork) 586 { 587 char *cp; 588 struct xfs_ifork *ifp; 589 xfs_mount_t *mp; 590 static const short brootflag[2] = 591 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT }; 592 static const short dataflag[2] = 593 { XFS_ILOG_DDATA, XFS_ILOG_ADATA }; 594 static const short extflag[2] = 595 { XFS_ILOG_DEXT, XFS_ILOG_AEXT }; 596 597 if (!iip) 598 return; 599 ifp = xfs_ifork_ptr(ip, whichfork); 600 /* 601 * This can happen if we gave up in iformat in an error path, 602 * for the attribute fork. 603 */ 604 if (!ifp) { 605 ASSERT(whichfork == XFS_ATTR_FORK); 606 return; 607 } 608 cp = XFS_DFORK_PTR(dip, whichfork); 609 mp = ip->i_mount; 610 switch (ifp->if_format) { 611 case XFS_DINODE_FMT_LOCAL: 612 if ((iip->ili_fields & dataflag[whichfork]) && 613 (ifp->if_bytes > 0)) { 614 ASSERT(ifp->if_u1.if_data != NULL); 615 ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork)); 616 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes); 617 } 618 break; 619 620 case XFS_DINODE_FMT_EXTENTS: 621 if ((iip->ili_fields & extflag[whichfork]) && 622 (ifp->if_bytes > 0)) { 623 ASSERT(ifp->if_nextents > 0); 624 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp, 625 whichfork); 626 } 627 break; 628 629 case XFS_DINODE_FMT_BTREE: 630 if ((iip->ili_fields & brootflag[whichfork]) && 631 (ifp->if_broot_bytes > 0)) { 632 ASSERT(ifp->if_broot != NULL); 633 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 634 xfs_inode_fork_size(ip, whichfork)); 635 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes, 636 (xfs_bmdr_block_t *)cp, 637 XFS_DFORK_SIZE(dip, mp, whichfork)); 638 } 639 break; 640 641 case XFS_DINODE_FMT_DEV: 642 if (iip->ili_fields & XFS_ILOG_DEV) { 643 ASSERT(whichfork == XFS_DATA_FORK); 644 xfs_dinode_put_rdev(dip, 645 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev)); 646 } 647 break; 648 649 default: 650 ASSERT(0); 651 break; 652 } 653 } 654 655 /* Convert bmap state flags to an inode fork. */ 656 struct xfs_ifork * 657 xfs_iext_state_to_fork( 658 struct xfs_inode *ip, 659 int state) 660 { 661 if (state & BMAP_COWFORK) 662 return ip->i_cowfp; 663 else if (state & BMAP_ATTRFORK) 664 return &ip->i_af; 665 return &ip->i_df; 666 } 667 668 /* 669 * Initialize an inode's copy-on-write fork. 670 */ 671 void 672 xfs_ifork_init_cow( 673 struct xfs_inode *ip) 674 { 675 if (ip->i_cowfp) 676 return; 677 678 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache, 679 GFP_NOFS | __GFP_NOFAIL); 680 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS; 681 } 682 683 /* Verify the inline contents of the data fork of an inode. */ 684 int 685 xfs_ifork_verify_local_data( 686 struct xfs_inode *ip) 687 { 688 xfs_failaddr_t fa = NULL; 689 690 switch (VFS_I(ip)->i_mode & S_IFMT) { 691 case S_IFDIR: 692 fa = xfs_dir2_sf_verify(ip); 693 break; 694 case S_IFLNK: 695 fa = xfs_symlink_shortform_verify(ip); 696 break; 697 default: 698 break; 699 } 700 701 if (fa) { 702 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork", 703 ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa); 704 return -EFSCORRUPTED; 705 } 706 707 return 0; 708 } 709 710 /* Verify the inline contents of the attr fork of an inode. */ 711 int 712 xfs_ifork_verify_local_attr( 713 struct xfs_inode *ip) 714 { 715 struct xfs_ifork *ifp = &ip->i_af; 716 xfs_failaddr_t fa; 717 718 if (!xfs_inode_has_attr_fork(ip)) 719 fa = __this_address; 720 else 721 fa = xfs_attr_shortform_verify(ip); 722 723 if (fa) { 724 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork", 725 ifp->if_u1.if_data, ifp->if_bytes, fa); 726 return -EFSCORRUPTED; 727 } 728 729 return 0; 730 } 731 732 int 733 xfs_iext_count_may_overflow( 734 struct xfs_inode *ip, 735 int whichfork, 736 int nr_to_add) 737 { 738 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 739 uint64_t max_exts; 740 uint64_t nr_exts; 741 742 if (whichfork == XFS_COW_FORK) 743 return 0; 744 745 max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip), 746 whichfork); 747 748 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS)) 749 max_exts = 10; 750 751 nr_exts = ifp->if_nextents + nr_to_add; 752 if (nr_exts < ifp->if_nextents || nr_exts > max_exts) 753 return -EFBIG; 754 755 return 0; 756 } 757 758 /* 759 * Upgrade this inode's extent counter fields to be able to handle a potential 760 * increase in the extent count by nr_to_add. Normally this is the same 761 * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG. 762 */ 763 int 764 xfs_iext_count_upgrade( 765 struct xfs_trans *tp, 766 struct xfs_inode *ip, 767 uint nr_to_add) 768 { 769 ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR); 770 771 if (!xfs_has_large_extent_counts(ip->i_mount) || 772 xfs_inode_has_large_extent_counts(ip) || 773 XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS)) 774 return -EFBIG; 775 776 ip->i_diflags2 |= XFS_DIFLAG2_NREXT64; 777 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 778 779 return 0; 780 } 781