1 /* $NetBSD: udf_allocation.c,v 1.36 2013/10/30 08:41:38 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2008 Reinoud Zandijk 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __KERNEL_RCSID(0, "$NetBSD: udf_allocation.c,v 1.36 2013/10/30 08:41:38 mrg Exp $"); 32 #endif /* not lint */ 33 34 35 #if defined(_KERNEL_OPT) 36 #include "opt_compat_netbsd.h" 37 #endif 38 39 /* TODO strip */ 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/sysctl.h> 43 #include <sys/namei.h> 44 #include <sys/proc.h> 45 #include <sys/kernel.h> 46 #include <sys/vnode.h> 47 #include <miscfs/genfs/genfs_node.h> 48 #include <sys/mount.h> 49 #include <sys/buf.h> 50 #include <sys/file.h> 51 #include <sys/device.h> 52 #include <sys/disklabel.h> 53 #include <sys/ioctl.h> 54 #include <sys/malloc.h> 55 #include <sys/dirent.h> 56 #include <sys/stat.h> 57 #include <sys/conf.h> 58 #include <sys/kauth.h> 59 #include <sys/kthread.h> 60 #include <dev/clock_subr.h> 61 62 #include <fs/udf/ecma167-udf.h> 63 #include <fs/udf/udf_mount.h> 64 65 #include "udf.h" 66 #include "udf_subr.h" 67 #include "udf_bswap.h" 68 69 70 #define VTOI(vnode) ((struct udf_node *) vnode->v_data) 71 72 static void udf_record_allocation_in_node(struct udf_mount *ump, 73 struct buf *buf, uint16_t vpart_num, uint64_t *mapping, 74 struct long_ad *node_ad_cpy); 75 76 static void udf_collect_free_space_for_vpart(struct udf_mount *ump, 77 uint16_t vpart_num, uint32_t num_lb); 78 79 static int udf_ads_merge(uint32_t max_len, uint32_t lb_size, struct long_ad *a1, struct long_ad *a2); 80 static void udf_wipe_adslots(struct udf_node *udf_node); 81 static void udf_count_alloc_exts(struct udf_node *udf_node); 82 83 84 /* --------------------------------------------------------------------- */ 85 86 #if 0 87 #if 1 88 static void 89 udf_node_dump(struct udf_node *udf_node) { 90 struct file_entry *fe; 91 struct extfile_entry *efe; 92 struct icb_tag *icbtag; 93 struct long_ad s_ad; 94 uint64_t inflen; 95 uint32_t icbflags, addr_type; 96 uint32_t len, lb_num; 97 uint32_t flags; 98 int part_num; 99 int lb_size, eof, slot; 100 101 if ((udf_verbose & UDF_DEBUG_NODEDUMP) == 0) 102 return; 103 104 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 105 106 fe = udf_node->fe; 107 efe = udf_node->efe; 108 if (fe) { 109 icbtag = &fe->icbtag; 110 inflen = udf_rw64(fe->inf_len); 111 } else { 112 icbtag = &efe->icbtag; 113 inflen = udf_rw64(efe->inf_len); 114 } 115 116 icbflags = udf_rw16(icbtag->flags); 117 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 118 119 printf("udf_node_dump %p :\n", udf_node); 120 121 if (addr_type == UDF_ICB_INTERN_ALLOC) { 122 printf("\tIntern alloc, len = %"PRIu64"\n", inflen); 123 return; 124 } 125 126 printf("\tInflen = %"PRIu64"\n", inflen); 127 printf("\t\t"); 128 129 slot = 0; 130 for (;;) { 131 udf_get_adslot(udf_node, slot, &s_ad, &eof); 132 if (eof) 133 break; 134 part_num = udf_rw16(s_ad.loc.part_num); 135 lb_num = udf_rw32(s_ad.loc.lb_num); 136 len = udf_rw32(s_ad.len); 137 flags = UDF_EXT_FLAGS(len); 138 len = UDF_EXT_LEN(len); 139 140 printf("["); 141 if (part_num >= 0) 142 printf("part %d, ", part_num); 143 printf("lb_num %d, len %d", lb_num, len); 144 if (flags) 145 printf(", flags %d", flags>>30); 146 printf("] "); 147 148 if (flags == UDF_EXT_REDIRECT) { 149 printf("\n\textent END\n\tallocation extent\n\t\t"); 150 } 151 152 slot++; 153 } 154 printf("\n\tl_ad END\n\n"); 155 } 156 #else 157 #define udf_node_dump(a) 158 #endif 159 160 161 static void 162 udf_assert_allocated(struct udf_mount *ump, uint16_t vpart_num, 163 uint32_t lb_num, uint32_t num_lb) 164 { 165 struct udf_bitmap *bitmap; 166 struct part_desc *pdesc; 167 uint32_t ptov; 168 uint32_t bitval; 169 uint8_t *bpos; 170 int bit; 171 int phys_part; 172 int ok; 173 174 DPRINTF(PARANOIA, ("udf_assert_allocated: check virt lbnum %d " 175 "part %d + %d sect\n", lb_num, vpart_num, num_lb)); 176 177 /* get partition backing up this vpart_num */ 178 pdesc = ump->partitions[ump->vtop[vpart_num]]; 179 180 switch (ump->vtop_tp[vpart_num]) { 181 case UDF_VTOP_TYPE_PHYS : 182 case UDF_VTOP_TYPE_SPARABLE : 183 /* free space to freed or unallocated space bitmap */ 184 ptov = udf_rw32(pdesc->start_loc); 185 phys_part = ump->vtop[vpart_num]; 186 187 /* use unallocated bitmap */ 188 bitmap = &ump->part_unalloc_bits[phys_part]; 189 190 /* if no bitmaps are defined, bail out */ 191 if (bitmap->bits == NULL) 192 break; 193 194 /* check bits */ 195 KASSERT(bitmap->bits); 196 ok = 1; 197 bpos = bitmap->bits + lb_num/8; 198 bit = lb_num % 8; 199 while (num_lb > 0) { 200 bitval = (1 << bit); 201 DPRINTF(PARANOIA, ("XXX : check %d, %p, bit %d\n", 202 lb_num, bpos, bit)); 203 KASSERT(bitmap->bits + lb_num/8 == bpos); 204 if (*bpos & bitval) { 205 printf("\tlb_num %d is NOT marked busy\n", 206 lb_num); 207 ok = 0; 208 } 209 lb_num++; num_lb--; 210 bit = (bit + 1) % 8; 211 if (bit == 0) 212 bpos++; 213 } 214 if (!ok) { 215 /* KASSERT(0); */ 216 } 217 218 break; 219 case UDF_VTOP_TYPE_VIRT : 220 /* TODO check space */ 221 KASSERT(num_lb == 1); 222 break; 223 case UDF_VTOP_TYPE_META : 224 /* TODO check space in the metadata bitmap */ 225 default: 226 /* not implemented */ 227 break; 228 } 229 } 230 231 232 static void 233 udf_node_sanity_check(struct udf_node *udf_node, 234 uint64_t *cnt_inflen, uint64_t *cnt_logblksrec) 235 { 236 union dscrptr *dscr; 237 struct file_entry *fe; 238 struct extfile_entry *efe; 239 struct icb_tag *icbtag; 240 struct long_ad s_ad; 241 uint64_t inflen, logblksrec; 242 uint32_t icbflags, addr_type; 243 uint32_t len, lb_num, l_ea, l_ad, max_l_ad; 244 uint16_t part_num; 245 uint8_t *data_pos; 246 int dscr_size, lb_size, flags, whole_lb; 247 int i, slot, eof; 248 249 // KASSERT(mutex_owned(&udf_node->ump->allocate_mutex)); 250 251 if (1) 252 udf_node_dump(udf_node); 253 254 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 255 256 fe = udf_node->fe; 257 efe = udf_node->efe; 258 if (fe) { 259 dscr = (union dscrptr *) fe; 260 icbtag = &fe->icbtag; 261 inflen = udf_rw64(fe->inf_len); 262 dscr_size = sizeof(struct file_entry) -1; 263 logblksrec = udf_rw64(fe->logblks_rec); 264 l_ad = udf_rw32(fe->l_ad); 265 l_ea = udf_rw32(fe->l_ea); 266 } else { 267 dscr = (union dscrptr *) efe; 268 icbtag = &efe->icbtag; 269 inflen = udf_rw64(efe->inf_len); 270 dscr_size = sizeof(struct extfile_entry) -1; 271 logblksrec = udf_rw64(efe->logblks_rec); 272 l_ad = udf_rw32(efe->l_ad); 273 l_ea = udf_rw32(efe->l_ea); 274 } 275 data_pos = (uint8_t *) dscr + dscr_size + l_ea; 276 max_l_ad = lb_size - dscr_size - l_ea; 277 icbflags = udf_rw16(icbtag->flags); 278 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 279 280 /* check if tail is zero */ 281 DPRINTF(PARANOIA, ("Sanity check blank tail\n")); 282 for (i = l_ad; i < max_l_ad; i++) { 283 if (data_pos[i] != 0) 284 printf( "sanity_check: violation: node byte %d " 285 "has value %d\n", i, data_pos[i]); 286 } 287 288 /* reset counters */ 289 *cnt_inflen = 0; 290 *cnt_logblksrec = 0; 291 292 if (addr_type == UDF_ICB_INTERN_ALLOC) { 293 KASSERT(l_ad <= max_l_ad); 294 KASSERT(l_ad == inflen); 295 *cnt_inflen = inflen; 296 return; 297 } 298 299 /* start counting */ 300 whole_lb = 1; 301 slot = 0; 302 for (;;) { 303 udf_get_adslot(udf_node, slot, &s_ad, &eof); 304 if (eof) 305 break; 306 KASSERT(whole_lb == 1); 307 308 part_num = udf_rw16(s_ad.loc.part_num); 309 lb_num = udf_rw32(s_ad.loc.lb_num); 310 len = udf_rw32(s_ad.len); 311 flags = UDF_EXT_FLAGS(len); 312 len = UDF_EXT_LEN(len); 313 314 if (flags != UDF_EXT_REDIRECT) { 315 *cnt_inflen += len; 316 if (flags == UDF_EXT_ALLOCATED) { 317 *cnt_logblksrec += (len + lb_size -1) / lb_size; 318 } 319 } else { 320 KASSERT(len == lb_size); 321 } 322 /* check allocation */ 323 if (flags == UDF_EXT_ALLOCATED) 324 udf_assert_allocated(udf_node->ump, part_num, lb_num, 325 (len + lb_size - 1) / lb_size); 326 327 /* check whole lb */ 328 whole_lb = ((len % lb_size) == 0); 329 330 slot++; 331 } 332 /* rest should be zero (ad_off > l_ad < max_l_ad - adlen) */ 333 334 KASSERT(*cnt_inflen == inflen); 335 KASSERT(*cnt_logblksrec == logblksrec); 336 337 // KASSERT(mutex_owned(&udf_node->ump->allocate_mutex)); 338 } 339 #else 340 static void 341 udf_node_sanity_check(struct udf_node *udf_node, 342 uint64_t *cnt_inflen, uint64_t *cnt_logblksrec) { 343 struct file_entry *fe; 344 struct extfile_entry *efe; 345 uint64_t inflen, logblksrec; 346 347 fe = udf_node->fe; 348 efe = udf_node->efe; 349 if (fe) { 350 inflen = udf_rw64(fe->inf_len); 351 logblksrec = udf_rw64(fe->logblks_rec); 352 } else { 353 inflen = udf_rw64(efe->inf_len); 354 logblksrec = udf_rw64(efe->logblks_rec); 355 } 356 *cnt_logblksrec = logblksrec; 357 *cnt_inflen = inflen; 358 } 359 #endif 360 361 /* --------------------------------------------------------------------- */ 362 363 void 364 udf_calc_freespace(struct udf_mount *ump, uint64_t *sizeblks, uint64_t *freeblks) 365 { 366 struct logvol_int_desc *lvid; 367 uint32_t *pos1, *pos2; 368 int vpart, num_vpart; 369 370 lvid = ump->logvol_integrity; 371 *freeblks = *sizeblks = 0; 372 373 /* 374 * Sequentials media report free space directly (CD/DVD/BD-R), for the 375 * other media we need the logical volume integrity. 376 * 377 * We sum all free space up here regardless of type. 378 */ 379 380 KASSERT(lvid); 381 num_vpart = udf_rw32(lvid->num_part); 382 383 if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) { 384 /* use track info directly summing if there are 2 open */ 385 /* XXX assumption at most two tracks open */ 386 *freeblks = ump->data_track.free_blocks; 387 if (ump->data_track.tracknr != ump->metadata_track.tracknr) 388 *freeblks += ump->metadata_track.free_blocks; 389 *sizeblks = ump->discinfo.last_possible_lba; 390 } else { 391 /* free and used space for mountpoint based on logvol integrity */ 392 for (vpart = 0; vpart < num_vpart; vpart++) { 393 pos1 = &lvid->tables[0] + vpart; 394 pos2 = &lvid->tables[0] + num_vpart + vpart; 395 if (udf_rw32(*pos1) != (uint32_t) -1) { 396 *freeblks += udf_rw32(*pos1); 397 *sizeblks += udf_rw32(*pos2); 398 } 399 } 400 } 401 /* adjust for accounted uncommitted blocks */ 402 for (vpart = 0; vpart < num_vpart; vpart++) 403 *freeblks -= ump->uncommitted_lbs[vpart]; 404 405 if (*freeblks > UDF_DISC_SLACK) { 406 *freeblks -= UDF_DISC_SLACK; 407 } else { 408 *freeblks = 0; 409 } 410 } 411 412 413 static void 414 udf_calc_vpart_freespace(struct udf_mount *ump, uint16_t vpart_num, uint64_t *freeblks) 415 { 416 struct logvol_int_desc *lvid; 417 uint32_t *pos1; 418 419 lvid = ump->logvol_integrity; 420 *freeblks = 0; 421 422 /* 423 * Sequentials media report free space directly (CD/DVD/BD-R), for the 424 * other media we need the logical volume integrity. 425 * 426 * We sum all free space up here regardless of type. 427 */ 428 429 KASSERT(lvid); 430 if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) { 431 /* XXX assumption at most two tracks open */ 432 if (vpart_num == ump->data_part) { 433 *freeblks = ump->data_track.free_blocks; 434 } else { 435 *freeblks = ump->metadata_track.free_blocks; 436 } 437 } else { 438 /* free and used space for mountpoint based on logvol integrity */ 439 pos1 = &lvid->tables[0] + vpart_num; 440 if (udf_rw32(*pos1) != (uint32_t) -1) 441 *freeblks += udf_rw32(*pos1); 442 } 443 444 /* adjust for accounted uncommitted blocks */ 445 if (*freeblks > ump->uncommitted_lbs[vpart_num]) { 446 *freeblks -= ump->uncommitted_lbs[vpart_num]; 447 } else { 448 *freeblks = 0; 449 } 450 } 451 452 /* --------------------------------------------------------------------- */ 453 454 int 455 udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc, 456 uint32_t *lb_numres, uint32_t *extres) 457 { 458 struct part_desc *pdesc; 459 struct spare_map_entry *sme; 460 struct long_ad s_icb_loc; 461 uint64_t foffset, end_foffset; 462 uint32_t lb_size, len; 463 uint32_t lb_num, lb_rel, lb_packet; 464 uint32_t udf_rw32_lbmap, ext_offset; 465 uint16_t vpart; 466 int rel, part, error, eof, slot, flags; 467 468 assert(ump && icb_loc && lb_numres); 469 470 vpart = udf_rw16(icb_loc->loc.part_num); 471 lb_num = udf_rw32(icb_loc->loc.lb_num); 472 if (vpart > UDF_VTOP_RAWPART) 473 return EINVAL; 474 475 translate_again: 476 part = ump->vtop[vpart]; 477 pdesc = ump->partitions[part]; 478 479 switch (ump->vtop_tp[vpart]) { 480 case UDF_VTOP_TYPE_RAW : 481 /* 1:1 to the end of the device */ 482 *lb_numres = lb_num; 483 *extres = INT_MAX; 484 return 0; 485 case UDF_VTOP_TYPE_PHYS : 486 /* transform into its disc logical block */ 487 if (lb_num > udf_rw32(pdesc->part_len)) 488 return EINVAL; 489 *lb_numres = lb_num + udf_rw32(pdesc->start_loc); 490 491 /* extent from here to the end of the partition */ 492 *extres = udf_rw32(pdesc->part_len) - lb_num; 493 return 0; 494 case UDF_VTOP_TYPE_VIRT : 495 /* only maps one logical block, lookup in VAT */ 496 if (lb_num >= ump->vat_entries) /* XXX > or >= ? */ 497 return EINVAL; 498 499 /* lookup in virtual allocation table file */ 500 mutex_enter(&ump->allocate_mutex); 501 error = udf_vat_read(ump->vat_node, 502 (uint8_t *) &udf_rw32_lbmap, 4, 503 ump->vat_offset + lb_num * 4); 504 mutex_exit(&ump->allocate_mutex); 505 506 if (error) 507 return error; 508 509 lb_num = udf_rw32(udf_rw32_lbmap); 510 511 /* transform into its disc logical block */ 512 if (lb_num > udf_rw32(pdesc->part_len)) 513 return EINVAL; 514 *lb_numres = lb_num + udf_rw32(pdesc->start_loc); 515 516 /* just one logical block */ 517 *extres = 1; 518 return 0; 519 case UDF_VTOP_TYPE_SPARABLE : 520 /* check if the packet containing the lb_num is remapped */ 521 lb_packet = lb_num / ump->sparable_packet_size; 522 lb_rel = lb_num % ump->sparable_packet_size; 523 524 for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) { 525 sme = &ump->sparing_table->entries[rel]; 526 if (lb_packet == udf_rw32(sme->org)) { 527 /* NOTE maps to absolute disc logical block! */ 528 *lb_numres = udf_rw32(sme->map) + lb_rel; 529 *extres = ump->sparable_packet_size - lb_rel; 530 return 0; 531 } 532 } 533 534 /* transform into its disc logical block */ 535 if (lb_num > udf_rw32(pdesc->part_len)) 536 return EINVAL; 537 *lb_numres = lb_num + udf_rw32(pdesc->start_loc); 538 539 /* rest of block */ 540 *extres = ump->sparable_packet_size - lb_rel; 541 return 0; 542 case UDF_VTOP_TYPE_META : 543 /* we have to look into the file's allocation descriptors */ 544 545 /* use metadatafile allocation mutex */ 546 lb_size = udf_rw32(ump->logical_vol->lb_size); 547 548 UDF_LOCK_NODE(ump->metadata_node, 0); 549 550 /* get first overlapping extent */ 551 foffset = 0; 552 slot = 0; 553 for (;;) { 554 udf_get_adslot(ump->metadata_node, 555 slot, &s_icb_loc, &eof); 556 DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, " 557 "len = %d, lb_num = %d, part = %d\n", 558 slot, eof, 559 UDF_EXT_FLAGS(udf_rw32(s_icb_loc.len)), 560 UDF_EXT_LEN(udf_rw32(s_icb_loc.len)), 561 udf_rw32(s_icb_loc.loc.lb_num), 562 udf_rw16(s_icb_loc.loc.part_num))); 563 if (eof) { 564 DPRINTF(TRANSLATE, 565 ("Meta partition translation " 566 "failed: can't seek location\n")); 567 UDF_UNLOCK_NODE(ump->metadata_node, 0); 568 return EINVAL; 569 } 570 len = udf_rw32(s_icb_loc.len); 571 flags = UDF_EXT_FLAGS(len); 572 len = UDF_EXT_LEN(len); 573 574 if (flags == UDF_EXT_REDIRECT) { 575 slot++; 576 continue; 577 } 578 579 end_foffset = foffset + len; 580 581 if (end_foffset > (uint64_t) lb_num * lb_size) 582 break; /* found */ 583 foffset = end_foffset; 584 slot++; 585 } 586 /* found overlapping slot */ 587 ext_offset = lb_num * lb_size - foffset; 588 589 /* process extent offset */ 590 lb_num = udf_rw32(s_icb_loc.loc.lb_num); 591 vpart = udf_rw16(s_icb_loc.loc.part_num); 592 lb_num += (ext_offset + lb_size -1) / lb_size; 593 ext_offset = 0; 594 595 UDF_UNLOCK_NODE(ump->metadata_node, 0); 596 if (flags != UDF_EXT_ALLOCATED) { 597 DPRINTF(TRANSLATE, ("Metadata partition translation " 598 "failed: not allocated\n")); 599 return EINVAL; 600 } 601 602 /* 603 * vpart and lb_num are updated, translate again since we 604 * might be mapped on sparable media 605 */ 606 goto translate_again; 607 default: 608 printf("UDF vtop translation scheme %d unimplemented yet\n", 609 ump->vtop_tp[vpart]); 610 } 611 612 return EINVAL; 613 } 614 615 616 /* XXX provisional primitive braindead version */ 617 /* TODO use ext_res */ 618 void 619 udf_translate_vtop_list(struct udf_mount *ump, uint32_t sectors, 620 uint16_t vpart_num, uint64_t *lmapping, uint64_t *pmapping) 621 { 622 struct long_ad loc; 623 uint32_t lb_numres, ext_res; 624 int sector; 625 626 for (sector = 0; sector < sectors; sector++) { 627 memset(&loc, 0, sizeof(struct long_ad)); 628 loc.loc.part_num = udf_rw16(vpart_num); 629 loc.loc.lb_num = udf_rw32(*lmapping); 630 udf_translate_vtop(ump, &loc, &lb_numres, &ext_res); 631 *pmapping = lb_numres; 632 lmapping++; pmapping++; 633 } 634 } 635 636 637 /* --------------------------------------------------------------------- */ 638 639 /* 640 * Translate an extent (in logical_blocks) into logical block numbers; used 641 * for read and write operations. DOESNT't check extents. 642 */ 643 644 int 645 udf_translate_file_extent(struct udf_node *udf_node, 646 uint32_t from, uint32_t num_lb, 647 uint64_t *map) 648 { 649 struct udf_mount *ump; 650 struct icb_tag *icbtag; 651 struct long_ad t_ad, s_ad; 652 uint64_t transsec; 653 uint64_t foffset, end_foffset; 654 uint32_t transsec32; 655 uint32_t lb_size; 656 uint32_t ext_offset; 657 uint32_t lb_num, len; 658 uint32_t overlap, translen; 659 uint16_t vpart_num; 660 int eof, error, flags; 661 int slot, addr_type, icbflags; 662 663 if (!udf_node) 664 return ENOENT; 665 666 KASSERT(num_lb > 0); 667 668 UDF_LOCK_NODE(udf_node, 0); 669 670 /* initialise derivative vars */ 671 ump = udf_node->ump; 672 lb_size = udf_rw32(ump->logical_vol->lb_size); 673 674 if (udf_node->fe) { 675 icbtag = &udf_node->fe->icbtag; 676 } else { 677 icbtag = &udf_node->efe->icbtag; 678 } 679 icbflags = udf_rw16(icbtag->flags); 680 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 681 682 /* do the work */ 683 if (addr_type == UDF_ICB_INTERN_ALLOC) { 684 *map = UDF_TRANS_INTERN; 685 UDF_UNLOCK_NODE(udf_node, 0); 686 return 0; 687 } 688 689 /* find first overlapping extent */ 690 foffset = 0; 691 slot = 0; 692 for (;;) { 693 udf_get_adslot(udf_node, slot, &s_ad, &eof); 694 DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, " 695 "lb_num = %d, part = %d\n", slot, eof, 696 UDF_EXT_FLAGS(udf_rw32(s_ad.len)), 697 UDF_EXT_LEN(udf_rw32(s_ad.len)), 698 udf_rw32(s_ad.loc.lb_num), 699 udf_rw16(s_ad.loc.part_num))); 700 if (eof) { 701 DPRINTF(TRANSLATE, 702 ("Translate file extent " 703 "failed: can't seek location\n")); 704 UDF_UNLOCK_NODE(udf_node, 0); 705 return EINVAL; 706 } 707 len = udf_rw32(s_ad.len); 708 flags = UDF_EXT_FLAGS(len); 709 len = UDF_EXT_LEN(len); 710 lb_num = udf_rw32(s_ad.loc.lb_num); 711 712 if (flags == UDF_EXT_REDIRECT) { 713 slot++; 714 continue; 715 } 716 717 end_foffset = foffset + len; 718 719 if (end_foffset > (uint64_t) from * lb_size) 720 break; /* found */ 721 foffset = end_foffset; 722 slot++; 723 } 724 /* found overlapping slot */ 725 ext_offset = (uint64_t) from * lb_size - foffset; 726 727 for (;;) { 728 udf_get_adslot(udf_node, slot, &s_ad, &eof); 729 DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, " 730 "lb_num = %d, part = %d\n", slot, eof, 731 UDF_EXT_FLAGS(udf_rw32(s_ad.len)), 732 UDF_EXT_LEN(udf_rw32(s_ad.len)), 733 udf_rw32(s_ad.loc.lb_num), 734 udf_rw16(s_ad.loc.part_num))); 735 if (eof) { 736 DPRINTF(TRANSLATE, 737 ("Translate file extent " 738 "failed: past eof\n")); 739 UDF_UNLOCK_NODE(udf_node, 0); 740 return EINVAL; 741 } 742 743 len = udf_rw32(s_ad.len); 744 flags = UDF_EXT_FLAGS(len); 745 len = UDF_EXT_LEN(len); 746 747 lb_num = udf_rw32(s_ad.loc.lb_num); 748 vpart_num = udf_rw16(s_ad.loc.part_num); 749 750 end_foffset = foffset + len; 751 752 /* process extent, don't forget to advance on ext_offset! */ 753 lb_num += (ext_offset + lb_size -1) / lb_size; 754 overlap = (len - ext_offset + lb_size -1) / lb_size; 755 ext_offset = 0; 756 757 /* 758 * note that the while(){} is nessisary for the extent that 759 * the udf_translate_vtop() returns doens't have to span the 760 * whole extent. 761 */ 762 763 overlap = MIN(overlap, num_lb); 764 while (overlap && (flags != UDF_EXT_REDIRECT)) { 765 switch (flags) { 766 case UDF_EXT_FREE : 767 case UDF_EXT_ALLOCATED_BUT_NOT_USED : 768 transsec = UDF_TRANS_ZERO; 769 translen = overlap; 770 while (overlap && num_lb && translen) { 771 *map++ = transsec; 772 lb_num++; 773 overlap--; num_lb--; translen--; 774 } 775 break; 776 case UDF_EXT_ALLOCATED : 777 t_ad.loc.lb_num = udf_rw32(lb_num); 778 t_ad.loc.part_num = udf_rw16(vpart_num); 779 error = udf_translate_vtop(ump, 780 &t_ad, &transsec32, &translen); 781 transsec = transsec32; 782 if (error) { 783 UDF_UNLOCK_NODE(udf_node, 0); 784 return error; 785 } 786 while (overlap && num_lb && translen) { 787 *map++ = transsec; 788 lb_num++; transsec++; 789 overlap--; num_lb--; translen--; 790 } 791 break; 792 default: 793 DPRINTF(TRANSLATE, 794 ("Translate file extent " 795 "failed: bad flags %x\n", flags)); 796 UDF_UNLOCK_NODE(udf_node, 0); 797 return EINVAL; 798 } 799 } 800 if (num_lb == 0) 801 break; 802 803 if (flags != UDF_EXT_REDIRECT) 804 foffset = end_foffset; 805 slot++; 806 } 807 UDF_UNLOCK_NODE(udf_node, 0); 808 809 return 0; 810 } 811 812 /* --------------------------------------------------------------------- */ 813 814 static int 815 udf_search_free_vatloc(struct udf_mount *ump, uint32_t *lbnumres) 816 { 817 uint32_t lb_size, lb_num, lb_map, udf_rw32_lbmap; 818 uint8_t *blob; 819 int entry, chunk, found, error; 820 821 KASSERT(ump); 822 KASSERT(ump->logical_vol); 823 824 lb_size = udf_rw32(ump->logical_vol->lb_size); 825 blob = malloc(lb_size, M_UDFTEMP, M_WAITOK); 826 827 /* TODO static allocation of search chunk */ 828 829 lb_num = MIN(ump->vat_entries, ump->vat_last_free_lb); 830 found = 0; 831 error = 0; 832 entry = 0; 833 do { 834 chunk = MIN(lb_size, (ump->vat_entries - lb_num) * 4); 835 if (chunk <= 0) 836 break; 837 /* load in chunk */ 838 error = udf_vat_read(ump->vat_node, blob, chunk, 839 ump->vat_offset + lb_num * 4); 840 841 if (error) 842 break; 843 844 /* search this chunk */ 845 for (entry=0; entry < chunk /4; entry++, lb_num++) { 846 udf_rw32_lbmap = *((uint32_t *) (blob + entry * 4)); 847 lb_map = udf_rw32(udf_rw32_lbmap); 848 if (lb_map == 0xffffffff) { 849 found = 1; 850 break; 851 } 852 } 853 } while (!found); 854 if (error) { 855 printf("udf_search_free_vatloc: error reading in vat chunk " 856 "(lb %d, size %d)\n", lb_num, chunk); 857 } 858 859 if (!found) { 860 /* extend VAT */ 861 DPRINTF(WRITE, ("udf_search_free_vatloc: extending\n")); 862 lb_num = ump->vat_entries; 863 ump->vat_entries++; 864 } 865 866 /* mark entry with initialiser just in case */ 867 lb_map = udf_rw32(0xfffffffe); 868 udf_vat_write(ump->vat_node, (uint8_t *) &lb_map, 4, 869 ump->vat_offset + lb_num *4); 870 ump->vat_last_free_lb = lb_num; 871 872 free(blob, M_UDFTEMP); 873 *lbnumres = lb_num; 874 return 0; 875 } 876 877 878 static void 879 udf_bitmap_allocate(struct udf_bitmap *bitmap, int ismetadata, 880 uint32_t *num_lb, uint64_t *lmappos) 881 { 882 uint32_t offset, lb_num, bit; 883 int32_t diff; 884 uint8_t *bpos; 885 int pass; 886 887 if (!ismetadata) { 888 /* heuristic to keep the two pointers not too close */ 889 diff = bitmap->data_pos - bitmap->metadata_pos; 890 if ((diff >= 0) && (diff < 1024)) 891 bitmap->data_pos = bitmap->metadata_pos + 1024; 892 } 893 offset = ismetadata ? bitmap->metadata_pos : bitmap->data_pos; 894 offset &= ~7; 895 for (pass = 0; pass < 2; pass++) { 896 if (offset >= bitmap->max_offset) 897 offset = 0; 898 899 while (offset < bitmap->max_offset) { 900 if (*num_lb == 0) 901 break; 902 903 /* use first bit not set */ 904 bpos = bitmap->bits + offset/8; 905 bit = ffs(*bpos); /* returns 0 or 1..8 */ 906 if (bit == 0) { 907 offset += 8; 908 continue; 909 } 910 911 /* check for ffs overshoot */ 912 if (offset + bit-1 >= bitmap->max_offset) { 913 offset = bitmap->max_offset; 914 break; 915 } 916 917 DPRINTF(PARANOIA, ("XXX : allocate %d, %p, bit %d\n", 918 offset + bit -1, bpos, bit-1)); 919 *bpos &= ~(1 << (bit-1)); 920 lb_num = offset + bit-1; 921 *lmappos++ = lb_num; 922 *num_lb = *num_lb - 1; 923 // offset = (offset & ~7); 924 } 925 } 926 927 if (ismetadata) { 928 bitmap->metadata_pos = offset; 929 } else { 930 bitmap->data_pos = offset; 931 } 932 } 933 934 935 static void 936 udf_bitmap_free(struct udf_bitmap *bitmap, uint32_t lb_num, uint32_t num_lb) 937 { 938 uint32_t offset; 939 uint32_t bit, bitval; 940 uint8_t *bpos; 941 942 offset = lb_num; 943 944 /* starter bits */ 945 bpos = bitmap->bits + offset/8; 946 bit = offset % 8; 947 while ((bit != 0) && (num_lb > 0)) { 948 bitval = (1 << bit); 949 KASSERT((*bpos & bitval) == 0); 950 DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n", 951 offset, bpos, bit)); 952 *bpos |= bitval; 953 offset++; num_lb--; 954 bit = (bit + 1) % 8; 955 } 956 if (num_lb == 0) 957 return; 958 959 /* whole bytes */ 960 KASSERT(bit == 0); 961 bpos = bitmap->bits + offset / 8; 962 while (num_lb >= 8) { 963 KASSERT((*bpos == 0)); 964 DPRINTF(PARANOIA, ("XXX : free %d + 8, %p\n", offset, bpos)); 965 *bpos = 255; 966 offset += 8; num_lb -= 8; 967 bpos++; 968 } 969 970 /* stop bits */ 971 KASSERT(num_lb < 8); 972 bit = 0; 973 while (num_lb > 0) { 974 bitval = (1 << bit); 975 KASSERT((*bpos & bitval) == 0); 976 DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n", 977 offset, bpos, bit)); 978 *bpos |= bitval; 979 offset++; num_lb--; 980 bit = (bit + 1) % 8; 981 } 982 } 983 984 985 static uint32_t 986 udf_bitmap_check_trunc_free(struct udf_bitmap *bitmap, uint32_t to_trunc) 987 { 988 uint32_t seq_free, offset; 989 uint8_t *bpos; 990 uint8_t bit, bitval; 991 992 DPRINTF(RESERVE, ("\ttrying to trunc %d bits from bitmap\n", to_trunc)); 993 offset = bitmap->max_offset - to_trunc; 994 995 /* starter bits (if any) */ 996 bpos = bitmap->bits + offset/8; 997 bit = offset % 8; 998 seq_free = 0; 999 while (to_trunc > 0) { 1000 seq_free++; 1001 bitval = (1 << bit); 1002 if (!(*bpos & bitval)) 1003 seq_free = 0; 1004 offset++; to_trunc--; 1005 bit++; 1006 if (bit == 8) { 1007 bpos++; 1008 bit = 0; 1009 } 1010 } 1011 1012 DPRINTF(RESERVE, ("\tfound %d sequential free bits in bitmap\n", seq_free)); 1013 return seq_free; 1014 } 1015 1016 /* --------------------------------------------------------------------- */ 1017 1018 /* 1019 * We check for overall disc space with a margin to prevent critical 1020 * conditions. If disc space is low we try to force a sync() to improve our 1021 * estimates. When confronted with meta-data partition size shortage we know 1022 * we have to check if it can be extended and we need to extend it when 1023 * needed. 1024 * 1025 * A 2nd strategy we could use when disc space is getting low on a disc 1026 * formatted with a meta-data partition is to see if there are sparse areas in 1027 * the meta-data partition and free blocks there for extra data. 1028 */ 1029 1030 void 1031 udf_do_reserve_space(struct udf_mount *ump, struct udf_node *udf_node, 1032 uint16_t vpart_num, uint32_t num_lb) 1033 { 1034 ump->uncommitted_lbs[vpart_num] += num_lb; 1035 if (udf_node) 1036 udf_node->uncommitted_lbs += num_lb; 1037 } 1038 1039 1040 void 1041 udf_do_unreserve_space(struct udf_mount *ump, struct udf_node *udf_node, 1042 uint16_t vpart_num, uint32_t num_lb) 1043 { 1044 ump->uncommitted_lbs[vpart_num] -= num_lb; 1045 if (ump->uncommitted_lbs[vpart_num] < 0) { 1046 DPRINTF(RESERVE, ("UDF: underflow on partition reservation, " 1047 "part %d: %d\n", vpart_num, 1048 ump->uncommitted_lbs[vpart_num])); 1049 ump->uncommitted_lbs[vpart_num] = 0; 1050 } 1051 if (udf_node) { 1052 udf_node->uncommitted_lbs -= num_lb; 1053 if (udf_node->uncommitted_lbs < 0) { 1054 DPRINTF(RESERVE, ("UDF: underflow of node " 1055 "reservation : %d\n", 1056 udf_node->uncommitted_lbs)); 1057 udf_node->uncommitted_lbs = 0; 1058 } 1059 } 1060 } 1061 1062 1063 int 1064 udf_reserve_space(struct udf_mount *ump, struct udf_node *udf_node, 1065 int udf_c_type, uint16_t vpart_num, uint32_t num_lb, int can_fail) 1066 { 1067 uint64_t freeblks; 1068 uint64_t slack; 1069 int i, error; 1070 1071 slack = 0; 1072 if (can_fail) 1073 slack = UDF_DISC_SLACK; 1074 1075 error = 0; 1076 mutex_enter(&ump->allocate_mutex); 1077 1078 /* check if there is enough space available */ 1079 for (i = 0; i < 3; i++) { /* XXX arbitrary number */ 1080 udf_calc_vpart_freespace(ump, vpart_num, &freeblks); 1081 if (num_lb + slack < freeblks) 1082 break; 1083 /* issue SYNC */ 1084 DPRINTF(RESERVE, ("udf_reserve_space: issuing sync\n")); 1085 mutex_exit(&ump->allocate_mutex); 1086 udf_do_sync(ump, FSCRED, 0); 1087 mutex_enter(&mntvnode_lock); 1088 /* 1/8 second wait */ 1089 cv_timedwait(&ump->dirtynodes_cv, &mntvnode_lock, 1090 hz/8); 1091 mutex_exit(&mntvnode_lock); 1092 mutex_enter(&ump->allocate_mutex); 1093 } 1094 1095 /* check if there is enough space available now */ 1096 udf_calc_vpart_freespace(ump, vpart_num, &freeblks); 1097 if (num_lb + slack >= freeblks) { 1098 DPRINTF(RESERVE, ("udf_reserve_space: try to redistribute " 1099 "partition space\n")); 1100 DPRINTF(RESERVE, ("\tvpart %d, type %d is full\n", 1101 vpart_num, ump->vtop_alloc[vpart_num])); 1102 /* Try to redistribute space if possible */ 1103 udf_collect_free_space_for_vpart(ump, vpart_num, num_lb + slack); 1104 } 1105 1106 /* check if there is enough space available now */ 1107 udf_calc_vpart_freespace(ump, vpart_num, &freeblks); 1108 if (num_lb + slack <= freeblks) { 1109 udf_do_reserve_space(ump, udf_node, vpart_num, num_lb); 1110 } else { 1111 DPRINTF(RESERVE, ("udf_reserve_space: out of disc space\n")); 1112 error = ENOSPC; 1113 } 1114 1115 mutex_exit(&ump->allocate_mutex); 1116 return error; 1117 } 1118 1119 1120 void 1121 udf_cleanup_reservation(struct udf_node *udf_node) 1122 { 1123 struct udf_mount *ump = udf_node->ump; 1124 int vpart_num; 1125 1126 mutex_enter(&ump->allocate_mutex); 1127 1128 /* compensate for overlapping blocks */ 1129 DPRINTF(RESERVE, ("UDF: overlapped %d blocks in count\n", udf_node->uncommitted_lbs)); 1130 1131 vpart_num = udf_get_record_vpart(ump, udf_get_c_type(udf_node)); 1132 udf_do_unreserve_space(ump, udf_node, vpart_num, udf_node->uncommitted_lbs); 1133 1134 DPRINTF(RESERVE, ("\ttotal now %d\n", ump->uncommitted_lbs[vpart_num])); 1135 1136 /* sanity */ 1137 if (ump->uncommitted_lbs[vpart_num] < 0) 1138 ump->uncommitted_lbs[vpart_num] = 0; 1139 1140 mutex_exit(&ump->allocate_mutex); 1141 } 1142 1143 /* --------------------------------------------------------------------- */ 1144 1145 /* 1146 * Allocate an extent of given length on given virt. partition. It doesn't 1147 * have to be one stretch. 1148 */ 1149 1150 int 1151 udf_allocate_space(struct udf_mount *ump, struct udf_node *udf_node, 1152 int udf_c_type, uint16_t vpart_num, uint32_t num_lb, uint64_t *lmapping) 1153 { 1154 struct mmc_trackinfo *alloc_track, *other_track; 1155 struct udf_bitmap *bitmap; 1156 struct part_desc *pdesc; 1157 struct logvol_int_desc *lvid; 1158 uint64_t *lmappos; 1159 uint32_t ptov, lb_num, *freepos, free_lbs; 1160 int lb_size __diagused, alloc_num_lb; 1161 int alloc_type, error; 1162 int is_node; 1163 1164 DPRINTF(CALL, ("udf_allocate_space(ctype %d, vpart %d, num_lb %d\n", 1165 udf_c_type, vpart_num, num_lb)); 1166 mutex_enter(&ump->allocate_mutex); 1167 1168 lb_size = udf_rw32(ump->logical_vol->lb_size); 1169 KASSERT(lb_size == ump->discinfo.sector_size); 1170 1171 alloc_type = ump->vtop_alloc[vpart_num]; 1172 is_node = (udf_c_type == UDF_C_NODE); 1173 1174 lmappos = lmapping; 1175 error = 0; 1176 switch (alloc_type) { 1177 case UDF_ALLOC_VAT : 1178 /* search empty slot in VAT file */ 1179 KASSERT(num_lb == 1); 1180 error = udf_search_free_vatloc(ump, &lb_num); 1181 if (!error) { 1182 *lmappos = lb_num; 1183 1184 /* reserve on the backing sequential partition since 1185 * that partition is credited back later */ 1186 udf_do_reserve_space(ump, udf_node, 1187 ump->vtop[vpart_num], num_lb); 1188 } 1189 break; 1190 case UDF_ALLOC_SEQUENTIAL : 1191 /* sequential allocation on recordable media */ 1192 /* get partition backing up this vpart_num_num */ 1193 pdesc = ump->partitions[ump->vtop[vpart_num]]; 1194 1195 /* calculate offset from physical base partition */ 1196 ptov = udf_rw32(pdesc->start_loc); 1197 1198 /* get our track descriptors */ 1199 if (vpart_num == ump->node_part) { 1200 alloc_track = &ump->metadata_track; 1201 other_track = &ump->data_track; 1202 } else { 1203 alloc_track = &ump->data_track; 1204 other_track = &ump->metadata_track; 1205 } 1206 1207 /* allocate */ 1208 for (lb_num = 0; lb_num < num_lb; lb_num++) { 1209 *lmappos++ = alloc_track->next_writable - ptov; 1210 alloc_track->next_writable++; 1211 alloc_track->free_blocks--; 1212 } 1213 1214 /* keep other track up-to-date */ 1215 if (alloc_track->tracknr == other_track->tracknr) 1216 memcpy(other_track, alloc_track, 1217 sizeof(struct mmc_trackinfo)); 1218 break; 1219 case UDF_ALLOC_SPACEMAP : 1220 /* try to allocate on unallocated bits */ 1221 alloc_num_lb = num_lb; 1222 bitmap = &ump->part_unalloc_bits[vpart_num]; 1223 udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos); 1224 ump->lvclose |= UDF_WRITE_PART_BITMAPS; 1225 1226 /* have we allocated all? */ 1227 if (alloc_num_lb) { 1228 /* TODO convert freed to unalloc and try again */ 1229 /* free allocated piece for now */ 1230 lmappos = lmapping; 1231 for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) { 1232 udf_bitmap_free(bitmap, *lmappos++, 1); 1233 } 1234 error = ENOSPC; 1235 } 1236 if (!error) { 1237 /* adjust freecount */ 1238 lvid = ump->logvol_integrity; 1239 freepos = &lvid->tables[0] + vpart_num; 1240 free_lbs = udf_rw32(*freepos); 1241 *freepos = udf_rw32(free_lbs - num_lb); 1242 } 1243 break; 1244 case UDF_ALLOC_METABITMAP : /* UDF 2.50, 2.60 BluRay-RE */ 1245 /* allocate on metadata unallocated bits */ 1246 alloc_num_lb = num_lb; 1247 bitmap = &ump->metadata_unalloc_bits; 1248 udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos); 1249 ump->lvclose |= UDF_WRITE_PART_BITMAPS; 1250 1251 /* have we allocated all? */ 1252 if (alloc_num_lb) { 1253 /* YIKES! TODO we need to extend the metadata partition */ 1254 /* free allocated piece for now */ 1255 lmappos = lmapping; 1256 for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) { 1257 udf_bitmap_free(bitmap, *lmappos++, 1); 1258 } 1259 error = ENOSPC; 1260 } 1261 if (!error) { 1262 /* adjust freecount */ 1263 lvid = ump->logvol_integrity; 1264 freepos = &lvid->tables[0] + vpart_num; 1265 free_lbs = udf_rw32(*freepos); 1266 *freepos = udf_rw32(free_lbs - num_lb); 1267 } 1268 break; 1269 case UDF_ALLOC_METASEQUENTIAL : /* UDF 2.60 BluRay-R */ 1270 case UDF_ALLOC_RELAXEDSEQUENTIAL : /* UDF 2.50/~meta BluRay-R */ 1271 printf("ALERT: udf_allocate_space : allocation %d " 1272 "not implemented yet!\n", alloc_type); 1273 /* TODO implement, doesn't have to be contiguous */ 1274 error = ENOSPC; 1275 break; 1276 } 1277 1278 if (!error) { 1279 /* credit our partition since we have committed the space */ 1280 udf_do_unreserve_space(ump, udf_node, vpart_num, num_lb); 1281 } 1282 1283 #ifdef DEBUG 1284 if (udf_verbose & UDF_DEBUG_ALLOC) { 1285 lmappos = lmapping; 1286 printf("udf_allocate_space, allocated logical lba :\n"); 1287 for (lb_num = 0; lb_num < num_lb; lb_num++) { 1288 printf("%s %"PRIu64, (lb_num > 0)?",":"", 1289 *lmappos++); 1290 } 1291 printf("\n"); 1292 } 1293 #endif 1294 mutex_exit(&ump->allocate_mutex); 1295 1296 return error; 1297 } 1298 1299 /* --------------------------------------------------------------------- */ 1300 1301 void 1302 udf_free_allocated_space(struct udf_mount *ump, uint32_t lb_num, 1303 uint16_t vpart_num, uint32_t num_lb) 1304 { 1305 struct udf_bitmap *bitmap; 1306 struct logvol_int_desc *lvid; 1307 uint32_t lb_map, udf_rw32_lbmap; 1308 uint32_t *freepos, free_lbs; 1309 int phys_part; 1310 int error __diagused; 1311 1312 DPRINTF(ALLOC, ("udf_free_allocated_space: freeing virt lbnum %d " 1313 "part %d + %d sect\n", lb_num, vpart_num, num_lb)); 1314 1315 /* no use freeing zero length */ 1316 if (num_lb == 0) 1317 return; 1318 1319 mutex_enter(&ump->allocate_mutex); 1320 1321 switch (ump->vtop_tp[vpart_num]) { 1322 case UDF_VTOP_TYPE_PHYS : 1323 case UDF_VTOP_TYPE_SPARABLE : 1324 /* free space to freed or unallocated space bitmap */ 1325 phys_part = ump->vtop[vpart_num]; 1326 1327 /* first try freed space bitmap */ 1328 bitmap = &ump->part_freed_bits[phys_part]; 1329 1330 /* if not defined, use unallocated bitmap */ 1331 if (bitmap->bits == NULL) 1332 bitmap = &ump->part_unalloc_bits[phys_part]; 1333 1334 /* if no bitmaps are defined, bail out; XXX OK? */ 1335 if (bitmap->bits == NULL) 1336 break; 1337 1338 /* free bits if its defined */ 1339 KASSERT(bitmap->bits); 1340 ump->lvclose |= UDF_WRITE_PART_BITMAPS; 1341 udf_bitmap_free(bitmap, lb_num, num_lb); 1342 1343 /* adjust freecount */ 1344 lvid = ump->logvol_integrity; 1345 freepos = &lvid->tables[0] + vpart_num; 1346 free_lbs = udf_rw32(*freepos); 1347 *freepos = udf_rw32(free_lbs + num_lb); 1348 break; 1349 case UDF_VTOP_TYPE_VIRT : 1350 /* free this VAT entry */ 1351 KASSERT(num_lb == 1); 1352 1353 lb_map = 0xffffffff; 1354 udf_rw32_lbmap = udf_rw32(lb_map); 1355 error = udf_vat_write(ump->vat_node, 1356 (uint8_t *) &udf_rw32_lbmap, 4, 1357 ump->vat_offset + lb_num * 4); 1358 KASSERT(error == 0); 1359 ump->vat_last_free_lb = MIN(ump->vat_last_free_lb, lb_num); 1360 break; 1361 case UDF_VTOP_TYPE_META : 1362 /* free space in the metadata bitmap */ 1363 bitmap = &ump->metadata_unalloc_bits; 1364 KASSERT(bitmap->bits); 1365 1366 ump->lvclose |= UDF_WRITE_PART_BITMAPS; 1367 udf_bitmap_free(bitmap, lb_num, num_lb); 1368 1369 /* adjust freecount */ 1370 lvid = ump->logvol_integrity; 1371 freepos = &lvid->tables[0] + vpart_num; 1372 free_lbs = udf_rw32(*freepos); 1373 *freepos = udf_rw32(free_lbs + num_lb); 1374 break; 1375 default: 1376 printf("ALERT: udf_free_allocated_space : allocation %d " 1377 "not implemented yet!\n", ump->vtop_tp[vpart_num]); 1378 break; 1379 } 1380 1381 mutex_exit(&ump->allocate_mutex); 1382 } 1383 1384 /* --------------------------------------------------------------------- */ 1385 1386 /* 1387 * Special function to synchronise the metadatamirror file when they change on 1388 * resizing. When the metadatafile is actually duplicated, this action is a 1389 * no-op since they describe different extents on the disc. 1390 */ 1391 1392 void 1393 udf_synchronise_metadatamirror_node(struct udf_mount *ump) 1394 { 1395 struct udf_node *meta_node, *metamirror_node; 1396 struct long_ad s_ad; 1397 uint32_t len, flags; 1398 int slot, cpy_slot; 1399 int error, eof; 1400 1401 if (ump->metadata_flags & METADATA_DUPLICATED) 1402 return; 1403 1404 meta_node = ump->metadata_node; 1405 metamirror_node = ump->metadatamirror_node; 1406 1407 /* 1) wipe mirror node */ 1408 udf_wipe_adslots(metamirror_node); 1409 1410 /* 2) copy all node descriptors from the meta_node */ 1411 slot = 0; 1412 cpy_slot = 0; 1413 for (;;) { 1414 udf_get_adslot(meta_node, slot, &s_ad, &eof); 1415 if (eof) 1416 break; 1417 len = udf_rw32(s_ad.len); 1418 flags = UDF_EXT_FLAGS(len); 1419 len = UDF_EXT_LEN(len); 1420 1421 if (flags == UDF_EXT_REDIRECT) { 1422 slot++; 1423 continue; 1424 } 1425 1426 error = udf_append_adslot(metamirror_node, &cpy_slot, &s_ad); 1427 if (error) { 1428 /* WTF, this shouldn't happen, what to do now? */ 1429 panic("udf_synchronise_metadatamirror_node failed!"); 1430 } 1431 cpy_slot++; 1432 slot++; 1433 } 1434 1435 /* 3) adjust metamirror_node size */ 1436 if (meta_node->fe) { 1437 KASSERT(metamirror_node->fe); 1438 metamirror_node->fe->inf_len = meta_node->fe->inf_len; 1439 } else { 1440 KASSERT(meta_node->efe); 1441 KASSERT(metamirror_node->efe); 1442 metamirror_node->efe->inf_len = meta_node->efe->inf_len; 1443 metamirror_node->efe->obj_size = meta_node->efe->obj_size; 1444 } 1445 1446 /* for sanity */ 1447 udf_count_alloc_exts(metamirror_node); 1448 } 1449 1450 /* --------------------------------------------------------------------- */ 1451 1452 /* 1453 * When faced with an out of space but there is still space available on other 1454 * partitions, try to redistribute the space. This is only defined for media 1455 * using Metadata partitions. 1456 * 1457 * There are two formats to deal with. Either its a `normal' metadata 1458 * partition and we can move blocks between a metadata bitmap and its 1459 * companion data spacemap OR its a UDF 2.60 formatted BluRay-R disc with POW 1460 * and a metadata partition. 1461 */ 1462 1463 /* implementation limit: ump->datapart is the companion partition */ 1464 static uint32_t 1465 udf_trunc_metadatapart(struct udf_mount *ump, uint32_t num_lb) 1466 { 1467 struct udf_node *bitmap_node; 1468 struct udf_bitmap *bitmap; 1469 struct space_bitmap_desc *sbd, *new_sbd; 1470 struct logvol_int_desc *lvid; 1471 uint64_t inf_len; 1472 uint64_t meta_free_lbs, data_free_lbs, to_trunc; 1473 uint32_t *freepos, *sizepos; 1474 uint32_t unit, lb_size; 1475 uint16_t meta_vpart_num, data_vpart_num, num_vpart; 1476 int err __diagused; 1477 1478 unit = ump->metadata_alloc_unit_size; 1479 lb_size = udf_rw32(ump->logical_vol->lb_size); 1480 lvid = ump->logvol_integrity; 1481 1482 /* XXX 1483 * 1484 * the following checks will fail for BD-R UDF 2.60! but they are 1485 * read-only for now anyway! Its even doubtfull if it is to be allowed 1486 * for these discs. 1487 */ 1488 1489 /* lookup vpart for metadata partition */ 1490 meta_vpart_num = ump->node_part; 1491 KASSERT(ump->vtop_alloc[meta_vpart_num] == UDF_ALLOC_METABITMAP); 1492 1493 /* lookup vpart for data partition */ 1494 data_vpart_num = ump->data_part; 1495 KASSERT(ump->vtop_alloc[data_vpart_num] == UDF_ALLOC_SPACEMAP); 1496 1497 udf_calc_vpart_freespace(ump, data_vpart_num, &data_free_lbs); 1498 udf_calc_vpart_freespace(ump, meta_vpart_num, &meta_free_lbs); 1499 1500 DPRINTF(RESERVE, ("\tfree space on data partition %"PRIu64" blks\n", data_free_lbs)); 1501 DPRINTF(RESERVE, ("\tfree space on metadata partition %"PRIu64" blks\n", meta_free_lbs)); 1502 1503 /* give away some of the free meta space, in unit block sizes */ 1504 to_trunc = meta_free_lbs/4; /* give out a quarter */ 1505 to_trunc = MAX(to_trunc, num_lb); 1506 to_trunc = unit * ((to_trunc + unit-1) / unit); /* round up */ 1507 1508 /* scale down if needed and bail out when out of space */ 1509 if (to_trunc >= meta_free_lbs) 1510 return num_lb; 1511 1512 /* check extent of bits marked free at the end of the map */ 1513 bitmap = &ump->metadata_unalloc_bits; 1514 to_trunc = udf_bitmap_check_trunc_free(bitmap, to_trunc); 1515 to_trunc = unit * (to_trunc / unit); /* round down again */ 1516 if (to_trunc == 0) 1517 return num_lb; 1518 1519 DPRINTF(RESERVE, ("\ttruncating %"PRIu64" lbs from the metadata bitmap\n", 1520 to_trunc)); 1521 1522 /* get length of the metadata bitmap node file */ 1523 bitmap_node = ump->metadatabitmap_node; 1524 if (bitmap_node->fe) { 1525 inf_len = udf_rw64(bitmap_node->fe->inf_len); 1526 } else { 1527 KASSERT(bitmap_node->efe); 1528 inf_len = udf_rw64(bitmap_node->efe->inf_len); 1529 } 1530 inf_len -= to_trunc/8; 1531 1532 /* as per [UDF 2.60/2.2.13.6] : */ 1533 /* 1) update the SBD in the metadata bitmap file */ 1534 sbd = (struct space_bitmap_desc *) bitmap->blob; 1535 sbd->num_bits = udf_rw32(udf_rw32(sbd->num_bits) - to_trunc); 1536 sbd->num_bytes = udf_rw32(udf_rw32(sbd->num_bytes) - to_trunc/8); 1537 bitmap->max_offset = udf_rw32(sbd->num_bits); 1538 1539 num_vpart = udf_rw32(lvid->num_part); 1540 freepos = &lvid->tables[0] + meta_vpart_num; 1541 sizepos = &lvid->tables[0] + num_vpart + meta_vpart_num; 1542 *freepos = udf_rw32(*freepos) - to_trunc; 1543 *sizepos = udf_rw32(*sizepos) - to_trunc; 1544 1545 /* realloc bitmap for better memory usage */ 1546 new_sbd = realloc(sbd, inf_len, M_UDFVOLD, 1547 M_CANFAIL | M_WAITOK); 1548 if (new_sbd) { 1549 /* update pointers */ 1550 ump->metadata_unalloc_dscr = new_sbd; 1551 bitmap->blob = (uint8_t *) new_sbd; 1552 } 1553 ump->lvclose |= UDF_WRITE_PART_BITMAPS; 1554 1555 /* 1556 * The truncated space is secured now and can't be allocated anymore. 1557 * Release the allocate mutex so we can shrink the nodes the normal 1558 * way. 1559 */ 1560 mutex_exit(&ump->allocate_mutex); 1561 1562 /* 2) trunc the metadata bitmap information file, freeing blocks */ 1563 err = udf_shrink_node(bitmap_node, inf_len); 1564 KASSERT(err == 0); 1565 1566 /* 3) trunc the metadata file and mirror file, freeing blocks */ 1567 inf_len = (uint64_t) udf_rw32(sbd->num_bits) * lb_size; /* [4/14.12.4] */ 1568 err = udf_shrink_node(ump->metadata_node, inf_len); 1569 KASSERT(err == 0); 1570 if (ump->metadatamirror_node) { 1571 if (ump->metadata_flags & METADATA_DUPLICATED) { 1572 err = udf_shrink_node(ump->metadatamirror_node, inf_len); 1573 } else { 1574 /* extents will be copied on writeout */ 1575 } 1576 KASSERT(err == 0); 1577 } 1578 ump->lvclose |= UDF_WRITE_METAPART_NODES; 1579 1580 /* relock before exit */ 1581 mutex_enter(&ump->allocate_mutex); 1582 1583 if (to_trunc > num_lb) 1584 return 0; 1585 return num_lb - to_trunc; 1586 } 1587 1588 1589 static void 1590 udf_sparsify_metadatapart(struct udf_mount *ump, uint32_t num_lb) 1591 { 1592 /* NOT IMPLEMENTED, fail */ 1593 } 1594 1595 1596 static void 1597 udf_collect_free_space_for_vpart(struct udf_mount *ump, 1598 uint16_t vpart_num, uint32_t num_lb) 1599 { 1600 /* allocate mutex is helt */ 1601 1602 /* only defined for metadata partitions */ 1603 if (ump->vtop_tp[ump->node_part] != UDF_VTOP_TYPE_META) { 1604 DPRINTF(RESERVE, ("\tcan't grow/shrink; no metadata partitioning\n")); 1605 return; 1606 } 1607 1608 /* UDF 2.60 BD-R+POW? */ 1609 if (ump->vtop_alloc[ump->node_part] == UDF_ALLOC_METASEQUENTIAL) { 1610 DPRINTF(RESERVE, ("\tUDF 2.60 BD-R+POW track grow not implemented yet\n")); 1611 return; 1612 } 1613 1614 if (ump->vtop_tp[vpart_num] == UDF_VTOP_TYPE_META) { 1615 /* try to grow the meta partition */ 1616 DPRINTF(RESERVE, ("\ttrying to grow the meta partition\n")); 1617 /* as per [UDF 2.60/2.2.13.5] : extend bitmap and metadata file(s) */ 1618 DPRINTF(NOTIMPL, ("\tgrowing meta partition not implemented yet\n")); 1619 } else { 1620 /* try to shrink the metadata partition */ 1621 DPRINTF(RESERVE, ("\ttrying to shrink the meta partition\n")); 1622 /* as per [UDF 2.60/2.2.13.6] : either trunc or make sparse */ 1623 num_lb = udf_trunc_metadatapart(ump, num_lb); 1624 if (num_lb) 1625 udf_sparsify_metadatapart(ump, num_lb); 1626 } 1627 1628 /* allocate mutex should still be helt */ 1629 } 1630 1631 /* --------------------------------------------------------------------- */ 1632 1633 /* 1634 * Allocate a buf on disc for direct write out. The space doesn't have to be 1635 * contiguous as the caller takes care of this. 1636 */ 1637 1638 void 1639 udf_late_allocate_buf(struct udf_mount *ump, struct buf *buf, 1640 uint64_t *lmapping, struct long_ad *node_ad_cpy, uint16_t *vpart_nump) 1641 { 1642 struct udf_node *udf_node = VTOI(buf->b_vp); 1643 int lb_size, udf_c_type; 1644 int vpart_num, num_lb; 1645 int error, s; 1646 1647 /* 1648 * for each sector in the buf, allocate a sector on disc and record 1649 * its position in the provided mapping array. 1650 * 1651 * If its userdata or FIDs, record its location in its node. 1652 */ 1653 1654 lb_size = udf_rw32(ump->logical_vol->lb_size); 1655 num_lb = (buf->b_bcount + lb_size -1) / lb_size; 1656 udf_c_type = buf->b_udf_c_type; 1657 1658 KASSERT(lb_size == ump->discinfo.sector_size); 1659 1660 /* select partition to record the buffer on */ 1661 vpart_num = *vpart_nump = udf_get_record_vpart(ump, udf_c_type); 1662 1663 if (udf_c_type == UDF_C_NODE) { 1664 /* if not VAT, its allready allocated */ 1665 if (ump->vtop_alloc[ump->node_part] != UDF_ALLOC_VAT) 1666 return; 1667 1668 /* allocate on its backing sequential partition */ 1669 vpart_num = ump->data_part; 1670 } 1671 1672 /* XXX can this still happen? */ 1673 /* do allocation on the selected partition */ 1674 error = udf_allocate_space(ump, udf_node, udf_c_type, 1675 vpart_num, num_lb, lmapping); 1676 if (error) { 1677 /* 1678 * ARGH! we haven't done our accounting right! it should 1679 * allways succeed. 1680 */ 1681 panic("UDF disc allocation accounting gone wrong"); 1682 } 1683 1684 /* If its userdata or FIDs, record its allocation in its node. */ 1685 if ((udf_c_type == UDF_C_USERDATA) || 1686 (udf_c_type == UDF_C_FIDS) || 1687 (udf_c_type == UDF_C_METADATA_SBM)) 1688 { 1689 udf_record_allocation_in_node(ump, buf, vpart_num, lmapping, 1690 node_ad_cpy); 1691 /* decrement our outstanding bufs counter */ 1692 s = splbio(); 1693 udf_node->outstanding_bufs--; 1694 splx(s); 1695 } 1696 } 1697 1698 /* --------------------------------------------------------------------- */ 1699 1700 /* 1701 * Try to merge a1 with the new piece a2. udf_ads_merge returns error when not 1702 * possible (anymore); a2 returns the rest piece. 1703 */ 1704 1705 static int 1706 udf_ads_merge(uint32_t max_len, uint32_t lb_size, struct long_ad *a1, struct long_ad *a2) 1707 { 1708 uint32_t merge_len; 1709 uint32_t a1_len, a2_len; 1710 uint32_t a1_flags, a2_flags; 1711 uint32_t a1_lbnum, a2_lbnum; 1712 uint16_t a1_part, a2_part; 1713 1714 a1_flags = UDF_EXT_FLAGS(udf_rw32(a1->len)); 1715 a1_len = UDF_EXT_LEN(udf_rw32(a1->len)); 1716 a1_lbnum = udf_rw32(a1->loc.lb_num); 1717 a1_part = udf_rw16(a1->loc.part_num); 1718 1719 a2_flags = UDF_EXT_FLAGS(udf_rw32(a2->len)); 1720 a2_len = UDF_EXT_LEN(udf_rw32(a2->len)); 1721 a2_lbnum = udf_rw32(a2->loc.lb_num); 1722 a2_part = udf_rw16(a2->loc.part_num); 1723 1724 /* defines same space */ 1725 if (a1_flags != a2_flags) 1726 return 1; 1727 1728 if (a1_flags != UDF_EXT_FREE) { 1729 /* the same partition */ 1730 if (a1_part != a2_part) 1731 return 1; 1732 1733 /* a2 is successor of a1 */ 1734 if (a1_lbnum * lb_size + a1_len != a2_lbnum * lb_size) 1735 return 1; 1736 } 1737 1738 /* merge as most from a2 if possible */ 1739 merge_len = MIN(a2_len, max_len - a1_len); 1740 a1_len += merge_len; 1741 a2_len -= merge_len; 1742 a2_lbnum += merge_len/lb_size; 1743 1744 a1->len = udf_rw32(a1_len | a1_flags); 1745 a2->len = udf_rw32(a2_len | a2_flags); 1746 a2->loc.lb_num = udf_rw32(a2_lbnum); 1747 1748 if (a2_len > 0) 1749 return 1; 1750 1751 /* there is space over to merge */ 1752 return 0; 1753 } 1754 1755 /* --------------------------------------------------------------------- */ 1756 1757 static void 1758 udf_wipe_adslots(struct udf_node *udf_node) 1759 { 1760 struct file_entry *fe; 1761 struct extfile_entry *efe; 1762 struct alloc_ext_entry *ext; 1763 uint32_t lb_size, dscr_size, l_ea, max_l_ad, crclen; 1764 uint8_t *data_pos; 1765 int extnr; 1766 1767 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 1768 1769 fe = udf_node->fe; 1770 efe = udf_node->efe; 1771 if (fe) { 1772 dscr_size = sizeof(struct file_entry) -1; 1773 l_ea = udf_rw32(fe->l_ea); 1774 data_pos = (uint8_t *) fe + dscr_size + l_ea; 1775 } else { 1776 dscr_size = sizeof(struct extfile_entry) -1; 1777 l_ea = udf_rw32(efe->l_ea); 1778 data_pos = (uint8_t *) efe + dscr_size + l_ea; 1779 } 1780 max_l_ad = lb_size - dscr_size - l_ea; 1781 1782 /* wipe fe/efe */ 1783 memset(data_pos, 0, max_l_ad); 1784 crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea; 1785 if (fe) { 1786 fe->l_ad = udf_rw32(0); 1787 fe->logblks_rec = udf_rw64(0); 1788 fe->tag.desc_crc_len = udf_rw16(crclen); 1789 } else { 1790 efe->l_ad = udf_rw32(0); 1791 efe->logblks_rec = udf_rw64(0); 1792 efe->tag.desc_crc_len = udf_rw16(crclen); 1793 } 1794 1795 /* wipe all allocation extent entries */ 1796 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) { 1797 ext = udf_node->ext[extnr]; 1798 dscr_size = sizeof(struct alloc_ext_entry) -1; 1799 data_pos = (uint8_t *) ext->data; 1800 max_l_ad = lb_size - dscr_size; 1801 memset(data_pos, 0, max_l_ad); 1802 ext->l_ad = udf_rw32(0); 1803 1804 crclen = dscr_size - UDF_DESC_TAG_LENGTH; 1805 ext->tag.desc_crc_len = udf_rw16(crclen); 1806 } 1807 udf_node->i_flags |= IN_NODE_REBUILD; 1808 } 1809 1810 /* --------------------------------------------------------------------- */ 1811 1812 void 1813 udf_get_adslot(struct udf_node *udf_node, int slot, struct long_ad *icb, 1814 int *eof) { 1815 struct file_entry *fe; 1816 struct extfile_entry *efe; 1817 struct alloc_ext_entry *ext; 1818 struct icb_tag *icbtag; 1819 struct short_ad *short_ad; 1820 struct long_ad *long_ad, l_icb; 1821 uint32_t offset; 1822 uint32_t dscr_size, l_ea, l_ad, flags; 1823 uint8_t *data_pos; 1824 int icbflags, addr_type, adlen, extnr; 1825 1826 fe = udf_node->fe; 1827 efe = udf_node->efe; 1828 if (fe) { 1829 icbtag = &fe->icbtag; 1830 dscr_size = sizeof(struct file_entry) -1; 1831 l_ea = udf_rw32(fe->l_ea); 1832 l_ad = udf_rw32(fe->l_ad); 1833 data_pos = (uint8_t *) fe + dscr_size + l_ea; 1834 } else { 1835 icbtag = &efe->icbtag; 1836 dscr_size = sizeof(struct extfile_entry) -1; 1837 l_ea = udf_rw32(efe->l_ea); 1838 l_ad = udf_rw32(efe->l_ad); 1839 data_pos = (uint8_t *) efe + dscr_size + l_ea; 1840 } 1841 1842 icbflags = udf_rw16(icbtag->flags); 1843 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 1844 1845 /* just in case we're called on an intern, its EOF */ 1846 if (addr_type == UDF_ICB_INTERN_ALLOC) { 1847 memset(icb, 0, sizeof(struct long_ad)); 1848 *eof = 1; 1849 return; 1850 } 1851 1852 adlen = 0; 1853 if (addr_type == UDF_ICB_SHORT_ALLOC) { 1854 adlen = sizeof(struct short_ad); 1855 } else if (addr_type == UDF_ICB_LONG_ALLOC) { 1856 adlen = sizeof(struct long_ad); 1857 } 1858 1859 /* if offset too big, we go to the allocation extensions */ 1860 offset = slot * adlen; 1861 extnr = -1; 1862 while (offset >= l_ad) { 1863 /* check if our last entry is a redirect */ 1864 if (addr_type == UDF_ICB_SHORT_ALLOC) { 1865 short_ad = (struct short_ad *) (data_pos + l_ad-adlen); 1866 l_icb.len = short_ad->len; 1867 l_icb.loc.part_num = udf_node->loc.loc.part_num; 1868 l_icb.loc.lb_num = short_ad->lb_num; 1869 } else { 1870 KASSERT(addr_type == UDF_ICB_LONG_ALLOC); 1871 long_ad = (struct long_ad *) (data_pos + l_ad-adlen); 1872 l_icb = *long_ad; 1873 } 1874 flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len)); 1875 if (flags != UDF_EXT_REDIRECT) { 1876 l_ad = 0; /* force EOF */ 1877 break; 1878 } 1879 1880 /* advance to next extent */ 1881 extnr++; 1882 if (extnr >= udf_node->num_extensions) { 1883 l_ad = 0; /* force EOF */ 1884 break; 1885 } 1886 offset = offset - l_ad; 1887 ext = udf_node->ext[extnr]; 1888 dscr_size = sizeof(struct alloc_ext_entry) -1; 1889 l_ad = udf_rw32(ext->l_ad); 1890 data_pos = (uint8_t *) ext + dscr_size; 1891 } 1892 1893 /* XXX l_ad == 0 should be enough to check */ 1894 *eof = (offset >= l_ad) || (l_ad == 0); 1895 if (*eof) { 1896 DPRINTF(PARANOIDADWLK, ("returning EOF, extnr %d, offset %d, " 1897 "l_ad %d\n", extnr, offset, l_ad)); 1898 memset(icb, 0, sizeof(struct long_ad)); 1899 return; 1900 } 1901 1902 /* get the element */ 1903 if (addr_type == UDF_ICB_SHORT_ALLOC) { 1904 short_ad = (struct short_ad *) (data_pos + offset); 1905 icb->len = short_ad->len; 1906 icb->loc.part_num = udf_node->loc.loc.part_num; 1907 icb->loc.lb_num = short_ad->lb_num; 1908 } else if (addr_type == UDF_ICB_LONG_ALLOC) { 1909 long_ad = (struct long_ad *) (data_pos + offset); 1910 *icb = *long_ad; 1911 } 1912 DPRINTF(PARANOIDADWLK, ("returning element : v %d, lb %d, len %d, " 1913 "flags %d\n", icb->loc.part_num, icb->loc.lb_num, 1914 UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len))); 1915 } 1916 1917 /* --------------------------------------------------------------------- */ 1918 1919 int 1920 udf_append_adslot(struct udf_node *udf_node, int *slot, struct long_ad *icb) { 1921 struct udf_mount *ump = udf_node->ump; 1922 union dscrptr *dscr, *extdscr; 1923 struct file_entry *fe; 1924 struct extfile_entry *efe; 1925 struct alloc_ext_entry *ext; 1926 struct icb_tag *icbtag; 1927 struct short_ad *short_ad; 1928 struct long_ad *long_ad, o_icb, l_icb; 1929 uint64_t logblks_rec, *logblks_rec_p; 1930 uint64_t lmapping; 1931 uint32_t offset, rest, len, lb_num; 1932 uint32_t lb_size, dscr_size, l_ea, l_ad, *l_ad_p, max_l_ad, crclen; 1933 uint32_t flags; 1934 uint16_t vpart_num; 1935 uint8_t *data_pos; 1936 int icbflags, addr_type, adlen, extnr; 1937 int error; 1938 1939 lb_size = udf_rw32(ump->logical_vol->lb_size); 1940 vpart_num = udf_rw16(udf_node->loc.loc.part_num); 1941 1942 /* determine what descriptor we are in */ 1943 fe = udf_node->fe; 1944 efe = udf_node->efe; 1945 if (fe) { 1946 icbtag = &fe->icbtag; 1947 dscr = (union dscrptr *) fe; 1948 dscr_size = sizeof(struct file_entry) -1; 1949 1950 l_ea = udf_rw32(fe->l_ea); 1951 l_ad_p = &fe->l_ad; 1952 logblks_rec_p = &fe->logblks_rec; 1953 } else { 1954 icbtag = &efe->icbtag; 1955 dscr = (union dscrptr *) efe; 1956 dscr_size = sizeof(struct extfile_entry) -1; 1957 1958 l_ea = udf_rw32(efe->l_ea); 1959 l_ad_p = &efe->l_ad; 1960 logblks_rec_p = &efe->logblks_rec; 1961 } 1962 data_pos = (uint8_t *) dscr + dscr_size + l_ea; 1963 max_l_ad = lb_size - dscr_size - l_ea; 1964 1965 icbflags = udf_rw16(icbtag->flags); 1966 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 1967 1968 /* just in case we're called on an intern, its EOF */ 1969 if (addr_type == UDF_ICB_INTERN_ALLOC) { 1970 panic("udf_append_adslot on UDF_ICB_INTERN_ALLOC\n"); 1971 } 1972 1973 adlen = 0; 1974 if (addr_type == UDF_ICB_SHORT_ALLOC) { 1975 adlen = sizeof(struct short_ad); 1976 } else if (addr_type == UDF_ICB_LONG_ALLOC) { 1977 adlen = sizeof(struct long_ad); 1978 } 1979 1980 /* clean up given long_ad since it can be a synthesized one */ 1981 flags = UDF_EXT_FLAGS(udf_rw32(icb->len)); 1982 if (flags == UDF_EXT_FREE) { 1983 icb->loc.part_num = udf_rw16(0); 1984 icb->loc.lb_num = udf_rw32(0); 1985 } 1986 1987 /* if offset too big, we go to the allocation extensions */ 1988 l_ad = udf_rw32(*l_ad_p); 1989 offset = (*slot) * adlen; 1990 extnr = -1; 1991 while (offset >= l_ad) { 1992 /* check if our last entry is a redirect */ 1993 if (addr_type == UDF_ICB_SHORT_ALLOC) { 1994 short_ad = (struct short_ad *) (data_pos + l_ad-adlen); 1995 l_icb.len = short_ad->len; 1996 l_icb.loc.part_num = udf_node->loc.loc.part_num; 1997 l_icb.loc.lb_num = short_ad->lb_num; 1998 } else { 1999 KASSERT(addr_type == UDF_ICB_LONG_ALLOC); 2000 long_ad = (struct long_ad *) (data_pos + l_ad-adlen); 2001 l_icb = *long_ad; 2002 } 2003 flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len)); 2004 if (flags != UDF_EXT_REDIRECT) { 2005 /* only one past the last one is adressable */ 2006 break; 2007 } 2008 2009 /* advance to next extent */ 2010 extnr++; 2011 KASSERT(extnr < udf_node->num_extensions); 2012 offset = offset - l_ad; 2013 2014 ext = udf_node->ext[extnr]; 2015 dscr = (union dscrptr *) ext; 2016 dscr_size = sizeof(struct alloc_ext_entry) -1; 2017 max_l_ad = lb_size - dscr_size; 2018 l_ad_p = &ext->l_ad; 2019 l_ad = udf_rw32(*l_ad_p); 2020 data_pos = (uint8_t *) ext + dscr_size; 2021 } 2022 DPRINTF(PARANOIDADWLK, ("append, ext %d, offset %d, l_ad %d\n", 2023 extnr, offset, udf_rw32(*l_ad_p))); 2024 KASSERT(l_ad == udf_rw32(*l_ad_p)); 2025 2026 /* offset is offset within the current (E)FE/AED */ 2027 l_ad = udf_rw32(*l_ad_p); 2028 crclen = udf_rw16(dscr->tag.desc_crc_len); 2029 logblks_rec = udf_rw64(*logblks_rec_p); 2030 2031 /* overwriting old piece? */ 2032 if (offset < l_ad) { 2033 /* overwrite entry; compensate for the old element */ 2034 if (addr_type == UDF_ICB_SHORT_ALLOC) { 2035 short_ad = (struct short_ad *) (data_pos + offset); 2036 o_icb.len = short_ad->len; 2037 o_icb.loc.part_num = udf_rw16(0); /* ignore */ 2038 o_icb.loc.lb_num = short_ad->lb_num; 2039 } else if (addr_type == UDF_ICB_LONG_ALLOC) { 2040 long_ad = (struct long_ad *) (data_pos + offset); 2041 o_icb = *long_ad; 2042 } else { 2043 panic("Invalid address type in udf_append_adslot\n"); 2044 } 2045 2046 len = udf_rw32(o_icb.len); 2047 if (UDF_EXT_FLAGS(len) == UDF_EXT_ALLOCATED) { 2048 /* adjust counts */ 2049 len = UDF_EXT_LEN(len); 2050 logblks_rec -= (len + lb_size -1) / lb_size; 2051 } 2052 } 2053 2054 /* check if we're not appending a redirection */ 2055 flags = UDF_EXT_FLAGS(udf_rw32(icb->len)); 2056 KASSERT(flags != UDF_EXT_REDIRECT); 2057 2058 /* round down available space */ 2059 rest = adlen * ((max_l_ad - offset) / adlen); 2060 if (rest <= adlen) { 2061 /* have to append aed, see if we already have a spare one */ 2062 extnr++; 2063 ext = udf_node->ext[extnr]; 2064 l_icb = udf_node->ext_loc[extnr]; 2065 if (ext == NULL) { 2066 DPRINTF(ALLOC,("adding allocation extent %d\n", extnr)); 2067 2068 error = udf_reserve_space(ump, NULL, UDF_C_NODE, 2069 vpart_num, 1, /* can fail */ false); 2070 if (error) { 2071 printf("UDF: couldn't reserve space for AED!\n"); 2072 return error; 2073 } 2074 error = udf_allocate_space(ump, NULL, UDF_C_NODE, 2075 vpart_num, 1, &lmapping); 2076 lb_num = lmapping; 2077 if (error) 2078 panic("UDF: couldn't allocate AED!\n"); 2079 2080 /* initialise pointer to location */ 2081 memset(&l_icb, 0, sizeof(struct long_ad)); 2082 l_icb.len = udf_rw32(lb_size | UDF_EXT_REDIRECT); 2083 l_icb.loc.lb_num = udf_rw32(lb_num); 2084 l_icb.loc.part_num = udf_rw16(vpart_num); 2085 2086 /* create new aed descriptor */ 2087 udf_create_logvol_dscr(ump, udf_node, &l_icb, &extdscr); 2088 ext = &extdscr->aee; 2089 2090 udf_inittag(ump, &ext->tag, TAGID_ALLOCEXTENT, lb_num); 2091 dscr_size = sizeof(struct alloc_ext_entry) -1; 2092 max_l_ad = lb_size - dscr_size; 2093 memset(ext->data, 0, max_l_ad); 2094 ext->l_ad = udf_rw32(0); 2095 ext->tag.desc_crc_len = 2096 udf_rw16(dscr_size - UDF_DESC_TAG_LENGTH); 2097 2098 /* declare aed */ 2099 udf_node->num_extensions++; 2100 udf_node->ext_loc[extnr] = l_icb; 2101 udf_node->ext[extnr] = ext; 2102 } 2103 /* add redirect and adjust l_ad and crclen for old descr */ 2104 if (addr_type == UDF_ICB_SHORT_ALLOC) { 2105 short_ad = (struct short_ad *) (data_pos + offset); 2106 short_ad->len = l_icb.len; 2107 short_ad->lb_num = l_icb.loc.lb_num; 2108 } else if (addr_type == UDF_ICB_LONG_ALLOC) { 2109 long_ad = (struct long_ad *) (data_pos + offset); 2110 *long_ad = l_icb; 2111 } 2112 l_ad += adlen; 2113 crclen += adlen; 2114 dscr->tag.desc_crc_len = udf_rw16(crclen); 2115 *l_ad_p = udf_rw32(l_ad); 2116 2117 /* advance to the new extension */ 2118 KASSERT(ext != NULL); 2119 dscr = (union dscrptr *) ext; 2120 dscr_size = sizeof(struct alloc_ext_entry) -1; 2121 max_l_ad = lb_size - dscr_size; 2122 data_pos = (uint8_t *) dscr + dscr_size; 2123 2124 l_ad_p = &ext->l_ad; 2125 l_ad = udf_rw32(*l_ad_p); 2126 crclen = udf_rw16(dscr->tag.desc_crc_len); 2127 offset = 0; 2128 2129 /* adjust callees slot count for link insert */ 2130 *slot += 1; 2131 } 2132 2133 /* write out the element */ 2134 DPRINTF(PARANOIDADWLK, ("adding element : %p : v %d, lb %d, " 2135 "len %d, flags %d\n", data_pos + offset, 2136 icb->loc.part_num, icb->loc.lb_num, 2137 UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len))); 2138 if (addr_type == UDF_ICB_SHORT_ALLOC) { 2139 short_ad = (struct short_ad *) (data_pos + offset); 2140 short_ad->len = icb->len; 2141 short_ad->lb_num = icb->loc.lb_num; 2142 } else if (addr_type == UDF_ICB_LONG_ALLOC) { 2143 long_ad = (struct long_ad *) (data_pos + offset); 2144 *long_ad = *icb; 2145 } 2146 2147 /* adjust logblks recorded count */ 2148 len = udf_rw32(icb->len); 2149 flags = UDF_EXT_FLAGS(len); 2150 if (flags == UDF_EXT_ALLOCATED) 2151 logblks_rec += (UDF_EXT_LEN(len) + lb_size -1) / lb_size; 2152 *logblks_rec_p = udf_rw64(logblks_rec); 2153 2154 /* adjust l_ad and crclen when needed */ 2155 if (offset >= l_ad) { 2156 l_ad += adlen; 2157 crclen += adlen; 2158 dscr->tag.desc_crc_len = udf_rw16(crclen); 2159 *l_ad_p = udf_rw32(l_ad); 2160 } 2161 2162 return 0; 2163 } 2164 2165 /* --------------------------------------------------------------------- */ 2166 2167 static void 2168 udf_count_alloc_exts(struct udf_node *udf_node) 2169 { 2170 struct long_ad s_ad; 2171 uint32_t lb_num, len, flags; 2172 uint16_t vpart_num; 2173 int slot, eof; 2174 int num_extents, extnr; 2175 2176 if (udf_node->num_extensions == 0) 2177 return; 2178 2179 /* count number of allocation extents in use */ 2180 num_extents = 0; 2181 slot = 0; 2182 for (;;) { 2183 udf_get_adslot(udf_node, slot, &s_ad, &eof); 2184 if (eof) 2185 break; 2186 len = udf_rw32(s_ad.len); 2187 flags = UDF_EXT_FLAGS(len); 2188 2189 if (flags == UDF_EXT_REDIRECT) 2190 num_extents++; 2191 2192 slot++; 2193 } 2194 2195 DPRINTF(ALLOC, ("udf_count_alloc_ext counted %d live extents\n", 2196 num_extents)); 2197 2198 /* XXX choice: we could delay freeing them on node writeout */ 2199 /* free excess entries */ 2200 extnr = num_extents; 2201 for (;extnr < udf_node->num_extensions; extnr++) { 2202 DPRINTF(ALLOC, ("freeing alloc ext %d\n", extnr)); 2203 /* free dscriptor */ 2204 s_ad = udf_node->ext_loc[extnr]; 2205 udf_free_logvol_dscr(udf_node->ump, &s_ad, 2206 udf_node->ext[extnr]); 2207 udf_node->ext[extnr] = NULL; 2208 2209 /* free disc space */ 2210 lb_num = udf_rw32(s_ad.loc.lb_num); 2211 vpart_num = udf_rw16(s_ad.loc.part_num); 2212 udf_free_allocated_space(udf_node->ump, lb_num, vpart_num, 1); 2213 2214 memset(&udf_node->ext_loc[extnr], 0, sizeof(struct long_ad)); 2215 } 2216 2217 /* set our new number of allocation extents */ 2218 udf_node->num_extensions = num_extents; 2219 } 2220 2221 2222 /* --------------------------------------------------------------------- */ 2223 2224 /* 2225 * Adjust the node's allocation descriptors to reflect the new mapping; do 2226 * take note that we might glue to existing allocation descriptors. 2227 * 2228 * XXX Note there can only be one allocation being recorded/mount; maybe 2229 * explicit allocation in shedule thread? 2230 */ 2231 2232 static void 2233 udf_record_allocation_in_node(struct udf_mount *ump, struct buf *buf, 2234 uint16_t vpart_num, uint64_t *mapping, struct long_ad *node_ad_cpy) 2235 { 2236 struct vnode *vp = buf->b_vp; 2237 struct udf_node *udf_node = VTOI(vp); 2238 struct file_entry *fe; 2239 struct extfile_entry *efe; 2240 struct icb_tag *icbtag; 2241 struct long_ad s_ad, c_ad; 2242 uint64_t inflen, from, till; 2243 uint64_t foffset, end_foffset, restart_foffset; 2244 uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec; 2245 uint32_t max_len; 2246 uint32_t num_lb, len, flags, lb_num; 2247 uint32_t run_start; 2248 uint32_t slot_offset, replace_len, replace; 2249 int addr_type, icbflags; 2250 // int udf_c_type = buf->b_udf_c_type; 2251 int lb_size, run_length, eof; 2252 int slot, cpy_slot, cpy_slots, restart_slot; 2253 int error; 2254 2255 DPRINTF(ALLOC, ("udf_record_allocation_in_node\n")); 2256 2257 #if 0 2258 /* XXX disable sanity check for now */ 2259 /* sanity check ... should be panic ? */ 2260 if ((udf_c_type != UDF_C_USERDATA) && (udf_c_type != UDF_C_FIDS)) 2261 return; 2262 #endif 2263 2264 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 2265 max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size); 2266 2267 /* do the job */ 2268 UDF_LOCK_NODE(udf_node, 0); /* XXX can deadlock ? */ 2269 udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec); 2270 2271 fe = udf_node->fe; 2272 efe = udf_node->efe; 2273 if (fe) { 2274 icbtag = &fe->icbtag; 2275 inflen = udf_rw64(fe->inf_len); 2276 } else { 2277 icbtag = &efe->icbtag; 2278 inflen = udf_rw64(efe->inf_len); 2279 } 2280 2281 /* do check if `till' is not past file information length */ 2282 from = buf->b_lblkno * lb_size; 2283 till = MIN(inflen, from + buf->b_resid); 2284 2285 num_lb = (till - from + lb_size -1) / lb_size; 2286 2287 DPRINTF(ALLOC, ("record allocation from %"PRIu64" + %d\n", from, buf->b_bcount)); 2288 2289 icbflags = udf_rw16(icbtag->flags); 2290 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 2291 2292 if (addr_type == UDF_ICB_INTERN_ALLOC) { 2293 /* nothing to do */ 2294 /* XXX clean up rest of node? just in case? */ 2295 UDF_UNLOCK_NODE(udf_node, 0); 2296 return; 2297 } 2298 2299 slot = 0; 2300 cpy_slot = 0; 2301 foffset = 0; 2302 2303 /* 1) copy till first overlap piece to the rewrite buffer */ 2304 for (;;) { 2305 udf_get_adslot(udf_node, slot, &s_ad, &eof); 2306 if (eof) { 2307 DPRINTF(WRITE, 2308 ("Record allocation in node " 2309 "failed: encountered EOF\n")); 2310 UDF_UNLOCK_NODE(udf_node, 0); 2311 buf->b_error = EINVAL; 2312 return; 2313 } 2314 len = udf_rw32(s_ad.len); 2315 flags = UDF_EXT_FLAGS(len); 2316 len = UDF_EXT_LEN(len); 2317 2318 if (flags == UDF_EXT_REDIRECT) { 2319 slot++; 2320 continue; 2321 } 2322 2323 end_foffset = foffset + len; 2324 if (end_foffset > from) 2325 break; /* found */ 2326 2327 node_ad_cpy[cpy_slot++] = s_ad; 2328 2329 DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d " 2330 "-> stack\n", 2331 udf_rw16(s_ad.loc.part_num), 2332 udf_rw32(s_ad.loc.lb_num), 2333 UDF_EXT_LEN(udf_rw32(s_ad.len)), 2334 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 2335 2336 foffset = end_foffset; 2337 slot++; 2338 } 2339 restart_slot = slot; 2340 restart_foffset = foffset; 2341 2342 /* 2) trunc overlapping slot at overlap and copy it */ 2343 slot_offset = from - foffset; 2344 if (slot_offset > 0) { 2345 DPRINTF(ALLOC, ("\tslot_offset = %d, flags = %d (%d)\n", 2346 slot_offset, flags >> 30, flags)); 2347 2348 s_ad.len = udf_rw32(slot_offset | flags); 2349 node_ad_cpy[cpy_slot++] = s_ad; 2350 2351 DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d " 2352 "-> stack\n", 2353 udf_rw16(s_ad.loc.part_num), 2354 udf_rw32(s_ad.loc.lb_num), 2355 UDF_EXT_LEN(udf_rw32(s_ad.len)), 2356 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 2357 } 2358 foffset += slot_offset; 2359 2360 /* 3) insert new mappings */ 2361 memset(&s_ad, 0, sizeof(struct long_ad)); 2362 lb_num = 0; 2363 for (lb_num = 0; lb_num < num_lb; lb_num++) { 2364 run_start = mapping[lb_num]; 2365 run_length = 1; 2366 while (lb_num < num_lb-1) { 2367 if (mapping[lb_num+1] != mapping[lb_num]+1) 2368 if (mapping[lb_num+1] != mapping[lb_num]) 2369 break; 2370 run_length++; 2371 lb_num++; 2372 } 2373 /* insert slot for this mapping */ 2374 len = run_length * lb_size; 2375 2376 /* bounds checking */ 2377 if (foffset + len > till) 2378 len = till - foffset; 2379 KASSERT(foffset + len <= inflen); 2380 2381 s_ad.len = udf_rw32(len | UDF_EXT_ALLOCATED); 2382 s_ad.loc.part_num = udf_rw16(vpart_num); 2383 s_ad.loc.lb_num = udf_rw32(run_start); 2384 2385 foffset += len; 2386 2387 /* paranoia */ 2388 if (len == 0) { 2389 DPRINTF(WRITE, 2390 ("Record allocation in node " 2391 "failed: insert failed\n")); 2392 UDF_UNLOCK_NODE(udf_node, 0); 2393 buf->b_error = EINVAL; 2394 return; 2395 } 2396 node_ad_cpy[cpy_slot++] = s_ad; 2397 2398 DPRINTF(ALLOC, ("\t3: insert new mapping vp %d lb %d, len %d, " 2399 "flags %d -> stack\n", 2400 udf_rw16(s_ad.loc.part_num), udf_rw32(s_ad.loc.lb_num), 2401 UDF_EXT_LEN(udf_rw32(s_ad.len)), 2402 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 2403 } 2404 2405 /* 4) pop replaced length */ 2406 slot = restart_slot; 2407 foffset = restart_foffset; 2408 2409 replace_len = till - foffset; /* total amount of bytes to pop */ 2410 slot_offset = from - foffset; /* offset in first encounted slot */ 2411 KASSERT((slot_offset % lb_size) == 0); 2412 2413 for (;;) { 2414 udf_get_adslot(udf_node, slot, &s_ad, &eof); 2415 if (eof) 2416 break; 2417 2418 len = udf_rw32(s_ad.len); 2419 flags = UDF_EXT_FLAGS(len); 2420 len = UDF_EXT_LEN(len); 2421 lb_num = udf_rw32(s_ad.loc.lb_num); 2422 2423 if (flags == UDF_EXT_REDIRECT) { 2424 slot++; 2425 continue; 2426 } 2427 2428 DPRINTF(ALLOC, ("\t4i: got slot %d, slot_offset %d, " 2429 "replace_len %d, " 2430 "vp %d, lb %d, len %d, flags %d\n", 2431 slot, slot_offset, replace_len, 2432 udf_rw16(s_ad.loc.part_num), 2433 udf_rw32(s_ad.loc.lb_num), 2434 UDF_EXT_LEN(udf_rw32(s_ad.len)), 2435 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 2436 2437 /* adjust for slot offset */ 2438 if (slot_offset) { 2439 DPRINTF(ALLOC, ("\t4s: skipping %d\n", slot_offset)); 2440 lb_num += slot_offset / lb_size; 2441 len -= slot_offset; 2442 foffset += slot_offset; 2443 replace_len -= slot_offset; 2444 2445 /* mark adjusted */ 2446 slot_offset = 0; 2447 } 2448 2449 /* advance for (the rest of) this slot */ 2450 replace = MIN(len, replace_len); 2451 DPRINTF(ALLOC, ("\t4d: replacing %d\n", replace)); 2452 2453 /* advance for this slot */ 2454 if (replace) { 2455 /* note: dont round DOWN on num_lb since we then 2456 * forget the last partial one */ 2457 num_lb = (replace + lb_size - 1) / lb_size; 2458 if (flags != UDF_EXT_FREE) { 2459 udf_free_allocated_space(ump, lb_num, 2460 udf_rw16(s_ad.loc.part_num), num_lb); 2461 } 2462 lb_num += num_lb; 2463 len -= replace; 2464 foffset += replace; 2465 replace_len -= replace; 2466 } 2467 2468 /* do we have a slot tail ? */ 2469 if (len) { 2470 KASSERT(foffset % lb_size == 0); 2471 2472 /* we arrived at our point, push remainder */ 2473 s_ad.len = udf_rw32(len | flags); 2474 s_ad.loc.lb_num = udf_rw32(lb_num); 2475 if (flags == UDF_EXT_FREE) 2476 s_ad.loc.lb_num = udf_rw32(0); 2477 node_ad_cpy[cpy_slot++] = s_ad; 2478 foffset += len; 2479 slot++; 2480 2481 DPRINTF(ALLOC, ("\t4: vp %d, lb %d, len %d, flags %d " 2482 "-> stack\n", 2483 udf_rw16(s_ad.loc.part_num), 2484 udf_rw32(s_ad.loc.lb_num), 2485 UDF_EXT_LEN(udf_rw32(s_ad.len)), 2486 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 2487 break; 2488 } 2489 2490 slot++; 2491 } 2492 2493 /* 5) copy remainder */ 2494 for (;;) { 2495 udf_get_adslot(udf_node, slot, &s_ad, &eof); 2496 if (eof) 2497 break; 2498 2499 len = udf_rw32(s_ad.len); 2500 flags = UDF_EXT_FLAGS(len); 2501 len = UDF_EXT_LEN(len); 2502 2503 if (flags == UDF_EXT_REDIRECT) { 2504 slot++; 2505 continue; 2506 } 2507 2508 node_ad_cpy[cpy_slot++] = s_ad; 2509 2510 DPRINTF(ALLOC, ("\t5: insert new mapping " 2511 "vp %d lb %d, len %d, flags %d " 2512 "-> stack\n", 2513 udf_rw16(s_ad.loc.part_num), 2514 udf_rw32(s_ad.loc.lb_num), 2515 UDF_EXT_LEN(udf_rw32(s_ad.len)), 2516 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 2517 2518 slot++; 2519 } 2520 2521 /* 6) reset node descriptors */ 2522 udf_wipe_adslots(udf_node); 2523 2524 /* 7) copy back extents; merge when possible. Recounting on the fly */ 2525 cpy_slots = cpy_slot; 2526 2527 c_ad = node_ad_cpy[0]; 2528 slot = 0; 2529 DPRINTF(ALLOC, ("\t7s: stack -> got mapping vp %d " 2530 "lb %d, len %d, flags %d\n", 2531 udf_rw16(c_ad.loc.part_num), 2532 udf_rw32(c_ad.loc.lb_num), 2533 UDF_EXT_LEN(udf_rw32(c_ad.len)), 2534 UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30)); 2535 2536 for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) { 2537 s_ad = node_ad_cpy[cpy_slot]; 2538 2539 DPRINTF(ALLOC, ("\t7i: stack -> got mapping vp %d " 2540 "lb %d, len %d, flags %d\n", 2541 udf_rw16(s_ad.loc.part_num), 2542 udf_rw32(s_ad.loc.lb_num), 2543 UDF_EXT_LEN(udf_rw32(s_ad.len)), 2544 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 2545 2546 /* see if we can merge */ 2547 if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) { 2548 /* not mergable (anymore) */ 2549 DPRINTF(ALLOC, ("\t7: appending vp %d lb %d, " 2550 "len %d, flags %d\n", 2551 udf_rw16(c_ad.loc.part_num), 2552 udf_rw32(c_ad.loc.lb_num), 2553 UDF_EXT_LEN(udf_rw32(c_ad.len)), 2554 UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30)); 2555 2556 error = udf_append_adslot(udf_node, &slot, &c_ad); 2557 if (error) { 2558 buf->b_error = error; 2559 goto out; 2560 } 2561 c_ad = s_ad; 2562 slot++; 2563 } 2564 } 2565 2566 /* 8) push rest slot (if any) */ 2567 if (UDF_EXT_LEN(c_ad.len) > 0) { 2568 DPRINTF(ALLOC, ("\t8: last append vp %d lb %d, " 2569 "len %d, flags %d\n", 2570 udf_rw16(c_ad.loc.part_num), 2571 udf_rw32(c_ad.loc.lb_num), 2572 UDF_EXT_LEN(udf_rw32(c_ad.len)), 2573 UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30)); 2574 2575 error = udf_append_adslot(udf_node, &slot, &c_ad); 2576 if (error) { 2577 buf->b_error = error; 2578 goto out; 2579 } 2580 } 2581 2582 out: 2583 udf_count_alloc_exts(udf_node); 2584 2585 /* the node's descriptors should now be sane */ 2586 udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec); 2587 UDF_UNLOCK_NODE(udf_node, 0); 2588 2589 KASSERT(orig_inflen == new_inflen); 2590 KASSERT(new_lbrec >= orig_lbrec); 2591 2592 return; 2593 } 2594 2595 /* --------------------------------------------------------------------- */ 2596 2597 int 2598 udf_grow_node(struct udf_node *udf_node, uint64_t new_size) 2599 { 2600 struct vnode *vp = udf_node->vnode; 2601 struct udf_mount *ump = udf_node->ump; 2602 struct file_entry *fe; 2603 struct extfile_entry *efe; 2604 struct icb_tag *icbtag; 2605 struct long_ad c_ad, s_ad; 2606 uint64_t size_diff, old_size, inflen, objsize, chunk, append_len; 2607 uint64_t foffset, end_foffset; 2608 uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec; 2609 uint32_t lb_size, unit_size, dscr_size, crclen, lastblock_grow; 2610 uint32_t icbflags, len, flags, max_len; 2611 uint32_t max_l_ad, l_ad, l_ea; 2612 uint16_t my_part, dst_part; 2613 uint8_t *evacuated_data; 2614 int addr_type; 2615 int slot; 2616 int eof, error; 2617 2618 DPRINTF(ALLOC, ("udf_grow_node\n")); 2619 2620 UDF_LOCK_NODE(udf_node, 0); 2621 udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec); 2622 2623 lb_size = udf_rw32(ump->logical_vol->lb_size); 2624 2625 /* max_len in unit's IFF its a metadata node or metadata mirror node */ 2626 unit_size = lb_size; 2627 if ((udf_node == ump->metadata_node) || (udf_node == ump->metadatamirror_node)) 2628 unit_size = ump->metadata_alloc_unit_size * lb_size; 2629 max_len = ((UDF_EXT_MAXLEN / unit_size) * unit_size); 2630 2631 fe = udf_node->fe; 2632 efe = udf_node->efe; 2633 if (fe) { 2634 icbtag = &fe->icbtag; 2635 inflen = udf_rw64(fe->inf_len); 2636 objsize = inflen; 2637 dscr_size = sizeof(struct file_entry) -1; 2638 l_ea = udf_rw32(fe->l_ea); 2639 l_ad = udf_rw32(fe->l_ad); 2640 } else { 2641 icbtag = &efe->icbtag; 2642 inflen = udf_rw64(efe->inf_len); 2643 objsize = udf_rw64(efe->obj_size); 2644 dscr_size = sizeof(struct extfile_entry) -1; 2645 l_ea = udf_rw32(efe->l_ea); 2646 l_ad = udf_rw32(efe->l_ad); 2647 } 2648 max_l_ad = lb_size - dscr_size - l_ea; 2649 2650 icbflags = udf_rw16(icbtag->flags); 2651 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 2652 2653 old_size = inflen; 2654 size_diff = new_size - old_size; 2655 2656 DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size)); 2657 2658 evacuated_data = NULL; 2659 if (addr_type == UDF_ICB_INTERN_ALLOC) { 2660 if (l_ad + size_diff <= max_l_ad) { 2661 /* only reflect size change directly in the node */ 2662 inflen += size_diff; 2663 objsize += size_diff; 2664 l_ad += size_diff; 2665 crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad; 2666 if (fe) { 2667 fe->inf_len = udf_rw64(inflen); 2668 fe->l_ad = udf_rw32(l_ad); 2669 fe->tag.desc_crc_len = udf_rw16(crclen); 2670 } else { 2671 efe->inf_len = udf_rw64(inflen); 2672 efe->obj_size = udf_rw64(objsize); 2673 efe->l_ad = udf_rw32(l_ad); 2674 efe->tag.desc_crc_len = udf_rw16(crclen); 2675 } 2676 error = 0; 2677 2678 /* set new size for uvm */ 2679 uvm_vnp_setwritesize(vp, new_size); 2680 uvm_vnp_setsize(vp, new_size); 2681 2682 #if 0 2683 /* zero append space in buffer */ 2684 ubc_zerorange(&vp->v_uobj, old_size, 2685 new_size - old_size, UBC_UNMAP_FLAG(vp)); 2686 #endif 2687 2688 udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec); 2689 2690 /* unlock */ 2691 UDF_UNLOCK_NODE(udf_node, 0); 2692 2693 KASSERT(new_inflen == orig_inflen + size_diff); 2694 KASSERT(new_lbrec == orig_lbrec); 2695 KASSERT(new_lbrec == 0); 2696 return 0; 2697 } 2698 2699 DPRINTF(ALLOC, ("\tCONVERT from internal\n")); 2700 2701 if (old_size > 0) { 2702 /* allocate some space and copy in the stuff to keep */ 2703 evacuated_data = malloc(lb_size, M_UDFTEMP, M_WAITOK); 2704 memset(evacuated_data, 0, lb_size); 2705 2706 /* node is locked, so safe to exit mutex */ 2707 UDF_UNLOCK_NODE(udf_node, 0); 2708 2709 /* read in using the `normal' vn_rdwr() */ 2710 error = vn_rdwr(UIO_READ, udf_node->vnode, 2711 evacuated_data, old_size, 0, 2712 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, 2713 FSCRED, NULL, NULL); 2714 2715 /* enter again */ 2716 UDF_LOCK_NODE(udf_node, 0); 2717 } 2718 2719 /* convert to a normal alloc and select type */ 2720 my_part = udf_rw16(udf_node->loc.loc.part_num); 2721 dst_part = udf_get_record_vpart(ump, udf_get_c_type(udf_node)); 2722 addr_type = UDF_ICB_SHORT_ALLOC; 2723 if (dst_part != my_part) 2724 addr_type = UDF_ICB_LONG_ALLOC; 2725 2726 icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK; 2727 icbflags |= addr_type; 2728 icbtag->flags = udf_rw16(icbflags); 2729 2730 /* wipe old descriptor space */ 2731 udf_wipe_adslots(udf_node); 2732 2733 memset(&c_ad, 0, sizeof(struct long_ad)); 2734 c_ad.len = udf_rw32(old_size | UDF_EXT_FREE); 2735 c_ad.loc.part_num = udf_rw16(0); /* not relevant */ 2736 c_ad.loc.lb_num = udf_rw32(0); /* not relevant */ 2737 2738 slot = 0; 2739 } else { 2740 /* goto the last entry (if any) */ 2741 slot = 0; 2742 foffset = 0; 2743 memset(&c_ad, 0, sizeof(struct long_ad)); 2744 for (;;) { 2745 udf_get_adslot(udf_node, slot, &c_ad, &eof); 2746 if (eof) 2747 break; 2748 2749 len = udf_rw32(c_ad.len); 2750 flags = UDF_EXT_FLAGS(len); 2751 len = UDF_EXT_LEN(len); 2752 2753 end_foffset = foffset + len; 2754 if (flags != UDF_EXT_REDIRECT) 2755 foffset = end_foffset; 2756 2757 slot++; 2758 } 2759 /* at end of adslots */ 2760 2761 /* special case if the old size was zero, then there is no last slot */ 2762 if (old_size == 0) { 2763 c_ad.len = udf_rw32(0 | UDF_EXT_FREE); 2764 c_ad.loc.part_num = udf_rw16(0); /* not relevant */ 2765 c_ad.loc.lb_num = udf_rw32(0); /* not relevant */ 2766 } else { 2767 /* refetch last slot */ 2768 slot--; 2769 udf_get_adslot(udf_node, slot, &c_ad, &eof); 2770 } 2771 } 2772 2773 /* 2774 * If the length of the last slot is not a multiple of lb_size, adjust 2775 * length so that it is; don't forget to adjust `append_len'! relevant for 2776 * extending existing files 2777 */ 2778 len = udf_rw32(c_ad.len); 2779 flags = UDF_EXT_FLAGS(len); 2780 len = UDF_EXT_LEN(len); 2781 2782 lastblock_grow = 0; 2783 if (len % lb_size > 0) { 2784 lastblock_grow = lb_size - (len % lb_size); 2785 lastblock_grow = MIN(size_diff, lastblock_grow); 2786 len += lastblock_grow; 2787 c_ad.len = udf_rw32(len | flags); 2788 2789 /* TODO zero appened space in buffer! */ 2790 /* using ubc_zerorange(&vp->v_uobj, old_size, */ 2791 /* new_size - old_size, UBC_UNMAP_FLAG(vp)); ? */ 2792 } 2793 memset(&s_ad, 0, sizeof(struct long_ad)); 2794 2795 /* size_diff can be bigger than allowed, so grow in chunks */ 2796 append_len = size_diff - lastblock_grow; 2797 while (append_len > 0) { 2798 chunk = MIN(append_len, max_len); 2799 s_ad.len = udf_rw32(chunk | UDF_EXT_FREE); 2800 s_ad.loc.part_num = udf_rw16(0); 2801 s_ad.loc.lb_num = udf_rw32(0); 2802 2803 if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) { 2804 /* not mergable (anymore) */ 2805 error = udf_append_adslot(udf_node, &slot, &c_ad); 2806 if (error) 2807 goto errorout; 2808 slot++; 2809 c_ad = s_ad; 2810 memset(&s_ad, 0, sizeof(struct long_ad)); 2811 } 2812 append_len -= chunk; 2813 } 2814 2815 /* if there is a rest piece in the accumulator, append it */ 2816 if (UDF_EXT_LEN(udf_rw32(c_ad.len)) > 0) { 2817 error = udf_append_adslot(udf_node, &slot, &c_ad); 2818 if (error) 2819 goto errorout; 2820 slot++; 2821 } 2822 2823 /* if there is a rest piece that didn't fit, append it */ 2824 if (UDF_EXT_LEN(udf_rw32(s_ad.len)) > 0) { 2825 error = udf_append_adslot(udf_node, &slot, &s_ad); 2826 if (error) 2827 goto errorout; 2828 slot++; 2829 } 2830 2831 inflen += size_diff; 2832 objsize += size_diff; 2833 if (fe) { 2834 fe->inf_len = udf_rw64(inflen); 2835 } else { 2836 efe->inf_len = udf_rw64(inflen); 2837 efe->obj_size = udf_rw64(objsize); 2838 } 2839 error = 0; 2840 2841 if (evacuated_data) { 2842 /* set new write size for uvm */ 2843 uvm_vnp_setwritesize(vp, old_size); 2844 2845 /* write out evacuated data */ 2846 error = vn_rdwr(UIO_WRITE, udf_node->vnode, 2847 evacuated_data, old_size, 0, 2848 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, 2849 FSCRED, NULL, NULL); 2850 uvm_vnp_setsize(vp, old_size); 2851 } 2852 2853 errorout: 2854 if (evacuated_data) 2855 free(evacuated_data, M_UDFTEMP); 2856 2857 udf_count_alloc_exts(udf_node); 2858 2859 udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec); 2860 UDF_UNLOCK_NODE(udf_node, 0); 2861 2862 KASSERT(new_inflen == orig_inflen + size_diff); 2863 KASSERT(new_lbrec == orig_lbrec); 2864 2865 return error; 2866 } 2867 2868 /* --------------------------------------------------------------------- */ 2869 2870 int 2871 udf_shrink_node(struct udf_node *udf_node, uint64_t new_size) 2872 { 2873 struct vnode *vp = udf_node->vnode; 2874 struct udf_mount *ump = udf_node->ump; 2875 struct file_entry *fe; 2876 struct extfile_entry *efe; 2877 struct icb_tag *icbtag; 2878 struct long_ad c_ad, s_ad, *node_ad_cpy; 2879 uint64_t size_diff, old_size, inflen, objsize; 2880 uint64_t foffset, end_foffset; 2881 uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec; 2882 uint32_t lb_size, unit_size, dscr_size, crclen; 2883 uint32_t slot_offset, slot_offset_lb; 2884 uint32_t len, flags, max_len; 2885 uint32_t num_lb, lb_num; 2886 uint32_t max_l_ad, l_ad, l_ea; 2887 uint16_t vpart_num; 2888 uint8_t *data_pos; 2889 int icbflags, addr_type; 2890 int slot, cpy_slot, cpy_slots; 2891 int eof, error; 2892 2893 DPRINTF(ALLOC, ("udf_shrink_node\n")); 2894 2895 UDF_LOCK_NODE(udf_node, 0); 2896 udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec); 2897 2898 lb_size = udf_rw32(ump->logical_vol->lb_size); 2899 2900 /* max_len in unit's IFF its a metadata node or metadata mirror node */ 2901 unit_size = lb_size; 2902 if ((udf_node == ump->metadata_node) || (udf_node == ump->metadatamirror_node)) 2903 unit_size = ump->metadata_alloc_unit_size * lb_size; 2904 max_len = ((UDF_EXT_MAXLEN / unit_size) * unit_size); 2905 2906 /* do the work */ 2907 fe = udf_node->fe; 2908 efe = udf_node->efe; 2909 if (fe) { 2910 icbtag = &fe->icbtag; 2911 inflen = udf_rw64(fe->inf_len); 2912 objsize = inflen; 2913 dscr_size = sizeof(struct file_entry) -1; 2914 l_ea = udf_rw32(fe->l_ea); 2915 l_ad = udf_rw32(fe->l_ad); 2916 data_pos = (uint8_t *) fe + dscr_size + l_ea; 2917 } else { 2918 icbtag = &efe->icbtag; 2919 inflen = udf_rw64(efe->inf_len); 2920 objsize = udf_rw64(efe->obj_size); 2921 dscr_size = sizeof(struct extfile_entry) -1; 2922 l_ea = udf_rw32(efe->l_ea); 2923 l_ad = udf_rw32(efe->l_ad); 2924 data_pos = (uint8_t *) efe + dscr_size + l_ea; 2925 } 2926 max_l_ad = lb_size - dscr_size - l_ea; 2927 2928 icbflags = udf_rw16(icbtag->flags); 2929 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 2930 2931 old_size = inflen; 2932 size_diff = old_size - new_size; 2933 2934 DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size)); 2935 2936 /* shrink the node to its new size */ 2937 if (addr_type == UDF_ICB_INTERN_ALLOC) { 2938 /* only reflect size change directly in the node */ 2939 KASSERT(new_size <= max_l_ad); 2940 inflen -= size_diff; 2941 objsize -= size_diff; 2942 l_ad -= size_diff; 2943 crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad; 2944 if (fe) { 2945 fe->inf_len = udf_rw64(inflen); 2946 fe->l_ad = udf_rw32(l_ad); 2947 fe->tag.desc_crc_len = udf_rw16(crclen); 2948 } else { 2949 efe->inf_len = udf_rw64(inflen); 2950 efe->obj_size = udf_rw64(objsize); 2951 efe->l_ad = udf_rw32(l_ad); 2952 efe->tag.desc_crc_len = udf_rw16(crclen); 2953 } 2954 error = 0; 2955 2956 /* clear the space in the descriptor */ 2957 KASSERT(old_size > new_size); 2958 memset(data_pos + new_size, 0, old_size - new_size); 2959 2960 /* TODO zero appened space in buffer! */ 2961 /* using ubc_zerorange(&vp->v_uobj, old_size, */ 2962 /* old_size - new_size, UBC_UNMAP_FLAG(vp)); ? */ 2963 2964 /* set new size for uvm */ 2965 uvm_vnp_setsize(vp, new_size); 2966 2967 udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec); 2968 UDF_UNLOCK_NODE(udf_node, 0); 2969 2970 KASSERT(new_inflen == orig_inflen - size_diff); 2971 KASSERT(new_lbrec == orig_lbrec); 2972 KASSERT(new_lbrec == 0); 2973 2974 return 0; 2975 } 2976 2977 /* setup node cleanup extents copy space */ 2978 node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS, 2979 M_UDFMNT, M_WAITOK); 2980 memset(node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS); 2981 2982 /* 2983 * Shrink the node by releasing the allocations and truncate the last 2984 * allocation to the new size. If the new size fits into the 2985 * allocation descriptor itself, transform it into an 2986 * UDF_ICB_INTERN_ALLOC. 2987 */ 2988 slot = 0; 2989 cpy_slot = 0; 2990 foffset = 0; 2991 2992 /* 1) copy till first overlap piece to the rewrite buffer */ 2993 for (;;) { 2994 udf_get_adslot(udf_node, slot, &s_ad, &eof); 2995 if (eof) { 2996 DPRINTF(WRITE, 2997 ("Shrink node failed: " 2998 "encountered EOF\n")); 2999 error = EINVAL; 3000 goto errorout; /* panic? */ 3001 } 3002 len = udf_rw32(s_ad.len); 3003 flags = UDF_EXT_FLAGS(len); 3004 len = UDF_EXT_LEN(len); 3005 3006 if (flags == UDF_EXT_REDIRECT) { 3007 slot++; 3008 continue; 3009 } 3010 3011 end_foffset = foffset + len; 3012 if (end_foffset > new_size) 3013 break; /* found */ 3014 3015 node_ad_cpy[cpy_slot++] = s_ad; 3016 3017 DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d " 3018 "-> stack\n", 3019 udf_rw16(s_ad.loc.part_num), 3020 udf_rw32(s_ad.loc.lb_num), 3021 UDF_EXT_LEN(udf_rw32(s_ad.len)), 3022 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 3023 3024 foffset = end_foffset; 3025 slot++; 3026 } 3027 slot_offset = new_size - foffset; 3028 3029 /* 2) trunc overlapping slot at overlap and copy it */ 3030 if (slot_offset > 0) { 3031 lb_num = udf_rw32(s_ad.loc.lb_num); 3032 vpart_num = udf_rw16(s_ad.loc.part_num); 3033 3034 if (flags == UDF_EXT_ALLOCATED) { 3035 /* calculate extent in lb, and offset in lb */ 3036 num_lb = (len + lb_size -1) / lb_size; 3037 slot_offset_lb = (slot_offset + lb_size -1) / lb_size; 3038 3039 /* adjust our slot */ 3040 lb_num += slot_offset_lb; 3041 num_lb -= slot_offset_lb; 3042 3043 udf_free_allocated_space(ump, lb_num, vpart_num, num_lb); 3044 } 3045 3046 s_ad.len = udf_rw32(slot_offset | flags); 3047 node_ad_cpy[cpy_slot++] = s_ad; 3048 slot++; 3049 3050 DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d " 3051 "-> stack\n", 3052 udf_rw16(s_ad.loc.part_num), 3053 udf_rw32(s_ad.loc.lb_num), 3054 UDF_EXT_LEN(udf_rw32(s_ad.len)), 3055 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 3056 } 3057 3058 /* 3) delete remainder */ 3059 for (;;) { 3060 udf_get_adslot(udf_node, slot, &s_ad, &eof); 3061 if (eof) 3062 break; 3063 3064 len = udf_rw32(s_ad.len); 3065 flags = UDF_EXT_FLAGS(len); 3066 len = UDF_EXT_LEN(len); 3067 3068 if (flags == UDF_EXT_REDIRECT) { 3069 slot++; 3070 continue; 3071 } 3072 3073 DPRINTF(ALLOC, ("\t3: delete remainder " 3074 "vp %d lb %d, len %d, flags %d\n", 3075 udf_rw16(s_ad.loc.part_num), 3076 udf_rw32(s_ad.loc.lb_num), 3077 UDF_EXT_LEN(udf_rw32(s_ad.len)), 3078 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 3079 3080 if (flags == UDF_EXT_ALLOCATED) { 3081 lb_num = udf_rw32(s_ad.loc.lb_num); 3082 vpart_num = udf_rw16(s_ad.loc.part_num); 3083 num_lb = (len + lb_size - 1) / lb_size; 3084 3085 udf_free_allocated_space(ump, lb_num, vpart_num, 3086 num_lb); 3087 } 3088 3089 slot++; 3090 } 3091 3092 /* 4) if it will fit into the descriptor then convert */ 3093 if (new_size < max_l_ad) { 3094 /* 3095 * resque/evacuate old piece by reading it in, and convert it 3096 * to internal alloc. 3097 */ 3098 if (new_size == 0) { 3099 /* XXX/TODO only for zero sizing now */ 3100 udf_wipe_adslots(udf_node); 3101 3102 icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK; 3103 icbflags |= UDF_ICB_INTERN_ALLOC; 3104 icbtag->flags = udf_rw16(icbflags); 3105 3106 inflen -= size_diff; KASSERT(inflen == 0); 3107 objsize -= size_diff; 3108 l_ad = new_size; 3109 crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad; 3110 if (fe) { 3111 fe->inf_len = udf_rw64(inflen); 3112 fe->l_ad = udf_rw32(l_ad); 3113 fe->tag.desc_crc_len = udf_rw16(crclen); 3114 } else { 3115 efe->inf_len = udf_rw64(inflen); 3116 efe->obj_size = udf_rw64(objsize); 3117 efe->l_ad = udf_rw32(l_ad); 3118 efe->tag.desc_crc_len = udf_rw16(crclen); 3119 } 3120 /* eventually copy in evacuated piece */ 3121 /* set new size for uvm */ 3122 uvm_vnp_setsize(vp, new_size); 3123 3124 free(node_ad_cpy, M_UDFMNT); 3125 udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec); 3126 3127 UDF_UNLOCK_NODE(udf_node, 0); 3128 3129 KASSERT(new_inflen == orig_inflen - size_diff); 3130 KASSERT(new_inflen == 0); 3131 KASSERT(new_lbrec == 0); 3132 3133 return 0; 3134 } 3135 3136 printf("UDF_SHRINK_NODE: could convert to internal alloc!\n"); 3137 } 3138 3139 /* 5) reset node descriptors */ 3140 udf_wipe_adslots(udf_node); 3141 3142 /* 6) copy back extents; merge when possible. Recounting on the fly */ 3143 cpy_slots = cpy_slot; 3144 3145 c_ad = node_ad_cpy[0]; 3146 slot = 0; 3147 for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) { 3148 s_ad = node_ad_cpy[cpy_slot]; 3149 3150 DPRINTF(ALLOC, ("\t6: stack -> got mapping vp %d " 3151 "lb %d, len %d, flags %d\n", 3152 udf_rw16(s_ad.loc.part_num), 3153 udf_rw32(s_ad.loc.lb_num), 3154 UDF_EXT_LEN(udf_rw32(s_ad.len)), 3155 UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30)); 3156 3157 /* see if we can merge */ 3158 if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) { 3159 /* not mergable (anymore) */ 3160 DPRINTF(ALLOC, ("\t6: appending vp %d lb %d, " 3161 "len %d, flags %d\n", 3162 udf_rw16(c_ad.loc.part_num), 3163 udf_rw32(c_ad.loc.lb_num), 3164 UDF_EXT_LEN(udf_rw32(c_ad.len)), 3165 UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30)); 3166 3167 error = udf_append_adslot(udf_node, &slot, &c_ad); 3168 if (error) 3169 goto errorout; /* panic? */ 3170 c_ad = s_ad; 3171 slot++; 3172 } 3173 } 3174 3175 /* 7) push rest slot (if any) */ 3176 if (UDF_EXT_LEN(c_ad.len) > 0) { 3177 DPRINTF(ALLOC, ("\t7: last append vp %d lb %d, " 3178 "len %d, flags %d\n", 3179 udf_rw16(c_ad.loc.part_num), 3180 udf_rw32(c_ad.loc.lb_num), 3181 UDF_EXT_LEN(udf_rw32(c_ad.len)), 3182 UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30)); 3183 3184 error = udf_append_adslot(udf_node, &slot, &c_ad); 3185 if (error) 3186 goto errorout; /* panic? */ 3187 ; 3188 } 3189 3190 inflen -= size_diff; 3191 objsize -= size_diff; 3192 if (fe) { 3193 fe->inf_len = udf_rw64(inflen); 3194 } else { 3195 efe->inf_len = udf_rw64(inflen); 3196 efe->obj_size = udf_rw64(objsize); 3197 } 3198 error = 0; 3199 3200 /* set new size for uvm */ 3201 uvm_vnp_setsize(vp, new_size); 3202 3203 errorout: 3204 free(node_ad_cpy, M_UDFMNT); 3205 3206 udf_count_alloc_exts(udf_node); 3207 3208 udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec); 3209 UDF_UNLOCK_NODE(udf_node, 0); 3210 3211 KASSERT(new_inflen == orig_inflen - size_diff); 3212 3213 return error; 3214 } 3215 3216