1 /* $NetBSD: udf_subr.c,v 1.36 2007/07/29 13:31:11 ad Exp $ */ 2 3 /* 4 * Copyright (c) 2006 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the 18 * NetBSD Project. See http://www.NetBSD.org/ for 19 * information about NetBSD. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 */ 35 36 37 #include <sys/cdefs.h> 38 #ifndef lint 39 __RCSID("$NetBSD: udf_subr.c,v 1.36 2007/07/29 13:31:11 ad Exp $"); 40 #endif /* not lint */ 41 42 43 #if defined(_KERNEL_OPT) 44 #include "opt_quota.h" 45 #include "opt_compat_netbsd.h" 46 #endif 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/sysctl.h> 51 #include <sys/namei.h> 52 #include <sys/proc.h> 53 #include <sys/kernel.h> 54 #include <sys/vnode.h> 55 #include <miscfs/genfs/genfs_node.h> 56 #include <sys/mount.h> 57 #include <sys/buf.h> 58 #include <sys/file.h> 59 #include <sys/device.h> 60 #include <sys/disklabel.h> 61 #include <sys/ioctl.h> 62 #include <sys/malloc.h> 63 #include <sys/dirent.h> 64 #include <sys/stat.h> 65 #include <sys/conf.h> 66 #include <sys/kauth.h> 67 #include <dev/clock_subr.h> 68 69 #include <fs/udf/ecma167-udf.h> 70 #include <fs/udf/udf_mount.h> 71 72 #include "udf.h" 73 #include "udf_subr.h" 74 #include "udf_bswap.h" 75 76 77 #define VTOI(vnode) ((struct udf_node *) vnode->v_data) 78 79 80 /* predefines */ 81 82 83 #if 0 84 { 85 int i, j, dlen; 86 uint8_t *blob; 87 88 blob = (uint8_t *) fid; 89 dlen = file_size - (*offset); 90 91 printf("blob = %p\n", blob); 92 printf("dump of %d bytes\n", dlen); 93 94 for (i = 0; i < dlen; i+ = 16) { 95 printf("%04x ", i); 96 for (j = 0; j < 16; j++) { 97 if (i+j < dlen) { 98 printf("%02x ", blob[i+j]); 99 } else { 100 printf(" "); 101 } 102 } 103 for (j = 0; j < 16; j++) { 104 if (i+j < dlen) { 105 if (blob[i+j]>32 && blob[i+j]! = 127) { 106 printf("%c", blob[i+j]); 107 } else { 108 printf("."); 109 } 110 } 111 } 112 printf("\n"); 113 } 114 printf("\n"); 115 } 116 Debugger(); 117 #endif 118 119 120 /* --------------------------------------------------------------------- */ 121 122 /* STUB */ 123 124 static int 125 udf_bread(struct udf_mount *ump, uint32_t sector, struct buf **bpp) 126 { 127 int sector_size = ump->discinfo.sector_size; 128 int blks = sector_size / DEV_BSIZE; 129 130 /* NOTE bread() checks if block is in cache or not */ 131 return bread(ump->devvp, sector*blks, sector_size, NOCRED, bpp); 132 } 133 134 135 /* --------------------------------------------------------------------- */ 136 137 /* 138 * Check if the blob starts with a good UDF tag. Tags are protected by a 139 * checksum over the reader except one byte at position 4 that is the checksum 140 * itself. 141 */ 142 143 int 144 udf_check_tag(void *blob) 145 { 146 struct desc_tag *tag = blob; 147 uint8_t *pos, sum, cnt; 148 149 /* check TAG header checksum */ 150 pos = (uint8_t *) tag; 151 sum = 0; 152 153 for(cnt = 0; cnt < 16; cnt++) { 154 if (cnt != 4) 155 sum += *pos; 156 pos++; 157 } 158 if (sum != tag->cksum) { 159 /* bad tag header checksum; this is not a valid tag */ 160 return EINVAL; 161 } 162 163 return 0; 164 } 165 166 /* --------------------------------------------------------------------- */ 167 168 /* 169 * check tag payload will check descriptor CRC as specified. 170 * If the descriptor is too short, it will return EIO otherwise EINVAL. 171 */ 172 173 int 174 udf_check_tag_payload(void *blob, uint32_t max_length) 175 { 176 struct desc_tag *tag = blob; 177 uint16_t crc, crc_len; 178 179 crc_len = udf_rw16(tag->desc_crc_len); 180 181 /* check payload CRC if applicable */ 182 if (crc_len == 0) 183 return 0; 184 185 if (crc_len > max_length) 186 return EIO; 187 188 crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len); 189 if (crc != udf_rw16(tag->desc_crc)) { 190 /* bad payload CRC; this is a broken tag */ 191 return EINVAL; 192 } 193 194 return 0; 195 } 196 197 /* --------------------------------------------------------------------- */ 198 199 int 200 udf_validate_tag_sum(void *blob) 201 { 202 struct desc_tag *tag = blob; 203 uint8_t *pos, sum, cnt; 204 205 /* calculate TAG header checksum */ 206 pos = (uint8_t *) tag; 207 sum = 0; 208 209 for(cnt = 0; cnt < 16; cnt++) { 210 if (cnt != 4) sum += *pos; 211 pos++; 212 } 213 tag->cksum = sum; /* 8 bit */ 214 215 return 0; 216 } 217 218 /* --------------------------------------------------------------------- */ 219 220 /* assumes sector number of descriptor to be saved already present */ 221 222 int 223 udf_validate_tag_and_crc_sums(void *blob) 224 { 225 struct desc_tag *tag = blob; 226 uint8_t *btag = (uint8_t *) tag; 227 uint16_t crc, crc_len; 228 229 crc_len = udf_rw16(tag->desc_crc_len); 230 231 /* check payload CRC if applicable */ 232 if (crc_len > 0) { 233 crc = udf_cksum(btag + UDF_DESC_TAG_LENGTH, crc_len); 234 tag->desc_crc = udf_rw16(crc); 235 } 236 237 /* calculate TAG header checksum */ 238 return udf_validate_tag_sum(blob); 239 } 240 241 /* --------------------------------------------------------------------- */ 242 243 /* 244 * XXX note the different semantics from udfclient: for FIDs it still rounds 245 * up to sectors. Use udf_fidsize() for a correct length. 246 */ 247 248 int 249 udf_tagsize(union dscrptr *dscr, uint32_t udf_sector_size) 250 { 251 uint32_t size, tag_id, num_secs, elmsz; 252 253 tag_id = udf_rw16(dscr->tag.id); 254 255 switch (tag_id) { 256 case TAGID_LOGVOL : 257 size = sizeof(struct logvol_desc) - 1; 258 size += udf_rw32(dscr->lvd.mt_l); 259 break; 260 case TAGID_UNALLOC_SPACE : 261 elmsz = sizeof(struct extent_ad); 262 size = sizeof(struct unalloc_sp_desc) - elmsz; 263 size += udf_rw32(dscr->usd.alloc_desc_num) * elmsz; 264 break; 265 case TAGID_FID : 266 size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu); 267 size = (size + 3) & ~3; 268 break; 269 case TAGID_LOGVOL_INTEGRITY : 270 size = sizeof(struct logvol_int_desc) - sizeof(uint32_t); 271 size += udf_rw32(dscr->lvid.l_iu); 272 size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t)); 273 break; 274 case TAGID_SPACE_BITMAP : 275 size = sizeof(struct space_bitmap_desc) - 1; 276 size += udf_rw32(dscr->sbd.num_bytes); 277 break; 278 case TAGID_SPARING_TABLE : 279 elmsz = sizeof(struct spare_map_entry); 280 size = sizeof(struct udf_sparing_table) - elmsz; 281 size += udf_rw16(dscr->spt.rt_l) * elmsz; 282 break; 283 case TAGID_FENTRY : 284 size = sizeof(struct file_entry); 285 size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1; 286 break; 287 case TAGID_EXTFENTRY : 288 size = sizeof(struct extfile_entry); 289 size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1; 290 break; 291 case TAGID_FSD : 292 size = sizeof(struct fileset_desc); 293 break; 294 default : 295 size = sizeof(union dscrptr); 296 break; 297 } 298 299 if ((size == 0) || (udf_sector_size == 0)) return 0; 300 301 /* round up in sectors */ 302 num_secs = (size + udf_sector_size -1) / udf_sector_size; 303 return num_secs * udf_sector_size; 304 } 305 306 307 static int 308 udf_fidsize(struct fileid_desc *fid, uint32_t udf_sector_size) 309 { 310 uint32_t size; 311 312 if (udf_rw16(fid->tag.id) != TAGID_FID) 313 panic("got udf_fidsize on non FID\n"); 314 315 size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu); 316 size = (size + 3) & ~3; 317 318 return size; 319 } 320 321 /* --------------------------------------------------------------------- */ 322 323 /* 324 * Problem with read_descriptor are long descriptors spanning more than one 325 * sector. Luckily long descriptors can't be in `logical space'. 326 * 327 * Size of allocated piece is returned in multiple of sector size due to 328 * udf_calc_udf_malloc_size(). 329 */ 330 331 int 332 udf_read_descriptor(struct udf_mount *ump, uint32_t sector, 333 struct malloc_type *mtype, union dscrptr **dstp) 334 { 335 union dscrptr *src, *dst; 336 struct buf *bp; 337 uint8_t *pos; 338 int blks, blk, dscrlen; 339 int i, error, sector_size; 340 341 sector_size = ump->discinfo.sector_size; 342 343 *dstp = dst = NULL; 344 dscrlen = sector_size; 345 346 /* read initial piece */ 347 error = udf_bread(ump, sector, &bp); 348 DPRINTFIF(DESCRIPTOR, error, ("read error (%d)\n", error)); 349 350 if (!error) { 351 /* check if its a valid tag */ 352 error = udf_check_tag(bp->b_data); 353 if (error) { 354 /* check if its an empty block */ 355 pos = bp->b_data; 356 for (i = 0; i < sector_size; i++, pos++) { 357 if (*pos) break; 358 } 359 if (i == sector_size) { 360 /* return no error but with no dscrptr */ 361 /* dispose first block */ 362 brelse(bp); 363 return 0; 364 } 365 } 366 } 367 DPRINTFIF(DESCRIPTOR, error, ("bad tag checksum\n")); 368 if (!error) { 369 src = (union dscrptr *) bp->b_data; 370 dscrlen = udf_tagsize(src, sector_size); 371 dst = malloc(dscrlen, mtype, M_WAITOK); 372 memcpy(dst, src, sector_size); 373 } 374 /* dispose first block */ 375 bp->b_flags |= B_AGE; 376 brelse(bp); 377 378 if (!error && (dscrlen > sector_size)) { 379 DPRINTF(DESCRIPTOR, ("multi block descriptor read\n")); 380 /* 381 * Read the rest of descriptor. Since it is only used at mount 382 * time its overdone to define and use a specific udf_breadn 383 * for this alone. 384 */ 385 blks = (dscrlen + sector_size -1) / sector_size; 386 for (blk = 1; blk < blks; blk++) { 387 error = udf_bread(ump, sector + blk, &bp); 388 if (error) { 389 brelse(bp); 390 break; 391 } 392 pos = (uint8_t *) dst + blk*sector_size; 393 memcpy(pos, bp->b_data, sector_size); 394 395 /* dispose block */ 396 bp->b_flags |= B_AGE; 397 brelse(bp); 398 } 399 DPRINTFIF(DESCRIPTOR, error, ("read error on multi (%d)\n", 400 error)); 401 } 402 if (!error) { 403 error = udf_check_tag_payload(dst, dscrlen); 404 DPRINTFIF(DESCRIPTOR, error, ("bad payload check sum\n")); 405 } 406 if (error && dst) { 407 free(dst, mtype); 408 dst = NULL; 409 } 410 *dstp = dst; 411 412 return error; 413 } 414 415 /* --------------------------------------------------------------------- */ 416 #ifdef DEBUG 417 static void 418 udf_dump_discinfo(struct udf_mount *ump) 419 { 420 char bits[128]; 421 struct mmc_discinfo *di = &ump->discinfo; 422 423 if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0) 424 return; 425 426 printf("Device/media info :\n"); 427 printf("\tMMC profile 0x%02x\n", di->mmc_profile); 428 printf("\tderived class %d\n", di->mmc_class); 429 printf("\tsector size %d\n", di->sector_size); 430 printf("\tdisc state %d\n", di->disc_state); 431 printf("\tlast ses state %d\n", di->last_session_state); 432 printf("\tbg format state %d\n", di->bg_format_state); 433 printf("\tfrst track %d\n", di->first_track); 434 printf("\tfst on last ses %d\n", di->first_track_last_session); 435 printf("\tlst on last ses %d\n", di->last_track_last_session); 436 printf("\tlink block penalty %d\n", di->link_block_penalty); 437 bitmask_snprintf(di->disc_flags, MMC_DFLAGS_FLAGBITS, bits, 438 sizeof(bits)); 439 printf("\tdisc flags %s\n", bits); 440 printf("\tdisc id %x\n", di->disc_id); 441 printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode); 442 443 printf("\tnum sessions %d\n", di->num_sessions); 444 printf("\tnum tracks %d\n", di->num_tracks); 445 446 bitmask_snprintf(di->mmc_cur, MMC_CAP_FLAGBITS, bits, sizeof(bits)); 447 printf("\tcapabilities cur %s\n", bits); 448 bitmask_snprintf(di->mmc_cap, MMC_CAP_FLAGBITS, bits, sizeof(bits)); 449 printf("\tcapabilities cap %s\n", bits); 450 } 451 #else 452 #define udf_dump_discinfo(a); 453 #endif 454 455 /* not called often */ 456 int 457 udf_update_discinfo(struct udf_mount *ump) 458 { 459 struct vnode *devvp = ump->devvp; 460 struct partinfo dpart; 461 struct mmc_discinfo *di; 462 int error; 463 464 DPRINTF(VOLUMES, ("read/update disc info\n")); 465 di = &ump->discinfo; 466 memset(di, 0, sizeof(struct mmc_discinfo)); 467 468 /* check if we're on a MMC capable device, i.e. CD/DVD */ 469 error = VOP_IOCTL(devvp, MMCGETDISCINFO, di, FKIOCTL, NOCRED, NULL); 470 if (error == 0) { 471 udf_dump_discinfo(ump); 472 return 0; 473 } 474 475 /* disc partition support */ 476 error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, NULL); 477 if (error) 478 return ENODEV; 479 480 /* set up a disc info profile for partitions */ 481 di->mmc_profile = 0x01; /* disc type */ 482 di->mmc_class = MMC_CLASS_DISC; 483 di->disc_state = MMC_STATE_CLOSED; 484 di->last_session_state = MMC_STATE_CLOSED; 485 di->bg_format_state = MMC_BGFSTATE_COMPLETED; 486 di->link_block_penalty = 0; 487 488 di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE | 489 MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE; 490 di->mmc_cap = di->mmc_cur; 491 di->disc_flags = MMC_DFLAGS_UNRESTRICTED; 492 493 /* TODO problem with last_possible_lba on resizable VND; request */ 494 di->last_possible_lba = dpart.part->p_size; 495 di->sector_size = dpart.disklab->d_secsize; 496 di->blockingnr = 1; 497 498 di->num_sessions = 1; 499 di->num_tracks = 1; 500 501 di->first_track = 1; 502 di->first_track_last_session = di->last_track_last_session = 1; 503 504 udf_dump_discinfo(ump); 505 return 0; 506 } 507 508 /* --------------------------------------------------------------------- */ 509 510 int 511 udf_update_trackinfo(struct udf_mount *ump, struct mmc_trackinfo *ti) 512 { 513 struct vnode *devvp = ump->devvp; 514 struct mmc_discinfo *di = &ump->discinfo; 515 int error, class; 516 517 DPRINTF(VOLUMES, ("read track info\n")); 518 519 class = di->mmc_class; 520 if (class != MMC_CLASS_DISC) { 521 /* tracknr specified in struct ti */ 522 error = VOP_IOCTL(devvp, MMCGETTRACKINFO, ti, FKIOCTL, 523 NOCRED, NULL); 524 return error; 525 } 526 527 /* disc partition support */ 528 if (ti->tracknr != 1) 529 return EIO; 530 531 /* create fake ti (TODO check for resized vnds) */ 532 ti->sessionnr = 1; 533 534 ti->track_mode = 0; /* XXX */ 535 ti->data_mode = 0; /* XXX */ 536 ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID; 537 538 ti->track_start = 0; 539 ti->packet_size = 1; 540 541 /* TODO support for resizable vnd */ 542 ti->track_size = di->last_possible_lba; 543 ti->next_writable = di->last_possible_lba; 544 ti->last_recorded = ti->next_writable; 545 ti->free_blocks = 0; 546 547 return 0; 548 } 549 550 /* --------------------------------------------------------------------- */ 551 552 /* track/session searching for mounting */ 553 554 static int 555 udf_search_tracks(struct udf_mount *ump, struct udf_args *args, 556 int *first_tracknr, int *last_tracknr) 557 { 558 struct mmc_trackinfo trackinfo; 559 uint32_t tracknr, start_track, num_tracks; 560 int error; 561 562 /* if negative, sessionnr is relative to last session */ 563 if (args->sessionnr < 0) { 564 args->sessionnr += ump->discinfo.num_sessions; 565 /* sanity */ 566 if (args->sessionnr < 0) 567 args->sessionnr = 0; 568 } 569 570 /* sanity */ 571 if (args->sessionnr > ump->discinfo.num_sessions) 572 args->sessionnr = ump->discinfo.num_sessions; 573 574 /* search the tracks for this session, zero session nr indicates last */ 575 if (args->sessionnr == 0) { 576 args->sessionnr = ump->discinfo.num_sessions; 577 if (ump->discinfo.last_session_state == MMC_STATE_EMPTY) { 578 args->sessionnr--; 579 } 580 } 581 582 /* search the first and last track of the specified session */ 583 num_tracks = ump->discinfo.num_tracks; 584 start_track = ump->discinfo.first_track; 585 586 /* search for first track of this session */ 587 for (tracknr = start_track; tracknr <= num_tracks; tracknr++) { 588 /* get track info */ 589 trackinfo.tracknr = tracknr; 590 error = udf_update_trackinfo(ump, &trackinfo); 591 if (error) 592 return error; 593 594 if (trackinfo.sessionnr == args->sessionnr) 595 break; 596 } 597 *first_tracknr = tracknr; 598 599 /* search for last track of this session */ 600 for (;tracknr <= num_tracks; tracknr++) { 601 /* get track info */ 602 trackinfo.tracknr = tracknr; 603 error = udf_update_trackinfo(ump, &trackinfo); 604 if (error || (trackinfo.sessionnr != args->sessionnr)) { 605 tracknr--; 606 break; 607 } 608 } 609 if (tracknr > num_tracks) 610 tracknr--; 611 612 *last_tracknr = tracknr; 613 614 assert(*last_tracknr >= *first_tracknr); 615 return 0; 616 } 617 618 /* --------------------------------------------------------------------- */ 619 620 static int 621 udf_read_anchor(struct udf_mount *ump, uint32_t sector, struct anchor_vdp **dst) 622 { 623 int error; 624 625 error = udf_read_descriptor(ump, sector, M_UDFVOLD, 626 (union dscrptr **) dst); 627 if (!error) { 628 /* blank terminator blocks are not allowed here */ 629 if (*dst == NULL) 630 return ENOENT; 631 if (udf_rw16((*dst)->tag.id) != TAGID_ANCHOR) { 632 error = ENOENT; 633 free(*dst, M_UDFVOLD); 634 *dst = NULL; 635 DPRINTF(VOLUMES, ("Not an anchor\n")); 636 } 637 } 638 639 return error; 640 } 641 642 643 int 644 udf_read_anchors(struct udf_mount *ump, struct udf_args *args) 645 { 646 struct mmc_trackinfo first_track; 647 struct mmc_trackinfo last_track; 648 struct anchor_vdp **anchorsp; 649 uint32_t track_start; 650 uint32_t track_end; 651 uint32_t positions[4]; 652 int first_tracknr, last_tracknr; 653 int error, anch, ok, first_anchor; 654 655 /* search the first and last track of the specified session */ 656 error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr); 657 if (!error) { 658 first_track.tracknr = first_tracknr; 659 error = udf_update_trackinfo(ump, &first_track); 660 } 661 if (!error) { 662 last_track.tracknr = last_tracknr; 663 error = udf_update_trackinfo(ump, &last_track); 664 } 665 if (error) { 666 printf("UDF mount: reading disc geometry failed\n"); 667 return 0; 668 } 669 670 track_start = first_track.track_start; 671 672 /* `end' is not as straitforward as start. */ 673 track_end = last_track.track_start 674 + last_track.track_size - last_track.free_blocks - 1; 675 676 if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) { 677 /* end of track is not straitforward here */ 678 if (last_track.flags & MMC_TRACKINFO_LRA_VALID) 679 track_end = last_track.last_recorded; 680 else if (last_track.flags & MMC_TRACKINFO_NWA_VALID) 681 track_end = last_track.next_writable 682 - ump->discinfo.link_block_penalty; 683 } 684 685 /* its no use reading a blank track */ 686 first_anchor = 0; 687 if (first_track.flags & MMC_TRACKINFO_BLANK) 688 first_anchor = 1; 689 690 /* read anchors start+256, start+512, end-256, end */ 691 positions[0] = track_start+256; 692 positions[1] = track_end-256; 693 positions[2] = track_end; 694 positions[3] = track_start+512; /* [UDF 2.60/6.11.2] */ 695 /* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */ 696 697 ok = 0; 698 anchorsp = ump->anchors; 699 for (anch = first_anchor; anch < 4; anch++) { 700 DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch, 701 positions[anch])); 702 error = udf_read_anchor(ump, positions[anch], anchorsp); 703 if (!error) { 704 anchorsp++; 705 ok++; 706 } 707 } 708 709 /* VATs are only recorded on sequential media, but initialise */ 710 ump->first_possible_vat_location = track_start + 256 + 1; 711 ump->last_possible_vat_location = track_end 712 + ump->discinfo.blockingnr; 713 714 return ok; 715 } 716 717 /* --------------------------------------------------------------------- */ 718 719 /* we dont try to be smart; we just record the parts */ 720 #define UDF_UPDATE_DSCR(name, dscr) \ 721 if (name) \ 722 free(name, M_UDFVOLD); \ 723 name = dscr; 724 725 static int 726 udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr) 727 { 728 struct part_desc *part; 729 uint16_t phys_part, raw_phys_part; 730 731 DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n", 732 udf_rw16(dscr->tag.id))); 733 switch (udf_rw16(dscr->tag.id)) { 734 case TAGID_PRI_VOL : /* primary partition */ 735 UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd); 736 break; 737 case TAGID_LOGVOL : /* logical volume */ 738 UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd); 739 break; 740 case TAGID_UNALLOC_SPACE : /* unallocated space */ 741 UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd); 742 break; 743 case TAGID_IMP_VOL : /* implementation */ 744 /* XXX do we care about multiple impl. descr ? */ 745 UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd); 746 break; 747 case TAGID_PARTITION : /* physical partition */ 748 /* not much use if its not allocated */ 749 if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) { 750 free(dscr, M_UDFVOLD); 751 break; 752 } 753 754 /* 755 * BUGALERT: some rogue implementations use random physical 756 * partion numbers to break other implementations so lookup 757 * the number. 758 */ 759 raw_phys_part = udf_rw16(dscr->pd.part_num); 760 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 761 part = ump->partitions[phys_part]; 762 if (part == NULL) 763 break; 764 if (udf_rw16(part->part_num) == raw_phys_part) 765 break; 766 } 767 if (phys_part == UDF_PARTITIONS) { 768 free(dscr, M_UDFVOLD); 769 return EINVAL; 770 } 771 772 UDF_UPDATE_DSCR(ump->partitions[phys_part], &dscr->pd); 773 break; 774 case TAGID_VOL : /* volume space extender; rare */ 775 DPRINTF(VOLUMES, ("VDS extender ignored\n")); 776 free(dscr, M_UDFVOLD); 777 break; 778 default : 779 DPRINTF(VOLUMES, ("Unhandled VDS type %d\n", 780 udf_rw16(dscr->tag.id))); 781 free(dscr, M_UDFVOLD); 782 } 783 784 return 0; 785 } 786 #undef UDF_UPDATE_DSCR 787 788 /* --------------------------------------------------------------------- */ 789 790 static int 791 udf_read_vds_extent(struct udf_mount *ump, uint32_t loc, uint32_t len) 792 { 793 union dscrptr *dscr; 794 uint32_t sector_size, dscr_size; 795 int error; 796 797 sector_size = ump->discinfo.sector_size; 798 799 /* loc is sectornr, len is in bytes */ 800 error = EIO; 801 while (len) { 802 error = udf_read_descriptor(ump, loc, M_UDFVOLD, &dscr); 803 if (error) 804 return error; 805 806 /* blank block is a terminator */ 807 if (dscr == NULL) 808 return 0; 809 810 /* TERM descriptor is a terminator */ 811 if (udf_rw16(dscr->tag.id) == TAGID_TERM) { 812 free(dscr, M_UDFVOLD); 813 return 0; 814 } 815 816 /* process all others */ 817 dscr_size = udf_tagsize(dscr, sector_size); 818 error = udf_process_vds_descriptor(ump, dscr); 819 if (error) { 820 free(dscr, M_UDFVOLD); 821 break; 822 } 823 assert((dscr_size % sector_size) == 0); 824 825 len -= dscr_size; 826 loc += dscr_size / sector_size; 827 } 828 829 return error; 830 } 831 832 833 int 834 udf_read_vds_space(struct udf_mount *ump) 835 { 836 struct anchor_vdp *anchor, *anchor2; 837 size_t size; 838 uint32_t main_loc, main_len; 839 uint32_t reserve_loc, reserve_len; 840 int error; 841 842 /* 843 * read in VDS space provided by the anchors; if one descriptor read 844 * fails, try the mirror sector. 845 * 846 * check if 2nd anchor is different from 1st; if so, go for 2nd. This 847 * avoids the `compatibility features' of DirectCD that may confuse 848 * stuff completely. 849 */ 850 851 anchor = ump->anchors[0]; 852 anchor2 = ump->anchors[1]; 853 assert(anchor); 854 855 if (anchor2) { 856 size = sizeof(struct extent_ad); 857 if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size)) 858 anchor = anchor2; 859 /* reserve is specified to be a literal copy of main */ 860 } 861 862 main_loc = udf_rw32(anchor->main_vds_ex.loc); 863 main_len = udf_rw32(anchor->main_vds_ex.len); 864 865 reserve_loc = udf_rw32(anchor->reserve_vds_ex.loc); 866 reserve_len = udf_rw32(anchor->reserve_vds_ex.len); 867 868 error = udf_read_vds_extent(ump, main_loc, main_len); 869 if (error) { 870 printf("UDF mount: reading in reserve VDS extent\n"); 871 error = udf_read_vds_extent(ump, reserve_loc, reserve_len); 872 } 873 874 return error; 875 } 876 877 /* --------------------------------------------------------------------- */ 878 879 /* 880 * Read in the logical volume integrity sequence pointed to by our logical 881 * volume descriptor. Its a sequence that can be extended using fields in the 882 * integrity descriptor itself. On sequential media only one is found, on 883 * rewritable media a sequence of descriptors can be found as a form of 884 * history keeping and on non sequential write-once media the chain is vital 885 * to allow more and more descriptors to be written. The last descriptor 886 * written in an extent needs to claim space for a new extent. 887 */ 888 889 static int 890 udf_retrieve_lvint(struct udf_mount *ump, struct logvol_int_desc **lvintp) 891 { 892 union dscrptr *dscr; 893 struct logvol_int_desc *lvint; 894 uint32_t sector_size, sector, len; 895 int dscr_type, error; 896 897 sector_size = ump->discinfo.sector_size; 898 len = udf_rw32(ump->logical_vol->integrity_seq_loc.len); 899 sector = udf_rw32(ump->logical_vol->integrity_seq_loc.loc); 900 901 lvint = NULL; 902 dscr = NULL; 903 error = 0; 904 while (len) { 905 /* read in our integrity descriptor */ 906 error = udf_read_descriptor(ump, sector, M_UDFVOLD, &dscr); 907 if (!error) { 908 if (dscr == NULL) 909 break; /* empty terminates */ 910 dscr_type = udf_rw16(dscr->tag.id); 911 if (dscr_type == TAGID_TERM) { 912 break; /* clean terminator */ 913 } 914 if (dscr_type != TAGID_LOGVOL_INTEGRITY) { 915 /* fatal... corrupt disc */ 916 error = ENOENT; 917 break; 918 } 919 if (lvint) 920 free(lvint, M_UDFVOLD); 921 lvint = &dscr->lvid; 922 dscr = NULL; 923 } /* else hope for the best... maybe the next is ok */ 924 925 DPRINTFIF(VOLUMES, lvint, ("logvol integrity read, state %s\n", 926 udf_rw32(lvint->integrity_type) ? "CLOSED" : "OPEN")); 927 928 /* proceed sequential */ 929 sector += 1; 930 len -= sector_size; 931 932 /* are we linking to a new piece? */ 933 if (lvint->next_extent.len) { 934 len = udf_rw32(lvint->next_extent.len); 935 sector = udf_rw32(lvint->next_extent.loc); 936 } 937 } 938 939 /* clean up the mess, esp. when there is an error */ 940 if (dscr) 941 free(dscr, M_UDFVOLD); 942 943 if (error && lvint) { 944 free(lvint, M_UDFVOLD); 945 lvint = NULL; 946 } 947 948 if (!lvint) 949 error = ENOENT; 950 951 *lvintp = lvint; 952 return error; 953 } 954 955 /* --------------------------------------------------------------------- */ 956 957 /* 958 * Checks if ump's vds information is correct and complete 959 */ 960 961 int 962 udf_process_vds(struct udf_mount *ump, struct udf_args *args) 963 { 964 union udf_pmap *mapping; 965 struct logvol_int_desc *lvint; 966 struct udf_logvol_info *lvinfo; 967 struct part_desc *part; 968 uint32_t n_pm, mt_l; 969 uint8_t *pmap_pos; 970 char *domain_name, *map_name; 971 const char *check_name; 972 int pmap_stype, pmap_size; 973 int pmap_type, log_part, phys_part, raw_phys_part; 974 int n_phys, n_virt, n_spar, n_meta; 975 int len, error; 976 977 if (ump == NULL) 978 return ENOENT; 979 980 /* we need at least an anchor (trivial, but for safety) */ 981 if (ump->anchors[0] == NULL) 982 return EINVAL; 983 984 /* we need at least one primary and one logical volume descriptor */ 985 if ((ump->primary_vol == NULL) || (ump->logical_vol) == NULL) 986 return EINVAL; 987 988 /* we need at least one partition descriptor */ 989 if (ump->partitions[0] == NULL) 990 return EINVAL; 991 992 /* check logical volume sector size verses device sector size */ 993 if (udf_rw32(ump->logical_vol->lb_size) != ump->discinfo.sector_size) { 994 printf("UDF mount: format violation, lb_size != sector size\n"); 995 return EINVAL; 996 } 997 998 domain_name = ump->logical_vol->domain_id.id; 999 if (strncmp(domain_name, "*OSTA UDF Compliant", 20)) { 1000 printf("mount_udf: disc not OSTA UDF Compliant, aborting\n"); 1001 return EINVAL; 1002 } 1003 1004 /* retrieve logical volume integrity sequence */ 1005 error = udf_retrieve_lvint(ump, &ump->logvol_integrity); 1006 1007 /* 1008 * We need at least one logvol integrity descriptor recorded. Note 1009 * that its OK to have an open logical volume integrity here. The VAT 1010 * will close/update the integrity. 1011 */ 1012 if (ump->logvol_integrity == NULL) 1013 return EINVAL; 1014 1015 /* process derived structures */ 1016 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */ 1017 lvint = ump->logvol_integrity; 1018 lvinfo = (struct udf_logvol_info *) (&lvint->tables[2 * n_pm]); 1019 ump->logvol_info = lvinfo; 1020 1021 /* TODO check udf versions? */ 1022 1023 /* 1024 * check logvol mappings: effective virt->log partmap translation 1025 * check and recording of the mapping results. Saves expensive 1026 * strncmp() in tight places. 1027 */ 1028 DPRINTF(VOLUMES, ("checking logvol mappings\n")); 1029 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */ 1030 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */ 1031 pmap_pos = ump->logical_vol->maps; 1032 1033 if (n_pm > UDF_PMAPS) { 1034 printf("UDF mount: too many mappings\n"); 1035 return EINVAL; 1036 } 1037 1038 n_phys = n_virt = n_spar = n_meta = 0; 1039 for (log_part = 0; log_part < n_pm; log_part++) { 1040 mapping = (union udf_pmap *) pmap_pos; 1041 pmap_stype = pmap_pos[0]; 1042 pmap_size = pmap_pos[1]; 1043 switch (pmap_stype) { 1044 case 1: /* physical mapping */ 1045 /* volseq = udf_rw16(mapping->pm1.vol_seq_num); */ 1046 raw_phys_part = udf_rw16(mapping->pm1.part_num); 1047 pmap_type = UDF_VTOP_TYPE_PHYS; 1048 n_phys++; 1049 break; 1050 case 2: /* virtual/sparable/meta mapping */ 1051 map_name = mapping->pm2.part_id.id; 1052 /* volseq = udf_rw16(mapping->pm2.vol_seq_num); */ 1053 raw_phys_part = udf_rw16(mapping->pm2.part_num); 1054 pmap_type = UDF_VTOP_TYPE_UNKNOWN; 1055 len = UDF_REGID_ID_SIZE; 1056 1057 check_name = "*UDF Virtual Partition"; 1058 if (strncmp(map_name, check_name, len) == 0) { 1059 pmap_type = UDF_VTOP_TYPE_VIRT; 1060 n_virt++; 1061 break; 1062 } 1063 check_name = "*UDF Sparable Partition"; 1064 if (strncmp(map_name, check_name, len) == 0) { 1065 pmap_type = UDF_VTOP_TYPE_SPARABLE; 1066 n_spar++; 1067 break; 1068 } 1069 check_name = "*UDF Metadata Partition"; 1070 if (strncmp(map_name, check_name, len) == 0) { 1071 pmap_type = UDF_VTOP_TYPE_META; 1072 n_meta++; 1073 break; 1074 } 1075 break; 1076 default: 1077 return EINVAL; 1078 } 1079 1080 /* 1081 * BUGALERT: some rogue implementations use random physical 1082 * partion numbers to break other implementations so lookup 1083 * the number. 1084 */ 1085 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) { 1086 part = ump->partitions[phys_part]; 1087 if (part == NULL) 1088 continue; 1089 if (udf_rw16(part->part_num) == raw_phys_part) 1090 break; 1091 } 1092 1093 DPRINTF(VOLUMES, ("\t%d -> %d(%d) type %d\n", log_part, 1094 raw_phys_part, phys_part, pmap_type)); 1095 1096 if (phys_part == UDF_PARTITIONS) 1097 return EINVAL; 1098 if (pmap_type == UDF_VTOP_TYPE_UNKNOWN) 1099 return EINVAL; 1100 1101 ump->vtop [log_part] = phys_part; 1102 ump->vtop_tp[log_part] = pmap_type; 1103 1104 pmap_pos += pmap_size; 1105 } 1106 /* not winning the beauty contest */ 1107 ump->vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW; 1108 1109 /* test some basic UDF assertions/requirements */ 1110 if ((n_virt > 1) || (n_spar > 1) || (n_meta > 1)) 1111 return EINVAL; 1112 1113 if (n_virt) { 1114 if ((n_phys == 0) || n_spar || n_meta) 1115 return EINVAL; 1116 } 1117 if (n_spar + n_phys == 0) 1118 return EINVAL; 1119 1120 /* vat's can only be on a sequential media */ 1121 ump->data_alloc = UDF_ALLOC_SPACEMAP; 1122 if (n_virt) 1123 ump->data_alloc = UDF_ALLOC_SEQUENTIAL; 1124 1125 ump->meta_alloc = UDF_ALLOC_SPACEMAP; 1126 if (n_virt) 1127 ump->meta_alloc = UDF_ALLOC_VAT; 1128 if (n_meta) 1129 ump->meta_alloc = UDF_ALLOC_METABITMAP; 1130 1131 /* special cases for pseudo-overwrite */ 1132 if (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) { 1133 ump->data_alloc = UDF_ALLOC_SEQUENTIAL; 1134 if (n_meta) { 1135 ump->meta_alloc = UDF_ALLOC_METASEQUENTIAL; 1136 } else { 1137 ump->meta_alloc = UDF_ALLOC_RELAXEDSEQUENTIAL; 1138 } 1139 } 1140 1141 DPRINTF(VOLUMES, ("\tdata alloc scheme %d, meta alloc scheme %d\n", 1142 ump->data_alloc, ump->meta_alloc)); 1143 /* TODO determine partitions to write data and metadata ? */ 1144 1145 /* signal its OK for now */ 1146 return 0; 1147 } 1148 1149 /* --------------------------------------------------------------------- */ 1150 1151 /* 1152 * Read in complete VAT file and check if its indeed a VAT file descriptor 1153 */ 1154 1155 static int 1156 udf_check_for_vat(struct udf_node *vat_node) 1157 { 1158 struct udf_mount *ump; 1159 struct icb_tag *icbtag; 1160 struct timestamp *mtime; 1161 struct regid *regid; 1162 struct udf_vat *vat; 1163 struct udf_logvol_info *lvinfo; 1164 uint32_t vat_length, alloc_length; 1165 uint32_t vat_offset, vat_entries; 1166 uint32_t sector_size; 1167 uint32_t sectors; 1168 uint32_t *raw_vat; 1169 char *regid_name; 1170 int filetype; 1171 int error; 1172 1173 /* vat_length is really 64 bits though impossible */ 1174 1175 DPRINTF(VOLUMES, ("Checking for VAT\n")); 1176 if (!vat_node) 1177 return ENOENT; 1178 1179 /* get mount info */ 1180 ump = vat_node->ump; 1181 1182 /* check assertions */ 1183 assert(vat_node->fe || vat_node->efe); 1184 assert(ump->logvol_integrity); 1185 1186 /* get information from fe/efe */ 1187 if (vat_node->fe) { 1188 vat_length = udf_rw64(vat_node->fe->inf_len); 1189 icbtag = &vat_node->fe->icbtag; 1190 mtime = &vat_node->fe->mtime; 1191 } else { 1192 vat_length = udf_rw64(vat_node->efe->inf_len); 1193 icbtag = &vat_node->efe->icbtag; 1194 mtime = &vat_node->efe->mtime; 1195 } 1196 1197 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */ 1198 filetype = icbtag->file_type; 1199 if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT)) 1200 return ENOENT; 1201 1202 DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length)); 1203 /* place a sanity check on the length; currently 1Mb in size */ 1204 if (vat_length > 1*1024*1024) 1205 return ENOENT; 1206 1207 /* get sector size */ 1208 sector_size = vat_node->ump->discinfo.sector_size; 1209 1210 /* calculate how many sectors to read in and how much to allocate */ 1211 sectors = (vat_length + sector_size -1) / sector_size; 1212 alloc_length = (sectors + 2) * sector_size; 1213 1214 /* try to allocate the space */ 1215 ump->vat_table_alloc_length = alloc_length; 1216 ump->vat_table = malloc(alloc_length, M_UDFVOLD, M_CANFAIL | M_WAITOK); 1217 if (!ump->vat_table) 1218 return ENOMEM; /* impossible to allocate */ 1219 DPRINTF(VOLUMES, ("\talloced fine\n")); 1220 1221 /* read it in! */ 1222 raw_vat = (uint32_t *) ump->vat_table; 1223 error = udf_read_file_extent(vat_node, 0, sectors, (uint8_t *) raw_vat); 1224 if (error) { 1225 DPRINTF(VOLUMES, ("\tread failed : %d\n", error)); 1226 /* not completely readable... :( bomb out */ 1227 free(ump->vat_table, M_UDFVOLD); 1228 ump->vat_table = NULL; 1229 return error; 1230 } 1231 DPRINTF(VOLUMES, ("VAT read in fine!\n")); 1232 1233 /* 1234 * check contents of the file if its the old 1.50 VAT table format. 1235 * Its notoriously broken and allthough some implementations support an 1236 * extention as defined in the UDF 1.50 errata document, its doubtfull 1237 * to be useable since a lot of implementations don't maintain it. 1238 */ 1239 lvinfo = ump->logvol_info; 1240 1241 if (filetype == 0) { 1242 /* definition */ 1243 vat_offset = 0; 1244 vat_entries = (vat_length-36)/4; 1245 1246 /* check 1.50 VAT */ 1247 regid = (struct regid *) (raw_vat + vat_entries); 1248 regid_name = (char *) regid->id; 1249 error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22); 1250 if (error) { 1251 DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n")); 1252 free(ump->vat_table, M_UDFVOLD); 1253 ump->vat_table = NULL; 1254 return ENOENT; 1255 } 1256 /* TODO update LVID from "*UDF VAT LVExtension" ext. attr. */ 1257 } else { 1258 vat = (struct udf_vat *) raw_vat; 1259 1260 /* definition */ 1261 vat_offset = vat->header_len; 1262 vat_entries = (vat_length - vat_offset)/4; 1263 1264 assert(lvinfo); 1265 lvinfo->num_files = vat->num_files; 1266 lvinfo->num_directories = vat->num_directories; 1267 lvinfo->min_udf_readver = vat->min_udf_readver; 1268 lvinfo->min_udf_writever = vat->min_udf_writever; 1269 lvinfo->max_udf_writever = vat->max_udf_writever; 1270 } 1271 1272 ump->vat_offset = vat_offset; 1273 ump->vat_entries = vat_entries; 1274 1275 DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n")); 1276 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED); 1277 ump->logvol_integrity->time = *mtime; 1278 1279 return 0; /* success! */ 1280 } 1281 1282 /* --------------------------------------------------------------------- */ 1283 1284 static int 1285 udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping) 1286 { 1287 struct udf_node *vat_node; 1288 struct long_ad icb_loc; 1289 uint32_t early_vat_loc, late_vat_loc, vat_loc; 1290 int error; 1291 1292 /* mapping info not needed */ 1293 mapping = mapping; 1294 1295 vat_loc = ump->last_possible_vat_location; 1296 early_vat_loc = vat_loc - 2 * ump->discinfo.blockingnr; 1297 early_vat_loc = MAX(early_vat_loc, ump->first_possible_vat_location); 1298 late_vat_loc = vat_loc + 1024; 1299 1300 /* TODO first search last sector? */ 1301 do { 1302 DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc)); 1303 icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART); 1304 icb_loc.loc.lb_num = udf_rw32(vat_loc); 1305 1306 error = udf_get_node(ump, &icb_loc, &vat_node); 1307 if (!error) error = udf_check_for_vat(vat_node); 1308 if (!error) break; 1309 if (vat_node) { 1310 vput(vat_node->vnode); 1311 udf_dispose_node(vat_node); 1312 } 1313 vat_loc--; /* walk backwards */ 1314 } while (vat_loc >= early_vat_loc); 1315 1316 /* we don't need our VAT node anymore */ 1317 if (vat_node) { 1318 vput(vat_node->vnode); 1319 udf_dispose_node(vat_node); 1320 } 1321 1322 return error; 1323 } 1324 1325 /* --------------------------------------------------------------------- */ 1326 1327 static int 1328 udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping) 1329 { 1330 union dscrptr *dscr; 1331 struct part_map_spare *pms = &mapping->pms; 1332 uint32_t lb_num; 1333 int spar, error; 1334 1335 /* 1336 * The partition mapping passed on to us specifies the information we 1337 * need to locate and initialise the sparable partition mapping 1338 * information we need. 1339 */ 1340 1341 DPRINTF(VOLUMES, ("Read sparable table\n")); 1342 ump->sparable_packet_len = udf_rw16(pms->packet_len); 1343 for (spar = 0; spar < pms->n_st; spar++) { 1344 lb_num = pms->st_loc[spar]; 1345 DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num)); 1346 error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr); 1347 if (!error && dscr) { 1348 if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) { 1349 if (ump->sparing_table) 1350 free(ump->sparing_table, M_UDFVOLD); 1351 ump->sparing_table = &dscr->spt; 1352 dscr = NULL; 1353 DPRINTF(VOLUMES, 1354 ("Sparing table accepted (%d entries)\n", 1355 udf_rw16(ump->sparing_table->rt_l))); 1356 break; /* we're done */ 1357 } 1358 } 1359 if (dscr) 1360 free(dscr, M_UDFVOLD); 1361 } 1362 1363 if (ump->sparing_table) 1364 return 0; 1365 1366 return ENOENT; 1367 } 1368 1369 /* --------------------------------------------------------------------- */ 1370 1371 #define UDF_SET_SYSTEMFILE(vp) \ 1372 simple_lock(&(vp)->v_interlock); \ 1373 (vp)->v_flag |= VSYSTEM; \ 1374 simple_unlock(&(vp)->v_interlock);\ 1375 vref(vp); \ 1376 vput(vp); \ 1377 1378 static int 1379 udf_read_metadata_files(struct udf_mount *ump, union udf_pmap *mapping) 1380 { 1381 struct part_map_meta *pmm = &mapping->pmm; 1382 struct long_ad icb_loc; 1383 struct vnode *vp; 1384 int error; 1385 1386 DPRINTF(VOLUMES, ("Reading in Metadata files\n")); 1387 icb_loc.loc.part_num = pmm->part_num; 1388 icb_loc.loc.lb_num = pmm->meta_file_lbn; 1389 DPRINTF(VOLUMES, ("Metadata file\n")); 1390 error = udf_get_node(ump, &icb_loc, &ump->metadata_file); 1391 if (ump->metadata_file) { 1392 vp = ump->metadata_file->vnode; 1393 UDF_SET_SYSTEMFILE(vp); 1394 } 1395 1396 icb_loc.loc.lb_num = pmm->meta_mirror_file_lbn; 1397 if (icb_loc.loc.lb_num != -1) { 1398 DPRINTF(VOLUMES, ("Metadata copy file\n")); 1399 error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_file); 1400 if (ump->metadatamirror_file) { 1401 vp = ump->metadatamirror_file->vnode; 1402 UDF_SET_SYSTEMFILE(vp); 1403 } 1404 } 1405 1406 icb_loc.loc.lb_num = pmm->meta_bitmap_file_lbn; 1407 if (icb_loc.loc.lb_num != -1) { 1408 DPRINTF(VOLUMES, ("Metadata bitmap file\n")); 1409 error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_file); 1410 if (ump->metadatabitmap_file) { 1411 vp = ump->metadatabitmap_file->vnode; 1412 UDF_SET_SYSTEMFILE(vp); 1413 } 1414 } 1415 1416 /* if we're mounting read-only we relax the requirements */ 1417 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) { 1418 error = EFAULT; 1419 if (ump->metadata_file) 1420 error = 0; 1421 if ((ump->metadata_file == NULL) && (ump->metadatamirror_file)) { 1422 printf( "udf mount: Metadata file not readable, " 1423 "substituting Metadata copy file\n"); 1424 ump->metadata_file = ump->metadatamirror_file; 1425 ump->metadatamirror_file = NULL; 1426 error = 0; 1427 } 1428 } else { 1429 /* mounting read/write */ 1430 DPRINTF(VOLUMES, ("udf mount: read only file system\n")); 1431 error = EROFS; 1432 } 1433 DPRINTFIF(VOLUMES, error, ("udf mount: failed to read " 1434 "metadata files\n")); 1435 return error; 1436 } 1437 #undef UDF_SET_SYSTEMFILE 1438 1439 /* --------------------------------------------------------------------- */ 1440 1441 int 1442 udf_read_vds_tables(struct udf_mount *ump, struct udf_args *args) 1443 { 1444 union udf_pmap *mapping; 1445 uint32_t n_pm, mt_l; 1446 uint32_t log_part; 1447 uint8_t *pmap_pos; 1448 int pmap_size; 1449 int error; 1450 1451 /* We have to iterate again over the part mappings for locations */ 1452 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */ 1453 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */ 1454 pmap_pos = ump->logical_vol->maps; 1455 1456 for (log_part = 0; log_part < n_pm; log_part++) { 1457 mapping = (union udf_pmap *) pmap_pos; 1458 switch (ump->vtop_tp[log_part]) { 1459 case UDF_VTOP_TYPE_PHYS : 1460 /* nothing */ 1461 break; 1462 case UDF_VTOP_TYPE_VIRT : 1463 /* search and load VAT */ 1464 error = udf_search_vat(ump, mapping); 1465 if (error) 1466 return ENOENT; 1467 break; 1468 case UDF_VTOP_TYPE_SPARABLE : 1469 /* load one of the sparable tables */ 1470 error = udf_read_sparables(ump, mapping); 1471 if (error) 1472 return ENOENT; 1473 break; 1474 case UDF_VTOP_TYPE_META : 1475 /* load the associated file descriptors */ 1476 error = udf_read_metadata_files(ump, mapping); 1477 if (error) 1478 return ENOENT; 1479 break; 1480 default: 1481 break; 1482 } 1483 pmap_size = pmap_pos[1]; 1484 pmap_pos += pmap_size; 1485 } 1486 1487 return 0; 1488 } 1489 1490 /* --------------------------------------------------------------------- */ 1491 1492 int 1493 udf_read_rootdirs(struct udf_mount *ump, struct udf_args *args) 1494 { 1495 struct udf_node *rootdir_node, *streamdir_node; 1496 union dscrptr *dscr; 1497 struct long_ad fsd_loc, *dir_loc; 1498 uint32_t lb_num, dummy; 1499 uint32_t fsd_len; 1500 int dscr_type; 1501 int error; 1502 1503 /* TODO implement FSD reading in separate function like integrity? */ 1504 /* get fileset descriptor sequence */ 1505 fsd_loc = ump->logical_vol->lv_fsd_loc; 1506 fsd_len = udf_rw32(fsd_loc.len); 1507 1508 dscr = NULL; 1509 error = 0; 1510 while (fsd_len || error) { 1511 DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len)); 1512 /* translate fsd_loc to lb_num */ 1513 error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy); 1514 if (error) 1515 break; 1516 DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num)); 1517 error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr); 1518 /* end markers */ 1519 if (error || (dscr == NULL)) 1520 break; 1521 1522 /* analyse */ 1523 dscr_type = udf_rw16(dscr->tag.id); 1524 if (dscr_type == TAGID_TERM) 1525 break; 1526 if (dscr_type != TAGID_FSD) { 1527 free(dscr, M_UDFVOLD); 1528 return ENOENT; 1529 } 1530 1531 /* 1532 * TODO check for multiple fileset descriptors; its only 1533 * picking the last now. Also check for FSD 1534 * correctness/interpretability 1535 */ 1536 1537 /* update */ 1538 if (ump->fileset_desc) { 1539 free(ump->fileset_desc, M_UDFVOLD); 1540 } 1541 ump->fileset_desc = &dscr->fsd; 1542 dscr = NULL; 1543 1544 /* continue to the next fsd */ 1545 fsd_len -= ump->discinfo.sector_size; 1546 fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1); 1547 1548 /* follow up to fsd->next_ex (long_ad) if its not null */ 1549 if (udf_rw32(ump->fileset_desc->next_ex.len)) { 1550 DPRINTF(VOLUMES, ("follow up FSD extent\n")); 1551 fsd_loc = ump->fileset_desc->next_ex; 1552 fsd_len = udf_rw32(ump->fileset_desc->next_ex.len); 1553 } 1554 } 1555 if (dscr) 1556 free(dscr, M_UDFVOLD); 1557 1558 /* there has to be one */ 1559 if (ump->fileset_desc == NULL) 1560 return ENOENT; 1561 1562 DPRINTF(VOLUMES, ("FSD read in fine\n")); 1563 1564 /* 1565 * Now the FSD is known, read in the rootdirectory and if one exists, 1566 * the system stream dir. Some files in the system streamdir are not 1567 * wanted in this implementation since they are not maintained. If 1568 * writing is enabled we'll delete these files if they exist. 1569 */ 1570 1571 rootdir_node = streamdir_node = NULL; 1572 dir_loc = NULL; 1573 1574 /* try to read in the rootdir */ 1575 dir_loc = &ump->fileset_desc->rootdir_icb; 1576 error = udf_get_node(ump, dir_loc, &rootdir_node); 1577 if (error) 1578 return ENOENT; 1579 1580 /* aparently it read in fine */ 1581 1582 /* 1583 * Try the system stream directory; not very likely in the ones we 1584 * test, but for completeness. 1585 */ 1586 dir_loc = &ump->fileset_desc->streamdir_icb; 1587 if (udf_rw32(dir_loc->len)) { 1588 error = udf_get_node(ump, dir_loc, &streamdir_node); 1589 if (error) 1590 printf("udf mount: streamdir defined but ignored\n"); 1591 if (!error) { 1592 /* 1593 * TODO process streamdir `baddies' i.e. files we dont 1594 * want if R/W 1595 */ 1596 } 1597 } 1598 1599 DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n")); 1600 1601 /* release the vnodes again; they'll be auto-recycled later */ 1602 if (streamdir_node) { 1603 vput(streamdir_node->vnode); 1604 } 1605 if (rootdir_node) { 1606 vput(rootdir_node->vnode); 1607 } 1608 1609 return 0; 1610 } 1611 1612 /* --------------------------------------------------------------------- */ 1613 1614 int 1615 udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc, 1616 uint32_t *lb_numres, uint32_t *extres) 1617 { 1618 struct part_desc *pdesc; 1619 struct spare_map_entry *sme; 1620 struct file_entry *fe; 1621 struct extfile_entry *efe; 1622 struct short_ad *s_ad; 1623 struct long_ad *l_ad; 1624 uint64_t cur_offset; 1625 uint32_t *trans; 1626 uint32_t lb_num, plb_num, lb_rel, lb_packet; 1627 uint32_t sector_size, len, alloclen; 1628 uint8_t *pos; 1629 int rel, vpart, part, addr_type, icblen, icbflags, flags; 1630 1631 assert(ump && icb_loc && lb_numres); 1632 1633 vpart = udf_rw16(icb_loc->loc.part_num); 1634 lb_num = udf_rw32(icb_loc->loc.lb_num); 1635 if (vpart < 0 || vpart > UDF_VTOP_RAWPART) 1636 return EINVAL; 1637 1638 part = ump->vtop[vpart]; 1639 pdesc = ump->partitions[part]; 1640 1641 switch (ump->vtop_tp[vpart]) { 1642 case UDF_VTOP_TYPE_RAW : 1643 /* 1:1 to the end of the device */ 1644 *lb_numres = lb_num; 1645 *extres = INT_MAX; 1646 return 0; 1647 case UDF_VTOP_TYPE_PHYS : 1648 /* transform into its disc logical block */ 1649 if (lb_num > udf_rw32(pdesc->part_len)) 1650 return EINVAL; 1651 *lb_numres = lb_num + udf_rw32(pdesc->start_loc); 1652 1653 /* extent from here to the end of the partition */ 1654 *extres = udf_rw32(pdesc->part_len) - lb_num; 1655 return 0; 1656 case UDF_VTOP_TYPE_VIRT : 1657 /* only maps one sector, lookup in VAT */ 1658 if (lb_num >= ump->vat_entries) /* XXX > or >= ? */ 1659 return EINVAL; 1660 1661 /* lookup in virtual allocation table */ 1662 trans = (uint32_t *) (ump->vat_table + ump->vat_offset); 1663 lb_num = udf_rw32(trans[lb_num]); 1664 1665 /* transform into its disc logical block */ 1666 if (lb_num > udf_rw32(pdesc->part_len)) 1667 return EINVAL; 1668 *lb_numres = lb_num + udf_rw32(pdesc->start_loc); 1669 1670 /* just one logical block */ 1671 *extres = 1; 1672 return 0; 1673 case UDF_VTOP_TYPE_SPARABLE : 1674 /* check if the packet containing the lb_num is remapped */ 1675 lb_packet = lb_num / ump->sparable_packet_len; 1676 lb_rel = lb_num % ump->sparable_packet_len; 1677 1678 for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) { 1679 sme = &ump->sparing_table->entries[rel]; 1680 if (lb_packet == udf_rw32(sme->org)) { 1681 /* NOTE maps to absolute disc logical block! */ 1682 *lb_numres = udf_rw32(sme->map) + lb_rel; 1683 *extres = ump->sparable_packet_len - lb_rel; 1684 return 0; 1685 } 1686 } 1687 1688 /* transform into its disc logical block */ 1689 if (lb_num > udf_rw32(pdesc->part_len)) 1690 return EINVAL; 1691 *lb_numres = lb_num + udf_rw32(pdesc->start_loc); 1692 1693 /* rest of block */ 1694 *extres = ump->sparable_packet_len - lb_rel; 1695 return 0; 1696 case UDF_VTOP_TYPE_META : 1697 /* we have to look into the file's allocation descriptors */ 1698 /* free after udf_translate_file_extent() */ 1699 /* XXX sector size or lb_size? */ 1700 sector_size = ump->discinfo.sector_size; 1701 /* XXX should we claim exclusive access to the metafile ? */ 1702 fe = ump->metadata_file->fe; 1703 efe = ump->metadata_file->efe; 1704 if (fe) { 1705 alloclen = udf_rw32(fe->l_ad); 1706 pos = &fe->data[0] + udf_rw32(fe->l_ea); 1707 icbflags = udf_rw16(fe->icbtag.flags); 1708 } else { 1709 assert(efe); 1710 alloclen = udf_rw32(efe->l_ad); 1711 pos = &efe->data[0] + udf_rw32(efe->l_ea); 1712 icbflags = udf_rw16(efe->icbtag.flags); 1713 } 1714 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 1715 1716 cur_offset = 0; 1717 while (alloclen) { 1718 if (addr_type == UDF_ICB_SHORT_ALLOC) { 1719 icblen = sizeof(struct short_ad); 1720 s_ad = (struct short_ad *) pos; 1721 len = udf_rw32(s_ad->len); 1722 plb_num = udf_rw32(s_ad->lb_num); 1723 } else { 1724 /* should not be present, but why not */ 1725 icblen = sizeof(struct long_ad); 1726 l_ad = (struct long_ad *) pos; 1727 len = udf_rw32(l_ad->len); 1728 plb_num = udf_rw32(l_ad->loc.lb_num); 1729 /* pvpart_num = udf_rw16(l_ad->loc.part_num); */ 1730 } 1731 /* process extent */ 1732 flags = UDF_EXT_FLAGS(len); 1733 len = UDF_EXT_LEN(len); 1734 1735 if (cur_offset + len > lb_num * sector_size) { 1736 if (flags != UDF_EXT_ALLOCATED) 1737 return EINVAL; 1738 lb_rel = lb_num - cur_offset / sector_size; 1739 /* remainder of this extent */ 1740 *lb_numres = plb_num + lb_rel + 1741 udf_rw32(pdesc->start_loc); 1742 *extres = (len / sector_size) - lb_rel; 1743 return 0; 1744 } 1745 cur_offset += len; 1746 pos += icblen; 1747 alloclen -= icblen; 1748 } 1749 /* not found */ 1750 DPRINTF(TRANSLATE, ("Metadata partition translation failed\n")); 1751 return EINVAL; 1752 default: 1753 printf("UDF vtop translation scheme %d unimplemented yet\n", 1754 ump->vtop_tp[vpart]); 1755 } 1756 1757 return EINVAL; 1758 } 1759 1760 /* --------------------------------------------------------------------- */ 1761 1762 /* To make absolutely sure we are NOT returning zero, add one :) */ 1763 1764 long 1765 udf_calchash(struct long_ad *icbptr) 1766 { 1767 /* ought to be enough since each mountpoint has its own chain */ 1768 return udf_rw32(icbptr->loc.lb_num) + 1; 1769 } 1770 1771 /* --------------------------------------------------------------------- */ 1772 1773 static struct udf_node * 1774 udf_hashget(struct udf_mount *ump, struct long_ad *icbptr) 1775 { 1776 struct udf_node *unp; 1777 struct vnode *vp; 1778 uint32_t hashline; 1779 1780 loop: 1781 simple_lock(&ump->ihash_slock); 1782 1783 hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK; 1784 LIST_FOREACH(unp, &ump->udf_nodes[hashline], hashchain) { 1785 assert(unp); 1786 if (unp->loc.loc.lb_num == icbptr->loc.lb_num && 1787 unp->loc.loc.part_num == icbptr->loc.part_num) { 1788 vp = unp->vnode; 1789 assert(vp); 1790 simple_lock(&vp->v_interlock); 1791 simple_unlock(&ump->ihash_slock); 1792 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) 1793 goto loop; 1794 return unp; 1795 } 1796 } 1797 simple_unlock(&ump->ihash_slock); 1798 1799 return NULL; 1800 } 1801 1802 /* --------------------------------------------------------------------- */ 1803 1804 static void 1805 udf_hashins(struct udf_node *unp) 1806 { 1807 struct udf_mount *ump; 1808 uint32_t hashline; 1809 1810 ump = unp->ump; 1811 simple_lock(&ump->ihash_slock); 1812 1813 hashline = udf_calchash(&unp->loc) & UDF_INODE_HASHMASK; 1814 LIST_INSERT_HEAD(&ump->udf_nodes[hashline], unp, hashchain); 1815 1816 simple_unlock(&ump->ihash_slock); 1817 } 1818 1819 /* --------------------------------------------------------------------- */ 1820 1821 static void 1822 udf_hashrem(struct udf_node *unp) 1823 { 1824 struct udf_mount *ump; 1825 1826 ump = unp->ump; 1827 simple_lock(&ump->ihash_slock); 1828 1829 LIST_REMOVE(unp, hashchain); 1830 1831 simple_unlock(&ump->ihash_slock); 1832 } 1833 1834 /* --------------------------------------------------------------------- */ 1835 1836 int 1837 udf_dispose_locked_node(struct udf_node *node) 1838 { 1839 if (!node) 1840 return 0; 1841 if (node->vnode) 1842 VOP_UNLOCK(node->vnode, 0); 1843 return udf_dispose_node(node); 1844 } 1845 1846 /* --------------------------------------------------------------------- */ 1847 1848 int 1849 udf_dispose_node(struct udf_node *node) 1850 { 1851 struct vnode *vp; 1852 1853 DPRINTF(NODE, ("udf_dispose_node called on node %p\n", node)); 1854 if (!node) { 1855 DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n")); 1856 return 0; 1857 } 1858 1859 vp = node->vnode; 1860 1861 /* TODO extended attributes and streamdir */ 1862 1863 /* remove from our hash lookup table */ 1864 udf_hashrem(node); 1865 1866 /* destroy genfs structures */ 1867 genfs_node_destroy(vp); 1868 1869 /* dissociate our udf_node from the vnode */ 1870 vp->v_data = NULL; 1871 1872 /* free associated memory and the node itself */ 1873 if (node->fe) 1874 pool_put(node->ump->desc_pool, node->fe); 1875 if (node->efe) 1876 pool_put(node->ump->desc_pool, node->efe); 1877 pool_put(&udf_node_pool, node); 1878 1879 return 0; 1880 } 1881 1882 /* --------------------------------------------------------------------- */ 1883 1884 /* 1885 * Genfs interfacing 1886 * 1887 * static const struct genfs_ops udffs_genfsops = { 1888 * .gop_size = genfs_size, 1889 * size of transfers 1890 * .gop_alloc = udf_gop_alloc, 1891 * unknown 1892 * .gop_write = genfs_gop_write, 1893 * putpages interface code 1894 * .gop_markupdate = udf_gop_markupdate, 1895 * set update/modify flags etc. 1896 * } 1897 */ 1898 1899 /* 1900 * Genfs interface. These four functions are the only ones defined though not 1901 * documented... great.... why is chosen for the `.' initialisers i dont know 1902 * but other filingsystems seem to use it this way. 1903 */ 1904 1905 static int 1906 udf_gop_alloc(struct vnode *vp, off_t off, 1907 off_t len, int flags, kauth_cred_t cred) 1908 { 1909 return 0; 1910 } 1911 1912 1913 static void 1914 udf_gop_markupdate(struct vnode *vp, int flags) 1915 { 1916 struct udf_node *udf_node = VTOI(vp); 1917 u_long mask; 1918 1919 udf_node = udf_node; /* shut up gcc */ 1920 1921 mask = 0; 1922 #ifdef notyet 1923 if ((flags & GOP_UPDATE_ACCESSED) != 0) { 1924 mask = UDF_SET_ACCESS; 1925 } 1926 if ((flags & GOP_UPDATE_MODIFIED) != 0) { 1927 mask |= UDF_SET_UPDATE; 1928 } 1929 if (mask) { 1930 udf_node->update_flag |= mask; 1931 } 1932 #endif 1933 /* msdosfs doesn't do it, but shouldn't we update the times here? */ 1934 } 1935 1936 1937 static const struct genfs_ops udf_genfsops = { 1938 .gop_size = genfs_size, 1939 .gop_alloc = udf_gop_alloc, 1940 .gop_write = genfs_gop_write, 1941 .gop_markupdate = udf_gop_markupdate, 1942 }; 1943 1944 /* --------------------------------------------------------------------- */ 1945 1946 /* 1947 * Each node can have an attached streamdir node though not 1948 * recursively. These are otherwise known as named substreams/named 1949 * extended attributes that have no size limitations. 1950 * 1951 * `Normal' extended attributes are indicated with a number and are recorded 1952 * in either the fe/efe descriptor itself for small descriptors or recorded in 1953 * the attached extended attribute file. Since this file can get fragmented, 1954 * care ought to be taken. 1955 */ 1956 1957 int 1958 udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc, 1959 struct udf_node **noderes) 1960 { 1961 union dscrptr *dscr, *tmpdscr; 1962 struct udf_node *node; 1963 struct vnode *nvp; 1964 struct long_ad icb_loc; 1965 extern int (**udf_vnodeop_p)(void *); 1966 uint64_t file_size; 1967 uint32_t lb_size, sector, dummy; 1968 int udf_file_type, dscr_type, strat, strat4096, needs_indirect; 1969 int error; 1970 1971 DPRINTF(NODE, ("udf_get_node called\n")); 1972 *noderes = node = NULL; 1973 1974 /* lock to disallow simultanious creation of same node */ 1975 lockmgr(&ump->get_node_lock, LK_EXCLUSIVE, NULL); 1976 1977 DPRINTF(NODE, ("\tlookup in hash table\n")); 1978 /* lookup in hash table */ 1979 assert(ump); 1980 assert(node_icb_loc); 1981 node = udf_hashget(ump, node_icb_loc); 1982 if (node) { 1983 DPRINTF(NODE, ("\tgot it from the hash!\n")); 1984 /* vnode is returned locked */ 1985 *noderes = node; 1986 lockmgr(&ump->get_node_lock, LK_RELEASE, NULL); 1987 return 0; 1988 } 1989 1990 /* garbage check: translate node_icb_loc to sectornr */ 1991 error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy); 1992 if (error) { 1993 /* no use, this will fail anyway */ 1994 lockmgr(&ump->get_node_lock, LK_RELEASE, NULL); 1995 return EINVAL; 1996 } 1997 1998 /* build node (do initialise!) */ 1999 node = pool_get(&udf_node_pool, PR_WAITOK); 2000 memset(node, 0, sizeof(struct udf_node)); 2001 2002 DPRINTF(NODE, ("\tget new vnode\n")); 2003 /* give it a vnode */ 2004 error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp); 2005 if (error) { 2006 pool_put(&udf_node_pool, node); 2007 lockmgr(&ump->get_node_lock, LK_RELEASE, NULL); 2008 return error; 2009 } 2010 2011 /* always return locked vnode */ 2012 if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) { 2013 /* recycle vnode and unlock; simultanious will fail too */ 2014 ungetnewvnode(nvp); 2015 lockmgr(&ump->get_node_lock, LK_RELEASE, NULL); 2016 return error; 2017 } 2018 2019 /* initialise crosslinks, note location of fe/efe for hashing */ 2020 node->ump = ump; 2021 node->vnode = nvp; 2022 nvp->v_data = node; 2023 node->loc = *node_icb_loc; 2024 node->lockf = 0; 2025 2026 /* insert into the hash lookup */ 2027 udf_hashins(node); 2028 2029 /* safe to unlock, the entry is in the hash table, vnode is locked */ 2030 lockmgr(&ump->get_node_lock, LK_RELEASE, NULL); 2031 2032 icb_loc = *node_icb_loc; 2033 needs_indirect = 0; 2034 strat4096 = 0; 2035 udf_file_type = UDF_ICB_FILETYPE_UNKNOWN; 2036 file_size = 0; 2037 lb_size = udf_rw32(ump->logical_vol->lb_size); 2038 2039 do { 2040 error = udf_translate_vtop(ump, &icb_loc, §or, &dummy); 2041 if (error) 2042 break; 2043 2044 /* try to read in fe/efe */ 2045 error = udf_read_descriptor(ump, sector, M_UDFTEMP, &tmpdscr); 2046 2047 /* blank sector marks end of sequence, check this */ 2048 if ((tmpdscr == NULL) && (!strat4096)) 2049 error = ENOENT; 2050 2051 /* break if read error or blank sector */ 2052 if (error || (tmpdscr == NULL)) 2053 break; 2054 2055 /* process descriptor based on the descriptor type */ 2056 dscr_type = udf_rw16(tmpdscr->tag.id); 2057 2058 /* if dealing with an indirect entry, follow the link */ 2059 if (dscr_type == TAGID_INDIRECT_ENTRY) { 2060 needs_indirect = 0; 2061 icb_loc = tmpdscr->inde.indirect_icb; 2062 free(tmpdscr, M_UDFTEMP); 2063 continue; 2064 } 2065 2066 /* only file entries and extended file entries allowed here */ 2067 if ((dscr_type != TAGID_FENTRY) && 2068 (dscr_type != TAGID_EXTFENTRY)) { 2069 free(tmpdscr, M_UDFTEMP); 2070 error = ENOENT; 2071 break; 2072 } 2073 2074 /* get descriptor space from our pool */ 2075 KASSERT(udf_tagsize(tmpdscr, lb_size) == lb_size); 2076 2077 dscr = pool_get(ump->desc_pool, PR_WAITOK); 2078 memcpy(dscr, tmpdscr, lb_size); 2079 free(tmpdscr, M_UDFTEMP); 2080 2081 /* record and process/update (ext)fentry */ 2082 if (dscr_type == TAGID_FENTRY) { 2083 if (node->fe) 2084 pool_put(ump->desc_pool, node->fe); 2085 node->fe = &dscr->fe; 2086 strat = udf_rw16(node->fe->icbtag.strat_type); 2087 udf_file_type = node->fe->icbtag.file_type; 2088 file_size = udf_rw64(node->fe->inf_len); 2089 } else { 2090 if (node->efe) 2091 pool_put(ump->desc_pool, node->efe); 2092 node->efe = &dscr->efe; 2093 strat = udf_rw16(node->efe->icbtag.strat_type); 2094 udf_file_type = node->efe->icbtag.file_type; 2095 file_size = udf_rw64(node->efe->inf_len); 2096 } 2097 2098 /* check recording strategy (structure) */ 2099 2100 /* 2101 * Strategy 4096 is a daisy linked chain terminating with an 2102 * unrecorded sector or a TERM descriptor. The next 2103 * descriptor is to be found in the sector that follows the 2104 * current sector. 2105 */ 2106 if (strat == 4096) { 2107 strat4096 = 1; 2108 needs_indirect = 1; 2109 2110 icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1; 2111 } 2112 2113 /* 2114 * Strategy 4 is the normal strategy and terminates, but if 2115 * we're in strategy 4096, we can't have strategy 4 mixed in 2116 */ 2117 2118 if (strat == 4) { 2119 if (strat4096) { 2120 error = EINVAL; 2121 break; 2122 } 2123 break; /* done */ 2124 } 2125 } while (!error); 2126 2127 if (error) { 2128 /* recycle udf_node */ 2129 udf_dispose_node(node); 2130 2131 /* recycle vnode */ 2132 nvp->v_data = NULL; 2133 ungetnewvnode(nvp); 2134 2135 return EINVAL; /* error code ok? */ 2136 } 2137 2138 /* post process and initialise node */ 2139 2140 /* assert no references to dscr anymore beyong this point */ 2141 assert((node->fe) || (node->efe)); 2142 dscr = NULL; 2143 2144 /* 2145 * Record where to record an updated version of the descriptor. If 2146 * there is a sequence of indirect entries, icb_loc will have been 2147 * updated. Its the write disipline to allocate new space and to make 2148 * sure the chain is maintained. 2149 * 2150 * `needs_indirect' flags if the next location is to be filled with 2151 * with an indirect entry. 2152 */ 2153 node->next_loc = icb_loc; 2154 node->needs_indirect = needs_indirect; 2155 2156 /* 2157 * Translate UDF filetypes into vnode types. 2158 * 2159 * Systemfiles like the meta main and mirror files are not treated as 2160 * normal files, so we type them as having no type. UDF dictates that 2161 * they are not allowed to be visible. 2162 */ 2163 2164 /* TODO specfs, fifofs etc etc. vnops setting */ 2165 switch (udf_file_type) { 2166 case UDF_ICB_FILETYPE_DIRECTORY : 2167 case UDF_ICB_FILETYPE_STREAMDIR : 2168 nvp->v_type = VDIR; 2169 break; 2170 case UDF_ICB_FILETYPE_BLOCKDEVICE : 2171 nvp->v_type = VBLK; 2172 break; 2173 case UDF_ICB_FILETYPE_CHARDEVICE : 2174 nvp->v_type = VCHR; 2175 break; 2176 case UDF_ICB_FILETYPE_SYMLINK : 2177 nvp->v_type = VLNK; 2178 break; 2179 case UDF_ICB_FILETYPE_VAT : 2180 case UDF_ICB_FILETYPE_META_MAIN : 2181 case UDF_ICB_FILETYPE_META_MIRROR : 2182 nvp->v_type = VNON; 2183 break; 2184 case UDF_ICB_FILETYPE_RANDOMACCESS : 2185 case UDF_ICB_FILETYPE_REALTIME : 2186 nvp->v_type = VREG; 2187 break; 2188 default: 2189 /* YIKES, either a block/char device, fifo or something else */ 2190 nvp->v_type = VNON; 2191 } 2192 2193 /* initialise genfs */ 2194 genfs_node_init(nvp, &udf_genfsops); 2195 2196 /* don't forget to set vnode's v_size */ 2197 uvm_vnp_setsize(nvp, file_size); 2198 2199 /* TODO ext attr and streamdir nodes */ 2200 2201 *noderes = node; 2202 2203 return 0; 2204 } 2205 2206 /* --------------------------------------------------------------------- */ 2207 2208 /* UDF<->unix converters */ 2209 2210 /* --------------------------------------------------------------------- */ 2211 2212 static mode_t 2213 udf_perm_to_unix_mode(uint32_t perm) 2214 { 2215 mode_t mode; 2216 2217 mode = ((perm & UDF_FENTRY_PERM_USER_MASK) ); 2218 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2); 2219 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4); 2220 2221 return mode; 2222 } 2223 2224 /* --------------------------------------------------------------------- */ 2225 2226 #ifdef notyet 2227 static uint32_t 2228 unix_mode_to_udf_perm(mode_t mode) 2229 { 2230 uint32_t perm; 2231 2232 perm = ((mode & S_IRWXO) ); 2233 perm |= ((mode & S_IRWXG) << 2); 2234 perm |= ((mode & S_IRWXU) << 4); 2235 perm |= ((mode & S_IWOTH) << 3); 2236 perm |= ((mode & S_IWGRP) << 5); 2237 perm |= ((mode & S_IWUSR) << 7); 2238 2239 return perm; 2240 } 2241 #endif 2242 2243 /* --------------------------------------------------------------------- */ 2244 2245 static uint32_t 2246 udf_icb_to_unix_filetype(uint32_t icbftype) 2247 { 2248 switch (icbftype) { 2249 case UDF_ICB_FILETYPE_DIRECTORY : 2250 case UDF_ICB_FILETYPE_STREAMDIR : 2251 return S_IFDIR; 2252 case UDF_ICB_FILETYPE_FIFO : 2253 return S_IFIFO; 2254 case UDF_ICB_FILETYPE_CHARDEVICE : 2255 return S_IFCHR; 2256 case UDF_ICB_FILETYPE_BLOCKDEVICE : 2257 return S_IFBLK; 2258 case UDF_ICB_FILETYPE_RANDOMACCESS : 2259 case UDF_ICB_FILETYPE_REALTIME : 2260 return S_IFREG; 2261 case UDF_ICB_FILETYPE_SYMLINK : 2262 return S_IFLNK; 2263 case UDF_ICB_FILETYPE_SOCKET : 2264 return S_IFSOCK; 2265 } 2266 /* no idea what this is */ 2267 return 0; 2268 } 2269 2270 /* --------------------------------------------------------------------- */ 2271 2272 /* TODO KNF-ify */ 2273 2274 void 2275 udf_to_unix_name(char *result, char *id, int len, struct charspec *chsp) 2276 { 2277 uint16_t *raw_name, *unix_name; 2278 uint16_t *inchp, ch; 2279 uint8_t *outchp; 2280 int ucode_chars, nice_uchars; 2281 2282 raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK); 2283 unix_name = raw_name + 1024; /* split space in half */ 2284 assert(sizeof(char) == sizeof(uint8_t)); 2285 outchp = (uint8_t *) result; 2286 if ((chsp->type == 0) && (strcmp((char*) chsp->inf, "OSTA Compressed Unicode") == 0)) { 2287 *raw_name = *unix_name = 0; 2288 ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name); 2289 ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name)); 2290 nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars); 2291 for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) { 2292 ch = *inchp; 2293 /* XXX sloppy unicode -> latin */ 2294 *outchp++ = ch & 255; 2295 if (!ch) break; 2296 } 2297 *outchp++ = 0; 2298 } else { 2299 /* assume 8bit char length byte latin-1 */ 2300 assert(*id == 8); 2301 strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1))); 2302 } 2303 free(raw_name, M_UDFTEMP); 2304 } 2305 2306 /* --------------------------------------------------------------------- */ 2307 2308 /* TODO KNF-ify */ 2309 2310 void 2311 unix_to_udf_name(char *result, char *name, 2312 uint8_t *result_len, struct charspec *chsp) 2313 { 2314 uint16_t *raw_name; 2315 int udf_chars, name_len; 2316 char *inchp; 2317 uint16_t *outchp; 2318 2319 raw_name = malloc(1024, M_UDFTEMP, M_WAITOK); 2320 /* convert latin-1 or whatever to unicode-16 */ 2321 *raw_name = 0; 2322 name_len = 0; 2323 inchp = name; 2324 outchp = raw_name; 2325 while (*inchp) { 2326 *outchp++ = (uint16_t) (*inchp++); 2327 name_len++; 2328 } 2329 2330 if ((chsp->type == 0) && (strcmp((char *) chsp->inf, "OSTA Compressed Unicode") == 0)) { 2331 udf_chars = udf_CompressUnicode(name_len, 8, (unicode_t *) raw_name, (byte *) result); 2332 } else { 2333 /* XXX assume 8bit char length byte latin-1 */ 2334 *result++ = 8; udf_chars = 1; 2335 strncpy(result, name + 1, strlen(name+1)); 2336 udf_chars += strlen(name); 2337 } 2338 *result_len = udf_chars; 2339 free(raw_name, M_UDFTEMP); 2340 } 2341 2342 /* --------------------------------------------------------------------- */ 2343 2344 void 2345 udf_timestamp_to_timespec(struct udf_mount *ump, 2346 struct timestamp *timestamp, 2347 struct timespec *timespec) 2348 { 2349 struct clock_ymdhms ymdhms; 2350 uint32_t usecs, secs, nsecs; 2351 uint16_t tz; 2352 2353 /* fill in ymdhms structure from timestamp */ 2354 memset(&ymdhms, 0, sizeof(ymdhms)); 2355 ymdhms.dt_year = udf_rw16(timestamp->year); 2356 ymdhms.dt_mon = timestamp->month; 2357 ymdhms.dt_day = timestamp->day; 2358 ymdhms.dt_wday = 0; /* ? */ 2359 ymdhms.dt_hour = timestamp->hour; 2360 ymdhms.dt_min = timestamp->minute; 2361 ymdhms.dt_sec = timestamp->second; 2362 2363 secs = clock_ymdhms_to_secs(&ymdhms); 2364 usecs = timestamp->usec + 2365 100*timestamp->hund_usec + 10000*timestamp->centisec; 2366 nsecs = usecs * 1000; 2367 2368 /* 2369 * Calculate the time zone. The timezone is 12 bit signed 2's 2370 * compliment, so we gotta do some extra magic to handle it right. 2371 */ 2372 tz = udf_rw16(timestamp->type_tz); 2373 tz &= 0x0fff; /* only lower 12 bits are significant */ 2374 if (tz & 0x0800) /* sign extention */ 2375 tz |= 0xf000; 2376 2377 /* TODO check timezone conversion */ 2378 /* check if we are specified a timezone to convert */ 2379 if (udf_rw16(timestamp->type_tz) & 0x1000) { 2380 if ((int16_t) tz != -2047) 2381 secs -= (int16_t) tz * 60; 2382 } else { 2383 secs -= ump->mount_args.gmtoff; 2384 } 2385 2386 timespec->tv_sec = secs; 2387 timespec->tv_nsec = nsecs; 2388 } 2389 2390 /* --------------------------------------------------------------------- */ 2391 2392 /* 2393 * Attribute and filetypes converters with get/set pairs 2394 */ 2395 2396 uint32_t 2397 udf_getaccessmode(struct udf_node *udf_node) 2398 { 2399 struct file_entry *fe; 2400 struct extfile_entry *efe; 2401 uint32_t udf_perm, icbftype; 2402 uint32_t mode, ftype; 2403 uint16_t icbflags; 2404 2405 if (udf_node->fe) { 2406 fe = udf_node->fe; 2407 udf_perm = udf_rw32(fe->perm); 2408 icbftype = fe->icbtag.file_type; 2409 icbflags = udf_rw16(fe->icbtag.flags); 2410 } else { 2411 assert(udf_node->efe); 2412 efe = udf_node->efe; 2413 udf_perm = udf_rw32(efe->perm); 2414 icbftype = efe->icbtag.file_type; 2415 icbflags = udf_rw16(efe->icbtag.flags); 2416 } 2417 2418 mode = udf_perm_to_unix_mode(udf_perm); 2419 ftype = udf_icb_to_unix_filetype(icbftype); 2420 2421 /* set suid, sgid, sticky from flags in fe/efe */ 2422 if (icbflags & UDF_ICB_TAG_FLAGS_SETUID) 2423 mode |= S_ISUID; 2424 if (icbflags & UDF_ICB_TAG_FLAGS_SETGID) 2425 mode |= S_ISGID; 2426 if (icbflags & UDF_ICB_TAG_FLAGS_STICKY) 2427 mode |= S_ISVTX; 2428 2429 return mode | ftype; 2430 } 2431 2432 /* --------------------------------------------------------------------- */ 2433 2434 /* 2435 * Directory read and manipulation functions 2436 */ 2437 2438 int 2439 udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen, 2440 struct long_ad *icb_loc) 2441 { 2442 struct udf_node *dir_node = VTOI(vp); 2443 struct file_entry *fe; 2444 struct extfile_entry *efe; 2445 struct fileid_desc *fid; 2446 struct dirent dirent; 2447 uint64_t file_size, diroffset; 2448 uint32_t lb_size; 2449 int found, error; 2450 2451 /* get directory filesize */ 2452 if (dir_node->fe) { 2453 fe = dir_node->fe; 2454 file_size = udf_rw64(fe->inf_len); 2455 } else { 2456 assert(dir_node->efe); 2457 efe = dir_node->efe; 2458 file_size = udf_rw64(efe->inf_len); 2459 } 2460 2461 /* allocate temporary space for fid */ 2462 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size); 2463 fid = malloc(lb_size, M_TEMP, M_WAITOK); 2464 2465 found = 0; 2466 diroffset = dir_node->last_diroffset; 2467 2468 /* 2469 * if the directory is trunced or if we have never visited it yet, 2470 * start at the end. 2471 */ 2472 if ((diroffset >= file_size) || (diroffset == 0)) { 2473 diroffset = dir_node->last_diroffset = file_size; 2474 } 2475 2476 while (!found) { 2477 /* if at the end, go trough zero */ 2478 if (diroffset >= file_size) 2479 diroffset = 0; 2480 2481 /* transfer a new fid/dirent */ 2482 error = udf_read_fid_stream(vp, &diroffset, fid, &dirent); 2483 if (error) 2484 break; 2485 2486 /* skip deleted entries */ 2487 if ((fid->file_char & UDF_FILE_CHAR_DEL) == 0) { 2488 if ((strlen(dirent.d_name) == namelen) && 2489 (strncmp(dirent.d_name, name, namelen) == 0)) { 2490 found = 1; 2491 *icb_loc = fid->icb; 2492 } 2493 } 2494 2495 if (diroffset == dir_node->last_diroffset) { 2496 /* we have cycled */ 2497 break; 2498 } 2499 } 2500 free(fid, M_TEMP); 2501 dir_node->last_diroffset = diroffset; 2502 2503 return found; 2504 } 2505 2506 /* --------------------------------------------------------------------- */ 2507 2508 /* 2509 * Read one fid and process it into a dirent and advance to the next (*fid) 2510 * has to be allocated a logical block in size, (*dirent) struct dirent length 2511 */ 2512 2513 int 2514 udf_read_fid_stream(struct vnode *vp, uint64_t *offset, 2515 struct fileid_desc *fid, struct dirent *dirent) 2516 { 2517 struct udf_node *dir_node = VTOI(vp); 2518 struct udf_mount *ump = dir_node->ump; 2519 struct file_entry *fe; 2520 struct extfile_entry *efe; 2521 struct uio dir_uio; 2522 struct iovec dir_iovec; 2523 uint32_t entry_length, lb_size; 2524 uint64_t file_size; 2525 char *fid_name; 2526 int enough, error; 2527 2528 assert(fid); 2529 assert(dirent); 2530 assert(dir_node); 2531 assert(offset); 2532 assert(*offset != 1); 2533 2534 DPRINTF(FIDS, ("read_fid_stream called\n")); 2535 /* check if we're past the end of the directory */ 2536 if (dir_node->fe) { 2537 fe = dir_node->fe; 2538 file_size = udf_rw64(fe->inf_len); 2539 } else { 2540 assert(dir_node->efe); 2541 efe = dir_node->efe; 2542 file_size = udf_rw64(efe->inf_len); 2543 } 2544 if (*offset >= file_size) 2545 return EINVAL; 2546 2547 /* get maximum length of FID descriptor */ 2548 lb_size = udf_rw32(ump->logical_vol->lb_size); 2549 2550 /* initialise return values */ 2551 entry_length = 0; 2552 memset(dirent, 0, sizeof(struct dirent)); 2553 memset(fid, 0, lb_size); 2554 2555 /* TODO use vn_rdwr instead of creating our own uio */ 2556 /* read part of the directory */ 2557 memset(&dir_uio, 0, sizeof(struct uio)); 2558 dir_uio.uio_rw = UIO_READ; /* read into this space */ 2559 dir_uio.uio_iovcnt = 1; 2560 dir_uio.uio_iov = &dir_iovec; 2561 UIO_SETUP_SYSSPACE(&dir_uio); 2562 dir_iovec.iov_base = fid; 2563 dir_iovec.iov_len = lb_size; 2564 dir_uio.uio_offset = *offset; 2565 2566 /* limit length of read in piece */ 2567 dir_uio.uio_resid = MIN(file_size - (*offset), lb_size); 2568 2569 /* read the part into the fid space */ 2570 error = VOP_READ(vp, &dir_uio, IO_ALTSEMANTICS, NOCRED); 2571 if (error) 2572 return error; 2573 2574 /* 2575 * Check if we got a whole descriptor. 2576 * XXX Try to `resync' directory stream when something is very wrong. 2577 * 2578 */ 2579 enough = (dir_uio.uio_offset - (*offset) >= UDF_FID_SIZE); 2580 if (!enough) { 2581 /* short dir ... */ 2582 return EIO; 2583 } 2584 2585 /* check if our FID header is OK */ 2586 error = udf_check_tag(fid); 2587 DPRINTFIF(FIDS, error, ("read fids: tag check failed\n")); 2588 if (!error) { 2589 if (udf_rw16(fid->tag.id) != TAGID_FID) 2590 error = ENOENT; 2591 } 2592 DPRINTFIF(FIDS, !error, ("\ttag checked ok: got TAGID_FID\n")); 2593 2594 /* check for length */ 2595 if (!error) { 2596 entry_length = udf_fidsize(fid, lb_size); 2597 enough = (dir_uio.uio_offset - (*offset) >= entry_length); 2598 } 2599 DPRINTFIF(FIDS, !error, ("\tentry_length = %d, enough = %s\n", 2600 entry_length, enough?"yes":"no")); 2601 2602 if (!enough) { 2603 /* short dir ... bomb out */ 2604 return EIO; 2605 } 2606 2607 /* check FID contents */ 2608 if (!error) { 2609 error = udf_check_tag_payload((union dscrptr *) fid, lb_size); 2610 DPRINTF(FIDS, ("\tpayload checked ok\n")); 2611 } 2612 if (error) { 2613 /* note that is sometimes a bit quick to report */ 2614 printf("BROKEN DIRECTORY ENTRY\n"); 2615 /* RESYNC? */ 2616 /* TODO: use udf_resync_fid_stream */ 2617 return EIO; 2618 } 2619 DPRINTF(FIDS, ("\tinterpret FID\n")); 2620 2621 /* we got a whole and valid descriptor! */ 2622 2623 /* create resulting dirent structure */ 2624 fid_name = (char *) fid->data + udf_rw16(fid->l_iu); 2625 udf_to_unix_name(dirent->d_name, 2626 fid_name, fid->l_fi, &ump->logical_vol->desc_charset); 2627 2628 /* '..' has no name, so provide one */ 2629 if (fid->file_char & UDF_FILE_CHAR_PAR) 2630 strcpy(dirent->d_name, ".."); 2631 2632 dirent->d_fileno = udf_calchash(&fid->icb); /* inode hash XXX */ 2633 dirent->d_namlen = strlen(dirent->d_name); 2634 dirent->d_reclen = _DIRENT_SIZE(dirent); 2635 2636 /* 2637 * Note that its not worth trying to go for the filetypes now... its 2638 * too expensive too 2639 */ 2640 dirent->d_type = DT_UNKNOWN; 2641 2642 /* initial guess for filetype we can make */ 2643 if (fid->file_char & UDF_FILE_CHAR_DIR) 2644 dirent->d_type = DT_DIR; 2645 2646 /* advance */ 2647 *offset += entry_length; 2648 2649 return error; 2650 } 2651 2652 /* --------------------------------------------------------------------- */ 2653 2654 /* 2655 * block based file reading and writing 2656 */ 2657 2658 static int 2659 udf_read_internal(struct udf_node *node, uint8_t *blob) 2660 { 2661 struct udf_mount *ump; 2662 struct file_entry *fe; 2663 struct extfile_entry *efe; 2664 uint64_t inflen; 2665 uint32_t sector_size; 2666 uint8_t *pos; 2667 int icbflags, addr_type; 2668 2669 /* shut up gcc */ 2670 inflen = addr_type = icbflags = 0; 2671 pos = NULL; 2672 2673 /* get extent and do some paranoia checks */ 2674 ump = node->ump; 2675 sector_size = ump->discinfo.sector_size; 2676 2677 fe = node->fe; 2678 efe = node->efe; 2679 if (fe) { 2680 inflen = udf_rw64(fe->inf_len); 2681 pos = &fe->data[0] + udf_rw32(fe->l_ea); 2682 icbflags = udf_rw16(fe->icbtag.flags); 2683 } 2684 if (efe) { 2685 inflen = udf_rw64(efe->inf_len); 2686 pos = &efe->data[0] + udf_rw32(efe->l_ea); 2687 icbflags = udf_rw16(efe->icbtag.flags); 2688 } 2689 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 2690 2691 assert(addr_type == UDF_ICB_INTERN_ALLOC); 2692 assert(inflen < sector_size); 2693 2694 /* copy out info */ 2695 memset(blob, 0, sector_size); 2696 memcpy(blob, pos, inflen); 2697 2698 return 0; 2699 } 2700 2701 /* --------------------------------------------------------------------- */ 2702 2703 /* 2704 * Read file extent reads an extent specified in sectors from the file. It is 2705 * sector based; i.e. no `fancy' offsets. 2706 */ 2707 2708 int 2709 udf_read_file_extent(struct udf_node *node, 2710 uint32_t from, uint32_t sectors, 2711 uint8_t *blob) 2712 { 2713 struct buf buf; 2714 uint32_t sector_size; 2715 2716 BUF_INIT(&buf); 2717 2718 sector_size = node->ump->discinfo.sector_size; 2719 2720 buf.b_bufsize = sectors * sector_size; 2721 buf.b_data = blob; 2722 buf.b_bcount = buf.b_bufsize; 2723 buf.b_resid = buf.b_bcount; 2724 buf.b_flags = B_BUSY | B_READ; 2725 buf.b_vp = node->vnode; 2726 buf.b_proc = NULL; 2727 2728 buf.b_blkno = from; 2729 buf.b_lblkno = 0; 2730 BIO_SETPRIO(&buf, BPRIO_TIMELIMITED); 2731 2732 udf_read_filebuf(node, &buf); 2733 return biowait(&buf); 2734 } 2735 2736 2737 /* --------------------------------------------------------------------- */ 2738 2739 /* 2740 * Read file extent in the buffer. 2741 * 2742 * The splitup of the extent into separate request-buffers is to minimise 2743 * copying around as much as possible. 2744 */ 2745 2746 2747 /* maximum of 128 translations (!) (64 kb in 512 byte sectors) */ 2748 #define FILEBUFSECT 128 2749 2750 void 2751 udf_read_filebuf(struct udf_node *node, struct buf *buf) 2752 { 2753 struct buf *nestbuf; 2754 uint64_t *mapping; 2755 uint64_t run_start; 2756 uint32_t sector_size; 2757 uint32_t buf_offset, sector, rbuflen, rblk; 2758 uint8_t *buf_pos; 2759 int error, run_length; 2760 2761 uint32_t from; 2762 uint32_t sectors; 2763 2764 sector_size = node->ump->discinfo.sector_size; 2765 2766 from = buf->b_blkno; 2767 sectors = buf->b_bcount / sector_size; 2768 2769 /* assure we have enough translation slots */ 2770 KASSERT(buf->b_bcount / sector_size <= FILEBUFSECT); 2771 KASSERT(MAXPHYS / sector_size <= FILEBUFSECT); 2772 2773 if (sectors > FILEBUFSECT) { 2774 printf("udf_read_filebuf: implementation limit on bufsize\n"); 2775 buf->b_error = EIO; 2776 biodone(buf); 2777 return; 2778 } 2779 2780 mapping = malloc(sizeof(*mapping) * FILEBUFSECT, M_TEMP, M_WAITOK); 2781 2782 error = 0; 2783 DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors)); 2784 error = udf_translate_file_extent(node, from, sectors, mapping); 2785 if (error) { 2786 buf->b_error = error; 2787 biodone(buf); 2788 goto out; 2789 } 2790 DPRINTF(READ, ("\ttranslate extent went OK\n")); 2791 2792 /* pre-check if internal or parts are zero */ 2793 if (*mapping == UDF_TRANS_INTERN) { 2794 error = udf_read_internal(node, (uint8_t *) buf->b_data); 2795 if (error) { 2796 buf->b_error = error; 2797 } 2798 biodone(buf); 2799 goto out; 2800 } 2801 DPRINTF(READ, ("\tnot intern\n")); 2802 2803 /* request read-in of data from disc scheduler */ 2804 buf->b_resid = buf->b_bcount; 2805 for (sector = 0; sector < sectors; sector++) { 2806 buf_offset = sector * sector_size; 2807 buf_pos = (uint8_t *) buf->b_data + buf_offset; 2808 DPRINTF(READ, ("\tprocessing rel sector %d\n", sector)); 2809 2810 switch (mapping[sector]) { 2811 case UDF_TRANS_UNMAPPED: 2812 case UDF_TRANS_ZERO: 2813 /* copy zero sector */ 2814 memset(buf_pos, 0, sector_size); 2815 DPRINTF(READ, ("\treturning zero sector\n")); 2816 nestiobuf_done(buf, sector_size, 0); 2817 break; 2818 default : 2819 DPRINTF(READ, ("\tread sector " 2820 "%"PRIu64"\n", mapping[sector])); 2821 2822 run_start = mapping[sector]; 2823 run_length = 1; 2824 while (sector < sectors-1) { 2825 if (mapping[sector+1] != mapping[sector]+1) 2826 break; 2827 run_length++; 2828 sector++; 2829 } 2830 2831 /* 2832 * nest an iobuf and mark it for async reading. Since 2833 * we're using nested buffers, they can't be cached by 2834 * design. 2835 */ 2836 rbuflen = run_length * sector_size; 2837 rblk = run_start * (sector_size/DEV_BSIZE); 2838 2839 nestbuf = getiobuf(); 2840 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen); 2841 /* nestbuf is B_ASYNC */ 2842 2843 /* CD schedules on raw blkno */ 2844 nestbuf->b_blkno = rblk; 2845 nestbuf->b_proc = NULL; 2846 nestbuf->b_cylinder = 0; 2847 nestbuf->b_rawblkno = rblk; 2848 VOP_STRATEGY(node->ump->devvp, nestbuf); 2849 } 2850 } 2851 out: 2852 DPRINTF(READ, ("\tend of read_filebuf\n")); 2853 free(mapping, M_TEMP); 2854 return; 2855 } 2856 #undef FILEBUFSECT 2857 2858 2859 /* --------------------------------------------------------------------- */ 2860 2861 /* 2862 * Translate an extent (in sectors) into sector numbers; used for read and 2863 * write operations. DOESNT't check extents. 2864 */ 2865 2866 int 2867 udf_translate_file_extent(struct udf_node *node, 2868 uint32_t from, uint32_t pages, 2869 uint64_t *map) 2870 { 2871 struct udf_mount *ump; 2872 struct file_entry *fe; 2873 struct extfile_entry *efe; 2874 struct short_ad *s_ad; 2875 struct long_ad *l_ad, t_ad; 2876 uint64_t transsec; 2877 uint32_t sector_size, transsec32; 2878 uint32_t overlap, translen; 2879 uint32_t vpart_num, lb_num, len, alloclen; 2880 uint8_t *pos; 2881 int error, flags, addr_type, icblen, icbflags; 2882 2883 if (!node) 2884 return ENOENT; 2885 2886 /* shut up gcc */ 2887 alloclen = addr_type = icbflags = 0; 2888 pos = NULL; 2889 2890 /* do the work */ 2891 ump = node->ump; 2892 sector_size = ump->discinfo.sector_size; 2893 fe = node->fe; 2894 efe = node->efe; 2895 if (fe) { 2896 alloclen = udf_rw32(fe->l_ad); 2897 pos = &fe->data[0] + udf_rw32(fe->l_ea); 2898 icbflags = udf_rw16(fe->icbtag.flags); 2899 } 2900 if (efe) { 2901 alloclen = udf_rw32(efe->l_ad); 2902 pos = &efe->data[0] + udf_rw32(efe->l_ea); 2903 icbflags = udf_rw16(efe->icbtag.flags); 2904 } 2905 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK; 2906 2907 DPRINTF(TRANSLATE, ("udf trans: alloc_len = %d, addr_type %d, " 2908 "fe %p, efe %p\n", alloclen, addr_type, fe, efe)); 2909 2910 vpart_num = udf_rw16(node->loc.loc.part_num); 2911 lb_num = len = icblen = 0; /* shut up gcc */ 2912 while (pages && alloclen) { 2913 DPRINTF(TRANSLATE, ("\taddr_type %d\n", addr_type)); 2914 switch (addr_type) { 2915 case UDF_ICB_INTERN_ALLOC : 2916 /* TODO check extents? */ 2917 *map = UDF_TRANS_INTERN; 2918 return 0; 2919 case UDF_ICB_SHORT_ALLOC : 2920 icblen = sizeof(struct short_ad); 2921 s_ad = (struct short_ad *) pos; 2922 len = udf_rw32(s_ad->len); 2923 lb_num = udf_rw32(s_ad->lb_num); 2924 break; 2925 case UDF_ICB_LONG_ALLOC : 2926 icblen = sizeof(struct long_ad); 2927 l_ad = (struct long_ad *) pos; 2928 len = udf_rw32(l_ad->len); 2929 lb_num = udf_rw32(l_ad->loc.lb_num); 2930 vpart_num = udf_rw16(l_ad->loc.part_num); 2931 DPRINTFIF(TRANSLATE, 2932 (l_ad->impl.im_used.flags & 2933 UDF_ADIMP_FLAGS_EXTENT_ERASED), 2934 ("UDF: got an `extent erased' flag in long_ad\n")); 2935 break; 2936 default: 2937 /* can't be here */ 2938 return EINVAL; /* for sure */ 2939 } 2940 2941 /* process extent */ 2942 flags = UDF_EXT_FLAGS(len); 2943 len = UDF_EXT_LEN(len); 2944 2945 overlap = (len + sector_size -1) / sector_size; 2946 if (from) { 2947 if (from > overlap) { 2948 from -= overlap; 2949 overlap = 0; 2950 } else { 2951 lb_num += from; /* advance in extent */ 2952 overlap -= from; 2953 from = 0; 2954 } 2955 } 2956 2957 overlap = MIN(overlap, pages); 2958 while (overlap) { 2959 switch (flags) { 2960 case UDF_EXT_REDIRECT : 2961 /* no support for allocation extentions yet */ 2962 /* TODO support for allocation extention */ 2963 return ENOENT; 2964 case UDF_EXT_FREED : 2965 case UDF_EXT_FREE : 2966 transsec = UDF_TRANS_ZERO; 2967 translen = overlap; 2968 while (overlap && pages && translen) { 2969 *map++ = transsec; 2970 lb_num++; 2971 overlap--; pages--; translen--; 2972 } 2973 break; 2974 case UDF_EXT_ALLOCATED : 2975 t_ad.loc.lb_num = udf_rw32(lb_num); 2976 t_ad.loc.part_num = udf_rw16(vpart_num); 2977 error = udf_translate_vtop(ump, 2978 &t_ad, &transsec32, &translen); 2979 transsec = transsec32; 2980 if (error) 2981 return error; 2982 while (overlap && pages && translen) { 2983 *map++ = transsec; 2984 lb_num++; transsec++; 2985 overlap--; pages--; translen--; 2986 } 2987 break; 2988 } 2989 } 2990 pos += icblen; 2991 alloclen -= icblen; 2992 } 2993 return 0; 2994 } 2995 2996 /* --------------------------------------------------------------------- */ 2997 2998