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