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