Home | History | Annotate | Line # | Download | only in udf
udf_subr.c revision 1.23.2.5
      1 /* $NetBSD: udf_subr.c,v 1.23.2.5 2007/11/04 01:19:50 xtraeme 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.23.2.5 2007/11/04 01:19:50 xtraeme 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 			vat_node = NULL;
   1313 		}
   1314 		vat_loc--;	/* walk backwards */
   1315 	} while (vat_loc >= early_vat_loc);
   1316 
   1317 	/* we don't need our VAT node anymore */
   1318 	if (vat_node) {
   1319 		vput(vat_node->vnode);
   1320 		udf_dispose_node(vat_node);
   1321 	}
   1322 
   1323 	return error;
   1324 }
   1325 
   1326 /* --------------------------------------------------------------------- */
   1327 
   1328 static int
   1329 udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
   1330 {
   1331 	union dscrptr *dscr;
   1332 	struct part_map_spare *pms = &mapping->pms;
   1333 	uint32_t lb_num;
   1334 	int spar, error;
   1335 
   1336 	/*
   1337 	 * The partition mapping passed on to us specifies the information we
   1338 	 * need to locate and initialise the sparable partition mapping
   1339 	 * information we need.
   1340 	 */
   1341 
   1342 	DPRINTF(VOLUMES, ("Read sparable table\n"));
   1343 	ump->sparable_packet_len = udf_rw16(pms->packet_len);
   1344 	for (spar = 0; spar < pms->n_st; spar++) {
   1345 		lb_num = pms->st_loc[spar];
   1346 		DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
   1347 		error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
   1348 		if (!error && dscr) {
   1349 			if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
   1350 				if (ump->sparing_table)
   1351 					free(ump->sparing_table, M_UDFVOLD);
   1352 				ump->sparing_table = &dscr->spt;
   1353 				dscr = NULL;
   1354 				DPRINTF(VOLUMES,
   1355 				    ("Sparing table accepted (%d entries)\n",
   1356 				     udf_rw16(ump->sparing_table->rt_l)));
   1357 				break;	/* we're done */
   1358 			}
   1359 		}
   1360 		if (dscr)
   1361 			free(dscr, M_UDFVOLD);
   1362 	}
   1363 
   1364 	if (ump->sparing_table)
   1365 		return 0;
   1366 
   1367 	return ENOENT;
   1368 }
   1369 
   1370 /* --------------------------------------------------------------------- */
   1371 
   1372 #define UDF_SET_SYSTEMFILE(vp) \
   1373 	simple_lock(&(vp)->v_interlock);	\
   1374 	(vp)->v_flag |= VSYSTEM;		\
   1375 	simple_unlock(&(vp)->v_interlock);\
   1376 	vref(vp);			\
   1377 	vput(vp);			\
   1378 
   1379 static int
   1380 udf_read_metadata_files(struct udf_mount *ump, union udf_pmap *mapping)
   1381 {
   1382 	struct part_map_meta *pmm = &mapping->pmm;
   1383 	struct long_ad	 icb_loc;
   1384 	struct vnode *vp;
   1385 	int error;
   1386 
   1387 	DPRINTF(VOLUMES, ("Reading in Metadata files\n"));
   1388 	icb_loc.loc.part_num = pmm->part_num;
   1389 	icb_loc.loc.lb_num   = pmm->meta_file_lbn;
   1390 	DPRINTF(VOLUMES, ("Metadata file\n"));
   1391 	error = udf_get_node(ump, &icb_loc, &ump->metadata_file);
   1392 	if (ump->metadata_file) {
   1393 		vp = ump->metadata_file->vnode;
   1394 		UDF_SET_SYSTEMFILE(vp);
   1395 	}
   1396 
   1397 	icb_loc.loc.lb_num   = pmm->meta_mirror_file_lbn;
   1398 	if (icb_loc.loc.lb_num != -1) {
   1399 		DPRINTF(VOLUMES, ("Metadata copy file\n"));
   1400 		error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_file);
   1401 		if (ump->metadatamirror_file) {
   1402 			vp = ump->metadatamirror_file->vnode;
   1403 			UDF_SET_SYSTEMFILE(vp);
   1404 		}
   1405 	}
   1406 
   1407 	icb_loc.loc.lb_num   = pmm->meta_bitmap_file_lbn;
   1408 	if (icb_loc.loc.lb_num != -1) {
   1409 		DPRINTF(VOLUMES, ("Metadata bitmap file\n"));
   1410 		error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_file);
   1411 		if (ump->metadatabitmap_file) {
   1412 			vp = ump->metadatabitmap_file->vnode;
   1413 			UDF_SET_SYSTEMFILE(vp);
   1414 		}
   1415 	}
   1416 
   1417 	/* if we're mounting read-only we relax the requirements */
   1418 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) {
   1419 		error = EFAULT;
   1420 		if (ump->metadata_file)
   1421 			error = 0;
   1422 		if ((ump->metadata_file == NULL) && (ump->metadatamirror_file)) {
   1423 			printf( "udf mount: Metadata file not readable, "
   1424 				"substituting Metadata copy file\n");
   1425 			ump->metadata_file = ump->metadatamirror_file;
   1426 			ump->metadatamirror_file = NULL;
   1427 			error = 0;
   1428 		}
   1429 	} else {
   1430 		/* mounting read/write */
   1431 		DPRINTF(VOLUMES, ("udf mount: read only file system\n"));
   1432 		error = EROFS;
   1433 	}
   1434 	DPRINTFIF(VOLUMES, error, ("udf mount: failed to read "
   1435 				   "metadata files\n"));
   1436 	return error;
   1437 }
   1438 #undef UDF_SET_SYSTEMFILE
   1439 
   1440 /* --------------------------------------------------------------------- */
   1441 
   1442 int
   1443 udf_read_vds_tables(struct udf_mount *ump, struct udf_args *args)
   1444 {
   1445 	union udf_pmap *mapping;
   1446 	uint32_t n_pm, mt_l;
   1447 	uint32_t log_part;
   1448 	uint8_t *pmap_pos;
   1449 	int pmap_size;
   1450 	int error;
   1451 
   1452 	/* We have to iterate again over the part mappings for locations   */
   1453 	n_pm = udf_rw32(ump->logical_vol->n_pm);   /* num partmaps         */
   1454 	mt_l = udf_rw32(ump->logical_vol->mt_l);   /* partmaps data length */
   1455 	pmap_pos =  ump->logical_vol->maps;
   1456 
   1457 	for (log_part = 0; log_part < n_pm; log_part++) {
   1458 		mapping = (union udf_pmap *) pmap_pos;
   1459 		switch (ump->vtop_tp[log_part]) {
   1460 		case UDF_VTOP_TYPE_PHYS :
   1461 			/* nothing */
   1462 			break;
   1463 		case UDF_VTOP_TYPE_VIRT :
   1464 			/* search and load VAT */
   1465 			error = udf_search_vat(ump, mapping);
   1466 			if (error)
   1467 				return ENOENT;
   1468 			break;
   1469 		case UDF_VTOP_TYPE_SPARABLE :
   1470 			/* load one of the sparable tables */
   1471 			error = udf_read_sparables(ump, mapping);
   1472 			if (error)
   1473 				return ENOENT;
   1474 			break;
   1475 		case UDF_VTOP_TYPE_META :
   1476 			/* load the associated file descriptors */
   1477 			error = udf_read_metadata_files(ump, mapping);
   1478 			if (error)
   1479 				return ENOENT;
   1480 			break;
   1481 		default:
   1482 			break;
   1483 		}
   1484 		pmap_size  = pmap_pos[1];
   1485 		pmap_pos  += pmap_size;
   1486 	}
   1487 
   1488 	return 0;
   1489 }
   1490 
   1491 /* --------------------------------------------------------------------- */
   1492 
   1493 int
   1494 udf_read_rootdirs(struct udf_mount *ump, struct udf_args *args)
   1495 {
   1496 	struct udf_node *rootdir_node, *streamdir_node;
   1497 	union dscrptr *dscr;
   1498 	struct long_ad  fsd_loc, *dir_loc;
   1499 	uint32_t lb_num, dummy;
   1500 	uint32_t fsd_len;
   1501 	int dscr_type;
   1502 	int error;
   1503 
   1504 	/* TODO implement FSD reading in seperate function like integrity? */
   1505 	/* get fileset descriptor sequence */
   1506 	fsd_loc = ump->logical_vol->lv_fsd_loc;
   1507 	fsd_len = udf_rw32(fsd_loc.len);
   1508 
   1509 	dscr  = NULL;
   1510 	error = 0;
   1511 	while (fsd_len || error) {
   1512 		DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
   1513 		/* translate fsd_loc to lb_num */
   1514 		error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
   1515 		if (error)
   1516 			break;
   1517 		DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
   1518 		error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
   1519 		/* end markers */
   1520 		if (error || (dscr == NULL))
   1521 			break;
   1522 
   1523 		/* analyse */
   1524 		dscr_type = udf_rw16(dscr->tag.id);
   1525 		if (dscr_type == TAGID_TERM)
   1526 			break;
   1527 		if (dscr_type != TAGID_FSD) {
   1528 			free(dscr, M_UDFVOLD);
   1529 			return ENOENT;
   1530 		}
   1531 
   1532 		/*
   1533 		 * TODO check for multiple fileset descriptors; its only
   1534 		 * picking the last now. Also check for FSD
   1535 		 * correctness/interpretability
   1536 		 */
   1537 
   1538 		/* update */
   1539 		if (ump->fileset_desc) {
   1540 			free(ump->fileset_desc, M_UDFVOLD);
   1541 		}
   1542 		ump->fileset_desc = &dscr->fsd;
   1543 		dscr = NULL;
   1544 
   1545 		/* continue to the next fsd */
   1546 		fsd_len -= ump->discinfo.sector_size;
   1547 		fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
   1548 
   1549 		/* follow up to fsd->next_ex (long_ad) if its not null */
   1550 		if (udf_rw32(ump->fileset_desc->next_ex.len)) {
   1551 			DPRINTF(VOLUMES, ("follow up FSD extent\n"));
   1552 			fsd_loc = ump->fileset_desc->next_ex;
   1553 			fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
   1554 		}
   1555 	}
   1556 	if (dscr)
   1557 		free(dscr, M_UDFVOLD);
   1558 
   1559 	/* there has to be one */
   1560 	if (ump->fileset_desc == NULL)
   1561 		return ENOENT;
   1562 
   1563 	DPRINTF(VOLUMES, ("FSD read in fine\n"));
   1564 
   1565 	/*
   1566 	 * Now the FSD is known, read in the rootdirectory and if one exists,
   1567 	 * the system stream dir. Some files in the system streamdir are not
   1568 	 * wanted in this implementation since they are not maintained. If
   1569 	 * writing is enabled we'll delete these files if they exist.
   1570 	 */
   1571 
   1572 	rootdir_node = streamdir_node = NULL;
   1573 	dir_loc = NULL;
   1574 
   1575 	/* try to read in the rootdir */
   1576 	dir_loc = &ump->fileset_desc->rootdir_icb;
   1577 	error = udf_get_node(ump, dir_loc, &rootdir_node);
   1578 	if (error)
   1579 		return ENOENT;
   1580 
   1581 	/* aparently it read in fine */
   1582 
   1583 	/*
   1584 	 * Try the system stream directory; not very likely in the ones we
   1585 	 * test, but for completeness.
   1586 	 */
   1587 	dir_loc = &ump->fileset_desc->streamdir_icb;
   1588 	if (udf_rw32(dir_loc->len)) {
   1589 		error = udf_get_node(ump, dir_loc, &streamdir_node);
   1590 		if (error)
   1591 			printf("udf mount: streamdir defined but ignored\n");
   1592 		if (!error) {
   1593 			/*
   1594 			 * TODO process streamdir `baddies' i.e. files we dont
   1595 			 * want if R/W
   1596 			 */
   1597 		}
   1598 	}
   1599 
   1600 	DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
   1601 
   1602 	/* release the vnodes again; they'll be auto-recycled later */
   1603 	if (streamdir_node) {
   1604 		vput(streamdir_node->vnode);
   1605 	}
   1606 	if (rootdir_node) {
   1607 		vput(rootdir_node->vnode);
   1608 	}
   1609 
   1610 	return 0;
   1611 }
   1612 
   1613 /* --------------------------------------------------------------------- */
   1614 
   1615 int
   1616 udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
   1617 		   uint32_t *lb_numres, uint32_t *extres)
   1618 {
   1619 	struct part_desc       *pdesc;
   1620 	struct spare_map_entry *sme;
   1621 	struct file_entry      *fe;
   1622 	struct extfile_entry   *efe;
   1623 	struct short_ad        *s_ad;
   1624 	struct long_ad         *l_ad;
   1625 	uint64_t  cur_offset;
   1626 	uint32_t *trans;
   1627 	uint32_t  lb_num, plb_num, lb_rel, lb_packet;
   1628 	uint32_t  sector_size, len, alloclen;
   1629 	uint8_t *pos;
   1630 	int rel, vpart, part, addr_type, icblen, icbflags, flags;
   1631 
   1632 	assert(ump && icb_loc && lb_numres);
   1633 
   1634 	vpart  = udf_rw16(icb_loc->loc.part_num);
   1635 	lb_num = udf_rw32(icb_loc->loc.lb_num);
   1636 	if (vpart < 0 || vpart > UDF_VTOP_RAWPART)
   1637 		return EINVAL;
   1638 
   1639 	part = ump->vtop[vpart];
   1640 	pdesc = ump->partitions[part];
   1641 
   1642 	switch (ump->vtop_tp[vpart]) {
   1643 	case UDF_VTOP_TYPE_RAW :
   1644 		/* 1:1 to the end of the device */
   1645 		*lb_numres = lb_num;
   1646 		*extres = INT_MAX;
   1647 		return 0;
   1648 	case UDF_VTOP_TYPE_PHYS :
   1649 		/* transform into its disc logical block */
   1650 		if (lb_num > udf_rw32(pdesc->part_len))
   1651 			return EINVAL;
   1652 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
   1653 
   1654 		/* extent from here to the end of the partition */
   1655 		*extres = udf_rw32(pdesc->part_len) - lb_num;
   1656 		return 0;
   1657 	case UDF_VTOP_TYPE_VIRT :
   1658 		/* only maps one sector, lookup in VAT */
   1659 		if (lb_num >= ump->vat_entries)		/* XXX > or >= ? */
   1660 			return EINVAL;
   1661 
   1662 		/* lookup in virtual allocation table */
   1663 		trans  = (uint32_t *) (ump->vat_table + ump->vat_offset);
   1664 		lb_num = udf_rw32(trans[lb_num]);
   1665 
   1666 		/* transform into its disc logical block */
   1667 		if (lb_num > udf_rw32(pdesc->part_len))
   1668 			return EINVAL;
   1669 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
   1670 
   1671 		/* just one logical block */
   1672 		*extres = 1;
   1673 		return 0;
   1674 	case UDF_VTOP_TYPE_SPARABLE :
   1675 		/* check if the packet containing the lb_num is remapped */
   1676 		lb_packet = lb_num / ump->sparable_packet_len;
   1677 		lb_rel    = lb_num % ump->sparable_packet_len;
   1678 
   1679 		for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
   1680 			sme = &ump->sparing_table->entries[rel];
   1681 			if (lb_packet == udf_rw32(sme->org)) {
   1682 				/* NOTE maps to absolute disc logical block! */
   1683 				*lb_numres = udf_rw32(sme->map) + lb_rel;
   1684 				*extres    = ump->sparable_packet_len - lb_rel;
   1685 				return 0;
   1686 			}
   1687 		}
   1688 
   1689 		/* transform into its disc logical block */
   1690 		if (lb_num > udf_rw32(pdesc->part_len))
   1691 			return EINVAL;
   1692 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
   1693 
   1694 		/* rest of block */
   1695 		*extres = ump->sparable_packet_len - lb_rel;
   1696 		return 0;
   1697 	case UDF_VTOP_TYPE_META :
   1698 		/* we have to look into the file's allocation descriptors */
   1699 		/* free after udf_translate_file_extent() */
   1700 		/* XXX sector size or lb_size? */
   1701 		sector_size = ump->discinfo.sector_size;
   1702 		/* XXX should we claim exclusive access to the metafile ? */
   1703 		fe  = ump->metadata_file->fe;
   1704 		efe = ump->metadata_file->efe;
   1705 		if (fe) {
   1706 			alloclen = udf_rw32(fe->l_ad);
   1707 			pos      = &fe->data[0] + udf_rw32(fe->l_ea);
   1708 			icbflags = udf_rw16(fe->icbtag.flags);
   1709 		} else {
   1710 			assert(efe);
   1711 			alloclen = udf_rw32(efe->l_ad);
   1712 			pos      = &efe->data[0] + udf_rw32(efe->l_ea);
   1713 			icbflags = udf_rw16(efe->icbtag.flags);
   1714 		}
   1715 		addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   1716 
   1717 		cur_offset = 0;
   1718 		while (alloclen) {
   1719 			if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1720 				icblen = sizeof(struct short_ad);
   1721 				s_ad   = (struct short_ad *) pos;
   1722 				len        = udf_rw32(s_ad->len);
   1723 				plb_num    = udf_rw32(s_ad->lb_num);
   1724 			} else {
   1725 				/* should not be present, but why not */
   1726 				icblen = sizeof(struct long_ad);
   1727 				l_ad   = (struct long_ad *) pos;
   1728 				len        = udf_rw32(l_ad->len);
   1729 				plb_num    = udf_rw32(l_ad->loc.lb_num);
   1730 				/* pvpart_num = udf_rw16(l_ad->loc.part_num); */
   1731 			}
   1732 			/* process extent */
   1733 			flags   = UDF_EXT_FLAGS(len);
   1734 			len     = UDF_EXT_LEN(len);
   1735 
   1736 			if (cur_offset + len > lb_num * sector_size) {
   1737 				if (flags != UDF_EXT_ALLOCATED)
   1738 					return EINVAL;
   1739 				lb_rel = lb_num - cur_offset / sector_size;
   1740 				/* remainder of this extent */
   1741 				*lb_numres = plb_num + lb_rel +
   1742 					udf_rw32(pdesc->start_loc);
   1743 				*extres = (len / sector_size) - lb_rel;
   1744 				return 0;
   1745 			}
   1746 			cur_offset += len;
   1747 			pos        += icblen;
   1748 			alloclen   -= icblen;
   1749 		}
   1750 		/* not found */
   1751 		DPRINTF(TRANSLATE, ("Metadata partition translation failed\n"));
   1752 		return EINVAL;
   1753 	default:
   1754 		printf("UDF vtop translation scheme %d unimplemented yet\n",
   1755 			ump->vtop_tp[vpart]);
   1756 	}
   1757 
   1758 	return EINVAL;
   1759 }
   1760 
   1761 /* --------------------------------------------------------------------- */
   1762 
   1763 /* To make absolutely sure we are NOT returning zero, add one :) */
   1764 
   1765 long
   1766 udf_calchash(struct long_ad *icbptr)
   1767 {
   1768 	/* ought to be enough since each mountpoint has its own chain */
   1769 	return udf_rw32(icbptr->loc.lb_num) + 1;
   1770 }
   1771 
   1772 /* --------------------------------------------------------------------- */
   1773 
   1774 static struct udf_node *
   1775 udf_hashget(struct udf_mount *ump, struct long_ad *icbptr)
   1776 {
   1777 	struct udf_node *unp;
   1778 	struct vnode *vp;
   1779 	uint32_t hashline;
   1780 
   1781 loop:
   1782 	simple_lock(&ump->ihash_slock);
   1783 
   1784 	hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK;
   1785 	LIST_FOREACH(unp, &ump->udf_nodes[hashline], hashchain) {
   1786 		assert(unp);
   1787 		if (unp->loc.loc.lb_num   == icbptr->loc.lb_num &&
   1788 		    unp->loc.loc.part_num == icbptr->loc.part_num) {
   1789 			vp = unp->vnode;
   1790 			assert(vp);
   1791 			simple_lock(&vp->v_interlock);
   1792 			simple_unlock(&ump->ihash_slock);
   1793 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
   1794 				goto loop;
   1795 			return unp;
   1796 		}
   1797 	}
   1798 	simple_unlock(&ump->ihash_slock);
   1799 
   1800 	return NULL;
   1801 }
   1802 
   1803 /* --------------------------------------------------------------------- */
   1804 
   1805 static void
   1806 udf_hashins(struct udf_node *unp)
   1807 {
   1808 	struct udf_mount *ump;
   1809 	uint32_t hashline;
   1810 
   1811 	ump = unp->ump;
   1812 	simple_lock(&ump->ihash_slock);
   1813 
   1814 	hashline = udf_calchash(&unp->loc) & UDF_INODE_HASHMASK;
   1815 	LIST_INSERT_HEAD(&ump->udf_nodes[hashline], unp, hashchain);
   1816 
   1817 	simple_unlock(&ump->ihash_slock);
   1818 }
   1819 
   1820 /* --------------------------------------------------------------------- */
   1821 
   1822 static void
   1823 udf_hashrem(struct udf_node *unp)
   1824 {
   1825 	struct udf_mount *ump;
   1826 
   1827 	ump = unp->ump;
   1828 	simple_lock(&ump->ihash_slock);
   1829 
   1830 	LIST_REMOVE(unp, hashchain);
   1831 
   1832 	simple_unlock(&ump->ihash_slock);
   1833 }
   1834 
   1835 /* --------------------------------------------------------------------- */
   1836 
   1837 int
   1838 udf_dispose_locked_node(struct udf_node *node)
   1839 {
   1840 	if (!node)
   1841 		return 0;
   1842 	if (node->vnode)
   1843 		VOP_UNLOCK(node->vnode, 0);
   1844 	return udf_dispose_node(node);
   1845 }
   1846 
   1847 /* --------------------------------------------------------------------- */
   1848 
   1849 int
   1850 udf_dispose_node(struct udf_node *node)
   1851 {
   1852 	struct vnode *vp;
   1853 
   1854 	DPRINTF(NODE, ("udf_dispose_node called on node %p\n", node));
   1855 	if (!node) {
   1856 		DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
   1857 		return 0;
   1858 	}
   1859 
   1860 	vp  = node->vnode;
   1861 
   1862 	/* TODO extended attributes and streamdir */
   1863 
   1864 	/* remove from our hash lookup table */
   1865 	udf_hashrem(node);
   1866 
   1867 	/* dissociate our udf_node from the vnode */
   1868 	vp->v_data = NULL;
   1869 
   1870 	/* free associated memory and the node itself */
   1871 	if (node->fe)
   1872 		pool_put(node->ump->desc_pool, node->fe);
   1873 	if (node->efe)
   1874 		pool_put(node->ump->desc_pool, node->efe);
   1875 	pool_put(&udf_node_pool, node);
   1876 
   1877 	return 0;
   1878 }
   1879 
   1880 /* --------------------------------------------------------------------- */
   1881 
   1882 /*
   1883  * Genfs interfacing
   1884  *
   1885  * static const struct genfs_ops udffs_genfsops = {
   1886  * 	.gop_size = genfs_size,
   1887  * 		size of transfers
   1888  * 	.gop_alloc = udf_gop_alloc,
   1889  * 		unknown
   1890  * 	.gop_write = genfs_gop_write,
   1891  * 		putpages interface code
   1892  * 	.gop_markupdate = udf_gop_markupdate,
   1893  * 		set update/modify flags etc.
   1894  * }
   1895  */
   1896 
   1897 /*
   1898  * Genfs interface. These four functions are the only ones defined though not
   1899  * documented... great.... why is chosen for the `.' initialisers i dont know
   1900  * but other filingsystems seem to use it this way.
   1901  */
   1902 
   1903 static int
   1904 udf_gop_alloc(struct vnode *vp, off_t off,
   1905     off_t len, int flags, kauth_cred_t cred)
   1906 {
   1907 	return 0;
   1908 }
   1909 
   1910 
   1911 static void
   1912 udf_gop_markupdate(struct vnode *vp, int flags)
   1913 {
   1914 	struct udf_node *udf_node = VTOI(vp);
   1915 	u_long mask;
   1916 
   1917 	udf_node = udf_node;	/* shut up gcc */
   1918 
   1919 	mask = 0;
   1920 #ifdef notyet
   1921 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
   1922 		mask = UDF_SET_ACCESS;
   1923 	}
   1924 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
   1925 		mask |= UDF_SET_UPDATE;
   1926 	}
   1927 	if (mask) {
   1928 		udf_node->update_flag |= mask;
   1929 	}
   1930 #endif
   1931 	/* msdosfs doesn't do it, but shouldn't we update the times here? */
   1932 }
   1933 
   1934 
   1935 static const struct genfs_ops udf_genfsops = {
   1936 	.gop_size = genfs_size,
   1937 	.gop_alloc = udf_gop_alloc,
   1938 	.gop_write = genfs_gop_write,
   1939 	.gop_markupdate = udf_gop_markupdate,
   1940 };
   1941 
   1942 /* --------------------------------------------------------------------- */
   1943 
   1944 /*
   1945  * Each node can have an attached streamdir node though not
   1946  * recursively. These are otherwise known as named substreams/named
   1947  * extended attributes that have no size limitations.
   1948  *
   1949  * `Normal' extended attributes are indicated with a number and are recorded
   1950  * in either the fe/efe descriptor itself for small descriptors or recorded in
   1951  * the attached extended attribute file. Since this file can get fragmented,
   1952  * care ought to be taken.
   1953  */
   1954 
   1955 int
   1956 udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
   1957 	     struct udf_node **noderes)
   1958 {
   1959 	union dscrptr   *dscr, *tmpdscr;
   1960 	struct udf_node *node;
   1961 	struct vnode    *nvp;
   1962 	struct long_ad   icb_loc;
   1963 	extern int (**udf_vnodeop_p)(void *);
   1964 	uint64_t file_size;
   1965 	uint32_t lb_size, sector, dummy;
   1966 	int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
   1967 	int error;
   1968 
   1969 	DPRINTF(NODE, ("udf_get_node called\n"));
   1970 	*noderes = node = NULL;
   1971 
   1972 	/* lock to disallow simultanious creation of same node */
   1973 	lockmgr(&ump->get_node_lock, LK_EXCLUSIVE, NULL);
   1974 
   1975 	DPRINTF(NODE, ("\tlookup in hash table\n"));
   1976 	/* lookup in hash table */
   1977 	assert(ump);
   1978 	assert(node_icb_loc);
   1979 	node = udf_hashget(ump, node_icb_loc);
   1980 	if (node) {
   1981 		DPRINTF(NODE, ("\tgot it from the hash!\n"));
   1982 		/* vnode is returned locked */
   1983 		*noderes = node;
   1984 		lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
   1985 		return 0;
   1986 	}
   1987 
   1988 	/* garbage check: translate node_icb_loc to sectornr */
   1989 	error = udf_translate_vtop(ump, node_icb_loc, &sector, &dummy);
   1990 	if (error) {
   1991 		/* no use, this will fail anyway */
   1992 		lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
   1993 		return EINVAL;
   1994 	}
   1995 
   1996 	/* build node (do initialise!) */
   1997 	node = pool_get(&udf_node_pool, PR_WAITOK);
   1998 	memset(node, 0, sizeof(struct udf_node));
   1999 
   2000 	DPRINTF(NODE, ("\tget new vnode\n"));
   2001 	/* give it a vnode */
   2002 	error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
   2003         if (error) {
   2004 		pool_put(&udf_node_pool, node);
   2005 		lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
   2006 		return error;
   2007 	}
   2008 
   2009 	/* allways return locked vnode */
   2010 	if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
   2011 		/* recycle vnode and unlock; simultanious will fail too */
   2012 		ungetnewvnode(nvp);
   2013 		lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
   2014 		return error;
   2015 	}
   2016 
   2017 	/* initialise crosslinks, note location of fe/efe for hashing */
   2018 	node->ump    =  ump;
   2019 	node->vnode  =  nvp;
   2020 	nvp->v_data  =  node;
   2021 	node->loc    = *node_icb_loc;
   2022 	node->lockf  =  0;
   2023 
   2024 	/* insert into the hash lookup */
   2025 	udf_hashins(node);
   2026 
   2027 	/* safe to unlock, the entry is in the hash table, vnode is locked */
   2028 	lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
   2029 
   2030 	icb_loc = *node_icb_loc;
   2031 	needs_indirect = 0;
   2032 	strat4096 = 0;
   2033 	udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
   2034 	file_size = 0;
   2035 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   2036 
   2037 	do {
   2038 		error = udf_translate_vtop(ump, &icb_loc, &sector, &dummy);
   2039 		if (error)
   2040 			break;
   2041 
   2042 		/* try to read in fe/efe */
   2043 		error = udf_read_descriptor(ump, sector, M_UDFTEMP, &tmpdscr);
   2044 
   2045 		/* blank sector marks end of sequence, check this */
   2046 		if ((tmpdscr == NULL) &&  (!strat4096))
   2047 			error = ENOENT;
   2048 
   2049 		/* break if read error or blank sector */
   2050 		if (error || (tmpdscr == NULL))
   2051 			break;
   2052 
   2053 		/* process descriptor based on the descriptor type */
   2054 		dscr_type = udf_rw16(tmpdscr->tag.id);
   2055 
   2056 		/* if dealing with an indirect entry, follow the link */
   2057 		if (dscr_type == TAGID_INDIRECT_ENTRY) {
   2058 			needs_indirect = 0;
   2059 			icb_loc = tmpdscr->inde.indirect_icb;
   2060 			free(tmpdscr, M_UDFTEMP);
   2061 			continue;
   2062 		}
   2063 
   2064 		/* only file entries and extended file entries allowed here */
   2065 		if ((dscr_type != TAGID_FENTRY) &&
   2066 		    (dscr_type != TAGID_EXTFENTRY)) {
   2067 			free(tmpdscr, M_UDFTEMP);
   2068 			error = ENOENT;
   2069 			break;
   2070 		}
   2071 
   2072 		/* get descriptor space from our pool */
   2073 		KASSERT(udf_tagsize(tmpdscr, lb_size) == lb_size);
   2074 
   2075 		dscr = pool_get(ump->desc_pool, PR_WAITOK);
   2076 		memcpy(dscr, tmpdscr, lb_size);
   2077 		free(tmpdscr, M_UDFTEMP);
   2078 
   2079 		/* record and process/update (ext)fentry */
   2080 		if (dscr_type == TAGID_FENTRY) {
   2081 			if (node->fe)
   2082 				pool_put(ump->desc_pool, node->fe);
   2083 			node->fe  = &dscr->fe;
   2084 			strat = udf_rw16(node->fe->icbtag.strat_type);
   2085 			udf_file_type = node->fe->icbtag.file_type;
   2086 			file_size = udf_rw64(node->fe->inf_len);
   2087 		} else {
   2088 			if (node->efe)
   2089 				pool_put(ump->desc_pool, node->efe);
   2090 			node->efe = &dscr->efe;
   2091 			strat = udf_rw16(node->efe->icbtag.strat_type);
   2092 			udf_file_type = node->efe->icbtag.file_type;
   2093 			file_size = udf_rw64(node->efe->inf_len);
   2094 		}
   2095 
   2096 		/* check recording strategy (structure) */
   2097 
   2098 		/*
   2099 		 * Strategy 4096 is a daisy linked chain terminating with an
   2100 		 * unrecorded sector or a TERM descriptor. The next
   2101 		 * descriptor is to be found in the sector that follows the
   2102 		 * current sector.
   2103 		 */
   2104 		if (strat == 4096) {
   2105 			strat4096 = 1;
   2106 			needs_indirect = 1;
   2107 
   2108 			icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
   2109 		}
   2110 
   2111 		/*
   2112 		 * Strategy 4 is the normal strategy and terminates, but if
   2113 		 * we're in strategy 4096, we can't have strategy 4 mixed in
   2114 		 */
   2115 
   2116 		if (strat == 4) {
   2117 			if (strat4096) {
   2118 				error = EINVAL;
   2119 				break;
   2120 			}
   2121 			break;		/* done */
   2122 		}
   2123 	} while (!error);
   2124 
   2125 	if (error) {
   2126 		/* recycle udf_node */
   2127 		udf_dispose_node(node);
   2128 
   2129 		/* recycle vnode */
   2130 		nvp->v_data = NULL;
   2131 		ungetnewvnode(nvp);
   2132 
   2133 		return EINVAL;		/* error code ok? */
   2134 	}
   2135 
   2136 	/* post process and initialise node */
   2137 
   2138 	/* assert no references to dscr anymore beyong this point */
   2139 	assert((node->fe) || (node->efe));
   2140 	dscr = NULL;
   2141 
   2142 	/*
   2143 	 * Record where to record an updated version of the descriptor. If
   2144 	 * there is a sequence of indirect entries, icb_loc will have been
   2145 	 * updated. Its the write disipline to allocate new space and to make
   2146 	 * sure the chain is maintained.
   2147 	 *
   2148 	 * `needs_indirect' flags if the next location is to be filled with
   2149 	 * with an indirect entry.
   2150 	 */
   2151 	node->next_loc = icb_loc;
   2152 	node->needs_indirect = needs_indirect;
   2153 
   2154 	/*
   2155 	 * Translate UDF filetypes into vnode types.
   2156 	 *
   2157 	 * Systemfiles like the meta main and mirror files are not treated as
   2158 	 * normal files, so we type them as having no type. UDF dictates that
   2159 	 * they are not allowed to be visible.
   2160 	 */
   2161 
   2162 	/* TODO specfs, fifofs etc etc. vnops setting */
   2163 	switch (udf_file_type) {
   2164 	case UDF_ICB_FILETYPE_DIRECTORY :
   2165 	case UDF_ICB_FILETYPE_STREAMDIR :
   2166 		nvp->v_type = VDIR;
   2167 		break;
   2168 	case UDF_ICB_FILETYPE_BLOCKDEVICE :
   2169 		nvp->v_type = VBLK;
   2170 		break;
   2171 	case UDF_ICB_FILETYPE_CHARDEVICE :
   2172 		nvp->v_type = VCHR;
   2173 		break;
   2174 	case UDF_ICB_FILETYPE_SYMLINK :
   2175 		nvp->v_type = VLNK;
   2176 		break;
   2177 	case UDF_ICB_FILETYPE_VAT :
   2178 	case UDF_ICB_FILETYPE_META_MAIN :
   2179 	case UDF_ICB_FILETYPE_META_MIRROR :
   2180 		nvp->v_type = VNON;
   2181 		break;
   2182 	case UDF_ICB_FILETYPE_RANDOMACCESS :
   2183 	case UDF_ICB_FILETYPE_REALTIME :
   2184 		nvp->v_type = VREG;
   2185 		break;
   2186 	default:
   2187 		/* YIKES, either a block/char device, fifo or something else */
   2188 		nvp->v_type = VNON;
   2189 	}
   2190 
   2191 	/* initialise genfs */
   2192 	genfs_node_init(nvp, &udf_genfsops);
   2193 
   2194 	/* don't forget to set vnode's v_size */
   2195 	nvp->v_size = file_size;
   2196 
   2197 	/* TODO ext attr and streamdir nodes */
   2198 
   2199 	*noderes = node;
   2200 
   2201 	return 0;
   2202 }
   2203 
   2204 /* --------------------------------------------------------------------- */
   2205 
   2206 /* UDF<->unix converters */
   2207 
   2208 /* --------------------------------------------------------------------- */
   2209 
   2210 static mode_t
   2211 udf_perm_to_unix_mode(uint32_t perm)
   2212 {
   2213 	mode_t mode;
   2214 
   2215 	mode  = ((perm & UDF_FENTRY_PERM_USER_MASK)      );
   2216 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK  ) >> 2);
   2217 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
   2218 
   2219 	return mode;
   2220 }
   2221 
   2222 /* --------------------------------------------------------------------- */
   2223 
   2224 #ifdef notyet
   2225 static uint32_t
   2226 unix_mode_to_udf_perm(mode_t mode)
   2227 {
   2228 	uint32_t perm;
   2229 
   2230 	perm  = ((mode & S_IRWXO)     );
   2231 	perm |= ((mode & S_IRWXG) << 2);
   2232 	perm |= ((mode & S_IRWXU) << 4);
   2233 	perm |= ((mode & S_IWOTH) << 3);
   2234 	perm |= ((mode & S_IWGRP) << 5);
   2235 	perm |= ((mode & S_IWUSR) << 7);
   2236 
   2237 	return perm;
   2238 }
   2239 #endif
   2240 
   2241 /* --------------------------------------------------------------------- */
   2242 
   2243 static uint32_t
   2244 udf_icb_to_unix_filetype(uint32_t icbftype)
   2245 {
   2246 	switch (icbftype) {
   2247 	case UDF_ICB_FILETYPE_DIRECTORY :
   2248 	case UDF_ICB_FILETYPE_STREAMDIR :
   2249 		return S_IFDIR;
   2250 	case UDF_ICB_FILETYPE_FIFO :
   2251 		return S_IFIFO;
   2252 	case UDF_ICB_FILETYPE_CHARDEVICE :
   2253 		return S_IFCHR;
   2254 	case UDF_ICB_FILETYPE_BLOCKDEVICE :
   2255 		return S_IFBLK;
   2256 	case UDF_ICB_FILETYPE_RANDOMACCESS :
   2257 	case UDF_ICB_FILETYPE_REALTIME :
   2258 		return S_IFREG;
   2259 	case UDF_ICB_FILETYPE_SYMLINK :
   2260 		return S_IFLNK;
   2261 	case UDF_ICB_FILETYPE_SOCKET :
   2262 		return S_IFSOCK;
   2263 	}
   2264 	/* no idea what this is */
   2265 	return 0;
   2266 }
   2267 
   2268 /* --------------------------------------------------------------------- */
   2269 
   2270 /* TODO KNF-ify */
   2271 
   2272 void
   2273 udf_to_unix_name(char *result, char *id, int len, struct charspec *chsp)
   2274 {
   2275 	uint16_t *raw_name, *unix_name;
   2276 	uint16_t *inchp, ch;
   2277 	uint8_t	 *outchp;
   2278 	int       ucode_chars, nice_uchars;
   2279 
   2280 	raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK);
   2281 	unix_name = raw_name + 1024;			/* split space in half */
   2282 	assert(sizeof(char) == sizeof(uint8_t));
   2283 	outchp = (uint8_t *) result;
   2284 	if ((chsp->type == 0) && (strcmp((char*) chsp->inf, "OSTA Compressed Unicode") == 0)) {
   2285 		*raw_name = *unix_name = 0;
   2286 		ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
   2287 		ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
   2288 		nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
   2289 		for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
   2290 			ch = *inchp;
   2291 			/* XXX sloppy unicode -> latin */
   2292 			*outchp++ = ch & 255;
   2293 			if (!ch) break;
   2294 		}
   2295 		*outchp++ = 0;
   2296 	} else {
   2297 		/* assume 8bit char length byte latin-1 */
   2298 		assert(*id == 8);
   2299 		strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
   2300 	}
   2301 	free(raw_name, M_UDFTEMP);
   2302 }
   2303 
   2304 /* --------------------------------------------------------------------- */
   2305 
   2306 /* TODO KNF-ify */
   2307 
   2308 void
   2309 unix_to_udf_name(char *result, char *name,
   2310 		 uint8_t *result_len, struct charspec *chsp)
   2311 {
   2312 	uint16_t *raw_name;
   2313 	int       udf_chars, name_len;
   2314 	char     *inchp;
   2315 	uint16_t *outchp;
   2316 
   2317 	raw_name = malloc(1024, M_UDFTEMP, M_WAITOK);
   2318 	/* convert latin-1 or whatever to unicode-16 */
   2319 	*raw_name = 0;
   2320 	name_len  = 0;
   2321 	inchp  = name;
   2322 	outchp = raw_name;
   2323 	while (*inchp) {
   2324 		*outchp++ = (uint16_t) (*inchp++);
   2325 		name_len++;
   2326 	}
   2327 
   2328 	if ((chsp->type == 0) && (strcmp((char *) chsp->inf, "OSTA Compressed Unicode") == 0)) {
   2329 		udf_chars = udf_CompressUnicode(name_len, 8, (unicode_t *) raw_name, (byte *) result);
   2330 	} else {
   2331 		/* XXX assume 8bit char length byte latin-1 */
   2332 		*result++ = 8; udf_chars = 1;
   2333 		strncpy(result, name + 1, strlen(name+1));
   2334 		udf_chars += strlen(name);
   2335 	}
   2336 	*result_len = udf_chars;
   2337 	free(raw_name, M_UDFTEMP);
   2338 }
   2339 
   2340 /* --------------------------------------------------------------------- */
   2341 
   2342 void
   2343 udf_timestamp_to_timespec(struct udf_mount *ump,
   2344 			  struct timestamp *timestamp,
   2345 			  struct timespec  *timespec)
   2346 {
   2347 	struct clock_ymdhms ymdhms;
   2348 	uint32_t usecs, secs, nsecs;
   2349 	uint16_t tz;
   2350 
   2351 	/* fill in ymdhms structure from timestamp */
   2352 	memset(&ymdhms, 0, sizeof(ymdhms));
   2353 	ymdhms.dt_year = udf_rw16(timestamp->year);
   2354 	ymdhms.dt_mon  = timestamp->month;
   2355 	ymdhms.dt_day  = timestamp->day;
   2356 	ymdhms.dt_wday = 0; /* ? */
   2357 	ymdhms.dt_hour = timestamp->hour;
   2358 	ymdhms.dt_min  = timestamp->minute;
   2359 	ymdhms.dt_sec  = timestamp->second;
   2360 
   2361 	secs = clock_ymdhms_to_secs(&ymdhms);
   2362 	usecs = timestamp->usec +
   2363 		100*timestamp->hund_usec + 10000*timestamp->centisec;
   2364 	nsecs = usecs * 1000;
   2365 
   2366 	/*
   2367 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
   2368 	 * compliment, so we gotta do some extra magic to handle it right.
   2369 	 */
   2370 	tz  = udf_rw16(timestamp->type_tz);
   2371 	tz &= 0x0fff;			/* only lower 12 bits are significant */
   2372 	if (tz & 0x0800)		/* sign extention */
   2373 		tz |= 0xf000;
   2374 
   2375 	/* TODO check timezone conversion */
   2376 	/* check if we are specified a timezone to convert */
   2377 	if (udf_rw16(timestamp->type_tz) & 0x1000) {
   2378 		if ((int16_t) tz != -2047)
   2379 			secs -= (int16_t) tz * 60;
   2380 	} else {
   2381 		secs -= ump->mount_args.gmtoff;
   2382 	}
   2383 
   2384 	timespec->tv_sec  = secs;
   2385 	timespec->tv_nsec = nsecs;
   2386 }
   2387 
   2388 /* --------------------------------------------------------------------- */
   2389 
   2390 /*
   2391  * Attribute and filetypes converters with get/set pairs
   2392  */
   2393 
   2394 uint32_t
   2395 udf_getaccessmode(struct udf_node *udf_node)
   2396 {
   2397 	struct file_entry *fe;
   2398 	struct extfile_entry *efe;
   2399 	uint32_t udf_perm, icbftype;
   2400 	uint32_t mode, ftype;
   2401 	uint16_t icbflags;
   2402 
   2403 	if (udf_node->fe) {
   2404 		fe = udf_node->fe;
   2405 		udf_perm = udf_rw32(fe->perm);
   2406 		icbftype = fe->icbtag.file_type;
   2407 		icbflags = udf_rw16(fe->icbtag.flags);
   2408 	} else {
   2409 		assert(udf_node->efe);
   2410 		efe = udf_node->efe;
   2411 		udf_perm = udf_rw32(efe->perm);
   2412 		icbftype = efe->icbtag.file_type;
   2413 		icbflags = udf_rw16(efe->icbtag.flags);
   2414 	}
   2415 
   2416 	mode  = udf_perm_to_unix_mode(udf_perm);
   2417 	ftype = udf_icb_to_unix_filetype(icbftype);
   2418 
   2419 	/* set suid, sgid, sticky from flags in fe/efe */
   2420 	if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
   2421 		mode |= S_ISUID;
   2422 	if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
   2423 		mode |= S_ISGID;
   2424 	if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
   2425 		mode |= S_ISVTX;
   2426 
   2427 	return mode | ftype;
   2428 }
   2429 
   2430 /* --------------------------------------------------------------------- */
   2431 
   2432 /*
   2433  * Directory read and manipulation functions
   2434  */
   2435 
   2436 int
   2437 udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
   2438 		       struct long_ad *icb_loc)
   2439 {
   2440 	struct udf_node  *dir_node = VTOI(vp);
   2441 	struct file_entry    *fe;
   2442 	struct extfile_entry *efe;
   2443 	struct fileid_desc *fid;
   2444 	struct dirent dirent;
   2445 	uint64_t file_size, diroffset;
   2446 	uint32_t lb_size;
   2447 	int found, error;
   2448 
   2449 	/* get directory filesize */
   2450 	if (dir_node->fe) {
   2451 		fe = dir_node->fe;
   2452 		file_size = udf_rw64(fe->inf_len);
   2453 	} else {
   2454 		assert(dir_node->efe);
   2455 		efe = dir_node->efe;
   2456 		file_size = udf_rw64(efe->inf_len);
   2457 	}
   2458 
   2459 	/* allocate temporary space for fid */
   2460 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
   2461 	fid = malloc(lb_size, M_TEMP, M_WAITOK);
   2462 
   2463 	found = 0;
   2464 	diroffset = dir_node->last_diroffset;
   2465 
   2466 	/*
   2467 	 * if the directory is trunced or if we have never visited it yet,
   2468 	 * start at the end.
   2469 	 */
   2470 	if ((diroffset >= file_size) || (diroffset == 0)) {
   2471 		diroffset = dir_node->last_diroffset = file_size;
   2472 	}
   2473 
   2474 	while (!found) {
   2475 		/* if at the end, go trough zero */
   2476 		if (diroffset >= file_size)
   2477 			diroffset = 0;
   2478 
   2479 		/* transfer a new fid/dirent */
   2480 		error = udf_read_fid_stream(vp, &diroffset, fid, &dirent);
   2481 		if (error)
   2482 			break;
   2483 
   2484 		/* skip deleted entries */
   2485 		if ((fid->file_char & UDF_FILE_CHAR_DEL) == 0) {
   2486 			if ((strlen(dirent.d_name) == namelen) &&
   2487 			    (strncmp(dirent.d_name, name, namelen) == 0)) {
   2488 				found = 1;
   2489 				*icb_loc = fid->icb;
   2490 			}
   2491 		}
   2492 
   2493 		if (diroffset == dir_node->last_diroffset) {
   2494 			/* we have cycled */
   2495 			break;
   2496 		}
   2497 	}
   2498 	free(fid, M_TEMP);
   2499 	dir_node->last_diroffset = diroffset;
   2500 
   2501 	return found;
   2502 }
   2503 
   2504 /* --------------------------------------------------------------------- */
   2505 
   2506 /*
   2507  * Read one fid and process it into a dirent and advance to the next (*fid)
   2508  * has to be allocated a logical block in size, (*dirent) struct dirent length
   2509  */
   2510 
   2511 int
   2512 udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
   2513 		    struct fileid_desc *fid, struct dirent *dirent)
   2514 {
   2515 	struct udf_node  *dir_node = VTOI(vp);
   2516 	struct udf_mount *ump = dir_node->ump;
   2517 	struct file_entry    *fe;
   2518 	struct extfile_entry *efe;
   2519 	struct uio    dir_uio;
   2520 	struct iovec  dir_iovec;
   2521 	uint32_t      entry_length, lb_size;
   2522 	uint64_t      file_size;
   2523 	char         *fid_name;
   2524 	int           enough, error;
   2525 
   2526 	assert(fid);
   2527 	assert(dirent);
   2528 	assert(dir_node);
   2529 	assert(offset);
   2530 	assert(*offset != 1);
   2531 
   2532 	DPRINTF(FIDS, ("read_fid_stream called\n"));
   2533 	/* check if we're past the end of the directory */
   2534 	if (dir_node->fe) {
   2535 		fe = dir_node->fe;
   2536 		file_size = udf_rw64(fe->inf_len);
   2537 	} else {
   2538 		assert(dir_node->efe);
   2539 		efe = dir_node->efe;
   2540 		file_size = udf_rw64(efe->inf_len);
   2541 	}
   2542 	if (*offset >= file_size)
   2543 		return EINVAL;
   2544 
   2545 	/* get maximum length of FID descriptor */
   2546 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   2547 
   2548 	/* initialise return values */
   2549 	entry_length = 0;
   2550 	memset(dirent, 0, sizeof(struct dirent));
   2551 	memset(fid, 0, lb_size);
   2552 
   2553 	/* TODO use vn_rdwr instead of creating our own uio */
   2554 	/* read part of the directory */
   2555 	memset(&dir_uio, 0, sizeof(struct uio));
   2556 	dir_uio.uio_rw     = UIO_READ;	/* read into this space */
   2557 	dir_uio.uio_iovcnt = 1;
   2558 	dir_uio.uio_iov    = &dir_iovec;
   2559 	UIO_SETUP_SYSSPACE(&dir_uio);
   2560 	dir_iovec.iov_base = fid;
   2561 	dir_iovec.iov_len  = lb_size;
   2562 	dir_uio.uio_offset = *offset;
   2563 
   2564 	/* limit length of read in piece */
   2565 	dir_uio.uio_resid  = MIN(file_size - (*offset), lb_size);
   2566 
   2567 	/* read the part into the fid space */
   2568 	error = VOP_READ(vp, &dir_uio, IO_ALTSEMANTICS, NOCRED);
   2569 	if (error)
   2570 		return error;
   2571 
   2572 	/*
   2573 	 * Check if we got a whole descriptor.
   2574 	 * XXX Try to `resync' directory stream when something is very wrong.
   2575 	 *
   2576 	 */
   2577 	enough = (dir_uio.uio_offset - (*offset) >= UDF_FID_SIZE);
   2578 	if (!enough) {
   2579 		/* short dir ... */
   2580 		return EIO;
   2581 	}
   2582 
   2583 	/* check if our FID header is OK */
   2584 	error = udf_check_tag(fid);
   2585 	DPRINTFIF(FIDS, error, ("read fids: tag check failed\n"));
   2586 	if (!error) {
   2587 		if (udf_rw16(fid->tag.id) != TAGID_FID)
   2588 			error = ENOENT;
   2589 	}
   2590 	DPRINTFIF(FIDS, !error, ("\ttag checked ok: got TAGID_FID\n"));
   2591 
   2592 	/* check for length */
   2593 	if (!error) {
   2594 		entry_length = udf_fidsize(fid, lb_size);
   2595 		enough = (dir_uio.uio_offset - (*offset) >= entry_length);
   2596 	}
   2597 	DPRINTFIF(FIDS, !error, ("\tentry_length = %d, enough = %s\n",
   2598 	    entry_length, enough?"yes":"no"));
   2599 
   2600 	if (!enough) {
   2601 		/* short dir ... bomb out */
   2602 		return EIO;
   2603 	}
   2604 
   2605 	/* check FID contents */
   2606 	if (!error) {
   2607 		error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
   2608 		DPRINTF(FIDS, ("\tpayload checked ok\n"));
   2609 	}
   2610 	if (error) {
   2611 		/* note that is sometimes a bit quick to report */
   2612 		printf("BROKEN DIRECTORY ENTRY\n");
   2613 		/* RESYNC? */
   2614 		/* TODO: use udf_resync_fid_stream */
   2615 		return EIO;
   2616 	}
   2617 	DPRINTF(FIDS, ("\tinterpret FID\n"));
   2618 
   2619 	/* we got a whole and valid descriptor! */
   2620 
   2621 	/* create resulting dirent structure */
   2622 	fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
   2623 	udf_to_unix_name(dirent->d_name,
   2624 		fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
   2625 
   2626 	/* '..' has no name, so provide one */
   2627 	if (fid->file_char & UDF_FILE_CHAR_PAR)
   2628 		strcpy(dirent->d_name, "..");
   2629 
   2630 	dirent->d_fileno = udf_calchash(&fid->icb);	/* inode hash XXX */
   2631 	dirent->d_namlen = strlen(dirent->d_name);
   2632 	dirent->d_reclen = _DIRENT_SIZE(dirent);
   2633 
   2634 	/*
   2635 	 * Note that its not worth trying to go for the filetypes now... its
   2636 	 * too expensive too
   2637 	 */
   2638 	dirent->d_type = DT_UNKNOWN;
   2639 
   2640 	/* initial guess for filetype we can make */
   2641 	if (fid->file_char & UDF_FILE_CHAR_DIR)
   2642 		dirent->d_type = DT_DIR;
   2643 
   2644 	/* advance */
   2645 	*offset += entry_length;
   2646 
   2647 	return error;
   2648 }
   2649 
   2650 /* --------------------------------------------------------------------- */
   2651 
   2652 /*
   2653  * block based file reading and writing
   2654  */
   2655 
   2656 static int
   2657 udf_read_internal(struct udf_node *node, uint8_t *blob)
   2658 {
   2659 	struct udf_mount *ump;
   2660 	struct file_entry *fe;
   2661 	struct extfile_entry *efe;
   2662 	uint64_t inflen;
   2663 	uint32_t sector_size;
   2664 	uint8_t  *pos;
   2665 	int icbflags, addr_type;
   2666 
   2667 	/* shut up gcc */
   2668 	inflen = addr_type = icbflags = 0;
   2669 	pos = NULL;
   2670 
   2671 	/* get extent and do some paranoia checks */
   2672 	ump = node->ump;
   2673 	sector_size = ump->discinfo.sector_size;
   2674 
   2675 	fe  = node->fe;
   2676 	efe = node->efe;
   2677 	if (fe) {
   2678 		inflen   = udf_rw64(fe->inf_len);
   2679 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
   2680 		icbflags = udf_rw16(fe->icbtag.flags);
   2681 	}
   2682 	if (efe) {
   2683 		inflen   = udf_rw64(efe->inf_len);
   2684 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
   2685 		icbflags = udf_rw16(efe->icbtag.flags);
   2686 	}
   2687 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   2688 
   2689 	assert(addr_type == UDF_ICB_INTERN_ALLOC);
   2690 	assert(inflen < sector_size);
   2691 
   2692 	/* copy out info */
   2693 	memset(blob, 0, sector_size);
   2694 	memcpy(blob, pos, inflen);
   2695 
   2696 	return 0;
   2697 }
   2698 
   2699 /* --------------------------------------------------------------------- */
   2700 
   2701 /*
   2702  * Read file extent reads an extent specified in sectors from the file. It is
   2703  * sector based; i.e. no `fancy' offsets.
   2704  */
   2705 
   2706 int
   2707 udf_read_file_extent(struct udf_node *node,
   2708 		     uint32_t from, uint32_t sectors,
   2709 		     uint8_t *blob)
   2710 {
   2711 	struct buf buf;
   2712 	uint32_t sector_size;
   2713 
   2714 	BUF_INIT(&buf);
   2715 
   2716 	sector_size = node->ump->discinfo.sector_size;
   2717 
   2718 	buf.b_bufsize = sectors * sector_size;
   2719 	buf.b_data    = blob;
   2720 	buf.b_bcount  = buf.b_bufsize;
   2721 	buf.b_resid   = buf.b_bcount;
   2722 	buf.b_flags   = B_BUSY | B_READ;
   2723 	buf.b_vp      = node->vnode;
   2724 	buf.b_proc    = NULL;
   2725 
   2726 	buf.b_blkno  = from;
   2727 	buf.b_lblkno = 0;
   2728 	BIO_SETPRIO(&buf, BPRIO_TIMELIMITED);
   2729 
   2730 	udf_read_filebuf(node, &buf);
   2731 	return biowait(&buf);
   2732 }
   2733 
   2734 
   2735 /* --------------------------------------------------------------------- */
   2736 
   2737 /*
   2738  * Read file extent in the buffer.
   2739  *
   2740  * The splitup of the extent into seperate request-buffers is to minimise
   2741  * copying around as much as possible.
   2742  */
   2743 
   2744 
   2745 /* maximum of 128 translations (!) (64 kb in 512 byte sectors) */
   2746 #define FILEBUFSECT 128
   2747 
   2748 void
   2749 udf_read_filebuf(struct udf_node *node, struct buf *buf)
   2750 {
   2751 	struct buf *nestbuf;
   2752 	uint64_t   *mapping;
   2753 	uint64_t    run_start;
   2754 	uint32_t    sector_size;
   2755 	uint32_t    buf_offset, sector, rbuflen, rblk;
   2756 	uint8_t    *buf_pos;
   2757 	int error, run_length;
   2758 
   2759 	uint32_t  from;
   2760 	uint32_t  sectors;
   2761 
   2762 	sector_size = node->ump->discinfo.sector_size;
   2763 
   2764 	from    = buf->b_blkno;
   2765 	sectors = buf->b_bcount / sector_size;
   2766 
   2767 	/* assure we have enough translation slots */
   2768 	KASSERT(buf->b_bcount / sector_size <= FILEBUFSECT);
   2769 	KASSERT(MAXPHYS / sector_size <= FILEBUFSECT);
   2770 
   2771 	if (sectors > FILEBUFSECT) {
   2772 		printf("udf_read_filebuf: implementation limit on bufsize\n");
   2773 		buf->b_error  = EIO;
   2774 		buf->b_flags |= B_ERROR;
   2775 		biodone(buf);
   2776 		return;
   2777 	}
   2778 
   2779 	mapping = malloc(sizeof(*mapping) * FILEBUFSECT, M_TEMP, M_WAITOK);
   2780 
   2781 	error = 0;
   2782 	DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
   2783 	error = udf_translate_file_extent(node, from, sectors, mapping);
   2784 	if (error) {
   2785 		buf->b_error  = error;
   2786 		buf->b_flags |= B_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 			buf->b_flags |= B_ERROR;
   2798 		}
   2799 		biodone(buf);
   2800 		goto out;
   2801 	}
   2802 	DPRINTF(READ, ("\tnot intern\n"));
   2803 
   2804 	/* request read-in of data from disc sheduler */
   2805 	buf->b_resid = buf->b_bcount;
   2806 	for (sector = 0; sector < sectors; sector++) {
   2807 		buf_offset = sector * sector_size;
   2808 		buf_pos    = (uint8_t *) buf->b_data + buf_offset;
   2809 		DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
   2810 
   2811 		switch (mapping[sector]) {
   2812 		case UDF_TRANS_UNMAPPED:
   2813 		case UDF_TRANS_ZERO:
   2814 			/* copy zero sector */
   2815 			memset(buf_pos, 0, sector_size);
   2816 			DPRINTF(READ, ("\treturning zero sector\n"));
   2817 			nestiobuf_done(buf, sector_size, 0);
   2818 			break;
   2819 		default :
   2820 			DPRINTF(READ, ("\tread sector "
   2821 			    "%"PRIu64"\n", mapping[sector]));
   2822 
   2823 			run_start  = mapping[sector];
   2824 			run_length = 1;
   2825 			while (sector < sectors-1) {
   2826 				if (mapping[sector+1] != mapping[sector]+1)
   2827 					break;
   2828 				run_length++;
   2829 				sector++;
   2830 			}
   2831 
   2832 			/*
   2833 			 * nest an iobuf and mark it for async reading. Since
   2834 			 * we're using nested buffers, they can't be cached by
   2835 			 * design.
   2836 			 */
   2837 			rbuflen = run_length * sector_size;
   2838 			rblk    = run_start  * (sector_size/DEV_BSIZE);
   2839 
   2840 			nestbuf = getiobuf();
   2841 			nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
   2842 			/* nestbuf is B_ASYNC */
   2843 
   2844 			/* CD shedules on raw blkno */
   2845 			nestbuf->b_blkno    = rblk;
   2846 			nestbuf->b_proc     = NULL;
   2847 			nestbuf->b_cylinder = 0;
   2848 			nestbuf->b_rawblkno = rblk;
   2849 			VOP_STRATEGY(node->ump->devvp, nestbuf);
   2850 		}
   2851 	}
   2852 out:
   2853 	DPRINTF(READ, ("\tend of read_filebuf\n"));
   2854 	free(mapping, M_TEMP);
   2855 	return;
   2856 }
   2857 #undef FILEBUFSECT
   2858 
   2859 
   2860 /* --------------------------------------------------------------------- */
   2861 
   2862 /*
   2863  * Translate an extent (in sectors) into sector numbers; used for read and
   2864  * write operations. DOESNT't check extents.
   2865  */
   2866 
   2867 int
   2868 udf_translate_file_extent(struct udf_node *node,
   2869 		          uint32_t from, uint32_t pages,
   2870 			  uint64_t *map)
   2871 {
   2872 	struct udf_mount *ump;
   2873 	struct file_entry *fe;
   2874 	struct extfile_entry *efe;
   2875 	struct short_ad *s_ad;
   2876 	struct long_ad  *l_ad, t_ad;
   2877 	uint64_t transsec;
   2878 	uint32_t sector_size, transsec32;
   2879 	uint32_t overlap, translen;
   2880 	uint32_t vpart_num, lb_num, len, alloclen;
   2881 	uint8_t *pos;
   2882 	int error, flags, addr_type, icblen, icbflags;
   2883 
   2884 	if (!node)
   2885 		return ENOENT;
   2886 
   2887 	/* shut up gcc */
   2888 	alloclen = addr_type = icbflags = 0;
   2889 	pos = NULL;
   2890 
   2891 	/* do the work */
   2892 	ump = node->ump;
   2893 	sector_size = ump->discinfo.sector_size;
   2894 	fe  = node->fe;
   2895 	efe = node->efe;
   2896 	if (fe) {
   2897 		alloclen = udf_rw32(fe->l_ad);
   2898 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
   2899 		icbflags = udf_rw16(fe->icbtag.flags);
   2900 	}
   2901 	if (efe) {
   2902 		alloclen = udf_rw32(efe->l_ad);
   2903 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
   2904 		icbflags = udf_rw16(efe->icbtag.flags);
   2905 	}
   2906 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   2907 
   2908 	DPRINTF(TRANSLATE, ("udf trans: alloc_len = %d, addr_type %d, "
   2909 	    "fe %p, efe %p\n", alloclen, addr_type, fe, efe));
   2910 
   2911 	vpart_num = udf_rw16(node->loc.loc.part_num);
   2912 	lb_num = len = icblen = 0;	/* shut up gcc */
   2913 	while (pages && alloclen) {
   2914 		DPRINTF(TRANSLATE, ("\taddr_type %d\n", addr_type));
   2915 		switch (addr_type) {
   2916 		case UDF_ICB_INTERN_ALLOC :
   2917 			/* TODO check extents? */
   2918 			*map = UDF_TRANS_INTERN;
   2919 			return 0;
   2920 		case UDF_ICB_SHORT_ALLOC :
   2921 			icblen = sizeof(struct short_ad);
   2922 			s_ad   = (struct short_ad *) pos;
   2923 			len       = udf_rw32(s_ad->len);
   2924 			lb_num    = udf_rw32(s_ad->lb_num);
   2925 			break;
   2926 		case UDF_ICB_LONG_ALLOC  :
   2927 			icblen = sizeof(struct long_ad);
   2928 			l_ad   = (struct long_ad *) pos;
   2929 			len       = udf_rw32(l_ad->len);
   2930 			lb_num    = udf_rw32(l_ad->loc.lb_num);
   2931 			vpart_num = udf_rw16(l_ad->loc.part_num);
   2932 			DPRINTFIF(TRANSLATE,
   2933 			    (l_ad->impl.im_used.flags &
   2934 			     UDF_ADIMP_FLAGS_EXTENT_ERASED),
   2935 			    ("UDF: got an `extent erased' flag in long_ad\n"));
   2936 			break;
   2937 		default:
   2938 			/* can't be here */
   2939 			return EINVAL;	/* for sure */
   2940 		}
   2941 
   2942 		/* process extent */
   2943 		flags   = UDF_EXT_FLAGS(len);
   2944 		len     = UDF_EXT_LEN(len);
   2945 
   2946 		overlap = (len + sector_size -1) / sector_size;
   2947 		if (from) {
   2948 			if (from > overlap) {
   2949 				from -= overlap;
   2950 				overlap = 0;
   2951 			} else {
   2952 				lb_num  += from;	/* advance in extent */
   2953 				overlap -= from;
   2954 				from = 0;
   2955 			}
   2956 		}
   2957 
   2958 		overlap = MIN(overlap, pages);
   2959 		while (overlap) {
   2960 			switch (flags) {
   2961 			case UDF_EXT_REDIRECT :
   2962 				/* no support for allocation extentions yet */
   2963 				/* TODO support for allocation extention */
   2964 				return ENOENT;
   2965 			case UDF_EXT_FREED :
   2966 			case UDF_EXT_FREE :
   2967 				transsec = UDF_TRANS_ZERO;
   2968 				translen = overlap;
   2969 				while (overlap && pages && translen) {
   2970 					*map++ = transsec;
   2971 					lb_num++;
   2972 					overlap--; pages--; translen--;
   2973 				}
   2974 				break;
   2975 			case UDF_EXT_ALLOCATED :
   2976 				t_ad.loc.lb_num   = udf_rw32(lb_num);
   2977 				t_ad.loc.part_num = udf_rw16(vpart_num);
   2978 				error = udf_translate_vtop(ump,
   2979 						&t_ad, &transsec32, &translen);
   2980 				transsec = transsec32;
   2981 				if (error)
   2982 					return error;
   2983 				while (overlap && pages && translen) {
   2984 					*map++ = transsec;
   2985 					lb_num++; transsec++;
   2986 					overlap--; pages--; translen--;
   2987 				}
   2988 				break;
   2989 			}
   2990 		}
   2991 		pos      += icblen;
   2992 		alloclen -= icblen;
   2993 	}
   2994 	return 0;
   2995 }
   2996 
   2997 /* --------------------------------------------------------------------- */
   2998 
   2999