1 /* $NetBSD: udf_subr.c,v 1.113 2011/01/22 18:02:18 reinoud 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 30 #include <sys/cdefs.h> 31 #ifndef lint 32 __KERNEL_RCSID(0, "$NetBSD: udf_subr.c,v 1.113 2011/01/22 18:02:18 reinoud Exp $"); 33 #endif /* not lint */ 34 35 36 #if defined(_KERNEL_OPT) 37 #include "opt_compat_netbsd.h" 38 #endif 39 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 <fs/unicode.h> 60 #include <dev/clock_subr.h> 61 62 #include <fs/udf/ecma167-udf.h> 63 #include <fs/udf/udf_mount.h> 64 #include <sys/dirhash.h> 65 66 #include "udf.h" 67 #include "udf_subr.h" 68 #include "udf_bswap.h" 69 70 71 #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data) 72 73 #define UDF_SET_SYSTEMFILE(vp) \ 74 /* XXXAD Is the vnode locked? */ \ 75 (vp)->v_vflag |= VV_SYSTEM; \ 76 vref((vp)); \ 77 vput((vp)); \ 78 79 extern int syncer_maxdelay; /* maximum delay time */ 80 extern int (**udf_vnodeop_p)(void *); 81 82 /* --------------------------------------------------------------------- */ 83 84 //#ifdef DEBUG 85 #if 1 86 87 #if 0 88 static void 89 udf_dumpblob(boid *blob, uint32_t dlen) 90 { 91 int i, j; 92 93 printf("blob = %p\n", blob); 94 printf("dump of %d bytes\n", dlen); 95 96 for (i = 0; i < dlen; i+ = 16) { 97 printf("%04x ", i); 98 for (j = 0; j < 16; j++) { 99 if (i+j < dlen) { 100 printf("%02x ", blob[i+j]); 101 } else { 102 printf(" "); 103 } 104 } 105 for (j = 0; j < 16; j++) { 106 if (i+j < dlen) { 107 if (blob[i+j]>32 && blob[i+j]! = 127) { 108 printf("%c", blob[i+j]); 109 } else { 110 printf("."); 111 } 112 } 113 } 114 printf("\n"); 115 } 116 printf("\n"); 117 Debugger(); 118 } 119 #endif 120 121 static void 122 udf_dump_discinfo(struct udf_mount *ump) 123 { 124 char bits[128]; 125 struct mmc_discinfo *di = &ump->discinfo; 126 127 if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0) 128 return; 129 130 printf("Device/media info :\n"); 131 printf("\tMMC profile 0x%02x\n", di->mmc_profile); 132 printf("\tderived class %d\n", di->mmc_class); 133 printf("\tsector size %d\n", di->sector_size); 134 printf("\tdisc state %d\n", di->disc_state); 135 printf("\tlast ses state %d\n", di->last_session_state); 136 printf("\tbg format state %d\n", di->bg_format_state); 137 printf("\tfrst track %d\n", di->first_track); 138 printf("\tfst on last ses %d\n", di->first_track_last_session); 139 printf("\tlst on last ses %d\n", di->last_track_last_session); 140 printf("\tlink block penalty %d\n", di->link_block_penalty); 141 snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS, di->disc_flags); 142 printf("\tdisc flags %s\n", bits); 143 printf("\tdisc id %x\n", di->disc_id); 144 printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode); 145 146 printf("\tnum sessions %d\n", di->num_sessions); 147 printf("\tnum tracks %d\n", di->num_tracks); 148 149 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cur); 150 printf("\tcapabilities cur %s\n", bits); 151 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cap); 152 printf("\tcapabilities cap %s\n", bits); 153 } 154 155 static void 156 udf_dump_trackinfo(struct mmc_trackinfo *trackinfo) 157 { 158 char bits[128]; 159 160 if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0) 161 return; 162 163 printf("Trackinfo for track %d:\n", trackinfo->tracknr); 164 printf("\tsessionnr %d\n", trackinfo->sessionnr); 165 printf("\ttrack mode %d\n", trackinfo->track_mode); 166 printf("\tdata mode %d\n", trackinfo->data_mode); 167 snprintb(bits, sizeof(bits), MMC_TRACKINFO_FLAGBITS, trackinfo->flags); 168 printf("\tflags %s\n", bits); 169 170 printf("\ttrack start %d\n", trackinfo->track_start); 171 printf("\tnext_writable %d\n", trackinfo->next_writable); 172 printf("\tfree_blocks %d\n", trackinfo->free_blocks); 173 printf("\tpacket_size %d\n", trackinfo->packet_size); 174 printf("\ttrack size %d\n", trackinfo->track_size); 175 printf("\tlast recorded block %d\n", trackinfo->last_recorded); 176 } 177 178 #else 179 #define udf_dump_discinfo(a); 180 #define udf_dump_trackinfo(a); 181 #endif 182 183 184 /* --------------------------------------------------------------------- */ 185 186 /* not called often */ 187 int 188 udf_update_discinfo(struct udf_mount *ump) 189 { 190 struct vnode *devvp = ump->devvp; 191 struct partinfo dpart; 192 struct mmc_discinfo *di; 193 int error; 194 195 DPRINTF(VOLUMES, ("read/update disc info\n")); 196 di = &ump->discinfo; 197 memset(di, 0, sizeof(struct mmc_discinfo)); 198 199 /* check if we're on a MMC capable device, i.e. CD/DVD */ 200 error = VOP_IOCTL(devvp, MMCGETDISCINFO, di, FKIOCTL, NOCRED); 201 if (error == 0) { 202 udf_dump_discinfo(ump); 203 return 0; 204 } 205 206 /* disc partition support */ 207 error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED); 208 if (error) 209 return ENODEV; 210 211 /* set up a disc info profile for partitions */ 212 di->mmc_profile = 0x01; /* disc type */ 213 di->mmc_class = MMC_CLASS_DISC; 214 di->disc_state = MMC_STATE_CLOSED; 215 di->last_session_state = MMC_STATE_CLOSED; 216 di->bg_format_state = MMC_BGFSTATE_COMPLETED; 217 di->link_block_penalty = 0; 218 219 di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE | 220 MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE; 221 di->mmc_cap = di->mmc_cur; 222 di->disc_flags = MMC_DFLAGS_UNRESTRICTED; 223 224 /* TODO problem with last_possible_lba on resizable VND; request */ 225 di->last_possible_lba = dpart.part->p_size; 226 di->sector_size = dpart.disklab->d_secsize; 227 228 di->num_sessions = 1; 229 di->num_tracks = 1; 230 231 di->first_track = 1; 232 di->first_track_last_session = di->last_track_last_session = 1; 233 234 udf_dump_discinfo(ump); 235 return 0; 236 } 237 238 239 int 240 udf_update_trackinfo(struct udf_mount *ump, struct mmc_trackinfo *ti) 241 { 242 struct vnode *devvp = ump->devvp; 243 struct mmc_discinfo *di = &ump->discinfo; 244 int error, class; 245 246 DPRINTF(VOLUMES, ("read track info\n")); 247 248 class = di->mmc_class; 249 if (class != MMC_CLASS_DISC) { 250 /* tracknr specified in struct ti */ 251 error = VOP_IOCTL(devvp, MMCGETTRACKINFO, ti, FKIOCTL, NOCRED); 252 return error; 253 } 254 255 /* disc partition support */ 256 if (ti->tracknr != 1) 257 return EIO; 258 259 /* create fake ti (TODO check for resized vnds) */ 260 ti->sessionnr = 1; 261 262 ti->track_mode = 0; /* XXX */ 263 ti->data_mode = 0; /* XXX */ 264 ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID; 265 266 ti->track_start = 0; 267 ti->packet_size = 1; 268 269 /* TODO support for resizable vnd */ 270 ti->track_size = di->last_possible_lba; 271 ti->next_writable = di->last_possible_lba; 272 ti->last_recorded = ti->next_writable; 273 ti->free_blocks = 0; 274 275 return 0; 276 } 277 278 279 int 280 udf_setup_writeparams(struct udf_mount *ump) 281 { 282 struct mmc_writeparams mmc_writeparams; 283 int error; 284 285 if (ump->discinfo.mmc_class == MMC_CLASS_DISC) 286 return 0; 287 288 /* 289 * only CD burning normally needs setting up, but other disc types 290 * might need other settings to be made. The MMC framework will set up 291 * the nessisary recording parameters according to the disc 292 * characteristics read in. Modifications can be made in the discinfo 293 * structure passed to change the nature of the disc. 294 */ 295 296 memset(&mmc_writeparams, 0, sizeof(struct mmc_writeparams)); 297 mmc_writeparams.mmc_class = ump->discinfo.mmc_class; 298 mmc_writeparams.mmc_cur = ump->discinfo.mmc_cur; 299 300 /* 301 * UDF dictates first track to determine track mode for the whole 302 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1] 303 * To prevent problems with a `reserved' track in front we start with 304 * the 2nd track and if that is not valid, go for the 1st. 305 */ 306 mmc_writeparams.tracknr = 2; 307 mmc_writeparams.data_mode = MMC_DATAMODE_DEFAULT; /* XA disc */ 308 mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT; /* data */ 309 310 error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS, &mmc_writeparams, 311 FKIOCTL, NOCRED); 312 if (error) { 313 mmc_writeparams.tracknr = 1; 314 error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS, 315 &mmc_writeparams, FKIOCTL, NOCRED); 316 } 317 return error; 318 } 319 320 321 int 322 udf_synchronise_caches(struct udf_mount *ump) 323 { 324 struct mmc_op mmc_op; 325 326 DPRINTF(CALL, ("udf_synchronise_caches()\n")); 327 328 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) 329 return 0; 330 331 /* discs are done now */ 332 if (ump->discinfo.mmc_class == MMC_CLASS_DISC) 333 return 0; 334 335 memset(&mmc_op, 0, sizeof(struct mmc_op)); 336 mmc_op.operation = MMC_OP_SYNCHRONISECACHE; 337 338 /* ignore return code */ 339 (void) VOP_IOCTL(ump->devvp, MMCOP, &mmc_op, FKIOCTL, NOCRED); 340 341 return 0; 342 } 343 344 /* --------------------------------------------------------------------- */ 345 346 /* track/session searching for mounting */ 347 int 348 udf_search_tracks(struct udf_mount *ump, struct udf_args *args, 349 int *first_tracknr, int *last_tracknr) 350 { 351 struct mmc_trackinfo trackinfo; 352 uint32_t tracknr, start_track, num_tracks; 353 int error; 354 355 /* if negative, sessionnr is relative to last session */ 356 if (args->sessionnr < 0) { 357 args->sessionnr += ump->discinfo.num_sessions; 358 } 359 360 /* sanity */ 361 if (args->sessionnr < 0) 362 args->sessionnr = 0; 363 if (args->sessionnr > ump->discinfo.num_sessions) 364 args->sessionnr = ump->discinfo.num_sessions; 365 366 /* search the tracks for this session, zero session nr indicates last */ 367 if (args->sessionnr == 0) 368 args->sessionnr = ump->discinfo.num_sessions; 369 if (ump->discinfo.last_session_state == MMC_STATE_EMPTY) 370 args->sessionnr--; 371 372 /* sanity again */ 373 if (args->sessionnr < 0) 374 args->sessionnr = 0; 375 376 /* search the first and last track of the specified session */ 377 num_tracks = ump->discinfo.num_tracks; 378 start_track = ump->discinfo.first_track; 379 380 /* search for first track of this session */ 381 for (tracknr = start_track; tracknr <= num_tracks; tracknr++) { 382 /* get track info */ 383 trackinfo.tracknr = tracknr; 384 error = udf_update_trackinfo(ump, &trackinfo); 385 if (error) 386 return error; 387 388 if (trackinfo.sessionnr == args->sessionnr) 389 break; 390 } 391 *first_tracknr = tracknr; 392 393 /* search for last track of this session */ 394 for (;tracknr <= num_tracks; tracknr++) { 395 /* get track info */ 396 trackinfo.tracknr = tracknr; 397 error = udf_update_trackinfo(ump, &trackinfo); 398 if (error || (trackinfo.sessionnr != args->sessionnr)) { 399 tracknr--; 400 break; 401 } 402 } 403 if (tracknr > num_tracks) 404 tracknr--; 405 406 *last_tracknr = tracknr; 407 408 if (*last_tracknr < *first_tracknr) { 409 printf( "udf_search_tracks: sanity check on drive+disc failed, " 410 "drive returned garbage\n"); 411 return EINVAL; 412 } 413 414 assert(*last_tracknr >= *first_tracknr); 415 return 0; 416 } 417 418 419 /* 420 * NOTE: this is the only routine in this file that directly peeks into the 421 * metadata file but since its at a larval state of the mount it can't hurt. 422 * 423 * XXX candidate for udf_allocation.c 424 * XXX clean me up!, change to new node reading code. 425 */ 426 427 static void 428 udf_check_track_metadata_overlap(struct udf_mount *ump, 429 struct mmc_trackinfo *trackinfo) 430 { 431 struct part_desc *part; 432 struct file_entry *fe; 433 struct extfile_entry *efe; 434 struct short_ad *s_ad; 435 struct long_ad *l_ad; 436 uint32_t track_start, track_end; 437 uint32_t phys_part_start, phys_part_end, part_start, part_end; 438 uint32_t sector_size, len, alloclen, plb_num; 439 uint8_t *pos; 440 int addr_type, icblen, icbflags, flags; 441 442 /* get our track extents */ 443 track_start = trackinfo->track_start; 444 track_end = track_start + trackinfo->track_size; 445 446 /* get our base partition extent */ 447 KASSERT(ump->node_part == ump->fids_part); 448 part = ump->partitions[ump->vtop[ump->node_part]]; 449 phys_part_start = udf_rw32(part->start_loc); 450 phys_part_end = phys_part_start + udf_rw32(part->part_len); 451 452 /* no use if its outside the physical partition */ 453 if ((phys_part_start >= track_end) || (phys_part_end < track_start)) 454 return; 455 456 /* 457 * now follow all extents in the fe/efe to see if they refer to this 458 * track 459 */ 460 461 sector_size = ump->discinfo.sector_size; 462 463 /* XXX should we claim exclusive access to the metafile ? */ 464 /* TODO: move to new node read code */ 465 fe = ump->metadata_node->fe; 466 efe = ump->metadata_node->efe; 467 if (fe) { 468 alloclen = udf_rw32(fe->l_ad); 469 pos = &fe->data[0] + udf_rw32(fe->l_ea); 470 icbflags = udf_rw16(fe->icbtag.flags); 471 } else { 472 assert(efe); 473 alloclen = udf_rw32(efe->l_ad); 474 pos = &efe->data[0] + udf_rw32(efe->l_ea); 475 icbflags = udf_rw16(efe->icbtag.flags); 476 } 477 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 478 479 while (alloclen) { 480 if (addr_type == UDF_ICB_SHORT_ALLOC) { 481 icblen = sizeof(struct short_ad); 482 s_ad = (struct short_ad *) pos; 483 len = udf_rw32(s_ad->len); 484 plb_num = udf_rw32(s_ad->lb_num); 485 } else { 486 /* should not be present, but why not */ 487 icblen = sizeof(struct long_ad); 488 l_ad = (struct long_ad *) pos; 489 len = udf_rw32(l_ad->len); 490 plb_num = udf_rw32(l_ad->loc.lb_num); 491 /* pvpart_num = udf_rw16(l_ad->loc.part_num); */ 492 } 493 /* process extent */ 494 flags = UDF_EXT_FLAGS(len); 495 len = UDF_EXT_LEN(len); 496 497 part_start = phys_part_start + plb_num; 498 part_end = part_start + (len / sector_size); 499 500 if ((part_start >= track_start) && (part_end <= track_end)) { 501 /* extent is enclosed within this track */ 502 ump->metadata_track = *trackinfo; 503 return; 504 } 505 506 pos += icblen; 507 alloclen -= icblen; 508 } 509 } 510 511 512 int 513 udf_search_writing_tracks(struct udf_mount *ump) 514 { 515 struct vnode *devvp = ump->devvp; 516 struct mmc_trackinfo trackinfo; 517 struct mmc_op mmc_op; 518 struct part_desc *part; 519 uint32_t tracknr, start_track, num_tracks; 520 uint32_t track_start, track_end, part_start, part_end; 521 int node_alloc, error; 522 523 /* 524 * in the CD/(HD)DVD/BD recordable device model a few tracks within 525 * the last session might be open but in the UDF device model at most 526 * three tracks can be open: a reserved track for delayed ISO VRS 527 * writing, a data track and a metadata track. We search here for the 528 * data track and the metadata track. Note that the reserved track is 529 * troublesome but can be detected by its small size of < 512 sectors. 530 */ 531 532 /* update discinfo since it might have changed */ 533 error = udf_update_discinfo(ump); 534 if (error) 535 return error; 536 537 num_tracks = ump->discinfo.num_tracks; 538 start_track = ump->discinfo.first_track; 539 540 /* fetch info on first and possibly only track */ 541 trackinfo.tracknr = start_track; 542 error = udf_update_trackinfo(ump, &trackinfo); 543 if (error) 544 return error; 545 546 /* copy results to our mount point */ 547 ump->data_track = trackinfo; 548 ump->metadata_track = trackinfo; 549 550 /* if not sequential, we're done */ 551 if (num_tracks == 1) 552 return 0; 553 554 for (tracknr = start_track;tracknr <= num_tracks; tracknr++) { 555 /* get track info */ 556 trackinfo.tracknr = tracknr; 557 error = udf_update_trackinfo(ump, &trackinfo); 558 if (error) 559 return error; 560 561 /* 562 * If this track is marked damaged, ask for repair. This is an 563 * optional command, so ignore its error but report warning. 564 */ 565 if (trackinfo.flags & MMC_TRACKINFO_DAMAGED) { 566 memset(&mmc_op, 0, sizeof(mmc_op)); 567 mmc_op.operation = MMC_OP_REPAIRTRACK; 568 mmc_op.mmc_profile = ump->discinfo.mmc_profile; 569 mmc_op.tracknr = tracknr; 570 error = VOP_IOCTL(devvp, MMCOP, &mmc_op, FKIOCTL, NOCRED); 571 if (error) 572 (void)printf("Drive can't explicitly repair " 573 "damaged track %d, but it might " 574 "autorepair\n", tracknr); 575 576 /* reget track info */ 577 error = udf_update_trackinfo(ump, &trackinfo); 578 if (error) 579 return error; 580 } 581 if ((trackinfo.flags & MMC_TRACKINFO_NWA_VALID) == 0) 582 continue; 583 584 track_start = trackinfo.track_start; 585 track_end = track_start + trackinfo.track_size; 586 587 /* check for overlap on data partition */ 588 part = ump->partitions[ump->data_part]; 589 part_start = udf_rw32(part->start_loc); 590 part_end = part_start + udf_rw32(part->part_len); 591 if ((part_start < track_end) && (part_end > track_start)) { 592 ump->data_track = trackinfo; 593 /* TODO check if UDF partition data_part is writable */ 594 } 595 596 /* check for overlap on metadata partition */ 597 node_alloc = ump->vtop_alloc[ump->node_part]; 598 if ((node_alloc == UDF_ALLOC_METASEQUENTIAL) || 599 (node_alloc == UDF_ALLOC_METABITMAP)) { 600 udf_check_track_metadata_overlap(ump, &trackinfo); 601 } else { 602 ump->metadata_track = trackinfo; 603 } 604 } 605 606 if ((ump->data_track.flags & MMC_TRACKINFO_NWA_VALID) == 0) 607 return EROFS; 608 609 if ((ump->metadata_track.flags & MMC_TRACKINFO_NWA_VALID) == 0) 610 return EROFS; 611 612 return 0; 613 } 614 615 /* --------------------------------------------------------------------- */ 616 617 /* 618 * Check if the blob starts with a good UDF tag. Tags are protected by a 619 * checksum over the reader except one byte at position 4 that is the checksum 620 * itself. 621 */ 622 623 int 624 udf_check_tag(void *blob) 625 { 626 struct desc_tag *tag = blob; 627 uint8_t *pos, sum, cnt; 628 629 /* check TAG header checksum */ 630 pos = (uint8_t *) tag; 631 sum = 0; 632 633 for(cnt = 0; cnt < 16; cnt++) { 634 if (cnt != 4) 635 sum += *pos; 636 pos++; 637 } 638 if (sum != tag->cksum) { 639 /* bad tag header checksum; this is not a valid tag */ 640 return EINVAL; 641 } 642 643 return 0; 644 } 645 646 647 /* 648 * check tag payload will check descriptor CRC as specified. 649 * If the descriptor is too long, it will return EIO otherwise EINVAL. 650 */ 651 652 int 653 udf_check_tag_payload(void *blob, uint32_t max_length) 654 { 655 struct desc_tag *tag = blob; 656 uint16_t crc, crc_len; 657 658 crc_len = udf_rw16(tag->desc_crc_len); 659 660 /* check payload CRC if applicable */ 661 if (crc_len == 0) 662 return 0; 663 664 if (crc_len > max_length) 665 return EIO; 666 667 crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len); 668 if (crc != udf_rw16(tag->desc_crc)) { 669 /* bad payload CRC; this is a broken tag */ 670 return EINVAL; 671 } 672 673 return 0; 674 } 675 676 677 void 678 udf_validate_tag_sum(void *blob) 679 { 680 struct desc_tag *tag = blob; 681 uint8_t *pos, sum, cnt; 682 683 /* calculate TAG header checksum */ 684 pos = (uint8_t *) tag; 685 sum = 0; 686 687 for(cnt = 0; cnt < 16; cnt++) { 688 if (cnt != 4) sum += *pos; 689 pos++; 690 } 691 tag->cksum = sum; /* 8 bit */ 692 } 693 694 695 /* assumes sector number of descriptor to be saved already present */ 696 void 697 udf_validate_tag_and_crc_sums(void *blob) 698 { 699 struct desc_tag *tag = blob; 700 uint8_t *btag = (uint8_t *) tag; 701 uint16_t crc, crc_len; 702 703 crc_len = udf_rw16(tag->desc_crc_len); 704 705 /* check payload CRC if applicable */ 706 if (crc_len > 0) { 707 crc = udf_cksum(btag + UDF_DESC_TAG_LENGTH, crc_len); 708 tag->desc_crc = udf_rw16(crc); 709 } 710 711 /* calculate TAG header checksum */ 712 udf_validate_tag_sum(blob); 713 } 714 715 /* --------------------------------------------------------------------- */ 716 717 /* 718 * XXX note the different semantics from udfclient: for FIDs it still rounds 719 * up to sectors. Use udf_fidsize() for a correct length. 720 */ 721 722 int 723 udf_tagsize(union dscrptr *dscr, uint32_t lb_size) 724 { 725 uint32_t size, tag_id, num_lb, elmsz; 726 727 tag_id = udf_rw16(dscr->tag.id); 728 729 switch (tag_id) { 730 case TAGID_LOGVOL : 731 size = sizeof(struct logvol_desc) - 1; 732 size += udf_rw32(dscr->lvd.mt_l); 733 break; 734 case TAGID_UNALLOC_SPACE : 735 elmsz = sizeof(struct extent_ad); 736 size = sizeof(struct unalloc_sp_desc) - elmsz; 737 size += udf_rw32(dscr->usd.alloc_desc_num) * elmsz; 738 break; 739 case TAGID_FID : 740 size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu); 741 size = (size + 3) & ~3; 742 break; 743 case TAGID_LOGVOL_INTEGRITY : 744 size = sizeof(struct logvol_int_desc) - sizeof(uint32_t); 745 size += udf_rw32(dscr->lvid.l_iu); 746 size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t)); 747 break; 748 case TAGID_SPACE_BITMAP : 749 size = sizeof(struct space_bitmap_desc) - 1; 750 size += udf_rw32(dscr->sbd.num_bytes); 751 break; 752 case TAGID_SPARING_TABLE : 753 elmsz = sizeof(struct spare_map_entry); 754 size = sizeof(struct udf_sparing_table) - elmsz; 755 size += udf_rw16(dscr->spt.rt_l) * elmsz; 756 break; 757 case TAGID_FENTRY : 758 size = sizeof(struct file_entry); 759 size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1; 760 break; 761 case TAGID_EXTFENTRY : 762 size = sizeof(struct extfile_entry); 763 size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1; 764 break; 765 case TAGID_FSD : 766 size = sizeof(struct fileset_desc); 767 break; 768 default : 769 size = sizeof(union dscrptr); 770 break; 771 } 772 773 if ((size == 0) || (lb_size == 0)) 774 return 0; 775 776 if (lb_size == 1) 777 return size; 778 779 /* round up in sectors */ 780 num_lb = (size + lb_size -1) / lb_size; 781 return num_lb * lb_size; 782 } 783 784 785 int 786 udf_fidsize(struct fileid_desc *fid) 787 { 788 uint32_t size; 789 790 if (udf_rw16(fid->tag.id) != TAGID_FID) 791 panic("got udf_fidsize on non FID\n"); 792 793 size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu); 794 size = (size + 3) & ~3; 795 796 return size; 797 } 798 799 /* --------------------------------------------------------------------- */ 800 801 void 802 udf_lock_node(struct udf_node *udf_node, int flag, char const *fname, const int lineno) 803 { 804 int ret; 805 806 mutex_enter(&udf_node->node_mutex); 807 /* wait until free */ 808 while (udf_node->i_flags & IN_LOCKED) { 809 ret = cv_timedwait(&udf_node->node_lock, &udf_node->node_mutex, hz/8); 810 /* TODO check if we should return error; abort */ 811 if (ret == EWOULDBLOCK) { 812 DPRINTF(LOCKING, ( "udf_lock_node: udf_node %p would block " 813 "wanted at %s:%d, previously locked at %s:%d\n", 814 udf_node, fname, lineno, 815 udf_node->lock_fname, udf_node->lock_lineno)); 816 } 817 } 818 /* grab */ 819 udf_node->i_flags |= IN_LOCKED | flag; 820 /* debug */ 821 udf_node->lock_fname = fname; 822 udf_node->lock_lineno = lineno; 823 824 mutex_exit(&udf_node->node_mutex); 825 } 826 827 828 void 829 udf_unlock_node(struct udf_node *udf_node, int flag) 830 { 831 mutex_enter(&udf_node->node_mutex); 832 udf_node->i_flags &= ~(IN_LOCKED | flag); 833 cv_broadcast(&udf_node->node_lock); 834 mutex_exit(&udf_node->node_mutex); 835 } 836 837 838 /* --------------------------------------------------------------------- */ 839 840 static int 841 udf_read_anchor(struct udf_mount *ump, uint32_t sector, struct anchor_vdp **dst) 842 { 843 int error; 844 845 error = udf_read_phys_dscr(ump, sector, M_UDFVOLD, 846 (union dscrptr **) dst); 847 if (!error) { 848 /* blank terminator blocks are not allowed here */ 849 if (*dst == NULL) 850 return ENOENT; 851 if (udf_rw16((*dst)->tag.id) != TAGID_ANCHOR) { 852 error = ENOENT; 853 free(*dst, M_UDFVOLD); 854 *dst = NULL; 855 DPRINTF(VOLUMES, ("Not an anchor\n")); 856 } 857 } 858 859 return error; 860 } 861 862 863 int 864 udf_read_anchors(struct udf_mount *ump) 865 { 866 struct udf_args *args = &ump->mount_args; 867 struct mmc_trackinfo first_track; 868 struct mmc_trackinfo second_track; 869 struct mmc_trackinfo last_track; 870 struct anchor_vdp **anchorsp; 871 uint32_t track_start; 872 uint32_t track_end; 873 uint32_t positions[4]; 874 int first_tracknr, last_tracknr; 875 int error, anch, ok, first_anchor; 876 877 /* search the first and last track of the specified session */ 878 error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr); 879 if (!error) { 880 first_track.tracknr = first_tracknr; 881 error = udf_update_trackinfo(ump, &first_track); 882 } 883 if (!error) { 884 last_track.tracknr = last_tracknr; 885 error = udf_update_trackinfo(ump, &last_track); 886 } 887 if ((!error) && (first_tracknr != last_tracknr)) { 888 second_track.tracknr = first_tracknr+1; 889 error = udf_update_trackinfo(ump, &second_track); 890 } 891 if (error) { 892 printf("UDF mount: reading disc geometry failed\n"); 893 return 0; 894 } 895 896 track_start = first_track.track_start; 897 898 /* `end' is not as straitforward as start. */ 899 track_end = last_track.track_start 900 + last_track.track_size - last_track.free_blocks - 1; 901 902 if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) { 903 /* end of track is not straitforward here */ 904 if (last_track.flags & MMC_TRACKINFO_LRA_VALID) 905 track_end = last_track.last_recorded; 906 else if (last_track.flags & MMC_TRACKINFO_NWA_VALID) 907 track_end = last_track.next_writable 908 - ump->discinfo.link_block_penalty; 909 } 910 911 /* its no use reading a blank track */ 912 first_anchor = 0; 913 if (first_track.flags & MMC_TRACKINFO_BLANK) 914 first_anchor = 1; 915 916 /* get our packet size */ 917 ump->packet_size = first_track.packet_size; 918 if (first_track.flags & MMC_TRACKINFO_BLANK) 919 ump->packet_size = second_track.packet_size; 920 921 if (ump->packet_size <= 1) { 922 /* take max, but not bigger than 64 */ 923 ump->packet_size = MAXPHYS / ump->discinfo.sector_size; 924 ump->packet_size = MIN(ump->packet_size, 64); 925 } 926 KASSERT(ump->packet_size >= 1); 927 928 /* read anchors start+256, start+512, end-256, end */ 929 positions[0] = track_start+256; 930 positions[1] = track_end-256; 931 positions[2] = track_end; 932 positions[3] = track_start+512; /* [UDF 2.60/6.11.2] */ 933 /* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */ 934 935 ok = 0; 936 anchorsp = ump->anchors; 937 for (anch = first_anchor; anch < 4; anch++) { 938 DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch, 939 positions[anch])); 940 error = udf_read_anchor(ump, positions[anch], anchorsp); 941 if (!error) { 942 anchorsp++; 943 ok++; 944 } 945 } 946 947 /* VATs are only recorded on sequential media, but initialise */ 948 ump->first_possible_vat_location = track_start + 2; 949 ump->last_possible_vat_location = track_end + last_track.packet_size; 950 951 return ok; 952 } 953 954 /* --------------------------------------------------------------------- */ 955 956 int 957 udf_get_c_type(struct udf_node *udf_node) 958 { 959 int isdir, what; 960 961 isdir = (udf_node->vnode->v_type == VDIR); 962 what = isdir ? UDF_C_FIDS : UDF_C_USERDATA; 963 964 if (udf_node->ump) 965 if (udf_node == udf_node->ump->metadatabitmap_node) 966 what = UDF_C_METADATA_SBM; 967 968 return what; 969 } 970 971 972 int 973 udf_get_record_vpart(struct udf_mount *ump, int udf_c_type) 974 { 975 int vpart_num; 976 977 vpart_num = ump->data_part; 978 if (udf_c_type == UDF_C_NODE) 979 vpart_num = ump->node_part; 980 if (udf_c_type == UDF_C_FIDS) 981 vpart_num = ump->fids_part; 982 983 return vpart_num; 984 } 985 986 987 /* 988 * BUGALERT: some rogue implementations use random physical partition 989 * numbers to break other implementations so lookup the number. 990 */ 991 992 static uint16_t 993 udf_find_raw_phys(struct udf_mount *ump, uint16_t raw_phys_part) 994 { 995 struct part_desc *part; 996 uint16_t phys_part; 997 998 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 999 part = ump->partitions[phys_part]; 1000 if (part == NULL) 1001 break; 1002 if (udf_rw16(part->part_num) == raw_phys_part) 1003 break; 1004 } 1005 return phys_part; 1006 } 1007 1008 /* --------------------------------------------------------------------- */ 1009 1010 /* we dont try to be smart; we just record the parts */ 1011 #define UDF_UPDATE_DSCR(name, dscr) \ 1012 if (name) \ 1013 free(name, M_UDFVOLD); \ 1014 name = dscr; 1015 1016 static int 1017 udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr) 1018 { 1019 uint16_t phys_part, raw_phys_part; 1020 1021 DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n", 1022 udf_rw16(dscr->tag.id))); 1023 switch (udf_rw16(dscr->tag.id)) { 1024 case TAGID_PRI_VOL : /* primary partition */ 1025 UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd); 1026 break; 1027 case TAGID_LOGVOL : /* logical volume */ 1028 UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd); 1029 break; 1030 case TAGID_UNALLOC_SPACE : /* unallocated space */ 1031 UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd); 1032 break; 1033 case TAGID_IMP_VOL : /* implementation */ 1034 /* XXX do we care about multiple impl. descr ? */ 1035 UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd); 1036 break; 1037 case TAGID_PARTITION : /* physical partition */ 1038 /* not much use if its not allocated */ 1039 if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) { 1040 free(dscr, M_UDFVOLD); 1041 break; 1042 } 1043 1044 /* 1045 * BUGALERT: some rogue implementations use random physical 1046 * partition numbers to break other implementations so lookup 1047 * the number. 1048 */ 1049 raw_phys_part = udf_rw16(dscr->pd.part_num); 1050 phys_part = udf_find_raw_phys(ump, raw_phys_part); 1051 1052 if (phys_part == UDF_PARTITIONS) { 1053 free(dscr, M_UDFVOLD); 1054 return EINVAL; 1055 } 1056 1057 UDF_UPDATE_DSCR(ump->partitions[phys_part], &dscr->pd); 1058 break; 1059 case TAGID_VOL : /* volume space extender; rare */ 1060 DPRINTF(VOLUMES, ("VDS extender ignored\n")); 1061 free(dscr, M_UDFVOLD); 1062 break; 1063 default : 1064 DPRINTF(VOLUMES, ("Unhandled VDS type %d\n", 1065 udf_rw16(dscr->tag.id))); 1066 free(dscr, M_UDFVOLD); 1067 } 1068 1069 return 0; 1070 } 1071 #undef UDF_UPDATE_DSCR 1072 1073 /* --------------------------------------------------------------------- */ 1074 1075 static int 1076 udf_read_vds_extent(struct udf_mount *ump, uint32_t loc, uint32_t len) 1077 { 1078 union dscrptr *dscr; 1079 uint32_t sector_size, dscr_size; 1080 int error; 1081 1082 sector_size = ump->discinfo.sector_size; 1083 1084 /* loc is sectornr, len is in bytes */ 1085 error = EIO; 1086 while (len) { 1087 error = udf_read_phys_dscr(ump, loc, M_UDFVOLD, &dscr); 1088 if (error) 1089 return error; 1090 1091 /* blank block is a terminator */ 1092 if (dscr == NULL) 1093 return 0; 1094 1095 /* TERM descriptor is a terminator */ 1096 if (udf_rw16(dscr->tag.id) == TAGID_TERM) { 1097 free(dscr, M_UDFVOLD); 1098 return 0; 1099 } 1100 1101 /* process all others */ 1102 dscr_size = udf_tagsize(dscr, sector_size); 1103 error = udf_process_vds_descriptor(ump, dscr); 1104 if (error) { 1105 free(dscr, M_UDFVOLD); 1106 break; 1107 } 1108 assert((dscr_size % sector_size) == 0); 1109 1110 len -= dscr_size; 1111 loc += dscr_size / sector_size; 1112 } 1113 1114 return error; 1115 } 1116 1117 1118 int 1119 udf_read_vds_space(struct udf_mount *ump) 1120 { 1121 /* struct udf_args *args = &ump->mount_args; */ 1122 struct anchor_vdp *anchor, *anchor2; 1123 size_t size; 1124 uint32_t main_loc, main_len; 1125 uint32_t reserve_loc, reserve_len; 1126 int error; 1127 1128 /* 1129 * read in VDS space provided by the anchors; if one descriptor read 1130 * fails, try the mirror sector. 1131 * 1132 * check if 2nd anchor is different from 1st; if so, go for 2nd. This 1133 * avoids the `compatibility features' of DirectCD that may confuse 1134 * stuff completely. 1135 */ 1136 1137 anchor = ump->anchors[0]; 1138 anchor2 = ump->anchors[1]; 1139 assert(anchor); 1140 1141 if (anchor2) { 1142 size = sizeof(struct extent_ad); 1143 if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size)) 1144 anchor = anchor2; 1145 /* reserve is specified to be a literal copy of main */ 1146 } 1147 1148 main_loc = udf_rw32(anchor->main_vds_ex.loc); 1149 main_len = udf_rw32(anchor->main_vds_ex.len); 1150 1151 reserve_loc = udf_rw32(anchor->reserve_vds_ex.loc); 1152 reserve_len = udf_rw32(anchor->reserve_vds_ex.len); 1153 1154 error = udf_read_vds_extent(ump, main_loc, main_len); 1155 if (error) { 1156 printf("UDF mount: reading in reserve VDS extent\n"); 1157 error = udf_read_vds_extent(ump, reserve_loc, reserve_len); 1158 } 1159 1160 return error; 1161 } 1162 1163 /* --------------------------------------------------------------------- */ 1164 1165 /* 1166 * Read in the logical volume integrity sequence pointed to by our logical 1167 * volume descriptor. Its a sequence that can be extended using fields in the 1168 * integrity descriptor itself. On sequential media only one is found, on 1169 * rewritable media a sequence of descriptors can be found as a form of 1170 * history keeping and on non sequential write-once media the chain is vital 1171 * to allow more and more descriptors to be written. The last descriptor 1172 * written in an extent needs to claim space for a new extent. 1173 */ 1174 1175 static int 1176 udf_retrieve_lvint(struct udf_mount *ump) 1177 { 1178 union dscrptr *dscr; 1179 struct logvol_int_desc *lvint; 1180 struct udf_lvintq *trace; 1181 uint32_t lb_size, lbnum, len; 1182 int dscr_type, error, trace_len; 1183 1184 lb_size = udf_rw32(ump->logical_vol->lb_size); 1185 len = udf_rw32(ump->logical_vol->integrity_seq_loc.len); 1186 lbnum = udf_rw32(ump->logical_vol->integrity_seq_loc.loc); 1187 1188 /* clean trace */ 1189 memset(ump->lvint_trace, 0, 1190 UDF_LVDINT_SEGMENTS * sizeof(struct udf_lvintq)); 1191 1192 trace_len = 0; 1193 trace = ump->lvint_trace; 1194 trace->start = lbnum; 1195 trace->end = lbnum + len/lb_size; 1196 trace->pos = 0; 1197 trace->wpos = 0; 1198 1199 lvint = NULL; 1200 dscr = NULL; 1201 error = 0; 1202 while (len) { 1203 trace->pos = lbnum - trace->start; 1204 trace->wpos = trace->pos + 1; 1205 1206 /* read in our integrity descriptor */ 1207 error = udf_read_phys_dscr(ump, lbnum, M_UDFVOLD, &dscr); 1208 if (!error) { 1209 if (dscr == NULL) { 1210 trace->wpos = trace->pos; 1211 break; /* empty terminates */ 1212 } 1213 dscr_type = udf_rw16(dscr->tag.id); 1214 if (dscr_type == TAGID_TERM) { 1215 trace->wpos = trace->pos; 1216 break; /* clean terminator */ 1217 } 1218 if (dscr_type != TAGID_LOGVOL_INTEGRITY) { 1219 /* fatal... corrupt disc */ 1220 error = ENOENT; 1221 break; 1222 } 1223 if (lvint) 1224 free(lvint, M_UDFVOLD); 1225 lvint = &dscr->lvid; 1226 dscr = NULL; 1227 } /* else hope for the best... maybe the next is ok */ 1228 1229 DPRINTFIF(VOLUMES, lvint, ("logvol integrity read, state %s\n", 1230 udf_rw32(lvint->integrity_type) ? "CLOSED" : "OPEN")); 1231 1232 /* proceed sequential */ 1233 lbnum += 1; 1234 len -= lb_size; 1235 1236 /* are we linking to a new piece? */ 1237 if (dscr && lvint->next_extent.len) { 1238 len = udf_rw32(lvint->next_extent.len); 1239 lbnum = udf_rw32(lvint->next_extent.loc); 1240 1241 if (trace_len >= UDF_LVDINT_SEGMENTS-1) { 1242 /* IEK! segment link full... */ 1243 DPRINTF(VOLUMES, ("lvdint segments full\n")); 1244 error = EINVAL; 1245 } else { 1246 trace++; 1247 trace_len++; 1248 1249 trace->start = lbnum; 1250 trace->end = lbnum + len/lb_size; 1251 trace->pos = 0; 1252 trace->wpos = 0; 1253 } 1254 } 1255 } 1256 1257 /* clean up the mess, esp. when there is an error */ 1258 if (dscr) 1259 free(dscr, M_UDFVOLD); 1260 1261 if (error && lvint) { 1262 free(lvint, M_UDFVOLD); 1263 lvint = NULL; 1264 } 1265 1266 if (!lvint) 1267 error = ENOENT; 1268 1269 ump->logvol_integrity = lvint; 1270 return error; 1271 } 1272 1273 1274 static int 1275 udf_loose_lvint_history(struct udf_mount *ump) 1276 { 1277 union dscrptr **bufs, *dscr, *last_dscr; 1278 struct udf_lvintq *trace, *in_trace, *out_trace; 1279 struct logvol_int_desc *lvint; 1280 uint32_t in_ext, in_pos, in_len; 1281 uint32_t out_ext, out_wpos, out_len; 1282 uint32_t lb_size, packet_size, lb_num; 1283 uint32_t len, start; 1284 int ext, minext, extlen, cnt, cpy_len, dscr_type; 1285 int losing; 1286 int error; 1287 1288 DPRINTF(VOLUMES, ("need to lose some lvint history\n")); 1289 1290 lb_size = udf_rw32(ump->logical_vol->lb_size); 1291 packet_size = ump->data_track.packet_size; /* XXX data track */ 1292 1293 /* search smallest extent */ 1294 trace = &ump->lvint_trace[0]; 1295 minext = trace->end - trace->start; 1296 for (ext = 1; ext < UDF_LVDINT_SEGMENTS; ext++) { 1297 trace = &ump->lvint_trace[ext]; 1298 extlen = trace->end - trace->start; 1299 if (extlen == 0) 1300 break; 1301 minext = MIN(minext, extlen); 1302 } 1303 losing = MIN(minext, UDF_LVINT_LOSSAGE); 1304 /* no sense wiping all */ 1305 if (losing == minext) 1306 losing--; 1307 1308 DPRINTF(VOLUMES, ("\tlosing %d entries\n", losing)); 1309 1310 /* get buffer for pieces */ 1311 bufs = malloc(UDF_LVDINT_SEGMENTS * sizeof(void *), M_TEMP, M_WAITOK); 1312 1313 in_ext = 0; 1314 in_pos = losing; 1315 in_trace = &ump->lvint_trace[in_ext]; 1316 in_len = in_trace->end - in_trace->start; 1317 out_ext = 0; 1318 out_wpos = 0; 1319 out_trace = &ump->lvint_trace[out_ext]; 1320 out_len = out_trace->end - out_trace->start; 1321 1322 last_dscr = NULL; 1323 for(;;) { 1324 out_trace->pos = out_wpos; 1325 out_trace->wpos = out_trace->pos; 1326 if (in_pos >= in_len) { 1327 in_ext++; 1328 in_pos = 0; 1329 in_trace = &ump->lvint_trace[in_ext]; 1330 in_len = in_trace->end - in_trace->start; 1331 } 1332 if (out_wpos >= out_len) { 1333 out_ext++; 1334 out_wpos = 0; 1335 out_trace = &ump->lvint_trace[out_ext]; 1336 out_len = out_trace->end - out_trace->start; 1337 } 1338 /* copy overlap contents */ 1339 cpy_len = MIN(in_len - in_pos, out_len - out_wpos); 1340 cpy_len = MIN(cpy_len, in_len - in_trace->pos); 1341 if (cpy_len == 0) 1342 break; 1343 1344 /* copy */ 1345 DPRINTF(VOLUMES, ("\treading %d lvid descriptors\n", cpy_len)); 1346 for (cnt = 0; cnt < cpy_len; cnt++) { 1347 /* read in our integrity descriptor */ 1348 lb_num = in_trace->start + in_pos + cnt; 1349 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, 1350 &dscr); 1351 if (error) { 1352 /* copy last one */ 1353 dscr = last_dscr; 1354 } 1355 bufs[cnt] = dscr; 1356 if (!error) { 1357 if (dscr == NULL) { 1358 out_trace->pos = out_wpos + cnt; 1359 out_trace->wpos = out_trace->pos; 1360 break; /* empty terminates */ 1361 } 1362 dscr_type = udf_rw16(dscr->tag.id); 1363 if (dscr_type == TAGID_TERM) { 1364 out_trace->pos = out_wpos + cnt; 1365 out_trace->wpos = out_trace->pos; 1366 break; /* clean terminator */ 1367 } 1368 if (dscr_type != TAGID_LOGVOL_INTEGRITY) { 1369 panic( "UDF integrity sequence " 1370 "corrupted while mounted!\n"); 1371 } 1372 last_dscr = dscr; 1373 } 1374 } 1375 1376 /* patch up if first entry was on error */ 1377 if (bufs[0] == NULL) { 1378 for (cnt = 0; cnt < cpy_len; cnt++) 1379 if (bufs[cnt] != NULL) 1380 break; 1381 last_dscr = bufs[cnt]; 1382 for (; cnt > 0; cnt--) { 1383 bufs[cnt] = last_dscr; 1384 } 1385 } 1386 1387 /* glue + write out */ 1388 DPRINTF(VOLUMES, ("\twriting %d lvid descriptors\n", cpy_len)); 1389 for (cnt = 0; cnt < cpy_len; cnt++) { 1390 lb_num = out_trace->start + out_wpos + cnt; 1391 lvint = &bufs[cnt]->lvid; 1392 1393 /* set continuation */ 1394 len = 0; 1395 start = 0; 1396 if (out_wpos + cnt == out_len) { 1397 /* get continuation */ 1398 trace = &ump->lvint_trace[out_ext+1]; 1399 len = trace->end - trace->start; 1400 start = trace->start; 1401 } 1402 lvint->next_extent.len = udf_rw32(len); 1403 lvint->next_extent.loc = udf_rw32(start); 1404 1405 lb_num = trace->start + trace->wpos; 1406 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR, 1407 bufs[cnt], lb_num, lb_num); 1408 DPRINTFIF(VOLUMES, error, 1409 ("error writing lvint lb_num\n")); 1410 } 1411 1412 /* free non repeating descriptors */ 1413 last_dscr = NULL; 1414 for (cnt = 0; cnt < cpy_len; cnt++) { 1415 if (bufs[cnt] != last_dscr) 1416 free(bufs[cnt], M_UDFVOLD); 1417 last_dscr = bufs[cnt]; 1418 } 1419 1420 /* advance */ 1421 in_pos += cpy_len; 1422 out_wpos += cpy_len; 1423 } 1424 1425 free(bufs, M_TEMP); 1426 1427 return 0; 1428 } 1429 1430 1431 static int 1432 udf_writeout_lvint(struct udf_mount *ump, int lvflag) 1433 { 1434 struct udf_lvintq *trace; 1435 struct timeval now_v; 1436 struct timespec now_s; 1437 uint32_t sector; 1438 int logvol_integrity; 1439 int space, error; 1440 1441 DPRINTF(VOLUMES, ("writing out logvol integrity descriptor\n")); 1442 1443 again: 1444 /* get free space in last chunk */ 1445 trace = ump->lvint_trace; 1446 while (trace->wpos > (trace->end - trace->start)) { 1447 DPRINTF(VOLUMES, ("skip : start = %d, end = %d, pos = %d, " 1448 "wpos = %d\n", trace->start, trace->end, 1449 trace->pos, trace->wpos)); 1450 trace++; 1451 } 1452 1453 /* check if there is space to append */ 1454 space = (trace->end - trace->start) - trace->wpos; 1455 DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, " 1456 "space = %d\n", trace->start, trace->end, trace->pos, 1457 trace->wpos, space)); 1458 1459 /* get state */ 1460 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type); 1461 if (logvol_integrity == UDF_INTEGRITY_CLOSED) { 1462 if ((space < 3) && (lvflag & UDF_APPENDONLY_LVINT)) { 1463 /* TODO extent LVINT space if possible */ 1464 return EROFS; 1465 } 1466 } 1467 1468 if (space < 1) { 1469 if (lvflag & UDF_APPENDONLY_LVINT) 1470 return EROFS; 1471 /* loose history by re-writing extents */ 1472 error = udf_loose_lvint_history(ump); 1473 if (error) 1474 return error; 1475 goto again; 1476 } 1477 1478 /* update our integrity descriptor to identify us and timestamp it */ 1479 DPRINTF(VOLUMES, ("updating integrity descriptor\n")); 1480 microtime(&now_v); 1481 TIMEVAL_TO_TIMESPEC(&now_v, &now_s); 1482 udf_timespec_to_timestamp(&now_s, &ump->logvol_integrity->time); 1483 udf_set_regid(&ump->logvol_info->impl_id, IMPL_NAME); 1484 udf_add_impl_regid(ump, &ump->logvol_info->impl_id); 1485 1486 /* writeout integrity descriptor */ 1487 sector = trace->start + trace->wpos; 1488 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR, 1489 (union dscrptr *) ump->logvol_integrity, 1490 sector, sector); 1491 DPRINTF(VOLUMES, ("writeout lvint : error = %d\n", error)); 1492 if (error) 1493 return error; 1494 1495 /* advance write position */ 1496 trace->wpos++; space--; 1497 if (space >= 1) { 1498 /* append terminator */ 1499 sector = trace->start + trace->wpos; 1500 error = udf_write_terminator(ump, sector); 1501 1502 DPRINTF(VOLUMES, ("write terminator : error = %d\n", error)); 1503 } 1504 1505 space = (trace->end - trace->start) - trace->wpos; 1506 DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, " 1507 "space = %d\n", trace->start, trace->end, trace->pos, 1508 trace->wpos, space)); 1509 DPRINTF(VOLUMES, ("finished writing out logvol integrity descriptor " 1510 "successfull\n")); 1511 1512 return error; 1513 } 1514 1515 /* --------------------------------------------------------------------- */ 1516 1517 static int 1518 udf_read_physical_partition_spacetables(struct udf_mount *ump) 1519 { 1520 union dscrptr *dscr; 1521 /* struct udf_args *args = &ump->mount_args; */ 1522 struct part_desc *partd; 1523 struct part_hdr_desc *parthdr; 1524 struct udf_bitmap *bitmap; 1525 uint32_t phys_part; 1526 uint32_t lb_num, len; 1527 int error, dscr_type; 1528 1529 /* unallocated space map */ 1530 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 1531 partd = ump->partitions[phys_part]; 1532 if (partd == NULL) 1533 continue; 1534 parthdr = &partd->_impl_use.part_hdr; 1535 1536 lb_num = udf_rw32(partd->start_loc); 1537 lb_num += udf_rw32(parthdr->unalloc_space_bitmap.lb_num); 1538 len = udf_rw32(parthdr->unalloc_space_bitmap.len); 1539 if (len == 0) 1540 continue; 1541 1542 DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num)); 1543 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr); 1544 if (!error && dscr) { 1545 /* analyse */ 1546 dscr_type = udf_rw16(dscr->tag.id); 1547 if (dscr_type == TAGID_SPACE_BITMAP) { 1548 DPRINTF(VOLUMES, ("Accepting space bitmap\n")); 1549 ump->part_unalloc_dscr[phys_part] = &dscr->sbd; 1550 1551 /* fill in ump->part_unalloc_bits */ 1552 bitmap = &ump->part_unalloc_bits[phys_part]; 1553 bitmap->blob = (uint8_t *) dscr; 1554 bitmap->bits = dscr->sbd.data; 1555 bitmap->max_offset = udf_rw32(dscr->sbd.num_bits); 1556 bitmap->pages = NULL; /* TODO */ 1557 bitmap->data_pos = 0; 1558 bitmap->metadata_pos = 0; 1559 } else { 1560 free(dscr, M_UDFVOLD); 1561 1562 printf( "UDF mount: error reading unallocated " 1563 "space bitmap\n"); 1564 return EROFS; 1565 } 1566 } else { 1567 /* blank not allowed */ 1568 printf("UDF mount: blank unallocated space bitmap\n"); 1569 return EROFS; 1570 } 1571 } 1572 1573 /* unallocated space table (not supported) */ 1574 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 1575 partd = ump->partitions[phys_part]; 1576 if (partd == NULL) 1577 continue; 1578 parthdr = &partd->_impl_use.part_hdr; 1579 1580 len = udf_rw32(parthdr->unalloc_space_table.len); 1581 if (len) { 1582 printf("UDF mount: space tables not supported\n"); 1583 return EROFS; 1584 } 1585 } 1586 1587 /* freed space map */ 1588 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 1589 partd = ump->partitions[phys_part]; 1590 if (partd == NULL) 1591 continue; 1592 parthdr = &partd->_impl_use.part_hdr; 1593 1594 /* freed space map */ 1595 lb_num = udf_rw32(partd->start_loc); 1596 lb_num += udf_rw32(parthdr->freed_space_bitmap.lb_num); 1597 len = udf_rw32(parthdr->freed_space_bitmap.len); 1598 if (len == 0) 1599 continue; 1600 1601 DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num)); 1602 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr); 1603 if (!error && dscr) { 1604 /* analyse */ 1605 dscr_type = udf_rw16(dscr->tag.id); 1606 if (dscr_type == TAGID_SPACE_BITMAP) { 1607 DPRINTF(VOLUMES, ("Accepting space bitmap\n")); 1608 ump->part_freed_dscr[phys_part] = &dscr->sbd; 1609 1610 /* fill in ump->part_freed_bits */ 1611 bitmap = &ump->part_unalloc_bits[phys_part]; 1612 bitmap->blob = (uint8_t *) dscr; 1613 bitmap->bits = dscr->sbd.data; 1614 bitmap->max_offset = udf_rw32(dscr->sbd.num_bits); 1615 bitmap->pages = NULL; /* TODO */ 1616 bitmap->data_pos = 0; 1617 bitmap->metadata_pos = 0; 1618 } else { 1619 free(dscr, M_UDFVOLD); 1620 1621 printf( "UDF mount: error reading freed " 1622 "space bitmap\n"); 1623 return EROFS; 1624 } 1625 } else { 1626 /* blank not allowed */ 1627 printf("UDF mount: blank freed space bitmap\n"); 1628 return EROFS; 1629 } 1630 } 1631 1632 /* freed space table (not supported) */ 1633 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 1634 partd = ump->partitions[phys_part]; 1635 if (partd == NULL) 1636 continue; 1637 parthdr = &partd->_impl_use.part_hdr; 1638 1639 len = udf_rw32(parthdr->freed_space_table.len); 1640 if (len) { 1641 printf("UDF mount: space tables not supported\n"); 1642 return EROFS; 1643 } 1644 } 1645 1646 return 0; 1647 } 1648 1649 1650 /* TODO implement async writeout */ 1651 int 1652 udf_write_physical_partition_spacetables(struct udf_mount *ump, int waitfor) 1653 { 1654 union dscrptr *dscr; 1655 /* struct udf_args *args = &ump->mount_args; */ 1656 struct part_desc *partd; 1657 struct part_hdr_desc *parthdr; 1658 uint32_t phys_part; 1659 uint32_t lb_num, len, ptov; 1660 int error_all, error; 1661 1662 error_all = 0; 1663 /* unallocated space map */ 1664 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 1665 partd = ump->partitions[phys_part]; 1666 if (partd == NULL) 1667 continue; 1668 parthdr = &partd->_impl_use.part_hdr; 1669 1670 ptov = udf_rw32(partd->start_loc); 1671 lb_num = udf_rw32(parthdr->unalloc_space_bitmap.lb_num); 1672 len = udf_rw32(parthdr->unalloc_space_bitmap.len); 1673 if (len == 0) 1674 continue; 1675 1676 DPRINTF(VOLUMES, ("Write unalloc. space bitmap %d\n", 1677 lb_num + ptov)); 1678 dscr = (union dscrptr *) ump->part_unalloc_dscr[phys_part]; 1679 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR, 1680 (union dscrptr *) dscr, 1681 ptov + lb_num, lb_num); 1682 if (error) { 1683 DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error)); 1684 error_all = error; 1685 } 1686 } 1687 1688 /* freed space map */ 1689 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 1690 partd = ump->partitions[phys_part]; 1691 if (partd == NULL) 1692 continue; 1693 parthdr = &partd->_impl_use.part_hdr; 1694 1695 /* freed space map */ 1696 ptov = udf_rw32(partd->start_loc); 1697 lb_num = udf_rw32(parthdr->freed_space_bitmap.lb_num); 1698 len = udf_rw32(parthdr->freed_space_bitmap.len); 1699 if (len == 0) 1700 continue; 1701 1702 DPRINTF(VOLUMES, ("Write freed space bitmap %d\n", 1703 lb_num + ptov)); 1704 dscr = (union dscrptr *) ump->part_freed_dscr[phys_part]; 1705 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR, 1706 (union dscrptr *) dscr, 1707 ptov + lb_num, lb_num); 1708 if (error) { 1709 DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error)); 1710 error_all = error; 1711 } 1712 } 1713 1714 return error_all; 1715 } 1716 1717 1718 static int 1719 udf_read_metadata_partition_spacetable(struct udf_mount *ump) 1720 { 1721 struct udf_node *bitmap_node; 1722 union dscrptr *dscr; 1723 struct udf_bitmap *bitmap; 1724 uint64_t inflen; 1725 int error, dscr_type; 1726 1727 bitmap_node = ump->metadatabitmap_node; 1728 1729 /* only read in when metadata bitmap node is read in */ 1730 if (bitmap_node == NULL) 1731 return 0; 1732 1733 if (bitmap_node->fe) { 1734 inflen = udf_rw64(bitmap_node->fe->inf_len); 1735 } else { 1736 KASSERT(bitmap_node->efe); 1737 inflen = udf_rw64(bitmap_node->efe->inf_len); 1738 } 1739 1740 DPRINTF(VOLUMES, ("Reading metadata space bitmap for " 1741 "%"PRIu64" bytes\n", inflen)); 1742 1743 /* allocate space for bitmap */ 1744 dscr = malloc(inflen, M_UDFVOLD, M_CANFAIL | M_WAITOK); 1745 if (!dscr) 1746 return ENOMEM; 1747 1748 /* set vnode type to regular file or we can't read from it! */ 1749 bitmap_node->vnode->v_type = VREG; 1750 1751 /* read in complete metadata bitmap file */ 1752 error = vn_rdwr(UIO_READ, bitmap_node->vnode, 1753 dscr, 1754 inflen, 0, 1755 UIO_SYSSPACE, 1756 IO_SYNC | IO_ALTSEMANTICS, FSCRED, 1757 NULL, NULL); 1758 if (error) { 1759 DPRINTF(VOLUMES, ("Error reading metadata space bitmap\n")); 1760 goto errorout; 1761 } 1762 1763 /* analyse */ 1764 dscr_type = udf_rw16(dscr->tag.id); 1765 if (dscr_type == TAGID_SPACE_BITMAP) { 1766 DPRINTF(VOLUMES, ("Accepting metadata space bitmap\n")); 1767 ump->metadata_unalloc_dscr = &dscr->sbd; 1768 1769 /* fill in bitmap bits */ 1770 bitmap = &ump->metadata_unalloc_bits; 1771 bitmap->blob = (uint8_t *) dscr; 1772 bitmap->bits = dscr->sbd.data; 1773 bitmap->max_offset = udf_rw32(dscr->sbd.num_bits); 1774 bitmap->pages = NULL; /* TODO */ 1775 bitmap->data_pos = 0; 1776 bitmap->metadata_pos = 0; 1777 } else { 1778 DPRINTF(VOLUMES, ("No valid bitmap found!\n")); 1779 goto errorout; 1780 } 1781 1782 return 0; 1783 1784 errorout: 1785 free(dscr, M_UDFVOLD); 1786 printf( "UDF mount: error reading unallocated " 1787 "space bitmap for metadata partition\n"); 1788 return EROFS; 1789 } 1790 1791 1792 int 1793 udf_write_metadata_partition_spacetable(struct udf_mount *ump, int waitfor) 1794 { 1795 struct udf_node *bitmap_node; 1796 union dscrptr *dscr; 1797 uint64_t inflen, new_inflen; 1798 int dummy, error; 1799 1800 bitmap_node = ump->metadatabitmap_node; 1801 1802 /* only write out when metadata bitmap node is known */ 1803 if (bitmap_node == NULL) 1804 return 0; 1805 1806 if (bitmap_node->fe) { 1807 inflen = udf_rw64(bitmap_node->fe->inf_len); 1808 } else { 1809 KASSERT(bitmap_node->efe); 1810 inflen = udf_rw64(bitmap_node->efe->inf_len); 1811 } 1812 1813 /* reduce length to zero */ 1814 dscr = (union dscrptr *) ump->metadata_unalloc_dscr; 1815 new_inflen = udf_tagsize(dscr, 1); 1816 1817 DPRINTF(VOLUMES, ("Resize and write out metadata space bitmap from " 1818 "%"PRIu64" to %"PRIu64" bytes\n", inflen, new_inflen)); 1819 1820 error = udf_resize_node(bitmap_node, new_inflen, &dummy); 1821 if (error) 1822 printf("Error resizing metadata space bitmap\n"); 1823 1824 error = vn_rdwr(UIO_WRITE, bitmap_node->vnode, 1825 dscr, 1826 new_inflen, 0, 1827 UIO_SYSSPACE, 1828 IO_ALTSEMANTICS, FSCRED, 1829 NULL, NULL); 1830 1831 bitmap_node->i_flags |= IN_MODIFIED; 1832 vflushbuf(bitmap_node->vnode, 1 /* sync */); 1833 1834 error = VOP_FSYNC(bitmap_node->vnode, 1835 FSCRED, FSYNC_WAIT, 0, 0); 1836 1837 if (error) 1838 printf( "Error writing out metadata partition unalloced " 1839 "space bitmap!\n"); 1840 1841 return error; 1842 } 1843 1844 1845 /* --------------------------------------------------------------------- */ 1846 1847 /* 1848 * Checks if ump's vds information is correct and complete 1849 */ 1850 1851 int 1852 udf_process_vds(struct udf_mount *ump) { 1853 union udf_pmap *mapping; 1854 /* struct udf_args *args = &ump->mount_args; */ 1855 struct logvol_int_desc *lvint; 1856 struct udf_logvol_info *lvinfo; 1857 uint32_t n_pm, mt_l; 1858 uint8_t *pmap_pos; 1859 char *domain_name, *map_name; 1860 const char *check_name; 1861 char bits[128]; 1862 int pmap_stype, pmap_size; 1863 int pmap_type, log_part, phys_part, raw_phys_part, maps_on; 1864 int n_phys, n_virt, n_spar, n_meta; 1865 int len, error; 1866 1867 if (ump == NULL) 1868 return ENOENT; 1869 1870 /* we need at least an anchor (trivial, but for safety) */ 1871 if (ump->anchors[0] == NULL) 1872 return EINVAL; 1873 1874 /* we need at least one primary and one logical volume descriptor */ 1875 if ((ump->primary_vol == NULL) || (ump->logical_vol) == NULL) 1876 return EINVAL; 1877 1878 /* we need at least one partition descriptor */ 1879 if (ump->partitions[0] == NULL) 1880 return EINVAL; 1881 1882 /* check logical volume sector size verses device sector size */ 1883 if (udf_rw32(ump->logical_vol->lb_size) != ump->discinfo.sector_size) { 1884 printf("UDF mount: format violation, lb_size != sector size\n"); 1885 return EINVAL; 1886 } 1887 1888 /* check domain name */ 1889 domain_name = ump->logical_vol->domain_id.id; 1890 if (strncmp(domain_name, "*OSTA UDF Compliant", 20)) { 1891 printf("mount_udf: disc not OSTA UDF Compliant, aborting\n"); 1892 return EINVAL; 1893 } 1894 1895 /* retrieve logical volume integrity sequence */ 1896 error = udf_retrieve_lvint(ump); 1897 1898 /* 1899 * We need at least one logvol integrity descriptor recorded. Note 1900 * that its OK to have an open logical volume integrity here. The VAT 1901 * will close/update the integrity. 1902 */ 1903 if (ump->logvol_integrity == NULL) 1904 return EINVAL; 1905 1906 /* process derived structures */ 1907 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */ 1908 lvint = ump->logvol_integrity; 1909 lvinfo = (struct udf_logvol_info *) (&lvint->tables[2 * n_pm]); 1910 ump->logvol_info = lvinfo; 1911 1912 /* TODO check udf versions? */ 1913 1914 /* 1915 * check logvol mappings: effective virt->log partmap translation 1916 * check and recording of the mapping results. Saves expensive 1917 * strncmp() in tight places. 1918 */ 1919 DPRINTF(VOLUMES, ("checking logvol mappings\n")); 1920 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */ 1921 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */ 1922 pmap_pos = ump->logical_vol->maps; 1923 1924 if (n_pm > UDF_PMAPS) { 1925 printf("UDF mount: too many mappings\n"); 1926 return EINVAL; 1927 } 1928 1929 /* count types and set partition numbers */ 1930 ump->data_part = ump->node_part = ump->fids_part = 0; 1931 n_phys = n_virt = n_spar = n_meta = 0; 1932 for (log_part = 0; log_part < n_pm; log_part++) { 1933 mapping = (union udf_pmap *) pmap_pos; 1934 pmap_stype = pmap_pos[0]; 1935 pmap_size = pmap_pos[1]; 1936 switch (pmap_stype) { 1937 case 1: /* physical mapping */ 1938 /* volseq = udf_rw16(mapping->pm1.vol_seq_num); */ 1939 raw_phys_part = udf_rw16(mapping->pm1.part_num); 1940 pmap_type = UDF_VTOP_TYPE_PHYS; 1941 n_phys++; 1942 ump->data_part = log_part; 1943 ump->node_part = log_part; 1944 ump->fids_part = log_part; 1945 break; 1946 case 2: /* virtual/sparable/meta mapping */ 1947 map_name = mapping->pm2.part_id.id; 1948 /* volseq = udf_rw16(mapping->pm2.vol_seq_num); */ 1949 raw_phys_part = udf_rw16(mapping->pm2.part_num); 1950 pmap_type = UDF_VTOP_TYPE_UNKNOWN; 1951 len = UDF_REGID_ID_SIZE; 1952 1953 check_name = "*UDF Virtual Partition"; 1954 if (strncmp(map_name, check_name, len) == 0) { 1955 pmap_type = UDF_VTOP_TYPE_VIRT; 1956 n_virt++; 1957 ump->node_part = log_part; 1958 break; 1959 } 1960 check_name = "*UDF Sparable Partition"; 1961 if (strncmp(map_name, check_name, len) == 0) { 1962 pmap_type = UDF_VTOP_TYPE_SPARABLE; 1963 n_spar++; 1964 ump->data_part = log_part; 1965 ump->node_part = log_part; 1966 ump->fids_part = log_part; 1967 break; 1968 } 1969 check_name = "*UDF Metadata Partition"; 1970 if (strncmp(map_name, check_name, len) == 0) { 1971 pmap_type = UDF_VTOP_TYPE_META; 1972 n_meta++; 1973 ump->node_part = log_part; 1974 ump->fids_part = log_part; 1975 break; 1976 } 1977 break; 1978 default: 1979 return EINVAL; 1980 } 1981 1982 /* 1983 * BUGALERT: some rogue implementations use random physical 1984 * partition numbers to break other implementations so lookup 1985 * the number. 1986 */ 1987 phys_part = udf_find_raw_phys(ump, raw_phys_part); 1988 1989 DPRINTF(VOLUMES, ("\t%d -> %d(%d) type %d\n", log_part, 1990 raw_phys_part, phys_part, pmap_type)); 1991 1992 if (phys_part == UDF_PARTITIONS) 1993 return EINVAL; 1994 if (pmap_type == UDF_VTOP_TYPE_UNKNOWN) 1995 return EINVAL; 1996 1997 ump->vtop [log_part] = phys_part; 1998 ump->vtop_tp[log_part] = pmap_type; 1999 2000 pmap_pos += pmap_size; 2001 } 2002 /* not winning the beauty contest */ 2003 ump->vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW; 2004 2005 /* test some basic UDF assertions/requirements */ 2006 if ((n_virt > 1) || (n_spar > 1) || (n_meta > 1)) 2007 return EINVAL; 2008 2009 if (n_virt) { 2010 if ((n_phys == 0) || n_spar || n_meta) 2011 return EINVAL; 2012 } 2013 if (n_spar + n_phys == 0) 2014 return EINVAL; 2015 2016 /* select allocation type for each logical partition */ 2017 for (log_part = 0; log_part < n_pm; log_part++) { 2018 maps_on = ump->vtop[log_part]; 2019 switch (ump->vtop_tp[log_part]) { 2020 case UDF_VTOP_TYPE_PHYS : 2021 assert(maps_on == log_part); 2022 ump->vtop_alloc[log_part] = UDF_ALLOC_SPACEMAP; 2023 break; 2024 case UDF_VTOP_TYPE_VIRT : 2025 ump->vtop_alloc[log_part] = UDF_ALLOC_VAT; 2026 ump->vtop_alloc[maps_on] = UDF_ALLOC_SEQUENTIAL; 2027 break; 2028 case UDF_VTOP_TYPE_SPARABLE : 2029 assert(maps_on == log_part); 2030 ump->vtop_alloc[log_part] = UDF_ALLOC_SPACEMAP; 2031 break; 2032 case UDF_VTOP_TYPE_META : 2033 ump->vtop_alloc[log_part] = UDF_ALLOC_METABITMAP; 2034 if (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) { 2035 /* special case for UDF 2.60 */ 2036 ump->vtop_alloc[log_part] = UDF_ALLOC_METASEQUENTIAL; 2037 ump->vtop_alloc[maps_on] = UDF_ALLOC_SEQUENTIAL; 2038 } 2039 break; 2040 default: 2041 panic("bad alloction type in udf's ump->vtop\n"); 2042 } 2043 } 2044 2045 /* determine logical volume open/closure actions */ 2046 if (n_virt) { 2047 ump->lvopen = 0; 2048 if (ump->discinfo.last_session_state == MMC_STATE_EMPTY) 2049 ump->lvopen |= UDF_OPEN_SESSION ; 2050 ump->lvclose = UDF_WRITE_VAT; 2051 if (ump->mount_args.udfmflags & UDFMNT_CLOSESESSION) 2052 ump->lvclose |= UDF_CLOSE_SESSION; 2053 } else { 2054 /* `normal' rewritable or non sequential media */ 2055 ump->lvopen = UDF_WRITE_LVINT; 2056 ump->lvclose = UDF_WRITE_LVINT; 2057 if ((ump->discinfo.mmc_cur & MMC_CAP_REWRITABLE) == 0) 2058 ump->lvopen |= UDF_APPENDONLY_LVINT; 2059 if ((ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE)) 2060 ump->lvopen &= ~UDF_APPENDONLY_LVINT; 2061 } 2062 2063 /* 2064 * Determine sheduler error behaviour. For virtual partitions, update 2065 * the trackinfo; for sparable partitions replace a whole block on the 2066 * sparable table. Allways requeue. 2067 */ 2068 ump->lvreadwrite = 0; 2069 if (n_virt) 2070 ump->lvreadwrite = UDF_UPDATE_TRACKINFO; 2071 if (n_spar) 2072 ump->lvreadwrite = UDF_REMAP_BLOCK; 2073 2074 /* 2075 * Select our sheduler 2076 */ 2077 ump->strategy = &udf_strat_rmw; 2078 if (n_virt || (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE)) 2079 ump->strategy = &udf_strat_sequential; 2080 if ((ump->discinfo.mmc_class == MMC_CLASS_DISC) || 2081 (ump->discinfo.mmc_class == MMC_CLASS_UNKN)) 2082 ump->strategy = &udf_strat_direct; 2083 if (n_spar) 2084 ump->strategy = &udf_strat_rmw; 2085 2086 #if 0 2087 /* read-only access won't benefit from the other shedulers */ 2088 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) 2089 ump->strategy = &udf_strat_direct; 2090 #endif 2091 2092 /* print results */ 2093 DPRINTF(VOLUMES, ("\tdata partition %d\n", ump->data_part)); 2094 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->data_part])); 2095 DPRINTF(VOLUMES, ("\tnode partition %d\n", ump->node_part)); 2096 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->node_part])); 2097 DPRINTF(VOLUMES, ("\tfids partition %d\n", ump->fids_part)); 2098 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->fids_part])); 2099 2100 snprintb(bits, sizeof(bits), UDFLOGVOL_BITS, ump->lvopen); 2101 DPRINTF(VOLUMES, ("\tactions on logvol open %s\n", bits)); 2102 snprintb(bits, sizeof(bits), UDFLOGVOL_BITS, ump->lvclose); 2103 DPRINTF(VOLUMES, ("\tactions on logvol close %s\n", bits)); 2104 snprintb(bits, sizeof(bits), UDFONERROR_BITS, ump->lvreadwrite); 2105 DPRINTF(VOLUMES, ("\tactions on logvol errors %s\n", bits)); 2106 2107 DPRINTF(VOLUMES, ("\tselected sheduler `%s`\n", 2108 (ump->strategy == &udf_strat_direct) ? "Direct" : 2109 (ump->strategy == &udf_strat_sequential) ? "Sequential" : 2110 (ump->strategy == &udf_strat_rmw) ? "RMW" : "UNKNOWN!")); 2111 2112 /* signal its OK for now */ 2113 return 0; 2114 } 2115 2116 /* --------------------------------------------------------------------- */ 2117 2118 /* 2119 * Update logical volume name in all structures that keep a record of it. We 2120 * use memmove since each of them might be specified as a source. 2121 * 2122 * Note that it doesn't update the VAT structure! 2123 */ 2124 2125 static void 2126 udf_update_logvolname(struct udf_mount *ump, char *logvol_id) 2127 { 2128 struct logvol_desc *lvd = NULL; 2129 struct fileset_desc *fsd = NULL; 2130 struct udf_lv_info *lvi = NULL; 2131 2132 DPRINTF(VOLUMES, ("Updating logical volume name\n")); 2133 lvd = ump->logical_vol; 2134 fsd = ump->fileset_desc; 2135 if (ump->implementation) 2136 lvi = &ump->implementation->_impl_use.lv_info; 2137 2138 /* logvol's id might be specified as origional so use memmove here */ 2139 memmove(lvd->logvol_id, logvol_id, 128); 2140 if (fsd) 2141 memmove(fsd->logvol_id, logvol_id, 128); 2142 if (lvi) 2143 memmove(lvi->logvol_id, logvol_id, 128); 2144 } 2145 2146 /* --------------------------------------------------------------------- */ 2147 2148 void 2149 udf_inittag(struct udf_mount *ump, struct desc_tag *tag, int tagid, 2150 uint32_t sector) 2151 { 2152 assert(ump->logical_vol); 2153 2154 tag->id = udf_rw16(tagid); 2155 tag->descriptor_ver = ump->logical_vol->tag.descriptor_ver; 2156 tag->cksum = 0; 2157 tag->reserved = 0; 2158 tag->serial_num = ump->logical_vol->tag.serial_num; 2159 tag->tag_loc = udf_rw32(sector); 2160 } 2161 2162 2163 uint64_t 2164 udf_advance_uniqueid(struct udf_mount *ump) 2165 { 2166 uint64_t unique_id; 2167 2168 mutex_enter(&ump->logvol_mutex); 2169 unique_id = udf_rw64(ump->logvol_integrity->lvint_next_unique_id); 2170 if (unique_id < 0x10) 2171 unique_id = 0x10; 2172 ump->logvol_integrity->lvint_next_unique_id = udf_rw64(unique_id + 1); 2173 mutex_exit(&ump->logvol_mutex); 2174 2175 return unique_id; 2176 } 2177 2178 2179 static void 2180 udf_adjust_filecount(struct udf_node *udf_node, int sign) 2181 { 2182 struct udf_mount *ump = udf_node->ump; 2183 uint32_t num_dirs, num_files; 2184 int udf_file_type; 2185 2186 /* get file type */ 2187 if (udf_node->fe) { 2188 udf_file_type = udf_node->fe->icbtag.file_type; 2189 } else { 2190 udf_file_type = udf_node->efe->icbtag.file_type; 2191 } 2192 2193 /* adjust file count */ 2194 mutex_enter(&ump->allocate_mutex); 2195 if (udf_file_type == UDF_ICB_FILETYPE_DIRECTORY) { 2196 num_dirs = udf_rw32(ump->logvol_info->num_directories); 2197 ump->logvol_info->num_directories = 2198 udf_rw32((num_dirs + sign)); 2199 } else { 2200 num_files = udf_rw32(ump->logvol_info->num_files); 2201 ump->logvol_info->num_files = 2202 udf_rw32((num_files + sign)); 2203 } 2204 mutex_exit(&ump->allocate_mutex); 2205 } 2206 2207 2208 void 2209 udf_osta_charset(struct charspec *charspec) 2210 { 2211 memset(charspec, 0, sizeof(struct charspec)); 2212 charspec->type = 0; 2213 strcpy((char *) charspec->inf, "OSTA Compressed Unicode"); 2214 } 2215 2216 2217 /* first call udf_set_regid and then the suffix */ 2218 void 2219 udf_set_regid(struct regid *regid, char const *name) 2220 { 2221 memset(regid, 0, sizeof(struct regid)); 2222 regid->flags = 0; /* not dirty and not protected */ 2223 strcpy((char *) regid->id, name); 2224 } 2225 2226 2227 void 2228 udf_add_domain_regid(struct udf_mount *ump, struct regid *regid) 2229 { 2230 uint16_t *ver; 2231 2232 ver = (uint16_t *) regid->id_suffix; 2233 *ver = ump->logvol_info->min_udf_readver; 2234 } 2235 2236 2237 void 2238 udf_add_udf_regid(struct udf_mount *ump, struct regid *regid) 2239 { 2240 uint16_t *ver; 2241 2242 ver = (uint16_t *) regid->id_suffix; 2243 *ver = ump->logvol_info->min_udf_readver; 2244 2245 regid->id_suffix[2] = 4; /* unix */ 2246 regid->id_suffix[3] = 8; /* NetBSD */ 2247 } 2248 2249 2250 void 2251 udf_add_impl_regid(struct udf_mount *ump, struct regid *regid) 2252 { 2253 regid->id_suffix[0] = 4; /* unix */ 2254 regid->id_suffix[1] = 8; /* NetBSD */ 2255 } 2256 2257 2258 void 2259 udf_add_app_regid(struct udf_mount *ump, struct regid *regid) 2260 { 2261 regid->id_suffix[0] = APP_VERSION_MAIN; 2262 regid->id_suffix[1] = APP_VERSION_SUB; 2263 } 2264 2265 static int 2266 udf_create_parentfid(struct udf_mount *ump, struct fileid_desc *fid, 2267 struct long_ad *parent, uint64_t unique_id) 2268 { 2269 /* the size of an empty FID is 38 but needs to be a multiple of 4 */ 2270 int fidsize = 40; 2271 2272 udf_inittag(ump, &fid->tag, TAGID_FID, udf_rw32(parent->loc.lb_num)); 2273 fid->file_version_num = udf_rw16(1); /* UDF 2.3.4.1 */ 2274 fid->file_char = UDF_FILE_CHAR_DIR | UDF_FILE_CHAR_PAR; 2275 fid->icb = *parent; 2276 fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id); 2277 fid->tag.desc_crc_len = udf_rw16(fidsize - UDF_DESC_TAG_LENGTH); 2278 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid); 2279 2280 return fidsize; 2281 } 2282 2283 /* --------------------------------------------------------------------- */ 2284 2285 /* 2286 * Extended attribute support. UDF knows of 3 places for extended attributes: 2287 * 2288 * (a) inside the file's (e)fe in the length of the extended attribute area 2289 * before the allocation descriptors/filedata 2290 * 2291 * (b) in a file referenced by (e)fe->ext_attr_icb and 2292 * 2293 * (c) in the e(fe)'s associated stream directory that can hold various 2294 * sub-files. In the stream directory a few fixed named subfiles are reserved 2295 * for NT/Unix ACL's and OS/2 attributes. 2296 * 2297 * NOTE: Extended attributes are read randomly but allways written 2298 * *atomicaly*. For ACL's this interface is propably different but not known 2299 * to me yet. 2300 * 2301 * Order of extended attributes in a space : 2302 * ECMA 167 EAs 2303 * Non block aligned Implementation Use EAs 2304 * Block aligned Implementation Use EAs 2305 * Application Use EAs 2306 */ 2307 2308 static int 2309 udf_impl_extattr_check(struct impl_extattr_entry *implext) 2310 { 2311 uint16_t *spos; 2312 2313 if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) { 2314 /* checksum valid? */ 2315 DPRINTF(EXTATTR, ("checking UDF impl. attr checksum\n")); 2316 spos = (uint16_t *) implext->data; 2317 if (udf_rw16(*spos) != udf_ea_cksum((uint8_t *) implext)) 2318 return EINVAL; 2319 } 2320 return 0; 2321 } 2322 2323 static void 2324 udf_calc_impl_extattr_checksum(struct impl_extattr_entry *implext) 2325 { 2326 uint16_t *spos; 2327 2328 if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) { 2329 /* set checksum */ 2330 spos = (uint16_t *) implext->data; 2331 *spos = udf_rw16(udf_ea_cksum((uint8_t *) implext)); 2332 } 2333 } 2334 2335 2336 int 2337 udf_extattr_search_intern(struct udf_node *node, 2338 uint32_t sattr, char const *sattrname, 2339 uint32_t *offsetp, uint32_t *lengthp) 2340 { 2341 struct extattrhdr_desc *eahdr; 2342 struct extattr_entry *attrhdr; 2343 struct impl_extattr_entry *implext; 2344 uint32_t offset, a_l, sector_size; 2345 int32_t l_ea; 2346 uint8_t *pos; 2347 int error; 2348 2349 /* get mountpoint */ 2350 sector_size = node->ump->discinfo.sector_size; 2351 2352 /* get information from fe/efe */ 2353 if (node->fe) { 2354 l_ea = udf_rw32(node->fe->l_ea); 2355 eahdr = (struct extattrhdr_desc *) node->fe->data; 2356 } else { 2357 assert(node->efe); 2358 l_ea = udf_rw32(node->efe->l_ea); 2359 eahdr = (struct extattrhdr_desc *) node->efe->data; 2360 } 2361 2362 /* something recorded here? */ 2363 if (l_ea == 0) 2364 return ENOENT; 2365 2366 /* check extended attribute tag; what to do if it fails? */ 2367 error = udf_check_tag(eahdr); 2368 if (error) 2369 return EINVAL; 2370 if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR) 2371 return EINVAL; 2372 error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc)); 2373 if (error) 2374 return EINVAL; 2375 2376 DPRINTF(EXTATTR, ("Found %d bytes of extended attributes\n", l_ea)); 2377 2378 /* looking for Ecma-167 attributes? */ 2379 offset = sizeof(struct extattrhdr_desc); 2380 2381 /* looking for either implemenation use or application use */ 2382 if (sattr == 2048) { /* [4/48.10.8] */ 2383 offset = udf_rw32(eahdr->impl_attr_loc); 2384 if (offset == UDF_IMPL_ATTR_LOC_NOT_PRESENT) 2385 return ENOENT; 2386 } 2387 if (sattr == 65536) { /* [4/48.10.9] */ 2388 offset = udf_rw32(eahdr->appl_attr_loc); 2389 if (offset == UDF_APPL_ATTR_LOC_NOT_PRESENT) 2390 return ENOENT; 2391 } 2392 2393 /* paranoia check offset and l_ea */ 2394 if (l_ea + offset >= sector_size - sizeof(struct extattr_entry)) 2395 return EINVAL; 2396 2397 DPRINTF(EXTATTR, ("Starting at offset %d\n", offset)); 2398 2399 /* find our extended attribute */ 2400 l_ea -= offset; 2401 pos = (uint8_t *) eahdr + offset; 2402 2403 while (l_ea >= sizeof(struct extattr_entry)) { 2404 DPRINTF(EXTATTR, ("%d extended attr bytes left\n", l_ea)); 2405 attrhdr = (struct extattr_entry *) pos; 2406 implext = (struct impl_extattr_entry *) pos; 2407 2408 /* get complete attribute length and check for roque values */ 2409 a_l = udf_rw32(attrhdr->a_l); 2410 DPRINTF(EXTATTR, ("attribute %d:%d, len %d/%d\n", 2411 udf_rw32(attrhdr->type), 2412 attrhdr->subtype, a_l, l_ea)); 2413 if ((a_l == 0) || (a_l > l_ea)) 2414 return EINVAL; 2415 2416 if (attrhdr->type != sattr) 2417 goto next_attribute; 2418 2419 /* we might have found it! */ 2420 if (attrhdr->type < 2048) { /* Ecma-167 attribute */ 2421 *offsetp = offset; 2422 *lengthp = a_l; 2423 return 0; /* success */ 2424 } 2425 2426 /* 2427 * Implementation use and application use extended attributes 2428 * have a name to identify. They share the same structure only 2429 * UDF implementation use extended attributes have a checksum 2430 * we need to check 2431 */ 2432 2433 DPRINTF(EXTATTR, ("named attribute %s\n", implext->imp_id.id)); 2434 if (strcmp(implext->imp_id.id, sattrname) == 0) { 2435 /* we have found our appl/implementation attribute */ 2436 *offsetp = offset; 2437 *lengthp = a_l; 2438 return 0; /* success */ 2439 } 2440 2441 next_attribute: 2442 /* next attribute */ 2443 pos += a_l; 2444 l_ea -= a_l; 2445 offset += a_l; 2446 } 2447 /* not found */ 2448 return ENOENT; 2449 } 2450 2451 2452 static void 2453 udf_extattr_insert_internal(struct udf_mount *ump, union dscrptr *dscr, 2454 struct extattr_entry *extattr) 2455 { 2456 struct file_entry *fe; 2457 struct extfile_entry *efe; 2458 struct extattrhdr_desc *extattrhdr; 2459 struct impl_extattr_entry *implext; 2460 uint32_t impl_attr_loc, appl_attr_loc, l_ea, a_l, exthdr_len; 2461 uint32_t *l_eap, l_ad; 2462 uint16_t *spos; 2463 uint8_t *bpos, *data; 2464 2465 if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) { 2466 fe = &dscr->fe; 2467 data = fe->data; 2468 l_eap = &fe->l_ea; 2469 l_ad = udf_rw32(fe->l_ad); 2470 } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) { 2471 efe = &dscr->efe; 2472 data = efe->data; 2473 l_eap = &efe->l_ea; 2474 l_ad = udf_rw32(efe->l_ad); 2475 } else { 2476 panic("Bad tag passed to udf_extattr_insert_internal"); 2477 } 2478 2479 /* can't append already written to file descriptors yet */ 2480 assert(l_ad == 0); 2481 2482 /* should have a header! */ 2483 extattrhdr = (struct extattrhdr_desc *) data; 2484 l_ea = udf_rw32(*l_eap); 2485 if (l_ea == 0) { 2486 /* create empty extended attribute header */ 2487 exthdr_len = sizeof(struct extattrhdr_desc); 2488 2489 udf_inittag(ump, &extattrhdr->tag, TAGID_EXTATTR_HDR, 2490 /* loc */ 0); 2491 extattrhdr->impl_attr_loc = udf_rw32(exthdr_len); 2492 extattrhdr->appl_attr_loc = udf_rw32(exthdr_len); 2493 extattrhdr->tag.desc_crc_len = udf_rw16(8); 2494 2495 /* record extended attribute header length */ 2496 l_ea = exthdr_len; 2497 *l_eap = udf_rw32(l_ea); 2498 } 2499 2500 /* extract locations */ 2501 impl_attr_loc = udf_rw32(extattrhdr->impl_attr_loc); 2502 appl_attr_loc = udf_rw32(extattrhdr->appl_attr_loc); 2503 if (impl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT) 2504 impl_attr_loc = l_ea; 2505 if (appl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT) 2506 appl_attr_loc = l_ea; 2507 2508 /* Ecma 167 EAs */ 2509 if (udf_rw32(extattr->type) < 2048) { 2510 assert(impl_attr_loc == l_ea); 2511 assert(appl_attr_loc == l_ea); 2512 } 2513 2514 /* implementation use extended attributes */ 2515 if (udf_rw32(extattr->type) == 2048) { 2516 assert(appl_attr_loc == l_ea); 2517 2518 /* calculate and write extended attribute header checksum */ 2519 implext = (struct impl_extattr_entry *) extattr; 2520 assert(udf_rw32(implext->iu_l) == 4); /* [UDF 3.3.4.5] */ 2521 spos = (uint16_t *) implext->data; 2522 *spos = udf_rw16(udf_ea_cksum((uint8_t *) implext)); 2523 } 2524 2525 /* application use extended attributes */ 2526 assert(udf_rw32(extattr->type) != 65536); 2527 assert(appl_attr_loc == l_ea); 2528 2529 /* append the attribute at the end of the current space */ 2530 bpos = data + udf_rw32(*l_eap); 2531 a_l = udf_rw32(extattr->a_l); 2532 2533 /* update impl. attribute locations */ 2534 if (udf_rw32(extattr->type) < 2048) { 2535 impl_attr_loc = l_ea + a_l; 2536 appl_attr_loc = l_ea + a_l; 2537 } 2538 if (udf_rw32(extattr->type) == 2048) { 2539 appl_attr_loc = l_ea + a_l; 2540 } 2541 2542 /* copy and advance */ 2543 memcpy(bpos, extattr, a_l); 2544 l_ea += a_l; 2545 *l_eap = udf_rw32(l_ea); 2546 2547 /* do the `dance` again backwards */ 2548 if (udf_rw16(ump->logical_vol->tag.descriptor_ver) != 2) { 2549 if (impl_attr_loc == l_ea) 2550 impl_attr_loc = UDF_IMPL_ATTR_LOC_NOT_PRESENT; 2551 if (appl_attr_loc == l_ea) 2552 appl_attr_loc = UDF_APPL_ATTR_LOC_NOT_PRESENT; 2553 } 2554 2555 /* store offsets */ 2556 extattrhdr->impl_attr_loc = udf_rw32(impl_attr_loc); 2557 extattrhdr->appl_attr_loc = udf_rw32(appl_attr_loc); 2558 } 2559 2560 2561 /* --------------------------------------------------------------------- */ 2562 2563 static int 2564 udf_update_lvid_from_vat_extattr(struct udf_node *vat_node) 2565 { 2566 struct udf_mount *ump; 2567 struct udf_logvol_info *lvinfo; 2568 struct impl_extattr_entry *implext; 2569 struct vatlvext_extattr_entry lvext; 2570 const char *extstr = "*UDF VAT LVExtension"; 2571 uint64_t vat_uniqueid; 2572 uint32_t offset, a_l; 2573 uint8_t *ea_start, *lvextpos; 2574 int error; 2575 2576 /* get mountpoint and lvinfo */ 2577 ump = vat_node->ump; 2578 lvinfo = ump->logvol_info; 2579 2580 /* get information from fe/efe */ 2581 if (vat_node->fe) { 2582 vat_uniqueid = udf_rw64(vat_node->fe->unique_id); 2583 ea_start = vat_node->fe->data; 2584 } else { 2585 vat_uniqueid = udf_rw64(vat_node->efe->unique_id); 2586 ea_start = vat_node->efe->data; 2587 } 2588 2589 error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l); 2590 if (error) 2591 return error; 2592 2593 implext = (struct impl_extattr_entry *) (ea_start + offset); 2594 error = udf_impl_extattr_check(implext); 2595 if (error) 2596 return error; 2597 2598 /* paranoia */ 2599 if (a_l != sizeof(*implext) -1 + udf_rw32(implext->iu_l) + sizeof(lvext)) { 2600 DPRINTF(VOLUMES, ("VAT LVExtension size doesn't compute\n")); 2601 return EINVAL; 2602 } 2603 2604 /* 2605 * we have found our "VAT LVExtension attribute. BUT due to a 2606 * bug in the specification it might not be word aligned so 2607 * copy first to avoid panics on some machines (!!) 2608 */ 2609 DPRINTF(VOLUMES, ("Found VAT LVExtension attr\n")); 2610 lvextpos = implext->data + udf_rw32(implext->iu_l); 2611 memcpy(&lvext, lvextpos, sizeof(lvext)); 2612 2613 /* check if it was updated the last time */ 2614 if (udf_rw64(lvext.unique_id_chk) == vat_uniqueid) { 2615 lvinfo->num_files = lvext.num_files; 2616 lvinfo->num_directories = lvext.num_directories; 2617 udf_update_logvolname(ump, lvext.logvol_id); 2618 } else { 2619 DPRINTF(VOLUMES, ("VAT LVExtension out of date\n")); 2620 /* replace VAT LVExt by free space EA */ 2621 memset(implext->imp_id.id, 0, UDF_REGID_ID_SIZE); 2622 strcpy(implext->imp_id.id, "*UDF FreeEASpace"); 2623 udf_calc_impl_extattr_checksum(implext); 2624 } 2625 2626 return 0; 2627 } 2628 2629 2630 static int 2631 udf_update_vat_extattr_from_lvid(struct udf_node *vat_node) 2632 { 2633 struct udf_mount *ump; 2634 struct udf_logvol_info *lvinfo; 2635 struct impl_extattr_entry *implext; 2636 struct vatlvext_extattr_entry lvext; 2637 const char *extstr = "*UDF VAT LVExtension"; 2638 uint64_t vat_uniqueid; 2639 uint32_t offset, a_l; 2640 uint8_t *ea_start, *lvextpos; 2641 int error; 2642 2643 /* get mountpoint and lvinfo */ 2644 ump = vat_node->ump; 2645 lvinfo = ump->logvol_info; 2646 2647 /* get information from fe/efe */ 2648 if (vat_node->fe) { 2649 vat_uniqueid = udf_rw64(vat_node->fe->unique_id); 2650 ea_start = vat_node->fe->data; 2651 } else { 2652 vat_uniqueid = udf_rw64(vat_node->efe->unique_id); 2653 ea_start = vat_node->efe->data; 2654 } 2655 2656 error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l); 2657 if (error) 2658 return error; 2659 /* found, it existed */ 2660 2661 /* paranoia */ 2662 implext = (struct impl_extattr_entry *) (ea_start + offset); 2663 error = udf_impl_extattr_check(implext); 2664 if (error) { 2665 DPRINTF(VOLUMES, ("VAT LVExtension bad on update\n")); 2666 return error; 2667 } 2668 /* it is correct */ 2669 2670 /* 2671 * we have found our "VAT LVExtension attribute. BUT due to a 2672 * bug in the specification it might not be word aligned so 2673 * copy first to avoid panics on some machines (!!) 2674 */ 2675 DPRINTF(VOLUMES, ("Updating VAT LVExtension attr\n")); 2676 lvextpos = implext->data + udf_rw32(implext->iu_l); 2677 2678 lvext.unique_id_chk = vat_uniqueid; 2679 lvext.num_files = lvinfo->num_files; 2680 lvext.num_directories = lvinfo->num_directories; 2681 memmove(lvext.logvol_id, ump->logical_vol->logvol_id, 128); 2682 2683 memcpy(lvextpos, &lvext, sizeof(lvext)); 2684 2685 return 0; 2686 } 2687 2688 /* --------------------------------------------------------------------- */ 2689 2690 int 2691 udf_vat_read(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset) 2692 { 2693 struct udf_mount *ump = vat_node->ump; 2694 2695 if (offset + size > ump->vat_offset + ump->vat_entries * 4) 2696 return EINVAL; 2697 2698 memcpy(blob, ump->vat_table + offset, size); 2699 return 0; 2700 } 2701 2702 int 2703 udf_vat_write(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset) 2704 { 2705 struct udf_mount *ump = vat_node->ump; 2706 uint32_t offset_high; 2707 uint8_t *new_vat_table; 2708 2709 /* extent VAT allocation if needed */ 2710 offset_high = offset + size; 2711 if (offset_high >= ump->vat_table_alloc_len) { 2712 /* realloc */ 2713 new_vat_table = realloc(ump->vat_table, 2714 ump->vat_table_alloc_len + UDF_VAT_CHUNKSIZE, 2715 M_UDFVOLD, M_WAITOK | M_CANFAIL); 2716 if (!new_vat_table) { 2717 printf("udf_vat_write: can't extent VAT, out of mem\n"); 2718 return ENOMEM; 2719 } 2720 ump->vat_table = new_vat_table; 2721 ump->vat_table_alloc_len += UDF_VAT_CHUNKSIZE; 2722 } 2723 ump->vat_table_len = MAX(ump->vat_table_len, offset_high); 2724 2725 memcpy(ump->vat_table + offset, blob, size); 2726 return 0; 2727 } 2728 2729 /* --------------------------------------------------------------------- */ 2730 2731 /* TODO support previous VAT location writeout */ 2732 static int 2733 udf_update_vat_descriptor(struct udf_mount *ump) 2734 { 2735 struct udf_node *vat_node = ump->vat_node; 2736 struct udf_logvol_info *lvinfo = ump->logvol_info; 2737 struct icb_tag *icbtag; 2738 struct udf_oldvat_tail *oldvat_tl; 2739 struct udf_vat *vat; 2740 uint64_t unique_id; 2741 uint32_t lb_size; 2742 uint8_t *raw_vat; 2743 int filetype, error; 2744 2745 KASSERT(vat_node); 2746 KASSERT(lvinfo); 2747 lb_size = udf_rw32(ump->logical_vol->lb_size); 2748 2749 /* get our new unique_id */ 2750 unique_id = udf_advance_uniqueid(ump); 2751 2752 /* get information from fe/efe */ 2753 if (vat_node->fe) { 2754 icbtag = &vat_node->fe->icbtag; 2755 vat_node->fe->unique_id = udf_rw64(unique_id); 2756 } else { 2757 icbtag = &vat_node->efe->icbtag; 2758 vat_node->efe->unique_id = udf_rw64(unique_id); 2759 } 2760 2761 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */ 2762 filetype = icbtag->file_type; 2763 KASSERT((filetype == 0) || (filetype == UDF_ICB_FILETYPE_VAT)); 2764 2765 /* allocate piece to process head or tail of VAT file */ 2766 raw_vat = malloc(lb_size, M_TEMP, M_WAITOK); 2767 2768 if (filetype == 0) { 2769 /* 2770 * Update "*UDF VAT LVExtension" extended attribute from the 2771 * lvint if present. 2772 */ 2773 udf_update_vat_extattr_from_lvid(vat_node); 2774 2775 /* setup identifying regid */ 2776 oldvat_tl = (struct udf_oldvat_tail *) raw_vat; 2777 memset(oldvat_tl, 0, sizeof(struct udf_oldvat_tail)); 2778 2779 udf_set_regid(&oldvat_tl->id, "*UDF Virtual Alloc Tbl"); 2780 udf_add_udf_regid(ump, &oldvat_tl->id); 2781 oldvat_tl->prev_vat = udf_rw32(0xffffffff); 2782 2783 /* write out new tail of virtual allocation table file */ 2784 error = udf_vat_write(vat_node, raw_vat, 2785 sizeof(struct udf_oldvat_tail), ump->vat_entries * 4); 2786 } else { 2787 /* compose the VAT2 header */ 2788 vat = (struct udf_vat *) raw_vat; 2789 memset(vat, 0, sizeof(struct udf_vat)); 2790 2791 vat->header_len = udf_rw16(152); /* as per spec */ 2792 vat->impl_use_len = udf_rw16(0); 2793 memmove(vat->logvol_id, ump->logical_vol->logvol_id, 128); 2794 vat->prev_vat = udf_rw32(0xffffffff); 2795 vat->num_files = lvinfo->num_files; 2796 vat->num_directories = lvinfo->num_directories; 2797 vat->min_udf_readver = lvinfo->min_udf_readver; 2798 vat->min_udf_writever = lvinfo->min_udf_writever; 2799 vat->max_udf_writever = lvinfo->max_udf_writever; 2800 2801 error = udf_vat_write(vat_node, raw_vat, 2802 sizeof(struct udf_vat), 0); 2803 } 2804 free(raw_vat, M_TEMP); 2805 2806 return error; /* success! */ 2807 } 2808 2809 2810 int 2811 udf_writeout_vat(struct udf_mount *ump) 2812 { 2813 struct udf_node *vat_node = ump->vat_node; 2814 uint32_t vat_length; 2815 int error; 2816 2817 KASSERT(vat_node); 2818 2819 DPRINTF(CALL, ("udf_writeout_vat\n")); 2820 2821 // mutex_enter(&ump->allocate_mutex); 2822 udf_update_vat_descriptor(ump); 2823 2824 /* write out the VAT contents ; TODO intelligent writing */ 2825 vat_length = ump->vat_table_len; 2826 error = vn_rdwr(UIO_WRITE, vat_node->vnode, 2827 ump->vat_table, ump->vat_table_len, 0, 2828 UIO_SYSSPACE, 0, FSCRED, NULL, NULL); 2829 if (error) { 2830 printf("udf_writeout_vat: failed to write out VAT contents\n"); 2831 goto out; 2832 } 2833 2834 // mutex_exit(&ump->allocate_mutex); 2835 2836 vflushbuf(ump->vat_node->vnode, 1 /* sync */); 2837 error = VOP_FSYNC(ump->vat_node->vnode, 2838 FSCRED, FSYNC_WAIT, 0, 0); 2839 if (error) 2840 printf("udf_writeout_vat: error writing VAT node!\n"); 2841 out: 2842 2843 return error; 2844 } 2845 2846 /* --------------------------------------------------------------------- */ 2847 2848 /* 2849 * Read in relevant pieces of VAT file and check if its indeed a VAT file 2850 * descriptor. If OK, read in complete VAT file. 2851 */ 2852 2853 static int 2854 udf_check_for_vat(struct udf_node *vat_node) 2855 { 2856 struct udf_mount *ump; 2857 struct icb_tag *icbtag; 2858 struct timestamp *mtime; 2859 struct udf_vat *vat; 2860 struct udf_oldvat_tail *oldvat_tl; 2861 struct udf_logvol_info *lvinfo; 2862 uint64_t unique_id; 2863 uint32_t vat_length; 2864 uint32_t vat_offset, vat_entries, vat_table_alloc_len; 2865 uint32_t sector_size; 2866 uint32_t *raw_vat; 2867 uint8_t *vat_table; 2868 char *regid_name; 2869 int filetype; 2870 int error; 2871 2872 /* vat_length is really 64 bits though impossible */ 2873 2874 DPRINTF(VOLUMES, ("Checking for VAT\n")); 2875 if (!vat_node) 2876 return ENOENT; 2877 2878 /* get mount info */ 2879 ump = vat_node->ump; 2880 sector_size = udf_rw32(ump->logical_vol->lb_size); 2881 2882 /* check assertions */ 2883 assert(vat_node->fe || vat_node->efe); 2884 assert(ump->logvol_integrity); 2885 2886 /* set vnode type to regular file or we can't read from it! */ 2887 vat_node->vnode->v_type = VREG; 2888 2889 /* get information from fe/efe */ 2890 if (vat_node->fe) { 2891 vat_length = udf_rw64(vat_node->fe->inf_len); 2892 icbtag = &vat_node->fe->icbtag; 2893 mtime = &vat_node->fe->mtime; 2894 unique_id = udf_rw64(vat_node->fe->unique_id); 2895 } else { 2896 vat_length = udf_rw64(vat_node->efe->inf_len); 2897 icbtag = &vat_node->efe->icbtag; 2898 mtime = &vat_node->efe->mtime; 2899 unique_id = udf_rw64(vat_node->efe->unique_id); 2900 } 2901 2902 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */ 2903 filetype = icbtag->file_type; 2904 if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT)) 2905 return ENOENT; 2906 2907 DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length)); 2908 2909 vat_table_alloc_len = 2910 ((vat_length + UDF_VAT_CHUNKSIZE-1) / UDF_VAT_CHUNKSIZE) 2911 * UDF_VAT_CHUNKSIZE; 2912 2913 vat_table = malloc(vat_table_alloc_len, M_UDFVOLD, 2914 M_CANFAIL | M_WAITOK); 2915 if (vat_table == NULL) { 2916 printf("allocation of %d bytes failed for VAT\n", 2917 vat_table_alloc_len); 2918 return ENOMEM; 2919 } 2920 2921 /* allocate piece to read in head or tail of VAT file */ 2922 raw_vat = malloc(sector_size, M_TEMP, M_WAITOK); 2923 2924 /* 2925 * check contents of the file if its the old 1.50 VAT table format. 2926 * Its notoriously broken and allthough some implementations support an 2927 * extention as defined in the UDF 1.50 errata document, its doubtfull 2928 * to be useable since a lot of implementations don't maintain it. 2929 */ 2930 lvinfo = ump->logvol_info; 2931 2932 if (filetype == 0) { 2933 /* definition */ 2934 vat_offset = 0; 2935 vat_entries = (vat_length-36)/4; 2936 2937 /* read in tail of virtual allocation table file */ 2938 error = vn_rdwr(UIO_READ, vat_node->vnode, 2939 (uint8_t *) raw_vat, 2940 sizeof(struct udf_oldvat_tail), 2941 vat_entries * 4, 2942 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED, 2943 NULL, NULL); 2944 if (error) 2945 goto out; 2946 2947 /* check 1.50 VAT */ 2948 oldvat_tl = (struct udf_oldvat_tail *) raw_vat; 2949 regid_name = (char *) oldvat_tl->id.id; 2950 error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22); 2951 if (error) { 2952 DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n")); 2953 error = ENOENT; 2954 goto out; 2955 } 2956 2957 /* 2958 * update LVID from "*UDF VAT LVExtension" extended attribute 2959 * if present. 2960 */ 2961 udf_update_lvid_from_vat_extattr(vat_node); 2962 } else { 2963 /* read in head of virtual allocation table file */ 2964 error = vn_rdwr(UIO_READ, vat_node->vnode, 2965 (uint8_t *) raw_vat, 2966 sizeof(struct udf_vat), 0, 2967 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED, 2968 NULL, NULL); 2969 if (error) 2970 goto out; 2971 2972 /* definition */ 2973 vat = (struct udf_vat *) raw_vat; 2974 vat_offset = vat->header_len; 2975 vat_entries = (vat_length - vat_offset)/4; 2976 2977 assert(lvinfo); 2978 lvinfo->num_files = vat->num_files; 2979 lvinfo->num_directories = vat->num_directories; 2980 lvinfo->min_udf_readver = vat->min_udf_readver; 2981 lvinfo->min_udf_writever = vat->min_udf_writever; 2982 lvinfo->max_udf_writever = vat->max_udf_writever; 2983 2984 udf_update_logvolname(ump, vat->logvol_id); 2985 } 2986 2987 /* read in complete VAT file */ 2988 error = vn_rdwr(UIO_READ, vat_node->vnode, 2989 vat_table, 2990 vat_length, 0, 2991 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED, 2992 NULL, NULL); 2993 if (error) 2994 printf("read in of complete VAT file failed (error %d)\n", 2995 error); 2996 if (error) 2997 goto out; 2998 2999 DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n")); 3000 ump->logvol_integrity->lvint_next_unique_id = udf_rw64(unique_id); 3001 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED); 3002 ump->logvol_integrity->time = *mtime; 3003 3004 ump->vat_table_len = vat_length; 3005 ump->vat_table_alloc_len = vat_table_alloc_len; 3006 ump->vat_table = vat_table; 3007 ump->vat_offset = vat_offset; 3008 ump->vat_entries = vat_entries; 3009 ump->vat_last_free_lb = 0; /* start at beginning */ 3010 3011 out: 3012 if (error) { 3013 if (vat_table) 3014 free(vat_table, M_UDFVOLD); 3015 } 3016 free(raw_vat, M_TEMP); 3017 3018 return error; 3019 } 3020 3021 /* --------------------------------------------------------------------- */ 3022 3023 static int 3024 udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping) 3025 { 3026 struct udf_node *vat_node; 3027 struct long_ad icb_loc; 3028 uint32_t early_vat_loc, late_vat_loc, vat_loc; 3029 int error; 3030 3031 /* mapping info not needed */ 3032 mapping = mapping; 3033 3034 vat_loc = ump->last_possible_vat_location; 3035 early_vat_loc = vat_loc - 256; /* 8 blocks of 32 sectors */ 3036 3037 DPRINTF(VOLUMES, ("1) last possible %d, early_vat_loc %d \n", 3038 vat_loc, early_vat_loc)); 3039 early_vat_loc = MAX(early_vat_loc, ump->first_possible_vat_location); 3040 late_vat_loc = vat_loc + 1024; 3041 3042 DPRINTF(VOLUMES, ("2) last possible %d, early_vat_loc %d \n", 3043 vat_loc, early_vat_loc)); 3044 3045 /* start looking from the end of the range */ 3046 do { 3047 DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc)); 3048 icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART); 3049 icb_loc.loc.lb_num = udf_rw32(vat_loc); 3050 3051 error = udf_get_node(ump, &icb_loc, &vat_node); 3052 if (!error) { 3053 error = udf_check_for_vat(vat_node); 3054 DPRINTFIF(VOLUMES, !error, 3055 ("VAT accepted at %d\n", vat_loc)); 3056 if (!error) 3057 break; 3058 } 3059 if (vat_node) { 3060 vput(vat_node->vnode); 3061 vat_node = NULL; 3062 } 3063 vat_loc--; /* walk backwards */ 3064 } while (vat_loc >= early_vat_loc); 3065 3066 /* keep our VAT node around */ 3067 if (vat_node) { 3068 UDF_SET_SYSTEMFILE(vat_node->vnode); 3069 ump->vat_node = vat_node; 3070 } 3071 3072 return error; 3073 } 3074 3075 /* --------------------------------------------------------------------- */ 3076 3077 static int 3078 udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping) 3079 { 3080 union dscrptr *dscr; 3081 struct part_map_spare *pms = &mapping->pms; 3082 uint32_t lb_num; 3083 int spar, error; 3084 3085 /* 3086 * The partition mapping passed on to us specifies the information we 3087 * need to locate and initialise the sparable partition mapping 3088 * information we need. 3089 */ 3090 3091 DPRINTF(VOLUMES, ("Read sparable table\n")); 3092 ump->sparable_packet_size = udf_rw16(pms->packet_len); 3093 KASSERT(ump->sparable_packet_size >= ump->packet_size); /* XXX */ 3094 3095 for (spar = 0; spar < pms->n_st; spar++) { 3096 lb_num = pms->st_loc[spar]; 3097 DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num)); 3098 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr); 3099 if (!error && dscr) { 3100 if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) { 3101 if (ump->sparing_table) 3102 free(ump->sparing_table, M_UDFVOLD); 3103 ump->sparing_table = &dscr->spt; 3104 dscr = NULL; 3105 DPRINTF(VOLUMES, 3106 ("Sparing table accepted (%d entries)\n", 3107 udf_rw16(ump->sparing_table->rt_l))); 3108 break; /* we're done */ 3109 } 3110 } 3111 if (dscr) 3112 free(dscr, M_UDFVOLD); 3113 } 3114 3115 if (ump->sparing_table) 3116 return 0; 3117 3118 return ENOENT; 3119 } 3120 3121 /* --------------------------------------------------------------------- */ 3122 3123 static int 3124 udf_read_metadata_nodes(struct udf_mount *ump, union udf_pmap *mapping) 3125 { 3126 struct part_map_meta *pmm = &mapping->pmm; 3127 struct long_ad icb_loc; 3128 struct vnode *vp; 3129 uint16_t raw_phys_part, phys_part; 3130 int error; 3131 3132 /* 3133 * BUGALERT: some rogue implementations use random physical 3134 * partition numbers to break other implementations so lookup 3135 * the number. 3136 */ 3137 3138 /* extract our allocation parameters set up on format */ 3139 ump->metadata_alloc_unit_size = udf_rw32(mapping->pmm.alloc_unit_size); 3140 ump->metadata_alignment_unit_size = udf_rw16(mapping->pmm.alignment_unit_size); 3141 ump->metadata_flags = mapping->pmm.flags; 3142 3143 DPRINTF(VOLUMES, ("Reading in Metadata files\n")); 3144 raw_phys_part = udf_rw16(pmm->part_num); 3145 phys_part = udf_find_raw_phys(ump, raw_phys_part); 3146 3147 icb_loc.loc.part_num = udf_rw16(phys_part); 3148 3149 DPRINTF(VOLUMES, ("Metadata file\n")); 3150 icb_loc.loc.lb_num = pmm->meta_file_lbn; 3151 error = udf_get_node(ump, &icb_loc, &ump->metadata_node); 3152 if (ump->metadata_node) { 3153 vp = ump->metadata_node->vnode; 3154 UDF_SET_SYSTEMFILE(vp); 3155 } 3156 3157 icb_loc.loc.lb_num = pmm->meta_mirror_file_lbn; 3158 if (icb_loc.loc.lb_num != -1) { 3159 DPRINTF(VOLUMES, ("Metadata copy file\n")); 3160 error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_node); 3161 if (ump->metadatamirror_node) { 3162 vp = ump->metadatamirror_node->vnode; 3163 UDF_SET_SYSTEMFILE(vp); 3164 } 3165 } 3166 3167 icb_loc.loc.lb_num = pmm->meta_bitmap_file_lbn; 3168 if (icb_loc.loc.lb_num != -1) { 3169 DPRINTF(VOLUMES, ("Metadata bitmap file\n")); 3170 error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_node); 3171 if (ump->metadatabitmap_node) { 3172 vp = ump->metadatabitmap_node->vnode; 3173 UDF_SET_SYSTEMFILE(vp); 3174 } 3175 } 3176 3177 /* if we're mounting read-only we relax the requirements */ 3178 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) { 3179 error = EFAULT; 3180 if (ump->metadata_node) 3181 error = 0; 3182 if ((ump->metadata_node == NULL) && (ump->metadatamirror_node)) { 3183 printf( "udf mount: Metadata file not readable, " 3184 "substituting Metadata copy file\n"); 3185 ump->metadata_node = ump->metadatamirror_node; 3186 ump->metadatamirror_node = NULL; 3187 error = 0; 3188 } 3189 } else { 3190 /* mounting read/write */ 3191 /* XXX DISABLED! metadata writing is not working yet XXX */ 3192 if (error) 3193 error = EROFS; 3194 } 3195 DPRINTFIF(VOLUMES, error, ("udf mount: failed to read " 3196 "metadata files\n")); 3197 return error; 3198 } 3199 3200 /* --------------------------------------------------------------------- */ 3201 3202 int 3203 udf_read_vds_tables(struct udf_mount *ump) 3204 { 3205 union udf_pmap *mapping; 3206 /* struct udf_args *args = &ump->mount_args; */ 3207 uint32_t n_pm, mt_l; 3208 uint32_t log_part; 3209 uint8_t *pmap_pos; 3210 int pmap_size; 3211 int error; 3212 3213 /* Iterate (again) over the part mappings for locations */ 3214 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */ 3215 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */ 3216 pmap_pos = ump->logical_vol->maps; 3217 3218 for (log_part = 0; log_part < n_pm; log_part++) { 3219 mapping = (union udf_pmap *) pmap_pos; 3220 switch (ump->vtop_tp[log_part]) { 3221 case UDF_VTOP_TYPE_PHYS : 3222 /* nothing */ 3223 break; 3224 case UDF_VTOP_TYPE_VIRT : 3225 /* search and load VAT */ 3226 error = udf_search_vat(ump, mapping); 3227 if (error) 3228 return ENOENT; 3229 break; 3230 case UDF_VTOP_TYPE_SPARABLE : 3231 /* load one of the sparable tables */ 3232 error = udf_read_sparables(ump, mapping); 3233 if (error) 3234 return ENOENT; 3235 break; 3236 case UDF_VTOP_TYPE_META : 3237 /* load the associated file descriptors */ 3238 error = udf_read_metadata_nodes(ump, mapping); 3239 if (error) 3240 return ENOENT; 3241 break; 3242 default: 3243 break; 3244 } 3245 pmap_size = pmap_pos[1]; 3246 pmap_pos += pmap_size; 3247 } 3248 3249 /* read in and check unallocated and free space info if writing */ 3250 if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY) == 0) { 3251 error = udf_read_physical_partition_spacetables(ump); 3252 if (error) 3253 return error; 3254 3255 /* also read in metadata partition spacebitmap if defined */ 3256 error = udf_read_metadata_partition_spacetable(ump); 3257 return error; 3258 } 3259 3260 return 0; 3261 } 3262 3263 /* --------------------------------------------------------------------- */ 3264 3265 int 3266 udf_read_rootdirs(struct udf_mount *ump) 3267 { 3268 union dscrptr *dscr; 3269 /* struct udf_args *args = &ump->mount_args; */ 3270 struct udf_node *rootdir_node, *streamdir_node; 3271 struct long_ad fsd_loc, *dir_loc; 3272 uint32_t lb_num, dummy; 3273 uint32_t fsd_len; 3274 int dscr_type; 3275 int error; 3276 3277 /* TODO implement FSD reading in separate function like integrity? */ 3278 /* get fileset descriptor sequence */ 3279 fsd_loc = ump->logical_vol->lv_fsd_loc; 3280 fsd_len = udf_rw32(fsd_loc.len); 3281 3282 dscr = NULL; 3283 error = 0; 3284 while (fsd_len || error) { 3285 DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len)); 3286 /* translate fsd_loc to lb_num */ 3287 error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy); 3288 if (error) 3289 break; 3290 DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num)); 3291 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr); 3292 /* end markers */ 3293 if (error || (dscr == NULL)) 3294 break; 3295 3296 /* analyse */ 3297 dscr_type = udf_rw16(dscr->tag.id); 3298 if (dscr_type == TAGID_TERM) 3299 break; 3300 if (dscr_type != TAGID_FSD) { 3301 free(dscr, M_UDFVOLD); 3302 return ENOENT; 3303 } 3304 3305 /* 3306 * TODO check for multiple fileset descriptors; its only 3307 * picking the last now. Also check for FSD 3308 * correctness/interpretability 3309 */ 3310 3311 /* update */ 3312 if (ump->fileset_desc) { 3313 free(ump->fileset_desc, M_UDFVOLD); 3314 } 3315 ump->fileset_desc = &dscr->fsd; 3316 dscr = NULL; 3317 3318 /* continue to the next fsd */ 3319 fsd_len -= ump->discinfo.sector_size; 3320 fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1); 3321 3322 /* follow up to fsd->next_ex (long_ad) if its not null */ 3323 if (udf_rw32(ump->fileset_desc->next_ex.len)) { 3324 DPRINTF(VOLUMES, ("follow up FSD extent\n")); 3325 fsd_loc = ump->fileset_desc->next_ex; 3326 fsd_len = udf_rw32(ump->fileset_desc->next_ex.len); 3327 } 3328 } 3329 if (dscr) 3330 free(dscr, M_UDFVOLD); 3331 3332 /* there has to be one */ 3333 if (ump->fileset_desc == NULL) 3334 return ENOENT; 3335 3336 DPRINTF(VOLUMES, ("FSD read in fine\n")); 3337 DPRINTF(VOLUMES, ("Updating fsd logical volume id\n")); 3338 udf_update_logvolname(ump, ump->logical_vol->logvol_id); 3339 3340 /* 3341 * Now the FSD is known, read in the rootdirectory and if one exists, 3342 * the system stream dir. Some files in the system streamdir are not 3343 * wanted in this implementation since they are not maintained. If 3344 * writing is enabled we'll delete these files if they exist. 3345 */ 3346 3347 rootdir_node = streamdir_node = NULL; 3348 dir_loc = NULL; 3349 3350 /* try to read in the rootdir */ 3351 dir_loc = &ump->fileset_desc->rootdir_icb; 3352 error = udf_get_node(ump, dir_loc, &rootdir_node); 3353 if (error) 3354 return ENOENT; 3355 3356 /* aparently it read in fine */ 3357 3358 /* 3359 * Try the system stream directory; not very likely in the ones we 3360 * test, but for completeness. 3361 */ 3362 dir_loc = &ump->fileset_desc->streamdir_icb; 3363 if (udf_rw32(dir_loc->len)) { 3364 printf("udf_read_rootdirs: streamdir defined "); 3365 error = udf_get_node(ump, dir_loc, &streamdir_node); 3366 if (error) { 3367 printf("but error in streamdir reading\n"); 3368 } else { 3369 printf("but ignored\n"); 3370 /* 3371 * TODO process streamdir `baddies' i.e. files we dont 3372 * want if R/W 3373 */ 3374 } 3375 } 3376 3377 DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n")); 3378 3379 /* release the vnodes again; they'll be auto-recycled later */ 3380 if (streamdir_node) { 3381 vput(streamdir_node->vnode); 3382 } 3383 if (rootdir_node) { 3384 vput(rootdir_node->vnode); 3385 } 3386 3387 return 0; 3388 } 3389 3390 /* --------------------------------------------------------------------- */ 3391 3392 /* To make absolutely sure we are NOT returning zero, add one :) */ 3393 3394 long 3395 udf_get_node_id(const struct long_ad *icbptr) 3396 { 3397 /* ought to be enough since each mountpoint has its own chain */ 3398 return udf_rw32(icbptr->loc.lb_num) + 1; 3399 } 3400 3401 3402 int 3403 udf_compare_icb(const struct long_ad *a, const struct long_ad *b) 3404 { 3405 if (udf_rw16(a->loc.part_num) < udf_rw16(b->loc.part_num)) 3406 return -1; 3407 if (udf_rw16(a->loc.part_num) > udf_rw16(b->loc.part_num)) 3408 return 1; 3409 3410 if (udf_rw32(a->loc.lb_num) < udf_rw32(b->loc.lb_num)) 3411 return -1; 3412 if (udf_rw32(a->loc.lb_num) > udf_rw32(b->loc.lb_num)) 3413 return 1; 3414 3415 return 0; 3416 } 3417 3418 3419 static int 3420 udf_compare_rbnodes(void *ctx, const void *a, const void *b) 3421 { 3422 const struct udf_node *a_node = a; 3423 const struct udf_node *b_node = b; 3424 3425 return udf_compare_icb(&a_node->loc, &b_node->loc); 3426 } 3427 3428 3429 static int 3430 udf_compare_rbnode_icb(void *ctx, const void *a, const void *key) 3431 { 3432 const struct udf_node *a_node = a; 3433 const struct long_ad * const icb = key; 3434 3435 return udf_compare_icb(&a_node->loc, icb); 3436 } 3437 3438 3439 static const rb_tree_ops_t udf_node_rbtree_ops = { 3440 .rbto_compare_nodes = udf_compare_rbnodes, 3441 .rbto_compare_key = udf_compare_rbnode_icb, 3442 .rbto_node_offset = offsetof(struct udf_node, rbnode), 3443 .rbto_context = NULL 3444 }; 3445 3446 3447 void 3448 udf_init_nodes_tree(struct udf_mount *ump) 3449 { 3450 3451 rb_tree_init(&ump->udf_node_tree, &udf_node_rbtree_ops); 3452 } 3453 3454 3455 static struct udf_node * 3456 udf_node_lookup(struct udf_mount *ump, struct long_ad *icbptr) 3457 { 3458 struct udf_node *udf_node; 3459 struct vnode *vp; 3460 3461 loop: 3462 mutex_enter(&ump->ihash_lock); 3463 3464 udf_node = rb_tree_find_node(&ump->udf_node_tree, icbptr); 3465 if (udf_node) { 3466 vp = udf_node->vnode; 3467 assert(vp); 3468 mutex_enter(&vp->v_interlock); 3469 mutex_exit(&ump->ihash_lock); 3470 if (vget(vp, LK_EXCLUSIVE)) 3471 goto loop; 3472 return udf_node; 3473 } 3474 mutex_exit(&ump->ihash_lock); 3475 3476 return NULL; 3477 } 3478 3479 3480 static void 3481 udf_register_node(struct udf_node *udf_node) 3482 { 3483 struct udf_mount *ump = udf_node->ump; 3484 3485 /* add node to the rb tree */ 3486 mutex_enter(&ump->ihash_lock); 3487 rb_tree_insert_node(&ump->udf_node_tree, udf_node); 3488 mutex_exit(&ump->ihash_lock); 3489 } 3490 3491 3492 static void 3493 udf_deregister_node(struct udf_node *udf_node) 3494 { 3495 struct udf_mount *ump = udf_node->ump; 3496 3497 /* remove node from the rb tree */ 3498 mutex_enter(&ump->ihash_lock); 3499 rb_tree_remove_node(&ump->udf_node_tree, udf_node); 3500 mutex_exit(&ump->ihash_lock); 3501 } 3502 3503 /* --------------------------------------------------------------------- */ 3504 3505 static int 3506 udf_validate_session_start(struct udf_mount *ump) 3507 { 3508 struct mmc_trackinfo trackinfo; 3509 struct vrs_desc *vrs; 3510 uint32_t tracknr, sessionnr, sector, sector_size; 3511 uint32_t iso9660_vrs, write_track_start; 3512 uint8_t *buffer, *blank, *pos; 3513 int blks, max_sectors, vrs_len; 3514 int error; 3515 3516 /* disc appendable? */ 3517 if (ump->discinfo.disc_state == MMC_STATE_FULL) 3518 return EROFS; 3519 3520 /* already written here? if so, there should be an ISO VDS */ 3521 if (ump->discinfo.last_session_state == MMC_STATE_INCOMPLETE) 3522 return 0; 3523 3524 /* 3525 * Check if the first track of the session is blank and if so, copy or 3526 * create a dummy ISO descriptor so the disc is valid again. 3527 */ 3528 3529 tracknr = ump->discinfo.first_track_last_session; 3530 memset(&trackinfo, 0, sizeof(struct mmc_trackinfo)); 3531 trackinfo.tracknr = tracknr; 3532 error = udf_update_trackinfo(ump, &trackinfo); 3533 if (error) 3534 return error; 3535 3536 udf_dump_trackinfo(&trackinfo); 3537 KASSERT(trackinfo.flags & (MMC_TRACKINFO_BLANK | MMC_TRACKINFO_RESERVED)); 3538 KASSERT(trackinfo.sessionnr > 1); 3539 3540 KASSERT(trackinfo.flags & MMC_TRACKINFO_NWA_VALID); 3541 write_track_start = trackinfo.next_writable; 3542 3543 /* we have to copy the ISO VRS from a former session */ 3544 DPRINTF(VOLUMES, ("validate_session_start: " 3545 "blank or reserved track, copying VRS\n")); 3546 3547 /* sessionnr should be the session we're mounting */ 3548 sessionnr = ump->mount_args.sessionnr; 3549 3550 /* start at the first track */ 3551 tracknr = ump->discinfo.first_track; 3552 while (tracknr <= ump->discinfo.num_tracks) { 3553 trackinfo.tracknr = tracknr; 3554 error = udf_update_trackinfo(ump, &trackinfo); 3555 if (error) { 3556 DPRINTF(VOLUMES, ("failed to get trackinfo; aborting\n")); 3557 return error; 3558 } 3559 if (trackinfo.sessionnr == sessionnr) 3560 break; 3561 tracknr++; 3562 } 3563 if (trackinfo.sessionnr != sessionnr) { 3564 DPRINTF(VOLUMES, ("failed to get trackinfo; aborting\n")); 3565 return ENOENT; 3566 } 3567 3568 DPRINTF(VOLUMES, ("found possible former ISO VRS at\n")); 3569 udf_dump_trackinfo(&trackinfo); 3570 3571 /* 3572 * location of iso9660 vrs is defined as first sector AFTER 32kb, 3573 * minimum ISO `sector size' 2048 3574 */ 3575 sector_size = ump->discinfo.sector_size; 3576 iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size) 3577 + trackinfo.track_start; 3578 3579 buffer = malloc(UDF_ISO_VRS_SIZE, M_TEMP, M_WAITOK); 3580 max_sectors = UDF_ISO_VRS_SIZE / sector_size; 3581 blks = MAX(1, 2048 / sector_size); 3582 3583 error = 0; 3584 for (sector = 0; sector < max_sectors; sector += blks) { 3585 pos = buffer + sector * sector_size; 3586 error = udf_read_phys_sectors(ump, UDF_C_DSCR, pos, 3587 iso9660_vrs + sector, blks); 3588 if (error) 3589 break; 3590 /* check this ISO descriptor */ 3591 vrs = (struct vrs_desc *) pos; 3592 DPRINTF(VOLUMES, ("got VRS id `%4s`\n", vrs->identifier)); 3593 if (strncmp(vrs->identifier, VRS_CD001, 5) == 0) 3594 continue; 3595 if (strncmp(vrs->identifier, VRS_CDW02, 5) == 0) 3596 continue; 3597 if (strncmp(vrs->identifier, VRS_BEA01, 5) == 0) 3598 continue; 3599 if (strncmp(vrs->identifier, VRS_NSR02, 5) == 0) 3600 continue; 3601 if (strncmp(vrs->identifier, VRS_NSR03, 5) == 0) 3602 continue; 3603 if (strncmp(vrs->identifier, VRS_TEA01, 5) == 0) 3604 break; 3605 /* now what? for now, end of sequence */ 3606 break; 3607 } 3608 vrs_len = sector + blks; 3609 if (error) { 3610 DPRINTF(VOLUMES, ("error reading old ISO VRS\n")); 3611 DPRINTF(VOLUMES, ("creating minimal ISO VRS\n")); 3612 3613 memset(buffer, 0, UDF_ISO_VRS_SIZE); 3614 3615 vrs = (struct vrs_desc *) (buffer); 3616 vrs->struct_type = 0; 3617 vrs->version = 1; 3618 memcpy(vrs->identifier,VRS_BEA01, 5); 3619 3620 vrs = (struct vrs_desc *) (buffer + 2048); 3621 vrs->struct_type = 0; 3622 vrs->version = 1; 3623 if (udf_rw16(ump->logical_vol->tag.descriptor_ver) == 2) { 3624 memcpy(vrs->identifier,VRS_NSR02, 5); 3625 } else { 3626 memcpy(vrs->identifier,VRS_NSR03, 5); 3627 } 3628 3629 vrs = (struct vrs_desc *) (buffer + 4096); 3630 vrs->struct_type = 0; 3631 vrs->version = 1; 3632 memcpy(vrs->identifier, VRS_TEA01, 5); 3633 3634 vrs_len = 3*blks; 3635 } 3636 3637 DPRINTF(VOLUMES, ("Got VRS of %d sectors long\n", vrs_len)); 3638 3639 /* 3640 * location of iso9660 vrs is defined as first sector AFTER 32kb, 3641 * minimum ISO `sector size' 2048 3642 */ 3643 sector_size = ump->discinfo.sector_size; 3644 iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size) 3645 + write_track_start; 3646 3647 /* write out 32 kb */ 3648 blank = malloc(sector_size, M_TEMP, M_WAITOK); 3649 memset(blank, 0, sector_size); 3650 error = 0; 3651 for (sector = write_track_start; sector < iso9660_vrs; sector ++) { 3652 error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE, 3653 blank, sector, 1); 3654 if (error) 3655 break; 3656 } 3657 if (!error) { 3658 /* write out our ISO VRS */ 3659 KASSERT(sector == iso9660_vrs); 3660 error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE, buffer, 3661 sector, vrs_len); 3662 sector += vrs_len; 3663 } 3664 if (!error) { 3665 /* fill upto the first anchor at S+256 */ 3666 for (; sector < write_track_start+256; sector++) { 3667 error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE, 3668 blank, sector, 1); 3669 if (error) 3670 break; 3671 } 3672 } 3673 if (!error) { 3674 /* write out anchor; write at ABSOLUTE place! */ 3675 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_ABSOLUTE, 3676 (union dscrptr *) ump->anchors[0], sector, sector); 3677 if (error) 3678 printf("writeout of anchor failed!\n"); 3679 } 3680 3681 free(blank, M_TEMP); 3682 free(buffer, M_TEMP); 3683 3684 if (error) 3685 printf("udf_open_session: error writing iso vrs! : " 3686 "leaving disc in compromised state!\n"); 3687 3688 /* synchronise device caches */ 3689 (void) udf_synchronise_caches(ump); 3690 3691 return error; 3692 } 3693 3694 3695 int 3696 udf_open_logvol(struct udf_mount *ump) 3697 { 3698 int logvol_integrity; 3699 int error; 3700 3701 /* already/still open? */ 3702 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type); 3703 if (logvol_integrity == UDF_INTEGRITY_OPEN) 3704 return 0; 3705 3706 /* can we open it ? */ 3707 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) 3708 return EROFS; 3709 3710 /* setup write parameters */ 3711 DPRINTF(VOLUMES, ("Setting up write parameters\n")); 3712 if ((error = udf_setup_writeparams(ump)) != 0) 3713 return error; 3714 3715 /* determine data and metadata tracks (most likely same) */ 3716 error = udf_search_writing_tracks(ump); 3717 if (error) { 3718 /* most likely lack of space */ 3719 printf("udf_open_logvol: error searching writing tracks\n"); 3720 return EROFS; 3721 } 3722 3723 /* writeout/update lvint on disc or only in memory */ 3724 DPRINTF(VOLUMES, ("Opening logical volume\n")); 3725 if (ump->lvopen & UDF_OPEN_SESSION) { 3726 /* TODO optional track reservation opening */ 3727 error = udf_validate_session_start(ump); 3728 if (error) 3729 return error; 3730 3731 /* determine data and metadata tracks again */ 3732 error = udf_search_writing_tracks(ump); 3733 } 3734 3735 /* mark it open */ 3736 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_OPEN); 3737 3738 /* do we need to write it out? */ 3739 if (ump->lvopen & UDF_WRITE_LVINT) { 3740 error = udf_writeout_lvint(ump, ump->lvopen); 3741 /* if we couldn't write it mark it closed again */ 3742 if (error) { 3743 ump->logvol_integrity->integrity_type = 3744 udf_rw32(UDF_INTEGRITY_CLOSED); 3745 return error; 3746 } 3747 } 3748 3749 return 0; 3750 } 3751 3752 3753 int 3754 udf_close_logvol(struct udf_mount *ump, int mntflags) 3755 { 3756 struct vnode *devvp = ump->devvp; 3757 struct mmc_op mmc_op; 3758 int logvol_integrity; 3759 int error = 0, error1 = 0, error2 = 0; 3760 int tracknr; 3761 int nvats, n, nok; 3762 3763 /* already/still closed? */ 3764 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type); 3765 if (logvol_integrity == UDF_INTEGRITY_CLOSED) 3766 return 0; 3767 3768 /* writeout/update lvint or write out VAT */ 3769 DPRINTF(VOLUMES, ("udf_close_logvol: closing logical volume\n")); 3770 #ifdef DIAGNOSTIC 3771 if (ump->lvclose & UDF_CLOSE_SESSION) 3772 KASSERT(ump->lvclose & UDF_WRITE_VAT); 3773 #endif 3774 3775 if (ump->lvclose & UDF_WRITE_VAT) { 3776 DPRINTF(VOLUMES, ("lvclose & UDF_WRITE_VAT\n")); 3777 3778 /* write out the VAT data and all its descriptors */ 3779 DPRINTF(VOLUMES, ("writeout vat_node\n")); 3780 udf_writeout_vat(ump); 3781 vflushbuf(ump->vat_node->vnode, 1 /* sync */); 3782 3783 (void) VOP_FSYNC(ump->vat_node->vnode, 3784 FSCRED, FSYNC_WAIT, 0, 0); 3785 3786 if (ump->lvclose & UDF_CLOSE_SESSION) { 3787 DPRINTF(VOLUMES, ("udf_close_logvol: closing session " 3788 "as requested\n")); 3789 } 3790 3791 /* at least two DVD packets and 3 CD-R packets */ 3792 nvats = 32; 3793 3794 #if notyet 3795 /* 3796 * TODO calculate the available space and if the disc is 3797 * allmost full, write out till end-256-1 with banks, write 3798 * AVDP and fill up with VATs, then close session and close 3799 * disc. 3800 */ 3801 if (ump->lvclose & UDF_FINALISE_DISC) { 3802 error = udf_write_phys_dscr_sync(ump, NULL, 3803 UDF_C_FLOAT_DSCR, 3804 (union dscrptr *) ump->anchors[0], 3805 0, 0); 3806 if (error) 3807 printf("writeout of anchor failed!\n"); 3808 3809 /* pad space with VAT ICBs */ 3810 nvats = 256; 3811 } 3812 #endif 3813 3814 /* write out a number of VAT nodes */ 3815 nok = 0; 3816 for (n = 0; n < nvats; n++) { 3817 /* will now only write last FE/EFE */ 3818 ump->vat_node->i_flags |= IN_MODIFIED; 3819 error = VOP_FSYNC(ump->vat_node->vnode, 3820 FSCRED, FSYNC_WAIT, 0, 0); 3821 if (!error) 3822 nok++; 3823 } 3824 if (nok < 14) { 3825 /* arbitrary; but at least one or two CD frames */ 3826 printf("writeout of at least 14 VATs failed\n"); 3827 return error; 3828 } 3829 } 3830 3831 /* NOTE the disc is in a (minimal) valid state now; no erroring out */ 3832 3833 /* finish closing of session */ 3834 if (ump->lvclose & UDF_CLOSE_SESSION) { 3835 error = udf_validate_session_start(ump); 3836 if (error) 3837 return error; 3838 3839 (void) udf_synchronise_caches(ump); 3840 3841 /* close all associated tracks */ 3842 tracknr = ump->discinfo.first_track_last_session; 3843 error = 0; 3844 while (tracknr <= ump->discinfo.last_track_last_session) { 3845 DPRINTF(VOLUMES, ("\tclosing possible open " 3846 "track %d\n", tracknr)); 3847 memset(&mmc_op, 0, sizeof(mmc_op)); 3848 mmc_op.operation = MMC_OP_CLOSETRACK; 3849 mmc_op.mmc_profile = ump->discinfo.mmc_profile; 3850 mmc_op.tracknr = tracknr; 3851 error = VOP_IOCTL(devvp, MMCOP, &mmc_op, 3852 FKIOCTL, NOCRED); 3853 if (error) 3854 printf("udf_close_logvol: closing of " 3855 "track %d failed\n", tracknr); 3856 tracknr ++; 3857 } 3858 if (!error) { 3859 DPRINTF(VOLUMES, ("closing session\n")); 3860 memset(&mmc_op, 0, sizeof(mmc_op)); 3861 mmc_op.operation = MMC_OP_CLOSESESSION; 3862 mmc_op.mmc_profile = ump->discinfo.mmc_profile; 3863 mmc_op.sessionnr = ump->discinfo.num_sessions; 3864 error = VOP_IOCTL(devvp, MMCOP, &mmc_op, 3865 FKIOCTL, NOCRED); 3866 if (error) 3867 printf("udf_close_logvol: closing of session" 3868 "failed\n"); 3869 } 3870 if (!error) 3871 ump->lvopen |= UDF_OPEN_SESSION; 3872 if (error) { 3873 printf("udf_close_logvol: leaving disc as it is\n"); 3874 ump->lvclose &= ~UDF_FINALISE_DISC; 3875 } 3876 } 3877 3878 if (ump->lvclose & UDF_FINALISE_DISC) { 3879 memset(&mmc_op, 0, sizeof(mmc_op)); 3880 mmc_op.operation = MMC_OP_FINALISEDISC; 3881 mmc_op.mmc_profile = ump->discinfo.mmc_profile; 3882 mmc_op.sessionnr = ump->discinfo.num_sessions; 3883 error = VOP_IOCTL(devvp, MMCOP, &mmc_op, 3884 FKIOCTL, NOCRED); 3885 if (error) 3886 printf("udf_close_logvol: finalising disc" 3887 "failed\n"); 3888 } 3889 3890 /* write out partition bitmaps if requested */ 3891 if (ump->lvclose & UDF_WRITE_PART_BITMAPS) { 3892 /* sync writeout metadata spacetable if existing */ 3893 error1 = udf_write_metadata_partition_spacetable(ump, true); 3894 if (error1) 3895 printf( "udf_close_logvol: writeout of metadata space " 3896 "bitmap failed\n"); 3897 3898 /* sync writeout partition spacetables */ 3899 error2 = udf_write_physical_partition_spacetables(ump, true); 3900 if (error2) 3901 printf( "udf_close_logvol: writeout of space tables " 3902 "failed\n"); 3903 3904 if (error1 || error2) 3905 return (error1 | error2); 3906 3907 ump->lvclose &= ~UDF_WRITE_PART_BITMAPS; 3908 } 3909 3910 /* write out metadata partition nodes if requested */ 3911 if (ump->lvclose & UDF_WRITE_METAPART_NODES) { 3912 /* sync writeout metadata descriptor node */ 3913 error1 = udf_writeout_node(ump->metadata_node, FSYNC_WAIT); 3914 if (error1) 3915 printf( "udf_close_logvol: writeout of metadata partition " 3916 "node failed\n"); 3917 3918 /* duplicate metadata partition descriptor if needed */ 3919 udf_synchronise_metadatamirror_node(ump); 3920 3921 /* sync writeout metadatamirror descriptor node */ 3922 error2 = udf_writeout_node(ump->metadatamirror_node, FSYNC_WAIT); 3923 if (error2) 3924 printf( "udf_close_logvol: writeout of metadata partition " 3925 "mirror node failed\n"); 3926 3927 if (error1 || error2) 3928 return (error1 | error2); 3929 3930 ump->lvclose &= ~UDF_WRITE_METAPART_NODES; 3931 } 3932 3933 /* mark it closed */ 3934 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED); 3935 3936 /* do we need to write out the logical volume integrity? */ 3937 if (ump->lvclose & UDF_WRITE_LVINT) 3938 error = udf_writeout_lvint(ump, ump->lvopen); 3939 if (error) { 3940 /* HELP now what? mark it open again for now */ 3941 ump->logvol_integrity->integrity_type = 3942 udf_rw32(UDF_INTEGRITY_OPEN); 3943 return error; 3944 } 3945 3946 (void) udf_synchronise_caches(ump); 3947 3948 return 0; 3949 } 3950 3951 /* --------------------------------------------------------------------- */ 3952 3953 /* 3954 * Genfs interfacing 3955 * 3956 * static const struct genfs_ops udf_genfsops = { 3957 * .gop_size = genfs_size, 3958 * size of transfers 3959 * .gop_alloc = udf_gop_alloc, 3960 * allocate len bytes at offset 3961 * .gop_write = genfs_gop_write, 3962 * putpages interface code 3963 * .gop_markupdate = udf_gop_markupdate, 3964 * set update/modify flags etc. 3965 * } 3966 */ 3967 3968 /* 3969 * Genfs interface. These four functions are the only ones defined though not 3970 * documented... great.... 3971 */ 3972 3973 /* 3974 * Called for allocating an extent of the file either by VOP_WRITE() or by 3975 * genfs filling up gaps. 3976 */ 3977 static int 3978 udf_gop_alloc(struct vnode *vp, off_t off, 3979 off_t len, int flags, kauth_cred_t cred) 3980 { 3981 struct udf_node *udf_node = VTOI(vp); 3982 struct udf_mount *ump = udf_node->ump; 3983 uint64_t lb_start, lb_end; 3984 uint32_t lb_size, num_lb; 3985 int udf_c_type, vpart_num, can_fail; 3986 int error; 3987 3988 DPRINTF(ALLOC, ("udf_gop_alloc called for offset %"PRIu64" for %"PRIu64" bytes, %s\n", 3989 off, len, flags? "SYNC":"NONE")); 3990 3991 /* 3992 * request the pages of our vnode and see how many pages will need to 3993 * be allocated and reserve that space 3994 */ 3995 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 3996 lb_start = off / lb_size; 3997 lb_end = (off + len + lb_size -1) / lb_size; 3998 num_lb = lb_end - lb_start; 3999 4000 udf_c_type = udf_get_c_type(udf_node); 4001 vpart_num = udf_get_record_vpart(ump, udf_c_type); 4002 4003 /* all requests can fail */ 4004 can_fail = true; 4005 4006 /* fid's (directories) can't fail */ 4007 if (udf_c_type == UDF_C_FIDS) 4008 can_fail = false; 4009 4010 /* system files can't fail */ 4011 if (vp->v_vflag & VV_SYSTEM) 4012 can_fail = false; 4013 4014 error = udf_reserve_space(ump, udf_node, udf_c_type, 4015 vpart_num, num_lb, can_fail); 4016 4017 DPRINTF(ALLOC, ("\tlb_start %"PRIu64", lb_end %"PRIu64", num_lb %d\n", 4018 lb_start, lb_end, num_lb)); 4019 4020 return error; 4021 } 4022 4023 4024 /* 4025 * callback from genfs to update our flags 4026 */ 4027 static void 4028 udf_gop_markupdate(struct vnode *vp, int flags) 4029 { 4030 struct udf_node *udf_node = VTOI(vp); 4031 u_long mask = 0; 4032 4033 if ((flags & GOP_UPDATE_ACCESSED) != 0) { 4034 mask = IN_ACCESS; 4035 } 4036 if ((flags & GOP_UPDATE_MODIFIED) != 0) { 4037 if (vp->v_type == VREG) { 4038 mask |= IN_CHANGE | IN_UPDATE; 4039 } else { 4040 mask |= IN_MODIFY; 4041 } 4042 } 4043 if (mask) { 4044 udf_node->i_flags |= mask; 4045 } 4046 } 4047 4048 4049 static const struct genfs_ops udf_genfsops = { 4050 .gop_size = genfs_size, 4051 .gop_alloc = udf_gop_alloc, 4052 .gop_write = genfs_gop_write_rwmap, 4053 .gop_markupdate = udf_gop_markupdate, 4054 }; 4055 4056 4057 /* --------------------------------------------------------------------- */ 4058 4059 int 4060 udf_write_terminator(struct udf_mount *ump, uint32_t sector) 4061 { 4062 union dscrptr *dscr; 4063 int error; 4064 4065 dscr = malloc(ump->discinfo.sector_size, M_TEMP, M_WAITOK|M_ZERO); 4066 udf_inittag(ump, &dscr->tag, TAGID_TERM, sector); 4067 4068 /* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */ 4069 dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH); 4070 (void) udf_validate_tag_and_crc_sums(dscr); 4071 4072 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR, 4073 dscr, sector, sector); 4074 4075 free(dscr, M_TEMP); 4076 4077 return error; 4078 } 4079 4080 4081 /* --------------------------------------------------------------------- */ 4082 4083 /* UDF<->unix converters */ 4084 4085 /* --------------------------------------------------------------------- */ 4086 4087 static mode_t 4088 udf_perm_to_unix_mode(uint32_t perm) 4089 { 4090 mode_t mode; 4091 4092 mode = ((perm & UDF_FENTRY_PERM_USER_MASK) ); 4093 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2); 4094 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); 4095 4096 return mode; 4097 } 4098 4099 /* --------------------------------------------------------------------- */ 4100 4101 static uint32_t 4102 unix_mode_to_udf_perm(mode_t mode) 4103 { 4104 uint32_t perm; 4105 4106 perm = ((mode & S_IRWXO) ); 4107 perm |= ((mode & S_IRWXG) << 2); 4108 perm |= ((mode & S_IRWXU) << 4); 4109 perm |= ((mode & S_IWOTH) << 3); 4110 perm |= ((mode & S_IWGRP) << 5); 4111 perm |= ((mode & S_IWUSR) << 7); 4112 4113 return perm; 4114 } 4115 4116 /* --------------------------------------------------------------------- */ 4117 4118 static uint32_t 4119 udf_icb_to_unix_filetype(uint32_t icbftype) 4120 { 4121 switch (icbftype) { 4122 case UDF_ICB_FILETYPE_DIRECTORY : 4123 case UDF_ICB_FILETYPE_STREAMDIR : 4124 return S_IFDIR; 4125 case UDF_ICB_FILETYPE_FIFO : 4126 return S_IFIFO; 4127 case UDF_ICB_FILETYPE_CHARDEVICE : 4128 return S_IFCHR; 4129 case UDF_ICB_FILETYPE_BLOCKDEVICE : 4130 return S_IFBLK; 4131 case UDF_ICB_FILETYPE_RANDOMACCESS : 4132 case UDF_ICB_FILETYPE_REALTIME : 4133 return S_IFREG; 4134 case UDF_ICB_FILETYPE_SYMLINK : 4135 return S_IFLNK; 4136 case UDF_ICB_FILETYPE_SOCKET : 4137 return S_IFSOCK; 4138 } 4139 /* no idea what this is */ 4140 return 0; 4141 } 4142 4143 /* --------------------------------------------------------------------- */ 4144 4145 void 4146 udf_to_unix_name(char *result, int result_len, char *id, int len, 4147 struct charspec *chsp) 4148 { 4149 uint16_t *raw_name, *unix_name; 4150 uint16_t *inchp, ch; 4151 uint8_t *outchp; 4152 const char *osta_id = "OSTA Compressed Unicode"; 4153 int ucode_chars, nice_uchars, is_osta_typ0, nout; 4154 4155 raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK); 4156 unix_name = raw_name + 1024; /* split space in half */ 4157 assert(sizeof(char) == sizeof(uint8_t)); 4158 outchp = (uint8_t *) result; 4159 4160 is_osta_typ0 = (chsp->type == 0); 4161 is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0); 4162 if (is_osta_typ0) { 4163 /* TODO clean up */ 4164 *raw_name = *unix_name = 0; 4165 ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name); 4166 ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name)); 4167 nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars); 4168 /* output UTF8 */ 4169 for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) { 4170 ch = *inchp; 4171 nout = wput_utf8(outchp, result_len, ch); 4172 outchp += nout; result_len -= nout; 4173 if (!ch) break; 4174 } 4175 *outchp++ = 0; 4176 } else { 4177 /* assume 8bit char length byte latin-1 */ 4178 assert(*id == 8); 4179 assert(strlen((char *) (id+1)) <= MAXNAMLEN); 4180 strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1))); 4181 } 4182 free(raw_name, M_UDFTEMP); 4183 } 4184 4185 /* --------------------------------------------------------------------- */ 4186 4187 void 4188 unix_to_udf_name(char *result, uint8_t *result_len, char const *name, int name_len, 4189 struct charspec *chsp) 4190 { 4191 uint16_t *raw_name; 4192 uint16_t *outchp; 4193 const char *inchp; 4194 const char *osta_id = "OSTA Compressed Unicode"; 4195 int udf_chars, is_osta_typ0, bits; 4196 size_t cnt; 4197 4198 /* allocate temporary unicode-16 buffer */ 4199 raw_name = malloc(1024, M_UDFTEMP, M_WAITOK); 4200 4201 /* convert utf8 to unicode-16 */ 4202 *raw_name = 0; 4203 inchp = name; 4204 outchp = raw_name; 4205 bits = 8; 4206 for (cnt = name_len, udf_chars = 0; cnt;) { 4207 /*###3490 [cc] warning: passing argument 2 of 'wget_utf8' from incompatible pointer type%%%*/ 4208 *outchp = wget_utf8(&inchp, &cnt); 4209 if (*outchp > 0xff) 4210 bits=16; 4211 outchp++; 4212 udf_chars++; 4213 } 4214 /* null terminate just in case */ 4215 *outchp++ = 0; 4216 4217 is_osta_typ0 = (chsp->type == 0); 4218 is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0); 4219 if (is_osta_typ0) { 4220 udf_chars = udf_CompressUnicode(udf_chars, bits, 4221 (unicode_t *) raw_name, 4222 (byte *) result); 4223 } else { 4224 printf("unix to udf name: no CHSP0 ?\n"); 4225 /* XXX assume 8bit char length byte latin-1 */ 4226 *result++ = 8; udf_chars = 1; 4227 strncpy(result, name + 1, name_len); 4228 udf_chars += name_len; 4229 } 4230 *result_len = udf_chars; 4231 free(raw_name, M_UDFTEMP); 4232 } 4233 4234 /* --------------------------------------------------------------------- */ 4235 4236 void 4237 udf_timestamp_to_timespec(struct udf_mount *ump, 4238 struct timestamp *timestamp, 4239 struct timespec *timespec) 4240 { 4241 struct clock_ymdhms ymdhms; 4242 uint32_t usecs, secs, nsecs; 4243 uint16_t tz; 4244 4245 /* fill in ymdhms structure from timestamp */ 4246 memset(&ymdhms, 0, sizeof(ymdhms)); 4247 ymdhms.dt_year = udf_rw16(timestamp->year); 4248 ymdhms.dt_mon = timestamp->month; 4249 ymdhms.dt_day = timestamp->day; 4250 ymdhms.dt_wday = 0; /* ? */ 4251 ymdhms.dt_hour = timestamp->hour; 4252 ymdhms.dt_min = timestamp->minute; 4253 ymdhms.dt_sec = timestamp->second; 4254 4255 secs = clock_ymdhms_to_secs(&ymdhms); 4256 usecs = timestamp->usec + 4257 100*timestamp->hund_usec + 10000*timestamp->centisec; 4258 nsecs = usecs * 1000; 4259 4260 /* 4261 * Calculate the time zone. The timezone is 12 bit signed 2's 4262 * compliment, so we gotta do some extra magic to handle it right. 4263 */ 4264 tz = udf_rw16(timestamp->type_tz); 4265 tz &= 0x0fff; /* only lower 12 bits are significant */ 4266 if (tz & 0x0800) /* sign extention */ 4267 tz |= 0xf000; 4268 4269 /* TODO check timezone conversion */ 4270 /* check if we are specified a timezone to convert */ 4271 if (udf_rw16(timestamp->type_tz) & 0x1000) { 4272 if ((int16_t) tz != -2047) 4273 secs -= (int16_t) tz * 60; 4274 } else { 4275 secs -= ump->mount_args.gmtoff; 4276 } 4277 4278 timespec->tv_sec = secs; 4279 timespec->tv_nsec = nsecs; 4280 } 4281 4282 4283 void 4284 udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp) 4285 { 4286 struct clock_ymdhms ymdhms; 4287 uint32_t husec, usec, csec; 4288 4289 (void) clock_secs_to_ymdhms(timespec->tv_sec, &ymdhms); 4290 4291 usec = timespec->tv_nsec / 1000; 4292 husec = usec / 100; 4293 usec -= husec * 100; /* only 0-99 in usec */ 4294 csec = husec / 100; /* only 0-99 in csec */ 4295 husec -= csec * 100; /* only 0-99 in husec */ 4296 4297 /* set method 1 for CUT/GMT */ 4298 timestamp->type_tz = udf_rw16((1<<12) + 0); 4299 timestamp->year = udf_rw16(ymdhms.dt_year); 4300 timestamp->month = ymdhms.dt_mon; 4301 timestamp->day = ymdhms.dt_day; 4302 timestamp->hour = ymdhms.dt_hour; 4303 timestamp->minute = ymdhms.dt_min; 4304 timestamp->second = ymdhms.dt_sec; 4305 timestamp->centisec = csec; 4306 timestamp->hund_usec = husec; 4307 timestamp->usec = usec; 4308 } 4309 4310 /* --------------------------------------------------------------------- */ 4311 4312 /* 4313 * Attribute and filetypes converters with get/set pairs 4314 */ 4315 4316 uint32_t 4317 udf_getaccessmode(struct udf_node *udf_node) 4318 { 4319 struct file_entry *fe = udf_node->fe; 4320 struct extfile_entry *efe = udf_node->efe; 4321 uint32_t udf_perm, icbftype; 4322 uint32_t mode, ftype; 4323 uint16_t icbflags; 4324 4325 UDF_LOCK_NODE(udf_node, 0); 4326 if (fe) { 4327 udf_perm = udf_rw32(fe->perm); 4328 icbftype = fe->icbtag.file_type; 4329 icbflags = udf_rw16(fe->icbtag.flags); 4330 } else { 4331 assert(udf_node->efe); 4332 udf_perm = udf_rw32(efe->perm); 4333 icbftype = efe->icbtag.file_type; 4334 icbflags = udf_rw16(efe->icbtag.flags); 4335 } 4336 4337 mode = udf_perm_to_unix_mode(udf_perm); 4338 ftype = udf_icb_to_unix_filetype(icbftype); 4339 4340 /* set suid, sgid, sticky from flags in fe/efe */ 4341 if (icbflags & UDF_ICB_TAG_FLAGS_SETUID) 4342 mode |= S_ISUID; 4343 if (icbflags & UDF_ICB_TAG_FLAGS_SETGID) 4344 mode |= S_ISGID; 4345 if (icbflags & UDF_ICB_TAG_FLAGS_STICKY) 4346 mode |= S_ISVTX; 4347 4348 UDF_UNLOCK_NODE(udf_node, 0); 4349 4350 return mode | ftype; 4351 } 4352 4353 4354 void 4355 udf_setaccessmode(struct udf_node *udf_node, mode_t mode) 4356 { 4357 struct file_entry *fe = udf_node->fe; 4358 struct extfile_entry *efe = udf_node->efe; 4359 uint32_t udf_perm; 4360 uint16_t icbflags; 4361 4362 UDF_LOCK_NODE(udf_node, 0); 4363 udf_perm = unix_mode_to_udf_perm(mode & ALLPERMS); 4364 if (fe) { 4365 icbflags = udf_rw16(fe->icbtag.flags); 4366 } else { 4367 icbflags = udf_rw16(efe->icbtag.flags); 4368 } 4369 4370 icbflags &= ~UDF_ICB_TAG_FLAGS_SETUID; 4371 icbflags &= ~UDF_ICB_TAG_FLAGS_SETGID; 4372 icbflags &= ~UDF_ICB_TAG_FLAGS_STICKY; 4373 if (mode & S_ISUID) 4374 icbflags |= UDF_ICB_TAG_FLAGS_SETUID; 4375 if (mode & S_ISGID) 4376 icbflags |= UDF_ICB_TAG_FLAGS_SETGID; 4377 if (mode & S_ISVTX) 4378 icbflags |= UDF_ICB_TAG_FLAGS_STICKY; 4379 4380 if (fe) { 4381 fe->perm = udf_rw32(udf_perm); 4382 fe->icbtag.flags = udf_rw16(icbflags); 4383 } else { 4384 efe->perm = udf_rw32(udf_perm); 4385 efe->icbtag.flags = udf_rw16(icbflags); 4386 } 4387 4388 UDF_UNLOCK_NODE(udf_node, 0); 4389 } 4390 4391 4392 void 4393 udf_getownership(struct udf_node *udf_node, uid_t *uidp, gid_t *gidp) 4394 { 4395 struct udf_mount *ump = udf_node->ump; 4396 struct file_entry *fe = udf_node->fe; 4397 struct extfile_entry *efe = udf_node->efe; 4398 uid_t uid; 4399 gid_t gid; 4400 4401 UDF_LOCK_NODE(udf_node, 0); 4402 if (fe) { 4403 uid = (uid_t)udf_rw32(fe->uid); 4404 gid = (gid_t)udf_rw32(fe->gid); 4405 } else { 4406 assert(udf_node->efe); 4407 uid = (uid_t)udf_rw32(efe->uid); 4408 gid = (gid_t)udf_rw32(efe->gid); 4409 } 4410 4411 /* do the uid/gid translation game */ 4412 if (uid == (uid_t) -1) 4413 uid = ump->mount_args.anon_uid; 4414 if (gid == (gid_t) -1) 4415 gid = ump->mount_args.anon_gid; 4416 4417 *uidp = uid; 4418 *gidp = gid; 4419 4420 UDF_UNLOCK_NODE(udf_node, 0); 4421 } 4422 4423 4424 void 4425 udf_setownership(struct udf_node *udf_node, uid_t uid, gid_t gid) 4426 { 4427 struct udf_mount *ump = udf_node->ump; 4428 struct file_entry *fe = udf_node->fe; 4429 struct extfile_entry *efe = udf_node->efe; 4430 uid_t nobody_uid; 4431 gid_t nobody_gid; 4432 4433 UDF_LOCK_NODE(udf_node, 0); 4434 4435 /* do the uid/gid translation game */ 4436 nobody_uid = ump->mount_args.nobody_uid; 4437 nobody_gid = ump->mount_args.nobody_gid; 4438 if (uid == nobody_uid) 4439 uid = (uid_t) -1; 4440 if (gid == nobody_gid) 4441 gid = (gid_t) -1; 4442 4443 if (fe) { 4444 fe->uid = udf_rw32((uint32_t) uid); 4445 fe->gid = udf_rw32((uint32_t) gid); 4446 } else { 4447 efe->uid = udf_rw32((uint32_t) uid); 4448 efe->gid = udf_rw32((uint32_t) gid); 4449 } 4450 4451 UDF_UNLOCK_NODE(udf_node, 0); 4452 } 4453 4454 4455 /* --------------------------------------------------------------------- */ 4456 4457 4458 static int 4459 dirhash_fill(struct udf_node *dir_node) 4460 { 4461 struct vnode *dvp = dir_node->vnode; 4462 struct dirhash *dirh; 4463 struct file_entry *fe = dir_node->fe; 4464 struct extfile_entry *efe = dir_node->efe; 4465 struct fileid_desc *fid; 4466 struct dirent *dirent; 4467 uint64_t file_size, pre_diroffset, diroffset; 4468 uint32_t lb_size; 4469 int error; 4470 4471 /* make sure we have a dirhash to work on */ 4472 dirh = dir_node->dir_hash; 4473 KASSERT(dirh); 4474 KASSERT(dirh->refcnt > 0); 4475 4476 if (dirh->flags & DIRH_BROKEN) 4477 return EIO; 4478 if (dirh->flags & DIRH_COMPLETE) 4479 return 0; 4480 4481 /* make sure we have a clean dirhash to add to */ 4482 dirhash_purge_entries(dirh); 4483 4484 /* get directory filesize */ 4485 if (fe) { 4486 file_size = udf_rw64(fe->inf_len); 4487 } else { 4488 assert(efe); 4489 file_size = udf_rw64(efe->inf_len); 4490 } 4491 4492 /* allocate temporary space for fid */ 4493 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size); 4494 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK); 4495 4496 /* allocate temporary space for dirent */ 4497 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK); 4498 4499 error = 0; 4500 diroffset = 0; 4501 while (diroffset < file_size) { 4502 /* transfer a new fid/dirent */ 4503 pre_diroffset = diroffset; 4504 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent); 4505 if (error) { 4506 /* TODO what to do? continue but not add? */ 4507 dirh->flags |= DIRH_BROKEN; 4508 dirhash_purge_entries(dirh); 4509 break; 4510 } 4511 4512 if ((fid->file_char & UDF_FILE_CHAR_DEL)) { 4513 /* register deleted extent for reuse */ 4514 dirhash_enter_freed(dirh, pre_diroffset, 4515 udf_fidsize(fid)); 4516 } else { 4517 /* append to the dirhash */ 4518 dirhash_enter(dirh, dirent, pre_diroffset, 4519 udf_fidsize(fid), 0); 4520 } 4521 } 4522 dirh->flags |= DIRH_COMPLETE; 4523 4524 free(fid, M_UDFTEMP); 4525 free(dirent, M_UDFTEMP); 4526 4527 return error; 4528 } 4529 4530 4531 /* --------------------------------------------------------------------- */ 4532 4533 /* 4534 * Directory read and manipulation functions. 4535 * 4536 */ 4537 4538 int 4539 udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen, 4540 struct long_ad *icb_loc, int *found) 4541 { 4542 struct udf_node *dir_node = VTOI(vp); 4543 struct dirhash *dirh; 4544 struct dirhash_entry *dirh_ep; 4545 struct fileid_desc *fid; 4546 struct dirent *dirent; 4547 uint64_t diroffset; 4548 uint32_t lb_size; 4549 int hit, error; 4550 4551 /* set default return */ 4552 *found = 0; 4553 4554 /* get our dirhash and make sure its read in */ 4555 dirhash_get(&dir_node->dir_hash); 4556 error = dirhash_fill(dir_node); 4557 if (error) { 4558 dirhash_put(dir_node->dir_hash); 4559 return error; 4560 } 4561 dirh = dir_node->dir_hash; 4562 4563 /* allocate temporary space for fid */ 4564 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size); 4565 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK); 4566 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK); 4567 4568 DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n", 4569 namelen, namelen, name)); 4570 4571 /* search our dirhash hits */ 4572 memset(icb_loc, 0, sizeof(*icb_loc)); 4573 dirh_ep = NULL; 4574 for (;;) { 4575 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep); 4576 /* if no hit, abort the search */ 4577 if (!hit) 4578 break; 4579 4580 /* check this hit */ 4581 diroffset = dirh_ep->offset; 4582 4583 /* transfer a new fid/dirent */ 4584 error = udf_read_fid_stream(vp, &diroffset, fid, dirent); 4585 if (error) 4586 break; 4587 4588 DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n", 4589 dirent->d_namlen, dirent->d_namlen, dirent->d_name)); 4590 4591 /* see if its our entry */ 4592 #ifdef DIAGNOSTIC 4593 if (dirent->d_namlen != namelen) { 4594 printf("WARNING: dirhash_lookup() returned wrong " 4595 "d_namelen: %d and ought to be %d\n", 4596 dirent->d_namlen, namelen); 4597 printf("\tlooked for `%s' and got `%s'\n", 4598 name, dirent->d_name); 4599 } 4600 #endif 4601 if (strncmp(dirent->d_name, name, namelen) == 0) { 4602 *found = 1; 4603 *icb_loc = fid->icb; 4604 break; 4605 } 4606 } 4607 free(fid, M_UDFTEMP); 4608 free(dirent, M_UDFTEMP); 4609 4610 dirhash_put(dir_node->dir_hash); 4611 4612 return error; 4613 } 4614 4615 /* --------------------------------------------------------------------- */ 4616 4617 static int 4618 udf_create_new_fe(struct udf_mount *ump, struct file_entry *fe, int file_type, 4619 struct long_ad *node_icb, struct long_ad *parent_icb, 4620 uint64_t parent_unique_id) 4621 { 4622 struct timespec now; 4623 struct icb_tag *icb; 4624 struct filetimes_extattr_entry *ft_extattr; 4625 uint64_t unique_id; 4626 uint32_t fidsize, lb_num; 4627 uint8_t *bpos; 4628 int crclen, attrlen; 4629 4630 lb_num = udf_rw32(node_icb->loc.lb_num); 4631 udf_inittag(ump, &fe->tag, TAGID_FENTRY, lb_num); 4632 icb = &fe->icbtag; 4633 4634 /* 4635 * Always use strategy type 4 unless on WORM wich we don't support 4636 * (yet). Fill in defaults and set for internal allocation of data. 4637 */ 4638 icb->strat_type = udf_rw16(4); 4639 icb->max_num_entries = udf_rw16(1); 4640 icb->file_type = file_type; /* 8 bit */ 4641 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC); 4642 4643 fe->perm = udf_rw32(0x7fff); /* all is allowed */ 4644 fe->link_cnt = udf_rw16(0); /* explicit setting */ 4645 4646 fe->ckpoint = udf_rw32(1); /* user supplied file version */ 4647 4648 vfs_timestamp(&now); 4649 udf_timespec_to_timestamp(&now, &fe->atime); 4650 udf_timespec_to_timestamp(&now, &fe->attrtime); 4651 udf_timespec_to_timestamp(&now, &fe->mtime); 4652 4653 udf_set_regid(&fe->imp_id, IMPL_NAME); 4654 udf_add_impl_regid(ump, &fe->imp_id); 4655 4656 unique_id = udf_advance_uniqueid(ump); 4657 fe->unique_id = udf_rw64(unique_id); 4658 fe->l_ea = udf_rw32(0); 4659 4660 /* create extended attribute to record our creation time */ 4661 attrlen = UDF_FILETIMES_ATTR_SIZE(1); 4662 ft_extattr = malloc(attrlen, M_UDFTEMP, M_WAITOK); 4663 memset(ft_extattr, 0, attrlen); 4664 ft_extattr->hdr.type = udf_rw32(UDF_FILETIMES_ATTR_NO); 4665 ft_extattr->hdr.subtype = 1; /* [4/48.10.5] */ 4666 ft_extattr->hdr.a_l = udf_rw32(UDF_FILETIMES_ATTR_SIZE(1)); 4667 ft_extattr->d_l = udf_rw32(UDF_TIMESTAMP_SIZE); /* one item */ 4668 ft_extattr->existence = UDF_FILETIMES_FILE_CREATION; 4669 udf_timespec_to_timestamp(&now, &ft_extattr->times[0]); 4670 4671 udf_extattr_insert_internal(ump, (union dscrptr *) fe, 4672 (struct extattr_entry *) ft_extattr); 4673 free(ft_extattr, M_UDFTEMP); 4674 4675 /* if its a directory, create '..' */ 4676 bpos = (uint8_t *) fe->data + udf_rw32(fe->l_ea); 4677 fidsize = 0; 4678 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) { 4679 fidsize = udf_create_parentfid(ump, 4680 (struct fileid_desc *) bpos, parent_icb, 4681 parent_unique_id); 4682 } 4683 4684 /* record fidlength information */ 4685 fe->inf_len = udf_rw64(fidsize); 4686 fe->l_ad = udf_rw32(fidsize); 4687 fe->logblks_rec = udf_rw64(0); /* intern */ 4688 4689 crclen = sizeof(struct file_entry) - 1 - UDF_DESC_TAG_LENGTH; 4690 crclen += udf_rw32(fe->l_ea) + fidsize; 4691 fe->tag.desc_crc_len = udf_rw16(crclen); 4692 4693 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fe); 4694 4695 return fidsize; 4696 } 4697 4698 /* --------------------------------------------------------------------- */ 4699 4700 static int 4701 udf_create_new_efe(struct udf_mount *ump, struct extfile_entry *efe, 4702 int file_type, struct long_ad *node_icb, struct long_ad *parent_icb, 4703 uint64_t parent_unique_id) 4704 { 4705 struct timespec now; 4706 struct icb_tag *icb; 4707 uint64_t unique_id; 4708 uint32_t fidsize, lb_num; 4709 uint8_t *bpos; 4710 int crclen; 4711 4712 lb_num = udf_rw32(node_icb->loc.lb_num); 4713 udf_inittag(ump, &efe->tag, TAGID_EXTFENTRY, lb_num); 4714 icb = &efe->icbtag; 4715 4716 /* 4717 * Always use strategy type 4 unless on WORM wich we don't support 4718 * (yet). Fill in defaults and set for internal allocation of data. 4719 */ 4720 icb->strat_type = udf_rw16(4); 4721 icb->max_num_entries = udf_rw16(1); 4722 icb->file_type = file_type; /* 8 bit */ 4723 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC); 4724 4725 efe->perm = udf_rw32(0x7fff); /* all is allowed */ 4726 efe->link_cnt = udf_rw16(0); /* explicit setting */ 4727 4728 efe->ckpoint = udf_rw32(1); /* user supplied file version */ 4729 4730 vfs_timestamp(&now); 4731 udf_timespec_to_timestamp(&now, &efe->ctime); 4732 udf_timespec_to_timestamp(&now, &efe->atime); 4733 udf_timespec_to_timestamp(&now, &efe->attrtime); 4734 udf_timespec_to_timestamp(&now, &efe->mtime); 4735 4736 udf_set_regid(&efe->imp_id, IMPL_NAME); 4737 udf_add_impl_regid(ump, &efe->imp_id); 4738 4739 unique_id = udf_advance_uniqueid(ump); 4740 efe->unique_id = udf_rw64(unique_id); 4741 efe->l_ea = udf_rw32(0); 4742 4743 /* if its a directory, create '..' */ 4744 bpos = (uint8_t *) efe->data + udf_rw32(efe->l_ea); 4745 fidsize = 0; 4746 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) { 4747 fidsize = udf_create_parentfid(ump, 4748 (struct fileid_desc *) bpos, parent_icb, 4749 parent_unique_id); 4750 } 4751 4752 /* record fidlength information */ 4753 efe->obj_size = udf_rw64(fidsize); 4754 efe->inf_len = udf_rw64(fidsize); 4755 efe->l_ad = udf_rw32(fidsize); 4756 efe->logblks_rec = udf_rw64(0); /* intern */ 4757 4758 crclen = sizeof(struct extfile_entry) - 1 - UDF_DESC_TAG_LENGTH; 4759 crclen += udf_rw32(efe->l_ea) + fidsize; 4760 efe->tag.desc_crc_len = udf_rw16(crclen); 4761 4762 (void) udf_validate_tag_and_crc_sums((union dscrptr *) efe); 4763 4764 return fidsize; 4765 } 4766 4767 /* --------------------------------------------------------------------- */ 4768 4769 int 4770 udf_dir_detach(struct udf_mount *ump, struct udf_node *dir_node, 4771 struct udf_node *udf_node, struct componentname *cnp) 4772 { 4773 struct vnode *dvp = dir_node->vnode; 4774 struct dirhash *dirh; 4775 struct dirhash_entry *dirh_ep; 4776 struct file_entry *fe = dir_node->fe; 4777 struct extfile_entry *efe = dir_node->efe; 4778 struct fileid_desc *fid; 4779 struct dirent *dirent; 4780 uint64_t file_size, diroffset; 4781 uint32_t lb_size, fidsize; 4782 int found, error; 4783 char const *name = cnp->cn_nameptr; 4784 int namelen = cnp->cn_namelen; 4785 int hit, refcnt; 4786 4787 /* get our dirhash and make sure its read in */ 4788 dirhash_get(&dir_node->dir_hash); 4789 error = dirhash_fill(dir_node); 4790 if (error) { 4791 dirhash_put(dir_node->dir_hash); 4792 return error; 4793 } 4794 dirh = dir_node->dir_hash; 4795 4796 /* get directory filesize */ 4797 if (fe) { 4798 file_size = udf_rw64(fe->inf_len); 4799 } else { 4800 assert(efe); 4801 file_size = udf_rw64(efe->inf_len); 4802 } 4803 4804 /* allocate temporary space for fid */ 4805 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size); 4806 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK); 4807 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK); 4808 4809 /* search our dirhash hits */ 4810 found = 0; 4811 dirh_ep = NULL; 4812 for (;;) { 4813 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep); 4814 /* if no hit, abort the search */ 4815 if (!hit) 4816 break; 4817 4818 /* check this hit */ 4819 diroffset = dirh_ep->offset; 4820 4821 /* transfer a new fid/dirent */ 4822 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent); 4823 if (error) 4824 break; 4825 4826 /* see if its our entry */ 4827 KASSERT(dirent->d_namlen == namelen); 4828 if (strncmp(dirent->d_name, name, namelen) == 0) { 4829 found = 1; 4830 break; 4831 } 4832 } 4833 4834 if (!found) 4835 error = ENOENT; 4836 if (error) 4837 goto error_out; 4838 4839 /* mark deleted */ 4840 fid->file_char |= UDF_FILE_CHAR_DEL; 4841 #ifdef UDF_COMPLETE_DELETE 4842 memset(&fid->icb, 0, sizeof(fid->icb)); 4843 #endif 4844 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid); 4845 4846 /* get size of fid and compensate for the read_fid_stream advance */ 4847 fidsize = udf_fidsize(fid); 4848 diroffset -= fidsize; 4849 4850 /* write out */ 4851 error = vn_rdwr(UIO_WRITE, dir_node->vnode, 4852 fid, fidsize, diroffset, 4853 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, 4854 FSCRED, NULL, NULL); 4855 if (error) 4856 goto error_out; 4857 4858 /* get reference count of attached node */ 4859 if (udf_node->fe) { 4860 refcnt = udf_rw16(udf_node->fe->link_cnt); 4861 } else { 4862 KASSERT(udf_node->efe); 4863 refcnt = udf_rw16(udf_node->efe->link_cnt); 4864 } 4865 #ifdef UDF_COMPLETE_DELETE 4866 /* substract reference counter in attached node */ 4867 refcnt -= 1; 4868 if (udf_node->fe) { 4869 udf_node->fe->link_cnt = udf_rw16(refcnt); 4870 } else { 4871 udf_node->efe->link_cnt = udf_rw16(refcnt); 4872 } 4873 4874 /* prevent writeout when refcnt == 0 */ 4875 if (refcnt == 0) 4876 udf_node->i_flags |= IN_DELETED; 4877 4878 if (fid->file_char & UDF_FILE_CHAR_DIR) { 4879 int drefcnt; 4880 4881 /* substract reference counter in directory node */ 4882 /* note subtract 2 (?) for its was also backreferenced */ 4883 if (dir_node->fe) { 4884 drefcnt = udf_rw16(dir_node->fe->link_cnt); 4885 drefcnt -= 1; 4886 dir_node->fe->link_cnt = udf_rw16(drefcnt); 4887 } else { 4888 KASSERT(dir_node->efe); 4889 drefcnt = udf_rw16(dir_node->efe->link_cnt); 4890 drefcnt -= 1; 4891 dir_node->efe->link_cnt = udf_rw16(drefcnt); 4892 } 4893 } 4894 4895 udf_node->i_flags |= IN_MODIFIED; 4896 dir_node->i_flags |= IN_MODIFIED; 4897 #endif 4898 /* if it is/was a hardlink adjust the file count */ 4899 if (refcnt > 0) 4900 udf_adjust_filecount(udf_node, -1); 4901 4902 /* remove from the dirhash */ 4903 dirhash_remove(dirh, dirent, diroffset, 4904 udf_fidsize(fid)); 4905 4906 error_out: 4907 free(fid, M_UDFTEMP); 4908 free(dirent, M_UDFTEMP); 4909 4910 dirhash_put(dir_node->dir_hash); 4911 4912 return error; 4913 } 4914 4915 /* --------------------------------------------------------------------- */ 4916 4917 int 4918 udf_dir_update_rootentry(struct udf_mount *ump, struct udf_node *dir_node, 4919 struct udf_node *new_parent_node) 4920 { 4921 struct vnode *dvp = dir_node->vnode; 4922 struct dirhash *dirh; 4923 struct dirhash_entry *dirh_ep; 4924 struct file_entry *fe; 4925 struct extfile_entry *efe; 4926 struct fileid_desc *fid; 4927 struct dirent *dirent; 4928 uint64_t file_size, diroffset; 4929 uint64_t new_parent_unique_id; 4930 uint32_t lb_size, fidsize; 4931 int found, error; 4932 char const *name = ".."; 4933 int namelen = 2; 4934 int hit; 4935 4936 /* get our dirhash and make sure its read in */ 4937 dirhash_get(&dir_node->dir_hash); 4938 error = dirhash_fill(dir_node); 4939 if (error) { 4940 dirhash_put(dir_node->dir_hash); 4941 return error; 4942 } 4943 dirh = dir_node->dir_hash; 4944 4945 /* get new parent's unique ID */ 4946 fe = new_parent_node->fe; 4947 efe = new_parent_node->efe; 4948 if (fe) { 4949 new_parent_unique_id = udf_rw64(fe->unique_id); 4950 } else { 4951 assert(efe); 4952 new_parent_unique_id = udf_rw64(efe->unique_id); 4953 } 4954 4955 /* get directory filesize */ 4956 fe = dir_node->fe; 4957 efe = dir_node->efe; 4958 if (fe) { 4959 file_size = udf_rw64(fe->inf_len); 4960 } else { 4961 assert(efe); 4962 file_size = udf_rw64(efe->inf_len); 4963 } 4964 4965 /* allocate temporary space for fid */ 4966 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size); 4967 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK); 4968 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK); 4969 4970 /* 4971 * NOTE the standard does not dictate the FID entry '..' should be 4972 * first, though in practice it will most likely be. 4973 */ 4974 4975 /* search our dirhash hits */ 4976 found = 0; 4977 dirh_ep = NULL; 4978 for (;;) { 4979 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep); 4980 /* if no hit, abort the search */ 4981 if (!hit) 4982 break; 4983 4984 /* check this hit */ 4985 diroffset = dirh_ep->offset; 4986 4987 /* transfer a new fid/dirent */ 4988 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent); 4989 if (error) 4990 break; 4991 4992 /* see if its our entry */ 4993 KASSERT(dirent->d_namlen == namelen); 4994 if (strncmp(dirent->d_name, name, namelen) == 0) { 4995 found = 1; 4996 break; 4997 } 4998 } 4999 5000 if (!found) 5001 error = ENOENT; 5002 if (error) 5003 goto error_out; 5004 5005 /* update our ICB to the new parent, hit of lower 32 bits of uniqueid */ 5006 fid->icb = new_parent_node->write_loc; 5007 fid->icb.longad_uniqueid = udf_rw32(new_parent_unique_id); 5008 5009 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid); 5010 5011 /* get size of fid and compensate for the read_fid_stream advance */ 5012 fidsize = udf_fidsize(fid); 5013 diroffset -= fidsize; 5014 5015 /* write out */ 5016 error = vn_rdwr(UIO_WRITE, dir_node->vnode, 5017 fid, fidsize, diroffset, 5018 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, 5019 FSCRED, NULL, NULL); 5020 5021 /* nothing to be done in the dirhash */ 5022 5023 error_out: 5024 free(fid, M_UDFTEMP); 5025 free(dirent, M_UDFTEMP); 5026 5027 dirhash_put(dir_node->dir_hash); 5028 5029 return error; 5030 } 5031 5032 /* --------------------------------------------------------------------- */ 5033 5034 /* 5035 * We are not allowed to split the fid tag itself over an logical block so 5036 * check the space remaining in the logical block. 5037 * 5038 * We try to select the smallest candidate for recycling or when none is 5039 * found, append a new one at the end of the directory. 5040 */ 5041 5042 int 5043 udf_dir_attach(struct udf_mount *ump, struct udf_node *dir_node, 5044 struct udf_node *udf_node, struct vattr *vap, struct componentname *cnp) 5045 { 5046 struct vnode *dvp = dir_node->vnode; 5047 struct dirhash *dirh; 5048 struct dirhash_entry *dirh_ep; 5049 struct fileid_desc *fid; 5050 struct icb_tag *icbtag; 5051 struct charspec osta_charspec; 5052 struct dirent dirent; 5053 uint64_t unique_id, dir_size; 5054 uint64_t fid_pos, end_fid_pos, chosen_fid_pos; 5055 uint32_t chosen_size, chosen_size_diff; 5056 int lb_size, lb_rest, fidsize, this_fidsize, size_diff; 5057 int file_char, refcnt, icbflags, addr_type, hit, error; 5058 5059 /* get our dirhash and make sure its read in */ 5060 dirhash_get(&dir_node->dir_hash); 5061 error = dirhash_fill(dir_node); 5062 if (error) { 5063 dirhash_put(dir_node->dir_hash); 5064 return error; 5065 } 5066 dirh = dir_node->dir_hash; 5067 5068 /* get info */ 5069 lb_size = udf_rw32(ump->logical_vol->lb_size); 5070 udf_osta_charset(&osta_charspec); 5071 5072 if (dir_node->fe) { 5073 dir_size = udf_rw64(dir_node->fe->inf_len); 5074 icbtag = &dir_node->fe->icbtag; 5075 } else { 5076 dir_size = udf_rw64(dir_node->efe->inf_len); 5077 icbtag = &dir_node->efe->icbtag; 5078 } 5079 5080 icbflags = udf_rw16(icbtag->flags); 5081 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 5082 5083 if (udf_node->fe) { 5084 unique_id = udf_rw64(udf_node->fe->unique_id); 5085 refcnt = udf_rw16(udf_node->fe->link_cnt); 5086 } else { 5087 unique_id = udf_rw64(udf_node->efe->unique_id); 5088 refcnt = udf_rw16(udf_node->efe->link_cnt); 5089 } 5090 5091 if (refcnt > 0) { 5092 unique_id = udf_advance_uniqueid(ump); 5093 udf_adjust_filecount(udf_node, 1); 5094 } 5095 5096 /* determine file characteristics */ 5097 file_char = 0; /* visible non deleted file and not stream metadata */ 5098 if (vap->va_type == VDIR) 5099 file_char = UDF_FILE_CHAR_DIR; 5100 5101 /* malloc scrap buffer */ 5102 fid = malloc(lb_size, M_TEMP, M_WAITOK|M_ZERO); 5103 5104 /* calculate _minimum_ fid size */ 5105 unix_to_udf_name((char *) fid->data, &fid->l_fi, 5106 cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec); 5107 fidsize = UDF_FID_SIZE + fid->l_fi; 5108 fidsize = (fidsize + 3) & ~3; /* multiple of 4 */ 5109 5110 /* find position that will fit the FID */ 5111 chosen_fid_pos = dir_size; 5112 chosen_size = 0; 5113 chosen_size_diff = UINT_MAX; 5114 5115 /* shut up gcc */ 5116 dirent.d_namlen = 0; 5117 5118 /* search our dirhash hits */ 5119 error = 0; 5120 dirh_ep = NULL; 5121 for (;;) { 5122 hit = dirhash_lookup_freed(dirh, fidsize, &dirh_ep); 5123 /* if no hit, abort the search */ 5124 if (!hit) 5125 break; 5126 5127 /* check this hit for size */ 5128 this_fidsize = dirh_ep->entry_size; 5129 5130 /* check this hit */ 5131 fid_pos = dirh_ep->offset; 5132 end_fid_pos = fid_pos + this_fidsize; 5133 size_diff = this_fidsize - fidsize; 5134 lb_rest = lb_size - (end_fid_pos % lb_size); 5135 5136 #ifndef UDF_COMPLETE_DELETE 5137 /* transfer a new fid/dirent */ 5138 error = udf_read_fid_stream(vp, &fid_pos, fid, dirent); 5139 if (error) 5140 goto error_out; 5141 5142 /* only reuse entries that are wiped */ 5143 /* check if the len + loc are marked zero */ 5144 if (udf_rw32(fid->icb.len) != 0) 5145 continue; 5146 if (udf_rw32(fid->icb.loc.lb_num) != 0) 5147 continue; 5148 if (udf_rw16(fid->icb.loc.part_num) != 0) 5149 continue; 5150 #endif /* UDF_COMPLETE_DELETE */ 5151 5152 /* select if not splitting the tag and its smaller */ 5153 if ((size_diff >= 0) && 5154 (size_diff < chosen_size_diff) && 5155 (lb_rest >= sizeof(struct desc_tag))) 5156 { 5157 /* UDF 2.3.4.2+3 specifies rules for iu size */ 5158 if ((size_diff == 0) || (size_diff >= 32)) { 5159 chosen_fid_pos = fid_pos; 5160 chosen_size = this_fidsize; 5161 chosen_size_diff = size_diff; 5162 } 5163 } 5164 } 5165 5166 5167 /* extend directory if no other candidate found */ 5168 if (chosen_size == 0) { 5169 chosen_fid_pos = dir_size; 5170 chosen_size = fidsize; 5171 chosen_size_diff = 0; 5172 5173 /* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */ 5174 if (addr_type == UDF_ICB_INTERN_ALLOC) { 5175 /* pre-grow directory to see if we're to switch */ 5176 udf_grow_node(dir_node, dir_size + chosen_size); 5177 5178 icbflags = udf_rw16(icbtag->flags); 5179 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 5180 } 5181 5182 /* make sure the next fid desc_tag won't be splitted */ 5183 if (addr_type != UDF_ICB_INTERN_ALLOC) { 5184 end_fid_pos = chosen_fid_pos + chosen_size; 5185 lb_rest = lb_size - (end_fid_pos % lb_size); 5186 5187 /* pad with implementation use regid if needed */ 5188 if (lb_rest < sizeof(struct desc_tag)) 5189 chosen_size += 32; 5190 } 5191 } 5192 chosen_size_diff = chosen_size - fidsize; 5193 5194 /* populate the FID */ 5195 memset(fid, 0, lb_size); 5196 udf_inittag(ump, &fid->tag, TAGID_FID, 0); 5197 fid->file_version_num = udf_rw16(1); /* UDF 2.3.4.1 */ 5198 fid->file_char = file_char; 5199 fid->icb = udf_node->loc; 5200 fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id); 5201 fid->l_iu = udf_rw16(0); 5202 5203 if (chosen_size > fidsize) { 5204 /* insert implementation-use regid to space it correctly */ 5205 fid->l_iu = udf_rw16(chosen_size_diff); 5206 5207 /* set implementation use */ 5208 udf_set_regid((struct regid *) fid->data, IMPL_NAME); 5209 udf_add_impl_regid(ump, (struct regid *) fid->data); 5210 } 5211 5212 /* fill in name */ 5213 unix_to_udf_name((char *) fid->data + udf_rw16(fid->l_iu), 5214 &fid->l_fi, cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec); 5215 5216 fid->tag.desc_crc_len = udf_rw16(chosen_size - UDF_DESC_TAG_LENGTH); 5217 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid); 5218 5219 /* writeout FID/update parent directory */ 5220 error = vn_rdwr(UIO_WRITE, dvp, 5221 fid, chosen_size, chosen_fid_pos, 5222 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, 5223 FSCRED, NULL, NULL); 5224 5225 if (error) 5226 goto error_out; 5227 5228 /* add reference counter in attached node */ 5229 if (udf_node->fe) { 5230 refcnt = udf_rw16(udf_node->fe->link_cnt); 5231 udf_node->fe->link_cnt = udf_rw16(refcnt+1); 5232 } else { 5233 KASSERT(udf_node->efe); 5234 refcnt = udf_rw16(udf_node->efe->link_cnt); 5235 udf_node->efe->link_cnt = udf_rw16(refcnt+1); 5236 } 5237 5238 /* mark not deleted if it was... just in case, but do warn */ 5239 if (udf_node->i_flags & IN_DELETED) { 5240 printf("udf: warning, marking a file undeleted\n"); 5241 udf_node->i_flags &= ~IN_DELETED; 5242 } 5243 5244 if (file_char & UDF_FILE_CHAR_DIR) { 5245 /* add reference counter in directory node for '..' */ 5246 if (dir_node->fe) { 5247 refcnt = udf_rw16(dir_node->fe->link_cnt); 5248 refcnt++; 5249 dir_node->fe->link_cnt = udf_rw16(refcnt); 5250 } else { 5251 KASSERT(dir_node->efe); 5252 refcnt = udf_rw16(dir_node->efe->link_cnt); 5253 refcnt++; 5254 dir_node->efe->link_cnt = udf_rw16(refcnt); 5255 } 5256 } 5257 5258 /* append to the dirhash */ 5259 /* NOTE do not use dirent anymore or it won't match later! */ 5260 udf_to_unix_name(dirent.d_name, MAXNAMLEN, 5261 (char *) fid->data + udf_rw16(fid->l_iu), fid->l_fi, &osta_charspec); 5262 dirent.d_namlen = strlen(dirent.d_name); 5263 dirhash_enter(dirh, &dirent, chosen_fid_pos, 5264 udf_fidsize(fid), 1); 5265 5266 /* note updates */ 5267 udf_node->i_flags |= IN_CHANGE | IN_MODIFY; /* | IN_CREATE? */ 5268 /* VN_KNOTE(udf_node, ...) */ 5269 udf_update(udf_node->vnode, NULL, NULL, NULL, 0); 5270 5271 error_out: 5272 free(fid, M_TEMP); 5273 5274 dirhash_put(dir_node->dir_hash); 5275 5276 return error; 5277 } 5278 5279 /* --------------------------------------------------------------------- */ 5280 5281 /* 5282 * Each node can have an attached streamdir node though not recursively. These 5283 * are otherwise known as named substreams/named extended attributes that have 5284 * no size limitations. 5285 * 5286 * `Normal' extended attributes are indicated with a number and are recorded 5287 * in either the fe/efe descriptor itself for small descriptors or recorded in 5288 * the attached extended attribute file. Since these spaces can get 5289 * fragmented, care ought to be taken. 5290 * 5291 * Since the size of the space reserved for allocation descriptors is limited, 5292 * there is a mechanim provided for extending this space; this is done by a 5293 * special extent to allow schrinking of the allocations without breaking the 5294 * linkage to the allocation extent descriptor. 5295 */ 5296 5297 int 5298 udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc, 5299 struct udf_node **udf_noderes) 5300 { 5301 union dscrptr *dscr; 5302 struct udf_node *udf_node; 5303 struct vnode *nvp; 5304 struct long_ad icb_loc, last_fe_icb_loc; 5305 uint64_t file_size; 5306 uint32_t lb_size, sector, dummy; 5307 uint8_t *file_data; 5308 int udf_file_type, dscr_type, strat, strat4096, needs_indirect; 5309 int slot, eof, error; 5310 5311 DPRINTF(NODE, ("udf_get_node called\n")); 5312 *udf_noderes = udf_node = NULL; 5313 5314 /* lock to disallow simultanious creation of same udf_node */ 5315 mutex_enter(&ump->get_node_lock); 5316 5317 DPRINTF(NODE, ("\tlookup in hash table\n")); 5318 /* lookup in hash table */ 5319 assert(ump); 5320 assert(node_icb_loc); 5321 udf_node = udf_node_lookup(ump, node_icb_loc); 5322 if (udf_node) { 5323 DPRINTF(NODE, ("\tgot it from the hash!\n")); 5324 /* vnode is returned locked */ 5325 *udf_noderes = udf_node; 5326 mutex_exit(&ump->get_node_lock); 5327 return 0; 5328 } 5329 5330 /* garbage check: translate udf_node_icb_loc to sectornr */ 5331 error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy); 5332 if (error) { 5333 DPRINTF(NODE, ("\tcan't translate icb address!\n")); 5334 /* no use, this will fail anyway */ 5335 mutex_exit(&ump->get_node_lock); 5336 return EINVAL; 5337 } 5338 5339 /* build udf_node (do initialise!) */ 5340 udf_node = pool_get(&udf_node_pool, PR_WAITOK); 5341 memset(udf_node, 0, sizeof(struct udf_node)); 5342 5343 DPRINTF(NODE, ("\tget new vnode\n")); 5344 /* give it a vnode */ 5345 error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp); 5346 if (error) { 5347 pool_put(&udf_node_pool, udf_node); 5348 mutex_exit(&ump->get_node_lock); 5349 return error; 5350 } 5351 5352 /* always return locked vnode */ 5353 if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) { 5354 /* recycle vnode and unlock; simultanious will fail too */ 5355 ungetnewvnode(nvp); 5356 mutex_exit(&ump->get_node_lock); 5357 return error; 5358 } 5359 5360 /* initialise crosslinks, note location of fe/efe for hashing */ 5361 udf_node->ump = ump; 5362 udf_node->vnode = nvp; 5363 nvp->v_data = udf_node; 5364 udf_node->loc = *node_icb_loc; 5365 udf_node->lockf = 0; 5366 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE); 5367 cv_init(&udf_node->node_lock, "udf_nlk"); 5368 genfs_node_init(nvp, &udf_genfsops); /* inititise genfs */ 5369 udf_node->outstanding_bufs = 0; 5370 udf_node->outstanding_nodedscr = 0; 5371 udf_node->uncommitted_lbs = 0; 5372 5373 /* check if we're fetching the root */ 5374 if (ump->fileset_desc) 5375 if (memcmp(&udf_node->loc, &ump->fileset_desc->rootdir_icb, 5376 sizeof(struct long_ad)) == 0) 5377 nvp->v_vflag |= VV_ROOT; 5378 5379 /* insert into the hash lookup */ 5380 udf_register_node(udf_node); 5381 5382 /* safe to unlock, the entry is in the hash table, vnode is locked */ 5383 mutex_exit(&ump->get_node_lock); 5384 5385 icb_loc = *node_icb_loc; 5386 needs_indirect = 0; 5387 strat4096 = 0; 5388 udf_file_type = UDF_ICB_FILETYPE_UNKNOWN; 5389 file_size = 0; 5390 file_data = NULL; 5391 lb_size = udf_rw32(ump->logical_vol->lb_size); 5392 5393 DPRINTF(NODE, ("\tstart reading descriptors\n")); 5394 do { 5395 /* try to read in fe/efe */ 5396 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr); 5397 5398 /* blank sector marks end of sequence, check this */ 5399 if ((dscr == NULL) && (!strat4096)) 5400 error = ENOENT; 5401 5402 /* break if read error or blank sector */ 5403 if (error || (dscr == NULL)) 5404 break; 5405 5406 /* process descriptor based on the descriptor type */ 5407 dscr_type = udf_rw16(dscr->tag.id); 5408 DPRINTF(NODE, ("\tread descriptor %d\n", dscr_type)); 5409 5410 /* if dealing with an indirect entry, follow the link */ 5411 if (dscr_type == TAGID_INDIRECTENTRY) { 5412 needs_indirect = 0; 5413 udf_free_logvol_dscr(ump, &icb_loc, dscr); 5414 icb_loc = dscr->inde.indirect_icb; 5415 continue; 5416 } 5417 5418 /* only file entries and extended file entries allowed here */ 5419 if ((dscr_type != TAGID_FENTRY) && 5420 (dscr_type != TAGID_EXTFENTRY)) { 5421 udf_free_logvol_dscr(ump, &icb_loc, dscr); 5422 error = ENOENT; 5423 break; 5424 } 5425 5426 KASSERT(udf_tagsize(dscr, lb_size) == lb_size); 5427 5428 /* choose this one */ 5429 last_fe_icb_loc = icb_loc; 5430 5431 /* record and process/update (ext)fentry */ 5432 file_data = NULL; 5433 if (dscr_type == TAGID_FENTRY) { 5434 if (udf_node->fe) 5435 udf_free_logvol_dscr(ump, &last_fe_icb_loc, 5436 udf_node->fe); 5437 udf_node->fe = &dscr->fe; 5438 strat = udf_rw16(udf_node->fe->icbtag.strat_type); 5439 udf_file_type = udf_node->fe->icbtag.file_type; 5440 file_size = udf_rw64(udf_node->fe->inf_len); 5441 file_data = udf_node->fe->data; 5442 } else { 5443 if (udf_node->efe) 5444 udf_free_logvol_dscr(ump, &last_fe_icb_loc, 5445 udf_node->efe); 5446 udf_node->efe = &dscr->efe; 5447 strat = udf_rw16(udf_node->efe->icbtag.strat_type); 5448 udf_file_type = udf_node->efe->icbtag.file_type; 5449 file_size = udf_rw64(udf_node->efe->inf_len); 5450 file_data = udf_node->efe->data; 5451 } 5452 5453 /* check recording strategy (structure) */ 5454 5455 /* 5456 * Strategy 4096 is a daisy linked chain terminating with an 5457 * unrecorded sector or a TERM descriptor. The next 5458 * descriptor is to be found in the sector that follows the 5459 * current sector. 5460 */ 5461 if (strat == 4096) { 5462 strat4096 = 1; 5463 needs_indirect = 1; 5464 5465 icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1; 5466 } 5467 5468 /* 5469 * Strategy 4 is the normal strategy and terminates, but if 5470 * we're in strategy 4096, we can't have strategy 4 mixed in 5471 */ 5472 5473 if (strat == 4) { 5474 if (strat4096) { 5475 error = EINVAL; 5476 break; 5477 } 5478 break; /* done */ 5479 } 5480 } while (!error); 5481 5482 /* first round of cleanup code */ 5483 if (error) { 5484 DPRINTF(NODE, ("\tnode fe/efe failed!\n")); 5485 /* recycle udf_node */ 5486 udf_dispose_node(udf_node); 5487 5488 VOP_UNLOCK(nvp); 5489 nvp->v_data = NULL; 5490 ungetnewvnode(nvp); 5491 5492 return EINVAL; /* error code ok? */ 5493 } 5494 DPRINTF(NODE, ("\tnode fe/efe read in fine\n")); 5495 5496 /* assert no references to dscr anymore beyong this point */ 5497 assert((udf_node->fe) || (udf_node->efe)); 5498 dscr = NULL; 5499 5500 /* 5501 * Remember where to record an updated version of the descriptor. If 5502 * there is a sequence of indirect entries, icb_loc will have been 5503 * updated. Its the write disipline to allocate new space and to make 5504 * sure the chain is maintained. 5505 * 5506 * `needs_indirect' flags if the next location is to be filled with 5507 * with an indirect entry. 5508 */ 5509 udf_node->write_loc = icb_loc; 5510 udf_node->needs_indirect = needs_indirect; 5511 5512 /* 5513 * Go trough all allocations extents of this descriptor and when 5514 * encountering a redirect read in the allocation extension. These are 5515 * daisy-chained. 5516 */ 5517 UDF_LOCK_NODE(udf_node, 0); 5518 udf_node->num_extensions = 0; 5519 5520 error = 0; 5521 slot = 0; 5522 for (;;) { 5523 udf_get_adslot(udf_node, slot, &icb_loc, &eof); 5524 DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, " 5525 "lb_num = %d, part = %d\n", slot, eof, 5526 UDF_EXT_FLAGS(udf_rw32(icb_loc.len)), 5527 UDF_EXT_LEN(udf_rw32(icb_loc.len)), 5528 udf_rw32(icb_loc.loc.lb_num), 5529 udf_rw16(icb_loc.loc.part_num))); 5530 if (eof) 5531 break; 5532 slot++; 5533 5534 if (UDF_EXT_FLAGS(udf_rw32(icb_loc.len)) != UDF_EXT_REDIRECT) 5535 continue; 5536 5537 DPRINTF(NODE, ("\tgot redirect extent\n")); 5538 if (udf_node->num_extensions >= UDF_MAX_ALLOC_EXTENTS) { 5539 DPRINTF(ALLOC, ("udf_get_node: implementation limit, " 5540 "too many allocation extensions on " 5541 "udf_node\n")); 5542 error = EINVAL; 5543 break; 5544 } 5545 5546 /* length can only be *one* lb : UDF 2.50/2.3.7.1 */ 5547 if (UDF_EXT_LEN(udf_rw32(icb_loc.len)) != lb_size) { 5548 DPRINTF(ALLOC, ("udf_get_node: bad allocation " 5549 "extension size in udf_node\n")); 5550 error = EINVAL; 5551 break; 5552 } 5553 5554 DPRINTF(NODE, ("read allocation extent at lb_num %d\n", 5555 UDF_EXT_LEN(udf_rw32(icb_loc.loc.lb_num)))); 5556 /* load in allocation extent */ 5557 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr); 5558 if (error || (dscr == NULL)) 5559 break; 5560 5561 /* process read-in descriptor */ 5562 dscr_type = udf_rw16(dscr->tag.id); 5563 5564 if (dscr_type != TAGID_ALLOCEXTENT) { 5565 udf_free_logvol_dscr(ump, &icb_loc, dscr); 5566 error = ENOENT; 5567 break; 5568 } 5569 5570 DPRINTF(NODE, ("\trecording redirect extent\n")); 5571 udf_node->ext[udf_node->num_extensions] = &dscr->aee; 5572 udf_node->ext_loc[udf_node->num_extensions] = icb_loc; 5573 5574 udf_node->num_extensions++; 5575 5576 } /* while */ 5577 UDF_UNLOCK_NODE(udf_node, 0); 5578 5579 /* second round of cleanup code */ 5580 if (error) { 5581 /* recycle udf_node */ 5582 udf_dispose_node(udf_node); 5583 5584 VOP_UNLOCK(nvp); 5585 nvp->v_data = NULL; 5586 ungetnewvnode(nvp); 5587 5588 return EINVAL; /* error code ok? */ 5589 } 5590 5591 DPRINTF(NODE, ("\tnode read in fine\n")); 5592 5593 /* 5594 * Translate UDF filetypes into vnode types. 5595 * 5596 * Systemfiles like the meta main and mirror files are not treated as 5597 * normal files, so we type them as having no type. UDF dictates that 5598 * they are not allowed to be visible. 5599 */ 5600 5601 switch (udf_file_type) { 5602 case UDF_ICB_FILETYPE_DIRECTORY : 5603 case UDF_ICB_FILETYPE_STREAMDIR : 5604 nvp->v_type = VDIR; 5605 break; 5606 case UDF_ICB_FILETYPE_BLOCKDEVICE : 5607 nvp->v_type = VBLK; 5608 break; 5609 case UDF_ICB_FILETYPE_CHARDEVICE : 5610 nvp->v_type = VCHR; 5611 break; 5612 case UDF_ICB_FILETYPE_SOCKET : 5613 nvp->v_type = VSOCK; 5614 break; 5615 case UDF_ICB_FILETYPE_FIFO : 5616 nvp->v_type = VFIFO; 5617 break; 5618 case UDF_ICB_FILETYPE_SYMLINK : 5619 nvp->v_type = VLNK; 5620 break; 5621 case UDF_ICB_FILETYPE_VAT : 5622 case UDF_ICB_FILETYPE_META_MAIN : 5623 case UDF_ICB_FILETYPE_META_MIRROR : 5624 nvp->v_type = VNON; 5625 break; 5626 case UDF_ICB_FILETYPE_RANDOMACCESS : 5627 case UDF_ICB_FILETYPE_REALTIME : 5628 nvp->v_type = VREG; 5629 break; 5630 default: 5631 /* YIKES, something else */ 5632 nvp->v_type = VNON; 5633 } 5634 5635 /* TODO specfs, fifofs etc etc. vnops setting */ 5636 5637 /* don't forget to set vnode's v_size */ 5638 uvm_vnp_setsize(nvp, file_size); 5639 5640 /* TODO ext attr and streamdir udf_nodes */ 5641 5642 *udf_noderes = udf_node; 5643 5644 return 0; 5645 } 5646 5647 /* --------------------------------------------------------------------- */ 5648 5649 int 5650 udf_writeout_node(struct udf_node *udf_node, int waitfor) 5651 { 5652 union dscrptr *dscr; 5653 struct long_ad *loc; 5654 int extnr, error; 5655 5656 DPRINTF(NODE, ("udf_writeout_node called\n")); 5657 5658 KASSERT(udf_node->outstanding_bufs == 0); 5659 KASSERT(udf_node->outstanding_nodedscr == 0); 5660 5661 KASSERT(LIST_EMPTY(&udf_node->vnode->v_dirtyblkhd)); 5662 5663 if (udf_node->i_flags & IN_DELETED) { 5664 DPRINTF(NODE, ("\tnode deleted; not writing out\n")); 5665 udf_cleanup_reservation(udf_node); 5666 return 0; 5667 } 5668 5669 /* lock node; unlocked in callback */ 5670 UDF_LOCK_NODE(udf_node, 0); 5671 5672 /* remove pending reservations, we're written out */ 5673 udf_cleanup_reservation(udf_node); 5674 5675 /* at least one descriptor writeout */ 5676 udf_node->outstanding_nodedscr = 1; 5677 5678 /* we're going to write out the descriptor so clear the flags */ 5679 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED); 5680 5681 /* if we were rebuild, write out the allocation extents */ 5682 if (udf_node->i_flags & IN_NODE_REBUILD) { 5683 /* mark outstanding node descriptors and issue them */ 5684 udf_node->outstanding_nodedscr += udf_node->num_extensions; 5685 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) { 5686 loc = &udf_node->ext_loc[extnr]; 5687 dscr = (union dscrptr *) udf_node->ext[extnr]; 5688 error = udf_write_logvol_dscr(udf_node, dscr, loc, 0); 5689 if (error) 5690 return error; 5691 } 5692 /* mark allocation extents written out */ 5693 udf_node->i_flags &= ~(IN_NODE_REBUILD); 5694 } 5695 5696 if (udf_node->fe) { 5697 KASSERT(udf_node->efe == NULL); 5698 dscr = (union dscrptr *) udf_node->fe; 5699 } else { 5700 KASSERT(udf_node->efe); 5701 KASSERT(udf_node->fe == NULL); 5702 dscr = (union dscrptr *) udf_node->efe; 5703 } 5704 KASSERT(dscr); 5705 5706 loc = &udf_node->write_loc; 5707 error = udf_write_logvol_dscr(udf_node, dscr, loc, waitfor); 5708 5709 return error; 5710 } 5711 5712 /* --------------------------------------------------------------------- */ 5713 5714 int 5715 udf_dispose_node(struct udf_node *udf_node) 5716 { 5717 struct vnode *vp; 5718 int extnr; 5719 5720 DPRINTF(NODE, ("udf_dispose_node called on node %p\n", udf_node)); 5721 if (!udf_node) { 5722 DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n")); 5723 return 0; 5724 } 5725 5726 vp = udf_node->vnode; 5727 #ifdef DIAGNOSTIC 5728 if (vp->v_numoutput) 5729 panic("disposing UDF node with pending I/O's, udf_node = %p, " 5730 "v_numoutput = %d", udf_node, vp->v_numoutput); 5731 #endif 5732 5733 udf_cleanup_reservation(udf_node); 5734 5735 /* TODO extended attributes and streamdir */ 5736 5737 /* remove dirhash if present */ 5738 dirhash_purge(&udf_node->dir_hash); 5739 5740 /* remove from our hash lookup table */ 5741 udf_deregister_node(udf_node); 5742 5743 /* destroy our lock */ 5744 mutex_destroy(&udf_node->node_mutex); 5745 cv_destroy(&udf_node->node_lock); 5746 5747 /* dissociate our udf_node from the vnode */ 5748 genfs_node_destroy(udf_node->vnode); 5749 vp->v_data = NULL; 5750 5751 /* free associated memory and the node itself */ 5752 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) { 5753 udf_free_logvol_dscr(udf_node->ump, &udf_node->ext_loc[extnr], 5754 udf_node->ext[extnr]); 5755 udf_node->ext[extnr] = (void *) 0xdeadcccc; 5756 } 5757 5758 if (udf_node->fe) 5759 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc, 5760 udf_node->fe); 5761 if (udf_node->efe) 5762 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc, 5763 udf_node->efe); 5764 5765 udf_node->fe = (void *) 0xdeadaaaa; 5766 udf_node->efe = (void *) 0xdeadbbbb; 5767 udf_node->ump = (void *) 0xdeadbeef; 5768 pool_put(&udf_node_pool, udf_node); 5769 5770 return 0; 5771 } 5772 5773 5774 5775 /* 5776 * create a new node using the specified vnodeops, vap and cnp but with the 5777 * udf_file_type. This allows special files to be created. Use with care. 5778 */ 5779 5780 static int 5781 udf_create_node_raw(struct vnode *dvp, struct vnode **vpp, int udf_file_type, 5782 int (**vnodeops)(void *), struct vattr *vap, struct componentname *cnp) 5783 { 5784 union dscrptr *dscr; 5785 struct udf_node *dir_node = VTOI(dvp); 5786 struct udf_node *udf_node; 5787 struct udf_mount *ump = dir_node->ump; 5788 struct vnode *nvp; 5789 struct long_ad node_icb_loc; 5790 uint64_t parent_unique_id; 5791 uint64_t lmapping; 5792 uint32_t lb_size, lb_num; 5793 uint16_t vpart_num; 5794 uid_t uid; 5795 gid_t gid, parent_gid; 5796 int fid_size, error; 5797 5798 lb_size = udf_rw32(ump->logical_vol->lb_size); 5799 *vpp = NULL; 5800 5801 /* allocate vnode */ 5802 error = getnewvnode(VT_UDF, ump->vfs_mountp, vnodeops, &nvp); 5803 if (error) 5804 return error; 5805 5806 /* lock node */ 5807 error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY); 5808 if (error) 5809 goto error_out_unget; 5810 5811 /* reserve space for one logical block */ 5812 vpart_num = ump->node_part; 5813 error = udf_reserve_space(ump, NULL, UDF_C_NODE, 5814 vpart_num, 1, /* can_fail */ true); 5815 if (error) 5816 goto error_out_unlock; 5817 5818 /* allocate node */ 5819 error = udf_allocate_space(ump, NULL, UDF_C_NODE, 5820 vpart_num, 1, &lmapping); 5821 if (error) 5822 goto error_out_unreserve; 5823 lb_num = lmapping; 5824 5825 /* initialise pointer to location */ 5826 memset(&node_icb_loc, 0, sizeof(struct long_ad)); 5827 node_icb_loc.len = udf_rw32(lb_size); 5828 node_icb_loc.loc.lb_num = udf_rw32(lb_num); 5829 node_icb_loc.loc.part_num = udf_rw16(vpart_num); 5830 5831 /* build udf_node (do initialise!) */ 5832 udf_node = pool_get(&udf_node_pool, PR_WAITOK); 5833 memset(udf_node, 0, sizeof(struct udf_node)); 5834 5835 /* initialise crosslinks, note location of fe/efe for hashing */ 5836 /* bugalert: synchronise with udf_get_node() */ 5837 udf_node->ump = ump; 5838 udf_node->vnode = nvp; 5839 nvp->v_data = udf_node; 5840 udf_node->loc = node_icb_loc; 5841 udf_node->write_loc = node_icb_loc; 5842 udf_node->lockf = 0; 5843 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE); 5844 cv_init(&udf_node->node_lock, "udf_nlk"); 5845 udf_node->outstanding_bufs = 0; 5846 udf_node->outstanding_nodedscr = 0; 5847 udf_node->uncommitted_lbs = 0; 5848 5849 /* initialise genfs */ 5850 genfs_node_init(nvp, &udf_genfsops); 5851 5852 /* insert into the hash lookup */ 5853 udf_register_node(udf_node); 5854 5855 /* get parent's unique ID for refering '..' if its a directory */ 5856 if (dir_node->fe) { 5857 parent_unique_id = udf_rw64(dir_node->fe->unique_id); 5858 parent_gid = (gid_t) udf_rw32(dir_node->fe->gid); 5859 } else { 5860 parent_unique_id = udf_rw64(dir_node->efe->unique_id); 5861 parent_gid = (gid_t) udf_rw32(dir_node->efe->gid); 5862 } 5863 5864 /* get descriptor */ 5865 udf_create_logvol_dscr(ump, udf_node, &node_icb_loc, &dscr); 5866 5867 /* choose a fe or an efe for it */ 5868 if (udf_rw16(ump->logical_vol->tag.descriptor_ver) == 2) { 5869 udf_node->fe = &dscr->fe; 5870 fid_size = udf_create_new_fe(ump, udf_node->fe, 5871 udf_file_type, &udf_node->loc, 5872 &dir_node->loc, parent_unique_id); 5873 /* TODO add extended attribute for creation time */ 5874 } else { 5875 udf_node->efe = &dscr->efe; 5876 fid_size = udf_create_new_efe(ump, udf_node->efe, 5877 udf_file_type, &udf_node->loc, 5878 &dir_node->loc, parent_unique_id); 5879 } 5880 KASSERT(dscr->tag.tag_loc == udf_node->loc.loc.lb_num); 5881 5882 /* update vnode's size and type */ 5883 nvp->v_type = vap->va_type; 5884 uvm_vnp_setsize(nvp, fid_size); 5885 5886 /* set access mode */ 5887 udf_setaccessmode(udf_node, vap->va_mode); 5888 5889 /* set ownership */ 5890 uid = kauth_cred_geteuid(cnp->cn_cred); 5891 gid = parent_gid; 5892 udf_setownership(udf_node, uid, gid); 5893 5894 error = udf_dir_attach(ump, dir_node, udf_node, vap, cnp); 5895 if (error) { 5896 /* free disc allocation for node */ 5897 udf_free_allocated_space(ump, lb_num, vpart_num, 1); 5898 5899 /* recycle udf_node */ 5900 udf_dispose_node(udf_node); 5901 vput(nvp); 5902 5903 *vpp = NULL; 5904 return error; 5905 } 5906 5907 /* adjust file count */ 5908 udf_adjust_filecount(udf_node, 1); 5909 5910 /* return result */ 5911 *vpp = nvp; 5912 5913 return 0; 5914 5915 error_out_unreserve: 5916 udf_do_unreserve_space(ump, NULL, vpart_num, 1); 5917 5918 error_out_unlock: 5919 VOP_UNLOCK(nvp); 5920 5921 error_out_unget: 5922 nvp->v_data = NULL; 5923 ungetnewvnode(nvp); 5924 5925 return error; 5926 } 5927 5928 5929 int 5930 udf_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap, 5931 struct componentname *cnp) 5932 { 5933 int (**vnodeops)(void *); 5934 int udf_file_type; 5935 5936 DPRINTF(NODE, ("udf_create_node called\n")); 5937 5938 /* what type are we creating ? */ 5939 vnodeops = udf_vnodeop_p; 5940 /* start with a default */ 5941 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS; 5942 5943 *vpp = NULL; 5944 5945 switch (vap->va_type) { 5946 case VREG : 5947 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS; 5948 break; 5949 case VDIR : 5950 udf_file_type = UDF_ICB_FILETYPE_DIRECTORY; 5951 break; 5952 case VLNK : 5953 udf_file_type = UDF_ICB_FILETYPE_SYMLINK; 5954 break; 5955 case VBLK : 5956 udf_file_type = UDF_ICB_FILETYPE_BLOCKDEVICE; 5957 /* specfs */ 5958 return ENOTSUP; 5959 break; 5960 case VCHR : 5961 udf_file_type = UDF_ICB_FILETYPE_CHARDEVICE; 5962 /* specfs */ 5963 return ENOTSUP; 5964 break; 5965 case VFIFO : 5966 udf_file_type = UDF_ICB_FILETYPE_FIFO; 5967 /* specfs */ 5968 return ENOTSUP; 5969 break; 5970 case VSOCK : 5971 udf_file_type = UDF_ICB_FILETYPE_SOCKET; 5972 /* specfs */ 5973 return ENOTSUP; 5974 break; 5975 case VNON : 5976 case VBAD : 5977 default : 5978 /* nothing; can we even create these? */ 5979 return EINVAL; 5980 } 5981 5982 return udf_create_node_raw(dvp, vpp, udf_file_type, vnodeops, vap, cnp); 5983 } 5984 5985 /* --------------------------------------------------------------------- */ 5986 5987 static void 5988 udf_free_descriptor_space(struct udf_node *udf_node, struct long_ad *loc, void *mem) 5989 { 5990 struct udf_mount *ump = udf_node->ump; 5991 uint32_t lb_size, lb_num, len, num_lb; 5992 uint16_t vpart_num; 5993 5994 /* is there really one? */ 5995 if (mem == NULL) 5996 return; 5997 5998 /* got a descriptor here */ 5999 len = UDF_EXT_LEN(udf_rw32(loc->len)); 6000 lb_num = udf_rw32(loc->loc.lb_num); 6001 vpart_num = udf_rw16(loc->loc.part_num); 6002 6003 lb_size = udf_rw32(ump->logical_vol->lb_size); 6004 num_lb = (len + lb_size -1) / lb_size; 6005 6006 udf_free_allocated_space(ump, lb_num, vpart_num, num_lb); 6007 } 6008 6009 void 6010 udf_delete_node(struct udf_node *udf_node) 6011 { 6012 void *dscr; 6013 struct udf_mount *ump; 6014 struct long_ad *loc; 6015 int extnr, lvint, dummy; 6016 6017 ump = udf_node->ump; 6018 6019 /* paranoia check on integrity; should be open!; we could panic */ 6020 lvint = udf_rw32(udf_node->ump->logvol_integrity->integrity_type); 6021 if (lvint == UDF_INTEGRITY_CLOSED) 6022 printf("\tIntegrity was CLOSED!\n"); 6023 6024 /* whatever the node type, change its size to zero */ 6025 (void) udf_resize_node(udf_node, 0, &dummy); 6026 6027 /* force it to be `clean'; no use writing it out */ 6028 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED | IN_ACCESS | 6029 IN_CHANGE | IN_UPDATE | IN_MODIFY); 6030 6031 /* adjust file count */ 6032 udf_adjust_filecount(udf_node, -1); 6033 6034 /* 6035 * Free its allocated descriptors; memory will be released when 6036 * vop_reclaim() is called. 6037 */ 6038 loc = &udf_node->loc; 6039 6040 dscr = udf_node->fe; 6041 udf_free_descriptor_space(udf_node, loc, dscr); 6042 dscr = udf_node->efe; 6043 udf_free_descriptor_space(udf_node, loc, dscr); 6044 6045 for (extnr = 0; extnr < UDF_MAX_ALLOC_EXTENTS; extnr++) { 6046 dscr = udf_node->ext[extnr]; 6047 loc = &udf_node->ext_loc[extnr]; 6048 udf_free_descriptor_space(udf_node, loc, dscr); 6049 } 6050 } 6051 6052 /* --------------------------------------------------------------------- */ 6053 6054 /* set new filesize; node but be LOCKED on entry and is locked on exit */ 6055 int 6056 udf_resize_node(struct udf_node *udf_node, uint64_t new_size, int *extended) 6057 { 6058 struct file_entry *fe = udf_node->fe; 6059 struct extfile_entry *efe = udf_node->efe; 6060 uint64_t file_size; 6061 int error; 6062 6063 if (fe) { 6064 file_size = udf_rw64(fe->inf_len); 6065 } else { 6066 assert(udf_node->efe); 6067 file_size = udf_rw64(efe->inf_len); 6068 } 6069 6070 DPRINTF(ATTR, ("\tchanging file length from %"PRIu64" to %"PRIu64"\n", 6071 file_size, new_size)); 6072 6073 /* if not changing, we're done */ 6074 if (file_size == new_size) 6075 return 0; 6076 6077 *extended = (new_size > file_size); 6078 if (*extended) { 6079 error = udf_grow_node(udf_node, new_size); 6080 } else { 6081 error = udf_shrink_node(udf_node, new_size); 6082 } 6083 6084 return error; 6085 } 6086 6087 6088 /* --------------------------------------------------------------------- */ 6089 6090 void 6091 udf_itimes(struct udf_node *udf_node, struct timespec *acc, 6092 struct timespec *mod, struct timespec *birth) 6093 { 6094 struct timespec now; 6095 struct file_entry *fe; 6096 struct extfile_entry *efe; 6097 struct filetimes_extattr_entry *ft_extattr; 6098 struct timestamp *atime, *mtime, *attrtime, *ctime; 6099 struct timestamp fe_ctime; 6100 struct timespec cur_birth; 6101 uint32_t offset, a_l; 6102 uint8_t *filedata; 6103 int error; 6104 6105 /* protect against rogue values */ 6106 if (!udf_node) 6107 return; 6108 6109 fe = udf_node->fe; 6110 efe = udf_node->efe; 6111 6112 if (!(udf_node->i_flags & (IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY))) 6113 return; 6114 6115 /* get descriptor information */ 6116 if (fe) { 6117 atime = &fe->atime; 6118 mtime = &fe->mtime; 6119 attrtime = &fe->attrtime; 6120 filedata = fe->data; 6121 6122 /* initial save dummy setting */ 6123 ctime = &fe_ctime; 6124 6125 /* check our extended attribute if present */ 6126 error = udf_extattr_search_intern(udf_node, 6127 UDF_FILETIMES_ATTR_NO, "", &offset, &a_l); 6128 if (!error) { 6129 ft_extattr = (struct filetimes_extattr_entry *) 6130 (filedata + offset); 6131 if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION) 6132 ctime = &ft_extattr->times[0]; 6133 } 6134 /* TODO create the extended attribute if not found ? */ 6135 } else { 6136 assert(udf_node->efe); 6137 atime = &efe->atime; 6138 mtime = &efe->mtime; 6139 attrtime = &efe->attrtime; 6140 ctime = &efe->ctime; 6141 } 6142 6143 vfs_timestamp(&now); 6144 6145 /* set access time */ 6146 if (udf_node->i_flags & IN_ACCESS) { 6147 if (acc == NULL) 6148 acc = &now; 6149 udf_timespec_to_timestamp(acc, atime); 6150 } 6151 6152 /* set modification time */ 6153 if (udf_node->i_flags & (IN_UPDATE | IN_MODIFY)) { 6154 if (mod == NULL) 6155 mod = &now; 6156 udf_timespec_to_timestamp(mod, mtime); 6157 6158 /* ensure birthtime is older than set modification! */ 6159 udf_timestamp_to_timespec(udf_node->ump, ctime, &cur_birth); 6160 if ((cur_birth.tv_sec > mod->tv_sec) || 6161 ((cur_birth.tv_sec == mod->tv_sec) && 6162 (cur_birth.tv_nsec > mod->tv_nsec))) { 6163 udf_timespec_to_timestamp(mod, ctime); 6164 } 6165 } 6166 6167 /* update birthtime if specified */ 6168 /* XXX we asume here that given birthtime is older than mod */ 6169 if (birth && (birth->tv_sec != VNOVAL)) { 6170 udf_timespec_to_timestamp(birth, ctime); 6171 } 6172 6173 /* set change time */ 6174 if (udf_node->i_flags & (IN_CHANGE | IN_MODIFY)) 6175 udf_timespec_to_timestamp(&now, attrtime); 6176 6177 /* notify updates to the node itself */ 6178 if (udf_node->i_flags & (IN_ACCESS | IN_MODIFY)) 6179 udf_node->i_flags |= IN_ACCESSED; 6180 if (udf_node->i_flags & (IN_UPDATE | IN_CHANGE)) 6181 udf_node->i_flags |= IN_MODIFIED; 6182 6183 /* clear modification flags */ 6184 udf_node->i_flags &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY); 6185 } 6186 6187 /* --------------------------------------------------------------------- */ 6188 6189 int 6190 udf_update(struct vnode *vp, struct timespec *acc, 6191 struct timespec *mod, struct timespec *birth, int updflags) 6192 { 6193 union dscrptr *dscrptr; 6194 struct udf_node *udf_node = VTOI(vp); 6195 struct udf_mount *ump = udf_node->ump; 6196 struct regid *impl_id; 6197 int mnt_async = (vp->v_mount->mnt_flag & MNT_ASYNC); 6198 int waitfor, flags; 6199 6200 #ifdef DEBUG 6201 char bits[128]; 6202 DPRINTF(CALL, ("udf_update(node, %p, %p, %p, %d)\n", acc, mod, birth, 6203 updflags)); 6204 snprintb(bits, sizeof(bits), IN_FLAGBITS, udf_node->i_flags); 6205 DPRINTF(CALL, ("\tnode flags %s\n", bits)); 6206 DPRINTF(CALL, ("\t\tmnt_async = %d\n", mnt_async)); 6207 #endif 6208 6209 /* set our times */ 6210 udf_itimes(udf_node, acc, mod, birth); 6211 6212 /* set our implementation id */ 6213 if (udf_node->fe) { 6214 dscrptr = (union dscrptr *) udf_node->fe; 6215 impl_id = &udf_node->fe->imp_id; 6216 } else { 6217 dscrptr = (union dscrptr *) udf_node->efe; 6218 impl_id = &udf_node->efe->imp_id; 6219 } 6220 6221 /* set our ID */ 6222 udf_set_regid(impl_id, IMPL_NAME); 6223 udf_add_impl_regid(ump, impl_id); 6224 6225 /* update our crc! on RMW we are not allowed to change a thing */ 6226 udf_validate_tag_and_crc_sums(dscrptr); 6227 6228 /* if called when mounted readonly, never write back */ 6229 if (vp->v_mount->mnt_flag & MNT_RDONLY) 6230 return 0; 6231 6232 /* check if the node is dirty 'enough'*/ 6233 if (updflags & UPDATE_CLOSE) { 6234 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED); 6235 } else { 6236 flags = udf_node->i_flags & IN_MODIFIED; 6237 } 6238 if (flags == 0) 6239 return 0; 6240 6241 /* determine if we need to write sync or async */ 6242 waitfor = 0; 6243 if ((flags & IN_MODIFIED) && (mnt_async == 0)) { 6244 /* sync mounted */ 6245 waitfor = updflags & UPDATE_WAIT; 6246 if (updflags & UPDATE_DIROP) 6247 waitfor |= UPDATE_WAIT; 6248 } 6249 if (waitfor) 6250 return VOP_FSYNC(vp, FSCRED, FSYNC_WAIT, 0,0); 6251 6252 return 0; 6253 } 6254 6255 6256 /* --------------------------------------------------------------------- */ 6257 6258 6259 /* 6260 * Read one fid and process it into a dirent and advance to the next (*fid) 6261 * has to be allocated a logical block in size, (*dirent) struct dirent length 6262 */ 6263 6264 int 6265 udf_read_fid_stream(struct vnode *vp, uint64_t *offset, 6266 struct fileid_desc *fid, struct dirent *dirent) 6267 { 6268 struct udf_node *dir_node = VTOI(vp); 6269 struct udf_mount *ump = dir_node->ump; 6270 struct file_entry *fe = dir_node->fe; 6271 struct extfile_entry *efe = dir_node->efe; 6272 uint32_t fid_size, lb_size; 6273 uint64_t file_size; 6274 char *fid_name; 6275 int enough, error; 6276 6277 assert(fid); 6278 assert(dirent); 6279 assert(dir_node); 6280 assert(offset); 6281 assert(*offset != 1); 6282 6283 DPRINTF(FIDS, ("read_fid_stream called at offset %"PRIu64"\n", *offset)); 6284 /* check if we're past the end of the directory */ 6285 if (fe) { 6286 file_size = udf_rw64(fe->inf_len); 6287 } else { 6288 assert(dir_node->efe); 6289 file_size = udf_rw64(efe->inf_len); 6290 } 6291 if (*offset >= file_size) 6292 return EINVAL; 6293 6294 /* get maximum length of FID descriptor */ 6295 lb_size = udf_rw32(ump->logical_vol->lb_size); 6296 6297 /* initialise return values */ 6298 fid_size = 0; 6299 memset(dirent, 0, sizeof(struct dirent)); 6300 memset(fid, 0, lb_size); 6301 6302 enough = (file_size - (*offset) >= UDF_FID_SIZE); 6303 if (!enough) { 6304 /* short dir ... */ 6305 return EIO; 6306 } 6307 6308 error = vn_rdwr(UIO_READ, vp, 6309 fid, MIN(file_size - (*offset), lb_size), *offset, 6310 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, FSCRED, 6311 NULL, NULL); 6312 if (error) 6313 return error; 6314 6315 DPRINTF(FIDS, ("\tfid piece read in fine\n")); 6316 /* 6317 * Check if we got a whole descriptor. 6318 * TODO Try to `resync' directory stream when something is very wrong. 6319 */ 6320 6321 /* check if our FID header is OK */ 6322 error = udf_check_tag(fid); 6323 if (error) { 6324 goto brokendir; 6325 } 6326 DPRINTF(FIDS, ("\ttag check ok\n")); 6327 6328 if (udf_rw16(fid->tag.id) != TAGID_FID) { 6329 error = EIO; 6330 goto brokendir; 6331 } 6332 DPRINTF(FIDS, ("\ttag checked ok: got TAGID_FID\n")); 6333 6334 /* check for length */ 6335 fid_size = udf_fidsize(fid); 6336 enough = (file_size - (*offset) >= fid_size); 6337 if (!enough) { 6338 error = EIO; 6339 goto brokendir; 6340 } 6341 DPRINTF(FIDS, ("\tthe complete fid is read in\n")); 6342 6343 /* check FID contents */ 6344 error = udf_check_tag_payload((union dscrptr *) fid, lb_size); 6345 brokendir: 6346 if (error) { 6347 /* note that is sometimes a bit quick to report */ 6348 printf("UDF: BROKEN DIRECTORY ENTRY\n"); 6349 /* RESYNC? */ 6350 /* TODO: use udf_resync_fid_stream */ 6351 return EIO; 6352 } 6353 DPRINTF(FIDS, ("\tpayload checked ok\n")); 6354 6355 /* we got a whole and valid descriptor! */ 6356 DPRINTF(FIDS, ("\tinterpret FID\n")); 6357 6358 /* create resulting dirent structure */ 6359 fid_name = (char *) fid->data + udf_rw16(fid->l_iu); 6360 udf_to_unix_name(dirent->d_name, MAXNAMLEN, 6361 fid_name, fid->l_fi, &ump->logical_vol->desc_charset); 6362 6363 /* '..' has no name, so provide one */ 6364 if (fid->file_char & UDF_FILE_CHAR_PAR) 6365 strcpy(dirent->d_name, ".."); 6366 6367 dirent->d_fileno = udf_get_node_id(&fid->icb); /* inode hash XXX */ 6368 dirent->d_namlen = strlen(dirent->d_name); 6369 dirent->d_reclen = _DIRENT_SIZE(dirent); 6370 6371 /* 6372 * Note that its not worth trying to go for the filetypes now... its 6373 * too expensive too 6374 */ 6375 dirent->d_type = DT_UNKNOWN; 6376 6377 /* initial guess for filetype we can make */ 6378 if (fid->file_char & UDF_FILE_CHAR_DIR) 6379 dirent->d_type = DT_DIR; 6380 6381 /* advance */ 6382 *offset += fid_size; 6383 6384 return error; 6385 } 6386 6387 6388 /* --------------------------------------------------------------------- */ 6389 6390 static void 6391 udf_sync_pass(struct udf_mount *ump, kauth_cred_t cred, int waitfor, 6392 int pass, int *ndirty) 6393 { 6394 struct udf_node *udf_node, *n_udf_node; 6395 struct vnode *vp; 6396 int vdirty, error; 6397 int on_type, on_flags, on_vnode; 6398 6399 derailed: 6400 KASSERT(mutex_owned(&mntvnode_lock)); 6401 6402 DPRINTF(SYNC, ("sync_pass %d\n", pass)); 6403 udf_node = RB_TREE_MIN(&ump->udf_node_tree); 6404 for (;udf_node; udf_node = n_udf_node) { 6405 DPRINTF(SYNC, (".")); 6406 6407 udf_node->i_flags &= ~IN_SYNCED; 6408 vp = udf_node->vnode; 6409 6410 mutex_enter(&vp->v_interlock); 6411 n_udf_node = rb_tree_iterate(&ump->udf_node_tree, 6412 udf_node, RB_DIR_RIGHT); 6413 6414 if (n_udf_node) 6415 n_udf_node->i_flags |= IN_SYNCED; 6416 6417 /* system nodes are not synced this way */ 6418 if (vp->v_vflag & VV_SYSTEM) { 6419 mutex_exit(&vp->v_interlock); 6420 continue; 6421 } 6422 6423 /* check if its dirty enough to even try */ 6424 on_type = (waitfor == MNT_LAZY || vp->v_type == VNON); 6425 on_flags = ((udf_node->i_flags & 6426 (IN_ACCESSED | IN_UPDATE | IN_MODIFIED)) == 0); 6427 on_vnode = LIST_EMPTY(&vp->v_dirtyblkhd) 6428 && UVM_OBJ_IS_CLEAN(&vp->v_uobj); 6429 if (on_type || (on_flags || on_vnode)) { /* XXX */ 6430 /* not dirty (enough?) */ 6431 mutex_exit(&vp->v_interlock); 6432 continue; 6433 } 6434 6435 mutex_exit(&mntvnode_lock); 6436 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT); 6437 if (error) { 6438 mutex_enter(&mntvnode_lock); 6439 if (error == ENOENT) 6440 goto derailed; 6441 *ndirty += 1; 6442 continue; 6443 } 6444 6445 switch (pass) { 6446 case 1: 6447 VOP_FSYNC(vp, cred, 0 | FSYNC_DATAONLY,0,0); 6448 break; 6449 case 2: 6450 vdirty = vp->v_numoutput; 6451 if (vp->v_tag == VT_UDF) 6452 vdirty += udf_node->outstanding_bufs + 6453 udf_node->outstanding_nodedscr; 6454 if (vdirty == 0) 6455 VOP_FSYNC(vp, cred, 0,0,0); 6456 *ndirty += vdirty; 6457 break; 6458 case 3: 6459 vdirty = vp->v_numoutput; 6460 if (vp->v_tag == VT_UDF) 6461 vdirty += udf_node->outstanding_bufs + 6462 udf_node->outstanding_nodedscr; 6463 *ndirty += vdirty; 6464 break; 6465 } 6466 6467 vput(vp); 6468 mutex_enter(&mntvnode_lock); 6469 } 6470 DPRINTF(SYNC, ("END sync_pass %d\n", pass)); 6471 } 6472 6473 6474 void 6475 udf_do_sync(struct udf_mount *ump, kauth_cred_t cred, int waitfor) 6476 { 6477 int dummy, ndirty; 6478 6479 mutex_enter(&mntvnode_lock); 6480 recount: 6481 dummy = 0; 6482 DPRINTF(CALL, ("issue VOP_FSYNC(DATA only) on all nodes\n")); 6483 DPRINTF(SYNC, ("issue VOP_FSYNC(DATA only) on all nodes\n")); 6484 udf_sync_pass(ump, cred, waitfor, 1, &dummy); 6485 6486 DPRINTF(CALL, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n")); 6487 DPRINTF(SYNC, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n")); 6488 udf_sync_pass(ump, cred, waitfor, 2, &dummy); 6489 6490 if (waitfor == MNT_WAIT) { 6491 ndirty = ump->devvp->v_numoutput; 6492 DPRINTF(SYNC, ("counting pending blocks: on devvp %d\n", 6493 ndirty)); 6494 udf_sync_pass(ump, cred, waitfor, 3, &ndirty); 6495 DPRINTF(SYNC, ("counted num dirty pending blocks %d\n", 6496 ndirty)); 6497 6498 if (ndirty) { 6499 /* 1/4 second wait */ 6500 cv_timedwait(&ump->dirtynodes_cv, &mntvnode_lock, 6501 hz/4); 6502 goto recount; 6503 } 6504 } 6505 6506 mutex_exit(&mntvnode_lock); 6507 } 6508 6509 /* --------------------------------------------------------------------- */ 6510 6511 /* 6512 * Read and write file extent in/from the buffer. 6513 * 6514 * The splitup of the extent into seperate request-buffers is to minimise 6515 * copying around as much as possible. 6516 * 6517 * block based file reading and writing 6518 */ 6519 6520 static int 6521 udf_read_internal(struct udf_node *node, uint8_t *blob) 6522 { 6523 struct udf_mount *ump; 6524 struct file_entry *fe = node->fe; 6525 struct extfile_entry *efe = node->efe; 6526 uint64_t inflen; 6527 uint32_t sector_size; 6528 uint8_t *pos; 6529 int icbflags, addr_type; 6530 6531 /* get extent and do some paranoia checks */ 6532 ump = node->ump; 6533 sector_size = ump->discinfo.sector_size; 6534 6535 if (fe) { 6536 inflen = udf_rw64(fe->inf_len); 6537 pos = &fe->data[0] + udf_rw32(fe->l_ea); 6538 icbflags = udf_rw16(fe->icbtag.flags); 6539 } else { 6540 assert(node->efe); 6541 inflen = udf_rw64(efe->inf_len); 6542 pos = &efe->data[0] + udf_rw32(efe->l_ea); 6543 icbflags = udf_rw16(efe->icbtag.flags); 6544 } 6545 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 6546 6547 assert(addr_type == UDF_ICB_INTERN_ALLOC); 6548 assert(inflen < sector_size); 6549 6550 /* copy out info */ 6551 memset(blob, 0, sector_size); 6552 memcpy(blob, pos, inflen); 6553 6554 return 0; 6555 } 6556 6557 6558 static int 6559 udf_write_internal(struct udf_node *node, uint8_t *blob) 6560 { 6561 struct udf_mount *ump; 6562 struct file_entry *fe = node->fe; 6563 struct extfile_entry *efe = node->efe; 6564 uint64_t inflen; 6565 uint32_t sector_size; 6566 uint8_t *pos; 6567 int icbflags, addr_type; 6568 6569 /* get extent and do some paranoia checks */ 6570 ump = node->ump; 6571 sector_size = ump->discinfo.sector_size; 6572 6573 if (fe) { 6574 inflen = udf_rw64(fe->inf_len); 6575 pos = &fe->data[0] + udf_rw32(fe->l_ea); 6576 icbflags = udf_rw16(fe->icbtag.flags); 6577 } else { 6578 assert(node->efe); 6579 inflen = udf_rw64(efe->inf_len); 6580 pos = &efe->data[0] + udf_rw32(efe->l_ea); 6581 icbflags = udf_rw16(efe->icbtag.flags); 6582 } 6583 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 6584 6585 assert(addr_type == UDF_ICB_INTERN_ALLOC); 6586 assert(inflen < sector_size); 6587 6588 /* copy in blob */ 6589 /* memset(pos, 0, inflen); */ 6590 memcpy(pos, blob, inflen); 6591 6592 return 0; 6593 } 6594 6595 6596 void 6597 udf_read_filebuf(struct udf_node *udf_node, struct buf *buf) 6598 { 6599 struct buf *nestbuf; 6600 struct udf_mount *ump = udf_node->ump; 6601 uint64_t *mapping; 6602 uint64_t run_start; 6603 uint32_t sector_size; 6604 uint32_t buf_offset, sector, rbuflen, rblk; 6605 uint32_t from, lblkno; 6606 uint32_t sectors; 6607 uint8_t *buf_pos; 6608 int error, run_length, what; 6609 6610 sector_size = udf_node->ump->discinfo.sector_size; 6611 6612 from = buf->b_blkno; 6613 sectors = buf->b_bcount / sector_size; 6614 6615 what = udf_get_c_type(udf_node); 6616 6617 /* assure we have enough translation slots */ 6618 KASSERT(buf->b_bcount / sector_size <= UDF_MAX_MAPPINGS); 6619 KASSERT(MAXPHYS / sector_size <= UDF_MAX_MAPPINGS); 6620 6621 if (sectors > UDF_MAX_MAPPINGS) { 6622 printf("udf_read_filebuf: implementation limit on bufsize\n"); 6623 buf->b_error = EIO; 6624 biodone(buf); 6625 return; 6626 } 6627 6628 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK); 6629 6630 error = 0; 6631 DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors)); 6632 error = udf_translate_file_extent(udf_node, from, sectors, mapping); 6633 if (error) { 6634 buf->b_error = error; 6635 biodone(buf); 6636 goto out; 6637 } 6638 DPRINTF(READ, ("\ttranslate extent went OK\n")); 6639 6640 /* pre-check if its an internal */ 6641 if (*mapping == UDF_TRANS_INTERN) { 6642 error = udf_read_internal(udf_node, (uint8_t *) buf->b_data); 6643 if (error) 6644 buf->b_error = error; 6645 biodone(buf); 6646 goto out; 6647 } 6648 DPRINTF(READ, ("\tnot intern\n")); 6649 6650 #ifdef DEBUG 6651 if (udf_verbose & UDF_DEBUG_TRANSLATE) { 6652 printf("Returned translation table:\n"); 6653 for (sector = 0; sector < sectors; sector++) { 6654 printf("%d : %"PRIu64"\n", sector, mapping[sector]); 6655 } 6656 } 6657 #endif 6658 6659 /* request read-in of data from disc sheduler */ 6660 buf->b_resid = buf->b_bcount; 6661 for (sector = 0; sector < sectors; sector++) { 6662 buf_offset = sector * sector_size; 6663 buf_pos = (uint8_t *) buf->b_data + buf_offset; 6664 DPRINTF(READ, ("\tprocessing rel sector %d\n", sector)); 6665 6666 /* check if its zero or unmapped to stop reading */ 6667 switch (mapping[sector]) { 6668 case UDF_TRANS_UNMAPPED: 6669 case UDF_TRANS_ZERO: 6670 /* copy zero sector TODO runlength like below */ 6671 memset(buf_pos, 0, sector_size); 6672 DPRINTF(READ, ("\treturning zero sector\n")); 6673 nestiobuf_done(buf, sector_size, 0); 6674 break; 6675 default : 6676 DPRINTF(READ, ("\tread sector " 6677 "%"PRIu64"\n", mapping[sector])); 6678 6679 lblkno = from + sector; 6680 run_start = mapping[sector]; 6681 run_length = 1; 6682 while (sector < sectors-1) { 6683 if (mapping[sector+1] != mapping[sector]+1) 6684 break; 6685 run_length++; 6686 sector++; 6687 } 6688 6689 /* 6690 * nest an iobuf and mark it for async reading. Since 6691 * we're using nested buffers, they can't be cached by 6692 * design. 6693 */ 6694 rbuflen = run_length * sector_size; 6695 rblk = run_start * (sector_size/DEV_BSIZE); 6696 6697 nestbuf = getiobuf(NULL, true); 6698 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen); 6699 /* nestbuf is B_ASYNC */ 6700 6701 /* identify this nestbuf */ 6702 nestbuf->b_lblkno = lblkno; 6703 assert(nestbuf->b_vp == udf_node->vnode); 6704 6705 /* CD shedules on raw blkno */ 6706 nestbuf->b_blkno = rblk; 6707 nestbuf->b_proc = NULL; 6708 nestbuf->b_rawblkno = rblk; 6709 nestbuf->b_udf_c_type = what; 6710 6711 udf_discstrat_queuebuf(ump, nestbuf); 6712 } 6713 } 6714 out: 6715 /* if we're synchronously reading, wait for the completion */ 6716 if ((buf->b_flags & B_ASYNC) == 0) 6717 biowait(buf); 6718 6719 DPRINTF(READ, ("\tend of read_filebuf\n")); 6720 free(mapping, M_TEMP); 6721 return; 6722 } 6723 6724 6725 void 6726 udf_write_filebuf(struct udf_node *udf_node, struct buf *buf) 6727 { 6728 struct buf *nestbuf; 6729 struct udf_mount *ump = udf_node->ump; 6730 uint64_t *mapping; 6731 uint64_t run_start; 6732 uint32_t lb_size; 6733 uint32_t buf_offset, lb_num, rbuflen, rblk; 6734 uint32_t from, lblkno; 6735 uint32_t num_lb; 6736 int error, run_length, what, s; 6737 6738 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size); 6739 6740 from = buf->b_blkno; 6741 num_lb = buf->b_bcount / lb_size; 6742 6743 what = udf_get_c_type(udf_node); 6744 6745 /* assure we have enough translation slots */ 6746 KASSERT(buf->b_bcount / lb_size <= UDF_MAX_MAPPINGS); 6747 KASSERT(MAXPHYS / lb_size <= UDF_MAX_MAPPINGS); 6748 6749 if (num_lb > UDF_MAX_MAPPINGS) { 6750 printf("udf_write_filebuf: implementation limit on bufsize\n"); 6751 buf->b_error = EIO; 6752 biodone(buf); 6753 return; 6754 } 6755 6756 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK); 6757 6758 error = 0; 6759 DPRINTF(WRITE, ("\ttranslate %d-%d\n", from, num_lb)); 6760 error = udf_translate_file_extent(udf_node, from, num_lb, mapping); 6761 if (error) { 6762 buf->b_error = error; 6763 biodone(buf); 6764 goto out; 6765 } 6766 DPRINTF(WRITE, ("\ttranslate extent went OK\n")); 6767 6768 /* if its internally mapped, we can write it in the descriptor itself */ 6769 if (*mapping == UDF_TRANS_INTERN) { 6770 /* TODO paranoia check if we ARE going to have enough space */ 6771 error = udf_write_internal(udf_node, (uint8_t *) buf->b_data); 6772 if (error) 6773 buf->b_error = error; 6774 biodone(buf); 6775 goto out; 6776 } 6777 DPRINTF(WRITE, ("\tnot intern\n")); 6778 6779 /* request write out of data to disc sheduler */ 6780 buf->b_resid = buf->b_bcount; 6781 for (lb_num = 0; lb_num < num_lb; lb_num++) { 6782 buf_offset = lb_num * lb_size; 6783 DPRINTF(WRITE, ("\tprocessing rel lb_num %d\n", lb_num)); 6784 6785 /* 6786 * Mappings are not that important here. Just before we write 6787 * the lb_num we late-allocate them when needed and update the 6788 * mapping in the udf_node. 6789 */ 6790 6791 /* XXX why not ignore the mapping altogether ? */ 6792 DPRINTF(WRITE, ("\twrite lb_num " 6793 "%"PRIu64, mapping[lb_num])); 6794 6795 lblkno = from + lb_num; 6796 run_start = mapping[lb_num]; 6797 run_length = 1; 6798 while (lb_num < num_lb-1) { 6799 if (mapping[lb_num+1] != mapping[lb_num]+1) 6800 if (mapping[lb_num+1] != mapping[lb_num]) 6801 break; 6802 run_length++; 6803 lb_num++; 6804 } 6805 DPRINTF(WRITE, ("+ %d\n", run_length)); 6806 6807 /* nest an iobuf on the master buffer for the extent */ 6808 rbuflen = run_length * lb_size; 6809 rblk = run_start * (lb_size/DEV_BSIZE); 6810 6811 nestbuf = getiobuf(NULL, true); 6812 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen); 6813 /* nestbuf is B_ASYNC */ 6814 6815 /* identify this nestbuf */ 6816 nestbuf->b_lblkno = lblkno; 6817 KASSERT(nestbuf->b_vp == udf_node->vnode); 6818 6819 /* CD shedules on raw blkno */ 6820 nestbuf->b_blkno = rblk; 6821 nestbuf->b_proc = NULL; 6822 nestbuf->b_rawblkno = rblk; 6823 nestbuf->b_udf_c_type = what; 6824 6825 /* increment our outstanding bufs counter */ 6826 s = splbio(); 6827 udf_node->outstanding_bufs++; 6828 splx(s); 6829 6830 udf_discstrat_queuebuf(ump, nestbuf); 6831 } 6832 out: 6833 /* if we're synchronously writing, wait for the completion */ 6834 if ((buf->b_flags & B_ASYNC) == 0) 6835 biowait(buf); 6836 6837 DPRINTF(WRITE, ("\tend of write_filebuf\n")); 6838 free(mapping, M_TEMP); 6839 return; 6840 } 6841 6842 /* --------------------------------------------------------------------- */ 6843 6844 6845