xref: /minix3/sys/fs/udf/udf_allocation.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: udf_allocation.c,v 1.38 2015/08/24 08:30:17 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 #include <sys/cdefs.h>
309f988b79SJean-Baptiste Boric #ifndef lint
31*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: udf_allocation.c,v 1.38 2015/08/24 08:30:17 hannken Exp $");
329f988b79SJean-Baptiste Boric #endif /* not lint */
339f988b79SJean-Baptiste Boric 
349f988b79SJean-Baptiste Boric 
359f988b79SJean-Baptiste Boric #if defined(_KERNEL_OPT)
369f988b79SJean-Baptiste Boric #include "opt_compat_netbsd.h"
379f988b79SJean-Baptiste Boric #endif
389f988b79SJean-Baptiste Boric 
399f988b79SJean-Baptiste Boric /* TODO strip */
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 <sys/kthread.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 
659f988b79SJean-Baptiste Boric #include "udf.h"
669f988b79SJean-Baptiste Boric #include "udf_subr.h"
679f988b79SJean-Baptiste Boric #include "udf_bswap.h"
689f988b79SJean-Baptiste Boric 
699f988b79SJean-Baptiste Boric 
709f988b79SJean-Baptiste Boric #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
719f988b79SJean-Baptiste Boric 
729f988b79SJean-Baptiste Boric static void udf_record_allocation_in_node(struct udf_mount *ump,
739f988b79SJean-Baptiste Boric 	struct buf *buf, uint16_t vpart_num, uint64_t *mapping,
749f988b79SJean-Baptiste Boric 	struct long_ad *node_ad_cpy);
759f988b79SJean-Baptiste Boric 
769f988b79SJean-Baptiste Boric static void udf_collect_free_space_for_vpart(struct udf_mount *ump,
779f988b79SJean-Baptiste Boric 	uint16_t vpart_num, uint32_t num_lb);
789f988b79SJean-Baptiste Boric 
799f988b79SJean-Baptiste Boric static int udf_ads_merge(uint32_t max_len, uint32_t lb_size, struct long_ad *a1, struct long_ad *a2);
809f988b79SJean-Baptiste Boric static void udf_wipe_adslots(struct udf_node *udf_node);
819f988b79SJean-Baptiste Boric static void udf_count_alloc_exts(struct udf_node *udf_node);
829f988b79SJean-Baptiste Boric 
839f988b79SJean-Baptiste Boric 
849f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
859f988b79SJean-Baptiste Boric 
869f988b79SJean-Baptiste Boric #if 0
879f988b79SJean-Baptiste Boric #if 1
889f988b79SJean-Baptiste Boric static void
899f988b79SJean-Baptiste Boric udf_node_dump(struct udf_node *udf_node) {
909f988b79SJean-Baptiste Boric 	struct file_entry    *fe;
919f988b79SJean-Baptiste Boric 	struct extfile_entry *efe;
929f988b79SJean-Baptiste Boric 	struct icb_tag *icbtag;
939f988b79SJean-Baptiste Boric 	struct long_ad s_ad;
949f988b79SJean-Baptiste Boric 	uint64_t inflen;
959f988b79SJean-Baptiste Boric 	uint32_t icbflags, addr_type;
969f988b79SJean-Baptiste Boric 	uint32_t len, lb_num;
979f988b79SJean-Baptiste Boric 	uint32_t flags;
989f988b79SJean-Baptiste Boric 	int part_num;
999f988b79SJean-Baptiste Boric 	int lb_size, eof, slot;
1009f988b79SJean-Baptiste Boric 
1019f988b79SJean-Baptiste Boric 	if ((udf_verbose & UDF_DEBUG_NODEDUMP) == 0)
1029f988b79SJean-Baptiste Boric 		return;
1039f988b79SJean-Baptiste Boric 
1049f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
1059f988b79SJean-Baptiste Boric 
1069f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
1079f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
1089f988b79SJean-Baptiste Boric 	if (fe) {
1099f988b79SJean-Baptiste Boric 		icbtag = &fe->icbtag;
1109f988b79SJean-Baptiste Boric 		inflen = udf_rw64(fe->inf_len);
1119f988b79SJean-Baptiste Boric 	} else {
1129f988b79SJean-Baptiste Boric 		icbtag = &efe->icbtag;
1139f988b79SJean-Baptiste Boric 		inflen = udf_rw64(efe->inf_len);
1149f988b79SJean-Baptiste Boric 	}
1159f988b79SJean-Baptiste Boric 
1169f988b79SJean-Baptiste Boric 	icbflags   = udf_rw16(icbtag->flags);
1179f988b79SJean-Baptiste Boric 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1189f988b79SJean-Baptiste Boric 
1199f988b79SJean-Baptiste Boric 	printf("udf_node_dump %p :\n", udf_node);
1209f988b79SJean-Baptiste Boric 
1219f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
1229f988b79SJean-Baptiste Boric 		printf("\tIntern alloc, len = %"PRIu64"\n", inflen);
1239f988b79SJean-Baptiste Boric 		return;
1249f988b79SJean-Baptiste Boric 	}
1259f988b79SJean-Baptiste Boric 
1269f988b79SJean-Baptiste Boric 	printf("\tInflen  = %"PRIu64"\n", inflen);
1279f988b79SJean-Baptiste Boric 	printf("\t\t");
1289f988b79SJean-Baptiste Boric 
1299f988b79SJean-Baptiste Boric 	slot = 0;
1309f988b79SJean-Baptiste Boric 	for (;;) {
1319f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
1329f988b79SJean-Baptiste Boric 		if (eof)
1339f988b79SJean-Baptiste Boric 			break;
1349f988b79SJean-Baptiste Boric 		part_num = udf_rw16(s_ad.loc.part_num);
1359f988b79SJean-Baptiste Boric 		lb_num = udf_rw32(s_ad.loc.lb_num);
1369f988b79SJean-Baptiste Boric 		len   = udf_rw32(s_ad.len);
1379f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(len);
1389f988b79SJean-Baptiste Boric 		len   = UDF_EXT_LEN(len);
1399f988b79SJean-Baptiste Boric 
1409f988b79SJean-Baptiste Boric 		printf("[");
1419f988b79SJean-Baptiste Boric 		if (part_num >= 0)
1429f988b79SJean-Baptiste Boric 			printf("part %d, ", part_num);
1439f988b79SJean-Baptiste Boric 		printf("lb_num %d, len %d", lb_num, len);
1449f988b79SJean-Baptiste Boric 		if (flags)
1459f988b79SJean-Baptiste Boric 			printf(", flags %d", flags>>30);
1469f988b79SJean-Baptiste Boric 		printf("] ");
1479f988b79SJean-Baptiste Boric 
1489f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
1499f988b79SJean-Baptiste Boric 			printf("\n\textent END\n\tallocation extent\n\t\t");
1509f988b79SJean-Baptiste Boric 		}
1519f988b79SJean-Baptiste Boric 
1529f988b79SJean-Baptiste Boric 		slot++;
1539f988b79SJean-Baptiste Boric 	}
1549f988b79SJean-Baptiste Boric 	printf("\n\tl_ad END\n\n");
1559f988b79SJean-Baptiste Boric }
1569f988b79SJean-Baptiste Boric #else
1579f988b79SJean-Baptiste Boric #define udf_node_dump(a)
1589f988b79SJean-Baptiste Boric #endif
1599f988b79SJean-Baptiste Boric 
1609f988b79SJean-Baptiste Boric 
1619f988b79SJean-Baptiste Boric static void
1629f988b79SJean-Baptiste Boric udf_assert_allocated(struct udf_mount *ump, uint16_t vpart_num,
1639f988b79SJean-Baptiste Boric 	uint32_t lb_num, uint32_t num_lb)
1649f988b79SJean-Baptiste Boric {
1659f988b79SJean-Baptiste Boric 	struct udf_bitmap *bitmap;
1669f988b79SJean-Baptiste Boric 	struct part_desc *pdesc;
1679f988b79SJean-Baptiste Boric 	uint32_t ptov;
1689f988b79SJean-Baptiste Boric 	uint32_t bitval;
1699f988b79SJean-Baptiste Boric 	uint8_t *bpos;
1709f988b79SJean-Baptiste Boric 	int bit;
1719f988b79SJean-Baptiste Boric 	int phys_part;
1729f988b79SJean-Baptiste Boric 	int ok;
1739f988b79SJean-Baptiste Boric 
1749f988b79SJean-Baptiste Boric 	DPRINTF(PARANOIA, ("udf_assert_allocated: check virt lbnum %d "
1759f988b79SJean-Baptiste Boric 			  "part %d + %d sect\n", lb_num, vpart_num, num_lb));
1769f988b79SJean-Baptiste Boric 
1779f988b79SJean-Baptiste Boric 	/* get partition backing up this vpart_num */
1789f988b79SJean-Baptiste Boric 	pdesc = ump->partitions[ump->vtop[vpart_num]];
1799f988b79SJean-Baptiste Boric 
1809f988b79SJean-Baptiste Boric 	switch (ump->vtop_tp[vpart_num]) {
1819f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_PHYS :
1829f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_SPARABLE :
1839f988b79SJean-Baptiste Boric 		/* free space to freed or unallocated space bitmap */
1849f988b79SJean-Baptiste Boric 		ptov      = udf_rw32(pdesc->start_loc);
1859f988b79SJean-Baptiste Boric 		phys_part = ump->vtop[vpart_num];
1869f988b79SJean-Baptiste Boric 
1879f988b79SJean-Baptiste Boric 		/* use unallocated bitmap */
1889f988b79SJean-Baptiste Boric 		bitmap = &ump->part_unalloc_bits[phys_part];
1899f988b79SJean-Baptiste Boric 
1909f988b79SJean-Baptiste Boric 		/* if no bitmaps are defined, bail out */
1919f988b79SJean-Baptiste Boric 		if (bitmap->bits == NULL)
1929f988b79SJean-Baptiste Boric 			break;
1939f988b79SJean-Baptiste Boric 
1949f988b79SJean-Baptiste Boric 		/* check bits */
1959f988b79SJean-Baptiste Boric 		KASSERT(bitmap->bits);
1969f988b79SJean-Baptiste Boric 		ok = 1;
1979f988b79SJean-Baptiste Boric 		bpos = bitmap->bits + lb_num/8;
1989f988b79SJean-Baptiste Boric 		bit  = lb_num % 8;
1999f988b79SJean-Baptiste Boric 		while (num_lb > 0) {
2009f988b79SJean-Baptiste Boric 			bitval = (1 << bit);
2019f988b79SJean-Baptiste Boric 			DPRINTF(PARANOIA, ("XXX : check %d, %p, bit %d\n",
2029f988b79SJean-Baptiste Boric 				lb_num, bpos, bit));
2039f988b79SJean-Baptiste Boric 			KASSERT(bitmap->bits + lb_num/8 == bpos);
2049f988b79SJean-Baptiste Boric 			if (*bpos & bitval) {
2059f988b79SJean-Baptiste Boric 				printf("\tlb_num %d is NOT marked busy\n",
2069f988b79SJean-Baptiste Boric 					lb_num);
2079f988b79SJean-Baptiste Boric 				ok = 0;
2089f988b79SJean-Baptiste Boric 			}
2099f988b79SJean-Baptiste Boric 			lb_num++; num_lb--;
2109f988b79SJean-Baptiste Boric 			bit = (bit + 1) % 8;
2119f988b79SJean-Baptiste Boric 			if (bit == 0)
2129f988b79SJean-Baptiste Boric 				bpos++;
2139f988b79SJean-Baptiste Boric 		}
2149f988b79SJean-Baptiste Boric 		if (!ok) {
2159f988b79SJean-Baptiste Boric 			/* KASSERT(0); */
2169f988b79SJean-Baptiste Boric 		}
2179f988b79SJean-Baptiste Boric 
2189f988b79SJean-Baptiste Boric 		break;
2199f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_VIRT :
2209f988b79SJean-Baptiste Boric 		/* TODO check space */
2219f988b79SJean-Baptiste Boric 		KASSERT(num_lb == 1);
2229f988b79SJean-Baptiste Boric 		break;
2239f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_META :
2249f988b79SJean-Baptiste Boric 		/* TODO check space in the metadata bitmap */
2259f988b79SJean-Baptiste Boric 	default:
2269f988b79SJean-Baptiste Boric 		/* not implemented */
2279f988b79SJean-Baptiste Boric 		break;
2289f988b79SJean-Baptiste Boric 	}
2299f988b79SJean-Baptiste Boric }
2309f988b79SJean-Baptiste Boric 
2319f988b79SJean-Baptiste Boric 
2329f988b79SJean-Baptiste Boric static void
2339f988b79SJean-Baptiste Boric udf_node_sanity_check(struct udf_node *udf_node,
2349f988b79SJean-Baptiste Boric 		uint64_t *cnt_inflen, uint64_t *cnt_logblksrec)
2359f988b79SJean-Baptiste Boric {
2369f988b79SJean-Baptiste Boric 	union dscrptr *dscr;
2379f988b79SJean-Baptiste Boric 	struct file_entry    *fe;
2389f988b79SJean-Baptiste Boric 	struct extfile_entry *efe;
2399f988b79SJean-Baptiste Boric 	struct icb_tag *icbtag;
2409f988b79SJean-Baptiste Boric 	struct long_ad  s_ad;
2419f988b79SJean-Baptiste Boric 	uint64_t inflen, logblksrec;
2429f988b79SJean-Baptiste Boric 	uint32_t icbflags, addr_type;
2439f988b79SJean-Baptiste Boric 	uint32_t len, lb_num, l_ea, l_ad, max_l_ad;
2449f988b79SJean-Baptiste Boric 	uint16_t part_num;
2459f988b79SJean-Baptiste Boric 	uint8_t *data_pos;
2469f988b79SJean-Baptiste Boric 	int dscr_size, lb_size, flags, whole_lb;
2479f988b79SJean-Baptiste Boric 	int i, slot, eof;
2489f988b79SJean-Baptiste Boric 
2499f988b79SJean-Baptiste Boric //	KASSERT(mutex_owned(&udf_node->ump->allocate_mutex));
2509f988b79SJean-Baptiste Boric 
2519f988b79SJean-Baptiste Boric 	if (1)
2529f988b79SJean-Baptiste Boric 		udf_node_dump(udf_node);
2539f988b79SJean-Baptiste Boric 
2549f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
2559f988b79SJean-Baptiste Boric 
2569f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
2579f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
2589f988b79SJean-Baptiste Boric 	if (fe) {
2599f988b79SJean-Baptiste Boric 		dscr       = (union dscrptr *) fe;
2609f988b79SJean-Baptiste Boric 		icbtag     = &fe->icbtag;
2619f988b79SJean-Baptiste Boric 		inflen     = udf_rw64(fe->inf_len);
2629f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct file_entry) -1;
2639f988b79SJean-Baptiste Boric 		logblksrec = udf_rw64(fe->logblks_rec);
2649f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(fe->l_ad);
2659f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(fe->l_ea);
2669f988b79SJean-Baptiste Boric 	} else {
2679f988b79SJean-Baptiste Boric 		dscr       = (union dscrptr *) efe;
2689f988b79SJean-Baptiste Boric 		icbtag     = &efe->icbtag;
2699f988b79SJean-Baptiste Boric 		inflen     = udf_rw64(efe->inf_len);
2709f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct extfile_entry) -1;
2719f988b79SJean-Baptiste Boric 		logblksrec = udf_rw64(efe->logblks_rec);
2729f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(efe->l_ad);
2739f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(efe->l_ea);
2749f988b79SJean-Baptiste Boric 	}
2759f988b79SJean-Baptiste Boric 	data_pos  = (uint8_t *) dscr + dscr_size + l_ea;
2769f988b79SJean-Baptiste Boric 	max_l_ad   = lb_size - dscr_size - l_ea;
2779f988b79SJean-Baptiste Boric 	icbflags   = udf_rw16(icbtag->flags);
2789f988b79SJean-Baptiste Boric 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2799f988b79SJean-Baptiste Boric 
2809f988b79SJean-Baptiste Boric 	/* check if tail is zero */
2819f988b79SJean-Baptiste Boric 	DPRINTF(PARANOIA, ("Sanity check blank tail\n"));
2829f988b79SJean-Baptiste Boric 	for (i = l_ad; i < max_l_ad; i++) {
2839f988b79SJean-Baptiste Boric 		if (data_pos[i] != 0)
2849f988b79SJean-Baptiste Boric 			printf( "sanity_check: violation: node byte %d "
2859f988b79SJean-Baptiste Boric 				"has value %d\n", i, data_pos[i]);
2869f988b79SJean-Baptiste Boric 	}
2879f988b79SJean-Baptiste Boric 
2889f988b79SJean-Baptiste Boric 	/* reset counters */
2899f988b79SJean-Baptiste Boric 	*cnt_inflen     = 0;
2909f988b79SJean-Baptiste Boric 	*cnt_logblksrec = 0;
2919f988b79SJean-Baptiste Boric 
2929f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
2939f988b79SJean-Baptiste Boric 		KASSERT(l_ad <= max_l_ad);
2949f988b79SJean-Baptiste Boric 		KASSERT(l_ad == inflen);
2959f988b79SJean-Baptiste Boric 		*cnt_inflen = inflen;
2969f988b79SJean-Baptiste Boric 		return;
2979f988b79SJean-Baptiste Boric 	}
2989f988b79SJean-Baptiste Boric 
2999f988b79SJean-Baptiste Boric 	/* start counting */
3009f988b79SJean-Baptiste Boric 	whole_lb = 1;
3019f988b79SJean-Baptiste Boric 	slot = 0;
3029f988b79SJean-Baptiste Boric 	for (;;) {
3039f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
3049f988b79SJean-Baptiste Boric 		if (eof)
3059f988b79SJean-Baptiste Boric 			break;
3069f988b79SJean-Baptiste Boric 		KASSERT(whole_lb == 1);
3079f988b79SJean-Baptiste Boric 
3089f988b79SJean-Baptiste Boric 		part_num = udf_rw16(s_ad.loc.part_num);
3099f988b79SJean-Baptiste Boric 		lb_num = udf_rw32(s_ad.loc.lb_num);
3109f988b79SJean-Baptiste Boric 		len   = udf_rw32(s_ad.len);
3119f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(len);
3129f988b79SJean-Baptiste Boric 		len   = UDF_EXT_LEN(len);
3139f988b79SJean-Baptiste Boric 
3149f988b79SJean-Baptiste Boric 		if (flags != UDF_EXT_REDIRECT) {
3159f988b79SJean-Baptiste Boric 			*cnt_inflen += len;
3169f988b79SJean-Baptiste Boric 			if (flags == UDF_EXT_ALLOCATED) {
3179f988b79SJean-Baptiste Boric 				*cnt_logblksrec += (len + lb_size -1) / lb_size;
3189f988b79SJean-Baptiste Boric 			}
3199f988b79SJean-Baptiste Boric 		} else {
3209f988b79SJean-Baptiste Boric 			KASSERT(len == lb_size);
3219f988b79SJean-Baptiste Boric 		}
3229f988b79SJean-Baptiste Boric 		/* check allocation */
3239f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_ALLOCATED)
3249f988b79SJean-Baptiste Boric 			udf_assert_allocated(udf_node->ump, part_num, lb_num,
3259f988b79SJean-Baptiste Boric 				(len + lb_size - 1) / lb_size);
3269f988b79SJean-Baptiste Boric 
3279f988b79SJean-Baptiste Boric 		/* check whole lb */
3289f988b79SJean-Baptiste Boric 		whole_lb = ((len % lb_size) == 0);
3299f988b79SJean-Baptiste Boric 
3309f988b79SJean-Baptiste Boric 		slot++;
3319f988b79SJean-Baptiste Boric 	}
3329f988b79SJean-Baptiste Boric 	/* rest should be zero (ad_off > l_ad < max_l_ad - adlen) */
3339f988b79SJean-Baptiste Boric 
3349f988b79SJean-Baptiste Boric 	KASSERT(*cnt_inflen == inflen);
3359f988b79SJean-Baptiste Boric 	KASSERT(*cnt_logblksrec == logblksrec);
3369f988b79SJean-Baptiste Boric 
3379f988b79SJean-Baptiste Boric //	KASSERT(mutex_owned(&udf_node->ump->allocate_mutex));
3389f988b79SJean-Baptiste Boric }
3399f988b79SJean-Baptiste Boric #else
3409f988b79SJean-Baptiste Boric static void
udf_node_sanity_check(struct udf_node * udf_node,uint64_t * cnt_inflen,uint64_t * cnt_logblksrec)3419f988b79SJean-Baptiste Boric udf_node_sanity_check(struct udf_node *udf_node,
3429f988b79SJean-Baptiste Boric 		uint64_t *cnt_inflen, uint64_t *cnt_logblksrec) {
3439f988b79SJean-Baptiste Boric 	struct file_entry    *fe;
3449f988b79SJean-Baptiste Boric 	struct extfile_entry *efe;
3459f988b79SJean-Baptiste Boric 	uint64_t inflen, logblksrec;
3469f988b79SJean-Baptiste Boric 
3479f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
3489f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
3499f988b79SJean-Baptiste Boric 	if (fe) {
3509f988b79SJean-Baptiste Boric 		inflen = udf_rw64(fe->inf_len);
3519f988b79SJean-Baptiste Boric 		logblksrec = udf_rw64(fe->logblks_rec);
3529f988b79SJean-Baptiste Boric 	} else {
3539f988b79SJean-Baptiste Boric 		inflen = udf_rw64(efe->inf_len);
3549f988b79SJean-Baptiste Boric 		logblksrec = udf_rw64(efe->logblks_rec);
3559f988b79SJean-Baptiste Boric 	}
3569f988b79SJean-Baptiste Boric 	*cnt_logblksrec = logblksrec;
3579f988b79SJean-Baptiste Boric 	*cnt_inflen     = inflen;
3589f988b79SJean-Baptiste Boric }
3599f988b79SJean-Baptiste Boric #endif
3609f988b79SJean-Baptiste Boric 
3619f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
3629f988b79SJean-Baptiste Boric 
3639f988b79SJean-Baptiste Boric void
udf_calc_freespace(struct udf_mount * ump,uint64_t * sizeblks,uint64_t * freeblks)3649f988b79SJean-Baptiste Boric udf_calc_freespace(struct udf_mount *ump, uint64_t *sizeblks, uint64_t *freeblks)
3659f988b79SJean-Baptiste Boric {
3669f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvid;
3679f988b79SJean-Baptiste Boric 	uint32_t *pos1, *pos2;
3689f988b79SJean-Baptiste Boric 	int vpart, num_vpart;
3699f988b79SJean-Baptiste Boric 
3709f988b79SJean-Baptiste Boric 	lvid = ump->logvol_integrity;
3719f988b79SJean-Baptiste Boric 	*freeblks = *sizeblks = 0;
3729f988b79SJean-Baptiste Boric 
3739f988b79SJean-Baptiste Boric 	/*
3749f988b79SJean-Baptiste Boric 	 * Sequentials media report free space directly (CD/DVD/BD-R), for the
3759f988b79SJean-Baptiste Boric 	 * other media we need the logical volume integrity.
3769f988b79SJean-Baptiste Boric 	 *
3779f988b79SJean-Baptiste Boric 	 * We sum all free space up here regardless of type.
3789f988b79SJean-Baptiste Boric 	 */
3799f988b79SJean-Baptiste Boric 
3809f988b79SJean-Baptiste Boric 	KASSERT(lvid);
3819f988b79SJean-Baptiste Boric 	num_vpart = udf_rw32(lvid->num_part);
3829f988b79SJean-Baptiste Boric 
3839f988b79SJean-Baptiste Boric 	if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
3849f988b79SJean-Baptiste Boric 		/* use track info directly summing if there are 2 open */
3859f988b79SJean-Baptiste Boric 		/* XXX assumption at most two tracks open */
3869f988b79SJean-Baptiste Boric 		*freeblks = ump->data_track.free_blocks;
3879f988b79SJean-Baptiste Boric 		if (ump->data_track.tracknr != ump->metadata_track.tracknr)
3889f988b79SJean-Baptiste Boric 			*freeblks += ump->metadata_track.free_blocks;
3899f988b79SJean-Baptiste Boric 		*sizeblks = ump->discinfo.last_possible_lba;
3909f988b79SJean-Baptiste Boric 	} else {
3919f988b79SJean-Baptiste Boric 		/* free and used space for mountpoint based on logvol integrity */
3929f988b79SJean-Baptiste Boric 		for (vpart = 0; vpart < num_vpart; vpart++) {
3939f988b79SJean-Baptiste Boric 			pos1 = &lvid->tables[0] + vpart;
3949f988b79SJean-Baptiste Boric 			pos2 = &lvid->tables[0] + num_vpart + vpart;
3959f988b79SJean-Baptiste Boric 			if (udf_rw32(*pos1) != (uint32_t) -1) {
3969f988b79SJean-Baptiste Boric 				*freeblks += udf_rw32(*pos1);
3979f988b79SJean-Baptiste Boric 				*sizeblks += udf_rw32(*pos2);
3989f988b79SJean-Baptiste Boric 			}
3999f988b79SJean-Baptiste Boric 		}
4009f988b79SJean-Baptiste Boric 	}
4019f988b79SJean-Baptiste Boric 	/* adjust for accounted uncommitted blocks */
4029f988b79SJean-Baptiste Boric 	for (vpart = 0; vpart < num_vpart; vpart++)
4039f988b79SJean-Baptiste Boric 		*freeblks -= ump->uncommitted_lbs[vpart];
4049f988b79SJean-Baptiste Boric 
4059f988b79SJean-Baptiste Boric 	if (*freeblks > UDF_DISC_SLACK) {
4069f988b79SJean-Baptiste Boric 		*freeblks -= UDF_DISC_SLACK;
4079f988b79SJean-Baptiste Boric 	} else {
4089f988b79SJean-Baptiste Boric 		*freeblks = 0;
4099f988b79SJean-Baptiste Boric 	}
4109f988b79SJean-Baptiste Boric }
4119f988b79SJean-Baptiste Boric 
4129f988b79SJean-Baptiste Boric 
4139f988b79SJean-Baptiste Boric static void
udf_calc_vpart_freespace(struct udf_mount * ump,uint16_t vpart_num,uint64_t * freeblks)4149f988b79SJean-Baptiste Boric udf_calc_vpart_freespace(struct udf_mount *ump, uint16_t vpart_num, uint64_t *freeblks)
4159f988b79SJean-Baptiste Boric {
4169f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvid;
4179f988b79SJean-Baptiste Boric 	uint32_t *pos1;
4189f988b79SJean-Baptiste Boric 
4199f988b79SJean-Baptiste Boric 	lvid = ump->logvol_integrity;
4209f988b79SJean-Baptiste Boric 	*freeblks = 0;
4219f988b79SJean-Baptiste Boric 
4229f988b79SJean-Baptiste Boric 	/*
4239f988b79SJean-Baptiste Boric 	 * Sequentials media report free space directly (CD/DVD/BD-R), for the
4249f988b79SJean-Baptiste Boric 	 * other media we need the logical volume integrity.
4259f988b79SJean-Baptiste Boric 	 *
4269f988b79SJean-Baptiste Boric 	 * We sum all free space up here regardless of type.
4279f988b79SJean-Baptiste Boric 	 */
4289f988b79SJean-Baptiste Boric 
4299f988b79SJean-Baptiste Boric 	KASSERT(lvid);
4309f988b79SJean-Baptiste Boric 	if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
4319f988b79SJean-Baptiste Boric 		/* XXX assumption at most two tracks open */
4329f988b79SJean-Baptiste Boric 		if (vpart_num == ump->data_part) {
4339f988b79SJean-Baptiste Boric 			*freeblks = ump->data_track.free_blocks;
4349f988b79SJean-Baptiste Boric 		} else {
4359f988b79SJean-Baptiste Boric 			*freeblks = ump->metadata_track.free_blocks;
4369f988b79SJean-Baptiste Boric 		}
4379f988b79SJean-Baptiste Boric 	} else {
4389f988b79SJean-Baptiste Boric 		/* free and used space for mountpoint based on logvol integrity */
4399f988b79SJean-Baptiste Boric 		pos1 = &lvid->tables[0] + vpart_num;
4409f988b79SJean-Baptiste Boric 		if (udf_rw32(*pos1) != (uint32_t) -1)
4419f988b79SJean-Baptiste Boric 			*freeblks += udf_rw32(*pos1);
4429f988b79SJean-Baptiste Boric 	}
4439f988b79SJean-Baptiste Boric 
4449f988b79SJean-Baptiste Boric 	/* adjust for accounted uncommitted blocks */
4459f988b79SJean-Baptiste Boric 	if (*freeblks > ump->uncommitted_lbs[vpart_num]) {
4469f988b79SJean-Baptiste Boric 		*freeblks -= ump->uncommitted_lbs[vpart_num];
4479f988b79SJean-Baptiste Boric 	} else {
4489f988b79SJean-Baptiste Boric 		*freeblks = 0;
4499f988b79SJean-Baptiste Boric 	}
4509f988b79SJean-Baptiste Boric }
4519f988b79SJean-Baptiste Boric 
4529f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
4539f988b79SJean-Baptiste Boric 
4549f988b79SJean-Baptiste Boric int
udf_translate_vtop(struct udf_mount * ump,struct long_ad * icb_loc,uint32_t * lb_numres,uint32_t * extres)4559f988b79SJean-Baptiste Boric udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
4569f988b79SJean-Baptiste Boric 		   uint32_t *lb_numres, uint32_t *extres)
4579f988b79SJean-Baptiste Boric {
4589f988b79SJean-Baptiste Boric 	struct part_desc       *pdesc;
4599f988b79SJean-Baptiste Boric 	struct spare_map_entry *sme;
4609f988b79SJean-Baptiste Boric 	struct long_ad s_icb_loc;
4619f988b79SJean-Baptiste Boric 	uint64_t foffset, end_foffset;
4629f988b79SJean-Baptiste Boric 	uint32_t lb_size, len;
4639f988b79SJean-Baptiste Boric 	uint32_t lb_num, lb_rel, lb_packet;
4649f988b79SJean-Baptiste Boric 	uint32_t udf_rw32_lbmap, ext_offset;
4659f988b79SJean-Baptiste Boric 	uint16_t vpart;
4669f988b79SJean-Baptiste Boric 	int rel, part, error, eof, slot, flags;
4679f988b79SJean-Baptiste Boric 
4689f988b79SJean-Baptiste Boric 	assert(ump && icb_loc && lb_numres);
4699f988b79SJean-Baptiste Boric 
4709f988b79SJean-Baptiste Boric 	vpart  = udf_rw16(icb_loc->loc.part_num);
4719f988b79SJean-Baptiste Boric 	lb_num = udf_rw32(icb_loc->loc.lb_num);
4729f988b79SJean-Baptiste Boric 	if (vpart > UDF_VTOP_RAWPART)
4739f988b79SJean-Baptiste Boric 		return EINVAL;
4749f988b79SJean-Baptiste Boric 
4759f988b79SJean-Baptiste Boric translate_again:
4769f988b79SJean-Baptiste Boric 	part = ump->vtop[vpart];
4779f988b79SJean-Baptiste Boric 	pdesc = ump->partitions[part];
4789f988b79SJean-Baptiste Boric 
4799f988b79SJean-Baptiste Boric 	switch (ump->vtop_tp[vpart]) {
4809f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_RAW :
4819f988b79SJean-Baptiste Boric 		/* 1:1 to the end of the device */
4829f988b79SJean-Baptiste Boric 		*lb_numres = lb_num;
4839f988b79SJean-Baptiste Boric 		*extres = INT_MAX;
4849f988b79SJean-Baptiste Boric 		return 0;
4859f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_PHYS :
4869f988b79SJean-Baptiste Boric 		/* transform into its disc logical block */
4879f988b79SJean-Baptiste Boric 		if (lb_num > udf_rw32(pdesc->part_len))
4889f988b79SJean-Baptiste Boric 			return EINVAL;
4899f988b79SJean-Baptiste Boric 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
4909f988b79SJean-Baptiste Boric 
4919f988b79SJean-Baptiste Boric 		/* extent from here to the end of the partition */
4929f988b79SJean-Baptiste Boric 		*extres = udf_rw32(pdesc->part_len) - lb_num;
4939f988b79SJean-Baptiste Boric 		return 0;
4949f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_VIRT :
4959f988b79SJean-Baptiste Boric 		/* only maps one logical block, lookup in VAT */
4969f988b79SJean-Baptiste Boric 		if (lb_num >= ump->vat_entries)		/* XXX > or >= ? */
4979f988b79SJean-Baptiste Boric 			return EINVAL;
4989f988b79SJean-Baptiste Boric 
4999f988b79SJean-Baptiste Boric 		/* lookup in virtual allocation table file */
5009f988b79SJean-Baptiste Boric 		mutex_enter(&ump->allocate_mutex);
5019f988b79SJean-Baptiste Boric 		error = udf_vat_read(ump->vat_node,
5029f988b79SJean-Baptiste Boric 				(uint8_t *) &udf_rw32_lbmap, 4,
5039f988b79SJean-Baptiste Boric 				ump->vat_offset + lb_num * 4);
5049f988b79SJean-Baptiste Boric 		mutex_exit(&ump->allocate_mutex);
5059f988b79SJean-Baptiste Boric 
5069f988b79SJean-Baptiste Boric 		if (error)
5079f988b79SJean-Baptiste Boric 			return error;
5089f988b79SJean-Baptiste Boric 
5099f988b79SJean-Baptiste Boric 		lb_num = udf_rw32(udf_rw32_lbmap);
5109f988b79SJean-Baptiste Boric 
5119f988b79SJean-Baptiste Boric 		/* transform into its disc logical block */
5129f988b79SJean-Baptiste Boric 		if (lb_num > udf_rw32(pdesc->part_len))
5139f988b79SJean-Baptiste Boric 			return EINVAL;
5149f988b79SJean-Baptiste Boric 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
5159f988b79SJean-Baptiste Boric 
5169f988b79SJean-Baptiste Boric 		/* just one logical block */
5179f988b79SJean-Baptiste Boric 		*extres = 1;
5189f988b79SJean-Baptiste Boric 		return 0;
5199f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_SPARABLE :
5209f988b79SJean-Baptiste Boric 		/* check if the packet containing the lb_num is remapped */
5219f988b79SJean-Baptiste Boric 		lb_packet = lb_num / ump->sparable_packet_size;
5229f988b79SJean-Baptiste Boric 		lb_rel    = lb_num % ump->sparable_packet_size;
5239f988b79SJean-Baptiste Boric 
5249f988b79SJean-Baptiste Boric 		for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
5259f988b79SJean-Baptiste Boric 			sme = &ump->sparing_table->entries[rel];
5269f988b79SJean-Baptiste Boric 			if (lb_packet == udf_rw32(sme->org)) {
5279f988b79SJean-Baptiste Boric 				/* NOTE maps to absolute disc logical block! */
5289f988b79SJean-Baptiste Boric 				*lb_numres = udf_rw32(sme->map) + lb_rel;
5299f988b79SJean-Baptiste Boric 				*extres    = ump->sparable_packet_size - lb_rel;
5309f988b79SJean-Baptiste Boric 				return 0;
5319f988b79SJean-Baptiste Boric 			}
5329f988b79SJean-Baptiste Boric 		}
5339f988b79SJean-Baptiste Boric 
5349f988b79SJean-Baptiste Boric 		/* transform into its disc logical block */
5359f988b79SJean-Baptiste Boric 		if (lb_num > udf_rw32(pdesc->part_len))
5369f988b79SJean-Baptiste Boric 			return EINVAL;
5379f988b79SJean-Baptiste Boric 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
5389f988b79SJean-Baptiste Boric 
5399f988b79SJean-Baptiste Boric 		/* rest of block */
5409f988b79SJean-Baptiste Boric 		*extres = ump->sparable_packet_size - lb_rel;
5419f988b79SJean-Baptiste Boric 		return 0;
5429f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_META :
5439f988b79SJean-Baptiste Boric 		/* we have to look into the file's allocation descriptors */
5449f988b79SJean-Baptiste Boric 
5459f988b79SJean-Baptiste Boric 		/* use metadatafile allocation mutex */
5469f988b79SJean-Baptiste Boric 		lb_size = udf_rw32(ump->logical_vol->lb_size);
5479f988b79SJean-Baptiste Boric 
5489f988b79SJean-Baptiste Boric 		UDF_LOCK_NODE(ump->metadata_node, 0);
5499f988b79SJean-Baptiste Boric 
5509f988b79SJean-Baptiste Boric 		/* get first overlapping extent */
5519f988b79SJean-Baptiste Boric 		foffset = 0;
5529f988b79SJean-Baptiste Boric 		slot    = 0;
5539f988b79SJean-Baptiste Boric 		for (;;) {
5549f988b79SJean-Baptiste Boric 			udf_get_adslot(ump->metadata_node,
5559f988b79SJean-Baptiste Boric 				slot, &s_icb_loc, &eof);
5569f988b79SJean-Baptiste Boric 			DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, "
5579f988b79SJean-Baptiste Boric 				"len = %d, lb_num = %d, part = %d\n",
5589f988b79SJean-Baptiste Boric 				slot, eof,
5599f988b79SJean-Baptiste Boric 				UDF_EXT_FLAGS(udf_rw32(s_icb_loc.len)),
5609f988b79SJean-Baptiste Boric 				UDF_EXT_LEN(udf_rw32(s_icb_loc.len)),
5619f988b79SJean-Baptiste Boric 				udf_rw32(s_icb_loc.loc.lb_num),
5629f988b79SJean-Baptiste Boric 				udf_rw16(s_icb_loc.loc.part_num)));
5639f988b79SJean-Baptiste Boric 			if (eof) {
5649f988b79SJean-Baptiste Boric 				DPRINTF(TRANSLATE,
5659f988b79SJean-Baptiste Boric 					("Meta partition translation "
5669f988b79SJean-Baptiste Boric 					 "failed: can't seek location\n"));
5679f988b79SJean-Baptiste Boric 				UDF_UNLOCK_NODE(ump->metadata_node, 0);
5689f988b79SJean-Baptiste Boric 				return EINVAL;
5699f988b79SJean-Baptiste Boric 			}
5709f988b79SJean-Baptiste Boric 			len   = udf_rw32(s_icb_loc.len);
5719f988b79SJean-Baptiste Boric 			flags = UDF_EXT_FLAGS(len);
5729f988b79SJean-Baptiste Boric 			len   = UDF_EXT_LEN(len);
5739f988b79SJean-Baptiste Boric 
5749f988b79SJean-Baptiste Boric 			if (flags == UDF_EXT_REDIRECT) {
5759f988b79SJean-Baptiste Boric 				slot++;
5769f988b79SJean-Baptiste Boric 				continue;
5779f988b79SJean-Baptiste Boric 			}
5789f988b79SJean-Baptiste Boric 
5799f988b79SJean-Baptiste Boric 			end_foffset = foffset + len;
5809f988b79SJean-Baptiste Boric 
5819f988b79SJean-Baptiste Boric 			if (end_foffset > (uint64_t) lb_num * lb_size)
5829f988b79SJean-Baptiste Boric 				break;	/* found */
5839f988b79SJean-Baptiste Boric 			foffset = end_foffset;
5849f988b79SJean-Baptiste Boric 			slot++;
5859f988b79SJean-Baptiste Boric 		}
5869f988b79SJean-Baptiste Boric 		/* found overlapping slot */
5879f988b79SJean-Baptiste Boric 		ext_offset = lb_num * lb_size - foffset;
5889f988b79SJean-Baptiste Boric 
5899f988b79SJean-Baptiste Boric 		/* process extent offset */
5909f988b79SJean-Baptiste Boric 		lb_num   = udf_rw32(s_icb_loc.loc.lb_num);
5919f988b79SJean-Baptiste Boric 		vpart    = udf_rw16(s_icb_loc.loc.part_num);
5929f988b79SJean-Baptiste Boric 		lb_num  += (ext_offset + lb_size -1) / lb_size;
5939f988b79SJean-Baptiste Boric 		ext_offset = 0;
5949f988b79SJean-Baptiste Boric 
5959f988b79SJean-Baptiste Boric 		UDF_UNLOCK_NODE(ump->metadata_node, 0);
5969f988b79SJean-Baptiste Boric 		if (flags != UDF_EXT_ALLOCATED) {
5979f988b79SJean-Baptiste Boric 			DPRINTF(TRANSLATE, ("Metadata partition translation "
5989f988b79SJean-Baptiste Boric 					    "failed: not allocated\n"));
5999f988b79SJean-Baptiste Boric 			return EINVAL;
6009f988b79SJean-Baptiste Boric 		}
6019f988b79SJean-Baptiste Boric 
6029f988b79SJean-Baptiste Boric 		/*
6039f988b79SJean-Baptiste Boric 		 * vpart and lb_num are updated, translate again since we
6049f988b79SJean-Baptiste Boric 		 * might be mapped on sparable media
6059f988b79SJean-Baptiste Boric 		 */
6069f988b79SJean-Baptiste Boric 		goto translate_again;
6079f988b79SJean-Baptiste Boric 	default:
6089f988b79SJean-Baptiste Boric 		printf("UDF vtop translation scheme %d unimplemented yet\n",
6099f988b79SJean-Baptiste Boric 			ump->vtop_tp[vpart]);
6109f988b79SJean-Baptiste Boric 	}
6119f988b79SJean-Baptiste Boric 
6129f988b79SJean-Baptiste Boric 	return EINVAL;
6139f988b79SJean-Baptiste Boric }
6149f988b79SJean-Baptiste Boric 
6159f988b79SJean-Baptiste Boric 
6169f988b79SJean-Baptiste Boric /* XXX  provisional primitive braindead version */
6179f988b79SJean-Baptiste Boric /* TODO use ext_res */
6189f988b79SJean-Baptiste Boric void
udf_translate_vtop_list(struct udf_mount * ump,uint32_t sectors,uint16_t vpart_num,uint64_t * lmapping,uint64_t * pmapping)6199f988b79SJean-Baptiste Boric udf_translate_vtop_list(struct udf_mount *ump, uint32_t sectors,
6209f988b79SJean-Baptiste Boric 	uint16_t vpart_num, uint64_t *lmapping, uint64_t *pmapping)
6219f988b79SJean-Baptiste Boric {
6229f988b79SJean-Baptiste Boric 	struct long_ad loc;
6239f988b79SJean-Baptiste Boric 	uint32_t lb_numres, ext_res;
6249f988b79SJean-Baptiste Boric 	int sector;
6259f988b79SJean-Baptiste Boric 
6269f988b79SJean-Baptiste Boric 	for (sector = 0; sector < sectors; sector++) {
6279f988b79SJean-Baptiste Boric 		memset(&loc, 0, sizeof(struct long_ad));
6289f988b79SJean-Baptiste Boric 		loc.loc.part_num = udf_rw16(vpart_num);
6299f988b79SJean-Baptiste Boric 		loc.loc.lb_num   = udf_rw32(*lmapping);
6309f988b79SJean-Baptiste Boric 		udf_translate_vtop(ump, &loc, &lb_numres, &ext_res);
6319f988b79SJean-Baptiste Boric 		*pmapping = lb_numres;
6329f988b79SJean-Baptiste Boric 		lmapping++; pmapping++;
6339f988b79SJean-Baptiste Boric 	}
6349f988b79SJean-Baptiste Boric }
6359f988b79SJean-Baptiste Boric 
6369f988b79SJean-Baptiste Boric 
6379f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
6389f988b79SJean-Baptiste Boric 
6399f988b79SJean-Baptiste Boric /*
6409f988b79SJean-Baptiste Boric  * Translate an extent (in logical_blocks) into logical block numbers; used
6419f988b79SJean-Baptiste Boric  * for read and write operations. DOESNT't check extents.
6429f988b79SJean-Baptiste Boric  */
6439f988b79SJean-Baptiste Boric 
6449f988b79SJean-Baptiste Boric int
udf_translate_file_extent(struct udf_node * udf_node,uint32_t from,uint32_t num_lb,uint64_t * map)6459f988b79SJean-Baptiste Boric udf_translate_file_extent(struct udf_node *udf_node,
6469f988b79SJean-Baptiste Boric 		          uint32_t from, uint32_t num_lb,
6479f988b79SJean-Baptiste Boric 			  uint64_t *map)
6489f988b79SJean-Baptiste Boric {
6499f988b79SJean-Baptiste Boric 	struct udf_mount *ump;
6509f988b79SJean-Baptiste Boric 	struct icb_tag *icbtag;
6519f988b79SJean-Baptiste Boric 	struct long_ad t_ad, s_ad;
6529f988b79SJean-Baptiste Boric 	uint64_t transsec;
6539f988b79SJean-Baptiste Boric 	uint64_t foffset, end_foffset;
6549f988b79SJean-Baptiste Boric 	uint32_t transsec32;
6559f988b79SJean-Baptiste Boric 	uint32_t lb_size;
6569f988b79SJean-Baptiste Boric 	uint32_t ext_offset;
6579f988b79SJean-Baptiste Boric 	uint32_t lb_num, len;
6589f988b79SJean-Baptiste Boric 	uint32_t overlap, translen;
6599f988b79SJean-Baptiste Boric 	uint16_t vpart_num;
6609f988b79SJean-Baptiste Boric 	int eof, error, flags;
6619f988b79SJean-Baptiste Boric 	int slot, addr_type, icbflags;
6629f988b79SJean-Baptiste Boric 
6639f988b79SJean-Baptiste Boric 	if (!udf_node)
6649f988b79SJean-Baptiste Boric 		return ENOENT;
6659f988b79SJean-Baptiste Boric 
6669f988b79SJean-Baptiste Boric 	KASSERT(num_lb > 0);
6679f988b79SJean-Baptiste Boric 
6689f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
6699f988b79SJean-Baptiste Boric 
6709f988b79SJean-Baptiste Boric 	/* initialise derivative vars */
6719f988b79SJean-Baptiste Boric 	ump = udf_node->ump;
6729f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
6739f988b79SJean-Baptiste Boric 
6749f988b79SJean-Baptiste Boric 	if (udf_node->fe) {
6759f988b79SJean-Baptiste Boric 		icbtag = &udf_node->fe->icbtag;
6769f988b79SJean-Baptiste Boric 	} else {
6779f988b79SJean-Baptiste Boric 		icbtag = &udf_node->efe->icbtag;
6789f988b79SJean-Baptiste Boric 	}
6799f988b79SJean-Baptiste Boric 	icbflags  = udf_rw16(icbtag->flags);
6809f988b79SJean-Baptiste Boric 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
6819f988b79SJean-Baptiste Boric 
6829f988b79SJean-Baptiste Boric 	/* do the work */
6839f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
6849f988b79SJean-Baptiste Boric 		*map = UDF_TRANS_INTERN;
6859f988b79SJean-Baptiste Boric 		UDF_UNLOCK_NODE(udf_node, 0);
6869f988b79SJean-Baptiste Boric 		return 0;
6879f988b79SJean-Baptiste Boric 	}
6889f988b79SJean-Baptiste Boric 
6899f988b79SJean-Baptiste Boric 	/* find first overlapping extent */
6909f988b79SJean-Baptiste Boric 	foffset = 0;
6919f988b79SJean-Baptiste Boric 	slot    = 0;
6929f988b79SJean-Baptiste Boric 	for (;;) {
6939f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
6949f988b79SJean-Baptiste Boric 		DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
6959f988b79SJean-Baptiste Boric 			"lb_num = %d, part = %d\n", slot, eof,
6969f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
6979f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
6989f988b79SJean-Baptiste Boric 			udf_rw32(s_ad.loc.lb_num),
6999f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num)));
7009f988b79SJean-Baptiste Boric 		if (eof) {
7019f988b79SJean-Baptiste Boric 			DPRINTF(TRANSLATE,
7029f988b79SJean-Baptiste Boric 				("Translate file extent "
7039f988b79SJean-Baptiste Boric 				 "failed: can't seek location\n"));
7049f988b79SJean-Baptiste Boric 			UDF_UNLOCK_NODE(udf_node, 0);
7059f988b79SJean-Baptiste Boric 			return EINVAL;
7069f988b79SJean-Baptiste Boric 		}
7079f988b79SJean-Baptiste Boric 		len    = udf_rw32(s_ad.len);
7089f988b79SJean-Baptiste Boric 		flags  = UDF_EXT_FLAGS(len);
7099f988b79SJean-Baptiste Boric 		len    = UDF_EXT_LEN(len);
7109f988b79SJean-Baptiste Boric 		lb_num = udf_rw32(s_ad.loc.lb_num);
7119f988b79SJean-Baptiste Boric 
7129f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
7139f988b79SJean-Baptiste Boric 			slot++;
7149f988b79SJean-Baptiste Boric 			continue;
7159f988b79SJean-Baptiste Boric 		}
7169f988b79SJean-Baptiste Boric 
7179f988b79SJean-Baptiste Boric 		end_foffset = foffset + len;
7189f988b79SJean-Baptiste Boric 
7199f988b79SJean-Baptiste Boric 		if (end_foffset > (uint64_t) from * lb_size)
7209f988b79SJean-Baptiste Boric 			break;	/* found */
7219f988b79SJean-Baptiste Boric 		foffset = end_foffset;
7229f988b79SJean-Baptiste Boric 		slot++;
7239f988b79SJean-Baptiste Boric 	}
7249f988b79SJean-Baptiste Boric 	/* found overlapping slot */
7259f988b79SJean-Baptiste Boric 	ext_offset = (uint64_t) from * lb_size - foffset;
7269f988b79SJean-Baptiste Boric 
7279f988b79SJean-Baptiste Boric 	for (;;) {
7289f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
7299f988b79SJean-Baptiste Boric 		DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
7309f988b79SJean-Baptiste Boric 			"lb_num = %d, part = %d\n", slot, eof,
7319f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
7329f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
7339f988b79SJean-Baptiste Boric 			udf_rw32(s_ad.loc.lb_num),
7349f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num)));
7359f988b79SJean-Baptiste Boric 		if (eof) {
7369f988b79SJean-Baptiste Boric 			DPRINTF(TRANSLATE,
7379f988b79SJean-Baptiste Boric 				("Translate file extent "
7389f988b79SJean-Baptiste Boric 				 "failed: past eof\n"));
7399f988b79SJean-Baptiste Boric 			UDF_UNLOCK_NODE(udf_node, 0);
7409f988b79SJean-Baptiste Boric 			return EINVAL;
7419f988b79SJean-Baptiste Boric 		}
7429f988b79SJean-Baptiste Boric 
7439f988b79SJean-Baptiste Boric 		len    = udf_rw32(s_ad.len);
7449f988b79SJean-Baptiste Boric 		flags  = UDF_EXT_FLAGS(len);
7459f988b79SJean-Baptiste Boric 		len    = UDF_EXT_LEN(len);
7469f988b79SJean-Baptiste Boric 
7479f988b79SJean-Baptiste Boric 		lb_num    = udf_rw32(s_ad.loc.lb_num);
7489f988b79SJean-Baptiste Boric 		vpart_num = udf_rw16(s_ad.loc.part_num);
7499f988b79SJean-Baptiste Boric 
7509f988b79SJean-Baptiste Boric 		end_foffset = foffset + len;
7519f988b79SJean-Baptiste Boric 
7529f988b79SJean-Baptiste Boric 		/* process extent, don't forget to advance on ext_offset! */
7539f988b79SJean-Baptiste Boric 		lb_num  += (ext_offset + lb_size -1) / lb_size;
7549f988b79SJean-Baptiste Boric 		overlap  = (len - ext_offset + lb_size -1) / lb_size;
7559f988b79SJean-Baptiste Boric 		ext_offset = 0;
7569f988b79SJean-Baptiste Boric 
7579f988b79SJean-Baptiste Boric 		/*
7589f988b79SJean-Baptiste Boric 		 * note that the while(){} is nessisary for the extent that
7599f988b79SJean-Baptiste Boric 		 * the udf_translate_vtop() returns doens't have to span the
7609f988b79SJean-Baptiste Boric 		 * whole extent.
7619f988b79SJean-Baptiste Boric 		 */
7629f988b79SJean-Baptiste Boric 
7639f988b79SJean-Baptiste Boric 		overlap = MIN(overlap, num_lb);
7649f988b79SJean-Baptiste Boric 		while (overlap && (flags != UDF_EXT_REDIRECT)) {
7659f988b79SJean-Baptiste Boric 			switch (flags) {
7669f988b79SJean-Baptiste Boric 			case UDF_EXT_FREE :
7679f988b79SJean-Baptiste Boric 			case UDF_EXT_ALLOCATED_BUT_NOT_USED :
7689f988b79SJean-Baptiste Boric 				transsec = UDF_TRANS_ZERO;
7699f988b79SJean-Baptiste Boric 				translen = overlap;
7709f988b79SJean-Baptiste Boric 				while (overlap && num_lb && translen) {
7719f988b79SJean-Baptiste Boric 					*map++ = transsec;
7729f988b79SJean-Baptiste Boric 					lb_num++;
7739f988b79SJean-Baptiste Boric 					overlap--; num_lb--; translen--;
7749f988b79SJean-Baptiste Boric 				}
7759f988b79SJean-Baptiste Boric 				break;
7769f988b79SJean-Baptiste Boric 			case UDF_EXT_ALLOCATED :
7779f988b79SJean-Baptiste Boric 				t_ad.loc.lb_num   = udf_rw32(lb_num);
7789f988b79SJean-Baptiste Boric 				t_ad.loc.part_num = udf_rw16(vpart_num);
7799f988b79SJean-Baptiste Boric 				error = udf_translate_vtop(ump,
7809f988b79SJean-Baptiste Boric 						&t_ad, &transsec32, &translen);
7819f988b79SJean-Baptiste Boric 				transsec = transsec32;
7829f988b79SJean-Baptiste Boric 				if (error) {
7839f988b79SJean-Baptiste Boric 					UDF_UNLOCK_NODE(udf_node, 0);
7849f988b79SJean-Baptiste Boric 					return error;
7859f988b79SJean-Baptiste Boric 				}
7869f988b79SJean-Baptiste Boric 				while (overlap && num_lb && translen) {
7879f988b79SJean-Baptiste Boric 					*map++ = transsec;
7889f988b79SJean-Baptiste Boric 					lb_num++; transsec++;
7899f988b79SJean-Baptiste Boric 					overlap--; num_lb--; translen--;
7909f988b79SJean-Baptiste Boric 				}
7919f988b79SJean-Baptiste Boric 				break;
7929f988b79SJean-Baptiste Boric 			default:
7939f988b79SJean-Baptiste Boric 				DPRINTF(TRANSLATE,
7949f988b79SJean-Baptiste Boric 					("Translate file extent "
7959f988b79SJean-Baptiste Boric 					 "failed: bad flags %x\n", flags));
7969f988b79SJean-Baptiste Boric 				UDF_UNLOCK_NODE(udf_node, 0);
7979f988b79SJean-Baptiste Boric 				return EINVAL;
7989f988b79SJean-Baptiste Boric 			}
7999f988b79SJean-Baptiste Boric 		}
8009f988b79SJean-Baptiste Boric 		if (num_lb == 0)
8019f988b79SJean-Baptiste Boric 			break;
8029f988b79SJean-Baptiste Boric 
8039f988b79SJean-Baptiste Boric 		if (flags != UDF_EXT_REDIRECT)
8049f988b79SJean-Baptiste Boric 			foffset = end_foffset;
8059f988b79SJean-Baptiste Boric 		slot++;
8069f988b79SJean-Baptiste Boric 	}
8079f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
8089f988b79SJean-Baptiste Boric 
8099f988b79SJean-Baptiste Boric 	return 0;
8109f988b79SJean-Baptiste Boric }
8119f988b79SJean-Baptiste Boric 
8129f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
8139f988b79SJean-Baptiste Boric 
8149f988b79SJean-Baptiste Boric static int
udf_search_free_vatloc(struct udf_mount * ump,uint32_t * lbnumres)8159f988b79SJean-Baptiste Boric udf_search_free_vatloc(struct udf_mount *ump, uint32_t *lbnumres)
8169f988b79SJean-Baptiste Boric {
8179f988b79SJean-Baptiste Boric 	uint32_t lb_size, lb_num, lb_map, udf_rw32_lbmap;
8189f988b79SJean-Baptiste Boric 	uint8_t *blob;
8199f988b79SJean-Baptiste Boric 	int entry, chunk, found, error;
8209f988b79SJean-Baptiste Boric 
8219f988b79SJean-Baptiste Boric 	KASSERT(ump);
8229f988b79SJean-Baptiste Boric 	KASSERT(ump->logical_vol);
8239f988b79SJean-Baptiste Boric 
8249f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
8259f988b79SJean-Baptiste Boric 	blob = malloc(lb_size, M_UDFTEMP, M_WAITOK);
8269f988b79SJean-Baptiste Boric 
8279f988b79SJean-Baptiste Boric 	/* TODO static allocation of search chunk */
8289f988b79SJean-Baptiste Boric 
8299f988b79SJean-Baptiste Boric 	lb_num = MIN(ump->vat_entries, ump->vat_last_free_lb);
8309f988b79SJean-Baptiste Boric 	found  = 0;
8319f988b79SJean-Baptiste Boric 	error  = 0;
8329f988b79SJean-Baptiste Boric 	entry  = 0;
8339f988b79SJean-Baptiste Boric 	do {
8349f988b79SJean-Baptiste Boric 		chunk = MIN(lb_size, (ump->vat_entries - lb_num) * 4);
8359f988b79SJean-Baptiste Boric 		if (chunk <= 0)
8369f988b79SJean-Baptiste Boric 			break;
8379f988b79SJean-Baptiste Boric 		/* load in chunk */
8389f988b79SJean-Baptiste Boric 		error = udf_vat_read(ump->vat_node, blob, chunk,
8399f988b79SJean-Baptiste Boric 				ump->vat_offset + lb_num * 4);
8409f988b79SJean-Baptiste Boric 
8419f988b79SJean-Baptiste Boric 		if (error)
8429f988b79SJean-Baptiste Boric 			break;
8439f988b79SJean-Baptiste Boric 
8449f988b79SJean-Baptiste Boric 		/* search this chunk */
8459f988b79SJean-Baptiste Boric 		for (entry=0; entry < chunk /4; entry++, lb_num++) {
8469f988b79SJean-Baptiste Boric 			udf_rw32_lbmap = *((uint32_t *) (blob + entry * 4));
8479f988b79SJean-Baptiste Boric 			lb_map = udf_rw32(udf_rw32_lbmap);
8489f988b79SJean-Baptiste Boric 			if (lb_map == 0xffffffff) {
8499f988b79SJean-Baptiste Boric 				found = 1;
8509f988b79SJean-Baptiste Boric 				break;
8519f988b79SJean-Baptiste Boric 			}
8529f988b79SJean-Baptiste Boric 		}
8539f988b79SJean-Baptiste Boric 	} while (!found);
8549f988b79SJean-Baptiste Boric 	if (error) {
8559f988b79SJean-Baptiste Boric 		printf("udf_search_free_vatloc: error reading in vat chunk "
8569f988b79SJean-Baptiste Boric 			"(lb %d, size %d)\n", lb_num, chunk);
8579f988b79SJean-Baptiste Boric 	}
8589f988b79SJean-Baptiste Boric 
8599f988b79SJean-Baptiste Boric 	if (!found) {
8609f988b79SJean-Baptiste Boric 		/* extend VAT */
8619f988b79SJean-Baptiste Boric 		DPRINTF(WRITE, ("udf_search_free_vatloc: extending\n"));
8629f988b79SJean-Baptiste Boric 		lb_num = ump->vat_entries;
8639f988b79SJean-Baptiste Boric 		ump->vat_entries++;
8649f988b79SJean-Baptiste Boric 	}
8659f988b79SJean-Baptiste Boric 
8669f988b79SJean-Baptiste Boric 	/* mark entry with initialiser just in case */
8679f988b79SJean-Baptiste Boric 	lb_map = udf_rw32(0xfffffffe);
8689f988b79SJean-Baptiste Boric 	udf_vat_write(ump->vat_node, (uint8_t *) &lb_map, 4,
8699f988b79SJean-Baptiste Boric 		ump->vat_offset + lb_num *4);
8709f988b79SJean-Baptiste Boric 	ump->vat_last_free_lb = lb_num;
8719f988b79SJean-Baptiste Boric 
8729f988b79SJean-Baptiste Boric 	free(blob, M_UDFTEMP);
8739f988b79SJean-Baptiste Boric 	*lbnumres = lb_num;
8749f988b79SJean-Baptiste Boric 	return 0;
8759f988b79SJean-Baptiste Boric }
8769f988b79SJean-Baptiste Boric 
8779f988b79SJean-Baptiste Boric 
8789f988b79SJean-Baptiste Boric static void
udf_bitmap_allocate(struct udf_bitmap * bitmap,int ismetadata,uint32_t * num_lb,uint64_t * lmappos)8799f988b79SJean-Baptiste Boric udf_bitmap_allocate(struct udf_bitmap *bitmap, int ismetadata,
8809f988b79SJean-Baptiste Boric 	uint32_t *num_lb, uint64_t *lmappos)
8819f988b79SJean-Baptiste Boric {
8829f988b79SJean-Baptiste Boric 	uint32_t offset, lb_num, bit;
8839f988b79SJean-Baptiste Boric 	int32_t  diff;
8849f988b79SJean-Baptiste Boric 	uint8_t *bpos;
8859f988b79SJean-Baptiste Boric 	int pass;
8869f988b79SJean-Baptiste Boric 
8879f988b79SJean-Baptiste Boric 	if (!ismetadata) {
8889f988b79SJean-Baptiste Boric 		/* heuristic to keep the two pointers not too close */
8899f988b79SJean-Baptiste Boric 		diff = bitmap->data_pos - bitmap->metadata_pos;
8909f988b79SJean-Baptiste Boric 		if ((diff >= 0) && (diff < 1024))
8919f988b79SJean-Baptiste Boric 			bitmap->data_pos = bitmap->metadata_pos + 1024;
8929f988b79SJean-Baptiste Boric 	}
8939f988b79SJean-Baptiste Boric 	offset = ismetadata ? bitmap->metadata_pos : bitmap->data_pos;
8949f988b79SJean-Baptiste Boric 	offset &= ~7;
8959f988b79SJean-Baptiste Boric 	for (pass = 0; pass < 2; pass++) {
8969f988b79SJean-Baptiste Boric 		if (offset >= bitmap->max_offset)
8979f988b79SJean-Baptiste Boric 			offset = 0;
8989f988b79SJean-Baptiste Boric 
8999f988b79SJean-Baptiste Boric 		while (offset < bitmap->max_offset) {
9009f988b79SJean-Baptiste Boric 			if (*num_lb == 0)
9019f988b79SJean-Baptiste Boric 				break;
9029f988b79SJean-Baptiste Boric 
9039f988b79SJean-Baptiste Boric 			/* use first bit not set */
9049f988b79SJean-Baptiste Boric 			bpos  = bitmap->bits + offset/8;
9059f988b79SJean-Baptiste Boric 			bit = ffs(*bpos);	/* returns 0 or 1..8 */
9069f988b79SJean-Baptiste Boric 			if (bit == 0) {
9079f988b79SJean-Baptiste Boric 				offset += 8;
9089f988b79SJean-Baptiste Boric 				continue;
9099f988b79SJean-Baptiste Boric 			}
9109f988b79SJean-Baptiste Boric 
9119f988b79SJean-Baptiste Boric 			/* check for ffs overshoot */
9129f988b79SJean-Baptiste Boric 			if (offset + bit-1 >= bitmap->max_offset) {
9139f988b79SJean-Baptiste Boric 				offset = bitmap->max_offset;
9149f988b79SJean-Baptiste Boric 				break;
9159f988b79SJean-Baptiste Boric 			}
9169f988b79SJean-Baptiste Boric 
9179f988b79SJean-Baptiste Boric 			DPRINTF(PARANOIA, ("XXX : allocate %d, %p, bit %d\n",
9189f988b79SJean-Baptiste Boric 				offset + bit -1, bpos, bit-1));
9199f988b79SJean-Baptiste Boric 			*bpos &= ~(1 << (bit-1));
9209f988b79SJean-Baptiste Boric 			lb_num = offset + bit-1;
9219f988b79SJean-Baptiste Boric 			*lmappos++ = lb_num;
9229f988b79SJean-Baptiste Boric 			*num_lb = *num_lb - 1;
9239f988b79SJean-Baptiste Boric 			// offset = (offset & ~7);
9249f988b79SJean-Baptiste Boric 		}
9259f988b79SJean-Baptiste Boric 	}
9269f988b79SJean-Baptiste Boric 
9279f988b79SJean-Baptiste Boric 	if (ismetadata) {
9289f988b79SJean-Baptiste Boric 		bitmap->metadata_pos = offset;
9299f988b79SJean-Baptiste Boric 	} else {
9309f988b79SJean-Baptiste Boric 		bitmap->data_pos = offset;
9319f988b79SJean-Baptiste Boric 	}
9329f988b79SJean-Baptiste Boric }
9339f988b79SJean-Baptiste Boric 
9349f988b79SJean-Baptiste Boric 
9359f988b79SJean-Baptiste Boric static void
udf_bitmap_free(struct udf_bitmap * bitmap,uint32_t lb_num,uint32_t num_lb)9369f988b79SJean-Baptiste Boric udf_bitmap_free(struct udf_bitmap *bitmap, uint32_t lb_num, uint32_t num_lb)
9379f988b79SJean-Baptiste Boric {
9389f988b79SJean-Baptiste Boric 	uint32_t offset;
9399f988b79SJean-Baptiste Boric 	uint32_t bit, bitval;
9409f988b79SJean-Baptiste Boric 	uint8_t *bpos;
9419f988b79SJean-Baptiste Boric 
9429f988b79SJean-Baptiste Boric 	offset = lb_num;
9439f988b79SJean-Baptiste Boric 
9449f988b79SJean-Baptiste Boric 	/* starter bits */
9459f988b79SJean-Baptiste Boric 	bpos = bitmap->bits + offset/8;
9469f988b79SJean-Baptiste Boric 	bit = offset % 8;
9479f988b79SJean-Baptiste Boric 	while ((bit != 0) && (num_lb > 0)) {
9489f988b79SJean-Baptiste Boric 		bitval = (1 << bit);
9499f988b79SJean-Baptiste Boric 		KASSERT((*bpos & bitval) == 0);
9509f988b79SJean-Baptiste Boric 		DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n",
9519f988b79SJean-Baptiste Boric 			offset, bpos, bit));
9529f988b79SJean-Baptiste Boric 		*bpos |= bitval;
9539f988b79SJean-Baptiste Boric 		offset++; num_lb--;
9549f988b79SJean-Baptiste Boric 		bit = (bit + 1) % 8;
9559f988b79SJean-Baptiste Boric 	}
9569f988b79SJean-Baptiste Boric 	if (num_lb == 0)
9579f988b79SJean-Baptiste Boric 		return;
9589f988b79SJean-Baptiste Boric 
9599f988b79SJean-Baptiste Boric 	/* whole bytes */
9609f988b79SJean-Baptiste Boric 	KASSERT(bit == 0);
9619f988b79SJean-Baptiste Boric 	bpos = bitmap->bits + offset / 8;
9629f988b79SJean-Baptiste Boric 	while (num_lb >= 8) {
9639f988b79SJean-Baptiste Boric 		KASSERT((*bpos == 0));
9649f988b79SJean-Baptiste Boric 		DPRINTF(PARANOIA, ("XXX : free %d + 8, %p\n", offset, bpos));
9659f988b79SJean-Baptiste Boric 		*bpos = 255;
9669f988b79SJean-Baptiste Boric 		offset += 8; num_lb -= 8;
9679f988b79SJean-Baptiste Boric 		bpos++;
9689f988b79SJean-Baptiste Boric 	}
9699f988b79SJean-Baptiste Boric 
9709f988b79SJean-Baptiste Boric 	/* stop bits */
9719f988b79SJean-Baptiste Boric 	KASSERT(num_lb < 8);
9729f988b79SJean-Baptiste Boric 	bit = 0;
9739f988b79SJean-Baptiste Boric 	while (num_lb > 0) {
9749f988b79SJean-Baptiste Boric 		bitval = (1 << bit);
9759f988b79SJean-Baptiste Boric 		KASSERT((*bpos & bitval) == 0);
9769f988b79SJean-Baptiste Boric 		DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n",
9779f988b79SJean-Baptiste Boric 			offset, bpos, bit));
9789f988b79SJean-Baptiste Boric 		*bpos |= bitval;
9799f988b79SJean-Baptiste Boric 		offset++; num_lb--;
9809f988b79SJean-Baptiste Boric 		bit = (bit + 1) % 8;
9819f988b79SJean-Baptiste Boric 	}
9829f988b79SJean-Baptiste Boric }
9839f988b79SJean-Baptiste Boric 
9849f988b79SJean-Baptiste Boric 
9859f988b79SJean-Baptiste Boric static uint32_t
udf_bitmap_check_trunc_free(struct udf_bitmap * bitmap,uint32_t to_trunc)9869f988b79SJean-Baptiste Boric udf_bitmap_check_trunc_free(struct udf_bitmap *bitmap, uint32_t to_trunc)
9879f988b79SJean-Baptiste Boric {
9889f988b79SJean-Baptiste Boric 	uint32_t seq_free, offset;
9899f988b79SJean-Baptiste Boric 	uint8_t *bpos;
9909f988b79SJean-Baptiste Boric 	uint8_t  bit, bitval;
9919f988b79SJean-Baptiste Boric 
9929f988b79SJean-Baptiste Boric 	DPRINTF(RESERVE, ("\ttrying to trunc %d bits from bitmap\n", to_trunc));
9939f988b79SJean-Baptiste Boric 	offset = bitmap->max_offset - to_trunc;
9949f988b79SJean-Baptiste Boric 
9959f988b79SJean-Baptiste Boric 	/* starter bits (if any) */
9969f988b79SJean-Baptiste Boric 	bpos = bitmap->bits + offset/8;
9979f988b79SJean-Baptiste Boric 	bit = offset % 8;
9989f988b79SJean-Baptiste Boric 	seq_free = 0;
9999f988b79SJean-Baptiste Boric 	while (to_trunc > 0) {
10009f988b79SJean-Baptiste Boric 		seq_free++;
10019f988b79SJean-Baptiste Boric 		bitval = (1 << bit);
10029f988b79SJean-Baptiste Boric 		if (!(*bpos & bitval))
10039f988b79SJean-Baptiste Boric 			seq_free = 0;
10049f988b79SJean-Baptiste Boric 		offset++; to_trunc--;
10059f988b79SJean-Baptiste Boric 		bit++;
10069f988b79SJean-Baptiste Boric 		if (bit == 8) {
10079f988b79SJean-Baptiste Boric 			bpos++;
10089f988b79SJean-Baptiste Boric 			bit = 0;
10099f988b79SJean-Baptiste Boric 		}
10109f988b79SJean-Baptiste Boric 	}
10119f988b79SJean-Baptiste Boric 
10129f988b79SJean-Baptiste Boric 	DPRINTF(RESERVE, ("\tfound %d sequential free bits in bitmap\n", seq_free));
10139f988b79SJean-Baptiste Boric 	return seq_free;
10149f988b79SJean-Baptiste Boric }
10159f988b79SJean-Baptiste Boric 
10169f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
10179f988b79SJean-Baptiste Boric 
10189f988b79SJean-Baptiste Boric /*
10199f988b79SJean-Baptiste Boric  * We check for overall disc space with a margin to prevent critical
10209f988b79SJean-Baptiste Boric  * conditions.  If disc space is low we try to force a sync() to improve our
10219f988b79SJean-Baptiste Boric  * estimates.  When confronted with meta-data partition size shortage we know
10229f988b79SJean-Baptiste Boric  * we have to check if it can be extended and we need to extend it when
10239f988b79SJean-Baptiste Boric  * needed.
10249f988b79SJean-Baptiste Boric  *
10259f988b79SJean-Baptiste Boric  * A 2nd strategy we could use when disc space is getting low on a disc
10269f988b79SJean-Baptiste Boric  * formatted with a meta-data partition is to see if there are sparse areas in
10279f988b79SJean-Baptiste Boric  * the meta-data partition and free blocks there for extra data.
10289f988b79SJean-Baptiste Boric  */
10299f988b79SJean-Baptiste Boric 
10309f988b79SJean-Baptiste Boric void
udf_do_reserve_space(struct udf_mount * ump,struct udf_node * udf_node,uint16_t vpart_num,uint32_t num_lb)10319f988b79SJean-Baptiste Boric udf_do_reserve_space(struct udf_mount *ump, struct udf_node *udf_node,
10329f988b79SJean-Baptiste Boric 	uint16_t vpart_num, uint32_t num_lb)
10339f988b79SJean-Baptiste Boric {
10349f988b79SJean-Baptiste Boric 	ump->uncommitted_lbs[vpart_num] += num_lb;
10359f988b79SJean-Baptiste Boric 	if (udf_node)
10369f988b79SJean-Baptiste Boric 		udf_node->uncommitted_lbs += num_lb;
10379f988b79SJean-Baptiste Boric }
10389f988b79SJean-Baptiste Boric 
10399f988b79SJean-Baptiste Boric 
10409f988b79SJean-Baptiste Boric void
udf_do_unreserve_space(struct udf_mount * ump,struct udf_node * udf_node,uint16_t vpart_num,uint32_t num_lb)10419f988b79SJean-Baptiste Boric udf_do_unreserve_space(struct udf_mount *ump, struct udf_node *udf_node,
10429f988b79SJean-Baptiste Boric 	uint16_t vpart_num, uint32_t num_lb)
10439f988b79SJean-Baptiste Boric {
10449f988b79SJean-Baptiste Boric 	ump->uncommitted_lbs[vpart_num] -= num_lb;
10459f988b79SJean-Baptiste Boric 	if (ump->uncommitted_lbs[vpart_num] < 0) {
10469f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("UDF: underflow on partition reservation, "
10479f988b79SJean-Baptiste Boric 			"part %d: %d\n", vpart_num,
10489f988b79SJean-Baptiste Boric 			ump->uncommitted_lbs[vpart_num]));
10499f988b79SJean-Baptiste Boric 		ump->uncommitted_lbs[vpart_num] = 0;
10509f988b79SJean-Baptiste Boric 	}
10519f988b79SJean-Baptiste Boric 	if (udf_node) {
10529f988b79SJean-Baptiste Boric 		udf_node->uncommitted_lbs -= num_lb;
10539f988b79SJean-Baptiste Boric 		if (udf_node->uncommitted_lbs < 0) {
10549f988b79SJean-Baptiste Boric 			DPRINTF(RESERVE, ("UDF: underflow of node "
10559f988b79SJean-Baptiste Boric 				"reservation : %d\n",
10569f988b79SJean-Baptiste Boric 				udf_node->uncommitted_lbs));
10579f988b79SJean-Baptiste Boric 			udf_node->uncommitted_lbs = 0;
10589f988b79SJean-Baptiste Boric 		}
10599f988b79SJean-Baptiste Boric 	}
10609f988b79SJean-Baptiste Boric }
10619f988b79SJean-Baptiste Boric 
10629f988b79SJean-Baptiste Boric 
10639f988b79SJean-Baptiste Boric int
udf_reserve_space(struct udf_mount * ump,struct udf_node * udf_node,int udf_c_type,uint16_t vpart_num,uint32_t num_lb,int can_fail)10649f988b79SJean-Baptiste Boric udf_reserve_space(struct udf_mount *ump, struct udf_node *udf_node,
10659f988b79SJean-Baptiste Boric 	int udf_c_type, uint16_t vpart_num, uint32_t num_lb, int can_fail)
10669f988b79SJean-Baptiste Boric {
10679f988b79SJean-Baptiste Boric 	uint64_t freeblks;
10689f988b79SJean-Baptiste Boric 	uint64_t slack;
10699f988b79SJean-Baptiste Boric 	int i, error;
10709f988b79SJean-Baptiste Boric 
10719f988b79SJean-Baptiste Boric 	slack = 0;
10729f988b79SJean-Baptiste Boric 	if (can_fail)
10739f988b79SJean-Baptiste Boric 		slack = UDF_DISC_SLACK;
10749f988b79SJean-Baptiste Boric 
10759f988b79SJean-Baptiste Boric 	error = 0;
10769f988b79SJean-Baptiste Boric 	mutex_enter(&ump->allocate_mutex);
10779f988b79SJean-Baptiste Boric 
10789f988b79SJean-Baptiste Boric 	/* check if there is enough space available */
10799f988b79SJean-Baptiste Boric 	for (i = 0; i < 3; i++) {	/* XXX arbitrary number */
10809f988b79SJean-Baptiste Boric 		udf_calc_vpart_freespace(ump, vpart_num, &freeblks);
10819f988b79SJean-Baptiste Boric 		if (num_lb + slack < freeblks)
10829f988b79SJean-Baptiste Boric 			break;
10839f988b79SJean-Baptiste Boric 		/* issue SYNC */
10849f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("udf_reserve_space: issuing sync\n"));
10859f988b79SJean-Baptiste Boric 		mutex_exit(&ump->allocate_mutex);
10869f988b79SJean-Baptiste Boric 		udf_do_sync(ump, FSCRED, 0);
10879f988b79SJean-Baptiste Boric 		/* 1/8 second wait */
1088*0a6a1f1dSLionel Sambuc 		kpause("udfsync2", false, hz/8, NULL);
10899f988b79SJean-Baptiste Boric 		mutex_enter(&ump->allocate_mutex);
10909f988b79SJean-Baptiste Boric 	}
10919f988b79SJean-Baptiste Boric 
10929f988b79SJean-Baptiste Boric 	/* check if there is enough space available now */
10939f988b79SJean-Baptiste Boric 	udf_calc_vpart_freespace(ump, vpart_num, &freeblks);
10949f988b79SJean-Baptiste Boric 	if (num_lb + slack >= freeblks) {
10959f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("udf_reserve_space: try to redistribute "
10969f988b79SJean-Baptiste Boric 				  "partition space\n"));
10979f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("\tvpart %d, type %d is full\n",
10989f988b79SJean-Baptiste Boric 				vpart_num, ump->vtop_alloc[vpart_num]));
10999f988b79SJean-Baptiste Boric 		/* Try to redistribute space if possible */
11009f988b79SJean-Baptiste Boric 		udf_collect_free_space_for_vpart(ump, vpart_num, num_lb + slack);
11019f988b79SJean-Baptiste Boric 	}
11029f988b79SJean-Baptiste Boric 
11039f988b79SJean-Baptiste Boric 	/* check if there is enough space available now */
11049f988b79SJean-Baptiste Boric 	udf_calc_vpart_freespace(ump, vpart_num, &freeblks);
11059f988b79SJean-Baptiste Boric 	if (num_lb + slack <= freeblks) {
11069f988b79SJean-Baptiste Boric 		udf_do_reserve_space(ump, udf_node, vpart_num, num_lb);
11079f988b79SJean-Baptiste Boric 	} else {
11089f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("udf_reserve_space: out of disc space\n"));
11099f988b79SJean-Baptiste Boric 		error = ENOSPC;
11109f988b79SJean-Baptiste Boric 	}
11119f988b79SJean-Baptiste Boric 
11129f988b79SJean-Baptiste Boric 	mutex_exit(&ump->allocate_mutex);
11139f988b79SJean-Baptiste Boric 	return error;
11149f988b79SJean-Baptiste Boric }
11159f988b79SJean-Baptiste Boric 
11169f988b79SJean-Baptiste Boric 
11179f988b79SJean-Baptiste Boric void
udf_cleanup_reservation(struct udf_node * udf_node)11189f988b79SJean-Baptiste Boric udf_cleanup_reservation(struct udf_node *udf_node)
11199f988b79SJean-Baptiste Boric {
11209f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
11219f988b79SJean-Baptiste Boric 	int vpart_num;
11229f988b79SJean-Baptiste Boric 
11239f988b79SJean-Baptiste Boric 	mutex_enter(&ump->allocate_mutex);
11249f988b79SJean-Baptiste Boric 
11259f988b79SJean-Baptiste Boric 	/* compensate for overlapping blocks */
11269f988b79SJean-Baptiste Boric 	DPRINTF(RESERVE, ("UDF: overlapped %d blocks in count\n", udf_node->uncommitted_lbs));
11279f988b79SJean-Baptiste Boric 
11289f988b79SJean-Baptiste Boric 	vpart_num = udf_get_record_vpart(ump, udf_get_c_type(udf_node));
11299f988b79SJean-Baptiste Boric 	udf_do_unreserve_space(ump, udf_node, vpart_num, udf_node->uncommitted_lbs);
11309f988b79SJean-Baptiste Boric 
11319f988b79SJean-Baptiste Boric 	DPRINTF(RESERVE, ("\ttotal now %d\n", ump->uncommitted_lbs[vpart_num]));
11329f988b79SJean-Baptiste Boric 
11339f988b79SJean-Baptiste Boric 	/* sanity */
11349f988b79SJean-Baptiste Boric 	if (ump->uncommitted_lbs[vpart_num] < 0)
11359f988b79SJean-Baptiste Boric 		ump->uncommitted_lbs[vpart_num] = 0;
11369f988b79SJean-Baptiste Boric 
11379f988b79SJean-Baptiste Boric 	mutex_exit(&ump->allocate_mutex);
11389f988b79SJean-Baptiste Boric }
11399f988b79SJean-Baptiste Boric 
11409f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
11419f988b79SJean-Baptiste Boric 
11429f988b79SJean-Baptiste Boric /*
11439f988b79SJean-Baptiste Boric  * Allocate an extent of given length on given virt. partition. It doesn't
11449f988b79SJean-Baptiste Boric  * have to be one stretch.
11459f988b79SJean-Baptiste Boric  */
11469f988b79SJean-Baptiste Boric 
11479f988b79SJean-Baptiste Boric int
udf_allocate_space(struct udf_mount * ump,struct udf_node * udf_node,int udf_c_type,uint16_t vpart_num,uint32_t num_lb,uint64_t * lmapping)11489f988b79SJean-Baptiste Boric udf_allocate_space(struct udf_mount *ump, struct udf_node *udf_node,
11499f988b79SJean-Baptiste Boric 	int udf_c_type, uint16_t vpart_num, uint32_t num_lb, uint64_t *lmapping)
11509f988b79SJean-Baptiste Boric {
11519f988b79SJean-Baptiste Boric 	struct mmc_trackinfo *alloc_track, *other_track;
11529f988b79SJean-Baptiste Boric 	struct udf_bitmap *bitmap;
11539f988b79SJean-Baptiste Boric 	struct part_desc *pdesc;
11549f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvid;
11559f988b79SJean-Baptiste Boric 	uint64_t *lmappos;
11569f988b79SJean-Baptiste Boric 	uint32_t ptov, lb_num, *freepos, free_lbs;
11579f988b79SJean-Baptiste Boric 	int lb_size __diagused, alloc_num_lb;
11589f988b79SJean-Baptiste Boric 	int alloc_type, error;
11599f988b79SJean-Baptiste Boric 	int is_node;
11609f988b79SJean-Baptiste Boric 
11619f988b79SJean-Baptiste Boric 	DPRINTF(CALL, ("udf_allocate_space(ctype %d, vpart %d, num_lb %d\n",
11629f988b79SJean-Baptiste Boric 		udf_c_type, vpart_num, num_lb));
11639f988b79SJean-Baptiste Boric 	mutex_enter(&ump->allocate_mutex);
11649f988b79SJean-Baptiste Boric 
11659f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
11669f988b79SJean-Baptiste Boric 	KASSERT(lb_size == ump->discinfo.sector_size);
11679f988b79SJean-Baptiste Boric 
11689f988b79SJean-Baptiste Boric 	alloc_type =  ump->vtop_alloc[vpart_num];
11699f988b79SJean-Baptiste Boric 	is_node    = (udf_c_type == UDF_C_NODE);
11709f988b79SJean-Baptiste Boric 
11719f988b79SJean-Baptiste Boric 	lmappos = lmapping;
11729f988b79SJean-Baptiste Boric 	error = 0;
11739f988b79SJean-Baptiste Boric 	switch (alloc_type) {
11749f988b79SJean-Baptiste Boric 	case UDF_ALLOC_VAT :
11759f988b79SJean-Baptiste Boric 		/* search empty slot in VAT file */
11769f988b79SJean-Baptiste Boric 		KASSERT(num_lb == 1);
11779f988b79SJean-Baptiste Boric 		error = udf_search_free_vatloc(ump, &lb_num);
11789f988b79SJean-Baptiste Boric 		if (!error) {
11799f988b79SJean-Baptiste Boric 			*lmappos = lb_num;
11809f988b79SJean-Baptiste Boric 
11819f988b79SJean-Baptiste Boric 			/* reserve on the backing sequential partition since
11829f988b79SJean-Baptiste Boric 			 * that partition is credited back later */
11839f988b79SJean-Baptiste Boric 			udf_do_reserve_space(ump, udf_node,
11849f988b79SJean-Baptiste Boric 				ump->vtop[vpart_num], num_lb);
11859f988b79SJean-Baptiste Boric 		}
11869f988b79SJean-Baptiste Boric 		break;
11879f988b79SJean-Baptiste Boric 	case UDF_ALLOC_SEQUENTIAL :
11889f988b79SJean-Baptiste Boric 		/* sequential allocation on recordable media */
11899f988b79SJean-Baptiste Boric 		/* get partition backing up this vpart_num_num */
11909f988b79SJean-Baptiste Boric 		pdesc = ump->partitions[ump->vtop[vpart_num]];
11919f988b79SJean-Baptiste Boric 
11929f988b79SJean-Baptiste Boric 		/* calculate offset from physical base partition */
11939f988b79SJean-Baptiste Boric 		ptov  = udf_rw32(pdesc->start_loc);
11949f988b79SJean-Baptiste Boric 
11959f988b79SJean-Baptiste Boric 		/* get our track descriptors */
11969f988b79SJean-Baptiste Boric 		if (vpart_num == ump->node_part) {
11979f988b79SJean-Baptiste Boric 			alloc_track = &ump->metadata_track;
11989f988b79SJean-Baptiste Boric 			other_track = &ump->data_track;
11999f988b79SJean-Baptiste Boric 		} else {
12009f988b79SJean-Baptiste Boric 			alloc_track = &ump->data_track;
12019f988b79SJean-Baptiste Boric 			other_track = &ump->metadata_track;
12029f988b79SJean-Baptiste Boric 		}
12039f988b79SJean-Baptiste Boric 
12049f988b79SJean-Baptiste Boric 		/* allocate */
12059f988b79SJean-Baptiste Boric 		for (lb_num = 0; lb_num < num_lb; lb_num++) {
12069f988b79SJean-Baptiste Boric 			*lmappos++ = alloc_track->next_writable - ptov;
12079f988b79SJean-Baptiste Boric 			alloc_track->next_writable++;
12089f988b79SJean-Baptiste Boric 			alloc_track->free_blocks--;
12099f988b79SJean-Baptiste Boric 		}
12109f988b79SJean-Baptiste Boric 
12119f988b79SJean-Baptiste Boric 		/* keep other track up-to-date */
12129f988b79SJean-Baptiste Boric 		if (alloc_track->tracknr == other_track->tracknr)
12139f988b79SJean-Baptiste Boric 			memcpy(other_track, alloc_track,
12149f988b79SJean-Baptiste Boric 				sizeof(struct mmc_trackinfo));
12159f988b79SJean-Baptiste Boric 		break;
12169f988b79SJean-Baptiste Boric 	case UDF_ALLOC_SPACEMAP :
12179f988b79SJean-Baptiste Boric 		/* try to allocate on unallocated bits */
12189f988b79SJean-Baptiste Boric 		alloc_num_lb = num_lb;
12199f988b79SJean-Baptiste Boric 		bitmap = &ump->part_unalloc_bits[vpart_num];
12209f988b79SJean-Baptiste Boric 		udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos);
12219f988b79SJean-Baptiste Boric 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
12229f988b79SJean-Baptiste Boric 
12239f988b79SJean-Baptiste Boric 		/* have we allocated all? */
12249f988b79SJean-Baptiste Boric 		if (alloc_num_lb) {
12259f988b79SJean-Baptiste Boric 			/* TODO convert freed to unalloc and try again */
12269f988b79SJean-Baptiste Boric 			/* free allocated piece for now */
12279f988b79SJean-Baptiste Boric 			lmappos = lmapping;
12289f988b79SJean-Baptiste Boric 			for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) {
12299f988b79SJean-Baptiste Boric 				udf_bitmap_free(bitmap, *lmappos++, 1);
12309f988b79SJean-Baptiste Boric 			}
12319f988b79SJean-Baptiste Boric 			error = ENOSPC;
12329f988b79SJean-Baptiste Boric 		}
12339f988b79SJean-Baptiste Boric 		if (!error) {
12349f988b79SJean-Baptiste Boric 			/* adjust freecount */
12359f988b79SJean-Baptiste Boric 			lvid = ump->logvol_integrity;
12369f988b79SJean-Baptiste Boric 			freepos = &lvid->tables[0] + vpart_num;
12379f988b79SJean-Baptiste Boric 			free_lbs = udf_rw32(*freepos);
12389f988b79SJean-Baptiste Boric 			*freepos = udf_rw32(free_lbs - num_lb);
12399f988b79SJean-Baptiste Boric 		}
12409f988b79SJean-Baptiste Boric 		break;
12419f988b79SJean-Baptiste Boric 	case UDF_ALLOC_METABITMAP :		/* UDF 2.50, 2.60 BluRay-RE */
12429f988b79SJean-Baptiste Boric 		/* allocate on metadata unallocated bits */
12439f988b79SJean-Baptiste Boric 		alloc_num_lb = num_lb;
12449f988b79SJean-Baptiste Boric 		bitmap = &ump->metadata_unalloc_bits;
12459f988b79SJean-Baptiste Boric 		udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos);
12469f988b79SJean-Baptiste Boric 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
12479f988b79SJean-Baptiste Boric 
12489f988b79SJean-Baptiste Boric 		/* have we allocated all? */
12499f988b79SJean-Baptiste Boric 		if (alloc_num_lb) {
12509f988b79SJean-Baptiste Boric 			/* YIKES! TODO we need to extend the metadata partition */
12519f988b79SJean-Baptiste Boric 			/* free allocated piece for now */
12529f988b79SJean-Baptiste Boric 			lmappos = lmapping;
12539f988b79SJean-Baptiste Boric 			for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) {
12549f988b79SJean-Baptiste Boric 				udf_bitmap_free(bitmap, *lmappos++, 1);
12559f988b79SJean-Baptiste Boric 			}
12569f988b79SJean-Baptiste Boric 			error = ENOSPC;
12579f988b79SJean-Baptiste Boric 		}
12589f988b79SJean-Baptiste Boric 		if (!error) {
12599f988b79SJean-Baptiste Boric 			/* adjust freecount */
12609f988b79SJean-Baptiste Boric 			lvid = ump->logvol_integrity;
12619f988b79SJean-Baptiste Boric 			freepos = &lvid->tables[0] + vpart_num;
12629f988b79SJean-Baptiste Boric 			free_lbs = udf_rw32(*freepos);
12639f988b79SJean-Baptiste Boric 			*freepos = udf_rw32(free_lbs - num_lb);
12649f988b79SJean-Baptiste Boric 		}
12659f988b79SJean-Baptiste Boric 		break;
12669f988b79SJean-Baptiste Boric 	case UDF_ALLOC_METASEQUENTIAL :		/* UDF 2.60       BluRay-R  */
12679f988b79SJean-Baptiste Boric 	case UDF_ALLOC_RELAXEDSEQUENTIAL :	/* UDF 2.50/~meta BluRay-R  */
12689f988b79SJean-Baptiste Boric 		printf("ALERT: udf_allocate_space : allocation %d "
12699f988b79SJean-Baptiste Boric 				"not implemented yet!\n", alloc_type);
12709f988b79SJean-Baptiste Boric 		/* TODO implement, doesn't have to be contiguous */
12719f988b79SJean-Baptiste Boric 		error = ENOSPC;
12729f988b79SJean-Baptiste Boric 		break;
12739f988b79SJean-Baptiste Boric 	}
12749f988b79SJean-Baptiste Boric 
12759f988b79SJean-Baptiste Boric 	if (!error) {
12769f988b79SJean-Baptiste Boric 		/* credit our partition since we have committed the space */
12779f988b79SJean-Baptiste Boric 		udf_do_unreserve_space(ump, udf_node, vpart_num, num_lb);
12789f988b79SJean-Baptiste Boric 	}
12799f988b79SJean-Baptiste Boric 
12809f988b79SJean-Baptiste Boric #ifdef DEBUG
12819f988b79SJean-Baptiste Boric 	if (udf_verbose & UDF_DEBUG_ALLOC) {
12829f988b79SJean-Baptiste Boric 		lmappos = lmapping;
12839f988b79SJean-Baptiste Boric 		printf("udf_allocate_space, allocated logical lba :\n");
12849f988b79SJean-Baptiste Boric 		for (lb_num = 0; lb_num < num_lb; lb_num++) {
12859f988b79SJean-Baptiste Boric 			printf("%s %"PRIu64, (lb_num > 0)?",":"",
12869f988b79SJean-Baptiste Boric 				*lmappos++);
12879f988b79SJean-Baptiste Boric 		}
12889f988b79SJean-Baptiste Boric 		printf("\n");
12899f988b79SJean-Baptiste Boric 	}
12909f988b79SJean-Baptiste Boric #endif
12919f988b79SJean-Baptiste Boric 	mutex_exit(&ump->allocate_mutex);
12929f988b79SJean-Baptiste Boric 
12939f988b79SJean-Baptiste Boric 	return error;
12949f988b79SJean-Baptiste Boric }
12959f988b79SJean-Baptiste Boric 
12969f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
12979f988b79SJean-Baptiste Boric 
12989f988b79SJean-Baptiste Boric void
udf_free_allocated_space(struct udf_mount * ump,uint32_t lb_num,uint16_t vpart_num,uint32_t num_lb)12999f988b79SJean-Baptiste Boric udf_free_allocated_space(struct udf_mount *ump, uint32_t lb_num,
13009f988b79SJean-Baptiste Boric 	uint16_t vpart_num, uint32_t num_lb)
13019f988b79SJean-Baptiste Boric {
13029f988b79SJean-Baptiste Boric 	struct udf_bitmap *bitmap;
13039f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvid;
13049f988b79SJean-Baptiste Boric 	uint32_t lb_map, udf_rw32_lbmap;
13059f988b79SJean-Baptiste Boric 	uint32_t *freepos, free_lbs;
13069f988b79SJean-Baptiste Boric 	int phys_part;
13079f988b79SJean-Baptiste Boric 	int error __diagused;
13089f988b79SJean-Baptiste Boric 
13099f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("udf_free_allocated_space: freeing virt lbnum %d "
13109f988b79SJean-Baptiste Boric 			  "part %d + %d sect\n", lb_num, vpart_num, num_lb));
13119f988b79SJean-Baptiste Boric 
13129f988b79SJean-Baptiste Boric 	/* no use freeing zero length */
13139f988b79SJean-Baptiste Boric 	if (num_lb == 0)
13149f988b79SJean-Baptiste Boric 		return;
13159f988b79SJean-Baptiste Boric 
13169f988b79SJean-Baptiste Boric 	mutex_enter(&ump->allocate_mutex);
13179f988b79SJean-Baptiste Boric 
13189f988b79SJean-Baptiste Boric 	switch (ump->vtop_tp[vpart_num]) {
13199f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_PHYS :
13209f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_SPARABLE :
13219f988b79SJean-Baptiste Boric 		/* free space to freed or unallocated space bitmap */
13229f988b79SJean-Baptiste Boric 		phys_part = ump->vtop[vpart_num];
13239f988b79SJean-Baptiste Boric 
13249f988b79SJean-Baptiste Boric 		/* first try freed space bitmap */
13259f988b79SJean-Baptiste Boric 		bitmap    = &ump->part_freed_bits[phys_part];
13269f988b79SJean-Baptiste Boric 
13279f988b79SJean-Baptiste Boric 		/* if not defined, use unallocated bitmap */
13289f988b79SJean-Baptiste Boric 		if (bitmap->bits == NULL)
13299f988b79SJean-Baptiste Boric 			bitmap = &ump->part_unalloc_bits[phys_part];
13309f988b79SJean-Baptiste Boric 
13319f988b79SJean-Baptiste Boric 		/* if no bitmaps are defined, bail out; XXX OK? */
13329f988b79SJean-Baptiste Boric 		if (bitmap->bits == NULL)
13339f988b79SJean-Baptiste Boric 			break;
13349f988b79SJean-Baptiste Boric 
13359f988b79SJean-Baptiste Boric 		/* free bits if its defined */
13369f988b79SJean-Baptiste Boric 		KASSERT(bitmap->bits);
13379f988b79SJean-Baptiste Boric 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
13389f988b79SJean-Baptiste Boric 		udf_bitmap_free(bitmap, lb_num, num_lb);
13399f988b79SJean-Baptiste Boric 
13409f988b79SJean-Baptiste Boric 		/* adjust freecount */
13419f988b79SJean-Baptiste Boric 		lvid = ump->logvol_integrity;
13429f988b79SJean-Baptiste Boric 		freepos = &lvid->tables[0] + vpart_num;
13439f988b79SJean-Baptiste Boric 		free_lbs = udf_rw32(*freepos);
13449f988b79SJean-Baptiste Boric 		*freepos = udf_rw32(free_lbs + num_lb);
13459f988b79SJean-Baptiste Boric 		break;
13469f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_VIRT :
13479f988b79SJean-Baptiste Boric 		/* free this VAT entry */
13489f988b79SJean-Baptiste Boric 		KASSERT(num_lb == 1);
13499f988b79SJean-Baptiste Boric 
13509f988b79SJean-Baptiste Boric 		lb_map = 0xffffffff;
13519f988b79SJean-Baptiste Boric 		udf_rw32_lbmap = udf_rw32(lb_map);
13529f988b79SJean-Baptiste Boric 		error = udf_vat_write(ump->vat_node,
13539f988b79SJean-Baptiste Boric 			(uint8_t *) &udf_rw32_lbmap, 4,
13549f988b79SJean-Baptiste Boric 			ump->vat_offset + lb_num * 4);
13559f988b79SJean-Baptiste Boric 		KASSERT(error == 0);
13569f988b79SJean-Baptiste Boric 		ump->vat_last_free_lb = MIN(ump->vat_last_free_lb, lb_num);
13579f988b79SJean-Baptiste Boric 		break;
13589f988b79SJean-Baptiste Boric 	case UDF_VTOP_TYPE_META :
13599f988b79SJean-Baptiste Boric 		/* free space in the metadata bitmap */
13609f988b79SJean-Baptiste Boric 		bitmap = &ump->metadata_unalloc_bits;
13619f988b79SJean-Baptiste Boric 		KASSERT(bitmap->bits);
13629f988b79SJean-Baptiste Boric 
13639f988b79SJean-Baptiste Boric 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
13649f988b79SJean-Baptiste Boric 		udf_bitmap_free(bitmap, lb_num, num_lb);
13659f988b79SJean-Baptiste Boric 
13669f988b79SJean-Baptiste Boric 		/* adjust freecount */
13679f988b79SJean-Baptiste Boric 		lvid = ump->logvol_integrity;
13689f988b79SJean-Baptiste Boric 		freepos = &lvid->tables[0] + vpart_num;
13699f988b79SJean-Baptiste Boric 		free_lbs = udf_rw32(*freepos);
13709f988b79SJean-Baptiste Boric 		*freepos = udf_rw32(free_lbs + num_lb);
13719f988b79SJean-Baptiste Boric 		break;
13729f988b79SJean-Baptiste Boric 	default:
13739f988b79SJean-Baptiste Boric 		printf("ALERT: udf_free_allocated_space : allocation %d "
13749f988b79SJean-Baptiste Boric 			"not implemented yet!\n", ump->vtop_tp[vpart_num]);
13759f988b79SJean-Baptiste Boric 		break;
13769f988b79SJean-Baptiste Boric 	}
13779f988b79SJean-Baptiste Boric 
13789f988b79SJean-Baptiste Boric 	mutex_exit(&ump->allocate_mutex);
13799f988b79SJean-Baptiste Boric }
13809f988b79SJean-Baptiste Boric 
13819f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
13829f988b79SJean-Baptiste Boric 
13839f988b79SJean-Baptiste Boric /*
13849f988b79SJean-Baptiste Boric  * Special function to synchronise the metadatamirror file when they change on
13859f988b79SJean-Baptiste Boric  * resizing. When the metadatafile is actually duplicated, this action is a
13869f988b79SJean-Baptiste Boric  * no-op since they describe different extents on the disc.
13879f988b79SJean-Baptiste Boric  */
13889f988b79SJean-Baptiste Boric 
13899f988b79SJean-Baptiste Boric void
udf_synchronise_metadatamirror_node(struct udf_mount * ump)13909f988b79SJean-Baptiste Boric udf_synchronise_metadatamirror_node(struct udf_mount *ump)
13919f988b79SJean-Baptiste Boric {
13929f988b79SJean-Baptiste Boric 	struct udf_node *meta_node, *metamirror_node;
13939f988b79SJean-Baptiste Boric 	struct long_ad s_ad;
13949f988b79SJean-Baptiste Boric 	uint32_t len, flags;
13959f988b79SJean-Baptiste Boric 	int slot, cpy_slot;
13969f988b79SJean-Baptiste Boric 	int error, eof;
13979f988b79SJean-Baptiste Boric 
13989f988b79SJean-Baptiste Boric 	if (ump->metadata_flags & METADATA_DUPLICATED)
13999f988b79SJean-Baptiste Boric 		return;
14009f988b79SJean-Baptiste Boric 
14019f988b79SJean-Baptiste Boric 	meta_node       = ump->metadata_node;
14029f988b79SJean-Baptiste Boric 	metamirror_node = ump->metadatamirror_node;
14039f988b79SJean-Baptiste Boric 
14049f988b79SJean-Baptiste Boric 	/* 1) wipe mirror node */
14059f988b79SJean-Baptiste Boric 	udf_wipe_adslots(metamirror_node);
14069f988b79SJean-Baptiste Boric 
14079f988b79SJean-Baptiste Boric 	/* 2) copy all node descriptors from the meta_node */
14089f988b79SJean-Baptiste Boric 	slot     = 0;
14099f988b79SJean-Baptiste Boric 	cpy_slot = 0;
14109f988b79SJean-Baptiste Boric 	for (;;) {
14119f988b79SJean-Baptiste Boric 		udf_get_adslot(meta_node, slot, &s_ad, &eof);
14129f988b79SJean-Baptiste Boric 		if (eof)
14139f988b79SJean-Baptiste Boric 			break;
14149f988b79SJean-Baptiste Boric 		len   = udf_rw32(s_ad.len);
14159f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(len);
14169f988b79SJean-Baptiste Boric 		len   = UDF_EXT_LEN(len);
14179f988b79SJean-Baptiste Boric 
14189f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
14199f988b79SJean-Baptiste Boric 			slot++;
14209f988b79SJean-Baptiste Boric 			continue;
14219f988b79SJean-Baptiste Boric 		}
14229f988b79SJean-Baptiste Boric 
14239f988b79SJean-Baptiste Boric 		error = udf_append_adslot(metamirror_node, &cpy_slot, &s_ad);
14249f988b79SJean-Baptiste Boric 		if (error) {
14259f988b79SJean-Baptiste Boric 			/* WTF, this shouldn't happen, what to do now? */
14269f988b79SJean-Baptiste Boric 			panic("udf_synchronise_metadatamirror_node failed!");
14279f988b79SJean-Baptiste Boric 		}
14289f988b79SJean-Baptiste Boric 		cpy_slot++;
14299f988b79SJean-Baptiste Boric 		slot++;
14309f988b79SJean-Baptiste Boric 	}
14319f988b79SJean-Baptiste Boric 
14329f988b79SJean-Baptiste Boric 	/* 3) adjust metamirror_node size */
14339f988b79SJean-Baptiste Boric 	if (meta_node->fe) {
14349f988b79SJean-Baptiste Boric 		KASSERT(metamirror_node->fe);
14359f988b79SJean-Baptiste Boric 		metamirror_node->fe->inf_len = meta_node->fe->inf_len;
14369f988b79SJean-Baptiste Boric 	} else {
14379f988b79SJean-Baptiste Boric 		KASSERT(meta_node->efe);
14389f988b79SJean-Baptiste Boric 		KASSERT(metamirror_node->efe);
14399f988b79SJean-Baptiste Boric 		metamirror_node->efe->inf_len  = meta_node->efe->inf_len;
14409f988b79SJean-Baptiste Boric 		metamirror_node->efe->obj_size = meta_node->efe->obj_size;
14419f988b79SJean-Baptiste Boric 	}
14429f988b79SJean-Baptiste Boric 
14439f988b79SJean-Baptiste Boric 	/* for sanity */
14449f988b79SJean-Baptiste Boric 	udf_count_alloc_exts(metamirror_node);
14459f988b79SJean-Baptiste Boric }
14469f988b79SJean-Baptiste Boric 
14479f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
14489f988b79SJean-Baptiste Boric 
14499f988b79SJean-Baptiste Boric /*
14509f988b79SJean-Baptiste Boric  * When faced with an out of space but there is still space available on other
14519f988b79SJean-Baptiste Boric  * partitions, try to redistribute the space. This is only defined for media
14529f988b79SJean-Baptiste Boric  * using Metadata partitions.
14539f988b79SJean-Baptiste Boric  *
14549f988b79SJean-Baptiste Boric  * There are two formats to deal with. Either its a `normal' metadata
14559f988b79SJean-Baptiste Boric  * partition and we can move blocks between a metadata bitmap and its
14569f988b79SJean-Baptiste Boric  * companion data spacemap OR its a UDF 2.60 formatted BluRay-R disc with POW
14579f988b79SJean-Baptiste Boric  * and a metadata partition.
14589f988b79SJean-Baptiste Boric  */
14599f988b79SJean-Baptiste Boric 
14609f988b79SJean-Baptiste Boric /* implementation limit: ump->datapart is the companion partition */
14619f988b79SJean-Baptiste Boric static uint32_t
udf_trunc_metadatapart(struct udf_mount * ump,uint32_t num_lb)14629f988b79SJean-Baptiste Boric udf_trunc_metadatapart(struct udf_mount *ump, uint32_t num_lb)
14639f988b79SJean-Baptiste Boric {
14649f988b79SJean-Baptiste Boric 	struct udf_node *bitmap_node;
14659f988b79SJean-Baptiste Boric 	struct udf_bitmap *bitmap;
14669f988b79SJean-Baptiste Boric 	struct space_bitmap_desc *sbd, *new_sbd;
14679f988b79SJean-Baptiste Boric 	struct logvol_int_desc *lvid;
14689f988b79SJean-Baptiste Boric 	uint64_t inf_len;
14699f988b79SJean-Baptiste Boric 	uint64_t meta_free_lbs, data_free_lbs, to_trunc;
14709f988b79SJean-Baptiste Boric 	uint32_t *freepos, *sizepos;
14719f988b79SJean-Baptiste Boric 	uint32_t unit, lb_size;
14729f988b79SJean-Baptiste Boric 	uint16_t meta_vpart_num, data_vpart_num, num_vpart;
14739f988b79SJean-Baptiste Boric 	int err __diagused;
14749f988b79SJean-Baptiste Boric 
14759f988b79SJean-Baptiste Boric 	unit = ump->metadata_alloc_unit_size;
14769f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
14779f988b79SJean-Baptiste Boric 	lvid = ump->logvol_integrity;
14789f988b79SJean-Baptiste Boric 
14799f988b79SJean-Baptiste Boric 	/* XXX
14809f988b79SJean-Baptiste Boric 	 *
14819f988b79SJean-Baptiste Boric 	 * the following checks will fail for BD-R UDF 2.60! but they are
14829f988b79SJean-Baptiste Boric 	 * read-only for now anyway! Its even doubtfull if it is to be allowed
14839f988b79SJean-Baptiste Boric 	 * for these discs.
14849f988b79SJean-Baptiste Boric 	 */
14859f988b79SJean-Baptiste Boric 
14869f988b79SJean-Baptiste Boric 	/* lookup vpart for metadata partition */
14879f988b79SJean-Baptiste Boric 	meta_vpart_num = ump->node_part;
14889f988b79SJean-Baptiste Boric 	KASSERT(ump->vtop_alloc[meta_vpart_num] == UDF_ALLOC_METABITMAP);
14899f988b79SJean-Baptiste Boric 
14909f988b79SJean-Baptiste Boric 	/* lookup vpart for data partition */
14919f988b79SJean-Baptiste Boric 	data_vpart_num = ump->data_part;
14929f988b79SJean-Baptiste Boric 	KASSERT(ump->vtop_alloc[data_vpart_num] == UDF_ALLOC_SPACEMAP);
14939f988b79SJean-Baptiste Boric 
14949f988b79SJean-Baptiste Boric 	udf_calc_vpart_freespace(ump, data_vpart_num, &data_free_lbs);
14959f988b79SJean-Baptiste Boric 	udf_calc_vpart_freespace(ump, meta_vpart_num, &meta_free_lbs);
14969f988b79SJean-Baptiste Boric 
14979f988b79SJean-Baptiste Boric 	DPRINTF(RESERVE, ("\tfree space on data partition     %"PRIu64" blks\n", data_free_lbs));
14989f988b79SJean-Baptiste Boric 	DPRINTF(RESERVE, ("\tfree space on metadata partition %"PRIu64" blks\n", meta_free_lbs));
14999f988b79SJean-Baptiste Boric 
15009f988b79SJean-Baptiste Boric 	/* give away some of the free meta space, in unit block sizes */
15019f988b79SJean-Baptiste Boric 	to_trunc = meta_free_lbs/4;			/* give out a quarter */
15029f988b79SJean-Baptiste Boric 	to_trunc = MAX(to_trunc, num_lb);
15039f988b79SJean-Baptiste Boric 	to_trunc = unit * ((to_trunc + unit-1) / unit);	/* round up */
15049f988b79SJean-Baptiste Boric 
15059f988b79SJean-Baptiste Boric 	/* scale down if needed and bail out when out of space */
15069f988b79SJean-Baptiste Boric 	if (to_trunc >= meta_free_lbs)
15079f988b79SJean-Baptiste Boric 		return num_lb;
15089f988b79SJean-Baptiste Boric 
15099f988b79SJean-Baptiste Boric 	/* check extent of bits marked free at the end of the map */
15109f988b79SJean-Baptiste Boric 	bitmap = &ump->metadata_unalloc_bits;
15119f988b79SJean-Baptiste Boric 	to_trunc = udf_bitmap_check_trunc_free(bitmap, to_trunc);
15129f988b79SJean-Baptiste Boric 	to_trunc = unit * (to_trunc / unit);		/* round down again */
15139f988b79SJean-Baptiste Boric 	if (to_trunc == 0)
15149f988b79SJean-Baptiste Boric 		return num_lb;
15159f988b79SJean-Baptiste Boric 
15169f988b79SJean-Baptiste Boric 	DPRINTF(RESERVE, ("\ttruncating %"PRIu64" lbs from the metadata bitmap\n",
15179f988b79SJean-Baptiste Boric 		to_trunc));
15189f988b79SJean-Baptiste Boric 
15199f988b79SJean-Baptiste Boric 	/* get length of the metadata bitmap node file */
15209f988b79SJean-Baptiste Boric 	bitmap_node = ump->metadatabitmap_node;
15219f988b79SJean-Baptiste Boric 	if (bitmap_node->fe) {
15229f988b79SJean-Baptiste Boric 		inf_len = udf_rw64(bitmap_node->fe->inf_len);
15239f988b79SJean-Baptiste Boric 	} else {
15249f988b79SJean-Baptiste Boric 		KASSERT(bitmap_node->efe);
15259f988b79SJean-Baptiste Boric 		inf_len = udf_rw64(bitmap_node->efe->inf_len);
15269f988b79SJean-Baptiste Boric 	}
15279f988b79SJean-Baptiste Boric 	inf_len -= to_trunc/8;
15289f988b79SJean-Baptiste Boric 
15299f988b79SJean-Baptiste Boric 	/* as per [UDF 2.60/2.2.13.6] : */
15309f988b79SJean-Baptiste Boric 	/* 1) update the SBD in the metadata bitmap file */
15319f988b79SJean-Baptiste Boric 	sbd = (struct space_bitmap_desc *) bitmap->blob;
15329f988b79SJean-Baptiste Boric 	sbd->num_bits  = udf_rw32(udf_rw32(sbd->num_bits)  - to_trunc);
15339f988b79SJean-Baptiste Boric 	sbd->num_bytes = udf_rw32(udf_rw32(sbd->num_bytes) - to_trunc/8);
15349f988b79SJean-Baptiste Boric 	bitmap->max_offset = udf_rw32(sbd->num_bits);
15359f988b79SJean-Baptiste Boric 
15369f988b79SJean-Baptiste Boric 	num_vpart = udf_rw32(lvid->num_part);
15379f988b79SJean-Baptiste Boric 	freepos = &lvid->tables[0] + meta_vpart_num;
15389f988b79SJean-Baptiste Boric 	sizepos = &lvid->tables[0] + num_vpart + meta_vpart_num;
15399f988b79SJean-Baptiste Boric 	*freepos = udf_rw32(*freepos) - to_trunc;
15409f988b79SJean-Baptiste Boric 	*sizepos = udf_rw32(*sizepos) - to_trunc;
15419f988b79SJean-Baptiste Boric 
15429f988b79SJean-Baptiste Boric 	/* realloc bitmap for better memory usage */
15439f988b79SJean-Baptiste Boric 	new_sbd = realloc(sbd, inf_len, M_UDFVOLD,
15449f988b79SJean-Baptiste Boric 		M_CANFAIL | M_WAITOK);
15459f988b79SJean-Baptiste Boric 	if (new_sbd) {
15469f988b79SJean-Baptiste Boric 		/* update pointers */
15479f988b79SJean-Baptiste Boric 		ump->metadata_unalloc_dscr = new_sbd;
15489f988b79SJean-Baptiste Boric 		bitmap->blob = (uint8_t *) new_sbd;
15499f988b79SJean-Baptiste Boric 	}
15509f988b79SJean-Baptiste Boric 	ump->lvclose |= UDF_WRITE_PART_BITMAPS;
15519f988b79SJean-Baptiste Boric 
15529f988b79SJean-Baptiste Boric 	/*
15539f988b79SJean-Baptiste Boric 	 * The truncated space is secured now and can't be allocated anymore.
15549f988b79SJean-Baptiste Boric 	 * Release the allocate mutex so we can shrink the nodes the normal
15559f988b79SJean-Baptiste Boric 	 * way.
15569f988b79SJean-Baptiste Boric 	 */
15579f988b79SJean-Baptiste Boric 	mutex_exit(&ump->allocate_mutex);
15589f988b79SJean-Baptiste Boric 
15599f988b79SJean-Baptiste Boric 	/* 2) trunc the metadata bitmap information file, freeing blocks */
15609f988b79SJean-Baptiste Boric 	err = udf_shrink_node(bitmap_node, inf_len);
15619f988b79SJean-Baptiste Boric 	KASSERT(err == 0);
15629f988b79SJean-Baptiste Boric 
15639f988b79SJean-Baptiste Boric 	/* 3) trunc the metadata file and mirror file, freeing blocks */
15649f988b79SJean-Baptiste Boric 	inf_len = (uint64_t) udf_rw32(sbd->num_bits) * lb_size;	/* [4/14.12.4] */
15659f988b79SJean-Baptiste Boric 	err = udf_shrink_node(ump->metadata_node, inf_len);
15669f988b79SJean-Baptiste Boric 	KASSERT(err == 0);
15679f988b79SJean-Baptiste Boric 	if (ump->metadatamirror_node) {
15689f988b79SJean-Baptiste Boric 		if (ump->metadata_flags & METADATA_DUPLICATED) {
15699f988b79SJean-Baptiste Boric 			err = udf_shrink_node(ump->metadatamirror_node, inf_len);
15709f988b79SJean-Baptiste Boric 		} else {
15719f988b79SJean-Baptiste Boric 			/* extents will be copied on writeout */
15729f988b79SJean-Baptiste Boric 		}
15739f988b79SJean-Baptiste Boric 		KASSERT(err == 0);
15749f988b79SJean-Baptiste Boric 	}
15759f988b79SJean-Baptiste Boric 	ump->lvclose |= UDF_WRITE_METAPART_NODES;
15769f988b79SJean-Baptiste Boric 
15779f988b79SJean-Baptiste Boric 	/* relock before exit */
15789f988b79SJean-Baptiste Boric 	mutex_enter(&ump->allocate_mutex);
15799f988b79SJean-Baptiste Boric 
15809f988b79SJean-Baptiste Boric 	if (to_trunc > num_lb)
15819f988b79SJean-Baptiste Boric 		return 0;
15829f988b79SJean-Baptiste Boric 	return num_lb - to_trunc;
15839f988b79SJean-Baptiste Boric }
15849f988b79SJean-Baptiste Boric 
15859f988b79SJean-Baptiste Boric 
15869f988b79SJean-Baptiste Boric static void
udf_sparsify_metadatapart(struct udf_mount * ump,uint32_t num_lb)15879f988b79SJean-Baptiste Boric udf_sparsify_metadatapart(struct udf_mount *ump, uint32_t num_lb)
15889f988b79SJean-Baptiste Boric {
15899f988b79SJean-Baptiste Boric 	/* NOT IMPLEMENTED, fail */
15909f988b79SJean-Baptiste Boric }
15919f988b79SJean-Baptiste Boric 
15929f988b79SJean-Baptiste Boric 
15939f988b79SJean-Baptiste Boric static void
udf_collect_free_space_for_vpart(struct udf_mount * ump,uint16_t vpart_num,uint32_t num_lb)15949f988b79SJean-Baptiste Boric udf_collect_free_space_for_vpart(struct udf_mount *ump,
15959f988b79SJean-Baptiste Boric 	uint16_t vpart_num, uint32_t num_lb)
15969f988b79SJean-Baptiste Boric {
15979f988b79SJean-Baptiste Boric 	/* allocate mutex is helt */
15989f988b79SJean-Baptiste Boric 
15999f988b79SJean-Baptiste Boric 	/* only defined for metadata partitions */
16009f988b79SJean-Baptiste Boric 	if (ump->vtop_tp[ump->node_part] != UDF_VTOP_TYPE_META) {
16019f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("\tcan't grow/shrink; no metadata partitioning\n"));
16029f988b79SJean-Baptiste Boric 		return;
16039f988b79SJean-Baptiste Boric 	}
16049f988b79SJean-Baptiste Boric 
16059f988b79SJean-Baptiste Boric 	/* UDF 2.60 BD-R+POW? */
16069f988b79SJean-Baptiste Boric 	if (ump->vtop_alloc[ump->node_part] == UDF_ALLOC_METASEQUENTIAL) {
16079f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("\tUDF 2.60 BD-R+POW track grow not implemented yet\n"));
16089f988b79SJean-Baptiste Boric 		return;
16099f988b79SJean-Baptiste Boric 	}
16109f988b79SJean-Baptiste Boric 
16119f988b79SJean-Baptiste Boric 	if (ump->vtop_tp[vpart_num] == UDF_VTOP_TYPE_META) {
16129f988b79SJean-Baptiste Boric 		/* try to grow the meta partition */
16139f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("\ttrying to grow the meta partition\n"));
16149f988b79SJean-Baptiste Boric 		/* as per [UDF 2.60/2.2.13.5] : extend bitmap and metadata file(s) */
16159f988b79SJean-Baptiste Boric 		DPRINTF(NOTIMPL, ("\tgrowing meta partition not implemented yet\n"));
16169f988b79SJean-Baptiste Boric 	} else {
16179f988b79SJean-Baptiste Boric 		/* try to shrink the metadata partition */
16189f988b79SJean-Baptiste Boric 		DPRINTF(RESERVE, ("\ttrying to shrink the meta partition\n"));
16199f988b79SJean-Baptiste Boric 		/* as per [UDF 2.60/2.2.13.6] : either trunc or make sparse */
16209f988b79SJean-Baptiste Boric 		num_lb = udf_trunc_metadatapart(ump, num_lb);
16219f988b79SJean-Baptiste Boric 		if (num_lb)
16229f988b79SJean-Baptiste Boric 			udf_sparsify_metadatapart(ump, num_lb);
16239f988b79SJean-Baptiste Boric 	}
16249f988b79SJean-Baptiste Boric 
16259f988b79SJean-Baptiste Boric 	/* allocate mutex should still be helt */
16269f988b79SJean-Baptiste Boric }
16279f988b79SJean-Baptiste Boric 
16289f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
16299f988b79SJean-Baptiste Boric 
16309f988b79SJean-Baptiste Boric /*
16319f988b79SJean-Baptiste Boric  * Allocate a buf on disc for direct write out. The space doesn't have to be
16329f988b79SJean-Baptiste Boric  * contiguous as the caller takes care of this.
16339f988b79SJean-Baptiste Boric  */
16349f988b79SJean-Baptiste Boric 
16359f988b79SJean-Baptiste Boric void
udf_late_allocate_buf(struct udf_mount * ump,struct buf * buf,uint64_t * lmapping,struct long_ad * node_ad_cpy,uint16_t * vpart_nump)16369f988b79SJean-Baptiste Boric udf_late_allocate_buf(struct udf_mount *ump, struct buf *buf,
16379f988b79SJean-Baptiste Boric 	uint64_t *lmapping, struct long_ad *node_ad_cpy, uint16_t *vpart_nump)
16389f988b79SJean-Baptiste Boric {
16399f988b79SJean-Baptiste Boric 	struct udf_node  *udf_node = VTOI(buf->b_vp);
16409f988b79SJean-Baptiste Boric 	int lb_size, udf_c_type;
16419f988b79SJean-Baptiste Boric 	int vpart_num, num_lb;
16429f988b79SJean-Baptiste Boric 	int error, s;
16439f988b79SJean-Baptiste Boric 
16449f988b79SJean-Baptiste Boric 	/*
16459f988b79SJean-Baptiste Boric 	 * for each sector in the buf, allocate a sector on disc and record
16469f988b79SJean-Baptiste Boric 	 * its position in the provided mapping array.
16479f988b79SJean-Baptiste Boric 	 *
16489f988b79SJean-Baptiste Boric 	 * If its userdata or FIDs, record its location in its node.
16499f988b79SJean-Baptiste Boric 	 */
16509f988b79SJean-Baptiste Boric 
16519f988b79SJean-Baptiste Boric 	lb_size    = udf_rw32(ump->logical_vol->lb_size);
16529f988b79SJean-Baptiste Boric 	num_lb     = (buf->b_bcount + lb_size -1) / lb_size;
16539f988b79SJean-Baptiste Boric 	udf_c_type = buf->b_udf_c_type;
16549f988b79SJean-Baptiste Boric 
16559f988b79SJean-Baptiste Boric 	KASSERT(lb_size == ump->discinfo.sector_size);
16569f988b79SJean-Baptiste Boric 
16579f988b79SJean-Baptiste Boric 	/* select partition to record the buffer on */
16589f988b79SJean-Baptiste Boric 	vpart_num = *vpart_nump = udf_get_record_vpart(ump, udf_c_type);
16599f988b79SJean-Baptiste Boric 
16609f988b79SJean-Baptiste Boric 	if (udf_c_type == UDF_C_NODE) {
16619f988b79SJean-Baptiste Boric 		/* if not VAT, its allready allocated */
16629f988b79SJean-Baptiste Boric 		if (ump->vtop_alloc[ump->node_part] != UDF_ALLOC_VAT)
16639f988b79SJean-Baptiste Boric 			return;
16649f988b79SJean-Baptiste Boric 
16659f988b79SJean-Baptiste Boric 		/* allocate on its backing sequential partition */
16669f988b79SJean-Baptiste Boric 		vpart_num = ump->data_part;
16679f988b79SJean-Baptiste Boric 	}
16689f988b79SJean-Baptiste Boric 
16699f988b79SJean-Baptiste Boric 	/* XXX can this still happen? */
16709f988b79SJean-Baptiste Boric 	/* do allocation on the selected partition */
16719f988b79SJean-Baptiste Boric 	error = udf_allocate_space(ump, udf_node, udf_c_type,
16729f988b79SJean-Baptiste Boric 			vpart_num, num_lb, lmapping);
16739f988b79SJean-Baptiste Boric 	if (error) {
16749f988b79SJean-Baptiste Boric 		/*
16759f988b79SJean-Baptiste Boric 		 * ARGH! we haven't done our accounting right! it should
16769f988b79SJean-Baptiste Boric 		 * allways succeed.
16779f988b79SJean-Baptiste Boric 		 */
16789f988b79SJean-Baptiste Boric 		panic("UDF disc allocation accounting gone wrong");
16799f988b79SJean-Baptiste Boric 	}
16809f988b79SJean-Baptiste Boric 
16819f988b79SJean-Baptiste Boric 	/* If its userdata or FIDs, record its allocation in its node. */
16829f988b79SJean-Baptiste Boric 	if ((udf_c_type == UDF_C_USERDATA) ||
16839f988b79SJean-Baptiste Boric 	    (udf_c_type == UDF_C_FIDS) ||
16849f988b79SJean-Baptiste Boric 	    (udf_c_type == UDF_C_METADATA_SBM))
16859f988b79SJean-Baptiste Boric 	{
16869f988b79SJean-Baptiste Boric 		udf_record_allocation_in_node(ump, buf, vpart_num, lmapping,
16879f988b79SJean-Baptiste Boric 			node_ad_cpy);
16889f988b79SJean-Baptiste Boric 		/* decrement our outstanding bufs counter */
16899f988b79SJean-Baptiste Boric 		s = splbio();
16909f988b79SJean-Baptiste Boric 			udf_node->outstanding_bufs--;
16919f988b79SJean-Baptiste Boric 		splx(s);
16929f988b79SJean-Baptiste Boric 	}
16939f988b79SJean-Baptiste Boric }
16949f988b79SJean-Baptiste Boric 
16959f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
16969f988b79SJean-Baptiste Boric 
16979f988b79SJean-Baptiste Boric /*
16989f988b79SJean-Baptiste Boric  * Try to merge a1 with the new piece a2. udf_ads_merge returns error when not
16999f988b79SJean-Baptiste Boric  * possible (anymore); a2 returns the rest piece.
17009f988b79SJean-Baptiste Boric  */
17019f988b79SJean-Baptiste Boric 
17029f988b79SJean-Baptiste Boric static int
udf_ads_merge(uint32_t max_len,uint32_t lb_size,struct long_ad * a1,struct long_ad * a2)17039f988b79SJean-Baptiste Boric udf_ads_merge(uint32_t max_len, uint32_t lb_size, struct long_ad *a1, struct long_ad *a2)
17049f988b79SJean-Baptiste Boric {
17059f988b79SJean-Baptiste Boric 	uint32_t merge_len;
17069f988b79SJean-Baptiste Boric 	uint32_t a1_len, a2_len;
17079f988b79SJean-Baptiste Boric 	uint32_t a1_flags, a2_flags;
17089f988b79SJean-Baptiste Boric 	uint32_t a1_lbnum, a2_lbnum;
17099f988b79SJean-Baptiste Boric 	uint16_t a1_part, a2_part;
17109f988b79SJean-Baptiste Boric 
17119f988b79SJean-Baptiste Boric 	a1_flags = UDF_EXT_FLAGS(udf_rw32(a1->len));
17129f988b79SJean-Baptiste Boric 	a1_len   = UDF_EXT_LEN(udf_rw32(a1->len));
17139f988b79SJean-Baptiste Boric 	a1_lbnum = udf_rw32(a1->loc.lb_num);
17149f988b79SJean-Baptiste Boric 	a1_part  = udf_rw16(a1->loc.part_num);
17159f988b79SJean-Baptiste Boric 
17169f988b79SJean-Baptiste Boric 	a2_flags = UDF_EXT_FLAGS(udf_rw32(a2->len));
17179f988b79SJean-Baptiste Boric 	a2_len   = UDF_EXT_LEN(udf_rw32(a2->len));
17189f988b79SJean-Baptiste Boric 	a2_lbnum = udf_rw32(a2->loc.lb_num);
17199f988b79SJean-Baptiste Boric 	a2_part  = udf_rw16(a2->loc.part_num);
17209f988b79SJean-Baptiste Boric 
17219f988b79SJean-Baptiste Boric 	/* defines same space */
17229f988b79SJean-Baptiste Boric 	if (a1_flags != a2_flags)
17239f988b79SJean-Baptiste Boric 		return 1;
17249f988b79SJean-Baptiste Boric 
17259f988b79SJean-Baptiste Boric 	if (a1_flags != UDF_EXT_FREE) {
17269f988b79SJean-Baptiste Boric 		/* the same partition */
17279f988b79SJean-Baptiste Boric 		if (a1_part != a2_part)
17289f988b79SJean-Baptiste Boric 			return 1;
17299f988b79SJean-Baptiste Boric 
17309f988b79SJean-Baptiste Boric 		/* a2 is successor of a1 */
17319f988b79SJean-Baptiste Boric 		if (a1_lbnum * lb_size + a1_len != a2_lbnum * lb_size)
17329f988b79SJean-Baptiste Boric 			return 1;
17339f988b79SJean-Baptiste Boric 	}
17349f988b79SJean-Baptiste Boric 
17359f988b79SJean-Baptiste Boric 	/* merge as most from a2 if possible */
17369f988b79SJean-Baptiste Boric 	merge_len = MIN(a2_len, max_len - a1_len);
17379f988b79SJean-Baptiste Boric 	a1_len   += merge_len;
17389f988b79SJean-Baptiste Boric 	a2_len   -= merge_len;
17399f988b79SJean-Baptiste Boric 	a2_lbnum += merge_len/lb_size;
17409f988b79SJean-Baptiste Boric 
17419f988b79SJean-Baptiste Boric 	a1->len = udf_rw32(a1_len | a1_flags);
17429f988b79SJean-Baptiste Boric 	a2->len = udf_rw32(a2_len | a2_flags);
17439f988b79SJean-Baptiste Boric 	a2->loc.lb_num = udf_rw32(a2_lbnum);
17449f988b79SJean-Baptiste Boric 
17459f988b79SJean-Baptiste Boric 	if (a2_len > 0)
17469f988b79SJean-Baptiste Boric 		return 1;
17479f988b79SJean-Baptiste Boric 
17489f988b79SJean-Baptiste Boric 	/* there is space over to merge */
17499f988b79SJean-Baptiste Boric 	return 0;
17509f988b79SJean-Baptiste Boric }
17519f988b79SJean-Baptiste Boric 
17529f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
17539f988b79SJean-Baptiste Boric 
17549f988b79SJean-Baptiste Boric static void
udf_wipe_adslots(struct udf_node * udf_node)17559f988b79SJean-Baptiste Boric udf_wipe_adslots(struct udf_node *udf_node)
17569f988b79SJean-Baptiste Boric {
17579f988b79SJean-Baptiste Boric 	struct file_entry      *fe;
17589f988b79SJean-Baptiste Boric 	struct extfile_entry   *efe;
17599f988b79SJean-Baptiste Boric 	struct alloc_ext_entry *ext;
17609f988b79SJean-Baptiste Boric 	uint32_t lb_size, dscr_size, l_ea, max_l_ad, crclen;
17619f988b79SJean-Baptiste Boric 	uint8_t *data_pos;
17629f988b79SJean-Baptiste Boric 	int extnr;
17639f988b79SJean-Baptiste Boric 
17649f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
17659f988b79SJean-Baptiste Boric 
17669f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
17679f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
17689f988b79SJean-Baptiste Boric 	if (fe) {
17699f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct file_entry) -1;
17709f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(fe->l_ea);
17719f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) fe + dscr_size + l_ea;
17729f988b79SJean-Baptiste Boric 	} else {
17739f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct extfile_entry) -1;
17749f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(efe->l_ea);
17759f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) efe + dscr_size + l_ea;
17769f988b79SJean-Baptiste Boric 	}
17779f988b79SJean-Baptiste Boric 	max_l_ad = lb_size - dscr_size - l_ea;
17789f988b79SJean-Baptiste Boric 
17799f988b79SJean-Baptiste Boric 	/* wipe fe/efe */
17809f988b79SJean-Baptiste Boric 	memset(data_pos, 0, max_l_ad);
17819f988b79SJean-Baptiste Boric 	crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea;
17829f988b79SJean-Baptiste Boric 	if (fe) {
17839f988b79SJean-Baptiste Boric 		fe->l_ad         = udf_rw32(0);
17849f988b79SJean-Baptiste Boric 		fe->logblks_rec  = udf_rw64(0);
17859f988b79SJean-Baptiste Boric 		fe->tag.desc_crc_len = udf_rw16(crclen);
17869f988b79SJean-Baptiste Boric 	} else {
17879f988b79SJean-Baptiste Boric 		efe->l_ad        = udf_rw32(0);
17889f988b79SJean-Baptiste Boric 		efe->logblks_rec = udf_rw64(0);
17899f988b79SJean-Baptiste Boric 		efe->tag.desc_crc_len = udf_rw16(crclen);
17909f988b79SJean-Baptiste Boric 	}
17919f988b79SJean-Baptiste Boric 
17929f988b79SJean-Baptiste Boric 	/* wipe all allocation extent entries */
17939f988b79SJean-Baptiste Boric 	for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
17949f988b79SJean-Baptiste Boric 		ext = udf_node->ext[extnr];
17959f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
17969f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) ext->data;
17979f988b79SJean-Baptiste Boric 		max_l_ad = lb_size - dscr_size;
17989f988b79SJean-Baptiste Boric 		memset(data_pos, 0, max_l_ad);
17999f988b79SJean-Baptiste Boric 		ext->l_ad = udf_rw32(0);
18009f988b79SJean-Baptiste Boric 
18019f988b79SJean-Baptiste Boric 		crclen = dscr_size - UDF_DESC_TAG_LENGTH;
18029f988b79SJean-Baptiste Boric 		ext->tag.desc_crc_len = udf_rw16(crclen);
18039f988b79SJean-Baptiste Boric 	}
18049f988b79SJean-Baptiste Boric 	udf_node->i_flags |= IN_NODE_REBUILD;
18059f988b79SJean-Baptiste Boric }
18069f988b79SJean-Baptiste Boric 
18079f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
18089f988b79SJean-Baptiste Boric 
18099f988b79SJean-Baptiste Boric void
udf_get_adslot(struct udf_node * udf_node,int slot,struct long_ad * icb,int * eof)18109f988b79SJean-Baptiste Boric udf_get_adslot(struct udf_node *udf_node, int slot, struct long_ad *icb,
18119f988b79SJean-Baptiste Boric 	int *eof) {
18129f988b79SJean-Baptiste Boric 	struct file_entry      *fe;
18139f988b79SJean-Baptiste Boric 	struct extfile_entry   *efe;
18149f988b79SJean-Baptiste Boric 	struct alloc_ext_entry *ext;
18159f988b79SJean-Baptiste Boric 	struct icb_tag *icbtag;
18169f988b79SJean-Baptiste Boric 	struct short_ad *short_ad;
18179f988b79SJean-Baptiste Boric 	struct long_ad *long_ad, l_icb;
18189f988b79SJean-Baptiste Boric 	uint32_t offset;
18199f988b79SJean-Baptiste Boric 	uint32_t dscr_size, l_ea, l_ad, flags;
18209f988b79SJean-Baptiste Boric 	uint8_t *data_pos;
18219f988b79SJean-Baptiste Boric 	int icbflags, addr_type, adlen, extnr;
18229f988b79SJean-Baptiste Boric 
18239f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
18249f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
18259f988b79SJean-Baptiste Boric 	if (fe) {
18269f988b79SJean-Baptiste Boric 		icbtag  = &fe->icbtag;
18279f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct file_entry) -1;
18289f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(fe->l_ea);
18299f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(fe->l_ad);
18309f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) fe + dscr_size + l_ea;
18319f988b79SJean-Baptiste Boric 	} else {
18329f988b79SJean-Baptiste Boric 		icbtag  = &efe->icbtag;
18339f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct extfile_entry) -1;
18349f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(efe->l_ea);
18359f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(efe->l_ad);
18369f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) efe + dscr_size + l_ea;
18379f988b79SJean-Baptiste Boric 	}
18389f988b79SJean-Baptiste Boric 
18399f988b79SJean-Baptiste Boric 	icbflags  = udf_rw16(icbtag->flags);
18409f988b79SJean-Baptiste Boric 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
18419f988b79SJean-Baptiste Boric 
18429f988b79SJean-Baptiste Boric 	/* just in case we're called on an intern, its EOF */
18439f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
18449f988b79SJean-Baptiste Boric 		memset(icb, 0, sizeof(struct long_ad));
18459f988b79SJean-Baptiste Boric 		*eof = 1;
18469f988b79SJean-Baptiste Boric 		return;
18479f988b79SJean-Baptiste Boric 	}
18489f988b79SJean-Baptiste Boric 
18499f988b79SJean-Baptiste Boric 	adlen = 0;
18509f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
18519f988b79SJean-Baptiste Boric 		adlen = sizeof(struct short_ad);
18529f988b79SJean-Baptiste Boric 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
18539f988b79SJean-Baptiste Boric 		adlen = sizeof(struct long_ad);
18549f988b79SJean-Baptiste Boric 	}
18559f988b79SJean-Baptiste Boric 
18569f988b79SJean-Baptiste Boric 	/* if offset too big, we go to the allocation extensions */
18579f988b79SJean-Baptiste Boric 	offset = slot * adlen;
18589f988b79SJean-Baptiste Boric 	extnr  = -1;
18599f988b79SJean-Baptiste Boric 	while (offset >= l_ad) {
18609f988b79SJean-Baptiste Boric 		/* check if our last entry is a redirect */
18619f988b79SJean-Baptiste Boric 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
18629f988b79SJean-Baptiste Boric 			short_ad = (struct short_ad *) (data_pos + l_ad-adlen);
18639f988b79SJean-Baptiste Boric 			l_icb.len          = short_ad->len;
18649f988b79SJean-Baptiste Boric 			l_icb.loc.part_num = udf_node->loc.loc.part_num;
18659f988b79SJean-Baptiste Boric 			l_icb.loc.lb_num   = short_ad->lb_num;
18669f988b79SJean-Baptiste Boric 		} else {
18679f988b79SJean-Baptiste Boric 			KASSERT(addr_type == UDF_ICB_LONG_ALLOC);
18689f988b79SJean-Baptiste Boric 			long_ad = (struct long_ad *) (data_pos + l_ad-adlen);
18699f988b79SJean-Baptiste Boric 			l_icb = *long_ad;
18709f988b79SJean-Baptiste Boric 		}
18719f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len));
18729f988b79SJean-Baptiste Boric 		if (flags != UDF_EXT_REDIRECT) {
18739f988b79SJean-Baptiste Boric 			l_ad = 0;	/* force EOF */
18749f988b79SJean-Baptiste Boric 			break;
18759f988b79SJean-Baptiste Boric 		}
18769f988b79SJean-Baptiste Boric 
18779f988b79SJean-Baptiste Boric 		/* advance to next extent */
18789f988b79SJean-Baptiste Boric 		extnr++;
18799f988b79SJean-Baptiste Boric 		if (extnr >= udf_node->num_extensions) {
18809f988b79SJean-Baptiste Boric 			l_ad = 0;	/* force EOF */
18819f988b79SJean-Baptiste Boric 			break;
18829f988b79SJean-Baptiste Boric 		}
18839f988b79SJean-Baptiste Boric 		offset = offset - l_ad;
18849f988b79SJean-Baptiste Boric 		ext  = udf_node->ext[extnr];
18859f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
18869f988b79SJean-Baptiste Boric 		l_ad = udf_rw32(ext->l_ad);
18879f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) ext + dscr_size;
18889f988b79SJean-Baptiste Boric 	}
18899f988b79SJean-Baptiste Boric 
18909f988b79SJean-Baptiste Boric 	/* XXX l_ad == 0 should be enough to check */
18919f988b79SJean-Baptiste Boric 	*eof = (offset >= l_ad) || (l_ad == 0);
18929f988b79SJean-Baptiste Boric 	if (*eof) {
18939f988b79SJean-Baptiste Boric 		DPRINTF(PARANOIDADWLK, ("returning EOF, extnr %d, offset %d, "
18949f988b79SJean-Baptiste Boric 			"l_ad %d\n", extnr, offset, l_ad));
18959f988b79SJean-Baptiste Boric 		memset(icb, 0, sizeof(struct long_ad));
18969f988b79SJean-Baptiste Boric 		return;
18979f988b79SJean-Baptiste Boric 	}
18989f988b79SJean-Baptiste Boric 
18999f988b79SJean-Baptiste Boric 	/* get the element */
19009f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
19019f988b79SJean-Baptiste Boric 		short_ad = (struct short_ad *) (data_pos + offset);
19029f988b79SJean-Baptiste Boric 		icb->len          = short_ad->len;
19039f988b79SJean-Baptiste Boric 		icb->loc.part_num = udf_node->loc.loc.part_num;
19049f988b79SJean-Baptiste Boric 		icb->loc.lb_num   = short_ad->lb_num;
19059f988b79SJean-Baptiste Boric 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
19069f988b79SJean-Baptiste Boric 		long_ad = (struct long_ad *) (data_pos + offset);
19079f988b79SJean-Baptiste Boric 		*icb = *long_ad;
19089f988b79SJean-Baptiste Boric 	}
19099f988b79SJean-Baptiste Boric 	DPRINTF(PARANOIDADWLK, ("returning element : v %d, lb %d, len %d, "
19109f988b79SJean-Baptiste Boric 		"flags %d\n", icb->loc.part_num, icb->loc.lb_num,
19119f988b79SJean-Baptiste Boric 		UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len)));
19129f988b79SJean-Baptiste Boric }
19139f988b79SJean-Baptiste Boric 
19149f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
19159f988b79SJean-Baptiste Boric 
19169f988b79SJean-Baptiste Boric int
udf_append_adslot(struct udf_node * udf_node,int * slot,struct long_ad * icb)19179f988b79SJean-Baptiste Boric udf_append_adslot(struct udf_node *udf_node, int *slot, struct long_ad *icb) {
19189f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
19199f988b79SJean-Baptiste Boric 	union dscrptr          *dscr, *extdscr;
19209f988b79SJean-Baptiste Boric 	struct file_entry      *fe;
19219f988b79SJean-Baptiste Boric 	struct extfile_entry   *efe;
19229f988b79SJean-Baptiste Boric 	struct alloc_ext_entry *ext;
19239f988b79SJean-Baptiste Boric 	struct icb_tag *icbtag;
19249f988b79SJean-Baptiste Boric 	struct short_ad *short_ad;
19259f988b79SJean-Baptiste Boric 	struct long_ad *long_ad, o_icb, l_icb;
19269f988b79SJean-Baptiste Boric 	uint64_t logblks_rec, *logblks_rec_p;
19279f988b79SJean-Baptiste Boric 	uint64_t lmapping;
19289f988b79SJean-Baptiste Boric 	uint32_t offset, rest, len, lb_num;
19299f988b79SJean-Baptiste Boric 	uint32_t lb_size, dscr_size, l_ea, l_ad, *l_ad_p, max_l_ad, crclen;
19309f988b79SJean-Baptiste Boric 	uint32_t flags;
19319f988b79SJean-Baptiste Boric 	uint16_t vpart_num;
19329f988b79SJean-Baptiste Boric 	uint8_t *data_pos;
19339f988b79SJean-Baptiste Boric 	int icbflags, addr_type, adlen, extnr;
19349f988b79SJean-Baptiste Boric 	int error;
19359f988b79SJean-Baptiste Boric 
19369f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
19379f988b79SJean-Baptiste Boric 	vpart_num = udf_rw16(udf_node->loc.loc.part_num);
19389f988b79SJean-Baptiste Boric 
19399f988b79SJean-Baptiste Boric 	/* determine what descriptor we are in */
19409f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
19419f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
19429f988b79SJean-Baptiste Boric 	if (fe) {
19439f988b79SJean-Baptiste Boric 		icbtag  = &fe->icbtag;
19449f988b79SJean-Baptiste Boric 		dscr      = (union dscrptr *) fe;
19459f988b79SJean-Baptiste Boric 		dscr_size = sizeof(struct file_entry) -1;
19469f988b79SJean-Baptiste Boric 
19479f988b79SJean-Baptiste Boric 		l_ea      = udf_rw32(fe->l_ea);
19489f988b79SJean-Baptiste Boric 		l_ad_p    = &fe->l_ad;
19499f988b79SJean-Baptiste Boric 		logblks_rec_p = &fe->logblks_rec;
19509f988b79SJean-Baptiste Boric 	} else {
19519f988b79SJean-Baptiste Boric 		icbtag    = &efe->icbtag;
19529f988b79SJean-Baptiste Boric 		dscr      = (union dscrptr *) efe;
19539f988b79SJean-Baptiste Boric 		dscr_size = sizeof(struct extfile_entry) -1;
19549f988b79SJean-Baptiste Boric 
19559f988b79SJean-Baptiste Boric 		l_ea      = udf_rw32(efe->l_ea);
19569f988b79SJean-Baptiste Boric 		l_ad_p    = &efe->l_ad;
19579f988b79SJean-Baptiste Boric 		logblks_rec_p = &efe->logblks_rec;
19589f988b79SJean-Baptiste Boric 	}
19599f988b79SJean-Baptiste Boric 	data_pos  = (uint8_t *) dscr + dscr_size + l_ea;
19609f988b79SJean-Baptiste Boric 	max_l_ad = lb_size - dscr_size - l_ea;
19619f988b79SJean-Baptiste Boric 
19629f988b79SJean-Baptiste Boric 	icbflags  = udf_rw16(icbtag->flags);
19639f988b79SJean-Baptiste Boric 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
19649f988b79SJean-Baptiste Boric 
19659f988b79SJean-Baptiste Boric 	/* just in case we're called on an intern, its EOF */
19669f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
19679f988b79SJean-Baptiste Boric 		panic("udf_append_adslot on UDF_ICB_INTERN_ALLOC\n");
19689f988b79SJean-Baptiste Boric 	}
19699f988b79SJean-Baptiste Boric 
19709f988b79SJean-Baptiste Boric 	adlen = 0;
19719f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
19729f988b79SJean-Baptiste Boric 		adlen = sizeof(struct short_ad);
19739f988b79SJean-Baptiste Boric 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
19749f988b79SJean-Baptiste Boric 		adlen = sizeof(struct long_ad);
19759f988b79SJean-Baptiste Boric 	}
19769f988b79SJean-Baptiste Boric 
19779f988b79SJean-Baptiste Boric 	/* clean up given long_ad since it can be a synthesized one */
19789f988b79SJean-Baptiste Boric 	flags = UDF_EXT_FLAGS(udf_rw32(icb->len));
19799f988b79SJean-Baptiste Boric 	if (flags == UDF_EXT_FREE) {
19809f988b79SJean-Baptiste Boric 		icb->loc.part_num = udf_rw16(0);
19819f988b79SJean-Baptiste Boric 		icb->loc.lb_num   = udf_rw32(0);
19829f988b79SJean-Baptiste Boric 	}
19839f988b79SJean-Baptiste Boric 
19849f988b79SJean-Baptiste Boric 	/* if offset too big, we go to the allocation extensions */
19859f988b79SJean-Baptiste Boric 	l_ad   = udf_rw32(*l_ad_p);
19869f988b79SJean-Baptiste Boric 	offset = (*slot) * adlen;
19879f988b79SJean-Baptiste Boric 	extnr  = -1;
19889f988b79SJean-Baptiste Boric 	while (offset >= l_ad) {
19899f988b79SJean-Baptiste Boric 		/* check if our last entry is a redirect */
19909f988b79SJean-Baptiste Boric 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
19919f988b79SJean-Baptiste Boric 			short_ad = (struct short_ad *) (data_pos + l_ad-adlen);
19929f988b79SJean-Baptiste Boric 			l_icb.len          = short_ad->len;
19939f988b79SJean-Baptiste Boric 			l_icb.loc.part_num = udf_node->loc.loc.part_num;
19949f988b79SJean-Baptiste Boric 			l_icb.loc.lb_num   = short_ad->lb_num;
19959f988b79SJean-Baptiste Boric 		} else {
19969f988b79SJean-Baptiste Boric 			KASSERT(addr_type == UDF_ICB_LONG_ALLOC);
19979f988b79SJean-Baptiste Boric 			long_ad = (struct long_ad *) (data_pos + l_ad-adlen);
19989f988b79SJean-Baptiste Boric 			l_icb = *long_ad;
19999f988b79SJean-Baptiste Boric 		}
20009f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len));
20019f988b79SJean-Baptiste Boric 		if (flags != UDF_EXT_REDIRECT) {
20029f988b79SJean-Baptiste Boric 			/* only one past the last one is adressable */
20039f988b79SJean-Baptiste Boric 			break;
20049f988b79SJean-Baptiste Boric 		}
20059f988b79SJean-Baptiste Boric 
20069f988b79SJean-Baptiste Boric 		/* advance to next extent */
20079f988b79SJean-Baptiste Boric 		extnr++;
20089f988b79SJean-Baptiste Boric 		KASSERT(extnr < udf_node->num_extensions);
20099f988b79SJean-Baptiste Boric 		offset = offset - l_ad;
20109f988b79SJean-Baptiste Boric 
20119f988b79SJean-Baptiste Boric 		ext  = udf_node->ext[extnr];
20129f988b79SJean-Baptiste Boric 		dscr = (union dscrptr *) ext;
20139f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
20149f988b79SJean-Baptiste Boric 		max_l_ad = lb_size - dscr_size;
20159f988b79SJean-Baptiste Boric 		l_ad_p = &ext->l_ad;
20169f988b79SJean-Baptiste Boric 		l_ad   = udf_rw32(*l_ad_p);
20179f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) ext + dscr_size;
20189f988b79SJean-Baptiste Boric 	}
20199f988b79SJean-Baptiste Boric 	DPRINTF(PARANOIDADWLK, ("append, ext %d, offset %d, l_ad %d\n",
20209f988b79SJean-Baptiste Boric 		extnr, offset, udf_rw32(*l_ad_p)));
20219f988b79SJean-Baptiste Boric 	KASSERT(l_ad == udf_rw32(*l_ad_p));
20229f988b79SJean-Baptiste Boric 
20239f988b79SJean-Baptiste Boric 	/* offset is offset within the current (E)FE/AED */
20249f988b79SJean-Baptiste Boric 	l_ad   = udf_rw32(*l_ad_p);
20259f988b79SJean-Baptiste Boric 	crclen = udf_rw16(dscr->tag.desc_crc_len);
20269f988b79SJean-Baptiste Boric 	logblks_rec = udf_rw64(*logblks_rec_p);
20279f988b79SJean-Baptiste Boric 
20289f988b79SJean-Baptiste Boric 	/* overwriting old piece? */
20299f988b79SJean-Baptiste Boric 	if (offset < l_ad) {
20309f988b79SJean-Baptiste Boric 		/* overwrite entry; compensate for the old element */
20319f988b79SJean-Baptiste Boric 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
20329f988b79SJean-Baptiste Boric 			short_ad = (struct short_ad *) (data_pos + offset);
20339f988b79SJean-Baptiste Boric 			o_icb.len          = short_ad->len;
20349f988b79SJean-Baptiste Boric 			o_icb.loc.part_num = udf_rw16(0);	/* ignore */
20359f988b79SJean-Baptiste Boric 			o_icb.loc.lb_num   = short_ad->lb_num;
20369f988b79SJean-Baptiste Boric 		} else if (addr_type == UDF_ICB_LONG_ALLOC) {
20379f988b79SJean-Baptiste Boric 			long_ad = (struct long_ad *) (data_pos + offset);
20389f988b79SJean-Baptiste Boric 			o_icb = *long_ad;
20399f988b79SJean-Baptiste Boric 		} else {
20409f988b79SJean-Baptiste Boric 			panic("Invalid address type in udf_append_adslot\n");
20419f988b79SJean-Baptiste Boric 		}
20429f988b79SJean-Baptiste Boric 
20439f988b79SJean-Baptiste Boric 		len = udf_rw32(o_icb.len);
20449f988b79SJean-Baptiste Boric 		if (UDF_EXT_FLAGS(len) == UDF_EXT_ALLOCATED) {
20459f988b79SJean-Baptiste Boric 			/* adjust counts */
20469f988b79SJean-Baptiste Boric 			len = UDF_EXT_LEN(len);
20479f988b79SJean-Baptiste Boric 			logblks_rec -= (len + lb_size -1) / lb_size;
20489f988b79SJean-Baptiste Boric 		}
20499f988b79SJean-Baptiste Boric 	}
20509f988b79SJean-Baptiste Boric 
20519f988b79SJean-Baptiste Boric 	/* check if we're not appending a redirection */
20529f988b79SJean-Baptiste Boric 	flags = UDF_EXT_FLAGS(udf_rw32(icb->len));
20539f988b79SJean-Baptiste Boric 	KASSERT(flags != UDF_EXT_REDIRECT);
20549f988b79SJean-Baptiste Boric 
20559f988b79SJean-Baptiste Boric 	/* round down available space */
20569f988b79SJean-Baptiste Boric 	rest = adlen * ((max_l_ad - offset) / adlen);
20579f988b79SJean-Baptiste Boric 	if (rest <= adlen) {
20589f988b79SJean-Baptiste Boric 		/* have to append aed, see if we already have a spare one */
20599f988b79SJean-Baptiste Boric 		extnr++;
20609f988b79SJean-Baptiste Boric 		ext = udf_node->ext[extnr];
20619f988b79SJean-Baptiste Boric 		l_icb = udf_node->ext_loc[extnr];
20629f988b79SJean-Baptiste Boric 		if (ext == NULL) {
20639f988b79SJean-Baptiste Boric 			DPRINTF(ALLOC,("adding allocation extent %d\n", extnr));
20649f988b79SJean-Baptiste Boric 
20659f988b79SJean-Baptiste Boric 			error = udf_reserve_space(ump, NULL, UDF_C_NODE,
20669f988b79SJean-Baptiste Boric 					vpart_num, 1, /* can fail */ false);
20679f988b79SJean-Baptiste Boric 			if (error) {
20689f988b79SJean-Baptiste Boric 				printf("UDF: couldn't reserve space for AED!\n");
20699f988b79SJean-Baptiste Boric 				return error;
20709f988b79SJean-Baptiste Boric 			}
20719f988b79SJean-Baptiste Boric 			error = udf_allocate_space(ump, NULL, UDF_C_NODE,
20729f988b79SJean-Baptiste Boric 					vpart_num, 1, &lmapping);
20739f988b79SJean-Baptiste Boric 			lb_num = lmapping;
20749f988b79SJean-Baptiste Boric 			if (error)
20759f988b79SJean-Baptiste Boric 				panic("UDF: couldn't allocate AED!\n");
20769f988b79SJean-Baptiste Boric 
20779f988b79SJean-Baptiste Boric 			/* initialise pointer to location */
20789f988b79SJean-Baptiste Boric 			memset(&l_icb, 0, sizeof(struct long_ad));
20799f988b79SJean-Baptiste Boric 			l_icb.len = udf_rw32(lb_size | UDF_EXT_REDIRECT);
20809f988b79SJean-Baptiste Boric 			l_icb.loc.lb_num   = udf_rw32(lb_num);
20819f988b79SJean-Baptiste Boric 			l_icb.loc.part_num = udf_rw16(vpart_num);
20829f988b79SJean-Baptiste Boric 
20839f988b79SJean-Baptiste Boric 			/* create new aed descriptor */
20849f988b79SJean-Baptiste Boric 			udf_create_logvol_dscr(ump, udf_node, &l_icb, &extdscr);
20859f988b79SJean-Baptiste Boric 			ext = &extdscr->aee;
20869f988b79SJean-Baptiste Boric 
20879f988b79SJean-Baptiste Boric 			udf_inittag(ump, &ext->tag, TAGID_ALLOCEXTENT, lb_num);
20889f988b79SJean-Baptiste Boric 			dscr_size  = sizeof(struct alloc_ext_entry) -1;
20899f988b79SJean-Baptiste Boric 			max_l_ad = lb_size - dscr_size;
20909f988b79SJean-Baptiste Boric 			memset(ext->data, 0, max_l_ad);
20919f988b79SJean-Baptiste Boric 			ext->l_ad = udf_rw32(0);
20929f988b79SJean-Baptiste Boric 			ext->tag.desc_crc_len =
20939f988b79SJean-Baptiste Boric 				udf_rw16(dscr_size - UDF_DESC_TAG_LENGTH);
20949f988b79SJean-Baptiste Boric 
20959f988b79SJean-Baptiste Boric 			/* declare aed */
20969f988b79SJean-Baptiste Boric 			udf_node->num_extensions++;
20979f988b79SJean-Baptiste Boric 			udf_node->ext_loc[extnr] = l_icb;
20989f988b79SJean-Baptiste Boric 			udf_node->ext[extnr] = ext;
20999f988b79SJean-Baptiste Boric 		}
21009f988b79SJean-Baptiste Boric 		/* add redirect and adjust l_ad and crclen for old descr */
21019f988b79SJean-Baptiste Boric 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
21029f988b79SJean-Baptiste Boric 			short_ad = (struct short_ad *) (data_pos + offset);
21039f988b79SJean-Baptiste Boric 			short_ad->len    = l_icb.len;
21049f988b79SJean-Baptiste Boric 			short_ad->lb_num = l_icb.loc.lb_num;
21059f988b79SJean-Baptiste Boric 		} else if (addr_type == UDF_ICB_LONG_ALLOC) {
21069f988b79SJean-Baptiste Boric 			long_ad = (struct long_ad *) (data_pos + offset);
21079f988b79SJean-Baptiste Boric 			*long_ad = l_icb;
21089f988b79SJean-Baptiste Boric 		}
21099f988b79SJean-Baptiste Boric 		l_ad   += adlen;
21109f988b79SJean-Baptiste Boric 		crclen += adlen;
21119f988b79SJean-Baptiste Boric 		dscr->tag.desc_crc_len = udf_rw16(crclen);
21129f988b79SJean-Baptiste Boric 		*l_ad_p = udf_rw32(l_ad);
21139f988b79SJean-Baptiste Boric 
21149f988b79SJean-Baptiste Boric 		/* advance to the new extension */
21159f988b79SJean-Baptiste Boric 		KASSERT(ext != NULL);
21169f988b79SJean-Baptiste Boric 		dscr = (union dscrptr *) ext;
21179f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
21189f988b79SJean-Baptiste Boric 		max_l_ad = lb_size - dscr_size;
21199f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) dscr + dscr_size;
21209f988b79SJean-Baptiste Boric 
21219f988b79SJean-Baptiste Boric 		l_ad_p = &ext->l_ad;
21229f988b79SJean-Baptiste Boric 		l_ad   = udf_rw32(*l_ad_p);
21239f988b79SJean-Baptiste Boric 		crclen = udf_rw16(dscr->tag.desc_crc_len);
21249f988b79SJean-Baptiste Boric 		offset = 0;
21259f988b79SJean-Baptiste Boric 
21269f988b79SJean-Baptiste Boric 		/* adjust callees slot count for link insert */
21279f988b79SJean-Baptiste Boric 		*slot += 1;
21289f988b79SJean-Baptiste Boric 	}
21299f988b79SJean-Baptiste Boric 
21309f988b79SJean-Baptiste Boric 	/* write out the element */
21319f988b79SJean-Baptiste Boric 	DPRINTF(PARANOIDADWLK, ("adding element : %p : v %d, lb %d, "
21329f988b79SJean-Baptiste Boric 			"len %d, flags %d\n", data_pos + offset,
21339f988b79SJean-Baptiste Boric 			icb->loc.part_num, icb->loc.lb_num,
21349f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len)));
21359f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
21369f988b79SJean-Baptiste Boric 		short_ad = (struct short_ad *) (data_pos + offset);
21379f988b79SJean-Baptiste Boric 		short_ad->len    = icb->len;
21389f988b79SJean-Baptiste Boric 		short_ad->lb_num = icb->loc.lb_num;
21399f988b79SJean-Baptiste Boric 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
21409f988b79SJean-Baptiste Boric 		long_ad = (struct long_ad *) (data_pos + offset);
21419f988b79SJean-Baptiste Boric 		*long_ad = *icb;
21429f988b79SJean-Baptiste Boric 	}
21439f988b79SJean-Baptiste Boric 
21449f988b79SJean-Baptiste Boric 	/* adjust logblks recorded count */
21459f988b79SJean-Baptiste Boric 	len = udf_rw32(icb->len);
21469f988b79SJean-Baptiste Boric 	flags = UDF_EXT_FLAGS(len);
21479f988b79SJean-Baptiste Boric 	if (flags == UDF_EXT_ALLOCATED)
21489f988b79SJean-Baptiste Boric 		logblks_rec += (UDF_EXT_LEN(len) + lb_size -1) / lb_size;
21499f988b79SJean-Baptiste Boric 	*logblks_rec_p = udf_rw64(logblks_rec);
21509f988b79SJean-Baptiste Boric 
21519f988b79SJean-Baptiste Boric 	/* adjust l_ad and crclen when needed */
21529f988b79SJean-Baptiste Boric 	if (offset >= l_ad) {
21539f988b79SJean-Baptiste Boric 		l_ad   += adlen;
21549f988b79SJean-Baptiste Boric 		crclen += adlen;
21559f988b79SJean-Baptiste Boric 		dscr->tag.desc_crc_len = udf_rw16(crclen);
21569f988b79SJean-Baptiste Boric 		*l_ad_p = udf_rw32(l_ad);
21579f988b79SJean-Baptiste Boric 	}
21589f988b79SJean-Baptiste Boric 
21599f988b79SJean-Baptiste Boric 	return 0;
21609f988b79SJean-Baptiste Boric }
21619f988b79SJean-Baptiste Boric 
21629f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
21639f988b79SJean-Baptiste Boric 
21649f988b79SJean-Baptiste Boric static void
udf_count_alloc_exts(struct udf_node * udf_node)21659f988b79SJean-Baptiste Boric udf_count_alloc_exts(struct udf_node *udf_node)
21669f988b79SJean-Baptiste Boric {
21679f988b79SJean-Baptiste Boric 	struct long_ad s_ad;
21689f988b79SJean-Baptiste Boric 	uint32_t lb_num, len, flags;
21699f988b79SJean-Baptiste Boric 	uint16_t vpart_num;
21709f988b79SJean-Baptiste Boric 	int slot, eof;
21719f988b79SJean-Baptiste Boric 	int num_extents, extnr;
21729f988b79SJean-Baptiste Boric 
21739f988b79SJean-Baptiste Boric 	if (udf_node->num_extensions == 0)
21749f988b79SJean-Baptiste Boric 		return;
21759f988b79SJean-Baptiste Boric 
21769f988b79SJean-Baptiste Boric 	/* count number of allocation extents in use */
21779f988b79SJean-Baptiste Boric 	num_extents = 0;
21789f988b79SJean-Baptiste Boric 	slot = 0;
21799f988b79SJean-Baptiste Boric 	for (;;) {
21809f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
21819f988b79SJean-Baptiste Boric 		if (eof)
21829f988b79SJean-Baptiste Boric 			break;
21839f988b79SJean-Baptiste Boric 		len   = udf_rw32(s_ad.len);
21849f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(len);
21859f988b79SJean-Baptiste Boric 
21869f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT)
21879f988b79SJean-Baptiste Boric 			num_extents++;
21889f988b79SJean-Baptiste Boric 
21899f988b79SJean-Baptiste Boric 		slot++;
21909f988b79SJean-Baptiste Boric 	}
21919f988b79SJean-Baptiste Boric 
21929f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("udf_count_alloc_ext counted %d live extents\n",
21939f988b79SJean-Baptiste Boric 		num_extents));
21949f988b79SJean-Baptiste Boric 
21959f988b79SJean-Baptiste Boric 	/* XXX choice: we could delay freeing them on node writeout */
21969f988b79SJean-Baptiste Boric 	/* free excess entries */
21979f988b79SJean-Baptiste Boric 	extnr = num_extents;
21989f988b79SJean-Baptiste Boric 	for (;extnr < udf_node->num_extensions; extnr++) {
21999f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("freeing alloc ext %d\n", extnr));
22009f988b79SJean-Baptiste Boric 		/* free dscriptor */
22019f988b79SJean-Baptiste Boric 		s_ad = udf_node->ext_loc[extnr];
22029f988b79SJean-Baptiste Boric 		udf_free_logvol_dscr(udf_node->ump, &s_ad,
22039f988b79SJean-Baptiste Boric 			udf_node->ext[extnr]);
22049f988b79SJean-Baptiste Boric 		udf_node->ext[extnr] = NULL;
22059f988b79SJean-Baptiste Boric 
22069f988b79SJean-Baptiste Boric 		/* free disc space */
22079f988b79SJean-Baptiste Boric 		lb_num    = udf_rw32(s_ad.loc.lb_num);
22089f988b79SJean-Baptiste Boric 		vpart_num = udf_rw16(s_ad.loc.part_num);
22099f988b79SJean-Baptiste Boric 		udf_free_allocated_space(udf_node->ump, lb_num, vpart_num, 1);
22109f988b79SJean-Baptiste Boric 
22119f988b79SJean-Baptiste Boric 		memset(&udf_node->ext_loc[extnr], 0, sizeof(struct long_ad));
22129f988b79SJean-Baptiste Boric 	}
22139f988b79SJean-Baptiste Boric 
22149f988b79SJean-Baptiste Boric 	/* set our new number of allocation extents */
22159f988b79SJean-Baptiste Boric 	udf_node->num_extensions = num_extents;
22169f988b79SJean-Baptiste Boric }
22179f988b79SJean-Baptiste Boric 
22189f988b79SJean-Baptiste Boric 
22199f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
22209f988b79SJean-Baptiste Boric 
22219f988b79SJean-Baptiste Boric /*
22229f988b79SJean-Baptiste Boric  * Adjust the node's allocation descriptors to reflect the new mapping; do
22239f988b79SJean-Baptiste Boric  * take note that we might glue to existing allocation descriptors.
22249f988b79SJean-Baptiste Boric  *
22259f988b79SJean-Baptiste Boric  * XXX Note there can only be one allocation being recorded/mount; maybe
22269f988b79SJean-Baptiste Boric  * explicit allocation in shedule thread?
22279f988b79SJean-Baptiste Boric  */
22289f988b79SJean-Baptiste Boric 
22299f988b79SJean-Baptiste Boric static void
udf_record_allocation_in_node(struct udf_mount * ump,struct buf * buf,uint16_t vpart_num,uint64_t * mapping,struct long_ad * node_ad_cpy)22309f988b79SJean-Baptiste Boric udf_record_allocation_in_node(struct udf_mount *ump, struct buf *buf,
22319f988b79SJean-Baptiste Boric 	uint16_t vpart_num, uint64_t *mapping, struct long_ad *node_ad_cpy)
22329f988b79SJean-Baptiste Boric {
22339f988b79SJean-Baptiste Boric 	struct vnode    *vp = buf->b_vp;
22349f988b79SJean-Baptiste Boric 	struct udf_node *udf_node = VTOI(vp);
22359f988b79SJean-Baptiste Boric 	struct file_entry      *fe;
22369f988b79SJean-Baptiste Boric 	struct extfile_entry   *efe;
22379f988b79SJean-Baptiste Boric 	struct icb_tag  *icbtag;
22389f988b79SJean-Baptiste Boric 	struct long_ad   s_ad, c_ad;
22399f988b79SJean-Baptiste Boric 	uint64_t inflen, from, till;
22409f988b79SJean-Baptiste Boric 	uint64_t foffset, end_foffset, restart_foffset;
22419f988b79SJean-Baptiste Boric 	uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
22429f988b79SJean-Baptiste Boric 	uint32_t max_len;
22439f988b79SJean-Baptiste Boric 	uint32_t num_lb, len, flags, lb_num;
22449f988b79SJean-Baptiste Boric 	uint32_t run_start;
22459f988b79SJean-Baptiste Boric 	uint32_t slot_offset, replace_len, replace;
22469f988b79SJean-Baptiste Boric 	int addr_type, icbflags;
22479f988b79SJean-Baptiste Boric //	int udf_c_type = buf->b_udf_c_type;
22489f988b79SJean-Baptiste Boric 	int lb_size, run_length, eof;
22499f988b79SJean-Baptiste Boric 	int slot, cpy_slot, cpy_slots, restart_slot;
22509f988b79SJean-Baptiste Boric 	int error;
22519f988b79SJean-Baptiste Boric 
22529f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("udf_record_allocation_in_node\n"));
22539f988b79SJean-Baptiste Boric 
22549f988b79SJean-Baptiste Boric #if 0
22559f988b79SJean-Baptiste Boric 	/* XXX disable sanity check for now */
22569f988b79SJean-Baptiste Boric 	/* sanity check ... should be panic ? */
22579f988b79SJean-Baptiste Boric 	if ((udf_c_type != UDF_C_USERDATA) && (udf_c_type != UDF_C_FIDS))
22589f988b79SJean-Baptiste Boric 		return;
22599f988b79SJean-Baptiste Boric #endif
22609f988b79SJean-Baptiste Boric 
22619f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
22629f988b79SJean-Baptiste Boric 	max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
22639f988b79SJean-Baptiste Boric 
22649f988b79SJean-Baptiste Boric 	/* do the job */
22659f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);	/* XXX can deadlock ? */
22669f988b79SJean-Baptiste Boric 	udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
22679f988b79SJean-Baptiste Boric 
22689f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
22699f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
22709f988b79SJean-Baptiste Boric 	if (fe) {
22719f988b79SJean-Baptiste Boric 		icbtag = &fe->icbtag;
22729f988b79SJean-Baptiste Boric 		inflen = udf_rw64(fe->inf_len);
22739f988b79SJean-Baptiste Boric 	} else {
22749f988b79SJean-Baptiste Boric 		icbtag = &efe->icbtag;
22759f988b79SJean-Baptiste Boric 		inflen = udf_rw64(efe->inf_len);
22769f988b79SJean-Baptiste Boric 	}
22779f988b79SJean-Baptiste Boric 
22789f988b79SJean-Baptiste Boric 	/* do check if `till' is not past file information length */
22799f988b79SJean-Baptiste Boric 	from = buf->b_lblkno * lb_size;
22809f988b79SJean-Baptiste Boric 	till = MIN(inflen, from + buf->b_resid);
22819f988b79SJean-Baptiste Boric 
22829f988b79SJean-Baptiste Boric 	num_lb = (till - from + lb_size -1) / lb_size;
22839f988b79SJean-Baptiste Boric 
22849f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("record allocation from %"PRIu64" + %d\n", from, buf->b_bcount));
22859f988b79SJean-Baptiste Boric 
22869f988b79SJean-Baptiste Boric 	icbflags  = udf_rw16(icbtag->flags);
22879f988b79SJean-Baptiste Boric 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
22889f988b79SJean-Baptiste Boric 
22899f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
22909f988b79SJean-Baptiste Boric 		/* nothing to do */
22919f988b79SJean-Baptiste Boric 		/* XXX clean up rest of node? just in case? */
22929f988b79SJean-Baptiste Boric 		UDF_UNLOCK_NODE(udf_node, 0);
22939f988b79SJean-Baptiste Boric 		return;
22949f988b79SJean-Baptiste Boric 	}
22959f988b79SJean-Baptiste Boric 
22969f988b79SJean-Baptiste Boric 	slot     = 0;
22979f988b79SJean-Baptiste Boric 	cpy_slot = 0;
22989f988b79SJean-Baptiste Boric 	foffset  = 0;
22999f988b79SJean-Baptiste Boric 
23009f988b79SJean-Baptiste Boric 	/* 1) copy till first overlap piece to the rewrite buffer */
23019f988b79SJean-Baptiste Boric 	for (;;) {
23029f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
23039f988b79SJean-Baptiste Boric 		if (eof) {
23049f988b79SJean-Baptiste Boric 			DPRINTF(WRITE,
23059f988b79SJean-Baptiste Boric 				("Record allocation in node "
23069f988b79SJean-Baptiste Boric 				 "failed: encountered EOF\n"));
23079f988b79SJean-Baptiste Boric 			UDF_UNLOCK_NODE(udf_node, 0);
23089f988b79SJean-Baptiste Boric 			buf->b_error = EINVAL;
23099f988b79SJean-Baptiste Boric 			return;
23109f988b79SJean-Baptiste Boric 		}
23119f988b79SJean-Baptiste Boric 		len   = udf_rw32(s_ad.len);
23129f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(len);
23139f988b79SJean-Baptiste Boric 		len   = UDF_EXT_LEN(len);
23149f988b79SJean-Baptiste Boric 
23159f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
23169f988b79SJean-Baptiste Boric 			slot++;
23179f988b79SJean-Baptiste Boric 			continue;
23189f988b79SJean-Baptiste Boric 		}
23199f988b79SJean-Baptiste Boric 
23209f988b79SJean-Baptiste Boric 		end_foffset = foffset + len;
23219f988b79SJean-Baptiste Boric 		if (end_foffset > from)
23229f988b79SJean-Baptiste Boric 			break;	/* found */
23239f988b79SJean-Baptiste Boric 
23249f988b79SJean-Baptiste Boric 		node_ad_cpy[cpy_slot++] = s_ad;
23259f988b79SJean-Baptiste Boric 
23269f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
23279f988b79SJean-Baptiste Boric 			"-> stack\n",
23289f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num),
23299f988b79SJean-Baptiste Boric 			udf_rw32(s_ad.loc.lb_num),
23309f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
23319f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
23329f988b79SJean-Baptiste Boric 
23339f988b79SJean-Baptiste Boric 		foffset = end_foffset;
23349f988b79SJean-Baptiste Boric 		slot++;
23359f988b79SJean-Baptiste Boric 	}
23369f988b79SJean-Baptiste Boric 	restart_slot    = slot;
23379f988b79SJean-Baptiste Boric 	restart_foffset = foffset;
23389f988b79SJean-Baptiste Boric 
23399f988b79SJean-Baptiste Boric 	/* 2) trunc overlapping slot at overlap and copy it */
23409f988b79SJean-Baptiste Boric 	slot_offset = from - foffset;
23419f988b79SJean-Baptiste Boric 	if (slot_offset > 0) {
23429f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\tslot_offset = %d, flags = %d (%d)\n",
23439f988b79SJean-Baptiste Boric 				slot_offset, flags >> 30, flags));
23449f988b79SJean-Baptiste Boric 
23459f988b79SJean-Baptiste Boric 		s_ad.len = udf_rw32(slot_offset | flags);
23469f988b79SJean-Baptiste Boric 		node_ad_cpy[cpy_slot++] = s_ad;
23479f988b79SJean-Baptiste Boric 
23489f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
23499f988b79SJean-Baptiste Boric 			"-> stack\n",
23509f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num),
23519f988b79SJean-Baptiste Boric 			udf_rw32(s_ad.loc.lb_num),
23529f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
23539f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
23549f988b79SJean-Baptiste Boric 	}
23559f988b79SJean-Baptiste Boric 	foffset += slot_offset;
23569f988b79SJean-Baptiste Boric 
23579f988b79SJean-Baptiste Boric 	/* 3) insert new mappings */
23589f988b79SJean-Baptiste Boric 	memset(&s_ad, 0, sizeof(struct long_ad));
23599f988b79SJean-Baptiste Boric 	lb_num = 0;
23609f988b79SJean-Baptiste Boric 	for (lb_num = 0; lb_num < num_lb; lb_num++) {
23619f988b79SJean-Baptiste Boric 		run_start  = mapping[lb_num];
23629f988b79SJean-Baptiste Boric 		run_length = 1;
23639f988b79SJean-Baptiste Boric 		while (lb_num < num_lb-1) {
23649f988b79SJean-Baptiste Boric 			if (mapping[lb_num+1] != mapping[lb_num]+1)
23659f988b79SJean-Baptiste Boric 				if (mapping[lb_num+1] != mapping[lb_num])
23669f988b79SJean-Baptiste Boric 					break;
23679f988b79SJean-Baptiste Boric 			run_length++;
23689f988b79SJean-Baptiste Boric 			lb_num++;
23699f988b79SJean-Baptiste Boric 		}
23709f988b79SJean-Baptiste Boric 		/* insert slot for this mapping */
23719f988b79SJean-Baptiste Boric 		len = run_length * lb_size;
23729f988b79SJean-Baptiste Boric 
23739f988b79SJean-Baptiste Boric 		/* bounds checking */
23749f988b79SJean-Baptiste Boric 		if (foffset + len > till)
23759f988b79SJean-Baptiste Boric 			len = till - foffset;
23769f988b79SJean-Baptiste Boric 		KASSERT(foffset + len <= inflen);
23779f988b79SJean-Baptiste Boric 
23789f988b79SJean-Baptiste Boric 		s_ad.len = udf_rw32(len | UDF_EXT_ALLOCATED);
23799f988b79SJean-Baptiste Boric 		s_ad.loc.part_num = udf_rw16(vpart_num);
23809f988b79SJean-Baptiste Boric 		s_ad.loc.lb_num   = udf_rw32(run_start);
23819f988b79SJean-Baptiste Boric 
23829f988b79SJean-Baptiste Boric 		foffset += len;
23839f988b79SJean-Baptiste Boric 
23849f988b79SJean-Baptiste Boric 		/* paranoia */
23859f988b79SJean-Baptiste Boric 		if (len == 0) {
23869f988b79SJean-Baptiste Boric 			DPRINTF(WRITE,
23879f988b79SJean-Baptiste Boric 				("Record allocation in node "
23889f988b79SJean-Baptiste Boric 				 "failed: insert failed\n"));
23899f988b79SJean-Baptiste Boric 			UDF_UNLOCK_NODE(udf_node, 0);
23909f988b79SJean-Baptiste Boric 			buf->b_error = EINVAL;
23919f988b79SJean-Baptiste Boric 			return;
23929f988b79SJean-Baptiste Boric 		}
23939f988b79SJean-Baptiste Boric 		node_ad_cpy[cpy_slot++] = s_ad;
23949f988b79SJean-Baptiste Boric 
23959f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t3: insert new mapping vp %d lb %d, len %d, "
23969f988b79SJean-Baptiste Boric 				"flags %d -> stack\n",
23979f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num), udf_rw32(s_ad.loc.lb_num),
23989f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
23999f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
24009f988b79SJean-Baptiste Boric 	}
24019f988b79SJean-Baptiste Boric 
24029f988b79SJean-Baptiste Boric 	/* 4) pop replaced length */
24039f988b79SJean-Baptiste Boric 	slot    = restart_slot;
24049f988b79SJean-Baptiste Boric 	foffset = restart_foffset;
24059f988b79SJean-Baptiste Boric 
24069f988b79SJean-Baptiste Boric 	replace_len = till - foffset;	/* total amount of bytes to pop */
24079f988b79SJean-Baptiste Boric 	slot_offset = from - foffset;	/* offset in first encounted slot */
24089f988b79SJean-Baptiste Boric 	KASSERT((slot_offset % lb_size) == 0);
24099f988b79SJean-Baptiste Boric 
24109f988b79SJean-Baptiste Boric 	for (;;) {
24119f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
24129f988b79SJean-Baptiste Boric 		if (eof)
24139f988b79SJean-Baptiste Boric 			break;
24149f988b79SJean-Baptiste Boric 
24159f988b79SJean-Baptiste Boric 		len    = udf_rw32(s_ad.len);
24169f988b79SJean-Baptiste Boric 		flags  = UDF_EXT_FLAGS(len);
24179f988b79SJean-Baptiste Boric 		len    = UDF_EXT_LEN(len);
24189f988b79SJean-Baptiste Boric 		lb_num = udf_rw32(s_ad.loc.lb_num);
24199f988b79SJean-Baptiste Boric 
24209f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
24219f988b79SJean-Baptiste Boric 			slot++;
24229f988b79SJean-Baptiste Boric 			continue;
24239f988b79SJean-Baptiste Boric 		}
24249f988b79SJean-Baptiste Boric 
24259f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t4i: got slot %d, slot_offset %d, "
24269f988b79SJean-Baptiste Boric 				"replace_len %d, "
24279f988b79SJean-Baptiste Boric 				"vp %d, lb %d, len %d, flags %d\n",
24289f988b79SJean-Baptiste Boric 			slot, slot_offset, replace_len,
24299f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num),
24309f988b79SJean-Baptiste Boric 			udf_rw32(s_ad.loc.lb_num),
24319f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
24329f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
24339f988b79SJean-Baptiste Boric 
24349f988b79SJean-Baptiste Boric 		/* adjust for slot offset */
24359f988b79SJean-Baptiste Boric 		if (slot_offset) {
24369f988b79SJean-Baptiste Boric 			DPRINTF(ALLOC, ("\t4s: skipping %d\n", slot_offset));
24379f988b79SJean-Baptiste Boric 			lb_num += slot_offset / lb_size;
24389f988b79SJean-Baptiste Boric 			len    -= slot_offset;
24399f988b79SJean-Baptiste Boric 			foffset += slot_offset;
24409f988b79SJean-Baptiste Boric 			replace_len -= slot_offset;
24419f988b79SJean-Baptiste Boric 
24429f988b79SJean-Baptiste Boric 			/* mark adjusted */
24439f988b79SJean-Baptiste Boric 			slot_offset = 0;
24449f988b79SJean-Baptiste Boric 		}
24459f988b79SJean-Baptiste Boric 
24469f988b79SJean-Baptiste Boric 		/* advance for (the rest of) this slot */
24479f988b79SJean-Baptiste Boric 		replace = MIN(len, replace_len);
24489f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t4d: replacing %d\n", replace));
24499f988b79SJean-Baptiste Boric 
24509f988b79SJean-Baptiste Boric 		/* advance for this slot */
24519f988b79SJean-Baptiste Boric 		if (replace) {
24529f988b79SJean-Baptiste Boric 			/* note: dont round DOWN on num_lb since we then
24539f988b79SJean-Baptiste Boric 			 * forget the last partial one */
24549f988b79SJean-Baptiste Boric 			num_lb = (replace + lb_size - 1) / lb_size;
24559f988b79SJean-Baptiste Boric 			if (flags != UDF_EXT_FREE) {
24569f988b79SJean-Baptiste Boric 				udf_free_allocated_space(ump, lb_num,
24579f988b79SJean-Baptiste Boric 					udf_rw16(s_ad.loc.part_num), num_lb);
24589f988b79SJean-Baptiste Boric 			}
24599f988b79SJean-Baptiste Boric 			lb_num      += num_lb;
24609f988b79SJean-Baptiste Boric 			len         -= replace;
24619f988b79SJean-Baptiste Boric 			foffset     += replace;
24629f988b79SJean-Baptiste Boric 			replace_len -= replace;
24639f988b79SJean-Baptiste Boric 		}
24649f988b79SJean-Baptiste Boric 
24659f988b79SJean-Baptiste Boric 		/* do we have a slot tail ? */
24669f988b79SJean-Baptiste Boric 		if (len) {
24679f988b79SJean-Baptiste Boric 			KASSERT(foffset % lb_size == 0);
24689f988b79SJean-Baptiste Boric 
24699f988b79SJean-Baptiste Boric 			/* we arrived at our point, push remainder */
24709f988b79SJean-Baptiste Boric 			s_ad.len        = udf_rw32(len | flags);
24719f988b79SJean-Baptiste Boric 			s_ad.loc.lb_num = udf_rw32(lb_num);
24729f988b79SJean-Baptiste Boric 			if (flags == UDF_EXT_FREE)
24739f988b79SJean-Baptiste Boric 				s_ad.loc.lb_num = udf_rw32(0);
24749f988b79SJean-Baptiste Boric 			node_ad_cpy[cpy_slot++] = s_ad;
24759f988b79SJean-Baptiste Boric 			foffset += len;
24769f988b79SJean-Baptiste Boric 			slot++;
24779f988b79SJean-Baptiste Boric 
24789f988b79SJean-Baptiste Boric 			DPRINTF(ALLOC, ("\t4: vp %d, lb %d, len %d, flags %d "
24799f988b79SJean-Baptiste Boric 				"-> stack\n",
24809f988b79SJean-Baptiste Boric 				udf_rw16(s_ad.loc.part_num),
24819f988b79SJean-Baptiste Boric 				udf_rw32(s_ad.loc.lb_num),
24829f988b79SJean-Baptiste Boric 				UDF_EXT_LEN(udf_rw32(s_ad.len)),
24839f988b79SJean-Baptiste Boric 				UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
24849f988b79SJean-Baptiste Boric 			break;
24859f988b79SJean-Baptiste Boric 		}
24869f988b79SJean-Baptiste Boric 
24879f988b79SJean-Baptiste Boric 		slot++;
24889f988b79SJean-Baptiste Boric 	}
24899f988b79SJean-Baptiste Boric 
24909f988b79SJean-Baptiste Boric 	/* 5) copy remainder */
24919f988b79SJean-Baptiste Boric 	for (;;) {
24929f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
24939f988b79SJean-Baptiste Boric 		if (eof)
24949f988b79SJean-Baptiste Boric 			break;
24959f988b79SJean-Baptiste Boric 
24969f988b79SJean-Baptiste Boric 		len   = udf_rw32(s_ad.len);
24979f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(len);
24989f988b79SJean-Baptiste Boric 		len   = UDF_EXT_LEN(len);
24999f988b79SJean-Baptiste Boric 
25009f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
25019f988b79SJean-Baptiste Boric 			slot++;
25029f988b79SJean-Baptiste Boric 			continue;
25039f988b79SJean-Baptiste Boric 		}
25049f988b79SJean-Baptiste Boric 
25059f988b79SJean-Baptiste Boric 		node_ad_cpy[cpy_slot++] = s_ad;
25069f988b79SJean-Baptiste Boric 
25079f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t5: insert new mapping "
25089f988b79SJean-Baptiste Boric 			"vp %d lb %d, len %d, flags %d "
25099f988b79SJean-Baptiste Boric 			"-> stack\n",
25109f988b79SJean-Baptiste Boric 		udf_rw16(s_ad.loc.part_num),
25119f988b79SJean-Baptiste Boric 		udf_rw32(s_ad.loc.lb_num),
25129f988b79SJean-Baptiste Boric 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
25139f988b79SJean-Baptiste Boric 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
25149f988b79SJean-Baptiste Boric 
25159f988b79SJean-Baptiste Boric 		slot++;
25169f988b79SJean-Baptiste Boric 	}
25179f988b79SJean-Baptiste Boric 
25189f988b79SJean-Baptiste Boric 	/* 6) reset node descriptors */
25199f988b79SJean-Baptiste Boric 	udf_wipe_adslots(udf_node);
25209f988b79SJean-Baptiste Boric 
25219f988b79SJean-Baptiste Boric 	/* 7) copy back extents; merge when possible. Recounting on the fly */
25229f988b79SJean-Baptiste Boric 	cpy_slots = cpy_slot;
25239f988b79SJean-Baptiste Boric 
25249f988b79SJean-Baptiste Boric 	c_ad = node_ad_cpy[0];
25259f988b79SJean-Baptiste Boric 	slot = 0;
25269f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("\t7s: stack -> got mapping vp %d "
25279f988b79SJean-Baptiste Boric 		"lb %d, len %d, flags %d\n",
25289f988b79SJean-Baptiste Boric 	udf_rw16(c_ad.loc.part_num),
25299f988b79SJean-Baptiste Boric 	udf_rw32(c_ad.loc.lb_num),
25309f988b79SJean-Baptiste Boric 	UDF_EXT_LEN(udf_rw32(c_ad.len)),
25319f988b79SJean-Baptiste Boric 	UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
25329f988b79SJean-Baptiste Boric 
25339f988b79SJean-Baptiste Boric 	for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
25349f988b79SJean-Baptiste Boric 		s_ad = node_ad_cpy[cpy_slot];
25359f988b79SJean-Baptiste Boric 
25369f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t7i: stack -> got mapping vp %d "
25379f988b79SJean-Baptiste Boric 			"lb %d, len %d, flags %d\n",
25389f988b79SJean-Baptiste Boric 		udf_rw16(s_ad.loc.part_num),
25399f988b79SJean-Baptiste Boric 		udf_rw32(s_ad.loc.lb_num),
25409f988b79SJean-Baptiste Boric 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
25419f988b79SJean-Baptiste Boric 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
25429f988b79SJean-Baptiste Boric 
25439f988b79SJean-Baptiste Boric 		/* see if we can merge */
25449f988b79SJean-Baptiste Boric 		if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) {
25459f988b79SJean-Baptiste Boric 			/* not mergable (anymore) */
25469f988b79SJean-Baptiste Boric 			DPRINTF(ALLOC, ("\t7: appending vp %d lb %d, "
25479f988b79SJean-Baptiste Boric 				"len %d, flags %d\n",
25489f988b79SJean-Baptiste Boric 			udf_rw16(c_ad.loc.part_num),
25499f988b79SJean-Baptiste Boric 			udf_rw32(c_ad.loc.lb_num),
25509f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(c_ad.len)),
25519f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
25529f988b79SJean-Baptiste Boric 
25539f988b79SJean-Baptiste Boric 			error = udf_append_adslot(udf_node, &slot, &c_ad);
25549f988b79SJean-Baptiste Boric 			if (error) {
25559f988b79SJean-Baptiste Boric 				buf->b_error = error;
25569f988b79SJean-Baptiste Boric 				goto out;
25579f988b79SJean-Baptiste Boric 			}
25589f988b79SJean-Baptiste Boric 			c_ad = s_ad;
25599f988b79SJean-Baptiste Boric 			slot++;
25609f988b79SJean-Baptiste Boric 		}
25619f988b79SJean-Baptiste Boric 	}
25629f988b79SJean-Baptiste Boric 
25639f988b79SJean-Baptiste Boric 	/* 8) push rest slot (if any) */
25649f988b79SJean-Baptiste Boric 	if (UDF_EXT_LEN(c_ad.len) > 0) {
25659f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t8: last append vp %d lb %d, "
25669f988b79SJean-Baptiste Boric 				"len %d, flags %d\n",
25679f988b79SJean-Baptiste Boric 		udf_rw16(c_ad.loc.part_num),
25689f988b79SJean-Baptiste Boric 		udf_rw32(c_ad.loc.lb_num),
25699f988b79SJean-Baptiste Boric 		UDF_EXT_LEN(udf_rw32(c_ad.len)),
25709f988b79SJean-Baptiste Boric 		UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
25719f988b79SJean-Baptiste Boric 
25729f988b79SJean-Baptiste Boric 		error = udf_append_adslot(udf_node, &slot, &c_ad);
25739f988b79SJean-Baptiste Boric 		if (error) {
25749f988b79SJean-Baptiste Boric 			buf->b_error = error;
25759f988b79SJean-Baptiste Boric 			goto out;
25769f988b79SJean-Baptiste Boric 		}
25779f988b79SJean-Baptiste Boric 	}
25789f988b79SJean-Baptiste Boric 
25799f988b79SJean-Baptiste Boric out:
25809f988b79SJean-Baptiste Boric 	udf_count_alloc_exts(udf_node);
25819f988b79SJean-Baptiste Boric 
25829f988b79SJean-Baptiste Boric 	/* the node's descriptors should now be sane */
25839f988b79SJean-Baptiste Boric 	udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
25849f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
25859f988b79SJean-Baptiste Boric 
25869f988b79SJean-Baptiste Boric 	KASSERT(orig_inflen == new_inflen);
25879f988b79SJean-Baptiste Boric 	KASSERT(new_lbrec >= orig_lbrec);
25889f988b79SJean-Baptiste Boric 
25899f988b79SJean-Baptiste Boric 	return;
25909f988b79SJean-Baptiste Boric }
25919f988b79SJean-Baptiste Boric 
25929f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
25939f988b79SJean-Baptiste Boric 
25949f988b79SJean-Baptiste Boric int
udf_grow_node(struct udf_node * udf_node,uint64_t new_size)25959f988b79SJean-Baptiste Boric udf_grow_node(struct udf_node *udf_node, uint64_t new_size)
25969f988b79SJean-Baptiste Boric {
25979f988b79SJean-Baptiste Boric 	struct vnode *vp = udf_node->vnode;
25989f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
25999f988b79SJean-Baptiste Boric 	struct file_entry    *fe;
26009f988b79SJean-Baptiste Boric 	struct extfile_entry *efe;
26019f988b79SJean-Baptiste Boric 	struct icb_tag  *icbtag;
26029f988b79SJean-Baptiste Boric 	struct long_ad c_ad, s_ad;
26039f988b79SJean-Baptiste Boric 	uint64_t size_diff, old_size, inflen, objsize, chunk, append_len;
26049f988b79SJean-Baptiste Boric 	uint64_t foffset, end_foffset;
26059f988b79SJean-Baptiste Boric 	uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
26069f988b79SJean-Baptiste Boric 	uint32_t lb_size, unit_size, dscr_size, crclen, lastblock_grow;
26079f988b79SJean-Baptiste Boric 	uint32_t icbflags, len, flags, max_len;
26089f988b79SJean-Baptiste Boric 	uint32_t max_l_ad, l_ad, l_ea;
26099f988b79SJean-Baptiste Boric 	uint16_t my_part, dst_part;
26109f988b79SJean-Baptiste Boric 	uint8_t *evacuated_data;
26119f988b79SJean-Baptiste Boric 	int addr_type;
26129f988b79SJean-Baptiste Boric 	int slot;
26139f988b79SJean-Baptiste Boric 	int eof, error;
26149f988b79SJean-Baptiste Boric 
26159f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("udf_grow_node\n"));
26169f988b79SJean-Baptiste Boric 
26179f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
26189f988b79SJean-Baptiste Boric 	udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
26199f988b79SJean-Baptiste Boric 
26209f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
26219f988b79SJean-Baptiste Boric 
26229f988b79SJean-Baptiste Boric 	/* max_len in unit's IFF its a metadata node or metadata mirror node */
26239f988b79SJean-Baptiste Boric 	unit_size = lb_size;
26249f988b79SJean-Baptiste Boric 	if ((udf_node == ump->metadata_node) || (udf_node == ump->metadatamirror_node))
26259f988b79SJean-Baptiste Boric 		unit_size = ump->metadata_alloc_unit_size * lb_size;
26269f988b79SJean-Baptiste Boric 	max_len = ((UDF_EXT_MAXLEN / unit_size) * unit_size);
26279f988b79SJean-Baptiste Boric 
26289f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
26299f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
26309f988b79SJean-Baptiste Boric 	if (fe) {
26319f988b79SJean-Baptiste Boric 		icbtag  = &fe->icbtag;
26329f988b79SJean-Baptiste Boric 		inflen  = udf_rw64(fe->inf_len);
26339f988b79SJean-Baptiste Boric 		objsize = inflen;
26349f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct file_entry) -1;
26359f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(fe->l_ea);
26369f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(fe->l_ad);
26379f988b79SJean-Baptiste Boric 	} else {
26389f988b79SJean-Baptiste Boric 		icbtag  = &efe->icbtag;
26399f988b79SJean-Baptiste Boric 		inflen  = udf_rw64(efe->inf_len);
26409f988b79SJean-Baptiste Boric 		objsize = udf_rw64(efe->obj_size);
26419f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct extfile_entry) -1;
26429f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(efe->l_ea);
26439f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(efe->l_ad);
26449f988b79SJean-Baptiste Boric 	}
26459f988b79SJean-Baptiste Boric 	max_l_ad = lb_size - dscr_size - l_ea;
26469f988b79SJean-Baptiste Boric 
26479f988b79SJean-Baptiste Boric 	icbflags   = udf_rw16(icbtag->flags);
26489f988b79SJean-Baptiste Boric 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
26499f988b79SJean-Baptiste Boric 
26509f988b79SJean-Baptiste Boric 	old_size  = inflen;
26519f988b79SJean-Baptiste Boric 	size_diff = new_size - old_size;
26529f988b79SJean-Baptiste Boric 
26539f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
26549f988b79SJean-Baptiste Boric 
26559f988b79SJean-Baptiste Boric 	evacuated_data = NULL;
26569f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
26579f988b79SJean-Baptiste Boric 		if (l_ad + size_diff <= max_l_ad) {
26589f988b79SJean-Baptiste Boric 			/* only reflect size change directly in the node */
26599f988b79SJean-Baptiste Boric 			inflen  += size_diff;
26609f988b79SJean-Baptiste Boric 			objsize += size_diff;
26619f988b79SJean-Baptiste Boric 			l_ad    += size_diff;
26629f988b79SJean-Baptiste Boric 			crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
26639f988b79SJean-Baptiste Boric 			if (fe) {
26649f988b79SJean-Baptiste Boric 				fe->inf_len   = udf_rw64(inflen);
26659f988b79SJean-Baptiste Boric 				fe->l_ad      = udf_rw32(l_ad);
26669f988b79SJean-Baptiste Boric 				fe->tag.desc_crc_len = udf_rw16(crclen);
26679f988b79SJean-Baptiste Boric 			} else {
26689f988b79SJean-Baptiste Boric 				efe->inf_len  = udf_rw64(inflen);
26699f988b79SJean-Baptiste Boric 				efe->obj_size = udf_rw64(objsize);
26709f988b79SJean-Baptiste Boric 				efe->l_ad     = udf_rw32(l_ad);
26719f988b79SJean-Baptiste Boric 				efe->tag.desc_crc_len = udf_rw16(crclen);
26729f988b79SJean-Baptiste Boric 			}
26739f988b79SJean-Baptiste Boric 			error = 0;
26749f988b79SJean-Baptiste Boric 
26759f988b79SJean-Baptiste Boric 			/* set new size for uvm */
26769f988b79SJean-Baptiste Boric 			uvm_vnp_setwritesize(vp, new_size);
26779f988b79SJean-Baptiste Boric 			uvm_vnp_setsize(vp, new_size);
26789f988b79SJean-Baptiste Boric 
26799f988b79SJean-Baptiste Boric #if 0
26809f988b79SJean-Baptiste Boric 			/* zero append space in buffer */
26819f988b79SJean-Baptiste Boric 			ubc_zerorange(&vp->v_uobj, old_size,
26829f988b79SJean-Baptiste Boric 			    new_size - old_size, UBC_UNMAP_FLAG(vp));
26839f988b79SJean-Baptiste Boric #endif
26849f988b79SJean-Baptiste Boric 
26859f988b79SJean-Baptiste Boric 			udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
26869f988b79SJean-Baptiste Boric 
26879f988b79SJean-Baptiste Boric 			/* unlock */
26889f988b79SJean-Baptiste Boric 			UDF_UNLOCK_NODE(udf_node, 0);
26899f988b79SJean-Baptiste Boric 
26909f988b79SJean-Baptiste Boric 			KASSERT(new_inflen == orig_inflen + size_diff);
26919f988b79SJean-Baptiste Boric 			KASSERT(new_lbrec == orig_lbrec);
26929f988b79SJean-Baptiste Boric 			KASSERT(new_lbrec == 0);
26939f988b79SJean-Baptiste Boric 			return 0;
26949f988b79SJean-Baptiste Boric 		}
26959f988b79SJean-Baptiste Boric 
26969f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\tCONVERT from internal\n"));
26979f988b79SJean-Baptiste Boric 
26989f988b79SJean-Baptiste Boric 		if (old_size > 0) {
26999f988b79SJean-Baptiste Boric 			/* allocate some space and copy in the stuff to keep */
27009f988b79SJean-Baptiste Boric 			evacuated_data = malloc(lb_size, M_UDFTEMP, M_WAITOK);
27019f988b79SJean-Baptiste Boric 			memset(evacuated_data, 0, lb_size);
27029f988b79SJean-Baptiste Boric 
27039f988b79SJean-Baptiste Boric 			/* node is locked, so safe to exit mutex */
27049f988b79SJean-Baptiste Boric 			UDF_UNLOCK_NODE(udf_node, 0);
27059f988b79SJean-Baptiste Boric 
27069f988b79SJean-Baptiste Boric 			/* read in using the `normal' vn_rdwr() */
27079f988b79SJean-Baptiste Boric 			error = vn_rdwr(UIO_READ, udf_node->vnode,
27089f988b79SJean-Baptiste Boric 					evacuated_data, old_size, 0,
27099f988b79SJean-Baptiste Boric 					UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
27109f988b79SJean-Baptiste Boric 					FSCRED, NULL, NULL);
27119f988b79SJean-Baptiste Boric 
27129f988b79SJean-Baptiste Boric 			/* enter again */
27139f988b79SJean-Baptiste Boric 			UDF_LOCK_NODE(udf_node, 0);
27149f988b79SJean-Baptiste Boric 		}
27159f988b79SJean-Baptiste Boric 
27169f988b79SJean-Baptiste Boric 		/* convert to a normal alloc and select type */
27179f988b79SJean-Baptiste Boric 		my_part  = udf_rw16(udf_node->loc.loc.part_num);
27189f988b79SJean-Baptiste Boric 		dst_part = udf_get_record_vpart(ump, udf_get_c_type(udf_node));
27199f988b79SJean-Baptiste Boric 		addr_type = UDF_ICB_SHORT_ALLOC;
27209f988b79SJean-Baptiste Boric 		if (dst_part != my_part)
27219f988b79SJean-Baptiste Boric 			addr_type = UDF_ICB_LONG_ALLOC;
27229f988b79SJean-Baptiste Boric 
27239f988b79SJean-Baptiste Boric 		icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
27249f988b79SJean-Baptiste Boric 		icbflags |= addr_type;
27259f988b79SJean-Baptiste Boric 		icbtag->flags = udf_rw16(icbflags);
27269f988b79SJean-Baptiste Boric 
27279f988b79SJean-Baptiste Boric 		/* wipe old descriptor space */
27289f988b79SJean-Baptiste Boric 		udf_wipe_adslots(udf_node);
27299f988b79SJean-Baptiste Boric 
27309f988b79SJean-Baptiste Boric 		memset(&c_ad, 0, sizeof(struct long_ad));
27319f988b79SJean-Baptiste Boric 		c_ad.len          = udf_rw32(old_size | UDF_EXT_FREE);
27329f988b79SJean-Baptiste Boric 		c_ad.loc.part_num = udf_rw16(0); /* not relevant */
27339f988b79SJean-Baptiste Boric 		c_ad.loc.lb_num   = udf_rw32(0); /* not relevant */
27349f988b79SJean-Baptiste Boric 
27359f988b79SJean-Baptiste Boric 		slot = 0;
27369f988b79SJean-Baptiste Boric 	} else {
27379f988b79SJean-Baptiste Boric 		/* goto the last entry (if any) */
27389f988b79SJean-Baptiste Boric 		slot     = 0;
27399f988b79SJean-Baptiste Boric 		foffset  = 0;
27409f988b79SJean-Baptiste Boric 		memset(&c_ad, 0, sizeof(struct long_ad));
27419f988b79SJean-Baptiste Boric 		for (;;) {
27429f988b79SJean-Baptiste Boric 			udf_get_adslot(udf_node, slot, &c_ad, &eof);
27439f988b79SJean-Baptiste Boric 			if (eof)
27449f988b79SJean-Baptiste Boric 				break;
27459f988b79SJean-Baptiste Boric 
27469f988b79SJean-Baptiste Boric 			len   = udf_rw32(c_ad.len);
27479f988b79SJean-Baptiste Boric 			flags = UDF_EXT_FLAGS(len);
27489f988b79SJean-Baptiste Boric 			len   = UDF_EXT_LEN(len);
27499f988b79SJean-Baptiste Boric 
27509f988b79SJean-Baptiste Boric 			end_foffset = foffset + len;
27519f988b79SJean-Baptiste Boric 			if (flags != UDF_EXT_REDIRECT)
27529f988b79SJean-Baptiste Boric 				foffset = end_foffset;
27539f988b79SJean-Baptiste Boric 
27549f988b79SJean-Baptiste Boric 			slot++;
27559f988b79SJean-Baptiste Boric 		}
27569f988b79SJean-Baptiste Boric 		/* at end of adslots */
27579f988b79SJean-Baptiste Boric 
27589f988b79SJean-Baptiste Boric 		/* special case if the old size was zero, then there is no last slot */
27599f988b79SJean-Baptiste Boric 		if (old_size == 0) {
27609f988b79SJean-Baptiste Boric 			c_ad.len          = udf_rw32(0 | UDF_EXT_FREE);
27619f988b79SJean-Baptiste Boric 			c_ad.loc.part_num = udf_rw16(0); /* not relevant */
27629f988b79SJean-Baptiste Boric 			c_ad.loc.lb_num   = udf_rw32(0); /* not relevant */
27639f988b79SJean-Baptiste Boric 		} else {
27649f988b79SJean-Baptiste Boric 			/* refetch last slot */
27659f988b79SJean-Baptiste Boric 			slot--;
27669f988b79SJean-Baptiste Boric 			udf_get_adslot(udf_node, slot, &c_ad, &eof);
27679f988b79SJean-Baptiste Boric 		}
27689f988b79SJean-Baptiste Boric 	}
27699f988b79SJean-Baptiste Boric 
27709f988b79SJean-Baptiste Boric 	/*
27719f988b79SJean-Baptiste Boric 	 * If the length of the last slot is not a multiple of lb_size, adjust
27729f988b79SJean-Baptiste Boric 	 * length so that it is; don't forget to adjust `append_len'! relevant for
27739f988b79SJean-Baptiste Boric 	 * extending existing files
27749f988b79SJean-Baptiste Boric 	 */
27759f988b79SJean-Baptiste Boric 	len   = udf_rw32(c_ad.len);
27769f988b79SJean-Baptiste Boric 	flags = UDF_EXT_FLAGS(len);
27779f988b79SJean-Baptiste Boric 	len   = UDF_EXT_LEN(len);
27789f988b79SJean-Baptiste Boric 
27799f988b79SJean-Baptiste Boric 	lastblock_grow = 0;
27809f988b79SJean-Baptiste Boric 	if (len % lb_size > 0) {
27819f988b79SJean-Baptiste Boric 		lastblock_grow = lb_size - (len % lb_size);
27829f988b79SJean-Baptiste Boric 		lastblock_grow = MIN(size_diff, lastblock_grow);
27839f988b79SJean-Baptiste Boric 		len += lastblock_grow;
27849f988b79SJean-Baptiste Boric 		c_ad.len = udf_rw32(len | flags);
27859f988b79SJean-Baptiste Boric 
27869f988b79SJean-Baptiste Boric 		/* TODO zero appened space in buffer! */
27879f988b79SJean-Baptiste Boric 		/* using ubc_zerorange(&vp->v_uobj, old_size, */
27889f988b79SJean-Baptiste Boric 		/*    new_size - old_size, UBC_UNMAP_FLAG(vp)); ? */
27899f988b79SJean-Baptiste Boric 	}
27909f988b79SJean-Baptiste Boric 	memset(&s_ad, 0, sizeof(struct long_ad));
27919f988b79SJean-Baptiste Boric 
27929f988b79SJean-Baptiste Boric 	/* size_diff can be bigger than allowed, so grow in chunks */
27939f988b79SJean-Baptiste Boric 	append_len = size_diff - lastblock_grow;
27949f988b79SJean-Baptiste Boric 	while (append_len > 0) {
27959f988b79SJean-Baptiste Boric 		chunk = MIN(append_len, max_len);
27969f988b79SJean-Baptiste Boric 		s_ad.len = udf_rw32(chunk | UDF_EXT_FREE);
27979f988b79SJean-Baptiste Boric 		s_ad.loc.part_num = udf_rw16(0);
27989f988b79SJean-Baptiste Boric 		s_ad.loc.lb_num   = udf_rw32(0);
27999f988b79SJean-Baptiste Boric 
28009f988b79SJean-Baptiste Boric 		if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) {
28019f988b79SJean-Baptiste Boric 			/* not mergable (anymore) */
28029f988b79SJean-Baptiste Boric 			error = udf_append_adslot(udf_node, &slot, &c_ad);
28039f988b79SJean-Baptiste Boric 			if (error)
28049f988b79SJean-Baptiste Boric 				goto errorout;
28059f988b79SJean-Baptiste Boric 			slot++;
28069f988b79SJean-Baptiste Boric 			c_ad = s_ad;
28079f988b79SJean-Baptiste Boric 			memset(&s_ad, 0, sizeof(struct long_ad));
28089f988b79SJean-Baptiste Boric 		}
28099f988b79SJean-Baptiste Boric 		append_len -= chunk;
28109f988b79SJean-Baptiste Boric 	}
28119f988b79SJean-Baptiste Boric 
28129f988b79SJean-Baptiste Boric 	/* if there is a rest piece in the accumulator, append it */
28139f988b79SJean-Baptiste Boric 	if (UDF_EXT_LEN(udf_rw32(c_ad.len)) > 0) {
28149f988b79SJean-Baptiste Boric 		error = udf_append_adslot(udf_node, &slot, &c_ad);
28159f988b79SJean-Baptiste Boric 		if (error)
28169f988b79SJean-Baptiste Boric 			goto errorout;
28179f988b79SJean-Baptiste Boric 		slot++;
28189f988b79SJean-Baptiste Boric 	}
28199f988b79SJean-Baptiste Boric 
28209f988b79SJean-Baptiste Boric 	/* if there is a rest piece that didn't fit, append it */
28219f988b79SJean-Baptiste Boric 	if (UDF_EXT_LEN(udf_rw32(s_ad.len)) > 0) {
28229f988b79SJean-Baptiste Boric 		error = udf_append_adslot(udf_node, &slot, &s_ad);
28239f988b79SJean-Baptiste Boric 		if (error)
28249f988b79SJean-Baptiste Boric 			goto errorout;
28259f988b79SJean-Baptiste Boric 		slot++;
28269f988b79SJean-Baptiste Boric 	}
28279f988b79SJean-Baptiste Boric 
28289f988b79SJean-Baptiste Boric 	inflen  += size_diff;
28299f988b79SJean-Baptiste Boric 	objsize += size_diff;
28309f988b79SJean-Baptiste Boric 	if (fe) {
28319f988b79SJean-Baptiste Boric 		fe->inf_len   = udf_rw64(inflen);
28329f988b79SJean-Baptiste Boric 	} else {
28339f988b79SJean-Baptiste Boric 		efe->inf_len  = udf_rw64(inflen);
28349f988b79SJean-Baptiste Boric 		efe->obj_size = udf_rw64(objsize);
28359f988b79SJean-Baptiste Boric 	}
28369f988b79SJean-Baptiste Boric 	error = 0;
28379f988b79SJean-Baptiste Boric 
28389f988b79SJean-Baptiste Boric 	if (evacuated_data) {
28399f988b79SJean-Baptiste Boric 		/* set new write size for uvm */
28409f988b79SJean-Baptiste Boric 		uvm_vnp_setwritesize(vp, old_size);
28419f988b79SJean-Baptiste Boric 
28429f988b79SJean-Baptiste Boric 		/* write out evacuated data */
28439f988b79SJean-Baptiste Boric 		error = vn_rdwr(UIO_WRITE, udf_node->vnode,
28449f988b79SJean-Baptiste Boric 				evacuated_data, old_size, 0,
28459f988b79SJean-Baptiste Boric 				UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
28469f988b79SJean-Baptiste Boric 				FSCRED, NULL, NULL);
28479f988b79SJean-Baptiste Boric 		uvm_vnp_setsize(vp, old_size);
28489f988b79SJean-Baptiste Boric 	}
28499f988b79SJean-Baptiste Boric 
28509f988b79SJean-Baptiste Boric errorout:
28519f988b79SJean-Baptiste Boric 	if (evacuated_data)
28529f988b79SJean-Baptiste Boric 		free(evacuated_data, M_UDFTEMP);
28539f988b79SJean-Baptiste Boric 
28549f988b79SJean-Baptiste Boric 	udf_count_alloc_exts(udf_node);
28559f988b79SJean-Baptiste Boric 
28569f988b79SJean-Baptiste Boric 	udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
28579f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
28589f988b79SJean-Baptiste Boric 
28599f988b79SJean-Baptiste Boric 	KASSERT(new_inflen == orig_inflen + size_diff);
28609f988b79SJean-Baptiste Boric 	KASSERT(new_lbrec == orig_lbrec);
28619f988b79SJean-Baptiste Boric 
28629f988b79SJean-Baptiste Boric 	return error;
28639f988b79SJean-Baptiste Boric }
28649f988b79SJean-Baptiste Boric 
28659f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
28669f988b79SJean-Baptiste Boric 
28679f988b79SJean-Baptiste Boric int
udf_shrink_node(struct udf_node * udf_node,uint64_t new_size)28689f988b79SJean-Baptiste Boric udf_shrink_node(struct udf_node *udf_node, uint64_t new_size)
28699f988b79SJean-Baptiste Boric {
28709f988b79SJean-Baptiste Boric 	struct vnode *vp = udf_node->vnode;
28719f988b79SJean-Baptiste Boric 	struct udf_mount *ump = udf_node->ump;
28729f988b79SJean-Baptiste Boric 	struct file_entry    *fe;
28739f988b79SJean-Baptiste Boric 	struct extfile_entry *efe;
28749f988b79SJean-Baptiste Boric 	struct icb_tag  *icbtag;
28759f988b79SJean-Baptiste Boric 	struct long_ad c_ad, s_ad, *node_ad_cpy;
28769f988b79SJean-Baptiste Boric 	uint64_t size_diff, old_size, inflen, objsize;
28779f988b79SJean-Baptiste Boric 	uint64_t foffset, end_foffset;
28789f988b79SJean-Baptiste Boric 	uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
28799f988b79SJean-Baptiste Boric 	uint32_t lb_size, unit_size, dscr_size, crclen;
28809f988b79SJean-Baptiste Boric 	uint32_t slot_offset, slot_offset_lb;
28819f988b79SJean-Baptiste Boric 	uint32_t len, flags, max_len;
28829f988b79SJean-Baptiste Boric 	uint32_t num_lb, lb_num;
28839f988b79SJean-Baptiste Boric 	uint32_t max_l_ad, l_ad, l_ea;
28849f988b79SJean-Baptiste Boric 	uint16_t vpart_num;
28859f988b79SJean-Baptiste Boric 	uint8_t *data_pos;
28869f988b79SJean-Baptiste Boric 	int icbflags, addr_type;
28879f988b79SJean-Baptiste Boric 	int slot, cpy_slot, cpy_slots;
28889f988b79SJean-Baptiste Boric 	int eof, error;
28899f988b79SJean-Baptiste Boric 
28909f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("udf_shrink_node\n"));
28919f988b79SJean-Baptiste Boric 
28929f988b79SJean-Baptiste Boric 	UDF_LOCK_NODE(udf_node, 0);
28939f988b79SJean-Baptiste Boric 	udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
28949f988b79SJean-Baptiste Boric 
28959f988b79SJean-Baptiste Boric 	lb_size = udf_rw32(ump->logical_vol->lb_size);
28969f988b79SJean-Baptiste Boric 
28979f988b79SJean-Baptiste Boric 	/* max_len in unit's IFF its a metadata node or metadata mirror node */
28989f988b79SJean-Baptiste Boric 	unit_size = lb_size;
28999f988b79SJean-Baptiste Boric 	if ((udf_node == ump->metadata_node) || (udf_node == ump->metadatamirror_node))
29009f988b79SJean-Baptiste Boric 		unit_size = ump->metadata_alloc_unit_size * lb_size;
29019f988b79SJean-Baptiste Boric 	max_len = ((UDF_EXT_MAXLEN / unit_size) * unit_size);
29029f988b79SJean-Baptiste Boric 
29039f988b79SJean-Baptiste Boric 	/* do the work */
29049f988b79SJean-Baptiste Boric 	fe  = udf_node->fe;
29059f988b79SJean-Baptiste Boric 	efe = udf_node->efe;
29069f988b79SJean-Baptiste Boric 	if (fe) {
29079f988b79SJean-Baptiste Boric 		icbtag  = &fe->icbtag;
29089f988b79SJean-Baptiste Boric 		inflen  = udf_rw64(fe->inf_len);
29099f988b79SJean-Baptiste Boric 		objsize = inflen;
29109f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct file_entry) -1;
29119f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(fe->l_ea);
29129f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(fe->l_ad);
29139f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) fe + dscr_size + l_ea;
29149f988b79SJean-Baptiste Boric 	} else {
29159f988b79SJean-Baptiste Boric 		icbtag  = &efe->icbtag;
29169f988b79SJean-Baptiste Boric 		inflen  = udf_rw64(efe->inf_len);
29179f988b79SJean-Baptiste Boric 		objsize = udf_rw64(efe->obj_size);
29189f988b79SJean-Baptiste Boric 		dscr_size  = sizeof(struct extfile_entry) -1;
29199f988b79SJean-Baptiste Boric 		l_ea       = udf_rw32(efe->l_ea);
29209f988b79SJean-Baptiste Boric 		l_ad       = udf_rw32(efe->l_ad);
29219f988b79SJean-Baptiste Boric 		data_pos = (uint8_t *) efe + dscr_size + l_ea;
29229f988b79SJean-Baptiste Boric 	}
29239f988b79SJean-Baptiste Boric 	max_l_ad = lb_size - dscr_size - l_ea;
29249f988b79SJean-Baptiste Boric 
29259f988b79SJean-Baptiste Boric 	icbflags   = udf_rw16(icbtag->flags);
29269f988b79SJean-Baptiste Boric 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
29279f988b79SJean-Baptiste Boric 
29289f988b79SJean-Baptiste Boric 	old_size  = inflen;
29299f988b79SJean-Baptiste Boric 	size_diff = old_size - new_size;
29309f988b79SJean-Baptiste Boric 
29319f988b79SJean-Baptiste Boric 	DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
29329f988b79SJean-Baptiste Boric 
29339f988b79SJean-Baptiste Boric 	/* shrink the node to its new size */
29349f988b79SJean-Baptiste Boric 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
29359f988b79SJean-Baptiste Boric 		/* only reflect size change directly in the node */
29369f988b79SJean-Baptiste Boric 		KASSERT(new_size <= max_l_ad);
29379f988b79SJean-Baptiste Boric 		inflen  -= size_diff;
29389f988b79SJean-Baptiste Boric 		objsize -= size_diff;
29399f988b79SJean-Baptiste Boric 		l_ad    -= size_diff;
29409f988b79SJean-Baptiste Boric 		crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
29419f988b79SJean-Baptiste Boric 		if (fe) {
29429f988b79SJean-Baptiste Boric 			fe->inf_len   = udf_rw64(inflen);
29439f988b79SJean-Baptiste Boric 			fe->l_ad      = udf_rw32(l_ad);
29449f988b79SJean-Baptiste Boric 			fe->tag.desc_crc_len = udf_rw16(crclen);
29459f988b79SJean-Baptiste Boric 		} else {
29469f988b79SJean-Baptiste Boric 			efe->inf_len  = udf_rw64(inflen);
29479f988b79SJean-Baptiste Boric 			efe->obj_size = udf_rw64(objsize);
29489f988b79SJean-Baptiste Boric 			efe->l_ad     = udf_rw32(l_ad);
29499f988b79SJean-Baptiste Boric 			efe->tag.desc_crc_len = udf_rw16(crclen);
29509f988b79SJean-Baptiste Boric 		}
29519f988b79SJean-Baptiste Boric 		error = 0;
29529f988b79SJean-Baptiste Boric 
29539f988b79SJean-Baptiste Boric 		/* clear the space in the descriptor */
2954*0a6a1f1dSLionel Sambuc 		KASSERT(old_size >= new_size);
29559f988b79SJean-Baptiste Boric 		memset(data_pos + new_size, 0, old_size - new_size);
29569f988b79SJean-Baptiste Boric 
29579f988b79SJean-Baptiste Boric 		/* TODO zero appened space in buffer! */
29589f988b79SJean-Baptiste Boric 		/* using ubc_zerorange(&vp->v_uobj, old_size, */
29599f988b79SJean-Baptiste Boric 		/*    old_size - new_size, UBC_UNMAP_FLAG(vp)); ? */
29609f988b79SJean-Baptiste Boric 
29619f988b79SJean-Baptiste Boric 		/* set new size for uvm */
29629f988b79SJean-Baptiste Boric 		uvm_vnp_setsize(vp, new_size);
29639f988b79SJean-Baptiste Boric 
29649f988b79SJean-Baptiste Boric 		udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
29659f988b79SJean-Baptiste Boric 		UDF_UNLOCK_NODE(udf_node, 0);
29669f988b79SJean-Baptiste Boric 
29679f988b79SJean-Baptiste Boric 		KASSERT(new_inflen == orig_inflen - size_diff);
29689f988b79SJean-Baptiste Boric 		KASSERT(new_lbrec == orig_lbrec);
29699f988b79SJean-Baptiste Boric 		KASSERT(new_lbrec == 0);
29709f988b79SJean-Baptiste Boric 
29719f988b79SJean-Baptiste Boric 		return 0;
29729f988b79SJean-Baptiste Boric 	}
29739f988b79SJean-Baptiste Boric 
29749f988b79SJean-Baptiste Boric 	/* setup node cleanup extents copy space */
29759f988b79SJean-Baptiste Boric 	node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
29769f988b79SJean-Baptiste Boric 		M_UDFMNT, M_WAITOK);
29779f988b79SJean-Baptiste Boric 	memset(node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
29789f988b79SJean-Baptiste Boric 
29799f988b79SJean-Baptiste Boric 	/*
29809f988b79SJean-Baptiste Boric 	 * Shrink the node by releasing the allocations and truncate the last
29819f988b79SJean-Baptiste Boric 	 * allocation to the new size. If the new size fits into the
29829f988b79SJean-Baptiste Boric 	 * allocation descriptor itself, transform it into an
29839f988b79SJean-Baptiste Boric 	 * UDF_ICB_INTERN_ALLOC.
29849f988b79SJean-Baptiste Boric 	 */
29859f988b79SJean-Baptiste Boric 	slot     = 0;
29869f988b79SJean-Baptiste Boric 	cpy_slot = 0;
29879f988b79SJean-Baptiste Boric 	foffset  = 0;
29889f988b79SJean-Baptiste Boric 
29899f988b79SJean-Baptiste Boric 	/* 1) copy till first overlap piece to the rewrite buffer */
29909f988b79SJean-Baptiste Boric 	for (;;) {
29919f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
29929f988b79SJean-Baptiste Boric 		if (eof) {
29939f988b79SJean-Baptiste Boric 			DPRINTF(WRITE,
29949f988b79SJean-Baptiste Boric 				("Shrink node failed: "
29959f988b79SJean-Baptiste Boric 				 "encountered EOF\n"));
29969f988b79SJean-Baptiste Boric 			error = EINVAL;
29979f988b79SJean-Baptiste Boric 			goto errorout; /* panic? */
29989f988b79SJean-Baptiste Boric 		}
29999f988b79SJean-Baptiste Boric 		len   = udf_rw32(s_ad.len);
30009f988b79SJean-Baptiste Boric 		flags = UDF_EXT_FLAGS(len);
30019f988b79SJean-Baptiste Boric 		len   = UDF_EXT_LEN(len);
30029f988b79SJean-Baptiste Boric 
30039f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
30049f988b79SJean-Baptiste Boric 			slot++;
30059f988b79SJean-Baptiste Boric 			continue;
30069f988b79SJean-Baptiste Boric 		}
30079f988b79SJean-Baptiste Boric 
30089f988b79SJean-Baptiste Boric 		end_foffset = foffset + len;
30099f988b79SJean-Baptiste Boric 		if (end_foffset > new_size)
30109f988b79SJean-Baptiste Boric 			break;	/* found */
30119f988b79SJean-Baptiste Boric 
30129f988b79SJean-Baptiste Boric 		node_ad_cpy[cpy_slot++] = s_ad;
30139f988b79SJean-Baptiste Boric 
30149f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
30159f988b79SJean-Baptiste Boric 			"-> stack\n",
30169f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num),
30179f988b79SJean-Baptiste Boric 			udf_rw32(s_ad.loc.lb_num),
30189f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
30199f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
30209f988b79SJean-Baptiste Boric 
30219f988b79SJean-Baptiste Boric 		foffset = end_foffset;
30229f988b79SJean-Baptiste Boric 		slot++;
30239f988b79SJean-Baptiste Boric 	}
30249f988b79SJean-Baptiste Boric 	slot_offset = new_size - foffset;
30259f988b79SJean-Baptiste Boric 
30269f988b79SJean-Baptiste Boric 	/* 2) trunc overlapping slot at overlap and copy it */
30279f988b79SJean-Baptiste Boric 	if (slot_offset > 0) {
30289f988b79SJean-Baptiste Boric 		lb_num    = udf_rw32(s_ad.loc.lb_num);
30299f988b79SJean-Baptiste Boric 		vpart_num = udf_rw16(s_ad.loc.part_num);
30309f988b79SJean-Baptiste Boric 
30319f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_ALLOCATED) {
30329f988b79SJean-Baptiste Boric 			/* calculate extent in lb, and offset in lb */
30339f988b79SJean-Baptiste Boric 			num_lb = (len + lb_size -1) / lb_size;
30349f988b79SJean-Baptiste Boric 			slot_offset_lb = (slot_offset + lb_size -1) / lb_size;
30359f988b79SJean-Baptiste Boric 
30369f988b79SJean-Baptiste Boric 			/* adjust our slot */
30379f988b79SJean-Baptiste Boric 			lb_num += slot_offset_lb;
30389f988b79SJean-Baptiste Boric 			num_lb -= slot_offset_lb;
30399f988b79SJean-Baptiste Boric 
30409f988b79SJean-Baptiste Boric 			udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
30419f988b79SJean-Baptiste Boric 		}
30429f988b79SJean-Baptiste Boric 
30439f988b79SJean-Baptiste Boric 		s_ad.len = udf_rw32(slot_offset | flags);
30449f988b79SJean-Baptiste Boric 		node_ad_cpy[cpy_slot++] = s_ad;
30459f988b79SJean-Baptiste Boric 		slot++;
30469f988b79SJean-Baptiste Boric 
30479f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
30489f988b79SJean-Baptiste Boric 			"-> stack\n",
30499f988b79SJean-Baptiste Boric 			udf_rw16(s_ad.loc.part_num),
30509f988b79SJean-Baptiste Boric 			udf_rw32(s_ad.loc.lb_num),
30519f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
30529f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
30539f988b79SJean-Baptiste Boric 	}
30549f988b79SJean-Baptiste Boric 
30559f988b79SJean-Baptiste Boric 	/* 3) delete remainder */
30569f988b79SJean-Baptiste Boric 	for (;;) {
30579f988b79SJean-Baptiste Boric 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
30589f988b79SJean-Baptiste Boric 		if (eof)
30599f988b79SJean-Baptiste Boric 			break;
30609f988b79SJean-Baptiste Boric 
30619f988b79SJean-Baptiste Boric 		len       = udf_rw32(s_ad.len);
30629f988b79SJean-Baptiste Boric 		flags     = UDF_EXT_FLAGS(len);
30639f988b79SJean-Baptiste Boric 		len       = UDF_EXT_LEN(len);
30649f988b79SJean-Baptiste Boric 
30659f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_REDIRECT) {
30669f988b79SJean-Baptiste Boric 			slot++;
30679f988b79SJean-Baptiste Boric 			continue;
30689f988b79SJean-Baptiste Boric 		}
30699f988b79SJean-Baptiste Boric 
30709f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t3: delete remainder "
30719f988b79SJean-Baptiste Boric 			"vp %d lb %d, len %d, flags %d\n",
30729f988b79SJean-Baptiste Boric 		udf_rw16(s_ad.loc.part_num),
30739f988b79SJean-Baptiste Boric 		udf_rw32(s_ad.loc.lb_num),
30749f988b79SJean-Baptiste Boric 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
30759f988b79SJean-Baptiste Boric 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
30769f988b79SJean-Baptiste Boric 
30779f988b79SJean-Baptiste Boric 		if (flags == UDF_EXT_ALLOCATED) {
30789f988b79SJean-Baptiste Boric 			lb_num    = udf_rw32(s_ad.loc.lb_num);
30799f988b79SJean-Baptiste Boric 			vpart_num = udf_rw16(s_ad.loc.part_num);
30809f988b79SJean-Baptiste Boric 			num_lb    = (len + lb_size - 1) / lb_size;
30819f988b79SJean-Baptiste Boric 
30829f988b79SJean-Baptiste Boric 			udf_free_allocated_space(ump, lb_num, vpart_num,
30839f988b79SJean-Baptiste Boric 				num_lb);
30849f988b79SJean-Baptiste Boric 		}
30859f988b79SJean-Baptiste Boric 
30869f988b79SJean-Baptiste Boric 		slot++;
30879f988b79SJean-Baptiste Boric 	}
30889f988b79SJean-Baptiste Boric 
30899f988b79SJean-Baptiste Boric 	/* 4) if it will fit into the descriptor then convert */
30909f988b79SJean-Baptiste Boric 	if (new_size < max_l_ad) {
30919f988b79SJean-Baptiste Boric 		/*
30929f988b79SJean-Baptiste Boric 		 * resque/evacuate old piece by reading it in, and convert it
30939f988b79SJean-Baptiste Boric 		 * to internal alloc.
30949f988b79SJean-Baptiste Boric 		 */
30959f988b79SJean-Baptiste Boric 		if (new_size == 0) {
30969f988b79SJean-Baptiste Boric 			/* XXX/TODO only for zero sizing now */
30979f988b79SJean-Baptiste Boric 			udf_wipe_adslots(udf_node);
30989f988b79SJean-Baptiste Boric 
30999f988b79SJean-Baptiste Boric 			icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
31009f988b79SJean-Baptiste Boric 			icbflags |=  UDF_ICB_INTERN_ALLOC;
31019f988b79SJean-Baptiste Boric 			icbtag->flags = udf_rw16(icbflags);
31029f988b79SJean-Baptiste Boric 
31039f988b79SJean-Baptiste Boric 			inflen  -= size_diff;	KASSERT(inflen == 0);
31049f988b79SJean-Baptiste Boric 			objsize -= size_diff;
31059f988b79SJean-Baptiste Boric 			l_ad     = new_size;
31069f988b79SJean-Baptiste Boric 			crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
31079f988b79SJean-Baptiste Boric 			if (fe) {
31089f988b79SJean-Baptiste Boric 				fe->inf_len   = udf_rw64(inflen);
31099f988b79SJean-Baptiste Boric 				fe->l_ad      = udf_rw32(l_ad);
31109f988b79SJean-Baptiste Boric 				fe->tag.desc_crc_len = udf_rw16(crclen);
31119f988b79SJean-Baptiste Boric 			} else {
31129f988b79SJean-Baptiste Boric 				efe->inf_len  = udf_rw64(inflen);
31139f988b79SJean-Baptiste Boric 				efe->obj_size = udf_rw64(objsize);
31149f988b79SJean-Baptiste Boric 				efe->l_ad     = udf_rw32(l_ad);
31159f988b79SJean-Baptiste Boric 				efe->tag.desc_crc_len = udf_rw16(crclen);
31169f988b79SJean-Baptiste Boric 			}
31179f988b79SJean-Baptiste Boric 			/* eventually copy in evacuated piece */
31189f988b79SJean-Baptiste Boric 			/* set new size for uvm */
31199f988b79SJean-Baptiste Boric 			uvm_vnp_setsize(vp, new_size);
31209f988b79SJean-Baptiste Boric 
31219f988b79SJean-Baptiste Boric 			free(node_ad_cpy, M_UDFMNT);
31229f988b79SJean-Baptiste Boric 			udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
31239f988b79SJean-Baptiste Boric 
31249f988b79SJean-Baptiste Boric 			UDF_UNLOCK_NODE(udf_node, 0);
31259f988b79SJean-Baptiste Boric 
31269f988b79SJean-Baptiste Boric 			KASSERT(new_inflen == orig_inflen - size_diff);
31279f988b79SJean-Baptiste Boric 			KASSERT(new_inflen == 0);
31289f988b79SJean-Baptiste Boric 			KASSERT(new_lbrec == 0);
31299f988b79SJean-Baptiste Boric 
31309f988b79SJean-Baptiste Boric 			return 0;
31319f988b79SJean-Baptiste Boric 		}
31329f988b79SJean-Baptiste Boric 
31339f988b79SJean-Baptiste Boric 		printf("UDF_SHRINK_NODE: could convert to internal alloc!\n");
31349f988b79SJean-Baptiste Boric 	}
31359f988b79SJean-Baptiste Boric 
31369f988b79SJean-Baptiste Boric 	/* 5) reset node descriptors */
31379f988b79SJean-Baptiste Boric 	udf_wipe_adslots(udf_node);
31389f988b79SJean-Baptiste Boric 
31399f988b79SJean-Baptiste Boric 	/* 6) copy back extents; merge when possible. Recounting on the fly */
31409f988b79SJean-Baptiste Boric 	cpy_slots = cpy_slot;
31419f988b79SJean-Baptiste Boric 
31429f988b79SJean-Baptiste Boric 	c_ad = node_ad_cpy[0];
31439f988b79SJean-Baptiste Boric 	slot = 0;
31449f988b79SJean-Baptiste Boric 	for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
31459f988b79SJean-Baptiste Boric 		s_ad = node_ad_cpy[cpy_slot];
31469f988b79SJean-Baptiste Boric 
31479f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t6: stack -> got mapping vp %d "
31489f988b79SJean-Baptiste Boric 			"lb %d, len %d, flags %d\n",
31499f988b79SJean-Baptiste Boric 		udf_rw16(s_ad.loc.part_num),
31509f988b79SJean-Baptiste Boric 		udf_rw32(s_ad.loc.lb_num),
31519f988b79SJean-Baptiste Boric 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
31529f988b79SJean-Baptiste Boric 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
31539f988b79SJean-Baptiste Boric 
31549f988b79SJean-Baptiste Boric 		/* see if we can merge */
31559f988b79SJean-Baptiste Boric 		if (udf_ads_merge(max_len, lb_size, &c_ad, &s_ad)) {
31569f988b79SJean-Baptiste Boric 			/* not mergable (anymore) */
31579f988b79SJean-Baptiste Boric 			DPRINTF(ALLOC, ("\t6: appending vp %d lb %d, "
31589f988b79SJean-Baptiste Boric 				"len %d, flags %d\n",
31599f988b79SJean-Baptiste Boric 			udf_rw16(c_ad.loc.part_num),
31609f988b79SJean-Baptiste Boric 			udf_rw32(c_ad.loc.lb_num),
31619f988b79SJean-Baptiste Boric 			UDF_EXT_LEN(udf_rw32(c_ad.len)),
31629f988b79SJean-Baptiste Boric 			UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
31639f988b79SJean-Baptiste Boric 
31649f988b79SJean-Baptiste Boric 			error = udf_append_adslot(udf_node, &slot, &c_ad);
31659f988b79SJean-Baptiste Boric 			if (error)
31669f988b79SJean-Baptiste Boric 				goto errorout; /* panic? */
31679f988b79SJean-Baptiste Boric 			c_ad = s_ad;
31689f988b79SJean-Baptiste Boric 			slot++;
31699f988b79SJean-Baptiste Boric 		}
31709f988b79SJean-Baptiste Boric 	}
31719f988b79SJean-Baptiste Boric 
31729f988b79SJean-Baptiste Boric 	/* 7) push rest slot (if any) */
31739f988b79SJean-Baptiste Boric 	if (UDF_EXT_LEN(c_ad.len) > 0) {
31749f988b79SJean-Baptiste Boric 		DPRINTF(ALLOC, ("\t7: last append vp %d lb %d, "
31759f988b79SJean-Baptiste Boric 				"len %d, flags %d\n",
31769f988b79SJean-Baptiste Boric 		udf_rw16(c_ad.loc.part_num),
31779f988b79SJean-Baptiste Boric 		udf_rw32(c_ad.loc.lb_num),
31789f988b79SJean-Baptiste Boric 		UDF_EXT_LEN(udf_rw32(c_ad.len)),
31799f988b79SJean-Baptiste Boric 		UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
31809f988b79SJean-Baptiste Boric 
31819f988b79SJean-Baptiste Boric 		error = udf_append_adslot(udf_node, &slot, &c_ad);
31829f988b79SJean-Baptiste Boric 		if (error)
31839f988b79SJean-Baptiste Boric 			goto errorout; /* panic? */
31849f988b79SJean-Baptiste Boric 		;
31859f988b79SJean-Baptiste Boric 	}
31869f988b79SJean-Baptiste Boric 
31879f988b79SJean-Baptiste Boric 	inflen  -= size_diff;
31889f988b79SJean-Baptiste Boric 	objsize -= size_diff;
31899f988b79SJean-Baptiste Boric 	if (fe) {
31909f988b79SJean-Baptiste Boric 		fe->inf_len   = udf_rw64(inflen);
31919f988b79SJean-Baptiste Boric 	} else {
31929f988b79SJean-Baptiste Boric 		efe->inf_len  = udf_rw64(inflen);
31939f988b79SJean-Baptiste Boric 		efe->obj_size = udf_rw64(objsize);
31949f988b79SJean-Baptiste Boric 	}
31959f988b79SJean-Baptiste Boric 	error = 0;
31969f988b79SJean-Baptiste Boric 
31979f988b79SJean-Baptiste Boric 	/* set new size for uvm */
31989f988b79SJean-Baptiste Boric 	uvm_vnp_setsize(vp, new_size);
31999f988b79SJean-Baptiste Boric 
32009f988b79SJean-Baptiste Boric errorout:
32019f988b79SJean-Baptiste Boric 	free(node_ad_cpy, M_UDFMNT);
32029f988b79SJean-Baptiste Boric 
32039f988b79SJean-Baptiste Boric 	udf_count_alloc_exts(udf_node);
32049f988b79SJean-Baptiste Boric 
32059f988b79SJean-Baptiste Boric 	udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
32069f988b79SJean-Baptiste Boric 	UDF_UNLOCK_NODE(udf_node, 0);
32079f988b79SJean-Baptiste Boric 
32089f988b79SJean-Baptiste Boric 	KASSERT(new_inflen == orig_inflen - size_diff);
32099f988b79SJean-Baptiste Boric 
32109f988b79SJean-Baptiste Boric 	return error;
32119f988b79SJean-Baptiste Boric }
32129f988b79SJean-Baptiste Boric 
3213