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