xref: /minix3/sys/fs/udf/udf_subr.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: udf_subr.c,v 1.132 2015/08/24 08:31:56 hannken Exp $ */
29f988b79SJean-Baptiste Boric 
39f988b79SJean-Baptiste Boric /*
49f988b79SJean-Baptiste Boric  * Copyright (c) 2006, 2008 Reinoud Zandijk
59f988b79SJean-Baptiste Boric  * All rights reserved.
69f988b79SJean-Baptiste Boric  *
79f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
89f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
99f988b79SJean-Baptiste Boric  * are met:
109f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
119f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
129f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
139f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
149f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
159f988b79SJean-Baptiste Boric  *
169f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
179f988b79SJean-Baptiste Boric  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
189f988b79SJean-Baptiste Boric  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199f988b79SJean-Baptiste Boric  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
209f988b79SJean-Baptiste Boric  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
219f988b79SJean-Baptiste Boric  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
229f988b79SJean-Baptiste Boric  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239f988b79SJean-Baptiste Boric  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249f988b79SJean-Baptiste Boric  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
259f988b79SJean-Baptiste Boric  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269f988b79SJean-Baptiste Boric  *
279f988b79SJean-Baptiste Boric  */
289f988b79SJean-Baptiste Boric 
299f988b79SJean-Baptiste Boric 
309f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
319f988b79SJean-Baptiste Boric #ifndef lint
32*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: udf_subr.c,v 1.132 2015/08/24 08:31:56 hannken Exp $");
339f988b79SJean-Baptiste Boric #endif /* not lint */
349f988b79SJean-Baptiste Boric 
359f988b79SJean-Baptiste Boric 
369f988b79SJean-Baptiste Boric #if defined(_KERNEL_OPT)
379f988b79SJean-Baptiste Boric #include "opt_compat_netbsd.h"
389f988b79SJean-Baptiste Boric #endif
399f988b79SJean-Baptiste Boric 
409f988b79SJean-Baptiste Boric #include <sys/param.h>
419f988b79SJean-Baptiste Boric #include <sys/systm.h>
429f988b79SJean-Baptiste Boric #include <sys/sysctl.h>
439f988b79SJean-Baptiste Boric #include <sys/namei.h>
449f988b79SJean-Baptiste Boric #include <sys/proc.h>
459f988b79SJean-Baptiste Boric #include <sys/kernel.h>
469f988b79SJean-Baptiste Boric #include <sys/vnode.h>
479f988b79SJean-Baptiste Boric #include <miscfs/genfs/genfs_node.h>
489f988b79SJean-Baptiste Boric #include <sys/mount.h>
499f988b79SJean-Baptiste Boric #include <sys/buf.h>
509f988b79SJean-Baptiste Boric #include <sys/file.h>
519f988b79SJean-Baptiste Boric #include <sys/device.h>
529f988b79SJean-Baptiste Boric #include <sys/disklabel.h>
539f988b79SJean-Baptiste Boric #include <sys/ioctl.h>
549f988b79SJean-Baptiste Boric #include <sys/malloc.h>
559f988b79SJean-Baptiste Boric #include <sys/dirent.h>
569f988b79SJean-Baptiste Boric #include <sys/stat.h>
579f988b79SJean-Baptiste Boric #include <sys/conf.h>
589f988b79SJean-Baptiste Boric #include <sys/kauth.h>
599f988b79SJean-Baptiste Boric #include <fs/unicode.h>
609f988b79SJean-Baptiste Boric #include <dev/clock_subr.h>
619f988b79SJean-Baptiste Boric 
629f988b79SJean-Baptiste Boric #include <fs/udf/ecma167-udf.h>
639f988b79SJean-Baptiste Boric #include <fs/udf/udf_mount.h>
649f988b79SJean-Baptiste Boric #include <sys/dirhash.h>
659f988b79SJean-Baptiste Boric 
669f988b79SJean-Baptiste Boric #include "udf.h"
679f988b79SJean-Baptiste Boric #include "udf_subr.h"
689f988b79SJean-Baptiste Boric #include "udf_bswap.h"
699f988b79SJean-Baptiste Boric 
709f988b79SJean-Baptiste Boric 
719f988b79SJean-Baptiste Boric #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
729f988b79SJean-Baptiste Boric 
739f988b79SJean-Baptiste Boric #define UDF_SET_SYSTEMFILE(vp) \
749f988b79SJean-Baptiste Boric 	/* XXXAD Is the vnode locked? */	\
759f988b79SJean-Baptiste Boric 	(vp)->v_vflag |= VV_SYSTEM;		\
769f988b79SJean-Baptiste Boric 	vref((vp));			\
779f988b79SJean-Baptiste Boric 	vput((vp));			\
789f988b79SJean-Baptiste Boric 
799f988b79SJean-Baptiste Boric extern int syncer_maxdelay;     /* maximum delay time */
809f988b79SJean-Baptiste Boric extern int (**udf_vnodeop_p)(void *);
819f988b79SJean-Baptiste Boric 
829f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
839f988b79SJean-Baptiste Boric 
849f988b79SJean-Baptiste Boric //#ifdef DEBUG
859f988b79SJean-Baptiste Boric #if 1
869f988b79SJean-Baptiste Boric 
879f988b79SJean-Baptiste Boric #if 0
889f988b79SJean-Baptiste Boric static void
899f988b79SJean-Baptiste Boric udf_dumpblob(boid *blob, uint32_t dlen)
909f988b79SJean-Baptiste Boric {
919f988b79SJean-Baptiste Boric 	int i, j;
929f988b79SJean-Baptiste Boric 
939f988b79SJean-Baptiste Boric 	printf("blob = %p\n", blob);
949f988b79SJean-Baptiste Boric 	printf("dump of %d bytes\n", dlen);
959f988b79SJean-Baptiste Boric 
969f988b79SJean-Baptiste Boric 	for (i = 0; i < dlen; i+ = 16) {
979f988b79SJean-Baptiste Boric 		printf("%04x ", i);
989f988b79SJean-Baptiste Boric 		for (j = 0; j < 16; j++) {
999f988b79SJean-Baptiste Boric 			if (i+j < dlen) {
1009f988b79SJean-Baptiste Boric 				printf("%02x ", blob[i+j]);
1019f988b79SJean-Baptiste Boric 			} else {
1029f988b79SJean-Baptiste Boric 				printf("   ");
1039f988b79SJean-Baptiste Boric 			}
1049f988b79SJean-Baptiste Boric 		}
1059f988b79SJean-Baptiste Boric 		for (j = 0; j < 16; j++) {
1069f988b79SJean-Baptiste Boric 			if (i+j < dlen) {
1079f988b79SJean-Baptiste Boric 				if (blob[i+j]>32 && blob[i+j]! = 127) {
1089f988b79SJean-Baptiste Boric 					printf("%c", blob[i+j]);
1099f988b79SJean-Baptiste Boric 				} else {
1109f988b79SJean-Baptiste Boric 					printf(".");
1119f988b79SJean-Baptiste Boric 				}
1129f988b79SJean-Baptiste Boric 			}
1139f988b79SJean-Baptiste Boric 		}
1149f988b79SJean-Baptiste Boric 		printf("\n");
1159f988b79SJean-Baptiste Boric 	}
1169f988b79SJean-Baptiste Boric 	printf("\n");
1179f988b79SJean-Baptiste Boric 	Debugger();
1189f988b79SJean-Baptiste Boric }
1199f988b79SJean-Baptiste Boric #endif
1209f988b79SJean-Baptiste Boric 
1219f988b79SJean-Baptiste Boric static void
udf_dump_discinfo(struct udf_mount * ump)1229f988b79SJean-Baptiste Boric udf_dump_discinfo(struct udf_mount *ump)
1239f988b79SJean-Baptiste Boric {
1249f988b79SJean-Baptiste Boric 	char   bits[128];
1259f988b79SJean-Baptiste Boric 	struct mmc_discinfo *di = &ump->discinfo;
1269f988b79SJean-Baptiste Boric 
1279f988b79SJean-Baptiste Boric 	if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0)
1289f988b79SJean-Baptiste Boric 		return;
1299f988b79SJean-Baptiste Boric 
1309f988b79SJean-Baptiste Boric 	printf("Device/media info  :\n");
1319f988b79SJean-Baptiste Boric 	printf("\tMMC profile        0x%02x\n", di->mmc_profile);
1329f988b79SJean-Baptiste Boric 	printf("\tderived class      %d\n", di->mmc_class);
1339f988b79SJean-Baptiste Boric 	printf("\tsector size        %d\n", di->sector_size);
1349f988b79SJean-Baptiste Boric 	printf("\tdisc state         %d\n", di->disc_state);
1359f988b79SJean-Baptiste Boric 	printf("\tlast ses state     %d\n", di->last_session_state);
1369f988b79SJean-Baptiste Boric 	printf("\tbg format state    %d\n", di->bg_format_state);
1379f988b79SJean-Baptiste Boric 	printf("\tfrst track         %d\n", di->first_track);
1389f988b79SJean-Baptiste Boric 	printf("\tfst on last ses    %d\n", di->first_track_last_session);
1399f988b79SJean-Baptiste Boric 	printf("\tlst on last ses    %d\n", di->last_track_last_session);
1409f988b79SJean-Baptiste Boric 	printf("\tlink block penalty %d\n", di->link_block_penalty);
1419f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS, di->disc_flags);
1429f988b79SJean-Baptiste Boric 	printf("\tdisc flags         %s\n", bits);
1439f988b79SJean-Baptiste Boric 	printf("\tdisc id            %x\n", di->disc_id);
1449f988b79SJean-Baptiste Boric 	printf("\tdisc barcode       %"PRIx64"\n", di->disc_barcode);
1459f988b79SJean-Baptiste Boric 
1469f988b79SJean-Baptiste Boric 	printf("\tnum sessions       %d\n", di->num_sessions);
1479f988b79SJean-Baptiste Boric 	printf("\tnum tracks         %d\n", di->num_tracks);
1489f988b79SJean-Baptiste Boric 
1499f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cur);
1509f988b79SJean-Baptiste Boric 	printf("\tcapabilities cur   %s\n", bits);
1519f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cap);
1529f988b79SJean-Baptiste Boric 	printf("\tcapabilities cap   %s\n", bits);
1539f988b79SJean-Baptiste Boric }
1549f988b79SJean-Baptiste Boric 
1559f988b79SJean-Baptiste Boric static void
udf_dump_trackinfo(struct mmc_trackinfo * trackinfo)1569f988b79SJean-Baptiste Boric udf_dump_trackinfo(struct mmc_trackinfo *trackinfo)
1579f988b79SJean-Baptiste Boric {
1589f988b79SJean-Baptiste Boric 	char   bits[128];
1599f988b79SJean-Baptiste Boric 
1609f988b79SJean-Baptiste Boric 	if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0)
1619f988b79SJean-Baptiste Boric 		return;
1629f988b79SJean-Baptiste Boric 
1639f988b79SJean-Baptiste Boric 	printf("Trackinfo for track %d:\n", trackinfo->tracknr);
1649f988b79SJean-Baptiste Boric 	printf("\tsessionnr           %d\n", trackinfo->sessionnr);
1659f988b79SJean-Baptiste Boric 	printf("\ttrack mode          %d\n", trackinfo->track_mode);
1669f988b79SJean-Baptiste Boric 	printf("\tdata mode           %d\n", trackinfo->data_mode);
1679f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), MMC_TRACKINFO_FLAGBITS, trackinfo->flags);
1689f988b79SJean-Baptiste Boric 	printf("\tflags               %s\n", bits);
1699f988b79SJean-Baptiste Boric 
1709f988b79SJean-Baptiste Boric 	printf("\ttrack start         %d\n", trackinfo->track_start);
1719f988b79SJean-Baptiste Boric 	printf("\tnext_writable       %d\n", trackinfo->next_writable);
1729f988b79SJean-Baptiste Boric 	printf("\tfree_blocks         %d\n", trackinfo->free_blocks);
1739f988b79SJean-Baptiste Boric 	printf("\tpacket_size         %d\n", trackinfo->packet_size);
1749f988b79SJean-Baptiste Boric 	printf("\ttrack size          %d\n", trackinfo->track_size);
1759f988b79SJean-Baptiste Boric 	printf("\tlast recorded block %d\n", trackinfo->last_recorded);
1769f988b79SJean-Baptiste Boric }
1779f988b79SJean-Baptiste Boric 
1789f988b79SJean-Baptiste Boric #else
1799f988b79SJean-Baptiste Boric #define udf_dump_discinfo(a);
1809f988b79SJean-Baptiste Boric #define udf_dump_trackinfo(a);
1819f988b79SJean-Baptiste Boric #endif
1829f988b79SJean-Baptiste Boric 
1839f988b79SJean-Baptiste Boric 
1849f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
1859f988b79SJean-Baptiste Boric 
1869f988b79SJean-Baptiste Boric /* not called often */
1879f988b79SJean-Baptiste Boric int
udf_update_discinfo(struct udf_mount * ump)1889f988b79SJean-Baptiste Boric udf_update_discinfo(struct udf_mount *ump)
1899f988b79SJean-Baptiste Boric {
1909f988b79SJean-Baptiste Boric 	struct vnode *devvp = ump->devvp;
1919f988b79SJean-Baptiste Boric 	uint64_t psize;
1929f988b79SJean-Baptiste Boric 	unsigned secsize;
1939f988b79SJean-Baptiste Boric 	struct mmc_discinfo *di;
1949f988b79SJean-Baptiste Boric 	int error;
1959f988b79SJean-Baptiste Boric 
1969f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("read/update disc info\n"));
1979f988b79SJean-Baptiste Boric 	di = &ump->discinfo;
1989f988b79SJean-Baptiste Boric 	memset(di, 0, sizeof(struct mmc_discinfo));
1999f988b79SJean-Baptiste Boric 
2009f988b79SJean-Baptiste Boric 	/* check if we're on a MMC capable device, i.e. CD/DVD */
2019f988b79SJean-Baptiste Boric 	error = VOP_IOCTL(devvp, MMCGETDISCINFO, di, FKIOCTL, NOCRED);
2029f988b79SJean-Baptiste Boric 	if (error == 0) {
2039f988b79SJean-Baptiste Boric 		udf_dump_discinfo(ump);
2049f988b79SJean-Baptiste Boric 		return 0;
2059f988b79SJean-Baptiste Boric 	}
2069f988b79SJean-Baptiste Boric 
2079f988b79SJean-Baptiste Boric 	/* disc partition support */
2089f988b79SJean-Baptiste Boric 	error = getdisksize(devvp, &psize, &secsize);
2099f988b79SJean-Baptiste Boric 	if (error)
2109f988b79SJean-Baptiste Boric 		return error;
2119f988b79SJean-Baptiste Boric 
2129f988b79SJean-Baptiste Boric 	/* set up a disc info profile for partitions */
2139f988b79SJean-Baptiste Boric 	di->mmc_profile		= 0x01;	/* disc type */
2149f988b79SJean-Baptiste Boric 	di->mmc_class		= MMC_CLASS_DISC;
2159f988b79SJean-Baptiste Boric 	di->disc_state		= MMC_STATE_CLOSED;
2169f988b79SJean-Baptiste Boric 	di->last_session_state	= MMC_STATE_CLOSED;
2179f988b79SJean-Baptiste Boric 	di->bg_format_state	= MMC_BGFSTATE_COMPLETED;
2189f988b79SJean-Baptiste Boric 	di->link_block_penalty	= 0;
2199f988b79SJean-Baptiste Boric 
2209f988b79SJean-Baptiste Boric 	di->mmc_cur     = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE |
2219f988b79SJean-Baptiste Boric 		MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE;
2229f988b79SJean-Baptiste Boric 	di->mmc_cap    = di->mmc_cur;
2239f988b79SJean-Baptiste Boric 	di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
2249f988b79SJean-Baptiste Boric 
2259f988b79SJean-Baptiste Boric 	/* TODO problem with last_possible_lba on resizable VND; request */
2269f988b79SJean-Baptiste Boric 	di->last_possible_lba = psize;
2279f988b79SJean-Baptiste Boric 	di->sector_size       = secsize;
2289f988b79SJean-Baptiste Boric 
2299f988b79SJean-Baptiste Boric 	di->num_sessions = 1;
2309f988b79SJean-Baptiste Boric 	di->num_tracks   = 1;
2319f988b79SJean-Baptiste Boric 
2329f988b79SJean-Baptiste Boric 	di->first_track  = 1;
2339f988b79SJean-Baptiste Boric 	di->first_track_last_session = di->last_track_last_session = 1;
2349f988b79SJean-Baptiste Boric 
2359f988b79SJean-Baptiste Boric 	udf_dump_discinfo(ump);
2369f988b79SJean-Baptiste Boric 	return 0;
2379f988b79SJean-Baptiste Boric }
2389f988b79SJean-Baptiste Boric 
2399f988b79SJean-Baptiste Boric 
2409f988b79SJean-Baptiste Boric int
udf_update_trackinfo(struct udf_mount * ump,struct mmc_trackinfo * ti)2419f988b79SJean-Baptiste Boric udf_update_trackinfo(struct udf_mount *ump, struct mmc_trackinfo *ti)
2429f988b79SJean-Baptiste Boric {
2439f988b79SJean-Baptiste Boric 	struct vnode *devvp = ump->devvp;
2449f988b79SJean-Baptiste Boric 	struct mmc_discinfo *di = &ump->discinfo;
2459f988b79SJean-Baptiste Boric 	int error, class;
2469f988b79SJean-Baptiste Boric 
2479f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("read track info\n"));
2489f988b79SJean-Baptiste Boric 
2499f988b79SJean-Baptiste Boric 	class = di->mmc_class;
2509f988b79SJean-Baptiste Boric 	if (class != MMC_CLASS_DISC) {
2519f988b79SJean-Baptiste Boric 		/* tracknr specified in struct ti */
2529f988b79SJean-Baptiste Boric 		error = VOP_IOCTL(devvp, MMCGETTRACKINFO, ti, FKIOCTL, NOCRED);
2539f988b79SJean-Baptiste Boric 		return error;
2549f988b79SJean-Baptiste Boric 	}
2559f988b79SJean-Baptiste Boric 
2569f988b79SJean-Baptiste Boric 	/* disc partition support */
2579f988b79SJean-Baptiste Boric 	if (ti->tracknr != 1)
2589f988b79SJean-Baptiste Boric 		return EIO;
2599f988b79SJean-Baptiste Boric 
2609f988b79SJean-Baptiste Boric 	/* create fake ti (TODO check for resized vnds) */
2619f988b79SJean-Baptiste Boric 	ti->sessionnr  = 1;
2629f988b79SJean-Baptiste Boric 
2639f988b79SJean-Baptiste Boric 	ti->track_mode = 0;	/* XXX */
2649f988b79SJean-Baptiste Boric 	ti->data_mode  = 0;	/* XXX */
2659f988b79SJean-Baptiste Boric 	ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
2669f988b79SJean-Baptiste Boric 
2679f988b79SJean-Baptiste Boric 	ti->track_start    = 0;
2689f988b79SJean-Baptiste Boric 	ti->packet_size    = 1;
2699f988b79SJean-Baptiste Boric 
2709f988b79SJean-Baptiste Boric 	/* TODO support for resizable vnd */
2719f988b79SJean-Baptiste Boric 	ti->track_size    = di->last_possible_lba;
2729f988b79SJean-Baptiste Boric 	ti->next_writable = di->last_possible_lba;
2739f988b79SJean-Baptiste Boric 	ti->last_recorded = ti->next_writable;
2749f988b79SJean-Baptiste Boric 	ti->free_blocks   = 0;
2759f988b79SJean-Baptiste Boric 
2769f988b79SJean-Baptiste Boric 	return 0;
2779f988b79SJean-Baptiste Boric }
2789f988b79SJean-Baptiste Boric 
2799f988b79SJean-Baptiste Boric 
2809f988b79SJean-Baptiste Boric int
udf_setup_writeparams(struct udf_mount * ump)2819f988b79SJean-Baptiste Boric udf_setup_writeparams(struct udf_mount *ump)
2829f988b79SJean-Baptiste Boric {
2839f988b79SJean-Baptiste Boric 	struct mmc_writeparams mmc_writeparams;
2849f988b79SJean-Baptiste Boric 	int error;
2859f988b79SJean-Baptiste Boric 
2869f988b79SJean-Baptiste Boric 	if (ump->discinfo.mmc_class == MMC_CLASS_DISC)
2879f988b79SJean-Baptiste Boric 		return 0;
2889f988b79SJean-Baptiste Boric 
2899f988b79SJean-Baptiste Boric 	/*
2909f988b79SJean-Baptiste Boric 	 * only CD burning normally needs setting up, but other disc types
2919f988b79SJean-Baptiste Boric 	 * might need other settings to be made. The MMC framework will set up
2929f988b79SJean-Baptiste Boric 	 * the nessisary recording parameters according to the disc
2939f988b79SJean-Baptiste Boric 	 * characteristics read in. Modifications can be made in the discinfo
2949f988b79SJean-Baptiste Boric 	 * structure passed to change the nature of the disc.
2959f988b79SJean-Baptiste Boric 	 */
2969f988b79SJean-Baptiste Boric 
2979f988b79SJean-Baptiste Boric 	memset(&mmc_writeparams, 0, sizeof(struct mmc_writeparams));
2989f988b79SJean-Baptiste Boric 	mmc_writeparams.mmc_class  = ump->discinfo.mmc_class;
2999f988b79SJean-Baptiste Boric 	mmc_writeparams.mmc_cur    = ump->discinfo.mmc_cur;
3009f988b79SJean-Baptiste Boric 
3019f988b79SJean-Baptiste Boric 	/*
3029f988b79SJean-Baptiste Boric 	 * UDF dictates first track to determine track mode for the whole
3039f988b79SJean-Baptiste Boric 	 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1]
3049f988b79SJean-Baptiste Boric 	 * To prevent problems with a `reserved' track in front we start with
3059f988b79SJean-Baptiste Boric 	 * the 2nd track and if that is not valid, go for the 1st.
3069f988b79SJean-Baptiste Boric 	 */
3079f988b79SJean-Baptiste Boric 	mmc_writeparams.tracknr = 2;
3089f988b79SJean-Baptiste Boric 	mmc_writeparams.data_mode  = MMC_DATAMODE_DEFAULT;	/* XA disc */
3099f988b79SJean-Baptiste Boric 	mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT;	/* data */
3109f988b79SJean-Baptiste Boric 
3119f988b79SJean-Baptiste Boric 	error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS, &mmc_writeparams,
3129f988b79SJean-Baptiste Boric 			FKIOCTL, NOCRED);
3139f988b79SJean-Baptiste Boric 	if (error) {
3149f988b79SJean-Baptiste Boric 		mmc_writeparams.tracknr = 1;
3159f988b79SJean-Baptiste Boric 		error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS,
3169f988b79SJean-Baptiste Boric 				&mmc_writeparams, FKIOCTL, NOCRED);
3179f988b79SJean-Baptiste Boric 	}
3189f988b79SJean-Baptiste Boric 	return error;
3199f988b79SJean-Baptiste Boric }
3209f988b79SJean-Baptiste Boric 
3219f988b79SJean-Baptiste Boric 
3229f988b79SJean-Baptiste Boric int
udf_synchronise_caches(struct udf_mount * ump)3239f988b79SJean-Baptiste Boric udf_synchronise_caches(struct udf_mount *ump)
3249f988b79SJean-Baptiste Boric {
3259f988b79SJean-Baptiste Boric 	struct mmc_op mmc_op;
3269f988b79SJean-Baptiste Boric 
3279f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("udf_synchronise_caches()\n"));
3289f988b79SJean-Baptiste Boric 
3299f988b79SJean-Baptiste Boric 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
3309f988b79SJean-Baptiste Boric 		return 0;
3319f988b79SJean-Baptiste Boric 
3329f988b79SJean-Baptiste Boric 	/* discs are done now */
3339f988b79SJean-Baptiste Boric 	if (ump->discinfo.mmc_class == MMC_CLASS_DISC)
3349f988b79SJean-Baptiste Boric 		return 0;
3359f988b79SJean-Baptiste Boric 
3369f988b79SJean-Baptiste Boric 	memset(&mmc_op, 0, sizeof(struct mmc_op));
3379f988b79SJean-Baptiste Boric 	mmc_op.operation = MMC_OP_SYNCHRONISECACHE;
3389f988b79SJean-Baptiste Boric 
3399f988b79SJean-Baptiste Boric 	/* ignore return code */
3409f988b79SJean-Baptiste Boric 	(void) VOP_IOCTL(ump->devvp, MMCOP, &mmc_op, FKIOCTL, NOCRED);
3419f988b79SJean-Baptiste Boric 
3429f988b79SJean-Baptiste Boric 	return 0;
3439f988b79SJean-Baptiste Boric }
3449f988b79SJean-Baptiste Boric 
3459f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
3469f988b79SJean-Baptiste Boric 
3479f988b79SJean-Baptiste Boric /* track/session searching for mounting */
3489f988b79SJean-Baptiste Boric int
udf_search_tracks(struct udf_mount * ump,struct udf_args * args,int * first_tracknr,int * last_tracknr)3499f988b79SJean-Baptiste Boric udf_search_tracks(struct udf_mount *ump, struct udf_args *args,
3509f988b79SJean-Baptiste Boric 		  int *first_tracknr, int *last_tracknr)
3519f988b79SJean-Baptiste Boric {
3529f988b79SJean-Baptiste Boric 	struct mmc_trackinfo trackinfo;
3539f988b79SJean-Baptiste Boric 	uint32_t tracknr, start_track, num_tracks;
3549f988b79SJean-Baptiste Boric 	int error;
3559f988b79SJean-Baptiste Boric 
3569f988b79SJean-Baptiste Boric 	/* if negative, sessionnr is relative to last session */
3579f988b79SJean-Baptiste Boric 	if (args->sessionnr < 0) {
3589f988b79SJean-Baptiste Boric 		args->sessionnr += ump->discinfo.num_sessions;
3599f988b79SJean-Baptiste Boric 	}
3609f988b79SJean-Baptiste Boric 
3619f988b79SJean-Baptiste Boric 	/* sanity */
3629f988b79SJean-Baptiste Boric 	if (args->sessionnr < 0)
3639f988b79SJean-Baptiste Boric 		args->sessionnr = 0;
3649f988b79SJean-Baptiste Boric 	if (args->sessionnr > ump->discinfo.num_sessions)
3659f988b79SJean-Baptiste Boric 		args->sessionnr = ump->discinfo.num_sessions;
3669f988b79SJean-Baptiste Boric 
3679f988b79SJean-Baptiste Boric 	/* search the tracks for this session, zero session nr indicates last */
3689f988b79SJean-Baptiste Boric 	if (args->sessionnr == 0)
3699f988b79SJean-Baptiste Boric 		args->sessionnr = ump->discinfo.num_sessions;
3709f988b79SJean-Baptiste Boric 	if (ump->discinfo.last_session_state == MMC_STATE_EMPTY)
3719f988b79SJean-Baptiste Boric 		args->sessionnr--;
3729f988b79SJean-Baptiste Boric 
3739f988b79SJean-Baptiste Boric 	/* sanity again */
3749f988b79SJean-Baptiste Boric 	if (args->sessionnr < 0)
3759f988b79SJean-Baptiste Boric 		args->sessionnr = 0;
3769f988b79SJean-Baptiste Boric 
3779f988b79SJean-Baptiste Boric 	/* search the first and last track of the specified session */
3789f988b79SJean-Baptiste Boric 	num_tracks  = ump->discinfo.num_tracks;
3799f988b79SJean-Baptiste Boric 	start_track = ump->discinfo.first_track;
3809f988b79SJean-Baptiste Boric 
3819f988b79SJean-Baptiste Boric 	/* search for first track of this session */
3829f988b79SJean-Baptiste Boric 	for (tracknr = start_track; tracknr <= num_tracks; tracknr++) {
3839f988b79SJean-Baptiste Boric 		/* get track info */
3849f988b79SJean-Baptiste Boric 		trackinfo.tracknr = tracknr;
3859f988b79SJean-Baptiste Boric 		error = udf_update_trackinfo(ump, &trackinfo);
3869f988b79SJean-Baptiste Boric 		if (error)
3879f988b79SJean-Baptiste Boric 			return error;
3889f988b79SJean-Baptiste Boric 
3899f988b79SJean-Baptiste Boric 		if (trackinfo.sessionnr == args->sessionnr)
3909f988b79SJean-Baptiste Boric 			break;
3919f988b79SJean-Baptiste Boric 	}
3929f988b79SJean-Baptiste Boric 	*first_tracknr = tracknr;
3939f988b79SJean-Baptiste Boric 
3949f988b79SJean-Baptiste Boric 	/* search for last track of this session */
3959f988b79SJean-Baptiste Boric 	for (;tracknr <= num_tracks; tracknr++) {
3969f988b79SJean-Baptiste Boric 		/* get track info */
3979f988b79SJean-Baptiste Boric 		trackinfo.tracknr = tracknr;
3989f988b79SJean-Baptiste Boric 		error = udf_update_trackinfo(ump, &trackinfo);
3999f988b79SJean-Baptiste Boric 		if (error || (trackinfo.sessionnr != args->sessionnr)) {
4009f988b79SJean-Baptiste Boric 			tracknr--;
4019f988b79SJean-Baptiste Boric 			break;
4029f988b79SJean-Baptiste Boric 		}
4039f988b79SJean-Baptiste Boric 	}
4049f988b79SJean-Baptiste Boric 	if (tracknr > num_tracks)
4059f988b79SJean-Baptiste Boric 		tracknr--;
4069f988b79SJean-Baptiste Boric 
4079f988b79SJean-Baptiste Boric 	*last_tracknr = tracknr;
4089f988b79SJean-Baptiste Boric 
4099f988b79SJean-Baptiste Boric 	if (*last_tracknr < *first_tracknr) {
4109f988b79SJean-Baptiste Boric 		printf( "udf_search_tracks: sanity check on drive+disc failed, "
4119f988b79SJean-Baptiste Boric 			"drive returned garbage\n");
4129f988b79SJean-Baptiste Boric 		return EINVAL;
4139f988b79SJean-Baptiste Boric 	}
4149f988b79SJean-Baptiste Boric 
4159f988b79SJean-Baptiste Boric 	assert(*last_tracknr >= *first_tracknr);
4169f988b79SJean-Baptiste Boric 	return 0;
4179f988b79SJean-Baptiste Boric }
4189f988b79SJean-Baptiste Boric 
4199f988b79SJean-Baptiste Boric 
4209f988b79SJean-Baptiste Boric /*
4219f988b79SJean-Baptiste Boric  * NOTE: this is the only routine in this file that directly peeks into the
4229f988b79SJean-Baptiste Boric  * metadata file but since its at a larval state of the mount it can't hurt.
4239f988b79SJean-Baptiste Boric  *
4249f988b79SJean-Baptiste Boric  * XXX candidate for udf_allocation.c
4259f988b79SJean-Baptiste Boric  * XXX clean me up!, change to new node reading code.
4269f988b79SJean-Baptiste Boric  */
4279f988b79SJean-Baptiste Boric 
4289f988b79SJean-Baptiste Boric static void
udf_check_track_metadata_overlap(struct udf_mount * ump,struct mmc_trackinfo * trackinfo)4299f988b79SJean-Baptiste Boric udf_check_track_metadata_overlap(struct udf_mount *ump,
4309f988b79SJean-Baptiste Boric 	struct mmc_trackinfo *trackinfo)
4319f988b79SJean-Baptiste Boric {
4329f988b79SJean-Baptiste Boric 	struct part_desc *part;
4339f988b79SJean-Baptiste Boric 	struct file_entry      *fe;
4349f988b79SJean-Baptiste Boric 	struct extfile_entry   *efe;
4359f988b79SJean-Baptiste Boric 	struct short_ad        *s_ad;
4369f988b79SJean-Baptiste Boric 	struct long_ad         *l_ad;
4379f988b79SJean-Baptiste Boric 	uint32_t track_start, track_end;
4389f988b79SJean-Baptiste Boric 	uint32_t phys_part_start, phys_part_end, part_start, part_end;
4399f988b79SJean-Baptiste Boric 	uint32_t sector_size, len, alloclen, plb_num;
4409f988b79SJean-Baptiste Boric 	uint8_t *pos;
4419f988b79SJean-Baptiste Boric 	int addr_type, icblen, icbflags;
4429f988b79SJean-Baptiste Boric 
4439f988b79SJean-Baptiste Boric 	/* get our track extents */
4449f988b79SJean-Baptiste Boric 	track_start = trackinfo->track_start;
4459f988b79SJean-Baptiste Boric 	track_end   = track_start + trackinfo->track_size;
4469f988b79SJean-Baptiste Boric 
4479f988b79SJean-Baptiste Boric 	/* get our base partition extent */
4489f988b79SJean-Baptiste Boric 	KASSERT(ump->node_part == ump->fids_part);
4499f988b79SJean-Baptiste Boric 	part = ump->partitions[ump->vtop[ump->node_part]];
4509f988b79SJean-Baptiste Boric 	phys_part_start = udf_rw32(part->start_loc);
4519f988b79SJean-Baptiste Boric 	phys_part_end   = phys_part_start + udf_rw32(part->part_len);
4529f988b79SJean-Baptiste Boric 
4539f988b79SJean-Baptiste Boric 	/* no use if its outside the physical partition */
4549f988b79SJean-Baptiste Boric 	if ((phys_part_start >= track_end) || (phys_part_end < track_start))
4559f988b79SJean-Baptiste Boric 		return;
4569f988b79SJean-Baptiste Boric 
4579f988b79SJean-Baptiste Boric 	/*
4589f988b79SJean-Baptiste Boric 	 * now follow all extents in the fe/efe to see if they refer to this
4599f988b79SJean-Baptiste Boric 	 * track
4609f988b79SJean-Baptiste Boric 	 */
4619f988b79SJean-Baptiste Boric 
4629f988b79SJean-Baptiste Boric 	sector_size = ump->discinfo.sector_size;
4639f988b79SJean-Baptiste Boric 
4649f988b79SJean-Baptiste Boric 	/* XXX should we claim exclusive access to the metafile ? */
4659f988b79SJean-Baptiste Boric 	/* TODO: move to new node read code */
4669f988b79SJean-Baptiste Boric 	fe  = ump->metadata_node->fe;
4679f988b79SJean-Baptiste Boric 	efe = ump->metadata_node->efe;
4689f988b79SJean-Baptiste Boric 	if (fe) {
4699f988b79SJean-Baptiste Boric 		alloclen = udf_rw32(fe->l_ad);
4709f988b79SJean-Baptiste Boric 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
4719f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(fe->icbtag.flags);
4729f988b79SJean-Baptiste Boric 	} else {
4739f988b79SJean-Baptiste Boric 		assert(efe);
4749f988b79SJean-Baptiste Boric 		alloclen = udf_rw32(efe->l_ad);
4759f988b79SJean-Baptiste Boric 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
4769f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(efe->icbtag.flags);
4779f988b79SJean-Baptiste Boric 	}
4789f988b79SJean-Baptiste Boric 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
4799f988b79SJean-Baptiste Boric 
4809f988b79SJean-Baptiste Boric 	while (alloclen) {
4819f988b79SJean-Baptiste Boric 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
4829f988b79SJean-Baptiste Boric 			icblen = sizeof(struct short_ad);
4839f988b79SJean-Baptiste Boric 			s_ad   = (struct short_ad *) pos;
4849f988b79SJean-Baptiste Boric 			len        = udf_rw32(s_ad->len);
4859f988b79SJean-Baptiste Boric 			plb_num    = udf_rw32(s_ad->lb_num);
4869f988b79SJean-Baptiste Boric 		} else {
4879f988b79SJean-Baptiste Boric 			/* should not be present, but why not */
4889f988b79SJean-Baptiste Boric 			icblen = sizeof(struct long_ad);
4899f988b79SJean-Baptiste Boric 			l_ad   = (struct long_ad *) pos;
4909f988b79SJean-Baptiste Boric 			len        = udf_rw32(l_ad->len);
4919f988b79SJean-Baptiste Boric 			plb_num    = udf_rw32(l_ad->loc.lb_num);
4929f988b79SJean-Baptiste Boric 			/* pvpart_num = udf_rw16(l_ad->loc.part_num); */
4939f988b79SJean-Baptiste Boric 		}
4949f988b79SJean-Baptiste Boric 		/* process extent */
4959f988b79SJean-Baptiste Boric 		len     = UDF_EXT_LEN(len);
4969f988b79SJean-Baptiste Boric 
4979f988b79SJean-Baptiste Boric 		part_start = phys_part_start + plb_num;
4989f988b79SJean-Baptiste Boric 		part_end   = part_start + (len / sector_size);
4999f988b79SJean-Baptiste Boric 
5009f988b79SJean-Baptiste Boric 		if ((part_start >= track_start) && (part_end <= track_end)) {
5019f988b79SJean-Baptiste Boric 			/* extent is enclosed within this track */
5029f988b79SJean-Baptiste Boric 			ump->metadata_track = *trackinfo;
5039f988b79SJean-Baptiste Boric 			return;
5049f988b79SJean-Baptiste Boric 		}
5059f988b79SJean-Baptiste Boric 
5069f988b79SJean-Baptiste Boric 		pos        += icblen;
5079f988b79SJean-Baptiste Boric 		alloclen   -= icblen;
5089f988b79SJean-Baptiste Boric 	}
5099f988b79SJean-Baptiste Boric }
5109f988b79SJean-Baptiste Boric 
5119f988b79SJean-Baptiste Boric 
5129f988b79SJean-Baptiste Boric int
udf_search_writing_tracks(struct udf_mount * ump)5139f988b79SJean-Baptiste Boric udf_search_writing_tracks(struct udf_mount *ump)
5149f988b79SJean-Baptiste Boric {
5159f988b79SJean-Baptiste Boric 	struct vnode *devvp = ump->devvp;
5169f988b79SJean-Baptiste Boric 	struct mmc_trackinfo trackinfo;
5179f988b79SJean-Baptiste Boric 	struct mmc_op        mmc_op;
5189f988b79SJean-Baptiste Boric 	struct part_desc *part;
5199f988b79SJean-Baptiste Boric 	uint32_t tracknr, start_track, num_tracks;
5209f988b79SJean-Baptiste Boric 	uint32_t track_start, track_end, part_start, part_end;
5219f988b79SJean-Baptiste Boric 	int node_alloc, error;
5229f988b79SJean-Baptiste Boric 
5239f988b79SJean-Baptiste Boric 	/*
5249f988b79SJean-Baptiste Boric 	 * in the CD/(HD)DVD/BD recordable device model a few tracks within
5259f988b79SJean-Baptiste Boric 	 * the last session might be open but in the UDF device model at most
5269f988b79SJean-Baptiste Boric 	 * three tracks can be open: a reserved track for delayed ISO VRS
5279f988b79SJean-Baptiste Boric 	 * writing, a data track and a metadata track. We search here for the
5289f988b79SJean-Baptiste Boric 	 * data track and the metadata track. Note that the reserved track is
5299f988b79SJean-Baptiste Boric 	 * troublesome but can be detected by its small size of < 512 sectors.
5309f988b79SJean-Baptiste Boric 	 */
5319f988b79SJean-Baptiste Boric 
5329f988b79SJean-Baptiste Boric 	/* update discinfo since it might have changed */
5339f988b79SJean-Baptiste Boric 	error = udf_update_discinfo(ump);
5349f988b79SJean-Baptiste Boric 	if (error)
5359f988b79SJean-Baptiste Boric 		return error;
5369f988b79SJean-Baptiste Boric 
5379f988b79SJean-Baptiste Boric 	num_tracks  = ump->discinfo.num_tracks;
5389f988b79SJean-Baptiste Boric 	start_track = ump->discinfo.first_track;
5399f988b79SJean-Baptiste Boric 
5409f988b79SJean-Baptiste Boric 	/* fetch info on first and possibly only track */
5419f988b79SJean-Baptiste Boric 	trackinfo.tracknr = start_track;
5429f988b79SJean-Baptiste Boric 	error = udf_update_trackinfo(ump, &trackinfo);
5439f988b79SJean-Baptiste Boric 	if (error)
5449f988b79SJean-Baptiste Boric 		return error;
5459f988b79SJean-Baptiste Boric 
5469f988b79SJean-Baptiste Boric 	/* copy results to our mount point */
5479f988b79SJean-Baptiste Boric 	ump->data_track     = trackinfo;
5489f988b79SJean-Baptiste Boric 	ump->metadata_track = trackinfo;
5499f988b79SJean-Baptiste Boric 
5509f988b79SJean-Baptiste Boric 	/* if not sequential, we're done */
5519f988b79SJean-Baptiste Boric 	if (num_tracks == 1)
5529f988b79SJean-Baptiste Boric 		return 0;
5539f988b79SJean-Baptiste Boric 
5549f988b79SJean-Baptiste Boric 	for (tracknr = start_track;tracknr <= num_tracks; tracknr++) {
5559f988b79SJean-Baptiste Boric 		/* get track info */
5569f988b79SJean-Baptiste Boric 		trackinfo.tracknr = tracknr;
5579f988b79SJean-Baptiste Boric 		error = udf_update_trackinfo(ump, &trackinfo);
5589f988b79SJean-Baptiste Boric 		if (error)
5599f988b79SJean-Baptiste Boric 			return error;
5609f988b79SJean-Baptiste Boric 
5619f988b79SJean-Baptiste Boric 		/*
5629f988b79SJean-Baptiste Boric 		 * If this track is marked damaged, ask for repair. This is an
5639f988b79SJean-Baptiste Boric 		 * optional command, so ignore its error but report warning.
5649f988b79SJean-Baptiste Boric 		 */
5659f988b79SJean-Baptiste Boric 		if (trackinfo.flags & MMC_TRACKINFO_DAMAGED) {
5669f988b79SJean-Baptiste Boric 			memset(&mmc_op, 0, sizeof(mmc_op));
5679f988b79SJean-Baptiste Boric 			mmc_op.operation   = MMC_OP_REPAIRTRACK;
5689f988b79SJean-Baptiste Boric 			mmc_op.mmc_profile = ump->discinfo.mmc_profile;
5699f988b79SJean-Baptiste Boric 			mmc_op.tracknr     = tracknr;
5709f988b79SJean-Baptiste Boric 			error = VOP_IOCTL(devvp, MMCOP, &mmc_op, FKIOCTL, NOCRED);
5719f988b79SJean-Baptiste Boric 			if (error)
5729f988b79SJean-Baptiste Boric 				(void)printf("Drive can't explicitly repair "
5739f988b79SJean-Baptiste Boric 					"damaged track %d, but it might "
5749f988b79SJean-Baptiste Boric 					"autorepair\n", tracknr);
5759f988b79SJean-Baptiste Boric 
5769f988b79SJean-Baptiste Boric 			/* reget track info */
5779f988b79SJean-Baptiste Boric 			error = udf_update_trackinfo(ump, &trackinfo);
5789f988b79SJean-Baptiste Boric 			if (error)
5799f988b79SJean-Baptiste Boric 				return error;
5809f988b79SJean-Baptiste Boric 		}
5819f988b79SJean-Baptiste Boric 		if ((trackinfo.flags & MMC_TRACKINFO_NWA_VALID) == 0)
5829f988b79SJean-Baptiste Boric 			continue;
5839f988b79SJean-Baptiste Boric 
5849f988b79SJean-Baptiste Boric 		track_start = trackinfo.track_start;
5859f988b79SJean-Baptiste Boric 		track_end   = track_start + trackinfo.track_size;
5869f988b79SJean-Baptiste Boric 
5879f988b79SJean-Baptiste Boric 		/* check for overlap on data partition */
5889f988b79SJean-Baptiste Boric 		part = ump->partitions[ump->data_part];
5899f988b79SJean-Baptiste Boric 		part_start = udf_rw32(part->start_loc);
5909f988b79SJean-Baptiste Boric 		part_end   = part_start + udf_rw32(part->part_len);
5919f988b79SJean-Baptiste Boric 		if ((part_start < track_end) && (part_end > track_start)) {
5929f988b79SJean-Baptiste Boric 			ump->data_track = trackinfo;
5939f988b79SJean-Baptiste Boric 			/* TODO check if UDF partition data_part is writable */
5949f988b79SJean-Baptiste Boric 		}
5959f988b79SJean-Baptiste Boric 
5969f988b79SJean-Baptiste Boric 		/* check for overlap on metadata partition */
5979f988b79SJean-Baptiste Boric 		node_alloc = ump->vtop_alloc[ump->node_part];
5989f988b79SJean-Baptiste Boric 		if ((node_alloc == UDF_ALLOC_METASEQUENTIAL) ||
5999f988b79SJean-Baptiste Boric 		    (node_alloc == UDF_ALLOC_METABITMAP)) {
6009f988b79SJean-Baptiste Boric 			udf_check_track_metadata_overlap(ump, &trackinfo);
6019f988b79SJean-Baptiste Boric 		} else {
6029f988b79SJean-Baptiste Boric 			ump->metadata_track = trackinfo;
6039f988b79SJean-Baptiste Boric 		}
6049f988b79SJean-Baptiste Boric 	}
6059f988b79SJean-Baptiste Boric 
6069f988b79SJean-Baptiste Boric 	if ((ump->data_track.flags & MMC_TRACKINFO_NWA_VALID) == 0)
6079f988b79SJean-Baptiste Boric 		return EROFS;
6089f988b79SJean-Baptiste Boric 
6099f988b79SJean-Baptiste Boric 	if ((ump->metadata_track.flags & MMC_TRACKINFO_NWA_VALID) == 0)
6109f988b79SJean-Baptiste Boric 		return EROFS;
6119f988b79SJean-Baptiste Boric 
6129f988b79SJean-Baptiste Boric 	return 0;
6139f988b79SJean-Baptiste Boric }
6149f988b79SJean-Baptiste Boric 
6159f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
6169f988b79SJean-Baptiste Boric 
6179f988b79SJean-Baptiste Boric /*
6189f988b79SJean-Baptiste Boric  * Check if the blob starts with a good UDF tag. Tags are protected by a
6199f988b79SJean-Baptiste Boric  * checksum over the reader except one byte at position 4 that is the checksum
6209f988b79SJean-Baptiste Boric  * itself.
6219f988b79SJean-Baptiste Boric  */
6229f988b79SJean-Baptiste Boric 
6239f988b79SJean-Baptiste Boric int
udf_check_tag(void * blob)6249f988b79SJean-Baptiste Boric udf_check_tag(void *blob)
6259f988b79SJean-Baptiste Boric {
6269f988b79SJean-Baptiste Boric 	struct desc_tag *tag = blob;
6279f988b79SJean-Baptiste Boric 	uint8_t *pos, sum, cnt;
6289f988b79SJean-Baptiste Boric 
6299f988b79SJean-Baptiste Boric 	/* check TAG header checksum */
6309f988b79SJean-Baptiste Boric 	pos = (uint8_t *) tag;
6319f988b79SJean-Baptiste Boric 	sum = 0;
6329f988b79SJean-Baptiste Boric 
6339f988b79SJean-Baptiste Boric 	for(cnt = 0; cnt < 16; cnt++) {
6349f988b79SJean-Baptiste Boric 		if (cnt != 4)
6359f988b79SJean-Baptiste Boric 			sum += *pos;
6369f988b79SJean-Baptiste Boric 		pos++;
6379f988b79SJean-Baptiste Boric 	}
6389f988b79SJean-Baptiste Boric 	if (sum != tag->cksum) {
6399f988b79SJean-Baptiste Boric 		/* bad tag header checksum; this is not a valid tag */
6409f988b79SJean-Baptiste Boric 		return EINVAL;
6419f988b79SJean-Baptiste Boric 	}
6429f988b79SJean-Baptiste Boric 
6439f988b79SJean-Baptiste Boric 	return 0;
6449f988b79SJean-Baptiste Boric }
6459f988b79SJean-Baptiste Boric 
6469f988b79SJean-Baptiste Boric 
6479f988b79SJean-Baptiste Boric /*
6489f988b79SJean-Baptiste Boric  * check tag payload will check descriptor CRC as specified.
6499f988b79SJean-Baptiste Boric  * If the descriptor is too long, it will return EIO otherwise EINVAL.
6509f988b79SJean-Baptiste Boric  */
6519f988b79SJean-Baptiste Boric 
6529f988b79SJean-Baptiste Boric int
udf_check_tag_payload(void * blob,uint32_t max_length)6539f988b79SJean-Baptiste Boric udf_check_tag_payload(void *blob, uint32_t max_length)
6549f988b79SJean-Baptiste Boric {
6559f988b79SJean-Baptiste Boric 	struct desc_tag *tag = blob;
6569f988b79SJean-Baptiste Boric 	uint16_t crc, crc_len;
6579f988b79SJean-Baptiste Boric 
6589f988b79SJean-Baptiste Boric 	crc_len = udf_rw16(tag->desc_crc_len);
6599f988b79SJean-Baptiste Boric 
6609f988b79SJean-Baptiste Boric 	/* check payload CRC if applicable */
6619f988b79SJean-Baptiste Boric 	if (crc_len == 0)
6629f988b79SJean-Baptiste Boric 		return 0;
6639f988b79SJean-Baptiste Boric 
6649f988b79SJean-Baptiste Boric 	if (crc_len > max_length)
6659f988b79SJean-Baptiste Boric 		return EIO;
6669f988b79SJean-Baptiste Boric 
6679f988b79SJean-Baptiste Boric 	crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len);
6689f988b79SJean-Baptiste Boric 	if (crc != udf_rw16(tag->desc_crc)) {
6699f988b79SJean-Baptiste Boric 		/* bad payload CRC; this is a broken tag */
6709f988b79SJean-Baptiste Boric 		return EINVAL;
6719f988b79SJean-Baptiste Boric 	}
6729f988b79SJean-Baptiste Boric 
6739f988b79SJean-Baptiste Boric 	return 0;
6749f988b79SJean-Baptiste Boric }
6759f988b79SJean-Baptiste Boric 
6769f988b79SJean-Baptiste Boric 
6779f988b79SJean-Baptiste Boric void
udf_validate_tag_sum(void * blob)6789f988b79SJean-Baptiste Boric udf_validate_tag_sum(void *blob)
6799f988b79SJean-Baptiste Boric {
6809f988b79SJean-Baptiste Boric 	struct desc_tag *tag = blob;
6819f988b79SJean-Baptiste Boric 	uint8_t *pos, sum, cnt;
6829f988b79SJean-Baptiste Boric 
6839f988b79SJean-Baptiste Boric 	/* calculate TAG header checksum */
6849f988b79SJean-Baptiste Boric 	pos = (uint8_t *) tag;
6859f988b79SJean-Baptiste Boric 	sum = 0;
6869f988b79SJean-Baptiste Boric 
6879f988b79SJean-Baptiste Boric 	for(cnt = 0; cnt < 16; cnt++) {
6889f988b79SJean-Baptiste Boric 		if (cnt != 4) sum += *pos;
6899f988b79SJean-Baptiste Boric 		pos++;
6909f988b79SJean-Baptiste Boric 	}
6919f988b79SJean-Baptiste Boric 	tag->cksum = sum;	/* 8 bit */
6929f988b79SJean-Baptiste Boric }
6939f988b79SJean-Baptiste Boric 
6949f988b79SJean-Baptiste Boric 
6959f988b79SJean-Baptiste Boric /* assumes sector number of descriptor to be saved already present */
6969f988b79SJean-Baptiste Boric void
udf_validate_tag_and_crc_sums(void * blob)6979f988b79SJean-Baptiste Boric udf_validate_tag_and_crc_sums(void *blob)
6989f988b79SJean-Baptiste Boric {
6999f988b79SJean-Baptiste Boric 	struct desc_tag *tag  = blob;
7009f988b79SJean-Baptiste Boric 	uint8_t         *btag = (uint8_t *) tag;
7019f988b79SJean-Baptiste Boric 	uint16_t crc, crc_len;
7029f988b79SJean-Baptiste Boric 
7039f988b79SJean-Baptiste Boric 	crc_len = udf_rw16(tag->desc_crc_len);
7049f988b79SJean-Baptiste Boric 
7059f988b79SJean-Baptiste Boric 	/* check payload CRC if applicable */
7069f988b79SJean-Baptiste Boric 	if (crc_len > 0) {
7079f988b79SJean-Baptiste Boric 		crc = udf_cksum(btag + UDF_DESC_TAG_LENGTH, crc_len);
7089f988b79SJean-Baptiste Boric 		tag->desc_crc = udf_rw16(crc);
7099f988b79SJean-Baptiste Boric 	}
7109f988b79SJean-Baptiste Boric 
7119f988b79SJean-Baptiste Boric 	/* calculate TAG header checksum */
7129f988b79SJean-Baptiste Boric 	udf_validate_tag_sum(blob);
7139f988b79SJean-Baptiste Boric }
7149f988b79SJean-Baptiste Boric 
7159f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
7169f988b79SJean-Baptiste Boric 
7179f988b79SJean-Baptiste Boric /*
7189f988b79SJean-Baptiste Boric  * XXX note the different semantics from udfclient: for FIDs it still rounds
7199f988b79SJean-Baptiste Boric  * up to sectors. Use udf_fidsize() for a correct length.
7209f988b79SJean-Baptiste Boric  */
7219f988b79SJean-Baptiste Boric 
7229f988b79SJean-Baptiste Boric int
udf_tagsize(union dscrptr * dscr,uint32_t lb_size)7239f988b79SJean-Baptiste Boric udf_tagsize(union dscrptr *dscr, uint32_t lb_size)
7249f988b79SJean-Baptiste Boric {
7259f988b79SJean-Baptiste Boric 	uint32_t size, tag_id, num_lb, elmsz;
7269f988b79SJean-Baptiste Boric 
7279f988b79SJean-Baptiste Boric 	tag_id = udf_rw16(dscr->tag.id);
7289f988b79SJean-Baptiste Boric 
7299f988b79SJean-Baptiste Boric 	switch (tag_id) {
7309f988b79SJean-Baptiste Boric 	case TAGID_LOGVOL :
7319f988b79SJean-Baptiste Boric 		size  = sizeof(struct logvol_desc) - 1;
7329f988b79SJean-Baptiste Boric 		size += udf_rw32(dscr->lvd.mt_l);
7339f988b79SJean-Baptiste Boric 		break;
7349f988b79SJean-Baptiste Boric 	case TAGID_UNALLOC_SPACE :
7359f988b79SJean-Baptiste Boric 		elmsz = sizeof(struct extent_ad);
7369f988b79SJean-Baptiste Boric 		size  = sizeof(struct unalloc_sp_desc) - elmsz;
7379f988b79SJean-Baptiste Boric 		size += udf_rw32(dscr->usd.alloc_desc_num) * elmsz;
7389f988b79SJean-Baptiste Boric 		break;
7399f988b79SJean-Baptiste Boric 	case TAGID_FID :
7409f988b79SJean-Baptiste Boric 		size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu);
7419f988b79SJean-Baptiste Boric 		size = (size + 3) & ~3;
7429f988b79SJean-Baptiste Boric 		break;
7439f988b79SJean-Baptiste Boric 	case TAGID_LOGVOL_INTEGRITY :
7449f988b79SJean-Baptiste Boric 		size  = sizeof(struct logvol_int_desc) - sizeof(uint32_t);
7459f988b79SJean-Baptiste Boric 		size += udf_rw32(dscr->lvid.l_iu);
7469f988b79SJean-Baptiste Boric 		size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t));
7479f988b79SJean-Baptiste Boric 		break;
7489f988b79SJean-Baptiste Boric 	case TAGID_SPACE_BITMAP :
7499f988b79SJean-Baptiste Boric 		size  = sizeof(struct space_bitmap_desc) - 1;
7509f988b79SJean-Baptiste Boric 		size += udf_rw32(dscr->sbd.num_bytes);
7519f988b79SJean-Baptiste Boric 		break;
7529f988b79SJean-Baptiste Boric 	case TAGID_SPARING_TABLE :
7539f988b79SJean-Baptiste Boric 		elmsz = sizeof(struct spare_map_entry);
7549f988b79SJean-Baptiste Boric 		size  = sizeof(struct udf_sparing_table) - elmsz;
7559f988b79SJean-Baptiste Boric 		size += udf_rw16(dscr->spt.rt_l) * elmsz;
7569f988b79SJean-Baptiste Boric 		break;
7579f988b79SJean-Baptiste Boric 	case TAGID_FENTRY :
7589f988b79SJean-Baptiste Boric 		size  = sizeof(struct file_entry);
7599f988b79SJean-Baptiste Boric 		size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1;
7609f988b79SJean-Baptiste Boric 		break;
7619f988b79SJean-Baptiste Boric 	case TAGID_EXTFENTRY :
7629f988b79SJean-Baptiste Boric 		size  = sizeof(struct extfile_entry);
7639f988b79SJean-Baptiste Boric 		size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1;
7649f988b79SJean-Baptiste Boric 		break;
7659f988b79SJean-Baptiste Boric 	case TAGID_FSD :
7669f988b79SJean-Baptiste Boric 		size  = sizeof(struct fileset_desc);
7679f988b79SJean-Baptiste Boric 		break;
7689f988b79SJean-Baptiste Boric 	default :
7699f988b79SJean-Baptiste Boric 		size = sizeof(union dscrptr);
7709f988b79SJean-Baptiste Boric 		break;
7719f988b79SJean-Baptiste Boric 	}
7729f988b79SJean-Baptiste Boric 
7739f988b79SJean-Baptiste Boric 	if ((size == 0) || (lb_size == 0))
7749f988b79SJean-Baptiste Boric 		return 0;
7759f988b79SJean-Baptiste Boric 
7769f988b79SJean-Baptiste Boric 	if (lb_size == 1)
7779f988b79SJean-Baptiste Boric 		return size;
7789f988b79SJean-Baptiste Boric 
7799f988b79SJean-Baptiste Boric 	/* round up in sectors */
7809f988b79SJean-Baptiste Boric 	num_lb = (size + lb_size -1) / lb_size;
7819f988b79SJean-Baptiste Boric 	return num_lb * lb_size;
7829f988b79SJean-Baptiste Boric }
7839f988b79SJean-Baptiste Boric 
7849f988b79SJean-Baptiste Boric 
7859f988b79SJean-Baptiste Boric int
udf_fidsize(struct fileid_desc * fid)7869f988b79SJean-Baptiste Boric udf_fidsize(struct fileid_desc *fid)
7879f988b79SJean-Baptiste Boric {
7889f988b79SJean-Baptiste Boric 	uint32_t size;
7899f988b79SJean-Baptiste Boric 
7909f988b79SJean-Baptiste Boric 	if (udf_rw16(fid->tag.id) != TAGID_FID)
7919f988b79SJean-Baptiste Boric 		panic("got udf_fidsize on non FID\n");
7929f988b79SJean-Baptiste Boric 
7939f988b79SJean-Baptiste Boric 	size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu);
7949f988b79SJean-Baptiste Boric 	size = (size + 3) & ~3;
7959f988b79SJean-Baptiste Boric 
7969f988b79SJean-Baptiste Boric 	return size;
7979f988b79SJean-Baptiste Boric }
7989f988b79SJean-Baptiste Boric 
7999f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
8009f988b79SJean-Baptiste Boric 
8019f988b79SJean-Baptiste Boric void
udf_lock_node(struct udf_node * udf_node,int flag,char const * fname,const int lineno)8029f988b79SJean-Baptiste Boric udf_lock_node(struct udf_node *udf_node, int flag, char const *fname, const int lineno)
8039f988b79SJean-Baptiste Boric {
8049f988b79SJean-Baptiste Boric 	int ret;
8059f988b79SJean-Baptiste Boric 
8069f988b79SJean-Baptiste Boric 	mutex_enter(&udf_node->node_mutex);
8079f988b79SJean-Baptiste Boric 	/* wait until free */
8089f988b79SJean-Baptiste Boric 	while (udf_node->i_flags & IN_LOCKED) {
8099f988b79SJean-Baptiste Boric 		ret = cv_timedwait(&udf_node->node_lock, &udf_node->node_mutex, hz/8);
8109f988b79SJean-Baptiste Boric 		/* TODO check if we should return error; abort */
8119f988b79SJean-Baptiste Boric 		if (ret == EWOULDBLOCK) {
8129f988b79SJean-Baptiste Boric 			DPRINTF(LOCKING, ( "udf_lock_node: udf_node %p would block "
8139f988b79SJean-Baptiste Boric 				"wanted at %s:%d, previously locked at %s:%d\n",
8149f988b79SJean-Baptiste Boric 				udf_node, fname, lineno,
8159f988b79SJean-Baptiste Boric 				udf_node->lock_fname, udf_node->lock_lineno));
8169f988b79SJean-Baptiste Boric 		}
8179f988b79SJean-Baptiste Boric 	}
8189f988b79SJean-Baptiste Boric 	/* grab */
8199f988b79SJean-Baptiste Boric 	udf_node->i_flags |= IN_LOCKED | flag;
8209f988b79SJean-Baptiste Boric 	/* debug */
8219f988b79SJean-Baptiste Boric 	udf_node->lock_fname  = fname;
8229f988b79SJean-Baptiste Boric 	udf_node->lock_lineno = lineno;
8239f988b79SJean-Baptiste Boric 
8249f988b79SJean-Baptiste Boric 	mutex_exit(&udf_node->node_mutex);
8259f988b79SJean-Baptiste Boric }
8269f988b79SJean-Baptiste Boric 
8279f988b79SJean-Baptiste Boric 
8289f988b79SJean-Baptiste Boric void
udf_unlock_node(struct udf_node * udf_node,int flag)8299f988b79SJean-Baptiste Boric udf_unlock_node(struct udf_node *udf_node, int flag)
8309f988b79SJean-Baptiste Boric {
8319f988b79SJean-Baptiste Boric 	mutex_enter(&udf_node->node_mutex);
8329f988b79SJean-Baptiste Boric 	udf_node->i_flags &= ~(IN_LOCKED | flag);
8339f988b79SJean-Baptiste Boric 	cv_broadcast(&udf_node->node_lock);
8349f988b79SJean-Baptiste Boric 	mutex_exit(&udf_node->node_mutex);
8359f988b79SJean-Baptiste Boric }
8369f988b79SJean-Baptiste Boric 
8379f988b79SJean-Baptiste Boric 
8389f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
8399f988b79SJean-Baptiste Boric 
8409f988b79SJean-Baptiste Boric static int
udf_read_anchor(struct udf_mount * ump,uint32_t sector,struct anchor_vdp ** dst)8419f988b79SJean-Baptiste Boric udf_read_anchor(struct udf_mount *ump, uint32_t sector, struct anchor_vdp **dst)
8429f988b79SJean-Baptiste Boric {
8439f988b79SJean-Baptiste Boric 	int error;
8449f988b79SJean-Baptiste Boric 
8459f988b79SJean-Baptiste Boric 	error = udf_read_phys_dscr(ump, sector, M_UDFVOLD,
8469f988b79SJean-Baptiste Boric 			(union dscrptr **) dst);
8479f988b79SJean-Baptiste Boric 	if (!error) {
8489f988b79SJean-Baptiste Boric 		/* blank terminator blocks are not allowed here */
8499f988b79SJean-Baptiste Boric 		if (*dst == NULL)
8509f988b79SJean-Baptiste Boric 			return ENOENT;
8519f988b79SJean-Baptiste Boric 		if (udf_rw16((*dst)->tag.id) != TAGID_ANCHOR) {
8529f988b79SJean-Baptiste Boric 			error = ENOENT;
8539f988b79SJean-Baptiste Boric 			free(*dst, M_UDFVOLD);
8549f988b79SJean-Baptiste Boric 			*dst = NULL;
8559f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("Not an anchor\n"));
8569f988b79SJean-Baptiste Boric 		}
8579f988b79SJean-Baptiste Boric 	}
8589f988b79SJean-Baptiste Boric 
8599f988b79SJean-Baptiste Boric 	return error;
8609f988b79SJean-Baptiste Boric }
8619f988b79SJean-Baptiste Boric 
8629f988b79SJean-Baptiste Boric 
8639f988b79SJean-Baptiste Boric int
udf_read_anchors(struct udf_mount * ump)8649f988b79SJean-Baptiste Boric udf_read_anchors(struct udf_mount *ump)
8659f988b79SJean-Baptiste Boric {
8669f988b79SJean-Baptiste Boric 	struct udf_args *args = &ump->mount_args;
8679f988b79SJean-Baptiste Boric 	struct mmc_trackinfo first_track;
8689f988b79SJean-Baptiste Boric 	struct mmc_trackinfo second_track;
8699f988b79SJean-Baptiste Boric 	struct mmc_trackinfo last_track;
8709f988b79SJean-Baptiste Boric 	struct anchor_vdp **anchorsp;
8719f988b79SJean-Baptiste Boric 	uint32_t track_start;
8729f988b79SJean-Baptiste Boric 	uint32_t track_end;
8739f988b79SJean-Baptiste Boric 	uint32_t positions[4];
8749f988b79SJean-Baptiste Boric 	int first_tracknr, last_tracknr;
8759f988b79SJean-Baptiste Boric 	int error, anch, ok, first_anchor;
8769f988b79SJean-Baptiste Boric 
8779f988b79SJean-Baptiste Boric 	/* search the first and last track of the specified session */
8789f988b79SJean-Baptiste Boric 	error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr);
8799f988b79SJean-Baptiste Boric 	if (!error) {
8809f988b79SJean-Baptiste Boric 		first_track.tracknr = first_tracknr;
8819f988b79SJean-Baptiste Boric 		error = udf_update_trackinfo(ump, &first_track);
8829f988b79SJean-Baptiste Boric 	}
8839f988b79SJean-Baptiste Boric 	if (!error) {
8849f988b79SJean-Baptiste Boric 		last_track.tracknr = last_tracknr;
8859f988b79SJean-Baptiste Boric 		error = udf_update_trackinfo(ump, &last_track);
8869f988b79SJean-Baptiste Boric 	}
8879f988b79SJean-Baptiste Boric 	if ((!error) && (first_tracknr != last_tracknr)) {
8889f988b79SJean-Baptiste Boric 		second_track.tracknr = first_tracknr+1;
8899f988b79SJean-Baptiste Boric 		error = udf_update_trackinfo(ump, &second_track);
8909f988b79SJean-Baptiste Boric 	}
8919f988b79SJean-Baptiste Boric 	if (error) {
8929f988b79SJean-Baptiste Boric 		printf("UDF mount: reading disc geometry failed\n");
8939f988b79SJean-Baptiste Boric 		return 0;
8949f988b79SJean-Baptiste Boric 	}
8959f988b79SJean-Baptiste Boric 
8969f988b79SJean-Baptiste Boric 	track_start = first_track.track_start;
8979f988b79SJean-Baptiste Boric 
8989f988b79SJean-Baptiste Boric 	/* `end' is not as straitforward as start. */
8999f988b79SJean-Baptiste Boric 	track_end =   last_track.track_start
9009f988b79SJean-Baptiste Boric 		    + last_track.track_size - last_track.free_blocks - 1;
9019f988b79SJean-Baptiste Boric 
9029f988b79SJean-Baptiste Boric 	if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
9039f988b79SJean-Baptiste Boric 		/* end of track is not straitforward here */
9049f988b79SJean-Baptiste Boric 		if (last_track.flags & MMC_TRACKINFO_LRA_VALID)
9059f988b79SJean-Baptiste Boric 			track_end = last_track.last_recorded;
9069f988b79SJean-Baptiste Boric 		else if (last_track.flags & MMC_TRACKINFO_NWA_VALID)
9079f988b79SJean-Baptiste Boric 			track_end = last_track.next_writable
9089f988b79SJean-Baptiste Boric 				    - ump->discinfo.link_block_penalty;
9099f988b79SJean-Baptiste Boric 	}
9109f988b79SJean-Baptiste Boric 
9119f988b79SJean-Baptiste Boric 	/* its no use reading a blank track */
9129f988b79SJean-Baptiste Boric 	first_anchor = 0;
9139f988b79SJean-Baptiste Boric 	if (first_track.flags & MMC_TRACKINFO_BLANK)
9149f988b79SJean-Baptiste Boric 		first_anchor = 1;
9159f988b79SJean-Baptiste Boric 
9169f988b79SJean-Baptiste Boric 	/* get our packet size */
9179f988b79SJean-Baptiste Boric 	ump->packet_size = first_track.packet_size;
9189f988b79SJean-Baptiste Boric 	if (first_track.flags & MMC_TRACKINFO_BLANK)
9199f988b79SJean-Baptiste Boric 		ump->packet_size = second_track.packet_size;
9209f988b79SJean-Baptiste Boric 
9219f988b79SJean-Baptiste Boric 	if (ump->packet_size <= 1) {
9229f988b79SJean-Baptiste Boric 		/* take max, but not bigger than 64 */
9239f988b79SJean-Baptiste Boric 		ump->packet_size = MAXPHYS / ump->discinfo.sector_size;
9249f988b79SJean-Baptiste Boric 		ump->packet_size = MIN(ump->packet_size, 64);
9259f988b79SJean-Baptiste Boric 	}
9269f988b79SJean-Baptiste Boric 	KASSERT(ump->packet_size >= 1);
9279f988b79SJean-Baptiste Boric 
9289f988b79SJean-Baptiste Boric 	/* read anchors start+256, start+512, end-256, end */
9299f988b79SJean-Baptiste Boric 	positions[0] = track_start+256;
9309f988b79SJean-Baptiste Boric 	positions[1] =   track_end-256;
9319f988b79SJean-Baptiste Boric 	positions[2] =   track_end;
9329f988b79SJean-Baptiste Boric 	positions[3] = track_start+512;	/* [UDF 2.60/6.11.2] */
9339f988b79SJean-Baptiste Boric 	/* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */
9349f988b79SJean-Baptiste Boric 
9359f988b79SJean-Baptiste Boric 	ok = 0;
9369f988b79SJean-Baptiste Boric 	anchorsp = ump->anchors;
9379f988b79SJean-Baptiste Boric 	for (anch = first_anchor; anch < 4; anch++) {
9389f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch,
9399f988b79SJean-Baptiste Boric 		    positions[anch]));
9409f988b79SJean-Baptiste Boric 		error = udf_read_anchor(ump, positions[anch], anchorsp);
9419f988b79SJean-Baptiste Boric 		if (!error) {
9429f988b79SJean-Baptiste Boric 			anchorsp++;
9439f988b79SJean-Baptiste Boric 			ok++;
9449f988b79SJean-Baptiste Boric 		}
9459f988b79SJean-Baptiste Boric 	}
9469f988b79SJean-Baptiste Boric 
9479f988b79SJean-Baptiste Boric 	/* VATs are only recorded on sequential media, but initialise */
9489f988b79SJean-Baptiste Boric 	ump->first_possible_vat_location = track_start + 2;
9499f988b79SJean-Baptiste Boric 	ump->last_possible_vat_location  = track_end + last_track.packet_size;
9509f988b79SJean-Baptiste Boric 
9519f988b79SJean-Baptiste Boric 	return ok;
9529f988b79SJean-Baptiste Boric }
9539f988b79SJean-Baptiste Boric 
9549f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
9559f988b79SJean-Baptiste Boric 
9569f988b79SJean-Baptiste Boric int
udf_get_c_type(struct udf_node * udf_node)9579f988b79SJean-Baptiste Boric udf_get_c_type(struct udf_node *udf_node)
9589f988b79SJean-Baptiste Boric {
9599f988b79SJean-Baptiste Boric 	int isdir, what;
9609f988b79SJean-Baptiste Boric 
9619f988b79SJean-Baptiste Boric 	isdir  = (udf_node->vnode->v_type == VDIR);
9629f988b79SJean-Baptiste Boric 	what   = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
9639f988b79SJean-Baptiste Boric 
9649f988b79SJean-Baptiste Boric 	if (udf_node->ump)
9659f988b79SJean-Baptiste Boric 		if (udf_node == udf_node->ump->metadatabitmap_node)
9669f988b79SJean-Baptiste Boric 			what = UDF_C_METADATA_SBM;
9679f988b79SJean-Baptiste Boric 
9689f988b79SJean-Baptiste Boric 	return what;
9699f988b79SJean-Baptiste Boric }
9709f988b79SJean-Baptiste Boric 
9719f988b79SJean-Baptiste Boric 
9729f988b79SJean-Baptiste Boric int
udf_get_record_vpart(struct udf_mount * ump,int udf_c_type)9739f988b79SJean-Baptiste Boric udf_get_record_vpart(struct udf_mount *ump, int udf_c_type)
9749f988b79SJean-Baptiste Boric {
9759f988b79SJean-Baptiste Boric 	int vpart_num;
9769f988b79SJean-Baptiste Boric 
9779f988b79SJean-Baptiste Boric 	vpart_num = ump->data_part;
9789f988b79SJean-Baptiste Boric 	if (udf_c_type == UDF_C_NODE)
9799f988b79SJean-Baptiste Boric 		vpart_num = ump->node_part;
9809f988b79SJean-Baptiste Boric 	if (udf_c_type == UDF_C_FIDS)
9819f988b79SJean-Baptiste Boric 		vpart_num = ump->fids_part;
9829f988b79SJean-Baptiste Boric 
9839f988b79SJean-Baptiste Boric 	return vpart_num;
9849f988b79SJean-Baptiste Boric }
9859f988b79SJean-Baptiste Boric 
9869f988b79SJean-Baptiste Boric 
9879f988b79SJean-Baptiste Boric /*
9889f988b79SJean-Baptiste Boric  * BUGALERT: some rogue implementations use random physical partition
9899f988b79SJean-Baptiste Boric  * numbers to break other implementations so lookup the number.
9909f988b79SJean-Baptiste Boric  */
9919f988b79SJean-Baptiste Boric 
9929f988b79SJean-Baptiste Boric static uint16_t
udf_find_raw_phys(struct udf_mount * ump,uint16_t raw_phys_part)9939f988b79SJean-Baptiste Boric udf_find_raw_phys(struct udf_mount *ump, uint16_t raw_phys_part)
9949f988b79SJean-Baptiste Boric {
9959f988b79SJean-Baptiste Boric 	struct part_desc *part;
9969f988b79SJean-Baptiste Boric 	uint16_t phys_part;
9979f988b79SJean-Baptiste Boric 
9989f988b79SJean-Baptiste Boric 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
9999f988b79SJean-Baptiste Boric 		part = ump->partitions[phys_part];
10009f988b79SJean-Baptiste Boric 		if (part == NULL)
10019f988b79SJean-Baptiste Boric 			break;
10029f988b79SJean-Baptiste Boric 		if (udf_rw16(part->part_num) == raw_phys_part)
10039f988b79SJean-Baptiste Boric 			break;
10049f988b79SJean-Baptiste Boric 	}
10059f988b79SJean-Baptiste Boric 	return phys_part;
10069f988b79SJean-Baptiste Boric }
10079f988b79SJean-Baptiste Boric 
10089f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
10099f988b79SJean-Baptiste Boric 
10109f988b79SJean-Baptiste Boric /* we dont try to be smart; we just record the parts */
10119f988b79SJean-Baptiste Boric #define UDF_UPDATE_DSCR(name, dscr) \
10129f988b79SJean-Baptiste Boric 	if (name) \
10139f988b79SJean-Baptiste Boric 		free(name, M_UDFVOLD); \
10149f988b79SJean-Baptiste Boric 	name = dscr;
10159f988b79SJean-Baptiste Boric 
10169f988b79SJean-Baptiste Boric static int
udf_process_vds_descriptor(struct udf_mount * ump,union dscrptr * dscr)10179f988b79SJean-Baptiste Boric udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr)
10189f988b79SJean-Baptiste Boric {
10199f988b79SJean-Baptiste Boric 	uint16_t phys_part, raw_phys_part;
10209f988b79SJean-Baptiste Boric 
10219f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n",
10229f988b79SJean-Baptiste Boric 	    udf_rw16(dscr->tag.id)));
10239f988b79SJean-Baptiste Boric 	switch (udf_rw16(dscr->tag.id)) {
10249f988b79SJean-Baptiste Boric 	case TAGID_PRI_VOL :		/* primary partition		*/
10259f988b79SJean-Baptiste Boric 		UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd);
10269f988b79SJean-Baptiste Boric 		break;
10279f988b79SJean-Baptiste Boric 	case TAGID_LOGVOL :		/* logical volume		*/
10289f988b79SJean-Baptiste Boric 		UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd);
10299f988b79SJean-Baptiste Boric 		break;
10309f988b79SJean-Baptiste Boric 	case TAGID_UNALLOC_SPACE :	/* unallocated space		*/
10319f988b79SJean-Baptiste Boric 		UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd);
10329f988b79SJean-Baptiste Boric 		break;
10339f988b79SJean-Baptiste Boric 	case TAGID_IMP_VOL :		/* implementation		*/
10349f988b79SJean-Baptiste Boric 		/* XXX do we care about multiple impl. descr ? */
10359f988b79SJean-Baptiste Boric 		UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd);
10369f988b79SJean-Baptiste Boric 		break;
10379f988b79SJean-Baptiste Boric 	case TAGID_PARTITION :		/* physical partition		*/
10389f988b79SJean-Baptiste Boric 		/* not much use if its not allocated */
10399f988b79SJean-Baptiste Boric 		if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) {
10409f988b79SJean-Baptiste Boric 			free(dscr, M_UDFVOLD);
10419f988b79SJean-Baptiste Boric 			break;
10429f988b79SJean-Baptiste Boric 		}
10439f988b79SJean-Baptiste Boric 
10449f988b79SJean-Baptiste Boric 		/*
10459f988b79SJean-Baptiste Boric 		 * BUGALERT: some rogue implementations use random physical
10469f988b79SJean-Baptiste Boric 		 * partition numbers to break other implementations so lookup
10479f988b79SJean-Baptiste Boric 		 * the number.
10489f988b79SJean-Baptiste Boric 		 */
10499f988b79SJean-Baptiste Boric 		raw_phys_part = udf_rw16(dscr->pd.part_num);
10509f988b79SJean-Baptiste Boric 		phys_part = udf_find_raw_phys(ump, raw_phys_part);
10519f988b79SJean-Baptiste Boric 
10529f988b79SJean-Baptiste Boric 		if (phys_part == UDF_PARTITIONS) {
10539f988b79SJean-Baptiste Boric 			free(dscr, M_UDFVOLD);
10549f988b79SJean-Baptiste Boric 			return EINVAL;
10559f988b79SJean-Baptiste Boric 		}
10569f988b79SJean-Baptiste Boric 
10579f988b79SJean-Baptiste Boric 		UDF_UPDATE_DSCR(ump->partitions[phys_part], &dscr->pd);
10589f988b79SJean-Baptiste Boric 		break;
10599f988b79SJean-Baptiste Boric 	case TAGID_VOL :		/* volume space extender; rare	*/
10609f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("VDS extender ignored\n"));
10619f988b79SJean-Baptiste Boric 		free(dscr, M_UDFVOLD);
10629f988b79SJean-Baptiste Boric 		break;
10639f988b79SJean-Baptiste Boric 	default :
10649f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Unhandled VDS type %d\n",
10659f988b79SJean-Baptiste Boric 		    udf_rw16(dscr->tag.id)));
10669f988b79SJean-Baptiste Boric 		free(dscr, M_UDFVOLD);
10679f988b79SJean-Baptiste Boric 	}
10689f988b79SJean-Baptiste Boric 
10699f988b79SJean-Baptiste Boric 	return 0;
10709f988b79SJean-Baptiste Boric }
10719f988b79SJean-Baptiste Boric #undef UDF_UPDATE_DSCR
10729f988b79SJean-Baptiste Boric 
10739f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
10749f988b79SJean-Baptiste Boric 
10759f988b79SJean-Baptiste Boric static int
udf_read_vds_extent(struct udf_mount * ump,uint32_t loc,uint32_t len)10769f988b79SJean-Baptiste Boric udf_read_vds_extent(struct udf_mount *ump, uint32_t loc, uint32_t len)
10779f988b79SJean-Baptiste Boric {
10789f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
10799f988b79SJean-Baptiste Boric 	uint32_t sector_size, dscr_size;
10809f988b79SJean-Baptiste Boric 	int error;
10819f988b79SJean-Baptiste Boric 
10829f988b79SJean-Baptiste Boric 	sector_size = ump->discinfo.sector_size;
10839f988b79SJean-Baptiste Boric 
10849f988b79SJean-Baptiste Boric 	/* loc is sectornr, len is in bytes */
10859f988b79SJean-Baptiste Boric 	error = EIO;
10869f988b79SJean-Baptiste Boric 	while (len) {
10879f988b79SJean-Baptiste Boric 		error = udf_read_phys_dscr(ump, loc, M_UDFVOLD, &dscr);
10889f988b79SJean-Baptiste Boric 		if (error)
10899f988b79SJean-Baptiste Boric 			return error;
10909f988b79SJean-Baptiste Boric 
10919f988b79SJean-Baptiste Boric 		/* blank block is a terminator */
10929f988b79SJean-Baptiste Boric 		if (dscr == NULL)
10939f988b79SJean-Baptiste Boric 			return 0;
10949f988b79SJean-Baptiste Boric 
10959f988b79SJean-Baptiste Boric 		/* TERM descriptor is a terminator */
10969f988b79SJean-Baptiste Boric 		if (udf_rw16(dscr->tag.id) == TAGID_TERM) {
10979f988b79SJean-Baptiste Boric 			free(dscr, M_UDFVOLD);
10989f988b79SJean-Baptiste Boric 			return 0;
10999f988b79SJean-Baptiste Boric 		}
11009f988b79SJean-Baptiste Boric 
11019f988b79SJean-Baptiste Boric 		/* process all others */
11029f988b79SJean-Baptiste Boric 		dscr_size = udf_tagsize(dscr, sector_size);
11039f988b79SJean-Baptiste Boric 		error = udf_process_vds_descriptor(ump, dscr);
11049f988b79SJean-Baptiste Boric 		if (error) {
11059f988b79SJean-Baptiste Boric 			free(dscr, M_UDFVOLD);
11069f988b79SJean-Baptiste Boric 			break;
11079f988b79SJean-Baptiste Boric 		}
11089f988b79SJean-Baptiste Boric 		assert((dscr_size % sector_size) == 0);
11099f988b79SJean-Baptiste Boric 
11109f988b79SJean-Baptiste Boric 		len -= dscr_size;
11119f988b79SJean-Baptiste Boric 		loc += dscr_size / sector_size;
11129f988b79SJean-Baptiste Boric 	}
11139f988b79SJean-Baptiste Boric 
11149f988b79SJean-Baptiste Boric 	return error;
11159f988b79SJean-Baptiste Boric }
11169f988b79SJean-Baptiste Boric 
11179f988b79SJean-Baptiste Boric 
11189f988b79SJean-Baptiste Boric int
udf_read_vds_space(struct udf_mount * ump)11199f988b79SJean-Baptiste Boric udf_read_vds_space(struct udf_mount *ump)
11209f988b79SJean-Baptiste Boric {
11219f988b79SJean-Baptiste Boric 	/* struct udf_args *args = &ump->mount_args; */
11229f988b79SJean-Baptiste Boric 	struct anchor_vdp *anchor, *anchor2;
11239f988b79SJean-Baptiste Boric 	size_t size;
11249f988b79SJean-Baptiste Boric 	uint32_t main_loc, main_len;
11259f988b79SJean-Baptiste Boric 	uint32_t reserve_loc, reserve_len;
11269f988b79SJean-Baptiste Boric 	int error;
11279f988b79SJean-Baptiste Boric 
11289f988b79SJean-Baptiste Boric 	/*
11299f988b79SJean-Baptiste Boric 	 * read in VDS space provided by the anchors; if one descriptor read
11309f988b79SJean-Baptiste Boric 	 * fails, try the mirror sector.
11319f988b79SJean-Baptiste Boric 	 *
11329f988b79SJean-Baptiste Boric 	 * check if 2nd anchor is different from 1st; if so, go for 2nd. This
11339f988b79SJean-Baptiste Boric 	 * avoids the `compatibility features' of DirectCD that may confuse
11349f988b79SJean-Baptiste Boric 	 * stuff completely.
11359f988b79SJean-Baptiste Boric 	 */
11369f988b79SJean-Baptiste Boric 
11379f988b79SJean-Baptiste Boric 	anchor  = ump->anchors[0];
11389f988b79SJean-Baptiste Boric 	anchor2 = ump->anchors[1];
11399f988b79SJean-Baptiste Boric 	assert(anchor);
11409f988b79SJean-Baptiste Boric 
11419f988b79SJean-Baptiste Boric 	if (anchor2) {
11429f988b79SJean-Baptiste Boric 		size = sizeof(struct extent_ad);
11439f988b79SJean-Baptiste Boric 		if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size))
11449f988b79SJean-Baptiste Boric 			anchor = anchor2;
11459f988b79SJean-Baptiste Boric 		/* reserve is specified to be a literal copy of main */
11469f988b79SJean-Baptiste Boric 	}
11479f988b79SJean-Baptiste Boric 
11489f988b79SJean-Baptiste Boric 	main_loc    = udf_rw32(anchor->main_vds_ex.loc);
11499f988b79SJean-Baptiste Boric 	main_len    = udf_rw32(anchor->main_vds_ex.len);
11509f988b79SJean-Baptiste Boric 
11519f988b79SJean-Baptiste Boric 	reserve_loc = udf_rw32(anchor->reserve_vds_ex.loc);
11529f988b79SJean-Baptiste Boric 	reserve_len = udf_rw32(anchor->reserve_vds_ex.len);
11539f988b79SJean-Baptiste Boric 
11549f988b79SJean-Baptiste Boric 	error = udf_read_vds_extent(ump, main_loc, main_len);
11559f988b79SJean-Baptiste Boric 	if (error) {
11569f988b79SJean-Baptiste Boric 		printf("UDF mount: reading in reserve VDS extent\n");
11579f988b79SJean-Baptiste Boric 		error = udf_read_vds_extent(ump, reserve_loc, reserve_len);
11589f988b79SJean-Baptiste Boric 	}
11599f988b79SJean-Baptiste Boric 
11609f988b79SJean-Baptiste Boric 	return error;
11619f988b79SJean-Baptiste Boric }
11629f988b79SJean-Baptiste Boric 
11639f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
11649f988b79SJean-Baptiste Boric 
11659f988b79SJean-Baptiste Boric /*
11669f988b79SJean-Baptiste Boric  * Read in the logical volume integrity sequence pointed to by our logical
11679f988b79SJean-Baptiste Boric  * volume descriptor. Its a sequence that can be extended using fields in the
11689f988b79SJean-Baptiste Boric  * integrity descriptor itself. On sequential media only one is found, on
11699f988b79SJean-Baptiste Boric  * rewritable media a sequence of descriptors can be found as a form of
11709f988b79SJean-Baptiste Boric  * history keeping and on non sequential write-once media the chain is vital
11719f988b79SJean-Baptiste Boric  * to allow more and more descriptors to be written. The last descriptor
11729f988b79SJean-Baptiste Boric  * written in an extent needs to claim space for a new extent.
11739f988b79SJean-Baptiste Boric  */
11749f988b79SJean-Baptiste Boric 
11759f988b79SJean-Baptiste Boric static int
udf_retrieve_lvint(struct udf_mount * ump)11769f988b79SJean-Baptiste Boric udf_retrieve_lvint(struct udf_mount *ump)
11779f988b79SJean-Baptiste Boric {
11789f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
11799f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvint;
11809f988b79SJean-Baptiste Boric 	struct udf_lvintq *trace;
11819f988b79SJean-Baptiste Boric 	uint32_t lb_size, lbnum, len;
11829f988b79SJean-Baptiste Boric 	int dscr_type, error, trace_len;
11839f988b79SJean-Baptiste Boric 
11849f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
11859f988b79SJean-Baptiste Boric 	len     = udf_rw32(ump->logical_vol->integrity_seq_loc.len);
11869f988b79SJean-Baptiste Boric 	lbnum   = udf_rw32(ump->logical_vol->integrity_seq_loc.loc);
11879f988b79SJean-Baptiste Boric 
11889f988b79SJean-Baptiste Boric 	/* clean trace */
11899f988b79SJean-Baptiste Boric 	memset(ump->lvint_trace, 0,
11909f988b79SJean-Baptiste Boric 	    UDF_LVDINT_SEGMENTS * sizeof(struct udf_lvintq));
11919f988b79SJean-Baptiste Boric 
11929f988b79SJean-Baptiste Boric 	trace_len    = 0;
11939f988b79SJean-Baptiste Boric 	trace        = ump->lvint_trace;
11949f988b79SJean-Baptiste Boric 	trace->start = lbnum;
11959f988b79SJean-Baptiste Boric 	trace->end   = lbnum + len/lb_size;
11969f988b79SJean-Baptiste Boric 	trace->pos   = 0;
11979f988b79SJean-Baptiste Boric 	trace->wpos  = 0;
11989f988b79SJean-Baptiste Boric 
11999f988b79SJean-Baptiste Boric 	lvint = NULL;
12009f988b79SJean-Baptiste Boric 	dscr  = NULL;
12019f988b79SJean-Baptiste Boric 	error = 0;
12029f988b79SJean-Baptiste Boric 	while (len) {
12039f988b79SJean-Baptiste Boric 		trace->pos  = lbnum - trace->start;
12049f988b79SJean-Baptiste Boric 		trace->wpos = trace->pos + 1;
12059f988b79SJean-Baptiste Boric 
12069f988b79SJean-Baptiste Boric 		/* read in our integrity descriptor */
12079f988b79SJean-Baptiste Boric 		error = udf_read_phys_dscr(ump, lbnum, M_UDFVOLD, &dscr);
12089f988b79SJean-Baptiste Boric 		if (!error) {
12099f988b79SJean-Baptiste Boric 			if (dscr == NULL) {
12109f988b79SJean-Baptiste Boric 				trace->wpos = trace->pos;
12119f988b79SJean-Baptiste Boric 				break;		/* empty terminates */
12129f988b79SJean-Baptiste Boric 			}
12139f988b79SJean-Baptiste Boric 			dscr_type = udf_rw16(dscr->tag.id);
12149f988b79SJean-Baptiste Boric 			if (dscr_type == TAGID_TERM) {
12159f988b79SJean-Baptiste Boric 				trace->wpos = trace->pos;
12169f988b79SJean-Baptiste Boric 				break;		/* clean terminator */
12179f988b79SJean-Baptiste Boric 			}
12189f988b79SJean-Baptiste Boric 			if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
12199f988b79SJean-Baptiste Boric 				/* fatal... corrupt disc */
12209f988b79SJean-Baptiste Boric 				error = ENOENT;
12219f988b79SJean-Baptiste Boric 				break;
12229f988b79SJean-Baptiste Boric 			}
12239f988b79SJean-Baptiste Boric 			if (lvint)
12249f988b79SJean-Baptiste Boric 				free(lvint, M_UDFVOLD);
12259f988b79SJean-Baptiste Boric 			lvint = &dscr->lvid;
12269f988b79SJean-Baptiste Boric 			dscr = NULL;
12279f988b79SJean-Baptiste Boric 		} /* else hope for the best... maybe the next is ok */
12289f988b79SJean-Baptiste Boric 
12299f988b79SJean-Baptiste Boric 		DPRINTFIF(VOLUMES, lvint, ("logvol integrity read, state %s\n",
12309f988b79SJean-Baptiste Boric 		    udf_rw32(lvint->integrity_type) ? "CLOSED" : "OPEN"));
12319f988b79SJean-Baptiste Boric 
12329f988b79SJean-Baptiste Boric 		/* proceed sequential */
12339f988b79SJean-Baptiste Boric 		lbnum += 1;
12349f988b79SJean-Baptiste Boric 		len    -= lb_size;
12359f988b79SJean-Baptiste Boric 
12369f988b79SJean-Baptiste Boric 		/* are we linking to a new piece? */
12379f988b79SJean-Baptiste Boric 		if (dscr && lvint->next_extent.len) {
12389f988b79SJean-Baptiste Boric 			len    = udf_rw32(lvint->next_extent.len);
12399f988b79SJean-Baptiste Boric 			lbnum = udf_rw32(lvint->next_extent.loc);
12409f988b79SJean-Baptiste Boric 
12419f988b79SJean-Baptiste Boric 			if (trace_len >= UDF_LVDINT_SEGMENTS-1) {
12429f988b79SJean-Baptiste Boric 				/* IEK! segment link full... */
12439f988b79SJean-Baptiste Boric 				DPRINTF(VOLUMES, ("lvdint segments full\n"));
12449f988b79SJean-Baptiste Boric 				error = EINVAL;
12459f988b79SJean-Baptiste Boric 			} else {
12469f988b79SJean-Baptiste Boric 				trace++;
12479f988b79SJean-Baptiste Boric 				trace_len++;
12489f988b79SJean-Baptiste Boric 
12499f988b79SJean-Baptiste Boric 				trace->start = lbnum;
12509f988b79SJean-Baptiste Boric 				trace->end   = lbnum + len/lb_size;
12519f988b79SJean-Baptiste Boric 				trace->pos   = 0;
12529f988b79SJean-Baptiste Boric 				trace->wpos  = 0;
12539f988b79SJean-Baptiste Boric 			}
12549f988b79SJean-Baptiste Boric 		}
12559f988b79SJean-Baptiste Boric 	}
12569f988b79SJean-Baptiste Boric 
12579f988b79SJean-Baptiste Boric 	/* clean up the mess, esp. when there is an error */
12589f988b79SJean-Baptiste Boric 	if (dscr)
12599f988b79SJean-Baptiste Boric 		free(dscr, M_UDFVOLD);
12609f988b79SJean-Baptiste Boric 
12619f988b79SJean-Baptiste Boric 	if (error && lvint) {
12629f988b79SJean-Baptiste Boric 		free(lvint, M_UDFVOLD);
12639f988b79SJean-Baptiste Boric 		lvint = NULL;
12649f988b79SJean-Baptiste Boric 	}
12659f988b79SJean-Baptiste Boric 
12669f988b79SJean-Baptiste Boric 	if (!lvint)
12679f988b79SJean-Baptiste Boric 		error = ENOENT;
12689f988b79SJean-Baptiste Boric 
12699f988b79SJean-Baptiste Boric 	ump->logvol_integrity = lvint;
12709f988b79SJean-Baptiste Boric 	return error;
12719f988b79SJean-Baptiste Boric }
12729f988b79SJean-Baptiste Boric 
12739f988b79SJean-Baptiste Boric 
12749f988b79SJean-Baptiste Boric static int
udf_loose_lvint_history(struct udf_mount * ump)12759f988b79SJean-Baptiste Boric udf_loose_lvint_history(struct udf_mount *ump)
12769f988b79SJean-Baptiste Boric {
12779f988b79SJean-Baptiste Boric 	union dscrptr **bufs, *dscr, *last_dscr;
12789f988b79SJean-Baptiste Boric 	struct udf_lvintq *trace, *in_trace, *out_trace;
12799f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvint;
12809f988b79SJean-Baptiste Boric 	uint32_t in_ext, in_pos, in_len;
12819f988b79SJean-Baptiste Boric 	uint32_t out_ext, out_wpos, out_len;
12829f988b79SJean-Baptiste Boric 	uint32_t lb_num;
12839f988b79SJean-Baptiste Boric 	uint32_t len, start;
12849f988b79SJean-Baptiste Boric 	int ext, minext, extlen, cnt, cpy_len, dscr_type;
12859f988b79SJean-Baptiste Boric 	int losing;
12869f988b79SJean-Baptiste Boric 	int error;
12879f988b79SJean-Baptiste Boric 
12889f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("need to lose some lvint history\n"));
12899f988b79SJean-Baptiste Boric 
12909f988b79SJean-Baptiste Boric 	/* search smallest extent */
12919f988b79SJean-Baptiste Boric 	trace = &ump->lvint_trace[0];
12929f988b79SJean-Baptiste Boric 	minext = trace->end - trace->start;
12939f988b79SJean-Baptiste Boric 	for (ext = 1; ext < UDF_LVDINT_SEGMENTS; ext++) {
12949f988b79SJean-Baptiste Boric 		trace = &ump->lvint_trace[ext];
12959f988b79SJean-Baptiste Boric 		extlen = trace->end - trace->start;
12969f988b79SJean-Baptiste Boric 		if (extlen == 0)
12979f988b79SJean-Baptiste Boric 			break;
12989f988b79SJean-Baptiste Boric 		minext = MIN(minext, extlen);
12999f988b79SJean-Baptiste Boric 	}
13009f988b79SJean-Baptiste Boric 	losing = MIN(minext, UDF_LVINT_LOSSAGE);
13019f988b79SJean-Baptiste Boric 	/* no sense wiping all */
13029f988b79SJean-Baptiste Boric 	if (losing == minext)
13039f988b79SJean-Baptiste Boric 		losing--;
13049f988b79SJean-Baptiste Boric 
13059f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tlosing %d entries\n", losing));
13069f988b79SJean-Baptiste Boric 
13079f988b79SJean-Baptiste Boric 	/* get buffer for pieces */
13089f988b79SJean-Baptiste Boric 	bufs = malloc(UDF_LVDINT_SEGMENTS * sizeof(void *), M_TEMP, M_WAITOK);
13099f988b79SJean-Baptiste Boric 
13109f988b79SJean-Baptiste Boric 	in_ext    = 0;
13119f988b79SJean-Baptiste Boric 	in_pos    = losing;
13129f988b79SJean-Baptiste Boric 	in_trace  = &ump->lvint_trace[in_ext];
13139f988b79SJean-Baptiste Boric 	in_len    = in_trace->end - in_trace->start;
13149f988b79SJean-Baptiste Boric 	out_ext   = 0;
13159f988b79SJean-Baptiste Boric 	out_wpos  = 0;
13169f988b79SJean-Baptiste Boric 	out_trace = &ump->lvint_trace[out_ext];
13179f988b79SJean-Baptiste Boric 	out_len   = out_trace->end - out_trace->start;
13189f988b79SJean-Baptiste Boric 
13199f988b79SJean-Baptiste Boric 	last_dscr = NULL;
13209f988b79SJean-Baptiste Boric 	for(;;) {
13219f988b79SJean-Baptiste Boric 		out_trace->pos  = out_wpos;
13229f988b79SJean-Baptiste Boric 		out_trace->wpos = out_trace->pos;
13239f988b79SJean-Baptiste Boric 		if (in_pos >= in_len) {
13249f988b79SJean-Baptiste Boric 			in_ext++;
13259f988b79SJean-Baptiste Boric 			in_pos = 0;
13269f988b79SJean-Baptiste Boric 			in_trace = &ump->lvint_trace[in_ext];
13279f988b79SJean-Baptiste Boric 			in_len   = in_trace->end - in_trace->start;
13289f988b79SJean-Baptiste Boric 		}
13299f988b79SJean-Baptiste Boric 		if (out_wpos >= out_len) {
13309f988b79SJean-Baptiste Boric 			out_ext++;
13319f988b79SJean-Baptiste Boric 			out_wpos = 0;
13329f988b79SJean-Baptiste Boric 			out_trace = &ump->lvint_trace[out_ext];
13339f988b79SJean-Baptiste Boric 			out_len   = out_trace->end - out_trace->start;
13349f988b79SJean-Baptiste Boric 		}
13359f988b79SJean-Baptiste Boric 		/* copy overlap contents */
13369f988b79SJean-Baptiste Boric 		cpy_len = MIN(in_len - in_pos, out_len - out_wpos);
13379f988b79SJean-Baptiste Boric 		cpy_len = MIN(cpy_len, in_len - in_trace->pos);
13389f988b79SJean-Baptiste Boric 		if (cpy_len == 0)
13399f988b79SJean-Baptiste Boric 			break;
13409f988b79SJean-Baptiste Boric 
13419f988b79SJean-Baptiste Boric 		/* copy */
13429f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("\treading %d lvid descriptors\n", cpy_len));
13439f988b79SJean-Baptiste Boric 		for (cnt = 0; cnt < cpy_len; cnt++) {
13449f988b79SJean-Baptiste Boric 			/* read in our integrity descriptor */
13459f988b79SJean-Baptiste Boric 			lb_num = in_trace->start + in_pos + cnt;
13469f988b79SJean-Baptiste Boric 			error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD,
13479f988b79SJean-Baptiste Boric 				&dscr);
13489f988b79SJean-Baptiste Boric 			if (error) {
13499f988b79SJean-Baptiste Boric 				/* copy last one */
13509f988b79SJean-Baptiste Boric 				dscr = last_dscr;
13519f988b79SJean-Baptiste Boric 			}
13529f988b79SJean-Baptiste Boric 			bufs[cnt] = dscr;
13539f988b79SJean-Baptiste Boric 			if (!error) {
13549f988b79SJean-Baptiste Boric 				if (dscr == NULL) {
13559f988b79SJean-Baptiste Boric 					out_trace->pos  = out_wpos + cnt;
13569f988b79SJean-Baptiste Boric 					out_trace->wpos = out_trace->pos;
13579f988b79SJean-Baptiste Boric 					break;		/* empty terminates */
13589f988b79SJean-Baptiste Boric 				}
13599f988b79SJean-Baptiste Boric 				dscr_type = udf_rw16(dscr->tag.id);
13609f988b79SJean-Baptiste Boric 				if (dscr_type == TAGID_TERM) {
13619f988b79SJean-Baptiste Boric 					out_trace->pos  = out_wpos + cnt;
13629f988b79SJean-Baptiste Boric 					out_trace->wpos = out_trace->pos;
13639f988b79SJean-Baptiste Boric 					break;		/* clean terminator */
13649f988b79SJean-Baptiste Boric 				}
13659f988b79SJean-Baptiste Boric 				if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
13669f988b79SJean-Baptiste Boric 					panic(  "UDF integrity sequence "
13679f988b79SJean-Baptiste Boric 						"corrupted while mounted!\n");
13689f988b79SJean-Baptiste Boric 				}
13699f988b79SJean-Baptiste Boric 				last_dscr = dscr;
13709f988b79SJean-Baptiste Boric 			}
13719f988b79SJean-Baptiste Boric 		}
13729f988b79SJean-Baptiste Boric 
13739f988b79SJean-Baptiste Boric 		/* patch up if first entry was on error */
13749f988b79SJean-Baptiste Boric 		if (bufs[0] == NULL) {
13759f988b79SJean-Baptiste Boric 			for (cnt = 0; cnt < cpy_len; cnt++)
13769f988b79SJean-Baptiste Boric 				if (bufs[cnt] != NULL)
13779f988b79SJean-Baptiste Boric 					break;
13789f988b79SJean-Baptiste Boric 			last_dscr = bufs[cnt];
13799f988b79SJean-Baptiste Boric 			for (; cnt > 0; cnt--) {
13809f988b79SJean-Baptiste Boric 				bufs[cnt] = last_dscr;
13819f988b79SJean-Baptiste Boric 			}
13829f988b79SJean-Baptiste Boric 		}
13839f988b79SJean-Baptiste Boric 
13849f988b79SJean-Baptiste Boric 		/* glue + write out */
13859f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("\twriting %d lvid descriptors\n", cpy_len));
13869f988b79SJean-Baptiste Boric 		for (cnt = 0; cnt < cpy_len; cnt++) {
13879f988b79SJean-Baptiste Boric 			lb_num = out_trace->start + out_wpos + cnt;
13889f988b79SJean-Baptiste Boric 			lvint  = &bufs[cnt]->lvid;
13899f988b79SJean-Baptiste Boric 
13909f988b79SJean-Baptiste Boric 			/* set continuation */
13919f988b79SJean-Baptiste Boric 			len = 0;
13929f988b79SJean-Baptiste Boric 			start = 0;
13939f988b79SJean-Baptiste Boric 			if (out_wpos + cnt == out_len) {
13949f988b79SJean-Baptiste Boric 				/* get continuation */
13959f988b79SJean-Baptiste Boric 				trace = &ump->lvint_trace[out_ext+1];
13969f988b79SJean-Baptiste Boric 				len   = trace->end - trace->start;
13979f988b79SJean-Baptiste Boric 				start = trace->start;
13989f988b79SJean-Baptiste Boric 			}
13999f988b79SJean-Baptiste Boric 			lvint->next_extent.len = udf_rw32(len);
14009f988b79SJean-Baptiste Boric 			lvint->next_extent.loc = udf_rw32(start);
14019f988b79SJean-Baptiste Boric 
14029f988b79SJean-Baptiste Boric 			lb_num = trace->start + trace->wpos;
14039f988b79SJean-Baptiste Boric 			error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
14049f988b79SJean-Baptiste Boric 				bufs[cnt], lb_num, lb_num);
14059f988b79SJean-Baptiste Boric 			DPRINTFIF(VOLUMES, error,
14069f988b79SJean-Baptiste Boric 				("error writing lvint lb_num\n"));
14079f988b79SJean-Baptiste Boric 		}
14089f988b79SJean-Baptiste Boric 
14099f988b79SJean-Baptiste Boric 		/* free non repeating descriptors */
14109f988b79SJean-Baptiste Boric 		last_dscr = NULL;
14119f988b79SJean-Baptiste Boric 		for (cnt = 0; cnt < cpy_len; cnt++) {
14129f988b79SJean-Baptiste Boric 			if (bufs[cnt] != last_dscr)
14139f988b79SJean-Baptiste Boric 				free(bufs[cnt], M_UDFVOLD);
14149f988b79SJean-Baptiste Boric 			last_dscr = bufs[cnt];
14159f988b79SJean-Baptiste Boric 		}
14169f988b79SJean-Baptiste Boric 
14179f988b79SJean-Baptiste Boric 		/* advance */
14189f988b79SJean-Baptiste Boric 		in_pos   += cpy_len;
14199f988b79SJean-Baptiste Boric 		out_wpos += cpy_len;
14209f988b79SJean-Baptiste Boric 	}
14219f988b79SJean-Baptiste Boric 
14229f988b79SJean-Baptiste Boric 	free(bufs, M_TEMP);
14239f988b79SJean-Baptiste Boric 
14249f988b79SJean-Baptiste Boric 	return 0;
14259f988b79SJean-Baptiste Boric }
14269f988b79SJean-Baptiste Boric 
14279f988b79SJean-Baptiste Boric 
14289f988b79SJean-Baptiste Boric static int
udf_writeout_lvint(struct udf_mount * ump,int lvflag)14299f988b79SJean-Baptiste Boric udf_writeout_lvint(struct udf_mount *ump, int lvflag)
14309f988b79SJean-Baptiste Boric {
14319f988b79SJean-Baptiste Boric 	struct udf_lvintq *trace;
14329f988b79SJean-Baptiste Boric 	struct timeval  now_v;
14339f988b79SJean-Baptiste Boric 	struct timespec now_s;
14349f988b79SJean-Baptiste Boric 	uint32_t sector;
14359f988b79SJean-Baptiste Boric 	int logvol_integrity;
14369f988b79SJean-Baptiste Boric 	int space, error;
14379f988b79SJean-Baptiste Boric 
14389f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("writing out logvol integrity descriptor\n"));
14399f988b79SJean-Baptiste Boric 
14409f988b79SJean-Baptiste Boric again:
14419f988b79SJean-Baptiste Boric 	/* get free space in last chunk */
14429f988b79SJean-Baptiste Boric 	trace = ump->lvint_trace;
14439f988b79SJean-Baptiste Boric 	while (trace->wpos > (trace->end - trace->start)) {
14449f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("skip : start = %d, end = %d, pos = %d, "
14459f988b79SJean-Baptiste Boric 				  "wpos = %d\n", trace->start, trace->end,
14469f988b79SJean-Baptiste Boric 				  trace->pos, trace->wpos));
14479f988b79SJean-Baptiste Boric 		trace++;
14489f988b79SJean-Baptiste Boric 	}
14499f988b79SJean-Baptiste Boric 
14509f988b79SJean-Baptiste Boric 	/* check if there is space to append */
14519f988b79SJean-Baptiste Boric 	space = (trace->end - trace->start) - trace->wpos;
14529f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, "
14539f988b79SJean-Baptiste Boric 			  "space = %d\n", trace->start, trace->end, trace->pos,
14549f988b79SJean-Baptiste Boric 			  trace->wpos, space));
14559f988b79SJean-Baptiste Boric 
14569f988b79SJean-Baptiste Boric 	/* get state */
14579f988b79SJean-Baptiste Boric 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
14589f988b79SJean-Baptiste Boric 	if (logvol_integrity == UDF_INTEGRITY_CLOSED) {
14599f988b79SJean-Baptiste Boric 		if ((space < 3) && (lvflag & UDF_APPENDONLY_LVINT)) {
14609f988b79SJean-Baptiste Boric 			/* TODO extent LVINT space if possible */
14619f988b79SJean-Baptiste Boric 			return EROFS;
14629f988b79SJean-Baptiste Boric 		}
14639f988b79SJean-Baptiste Boric 	}
14649f988b79SJean-Baptiste Boric 
14659f988b79SJean-Baptiste Boric 	if (space < 1) {
14669f988b79SJean-Baptiste Boric 		if (lvflag & UDF_APPENDONLY_LVINT)
14679f988b79SJean-Baptiste Boric 			return EROFS;
14689f988b79SJean-Baptiste Boric 		/* loose history by re-writing extents */
14699f988b79SJean-Baptiste Boric 		error = udf_loose_lvint_history(ump);
14709f988b79SJean-Baptiste Boric 		if (error)
14719f988b79SJean-Baptiste Boric 			return error;
14729f988b79SJean-Baptiste Boric 		goto again;
14739f988b79SJean-Baptiste Boric 	}
14749f988b79SJean-Baptiste Boric 
14759f988b79SJean-Baptiste Boric 	/* update our integrity descriptor to identify us and timestamp it */
14769f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("updating integrity descriptor\n"));
14779f988b79SJean-Baptiste Boric 	microtime(&now_v);
14789f988b79SJean-Baptiste Boric 	TIMEVAL_TO_TIMESPEC(&now_v, &now_s);
14799f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now_s, &ump->logvol_integrity->time);
14809f988b79SJean-Baptiste Boric 	udf_set_regid(&ump->logvol_info->impl_id, IMPL_NAME);
14819f988b79SJean-Baptiste Boric 	udf_add_impl_regid(ump, &ump->logvol_info->impl_id);
14829f988b79SJean-Baptiste Boric 
14839f988b79SJean-Baptiste Boric 	/* writeout integrity descriptor */
14849f988b79SJean-Baptiste Boric 	sector = trace->start + trace->wpos;
14859f988b79SJean-Baptiste Boric 	error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
14869f988b79SJean-Baptiste Boric 			(union dscrptr *) ump->logvol_integrity,
14879f988b79SJean-Baptiste Boric 			sector, sector);
14889f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("writeout lvint : error = %d\n", error));
14899f988b79SJean-Baptiste Boric 	if (error)
14909f988b79SJean-Baptiste Boric 		return error;
14919f988b79SJean-Baptiste Boric 
14929f988b79SJean-Baptiste Boric 	/* advance write position */
14939f988b79SJean-Baptiste Boric 	trace->wpos++; space--;
14949f988b79SJean-Baptiste Boric 	if (space >= 1) {
14959f988b79SJean-Baptiste Boric 		/* append terminator */
14969f988b79SJean-Baptiste Boric 		sector = trace->start + trace->wpos;
14979f988b79SJean-Baptiste Boric 		error = udf_write_terminator(ump, sector);
14989f988b79SJean-Baptiste Boric 
14999f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("write terminator : error = %d\n", error));
15009f988b79SJean-Baptiste Boric 	}
15019f988b79SJean-Baptiste Boric 
15029f988b79SJean-Baptiste Boric 	space = (trace->end - trace->start) - trace->wpos;
15039f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, "
15049f988b79SJean-Baptiste Boric 		"space = %d\n", trace->start, trace->end, trace->pos,
15059f988b79SJean-Baptiste Boric 		trace->wpos, space));
15069f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("finished writing out logvol integrity descriptor "
15079f988b79SJean-Baptiste Boric 		"successfull\n"));
15089f988b79SJean-Baptiste Boric 
15099f988b79SJean-Baptiste Boric 	return error;
15109f988b79SJean-Baptiste Boric }
15119f988b79SJean-Baptiste Boric 
15129f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
15139f988b79SJean-Baptiste Boric 
15149f988b79SJean-Baptiste Boric static int
udf_read_physical_partition_spacetables(struct udf_mount * ump)15159f988b79SJean-Baptiste Boric udf_read_physical_partition_spacetables(struct udf_mount *ump)
15169f988b79SJean-Baptiste Boric {
15179f988b79SJean-Baptiste Boric 	union dscrptr        *dscr;
15189f988b79SJean-Baptiste Boric 	/* struct udf_args *args = &ump->mount_args; */
15199f988b79SJean-Baptiste Boric 	struct part_desc     *partd;
15209f988b79SJean-Baptiste Boric 	struct part_hdr_desc *parthdr;
15219f988b79SJean-Baptiste Boric 	struct udf_bitmap    *bitmap;
15229f988b79SJean-Baptiste Boric 	uint32_t phys_part;
15239f988b79SJean-Baptiste Boric 	uint32_t lb_num, len;
15249f988b79SJean-Baptiste Boric 	int error, dscr_type;
15259f988b79SJean-Baptiste Boric 
15269f988b79SJean-Baptiste Boric 	/* unallocated space map */
15279f988b79SJean-Baptiste Boric 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
15289f988b79SJean-Baptiste Boric 		partd = ump->partitions[phys_part];
15299f988b79SJean-Baptiste Boric 		if (partd == NULL)
15309f988b79SJean-Baptiste Boric 			continue;
15319f988b79SJean-Baptiste Boric 		parthdr = &partd->_impl_use.part_hdr;
15329f988b79SJean-Baptiste Boric 
15339f988b79SJean-Baptiste Boric 		lb_num  = udf_rw32(partd->start_loc);
15349f988b79SJean-Baptiste Boric 		lb_num += udf_rw32(parthdr->unalloc_space_bitmap.lb_num);
15359f988b79SJean-Baptiste Boric 		len     = udf_rw32(parthdr->unalloc_space_bitmap.len);
15369f988b79SJean-Baptiste Boric 		if (len == 0)
15379f988b79SJean-Baptiste Boric 			continue;
15389f988b79SJean-Baptiste Boric 
15399f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num));
15409f988b79SJean-Baptiste Boric 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
15419f988b79SJean-Baptiste Boric 		if (!error && dscr) {
15429f988b79SJean-Baptiste Boric 			/* analyse */
15439f988b79SJean-Baptiste Boric 			dscr_type = udf_rw16(dscr->tag.id);
15449f988b79SJean-Baptiste Boric 			if (dscr_type == TAGID_SPACE_BITMAP) {
15459f988b79SJean-Baptiste Boric 				DPRINTF(VOLUMES, ("Accepting space bitmap\n"));
15469f988b79SJean-Baptiste Boric 				ump->part_unalloc_dscr[phys_part] = &dscr->sbd;
15479f988b79SJean-Baptiste Boric 
15489f988b79SJean-Baptiste Boric 				/* fill in ump->part_unalloc_bits */
15499f988b79SJean-Baptiste Boric 				bitmap = &ump->part_unalloc_bits[phys_part];
15509f988b79SJean-Baptiste Boric 				bitmap->blob  = (uint8_t *) dscr;
15519f988b79SJean-Baptiste Boric 				bitmap->bits  = dscr->sbd.data;
15529f988b79SJean-Baptiste Boric 				bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
15539f988b79SJean-Baptiste Boric 				bitmap->pages = NULL;	/* TODO */
15549f988b79SJean-Baptiste Boric 				bitmap->data_pos     = 0;
15559f988b79SJean-Baptiste Boric 				bitmap->metadata_pos = 0;
15569f988b79SJean-Baptiste Boric 			} else {
15579f988b79SJean-Baptiste Boric 				free(dscr, M_UDFVOLD);
15589f988b79SJean-Baptiste Boric 
15599f988b79SJean-Baptiste Boric 				printf( "UDF mount: error reading unallocated "
15609f988b79SJean-Baptiste Boric 					"space bitmap\n");
15619f988b79SJean-Baptiste Boric 				return EROFS;
15629f988b79SJean-Baptiste Boric 			}
15639f988b79SJean-Baptiste Boric 		} else {
15649f988b79SJean-Baptiste Boric 			/* blank not allowed */
15659f988b79SJean-Baptiste Boric 			printf("UDF mount: blank unallocated space bitmap\n");
15669f988b79SJean-Baptiste Boric 			return EROFS;
15679f988b79SJean-Baptiste Boric 		}
15689f988b79SJean-Baptiste Boric 	}
15699f988b79SJean-Baptiste Boric 
15709f988b79SJean-Baptiste Boric 	/* unallocated space table (not supported) */
15719f988b79SJean-Baptiste Boric 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
15729f988b79SJean-Baptiste Boric 		partd = ump->partitions[phys_part];
15739f988b79SJean-Baptiste Boric 		if (partd == NULL)
15749f988b79SJean-Baptiste Boric 			continue;
15759f988b79SJean-Baptiste Boric 		parthdr = &partd->_impl_use.part_hdr;
15769f988b79SJean-Baptiste Boric 
15779f988b79SJean-Baptiste Boric 		len     = udf_rw32(parthdr->unalloc_space_table.len);
15789f988b79SJean-Baptiste Boric 		if (len) {
15799f988b79SJean-Baptiste Boric 			printf("UDF mount: space tables not supported\n");
15809f988b79SJean-Baptiste Boric 			return EROFS;
15819f988b79SJean-Baptiste Boric 		}
15829f988b79SJean-Baptiste Boric 	}
15839f988b79SJean-Baptiste Boric 
15849f988b79SJean-Baptiste Boric 	/* freed space map */
15859f988b79SJean-Baptiste Boric 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
15869f988b79SJean-Baptiste Boric 		partd = ump->partitions[phys_part];
15879f988b79SJean-Baptiste Boric 		if (partd == NULL)
15889f988b79SJean-Baptiste Boric 			continue;
15899f988b79SJean-Baptiste Boric 		parthdr = &partd->_impl_use.part_hdr;
15909f988b79SJean-Baptiste Boric 
15919f988b79SJean-Baptiste Boric 		/* freed space map */
15929f988b79SJean-Baptiste Boric 		lb_num  = udf_rw32(partd->start_loc);
15939f988b79SJean-Baptiste Boric 		lb_num += udf_rw32(parthdr->freed_space_bitmap.lb_num);
15949f988b79SJean-Baptiste Boric 		len     = udf_rw32(parthdr->freed_space_bitmap.len);
15959f988b79SJean-Baptiste Boric 		if (len == 0)
15969f988b79SJean-Baptiste Boric 			continue;
15979f988b79SJean-Baptiste Boric 
15989f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num));
15999f988b79SJean-Baptiste Boric 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
16009f988b79SJean-Baptiste Boric 		if (!error && dscr) {
16019f988b79SJean-Baptiste Boric 			/* analyse */
16029f988b79SJean-Baptiste Boric 			dscr_type = udf_rw16(dscr->tag.id);
16039f988b79SJean-Baptiste Boric 			if (dscr_type == TAGID_SPACE_BITMAP) {
16049f988b79SJean-Baptiste Boric 				DPRINTF(VOLUMES, ("Accepting space bitmap\n"));
16059f988b79SJean-Baptiste Boric 				ump->part_freed_dscr[phys_part] = &dscr->sbd;
16069f988b79SJean-Baptiste Boric 
16079f988b79SJean-Baptiste Boric 				/* fill in ump->part_freed_bits */
16089f988b79SJean-Baptiste Boric 				bitmap = &ump->part_unalloc_bits[phys_part];
16099f988b79SJean-Baptiste Boric 				bitmap->blob  = (uint8_t *) dscr;
16109f988b79SJean-Baptiste Boric 				bitmap->bits  = dscr->sbd.data;
16119f988b79SJean-Baptiste Boric 				bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
16129f988b79SJean-Baptiste Boric 				bitmap->pages = NULL;	/* TODO */
16139f988b79SJean-Baptiste Boric 				bitmap->data_pos     = 0;
16149f988b79SJean-Baptiste Boric 				bitmap->metadata_pos = 0;
16159f988b79SJean-Baptiste Boric 			} else {
16169f988b79SJean-Baptiste Boric 				free(dscr, M_UDFVOLD);
16179f988b79SJean-Baptiste Boric 
16189f988b79SJean-Baptiste Boric 				printf( "UDF mount: error reading freed  "
16199f988b79SJean-Baptiste Boric 					"space bitmap\n");
16209f988b79SJean-Baptiste Boric 				return EROFS;
16219f988b79SJean-Baptiste Boric 			}
16229f988b79SJean-Baptiste Boric 		} else {
16239f988b79SJean-Baptiste Boric 			/* blank not allowed */
16249f988b79SJean-Baptiste Boric 			printf("UDF mount: blank freed space bitmap\n");
16259f988b79SJean-Baptiste Boric 			return EROFS;
16269f988b79SJean-Baptiste Boric 		}
16279f988b79SJean-Baptiste Boric 	}
16289f988b79SJean-Baptiste Boric 
16299f988b79SJean-Baptiste Boric 	/* freed space table (not supported) */
16309f988b79SJean-Baptiste Boric 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
16319f988b79SJean-Baptiste Boric 		partd = ump->partitions[phys_part];
16329f988b79SJean-Baptiste Boric 		if (partd == NULL)
16339f988b79SJean-Baptiste Boric 			continue;
16349f988b79SJean-Baptiste Boric 		parthdr = &partd->_impl_use.part_hdr;
16359f988b79SJean-Baptiste Boric 
16369f988b79SJean-Baptiste Boric 		len     = udf_rw32(parthdr->freed_space_table.len);
16379f988b79SJean-Baptiste Boric 		if (len) {
16389f988b79SJean-Baptiste Boric 			printf("UDF mount: space tables not supported\n");
16399f988b79SJean-Baptiste Boric 			return EROFS;
16409f988b79SJean-Baptiste Boric 		}
16419f988b79SJean-Baptiste Boric 	}
16429f988b79SJean-Baptiste Boric 
16439f988b79SJean-Baptiste Boric 	return 0;
16449f988b79SJean-Baptiste Boric }
16459f988b79SJean-Baptiste Boric 
16469f988b79SJean-Baptiste Boric 
16479f988b79SJean-Baptiste Boric /* TODO implement async writeout */
16489f988b79SJean-Baptiste Boric int
udf_write_physical_partition_spacetables(struct udf_mount * ump,int waitfor)16499f988b79SJean-Baptiste Boric udf_write_physical_partition_spacetables(struct udf_mount *ump, int waitfor)
16509f988b79SJean-Baptiste Boric {
16519f988b79SJean-Baptiste Boric 	union dscrptr        *dscr;
16529f988b79SJean-Baptiste Boric 	/* struct udf_args *args = &ump->mount_args; */
16539f988b79SJean-Baptiste Boric 	struct part_desc     *partd;
16549f988b79SJean-Baptiste Boric 	struct part_hdr_desc *parthdr;
16559f988b79SJean-Baptiste Boric 	uint32_t phys_part;
16569f988b79SJean-Baptiste Boric 	uint32_t lb_num, len, ptov;
16579f988b79SJean-Baptiste Boric 	int error_all, error;
16589f988b79SJean-Baptiste Boric 
16599f988b79SJean-Baptiste Boric 	error_all = 0;
16609f988b79SJean-Baptiste Boric 	/* unallocated space map */
16619f988b79SJean-Baptiste Boric 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
16629f988b79SJean-Baptiste Boric 		partd = ump->partitions[phys_part];
16639f988b79SJean-Baptiste Boric 		if (partd == NULL)
16649f988b79SJean-Baptiste Boric 			continue;
16659f988b79SJean-Baptiste Boric 		parthdr = &partd->_impl_use.part_hdr;
16669f988b79SJean-Baptiste Boric 
16679f988b79SJean-Baptiste Boric 		ptov   = udf_rw32(partd->start_loc);
16689f988b79SJean-Baptiste Boric 		lb_num = udf_rw32(parthdr->unalloc_space_bitmap.lb_num);
16699f988b79SJean-Baptiste Boric 		len    = udf_rw32(parthdr->unalloc_space_bitmap.len);
16709f988b79SJean-Baptiste Boric 		if (len == 0)
16719f988b79SJean-Baptiste Boric 			continue;
16729f988b79SJean-Baptiste Boric 
16739f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Write unalloc. space bitmap %d\n",
16749f988b79SJean-Baptiste Boric 			lb_num + ptov));
16759f988b79SJean-Baptiste Boric 		dscr = (union dscrptr *) ump->part_unalloc_dscr[phys_part];
16769f988b79SJean-Baptiste Boric 		error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
16779f988b79SJean-Baptiste Boric 				(union dscrptr *) dscr,
16789f988b79SJean-Baptiste Boric 				ptov + lb_num, lb_num);
16799f988b79SJean-Baptiste Boric 		if (error) {
16809f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error));
16819f988b79SJean-Baptiste Boric 			error_all = error;
16829f988b79SJean-Baptiste Boric 		}
16839f988b79SJean-Baptiste Boric 	}
16849f988b79SJean-Baptiste Boric 
16859f988b79SJean-Baptiste Boric 	/* freed space map */
16869f988b79SJean-Baptiste Boric 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
16879f988b79SJean-Baptiste Boric 		partd = ump->partitions[phys_part];
16889f988b79SJean-Baptiste Boric 		if (partd == NULL)
16899f988b79SJean-Baptiste Boric 			continue;
16909f988b79SJean-Baptiste Boric 		parthdr = &partd->_impl_use.part_hdr;
16919f988b79SJean-Baptiste Boric 
16929f988b79SJean-Baptiste Boric 		/* freed space map */
16939f988b79SJean-Baptiste Boric 		ptov   = udf_rw32(partd->start_loc);
16949f988b79SJean-Baptiste Boric 		lb_num = udf_rw32(parthdr->freed_space_bitmap.lb_num);
16959f988b79SJean-Baptiste Boric 		len    = udf_rw32(parthdr->freed_space_bitmap.len);
16969f988b79SJean-Baptiste Boric 		if (len == 0)
16979f988b79SJean-Baptiste Boric 			continue;
16989f988b79SJean-Baptiste Boric 
16999f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Write freed space bitmap %d\n",
17009f988b79SJean-Baptiste Boric 			lb_num + ptov));
17019f988b79SJean-Baptiste Boric 		dscr = (union dscrptr *) ump->part_freed_dscr[phys_part];
17029f988b79SJean-Baptiste Boric 		error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
17039f988b79SJean-Baptiste Boric 				(union dscrptr *) dscr,
17049f988b79SJean-Baptiste Boric 				ptov + lb_num, lb_num);
17059f988b79SJean-Baptiste Boric 		if (error) {
17069f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error));
17079f988b79SJean-Baptiste Boric 			error_all = error;
17089f988b79SJean-Baptiste Boric 		}
17099f988b79SJean-Baptiste Boric 	}
17109f988b79SJean-Baptiste Boric 
17119f988b79SJean-Baptiste Boric 	return error_all;
17129f988b79SJean-Baptiste Boric }
17139f988b79SJean-Baptiste Boric 
17149f988b79SJean-Baptiste Boric 
17159f988b79SJean-Baptiste Boric static int
udf_read_metadata_partition_spacetable(struct udf_mount * ump)17169f988b79SJean-Baptiste Boric udf_read_metadata_partition_spacetable(struct udf_mount *ump)
17179f988b79SJean-Baptiste Boric {
17189f988b79SJean-Baptiste Boric 	struct udf_node	     *bitmap_node;
17199f988b79SJean-Baptiste Boric 	union dscrptr        *dscr;
17209f988b79SJean-Baptiste Boric 	struct udf_bitmap    *bitmap;
17219f988b79SJean-Baptiste Boric 	uint64_t inflen;
17229f988b79SJean-Baptiste Boric 	int error, dscr_type;
17239f988b79SJean-Baptiste Boric 
17249f988b79SJean-Baptiste Boric 	bitmap_node = ump->metadatabitmap_node;
17259f988b79SJean-Baptiste Boric 
17269f988b79SJean-Baptiste Boric 	/* only read in when metadata bitmap node is read in */
17279f988b79SJean-Baptiste Boric 	if (bitmap_node == NULL)
17289f988b79SJean-Baptiste Boric 		return 0;
17299f988b79SJean-Baptiste Boric 
17309f988b79SJean-Baptiste Boric 	if (bitmap_node->fe) {
17319f988b79SJean-Baptiste Boric 		inflen = udf_rw64(bitmap_node->fe->inf_len);
17329f988b79SJean-Baptiste Boric 	} else {
17339f988b79SJean-Baptiste Boric 		KASSERT(bitmap_node->efe);
17349f988b79SJean-Baptiste Boric 		inflen = udf_rw64(bitmap_node->efe->inf_len);
17359f988b79SJean-Baptiste Boric 	}
17369f988b79SJean-Baptiste Boric 
17379f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Reading metadata space bitmap for "
17389f988b79SJean-Baptiste Boric 		"%"PRIu64" bytes\n", inflen));
17399f988b79SJean-Baptiste Boric 
17409f988b79SJean-Baptiste Boric 	/* allocate space for bitmap */
17419f988b79SJean-Baptiste Boric 	dscr = malloc(inflen, M_UDFVOLD, M_CANFAIL | M_WAITOK);
17429f988b79SJean-Baptiste Boric 	if (!dscr)
17439f988b79SJean-Baptiste Boric 		return ENOMEM;
17449f988b79SJean-Baptiste Boric 
17459f988b79SJean-Baptiste Boric 	/* set vnode type to regular file or we can't read from it! */
17469f988b79SJean-Baptiste Boric 	bitmap_node->vnode->v_type = VREG;
17479f988b79SJean-Baptiste Boric 
17489f988b79SJean-Baptiste Boric 	/* read in complete metadata bitmap file */
17499f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_READ, bitmap_node->vnode,
17509f988b79SJean-Baptiste Boric 			dscr,
17519f988b79SJean-Baptiste Boric 			inflen, 0,
17529f988b79SJean-Baptiste Boric 			UIO_SYSSPACE,
17539f988b79SJean-Baptiste Boric 			IO_SYNC | IO_ALTSEMANTICS, FSCRED,
17549f988b79SJean-Baptiste Boric 			NULL, NULL);
17559f988b79SJean-Baptiste Boric 	if (error) {
17569f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Error reading metadata space bitmap\n"));
17579f988b79SJean-Baptiste Boric 		goto errorout;
17589f988b79SJean-Baptiste Boric 	}
17599f988b79SJean-Baptiste Boric 
17609f988b79SJean-Baptiste Boric 	/* analyse */
17619f988b79SJean-Baptiste Boric 	dscr_type = udf_rw16(dscr->tag.id);
17629f988b79SJean-Baptiste Boric 	if (dscr_type == TAGID_SPACE_BITMAP) {
17639f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Accepting metadata space bitmap\n"));
17649f988b79SJean-Baptiste Boric 		ump->metadata_unalloc_dscr = &dscr->sbd;
17659f988b79SJean-Baptiste Boric 
17669f988b79SJean-Baptiste Boric 		/* fill in bitmap bits */
17679f988b79SJean-Baptiste Boric 		bitmap = &ump->metadata_unalloc_bits;
17689f988b79SJean-Baptiste Boric 		bitmap->blob  = (uint8_t *) dscr;
17699f988b79SJean-Baptiste Boric 		bitmap->bits  = dscr->sbd.data;
17709f988b79SJean-Baptiste Boric 		bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
17719f988b79SJean-Baptiste Boric 		bitmap->pages = NULL;	/* TODO */
17729f988b79SJean-Baptiste Boric 		bitmap->data_pos     = 0;
17739f988b79SJean-Baptiste Boric 		bitmap->metadata_pos = 0;
17749f988b79SJean-Baptiste Boric 	} else {
17759f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("No valid bitmap found!\n"));
17769f988b79SJean-Baptiste Boric 		goto errorout;
17779f988b79SJean-Baptiste Boric 	}
17789f988b79SJean-Baptiste Boric 
17799f988b79SJean-Baptiste Boric 	return 0;
17809f988b79SJean-Baptiste Boric 
17819f988b79SJean-Baptiste Boric errorout:
17829f988b79SJean-Baptiste Boric 	free(dscr, M_UDFVOLD);
17839f988b79SJean-Baptiste Boric 	printf( "UDF mount: error reading unallocated "
17849f988b79SJean-Baptiste Boric 		"space bitmap for metadata partition\n");
17859f988b79SJean-Baptiste Boric 	return EROFS;
17869f988b79SJean-Baptiste Boric }
17879f988b79SJean-Baptiste Boric 
17889f988b79SJean-Baptiste Boric 
17899f988b79SJean-Baptiste Boric int
udf_write_metadata_partition_spacetable(struct udf_mount * ump,int waitfor)17909f988b79SJean-Baptiste Boric udf_write_metadata_partition_spacetable(struct udf_mount *ump, int waitfor)
17919f988b79SJean-Baptiste Boric {
17929f988b79SJean-Baptiste Boric 	struct udf_node	     *bitmap_node;
17939f988b79SJean-Baptiste Boric 	union dscrptr        *dscr;
17949f988b79SJean-Baptiste Boric 	uint64_t new_inflen;
17959f988b79SJean-Baptiste Boric 	int dummy, error;
17969f988b79SJean-Baptiste Boric 
17979f988b79SJean-Baptiste Boric 	bitmap_node = ump->metadatabitmap_node;
17989f988b79SJean-Baptiste Boric 
17999f988b79SJean-Baptiste Boric 	/* only write out when metadata bitmap node is known */
18009f988b79SJean-Baptiste Boric 	if (bitmap_node == NULL)
18019f988b79SJean-Baptiste Boric 		return 0;
18029f988b79SJean-Baptiste Boric 
18039f988b79SJean-Baptiste Boric 	if (!bitmap_node->fe) {
18049f988b79SJean-Baptiste Boric 		KASSERT(bitmap_node->efe);
18059f988b79SJean-Baptiste Boric 	}
18069f988b79SJean-Baptiste Boric 
18079f988b79SJean-Baptiste Boric 	/* reduce length to zero */
18089f988b79SJean-Baptiste Boric 	dscr = (union dscrptr *) ump->metadata_unalloc_dscr;
18099f988b79SJean-Baptiste Boric 	new_inflen = udf_tagsize(dscr, 1);
18109f988b79SJean-Baptiste Boric 
1811*0a6a1f1dSLionel Sambuc 	DPRINTF(VOLUMES, ("Resize and write out metadata space bitmap "
1812*0a6a1f1dSLionel Sambuc 		" for %"PRIu64" bytes\n", new_inflen));
18139f988b79SJean-Baptiste Boric 
18149f988b79SJean-Baptiste Boric 	error = udf_resize_node(bitmap_node, new_inflen, &dummy);
18159f988b79SJean-Baptiste Boric 	if (error)
18169f988b79SJean-Baptiste Boric 		printf("Error resizing metadata space bitmap\n");
18179f988b79SJean-Baptiste Boric 
18189f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_WRITE, bitmap_node->vnode,
18199f988b79SJean-Baptiste Boric 			dscr,
18209f988b79SJean-Baptiste Boric 			new_inflen, 0,
18219f988b79SJean-Baptiste Boric 			UIO_SYSSPACE,
18229f988b79SJean-Baptiste Boric 			IO_ALTSEMANTICS, FSCRED,
18239f988b79SJean-Baptiste Boric 			NULL, NULL);
18249f988b79SJean-Baptiste Boric 
18259f988b79SJean-Baptiste Boric 	bitmap_node->i_flags |= IN_MODIFIED;
18269f988b79SJean-Baptiste Boric 	error = vflushbuf(bitmap_node->vnode, FSYNC_WAIT);
18279f988b79SJean-Baptiste Boric 	if (error == 0)
18289f988b79SJean-Baptiste Boric 		error = VOP_FSYNC(bitmap_node->vnode,
18299f988b79SJean-Baptiste Boric 				FSCRED, FSYNC_WAIT, 0, 0);
18309f988b79SJean-Baptiste Boric 
18319f988b79SJean-Baptiste Boric 	if (error)
18329f988b79SJean-Baptiste Boric 		printf( "Error writing out metadata partition unalloced "
18339f988b79SJean-Baptiste Boric 			"space bitmap!\n");
18349f988b79SJean-Baptiste Boric 
18359f988b79SJean-Baptiste Boric 	return error;
18369f988b79SJean-Baptiste Boric }
18379f988b79SJean-Baptiste Boric 
18389f988b79SJean-Baptiste Boric 
18399f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
18409f988b79SJean-Baptiste Boric 
18419f988b79SJean-Baptiste Boric /*
18429f988b79SJean-Baptiste Boric  * Checks if ump's vds information is correct and complete
18439f988b79SJean-Baptiste Boric  */
18449f988b79SJean-Baptiste Boric 
18459f988b79SJean-Baptiste Boric int
udf_process_vds(struct udf_mount * ump)18469f988b79SJean-Baptiste Boric udf_process_vds(struct udf_mount *ump) {
18479f988b79SJean-Baptiste Boric 	union udf_pmap *mapping;
18489f988b79SJean-Baptiste Boric 	/* struct udf_args *args = &ump->mount_args; */
18499f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvint;
18509f988b79SJean-Baptiste Boric 	struct udf_logvol_info *lvinfo;
18519f988b79SJean-Baptiste Boric 	uint32_t n_pm;
18529f988b79SJean-Baptiste Boric 	uint8_t *pmap_pos;
18539f988b79SJean-Baptiste Boric 	char *domain_name, *map_name;
18549f988b79SJean-Baptiste Boric 	const char *check_name;
18559f988b79SJean-Baptiste Boric 	char bits[128];
18569f988b79SJean-Baptiste Boric 	int pmap_stype, pmap_size;
18579f988b79SJean-Baptiste Boric 	int pmap_type, log_part, phys_part, raw_phys_part, maps_on;
18589f988b79SJean-Baptiste Boric 	int n_phys, n_virt, n_spar, n_meta;
18599f988b79SJean-Baptiste Boric 	int len;
18609f988b79SJean-Baptiste Boric 
18619f988b79SJean-Baptiste Boric 	if (ump == NULL)
18629f988b79SJean-Baptiste Boric 		return ENOENT;
18639f988b79SJean-Baptiste Boric 
18649f988b79SJean-Baptiste Boric 	/* we need at least an anchor (trivial, but for safety) */
18659f988b79SJean-Baptiste Boric 	if (ump->anchors[0] == NULL)
18669f988b79SJean-Baptiste Boric 		return EINVAL;
18679f988b79SJean-Baptiste Boric 
18689f988b79SJean-Baptiste Boric 	/* we need at least one primary and one logical volume descriptor */
18699f988b79SJean-Baptiste Boric 	if ((ump->primary_vol == NULL) || (ump->logical_vol) == NULL)
18709f988b79SJean-Baptiste Boric 		return EINVAL;
18719f988b79SJean-Baptiste Boric 
18729f988b79SJean-Baptiste Boric 	/* we need at least one partition descriptor */
18739f988b79SJean-Baptiste Boric 	if (ump->partitions[0] == NULL)
18749f988b79SJean-Baptiste Boric 		return EINVAL;
18759f988b79SJean-Baptiste Boric 
18769f988b79SJean-Baptiste Boric 	/* check logical volume sector size verses device sector size */
18779f988b79SJean-Baptiste Boric 	if (udf_rw32(ump->logical_vol->lb_size) != ump->discinfo.sector_size) {
18789f988b79SJean-Baptiste Boric 		printf("UDF mount: format violation, lb_size != sector size\n");
18799f988b79SJean-Baptiste Boric 		return EINVAL;
18809f988b79SJean-Baptiste Boric 	}
18819f988b79SJean-Baptiste Boric 
18829f988b79SJean-Baptiste Boric 	/* check domain name */
18839f988b79SJean-Baptiste Boric 	domain_name = ump->logical_vol->domain_id.id;
18849f988b79SJean-Baptiste Boric 	if (strncmp(domain_name, "*OSTA UDF Compliant", 20)) {
18859f988b79SJean-Baptiste Boric 		printf("mount_udf: disc not OSTA UDF Compliant, aborting\n");
18869f988b79SJean-Baptiste Boric 		return EINVAL;
18879f988b79SJean-Baptiste Boric 	}
18889f988b79SJean-Baptiste Boric 
18899f988b79SJean-Baptiste Boric 	/* retrieve logical volume integrity sequence */
18909f988b79SJean-Baptiste Boric 	(void)udf_retrieve_lvint(ump);
18919f988b79SJean-Baptiste Boric 
18929f988b79SJean-Baptiste Boric 	/*
18939f988b79SJean-Baptiste Boric 	 * We need at least one logvol integrity descriptor recorded.  Note
18949f988b79SJean-Baptiste Boric 	 * that its OK to have an open logical volume integrity here. The VAT
18959f988b79SJean-Baptiste Boric 	 * will close/update the integrity.
18969f988b79SJean-Baptiste Boric 	 */
18979f988b79SJean-Baptiste Boric 	if (ump->logvol_integrity == NULL)
18989f988b79SJean-Baptiste Boric 		return EINVAL;
18999f988b79SJean-Baptiste Boric 
19009f988b79SJean-Baptiste Boric 	/* process derived structures */
19019f988b79SJean-Baptiste Boric 	n_pm   = udf_rw32(ump->logical_vol->n_pm);   /* num partmaps         */
19029f988b79SJean-Baptiste Boric 	lvint  = ump->logvol_integrity;
19039f988b79SJean-Baptiste Boric 	lvinfo = (struct udf_logvol_info *) (&lvint->tables[2 * n_pm]);
19049f988b79SJean-Baptiste Boric 	ump->logvol_info = lvinfo;
19059f988b79SJean-Baptiste Boric 
19069f988b79SJean-Baptiste Boric 	/* TODO check udf versions? */
19079f988b79SJean-Baptiste Boric 
19089f988b79SJean-Baptiste Boric 	/*
19099f988b79SJean-Baptiste Boric 	 * check logvol mappings: effective virt->log partmap translation
19109f988b79SJean-Baptiste Boric 	 * check and recording of the mapping results. Saves expensive
19119f988b79SJean-Baptiste Boric 	 * strncmp() in tight places.
19129f988b79SJean-Baptiste Boric 	 */
19139f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("checking logvol mappings\n"));
19149f988b79SJean-Baptiste Boric 	n_pm = udf_rw32(ump->logical_vol->n_pm);   /* num partmaps         */
19159f988b79SJean-Baptiste Boric 	pmap_pos =  ump->logical_vol->maps;
19169f988b79SJean-Baptiste Boric 
19179f988b79SJean-Baptiste Boric 	if (n_pm > UDF_PMAPS) {
19189f988b79SJean-Baptiste Boric 		printf("UDF mount: too many mappings\n");
19199f988b79SJean-Baptiste Boric 		return EINVAL;
19209f988b79SJean-Baptiste Boric 	}
19219f988b79SJean-Baptiste Boric 
19229f988b79SJean-Baptiste Boric 	/* count types and set partition numbers */
19239f988b79SJean-Baptiste Boric 	ump->data_part = ump->node_part = ump->fids_part = 0;
19249f988b79SJean-Baptiste Boric 	n_phys = n_virt = n_spar = n_meta = 0;
19259f988b79SJean-Baptiste Boric 	for (log_part = 0; log_part < n_pm; log_part++) {
19269f988b79SJean-Baptiste Boric 		mapping = (union udf_pmap *) pmap_pos;
19279f988b79SJean-Baptiste Boric 		pmap_stype = pmap_pos[0];
19289f988b79SJean-Baptiste Boric 		pmap_size  = pmap_pos[1];
19299f988b79SJean-Baptiste Boric 		switch (pmap_stype) {
19309f988b79SJean-Baptiste Boric 		case 1:	/* physical mapping */
19319f988b79SJean-Baptiste Boric 			/* volseq    = udf_rw16(mapping->pm1.vol_seq_num); */
19329f988b79SJean-Baptiste Boric 			raw_phys_part = udf_rw16(mapping->pm1.part_num);
19339f988b79SJean-Baptiste Boric 			pmap_type = UDF_VTOP_TYPE_PHYS;
19349f988b79SJean-Baptiste Boric 			n_phys++;
19359f988b79SJean-Baptiste Boric 			ump->data_part = log_part;
19369f988b79SJean-Baptiste Boric 			ump->node_part = log_part;
19379f988b79SJean-Baptiste Boric 			ump->fids_part = log_part;
19389f988b79SJean-Baptiste Boric 			break;
19399f988b79SJean-Baptiste Boric 		case 2: /* virtual/sparable/meta mapping */
19409f988b79SJean-Baptiste Boric 			map_name  = mapping->pm2.part_id.id;
19419f988b79SJean-Baptiste Boric 			/* volseq  = udf_rw16(mapping->pm2.vol_seq_num); */
19429f988b79SJean-Baptiste Boric 			raw_phys_part = udf_rw16(mapping->pm2.part_num);
19439f988b79SJean-Baptiste Boric 			pmap_type = UDF_VTOP_TYPE_UNKNOWN;
19449f988b79SJean-Baptiste Boric 			len = UDF_REGID_ID_SIZE;
19459f988b79SJean-Baptiste Boric 
19469f988b79SJean-Baptiste Boric 			check_name = "*UDF Virtual Partition";
19479f988b79SJean-Baptiste Boric 			if (strncmp(map_name, check_name, len) == 0) {
19489f988b79SJean-Baptiste Boric 				pmap_type = UDF_VTOP_TYPE_VIRT;
19499f988b79SJean-Baptiste Boric 				n_virt++;
19509f988b79SJean-Baptiste Boric 				ump->node_part = log_part;
19519f988b79SJean-Baptiste Boric 				break;
19529f988b79SJean-Baptiste Boric 			}
19539f988b79SJean-Baptiste Boric 			check_name = "*UDF Sparable Partition";
19549f988b79SJean-Baptiste Boric 			if (strncmp(map_name, check_name, len) == 0) {
19559f988b79SJean-Baptiste Boric 				pmap_type = UDF_VTOP_TYPE_SPARABLE;
19569f988b79SJean-Baptiste Boric 				n_spar++;
19579f988b79SJean-Baptiste Boric 				ump->data_part = log_part;
19589f988b79SJean-Baptiste Boric 				ump->node_part = log_part;
19599f988b79SJean-Baptiste Boric 				ump->fids_part = log_part;
19609f988b79SJean-Baptiste Boric 				break;
19619f988b79SJean-Baptiste Boric 			}
19629f988b79SJean-Baptiste Boric 			check_name = "*UDF Metadata Partition";
19639f988b79SJean-Baptiste Boric 			if (strncmp(map_name, check_name, len) == 0) {
19649f988b79SJean-Baptiste Boric 				pmap_type = UDF_VTOP_TYPE_META;
19659f988b79SJean-Baptiste Boric 				n_meta++;
19669f988b79SJean-Baptiste Boric 				ump->node_part = log_part;
19679f988b79SJean-Baptiste Boric 				ump->fids_part = log_part;
19689f988b79SJean-Baptiste Boric 				break;
19699f988b79SJean-Baptiste Boric 			}
19709f988b79SJean-Baptiste Boric 			break;
19719f988b79SJean-Baptiste Boric 		default:
19729f988b79SJean-Baptiste Boric 			return EINVAL;
19739f988b79SJean-Baptiste Boric 		}
19749f988b79SJean-Baptiste Boric 
19759f988b79SJean-Baptiste Boric 		/*
19769f988b79SJean-Baptiste Boric 		 * BUGALERT: some rogue implementations use random physical
19779f988b79SJean-Baptiste Boric 		 * partition numbers to break other implementations so lookup
19789f988b79SJean-Baptiste Boric 		 * the number.
19799f988b79SJean-Baptiste Boric 		 */
19809f988b79SJean-Baptiste Boric 		phys_part = udf_find_raw_phys(ump, raw_phys_part);
19819f988b79SJean-Baptiste Boric 
19829f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("\t%d -> %d(%d) type %d\n", log_part,
19839f988b79SJean-Baptiste Boric 		    raw_phys_part, phys_part, pmap_type));
19849f988b79SJean-Baptiste Boric 
19859f988b79SJean-Baptiste Boric 		if (phys_part == UDF_PARTITIONS)
19869f988b79SJean-Baptiste Boric 			return EINVAL;
19879f988b79SJean-Baptiste Boric 		if (pmap_type == UDF_VTOP_TYPE_UNKNOWN)
19889f988b79SJean-Baptiste Boric 			return EINVAL;
19899f988b79SJean-Baptiste Boric 
19909f988b79SJean-Baptiste Boric 		ump->vtop   [log_part] = phys_part;
19919f988b79SJean-Baptiste Boric 		ump->vtop_tp[log_part] = pmap_type;
19929f988b79SJean-Baptiste Boric 
19939f988b79SJean-Baptiste Boric 		pmap_pos += pmap_size;
19949f988b79SJean-Baptiste Boric 	}
19959f988b79SJean-Baptiste Boric 	/* not winning the beauty contest */
19969f988b79SJean-Baptiste Boric 	ump->vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW;
19979f988b79SJean-Baptiste Boric 
19989f988b79SJean-Baptiste Boric 	/* test some basic UDF assertions/requirements */
19999f988b79SJean-Baptiste Boric 	if ((n_virt > 1) || (n_spar > 1) || (n_meta > 1))
20009f988b79SJean-Baptiste Boric 		return EINVAL;
20019f988b79SJean-Baptiste Boric 
20029f988b79SJean-Baptiste Boric 	if (n_virt) {
20039f988b79SJean-Baptiste Boric 		if ((n_phys == 0) || n_spar || n_meta)
20049f988b79SJean-Baptiste Boric 			return EINVAL;
20059f988b79SJean-Baptiste Boric 	}
20069f988b79SJean-Baptiste Boric 	if (n_spar + n_phys == 0)
20079f988b79SJean-Baptiste Boric 		return EINVAL;
20089f988b79SJean-Baptiste Boric 
20099f988b79SJean-Baptiste Boric 	/* select allocation type for each logical partition */
20109f988b79SJean-Baptiste Boric 	for (log_part = 0; log_part < n_pm; log_part++) {
20119f988b79SJean-Baptiste Boric 		maps_on = ump->vtop[log_part];
20129f988b79SJean-Baptiste Boric 		switch (ump->vtop_tp[log_part]) {
20139f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_PHYS :
20149f988b79SJean-Baptiste Boric 			assert(maps_on == log_part);
20159f988b79SJean-Baptiste Boric 			ump->vtop_alloc[log_part] = UDF_ALLOC_SPACEMAP;
20169f988b79SJean-Baptiste Boric 			break;
20179f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_VIRT :
20189f988b79SJean-Baptiste Boric 			ump->vtop_alloc[log_part] = UDF_ALLOC_VAT;
20199f988b79SJean-Baptiste Boric 			ump->vtop_alloc[maps_on]  = UDF_ALLOC_SEQUENTIAL;
20209f988b79SJean-Baptiste Boric 			break;
20219f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_SPARABLE :
20229f988b79SJean-Baptiste Boric 			assert(maps_on == log_part);
20239f988b79SJean-Baptiste Boric 			ump->vtop_alloc[log_part] = UDF_ALLOC_SPACEMAP;
20249f988b79SJean-Baptiste Boric 			break;
20259f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_META :
20269f988b79SJean-Baptiste Boric 			ump->vtop_alloc[log_part] = UDF_ALLOC_METABITMAP;
20279f988b79SJean-Baptiste Boric 			if (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) {
20289f988b79SJean-Baptiste Boric 				/* special case for UDF 2.60 */
20299f988b79SJean-Baptiste Boric 				ump->vtop_alloc[log_part] = UDF_ALLOC_METASEQUENTIAL;
20309f988b79SJean-Baptiste Boric 				ump->vtop_alloc[maps_on]  = UDF_ALLOC_SEQUENTIAL;
20319f988b79SJean-Baptiste Boric 			}
20329f988b79SJean-Baptiste Boric 			break;
20339f988b79SJean-Baptiste Boric 		default:
20349f988b79SJean-Baptiste Boric 			panic("bad alloction type in udf's ump->vtop\n");
20359f988b79SJean-Baptiste Boric 		}
20369f988b79SJean-Baptiste Boric 	}
20379f988b79SJean-Baptiste Boric 
20389f988b79SJean-Baptiste Boric 	/* determine logical volume open/closure actions */
20399f988b79SJean-Baptiste Boric 	if (n_virt) {
20409f988b79SJean-Baptiste Boric 		ump->lvopen  = 0;
20419f988b79SJean-Baptiste Boric 		if (ump->discinfo.last_session_state == MMC_STATE_EMPTY)
20429f988b79SJean-Baptiste Boric 			ump->lvopen |= UDF_OPEN_SESSION ;
20439f988b79SJean-Baptiste Boric 		ump->lvclose = UDF_WRITE_VAT;
20449f988b79SJean-Baptiste Boric 		if (ump->mount_args.udfmflags & UDFMNT_CLOSESESSION)
20459f988b79SJean-Baptiste Boric 			ump->lvclose |= UDF_CLOSE_SESSION;
20469f988b79SJean-Baptiste Boric 	} else {
20479f988b79SJean-Baptiste Boric 		/* `normal' rewritable or non sequential media */
20489f988b79SJean-Baptiste Boric 		ump->lvopen  = UDF_WRITE_LVINT;
20499f988b79SJean-Baptiste Boric 		ump->lvclose = UDF_WRITE_LVINT;
20509f988b79SJean-Baptiste Boric 		if ((ump->discinfo.mmc_cur & MMC_CAP_REWRITABLE) == 0)
20519f988b79SJean-Baptiste Boric 			ump->lvopen  |=  UDF_APPENDONLY_LVINT;
20529f988b79SJean-Baptiste Boric 		if ((ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE))
20539f988b79SJean-Baptiste Boric 			ump->lvopen  &= ~UDF_APPENDONLY_LVINT;
20549f988b79SJean-Baptiste Boric 	}
20559f988b79SJean-Baptiste Boric 
20569f988b79SJean-Baptiste Boric 	/*
20579f988b79SJean-Baptiste Boric 	 * Determine sheduler error behaviour. For virtual partitions, update
20589f988b79SJean-Baptiste Boric 	 * the trackinfo; for sparable partitions replace a whole block on the
20599f988b79SJean-Baptiste Boric 	 * sparable table. Allways requeue.
20609f988b79SJean-Baptiste Boric 	 */
20619f988b79SJean-Baptiste Boric 	ump->lvreadwrite = 0;
20629f988b79SJean-Baptiste Boric 	if (n_virt)
20639f988b79SJean-Baptiste Boric 		ump->lvreadwrite = UDF_UPDATE_TRACKINFO;
20649f988b79SJean-Baptiste Boric 	if (n_spar)
20659f988b79SJean-Baptiste Boric 		ump->lvreadwrite = UDF_REMAP_BLOCK;
20669f988b79SJean-Baptiste Boric 
20679f988b79SJean-Baptiste Boric 	/*
20689f988b79SJean-Baptiste Boric 	 * Select our sheduler
20699f988b79SJean-Baptiste Boric 	 */
20709f988b79SJean-Baptiste Boric 	ump->strategy = &udf_strat_rmw;
20719f988b79SJean-Baptiste Boric 	if (n_virt || (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE))
20729f988b79SJean-Baptiste Boric 		ump->strategy = &udf_strat_sequential;
20739f988b79SJean-Baptiste Boric 	if ((ump->discinfo.mmc_class == MMC_CLASS_DISC) ||
20749f988b79SJean-Baptiste Boric 		(ump->discinfo.mmc_class == MMC_CLASS_UNKN))
20759f988b79SJean-Baptiste Boric 			ump->strategy = &udf_strat_direct;
20769f988b79SJean-Baptiste Boric 	if (n_spar)
20779f988b79SJean-Baptiste Boric 		ump->strategy = &udf_strat_rmw;
20789f988b79SJean-Baptiste Boric 
20799f988b79SJean-Baptiste Boric #if 0
20809f988b79SJean-Baptiste Boric 	/* read-only access won't benefit from the other shedulers */
20819f988b79SJean-Baptiste Boric 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
20829f988b79SJean-Baptiste Boric 		ump->strategy = &udf_strat_direct;
20839f988b79SJean-Baptiste Boric #endif
20849f988b79SJean-Baptiste Boric 
20859f988b79SJean-Baptiste Boric 	/* print results */
20869f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tdata partition    %d\n", ump->data_part));
20879f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->data_part]));
20889f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tnode partition    %d\n", ump->node_part));
20899f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->node_part]));
20909f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tfids partition    %d\n", ump->fids_part));
20919f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->fids_part]));
20929f988b79SJean-Baptiste Boric 
20939f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), UDFLOGVOL_BITS, ump->lvopen);
20949f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tactions on logvol open  %s\n", bits));
20959f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), UDFLOGVOL_BITS, ump->lvclose);
20969f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tactions on logvol close %s\n", bits));
20979f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), UDFONERROR_BITS, ump->lvreadwrite);
20989f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tactions on logvol errors %s\n", bits));
20999f988b79SJean-Baptiste Boric 
21009f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tselected sheduler `%s`\n",
21019f988b79SJean-Baptiste Boric 		(ump->strategy == &udf_strat_direct) ? "Direct" :
21029f988b79SJean-Baptiste Boric 		(ump->strategy == &udf_strat_sequential) ? "Sequential" :
21039f988b79SJean-Baptiste Boric 		(ump->strategy == &udf_strat_rmw) ? "RMW" : "UNKNOWN!"));
21049f988b79SJean-Baptiste Boric 
21059f988b79SJean-Baptiste Boric 	/* signal its OK for now */
21069f988b79SJean-Baptiste Boric 	return 0;
21079f988b79SJean-Baptiste Boric }
21089f988b79SJean-Baptiste Boric 
21099f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
21109f988b79SJean-Baptiste Boric 
21119f988b79SJean-Baptiste Boric /*
21129f988b79SJean-Baptiste Boric  * Update logical volume name in all structures that keep a record of it. We
21139f988b79SJean-Baptiste Boric  * use memmove since each of them might be specified as a source.
21149f988b79SJean-Baptiste Boric  *
21159f988b79SJean-Baptiste Boric  * Note that it doesn't update the VAT structure!
21169f988b79SJean-Baptiste Boric  */
21179f988b79SJean-Baptiste Boric 
21189f988b79SJean-Baptiste Boric static void
udf_update_logvolname(struct udf_mount * ump,char * logvol_id)21199f988b79SJean-Baptiste Boric udf_update_logvolname(struct udf_mount *ump, char *logvol_id)
21209f988b79SJean-Baptiste Boric {
21219f988b79SJean-Baptiste Boric 	struct logvol_desc     *lvd = NULL;
21229f988b79SJean-Baptiste Boric 	struct fileset_desc    *fsd = NULL;
21239f988b79SJean-Baptiste Boric 	struct udf_lv_info     *lvi = NULL;
21249f988b79SJean-Baptiste Boric 
21259f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Updating logical volume name\n"));
21269f988b79SJean-Baptiste Boric 	lvd = ump->logical_vol;
21279f988b79SJean-Baptiste Boric 	fsd = ump->fileset_desc;
21289f988b79SJean-Baptiste Boric 	if (ump->implementation)
21299f988b79SJean-Baptiste Boric 		lvi = &ump->implementation->_impl_use.lv_info;
21309f988b79SJean-Baptiste Boric 
21319f988b79SJean-Baptiste Boric 	/* logvol's id might be specified as origional so use memmove here */
21329f988b79SJean-Baptiste Boric 	memmove(lvd->logvol_id, logvol_id, 128);
21339f988b79SJean-Baptiste Boric 	if (fsd)
21349f988b79SJean-Baptiste Boric 		memmove(fsd->logvol_id, logvol_id, 128);
21359f988b79SJean-Baptiste Boric 	if (lvi)
21369f988b79SJean-Baptiste Boric 		memmove(lvi->logvol_id, logvol_id, 128);
21379f988b79SJean-Baptiste Boric }
21389f988b79SJean-Baptiste Boric 
21399f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
21409f988b79SJean-Baptiste Boric 
21419f988b79SJean-Baptiste Boric void
udf_inittag(struct udf_mount * ump,struct desc_tag * tag,int tagid,uint32_t sector)21429f988b79SJean-Baptiste Boric udf_inittag(struct udf_mount *ump, struct desc_tag *tag, int tagid,
21439f988b79SJean-Baptiste Boric 	uint32_t sector)
21449f988b79SJean-Baptiste Boric {
21459f988b79SJean-Baptiste Boric 	assert(ump->logical_vol);
21469f988b79SJean-Baptiste Boric 
21479f988b79SJean-Baptiste Boric 	tag->id 		= udf_rw16(tagid);
21489f988b79SJean-Baptiste Boric 	tag->descriptor_ver	= ump->logical_vol->tag.descriptor_ver;
21499f988b79SJean-Baptiste Boric 	tag->cksum		= 0;
21509f988b79SJean-Baptiste Boric 	tag->reserved		= 0;
21519f988b79SJean-Baptiste Boric 	tag->serial_num		= ump->logical_vol->tag.serial_num;
21529f988b79SJean-Baptiste Boric 	tag->tag_loc            = udf_rw32(sector);
21539f988b79SJean-Baptiste Boric }
21549f988b79SJean-Baptiste Boric 
21559f988b79SJean-Baptiste Boric 
21569f988b79SJean-Baptiste Boric uint64_t
udf_advance_uniqueid(struct udf_mount * ump)21579f988b79SJean-Baptiste Boric udf_advance_uniqueid(struct udf_mount *ump)
21589f988b79SJean-Baptiste Boric {
21599f988b79SJean-Baptiste Boric 	uint64_t unique_id;
21609f988b79SJean-Baptiste Boric 
21619f988b79SJean-Baptiste Boric 	mutex_enter(&ump->logvol_mutex);
21629f988b79SJean-Baptiste Boric 	unique_id = udf_rw64(ump->logvol_integrity->lvint_next_unique_id);
21639f988b79SJean-Baptiste Boric 	if (unique_id < 0x10)
21649f988b79SJean-Baptiste Boric 		unique_id = 0x10;
21659f988b79SJean-Baptiste Boric 	ump->logvol_integrity->lvint_next_unique_id = udf_rw64(unique_id + 1);
21669f988b79SJean-Baptiste Boric 	mutex_exit(&ump->logvol_mutex);
21679f988b79SJean-Baptiste Boric 
21689f988b79SJean-Baptiste Boric 	return unique_id;
21699f988b79SJean-Baptiste Boric }
21709f988b79SJean-Baptiste Boric 
21719f988b79SJean-Baptiste Boric 
21729f988b79SJean-Baptiste Boric static void
udf_adjust_filecount(struct udf_node * udf_node,int sign)21739f988b79SJean-Baptiste Boric udf_adjust_filecount(struct udf_node *udf_node, int sign)
21749f988b79SJean-Baptiste Boric {
21759f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
21769f988b79SJean-Baptiste Boric 	uint32_t num_dirs, num_files;
21779f988b79SJean-Baptiste Boric 	int udf_file_type;
21789f988b79SJean-Baptiste Boric 
21799f988b79SJean-Baptiste Boric 	/* get file type */
21809f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
21819f988b79SJean-Baptiste Boric 		udf_file_type = udf_node->fe->icbtag.file_type;
21829f988b79SJean-Baptiste Boric 	} else {
21839f988b79SJean-Baptiste Boric 		udf_file_type = udf_node->efe->icbtag.file_type;
21849f988b79SJean-Baptiste Boric 	}
21859f988b79SJean-Baptiste Boric 
21869f988b79SJean-Baptiste Boric 	/* adjust file count */
21879f988b79SJean-Baptiste Boric 	mutex_enter(&ump->allocate_mutex);
21889f988b79SJean-Baptiste Boric 	if (udf_file_type == UDF_ICB_FILETYPE_DIRECTORY) {
21899f988b79SJean-Baptiste Boric 		num_dirs = udf_rw32(ump->logvol_info->num_directories);
21909f988b79SJean-Baptiste Boric 		ump->logvol_info->num_directories =
21919f988b79SJean-Baptiste Boric 			udf_rw32((num_dirs + sign));
21929f988b79SJean-Baptiste Boric 	} else {
21939f988b79SJean-Baptiste Boric 		num_files = udf_rw32(ump->logvol_info->num_files);
21949f988b79SJean-Baptiste Boric 		ump->logvol_info->num_files =
21959f988b79SJean-Baptiste Boric 			udf_rw32((num_files + sign));
21969f988b79SJean-Baptiste Boric 	}
21979f988b79SJean-Baptiste Boric 	mutex_exit(&ump->allocate_mutex);
21989f988b79SJean-Baptiste Boric }
21999f988b79SJean-Baptiste Boric 
22009f988b79SJean-Baptiste Boric 
22019f988b79SJean-Baptiste Boric void
udf_osta_charset(struct charspec * charspec)22029f988b79SJean-Baptiste Boric udf_osta_charset(struct charspec *charspec)
22039f988b79SJean-Baptiste Boric {
22049f988b79SJean-Baptiste Boric 	memset(charspec, 0, sizeof(struct charspec));
22059f988b79SJean-Baptiste Boric 	charspec->type = 0;
22069f988b79SJean-Baptiste Boric 	strcpy((char *) charspec->inf, "OSTA Compressed Unicode");
22079f988b79SJean-Baptiste Boric }
22089f988b79SJean-Baptiste Boric 
22099f988b79SJean-Baptiste Boric 
22109f988b79SJean-Baptiste Boric /* first call udf_set_regid and then the suffix */
22119f988b79SJean-Baptiste Boric void
udf_set_regid(struct regid * regid,char const * name)22129f988b79SJean-Baptiste Boric udf_set_regid(struct regid *regid, char const *name)
22139f988b79SJean-Baptiste Boric {
22149f988b79SJean-Baptiste Boric 	memset(regid, 0, sizeof(struct regid));
22159f988b79SJean-Baptiste Boric 	regid->flags    = 0;		/* not dirty and not protected */
22169f988b79SJean-Baptiste Boric 	strcpy((char *) regid->id, name);
22179f988b79SJean-Baptiste Boric }
22189f988b79SJean-Baptiste Boric 
22199f988b79SJean-Baptiste Boric 
22209f988b79SJean-Baptiste Boric void
udf_add_domain_regid(struct udf_mount * ump,struct regid * regid)22219f988b79SJean-Baptiste Boric udf_add_domain_regid(struct udf_mount *ump, struct regid *regid)
22229f988b79SJean-Baptiste Boric {
22239f988b79SJean-Baptiste Boric 	uint16_t *ver;
22249f988b79SJean-Baptiste Boric 
22259f988b79SJean-Baptiste Boric 	ver  = (uint16_t *) regid->id_suffix;
22269f988b79SJean-Baptiste Boric 	*ver = ump->logvol_info->min_udf_readver;
22279f988b79SJean-Baptiste Boric }
22289f988b79SJean-Baptiste Boric 
22299f988b79SJean-Baptiste Boric 
22309f988b79SJean-Baptiste Boric void
udf_add_udf_regid(struct udf_mount * ump,struct regid * regid)22319f988b79SJean-Baptiste Boric udf_add_udf_regid(struct udf_mount *ump, struct regid *regid)
22329f988b79SJean-Baptiste Boric {
22339f988b79SJean-Baptiste Boric 	uint16_t *ver;
22349f988b79SJean-Baptiste Boric 
22359f988b79SJean-Baptiste Boric 	ver  = (uint16_t *) regid->id_suffix;
22369f988b79SJean-Baptiste Boric 	*ver = ump->logvol_info->min_udf_readver;
22379f988b79SJean-Baptiste Boric 
22389f988b79SJean-Baptiste Boric 	regid->id_suffix[2] = 4;	/* unix */
22399f988b79SJean-Baptiste Boric 	regid->id_suffix[3] = 8;	/* NetBSD */
22409f988b79SJean-Baptiste Boric }
22419f988b79SJean-Baptiste Boric 
22429f988b79SJean-Baptiste Boric 
22439f988b79SJean-Baptiste Boric void
udf_add_impl_regid(struct udf_mount * ump,struct regid * regid)22449f988b79SJean-Baptiste Boric udf_add_impl_regid(struct udf_mount *ump, struct regid *regid)
22459f988b79SJean-Baptiste Boric {
22469f988b79SJean-Baptiste Boric 	regid->id_suffix[0] = 4;	/* unix */
22479f988b79SJean-Baptiste Boric 	regid->id_suffix[1] = 8;	/* NetBSD */
22489f988b79SJean-Baptiste Boric }
22499f988b79SJean-Baptiste Boric 
22509f988b79SJean-Baptiste Boric 
22519f988b79SJean-Baptiste Boric void
udf_add_app_regid(struct udf_mount * ump,struct regid * regid)22529f988b79SJean-Baptiste Boric udf_add_app_regid(struct udf_mount *ump, struct regid *regid)
22539f988b79SJean-Baptiste Boric {
22549f988b79SJean-Baptiste Boric 	regid->id_suffix[0] = APP_VERSION_MAIN;
22559f988b79SJean-Baptiste Boric 	regid->id_suffix[1] = APP_VERSION_SUB;
22569f988b79SJean-Baptiste Boric }
22579f988b79SJean-Baptiste Boric 
22589f988b79SJean-Baptiste Boric static int
udf_create_parentfid(struct udf_mount * ump,struct fileid_desc * fid,struct long_ad * parent,uint64_t unique_id)22599f988b79SJean-Baptiste Boric udf_create_parentfid(struct udf_mount *ump, struct fileid_desc *fid,
22609f988b79SJean-Baptiste Boric 	struct long_ad *parent, uint64_t unique_id)
22619f988b79SJean-Baptiste Boric {
22629f988b79SJean-Baptiste Boric 	/* the size of an empty FID is 38 but needs to be a multiple of 4 */
22639f988b79SJean-Baptiste Boric 	int fidsize = 40;
22649f988b79SJean-Baptiste Boric 
22659f988b79SJean-Baptiste Boric 	udf_inittag(ump, &fid->tag, TAGID_FID, udf_rw32(parent->loc.lb_num));
22669f988b79SJean-Baptiste Boric 	fid->file_version_num = udf_rw16(1);	/* UDF 2.3.4.1 */
22679f988b79SJean-Baptiste Boric 	fid->file_char = UDF_FILE_CHAR_DIR | UDF_FILE_CHAR_PAR;
22689f988b79SJean-Baptiste Boric 	fid->icb = *parent;
22699f988b79SJean-Baptiste Boric 	fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
22709f988b79SJean-Baptiste Boric 	fid->tag.desc_crc_len = udf_rw16(fidsize - UDF_DESC_TAG_LENGTH);
22719f988b79SJean-Baptiste Boric 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
22729f988b79SJean-Baptiste Boric 
22739f988b79SJean-Baptiste Boric 	return fidsize;
22749f988b79SJean-Baptiste Boric }
22759f988b79SJean-Baptiste Boric 
22769f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
22779f988b79SJean-Baptiste Boric 
22789f988b79SJean-Baptiste Boric /*
22799f988b79SJean-Baptiste Boric  * Extended attribute support. UDF knows of 3 places for extended attributes:
22809f988b79SJean-Baptiste Boric  *
22819f988b79SJean-Baptiste Boric  * (a) inside the file's (e)fe in the length of the extended attribute area
22829f988b79SJean-Baptiste Boric  * before the allocation descriptors/filedata
22839f988b79SJean-Baptiste Boric  *
22849f988b79SJean-Baptiste Boric  * (b) in a file referenced by (e)fe->ext_attr_icb and
22859f988b79SJean-Baptiste Boric  *
22869f988b79SJean-Baptiste Boric  * (c) in the e(fe)'s associated stream directory that can hold various
22879f988b79SJean-Baptiste Boric  * sub-files. In the stream directory a few fixed named subfiles are reserved
22889f988b79SJean-Baptiste Boric  * for NT/Unix ACL's and OS/2 attributes.
22899f988b79SJean-Baptiste Boric  *
22909f988b79SJean-Baptiste Boric  * NOTE: Extended attributes are read randomly but allways written
22919f988b79SJean-Baptiste Boric  * *atomicaly*. For ACL's this interface is propably different but not known
22929f988b79SJean-Baptiste Boric  * to me yet.
22939f988b79SJean-Baptiste Boric  *
22949f988b79SJean-Baptiste Boric  * Order of extended attributes in a space :
22959f988b79SJean-Baptiste Boric  *   ECMA 167 EAs
22969f988b79SJean-Baptiste Boric  *   Non block aligned Implementation Use EAs
22979f988b79SJean-Baptiste Boric  *   Block aligned Implementation Use EAs
22989f988b79SJean-Baptiste Boric  *   Application Use EAs
22999f988b79SJean-Baptiste Boric  */
23009f988b79SJean-Baptiste Boric 
23019f988b79SJean-Baptiste Boric static int
udf_impl_extattr_check(struct impl_extattr_entry * implext)23029f988b79SJean-Baptiste Boric udf_impl_extattr_check(struct impl_extattr_entry *implext)
23039f988b79SJean-Baptiste Boric {
23049f988b79SJean-Baptiste Boric 	uint16_t   *spos;
23059f988b79SJean-Baptiste Boric 
23069f988b79SJean-Baptiste Boric 	if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
23079f988b79SJean-Baptiste Boric 		/* checksum valid? */
23089f988b79SJean-Baptiste Boric 		DPRINTF(EXTATTR, ("checking UDF impl. attr checksum\n"));
23099f988b79SJean-Baptiste Boric 		spos = (uint16_t *) implext->data;
23109f988b79SJean-Baptiste Boric 		if (udf_rw16(*spos) != udf_ea_cksum((uint8_t *) implext))
23119f988b79SJean-Baptiste Boric 			return EINVAL;
23129f988b79SJean-Baptiste Boric 	}
23139f988b79SJean-Baptiste Boric 	return 0;
23149f988b79SJean-Baptiste Boric }
23159f988b79SJean-Baptiste Boric 
23169f988b79SJean-Baptiste Boric static void
udf_calc_impl_extattr_checksum(struct impl_extattr_entry * implext)23179f988b79SJean-Baptiste Boric udf_calc_impl_extattr_checksum(struct impl_extattr_entry *implext)
23189f988b79SJean-Baptiste Boric {
23199f988b79SJean-Baptiste Boric 	uint16_t   *spos;
23209f988b79SJean-Baptiste Boric 
23219f988b79SJean-Baptiste Boric 	if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
23229f988b79SJean-Baptiste Boric 		/* set checksum */
23239f988b79SJean-Baptiste Boric 		spos = (uint16_t *) implext->data;
23249f988b79SJean-Baptiste Boric 		*spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
23259f988b79SJean-Baptiste Boric 	}
23269f988b79SJean-Baptiste Boric }
23279f988b79SJean-Baptiste Boric 
23289f988b79SJean-Baptiste Boric 
23299f988b79SJean-Baptiste Boric int
udf_extattr_search_intern(struct udf_node * node,uint32_t sattr,char const * sattrname,uint32_t * offsetp,uint32_t * lengthp)23309f988b79SJean-Baptiste Boric udf_extattr_search_intern(struct udf_node *node,
23319f988b79SJean-Baptiste Boric 	uint32_t sattr, char const *sattrname,
23329f988b79SJean-Baptiste Boric 	uint32_t *offsetp, uint32_t *lengthp)
23339f988b79SJean-Baptiste Boric {
23349f988b79SJean-Baptiste Boric 	struct extattrhdr_desc    *eahdr;
23359f988b79SJean-Baptiste Boric 	struct extattr_entry      *attrhdr;
23369f988b79SJean-Baptiste Boric 	struct impl_extattr_entry *implext;
23379f988b79SJean-Baptiste Boric 	uint32_t    offset, a_l, sector_size;
23389f988b79SJean-Baptiste Boric 	 int32_t    l_ea;
23399f988b79SJean-Baptiste Boric 	uint8_t    *pos;
23409f988b79SJean-Baptiste Boric 	int         error;
23419f988b79SJean-Baptiste Boric 
23429f988b79SJean-Baptiste Boric 	/* get mountpoint */
23439f988b79SJean-Baptiste Boric 	sector_size = node->ump->discinfo.sector_size;
23449f988b79SJean-Baptiste Boric 
23459f988b79SJean-Baptiste Boric 	/* get information from fe/efe */
23469f988b79SJean-Baptiste Boric 	if (node->fe) {
23479f988b79SJean-Baptiste Boric 		l_ea  = udf_rw32(node->fe->l_ea);
23489f988b79SJean-Baptiste Boric 		eahdr = (struct extattrhdr_desc *) node->fe->data;
23499f988b79SJean-Baptiste Boric 	} else {
23509f988b79SJean-Baptiste Boric 		assert(node->efe);
23519f988b79SJean-Baptiste Boric 		l_ea  = udf_rw32(node->efe->l_ea);
23529f988b79SJean-Baptiste Boric 		eahdr = (struct extattrhdr_desc *) node->efe->data;
23539f988b79SJean-Baptiste Boric 	}
23549f988b79SJean-Baptiste Boric 
23559f988b79SJean-Baptiste Boric 	/* something recorded here? */
23569f988b79SJean-Baptiste Boric 	if (l_ea == 0)
23579f988b79SJean-Baptiste Boric 		return ENOENT;
23589f988b79SJean-Baptiste Boric 
23599f988b79SJean-Baptiste Boric 	/* check extended attribute tag; what to do if it fails? */
23609f988b79SJean-Baptiste Boric 	error = udf_check_tag(eahdr);
23619f988b79SJean-Baptiste Boric 	if (error)
23629f988b79SJean-Baptiste Boric 		return EINVAL;
23639f988b79SJean-Baptiste Boric 	if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR)
23649f988b79SJean-Baptiste Boric 		return EINVAL;
23659f988b79SJean-Baptiste Boric 	error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc));
23669f988b79SJean-Baptiste Boric 	if (error)
23679f988b79SJean-Baptiste Boric 		return EINVAL;
23689f988b79SJean-Baptiste Boric 
23699f988b79SJean-Baptiste Boric 	DPRINTF(EXTATTR, ("Found %d bytes of extended attributes\n", l_ea));
23709f988b79SJean-Baptiste Boric 
23719f988b79SJean-Baptiste Boric 	/* looking for Ecma-167 attributes? */
23729f988b79SJean-Baptiste Boric 	offset = sizeof(struct extattrhdr_desc);
23739f988b79SJean-Baptiste Boric 
23749f988b79SJean-Baptiste Boric 	/* looking for either implemenation use or application use */
23759f988b79SJean-Baptiste Boric 	if (sattr == 2048) {				/* [4/48.10.8] */
23769f988b79SJean-Baptiste Boric 		offset = udf_rw32(eahdr->impl_attr_loc);
23779f988b79SJean-Baptiste Boric 		if (offset == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
23789f988b79SJean-Baptiste Boric 			return ENOENT;
23799f988b79SJean-Baptiste Boric 	}
23809f988b79SJean-Baptiste Boric 	if (sattr == 65536) {				/* [4/48.10.9] */
23819f988b79SJean-Baptiste Boric 		offset = udf_rw32(eahdr->appl_attr_loc);
23829f988b79SJean-Baptiste Boric 		if (offset == UDF_APPL_ATTR_LOC_NOT_PRESENT)
23839f988b79SJean-Baptiste Boric 			return ENOENT;
23849f988b79SJean-Baptiste Boric 	}
23859f988b79SJean-Baptiste Boric 
23869f988b79SJean-Baptiste Boric 	/* paranoia check offset and l_ea */
23879f988b79SJean-Baptiste Boric 	if (l_ea + offset >= sector_size - sizeof(struct extattr_entry))
23889f988b79SJean-Baptiste Boric 		return EINVAL;
23899f988b79SJean-Baptiste Boric 
23909f988b79SJean-Baptiste Boric 	DPRINTF(EXTATTR, ("Starting at offset %d\n", offset));
23919f988b79SJean-Baptiste Boric 
23929f988b79SJean-Baptiste Boric 	/* find our extended attribute  */
23939f988b79SJean-Baptiste Boric 	l_ea -= offset;
23949f988b79SJean-Baptiste Boric 	pos = (uint8_t *) eahdr + offset;
23959f988b79SJean-Baptiste Boric 
23969f988b79SJean-Baptiste Boric 	while (l_ea >= sizeof(struct extattr_entry)) {
23979f988b79SJean-Baptiste Boric 		DPRINTF(EXTATTR, ("%d extended attr bytes left\n", l_ea));
23989f988b79SJean-Baptiste Boric 		attrhdr = (struct extattr_entry *) pos;
23999f988b79SJean-Baptiste Boric 		implext = (struct impl_extattr_entry *) pos;
24009f988b79SJean-Baptiste Boric 
24019f988b79SJean-Baptiste Boric 		/* get complete attribute length and check for roque values */
24029f988b79SJean-Baptiste Boric 		a_l = udf_rw32(attrhdr->a_l);
24039f988b79SJean-Baptiste Boric 		DPRINTF(EXTATTR, ("attribute %d:%d, len %d/%d\n",
24049f988b79SJean-Baptiste Boric 				udf_rw32(attrhdr->type),
24059f988b79SJean-Baptiste Boric 				attrhdr->subtype, a_l, l_ea));
24069f988b79SJean-Baptiste Boric 		if ((a_l == 0) || (a_l > l_ea))
24079f988b79SJean-Baptiste Boric 			return EINVAL;
24089f988b79SJean-Baptiste Boric 
24099f988b79SJean-Baptiste Boric 		if (attrhdr->type != sattr)
24109f988b79SJean-Baptiste Boric 			goto next_attribute;
24119f988b79SJean-Baptiste Boric 
24129f988b79SJean-Baptiste Boric 		/* we might have found it! */
24139f988b79SJean-Baptiste Boric 		if (attrhdr->type < 2048) {	/* Ecma-167 attribute */
24149f988b79SJean-Baptiste Boric 			*offsetp = offset;
24159f988b79SJean-Baptiste Boric 			*lengthp = a_l;
24169f988b79SJean-Baptiste Boric 			return 0;		/* success */
24179f988b79SJean-Baptiste Boric 		}
24189f988b79SJean-Baptiste Boric 
24199f988b79SJean-Baptiste Boric 		/*
24209f988b79SJean-Baptiste Boric 		 * Implementation use and application use extended attributes
24219f988b79SJean-Baptiste Boric 		 * have a name to identify. They share the same structure only
24229f988b79SJean-Baptiste Boric 		 * UDF implementation use extended attributes have a checksum
24239f988b79SJean-Baptiste Boric 		 * we need to check
24249f988b79SJean-Baptiste Boric 		 */
24259f988b79SJean-Baptiste Boric 
24269f988b79SJean-Baptiste Boric 		DPRINTF(EXTATTR, ("named attribute %s\n", implext->imp_id.id));
24279f988b79SJean-Baptiste Boric 		if (strcmp(implext->imp_id.id, sattrname) == 0) {
24289f988b79SJean-Baptiste Boric 			/* we have found our appl/implementation attribute */
24299f988b79SJean-Baptiste Boric 			*offsetp = offset;
24309f988b79SJean-Baptiste Boric 			*lengthp = a_l;
24319f988b79SJean-Baptiste Boric 			return 0;		/* success */
24329f988b79SJean-Baptiste Boric 		}
24339f988b79SJean-Baptiste Boric 
24349f988b79SJean-Baptiste Boric next_attribute:
24359f988b79SJean-Baptiste Boric 		/* next attribute */
24369f988b79SJean-Baptiste Boric 		pos    += a_l;
24379f988b79SJean-Baptiste Boric 		l_ea   -= a_l;
24389f988b79SJean-Baptiste Boric 		offset += a_l;
24399f988b79SJean-Baptiste Boric 	}
24409f988b79SJean-Baptiste Boric 	/* not found */
24419f988b79SJean-Baptiste Boric 	return ENOENT;
24429f988b79SJean-Baptiste Boric }
24439f988b79SJean-Baptiste Boric 
24449f988b79SJean-Baptiste Boric 
24459f988b79SJean-Baptiste Boric static void
udf_extattr_insert_internal(struct udf_mount * ump,union dscrptr * dscr,struct extattr_entry * extattr)24469f988b79SJean-Baptiste Boric udf_extattr_insert_internal(struct udf_mount *ump, union dscrptr *dscr,
24479f988b79SJean-Baptiste Boric 	struct extattr_entry *extattr)
24489f988b79SJean-Baptiste Boric {
24499f988b79SJean-Baptiste Boric 	struct file_entry      *fe;
24509f988b79SJean-Baptiste Boric 	struct extfile_entry   *efe;
24519f988b79SJean-Baptiste Boric 	struct extattrhdr_desc *extattrhdr;
24529f988b79SJean-Baptiste Boric 	struct impl_extattr_entry *implext;
24539f988b79SJean-Baptiste Boric 	uint32_t impl_attr_loc, appl_attr_loc, l_ea, a_l, exthdr_len;
24549f988b79SJean-Baptiste Boric 	uint32_t *l_eap, l_ad;
24559f988b79SJean-Baptiste Boric 	uint16_t *spos;
24569f988b79SJean-Baptiste Boric 	uint8_t *bpos, *data;
24579f988b79SJean-Baptiste Boric 
24589f988b79SJean-Baptiste Boric 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
24599f988b79SJean-Baptiste Boric 		fe    = &dscr->fe;
24609f988b79SJean-Baptiste Boric 		data  = fe->data;
24619f988b79SJean-Baptiste Boric 		l_eap = &fe->l_ea;
24629f988b79SJean-Baptiste Boric 		l_ad  = udf_rw32(fe->l_ad);
24639f988b79SJean-Baptiste Boric 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
24649f988b79SJean-Baptiste Boric 		efe   = &dscr->efe;
24659f988b79SJean-Baptiste Boric 		data  = efe->data;
24669f988b79SJean-Baptiste Boric 		l_eap = &efe->l_ea;
24679f988b79SJean-Baptiste Boric 		l_ad  = udf_rw32(efe->l_ad);
24689f988b79SJean-Baptiste Boric 	} else {
24699f988b79SJean-Baptiste Boric 		panic("Bad tag passed to udf_extattr_insert_internal");
24709f988b79SJean-Baptiste Boric 	}
24719f988b79SJean-Baptiste Boric 
24729f988b79SJean-Baptiste Boric 	/* can't append already written to file descriptors yet */
24739f988b79SJean-Baptiste Boric 	assert(l_ad == 0);
2474*0a6a1f1dSLionel Sambuc 	__USE(l_ad);
24759f988b79SJean-Baptiste Boric 
24769f988b79SJean-Baptiste Boric 	/* should have a header! */
24779f988b79SJean-Baptiste Boric 	extattrhdr = (struct extattrhdr_desc *) data;
24789f988b79SJean-Baptiste Boric 	l_ea = udf_rw32(*l_eap);
24799f988b79SJean-Baptiste Boric 	if (l_ea == 0) {
24809f988b79SJean-Baptiste Boric 		/* create empty extended attribute header */
24819f988b79SJean-Baptiste Boric 		exthdr_len = sizeof(struct extattrhdr_desc);
24829f988b79SJean-Baptiste Boric 
24839f988b79SJean-Baptiste Boric 		udf_inittag(ump, &extattrhdr->tag, TAGID_EXTATTR_HDR,
24849f988b79SJean-Baptiste Boric 			/* loc */ 0);
24859f988b79SJean-Baptiste Boric 		extattrhdr->impl_attr_loc = udf_rw32(exthdr_len);
24869f988b79SJean-Baptiste Boric 		extattrhdr->appl_attr_loc = udf_rw32(exthdr_len);
24879f988b79SJean-Baptiste Boric 		extattrhdr->tag.desc_crc_len = udf_rw16(8);
24889f988b79SJean-Baptiste Boric 
24899f988b79SJean-Baptiste Boric 		/* record extended attribute header length */
24909f988b79SJean-Baptiste Boric 		l_ea = exthdr_len;
24919f988b79SJean-Baptiste Boric 		*l_eap = udf_rw32(l_ea);
24929f988b79SJean-Baptiste Boric 	}
24939f988b79SJean-Baptiste Boric 
24949f988b79SJean-Baptiste Boric 	/* extract locations */
24959f988b79SJean-Baptiste Boric 	impl_attr_loc = udf_rw32(extattrhdr->impl_attr_loc);
24969f988b79SJean-Baptiste Boric 	appl_attr_loc = udf_rw32(extattrhdr->appl_attr_loc);
24979f988b79SJean-Baptiste Boric 	if (impl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
24989f988b79SJean-Baptiste Boric 		impl_attr_loc = l_ea;
24999f988b79SJean-Baptiste Boric 	if (appl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
25009f988b79SJean-Baptiste Boric 		appl_attr_loc = l_ea;
25019f988b79SJean-Baptiste Boric 
25029f988b79SJean-Baptiste Boric 	/* Ecma 167 EAs */
25039f988b79SJean-Baptiste Boric 	if (udf_rw32(extattr->type) < 2048) {
25049f988b79SJean-Baptiste Boric 		assert(impl_attr_loc == l_ea);
25059f988b79SJean-Baptiste Boric 		assert(appl_attr_loc == l_ea);
25069f988b79SJean-Baptiste Boric 	}
25079f988b79SJean-Baptiste Boric 
25089f988b79SJean-Baptiste Boric 	/* implementation use extended attributes */
25099f988b79SJean-Baptiste Boric 	if (udf_rw32(extattr->type) == 2048) {
25109f988b79SJean-Baptiste Boric 		assert(appl_attr_loc == l_ea);
25119f988b79SJean-Baptiste Boric 
25129f988b79SJean-Baptiste Boric 		/* calculate and write extended attribute header checksum */
25139f988b79SJean-Baptiste Boric 		implext = (struct impl_extattr_entry *) extattr;
25149f988b79SJean-Baptiste Boric 		assert(udf_rw32(implext->iu_l) == 4);	/* [UDF 3.3.4.5] */
25159f988b79SJean-Baptiste Boric 		spos = (uint16_t *) implext->data;
25169f988b79SJean-Baptiste Boric 		*spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
25179f988b79SJean-Baptiste Boric 	}
25189f988b79SJean-Baptiste Boric 
25199f988b79SJean-Baptiste Boric 	/* application use extended attributes */
25209f988b79SJean-Baptiste Boric 	assert(udf_rw32(extattr->type) != 65536);
25219f988b79SJean-Baptiste Boric 	assert(appl_attr_loc == l_ea);
25229f988b79SJean-Baptiste Boric 
25239f988b79SJean-Baptiste Boric 	/* append the attribute at the end of the current space */
25249f988b79SJean-Baptiste Boric 	bpos = data + udf_rw32(*l_eap);
25259f988b79SJean-Baptiste Boric 	a_l  = udf_rw32(extattr->a_l);
25269f988b79SJean-Baptiste Boric 
25279f988b79SJean-Baptiste Boric 	/* update impl. attribute locations */
25289f988b79SJean-Baptiste Boric 	if (udf_rw32(extattr->type) < 2048) {
25299f988b79SJean-Baptiste Boric 		impl_attr_loc = l_ea + a_l;
25309f988b79SJean-Baptiste Boric 		appl_attr_loc = l_ea + a_l;
25319f988b79SJean-Baptiste Boric 	}
25329f988b79SJean-Baptiste Boric 	if (udf_rw32(extattr->type) == 2048) {
25339f988b79SJean-Baptiste Boric 		appl_attr_loc = l_ea + a_l;
25349f988b79SJean-Baptiste Boric 	}
25359f988b79SJean-Baptiste Boric 
25369f988b79SJean-Baptiste Boric 	/* copy and advance */
25379f988b79SJean-Baptiste Boric 	memcpy(bpos, extattr, a_l);
25389f988b79SJean-Baptiste Boric 	l_ea += a_l;
25399f988b79SJean-Baptiste Boric 	*l_eap = udf_rw32(l_ea);
25409f988b79SJean-Baptiste Boric 
25419f988b79SJean-Baptiste Boric 	/* do the `dance` again backwards */
25429f988b79SJean-Baptiste Boric 	if (udf_rw16(ump->logical_vol->tag.descriptor_ver) != 2) {
25439f988b79SJean-Baptiste Boric 		if (impl_attr_loc == l_ea)
25449f988b79SJean-Baptiste Boric 			impl_attr_loc = UDF_IMPL_ATTR_LOC_NOT_PRESENT;
25459f988b79SJean-Baptiste Boric 		if (appl_attr_loc == l_ea)
25469f988b79SJean-Baptiste Boric 			appl_attr_loc = UDF_APPL_ATTR_LOC_NOT_PRESENT;
25479f988b79SJean-Baptiste Boric 	}
25489f988b79SJean-Baptiste Boric 
25499f988b79SJean-Baptiste Boric 	/* store offsets */
25509f988b79SJean-Baptiste Boric 	extattrhdr->impl_attr_loc = udf_rw32(impl_attr_loc);
25519f988b79SJean-Baptiste Boric 	extattrhdr->appl_attr_loc = udf_rw32(appl_attr_loc);
25529f988b79SJean-Baptiste Boric }
25539f988b79SJean-Baptiste Boric 
25549f988b79SJean-Baptiste Boric 
25559f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
25569f988b79SJean-Baptiste Boric 
25579f988b79SJean-Baptiste Boric static int
udf_update_lvid_from_vat_extattr(struct udf_node * vat_node)25589f988b79SJean-Baptiste Boric udf_update_lvid_from_vat_extattr(struct udf_node *vat_node)
25599f988b79SJean-Baptiste Boric {
25609f988b79SJean-Baptiste Boric 	struct udf_mount       *ump;
25619f988b79SJean-Baptiste Boric 	struct udf_logvol_info *lvinfo;
25629f988b79SJean-Baptiste Boric 	struct impl_extattr_entry     *implext;
25639f988b79SJean-Baptiste Boric 	struct vatlvext_extattr_entry  lvext;
25649f988b79SJean-Baptiste Boric 	const char *extstr = "*UDF VAT LVExtension";
25659f988b79SJean-Baptiste Boric 	uint64_t    vat_uniqueid;
25669f988b79SJean-Baptiste Boric 	uint32_t    offset, a_l;
25679f988b79SJean-Baptiste Boric 	uint8_t    *ea_start, *lvextpos;
25689f988b79SJean-Baptiste Boric 	int         error;
25699f988b79SJean-Baptiste Boric 
25709f988b79SJean-Baptiste Boric 	/* get mountpoint and lvinfo */
25719f988b79SJean-Baptiste Boric 	ump    = vat_node->ump;
25729f988b79SJean-Baptiste Boric 	lvinfo = ump->logvol_info;
25739f988b79SJean-Baptiste Boric 
25749f988b79SJean-Baptiste Boric 	/* get information from fe/efe */
25759f988b79SJean-Baptiste Boric 	if (vat_node->fe) {
25769f988b79SJean-Baptiste Boric 		vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
25779f988b79SJean-Baptiste Boric 		ea_start     = vat_node->fe->data;
25789f988b79SJean-Baptiste Boric 	} else {
25799f988b79SJean-Baptiste Boric 		vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
25809f988b79SJean-Baptiste Boric 		ea_start     = vat_node->efe->data;
25819f988b79SJean-Baptiste Boric 	}
25829f988b79SJean-Baptiste Boric 
25839f988b79SJean-Baptiste Boric 	error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
25849f988b79SJean-Baptiste Boric 	if (error)
25859f988b79SJean-Baptiste Boric 		return error;
25869f988b79SJean-Baptiste Boric 
25879f988b79SJean-Baptiste Boric 	implext = (struct impl_extattr_entry *) (ea_start + offset);
25889f988b79SJean-Baptiste Boric 	error = udf_impl_extattr_check(implext);
25899f988b79SJean-Baptiste Boric 	if (error)
25909f988b79SJean-Baptiste Boric 		return error;
25919f988b79SJean-Baptiste Boric 
25929f988b79SJean-Baptiste Boric 	/* paranoia */
25939f988b79SJean-Baptiste Boric 	if (a_l != sizeof(*implext) -1 + udf_rw32(implext->iu_l) + sizeof(lvext)) {
25949f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("VAT LVExtension size doesn't compute\n"));
25959f988b79SJean-Baptiste Boric 		return EINVAL;
25969f988b79SJean-Baptiste Boric 	}
25979f988b79SJean-Baptiste Boric 
25989f988b79SJean-Baptiste Boric 	/*
25999f988b79SJean-Baptiste Boric 	 * we have found our "VAT LVExtension attribute. BUT due to a
26009f988b79SJean-Baptiste Boric 	 * bug in the specification it might not be word aligned so
26019f988b79SJean-Baptiste Boric 	 * copy first to avoid panics on some machines (!!)
26029f988b79SJean-Baptiste Boric 	 */
26039f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Found VAT LVExtension attr\n"));
26049f988b79SJean-Baptiste Boric 	lvextpos = implext->data + udf_rw32(implext->iu_l);
26059f988b79SJean-Baptiste Boric 	memcpy(&lvext, lvextpos, sizeof(lvext));
26069f988b79SJean-Baptiste Boric 
26079f988b79SJean-Baptiste Boric 	/* check if it was updated the last time */
26089f988b79SJean-Baptiste Boric 	if (udf_rw64(lvext.unique_id_chk) == vat_uniqueid) {
26099f988b79SJean-Baptiste Boric 		lvinfo->num_files       = lvext.num_files;
26109f988b79SJean-Baptiste Boric 		lvinfo->num_directories = lvext.num_directories;
26119f988b79SJean-Baptiste Boric 		udf_update_logvolname(ump, lvext.logvol_id);
26129f988b79SJean-Baptiste Boric 	} else {
26139f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("VAT LVExtension out of date\n"));
26149f988b79SJean-Baptiste Boric 		/* replace VAT LVExt by free space EA */
26159f988b79SJean-Baptiste Boric 		memset(implext->imp_id.id, 0, UDF_REGID_ID_SIZE);
26169f988b79SJean-Baptiste Boric 		strcpy(implext->imp_id.id, "*UDF FreeEASpace");
26179f988b79SJean-Baptiste Boric 		udf_calc_impl_extattr_checksum(implext);
26189f988b79SJean-Baptiste Boric 	}
26199f988b79SJean-Baptiste Boric 
26209f988b79SJean-Baptiste Boric 	return 0;
26219f988b79SJean-Baptiste Boric }
26229f988b79SJean-Baptiste Boric 
26239f988b79SJean-Baptiste Boric 
26249f988b79SJean-Baptiste Boric static int
udf_update_vat_extattr_from_lvid(struct udf_node * vat_node)26259f988b79SJean-Baptiste Boric udf_update_vat_extattr_from_lvid(struct udf_node *vat_node)
26269f988b79SJean-Baptiste Boric {
26279f988b79SJean-Baptiste Boric 	struct udf_mount       *ump;
26289f988b79SJean-Baptiste Boric 	struct udf_logvol_info *lvinfo;
26299f988b79SJean-Baptiste Boric 	struct impl_extattr_entry     *implext;
26309f988b79SJean-Baptiste Boric 	struct vatlvext_extattr_entry  lvext;
26319f988b79SJean-Baptiste Boric 	const char *extstr = "*UDF VAT LVExtension";
26329f988b79SJean-Baptiste Boric 	uint64_t    vat_uniqueid;
26339f988b79SJean-Baptiste Boric 	uint32_t    offset, a_l;
26349f988b79SJean-Baptiste Boric 	uint8_t    *ea_start, *lvextpos;
26359f988b79SJean-Baptiste Boric 	int         error;
26369f988b79SJean-Baptiste Boric 
26379f988b79SJean-Baptiste Boric 	/* get mountpoint and lvinfo */
26389f988b79SJean-Baptiste Boric 	ump    = vat_node->ump;
26399f988b79SJean-Baptiste Boric 	lvinfo = ump->logvol_info;
26409f988b79SJean-Baptiste Boric 
26419f988b79SJean-Baptiste Boric 	/* get information from fe/efe */
26429f988b79SJean-Baptiste Boric 	if (vat_node->fe) {
26439f988b79SJean-Baptiste Boric 		vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
26449f988b79SJean-Baptiste Boric 		ea_start     = vat_node->fe->data;
26459f988b79SJean-Baptiste Boric 	} else {
26469f988b79SJean-Baptiste Boric 		vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
26479f988b79SJean-Baptiste Boric 		ea_start     = vat_node->efe->data;
26489f988b79SJean-Baptiste Boric 	}
26499f988b79SJean-Baptiste Boric 
26509f988b79SJean-Baptiste Boric 	error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
26519f988b79SJean-Baptiste Boric 	if (error)
26529f988b79SJean-Baptiste Boric 		return error;
26539f988b79SJean-Baptiste Boric 	/* found, it existed */
26549f988b79SJean-Baptiste Boric 
26559f988b79SJean-Baptiste Boric 	/* paranoia */
26569f988b79SJean-Baptiste Boric 	implext = (struct impl_extattr_entry *) (ea_start + offset);
26579f988b79SJean-Baptiste Boric 	error = udf_impl_extattr_check(implext);
26589f988b79SJean-Baptiste Boric 	if (error) {
26599f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("VAT LVExtension bad on update\n"));
26609f988b79SJean-Baptiste Boric 		return error;
26619f988b79SJean-Baptiste Boric 	}
26629f988b79SJean-Baptiste Boric 	/* it is correct */
26639f988b79SJean-Baptiste Boric 
26649f988b79SJean-Baptiste Boric 	/*
26659f988b79SJean-Baptiste Boric 	 * we have found our "VAT LVExtension attribute. BUT due to a
26669f988b79SJean-Baptiste Boric 	 * bug in the specification it might not be word aligned so
26679f988b79SJean-Baptiste Boric 	 * copy first to avoid panics on some machines (!!)
26689f988b79SJean-Baptiste Boric 	 */
26699f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Updating VAT LVExtension attr\n"));
26709f988b79SJean-Baptiste Boric 	lvextpos = implext->data + udf_rw32(implext->iu_l);
26719f988b79SJean-Baptiste Boric 
26729f988b79SJean-Baptiste Boric 	lvext.unique_id_chk   = vat_uniqueid;
26739f988b79SJean-Baptiste Boric 	lvext.num_files       = lvinfo->num_files;
26749f988b79SJean-Baptiste Boric 	lvext.num_directories = lvinfo->num_directories;
26759f988b79SJean-Baptiste Boric 	memmove(lvext.logvol_id, ump->logical_vol->logvol_id, 128);
26769f988b79SJean-Baptiste Boric 
26779f988b79SJean-Baptiste Boric 	memcpy(lvextpos, &lvext, sizeof(lvext));
26789f988b79SJean-Baptiste Boric 
26799f988b79SJean-Baptiste Boric 	return 0;
26809f988b79SJean-Baptiste Boric }
26819f988b79SJean-Baptiste Boric 
26829f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
26839f988b79SJean-Baptiste Boric 
26849f988b79SJean-Baptiste Boric int
udf_vat_read(struct udf_node * vat_node,uint8_t * blob,int size,uint32_t offset)26859f988b79SJean-Baptiste Boric udf_vat_read(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
26869f988b79SJean-Baptiste Boric {
26879f988b79SJean-Baptiste Boric 	struct udf_mount *ump = vat_node->ump;
26889f988b79SJean-Baptiste Boric 
26899f988b79SJean-Baptiste Boric 	if (offset + size > ump->vat_offset + ump->vat_entries * 4)
26909f988b79SJean-Baptiste Boric 		return EINVAL;
26919f988b79SJean-Baptiste Boric 
26929f988b79SJean-Baptiste Boric 	memcpy(blob, ump->vat_table + offset, size);
26939f988b79SJean-Baptiste Boric 	return 0;
26949f988b79SJean-Baptiste Boric }
26959f988b79SJean-Baptiste Boric 
26969f988b79SJean-Baptiste Boric int
udf_vat_write(struct udf_node * vat_node,uint8_t * blob,int size,uint32_t offset)26979f988b79SJean-Baptiste Boric udf_vat_write(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
26989f988b79SJean-Baptiste Boric {
26999f988b79SJean-Baptiste Boric 	struct udf_mount *ump = vat_node->ump;
27009f988b79SJean-Baptiste Boric 	uint32_t offset_high;
27019f988b79SJean-Baptiste Boric 	uint8_t *new_vat_table;
27029f988b79SJean-Baptiste Boric 
27039f988b79SJean-Baptiste Boric 	/* extent VAT allocation if needed */
27049f988b79SJean-Baptiste Boric 	offset_high = offset + size;
27059f988b79SJean-Baptiste Boric 	if (offset_high >= ump->vat_table_alloc_len) {
27069f988b79SJean-Baptiste Boric 		/* realloc */
27079f988b79SJean-Baptiste Boric 		new_vat_table = realloc(ump->vat_table,
27089f988b79SJean-Baptiste Boric 			ump->vat_table_alloc_len + UDF_VAT_CHUNKSIZE,
27099f988b79SJean-Baptiste Boric 			M_UDFVOLD, M_WAITOK | M_CANFAIL);
27109f988b79SJean-Baptiste Boric 		if (!new_vat_table) {
27119f988b79SJean-Baptiste Boric 			printf("udf_vat_write: can't extent VAT, out of mem\n");
27129f988b79SJean-Baptiste Boric 			return ENOMEM;
27139f988b79SJean-Baptiste Boric 		}
27149f988b79SJean-Baptiste Boric 		ump->vat_table = new_vat_table;
27159f988b79SJean-Baptiste Boric 		ump->vat_table_alloc_len += UDF_VAT_CHUNKSIZE;
27169f988b79SJean-Baptiste Boric 	}
27179f988b79SJean-Baptiste Boric 	ump->vat_table_len = MAX(ump->vat_table_len, offset_high);
27189f988b79SJean-Baptiste Boric 
27199f988b79SJean-Baptiste Boric 	memcpy(ump->vat_table + offset, blob, size);
27209f988b79SJean-Baptiste Boric 	return 0;
27219f988b79SJean-Baptiste Boric }
27229f988b79SJean-Baptiste Boric 
27239f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
27249f988b79SJean-Baptiste Boric 
27259f988b79SJean-Baptiste Boric /* TODO support previous VAT location writeout */
27269f988b79SJean-Baptiste Boric static int
udf_update_vat_descriptor(struct udf_mount * ump)27279f988b79SJean-Baptiste Boric udf_update_vat_descriptor(struct udf_mount *ump)
27289f988b79SJean-Baptiste Boric {
27299f988b79SJean-Baptiste Boric 	struct udf_node *vat_node = ump->vat_node;
27309f988b79SJean-Baptiste Boric 	struct udf_logvol_info *lvinfo = ump->logvol_info;
27319f988b79SJean-Baptiste Boric 	struct icb_tag *icbtag;
27329f988b79SJean-Baptiste Boric 	struct udf_oldvat_tail *oldvat_tl;
27339f988b79SJean-Baptiste Boric 	struct udf_vat *vat;
27349f988b79SJean-Baptiste Boric 	uint64_t unique_id;
27359f988b79SJean-Baptiste Boric 	uint32_t lb_size;
27369f988b79SJean-Baptiste Boric 	uint8_t *raw_vat;
27379f988b79SJean-Baptiste Boric 	int filetype, error;
27389f988b79SJean-Baptiste Boric 
27399f988b79SJean-Baptiste Boric 	KASSERT(vat_node);
27409f988b79SJean-Baptiste Boric 	KASSERT(lvinfo);
27419f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
27429f988b79SJean-Baptiste Boric 
27439f988b79SJean-Baptiste Boric 	/* get our new unique_id */
27449f988b79SJean-Baptiste Boric 	unique_id = udf_advance_uniqueid(ump);
27459f988b79SJean-Baptiste Boric 
27469f988b79SJean-Baptiste Boric 	/* get information from fe/efe */
27479f988b79SJean-Baptiste Boric 	if (vat_node->fe) {
27489f988b79SJean-Baptiste Boric 		icbtag    = &vat_node->fe->icbtag;
27499f988b79SJean-Baptiste Boric 		vat_node->fe->unique_id = udf_rw64(unique_id);
27509f988b79SJean-Baptiste Boric 	} else {
27519f988b79SJean-Baptiste Boric 		icbtag = &vat_node->efe->icbtag;
27529f988b79SJean-Baptiste Boric 		vat_node->efe->unique_id = udf_rw64(unique_id);
27539f988b79SJean-Baptiste Boric 	}
27549f988b79SJean-Baptiste Boric 
27559f988b79SJean-Baptiste Boric 	/* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
27569f988b79SJean-Baptiste Boric 	filetype = icbtag->file_type;
27579f988b79SJean-Baptiste Boric 	KASSERT((filetype == 0) || (filetype == UDF_ICB_FILETYPE_VAT));
27589f988b79SJean-Baptiste Boric 
27599f988b79SJean-Baptiste Boric 	/* allocate piece to process head or tail of VAT file */
27609f988b79SJean-Baptiste Boric 	raw_vat = malloc(lb_size, M_TEMP, M_WAITOK);
27619f988b79SJean-Baptiste Boric 
27629f988b79SJean-Baptiste Boric 	if (filetype == 0) {
27639f988b79SJean-Baptiste Boric 		/*
27649f988b79SJean-Baptiste Boric 		 * Update "*UDF VAT LVExtension" extended attribute from the
27659f988b79SJean-Baptiste Boric 		 * lvint if present.
27669f988b79SJean-Baptiste Boric 		 */
27679f988b79SJean-Baptiste Boric 		udf_update_vat_extattr_from_lvid(vat_node);
27689f988b79SJean-Baptiste Boric 
27699f988b79SJean-Baptiste Boric 		/* setup identifying regid */
27709f988b79SJean-Baptiste Boric 		oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
27719f988b79SJean-Baptiste Boric 		memset(oldvat_tl, 0, sizeof(struct udf_oldvat_tail));
27729f988b79SJean-Baptiste Boric 
27739f988b79SJean-Baptiste Boric 		udf_set_regid(&oldvat_tl->id, "*UDF Virtual Alloc Tbl");
27749f988b79SJean-Baptiste Boric 		udf_add_udf_regid(ump, &oldvat_tl->id);
27759f988b79SJean-Baptiste Boric 		oldvat_tl->prev_vat = udf_rw32(0xffffffff);
27769f988b79SJean-Baptiste Boric 
27779f988b79SJean-Baptiste Boric 		/* write out new tail of virtual allocation table file */
27789f988b79SJean-Baptiste Boric 		error = udf_vat_write(vat_node, raw_vat,
27799f988b79SJean-Baptiste Boric 			sizeof(struct udf_oldvat_tail), ump->vat_entries * 4);
27809f988b79SJean-Baptiste Boric 	} else {
27819f988b79SJean-Baptiste Boric 		/* compose the VAT2 header */
27829f988b79SJean-Baptiste Boric 		vat = (struct udf_vat *) raw_vat;
27839f988b79SJean-Baptiste Boric 		memset(vat, 0, sizeof(struct udf_vat));
27849f988b79SJean-Baptiste Boric 
27859f988b79SJean-Baptiste Boric 		vat->header_len       = udf_rw16(152);	/* as per spec */
27869f988b79SJean-Baptiste Boric 		vat->impl_use_len     = udf_rw16(0);
27879f988b79SJean-Baptiste Boric 		memmove(vat->logvol_id, ump->logical_vol->logvol_id, 128);
27889f988b79SJean-Baptiste Boric 		vat->prev_vat         = udf_rw32(0xffffffff);
27899f988b79SJean-Baptiste Boric 		vat->num_files        = lvinfo->num_files;
27909f988b79SJean-Baptiste Boric 		vat->num_directories  = lvinfo->num_directories;
27919f988b79SJean-Baptiste Boric 		vat->min_udf_readver  = lvinfo->min_udf_readver;
27929f988b79SJean-Baptiste Boric 		vat->min_udf_writever = lvinfo->min_udf_writever;
27939f988b79SJean-Baptiste Boric 		vat->max_udf_writever = lvinfo->max_udf_writever;
27949f988b79SJean-Baptiste Boric 
27959f988b79SJean-Baptiste Boric 		error = udf_vat_write(vat_node, raw_vat,
27969f988b79SJean-Baptiste Boric 			sizeof(struct udf_vat), 0);
27979f988b79SJean-Baptiste Boric 	}
27989f988b79SJean-Baptiste Boric 	free(raw_vat, M_TEMP);
27999f988b79SJean-Baptiste Boric 
28009f988b79SJean-Baptiste Boric 	return error;	/* success! */
28019f988b79SJean-Baptiste Boric }
28029f988b79SJean-Baptiste Boric 
28039f988b79SJean-Baptiste Boric 
28049f988b79SJean-Baptiste Boric int
udf_writeout_vat(struct udf_mount * ump)28059f988b79SJean-Baptiste Boric udf_writeout_vat(struct udf_mount *ump)
28069f988b79SJean-Baptiste Boric {
28079f988b79SJean-Baptiste Boric 	struct udf_node *vat_node = ump->vat_node;
28089f988b79SJean-Baptiste Boric 	int error;
28099f988b79SJean-Baptiste Boric 
28109f988b79SJean-Baptiste Boric 	KASSERT(vat_node);
28119f988b79SJean-Baptiste Boric 
28129f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("udf_writeout_vat\n"));
28139f988b79SJean-Baptiste Boric 
28149f988b79SJean-Baptiste Boric //	mutex_enter(&ump->allocate_mutex);
28159f988b79SJean-Baptiste Boric 	udf_update_vat_descriptor(ump);
28169f988b79SJean-Baptiste Boric 
28179f988b79SJean-Baptiste Boric 	/* write out the VAT contents ; TODO intelligent writing */
28189f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_WRITE, vat_node->vnode,
28199f988b79SJean-Baptiste Boric 		ump->vat_table, ump->vat_table_len, 0,
28209f988b79SJean-Baptiste Boric 		UIO_SYSSPACE, 0, FSCRED, NULL, NULL);
28219f988b79SJean-Baptiste Boric 	if (error) {
28229f988b79SJean-Baptiste Boric 		printf("udf_writeout_vat: failed to write out VAT contents\n");
28239f988b79SJean-Baptiste Boric 		goto out;
28249f988b79SJean-Baptiste Boric 	}
28259f988b79SJean-Baptiste Boric 
28269f988b79SJean-Baptiste Boric //	mutex_exit(&ump->allocate_mutex);
28279f988b79SJean-Baptiste Boric 
28289f988b79SJean-Baptiste Boric 	error = vflushbuf(ump->vat_node->vnode, FSYNC_WAIT);
28299f988b79SJean-Baptiste Boric 	if (error)
28309f988b79SJean-Baptiste Boric 		goto out;
28319f988b79SJean-Baptiste Boric 	error = VOP_FSYNC(ump->vat_node->vnode,
28329f988b79SJean-Baptiste Boric 			FSCRED, FSYNC_WAIT, 0, 0);
28339f988b79SJean-Baptiste Boric 	if (error)
28349f988b79SJean-Baptiste Boric 		printf("udf_writeout_vat: error writing VAT node!\n");
28359f988b79SJean-Baptiste Boric out:
28369f988b79SJean-Baptiste Boric 
28379f988b79SJean-Baptiste Boric 	return error;
28389f988b79SJean-Baptiste Boric }
28399f988b79SJean-Baptiste Boric 
28409f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
28419f988b79SJean-Baptiste Boric 
28429f988b79SJean-Baptiste Boric /*
28439f988b79SJean-Baptiste Boric  * Read in relevant pieces of VAT file and check if its indeed a VAT file
28449f988b79SJean-Baptiste Boric  * descriptor. If OK, read in complete VAT file.
28459f988b79SJean-Baptiste Boric  */
28469f988b79SJean-Baptiste Boric 
28479f988b79SJean-Baptiste Boric static int
udf_check_for_vat(struct udf_node * vat_node)28489f988b79SJean-Baptiste Boric udf_check_for_vat(struct udf_node *vat_node)
28499f988b79SJean-Baptiste Boric {
28509f988b79SJean-Baptiste Boric 	struct udf_mount *ump;
28519f988b79SJean-Baptiste Boric 	struct icb_tag   *icbtag;
28529f988b79SJean-Baptiste Boric 	struct timestamp *mtime;
28539f988b79SJean-Baptiste Boric 	struct udf_vat   *vat;
28549f988b79SJean-Baptiste Boric 	struct udf_oldvat_tail *oldvat_tl;
28559f988b79SJean-Baptiste Boric 	struct udf_logvol_info *lvinfo;
28569f988b79SJean-Baptiste Boric 	uint64_t  unique_id;
28579f988b79SJean-Baptiste Boric 	uint32_t  vat_length;
28589f988b79SJean-Baptiste Boric 	uint32_t  vat_offset, vat_entries, vat_table_alloc_len;
28599f988b79SJean-Baptiste Boric 	uint32_t  sector_size;
28609f988b79SJean-Baptiste Boric 	uint32_t *raw_vat;
28619f988b79SJean-Baptiste Boric 	uint8_t  *vat_table;
28629f988b79SJean-Baptiste Boric 	char     *regid_name;
28639f988b79SJean-Baptiste Boric 	int filetype;
28649f988b79SJean-Baptiste Boric 	int error;
28659f988b79SJean-Baptiste Boric 
28669f988b79SJean-Baptiste Boric 	/* vat_length is really 64 bits though impossible */
28679f988b79SJean-Baptiste Boric 
28689f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Checking for VAT\n"));
28699f988b79SJean-Baptiste Boric 	if (!vat_node)
28709f988b79SJean-Baptiste Boric 		return ENOENT;
28719f988b79SJean-Baptiste Boric 
28729f988b79SJean-Baptiste Boric 	/* get mount info */
28739f988b79SJean-Baptiste Boric 	ump = vat_node->ump;
28749f988b79SJean-Baptiste Boric 	sector_size = udf_rw32(ump->logical_vol->lb_size);
28759f988b79SJean-Baptiste Boric 
28769f988b79SJean-Baptiste Boric 	/* check assertions */
28779f988b79SJean-Baptiste Boric 	assert(vat_node->fe || vat_node->efe);
28789f988b79SJean-Baptiste Boric 	assert(ump->logvol_integrity);
28799f988b79SJean-Baptiste Boric 
28809f988b79SJean-Baptiste Boric 	/* set vnode type to regular file or we can't read from it! */
28819f988b79SJean-Baptiste Boric 	vat_node->vnode->v_type = VREG;
28829f988b79SJean-Baptiste Boric 
28839f988b79SJean-Baptiste Boric 	/* get information from fe/efe */
28849f988b79SJean-Baptiste Boric 	if (vat_node->fe) {
28859f988b79SJean-Baptiste Boric 		vat_length = udf_rw64(vat_node->fe->inf_len);
28869f988b79SJean-Baptiste Boric 		icbtag    = &vat_node->fe->icbtag;
28879f988b79SJean-Baptiste Boric 		mtime     = &vat_node->fe->mtime;
28889f988b79SJean-Baptiste Boric 		unique_id = udf_rw64(vat_node->fe->unique_id);
28899f988b79SJean-Baptiste Boric 	} else {
28909f988b79SJean-Baptiste Boric 		vat_length = udf_rw64(vat_node->efe->inf_len);
28919f988b79SJean-Baptiste Boric 		icbtag = &vat_node->efe->icbtag;
28929f988b79SJean-Baptiste Boric 		mtime  = &vat_node->efe->mtime;
28939f988b79SJean-Baptiste Boric 		unique_id = udf_rw64(vat_node->efe->unique_id);
28949f988b79SJean-Baptiste Boric 	}
28959f988b79SJean-Baptiste Boric 
28969f988b79SJean-Baptiste Boric 	/* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
28979f988b79SJean-Baptiste Boric 	filetype = icbtag->file_type;
28989f988b79SJean-Baptiste Boric 	if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT))
28999f988b79SJean-Baptiste Boric 		return ENOENT;
29009f988b79SJean-Baptiste Boric 
29019f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length));
29029f988b79SJean-Baptiste Boric 
29039f988b79SJean-Baptiste Boric 	vat_table_alloc_len =
29049f988b79SJean-Baptiste Boric 		((vat_length + UDF_VAT_CHUNKSIZE-1) / UDF_VAT_CHUNKSIZE)
29059f988b79SJean-Baptiste Boric 			* UDF_VAT_CHUNKSIZE;
29069f988b79SJean-Baptiste Boric 
29079f988b79SJean-Baptiste Boric 	vat_table = malloc(vat_table_alloc_len, M_UDFVOLD,
29089f988b79SJean-Baptiste Boric 		M_CANFAIL | M_WAITOK);
29099f988b79SJean-Baptiste Boric 	if (vat_table == NULL) {
29109f988b79SJean-Baptiste Boric 		printf("allocation of %d bytes failed for VAT\n",
29119f988b79SJean-Baptiste Boric 			vat_table_alloc_len);
29129f988b79SJean-Baptiste Boric 		return ENOMEM;
29139f988b79SJean-Baptiste Boric 	}
29149f988b79SJean-Baptiste Boric 
29159f988b79SJean-Baptiste Boric 	/* allocate piece to read in head or tail of VAT file */
29169f988b79SJean-Baptiste Boric 	raw_vat = malloc(sector_size, M_TEMP, M_WAITOK);
29179f988b79SJean-Baptiste Boric 
29189f988b79SJean-Baptiste Boric 	/*
29199f988b79SJean-Baptiste Boric 	 * check contents of the file if its the old 1.50 VAT table format.
29209f988b79SJean-Baptiste Boric 	 * Its notoriously broken and allthough some implementations support an
29219f988b79SJean-Baptiste Boric 	 * extention as defined in the UDF 1.50 errata document, its doubtfull
29229f988b79SJean-Baptiste Boric 	 * to be useable since a lot of implementations don't maintain it.
29239f988b79SJean-Baptiste Boric 	 */
29249f988b79SJean-Baptiste Boric 	lvinfo = ump->logvol_info;
29259f988b79SJean-Baptiste Boric 
29269f988b79SJean-Baptiste Boric 	if (filetype == 0) {
29279f988b79SJean-Baptiste Boric 		/* definition */
29289f988b79SJean-Baptiste Boric 		vat_offset  = 0;
29299f988b79SJean-Baptiste Boric 		vat_entries = (vat_length-36)/4;
29309f988b79SJean-Baptiste Boric 
29319f988b79SJean-Baptiste Boric 		/* read in tail of virtual allocation table file */
29329f988b79SJean-Baptiste Boric 		error = vn_rdwr(UIO_READ, vat_node->vnode,
29339f988b79SJean-Baptiste Boric 				(uint8_t *) raw_vat,
29349f988b79SJean-Baptiste Boric 				sizeof(struct udf_oldvat_tail),
29359f988b79SJean-Baptiste Boric 				vat_entries * 4,
29369f988b79SJean-Baptiste Boric 				UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
29379f988b79SJean-Baptiste Boric 				NULL, NULL);
29389f988b79SJean-Baptiste Boric 		if (error)
29399f988b79SJean-Baptiste Boric 			goto out;
29409f988b79SJean-Baptiste Boric 
29419f988b79SJean-Baptiste Boric 		/* check 1.50 VAT */
29429f988b79SJean-Baptiste Boric 		oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
29439f988b79SJean-Baptiste Boric 		regid_name = (char *) oldvat_tl->id.id;
29449f988b79SJean-Baptiste Boric 		error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22);
29459f988b79SJean-Baptiste Boric 		if (error) {
29469f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n"));
29479f988b79SJean-Baptiste Boric 			error = ENOENT;
29489f988b79SJean-Baptiste Boric 			goto out;
29499f988b79SJean-Baptiste Boric 		}
29509f988b79SJean-Baptiste Boric 
29519f988b79SJean-Baptiste Boric 		/*
29529f988b79SJean-Baptiste Boric 		 * update LVID from "*UDF VAT LVExtension" extended attribute
29539f988b79SJean-Baptiste Boric 		 * if present.
29549f988b79SJean-Baptiste Boric 		 */
29559f988b79SJean-Baptiste Boric 		udf_update_lvid_from_vat_extattr(vat_node);
29569f988b79SJean-Baptiste Boric 	} else {
29579f988b79SJean-Baptiste Boric 		/* read in head of virtual allocation table file */
29589f988b79SJean-Baptiste Boric 		error = vn_rdwr(UIO_READ, vat_node->vnode,
29599f988b79SJean-Baptiste Boric 				(uint8_t *) raw_vat,
29609f988b79SJean-Baptiste Boric 				sizeof(struct udf_vat), 0,
29619f988b79SJean-Baptiste Boric 				UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
29629f988b79SJean-Baptiste Boric 				NULL, NULL);
29639f988b79SJean-Baptiste Boric 		if (error)
29649f988b79SJean-Baptiste Boric 			goto out;
29659f988b79SJean-Baptiste Boric 
29669f988b79SJean-Baptiste Boric 		/* definition */
29679f988b79SJean-Baptiste Boric 		vat = (struct udf_vat *) raw_vat;
29689f988b79SJean-Baptiste Boric 		vat_offset  = vat->header_len;
29699f988b79SJean-Baptiste Boric 		vat_entries = (vat_length - vat_offset)/4;
29709f988b79SJean-Baptiste Boric 
29719f988b79SJean-Baptiste Boric 		assert(lvinfo);
29729f988b79SJean-Baptiste Boric 		lvinfo->num_files        = vat->num_files;
29739f988b79SJean-Baptiste Boric 		lvinfo->num_directories  = vat->num_directories;
29749f988b79SJean-Baptiste Boric 		lvinfo->min_udf_readver  = vat->min_udf_readver;
29759f988b79SJean-Baptiste Boric 		lvinfo->min_udf_writever = vat->min_udf_writever;
29769f988b79SJean-Baptiste Boric 		lvinfo->max_udf_writever = vat->max_udf_writever;
29779f988b79SJean-Baptiste Boric 
29789f988b79SJean-Baptiste Boric 		udf_update_logvolname(ump, vat->logvol_id);
29799f988b79SJean-Baptiste Boric 	}
29809f988b79SJean-Baptiste Boric 
29819f988b79SJean-Baptiste Boric 	/* read in complete VAT file */
29829f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_READ, vat_node->vnode,
29839f988b79SJean-Baptiste Boric 			vat_table,
29849f988b79SJean-Baptiste Boric 			vat_length, 0,
29859f988b79SJean-Baptiste Boric 			UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
29869f988b79SJean-Baptiste Boric 			NULL, NULL);
29879f988b79SJean-Baptiste Boric 	if (error)
29889f988b79SJean-Baptiste Boric 		printf("read in of complete VAT file failed (error %d)\n",
29899f988b79SJean-Baptiste Boric 			error);
29909f988b79SJean-Baptiste Boric 	if (error)
29919f988b79SJean-Baptiste Boric 		goto out;
29929f988b79SJean-Baptiste Boric 
29939f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n"));
29949f988b79SJean-Baptiste Boric 	ump->logvol_integrity->lvint_next_unique_id = udf_rw64(unique_id);
29959f988b79SJean-Baptiste Boric 	ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
29969f988b79SJean-Baptiste Boric 	ump->logvol_integrity->time           = *mtime;
29979f988b79SJean-Baptiste Boric 
29989f988b79SJean-Baptiste Boric 	ump->vat_table_len = vat_length;
29999f988b79SJean-Baptiste Boric 	ump->vat_table_alloc_len = vat_table_alloc_len;
30009f988b79SJean-Baptiste Boric 	ump->vat_table   = vat_table;
30019f988b79SJean-Baptiste Boric 	ump->vat_offset  = vat_offset;
30029f988b79SJean-Baptiste Boric 	ump->vat_entries = vat_entries;
30039f988b79SJean-Baptiste Boric 	ump->vat_last_free_lb = 0;		/* start at beginning */
30049f988b79SJean-Baptiste Boric 
30059f988b79SJean-Baptiste Boric out:
30069f988b79SJean-Baptiste Boric 	if (error) {
30079f988b79SJean-Baptiste Boric 		if (vat_table)
30089f988b79SJean-Baptiste Boric 			free(vat_table, M_UDFVOLD);
30099f988b79SJean-Baptiste Boric 	}
30109f988b79SJean-Baptiste Boric 	free(raw_vat, M_TEMP);
30119f988b79SJean-Baptiste Boric 
30129f988b79SJean-Baptiste Boric 	return error;
30139f988b79SJean-Baptiste Boric }
30149f988b79SJean-Baptiste Boric 
30159f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
30169f988b79SJean-Baptiste Boric 
30179f988b79SJean-Baptiste Boric static int
udf_search_vat(struct udf_mount * ump,union udf_pmap * mapping)30189f988b79SJean-Baptiste Boric udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping)
30199f988b79SJean-Baptiste Boric {
30209f988b79SJean-Baptiste Boric 	struct udf_node *vat_node;
30219f988b79SJean-Baptiste Boric 	struct long_ad	 icb_loc;
30229f988b79SJean-Baptiste Boric 	uint32_t early_vat_loc, vat_loc;
30239f988b79SJean-Baptiste Boric 	int error;
30249f988b79SJean-Baptiste Boric 
30259f988b79SJean-Baptiste Boric 	/* mapping info not needed */
30269f988b79SJean-Baptiste Boric 	mapping = mapping;
30279f988b79SJean-Baptiste Boric 
30289f988b79SJean-Baptiste Boric 	vat_loc = ump->last_possible_vat_location;
30299f988b79SJean-Baptiste Boric 	early_vat_loc = vat_loc - 256;	/* 8 blocks of 32 sectors */
30309f988b79SJean-Baptiste Boric 
30319f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("1) last possible %d, early_vat_loc %d \n",
30329f988b79SJean-Baptiste Boric 		vat_loc, early_vat_loc));
30339f988b79SJean-Baptiste Boric 	early_vat_loc = MAX(early_vat_loc, ump->first_possible_vat_location);
30349f988b79SJean-Baptiste Boric 
30359f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("2) last possible %d, early_vat_loc %d \n",
30369f988b79SJean-Baptiste Boric 		vat_loc, early_vat_loc));
30379f988b79SJean-Baptiste Boric 
30389f988b79SJean-Baptiste Boric 	/* start looking from the end of the range */
30399f988b79SJean-Baptiste Boric 	do {
30409f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc));
30419f988b79SJean-Baptiste Boric 		icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART);
30429f988b79SJean-Baptiste Boric 		icb_loc.loc.lb_num   = udf_rw32(vat_loc);
30439f988b79SJean-Baptiste Boric 
30449f988b79SJean-Baptiste Boric 		error = udf_get_node(ump, &icb_loc, &vat_node);
30459f988b79SJean-Baptiste Boric 		if (!error) {
30469f988b79SJean-Baptiste Boric 			error = udf_check_for_vat(vat_node);
30479f988b79SJean-Baptiste Boric 			DPRINTFIF(VOLUMES, !error,
30489f988b79SJean-Baptiste Boric 				("VAT accepted at %d\n", vat_loc));
30499f988b79SJean-Baptiste Boric 			if (!error)
30509f988b79SJean-Baptiste Boric 				break;
30519f988b79SJean-Baptiste Boric 		}
30529f988b79SJean-Baptiste Boric 		if (vat_node) {
30539f988b79SJean-Baptiste Boric 			vput(vat_node->vnode);
30549f988b79SJean-Baptiste Boric 			vat_node = NULL;
30559f988b79SJean-Baptiste Boric 		}
30569f988b79SJean-Baptiste Boric 		vat_loc--;	/* walk backwards */
30579f988b79SJean-Baptiste Boric 	} while (vat_loc >= early_vat_loc);
30589f988b79SJean-Baptiste Boric 
30599f988b79SJean-Baptiste Boric 	/* keep our VAT node around */
30609f988b79SJean-Baptiste Boric 	if (vat_node) {
30619f988b79SJean-Baptiste Boric 		UDF_SET_SYSTEMFILE(vat_node->vnode);
30629f988b79SJean-Baptiste Boric 		ump->vat_node = vat_node;
30639f988b79SJean-Baptiste Boric 	}
30649f988b79SJean-Baptiste Boric 
30659f988b79SJean-Baptiste Boric 	return error;
30669f988b79SJean-Baptiste Boric }
30679f988b79SJean-Baptiste Boric 
30689f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
30699f988b79SJean-Baptiste Boric 
30709f988b79SJean-Baptiste Boric static int
udf_read_sparables(struct udf_mount * ump,union udf_pmap * mapping)30719f988b79SJean-Baptiste Boric udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
30729f988b79SJean-Baptiste Boric {
30739f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
30749f988b79SJean-Baptiste Boric 	struct part_map_spare *pms = &mapping->pms;
30759f988b79SJean-Baptiste Boric 	uint32_t lb_num;
30769f988b79SJean-Baptiste Boric 	int spar, error;
30779f988b79SJean-Baptiste Boric 
30789f988b79SJean-Baptiste Boric 	/*
30799f988b79SJean-Baptiste Boric 	 * The partition mapping passed on to us specifies the information we
30809f988b79SJean-Baptiste Boric 	 * need to locate and initialise the sparable partition mapping
30819f988b79SJean-Baptiste Boric 	 * information we need.
30829f988b79SJean-Baptiste Boric 	 */
30839f988b79SJean-Baptiste Boric 
30849f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Read sparable table\n"));
30859f988b79SJean-Baptiste Boric 	ump->sparable_packet_size = udf_rw16(pms->packet_len);
30869f988b79SJean-Baptiste Boric 	KASSERT(ump->sparable_packet_size >= ump->packet_size);	/* XXX */
30879f988b79SJean-Baptiste Boric 
30889f988b79SJean-Baptiste Boric 	for (spar = 0; spar < pms->n_st; spar++) {
30899f988b79SJean-Baptiste Boric 		lb_num = pms->st_loc[spar];
30909f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
30919f988b79SJean-Baptiste Boric 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
30929f988b79SJean-Baptiste Boric 		if (!error && dscr) {
30939f988b79SJean-Baptiste Boric 			if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
30949f988b79SJean-Baptiste Boric 				if (ump->sparing_table)
30959f988b79SJean-Baptiste Boric 					free(ump->sparing_table, M_UDFVOLD);
30969f988b79SJean-Baptiste Boric 				ump->sparing_table = &dscr->spt;
30979f988b79SJean-Baptiste Boric 				dscr = NULL;
30989f988b79SJean-Baptiste Boric 				DPRINTF(VOLUMES,
30999f988b79SJean-Baptiste Boric 				    ("Sparing table accepted (%d entries)\n",
31009f988b79SJean-Baptiste Boric 				     udf_rw16(ump->sparing_table->rt_l)));
31019f988b79SJean-Baptiste Boric 				break;	/* we're done */
31029f988b79SJean-Baptiste Boric 			}
31039f988b79SJean-Baptiste Boric 		}
31049f988b79SJean-Baptiste Boric 		if (dscr)
31059f988b79SJean-Baptiste Boric 			free(dscr, M_UDFVOLD);
31069f988b79SJean-Baptiste Boric 	}
31079f988b79SJean-Baptiste Boric 
31089f988b79SJean-Baptiste Boric 	if (ump->sparing_table)
31099f988b79SJean-Baptiste Boric 		return 0;
31109f988b79SJean-Baptiste Boric 
31119f988b79SJean-Baptiste Boric 	return ENOENT;
31129f988b79SJean-Baptiste Boric }
31139f988b79SJean-Baptiste Boric 
31149f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
31159f988b79SJean-Baptiste Boric 
31169f988b79SJean-Baptiste Boric static int
udf_read_metadata_nodes(struct udf_mount * ump,union udf_pmap * mapping)31179f988b79SJean-Baptiste Boric udf_read_metadata_nodes(struct udf_mount *ump, union udf_pmap *mapping)
31189f988b79SJean-Baptiste Boric {
31199f988b79SJean-Baptiste Boric 	struct part_map_meta *pmm = &mapping->pmm;
31209f988b79SJean-Baptiste Boric 	struct long_ad	 icb_loc;
31219f988b79SJean-Baptiste Boric 	struct vnode *vp;
31229f988b79SJean-Baptiste Boric 	uint16_t raw_phys_part, phys_part;
31239f988b79SJean-Baptiste Boric 	int error;
31249f988b79SJean-Baptiste Boric 
31259f988b79SJean-Baptiste Boric 	/*
31269f988b79SJean-Baptiste Boric 	 * BUGALERT: some rogue implementations use random physical
31279f988b79SJean-Baptiste Boric 	 * partition numbers to break other implementations so lookup
31289f988b79SJean-Baptiste Boric 	 * the number.
31299f988b79SJean-Baptiste Boric 	 */
31309f988b79SJean-Baptiste Boric 
31319f988b79SJean-Baptiste Boric 	/* extract our allocation parameters set up on format */
31329f988b79SJean-Baptiste Boric 	ump->metadata_alloc_unit_size     = udf_rw32(mapping->pmm.alloc_unit_size);
31339f988b79SJean-Baptiste Boric 	ump->metadata_alignment_unit_size = udf_rw16(mapping->pmm.alignment_unit_size);
31349f988b79SJean-Baptiste Boric 	ump->metadata_flags = mapping->pmm.flags;
31359f988b79SJean-Baptiste Boric 
31369f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Reading in Metadata files\n"));
31379f988b79SJean-Baptiste Boric 	raw_phys_part = udf_rw16(pmm->part_num);
31389f988b79SJean-Baptiste Boric 	phys_part = udf_find_raw_phys(ump, raw_phys_part);
31399f988b79SJean-Baptiste Boric 
31409f988b79SJean-Baptiste Boric 	icb_loc.loc.part_num = udf_rw16(phys_part);
31419f988b79SJean-Baptiste Boric 
31429f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Metadata file\n"));
31439f988b79SJean-Baptiste Boric 	icb_loc.loc.lb_num   = pmm->meta_file_lbn;
31449f988b79SJean-Baptiste Boric 	error = udf_get_node(ump, &icb_loc, &ump->metadata_node);
31459f988b79SJean-Baptiste Boric 	if (ump->metadata_node) {
31469f988b79SJean-Baptiste Boric 		vp = ump->metadata_node->vnode;
31479f988b79SJean-Baptiste Boric 		UDF_SET_SYSTEMFILE(vp);
31489f988b79SJean-Baptiste Boric 	}
31499f988b79SJean-Baptiste Boric 
31509f988b79SJean-Baptiste Boric 	icb_loc.loc.lb_num   = pmm->meta_mirror_file_lbn;
31519f988b79SJean-Baptiste Boric 	if (icb_loc.loc.lb_num != -1) {
31529f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Metadata copy file\n"));
31539f988b79SJean-Baptiste Boric 		error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_node);
31549f988b79SJean-Baptiste Boric 		if (ump->metadatamirror_node) {
31559f988b79SJean-Baptiste Boric 			vp = ump->metadatamirror_node->vnode;
31569f988b79SJean-Baptiste Boric 			UDF_SET_SYSTEMFILE(vp);
31579f988b79SJean-Baptiste Boric 		}
31589f988b79SJean-Baptiste Boric 	}
31599f988b79SJean-Baptiste Boric 
31609f988b79SJean-Baptiste Boric 	icb_loc.loc.lb_num   = pmm->meta_bitmap_file_lbn;
31619f988b79SJean-Baptiste Boric 	if (icb_loc.loc.lb_num != -1) {
31629f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Metadata bitmap file\n"));
31639f988b79SJean-Baptiste Boric 		error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_node);
31649f988b79SJean-Baptiste Boric 		if (ump->metadatabitmap_node) {
31659f988b79SJean-Baptiste Boric 			vp = ump->metadatabitmap_node->vnode;
31669f988b79SJean-Baptiste Boric 			UDF_SET_SYSTEMFILE(vp);
31679f988b79SJean-Baptiste Boric 		}
31689f988b79SJean-Baptiste Boric 	}
31699f988b79SJean-Baptiste Boric 
31709f988b79SJean-Baptiste Boric 	/* if we're mounting read-only we relax the requirements */
31719f988b79SJean-Baptiste Boric 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) {
31729f988b79SJean-Baptiste Boric 		error = EFAULT;
31739f988b79SJean-Baptiste Boric 		if (ump->metadata_node)
31749f988b79SJean-Baptiste Boric 			error = 0;
31759f988b79SJean-Baptiste Boric 		if ((ump->metadata_node == NULL) && (ump->metadatamirror_node)) {
31769f988b79SJean-Baptiste Boric 			printf( "udf mount: Metadata file not readable, "
31779f988b79SJean-Baptiste Boric 				"substituting Metadata copy file\n");
31789f988b79SJean-Baptiste Boric 			ump->metadata_node = ump->metadatamirror_node;
31799f988b79SJean-Baptiste Boric 			ump->metadatamirror_node = NULL;
31809f988b79SJean-Baptiste Boric 			error = 0;
31819f988b79SJean-Baptiste Boric 		}
31829f988b79SJean-Baptiste Boric 	} else {
31839f988b79SJean-Baptiste Boric 		/* mounting read/write */
31849f988b79SJean-Baptiste Boric 		/* XXX DISABLED! metadata writing is not working yet XXX */
31859f988b79SJean-Baptiste Boric 		if (error)
31869f988b79SJean-Baptiste Boric 			error = EROFS;
31879f988b79SJean-Baptiste Boric 	}
31889f988b79SJean-Baptiste Boric 	DPRINTFIF(VOLUMES, error, ("udf mount: failed to read "
31899f988b79SJean-Baptiste Boric 				   "metadata files\n"));
31909f988b79SJean-Baptiste Boric 	return error;
31919f988b79SJean-Baptiste Boric }
31929f988b79SJean-Baptiste Boric 
31939f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
31949f988b79SJean-Baptiste Boric 
31959f988b79SJean-Baptiste Boric int
udf_read_vds_tables(struct udf_mount * ump)31969f988b79SJean-Baptiste Boric udf_read_vds_tables(struct udf_mount *ump)
31979f988b79SJean-Baptiste Boric {
31989f988b79SJean-Baptiste Boric 	union udf_pmap *mapping;
31999f988b79SJean-Baptiste Boric 	/* struct udf_args *args = &ump->mount_args; */
32009f988b79SJean-Baptiste Boric 	uint32_t n_pm;
32019f988b79SJean-Baptiste Boric 	uint32_t log_part;
32029f988b79SJean-Baptiste Boric 	uint8_t *pmap_pos;
32039f988b79SJean-Baptiste Boric 	int pmap_size;
32049f988b79SJean-Baptiste Boric 	int error;
32059f988b79SJean-Baptiste Boric 
32069f988b79SJean-Baptiste Boric 	/* Iterate (again) over the part mappings for locations   */
32079f988b79SJean-Baptiste Boric 	n_pm = udf_rw32(ump->logical_vol->n_pm);   /* num partmaps         */
32089f988b79SJean-Baptiste Boric 	pmap_pos =  ump->logical_vol->maps;
32099f988b79SJean-Baptiste Boric 
32109f988b79SJean-Baptiste Boric 	for (log_part = 0; log_part < n_pm; log_part++) {
32119f988b79SJean-Baptiste Boric 		mapping = (union udf_pmap *) pmap_pos;
32129f988b79SJean-Baptiste Boric 		switch (ump->vtop_tp[log_part]) {
32139f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_PHYS :
32149f988b79SJean-Baptiste Boric 			/* nothing */
32159f988b79SJean-Baptiste Boric 			break;
32169f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_VIRT :
32179f988b79SJean-Baptiste Boric 			/* search and load VAT */
32189f988b79SJean-Baptiste Boric 			error = udf_search_vat(ump, mapping);
32199f988b79SJean-Baptiste Boric 			if (error)
32209f988b79SJean-Baptiste Boric 				return ENOENT;
32219f988b79SJean-Baptiste Boric 			break;
32229f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_SPARABLE :
32239f988b79SJean-Baptiste Boric 			/* load one of the sparable tables */
32249f988b79SJean-Baptiste Boric 			error = udf_read_sparables(ump, mapping);
32259f988b79SJean-Baptiste Boric 			if (error)
32269f988b79SJean-Baptiste Boric 				return ENOENT;
32279f988b79SJean-Baptiste Boric 			break;
32289f988b79SJean-Baptiste Boric 		case UDF_VTOP_TYPE_META :
32299f988b79SJean-Baptiste Boric 			/* load the associated file descriptors */
32309f988b79SJean-Baptiste Boric 			error = udf_read_metadata_nodes(ump, mapping);
32319f988b79SJean-Baptiste Boric 			if (error)
32329f988b79SJean-Baptiste Boric 				return ENOENT;
32339f988b79SJean-Baptiste Boric 			break;
32349f988b79SJean-Baptiste Boric 		default:
32359f988b79SJean-Baptiste Boric 			break;
32369f988b79SJean-Baptiste Boric 		}
32379f988b79SJean-Baptiste Boric 		pmap_size  = pmap_pos[1];
32389f988b79SJean-Baptiste Boric 		pmap_pos  += pmap_size;
32399f988b79SJean-Baptiste Boric 	}
32409f988b79SJean-Baptiste Boric 
32419f988b79SJean-Baptiste Boric 	/* read in and check unallocated and free space info if writing */
32429f988b79SJean-Baptiste Boric 	if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY) == 0) {
32439f988b79SJean-Baptiste Boric 		error = udf_read_physical_partition_spacetables(ump);
32449f988b79SJean-Baptiste Boric 		if (error)
32459f988b79SJean-Baptiste Boric 			return error;
32469f988b79SJean-Baptiste Boric 
32479f988b79SJean-Baptiste Boric 		/* also read in metadata partition spacebitmap if defined */
32489f988b79SJean-Baptiste Boric 		error = udf_read_metadata_partition_spacetable(ump);
32499f988b79SJean-Baptiste Boric 			return error;
32509f988b79SJean-Baptiste Boric 	}
32519f988b79SJean-Baptiste Boric 
32529f988b79SJean-Baptiste Boric 	return 0;
32539f988b79SJean-Baptiste Boric }
32549f988b79SJean-Baptiste Boric 
32559f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
32569f988b79SJean-Baptiste Boric 
32579f988b79SJean-Baptiste Boric int
udf_read_rootdirs(struct udf_mount * ump)32589f988b79SJean-Baptiste Boric udf_read_rootdirs(struct udf_mount *ump)
32599f988b79SJean-Baptiste Boric {
32609f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
32619f988b79SJean-Baptiste Boric 	/* struct udf_args *args = &ump->mount_args; */
32629f988b79SJean-Baptiste Boric 	struct udf_node *rootdir_node, *streamdir_node;
32639f988b79SJean-Baptiste Boric 	struct long_ad  fsd_loc, *dir_loc;
32649f988b79SJean-Baptiste Boric 	uint32_t lb_num, dummy;
32659f988b79SJean-Baptiste Boric 	uint32_t fsd_len;
32669f988b79SJean-Baptiste Boric 	int dscr_type;
32679f988b79SJean-Baptiste Boric 	int error;
32689f988b79SJean-Baptiste Boric 
32699f988b79SJean-Baptiste Boric 	/* TODO implement FSD reading in separate function like integrity? */
32709f988b79SJean-Baptiste Boric 	/* get fileset descriptor sequence */
32719f988b79SJean-Baptiste Boric 	fsd_loc = ump->logical_vol->lv_fsd_loc;
32729f988b79SJean-Baptiste Boric 	fsd_len = udf_rw32(fsd_loc.len);
32739f988b79SJean-Baptiste Boric 
32749f988b79SJean-Baptiste Boric 	dscr  = NULL;
32759f988b79SJean-Baptiste Boric 	error = 0;
32769f988b79SJean-Baptiste Boric 	while (fsd_len || error) {
32779f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
32789f988b79SJean-Baptiste Boric 		/* translate fsd_loc to lb_num */
32799f988b79SJean-Baptiste Boric 		error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
32809f988b79SJean-Baptiste Boric 		if (error)
32819f988b79SJean-Baptiste Boric 			break;
32829f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
32839f988b79SJean-Baptiste Boric 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
32849f988b79SJean-Baptiste Boric 		/* end markers */
32859f988b79SJean-Baptiste Boric 		if (error || (dscr == NULL))
32869f988b79SJean-Baptiste Boric 			break;
32879f988b79SJean-Baptiste Boric 
32889f988b79SJean-Baptiste Boric 		/* analyse */
32899f988b79SJean-Baptiste Boric 		dscr_type = udf_rw16(dscr->tag.id);
32909f988b79SJean-Baptiste Boric 		if (dscr_type == TAGID_TERM)
32919f988b79SJean-Baptiste Boric 			break;
32929f988b79SJean-Baptiste Boric 		if (dscr_type != TAGID_FSD) {
32939f988b79SJean-Baptiste Boric 			free(dscr, M_UDFVOLD);
32949f988b79SJean-Baptiste Boric 			return ENOENT;
32959f988b79SJean-Baptiste Boric 		}
32969f988b79SJean-Baptiste Boric 
32979f988b79SJean-Baptiste Boric 		/*
32989f988b79SJean-Baptiste Boric 		 * TODO check for multiple fileset descriptors; its only
32999f988b79SJean-Baptiste Boric 		 * picking the last now. Also check for FSD
33009f988b79SJean-Baptiste Boric 		 * correctness/interpretability
33019f988b79SJean-Baptiste Boric 		 */
33029f988b79SJean-Baptiste Boric 
33039f988b79SJean-Baptiste Boric 		/* update */
33049f988b79SJean-Baptiste Boric 		if (ump->fileset_desc) {
33059f988b79SJean-Baptiste Boric 			free(ump->fileset_desc, M_UDFVOLD);
33069f988b79SJean-Baptiste Boric 		}
33079f988b79SJean-Baptiste Boric 		ump->fileset_desc = &dscr->fsd;
33089f988b79SJean-Baptiste Boric 		dscr = NULL;
33099f988b79SJean-Baptiste Boric 
33109f988b79SJean-Baptiste Boric 		/* continue to the next fsd */
33119f988b79SJean-Baptiste Boric 		fsd_len -= ump->discinfo.sector_size;
33129f988b79SJean-Baptiste Boric 		fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
33139f988b79SJean-Baptiste Boric 
33149f988b79SJean-Baptiste Boric 		/* follow up to fsd->next_ex (long_ad) if its not null */
33159f988b79SJean-Baptiste Boric 		if (udf_rw32(ump->fileset_desc->next_ex.len)) {
33169f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("follow up FSD extent\n"));
33179f988b79SJean-Baptiste Boric 			fsd_loc = ump->fileset_desc->next_ex;
33189f988b79SJean-Baptiste Boric 			fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
33199f988b79SJean-Baptiste Boric 		}
33209f988b79SJean-Baptiste Boric 	}
33219f988b79SJean-Baptiste Boric 	if (dscr)
33229f988b79SJean-Baptiste Boric 		free(dscr, M_UDFVOLD);
33239f988b79SJean-Baptiste Boric 
33249f988b79SJean-Baptiste Boric 	/* there has to be one */
33259f988b79SJean-Baptiste Boric 	if (ump->fileset_desc == NULL)
33269f988b79SJean-Baptiste Boric 		return ENOENT;
33279f988b79SJean-Baptiste Boric 
33289f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("FSD read in fine\n"));
33299f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Updating fsd logical volume id\n"));
33309f988b79SJean-Baptiste Boric 	udf_update_logvolname(ump, ump->logical_vol->logvol_id);
33319f988b79SJean-Baptiste Boric 
33329f988b79SJean-Baptiste Boric 	/*
33339f988b79SJean-Baptiste Boric 	 * Now the FSD is known, read in the rootdirectory and if one exists,
33349f988b79SJean-Baptiste Boric 	 * the system stream dir. Some files in the system streamdir are not
33359f988b79SJean-Baptiste Boric 	 * wanted in this implementation since they are not maintained. If
33369f988b79SJean-Baptiste Boric 	 * writing is enabled we'll delete these files if they exist.
33379f988b79SJean-Baptiste Boric 	 */
33389f988b79SJean-Baptiste Boric 
33399f988b79SJean-Baptiste Boric 	rootdir_node = streamdir_node = NULL;
33409f988b79SJean-Baptiste Boric 	dir_loc = NULL;
33419f988b79SJean-Baptiste Boric 
33429f988b79SJean-Baptiste Boric 	/* try to read in the rootdir */
33439f988b79SJean-Baptiste Boric 	dir_loc = &ump->fileset_desc->rootdir_icb;
33449f988b79SJean-Baptiste Boric 	error = udf_get_node(ump, dir_loc, &rootdir_node);
33459f988b79SJean-Baptiste Boric 	if (error)
33469f988b79SJean-Baptiste Boric 		return ENOENT;
33479f988b79SJean-Baptiste Boric 
33489f988b79SJean-Baptiste Boric 	/* aparently it read in fine */
33499f988b79SJean-Baptiste Boric 
33509f988b79SJean-Baptiste Boric 	/*
33519f988b79SJean-Baptiste Boric 	 * Try the system stream directory; not very likely in the ones we
33529f988b79SJean-Baptiste Boric 	 * test, but for completeness.
33539f988b79SJean-Baptiste Boric 	 */
33549f988b79SJean-Baptiste Boric 	dir_loc = &ump->fileset_desc->streamdir_icb;
33559f988b79SJean-Baptiste Boric 	if (udf_rw32(dir_loc->len)) {
33569f988b79SJean-Baptiste Boric 		printf("udf_read_rootdirs: streamdir defined ");
33579f988b79SJean-Baptiste Boric 		error = udf_get_node(ump, dir_loc, &streamdir_node);
33589f988b79SJean-Baptiste Boric 		if (error) {
33599f988b79SJean-Baptiste Boric 			printf("but error in streamdir reading\n");
33609f988b79SJean-Baptiste Boric 		} else {
33619f988b79SJean-Baptiste Boric 			printf("but ignored\n");
33629f988b79SJean-Baptiste Boric 			/*
33639f988b79SJean-Baptiste Boric 			 * TODO process streamdir `baddies' i.e. files we dont
33649f988b79SJean-Baptiste Boric 			 * want if R/W
33659f988b79SJean-Baptiste Boric 			 */
33669f988b79SJean-Baptiste Boric 		}
33679f988b79SJean-Baptiste Boric 	}
33689f988b79SJean-Baptiste Boric 
33699f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
33709f988b79SJean-Baptiste Boric 
33719f988b79SJean-Baptiste Boric 	/* release the vnodes again; they'll be auto-recycled later */
33729f988b79SJean-Baptiste Boric 	if (streamdir_node) {
33739f988b79SJean-Baptiste Boric 		vput(streamdir_node->vnode);
33749f988b79SJean-Baptiste Boric 	}
33759f988b79SJean-Baptiste Boric 	if (rootdir_node) {
33769f988b79SJean-Baptiste Boric 		vput(rootdir_node->vnode);
33779f988b79SJean-Baptiste Boric 	}
33789f988b79SJean-Baptiste Boric 
33799f988b79SJean-Baptiste Boric 	return 0;
33809f988b79SJean-Baptiste Boric }
33819f988b79SJean-Baptiste Boric 
33829f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
33839f988b79SJean-Baptiste Boric 
33849f988b79SJean-Baptiste Boric /* To make absolutely sure we are NOT returning zero, add one :) */
33859f988b79SJean-Baptiste Boric 
33869f988b79SJean-Baptiste Boric long
udf_get_node_id(const struct long_ad * icbptr)33879f988b79SJean-Baptiste Boric udf_get_node_id(const struct long_ad *icbptr)
33889f988b79SJean-Baptiste Boric {
33899f988b79SJean-Baptiste Boric 	/* ought to be enough since each mountpoint has its own chain */
33909f988b79SJean-Baptiste Boric 	return udf_rw32(icbptr->loc.lb_num) + 1;
33919f988b79SJean-Baptiste Boric }
33929f988b79SJean-Baptiste Boric 
33939f988b79SJean-Baptiste Boric 
33949f988b79SJean-Baptiste Boric int
udf_compare_icb(const struct long_ad * a,const struct long_ad * b)33959f988b79SJean-Baptiste Boric udf_compare_icb(const struct long_ad *a, const struct long_ad *b)
33969f988b79SJean-Baptiste Boric {
33979f988b79SJean-Baptiste Boric 	if (udf_rw16(a->loc.part_num) < udf_rw16(b->loc.part_num))
33989f988b79SJean-Baptiste Boric 		return -1;
33999f988b79SJean-Baptiste Boric 	if (udf_rw16(a->loc.part_num) > udf_rw16(b->loc.part_num))
34009f988b79SJean-Baptiste Boric 		return 1;
34019f988b79SJean-Baptiste Boric 
34029f988b79SJean-Baptiste Boric 	if (udf_rw32(a->loc.lb_num) < udf_rw32(b->loc.lb_num))
34039f988b79SJean-Baptiste Boric 		return -1;
34049f988b79SJean-Baptiste Boric 	if (udf_rw32(a->loc.lb_num) > udf_rw32(b->loc.lb_num))
34059f988b79SJean-Baptiste Boric 		return 1;
34069f988b79SJean-Baptiste Boric 
34079f988b79SJean-Baptiste Boric 	return 0;
34089f988b79SJean-Baptiste Boric }
34099f988b79SJean-Baptiste Boric 
34109f988b79SJean-Baptiste Boric 
34119f988b79SJean-Baptiste Boric static int
udf_compare_rbnodes(void * ctx,const void * a,const void * b)34129f988b79SJean-Baptiste Boric udf_compare_rbnodes(void *ctx, const void *a, const void *b)
34139f988b79SJean-Baptiste Boric {
34149f988b79SJean-Baptiste Boric 	const struct udf_node *a_node = a;
34159f988b79SJean-Baptiste Boric 	const struct udf_node *b_node = b;
34169f988b79SJean-Baptiste Boric 
34179f988b79SJean-Baptiste Boric 	return udf_compare_icb(&a_node->loc, &b_node->loc);
34189f988b79SJean-Baptiste Boric }
34199f988b79SJean-Baptiste Boric 
34209f988b79SJean-Baptiste Boric 
34219f988b79SJean-Baptiste Boric static int
udf_compare_rbnode_icb(void * ctx,const void * a,const void * key)34229f988b79SJean-Baptiste Boric udf_compare_rbnode_icb(void *ctx, const void *a, const void *key)
34239f988b79SJean-Baptiste Boric {
34249f988b79SJean-Baptiste Boric 	const struct udf_node *a_node = a;
34259f988b79SJean-Baptiste Boric 	const struct long_ad * const icb = key;
34269f988b79SJean-Baptiste Boric 
34279f988b79SJean-Baptiste Boric 	return udf_compare_icb(&a_node->loc, icb);
34289f988b79SJean-Baptiste Boric }
34299f988b79SJean-Baptiste Boric 
34309f988b79SJean-Baptiste Boric 
34319f988b79SJean-Baptiste Boric static const rb_tree_ops_t udf_node_rbtree_ops = {
34329f988b79SJean-Baptiste Boric 	.rbto_compare_nodes = udf_compare_rbnodes,
34339f988b79SJean-Baptiste Boric 	.rbto_compare_key = udf_compare_rbnode_icb,
34349f988b79SJean-Baptiste Boric 	.rbto_node_offset = offsetof(struct udf_node, rbnode),
34359f988b79SJean-Baptiste Boric 	.rbto_context = NULL
34369f988b79SJean-Baptiste Boric };
34379f988b79SJean-Baptiste Boric 
34389f988b79SJean-Baptiste Boric 
34399f988b79SJean-Baptiste Boric void
udf_init_nodes_tree(struct udf_mount * ump)34409f988b79SJean-Baptiste Boric udf_init_nodes_tree(struct udf_mount *ump)
34419f988b79SJean-Baptiste Boric {
34429f988b79SJean-Baptiste Boric 
34439f988b79SJean-Baptiste Boric 	rb_tree_init(&ump->udf_node_tree, &udf_node_rbtree_ops);
34449f988b79SJean-Baptiste Boric }
34459f988b79SJean-Baptiste Boric 
34469f988b79SJean-Baptiste Boric 
34479f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
34489f988b79SJean-Baptiste Boric 
34499f988b79SJean-Baptiste Boric static int
udf_validate_session_start(struct udf_mount * ump)34509f988b79SJean-Baptiste Boric udf_validate_session_start(struct udf_mount *ump)
34519f988b79SJean-Baptiste Boric {
34529f988b79SJean-Baptiste Boric 	struct mmc_trackinfo trackinfo;
34539f988b79SJean-Baptiste Boric 	struct vrs_desc *vrs;
34549f988b79SJean-Baptiste Boric 	uint32_t tracknr, sessionnr, sector, sector_size;
34559f988b79SJean-Baptiste Boric 	uint32_t iso9660_vrs, write_track_start;
34569f988b79SJean-Baptiste Boric 	uint8_t *buffer, *blank, *pos;
34579f988b79SJean-Baptiste Boric 	int blks, max_sectors, vrs_len;
34589f988b79SJean-Baptiste Boric 	int error;
34599f988b79SJean-Baptiste Boric 
34609f988b79SJean-Baptiste Boric 	/* disc appendable? */
34619f988b79SJean-Baptiste Boric 	if (ump->discinfo.disc_state == MMC_STATE_FULL)
34629f988b79SJean-Baptiste Boric 		return EROFS;
34639f988b79SJean-Baptiste Boric 
34649f988b79SJean-Baptiste Boric 	/* already written here? if so, there should be an ISO VDS */
34659f988b79SJean-Baptiste Boric 	if (ump->discinfo.last_session_state == MMC_STATE_INCOMPLETE)
34669f988b79SJean-Baptiste Boric 		return 0;
34679f988b79SJean-Baptiste Boric 
34689f988b79SJean-Baptiste Boric 	/*
34699f988b79SJean-Baptiste Boric 	 * Check if the first track of the session is blank and if so, copy or
34709f988b79SJean-Baptiste Boric 	 * create a dummy ISO descriptor so the disc is valid again.
34719f988b79SJean-Baptiste Boric 	 */
34729f988b79SJean-Baptiste Boric 
34739f988b79SJean-Baptiste Boric 	tracknr = ump->discinfo.first_track_last_session;
34749f988b79SJean-Baptiste Boric 	memset(&trackinfo, 0, sizeof(struct mmc_trackinfo));
34759f988b79SJean-Baptiste Boric 	trackinfo.tracknr = tracknr;
34769f988b79SJean-Baptiste Boric 	error = udf_update_trackinfo(ump, &trackinfo);
34779f988b79SJean-Baptiste Boric 	if (error)
34789f988b79SJean-Baptiste Boric 		return error;
34799f988b79SJean-Baptiste Boric 
34809f988b79SJean-Baptiste Boric 	udf_dump_trackinfo(&trackinfo);
34819f988b79SJean-Baptiste Boric 	KASSERT(trackinfo.flags & (MMC_TRACKINFO_BLANK | MMC_TRACKINFO_RESERVED));
34829f988b79SJean-Baptiste Boric 	KASSERT(trackinfo.sessionnr > 1);
34839f988b79SJean-Baptiste Boric 
34849f988b79SJean-Baptiste Boric 	KASSERT(trackinfo.flags & MMC_TRACKINFO_NWA_VALID);
34859f988b79SJean-Baptiste Boric 	write_track_start = trackinfo.next_writable;
34869f988b79SJean-Baptiste Boric 
34879f988b79SJean-Baptiste Boric 	/* we have to copy the ISO VRS from a former session */
34889f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("validate_session_start: "
34899f988b79SJean-Baptiste Boric 			"blank or reserved track, copying VRS\n"));
34909f988b79SJean-Baptiste Boric 
34919f988b79SJean-Baptiste Boric 	/* sessionnr should be the session we're mounting */
34929f988b79SJean-Baptiste Boric 	sessionnr = ump->mount_args.sessionnr;
34939f988b79SJean-Baptiste Boric 
34949f988b79SJean-Baptiste Boric 	/* start at the first track */
34959f988b79SJean-Baptiste Boric 	tracknr   = ump->discinfo.first_track;
34969f988b79SJean-Baptiste Boric 	while (tracknr <= ump->discinfo.num_tracks) {
34979f988b79SJean-Baptiste Boric 		trackinfo.tracknr = tracknr;
34989f988b79SJean-Baptiste Boric 		error = udf_update_trackinfo(ump, &trackinfo);
34999f988b79SJean-Baptiste Boric 		if (error) {
35009f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("failed to get trackinfo; aborting\n"));
35019f988b79SJean-Baptiste Boric 			return error;
35029f988b79SJean-Baptiste Boric 		}
35039f988b79SJean-Baptiste Boric 		if (trackinfo.sessionnr == sessionnr)
35049f988b79SJean-Baptiste Boric 			break;
35059f988b79SJean-Baptiste Boric 		tracknr++;
35069f988b79SJean-Baptiste Boric 	}
35079f988b79SJean-Baptiste Boric 	if (trackinfo.sessionnr != sessionnr) {
35089f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("failed to get trackinfo; aborting\n"));
35099f988b79SJean-Baptiste Boric 		return ENOENT;
35109f988b79SJean-Baptiste Boric 	}
35119f988b79SJean-Baptiste Boric 
35129f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("found possible former ISO VRS at\n"));
35139f988b79SJean-Baptiste Boric 	udf_dump_trackinfo(&trackinfo);
35149f988b79SJean-Baptiste Boric 
35159f988b79SJean-Baptiste Boric         /*
35169f988b79SJean-Baptiste Boric          * location of iso9660 vrs is defined as first sector AFTER 32kb,
35179f988b79SJean-Baptiste Boric          * minimum ISO `sector size' 2048
35189f988b79SJean-Baptiste Boric          */
35199f988b79SJean-Baptiste Boric 	sector_size = ump->discinfo.sector_size;
35209f988b79SJean-Baptiste Boric 	iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size)
35219f988b79SJean-Baptiste Boric 		 + trackinfo.track_start;
35229f988b79SJean-Baptiste Boric 
35239f988b79SJean-Baptiste Boric 	buffer = malloc(UDF_ISO_VRS_SIZE, M_TEMP, M_WAITOK);
35249f988b79SJean-Baptiste Boric 	max_sectors = UDF_ISO_VRS_SIZE / sector_size;
35259f988b79SJean-Baptiste Boric 	blks = MAX(1, 2048 / sector_size);
35269f988b79SJean-Baptiste Boric 
35279f988b79SJean-Baptiste Boric 	error = 0;
35289f988b79SJean-Baptiste Boric 	for (sector = 0; sector < max_sectors; sector += blks) {
35299f988b79SJean-Baptiste Boric 		pos = buffer + sector * sector_size;
35309f988b79SJean-Baptiste Boric 		error = udf_read_phys_sectors(ump, UDF_C_DSCR, pos,
35319f988b79SJean-Baptiste Boric 			iso9660_vrs + sector, blks);
35329f988b79SJean-Baptiste Boric 		if (error)
35339f988b79SJean-Baptiste Boric 			break;
35349f988b79SJean-Baptiste Boric 		/* check this ISO descriptor */
35359f988b79SJean-Baptiste Boric 		vrs = (struct vrs_desc *) pos;
35369f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("got VRS id `%4s`\n", vrs->identifier));
35379f988b79SJean-Baptiste Boric 		if (strncmp(vrs->identifier, VRS_CD001, 5) == 0)
35389f988b79SJean-Baptiste Boric 			continue;
35399f988b79SJean-Baptiste Boric 		if (strncmp(vrs->identifier, VRS_CDW02, 5) == 0)
35409f988b79SJean-Baptiste Boric 			continue;
35419f988b79SJean-Baptiste Boric 		if (strncmp(vrs->identifier, VRS_BEA01, 5) == 0)
35429f988b79SJean-Baptiste Boric 			continue;
35439f988b79SJean-Baptiste Boric 		if (strncmp(vrs->identifier, VRS_NSR02, 5) == 0)
35449f988b79SJean-Baptiste Boric 			continue;
35459f988b79SJean-Baptiste Boric 		if (strncmp(vrs->identifier, VRS_NSR03, 5) == 0)
35469f988b79SJean-Baptiste Boric 			continue;
35479f988b79SJean-Baptiste Boric 		if (strncmp(vrs->identifier, VRS_TEA01, 5) == 0)
35489f988b79SJean-Baptiste Boric 			break;
35499f988b79SJean-Baptiste Boric 		/* now what? for now, end of sequence */
35509f988b79SJean-Baptiste Boric 		break;
35519f988b79SJean-Baptiste Boric 	}
35529f988b79SJean-Baptiste Boric 	vrs_len = sector + blks;
35539f988b79SJean-Baptiste Boric 	if (error) {
35549f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("error reading old ISO VRS\n"));
35559f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("creating minimal ISO VRS\n"));
35569f988b79SJean-Baptiste Boric 
35579f988b79SJean-Baptiste Boric 		memset(buffer, 0, UDF_ISO_VRS_SIZE);
35589f988b79SJean-Baptiste Boric 
35599f988b79SJean-Baptiste Boric 		vrs = (struct vrs_desc *) (buffer);
35609f988b79SJean-Baptiste Boric 		vrs->struct_type = 0;
35619f988b79SJean-Baptiste Boric 		vrs->version     = 1;
35629f988b79SJean-Baptiste Boric 		memcpy(vrs->identifier,VRS_BEA01, 5);
35639f988b79SJean-Baptiste Boric 
35649f988b79SJean-Baptiste Boric 		vrs = (struct vrs_desc *) (buffer + 2048);
35659f988b79SJean-Baptiste Boric 		vrs->struct_type = 0;
35669f988b79SJean-Baptiste Boric 		vrs->version     = 1;
35679f988b79SJean-Baptiste Boric 		if (udf_rw16(ump->logical_vol->tag.descriptor_ver) == 2) {
35689f988b79SJean-Baptiste Boric 			memcpy(vrs->identifier,VRS_NSR02, 5);
35699f988b79SJean-Baptiste Boric 		} else {
35709f988b79SJean-Baptiste Boric 			memcpy(vrs->identifier,VRS_NSR03, 5);
35719f988b79SJean-Baptiste Boric 		}
35729f988b79SJean-Baptiste Boric 
35739f988b79SJean-Baptiste Boric 		vrs = (struct vrs_desc *) (buffer + 4096);
35749f988b79SJean-Baptiste Boric 		vrs->struct_type = 0;
35759f988b79SJean-Baptiste Boric 		vrs->version     = 1;
35769f988b79SJean-Baptiste Boric 		memcpy(vrs->identifier, VRS_TEA01, 5);
35779f988b79SJean-Baptiste Boric 
35789f988b79SJean-Baptiste Boric 		vrs_len = 3*blks;
35799f988b79SJean-Baptiste Boric 	}
35809f988b79SJean-Baptiste Boric 
35819f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Got VRS of %d sectors long\n", vrs_len));
35829f988b79SJean-Baptiste Boric 
35839f988b79SJean-Baptiste Boric         /*
35849f988b79SJean-Baptiste Boric          * location of iso9660 vrs is defined as first sector AFTER 32kb,
35859f988b79SJean-Baptiste Boric          * minimum ISO `sector size' 2048
35869f988b79SJean-Baptiste Boric          */
35879f988b79SJean-Baptiste Boric 	sector_size = ump->discinfo.sector_size;
35889f988b79SJean-Baptiste Boric 	iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size)
35899f988b79SJean-Baptiste Boric 		 + write_track_start;
35909f988b79SJean-Baptiste Boric 
35919f988b79SJean-Baptiste Boric 	/* write out 32 kb */
35929f988b79SJean-Baptiste Boric 	blank = malloc(sector_size, M_TEMP, M_WAITOK);
35939f988b79SJean-Baptiste Boric 	memset(blank, 0, sector_size);
35949f988b79SJean-Baptiste Boric 	error = 0;
35959f988b79SJean-Baptiste Boric 	for (sector = write_track_start; sector < iso9660_vrs; sector ++) {
35969f988b79SJean-Baptiste Boric 		error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE,
35979f988b79SJean-Baptiste Boric 			blank, sector, 1);
35989f988b79SJean-Baptiste Boric 		if (error)
35999f988b79SJean-Baptiste Boric 			break;
36009f988b79SJean-Baptiste Boric 	}
36019f988b79SJean-Baptiste Boric 	if (!error) {
36029f988b79SJean-Baptiste Boric 		/* write out our ISO VRS */
36039f988b79SJean-Baptiste Boric 		KASSERT(sector == iso9660_vrs);
36049f988b79SJean-Baptiste Boric 		error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE, buffer,
36059f988b79SJean-Baptiste Boric 				sector, vrs_len);
36069f988b79SJean-Baptiste Boric 		sector += vrs_len;
36079f988b79SJean-Baptiste Boric 	}
36089f988b79SJean-Baptiste Boric 	if (!error) {
36099f988b79SJean-Baptiste Boric 		/* fill upto the first anchor at S+256 */
36109f988b79SJean-Baptiste Boric 		for (; sector < write_track_start+256; sector++) {
36119f988b79SJean-Baptiste Boric 			error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE,
36129f988b79SJean-Baptiste Boric 				blank, sector, 1);
36139f988b79SJean-Baptiste Boric 			if (error)
36149f988b79SJean-Baptiste Boric 				break;
36159f988b79SJean-Baptiste Boric 		}
36169f988b79SJean-Baptiste Boric 	}
36179f988b79SJean-Baptiste Boric 	if (!error) {
36189f988b79SJean-Baptiste Boric 		/* write out anchor; write at ABSOLUTE place! */
36199f988b79SJean-Baptiste Boric 		error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_ABSOLUTE,
36209f988b79SJean-Baptiste Boric 			(union dscrptr *) ump->anchors[0], sector, sector);
36219f988b79SJean-Baptiste Boric 		if (error)
36229f988b79SJean-Baptiste Boric 			printf("writeout of anchor failed!\n");
36239f988b79SJean-Baptiste Boric 	}
36249f988b79SJean-Baptiste Boric 
36259f988b79SJean-Baptiste Boric 	free(blank, M_TEMP);
36269f988b79SJean-Baptiste Boric 	free(buffer, M_TEMP);
36279f988b79SJean-Baptiste Boric 
36289f988b79SJean-Baptiste Boric 	if (error)
36299f988b79SJean-Baptiste Boric 		printf("udf_open_session: error writing iso vrs! : "
36309f988b79SJean-Baptiste Boric 				"leaving disc in compromised state!\n");
36319f988b79SJean-Baptiste Boric 
36329f988b79SJean-Baptiste Boric 	/* synchronise device caches */
36339f988b79SJean-Baptiste Boric 	(void) udf_synchronise_caches(ump);
36349f988b79SJean-Baptiste Boric 
36359f988b79SJean-Baptiste Boric 	return error;
36369f988b79SJean-Baptiste Boric }
36379f988b79SJean-Baptiste Boric 
36389f988b79SJean-Baptiste Boric 
36399f988b79SJean-Baptiste Boric int
udf_open_logvol(struct udf_mount * ump)36409f988b79SJean-Baptiste Boric udf_open_logvol(struct udf_mount *ump)
36419f988b79SJean-Baptiste Boric {
36429f988b79SJean-Baptiste Boric 	int logvol_integrity;
36439f988b79SJean-Baptiste Boric 	int error;
36449f988b79SJean-Baptiste Boric 
36459f988b79SJean-Baptiste Boric 	/* already/still open? */
36469f988b79SJean-Baptiste Boric 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
36479f988b79SJean-Baptiste Boric 	if (logvol_integrity == UDF_INTEGRITY_OPEN)
36489f988b79SJean-Baptiste Boric 		return 0;
36499f988b79SJean-Baptiste Boric 
36509f988b79SJean-Baptiste Boric 	/* can we open it ? */
36519f988b79SJean-Baptiste Boric 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
36529f988b79SJean-Baptiste Boric 		return EROFS;
36539f988b79SJean-Baptiste Boric 
36549f988b79SJean-Baptiste Boric 	/* setup write parameters */
36559f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Setting up write parameters\n"));
36569f988b79SJean-Baptiste Boric 	if ((error = udf_setup_writeparams(ump)) != 0)
36579f988b79SJean-Baptiste Boric 		return error;
36589f988b79SJean-Baptiste Boric 
36599f988b79SJean-Baptiste Boric 	/* determine data and metadata tracks (most likely same) */
36609f988b79SJean-Baptiste Boric 	error = udf_search_writing_tracks(ump);
36619f988b79SJean-Baptiste Boric 	if (error) {
36629f988b79SJean-Baptiste Boric 		/* most likely lack of space */
36639f988b79SJean-Baptiste Boric 		printf("udf_open_logvol: error searching writing tracks\n");
36649f988b79SJean-Baptiste Boric 		return EROFS;
36659f988b79SJean-Baptiste Boric 	}
36669f988b79SJean-Baptiste Boric 
36679f988b79SJean-Baptiste Boric 	/* writeout/update lvint on disc or only in memory */
36689f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("Opening logical volume\n"));
36699f988b79SJean-Baptiste Boric 	if (ump->lvopen & UDF_OPEN_SESSION) {
36709f988b79SJean-Baptiste Boric 		/* TODO optional track reservation opening */
36719f988b79SJean-Baptiste Boric 		error = udf_validate_session_start(ump);
36729f988b79SJean-Baptiste Boric 		if (error)
36739f988b79SJean-Baptiste Boric 			return error;
36749f988b79SJean-Baptiste Boric 
36759f988b79SJean-Baptiste Boric 		/* determine data and metadata tracks again */
36769f988b79SJean-Baptiste Boric 		error = udf_search_writing_tracks(ump);
36779f988b79SJean-Baptiste Boric 	}
36789f988b79SJean-Baptiste Boric 
36799f988b79SJean-Baptiste Boric 	/* mark it open */
36809f988b79SJean-Baptiste Boric 	ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_OPEN);
36819f988b79SJean-Baptiste Boric 
36829f988b79SJean-Baptiste Boric 	/* do we need to write it out? */
36839f988b79SJean-Baptiste Boric 	if (ump->lvopen & UDF_WRITE_LVINT) {
36849f988b79SJean-Baptiste Boric 		error = udf_writeout_lvint(ump, ump->lvopen);
36859f988b79SJean-Baptiste Boric 		/* if we couldn't write it mark it closed again */
36869f988b79SJean-Baptiste Boric 		if (error) {
36879f988b79SJean-Baptiste Boric 			ump->logvol_integrity->integrity_type =
36889f988b79SJean-Baptiste Boric 						udf_rw32(UDF_INTEGRITY_CLOSED);
36899f988b79SJean-Baptiste Boric 			return error;
36909f988b79SJean-Baptiste Boric 		}
36919f988b79SJean-Baptiste Boric 	}
36929f988b79SJean-Baptiste Boric 
36939f988b79SJean-Baptiste Boric 	return 0;
36949f988b79SJean-Baptiste Boric }
36959f988b79SJean-Baptiste Boric 
36969f988b79SJean-Baptiste Boric 
36979f988b79SJean-Baptiste Boric int
udf_close_logvol(struct udf_mount * ump,int mntflags)36989f988b79SJean-Baptiste Boric udf_close_logvol(struct udf_mount *ump, int mntflags)
36999f988b79SJean-Baptiste Boric {
37009f988b79SJean-Baptiste Boric 	struct vnode *devvp = ump->devvp;
37019f988b79SJean-Baptiste Boric 	struct mmc_op mmc_op;
37029f988b79SJean-Baptiste Boric 	int logvol_integrity;
37039f988b79SJean-Baptiste Boric 	int error = 0, error1 = 0, error2 = 0;
37049f988b79SJean-Baptiste Boric 	int tracknr;
37059f988b79SJean-Baptiste Boric 	int nvats, n, nok;
37069f988b79SJean-Baptiste Boric 
37079f988b79SJean-Baptiste Boric 	/* already/still closed? */
37089f988b79SJean-Baptiste Boric 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
37099f988b79SJean-Baptiste Boric 	if (logvol_integrity == UDF_INTEGRITY_CLOSED)
37109f988b79SJean-Baptiste Boric 		return 0;
37119f988b79SJean-Baptiste Boric 
37129f988b79SJean-Baptiste Boric 	/* writeout/update lvint or write out VAT */
37139f988b79SJean-Baptiste Boric 	DPRINTF(VOLUMES, ("udf_close_logvol: closing logical volume\n"));
37149f988b79SJean-Baptiste Boric #ifdef DIAGNOSTIC
37159f988b79SJean-Baptiste Boric 	if (ump->lvclose & UDF_CLOSE_SESSION)
37169f988b79SJean-Baptiste Boric 		KASSERT(ump->lvclose & UDF_WRITE_VAT);
37179f988b79SJean-Baptiste Boric #endif
37189f988b79SJean-Baptiste Boric 
37199f988b79SJean-Baptiste Boric 	if (ump->lvclose & UDF_WRITE_VAT) {
37209f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("lvclose & UDF_WRITE_VAT\n"));
37219f988b79SJean-Baptiste Boric 
37229f988b79SJean-Baptiste Boric 		/* write out the VAT data and all its descriptors */
37239f988b79SJean-Baptiste Boric 		DPRINTF(VOLUMES, ("writeout vat_node\n"));
37249f988b79SJean-Baptiste Boric 		udf_writeout_vat(ump);
37259f988b79SJean-Baptiste Boric 		(void) vflushbuf(ump->vat_node->vnode, FSYNC_WAIT);
37269f988b79SJean-Baptiste Boric 
37279f988b79SJean-Baptiste Boric 		(void) VOP_FSYNC(ump->vat_node->vnode,
37289f988b79SJean-Baptiste Boric 				FSCRED, FSYNC_WAIT, 0, 0);
37299f988b79SJean-Baptiste Boric 
37309f988b79SJean-Baptiste Boric 		if (ump->lvclose & UDF_CLOSE_SESSION) {
37319f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("udf_close_logvol: closing session "
37329f988b79SJean-Baptiste Boric 				"as requested\n"));
37339f988b79SJean-Baptiste Boric 		}
37349f988b79SJean-Baptiste Boric 
37359f988b79SJean-Baptiste Boric 		/* at least two DVD packets and 3 CD-R packets */
37369f988b79SJean-Baptiste Boric 		nvats = 32;
37379f988b79SJean-Baptiste Boric 
37389f988b79SJean-Baptiste Boric #if notyet
37399f988b79SJean-Baptiste Boric 		/*
37409f988b79SJean-Baptiste Boric 		 * TODO calculate the available space and if the disc is
37419f988b79SJean-Baptiste Boric 		 * allmost full, write out till end-256-1 with banks, write
37429f988b79SJean-Baptiste Boric 		 * AVDP and fill up with VATs, then close session and close
37439f988b79SJean-Baptiste Boric 		 * disc.
37449f988b79SJean-Baptiste Boric 		 */
37459f988b79SJean-Baptiste Boric 		if (ump->lvclose & UDF_FINALISE_DISC) {
37469f988b79SJean-Baptiste Boric 			error = udf_write_phys_dscr_sync(ump, NULL,
37479f988b79SJean-Baptiste Boric 					UDF_C_FLOAT_DSCR,
37489f988b79SJean-Baptiste Boric 					(union dscrptr *) ump->anchors[0],
37499f988b79SJean-Baptiste Boric 					0, 0);
37509f988b79SJean-Baptiste Boric 			if (error)
37519f988b79SJean-Baptiste Boric 				printf("writeout of anchor failed!\n");
37529f988b79SJean-Baptiste Boric 
37539f988b79SJean-Baptiste Boric 			/* pad space with VAT ICBs */
37549f988b79SJean-Baptiste Boric 			nvats = 256;
37559f988b79SJean-Baptiste Boric 		}
37569f988b79SJean-Baptiste Boric #endif
37579f988b79SJean-Baptiste Boric 
37589f988b79SJean-Baptiste Boric 		/* write out a number of VAT nodes */
37599f988b79SJean-Baptiste Boric 		nok = 0;
37609f988b79SJean-Baptiste Boric 		for (n = 0; n < nvats; n++) {
37619f988b79SJean-Baptiste Boric 			/* will now only write last FE/EFE */
37629f988b79SJean-Baptiste Boric 			ump->vat_node->i_flags |= IN_MODIFIED;
37639f988b79SJean-Baptiste Boric 			error = VOP_FSYNC(ump->vat_node->vnode,
37649f988b79SJean-Baptiste Boric 					FSCRED, FSYNC_WAIT, 0, 0);
37659f988b79SJean-Baptiste Boric 			if (!error)
37669f988b79SJean-Baptiste Boric 				nok++;
37679f988b79SJean-Baptiste Boric 		}
37689f988b79SJean-Baptiste Boric 		if (nok < 14) {
37699f988b79SJean-Baptiste Boric 			/* arbitrary; but at least one or two CD frames */
37709f988b79SJean-Baptiste Boric 			printf("writeout of at least 14 VATs failed\n");
37719f988b79SJean-Baptiste Boric 			return error;
37729f988b79SJean-Baptiste Boric 		}
37739f988b79SJean-Baptiste Boric 	}
37749f988b79SJean-Baptiste Boric 
37759f988b79SJean-Baptiste Boric 	/* NOTE the disc is in a (minimal) valid state now; no erroring out */
37769f988b79SJean-Baptiste Boric 
37779f988b79SJean-Baptiste Boric 	/* finish closing of session */
37789f988b79SJean-Baptiste Boric 	if (ump->lvclose & UDF_CLOSE_SESSION) {
37799f988b79SJean-Baptiste Boric 		error = udf_validate_session_start(ump);
37809f988b79SJean-Baptiste Boric 		if (error)
37819f988b79SJean-Baptiste Boric 			return error;
37829f988b79SJean-Baptiste Boric 
37839f988b79SJean-Baptiste Boric 		(void) udf_synchronise_caches(ump);
37849f988b79SJean-Baptiste Boric 
37859f988b79SJean-Baptiste Boric 		/* close all associated tracks */
37869f988b79SJean-Baptiste Boric 		tracknr = ump->discinfo.first_track_last_session;
37879f988b79SJean-Baptiste Boric 		error = 0;
37889f988b79SJean-Baptiste Boric 		while (tracknr <= ump->discinfo.last_track_last_session) {
37899f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("\tclosing possible open "
37909f988b79SJean-Baptiste Boric 				"track %d\n", tracknr));
37919f988b79SJean-Baptiste Boric 			memset(&mmc_op, 0, sizeof(mmc_op));
37929f988b79SJean-Baptiste Boric 			mmc_op.operation   = MMC_OP_CLOSETRACK;
37939f988b79SJean-Baptiste Boric 			mmc_op.mmc_profile = ump->discinfo.mmc_profile;
37949f988b79SJean-Baptiste Boric 			mmc_op.tracknr     = tracknr;
37959f988b79SJean-Baptiste Boric 			error = VOP_IOCTL(devvp, MMCOP, &mmc_op,
37969f988b79SJean-Baptiste Boric 					FKIOCTL, NOCRED);
37979f988b79SJean-Baptiste Boric 			if (error)
37989f988b79SJean-Baptiste Boric 				printf("udf_close_logvol: closing of "
37999f988b79SJean-Baptiste Boric 					"track %d failed\n", tracknr);
38009f988b79SJean-Baptiste Boric 			tracknr ++;
38019f988b79SJean-Baptiste Boric 		}
38029f988b79SJean-Baptiste Boric 		if (!error) {
38039f988b79SJean-Baptiste Boric 			DPRINTF(VOLUMES, ("closing session\n"));
38049f988b79SJean-Baptiste Boric 			memset(&mmc_op, 0, sizeof(mmc_op));
38059f988b79SJean-Baptiste Boric 			mmc_op.operation   = MMC_OP_CLOSESESSION;
38069f988b79SJean-Baptiste Boric 			mmc_op.mmc_profile = ump->discinfo.mmc_profile;
38079f988b79SJean-Baptiste Boric 			mmc_op.sessionnr   = ump->discinfo.num_sessions;
38089f988b79SJean-Baptiste Boric 			error = VOP_IOCTL(devvp, MMCOP, &mmc_op,
38099f988b79SJean-Baptiste Boric 					FKIOCTL, NOCRED);
38109f988b79SJean-Baptiste Boric 			if (error)
38119f988b79SJean-Baptiste Boric 				printf("udf_close_logvol: closing of session"
38129f988b79SJean-Baptiste Boric 						"failed\n");
38139f988b79SJean-Baptiste Boric 		}
38149f988b79SJean-Baptiste Boric 		if (!error)
38159f988b79SJean-Baptiste Boric 			ump->lvopen |= UDF_OPEN_SESSION;
38169f988b79SJean-Baptiste Boric 		if (error) {
38179f988b79SJean-Baptiste Boric 			printf("udf_close_logvol: leaving disc as it is\n");
38189f988b79SJean-Baptiste Boric 			ump->lvclose &= ~UDF_FINALISE_DISC;
38199f988b79SJean-Baptiste Boric 		}
38209f988b79SJean-Baptiste Boric 	}
38219f988b79SJean-Baptiste Boric 
38229f988b79SJean-Baptiste Boric 	if (ump->lvclose & UDF_FINALISE_DISC) {
38239f988b79SJean-Baptiste Boric 		memset(&mmc_op, 0, sizeof(mmc_op));
38249f988b79SJean-Baptiste Boric 		mmc_op.operation   = MMC_OP_FINALISEDISC;
38259f988b79SJean-Baptiste Boric 		mmc_op.mmc_profile = ump->discinfo.mmc_profile;
38269f988b79SJean-Baptiste Boric 		mmc_op.sessionnr   = ump->discinfo.num_sessions;
38279f988b79SJean-Baptiste Boric 		error = VOP_IOCTL(devvp, MMCOP, &mmc_op,
38289f988b79SJean-Baptiste Boric 				FKIOCTL, NOCRED);
38299f988b79SJean-Baptiste Boric 		if (error)
38309f988b79SJean-Baptiste Boric 			printf("udf_close_logvol: finalising disc"
38319f988b79SJean-Baptiste Boric 					"failed\n");
38329f988b79SJean-Baptiste Boric 	}
38339f988b79SJean-Baptiste Boric 
38349f988b79SJean-Baptiste Boric 	/* write out partition bitmaps if requested */
38359f988b79SJean-Baptiste Boric 	if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
38369f988b79SJean-Baptiste Boric 		/* sync writeout metadata spacetable if existing */
38379f988b79SJean-Baptiste Boric 		error1 = udf_write_metadata_partition_spacetable(ump, true);
38389f988b79SJean-Baptiste Boric 		if (error1)
38399f988b79SJean-Baptiste Boric 			printf( "udf_close_logvol: writeout of metadata space "
38409f988b79SJean-Baptiste Boric 				"bitmap failed\n");
38419f988b79SJean-Baptiste Boric 
38429f988b79SJean-Baptiste Boric 		/* sync writeout partition spacetables */
38439f988b79SJean-Baptiste Boric 		error2 = udf_write_physical_partition_spacetables(ump, true);
38449f988b79SJean-Baptiste Boric 		if (error2)
38459f988b79SJean-Baptiste Boric 			printf( "udf_close_logvol: writeout of space tables "
38469f988b79SJean-Baptiste Boric 				"failed\n");
38479f988b79SJean-Baptiste Boric 
38489f988b79SJean-Baptiste Boric 		if (error1 || error2)
38499f988b79SJean-Baptiste Boric 			return (error1 | error2);
38509f988b79SJean-Baptiste Boric 
38519f988b79SJean-Baptiste Boric 		ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
38529f988b79SJean-Baptiste Boric 	}
38539f988b79SJean-Baptiste Boric 
38549f988b79SJean-Baptiste Boric 	/* write out metadata partition nodes if requested */
38559f988b79SJean-Baptiste Boric 	if (ump->lvclose & UDF_WRITE_METAPART_NODES) {
38569f988b79SJean-Baptiste Boric 		/* sync writeout metadata descriptor node */
38579f988b79SJean-Baptiste Boric 		error1 = udf_writeout_node(ump->metadata_node, FSYNC_WAIT);
38589f988b79SJean-Baptiste Boric 		if (error1)
38599f988b79SJean-Baptiste Boric 			printf( "udf_close_logvol: writeout of metadata partition "
38609f988b79SJean-Baptiste Boric 				"node failed\n");
38619f988b79SJean-Baptiste Boric 
38629f988b79SJean-Baptiste Boric 		/* duplicate metadata partition descriptor if needed */
38639f988b79SJean-Baptiste Boric 		udf_synchronise_metadatamirror_node(ump);
38649f988b79SJean-Baptiste Boric 
38659f988b79SJean-Baptiste Boric 		/* sync writeout metadatamirror descriptor node */
38669f988b79SJean-Baptiste Boric 		error2 = udf_writeout_node(ump->metadatamirror_node, FSYNC_WAIT);
38679f988b79SJean-Baptiste Boric 		if (error2)
38689f988b79SJean-Baptiste Boric 			printf( "udf_close_logvol: writeout of metadata partition "
38699f988b79SJean-Baptiste Boric 				"mirror node failed\n");
38709f988b79SJean-Baptiste Boric 
38719f988b79SJean-Baptiste Boric 		if (error1 || error2)
38729f988b79SJean-Baptiste Boric 			return (error1 | error2);
38739f988b79SJean-Baptiste Boric 
38749f988b79SJean-Baptiste Boric 		ump->lvclose &= ~UDF_WRITE_METAPART_NODES;
38759f988b79SJean-Baptiste Boric 	}
38769f988b79SJean-Baptiste Boric 
38779f988b79SJean-Baptiste Boric 	/* mark it closed */
38789f988b79SJean-Baptiste Boric 	ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
38799f988b79SJean-Baptiste Boric 
38809f988b79SJean-Baptiste Boric 	/* do we need to write out the logical volume integrity? */
38819f988b79SJean-Baptiste Boric 	if (ump->lvclose & UDF_WRITE_LVINT)
38829f988b79SJean-Baptiste Boric 		error = udf_writeout_lvint(ump, ump->lvopen);
38839f988b79SJean-Baptiste Boric 	if (error) {
38849f988b79SJean-Baptiste Boric 		/* HELP now what? mark it open again for now */
38859f988b79SJean-Baptiste Boric 		ump->logvol_integrity->integrity_type =
38869f988b79SJean-Baptiste Boric 			udf_rw32(UDF_INTEGRITY_OPEN);
38879f988b79SJean-Baptiste Boric 		return error;
38889f988b79SJean-Baptiste Boric 	}
38899f988b79SJean-Baptiste Boric 
38909f988b79SJean-Baptiste Boric 	(void) udf_synchronise_caches(ump);
38919f988b79SJean-Baptiste Boric 
38929f988b79SJean-Baptiste Boric 	return 0;
38939f988b79SJean-Baptiste Boric }
38949f988b79SJean-Baptiste Boric 
38959f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
38969f988b79SJean-Baptiste Boric 
38979f988b79SJean-Baptiste Boric /*
38989f988b79SJean-Baptiste Boric  * Genfs interfacing
38999f988b79SJean-Baptiste Boric  *
39009f988b79SJean-Baptiste Boric  * static const struct genfs_ops udf_genfsops = {
39019f988b79SJean-Baptiste Boric  * 	.gop_size = genfs_size,
39029f988b79SJean-Baptiste Boric  * 		size of transfers
39039f988b79SJean-Baptiste Boric  * 	.gop_alloc = udf_gop_alloc,
39049f988b79SJean-Baptiste Boric  * 		allocate len bytes at offset
39059f988b79SJean-Baptiste Boric  * 	.gop_write = genfs_gop_write,
39069f988b79SJean-Baptiste Boric  * 		putpages interface code
39079f988b79SJean-Baptiste Boric  * 	.gop_markupdate = udf_gop_markupdate,
39089f988b79SJean-Baptiste Boric  * 		set update/modify flags etc.
39099f988b79SJean-Baptiste Boric  * }
39109f988b79SJean-Baptiste Boric  */
39119f988b79SJean-Baptiste Boric 
39129f988b79SJean-Baptiste Boric /*
39139f988b79SJean-Baptiste Boric  * Genfs interface. These four functions are the only ones defined though not
39149f988b79SJean-Baptiste Boric  * documented... great....
39159f988b79SJean-Baptiste Boric  */
39169f988b79SJean-Baptiste Boric 
39179f988b79SJean-Baptiste Boric /*
39189f988b79SJean-Baptiste Boric  * Called for allocating an extent of the file either by VOP_WRITE() or by
39199f988b79SJean-Baptiste Boric  * genfs filling up gaps.
39209f988b79SJean-Baptiste Boric  */
39219f988b79SJean-Baptiste Boric static int
udf_gop_alloc(struct vnode * vp,off_t off,off_t len,int flags,kauth_cred_t cred)39229f988b79SJean-Baptiste Boric udf_gop_alloc(struct vnode *vp, off_t off,
39239f988b79SJean-Baptiste Boric     off_t len, int flags, kauth_cred_t cred)
39249f988b79SJean-Baptiste Boric {
39259f988b79SJean-Baptiste Boric 	struct udf_node *udf_node = VTOI(vp);
39269f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
39279f988b79SJean-Baptiste Boric 	uint64_t lb_start, lb_end;
39289f988b79SJean-Baptiste Boric 	uint32_t lb_size, num_lb;
39299f988b79SJean-Baptiste Boric 	int udf_c_type, vpart_num, can_fail;
39309f988b79SJean-Baptiste Boric 	int error;
39319f988b79SJean-Baptiste Boric 
39329f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("udf_gop_alloc called for offset %"PRIu64" for %"PRIu64" bytes, %s\n",
39339f988b79SJean-Baptiste Boric 		off, len, flags? "SYNC":"NONE"));
39349f988b79SJean-Baptiste Boric 
39359f988b79SJean-Baptiste Boric 	/*
39369f988b79SJean-Baptiste Boric 	 * request the pages of our vnode and see how many pages will need to
39379f988b79SJean-Baptiste Boric 	 * be allocated and reserve that space
39389f988b79SJean-Baptiste Boric 	 */
39399f988b79SJean-Baptiste Boric 	lb_size  = udf_rw32(udf_node->ump->logical_vol->lb_size);
39409f988b79SJean-Baptiste Boric 	lb_start = off / lb_size;
39419f988b79SJean-Baptiste Boric 	lb_end   = (off + len + lb_size -1) / lb_size;
39429f988b79SJean-Baptiste Boric 	num_lb   = lb_end - lb_start;
39439f988b79SJean-Baptiste Boric 
39449f988b79SJean-Baptiste Boric 	udf_c_type = udf_get_c_type(udf_node);
39459f988b79SJean-Baptiste Boric 	vpart_num  = udf_get_record_vpart(ump, udf_c_type);
39469f988b79SJean-Baptiste Boric 
39479f988b79SJean-Baptiste Boric 	/* all requests can fail */
39489f988b79SJean-Baptiste Boric 	can_fail   = true;
39499f988b79SJean-Baptiste Boric 
39509f988b79SJean-Baptiste Boric 	/* fid's (directories) can't fail */
39519f988b79SJean-Baptiste Boric 	if (udf_c_type == UDF_C_FIDS)
39529f988b79SJean-Baptiste Boric 		can_fail   = false;
39539f988b79SJean-Baptiste Boric 
39549f988b79SJean-Baptiste Boric 	/* system files can't fail */
39559f988b79SJean-Baptiste Boric 	if (vp->v_vflag & VV_SYSTEM)
39569f988b79SJean-Baptiste Boric 		can_fail = false;
39579f988b79SJean-Baptiste Boric 
39589f988b79SJean-Baptiste Boric 	error = udf_reserve_space(ump, udf_node, udf_c_type,
39599f988b79SJean-Baptiste Boric 		vpart_num, num_lb, can_fail);
39609f988b79SJean-Baptiste Boric 
39619f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("\tlb_start %"PRIu64", lb_end %"PRIu64", num_lb %d\n",
39629f988b79SJean-Baptiste Boric 		lb_start, lb_end, num_lb));
39639f988b79SJean-Baptiste Boric 
39649f988b79SJean-Baptiste Boric 	return error;
39659f988b79SJean-Baptiste Boric }
39669f988b79SJean-Baptiste Boric 
39679f988b79SJean-Baptiste Boric 
39689f988b79SJean-Baptiste Boric /*
39699f988b79SJean-Baptiste Boric  * callback from genfs to update our flags
39709f988b79SJean-Baptiste Boric  */
39719f988b79SJean-Baptiste Boric static void
udf_gop_markupdate(struct vnode * vp,int flags)39729f988b79SJean-Baptiste Boric udf_gop_markupdate(struct vnode *vp, int flags)
39739f988b79SJean-Baptiste Boric {
39749f988b79SJean-Baptiste Boric 	struct udf_node *udf_node = VTOI(vp);
39759f988b79SJean-Baptiste Boric 	u_long mask = 0;
39769f988b79SJean-Baptiste Boric 
39779f988b79SJean-Baptiste Boric 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
39789f988b79SJean-Baptiste Boric 		mask = IN_ACCESS;
39799f988b79SJean-Baptiste Boric 	}
39809f988b79SJean-Baptiste Boric 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
39819f988b79SJean-Baptiste Boric 		if (vp->v_type == VREG) {
39829f988b79SJean-Baptiste Boric 			mask |= IN_CHANGE | IN_UPDATE;
39839f988b79SJean-Baptiste Boric 		} else {
39849f988b79SJean-Baptiste Boric 			mask |= IN_MODIFY;
39859f988b79SJean-Baptiste Boric 		}
39869f988b79SJean-Baptiste Boric 	}
39879f988b79SJean-Baptiste Boric 	if (mask) {
39889f988b79SJean-Baptiste Boric 		udf_node->i_flags |= mask;
39899f988b79SJean-Baptiste Boric 	}
39909f988b79SJean-Baptiste Boric }
39919f988b79SJean-Baptiste Boric 
39929f988b79SJean-Baptiste Boric 
39939f988b79SJean-Baptiste Boric static const struct genfs_ops udf_genfsops = {
39949f988b79SJean-Baptiste Boric 	.gop_size = genfs_size,
39959f988b79SJean-Baptiste Boric 	.gop_alloc = udf_gop_alloc,
39969f988b79SJean-Baptiste Boric 	.gop_write = genfs_gop_write_rwmap,
39979f988b79SJean-Baptiste Boric 	.gop_markupdate = udf_gop_markupdate,
39989f988b79SJean-Baptiste Boric };
39999f988b79SJean-Baptiste Boric 
40009f988b79SJean-Baptiste Boric 
40019f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
40029f988b79SJean-Baptiste Boric 
40039f988b79SJean-Baptiste Boric int
udf_write_terminator(struct udf_mount * ump,uint32_t sector)40049f988b79SJean-Baptiste Boric udf_write_terminator(struct udf_mount *ump, uint32_t sector)
40059f988b79SJean-Baptiste Boric {
40069f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
40079f988b79SJean-Baptiste Boric 	int error;
40089f988b79SJean-Baptiste Boric 
40099f988b79SJean-Baptiste Boric 	dscr = malloc(ump->discinfo.sector_size, M_TEMP, M_WAITOK|M_ZERO);
40109f988b79SJean-Baptiste Boric 	udf_inittag(ump, &dscr->tag, TAGID_TERM, sector);
40119f988b79SJean-Baptiste Boric 
40129f988b79SJean-Baptiste Boric 	/* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
40139f988b79SJean-Baptiste Boric 	dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
40149f988b79SJean-Baptiste Boric 	(void) udf_validate_tag_and_crc_sums(dscr);
40159f988b79SJean-Baptiste Boric 
40169f988b79SJean-Baptiste Boric 	error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
40179f988b79SJean-Baptiste Boric 			dscr, sector, sector);
40189f988b79SJean-Baptiste Boric 
40199f988b79SJean-Baptiste Boric 	free(dscr, M_TEMP);
40209f988b79SJean-Baptiste Boric 
40219f988b79SJean-Baptiste Boric 	return error;
40229f988b79SJean-Baptiste Boric }
40239f988b79SJean-Baptiste Boric 
40249f988b79SJean-Baptiste Boric 
40259f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
40269f988b79SJean-Baptiste Boric 
40279f988b79SJean-Baptiste Boric /* UDF<->unix converters */
40289f988b79SJean-Baptiste Boric 
40299f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
40309f988b79SJean-Baptiste Boric 
40319f988b79SJean-Baptiste Boric static mode_t
udf_perm_to_unix_mode(uint32_t perm)40329f988b79SJean-Baptiste Boric udf_perm_to_unix_mode(uint32_t perm)
40339f988b79SJean-Baptiste Boric {
40349f988b79SJean-Baptiste Boric 	mode_t mode;
40359f988b79SJean-Baptiste Boric 
40369f988b79SJean-Baptiste Boric 	mode  = ((perm & UDF_FENTRY_PERM_USER_MASK)      );
40379f988b79SJean-Baptiste Boric 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK  ) >> 2);
40389f988b79SJean-Baptiste Boric 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
40399f988b79SJean-Baptiste Boric 
40409f988b79SJean-Baptiste Boric 	return mode;
40419f988b79SJean-Baptiste Boric }
40429f988b79SJean-Baptiste Boric 
40439f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
40449f988b79SJean-Baptiste Boric 
40459f988b79SJean-Baptiste Boric static uint32_t
unix_mode_to_udf_perm(mode_t mode)40469f988b79SJean-Baptiste Boric unix_mode_to_udf_perm(mode_t mode)
40479f988b79SJean-Baptiste Boric {
40489f988b79SJean-Baptiste Boric 	uint32_t perm;
40499f988b79SJean-Baptiste Boric 
40509f988b79SJean-Baptiste Boric 	perm  = ((mode & S_IRWXO)     );
40519f988b79SJean-Baptiste Boric 	perm |= ((mode & S_IRWXG) << 2);
40529f988b79SJean-Baptiste Boric 	perm |= ((mode & S_IRWXU) << 4);
40539f988b79SJean-Baptiste Boric 	perm |= ((mode & S_IWOTH) << 3);
40549f988b79SJean-Baptiste Boric 	perm |= ((mode & S_IWGRP) << 5);
40559f988b79SJean-Baptiste Boric 	perm |= ((mode & S_IWUSR) << 7);
40569f988b79SJean-Baptiste Boric 
40579f988b79SJean-Baptiste Boric 	return perm;
40589f988b79SJean-Baptiste Boric }
40599f988b79SJean-Baptiste Boric 
40609f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
40619f988b79SJean-Baptiste Boric 
40629f988b79SJean-Baptiste Boric static uint32_t
udf_icb_to_unix_filetype(uint32_t icbftype)40639f988b79SJean-Baptiste Boric udf_icb_to_unix_filetype(uint32_t icbftype)
40649f988b79SJean-Baptiste Boric {
40659f988b79SJean-Baptiste Boric 	switch (icbftype) {
40669f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_DIRECTORY :
40679f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_STREAMDIR :
40689f988b79SJean-Baptiste Boric 		return S_IFDIR;
40699f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_FIFO :
40709f988b79SJean-Baptiste Boric 		return S_IFIFO;
40719f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_CHARDEVICE :
40729f988b79SJean-Baptiste Boric 		return S_IFCHR;
40739f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_BLOCKDEVICE :
40749f988b79SJean-Baptiste Boric 		return S_IFBLK;
40759f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_RANDOMACCESS :
40769f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_REALTIME :
40779f988b79SJean-Baptiste Boric 		return S_IFREG;
40789f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_SYMLINK :
40799f988b79SJean-Baptiste Boric 		return S_IFLNK;
40809f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_SOCKET :
40819f988b79SJean-Baptiste Boric 		return S_IFSOCK;
40829f988b79SJean-Baptiste Boric 	}
40839f988b79SJean-Baptiste Boric 	/* no idea what this is */
40849f988b79SJean-Baptiste Boric 	return 0;
40859f988b79SJean-Baptiste Boric }
40869f988b79SJean-Baptiste Boric 
40879f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
40889f988b79SJean-Baptiste Boric 
40899f988b79SJean-Baptiste Boric void
udf_to_unix_name(char * result,int result_len,char * id,int len,struct charspec * chsp)40909f988b79SJean-Baptiste Boric udf_to_unix_name(char *result, int result_len, char *id, int len,
40919f988b79SJean-Baptiste Boric 	struct charspec *chsp)
40929f988b79SJean-Baptiste Boric {
40939f988b79SJean-Baptiste Boric 	uint16_t   *raw_name, *unix_name;
40949f988b79SJean-Baptiste Boric 	uint16_t   *inchp, ch;
40959f988b79SJean-Baptiste Boric 	uint8_t	   *outchp;
40969f988b79SJean-Baptiste Boric 	const char *osta_id = "OSTA Compressed Unicode";
40979f988b79SJean-Baptiste Boric 	int         ucode_chars, nice_uchars, is_osta_typ0, nout;
40989f988b79SJean-Baptiste Boric 
40999f988b79SJean-Baptiste Boric 	raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK);
41009f988b79SJean-Baptiste Boric 	unix_name = raw_name + 1024;			/* split space in half */
41019f988b79SJean-Baptiste Boric 	assert(sizeof(char) == sizeof(uint8_t));
41029f988b79SJean-Baptiste Boric 	outchp = (uint8_t *) result;
41039f988b79SJean-Baptiste Boric 
41049f988b79SJean-Baptiste Boric 	is_osta_typ0  = (chsp->type == 0);
41059f988b79SJean-Baptiste Boric 	is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
41069f988b79SJean-Baptiste Boric 	if (is_osta_typ0) {
41079f988b79SJean-Baptiste Boric 		/* TODO clean up */
41089f988b79SJean-Baptiste Boric 		*raw_name = *unix_name = 0;
41099f988b79SJean-Baptiste Boric 		ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
41109f988b79SJean-Baptiste Boric 		ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
41119f988b79SJean-Baptiste Boric 		nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
41129f988b79SJean-Baptiste Boric 		/* output UTF8 */
41139f988b79SJean-Baptiste Boric 		for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
41149f988b79SJean-Baptiste Boric 			ch = *inchp;
41159f988b79SJean-Baptiste Boric 			nout = wput_utf8(outchp, result_len, ch);
41169f988b79SJean-Baptiste Boric 			outchp += nout; result_len -= nout;
41179f988b79SJean-Baptiste Boric 			if (!ch) break;
41189f988b79SJean-Baptiste Boric 		}
41199f988b79SJean-Baptiste Boric 		*outchp++ = 0;
41209f988b79SJean-Baptiste Boric 	} else {
41219f988b79SJean-Baptiste Boric 		/* assume 8bit char length byte latin-1 */
41229f988b79SJean-Baptiste Boric 		assert(*id == 8);
41239f988b79SJean-Baptiste Boric 		assert(strlen((char *) (id+1)) <= NAME_MAX);
41249f988b79SJean-Baptiste Boric 		strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
41259f988b79SJean-Baptiste Boric 	}
41269f988b79SJean-Baptiste Boric 	free(raw_name, M_UDFTEMP);
41279f988b79SJean-Baptiste Boric }
41289f988b79SJean-Baptiste Boric 
41299f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
41309f988b79SJean-Baptiste Boric 
41319f988b79SJean-Baptiste Boric void
unix_to_udf_name(char * result,uint8_t * result_len,char const * name,int name_len,struct charspec * chsp)41329f988b79SJean-Baptiste Boric unix_to_udf_name(char *result, uint8_t *result_len, char const *name, int name_len,
41339f988b79SJean-Baptiste Boric 	struct charspec *chsp)
41349f988b79SJean-Baptiste Boric {
41359f988b79SJean-Baptiste Boric 	uint16_t   *raw_name;
41369f988b79SJean-Baptiste Boric 	uint16_t   *outchp;
41379f988b79SJean-Baptiste Boric 	const char *inchp;
41389f988b79SJean-Baptiste Boric 	const char *osta_id = "OSTA Compressed Unicode";
41399f988b79SJean-Baptiste Boric 	int         udf_chars, is_osta_typ0, bits;
41409f988b79SJean-Baptiste Boric 	size_t      cnt;
41419f988b79SJean-Baptiste Boric 
41429f988b79SJean-Baptiste Boric 	/* allocate temporary unicode-16 buffer */
41439f988b79SJean-Baptiste Boric 	raw_name = malloc(1024, M_UDFTEMP, M_WAITOK);
41449f988b79SJean-Baptiste Boric 
41459f988b79SJean-Baptiste Boric 	/* convert utf8 to unicode-16 */
41469f988b79SJean-Baptiste Boric 	*raw_name = 0;
41479f988b79SJean-Baptiste Boric 	inchp  = name;
41489f988b79SJean-Baptiste Boric 	outchp = raw_name;
41499f988b79SJean-Baptiste Boric 	bits = 8;
41509f988b79SJean-Baptiste Boric 	for (cnt = name_len, udf_chars = 0; cnt;) {
41519f988b79SJean-Baptiste Boric 		*outchp = wget_utf8(&inchp, &cnt);
41529f988b79SJean-Baptiste Boric 		if (*outchp > 0xff)
41539f988b79SJean-Baptiste Boric 			bits=16;
41549f988b79SJean-Baptiste Boric 		outchp++;
41559f988b79SJean-Baptiste Boric 		udf_chars++;
41569f988b79SJean-Baptiste Boric 	}
41579f988b79SJean-Baptiste Boric 	/* null terminate just in case */
41589f988b79SJean-Baptiste Boric 	*outchp++ = 0;
41599f988b79SJean-Baptiste Boric 
41609f988b79SJean-Baptiste Boric 	is_osta_typ0  = (chsp->type == 0);
41619f988b79SJean-Baptiste Boric 	is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
41629f988b79SJean-Baptiste Boric 	if (is_osta_typ0) {
41639f988b79SJean-Baptiste Boric 		udf_chars = udf_CompressUnicode(udf_chars, bits,
41649f988b79SJean-Baptiste Boric 				(unicode_t *) raw_name,
41659f988b79SJean-Baptiste Boric 				(byte *) result);
41669f988b79SJean-Baptiste Boric 	} else {
41679f988b79SJean-Baptiste Boric 		printf("unix to udf name: no CHSP0 ?\n");
41689f988b79SJean-Baptiste Boric 		/* XXX assume 8bit char length byte latin-1 */
41699f988b79SJean-Baptiste Boric 		*result++ = 8; udf_chars = 1;
41709f988b79SJean-Baptiste Boric 		strncpy(result, name + 1, name_len);
41719f988b79SJean-Baptiste Boric 		udf_chars += name_len;
41729f988b79SJean-Baptiste Boric 	}
41739f988b79SJean-Baptiste Boric 	*result_len = udf_chars;
41749f988b79SJean-Baptiste Boric 	free(raw_name, M_UDFTEMP);
41759f988b79SJean-Baptiste Boric }
41769f988b79SJean-Baptiste Boric 
41779f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
41789f988b79SJean-Baptiste Boric 
41799f988b79SJean-Baptiste Boric void
udf_timestamp_to_timespec(struct udf_mount * ump,struct timestamp * timestamp,struct timespec * timespec)41809f988b79SJean-Baptiste Boric udf_timestamp_to_timespec(struct udf_mount *ump,
41819f988b79SJean-Baptiste Boric 			  struct timestamp *timestamp,
41829f988b79SJean-Baptiste Boric 			  struct timespec  *timespec)
41839f988b79SJean-Baptiste Boric {
41849f988b79SJean-Baptiste Boric 	struct clock_ymdhms ymdhms;
41859f988b79SJean-Baptiste Boric 	uint32_t usecs, secs, nsecs;
41869f988b79SJean-Baptiste Boric 	uint16_t tz;
41879f988b79SJean-Baptiste Boric 
41889f988b79SJean-Baptiste Boric 	/* fill in ymdhms structure from timestamp */
41899f988b79SJean-Baptiste Boric 	memset(&ymdhms, 0, sizeof(ymdhms));
41909f988b79SJean-Baptiste Boric 	ymdhms.dt_year = udf_rw16(timestamp->year);
41919f988b79SJean-Baptiste Boric 	ymdhms.dt_mon  = timestamp->month;
41929f988b79SJean-Baptiste Boric 	ymdhms.dt_day  = timestamp->day;
41939f988b79SJean-Baptiste Boric 	ymdhms.dt_wday = 0; /* ? */
41949f988b79SJean-Baptiste Boric 	ymdhms.dt_hour = timestamp->hour;
41959f988b79SJean-Baptiste Boric 	ymdhms.dt_min  = timestamp->minute;
41969f988b79SJean-Baptiste Boric 	ymdhms.dt_sec  = timestamp->second;
41979f988b79SJean-Baptiste Boric 
41989f988b79SJean-Baptiste Boric 	secs = clock_ymdhms_to_secs(&ymdhms);
41999f988b79SJean-Baptiste Boric 	usecs = timestamp->usec +
42009f988b79SJean-Baptiste Boric 		100*timestamp->hund_usec + 10000*timestamp->centisec;
42019f988b79SJean-Baptiste Boric 	nsecs = usecs * 1000;
42029f988b79SJean-Baptiste Boric 
42039f988b79SJean-Baptiste Boric 	/*
42049f988b79SJean-Baptiste Boric 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
42059f988b79SJean-Baptiste Boric 	 * compliment, so we gotta do some extra magic to handle it right.
42069f988b79SJean-Baptiste Boric 	 */
42079f988b79SJean-Baptiste Boric 	tz  = udf_rw16(timestamp->type_tz);
42089f988b79SJean-Baptiste Boric 	tz &= 0x0fff;			/* only lower 12 bits are significant */
42099f988b79SJean-Baptiste Boric 	if (tz & 0x0800)		/* sign extention */
42109f988b79SJean-Baptiste Boric 		tz |= 0xf000;
42119f988b79SJean-Baptiste Boric 
42129f988b79SJean-Baptiste Boric 	/* TODO check timezone conversion */
42139f988b79SJean-Baptiste Boric 	/* check if we are specified a timezone to convert */
42149f988b79SJean-Baptiste Boric 	if (udf_rw16(timestamp->type_tz) & 0x1000) {
42159f988b79SJean-Baptiste Boric 		if ((int16_t) tz != -2047)
42169f988b79SJean-Baptiste Boric 			secs -= (int16_t) tz * 60;
42179f988b79SJean-Baptiste Boric 	} else {
42189f988b79SJean-Baptiste Boric 		secs -= ump->mount_args.gmtoff;
42199f988b79SJean-Baptiste Boric 	}
42209f988b79SJean-Baptiste Boric 
42219f988b79SJean-Baptiste Boric 	timespec->tv_sec  = secs;
42229f988b79SJean-Baptiste Boric 	timespec->tv_nsec = nsecs;
42239f988b79SJean-Baptiste Boric }
42249f988b79SJean-Baptiste Boric 
42259f988b79SJean-Baptiste Boric 
42269f988b79SJean-Baptiste Boric void
udf_timespec_to_timestamp(struct timespec * timespec,struct timestamp * timestamp)42279f988b79SJean-Baptiste Boric udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp)
42289f988b79SJean-Baptiste Boric {
42299f988b79SJean-Baptiste Boric 	struct clock_ymdhms ymdhms;
42309f988b79SJean-Baptiste Boric 	uint32_t husec, usec, csec;
42319f988b79SJean-Baptiste Boric 
42329f988b79SJean-Baptiste Boric 	(void) clock_secs_to_ymdhms(timespec->tv_sec, &ymdhms);
42339f988b79SJean-Baptiste Boric 
42349f988b79SJean-Baptiste Boric 	usec   = timespec->tv_nsec / 1000;
42359f988b79SJean-Baptiste Boric 	husec  =  usec / 100;
42369f988b79SJean-Baptiste Boric 	usec  -= husec * 100;				/* only 0-99 in usec  */
42379f988b79SJean-Baptiste Boric 	csec   = husec / 100;				/* only 0-99 in csec  */
42389f988b79SJean-Baptiste Boric 	husec -=  csec * 100;				/* only 0-99 in husec */
42399f988b79SJean-Baptiste Boric 
42409f988b79SJean-Baptiste Boric 	/* set method 1 for CUT/GMT */
42419f988b79SJean-Baptiste Boric 	timestamp->type_tz	= udf_rw16((1<<12) + 0);
42429f988b79SJean-Baptiste Boric 	timestamp->year		= udf_rw16(ymdhms.dt_year);
42439f988b79SJean-Baptiste Boric 	timestamp->month	= ymdhms.dt_mon;
42449f988b79SJean-Baptiste Boric 	timestamp->day		= ymdhms.dt_day;
42459f988b79SJean-Baptiste Boric 	timestamp->hour		= ymdhms.dt_hour;
42469f988b79SJean-Baptiste Boric 	timestamp->minute	= ymdhms.dt_min;
42479f988b79SJean-Baptiste Boric 	timestamp->second	= ymdhms.dt_sec;
42489f988b79SJean-Baptiste Boric 	timestamp->centisec	= csec;
42499f988b79SJean-Baptiste Boric 	timestamp->hund_usec	= husec;
42509f988b79SJean-Baptiste Boric 	timestamp->usec		= usec;
42519f988b79SJean-Baptiste Boric }
42529f988b79SJean-Baptiste Boric 
42539f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
42549f988b79SJean-Baptiste Boric 
42559f988b79SJean-Baptiste Boric /*
42569f988b79SJean-Baptiste Boric  * Attribute and filetypes converters with get/set pairs
42579f988b79SJean-Baptiste Boric  */
42589f988b79SJean-Baptiste Boric 
42599f988b79SJean-Baptiste Boric uint32_t
udf_getaccessmode(struct udf_node * udf_node)42609f988b79SJean-Baptiste Boric udf_getaccessmode(struct udf_node *udf_node)
42619f988b79SJean-Baptiste Boric {
42629f988b79SJean-Baptiste Boric 	struct file_entry     *fe = udf_node->fe;
42639f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = udf_node->efe;
42649f988b79SJean-Baptiste Boric 	uint32_t udf_perm, icbftype;
42659f988b79SJean-Baptiste Boric 	uint32_t mode, ftype;
42669f988b79SJean-Baptiste Boric 	uint16_t icbflags;
42679f988b79SJean-Baptiste Boric 
42689f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
42699f988b79SJean-Baptiste Boric 	if (fe) {
42709f988b79SJean-Baptiste Boric 		udf_perm = udf_rw32(fe->perm);
42719f988b79SJean-Baptiste Boric 		icbftype = fe->icbtag.file_type;
42729f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(fe->icbtag.flags);
42739f988b79SJean-Baptiste Boric 	} else {
42749f988b79SJean-Baptiste Boric 		assert(udf_node->efe);
42759f988b79SJean-Baptiste Boric 		udf_perm = udf_rw32(efe->perm);
42769f988b79SJean-Baptiste Boric 		icbftype = efe->icbtag.file_type;
42779f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(efe->icbtag.flags);
42789f988b79SJean-Baptiste Boric 	}
42799f988b79SJean-Baptiste Boric 
42809f988b79SJean-Baptiste Boric 	mode  = udf_perm_to_unix_mode(udf_perm);
42819f988b79SJean-Baptiste Boric 	ftype = udf_icb_to_unix_filetype(icbftype);
42829f988b79SJean-Baptiste Boric 
42839f988b79SJean-Baptiste Boric 	/* set suid, sgid, sticky from flags in fe/efe */
42849f988b79SJean-Baptiste Boric 	if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
42859f988b79SJean-Baptiste Boric 		mode |= S_ISUID;
42869f988b79SJean-Baptiste Boric 	if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
42879f988b79SJean-Baptiste Boric 		mode |= S_ISGID;
42889f988b79SJean-Baptiste Boric 	if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
42899f988b79SJean-Baptiste Boric 		mode |= S_ISVTX;
42909f988b79SJean-Baptiste Boric 
42919f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
42929f988b79SJean-Baptiste Boric 
42939f988b79SJean-Baptiste Boric 	return mode | ftype;
42949f988b79SJean-Baptiste Boric }
42959f988b79SJean-Baptiste Boric 
42969f988b79SJean-Baptiste Boric 
42979f988b79SJean-Baptiste Boric void
udf_setaccessmode(struct udf_node * udf_node,mode_t mode)42989f988b79SJean-Baptiste Boric udf_setaccessmode(struct udf_node *udf_node, mode_t mode)
42999f988b79SJean-Baptiste Boric {
43009f988b79SJean-Baptiste Boric 	struct file_entry    *fe  = udf_node->fe;
43019f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = udf_node->efe;
43029f988b79SJean-Baptiste Boric 	uint32_t udf_perm;
43039f988b79SJean-Baptiste Boric 	uint16_t icbflags;
43049f988b79SJean-Baptiste Boric 
43059f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
43069f988b79SJean-Baptiste Boric 	udf_perm = unix_mode_to_udf_perm(mode & ALLPERMS);
43079f988b79SJean-Baptiste Boric 	if (fe) {
43089f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(fe->icbtag.flags);
43099f988b79SJean-Baptiste Boric 	} else {
43109f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(efe->icbtag.flags);
43119f988b79SJean-Baptiste Boric 	}
43129f988b79SJean-Baptiste Boric 
43139f988b79SJean-Baptiste Boric 	icbflags &= ~UDF_ICB_TAG_FLAGS_SETUID;
43149f988b79SJean-Baptiste Boric 	icbflags &= ~UDF_ICB_TAG_FLAGS_SETGID;
43159f988b79SJean-Baptiste Boric 	icbflags &= ~UDF_ICB_TAG_FLAGS_STICKY;
43169f988b79SJean-Baptiste Boric 	if (mode & S_ISUID)
43179f988b79SJean-Baptiste Boric 		icbflags |= UDF_ICB_TAG_FLAGS_SETUID;
43189f988b79SJean-Baptiste Boric 	if (mode & S_ISGID)
43199f988b79SJean-Baptiste Boric 		icbflags |= UDF_ICB_TAG_FLAGS_SETGID;
43209f988b79SJean-Baptiste Boric 	if (mode & S_ISVTX)
43219f988b79SJean-Baptiste Boric 		icbflags |= UDF_ICB_TAG_FLAGS_STICKY;
43229f988b79SJean-Baptiste Boric 
43239f988b79SJean-Baptiste Boric 	if (fe) {
43249f988b79SJean-Baptiste Boric 		fe->perm  = udf_rw32(udf_perm);
43259f988b79SJean-Baptiste Boric 		fe->icbtag.flags  = udf_rw16(icbflags);
43269f988b79SJean-Baptiste Boric 	} else {
43279f988b79SJean-Baptiste Boric 		efe->perm = udf_rw32(udf_perm);
43289f988b79SJean-Baptiste Boric 		efe->icbtag.flags = udf_rw16(icbflags);
43299f988b79SJean-Baptiste Boric 	}
43309f988b79SJean-Baptiste Boric 
43319f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
43329f988b79SJean-Baptiste Boric }
43339f988b79SJean-Baptiste Boric 
43349f988b79SJean-Baptiste Boric 
43359f988b79SJean-Baptiste Boric void
udf_getownership(struct udf_node * udf_node,uid_t * uidp,gid_t * gidp)43369f988b79SJean-Baptiste Boric udf_getownership(struct udf_node *udf_node, uid_t *uidp, gid_t *gidp)
43379f988b79SJean-Baptiste Boric {
43389f988b79SJean-Baptiste Boric 	struct udf_mount     *ump = udf_node->ump;
43399f988b79SJean-Baptiste Boric 	struct file_entry    *fe  = udf_node->fe;
43409f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = udf_node->efe;
43419f988b79SJean-Baptiste Boric 	uid_t uid;
43429f988b79SJean-Baptiste Boric 	gid_t gid;
43439f988b79SJean-Baptiste Boric 
43449f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
43459f988b79SJean-Baptiste Boric 	if (fe) {
43469f988b79SJean-Baptiste Boric 		uid = (uid_t)udf_rw32(fe->uid);
43479f988b79SJean-Baptiste Boric 		gid = (gid_t)udf_rw32(fe->gid);
43489f988b79SJean-Baptiste Boric 	} else {
43499f988b79SJean-Baptiste Boric 		assert(udf_node->efe);
43509f988b79SJean-Baptiste Boric 		uid = (uid_t)udf_rw32(efe->uid);
43519f988b79SJean-Baptiste Boric 		gid = (gid_t)udf_rw32(efe->gid);
43529f988b79SJean-Baptiste Boric 	}
43539f988b79SJean-Baptiste Boric 
43549f988b79SJean-Baptiste Boric 	/* do the uid/gid translation game */
43559f988b79SJean-Baptiste Boric 	if (uid == (uid_t) -1)
43569f988b79SJean-Baptiste Boric 		uid = ump->mount_args.anon_uid;
43579f988b79SJean-Baptiste Boric 	if (gid == (gid_t) -1)
43589f988b79SJean-Baptiste Boric 		gid = ump->mount_args.anon_gid;
43599f988b79SJean-Baptiste Boric 
43609f988b79SJean-Baptiste Boric 	*uidp = uid;
43619f988b79SJean-Baptiste Boric 	*gidp = gid;
43629f988b79SJean-Baptiste Boric 
43639f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
43649f988b79SJean-Baptiste Boric }
43659f988b79SJean-Baptiste Boric 
43669f988b79SJean-Baptiste Boric 
43679f988b79SJean-Baptiste Boric void
udf_setownership(struct udf_node * udf_node,uid_t uid,gid_t gid)43689f988b79SJean-Baptiste Boric udf_setownership(struct udf_node *udf_node, uid_t uid, gid_t gid)
43699f988b79SJean-Baptiste Boric {
43709f988b79SJean-Baptiste Boric 	struct udf_mount     *ump = udf_node->ump;
43719f988b79SJean-Baptiste Boric 	struct file_entry    *fe  = udf_node->fe;
43729f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = udf_node->efe;
43739f988b79SJean-Baptiste Boric 	uid_t nobody_uid;
43749f988b79SJean-Baptiste Boric 	gid_t nobody_gid;
43759f988b79SJean-Baptiste Boric 
43769f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
43779f988b79SJean-Baptiste Boric 
43789f988b79SJean-Baptiste Boric 	/* do the uid/gid translation game */
43799f988b79SJean-Baptiste Boric 	nobody_uid = ump->mount_args.nobody_uid;
43809f988b79SJean-Baptiste Boric 	nobody_gid = ump->mount_args.nobody_gid;
43819f988b79SJean-Baptiste Boric 	if (uid == nobody_uid)
43829f988b79SJean-Baptiste Boric 		uid = (uid_t) -1;
43839f988b79SJean-Baptiste Boric 	if (gid == nobody_gid)
43849f988b79SJean-Baptiste Boric 		gid = (gid_t) -1;
43859f988b79SJean-Baptiste Boric 
43869f988b79SJean-Baptiste Boric 	if (fe) {
43879f988b79SJean-Baptiste Boric 		fe->uid  = udf_rw32((uint32_t) uid);
43889f988b79SJean-Baptiste Boric 		fe->gid  = udf_rw32((uint32_t) gid);
43899f988b79SJean-Baptiste Boric 	} else {
43909f988b79SJean-Baptiste Boric 		efe->uid = udf_rw32((uint32_t) uid);
43919f988b79SJean-Baptiste Boric 		efe->gid = udf_rw32((uint32_t) gid);
43929f988b79SJean-Baptiste Boric 	}
43939f988b79SJean-Baptiste Boric 
43949f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
43959f988b79SJean-Baptiste Boric }
43969f988b79SJean-Baptiste Boric 
43979f988b79SJean-Baptiste Boric 
43989f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
43999f988b79SJean-Baptiste Boric 
44009f988b79SJean-Baptiste Boric 
44019f988b79SJean-Baptiste Boric int
udf_dirhash_fill(struct udf_node * dir_node)44029f988b79SJean-Baptiste Boric udf_dirhash_fill(struct udf_node *dir_node)
44039f988b79SJean-Baptiste Boric {
44049f988b79SJean-Baptiste Boric 	struct vnode *dvp = dir_node->vnode;
44059f988b79SJean-Baptiste Boric 	struct dirhash *dirh;
44069f988b79SJean-Baptiste Boric 	struct file_entry    *fe  = dir_node->fe;
44079f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = dir_node->efe;
44089f988b79SJean-Baptiste Boric 	struct fileid_desc *fid;
44099f988b79SJean-Baptiste Boric 	struct dirent *dirent;
44109f988b79SJean-Baptiste Boric 	uint64_t file_size, pre_diroffset, diroffset;
44119f988b79SJean-Baptiste Boric 	uint32_t lb_size;
44129f988b79SJean-Baptiste Boric 	int error;
44139f988b79SJean-Baptiste Boric 
44149f988b79SJean-Baptiste Boric 	/* make sure we have a dirhash to work on */
44159f988b79SJean-Baptiste Boric 	dirh = dir_node->dir_hash;
44169f988b79SJean-Baptiste Boric 	KASSERT(dirh);
44179f988b79SJean-Baptiste Boric 	KASSERT(dirh->refcnt > 0);
44189f988b79SJean-Baptiste Boric 
44199f988b79SJean-Baptiste Boric 	if (dirh->flags & DIRH_BROKEN)
44209f988b79SJean-Baptiste Boric 		return EIO;
44219f988b79SJean-Baptiste Boric 	if (dirh->flags & DIRH_COMPLETE)
44229f988b79SJean-Baptiste Boric 		return 0;
44239f988b79SJean-Baptiste Boric 
44249f988b79SJean-Baptiste Boric 	/* make sure we have a clean dirhash to add to */
44259f988b79SJean-Baptiste Boric 	dirhash_purge_entries(dirh);
44269f988b79SJean-Baptiste Boric 
44279f988b79SJean-Baptiste Boric 	/* get directory filesize */
44289f988b79SJean-Baptiste Boric 	if (fe) {
44299f988b79SJean-Baptiste Boric 		file_size = udf_rw64(fe->inf_len);
44309f988b79SJean-Baptiste Boric 	} else {
44319f988b79SJean-Baptiste Boric 		assert(efe);
44329f988b79SJean-Baptiste Boric 		file_size = udf_rw64(efe->inf_len);
44339f988b79SJean-Baptiste Boric 	}
44349f988b79SJean-Baptiste Boric 
44359f988b79SJean-Baptiste Boric 	/* allocate temporary space for fid */
44369f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
44379f988b79SJean-Baptiste Boric 	fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
44389f988b79SJean-Baptiste Boric 
44399f988b79SJean-Baptiste Boric 	/* allocate temporary space for dirent */
44409f988b79SJean-Baptiste Boric 	dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
44419f988b79SJean-Baptiste Boric 
44429f988b79SJean-Baptiste Boric 	error = 0;
44439f988b79SJean-Baptiste Boric 	diroffset = 0;
44449f988b79SJean-Baptiste Boric 	while (diroffset < file_size) {
44459f988b79SJean-Baptiste Boric 		/* transfer a new fid/dirent */
44469f988b79SJean-Baptiste Boric 		pre_diroffset = diroffset;
44479f988b79SJean-Baptiste Boric 		error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
44489f988b79SJean-Baptiste Boric 		if (error) {
44499f988b79SJean-Baptiste Boric 			/* TODO what to do? continue but not add? */
44509f988b79SJean-Baptiste Boric 			dirh->flags |= DIRH_BROKEN;
44519f988b79SJean-Baptiste Boric 			dirhash_purge_entries(dirh);
44529f988b79SJean-Baptiste Boric 			break;
44539f988b79SJean-Baptiste Boric 		}
44549f988b79SJean-Baptiste Boric 
44559f988b79SJean-Baptiste Boric 		if ((fid->file_char & UDF_FILE_CHAR_DEL)) {
44569f988b79SJean-Baptiste Boric 			/* register deleted extent for reuse */
44579f988b79SJean-Baptiste Boric 			dirhash_enter_freed(dirh, pre_diroffset,
44589f988b79SJean-Baptiste Boric 				udf_fidsize(fid));
44599f988b79SJean-Baptiste Boric 		} else {
44609f988b79SJean-Baptiste Boric 			/* append to the dirhash */
44619f988b79SJean-Baptiste Boric 			dirhash_enter(dirh, dirent, pre_diroffset,
44629f988b79SJean-Baptiste Boric 				udf_fidsize(fid), 0);
44639f988b79SJean-Baptiste Boric 		}
44649f988b79SJean-Baptiste Boric 	}
44659f988b79SJean-Baptiste Boric 	dirh->flags |= DIRH_COMPLETE;
44669f988b79SJean-Baptiste Boric 
44679f988b79SJean-Baptiste Boric 	free(fid, M_UDFTEMP);
44689f988b79SJean-Baptiste Boric 	free(dirent, M_UDFTEMP);
44699f988b79SJean-Baptiste Boric 
44709f988b79SJean-Baptiste Boric 	return error;
44719f988b79SJean-Baptiste Boric }
44729f988b79SJean-Baptiste Boric 
44739f988b79SJean-Baptiste Boric 
44749f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
44759f988b79SJean-Baptiste Boric 
44769f988b79SJean-Baptiste Boric /*
44779f988b79SJean-Baptiste Boric  * Directory read and manipulation functions.
44789f988b79SJean-Baptiste Boric  *
44799f988b79SJean-Baptiste Boric  */
44809f988b79SJean-Baptiste Boric 
44819f988b79SJean-Baptiste Boric int
udf_lookup_name_in_dir(struct vnode * vp,const char * name,int namelen,struct long_ad * icb_loc,int * found)44829f988b79SJean-Baptiste Boric udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
44839f988b79SJean-Baptiste Boric        struct long_ad *icb_loc, int *found)
44849f988b79SJean-Baptiste Boric {
44859f988b79SJean-Baptiste Boric 	struct udf_node  *dir_node = VTOI(vp);
44869f988b79SJean-Baptiste Boric 	struct dirhash       *dirh;
44879f988b79SJean-Baptiste Boric 	struct dirhash_entry *dirh_ep;
44889f988b79SJean-Baptiste Boric 	struct fileid_desc *fid;
44899f988b79SJean-Baptiste Boric 	struct dirent *dirent;
44909f988b79SJean-Baptiste Boric 	uint64_t diroffset;
44919f988b79SJean-Baptiste Boric 	uint32_t lb_size;
44929f988b79SJean-Baptiste Boric 	int hit, error;
44939f988b79SJean-Baptiste Boric 
44949f988b79SJean-Baptiste Boric 	/* set default return */
44959f988b79SJean-Baptiste Boric 	*found = 0;
44969f988b79SJean-Baptiste Boric 
44979f988b79SJean-Baptiste Boric 	/* get our dirhash and make sure its read in */
44989f988b79SJean-Baptiste Boric 	dirhash_get(&dir_node->dir_hash);
44999f988b79SJean-Baptiste Boric 	error = udf_dirhash_fill(dir_node);
45009f988b79SJean-Baptiste Boric 	if (error) {
45019f988b79SJean-Baptiste Boric 		dirhash_put(dir_node->dir_hash);
45029f988b79SJean-Baptiste Boric 		return error;
45039f988b79SJean-Baptiste Boric 	}
45049f988b79SJean-Baptiste Boric 	dirh = dir_node->dir_hash;
45059f988b79SJean-Baptiste Boric 
45069f988b79SJean-Baptiste Boric 	/* allocate temporary space for fid */
45079f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
45089f988b79SJean-Baptiste Boric 	fid     = malloc(lb_size, M_UDFTEMP, M_WAITOK);
45099f988b79SJean-Baptiste Boric 	dirent  = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
45109f988b79SJean-Baptiste Boric 
45119f988b79SJean-Baptiste Boric 	DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
45129f988b79SJean-Baptiste Boric 		namelen, namelen, name));
45139f988b79SJean-Baptiste Boric 
45149f988b79SJean-Baptiste Boric 	/* search our dirhash hits */
45159f988b79SJean-Baptiste Boric 	memset(icb_loc, 0, sizeof(*icb_loc));
45169f988b79SJean-Baptiste Boric 	dirh_ep = NULL;
45179f988b79SJean-Baptiste Boric 	for (;;) {
45189f988b79SJean-Baptiste Boric 		hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
45199f988b79SJean-Baptiste Boric 		/* if no hit, abort the search */
45209f988b79SJean-Baptiste Boric 		if (!hit)
45219f988b79SJean-Baptiste Boric 			break;
45229f988b79SJean-Baptiste Boric 
45239f988b79SJean-Baptiste Boric 		/* check this hit */
45249f988b79SJean-Baptiste Boric 		diroffset = dirh_ep->offset;
45259f988b79SJean-Baptiste Boric 
45269f988b79SJean-Baptiste Boric 		/* transfer a new fid/dirent */
45279f988b79SJean-Baptiste Boric 		error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
45289f988b79SJean-Baptiste Boric 		if (error)
45299f988b79SJean-Baptiste Boric 			break;
45309f988b79SJean-Baptiste Boric 
45319f988b79SJean-Baptiste Boric 		DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
45329f988b79SJean-Baptiste Boric 			dirent->d_namlen, dirent->d_namlen, dirent->d_name));
45339f988b79SJean-Baptiste Boric 
45349f988b79SJean-Baptiste Boric 		/* see if its our entry */
45359f988b79SJean-Baptiste Boric #ifdef DIAGNOSTIC
45369f988b79SJean-Baptiste Boric 		if (dirent->d_namlen != namelen) {
45379f988b79SJean-Baptiste Boric 			printf("WARNING: dirhash_lookup() returned wrong "
45389f988b79SJean-Baptiste Boric 				"d_namelen: %d and ought to be %d\n",
45399f988b79SJean-Baptiste Boric 				dirent->d_namlen, namelen);
45409f988b79SJean-Baptiste Boric 			printf("\tlooked for `%s' and got `%s'\n",
45419f988b79SJean-Baptiste Boric 				name, dirent->d_name);
45429f988b79SJean-Baptiste Boric 		}
45439f988b79SJean-Baptiste Boric #endif
45449f988b79SJean-Baptiste Boric 		if (strncmp(dirent->d_name, name, namelen) == 0) {
45459f988b79SJean-Baptiste Boric 			*found = 1;
45469f988b79SJean-Baptiste Boric 			*icb_loc = fid->icb;
45479f988b79SJean-Baptiste Boric 			break;
45489f988b79SJean-Baptiste Boric 		}
45499f988b79SJean-Baptiste Boric 	}
45509f988b79SJean-Baptiste Boric 	free(fid, M_UDFTEMP);
45519f988b79SJean-Baptiste Boric 	free(dirent, M_UDFTEMP);
45529f988b79SJean-Baptiste Boric 
45539f988b79SJean-Baptiste Boric 	dirhash_put(dir_node->dir_hash);
45549f988b79SJean-Baptiste Boric 
45559f988b79SJean-Baptiste Boric 	return error;
45569f988b79SJean-Baptiste Boric }
45579f988b79SJean-Baptiste Boric 
45589f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
45599f988b79SJean-Baptiste Boric 
45609f988b79SJean-Baptiste Boric static int
udf_create_new_fe(struct udf_mount * ump,struct file_entry * fe,int file_type,struct long_ad * node_icb,struct long_ad * parent_icb,uint64_t parent_unique_id)45619f988b79SJean-Baptiste Boric udf_create_new_fe(struct udf_mount *ump, struct file_entry *fe, int file_type,
45629f988b79SJean-Baptiste Boric 	struct long_ad *node_icb, struct long_ad *parent_icb,
45639f988b79SJean-Baptiste Boric 	uint64_t parent_unique_id)
45649f988b79SJean-Baptiste Boric {
45659f988b79SJean-Baptiste Boric 	struct timespec now;
45669f988b79SJean-Baptiste Boric 	struct icb_tag *icb;
45679f988b79SJean-Baptiste Boric 	struct filetimes_extattr_entry *ft_extattr;
45689f988b79SJean-Baptiste Boric 	uint64_t unique_id;
45699f988b79SJean-Baptiste Boric 	uint32_t fidsize, lb_num;
45709f988b79SJean-Baptiste Boric 	uint8_t *bpos;
45719f988b79SJean-Baptiste Boric 	int crclen, attrlen;
45729f988b79SJean-Baptiste Boric 
45739f988b79SJean-Baptiste Boric 	lb_num = udf_rw32(node_icb->loc.lb_num);
45749f988b79SJean-Baptiste Boric 	udf_inittag(ump, &fe->tag, TAGID_FENTRY, lb_num);
45759f988b79SJean-Baptiste Boric 	icb = &fe->icbtag;
45769f988b79SJean-Baptiste Boric 
45779f988b79SJean-Baptiste Boric 	/*
45789f988b79SJean-Baptiste Boric 	 * Always use strategy type 4 unless on WORM wich we don't support
45799f988b79SJean-Baptiste Boric 	 * (yet). Fill in defaults and set for internal allocation of data.
45809f988b79SJean-Baptiste Boric 	 */
45819f988b79SJean-Baptiste Boric 	icb->strat_type      = udf_rw16(4);
45829f988b79SJean-Baptiste Boric 	icb->max_num_entries = udf_rw16(1);
45839f988b79SJean-Baptiste Boric 	icb->file_type       = file_type;	/* 8 bit */
45849f988b79SJean-Baptiste Boric 	icb->flags           = udf_rw16(UDF_ICB_INTERN_ALLOC);
45859f988b79SJean-Baptiste Boric 
45869f988b79SJean-Baptiste Boric 	fe->perm     = udf_rw32(0x7fff);	/* all is allowed   */
45879f988b79SJean-Baptiste Boric 	fe->link_cnt = udf_rw16(0);		/* explicit setting */
45889f988b79SJean-Baptiste Boric 
45899f988b79SJean-Baptiste Boric 	fe->ckpoint  = udf_rw32(1);		/* user supplied file version */
45909f988b79SJean-Baptiste Boric 
45919f988b79SJean-Baptiste Boric 	vfs_timestamp(&now);
45929f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &fe->atime);
45939f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &fe->attrtime);
45949f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &fe->mtime);
45959f988b79SJean-Baptiste Boric 
45969f988b79SJean-Baptiste Boric 	udf_set_regid(&fe->imp_id, IMPL_NAME);
45979f988b79SJean-Baptiste Boric 	udf_add_impl_regid(ump, &fe->imp_id);
45989f988b79SJean-Baptiste Boric 
45999f988b79SJean-Baptiste Boric 	unique_id = udf_advance_uniqueid(ump);
46009f988b79SJean-Baptiste Boric 	fe->unique_id = udf_rw64(unique_id);
46019f988b79SJean-Baptiste Boric 	fe->l_ea = udf_rw32(0);
46029f988b79SJean-Baptiste Boric 
46039f988b79SJean-Baptiste Boric 	/* create extended attribute to record our creation time */
46049f988b79SJean-Baptiste Boric 	attrlen = UDF_FILETIMES_ATTR_SIZE(1);
46059f988b79SJean-Baptiste Boric 	ft_extattr = malloc(attrlen, M_UDFTEMP, M_WAITOK);
46069f988b79SJean-Baptiste Boric 	memset(ft_extattr, 0, attrlen);
46079f988b79SJean-Baptiste Boric 	ft_extattr->hdr.type = udf_rw32(UDF_FILETIMES_ATTR_NO);
46089f988b79SJean-Baptiste Boric 	ft_extattr->hdr.subtype = 1;	/* [4/48.10.5] */
46099f988b79SJean-Baptiste Boric 	ft_extattr->hdr.a_l = udf_rw32(UDF_FILETIMES_ATTR_SIZE(1));
46109f988b79SJean-Baptiste Boric 	ft_extattr->d_l     = udf_rw32(UDF_TIMESTAMP_SIZE); /* one item */
46119f988b79SJean-Baptiste Boric 	ft_extattr->existence = UDF_FILETIMES_FILE_CREATION;
46129f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &ft_extattr->times[0]);
46139f988b79SJean-Baptiste Boric 
46149f988b79SJean-Baptiste Boric 	udf_extattr_insert_internal(ump, (union dscrptr *) fe,
46159f988b79SJean-Baptiste Boric 		(struct extattr_entry *) ft_extattr);
46169f988b79SJean-Baptiste Boric 	free(ft_extattr, M_UDFTEMP);
46179f988b79SJean-Baptiste Boric 
46189f988b79SJean-Baptiste Boric 	/* if its a directory, create '..' */
46199f988b79SJean-Baptiste Boric 	bpos = (uint8_t *) fe->data + udf_rw32(fe->l_ea);
46209f988b79SJean-Baptiste Boric 	fidsize = 0;
46219f988b79SJean-Baptiste Boric 	if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
46229f988b79SJean-Baptiste Boric 		fidsize = udf_create_parentfid(ump,
46239f988b79SJean-Baptiste Boric 			(struct fileid_desc *) bpos, parent_icb,
46249f988b79SJean-Baptiste Boric 			parent_unique_id);
46259f988b79SJean-Baptiste Boric 	}
46269f988b79SJean-Baptiste Boric 
46279f988b79SJean-Baptiste Boric 	/* record fidlength information */
46289f988b79SJean-Baptiste Boric 	fe->inf_len = udf_rw64(fidsize);
46299f988b79SJean-Baptiste Boric 	fe->l_ad    = udf_rw32(fidsize);
46309f988b79SJean-Baptiste Boric 	fe->logblks_rec = udf_rw64(0);		/* intern */
46319f988b79SJean-Baptiste Boric 
46329f988b79SJean-Baptiste Boric 	crclen  = sizeof(struct file_entry) - 1 - UDF_DESC_TAG_LENGTH;
46339f988b79SJean-Baptiste Boric 	crclen += udf_rw32(fe->l_ea) + fidsize;
46349f988b79SJean-Baptiste Boric 	fe->tag.desc_crc_len = udf_rw16(crclen);
46359f988b79SJean-Baptiste Boric 
46369f988b79SJean-Baptiste Boric 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fe);
46379f988b79SJean-Baptiste Boric 
46389f988b79SJean-Baptiste Boric 	return fidsize;
46399f988b79SJean-Baptiste Boric }
46409f988b79SJean-Baptiste Boric 
46419f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
46429f988b79SJean-Baptiste Boric 
46439f988b79SJean-Baptiste Boric static int
udf_create_new_efe(struct udf_mount * ump,struct extfile_entry * efe,int file_type,struct long_ad * node_icb,struct long_ad * parent_icb,uint64_t parent_unique_id)46449f988b79SJean-Baptiste Boric udf_create_new_efe(struct udf_mount *ump, struct extfile_entry *efe,
46459f988b79SJean-Baptiste Boric 	int file_type, struct long_ad *node_icb, struct long_ad *parent_icb,
46469f988b79SJean-Baptiste Boric 	uint64_t parent_unique_id)
46479f988b79SJean-Baptiste Boric {
46489f988b79SJean-Baptiste Boric 	struct timespec now;
46499f988b79SJean-Baptiste Boric 	struct icb_tag *icb;
46509f988b79SJean-Baptiste Boric 	uint64_t unique_id;
46519f988b79SJean-Baptiste Boric 	uint32_t fidsize, lb_num;
46529f988b79SJean-Baptiste Boric 	uint8_t *bpos;
46539f988b79SJean-Baptiste Boric 	int crclen;
46549f988b79SJean-Baptiste Boric 
46559f988b79SJean-Baptiste Boric 	lb_num = udf_rw32(node_icb->loc.lb_num);
46569f988b79SJean-Baptiste Boric 	udf_inittag(ump, &efe->tag, TAGID_EXTFENTRY, lb_num);
46579f988b79SJean-Baptiste Boric 	icb = &efe->icbtag;
46589f988b79SJean-Baptiste Boric 
46599f988b79SJean-Baptiste Boric 	/*
46609f988b79SJean-Baptiste Boric 	 * Always use strategy type 4 unless on WORM wich we don't support
46619f988b79SJean-Baptiste Boric 	 * (yet). Fill in defaults and set for internal allocation of data.
46629f988b79SJean-Baptiste Boric 	 */
46639f988b79SJean-Baptiste Boric 	icb->strat_type      = udf_rw16(4);
46649f988b79SJean-Baptiste Boric 	icb->max_num_entries = udf_rw16(1);
46659f988b79SJean-Baptiste Boric 	icb->file_type       = file_type;	/* 8 bit */
46669f988b79SJean-Baptiste Boric 	icb->flags           = udf_rw16(UDF_ICB_INTERN_ALLOC);
46679f988b79SJean-Baptiste Boric 
46689f988b79SJean-Baptiste Boric 	efe->perm     = udf_rw32(0x7fff);	/* all is allowed   */
46699f988b79SJean-Baptiste Boric 	efe->link_cnt = udf_rw16(0);		/* explicit setting */
46709f988b79SJean-Baptiste Boric 
46719f988b79SJean-Baptiste Boric 	efe->ckpoint  = udf_rw32(1);		/* user supplied file version */
46729f988b79SJean-Baptiste Boric 
46739f988b79SJean-Baptiste Boric 	vfs_timestamp(&now);
46749f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &efe->ctime);
46759f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &efe->atime);
46769f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &efe->attrtime);
46779f988b79SJean-Baptiste Boric 	udf_timespec_to_timestamp(&now, &efe->mtime);
46789f988b79SJean-Baptiste Boric 
46799f988b79SJean-Baptiste Boric 	udf_set_regid(&efe->imp_id, IMPL_NAME);
46809f988b79SJean-Baptiste Boric 	udf_add_impl_regid(ump, &efe->imp_id);
46819f988b79SJean-Baptiste Boric 
46829f988b79SJean-Baptiste Boric 	unique_id = udf_advance_uniqueid(ump);
46839f988b79SJean-Baptiste Boric 	efe->unique_id = udf_rw64(unique_id);
46849f988b79SJean-Baptiste Boric 	efe->l_ea = udf_rw32(0);
46859f988b79SJean-Baptiste Boric 
46869f988b79SJean-Baptiste Boric 	/* if its a directory, create '..' */
46879f988b79SJean-Baptiste Boric 	bpos = (uint8_t *) efe->data + udf_rw32(efe->l_ea);
46889f988b79SJean-Baptiste Boric 	fidsize = 0;
46899f988b79SJean-Baptiste Boric 	if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
46909f988b79SJean-Baptiste Boric 		fidsize = udf_create_parentfid(ump,
46919f988b79SJean-Baptiste Boric 			(struct fileid_desc *) bpos, parent_icb,
46929f988b79SJean-Baptiste Boric 			parent_unique_id);
46939f988b79SJean-Baptiste Boric 	}
46949f988b79SJean-Baptiste Boric 
46959f988b79SJean-Baptiste Boric 	/* record fidlength information */
46969f988b79SJean-Baptiste Boric 	efe->obj_size = udf_rw64(fidsize);
46979f988b79SJean-Baptiste Boric 	efe->inf_len  = udf_rw64(fidsize);
46989f988b79SJean-Baptiste Boric 	efe->l_ad     = udf_rw32(fidsize);
46999f988b79SJean-Baptiste Boric 	efe->logblks_rec = udf_rw64(0);		/* intern */
47009f988b79SJean-Baptiste Boric 
47019f988b79SJean-Baptiste Boric 	crclen  = sizeof(struct extfile_entry) - 1 - UDF_DESC_TAG_LENGTH;
47029f988b79SJean-Baptiste Boric 	crclen += udf_rw32(efe->l_ea) + fidsize;
47039f988b79SJean-Baptiste Boric 	efe->tag.desc_crc_len = udf_rw16(crclen);
47049f988b79SJean-Baptiste Boric 
47059f988b79SJean-Baptiste Boric 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) efe);
47069f988b79SJean-Baptiste Boric 
47079f988b79SJean-Baptiste Boric 	return fidsize;
47089f988b79SJean-Baptiste Boric }
47099f988b79SJean-Baptiste Boric 
47109f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
47119f988b79SJean-Baptiste Boric 
47129f988b79SJean-Baptiste Boric int
udf_dir_detach(struct udf_mount * ump,struct udf_node * dir_node,struct udf_node * udf_node,struct componentname * cnp)47139f988b79SJean-Baptiste Boric udf_dir_detach(struct udf_mount *ump, struct udf_node *dir_node,
47149f988b79SJean-Baptiste Boric 	struct udf_node *udf_node, struct componentname *cnp)
47159f988b79SJean-Baptiste Boric {
47169f988b79SJean-Baptiste Boric 	struct vnode *dvp = dir_node->vnode;
47179f988b79SJean-Baptiste Boric 	struct dirhash       *dirh;
47189f988b79SJean-Baptiste Boric 	struct dirhash_entry *dirh_ep;
47199f988b79SJean-Baptiste Boric 	struct file_entry    *fe  = dir_node->fe;
47209f988b79SJean-Baptiste Boric 	struct fileid_desc *fid;
47219f988b79SJean-Baptiste Boric 	struct dirent *dirent;
47229f988b79SJean-Baptiste Boric 	uint64_t diroffset;
47239f988b79SJean-Baptiste Boric 	uint32_t lb_size, fidsize;
47249f988b79SJean-Baptiste Boric 	int found, error;
47259f988b79SJean-Baptiste Boric 	char const *name  = cnp->cn_nameptr;
47269f988b79SJean-Baptiste Boric 	int namelen = cnp->cn_namelen;
47279f988b79SJean-Baptiste Boric 	int hit, refcnt;
47289f988b79SJean-Baptiste Boric 
47299f988b79SJean-Baptiste Boric 	/* get our dirhash and make sure its read in */
47309f988b79SJean-Baptiste Boric 	dirhash_get(&dir_node->dir_hash);
47319f988b79SJean-Baptiste Boric 	error = udf_dirhash_fill(dir_node);
47329f988b79SJean-Baptiste Boric 	if (error) {
47339f988b79SJean-Baptiste Boric 		dirhash_put(dir_node->dir_hash);
47349f988b79SJean-Baptiste Boric 		return error;
47359f988b79SJean-Baptiste Boric 	}
47369f988b79SJean-Baptiste Boric 	dirh = dir_node->dir_hash;
47379f988b79SJean-Baptiste Boric 
47389f988b79SJean-Baptiste Boric 	/* get directory filesize */
47399f988b79SJean-Baptiste Boric 	if (!fe) {
47409f988b79SJean-Baptiste Boric 		assert(dir_node->efe);
47419f988b79SJean-Baptiste Boric 	}
47429f988b79SJean-Baptiste Boric 
47439f988b79SJean-Baptiste Boric 	/* allocate temporary space for fid */
47449f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
47459f988b79SJean-Baptiste Boric 	fid     = malloc(lb_size, M_UDFTEMP, M_WAITOK);
47469f988b79SJean-Baptiste Boric 	dirent  = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
47479f988b79SJean-Baptiste Boric 
47489f988b79SJean-Baptiste Boric 	/* search our dirhash hits */
47499f988b79SJean-Baptiste Boric 	found = 0;
47509f988b79SJean-Baptiste Boric 	dirh_ep = NULL;
47519f988b79SJean-Baptiste Boric 	for (;;) {
47529f988b79SJean-Baptiste Boric 		hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
47539f988b79SJean-Baptiste Boric 		/* if no hit, abort the search */
47549f988b79SJean-Baptiste Boric 		if (!hit)
47559f988b79SJean-Baptiste Boric 			break;
47569f988b79SJean-Baptiste Boric 
47579f988b79SJean-Baptiste Boric 		/* check this hit */
47589f988b79SJean-Baptiste Boric 		diroffset = dirh_ep->offset;
47599f988b79SJean-Baptiste Boric 
47609f988b79SJean-Baptiste Boric 		/* transfer a new fid/dirent */
47619f988b79SJean-Baptiste Boric 		error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
47629f988b79SJean-Baptiste Boric 		if (error)
47639f988b79SJean-Baptiste Boric 			break;
47649f988b79SJean-Baptiste Boric 
47659f988b79SJean-Baptiste Boric 		/* see if its our entry */
47669f988b79SJean-Baptiste Boric 		KASSERT(dirent->d_namlen == namelen);
47679f988b79SJean-Baptiste Boric 		if (strncmp(dirent->d_name, name, namelen) == 0) {
47689f988b79SJean-Baptiste Boric 			found = 1;
47699f988b79SJean-Baptiste Boric 			break;
47709f988b79SJean-Baptiste Boric 		}
47719f988b79SJean-Baptiste Boric 	}
47729f988b79SJean-Baptiste Boric 
47739f988b79SJean-Baptiste Boric 	if (!found)
47749f988b79SJean-Baptiste Boric 		error = ENOENT;
47759f988b79SJean-Baptiste Boric 	if (error)
47769f988b79SJean-Baptiste Boric 		goto error_out;
47779f988b79SJean-Baptiste Boric 
47789f988b79SJean-Baptiste Boric 	/* mark deleted */
47799f988b79SJean-Baptiste Boric 	fid->file_char |= UDF_FILE_CHAR_DEL;
47809f988b79SJean-Baptiste Boric #ifdef UDF_COMPLETE_DELETE
47819f988b79SJean-Baptiste Boric 	memset(&fid->icb, 0, sizeof(fid->icb));
47829f988b79SJean-Baptiste Boric #endif
47839f988b79SJean-Baptiste Boric 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
47849f988b79SJean-Baptiste Boric 
47859f988b79SJean-Baptiste Boric 	/* get size of fid and compensate for the read_fid_stream advance */
47869f988b79SJean-Baptiste Boric 	fidsize = udf_fidsize(fid);
47879f988b79SJean-Baptiste Boric 	diroffset -= fidsize;
47889f988b79SJean-Baptiste Boric 
47899f988b79SJean-Baptiste Boric 	/* write out */
47909f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_WRITE, dir_node->vnode,
47919f988b79SJean-Baptiste Boric 			fid, fidsize, diroffset,
47929f988b79SJean-Baptiste Boric 			UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
47939f988b79SJean-Baptiste Boric 			FSCRED, NULL, NULL);
47949f988b79SJean-Baptiste Boric 	if (error)
47959f988b79SJean-Baptiste Boric 		goto error_out;
47969f988b79SJean-Baptiste Boric 
47979f988b79SJean-Baptiste Boric 	/* get reference count of attached node */
47989f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
47999f988b79SJean-Baptiste Boric 		refcnt = udf_rw16(udf_node->fe->link_cnt);
48009f988b79SJean-Baptiste Boric 	} else {
48019f988b79SJean-Baptiste Boric 		KASSERT(udf_node->efe);
48029f988b79SJean-Baptiste Boric 		refcnt = udf_rw16(udf_node->efe->link_cnt);
48039f988b79SJean-Baptiste Boric 	}
48049f988b79SJean-Baptiste Boric #ifdef UDF_COMPLETE_DELETE
48059f988b79SJean-Baptiste Boric 	/* substract reference counter in attached node */
48069f988b79SJean-Baptiste Boric 	refcnt -= 1;
48079f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
48089f988b79SJean-Baptiste Boric 		udf_node->fe->link_cnt = udf_rw16(refcnt);
48099f988b79SJean-Baptiste Boric 	} else {
48109f988b79SJean-Baptiste Boric 		udf_node->efe->link_cnt = udf_rw16(refcnt);
48119f988b79SJean-Baptiste Boric 	}
48129f988b79SJean-Baptiste Boric 
48139f988b79SJean-Baptiste Boric 	/* prevent writeout when refcnt == 0 */
48149f988b79SJean-Baptiste Boric 	if (refcnt == 0)
48159f988b79SJean-Baptiste Boric 		udf_node->i_flags |= IN_DELETED;
48169f988b79SJean-Baptiste Boric 
48179f988b79SJean-Baptiste Boric 	if (fid->file_char & UDF_FILE_CHAR_DIR) {
48189f988b79SJean-Baptiste Boric 		int drefcnt;
48199f988b79SJean-Baptiste Boric 
48209f988b79SJean-Baptiste Boric 		/* substract reference counter in directory node */
48219f988b79SJean-Baptiste Boric 		/* note subtract 2 (?) for its was also backreferenced */
48229f988b79SJean-Baptiste Boric 		if (dir_node->fe) {
48239f988b79SJean-Baptiste Boric 			drefcnt  = udf_rw16(dir_node->fe->link_cnt);
48249f988b79SJean-Baptiste Boric 			drefcnt -= 1;
48259f988b79SJean-Baptiste Boric 			dir_node->fe->link_cnt = udf_rw16(drefcnt);
48269f988b79SJean-Baptiste Boric 		} else {
48279f988b79SJean-Baptiste Boric 			KASSERT(dir_node->efe);
48289f988b79SJean-Baptiste Boric 			drefcnt  = udf_rw16(dir_node->efe->link_cnt);
48299f988b79SJean-Baptiste Boric 			drefcnt -= 1;
48309f988b79SJean-Baptiste Boric 			dir_node->efe->link_cnt = udf_rw16(drefcnt);
48319f988b79SJean-Baptiste Boric 		}
48329f988b79SJean-Baptiste Boric 	}
48339f988b79SJean-Baptiste Boric 
48349f988b79SJean-Baptiste Boric 	udf_node->i_flags |= IN_MODIFIED;
48359f988b79SJean-Baptiste Boric 	dir_node->i_flags |= IN_MODIFIED;
48369f988b79SJean-Baptiste Boric #endif
48379f988b79SJean-Baptiste Boric 	/* if it is/was a hardlink adjust the file count */
48389f988b79SJean-Baptiste Boric 	if (refcnt > 0)
48399f988b79SJean-Baptiste Boric 		udf_adjust_filecount(udf_node, -1);
48409f988b79SJean-Baptiste Boric 
48419f988b79SJean-Baptiste Boric 	/* remove from the dirhash */
48429f988b79SJean-Baptiste Boric 	dirhash_remove(dirh, dirent, diroffset,
48439f988b79SJean-Baptiste Boric 		udf_fidsize(fid));
48449f988b79SJean-Baptiste Boric 
48459f988b79SJean-Baptiste Boric error_out:
48469f988b79SJean-Baptiste Boric 	free(fid, M_UDFTEMP);
48479f988b79SJean-Baptiste Boric 	free(dirent, M_UDFTEMP);
48489f988b79SJean-Baptiste Boric 
48499f988b79SJean-Baptiste Boric 	dirhash_put(dir_node->dir_hash);
48509f988b79SJean-Baptiste Boric 
48519f988b79SJean-Baptiste Boric 	return error;
48529f988b79SJean-Baptiste Boric }
48539f988b79SJean-Baptiste Boric 
48549f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
48559f988b79SJean-Baptiste Boric 
48569f988b79SJean-Baptiste Boric int
udf_dir_update_rootentry(struct udf_mount * ump,struct udf_node * dir_node,struct udf_node * new_parent_node)48579f988b79SJean-Baptiste Boric udf_dir_update_rootentry(struct udf_mount *ump, struct udf_node *dir_node,
48589f988b79SJean-Baptiste Boric 	struct udf_node *new_parent_node)
48599f988b79SJean-Baptiste Boric {
48609f988b79SJean-Baptiste Boric 	struct vnode *dvp = dir_node->vnode;
48619f988b79SJean-Baptiste Boric 	struct dirhash       *dirh;
48629f988b79SJean-Baptiste Boric 	struct dirhash_entry *dirh_ep;
48639f988b79SJean-Baptiste Boric 	struct file_entry    *fe;
48649f988b79SJean-Baptiste Boric 	struct extfile_entry *efe;
48659f988b79SJean-Baptiste Boric 	struct fileid_desc *fid;
48669f988b79SJean-Baptiste Boric 	struct dirent *dirent;
48679f988b79SJean-Baptiste Boric 	uint64_t diroffset;
48689f988b79SJean-Baptiste Boric 	uint64_t new_parent_unique_id;
48699f988b79SJean-Baptiste Boric 	uint32_t lb_size, fidsize;
48709f988b79SJean-Baptiste Boric 	int found, error;
48719f988b79SJean-Baptiste Boric 	char const *name  = "..";
48729f988b79SJean-Baptiste Boric 	int namelen = 2;
48739f988b79SJean-Baptiste Boric 	int hit;
48749f988b79SJean-Baptiste Boric 
48759f988b79SJean-Baptiste Boric 	/* get our dirhash and make sure its read in */
48769f988b79SJean-Baptiste Boric 	dirhash_get(&dir_node->dir_hash);
48779f988b79SJean-Baptiste Boric 	error = udf_dirhash_fill(dir_node);
48789f988b79SJean-Baptiste Boric 	if (error) {
48799f988b79SJean-Baptiste Boric 		dirhash_put(dir_node->dir_hash);
48809f988b79SJean-Baptiste Boric 		return error;
48819f988b79SJean-Baptiste Boric 	}
48829f988b79SJean-Baptiste Boric 	dirh = dir_node->dir_hash;
48839f988b79SJean-Baptiste Boric 
48849f988b79SJean-Baptiste Boric 	/* get new parent's unique ID */
48859f988b79SJean-Baptiste Boric 	fe  = new_parent_node->fe;
48869f988b79SJean-Baptiste Boric 	efe = new_parent_node->efe;
48879f988b79SJean-Baptiste Boric 	if (fe) {
48889f988b79SJean-Baptiste Boric 		new_parent_unique_id = udf_rw64(fe->unique_id);
48899f988b79SJean-Baptiste Boric 	} else {
48909f988b79SJean-Baptiste Boric 		assert(efe);
48919f988b79SJean-Baptiste Boric 		new_parent_unique_id = udf_rw64(efe->unique_id);
48929f988b79SJean-Baptiste Boric 	}
48939f988b79SJean-Baptiste Boric 
48949f988b79SJean-Baptiste Boric 	/* get directory filesize */
48959f988b79SJean-Baptiste Boric 	fe  = dir_node->fe;
48969f988b79SJean-Baptiste Boric 	efe = dir_node->efe;
48979f988b79SJean-Baptiste Boric 	if (!fe) {
48989f988b79SJean-Baptiste Boric 		assert(efe);
48999f988b79SJean-Baptiste Boric 	}
49009f988b79SJean-Baptiste Boric 
49019f988b79SJean-Baptiste Boric 	/* allocate temporary space for fid */
49029f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
49039f988b79SJean-Baptiste Boric 	fid     = malloc(lb_size, M_UDFTEMP, M_WAITOK);
49049f988b79SJean-Baptiste Boric 	dirent  = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
49059f988b79SJean-Baptiste Boric 
49069f988b79SJean-Baptiste Boric 	/*
49079f988b79SJean-Baptiste Boric 	 * NOTE the standard does not dictate the FID entry '..' should be
49089f988b79SJean-Baptiste Boric 	 * first, though in practice it will most likely be.
49099f988b79SJean-Baptiste Boric 	 */
49109f988b79SJean-Baptiste Boric 
49119f988b79SJean-Baptiste Boric 	/* search our dirhash hits */
49129f988b79SJean-Baptiste Boric 	found = 0;
49139f988b79SJean-Baptiste Boric 	dirh_ep = NULL;
49149f988b79SJean-Baptiste Boric 	for (;;) {
49159f988b79SJean-Baptiste Boric 		hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
49169f988b79SJean-Baptiste Boric 		/* if no hit, abort the search */
49179f988b79SJean-Baptiste Boric 		if (!hit)
49189f988b79SJean-Baptiste Boric 			break;
49199f988b79SJean-Baptiste Boric 
49209f988b79SJean-Baptiste Boric 		/* check this hit */
49219f988b79SJean-Baptiste Boric 		diroffset = dirh_ep->offset;
49229f988b79SJean-Baptiste Boric 
49239f988b79SJean-Baptiste Boric 		/* transfer a new fid/dirent */
49249f988b79SJean-Baptiste Boric 		error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
49259f988b79SJean-Baptiste Boric 		if (error)
49269f988b79SJean-Baptiste Boric 			break;
49279f988b79SJean-Baptiste Boric 
49289f988b79SJean-Baptiste Boric 		/* see if its our entry */
49299f988b79SJean-Baptiste Boric 		KASSERT(dirent->d_namlen == namelen);
49309f988b79SJean-Baptiste Boric 		if (strncmp(dirent->d_name, name, namelen) == 0) {
49319f988b79SJean-Baptiste Boric 			found = 1;
49329f988b79SJean-Baptiste Boric 			break;
49339f988b79SJean-Baptiste Boric 		}
49349f988b79SJean-Baptiste Boric 	}
49359f988b79SJean-Baptiste Boric 
49369f988b79SJean-Baptiste Boric 	if (!found)
49379f988b79SJean-Baptiste Boric 		error = ENOENT;
49389f988b79SJean-Baptiste Boric 	if (error)
49399f988b79SJean-Baptiste Boric 		goto error_out;
49409f988b79SJean-Baptiste Boric 
49419f988b79SJean-Baptiste Boric 	/* update our ICB to the new parent, hit of lower 32 bits of uniqueid */
49429f988b79SJean-Baptiste Boric 	fid->icb = new_parent_node->write_loc;
49439f988b79SJean-Baptiste Boric 	fid->icb.longad_uniqueid = udf_rw32(new_parent_unique_id);
49449f988b79SJean-Baptiste Boric 
49459f988b79SJean-Baptiste Boric 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
49469f988b79SJean-Baptiste Boric 
49479f988b79SJean-Baptiste Boric 	/* get size of fid and compensate for the read_fid_stream advance */
49489f988b79SJean-Baptiste Boric 	fidsize = udf_fidsize(fid);
49499f988b79SJean-Baptiste Boric 	diroffset -= fidsize;
49509f988b79SJean-Baptiste Boric 
49519f988b79SJean-Baptiste Boric 	/* write out */
49529f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_WRITE, dir_node->vnode,
49539f988b79SJean-Baptiste Boric 			fid, fidsize, diroffset,
49549f988b79SJean-Baptiste Boric 			UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
49559f988b79SJean-Baptiste Boric 			FSCRED, NULL, NULL);
49569f988b79SJean-Baptiste Boric 
49579f988b79SJean-Baptiste Boric 	/* nothing to be done in the dirhash */
49589f988b79SJean-Baptiste Boric 
49599f988b79SJean-Baptiste Boric error_out:
49609f988b79SJean-Baptiste Boric 	free(fid, M_UDFTEMP);
49619f988b79SJean-Baptiste Boric 	free(dirent, M_UDFTEMP);
49629f988b79SJean-Baptiste Boric 
49639f988b79SJean-Baptiste Boric 	dirhash_put(dir_node->dir_hash);
49649f988b79SJean-Baptiste Boric 
49659f988b79SJean-Baptiste Boric 	return error;
49669f988b79SJean-Baptiste Boric }
49679f988b79SJean-Baptiste Boric 
49689f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
49699f988b79SJean-Baptiste Boric 
49709f988b79SJean-Baptiste Boric /*
49719f988b79SJean-Baptiste Boric  * We are not allowed to split the fid tag itself over an logical block so
49729f988b79SJean-Baptiste Boric  * check the space remaining in the logical block.
49739f988b79SJean-Baptiste Boric  *
49749f988b79SJean-Baptiste Boric  * We try to select the smallest candidate for recycling or when none is
49759f988b79SJean-Baptiste Boric  * found, append a new one at the end of the directory.
49769f988b79SJean-Baptiste Boric  */
49779f988b79SJean-Baptiste Boric 
49789f988b79SJean-Baptiste Boric int
udf_dir_attach(struct udf_mount * ump,struct udf_node * dir_node,struct udf_node * udf_node,struct vattr * vap,struct componentname * cnp)49799f988b79SJean-Baptiste Boric udf_dir_attach(struct udf_mount *ump, struct udf_node *dir_node,
49809f988b79SJean-Baptiste Boric 	struct udf_node *udf_node, struct vattr *vap, struct componentname *cnp)
49819f988b79SJean-Baptiste Boric {
49829f988b79SJean-Baptiste Boric 	struct vnode *dvp = dir_node->vnode;
49839f988b79SJean-Baptiste Boric 	struct dirhash       *dirh;
49849f988b79SJean-Baptiste Boric 	struct dirhash_entry *dirh_ep;
49859f988b79SJean-Baptiste Boric 	struct fileid_desc   *fid;
49869f988b79SJean-Baptiste Boric 	struct icb_tag       *icbtag;
49879f988b79SJean-Baptiste Boric 	struct charspec osta_charspec;
49889f988b79SJean-Baptiste Boric 	struct dirent   dirent;
49899f988b79SJean-Baptiste Boric 	uint64_t unique_id, dir_size;
49909f988b79SJean-Baptiste Boric 	uint64_t fid_pos, end_fid_pos, chosen_fid_pos;
49919f988b79SJean-Baptiste Boric 	uint32_t chosen_size, chosen_size_diff;
49929f988b79SJean-Baptiste Boric 	int lb_size, lb_rest, fidsize, this_fidsize, size_diff;
49939f988b79SJean-Baptiste Boric 	int file_char, refcnt, icbflags, addr_type, hit, error;
49949f988b79SJean-Baptiste Boric 
49959f988b79SJean-Baptiste Boric 	/* get our dirhash and make sure its read in */
49969f988b79SJean-Baptiste Boric 	dirhash_get(&dir_node->dir_hash);
49979f988b79SJean-Baptiste Boric 	error = udf_dirhash_fill(dir_node);
49989f988b79SJean-Baptiste Boric 	if (error) {
49999f988b79SJean-Baptiste Boric 		dirhash_put(dir_node->dir_hash);
50009f988b79SJean-Baptiste Boric 		return error;
50019f988b79SJean-Baptiste Boric 	}
50029f988b79SJean-Baptiste Boric 	dirh = dir_node->dir_hash;
50039f988b79SJean-Baptiste Boric 
50049f988b79SJean-Baptiste Boric 	/* get info */
50059f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
50069f988b79SJean-Baptiste Boric 	udf_osta_charset(&osta_charspec);
50079f988b79SJean-Baptiste Boric 
50089f988b79SJean-Baptiste Boric 	if (dir_node->fe) {
50099f988b79SJean-Baptiste Boric 		dir_size = udf_rw64(dir_node->fe->inf_len);
50109f988b79SJean-Baptiste Boric 		icbtag   = &dir_node->fe->icbtag;
50119f988b79SJean-Baptiste Boric 	} else {
50129f988b79SJean-Baptiste Boric 		dir_size = udf_rw64(dir_node->efe->inf_len);
50139f988b79SJean-Baptiste Boric 		icbtag   = &dir_node->efe->icbtag;
50149f988b79SJean-Baptiste Boric 	}
50159f988b79SJean-Baptiste Boric 
50169f988b79SJean-Baptiste Boric 	icbflags   = udf_rw16(icbtag->flags);
50179f988b79SJean-Baptiste Boric 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
50189f988b79SJean-Baptiste Boric 
50199f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
50209f988b79SJean-Baptiste Boric 		unique_id = udf_rw64(udf_node->fe->unique_id);
50219f988b79SJean-Baptiste Boric 		refcnt    = udf_rw16(udf_node->fe->link_cnt);
50229f988b79SJean-Baptiste Boric 	} else {
50239f988b79SJean-Baptiste Boric 		unique_id = udf_rw64(udf_node->efe->unique_id);
50249f988b79SJean-Baptiste Boric 		refcnt    = udf_rw16(udf_node->efe->link_cnt);
50259f988b79SJean-Baptiste Boric 	}
50269f988b79SJean-Baptiste Boric 
50279f988b79SJean-Baptiste Boric 	if (refcnt > 0) {
50289f988b79SJean-Baptiste Boric 		unique_id = udf_advance_uniqueid(ump);
50299f988b79SJean-Baptiste Boric 		udf_adjust_filecount(udf_node, 1);
50309f988b79SJean-Baptiste Boric 	}
50319f988b79SJean-Baptiste Boric 
50329f988b79SJean-Baptiste Boric 	/* determine file characteristics */
50339f988b79SJean-Baptiste Boric 	file_char = 0;	/* visible non deleted file and not stream metadata */
50349f988b79SJean-Baptiste Boric 	if (vap->va_type == VDIR)
50359f988b79SJean-Baptiste Boric 		file_char = UDF_FILE_CHAR_DIR;
50369f988b79SJean-Baptiste Boric 
50379f988b79SJean-Baptiste Boric 	/* malloc scrap buffer */
50389f988b79SJean-Baptiste Boric 	fid = malloc(lb_size, M_TEMP, M_WAITOK|M_ZERO);
50399f988b79SJean-Baptiste Boric 
50409f988b79SJean-Baptiste Boric 	/* calculate _minimum_ fid size */
50419f988b79SJean-Baptiste Boric 	unix_to_udf_name((char *) fid->data, &fid->l_fi,
50429f988b79SJean-Baptiste Boric 		cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
50439f988b79SJean-Baptiste Boric 	fidsize = UDF_FID_SIZE + fid->l_fi;
50449f988b79SJean-Baptiste Boric 	fidsize = (fidsize + 3) & ~3;		/* multiple of 4 */
50459f988b79SJean-Baptiste Boric 
50469f988b79SJean-Baptiste Boric 	/* find position that will fit the FID */
50479f988b79SJean-Baptiste Boric 	chosen_fid_pos   = dir_size;
50489f988b79SJean-Baptiste Boric 	chosen_size      = 0;
50499f988b79SJean-Baptiste Boric 	chosen_size_diff = UINT_MAX;
50509f988b79SJean-Baptiste Boric 
50519f988b79SJean-Baptiste Boric 	/* shut up gcc */
50529f988b79SJean-Baptiste Boric 	dirent.d_namlen = 0;
50539f988b79SJean-Baptiste Boric 
50549f988b79SJean-Baptiste Boric 	/* search our dirhash hits */
50559f988b79SJean-Baptiste Boric 	error = 0;
50569f988b79SJean-Baptiste Boric 	dirh_ep = NULL;
50579f988b79SJean-Baptiste Boric 	for (;;) {
50589f988b79SJean-Baptiste Boric 		hit = dirhash_lookup_freed(dirh, fidsize, &dirh_ep);
50599f988b79SJean-Baptiste Boric 		/* if no hit, abort the search */
50609f988b79SJean-Baptiste Boric 		if (!hit)
50619f988b79SJean-Baptiste Boric 			break;
50629f988b79SJean-Baptiste Boric 
50639f988b79SJean-Baptiste Boric 		/* check this hit for size */
50649f988b79SJean-Baptiste Boric 		this_fidsize = dirh_ep->entry_size;
50659f988b79SJean-Baptiste Boric 
50669f988b79SJean-Baptiste Boric 		/* check this hit */
50679f988b79SJean-Baptiste Boric 		fid_pos     = dirh_ep->offset;
50689f988b79SJean-Baptiste Boric 		end_fid_pos = fid_pos + this_fidsize;
50699f988b79SJean-Baptiste Boric 		size_diff   = this_fidsize - fidsize;
50709f988b79SJean-Baptiste Boric 		lb_rest = lb_size - (end_fid_pos % lb_size);
50719f988b79SJean-Baptiste Boric 
50729f988b79SJean-Baptiste Boric #ifndef UDF_COMPLETE_DELETE
50739f988b79SJean-Baptiste Boric 		/* transfer a new fid/dirent */
50749f988b79SJean-Baptiste Boric 		error = udf_read_fid_stream(vp, &fid_pos, fid, dirent);
50759f988b79SJean-Baptiste Boric 		if (error)
50769f988b79SJean-Baptiste Boric 			goto error_out;
50779f988b79SJean-Baptiste Boric 
50789f988b79SJean-Baptiste Boric 		/* only reuse entries that are wiped */
50799f988b79SJean-Baptiste Boric 		/* check if the len + loc are marked zero */
50809f988b79SJean-Baptiste Boric 		if (udf_rw32(fid->icb.len) != 0)
50819f988b79SJean-Baptiste Boric 			continue;
50829f988b79SJean-Baptiste Boric 		if (udf_rw32(fid->icb.loc.lb_num) != 0)
50839f988b79SJean-Baptiste Boric 			continue;
50849f988b79SJean-Baptiste Boric 		if (udf_rw16(fid->icb.loc.part_num) != 0)
50859f988b79SJean-Baptiste Boric 			continue;
50869f988b79SJean-Baptiste Boric #endif	/* UDF_COMPLETE_DELETE */
50879f988b79SJean-Baptiste Boric 
50889f988b79SJean-Baptiste Boric 		/* select if not splitting the tag and its smaller */
50899f988b79SJean-Baptiste Boric 		if ((size_diff >= 0)  &&
50909f988b79SJean-Baptiste Boric 			(size_diff < chosen_size_diff) &&
50919f988b79SJean-Baptiste Boric 			(lb_rest >= sizeof(struct desc_tag)))
50929f988b79SJean-Baptiste Boric 		{
50939f988b79SJean-Baptiste Boric 			/* UDF 2.3.4.2+3 specifies rules for iu size */
50949f988b79SJean-Baptiste Boric 			if ((size_diff == 0) || (size_diff >= 32)) {
50959f988b79SJean-Baptiste Boric 				chosen_fid_pos   = fid_pos;
50969f988b79SJean-Baptiste Boric 				chosen_size      = this_fidsize;
50979f988b79SJean-Baptiste Boric 				chosen_size_diff = size_diff;
50989f988b79SJean-Baptiste Boric 			}
50999f988b79SJean-Baptiste Boric 		}
51009f988b79SJean-Baptiste Boric 	}
51019f988b79SJean-Baptiste Boric 
51029f988b79SJean-Baptiste Boric 
51039f988b79SJean-Baptiste Boric 	/* extend directory if no other candidate found */
51049f988b79SJean-Baptiste Boric 	if (chosen_size == 0) {
51059f988b79SJean-Baptiste Boric 		chosen_fid_pos   = dir_size;
51069f988b79SJean-Baptiste Boric 		chosen_size      = fidsize;
51079f988b79SJean-Baptiste Boric 		chosen_size_diff = 0;
51089f988b79SJean-Baptiste Boric 
51099f988b79SJean-Baptiste Boric 		/* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
51109f988b79SJean-Baptiste Boric 		if (addr_type == UDF_ICB_INTERN_ALLOC) {
51119f988b79SJean-Baptiste Boric 			/* pre-grow directory to see if we're to switch */
51129f988b79SJean-Baptiste Boric 			udf_grow_node(dir_node, dir_size + chosen_size);
51139f988b79SJean-Baptiste Boric 
51149f988b79SJean-Baptiste Boric 			icbflags   = udf_rw16(icbtag->flags);
51159f988b79SJean-Baptiste Boric 			addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
51169f988b79SJean-Baptiste Boric 		}
51179f988b79SJean-Baptiste Boric 
51189f988b79SJean-Baptiste Boric 		/* make sure the next fid desc_tag won't be splitted */
51199f988b79SJean-Baptiste Boric 		if (addr_type != UDF_ICB_INTERN_ALLOC) {
51209f988b79SJean-Baptiste Boric 			end_fid_pos = chosen_fid_pos + chosen_size;
51219f988b79SJean-Baptiste Boric 			lb_rest = lb_size - (end_fid_pos % lb_size);
51229f988b79SJean-Baptiste Boric 
51239f988b79SJean-Baptiste Boric 			/* pad with implementation use regid if needed */
51249f988b79SJean-Baptiste Boric 			if (lb_rest < sizeof(struct desc_tag))
51259f988b79SJean-Baptiste Boric 				chosen_size += 32;
51269f988b79SJean-Baptiste Boric 		}
51279f988b79SJean-Baptiste Boric 	}
51289f988b79SJean-Baptiste Boric 	chosen_size_diff = chosen_size - fidsize;
51299f988b79SJean-Baptiste Boric 
51309f988b79SJean-Baptiste Boric 	/* populate the FID */
51319f988b79SJean-Baptiste Boric 	memset(fid, 0, lb_size);
51329f988b79SJean-Baptiste Boric 	udf_inittag(ump, &fid->tag, TAGID_FID, 0);
51339f988b79SJean-Baptiste Boric 	fid->file_version_num    = udf_rw16(1);	/* UDF 2.3.4.1 */
51349f988b79SJean-Baptiste Boric 	fid->file_char           = file_char;
51359f988b79SJean-Baptiste Boric 	fid->icb                 = udf_node->loc;
51369f988b79SJean-Baptiste Boric 	fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
51379f988b79SJean-Baptiste Boric 	fid->l_iu                = udf_rw16(0);
51389f988b79SJean-Baptiste Boric 
51399f988b79SJean-Baptiste Boric 	if (chosen_size > fidsize) {
51409f988b79SJean-Baptiste Boric 		/* insert implementation-use regid to space it correctly */
51419f988b79SJean-Baptiste Boric 		fid->l_iu = udf_rw16(chosen_size_diff);
51429f988b79SJean-Baptiste Boric 
51439f988b79SJean-Baptiste Boric 		/* set implementation use */
51449f988b79SJean-Baptiste Boric 		udf_set_regid((struct regid *) fid->data, IMPL_NAME);
51459f988b79SJean-Baptiste Boric 		udf_add_impl_regid(ump, (struct regid *) fid->data);
51469f988b79SJean-Baptiste Boric 	}
51479f988b79SJean-Baptiste Boric 
51489f988b79SJean-Baptiste Boric 	/* fill in name */
51499f988b79SJean-Baptiste Boric 	unix_to_udf_name((char *) fid->data + udf_rw16(fid->l_iu),
51509f988b79SJean-Baptiste Boric 		&fid->l_fi, cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
51519f988b79SJean-Baptiste Boric 
51529f988b79SJean-Baptiste Boric 	fid->tag.desc_crc_len = udf_rw16(chosen_size - UDF_DESC_TAG_LENGTH);
51539f988b79SJean-Baptiste Boric 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
51549f988b79SJean-Baptiste Boric 
51559f988b79SJean-Baptiste Boric 	/* writeout FID/update parent directory */
51569f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_WRITE, dvp,
51579f988b79SJean-Baptiste Boric 			fid, chosen_size, chosen_fid_pos,
51589f988b79SJean-Baptiste Boric 			UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
51599f988b79SJean-Baptiste Boric 			FSCRED, NULL, NULL);
51609f988b79SJean-Baptiste Boric 
51619f988b79SJean-Baptiste Boric 	if (error)
51629f988b79SJean-Baptiste Boric 		goto error_out;
51639f988b79SJean-Baptiste Boric 
51649f988b79SJean-Baptiste Boric 	/* add reference counter in attached node */
51659f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
51669f988b79SJean-Baptiste Boric 		refcnt = udf_rw16(udf_node->fe->link_cnt);
51679f988b79SJean-Baptiste Boric 		udf_node->fe->link_cnt = udf_rw16(refcnt+1);
51689f988b79SJean-Baptiste Boric 	} else {
51699f988b79SJean-Baptiste Boric 		KASSERT(udf_node->efe);
51709f988b79SJean-Baptiste Boric 		refcnt = udf_rw16(udf_node->efe->link_cnt);
51719f988b79SJean-Baptiste Boric 		udf_node->efe->link_cnt = udf_rw16(refcnt+1);
51729f988b79SJean-Baptiste Boric 	}
51739f988b79SJean-Baptiste Boric 
51749f988b79SJean-Baptiste Boric 	/* mark not deleted if it was... just in case, but do warn */
51759f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & IN_DELETED) {
51769f988b79SJean-Baptiste Boric 		printf("udf: warning, marking a file undeleted\n");
51779f988b79SJean-Baptiste Boric 		udf_node->i_flags &= ~IN_DELETED;
51789f988b79SJean-Baptiste Boric 	}
51799f988b79SJean-Baptiste Boric 
51809f988b79SJean-Baptiste Boric 	if (file_char & UDF_FILE_CHAR_DIR) {
51819f988b79SJean-Baptiste Boric 		/* add reference counter in directory node for '..' */
51829f988b79SJean-Baptiste Boric 		if (dir_node->fe) {
51839f988b79SJean-Baptiste Boric 			refcnt = udf_rw16(dir_node->fe->link_cnt);
51849f988b79SJean-Baptiste Boric 			refcnt++;
51859f988b79SJean-Baptiste Boric 			dir_node->fe->link_cnt = udf_rw16(refcnt);
51869f988b79SJean-Baptiste Boric 		} else {
51879f988b79SJean-Baptiste Boric 			KASSERT(dir_node->efe);
51889f988b79SJean-Baptiste Boric 			refcnt = udf_rw16(dir_node->efe->link_cnt);
51899f988b79SJean-Baptiste Boric 			refcnt++;
51909f988b79SJean-Baptiste Boric 			dir_node->efe->link_cnt = udf_rw16(refcnt);
51919f988b79SJean-Baptiste Boric 		}
51929f988b79SJean-Baptiste Boric 	}
51939f988b79SJean-Baptiste Boric 
51949f988b79SJean-Baptiste Boric 	/* append to the dirhash */
51959f988b79SJean-Baptiste Boric 	/* NOTE do not use dirent anymore or it won't match later! */
51969f988b79SJean-Baptiste Boric 	udf_to_unix_name(dirent.d_name, NAME_MAX,
51979f988b79SJean-Baptiste Boric 		(char *) fid->data + udf_rw16(fid->l_iu), fid->l_fi, &osta_charspec);
51989f988b79SJean-Baptiste Boric 	dirent.d_namlen = strlen(dirent.d_name);
51999f988b79SJean-Baptiste Boric 	dirhash_enter(dirh, &dirent, chosen_fid_pos,
52009f988b79SJean-Baptiste Boric 		udf_fidsize(fid), 1);
52019f988b79SJean-Baptiste Boric 
52029f988b79SJean-Baptiste Boric 	/* note updates */
52039f988b79SJean-Baptiste Boric 	udf_node->i_flags |= IN_CHANGE | IN_MODIFY; /* | IN_CREATE? */
52049f988b79SJean-Baptiste Boric 	/* VN_KNOTE(udf_node,  ...) */
52059f988b79SJean-Baptiste Boric 	udf_update(udf_node->vnode, NULL, NULL, NULL, 0);
52069f988b79SJean-Baptiste Boric 
52079f988b79SJean-Baptiste Boric error_out:
52089f988b79SJean-Baptiste Boric 	free(fid, M_TEMP);
52099f988b79SJean-Baptiste Boric 
52109f988b79SJean-Baptiste Boric 	dirhash_put(dir_node->dir_hash);
52119f988b79SJean-Baptiste Boric 
52129f988b79SJean-Baptiste Boric 	return error;
52139f988b79SJean-Baptiste Boric }
52149f988b79SJean-Baptiste Boric 
52159f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
52169f988b79SJean-Baptiste Boric 
52179f988b79SJean-Baptiste Boric /*
52189f988b79SJean-Baptiste Boric  * Each node can have an attached streamdir node though not recursively. These
52199f988b79SJean-Baptiste Boric  * are otherwise known as named substreams/named extended attributes that have
52209f988b79SJean-Baptiste Boric  * no size limitations.
52219f988b79SJean-Baptiste Boric  *
52229f988b79SJean-Baptiste Boric  * `Normal' extended attributes are indicated with a number and are recorded
52239f988b79SJean-Baptiste Boric  * in either the fe/efe descriptor itself for small descriptors or recorded in
52249f988b79SJean-Baptiste Boric  * the attached extended attribute file. Since these spaces can get
52259f988b79SJean-Baptiste Boric  * fragmented, care ought to be taken.
52269f988b79SJean-Baptiste Boric  *
52279f988b79SJean-Baptiste Boric  * Since the size of the space reserved for allocation descriptors is limited,
52289f988b79SJean-Baptiste Boric  * there is a mechanim provided for extending this space; this is done by a
52299f988b79SJean-Baptiste Boric  * special extent to allow schrinking of the allocations without breaking the
52309f988b79SJean-Baptiste Boric  * linkage to the allocation extent descriptor.
52319f988b79SJean-Baptiste Boric  */
52329f988b79SJean-Baptiste Boric 
52339f988b79SJean-Baptiste Boric int
udf_loadvnode(struct mount * mp,struct vnode * vp,const void * key,size_t key_len,const void ** new_key)5234*0a6a1f1dSLionel Sambuc udf_loadvnode(struct mount *mp, struct vnode *vp,
5235*0a6a1f1dSLionel Sambuc      const void *key, size_t key_len, const void **new_key)
52369f988b79SJean-Baptiste Boric {
52379f988b79SJean-Baptiste Boric 	union dscrptr   *dscr;
5238*0a6a1f1dSLionel Sambuc 	struct udf_mount *ump;
52399f988b79SJean-Baptiste Boric 	struct udf_node *udf_node;
5240*0a6a1f1dSLionel Sambuc 	struct long_ad node_icb_loc, icb_loc, next_icb_loc, last_fe_icb_loc;
52419f988b79SJean-Baptiste Boric 	uint64_t file_size;
52429f988b79SJean-Baptiste Boric 	uint32_t lb_size, sector, dummy;
52439f988b79SJean-Baptiste Boric 	int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
52449f988b79SJean-Baptiste Boric 	int slot, eof, error;
5245*0a6a1f1dSLionel Sambuc 	int num_indir_followed = 0;
52469f988b79SJean-Baptiste Boric 
5247*0a6a1f1dSLionel Sambuc 	DPRINTF(NODE, ("udf_loadvnode called\n"));
5248*0a6a1f1dSLionel Sambuc 	udf_node = NULL;
5249*0a6a1f1dSLionel Sambuc 	ump = VFSTOUDF(mp);
52509f988b79SJean-Baptiste Boric 
5251*0a6a1f1dSLionel Sambuc 	KASSERT(key_len == sizeof(node_icb_loc.loc));
5252*0a6a1f1dSLionel Sambuc 	memset(&node_icb_loc, 0, sizeof(node_icb_loc));
5253*0a6a1f1dSLionel Sambuc 	node_icb_loc.len = ump->logical_vol->lb_size;
5254*0a6a1f1dSLionel Sambuc 	memcpy(&node_icb_loc.loc, key, key_len);
52559f988b79SJean-Baptiste Boric 
52569f988b79SJean-Baptiste Boric 	/* garbage check: translate udf_node_icb_loc to sectornr */
5257*0a6a1f1dSLionel Sambuc 	error = udf_translate_vtop(ump, &node_icb_loc, &sector, &dummy);
52589f988b79SJean-Baptiste Boric 	if (error) {
52599f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("\tcan't translate icb address!\n"));
52609f988b79SJean-Baptiste Boric 		/* no use, this will fail anyway */
52619f988b79SJean-Baptiste Boric 		return EINVAL;
52629f988b79SJean-Baptiste Boric 	}
52639f988b79SJean-Baptiste Boric 
52649f988b79SJean-Baptiste Boric 	/* build udf_node (do initialise!) */
52659f988b79SJean-Baptiste Boric 	udf_node = pool_get(&udf_node_pool, PR_WAITOK);
52669f988b79SJean-Baptiste Boric 	memset(udf_node, 0, sizeof(struct udf_node));
52679f988b79SJean-Baptiste Boric 
5268*0a6a1f1dSLionel Sambuc 	vp->v_tag = VT_UDF;
5269*0a6a1f1dSLionel Sambuc 	vp->v_op = udf_vnodeop_p;
5270*0a6a1f1dSLionel Sambuc 	vp->v_data = udf_node;
52719f988b79SJean-Baptiste Boric 
52729f988b79SJean-Baptiste Boric 	/* initialise crosslinks, note location of fe/efe for hashing */
52739f988b79SJean-Baptiste Boric 	udf_node->ump    =  ump;
5274*0a6a1f1dSLionel Sambuc 	udf_node->vnode  =  vp;
5275*0a6a1f1dSLionel Sambuc 	udf_node->loc    =  node_icb_loc;
52769f988b79SJean-Baptiste Boric 	udf_node->lockf  =  0;
52779f988b79SJean-Baptiste Boric 	mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
52789f988b79SJean-Baptiste Boric 	cv_init(&udf_node->node_lock, "udf_nlk");
5279*0a6a1f1dSLionel Sambuc 	genfs_node_init(vp, &udf_genfsops);	/* inititise genfs */
52809f988b79SJean-Baptiste Boric 	udf_node->outstanding_bufs = 0;
52819f988b79SJean-Baptiste Boric 	udf_node->outstanding_nodedscr = 0;
52829f988b79SJean-Baptiste Boric 	udf_node->uncommitted_lbs = 0;
52839f988b79SJean-Baptiste Boric 
52849f988b79SJean-Baptiste Boric 	/* check if we're fetching the root */
52859f988b79SJean-Baptiste Boric 	if (ump->fileset_desc)
52869f988b79SJean-Baptiste Boric 		if (memcmp(&udf_node->loc, &ump->fileset_desc->rootdir_icb,
52879f988b79SJean-Baptiste Boric 		    sizeof(struct long_ad)) == 0)
5288*0a6a1f1dSLionel Sambuc 			vp->v_vflag |= VV_ROOT;
52899f988b79SJean-Baptiste Boric 
5290*0a6a1f1dSLionel Sambuc 	icb_loc = node_icb_loc;
52919f988b79SJean-Baptiste Boric 	needs_indirect = 0;
52929f988b79SJean-Baptiste Boric 	strat4096 = 0;
52939f988b79SJean-Baptiste Boric 	udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
52949f988b79SJean-Baptiste Boric 	file_size = 0;
52959f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
52969f988b79SJean-Baptiste Boric 
52979f988b79SJean-Baptiste Boric 	DPRINTF(NODE, ("\tstart reading descriptors\n"));
52989f988b79SJean-Baptiste Boric 	do {
52999f988b79SJean-Baptiste Boric 		/* try to read in fe/efe */
53009f988b79SJean-Baptiste Boric 		error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
53019f988b79SJean-Baptiste Boric 
53029f988b79SJean-Baptiste Boric 		/* blank sector marks end of sequence, check this */
53039f988b79SJean-Baptiste Boric 		if ((dscr == NULL) &&  (!strat4096))
53049f988b79SJean-Baptiste Boric 			error = ENOENT;
53059f988b79SJean-Baptiste Boric 
53069f988b79SJean-Baptiste Boric 		/* break if read error or blank sector */
53079f988b79SJean-Baptiste Boric 		if (error || (dscr == NULL))
53089f988b79SJean-Baptiste Boric 			break;
53099f988b79SJean-Baptiste Boric 
53109f988b79SJean-Baptiste Boric 		/* process descriptor based on the descriptor type */
53119f988b79SJean-Baptiste Boric 		dscr_type = udf_rw16(dscr->tag.id);
53129f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("\tread descriptor %d\n", dscr_type));
53139f988b79SJean-Baptiste Boric 
53149f988b79SJean-Baptiste Boric 		/* if dealing with an indirect entry, follow the link */
53159f988b79SJean-Baptiste Boric 		if (dscr_type == TAGID_INDIRECTENTRY) {
53169f988b79SJean-Baptiste Boric 			needs_indirect = 0;
5317*0a6a1f1dSLionel Sambuc 			next_icb_loc = dscr->inde.indirect_icb;
53189f988b79SJean-Baptiste Boric 			udf_free_logvol_dscr(ump, &icb_loc, dscr);
5319*0a6a1f1dSLionel Sambuc 			icb_loc = next_icb_loc;
5320*0a6a1f1dSLionel Sambuc 			if (++num_indir_followed > UDF_MAX_INDIRS_FOLLOW) {
5321*0a6a1f1dSLionel Sambuc 				error = EMLINK;
5322*0a6a1f1dSLionel Sambuc 				break;
5323*0a6a1f1dSLionel Sambuc 			}
53249f988b79SJean-Baptiste Boric 			continue;
53259f988b79SJean-Baptiste Boric 		}
53269f988b79SJean-Baptiste Boric 
53279f988b79SJean-Baptiste Boric 		/* only file entries and extended file entries allowed here */
53289f988b79SJean-Baptiste Boric 		if ((dscr_type != TAGID_FENTRY) &&
53299f988b79SJean-Baptiste Boric 		    (dscr_type != TAGID_EXTFENTRY)) {
53309f988b79SJean-Baptiste Boric 			udf_free_logvol_dscr(ump, &icb_loc, dscr);
53319f988b79SJean-Baptiste Boric 			error = ENOENT;
53329f988b79SJean-Baptiste Boric 			break;
53339f988b79SJean-Baptiste Boric 		}
53349f988b79SJean-Baptiste Boric 
53359f988b79SJean-Baptiste Boric 		KASSERT(udf_tagsize(dscr, lb_size) == lb_size);
53369f988b79SJean-Baptiste Boric 
53379f988b79SJean-Baptiste Boric 		/* choose this one */
53389f988b79SJean-Baptiste Boric 		last_fe_icb_loc = icb_loc;
53399f988b79SJean-Baptiste Boric 
53409f988b79SJean-Baptiste Boric 		/* record and process/update (ext)fentry */
53419f988b79SJean-Baptiste Boric 		if (dscr_type == TAGID_FENTRY) {
53429f988b79SJean-Baptiste Boric 			if (udf_node->fe)
53439f988b79SJean-Baptiste Boric 				udf_free_logvol_dscr(ump, &last_fe_icb_loc,
53449f988b79SJean-Baptiste Boric 					udf_node->fe);
53459f988b79SJean-Baptiste Boric 			udf_node->fe  = &dscr->fe;
53469f988b79SJean-Baptiste Boric 			strat = udf_rw16(udf_node->fe->icbtag.strat_type);
53479f988b79SJean-Baptiste Boric 			udf_file_type = udf_node->fe->icbtag.file_type;
53489f988b79SJean-Baptiste Boric 			file_size = udf_rw64(udf_node->fe->inf_len);
53499f988b79SJean-Baptiste Boric 		} else {
53509f988b79SJean-Baptiste Boric 			if (udf_node->efe)
53519f988b79SJean-Baptiste Boric 				udf_free_logvol_dscr(ump, &last_fe_icb_loc,
53529f988b79SJean-Baptiste Boric 					udf_node->efe);
53539f988b79SJean-Baptiste Boric 			udf_node->efe = &dscr->efe;
53549f988b79SJean-Baptiste Boric 			strat = udf_rw16(udf_node->efe->icbtag.strat_type);
53559f988b79SJean-Baptiste Boric 			udf_file_type = udf_node->efe->icbtag.file_type;
53569f988b79SJean-Baptiste Boric 			file_size = udf_rw64(udf_node->efe->inf_len);
53579f988b79SJean-Baptiste Boric 		}
53589f988b79SJean-Baptiste Boric 
53599f988b79SJean-Baptiste Boric 		/* check recording strategy (structure) */
53609f988b79SJean-Baptiste Boric 
53619f988b79SJean-Baptiste Boric 		/*
53629f988b79SJean-Baptiste Boric 		 * Strategy 4096 is a daisy linked chain terminating with an
53639f988b79SJean-Baptiste Boric 		 * unrecorded sector or a TERM descriptor. The next
53649f988b79SJean-Baptiste Boric 		 * descriptor is to be found in the sector that follows the
53659f988b79SJean-Baptiste Boric 		 * current sector.
53669f988b79SJean-Baptiste Boric 		 */
53679f988b79SJean-Baptiste Boric 		if (strat == 4096) {
53689f988b79SJean-Baptiste Boric 			strat4096 = 1;
53699f988b79SJean-Baptiste Boric 			needs_indirect = 1;
53709f988b79SJean-Baptiste Boric 
53719f988b79SJean-Baptiste Boric 			icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
53729f988b79SJean-Baptiste Boric 		}
53739f988b79SJean-Baptiste Boric 
53749f988b79SJean-Baptiste Boric 		/*
53759f988b79SJean-Baptiste Boric 		 * Strategy 4 is the normal strategy and terminates, but if
53769f988b79SJean-Baptiste Boric 		 * we're in strategy 4096, we can't have strategy 4 mixed in
53779f988b79SJean-Baptiste Boric 		 */
53789f988b79SJean-Baptiste Boric 
53799f988b79SJean-Baptiste Boric 		if (strat == 4) {
53809f988b79SJean-Baptiste Boric 			if (strat4096) {
53819f988b79SJean-Baptiste Boric 				error = EINVAL;
53829f988b79SJean-Baptiste Boric 				break;
53839f988b79SJean-Baptiste Boric 			}
53849f988b79SJean-Baptiste Boric 			break;		/* done */
53859f988b79SJean-Baptiste Boric 		}
53869f988b79SJean-Baptiste Boric 	} while (!error);
53879f988b79SJean-Baptiste Boric 
53889f988b79SJean-Baptiste Boric 	/* first round of cleanup code */
53899f988b79SJean-Baptiste Boric 	if (error) {
53909f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("\tnode fe/efe failed!\n"));
53919f988b79SJean-Baptiste Boric 		/* recycle udf_node */
53929f988b79SJean-Baptiste Boric 		udf_dispose_node(udf_node);
53939f988b79SJean-Baptiste Boric 
53949f988b79SJean-Baptiste Boric 		return EINVAL;		/* error code ok? */
53959f988b79SJean-Baptiste Boric 	}
53969f988b79SJean-Baptiste Boric 	DPRINTF(NODE, ("\tnode fe/efe read in fine\n"));
53979f988b79SJean-Baptiste Boric 
53989f988b79SJean-Baptiste Boric 	/* assert no references to dscr anymore beyong this point */
53999f988b79SJean-Baptiste Boric 	assert((udf_node->fe) || (udf_node->efe));
54009f988b79SJean-Baptiste Boric 	dscr = NULL;
54019f988b79SJean-Baptiste Boric 
54029f988b79SJean-Baptiste Boric 	/*
54039f988b79SJean-Baptiste Boric 	 * Remember where to record an updated version of the descriptor. If
54049f988b79SJean-Baptiste Boric 	 * there is a sequence of indirect entries, icb_loc will have been
54059f988b79SJean-Baptiste Boric 	 * updated. Its the write disipline to allocate new space and to make
54069f988b79SJean-Baptiste Boric 	 * sure the chain is maintained.
54079f988b79SJean-Baptiste Boric 	 *
54089f988b79SJean-Baptiste Boric 	 * `needs_indirect' flags if the next location is to be filled with
54099f988b79SJean-Baptiste Boric 	 * with an indirect entry.
54109f988b79SJean-Baptiste Boric 	 */
54119f988b79SJean-Baptiste Boric 	udf_node->write_loc = icb_loc;
54129f988b79SJean-Baptiste Boric 	udf_node->needs_indirect = needs_indirect;
54139f988b79SJean-Baptiste Boric 
54149f988b79SJean-Baptiste Boric 	/*
54159f988b79SJean-Baptiste Boric 	 * Go trough all allocations extents of this descriptor and when
54169f988b79SJean-Baptiste Boric 	 * encountering a redirect read in the allocation extension. These are
54179f988b79SJean-Baptiste Boric 	 * daisy-chained.
54189f988b79SJean-Baptiste Boric 	 */
54199f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
54209f988b79SJean-Baptiste Boric 	udf_node->num_extensions = 0;
54219f988b79SJean-Baptiste Boric 
54229f988b79SJean-Baptiste Boric 	error   = 0;
54239f988b79SJean-Baptiste Boric 	slot    = 0;
54249f988b79SJean-Baptiste Boric 	for (;;) {
54259f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &icb_loc, &eof);
54269f988b79SJean-Baptiste Boric 		DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
54279f988b79SJean-Baptiste Boric 			"lb_num = %d, part = %d\n", slot, eof,
54289f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(icb_loc.len)),
54299f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(icb_loc.len)),
54309f988b79SJean-Baptiste Boric 			udf_rw32(icb_loc.loc.lb_num),
54319f988b79SJean-Baptiste Boric 			udf_rw16(icb_loc.loc.part_num)));
54329f988b79SJean-Baptiste Boric 		if (eof)
54339f988b79SJean-Baptiste Boric 			break;
54349f988b79SJean-Baptiste Boric 		slot++;
54359f988b79SJean-Baptiste Boric 
54369f988b79SJean-Baptiste Boric 		if (UDF_EXT_FLAGS(udf_rw32(icb_loc.len)) != UDF_EXT_REDIRECT)
54379f988b79SJean-Baptiste Boric 			continue;
54389f988b79SJean-Baptiste Boric 
54399f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("\tgot redirect extent\n"));
54409f988b79SJean-Baptiste Boric 		if (udf_node->num_extensions >= UDF_MAX_ALLOC_EXTENTS) {
54419f988b79SJean-Baptiste Boric 			DPRINTF(ALLOC, ("udf_get_node: implementation limit, "
54429f988b79SJean-Baptiste Boric 					"too many allocation extensions on "
54439f988b79SJean-Baptiste Boric 					"udf_node\n"));
54449f988b79SJean-Baptiste Boric 			error = EINVAL;
54459f988b79SJean-Baptiste Boric 			break;
54469f988b79SJean-Baptiste Boric 		}
54479f988b79SJean-Baptiste Boric 
54489f988b79SJean-Baptiste Boric 		/* length can only be *one* lb : UDF 2.50/2.3.7.1 */
54499f988b79SJean-Baptiste Boric 		if (UDF_EXT_LEN(udf_rw32(icb_loc.len)) != lb_size) {
54509f988b79SJean-Baptiste Boric 			DPRINTF(ALLOC, ("udf_get_node: bad allocation "
54519f988b79SJean-Baptiste Boric 					"extension size in udf_node\n"));
54529f988b79SJean-Baptiste Boric 			error = EINVAL;
54539f988b79SJean-Baptiste Boric 			break;
54549f988b79SJean-Baptiste Boric 		}
54559f988b79SJean-Baptiste Boric 
54569f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("read allocation extent at lb_num %d\n",
54579f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(icb_loc.loc.lb_num))));
54589f988b79SJean-Baptiste Boric 		/* load in allocation extent */
54599f988b79SJean-Baptiste Boric 		error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
54609f988b79SJean-Baptiste Boric 		if (error || (dscr == NULL))
54619f988b79SJean-Baptiste Boric 			break;
54629f988b79SJean-Baptiste Boric 
54639f988b79SJean-Baptiste Boric 		/* process read-in descriptor */
54649f988b79SJean-Baptiste Boric 		dscr_type = udf_rw16(dscr->tag.id);
54659f988b79SJean-Baptiste Boric 
54669f988b79SJean-Baptiste Boric 		if (dscr_type != TAGID_ALLOCEXTENT) {
54679f988b79SJean-Baptiste Boric 			udf_free_logvol_dscr(ump, &icb_loc, dscr);
54689f988b79SJean-Baptiste Boric 			error = ENOENT;
54699f988b79SJean-Baptiste Boric 			break;
54709f988b79SJean-Baptiste Boric 		}
54719f988b79SJean-Baptiste Boric 
54729f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("\trecording redirect extent\n"));
54739f988b79SJean-Baptiste Boric 		udf_node->ext[udf_node->num_extensions] = &dscr->aee;
54749f988b79SJean-Baptiste Boric 		udf_node->ext_loc[udf_node->num_extensions] = icb_loc;
54759f988b79SJean-Baptiste Boric 
54769f988b79SJean-Baptiste Boric 		udf_node->num_extensions++;
54779f988b79SJean-Baptiste Boric 
54789f988b79SJean-Baptiste Boric 	} /* while */
54799f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
54809f988b79SJean-Baptiste Boric 
54819f988b79SJean-Baptiste Boric 	/* second round of cleanup code */
54829f988b79SJean-Baptiste Boric 	if (error) {
54839f988b79SJean-Baptiste Boric 		/* recycle udf_node */
54849f988b79SJean-Baptiste Boric 		udf_dispose_node(udf_node);
54859f988b79SJean-Baptiste Boric 
54869f988b79SJean-Baptiste Boric 		return EINVAL;		/* error code ok? */
54879f988b79SJean-Baptiste Boric 	}
54889f988b79SJean-Baptiste Boric 
54899f988b79SJean-Baptiste Boric 	DPRINTF(NODE, ("\tnode read in fine\n"));
54909f988b79SJean-Baptiste Boric 
54919f988b79SJean-Baptiste Boric 	/*
54929f988b79SJean-Baptiste Boric 	 * Translate UDF filetypes into vnode types.
54939f988b79SJean-Baptiste Boric 	 *
54949f988b79SJean-Baptiste Boric 	 * Systemfiles like the meta main and mirror files are not treated as
54959f988b79SJean-Baptiste Boric 	 * normal files, so we type them as having no type. UDF dictates that
54969f988b79SJean-Baptiste Boric 	 * they are not allowed to be visible.
54979f988b79SJean-Baptiste Boric 	 */
54989f988b79SJean-Baptiste Boric 
54999f988b79SJean-Baptiste Boric 	switch (udf_file_type) {
55009f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_DIRECTORY :
55019f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_STREAMDIR :
5502*0a6a1f1dSLionel Sambuc 		vp->v_type = VDIR;
55039f988b79SJean-Baptiste Boric 		break;
55049f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_BLOCKDEVICE :
5505*0a6a1f1dSLionel Sambuc 		vp->v_type = VBLK;
55069f988b79SJean-Baptiste Boric 		break;
55079f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_CHARDEVICE :
5508*0a6a1f1dSLionel Sambuc 		vp->v_type = VCHR;
55099f988b79SJean-Baptiste Boric 		break;
55109f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_SOCKET :
5511*0a6a1f1dSLionel Sambuc 		vp->v_type = VSOCK;
55129f988b79SJean-Baptiste Boric 		break;
55139f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_FIFO :
5514*0a6a1f1dSLionel Sambuc 		vp->v_type = VFIFO;
55159f988b79SJean-Baptiste Boric 		break;
55169f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_SYMLINK :
5517*0a6a1f1dSLionel Sambuc 		vp->v_type = VLNK;
55189f988b79SJean-Baptiste Boric 		break;
55199f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_VAT :
55209f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_META_MAIN :
55219f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_META_MIRROR :
5522*0a6a1f1dSLionel Sambuc 		vp->v_type = VNON;
55239f988b79SJean-Baptiste Boric 		break;
55249f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_RANDOMACCESS :
55259f988b79SJean-Baptiste Boric 	case UDF_ICB_FILETYPE_REALTIME :
5526*0a6a1f1dSLionel Sambuc 		vp->v_type = VREG;
55279f988b79SJean-Baptiste Boric 		break;
55289f988b79SJean-Baptiste Boric 	default:
55299f988b79SJean-Baptiste Boric 		/* YIKES, something else */
5530*0a6a1f1dSLionel Sambuc 		vp->v_type = VNON;
55319f988b79SJean-Baptiste Boric 	}
55329f988b79SJean-Baptiste Boric 
55339f988b79SJean-Baptiste Boric 	/* TODO specfs, fifofs etc etc. vnops setting */
55349f988b79SJean-Baptiste Boric 
55359f988b79SJean-Baptiste Boric 	/* don't forget to set vnode's v_size */
5536*0a6a1f1dSLionel Sambuc 	uvm_vnp_setsize(vp, file_size);
55379f988b79SJean-Baptiste Boric 
55389f988b79SJean-Baptiste Boric 	/* TODO ext attr and streamdir udf_nodes */
55399f988b79SJean-Baptiste Boric 
5540*0a6a1f1dSLionel Sambuc 	*new_key = &udf_node->loc.loc;
55419f988b79SJean-Baptiste Boric 
55429f988b79SJean-Baptiste Boric 	return 0;
55439f988b79SJean-Baptiste Boric }
55449f988b79SJean-Baptiste Boric 
5545*0a6a1f1dSLionel Sambuc int
udf_get_node(struct udf_mount * ump,struct long_ad * node_icb_loc,struct udf_node ** udf_noderes)5546*0a6a1f1dSLionel Sambuc udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
5547*0a6a1f1dSLionel Sambuc 	     struct udf_node **udf_noderes)
5548*0a6a1f1dSLionel Sambuc {
5549*0a6a1f1dSLionel Sambuc 	int error;
5550*0a6a1f1dSLionel Sambuc 	struct vnode *vp;
5551*0a6a1f1dSLionel Sambuc 
5552*0a6a1f1dSLionel Sambuc 	error = vcache_get(ump->vfs_mountp, &node_icb_loc->loc,
5553*0a6a1f1dSLionel Sambuc 	    sizeof(node_icb_loc->loc), &vp);
5554*0a6a1f1dSLionel Sambuc 	if (error)
5555*0a6a1f1dSLionel Sambuc 		return error;
5556*0a6a1f1dSLionel Sambuc 	error = vn_lock(vp, LK_EXCLUSIVE);
5557*0a6a1f1dSLionel Sambuc 	if (error) {
5558*0a6a1f1dSLionel Sambuc 		vrele(vp);
5559*0a6a1f1dSLionel Sambuc 		return error;
5560*0a6a1f1dSLionel Sambuc 	}
5561*0a6a1f1dSLionel Sambuc 	*udf_noderes = VTOI(vp);
5562*0a6a1f1dSLionel Sambuc 	return 0;
5563*0a6a1f1dSLionel Sambuc }
5564*0a6a1f1dSLionel Sambuc 
55659f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
55669f988b79SJean-Baptiste Boric 
55679f988b79SJean-Baptiste Boric int
udf_writeout_node(struct udf_node * udf_node,int waitfor)55689f988b79SJean-Baptiste Boric udf_writeout_node(struct udf_node *udf_node, int waitfor)
55699f988b79SJean-Baptiste Boric {
55709f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
55719f988b79SJean-Baptiste Boric 	struct long_ad *loc;
55729f988b79SJean-Baptiste Boric 	int extnr, error;
55739f988b79SJean-Baptiste Boric 
55749f988b79SJean-Baptiste Boric 	DPRINTF(NODE, ("udf_writeout_node called\n"));
55759f988b79SJean-Baptiste Boric 
55769f988b79SJean-Baptiste Boric 	KASSERT(udf_node->outstanding_bufs == 0);
55779f988b79SJean-Baptiste Boric 	KASSERT(udf_node->outstanding_nodedscr == 0);
55789f988b79SJean-Baptiste Boric 
55799f988b79SJean-Baptiste Boric 	KASSERT(LIST_EMPTY(&udf_node->vnode->v_dirtyblkhd));
55809f988b79SJean-Baptiste Boric 
55819f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & IN_DELETED) {
55829f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("\tnode deleted; not writing out\n"));
55839f988b79SJean-Baptiste Boric 		udf_cleanup_reservation(udf_node);
55849f988b79SJean-Baptiste Boric 		return 0;
55859f988b79SJean-Baptiste Boric 	}
55869f988b79SJean-Baptiste Boric 
55879f988b79SJean-Baptiste Boric 	/* lock node; unlocked in callback */
55889f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
55899f988b79SJean-Baptiste Boric 
55909f988b79SJean-Baptiste Boric 	/* remove pending reservations, we're written out */
55919f988b79SJean-Baptiste Boric 	udf_cleanup_reservation(udf_node);
55929f988b79SJean-Baptiste Boric 
55939f988b79SJean-Baptiste Boric 	/* at least one descriptor writeout */
55949f988b79SJean-Baptiste Boric 	udf_node->outstanding_nodedscr = 1;
55959f988b79SJean-Baptiste Boric 
55969f988b79SJean-Baptiste Boric 	/* we're going to write out the descriptor so clear the flags */
55979f988b79SJean-Baptiste Boric 	udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED);
55989f988b79SJean-Baptiste Boric 
55999f988b79SJean-Baptiste Boric 	/* if we were rebuild, write out the allocation extents */
56009f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & IN_NODE_REBUILD) {
56019f988b79SJean-Baptiste Boric 		/* mark outstanding node descriptors and issue them */
56029f988b79SJean-Baptiste Boric 		udf_node->outstanding_nodedscr += udf_node->num_extensions;
56039f988b79SJean-Baptiste Boric 		for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
56049f988b79SJean-Baptiste Boric 			loc = &udf_node->ext_loc[extnr];
56059f988b79SJean-Baptiste Boric 			dscr = (union dscrptr *) udf_node->ext[extnr];
56069f988b79SJean-Baptiste Boric 			error = udf_write_logvol_dscr(udf_node, dscr, loc, 0);
56079f988b79SJean-Baptiste Boric 			if (error)
56089f988b79SJean-Baptiste Boric 				return error;
56099f988b79SJean-Baptiste Boric 		}
56109f988b79SJean-Baptiste Boric 		/* mark allocation extents written out */
56119f988b79SJean-Baptiste Boric 		udf_node->i_flags &= ~(IN_NODE_REBUILD);
56129f988b79SJean-Baptiste Boric 	}
56139f988b79SJean-Baptiste Boric 
56149f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
56159f988b79SJean-Baptiste Boric 		KASSERT(udf_node->efe == NULL);
56169f988b79SJean-Baptiste Boric 		dscr = (union dscrptr *) udf_node->fe;
56179f988b79SJean-Baptiste Boric 	} else {
56189f988b79SJean-Baptiste Boric 		KASSERT(udf_node->efe);
56199f988b79SJean-Baptiste Boric 		KASSERT(udf_node->fe == NULL);
56209f988b79SJean-Baptiste Boric 		dscr = (union dscrptr *) udf_node->efe;
56219f988b79SJean-Baptiste Boric 	}
56229f988b79SJean-Baptiste Boric 	KASSERT(dscr);
56239f988b79SJean-Baptiste Boric 
56249f988b79SJean-Baptiste Boric 	loc = &udf_node->write_loc;
56259f988b79SJean-Baptiste Boric 	error = udf_write_logvol_dscr(udf_node, dscr, loc, waitfor);
56269f988b79SJean-Baptiste Boric 
56279f988b79SJean-Baptiste Boric 	return error;
56289f988b79SJean-Baptiste Boric }
56299f988b79SJean-Baptiste Boric 
56309f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
56319f988b79SJean-Baptiste Boric 
56329f988b79SJean-Baptiste Boric int
udf_dispose_node(struct udf_node * udf_node)56339f988b79SJean-Baptiste Boric udf_dispose_node(struct udf_node *udf_node)
56349f988b79SJean-Baptiste Boric {
56359f988b79SJean-Baptiste Boric 	struct vnode *vp;
56369f988b79SJean-Baptiste Boric 	int extnr;
56379f988b79SJean-Baptiste Boric 
56389f988b79SJean-Baptiste Boric 	DPRINTF(NODE, ("udf_dispose_node called on node %p\n", udf_node));
56399f988b79SJean-Baptiste Boric 	if (!udf_node) {
56409f988b79SJean-Baptiste Boric 		DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
56419f988b79SJean-Baptiste Boric 		return 0;
56429f988b79SJean-Baptiste Boric 	}
56439f988b79SJean-Baptiste Boric 
56449f988b79SJean-Baptiste Boric 	vp  = udf_node->vnode;
56459f988b79SJean-Baptiste Boric #ifdef DIAGNOSTIC
56469f988b79SJean-Baptiste Boric 	if (vp->v_numoutput)
56479f988b79SJean-Baptiste Boric 		panic("disposing UDF node with pending I/O's, udf_node = %p, "
56489f988b79SJean-Baptiste Boric 				"v_numoutput = %d", udf_node, vp->v_numoutput);
56499f988b79SJean-Baptiste Boric #endif
56509f988b79SJean-Baptiste Boric 
56519f988b79SJean-Baptiste Boric 	udf_cleanup_reservation(udf_node);
56529f988b79SJean-Baptiste Boric 
56539f988b79SJean-Baptiste Boric 	/* TODO extended attributes and streamdir */
56549f988b79SJean-Baptiste Boric 
56559f988b79SJean-Baptiste Boric 	/* remove dirhash if present */
56569f988b79SJean-Baptiste Boric 	dirhash_purge(&udf_node->dir_hash);
56579f988b79SJean-Baptiste Boric 
56589f988b79SJean-Baptiste Boric 	/* destroy our lock */
56599f988b79SJean-Baptiste Boric 	mutex_destroy(&udf_node->node_mutex);
56609f988b79SJean-Baptiste Boric 	cv_destroy(&udf_node->node_lock);
56619f988b79SJean-Baptiste Boric 
56629f988b79SJean-Baptiste Boric 	/* dissociate our udf_node from the vnode */
56639f988b79SJean-Baptiste Boric 	genfs_node_destroy(udf_node->vnode);
5664*0a6a1f1dSLionel Sambuc 	mutex_enter(vp->v_interlock);
56659f988b79SJean-Baptiste Boric 	vp->v_data = NULL;
5666*0a6a1f1dSLionel Sambuc 	mutex_exit(vp->v_interlock);
56679f988b79SJean-Baptiste Boric 
56689f988b79SJean-Baptiste Boric 	/* free associated memory and the node itself */
56699f988b79SJean-Baptiste Boric 	for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
56709f988b79SJean-Baptiste Boric 		udf_free_logvol_dscr(udf_node->ump, &udf_node->ext_loc[extnr],
56719f988b79SJean-Baptiste Boric 			udf_node->ext[extnr]);
56729f988b79SJean-Baptiste Boric 		udf_node->ext[extnr] = (void *) 0xdeadcccc;
56739f988b79SJean-Baptiste Boric 	}
56749f988b79SJean-Baptiste Boric 
56759f988b79SJean-Baptiste Boric 	if (udf_node->fe)
56769f988b79SJean-Baptiste Boric 		udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
56779f988b79SJean-Baptiste Boric 			udf_node->fe);
56789f988b79SJean-Baptiste Boric 	if (udf_node->efe)
56799f988b79SJean-Baptiste Boric 		udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
56809f988b79SJean-Baptiste Boric 			udf_node->efe);
56819f988b79SJean-Baptiste Boric 
56829f988b79SJean-Baptiste Boric 	udf_node->fe  = (void *) 0xdeadaaaa;
56839f988b79SJean-Baptiste Boric 	udf_node->efe = (void *) 0xdeadbbbb;
56849f988b79SJean-Baptiste Boric 	udf_node->ump = (void *) 0xdeadbeef;
56859f988b79SJean-Baptiste Boric 	pool_put(&udf_node_pool, udf_node);
56869f988b79SJean-Baptiste Boric 
56879f988b79SJean-Baptiste Boric 	return 0;
56889f988b79SJean-Baptiste Boric }
56899f988b79SJean-Baptiste Boric 
56909f988b79SJean-Baptiste Boric 
56919f988b79SJean-Baptiste Boric 
56929f988b79SJean-Baptiste Boric /*
5693*0a6a1f1dSLionel Sambuc  * create a new node using the specified dvp, vap and cnp.
5694*0a6a1f1dSLionel Sambuc  * This allows special files to be created. Use with care.
56959f988b79SJean-Baptiste Boric  */
56969f988b79SJean-Baptiste Boric 
5697*0a6a1f1dSLionel Sambuc int
udf_newvnode(struct mount * mp,struct vnode * dvp,struct vnode * vp,struct vattr * vap,kauth_cred_t cred,size_t * key_len,const void ** new_key)5698*0a6a1f1dSLionel Sambuc udf_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
5699*0a6a1f1dSLionel Sambuc     struct vattr *vap, kauth_cred_t cred,
5700*0a6a1f1dSLionel Sambuc     size_t *key_len, const void **new_key)
57019f988b79SJean-Baptiste Boric {
57029f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
57039f988b79SJean-Baptiste Boric 	struct udf_node *dir_node = VTOI(dvp);
57049f988b79SJean-Baptiste Boric 	struct udf_node *udf_node;
57059f988b79SJean-Baptiste Boric 	struct udf_mount *ump = dir_node->ump;
57069f988b79SJean-Baptiste Boric 	struct long_ad node_icb_loc;
57079f988b79SJean-Baptiste Boric 	uint64_t parent_unique_id;
57089f988b79SJean-Baptiste Boric 	uint64_t lmapping;
57099f988b79SJean-Baptiste Boric 	uint32_t lb_size, lb_num;
57109f988b79SJean-Baptiste Boric 	uint16_t vpart_num;
57119f988b79SJean-Baptiste Boric 	uid_t uid;
57129f988b79SJean-Baptiste Boric 	gid_t gid, parent_gid;
5713*0a6a1f1dSLionel Sambuc 	int (**vnodeops)(void *);
5714*0a6a1f1dSLionel Sambuc 	int udf_file_type, fid_size, error;
5715*0a6a1f1dSLionel Sambuc 
5716*0a6a1f1dSLionel Sambuc 	vnodeops = udf_vnodeop_p;
5717*0a6a1f1dSLionel Sambuc 	udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5718*0a6a1f1dSLionel Sambuc 
5719*0a6a1f1dSLionel Sambuc 	switch (vap->va_type) {
5720*0a6a1f1dSLionel Sambuc 	case VREG :
5721*0a6a1f1dSLionel Sambuc 		udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5722*0a6a1f1dSLionel Sambuc 		break;
5723*0a6a1f1dSLionel Sambuc 	case VDIR :
5724*0a6a1f1dSLionel Sambuc 		udf_file_type = UDF_ICB_FILETYPE_DIRECTORY;
5725*0a6a1f1dSLionel Sambuc 		break;
5726*0a6a1f1dSLionel Sambuc 	case VLNK :
5727*0a6a1f1dSLionel Sambuc 		udf_file_type = UDF_ICB_FILETYPE_SYMLINK;
5728*0a6a1f1dSLionel Sambuc 		break;
5729*0a6a1f1dSLionel Sambuc 	case VBLK :
5730*0a6a1f1dSLionel Sambuc 		udf_file_type = UDF_ICB_FILETYPE_BLOCKDEVICE;
5731*0a6a1f1dSLionel Sambuc 		/* specfs */
5732*0a6a1f1dSLionel Sambuc 		return ENOTSUP;
5733*0a6a1f1dSLionel Sambuc 		break;
5734*0a6a1f1dSLionel Sambuc 	case VCHR :
5735*0a6a1f1dSLionel Sambuc 		udf_file_type = UDF_ICB_FILETYPE_CHARDEVICE;
5736*0a6a1f1dSLionel Sambuc 		/* specfs */
5737*0a6a1f1dSLionel Sambuc 		return ENOTSUP;
5738*0a6a1f1dSLionel Sambuc 		break;
5739*0a6a1f1dSLionel Sambuc 	case VFIFO :
5740*0a6a1f1dSLionel Sambuc 		udf_file_type = UDF_ICB_FILETYPE_FIFO;
5741*0a6a1f1dSLionel Sambuc 		/* fifofs */
5742*0a6a1f1dSLionel Sambuc 		return ENOTSUP;
5743*0a6a1f1dSLionel Sambuc 		break;
5744*0a6a1f1dSLionel Sambuc 	case VSOCK :
5745*0a6a1f1dSLionel Sambuc 		udf_file_type = UDF_ICB_FILETYPE_SOCKET;
5746*0a6a1f1dSLionel Sambuc 		return ENOTSUP;
5747*0a6a1f1dSLionel Sambuc 		break;
5748*0a6a1f1dSLionel Sambuc 	case VNON :
5749*0a6a1f1dSLionel Sambuc 	case VBAD :
5750*0a6a1f1dSLionel Sambuc 	default :
5751*0a6a1f1dSLionel Sambuc 		/* nothing; can we even create these? */
5752*0a6a1f1dSLionel Sambuc 		return EINVAL;
5753*0a6a1f1dSLionel Sambuc 	}
57549f988b79SJean-Baptiste Boric 
57559f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
57569f988b79SJean-Baptiste Boric 
57579f988b79SJean-Baptiste Boric 	/* reserve space for one logical block */
57589f988b79SJean-Baptiste Boric 	vpart_num = ump->node_part;
57599f988b79SJean-Baptiste Boric 	error = udf_reserve_space(ump, NULL, UDF_C_NODE,
57609f988b79SJean-Baptiste Boric 		vpart_num, 1, /* can_fail */ true);
57619f988b79SJean-Baptiste Boric 	if (error)
5762*0a6a1f1dSLionel Sambuc 		return error;
57639f988b79SJean-Baptiste Boric 
57649f988b79SJean-Baptiste Boric 	/* allocate node */
57659f988b79SJean-Baptiste Boric 	error = udf_allocate_space(ump, NULL, UDF_C_NODE,
57669f988b79SJean-Baptiste Boric 			vpart_num, 1, &lmapping);
5767*0a6a1f1dSLionel Sambuc 	if (error) {
5768*0a6a1f1dSLionel Sambuc 		udf_do_unreserve_space(ump, NULL, vpart_num, 1);
5769*0a6a1f1dSLionel Sambuc 		return error;
5770*0a6a1f1dSLionel Sambuc 	}
5771*0a6a1f1dSLionel Sambuc 
57729f988b79SJean-Baptiste Boric 	lb_num = lmapping;
57739f988b79SJean-Baptiste Boric 
57749f988b79SJean-Baptiste Boric 	/* initialise pointer to location */
57759f988b79SJean-Baptiste Boric 	memset(&node_icb_loc, 0, sizeof(struct long_ad));
57769f988b79SJean-Baptiste Boric 	node_icb_loc.len = udf_rw32(lb_size);
57779f988b79SJean-Baptiste Boric 	node_icb_loc.loc.lb_num   = udf_rw32(lb_num);
57789f988b79SJean-Baptiste Boric 	node_icb_loc.loc.part_num = udf_rw16(vpart_num);
57799f988b79SJean-Baptiste Boric 
57809f988b79SJean-Baptiste Boric 	/* build udf_node (do initialise!) */
57819f988b79SJean-Baptiste Boric 	udf_node = pool_get(&udf_node_pool, PR_WAITOK);
57829f988b79SJean-Baptiste Boric 	memset(udf_node, 0, sizeof(struct udf_node));
57839f988b79SJean-Baptiste Boric 
57849f988b79SJean-Baptiste Boric 	/* initialise crosslinks, note location of fe/efe for hashing */
57859f988b79SJean-Baptiste Boric 	/* bugalert: synchronise with udf_get_node() */
57869f988b79SJean-Baptiste Boric 	udf_node->ump       = ump;
5787*0a6a1f1dSLionel Sambuc 	udf_node->vnode     = vp;
5788*0a6a1f1dSLionel Sambuc 	vp->v_data          = udf_node;
57899f988b79SJean-Baptiste Boric 	udf_node->loc       = node_icb_loc;
57909f988b79SJean-Baptiste Boric 	udf_node->write_loc = node_icb_loc;
57919f988b79SJean-Baptiste Boric 	udf_node->lockf     = 0;
57929f988b79SJean-Baptiste Boric 	mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
57939f988b79SJean-Baptiste Boric 	cv_init(&udf_node->node_lock, "udf_nlk");
57949f988b79SJean-Baptiste Boric 	udf_node->outstanding_bufs = 0;
57959f988b79SJean-Baptiste Boric 	udf_node->outstanding_nodedscr = 0;
57969f988b79SJean-Baptiste Boric 	udf_node->uncommitted_lbs = 0;
57979f988b79SJean-Baptiste Boric 
5798*0a6a1f1dSLionel Sambuc 	vp->v_tag = VT_UDF;
5799*0a6a1f1dSLionel Sambuc 	vp->v_op = vnodeops;
58009f988b79SJean-Baptiste Boric 
5801*0a6a1f1dSLionel Sambuc 	/* initialise genfs */
5802*0a6a1f1dSLionel Sambuc 	genfs_node_init(vp, &udf_genfsops);
58039f988b79SJean-Baptiste Boric 
58049f988b79SJean-Baptiste Boric 	/* get parent's unique ID for refering '..' if its a directory */
58059f988b79SJean-Baptiste Boric 	if (dir_node->fe) {
58069f988b79SJean-Baptiste Boric 		parent_unique_id = udf_rw64(dir_node->fe->unique_id);
58079f988b79SJean-Baptiste Boric 		parent_gid       = (gid_t) udf_rw32(dir_node->fe->gid);
58089f988b79SJean-Baptiste Boric 	} else {
58099f988b79SJean-Baptiste Boric 		parent_unique_id = udf_rw64(dir_node->efe->unique_id);
58109f988b79SJean-Baptiste Boric 		parent_gid       = (gid_t) udf_rw32(dir_node->efe->gid);
58119f988b79SJean-Baptiste Boric 	}
58129f988b79SJean-Baptiste Boric 
58139f988b79SJean-Baptiste Boric 	/* get descriptor */
58149f988b79SJean-Baptiste Boric 	udf_create_logvol_dscr(ump, udf_node, &node_icb_loc, &dscr);
58159f988b79SJean-Baptiste Boric 
58169f988b79SJean-Baptiste Boric 	/* choose a fe or an efe for it */
58179f988b79SJean-Baptiste Boric 	if (udf_rw16(ump->logical_vol->tag.descriptor_ver) == 2) {
58189f988b79SJean-Baptiste Boric 		udf_node->fe = &dscr->fe;
58199f988b79SJean-Baptiste Boric 		fid_size = udf_create_new_fe(ump, udf_node->fe,
58209f988b79SJean-Baptiste Boric 			udf_file_type, &udf_node->loc,
58219f988b79SJean-Baptiste Boric 			&dir_node->loc, parent_unique_id);
58229f988b79SJean-Baptiste Boric 		/* TODO add extended attribute for creation time */
58239f988b79SJean-Baptiste Boric 	} else {
58249f988b79SJean-Baptiste Boric 		udf_node->efe = &dscr->efe;
58259f988b79SJean-Baptiste Boric 		fid_size = udf_create_new_efe(ump, udf_node->efe,
58269f988b79SJean-Baptiste Boric 			udf_file_type, &udf_node->loc,
58279f988b79SJean-Baptiste Boric 			&dir_node->loc, parent_unique_id);
58289f988b79SJean-Baptiste Boric 	}
58299f988b79SJean-Baptiste Boric 	KASSERT(dscr->tag.tag_loc == udf_node->loc.loc.lb_num);
58309f988b79SJean-Baptiste Boric 
58319f988b79SJean-Baptiste Boric 	/* update vnode's size and type */
5832*0a6a1f1dSLionel Sambuc 	vp->v_type = vap->va_type;
5833*0a6a1f1dSLionel Sambuc 	uvm_vnp_setsize(vp, fid_size);
58349f988b79SJean-Baptiste Boric 
58359f988b79SJean-Baptiste Boric 	/* set access mode */
58369f988b79SJean-Baptiste Boric 	udf_setaccessmode(udf_node, vap->va_mode);
58379f988b79SJean-Baptiste Boric 
58389f988b79SJean-Baptiste Boric 	/* set ownership */
5839*0a6a1f1dSLionel Sambuc 	uid = kauth_cred_geteuid(cred);
58409f988b79SJean-Baptiste Boric 	gid = parent_gid;
58419f988b79SJean-Baptiste Boric 	udf_setownership(udf_node, uid, gid);
58429f988b79SJean-Baptiste Boric 
5843*0a6a1f1dSLionel Sambuc 	*key_len = sizeof(udf_node->loc.loc);;
5844*0a6a1f1dSLionel Sambuc 	*new_key = &udf_node->loc.loc;
5845*0a6a1f1dSLionel Sambuc 
5846*0a6a1f1dSLionel Sambuc 	return 0;
5847*0a6a1f1dSLionel Sambuc }
5848*0a6a1f1dSLionel Sambuc 
5849*0a6a1f1dSLionel Sambuc 
5850*0a6a1f1dSLionel Sambuc int
udf_create_node(struct vnode * dvp,struct vnode ** vpp,struct vattr * vap,struct componentname * cnp)5851*0a6a1f1dSLionel Sambuc udf_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
5852*0a6a1f1dSLionel Sambuc 	struct componentname *cnp)
5853*0a6a1f1dSLionel Sambuc {
5854*0a6a1f1dSLionel Sambuc 	struct udf_node *udf_node, *dir_node = VTOI(dvp);
5855*0a6a1f1dSLionel Sambuc 	struct udf_mount *ump = dir_node->ump;
5856*0a6a1f1dSLionel Sambuc 	int error;
5857*0a6a1f1dSLionel Sambuc 
5858*0a6a1f1dSLionel Sambuc 	error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, vpp);
5859*0a6a1f1dSLionel Sambuc 	if (error)
5860*0a6a1f1dSLionel Sambuc 		return error;
5861*0a6a1f1dSLionel Sambuc 
5862*0a6a1f1dSLionel Sambuc 	udf_node = VTOI(*vpp);
58639f988b79SJean-Baptiste Boric 	error = udf_dir_attach(ump, dir_node, udf_node, vap, cnp);
58649f988b79SJean-Baptiste Boric 	if (error) {
5865*0a6a1f1dSLionel Sambuc 		struct long_ad *node_icb_loc = &udf_node->loc;
5866*0a6a1f1dSLionel Sambuc 		uint32_t lb_num = udf_rw32(node_icb_loc->loc.lb_num);
5867*0a6a1f1dSLionel Sambuc 		uint16_t vpart_num = udf_rw16(node_icb_loc->loc.part_num);
5868*0a6a1f1dSLionel Sambuc 
58699f988b79SJean-Baptiste Boric 		/* free disc allocation for node */
58709f988b79SJean-Baptiste Boric 		udf_free_allocated_space(ump, lb_num, vpart_num, 1);
58719f988b79SJean-Baptiste Boric 
58729f988b79SJean-Baptiste Boric 		/* recycle udf_node */
58739f988b79SJean-Baptiste Boric 		udf_dispose_node(udf_node);
5874*0a6a1f1dSLionel Sambuc 		vrele(*vpp);
58759f988b79SJean-Baptiste Boric 
58769f988b79SJean-Baptiste Boric 		*vpp = NULL;
58779f988b79SJean-Baptiste Boric 		return error;
58789f988b79SJean-Baptiste Boric 	}
58799f988b79SJean-Baptiste Boric 
58809f988b79SJean-Baptiste Boric 	/* adjust file count */
58819f988b79SJean-Baptiste Boric 	udf_adjust_filecount(udf_node, 1);
58829f988b79SJean-Baptiste Boric 
58839f988b79SJean-Baptiste Boric 	return 0;
58849f988b79SJean-Baptiste Boric }
58859f988b79SJean-Baptiste Boric 
58869f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
58879f988b79SJean-Baptiste Boric 
58889f988b79SJean-Baptiste Boric static void
udf_free_descriptor_space(struct udf_node * udf_node,struct long_ad * loc,void * mem)58899f988b79SJean-Baptiste Boric udf_free_descriptor_space(struct udf_node *udf_node, struct long_ad *loc, void *mem)
58909f988b79SJean-Baptiste Boric {
58919f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
58929f988b79SJean-Baptiste Boric 	uint32_t lb_size, lb_num, len, num_lb;
58939f988b79SJean-Baptiste Boric 	uint16_t vpart_num;
58949f988b79SJean-Baptiste Boric 
58959f988b79SJean-Baptiste Boric 	/* is there really one? */
58969f988b79SJean-Baptiste Boric 	if (mem == NULL)
58979f988b79SJean-Baptiste Boric 		return;
58989f988b79SJean-Baptiste Boric 
58999f988b79SJean-Baptiste Boric 	/* got a descriptor here */
59009f988b79SJean-Baptiste Boric 	len       = UDF_EXT_LEN(udf_rw32(loc->len));
59019f988b79SJean-Baptiste Boric 	lb_num    = udf_rw32(loc->loc.lb_num);
59029f988b79SJean-Baptiste Boric 	vpart_num = udf_rw16(loc->loc.part_num);
59039f988b79SJean-Baptiste Boric 
59049f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
59059f988b79SJean-Baptiste Boric 	num_lb = (len + lb_size -1) / lb_size;
59069f988b79SJean-Baptiste Boric 
59079f988b79SJean-Baptiste Boric 	udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
59089f988b79SJean-Baptiste Boric }
59099f988b79SJean-Baptiste Boric 
59109f988b79SJean-Baptiste Boric void
udf_delete_node(struct udf_node * udf_node)59119f988b79SJean-Baptiste Boric udf_delete_node(struct udf_node *udf_node)
59129f988b79SJean-Baptiste Boric {
59139f988b79SJean-Baptiste Boric 	void *dscr;
59149f988b79SJean-Baptiste Boric 	struct long_ad *loc;
59159f988b79SJean-Baptiste Boric 	int extnr, lvint, dummy;
59169f988b79SJean-Baptiste Boric 
59179f988b79SJean-Baptiste Boric 	/* paranoia check on integrity; should be open!; we could panic */
59189f988b79SJean-Baptiste Boric 	lvint = udf_rw32(udf_node->ump->logvol_integrity->integrity_type);
59199f988b79SJean-Baptiste Boric 	if (lvint == UDF_INTEGRITY_CLOSED)
59209f988b79SJean-Baptiste Boric 		printf("\tIntegrity was CLOSED!\n");
59219f988b79SJean-Baptiste Boric 
59229f988b79SJean-Baptiste Boric 	/* whatever the node type, change its size to zero */
59239f988b79SJean-Baptiste Boric 	(void) udf_resize_node(udf_node, 0, &dummy);
59249f988b79SJean-Baptiste Boric 
59259f988b79SJean-Baptiste Boric 	/* force it to be `clean'; no use writing it out */
59269f988b79SJean-Baptiste Boric 	udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED | IN_ACCESS |
59279f988b79SJean-Baptiste Boric 		IN_CHANGE | IN_UPDATE | IN_MODIFY);
59289f988b79SJean-Baptiste Boric 
59299f988b79SJean-Baptiste Boric 	/* adjust file count */
59309f988b79SJean-Baptiste Boric 	udf_adjust_filecount(udf_node, -1);
59319f988b79SJean-Baptiste Boric 
59329f988b79SJean-Baptiste Boric 	/*
59339f988b79SJean-Baptiste Boric 	 * Free its allocated descriptors; memory will be released when
59349f988b79SJean-Baptiste Boric 	 * vop_reclaim() is called.
59359f988b79SJean-Baptiste Boric 	 */
59369f988b79SJean-Baptiste Boric 	loc = &udf_node->loc;
59379f988b79SJean-Baptiste Boric 
59389f988b79SJean-Baptiste Boric 	dscr = udf_node->fe;
59399f988b79SJean-Baptiste Boric 	udf_free_descriptor_space(udf_node, loc, dscr);
59409f988b79SJean-Baptiste Boric 	dscr = udf_node->efe;
59419f988b79SJean-Baptiste Boric 	udf_free_descriptor_space(udf_node, loc, dscr);
59429f988b79SJean-Baptiste Boric 
59439f988b79SJean-Baptiste Boric 	for (extnr = 0; extnr < UDF_MAX_ALLOC_EXTENTS; extnr++) {
59449f988b79SJean-Baptiste Boric 		dscr =  udf_node->ext[extnr];
59459f988b79SJean-Baptiste Boric 		loc  = &udf_node->ext_loc[extnr];
59469f988b79SJean-Baptiste Boric 		udf_free_descriptor_space(udf_node, loc, dscr);
59479f988b79SJean-Baptiste Boric 	}
59489f988b79SJean-Baptiste Boric }
59499f988b79SJean-Baptiste Boric 
59509f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
59519f988b79SJean-Baptiste Boric 
59529f988b79SJean-Baptiste Boric /* set new filesize; node but be LOCKED on entry and is locked on exit */
59539f988b79SJean-Baptiste Boric int
udf_resize_node(struct udf_node * udf_node,uint64_t new_size,int * extended)59549f988b79SJean-Baptiste Boric udf_resize_node(struct udf_node *udf_node, uint64_t new_size, int *extended)
59559f988b79SJean-Baptiste Boric {
59569f988b79SJean-Baptiste Boric 	struct file_entry    *fe  = udf_node->fe;
59579f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = udf_node->efe;
59589f988b79SJean-Baptiste Boric 	uint64_t file_size;
59599f988b79SJean-Baptiste Boric 	int error;
59609f988b79SJean-Baptiste Boric 
59619f988b79SJean-Baptiste Boric 	if (fe) {
59629f988b79SJean-Baptiste Boric 		file_size  = udf_rw64(fe->inf_len);
59639f988b79SJean-Baptiste Boric 	} else {
59649f988b79SJean-Baptiste Boric 		assert(udf_node->efe);
59659f988b79SJean-Baptiste Boric 		file_size  = udf_rw64(efe->inf_len);
59669f988b79SJean-Baptiste Boric 	}
59679f988b79SJean-Baptiste Boric 
59689f988b79SJean-Baptiste Boric 	DPRINTF(ATTR, ("\tchanging file length from %"PRIu64" to %"PRIu64"\n",
59699f988b79SJean-Baptiste Boric 			file_size, new_size));
59709f988b79SJean-Baptiste Boric 
59719f988b79SJean-Baptiste Boric 	/* if not changing, we're done */
59729f988b79SJean-Baptiste Boric 	if (file_size == new_size)
59739f988b79SJean-Baptiste Boric 		return 0;
59749f988b79SJean-Baptiste Boric 
59759f988b79SJean-Baptiste Boric 	*extended = (new_size > file_size);
59769f988b79SJean-Baptiste Boric 	if (*extended) {
59779f988b79SJean-Baptiste Boric 		error = udf_grow_node(udf_node, new_size);
59789f988b79SJean-Baptiste Boric 	} else {
59799f988b79SJean-Baptiste Boric 		error = udf_shrink_node(udf_node, new_size);
59809f988b79SJean-Baptiste Boric 	}
59819f988b79SJean-Baptiste Boric 
59829f988b79SJean-Baptiste Boric 	return error;
59839f988b79SJean-Baptiste Boric }
59849f988b79SJean-Baptiste Boric 
59859f988b79SJean-Baptiste Boric 
59869f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
59879f988b79SJean-Baptiste Boric 
59889f988b79SJean-Baptiste Boric void
udf_itimes(struct udf_node * udf_node,struct timespec * acc,struct timespec * mod,struct timespec * birth)59899f988b79SJean-Baptiste Boric udf_itimes(struct udf_node *udf_node, struct timespec *acc,
59909f988b79SJean-Baptiste Boric 	struct timespec *mod, struct timespec *birth)
59919f988b79SJean-Baptiste Boric {
59929f988b79SJean-Baptiste Boric 	struct timespec now;
59939f988b79SJean-Baptiste Boric 	struct file_entry    *fe;
59949f988b79SJean-Baptiste Boric 	struct extfile_entry *efe;
59959f988b79SJean-Baptiste Boric 	struct filetimes_extattr_entry *ft_extattr;
59969f988b79SJean-Baptiste Boric 	struct timestamp *atime, *mtime, *attrtime, *ctime;
59979f988b79SJean-Baptiste Boric 	struct timestamp  fe_ctime;
59989f988b79SJean-Baptiste Boric 	struct timespec   cur_birth;
59999f988b79SJean-Baptiste Boric 	uint32_t offset, a_l;
60009f988b79SJean-Baptiste Boric 	uint8_t *filedata;
60019f988b79SJean-Baptiste Boric 	int error;
60029f988b79SJean-Baptiste Boric 
60039f988b79SJean-Baptiste Boric 	/* protect against rogue values */
60049f988b79SJean-Baptiste Boric 	if (!udf_node)
60059f988b79SJean-Baptiste Boric 		return;
60069f988b79SJean-Baptiste Boric 
60079f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
60089f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
60099f988b79SJean-Baptiste Boric 
60109f988b79SJean-Baptiste Boric 	if (!(udf_node->i_flags & (IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY)))
60119f988b79SJean-Baptiste Boric 		return;
60129f988b79SJean-Baptiste Boric 
60139f988b79SJean-Baptiste Boric 	/* get descriptor information */
60149f988b79SJean-Baptiste Boric 	if (fe) {
60159f988b79SJean-Baptiste Boric 		atime    = &fe->atime;
60169f988b79SJean-Baptiste Boric 		mtime    = &fe->mtime;
60179f988b79SJean-Baptiste Boric 		attrtime = &fe->attrtime;
60189f988b79SJean-Baptiste Boric 		filedata = fe->data;
60199f988b79SJean-Baptiste Boric 
60209f988b79SJean-Baptiste Boric 		/* initial save dummy setting */
60219f988b79SJean-Baptiste Boric 		ctime    = &fe_ctime;
60229f988b79SJean-Baptiste Boric 
60239f988b79SJean-Baptiste Boric 		/* check our extended attribute if present */
60249f988b79SJean-Baptiste Boric 		error = udf_extattr_search_intern(udf_node,
60259f988b79SJean-Baptiste Boric 			UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
60269f988b79SJean-Baptiste Boric 		if (!error) {
60279f988b79SJean-Baptiste Boric 			ft_extattr = (struct filetimes_extattr_entry *)
60289f988b79SJean-Baptiste Boric 				(filedata + offset);
60299f988b79SJean-Baptiste Boric 			if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
60309f988b79SJean-Baptiste Boric 				ctime = &ft_extattr->times[0];
60319f988b79SJean-Baptiste Boric 		}
60329f988b79SJean-Baptiste Boric 		/* TODO create the extended attribute if not found ? */
60339f988b79SJean-Baptiste Boric 	} else {
60349f988b79SJean-Baptiste Boric 		assert(udf_node->efe);
60359f988b79SJean-Baptiste Boric 		atime    = &efe->atime;
60369f988b79SJean-Baptiste Boric 		mtime    = &efe->mtime;
60379f988b79SJean-Baptiste Boric 		attrtime = &efe->attrtime;
60389f988b79SJean-Baptiste Boric 		ctime    = &efe->ctime;
60399f988b79SJean-Baptiste Boric 	}
60409f988b79SJean-Baptiste Boric 
60419f988b79SJean-Baptiste Boric 	vfs_timestamp(&now);
60429f988b79SJean-Baptiste Boric 
60439f988b79SJean-Baptiste Boric 	/* set access time */
60449f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & IN_ACCESS) {
60459f988b79SJean-Baptiste Boric 		if (acc == NULL)
60469f988b79SJean-Baptiste Boric 			acc = &now;
60479f988b79SJean-Baptiste Boric 		udf_timespec_to_timestamp(acc, atime);
60489f988b79SJean-Baptiste Boric 	}
60499f988b79SJean-Baptiste Boric 
60509f988b79SJean-Baptiste Boric 	/* set modification time */
60519f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & (IN_UPDATE | IN_MODIFY)) {
60529f988b79SJean-Baptiste Boric 		if (mod == NULL)
60539f988b79SJean-Baptiste Boric 			mod = &now;
60549f988b79SJean-Baptiste Boric 		udf_timespec_to_timestamp(mod, mtime);
60559f988b79SJean-Baptiste Boric 
60569f988b79SJean-Baptiste Boric 		/* ensure birthtime is older than set modification! */
60579f988b79SJean-Baptiste Boric 		udf_timestamp_to_timespec(udf_node->ump, ctime, &cur_birth);
60589f988b79SJean-Baptiste Boric 		if ((cur_birth.tv_sec > mod->tv_sec) ||
60599f988b79SJean-Baptiste Boric 			  ((cur_birth.tv_sec == mod->tv_sec) &&
60609f988b79SJean-Baptiste Boric 			     (cur_birth.tv_nsec > mod->tv_nsec))) {
60619f988b79SJean-Baptiste Boric 			udf_timespec_to_timestamp(mod, ctime);
60629f988b79SJean-Baptiste Boric 		}
60639f988b79SJean-Baptiste Boric 	}
60649f988b79SJean-Baptiste Boric 
60659f988b79SJean-Baptiste Boric 	/* update birthtime if specified */
60669f988b79SJean-Baptiste Boric 	/* XXX we assume here that given birthtime is older than mod */
60679f988b79SJean-Baptiste Boric 	if (birth && (birth->tv_sec != VNOVAL)) {
60689f988b79SJean-Baptiste Boric 		udf_timespec_to_timestamp(birth, ctime);
60699f988b79SJean-Baptiste Boric 	}
60709f988b79SJean-Baptiste Boric 
60719f988b79SJean-Baptiste Boric 	/* set change time */
60729f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & (IN_CHANGE | IN_MODIFY))
60739f988b79SJean-Baptiste Boric 		udf_timespec_to_timestamp(&now, attrtime);
60749f988b79SJean-Baptiste Boric 
60759f988b79SJean-Baptiste Boric 	/* notify updates to the node itself */
60769f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & (IN_ACCESS | IN_MODIFY))
60779f988b79SJean-Baptiste Boric 		udf_node->i_flags |= IN_ACCESSED;
60789f988b79SJean-Baptiste Boric 	if (udf_node->i_flags & (IN_UPDATE | IN_CHANGE))
60799f988b79SJean-Baptiste Boric 		udf_node->i_flags |= IN_MODIFIED;
60809f988b79SJean-Baptiste Boric 
60819f988b79SJean-Baptiste Boric 	/* clear modification flags */
60829f988b79SJean-Baptiste Boric 	udf_node->i_flags &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
60839f988b79SJean-Baptiste Boric }
60849f988b79SJean-Baptiste Boric 
60859f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
60869f988b79SJean-Baptiste Boric 
60879f988b79SJean-Baptiste Boric int
udf_update(struct vnode * vp,struct timespec * acc,struct timespec * mod,struct timespec * birth,int updflags)60889f988b79SJean-Baptiste Boric udf_update(struct vnode *vp, struct timespec *acc,
60899f988b79SJean-Baptiste Boric 	struct timespec *mod, struct timespec *birth, int updflags)
60909f988b79SJean-Baptiste Boric {
60919f988b79SJean-Baptiste Boric 	union dscrptr *dscrptr;
60929f988b79SJean-Baptiste Boric 	struct udf_node  *udf_node = VTOI(vp);
60939f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
60949f988b79SJean-Baptiste Boric 	struct regid     *impl_id;
60959f988b79SJean-Baptiste Boric 	int mnt_async = (vp->v_mount->mnt_flag & MNT_ASYNC);
60969f988b79SJean-Baptiste Boric 	int waitfor, flags;
60979f988b79SJean-Baptiste Boric 
60989f988b79SJean-Baptiste Boric #ifdef DEBUG
60999f988b79SJean-Baptiste Boric 	char bits[128];
61009f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("udf_update(node, %p, %p, %p, %d)\n", acc, mod, birth,
61019f988b79SJean-Baptiste Boric 		updflags));
61029f988b79SJean-Baptiste Boric 	snprintb(bits, sizeof(bits), IN_FLAGBITS, udf_node->i_flags);
61039f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("\tnode flags %s\n", bits));
61049f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("\t\tmnt_async = %d\n", mnt_async));
61059f988b79SJean-Baptiste Boric #endif
61069f988b79SJean-Baptiste Boric 
61079f988b79SJean-Baptiste Boric 	/* set our times */
61089f988b79SJean-Baptiste Boric 	udf_itimes(udf_node, acc, mod, birth);
61099f988b79SJean-Baptiste Boric 
61109f988b79SJean-Baptiste Boric 	/* set our implementation id */
61119f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
61129f988b79SJean-Baptiste Boric 		dscrptr = (union dscrptr *) udf_node->fe;
61139f988b79SJean-Baptiste Boric 		impl_id = &udf_node->fe->imp_id;
61149f988b79SJean-Baptiste Boric 	} else {
61159f988b79SJean-Baptiste Boric 		dscrptr = (union dscrptr *) udf_node->efe;
61169f988b79SJean-Baptiste Boric 		impl_id = &udf_node->efe->imp_id;
61179f988b79SJean-Baptiste Boric 	}
61189f988b79SJean-Baptiste Boric 
61199f988b79SJean-Baptiste Boric 	/* set our ID */
61209f988b79SJean-Baptiste Boric 	udf_set_regid(impl_id, IMPL_NAME);
61219f988b79SJean-Baptiste Boric 	udf_add_impl_regid(ump, impl_id);
61229f988b79SJean-Baptiste Boric 
61239f988b79SJean-Baptiste Boric 	/* update our crc! on RMW we are not allowed to change a thing */
61249f988b79SJean-Baptiste Boric 	udf_validate_tag_and_crc_sums(dscrptr);
61259f988b79SJean-Baptiste Boric 
61269f988b79SJean-Baptiste Boric 	/* if called when mounted readonly, never write back */
61279f988b79SJean-Baptiste Boric 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
61289f988b79SJean-Baptiste Boric 		return 0;
61299f988b79SJean-Baptiste Boric 
61309f988b79SJean-Baptiste Boric 	/* check if the node is dirty 'enough'*/
61319f988b79SJean-Baptiste Boric 	if (updflags & UPDATE_CLOSE) {
61329f988b79SJean-Baptiste Boric 		flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
61339f988b79SJean-Baptiste Boric 	} else {
61349f988b79SJean-Baptiste Boric 		flags = udf_node->i_flags & IN_MODIFIED;
61359f988b79SJean-Baptiste Boric 	}
61369f988b79SJean-Baptiste Boric 	if (flags == 0)
61379f988b79SJean-Baptiste Boric 		return 0;
61389f988b79SJean-Baptiste Boric 
61399f988b79SJean-Baptiste Boric 	/* determine if we need to write sync or async */
61409f988b79SJean-Baptiste Boric 	waitfor = 0;
61419f988b79SJean-Baptiste Boric 	if ((flags & IN_MODIFIED) && (mnt_async == 0)) {
61429f988b79SJean-Baptiste Boric 		/* sync mounted */
61439f988b79SJean-Baptiste Boric 		waitfor = updflags & UPDATE_WAIT;
61449f988b79SJean-Baptiste Boric 		if (updflags & UPDATE_DIROP)
61459f988b79SJean-Baptiste Boric 			waitfor |= UPDATE_WAIT;
61469f988b79SJean-Baptiste Boric 	}
61479f988b79SJean-Baptiste Boric 	if (waitfor)
61489f988b79SJean-Baptiste Boric 		return VOP_FSYNC(vp, FSCRED, FSYNC_WAIT, 0,0);
61499f988b79SJean-Baptiste Boric 
61509f988b79SJean-Baptiste Boric 	return 0;
61519f988b79SJean-Baptiste Boric }
61529f988b79SJean-Baptiste Boric 
61539f988b79SJean-Baptiste Boric 
61549f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
61559f988b79SJean-Baptiste Boric 
61569f988b79SJean-Baptiste Boric 
61579f988b79SJean-Baptiste Boric /*
61589f988b79SJean-Baptiste Boric  * Read one fid and process it into a dirent and advance to the next (*fid)
61599f988b79SJean-Baptiste Boric  * has to be allocated a logical block in size, (*dirent) struct dirent length
61609f988b79SJean-Baptiste Boric  */
61619f988b79SJean-Baptiste Boric 
61629f988b79SJean-Baptiste Boric int
udf_read_fid_stream(struct vnode * vp,uint64_t * offset,struct fileid_desc * fid,struct dirent * dirent)61639f988b79SJean-Baptiste Boric udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
61649f988b79SJean-Baptiste Boric 		struct fileid_desc *fid, struct dirent *dirent)
61659f988b79SJean-Baptiste Boric {
61669f988b79SJean-Baptiste Boric 	struct udf_node  *dir_node = VTOI(vp);
61679f988b79SJean-Baptiste Boric 	struct udf_mount *ump = dir_node->ump;
61689f988b79SJean-Baptiste Boric 	struct file_entry    *fe  = dir_node->fe;
61699f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = dir_node->efe;
61709f988b79SJean-Baptiste Boric 	uint32_t      fid_size, lb_size;
61719f988b79SJean-Baptiste Boric 	uint64_t      file_size;
61729f988b79SJean-Baptiste Boric 	char         *fid_name;
61739f988b79SJean-Baptiste Boric 	int           enough, error;
61749f988b79SJean-Baptiste Boric 
61759f988b79SJean-Baptiste Boric 	assert(fid);
61769f988b79SJean-Baptiste Boric 	assert(dirent);
61779f988b79SJean-Baptiste Boric 	assert(dir_node);
61789f988b79SJean-Baptiste Boric 	assert(offset);
61799f988b79SJean-Baptiste Boric 	assert(*offset != 1);
61809f988b79SJean-Baptiste Boric 
61819f988b79SJean-Baptiste Boric 	DPRINTF(FIDS, ("read_fid_stream called at offset %"PRIu64"\n", *offset));
61829f988b79SJean-Baptiste Boric 	/* check if we're past the end of the directory */
61839f988b79SJean-Baptiste Boric 	if (fe) {
61849f988b79SJean-Baptiste Boric 		file_size = udf_rw64(fe->inf_len);
61859f988b79SJean-Baptiste Boric 	} else {
61869f988b79SJean-Baptiste Boric 		assert(dir_node->efe);
61879f988b79SJean-Baptiste Boric 		file_size = udf_rw64(efe->inf_len);
61889f988b79SJean-Baptiste Boric 	}
61899f988b79SJean-Baptiste Boric 	if (*offset >= file_size)
61909f988b79SJean-Baptiste Boric 		return EINVAL;
61919f988b79SJean-Baptiste Boric 
61929f988b79SJean-Baptiste Boric 	/* get maximum length of FID descriptor */
61939f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
61949f988b79SJean-Baptiste Boric 
61959f988b79SJean-Baptiste Boric 	/* initialise return values */
61969f988b79SJean-Baptiste Boric 	fid_size = 0;
61979f988b79SJean-Baptiste Boric 	memset(dirent, 0, sizeof(struct dirent));
61989f988b79SJean-Baptiste Boric 	memset(fid, 0, lb_size);
61999f988b79SJean-Baptiste Boric 
62009f988b79SJean-Baptiste Boric 	enough  = (file_size - (*offset) >= UDF_FID_SIZE);
62019f988b79SJean-Baptiste Boric 	if (!enough) {
62029f988b79SJean-Baptiste Boric 		/* short dir ... */
62039f988b79SJean-Baptiste Boric 		return EIO;
62049f988b79SJean-Baptiste Boric 	}
62059f988b79SJean-Baptiste Boric 
62069f988b79SJean-Baptiste Boric 	error = vn_rdwr(UIO_READ, vp,
62079f988b79SJean-Baptiste Boric 			fid, MIN(file_size - (*offset), lb_size), *offset,
62089f988b79SJean-Baptiste Boric 			UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, FSCRED,
62099f988b79SJean-Baptiste Boric 			NULL, NULL);
62109f988b79SJean-Baptiste Boric 	if (error)
62119f988b79SJean-Baptiste Boric 		return error;
62129f988b79SJean-Baptiste Boric 
62139f988b79SJean-Baptiste Boric 	DPRINTF(FIDS, ("\tfid piece read in fine\n"));
62149f988b79SJean-Baptiste Boric 	/*
62159f988b79SJean-Baptiste Boric 	 * Check if we got a whole descriptor.
62169f988b79SJean-Baptiste Boric 	 * TODO Try to `resync' directory stream when something is very wrong.
62179f988b79SJean-Baptiste Boric 	 */
62189f988b79SJean-Baptiste Boric 
62199f988b79SJean-Baptiste Boric 	/* check if our FID header is OK */
62209f988b79SJean-Baptiste Boric 	error = udf_check_tag(fid);
62219f988b79SJean-Baptiste Boric 	if (error) {
62229f988b79SJean-Baptiste Boric 		goto brokendir;
62239f988b79SJean-Baptiste Boric 	}
62249f988b79SJean-Baptiste Boric 	DPRINTF(FIDS, ("\ttag check ok\n"));
62259f988b79SJean-Baptiste Boric 
62269f988b79SJean-Baptiste Boric 	if (udf_rw16(fid->tag.id) != TAGID_FID) {
62279f988b79SJean-Baptiste Boric 		error = EIO;
62289f988b79SJean-Baptiste Boric 		goto brokendir;
62299f988b79SJean-Baptiste Boric 	}
62309f988b79SJean-Baptiste Boric 	DPRINTF(FIDS, ("\ttag checked ok: got TAGID_FID\n"));
62319f988b79SJean-Baptiste Boric 
62329f988b79SJean-Baptiste Boric 	/* check for length */
62339f988b79SJean-Baptiste Boric 	fid_size = udf_fidsize(fid);
62349f988b79SJean-Baptiste Boric 	enough = (file_size - (*offset) >= fid_size);
62359f988b79SJean-Baptiste Boric 	if (!enough) {
62369f988b79SJean-Baptiste Boric 		error = EIO;
62379f988b79SJean-Baptiste Boric 		goto brokendir;
62389f988b79SJean-Baptiste Boric 	}
62399f988b79SJean-Baptiste Boric 	DPRINTF(FIDS, ("\tthe complete fid is read in\n"));
62409f988b79SJean-Baptiste Boric 
62419f988b79SJean-Baptiste Boric 	/* check FID contents */
62429f988b79SJean-Baptiste Boric 	error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
62439f988b79SJean-Baptiste Boric brokendir:
62449f988b79SJean-Baptiste Boric 	if (error) {
62459f988b79SJean-Baptiste Boric 		/* note that is sometimes a bit quick to report */
62469f988b79SJean-Baptiste Boric 		printf("UDF: BROKEN DIRECTORY ENTRY\n");
62479f988b79SJean-Baptiste Boric 		/* RESYNC? */
62489f988b79SJean-Baptiste Boric 		/* TODO: use udf_resync_fid_stream */
62499f988b79SJean-Baptiste Boric 		return EIO;
62509f988b79SJean-Baptiste Boric 	}
62519f988b79SJean-Baptiste Boric 	DPRINTF(FIDS, ("\tpayload checked ok\n"));
62529f988b79SJean-Baptiste Boric 
62539f988b79SJean-Baptiste Boric 	/* we got a whole and valid descriptor! */
62549f988b79SJean-Baptiste Boric 	DPRINTF(FIDS, ("\tinterpret FID\n"));
62559f988b79SJean-Baptiste Boric 
62569f988b79SJean-Baptiste Boric 	/* create resulting dirent structure */
62579f988b79SJean-Baptiste Boric 	fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
62589f988b79SJean-Baptiste Boric 	udf_to_unix_name(dirent->d_name, NAME_MAX,
62599f988b79SJean-Baptiste Boric 		fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
62609f988b79SJean-Baptiste Boric 
62619f988b79SJean-Baptiste Boric 	/* '..' has no name, so provide one */
62629f988b79SJean-Baptiste Boric 	if (fid->file_char & UDF_FILE_CHAR_PAR)
62639f988b79SJean-Baptiste Boric 		strcpy(dirent->d_name, "..");
62649f988b79SJean-Baptiste Boric 
62659f988b79SJean-Baptiste Boric 	dirent->d_fileno = udf_get_node_id(&fid->icb);	/* inode hash XXX */
62669f988b79SJean-Baptiste Boric 	dirent->d_namlen = strlen(dirent->d_name);
62679f988b79SJean-Baptiste Boric 	dirent->d_reclen = _DIRENT_SIZE(dirent);
62689f988b79SJean-Baptiste Boric 
62699f988b79SJean-Baptiste Boric 	/*
62709f988b79SJean-Baptiste Boric 	 * Note that its not worth trying to go for the filetypes now... its
62719f988b79SJean-Baptiste Boric 	 * too expensive too
62729f988b79SJean-Baptiste Boric 	 */
62739f988b79SJean-Baptiste Boric 	dirent->d_type = DT_UNKNOWN;
62749f988b79SJean-Baptiste Boric 
62759f988b79SJean-Baptiste Boric 	/* initial guess for filetype we can make */
62769f988b79SJean-Baptiste Boric 	if (fid->file_char & UDF_FILE_CHAR_DIR)
62779f988b79SJean-Baptiste Boric 		dirent->d_type = DT_DIR;
62789f988b79SJean-Baptiste Boric 
62799f988b79SJean-Baptiste Boric 	/* advance */
62809f988b79SJean-Baptiste Boric 	*offset += fid_size;
62819f988b79SJean-Baptiste Boric 
62829f988b79SJean-Baptiste Boric 	return error;
62839f988b79SJean-Baptiste Boric }
62849f988b79SJean-Baptiste Boric 
62859f988b79SJean-Baptiste Boric 
62869f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
62879f988b79SJean-Baptiste Boric 
62889f988b79SJean-Baptiste Boric static void
udf_sync_pass(struct udf_mount * ump,kauth_cred_t cred,int pass,int * ndirty)6289*0a6a1f1dSLionel Sambuc udf_sync_pass(struct udf_mount *ump, kauth_cred_t cred, int pass, int *ndirty)
62909f988b79SJean-Baptiste Boric {
62919f988b79SJean-Baptiste Boric 	struct udf_node *udf_node, *n_udf_node;
62929f988b79SJean-Baptiste Boric 	struct vnode *vp;
62939f988b79SJean-Baptiste Boric 	int vdirty, error;
62949f988b79SJean-Baptiste Boric 
6295*0a6a1f1dSLionel Sambuc 	KASSERT(mutex_owned(&ump->sync_lock));
62969f988b79SJean-Baptiste Boric 
62979f988b79SJean-Baptiste Boric 	DPRINTF(SYNC, ("sync_pass %d\n", pass));
62989f988b79SJean-Baptiste Boric 	udf_node = RB_TREE_MIN(&ump->udf_node_tree);
62999f988b79SJean-Baptiste Boric 	for (;udf_node; udf_node = n_udf_node) {
63009f988b79SJean-Baptiste Boric 		DPRINTF(SYNC, ("."));
63019f988b79SJean-Baptiste Boric 
63029f988b79SJean-Baptiste Boric 		vp = udf_node->vnode;
63039f988b79SJean-Baptiste Boric 
63049f988b79SJean-Baptiste Boric 		n_udf_node = rb_tree_iterate(&ump->udf_node_tree,
63059f988b79SJean-Baptiste Boric 		    udf_node, RB_DIR_RIGHT);
63069f988b79SJean-Baptiste Boric 
6307*0a6a1f1dSLionel Sambuc 		error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
63089f988b79SJean-Baptiste Boric 		if (error) {
6309*0a6a1f1dSLionel Sambuc 			KASSERT(error == EBUSY);
63109f988b79SJean-Baptiste Boric 			*ndirty += 1;
63119f988b79SJean-Baptiste Boric 			continue;
63129f988b79SJean-Baptiste Boric 		}
63139f988b79SJean-Baptiste Boric 
63149f988b79SJean-Baptiste Boric 		switch (pass) {
63159f988b79SJean-Baptiste Boric 		case 1:
63169f988b79SJean-Baptiste Boric 			VOP_FSYNC(vp, cred, 0 | FSYNC_DATAONLY,0,0);
63179f988b79SJean-Baptiste Boric 			break;
63189f988b79SJean-Baptiste Boric 		case 2:
63199f988b79SJean-Baptiste Boric 			vdirty = vp->v_numoutput;
63209f988b79SJean-Baptiste Boric 			if (vp->v_tag == VT_UDF)
63219f988b79SJean-Baptiste Boric 				vdirty += udf_node->outstanding_bufs +
63229f988b79SJean-Baptiste Boric 					udf_node->outstanding_nodedscr;
63239f988b79SJean-Baptiste Boric 			if (vdirty == 0)
63249f988b79SJean-Baptiste Boric 				VOP_FSYNC(vp, cred, 0,0,0);
63259f988b79SJean-Baptiste Boric 			*ndirty += vdirty;
63269f988b79SJean-Baptiste Boric 			break;
63279f988b79SJean-Baptiste Boric 		case 3:
63289f988b79SJean-Baptiste Boric 			vdirty = vp->v_numoutput;
63299f988b79SJean-Baptiste Boric 			if (vp->v_tag == VT_UDF)
63309f988b79SJean-Baptiste Boric 				vdirty += udf_node->outstanding_bufs +
63319f988b79SJean-Baptiste Boric 					udf_node->outstanding_nodedscr;
63329f988b79SJean-Baptiste Boric 			*ndirty += vdirty;
63339f988b79SJean-Baptiste Boric 			break;
63349f988b79SJean-Baptiste Boric 		}
63359f988b79SJean-Baptiste Boric 
6336*0a6a1f1dSLionel Sambuc 		VOP_UNLOCK(vp);
63379f988b79SJean-Baptiste Boric 	}
63389f988b79SJean-Baptiste Boric 	DPRINTF(SYNC, ("END sync_pass %d\n", pass));
63399f988b79SJean-Baptiste Boric }
63409f988b79SJean-Baptiste Boric 
63419f988b79SJean-Baptiste Boric 
6342*0a6a1f1dSLionel Sambuc static bool
udf_sync_selector(void * cl,struct vnode * vp)6343*0a6a1f1dSLionel Sambuc udf_sync_selector(void *cl, struct vnode *vp)
6344*0a6a1f1dSLionel Sambuc {
6345*0a6a1f1dSLionel Sambuc 	struct udf_node *udf_node = VTOI(vp);
6346*0a6a1f1dSLionel Sambuc 
6347*0a6a1f1dSLionel Sambuc 	if (vp->v_vflag & VV_SYSTEM)
6348*0a6a1f1dSLionel Sambuc 		return false;
6349*0a6a1f1dSLionel Sambuc 	if (vp->v_type == VNON)
6350*0a6a1f1dSLionel Sambuc 		return false;
6351*0a6a1f1dSLionel Sambuc 	if (udf_node == NULL)
6352*0a6a1f1dSLionel Sambuc 		return false;
6353*0a6a1f1dSLionel Sambuc 	if ((udf_node->i_flags & (IN_ACCESSED | IN_UPDATE | IN_MODIFIED)) == 0)
6354*0a6a1f1dSLionel Sambuc 		return false;
6355*0a6a1f1dSLionel Sambuc 	if (LIST_EMPTY(&vp->v_dirtyblkhd) && UVM_OBJ_IS_CLEAN(&vp->v_uobj))
6356*0a6a1f1dSLionel Sambuc 		return false;
6357*0a6a1f1dSLionel Sambuc 
6358*0a6a1f1dSLionel Sambuc 	return true;
6359*0a6a1f1dSLionel Sambuc }
6360*0a6a1f1dSLionel Sambuc 
63619f988b79SJean-Baptiste Boric void
udf_do_sync(struct udf_mount * ump,kauth_cred_t cred,int waitfor)63629f988b79SJean-Baptiste Boric udf_do_sync(struct udf_mount *ump, kauth_cred_t cred, int waitfor)
63639f988b79SJean-Baptiste Boric {
6364*0a6a1f1dSLionel Sambuc 	struct vnode_iterator *marker;
6365*0a6a1f1dSLionel Sambuc 	struct vnode *vp;
6366*0a6a1f1dSLionel Sambuc 	struct udf_node *udf_node, *udf_next_node;
63679f988b79SJean-Baptiste Boric 	int dummy, ndirty;
63689f988b79SJean-Baptiste Boric 
6369*0a6a1f1dSLionel Sambuc 	if (waitfor == MNT_LAZY)
6370*0a6a1f1dSLionel Sambuc 		return;
6371*0a6a1f1dSLionel Sambuc 
6372*0a6a1f1dSLionel Sambuc 	mutex_enter(&ump->sync_lock);
6373*0a6a1f1dSLionel Sambuc 
6374*0a6a1f1dSLionel Sambuc 	/* Fill the rbtree with nodes to sync. */
6375*0a6a1f1dSLionel Sambuc 	vfs_vnode_iterator_init(ump->vfs_mountp, &marker);
6376*0a6a1f1dSLionel Sambuc 	while ((vp = vfs_vnode_iterator_next(marker,
6377*0a6a1f1dSLionel Sambuc 	    udf_sync_selector, NULL)) != NULL) {
6378*0a6a1f1dSLionel Sambuc 		udf_node = VTOI(vp);
6379*0a6a1f1dSLionel Sambuc 		udf_node->i_flags |= IN_SYNCED;
6380*0a6a1f1dSLionel Sambuc 		rb_tree_insert_node(&ump->udf_node_tree, udf_node);
6381*0a6a1f1dSLionel Sambuc 	}
6382*0a6a1f1dSLionel Sambuc 	vfs_vnode_iterator_destroy(marker);
6383*0a6a1f1dSLionel Sambuc 
63849f988b79SJean-Baptiste Boric 	dummy = 0;
63859f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
63869f988b79SJean-Baptiste Boric 	DPRINTF(SYNC, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
6387*0a6a1f1dSLionel Sambuc 	udf_sync_pass(ump, cred, 1, &dummy);
63889f988b79SJean-Baptiste Boric 
63899f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
63909f988b79SJean-Baptiste Boric 	DPRINTF(SYNC, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
6391*0a6a1f1dSLionel Sambuc 	udf_sync_pass(ump, cred, 2, &dummy);
63929f988b79SJean-Baptiste Boric 
63939f988b79SJean-Baptiste Boric 	if (waitfor == MNT_WAIT) {
6394*0a6a1f1dSLionel Sambuc recount:
63959f988b79SJean-Baptiste Boric 		ndirty = ump->devvp->v_numoutput;
63969f988b79SJean-Baptiste Boric 		DPRINTF(SYNC, ("counting pending blocks: on devvp %d\n",
63979f988b79SJean-Baptiste Boric 			ndirty));
6398*0a6a1f1dSLionel Sambuc 		udf_sync_pass(ump, cred, 3, &ndirty);
63999f988b79SJean-Baptiste Boric 		DPRINTF(SYNC, ("counted num dirty pending blocks %d\n",
64009f988b79SJean-Baptiste Boric 			ndirty));
64019f988b79SJean-Baptiste Boric 
64029f988b79SJean-Baptiste Boric 		if (ndirty) {
64039f988b79SJean-Baptiste Boric 			/* 1/4 second wait */
6404*0a6a1f1dSLionel Sambuc 			kpause("udfsync2", false, hz/4, NULL);
64059f988b79SJean-Baptiste Boric 			goto recount;
64069f988b79SJean-Baptiste Boric 		}
64079f988b79SJean-Baptiste Boric 	}
64089f988b79SJean-Baptiste Boric 
6409*0a6a1f1dSLionel Sambuc 	/* Clean the rbtree. */
6410*0a6a1f1dSLionel Sambuc 	for (udf_node = RB_TREE_MIN(&ump->udf_node_tree);
6411*0a6a1f1dSLionel Sambuc 	    udf_node; udf_node = udf_next_node) {
6412*0a6a1f1dSLionel Sambuc 		udf_next_node = rb_tree_iterate(&ump->udf_node_tree,
6413*0a6a1f1dSLionel Sambuc 		    udf_node, RB_DIR_RIGHT);
6414*0a6a1f1dSLionel Sambuc 		rb_tree_remove_node(&ump->udf_node_tree, udf_node);
6415*0a6a1f1dSLionel Sambuc 		udf_node->i_flags &= ~IN_SYNCED;
6416*0a6a1f1dSLionel Sambuc 		vrele(udf_node->vnode);
6417*0a6a1f1dSLionel Sambuc 	}
6418*0a6a1f1dSLionel Sambuc 
6419*0a6a1f1dSLionel Sambuc 	mutex_exit(&ump->sync_lock);
64209f988b79SJean-Baptiste Boric }
64219f988b79SJean-Baptiste Boric 
64229f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
64239f988b79SJean-Baptiste Boric 
64249f988b79SJean-Baptiste Boric /*
64259f988b79SJean-Baptiste Boric  * Read and write file extent in/from the buffer.
64269f988b79SJean-Baptiste Boric  *
64279f988b79SJean-Baptiste Boric  * The splitup of the extent into seperate request-buffers is to minimise
64289f988b79SJean-Baptiste Boric  * copying around as much as possible.
64299f988b79SJean-Baptiste Boric  *
64309f988b79SJean-Baptiste Boric  * block based file reading and writing
64319f988b79SJean-Baptiste Boric  */
64329f988b79SJean-Baptiste Boric 
64339f988b79SJean-Baptiste Boric static int
udf_read_internal(struct udf_node * node,uint8_t * blob)64349f988b79SJean-Baptiste Boric udf_read_internal(struct udf_node *node, uint8_t *blob)
64359f988b79SJean-Baptiste Boric {
64369f988b79SJean-Baptiste Boric 	struct udf_mount *ump;
64379f988b79SJean-Baptiste Boric 	struct file_entry     *fe = node->fe;
64389f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = node->efe;
64399f988b79SJean-Baptiste Boric 	uint64_t inflen;
64409f988b79SJean-Baptiste Boric 	uint32_t sector_size;
64419f988b79SJean-Baptiste Boric 	uint8_t  *pos;
64429f988b79SJean-Baptiste Boric 	int icbflags, addr_type;
64439f988b79SJean-Baptiste Boric 
64449f988b79SJean-Baptiste Boric 	/* get extent and do some paranoia checks */
64459f988b79SJean-Baptiste Boric 	ump = node->ump;
64469f988b79SJean-Baptiste Boric 	sector_size = ump->discinfo.sector_size;
64479f988b79SJean-Baptiste Boric 
64489f988b79SJean-Baptiste Boric 	if (fe) {
64499f988b79SJean-Baptiste Boric 		inflen   = udf_rw64(fe->inf_len);
64509f988b79SJean-Baptiste Boric 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
64519f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(fe->icbtag.flags);
64529f988b79SJean-Baptiste Boric 	} else {
64539f988b79SJean-Baptiste Boric 		assert(node->efe);
64549f988b79SJean-Baptiste Boric 		inflen   = udf_rw64(efe->inf_len);
64559f988b79SJean-Baptiste Boric 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
64569f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(efe->icbtag.flags);
64579f988b79SJean-Baptiste Boric 	}
64589f988b79SJean-Baptiste Boric 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
64599f988b79SJean-Baptiste Boric 
64609f988b79SJean-Baptiste Boric 	assert(addr_type == UDF_ICB_INTERN_ALLOC);
6461*0a6a1f1dSLionel Sambuc 	__USE(addr_type);
64629f988b79SJean-Baptiste Boric 	assert(inflen < sector_size);
64639f988b79SJean-Baptiste Boric 
64649f988b79SJean-Baptiste Boric 	/* copy out info */
64659f988b79SJean-Baptiste Boric 	memset(blob, 0, sector_size);
64669f988b79SJean-Baptiste Boric 	memcpy(blob, pos, inflen);
64679f988b79SJean-Baptiste Boric 
64689f988b79SJean-Baptiste Boric 	return 0;
64699f988b79SJean-Baptiste Boric }
64709f988b79SJean-Baptiste Boric 
64719f988b79SJean-Baptiste Boric 
64729f988b79SJean-Baptiste Boric static int
udf_write_internal(struct udf_node * node,uint8_t * blob)64739f988b79SJean-Baptiste Boric udf_write_internal(struct udf_node *node, uint8_t *blob)
64749f988b79SJean-Baptiste Boric {
64759f988b79SJean-Baptiste Boric 	struct udf_mount *ump;
64769f988b79SJean-Baptiste Boric 	struct file_entry     *fe = node->fe;
64779f988b79SJean-Baptiste Boric 	struct extfile_entry *efe = node->efe;
64789f988b79SJean-Baptiste Boric 	uint64_t inflen;
64799f988b79SJean-Baptiste Boric 	uint32_t sector_size;
64809f988b79SJean-Baptiste Boric 	uint8_t  *pos;
64819f988b79SJean-Baptiste Boric 	int icbflags, addr_type;
64829f988b79SJean-Baptiste Boric 
64839f988b79SJean-Baptiste Boric 	/* get extent and do some paranoia checks */
64849f988b79SJean-Baptiste Boric 	ump = node->ump;
64859f988b79SJean-Baptiste Boric 	sector_size = ump->discinfo.sector_size;
64869f988b79SJean-Baptiste Boric 
64879f988b79SJean-Baptiste Boric 	if (fe) {
64889f988b79SJean-Baptiste Boric 		inflen   = udf_rw64(fe->inf_len);
64899f988b79SJean-Baptiste Boric 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
64909f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(fe->icbtag.flags);
64919f988b79SJean-Baptiste Boric 	} else {
64929f988b79SJean-Baptiste Boric 		assert(node->efe);
64939f988b79SJean-Baptiste Boric 		inflen   = udf_rw64(efe->inf_len);
64949f988b79SJean-Baptiste Boric 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
64959f988b79SJean-Baptiste Boric 		icbflags = udf_rw16(efe->icbtag.flags);
64969f988b79SJean-Baptiste Boric 	}
64979f988b79SJean-Baptiste Boric 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
64989f988b79SJean-Baptiste Boric 
64999f988b79SJean-Baptiste Boric 	assert(addr_type == UDF_ICB_INTERN_ALLOC);
6500*0a6a1f1dSLionel Sambuc 	__USE(addr_type);
65019f988b79SJean-Baptiste Boric 	assert(inflen < sector_size);
6502*0a6a1f1dSLionel Sambuc 	__USE(sector_size);
65039f988b79SJean-Baptiste Boric 
65049f988b79SJean-Baptiste Boric 	/* copy in blob */
65059f988b79SJean-Baptiste Boric 	/* memset(pos, 0, inflen); */
65069f988b79SJean-Baptiste Boric 	memcpy(pos, blob, inflen);
65079f988b79SJean-Baptiste Boric 
65089f988b79SJean-Baptiste Boric 	return 0;
65099f988b79SJean-Baptiste Boric }
65109f988b79SJean-Baptiste Boric 
65119f988b79SJean-Baptiste Boric 
65129f988b79SJean-Baptiste Boric void
udf_read_filebuf(struct udf_node * udf_node,struct buf * buf)65139f988b79SJean-Baptiste Boric udf_read_filebuf(struct udf_node *udf_node, struct buf *buf)
65149f988b79SJean-Baptiste Boric {
65159f988b79SJean-Baptiste Boric 	struct buf *nestbuf;
65169f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
65179f988b79SJean-Baptiste Boric 	uint64_t   *mapping;
65189f988b79SJean-Baptiste Boric 	uint64_t    run_start;
65199f988b79SJean-Baptiste Boric 	uint32_t    sector_size;
65209f988b79SJean-Baptiste Boric 	uint32_t    buf_offset, sector, rbuflen, rblk;
65219f988b79SJean-Baptiste Boric 	uint32_t    from, lblkno;
65229f988b79SJean-Baptiste Boric 	uint32_t    sectors;
65239f988b79SJean-Baptiste Boric 	uint8_t    *buf_pos;
65249f988b79SJean-Baptiste Boric 	int error, run_length, what;
65259f988b79SJean-Baptiste Boric 
65269f988b79SJean-Baptiste Boric 	sector_size = udf_node->ump->discinfo.sector_size;
65279f988b79SJean-Baptiste Boric 
65289f988b79SJean-Baptiste Boric 	from    = buf->b_blkno;
65299f988b79SJean-Baptiste Boric 	sectors = buf->b_bcount / sector_size;
65309f988b79SJean-Baptiste Boric 
65319f988b79SJean-Baptiste Boric 	what = udf_get_c_type(udf_node);
65329f988b79SJean-Baptiste Boric 
65339f988b79SJean-Baptiste Boric 	/* assure we have enough translation slots */
65349f988b79SJean-Baptiste Boric 	KASSERT(buf->b_bcount / sector_size <= UDF_MAX_MAPPINGS);
65359f988b79SJean-Baptiste Boric 	KASSERT(MAXPHYS / sector_size <= UDF_MAX_MAPPINGS);
65369f988b79SJean-Baptiste Boric 
65379f988b79SJean-Baptiste Boric 	if (sectors > UDF_MAX_MAPPINGS) {
65389f988b79SJean-Baptiste Boric 		printf("udf_read_filebuf: implementation limit on bufsize\n");
65399f988b79SJean-Baptiste Boric 		buf->b_error  = EIO;
65409f988b79SJean-Baptiste Boric 		biodone(buf);
65419f988b79SJean-Baptiste Boric 		return;
65429f988b79SJean-Baptiste Boric 	}
65439f988b79SJean-Baptiste Boric 
65449f988b79SJean-Baptiste Boric 	mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
65459f988b79SJean-Baptiste Boric 
65469f988b79SJean-Baptiste Boric 	error = 0;
65479f988b79SJean-Baptiste Boric 	DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
65489f988b79SJean-Baptiste Boric 	error = udf_translate_file_extent(udf_node, from, sectors, mapping);
65499f988b79SJean-Baptiste Boric 	if (error) {
65509f988b79SJean-Baptiste Boric 		buf->b_error  = error;
65519f988b79SJean-Baptiste Boric 		biodone(buf);
65529f988b79SJean-Baptiste Boric 		goto out;
65539f988b79SJean-Baptiste Boric 	}
65549f988b79SJean-Baptiste Boric 	DPRINTF(READ, ("\ttranslate extent went OK\n"));
65559f988b79SJean-Baptiste Boric 
65569f988b79SJean-Baptiste Boric 	/* pre-check if its an internal */
65579f988b79SJean-Baptiste Boric 	if (*mapping == UDF_TRANS_INTERN) {
65589f988b79SJean-Baptiste Boric 		error = udf_read_internal(udf_node, (uint8_t *) buf->b_data);
65599f988b79SJean-Baptiste Boric 		if (error)
65609f988b79SJean-Baptiste Boric 			buf->b_error  = error;
65619f988b79SJean-Baptiste Boric 		biodone(buf);
65629f988b79SJean-Baptiste Boric 		goto out;
65639f988b79SJean-Baptiste Boric 	}
65649f988b79SJean-Baptiste Boric 	DPRINTF(READ, ("\tnot intern\n"));
65659f988b79SJean-Baptiste Boric 
65669f988b79SJean-Baptiste Boric #ifdef DEBUG
65679f988b79SJean-Baptiste Boric 	if (udf_verbose & UDF_DEBUG_TRANSLATE) {
65689f988b79SJean-Baptiste Boric 		printf("Returned translation table:\n");
65699f988b79SJean-Baptiste Boric 		for (sector = 0; sector < sectors; sector++) {
65709f988b79SJean-Baptiste Boric 			printf("%d : %"PRIu64"\n", sector, mapping[sector]);
65719f988b79SJean-Baptiste Boric 		}
65729f988b79SJean-Baptiste Boric 	}
65739f988b79SJean-Baptiste Boric #endif
65749f988b79SJean-Baptiste Boric 
65759f988b79SJean-Baptiste Boric 	/* request read-in of data from disc sheduler */
65769f988b79SJean-Baptiste Boric 	buf->b_resid = buf->b_bcount;
65779f988b79SJean-Baptiste Boric 	for (sector = 0; sector < sectors; sector++) {
65789f988b79SJean-Baptiste Boric 		buf_offset = sector * sector_size;
65799f988b79SJean-Baptiste Boric 		buf_pos    = (uint8_t *) buf->b_data + buf_offset;
65809f988b79SJean-Baptiste Boric 		DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
65819f988b79SJean-Baptiste Boric 
65829f988b79SJean-Baptiste Boric 		/* check if its zero or unmapped to stop reading */
65839f988b79SJean-Baptiste Boric 		switch (mapping[sector]) {
65849f988b79SJean-Baptiste Boric 		case UDF_TRANS_UNMAPPED:
65859f988b79SJean-Baptiste Boric 		case UDF_TRANS_ZERO:
65869f988b79SJean-Baptiste Boric 			/* copy zero sector TODO runlength like below */
65879f988b79SJean-Baptiste Boric 			memset(buf_pos, 0, sector_size);
65889f988b79SJean-Baptiste Boric 			DPRINTF(READ, ("\treturning zero sector\n"));
65899f988b79SJean-Baptiste Boric 			nestiobuf_done(buf, sector_size, 0);
65909f988b79SJean-Baptiste Boric 			break;
65919f988b79SJean-Baptiste Boric 		default :
65929f988b79SJean-Baptiste Boric 			DPRINTF(READ, ("\tread sector "
65939f988b79SJean-Baptiste Boric 			    "%"PRIu64"\n", mapping[sector]));
65949f988b79SJean-Baptiste Boric 
65959f988b79SJean-Baptiste Boric 			lblkno = from + sector;
65969f988b79SJean-Baptiste Boric 			run_start  = mapping[sector];
65979f988b79SJean-Baptiste Boric 			run_length = 1;
65989f988b79SJean-Baptiste Boric 			while (sector < sectors-1) {
65999f988b79SJean-Baptiste Boric 				if (mapping[sector+1] != mapping[sector]+1)
66009f988b79SJean-Baptiste Boric 					break;
66019f988b79SJean-Baptiste Boric 				run_length++;
66029f988b79SJean-Baptiste Boric 				sector++;
66039f988b79SJean-Baptiste Boric 			}
66049f988b79SJean-Baptiste Boric 
66059f988b79SJean-Baptiste Boric 			/*
66069f988b79SJean-Baptiste Boric 			 * nest an iobuf and mark it for async reading. Since
66079f988b79SJean-Baptiste Boric 			 * we're using nested buffers, they can't be cached by
66089f988b79SJean-Baptiste Boric 			 * design.
66099f988b79SJean-Baptiste Boric 			 */
66109f988b79SJean-Baptiste Boric 			rbuflen = run_length * sector_size;
66119f988b79SJean-Baptiste Boric 			rblk    = run_start  * (sector_size/DEV_BSIZE);
66129f988b79SJean-Baptiste Boric 
66139f988b79SJean-Baptiste Boric 			nestbuf = getiobuf(NULL, true);
66149f988b79SJean-Baptiste Boric 			nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
66159f988b79SJean-Baptiste Boric 			/* nestbuf is B_ASYNC */
66169f988b79SJean-Baptiste Boric 
66179f988b79SJean-Baptiste Boric 			/* identify this nestbuf */
66189f988b79SJean-Baptiste Boric 			nestbuf->b_lblkno   = lblkno;
66199f988b79SJean-Baptiste Boric 			assert(nestbuf->b_vp == udf_node->vnode);
66209f988b79SJean-Baptiste Boric 
66219f988b79SJean-Baptiste Boric 			/* CD shedules on raw blkno */
66229f988b79SJean-Baptiste Boric 			nestbuf->b_blkno      = rblk;
66239f988b79SJean-Baptiste Boric 			nestbuf->b_proc       = NULL;
66249f988b79SJean-Baptiste Boric 			nestbuf->b_rawblkno   = rblk;
66259f988b79SJean-Baptiste Boric 			nestbuf->b_udf_c_type = what;
66269f988b79SJean-Baptiste Boric 
66279f988b79SJean-Baptiste Boric 			udf_discstrat_queuebuf(ump, nestbuf);
66289f988b79SJean-Baptiste Boric 		}
66299f988b79SJean-Baptiste Boric 	}
66309f988b79SJean-Baptiste Boric out:
66319f988b79SJean-Baptiste Boric 	/* if we're synchronously reading, wait for the completion */
66329f988b79SJean-Baptiste Boric 	if ((buf->b_flags & B_ASYNC) == 0)
66339f988b79SJean-Baptiste Boric 		biowait(buf);
66349f988b79SJean-Baptiste Boric 
66359f988b79SJean-Baptiste Boric 	DPRINTF(READ, ("\tend of read_filebuf\n"));
66369f988b79SJean-Baptiste Boric 	free(mapping, M_TEMP);
66379f988b79SJean-Baptiste Boric 	return;
66389f988b79SJean-Baptiste Boric }
66399f988b79SJean-Baptiste Boric 
66409f988b79SJean-Baptiste Boric 
66419f988b79SJean-Baptiste Boric void
udf_write_filebuf(struct udf_node * udf_node,struct buf * buf)66429f988b79SJean-Baptiste Boric udf_write_filebuf(struct udf_node *udf_node, struct buf *buf)
66439f988b79SJean-Baptiste Boric {
66449f988b79SJean-Baptiste Boric 	struct buf *nestbuf;
66459f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
66469f988b79SJean-Baptiste Boric 	uint64_t   *mapping;
66479f988b79SJean-Baptiste Boric 	uint64_t    run_start;
66489f988b79SJean-Baptiste Boric 	uint32_t    lb_size;
66499f988b79SJean-Baptiste Boric 	uint32_t    buf_offset, lb_num, rbuflen, rblk;
66509f988b79SJean-Baptiste Boric 	uint32_t    from, lblkno;
66519f988b79SJean-Baptiste Boric 	uint32_t    num_lb;
66529f988b79SJean-Baptiste Boric 	int error, run_length, what, s;
66539f988b79SJean-Baptiste Boric 
66549f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
66559f988b79SJean-Baptiste Boric 
66569f988b79SJean-Baptiste Boric 	from   = buf->b_blkno;
66579f988b79SJean-Baptiste Boric 	num_lb = buf->b_bcount / lb_size;
66589f988b79SJean-Baptiste Boric 
66599f988b79SJean-Baptiste Boric 	what = udf_get_c_type(udf_node);
66609f988b79SJean-Baptiste Boric 
66619f988b79SJean-Baptiste Boric 	/* assure we have enough translation slots */
66629f988b79SJean-Baptiste Boric 	KASSERT(buf->b_bcount / lb_size <= UDF_MAX_MAPPINGS);
66639f988b79SJean-Baptiste Boric 	KASSERT(MAXPHYS / lb_size <= UDF_MAX_MAPPINGS);
66649f988b79SJean-Baptiste Boric 
66659f988b79SJean-Baptiste Boric 	if (num_lb > UDF_MAX_MAPPINGS) {
66669f988b79SJean-Baptiste Boric 		printf("udf_write_filebuf: implementation limit on bufsize\n");
66679f988b79SJean-Baptiste Boric 		buf->b_error  = EIO;
66689f988b79SJean-Baptiste Boric 		biodone(buf);
66699f988b79SJean-Baptiste Boric 		return;
66709f988b79SJean-Baptiste Boric 	}
66719f988b79SJean-Baptiste Boric 
66729f988b79SJean-Baptiste Boric 	mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
66739f988b79SJean-Baptiste Boric 
66749f988b79SJean-Baptiste Boric 	error = 0;
66759f988b79SJean-Baptiste Boric 	DPRINTF(WRITE, ("\ttranslate %d-%d\n", from, num_lb));
66769f988b79SJean-Baptiste Boric 	error = udf_translate_file_extent(udf_node, from, num_lb, mapping);
66779f988b79SJean-Baptiste Boric 	if (error) {
66789f988b79SJean-Baptiste Boric 		buf->b_error  = error;
66799f988b79SJean-Baptiste Boric 		biodone(buf);
66809f988b79SJean-Baptiste Boric 		goto out;
66819f988b79SJean-Baptiste Boric 	}
66829f988b79SJean-Baptiste Boric 	DPRINTF(WRITE, ("\ttranslate extent went OK\n"));
66839f988b79SJean-Baptiste Boric 
66849f988b79SJean-Baptiste Boric 	/* if its internally mapped, we can write it in the descriptor itself */
66859f988b79SJean-Baptiste Boric 	if (*mapping == UDF_TRANS_INTERN) {
66869f988b79SJean-Baptiste Boric 		/* TODO paranoia check if we ARE going to have enough space */
66879f988b79SJean-Baptiste Boric 		error = udf_write_internal(udf_node, (uint8_t *) buf->b_data);
66889f988b79SJean-Baptiste Boric 		if (error)
66899f988b79SJean-Baptiste Boric 			buf->b_error  = error;
66909f988b79SJean-Baptiste Boric 		biodone(buf);
66919f988b79SJean-Baptiste Boric 		goto out;
66929f988b79SJean-Baptiste Boric 	}
66939f988b79SJean-Baptiste Boric 	DPRINTF(WRITE, ("\tnot intern\n"));
66949f988b79SJean-Baptiste Boric 
66959f988b79SJean-Baptiste Boric 	/* request write out of data to disc sheduler */
66969f988b79SJean-Baptiste Boric 	buf->b_resid = buf->b_bcount;
66979f988b79SJean-Baptiste Boric 	for (lb_num = 0; lb_num < num_lb; lb_num++) {
66989f988b79SJean-Baptiste Boric 		buf_offset = lb_num * lb_size;
66999f988b79SJean-Baptiste Boric 		DPRINTF(WRITE, ("\tprocessing rel lb_num %d\n", lb_num));
67009f988b79SJean-Baptiste Boric 
67019f988b79SJean-Baptiste Boric 		/*
67029f988b79SJean-Baptiste Boric 		 * Mappings are not that important here. Just before we write
67039f988b79SJean-Baptiste Boric 		 * the lb_num we late-allocate them when needed and update the
67049f988b79SJean-Baptiste Boric 		 * mapping in the udf_node.
67059f988b79SJean-Baptiste Boric 		 */
67069f988b79SJean-Baptiste Boric 
67079f988b79SJean-Baptiste Boric 		/* XXX why not ignore the mapping altogether ? */
67089f988b79SJean-Baptiste Boric 		DPRINTF(WRITE, ("\twrite lb_num "
67099f988b79SJean-Baptiste Boric 		    "%"PRIu64, mapping[lb_num]));
67109f988b79SJean-Baptiste Boric 
67119f988b79SJean-Baptiste Boric 		lblkno = from + lb_num;
67129f988b79SJean-Baptiste Boric 		run_start  = mapping[lb_num];
67139f988b79SJean-Baptiste Boric 		run_length = 1;
67149f988b79SJean-Baptiste Boric 		while (lb_num < num_lb-1) {
67159f988b79SJean-Baptiste Boric 			if (mapping[lb_num+1] != mapping[lb_num]+1)
67169f988b79SJean-Baptiste Boric 				if (mapping[lb_num+1] != mapping[lb_num])
67179f988b79SJean-Baptiste Boric 					break;
67189f988b79SJean-Baptiste Boric 			run_length++;
67199f988b79SJean-Baptiste Boric 			lb_num++;
67209f988b79SJean-Baptiste Boric 		}
67219f988b79SJean-Baptiste Boric 		DPRINTF(WRITE, ("+ %d\n", run_length));
67229f988b79SJean-Baptiste Boric 
67239f988b79SJean-Baptiste Boric 		/* nest an iobuf on the master buffer for the extent */
67249f988b79SJean-Baptiste Boric 		rbuflen = run_length * lb_size;
67259f988b79SJean-Baptiste Boric 		rblk = run_start * (lb_size/DEV_BSIZE);
67269f988b79SJean-Baptiste Boric 
67279f988b79SJean-Baptiste Boric 		nestbuf = getiobuf(NULL, true);
67289f988b79SJean-Baptiste Boric 		nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
67299f988b79SJean-Baptiste Boric 		/* nestbuf is B_ASYNC */
67309f988b79SJean-Baptiste Boric 
67319f988b79SJean-Baptiste Boric 		/* identify this nestbuf */
67329f988b79SJean-Baptiste Boric 		nestbuf->b_lblkno   = lblkno;
67339f988b79SJean-Baptiste Boric 		KASSERT(nestbuf->b_vp == udf_node->vnode);
67349f988b79SJean-Baptiste Boric 
67359f988b79SJean-Baptiste Boric 		/* CD shedules on raw blkno */
67369f988b79SJean-Baptiste Boric 		nestbuf->b_blkno      = rblk;
67379f988b79SJean-Baptiste Boric 		nestbuf->b_proc       = NULL;
67389f988b79SJean-Baptiste Boric 		nestbuf->b_rawblkno   = rblk;
67399f988b79SJean-Baptiste Boric 		nestbuf->b_udf_c_type = what;
67409f988b79SJean-Baptiste Boric 
67419f988b79SJean-Baptiste Boric 		/* increment our outstanding bufs counter */
67429f988b79SJean-Baptiste Boric 		s = splbio();
67439f988b79SJean-Baptiste Boric 			udf_node->outstanding_bufs++;
67449f988b79SJean-Baptiste Boric 		splx(s);
67459f988b79SJean-Baptiste Boric 
67469f988b79SJean-Baptiste Boric 		udf_discstrat_queuebuf(ump, nestbuf);
67479f988b79SJean-Baptiste Boric 	}
67489f988b79SJean-Baptiste Boric out:
67499f988b79SJean-Baptiste Boric 	/* if we're synchronously writing, wait for the completion */
67509f988b79SJean-Baptiste Boric 	if ((buf->b_flags & B_ASYNC) == 0)
67519f988b79SJean-Baptiste Boric 		biowait(buf);
67529f988b79SJean-Baptiste Boric 
67539f988b79SJean-Baptiste Boric 	DPRINTF(WRITE, ("\tend of write_filebuf\n"));
67549f988b79SJean-Baptiste Boric 	free(mapping, M_TEMP);
67559f988b79SJean-Baptiste Boric 	return;
67569f988b79SJean-Baptiste Boric }
67579f988b79SJean-Baptiste Boric 
67589f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
6759