1 /* 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc. 3 * All Rights Reserved. 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 #include <linux/log2.h> 19 20 #include "xfs.h" 21 #include "xfs_fs.h" 22 #include "xfs_format.h" 23 #include "xfs_log_format.h" 24 #include "xfs_trans_resv.h" 25 #include "xfs_mount.h" 26 #include "xfs_inode.h" 27 #include "xfs_trans.h" 28 #include "xfs_inode_item.h" 29 #include "xfs_btree.h" 30 #include "xfs_bmap_btree.h" 31 #include "xfs_bmap.h" 32 #include "xfs_error.h" 33 #include "xfs_trace.h" 34 #include "xfs_attr_sf.h" 35 #include "xfs_da_format.h" 36 #include "xfs_da_btree.h" 37 #include "xfs_dir2_priv.h" 38 39 kmem_zone_t *xfs_ifork_zone; 40 41 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int); 42 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int); 43 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int); 44 45 /* 46 * Move inode type and inode format specific information from the 47 * on-disk inode to the in-core inode. For fifos, devs, and sockets 48 * this means set if_rdev to the proper value. For files, directories, 49 * and symlinks this means to bring in the in-line data or extent 50 * pointers. For a file in B-tree format, only the root is immediately 51 * brought in-core. The rest will be in-lined in if_extents when it 52 * is first referenced (see xfs_iread_extents()). 53 */ 54 int 55 xfs_iformat_fork( 56 xfs_inode_t *ip, 57 xfs_dinode_t *dip) 58 { 59 xfs_attr_shortform_t *atp; 60 int size; 61 int error = 0; 62 xfs_fsize_t di_size; 63 64 if (unlikely(be32_to_cpu(dip->di_nextents) + 65 be16_to_cpu(dip->di_anextents) > 66 be64_to_cpu(dip->di_nblocks))) { 67 xfs_warn(ip->i_mount, 68 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.", 69 (unsigned long long)ip->i_ino, 70 (int)(be32_to_cpu(dip->di_nextents) + 71 be16_to_cpu(dip->di_anextents)), 72 (unsigned long long) 73 be64_to_cpu(dip->di_nblocks)); 74 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW, 75 ip->i_mount, dip); 76 return -EFSCORRUPTED; 77 } 78 79 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) { 80 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.", 81 (unsigned long long)ip->i_ino, 82 dip->di_forkoff); 83 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW, 84 ip->i_mount, dip); 85 return -EFSCORRUPTED; 86 } 87 88 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && 89 !ip->i_mount->m_rtdev_targp)) { 90 xfs_warn(ip->i_mount, 91 "corrupt dinode %Lu, has realtime flag set.", 92 ip->i_ino); 93 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)", 94 XFS_ERRLEVEL_LOW, ip->i_mount, dip); 95 return -EFSCORRUPTED; 96 } 97 98 if (unlikely(xfs_is_reflink_inode(ip) && 99 (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) { 100 xfs_warn(ip->i_mount, 101 "corrupt dinode %llu, wrong file type for reflink.", 102 ip->i_ino); 103 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)", 104 XFS_ERRLEVEL_LOW, ip->i_mount, dip); 105 return -EFSCORRUPTED; 106 } 107 108 if (unlikely(xfs_is_reflink_inode(ip) && 109 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) { 110 xfs_warn(ip->i_mount, 111 "corrupt dinode %llu, has reflink+realtime flag set.", 112 ip->i_ino); 113 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)", 114 XFS_ERRLEVEL_LOW, ip->i_mount, dip); 115 return -EFSCORRUPTED; 116 } 117 118 switch (VFS_I(ip)->i_mode & S_IFMT) { 119 case S_IFIFO: 120 case S_IFCHR: 121 case S_IFBLK: 122 case S_IFSOCK: 123 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) { 124 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW, 125 ip->i_mount, dip); 126 return -EFSCORRUPTED; 127 } 128 ip->i_d.di_size = 0; 129 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip); 130 break; 131 132 case S_IFREG: 133 case S_IFLNK: 134 case S_IFDIR: 135 switch (dip->di_format) { 136 case XFS_DINODE_FMT_LOCAL: 137 /* 138 * no local regular files yet 139 */ 140 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) { 141 xfs_warn(ip->i_mount, 142 "corrupt inode %Lu (local format for regular file).", 143 (unsigned long long) ip->i_ino); 144 XFS_CORRUPTION_ERROR("xfs_iformat(4)", 145 XFS_ERRLEVEL_LOW, 146 ip->i_mount, dip); 147 return -EFSCORRUPTED; 148 } 149 150 di_size = be64_to_cpu(dip->di_size); 151 if (unlikely(di_size < 0 || 152 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) { 153 xfs_warn(ip->i_mount, 154 "corrupt inode %Lu (bad size %Ld for local inode).", 155 (unsigned long long) ip->i_ino, 156 (long long) di_size); 157 XFS_CORRUPTION_ERROR("xfs_iformat(5)", 158 XFS_ERRLEVEL_LOW, 159 ip->i_mount, dip); 160 return -EFSCORRUPTED; 161 } 162 163 size = (int)di_size; 164 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size); 165 break; 166 case XFS_DINODE_FMT_EXTENTS: 167 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK); 168 break; 169 case XFS_DINODE_FMT_BTREE: 170 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK); 171 break; 172 default: 173 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW, 174 ip->i_mount); 175 return -EFSCORRUPTED; 176 } 177 break; 178 179 default: 180 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount); 181 return -EFSCORRUPTED; 182 } 183 if (error) 184 return error; 185 186 /* Check inline dir contents. */ 187 if (S_ISDIR(VFS_I(ip)->i_mode) && 188 dip->di_format == XFS_DINODE_FMT_LOCAL) { 189 error = xfs_dir2_sf_verify(ip); 190 if (error) { 191 xfs_idestroy_fork(ip, XFS_DATA_FORK); 192 return error; 193 } 194 } 195 196 if (xfs_is_reflink_inode(ip)) { 197 ASSERT(ip->i_cowfp == NULL); 198 xfs_ifork_init_cow(ip); 199 } 200 201 if (!XFS_DFORK_Q(dip)) 202 return 0; 203 204 ASSERT(ip->i_afp == NULL); 205 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS); 206 207 switch (dip->di_aformat) { 208 case XFS_DINODE_FMT_LOCAL: 209 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip); 210 size = be16_to_cpu(atp->hdr.totsize); 211 212 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) { 213 xfs_warn(ip->i_mount, 214 "corrupt inode %Lu (bad attr fork size %Ld).", 215 (unsigned long long) ip->i_ino, 216 (long long) size); 217 XFS_CORRUPTION_ERROR("xfs_iformat(8)", 218 XFS_ERRLEVEL_LOW, 219 ip->i_mount, dip); 220 error = -EFSCORRUPTED; 221 break; 222 } 223 224 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size); 225 break; 226 case XFS_DINODE_FMT_EXTENTS: 227 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK); 228 break; 229 case XFS_DINODE_FMT_BTREE: 230 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK); 231 break; 232 default: 233 error = -EFSCORRUPTED; 234 break; 235 } 236 if (error) { 237 kmem_zone_free(xfs_ifork_zone, ip->i_afp); 238 ip->i_afp = NULL; 239 if (ip->i_cowfp) 240 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp); 241 ip->i_cowfp = NULL; 242 xfs_idestroy_fork(ip, XFS_DATA_FORK); 243 } 244 return error; 245 } 246 247 void 248 xfs_init_local_fork( 249 struct xfs_inode *ip, 250 int whichfork, 251 const void *data, 252 int size) 253 { 254 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); 255 int mem_size = size, real_size = 0; 256 bool zero_terminate; 257 258 /* 259 * If we are using the local fork to store a symlink body we need to 260 * zero-terminate it so that we can pass it back to the VFS directly. 261 * Overallocate the in-memory fork by one for that and add a zero 262 * to terminate it below. 263 */ 264 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode); 265 if (zero_terminate) 266 mem_size++; 267 268 if (size == 0) 269 ifp->if_u1.if_data = NULL; 270 else if (mem_size <= sizeof(ifp->if_u2.if_inline_data)) 271 ifp->if_u1.if_data = ifp->if_u2.if_inline_data; 272 else { 273 real_size = roundup(mem_size, 4); 274 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS); 275 } 276 277 if (size) { 278 memcpy(ifp->if_u1.if_data, data, size); 279 if (zero_terminate) 280 ifp->if_u1.if_data[size] = '\0'; 281 } 282 283 ifp->if_bytes = size; 284 ifp->if_real_bytes = real_size; 285 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT); 286 ifp->if_flags |= XFS_IFINLINE; 287 } 288 289 /* 290 * The file is in-lined in the on-disk inode. 291 * If it fits into if_inline_data, then copy 292 * it there, otherwise allocate a buffer for it 293 * and copy the data there. Either way, set 294 * if_data to point at the data. 295 * If we allocate a buffer for the data, make 296 * sure that its size is a multiple of 4 and 297 * record the real size in i_real_bytes. 298 */ 299 STATIC int 300 xfs_iformat_local( 301 xfs_inode_t *ip, 302 xfs_dinode_t *dip, 303 int whichfork, 304 int size) 305 { 306 /* 307 * If the size is unreasonable, then something 308 * is wrong and we just bail out rather than crash in 309 * kmem_alloc() or memcpy() below. 310 */ 311 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) { 312 xfs_warn(ip->i_mount, 313 "corrupt inode %Lu (bad size %d for local fork, size = %d).", 314 (unsigned long long) ip->i_ino, size, 315 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)); 316 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW, 317 ip->i_mount, dip); 318 return -EFSCORRUPTED; 319 } 320 321 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size); 322 return 0; 323 } 324 325 /* 326 * The file consists of a set of extents all of which fit into the on-disk 327 * inode. If there are few enough extents to fit into the if_inline_ext, then 328 * copy them there. Otherwise allocate a buffer for them and copy them into it. 329 * Either way, set if_extents to point at the extents. 330 */ 331 STATIC int 332 xfs_iformat_extents( 333 struct xfs_inode *ip, 334 struct xfs_dinode *dip, 335 int whichfork) 336 { 337 struct xfs_mount *mp = ip->i_mount; 338 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork); 339 int nex = XFS_DFORK_NEXTENTS(dip, whichfork); 340 int size = nex * sizeof(xfs_bmbt_rec_t); 341 struct xfs_bmbt_rec *dp; 342 int i; 343 344 /* 345 * If the number of extents is unreasonable, then something is wrong and 346 * we just bail out rather than crash in kmem_alloc() or memcpy() below. 347 */ 348 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) { 349 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).", 350 (unsigned long long) ip->i_ino, nex); 351 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW, 352 mp, dip); 353 return -EFSCORRUPTED; 354 } 355 356 ifp->if_real_bytes = 0; 357 if (nex == 0) 358 ifp->if_u1.if_extents = NULL; 359 else if (nex <= XFS_INLINE_EXTS) 360 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; 361 else 362 xfs_iext_add(ifp, 0, nex); 363 364 ifp->if_bytes = size; 365 if (size) { 366 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork); 367 for (i = 0; i < nex; i++, dp++) { 368 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); 369 ep->l0 = get_unaligned_be64(&dp->l0); 370 ep->l1 = get_unaligned_be64(&dp->l1); 371 if (!xfs_bmbt_validate_extent(mp, whichfork, ep)) { 372 XFS_ERROR_REPORT("xfs_iformat_extents(2)", 373 XFS_ERRLEVEL_LOW, mp); 374 return -EFSCORRUPTED; 375 } 376 } 377 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork); 378 } 379 ifp->if_flags |= XFS_IFEXTENTS; 380 return 0; 381 } 382 383 /* 384 * The file has too many extents to fit into 385 * the inode, so they are in B-tree format. 386 * Allocate a buffer for the root of the B-tree 387 * and copy the root into it. The i_extents 388 * field will remain NULL until all of the 389 * extents are read in (when they are needed). 390 */ 391 STATIC int 392 xfs_iformat_btree( 393 xfs_inode_t *ip, 394 xfs_dinode_t *dip, 395 int whichfork) 396 { 397 struct xfs_mount *mp = ip->i_mount; 398 xfs_bmdr_block_t *dfp; 399 xfs_ifork_t *ifp; 400 /* REFERENCED */ 401 int nrecs; 402 int size; 403 int level; 404 405 ifp = XFS_IFORK_PTR(ip, whichfork); 406 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork); 407 size = XFS_BMAP_BROOT_SPACE(mp, dfp); 408 nrecs = be16_to_cpu(dfp->bb_numrecs); 409 level = be16_to_cpu(dfp->bb_level); 410 411 /* 412 * blow out if -- fork has less extents than can fit in 413 * fork (fork shouldn't be a btree format), root btree 414 * block has more records than can fit into the fork, 415 * or the number of extents is greater than the number of 416 * blocks. 417 */ 418 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= 419 XFS_IFORK_MAXEXT(ip, whichfork) || 420 XFS_BMDR_SPACE_CALC(nrecs) > 421 XFS_DFORK_SIZE(dip, mp, whichfork) || 422 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) || 423 level == 0 || level > XFS_BTREE_MAXLEVELS) { 424 xfs_warn(mp, "corrupt inode %Lu (btree).", 425 (unsigned long long) ip->i_ino); 426 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW, 427 mp, dip); 428 return -EFSCORRUPTED; 429 } 430 431 ifp->if_broot_bytes = size; 432 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS); 433 ASSERT(ifp->if_broot != NULL); 434 /* 435 * Copy and convert from the on-disk structure 436 * to the in-memory structure. 437 */ 438 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork), 439 ifp->if_broot, size); 440 ifp->if_flags &= ~XFS_IFEXTENTS; 441 ifp->if_flags |= XFS_IFBROOT; 442 443 return 0; 444 } 445 446 /* 447 * Read in extents from a btree-format inode. 448 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c. 449 */ 450 int 451 xfs_iread_extents( 452 xfs_trans_t *tp, 453 xfs_inode_t *ip, 454 int whichfork) 455 { 456 int error; 457 xfs_ifork_t *ifp; 458 xfs_extnum_t nextents; 459 460 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL)); 461 462 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) { 463 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW, 464 ip->i_mount); 465 return -EFSCORRUPTED; 466 } 467 nextents = XFS_IFORK_NEXTENTS(ip, whichfork); 468 ifp = XFS_IFORK_PTR(ip, whichfork); 469 470 /* 471 * We know that the size is valid (it's checked in iformat_btree) 472 */ 473 ifp->if_bytes = ifp->if_real_bytes = 0; 474 xfs_iext_add(ifp, 0, nextents); 475 error = xfs_bmap_read_extents(tp, ip, whichfork); 476 if (error) { 477 xfs_iext_destroy(ifp); 478 return error; 479 } 480 ifp->if_flags |= XFS_IFEXTENTS; 481 return 0; 482 } 483 /* 484 * Reallocate the space for if_broot based on the number of records 485 * being added or deleted as indicated in rec_diff. Move the records 486 * and pointers in if_broot to fit the new size. When shrinking this 487 * will eliminate holes between the records and pointers created by 488 * the caller. When growing this will create holes to be filled in 489 * by the caller. 490 * 491 * The caller must not request to add more records than would fit in 492 * the on-disk inode root. If the if_broot is currently NULL, then 493 * if we are adding records, one will be allocated. The caller must also 494 * not request that the number of records go below zero, although 495 * it can go to zero. 496 * 497 * ip -- the inode whose if_broot area is changing 498 * ext_diff -- the change in the number of records, positive or negative, 499 * requested for the if_broot array. 500 */ 501 void 502 xfs_iroot_realloc( 503 xfs_inode_t *ip, 504 int rec_diff, 505 int whichfork) 506 { 507 struct xfs_mount *mp = ip->i_mount; 508 int cur_max; 509 xfs_ifork_t *ifp; 510 struct xfs_btree_block *new_broot; 511 int new_max; 512 size_t new_size; 513 char *np; 514 char *op; 515 516 /* 517 * Handle the degenerate case quietly. 518 */ 519 if (rec_diff == 0) { 520 return; 521 } 522 523 ifp = XFS_IFORK_PTR(ip, whichfork); 524 if (rec_diff > 0) { 525 /* 526 * If there wasn't any memory allocated before, just 527 * allocate it now and get out. 528 */ 529 if (ifp->if_broot_bytes == 0) { 530 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff); 531 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); 532 ifp->if_broot_bytes = (int)new_size; 533 return; 534 } 535 536 /* 537 * If there is already an existing if_broot, then we need 538 * to realloc() it and shift the pointers to their new 539 * location. The records don't change location because 540 * they are kept butted up against the btree block header. 541 */ 542 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); 543 new_max = cur_max + rec_diff; 544 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); 545 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size, 546 KM_SLEEP | KM_NOFS); 547 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 548 ifp->if_broot_bytes); 549 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 550 (int)new_size); 551 ifp->if_broot_bytes = (int)new_size; 552 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 553 XFS_IFORK_SIZE(ip, whichfork)); 554 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t)); 555 return; 556 } 557 558 /* 559 * rec_diff is less than 0. In this case, we are shrinking the 560 * if_broot buffer. It must already exist. If we go to zero 561 * records, just get rid of the root and clear the status bit. 562 */ 563 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0)); 564 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0); 565 new_max = cur_max + rec_diff; 566 ASSERT(new_max >= 0); 567 if (new_max > 0) 568 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max); 569 else 570 new_size = 0; 571 if (new_size > 0) { 572 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS); 573 /* 574 * First copy over the btree block header. 575 */ 576 memcpy(new_broot, ifp->if_broot, 577 XFS_BMBT_BLOCK_LEN(ip->i_mount)); 578 } else { 579 new_broot = NULL; 580 ifp->if_flags &= ~XFS_IFBROOT; 581 } 582 583 /* 584 * Only copy the records and pointers if there are any. 585 */ 586 if (new_max > 0) { 587 /* 588 * First copy the records. 589 */ 590 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1); 591 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1); 592 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t)); 593 594 /* 595 * Then copy the pointers. 596 */ 597 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1, 598 ifp->if_broot_bytes); 599 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1, 600 (int)new_size); 601 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t)); 602 } 603 kmem_free(ifp->if_broot); 604 ifp->if_broot = new_broot; 605 ifp->if_broot_bytes = (int)new_size; 606 if (ifp->if_broot) 607 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 608 XFS_IFORK_SIZE(ip, whichfork)); 609 return; 610 } 611 612 613 /* 614 * This is called when the amount of space needed for if_data 615 * is increased or decreased. The change in size is indicated by 616 * the number of bytes that need to be added or deleted in the 617 * byte_diff parameter. 618 * 619 * If the amount of space needed has decreased below the size of the 620 * inline buffer, then switch to using the inline buffer. Otherwise, 621 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer 622 * to what is needed. 623 * 624 * ip -- the inode whose if_data area is changing 625 * byte_diff -- the change in the number of bytes, positive or negative, 626 * requested for the if_data array. 627 */ 628 void 629 xfs_idata_realloc( 630 xfs_inode_t *ip, 631 int byte_diff, 632 int whichfork) 633 { 634 xfs_ifork_t *ifp; 635 int new_size; 636 int real_size; 637 638 if (byte_diff == 0) { 639 return; 640 } 641 642 ifp = XFS_IFORK_PTR(ip, whichfork); 643 new_size = (int)ifp->if_bytes + byte_diff; 644 ASSERT(new_size >= 0); 645 646 if (new_size == 0) { 647 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { 648 kmem_free(ifp->if_u1.if_data); 649 } 650 ifp->if_u1.if_data = NULL; 651 real_size = 0; 652 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) { 653 /* 654 * If the valid extents/data can fit in if_inline_ext/data, 655 * copy them from the malloc'd vector and free it. 656 */ 657 if (ifp->if_u1.if_data == NULL) { 658 ifp->if_u1.if_data = ifp->if_u2.if_inline_data; 659 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { 660 ASSERT(ifp->if_real_bytes != 0); 661 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data, 662 new_size); 663 kmem_free(ifp->if_u1.if_data); 664 ifp->if_u1.if_data = ifp->if_u2.if_inline_data; 665 } 666 real_size = 0; 667 } else { 668 /* 669 * Stuck with malloc/realloc. 670 * For inline data, the underlying buffer must be 671 * a multiple of 4 bytes in size so that it can be 672 * logged and stay on word boundaries. We enforce 673 * that here. 674 */ 675 real_size = roundup(new_size, 4); 676 if (ifp->if_u1.if_data == NULL) { 677 ASSERT(ifp->if_real_bytes == 0); 678 ifp->if_u1.if_data = kmem_alloc(real_size, 679 KM_SLEEP | KM_NOFS); 680 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) { 681 /* 682 * Only do the realloc if the underlying size 683 * is really changing. 684 */ 685 if (ifp->if_real_bytes != real_size) { 686 ifp->if_u1.if_data = 687 kmem_realloc(ifp->if_u1.if_data, 688 real_size, 689 KM_SLEEP | KM_NOFS); 690 } 691 } else { 692 ASSERT(ifp->if_real_bytes == 0); 693 ifp->if_u1.if_data = kmem_alloc(real_size, 694 KM_SLEEP | KM_NOFS); 695 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data, 696 ifp->if_bytes); 697 } 698 } 699 ifp->if_real_bytes = real_size; 700 ifp->if_bytes = new_size; 701 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); 702 } 703 704 void 705 xfs_idestroy_fork( 706 xfs_inode_t *ip, 707 int whichfork) 708 { 709 xfs_ifork_t *ifp; 710 711 ifp = XFS_IFORK_PTR(ip, whichfork); 712 if (ifp->if_broot != NULL) { 713 kmem_free(ifp->if_broot); 714 ifp->if_broot = NULL; 715 } 716 717 /* 718 * If the format is local, then we can't have an extents 719 * array so just look for an inline data array. If we're 720 * not local then we may or may not have an extents list, 721 * so check and free it up if we do. 722 */ 723 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) { 724 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) && 725 (ifp->if_u1.if_data != NULL)) { 726 ASSERT(ifp->if_real_bytes != 0); 727 kmem_free(ifp->if_u1.if_data); 728 ifp->if_u1.if_data = NULL; 729 ifp->if_real_bytes = 0; 730 } 731 } else if ((ifp->if_flags & XFS_IFEXTENTS) && 732 ((ifp->if_flags & XFS_IFEXTIREC) || 733 ((ifp->if_u1.if_extents != NULL) && 734 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) { 735 ASSERT(ifp->if_real_bytes != 0); 736 xfs_iext_destroy(ifp); 737 } 738 ASSERT(ifp->if_u1.if_extents == NULL || 739 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext); 740 ASSERT(ifp->if_real_bytes == 0); 741 if (whichfork == XFS_ATTR_FORK) { 742 kmem_zone_free(xfs_ifork_zone, ip->i_afp); 743 ip->i_afp = NULL; 744 } else if (whichfork == XFS_COW_FORK) { 745 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp); 746 ip->i_cowfp = NULL; 747 } 748 } 749 750 /* Count number of incore extents based on if_bytes */ 751 xfs_extnum_t 752 xfs_iext_count(struct xfs_ifork *ifp) 753 { 754 return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t); 755 } 756 757 /* 758 * Convert in-core extents to on-disk form 759 * 760 * For either the data or attr fork in extent format, we need to endian convert 761 * the in-core extent as we place them into the on-disk inode. 762 * 763 * In the case of the data fork, the in-core and on-disk fork sizes can be 764 * different due to delayed allocation extents. We only copy on-disk extents 765 * here, so callers must always use the physical fork size to determine the 766 * size of the buffer passed to this routine. We will return the size actually 767 * used. 768 */ 769 int 770 xfs_iextents_copy( 771 xfs_inode_t *ip, 772 xfs_bmbt_rec_t *dp, 773 int whichfork) 774 { 775 int copied; 776 int i; 777 xfs_ifork_t *ifp; 778 int nrecs; 779 xfs_fsblock_t start_block; 780 781 ifp = XFS_IFORK_PTR(ip, whichfork); 782 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)); 783 ASSERT(ifp->if_bytes > 0); 784 785 nrecs = xfs_iext_count(ifp); 786 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork); 787 ASSERT(nrecs > 0); 788 789 /* 790 * There are some delayed allocation extents in the 791 * inode, so copy the extents one at a time and skip 792 * the delayed ones. There must be at least one 793 * non-delayed extent. 794 */ 795 copied = 0; 796 for (i = 0; i < nrecs; i++) { 797 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i); 798 799 ASSERT(xfs_bmbt_validate_extent(ip->i_mount, whichfork, ep)); 800 801 start_block = xfs_bmbt_get_startblock(ep); 802 if (isnullstartblock(start_block)) { 803 /* 804 * It's a delayed allocation extent, so skip it. 805 */ 806 continue; 807 } 808 809 /* Translate to on disk format */ 810 put_unaligned_be64(ep->l0, &dp->l0); 811 put_unaligned_be64(ep->l1, &dp->l1); 812 dp++; 813 copied++; 814 } 815 ASSERT(copied != 0); 816 817 return (copied * (uint)sizeof(xfs_bmbt_rec_t)); 818 } 819 820 /* 821 * Each of the following cases stores data into the same region 822 * of the on-disk inode, so only one of them can be valid at 823 * any given time. While it is possible to have conflicting formats 824 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is 825 * in EXTENTS format, this can only happen when the fork has 826 * changed formats after being modified but before being flushed. 827 * In these cases, the format always takes precedence, because the 828 * format indicates the current state of the fork. 829 */ 830 void 831 xfs_iflush_fork( 832 xfs_inode_t *ip, 833 xfs_dinode_t *dip, 834 xfs_inode_log_item_t *iip, 835 int whichfork) 836 { 837 char *cp; 838 xfs_ifork_t *ifp; 839 xfs_mount_t *mp; 840 static const short brootflag[2] = 841 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT }; 842 static const short dataflag[2] = 843 { XFS_ILOG_DDATA, XFS_ILOG_ADATA }; 844 static const short extflag[2] = 845 { XFS_ILOG_DEXT, XFS_ILOG_AEXT }; 846 847 if (!iip) 848 return; 849 ifp = XFS_IFORK_PTR(ip, whichfork); 850 /* 851 * This can happen if we gave up in iformat in an error path, 852 * for the attribute fork. 853 */ 854 if (!ifp) { 855 ASSERT(whichfork == XFS_ATTR_FORK); 856 return; 857 } 858 cp = XFS_DFORK_PTR(dip, whichfork); 859 mp = ip->i_mount; 860 switch (XFS_IFORK_FORMAT(ip, whichfork)) { 861 case XFS_DINODE_FMT_LOCAL: 862 if ((iip->ili_fields & dataflag[whichfork]) && 863 (ifp->if_bytes > 0)) { 864 ASSERT(ifp->if_u1.if_data != NULL); 865 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork)); 866 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes); 867 } 868 break; 869 870 case XFS_DINODE_FMT_EXTENTS: 871 ASSERT((ifp->if_flags & XFS_IFEXTENTS) || 872 !(iip->ili_fields & extflag[whichfork])); 873 if ((iip->ili_fields & extflag[whichfork]) && 874 (ifp->if_bytes > 0)) { 875 ASSERT(xfs_iext_get_ext(ifp, 0)); 876 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0); 877 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp, 878 whichfork); 879 } 880 break; 881 882 case XFS_DINODE_FMT_BTREE: 883 if ((iip->ili_fields & brootflag[whichfork]) && 884 (ifp->if_broot_bytes > 0)) { 885 ASSERT(ifp->if_broot != NULL); 886 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <= 887 XFS_IFORK_SIZE(ip, whichfork)); 888 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes, 889 (xfs_bmdr_block_t *)cp, 890 XFS_DFORK_SIZE(dip, mp, whichfork)); 891 } 892 break; 893 894 case XFS_DINODE_FMT_DEV: 895 if (iip->ili_fields & XFS_ILOG_DEV) { 896 ASSERT(whichfork == XFS_DATA_FORK); 897 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev); 898 } 899 break; 900 901 case XFS_DINODE_FMT_UUID: 902 if (iip->ili_fields & XFS_ILOG_UUID) { 903 ASSERT(whichfork == XFS_DATA_FORK); 904 memcpy(XFS_DFORK_DPTR(dip), 905 &ip->i_df.if_u2.if_uuid, 906 sizeof(uuid_t)); 907 } 908 break; 909 910 default: 911 ASSERT(0); 912 break; 913 } 914 } 915 916 /* 917 * Return a pointer to the extent record at file index idx. 918 */ 919 xfs_bmbt_rec_host_t * 920 xfs_iext_get_ext( 921 xfs_ifork_t *ifp, /* inode fork pointer */ 922 xfs_extnum_t idx) /* index of target extent */ 923 { 924 ASSERT(idx >= 0); 925 ASSERT(idx < xfs_iext_count(ifp)); 926 927 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) { 928 return ifp->if_u1.if_ext_irec->er_extbuf; 929 } else if (ifp->if_flags & XFS_IFEXTIREC) { 930 xfs_ext_irec_t *erp; /* irec pointer */ 931 int erp_idx = 0; /* irec index */ 932 xfs_extnum_t page_idx = idx; /* ext index in target list */ 933 934 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); 935 return &erp->er_extbuf[page_idx]; 936 } else if (ifp->if_bytes) { 937 return &ifp->if_u1.if_extents[idx]; 938 } else { 939 return NULL; 940 } 941 } 942 943 /* Convert bmap state flags to an inode fork. */ 944 struct xfs_ifork * 945 xfs_iext_state_to_fork( 946 struct xfs_inode *ip, 947 int state) 948 { 949 if (state & BMAP_COWFORK) 950 return ip->i_cowfp; 951 else if (state & BMAP_ATTRFORK) 952 return ip->i_afp; 953 return &ip->i_df; 954 } 955 956 /* 957 * Insert new item(s) into the extent records for incore inode 958 * fork 'ifp'. 'count' new items are inserted at index 'idx'. 959 */ 960 void 961 xfs_iext_insert( 962 xfs_inode_t *ip, /* incore inode pointer */ 963 xfs_extnum_t idx, /* starting index of new items */ 964 xfs_extnum_t count, /* number of inserted items */ 965 xfs_bmbt_irec_t *new, /* items to insert */ 966 int state) /* type of extent conversion */ 967 { 968 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state); 969 xfs_extnum_t i; /* extent record index */ 970 971 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_); 972 973 ASSERT(ifp->if_flags & XFS_IFEXTENTS); 974 xfs_iext_add(ifp, idx, count); 975 for (i = idx; i < idx + count; i++, new++) 976 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new); 977 } 978 979 /* 980 * This is called when the amount of space required for incore file 981 * extents needs to be increased. The ext_diff parameter stores the 982 * number of new extents being added and the idx parameter contains 983 * the extent index where the new extents will be added. If the new 984 * extents are being appended, then we just need to (re)allocate and 985 * initialize the space. Otherwise, if the new extents are being 986 * inserted into the middle of the existing entries, a bit more work 987 * is required to make room for the new extents to be inserted. The 988 * caller is responsible for filling in the new extent entries upon 989 * return. 990 */ 991 void 992 xfs_iext_add( 993 xfs_ifork_t *ifp, /* inode fork pointer */ 994 xfs_extnum_t idx, /* index to begin adding exts */ 995 int ext_diff) /* number of extents to add */ 996 { 997 int byte_diff; /* new bytes being added */ 998 int new_size; /* size of extents after adding */ 999 xfs_extnum_t nextents; /* number of extents in file */ 1000 1001 nextents = xfs_iext_count(ifp); 1002 ASSERT((idx >= 0) && (idx <= nextents)); 1003 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t); 1004 new_size = ifp->if_bytes + byte_diff; 1005 /* 1006 * If the new number of extents (nextents + ext_diff) 1007 * fits inside the inode, then continue to use the inline 1008 * extent buffer. 1009 */ 1010 if (nextents + ext_diff <= XFS_INLINE_EXTS) { 1011 if (idx < nextents) { 1012 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff], 1013 &ifp->if_u2.if_inline_ext[idx], 1014 (nextents - idx) * sizeof(xfs_bmbt_rec_t)); 1015 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff); 1016 } 1017 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; 1018 ifp->if_real_bytes = 0; 1019 } 1020 /* 1021 * Otherwise use a linear (direct) extent list. 1022 * If the extents are currently inside the inode, 1023 * xfs_iext_realloc_direct will switch us from 1024 * inline to direct extent allocation mode. 1025 */ 1026 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) { 1027 xfs_iext_realloc_direct(ifp, new_size); 1028 if (idx < nextents) { 1029 memmove(&ifp->if_u1.if_extents[idx + ext_diff], 1030 &ifp->if_u1.if_extents[idx], 1031 (nextents - idx) * sizeof(xfs_bmbt_rec_t)); 1032 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff); 1033 } 1034 } 1035 /* Indirection array */ 1036 else { 1037 xfs_ext_irec_t *erp; 1038 int erp_idx = 0; 1039 int page_idx = idx; 1040 1041 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS); 1042 if (ifp->if_flags & XFS_IFEXTIREC) { 1043 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1); 1044 } else { 1045 xfs_iext_irec_init(ifp); 1046 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1047 erp = ifp->if_u1.if_ext_irec; 1048 } 1049 /* Extents fit in target extent page */ 1050 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) { 1051 if (page_idx < erp->er_extcount) { 1052 memmove(&erp->er_extbuf[page_idx + ext_diff], 1053 &erp->er_extbuf[page_idx], 1054 (erp->er_extcount - page_idx) * 1055 sizeof(xfs_bmbt_rec_t)); 1056 memset(&erp->er_extbuf[page_idx], 0, byte_diff); 1057 } 1058 erp->er_extcount += ext_diff; 1059 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); 1060 } 1061 /* Insert a new extent page */ 1062 else if (erp) { 1063 xfs_iext_add_indirect_multi(ifp, 1064 erp_idx, page_idx, ext_diff); 1065 } 1066 /* 1067 * If extent(s) are being appended to the last page in 1068 * the indirection array and the new extent(s) don't fit 1069 * in the page, then erp is NULL and erp_idx is set to 1070 * the next index needed in the indirection array. 1071 */ 1072 else { 1073 uint count = ext_diff; 1074 1075 while (count) { 1076 erp = xfs_iext_irec_new(ifp, erp_idx); 1077 erp->er_extcount = min(count, XFS_LINEAR_EXTS); 1078 count -= erp->er_extcount; 1079 if (count) 1080 erp_idx++; 1081 } 1082 } 1083 } 1084 ifp->if_bytes = new_size; 1085 } 1086 1087 /* 1088 * This is called when incore extents are being added to the indirection 1089 * array and the new extents do not fit in the target extent list. The 1090 * erp_idx parameter contains the irec index for the target extent list 1091 * in the indirection array, and the idx parameter contains the extent 1092 * index within the list. The number of extents being added is stored 1093 * in the count parameter. 1094 * 1095 * |-------| |-------| 1096 * | | | | idx - number of extents before idx 1097 * | idx | | count | 1098 * | | | | count - number of extents being inserted at idx 1099 * |-------| |-------| 1100 * | count | | nex2 | nex2 - number of extents after idx + count 1101 * |-------| |-------| 1102 */ 1103 void 1104 xfs_iext_add_indirect_multi( 1105 xfs_ifork_t *ifp, /* inode fork pointer */ 1106 int erp_idx, /* target extent irec index */ 1107 xfs_extnum_t idx, /* index within target list */ 1108 int count) /* new extents being added */ 1109 { 1110 int byte_diff; /* new bytes being added */ 1111 xfs_ext_irec_t *erp; /* pointer to irec entry */ 1112 xfs_extnum_t ext_diff; /* number of extents to add */ 1113 xfs_extnum_t ext_cnt; /* new extents still needed */ 1114 xfs_extnum_t nex2; /* extents after idx + count */ 1115 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */ 1116 int nlists; /* number of irec's (lists) */ 1117 1118 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1119 erp = &ifp->if_u1.if_ext_irec[erp_idx]; 1120 nex2 = erp->er_extcount - idx; 1121 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1122 1123 /* 1124 * Save second part of target extent list 1125 * (all extents past */ 1126 if (nex2) { 1127 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); 1128 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS); 1129 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff); 1130 erp->er_extcount -= nex2; 1131 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2); 1132 memset(&erp->er_extbuf[idx], 0, byte_diff); 1133 } 1134 1135 /* 1136 * Add the new extents to the end of the target 1137 * list, then allocate new irec record(s) and 1138 * extent buffer(s) as needed to store the rest 1139 * of the new extents. 1140 */ 1141 ext_cnt = count; 1142 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount); 1143 if (ext_diff) { 1144 erp->er_extcount += ext_diff; 1145 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); 1146 ext_cnt -= ext_diff; 1147 } 1148 while (ext_cnt) { 1149 erp_idx++; 1150 erp = xfs_iext_irec_new(ifp, erp_idx); 1151 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS); 1152 erp->er_extcount = ext_diff; 1153 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff); 1154 ext_cnt -= ext_diff; 1155 } 1156 1157 /* Add nex2 extents back to indirection array */ 1158 if (nex2) { 1159 xfs_extnum_t ext_avail; 1160 int i; 1161 1162 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t); 1163 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount; 1164 i = 0; 1165 /* 1166 * If nex2 extents fit in the current page, append 1167 * nex2_ep after the new extents. 1168 */ 1169 if (nex2 <= ext_avail) { 1170 i = erp->er_extcount; 1171 } 1172 /* 1173 * Otherwise, check if space is available in the 1174 * next page. 1175 */ 1176 else if ((erp_idx < nlists - 1) && 1177 (nex2 <= (ext_avail = XFS_LINEAR_EXTS - 1178 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) { 1179 erp_idx++; 1180 erp++; 1181 /* Create a hole for nex2 extents */ 1182 memmove(&erp->er_extbuf[nex2], erp->er_extbuf, 1183 erp->er_extcount * sizeof(xfs_bmbt_rec_t)); 1184 } 1185 /* 1186 * Final choice, create a new extent page for 1187 * nex2 extents. 1188 */ 1189 else { 1190 erp_idx++; 1191 erp = xfs_iext_irec_new(ifp, erp_idx); 1192 } 1193 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff); 1194 kmem_free(nex2_ep); 1195 erp->er_extcount += nex2; 1196 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2); 1197 } 1198 } 1199 1200 /* 1201 * This is called when the amount of space required for incore file 1202 * extents needs to be decreased. The ext_diff parameter stores the 1203 * number of extents to be removed and the idx parameter contains 1204 * the extent index where the extents will be removed from. 1205 * 1206 * If the amount of space needed has decreased below the linear 1207 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous 1208 * extent array. Otherwise, use kmem_realloc() to adjust the 1209 * size to what is needed. 1210 */ 1211 void 1212 xfs_iext_remove( 1213 xfs_inode_t *ip, /* incore inode pointer */ 1214 xfs_extnum_t idx, /* index to begin removing exts */ 1215 int ext_diff, /* number of extents to remove */ 1216 int state) /* type of extent conversion */ 1217 { 1218 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state); 1219 xfs_extnum_t nextents; /* number of extents in file */ 1220 int new_size; /* size of extents after removal */ 1221 1222 trace_xfs_iext_remove(ip, idx, state, _RET_IP_); 1223 1224 ASSERT(ext_diff > 0); 1225 nextents = xfs_iext_count(ifp); 1226 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t); 1227 1228 if (new_size == 0) { 1229 xfs_iext_destroy(ifp); 1230 } else if (ifp->if_flags & XFS_IFEXTIREC) { 1231 xfs_iext_remove_indirect(ifp, idx, ext_diff); 1232 } else if (ifp->if_real_bytes) { 1233 xfs_iext_remove_direct(ifp, idx, ext_diff); 1234 } else { 1235 xfs_iext_remove_inline(ifp, idx, ext_diff); 1236 } 1237 ifp->if_bytes = new_size; 1238 } 1239 1240 /* 1241 * This removes ext_diff extents from the inline buffer, beginning 1242 * at extent index idx. 1243 */ 1244 void 1245 xfs_iext_remove_inline( 1246 xfs_ifork_t *ifp, /* inode fork pointer */ 1247 xfs_extnum_t idx, /* index to begin removing exts */ 1248 int ext_diff) /* number of extents to remove */ 1249 { 1250 int nextents; /* number of extents in file */ 1251 1252 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); 1253 ASSERT(idx < XFS_INLINE_EXTS); 1254 nextents = xfs_iext_count(ifp); 1255 ASSERT(((nextents - ext_diff) > 0) && 1256 (nextents - ext_diff) < XFS_INLINE_EXTS); 1257 1258 if (idx + ext_diff < nextents) { 1259 memmove(&ifp->if_u2.if_inline_ext[idx], 1260 &ifp->if_u2.if_inline_ext[idx + ext_diff], 1261 (nextents - (idx + ext_diff)) * 1262 sizeof(xfs_bmbt_rec_t)); 1263 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff], 1264 0, ext_diff * sizeof(xfs_bmbt_rec_t)); 1265 } else { 1266 memset(&ifp->if_u2.if_inline_ext[idx], 0, 1267 ext_diff * sizeof(xfs_bmbt_rec_t)); 1268 } 1269 } 1270 1271 /* 1272 * This removes ext_diff extents from a linear (direct) extent list, 1273 * beginning at extent index idx. If the extents are being removed 1274 * from the end of the list (ie. truncate) then we just need to re- 1275 * allocate the list to remove the extra space. Otherwise, if the 1276 * extents are being removed from the middle of the existing extent 1277 * entries, then we first need to move the extent records beginning 1278 * at idx + ext_diff up in the list to overwrite the records being 1279 * removed, then remove the extra space via kmem_realloc. 1280 */ 1281 void 1282 xfs_iext_remove_direct( 1283 xfs_ifork_t *ifp, /* inode fork pointer */ 1284 xfs_extnum_t idx, /* index to begin removing exts */ 1285 int ext_diff) /* number of extents to remove */ 1286 { 1287 xfs_extnum_t nextents; /* number of extents in file */ 1288 int new_size; /* size of extents after removal */ 1289 1290 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); 1291 new_size = ifp->if_bytes - 1292 (ext_diff * sizeof(xfs_bmbt_rec_t)); 1293 nextents = xfs_iext_count(ifp); 1294 1295 if (new_size == 0) { 1296 xfs_iext_destroy(ifp); 1297 return; 1298 } 1299 /* Move extents up in the list (if needed) */ 1300 if (idx + ext_diff < nextents) { 1301 memmove(&ifp->if_u1.if_extents[idx], 1302 &ifp->if_u1.if_extents[idx + ext_diff], 1303 (nextents - (idx + ext_diff)) * 1304 sizeof(xfs_bmbt_rec_t)); 1305 } 1306 memset(&ifp->if_u1.if_extents[nextents - ext_diff], 1307 0, ext_diff * sizeof(xfs_bmbt_rec_t)); 1308 /* 1309 * Reallocate the direct extent list. If the extents 1310 * will fit inside the inode then xfs_iext_realloc_direct 1311 * will switch from direct to inline extent allocation 1312 * mode for us. 1313 */ 1314 xfs_iext_realloc_direct(ifp, new_size); 1315 ifp->if_bytes = new_size; 1316 } 1317 1318 /* 1319 * This is called when incore extents are being removed from the 1320 * indirection array and the extents being removed span multiple extent 1321 * buffers. The idx parameter contains the file extent index where we 1322 * want to begin removing extents, and the count parameter contains 1323 * how many extents need to be removed. 1324 * 1325 * |-------| |-------| 1326 * | nex1 | | | nex1 - number of extents before idx 1327 * |-------| | count | 1328 * | | | | count - number of extents being removed at idx 1329 * | count | |-------| 1330 * | | | nex2 | nex2 - number of extents after idx + count 1331 * |-------| |-------| 1332 */ 1333 void 1334 xfs_iext_remove_indirect( 1335 xfs_ifork_t *ifp, /* inode fork pointer */ 1336 xfs_extnum_t idx, /* index to begin removing extents */ 1337 int count) /* number of extents to remove */ 1338 { 1339 xfs_ext_irec_t *erp; /* indirection array pointer */ 1340 int erp_idx = 0; /* indirection array index */ 1341 xfs_extnum_t ext_cnt; /* extents left to remove */ 1342 xfs_extnum_t ext_diff; /* extents to remove in current list */ 1343 xfs_extnum_t nex1; /* number of extents before idx */ 1344 xfs_extnum_t nex2; /* extents after idx + count */ 1345 int page_idx = idx; /* index in target extent list */ 1346 1347 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1348 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0); 1349 ASSERT(erp != NULL); 1350 nex1 = page_idx; 1351 ext_cnt = count; 1352 while (ext_cnt) { 1353 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0); 1354 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1)); 1355 /* 1356 * Check for deletion of entire list; 1357 * xfs_iext_irec_remove() updates extent offsets. 1358 */ 1359 if (ext_diff == erp->er_extcount) { 1360 xfs_iext_irec_remove(ifp, erp_idx); 1361 ext_cnt -= ext_diff; 1362 nex1 = 0; 1363 if (ext_cnt) { 1364 ASSERT(erp_idx < ifp->if_real_bytes / 1365 XFS_IEXT_BUFSZ); 1366 erp = &ifp->if_u1.if_ext_irec[erp_idx]; 1367 nex1 = 0; 1368 continue; 1369 } else { 1370 break; 1371 } 1372 } 1373 /* Move extents up (if needed) */ 1374 if (nex2) { 1375 memmove(&erp->er_extbuf[nex1], 1376 &erp->er_extbuf[nex1 + ext_diff], 1377 nex2 * sizeof(xfs_bmbt_rec_t)); 1378 } 1379 /* Zero out rest of page */ 1380 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ - 1381 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t)))); 1382 /* Update remaining counters */ 1383 erp->er_extcount -= ext_diff; 1384 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff); 1385 ext_cnt -= ext_diff; 1386 nex1 = 0; 1387 erp_idx++; 1388 erp++; 1389 } 1390 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t); 1391 xfs_iext_irec_compact(ifp); 1392 } 1393 1394 /* 1395 * Create, destroy, or resize a linear (direct) block of extents. 1396 */ 1397 void 1398 xfs_iext_realloc_direct( 1399 xfs_ifork_t *ifp, /* inode fork pointer */ 1400 int new_size) /* new size of extents after adding */ 1401 { 1402 int rnew_size; /* real new size of extents */ 1403 1404 rnew_size = new_size; 1405 1406 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) || 1407 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) && 1408 (new_size != ifp->if_real_bytes))); 1409 1410 /* Free extent records */ 1411 if (new_size == 0) { 1412 xfs_iext_destroy(ifp); 1413 } 1414 /* Resize direct extent list and zero any new bytes */ 1415 else if (ifp->if_real_bytes) { 1416 /* Check if extents will fit inside the inode */ 1417 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) { 1418 xfs_iext_direct_to_inline(ifp, new_size / 1419 (uint)sizeof(xfs_bmbt_rec_t)); 1420 ifp->if_bytes = new_size; 1421 return; 1422 } 1423 if (!is_power_of_2(new_size)){ 1424 rnew_size = roundup_pow_of_two(new_size); 1425 } 1426 if (rnew_size != ifp->if_real_bytes) { 1427 ifp->if_u1.if_extents = 1428 kmem_realloc(ifp->if_u1.if_extents, 1429 rnew_size, KM_NOFS); 1430 } 1431 if (rnew_size > ifp->if_real_bytes) { 1432 memset(&ifp->if_u1.if_extents[ifp->if_bytes / 1433 (uint)sizeof(xfs_bmbt_rec_t)], 0, 1434 rnew_size - ifp->if_real_bytes); 1435 } 1436 } 1437 /* Switch from the inline extent buffer to a direct extent list */ 1438 else { 1439 if (!is_power_of_2(new_size)) { 1440 rnew_size = roundup_pow_of_two(new_size); 1441 } 1442 xfs_iext_inline_to_direct(ifp, rnew_size); 1443 } 1444 ifp->if_real_bytes = rnew_size; 1445 ifp->if_bytes = new_size; 1446 } 1447 1448 /* 1449 * Switch from linear (direct) extent records to inline buffer. 1450 */ 1451 void 1452 xfs_iext_direct_to_inline( 1453 xfs_ifork_t *ifp, /* inode fork pointer */ 1454 xfs_extnum_t nextents) /* number of extents in file */ 1455 { 1456 ASSERT(ifp->if_flags & XFS_IFEXTENTS); 1457 ASSERT(nextents <= XFS_INLINE_EXTS); 1458 /* 1459 * The inline buffer was zeroed when we switched 1460 * from inline to direct extent allocation mode, 1461 * so we don't need to clear it here. 1462 */ 1463 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents, 1464 nextents * sizeof(xfs_bmbt_rec_t)); 1465 kmem_free(ifp->if_u1.if_extents); 1466 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext; 1467 ifp->if_real_bytes = 0; 1468 } 1469 1470 /* 1471 * Switch from inline buffer to linear (direct) extent records. 1472 * new_size should already be rounded up to the next power of 2 1473 * by the caller (when appropriate), so use new_size as it is. 1474 * However, since new_size may be rounded up, we can't update 1475 * if_bytes here. It is the caller's responsibility to update 1476 * if_bytes upon return. 1477 */ 1478 void 1479 xfs_iext_inline_to_direct( 1480 xfs_ifork_t *ifp, /* inode fork pointer */ 1481 int new_size) /* number of extents in file */ 1482 { 1483 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS); 1484 memset(ifp->if_u1.if_extents, 0, new_size); 1485 if (ifp->if_bytes) { 1486 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext, 1487 ifp->if_bytes); 1488 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * 1489 sizeof(xfs_bmbt_rec_t)); 1490 } 1491 ifp->if_real_bytes = new_size; 1492 } 1493 1494 /* 1495 * Resize an extent indirection array to new_size bytes. 1496 */ 1497 STATIC void 1498 xfs_iext_realloc_indirect( 1499 xfs_ifork_t *ifp, /* inode fork pointer */ 1500 int new_size) /* new indirection array size */ 1501 { 1502 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1503 ASSERT(ifp->if_real_bytes); 1504 ASSERT((new_size >= 0) && 1505 (new_size != ((ifp->if_real_bytes / XFS_IEXT_BUFSZ) * 1506 sizeof(xfs_ext_irec_t)))); 1507 if (new_size == 0) { 1508 xfs_iext_destroy(ifp); 1509 } else { 1510 ifp->if_u1.if_ext_irec = 1511 kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS); 1512 } 1513 } 1514 1515 /* 1516 * Switch from indirection array to linear (direct) extent allocations. 1517 */ 1518 STATIC void 1519 xfs_iext_indirect_to_direct( 1520 xfs_ifork_t *ifp) /* inode fork pointer */ 1521 { 1522 xfs_bmbt_rec_host_t *ep; /* extent record pointer */ 1523 xfs_extnum_t nextents; /* number of extents in file */ 1524 int size; /* size of file extents */ 1525 1526 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1527 nextents = xfs_iext_count(ifp); 1528 ASSERT(nextents <= XFS_LINEAR_EXTS); 1529 size = nextents * sizeof(xfs_bmbt_rec_t); 1530 1531 xfs_iext_irec_compact_pages(ifp); 1532 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ); 1533 1534 ep = ifp->if_u1.if_ext_irec->er_extbuf; 1535 kmem_free(ifp->if_u1.if_ext_irec); 1536 ifp->if_flags &= ~XFS_IFEXTIREC; 1537 ifp->if_u1.if_extents = ep; 1538 ifp->if_bytes = size; 1539 if (nextents < XFS_LINEAR_EXTS) { 1540 xfs_iext_realloc_direct(ifp, size); 1541 } 1542 } 1543 1544 /* 1545 * Remove all records from the indirection array. 1546 */ 1547 STATIC void 1548 xfs_iext_irec_remove_all( 1549 struct xfs_ifork *ifp) 1550 { 1551 int nlists; 1552 int i; 1553 1554 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1555 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1556 for (i = 0; i < nlists; i++) 1557 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf); 1558 kmem_free(ifp->if_u1.if_ext_irec); 1559 ifp->if_flags &= ~XFS_IFEXTIREC; 1560 } 1561 1562 /* 1563 * Free incore file extents. 1564 */ 1565 void 1566 xfs_iext_destroy( 1567 xfs_ifork_t *ifp) /* inode fork pointer */ 1568 { 1569 if (ifp->if_flags & XFS_IFEXTIREC) { 1570 xfs_iext_irec_remove_all(ifp); 1571 } else if (ifp->if_real_bytes) { 1572 kmem_free(ifp->if_u1.if_extents); 1573 } else if (ifp->if_bytes) { 1574 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS * 1575 sizeof(xfs_bmbt_rec_t)); 1576 } 1577 ifp->if_u1.if_extents = NULL; 1578 ifp->if_real_bytes = 0; 1579 ifp->if_bytes = 0; 1580 } 1581 1582 /* 1583 * Return a pointer to the extent record for file system block bno. 1584 */ 1585 xfs_bmbt_rec_host_t * /* pointer to found extent record */ 1586 xfs_iext_bno_to_ext( 1587 xfs_ifork_t *ifp, /* inode fork pointer */ 1588 xfs_fileoff_t bno, /* block number to search for */ 1589 xfs_extnum_t *idxp) /* index of target extent */ 1590 { 1591 xfs_bmbt_rec_host_t *base; /* pointer to first extent */ 1592 xfs_filblks_t blockcount = 0; /* number of blocks in extent */ 1593 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */ 1594 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ 1595 int high; /* upper boundary in search */ 1596 xfs_extnum_t idx = 0; /* index of target extent */ 1597 int low; /* lower boundary in search */ 1598 xfs_extnum_t nextents; /* number of file extents */ 1599 xfs_fileoff_t startoff = 0; /* start offset of extent */ 1600 1601 nextents = xfs_iext_count(ifp); 1602 if (nextents == 0) { 1603 *idxp = 0; 1604 return NULL; 1605 } 1606 low = 0; 1607 if (ifp->if_flags & XFS_IFEXTIREC) { 1608 /* Find target extent list */ 1609 int erp_idx = 0; 1610 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx); 1611 base = erp->er_extbuf; 1612 high = erp->er_extcount - 1; 1613 } else { 1614 base = ifp->if_u1.if_extents; 1615 high = nextents - 1; 1616 } 1617 /* Binary search extent records */ 1618 while (low <= high) { 1619 idx = (low + high) >> 1; 1620 ep = base + idx; 1621 startoff = xfs_bmbt_get_startoff(ep); 1622 blockcount = xfs_bmbt_get_blockcount(ep); 1623 if (bno < startoff) { 1624 high = idx - 1; 1625 } else if (bno >= startoff + blockcount) { 1626 low = idx + 1; 1627 } else { 1628 /* Convert back to file-based extent index */ 1629 if (ifp->if_flags & XFS_IFEXTIREC) { 1630 idx += erp->er_extoff; 1631 } 1632 *idxp = idx; 1633 return ep; 1634 } 1635 } 1636 /* Convert back to file-based extent index */ 1637 if (ifp->if_flags & XFS_IFEXTIREC) { 1638 idx += erp->er_extoff; 1639 } 1640 if (bno >= startoff + blockcount) { 1641 if (++idx == nextents) { 1642 ep = NULL; 1643 } else { 1644 ep = xfs_iext_get_ext(ifp, idx); 1645 } 1646 } 1647 *idxp = idx; 1648 return ep; 1649 } 1650 1651 /* 1652 * Return a pointer to the indirection array entry containing the 1653 * extent record for filesystem block bno. Store the index of the 1654 * target irec in *erp_idxp. 1655 */ 1656 xfs_ext_irec_t * /* pointer to found extent record */ 1657 xfs_iext_bno_to_irec( 1658 xfs_ifork_t *ifp, /* inode fork pointer */ 1659 xfs_fileoff_t bno, /* block number to search for */ 1660 int *erp_idxp) /* irec index of target ext list */ 1661 { 1662 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */ 1663 xfs_ext_irec_t *erp_next; /* next indirection array entry */ 1664 int erp_idx; /* indirection array index */ 1665 int nlists; /* number of extent irec's (lists) */ 1666 int high; /* binary search upper limit */ 1667 int low; /* binary search lower limit */ 1668 1669 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1670 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1671 erp_idx = 0; 1672 low = 0; 1673 high = nlists - 1; 1674 while (low <= high) { 1675 erp_idx = (low + high) >> 1; 1676 erp = &ifp->if_u1.if_ext_irec[erp_idx]; 1677 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL; 1678 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) { 1679 high = erp_idx - 1; 1680 } else if (erp_next && bno >= 1681 xfs_bmbt_get_startoff(erp_next->er_extbuf)) { 1682 low = erp_idx + 1; 1683 } else { 1684 break; 1685 } 1686 } 1687 *erp_idxp = erp_idx; 1688 return erp; 1689 } 1690 1691 /* 1692 * Return a pointer to the indirection array entry containing the 1693 * extent record at file extent index *idxp. Store the index of the 1694 * target irec in *erp_idxp and store the page index of the target 1695 * extent record in *idxp. 1696 */ 1697 xfs_ext_irec_t * 1698 xfs_iext_idx_to_irec( 1699 xfs_ifork_t *ifp, /* inode fork pointer */ 1700 xfs_extnum_t *idxp, /* extent index (file -> page) */ 1701 int *erp_idxp, /* pointer to target irec */ 1702 int realloc) /* new bytes were just added */ 1703 { 1704 xfs_ext_irec_t *prev; /* pointer to previous irec */ 1705 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */ 1706 int erp_idx; /* indirection array index */ 1707 int nlists; /* number of irec's (ex lists) */ 1708 int high; /* binary search upper limit */ 1709 int low; /* binary search lower limit */ 1710 xfs_extnum_t page_idx = *idxp; /* extent index in target list */ 1711 1712 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1713 ASSERT(page_idx >= 0); 1714 ASSERT(page_idx <= xfs_iext_count(ifp)); 1715 ASSERT(page_idx < xfs_iext_count(ifp) || realloc); 1716 1717 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1718 erp_idx = 0; 1719 low = 0; 1720 high = nlists - 1; 1721 1722 /* Binary search extent irec's */ 1723 while (low <= high) { 1724 erp_idx = (low + high) >> 1; 1725 erp = &ifp->if_u1.if_ext_irec[erp_idx]; 1726 prev = erp_idx > 0 ? erp - 1 : NULL; 1727 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff && 1728 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) { 1729 high = erp_idx - 1; 1730 } else if (page_idx > erp->er_extoff + erp->er_extcount || 1731 (page_idx == erp->er_extoff + erp->er_extcount && 1732 !realloc)) { 1733 low = erp_idx + 1; 1734 } else if (page_idx == erp->er_extoff + erp->er_extcount && 1735 erp->er_extcount == XFS_LINEAR_EXTS) { 1736 ASSERT(realloc); 1737 page_idx = 0; 1738 erp_idx++; 1739 erp = erp_idx < nlists ? erp + 1 : NULL; 1740 break; 1741 } else { 1742 page_idx -= erp->er_extoff; 1743 break; 1744 } 1745 } 1746 *idxp = page_idx; 1747 *erp_idxp = erp_idx; 1748 return erp; 1749 } 1750 1751 /* 1752 * Allocate and initialize an indirection array once the space needed 1753 * for incore extents increases above XFS_IEXT_BUFSZ. 1754 */ 1755 void 1756 xfs_iext_irec_init( 1757 xfs_ifork_t *ifp) /* inode fork pointer */ 1758 { 1759 xfs_ext_irec_t *erp; /* indirection array pointer */ 1760 xfs_extnum_t nextents; /* number of extents in file */ 1761 1762 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC)); 1763 nextents = xfs_iext_count(ifp); 1764 ASSERT(nextents <= XFS_LINEAR_EXTS); 1765 1766 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS); 1767 1768 if (nextents == 0) { 1769 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); 1770 } else if (!ifp->if_real_bytes) { 1771 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ); 1772 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) { 1773 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ); 1774 } 1775 erp->er_extbuf = ifp->if_u1.if_extents; 1776 erp->er_extcount = nextents; 1777 erp->er_extoff = 0; 1778 1779 ifp->if_flags |= XFS_IFEXTIREC; 1780 ifp->if_real_bytes = XFS_IEXT_BUFSZ; 1781 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t); 1782 ifp->if_u1.if_ext_irec = erp; 1783 1784 return; 1785 } 1786 1787 /* 1788 * Allocate and initialize a new entry in the indirection array. 1789 */ 1790 xfs_ext_irec_t * 1791 xfs_iext_irec_new( 1792 xfs_ifork_t *ifp, /* inode fork pointer */ 1793 int erp_idx) /* index for new irec */ 1794 { 1795 xfs_ext_irec_t *erp; /* indirection array pointer */ 1796 int i; /* loop counter */ 1797 int nlists; /* number of irec's (ex lists) */ 1798 1799 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1800 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1801 1802 /* Resize indirection array */ 1803 xfs_iext_realloc_indirect(ifp, ++nlists * 1804 sizeof(xfs_ext_irec_t)); 1805 /* 1806 * Move records down in the array so the 1807 * new page can use erp_idx. 1808 */ 1809 erp = ifp->if_u1.if_ext_irec; 1810 for (i = nlists - 1; i > erp_idx; i--) { 1811 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t)); 1812 } 1813 ASSERT(i == erp_idx); 1814 1815 /* Initialize new extent record */ 1816 erp = ifp->if_u1.if_ext_irec; 1817 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS); 1818 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; 1819 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ); 1820 erp[erp_idx].er_extcount = 0; 1821 erp[erp_idx].er_extoff = erp_idx > 0 ? 1822 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0; 1823 return (&erp[erp_idx]); 1824 } 1825 1826 /* 1827 * Remove a record from the indirection array. 1828 */ 1829 void 1830 xfs_iext_irec_remove( 1831 xfs_ifork_t *ifp, /* inode fork pointer */ 1832 int erp_idx) /* irec index to remove */ 1833 { 1834 xfs_ext_irec_t *erp; /* indirection array pointer */ 1835 int i; /* loop counter */ 1836 int nlists; /* number of irec's (ex lists) */ 1837 1838 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1839 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1840 erp = &ifp->if_u1.if_ext_irec[erp_idx]; 1841 if (erp->er_extbuf) { 1842 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, 1843 -erp->er_extcount); 1844 kmem_free(erp->er_extbuf); 1845 } 1846 /* Compact extent records */ 1847 erp = ifp->if_u1.if_ext_irec; 1848 for (i = erp_idx; i < nlists - 1; i++) { 1849 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t)); 1850 } 1851 /* 1852 * Manually free the last extent record from the indirection 1853 * array. A call to xfs_iext_realloc_indirect() with a size 1854 * of zero would result in a call to xfs_iext_destroy() which 1855 * would in turn call this function again, creating a nasty 1856 * infinite loop. 1857 */ 1858 if (--nlists) { 1859 xfs_iext_realloc_indirect(ifp, 1860 nlists * sizeof(xfs_ext_irec_t)); 1861 } else { 1862 kmem_free(ifp->if_u1.if_ext_irec); 1863 } 1864 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ; 1865 } 1866 1867 /* 1868 * This is called to clean up large amounts of unused memory allocated 1869 * by the indirection array. Before compacting anything though, verify 1870 * that the indirection array is still needed and switch back to the 1871 * linear extent list (or even the inline buffer) if possible. The 1872 * compaction policy is as follows: 1873 * 1874 * Full Compaction: Extents fit into a single page (or inline buffer) 1875 * Partial Compaction: Extents occupy less than 50% of allocated space 1876 * No Compaction: Extents occupy at least 50% of allocated space 1877 */ 1878 void 1879 xfs_iext_irec_compact( 1880 xfs_ifork_t *ifp) /* inode fork pointer */ 1881 { 1882 xfs_extnum_t nextents; /* number of extents in file */ 1883 int nlists; /* number of irec's (ex lists) */ 1884 1885 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1886 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1887 nextents = xfs_iext_count(ifp); 1888 1889 if (nextents == 0) { 1890 xfs_iext_destroy(ifp); 1891 } else if (nextents <= XFS_INLINE_EXTS) { 1892 xfs_iext_indirect_to_direct(ifp); 1893 xfs_iext_direct_to_inline(ifp, nextents); 1894 } else if (nextents <= XFS_LINEAR_EXTS) { 1895 xfs_iext_indirect_to_direct(ifp); 1896 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) { 1897 xfs_iext_irec_compact_pages(ifp); 1898 } 1899 } 1900 1901 /* 1902 * Combine extents from neighboring extent pages. 1903 */ 1904 void 1905 xfs_iext_irec_compact_pages( 1906 xfs_ifork_t *ifp) /* inode fork pointer */ 1907 { 1908 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */ 1909 int erp_idx = 0; /* indirection array index */ 1910 int nlists; /* number of irec's (ex lists) */ 1911 1912 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1913 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1914 while (erp_idx < nlists - 1) { 1915 erp = &ifp->if_u1.if_ext_irec[erp_idx]; 1916 erp_next = erp + 1; 1917 if (erp_next->er_extcount <= 1918 (XFS_LINEAR_EXTS - erp->er_extcount)) { 1919 memcpy(&erp->er_extbuf[erp->er_extcount], 1920 erp_next->er_extbuf, erp_next->er_extcount * 1921 sizeof(xfs_bmbt_rec_t)); 1922 erp->er_extcount += erp_next->er_extcount; 1923 /* 1924 * Free page before removing extent record 1925 * so er_extoffs don't get modified in 1926 * xfs_iext_irec_remove. 1927 */ 1928 kmem_free(erp_next->er_extbuf); 1929 erp_next->er_extbuf = NULL; 1930 xfs_iext_irec_remove(ifp, erp_idx + 1); 1931 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1932 } else { 1933 erp_idx++; 1934 } 1935 } 1936 } 1937 1938 /* 1939 * This is called to update the er_extoff field in the indirection 1940 * array when extents have been added or removed from one of the 1941 * extent lists. erp_idx contains the irec index to begin updating 1942 * at and ext_diff contains the number of extents that were added 1943 * or removed. 1944 */ 1945 void 1946 xfs_iext_irec_update_extoffs( 1947 xfs_ifork_t *ifp, /* inode fork pointer */ 1948 int erp_idx, /* irec index to update */ 1949 int ext_diff) /* number of new extents */ 1950 { 1951 int i; /* loop counter */ 1952 int nlists; /* number of irec's (ex lists */ 1953 1954 ASSERT(ifp->if_flags & XFS_IFEXTIREC); 1955 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ; 1956 for (i = erp_idx; i < nlists; i++) { 1957 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff; 1958 } 1959 } 1960 1961 /* 1962 * Initialize an inode's copy-on-write fork. 1963 */ 1964 void 1965 xfs_ifork_init_cow( 1966 struct xfs_inode *ip) 1967 { 1968 if (ip->i_cowfp) 1969 return; 1970 1971 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone, 1972 KM_SLEEP | KM_NOFS); 1973 ip->i_cowfp->if_flags = XFS_IFEXTENTS; 1974 ip->i_cformat = XFS_DINODE_FMT_EXTENTS; 1975 ip->i_cnextents = 0; 1976 } 1977 1978 /* 1979 * Lookup the extent covering bno. 1980 * 1981 * If there is an extent covering bno return the extent index, and store the 1982 * expanded extent structure in *gotp, and the extent index in *idx. 1983 * If there is no extent covering bno, but there is an extent after it (e.g. 1984 * it lies in a hole) return that extent in *gotp and its index in *idx 1985 * instead. 1986 * If bno is beyond the last extent return false, and return the index after 1987 * the last valid index in *idxp. 1988 */ 1989 bool 1990 xfs_iext_lookup_extent( 1991 struct xfs_inode *ip, 1992 struct xfs_ifork *ifp, 1993 xfs_fileoff_t bno, 1994 xfs_extnum_t *idxp, 1995 struct xfs_bmbt_irec *gotp) 1996 { 1997 struct xfs_bmbt_rec_host *ep; 1998 1999 XFS_STATS_INC(ip->i_mount, xs_look_exlist); 2000 2001 ep = xfs_iext_bno_to_ext(ifp, bno, idxp); 2002 if (!ep) 2003 return false; 2004 xfs_bmbt_get_all(ep, gotp); 2005 return true; 2006 } 2007 2008 /* 2009 * Return true if there is an extent at index idx, and return the expanded 2010 * extent structure at idx in that case. Else return false. 2011 */ 2012 bool 2013 xfs_iext_get_extent( 2014 struct xfs_ifork *ifp, 2015 xfs_extnum_t idx, 2016 struct xfs_bmbt_irec *gotp) 2017 { 2018 if (idx < 0 || idx >= xfs_iext_count(ifp)) 2019 return false; 2020 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp); 2021 return true; 2022 } 2023 2024 void 2025 xfs_iext_update_extent( 2026 struct xfs_ifork *ifp, 2027 xfs_extnum_t idx, 2028 struct xfs_bmbt_irec *gotp) 2029 { 2030 ASSERT(idx >= 0); 2031 ASSERT(idx < xfs_iext_count(ifp)); 2032 2033 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, idx), gotp); 2034 } 2035