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 char *new_data = kmem_alloc(mem_size, KM_NOFS); 54 55 memcpy(new_data, data, size); 56 if (zero_terminate) 57 new_data[size] = '\0'; 58 59 ifp->if_data = new_data; 60 } else { 61 ifp->if_data = NULL; 62 } 63 64 ifp->if_bytes = size; 65 } 66 67 /* 68 * The file is in-lined in the on-disk inode. 69 */ 70 STATIC int 71 xfs_iformat_local( 72 struct xfs_inode *ip, 73 struct xfs_dinode *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 %llu (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 xfs_extnum_t 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 %llu ((a)extents = %llu).", 123 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_data = 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 xfs_bmap_complain_bad_rec(ip, whichfork, 147 fa, &new); 148 } 149 150 xfs_iext_insert(ip, &icur, &new, state); 151 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_); 152 xfs_iext_next(ifp, &icur); 153 } 154 } 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 struct xfs_inode *ip, 169 struct xfs_dinode *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_nblocks) || 198 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) { 199 xfs_warn(mp, "corrupt inode %llu (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 217 ifp->if_bytes = 0; 218 ifp->if_data = NULL; 219 ifp->if_height = 0; 220 return 0; 221 } 222 223 int 224 xfs_iformat_data_fork( 225 struct xfs_inode *ip, 226 struct xfs_dinode *dip) 227 { 228 struct inode *inode = VFS_I(ip); 229 int error; 230 231 /* 232 * Initialize the extent count early, as the per-format routines may 233 * depend on it. Use release semantics to set needextents /after/ we 234 * set the format. This ensures that we can use acquire semantics on 235 * needextents in xfs_need_iread_extents() and be guaranteed to see a 236 * valid format value after that load. 237 */ 238 ip->i_df.if_format = dip->di_format; 239 ip->i_df.if_nextents = xfs_dfork_data_extents(dip); 240 smp_store_release(&ip->i_df.if_needextents, 241 ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0); 242 243 switch (inode->i_mode & S_IFMT) { 244 case S_IFIFO: 245 case S_IFCHR: 246 case S_IFBLK: 247 case S_IFSOCK: 248 ip->i_disk_size = 0; 249 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip)); 250 return 0; 251 case S_IFREG: 252 case S_IFLNK: 253 case S_IFDIR: 254 switch (ip->i_df.if_format) { 255 case XFS_DINODE_FMT_LOCAL: 256 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, 257 be64_to_cpu(dip->di_size)); 258 if (!error) 259 error = xfs_ifork_verify_local_data(ip); 260 return error; 261 case XFS_DINODE_FMT_EXTENTS: 262 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK); 263 case XFS_DINODE_FMT_BTREE: 264 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK); 265 default: 266 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, 267 dip, sizeof(*dip), __this_address); 268 return -EFSCORRUPTED; 269 } 270 break; 271 default: 272 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip, 273 sizeof(*dip), __this_address); 274 return -EFSCORRUPTED; 275 } 276 } 277 278 static uint16_t 279 xfs_dfork_attr_shortform_size( 280 struct xfs_dinode *dip) 281 { 282 struct xfs_attr_sf_hdr *sf = XFS_DFORK_APTR(dip); 283 284 return be16_to_cpu(sf->totsize); 285 } 286 287 void 288 xfs_ifork_init_attr( 289 struct xfs_inode *ip, 290 enum xfs_dinode_fmt format, 291 xfs_extnum_t nextents) 292 { 293 /* 294 * Initialize the extent count early, as the per-format routines may 295 * depend on it. Use release semantics to set needextents /after/ we 296 * set the format. This ensures that we can use acquire semantics on 297 * needextents in xfs_need_iread_extents() and be guaranteed to see a 298 * valid format value after that load. 299 */ 300 ip->i_af.if_format = format; 301 ip->i_af.if_nextents = nextents; 302 smp_store_release(&ip->i_af.if_needextents, 303 ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0); 304 } 305 306 void 307 xfs_ifork_zap_attr( 308 struct xfs_inode *ip) 309 { 310 xfs_idestroy_fork(&ip->i_af); 311 memset(&ip->i_af, 0, sizeof(struct xfs_ifork)); 312 ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS; 313 } 314 315 int 316 xfs_iformat_attr_fork( 317 struct xfs_inode *ip, 318 struct xfs_dinode *dip) 319 { 320 xfs_extnum_t naextents = xfs_dfork_attr_extents(dip); 321 int error = 0; 322 323 /* 324 * Initialize the extent count early, as the per-format routines may 325 * depend on it. 326 */ 327 xfs_ifork_init_attr(ip, dip->di_aformat, naextents); 328 329 switch (ip->i_af.if_format) { 330 case XFS_DINODE_FMT_LOCAL: 331 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, 332 xfs_dfork_attr_shortform_size(dip)); 333 if (!error) 334 error = xfs_ifork_verify_local_attr(ip); 335 break; 336 case XFS_DINODE_FMT_EXTENTS: 337 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); 338 break; 339 case XFS_DINODE_FMT_BTREE: 340 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); 341 break; 342 default: 343 xfs_inode_verifier_error(ip, error, __func__, dip, 344 sizeof(*dip), __this_address); 345 error = -EFSCORRUPTED; 346 break; 347 } 348 349 if (error) 350 xfs_ifork_zap_attr(ip); 351 return error; 352 } 353 354 /* 355 * Reallocate the space for if_broot based on the number of records 356 * being added or deleted as indicated in rec_diff. Move the records 357 * and pointers in if_broot to fit the new size. When shrinking this 358 * will eliminate holes between the records and pointers created by 359 * the caller. When growing this will create holes to be filled in 360 * by the caller. 361 * 362 * The caller must not request to add more records than would fit in 363 * the on-disk inode root. If the if_broot is currently NULL, then 364 * if we are adding records, one will be allocated. The caller must also 365 * not request that the number of records go below zero, although 366 * it can go to zero. 367 * 368 * ip -- the inode whose if_broot area is changing 369 * ext_diff -- the change in the number of records, positive or negative, 370 * requested for the if_broot array. 371 */ 372 void 373 xfs_iroot_realloc( 374 xfs_inode_t *ip, 375 int rec_diff, 376 int whichfork) 377 { 378 struct xfs_mount *mp = ip->i_mount; 379 int cur_max; 380 struct xfs_ifork *ifp; 381 struct xfs_btree_block *new_broot; 382 int new_max; 383 size_t new_size; 384 char *np; 385 char *op; 386 387 /* 388 * Handle the degenerate case quietly. 389 */ 390 if (rec_diff == 0) { 391 return; 392 } 393 394 ifp = xfs_ifork_ptr(ip, whichfork); 395 if (rec_diff > 0) { 396 /* 397 * If there wasn't any memory allocated before, just 398 * allocate it now and get out. 399 */ 400 if (ifp->if_broot_bytes == 0) { 401 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff); 402 ifp->if_broot = kmem_alloc(new_size, KM_NOFS); 403 ifp->if_broot_bytes = (int)new_size; 404 return; 405 } 406 407 /* 408 * If there is already an existing if_broot, then we need 409 * to realloc() it and shift the pointers to their new 410 * location. The records don't change location because 411 * they are kept butted up against the btree block header. 412 */ 413 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); 414 new_max = cur_max + rec_diff; 415 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); 416 ifp->if_broot = krealloc(ifp->if_broot, new_size, 417 GFP_NOFS | __GFP_NOFAIL); 418 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 419 ifp->if_broot_bytes); 420 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 421 (int)new_size); 422 ifp->if_broot_bytes = (int)new_size; 423 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 424 xfs_inode_fork_size(ip, whichfork)); 425 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t)); 426 return; 427 } 428 429 /* 430 * rec_diff is less than 0. In this case, we are shrinking the 431 * if_broot buffer. It must already exist. If we go to zero 432 * records, just get rid of the root and clear the status bit. 433 */ 434 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); 435 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); 436 new_max = cur_max + rec_diff; 437 ASSERT(new_max >= 0); 438 if (new_max > 0) 439 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); 440 else 441 new_size = 0; 442 if (new_size > 0) { 443 new_broot = kmem_alloc(new_size, KM_NOFS); 444 /* 445 * First copy over the btree block header. 446 */ 447 memcpy(new_broot, ifp->if_broot, 448 XFS_BMBT_BLOCK_LEN(ip->i_mount)); 449 } else { 450 new_broot = NULL; 451 } 452 453 /* 454 * Only copy the records and pointers if there are any. 455 */ 456 if (new_max > 0) { 457 /* 458 * First copy the records. 459 */ 460 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); 461 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); 462 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); 463 464 /* 465 * Then copy the pointers. 466 */ 467 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 468 ifp->if_broot_bytes); 469 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, 470 (int)new_size); 471 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t)); 472 } 473 kmem_free(ifp->if_broot); 474 ifp->if_broot = new_broot; 475 ifp->if_broot_bytes = (int)new_size; 476 if (ifp->if_broot) 477 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 478 xfs_inode_fork_size(ip, whichfork)); 479 return; 480 } 481 482 483 /* 484 * This is called when the amount of space needed for if_data 485 * is increased or decreased. The change in size is indicated by 486 * the number of bytes that need to be added or deleted in the 487 * byte_diff parameter. 488 * 489 * If the amount of space needed has decreased below the size of the 490 * inline buffer, then switch to using the inline buffer. Otherwise, 491 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer 492 * to what is needed. 493 * 494 * ip -- the inode whose if_data area is changing 495 * byte_diff -- the change in the number of bytes, positive or negative, 496 * requested for the if_data array. 497 */ 498 void * 499 xfs_idata_realloc( 500 struct xfs_inode *ip, 501 int64_t byte_diff, 502 int whichfork) 503 { 504 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 505 int64_t new_size = ifp->if_bytes + byte_diff; 506 507 ASSERT(new_size >= 0); 508 ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork)); 509 510 if (byte_diff) { 511 ifp->if_data = krealloc(ifp->if_data, new_size, 512 GFP_NOFS | __GFP_NOFAIL); 513 if (new_size == 0) 514 ifp->if_data = NULL; 515 ifp->if_bytes = new_size; 516 } 517 518 return ifp->if_data; 519 } 520 521 /* Free all memory and reset a fork back to its initial state. */ 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_data); 534 ifp->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_data != NULL); 628 ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork)); 629 memcpy(cp, ifp->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 struct xfs_mount *mp = ip->i_mount; 706 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK); 707 struct xfs_dir2_sf_hdr *sfp = ifp->if_data; 708 709 fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes); 710 break; 711 } 712 case S_IFLNK: { 713 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK); 714 715 fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes); 716 break; 717 } 718 default: 719 break; 720 } 721 722 if (fa) { 723 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork", 724 ip->i_df.if_data, ip->i_df.if_bytes, fa); 725 return -EFSCORRUPTED; 726 } 727 728 return 0; 729 } 730 731 /* Verify the inline contents of the attr fork of an inode. */ 732 int 733 xfs_ifork_verify_local_attr( 734 struct xfs_inode *ip) 735 { 736 struct xfs_ifork *ifp = &ip->i_af; 737 xfs_failaddr_t fa; 738 739 if (!xfs_inode_has_attr_fork(ip)) { 740 fa = __this_address; 741 } else { 742 struct xfs_ifork *ifp = &ip->i_af; 743 744 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL); 745 fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes); 746 } 747 if (fa) { 748 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork", 749 ifp->if_data, ifp->if_bytes, fa); 750 return -EFSCORRUPTED; 751 } 752 753 return 0; 754 } 755 756 int 757 xfs_iext_count_may_overflow( 758 struct xfs_inode *ip, 759 int whichfork, 760 int nr_to_add) 761 { 762 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork); 763 uint64_t max_exts; 764 uint64_t nr_exts; 765 766 if (whichfork == XFS_COW_FORK) 767 return 0; 768 769 max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip), 770 whichfork); 771 772 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS)) 773 max_exts = 10; 774 775 nr_exts = ifp->if_nextents + nr_to_add; 776 if (nr_exts < ifp->if_nextents || nr_exts > max_exts) 777 return -EFBIG; 778 779 return 0; 780 } 781 782 /* 783 * Upgrade this inode's extent counter fields to be able to handle a potential 784 * increase in the extent count by nr_to_add. Normally this is the same 785 * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG. 786 */ 787 int 788 xfs_iext_count_upgrade( 789 struct xfs_trans *tp, 790 struct xfs_inode *ip, 791 uint nr_to_add) 792 { 793 ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR); 794 795 if (!xfs_has_large_extent_counts(ip->i_mount) || 796 xfs_inode_has_large_extent_counts(ip) || 797 XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS)) 798 return -EFBIG; 799 800 ip->i_diflags2 |= XFS_DIFLAG2_NREXT64; 801 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); 802 803 return 0; 804 } 805