Home | History | Annotate | Line # | Download | only in udf
udf_allocation.c revision 1.19
      1  1.19    pooka /* $NetBSD: udf_allocation.c,v 1.19 2008/12/16 16:18:25 pooka Exp $ */
      2   1.1  reinoud 
      3   1.1  reinoud /*
      4   1.1  reinoud  * Copyright (c) 2006, 2008 Reinoud Zandijk
      5   1.1  reinoud  * All rights reserved.
      6   1.1  reinoud  *
      7   1.1  reinoud  * Redistribution and use in source and binary forms, with or without
      8   1.1  reinoud  * modification, are permitted provided that the following conditions
      9   1.1  reinoud  * are met:
     10   1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     11   1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     12   1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     15   1.1  reinoud  *
     16   1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1  reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1  reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1  reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1  reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1  reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1  reinoud  *
     27   1.1  reinoud  */
     28   1.1  reinoud 
     29   1.1  reinoud #include <sys/cdefs.h>
     30   1.1  reinoud #ifndef lint
     31  1.19    pooka __KERNEL_RCSID(0, "$NetBSD: udf_allocation.c,v 1.19 2008/12/16 16:18:25 pooka Exp $");
     32   1.1  reinoud #endif /* not lint */
     33   1.1  reinoud 
     34   1.1  reinoud 
     35   1.1  reinoud #if defined(_KERNEL_OPT)
     36   1.1  reinoud #include "opt_compat_netbsd.h"
     37   1.1  reinoud #endif
     38   1.1  reinoud 
     39   1.1  reinoud /* TODO strip */
     40   1.1  reinoud #include <sys/param.h>
     41   1.1  reinoud #include <sys/systm.h>
     42   1.1  reinoud #include <sys/sysctl.h>
     43   1.1  reinoud #include <sys/namei.h>
     44   1.1  reinoud #include <sys/proc.h>
     45   1.1  reinoud #include <sys/kernel.h>
     46   1.1  reinoud #include <sys/vnode.h>
     47   1.1  reinoud #include <miscfs/genfs/genfs_node.h>
     48   1.1  reinoud #include <sys/mount.h>
     49   1.1  reinoud #include <sys/buf.h>
     50   1.1  reinoud #include <sys/file.h>
     51   1.1  reinoud #include <sys/device.h>
     52   1.1  reinoud #include <sys/disklabel.h>
     53   1.1  reinoud #include <sys/ioctl.h>
     54   1.1  reinoud #include <sys/malloc.h>
     55   1.1  reinoud #include <sys/dirent.h>
     56   1.1  reinoud #include <sys/stat.h>
     57   1.1  reinoud #include <sys/conf.h>
     58   1.1  reinoud #include <sys/kauth.h>
     59   1.1  reinoud #include <sys/kthread.h>
     60   1.1  reinoud #include <dev/clock_subr.h>
     61   1.1  reinoud 
     62   1.1  reinoud #include <fs/udf/ecma167-udf.h>
     63   1.1  reinoud #include <fs/udf/udf_mount.h>
     64   1.1  reinoud 
     65   1.1  reinoud #include "udf.h"
     66   1.1  reinoud #include "udf_subr.h"
     67   1.1  reinoud #include "udf_bswap.h"
     68   1.1  reinoud 
     69   1.1  reinoud 
     70   1.1  reinoud #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
     71   1.1  reinoud 
     72   1.1  reinoud static void udf_record_allocation_in_node(struct udf_mount *ump,
     73   1.1  reinoud 	struct buf *buf, uint16_t vpart_num, uint64_t *mapping,
     74   1.1  reinoud 	struct long_ad *node_ad_cpy);
     75   1.1  reinoud 
     76   1.1  reinoud /*
     77   1.1  reinoud  * IDEA/BUSY: Each udf_node gets its own extentwalker state for all operations;
     78   1.1  reinoud  * this will hopefully/likely reduce O(nlog(n)) to O(1) for most functionality
     79   1.1  reinoud  * since actions are most likely sequencial and thus seeking doesn't need
     80   1.1  reinoud  * searching for the same or adjacent position again.
     81   1.1  reinoud  */
     82   1.1  reinoud 
     83   1.1  reinoud /* --------------------------------------------------------------------- */
     84  1.11  reinoud 
     85  1.12  reinoud #if 0
     86   1.1  reinoud #if 1
     87   1.1  reinoud static void
     88   1.1  reinoud udf_node_dump(struct udf_node *udf_node) {
     89   1.1  reinoud 	struct file_entry    *fe;
     90   1.1  reinoud 	struct extfile_entry *efe;
     91   1.1  reinoud 	struct icb_tag *icbtag;
     92  1.11  reinoud 	struct long_ad s_ad;
     93   1.1  reinoud 	uint64_t inflen;
     94  1.11  reinoud 	uint32_t icbflags, addr_type;
     95   1.1  reinoud 	uint32_t len, lb_num;
     96  1.11  reinoud 	uint32_t flags;
     97   1.1  reinoud 	int part_num;
     98  1.11  reinoud 	int lb_size, eof, slot;
     99   1.1  reinoud 
    100  1.10  reinoud 	if ((udf_verbose & UDF_DEBUG_NODEDUMP) == 0)
    101   1.1  reinoud 		return;
    102   1.1  reinoud 
    103   1.1  reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    104   1.1  reinoud 
    105   1.1  reinoud 	fe  = udf_node->fe;
    106   1.1  reinoud 	efe = udf_node->efe;
    107   1.1  reinoud 	if (fe) {
    108   1.1  reinoud 		icbtag = &fe->icbtag;
    109   1.1  reinoud 		inflen = udf_rw64(fe->inf_len);
    110   1.1  reinoud 	} else {
    111   1.1  reinoud 		icbtag = &efe->icbtag;
    112   1.1  reinoud 		inflen = udf_rw64(efe->inf_len);
    113   1.1  reinoud 	}
    114   1.1  reinoud 
    115   1.1  reinoud 	icbflags   = udf_rw16(icbtag->flags);
    116   1.1  reinoud 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
    117   1.1  reinoud 
    118  1.11  reinoud 	printf("udf_node_dump %p :\n", udf_node);
    119   1.1  reinoud 
    120   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
    121  1.11  reinoud 		printf("\tIntern alloc, len = %"PRIu64"\n", inflen);
    122   1.1  reinoud 		return;
    123   1.1  reinoud 	}
    124   1.1  reinoud 
    125  1.11  reinoud 	printf("\tInflen  = %"PRIu64"\n", inflen);
    126  1.11  reinoud 	printf("\t\t");
    127   1.1  reinoud 
    128  1.11  reinoud 	slot = 0;
    129  1.11  reinoud 	for (;;) {
    130  1.11  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
    131  1.11  reinoud 		if (eof)
    132  1.11  reinoud 			break;
    133  1.11  reinoud 		part_num = udf_rw16(s_ad.loc.part_num);
    134  1.11  reinoud 		lb_num = udf_rw32(s_ad.loc.lb_num);
    135  1.11  reinoud 		len   = udf_rw32(s_ad.len);
    136  1.11  reinoud 		flags = UDF_EXT_FLAGS(len);
    137  1.11  reinoud 		len   = UDF_EXT_LEN(len);
    138   1.1  reinoud 
    139   1.1  reinoud 		printf("[");
    140   1.1  reinoud 		if (part_num >= 0)
    141   1.1  reinoud 			printf("part %d, ", part_num);
    142   1.1  reinoud 		printf("lb_num %d, len %d", lb_num, len);
    143   1.1  reinoud 		if (flags)
    144  1.10  reinoud 			printf(", flags %d", flags>>30);
    145   1.1  reinoud 		printf("] ");
    146  1.11  reinoud 
    147  1.11  reinoud 		if (flags == UDF_EXT_REDIRECT) {
    148  1.11  reinoud 			printf("\n\textent END\n\tallocation extent\n\t\t");
    149  1.11  reinoud 		}
    150  1.11  reinoud 
    151  1.11  reinoud 		slot++;
    152   1.1  reinoud 	}
    153  1.11  reinoud 	printf("\n\tl_ad END\n\n");
    154   1.1  reinoud }
    155   1.1  reinoud #else
    156   1.1  reinoud #define udf_node_dump(a)
    157   1.1  reinoud #endif
    158   1.1  reinoud 
    159   1.9  reinoud 
    160   1.9  reinoud static void
    161   1.9  reinoud udf_assert_allocated(struct udf_mount *ump, uint16_t vpart_num,
    162   1.9  reinoud 	uint32_t lb_num, uint32_t num_lb)
    163   1.9  reinoud {
    164   1.9  reinoud 	struct udf_bitmap *bitmap;
    165   1.9  reinoud 	struct part_desc *pdesc;
    166   1.9  reinoud 	uint32_t ptov;
    167   1.9  reinoud 	uint32_t bitval;
    168   1.9  reinoud 	uint8_t *bpos;
    169   1.9  reinoud 	int bit;
    170   1.9  reinoud 	int phys_part;
    171   1.9  reinoud 	int ok;
    172   1.9  reinoud 
    173  1.10  reinoud 	DPRINTF(PARANOIA, ("udf_assert_allocated: check virt lbnum %d "
    174   1.9  reinoud 			  "part %d + %d sect\n", lb_num, vpart_num, num_lb));
    175   1.9  reinoud 
    176   1.9  reinoud 	/* get partition backing up this vpart_num */
    177   1.9  reinoud 	pdesc = ump->partitions[ump->vtop[vpart_num]];
    178   1.9  reinoud 
    179   1.9  reinoud 	switch (ump->vtop_tp[vpart_num]) {
    180   1.9  reinoud 	case UDF_VTOP_TYPE_PHYS :
    181   1.9  reinoud 	case UDF_VTOP_TYPE_SPARABLE :
    182   1.9  reinoud 		/* free space to freed or unallocated space bitmap */
    183   1.9  reinoud 		ptov      = udf_rw32(pdesc->start_loc);
    184   1.9  reinoud 		phys_part = ump->vtop[vpart_num];
    185   1.9  reinoud 
    186   1.9  reinoud 		/* use unallocated bitmap */
    187   1.9  reinoud 		bitmap = &ump->part_unalloc_bits[phys_part];
    188   1.9  reinoud 
    189   1.9  reinoud 		/* if no bitmaps are defined, bail out */
    190   1.9  reinoud 		if (bitmap->bits == NULL)
    191   1.9  reinoud 			break;
    192   1.9  reinoud 
    193   1.9  reinoud 		/* check bits */
    194   1.9  reinoud 		KASSERT(bitmap->bits);
    195   1.9  reinoud 		ok = 1;
    196   1.9  reinoud 		bpos = bitmap->bits + lb_num/8;
    197   1.9  reinoud 		bit  = lb_num % 8;
    198   1.9  reinoud 		while (num_lb > 0) {
    199   1.9  reinoud 			bitval = (1 << bit);
    200   1.9  reinoud 			DPRINTF(PARANOIA, ("XXX : check %d, %p, bit %d\n",
    201   1.9  reinoud 				lb_num, bpos, bit));
    202   1.9  reinoud 			KASSERT(bitmap->bits + lb_num/8 == bpos);
    203   1.9  reinoud 			if (*bpos & bitval) {
    204   1.9  reinoud 				printf("\tlb_num %d is NOT marked busy\n",
    205   1.9  reinoud 					lb_num);
    206   1.9  reinoud 				ok = 0;
    207   1.9  reinoud 			}
    208   1.9  reinoud 			lb_num++; num_lb--;
    209   1.9  reinoud 			bit = (bit + 1) % 8;
    210   1.9  reinoud 			if (bit == 0)
    211   1.9  reinoud 				bpos++;
    212   1.9  reinoud 		}
    213   1.9  reinoud 		if (!ok) {
    214   1.9  reinoud 			/* KASSERT(0); */
    215   1.9  reinoud 		}
    216   1.9  reinoud 
    217   1.9  reinoud 		break;
    218   1.9  reinoud 	case UDF_VTOP_TYPE_VIRT :
    219   1.9  reinoud 		/* TODO check space */
    220   1.9  reinoud 		KASSERT(num_lb == 1);
    221   1.9  reinoud 		break;
    222   1.9  reinoud 	case UDF_VTOP_TYPE_META :
    223   1.9  reinoud 		/* TODO check space in the metadata bitmap */
    224   1.9  reinoud 	default:
    225   1.9  reinoud 		/* not implemented */
    226   1.9  reinoud 		break;
    227   1.9  reinoud 	}
    228   1.9  reinoud }
    229   1.9  reinoud 
    230   1.9  reinoud 
    231   1.1  reinoud static void
    232   1.1  reinoud udf_node_sanity_check(struct udf_node *udf_node,
    233  1.17  reinoud 		uint64_t *cnt_inflen, uint64_t *cnt_logblksrec)
    234  1.17  reinoud {
    235  1.17  reinoud 	union dscrptr *dscr;
    236   1.1  reinoud 	struct file_entry    *fe;
    237   1.1  reinoud 	struct extfile_entry *efe;
    238   1.1  reinoud 	struct icb_tag *icbtag;
    239  1.11  reinoud 	struct long_ad  s_ad;
    240   1.1  reinoud 	uint64_t inflen, logblksrec;
    241  1.11  reinoud 	uint32_t icbflags, addr_type;
    242  1.11  reinoud 	uint32_t len, lb_num, l_ea, l_ad, max_l_ad;
    243   1.9  reinoud 	uint16_t part_num;
    244  1.17  reinoud 	uint8_t *data_pos;
    245  1.11  reinoud 	int dscr_size, lb_size, flags, whole_lb;
    246  1.17  reinoud 	int i, slot, eof;
    247   1.1  reinoud 
    248   1.9  reinoud //	KASSERT(mutex_owned(&udf_node->ump->allocate_mutex));
    249   1.1  reinoud 
    250  1.10  reinoud 	if (1)
    251  1.10  reinoud 		udf_node_dump(udf_node);
    252  1.10  reinoud 
    253   1.1  reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    254   1.1  reinoud 
    255   1.1  reinoud 	fe  = udf_node->fe;
    256   1.1  reinoud 	efe = udf_node->efe;
    257   1.1  reinoud 	if (fe) {
    258  1.17  reinoud 		dscr       = (union dscrptr *) fe;
    259  1.17  reinoud 		icbtag     = &fe->icbtag;
    260  1.17  reinoud 		inflen     = udf_rw64(fe->inf_len);
    261  1.11  reinoud 		dscr_size  = sizeof(struct file_entry) -1;
    262   1.1  reinoud 		logblksrec = udf_rw64(fe->logblks_rec);
    263  1.11  reinoud 		l_ad       = udf_rw32(fe->l_ad);
    264   1.1  reinoud 		l_ea       = udf_rw32(fe->l_ea);
    265   1.1  reinoud 	} else {
    266  1.17  reinoud 		dscr       = (union dscrptr *) efe;
    267  1.17  reinoud 		icbtag     = &efe->icbtag;
    268  1.17  reinoud 		inflen     = udf_rw64(efe->inf_len);
    269  1.11  reinoud 		dscr_size  = sizeof(struct extfile_entry) -1;
    270   1.1  reinoud 		logblksrec = udf_rw64(efe->logblks_rec);
    271  1.11  reinoud 		l_ad       = udf_rw32(efe->l_ad);
    272   1.1  reinoud 		l_ea       = udf_rw32(efe->l_ea);
    273   1.1  reinoud 	}
    274  1.17  reinoud 	data_pos  = (uint8_t *) dscr + dscr_size + l_ea;
    275  1.11  reinoud 	max_l_ad   = lb_size - dscr_size - l_ea;
    276   1.1  reinoud 	icbflags   = udf_rw16(icbtag->flags);
    277   1.1  reinoud 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
    278   1.1  reinoud 
    279  1.17  reinoud 	/* check if tail is zero */
    280  1.17  reinoud 	DPRINTF(PARANOIA, ("Sanity check blank tail\n"));
    281  1.17  reinoud 	for (i = l_ad; i < max_l_ad; i++) {
    282  1.17  reinoud 		if (data_pos[i] != 0)
    283  1.17  reinoud 			printf( "sanity_check: violation: node byte %d "
    284  1.17  reinoud 				"has value %d\n", i, data_pos[i]);
    285  1.17  reinoud 	}
    286  1.17  reinoud 
    287   1.1  reinoud 	/* reset counters */
    288   1.1  reinoud 	*cnt_inflen     = 0;
    289   1.1  reinoud 	*cnt_logblksrec = 0;
    290   1.1  reinoud 
    291   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
    292   1.1  reinoud 		KASSERT(l_ad <= max_l_ad);
    293   1.1  reinoud 		KASSERT(l_ad == inflen);
    294   1.1  reinoud 		*cnt_inflen = inflen;
    295   1.1  reinoud 		return;
    296   1.1  reinoud 	}
    297   1.1  reinoud 
    298   1.1  reinoud 	/* start counting */
    299   1.1  reinoud 	whole_lb = 1;
    300  1.11  reinoud 	slot = 0;
    301  1.11  reinoud 	for (;;) {
    302  1.11  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
    303  1.11  reinoud 		if (eof)
    304  1.11  reinoud 			break;
    305   1.1  reinoud 		KASSERT(whole_lb == 1);
    306  1.11  reinoud 
    307  1.11  reinoud 		part_num = udf_rw16(s_ad.loc.part_num);
    308  1.11  reinoud 		lb_num = udf_rw32(s_ad.loc.lb_num);
    309  1.11  reinoud 		len   = udf_rw32(s_ad.len);
    310  1.11  reinoud 		flags = UDF_EXT_FLAGS(len);
    311  1.11  reinoud 		len   = UDF_EXT_LEN(len);
    312  1.11  reinoud 
    313   1.6  reinoud 		if (flags != UDF_EXT_REDIRECT) {
    314   1.6  reinoud 			*cnt_inflen += len;
    315   1.6  reinoud 			if (flags == UDF_EXT_ALLOCATED) {
    316   1.6  reinoud 				*cnt_logblksrec += (len + lb_size -1) / lb_size;
    317   1.6  reinoud 			}
    318   1.6  reinoud 		} else {
    319   1.6  reinoud 			KASSERT(len == lb_size);
    320   1.1  reinoud 		}
    321   1.9  reinoud 		/* check allocation */
    322   1.9  reinoud 		if (flags == UDF_EXT_ALLOCATED)
    323   1.9  reinoud 			udf_assert_allocated(udf_node->ump, part_num, lb_num,
    324   1.9  reinoud 				(len + lb_size - 1) / lb_size);
    325   1.8  reinoud 
    326   1.8  reinoud 		/* check whole lb */
    327   1.1  reinoud 		whole_lb = ((len % lb_size) == 0);
    328  1.11  reinoud 
    329  1.11  reinoud 		slot++;
    330   1.1  reinoud 	}
    331   1.1  reinoud 	/* rest should be zero (ad_off > l_ad < max_l_ad - adlen) */
    332   1.1  reinoud 
    333   1.1  reinoud 	KASSERT(*cnt_inflen == inflen);
    334   1.1  reinoud 	KASSERT(*cnt_logblksrec == logblksrec);
    335   1.1  reinoud 
    336   1.9  reinoud //	KASSERT(mutex_owned(&udf_node->ump->allocate_mutex));
    337   1.1  reinoud }
    338   1.1  reinoud #else
    339  1.12  reinoud static void
    340  1.12  reinoud udf_node_sanity_check(struct udf_node *udf_node,
    341  1.12  reinoud 		uint64_t *cnt_inflen, uint64_t *cnt_logblksrec) {
    342  1.12  reinoud 	struct file_entry    *fe;
    343  1.12  reinoud 	struct extfile_entry *efe;
    344  1.12  reinoud 	struct icb_tag *icbtag;
    345  1.12  reinoud 	uint64_t inflen, logblksrec;
    346  1.12  reinoud 	int dscr_size, lb_size;
    347  1.12  reinoud 
    348  1.12  reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    349  1.12  reinoud 
    350  1.12  reinoud 	fe  = udf_node->fe;
    351  1.12  reinoud 	efe = udf_node->efe;
    352  1.12  reinoud 	if (fe) {
    353  1.12  reinoud 		icbtag = &fe->icbtag;
    354  1.12  reinoud 		inflen = udf_rw64(fe->inf_len);
    355  1.12  reinoud 		dscr_size  = sizeof(struct file_entry) -1;
    356  1.12  reinoud 		logblksrec = udf_rw64(fe->logblks_rec);
    357  1.12  reinoud 	} else {
    358  1.12  reinoud 		icbtag = &efe->icbtag;
    359  1.12  reinoud 		inflen = udf_rw64(efe->inf_len);
    360  1.12  reinoud 		dscr_size  = sizeof(struct extfile_entry) -1;
    361  1.12  reinoud 		logblksrec = udf_rw64(efe->logblks_rec);
    362  1.12  reinoud 	}
    363  1.12  reinoud 	*cnt_logblksrec = logblksrec;
    364  1.12  reinoud 	*cnt_inflen     = inflen;
    365  1.12  reinoud }
    366   1.1  reinoud #endif
    367   1.1  reinoud 
    368   1.1  reinoud /* --------------------------------------------------------------------- */
    369   1.1  reinoud 
    370   1.1  reinoud int
    371   1.1  reinoud udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
    372   1.1  reinoud 		   uint32_t *lb_numres, uint32_t *extres)
    373   1.1  reinoud {
    374   1.1  reinoud 	struct part_desc       *pdesc;
    375   1.1  reinoud 	struct spare_map_entry *sme;
    376   1.1  reinoud 	struct long_ad s_icb_loc;
    377   1.1  reinoud 	uint64_t foffset, end_foffset;
    378   1.1  reinoud 	uint32_t lb_size, len;
    379   1.1  reinoud 	uint32_t lb_num, lb_rel, lb_packet;
    380   1.1  reinoud 	uint32_t udf_rw32_lbmap, ext_offset;
    381   1.1  reinoud 	uint16_t vpart;
    382   1.1  reinoud 	int rel, part, error, eof, slot, flags;
    383   1.1  reinoud 
    384   1.1  reinoud 	assert(ump && icb_loc && lb_numres);
    385   1.1  reinoud 
    386   1.1  reinoud 	vpart  = udf_rw16(icb_loc->loc.part_num);
    387   1.1  reinoud 	lb_num = udf_rw32(icb_loc->loc.lb_num);
    388   1.1  reinoud 	if (vpart > UDF_VTOP_RAWPART)
    389   1.1  reinoud 		return EINVAL;
    390   1.1  reinoud 
    391   1.1  reinoud translate_again:
    392   1.1  reinoud 	part = ump->vtop[vpart];
    393   1.1  reinoud 	pdesc = ump->partitions[part];
    394   1.1  reinoud 
    395   1.1  reinoud 	switch (ump->vtop_tp[vpart]) {
    396   1.1  reinoud 	case UDF_VTOP_TYPE_RAW :
    397   1.1  reinoud 		/* 1:1 to the end of the device */
    398   1.1  reinoud 		*lb_numres = lb_num;
    399   1.1  reinoud 		*extres = INT_MAX;
    400   1.1  reinoud 		return 0;
    401   1.1  reinoud 	case UDF_VTOP_TYPE_PHYS :
    402   1.1  reinoud 		/* transform into its disc logical block */
    403   1.1  reinoud 		if (lb_num > udf_rw32(pdesc->part_len))
    404   1.1  reinoud 			return EINVAL;
    405   1.1  reinoud 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
    406   1.1  reinoud 
    407   1.1  reinoud 		/* extent from here to the end of the partition */
    408   1.1  reinoud 		*extres = udf_rw32(pdesc->part_len) - lb_num;
    409   1.1  reinoud 		return 0;
    410   1.1  reinoud 	case UDF_VTOP_TYPE_VIRT :
    411   1.1  reinoud 		/* only maps one logical block, lookup in VAT */
    412   1.1  reinoud 		if (lb_num >= ump->vat_entries)		/* XXX > or >= ? */
    413   1.1  reinoud 			return EINVAL;
    414   1.1  reinoud 
    415   1.1  reinoud 		/* lookup in virtual allocation table file */
    416   1.1  reinoud 		mutex_enter(&ump->allocate_mutex);
    417   1.1  reinoud 		error = udf_vat_read(ump->vat_node,
    418   1.1  reinoud 				(uint8_t *) &udf_rw32_lbmap, 4,
    419   1.1  reinoud 				ump->vat_offset + lb_num * 4);
    420   1.1  reinoud 		mutex_exit(&ump->allocate_mutex);
    421   1.1  reinoud 
    422   1.1  reinoud 		if (error)
    423   1.1  reinoud 			return error;
    424   1.1  reinoud 
    425   1.1  reinoud 		lb_num = udf_rw32(udf_rw32_lbmap);
    426   1.1  reinoud 
    427   1.1  reinoud 		/* transform into its disc logical block */
    428   1.1  reinoud 		if (lb_num > udf_rw32(pdesc->part_len))
    429   1.1  reinoud 			return EINVAL;
    430   1.1  reinoud 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
    431   1.1  reinoud 
    432   1.1  reinoud 		/* just one logical block */
    433   1.1  reinoud 		*extres = 1;
    434   1.1  reinoud 		return 0;
    435   1.1  reinoud 	case UDF_VTOP_TYPE_SPARABLE :
    436   1.1  reinoud 		/* check if the packet containing the lb_num is remapped */
    437   1.1  reinoud 		lb_packet = lb_num / ump->sparable_packet_size;
    438   1.1  reinoud 		lb_rel    = lb_num % ump->sparable_packet_size;
    439   1.1  reinoud 
    440   1.1  reinoud 		for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
    441   1.1  reinoud 			sme = &ump->sparing_table->entries[rel];
    442   1.1  reinoud 			if (lb_packet == udf_rw32(sme->org)) {
    443   1.1  reinoud 				/* NOTE maps to absolute disc logical block! */
    444   1.1  reinoud 				*lb_numres = udf_rw32(sme->map) + lb_rel;
    445   1.1  reinoud 				*extres    = ump->sparable_packet_size - lb_rel;
    446   1.1  reinoud 				return 0;
    447   1.1  reinoud 			}
    448   1.1  reinoud 		}
    449   1.1  reinoud 
    450   1.1  reinoud 		/* transform into its disc logical block */
    451   1.1  reinoud 		if (lb_num > udf_rw32(pdesc->part_len))
    452   1.1  reinoud 			return EINVAL;
    453   1.1  reinoud 		*lb_numres = lb_num + udf_rw32(pdesc->start_loc);
    454   1.1  reinoud 
    455   1.1  reinoud 		/* rest of block */
    456   1.1  reinoud 		*extres = ump->sparable_packet_size - lb_rel;
    457   1.1  reinoud 		return 0;
    458   1.1  reinoud 	case UDF_VTOP_TYPE_META :
    459   1.1  reinoud 		/* we have to look into the file's allocation descriptors */
    460   1.1  reinoud 
    461   1.1  reinoud 		/* use metadatafile allocation mutex */
    462   1.1  reinoud 		lb_size = udf_rw32(ump->logical_vol->lb_size);
    463   1.1  reinoud 
    464   1.1  reinoud 		UDF_LOCK_NODE(ump->metadata_node, 0);
    465   1.1  reinoud 
    466   1.1  reinoud 		/* get first overlapping extent */
    467   1.1  reinoud 		foffset = 0;
    468   1.1  reinoud 		slot    = 0;
    469   1.1  reinoud 		for (;;) {
    470   1.1  reinoud 			udf_get_adslot(ump->metadata_node,
    471   1.1  reinoud 				slot, &s_icb_loc, &eof);
    472   1.5  reinoud 			DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, "
    473   1.5  reinoud 				"len = %d, lb_num = %d, part = %d\n",
    474   1.5  reinoud 				slot, eof,
    475   1.5  reinoud 				UDF_EXT_FLAGS(udf_rw32(s_icb_loc.len)),
    476   1.5  reinoud 				UDF_EXT_LEN(udf_rw32(s_icb_loc.len)),
    477   1.5  reinoud 				udf_rw32(s_icb_loc.loc.lb_num),
    478   1.5  reinoud 				udf_rw16(s_icb_loc.loc.part_num)));
    479   1.1  reinoud 			if (eof) {
    480   1.1  reinoud 				DPRINTF(TRANSLATE,
    481   1.1  reinoud 					("Meta partition translation "
    482   1.1  reinoud 					 "failed: can't seek location\n"));
    483   1.1  reinoud 				UDF_UNLOCK_NODE(ump->metadata_node, 0);
    484   1.1  reinoud 				return EINVAL;
    485   1.1  reinoud 			}
    486   1.1  reinoud 			len   = udf_rw32(s_icb_loc.len);
    487   1.1  reinoud 			flags = UDF_EXT_FLAGS(len);
    488   1.1  reinoud 			len   = UDF_EXT_LEN(len);
    489   1.1  reinoud 
    490   1.5  reinoud 			if (flags == UDF_EXT_REDIRECT) {
    491   1.5  reinoud 				slot++;
    492   1.5  reinoud 				continue;
    493   1.5  reinoud 			}
    494   1.5  reinoud 
    495   1.1  reinoud 			end_foffset = foffset + len;
    496   1.1  reinoud 
    497   1.1  reinoud 			if (end_foffset > lb_num * lb_size)
    498   1.1  reinoud 				break;	/* found */
    499   1.5  reinoud 			foffset = end_foffset;
    500   1.1  reinoud 			slot++;
    501   1.1  reinoud 		}
    502   1.1  reinoud 		/* found overlapping slot */
    503   1.1  reinoud 		ext_offset = lb_num * lb_size - foffset;
    504   1.1  reinoud 
    505   1.1  reinoud 		/* process extent offset */
    506   1.1  reinoud 		lb_num   = udf_rw32(s_icb_loc.loc.lb_num);
    507   1.1  reinoud 		vpart    = udf_rw16(s_icb_loc.loc.part_num);
    508   1.1  reinoud 		lb_num  += (ext_offset + lb_size -1) / lb_size;
    509   1.1  reinoud 		len     -= ext_offset;
    510   1.1  reinoud 		ext_offset = 0;
    511   1.1  reinoud 
    512   1.1  reinoud 		flags = UDF_EXT_FLAGS(s_icb_loc.len);
    513   1.1  reinoud 
    514   1.1  reinoud 		UDF_UNLOCK_NODE(ump->metadata_node, 0);
    515   1.1  reinoud 		if (flags != UDF_EXT_ALLOCATED) {
    516   1.1  reinoud 			DPRINTF(TRANSLATE, ("Metadata partition translation "
    517   1.1  reinoud 					    "failed: not allocated\n"));
    518   1.1  reinoud 			return EINVAL;
    519   1.1  reinoud 		}
    520   1.1  reinoud 
    521   1.1  reinoud 		/*
    522   1.1  reinoud 		 * vpart and lb_num are updated, translate again since we
    523   1.1  reinoud 		 * might be mapped on sparable media
    524   1.1  reinoud 		 */
    525   1.1  reinoud 		goto translate_again;
    526   1.1  reinoud 	default:
    527   1.1  reinoud 		printf("UDF vtop translation scheme %d unimplemented yet\n",
    528   1.1  reinoud 			ump->vtop_tp[vpart]);
    529   1.1  reinoud 	}
    530   1.1  reinoud 
    531   1.1  reinoud 	return EINVAL;
    532   1.1  reinoud }
    533   1.1  reinoud 
    534  1.16  reinoud 
    535  1.16  reinoud /* XXX  provisional primitive braindead version */
    536  1.16  reinoud /* TODO use ext_res */
    537  1.16  reinoud void
    538  1.16  reinoud udf_translate_vtop_list(struct udf_mount *ump, uint32_t sectors,
    539  1.16  reinoud 	uint16_t vpart_num, uint64_t *lmapping, uint64_t *pmapping)
    540  1.16  reinoud {
    541  1.16  reinoud 	struct long_ad loc;
    542  1.16  reinoud 	uint32_t lb_numres, ext_res;
    543  1.16  reinoud 	int sector;
    544  1.16  reinoud 
    545  1.16  reinoud 	for (sector = 0; sector < sectors; sector++) {
    546  1.16  reinoud 		memset(&loc, 0, sizeof(struct long_ad));
    547  1.16  reinoud 		loc.loc.part_num = udf_rw16(vpart_num);
    548  1.16  reinoud 		loc.loc.lb_num   = udf_rw32(*lmapping);
    549  1.16  reinoud 		udf_translate_vtop(ump, &loc, &lb_numres, &ext_res);
    550  1.16  reinoud 		*pmapping = lb_numres;
    551  1.16  reinoud 		lmapping++; pmapping++;
    552  1.16  reinoud 	}
    553  1.16  reinoud }
    554  1.16  reinoud 
    555  1.16  reinoud 
    556   1.1  reinoud /* --------------------------------------------------------------------- */
    557   1.1  reinoud 
    558   1.1  reinoud /*
    559   1.1  reinoud  * Translate an extent (in logical_blocks) into logical block numbers; used
    560   1.1  reinoud  * for read and write operations. DOESNT't check extents.
    561   1.1  reinoud  */
    562   1.1  reinoud 
    563   1.1  reinoud int
    564   1.1  reinoud udf_translate_file_extent(struct udf_node *udf_node,
    565   1.1  reinoud 		          uint32_t from, uint32_t num_lb,
    566   1.1  reinoud 			  uint64_t *map)
    567   1.1  reinoud {
    568   1.1  reinoud 	struct udf_mount *ump;
    569   1.1  reinoud 	struct icb_tag *icbtag;
    570   1.1  reinoud 	struct long_ad t_ad, s_ad;
    571   1.1  reinoud 	uint64_t transsec;
    572   1.1  reinoud 	uint64_t foffset, end_foffset;
    573   1.1  reinoud 	uint32_t transsec32;
    574   1.1  reinoud 	uint32_t lb_size;
    575   1.1  reinoud 	uint32_t ext_offset;
    576   1.1  reinoud 	uint32_t lb_num, len;
    577   1.1  reinoud 	uint32_t overlap, translen;
    578   1.1  reinoud 	uint16_t vpart_num;
    579   1.1  reinoud 	int eof, error, flags;
    580   1.1  reinoud 	int slot, addr_type, icbflags;
    581   1.1  reinoud 
    582   1.1  reinoud 	if (!udf_node)
    583   1.1  reinoud 		return ENOENT;
    584   1.1  reinoud 
    585   1.1  reinoud 	KASSERT(num_lb > 0);
    586   1.1  reinoud 
    587   1.1  reinoud 	UDF_LOCK_NODE(udf_node, 0);
    588   1.1  reinoud 
    589   1.1  reinoud 	/* initialise derivative vars */
    590   1.1  reinoud 	ump = udf_node->ump;
    591   1.1  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    592   1.1  reinoud 
    593   1.1  reinoud 	if (udf_node->fe) {
    594   1.1  reinoud 		icbtag = &udf_node->fe->icbtag;
    595   1.1  reinoud 	} else {
    596   1.1  reinoud 		icbtag = &udf_node->efe->icbtag;
    597   1.1  reinoud 	}
    598   1.1  reinoud 	icbflags  = udf_rw16(icbtag->flags);
    599   1.1  reinoud 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
    600   1.1  reinoud 
    601   1.1  reinoud 	/* do the work */
    602   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
    603   1.1  reinoud 		*map = UDF_TRANS_INTERN;
    604   1.1  reinoud 		UDF_UNLOCK_NODE(udf_node, 0);
    605   1.1  reinoud 		return 0;
    606   1.1  reinoud 	}
    607   1.1  reinoud 
    608   1.1  reinoud 	/* find first overlapping extent */
    609   1.1  reinoud 	foffset = 0;
    610   1.1  reinoud 	slot    = 0;
    611   1.1  reinoud 	for (;;) {
    612   1.1  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
    613   1.4  reinoud 		DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
    614   1.4  reinoud 			"lb_num = %d, part = %d\n", slot, eof,
    615   1.4  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
    616   1.4  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
    617   1.4  reinoud 			udf_rw32(s_ad.loc.lb_num),
    618   1.4  reinoud 			udf_rw16(s_ad.loc.part_num)));
    619   1.1  reinoud 		if (eof) {
    620   1.1  reinoud 			DPRINTF(TRANSLATE,
    621   1.1  reinoud 				("Translate file extent "
    622   1.1  reinoud 				 "failed: can't seek location\n"));
    623   1.1  reinoud 			UDF_UNLOCK_NODE(udf_node, 0);
    624   1.1  reinoud 			return EINVAL;
    625   1.1  reinoud 		}
    626   1.1  reinoud 		len    = udf_rw32(s_ad.len);
    627   1.1  reinoud 		flags  = UDF_EXT_FLAGS(len);
    628   1.1  reinoud 		len    = UDF_EXT_LEN(len);
    629   1.1  reinoud 		lb_num = udf_rw32(s_ad.loc.lb_num);
    630   1.1  reinoud 
    631   1.1  reinoud 		if (flags == UDF_EXT_REDIRECT) {
    632   1.1  reinoud 			slot++;
    633   1.1  reinoud 			continue;
    634   1.1  reinoud 		}
    635   1.1  reinoud 
    636   1.1  reinoud 		end_foffset = foffset + len;
    637   1.1  reinoud 
    638   1.1  reinoud 		if (end_foffset > from * lb_size)
    639   1.1  reinoud 			break;	/* found */
    640   1.1  reinoud 		foffset = end_foffset;
    641   1.1  reinoud 		slot++;
    642   1.1  reinoud 	}
    643   1.1  reinoud 	/* found overlapping slot */
    644   1.1  reinoud 	ext_offset = from * lb_size - foffset;
    645   1.1  reinoud 
    646   1.1  reinoud 	for (;;) {
    647   1.1  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
    648   1.4  reinoud 		DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
    649   1.4  reinoud 			"lb_num = %d, part = %d\n", slot, eof,
    650   1.4  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)),
    651   1.4  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
    652   1.4  reinoud 			udf_rw32(s_ad.loc.lb_num),
    653   1.4  reinoud 			udf_rw16(s_ad.loc.part_num)));
    654   1.1  reinoud 		if (eof) {
    655   1.1  reinoud 			DPRINTF(TRANSLATE,
    656   1.1  reinoud 				("Translate file extent "
    657   1.1  reinoud 				 "failed: past eof\n"));
    658   1.1  reinoud 			UDF_UNLOCK_NODE(udf_node, 0);
    659   1.1  reinoud 			return EINVAL;
    660   1.1  reinoud 		}
    661   1.1  reinoud 
    662   1.1  reinoud 		len    = udf_rw32(s_ad.len);
    663   1.1  reinoud 		flags  = UDF_EXT_FLAGS(len);
    664   1.1  reinoud 		len    = UDF_EXT_LEN(len);
    665   1.1  reinoud 
    666   1.1  reinoud 		lb_num    = udf_rw32(s_ad.loc.lb_num);
    667   1.1  reinoud 		vpart_num = udf_rw16(s_ad.loc.part_num);
    668   1.1  reinoud 
    669   1.1  reinoud 		end_foffset = foffset + len;
    670   1.1  reinoud 
    671   1.1  reinoud 		/* process extent, don't forget to advance on ext_offset! */
    672   1.1  reinoud 		lb_num  += (ext_offset + lb_size -1) / lb_size;
    673   1.1  reinoud 		overlap  = (len - ext_offset + lb_size -1) / lb_size;
    674   1.1  reinoud 		ext_offset = 0;
    675   1.1  reinoud 
    676   1.1  reinoud 		/*
    677   1.1  reinoud 		 * note that the while(){} is nessisary for the extent that
    678   1.1  reinoud 		 * the udf_translate_vtop() returns doens't have to span the
    679   1.1  reinoud 		 * whole extent.
    680   1.1  reinoud 		 */
    681   1.1  reinoud 
    682   1.1  reinoud 		overlap = MIN(overlap, num_lb);
    683   1.4  reinoud 		while (overlap && (flags != UDF_EXT_REDIRECT)) {
    684   1.1  reinoud 			switch (flags) {
    685   1.1  reinoud 			case UDF_EXT_FREE :
    686   1.1  reinoud 			case UDF_EXT_ALLOCATED_BUT_NOT_USED :
    687   1.1  reinoud 				transsec = UDF_TRANS_ZERO;
    688   1.1  reinoud 				translen = overlap;
    689   1.1  reinoud 				while (overlap && num_lb && translen) {
    690   1.1  reinoud 					*map++ = transsec;
    691   1.1  reinoud 					lb_num++;
    692   1.1  reinoud 					overlap--; num_lb--; translen--;
    693   1.1  reinoud 				}
    694   1.1  reinoud 				break;
    695   1.1  reinoud 			case UDF_EXT_ALLOCATED :
    696   1.1  reinoud 				t_ad.loc.lb_num   = udf_rw32(lb_num);
    697   1.1  reinoud 				t_ad.loc.part_num = udf_rw16(vpart_num);
    698   1.1  reinoud 				error = udf_translate_vtop(ump,
    699   1.1  reinoud 						&t_ad, &transsec32, &translen);
    700   1.1  reinoud 				transsec = transsec32;
    701   1.1  reinoud 				if (error) {
    702   1.1  reinoud 					UDF_UNLOCK_NODE(udf_node, 0);
    703   1.1  reinoud 					return error;
    704   1.1  reinoud 				}
    705   1.1  reinoud 				while (overlap && num_lb && translen) {
    706   1.1  reinoud 					*map++ = transsec;
    707   1.1  reinoud 					lb_num++; transsec++;
    708   1.1  reinoud 					overlap--; num_lb--; translen--;
    709   1.1  reinoud 				}
    710   1.1  reinoud 				break;
    711   1.4  reinoud 			default:
    712   1.4  reinoud 				DPRINTF(TRANSLATE,
    713   1.4  reinoud 					("Translate file extent "
    714   1.4  reinoud 					 "failed: bad flags %x\n", flags));
    715   1.4  reinoud 				UDF_UNLOCK_NODE(udf_node, 0);
    716   1.4  reinoud 				return EINVAL;
    717   1.1  reinoud 			}
    718   1.1  reinoud 		}
    719   1.1  reinoud 		if (num_lb == 0)
    720   1.1  reinoud 			break;
    721   1.1  reinoud 
    722   1.1  reinoud 		if (flags != UDF_EXT_REDIRECT)
    723   1.1  reinoud 			foffset = end_foffset;
    724   1.1  reinoud 		slot++;
    725   1.1  reinoud 	}
    726   1.1  reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
    727   1.1  reinoud 
    728   1.1  reinoud 	return 0;
    729   1.1  reinoud }
    730   1.1  reinoud 
    731   1.1  reinoud /* --------------------------------------------------------------------- */
    732   1.1  reinoud 
    733   1.1  reinoud static int
    734   1.1  reinoud udf_search_free_vatloc(struct udf_mount *ump, uint32_t *lbnumres)
    735   1.1  reinoud {
    736   1.1  reinoud 	uint32_t lb_size, lb_num, lb_map, udf_rw32_lbmap;
    737   1.1  reinoud 	uint8_t *blob;
    738   1.1  reinoud 	int entry, chunk, found, error;
    739   1.1  reinoud 
    740   1.1  reinoud 	KASSERT(ump);
    741   1.1  reinoud 	KASSERT(ump->logical_vol);
    742   1.1  reinoud 
    743   1.1  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    744   1.1  reinoud 	blob = malloc(lb_size, M_UDFTEMP, M_WAITOK);
    745   1.1  reinoud 
    746   1.1  reinoud 	/* TODO static allocation of search chunk */
    747   1.1  reinoud 
    748   1.1  reinoud 	lb_num = MIN(ump->vat_entries, ump->vat_last_free_lb);
    749   1.1  reinoud 	found  = 0;
    750   1.1  reinoud 	error  = 0;
    751   1.1  reinoud 	entry  = 0;
    752   1.1  reinoud 	do {
    753   1.1  reinoud 		chunk = MIN(lb_size, (ump->vat_entries - lb_num) * 4);
    754   1.1  reinoud 		if (chunk <= 0)
    755   1.1  reinoud 			break;
    756   1.1  reinoud 		/* load in chunk */
    757   1.1  reinoud 		error = udf_vat_read(ump->vat_node, blob, chunk,
    758   1.1  reinoud 				ump->vat_offset + lb_num * 4);
    759   1.1  reinoud 
    760   1.1  reinoud 		if (error)
    761   1.1  reinoud 			break;
    762   1.1  reinoud 
    763   1.1  reinoud 		/* search this chunk */
    764   1.1  reinoud 		for (entry=0; entry < chunk /4; entry++, lb_num++) {
    765   1.1  reinoud 			udf_rw32_lbmap = *((uint32_t *) (blob + entry * 4));
    766   1.1  reinoud 			lb_map = udf_rw32(udf_rw32_lbmap);
    767   1.1  reinoud 			if (lb_map == 0xffffffff) {
    768   1.1  reinoud 				found = 1;
    769   1.1  reinoud 				break;
    770   1.1  reinoud 			}
    771   1.1  reinoud 		}
    772   1.1  reinoud 	} while (!found);
    773   1.1  reinoud 	if (error) {
    774   1.1  reinoud 		printf("udf_search_free_vatloc: error reading in vat chunk "
    775   1.1  reinoud 			"(lb %d, size %d)\n", lb_num, chunk);
    776   1.1  reinoud 	}
    777   1.1  reinoud 
    778   1.1  reinoud 	if (!found) {
    779   1.1  reinoud 		/* extend VAT */
    780   1.1  reinoud 		DPRINTF(WRITE, ("udf_search_free_vatloc: extending\n"));
    781   1.1  reinoud 		lb_num = ump->vat_entries;
    782   1.1  reinoud 		ump->vat_entries++;
    783   1.1  reinoud 	}
    784   1.1  reinoud 
    785   1.1  reinoud 	/* mark entry with initialiser just in case */
    786   1.1  reinoud 	lb_map = udf_rw32(0xfffffffe);
    787   1.1  reinoud 	udf_vat_write(ump->vat_node, (uint8_t *) &lb_map, 4,
    788   1.1  reinoud 		ump->vat_offset + lb_num *4);
    789   1.1  reinoud 	ump->vat_last_free_lb = lb_num;
    790   1.1  reinoud 
    791   1.1  reinoud 	free(blob, M_UDFTEMP);
    792   1.1  reinoud 	*lbnumres = lb_num;
    793   1.1  reinoud 	return 0;
    794   1.1  reinoud }
    795   1.1  reinoud 
    796   1.1  reinoud 
    797   1.1  reinoud static void
    798   1.1  reinoud udf_bitmap_allocate(struct udf_bitmap *bitmap, int ismetadata,
    799  1.16  reinoud 	uint32_t *num_lb, uint64_t *lmappos)
    800   1.1  reinoud {
    801   1.1  reinoud 	uint32_t offset, lb_num, bit;
    802   1.1  reinoud 	int32_t  diff;
    803   1.1  reinoud 	uint8_t *bpos;
    804   1.1  reinoud 	int pass;
    805   1.1  reinoud 
    806   1.1  reinoud 	if (!ismetadata) {
    807   1.1  reinoud 		/* heuristic to keep the two pointers not too close */
    808   1.1  reinoud 		diff = bitmap->data_pos - bitmap->metadata_pos;
    809   1.1  reinoud 		if ((diff >= 0) && (diff < 1024))
    810   1.1  reinoud 			bitmap->data_pos = bitmap->metadata_pos + 1024;
    811   1.1  reinoud 	}
    812   1.1  reinoud 	offset = ismetadata ? bitmap->metadata_pos : bitmap->data_pos;
    813   1.1  reinoud 	offset &= ~7;
    814   1.1  reinoud 	for (pass = 0; pass < 2; pass++) {
    815   1.1  reinoud 		if (offset >= bitmap->max_offset)
    816   1.1  reinoud 			offset = 0;
    817   1.1  reinoud 
    818   1.1  reinoud 		while (offset < bitmap->max_offset) {
    819   1.1  reinoud 			if (*num_lb == 0)
    820   1.1  reinoud 				break;
    821   1.1  reinoud 
    822   1.1  reinoud 			/* use first bit not set */
    823   1.1  reinoud 			bpos  = bitmap->bits + offset/8;
    824   1.9  reinoud 			bit = ffs(*bpos);	/* returns 0 or 1..8 */
    825   1.1  reinoud 			if (bit == 0) {
    826   1.1  reinoud 				offset += 8;
    827   1.1  reinoud 				continue;
    828   1.1  reinoud 			}
    829   1.9  reinoud 			DPRINTF(PARANOIA, ("XXX : allocate %d, %p, bit %d\n",
    830   1.9  reinoud 				offset + bit -1, bpos, bit-1));
    831   1.1  reinoud 			*bpos &= ~(1 << (bit-1));
    832   1.1  reinoud 			lb_num = offset + bit-1;
    833   1.1  reinoud 			*lmappos++ = lb_num;
    834   1.1  reinoud 			*num_lb = *num_lb - 1;
    835   1.1  reinoud 			// offset = (offset & ~7);
    836   1.1  reinoud 		}
    837   1.1  reinoud 	}
    838   1.1  reinoud 
    839   1.1  reinoud 	if (ismetadata) {
    840   1.1  reinoud 		bitmap->metadata_pos = offset;
    841   1.1  reinoud 	} else {
    842   1.1  reinoud 		bitmap->data_pos = offset;
    843   1.1  reinoud 	}
    844   1.1  reinoud }
    845   1.1  reinoud 
    846   1.1  reinoud 
    847   1.1  reinoud static void
    848   1.1  reinoud udf_bitmap_free(struct udf_bitmap *bitmap, uint32_t lb_num, uint32_t num_lb)
    849   1.1  reinoud {
    850   1.1  reinoud 	uint32_t offset;
    851   1.1  reinoud 	uint32_t bit, bitval;
    852   1.1  reinoud 	uint8_t *bpos;
    853   1.1  reinoud 
    854   1.1  reinoud 	offset = lb_num;
    855   1.1  reinoud 
    856   1.1  reinoud 	/* starter bits */
    857   1.1  reinoud 	bpos = bitmap->bits + offset/8;
    858   1.1  reinoud 	bit = offset % 8;
    859   1.1  reinoud 	while ((bit != 0) && (num_lb > 0)) {
    860   1.1  reinoud 		bitval = (1 << bit);
    861   1.1  reinoud 		KASSERT((*bpos & bitval) == 0);
    862   1.9  reinoud 		DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n",
    863   1.9  reinoud 			offset, bpos, bit));
    864   1.1  reinoud 		*bpos |= bitval;
    865   1.1  reinoud 		offset++; num_lb--;
    866   1.1  reinoud 		bit = (bit + 1) % 8;
    867   1.1  reinoud 	}
    868   1.1  reinoud 	if (num_lb == 0)
    869   1.1  reinoud 		return;
    870   1.1  reinoud 
    871   1.1  reinoud 	/* whole bytes */
    872   1.1  reinoud 	KASSERT(bit == 0);
    873   1.1  reinoud 	bpos = bitmap->bits + offset / 8;
    874   1.1  reinoud 	while (num_lb >= 8) {
    875   1.1  reinoud 		KASSERT((*bpos == 0));
    876   1.9  reinoud 		DPRINTF(PARANOIA, ("XXX : free %d + 8, %p\n", offset, bpos));
    877   1.1  reinoud 		*bpos = 255;
    878   1.1  reinoud 		offset += 8; num_lb -= 8;
    879   1.1  reinoud 		bpos++;
    880   1.1  reinoud 	}
    881   1.1  reinoud 
    882   1.1  reinoud 	/* stop bits */
    883   1.1  reinoud 	KASSERT(num_lb < 8);
    884   1.1  reinoud 	bit = 0;
    885   1.1  reinoud 	while (num_lb > 0) {
    886   1.1  reinoud 		bitval = (1 << bit);
    887   1.1  reinoud 		KASSERT((*bpos & bitval) == 0);
    888   1.9  reinoud 		DPRINTF(PARANOIA, ("XXX : free %d, %p, %d\n",
    889   1.9  reinoud 			offset, bpos, bit));
    890   1.1  reinoud 		*bpos |= bitval;
    891   1.1  reinoud 		offset++; num_lb--;
    892   1.1  reinoud 		bit = (bit + 1) % 8;
    893   1.1  reinoud 	}
    894   1.1  reinoud }
    895   1.1  reinoud 
    896   1.1  reinoud 
    897   1.1  reinoud /* allocate a contiguous sequence of sectornumbers */
    898   1.1  reinoud static int
    899  1.17  reinoud udf_allocate_space(struct udf_mount *ump, int udf_c_type,
    900  1.17  reinoud 	uint16_t vpart_num, uint32_t num_lb, uint64_t *lmapping)
    901   1.1  reinoud {
    902   1.1  reinoud 	struct mmc_trackinfo *alloc_track, *other_track;
    903   1.1  reinoud 	struct udf_bitmap *bitmap;
    904   1.1  reinoud 	struct part_desc *pdesc;
    905   1.1  reinoud 	struct logvol_int_desc *lvid;
    906  1.16  reinoud 	uint64_t *lmappos;
    907   1.1  reinoud 	uint32_t ptov, lb_num, *freepos, free_lbs;
    908   1.1  reinoud 	int lb_size, alloc_num_lb;
    909  1.17  reinoud 	int alloc_type, error;
    910  1.17  reinoud 	int is_node;
    911   1.1  reinoud 
    912  1.17  reinoud 	DPRINTF(CALL, ("udf_allocate_space(ctype %d, vpart %d, num_lb %d\n",
    913  1.17  reinoud 		udf_c_type, vpart_num, num_lb));
    914   1.1  reinoud 	mutex_enter(&ump->allocate_mutex);
    915  1.16  reinoud 
    916   1.1  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    917   1.1  reinoud 	KASSERT(lb_size == ump->discinfo.sector_size);
    918   1.1  reinoud 
    919  1.17  reinoud 	/* XXX TODO check disc space */
    920   1.1  reinoud 
    921  1.17  reinoud 	alloc_type =  ump->vtop_alloc[vpart_num];
    922  1.17  reinoud 	is_node    = (udf_c_type == UDF_C_NODE);
    923   1.1  reinoud 
    924   1.1  reinoud 	lmappos = lmapping;
    925  1.16  reinoud 	error = 0;
    926   1.1  reinoud 	switch (alloc_type) {
    927   1.1  reinoud 	case UDF_ALLOC_VAT :
    928   1.1  reinoud 		/* search empty slot in VAT file */
    929   1.1  reinoud 		KASSERT(num_lb == 1);
    930   1.1  reinoud 		error = udf_search_free_vatloc(ump, &lb_num);
    931  1.16  reinoud 		if (!error)
    932   1.1  reinoud 			*lmappos = lb_num;
    933   1.1  reinoud 		break;
    934   1.1  reinoud 	case UDF_ALLOC_SEQUENTIAL :
    935   1.1  reinoud 		/* sequential allocation on recordable media */
    936  1.17  reinoud 		/* get partition backing up this vpart_num_num */
    937  1.17  reinoud 		pdesc = ump->partitions[ump->vtop[vpart_num]];
    938  1.16  reinoud 
    939   1.1  reinoud 		/* calculate offset from physical base partition */
    940   1.1  reinoud 		ptov  = udf_rw32(pdesc->start_loc);
    941   1.1  reinoud 
    942  1.17  reinoud 		/* get our track descriptors */
    943  1.17  reinoud 		if (vpart_num == ump->node_part) {
    944  1.17  reinoud 			alloc_track = &ump->metadata_track;
    945  1.17  reinoud 			other_track = &ump->data_track;
    946  1.17  reinoud 		} else {
    947  1.17  reinoud 			alloc_track = &ump->data_track;
    948  1.17  reinoud 			other_track = &ump->metadata_track;
    949  1.17  reinoud 		}
    950  1.17  reinoud 
    951  1.17  reinoud 		/* allocate */
    952   1.1  reinoud 		for (lb_num = 0; lb_num < num_lb; lb_num++) {
    953   1.1  reinoud 			*lmappos++ = alloc_track->next_writable - ptov;
    954   1.1  reinoud 			alloc_track->next_writable++;
    955   1.1  reinoud 			alloc_track->free_blocks--;
    956   1.1  reinoud 		}
    957  1.17  reinoud 
    958  1.17  reinoud 		/* keep other track up-to-date */
    959   1.1  reinoud 		if (alloc_track->tracknr == other_track->tracknr)
    960   1.1  reinoud 			memcpy(other_track, alloc_track,
    961   1.1  reinoud 				sizeof(struct mmc_trackinfo));
    962   1.1  reinoud 		break;
    963   1.1  reinoud 	case UDF_ALLOC_SPACEMAP :
    964  1.16  reinoud 		/* try to allocate on unallocated bits */
    965   1.1  reinoud 		alloc_num_lb = num_lb;
    966  1.17  reinoud 		bitmap = &ump->part_unalloc_bits[vpart_num];
    967  1.17  reinoud 		udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos);
    968   1.1  reinoud 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
    969  1.16  reinoud 
    970  1.16  reinoud 		/* have we allocated all? */
    971   1.1  reinoud 		if (alloc_num_lb) {
    972   1.1  reinoud 			/* TODO convert freed to unalloc and try again */
    973   1.1  reinoud 			/* free allocated piece for now */
    974   1.1  reinoud 			lmappos = lmapping;
    975   1.1  reinoud 			for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) {
    976   1.1  reinoud 				udf_bitmap_free(bitmap, *lmappos++, 1);
    977   1.1  reinoud 			}
    978   1.1  reinoud 			error = ENOSPC;
    979   1.1  reinoud 		}
    980   1.1  reinoud 		if (!error) {
    981   1.1  reinoud 			/* adjust freecount */
    982   1.1  reinoud 			lvid = ump->logvol_integrity;
    983  1.17  reinoud 			freepos = &lvid->tables[0] + vpart_num;
    984   1.1  reinoud 			free_lbs = udf_rw32(*freepos);
    985   1.1  reinoud 			*freepos = udf_rw32(free_lbs - num_lb);
    986   1.1  reinoud 		}
    987   1.1  reinoud 		break;
    988  1.15  reinoud 	case UDF_ALLOC_METABITMAP :		/* UDF 2.50, 2.60 BluRay-RE */
    989  1.16  reinoud 		/* allocate on metadata unallocated bits */
    990  1.16  reinoud 		alloc_num_lb = num_lb;
    991  1.16  reinoud 		bitmap = &ump->metadata_unalloc_bits;
    992  1.17  reinoud 		udf_bitmap_allocate(bitmap, is_node, &alloc_num_lb, lmappos);
    993  1.16  reinoud 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
    994  1.16  reinoud 
    995  1.16  reinoud 		/* have we allocated all? */
    996  1.17  reinoud 		if (alloc_num_lb) {
    997  1.16  reinoud 			/* YIKES! TODO we need to extend the metadata partition */
    998  1.16  reinoud 			/* free allocated piece for now */
    999  1.16  reinoud 			lmappos = lmapping;
   1000  1.16  reinoud 			for (lb_num=0; lb_num < num_lb-alloc_num_lb; lb_num++) {
   1001  1.16  reinoud 				udf_bitmap_free(bitmap, *lmappos++, 1);
   1002  1.16  reinoud 			}
   1003  1.16  reinoud 			error = ENOSPC;
   1004  1.16  reinoud 		}
   1005  1.16  reinoud 		if (!error) {
   1006  1.16  reinoud 			/* adjust freecount */
   1007  1.16  reinoud 			lvid = ump->logvol_integrity;
   1008  1.17  reinoud 			freepos = &lvid->tables[0] + vpart_num;
   1009  1.16  reinoud 			free_lbs = udf_rw32(*freepos);
   1010  1.16  reinoud 			*freepos = udf_rw32(free_lbs - num_lb);
   1011  1.16  reinoud 		}
   1012  1.16  reinoud 		break;
   1013  1.15  reinoud 	case UDF_ALLOC_METASEQUENTIAL :		/* UDF 2.60       BluRay-R  */
   1014  1.15  reinoud 	case UDF_ALLOC_RELAXEDSEQUENTIAL :	/* UDF 2.50/~meta BluRay-R  */
   1015   1.1  reinoud 		printf("ALERT: udf_allocate_space : allocation %d "
   1016   1.1  reinoud 				"not implemented yet!\n", alloc_type);
   1017   1.1  reinoud 		/* TODO implement, doesn't have to be contiguous */
   1018   1.1  reinoud 		error = ENOSPC;
   1019   1.1  reinoud 		break;
   1020   1.1  reinoud 	}
   1021   1.1  reinoud 
   1022   1.1  reinoud #ifdef DEBUG
   1023   1.1  reinoud 	if (udf_verbose & UDF_DEBUG_ALLOC) {
   1024   1.1  reinoud 		lmappos = lmapping;
   1025  1.16  reinoud 		printf("udf_allocate_space, allocated logical lba :\n");
   1026   1.1  reinoud 		for (lb_num = 0; lb_num < num_lb; lb_num++) {
   1027  1.16  reinoud 			printf("%s %"PRIu64",", (lb_num > 0)?",":"",
   1028  1.16  reinoud 				*lmappos++);
   1029   1.1  reinoud 		}
   1030  1.16  reinoud 		printf("\n");
   1031   1.1  reinoud 	}
   1032   1.1  reinoud #endif
   1033   1.1  reinoud 	mutex_exit(&ump->allocate_mutex);
   1034   1.1  reinoud 
   1035   1.1  reinoud 	return error;
   1036   1.1  reinoud }
   1037   1.1  reinoud 
   1038   1.1  reinoud /* --------------------------------------------------------------------- */
   1039   1.1  reinoud 
   1040   1.1  reinoud void
   1041   1.1  reinoud udf_free_allocated_space(struct udf_mount *ump, uint32_t lb_num,
   1042   1.1  reinoud 	uint16_t vpart_num, uint32_t num_lb)
   1043   1.1  reinoud {
   1044   1.1  reinoud 	struct udf_bitmap *bitmap;
   1045   1.1  reinoud 	struct part_desc *pdesc;
   1046   1.1  reinoud 	struct logvol_int_desc *lvid;
   1047   1.1  reinoud 	uint32_t ptov, lb_map, udf_rw32_lbmap;
   1048   1.1  reinoud 	uint32_t *freepos, free_lbs;
   1049   1.1  reinoud 	int phys_part;
   1050   1.1  reinoud 	int error;
   1051   1.1  reinoud 
   1052   1.1  reinoud 	DPRINTF(ALLOC, ("udf_free_allocated_space: freeing virt lbnum %d "
   1053   1.1  reinoud 			  "part %d + %d sect\n", lb_num, vpart_num, num_lb));
   1054   1.1  reinoud 
   1055  1.10  reinoud 	/* no use freeing zero length */
   1056  1.10  reinoud 	if (num_lb == 0)
   1057  1.10  reinoud 		return;
   1058  1.10  reinoud 
   1059   1.1  reinoud 	mutex_enter(&ump->allocate_mutex);
   1060   1.1  reinoud 
   1061   1.1  reinoud 	/* get partition backing up this vpart_num */
   1062   1.1  reinoud 	pdesc = ump->partitions[ump->vtop[vpart_num]];
   1063   1.1  reinoud 
   1064   1.1  reinoud 	switch (ump->vtop_tp[vpart_num]) {
   1065   1.1  reinoud 	case UDF_VTOP_TYPE_PHYS :
   1066   1.1  reinoud 	case UDF_VTOP_TYPE_SPARABLE :
   1067   1.1  reinoud 		/* free space to freed or unallocated space bitmap */
   1068   1.1  reinoud 		ptov      = udf_rw32(pdesc->start_loc);
   1069   1.1  reinoud 		phys_part = ump->vtop[vpart_num];
   1070   1.1  reinoud 
   1071   1.1  reinoud 		/* first try freed space bitmap */
   1072   1.1  reinoud 		bitmap    = &ump->part_freed_bits[phys_part];
   1073   1.1  reinoud 
   1074   1.1  reinoud 		/* if not defined, use unallocated bitmap */
   1075   1.1  reinoud 		if (bitmap->bits == NULL)
   1076   1.1  reinoud 			bitmap = &ump->part_unalloc_bits[phys_part];
   1077   1.1  reinoud 
   1078  1.17  reinoud 		/* if no bitmaps are defined, bail out; XXX OK? */
   1079   1.1  reinoud 		if (bitmap->bits == NULL)
   1080   1.1  reinoud 			break;
   1081   1.1  reinoud 
   1082   1.1  reinoud 		/* free bits if its defined */
   1083   1.1  reinoud 		KASSERT(bitmap->bits);
   1084   1.1  reinoud 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
   1085   1.1  reinoud 		udf_bitmap_free(bitmap, lb_num, num_lb);
   1086   1.1  reinoud 
   1087   1.1  reinoud 		/* adjust freecount */
   1088   1.1  reinoud 		lvid = ump->logvol_integrity;
   1089   1.1  reinoud 		freepos = &lvid->tables[0] + vpart_num;
   1090   1.1  reinoud 		free_lbs = udf_rw32(*freepos);
   1091   1.1  reinoud 		*freepos = udf_rw32(free_lbs + num_lb);
   1092   1.1  reinoud 		break;
   1093   1.1  reinoud 	case UDF_VTOP_TYPE_VIRT :
   1094   1.1  reinoud 		/* free this VAT entry */
   1095   1.1  reinoud 		KASSERT(num_lb == 1);
   1096   1.1  reinoud 
   1097   1.1  reinoud 		lb_map = 0xffffffff;
   1098   1.1  reinoud 		udf_rw32_lbmap = udf_rw32(lb_map);
   1099   1.1  reinoud 		error = udf_vat_write(ump->vat_node,
   1100   1.1  reinoud 			(uint8_t *) &udf_rw32_lbmap, 4,
   1101   1.1  reinoud 			ump->vat_offset + lb_num * 4);
   1102   1.1  reinoud 		KASSERT(error == 0);
   1103   1.1  reinoud 		ump->vat_last_free_lb = MIN(ump->vat_last_free_lb, lb_num);
   1104   1.1  reinoud 		break;
   1105   1.1  reinoud 	case UDF_VTOP_TYPE_META :
   1106   1.1  reinoud 		/* free space in the metadata bitmap */
   1107  1.17  reinoud 		bitmap = &ump->metadata_unalloc_bits;
   1108  1.17  reinoud 		KASSERT(bitmap->bits);
   1109  1.17  reinoud 
   1110  1.17  reinoud 		ump->lvclose |= UDF_WRITE_PART_BITMAPS;
   1111  1.17  reinoud 		udf_bitmap_free(bitmap, lb_num, num_lb);
   1112  1.17  reinoud 
   1113  1.17  reinoud 		/* adjust freecount */
   1114  1.17  reinoud 		lvid = ump->logvol_integrity;
   1115  1.17  reinoud 		freepos = &lvid->tables[0] + vpart_num;
   1116  1.17  reinoud 		free_lbs = udf_rw32(*freepos);
   1117  1.17  reinoud 		*freepos = udf_rw32(free_lbs + num_lb);
   1118  1.17  reinoud 		break;
   1119   1.1  reinoud 	default:
   1120   1.1  reinoud 		printf("ALERT: udf_free_allocated_space : allocation %d "
   1121   1.1  reinoud 			"not implemented yet!\n", ump->vtop_tp[vpart_num]);
   1122   1.1  reinoud 		break;
   1123   1.1  reinoud 	}
   1124   1.1  reinoud 
   1125   1.1  reinoud 	mutex_exit(&ump->allocate_mutex);
   1126   1.1  reinoud }
   1127   1.1  reinoud 
   1128   1.1  reinoud /* --------------------------------------------------------------------- */
   1129   1.1  reinoud 
   1130   1.1  reinoud int
   1131  1.17  reinoud udf_pre_allocate_space(struct udf_mount *ump, int udf_c_type,
   1132  1.17  reinoud 	uint32_t num_lb, uint16_t vpartnr, uint64_t *lmapping)
   1133   1.1  reinoud {
   1134  1.17  reinoud 	/* TODO properly maintain uncomitted_lb per partition */
   1135   1.1  reinoud 
   1136   1.1  reinoud 	/* reserve size for VAT allocated data */
   1137  1.17  reinoud 	if (ump->vtop_alloc[vpartnr] == UDF_ALLOC_VAT) {
   1138   1.1  reinoud 		mutex_enter(&ump->allocate_mutex);
   1139   1.1  reinoud 			ump->uncomitted_lb += num_lb;
   1140   1.1  reinoud 		mutex_exit(&ump->allocate_mutex);
   1141   1.1  reinoud 	}
   1142   1.1  reinoud 
   1143  1.17  reinoud 	return udf_allocate_space(ump, udf_c_type, vpartnr, num_lb, lmapping);
   1144   1.1  reinoud }
   1145   1.1  reinoud 
   1146   1.1  reinoud /* --------------------------------------------------------------------- */
   1147   1.1  reinoud 
   1148   1.1  reinoud /*
   1149   1.1  reinoud  * Allocate a buf on disc for direct write out. The space doesn't have to be
   1150   1.1  reinoud  * contiguous as the caller takes care of this.
   1151   1.1  reinoud  */
   1152   1.1  reinoud 
   1153   1.1  reinoud void
   1154   1.1  reinoud udf_late_allocate_buf(struct udf_mount *ump, struct buf *buf,
   1155  1.17  reinoud 	uint64_t *lmapping, struct long_ad *node_ad_cpy, uint16_t *vpart_nump)
   1156   1.1  reinoud {
   1157   1.1  reinoud 	struct udf_node  *udf_node = VTOI(buf->b_vp);
   1158   1.1  reinoud 	int lb_size, blks, udf_c_type;
   1159  1.17  reinoud 	int vpart_num, num_lb;
   1160   1.1  reinoud 	int error, s;
   1161   1.1  reinoud 
   1162   1.1  reinoud 	/*
   1163   1.1  reinoud 	 * for each sector in the buf, allocate a sector on disc and record
   1164   1.1  reinoud 	 * its position in the provided mapping array.
   1165   1.1  reinoud 	 *
   1166   1.1  reinoud 	 * If its userdata or FIDs, record its location in its node.
   1167   1.1  reinoud 	 */
   1168   1.1  reinoud 
   1169   1.1  reinoud 	lb_size    = udf_rw32(ump->logical_vol->lb_size);
   1170   1.1  reinoud 	num_lb     = (buf->b_bcount + lb_size -1) / lb_size;
   1171   1.1  reinoud 	blks       = lb_size / DEV_BSIZE;
   1172   1.1  reinoud 	udf_c_type = buf->b_udf_c_type;
   1173   1.1  reinoud 
   1174   1.1  reinoud 	KASSERT(lb_size == ump->discinfo.sector_size);
   1175   1.1  reinoud 
   1176  1.17  reinoud 	/* select partition to record the buffer on */
   1177  1.17  reinoud 	vpart_num = ump->data_part;
   1178  1.17  reinoud 	if (udf_c_type == UDF_C_NODE)
   1179  1.17  reinoud 		vpart_num = ump->node_part;
   1180  1.17  reinoud 	if (udf_c_type == UDF_C_FIDS)
   1181  1.17  reinoud 		vpart_num = ump->fids_part;
   1182  1.17  reinoud 	*vpart_nump = vpart_num;
   1183   1.1  reinoud 
   1184   1.1  reinoud 	if (udf_c_type == UDF_C_NODE) {
   1185   1.1  reinoud 		/* if not VAT, its allready allocated */
   1186  1.17  reinoud 		if (ump->vtop_alloc[ump->node_part] != UDF_ALLOC_VAT)
   1187   1.1  reinoud 			return;
   1188   1.1  reinoud 
   1189  1.17  reinoud 		/* allocate on its backing sequential partition */
   1190  1.17  reinoud 		vpart_num = ump->data_part;
   1191   1.1  reinoud 	}
   1192   1.1  reinoud 
   1193  1.17  reinoud 	/* do allocation on the selected partition */
   1194  1.17  reinoud 	error = udf_allocate_space(ump, udf_c_type,
   1195  1.17  reinoud 			vpart_num, num_lb, lmapping);
   1196   1.1  reinoud 	if (error) {
   1197   1.1  reinoud 		/* ARGH! we've not done our accounting right! */
   1198   1.1  reinoud 		panic("UDF disc allocation accounting gone wrong");
   1199   1.1  reinoud 	}
   1200   1.1  reinoud 
   1201   1.1  reinoud 	/* commit our sector count */
   1202   1.1  reinoud 	mutex_enter(&ump->allocate_mutex);
   1203   1.1  reinoud 		if (num_lb > ump->uncomitted_lb) {
   1204   1.1  reinoud 			ump->uncomitted_lb = 0;
   1205   1.1  reinoud 		} else {
   1206   1.1  reinoud 			ump->uncomitted_lb -= num_lb;
   1207   1.1  reinoud 		}
   1208   1.1  reinoud 	mutex_exit(&ump->allocate_mutex);
   1209   1.1  reinoud 
   1210   1.1  reinoud 	/* If its userdata or FIDs, record its allocation in its node. */
   1211  1.16  reinoud 	if ((udf_c_type == UDF_C_USERDATA) ||
   1212  1.16  reinoud 	    (udf_c_type == UDF_C_FIDS) ||
   1213  1.16  reinoud 	    (udf_c_type == UDF_C_METADATA_SBM))
   1214  1.16  reinoud 	{
   1215  1.17  reinoud 		udf_record_allocation_in_node(ump, buf, vpart_num, lmapping,
   1216   1.1  reinoud 			node_ad_cpy);
   1217   1.1  reinoud 		/* decrement our outstanding bufs counter */
   1218   1.1  reinoud 		s = splbio();
   1219   1.1  reinoud 			udf_node->outstanding_bufs--;
   1220   1.1  reinoud 		splx(s);
   1221   1.1  reinoud 	}
   1222   1.1  reinoud }
   1223   1.1  reinoud 
   1224   1.1  reinoud /* --------------------------------------------------------------------- */
   1225   1.1  reinoud 
   1226   1.1  reinoud /*
   1227   1.1  reinoud  * Try to merge a1 with the new piece a2. udf_ads_merge returns error when not
   1228   1.1  reinoud  * possible (anymore); a2 returns the rest piece.
   1229   1.1  reinoud  */
   1230   1.1  reinoud 
   1231   1.1  reinoud static int
   1232   1.1  reinoud udf_ads_merge(uint32_t lb_size, struct long_ad *a1, struct long_ad *a2)
   1233   1.1  reinoud {
   1234   1.1  reinoud 	uint32_t max_len, merge_len;
   1235   1.1  reinoud 	uint32_t a1_len, a2_len;
   1236   1.1  reinoud 	uint32_t a1_flags, a2_flags;
   1237   1.1  reinoud 	uint32_t a1_lbnum, a2_lbnum;
   1238   1.1  reinoud 	uint16_t a1_part, a2_part;
   1239   1.1  reinoud 
   1240   1.1  reinoud 	max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
   1241   1.1  reinoud 
   1242   1.1  reinoud 	a1_flags = UDF_EXT_FLAGS(udf_rw32(a1->len));
   1243   1.1  reinoud 	a1_len   = UDF_EXT_LEN(udf_rw32(a1->len));
   1244   1.1  reinoud 	a1_lbnum = udf_rw32(a1->loc.lb_num);
   1245   1.1  reinoud 	a1_part  = udf_rw16(a1->loc.part_num);
   1246   1.1  reinoud 
   1247   1.1  reinoud 	a2_flags = UDF_EXT_FLAGS(udf_rw32(a2->len));
   1248   1.1  reinoud 	a2_len   = UDF_EXT_LEN(udf_rw32(a2->len));
   1249   1.1  reinoud 	a2_lbnum = udf_rw32(a2->loc.lb_num);
   1250   1.1  reinoud 	a2_part  = udf_rw16(a2->loc.part_num);
   1251   1.1  reinoud 
   1252   1.1  reinoud 	/* defines same space */
   1253   1.1  reinoud 	if (a1_flags != a2_flags)
   1254   1.1  reinoud 		return 1;
   1255   1.1  reinoud 
   1256   1.1  reinoud 	if (a1_flags != UDF_EXT_FREE) {
   1257   1.1  reinoud 		/* the same partition */
   1258   1.1  reinoud 		if (a1_part != a2_part)
   1259   1.1  reinoud 			return 1;
   1260   1.1  reinoud 
   1261   1.1  reinoud 		/* a2 is successor of a1 */
   1262   1.1  reinoud 		if (a1_lbnum * lb_size + a1_len != a2_lbnum * lb_size)
   1263   1.1  reinoud 			return 1;
   1264   1.1  reinoud 	}
   1265   1.9  reinoud 
   1266   1.1  reinoud 	/* merge as most from a2 if possible */
   1267   1.1  reinoud 	merge_len = MIN(a2_len, max_len - a1_len);
   1268   1.1  reinoud 	a1_len   += merge_len;
   1269   1.1  reinoud 	a2_len   -= merge_len;
   1270   1.1  reinoud 	a2_lbnum += merge_len/lb_size;
   1271   1.1  reinoud 
   1272   1.1  reinoud 	a1->len = udf_rw32(a1_len | a1_flags);
   1273   1.1  reinoud 	a2->len = udf_rw32(a2_len | a2_flags);
   1274   1.1  reinoud 	a2->loc.lb_num = udf_rw32(a2_lbnum);
   1275   1.1  reinoud 
   1276   1.1  reinoud 	if (a2_len > 0)
   1277   1.1  reinoud 		return 1;
   1278   1.1  reinoud 
   1279   1.1  reinoud 	/* there is space over to merge */
   1280   1.1  reinoud 	return 0;
   1281   1.1  reinoud }
   1282   1.1  reinoud 
   1283   1.1  reinoud /* --------------------------------------------------------------------- */
   1284   1.1  reinoud 
   1285   1.1  reinoud static void
   1286   1.1  reinoud udf_wipe_adslots(struct udf_node *udf_node)
   1287   1.1  reinoud {
   1288   1.1  reinoud 	struct file_entry      *fe;
   1289   1.1  reinoud 	struct extfile_entry   *efe;
   1290   1.1  reinoud 	struct alloc_ext_entry *ext;
   1291   1.1  reinoud 	uint64_t inflen, objsize;
   1292   1.1  reinoud 	uint32_t lb_size, dscr_size, l_ea, l_ad, max_l_ad, crclen;
   1293   1.1  reinoud 	uint8_t *data_pos;
   1294   1.1  reinoud 	int extnr;
   1295   1.1  reinoud 
   1296   1.1  reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
   1297   1.1  reinoud 
   1298   1.1  reinoud 	fe  = udf_node->fe;
   1299   1.1  reinoud 	efe = udf_node->efe;
   1300   1.1  reinoud 	if (fe) {
   1301   1.1  reinoud 		inflen  = udf_rw64(fe->inf_len);
   1302   1.1  reinoud 		objsize = inflen;
   1303   1.1  reinoud 		dscr_size  = sizeof(struct file_entry) -1;
   1304   1.1  reinoud 		l_ea       = udf_rw32(fe->l_ea);
   1305   1.1  reinoud 		l_ad       = udf_rw32(fe->l_ad);
   1306   1.1  reinoud 		data_pos = (uint8_t *) fe + dscr_size + l_ea;
   1307   1.1  reinoud 	} else {
   1308   1.1  reinoud 		inflen  = udf_rw64(efe->inf_len);
   1309   1.1  reinoud 		objsize = udf_rw64(efe->obj_size);
   1310   1.1  reinoud 		dscr_size  = sizeof(struct extfile_entry) -1;
   1311   1.1  reinoud 		l_ea       = udf_rw32(efe->l_ea);
   1312   1.1  reinoud 		l_ad       = udf_rw32(efe->l_ad);
   1313   1.1  reinoud 		data_pos = (uint8_t *) efe + dscr_size + l_ea;
   1314   1.1  reinoud 	}
   1315   1.1  reinoud 	max_l_ad = lb_size - dscr_size - l_ea;
   1316   1.1  reinoud 
   1317   1.1  reinoud 	/* wipe fe/efe */
   1318   1.1  reinoud 	memset(data_pos, 0, max_l_ad);
   1319   1.1  reinoud 	crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea;
   1320   1.1  reinoud 	if (fe) {
   1321   1.1  reinoud 		fe->l_ad         = udf_rw32(0);
   1322   1.1  reinoud 		fe->logblks_rec  = udf_rw64(0);
   1323   1.1  reinoud 		fe->tag.desc_crc_len = udf_rw32(crclen);
   1324   1.1  reinoud 	} else {
   1325   1.1  reinoud 		efe->l_ad        = udf_rw32(0);
   1326   1.1  reinoud 		efe->logblks_rec = udf_rw64(0);
   1327   1.1  reinoud 		efe->tag.desc_crc_len = udf_rw32(crclen);
   1328   1.1  reinoud 	}
   1329   1.1  reinoud 
   1330   1.1  reinoud 	/* wipe all allocation extent entries */
   1331   1.1  reinoud 	for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
   1332   1.1  reinoud 		ext = udf_node->ext[extnr];
   1333   1.1  reinoud 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
   1334  1.11  reinoud 		data_pos = (uint8_t *) ext->data;
   1335   1.1  reinoud 		max_l_ad = lb_size - dscr_size;
   1336   1.1  reinoud 		memset(data_pos, 0, max_l_ad);
   1337   1.1  reinoud 		ext->l_ad = udf_rw32(0);
   1338   1.1  reinoud 
   1339   1.1  reinoud 		crclen = dscr_size - UDF_DESC_TAG_LENGTH;
   1340   1.1  reinoud 		ext->tag.desc_crc_len = udf_rw32(crclen);
   1341   1.1  reinoud 	}
   1342  1.11  reinoud 	udf_node->i_flags |= IN_NODE_REBUILD;
   1343   1.1  reinoud }
   1344   1.1  reinoud 
   1345   1.1  reinoud /* --------------------------------------------------------------------- */
   1346   1.1  reinoud 
   1347   1.1  reinoud void
   1348   1.1  reinoud udf_get_adslot(struct udf_node *udf_node, int slot, struct long_ad *icb,
   1349   1.1  reinoud 	int *eof) {
   1350   1.1  reinoud 	struct file_entry      *fe;
   1351   1.1  reinoud 	struct extfile_entry   *efe;
   1352   1.1  reinoud 	struct alloc_ext_entry *ext;
   1353   1.1  reinoud 	struct icb_tag *icbtag;
   1354   1.1  reinoud 	struct short_ad *short_ad;
   1355  1.11  reinoud 	struct long_ad *long_ad, l_icb;
   1356   1.1  reinoud 	uint32_t offset;
   1357  1.11  reinoud 	uint32_t lb_size, dscr_size, l_ea, l_ad, flags;
   1358   1.1  reinoud 	uint8_t *data_pos;
   1359   1.1  reinoud 	int icbflags, addr_type, adlen, extnr;
   1360   1.1  reinoud 
   1361   1.1  reinoud 	/* determine what descriptor we are in */
   1362   1.1  reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
   1363   1.1  reinoud 
   1364   1.1  reinoud 	fe  = udf_node->fe;
   1365   1.1  reinoud 	efe = udf_node->efe;
   1366   1.1  reinoud 	if (fe) {
   1367   1.1  reinoud 		icbtag  = &fe->icbtag;
   1368   1.1  reinoud 		dscr_size  = sizeof(struct file_entry) -1;
   1369   1.1  reinoud 		l_ea       = udf_rw32(fe->l_ea);
   1370   1.1  reinoud 		l_ad       = udf_rw32(fe->l_ad);
   1371   1.1  reinoud 		data_pos = (uint8_t *) fe + dscr_size + l_ea;
   1372   1.1  reinoud 	} else {
   1373   1.1  reinoud 		icbtag  = &efe->icbtag;
   1374   1.1  reinoud 		dscr_size  = sizeof(struct extfile_entry) -1;
   1375   1.1  reinoud 		l_ea       = udf_rw32(efe->l_ea);
   1376   1.1  reinoud 		l_ad       = udf_rw32(efe->l_ad);
   1377   1.1  reinoud 		data_pos = (uint8_t *) efe + dscr_size + l_ea;
   1378   1.1  reinoud 	}
   1379   1.1  reinoud 
   1380   1.1  reinoud 	icbflags  = udf_rw16(icbtag->flags);
   1381   1.1  reinoud 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   1382   1.1  reinoud 
   1383   1.1  reinoud 	/* just in case we're called on an intern, its EOF */
   1384   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
   1385   1.1  reinoud 		memset(icb, 0, sizeof(struct long_ad));
   1386   1.1  reinoud 		*eof = 1;
   1387   1.1  reinoud 		return;
   1388   1.1  reinoud 	}
   1389   1.1  reinoud 
   1390   1.1  reinoud 	adlen = 0;
   1391   1.1  reinoud 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1392   1.1  reinoud 		adlen = sizeof(struct short_ad);
   1393   1.1  reinoud 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
   1394   1.1  reinoud 		adlen = sizeof(struct long_ad);
   1395   1.1  reinoud 	}
   1396   1.1  reinoud 
   1397   1.1  reinoud 	/* if offset too big, we go to the allocation extensions */
   1398   1.1  reinoud 	offset = slot * adlen;
   1399   1.3  reinoud 	extnr  = -1;
   1400  1.11  reinoud 	while (offset >= l_ad) {
   1401  1.11  reinoud 		/* check if our last entry is a redirect */
   1402  1.11  reinoud 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1403  1.11  reinoud 			short_ad = (struct short_ad *) (data_pos + l_ad-adlen);
   1404  1.11  reinoud 			l_icb.len          = short_ad->len;
   1405  1.11  reinoud 			l_icb.loc.part_num = udf_node->loc.loc.part_num;
   1406  1.11  reinoud 			l_icb.loc.lb_num   = short_ad->lb_num;
   1407  1.11  reinoud 		} else {
   1408  1.11  reinoud 			KASSERT(addr_type == UDF_ICB_LONG_ALLOC);
   1409  1.11  reinoud 			long_ad = (struct long_ad *) (data_pos + l_ad-adlen);
   1410  1.11  reinoud 			l_icb = *long_ad;
   1411  1.11  reinoud 		}
   1412  1.11  reinoud 		flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len));
   1413  1.11  reinoud 		if (flags != UDF_EXT_REDIRECT) {
   1414  1.11  reinoud 			l_ad = 0;	/* force EOF */
   1415  1.11  reinoud 			break;
   1416  1.11  reinoud 		}
   1417  1.11  reinoud 
   1418  1.11  reinoud 		/* advance to next extent */
   1419   1.3  reinoud 		extnr++;
   1420  1.11  reinoud 		if (extnr >= udf_node->num_extensions) {
   1421  1.11  reinoud 			l_ad = 0;	/* force EOF */
   1422  1.11  reinoud 			break;
   1423  1.11  reinoud 		}
   1424  1.11  reinoud 		offset = offset - l_ad;
   1425   1.1  reinoud 		ext  = udf_node->ext[extnr];
   1426   1.1  reinoud 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
   1427   1.1  reinoud 		l_ad = udf_rw32(ext->l_ad);
   1428   1.3  reinoud 		data_pos = (uint8_t *) ext + dscr_size;
   1429   1.1  reinoud 	}
   1430   1.1  reinoud 
   1431  1.11  reinoud 	/* XXX l_ad == 0 should be enough to check */
   1432   1.1  reinoud 	*eof = (offset >= l_ad) || (l_ad == 0);
   1433   1.1  reinoud 	if (*eof) {
   1434  1.11  reinoud 		DPRINTF(PARANOIDADWLK, ("returning EOF, extnr %d, offset %d, "
   1435  1.11  reinoud 			"l_ad %d\n", extnr, offset, l_ad));
   1436   1.1  reinoud 		memset(icb, 0, sizeof(struct long_ad));
   1437   1.1  reinoud 		return;
   1438   1.1  reinoud 	}
   1439   1.1  reinoud 
   1440   1.1  reinoud 	/* get the element */
   1441   1.1  reinoud 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1442   1.1  reinoud 		short_ad = (struct short_ad *) (data_pos + offset);
   1443   1.1  reinoud 		icb->len          = short_ad->len;
   1444   1.5  reinoud 		icb->loc.part_num = udf_node->loc.loc.part_num;
   1445   1.1  reinoud 		icb->loc.lb_num   = short_ad->lb_num;
   1446   1.1  reinoud 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
   1447   1.1  reinoud 		long_ad = (struct long_ad *) (data_pos + offset);
   1448   1.1  reinoud 		*icb = *long_ad;
   1449   1.1  reinoud 	}
   1450  1.11  reinoud 	DPRINTF(PARANOIDADWLK, ("returning element : v %d, lb %d, len %d, "
   1451  1.11  reinoud 		"flags %d\n", icb->loc.part_num, icb->loc.lb_num,
   1452  1.11  reinoud 		UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len)));
   1453   1.1  reinoud }
   1454   1.1  reinoud 
   1455   1.1  reinoud /* --------------------------------------------------------------------- */
   1456   1.1  reinoud 
   1457   1.1  reinoud int
   1458  1.11  reinoud udf_append_adslot(struct udf_node *udf_node, int *slot, struct long_ad *icb) {
   1459  1.11  reinoud 	struct udf_mount *ump = udf_node->ump;
   1460  1.13  reinoud 	union dscrptr          *dscr, *extdscr;
   1461   1.1  reinoud 	struct file_entry      *fe;
   1462   1.1  reinoud 	struct extfile_entry   *efe;
   1463   1.1  reinoud 	struct alloc_ext_entry *ext;
   1464   1.1  reinoud 	struct icb_tag *icbtag;
   1465   1.1  reinoud 	struct short_ad *short_ad;
   1466  1.11  reinoud 	struct long_ad *long_ad, o_icb, l_icb;
   1467   1.1  reinoud 	uint64_t logblks_rec, *logblks_rec_p;
   1468  1.16  reinoud 	uint64_t lmapping;
   1469  1.11  reinoud 	uint32_t offset, rest, len, lb_num;
   1470   1.1  reinoud 	uint32_t lb_size, dscr_size, l_ea, l_ad, *l_ad_p, max_l_ad, crclen;
   1471  1.11  reinoud 	uint32_t flags;
   1472  1.11  reinoud 	uint16_t vpart_num;
   1473   1.1  reinoud 	uint8_t *data_pos;
   1474   1.1  reinoud 	int icbflags, addr_type, adlen, extnr;
   1475  1.11  reinoud 	int error;
   1476   1.1  reinoud 
   1477  1.11  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   1478  1.17  reinoud 	vpart_num = udf_rw16(udf_node->loc.loc.part_num);
   1479   1.1  reinoud 
   1480  1.17  reinoud 	/* determine what descriptor we are in */
   1481   1.1  reinoud 	fe  = udf_node->fe;
   1482   1.1  reinoud 	efe = udf_node->efe;
   1483   1.1  reinoud 	if (fe) {
   1484   1.1  reinoud 		icbtag  = &fe->icbtag;
   1485   1.1  reinoud 		dscr      = (union dscrptr *) fe;
   1486   1.1  reinoud 		dscr_size = sizeof(struct file_entry) -1;
   1487   1.1  reinoud 
   1488   1.1  reinoud 		l_ea      = udf_rw32(fe->l_ea);
   1489   1.1  reinoud 		l_ad_p    = &fe->l_ad;
   1490   1.1  reinoud 		logblks_rec_p = &fe->logblks_rec;
   1491   1.1  reinoud 	} else {
   1492   1.1  reinoud 		icbtag    = &efe->icbtag;
   1493   1.1  reinoud 		dscr      = (union dscrptr *) efe;
   1494   1.1  reinoud 		dscr_size = sizeof(struct extfile_entry) -1;
   1495   1.1  reinoud 
   1496   1.1  reinoud 		l_ea      = udf_rw32(efe->l_ea);
   1497   1.1  reinoud 		l_ad_p    = &efe->l_ad;
   1498   1.1  reinoud 		logblks_rec_p = &efe->logblks_rec;
   1499   1.1  reinoud 	}
   1500   1.1  reinoud 	data_pos  = (uint8_t *) dscr + dscr_size + l_ea;
   1501   1.1  reinoud 	max_l_ad = lb_size - dscr_size - l_ea;
   1502   1.1  reinoud 
   1503   1.1  reinoud 	icbflags  = udf_rw16(icbtag->flags);
   1504   1.1  reinoud 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   1505   1.1  reinoud 
   1506   1.1  reinoud 	/* just in case we're called on an intern, its EOF */
   1507   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
   1508   1.1  reinoud 		panic("udf_append_adslot on UDF_ICB_INTERN_ALLOC\n");
   1509   1.1  reinoud 	}
   1510   1.1  reinoud 
   1511   1.1  reinoud 	adlen = 0;
   1512   1.1  reinoud 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1513   1.1  reinoud 		adlen = sizeof(struct short_ad);
   1514   1.1  reinoud 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
   1515   1.1  reinoud 		adlen = sizeof(struct long_ad);
   1516   1.1  reinoud 	}
   1517   1.1  reinoud 
   1518  1.17  reinoud 	/* clean up given long_ad since it can be a synthesized one */
   1519  1.11  reinoud 	flags = UDF_EXT_FLAGS(udf_rw32(icb->len));
   1520  1.11  reinoud 	if (flags == UDF_EXT_FREE) {
   1521   1.9  reinoud 		icb->loc.part_num = udf_rw16(0);
   1522   1.9  reinoud 		icb->loc.lb_num   = udf_rw32(0);
   1523   1.9  reinoud 	}
   1524   1.9  reinoud 
   1525   1.1  reinoud 	/* if offset too big, we go to the allocation extensions */
   1526  1.11  reinoud 	l_ad   = udf_rw32(*l_ad_p);
   1527  1.11  reinoud 	offset = (*slot) * adlen;
   1528  1.11  reinoud 	extnr  = -1;
   1529  1.11  reinoud 	while (offset >= l_ad) {
   1530  1.11  reinoud 		/* check if our last entry is a redirect */
   1531  1.11  reinoud 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1532  1.11  reinoud 			short_ad = (struct short_ad *) (data_pos + l_ad-adlen);
   1533  1.11  reinoud 			l_icb.len          = short_ad->len;
   1534  1.11  reinoud 			l_icb.loc.part_num = udf_node->loc.loc.part_num;
   1535  1.11  reinoud 			l_icb.loc.lb_num   = short_ad->lb_num;
   1536  1.11  reinoud 		} else {
   1537  1.11  reinoud 			KASSERT(addr_type == UDF_ICB_LONG_ALLOC);
   1538  1.11  reinoud 			long_ad = (struct long_ad *) (data_pos + l_ad-adlen);
   1539  1.11  reinoud 			l_icb = *long_ad;
   1540  1.11  reinoud 		}
   1541  1.11  reinoud 		flags = UDF_EXT_FLAGS(udf_rw32(l_icb.len));
   1542  1.11  reinoud 		if (flags != UDF_EXT_REDIRECT) {
   1543  1.11  reinoud 			/* only one past the last one is adressable */
   1544  1.11  reinoud 			break;
   1545  1.11  reinoud 		}
   1546  1.11  reinoud 
   1547  1.11  reinoud 		/* advance to next extent */
   1548  1.11  reinoud 		extnr++;
   1549  1.11  reinoud 		KASSERT(extnr < udf_node->num_extensions);
   1550  1.11  reinoud 		offset = offset - l_ad;
   1551  1.11  reinoud 
   1552   1.1  reinoud 		ext  = udf_node->ext[extnr];
   1553   1.1  reinoud 		dscr = (union dscrptr *) ext;
   1554   1.1  reinoud 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
   1555  1.11  reinoud 		max_l_ad = lb_size - dscr_size;
   1556   1.1  reinoud 		l_ad_p = &ext->l_ad;
   1557  1.11  reinoud 		l_ad   = udf_rw32(*l_ad_p);
   1558  1.11  reinoud 		data_pos = (uint8_t *) ext + dscr_size;
   1559  1.11  reinoud 	}
   1560  1.11  reinoud 	DPRINTF(PARANOIDADWLK, ("append, ext %d, offset %d, l_ad %d\n",
   1561  1.11  reinoud 		extnr, offset, udf_rw32(*l_ad_p)));
   1562  1.11  reinoud 	KASSERT(l_ad == udf_rw32(*l_ad_p));
   1563   1.1  reinoud 
   1564   1.1  reinoud 	/* offset is offset within the current (E)FE/AED */
   1565   1.1  reinoud 	l_ad   = udf_rw32(*l_ad_p);
   1566   1.1  reinoud 	crclen = udf_rw32(dscr->tag.desc_crc_len);
   1567   1.1  reinoud 	logblks_rec = udf_rw64(*logblks_rec_p);
   1568   1.1  reinoud 
   1569   1.1  reinoud 	/* overwriting old piece? */
   1570   1.1  reinoud 	if (offset < l_ad) {
   1571   1.1  reinoud 		/* overwrite entry; compensate for the old element */
   1572   1.1  reinoud 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1573   1.1  reinoud 			short_ad = (struct short_ad *) (data_pos + offset);
   1574   1.1  reinoud 			o_icb.len          = short_ad->len;
   1575   1.1  reinoud 			o_icb.loc.part_num = udf_rw16(0);	/* ignore */
   1576   1.1  reinoud 			o_icb.loc.lb_num   = short_ad->lb_num;
   1577   1.1  reinoud 		} else if (addr_type == UDF_ICB_LONG_ALLOC) {
   1578   1.1  reinoud 			long_ad = (struct long_ad *) (data_pos + offset);
   1579   1.1  reinoud 			o_icb = *long_ad;
   1580   1.1  reinoud 		} else {
   1581   1.1  reinoud 			panic("Invalid address type in udf_append_adslot\n");
   1582   1.1  reinoud 		}
   1583   1.1  reinoud 
   1584   1.1  reinoud 		len = udf_rw32(o_icb.len);
   1585   1.1  reinoud 		if (UDF_EXT_FLAGS(len) == UDF_EXT_ALLOCATED) {
   1586   1.1  reinoud 			/* adjust counts */
   1587   1.1  reinoud 			len = UDF_EXT_LEN(len);
   1588   1.1  reinoud 			logblks_rec -= (len + lb_size -1) / lb_size;
   1589   1.1  reinoud 		}
   1590   1.1  reinoud 	}
   1591   1.1  reinoud 
   1592  1.11  reinoud 	/* check if we're not appending a redirection */
   1593  1.11  reinoud 	flags = UDF_EXT_FLAGS(udf_rw32(icb->len));
   1594  1.11  reinoud 	KASSERT(flags != UDF_EXT_REDIRECT);
   1595  1.11  reinoud 
   1596  1.11  reinoud 	/* round down available space */
   1597  1.11  reinoud 	rest = adlen * ((max_l_ad - offset) / adlen);
   1598   1.1  reinoud 	if (rest <= adlen) {
   1599  1.11  reinoud 		/* have to append aed, see if we already have a spare one */
   1600  1.11  reinoud 		extnr++;
   1601  1.11  reinoud 		ext = udf_node->ext[extnr];
   1602  1.11  reinoud 		l_icb = udf_node->ext_loc[extnr];
   1603  1.11  reinoud 		if (ext == NULL) {
   1604  1.11  reinoud 			DPRINTF(ALLOC,("adding allocation extent %d\n", extnr));
   1605  1.17  reinoud 
   1606  1.11  reinoud 			error = udf_pre_allocate_space(ump, UDF_C_NODE, 1,
   1607  1.17  reinoud 					vpart_num, &lmapping);
   1608  1.11  reinoud 			lb_num = lmapping;
   1609  1.11  reinoud 			if (error)
   1610  1.11  reinoud 				return error;
   1611  1.11  reinoud 
   1612  1.11  reinoud 			/* initialise pointer to location */
   1613  1.11  reinoud 			memset(&l_icb, 0, sizeof(struct long_ad));
   1614  1.11  reinoud 			l_icb.len = udf_rw32(lb_size | UDF_EXT_REDIRECT);
   1615  1.11  reinoud 			l_icb.loc.lb_num   = udf_rw32(lb_num);
   1616  1.11  reinoud 			l_icb.loc.part_num = udf_rw16(vpart_num);
   1617  1.11  reinoud 
   1618  1.11  reinoud 			/* create new aed descriptor */
   1619  1.13  reinoud 			udf_create_logvol_dscr(ump, udf_node, &l_icb, &extdscr);
   1620  1.13  reinoud 			ext = &extdscr->aee;
   1621  1.11  reinoud 
   1622  1.11  reinoud 			udf_inittag(ump, &ext->tag, TAGID_ALLOCEXTENT, lb_num);
   1623  1.11  reinoud 			dscr_size  = sizeof(struct alloc_ext_entry) -1;
   1624  1.11  reinoud 			max_l_ad = lb_size - dscr_size;
   1625  1.11  reinoud 			memset(ext->data, 0, max_l_ad);
   1626  1.11  reinoud 			ext->l_ad = udf_rw32(0);
   1627  1.11  reinoud 			ext->tag.desc_crc_len =
   1628  1.11  reinoud 				udf_rw32(dscr_size - UDF_DESC_TAG_LENGTH);
   1629  1.11  reinoud 
   1630  1.11  reinoud 			/* declare aed */
   1631  1.11  reinoud 			udf_node->num_extensions++;
   1632  1.11  reinoud 			udf_node->ext_loc[extnr] = l_icb;
   1633  1.11  reinoud 			udf_node->ext[extnr] = ext;
   1634  1.11  reinoud 		}
   1635  1.11  reinoud 		/* add redirect and adjust l_ad and crclen for old descr */
   1636  1.11  reinoud 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1637  1.11  reinoud 			short_ad = (struct short_ad *) (data_pos + offset);
   1638  1.11  reinoud 			short_ad->len    = l_icb.len;
   1639  1.11  reinoud 			short_ad->lb_num = l_icb.loc.lb_num;
   1640  1.11  reinoud 		} else if (addr_type == UDF_ICB_LONG_ALLOC) {
   1641  1.11  reinoud 			long_ad = (struct long_ad *) (data_pos + offset);
   1642  1.11  reinoud 			*long_ad = l_icb;
   1643  1.11  reinoud 		}
   1644  1.11  reinoud 		l_ad   += adlen;
   1645  1.11  reinoud 		crclen += adlen;
   1646  1.11  reinoud 		dscr->tag.desc_crc_len = udf_rw32(crclen);
   1647  1.11  reinoud 		*l_ad_p = udf_rw32(l_ad);
   1648  1.11  reinoud 
   1649  1.11  reinoud 		/* advance to the new extension */
   1650  1.11  reinoud 		KASSERT(ext != NULL);
   1651  1.11  reinoud 		dscr = (union dscrptr *) ext;
   1652  1.11  reinoud 		dscr_size  = sizeof(struct alloc_ext_entry) -1;
   1653  1.11  reinoud 		max_l_ad = lb_size - dscr_size;
   1654  1.11  reinoud 		data_pos = (uint8_t *) dscr + dscr_size;
   1655  1.11  reinoud 
   1656  1.11  reinoud 		l_ad_p = &ext->l_ad;
   1657  1.11  reinoud 		l_ad   = udf_rw32(*l_ad_p);
   1658  1.11  reinoud 		crclen = udf_rw32(dscr->tag.desc_crc_len);
   1659  1.11  reinoud 		offset = 0;
   1660  1.11  reinoud 
   1661  1.11  reinoud 		/* adjust callees slot count for link insert */
   1662  1.11  reinoud 		*slot += 1;
   1663   1.1  reinoud 	}
   1664   1.1  reinoud 
   1665   1.1  reinoud 	/* write out the element */
   1666  1.11  reinoud 	DPRINTF(PARANOIDADWLK, ("adding element : %p : v %d, lb %d, "
   1667  1.11  reinoud 			"len %d, flags %d\n", data_pos + offset,
   1668  1.11  reinoud 			icb->loc.part_num, icb->loc.lb_num,
   1669  1.11  reinoud 			UDF_EXT_LEN(icb->len), UDF_EXT_FLAGS(icb->len)));
   1670   1.1  reinoud 	if (addr_type == UDF_ICB_SHORT_ALLOC) {
   1671   1.1  reinoud 		short_ad = (struct short_ad *) (data_pos + offset);
   1672   1.1  reinoud 		short_ad->len    = icb->len;
   1673   1.1  reinoud 		short_ad->lb_num = icb->loc.lb_num;
   1674   1.1  reinoud 	} else if (addr_type == UDF_ICB_LONG_ALLOC) {
   1675   1.1  reinoud 		long_ad = (struct long_ad *) (data_pos + offset);
   1676   1.1  reinoud 		*long_ad = *icb;
   1677   1.1  reinoud 	}
   1678   1.1  reinoud 
   1679   1.1  reinoud 	/* adjust logblks recorded count */
   1680  1.11  reinoud 	flags = UDF_EXT_FLAGS(udf_rw32(icb->len));
   1681  1.11  reinoud 	if (flags == UDF_EXT_ALLOCATED)
   1682   1.1  reinoud 		logblks_rec += (UDF_EXT_LEN(icb->len) + lb_size -1) / lb_size;
   1683   1.1  reinoud 	*logblks_rec_p = udf_rw64(logblks_rec);
   1684   1.1  reinoud 
   1685   1.1  reinoud 	/* adjust l_ad and crclen when needed */
   1686   1.1  reinoud 	if (offset >= l_ad) {
   1687   1.1  reinoud 		l_ad   += adlen;
   1688   1.1  reinoud 		crclen += adlen;
   1689   1.1  reinoud 		dscr->tag.desc_crc_len = udf_rw32(crclen);
   1690   1.1  reinoud 		*l_ad_p = udf_rw32(l_ad);
   1691   1.1  reinoud 	}
   1692   1.1  reinoud 
   1693   1.1  reinoud 	return 0;
   1694   1.1  reinoud }
   1695   1.1  reinoud 
   1696   1.1  reinoud /* --------------------------------------------------------------------- */
   1697   1.1  reinoud 
   1698  1.11  reinoud static void
   1699  1.11  reinoud udf_count_alloc_exts(struct udf_node *udf_node)
   1700  1.11  reinoud {
   1701  1.11  reinoud 	struct long_ad s_ad;
   1702  1.11  reinoud 	uint32_t lb_num, len, flags;
   1703  1.11  reinoud 	uint16_t vpart_num;
   1704  1.11  reinoud 	int slot, eof;
   1705  1.11  reinoud 	int num_extents, extnr;
   1706  1.11  reinoud 	int lb_size;
   1707  1.11  reinoud 
   1708  1.11  reinoud 	if (udf_node->num_extensions == 0)
   1709  1.11  reinoud 		return;
   1710  1.11  reinoud 
   1711  1.11  reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
   1712  1.11  reinoud 	/* count number of allocation extents in use */
   1713  1.11  reinoud 	num_extents = 0;
   1714  1.11  reinoud 	slot = 0;
   1715  1.11  reinoud 	for (;;) {
   1716  1.11  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
   1717  1.11  reinoud 		if (eof)
   1718  1.11  reinoud 			break;
   1719  1.11  reinoud 		len   = udf_rw32(s_ad.len);
   1720  1.11  reinoud 		flags = UDF_EXT_FLAGS(len);
   1721  1.11  reinoud 
   1722  1.11  reinoud 		if (flags == UDF_EXT_REDIRECT)
   1723  1.11  reinoud 			num_extents++;
   1724  1.11  reinoud 
   1725  1.11  reinoud 		slot++;
   1726  1.11  reinoud 	}
   1727  1.11  reinoud 
   1728  1.11  reinoud 	DPRINTF(ALLOC, ("udf_count_alloc_ext counted %d live extents\n",
   1729  1.11  reinoud 		num_extents));
   1730  1.11  reinoud 
   1731  1.11  reinoud 	/* XXX choice: we could delay freeing them on node writeout */
   1732  1.11  reinoud 	/* free excess entries */
   1733  1.11  reinoud 	extnr = num_extents;
   1734  1.11  reinoud 	for (;extnr < udf_node->num_extensions; extnr++) {
   1735  1.11  reinoud 		DPRINTF(ALLOC, ("freeing alloc ext %d\n", extnr));
   1736  1.11  reinoud 		/* free dscriptor */
   1737  1.11  reinoud 		s_ad = udf_node->ext_loc[extnr];
   1738  1.11  reinoud 		udf_free_logvol_dscr(udf_node->ump, &s_ad,
   1739  1.11  reinoud 			udf_node->ext[extnr]);
   1740  1.11  reinoud 		udf_node->ext[extnr] = NULL;
   1741  1.11  reinoud 
   1742  1.11  reinoud 		/* free disc space */
   1743  1.11  reinoud 		lb_num    = udf_rw32(s_ad.loc.lb_num);
   1744  1.11  reinoud 		vpart_num = udf_rw16(s_ad.loc.part_num);
   1745  1.11  reinoud 		udf_free_allocated_space(udf_node->ump, lb_num, vpart_num, 1);
   1746  1.11  reinoud 
   1747  1.11  reinoud 		memset(&udf_node->ext_loc[extnr], 0, sizeof(struct long_ad));
   1748  1.11  reinoud 	}
   1749  1.11  reinoud 
   1750  1.11  reinoud 	/* set our new number of allocation extents */
   1751  1.11  reinoud 	udf_node->num_extensions = num_extents;
   1752  1.11  reinoud }
   1753  1.11  reinoud 
   1754  1.11  reinoud 
   1755  1.11  reinoud /* --------------------------------------------------------------------- */
   1756  1.11  reinoud 
   1757   1.1  reinoud /*
   1758   1.1  reinoud  * Adjust the node's allocation descriptors to reflect the new mapping; do
   1759   1.1  reinoud  * take note that we might glue to existing allocation descriptors.
   1760   1.1  reinoud  *
   1761   1.1  reinoud  * XXX Note there can only be one allocation being recorded/mount; maybe
   1762   1.1  reinoud  * explicit allocation in shedule thread?
   1763   1.1  reinoud  */
   1764   1.1  reinoud 
   1765   1.1  reinoud static void
   1766   1.1  reinoud udf_record_allocation_in_node(struct udf_mount *ump, struct buf *buf,
   1767   1.1  reinoud 	uint16_t vpart_num, uint64_t *mapping, struct long_ad *node_ad_cpy)
   1768   1.1  reinoud {
   1769   1.1  reinoud 	struct vnode    *vp = buf->b_vp;
   1770   1.1  reinoud 	struct udf_node *udf_node = VTOI(vp);
   1771   1.1  reinoud 	struct file_entry      *fe;
   1772   1.1  reinoud 	struct extfile_entry   *efe;
   1773   1.1  reinoud 	struct icb_tag  *icbtag;
   1774   1.1  reinoud 	struct long_ad   s_ad, c_ad;
   1775   1.1  reinoud 	uint64_t inflen, from, till;
   1776   1.1  reinoud 	uint64_t foffset, end_foffset, restart_foffset;
   1777   1.1  reinoud 	uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
   1778   1.1  reinoud 	uint32_t num_lb, len, flags, lb_num;
   1779   1.1  reinoud 	uint32_t run_start;
   1780   1.9  reinoud 	uint32_t slot_offset, replace_len, replace;
   1781   1.1  reinoud 	int addr_type, icbflags;
   1782  1.16  reinoud //	int udf_c_type = buf->b_udf_c_type;
   1783   1.1  reinoud 	int lb_size, run_length, eof;
   1784   1.1  reinoud 	int slot, cpy_slot, cpy_slots, restart_slot;
   1785   1.1  reinoud 	int error;
   1786   1.1  reinoud 
   1787   1.1  reinoud 	DPRINTF(ALLOC, ("udf_record_allocation_in_node\n"));
   1788   1.1  reinoud 
   1789  1.16  reinoud #if 0
   1790  1.16  reinoud 	/* XXX disable sanity check for now */
   1791   1.1  reinoud 	/* sanity check ... should be panic ? */
   1792   1.1  reinoud 	if ((udf_c_type != UDF_C_USERDATA) && (udf_c_type != UDF_C_FIDS))
   1793   1.1  reinoud 		return;
   1794  1.16  reinoud #endif
   1795   1.1  reinoud 
   1796   1.1  reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
   1797   1.1  reinoud 
   1798   1.1  reinoud 	/* do the job */
   1799   1.1  reinoud 	UDF_LOCK_NODE(udf_node, 0);	/* XXX can deadlock ? */
   1800   1.9  reinoud 	udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
   1801   1.1  reinoud 
   1802   1.1  reinoud 	fe  = udf_node->fe;
   1803   1.1  reinoud 	efe = udf_node->efe;
   1804   1.1  reinoud 	if (fe) {
   1805   1.1  reinoud 		icbtag = &fe->icbtag;
   1806   1.1  reinoud 		inflen = udf_rw64(fe->inf_len);
   1807   1.1  reinoud 	} else {
   1808   1.1  reinoud 		icbtag = &efe->icbtag;
   1809   1.1  reinoud 		inflen = udf_rw64(efe->inf_len);
   1810   1.1  reinoud 	}
   1811   1.1  reinoud 
   1812   1.1  reinoud 	/* do check if `till' is not past file information length */
   1813   1.1  reinoud 	from = buf->b_lblkno * lb_size;
   1814   1.1  reinoud 	till = MIN(inflen, from + buf->b_resid);
   1815   1.1  reinoud 
   1816   1.1  reinoud 	num_lb = (till - from + lb_size -1) / lb_size;
   1817   1.1  reinoud 
   1818   1.9  reinoud 	DPRINTF(ALLOC, ("record allocation from %"PRIu64" + %d\n", from, buf->b_bcount));
   1819   1.1  reinoud 
   1820   1.1  reinoud 	icbflags  = udf_rw16(icbtag->flags);
   1821   1.1  reinoud 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   1822   1.1  reinoud 
   1823   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
   1824   1.1  reinoud 		/* nothing to do */
   1825   1.1  reinoud 		/* XXX clean up rest of node? just in case? */
   1826   1.1  reinoud 		UDF_UNLOCK_NODE(udf_node, 0);
   1827   1.1  reinoud 		return;
   1828   1.1  reinoud 	}
   1829   1.1  reinoud 
   1830   1.1  reinoud 	slot     = 0;
   1831   1.1  reinoud 	cpy_slot = 0;
   1832   1.1  reinoud 	foffset  = 0;
   1833   1.1  reinoud 
   1834   1.1  reinoud 	/* 1) copy till first overlap piece to the rewrite buffer */
   1835   1.1  reinoud 	for (;;) {
   1836   1.1  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
   1837   1.1  reinoud 		if (eof) {
   1838   1.1  reinoud 			DPRINTF(WRITE,
   1839   1.1  reinoud 				("Record allocation in node "
   1840   1.1  reinoud 				 "failed: encountered EOF\n"));
   1841   1.1  reinoud 			UDF_UNLOCK_NODE(udf_node, 0);
   1842   1.1  reinoud 			buf->b_error = EINVAL;
   1843   1.1  reinoud 			return;
   1844   1.1  reinoud 		}
   1845   1.1  reinoud 		len   = udf_rw32(s_ad.len);
   1846   1.1  reinoud 		flags = UDF_EXT_FLAGS(len);
   1847   1.1  reinoud 		len   = UDF_EXT_LEN(len);
   1848   1.1  reinoud 
   1849   1.1  reinoud 		if (flags == UDF_EXT_REDIRECT) {
   1850   1.1  reinoud 			slot++;
   1851   1.1  reinoud 			continue;
   1852   1.1  reinoud 		}
   1853   1.1  reinoud 
   1854   1.1  reinoud 		end_foffset = foffset + len;
   1855   1.1  reinoud 		if (end_foffset > from)
   1856   1.1  reinoud 			break;	/* found */
   1857   1.1  reinoud 
   1858   1.1  reinoud 		node_ad_cpy[cpy_slot++] = s_ad;
   1859   1.1  reinoud 
   1860   1.1  reinoud 		DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
   1861   1.1  reinoud 			"-> stack\n",
   1862   1.1  reinoud 			udf_rw16(s_ad.loc.part_num),
   1863   1.1  reinoud 			udf_rw32(s_ad.loc.lb_num),
   1864   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
   1865   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   1866   1.1  reinoud 
   1867   1.1  reinoud 		foffset = end_foffset;
   1868   1.1  reinoud 		slot++;
   1869   1.1  reinoud 	}
   1870   1.1  reinoud 	restart_slot    = slot;
   1871   1.1  reinoud 	restart_foffset = foffset;
   1872   1.1  reinoud 
   1873   1.1  reinoud 	/* 2) trunc overlapping slot at overlap and copy it */
   1874   1.1  reinoud 	slot_offset = from - foffset;
   1875   1.1  reinoud 	if (slot_offset > 0) {
   1876   1.1  reinoud 		DPRINTF(ALLOC, ("\tslot_offset = %d, flags = %d (%d)\n",
   1877   1.1  reinoud 				slot_offset, flags >> 30, flags));
   1878   1.1  reinoud 
   1879   1.1  reinoud 		s_ad.len = udf_rw32(slot_offset | flags);
   1880   1.1  reinoud 		node_ad_cpy[cpy_slot++] = s_ad;
   1881   1.1  reinoud 
   1882   1.1  reinoud 		DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
   1883   1.1  reinoud 			"-> stack\n",
   1884   1.1  reinoud 			udf_rw16(s_ad.loc.part_num),
   1885   1.1  reinoud 			udf_rw32(s_ad.loc.lb_num),
   1886   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
   1887   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   1888   1.1  reinoud 	}
   1889   1.1  reinoud 	foffset += slot_offset;
   1890   1.1  reinoud 
   1891   1.1  reinoud 	/* 3) insert new mappings */
   1892   1.1  reinoud 	memset(&s_ad, 0, sizeof(struct long_ad));
   1893   1.1  reinoud 	lb_num = 0;
   1894   1.1  reinoud 	for (lb_num = 0; lb_num < num_lb; lb_num++) {
   1895   1.1  reinoud 		run_start  = mapping[lb_num];
   1896   1.1  reinoud 		run_length = 1;
   1897   1.1  reinoud 		while (lb_num < num_lb-1) {
   1898   1.1  reinoud 			if (mapping[lb_num+1] != mapping[lb_num]+1)
   1899   1.1  reinoud 				if (mapping[lb_num+1] != mapping[lb_num])
   1900   1.1  reinoud 					break;
   1901   1.1  reinoud 			run_length++;
   1902   1.1  reinoud 			lb_num++;
   1903   1.1  reinoud 		}
   1904   1.1  reinoud 		/* insert slot for this mapping */
   1905   1.1  reinoud 		len = run_length * lb_size;
   1906   1.1  reinoud 
   1907   1.1  reinoud 		/* bounds checking */
   1908   1.1  reinoud 		if (foffset + len > till)
   1909   1.1  reinoud 			len = till - foffset;
   1910   1.1  reinoud 		KASSERT(foffset + len <= inflen);
   1911   1.1  reinoud 
   1912   1.1  reinoud 		s_ad.len = udf_rw32(len | UDF_EXT_ALLOCATED);
   1913   1.1  reinoud 		s_ad.loc.part_num = udf_rw16(vpart_num);
   1914   1.1  reinoud 		s_ad.loc.lb_num   = udf_rw32(run_start);
   1915   1.1  reinoud 
   1916   1.1  reinoud 		foffset += len;
   1917   1.1  reinoud 
   1918   1.1  reinoud 		/* paranoia */
   1919   1.1  reinoud 		if (len == 0) {
   1920   1.1  reinoud 			DPRINTF(WRITE,
   1921   1.1  reinoud 				("Record allocation in node "
   1922   1.1  reinoud 				 "failed: insert failed\n"));
   1923   1.1  reinoud 			UDF_UNLOCK_NODE(udf_node, 0);
   1924   1.1  reinoud 			buf->b_error = EINVAL;
   1925   1.1  reinoud 			return;
   1926   1.1  reinoud 		}
   1927   1.1  reinoud 		node_ad_cpy[cpy_slot++] = s_ad;
   1928   1.1  reinoud 
   1929   1.1  reinoud 		DPRINTF(ALLOC, ("\t3: insert new mapping vp %d lb %d, len %d, "
   1930   1.1  reinoud 				"flags %d -> stack\n",
   1931   1.1  reinoud 			udf_rw16(s_ad.loc.part_num), udf_rw32(s_ad.loc.lb_num),
   1932   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
   1933   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   1934   1.1  reinoud 	}
   1935   1.1  reinoud 
   1936   1.1  reinoud 	/* 4) pop replaced length */
   1937   1.9  reinoud 	slot    = restart_slot;
   1938   1.1  reinoud 	foffset = restart_foffset;
   1939   1.1  reinoud 
   1940   1.9  reinoud 	replace_len = till - foffset;	/* total amount of bytes to pop */
   1941   1.8  reinoud 	slot_offset = from - foffset;	/* offset in first encounted slot */
   1942   1.9  reinoud 	KASSERT((slot_offset % lb_size) == 0);
   1943   1.9  reinoud 
   1944   1.1  reinoud 	for (;;) {
   1945   1.1  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
   1946   1.1  reinoud 		if (eof)
   1947   1.1  reinoud 			break;
   1948   1.1  reinoud 
   1949   1.1  reinoud 		len    = udf_rw32(s_ad.len);
   1950   1.1  reinoud 		flags  = UDF_EXT_FLAGS(len);
   1951   1.1  reinoud 		len    = UDF_EXT_LEN(len);
   1952   1.1  reinoud 		lb_num = udf_rw32(s_ad.loc.lb_num);
   1953   1.1  reinoud 
   1954   1.1  reinoud 		if (flags == UDF_EXT_REDIRECT) {
   1955   1.1  reinoud 			slot++;
   1956   1.1  reinoud 			continue;
   1957   1.1  reinoud 		}
   1958   1.1  reinoud 
   1959   1.8  reinoud 		DPRINTF(ALLOC, ("\t4i: got slot %d, slot_offset %d, "
   1960   1.9  reinoud 				"replace_len %d, "
   1961   1.8  reinoud 				"vp %d, lb %d, len %d, flags %d\n",
   1962   1.9  reinoud 			slot, slot_offset, replace_len,
   1963   1.8  reinoud 			udf_rw16(s_ad.loc.part_num),
   1964   1.1  reinoud 			udf_rw32(s_ad.loc.lb_num),
   1965   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
   1966   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   1967   1.1  reinoud 
   1968   1.9  reinoud 		/* adjust for slot offset */
   1969   1.9  reinoud 		if (slot_offset) {
   1970   1.9  reinoud 			DPRINTF(ALLOC, ("\t4s: skipping %d\n", slot_offset));
   1971   1.9  reinoud 			lb_num += slot_offset / lb_size;
   1972   1.9  reinoud 			len    -= slot_offset;
   1973   1.9  reinoud 			foffset += slot_offset;
   1974   1.9  reinoud 			replace_len -= slot_offset;
   1975   1.9  reinoud 
   1976   1.9  reinoud 			/* mark adjusted */
   1977   1.9  reinoud 			slot_offset = 0;
   1978   1.9  reinoud 		}
   1979   1.9  reinoud 
   1980   1.9  reinoud 		/* advance for (the rest of) this slot */
   1981   1.9  reinoud 		replace = MIN(len, replace_len);
   1982   1.9  reinoud 		DPRINTF(ALLOC, ("\t4d: replacing %d\n", replace));
   1983   1.9  reinoud 
   1984   1.9  reinoud 		/* advance for this slot */
   1985   1.9  reinoud 		if (replace) {
   1986  1.10  reinoud 			/* note: dont round DOWN on num_lb since we then
   1987  1.10  reinoud 			 * forget the last partial one */
   1988   1.9  reinoud 			num_lb = (replace + lb_size - 1) / lb_size;
   1989   1.9  reinoud 			if (flags != UDF_EXT_FREE) {
   1990   1.9  reinoud 				udf_free_allocated_space(ump, lb_num,
   1991   1.9  reinoud 					udf_rw16(s_ad.loc.part_num), num_lb);
   1992   1.9  reinoud 			}
   1993   1.9  reinoud 			lb_num      += num_lb;
   1994   1.9  reinoud 			len         -= replace;
   1995   1.9  reinoud 			foffset     += replace;
   1996   1.9  reinoud 			replace_len -= replace;
   1997   1.9  reinoud 		}
   1998   1.1  reinoud 
   1999   1.9  reinoud 		/* do we have a slot tail ? */
   2000   1.1  reinoud 		if (len) {
   2001   1.9  reinoud 			KASSERT(foffset % lb_size == 0);
   2002   1.1  reinoud 
   2003   1.1  reinoud 			/* we arrived at our point, push remainder */
   2004   1.1  reinoud 			s_ad.len        = udf_rw32(len | flags);
   2005   1.1  reinoud 			s_ad.loc.lb_num = udf_rw32(lb_num);
   2006   1.9  reinoud 			if (flags == UDF_EXT_FREE)
   2007   1.9  reinoud 				s_ad.loc.lb_num = udf_rw32(0);
   2008   1.1  reinoud 			node_ad_cpy[cpy_slot++] = s_ad;
   2009   1.1  reinoud 			foffset += len;
   2010   1.1  reinoud 			slot++;
   2011   1.1  reinoud 
   2012   1.1  reinoud 			DPRINTF(ALLOC, ("\t4: vp %d, lb %d, len %d, flags %d "
   2013   1.1  reinoud 				"-> stack\n",
   2014   1.1  reinoud 				udf_rw16(s_ad.loc.part_num),
   2015   1.1  reinoud 				udf_rw32(s_ad.loc.lb_num),
   2016   1.1  reinoud 				UDF_EXT_LEN(udf_rw32(s_ad.len)),
   2017   1.1  reinoud 				UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   2018   1.1  reinoud 			break;
   2019   1.1  reinoud 		}
   2020   1.9  reinoud 
   2021   1.1  reinoud 		slot++;
   2022   1.1  reinoud 	}
   2023   1.1  reinoud 
   2024   1.1  reinoud 	/* 5) copy remainder */
   2025   1.1  reinoud 	for (;;) {
   2026   1.1  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
   2027   1.1  reinoud 		if (eof)
   2028   1.1  reinoud 			break;
   2029   1.1  reinoud 
   2030   1.1  reinoud 		len   = udf_rw32(s_ad.len);
   2031   1.1  reinoud 		flags = UDF_EXT_FLAGS(len);
   2032   1.1  reinoud 		len   = UDF_EXT_LEN(len);
   2033   1.1  reinoud 
   2034   1.1  reinoud 		if (flags == UDF_EXT_REDIRECT) {
   2035   1.1  reinoud 			slot++;
   2036   1.1  reinoud 			continue;
   2037   1.1  reinoud 		}
   2038   1.1  reinoud 
   2039   1.1  reinoud 		node_ad_cpy[cpy_slot++] = s_ad;
   2040   1.1  reinoud 
   2041   1.1  reinoud 		DPRINTF(ALLOC, ("\t5: insert new mapping "
   2042   1.1  reinoud 			"vp %d lb %d, len %d, flags %d "
   2043   1.1  reinoud 			"-> stack\n",
   2044   1.1  reinoud 		udf_rw16(s_ad.loc.part_num),
   2045   1.1  reinoud 		udf_rw32(s_ad.loc.lb_num),
   2046   1.1  reinoud 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
   2047   1.1  reinoud 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   2048   1.1  reinoud 
   2049   1.1  reinoud 		slot++;
   2050   1.1  reinoud 	}
   2051   1.1  reinoud 
   2052   1.1  reinoud 	/* 6) reset node descriptors */
   2053   1.1  reinoud 	udf_wipe_adslots(udf_node);
   2054   1.1  reinoud 
   2055   1.1  reinoud 	/* 7) copy back extents; merge when possible. Recounting on the fly */
   2056   1.1  reinoud 	cpy_slots = cpy_slot;
   2057   1.1  reinoud 
   2058   1.1  reinoud 	c_ad = node_ad_cpy[0];
   2059   1.1  reinoud 	slot = 0;
   2060   1.1  reinoud 	DPRINTF(ALLOC, ("\t7s: stack -> got mapping vp %d "
   2061   1.1  reinoud 		"lb %d, len %d, flags %d\n",
   2062   1.1  reinoud 	udf_rw16(c_ad.loc.part_num),
   2063   1.1  reinoud 	udf_rw32(c_ad.loc.lb_num),
   2064   1.1  reinoud 	UDF_EXT_LEN(udf_rw32(c_ad.len)),
   2065   1.1  reinoud 	UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
   2066   1.1  reinoud 
   2067   1.1  reinoud 	for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
   2068   1.1  reinoud 		s_ad = node_ad_cpy[cpy_slot];
   2069   1.1  reinoud 
   2070   1.1  reinoud 		DPRINTF(ALLOC, ("\t7i: stack -> got mapping vp %d "
   2071   1.1  reinoud 			"lb %d, len %d, flags %d\n",
   2072   1.1  reinoud 		udf_rw16(s_ad.loc.part_num),
   2073   1.1  reinoud 		udf_rw32(s_ad.loc.lb_num),
   2074   1.1  reinoud 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
   2075   1.1  reinoud 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   2076   1.1  reinoud 
   2077   1.1  reinoud 		/* see if we can merge */
   2078   1.1  reinoud 		if (udf_ads_merge(lb_size, &c_ad, &s_ad)) {
   2079   1.1  reinoud 			/* not mergable (anymore) */
   2080   1.1  reinoud 			DPRINTF(ALLOC, ("\t7: appending vp %d lb %d, "
   2081   1.1  reinoud 				"len %d, flags %d\n",
   2082   1.1  reinoud 			udf_rw16(c_ad.loc.part_num),
   2083   1.1  reinoud 			udf_rw32(c_ad.loc.lb_num),
   2084   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(c_ad.len)),
   2085   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
   2086   1.1  reinoud 
   2087  1.11  reinoud 			error = udf_append_adslot(udf_node, &slot, &c_ad);
   2088   1.1  reinoud 			if (error) {
   2089   1.1  reinoud 				buf->b_error = error;
   2090   1.1  reinoud 				goto out;
   2091   1.1  reinoud 			}
   2092   1.1  reinoud 			c_ad = s_ad;
   2093   1.1  reinoud 			slot++;
   2094   1.1  reinoud 		}
   2095   1.1  reinoud 	}
   2096   1.1  reinoud 
   2097   1.1  reinoud 	/* 8) push rest slot (if any) */
   2098   1.1  reinoud 	if (UDF_EXT_LEN(c_ad.len) > 0) {
   2099   1.1  reinoud 		DPRINTF(ALLOC, ("\t8: last append vp %d lb %d, "
   2100   1.1  reinoud 				"len %d, flags %d\n",
   2101   1.1  reinoud 		udf_rw16(c_ad.loc.part_num),
   2102   1.1  reinoud 		udf_rw32(c_ad.loc.lb_num),
   2103   1.1  reinoud 		UDF_EXT_LEN(udf_rw32(c_ad.len)),
   2104   1.1  reinoud 		UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
   2105   1.1  reinoud 
   2106  1.11  reinoud 		error = udf_append_adslot(udf_node, &slot, &c_ad);
   2107   1.1  reinoud 		if (error) {
   2108   1.1  reinoud 			buf->b_error = error;
   2109   1.1  reinoud 			goto out;
   2110   1.1  reinoud 		}
   2111   1.1  reinoud 	}
   2112   1.1  reinoud 
   2113   1.1  reinoud out:
   2114  1.11  reinoud 	udf_count_alloc_exts(udf_node);
   2115  1.11  reinoud 
   2116   1.1  reinoud 	/* the node's descriptors should now be sane */
   2117   1.9  reinoud 	udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
   2118   1.1  reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   2119   1.1  reinoud 
   2120   1.1  reinoud 	KASSERT(orig_inflen == new_inflen);
   2121   1.1  reinoud 	KASSERT(new_lbrec >= orig_lbrec);
   2122   1.1  reinoud 
   2123   1.1  reinoud 	return;
   2124   1.1  reinoud }
   2125   1.1  reinoud 
   2126   1.1  reinoud /* --------------------------------------------------------------------- */
   2127   1.1  reinoud 
   2128   1.1  reinoud int
   2129   1.1  reinoud udf_grow_node(struct udf_node *udf_node, uint64_t new_size)
   2130   1.1  reinoud {
   2131   1.1  reinoud 	union dscrptr *dscr;
   2132   1.1  reinoud 	struct vnode *vp = udf_node->vnode;
   2133   1.1  reinoud 	struct udf_mount *ump = udf_node->ump;
   2134   1.1  reinoud 	struct file_entry    *fe;
   2135   1.1  reinoud 	struct extfile_entry *efe;
   2136   1.1  reinoud 	struct icb_tag  *icbtag;
   2137   1.1  reinoud 	struct long_ad c_ad, s_ad;
   2138   1.1  reinoud 	uint64_t size_diff, old_size, inflen, objsize, chunk, append_len;
   2139   1.1  reinoud 	uint64_t foffset, end_foffset;
   2140   1.1  reinoud 	uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
   2141   1.1  reinoud 	uint32_t lb_size, dscr_size, crclen, lastblock_grow;
   2142  1.17  reinoud 	uint32_t icbflags, len, flags, max_len;
   2143   1.1  reinoud 	uint32_t max_l_ad, l_ad, l_ea;
   2144  1.17  reinoud 	uint16_t my_part, dst_part;
   2145   1.1  reinoud 	uint8_t *data_pos, *evacuated_data;
   2146  1.17  reinoud 	int addr_type;
   2147   1.1  reinoud 	int slot, cpy_slot;
   2148  1.14  reinoud 	int isdir, eof, error;
   2149   1.1  reinoud 
   2150   1.1  reinoud 	DPRINTF(ALLOC, ("udf_grow_node\n"));
   2151   1.9  reinoud 
   2152   1.9  reinoud 	UDF_LOCK_NODE(udf_node, 0);
   2153   1.1  reinoud 	udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
   2154   1.1  reinoud 
   2155   1.1  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   2156   1.1  reinoud 	max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
   2157   1.1  reinoud 
   2158   1.1  reinoud 	fe  = udf_node->fe;
   2159   1.1  reinoud 	efe = udf_node->efe;
   2160   1.1  reinoud 	if (fe) {
   2161   1.1  reinoud 		dscr       = (union dscrptr *) fe;
   2162   1.1  reinoud 		icbtag  = &fe->icbtag;
   2163   1.1  reinoud 		inflen  = udf_rw64(fe->inf_len);
   2164   1.1  reinoud 		objsize = inflen;
   2165   1.1  reinoud 		dscr_size  = sizeof(struct file_entry) -1;
   2166   1.1  reinoud 		l_ea       = udf_rw32(fe->l_ea);
   2167   1.1  reinoud 		l_ad       = udf_rw32(fe->l_ad);
   2168   1.1  reinoud 	} else {
   2169   1.1  reinoud 		dscr       = (union dscrptr *) efe;
   2170   1.1  reinoud 		icbtag  = &efe->icbtag;
   2171   1.1  reinoud 		inflen  = udf_rw64(efe->inf_len);
   2172   1.1  reinoud 		objsize = udf_rw64(efe->obj_size);
   2173   1.1  reinoud 		dscr_size  = sizeof(struct extfile_entry) -1;
   2174   1.1  reinoud 		l_ea       = udf_rw32(efe->l_ea);
   2175   1.1  reinoud 		l_ad       = udf_rw32(efe->l_ad);
   2176   1.1  reinoud 	}
   2177   1.1  reinoud 	data_pos  = (uint8_t *) dscr + dscr_size + l_ea;
   2178   1.1  reinoud 	max_l_ad = lb_size - dscr_size - l_ea;
   2179   1.1  reinoud 
   2180   1.1  reinoud 	icbflags   = udf_rw16(icbtag->flags);
   2181   1.1  reinoud 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   2182   1.1  reinoud 
   2183   1.1  reinoud 	old_size  = inflen;
   2184   1.1  reinoud 	size_diff = new_size - old_size;
   2185   1.1  reinoud 
   2186   1.1  reinoud 	DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
   2187   1.1  reinoud 
   2188   1.1  reinoud 	evacuated_data = NULL;
   2189   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
   2190   1.1  reinoud 		if (l_ad + size_diff <= max_l_ad) {
   2191   1.1  reinoud 			/* only reflect size change directly in the node */
   2192   1.1  reinoud 			inflen  += size_diff;
   2193   1.1  reinoud 			objsize += size_diff;
   2194   1.1  reinoud 			l_ad    += size_diff;
   2195   1.1  reinoud 			crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
   2196   1.1  reinoud 			if (fe) {
   2197   1.1  reinoud 				fe->inf_len   = udf_rw64(inflen);
   2198   1.1  reinoud 				fe->l_ad      = udf_rw32(l_ad);
   2199   1.1  reinoud 				fe->tag.desc_crc_len = udf_rw32(crclen);
   2200   1.1  reinoud 			} else {
   2201   1.1  reinoud 				efe->inf_len  = udf_rw64(inflen);
   2202   1.1  reinoud 				efe->obj_size = udf_rw64(objsize);
   2203   1.1  reinoud 				efe->l_ad     = udf_rw32(l_ad);
   2204   1.1  reinoud 				efe->tag.desc_crc_len = udf_rw32(crclen);
   2205   1.1  reinoud 			}
   2206   1.1  reinoud 			error = 0;
   2207   1.1  reinoud 
   2208   1.1  reinoud 			/* set new size for uvm */
   2209   1.1  reinoud 			uvm_vnp_setsize(vp, old_size);
   2210   1.1  reinoud 			uvm_vnp_setwritesize(vp, new_size);
   2211   1.1  reinoud 
   2212   1.1  reinoud #if 0
   2213   1.1  reinoud 			/* zero append space in buffer */
   2214   1.1  reinoud 			uvm_vnp_zerorange(vp, old_size, new_size - old_size);
   2215   1.1  reinoud #endif
   2216   1.1  reinoud 
   2217   1.9  reinoud 			udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
   2218   1.9  reinoud 
   2219   1.1  reinoud 			/* unlock */
   2220   1.1  reinoud 			UDF_UNLOCK_NODE(udf_node, 0);
   2221   1.1  reinoud 
   2222   1.1  reinoud 			KASSERT(new_inflen == orig_inflen + size_diff);
   2223   1.1  reinoud 			KASSERT(new_lbrec == orig_lbrec);
   2224   1.1  reinoud 			KASSERT(new_lbrec == 0);
   2225   1.1  reinoud 			return 0;
   2226   1.1  reinoud 		}
   2227   1.1  reinoud 
   2228   1.1  reinoud 		DPRINTF(ALLOC, ("\tCONVERT from internal\n"));
   2229   1.1  reinoud 
   2230   1.1  reinoud 		if (old_size > 0) {
   2231   1.1  reinoud 			/* allocate some space and copy in the stuff to keep */
   2232   1.1  reinoud 			evacuated_data = malloc(lb_size, M_UDFTEMP, M_WAITOK);
   2233   1.1  reinoud 			memset(evacuated_data, 0, lb_size);
   2234   1.1  reinoud 
   2235   1.1  reinoud 			/* node is locked, so safe to exit mutex */
   2236   1.1  reinoud 			UDF_UNLOCK_NODE(udf_node, 0);
   2237   1.1  reinoud 
   2238   1.1  reinoud 			/* read in using the `normal' vn_rdwr() */
   2239   1.1  reinoud 			error = vn_rdwr(UIO_READ, udf_node->vnode,
   2240   1.1  reinoud 					evacuated_data, old_size, 0,
   2241   1.1  reinoud 					UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
   2242   1.1  reinoud 					FSCRED, NULL, NULL);
   2243   1.1  reinoud 
   2244   1.1  reinoud 			/* enter again */
   2245   1.1  reinoud 			UDF_LOCK_NODE(udf_node, 0);
   2246   1.1  reinoud 		}
   2247   1.1  reinoud 
   2248  1.14  reinoud 		/* convert to a normal alloc and select type */
   2249  1.17  reinoud 		isdir    = (vp->v_type == VDIR);
   2250  1.17  reinoud 		my_part  = udf_rw16(udf_node->loc.loc.part_num);
   2251  1.17  reinoud 		dst_part = isdir? ump->fids_part : ump->data_part;
   2252  1.17  reinoud 		addr_type = UDF_ICB_SHORT_ALLOC;
   2253  1.17  reinoud 		if (dst_part != my_part)
   2254  1.17  reinoud 			addr_type = UDF_ICB_LONG_ALLOC;
   2255  1.14  reinoud 
   2256   1.1  reinoud 		icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   2257  1.17  reinoud 		icbflags |= addr_type;
   2258   1.1  reinoud 		icbtag->flags = udf_rw16(icbflags);
   2259   1.1  reinoud 
   2260   1.1  reinoud 		/* wipe old descriptor space */
   2261   1.1  reinoud 		udf_wipe_adslots(udf_node);
   2262   1.1  reinoud 
   2263   1.1  reinoud 		memset(&c_ad, 0, sizeof(struct long_ad));
   2264   1.1  reinoud 		c_ad.len          = udf_rw32(old_size | UDF_EXT_FREE);
   2265   1.1  reinoud 		c_ad.loc.part_num = udf_rw16(0); /* not relevant */
   2266   1.1  reinoud 		c_ad.loc.lb_num   = udf_rw32(0); /* not relevant */
   2267   1.1  reinoud 
   2268   1.1  reinoud 		slot = 0;
   2269   1.1  reinoud 	} else {
   2270   1.1  reinoud 		/* goto the last entry (if any) */
   2271   1.1  reinoud 		slot     = 0;
   2272   1.1  reinoud 		cpy_slot = 0;
   2273   1.1  reinoud 		foffset  = 0;
   2274   1.1  reinoud 		memset(&c_ad, 0, sizeof(struct long_ad));
   2275   1.1  reinoud 		for (;;) {
   2276   1.1  reinoud 			udf_get_adslot(udf_node, slot, &c_ad, &eof);
   2277   1.1  reinoud 			if (eof)
   2278   1.1  reinoud 				break;
   2279   1.1  reinoud 
   2280   1.1  reinoud 			len   = udf_rw32(c_ad.len);
   2281   1.1  reinoud 			flags = UDF_EXT_FLAGS(len);
   2282   1.1  reinoud 			len   = UDF_EXT_LEN(len);
   2283   1.1  reinoud 
   2284   1.1  reinoud 			end_foffset = foffset + len;
   2285   1.1  reinoud 			if (flags != UDF_EXT_REDIRECT)
   2286   1.1  reinoud 				foffset = end_foffset;
   2287   1.1  reinoud 
   2288   1.1  reinoud 			slot++;
   2289   1.1  reinoud 		}
   2290   1.1  reinoud 		/* at end of adslots */
   2291   1.1  reinoud 
   2292   1.1  reinoud 		/* special case if the old size was zero, then there is no last slot */
   2293   1.1  reinoud 		if (old_size == 0) {
   2294   1.1  reinoud 			c_ad.len          = udf_rw32(0 | UDF_EXT_FREE);
   2295   1.1  reinoud 			c_ad.loc.part_num = udf_rw16(0); /* not relevant */
   2296   1.1  reinoud 			c_ad.loc.lb_num   = udf_rw32(0); /* not relevant */
   2297   1.1  reinoud 		} else {
   2298   1.1  reinoud 			/* refetch last slot */
   2299   1.1  reinoud 			slot--;
   2300   1.1  reinoud 			udf_get_adslot(udf_node, slot, &c_ad, &eof);
   2301   1.1  reinoud 		}
   2302   1.1  reinoud 	}
   2303   1.1  reinoud 
   2304   1.1  reinoud 	/*
   2305   1.1  reinoud 	 * If the length of the last slot is not a multiple of lb_size, adjust
   2306   1.1  reinoud 	 * length so that it is; don't forget to adjust `append_len'! relevant for
   2307   1.1  reinoud 	 * extending existing files
   2308   1.1  reinoud 	 */
   2309   1.1  reinoud 	len   = udf_rw32(c_ad.len);
   2310   1.1  reinoud 	flags = UDF_EXT_FLAGS(len);
   2311   1.1  reinoud 	len   = UDF_EXT_LEN(len);
   2312   1.1  reinoud 
   2313   1.1  reinoud 	lastblock_grow = 0;
   2314   1.1  reinoud 	if (len % lb_size > 0) {
   2315   1.1  reinoud 		lastblock_grow = lb_size - (len % lb_size);
   2316   1.1  reinoud 		lastblock_grow = MIN(size_diff, lastblock_grow);
   2317   1.1  reinoud 		len += lastblock_grow;
   2318   1.1  reinoud 		c_ad.len = udf_rw32(len | flags);
   2319   1.1  reinoud 
   2320   1.1  reinoud 		/* TODO zero appened space in buffer! */
   2321   1.1  reinoud 		/* using uvm_vnp_zerorange(vp, old_size, new_size - old_size); ? */
   2322   1.1  reinoud 	}
   2323   1.1  reinoud 	memset(&s_ad, 0, sizeof(struct long_ad));
   2324   1.1  reinoud 
   2325   1.1  reinoud 	/* size_diff can be bigger than allowed, so grow in chunks */
   2326   1.1  reinoud 	append_len = size_diff - lastblock_grow;
   2327   1.1  reinoud 	while (append_len > 0) {
   2328   1.1  reinoud 		chunk = MIN(append_len, max_len);
   2329   1.1  reinoud 		s_ad.len = udf_rw32(chunk | UDF_EXT_FREE);
   2330   1.1  reinoud 		s_ad.loc.part_num = udf_rw16(0);
   2331   1.1  reinoud 		s_ad.loc.lb_num   = udf_rw32(0);
   2332   1.1  reinoud 
   2333   1.1  reinoud 		if (udf_ads_merge(lb_size, &c_ad, &s_ad)) {
   2334   1.1  reinoud 			/* not mergable (anymore) */
   2335  1.11  reinoud 			error = udf_append_adslot(udf_node, &slot, &c_ad);
   2336   1.1  reinoud 			if (error)
   2337   1.1  reinoud 				goto errorout;
   2338   1.1  reinoud 			slot++;
   2339   1.1  reinoud 			c_ad = s_ad;
   2340   1.1  reinoud 			memset(&s_ad, 0, sizeof(struct long_ad));
   2341   1.1  reinoud 		}
   2342   1.1  reinoud 		append_len -= chunk;
   2343   1.1  reinoud 	}
   2344   1.1  reinoud 
   2345   1.1  reinoud 	/* if there is a rest piece in the accumulator, append it */
   2346   1.8  reinoud 	if (UDF_EXT_LEN(udf_rw32(c_ad.len)) > 0) {
   2347  1.11  reinoud 		error = udf_append_adslot(udf_node, &slot, &c_ad);
   2348   1.1  reinoud 		if (error)
   2349   1.1  reinoud 			goto errorout;
   2350   1.1  reinoud 		slot++;
   2351   1.1  reinoud 	}
   2352   1.1  reinoud 
   2353   1.1  reinoud 	/* if there is a rest piece that didn't fit, append it */
   2354   1.8  reinoud 	if (UDF_EXT_LEN(udf_rw32(s_ad.len)) > 0) {
   2355  1.11  reinoud 		error = udf_append_adslot(udf_node, &slot, &s_ad);
   2356   1.1  reinoud 		if (error)
   2357   1.1  reinoud 			goto errorout;
   2358   1.1  reinoud 		slot++;
   2359   1.1  reinoud 	}
   2360   1.1  reinoud 
   2361   1.1  reinoud 	inflen  += size_diff;
   2362   1.1  reinoud 	objsize += size_diff;
   2363   1.1  reinoud 	if (fe) {
   2364   1.1  reinoud 		fe->inf_len   = udf_rw64(inflen);
   2365   1.1  reinoud 	} else {
   2366   1.1  reinoud 		efe->inf_len  = udf_rw64(inflen);
   2367   1.1  reinoud 		efe->obj_size = udf_rw64(objsize);
   2368   1.1  reinoud 	}
   2369   1.1  reinoud 	error = 0;
   2370   1.1  reinoud 
   2371   1.1  reinoud 	if (evacuated_data) {
   2372   1.1  reinoud 		/* set new write size for uvm */
   2373   1.1  reinoud 		uvm_vnp_setwritesize(vp, old_size);
   2374   1.1  reinoud 
   2375   1.1  reinoud 		/* write out evacuated data */
   2376   1.1  reinoud 		error = vn_rdwr(UIO_WRITE, udf_node->vnode,
   2377   1.1  reinoud 				evacuated_data, old_size, 0,
   2378   1.1  reinoud 				UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
   2379   1.1  reinoud 				FSCRED, NULL, NULL);
   2380   1.1  reinoud 		uvm_vnp_setsize(vp, old_size);
   2381   1.1  reinoud 	}
   2382   1.1  reinoud 
   2383   1.1  reinoud errorout:
   2384   1.1  reinoud 	if (evacuated_data)
   2385   1.1  reinoud 		free(evacuated_data, M_UDFTEMP);
   2386   1.9  reinoud 
   2387  1.11  reinoud 	udf_count_alloc_exts(udf_node);
   2388  1.11  reinoud 
   2389   1.9  reinoud 	udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
   2390   1.1  reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   2391   1.1  reinoud 
   2392   1.1  reinoud 	KASSERT(new_inflen == orig_inflen + size_diff);
   2393   1.1  reinoud 	KASSERT(new_lbrec == orig_lbrec);
   2394   1.1  reinoud 
   2395   1.1  reinoud 	return error;
   2396   1.1  reinoud }
   2397   1.1  reinoud 
   2398   1.1  reinoud /* --------------------------------------------------------------------- */
   2399   1.1  reinoud 
   2400   1.1  reinoud int
   2401   1.1  reinoud udf_shrink_node(struct udf_node *udf_node, uint64_t new_size)
   2402   1.1  reinoud {
   2403   1.1  reinoud 	struct vnode *vp = udf_node->vnode;
   2404   1.1  reinoud 	struct udf_mount *ump = udf_node->ump;
   2405   1.1  reinoud 	struct file_entry    *fe;
   2406   1.1  reinoud 	struct extfile_entry *efe;
   2407   1.1  reinoud 	struct icb_tag  *icbtag;
   2408   1.1  reinoud 	struct long_ad c_ad, s_ad, *node_ad_cpy;
   2409   1.1  reinoud 	uint64_t size_diff, old_size, inflen, objsize;
   2410   1.1  reinoud 	uint64_t foffset, end_foffset;
   2411   1.1  reinoud 	uint64_t orig_inflen, orig_lbrec, new_inflen, new_lbrec;
   2412   1.1  reinoud 	uint32_t lb_size, dscr_size, crclen;
   2413   1.1  reinoud 	uint32_t slot_offset;
   2414   1.1  reinoud 	uint32_t len, flags, max_len;
   2415   1.1  reinoud 	uint32_t num_lb, lb_num;
   2416   1.1  reinoud 	uint32_t max_l_ad, l_ad, l_ea;
   2417   1.1  reinoud 	uint16_t vpart_num;
   2418   1.1  reinoud 	uint8_t *data_pos;
   2419   1.1  reinoud 	int icbflags, addr_type;
   2420   1.1  reinoud 	int slot, cpy_slot, cpy_slots;
   2421   1.1  reinoud 	int eof, error;
   2422   1.1  reinoud 
   2423   1.1  reinoud 	DPRINTF(ALLOC, ("udf_shrink_node\n"));
   2424   1.9  reinoud 
   2425   1.9  reinoud 	UDF_LOCK_NODE(udf_node, 0);
   2426   1.1  reinoud 	udf_node_sanity_check(udf_node, &orig_inflen, &orig_lbrec);
   2427   1.1  reinoud 
   2428   1.1  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   2429   1.1  reinoud 	max_len = ((UDF_EXT_MAXLEN / lb_size) * lb_size);
   2430   1.1  reinoud 
   2431   1.1  reinoud 	/* do the work */
   2432   1.1  reinoud 	fe  = udf_node->fe;
   2433   1.1  reinoud 	efe = udf_node->efe;
   2434   1.1  reinoud 	if (fe) {
   2435   1.1  reinoud 		icbtag  = &fe->icbtag;
   2436   1.1  reinoud 		inflen  = udf_rw64(fe->inf_len);
   2437   1.1  reinoud 		objsize = inflen;
   2438   1.1  reinoud 		dscr_size  = sizeof(struct file_entry) -1;
   2439   1.1  reinoud 		l_ea       = udf_rw32(fe->l_ea);
   2440   1.1  reinoud 		l_ad       = udf_rw32(fe->l_ad);
   2441   1.1  reinoud 		data_pos = (uint8_t *) fe + dscr_size + l_ea;
   2442   1.1  reinoud 	} else {
   2443   1.1  reinoud 		icbtag  = &efe->icbtag;
   2444   1.1  reinoud 		inflen  = udf_rw64(efe->inf_len);
   2445   1.1  reinoud 		objsize = udf_rw64(efe->obj_size);
   2446   1.1  reinoud 		dscr_size  = sizeof(struct extfile_entry) -1;
   2447   1.1  reinoud 		l_ea       = udf_rw32(efe->l_ea);
   2448   1.1  reinoud 		l_ad       = udf_rw32(efe->l_ad);
   2449   1.1  reinoud 		data_pos = (uint8_t *) efe + dscr_size + l_ea;
   2450   1.1  reinoud 	}
   2451   1.1  reinoud 	max_l_ad = lb_size - dscr_size - l_ea;
   2452   1.1  reinoud 
   2453   1.1  reinoud 	icbflags   = udf_rw16(icbtag->flags);
   2454   1.1  reinoud 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   2455   1.1  reinoud 
   2456   1.1  reinoud 	old_size  = inflen;
   2457   1.1  reinoud 	size_diff = old_size - new_size;
   2458   1.1  reinoud 
   2459   1.1  reinoud 	DPRINTF(ALLOC, ("\tfrom %"PRIu64" to %"PRIu64"\n", old_size, new_size));
   2460   1.1  reinoud 
   2461   1.1  reinoud 	/* shrink the node to its new size */
   2462   1.1  reinoud 	if (addr_type == UDF_ICB_INTERN_ALLOC) {
   2463   1.1  reinoud 		/* only reflect size change directly in the node */
   2464   1.1  reinoud 		KASSERT(new_size <= max_l_ad);
   2465   1.1  reinoud 		inflen  -= size_diff;
   2466   1.1  reinoud 		objsize -= size_diff;
   2467   1.1  reinoud 		l_ad    -= size_diff;
   2468   1.1  reinoud 		crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
   2469   1.1  reinoud 		if (fe) {
   2470   1.1  reinoud 			fe->inf_len   = udf_rw64(inflen);
   2471   1.1  reinoud 			fe->l_ad      = udf_rw32(l_ad);
   2472   1.1  reinoud 			fe->tag.desc_crc_len = udf_rw32(crclen);
   2473   1.1  reinoud 		} else {
   2474   1.1  reinoud 			efe->inf_len  = udf_rw64(inflen);
   2475   1.1  reinoud 			efe->obj_size = udf_rw64(objsize);
   2476   1.1  reinoud 			efe->l_ad     = udf_rw32(l_ad);
   2477   1.1  reinoud 			efe->tag.desc_crc_len = udf_rw32(crclen);
   2478   1.1  reinoud 		}
   2479   1.1  reinoud 		error = 0;
   2480   1.7  reinoud 
   2481   1.7  reinoud 		/* clear the space in the descriptor */
   2482   1.7  reinoud 		KASSERT(old_size > new_size);
   2483   1.7  reinoud 		memset(data_pos + new_size, 0, old_size - new_size);
   2484   1.7  reinoud 
   2485   1.1  reinoud 		/* TODO zero appened space in buffer! */
   2486   1.1  reinoud 		/* using uvm_vnp_zerorange(vp, old_size, old_size - new_size); ? */
   2487   1.1  reinoud 
   2488   1.1  reinoud 		/* set new size for uvm */
   2489   1.1  reinoud 		uvm_vnp_setsize(vp, new_size);
   2490   1.9  reinoud 
   2491   1.9  reinoud 		udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
   2492   1.1  reinoud 		UDF_UNLOCK_NODE(udf_node, 0);
   2493   1.1  reinoud 
   2494   1.1  reinoud 		KASSERT(new_inflen == orig_inflen - size_diff);
   2495   1.1  reinoud 		KASSERT(new_lbrec == orig_lbrec);
   2496   1.1  reinoud 		KASSERT(new_lbrec == 0);
   2497   1.1  reinoud 
   2498   1.1  reinoud 		return 0;
   2499   1.1  reinoud 	}
   2500   1.1  reinoud 
   2501   1.1  reinoud 	/* setup node cleanup extents copy space */
   2502   1.1  reinoud 	node_ad_cpy = malloc(lb_size * UDF_MAX_ALLOC_EXTENTS,
   2503   1.1  reinoud 		M_UDFMNT, M_WAITOK);
   2504   1.1  reinoud 	memset(node_ad_cpy, 0, lb_size * UDF_MAX_ALLOC_EXTENTS);
   2505   1.1  reinoud 
   2506   1.1  reinoud 	/*
   2507   1.1  reinoud 	 * Shrink the node by releasing the allocations and truncate the last
   2508   1.1  reinoud 	 * allocation to the new size. If the new size fits into the
   2509   1.1  reinoud 	 * allocation descriptor itself, transform it into an
   2510   1.1  reinoud 	 * UDF_ICB_INTERN_ALLOC.
   2511   1.1  reinoud 	 */
   2512   1.1  reinoud 	slot     = 0;
   2513   1.1  reinoud 	cpy_slot = 0;
   2514   1.1  reinoud 	foffset  = 0;
   2515   1.1  reinoud 
   2516   1.1  reinoud 	/* 1) copy till first overlap piece to the rewrite buffer */
   2517   1.1  reinoud 	for (;;) {
   2518   1.1  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
   2519   1.1  reinoud 		if (eof) {
   2520   1.1  reinoud 			DPRINTF(WRITE,
   2521   1.1  reinoud 				("Shrink node failed: "
   2522   1.1  reinoud 				 "encountered EOF\n"));
   2523   1.1  reinoud 			error = EINVAL;
   2524   1.1  reinoud 			goto errorout; /* panic? */
   2525   1.1  reinoud 		}
   2526   1.1  reinoud 		len   = udf_rw32(s_ad.len);
   2527   1.1  reinoud 		flags = UDF_EXT_FLAGS(len);
   2528   1.1  reinoud 		len   = UDF_EXT_LEN(len);
   2529   1.1  reinoud 
   2530   1.1  reinoud 		if (flags == UDF_EXT_REDIRECT) {
   2531   1.1  reinoud 			slot++;
   2532   1.1  reinoud 			continue;
   2533   1.1  reinoud 		}
   2534   1.1  reinoud 
   2535   1.1  reinoud 		end_foffset = foffset + len;
   2536   1.1  reinoud 		if (end_foffset > new_size)
   2537   1.1  reinoud 			break;	/* found */
   2538   1.1  reinoud 
   2539   1.1  reinoud 		node_ad_cpy[cpy_slot++] = s_ad;
   2540   1.1  reinoud 
   2541   1.1  reinoud 		DPRINTF(ALLOC, ("\t1: vp %d, lb %d, len %d, flags %d "
   2542   1.1  reinoud 			"-> stack\n",
   2543   1.1  reinoud 			udf_rw16(s_ad.loc.part_num),
   2544   1.1  reinoud 			udf_rw32(s_ad.loc.lb_num),
   2545   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
   2546   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   2547   1.1  reinoud 
   2548   1.1  reinoud 		foffset = end_foffset;
   2549   1.1  reinoud 		slot++;
   2550   1.1  reinoud 	}
   2551   1.1  reinoud 	slot_offset = new_size - foffset;
   2552   1.1  reinoud 
   2553   1.1  reinoud 	/* 2) trunc overlapping slot at overlap and copy it */
   2554   1.1  reinoud 	if (slot_offset > 0) {
   2555   1.1  reinoud 		lb_num    = udf_rw32(s_ad.loc.lb_num);
   2556   1.1  reinoud 		vpart_num = udf_rw16(s_ad.loc.part_num);
   2557   1.1  reinoud 
   2558   1.1  reinoud 		if (flags == UDF_EXT_ALLOCATED) {
   2559  1.10  reinoud 			/* note: round DOWN on num_lb */
   2560   1.1  reinoud 			lb_num += (slot_offset + lb_size -1) / lb_size;
   2561  1.10  reinoud 			num_lb  = (len - slot_offset) / lb_size;
   2562   1.1  reinoud 
   2563   1.1  reinoud 			udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
   2564   1.1  reinoud 		}
   2565   1.1  reinoud 
   2566   1.1  reinoud 		s_ad.len = udf_rw32(slot_offset | flags);
   2567   1.1  reinoud 		node_ad_cpy[cpy_slot++] = s_ad;
   2568   1.1  reinoud 		slot++;
   2569   1.1  reinoud 
   2570   1.1  reinoud 		DPRINTF(ALLOC, ("\t2: vp %d, lb %d, len %d, flags %d "
   2571   1.1  reinoud 			"-> stack\n",
   2572   1.1  reinoud 			udf_rw16(s_ad.loc.part_num),
   2573   1.1  reinoud 			udf_rw32(s_ad.loc.lb_num),
   2574   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(s_ad.len)),
   2575   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   2576   1.1  reinoud 	}
   2577   1.1  reinoud 
   2578   1.1  reinoud 	/* 3) delete remainder */
   2579   1.1  reinoud 	for (;;) {
   2580   1.1  reinoud 		udf_get_adslot(udf_node, slot, &s_ad, &eof);
   2581   1.1  reinoud 		if (eof)
   2582   1.1  reinoud 			break;
   2583   1.1  reinoud 
   2584   1.1  reinoud 		len       = udf_rw32(s_ad.len);
   2585   1.1  reinoud 		flags     = UDF_EXT_FLAGS(len);
   2586   1.1  reinoud 		len       = UDF_EXT_LEN(len);
   2587   1.1  reinoud 
   2588   1.1  reinoud 		if (flags == UDF_EXT_REDIRECT) {
   2589   1.1  reinoud 			slot++;
   2590   1.1  reinoud 			continue;
   2591   1.1  reinoud 		}
   2592   1.1  reinoud 
   2593   1.1  reinoud 		DPRINTF(ALLOC, ("\t3: delete remainder "
   2594   1.1  reinoud 			"vp %d lb %d, len %d, flags %d\n",
   2595   1.1  reinoud 		udf_rw16(s_ad.loc.part_num),
   2596   1.1  reinoud 		udf_rw32(s_ad.loc.lb_num),
   2597   1.1  reinoud 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
   2598   1.1  reinoud 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   2599   1.1  reinoud 
   2600   1.1  reinoud 		if (flags == UDF_EXT_ALLOCATED) {
   2601   1.1  reinoud 			lb_num    = udf_rw32(s_ad.loc.lb_num);
   2602   1.1  reinoud 			vpart_num = udf_rw16(s_ad.loc.part_num);
   2603   1.1  reinoud 			num_lb    = (len + lb_size - 1) / lb_size;
   2604   1.1  reinoud 
   2605   1.1  reinoud 			udf_free_allocated_space(ump, lb_num, vpart_num,
   2606   1.1  reinoud 				num_lb);
   2607   1.1  reinoud 		}
   2608   1.1  reinoud 
   2609   1.1  reinoud 		slot++;
   2610   1.1  reinoud 	}
   2611   1.1  reinoud 
   2612   1.1  reinoud 	/* 4) if it will fit into the descriptor then convert */
   2613   1.1  reinoud 	if (new_size < max_l_ad) {
   2614   1.1  reinoud 		/*
   2615   1.1  reinoud 		 * resque/evacuate old piece by reading it in, and convert it
   2616   1.1  reinoud 		 * to internal alloc.
   2617   1.1  reinoud 		 */
   2618   1.1  reinoud 		if (new_size == 0) {
   2619   1.1  reinoud 			/* XXX/TODO only for zero sizing now */
   2620   1.1  reinoud 			udf_wipe_adslots(udf_node);
   2621   1.1  reinoud 
   2622   1.1  reinoud 			icbflags &= ~UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   2623   1.1  reinoud 			icbflags |=  UDF_ICB_INTERN_ALLOC;
   2624   1.1  reinoud 			icbtag->flags = udf_rw16(icbflags);
   2625   1.1  reinoud 
   2626   1.1  reinoud 			inflen  -= size_diff;	KASSERT(inflen == 0);
   2627   1.1  reinoud 			objsize -= size_diff;
   2628   1.1  reinoud 			l_ad     = new_size;
   2629   1.1  reinoud 			crclen = dscr_size - UDF_DESC_TAG_LENGTH + l_ea + l_ad;
   2630   1.1  reinoud 			if (fe) {
   2631   1.1  reinoud 				fe->inf_len   = udf_rw64(inflen);
   2632   1.1  reinoud 				fe->l_ad      = udf_rw32(l_ad);
   2633   1.1  reinoud 				fe->tag.desc_crc_len = udf_rw32(crclen);
   2634   1.1  reinoud 			} else {
   2635   1.1  reinoud 				efe->inf_len  = udf_rw64(inflen);
   2636   1.1  reinoud 				efe->obj_size = udf_rw64(objsize);
   2637   1.1  reinoud 				efe->l_ad     = udf_rw32(l_ad);
   2638   1.1  reinoud 				efe->tag.desc_crc_len = udf_rw32(crclen);
   2639   1.1  reinoud 			}
   2640   1.1  reinoud 			/* eventually copy in evacuated piece */
   2641   1.1  reinoud 			/* set new size for uvm */
   2642   1.1  reinoud 			uvm_vnp_setsize(vp, new_size);
   2643   1.1  reinoud 
   2644   1.1  reinoud 			free(node_ad_cpy, M_UDFMNT);
   2645   1.9  reinoud 			udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
   2646   1.9  reinoud 
   2647   1.1  reinoud 			UDF_UNLOCK_NODE(udf_node, 0);
   2648   1.1  reinoud 
   2649   1.1  reinoud 			KASSERT(new_inflen == orig_inflen - size_diff);
   2650   1.1  reinoud 			KASSERT(new_inflen == 0);
   2651   1.1  reinoud 			KASSERT(new_lbrec == 0);
   2652   1.1  reinoud 
   2653   1.1  reinoud 			return 0;
   2654   1.1  reinoud 		}
   2655   1.1  reinoud 
   2656   1.1  reinoud 		printf("UDF_SHRINK_NODE: could convert to internal alloc!\n");
   2657   1.1  reinoud 	}
   2658   1.1  reinoud 
   2659   1.1  reinoud 	/* 5) reset node descriptors */
   2660   1.1  reinoud 	udf_wipe_adslots(udf_node);
   2661   1.1  reinoud 
   2662   1.1  reinoud 	/* 6) copy back extents; merge when possible. Recounting on the fly */
   2663   1.1  reinoud 	cpy_slots = cpy_slot;
   2664   1.1  reinoud 
   2665   1.1  reinoud 	c_ad = node_ad_cpy[0];
   2666   1.1  reinoud 	slot = 0;
   2667   1.1  reinoud 	for (cpy_slot = 1; cpy_slot < cpy_slots; cpy_slot++) {
   2668   1.1  reinoud 		s_ad = node_ad_cpy[cpy_slot];
   2669   1.1  reinoud 
   2670   1.1  reinoud 		DPRINTF(ALLOC, ("\t6: stack -> got mapping vp %d "
   2671   1.1  reinoud 			"lb %d, len %d, flags %d\n",
   2672   1.1  reinoud 		udf_rw16(s_ad.loc.part_num),
   2673   1.1  reinoud 		udf_rw32(s_ad.loc.lb_num),
   2674   1.1  reinoud 		UDF_EXT_LEN(udf_rw32(s_ad.len)),
   2675   1.1  reinoud 		UDF_EXT_FLAGS(udf_rw32(s_ad.len)) >> 30));
   2676   1.1  reinoud 
   2677   1.1  reinoud 		/* see if we can merge */
   2678   1.1  reinoud 		if (udf_ads_merge(lb_size, &c_ad, &s_ad)) {
   2679   1.1  reinoud 			/* not mergable (anymore) */
   2680   1.1  reinoud 			DPRINTF(ALLOC, ("\t6: appending vp %d lb %d, "
   2681   1.1  reinoud 				"len %d, flags %d\n",
   2682   1.1  reinoud 			udf_rw16(c_ad.loc.part_num),
   2683   1.1  reinoud 			udf_rw32(c_ad.loc.lb_num),
   2684   1.1  reinoud 			UDF_EXT_LEN(udf_rw32(c_ad.len)),
   2685   1.1  reinoud 			UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
   2686   1.1  reinoud 
   2687  1.11  reinoud 			error = udf_append_adslot(udf_node, &slot, &c_ad);
   2688   1.1  reinoud 			if (error)
   2689   1.1  reinoud 				goto errorout; /* panic? */
   2690   1.1  reinoud 			c_ad = s_ad;
   2691   1.1  reinoud 			slot++;
   2692   1.1  reinoud 		}
   2693   1.1  reinoud 	}
   2694   1.1  reinoud 
   2695   1.1  reinoud 	/* 7) push rest slot (if any) */
   2696   1.1  reinoud 	if (UDF_EXT_LEN(c_ad.len) > 0) {
   2697   1.1  reinoud 		DPRINTF(ALLOC, ("\t7: last append vp %d lb %d, "
   2698   1.1  reinoud 				"len %d, flags %d\n",
   2699   1.1  reinoud 		udf_rw16(c_ad.loc.part_num),
   2700   1.1  reinoud 		udf_rw32(c_ad.loc.lb_num),
   2701   1.1  reinoud 		UDF_EXT_LEN(udf_rw32(c_ad.len)),
   2702   1.1  reinoud 		UDF_EXT_FLAGS(udf_rw32(c_ad.len)) >> 30));
   2703   1.1  reinoud 
   2704  1.11  reinoud 		error = udf_append_adslot(udf_node, &slot, &c_ad);
   2705   1.1  reinoud 		if (error)
   2706   1.1  reinoud 			goto errorout; /* panic? */
   2707   1.1  reinoud 		;
   2708   1.1  reinoud 	}
   2709   1.1  reinoud 
   2710   1.1  reinoud 	inflen  -= size_diff;
   2711   1.1  reinoud 	objsize -= size_diff;
   2712   1.1  reinoud 	if (fe) {
   2713   1.1  reinoud 		fe->inf_len   = udf_rw64(inflen);
   2714   1.1  reinoud 	} else {
   2715   1.1  reinoud 		efe->inf_len  = udf_rw64(inflen);
   2716   1.1  reinoud 		efe->obj_size = udf_rw64(objsize);
   2717   1.1  reinoud 	}
   2718   1.1  reinoud 	error = 0;
   2719   1.1  reinoud 
   2720   1.1  reinoud 	/* set new size for uvm */
   2721   1.1  reinoud 	uvm_vnp_setsize(vp, new_size);
   2722   1.1  reinoud 
   2723   1.1  reinoud errorout:
   2724   1.1  reinoud 	free(node_ad_cpy, M_UDFMNT);
   2725   1.9  reinoud 
   2726  1.11  reinoud 	udf_count_alloc_exts(udf_node);
   2727  1.11  reinoud 
   2728   1.9  reinoud 	udf_node_sanity_check(udf_node, &new_inflen, &new_lbrec);
   2729   1.1  reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   2730   1.1  reinoud 
   2731   1.1  reinoud 	KASSERT(new_inflen == orig_inflen - size_diff);
   2732   1.1  reinoud 
   2733   1.1  reinoud 	return error;
   2734   1.1  reinoud }
   2735   1.1  reinoud 
   2736