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