Home | History | Annotate | Line # | Download | only in udf
udf_subr.c revision 1.49.2.3
      1  1.49.2.3    simonb /* $NetBSD: udf_subr.c,v 1.49.2.3 2008/07/22 05:44:02 simonb Exp $ */
      2       1.1   reinoud 
      3       1.1   reinoud /*
      4      1.45   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 
     30       1.1   reinoud #include <sys/cdefs.h>
     31       1.1   reinoud #ifndef lint
     32  1.49.2.3    simonb __KERNEL_RCSID(0, "$NetBSD: udf_subr.c,v 1.49.2.3 2008/07/22 05:44:02 simonb Exp $");
     33       1.1   reinoud #endif /* not lint */
     34       1.1   reinoud 
     35       1.1   reinoud 
     36       1.1   reinoud #if defined(_KERNEL_OPT)
     37       1.1   reinoud #include "opt_quota.h"
     38       1.1   reinoud #include "opt_compat_netbsd.h"
     39       1.1   reinoud #endif
     40       1.1   reinoud 
     41       1.1   reinoud #include <sys/param.h>
     42       1.1   reinoud #include <sys/systm.h>
     43       1.1   reinoud #include <sys/sysctl.h>
     44       1.1   reinoud #include <sys/namei.h>
     45       1.1   reinoud #include <sys/proc.h>
     46       1.1   reinoud #include <sys/kernel.h>
     47       1.1   reinoud #include <sys/vnode.h>
     48       1.1   reinoud #include <miscfs/genfs/genfs_node.h>
     49       1.1   reinoud #include <sys/mount.h>
     50       1.1   reinoud #include <sys/buf.h>
     51       1.1   reinoud #include <sys/file.h>
     52       1.1   reinoud #include <sys/device.h>
     53       1.1   reinoud #include <sys/disklabel.h>
     54       1.1   reinoud #include <sys/ioctl.h>
     55       1.1   reinoud #include <sys/malloc.h>
     56       1.1   reinoud #include <sys/dirent.h>
     57       1.1   reinoud #include <sys/stat.h>
     58       1.1   reinoud #include <sys/conf.h>
     59       1.8  christos #include <sys/kauth.h>
     60      1.48   reinoud #include <fs/unicode.h>
     61      1.30   reinoud #include <dev/clock_subr.h>
     62       1.1   reinoud 
     63       1.1   reinoud #include <fs/udf/ecma167-udf.h>
     64       1.1   reinoud #include <fs/udf/udf_mount.h>
     65       1.1   reinoud 
     66      1.45   reinoud #if defined(_KERNEL_OPT)
     67      1.45   reinoud #include "opt_udf.h"
     68      1.45   reinoud #endif
     69      1.45   reinoud 
     70       1.1   reinoud #include "udf.h"
     71       1.1   reinoud #include "udf_subr.h"
     72       1.1   reinoud #include "udf_bswap.h"
     73       1.1   reinoud 
     74       1.1   reinoud 
     75      1.45   reinoud #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
     76      1.45   reinoud 
     77      1.45   reinoud #define UDF_SET_SYSTEMFILE(vp) \
     78      1.45   reinoud 	/* XXXAD Is the vnode locked? */	\
     79      1.45   reinoud 	(vp)->v_vflag |= VV_SYSTEM;		\
     80      1.45   reinoud 	vref(vp);			\
     81      1.45   reinoud 	vput(vp);			\
     82       1.1   reinoud 
     83      1.45   reinoud extern int syncer_maxdelay;     /* maximum delay time */
     84      1.45   reinoud extern int (**udf_vnodeop_p)(void *);
     85       1.1   reinoud 
     86      1.45   reinoud /* --------------------------------------------------------------------- */
     87       1.1   reinoud 
     88      1.45   reinoud //#ifdef DEBUG
     89      1.45   reinoud #if 1
     90       1.1   reinoud 
     91       1.1   reinoud #if 0
     92      1.45   reinoud static void
     93      1.45   reinoud udf_dumpblob(boid *blob, uint32_t dlen)
     94       1.1   reinoud {
     95      1.45   reinoud 	int i, j;
     96       1.1   reinoud 
     97       1.1   reinoud 	printf("blob = %p\n", blob);
     98       1.1   reinoud 	printf("dump of %d bytes\n", dlen);
     99       1.1   reinoud 
    100       1.1   reinoud 	for (i = 0; i < dlen; i+ = 16) {
    101       1.1   reinoud 		printf("%04x ", i);
    102       1.1   reinoud 		for (j = 0; j < 16; j++) {
    103       1.1   reinoud 			if (i+j < dlen) {
    104       1.1   reinoud 				printf("%02x ", blob[i+j]);
    105       1.1   reinoud 			} else {
    106       1.1   reinoud 				printf("   ");
    107       1.9  christos 			}
    108       1.9  christos 		}
    109       1.1   reinoud 		for (j = 0; j < 16; j++) {
    110       1.1   reinoud 			if (i+j < dlen) {
    111       1.1   reinoud 				if (blob[i+j]>32 && blob[i+j]! = 127) {
    112       1.1   reinoud 					printf("%c", blob[i+j]);
    113       1.1   reinoud 				} else {
    114       1.1   reinoud 					printf(".");
    115       1.9  christos 				}
    116       1.9  christos 			}
    117       1.9  christos 		}
    118       1.1   reinoud 		printf("\n");
    119       1.9  christos 	}
    120       1.1   reinoud 	printf("\n");
    121      1.45   reinoud 	Debugger();
    122       1.9  christos }
    123       1.1   reinoud #endif
    124       1.1   reinoud 
    125       1.1   reinoud static void
    126       1.1   reinoud udf_dump_discinfo(struct udf_mount *ump)
    127       1.1   reinoud {
    128       1.1   reinoud 	char   bits[128];
    129       1.1   reinoud 	struct mmc_discinfo *di = &ump->discinfo;
    130       1.1   reinoud 
    131       1.1   reinoud 	if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0)
    132       1.1   reinoud 		return;
    133       1.1   reinoud 
    134       1.1   reinoud 	printf("Device/media info  :\n");
    135       1.1   reinoud 	printf("\tMMC profile        0x%02x\n", di->mmc_profile);
    136       1.1   reinoud 	printf("\tderived class      %d\n", di->mmc_class);
    137       1.1   reinoud 	printf("\tsector size        %d\n", di->sector_size);
    138       1.1   reinoud 	printf("\tdisc state         %d\n", di->disc_state);
    139       1.1   reinoud 	printf("\tlast ses state     %d\n", di->last_session_state);
    140       1.1   reinoud 	printf("\tbg format state    %d\n", di->bg_format_state);
    141       1.1   reinoud 	printf("\tfrst track         %d\n", di->first_track);
    142       1.1   reinoud 	printf("\tfst on last ses    %d\n", di->first_track_last_session);
    143       1.1   reinoud 	printf("\tlst on last ses    %d\n", di->last_track_last_session);
    144       1.1   reinoud 	printf("\tlink block penalty %d\n", di->link_block_penalty);
    145       1.1   reinoud 	bitmask_snprintf(di->disc_flags, MMC_DFLAGS_FLAGBITS, bits,
    146       1.1   reinoud 		sizeof(bits));
    147       1.1   reinoud 	printf("\tdisc flags         %s\n", bits);
    148       1.1   reinoud 	printf("\tdisc id            %x\n", di->disc_id);
    149       1.1   reinoud 	printf("\tdisc barcode       %"PRIx64"\n", di->disc_barcode);
    150       1.1   reinoud 
    151       1.1   reinoud 	printf("\tnum sessions       %d\n", di->num_sessions);
    152       1.1   reinoud 	printf("\tnum tracks         %d\n", di->num_tracks);
    153       1.1   reinoud 
    154       1.1   reinoud 	bitmask_snprintf(di->mmc_cur, MMC_CAP_FLAGBITS, bits, sizeof(bits));
    155       1.1   reinoud 	printf("\tcapabilities cur   %s\n", bits);
    156       1.1   reinoud 	bitmask_snprintf(di->mmc_cap, MMC_CAP_FLAGBITS, bits, sizeof(bits));
    157       1.1   reinoud 	printf("\tcapabilities cap   %s\n", bits);
    158       1.1   reinoud }
    159       1.1   reinoud #else
    160       1.1   reinoud #define udf_dump_discinfo(a);
    161       1.1   reinoud #endif
    162       1.1   reinoud 
    163      1.45   reinoud 
    164      1.45   reinoud /* --------------------------------------------------------------------- */
    165      1.45   reinoud 
    166       1.1   reinoud /* not called often */
    167       1.1   reinoud int
    168       1.1   reinoud udf_update_discinfo(struct udf_mount *ump)
    169       1.1   reinoud {
    170       1.1   reinoud 	struct vnode *devvp = ump->devvp;
    171       1.1   reinoud 	struct partinfo dpart;
    172       1.1   reinoud 	struct mmc_discinfo *di;
    173       1.1   reinoud 	int error;
    174       1.1   reinoud 
    175       1.1   reinoud 	DPRINTF(VOLUMES, ("read/update disc info\n"));
    176       1.1   reinoud 	di = &ump->discinfo;
    177       1.1   reinoud 	memset(di, 0, sizeof(struct mmc_discinfo));
    178       1.1   reinoud 
    179       1.1   reinoud 	/* check if we're on a MMC capable device, i.e. CD/DVD */
    180      1.41     pooka 	error = VOP_IOCTL(devvp, MMCGETDISCINFO, di, FKIOCTL, NOCRED);
    181       1.1   reinoud 	if (error == 0) {
    182       1.1   reinoud 		udf_dump_discinfo(ump);
    183       1.1   reinoud 		return 0;
    184       1.9  christos 	}
    185       1.1   reinoud 
    186       1.1   reinoud 	/* disc partition support */
    187      1.41     pooka 	error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED);
    188       1.1   reinoud 	if (error)
    189       1.1   reinoud 		return ENODEV;
    190       1.1   reinoud 
    191       1.1   reinoud 	/* set up a disc info profile for partitions */
    192       1.1   reinoud 	di->mmc_profile		= 0x01;	/* disc type */
    193       1.1   reinoud 	di->mmc_class		= MMC_CLASS_DISC;
    194       1.1   reinoud 	di->disc_state		= MMC_STATE_CLOSED;
    195       1.1   reinoud 	di->last_session_state	= MMC_STATE_CLOSED;
    196       1.1   reinoud 	di->bg_format_state	= MMC_BGFSTATE_COMPLETED;
    197       1.1   reinoud 	di->link_block_penalty	= 0;
    198       1.1   reinoud 
    199       1.1   reinoud 	di->mmc_cur     = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE |
    200       1.4   reinoud 		MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE;
    201       1.1   reinoud 	di->mmc_cap    = di->mmc_cur;
    202       1.1   reinoud 	di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
    203       1.1   reinoud 
    204       1.1   reinoud 	/* TODO problem with last_possible_lba on resizable VND; request */
    205       1.1   reinoud 	di->last_possible_lba = dpart.part->p_size;
    206       1.1   reinoud 	di->sector_size       = dpart.disklab->d_secsize;
    207       1.1   reinoud 
    208       1.1   reinoud 	di->num_sessions = 1;
    209       1.1   reinoud 	di->num_tracks   = 1;
    210       1.1   reinoud 
    211       1.1   reinoud 	di->first_track  = 1;
    212       1.1   reinoud 	di->first_track_last_session = di->last_track_last_session = 1;
    213       1.1   reinoud 
    214       1.1   reinoud 	udf_dump_discinfo(ump);
    215       1.1   reinoud 	return 0;
    216       1.1   reinoud }
    217       1.1   reinoud 
    218       1.1   reinoud 
    219       1.1   reinoud int
    220       1.1   reinoud udf_update_trackinfo(struct udf_mount *ump, struct mmc_trackinfo *ti)
    221       1.1   reinoud {
    222       1.1   reinoud 	struct vnode *devvp = ump->devvp;
    223       1.1   reinoud 	struct mmc_discinfo *di = &ump->discinfo;
    224       1.1   reinoud 	int error, class;
    225       1.1   reinoud 
    226       1.1   reinoud 	DPRINTF(VOLUMES, ("read track info\n"));
    227       1.1   reinoud 
    228       1.1   reinoud 	class = di->mmc_class;
    229       1.1   reinoud 	if (class != MMC_CLASS_DISC) {
    230       1.1   reinoud 		/* tracknr specified in struct ti */
    231      1.41     pooka 		error = VOP_IOCTL(devvp, MMCGETTRACKINFO, ti, FKIOCTL, NOCRED);
    232       1.1   reinoud 		return error;
    233       1.9  christos 	}
    234       1.1   reinoud 
    235       1.1   reinoud 	/* disc partition support */
    236       1.1   reinoud 	if (ti->tracknr != 1)
    237       1.1   reinoud 		return EIO;
    238       1.1   reinoud 
    239       1.1   reinoud 	/* create fake ti (TODO check for resized vnds) */
    240       1.1   reinoud 	ti->sessionnr  = 1;
    241       1.1   reinoud 
    242       1.1   reinoud 	ti->track_mode = 0;	/* XXX */
    243       1.1   reinoud 	ti->data_mode  = 0;	/* XXX */
    244       1.1   reinoud 	ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
    245       1.1   reinoud 
    246       1.1   reinoud 	ti->track_start    = 0;
    247       1.1   reinoud 	ti->packet_size    = 1;
    248       1.1   reinoud 
    249       1.1   reinoud 	/* TODO support for resizable vnd */
    250       1.1   reinoud 	ti->track_size    = di->last_possible_lba;
    251       1.1   reinoud 	ti->next_writable = di->last_possible_lba;
    252       1.1   reinoud 	ti->last_recorded = ti->next_writable;
    253       1.1   reinoud 	ti->free_blocks   = 0;
    254       1.1   reinoud 
    255       1.1   reinoud 	return 0;
    256       1.1   reinoud }
    257       1.1   reinoud 
    258       1.1   reinoud 
    259      1.45   reinoud int
    260      1.45   reinoud udf_setup_writeparams(struct udf_mount *ump)
    261       1.1   reinoud {
    262      1.45   reinoud 	struct mmc_writeparams mmc_writeparams;
    263      1.45   reinoud 	int error;
    264      1.45   reinoud 
    265      1.45   reinoud 	if (ump->discinfo.mmc_class == MMC_CLASS_DISC)
    266      1.45   reinoud 		return 0;
    267      1.45   reinoud 
    268      1.45   reinoud 	/*
    269      1.45   reinoud 	 * only CD burning normally needs setting up, but other disc types
    270      1.45   reinoud 	 * might need other settings to be made. The MMC framework will set up
    271      1.45   reinoud 	 * the nessisary recording parameters according to the disc
    272      1.45   reinoud 	 * characteristics read in. Modifications can be made in the discinfo
    273      1.45   reinoud 	 * structure passed to change the nature of the disc.
    274      1.45   reinoud 	 */
    275      1.45   reinoud 
    276      1.45   reinoud 	memset(&mmc_writeparams, 0, sizeof(struct mmc_writeparams));
    277      1.45   reinoud 	mmc_writeparams.mmc_class  = ump->discinfo.mmc_class;
    278      1.45   reinoud 	mmc_writeparams.mmc_cur    = ump->discinfo.mmc_cur;
    279      1.45   reinoud 
    280      1.45   reinoud 	/*
    281      1.45   reinoud 	 * UDF dictates first track to determine track mode for the whole
    282      1.45   reinoud 	 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1]
    283      1.45   reinoud 	 * To prevent problems with a `reserved' track in front we start with
    284      1.45   reinoud 	 * the 2nd track and if that is not valid, go for the 1st.
    285      1.45   reinoud 	 */
    286      1.45   reinoud 	mmc_writeparams.tracknr = 2;
    287      1.45   reinoud 	mmc_writeparams.data_mode  = MMC_DATAMODE_DEFAULT;	/* XA disc */
    288      1.45   reinoud 	mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT;	/* data */
    289      1.45   reinoud 
    290      1.45   reinoud 	error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS, &mmc_writeparams,
    291      1.45   reinoud 			FKIOCTL, NOCRED);
    292      1.45   reinoud 	if (error) {
    293      1.45   reinoud 		mmc_writeparams.tracknr = 1;
    294      1.45   reinoud 		error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS,
    295      1.45   reinoud 				&mmc_writeparams, FKIOCTL, NOCRED);
    296      1.45   reinoud 	}
    297      1.45   reinoud 	return error;
    298      1.45   reinoud }
    299      1.45   reinoud 
    300      1.45   reinoud 
    301      1.45   reinoud int
    302      1.45   reinoud udf_synchronise_caches(struct udf_mount *ump)
    303      1.45   reinoud {
    304      1.45   reinoud 	struct mmc_op mmc_op;
    305      1.45   reinoud 
    306      1.45   reinoud 	DPRINTF(CALL, ("udf_synchronise_caches()\n"));
    307      1.45   reinoud 
    308      1.45   reinoud 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
    309      1.45   reinoud 		return 0;
    310      1.45   reinoud 
    311      1.45   reinoud 	/* discs are done now */
    312      1.45   reinoud 	if (ump->discinfo.mmc_class == MMC_CLASS_DISC)
    313      1.45   reinoud 		return 0;
    314      1.45   reinoud 
    315      1.45   reinoud 	bzero(&mmc_op, sizeof(struct mmc_op));
    316      1.45   reinoud 	mmc_op.operation = MMC_OP_SYNCHRONISECACHE;
    317      1.45   reinoud 
    318      1.45   reinoud 	/* ignore return code */
    319      1.45   reinoud 	(void) VOP_IOCTL(ump->devvp, MMCOP, &mmc_op, FKIOCTL, NOCRED);
    320      1.45   reinoud 
    321      1.45   reinoud 	return 0;
    322      1.45   reinoud }
    323      1.45   reinoud 
    324      1.45   reinoud /* --------------------------------------------------------------------- */
    325      1.45   reinoud 
    326      1.45   reinoud /* track/session searching for mounting */
    327      1.45   reinoud int
    328      1.45   reinoud udf_search_tracks(struct udf_mount *ump, struct udf_args *args,
    329      1.45   reinoud 		  int *first_tracknr, int *last_tracknr)
    330      1.45   reinoud {
    331      1.45   reinoud 	struct mmc_trackinfo trackinfo;
    332      1.45   reinoud 	uint32_t tracknr, start_track, num_tracks;
    333       1.1   reinoud 	int error;
    334       1.1   reinoud 
    335       1.1   reinoud 	/* if negative, sessionnr is relative to last session */
    336       1.1   reinoud 	if (args->sessionnr < 0) {
    337       1.1   reinoud 		args->sessionnr += ump->discinfo.num_sessions;
    338       1.9  christos 	}
    339       1.1   reinoud 
    340       1.1   reinoud 	/* sanity */
    341      1.45   reinoud 	if (args->sessionnr < 0)
    342      1.45   reinoud 		args->sessionnr = 0;
    343       1.1   reinoud 	if (args->sessionnr > ump->discinfo.num_sessions)
    344       1.1   reinoud 		args->sessionnr = ump->discinfo.num_sessions;
    345       1.1   reinoud 
    346       1.1   reinoud 	/* search the tracks for this session, zero session nr indicates last */
    347      1.42   reinoud 	if (args->sessionnr == 0)
    348       1.1   reinoud 		args->sessionnr = ump->discinfo.num_sessions;
    349      1.42   reinoud 	if (ump->discinfo.last_session_state == MMC_STATE_EMPTY)
    350      1.42   reinoud 		args->sessionnr--;
    351      1.42   reinoud 
    352      1.45   reinoud 	/* sanity again */
    353      1.45   reinoud 	if (args->sessionnr < 0)
    354      1.45   reinoud 		args->sessionnr = 0;
    355       1.1   reinoud 
    356       1.1   reinoud 	/* search the first and last track of the specified session */
    357       1.1   reinoud 	num_tracks  = ump->discinfo.num_tracks;
    358       1.1   reinoud 	start_track = ump->discinfo.first_track;
    359       1.1   reinoud 
    360       1.1   reinoud 	/* search for first track of this session */
    361       1.1   reinoud 	for (tracknr = start_track; tracknr <= num_tracks; tracknr++) {
    362       1.1   reinoud 		/* get track info */
    363       1.1   reinoud 		trackinfo.tracknr = tracknr;
    364       1.1   reinoud 		error = udf_update_trackinfo(ump, &trackinfo);
    365       1.1   reinoud 		if (error)
    366       1.1   reinoud 			return error;
    367       1.1   reinoud 
    368       1.1   reinoud 		if (trackinfo.sessionnr == args->sessionnr)
    369       1.1   reinoud 			break;
    370       1.1   reinoud 	}
    371       1.1   reinoud 	*first_tracknr = tracknr;
    372       1.1   reinoud 
    373       1.1   reinoud 	/* search for last track of this session */
    374       1.1   reinoud 	for (;tracknr <= num_tracks; tracknr++) {
    375       1.1   reinoud 		/* get track info */
    376       1.1   reinoud 		trackinfo.tracknr = tracknr;
    377       1.1   reinoud 		error = udf_update_trackinfo(ump, &trackinfo);
    378       1.1   reinoud 		if (error || (trackinfo.sessionnr != args->sessionnr)) {
    379       1.1   reinoud 			tracknr--;
    380       1.1   reinoud 			break;
    381       1.9  christos 		}
    382       1.9  christos 	}
    383       1.1   reinoud 	if (tracknr > num_tracks)
    384       1.1   reinoud 		tracknr--;
    385       1.1   reinoud 
    386       1.1   reinoud 	*last_tracknr = tracknr;
    387       1.1   reinoud 
    388      1.45   reinoud 	if (*last_tracknr < *first_tracknr) {
    389      1.45   reinoud 		printf( "udf_search_tracks: sanity check on drive+disc failed, "
    390      1.45   reinoud 			"drive returned garbage\n");
    391      1.45   reinoud 		return EINVAL;
    392      1.45   reinoud 	}
    393      1.45   reinoud 
    394       1.1   reinoud 	assert(*last_tracknr >= *first_tracknr);
    395       1.1   reinoud 	return 0;
    396       1.1   reinoud }
    397       1.1   reinoud 
    398       1.1   reinoud 
    399      1.45   reinoud /*
    400      1.45   reinoud  * NOTE: this is the only routine in this file that directly peeks into the
    401      1.45   reinoud  * metadata file but since its at a larval state of the mount it can't hurt.
    402      1.45   reinoud  *
    403      1.45   reinoud  * XXX candidate for udf_allocation.c
    404      1.45   reinoud  * XXX clean me up!, change to new node reading code.
    405      1.45   reinoud  */
    406      1.45   reinoud 
    407      1.45   reinoud static void
    408      1.45   reinoud udf_check_track_metadata_overlap(struct udf_mount *ump,
    409      1.45   reinoud 	struct mmc_trackinfo *trackinfo)
    410       1.1   reinoud {
    411      1.45   reinoud 	struct part_desc *part;
    412      1.45   reinoud 	struct file_entry      *fe;
    413      1.45   reinoud 	struct extfile_entry   *efe;
    414      1.45   reinoud 	struct short_ad        *s_ad;
    415      1.45   reinoud 	struct long_ad         *l_ad;
    416      1.45   reinoud 	uint32_t track_start, track_end;
    417      1.45   reinoud 	uint32_t phys_part_start, phys_part_end, part_start, part_end;
    418      1.45   reinoud 	uint32_t sector_size, len, alloclen, plb_num;
    419      1.45   reinoud 	uint8_t *pos;
    420      1.45   reinoud 	int addr_type, icblen, icbflags, flags;
    421       1.1   reinoud 
    422      1.45   reinoud 	/* get our track extents */
    423      1.45   reinoud 	track_start = trackinfo->track_start;
    424      1.45   reinoud 	track_end   = track_start + trackinfo->track_size;
    425      1.45   reinoud 
    426      1.45   reinoud 	/* get our base partition extent */
    427      1.45   reinoud 	part = ump->partitions[ump->metadata_part];
    428      1.45   reinoud 	phys_part_start = udf_rw32(part->start_loc);
    429      1.45   reinoud 	phys_part_end   = phys_part_start + udf_rw32(part->part_len);
    430       1.1   reinoud 
    431      1.45   reinoud 	/* no use if its outside the physical partition */
    432      1.45   reinoud 	if ((phys_part_start >= track_end) || (phys_part_end < track_start))
    433      1.45   reinoud 		return;
    434       1.1   reinoud 
    435      1.45   reinoud 	/*
    436      1.45   reinoud 	 * now follow all extents in the fe/efe to see if they refer to this
    437      1.45   reinoud 	 * track
    438      1.45   reinoud 	 */
    439       1.1   reinoud 
    440      1.45   reinoud 	sector_size = ump->discinfo.sector_size;
    441       1.1   reinoud 
    442      1.45   reinoud 	/* XXX should we claim exclusive access to the metafile ? */
    443      1.45   reinoud 	/* TODO: move to new node read code */
    444      1.45   reinoud 	fe  = ump->metadata_node->fe;
    445      1.45   reinoud 	efe = ump->metadata_node->efe;
    446      1.45   reinoud 	if (fe) {
    447      1.45   reinoud 		alloclen = udf_rw32(fe->l_ad);
    448      1.45   reinoud 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
    449      1.45   reinoud 		icbflags = udf_rw16(fe->icbtag.flags);
    450      1.45   reinoud 	} else {
    451      1.45   reinoud 		assert(efe);
    452      1.45   reinoud 		alloclen = udf_rw32(efe->l_ad);
    453      1.45   reinoud 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
    454      1.45   reinoud 		icbflags = udf_rw16(efe->icbtag.flags);
    455       1.9  christos 	}
    456      1.45   reinoud 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
    457      1.45   reinoud 
    458      1.45   reinoud 	while (alloclen) {
    459      1.45   reinoud 		if (addr_type == UDF_ICB_SHORT_ALLOC) {
    460      1.45   reinoud 			icblen = sizeof(struct short_ad);
    461      1.45   reinoud 			s_ad   = (struct short_ad *) pos;
    462      1.45   reinoud 			len        = udf_rw32(s_ad->len);
    463      1.45   reinoud 			plb_num    = udf_rw32(s_ad->lb_num);
    464      1.45   reinoud 		} else {
    465      1.45   reinoud 			/* should not be present, but why not */
    466      1.45   reinoud 			icblen = sizeof(struct long_ad);
    467      1.45   reinoud 			l_ad   = (struct long_ad *) pos;
    468      1.45   reinoud 			len        = udf_rw32(l_ad->len);
    469      1.45   reinoud 			plb_num    = udf_rw32(l_ad->loc.lb_num);
    470      1.45   reinoud 			/* pvpart_num = udf_rw16(l_ad->loc.part_num); */
    471      1.45   reinoud 		}
    472      1.45   reinoud 		/* process extent */
    473      1.45   reinoud 		flags   = UDF_EXT_FLAGS(len);
    474      1.45   reinoud 		len     = UDF_EXT_LEN(len);
    475       1.1   reinoud 
    476      1.45   reinoud 		part_start = phys_part_start + plb_num;
    477      1.45   reinoud 		part_end   = part_start + (len / sector_size);
    478       1.1   reinoud 
    479      1.45   reinoud 		if ((part_start >= track_start) && (part_end <= track_end)) {
    480      1.45   reinoud 			/* extent is enclosed within this track */
    481      1.45   reinoud 			ump->metadata_track = *trackinfo;
    482      1.45   reinoud 			return;
    483      1.45   reinoud 		}
    484       1.1   reinoud 
    485      1.45   reinoud 		pos        += icblen;
    486      1.45   reinoud 		alloclen   -= icblen;
    487       1.9  christos 	}
    488      1.45   reinoud }
    489      1.45   reinoud 
    490       1.1   reinoud 
    491      1.45   reinoud int
    492      1.45   reinoud udf_search_writing_tracks(struct udf_mount *ump)
    493      1.45   reinoud {
    494      1.45   reinoud 	struct mmc_trackinfo trackinfo;
    495      1.45   reinoud 	struct part_desc *part;
    496      1.45   reinoud 	uint32_t tracknr, start_track, num_tracks;
    497      1.45   reinoud 	uint32_t track_start, track_end, part_start, part_end;
    498      1.45   reinoud 	int error;
    499       1.1   reinoud 
    500      1.45   reinoud 	/*
    501      1.45   reinoud 	 * in the CD/(HD)DVD/BD recordable device model a few tracks within
    502      1.45   reinoud 	 * the last session might be open but in the UDF device model at most
    503      1.45   reinoud 	 * three tracks can be open: a reserved track for delayed ISO VRS
    504      1.45   reinoud 	 * writing, a data track and a metadata track. We search here for the
    505      1.45   reinoud 	 * data track and the metadata track. Note that the reserved track is
    506      1.45   reinoud 	 * troublesome but can be detected by its small size of < 512 sectors.
    507      1.45   reinoud 	 */
    508       1.1   reinoud 
    509      1.45   reinoud 	num_tracks  = ump->discinfo.num_tracks;
    510      1.45   reinoud 	start_track = ump->discinfo.first_track;
    511       1.1   reinoud 
    512      1.45   reinoud 	/* fetch info on first and possibly only track */
    513      1.45   reinoud 	trackinfo.tracknr = start_track;
    514      1.45   reinoud 	error = udf_update_trackinfo(ump, &trackinfo);
    515      1.45   reinoud 	if (error)
    516      1.45   reinoud 		return error;
    517      1.13   reinoud 
    518      1.45   reinoud 	/* copy results to our mount point */
    519      1.45   reinoud 	ump->data_track     = trackinfo;
    520      1.45   reinoud 	ump->metadata_track = trackinfo;
    521       1.1   reinoud 
    522      1.45   reinoud 	/* if not sequential, we're done */
    523      1.45   reinoud 	if (num_tracks == 1)
    524      1.45   reinoud 		return 0;
    525       1.1   reinoud 
    526      1.45   reinoud 	for (tracknr = start_track;tracknr <= num_tracks; tracknr++) {
    527      1.45   reinoud 		/* get track info */
    528      1.45   reinoud 		trackinfo.tracknr = tracknr;
    529      1.45   reinoud 		error = udf_update_trackinfo(ump, &trackinfo);
    530      1.45   reinoud 		if (error)
    531      1.45   reinoud 			return error;
    532       1.1   reinoud 
    533      1.45   reinoud 		if ((trackinfo.flags & MMC_TRACKINFO_NWA_VALID) == 0)
    534      1.45   reinoud 			continue;
    535      1.45   reinoud 
    536      1.45   reinoud 		track_start = trackinfo.track_start;
    537      1.45   reinoud 		track_end   = track_start + trackinfo.track_size;
    538       1.1   reinoud 
    539      1.45   reinoud 		/* check for overlap on data partition */
    540      1.45   reinoud 		part = ump->partitions[ump->data_part];
    541      1.45   reinoud 		part_start = udf_rw32(part->start_loc);
    542      1.45   reinoud 		part_end   = part_start + udf_rw32(part->part_len);
    543      1.45   reinoud 		if ((part_start < track_end) && (part_end > track_start)) {
    544      1.45   reinoud 			ump->data_track = trackinfo;
    545      1.45   reinoud 			/* TODO check if UDF partition data_part is writable */
    546      1.45   reinoud 		}
    547      1.45   reinoud 
    548      1.45   reinoud 		/* check for overlap on metadata partition */
    549      1.45   reinoud 		if ((ump->meta_alloc == UDF_ALLOC_METASEQUENTIAL) ||
    550      1.45   reinoud 		    (ump->meta_alloc == UDF_ALLOC_METABITMAP)) {
    551      1.45   reinoud 			udf_check_track_metadata_overlap(ump, &trackinfo);
    552      1.45   reinoud 		} else {
    553      1.45   reinoud 			ump->metadata_track = trackinfo;
    554       1.9  christos 		}
    555      1.45   reinoud 	}
    556       1.1   reinoud 
    557      1.45   reinoud 	if ((ump->data_track.flags & MMC_TRACKINFO_NWA_VALID) == 0)
    558      1.45   reinoud 		return EROFS;
    559       1.1   reinoud 
    560      1.45   reinoud 	if ((ump->metadata_track.flags & MMC_TRACKINFO_NWA_VALID) == 0)
    561      1.45   reinoud 		return EROFS;
    562       1.1   reinoud 
    563       1.1   reinoud 	return 0;
    564       1.1   reinoud }
    565       1.1   reinoud 
    566       1.1   reinoud /* --------------------------------------------------------------------- */
    567       1.1   reinoud 
    568      1.45   reinoud /*
    569      1.45   reinoud  * Check if the blob starts with a good UDF tag. Tags are protected by a
    570      1.45   reinoud  * checksum over the reader except one byte at position 4 that is the checksum
    571      1.45   reinoud  * itself.
    572      1.45   reinoud  */
    573      1.45   reinoud 
    574      1.45   reinoud int
    575      1.45   reinoud udf_check_tag(void *blob)
    576       1.1   reinoud {
    577      1.45   reinoud 	struct desc_tag *tag = blob;
    578      1.45   reinoud 	uint8_t *pos, sum, cnt;
    579       1.1   reinoud 
    580      1.45   reinoud 	/* check TAG header checksum */
    581      1.45   reinoud 	pos = (uint8_t *) tag;
    582      1.45   reinoud 	sum = 0;
    583      1.45   reinoud 
    584      1.45   reinoud 	for(cnt = 0; cnt < 16; cnt++) {
    585      1.45   reinoud 		if (cnt != 4)
    586      1.45   reinoud 			sum += *pos;
    587      1.45   reinoud 		pos++;
    588      1.45   reinoud 	}
    589      1.45   reinoud 	if (sum != tag->cksum) {
    590      1.45   reinoud 		/* bad tag header checksum; this is not a valid tag */
    591      1.45   reinoud 		return EINVAL;
    592      1.45   reinoud 	}
    593      1.45   reinoud 
    594      1.45   reinoud 	return 0;
    595      1.45   reinoud }
    596      1.45   reinoud 
    597      1.45   reinoud 
    598      1.45   reinoud /*
    599      1.45   reinoud  * check tag payload will check descriptor CRC as specified.
    600      1.45   reinoud  * If the descriptor is too long, it will return EIO otherwise EINVAL.
    601      1.45   reinoud  */
    602       1.1   reinoud 
    603      1.45   reinoud int
    604      1.45   reinoud udf_check_tag_payload(void *blob, uint32_t max_length)
    605      1.45   reinoud {
    606      1.45   reinoud 	struct desc_tag *tag = blob;
    607      1.45   reinoud 	uint16_t crc, crc_len;
    608       1.1   reinoud 
    609      1.45   reinoud 	crc_len = udf_rw16(tag->desc_crc_len);
    610       1.1   reinoud 
    611      1.45   reinoud 	/* check payload CRC if applicable */
    612      1.45   reinoud 	if (crc_len == 0)
    613      1.45   reinoud 		return 0;
    614       1.1   reinoud 
    615      1.45   reinoud 	if (crc_len > max_length)
    616      1.45   reinoud 		return EIO;
    617       1.1   reinoud 
    618      1.45   reinoud 	crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len);
    619      1.45   reinoud 	if (crc != udf_rw16(tag->desc_crc)) {
    620      1.45   reinoud 		/* bad payload CRC; this is a broken tag */
    621      1.45   reinoud 		return EINVAL;
    622       1.9  christos 	}
    623       1.1   reinoud 
    624      1.45   reinoud 	return 0;
    625       1.1   reinoud }
    626       1.1   reinoud 
    627       1.1   reinoud 
    628      1.45   reinoud void
    629      1.45   reinoud udf_validate_tag_sum(void *blob)
    630       1.1   reinoud {
    631      1.45   reinoud 	struct desc_tag *tag = blob;
    632      1.45   reinoud 	uint8_t *pos, sum, cnt;
    633       1.1   reinoud 
    634      1.45   reinoud 	/* calculate TAG header checksum */
    635      1.45   reinoud 	pos = (uint8_t *) tag;
    636      1.45   reinoud 	sum = 0;
    637       1.1   reinoud 
    638      1.45   reinoud 	for(cnt = 0; cnt < 16; cnt++) {
    639      1.45   reinoud 		if (cnt != 4) sum += *pos;
    640      1.45   reinoud 		pos++;
    641      1.45   reinoud 	}
    642      1.45   reinoud 	tag->cksum = sum;	/* 8 bit */
    643      1.45   reinoud }
    644       1.1   reinoud 
    645       1.1   reinoud 
    646      1.45   reinoud /* assumes sector number of descriptor to be saved already present */
    647      1.45   reinoud void
    648      1.45   reinoud udf_validate_tag_and_crc_sums(void *blob)
    649      1.45   reinoud {
    650      1.45   reinoud 	struct desc_tag *tag  = blob;
    651      1.45   reinoud 	uint8_t         *btag = (uint8_t *) tag;
    652      1.45   reinoud 	uint16_t crc, crc_len;
    653       1.1   reinoud 
    654      1.45   reinoud 	crc_len = udf_rw16(tag->desc_crc_len);
    655       1.1   reinoud 
    656      1.45   reinoud 	/* check payload CRC if applicable */
    657      1.45   reinoud 	if (crc_len > 0) {
    658      1.45   reinoud 		crc = udf_cksum(btag + UDF_DESC_TAG_LENGTH, crc_len);
    659      1.45   reinoud 		tag->desc_crc = udf_rw16(crc);
    660       1.9  christos 	}
    661       1.1   reinoud 
    662      1.45   reinoud 	/* calculate TAG header checksum */
    663      1.45   reinoud 	udf_validate_tag_sum(blob);
    664       1.1   reinoud }
    665       1.1   reinoud 
    666       1.1   reinoud /* --------------------------------------------------------------------- */
    667       1.1   reinoud 
    668       1.1   reinoud /*
    669      1.45   reinoud  * XXX note the different semantics from udfclient: for FIDs it still rounds
    670      1.45   reinoud  * up to sectors. Use udf_fidsize() for a correct length.
    671       1.1   reinoud  */
    672       1.1   reinoud 
    673      1.45   reinoud int
    674      1.45   reinoud udf_tagsize(union dscrptr *dscr, uint32_t lb_size)
    675       1.1   reinoud {
    676      1.45   reinoud 	uint32_t size, tag_id, num_lb, elmsz;
    677       1.1   reinoud 
    678      1.45   reinoud 	tag_id = udf_rw16(dscr->tag.id);
    679       1.1   reinoud 
    680      1.45   reinoud 	switch (tag_id) {
    681      1.45   reinoud 	case TAGID_LOGVOL :
    682      1.45   reinoud 		size  = sizeof(struct logvol_desc) - 1;
    683      1.45   reinoud 		size += udf_rw32(dscr->lvd.mt_l);
    684      1.45   reinoud 		break;
    685      1.45   reinoud 	case TAGID_UNALLOC_SPACE :
    686      1.45   reinoud 		elmsz = sizeof(struct extent_ad);
    687      1.45   reinoud 		size  = sizeof(struct unalloc_sp_desc) - elmsz;
    688      1.45   reinoud 		size += udf_rw32(dscr->usd.alloc_desc_num) * elmsz;
    689      1.45   reinoud 		break;
    690      1.45   reinoud 	case TAGID_FID :
    691      1.45   reinoud 		size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu);
    692      1.45   reinoud 		size = (size + 3) & ~3;
    693      1.45   reinoud 		break;
    694      1.45   reinoud 	case TAGID_LOGVOL_INTEGRITY :
    695      1.45   reinoud 		size  = sizeof(struct logvol_int_desc) - sizeof(uint32_t);
    696      1.45   reinoud 		size += udf_rw32(dscr->lvid.l_iu);
    697      1.45   reinoud 		size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t));
    698      1.45   reinoud 		break;
    699      1.45   reinoud 	case TAGID_SPACE_BITMAP :
    700      1.45   reinoud 		size  = sizeof(struct space_bitmap_desc) - 1;
    701      1.45   reinoud 		size += udf_rw32(dscr->sbd.num_bytes);
    702      1.45   reinoud 		break;
    703      1.45   reinoud 	case TAGID_SPARING_TABLE :
    704      1.45   reinoud 		elmsz = sizeof(struct spare_map_entry);
    705      1.45   reinoud 		size  = sizeof(struct udf_sparing_table) - elmsz;
    706      1.45   reinoud 		size += udf_rw16(dscr->spt.rt_l) * elmsz;
    707      1.45   reinoud 		break;
    708      1.45   reinoud 	case TAGID_FENTRY :
    709      1.45   reinoud 		size  = sizeof(struct file_entry);
    710      1.45   reinoud 		size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1;
    711      1.45   reinoud 		break;
    712      1.45   reinoud 	case TAGID_EXTFENTRY :
    713      1.45   reinoud 		size  = sizeof(struct extfile_entry);
    714      1.45   reinoud 		size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1;
    715      1.45   reinoud 		break;
    716      1.45   reinoud 	case TAGID_FSD :
    717      1.45   reinoud 		size  = sizeof(struct fileset_desc);
    718      1.45   reinoud 		break;
    719      1.45   reinoud 	default :
    720      1.45   reinoud 		size = sizeof(union dscrptr);
    721      1.45   reinoud 		break;
    722      1.45   reinoud 	}
    723       1.1   reinoud 
    724      1.45   reinoud 	if ((size == 0) || (lb_size == 0)) return 0;
    725       1.1   reinoud 
    726      1.45   reinoud 	/* round up in sectors */
    727      1.45   reinoud 	num_lb = (size + lb_size -1) / lb_size;
    728      1.45   reinoud 	return num_lb * lb_size;
    729      1.45   reinoud }
    730       1.1   reinoud 
    731       1.1   reinoud 
    732      1.45   reinoud int
    733      1.45   reinoud udf_fidsize(struct fileid_desc *fid)
    734      1.45   reinoud {
    735      1.45   reinoud 	uint32_t size;
    736       1.3   reinoud 
    737      1.45   reinoud 	if (udf_rw16(fid->tag.id) != TAGID_FID)
    738      1.45   reinoud 		panic("got udf_fidsize on non FID\n");
    739       1.1   reinoud 
    740      1.45   reinoud 	size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu);
    741      1.45   reinoud 	size = (size + 3) & ~3;
    742       1.1   reinoud 
    743      1.45   reinoud 	return size;
    744       1.1   reinoud }
    745       1.1   reinoud 
    746       1.1   reinoud /* --------------------------------------------------------------------- */
    747       1.1   reinoud 
    748      1.45   reinoud void
    749      1.45   reinoud udf_lock_node(struct udf_node *udf_node, int flag, char const *fname, const int lineno)
    750      1.45   reinoud {
    751      1.45   reinoud 	int ret;
    752      1.45   reinoud 
    753      1.45   reinoud 	mutex_enter(&udf_node->node_mutex);
    754      1.45   reinoud 	/* wait until free */
    755      1.45   reinoud 	while (udf_node->i_flags & IN_LOCKED) {
    756      1.45   reinoud 		ret = cv_timedwait(&udf_node->node_lock, &udf_node->node_mutex, hz/8);
    757      1.45   reinoud 		/* TODO check if we should return error; abort */
    758      1.45   reinoud 		if (ret == EWOULDBLOCK) {
    759      1.45   reinoud 			DPRINTF(LOCKING, ( "udf_lock_node: udf_node %p would block "
    760      1.45   reinoud 				"wanted at %s:%d, previously locked at %s:%d\n",
    761      1.45   reinoud 				udf_node, fname, lineno,
    762      1.45   reinoud 				udf_node->lock_fname, udf_node->lock_lineno));
    763      1.45   reinoud 		}
    764      1.45   reinoud 	}
    765      1.45   reinoud 	/* grab */
    766      1.45   reinoud 	udf_node->i_flags |= IN_LOCKED | flag;
    767      1.45   reinoud 	/* debug */
    768      1.45   reinoud 	udf_node->lock_fname  = fname;
    769      1.45   reinoud 	udf_node->lock_lineno = lineno;
    770      1.45   reinoud 
    771      1.45   reinoud 	mutex_exit(&udf_node->node_mutex);
    772      1.45   reinoud }
    773      1.45   reinoud 
    774       1.1   reinoud 
    775      1.45   reinoud void
    776      1.45   reinoud udf_unlock_node(struct udf_node *udf_node, int flag)
    777      1.22  christos {
    778      1.45   reinoud 	mutex_enter(&udf_node->node_mutex);
    779      1.45   reinoud 	udf_node->i_flags &= ~(IN_LOCKED | flag);
    780      1.45   reinoud 	cv_broadcast(&udf_node->node_lock);
    781      1.45   reinoud 	mutex_exit(&udf_node->node_mutex);
    782      1.45   reinoud }
    783       1.1   reinoud 
    784       1.1   reinoud 
    785      1.45   reinoud /* --------------------------------------------------------------------- */
    786       1.1   reinoud 
    787      1.45   reinoud static int
    788      1.45   reinoud udf_read_anchor(struct udf_mount *ump, uint32_t sector, struct anchor_vdp **dst)
    789      1.45   reinoud {
    790      1.45   reinoud 	int error;
    791      1.45   reinoud 
    792      1.45   reinoud 	error = udf_read_phys_dscr(ump, sector, M_UDFVOLD,
    793      1.45   reinoud 			(union dscrptr **) dst);
    794      1.45   reinoud 	if (!error) {
    795      1.45   reinoud 		/* blank terminator blocks are not allowed here */
    796      1.45   reinoud 		if (*dst == NULL)
    797      1.45   reinoud 			return ENOENT;
    798      1.45   reinoud 		if (udf_rw16((*dst)->tag.id) != TAGID_ANCHOR) {
    799      1.45   reinoud 			error = ENOENT;
    800      1.45   reinoud 			free(*dst, M_UDFVOLD);
    801      1.45   reinoud 			*dst = NULL;
    802      1.45   reinoud 			DPRINTF(VOLUMES, ("Not an anchor\n"));
    803      1.45   reinoud 		}
    804      1.45   reinoud 	}
    805      1.45   reinoud 
    806      1.45   reinoud 	return error;
    807      1.45   reinoud }
    808      1.45   reinoud 
    809      1.45   reinoud 
    810      1.45   reinoud int
    811      1.45   reinoud udf_read_anchors(struct udf_mount *ump)
    812      1.45   reinoud {
    813      1.45   reinoud 	struct udf_args *args = &ump->mount_args;
    814      1.45   reinoud 	struct mmc_trackinfo first_track;
    815      1.45   reinoud 	struct mmc_trackinfo second_track;
    816      1.45   reinoud 	struct mmc_trackinfo last_track;
    817      1.45   reinoud 	struct anchor_vdp **anchorsp;
    818      1.45   reinoud 	uint32_t track_start;
    819      1.45   reinoud 	uint32_t track_end;
    820      1.45   reinoud 	uint32_t positions[4];
    821      1.45   reinoud 	int first_tracknr, last_tracknr;
    822      1.45   reinoud 	int error, anch, ok, first_anchor;
    823      1.45   reinoud 
    824      1.45   reinoud 	/* search the first and last track of the specified session */
    825      1.45   reinoud 	error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr);
    826      1.45   reinoud 	if (!error) {
    827      1.45   reinoud 		first_track.tracknr = first_tracknr;
    828      1.45   reinoud 		error = udf_update_trackinfo(ump, &first_track);
    829      1.45   reinoud 	}
    830      1.45   reinoud 	if (!error) {
    831      1.45   reinoud 		last_track.tracknr = last_tracknr;
    832      1.45   reinoud 		error = udf_update_trackinfo(ump, &last_track);
    833      1.45   reinoud 	}
    834      1.45   reinoud 	if ((!error) && (first_tracknr != last_tracknr)) {
    835      1.45   reinoud 		second_track.tracknr = first_tracknr+1;
    836      1.45   reinoud 		error = udf_update_trackinfo(ump, &second_track);
    837      1.45   reinoud 	}
    838      1.45   reinoud 	if (error) {
    839      1.45   reinoud 		printf("UDF mount: reading disc geometry failed\n");
    840      1.45   reinoud 		return 0;
    841      1.45   reinoud 	}
    842      1.45   reinoud 
    843      1.45   reinoud 	track_start = first_track.track_start;
    844       1.1   reinoud 
    845      1.45   reinoud 	/* `end' is not as straitforward as start. */
    846      1.45   reinoud 	track_end =   last_track.track_start
    847      1.45   reinoud 		    + last_track.track_size - last_track.free_blocks - 1;
    848       1.1   reinoud 
    849      1.45   reinoud 	if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
    850      1.45   reinoud 		/* end of track is not straitforward here */
    851      1.45   reinoud 		if (last_track.flags & MMC_TRACKINFO_LRA_VALID)
    852      1.45   reinoud 			track_end = last_track.last_recorded;
    853      1.45   reinoud 		else if (last_track.flags & MMC_TRACKINFO_NWA_VALID)
    854      1.45   reinoud 			track_end = last_track.next_writable
    855      1.45   reinoud 				    - ump->discinfo.link_block_penalty;
    856       1.9  christos 	}
    857       1.1   reinoud 
    858      1.45   reinoud 	/* its no use reading a blank track */
    859      1.45   reinoud 	first_anchor = 0;
    860      1.45   reinoud 	if (first_track.flags & MMC_TRACKINFO_BLANK)
    861      1.45   reinoud 		first_anchor = 1;
    862      1.45   reinoud 
    863      1.45   reinoud 	/* get our packet size */
    864      1.45   reinoud 	ump->packet_size = first_track.packet_size;
    865      1.45   reinoud 	if (first_track.flags & MMC_TRACKINFO_BLANK)
    866      1.45   reinoud 		ump->packet_size = second_track.packet_size;
    867      1.45   reinoud 
    868      1.45   reinoud 	if (ump->packet_size <= 1) {
    869      1.45   reinoud 		/* take max, but not bigger than 64 */
    870      1.45   reinoud 		ump->packet_size = MAXPHYS / ump->discinfo.sector_size;
    871      1.45   reinoud 		ump->packet_size = MIN(ump->packet_size, 64);
    872       1.9  christos 	}
    873      1.45   reinoud 	KASSERT(ump->packet_size >= 1);
    874       1.1   reinoud 
    875      1.45   reinoud 	/* read anchors start+256, start+512, end-256, end */
    876      1.45   reinoud 	positions[0] = track_start+256;
    877      1.45   reinoud 	positions[1] =   track_end-256;
    878      1.45   reinoud 	positions[2] =   track_end;
    879      1.45   reinoud 	positions[3] = track_start+512;	/* [UDF 2.60/6.11.2] */
    880      1.45   reinoud 	/* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */
    881       1.1   reinoud 
    882      1.45   reinoud 	ok = 0;
    883      1.45   reinoud 	anchorsp = ump->anchors;
    884      1.45   reinoud 	for (anch = first_anchor; anch < 4; anch++) {
    885      1.45   reinoud 		DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch,
    886      1.45   reinoud 		    positions[anch]));
    887      1.45   reinoud 		error = udf_read_anchor(ump, positions[anch], anchorsp);
    888      1.45   reinoud 		if (!error) {
    889      1.45   reinoud 			anchorsp++;
    890      1.45   reinoud 			ok++;
    891      1.45   reinoud 		}
    892      1.45   reinoud 	}
    893       1.1   reinoud 
    894      1.45   reinoud 	/* VATs are only recorded on sequential media, but initialise */
    895      1.45   reinoud 	ump->first_possible_vat_location = track_start + 2;
    896      1.45   reinoud 	ump->last_possible_vat_location  = track_end + last_track.packet_size;
    897       1.1   reinoud 
    898      1.45   reinoud 	return ok;
    899      1.45   reinoud }
    900       1.1   reinoud 
    901      1.45   reinoud /* --------------------------------------------------------------------- */
    902       1.1   reinoud 
    903      1.45   reinoud /* we dont try to be smart; we just record the parts */
    904      1.45   reinoud #define UDF_UPDATE_DSCR(name, dscr) \
    905      1.45   reinoud 	if (name) \
    906      1.45   reinoud 		free(name, M_UDFVOLD); \
    907      1.45   reinoud 	name = dscr;
    908       1.1   reinoud 
    909      1.45   reinoud static int
    910      1.45   reinoud udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr)
    911      1.45   reinoud {
    912      1.45   reinoud 	struct part_desc *part;
    913      1.45   reinoud 	uint16_t phys_part, raw_phys_part;
    914       1.1   reinoud 
    915      1.45   reinoud 	DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n",
    916      1.45   reinoud 	    udf_rw16(dscr->tag.id)));
    917      1.45   reinoud 	switch (udf_rw16(dscr->tag.id)) {
    918      1.45   reinoud 	case TAGID_PRI_VOL :		/* primary partition		*/
    919      1.45   reinoud 		UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd);
    920      1.45   reinoud 		break;
    921      1.45   reinoud 	case TAGID_LOGVOL :		/* logical volume		*/
    922      1.45   reinoud 		UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd);
    923      1.45   reinoud 		break;
    924      1.45   reinoud 	case TAGID_UNALLOC_SPACE :	/* unallocated space		*/
    925      1.45   reinoud 		UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd);
    926      1.45   reinoud 		break;
    927      1.45   reinoud 	case TAGID_IMP_VOL :		/* implementation		*/
    928      1.45   reinoud 		/* XXX do we care about multiple impl. descr ? */
    929      1.45   reinoud 		UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd);
    930      1.45   reinoud 		break;
    931      1.45   reinoud 	case TAGID_PARTITION :		/* physical partition		*/
    932      1.45   reinoud 		/* not much use if its not allocated */
    933      1.45   reinoud 		if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) {
    934      1.45   reinoud 			free(dscr, M_UDFVOLD);
    935       1.1   reinoud 			break;
    936       1.9  christos 		}
    937       1.1   reinoud 
    938      1.33   reinoud 		/*
    939      1.33   reinoud 		 * BUGALERT: some rogue implementations use random physical
    940      1.33   reinoud 		 * partion numbers to break other implementations so lookup
    941      1.33   reinoud 		 * the number.
    942      1.33   reinoud 		 */
    943      1.45   reinoud 		raw_phys_part = udf_rw16(dscr->pd.part_num);
    944      1.33   reinoud 		for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
    945      1.33   reinoud 			part = ump->partitions[phys_part];
    946      1.33   reinoud 			if (part == NULL)
    947      1.45   reinoud 				break;
    948      1.33   reinoud 			if (udf_rw16(part->part_num) == raw_phys_part)
    949      1.33   reinoud 				break;
    950      1.33   reinoud 		}
    951      1.45   reinoud 		if (phys_part == UDF_PARTITIONS) {
    952      1.45   reinoud 			free(dscr, M_UDFVOLD);
    953       1.1   reinoud 			return EINVAL;
    954      1.45   reinoud 		}
    955       1.1   reinoud 
    956      1.45   reinoud 		UDF_UPDATE_DSCR(ump->partitions[phys_part], &dscr->pd);
    957      1.45   reinoud 		break;
    958      1.45   reinoud 	case TAGID_VOL :		/* volume space extender; rare	*/
    959      1.45   reinoud 		DPRINTF(VOLUMES, ("VDS extender ignored\n"));
    960      1.45   reinoud 		free(dscr, M_UDFVOLD);
    961      1.45   reinoud 		break;
    962      1.45   reinoud 	default :
    963      1.45   reinoud 		DPRINTF(VOLUMES, ("Unhandled VDS type %d\n",
    964      1.45   reinoud 		    udf_rw16(dscr->tag.id)));
    965      1.45   reinoud 		free(dscr, M_UDFVOLD);
    966       1.9  christos 	}
    967       1.1   reinoud 
    968      1.45   reinoud 	return 0;
    969      1.45   reinoud }
    970      1.45   reinoud #undef UDF_UPDATE_DSCR
    971       1.1   reinoud 
    972      1.45   reinoud /* --------------------------------------------------------------------- */
    973       1.1   reinoud 
    974      1.45   reinoud static int
    975      1.45   reinoud udf_read_vds_extent(struct udf_mount *ump, uint32_t loc, uint32_t len)
    976      1.45   reinoud {
    977      1.45   reinoud 	union dscrptr *dscr;
    978      1.45   reinoud 	uint32_t sector_size, dscr_size;
    979      1.45   reinoud 	int error;
    980       1.1   reinoud 
    981      1.45   reinoud 	sector_size = ump->discinfo.sector_size;
    982       1.1   reinoud 
    983      1.45   reinoud 	/* loc is sectornr, len is in bytes */
    984      1.45   reinoud 	error = EIO;
    985      1.45   reinoud 	while (len) {
    986      1.45   reinoud 		error = udf_read_phys_dscr(ump, loc, M_UDFVOLD, &dscr);
    987      1.45   reinoud 		if (error)
    988      1.45   reinoud 			return error;
    989      1.45   reinoud 
    990      1.45   reinoud 		/* blank block is a terminator */
    991      1.45   reinoud 		if (dscr == NULL)
    992      1.45   reinoud 			return 0;
    993      1.45   reinoud 
    994      1.45   reinoud 		/* TERM descriptor is a terminator */
    995      1.45   reinoud 		if (udf_rw16(dscr->tag.id) == TAGID_TERM) {
    996      1.45   reinoud 			free(dscr, M_UDFVOLD);
    997      1.45   reinoud 			return 0;
    998      1.45   reinoud 		}
    999      1.45   reinoud 
   1000      1.45   reinoud 		/* process all others */
   1001      1.45   reinoud 		dscr_size = udf_tagsize(dscr, sector_size);
   1002      1.45   reinoud 		error = udf_process_vds_descriptor(ump, dscr);
   1003      1.45   reinoud 		if (error) {
   1004      1.45   reinoud 			free(dscr, M_UDFVOLD);
   1005      1.45   reinoud 			break;
   1006      1.45   reinoud 		}
   1007      1.45   reinoud 		assert((dscr_size % sector_size) == 0);
   1008      1.45   reinoud 
   1009      1.45   reinoud 		len -= dscr_size;
   1010      1.45   reinoud 		loc += dscr_size / sector_size;
   1011      1.45   reinoud 	}
   1012      1.45   reinoud 
   1013      1.45   reinoud 	return error;
   1014      1.45   reinoud }
   1015      1.45   reinoud 
   1016      1.45   reinoud 
   1017      1.45   reinoud int
   1018      1.45   reinoud udf_read_vds_space(struct udf_mount *ump)
   1019      1.45   reinoud {
   1020      1.45   reinoud 	/* struct udf_args *args = &ump->mount_args; */
   1021      1.45   reinoud 	struct anchor_vdp *anchor, *anchor2;
   1022      1.45   reinoud 	size_t size;
   1023      1.45   reinoud 	uint32_t main_loc, main_len;
   1024      1.45   reinoud 	uint32_t reserve_loc, reserve_len;
   1025      1.45   reinoud 	int error;
   1026      1.45   reinoud 
   1027      1.45   reinoud 	/*
   1028      1.45   reinoud 	 * read in VDS space provided by the anchors; if one descriptor read
   1029      1.45   reinoud 	 * fails, try the mirror sector.
   1030      1.45   reinoud 	 *
   1031      1.45   reinoud 	 * check if 2nd anchor is different from 1st; if so, go for 2nd. This
   1032      1.45   reinoud 	 * avoids the `compatibility features' of DirectCD that may confuse
   1033      1.45   reinoud 	 * stuff completely.
   1034      1.45   reinoud 	 */
   1035      1.45   reinoud 
   1036      1.45   reinoud 	anchor  = ump->anchors[0];
   1037      1.45   reinoud 	anchor2 = ump->anchors[1];
   1038      1.45   reinoud 	assert(anchor);
   1039      1.45   reinoud 
   1040      1.45   reinoud 	if (anchor2) {
   1041      1.45   reinoud 		size = sizeof(struct extent_ad);
   1042      1.45   reinoud 		if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size))
   1043      1.45   reinoud 			anchor = anchor2;
   1044      1.45   reinoud 		/* reserve is specified to be a literal copy of main */
   1045      1.45   reinoud 	}
   1046      1.45   reinoud 
   1047      1.45   reinoud 	main_loc    = udf_rw32(anchor->main_vds_ex.loc);
   1048      1.45   reinoud 	main_len    = udf_rw32(anchor->main_vds_ex.len);
   1049      1.45   reinoud 
   1050      1.45   reinoud 	reserve_loc = udf_rw32(anchor->reserve_vds_ex.loc);
   1051      1.45   reinoud 	reserve_len = udf_rw32(anchor->reserve_vds_ex.len);
   1052      1.45   reinoud 
   1053      1.45   reinoud 	error = udf_read_vds_extent(ump, main_loc, main_len);
   1054      1.45   reinoud 	if (error) {
   1055      1.45   reinoud 		printf("UDF mount: reading in reserve VDS extent\n");
   1056      1.45   reinoud 		error = udf_read_vds_extent(ump, reserve_loc, reserve_len);
   1057      1.45   reinoud 	}
   1058      1.45   reinoud 
   1059      1.45   reinoud 	return error;
   1060      1.45   reinoud }
   1061      1.45   reinoud 
   1062      1.45   reinoud /* --------------------------------------------------------------------- */
   1063      1.45   reinoud 
   1064      1.45   reinoud /*
   1065      1.45   reinoud  * Read in the logical volume integrity sequence pointed to by our logical
   1066      1.45   reinoud  * volume descriptor. Its a sequence that can be extended using fields in the
   1067      1.45   reinoud  * integrity descriptor itself. On sequential media only one is found, on
   1068      1.45   reinoud  * rewritable media a sequence of descriptors can be found as a form of
   1069      1.45   reinoud  * history keeping and on non sequential write-once media the chain is vital
   1070      1.45   reinoud  * to allow more and more descriptors to be written. The last descriptor
   1071      1.45   reinoud  * written in an extent needs to claim space for a new extent.
   1072      1.45   reinoud  */
   1073      1.45   reinoud 
   1074      1.45   reinoud static int
   1075      1.45   reinoud udf_retrieve_lvint(struct udf_mount *ump)
   1076      1.45   reinoud {
   1077      1.45   reinoud 	union dscrptr *dscr;
   1078      1.45   reinoud 	struct logvol_int_desc *lvint;
   1079      1.45   reinoud 	struct udf_lvintq *trace;
   1080      1.45   reinoud 	uint32_t lb_size, lbnum, len;
   1081      1.45   reinoud 	int dscr_type, error, trace_len;
   1082      1.45   reinoud 
   1083      1.45   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   1084      1.45   reinoud 	len     = udf_rw32(ump->logical_vol->integrity_seq_loc.len);
   1085      1.45   reinoud 	lbnum   = udf_rw32(ump->logical_vol->integrity_seq_loc.loc);
   1086      1.45   reinoud 
   1087      1.45   reinoud 	/* clean trace */
   1088      1.45   reinoud 	memset(ump->lvint_trace, 0,
   1089      1.45   reinoud 	    UDF_LVDINT_SEGMENTS * sizeof(struct udf_lvintq));
   1090      1.45   reinoud 
   1091      1.45   reinoud 	trace_len    = 0;
   1092      1.45   reinoud 	trace        = ump->lvint_trace;
   1093      1.45   reinoud 	trace->start = lbnum;
   1094      1.45   reinoud 	trace->end   = lbnum + len/lb_size;
   1095      1.45   reinoud 	trace->pos   = 0;
   1096      1.45   reinoud 	trace->wpos  = 0;
   1097      1.45   reinoud 
   1098      1.45   reinoud 	lvint = NULL;
   1099      1.45   reinoud 	dscr  = NULL;
   1100      1.45   reinoud 	error = 0;
   1101      1.45   reinoud 	while (len) {
   1102      1.45   reinoud 		trace->pos  = lbnum - trace->start;
   1103      1.45   reinoud 		trace->wpos = trace->pos + 1;
   1104      1.45   reinoud 
   1105      1.45   reinoud 		/* read in our integrity descriptor */
   1106      1.45   reinoud 		error = udf_read_phys_dscr(ump, lbnum, M_UDFVOLD, &dscr);
   1107      1.45   reinoud 		if (!error) {
   1108      1.45   reinoud 			if (dscr == NULL) {
   1109      1.45   reinoud 				trace->wpos = trace->pos;
   1110      1.45   reinoud 				break;		/* empty terminates */
   1111      1.45   reinoud 			}
   1112      1.45   reinoud 			dscr_type = udf_rw16(dscr->tag.id);
   1113      1.45   reinoud 			if (dscr_type == TAGID_TERM) {
   1114      1.45   reinoud 				trace->wpos = trace->pos;
   1115      1.45   reinoud 				break;		/* clean terminator */
   1116      1.45   reinoud 			}
   1117      1.45   reinoud 			if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
   1118      1.45   reinoud 				/* fatal... corrupt disc */
   1119      1.45   reinoud 				error = ENOENT;
   1120      1.45   reinoud 				break;
   1121      1.45   reinoud 			}
   1122      1.45   reinoud 			if (lvint)
   1123      1.45   reinoud 				free(lvint, M_UDFVOLD);
   1124      1.45   reinoud 			lvint = &dscr->lvid;
   1125      1.45   reinoud 			dscr = NULL;
   1126      1.45   reinoud 		} /* else hope for the best... maybe the next is ok */
   1127      1.45   reinoud 
   1128      1.45   reinoud 		DPRINTFIF(VOLUMES, lvint, ("logvol integrity read, state %s\n",
   1129      1.45   reinoud 		    udf_rw32(lvint->integrity_type) ? "CLOSED" : "OPEN"));
   1130      1.45   reinoud 
   1131      1.45   reinoud 		/* proceed sequential */
   1132      1.45   reinoud 		lbnum += 1;
   1133      1.45   reinoud 		len    -= lb_size;
   1134      1.45   reinoud 
   1135      1.45   reinoud 		/* are we linking to a new piece? */
   1136      1.45   reinoud 		if (dscr && lvint->next_extent.len) {
   1137      1.45   reinoud 			len    = udf_rw32(lvint->next_extent.len);
   1138      1.45   reinoud 			lbnum = udf_rw32(lvint->next_extent.loc);
   1139      1.45   reinoud 
   1140      1.45   reinoud 			if (trace_len >= UDF_LVDINT_SEGMENTS-1) {
   1141      1.45   reinoud 				/* IEK! segment link full... */
   1142      1.45   reinoud 				DPRINTF(VOLUMES, ("lvdint segments full\n"));
   1143      1.45   reinoud 				error = EINVAL;
   1144      1.45   reinoud 			} else {
   1145      1.45   reinoud 				trace++;
   1146      1.45   reinoud 				trace_len++;
   1147      1.45   reinoud 
   1148      1.45   reinoud 				trace->start = lbnum;
   1149      1.45   reinoud 				trace->end   = lbnum + len/lb_size;
   1150      1.45   reinoud 				trace->pos   = 0;
   1151      1.45   reinoud 				trace->wpos  = 0;
   1152      1.45   reinoud 			}
   1153      1.45   reinoud 		}
   1154      1.45   reinoud 	}
   1155      1.45   reinoud 
   1156      1.45   reinoud 	/* clean up the mess, esp. when there is an error */
   1157      1.45   reinoud 	if (dscr)
   1158      1.45   reinoud 		free(dscr, M_UDFVOLD);
   1159      1.45   reinoud 
   1160      1.45   reinoud 	if (error && lvint) {
   1161      1.45   reinoud 		free(lvint, M_UDFVOLD);
   1162      1.45   reinoud 		lvint = NULL;
   1163      1.45   reinoud 	}
   1164      1.45   reinoud 
   1165      1.45   reinoud 	if (!lvint)
   1166      1.45   reinoud 		error = ENOENT;
   1167      1.45   reinoud 
   1168      1.45   reinoud 	ump->logvol_integrity = lvint;
   1169      1.45   reinoud 	return error;
   1170      1.45   reinoud }
   1171      1.45   reinoud 
   1172      1.45   reinoud 
   1173      1.45   reinoud static int
   1174      1.45   reinoud udf_loose_lvint_history(struct udf_mount *ump)
   1175      1.45   reinoud {
   1176      1.45   reinoud 	union dscrptr **bufs, *dscr, *last_dscr;
   1177      1.45   reinoud 	struct udf_lvintq *trace, *in_trace, *out_trace;
   1178      1.45   reinoud 	struct logvol_int_desc *lvint;
   1179      1.45   reinoud 	uint32_t in_ext, in_pos, in_len;
   1180      1.45   reinoud 	uint32_t out_ext, out_wpos, out_len;
   1181      1.45   reinoud 	uint32_t lb_size, packet_size, lb_num;
   1182      1.45   reinoud 	uint32_t len, start;
   1183      1.45   reinoud 	int ext, minext, extlen, cnt, cpy_len, dscr_type;
   1184      1.45   reinoud 	int losing;
   1185      1.45   reinoud 	int error;
   1186      1.45   reinoud 
   1187      1.45   reinoud 	DPRINTF(VOLUMES, ("need to lose some lvint history\n"));
   1188      1.45   reinoud 
   1189      1.45   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   1190      1.45   reinoud 	packet_size = ump->data_track.packet_size;	/* XXX data track */
   1191      1.45   reinoud 
   1192      1.45   reinoud 	/* search smallest extent */
   1193      1.45   reinoud 	trace = &ump->lvint_trace[0];
   1194      1.45   reinoud 	minext = trace->end - trace->start;
   1195      1.45   reinoud 	for (ext = 1; ext < UDF_LVDINT_SEGMENTS; ext++) {
   1196      1.45   reinoud 		trace = &ump->lvint_trace[ext];
   1197      1.45   reinoud 		extlen = trace->end - trace->start;
   1198      1.45   reinoud 		if (extlen == 0)
   1199      1.45   reinoud 			break;
   1200      1.45   reinoud 		minext = MIN(minext, extlen);
   1201      1.45   reinoud 	}
   1202      1.45   reinoud 	losing = MIN(minext, UDF_LVINT_LOSSAGE);
   1203      1.45   reinoud 	/* no sense wiping all */
   1204      1.45   reinoud 	if (losing == minext)
   1205      1.45   reinoud 		losing--;
   1206      1.45   reinoud 
   1207      1.45   reinoud 	DPRINTF(VOLUMES, ("\tlosing %d entries\n", losing));
   1208      1.45   reinoud 
   1209      1.45   reinoud 	/* get buffer for pieces */
   1210      1.45   reinoud 	bufs = malloc(UDF_LVDINT_SEGMENTS * sizeof(void *), M_TEMP, M_WAITOK);
   1211      1.45   reinoud 
   1212      1.45   reinoud 	in_ext    = 0;
   1213      1.45   reinoud 	in_pos    = losing;
   1214      1.45   reinoud 	in_trace  = &ump->lvint_trace[in_ext];
   1215      1.45   reinoud 	in_len    = in_trace->end - in_trace->start;
   1216      1.45   reinoud 	out_ext   = 0;
   1217      1.45   reinoud 	out_wpos  = 0;
   1218      1.45   reinoud 	out_trace = &ump->lvint_trace[out_ext];
   1219      1.45   reinoud 	out_len   = out_trace->end - out_trace->start;
   1220      1.45   reinoud 
   1221      1.45   reinoud 	last_dscr = NULL;
   1222      1.45   reinoud 	for(;;) {
   1223      1.45   reinoud 		out_trace->pos  = out_wpos;
   1224      1.45   reinoud 		out_trace->wpos = out_trace->pos;
   1225      1.45   reinoud 		if (in_pos >= in_len) {
   1226      1.45   reinoud 			in_ext++;
   1227      1.45   reinoud 			in_pos = 0;
   1228      1.45   reinoud 			in_trace = &ump->lvint_trace[in_ext];
   1229      1.45   reinoud 			in_len   = in_trace->end - in_trace->start;
   1230      1.45   reinoud 		}
   1231      1.45   reinoud 		if (out_wpos >= out_len) {
   1232      1.45   reinoud 			out_ext++;
   1233      1.45   reinoud 			out_wpos = 0;
   1234      1.45   reinoud 			out_trace = &ump->lvint_trace[out_ext];
   1235      1.45   reinoud 			out_len   = out_trace->end - out_trace->start;
   1236      1.45   reinoud 		}
   1237      1.45   reinoud 		/* copy overlap contents */
   1238      1.45   reinoud 		cpy_len = MIN(in_len - in_pos, out_len - out_wpos);
   1239      1.45   reinoud 		cpy_len = MIN(cpy_len, in_len - in_trace->pos);
   1240      1.45   reinoud 		if (cpy_len == 0)
   1241      1.45   reinoud 			break;
   1242      1.45   reinoud 
   1243      1.45   reinoud 		/* copy */
   1244      1.45   reinoud 		DPRINTF(VOLUMES, ("\treading %d lvid descriptors\n", cpy_len));
   1245      1.45   reinoud 		for (cnt = 0; cnt < cpy_len; cnt++) {
   1246      1.45   reinoud 			/* read in our integrity descriptor */
   1247      1.45   reinoud 			lb_num = in_trace->start + in_pos + cnt;
   1248      1.45   reinoud 			error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD,
   1249      1.45   reinoud 				&dscr);
   1250      1.45   reinoud 			if (error) {
   1251      1.45   reinoud 				/* copy last one */
   1252      1.45   reinoud 				dscr = last_dscr;
   1253      1.45   reinoud 			}
   1254      1.45   reinoud 			bufs[cnt] = dscr;
   1255      1.45   reinoud 			if (!error) {
   1256      1.45   reinoud 				if (dscr == NULL) {
   1257      1.45   reinoud 					out_trace->pos  = out_wpos + cnt;
   1258      1.45   reinoud 					out_trace->wpos = out_trace->pos;
   1259      1.45   reinoud 					break;		/* empty terminates */
   1260      1.45   reinoud 				}
   1261      1.45   reinoud 				dscr_type = udf_rw16(dscr->tag.id);
   1262      1.45   reinoud 				if (dscr_type == TAGID_TERM) {
   1263      1.45   reinoud 					out_trace->pos  = out_wpos + cnt;
   1264      1.45   reinoud 					out_trace->wpos = out_trace->pos;
   1265      1.45   reinoud 					break;		/* clean terminator */
   1266      1.45   reinoud 				}
   1267      1.45   reinoud 				if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
   1268      1.45   reinoud 					panic(  "UDF integrity sequence "
   1269      1.45   reinoud 						"corrupted while mounted!\n");
   1270      1.45   reinoud 				}
   1271      1.45   reinoud 				last_dscr = dscr;
   1272      1.45   reinoud 			}
   1273      1.45   reinoud 		}
   1274      1.45   reinoud 
   1275      1.45   reinoud 		/* patch up if first entry was on error */
   1276      1.45   reinoud 		if (bufs[0] == NULL) {
   1277      1.45   reinoud 			for (cnt = 0; cnt < cpy_len; cnt++)
   1278      1.45   reinoud 				if (bufs[cnt] != NULL)
   1279      1.45   reinoud 					break;
   1280      1.45   reinoud 			last_dscr = bufs[cnt];
   1281      1.45   reinoud 			for (; cnt > 0; cnt--) {
   1282      1.45   reinoud 				bufs[cnt] = last_dscr;
   1283      1.45   reinoud 			}
   1284      1.45   reinoud 		}
   1285      1.45   reinoud 
   1286      1.45   reinoud 		/* glue + write out */
   1287      1.45   reinoud 		DPRINTF(VOLUMES, ("\twriting %d lvid descriptors\n", cpy_len));
   1288      1.45   reinoud 		for (cnt = 0; cnt < cpy_len; cnt++) {
   1289      1.45   reinoud 			lb_num = out_trace->start + out_wpos + cnt;
   1290      1.45   reinoud 			lvint  = &bufs[cnt]->lvid;
   1291      1.45   reinoud 
   1292      1.45   reinoud 			/* set continuation */
   1293      1.45   reinoud 			len = 0;
   1294      1.45   reinoud 			start = 0;
   1295      1.45   reinoud 			if (out_wpos + cnt == out_len) {
   1296      1.45   reinoud 				/* get continuation */
   1297      1.45   reinoud 				trace = &ump->lvint_trace[out_ext+1];
   1298      1.45   reinoud 				len   = trace->end - trace->start;
   1299      1.45   reinoud 				start = trace->start;
   1300      1.45   reinoud 			}
   1301      1.45   reinoud 			lvint->next_extent.len = udf_rw32(len);
   1302      1.45   reinoud 			lvint->next_extent.loc = udf_rw32(start);
   1303      1.45   reinoud 
   1304      1.45   reinoud 			lb_num = trace->start + trace->wpos;
   1305      1.45   reinoud 			error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
   1306      1.45   reinoud 				bufs[cnt], lb_num, lb_num);
   1307      1.45   reinoud 			DPRINTFIF(VOLUMES, error,
   1308      1.45   reinoud 				("error writing lvint lb_num\n"));
   1309      1.45   reinoud 		}
   1310      1.45   reinoud 
   1311      1.45   reinoud 		/* free non repeating descriptors */
   1312      1.45   reinoud 		last_dscr = NULL;
   1313      1.45   reinoud 		for (cnt = 0; cnt < cpy_len; cnt++) {
   1314      1.45   reinoud 			if (bufs[cnt] != last_dscr)
   1315      1.45   reinoud 				free(bufs[cnt], M_UDFVOLD);
   1316      1.45   reinoud 			last_dscr = bufs[cnt];
   1317      1.45   reinoud 		}
   1318      1.45   reinoud 
   1319      1.45   reinoud 		/* advance */
   1320      1.45   reinoud 		in_pos   += cpy_len;
   1321      1.45   reinoud 		out_wpos += cpy_len;
   1322      1.45   reinoud 	}
   1323      1.45   reinoud 
   1324      1.45   reinoud 	free(bufs, M_TEMP);
   1325      1.45   reinoud 
   1326      1.45   reinoud 	return 0;
   1327      1.45   reinoud }
   1328      1.45   reinoud 
   1329      1.45   reinoud 
   1330      1.45   reinoud static int
   1331      1.45   reinoud udf_writeout_lvint(struct udf_mount *ump, int lvflag)
   1332      1.45   reinoud {
   1333      1.45   reinoud 	struct udf_lvintq *trace;
   1334      1.45   reinoud 	struct timeval  now_v;
   1335      1.45   reinoud 	struct timespec now_s;
   1336      1.45   reinoud 	uint32_t sector;
   1337      1.45   reinoud 	int logvol_integrity;
   1338      1.45   reinoud 	int space, error;
   1339      1.45   reinoud 
   1340      1.45   reinoud 	DPRINTF(VOLUMES, ("writing out logvol integrity descriptor\n"));
   1341      1.45   reinoud 
   1342      1.45   reinoud again:
   1343      1.45   reinoud 	/* get free space in last chunk */
   1344      1.45   reinoud 	trace = ump->lvint_trace;
   1345      1.45   reinoud 	while (trace->wpos > (trace->end - trace->start)) {
   1346      1.45   reinoud 		DPRINTF(VOLUMES, ("skip : start = %d, end = %d, pos = %d, "
   1347      1.45   reinoud 				  "wpos = %d\n", trace->start, trace->end,
   1348      1.45   reinoud 				  trace->pos, trace->wpos));
   1349      1.45   reinoud 		trace++;
   1350      1.45   reinoud 	}
   1351      1.45   reinoud 
   1352      1.45   reinoud 	/* check if there is space to append */
   1353      1.45   reinoud 	space = (trace->end - trace->start) - trace->wpos;
   1354      1.45   reinoud 	DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, "
   1355      1.45   reinoud 			  "space = %d\n", trace->start, trace->end, trace->pos,
   1356      1.45   reinoud 			  trace->wpos, space));
   1357      1.45   reinoud 
   1358      1.45   reinoud 	/* get state */
   1359      1.45   reinoud 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
   1360      1.45   reinoud 	if (logvol_integrity == UDF_INTEGRITY_CLOSED) {
   1361      1.45   reinoud 		if ((space < 3) && (lvflag & UDF_APPENDONLY_LVINT)) {
   1362      1.45   reinoud 			/* don't allow this logvol to be opened */
   1363      1.45   reinoud 			/* TODO extent LVINT space if possible */
   1364      1.45   reinoud 			return EROFS;
   1365      1.45   reinoud 		}
   1366      1.45   reinoud 	}
   1367      1.45   reinoud 
   1368      1.45   reinoud 	if (space < 1) {
   1369      1.45   reinoud 		if (lvflag & UDF_APPENDONLY_LVINT)
   1370      1.45   reinoud 			return EROFS;
   1371      1.45   reinoud 		/* loose history by re-writing extents */
   1372      1.45   reinoud 		error = udf_loose_lvint_history(ump);
   1373      1.45   reinoud 		if (error)
   1374      1.45   reinoud 			return error;
   1375      1.45   reinoud 		goto again;
   1376      1.45   reinoud 	}
   1377      1.45   reinoud 
   1378      1.45   reinoud 	/* update our integrity descriptor to identify us and timestamp it */
   1379      1.45   reinoud 	DPRINTF(VOLUMES, ("updating integrity descriptor\n"));
   1380      1.45   reinoud 	microtime(&now_v);
   1381      1.45   reinoud 	TIMEVAL_TO_TIMESPEC(&now_v, &now_s);
   1382      1.45   reinoud 	udf_timespec_to_timestamp(&now_s, &ump->logvol_integrity->time);
   1383      1.45   reinoud 	udf_set_regid(&ump->logvol_info->impl_id, IMPL_NAME);
   1384      1.45   reinoud 	udf_add_impl_regid(ump, &ump->logvol_info->impl_id);
   1385      1.45   reinoud 
   1386      1.45   reinoud 	/* writeout integrity descriptor */
   1387      1.45   reinoud 	sector = trace->start + trace->wpos;
   1388      1.45   reinoud 	error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
   1389      1.45   reinoud 			(union dscrptr *) ump->logvol_integrity,
   1390      1.45   reinoud 			sector, sector);
   1391      1.45   reinoud 	DPRINTF(VOLUMES, ("writeout lvint : error = %d\n", error));
   1392      1.45   reinoud 	if (error)
   1393      1.45   reinoud 		return error;
   1394      1.45   reinoud 
   1395      1.45   reinoud 	/* advance write position */
   1396      1.45   reinoud 	trace->wpos++; space--;
   1397      1.45   reinoud 	if (space >= 1) {
   1398      1.45   reinoud 		/* append terminator */
   1399      1.45   reinoud 		sector = trace->start + trace->wpos;
   1400      1.45   reinoud 		error = udf_write_terminator(ump, sector);
   1401      1.45   reinoud 
   1402      1.45   reinoud 		DPRINTF(VOLUMES, ("write terminator : error = %d\n", error));
   1403      1.45   reinoud 	}
   1404      1.45   reinoud 
   1405      1.45   reinoud 	space = (trace->end - trace->start) - trace->wpos;
   1406      1.45   reinoud 	DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, "
   1407      1.45   reinoud 		"space = %d\n", trace->start, trace->end, trace->pos,
   1408      1.45   reinoud 		trace->wpos, space));
   1409      1.45   reinoud 	DPRINTF(VOLUMES, ("finished writing out logvol integrity descriptor "
   1410      1.45   reinoud 		"successfull\n"));
   1411      1.45   reinoud 
   1412      1.45   reinoud 	return error;
   1413      1.45   reinoud }
   1414      1.45   reinoud 
   1415      1.45   reinoud /* --------------------------------------------------------------------- */
   1416      1.45   reinoud 
   1417      1.45   reinoud static int
   1418      1.45   reinoud udf_read_partition_spacetables(struct udf_mount *ump)
   1419      1.45   reinoud {
   1420      1.45   reinoud 	union dscrptr        *dscr;
   1421      1.45   reinoud 	/* struct udf_args *args = &ump->mount_args; */
   1422      1.45   reinoud 	struct part_desc     *partd;
   1423      1.45   reinoud 	struct part_hdr_desc *parthdr;
   1424      1.45   reinoud 	struct udf_bitmap    *bitmap;
   1425      1.45   reinoud 	uint32_t phys_part;
   1426      1.45   reinoud 	uint32_t lb_num, len;
   1427      1.45   reinoud 	int error, dscr_type;
   1428      1.45   reinoud 
   1429      1.45   reinoud 	/* unallocated space map */
   1430      1.45   reinoud 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
   1431      1.45   reinoud 		partd = ump->partitions[phys_part];
   1432      1.45   reinoud 		if (partd == NULL)
   1433      1.45   reinoud 			continue;
   1434      1.45   reinoud 		parthdr = &partd->_impl_use.part_hdr;
   1435      1.45   reinoud 
   1436      1.45   reinoud 		lb_num  = udf_rw32(partd->start_loc);
   1437      1.45   reinoud 		lb_num += udf_rw32(parthdr->unalloc_space_bitmap.lb_num);
   1438      1.45   reinoud 		len     = udf_rw32(parthdr->unalloc_space_bitmap.len);
   1439      1.45   reinoud 		if (len == 0)
   1440      1.45   reinoud 			continue;
   1441      1.45   reinoud 
   1442      1.45   reinoud 		DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num));
   1443      1.45   reinoud 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
   1444      1.45   reinoud 		if (!error && dscr) {
   1445      1.45   reinoud 			/* analyse */
   1446      1.45   reinoud 			dscr_type = udf_rw16(dscr->tag.id);
   1447      1.45   reinoud 			if (dscr_type == TAGID_SPACE_BITMAP) {
   1448      1.45   reinoud 				DPRINTF(VOLUMES, ("Accepting space bitmap\n"));
   1449      1.45   reinoud 				ump->part_unalloc_dscr[phys_part] = &dscr->sbd;
   1450      1.45   reinoud 
   1451      1.45   reinoud 				/* fill in ump->part_unalloc_bits */
   1452      1.45   reinoud 				bitmap = &ump->part_unalloc_bits[phys_part];
   1453      1.45   reinoud 				bitmap->blob  = (uint8_t *) dscr;
   1454      1.45   reinoud 				bitmap->bits  = dscr->sbd.data;
   1455      1.45   reinoud 				bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
   1456      1.45   reinoud 				bitmap->pages = NULL;	/* TODO */
   1457      1.45   reinoud 				bitmap->data_pos     = 0;
   1458      1.45   reinoud 				bitmap->metadata_pos = 0;
   1459      1.45   reinoud 			} else {
   1460      1.45   reinoud 				free(dscr, M_UDFVOLD);
   1461      1.45   reinoud 
   1462      1.45   reinoud 				printf( "UDF mount: error reading unallocated "
   1463      1.45   reinoud 					"space bitmap\n");
   1464      1.45   reinoud 				return EROFS;
   1465      1.45   reinoud 			}
   1466      1.45   reinoud 		} else {
   1467      1.45   reinoud 			/* blank not allowed */
   1468      1.45   reinoud 			printf("UDF mount: blank unallocated space bitmap\n");
   1469      1.45   reinoud 			return EROFS;
   1470      1.45   reinoud 		}
   1471      1.45   reinoud 	}
   1472      1.45   reinoud 
   1473      1.45   reinoud 	/* unallocated space table (not supported) */
   1474      1.45   reinoud 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
   1475      1.45   reinoud 		partd = ump->partitions[phys_part];
   1476      1.45   reinoud 		if (partd == NULL)
   1477      1.45   reinoud 			continue;
   1478      1.45   reinoud 		parthdr = &partd->_impl_use.part_hdr;
   1479      1.45   reinoud 
   1480      1.45   reinoud 		len     = udf_rw32(parthdr->unalloc_space_table.len);
   1481      1.45   reinoud 		if (len) {
   1482      1.45   reinoud 			printf("UDF mount: space tables not supported\n");
   1483      1.45   reinoud 			return EROFS;
   1484      1.45   reinoud 		}
   1485      1.45   reinoud 	}
   1486      1.45   reinoud 
   1487      1.45   reinoud 	/* freed space map */
   1488      1.45   reinoud 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
   1489      1.45   reinoud 		partd = ump->partitions[phys_part];
   1490      1.45   reinoud 		if (partd == NULL)
   1491      1.45   reinoud 			continue;
   1492      1.45   reinoud 		parthdr = &partd->_impl_use.part_hdr;
   1493      1.45   reinoud 
   1494      1.45   reinoud 		/* freed space map */
   1495      1.45   reinoud 		lb_num  = udf_rw32(partd->start_loc);
   1496      1.45   reinoud 		lb_num += udf_rw32(parthdr->freed_space_bitmap.lb_num);
   1497      1.45   reinoud 		len     = udf_rw32(parthdr->freed_space_bitmap.len);
   1498      1.45   reinoud 		if (len == 0)
   1499      1.45   reinoud 			continue;
   1500      1.45   reinoud 
   1501      1.45   reinoud 		DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num));
   1502      1.45   reinoud 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
   1503      1.45   reinoud 		if (!error && dscr) {
   1504      1.45   reinoud 			/* analyse */
   1505      1.45   reinoud 			dscr_type = udf_rw16(dscr->tag.id);
   1506      1.45   reinoud 			if (dscr_type == TAGID_SPACE_BITMAP) {
   1507      1.45   reinoud 				DPRINTF(VOLUMES, ("Accepting space bitmap\n"));
   1508      1.45   reinoud 				ump->part_freed_dscr[phys_part] = &dscr->sbd;
   1509      1.45   reinoud 
   1510      1.45   reinoud 				/* fill in ump->part_freed_bits */
   1511      1.45   reinoud 				bitmap = &ump->part_unalloc_bits[phys_part];
   1512      1.45   reinoud 				bitmap->blob  = (uint8_t *) dscr;
   1513      1.45   reinoud 				bitmap->bits  = dscr->sbd.data;
   1514      1.45   reinoud 				bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
   1515      1.45   reinoud 				bitmap->pages = NULL;	/* TODO */
   1516      1.45   reinoud 				bitmap->data_pos     = 0;
   1517      1.45   reinoud 				bitmap->metadata_pos = 0;
   1518      1.45   reinoud 			} else {
   1519      1.45   reinoud 				free(dscr, M_UDFVOLD);
   1520      1.45   reinoud 
   1521      1.45   reinoud 				printf( "UDF mount: error reading freed  "
   1522      1.45   reinoud 					"space bitmap\n");
   1523      1.45   reinoud 				return EROFS;
   1524      1.45   reinoud 			}
   1525      1.45   reinoud 		} else {
   1526      1.45   reinoud 			/* blank not allowed */
   1527      1.45   reinoud 			printf("UDF mount: blank freed space bitmap\n");
   1528      1.45   reinoud 			return EROFS;
   1529      1.45   reinoud 		}
   1530      1.45   reinoud 	}
   1531      1.45   reinoud 
   1532      1.45   reinoud 	/* freed space table (not supported) */
   1533      1.45   reinoud 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
   1534      1.45   reinoud 		partd = ump->partitions[phys_part];
   1535      1.45   reinoud 		if (partd == NULL)
   1536      1.45   reinoud 			continue;
   1537      1.45   reinoud 		parthdr = &partd->_impl_use.part_hdr;
   1538      1.45   reinoud 
   1539      1.45   reinoud 		len     = udf_rw32(parthdr->freed_space_table.len);
   1540      1.45   reinoud 		if (len) {
   1541      1.45   reinoud 			printf("UDF mount: space tables not supported\n");
   1542      1.45   reinoud 			return EROFS;
   1543      1.45   reinoud 		}
   1544      1.45   reinoud 	}
   1545      1.45   reinoud 
   1546      1.45   reinoud 	return 0;
   1547      1.45   reinoud }
   1548      1.45   reinoud 
   1549      1.45   reinoud 
   1550      1.45   reinoud /* TODO implement async writeout */
   1551      1.45   reinoud int
   1552      1.45   reinoud udf_write_partition_spacetables(struct udf_mount *ump, int waitfor)
   1553      1.45   reinoud {
   1554      1.45   reinoud 	union dscrptr        *dscr;
   1555      1.45   reinoud 	/* struct udf_args *args = &ump->mount_args; */
   1556      1.45   reinoud 	struct part_desc     *partd;
   1557      1.45   reinoud 	struct part_hdr_desc *parthdr;
   1558      1.45   reinoud 	uint32_t phys_part;
   1559      1.45   reinoud 	uint32_t lb_num, len, ptov;
   1560      1.45   reinoud 	int error_all, error;
   1561      1.45   reinoud 
   1562      1.45   reinoud 	error_all = 0;
   1563      1.45   reinoud 	/* unallocated space map */
   1564      1.45   reinoud 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
   1565      1.45   reinoud 		partd = ump->partitions[phys_part];
   1566      1.45   reinoud 		if (partd == NULL)
   1567      1.45   reinoud 			continue;
   1568      1.45   reinoud 		parthdr = &partd->_impl_use.part_hdr;
   1569      1.45   reinoud 
   1570      1.45   reinoud 		ptov   = udf_rw32(partd->start_loc);
   1571      1.45   reinoud 		lb_num = udf_rw32(parthdr->unalloc_space_bitmap.lb_num);
   1572      1.45   reinoud 		len    = udf_rw32(parthdr->unalloc_space_bitmap.len);
   1573      1.45   reinoud 		if (len == 0)
   1574      1.45   reinoud 			continue;
   1575      1.45   reinoud 
   1576      1.45   reinoud 		DPRINTF(VOLUMES, ("Write unalloc. space bitmap %d\n",
   1577      1.45   reinoud 			lb_num + ptov));
   1578      1.45   reinoud 		dscr = (union dscrptr *) ump->part_unalloc_dscr[phys_part];
   1579      1.45   reinoud 		error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
   1580      1.45   reinoud 				(union dscrptr *) dscr,
   1581      1.45   reinoud 				ptov + lb_num, lb_num);
   1582      1.45   reinoud 		if (error) {
   1583      1.45   reinoud 			DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error));
   1584      1.45   reinoud 			error_all = error;
   1585      1.45   reinoud 		}
   1586      1.45   reinoud 	}
   1587      1.45   reinoud 
   1588      1.45   reinoud 	/* freed space map */
   1589      1.45   reinoud 	for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
   1590      1.45   reinoud 		partd = ump->partitions[phys_part];
   1591      1.45   reinoud 		if (partd == NULL)
   1592      1.45   reinoud 			continue;
   1593      1.45   reinoud 		parthdr = &partd->_impl_use.part_hdr;
   1594      1.45   reinoud 
   1595      1.45   reinoud 		/* freed space map */
   1596      1.45   reinoud 		ptov   = udf_rw32(partd->start_loc);
   1597      1.45   reinoud 		lb_num = udf_rw32(parthdr->freed_space_bitmap.lb_num);
   1598      1.45   reinoud 		len    = udf_rw32(parthdr->freed_space_bitmap.len);
   1599      1.45   reinoud 		if (len == 0)
   1600      1.45   reinoud 			continue;
   1601      1.45   reinoud 
   1602      1.45   reinoud 		DPRINTF(VOLUMES, ("Write freed space bitmap %d\n",
   1603      1.45   reinoud 			lb_num + ptov));
   1604      1.45   reinoud 		dscr = (union dscrptr *) ump->part_freed_dscr[phys_part];
   1605      1.45   reinoud 		error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
   1606      1.45   reinoud 				(union dscrptr *) dscr,
   1607      1.45   reinoud 				ptov + lb_num, lb_num);
   1608      1.45   reinoud 		if (error) {
   1609      1.45   reinoud 			DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error));
   1610      1.45   reinoud 			error_all = error;
   1611      1.45   reinoud 		}
   1612      1.45   reinoud 	}
   1613      1.45   reinoud 
   1614      1.45   reinoud 	return error_all;
   1615      1.45   reinoud }
   1616      1.45   reinoud 
   1617      1.45   reinoud /*
   1618      1.45   reinoud  * Checks if ump's vds information is correct and complete
   1619      1.45   reinoud  */
   1620      1.45   reinoud 
   1621      1.45   reinoud int
   1622      1.45   reinoud udf_process_vds(struct udf_mount *ump) {
   1623      1.45   reinoud 	union udf_pmap *mapping;
   1624      1.45   reinoud 	/* struct udf_args *args = &ump->mount_args; */
   1625      1.45   reinoud 	struct logvol_int_desc *lvint;
   1626      1.45   reinoud 	struct udf_logvol_info *lvinfo;
   1627      1.45   reinoud 	struct part_desc *part;
   1628      1.45   reinoud 	uint32_t n_pm, mt_l;
   1629      1.45   reinoud 	uint8_t *pmap_pos;
   1630      1.45   reinoud 	char *domain_name, *map_name;
   1631      1.45   reinoud 	const char *check_name;
   1632      1.45   reinoud 	char bits[128];
   1633      1.45   reinoud 	int pmap_stype, pmap_size;
   1634      1.45   reinoud 	int pmap_type, log_part, phys_part, raw_phys_part;
   1635      1.45   reinoud 	int n_phys, n_virt, n_spar, n_meta;
   1636      1.45   reinoud 	int len, error;
   1637      1.45   reinoud 
   1638      1.45   reinoud 	if (ump == NULL)
   1639      1.45   reinoud 		return ENOENT;
   1640      1.45   reinoud 
   1641      1.45   reinoud 	/* we need at least an anchor (trivial, but for safety) */
   1642      1.45   reinoud 	if (ump->anchors[0] == NULL)
   1643      1.45   reinoud 		return EINVAL;
   1644      1.45   reinoud 
   1645      1.45   reinoud 	/* we need at least one primary and one logical volume descriptor */
   1646      1.45   reinoud 	if ((ump->primary_vol == NULL) || (ump->logical_vol) == NULL)
   1647      1.45   reinoud 		return EINVAL;
   1648      1.45   reinoud 
   1649      1.45   reinoud 	/* we need at least one partition descriptor */
   1650      1.45   reinoud 	if (ump->partitions[0] == NULL)
   1651      1.45   reinoud 		return EINVAL;
   1652      1.45   reinoud 
   1653      1.45   reinoud 	/* check logical volume sector size verses device sector size */
   1654      1.45   reinoud 	if (udf_rw32(ump->logical_vol->lb_size) != ump->discinfo.sector_size) {
   1655      1.45   reinoud 		printf("UDF mount: format violation, lb_size != sector size\n");
   1656      1.45   reinoud 		return EINVAL;
   1657      1.45   reinoud 	}
   1658      1.45   reinoud 
   1659      1.45   reinoud 	/* check domain name */
   1660      1.45   reinoud 	domain_name = ump->logical_vol->domain_id.id;
   1661      1.45   reinoud 	if (strncmp(domain_name, "*OSTA UDF Compliant", 20)) {
   1662      1.45   reinoud 		printf("mount_udf: disc not OSTA UDF Compliant, aborting\n");
   1663      1.45   reinoud 		return EINVAL;
   1664      1.45   reinoud 	}
   1665      1.45   reinoud 
   1666      1.45   reinoud 	/* retrieve logical volume integrity sequence */
   1667      1.45   reinoud 	error = udf_retrieve_lvint(ump);
   1668      1.45   reinoud 
   1669      1.45   reinoud 	/*
   1670      1.45   reinoud 	 * We need at least one logvol integrity descriptor recorded.  Note
   1671      1.45   reinoud 	 * that its OK to have an open logical volume integrity here. The VAT
   1672      1.45   reinoud 	 * will close/update the integrity.
   1673      1.45   reinoud 	 */
   1674      1.45   reinoud 	if (ump->logvol_integrity == NULL)
   1675      1.45   reinoud 		return EINVAL;
   1676      1.45   reinoud 
   1677      1.45   reinoud 	/* read in and check unallocated and free space info if writing */
   1678      1.45   reinoud 	if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY) == 0) {
   1679      1.45   reinoud 		error = udf_read_partition_spacetables(ump);
   1680      1.45   reinoud 		if (error)
   1681      1.45   reinoud 			return error;
   1682      1.45   reinoud 	}
   1683      1.45   reinoud 
   1684      1.45   reinoud 	/* process derived structures */
   1685      1.45   reinoud 	n_pm   = udf_rw32(ump->logical_vol->n_pm);   /* num partmaps         */
   1686      1.45   reinoud 	lvint  = ump->logvol_integrity;
   1687      1.45   reinoud 	lvinfo = (struct udf_logvol_info *) (&lvint->tables[2 * n_pm]);
   1688      1.45   reinoud 	ump->logvol_info = lvinfo;
   1689      1.45   reinoud 
   1690      1.45   reinoud 	/* TODO check udf versions? */
   1691      1.45   reinoud 
   1692      1.45   reinoud 	/*
   1693      1.45   reinoud 	 * check logvol mappings: effective virt->log partmap translation
   1694      1.45   reinoud 	 * check and recording of the mapping results. Saves expensive
   1695      1.45   reinoud 	 * strncmp() in tight places.
   1696      1.45   reinoud 	 */
   1697      1.45   reinoud 	DPRINTF(VOLUMES, ("checking logvol mappings\n"));
   1698      1.45   reinoud 	n_pm = udf_rw32(ump->logical_vol->n_pm);   /* num partmaps         */
   1699      1.45   reinoud 	mt_l = udf_rw32(ump->logical_vol->mt_l);   /* partmaps data length */
   1700      1.45   reinoud 	pmap_pos =  ump->logical_vol->maps;
   1701      1.45   reinoud 
   1702      1.45   reinoud 	if (n_pm > UDF_PMAPS) {
   1703      1.45   reinoud 		printf("UDF mount: too many mappings\n");
   1704      1.45   reinoud 		return EINVAL;
   1705      1.45   reinoud 	}
   1706      1.45   reinoud 
   1707      1.45   reinoud 	ump->data_part = ump->metadata_part = 0;
   1708      1.45   reinoud 	n_phys = n_virt = n_spar = n_meta = 0;
   1709      1.45   reinoud 	for (log_part = 0; log_part < n_pm; log_part++) {
   1710      1.45   reinoud 		mapping = (union udf_pmap *) pmap_pos;
   1711      1.45   reinoud 		pmap_stype = pmap_pos[0];
   1712      1.45   reinoud 		pmap_size  = pmap_pos[1];
   1713      1.45   reinoud 		switch (pmap_stype) {
   1714      1.45   reinoud 		case 1:	/* physical mapping */
   1715      1.45   reinoud 			/* volseq    = udf_rw16(mapping->pm1.vol_seq_num); */
   1716      1.45   reinoud 			raw_phys_part = udf_rw16(mapping->pm1.part_num);
   1717      1.45   reinoud 			pmap_type = UDF_VTOP_TYPE_PHYS;
   1718      1.45   reinoud 			n_phys++;
   1719      1.45   reinoud 			ump->data_part     = log_part;
   1720      1.45   reinoud 			ump->metadata_part = log_part;
   1721      1.45   reinoud 			break;
   1722      1.45   reinoud 		case 2: /* virtual/sparable/meta mapping */
   1723      1.45   reinoud 			map_name  = mapping->pm2.part_id.id;
   1724      1.45   reinoud 			/* volseq  = udf_rw16(mapping->pm2.vol_seq_num); */
   1725      1.45   reinoud 			raw_phys_part = udf_rw16(mapping->pm2.part_num);
   1726      1.45   reinoud 			pmap_type = UDF_VTOP_TYPE_UNKNOWN;
   1727      1.45   reinoud 			len = UDF_REGID_ID_SIZE;
   1728      1.45   reinoud 
   1729      1.45   reinoud 			check_name = "*UDF Virtual Partition";
   1730      1.45   reinoud 			if (strncmp(map_name, check_name, len) == 0) {
   1731      1.45   reinoud 				pmap_type = UDF_VTOP_TYPE_VIRT;
   1732      1.45   reinoud 				n_virt++;
   1733      1.45   reinoud 				ump->metadata_part = log_part;
   1734      1.45   reinoud 				break;
   1735      1.45   reinoud 			}
   1736      1.45   reinoud 			check_name = "*UDF Sparable Partition";
   1737      1.45   reinoud 			if (strncmp(map_name, check_name, len) == 0) {
   1738      1.45   reinoud 				pmap_type = UDF_VTOP_TYPE_SPARABLE;
   1739      1.45   reinoud 				n_spar++;
   1740      1.45   reinoud 				ump->data_part     = log_part;
   1741      1.45   reinoud 				ump->metadata_part = log_part;
   1742      1.45   reinoud 				break;
   1743      1.45   reinoud 			}
   1744      1.45   reinoud 			check_name = "*UDF Metadata Partition";
   1745      1.45   reinoud 			if (strncmp(map_name, check_name, len) == 0) {
   1746      1.45   reinoud 				pmap_type = UDF_VTOP_TYPE_META;
   1747      1.45   reinoud 				n_meta++;
   1748      1.45   reinoud 				ump->metadata_part = log_part;
   1749      1.45   reinoud 				break;
   1750      1.45   reinoud 			}
   1751      1.45   reinoud 			break;
   1752      1.45   reinoud 		default:
   1753      1.45   reinoud 			return EINVAL;
   1754      1.45   reinoud 		}
   1755      1.45   reinoud 
   1756      1.45   reinoud 		/*
   1757      1.45   reinoud 		 * BUGALERT: some rogue implementations use random physical
   1758      1.45   reinoud 		 * partion numbers to break other implementations so lookup
   1759      1.45   reinoud 		 * the number.
   1760      1.45   reinoud 		 */
   1761      1.45   reinoud 		for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
   1762      1.45   reinoud 			part = ump->partitions[phys_part];
   1763      1.45   reinoud 			if (part == NULL)
   1764      1.45   reinoud 				continue;
   1765      1.45   reinoud 			if (udf_rw16(part->part_num) == raw_phys_part)
   1766      1.45   reinoud 				break;
   1767      1.45   reinoud 		}
   1768      1.45   reinoud 
   1769      1.45   reinoud 		DPRINTF(VOLUMES, ("\t%d -> %d(%d) type %d\n", log_part,
   1770      1.45   reinoud 		    raw_phys_part, phys_part, pmap_type));
   1771      1.45   reinoud 
   1772      1.45   reinoud 		if (phys_part == UDF_PARTITIONS)
   1773      1.45   reinoud 			return EINVAL;
   1774      1.45   reinoud 		if (pmap_type == UDF_VTOP_TYPE_UNKNOWN)
   1775      1.45   reinoud 			return EINVAL;
   1776      1.45   reinoud 
   1777      1.45   reinoud 		ump->vtop   [log_part] = phys_part;
   1778      1.45   reinoud 		ump->vtop_tp[log_part] = pmap_type;
   1779      1.45   reinoud 
   1780      1.45   reinoud 		pmap_pos += pmap_size;
   1781      1.45   reinoud 	}
   1782      1.45   reinoud 	/* not winning the beauty contest */
   1783      1.45   reinoud 	ump->vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW;
   1784      1.45   reinoud 
   1785      1.45   reinoud 	/* test some basic UDF assertions/requirements */
   1786      1.45   reinoud 	if ((n_virt > 1) || (n_spar > 1) || (n_meta > 1))
   1787      1.45   reinoud 		return EINVAL;
   1788      1.45   reinoud 
   1789      1.45   reinoud 	if (n_virt) {
   1790      1.45   reinoud 		if ((n_phys == 0) || n_spar || n_meta)
   1791      1.45   reinoud 			return EINVAL;
   1792      1.45   reinoud 	}
   1793      1.45   reinoud 	if (n_spar + n_phys == 0)
   1794      1.45   reinoud 		return EINVAL;
   1795      1.45   reinoud 
   1796      1.45   reinoud 	/* determine allocation scheme's based on disc format */
   1797      1.45   reinoud 	/* VAT's can only be on a sequential media */
   1798      1.45   reinoud 	ump->data_alloc = UDF_ALLOC_SPACEMAP;
   1799      1.45   reinoud 	if (n_virt)
   1800      1.45   reinoud 		ump->data_alloc = UDF_ALLOC_SEQUENTIAL;
   1801      1.45   reinoud 
   1802      1.45   reinoud 	ump->meta_alloc = UDF_ALLOC_SPACEMAP;
   1803      1.45   reinoud 	if (n_virt)
   1804      1.45   reinoud 		ump->meta_alloc = UDF_ALLOC_VAT;
   1805      1.45   reinoud 	if (n_meta)
   1806      1.45   reinoud 		ump->meta_alloc = UDF_ALLOC_METABITMAP;
   1807      1.45   reinoud 
   1808      1.45   reinoud 	/* special cases for pseudo-overwrite */
   1809      1.45   reinoud 	if (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) {
   1810      1.45   reinoud 		ump->data_alloc = UDF_ALLOC_SEQUENTIAL;
   1811      1.45   reinoud 		if (n_meta) {
   1812      1.45   reinoud 			ump->meta_alloc = UDF_ALLOC_METASEQUENTIAL;
   1813      1.45   reinoud 		} else {
   1814      1.45   reinoud 			ump->meta_alloc = UDF_ALLOC_RELAXEDSEQUENTIAL;
   1815      1.45   reinoud 		}
   1816      1.45   reinoud 	}
   1817      1.45   reinoud 
   1818  1.49.2.3    simonb 	/* determine default allocation descriptors to use */
   1819  1.49.2.3    simonb 	ump->data_allocdscr = UDF_ICB_SHORT_ALLOC;
   1820  1.49.2.3    simonb 	ump->meta_allocdscr = UDF_ICB_SHORT_ALLOC;
   1821  1.49.2.3    simonb 	if (n_pm > 1) {
   1822  1.49.2.3    simonb 		ump->data_allocdscr = UDF_ICB_LONG_ALLOC;
   1823  1.49.2.3    simonb 		ump->meta_allocdscr = UDF_ICB_LONG_ALLOC;
   1824  1.49.2.3    simonb 		/* metadata partitions are forced to have short */
   1825  1.49.2.3    simonb 		if (n_meta)
   1826  1.49.2.3    simonb 			ump->meta_allocdscr = UDF_ICB_SHORT_ALLOC;
   1827  1.49.2.3    simonb 	}
   1828  1.49.2.3    simonb 
   1829      1.45   reinoud 	/* determine logical volume open/closure actions */
   1830      1.45   reinoud 	if (n_virt) {
   1831      1.45   reinoud 		ump->lvopen  = 0;
   1832      1.45   reinoud 		if (ump->discinfo.last_session_state == MMC_STATE_CLOSED)
   1833      1.45   reinoud 			ump->lvopen |= UDF_OPEN_SESSION ;
   1834      1.45   reinoud 		ump->lvclose = UDF_WRITE_VAT;
   1835      1.45   reinoud 		if (ump->mount_args.udfmflags & UDFMNT_CLOSESESSION)
   1836      1.45   reinoud 			ump->lvclose |= UDF_CLOSE_SESSION;
   1837      1.45   reinoud 	} else {
   1838      1.45   reinoud 		/* `normal' rewritable or non sequential media */
   1839      1.45   reinoud 		ump->lvopen  = UDF_WRITE_LVINT;
   1840      1.45   reinoud 		ump->lvclose = UDF_WRITE_LVINT;
   1841      1.45   reinoud 		if ((ump->discinfo.mmc_cur & MMC_CAP_REWRITABLE) == 0)
   1842      1.45   reinoud 			ump->lvopen  |= UDF_APPENDONLY_LVINT;
   1843      1.45   reinoud 	}
   1844      1.45   reinoud 
   1845      1.45   reinoud 	/*
   1846      1.45   reinoud 	 * Determine sheduler error behaviour. For virtual partions, update
   1847      1.45   reinoud 	 * the trackinfo; for sparable partitions replace a whole block on the
   1848      1.45   reinoud 	 * sparable table. Allways requeue.
   1849      1.45   reinoud 	 */
   1850      1.45   reinoud 	ump->lvreadwrite = 0;
   1851      1.45   reinoud 	if (n_virt)
   1852      1.45   reinoud 		ump->lvreadwrite = UDF_UPDATE_TRACKINFO;
   1853      1.45   reinoud 	if (n_spar)
   1854      1.45   reinoud 		ump->lvreadwrite = UDF_REMAP_BLOCK;
   1855      1.45   reinoud 
   1856      1.45   reinoud 	/*
   1857      1.45   reinoud 	 * Select our sheduler
   1858      1.45   reinoud 	 */
   1859      1.45   reinoud 	ump->strategy = &udf_strat_rmw;
   1860      1.45   reinoud 	if (n_virt || (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE))
   1861      1.45   reinoud 		ump->strategy = &udf_strat_sequential;
   1862      1.45   reinoud 	if ((ump->discinfo.mmc_class == MMC_CLASS_DISC) ||
   1863      1.45   reinoud 		(ump->discinfo.mmc_class == MMC_CLASS_UNKN))
   1864      1.45   reinoud 			ump->strategy = &udf_strat_direct;
   1865      1.45   reinoud 	if (n_spar)
   1866      1.45   reinoud 		ump->strategy = &udf_strat_rmw;
   1867      1.45   reinoud 
   1868      1.45   reinoud 	/* print results */
   1869      1.45   reinoud 	DPRINTF(VOLUMES, ("\tdata alloc scheme %d, meta alloc scheme %d\n",
   1870      1.45   reinoud 	    ump->data_alloc, ump->meta_alloc));
   1871      1.45   reinoud 	DPRINTF(VOLUMES, ("\tdata partition %d, metadata partition %d\n",
   1872      1.45   reinoud 	    ump->data_part, ump->metadata_part));
   1873      1.45   reinoud 
   1874      1.45   reinoud 	bitmask_snprintf(ump->lvopen,  UDFLOGVOL_BITS, bits, sizeof(bits));
   1875      1.45   reinoud 	DPRINTF(VOLUMES, ("\tactions on logvol open  %s\n", bits));
   1876      1.45   reinoud 	bitmask_snprintf(ump->lvclose, UDFLOGVOL_BITS, bits, sizeof(bits));
   1877      1.45   reinoud 	DPRINTF(VOLUMES, ("\tactions on logvol close %s\n", bits));
   1878      1.45   reinoud 	bitmask_snprintf(ump->lvreadwrite, UDFONERROR_BITS, bits, sizeof(bits));
   1879      1.45   reinoud 	DPRINTF(VOLUMES, ("\tactions on logvol errors %s\n", bits));
   1880      1.45   reinoud 
   1881      1.45   reinoud 	DPRINTF(VOLUMES, ("\tselected sheduler `%s`\n",
   1882      1.45   reinoud 		(ump->strategy == &udf_strat_direct) ? "Direct" :
   1883      1.45   reinoud 		(ump->strategy == &udf_strat_sequential) ? "Sequential" :
   1884      1.45   reinoud 		(ump->strategy == &udf_strat_rmw) ? "RMW" : "UNKNOWN!"));
   1885      1.45   reinoud 
   1886      1.45   reinoud 	/* signal its OK for now */
   1887      1.45   reinoud 	return 0;
   1888      1.45   reinoud }
   1889      1.45   reinoud 
   1890      1.45   reinoud /* --------------------------------------------------------------------- */
   1891      1.45   reinoud 
   1892      1.45   reinoud /*
   1893      1.45   reinoud  * Update logical volume name in all structures that keep a record of it. We
   1894      1.45   reinoud  * use memmove since each of them might be specified as a source.
   1895      1.45   reinoud  *
   1896      1.45   reinoud  * Note that it doesn't update the VAT structure!
   1897      1.45   reinoud  */
   1898      1.45   reinoud 
   1899      1.45   reinoud static void
   1900      1.45   reinoud udf_update_logvolname(struct udf_mount *ump, char *logvol_id)
   1901      1.45   reinoud {
   1902      1.45   reinoud 	struct logvol_desc     *lvd = NULL;
   1903      1.45   reinoud 	struct fileset_desc    *fsd = NULL;
   1904      1.45   reinoud 	struct udf_lv_info     *lvi = NULL;
   1905      1.45   reinoud 
   1906      1.45   reinoud 	DPRINTF(VOLUMES, ("Updating logical volume name\n"));
   1907      1.45   reinoud 	lvd = ump->logical_vol;
   1908      1.45   reinoud 	fsd = ump->fileset_desc;
   1909      1.45   reinoud 	if (ump->implementation)
   1910      1.45   reinoud 		lvi = &ump->implementation->_impl_use.lv_info;
   1911      1.45   reinoud 
   1912      1.45   reinoud 	/* logvol's id might be specified as origional so use memmove here */
   1913      1.45   reinoud 	memmove(lvd->logvol_id, logvol_id, 128);
   1914      1.45   reinoud 	if (fsd)
   1915      1.45   reinoud 		memmove(fsd->logvol_id, logvol_id, 128);
   1916      1.45   reinoud 	if (lvi)
   1917      1.45   reinoud 		memmove(lvi->logvol_id, logvol_id, 128);
   1918      1.45   reinoud }
   1919      1.45   reinoud 
   1920      1.45   reinoud /* --------------------------------------------------------------------- */
   1921      1.45   reinoud 
   1922  1.49.2.2    simonb void
   1923  1.49.2.1    simonb udf_inittag(struct udf_mount *ump, struct desc_tag *tag, int tagid,
   1924  1.49.2.1    simonb 	uint32_t sector)
   1925  1.49.2.1    simonb {
   1926  1.49.2.1    simonb 	assert(ump->logical_vol);
   1927  1.49.2.1    simonb 
   1928  1.49.2.1    simonb 	tag->id 		= udf_rw16(tagid);
   1929  1.49.2.1    simonb 	tag->descriptor_ver	= ump->logical_vol->tag.descriptor_ver;
   1930  1.49.2.1    simonb 	tag->cksum		= 0;
   1931  1.49.2.1    simonb 	tag->reserved		= 0;
   1932  1.49.2.1    simonb 	tag->serial_num		= ump->logical_vol->tag.serial_num;
   1933  1.49.2.1    simonb 	tag->tag_loc            = udf_rw32(sector);
   1934  1.49.2.1    simonb }
   1935  1.49.2.1    simonb 
   1936  1.49.2.1    simonb 
   1937  1.49.2.1    simonb uint64_t
   1938  1.49.2.1    simonb udf_advance_uniqueid(struct udf_mount *ump)
   1939  1.49.2.1    simonb {
   1940  1.49.2.1    simonb 	uint64_t unique_id;
   1941  1.49.2.1    simonb 
   1942  1.49.2.1    simonb 	mutex_enter(&ump->logvol_mutex);
   1943  1.49.2.1    simonb 	unique_id = udf_rw64(ump->logvol_integrity->lvint_next_unique_id);
   1944  1.49.2.1    simonb 	if (unique_id < 0x10)
   1945  1.49.2.1    simonb 		unique_id = 0x10;
   1946  1.49.2.1    simonb 	ump->logvol_integrity->lvint_next_unique_id = udf_rw64(unique_id + 1);
   1947  1.49.2.1    simonb 	mutex_exit(&ump->logvol_mutex);
   1948  1.49.2.1    simonb 
   1949  1.49.2.1    simonb 	return unique_id;
   1950  1.49.2.1    simonb }
   1951  1.49.2.1    simonb 
   1952  1.49.2.1    simonb 
   1953  1.49.2.1    simonb static void
   1954  1.49.2.1    simonb udf_adjust_filecount(struct udf_node *udf_node, int sign)
   1955  1.49.2.1    simonb {
   1956  1.49.2.1    simonb 	struct udf_mount *ump = udf_node->ump;
   1957  1.49.2.1    simonb 	uint32_t num_dirs, num_files;
   1958  1.49.2.1    simonb 	int udf_file_type;
   1959  1.49.2.1    simonb 
   1960  1.49.2.1    simonb 	/* get file type */
   1961  1.49.2.1    simonb 	if (udf_node->fe) {
   1962  1.49.2.1    simonb 		udf_file_type = udf_node->fe->icbtag.file_type;
   1963  1.49.2.1    simonb 	} else {
   1964  1.49.2.1    simonb 		udf_file_type = udf_node->efe->icbtag.file_type;
   1965  1.49.2.1    simonb 	}
   1966  1.49.2.1    simonb 
   1967  1.49.2.1    simonb 	/* adjust file count */
   1968  1.49.2.1    simonb 	mutex_enter(&ump->allocate_mutex);
   1969  1.49.2.1    simonb 	if (udf_file_type == UDF_ICB_FILETYPE_DIRECTORY) {
   1970  1.49.2.1    simonb 		num_dirs = udf_rw32(ump->logvol_info->num_directories);
   1971  1.49.2.1    simonb 		ump->logvol_info->num_directories =
   1972  1.49.2.1    simonb 			udf_rw32((num_dirs + sign));
   1973  1.49.2.1    simonb 	} else {
   1974  1.49.2.1    simonb 		num_files = udf_rw32(ump->logvol_info->num_files);
   1975  1.49.2.1    simonb 		ump->logvol_info->num_files =
   1976  1.49.2.1    simonb 			udf_rw32((num_files + sign));
   1977  1.49.2.1    simonb 	}
   1978  1.49.2.1    simonb 	mutex_exit(&ump->allocate_mutex);
   1979  1.49.2.1    simonb }
   1980  1.49.2.1    simonb 
   1981  1.49.2.1    simonb 
   1982  1.49.2.1    simonb void
   1983  1.49.2.1    simonb udf_osta_charset(struct charspec *charspec)
   1984  1.49.2.1    simonb {
   1985  1.49.2.1    simonb 	bzero(charspec, sizeof(struct charspec));
   1986  1.49.2.1    simonb 	charspec->type = 0;
   1987  1.49.2.1    simonb 	strcpy((char *) charspec->inf, "OSTA Compressed Unicode");
   1988  1.49.2.1    simonb }
   1989  1.49.2.1    simonb 
   1990  1.49.2.1    simonb 
   1991  1.49.2.1    simonb /* first call udf_set_regid and then the suffix */
   1992  1.49.2.1    simonb void
   1993  1.49.2.1    simonb udf_set_regid(struct regid *regid, char const *name)
   1994  1.49.2.1    simonb {
   1995  1.49.2.1    simonb 	bzero(regid, sizeof(struct regid));
   1996  1.49.2.1    simonb 	regid->flags    = 0;		/* not dirty and not protected */
   1997  1.49.2.1    simonb 	strcpy((char *) regid->id, name);
   1998  1.49.2.1    simonb }
   1999  1.49.2.1    simonb 
   2000  1.49.2.1    simonb 
   2001  1.49.2.1    simonb void
   2002  1.49.2.1    simonb udf_add_domain_regid(struct udf_mount *ump, struct regid *regid)
   2003  1.49.2.1    simonb {
   2004  1.49.2.1    simonb 	uint16_t *ver;
   2005  1.49.2.1    simonb 
   2006  1.49.2.1    simonb 	ver  = (uint16_t *) regid->id_suffix;
   2007  1.49.2.1    simonb 	*ver = ump->logvol_info->min_udf_readver;
   2008  1.49.2.1    simonb }
   2009  1.49.2.1    simonb 
   2010  1.49.2.1    simonb 
   2011  1.49.2.1    simonb void
   2012  1.49.2.1    simonb udf_add_udf_regid(struct udf_mount *ump, struct regid *regid)
   2013  1.49.2.1    simonb {
   2014  1.49.2.1    simonb 	uint16_t *ver;
   2015  1.49.2.1    simonb 
   2016  1.49.2.1    simonb 	ver  = (uint16_t *) regid->id_suffix;
   2017  1.49.2.1    simonb 	*ver = ump->logvol_info->min_udf_readver;
   2018  1.49.2.1    simonb 
   2019  1.49.2.1    simonb 	regid->id_suffix[2] = 4;	/* unix */
   2020  1.49.2.1    simonb 	regid->id_suffix[3] = 8;	/* NetBSD */
   2021  1.49.2.1    simonb }
   2022  1.49.2.1    simonb 
   2023  1.49.2.1    simonb 
   2024  1.49.2.1    simonb void
   2025  1.49.2.1    simonb udf_add_impl_regid(struct udf_mount *ump, struct regid *regid)
   2026  1.49.2.1    simonb {
   2027  1.49.2.1    simonb 	regid->id_suffix[0] = 4;	/* unix */
   2028  1.49.2.1    simonb 	regid->id_suffix[1] = 8;	/* NetBSD */
   2029  1.49.2.1    simonb }
   2030  1.49.2.1    simonb 
   2031  1.49.2.1    simonb 
   2032  1.49.2.1    simonb void
   2033  1.49.2.1    simonb udf_add_app_regid(struct udf_mount *ump, struct regid *regid)
   2034  1.49.2.1    simonb {
   2035  1.49.2.1    simonb 	regid->id_suffix[0] = APP_VERSION_MAIN;
   2036  1.49.2.1    simonb 	regid->id_suffix[1] = APP_VERSION_SUB;
   2037  1.49.2.1    simonb }
   2038  1.49.2.1    simonb 
   2039  1.49.2.1    simonb static int
   2040  1.49.2.1    simonb udf_create_parentfid(struct udf_mount *ump, struct fileid_desc *fid,
   2041  1.49.2.1    simonb 	struct long_ad *parent, uint64_t unique_id)
   2042  1.49.2.1    simonb {
   2043  1.49.2.1    simonb 	/* the size of an empty FID is 38 but needs to be a multiple of 4 */
   2044  1.49.2.1    simonb 	int fidsize = 40;
   2045  1.49.2.1    simonb 
   2046  1.49.2.1    simonb 	udf_inittag(ump, &fid->tag, TAGID_FID, udf_rw32(parent->loc.lb_num));
   2047  1.49.2.1    simonb 	fid->file_version_num = udf_rw16(1);	/* UDF 2.3.4.1 */
   2048  1.49.2.1    simonb 	fid->file_char = UDF_FILE_CHAR_DIR | UDF_FILE_CHAR_PAR;
   2049  1.49.2.1    simonb 	fid->icb = *parent;
   2050  1.49.2.1    simonb 	fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
   2051  1.49.2.1    simonb 	fid->tag.desc_crc_len = fidsize - UDF_DESC_TAG_LENGTH;
   2052  1.49.2.1    simonb 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
   2053  1.49.2.1    simonb 
   2054  1.49.2.1    simonb 	return fidsize;
   2055  1.49.2.1    simonb }
   2056  1.49.2.1    simonb 
   2057  1.49.2.1    simonb /* --------------------------------------------------------------------- */
   2058  1.49.2.1    simonb 
   2059      1.45   reinoud /*
   2060  1.49.2.1    simonb  * Extended attribute support. UDF knows of 3 places for extended attributes:
   2061      1.45   reinoud  *
   2062  1.49.2.1    simonb  * (a) inside the file's (e)fe in the length of the extended attribute area
   2063  1.49.2.1    simonb  * before the allocation descriptors/filedata
   2064      1.45   reinoud  *
   2065      1.45   reinoud  * (b) in a file referenced by (e)fe->ext_attr_icb and
   2066      1.45   reinoud  *
   2067      1.45   reinoud  * (c) in the e(fe)'s associated stream directory that can hold various
   2068      1.45   reinoud  * sub-files. In the stream directory a few fixed named subfiles are reserved
   2069      1.45   reinoud  * for NT/Unix ACL's and OS/2 attributes.
   2070      1.45   reinoud  *
   2071      1.45   reinoud  * NOTE: Extended attributes are read randomly but allways written
   2072      1.45   reinoud  * *atomicaly*. For ACL's this interface is propably different but not known
   2073      1.45   reinoud  * to me yet.
   2074  1.49.2.1    simonb  *
   2075  1.49.2.1    simonb  * Order of extended attributes in a space :
   2076  1.49.2.1    simonb  *   ECMA 167 EAs
   2077  1.49.2.1    simonb  *   Non block aligned Implementation Use EAs
   2078  1.49.2.1    simonb  *   Block aligned Implementation Use EAs
   2079  1.49.2.1    simonb  *   Application Use EAs
   2080      1.45   reinoud  */
   2081      1.45   reinoud 
   2082      1.45   reinoud static int
   2083      1.45   reinoud udf_impl_extattr_check(struct impl_extattr_entry *implext)
   2084      1.45   reinoud {
   2085      1.45   reinoud 	uint16_t   *spos;
   2086      1.45   reinoud 
   2087      1.45   reinoud 	if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
   2088      1.45   reinoud 		/* checksum valid? */
   2089      1.45   reinoud 		DPRINTF(EXTATTR, ("checking UDF impl. attr checksum\n"));
   2090      1.45   reinoud 		spos = (uint16_t *) implext->data;
   2091      1.45   reinoud 		if (udf_rw16(*spos) != udf_ea_cksum((uint8_t *) implext))
   2092      1.45   reinoud 			return EINVAL;
   2093      1.45   reinoud 	}
   2094      1.45   reinoud 	return 0;
   2095      1.45   reinoud }
   2096      1.45   reinoud 
   2097      1.45   reinoud static void
   2098      1.45   reinoud udf_calc_impl_extattr_checksum(struct impl_extattr_entry *implext)
   2099      1.45   reinoud {
   2100      1.45   reinoud 	uint16_t   *spos;
   2101      1.45   reinoud 
   2102      1.45   reinoud 	if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
   2103      1.45   reinoud 		/* set checksum */
   2104      1.45   reinoud 		spos = (uint16_t *) implext->data;
   2105      1.45   reinoud 		*spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
   2106      1.45   reinoud 	}
   2107      1.45   reinoud }
   2108      1.45   reinoud 
   2109      1.45   reinoud 
   2110      1.45   reinoud int
   2111      1.45   reinoud udf_extattr_search_intern(struct udf_node *node,
   2112      1.45   reinoud 	uint32_t sattr, char const *sattrname,
   2113      1.45   reinoud 	uint32_t *offsetp, uint32_t *lengthp)
   2114      1.45   reinoud {
   2115      1.45   reinoud 	struct extattrhdr_desc    *eahdr;
   2116      1.45   reinoud 	struct extattr_entry      *attrhdr;
   2117      1.45   reinoud 	struct impl_extattr_entry *implext;
   2118      1.45   reinoud 	uint32_t    offset, a_l, sector_size;
   2119      1.45   reinoud 	 int32_t    l_ea;
   2120      1.45   reinoud 	uint8_t    *pos;
   2121      1.45   reinoud 	int         error;
   2122      1.45   reinoud 
   2123      1.45   reinoud 	/* get mountpoint */
   2124      1.45   reinoud 	sector_size = node->ump->discinfo.sector_size;
   2125      1.45   reinoud 
   2126      1.45   reinoud 	/* get information from fe/efe */
   2127      1.45   reinoud 	if (node->fe) {
   2128      1.45   reinoud 		l_ea  = udf_rw32(node->fe->l_ea);
   2129      1.45   reinoud 		eahdr = (struct extattrhdr_desc *) node->fe->data;
   2130      1.45   reinoud 	} else {
   2131      1.45   reinoud 		assert(node->efe);
   2132      1.45   reinoud 		l_ea  = udf_rw32(node->efe->l_ea);
   2133      1.45   reinoud 		eahdr = (struct extattrhdr_desc *) node->efe->data;
   2134      1.45   reinoud 	}
   2135      1.45   reinoud 
   2136      1.45   reinoud 	/* something recorded here? */
   2137      1.45   reinoud 	if (l_ea == 0)
   2138      1.45   reinoud 		return ENOENT;
   2139      1.45   reinoud 
   2140      1.45   reinoud 	/* check extended attribute tag; what to do if it fails? */
   2141      1.45   reinoud 	error = udf_check_tag(eahdr);
   2142      1.45   reinoud 	if (error)
   2143      1.45   reinoud 		return EINVAL;
   2144      1.45   reinoud 	if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR)
   2145      1.45   reinoud 		return EINVAL;
   2146      1.45   reinoud 	error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc));
   2147      1.45   reinoud 	if (error)
   2148      1.45   reinoud 		return EINVAL;
   2149      1.45   reinoud 
   2150      1.45   reinoud 	DPRINTF(EXTATTR, ("Found %d bytes of extended attributes\n", l_ea));
   2151      1.45   reinoud 
   2152      1.45   reinoud 	/* looking for Ecma-167 attributes? */
   2153      1.45   reinoud 	offset = sizeof(struct extattrhdr_desc);
   2154      1.45   reinoud 
   2155      1.45   reinoud 	/* looking for either implemenation use or application use */
   2156      1.45   reinoud 	if (sattr == 2048) {				/* [4/48.10.8] */
   2157      1.45   reinoud 		offset = udf_rw32(eahdr->impl_attr_loc);
   2158      1.45   reinoud 		if (offset == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
   2159      1.45   reinoud 			return ENOENT;
   2160      1.45   reinoud 	}
   2161      1.45   reinoud 	if (sattr == 65536) {				/* [4/48.10.9] */
   2162      1.45   reinoud 		offset = udf_rw32(eahdr->appl_attr_loc);
   2163      1.45   reinoud 		if (offset == UDF_APPL_ATTR_LOC_NOT_PRESENT)
   2164      1.45   reinoud 			return ENOENT;
   2165      1.45   reinoud 	}
   2166      1.45   reinoud 
   2167      1.45   reinoud 	/* paranoia check offset and l_ea */
   2168      1.45   reinoud 	if (l_ea + offset >= sector_size - sizeof(struct extattr_entry))
   2169      1.45   reinoud 		return EINVAL;
   2170      1.45   reinoud 
   2171      1.45   reinoud 	DPRINTF(EXTATTR, ("Starting at offset %d\n", offset));
   2172      1.45   reinoud 
   2173      1.45   reinoud 	/* find our extended attribute  */
   2174      1.45   reinoud 	l_ea -= offset;
   2175      1.45   reinoud 	pos = (uint8_t *) eahdr + offset;
   2176      1.45   reinoud 
   2177      1.45   reinoud 	while (l_ea >= sizeof(struct extattr_entry)) {
   2178      1.45   reinoud 		DPRINTF(EXTATTR, ("%d extended attr bytes left\n", l_ea));
   2179      1.45   reinoud 		attrhdr = (struct extattr_entry *) pos;
   2180      1.45   reinoud 		implext = (struct impl_extattr_entry *) pos;
   2181      1.45   reinoud 
   2182      1.45   reinoud 		/* get complete attribute length and check for roque values */
   2183      1.45   reinoud 		a_l = udf_rw32(attrhdr->a_l);
   2184      1.45   reinoud 		DPRINTF(EXTATTR, ("attribute %d:%d, len %d/%d\n",
   2185      1.45   reinoud 				udf_rw32(attrhdr->type),
   2186      1.45   reinoud 				attrhdr->subtype, a_l, l_ea));
   2187      1.45   reinoud 		if ((a_l == 0) || (a_l > l_ea))
   2188      1.45   reinoud 			return EINVAL;
   2189      1.45   reinoud 
   2190      1.45   reinoud 		if (attrhdr->type != sattr)
   2191      1.45   reinoud 			goto next_attribute;
   2192      1.45   reinoud 
   2193      1.45   reinoud 		/* we might have found it! */
   2194      1.45   reinoud 		if (attrhdr->type < 2048) {	/* Ecma-167 attribute */
   2195      1.45   reinoud 			*offsetp = offset;
   2196      1.45   reinoud 			*lengthp = a_l;
   2197      1.45   reinoud 			return 0;		/* success */
   2198      1.45   reinoud 		}
   2199      1.45   reinoud 
   2200      1.45   reinoud 		/*
   2201      1.45   reinoud 		 * Implementation use and application use extended attributes
   2202      1.45   reinoud 		 * have a name to identify. They share the same structure only
   2203      1.45   reinoud 		 * UDF implementation use extended attributes have a checksum
   2204      1.45   reinoud 		 * we need to check
   2205      1.45   reinoud 		 */
   2206      1.45   reinoud 
   2207      1.45   reinoud 		DPRINTF(EXTATTR, ("named attribute %s\n", implext->imp_id.id));
   2208      1.45   reinoud 		if (strcmp(implext->imp_id.id, sattrname) == 0) {
   2209      1.45   reinoud 			/* we have found our appl/implementation attribute */
   2210      1.45   reinoud 			*offsetp = offset;
   2211      1.45   reinoud 			*lengthp = a_l;
   2212      1.45   reinoud 			return 0;		/* success */
   2213      1.45   reinoud 		}
   2214      1.45   reinoud 
   2215      1.45   reinoud next_attribute:
   2216      1.45   reinoud 		/* next attribute */
   2217      1.45   reinoud 		pos    += a_l;
   2218      1.45   reinoud 		l_ea   -= a_l;
   2219      1.45   reinoud 		offset += a_l;
   2220      1.45   reinoud 	}
   2221      1.45   reinoud 	/* not found */
   2222      1.45   reinoud 	return ENOENT;
   2223      1.45   reinoud }
   2224      1.45   reinoud 
   2225      1.45   reinoud 
   2226  1.49.2.1    simonb static void
   2227  1.49.2.1    simonb udf_extattr_insert_internal(struct udf_mount *ump, union dscrptr *dscr,
   2228  1.49.2.1    simonb 	struct extattr_entry *extattr)
   2229  1.49.2.1    simonb {
   2230  1.49.2.1    simonb 	struct file_entry      *fe;
   2231  1.49.2.1    simonb 	struct extfile_entry   *efe;
   2232  1.49.2.1    simonb 	struct extattrhdr_desc *extattrhdr;
   2233  1.49.2.1    simonb 	struct impl_extattr_entry *implext;
   2234  1.49.2.1    simonb 	uint32_t impl_attr_loc, appl_attr_loc, l_ea, a_l, exthdr_len;
   2235  1.49.2.1    simonb 	uint32_t *l_eap, l_ad;
   2236  1.49.2.1    simonb 	uint16_t *spos;
   2237  1.49.2.1    simonb 	uint8_t *bpos, *data;
   2238  1.49.2.1    simonb 
   2239  1.49.2.1    simonb 	if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
   2240  1.49.2.1    simonb 		fe    = &dscr->fe;
   2241  1.49.2.1    simonb 		data  = fe->data;
   2242  1.49.2.1    simonb 		l_eap = &fe->l_ea;
   2243  1.49.2.1    simonb 		l_ad  = udf_rw32(fe->l_ad);
   2244  1.49.2.1    simonb 	} else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
   2245  1.49.2.1    simonb 		efe   = &dscr->efe;
   2246  1.49.2.1    simonb 		data  = efe->data;
   2247  1.49.2.1    simonb 		l_eap = &efe->l_ea;
   2248  1.49.2.1    simonb 		l_ad  = udf_rw32(efe->l_ad);
   2249  1.49.2.1    simonb 	} else {
   2250  1.49.2.1    simonb 		panic("Bad tag passed to udf_extattr_insert_internal");
   2251  1.49.2.1    simonb 	}
   2252  1.49.2.1    simonb 
   2253  1.49.2.1    simonb 	/* can't append already written to file descriptors yet */
   2254  1.49.2.1    simonb 	assert(l_ad == 0);
   2255  1.49.2.1    simonb 
   2256  1.49.2.1    simonb 	/* should have a header! */
   2257  1.49.2.1    simonb 	extattrhdr = (struct extattrhdr_desc *) data;
   2258  1.49.2.1    simonb 	l_ea = udf_rw32(*l_eap);
   2259  1.49.2.1    simonb 	if (l_ea == 0) {
   2260  1.49.2.1    simonb 		/* create empty extended attribute header */
   2261  1.49.2.1    simonb 		exthdr_len = sizeof(struct extattrhdr_desc);
   2262  1.49.2.1    simonb 
   2263  1.49.2.1    simonb 		udf_inittag(ump, &extattrhdr->tag, TAGID_EXTATTR_HDR,
   2264  1.49.2.1    simonb 			/* loc */ 0);
   2265  1.49.2.1    simonb 		extattrhdr->impl_attr_loc = udf_rw32(exthdr_len);
   2266  1.49.2.1    simonb 		extattrhdr->appl_attr_loc = udf_rw32(exthdr_len);
   2267  1.49.2.1    simonb 		extattrhdr->tag.desc_crc_len = udf_rw16(8);
   2268  1.49.2.1    simonb 
   2269  1.49.2.1    simonb 		/* record extended attribute header length */
   2270  1.49.2.1    simonb 		l_ea = exthdr_len;
   2271  1.49.2.1    simonb 		*l_eap = udf_rw32(l_ea);
   2272  1.49.2.1    simonb 	}
   2273  1.49.2.1    simonb 
   2274  1.49.2.1    simonb 	/* extract locations */
   2275  1.49.2.1    simonb 	impl_attr_loc = udf_rw32(extattrhdr->impl_attr_loc);
   2276  1.49.2.1    simonb 	appl_attr_loc = udf_rw32(extattrhdr->appl_attr_loc);
   2277  1.49.2.1    simonb 	if (impl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
   2278  1.49.2.1    simonb 		impl_attr_loc = l_ea;
   2279  1.49.2.1    simonb 	if (appl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
   2280  1.49.2.1    simonb 		appl_attr_loc = l_ea;
   2281  1.49.2.1    simonb 
   2282  1.49.2.1    simonb 	/* Ecma 167 EAs */
   2283  1.49.2.1    simonb 	if (udf_rw32(extattr->type) < 2048) {
   2284  1.49.2.1    simonb 		assert(impl_attr_loc == l_ea);
   2285  1.49.2.1    simonb 		assert(appl_attr_loc == l_ea);
   2286  1.49.2.1    simonb 	}
   2287  1.49.2.1    simonb 
   2288  1.49.2.1    simonb 	/* implementation use extended attributes */
   2289  1.49.2.1    simonb 	if (udf_rw32(extattr->type) == 2048) {
   2290  1.49.2.1    simonb 		assert(appl_attr_loc == l_ea);
   2291  1.49.2.1    simonb 
   2292  1.49.2.1    simonb 		/* calculate and write extended attribute header checksum */
   2293  1.49.2.1    simonb 		implext = (struct impl_extattr_entry *) extattr;
   2294  1.49.2.1    simonb 		assert(udf_rw32(implext->iu_l) == 4);	/* [UDF 3.3.4.5] */
   2295  1.49.2.1    simonb 		spos = (uint16_t *) implext->data;
   2296  1.49.2.1    simonb 		*spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
   2297  1.49.2.1    simonb 	}
   2298  1.49.2.1    simonb 
   2299  1.49.2.1    simonb 	/* application use extended attributes */
   2300  1.49.2.1    simonb 	assert(udf_rw32(extattr->type) != 65536);
   2301  1.49.2.1    simonb 	assert(appl_attr_loc == l_ea);
   2302  1.49.2.1    simonb 
   2303  1.49.2.1    simonb 	/* append the attribute at the end of the current space */
   2304  1.49.2.1    simonb 	bpos = data + udf_rw32(*l_eap);
   2305  1.49.2.1    simonb 	a_l  = udf_rw32(extattr->a_l);
   2306  1.49.2.1    simonb 
   2307  1.49.2.1    simonb 	/* update impl. attribute locations */
   2308  1.49.2.1    simonb 	if (udf_rw32(extattr->type) < 2048) {
   2309  1.49.2.1    simonb 		impl_attr_loc = l_ea + a_l;
   2310  1.49.2.1    simonb 		appl_attr_loc = l_ea + a_l;
   2311  1.49.2.1    simonb 	}
   2312  1.49.2.1    simonb 	if (udf_rw32(extattr->type) == 2048) {
   2313  1.49.2.1    simonb 		appl_attr_loc = l_ea + a_l;
   2314  1.49.2.1    simonb 	}
   2315  1.49.2.1    simonb 
   2316  1.49.2.1    simonb 	/* copy and advance */
   2317  1.49.2.1    simonb 	memcpy(bpos, extattr, a_l);
   2318  1.49.2.1    simonb 	l_ea += a_l;
   2319  1.49.2.1    simonb 	*l_eap = udf_rw32(l_ea);
   2320  1.49.2.1    simonb 
   2321  1.49.2.1    simonb 	/* do the `dance` again backwards */
   2322  1.49.2.1    simonb 	if (udf_rw16(ump->logical_vol->tag.descriptor_ver) != 2) {
   2323  1.49.2.1    simonb 		if (impl_attr_loc == l_ea)
   2324  1.49.2.1    simonb 			impl_attr_loc = UDF_IMPL_ATTR_LOC_NOT_PRESENT;
   2325  1.49.2.1    simonb 		if (appl_attr_loc == l_ea)
   2326  1.49.2.1    simonb 			appl_attr_loc = UDF_APPL_ATTR_LOC_NOT_PRESENT;
   2327  1.49.2.1    simonb 	}
   2328  1.49.2.1    simonb 
   2329  1.49.2.1    simonb 	/* store offsets */
   2330  1.49.2.1    simonb 	extattrhdr->impl_attr_loc = udf_rw32(impl_attr_loc);
   2331  1.49.2.1    simonb 	extattrhdr->appl_attr_loc = udf_rw32(appl_attr_loc);
   2332  1.49.2.1    simonb }
   2333  1.49.2.1    simonb 
   2334      1.45   reinoud 
   2335      1.45   reinoud /* --------------------------------------------------------------------- */
   2336      1.45   reinoud 
   2337      1.45   reinoud static int
   2338      1.45   reinoud udf_update_lvid_from_vat_extattr(struct udf_node *vat_node)
   2339      1.45   reinoud {
   2340      1.45   reinoud 	struct udf_mount       *ump;
   2341      1.45   reinoud 	struct udf_logvol_info *lvinfo;
   2342      1.45   reinoud 	struct impl_extattr_entry     *implext;
   2343      1.45   reinoud 	struct vatlvext_extattr_entry  lvext;
   2344      1.45   reinoud 	const char *extstr = "*UDF VAT LVExtension";
   2345      1.45   reinoud 	uint64_t    vat_uniqueid;
   2346      1.45   reinoud 	uint32_t    offset, a_l;
   2347      1.45   reinoud 	uint8_t    *ea_start, *lvextpos;
   2348      1.45   reinoud 	int         error;
   2349      1.45   reinoud 
   2350      1.45   reinoud 	/* get mountpoint and lvinfo */
   2351      1.45   reinoud 	ump    = vat_node->ump;
   2352      1.45   reinoud 	lvinfo = ump->logvol_info;
   2353      1.45   reinoud 
   2354      1.45   reinoud 	/* get information from fe/efe */
   2355      1.45   reinoud 	if (vat_node->fe) {
   2356      1.45   reinoud 		vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
   2357      1.45   reinoud 		ea_start     = vat_node->fe->data;
   2358      1.45   reinoud 	} else {
   2359      1.45   reinoud 		vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
   2360      1.45   reinoud 		ea_start     = vat_node->efe->data;
   2361      1.45   reinoud 	}
   2362      1.45   reinoud 
   2363      1.45   reinoud 	error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
   2364      1.45   reinoud 	if (error)
   2365      1.45   reinoud 		return error;
   2366      1.45   reinoud 
   2367      1.45   reinoud 	implext = (struct impl_extattr_entry *) (ea_start + offset);
   2368      1.45   reinoud 	error = udf_impl_extattr_check(implext);
   2369      1.45   reinoud 	if (error)
   2370      1.45   reinoud 		return error;
   2371      1.45   reinoud 
   2372      1.45   reinoud 	/* paranoia */
   2373      1.45   reinoud 	if (a_l != sizeof(*implext) -1 + udf_rw32(implext->iu_l) + sizeof(lvext)) {
   2374      1.45   reinoud 		DPRINTF(VOLUMES, ("VAT LVExtension size doesn't compute\n"));
   2375      1.45   reinoud 		return EINVAL;
   2376      1.45   reinoud 	}
   2377      1.45   reinoud 
   2378      1.45   reinoud 	/*
   2379      1.45   reinoud 	 * we have found our "VAT LVExtension attribute. BUT due to a
   2380      1.45   reinoud 	 * bug in the specification it might not be word aligned so
   2381      1.45   reinoud 	 * copy first to avoid panics on some machines (!!)
   2382      1.45   reinoud 	 */
   2383      1.45   reinoud 	DPRINTF(VOLUMES, ("Found VAT LVExtension attr\n"));
   2384      1.45   reinoud 	lvextpos = implext->data + udf_rw32(implext->iu_l);
   2385      1.45   reinoud 	memcpy(&lvext, lvextpos, sizeof(lvext));
   2386      1.45   reinoud 
   2387      1.45   reinoud 	/* check if it was updated the last time */
   2388      1.45   reinoud 	if (udf_rw64(lvext.unique_id_chk) == vat_uniqueid) {
   2389      1.45   reinoud 		lvinfo->num_files       = lvext.num_files;
   2390      1.45   reinoud 		lvinfo->num_directories = lvext.num_directories;
   2391      1.45   reinoud 		udf_update_logvolname(ump, lvext.logvol_id);
   2392      1.45   reinoud 	} else {
   2393      1.45   reinoud 		DPRINTF(VOLUMES, ("VAT LVExtension out of date\n"));
   2394      1.45   reinoud 		/* replace VAT LVExt by free space EA */
   2395      1.45   reinoud 		memset(implext->imp_id.id, 0, UDF_REGID_ID_SIZE);
   2396      1.45   reinoud 		strcpy(implext->imp_id.id, "*UDF FreeEASpace");
   2397      1.45   reinoud 		udf_calc_impl_extattr_checksum(implext);
   2398      1.45   reinoud 	}
   2399      1.45   reinoud 
   2400      1.45   reinoud 	return 0;
   2401      1.45   reinoud }
   2402      1.45   reinoud 
   2403      1.45   reinoud 
   2404      1.45   reinoud static int
   2405      1.45   reinoud udf_update_vat_extattr_from_lvid(struct udf_node *vat_node)
   2406      1.45   reinoud {
   2407      1.45   reinoud 	struct udf_mount       *ump;
   2408      1.45   reinoud 	struct udf_logvol_info *lvinfo;
   2409      1.45   reinoud 	struct impl_extattr_entry     *implext;
   2410      1.45   reinoud 	struct vatlvext_extattr_entry  lvext;
   2411      1.45   reinoud 	const char *extstr = "*UDF VAT LVExtension";
   2412      1.45   reinoud 	uint64_t    vat_uniqueid;
   2413      1.45   reinoud 	uint32_t    offset, a_l;
   2414      1.45   reinoud 	uint8_t    *ea_start, *lvextpos;
   2415      1.45   reinoud 	int         error;
   2416      1.45   reinoud 
   2417      1.45   reinoud 	/* get mountpoint and lvinfo */
   2418      1.45   reinoud 	ump    = vat_node->ump;
   2419      1.45   reinoud 	lvinfo = ump->logvol_info;
   2420      1.45   reinoud 
   2421      1.45   reinoud 	/* get information from fe/efe */
   2422      1.45   reinoud 	if (vat_node->fe) {
   2423      1.45   reinoud 		vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
   2424      1.45   reinoud 		ea_start     = vat_node->fe->data;
   2425      1.45   reinoud 	} else {
   2426      1.45   reinoud 		vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
   2427      1.45   reinoud 		ea_start     = vat_node->efe->data;
   2428      1.45   reinoud 	}
   2429      1.45   reinoud 
   2430      1.45   reinoud 	error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
   2431      1.45   reinoud 	if (error)
   2432      1.45   reinoud 		return error;
   2433      1.45   reinoud 	/* found, it existed */
   2434      1.45   reinoud 
   2435      1.45   reinoud 	/* paranoia */
   2436      1.45   reinoud 	implext = (struct impl_extattr_entry *) (ea_start + offset);
   2437      1.45   reinoud 	error = udf_impl_extattr_check(implext);
   2438      1.45   reinoud 	if (error) {
   2439      1.45   reinoud 		DPRINTF(VOLUMES, ("VAT LVExtension bad on update\n"));
   2440      1.45   reinoud 		return error;
   2441      1.45   reinoud 	}
   2442      1.45   reinoud 	/* it is correct */
   2443      1.45   reinoud 
   2444      1.45   reinoud 	/*
   2445      1.45   reinoud 	 * we have found our "VAT LVExtension attribute. BUT due to a
   2446      1.45   reinoud 	 * bug in the specification it might not be word aligned so
   2447      1.45   reinoud 	 * copy first to avoid panics on some machines (!!)
   2448      1.45   reinoud 	 */
   2449      1.45   reinoud 	DPRINTF(VOLUMES, ("Updating VAT LVExtension attr\n"));
   2450      1.45   reinoud 	lvextpos = implext->data + udf_rw32(implext->iu_l);
   2451      1.45   reinoud 
   2452      1.45   reinoud 	lvext.unique_id_chk   = vat_uniqueid;
   2453      1.45   reinoud 	lvext.num_files       = lvinfo->num_files;
   2454      1.45   reinoud 	lvext.num_directories = lvinfo->num_directories;
   2455      1.45   reinoud 	memmove(lvext.logvol_id, ump->logical_vol->logvol_id, 128);
   2456      1.45   reinoud 
   2457      1.45   reinoud 	memcpy(lvextpos, &lvext, sizeof(lvext));
   2458      1.45   reinoud 
   2459      1.45   reinoud 	return 0;
   2460      1.45   reinoud }
   2461      1.45   reinoud 
   2462      1.45   reinoud /* --------------------------------------------------------------------- */
   2463      1.45   reinoud 
   2464      1.45   reinoud int
   2465      1.45   reinoud udf_vat_read(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
   2466      1.45   reinoud {
   2467      1.45   reinoud 	struct udf_mount *ump = vat_node->ump;
   2468      1.45   reinoud 
   2469      1.45   reinoud 	if (offset + size > ump->vat_offset + ump->vat_entries * 4)
   2470      1.45   reinoud 		return EINVAL;
   2471      1.45   reinoud 
   2472      1.45   reinoud 	memcpy(blob, ump->vat_table + offset, size);
   2473      1.45   reinoud 	return 0;
   2474      1.45   reinoud }
   2475      1.45   reinoud 
   2476      1.45   reinoud int
   2477      1.45   reinoud udf_vat_write(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
   2478      1.45   reinoud {
   2479      1.45   reinoud 	struct udf_mount *ump = vat_node->ump;
   2480      1.45   reinoud 	uint32_t offset_high;
   2481      1.45   reinoud 	uint8_t *new_vat_table;
   2482      1.45   reinoud 
   2483      1.45   reinoud 	/* extent VAT allocation if needed */
   2484      1.45   reinoud 	offset_high = offset + size;
   2485      1.45   reinoud 	if (offset_high >= ump->vat_table_alloc_len) {
   2486      1.45   reinoud 		/* realloc */
   2487      1.45   reinoud 		new_vat_table = realloc(ump->vat_table,
   2488      1.45   reinoud 			ump->vat_table_alloc_len + UDF_VAT_CHUNKSIZE,
   2489      1.45   reinoud 			M_UDFVOLD, M_WAITOK | M_CANFAIL);
   2490      1.45   reinoud 		if (!new_vat_table) {
   2491      1.45   reinoud 			printf("udf_vat_write: can't extent VAT, out of mem\n");
   2492      1.45   reinoud 			return ENOMEM;
   2493      1.45   reinoud 		}
   2494      1.45   reinoud 		ump->vat_table = new_vat_table;
   2495      1.45   reinoud 		ump->vat_table_alloc_len += UDF_VAT_CHUNKSIZE;
   2496      1.45   reinoud 	}
   2497      1.45   reinoud 	ump->vat_table_len = MAX(ump->vat_table_len, offset_high);
   2498      1.45   reinoud 
   2499      1.45   reinoud 	memcpy(ump->vat_table + offset, blob, size);
   2500      1.45   reinoud 	return 0;
   2501      1.45   reinoud }
   2502      1.45   reinoud 
   2503      1.45   reinoud /* --------------------------------------------------------------------- */
   2504      1.45   reinoud 
   2505      1.45   reinoud /* TODO support previous VAT location writeout */
   2506      1.45   reinoud static int
   2507      1.45   reinoud udf_update_vat_descriptor(struct udf_mount *ump)
   2508      1.45   reinoud {
   2509      1.45   reinoud 	struct udf_node *vat_node = ump->vat_node;
   2510      1.45   reinoud 	struct udf_logvol_info *lvinfo = ump->logvol_info;
   2511      1.45   reinoud 	struct icb_tag *icbtag;
   2512      1.45   reinoud 	struct udf_oldvat_tail *oldvat_tl;
   2513      1.45   reinoud 	struct udf_vat *vat;
   2514      1.45   reinoud 	uint64_t unique_id;
   2515      1.45   reinoud 	uint32_t lb_size;
   2516      1.45   reinoud 	uint8_t *raw_vat;
   2517      1.45   reinoud 	int filetype, error;
   2518      1.45   reinoud 
   2519      1.45   reinoud 	KASSERT(vat_node);
   2520      1.45   reinoud 	KASSERT(lvinfo);
   2521      1.45   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   2522      1.45   reinoud 
   2523      1.45   reinoud 	/* get our new unique_id */
   2524      1.45   reinoud 	unique_id = udf_advance_uniqueid(ump);
   2525      1.45   reinoud 
   2526      1.45   reinoud 	/* get information from fe/efe */
   2527      1.45   reinoud 	if (vat_node->fe) {
   2528      1.45   reinoud 		icbtag    = &vat_node->fe->icbtag;
   2529      1.45   reinoud 		vat_node->fe->unique_id = udf_rw64(unique_id);
   2530      1.45   reinoud 	} else {
   2531      1.45   reinoud 		icbtag = &vat_node->efe->icbtag;
   2532      1.45   reinoud 		vat_node->efe->unique_id = udf_rw64(unique_id);
   2533      1.45   reinoud 	}
   2534      1.45   reinoud 
   2535      1.45   reinoud 	/* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
   2536      1.45   reinoud 	filetype = icbtag->file_type;
   2537      1.45   reinoud 	KASSERT((filetype == 0) || (filetype == UDF_ICB_FILETYPE_VAT));
   2538      1.45   reinoud 
   2539      1.45   reinoud 	/* allocate piece to process head or tail of VAT file */
   2540      1.45   reinoud 	raw_vat = malloc(lb_size, M_TEMP, M_WAITOK);
   2541      1.45   reinoud 
   2542      1.45   reinoud 	if (filetype == 0) {
   2543      1.45   reinoud 		/*
   2544      1.45   reinoud 		 * Update "*UDF VAT LVExtension" extended attribute from the
   2545      1.45   reinoud 		 * lvint if present.
   2546      1.45   reinoud 		 */
   2547      1.45   reinoud 		udf_update_vat_extattr_from_lvid(vat_node);
   2548      1.45   reinoud 
   2549      1.45   reinoud 		/* setup identifying regid */
   2550      1.45   reinoud 		oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
   2551      1.45   reinoud 		memset(oldvat_tl, 0, sizeof(struct udf_oldvat_tail));
   2552      1.45   reinoud 
   2553      1.45   reinoud 		udf_set_regid(&oldvat_tl->id, "*UDF Virtual Alloc Tbl");
   2554      1.45   reinoud 		udf_add_udf_regid(ump, &oldvat_tl->id);
   2555      1.45   reinoud 		oldvat_tl->prev_vat = udf_rw32(0xffffffff);
   2556      1.45   reinoud 
   2557      1.45   reinoud 		/* write out new tail of virtual allocation table file */
   2558      1.45   reinoud 		error = udf_vat_write(vat_node, raw_vat,
   2559      1.45   reinoud 			sizeof(struct udf_oldvat_tail), ump->vat_entries * 4);
   2560      1.45   reinoud 	} else {
   2561      1.45   reinoud 		/* compose the VAT2 header */
   2562      1.45   reinoud 		vat = (struct udf_vat *) raw_vat;
   2563      1.45   reinoud 		memset(vat, 0, sizeof(struct udf_vat));
   2564      1.45   reinoud 
   2565      1.45   reinoud 		vat->header_len       = udf_rw16(152);	/* as per spec */
   2566      1.45   reinoud 		vat->impl_use_len     = udf_rw16(0);
   2567      1.45   reinoud 		memmove(vat->logvol_id, ump->logical_vol->logvol_id, 128);
   2568      1.45   reinoud 		vat->prev_vat         = udf_rw32(0xffffffff);
   2569      1.45   reinoud 		vat->num_files        = lvinfo->num_files;
   2570      1.45   reinoud 		vat->num_directories  = lvinfo->num_directories;
   2571      1.45   reinoud 		vat->min_udf_readver  = lvinfo->min_udf_readver;
   2572      1.45   reinoud 		vat->min_udf_writever = lvinfo->min_udf_writever;
   2573      1.45   reinoud 		vat->max_udf_writever = lvinfo->max_udf_writever;
   2574      1.45   reinoud 
   2575      1.45   reinoud 		error = udf_vat_write(vat_node, raw_vat,
   2576      1.45   reinoud 			sizeof(struct udf_vat), 0);
   2577      1.45   reinoud 	}
   2578      1.45   reinoud 	free(raw_vat, M_TEMP);
   2579      1.45   reinoud 
   2580      1.45   reinoud 	return error;	/* success! */
   2581      1.45   reinoud }
   2582      1.45   reinoud 
   2583      1.45   reinoud 
   2584      1.45   reinoud int
   2585      1.45   reinoud udf_writeout_vat(struct udf_mount *ump)
   2586      1.45   reinoud {
   2587      1.45   reinoud 	struct udf_node *vat_node = ump->vat_node;
   2588      1.45   reinoud 	uint32_t vat_length;
   2589      1.45   reinoud 	int error;
   2590      1.45   reinoud 
   2591      1.45   reinoud 	KASSERT(vat_node);
   2592      1.45   reinoud 
   2593      1.45   reinoud 	DPRINTF(CALL, ("udf_writeout_vat\n"));
   2594      1.45   reinoud 
   2595      1.45   reinoud 	mutex_enter(&ump->allocate_mutex);
   2596      1.45   reinoud 	udf_update_vat_descriptor(ump);
   2597      1.45   reinoud 
   2598      1.45   reinoud 	/* write out the VAT contents ; TODO intelligent writing */
   2599      1.45   reinoud 	vat_length = ump->vat_table_len;
   2600      1.45   reinoud 	error = vn_rdwr(UIO_WRITE, vat_node->vnode,
   2601      1.45   reinoud 		ump->vat_table, ump->vat_table_len, 0,
   2602      1.45   reinoud 		UIO_SYSSPACE, IO_NODELOCKED, FSCRED, NULL, NULL);
   2603      1.45   reinoud 	if (error) {
   2604      1.45   reinoud 		printf("udf_writeout_vat: failed to write out VAT contents\n");
   2605      1.45   reinoud 		goto out;
   2606      1.45   reinoud 	}
   2607      1.45   reinoud 
   2608      1.45   reinoud 	mutex_exit(&ump->allocate_mutex);
   2609      1.45   reinoud 
   2610      1.45   reinoud 	vflushbuf(ump->vat_node->vnode, 1 /* sync */);
   2611      1.45   reinoud 	error = VOP_FSYNC(ump->vat_node->vnode,
   2612      1.45   reinoud 			FSCRED, FSYNC_WAIT, 0, 0);
   2613      1.45   reinoud 	if (error)
   2614      1.45   reinoud 		printf("udf_writeout_vat: error writing VAT node!\n");
   2615      1.45   reinoud out:
   2616      1.45   reinoud 
   2617      1.45   reinoud 	return error;
   2618      1.45   reinoud }
   2619      1.45   reinoud 
   2620      1.45   reinoud /* --------------------------------------------------------------------- */
   2621      1.45   reinoud 
   2622      1.45   reinoud /*
   2623      1.45   reinoud  * Read in relevant pieces of VAT file and check if its indeed a VAT file
   2624      1.45   reinoud  * descriptor. If OK, read in complete VAT file.
   2625      1.45   reinoud  */
   2626      1.45   reinoud 
   2627      1.45   reinoud static int
   2628      1.45   reinoud udf_check_for_vat(struct udf_node *vat_node)
   2629      1.45   reinoud {
   2630      1.45   reinoud 	struct udf_mount *ump;
   2631      1.45   reinoud 	struct icb_tag   *icbtag;
   2632      1.45   reinoud 	struct timestamp *mtime;
   2633      1.45   reinoud 	struct udf_vat   *vat;
   2634      1.45   reinoud 	struct udf_oldvat_tail *oldvat_tl;
   2635      1.45   reinoud 	struct udf_logvol_info *lvinfo;
   2636      1.45   reinoud 	uint64_t  unique_id;
   2637      1.45   reinoud 	uint32_t  vat_length;
   2638      1.45   reinoud 	uint32_t  vat_offset, vat_entries, vat_table_alloc_len;
   2639      1.45   reinoud 	uint32_t  sector_size;
   2640      1.45   reinoud 	uint32_t *raw_vat;
   2641      1.45   reinoud 	uint8_t  *vat_table;
   2642      1.45   reinoud 	char     *regid_name;
   2643      1.45   reinoud 	int filetype;
   2644      1.45   reinoud 	int error;
   2645      1.45   reinoud 
   2646      1.45   reinoud 	/* vat_length is really 64 bits though impossible */
   2647      1.45   reinoud 
   2648      1.45   reinoud 	DPRINTF(VOLUMES, ("Checking for VAT\n"));
   2649      1.45   reinoud 	if (!vat_node)
   2650      1.45   reinoud 		return ENOENT;
   2651      1.45   reinoud 
   2652      1.45   reinoud 	/* get mount info */
   2653      1.45   reinoud 	ump = vat_node->ump;
   2654      1.45   reinoud 	sector_size = udf_rw32(ump->logical_vol->lb_size);
   2655      1.45   reinoud 
   2656      1.45   reinoud 	/* check assertions */
   2657      1.45   reinoud 	assert(vat_node->fe || vat_node->efe);
   2658      1.45   reinoud 	assert(ump->logvol_integrity);
   2659      1.45   reinoud 
   2660      1.45   reinoud 	/* set vnode type to regular file or we can't read from it! */
   2661      1.45   reinoud 	vat_node->vnode->v_type = VREG;
   2662      1.45   reinoud 
   2663      1.45   reinoud 	/* get information from fe/efe */
   2664      1.45   reinoud 	if (vat_node->fe) {
   2665      1.45   reinoud 		vat_length = udf_rw64(vat_node->fe->inf_len);
   2666      1.45   reinoud 		icbtag    = &vat_node->fe->icbtag;
   2667      1.45   reinoud 		mtime     = &vat_node->fe->mtime;
   2668      1.45   reinoud 		unique_id = udf_rw64(vat_node->fe->unique_id);
   2669      1.45   reinoud 	} else {
   2670      1.45   reinoud 		vat_length = udf_rw64(vat_node->efe->inf_len);
   2671      1.45   reinoud 		icbtag = &vat_node->efe->icbtag;
   2672      1.45   reinoud 		mtime  = &vat_node->efe->mtime;
   2673      1.45   reinoud 		unique_id = udf_rw64(vat_node->efe->unique_id);
   2674      1.45   reinoud 	}
   2675      1.45   reinoud 
   2676      1.45   reinoud 	/* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
   2677      1.45   reinoud 	filetype = icbtag->file_type;
   2678      1.45   reinoud 	if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT))
   2679      1.45   reinoud 		return ENOENT;
   2680      1.45   reinoud 
   2681      1.45   reinoud 	DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length));
   2682      1.45   reinoud 
   2683      1.45   reinoud 	vat_table_alloc_len =
   2684      1.45   reinoud 		((vat_length + UDF_VAT_CHUNKSIZE-1) / UDF_VAT_CHUNKSIZE)
   2685      1.45   reinoud 			* UDF_VAT_CHUNKSIZE;
   2686      1.45   reinoud 
   2687      1.45   reinoud 	vat_table = malloc(vat_table_alloc_len, M_UDFVOLD,
   2688      1.45   reinoud 		M_CANFAIL | M_WAITOK);
   2689      1.45   reinoud 	if (vat_table == NULL) {
   2690      1.45   reinoud 		printf("allocation of %d bytes failed for VAT\n",
   2691      1.45   reinoud 			vat_table_alloc_len);
   2692      1.45   reinoud 		return ENOMEM;
   2693      1.45   reinoud 	}
   2694      1.45   reinoud 
   2695      1.45   reinoud 	/* allocate piece to read in head or tail of VAT file */
   2696      1.45   reinoud 	raw_vat = malloc(sector_size, M_TEMP, M_WAITOK);
   2697      1.45   reinoud 
   2698      1.45   reinoud 	/*
   2699      1.45   reinoud 	 * check contents of the file if its the old 1.50 VAT table format.
   2700      1.45   reinoud 	 * Its notoriously broken and allthough some implementations support an
   2701      1.45   reinoud 	 * extention as defined in the UDF 1.50 errata document, its doubtfull
   2702      1.45   reinoud 	 * to be useable since a lot of implementations don't maintain it.
   2703      1.45   reinoud 	 */
   2704      1.45   reinoud 	lvinfo = ump->logvol_info;
   2705      1.45   reinoud 
   2706      1.45   reinoud 	if (filetype == 0) {
   2707      1.45   reinoud 		/* definition */
   2708      1.45   reinoud 		vat_offset  = 0;
   2709      1.45   reinoud 		vat_entries = (vat_length-36)/4;
   2710      1.45   reinoud 
   2711      1.45   reinoud 		/* read in tail of virtual allocation table file */
   2712      1.45   reinoud 		error = vn_rdwr(UIO_READ, vat_node->vnode,
   2713      1.45   reinoud 				(uint8_t *) raw_vat,
   2714      1.45   reinoud 				sizeof(struct udf_oldvat_tail),
   2715      1.45   reinoud 				vat_entries * 4,
   2716      1.45   reinoud 				UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
   2717      1.45   reinoud 				NULL, NULL);
   2718      1.45   reinoud 		if (error)
   2719      1.45   reinoud 			goto out;
   2720      1.45   reinoud 
   2721      1.45   reinoud 		/* check 1.50 VAT */
   2722      1.45   reinoud 		oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
   2723      1.45   reinoud 		regid_name = (char *) oldvat_tl->id.id;
   2724      1.45   reinoud 		error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22);
   2725      1.45   reinoud 		if (error) {
   2726      1.45   reinoud 			DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n"));
   2727      1.45   reinoud 			error = ENOENT;
   2728      1.45   reinoud 			goto out;
   2729      1.45   reinoud 		}
   2730      1.45   reinoud 
   2731      1.45   reinoud 		/*
   2732      1.45   reinoud 		 * update LVID from "*UDF VAT LVExtension" extended attribute
   2733      1.45   reinoud 		 * if present.
   2734      1.45   reinoud 		 */
   2735      1.45   reinoud 		udf_update_lvid_from_vat_extattr(vat_node);
   2736      1.45   reinoud 	} else {
   2737      1.45   reinoud 		/* read in head of virtual allocation table file */
   2738      1.45   reinoud 		error = vn_rdwr(UIO_READ, vat_node->vnode,
   2739      1.45   reinoud 				(uint8_t *) raw_vat,
   2740      1.45   reinoud 				sizeof(struct udf_vat), 0,
   2741      1.45   reinoud 				UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
   2742      1.45   reinoud 				NULL, NULL);
   2743      1.45   reinoud 		if (error)
   2744      1.45   reinoud 			goto out;
   2745      1.45   reinoud 
   2746      1.45   reinoud 		/* definition */
   2747      1.45   reinoud 		vat = (struct udf_vat *) raw_vat;
   2748      1.45   reinoud 		vat_offset  = vat->header_len;
   2749      1.45   reinoud 		vat_entries = (vat_length - vat_offset)/4;
   2750      1.45   reinoud 
   2751      1.45   reinoud 		assert(lvinfo);
   2752      1.45   reinoud 		lvinfo->num_files        = vat->num_files;
   2753      1.45   reinoud 		lvinfo->num_directories  = vat->num_directories;
   2754      1.45   reinoud 		lvinfo->min_udf_readver  = vat->min_udf_readver;
   2755      1.45   reinoud 		lvinfo->min_udf_writever = vat->min_udf_writever;
   2756      1.45   reinoud 		lvinfo->max_udf_writever = vat->max_udf_writever;
   2757      1.45   reinoud 
   2758      1.45   reinoud 		udf_update_logvolname(ump, vat->logvol_id);
   2759      1.45   reinoud 	}
   2760      1.45   reinoud 
   2761      1.45   reinoud 	/* read in complete VAT file */
   2762      1.45   reinoud 	error = vn_rdwr(UIO_READ, vat_node->vnode,
   2763      1.45   reinoud 			vat_table,
   2764      1.45   reinoud 			vat_length, 0,
   2765      1.45   reinoud 			UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
   2766      1.45   reinoud 			NULL, NULL);
   2767      1.45   reinoud 	if (error)
   2768      1.45   reinoud 		printf("read in of complete VAT file failed (error %d)\n",
   2769      1.45   reinoud 			error);
   2770      1.45   reinoud 	if (error)
   2771      1.45   reinoud 		goto out;
   2772      1.45   reinoud 
   2773      1.45   reinoud 	DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n"));
   2774      1.45   reinoud 	ump->logvol_integrity->lvint_next_unique_id = unique_id;
   2775      1.45   reinoud 	ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
   2776      1.45   reinoud 	ump->logvol_integrity->time           = *mtime;
   2777      1.45   reinoud 
   2778      1.45   reinoud 	ump->vat_table_len = vat_length;
   2779      1.45   reinoud 	ump->vat_table_alloc_len = vat_table_alloc_len;
   2780      1.45   reinoud 	ump->vat_table   = vat_table;
   2781      1.45   reinoud 	ump->vat_offset  = vat_offset;
   2782      1.45   reinoud 	ump->vat_entries = vat_entries;
   2783      1.45   reinoud 	ump->vat_last_free_lb = 0;		/* start at beginning */
   2784      1.45   reinoud 
   2785      1.45   reinoud out:
   2786      1.45   reinoud 	if (error) {
   2787      1.45   reinoud 		if (vat_table)
   2788      1.45   reinoud 			free(vat_table, M_UDFVOLD);
   2789      1.45   reinoud 	}
   2790      1.45   reinoud 	free(raw_vat, M_TEMP);
   2791      1.45   reinoud 
   2792      1.45   reinoud 	return error;
   2793      1.45   reinoud }
   2794      1.45   reinoud 
   2795      1.45   reinoud /* --------------------------------------------------------------------- */
   2796      1.45   reinoud 
   2797      1.45   reinoud static int
   2798      1.45   reinoud udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping)
   2799      1.45   reinoud {
   2800      1.45   reinoud 	struct udf_node *vat_node;
   2801      1.45   reinoud 	struct long_ad	 icb_loc;
   2802      1.45   reinoud 	uint32_t early_vat_loc, late_vat_loc, vat_loc;
   2803      1.45   reinoud 	int error;
   2804      1.45   reinoud 
   2805      1.45   reinoud 	/* mapping info not needed */
   2806      1.45   reinoud 	mapping = mapping;
   2807      1.45   reinoud 
   2808      1.45   reinoud 	vat_loc = ump->last_possible_vat_location;
   2809      1.45   reinoud 	early_vat_loc = vat_loc - 256;	/* 8 blocks of 32 sectors */
   2810      1.45   reinoud 
   2811      1.45   reinoud 	DPRINTF(VOLUMES, ("1) last possible %d, early_vat_loc %d \n",
   2812      1.45   reinoud 		vat_loc, early_vat_loc));
   2813      1.45   reinoud 	early_vat_loc = MAX(early_vat_loc, ump->first_possible_vat_location);
   2814      1.45   reinoud 	late_vat_loc  = vat_loc + 1024;
   2815      1.45   reinoud 
   2816      1.45   reinoud 	DPRINTF(VOLUMES, ("2) last possible %d, early_vat_loc %d \n",
   2817      1.45   reinoud 		vat_loc, early_vat_loc));
   2818      1.45   reinoud 
   2819      1.45   reinoud 	/* start looking from the end of the range */
   2820      1.45   reinoud 	do {
   2821      1.45   reinoud 		DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc));
   2822      1.45   reinoud 		icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART);
   2823      1.45   reinoud 		icb_loc.loc.lb_num   = udf_rw32(vat_loc);
   2824      1.45   reinoud 
   2825      1.45   reinoud 		error = udf_get_node(ump, &icb_loc, &vat_node);
   2826      1.45   reinoud 		if (!error) {
   2827      1.45   reinoud 			error = udf_check_for_vat(vat_node);
   2828      1.45   reinoud 			DPRINTFIF(VOLUMES, !error,
   2829      1.45   reinoud 				("VAT accepted at %d\n", vat_loc));
   2830      1.45   reinoud 			if (!error)
   2831      1.45   reinoud 				break;
   2832      1.45   reinoud 		}
   2833      1.45   reinoud 		if (vat_node) {
   2834      1.45   reinoud 			vput(vat_node->vnode);
   2835      1.45   reinoud 			vat_node = NULL;
   2836      1.45   reinoud 		}
   2837      1.45   reinoud 		vat_loc--;	/* walk backwards */
   2838      1.45   reinoud 	} while (vat_loc >= early_vat_loc);
   2839      1.45   reinoud 
   2840      1.45   reinoud 	/* keep our VAT node around */
   2841      1.45   reinoud 	if (vat_node) {
   2842      1.45   reinoud 		UDF_SET_SYSTEMFILE(vat_node->vnode);
   2843      1.45   reinoud 		ump->vat_node = vat_node;
   2844      1.45   reinoud 	}
   2845      1.45   reinoud 
   2846      1.45   reinoud 	return error;
   2847      1.45   reinoud }
   2848      1.45   reinoud 
   2849      1.45   reinoud /* --------------------------------------------------------------------- */
   2850      1.45   reinoud 
   2851      1.45   reinoud static int
   2852      1.45   reinoud udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
   2853      1.45   reinoud {
   2854      1.45   reinoud 	union dscrptr *dscr;
   2855      1.45   reinoud 	struct part_map_spare *pms = &mapping->pms;
   2856      1.45   reinoud 	uint32_t lb_num;
   2857      1.45   reinoud 	int spar, error;
   2858      1.45   reinoud 
   2859      1.45   reinoud 	/*
   2860      1.45   reinoud 	 * The partition mapping passed on to us specifies the information we
   2861      1.45   reinoud 	 * need to locate and initialise the sparable partition mapping
   2862      1.45   reinoud 	 * information we need.
   2863      1.45   reinoud 	 */
   2864      1.45   reinoud 
   2865      1.45   reinoud 	DPRINTF(VOLUMES, ("Read sparable table\n"));
   2866      1.45   reinoud 	ump->sparable_packet_size = udf_rw16(pms->packet_len);
   2867      1.45   reinoud 	KASSERT(ump->sparable_packet_size >= ump->packet_size);	/* XXX */
   2868      1.45   reinoud 
   2869      1.45   reinoud 	for (spar = 0; spar < pms->n_st; spar++) {
   2870      1.45   reinoud 		lb_num = pms->st_loc[spar];
   2871      1.45   reinoud 		DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
   2872      1.45   reinoud 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
   2873      1.45   reinoud 		if (!error && dscr) {
   2874      1.45   reinoud 			if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
   2875      1.45   reinoud 				if (ump->sparing_table)
   2876      1.45   reinoud 					free(ump->sparing_table, M_UDFVOLD);
   2877      1.45   reinoud 				ump->sparing_table = &dscr->spt;
   2878      1.45   reinoud 				dscr = NULL;
   2879      1.45   reinoud 				DPRINTF(VOLUMES,
   2880      1.45   reinoud 				    ("Sparing table accepted (%d entries)\n",
   2881      1.45   reinoud 				     udf_rw16(ump->sparing_table->rt_l)));
   2882      1.45   reinoud 				break;	/* we're done */
   2883      1.45   reinoud 			}
   2884      1.45   reinoud 		}
   2885      1.45   reinoud 		if (dscr)
   2886      1.45   reinoud 			free(dscr, M_UDFVOLD);
   2887      1.45   reinoud 	}
   2888      1.45   reinoud 
   2889      1.45   reinoud 	if (ump->sparing_table)
   2890      1.45   reinoud 		return 0;
   2891      1.45   reinoud 
   2892      1.45   reinoud 	return ENOENT;
   2893      1.45   reinoud }
   2894      1.45   reinoud 
   2895      1.45   reinoud /* --------------------------------------------------------------------- */
   2896      1.45   reinoud 
   2897      1.45   reinoud static int
   2898      1.45   reinoud udf_read_metadata_nodes(struct udf_mount *ump, union udf_pmap *mapping)
   2899      1.45   reinoud {
   2900      1.45   reinoud 	struct part_map_meta *pmm = &mapping->pmm;
   2901      1.45   reinoud 	struct long_ad	 icb_loc;
   2902      1.45   reinoud 	struct vnode *vp;
   2903      1.45   reinoud 	int error;
   2904      1.45   reinoud 
   2905      1.45   reinoud 	DPRINTF(VOLUMES, ("Reading in Metadata files\n"));
   2906      1.45   reinoud 	icb_loc.loc.part_num = pmm->part_num;
   2907      1.45   reinoud 	icb_loc.loc.lb_num   = pmm->meta_file_lbn;
   2908      1.45   reinoud 	DPRINTF(VOLUMES, ("Metadata file\n"));
   2909      1.45   reinoud 	error = udf_get_node(ump, &icb_loc, &ump->metadata_node);
   2910      1.45   reinoud 	if (ump->metadata_node) {
   2911      1.45   reinoud 		vp = ump->metadata_node->vnode;
   2912      1.45   reinoud 		UDF_SET_SYSTEMFILE(vp);
   2913      1.45   reinoud 	}
   2914      1.45   reinoud 
   2915      1.45   reinoud 	icb_loc.loc.lb_num   = pmm->meta_mirror_file_lbn;
   2916      1.45   reinoud 	if (icb_loc.loc.lb_num != -1) {
   2917      1.45   reinoud 		DPRINTF(VOLUMES, ("Metadata copy file\n"));
   2918      1.45   reinoud 		error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_node);
   2919      1.45   reinoud 		if (ump->metadatamirror_node) {
   2920      1.45   reinoud 			vp = ump->metadatamirror_node->vnode;
   2921      1.45   reinoud 			UDF_SET_SYSTEMFILE(vp);
   2922      1.45   reinoud 		}
   2923      1.45   reinoud 	}
   2924      1.45   reinoud 
   2925      1.45   reinoud 	icb_loc.loc.lb_num   = pmm->meta_bitmap_file_lbn;
   2926      1.45   reinoud 	if (icb_loc.loc.lb_num != -1) {
   2927      1.45   reinoud 		DPRINTF(VOLUMES, ("Metadata bitmap file\n"));
   2928      1.45   reinoud 		error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_node);
   2929      1.45   reinoud 		if (ump->metadatabitmap_node) {
   2930      1.45   reinoud 			vp = ump->metadatabitmap_node->vnode;
   2931      1.45   reinoud 			UDF_SET_SYSTEMFILE(vp);
   2932      1.45   reinoud 		}
   2933      1.45   reinoud 	}
   2934      1.45   reinoud 
   2935      1.45   reinoud 	/* if we're mounting read-only we relax the requirements */
   2936      1.45   reinoud 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) {
   2937      1.45   reinoud 		error = EFAULT;
   2938      1.45   reinoud 		if (ump->metadata_node)
   2939      1.45   reinoud 			error = 0;
   2940      1.45   reinoud 		if ((ump->metadata_node == NULL) && (ump->metadatamirror_node)) {
   2941      1.45   reinoud 			printf( "udf mount: Metadata file not readable, "
   2942      1.45   reinoud 				"substituting Metadata copy file\n");
   2943      1.45   reinoud 			ump->metadata_node = ump->metadatamirror_node;
   2944      1.45   reinoud 			ump->metadatamirror_node = NULL;
   2945      1.45   reinoud 			error = 0;
   2946      1.45   reinoud 		}
   2947      1.45   reinoud 	} else {
   2948      1.45   reinoud 		/* mounting read/write */
   2949  1.49.2.1    simonb /*		if (error) */
   2950  1.49.2.1    simonb 			error = EROFS;
   2951      1.45   reinoud 	}
   2952      1.45   reinoud 	DPRINTFIF(VOLUMES, error, ("udf mount: failed to read "
   2953      1.45   reinoud 				   "metadata files\n"));
   2954      1.45   reinoud 	return error;
   2955      1.45   reinoud }
   2956      1.45   reinoud 
   2957      1.45   reinoud /* --------------------------------------------------------------------- */
   2958      1.45   reinoud 
   2959      1.45   reinoud int
   2960      1.45   reinoud udf_read_vds_tables(struct udf_mount *ump)
   2961      1.45   reinoud {
   2962      1.45   reinoud 	union udf_pmap *mapping;
   2963      1.45   reinoud 	/* struct udf_args *args = &ump->mount_args; */
   2964      1.45   reinoud 	uint32_t n_pm, mt_l;
   2965      1.45   reinoud 	uint32_t log_part;
   2966      1.45   reinoud 	uint8_t *pmap_pos;
   2967      1.45   reinoud 	int pmap_size;
   2968      1.45   reinoud 	int error;
   2969      1.45   reinoud 
   2970      1.45   reinoud 	/* Iterate again over the part mappings for locations   */
   2971      1.45   reinoud 	n_pm = udf_rw32(ump->logical_vol->n_pm);   /* num partmaps         */
   2972      1.45   reinoud 	mt_l = udf_rw32(ump->logical_vol->mt_l);   /* partmaps data length */
   2973      1.45   reinoud 	pmap_pos =  ump->logical_vol->maps;
   2974      1.45   reinoud 
   2975      1.45   reinoud 	for (log_part = 0; log_part < n_pm; log_part++) {
   2976      1.45   reinoud 		mapping = (union udf_pmap *) pmap_pos;
   2977      1.45   reinoud 		switch (ump->vtop_tp[log_part]) {
   2978      1.45   reinoud 		case UDF_VTOP_TYPE_PHYS :
   2979      1.45   reinoud 			/* nothing */
   2980      1.45   reinoud 			break;
   2981      1.45   reinoud 		case UDF_VTOP_TYPE_VIRT :
   2982      1.45   reinoud 			/* search and load VAT */
   2983      1.45   reinoud 			error = udf_search_vat(ump, mapping);
   2984      1.45   reinoud 			if (error)
   2985      1.45   reinoud 				return ENOENT;
   2986      1.45   reinoud 			break;
   2987      1.45   reinoud 		case UDF_VTOP_TYPE_SPARABLE :
   2988      1.45   reinoud 			/* load one of the sparable tables */
   2989      1.45   reinoud 			error = udf_read_sparables(ump, mapping);
   2990      1.45   reinoud 			if (error)
   2991      1.45   reinoud 				return ENOENT;
   2992      1.45   reinoud 			break;
   2993      1.45   reinoud 		case UDF_VTOP_TYPE_META :
   2994      1.45   reinoud 			/* load the associated file descriptors */
   2995      1.45   reinoud 			error = udf_read_metadata_nodes(ump, mapping);
   2996      1.45   reinoud 			if (error)
   2997      1.45   reinoud 				return ENOENT;
   2998      1.45   reinoud 			break;
   2999      1.45   reinoud 		default:
   3000      1.45   reinoud 			break;
   3001      1.45   reinoud 		}
   3002      1.45   reinoud 		pmap_size  = pmap_pos[1];
   3003      1.45   reinoud 		pmap_pos  += pmap_size;
   3004      1.45   reinoud 	}
   3005      1.45   reinoud 
   3006      1.45   reinoud 	return 0;
   3007      1.45   reinoud }
   3008      1.45   reinoud 
   3009      1.45   reinoud /* --------------------------------------------------------------------- */
   3010      1.45   reinoud 
   3011      1.45   reinoud int
   3012      1.45   reinoud udf_read_rootdirs(struct udf_mount *ump)
   3013      1.45   reinoud {
   3014      1.45   reinoud 	union dscrptr *dscr;
   3015      1.45   reinoud 	/* struct udf_args *args = &ump->mount_args; */
   3016      1.45   reinoud 	struct udf_node *rootdir_node, *streamdir_node;
   3017      1.45   reinoud 	struct long_ad  fsd_loc, *dir_loc;
   3018      1.45   reinoud 	uint32_t lb_num, dummy;
   3019      1.45   reinoud 	uint32_t fsd_len;
   3020      1.45   reinoud 	int dscr_type;
   3021      1.45   reinoud 	int error;
   3022      1.45   reinoud 
   3023      1.45   reinoud 	/* TODO implement FSD reading in separate function like integrity? */
   3024      1.45   reinoud 	/* get fileset descriptor sequence */
   3025      1.45   reinoud 	fsd_loc = ump->logical_vol->lv_fsd_loc;
   3026      1.45   reinoud 	fsd_len = udf_rw32(fsd_loc.len);
   3027      1.45   reinoud 
   3028      1.45   reinoud 	dscr  = NULL;
   3029      1.45   reinoud 	error = 0;
   3030      1.45   reinoud 	while (fsd_len || error) {
   3031      1.45   reinoud 		DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
   3032      1.45   reinoud 		/* translate fsd_loc to lb_num */
   3033      1.45   reinoud 		error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
   3034      1.45   reinoud 		if (error)
   3035      1.45   reinoud 			break;
   3036      1.45   reinoud 		DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
   3037      1.45   reinoud 		error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
   3038      1.45   reinoud 		/* end markers */
   3039      1.45   reinoud 		if (error || (dscr == NULL))
   3040      1.45   reinoud 			break;
   3041      1.45   reinoud 
   3042      1.45   reinoud 		/* analyse */
   3043      1.45   reinoud 		dscr_type = udf_rw16(dscr->tag.id);
   3044      1.45   reinoud 		if (dscr_type == TAGID_TERM)
   3045      1.45   reinoud 			break;
   3046      1.45   reinoud 		if (dscr_type != TAGID_FSD) {
   3047      1.45   reinoud 			free(dscr, M_UDFVOLD);
   3048      1.45   reinoud 			return ENOENT;
   3049      1.45   reinoud 		}
   3050      1.45   reinoud 
   3051      1.45   reinoud 		/*
   3052      1.45   reinoud 		 * TODO check for multiple fileset descriptors; its only
   3053      1.45   reinoud 		 * picking the last now. Also check for FSD
   3054      1.45   reinoud 		 * correctness/interpretability
   3055      1.45   reinoud 		 */
   3056      1.45   reinoud 
   3057      1.45   reinoud 		/* update */
   3058      1.45   reinoud 		if (ump->fileset_desc) {
   3059      1.45   reinoud 			free(ump->fileset_desc, M_UDFVOLD);
   3060      1.45   reinoud 		}
   3061      1.45   reinoud 		ump->fileset_desc = &dscr->fsd;
   3062      1.45   reinoud 		dscr = NULL;
   3063      1.45   reinoud 
   3064      1.45   reinoud 		/* continue to the next fsd */
   3065      1.45   reinoud 		fsd_len -= ump->discinfo.sector_size;
   3066      1.45   reinoud 		fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
   3067      1.45   reinoud 
   3068      1.45   reinoud 		/* follow up to fsd->next_ex (long_ad) if its not null */
   3069      1.45   reinoud 		if (udf_rw32(ump->fileset_desc->next_ex.len)) {
   3070      1.45   reinoud 			DPRINTF(VOLUMES, ("follow up FSD extent\n"));
   3071      1.45   reinoud 			fsd_loc = ump->fileset_desc->next_ex;
   3072      1.45   reinoud 			fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
   3073      1.45   reinoud 		}
   3074      1.45   reinoud 	}
   3075      1.45   reinoud 	if (dscr)
   3076      1.45   reinoud 		free(dscr, M_UDFVOLD);
   3077      1.45   reinoud 
   3078      1.45   reinoud 	/* there has to be one */
   3079      1.45   reinoud 	if (ump->fileset_desc == NULL)
   3080      1.45   reinoud 		return ENOENT;
   3081      1.45   reinoud 
   3082      1.45   reinoud 	DPRINTF(VOLUMES, ("FSD read in fine\n"));
   3083      1.45   reinoud 	DPRINTF(VOLUMES, ("Updating fsd logical volume id\n"));
   3084      1.45   reinoud 	udf_update_logvolname(ump, ump->logical_vol->logvol_id);
   3085      1.45   reinoud 
   3086      1.45   reinoud 	/*
   3087      1.45   reinoud 	 * Now the FSD is known, read in the rootdirectory and if one exists,
   3088      1.45   reinoud 	 * the system stream dir. Some files in the system streamdir are not
   3089      1.45   reinoud 	 * wanted in this implementation since they are not maintained. If
   3090      1.45   reinoud 	 * writing is enabled we'll delete these files if they exist.
   3091      1.45   reinoud 	 */
   3092      1.45   reinoud 
   3093      1.45   reinoud 	rootdir_node = streamdir_node = NULL;
   3094      1.45   reinoud 	dir_loc = NULL;
   3095      1.45   reinoud 
   3096      1.45   reinoud 	/* try to read in the rootdir */
   3097      1.45   reinoud 	dir_loc = &ump->fileset_desc->rootdir_icb;
   3098      1.45   reinoud 	error = udf_get_node(ump, dir_loc, &rootdir_node);
   3099      1.45   reinoud 	if (error)
   3100      1.45   reinoud 		return ENOENT;
   3101      1.45   reinoud 
   3102      1.45   reinoud 	/* aparently it read in fine */
   3103      1.45   reinoud 
   3104      1.45   reinoud 	/*
   3105      1.45   reinoud 	 * Try the system stream directory; not very likely in the ones we
   3106      1.45   reinoud 	 * test, but for completeness.
   3107      1.45   reinoud 	 */
   3108      1.45   reinoud 	dir_loc = &ump->fileset_desc->streamdir_icb;
   3109      1.45   reinoud 	if (udf_rw32(dir_loc->len)) {
   3110      1.45   reinoud 		printf("udf_read_rootdirs: streamdir defined ");
   3111      1.45   reinoud 		error = udf_get_node(ump, dir_loc, &streamdir_node);
   3112      1.45   reinoud 		if (error) {
   3113      1.45   reinoud 			printf("but error in streamdir reading\n");
   3114       1.1   reinoud 		} else {
   3115      1.45   reinoud 			printf("but ignored\n");
   3116      1.45   reinoud 			/*
   3117      1.45   reinoud 			 * TODO process streamdir `baddies' i.e. files we dont
   3118      1.45   reinoud 			 * want if R/W
   3119      1.45   reinoud 			 */
   3120      1.45   reinoud 		}
   3121      1.45   reinoud 	}
   3122      1.45   reinoud 
   3123      1.45   reinoud 	DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
   3124      1.45   reinoud 
   3125      1.45   reinoud 	/* release the vnodes again; they'll be auto-recycled later */
   3126      1.45   reinoud 	if (streamdir_node) {
   3127      1.45   reinoud 		vput(streamdir_node->vnode);
   3128      1.45   reinoud 	}
   3129      1.45   reinoud 	if (rootdir_node) {
   3130      1.45   reinoud 		vput(rootdir_node->vnode);
   3131      1.45   reinoud 	}
   3132      1.45   reinoud 
   3133      1.45   reinoud 	return 0;
   3134      1.45   reinoud }
   3135      1.45   reinoud 
   3136      1.45   reinoud /* --------------------------------------------------------------------- */
   3137      1.45   reinoud 
   3138      1.45   reinoud /* To make absolutely sure we are NOT returning zero, add one :) */
   3139      1.45   reinoud 
   3140      1.45   reinoud long
   3141      1.45   reinoud udf_calchash(struct long_ad *icbptr)
   3142      1.45   reinoud {
   3143      1.45   reinoud 	/* ought to be enough since each mountpoint has its own chain */
   3144      1.45   reinoud 	return udf_rw32(icbptr->loc.lb_num) + 1;
   3145      1.45   reinoud }
   3146      1.45   reinoud 
   3147      1.45   reinoud 
   3148      1.45   reinoud static struct udf_node *
   3149      1.45   reinoud udf_hash_lookup(struct udf_mount *ump, struct long_ad *icbptr)
   3150      1.45   reinoud {
   3151      1.45   reinoud 	struct udf_node *node;
   3152      1.45   reinoud 	struct vnode *vp;
   3153      1.45   reinoud 	uint32_t hashline;
   3154      1.45   reinoud 
   3155      1.45   reinoud loop:
   3156      1.45   reinoud 	mutex_enter(&ump->ihash_lock);
   3157      1.45   reinoud 
   3158      1.45   reinoud 	hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK;
   3159      1.45   reinoud 	LIST_FOREACH(node, &ump->udf_nodes[hashline], hashchain) {
   3160      1.45   reinoud 		assert(node);
   3161      1.45   reinoud 		if (node->loc.loc.lb_num   == icbptr->loc.lb_num &&
   3162      1.45   reinoud 		    node->loc.loc.part_num == icbptr->loc.part_num) {
   3163      1.45   reinoud 			vp = node->vnode;
   3164      1.45   reinoud 			assert(vp);
   3165      1.45   reinoud 			mutex_enter(&vp->v_interlock);
   3166      1.45   reinoud 			mutex_exit(&ump->ihash_lock);
   3167      1.45   reinoud 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
   3168      1.45   reinoud 				goto loop;
   3169      1.45   reinoud 			return node;
   3170      1.45   reinoud 		}
   3171      1.45   reinoud 	}
   3172      1.45   reinoud 	mutex_exit(&ump->ihash_lock);
   3173      1.45   reinoud 
   3174      1.45   reinoud 	return NULL;
   3175      1.45   reinoud }
   3176      1.45   reinoud 
   3177      1.45   reinoud 
   3178      1.45   reinoud static void
   3179      1.45   reinoud udf_sorted_list_insert(struct udf_node *node)
   3180      1.45   reinoud {
   3181      1.45   reinoud 	struct udf_mount *ump;
   3182      1.45   reinoud 	struct udf_node  *s_node, *last_node;
   3183      1.45   reinoud 	uint32_t loc, s_loc;
   3184      1.45   reinoud 
   3185      1.45   reinoud 	ump = node->ump;
   3186      1.47   reinoud 	last_node = NULL;	/* XXX gcc */
   3187      1.45   reinoud 
   3188      1.45   reinoud 	if (LIST_EMPTY(&ump->sorted_udf_nodes)) {
   3189      1.45   reinoud 		LIST_INSERT_HEAD(&ump->sorted_udf_nodes, node, sortchain);
   3190      1.45   reinoud 		return;
   3191      1.45   reinoud 	}
   3192      1.45   reinoud 
   3193      1.45   reinoud 	/*
   3194      1.45   reinoud 	 * We sort on logical block number here and not on physical block
   3195      1.45   reinoud 	 * number here. Ideally we should go for the physical block nr to get
   3196      1.45   reinoud 	 * better sync performance though this sort will ensure that packets
   3197      1.45   reinoud 	 * won't get spit up unnessisarily.
   3198      1.45   reinoud 	 */
   3199      1.45   reinoud 
   3200      1.45   reinoud 	loc = udf_rw32(node->loc.loc.lb_num);
   3201      1.45   reinoud 	LIST_FOREACH(s_node, &ump->sorted_udf_nodes, sortchain) {
   3202      1.45   reinoud 		s_loc = udf_rw32(s_node->loc.loc.lb_num);
   3203      1.45   reinoud 		if (s_loc > loc) {
   3204      1.45   reinoud 			LIST_INSERT_BEFORE(s_node, node, sortchain);
   3205      1.45   reinoud 			return;
   3206      1.45   reinoud 		}
   3207      1.45   reinoud 		last_node = s_node;
   3208      1.45   reinoud 	}
   3209      1.45   reinoud 	LIST_INSERT_AFTER(last_node, node, sortchain);
   3210      1.45   reinoud }
   3211      1.45   reinoud 
   3212      1.45   reinoud 
   3213      1.45   reinoud static void
   3214      1.45   reinoud udf_register_node(struct udf_node *node)
   3215      1.45   reinoud {
   3216      1.45   reinoud 	struct udf_mount *ump;
   3217      1.45   reinoud 	struct udf_node *chk;
   3218      1.45   reinoud 	uint32_t hashline;
   3219      1.45   reinoud 
   3220      1.45   reinoud 	ump = node->ump;
   3221      1.45   reinoud 	mutex_enter(&ump->ihash_lock);
   3222      1.45   reinoud 
   3223      1.45   reinoud 	/* add to our hash table */
   3224      1.45   reinoud 	hashline = udf_calchash(&node->loc) & UDF_INODE_HASHMASK;
   3225      1.45   reinoud #ifdef DEBUG
   3226      1.45   reinoud 	LIST_FOREACH(chk, &ump->udf_nodes[hashline], hashchain) {
   3227      1.45   reinoud 		assert(chk);
   3228      1.45   reinoud 		if (chk->loc.loc.lb_num   == node->loc.loc.lb_num &&
   3229      1.45   reinoud 		    chk->loc.loc.part_num == node->loc.loc.part_num)
   3230      1.45   reinoud 			panic("Double node entered\n");
   3231      1.45   reinoud 	}
   3232      1.45   reinoud #else
   3233      1.45   reinoud 	chk = NULL;
   3234      1.45   reinoud #endif
   3235      1.45   reinoud 	LIST_INSERT_HEAD(&ump->udf_nodes[hashline], node, hashchain);
   3236      1.45   reinoud 
   3237      1.45   reinoud 	/* add to our sorted list */
   3238      1.45   reinoud 	udf_sorted_list_insert(node);
   3239      1.45   reinoud 
   3240      1.45   reinoud 	mutex_exit(&ump->ihash_lock);
   3241      1.45   reinoud }
   3242      1.45   reinoud 
   3243      1.45   reinoud 
   3244      1.45   reinoud static void
   3245      1.45   reinoud udf_deregister_node(struct udf_node *node)
   3246      1.45   reinoud {
   3247      1.45   reinoud 	struct udf_mount *ump;
   3248      1.45   reinoud 
   3249      1.45   reinoud 	ump = node->ump;
   3250      1.45   reinoud 	mutex_enter(&ump->ihash_lock);
   3251      1.45   reinoud 
   3252      1.45   reinoud 	/* from hash and sorted list */
   3253      1.45   reinoud 	LIST_REMOVE(node, hashchain);
   3254      1.45   reinoud 	LIST_REMOVE(node, sortchain);
   3255      1.45   reinoud 
   3256      1.45   reinoud 	mutex_exit(&ump->ihash_lock);
   3257      1.45   reinoud }
   3258      1.45   reinoud 
   3259      1.45   reinoud /* --------------------------------------------------------------------- */
   3260      1.45   reinoud 
   3261      1.45   reinoud int
   3262      1.45   reinoud udf_open_logvol(struct udf_mount *ump)
   3263      1.45   reinoud {
   3264      1.45   reinoud 	int logvol_integrity;
   3265      1.45   reinoud 	int error;
   3266      1.45   reinoud 
   3267      1.45   reinoud 	/* already/still open? */
   3268      1.45   reinoud 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
   3269      1.45   reinoud 	if (logvol_integrity == UDF_INTEGRITY_OPEN)
   3270      1.45   reinoud 		return 0;
   3271      1.45   reinoud 
   3272      1.45   reinoud 	/* can we open it ? */
   3273      1.45   reinoud 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
   3274      1.45   reinoud 		return EROFS;
   3275      1.45   reinoud 
   3276      1.45   reinoud 	/* setup write parameters */
   3277      1.45   reinoud 	DPRINTF(VOLUMES, ("Setting up write parameters\n"));
   3278      1.45   reinoud 	if ((error = udf_setup_writeparams(ump)) != 0)
   3279      1.45   reinoud 		return error;
   3280      1.45   reinoud 
   3281      1.45   reinoud 	/* determine data and metadata tracks (most likely same) */
   3282      1.45   reinoud 	error = udf_search_writing_tracks(ump);
   3283      1.45   reinoud 	if (error) {
   3284      1.45   reinoud 		/* most likely lack of space */
   3285      1.45   reinoud 		printf("udf_open_logvol: error searching writing tracks\n");
   3286      1.45   reinoud 		return EROFS;
   3287      1.45   reinoud 	}
   3288      1.45   reinoud 
   3289      1.45   reinoud 	/* writeout/update lvint on disc or only in memory */
   3290      1.45   reinoud 	DPRINTF(VOLUMES, ("Opening logical volume\n"));
   3291      1.45   reinoud 	if (ump->lvopen & UDF_OPEN_SESSION) {
   3292      1.45   reinoud 		/* TODO implement writeout of VRS + VDS */
   3293      1.45   reinoud 		printf( "udf_open_logvol:Opening a closed session not yet "
   3294      1.45   reinoud 			"implemented\n");
   3295      1.45   reinoud 		return EROFS;
   3296      1.45   reinoud 
   3297      1.45   reinoud 		/* determine data and metadata tracks again */
   3298      1.45   reinoud 		error = udf_search_writing_tracks(ump);
   3299      1.45   reinoud 	}
   3300      1.45   reinoud 
   3301      1.45   reinoud 	/* mark it open */
   3302      1.45   reinoud 	ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_OPEN);
   3303      1.45   reinoud 
   3304      1.45   reinoud 	/* do we need to write it out? */
   3305      1.45   reinoud 	if (ump->lvopen & UDF_WRITE_LVINT) {
   3306      1.45   reinoud 		error = udf_writeout_lvint(ump, ump->lvopen);
   3307      1.45   reinoud 		/* if we couldn't write it mark it closed again */
   3308      1.45   reinoud 		if (error) {
   3309      1.45   reinoud 			ump->logvol_integrity->integrity_type =
   3310      1.45   reinoud 						udf_rw32(UDF_INTEGRITY_CLOSED);
   3311      1.45   reinoud 			return error;
   3312      1.45   reinoud 		}
   3313      1.45   reinoud 	}
   3314      1.45   reinoud 
   3315      1.45   reinoud 	return 0;
   3316      1.45   reinoud }
   3317      1.45   reinoud 
   3318      1.45   reinoud 
   3319      1.45   reinoud int
   3320      1.45   reinoud udf_close_logvol(struct udf_mount *ump, int mntflags)
   3321      1.45   reinoud {
   3322      1.45   reinoud 	int logvol_integrity;
   3323      1.45   reinoud 	int error = 0;
   3324      1.45   reinoud 	int n;
   3325      1.45   reinoud 
   3326      1.45   reinoud 	/* already/still closed? */
   3327      1.45   reinoud 	logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
   3328      1.45   reinoud 	if (logvol_integrity == UDF_INTEGRITY_CLOSED)
   3329      1.45   reinoud 		return 0;
   3330      1.45   reinoud 
   3331      1.45   reinoud 	/* writeout/update lvint or write out VAT */
   3332      1.45   reinoud 	DPRINTF(VOLUMES, ("Closing logical volume\n"));
   3333      1.45   reinoud 	if (ump->lvclose & UDF_WRITE_VAT) {
   3334      1.45   reinoud 		DPRINTF(VOLUMES, ("lvclose & UDF_WRITE_VAT\n"));
   3335      1.45   reinoud 
   3336      1.45   reinoud 		/* preprocess the VAT node; its modified on every writeout */
   3337      1.45   reinoud 		DPRINTF(VOLUMES, ("writeout vat_node\n"));
   3338      1.45   reinoud 		udf_update_vat_descriptor(ump->vat_node->ump);
   3339      1.45   reinoud 
   3340      1.45   reinoud 		/* write out the VAT node */
   3341      1.45   reinoud 		vflushbuf(ump->vat_node->vnode, 1 /* sync */);
   3342      1.45   reinoud 		for (n = 0; n < 16; n++) {
   3343      1.45   reinoud 			ump->vat_node->i_flags |= IN_MODIFIED;
   3344      1.45   reinoud 			error = VOP_FSYNC(ump->vat_node->vnode,
   3345      1.45   reinoud 					FSCRED, FSYNC_WAIT, 0, 0);
   3346      1.45   reinoud 		}
   3347      1.45   reinoud 		if (error) {
   3348      1.45   reinoud 			printf("udf_close_logvol: writeout of VAT failed\n");
   3349      1.45   reinoud 			return error;
   3350      1.45   reinoud 		}
   3351      1.45   reinoud 	}
   3352      1.45   reinoud 
   3353      1.45   reinoud 	if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
   3354      1.45   reinoud 		error = udf_write_partition_spacetables(ump, 1 /* waitfor */);
   3355      1.45   reinoud 		if (error) {
   3356      1.45   reinoud 			printf( "udf_close_logvol: writeout of space tables "
   3357      1.45   reinoud 				"failed\n");
   3358      1.45   reinoud 			return error;
   3359      1.45   reinoud 		}
   3360      1.45   reinoud 		ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
   3361      1.45   reinoud 	}
   3362      1.45   reinoud 
   3363      1.45   reinoud 	if (ump->lvclose & UDF_CLOSE_SESSION) {
   3364      1.45   reinoud 		printf("TODO: Closing a session is not yet implemented\n");
   3365      1.45   reinoud 		return EROFS;
   3366      1.45   reinoud 		ump->lvopen |= UDF_OPEN_SESSION;
   3367      1.45   reinoud 	}
   3368      1.45   reinoud 
   3369      1.45   reinoud 	/* mark it closed */
   3370      1.45   reinoud 	ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
   3371      1.45   reinoud 
   3372      1.45   reinoud 	/* do we need to write out the logical volume integrity */
   3373      1.45   reinoud 	if (ump->lvclose & UDF_WRITE_LVINT)
   3374      1.45   reinoud 		error = udf_writeout_lvint(ump, ump->lvopen);
   3375      1.45   reinoud 	if (error) {
   3376      1.45   reinoud 		/* HELP now what? mark it open again for now */
   3377      1.45   reinoud 		ump->logvol_integrity->integrity_type =
   3378      1.45   reinoud 			udf_rw32(UDF_INTEGRITY_OPEN);
   3379      1.45   reinoud 		return error;
   3380       1.9  christos 	}
   3381       1.1   reinoud 
   3382      1.45   reinoud 	(void) udf_synchronise_caches(ump);
   3383       1.1   reinoud 
   3384       1.1   reinoud 	return 0;
   3385       1.1   reinoud }
   3386       1.1   reinoud 
   3387       1.1   reinoud /* --------------------------------------------------------------------- */
   3388       1.1   reinoud 
   3389       1.1   reinoud /*
   3390      1.45   reinoud  * Genfs interfacing
   3391      1.45   reinoud  *
   3392      1.45   reinoud  * static const struct genfs_ops udf_genfsops = {
   3393      1.45   reinoud  * 	.gop_size = genfs_size,
   3394      1.45   reinoud  * 		size of transfers
   3395      1.45   reinoud  * 	.gop_alloc = udf_gop_alloc,
   3396      1.45   reinoud  * 		allocate len bytes at offset
   3397      1.45   reinoud  * 	.gop_write = genfs_gop_write,
   3398      1.45   reinoud  * 		putpages interface code
   3399      1.45   reinoud  * 	.gop_markupdate = udf_gop_markupdate,
   3400      1.45   reinoud  * 		set update/modify flags etc.
   3401      1.45   reinoud  * }
   3402      1.45   reinoud  */
   3403      1.45   reinoud 
   3404      1.45   reinoud /*
   3405      1.45   reinoud  * Genfs interface. These four functions are the only ones defined though not
   3406      1.45   reinoud  * documented... great....
   3407       1.1   reinoud  */
   3408       1.1   reinoud 
   3409      1.45   reinoud /*
   3410      1.45   reinoud  * Callback from genfs to allocate len bytes at offset off; only called when
   3411      1.45   reinoud  * filling up gaps in the allocation.
   3412      1.45   reinoud  */
   3413  1.49.2.3    simonb /* XXX should we check if there is space enough in udf_gop_alloc? */
   3414       1.1   reinoud static int
   3415      1.45   reinoud udf_gop_alloc(struct vnode *vp, off_t off,
   3416      1.45   reinoud     off_t len, int flags, kauth_cred_t cred)
   3417      1.45   reinoud {
   3418      1.45   reinoud #if 0
   3419      1.45   reinoud 	struct udf_node *udf_node = VTOI(vp);
   3420      1.45   reinoud 	struct udf_mount *ump = udf_node->ump;
   3421      1.45   reinoud 	uint32_t lb_size, num_lb;
   3422      1.45   reinoud #endif
   3423      1.45   reinoud 
   3424      1.45   reinoud 	DPRINTF(NOTIMPL, ("udf_gop_alloc not implemented\n"));
   3425      1.45   reinoud 	DPRINTF(ALLOC, ("udf_gop_alloc called for %"PRIu64" bytes\n", len));
   3426      1.45   reinoud 
   3427      1.45   reinoud 	return 0;
   3428      1.45   reinoud }
   3429      1.45   reinoud 
   3430      1.45   reinoud 
   3431      1.45   reinoud /*
   3432      1.45   reinoud  * callback from genfs to update our flags
   3433      1.45   reinoud  */
   3434      1.45   reinoud static void
   3435      1.45   reinoud udf_gop_markupdate(struct vnode *vp, int flags)
   3436      1.45   reinoud {
   3437      1.45   reinoud 	struct udf_node *udf_node = VTOI(vp);
   3438      1.45   reinoud 	u_long mask = 0;
   3439      1.45   reinoud 
   3440      1.45   reinoud 	if ((flags & GOP_UPDATE_ACCESSED) != 0) {
   3441      1.45   reinoud 		mask = IN_ACCESS;
   3442      1.45   reinoud 	}
   3443      1.45   reinoud 	if ((flags & GOP_UPDATE_MODIFIED) != 0) {
   3444      1.45   reinoud 		if (vp->v_type == VREG) {
   3445      1.45   reinoud 			mask |= IN_CHANGE | IN_UPDATE;
   3446      1.45   reinoud 		} else {
   3447      1.45   reinoud 			mask |= IN_MODIFY;
   3448      1.45   reinoud 		}
   3449      1.45   reinoud 	}
   3450      1.45   reinoud 	if (mask) {
   3451      1.45   reinoud 		udf_node->i_flags |= mask;
   3452      1.45   reinoud 	}
   3453      1.45   reinoud }
   3454      1.45   reinoud 
   3455      1.45   reinoud 
   3456      1.45   reinoud static const struct genfs_ops udf_genfsops = {
   3457      1.45   reinoud 	.gop_size = genfs_size,
   3458      1.45   reinoud 	.gop_alloc = udf_gop_alloc,
   3459      1.45   reinoud 	.gop_write = genfs_gop_write_rwmap,
   3460      1.45   reinoud 	.gop_markupdate = udf_gop_markupdate,
   3461      1.45   reinoud };
   3462      1.45   reinoud 
   3463      1.45   reinoud 
   3464      1.45   reinoud /* --------------------------------------------------------------------- */
   3465      1.45   reinoud 
   3466      1.45   reinoud int
   3467      1.45   reinoud udf_write_terminator(struct udf_mount *ump, uint32_t sector)
   3468       1.1   reinoud {
   3469      1.45   reinoud 	union dscrptr *dscr;
   3470       1.1   reinoud 	int error;
   3471       1.1   reinoud 
   3472      1.45   reinoud 	dscr = malloc(ump->discinfo.sector_size, M_TEMP, M_WAITOK);
   3473      1.45   reinoud 	bzero(dscr, ump->discinfo.sector_size);
   3474      1.45   reinoud 	udf_inittag(ump, &dscr->tag, TAGID_TERM, sector);
   3475      1.45   reinoud 
   3476      1.45   reinoud 	/* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
   3477      1.45   reinoud 	dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
   3478      1.45   reinoud 	(void) udf_validate_tag_and_crc_sums(dscr);
   3479      1.45   reinoud 
   3480      1.45   reinoud 	error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
   3481      1.45   reinoud 			dscr, sector, sector);
   3482      1.45   reinoud 
   3483      1.45   reinoud 	free(dscr, M_TEMP);
   3484      1.45   reinoud 
   3485      1.45   reinoud 	return error;
   3486      1.45   reinoud }
   3487      1.45   reinoud 
   3488      1.45   reinoud 
   3489      1.45   reinoud /* --------------------------------------------------------------------- */
   3490      1.45   reinoud 
   3491      1.45   reinoud /* UDF<->unix converters */
   3492      1.45   reinoud 
   3493      1.45   reinoud /* --------------------------------------------------------------------- */
   3494      1.45   reinoud 
   3495      1.45   reinoud static mode_t
   3496      1.45   reinoud udf_perm_to_unix_mode(uint32_t perm)
   3497      1.45   reinoud {
   3498      1.45   reinoud 	mode_t mode;
   3499      1.45   reinoud 
   3500      1.45   reinoud 	mode  = ((perm & UDF_FENTRY_PERM_USER_MASK)      );
   3501      1.45   reinoud 	mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK  ) >> 2);
   3502      1.45   reinoud 	mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
   3503      1.45   reinoud 
   3504      1.45   reinoud 	return mode;
   3505      1.45   reinoud }
   3506      1.45   reinoud 
   3507      1.45   reinoud /* --------------------------------------------------------------------- */
   3508      1.45   reinoud 
   3509      1.45   reinoud static uint32_t
   3510      1.45   reinoud unix_mode_to_udf_perm(mode_t mode)
   3511      1.45   reinoud {
   3512      1.45   reinoud 	uint32_t perm;
   3513      1.45   reinoud 
   3514      1.45   reinoud 	perm  = ((mode & S_IRWXO)     );
   3515      1.45   reinoud 	perm |= ((mode & S_IRWXG) << 2);
   3516      1.45   reinoud 	perm |= ((mode & S_IRWXU) << 4);
   3517      1.45   reinoud 	perm |= ((mode & S_IWOTH) << 3);
   3518      1.45   reinoud 	perm |= ((mode & S_IWGRP) << 5);
   3519      1.45   reinoud 	perm |= ((mode & S_IWUSR) << 7);
   3520      1.45   reinoud 
   3521      1.45   reinoud 	return perm;
   3522      1.45   reinoud }
   3523      1.45   reinoud 
   3524      1.45   reinoud /* --------------------------------------------------------------------- */
   3525      1.45   reinoud 
   3526      1.45   reinoud static uint32_t
   3527      1.45   reinoud udf_icb_to_unix_filetype(uint32_t icbftype)
   3528      1.45   reinoud {
   3529      1.45   reinoud 	switch (icbftype) {
   3530      1.45   reinoud 	case UDF_ICB_FILETYPE_DIRECTORY :
   3531      1.45   reinoud 	case UDF_ICB_FILETYPE_STREAMDIR :
   3532      1.45   reinoud 		return S_IFDIR;
   3533      1.45   reinoud 	case UDF_ICB_FILETYPE_FIFO :
   3534      1.45   reinoud 		return S_IFIFO;
   3535      1.45   reinoud 	case UDF_ICB_FILETYPE_CHARDEVICE :
   3536      1.45   reinoud 		return S_IFCHR;
   3537      1.45   reinoud 	case UDF_ICB_FILETYPE_BLOCKDEVICE :
   3538      1.45   reinoud 		return S_IFBLK;
   3539      1.45   reinoud 	case UDF_ICB_FILETYPE_RANDOMACCESS :
   3540      1.45   reinoud 	case UDF_ICB_FILETYPE_REALTIME :
   3541      1.45   reinoud 		return S_IFREG;
   3542      1.45   reinoud 	case UDF_ICB_FILETYPE_SYMLINK :
   3543      1.45   reinoud 		return S_IFLNK;
   3544      1.45   reinoud 	case UDF_ICB_FILETYPE_SOCKET :
   3545      1.45   reinoud 		return S_IFSOCK;
   3546      1.45   reinoud 	}
   3547      1.45   reinoud 	/* no idea what this is */
   3548      1.45   reinoud 	return 0;
   3549      1.45   reinoud }
   3550       1.1   reinoud 
   3551      1.45   reinoud /* --------------------------------------------------------------------- */
   3552       1.1   reinoud 
   3553      1.45   reinoud void
   3554      1.48   reinoud udf_to_unix_name(char *result, int result_len, char *id, int len,
   3555      1.48   reinoud 	struct charspec *chsp)
   3556      1.45   reinoud {
   3557      1.45   reinoud 	uint16_t   *raw_name, *unix_name;
   3558      1.45   reinoud 	uint16_t   *inchp, ch;
   3559      1.45   reinoud 	uint8_t	   *outchp;
   3560      1.45   reinoud 	const char *osta_id = "OSTA Compressed Unicode";
   3561      1.48   reinoud 	int         ucode_chars, nice_uchars, is_osta_typ0, nout;
   3562       1.1   reinoud 
   3563      1.45   reinoud 	raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK);
   3564      1.45   reinoud 	unix_name = raw_name + 1024;			/* split space in half */
   3565      1.45   reinoud 	assert(sizeof(char) == sizeof(uint8_t));
   3566      1.45   reinoud 	outchp = (uint8_t *) result;
   3567       1.1   reinoud 
   3568      1.45   reinoud 	is_osta_typ0  = (chsp->type == 0);
   3569      1.45   reinoud 	is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
   3570      1.45   reinoud 	if (is_osta_typ0) {
   3571      1.48   reinoud 		/* TODO clean up */
   3572      1.45   reinoud 		*raw_name = *unix_name = 0;
   3573      1.45   reinoud 		ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
   3574      1.45   reinoud 		ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
   3575      1.45   reinoud 		nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
   3576      1.48   reinoud 		/* output UTF8 */
   3577      1.45   reinoud 		for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
   3578      1.45   reinoud 			ch = *inchp;
   3579      1.48   reinoud 			nout = wput_utf8(outchp, result_len, ch);
   3580      1.48   reinoud 			outchp += nout; result_len -= nout;
   3581      1.45   reinoud 			if (!ch) break;
   3582      1.45   reinoud 		}
   3583      1.45   reinoud 		*outchp++ = 0;
   3584       1.1   reinoud 	} else {
   3585      1.45   reinoud 		/* assume 8bit char length byte latin-1 */
   3586      1.45   reinoud 		assert(*id == 8);
   3587      1.48   reinoud 		assert(strlen((char *) (id+1)) <= MAXNAMLEN);
   3588      1.45   reinoud 		strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
   3589       1.9  christos 	}
   3590      1.45   reinoud 	free(raw_name, M_UDFTEMP);
   3591      1.45   reinoud }
   3592      1.45   reinoud 
   3593      1.45   reinoud /* --------------------------------------------------------------------- */
   3594       1.1   reinoud 
   3595      1.45   reinoud void
   3596      1.45   reinoud unix_to_udf_name(char *result, uint8_t *result_len, char const *name, int name_len,
   3597      1.45   reinoud 	struct charspec *chsp)
   3598      1.45   reinoud {
   3599      1.45   reinoud 	uint16_t   *raw_name;
   3600      1.45   reinoud 	uint16_t   *outchp;
   3601      1.45   reinoud 	const char *inchp;
   3602      1.45   reinoud 	const char *osta_id = "OSTA Compressed Unicode";
   3603      1.49  christos 	int         udf_chars, is_osta_typ0, bits;
   3604      1.49  christos 	size_t      cnt;
   3605       1.1   reinoud 
   3606      1.45   reinoud 	/* allocate temporary unicode-16 buffer */
   3607      1.45   reinoud 	raw_name = malloc(1024, M_UDFTEMP, M_WAITOK);
   3608       1.1   reinoud 
   3609      1.48   reinoud 	/* convert utf8 to unicode-16 */
   3610      1.45   reinoud 	*raw_name = 0;
   3611      1.45   reinoud 	inchp  = name;
   3612      1.45   reinoud 	outchp = raw_name;
   3613      1.48   reinoud 	bits = 8;
   3614      1.48   reinoud 	for (cnt = name_len, udf_chars = 0; cnt;) {
   3615      1.49  christos /*###3490 [cc] warning: passing argument 2 of 'wget_utf8' from incompatible pointer type%%%*/
   3616      1.48   reinoud 		*outchp = wget_utf8(&inchp, &cnt);
   3617      1.48   reinoud 		if (*outchp > 0xff)
   3618      1.48   reinoud 			bits=16;
   3619      1.48   reinoud 		outchp++;
   3620      1.48   reinoud 		udf_chars++;
   3621      1.45   reinoud 	}
   3622      1.48   reinoud 	/* null terminate just in case */
   3623      1.48   reinoud 	*outchp++ = 0;
   3624       1.1   reinoud 
   3625      1.45   reinoud 	is_osta_typ0  = (chsp->type == 0);
   3626      1.45   reinoud 	is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
   3627      1.45   reinoud 	if (is_osta_typ0) {
   3628      1.48   reinoud 		udf_chars = udf_CompressUnicode(udf_chars, bits,
   3629      1.45   reinoud 				(unicode_t *) raw_name,
   3630      1.45   reinoud 				(byte *) result);
   3631      1.45   reinoud 	} else {
   3632      1.48   reinoud 		printf("unix to udf name: no CHSP0 ?\n");
   3633      1.45   reinoud 		/* XXX assume 8bit char length byte latin-1 */
   3634      1.45   reinoud 		*result++ = 8; udf_chars = 1;
   3635      1.45   reinoud 		strncpy(result, name + 1, name_len);
   3636      1.45   reinoud 		udf_chars += name_len;
   3637       1.9  christos 	}
   3638      1.45   reinoud 	*result_len = udf_chars;
   3639      1.45   reinoud 	free(raw_name, M_UDFTEMP);
   3640      1.45   reinoud }
   3641      1.45   reinoud 
   3642      1.45   reinoud /* --------------------------------------------------------------------- */
   3643      1.45   reinoud 
   3644      1.45   reinoud void
   3645      1.45   reinoud udf_timestamp_to_timespec(struct udf_mount *ump,
   3646      1.45   reinoud 			  struct timestamp *timestamp,
   3647      1.45   reinoud 			  struct timespec  *timespec)
   3648      1.45   reinoud {
   3649      1.45   reinoud 	struct clock_ymdhms ymdhms;
   3650      1.45   reinoud 	uint32_t usecs, secs, nsecs;
   3651      1.45   reinoud 	uint16_t tz;
   3652      1.45   reinoud 
   3653      1.45   reinoud 	/* fill in ymdhms structure from timestamp */
   3654      1.45   reinoud 	memset(&ymdhms, 0, sizeof(ymdhms));
   3655      1.45   reinoud 	ymdhms.dt_year = udf_rw16(timestamp->year);
   3656      1.45   reinoud 	ymdhms.dt_mon  = timestamp->month;
   3657      1.45   reinoud 	ymdhms.dt_day  = timestamp->day;
   3658      1.45   reinoud 	ymdhms.dt_wday = 0; /* ? */
   3659      1.45   reinoud 	ymdhms.dt_hour = timestamp->hour;
   3660      1.45   reinoud 	ymdhms.dt_min  = timestamp->minute;
   3661      1.45   reinoud 	ymdhms.dt_sec  = timestamp->second;
   3662      1.45   reinoud 
   3663      1.45   reinoud 	secs = clock_ymdhms_to_secs(&ymdhms);
   3664      1.45   reinoud 	usecs = timestamp->usec +
   3665      1.45   reinoud 		100*timestamp->hund_usec + 10000*timestamp->centisec;
   3666      1.45   reinoud 	nsecs = usecs * 1000;
   3667       1.1   reinoud 
   3668       1.1   reinoud 	/*
   3669      1.45   reinoud 	 * Calculate the time zone.  The timezone is 12 bit signed 2's
   3670      1.45   reinoud 	 * compliment, so we gotta do some extra magic to handle it right.
   3671       1.1   reinoud 	 */
   3672      1.45   reinoud 	tz  = udf_rw16(timestamp->type_tz);
   3673      1.45   reinoud 	tz &= 0x0fff;			/* only lower 12 bits are significant */
   3674      1.45   reinoud 	if (tz & 0x0800)		/* sign extention */
   3675      1.45   reinoud 		tz |= 0xf000;
   3676       1.1   reinoud 
   3677      1.45   reinoud 	/* TODO check timezone conversion */
   3678      1.45   reinoud 	/* check if we are specified a timezone to convert */
   3679      1.45   reinoud 	if (udf_rw16(timestamp->type_tz) & 0x1000) {
   3680      1.45   reinoud 		if ((int16_t) tz != -2047)
   3681      1.45   reinoud 			secs -= (int16_t) tz * 60;
   3682       1.1   reinoud 	} else {
   3683      1.45   reinoud 		secs -= ump->mount_args.gmtoff;
   3684      1.45   reinoud 	}
   3685       1.1   reinoud 
   3686      1.45   reinoud 	timespec->tv_sec  = secs;
   3687      1.45   reinoud 	timespec->tv_nsec = nsecs;
   3688      1.45   reinoud }
   3689       1.1   reinoud 
   3690       1.1   reinoud 
   3691      1.45   reinoud void
   3692      1.45   reinoud udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp)
   3693      1.45   reinoud {
   3694      1.45   reinoud 	struct clock_ymdhms ymdhms;
   3695      1.45   reinoud 	uint32_t husec, usec, csec;
   3696       1.1   reinoud 
   3697      1.45   reinoud 	(void) clock_secs_to_ymdhms(timespec->tv_sec, &ymdhms);
   3698       1.1   reinoud 
   3699  1.49.2.2    simonb 	usec   = timespec->tv_nsec / 1000;
   3700  1.49.2.2    simonb 	husec  =  usec / 100;
   3701  1.49.2.2    simonb 	usec  -= husec * 100;				/* only 0-99 in usec  */
   3702  1.49.2.2    simonb 	csec   = husec / 100;				/* only 0-99 in csec  */
   3703  1.49.2.2    simonb 	husec -=  csec * 100;				/* only 0-99 in husec */
   3704      1.45   reinoud 
   3705      1.45   reinoud 	/* set method 1 for CUT/GMT */
   3706      1.45   reinoud 	timestamp->type_tz	= udf_rw16((1<<12) + 0);
   3707      1.45   reinoud 	timestamp->year		= udf_rw16(ymdhms.dt_year);
   3708      1.45   reinoud 	timestamp->month	= ymdhms.dt_mon;
   3709      1.45   reinoud 	timestamp->day		= ymdhms.dt_day;
   3710      1.45   reinoud 	timestamp->hour		= ymdhms.dt_hour;
   3711      1.45   reinoud 	timestamp->minute	= ymdhms.dt_min;
   3712      1.45   reinoud 	timestamp->second	= ymdhms.dt_sec;
   3713      1.45   reinoud 	timestamp->centisec	= csec;
   3714      1.45   reinoud 	timestamp->hund_usec	= husec;
   3715      1.45   reinoud 	timestamp->usec		= usec;
   3716       1.1   reinoud }
   3717       1.1   reinoud 
   3718       1.1   reinoud /* --------------------------------------------------------------------- */
   3719       1.1   reinoud 
   3720      1.45   reinoud /*
   3721      1.45   reinoud  * Attribute and filetypes converters with get/set pairs
   3722      1.45   reinoud  */
   3723      1.45   reinoud 
   3724      1.45   reinoud uint32_t
   3725      1.45   reinoud udf_getaccessmode(struct udf_node *udf_node)
   3726       1.1   reinoud {
   3727      1.45   reinoud 	struct file_entry     *fe = udf_node->fe;;
   3728      1.45   reinoud 	struct extfile_entry *efe = udf_node->efe;
   3729      1.45   reinoud 	uint32_t udf_perm, icbftype;
   3730      1.45   reinoud 	uint32_t mode, ftype;
   3731      1.45   reinoud 	uint16_t icbflags;
   3732       1.1   reinoud 
   3733      1.45   reinoud 	UDF_LOCK_NODE(udf_node, 0);
   3734      1.45   reinoud 	if (fe) {
   3735      1.45   reinoud 		udf_perm = udf_rw32(fe->perm);
   3736      1.45   reinoud 		icbftype = fe->icbtag.file_type;
   3737      1.45   reinoud 		icbflags = udf_rw16(fe->icbtag.flags);
   3738      1.45   reinoud 	} else {
   3739      1.45   reinoud 		assert(udf_node->efe);
   3740      1.45   reinoud 		udf_perm = udf_rw32(efe->perm);
   3741      1.45   reinoud 		icbftype = efe->icbtag.file_type;
   3742      1.45   reinoud 		icbflags = udf_rw16(efe->icbtag.flags);
   3743      1.45   reinoud 	}
   3744       1.1   reinoud 
   3745      1.45   reinoud 	mode  = udf_perm_to_unix_mode(udf_perm);
   3746      1.45   reinoud 	ftype = udf_icb_to_unix_filetype(icbftype);
   3747       1.1   reinoud 
   3748      1.45   reinoud 	/* set suid, sgid, sticky from flags in fe/efe */
   3749      1.45   reinoud 	if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
   3750      1.45   reinoud 		mode |= S_ISUID;
   3751      1.45   reinoud 	if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
   3752      1.45   reinoud 		mode |= S_ISGID;
   3753      1.45   reinoud 	if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
   3754      1.45   reinoud 		mode |= S_ISVTX;
   3755       1.1   reinoud 
   3756      1.45   reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   3757       1.1   reinoud 
   3758      1.45   reinoud 	return mode | ftype;
   3759       1.1   reinoud }
   3760       1.1   reinoud 
   3761       1.1   reinoud 
   3762      1.45   reinoud void
   3763      1.45   reinoud udf_setaccessmode(struct udf_node *udf_node, mode_t mode)
   3764       1.1   reinoud {
   3765      1.45   reinoud 	struct file_entry    *fe  = udf_node->fe;
   3766      1.45   reinoud 	struct extfile_entry *efe = udf_node->efe;
   3767      1.45   reinoud 	uint32_t udf_perm;
   3768      1.45   reinoud 	uint16_t icbflags;
   3769      1.45   reinoud 
   3770      1.45   reinoud 	UDF_LOCK_NODE(udf_node, 0);
   3771      1.45   reinoud 	udf_perm = unix_mode_to_udf_perm(mode & ALLPERMS);
   3772      1.45   reinoud 	if (fe) {
   3773      1.45   reinoud 		icbflags = udf_rw16(fe->icbtag.flags);
   3774      1.45   reinoud 	} else {
   3775      1.45   reinoud 		icbflags = udf_rw16(efe->icbtag.flags);
   3776      1.45   reinoud 	}
   3777       1.1   reinoud 
   3778      1.45   reinoud 	icbflags &= ~UDF_ICB_TAG_FLAGS_SETUID;
   3779      1.45   reinoud 	icbflags &= ~UDF_ICB_TAG_FLAGS_SETGID;
   3780      1.45   reinoud 	icbflags &= ~UDF_ICB_TAG_FLAGS_STICKY;
   3781      1.45   reinoud 	if (mode & S_ISUID)
   3782      1.45   reinoud 		icbflags |= UDF_ICB_TAG_FLAGS_SETUID;
   3783      1.45   reinoud 	if (mode & S_ISGID)
   3784      1.45   reinoud 		icbflags |= UDF_ICB_TAG_FLAGS_SETGID;
   3785      1.45   reinoud 	if (mode & S_ISVTX)
   3786      1.45   reinoud 		icbflags |= UDF_ICB_TAG_FLAGS_STICKY;
   3787       1.1   reinoud 
   3788      1.45   reinoud 	if (fe) {
   3789      1.45   reinoud 		fe->perm  = udf_rw32(udf_perm);
   3790      1.45   reinoud 		fe->icbtag.flags  = udf_rw16(icbflags);
   3791      1.45   reinoud 	} else {
   3792      1.45   reinoud 		efe->perm = udf_rw32(udf_perm);
   3793      1.45   reinoud 		efe->icbtag.flags = udf_rw16(icbflags);
   3794       1.9  christos 	}
   3795       1.1   reinoud 
   3796      1.45   reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   3797       1.1   reinoud }
   3798       1.1   reinoud 
   3799       1.1   reinoud 
   3800      1.45   reinoud void
   3801      1.45   reinoud udf_getownership(struct udf_node *udf_node, uid_t *uidp, gid_t *gidp)
   3802      1.26   reinoud {
   3803      1.45   reinoud 	struct udf_mount     *ump = udf_node->ump;
   3804      1.45   reinoud 	struct file_entry    *fe  = udf_node->fe;
   3805      1.45   reinoud 	struct extfile_entry *efe = udf_node->efe;
   3806      1.45   reinoud 	uid_t uid;
   3807      1.45   reinoud 	gid_t gid;
   3808      1.26   reinoud 
   3809      1.45   reinoud 	UDF_LOCK_NODE(udf_node, 0);
   3810      1.45   reinoud 	if (fe) {
   3811      1.45   reinoud 		uid = (uid_t)udf_rw32(fe->uid);
   3812      1.45   reinoud 		gid = (gid_t)udf_rw32(fe->gid);
   3813      1.45   reinoud 	} else {
   3814      1.45   reinoud 		assert(udf_node->efe);
   3815      1.45   reinoud 		uid = (uid_t)udf_rw32(efe->uid);
   3816      1.45   reinoud 		gid = (gid_t)udf_rw32(efe->gid);
   3817      1.45   reinoud 	}
   3818      1.45   reinoud 
   3819      1.45   reinoud 	/* do the uid/gid translation game */
   3820      1.45   reinoud 	if ((uid == (uid_t) -1) && (gid == (gid_t) -1)) {
   3821      1.45   reinoud 		uid = ump->mount_args.anon_uid;
   3822      1.45   reinoud 		gid = ump->mount_args.anon_gid;
   3823      1.26   reinoud 	}
   3824      1.45   reinoud 	*uidp = uid;
   3825      1.45   reinoud 	*gidp = gid;
   3826      1.45   reinoud 
   3827      1.45   reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   3828      1.45   reinoud }
   3829      1.26   reinoud 
   3830      1.26   reinoud 
   3831      1.45   reinoud void
   3832      1.45   reinoud udf_setownership(struct udf_node *udf_node, uid_t uid, gid_t gid)
   3833      1.45   reinoud {
   3834      1.45   reinoud 	struct udf_mount     *ump = udf_node->ump;
   3835      1.45   reinoud 	struct file_entry    *fe  = udf_node->fe;
   3836      1.45   reinoud 	struct extfile_entry *efe = udf_node->efe;
   3837      1.45   reinoud 	uid_t nobody_uid;
   3838      1.45   reinoud 	gid_t nobody_gid;
   3839      1.45   reinoud 
   3840      1.45   reinoud 	UDF_LOCK_NODE(udf_node, 0);
   3841      1.45   reinoud 
   3842      1.45   reinoud 	/* do the uid/gid translation game */
   3843      1.45   reinoud 	nobody_uid = ump->mount_args.nobody_uid;
   3844      1.45   reinoud 	nobody_gid = ump->mount_args.nobody_gid;
   3845      1.45   reinoud 	if ((uid == nobody_uid) && (gid == nobody_gid)) {
   3846      1.45   reinoud 		uid = (uid_t) -1;
   3847      1.45   reinoud 		gid = (gid_t) -1;
   3848      1.26   reinoud 	}
   3849      1.26   reinoud 
   3850      1.45   reinoud 	if (fe) {
   3851      1.45   reinoud 		fe->uid  = udf_rw32((uint32_t) uid);
   3852      1.45   reinoud 		fe->gid  = udf_rw32((uint32_t) gid);
   3853      1.26   reinoud 	} else {
   3854      1.45   reinoud 		efe->uid = udf_rw32((uint32_t) uid);
   3855      1.45   reinoud 		efe->gid = udf_rw32((uint32_t) gid);
   3856      1.26   reinoud 	}
   3857      1.45   reinoud 
   3858      1.45   reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   3859      1.26   reinoud }
   3860      1.45   reinoud 
   3861      1.26   reinoud 
   3862      1.26   reinoud /* --------------------------------------------------------------------- */
   3863      1.26   reinoud 
   3864      1.45   reinoud /*
   3865  1.49.2.2    simonb  * UDF dirhash implementation
   3866      1.45   reinoud  */
   3867      1.45   reinoud 
   3868  1.49.2.2    simonb static uint32_t
   3869  1.49.2.2    simonb udf_dirhash_hash(const char *str, int namelen)
   3870       1.1   reinoud {
   3871  1.49.2.2    simonb 	uint32_t hash = 5381;
   3872  1.49.2.2    simonb         int i, c;
   3873  1.49.2.2    simonb 
   3874  1.49.2.2    simonb 	for (i = 0; i < namelen; i++) {
   3875  1.49.2.2    simonb 		c = *str++;
   3876  1.49.2.2    simonb 		hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
   3877  1.49.2.2    simonb 	}
   3878  1.49.2.2    simonb         return hash;
   3879  1.49.2.2    simonb }
   3880  1.49.2.2    simonb 
   3881  1.49.2.2    simonb 
   3882  1.49.2.2    simonb static void
   3883  1.49.2.2    simonb udf_dirhash_init(struct udf_node *dir_node)
   3884  1.49.2.2    simonb {
   3885  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   3886  1.49.2.2    simonb 	uint32_t hashline;
   3887  1.49.2.2    simonb 
   3888  1.49.2.2    simonb 	if (dir_node->dir_hash == NULL) {
   3889  1.49.2.2    simonb 		dirh = pool_get(&udf_dirhash_pool, PR_WAITOK);
   3890  1.49.2.2    simonb 		dir_node->dir_hash = dirh;
   3891  1.49.2.2    simonb 		memset(dirh, 0, sizeof(struct udf_dirhash));
   3892  1.49.2.2    simonb 		for (hashline = 0; hashline < UDF_DIRHASH_HASHSIZE; hashline++)
   3893  1.49.2.2    simonb 			LIST_INIT(&dirh->entries[hashline]);
   3894  1.49.2.2    simonb 	}
   3895  1.49.2.2    simonb }
   3896  1.49.2.2    simonb 
   3897  1.49.2.2    simonb 
   3898  1.49.2.2    simonb static void
   3899  1.49.2.2    simonb udf_dirhash_destroy(struct udf_node *dir_node)
   3900  1.49.2.2    simonb {
   3901  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   3902  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_e;
   3903  1.49.2.2    simonb 	uint32_t hashline;
   3904  1.49.2.2    simonb 
   3905  1.49.2.2    simonb 	if (dir_node->dir_hash == NULL)
   3906  1.49.2.2    simonb 		return;
   3907  1.49.2.2    simonb 
   3908  1.49.2.2    simonb 	dirh = dir_node->dir_hash;
   3909  1.49.2.2    simonb 	for (hashline = 0; hashline < UDF_DIRHASH_HASHSIZE; hashline++) {
   3910  1.49.2.2    simonb 		dirh_e = LIST_FIRST(&dirh->entries[hashline]);
   3911  1.49.2.2    simonb 		while (dirh_e) {
   3912  1.49.2.2    simonb 			LIST_REMOVE(dirh_e, next);
   3913  1.49.2.2    simonb 			pool_put(&udf_dirhash_entry_pool, dirh_e);
   3914  1.49.2.2    simonb 			dirh_e = LIST_FIRST(&dirh->entries[hashline]);
   3915  1.49.2.2    simonb 		}
   3916  1.49.2.2    simonb 	}
   3917  1.49.2.2    simonb 	pool_put(&udf_dirhash_pool, dirh);
   3918  1.49.2.2    simonb 	dir_node->dir_hash = NULL;
   3919  1.49.2.2    simonb }
   3920  1.49.2.2    simonb 
   3921  1.49.2.2    simonb 
   3922  1.49.2.2    simonb static void
   3923  1.49.2.2    simonb udf_dirhash_enter(struct udf_node *dir_node, struct fileid_desc *fid,
   3924  1.49.2.2    simonb 	struct dirent *dirent, uint64_t offset, uint32_t fid_size, int new)
   3925  1.49.2.2    simonb {
   3926  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   3927  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_e;
   3928  1.49.2.2    simonb 	uint32_t hashvalue, hashline;
   3929  1.49.2.2    simonb 
   3930  1.49.2.2    simonb 	/* make sure we have a dirhash to add to; might be a fresh dir */
   3931  1.49.2.2    simonb 	udf_dirhash_init(dir_node);
   3932  1.49.2.2    simonb 	dirh = dir_node->dir_hash;
   3933  1.49.2.2    simonb 
   3934  1.49.2.2    simonb 	/* finished build? */
   3935  1.49.2.2    simonb 	if (!new && (dir_node->i_flags & IN_DIRHASH_COMPLETE))
   3936  1.49.2.2    simonb 		return;
   3937  1.49.2.2    simonb 
   3938  1.49.2.2    simonb 	/* calculate our hash */
   3939  1.49.2.2    simonb 	hashvalue = udf_dirhash_hash(dirent->d_name, dirent->d_namlen);
   3940  1.49.2.2    simonb 	hashline  = hashvalue & UDF_DIRHASH_HASHMASK;
   3941  1.49.2.2    simonb 
   3942  1.49.2.2    simonb 	/* lookup and insert entry if not there yet */
   3943  1.49.2.2    simonb 	LIST_FOREACH(dirh_e, &dirh->entries[hashline], next) {
   3944  1.49.2.2    simonb 		/* check for hash collision */
   3945  1.49.2.2    simonb 		if (dirh_e->hashvalue != hashvalue)
   3946  1.49.2.2    simonb 			continue;
   3947  1.49.2.2    simonb 		if (dirh_e->offset != offset)
   3948  1.49.2.2    simonb 			continue;
   3949  1.49.2.2    simonb 		/* got it already */
   3950  1.49.2.2    simonb 		KASSERT(dirh_e->d_namlen == dirent->d_namlen);
   3951  1.49.2.2    simonb 		KASSERT(dirh_e->fid_size == fid_size);
   3952  1.49.2.2    simonb 		return;
   3953  1.49.2.2    simonb 	}
   3954  1.49.2.2    simonb 
   3955  1.49.2.2    simonb 	DPRINTF(DIRHASH, ("dirhash enter %"PRIu64", %d, %d for `%*.*s`\n",
   3956  1.49.2.2    simonb 		offset, fid_size, dirent->d_namlen,
   3957  1.49.2.2    simonb 		dirent->d_namlen, dirent->d_namlen, dirent->d_name));
   3958  1.49.2.2    simonb 
   3959  1.49.2.2    simonb 	/* check if entry is in free space list */
   3960  1.49.2.2    simonb 	LIST_FOREACH(dirh_e, &dirh->free_entries, next) {
   3961  1.49.2.2    simonb 		if (dirh_e->offset == offset) {
   3962  1.49.2.2    simonb 			DPRINTF(DIRHASH, ("\tremoving free entry\n"));
   3963  1.49.2.2    simonb 			LIST_REMOVE(dirh_e, next);
   3964  1.49.2.2    simonb 			break;
   3965  1.49.2.2    simonb 		}
   3966  1.49.2.2    simonb 	}
   3967  1.49.2.2    simonb 
   3968  1.49.2.2    simonb 	/* add to the hashline */
   3969  1.49.2.2    simonb 	dirh_e = pool_get(&udf_dirhash_entry_pool, PR_WAITOK);
   3970  1.49.2.2    simonb 	memset(dirh_e, 0, sizeof(struct udf_dirhash_entry));
   3971  1.49.2.2    simonb 
   3972  1.49.2.2    simonb 	dirh_e->hashvalue = hashvalue;
   3973  1.49.2.2    simonb 	dirh_e->offset    = offset;
   3974  1.49.2.2    simonb 	dirh_e->d_namlen  = dirent->d_namlen;
   3975  1.49.2.2    simonb 	dirh_e->fid_size  = fid_size;
   3976  1.49.2.2    simonb 
   3977  1.49.2.2    simonb 	LIST_INSERT_HEAD(&dirh->entries[hashline], dirh_e, next);
   3978  1.49.2.2    simonb }
   3979  1.49.2.2    simonb 
   3980  1.49.2.2    simonb 
   3981  1.49.2.2    simonb static void
   3982  1.49.2.2    simonb udf_dirhash_enter_freed(struct udf_node *dir_node, uint64_t offset,
   3983  1.49.2.2    simonb 	uint32_t fid_size)
   3984  1.49.2.2    simonb {
   3985  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   3986  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_e;
   3987  1.49.2.2    simonb 
   3988  1.49.2.2    simonb 	/* make sure we have a dirhash to add to; might be a fresh dir */
   3989  1.49.2.2    simonb 	udf_dirhash_init(dir_node);
   3990  1.49.2.2    simonb 	dirh = dir_node->dir_hash;
   3991  1.49.2.2    simonb 	KASSERT(dirh);
   3992  1.49.2.2    simonb 
   3993  1.49.2.2    simonb #ifdef DEBUG
   3994  1.49.2.2    simonb 	/* check for double entry of free space */
   3995  1.49.2.2    simonb 	LIST_FOREACH(dirh_e, &dirh->free_entries, next)
   3996  1.49.2.2    simonb 		KASSERT(dirh_e->offset != offset);
   3997  1.49.2.2    simonb #endif
   3998  1.49.2.2    simonb 
   3999  1.49.2.2    simonb 	DPRINTF(DIRHASH, ("dirhash enter FREED %"PRIu64", %d\n",
   4000  1.49.2.2    simonb 		offset, fid_size));
   4001  1.49.2.2    simonb 	dirh_e = pool_get(&udf_dirhash_entry_pool, PR_WAITOK);
   4002  1.49.2.2    simonb 	memset(dirh_e, 0, sizeof(struct udf_dirhash_entry));
   4003  1.49.2.2    simonb 
   4004  1.49.2.2    simonb 	dirh_e->hashvalue = 0;		/* not relevant */
   4005  1.49.2.2    simonb 	dirh_e->offset    = offset;
   4006  1.49.2.2    simonb 	dirh_e->d_namlen  = 0;		/* not relevant */
   4007  1.49.2.2    simonb 	dirh_e->fid_size  = fid_size;
   4008  1.49.2.2    simonb 
   4009  1.49.2.2    simonb 	/* XXX it might be preferable to append them at the tail */
   4010  1.49.2.2    simonb 	LIST_INSERT_HEAD(&dirh->free_entries, dirh_e, next);
   4011  1.49.2.2    simonb }
   4012  1.49.2.2    simonb 
   4013  1.49.2.2    simonb 
   4014  1.49.2.2    simonb static void
   4015  1.49.2.2    simonb udf_dirhash_remove(struct udf_node *dir_node, struct dirent *dirent,
   4016  1.49.2.2    simonb 	uint64_t offset, uint32_t fid_size)
   4017  1.49.2.2    simonb {
   4018  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   4019  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_e;
   4020  1.49.2.2    simonb 	uint32_t hashvalue, hashline;
   4021  1.49.2.2    simonb 
   4022  1.49.2.2    simonb 	DPRINTF(DIRHASH, ("dirhash remove %"PRIu64", %d for `%*.*s`\n",
   4023  1.49.2.2    simonb 		offset, fid_size,
   4024  1.49.2.2    simonb 		dirent->d_namlen, dirent->d_namlen, dirent->d_name));
   4025  1.49.2.2    simonb 
   4026  1.49.2.2    simonb 	/* make sure we have a dirhash to add to */
   4027  1.49.2.2    simonb 	dirh = dir_node->dir_hash;
   4028  1.49.2.2    simonb 	KASSERT(dirh);
   4029  1.49.2.2    simonb 
   4030  1.49.2.2    simonb 	/* calculate our hash */
   4031  1.49.2.2    simonb 	hashvalue = udf_dirhash_hash(dirent->d_name, dirent->d_namlen);
   4032  1.49.2.2    simonb 	hashline  = hashvalue & UDF_DIRHASH_HASHMASK;
   4033  1.49.2.2    simonb 
   4034  1.49.2.2    simonb 	/* lookup entry */
   4035  1.49.2.2    simonb 	LIST_FOREACH(dirh_e, &dirh->entries[hashline], next) {
   4036  1.49.2.2    simonb 		/* check for hash collision */
   4037  1.49.2.2    simonb 		if (dirh_e->hashvalue != hashvalue)
   4038  1.49.2.2    simonb 			continue;
   4039  1.49.2.2    simonb 		if (dirh_e->offset != offset)
   4040  1.49.2.2    simonb 			continue;
   4041  1.49.2.2    simonb 
   4042  1.49.2.2    simonb 		/* got it! */
   4043  1.49.2.2    simonb 		KASSERT(dirh_e->d_namlen == dirent->d_namlen);
   4044  1.49.2.2    simonb 		KASSERT(dirh_e->fid_size == fid_size);
   4045  1.49.2.2    simonb 		LIST_REMOVE(dirh_e, next);
   4046  1.49.2.2    simonb 
   4047  1.49.2.2    simonb 		udf_dirhash_enter_freed(dir_node, offset, fid_size);
   4048  1.49.2.2    simonb 		return;
   4049  1.49.2.2    simonb 	}
   4050  1.49.2.2    simonb 
   4051  1.49.2.2    simonb 	/* not found! */
   4052  1.49.2.2    simonb 	panic("dirhash_remove couldn't find entry in hash table\n");
   4053  1.49.2.2    simonb }
   4054  1.49.2.2    simonb 
   4055  1.49.2.2    simonb 
   4056  1.49.2.2    simonb /* BUGALERT: don't use result longer than needed, never past the node lock */
   4057  1.49.2.2    simonb /* call with NULL *result initially and it will return nonzero if again */
   4058  1.49.2.2    simonb static int
   4059  1.49.2.2    simonb udf_dirhash_lookup(struct udf_node *dir_node, const char *d_name, int d_namlen,
   4060  1.49.2.2    simonb 	struct udf_dirhash_entry **result)
   4061  1.49.2.2    simonb {
   4062  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   4063  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_e;
   4064  1.49.2.2    simonb 	uint32_t hashvalue, hashline;
   4065  1.49.2.2    simonb 
   4066  1.49.2.2    simonb 	KASSERT(VOP_ISLOCKED(dir_node->vnode));
   4067  1.49.2.2    simonb 
   4068  1.49.2.2    simonb 	/* make sure we have a dirhash to look into */
   4069  1.49.2.2    simonb 	dirh = dir_node->dir_hash;
   4070  1.49.2.2    simonb 	KASSERT(dirh);
   4071  1.49.2.2    simonb 
   4072  1.49.2.2    simonb 	/* start where we were */
   4073  1.49.2.2    simonb 	if (*result) {
   4074  1.49.2.2    simonb 		KASSERT(dir_node->dir_hash);
   4075  1.49.2.2    simonb 		dirh_e = *result;
   4076  1.49.2.2    simonb 
   4077  1.49.2.2    simonb 		/* retrieve information to avoid recalculation and advance */
   4078  1.49.2.2    simonb 		hashvalue = dirh_e->hashvalue;
   4079  1.49.2.2    simonb 		dirh_e = LIST_NEXT(*result, next);
   4080  1.49.2.2    simonb 	} else {
   4081  1.49.2.2    simonb 		/* calculate our hash and lookup all entries in hashline */
   4082  1.49.2.2    simonb 		hashvalue = udf_dirhash_hash(d_name, d_namlen);
   4083  1.49.2.2    simonb 		hashline  = hashvalue & UDF_DIRHASH_HASHMASK;
   4084  1.49.2.2    simonb 		dirh_e = LIST_FIRST(&dirh->entries[hashline]);
   4085  1.49.2.2    simonb 	}
   4086  1.49.2.2    simonb 
   4087  1.49.2.2    simonb 	for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
   4088  1.49.2.2    simonb 		/* check for hash collision */
   4089  1.49.2.2    simonb 		if (dirh_e->hashvalue != hashvalue)
   4090  1.49.2.2    simonb 			continue;
   4091  1.49.2.2    simonb 		if (dirh_e->d_namlen != d_namlen)
   4092  1.49.2.2    simonb 			continue;
   4093  1.49.2.2    simonb 		/* might have an entry in the cache */
   4094  1.49.2.2    simonb 		*result = dirh_e;
   4095  1.49.2.2    simonb 		return 1;
   4096  1.49.2.2    simonb 	}
   4097  1.49.2.2    simonb 
   4098  1.49.2.2    simonb 	*result = NULL;
   4099  1.49.2.2    simonb 	return 0;
   4100  1.49.2.2    simonb }
   4101  1.49.2.2    simonb 
   4102  1.49.2.2    simonb 
   4103  1.49.2.2    simonb /* BUGALERT: don't use result longer than needed, never past the node lock */
   4104  1.49.2.2    simonb /* call with NULL *result initially and it will return nonzero if again */
   4105  1.49.2.2    simonb static int
   4106  1.49.2.2    simonb udf_dirhash_lookup_freed(struct udf_node *dir_node, uint32_t min_fidsize,
   4107  1.49.2.2    simonb 	struct udf_dirhash_entry **result)
   4108  1.49.2.2    simonb {
   4109  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   4110  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_e;
   4111  1.49.2.2    simonb 
   4112  1.49.2.2    simonb 	KASSERT(VOP_ISLOCKED(dir_node->vnode));
   4113  1.49.2.2    simonb 
   4114  1.49.2.2    simonb 	/* make sure we have a dirhash to look into */
   4115  1.49.2.2    simonb 	udf_dirhash_init(dir_node);
   4116  1.49.2.2    simonb 	dirh = dir_node->dir_hash;
   4117  1.49.2.2    simonb 
   4118  1.49.2.2    simonb 	/* start where we were */
   4119  1.49.2.2    simonb 	if (*result) {
   4120  1.49.2.2    simonb 		KASSERT(dir_node->dir_hash);
   4121  1.49.2.2    simonb 		dirh_e = LIST_NEXT(*result, next);
   4122  1.49.2.2    simonb 	} else {
   4123  1.49.2.2    simonb 		/* lookup all entries that match */
   4124  1.49.2.2    simonb 		dirh_e = LIST_FIRST(&dirh->free_entries);
   4125  1.49.2.2    simonb 	}
   4126  1.49.2.2    simonb 
   4127  1.49.2.2    simonb 	for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
   4128  1.49.2.2    simonb 		/* check for minimum size */
   4129  1.49.2.2    simonb 		if (dirh_e->fid_size < min_fidsize)
   4130  1.49.2.2    simonb 			continue;
   4131  1.49.2.2    simonb 		/* might be a candidate */
   4132  1.49.2.2    simonb 		*result = dirh_e;
   4133  1.49.2.2    simonb 		return 1;
   4134  1.49.2.2    simonb 	}
   4135  1.49.2.2    simonb 
   4136  1.49.2.2    simonb 	*result = NULL;
   4137  1.49.2.2    simonb 	return 0;
   4138  1.49.2.2    simonb }
   4139  1.49.2.2    simonb 
   4140  1.49.2.2    simonb 
   4141  1.49.2.2    simonb static int
   4142  1.49.2.2    simonb udf_dirhash_fill(struct udf_node *dir_node)
   4143  1.49.2.2    simonb {
   4144  1.49.2.2    simonb 	struct vnode *dvp = dir_node->vnode;
   4145  1.49.2.2    simonb 	struct udf_dirhash *dirh;
   4146      1.45   reinoud 	struct file_entry    *fe  = dir_node->fe;
   4147      1.45   reinoud 	struct extfile_entry *efe = dir_node->efe;
   4148      1.45   reinoud 	struct fileid_desc *fid;
   4149      1.45   reinoud 	struct dirent *dirent;
   4150  1.49.2.2    simonb 	uint64_t file_size, pre_diroffset, diroffset;
   4151      1.45   reinoud 	uint32_t lb_size;
   4152  1.49.2.2    simonb 	int error;
   4153  1.49.2.2    simonb 
   4154  1.49.2.2    simonb 	/* already filled/read in? */
   4155  1.49.2.2    simonb 	if (dir_node->i_flags & IN_DIRHASH_BROKEN)
   4156  1.49.2.2    simonb 		return EIO;
   4157  1.49.2.2    simonb 	if (dir_node->i_flags & IN_DIRHASH_COMPLETE)
   4158  1.49.2.2    simonb 		return 0;
   4159  1.49.2.2    simonb 
   4160  1.49.2.2    simonb 	/* make sure we have a dirhash to add to */
   4161  1.49.2.2    simonb 	udf_dirhash_init(dir_node);
   4162  1.49.2.2    simonb 	dirh = dir_node->dir_hash;
   4163  1.49.2.2    simonb 	KASSERT(dirh);
   4164      1.45   reinoud 
   4165      1.45   reinoud 	/* get directory filesize */
   4166      1.45   reinoud 	if (fe) {
   4167      1.45   reinoud 		file_size = udf_rw64(fe->inf_len);
   4168      1.45   reinoud 	} else {
   4169      1.45   reinoud 		assert(efe);
   4170      1.45   reinoud 		file_size = udf_rw64(efe->inf_len);
   4171      1.45   reinoud 	}
   4172      1.45   reinoud 
   4173      1.45   reinoud 	/* allocate temporary space for fid */
   4174      1.45   reinoud 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
   4175      1.45   reinoud 	fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
   4176       1.1   reinoud 
   4177  1.49.2.2    simonb 	/* allocate temporary space for dirent */
   4178  1.49.2.2    simonb 	dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
   4179       1.1   reinoud 
   4180  1.49.2.2    simonb 	error = 0;
   4181  1.49.2.2    simonb 	diroffset = 0;
   4182  1.49.2.2    simonb 	while (diroffset < file_size) {
   4183  1.49.2.2    simonb 		/* transfer a new fid/dirent */
   4184  1.49.2.2    simonb 		pre_diroffset = diroffset;
   4185  1.49.2.2    simonb 		error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
   4186  1.49.2.2    simonb 		if (error) {
   4187  1.49.2.2    simonb 			/* TODO what to do? continue but not add? */
   4188  1.49.2.2    simonb 			dir_node->i_flags |= IN_DIRHASH_BROKEN;
   4189  1.49.2.2    simonb 			break;
   4190  1.49.2.2    simonb 		}
   4191  1.49.2.2    simonb 
   4192  1.49.2.2    simonb 		if ((fid->file_char & UDF_FILE_CHAR_DEL)) {
   4193  1.49.2.2    simonb 			/* register deleted extent for reuse */
   4194  1.49.2.2    simonb 			udf_dirhash_enter_freed(dir_node, pre_diroffset,
   4195  1.49.2.2    simonb 				udf_fidsize(fid));
   4196  1.49.2.2    simonb 		} else {
   4197  1.49.2.2    simonb 			/* append to the dirhash */
   4198  1.49.2.2    simonb 			udf_dirhash_enter(dir_node, fid, dirent, pre_diroffset,
   4199  1.49.2.2    simonb 				udf_fidsize(fid), 0);
   4200  1.49.2.2    simonb 		}
   4201       1.9  christos 	}
   4202  1.49.2.2    simonb 	dir_node->i_flags |= IN_DIRHASH_COMPLETE;
   4203       1.1   reinoud 
   4204  1.49.2.2    simonb 	free(fid, M_UDFTEMP);
   4205  1.49.2.2    simonb 	free(dirent, M_UDFTEMP);
   4206       1.1   reinoud 
   4207  1.49.2.2    simonb 	return error;
   4208  1.49.2.2    simonb }
   4209       1.1   reinoud 
   4210  1.49.2.2    simonb 
   4211  1.49.2.2    simonb /* --------------------------------------------------------------------- */
   4212  1.49.2.2    simonb 
   4213  1.49.2.2    simonb /*
   4214  1.49.2.2    simonb  * Directory read and manipulation functions.
   4215  1.49.2.2    simonb  *
   4216  1.49.2.2    simonb  * Note that if the file is found, the cached diroffset position *before* the
   4217  1.49.2.2    simonb  * advance is remembered. Thus if the same filename is lookup again just after
   4218  1.49.2.2    simonb  * this lookup its immediately found.
   4219  1.49.2.2    simonb  */
   4220  1.49.2.2    simonb 
   4221  1.49.2.2    simonb int
   4222  1.49.2.2    simonb udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
   4223  1.49.2.2    simonb        struct long_ad *icb_loc, int *found)
   4224  1.49.2.2    simonb {
   4225  1.49.2.2    simonb 	struct udf_node  *dir_node = VTOI(vp);
   4226  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_ep;
   4227  1.49.2.2    simonb 	struct fileid_desc *fid;
   4228  1.49.2.2    simonb 	struct dirent *dirent;
   4229  1.49.2.2    simonb 	uint64_t diroffset;
   4230  1.49.2.2    simonb 	uint32_t lb_size;
   4231  1.49.2.2    simonb 	int hit, error;
   4232  1.49.2.2    simonb 
   4233  1.49.2.2    simonb 	/* set default return */
   4234  1.49.2.2    simonb 	*found = 0;
   4235  1.49.2.2    simonb 
   4236  1.49.2.2    simonb 	/* fillup with complete dir readin if needed */
   4237  1.49.2.2    simonb 	error = udf_dirhash_fill(dir_node);
   4238  1.49.2.2    simonb 	if (error)
   4239  1.49.2.2    simonb 		return error;
   4240  1.49.2.2    simonb 
   4241  1.49.2.2    simonb 	/* allocate temporary space for fid */
   4242  1.49.2.2    simonb 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
   4243  1.49.2.2    simonb 	fid     = malloc(lb_size, M_UDFTEMP, M_WAITOK);
   4244  1.49.2.2    simonb 	dirent  = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
   4245  1.49.2.2    simonb 
   4246  1.49.2.2    simonb 	DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
   4247  1.49.2.2    simonb 		namelen, namelen, name));
   4248  1.49.2.2    simonb 
   4249  1.49.2.2    simonb 	/* search our dirhash hits */
   4250  1.49.2.2    simonb 	memset(icb_loc, 0, sizeof(*icb_loc));
   4251  1.49.2.2    simonb 	dirh_ep = NULL;
   4252  1.49.2.2    simonb 	for (;;) {
   4253  1.49.2.2    simonb 		hit = udf_dirhash_lookup(dir_node, name, namelen, &dirh_ep);
   4254  1.49.2.2    simonb 		/* if no hit, abort the search */
   4255  1.49.2.2    simonb 		if (!hit)
   4256  1.49.2.2    simonb 			break;
   4257  1.49.2.2    simonb 
   4258  1.49.2.2    simonb 		/* check this hit */
   4259  1.49.2.2    simonb 		diroffset = dirh_ep->offset;
   4260       1.1   reinoud 
   4261      1.45   reinoud 		/* transfer a new fid/dirent */
   4262      1.45   reinoud 		error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
   4263       1.1   reinoud 		if (error)
   4264       1.1   reinoud 			break;
   4265       1.1   reinoud 
   4266  1.49.2.2    simonb 		DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
   4267  1.49.2.2    simonb 			dirent->d_namlen, dirent->d_namlen, dirent->d_name));
   4268       1.1   reinoud 
   4269  1.49.2.2    simonb 		/* see if its our entry */
   4270  1.49.2.2    simonb 		KASSERT(dirent->d_namlen == namelen);
   4271  1.49.2.2    simonb 		if (strncmp(dirent->d_name, name, namelen) == 0) {
   4272  1.49.2.2    simonb 			*found = 1;
   4273  1.49.2.2    simonb 			*icb_loc = fid->icb;
   4274      1.45   reinoud 			break;
   4275       1.9  christos 		}
   4276      1.45   reinoud 	}
   4277      1.45   reinoud 	free(fid, M_UDFTEMP);
   4278      1.45   reinoud 	free(dirent, M_UDFTEMP);
   4279       1.1   reinoud 
   4280  1.49.2.2    simonb 	return error;
   4281      1.45   reinoud }
   4282       1.1   reinoud 
   4283      1.45   reinoud /* --------------------------------------------------------------------- */
   4284       1.1   reinoud 
   4285      1.45   reinoud static int
   4286      1.45   reinoud udf_create_new_fe(struct udf_mount *ump, struct file_entry *fe, int file_type,
   4287      1.45   reinoud 	struct long_ad *node_icb, struct long_ad *parent_icb,
   4288      1.45   reinoud 	uint64_t parent_unique_id)
   4289      1.45   reinoud {
   4290      1.45   reinoud 	struct timespec now;
   4291      1.45   reinoud 	struct icb_tag *icb;
   4292  1.49.2.1    simonb 	struct filetimes_extattr_entry *ft_extattr;
   4293      1.45   reinoud 	uint64_t unique_id;
   4294      1.45   reinoud 	uint32_t fidsize, lb_num;
   4295  1.49.2.1    simonb 	uint8_t *bpos;
   4296  1.49.2.1    simonb 	int crclen, attrlen;
   4297      1.45   reinoud 
   4298      1.45   reinoud 	lb_num = udf_rw32(node_icb->loc.lb_num);
   4299      1.45   reinoud 	udf_inittag(ump, &fe->tag, TAGID_FENTRY, lb_num);
   4300      1.45   reinoud 	icb = &fe->icbtag;
   4301       1.1   reinoud 
   4302       1.1   reinoud 	/*
   4303      1.45   reinoud 	 * Always use strategy type 4 unless on WORM wich we don't support
   4304      1.45   reinoud 	 * (yet). Fill in defaults and set for internal allocation of data.
   4305       1.1   reinoud 	 */
   4306      1.45   reinoud 	icb->strat_type      = udf_rw16(4);
   4307      1.45   reinoud 	icb->max_num_entries = udf_rw16(1);
   4308      1.45   reinoud 	icb->file_type       = file_type;	/* 8 bit */
   4309      1.45   reinoud 	icb->flags           = udf_rw16(UDF_ICB_INTERN_ALLOC);
   4310      1.45   reinoud 
   4311      1.45   reinoud 	fe->perm     = udf_rw32(0x7fff);	/* all is allowed   */
   4312      1.45   reinoud 	fe->link_cnt = udf_rw16(0);		/* explicit setting */
   4313       1.1   reinoud 
   4314      1.45   reinoud 	fe->ckpoint  = udf_rw32(1);		/* user supplied file version */
   4315      1.45   reinoud 
   4316      1.45   reinoud 	vfs_timestamp(&now);
   4317      1.45   reinoud 	udf_timespec_to_timestamp(&now, &fe->atime);
   4318      1.45   reinoud 	udf_timespec_to_timestamp(&now, &fe->attrtime);
   4319      1.45   reinoud 	udf_timespec_to_timestamp(&now, &fe->mtime);
   4320       1.1   reinoud 
   4321      1.45   reinoud 	udf_set_regid(&fe->imp_id, IMPL_NAME);
   4322      1.45   reinoud 	udf_add_impl_regid(ump, &fe->imp_id);
   4323       1.1   reinoud 
   4324      1.45   reinoud 	unique_id = udf_advance_uniqueid(ump);
   4325      1.45   reinoud 	fe->unique_id = udf_rw64(unique_id);
   4326  1.49.2.1    simonb 	fe->l_ea = udf_rw32(0);
   4327       1.1   reinoud 
   4328  1.49.2.1    simonb 	/* create extended attribute to record our creation time */
   4329  1.49.2.1    simonb 	attrlen = UDF_FILETIMES_ATTR_SIZE(1);
   4330  1.49.2.1    simonb 	ft_extattr = malloc(attrlen, M_UDFTEMP, M_WAITOK);
   4331  1.49.2.1    simonb 	memset(ft_extattr, 0, attrlen);
   4332  1.49.2.1    simonb 	ft_extattr->hdr.type = udf_rw32(UDF_FILETIMES_ATTR_NO);
   4333  1.49.2.1    simonb 	ft_extattr->hdr.subtype = 1;	/* [4/48.10.5] */
   4334  1.49.2.1    simonb 	ft_extattr->hdr.a_l = udf_rw32(UDF_FILETIMES_ATTR_SIZE(1));
   4335  1.49.2.1    simonb 	ft_extattr->d_l     = udf_rw32(UDF_TIMESTAMP_SIZE); /* one item */
   4336  1.49.2.1    simonb 	ft_extattr->existence = UDF_FILETIMES_FILE_CREATION;
   4337  1.49.2.1    simonb 	udf_timespec_to_timestamp(&now, &ft_extattr->times[0]);
   4338  1.49.2.1    simonb 
   4339  1.49.2.1    simonb 	udf_extattr_insert_internal(ump, (union dscrptr *) fe,
   4340  1.49.2.1    simonb 		(struct extattr_entry *) ft_extattr);
   4341  1.49.2.1    simonb 	free(ft_extattr, M_UDFTEMP);
   4342  1.49.2.1    simonb 
   4343  1.49.2.1    simonb 	/* if its a directory, create '..' */
   4344  1.49.2.1    simonb 	bpos = (uint8_t *) fe->data + udf_rw32(fe->l_ea);
   4345      1.45   reinoud 	fidsize = 0;
   4346      1.45   reinoud 	if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
   4347      1.45   reinoud 		fidsize = udf_create_parentfid(ump,
   4348  1.49.2.1    simonb 			(struct fileid_desc *) bpos, parent_icb,
   4349      1.45   reinoud 			parent_unique_id);
   4350       1.9  christos 	}
   4351       1.1   reinoud 
   4352      1.45   reinoud 	/* record fidlength information */
   4353      1.45   reinoud 	fe->inf_len = udf_rw64(fidsize);
   4354      1.45   reinoud 	fe->l_ad    = udf_rw32(fidsize);
   4355      1.45   reinoud 	fe->logblks_rec = udf_rw64(0);		/* intern */
   4356      1.45   reinoud 
   4357      1.45   reinoud 	crclen  = sizeof(struct file_entry) - 1 - UDF_DESC_TAG_LENGTH;
   4358  1.49.2.1    simonb 	crclen += udf_rw32(fe->l_ea) + fidsize;
   4359      1.45   reinoud 	fe->tag.desc_crc_len = udf_rw16(crclen);
   4360       1.1   reinoud 
   4361      1.45   reinoud 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fe);
   4362       1.1   reinoud 
   4363      1.45   reinoud 	return fidsize;
   4364       1.1   reinoud }
   4365       1.1   reinoud 
   4366       1.1   reinoud /* --------------------------------------------------------------------- */
   4367       1.1   reinoud 
   4368      1.45   reinoud static int
   4369      1.45   reinoud udf_create_new_efe(struct udf_mount *ump, struct extfile_entry *efe,
   4370      1.45   reinoud 	int file_type, struct long_ad *node_icb, struct long_ad *parent_icb,
   4371      1.45   reinoud 	uint64_t parent_unique_id)
   4372      1.45   reinoud {
   4373      1.45   reinoud 	struct timespec now;
   4374      1.45   reinoud 	struct icb_tag *icb;
   4375      1.45   reinoud 	uint64_t unique_id;
   4376      1.45   reinoud 	uint32_t fidsize, lb_num;
   4377  1.49.2.1    simonb 	uint8_t *bpos;
   4378      1.45   reinoud 	int crclen;
   4379      1.45   reinoud 
   4380      1.45   reinoud 	lb_num = udf_rw32(node_icb->loc.lb_num);
   4381      1.45   reinoud 	udf_inittag(ump, &efe->tag, TAGID_EXTFENTRY, lb_num);
   4382      1.45   reinoud 	icb = &efe->icbtag;
   4383      1.45   reinoud 
   4384      1.45   reinoud 	/*
   4385      1.45   reinoud 	 * Always use strategy type 4 unless on WORM wich we don't support
   4386      1.45   reinoud 	 * (yet). Fill in defaults and set for internal allocation of data.
   4387      1.45   reinoud 	 */
   4388      1.45   reinoud 	icb->strat_type      = udf_rw16(4);
   4389      1.45   reinoud 	icb->max_num_entries = udf_rw16(1);
   4390      1.45   reinoud 	icb->file_type       = file_type;	/* 8 bit */
   4391      1.45   reinoud 	icb->flags           = udf_rw16(UDF_ICB_INTERN_ALLOC);
   4392       1.1   reinoud 
   4393      1.45   reinoud 	efe->perm     = udf_rw32(0x7fff);	/* all is allowed   */
   4394      1.45   reinoud 	efe->link_cnt = udf_rw16(0);		/* explicit setting */
   4395       1.1   reinoud 
   4396      1.45   reinoud 	efe->ckpoint  = udf_rw32(1);		/* user supplied file version */
   4397       1.1   reinoud 
   4398      1.45   reinoud 	vfs_timestamp(&now);
   4399      1.45   reinoud 	udf_timespec_to_timestamp(&now, &efe->ctime);
   4400      1.45   reinoud 	udf_timespec_to_timestamp(&now, &efe->atime);
   4401      1.45   reinoud 	udf_timespec_to_timestamp(&now, &efe->attrtime);
   4402      1.45   reinoud 	udf_timespec_to_timestamp(&now, &efe->mtime);
   4403      1.26   reinoud 
   4404      1.45   reinoud 	udf_set_regid(&efe->imp_id, IMPL_NAME);
   4405      1.45   reinoud 	udf_add_impl_regid(ump, &efe->imp_id);
   4406       1.1   reinoud 
   4407      1.45   reinoud 	unique_id = udf_advance_uniqueid(ump);
   4408      1.45   reinoud 	efe->unique_id = udf_rw64(unique_id);
   4409  1.49.2.1    simonb 	efe->l_ea = udf_rw32(0);
   4410       1.1   reinoud 
   4411  1.49.2.1    simonb 	/* if its a directory, create '..' */
   4412  1.49.2.1    simonb 	bpos = (uint8_t *) efe->data + udf_rw32(efe->l_ea);
   4413      1.45   reinoud 	fidsize = 0;
   4414      1.45   reinoud 	if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
   4415      1.45   reinoud 		fidsize = udf_create_parentfid(ump,
   4416  1.49.2.1    simonb 			(struct fileid_desc *) bpos, parent_icb,
   4417      1.45   reinoud 			parent_unique_id);
   4418      1.45   reinoud 	}
   4419       1.1   reinoud 
   4420      1.45   reinoud 	/* record fidlength information */
   4421      1.45   reinoud 	efe->obj_size = udf_rw64(fidsize);
   4422      1.45   reinoud 	efe->inf_len  = udf_rw64(fidsize);
   4423      1.45   reinoud 	efe->l_ad     = udf_rw32(fidsize);
   4424      1.45   reinoud 	efe->logblks_rec = udf_rw64(0);		/* intern */
   4425       1.1   reinoud 
   4426      1.45   reinoud 	crclen  = sizeof(struct extfile_entry) - 1 - UDF_DESC_TAG_LENGTH;
   4427  1.49.2.1    simonb 	crclen += udf_rw32(efe->l_ea) + fidsize;
   4428      1.45   reinoud 	efe->tag.desc_crc_len = udf_rw16(crclen);
   4429       1.1   reinoud 
   4430      1.45   reinoud 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) efe);
   4431       1.1   reinoud 
   4432      1.45   reinoud 	return fidsize;
   4433       1.1   reinoud }
   4434       1.1   reinoud 
   4435       1.1   reinoud /* --------------------------------------------------------------------- */
   4436       1.1   reinoud 
   4437      1.45   reinoud int
   4438      1.45   reinoud udf_dir_detach(struct udf_mount *ump, struct udf_node *dir_node,
   4439      1.45   reinoud 	struct udf_node *udf_node, struct componentname *cnp)
   4440       1.1   reinoud {
   4441  1.49.2.2    simonb 	struct vnode *dvp = dir_node->vnode;
   4442  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_ep;
   4443      1.45   reinoud 	struct file_entry    *fe  = dir_node->fe;
   4444      1.45   reinoud 	struct extfile_entry *efe = dir_node->efe;
   4445      1.45   reinoud 	struct fileid_desc *fid;
   4446      1.45   reinoud 	struct dirent *dirent;
   4447      1.45   reinoud 	uint64_t file_size, diroffset;
   4448      1.45   reinoud 	uint32_t lb_size, fidsize;
   4449      1.45   reinoud 	int found, error;
   4450      1.45   reinoud 	char const *name  = cnp->cn_nameptr;
   4451      1.45   reinoud 	int namelen = cnp->cn_namelen;
   4452  1.49.2.2    simonb 	int hit, refcnt;
   4453       1.1   reinoud 
   4454      1.45   reinoud 	/* get directory filesize */
   4455      1.45   reinoud 	if (fe) {
   4456      1.45   reinoud 		file_size = udf_rw64(fe->inf_len);
   4457      1.45   reinoud 	} else {
   4458      1.45   reinoud 		assert(efe);
   4459      1.45   reinoud 		file_size = udf_rw64(efe->inf_len);
   4460      1.45   reinoud 	}
   4461       1.1   reinoud 
   4462      1.45   reinoud 	/* allocate temporary space for fid */
   4463      1.45   reinoud 	lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
   4464  1.49.2.2    simonb 	fid     = malloc(lb_size, M_UDFTEMP, M_WAITOK);
   4465  1.49.2.2    simonb 	dirent  = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
   4466       1.1   reinoud 
   4467  1.49.2.2    simonb 	/* search our dirhash hits */
   4468      1.45   reinoud 	found = 0;
   4469  1.49.2.2    simonb 	dirh_ep = NULL;
   4470  1.49.2.2    simonb 	for (;;) {
   4471  1.49.2.2    simonb 		hit = udf_dirhash_lookup(dir_node, name, namelen, &dirh_ep);
   4472  1.49.2.2    simonb 		/* if no hit, abort the search */
   4473  1.49.2.2    simonb 		if (!hit)
   4474  1.49.2.2    simonb 			break;
   4475      1.45   reinoud 
   4476  1.49.2.2    simonb 		/* check this hit */
   4477  1.49.2.2    simonb 		diroffset = dirh_ep->offset;
   4478       1.1   reinoud 
   4479      1.45   reinoud 		/* transfer a new fid/dirent */
   4480  1.49.2.2    simonb 		error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
   4481      1.45   reinoud 		if (error)
   4482      1.45   reinoud 			break;
   4483       1.1   reinoud 
   4484  1.49.2.2    simonb 		/* see if its our entry */
   4485  1.49.2.2    simonb 		KASSERT(dirent->d_namlen == namelen);
   4486  1.49.2.2    simonb 		if (strncmp(dirent->d_name, name, namelen) == 0) {
   4487  1.49.2.2    simonb 			found = 1;
   4488      1.45   reinoud 			break;
   4489      1.45   reinoud 		}
   4490      1.45   reinoud 	}
   4491  1.49.2.2    simonb 
   4492  1.49.2.2    simonb 	if (!found)
   4493  1.49.2.2    simonb 		error = ENOENT;
   4494  1.49.2.2    simonb 	if (error)
   4495  1.49.2.2    simonb 		goto error_out;
   4496       1.1   reinoud 
   4497      1.45   reinoud 	/* mark deleted */
   4498      1.45   reinoud 	fid->file_char |= UDF_FILE_CHAR_DEL;
   4499      1.45   reinoud #ifdef UDF_COMPLETE_DELETE
   4500      1.45   reinoud 	memset(&fid->icb, 0, sizeof(fid->icb));
   4501      1.45   reinoud #endif
   4502      1.45   reinoud 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
   4503       1.1   reinoud 
   4504  1.49.2.2    simonb 	/* get size of fid and compensate for the read_fid_stream advance */
   4505      1.45   reinoud 	fidsize = udf_fidsize(fid);
   4506      1.45   reinoud 	diroffset -= fidsize;
   4507      1.45   reinoud 
   4508      1.45   reinoud 	/* write out */
   4509      1.45   reinoud 	error = vn_rdwr(UIO_WRITE, dir_node->vnode,
   4510      1.45   reinoud 			fid, fidsize, diroffset,
   4511      1.45   reinoud 			UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
   4512      1.45   reinoud 			FSCRED, NULL, NULL);
   4513  1.49.2.2    simonb 	if (error)
   4514  1.49.2.2    simonb 		goto error_out;
   4515  1.49.2.2    simonb 
   4516  1.49.2.2    simonb 	/* get reference count of attached node */
   4517  1.49.2.2    simonb 	if (udf_node->fe) {
   4518  1.49.2.2    simonb 		refcnt = udf_rw16(udf_node->fe->link_cnt);
   4519  1.49.2.2    simonb 	} else {
   4520  1.49.2.2    simonb 		KASSERT(udf_node->efe);
   4521  1.49.2.2    simonb 		refcnt = udf_rw16(udf_node->efe->link_cnt);
   4522  1.49.2.2    simonb 	}
   4523      1.45   reinoud #ifdef UDF_COMPLETE_DELETE
   4524  1.49.2.2    simonb 	/* substract reference counter in attached node */
   4525  1.49.2.2    simonb 	refcnt -= 1;
   4526  1.49.2.2    simonb 	if (udf_node->fe) {
   4527  1.49.2.2    simonb 		udf_node->fe->link_cnt = udf_rw16(refcnt);
   4528  1.49.2.2    simonb 	} else {
   4529  1.49.2.2    simonb 		udf_node->efe->link_cnt = udf_rw16(refcnt);
   4530  1.49.2.2    simonb 	}
   4531       1.1   reinoud 
   4532  1.49.2.2    simonb 	/* prevent writeout when refcnt == 0 */
   4533  1.49.2.2    simonb 	if (refcnt == 0)
   4534  1.49.2.2    simonb 		udf_node->i_flags |= IN_DELETED;
   4535       1.1   reinoud 
   4536  1.49.2.2    simonb 	if (fid->file_char & UDF_FILE_CHAR_DIR) {
   4537  1.49.2.2    simonb 		int drefcnt;
   4538       1.1   reinoud 
   4539  1.49.2.2    simonb 		/* substract reference counter in directory node */
   4540  1.49.2.2    simonb 		/* note subtract 2 (?) for its was also backreferenced */
   4541  1.49.2.2    simonb 		if (dir_node->fe) {
   4542  1.49.2.2    simonb 			drefcnt  = udf_rw16(dir_node->fe->link_cnt);
   4543  1.49.2.2    simonb 			drefcnt -= 1;
   4544  1.49.2.2    simonb 			dir_node->fe->link_cnt = udf_rw16(drefcnt);
   4545  1.49.2.2    simonb 		} else {
   4546  1.49.2.2    simonb 			KASSERT(dir_node->efe);
   4547  1.49.2.2    simonb 			drefcnt  = udf_rw16(dir_node->efe->link_cnt);
   4548  1.49.2.2    simonb 			drefcnt -= 1;
   4549  1.49.2.2    simonb 			dir_node->efe->link_cnt = udf_rw16(drefcnt);
   4550  1.49.2.2    simonb 		}
   4551      1.45   reinoud 	}
   4552       1.1   reinoud 
   4553  1.49.2.2    simonb 	udf_node->i_flags |= IN_MODIFIED;
   4554  1.49.2.2    simonb 	dir_node->i_flags |= IN_MODIFIED;
   4555  1.49.2.2    simonb #endif
   4556  1.49.2.2    simonb 	/* if it is/was a hardlink adjust the file count */
   4557  1.49.2.2    simonb 	if (refcnt > 0)
   4558  1.49.2.2    simonb 		udf_adjust_filecount(udf_node, -1);
   4559  1.49.2.2    simonb 
   4560  1.49.2.2    simonb 	/* remove from the dirhash */
   4561  1.49.2.2    simonb 	udf_dirhash_remove(dir_node, dirent, diroffset,
   4562  1.49.2.2    simonb 		udf_fidsize(fid));
   4563  1.49.2.2    simonb 
   4564  1.49.2.2    simonb error_out:
   4565      1.45   reinoud 	free(fid, M_UDFTEMP);
   4566      1.45   reinoud 	free(dirent, M_UDFTEMP);
   4567       1.1   reinoud 
   4568      1.45   reinoud 	return error;
   4569       1.1   reinoud }
   4570       1.1   reinoud 
   4571       1.1   reinoud /* --------------------------------------------------------------------- */
   4572       1.1   reinoud 
   4573      1.45   reinoud /*
   4574      1.45   reinoud  * We are not allowed to split the fid tag itself over an logical block so
   4575      1.45   reinoud  * check the space remaining in the logical block.
   4576      1.45   reinoud  *
   4577      1.45   reinoud  * We try to select the smallest candidate for recycling or when none is
   4578      1.45   reinoud  * found, append a new one at the end of the directory.
   4579      1.45   reinoud  */
   4580      1.45   reinoud 
   4581       1.1   reinoud int
   4582      1.45   reinoud udf_dir_attach(struct udf_mount *ump, struct udf_node *dir_node,
   4583      1.45   reinoud 	struct udf_node *udf_node, struct vattr *vap, struct componentname *cnp)
   4584       1.1   reinoud {
   4585      1.45   reinoud 	struct vnode *dvp = dir_node->vnode;
   4586  1.49.2.2    simonb 	struct udf_dirhash_entry *dirh_ep;
   4587      1.45   reinoud 	struct fileid_desc   *fid;
   4588      1.45   reinoud 	struct icb_tag       *icbtag;
   4589      1.45   reinoud 	struct charspec osta_charspec;
   4590      1.45   reinoud 	struct dirent   dirent;
   4591      1.45   reinoud 	uint64_t unique_id, dir_size, diroffset;
   4592      1.45   reinoud 	uint64_t fid_pos, end_fid_pos, chosen_fid_pos;
   4593      1.45   reinoud 	uint32_t chosen_size, chosen_size_diff;
   4594      1.45   reinoud 	int lb_size, lb_rest, fidsize, this_fidsize, size_diff;
   4595  1.49.2.2    simonb 	int file_char, refcnt, icbflags, addr_type, hit, error;
   4596      1.45   reinoud 
   4597      1.45   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   4598      1.45   reinoud 	udf_osta_charset(&osta_charspec);
   4599      1.45   reinoud 
   4600      1.45   reinoud 	if (dir_node->fe) {
   4601      1.45   reinoud 		dir_size = udf_rw64(dir_node->fe->inf_len);
   4602  1.49.2.2    simonb 		icbtag   = &dir_node->fe->icbtag;
   4603      1.45   reinoud 	} else {
   4604      1.45   reinoud 		dir_size = udf_rw64(dir_node->efe->inf_len);
   4605  1.49.2.2    simonb 		icbtag   = &dir_node->efe->icbtag;
   4606      1.45   reinoud 	}
   4607       1.1   reinoud 
   4608      1.45   reinoud 	icbflags   = udf_rw16(icbtag->flags);
   4609      1.45   reinoud 	addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   4610       1.1   reinoud 
   4611      1.45   reinoud 	if (udf_node->fe) {
   4612      1.45   reinoud 		unique_id = udf_rw64(udf_node->fe->unique_id);
   4613  1.49.2.2    simonb 		refcnt    = udf_rw16(udf_node->fe->link_cnt);
   4614      1.45   reinoud 	} else {
   4615      1.45   reinoud 		unique_id = udf_rw64(udf_node->efe->unique_id);
   4616  1.49.2.2    simonb 		refcnt    = udf_rw16(udf_node->efe->link_cnt);
   4617      1.45   reinoud 	}
   4618       1.1   reinoud 
   4619      1.45   reinoud 	if (refcnt > 0) {
   4620      1.45   reinoud 		unique_id = udf_advance_uniqueid(ump);
   4621      1.45   reinoud 		udf_adjust_filecount(udf_node, 1);
   4622       1.9  christos 	}
   4623       1.1   reinoud 
   4624      1.45   reinoud 	/* determine file characteristics */
   4625      1.45   reinoud 	file_char = 0;	/* visible non deleted file and not stream metadata */
   4626      1.45   reinoud 	if (vap->va_type == VDIR)
   4627      1.45   reinoud 		file_char = UDF_FILE_CHAR_DIR;
   4628      1.45   reinoud 
   4629      1.45   reinoud 	/* malloc scrap buffer */
   4630      1.45   reinoud 	fid = malloc(lb_size, M_TEMP, M_WAITOK);
   4631      1.45   reinoud 	bzero(fid, lb_size);
   4632      1.45   reinoud 
   4633      1.45   reinoud 	/* calculate _minimum_ fid size */
   4634      1.45   reinoud 	unix_to_udf_name((char *) fid->data, &fid->l_fi,
   4635      1.45   reinoud 		cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
   4636      1.45   reinoud 	fidsize = UDF_FID_SIZE + fid->l_fi;
   4637      1.45   reinoud 	fidsize = (fidsize + 3) & ~3;		/* multiple of 4 */
   4638       1.1   reinoud 
   4639      1.45   reinoud 	/* find position that will fit the FID */
   4640  1.49.2.2    simonb 	chosen_fid_pos   = dir_size;
   4641      1.45   reinoud 	chosen_size      = 0;
   4642      1.45   reinoud 	chosen_size_diff = UINT_MAX;
   4643       1.1   reinoud 
   4644  1.49.2.2    simonb 	/* shut up gcc */
   4645  1.49.2.2    simonb 	dirent.d_namlen = 0;
   4646  1.49.2.2    simonb 
   4647  1.49.2.2    simonb 	/* search our dirhash hits */
   4648  1.49.2.2    simonb 	error = 0;
   4649  1.49.2.2    simonb 	dirh_ep = NULL;
   4650      1.45   reinoud 	for (;;) {
   4651  1.49.2.2    simonb 		hit = udf_dirhash_lookup_freed(dir_node, fidsize, &dirh_ep);
   4652  1.49.2.2    simonb 		/* if no hit, abort the search */
   4653  1.49.2.2    simonb 		if (!hit)
   4654      1.45   reinoud 			break;
   4655       1.1   reinoud 
   4656  1.49.2.2    simonb 		/* check this hit for size */
   4657  1.49.2.2    simonb 		this_fidsize = dirh_ep->fid_size;
   4658       1.1   reinoud 
   4659  1.49.2.2    simonb 		/* check this hit */
   4660  1.49.2.2    simonb 		fid_pos     = dirh_ep->offset;
   4661  1.49.2.2    simonb 		end_fid_pos = fid_pos + this_fidsize;
   4662  1.49.2.2    simonb 		size_diff   = this_fidsize - fidsize;
   4663  1.49.2.2    simonb 		lb_rest = lb_size - (end_fid_pos % lb_size);
   4664      1.45   reinoud 
   4665      1.45   reinoud #ifndef UDF_COMPLETE_DELETE
   4666  1.49.2.2    simonb 		/* transfer a new fid/dirent */
   4667  1.49.2.2    simonb 		error = udf_read_fid_stream(vp, &fid_pos, fid, dirent);
   4668  1.49.2.2    simonb 		if (error)
   4669  1.49.2.2    simonb 			goto error_out;
   4670       1.1   reinoud 
   4671  1.49.2.2    simonb 		/* only reuse entries that are wiped */
   4672  1.49.2.2    simonb 		/* check if the len + loc are marked zero */
   4673  1.49.2.2    simonb 		if (udf_rw32(fid->icb.len != 0))
   4674  1.49.2.2    simonb 			continue;
   4675  1.49.2.2    simonb 		if (udf_rw32(fid->icb.loc.lb_num) != 0)
   4676  1.49.2.2    simonb 			continue;
   4677  1.49.2.2    simonb 		if (udf_rw16(fid->icb.loc.part_num != 0))
   4678  1.49.2.2    simonb 			continue;
   4679  1.49.2.2    simonb #endif	/* UDF_COMPLETE_DELETE */
   4680  1.49.2.2    simonb 
   4681  1.49.2.2    simonb 		/* select if not splitting the tag and its smaller */
   4682  1.49.2.2    simonb 		if ((size_diff >= 0)  &&
   4683  1.49.2.2    simonb 			(size_diff < chosen_size_diff) &&
   4684  1.49.2.2    simonb 			(lb_rest >= sizeof(struct desc_tag)))
   4685  1.49.2.2    simonb 		{
   4686  1.49.2.2    simonb 			/* UDF 2.3.4.2+3 specifies rules for iu size */
   4687  1.49.2.2    simonb 			if ((size_diff == 0) || (size_diff >= 32)) {
   4688  1.49.2.2    simonb 				chosen_fid_pos   = fid_pos;
   4689  1.49.2.2    simonb 				chosen_size      = this_fidsize;
   4690  1.49.2.2    simonb 				chosen_size_diff = size_diff;
   4691  1.49.2.2    simonb 			}
   4692      1.45   reinoud 		}
   4693      1.45   reinoud 	}
   4694  1.49.2.2    simonb 
   4695       1.1   reinoud 
   4696      1.45   reinoud 	/* extend directory if no other candidate found */
   4697      1.45   reinoud 	if (chosen_size == 0) {
   4698      1.45   reinoud 		chosen_fid_pos   = dir_size;
   4699      1.45   reinoud 		chosen_size      = fidsize;
   4700      1.45   reinoud 		chosen_size_diff = 0;
   4701       1.1   reinoud 
   4702      1.45   reinoud 		/* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
   4703      1.45   reinoud 		if (addr_type == UDF_ICB_INTERN_ALLOC) {
   4704      1.45   reinoud 			/* pre-grow directory to see if we're to switch */
   4705      1.45   reinoud 			udf_grow_node(dir_node, dir_size + chosen_size);
   4706       1.1   reinoud 
   4707      1.45   reinoud 			icbflags   = udf_rw16(icbtag->flags);
   4708      1.45   reinoud 			addr_type  = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   4709      1.45   reinoud 		}
   4710       1.1   reinoud 
   4711      1.45   reinoud 		/* make sure the next fid desc_tag won't be splitted */
   4712      1.45   reinoud 		if (addr_type != UDF_ICB_INTERN_ALLOC) {
   4713      1.45   reinoud 			end_fid_pos = chosen_fid_pos + chosen_size;
   4714      1.45   reinoud 			lb_rest = lb_size - (end_fid_pos % lb_size);
   4715       1.1   reinoud 
   4716      1.45   reinoud 			/* pad with implementation use regid if needed */
   4717      1.45   reinoud 			if (lb_rest < sizeof(struct desc_tag))
   4718      1.45   reinoud 				chosen_size += 32;
   4719      1.45   reinoud 		}
   4720       1.1   reinoud 	}
   4721      1.45   reinoud 	chosen_size_diff = chosen_size - fidsize;
   4722      1.45   reinoud 	diroffset = chosen_fid_pos + chosen_size;
   4723      1.45   reinoud 
   4724      1.45   reinoud 	/* populate the FID */
   4725      1.45   reinoud 	memset(fid, 0, lb_size);
   4726      1.45   reinoud 	udf_inittag(ump, &fid->tag, TAGID_FID, 0);
   4727      1.45   reinoud 	fid->file_version_num    = udf_rw16(1);	/* UDF 2.3.4.1 */
   4728      1.45   reinoud 	fid->file_char           = file_char;
   4729      1.45   reinoud 	fid->icb                 = udf_node->loc;
   4730      1.45   reinoud 	fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
   4731      1.45   reinoud 	fid->l_iu                = udf_rw16(0);
   4732      1.45   reinoud 
   4733      1.45   reinoud 	if (chosen_size > fidsize) {
   4734      1.45   reinoud 		/* insert implementation-use regid to space it correctly */
   4735      1.45   reinoud 		fid->l_iu = udf_rw16(chosen_size_diff);
   4736      1.45   reinoud 
   4737      1.45   reinoud 		/* set implementation use */
   4738      1.45   reinoud 		udf_set_regid((struct regid *) fid->data, IMPL_NAME);
   4739      1.45   reinoud 		udf_add_impl_regid(ump, (struct regid *) fid->data);
   4740      1.45   reinoud 	}
   4741      1.45   reinoud 
   4742      1.45   reinoud 	/* fill in name */
   4743      1.45   reinoud 	unix_to_udf_name((char *) fid->data + udf_rw16(fid->l_iu),
   4744      1.45   reinoud 		&fid->l_fi, cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
   4745      1.45   reinoud 
   4746      1.45   reinoud 	fid->tag.desc_crc_len = chosen_size - UDF_DESC_TAG_LENGTH;
   4747      1.45   reinoud 	(void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
   4748      1.45   reinoud 
   4749      1.45   reinoud 	/* writeout FID/update parent directory */
   4750      1.45   reinoud 	error = vn_rdwr(UIO_WRITE, dvp,
   4751      1.45   reinoud 			fid, chosen_size, chosen_fid_pos,
   4752      1.45   reinoud 			UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
   4753      1.45   reinoud 			FSCRED, NULL, NULL);
   4754      1.45   reinoud 
   4755  1.49.2.2    simonb 	if (error)
   4756  1.49.2.2    simonb 		goto error_out;
   4757      1.45   reinoud 
   4758      1.45   reinoud 	/* add reference counter in attached node */
   4759      1.45   reinoud 	if (udf_node->fe) {
   4760      1.45   reinoud 		refcnt = udf_rw16(udf_node->fe->link_cnt);
   4761      1.45   reinoud 		udf_node->fe->link_cnt = udf_rw16(refcnt+1);
   4762      1.45   reinoud 	} else {
   4763      1.45   reinoud 		KASSERT(udf_node->efe);
   4764      1.45   reinoud 		refcnt = udf_rw16(udf_node->efe->link_cnt);
   4765      1.45   reinoud 		udf_node->efe->link_cnt = udf_rw16(refcnt+1);
   4766      1.45   reinoud 	}
   4767      1.45   reinoud 
   4768      1.45   reinoud 	/* mark not deleted if it was... just in case, but do warn */
   4769      1.45   reinoud 	if (udf_node->i_flags & IN_DELETED) {
   4770      1.45   reinoud 		printf("udf: warning, marking a file undeleted\n");
   4771      1.45   reinoud 		udf_node->i_flags &= ~IN_DELETED;
   4772      1.45   reinoud 	}
   4773      1.45   reinoud 
   4774      1.45   reinoud 	if (file_char & UDF_FILE_CHAR_DIR) {
   4775      1.45   reinoud 		/* add reference counter in directory node for '..' */
   4776      1.45   reinoud 		if (dir_node->fe) {
   4777      1.45   reinoud 			refcnt = udf_rw16(dir_node->fe->link_cnt);
   4778      1.45   reinoud 			refcnt++;
   4779      1.45   reinoud 			dir_node->fe->link_cnt = udf_rw16(refcnt);
   4780      1.45   reinoud 		} else {
   4781      1.45   reinoud 			KASSERT(dir_node->efe);
   4782      1.45   reinoud 			refcnt = udf_rw16(dir_node->efe->link_cnt);
   4783      1.45   reinoud 			refcnt++;
   4784      1.45   reinoud 			dir_node->efe->link_cnt = udf_rw16(refcnt);
   4785      1.45   reinoud 		}
   4786       1.1   reinoud 	}
   4787       1.1   reinoud 
   4788  1.49.2.2    simonb 	/* append to the dirhash */
   4789  1.49.2.2    simonb 	dirent.d_namlen = cnp->cn_namelen;
   4790  1.49.2.2    simonb 	memcpy(dirent.d_name, cnp->cn_nameptr, cnp->cn_namelen);
   4791  1.49.2.2    simonb 	udf_dirhash_enter(dir_node, fid, &dirent, chosen_fid_pos,
   4792  1.49.2.2    simonb 		udf_fidsize(fid), 1);
   4793      1.45   reinoud 
   4794  1.49.2.2    simonb 	/* note updates */
   4795      1.45   reinoud 	udf_node->i_flags |= IN_CHANGE | IN_MODIFY; /* | IN_CREATE? */
   4796      1.45   reinoud 	/* VN_KNOTE(udf_node,  ...) */
   4797  1.49.2.1    simonb 	udf_update(udf_node->vnode, NULL, NULL, NULL, 0);
   4798      1.45   reinoud 
   4799  1.49.2.2    simonb error_out:
   4800      1.45   reinoud 	free(fid, M_TEMP);
   4801       1.1   reinoud 
   4802  1.49.2.2    simonb 	return error;
   4803      1.45   reinoud }
   4804       1.1   reinoud 
   4805       1.1   reinoud /* --------------------------------------------------------------------- */
   4806       1.1   reinoud 
   4807       1.1   reinoud /*
   4808      1.45   reinoud  * Each node can have an attached streamdir node though not recursively. These
   4809      1.45   reinoud  * are otherwise known as named substreams/named extended attributes that have
   4810      1.45   reinoud  * no size limitations.
   4811       1.1   reinoud  *
   4812       1.1   reinoud  * `Normal' extended attributes are indicated with a number and are recorded
   4813       1.1   reinoud  * in either the fe/efe descriptor itself for small descriptors or recorded in
   4814      1.45   reinoud  * the attached extended attribute file. Since these spaces can get
   4815      1.45   reinoud  * fragmented, care ought to be taken.
   4816      1.45   reinoud  *
   4817      1.45   reinoud  * Since the size of the space reserved for allocation descriptors is limited,
   4818      1.45   reinoud  * there is a mechanim provided for extending this space; this is done by a
   4819      1.45   reinoud  * special extent to allow schrinking of the allocations without breaking the
   4820      1.45   reinoud  * linkage to the allocation extent descriptor.
   4821       1.1   reinoud  */
   4822       1.1   reinoud 
   4823       1.1   reinoud int
   4824       1.1   reinoud udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
   4825      1.45   reinoud 	     struct udf_node **udf_noderes)
   4826       1.1   reinoud {
   4827      1.45   reinoud 	union dscrptr   *dscr;
   4828      1.45   reinoud 	struct udf_node *udf_node;
   4829       1.1   reinoud 	struct vnode    *nvp;
   4830      1.45   reinoud 	struct long_ad   icb_loc, last_fe_icb_loc;
   4831       1.1   reinoud 	uint64_t file_size;
   4832       1.1   reinoud 	uint32_t lb_size, sector, dummy;
   4833      1.45   reinoud 	uint8_t  *file_data;
   4834       1.1   reinoud 	int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
   4835      1.45   reinoud 	int slot, eof, error;
   4836       1.1   reinoud 
   4837       1.1   reinoud 	DPRINTF(NODE, ("udf_get_node called\n"));
   4838      1.45   reinoud 	*udf_noderes = udf_node = NULL;
   4839       1.1   reinoud 
   4840      1.45   reinoud 	/* lock to disallow simultanious creation of same udf_node */
   4841      1.39        ad 	mutex_enter(&ump->get_node_lock);
   4842       1.1   reinoud 
   4843       1.1   reinoud 	DPRINTF(NODE, ("\tlookup in hash table\n"));
   4844       1.1   reinoud 	/* lookup in hash table */
   4845       1.1   reinoud 	assert(ump);
   4846       1.1   reinoud 	assert(node_icb_loc);
   4847      1.45   reinoud 	udf_node = udf_hash_lookup(ump, node_icb_loc);
   4848      1.45   reinoud 	if (udf_node) {
   4849       1.1   reinoud 		DPRINTF(NODE, ("\tgot it from the hash!\n"));
   4850       1.1   reinoud 		/* vnode is returned locked */
   4851      1.45   reinoud 		*udf_noderes = udf_node;
   4852      1.39        ad 		mutex_exit(&ump->get_node_lock);
   4853       1.1   reinoud 		return 0;
   4854       1.9  christos 	}
   4855       1.1   reinoud 
   4856      1.45   reinoud 	/* garbage check: translate udf_node_icb_loc to sectornr */
   4857       1.1   reinoud 	error = udf_translate_vtop(ump, node_icb_loc, &sector, &dummy);
   4858       1.1   reinoud 	if (error) {
   4859       1.1   reinoud 		/* no use, this will fail anyway */
   4860      1.39        ad 		mutex_exit(&ump->get_node_lock);
   4861       1.1   reinoud 		return EINVAL;
   4862       1.9  christos 	}
   4863       1.1   reinoud 
   4864      1.45   reinoud 	/* build udf_node (do initialise!) */
   4865      1.45   reinoud 	udf_node = pool_get(&udf_node_pool, PR_WAITOK);
   4866      1.45   reinoud 	memset(udf_node, 0, sizeof(struct udf_node));
   4867       1.1   reinoud 
   4868       1.1   reinoud 	DPRINTF(NODE, ("\tget new vnode\n"));
   4869       1.1   reinoud 	/* give it a vnode */
   4870       1.1   reinoud 	error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
   4871       1.1   reinoud         if (error) {
   4872      1.45   reinoud 		pool_put(&udf_node_pool, udf_node);
   4873      1.39        ad 		mutex_exit(&ump->get_node_lock);
   4874       1.1   reinoud 		return error;
   4875       1.9  christos 	}
   4876       1.1   reinoud 
   4877      1.34   msaitoh 	/* always return locked vnode */
   4878       1.1   reinoud 	if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
   4879       1.1   reinoud 		/* recycle vnode and unlock; simultanious will fail too */
   4880       1.1   reinoud 		ungetnewvnode(nvp);
   4881      1.39        ad 		mutex_exit(&ump->get_node_lock);
   4882       1.1   reinoud 		return error;
   4883       1.9  christos 	}
   4884       1.1   reinoud 
   4885       1.1   reinoud 	/* initialise crosslinks, note location of fe/efe for hashing */
   4886      1.45   reinoud 	udf_node->ump    =  ump;
   4887      1.45   reinoud 	udf_node->vnode  =  nvp;
   4888      1.45   reinoud 	nvp->v_data      =  udf_node;
   4889      1.45   reinoud 	udf_node->loc    = *node_icb_loc;
   4890      1.45   reinoud 	udf_node->lockf  =  0;
   4891      1.45   reinoud 	mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
   4892      1.45   reinoud 	cv_init(&udf_node->node_lock, "udf_nlk");
   4893      1.45   reinoud 	genfs_node_init(nvp, &udf_genfsops);	/* inititise genfs */
   4894      1.45   reinoud 	udf_node->outstanding_bufs = 0;
   4895      1.45   reinoud 	udf_node->outstanding_nodedscr = 0;
   4896       1.1   reinoud 
   4897       1.1   reinoud 	/* insert into the hash lookup */
   4898      1.45   reinoud 	udf_register_node(udf_node);
   4899       1.1   reinoud 
   4900       1.1   reinoud 	/* safe to unlock, the entry is in the hash table, vnode is locked */
   4901      1.39        ad 	mutex_exit(&ump->get_node_lock);
   4902       1.1   reinoud 
   4903       1.1   reinoud 	icb_loc = *node_icb_loc;
   4904       1.1   reinoud 	needs_indirect = 0;
   4905       1.1   reinoud 	strat4096 = 0;
   4906       1.1   reinoud 	udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
   4907       1.1   reinoud 	file_size = 0;
   4908      1.45   reinoud 	file_data = NULL;
   4909       1.1   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   4910       1.1   reinoud 
   4911      1.45   reinoud 	DPRINTF(NODE, ("\tstart reading descriptors\n"));
   4912       1.1   reinoud 	do {
   4913       1.1   reinoud 		/* try to read in fe/efe */
   4914      1.45   reinoud 		error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
   4915       1.1   reinoud 
   4916       1.1   reinoud 		/* blank sector marks end of sequence, check this */
   4917      1.45   reinoud 		if ((dscr == NULL) &&  (!strat4096))
   4918       1.1   reinoud 			error = ENOENT;
   4919       1.1   reinoud 
   4920       1.1   reinoud 		/* break if read error or blank sector */
   4921      1.45   reinoud 		if (error || (dscr == NULL))
   4922       1.1   reinoud 			break;
   4923       1.1   reinoud 
   4924       1.1   reinoud 		/* process descriptor based on the descriptor type */
   4925      1.45   reinoud 		dscr_type = udf_rw16(dscr->tag.id);
   4926      1.45   reinoud 		DPRINTF(NODE, ("\tread descriptor %d\n", dscr_type));
   4927       1.1   reinoud 
   4928       1.1   reinoud 		/* if dealing with an indirect entry, follow the link */
   4929      1.45   reinoud 		if (dscr_type == TAGID_INDIRECTENTRY) {
   4930       1.1   reinoud 			needs_indirect = 0;
   4931      1.45   reinoud 			udf_free_logvol_dscr(ump, &icb_loc, dscr);
   4932      1.45   reinoud 			icb_loc = dscr->inde.indirect_icb;
   4933       1.1   reinoud 			continue;
   4934       1.9  christos 		}
   4935       1.1   reinoud 
   4936       1.1   reinoud 		/* only file entries and extended file entries allowed here */
   4937       1.1   reinoud 		if ((dscr_type != TAGID_FENTRY) &&
   4938       1.1   reinoud 		    (dscr_type != TAGID_EXTFENTRY)) {
   4939      1.45   reinoud 			udf_free_logvol_dscr(ump, &icb_loc, dscr);
   4940       1.1   reinoud 			error = ENOENT;
   4941       1.1   reinoud 			break;
   4942       1.9  christos 		}
   4943       1.1   reinoud 
   4944      1.45   reinoud 		KASSERT(udf_tagsize(dscr, lb_size) == lb_size);
   4945       1.1   reinoud 
   4946      1.45   reinoud 		/* choose this one */
   4947      1.45   reinoud 		last_fe_icb_loc = icb_loc;
   4948      1.45   reinoud 
   4949       1.1   reinoud 		/* record and process/update (ext)fentry */
   4950      1.45   reinoud 		file_data = NULL;
   4951       1.1   reinoud 		if (dscr_type == TAGID_FENTRY) {
   4952      1.45   reinoud 			if (udf_node->fe)
   4953      1.45   reinoud 				udf_free_logvol_dscr(ump, &last_fe_icb_loc,
   4954      1.45   reinoud 					udf_node->fe);
   4955      1.45   reinoud 			udf_node->fe  = &dscr->fe;
   4956      1.45   reinoud 			strat = udf_rw16(udf_node->fe->icbtag.strat_type);
   4957      1.45   reinoud 			udf_file_type = udf_node->fe->icbtag.file_type;
   4958      1.45   reinoud 			file_size = udf_rw64(udf_node->fe->inf_len);
   4959      1.45   reinoud 			file_data = udf_node->fe->data;
   4960       1.1   reinoud 		} else {
   4961      1.45   reinoud 			if (udf_node->efe)
   4962      1.45   reinoud 				udf_free_logvol_dscr(ump, &last_fe_icb_loc,
   4963      1.45   reinoud 					udf_node->efe);
   4964      1.45   reinoud 			udf_node->efe = &dscr->efe;
   4965      1.45   reinoud 			strat = udf_rw16(udf_node->efe->icbtag.strat_type);
   4966      1.45   reinoud 			udf_file_type = udf_node->efe->icbtag.file_type;
   4967      1.45   reinoud 			file_size = udf_rw64(udf_node->efe->inf_len);
   4968      1.45   reinoud 			file_data = udf_node->efe->data;
   4969       1.9  christos 		}
   4970       1.1   reinoud 
   4971       1.1   reinoud 		/* check recording strategy (structure) */
   4972       1.1   reinoud 
   4973       1.1   reinoud 		/*
   4974       1.1   reinoud 		 * Strategy 4096 is a daisy linked chain terminating with an
   4975       1.1   reinoud 		 * unrecorded sector or a TERM descriptor. The next
   4976       1.1   reinoud 		 * descriptor is to be found in the sector that follows the
   4977       1.1   reinoud 		 * current sector.
   4978       1.1   reinoud 		 */
   4979       1.1   reinoud 		if (strat == 4096) {
   4980       1.1   reinoud 			strat4096 = 1;
   4981       1.1   reinoud 			needs_indirect = 1;
   4982       1.1   reinoud 
   4983       1.1   reinoud 			icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
   4984       1.9  christos 		}
   4985       1.1   reinoud 
   4986       1.1   reinoud 		/*
   4987       1.1   reinoud 		 * Strategy 4 is the normal strategy and terminates, but if
   4988       1.1   reinoud 		 * we're in strategy 4096, we can't have strategy 4 mixed in
   4989       1.1   reinoud 		 */
   4990       1.1   reinoud 
   4991       1.1   reinoud 		if (strat == 4) {
   4992       1.1   reinoud 			if (strat4096) {
   4993       1.1   reinoud 				error = EINVAL;
   4994       1.1   reinoud 				break;
   4995       1.9  christos 			}
   4996       1.1   reinoud 			break;		/* done */
   4997       1.9  christos 		}
   4998       1.1   reinoud 	} while (!error);
   4999       1.1   reinoud 
   5000      1.45   reinoud 	/* first round of cleanup code */
   5001       1.1   reinoud 	if (error) {
   5002      1.45   reinoud 		DPRINTF(NODE, ("\tnode fe/efe failed!\n"));
   5003       1.1   reinoud 		/* recycle udf_node */
   5004      1.45   reinoud 		udf_dispose_node(udf_node);
   5005       1.1   reinoud 
   5006      1.45   reinoud 		vlockmgr(nvp->v_vnlock, LK_RELEASE);
   5007       1.1   reinoud 		nvp->v_data = NULL;
   5008       1.1   reinoud 		ungetnewvnode(nvp);
   5009       1.1   reinoud 
   5010       1.1   reinoud 		return EINVAL;		/* error code ok? */
   5011       1.9  christos 	}
   5012      1.45   reinoud 	DPRINTF(NODE, ("\tnode fe/efe read in fine\n"));
   5013       1.1   reinoud 
   5014       1.1   reinoud 	/* assert no references to dscr anymore beyong this point */
   5015      1.45   reinoud 	assert((udf_node->fe) || (udf_node->efe));
   5016       1.1   reinoud 	dscr = NULL;
   5017       1.1   reinoud 
   5018       1.1   reinoud 	/*
   5019      1.45   reinoud 	 * Remember where to record an updated version of the descriptor. If
   5020       1.1   reinoud 	 * there is a sequence of indirect entries, icb_loc will have been
   5021       1.1   reinoud 	 * updated. Its the write disipline to allocate new space and to make
   5022       1.1   reinoud 	 * sure the chain is maintained.
   5023       1.1   reinoud 	 *
   5024       1.1   reinoud 	 * `needs_indirect' flags if the next location is to be filled with
   5025       1.1   reinoud 	 * with an indirect entry.
   5026       1.1   reinoud 	 */
   5027      1.45   reinoud 	udf_node->write_loc = icb_loc;
   5028      1.45   reinoud 	udf_node->needs_indirect = needs_indirect;
   5029      1.45   reinoud 
   5030      1.45   reinoud 	/*
   5031      1.45   reinoud 	 * Go trough all allocations extents of this descriptor and when
   5032      1.45   reinoud 	 * encountering a redirect read in the allocation extension. These are
   5033      1.45   reinoud 	 * daisy-chained.
   5034      1.45   reinoud 	 */
   5035      1.45   reinoud 	UDF_LOCK_NODE(udf_node, 0);
   5036      1.45   reinoud 	udf_node->num_extensions = 0;
   5037      1.45   reinoud 
   5038      1.45   reinoud 	error   = 0;
   5039      1.45   reinoud 	slot    = 0;
   5040      1.45   reinoud 	for (;;) {
   5041      1.45   reinoud 		udf_get_adslot(udf_node, slot, &icb_loc, &eof);
   5042  1.49.2.1    simonb 		DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
   5043  1.49.2.1    simonb 			"lb_num = %d, part = %d\n", slot, eof,
   5044  1.49.2.1    simonb 			UDF_EXT_FLAGS(udf_rw32(icb_loc.len)),
   5045  1.49.2.1    simonb 			UDF_EXT_LEN(udf_rw32(icb_loc.len)),
   5046  1.49.2.1    simonb 			udf_rw32(icb_loc.loc.lb_num),
   5047  1.49.2.1    simonb 			udf_rw16(icb_loc.loc.part_num)));
   5048      1.45   reinoud 		if (eof)
   5049      1.45   reinoud 			break;
   5050  1.49.2.1    simonb 		slot++;
   5051      1.45   reinoud 
   5052  1.49.2.1    simonb 		if (UDF_EXT_FLAGS(udf_rw32(icb_loc.len)) != UDF_EXT_REDIRECT)
   5053      1.45   reinoud 			continue;
   5054      1.45   reinoud 
   5055      1.45   reinoud 		DPRINTF(NODE, ("\tgot redirect extent\n"));
   5056      1.45   reinoud 		if (udf_node->num_extensions >= UDF_MAX_ALLOC_EXTENTS) {
   5057      1.45   reinoud 			DPRINTF(ALLOC, ("udf_get_node: implementation limit, "
   5058      1.45   reinoud 					"too many allocation extensions on "
   5059      1.45   reinoud 					"udf_node\n"));
   5060      1.45   reinoud 			error = EINVAL;
   5061      1.45   reinoud 			break;
   5062      1.45   reinoud 		}
   5063      1.45   reinoud 
   5064      1.45   reinoud 		/* length can only be *one* lb : UDF 2.50/2.3.7.1 */
   5065  1.49.2.1    simonb 		if (UDF_EXT_LEN(udf_rw32(icb_loc.len)) != lb_size) {
   5066      1.45   reinoud 			DPRINTF(ALLOC, ("udf_get_node: bad allocation "
   5067      1.45   reinoud 					"extension size in udf_node\n"));
   5068      1.45   reinoud 			error = EINVAL;
   5069      1.45   reinoud 			break;
   5070      1.45   reinoud 		}
   5071      1.45   reinoud 
   5072  1.49.2.1    simonb 		DPRINTF(NODE, ("read allocation extent at lb_num %d\n",
   5073  1.49.2.1    simonb 			UDF_EXT_LEN(udf_rw32(icb_loc.loc.lb_num))));
   5074      1.45   reinoud 		/* load in allocation extent */
   5075      1.45   reinoud 		error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
   5076      1.45   reinoud 		if (error || (dscr == NULL))
   5077      1.45   reinoud 			break;
   5078      1.45   reinoud 
   5079      1.45   reinoud 		/* process read-in descriptor */
   5080      1.45   reinoud 		dscr_type = udf_rw16(dscr->tag.id);
   5081      1.45   reinoud 
   5082      1.45   reinoud 		if (dscr_type != TAGID_ALLOCEXTENT) {
   5083      1.45   reinoud 			udf_free_logvol_dscr(ump, &icb_loc, dscr);
   5084      1.45   reinoud 			error = ENOENT;
   5085      1.45   reinoud 			break;
   5086      1.45   reinoud 		}
   5087      1.45   reinoud 
   5088      1.45   reinoud 		DPRINTF(NODE, ("\trecording redirect extent\n"));
   5089      1.45   reinoud 		udf_node->ext[udf_node->num_extensions] = &dscr->aee;
   5090      1.45   reinoud 		udf_node->ext_loc[udf_node->num_extensions] = icb_loc;
   5091      1.45   reinoud 
   5092      1.45   reinoud 		udf_node->num_extensions++;
   5093      1.45   reinoud 
   5094      1.45   reinoud 	} /* while */
   5095      1.45   reinoud 	UDF_UNLOCK_NODE(udf_node, 0);
   5096      1.45   reinoud 
   5097      1.45   reinoud 	/* second round of cleanup code */
   5098      1.45   reinoud 	if (error) {
   5099      1.45   reinoud 		/* recycle udf_node */
   5100      1.45   reinoud 		udf_dispose_node(udf_node);
   5101      1.45   reinoud 
   5102      1.45   reinoud 		vlockmgr(nvp->v_vnlock, LK_RELEASE);
   5103      1.45   reinoud 		nvp->v_data = NULL;
   5104      1.45   reinoud 		ungetnewvnode(nvp);
   5105      1.45   reinoud 
   5106      1.45   reinoud 		return EINVAL;		/* error code ok? */
   5107      1.45   reinoud 	}
   5108      1.45   reinoud 
   5109      1.45   reinoud 	DPRINTF(NODE, ("\tnode read in fine\n"));
   5110       1.1   reinoud 
   5111       1.1   reinoud 	/*
   5112       1.1   reinoud 	 * Translate UDF filetypes into vnode types.
   5113       1.1   reinoud 	 *
   5114       1.1   reinoud 	 * Systemfiles like the meta main and mirror files are not treated as
   5115       1.1   reinoud 	 * normal files, so we type them as having no type. UDF dictates that
   5116       1.1   reinoud 	 * they are not allowed to be visible.
   5117       1.1   reinoud 	 */
   5118       1.1   reinoud 
   5119      1.45   reinoud 	switch (udf_file_type) {
   5120      1.45   reinoud 	case UDF_ICB_FILETYPE_DIRECTORY :
   5121      1.45   reinoud 	case UDF_ICB_FILETYPE_STREAMDIR :
   5122      1.45   reinoud 		nvp->v_type = VDIR;
   5123      1.45   reinoud 		break;
   5124      1.45   reinoud 	case UDF_ICB_FILETYPE_BLOCKDEVICE :
   5125      1.45   reinoud 		nvp->v_type = VBLK;
   5126      1.45   reinoud 		break;
   5127      1.45   reinoud 	case UDF_ICB_FILETYPE_CHARDEVICE :
   5128      1.45   reinoud 		nvp->v_type = VCHR;
   5129      1.45   reinoud 		break;
   5130      1.45   reinoud 	case UDF_ICB_FILETYPE_SOCKET :
   5131      1.45   reinoud 		nvp->v_type = VSOCK;
   5132      1.45   reinoud 		break;
   5133      1.45   reinoud 	case UDF_ICB_FILETYPE_FIFO :
   5134      1.45   reinoud 		nvp->v_type = VFIFO;
   5135      1.45   reinoud 		break;
   5136      1.45   reinoud 	case UDF_ICB_FILETYPE_SYMLINK :
   5137      1.45   reinoud 		nvp->v_type = VLNK;
   5138      1.45   reinoud 		break;
   5139      1.45   reinoud 	case UDF_ICB_FILETYPE_VAT :
   5140      1.45   reinoud 	case UDF_ICB_FILETYPE_META_MAIN :
   5141      1.45   reinoud 	case UDF_ICB_FILETYPE_META_MIRROR :
   5142      1.45   reinoud 		nvp->v_type = VNON;
   5143      1.45   reinoud 		break;
   5144      1.45   reinoud 	case UDF_ICB_FILETYPE_RANDOMACCESS :
   5145      1.45   reinoud 	case UDF_ICB_FILETYPE_REALTIME :
   5146      1.45   reinoud 		nvp->v_type = VREG;
   5147      1.45   reinoud 		break;
   5148      1.45   reinoud 	default:
   5149      1.45   reinoud 		/* YIKES, something else */
   5150      1.45   reinoud 		nvp->v_type = VNON;
   5151      1.45   reinoud 	}
   5152      1.45   reinoud 
   5153       1.1   reinoud 	/* TODO specfs, fifofs etc etc. vnops setting */
   5154      1.45   reinoud 
   5155      1.45   reinoud 	/* don't forget to set vnode's v_size */
   5156      1.45   reinoud 	uvm_vnp_setsize(nvp, file_size);
   5157      1.45   reinoud 
   5158      1.45   reinoud 	/* TODO ext attr and streamdir udf_nodes */
   5159      1.45   reinoud 
   5160      1.45   reinoud 	*udf_noderes = udf_node;
   5161      1.45   reinoud 
   5162      1.45   reinoud 	return 0;
   5163      1.45   reinoud }
   5164      1.45   reinoud 
   5165      1.45   reinoud /* --------------------------------------------------------------------- */
   5166      1.45   reinoud 
   5167      1.45   reinoud 
   5168      1.45   reinoud int
   5169      1.45   reinoud udf_writeout_node(struct udf_node *udf_node, int waitfor)
   5170      1.45   reinoud {
   5171      1.45   reinoud 	union dscrptr *dscr;
   5172      1.45   reinoud 	struct long_ad *loc;
   5173  1.49.2.2    simonb 	int extnr, flags, error;
   5174      1.45   reinoud 
   5175      1.45   reinoud 	DPRINTF(NODE, ("udf_writeout_node called\n"));
   5176      1.45   reinoud 
   5177      1.45   reinoud 	KASSERT(udf_node->outstanding_bufs == 0);
   5178      1.45   reinoud 	KASSERT(udf_node->outstanding_nodedscr == 0);
   5179      1.45   reinoud 
   5180      1.45   reinoud 	KASSERT(LIST_EMPTY(&udf_node->vnode->v_dirtyblkhd));
   5181      1.45   reinoud 
   5182      1.45   reinoud 	if (udf_node->i_flags & IN_DELETED) {
   5183      1.45   reinoud 		DPRINTF(NODE, ("\tnode deleted; not writing out\n"));
   5184      1.45   reinoud 		return 0;
   5185      1.45   reinoud 	}
   5186      1.45   reinoud 
   5187  1.49.2.2    simonb 	/* lock node */
   5188  1.49.2.2    simonb 	flags = waitfor ? 0 : IN_CALLBACK_ULK;
   5189  1.49.2.2    simonb 	UDF_LOCK_NODE(udf_node, flags);
   5190  1.49.2.2    simonb 
   5191  1.49.2.2    simonb 	/* at least one descriptor writeout */
   5192  1.49.2.2    simonb 	udf_node->outstanding_nodedscr = 1;
   5193  1.49.2.2    simonb 
   5194      1.45   reinoud 	/* we're going to write out the descriptor so clear the flags */
   5195      1.45   reinoud 	udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED);
   5196      1.45   reinoud 
   5197  1.49.2.2    simonb 	/* if we were rebuild, write out the allocation extents */
   5198  1.49.2.2    simonb 	if (udf_node->i_flags & IN_NODE_REBUILD) {
   5199  1.49.2.2    simonb 		/* mark outstanding node dscriptors and issue them */
   5200  1.49.2.2    simonb 		udf_node->outstanding_nodedscr += udf_node->num_extensions;
   5201  1.49.2.2    simonb 		for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
   5202  1.49.2.2    simonb 			loc = &udf_node->ext_loc[extnr];
   5203  1.49.2.2    simonb 			dscr = (union dscrptr *) udf_node->ext[extnr];
   5204  1.49.2.2    simonb 			error = udf_write_logvol_dscr(udf_node, dscr, loc, 0);
   5205  1.49.2.2    simonb 			if (error)
   5206  1.49.2.2    simonb 				return error;
   5207  1.49.2.2    simonb 		}
   5208  1.49.2.2    simonb 		/* mark allocation extents written out */
   5209  1.49.2.2    simonb 		udf_node->i_flags &= ~(IN_NODE_REBUILD);
   5210  1.49.2.2    simonb 	}
   5211  1.49.2.2    simonb 
   5212      1.45   reinoud 	if (udf_node->fe) {
   5213      1.45   reinoud 		dscr = (union dscrptr *) udf_node->fe;
   5214      1.45   reinoud 	} else {
   5215      1.45   reinoud 		KASSERT(udf_node->efe);
   5216      1.45   reinoud 		dscr = (union dscrptr *) udf_node->efe;
   5217      1.45   reinoud 	}
   5218      1.45   reinoud 	KASSERT(dscr);
   5219      1.45   reinoud 
   5220      1.45   reinoud 	loc = &udf_node->write_loc;
   5221      1.45   reinoud 	error = udf_write_logvol_dscr(udf_node, dscr, loc, waitfor);
   5222      1.45   reinoud 	return error;
   5223      1.45   reinoud }
   5224      1.45   reinoud 
   5225      1.45   reinoud /* --------------------------------------------------------------------- */
   5226      1.45   reinoud 
   5227      1.45   reinoud int
   5228      1.45   reinoud udf_dispose_node(struct udf_node *udf_node)
   5229      1.45   reinoud {
   5230      1.45   reinoud 	struct vnode *vp;
   5231  1.49.2.1    simonb 	int extnr;
   5232      1.45   reinoud 
   5233      1.45   reinoud 	DPRINTF(NODE, ("udf_dispose_node called on node %p\n", udf_node));
   5234      1.45   reinoud 	if (!udf_node) {
   5235      1.45   reinoud 		DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
   5236      1.45   reinoud 		return 0;
   5237      1.45   reinoud 	}
   5238      1.45   reinoud 
   5239      1.45   reinoud 	vp  = udf_node->vnode;
   5240      1.45   reinoud #ifdef DIAGNOSTIC
   5241      1.45   reinoud 	if (vp->v_numoutput)
   5242      1.45   reinoud 		panic("disposing UDF node with pending I/O's, udf_node = %p, "
   5243      1.45   reinoud 				"v_numoutput = %d", udf_node, vp->v_numoutput);
   5244      1.45   reinoud #endif
   5245      1.45   reinoud 
   5246      1.45   reinoud 	/* wait until out of sync (just in case we happen to stumble over one */
   5247      1.45   reinoud 	KASSERT(!mutex_owned(&mntvnode_lock));
   5248      1.45   reinoud 	mutex_enter(&mntvnode_lock);
   5249      1.45   reinoud 	while (udf_node->i_flags & IN_SYNCED) {
   5250      1.45   reinoud 		cv_timedwait(&udf_node->ump->dirtynodes_cv, &mntvnode_lock,
   5251      1.45   reinoud 			hz/16);
   5252      1.45   reinoud 	}
   5253      1.45   reinoud 	mutex_exit(&mntvnode_lock);
   5254      1.45   reinoud 
   5255      1.45   reinoud 	/* TODO extended attributes and streamdir */
   5256      1.45   reinoud 
   5257  1.49.2.2    simonb 	/* remove dirhash if present */
   5258  1.49.2.2    simonb 	udf_dirhash_destroy(udf_node);
   5259  1.49.2.2    simonb 
   5260      1.45   reinoud 	/* remove from our hash lookup table */
   5261      1.45   reinoud 	udf_deregister_node(udf_node);
   5262      1.45   reinoud 
   5263      1.45   reinoud 	/* destroy our lock */
   5264      1.45   reinoud 	mutex_destroy(&udf_node->node_mutex);
   5265      1.45   reinoud 	cv_destroy(&udf_node->node_lock);
   5266      1.45   reinoud 
   5267      1.45   reinoud 	/* dissociate our udf_node from the vnode */
   5268      1.45   reinoud 	genfs_node_destroy(udf_node->vnode);
   5269      1.45   reinoud 	vp->v_data = NULL;
   5270      1.45   reinoud 
   5271      1.45   reinoud 	/* free associated memory and the node itself */
   5272  1.49.2.1    simonb 	for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
   5273  1.49.2.1    simonb 		udf_free_logvol_dscr(udf_node->ump, &udf_node->ext_loc[extnr],
   5274  1.49.2.1    simonb 			udf_node->ext[extnr]);
   5275  1.49.2.1    simonb 		udf_node->ext[extnr] = (void *) 0xdeadcccc;
   5276  1.49.2.1    simonb 	}
   5277  1.49.2.1    simonb 
   5278      1.45   reinoud 	if (udf_node->fe)
   5279  1.49.2.1    simonb 		udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
   5280  1.49.2.1    simonb 			udf_node->fe);
   5281      1.45   reinoud 	if (udf_node->efe)
   5282  1.49.2.1    simonb 		udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
   5283  1.49.2.1    simonb 			udf_node->efe);
   5284      1.45   reinoud 
   5285      1.45   reinoud 	udf_node->fe  = (void *) 0xdeadaaaa;
   5286      1.45   reinoud 	udf_node->efe = (void *) 0xdeadbbbb;
   5287      1.45   reinoud 	udf_node->ump = (void *) 0xdeadbeef;
   5288      1.45   reinoud 	pool_put(&udf_node_pool, udf_node);
   5289      1.45   reinoud 
   5290      1.45   reinoud 	return 0;
   5291      1.45   reinoud }
   5292      1.45   reinoud 
   5293      1.45   reinoud 
   5294      1.45   reinoud 
   5295      1.45   reinoud /*
   5296      1.45   reinoud  * create a new node using the specified vnodeops, vap and cnp but with the
   5297      1.45   reinoud  * udf_file_type. This allows special files to be created. Use with care.
   5298      1.45   reinoud  */
   5299      1.45   reinoud 
   5300      1.45   reinoud static int
   5301      1.45   reinoud udf_create_node_raw(struct vnode *dvp, struct vnode **vpp, int udf_file_type,
   5302      1.45   reinoud 	int (**vnodeops)(void *), struct vattr *vap, struct componentname *cnp)
   5303      1.45   reinoud {
   5304      1.45   reinoud 	union dscrptr *dscr;
   5305      1.45   reinoud 	struct udf_node *dir_node = VTOI(dvp);;
   5306      1.45   reinoud 	struct udf_node *udf_node;
   5307      1.45   reinoud 	struct udf_mount *ump = dir_node->ump;
   5308      1.45   reinoud 	struct vnode *nvp;
   5309      1.45   reinoud 	struct long_ad node_icb_loc;
   5310      1.45   reinoud 	uint64_t parent_unique_id;
   5311      1.45   reinoud 	uint64_t lmapping, pmapping;
   5312      1.45   reinoud 	uint32_t lb_size, lb_num;
   5313      1.45   reinoud 	uint16_t vpart_num;
   5314  1.49.2.2    simonb 	uid_t uid;
   5315  1.49.2.2    simonb 	gid_t gid, parent_gid;
   5316      1.45   reinoud 	int fid_size, error;
   5317      1.45   reinoud 
   5318      1.45   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   5319      1.45   reinoud 	*vpp = NULL;
   5320      1.45   reinoud 
   5321      1.45   reinoud 	/* allocate vnode */
   5322      1.45   reinoud 	error = getnewvnode(VT_UDF, ump->vfs_mountp, vnodeops, &nvp);
   5323      1.45   reinoud         if (error)
   5324      1.45   reinoud 		return error;
   5325      1.45   reinoud 
   5326      1.45   reinoud 	/* lock node */
   5327      1.45   reinoud 	error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
   5328      1.45   reinoud 	if (error) {
   5329      1.45   reinoud 		nvp->v_data = NULL;
   5330      1.45   reinoud 		ungetnewvnode(nvp);
   5331      1.45   reinoud 		return error;
   5332      1.45   reinoud 	}
   5333      1.45   reinoud 
   5334      1.45   reinoud 	/* get disc allocation for one logical block */
   5335      1.45   reinoud 	error = udf_pre_allocate_space(ump, UDF_C_NODE, 1,
   5336      1.45   reinoud 			&vpart_num, &lmapping, &pmapping);
   5337      1.45   reinoud 	lb_num = lmapping;
   5338      1.45   reinoud 	if (error) {
   5339      1.45   reinoud 		vlockmgr(nvp->v_vnlock, LK_RELEASE);
   5340      1.45   reinoud 		ungetnewvnode(nvp);
   5341      1.45   reinoud 		return error;
   5342       1.9  christos 	}
   5343       1.1   reinoud 
   5344      1.45   reinoud 	/* initialise pointer to location */
   5345      1.45   reinoud 	memset(&node_icb_loc, 0, sizeof(struct long_ad));
   5346      1.45   reinoud 	node_icb_loc.len = lb_size;
   5347      1.45   reinoud 	node_icb_loc.loc.lb_num   = udf_rw32(lb_num);
   5348      1.45   reinoud 	node_icb_loc.loc.part_num = udf_rw16(vpart_num);
   5349      1.45   reinoud 
   5350      1.45   reinoud 	/* build udf_node (do initialise!) */
   5351      1.45   reinoud 	udf_node = pool_get(&udf_node_pool, PR_WAITOK);
   5352      1.45   reinoud 	memset(udf_node, 0, sizeof(struct udf_node));
   5353      1.45   reinoud 
   5354      1.45   reinoud 	/* initialise crosslinks, note location of fe/efe for hashing */
   5355      1.45   reinoud 	/* bugalert: synchronise with udf_get_node() */
   5356      1.45   reinoud 	udf_node->ump       = ump;
   5357      1.45   reinoud 	udf_node->vnode     = nvp;
   5358      1.45   reinoud 	nvp->v_data         = udf_node;
   5359      1.45   reinoud 	udf_node->loc       = node_icb_loc;
   5360      1.45   reinoud 	udf_node->write_loc = node_icb_loc;
   5361      1.45   reinoud 	udf_node->lockf     = 0;
   5362      1.45   reinoud 	mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
   5363      1.45   reinoud 	cv_init(&udf_node->node_lock, "udf_nlk");
   5364      1.45   reinoud 	udf_node->outstanding_bufs = 0;
   5365      1.45   reinoud 	udf_node->outstanding_nodedscr = 0;
   5366      1.45   reinoud 
   5367       1.1   reinoud 	/* initialise genfs */
   5368       1.1   reinoud 	genfs_node_init(nvp, &udf_genfsops);
   5369       1.1   reinoud 
   5370      1.45   reinoud 	/* insert into the hash lookup */
   5371      1.45   reinoud 	udf_register_node(udf_node);
   5372       1.1   reinoud 
   5373      1.45   reinoud 	/* get parent's unique ID for refering '..' if its a directory */
   5374      1.45   reinoud 	if (dir_node->fe) {
   5375      1.45   reinoud 		parent_unique_id = udf_rw64(dir_node->fe->unique_id);
   5376  1.49.2.2    simonb 		parent_gid       = (gid_t) udf_rw32(dir_node->fe->gid);
   5377      1.45   reinoud 	} else {
   5378      1.45   reinoud 		parent_unique_id = udf_rw64(dir_node->efe->unique_id);
   5379  1.49.2.2    simonb 		parent_gid       = (gid_t) udf_rw32(dir_node->efe->gid);
   5380      1.45   reinoud 	}
   5381       1.1   reinoud 
   5382      1.45   reinoud 	/* get descriptor */
   5383      1.45   reinoud 	udf_create_logvol_dscr(ump, udf_node, &node_icb_loc, &dscr);
   5384       1.1   reinoud 
   5385      1.45   reinoud 	/* choose a fe or an efe for it */
   5386      1.45   reinoud 	if (ump->logical_vol->tag.descriptor_ver == 2) {
   5387      1.45   reinoud 		udf_node->fe = &dscr->fe;
   5388      1.45   reinoud 		fid_size = udf_create_new_fe(ump, udf_node->fe,
   5389      1.45   reinoud 			udf_file_type, &udf_node->loc,
   5390      1.45   reinoud 			&dir_node->loc, parent_unique_id);
   5391      1.45   reinoud 		/* TODO add extended attribute for creation time */
   5392      1.45   reinoud 	} else {
   5393      1.45   reinoud 		udf_node->efe = &dscr->efe;
   5394      1.45   reinoud 		fid_size = udf_create_new_efe(ump, udf_node->efe,
   5395      1.45   reinoud 			udf_file_type, &udf_node->loc,
   5396      1.45   reinoud 			&dir_node->loc, parent_unique_id);
   5397      1.45   reinoud 	}
   5398      1.45   reinoud 	KASSERT(dscr->tag.tag_loc == udf_node->loc.loc.lb_num);
   5399       1.1   reinoud 
   5400      1.45   reinoud 	/* update vnode's size and type */
   5401      1.45   reinoud 	nvp->v_type = vap->va_type;
   5402      1.45   reinoud 	uvm_vnp_setsize(nvp, fid_size);
   5403       1.1   reinoud 
   5404      1.45   reinoud 	/* set access mode */
   5405      1.45   reinoud 	udf_setaccessmode(udf_node, vap->va_mode);
   5406       1.1   reinoud 
   5407      1.45   reinoud 	/* set ownership */
   5408  1.49.2.2    simonb 	uid = kauth_cred_geteuid(cnp->cn_cred);
   5409  1.49.2.2    simonb 	gid = parent_gid;
   5410  1.49.2.2    simonb 	udf_setownership(udf_node, uid, gid);
   5411       1.1   reinoud 
   5412      1.45   reinoud 	error = udf_dir_attach(ump, dir_node, udf_node, vap, cnp);
   5413      1.45   reinoud 	if (error) {
   5414      1.45   reinoud 		/* free disc allocation for node */
   5415      1.45   reinoud 		udf_free_allocated_space(ump, lb_num, vpart_num, 1);
   5416       1.1   reinoud 
   5417      1.45   reinoud 		/* recycle udf_node */
   5418      1.45   reinoud 		udf_dispose_node(udf_node);
   5419      1.45   reinoud 		vput(nvp);
   5420       1.1   reinoud 
   5421      1.45   reinoud 		*vpp = NULL;
   5422      1.45   reinoud 		return error;
   5423      1.45   reinoud 	}
   5424       1.1   reinoud 
   5425      1.45   reinoud 	/* adjust file count */
   5426      1.45   reinoud 	udf_adjust_filecount(udf_node, 1);
   5427       1.1   reinoud 
   5428      1.45   reinoud 	/* return result */
   5429      1.45   reinoud 	*vpp = nvp;
   5430       1.1   reinoud 
   5431      1.45   reinoud 	return 0;
   5432       1.1   reinoud }
   5433       1.1   reinoud 
   5434       1.1   reinoud 
   5435      1.45   reinoud int
   5436      1.45   reinoud udf_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
   5437      1.45   reinoud 	struct componentname *cnp)
   5438       1.1   reinoud {
   5439      1.45   reinoud 	int (**vnodeops)(void *);
   5440      1.45   reinoud 	int udf_file_type;
   5441       1.1   reinoud 
   5442      1.45   reinoud 	DPRINTF(NODE, ("udf_create_node called\n"));
   5443       1.1   reinoud 
   5444      1.45   reinoud 	/* what type are we creating ? */
   5445      1.45   reinoud 	vnodeops = udf_vnodeop_p;
   5446      1.45   reinoud 	/* start with a default */
   5447      1.45   reinoud 	udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
   5448       1.1   reinoud 
   5449      1.45   reinoud 	*vpp = NULL;
   5450       1.1   reinoud 
   5451      1.45   reinoud 	switch (vap->va_type) {
   5452      1.45   reinoud 	case VREG :
   5453      1.45   reinoud 		udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
   5454      1.45   reinoud 		break;
   5455      1.45   reinoud 	case VDIR :
   5456      1.45   reinoud 		udf_file_type = UDF_ICB_FILETYPE_DIRECTORY;
   5457      1.45   reinoud 		break;
   5458      1.45   reinoud 	case VLNK :
   5459      1.45   reinoud 		udf_file_type = UDF_ICB_FILETYPE_SYMLINK;
   5460      1.45   reinoud 		break;
   5461      1.45   reinoud 	case VBLK :
   5462      1.45   reinoud 		udf_file_type = UDF_ICB_FILETYPE_BLOCKDEVICE;
   5463      1.45   reinoud 		/* specfs */
   5464      1.45   reinoud 		return ENOTSUP;
   5465      1.45   reinoud 		break;
   5466      1.45   reinoud 	case VCHR :
   5467      1.45   reinoud 		udf_file_type = UDF_ICB_FILETYPE_CHARDEVICE;
   5468      1.45   reinoud 		/* specfs */
   5469      1.45   reinoud 		return ENOTSUP;
   5470      1.45   reinoud 		break;
   5471      1.45   reinoud 	case VFIFO :
   5472      1.45   reinoud 		udf_file_type = UDF_ICB_FILETYPE_FIFO;
   5473      1.45   reinoud 		/* specfs */
   5474      1.45   reinoud 		return ENOTSUP;
   5475      1.45   reinoud 		break;
   5476      1.45   reinoud 	case VSOCK :
   5477      1.45   reinoud 		udf_file_type = UDF_ICB_FILETYPE_SOCKET;
   5478      1.45   reinoud 		/* specfs */
   5479      1.45   reinoud 		return ENOTSUP;
   5480      1.45   reinoud 		break;
   5481      1.45   reinoud 	case VNON :
   5482      1.45   reinoud 	case VBAD :
   5483      1.45   reinoud 	default :
   5484      1.45   reinoud 		/* nothing; can we even create these? */
   5485      1.45   reinoud 		return EINVAL;
   5486       1.9  christos 	}
   5487      1.45   reinoud 
   5488      1.45   reinoud 	return udf_create_node_raw(dvp, vpp, udf_file_type, vnodeops, vap, cnp);
   5489       1.1   reinoud }
   5490       1.1   reinoud 
   5491       1.1   reinoud /* --------------------------------------------------------------------- */
   5492       1.1   reinoud 
   5493      1.45   reinoud static void
   5494      1.45   reinoud udf_free_descriptor_space(struct udf_node *udf_node, struct long_ad *loc, void *mem)
   5495      1.45   reinoud {
   5496      1.45   reinoud 	struct udf_mount *ump = udf_node->ump;
   5497      1.45   reinoud 	uint32_t lb_size, lb_num, len, num_lb;
   5498      1.45   reinoud 	uint16_t vpart_num;
   5499      1.45   reinoud 
   5500      1.45   reinoud 	/* is there really one? */
   5501      1.45   reinoud 	if (mem == NULL)
   5502      1.45   reinoud 		return;
   5503       1.1   reinoud 
   5504      1.45   reinoud 	/* got a descriptor here */
   5505  1.49.2.2    simonb 	len       = UDF_EXT_LEN(udf_rw32(loc->len));
   5506      1.45   reinoud 	lb_num    = udf_rw32(loc->loc.lb_num);
   5507      1.45   reinoud 	vpart_num = udf_rw16(loc->loc.part_num);
   5508       1.1   reinoud 
   5509      1.45   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   5510      1.45   reinoud 	num_lb = (len + lb_size -1) / lb_size;
   5511       1.1   reinoud 
   5512      1.45   reinoud 	udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
   5513       1.1   reinoud }
   5514       1.1   reinoud 
   5515       1.1   reinoud void
   5516      1.45   reinoud udf_delete_node(struct udf_node *udf_node)
   5517       1.1   reinoud {
   5518      1.45   reinoud 	void *dscr;
   5519      1.45   reinoud 	struct udf_mount *ump;
   5520      1.45   reinoud 	struct long_ad *loc;
   5521      1.45   reinoud 	int extnr, lvint, dummy;
   5522      1.45   reinoud 
   5523      1.45   reinoud 	ump = udf_node->ump;
   5524      1.45   reinoud 
   5525      1.45   reinoud 	/* paranoia check on integrity; should be open!; we could panic */
   5526      1.45   reinoud 	lvint = udf_rw32(udf_node->ump->logvol_integrity->integrity_type);
   5527      1.45   reinoud 	if (lvint == UDF_INTEGRITY_CLOSED)
   5528      1.45   reinoud 		printf("\tIntegrity was CLOSED!\n");
   5529      1.45   reinoud 
   5530      1.45   reinoud 	/* whatever the node type, change its size to zero */
   5531      1.45   reinoud 	(void) udf_resize_node(udf_node, 0, &dummy);
   5532       1.1   reinoud 
   5533      1.45   reinoud 	/* force it to be `clean'; no use writing it out */
   5534      1.45   reinoud 	udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED | IN_ACCESS |
   5535      1.45   reinoud 		IN_CHANGE | IN_UPDATE | IN_MODIFY);
   5536       1.1   reinoud 
   5537      1.45   reinoud 	/* adjust file count */
   5538      1.45   reinoud 	udf_adjust_filecount(udf_node, -1);
   5539       1.1   reinoud 
   5540       1.1   reinoud 	/*
   5541      1.45   reinoud 	 * Free its allocated descriptors; memory will be released when
   5542      1.45   reinoud 	 * vop_reclaim() is called.
   5543       1.1   reinoud 	 */
   5544      1.45   reinoud 	loc = &udf_node->loc;
   5545      1.45   reinoud 
   5546      1.45   reinoud 	dscr = udf_node->fe;
   5547      1.45   reinoud 	udf_free_descriptor_space(udf_node, loc, dscr);
   5548      1.45   reinoud 	dscr = udf_node->efe;
   5549      1.45   reinoud 	udf_free_descriptor_space(udf_node, loc, dscr);
   5550       1.1   reinoud 
   5551      1.45   reinoud 	for (extnr = 0; extnr < UDF_MAX_ALLOC_EXTENTS; extnr++) {
   5552      1.45   reinoud 		dscr =  udf_node->ext[extnr];
   5553      1.45   reinoud 		loc  = &udf_node->ext_loc[extnr];
   5554      1.45   reinoud 		udf_free_descriptor_space(udf_node, loc, dscr);
   5555       1.9  christos 	}
   5556       1.1   reinoud }
   5557       1.1   reinoud 
   5558       1.1   reinoud /* --------------------------------------------------------------------- */
   5559       1.1   reinoud 
   5560      1.45   reinoud /* set new filesize; node but be LOCKED on entry and is locked on exit */
   5561      1.45   reinoud int
   5562      1.45   reinoud udf_resize_node(struct udf_node *udf_node, uint64_t new_size, int *extended)
   5563       1.1   reinoud {
   5564      1.45   reinoud 	struct file_entry    *fe  = udf_node->fe;
   5565      1.45   reinoud 	struct extfile_entry *efe = udf_node->efe;
   5566      1.45   reinoud 	uint64_t file_size;
   5567      1.45   reinoud 	int error;
   5568       1.1   reinoud 
   5569      1.45   reinoud 	if (fe) {
   5570      1.45   reinoud 		file_size  = udf_rw64(fe->inf_len);
   5571       1.1   reinoud 	} else {
   5572       1.1   reinoud 		assert(udf_node->efe);
   5573      1.45   reinoud 		file_size  = udf_rw64(efe->inf_len);
   5574       1.9  christos 	}
   5575       1.1   reinoud 
   5576      1.45   reinoud 	DPRINTF(ATTR, ("\tchanging file length from %"PRIu64" to %"PRIu64"\n",
   5577      1.45   reinoud 			file_size, new_size));
   5578      1.45   reinoud 
   5579      1.45   reinoud 	/* if not changing, we're done */
   5580      1.45   reinoud 	if (file_size == new_size)
   5581      1.45   reinoud 		return 0;
   5582       1.1   reinoud 
   5583      1.45   reinoud 	*extended = (new_size > file_size);
   5584      1.45   reinoud 	if (*extended) {
   5585      1.45   reinoud 		error = udf_grow_node(udf_node, new_size);
   5586      1.45   reinoud 	} else {
   5587      1.45   reinoud 		error = udf_shrink_node(udf_node, new_size);
   5588      1.45   reinoud 	}
   5589       1.1   reinoud 
   5590      1.45   reinoud 	return error;
   5591       1.1   reinoud }
   5592       1.1   reinoud 
   5593      1.45   reinoud 
   5594       1.1   reinoud /* --------------------------------------------------------------------- */
   5595       1.1   reinoud 
   5596      1.45   reinoud void
   5597      1.45   reinoud udf_itimes(struct udf_node *udf_node, struct timespec *acc,
   5598  1.49.2.1    simonb 	struct timespec *mod, struct timespec *birth)
   5599       1.1   reinoud {
   5600      1.45   reinoud 	struct timespec now;
   5601       1.1   reinoud 	struct file_entry    *fe;
   5602       1.1   reinoud 	struct extfile_entry *efe;
   5603  1.49.2.1    simonb 	struct filetimes_extattr_entry *ft_extattr;
   5604  1.49.2.1    simonb 	struct timestamp *atime, *mtime, *attrtime, *ctime;
   5605  1.49.2.1    simonb 	struct timestamp  fe_ctime;
   5606  1.49.2.1    simonb 	struct timespec   cur_birth;
   5607  1.49.2.1    simonb 	uint32_t offset, a_l;
   5608  1.49.2.1    simonb 	uint8_t *filedata;
   5609  1.49.2.1    simonb 	int error;
   5610      1.45   reinoud 
   5611      1.45   reinoud 	/* protect against rogue values */
   5612      1.45   reinoud 	if (!udf_node)
   5613      1.45   reinoud 		return;
   5614      1.45   reinoud 
   5615      1.45   reinoud 	fe  = udf_node->fe;
   5616      1.45   reinoud 	efe = udf_node->efe;
   5617      1.45   reinoud 
   5618      1.45   reinoud 	if (!(udf_node->i_flags & (IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY)))
   5619      1.45   reinoud 		return;
   5620       1.1   reinoud 
   5621      1.45   reinoud 	/* get descriptor information */
   5622      1.45   reinoud 	if (fe) {
   5623      1.45   reinoud 		atime    = &fe->atime;
   5624      1.45   reinoud 		mtime    = &fe->mtime;
   5625      1.45   reinoud 		attrtime = &fe->attrtime;
   5626  1.49.2.1    simonb 		filedata = fe->data;
   5627  1.49.2.1    simonb 
   5628  1.49.2.1    simonb 		/* initial save dummy setting */
   5629  1.49.2.1    simonb 		ctime    = &fe_ctime;
   5630  1.49.2.1    simonb 
   5631  1.49.2.1    simonb 		/* check our extended attribute if present */
   5632  1.49.2.1    simonb 		error = udf_extattr_search_intern(udf_node,
   5633  1.49.2.1    simonb 			UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
   5634  1.49.2.1    simonb 		if (!error) {
   5635  1.49.2.1    simonb 			ft_extattr = (struct filetimes_extattr_entry *)
   5636  1.49.2.1    simonb 				(filedata + offset);
   5637  1.49.2.1    simonb 			if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
   5638  1.49.2.1    simonb 				ctime = &ft_extattr->times[0];
   5639  1.49.2.1    simonb 		}
   5640  1.49.2.1    simonb 		/* TODO create the extended attribute if not found ? */
   5641       1.1   reinoud 	} else {
   5642      1.45   reinoud 		assert(udf_node->efe);
   5643      1.45   reinoud 		atime    = &efe->atime;
   5644      1.45   reinoud 		mtime    = &efe->mtime;
   5645      1.45   reinoud 		attrtime = &efe->attrtime;
   5646  1.49.2.1    simonb 		ctime    = &efe->ctime;
   5647      1.45   reinoud 	}
   5648      1.45   reinoud 
   5649      1.45   reinoud 	vfs_timestamp(&now);
   5650      1.45   reinoud 
   5651      1.45   reinoud 	/* set access time */
   5652      1.45   reinoud 	if (udf_node->i_flags & IN_ACCESS) {
   5653      1.45   reinoud 		if (acc == NULL)
   5654      1.45   reinoud 			acc = &now;
   5655      1.45   reinoud 		udf_timespec_to_timestamp(acc, atime);
   5656      1.45   reinoud 	}
   5657      1.45   reinoud 
   5658      1.45   reinoud 	/* set modification time */
   5659      1.45   reinoud 	if (udf_node->i_flags & (IN_UPDATE | IN_MODIFY)) {
   5660      1.45   reinoud 		if (mod == NULL)
   5661      1.45   reinoud 			mod = &now;
   5662      1.45   reinoud 		udf_timespec_to_timestamp(mod, mtime);
   5663  1.49.2.1    simonb 
   5664  1.49.2.1    simonb 		/* ensure birthtime is older than set modification! */
   5665  1.49.2.1    simonb 		udf_timestamp_to_timespec(udf_node->ump, ctime, &cur_birth);
   5666  1.49.2.1    simonb 		if ((cur_birth.tv_sec > mod->tv_sec) ||
   5667  1.49.2.1    simonb 			  ((cur_birth.tv_sec == mod->tv_sec) &&
   5668  1.49.2.1    simonb 			     (cur_birth.tv_nsec > mod->tv_nsec))) {
   5669  1.49.2.1    simonb 			udf_timespec_to_timestamp(mod, ctime);
   5670  1.49.2.1    simonb 		}
   5671      1.45   reinoud 	}
   5672      1.45   reinoud 
   5673  1.49.2.1    simonb 	/* update birthtime if specified */
   5674  1.49.2.1    simonb 	/* XXX we asume here that given birthtime is older than mod */
   5675  1.49.2.1    simonb 	if (birth && (birth->tv_sec != VNOVAL)) {
   5676  1.49.2.1    simonb 		udf_timespec_to_timestamp(birth, ctime);
   5677       1.9  christos 	}
   5678       1.1   reinoud 
   5679  1.49.2.1    simonb 	/* set change time */
   5680  1.49.2.1    simonb 	if (udf_node->i_flags & (IN_CHANGE | IN_MODIFY))
   5681  1.49.2.1    simonb 		udf_timespec_to_timestamp(&now, attrtime);
   5682  1.49.2.1    simonb 
   5683      1.45   reinoud 	/* notify updates to the node itself */
   5684      1.45   reinoud 	if (udf_node->i_flags & (IN_ACCESS | IN_MODIFY))
   5685      1.45   reinoud 		udf_node->i_flags |= IN_ACCESSED;
   5686      1.45   reinoud 	if (udf_node->i_flags & (IN_UPDATE | IN_CHANGE))
   5687      1.45   reinoud 		udf_node->i_flags |= IN_MODIFIED;
   5688      1.45   reinoud 
   5689      1.45   reinoud 	/* clear modification flags */
   5690      1.45   reinoud 	udf_node->i_flags &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
   5691      1.45   reinoud }
   5692      1.45   reinoud 
   5693      1.45   reinoud /* --------------------------------------------------------------------- */
   5694       1.1   reinoud 
   5695      1.45   reinoud int
   5696      1.45   reinoud udf_update(struct vnode *vp, struct timespec *acc,
   5697  1.49.2.1    simonb 	struct timespec *mod, struct timespec *birth, int updflags)
   5698      1.45   reinoud {
   5699      1.45   reinoud 	struct udf_node  *udf_node = VTOI(vp);
   5700      1.45   reinoud 	struct udf_mount *ump = udf_node->ump;
   5701      1.45   reinoud 	struct regid     *impl_id;
   5702      1.45   reinoud 	int mnt_async = (vp->v_mount->mnt_flag & MNT_ASYNC);
   5703      1.45   reinoud 	int waitfor, flags;
   5704      1.31   reinoud 
   5705      1.45   reinoud #ifdef DEBUG
   5706      1.45   reinoud 	char bits[128];
   5707  1.49.2.1    simonb 	DPRINTF(CALL, ("udf_update(node, %p, %p, %p, %d)\n", acc, mod, birth,
   5708  1.49.2.1    simonb 		updflags));
   5709      1.45   reinoud 	bitmask_snprintf(udf_node->i_flags, IN_FLAGBITS, bits, sizeof(bits));
   5710      1.45   reinoud 	DPRINTF(CALL, ("\tnode flags %s\n", bits));
   5711      1.45   reinoud 	DPRINTF(CALL, ("\t\tmnt_async = %d\n", mnt_async));
   5712      1.45   reinoud #endif
   5713      1.31   reinoud 
   5714      1.45   reinoud 	/* set our times */
   5715  1.49.2.1    simonb 	udf_itimes(udf_node, acc, mod, birth);
   5716      1.37    rumble 
   5717      1.45   reinoud 	/* set our implementation id */
   5718      1.45   reinoud 	if (udf_node->fe) {
   5719      1.45   reinoud 		impl_id = &udf_node->fe->imp_id;
   5720      1.45   reinoud 	} else {
   5721      1.45   reinoud 		impl_id = &udf_node->efe->imp_id;
   5722      1.45   reinoud 	}
   5723      1.45   reinoud 	udf_set_regid(impl_id, IMPL_NAME);
   5724      1.45   reinoud 	udf_add_impl_regid(ump, impl_id);
   5725      1.18   reinoud 
   5726      1.45   reinoud 	/* if called when mounted readonly, never write back */
   5727      1.45   reinoud 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   5728      1.45   reinoud 		return 0;
   5729       1.1   reinoud 
   5730      1.45   reinoud 	/* check if the node is dirty 'enough'*/
   5731      1.45   reinoud 	if (updflags & UPDATE_CLOSE) {
   5732      1.45   reinoud 		flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
   5733      1.45   reinoud 	} else {
   5734      1.45   reinoud 		flags = udf_node->i_flags & IN_MODIFIED;
   5735      1.45   reinoud 	}
   5736      1.45   reinoud 	if (flags == 0)
   5737      1.45   reinoud 		return 0;
   5738      1.19   reinoud 
   5739      1.45   reinoud 	/* determine if we need to write sync or async */
   5740      1.45   reinoud 	waitfor = 0;
   5741      1.45   reinoud 	if ((flags & IN_MODIFIED) && (mnt_async == 0)) {
   5742      1.45   reinoud 		/* sync mounted */
   5743      1.45   reinoud 		waitfor = updflags & UPDATE_WAIT;
   5744      1.45   reinoud 		if (updflags & UPDATE_DIROP)
   5745      1.45   reinoud 			waitfor |= UPDATE_WAIT;
   5746       1.9  christos 	}
   5747      1.45   reinoud 	if (waitfor)
   5748      1.45   reinoud 		return VOP_FSYNC(vp, FSCRED, FSYNC_WAIT, 0,0);
   5749       1.1   reinoud 
   5750      1.45   reinoud 	return 0;
   5751       1.1   reinoud }
   5752       1.1   reinoud 
   5753      1.45   reinoud 
   5754       1.1   reinoud /* --------------------------------------------------------------------- */
   5755       1.1   reinoud 
   5756  1.49.2.2    simonb 
   5757       1.1   reinoud /*
   5758       1.1   reinoud  * Read one fid and process it into a dirent and advance to the next (*fid)
   5759       1.1   reinoud  * has to be allocated a logical block in size, (*dirent) struct dirent length
   5760       1.1   reinoud  */
   5761       1.1   reinoud 
   5762       1.1   reinoud int
   5763       1.1   reinoud udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
   5764       1.1   reinoud 		    struct fileid_desc *fid, struct dirent *dirent)
   5765       1.1   reinoud {
   5766       1.1   reinoud 	struct udf_node  *dir_node = VTOI(vp);
   5767       1.1   reinoud 	struct udf_mount *ump = dir_node->ump;
   5768      1.45   reinoud 	struct file_entry    *fe  = dir_node->fe;
   5769      1.45   reinoud 	struct extfile_entry *efe = dir_node->efe;
   5770      1.45   reinoud 	uint32_t      fid_size, lb_size;
   5771       1.1   reinoud 	uint64_t      file_size;
   5772       1.1   reinoud 	char         *fid_name;
   5773       1.1   reinoud 	int           enough, error;
   5774       1.1   reinoud 
   5775       1.1   reinoud 	assert(fid);
   5776       1.1   reinoud 	assert(dirent);
   5777       1.1   reinoud 	assert(dir_node);
   5778       1.1   reinoud 	assert(offset);
   5779       1.1   reinoud 	assert(*offset != 1);
   5780       1.1   reinoud 
   5781      1.45   reinoud 	DPRINTF(FIDS, ("read_fid_stream called at offset %"PRIu64"\n", *offset));
   5782       1.1   reinoud 	/* check if we're past the end of the directory */
   5783      1.45   reinoud 	if (fe) {
   5784       1.1   reinoud 		file_size = udf_rw64(fe->inf_len);
   5785       1.1   reinoud 	} else {
   5786       1.1   reinoud 		assert(dir_node->efe);
   5787       1.1   reinoud 		file_size = udf_rw64(efe->inf_len);
   5788       1.9  christos 	}
   5789       1.1   reinoud 	if (*offset >= file_size)
   5790       1.1   reinoud 		return EINVAL;
   5791       1.1   reinoud 
   5792       1.1   reinoud 	/* get maximum length of FID descriptor */
   5793       1.1   reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   5794       1.1   reinoud 
   5795       1.1   reinoud 	/* initialise return values */
   5796      1.45   reinoud 	fid_size = 0;
   5797       1.1   reinoud 	memset(dirent, 0, sizeof(struct dirent));
   5798       1.1   reinoud 	memset(fid, 0, lb_size);
   5799       1.1   reinoud 
   5800      1.45   reinoud 	enough  = (file_size - (*offset) >= UDF_FID_SIZE);
   5801      1.45   reinoud 	if (!enough) {
   5802      1.45   reinoud 		/* short dir ... */
   5803      1.45   reinoud 		return EIO;
   5804      1.45   reinoud 	}
   5805       1.1   reinoud 
   5806      1.45   reinoud 	error = vn_rdwr(UIO_READ, vp,
   5807      1.45   reinoud 			fid, MIN(file_size - (*offset), lb_size), *offset,
   5808      1.45   reinoud 			UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, FSCRED,
   5809      1.45   reinoud 			NULL, NULL);
   5810       1.1   reinoud 	if (error)
   5811       1.1   reinoud 		return error;
   5812       1.1   reinoud 
   5813      1.45   reinoud 	DPRINTF(FIDS, ("\tfid piece read in fine\n"));
   5814       1.1   reinoud 	/*
   5815       1.1   reinoud 	 * Check if we got a whole descriptor.
   5816      1.45   reinoud 	 * TODO Try to `resync' directory stream when something is very wrong.
   5817       1.1   reinoud 	 */
   5818       1.1   reinoud 
   5819       1.1   reinoud 	/* check if our FID header is OK */
   5820       1.1   reinoud 	error = udf_check_tag(fid);
   5821      1.45   reinoud 	if (error) {
   5822      1.45   reinoud 		goto brokendir;
   5823       1.9  christos 	}
   5824      1.45   reinoud 	DPRINTF(FIDS, ("\ttag check ok\n"));
   5825       1.1   reinoud 
   5826      1.45   reinoud 	if (udf_rw16(fid->tag.id) != TAGID_FID) {
   5827      1.45   reinoud 		error = EIO;
   5828      1.45   reinoud 		goto brokendir;
   5829       1.9  christos 	}
   5830      1.45   reinoud 	DPRINTF(FIDS, ("\ttag checked ok: got TAGID_FID\n"));
   5831       1.1   reinoud 
   5832      1.45   reinoud 	/* check for length */
   5833      1.45   reinoud 	fid_size = udf_fidsize(fid);
   5834      1.45   reinoud 	enough = (file_size - (*offset) >= fid_size);
   5835       1.1   reinoud 	if (!enough) {
   5836      1.45   reinoud 		error = EIO;
   5837      1.45   reinoud 		goto brokendir;
   5838       1.9  christos 	}
   5839      1.45   reinoud 	DPRINTF(FIDS, ("\tthe complete fid is read in\n"));
   5840       1.1   reinoud 
   5841       1.1   reinoud 	/* check FID contents */
   5842      1.45   reinoud 	error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
   5843      1.45   reinoud brokendir:
   5844       1.1   reinoud 	if (error) {
   5845       1.1   reinoud 		/* note that is sometimes a bit quick to report */
   5846       1.1   reinoud 		printf("BROKEN DIRECTORY ENTRY\n");
   5847       1.1   reinoud 		/* RESYNC? */
   5848       1.1   reinoud 		/* TODO: use udf_resync_fid_stream */
   5849       1.1   reinoud 		return EIO;
   5850       1.9  christos 	}
   5851      1.45   reinoud 	DPRINTF(FIDS, ("\tpayload checked ok\n"));
   5852       1.1   reinoud 
   5853       1.1   reinoud 	/* we got a whole and valid descriptor! */
   5854      1.45   reinoud 	DPRINTF(FIDS, ("\tinterpret FID\n"));
   5855       1.1   reinoud 
   5856       1.1   reinoud 	/* create resulting dirent structure */
   5857       1.1   reinoud 	fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
   5858      1.48   reinoud 	udf_to_unix_name(dirent->d_name, MAXNAMLEN,
   5859       1.1   reinoud 		fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
   5860       1.1   reinoud 
   5861       1.1   reinoud 	/* '..' has no name, so provide one */
   5862       1.1   reinoud 	if (fid->file_char & UDF_FILE_CHAR_PAR)
   5863       1.1   reinoud 		strcpy(dirent->d_name, "..");
   5864       1.1   reinoud 
   5865       1.1   reinoud 	dirent->d_fileno = udf_calchash(&fid->icb);	/* inode hash XXX */
   5866       1.1   reinoud 	dirent->d_namlen = strlen(dirent->d_name);
   5867       1.1   reinoud 	dirent->d_reclen = _DIRENT_SIZE(dirent);
   5868       1.1   reinoud 
   5869       1.1   reinoud 	/*
   5870       1.1   reinoud 	 * Note that its not worth trying to go for the filetypes now... its
   5871       1.1   reinoud 	 * too expensive too
   5872       1.1   reinoud 	 */
   5873       1.1   reinoud 	dirent->d_type = DT_UNKNOWN;
   5874       1.1   reinoud 
   5875       1.1   reinoud 	/* initial guess for filetype we can make */
   5876       1.1   reinoud 	if (fid->file_char & UDF_FILE_CHAR_DIR)
   5877       1.1   reinoud 		dirent->d_type = DT_DIR;
   5878       1.1   reinoud 
   5879       1.1   reinoud 	/* advance */
   5880      1.45   reinoud 	*offset += fid_size;
   5881       1.1   reinoud 
   5882       1.1   reinoud 	return error;
   5883       1.1   reinoud }
   5884       1.1   reinoud 
   5885      1.45   reinoud 
   5886      1.45   reinoud /* --------------------------------------------------------------------- */
   5887      1.45   reinoud 
   5888      1.45   reinoud static void
   5889      1.45   reinoud udf_sync_pass(struct udf_mount *ump, kauth_cred_t cred, int waitfor,
   5890      1.45   reinoud 	int pass, int *ndirty)
   5891      1.45   reinoud {
   5892      1.45   reinoud 	struct udf_node *udf_node, *n_udf_node;
   5893      1.45   reinoud 	struct vnode *vp;
   5894      1.45   reinoud 	int vdirty, error;
   5895      1.45   reinoud 	int on_type, on_flags, on_vnode;
   5896      1.45   reinoud 
   5897      1.45   reinoud derailed:
   5898      1.45   reinoud 	KASSERT(mutex_owned(&mntvnode_lock));
   5899      1.45   reinoud 
   5900      1.45   reinoud 	DPRINTF(SYNC, ("sync_pass %d\n", pass));
   5901      1.45   reinoud 	udf_node = LIST_FIRST(&ump->sorted_udf_nodes);
   5902      1.45   reinoud 	for (;udf_node; udf_node = n_udf_node) {
   5903      1.45   reinoud 		DPRINTF(SYNC, ("."));
   5904      1.45   reinoud 
   5905      1.45   reinoud 		udf_node->i_flags &= ~IN_SYNCED;
   5906      1.45   reinoud 		vp = udf_node->vnode;
   5907      1.45   reinoud 
   5908      1.45   reinoud 		mutex_enter(&vp->v_interlock);
   5909      1.45   reinoud 		n_udf_node = LIST_NEXT(udf_node, sortchain);
   5910      1.45   reinoud 		if (n_udf_node)
   5911      1.45   reinoud 			n_udf_node->i_flags |= IN_SYNCED;
   5912      1.45   reinoud 
   5913      1.45   reinoud 		/* system nodes are not synced this way */
   5914      1.45   reinoud 		if (vp->v_vflag & VV_SYSTEM) {
   5915      1.45   reinoud 			mutex_exit(&vp->v_interlock);
   5916      1.45   reinoud 			continue;
   5917      1.45   reinoud 		}
   5918      1.45   reinoud 
   5919      1.45   reinoud 		/* check if its dirty enough to even try */
   5920      1.45   reinoud 		on_type  = (waitfor == MNT_LAZY || vp->v_type == VNON);
   5921      1.45   reinoud 		on_flags = ((udf_node->i_flags &
   5922      1.45   reinoud 			(IN_ACCESSED | IN_UPDATE | IN_MODIFIED)) == 0);
   5923      1.45   reinoud 		on_vnode = LIST_EMPTY(&vp->v_dirtyblkhd)
   5924      1.45   reinoud 			&& UVM_OBJ_IS_CLEAN(&vp->v_uobj);
   5925      1.45   reinoud 		if (on_type || (on_flags || on_vnode)) { /* XXX */
   5926      1.45   reinoud 			/* not dirty (enough?) */
   5927      1.45   reinoud 			mutex_exit(&vp->v_interlock);
   5928      1.45   reinoud 			continue;
   5929      1.45   reinoud 		}
   5930      1.45   reinoud 
   5931      1.45   reinoud 		mutex_exit(&mntvnode_lock);
   5932      1.45   reinoud 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
   5933      1.45   reinoud 		if (error) {
   5934      1.45   reinoud 			mutex_enter(&mntvnode_lock);
   5935      1.45   reinoud 			if (error == ENOENT)
   5936      1.45   reinoud 				goto derailed;
   5937      1.45   reinoud 			*ndirty += 1;
   5938      1.45   reinoud 			continue;
   5939      1.45   reinoud 		}
   5940      1.45   reinoud 
   5941      1.45   reinoud 		switch (pass) {
   5942      1.45   reinoud 		case 1:
   5943      1.45   reinoud 			VOP_FSYNC(vp, cred, 0 | FSYNC_DATAONLY,0,0);
   5944      1.45   reinoud 			break;
   5945      1.45   reinoud 		case 2:
   5946      1.45   reinoud 			vdirty = vp->v_numoutput;
   5947      1.45   reinoud 			if (vp->v_tag == VT_UDF)
   5948      1.45   reinoud 				vdirty += udf_node->outstanding_bufs +
   5949      1.45   reinoud 					udf_node->outstanding_nodedscr;
   5950      1.45   reinoud 			if (vdirty == 0)
   5951      1.45   reinoud 				VOP_FSYNC(vp, cred, 0,0,0);
   5952      1.45   reinoud 			*ndirty += vdirty;
   5953      1.45   reinoud 			break;
   5954      1.45   reinoud 		case 3:
   5955      1.45   reinoud 			vdirty = vp->v_numoutput;
   5956      1.45   reinoud 			if (vp->v_tag == VT_UDF)
   5957      1.45   reinoud 				vdirty += udf_node->outstanding_bufs +
   5958      1.45   reinoud 					udf_node->outstanding_nodedscr;
   5959      1.45   reinoud 			*ndirty += vdirty;
   5960      1.45   reinoud 			break;
   5961      1.45   reinoud 		}
   5962      1.45   reinoud 
   5963      1.45   reinoud 		vput(vp);
   5964      1.45   reinoud 		mutex_enter(&mntvnode_lock);
   5965      1.45   reinoud 	}
   5966      1.45   reinoud 	DPRINTF(SYNC, ("END sync_pass %d\n", pass));
   5967      1.45   reinoud }
   5968      1.45   reinoud 
   5969      1.45   reinoud 
   5970      1.45   reinoud void
   5971      1.45   reinoud udf_do_sync(struct udf_mount *ump, kauth_cred_t cred, int waitfor)
   5972      1.45   reinoud {
   5973      1.45   reinoud 	int dummy, ndirty;
   5974      1.45   reinoud 
   5975      1.45   reinoud 	mutex_enter(&mntvnode_lock);
   5976      1.45   reinoud recount:
   5977      1.45   reinoud 	dummy = 0;
   5978      1.45   reinoud 	DPRINTF(CALL, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
   5979      1.45   reinoud 	DPRINTF(SYNC, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
   5980      1.45   reinoud 	udf_sync_pass(ump, cred, waitfor, 1, &dummy);
   5981      1.45   reinoud 
   5982      1.45   reinoud 	DPRINTF(CALL, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
   5983      1.45   reinoud 	DPRINTF(SYNC, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
   5984      1.45   reinoud 	udf_sync_pass(ump, cred, waitfor, 2, &dummy);
   5985      1.45   reinoud 
   5986      1.45   reinoud 	if (waitfor == MNT_WAIT) {
   5987      1.45   reinoud 		ndirty = ump->devvp->v_numoutput;
   5988      1.45   reinoud 		DPRINTF(NODE, ("counting pending blocks: on devvp %d\n",
   5989      1.45   reinoud 			ndirty));
   5990      1.45   reinoud 		udf_sync_pass(ump, cred, waitfor, 3, &ndirty);
   5991      1.45   reinoud 		DPRINTF(NODE, ("counted num dirty pending blocks %d\n",
   5992      1.45   reinoud 			ndirty));
   5993      1.45   reinoud 
   5994      1.45   reinoud 		if (ndirty) {
   5995      1.45   reinoud 			/* 1/4 second wait */
   5996      1.45   reinoud 			cv_timedwait(&ump->dirtynodes_cv, &mntvnode_lock,
   5997      1.45   reinoud 				hz/4);
   5998      1.45   reinoud 			goto recount;
   5999      1.45   reinoud 		}
   6000      1.45   reinoud 	}
   6001      1.45   reinoud 
   6002      1.45   reinoud 	mutex_exit(&mntvnode_lock);
   6003      1.45   reinoud }
   6004      1.45   reinoud 
   6005       1.1   reinoud /* --------------------------------------------------------------------- */
   6006       1.1   reinoud 
   6007       1.1   reinoud /*
   6008      1.45   reinoud  * Read and write file extent in/from the buffer.
   6009      1.45   reinoud  *
   6010      1.45   reinoud  * The splitup of the extent into seperate request-buffers is to minimise
   6011      1.45   reinoud  * copying around as much as possible.
   6012      1.45   reinoud  *
   6013       1.1   reinoud  * block based file reading and writing
   6014       1.1   reinoud  */
   6015       1.1   reinoud 
   6016       1.1   reinoud static int
   6017       1.1   reinoud udf_read_internal(struct udf_node *node, uint8_t *blob)
   6018       1.1   reinoud {
   6019       1.1   reinoud 	struct udf_mount *ump;
   6020      1.45   reinoud 	struct file_entry     *fe = node->fe;
   6021      1.45   reinoud 	struct extfile_entry *efe = node->efe;
   6022       1.1   reinoud 	uint64_t inflen;
   6023       1.1   reinoud 	uint32_t sector_size;
   6024       1.1   reinoud 	uint8_t  *pos;
   6025       1.1   reinoud 	int icbflags, addr_type;
   6026       1.1   reinoud 
   6027       1.1   reinoud 	/* get extent and do some paranoia checks */
   6028       1.1   reinoud 	ump = node->ump;
   6029       1.1   reinoud 	sector_size = ump->discinfo.sector_size;
   6030       1.1   reinoud 
   6031       1.1   reinoud 	if (fe) {
   6032       1.1   reinoud 		inflen   = udf_rw64(fe->inf_len);
   6033       1.1   reinoud 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
   6034       1.1   reinoud 		icbflags = udf_rw16(fe->icbtag.flags);
   6035      1.45   reinoud 	} else {
   6036      1.45   reinoud 		assert(node->efe);
   6037       1.1   reinoud 		inflen   = udf_rw64(efe->inf_len);
   6038       1.1   reinoud 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
   6039       1.1   reinoud 		icbflags = udf_rw16(efe->icbtag.flags);
   6040       1.9  christos 	}
   6041       1.1   reinoud 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   6042       1.1   reinoud 
   6043       1.1   reinoud 	assert(addr_type == UDF_ICB_INTERN_ALLOC);
   6044       1.1   reinoud 	assert(inflen < sector_size);
   6045       1.1   reinoud 
   6046       1.1   reinoud 	/* copy out info */
   6047       1.1   reinoud 	memset(blob, 0, sector_size);
   6048       1.1   reinoud 	memcpy(blob, pos, inflen);
   6049       1.1   reinoud 
   6050       1.1   reinoud 	return 0;
   6051       1.1   reinoud }
   6052       1.1   reinoud 
   6053       1.1   reinoud 
   6054      1.45   reinoud static int
   6055      1.45   reinoud udf_write_internal(struct udf_node *node, uint8_t *blob)
   6056       1.1   reinoud {
   6057      1.45   reinoud 	struct udf_mount *ump;
   6058      1.45   reinoud 	struct file_entry     *fe = node->fe;
   6059      1.45   reinoud 	struct extfile_entry *efe = node->efe;
   6060      1.45   reinoud 	uint64_t inflen;
   6061       1.1   reinoud 	uint32_t sector_size;
   6062      1.45   reinoud 	uint8_t  *pos;
   6063      1.45   reinoud 	int icbflags, addr_type;
   6064      1.45   reinoud 
   6065      1.45   reinoud 	/* get extent and do some paranoia checks */
   6066      1.45   reinoud 	ump = node->ump;
   6067      1.45   reinoud 	sector_size = ump->discinfo.sector_size;
   6068       1.1   reinoud 
   6069      1.45   reinoud 	if (fe) {
   6070      1.45   reinoud 		inflen   = udf_rw64(fe->inf_len);
   6071      1.45   reinoud 		pos      = &fe->data[0] + udf_rw32(fe->l_ea);
   6072      1.45   reinoud 		icbflags = udf_rw16(fe->icbtag.flags);
   6073      1.45   reinoud 	} else {
   6074      1.45   reinoud 		assert(node->efe);
   6075      1.45   reinoud 		inflen   = udf_rw64(efe->inf_len);
   6076      1.45   reinoud 		pos      = &efe->data[0] + udf_rw32(efe->l_ea);
   6077      1.45   reinoud 		icbflags = udf_rw16(efe->icbtag.flags);
   6078      1.45   reinoud 	}
   6079      1.45   reinoud 	addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
   6080       1.1   reinoud 
   6081      1.45   reinoud 	assert(addr_type == UDF_ICB_INTERN_ALLOC);
   6082      1.45   reinoud 	assert(inflen < sector_size);
   6083       1.1   reinoud 
   6084      1.45   reinoud 	/* copy in blob */
   6085      1.45   reinoud 	/* memset(pos, 0, inflen); */
   6086      1.45   reinoud 	memcpy(pos, blob, inflen);
   6087       1.1   reinoud 
   6088      1.45   reinoud 	return 0;
   6089       1.1   reinoud }
   6090       1.1   reinoud 
   6091       1.1   reinoud 
   6092       1.1   reinoud void
   6093      1.45   reinoud udf_read_filebuf(struct udf_node *udf_node, struct buf *buf)
   6094       1.1   reinoud {
   6095       1.1   reinoud 	struct buf *nestbuf;
   6096      1.45   reinoud 	struct udf_mount *ump = udf_node->ump;
   6097      1.10  christos 	uint64_t   *mapping;
   6098       1.1   reinoud 	uint64_t    run_start;
   6099       1.1   reinoud 	uint32_t    sector_size;
   6100       1.1   reinoud 	uint32_t    buf_offset, sector, rbuflen, rblk;
   6101      1.45   reinoud 	uint32_t    from, lblkno;
   6102      1.45   reinoud 	uint32_t    sectors;
   6103       1.1   reinoud 	uint8_t    *buf_pos;
   6104      1.45   reinoud 	int error, run_length, isdir, what;
   6105       1.1   reinoud 
   6106      1.45   reinoud 	sector_size = udf_node->ump->discinfo.sector_size;
   6107       1.1   reinoud 
   6108       1.1   reinoud 	from    = buf->b_blkno;
   6109       1.1   reinoud 	sectors = buf->b_bcount / sector_size;
   6110       1.1   reinoud 
   6111      1.45   reinoud 	isdir   = (udf_node->vnode->v_type == VDIR);
   6112      1.45   reinoud 	what    = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
   6113      1.45   reinoud 
   6114       1.1   reinoud 	/* assure we have enough translation slots */
   6115      1.45   reinoud 	KASSERT(buf->b_bcount / sector_size <= UDF_MAX_MAPPINGS);
   6116      1.45   reinoud 	KASSERT(MAXPHYS / sector_size <= UDF_MAX_MAPPINGS);
   6117       1.1   reinoud 
   6118      1.45   reinoud 	if (sectors > UDF_MAX_MAPPINGS) {
   6119       1.1   reinoud 		printf("udf_read_filebuf: implementation limit on bufsize\n");
   6120       1.1   reinoud 		buf->b_error  = EIO;
   6121       1.1   reinoud 		biodone(buf);
   6122       1.1   reinoud 		return;
   6123       1.9  christos 	}
   6124       1.1   reinoud 
   6125      1.45   reinoud 	mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
   6126      1.10  christos 
   6127       1.1   reinoud 	error = 0;
   6128       1.1   reinoud 	DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
   6129      1.45   reinoud 	error = udf_translate_file_extent(udf_node, from, sectors, mapping);
   6130       1.1   reinoud 	if (error) {
   6131       1.1   reinoud 		buf->b_error  = error;
   6132       1.1   reinoud 		biodone(buf);
   6133      1.10  christos 		goto out;
   6134       1.9  christos 	}
   6135       1.1   reinoud 	DPRINTF(READ, ("\ttranslate extent went OK\n"));
   6136       1.1   reinoud 
   6137      1.45   reinoud 	/* pre-check if its an internal */
   6138       1.1   reinoud 	if (*mapping == UDF_TRANS_INTERN) {
   6139      1.45   reinoud 		error = udf_read_internal(udf_node, (uint8_t *) buf->b_data);
   6140      1.45   reinoud 		if (error)
   6141       1.1   reinoud 			buf->b_error  = error;
   6142       1.1   reinoud 		biodone(buf);
   6143      1.10  christos 		goto out;
   6144       1.9  christos 	}
   6145       1.1   reinoud 	DPRINTF(READ, ("\tnot intern\n"));
   6146       1.1   reinoud 
   6147      1.45   reinoud #ifdef DEBUG
   6148      1.45   reinoud 	if (udf_verbose & UDF_DEBUG_TRANSLATE) {
   6149      1.45   reinoud 		printf("Returned translation table:\n");
   6150      1.45   reinoud 		for (sector = 0; sector < sectors; sector++) {
   6151      1.45   reinoud 			printf("%d : %"PRIu64"\n", sector, mapping[sector]);
   6152      1.45   reinoud 		}
   6153      1.45   reinoud 	}
   6154      1.45   reinoud #endif
   6155      1.45   reinoud 
   6156      1.45   reinoud 	/* request read-in of data from disc sheduler */
   6157       1.1   reinoud 	buf->b_resid = buf->b_bcount;
   6158       1.1   reinoud 	for (sector = 0; sector < sectors; sector++) {
   6159       1.1   reinoud 		buf_offset = sector * sector_size;
   6160       1.1   reinoud 		buf_pos    = (uint8_t *) buf->b_data + buf_offset;
   6161       1.1   reinoud 		DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
   6162       1.1   reinoud 
   6163      1.45   reinoud 		/* check if its zero or unmapped to stop reading */
   6164       1.1   reinoud 		switch (mapping[sector]) {
   6165       1.1   reinoud 		case UDF_TRANS_UNMAPPED:
   6166       1.1   reinoud 		case UDF_TRANS_ZERO:
   6167      1.45   reinoud 			/* copy zero sector TODO runlength like below */
   6168       1.1   reinoud 			memset(buf_pos, 0, sector_size);
   6169       1.1   reinoud 			DPRINTF(READ, ("\treturning zero sector\n"));
   6170       1.1   reinoud 			nestiobuf_done(buf, sector_size, 0);
   6171       1.1   reinoud 			break;
   6172       1.1   reinoud 		default :
   6173       1.1   reinoud 			DPRINTF(READ, ("\tread sector "
   6174       1.1   reinoud 			    "%"PRIu64"\n", mapping[sector]));
   6175       1.1   reinoud 
   6176      1.45   reinoud 			lblkno = from + sector;
   6177       1.1   reinoud 			run_start  = mapping[sector];
   6178       1.1   reinoud 			run_length = 1;
   6179       1.1   reinoud 			while (sector < sectors-1) {
   6180       1.1   reinoud 				if (mapping[sector+1] != mapping[sector]+1)
   6181       1.1   reinoud 					break;
   6182       1.1   reinoud 				run_length++;
   6183       1.1   reinoud 				sector++;
   6184       1.9  christos 			}
   6185       1.1   reinoud 
   6186       1.1   reinoud 			/*
   6187       1.1   reinoud 			 * nest an iobuf and mark it for async reading. Since
   6188       1.1   reinoud 			 * we're using nested buffers, they can't be cached by
   6189       1.1   reinoud 			 * design.
   6190       1.1   reinoud 			 */
   6191       1.1   reinoud 			rbuflen = run_length * sector_size;
   6192       1.1   reinoud 			rblk    = run_start  * (sector_size/DEV_BSIZE);
   6193       1.1   reinoud 
   6194      1.44        ad 			nestbuf = getiobuf(NULL, true);
   6195       1.1   reinoud 			nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
   6196       1.1   reinoud 			/* nestbuf is B_ASYNC */
   6197       1.1   reinoud 
   6198      1.45   reinoud 			/* identify this nestbuf */
   6199      1.45   reinoud 			nestbuf->b_lblkno   = lblkno;
   6200      1.45   reinoud 			assert(nestbuf->b_vp == udf_node->vnode);
   6201      1.45   reinoud 
   6202      1.45   reinoud 			/* CD shedules on raw blkno */
   6203      1.45   reinoud 			nestbuf->b_blkno      = rblk;
   6204      1.45   reinoud 			nestbuf->b_proc       = NULL;
   6205      1.45   reinoud 			nestbuf->b_rawblkno   = rblk;
   6206      1.45   reinoud 			nestbuf->b_udf_c_type = what;
   6207      1.45   reinoud 
   6208      1.45   reinoud 			udf_discstrat_queuebuf(ump, nestbuf);
   6209       1.9  christos 		}
   6210       1.9  christos 	}
   6211      1.10  christos out:
   6212      1.45   reinoud 	/* if we're synchronously reading, wait for the completion */
   6213      1.45   reinoud 	if ((buf->b_flags & B_ASYNC) == 0)
   6214      1.45   reinoud 		biowait(buf);
   6215      1.45   reinoud 
   6216       1.1   reinoud 	DPRINTF(READ, ("\tend of read_filebuf\n"));
   6217      1.45   reinoud 	free(mapping, M_TEMP);
   6218      1.10  christos 	return;
   6219       1.1   reinoud }
   6220       1.1   reinoud 
   6221       1.1   reinoud 
   6222      1.45   reinoud void
   6223      1.45   reinoud udf_write_filebuf(struct udf_node *udf_node, struct buf *buf)
   6224      1.45   reinoud {
   6225      1.45   reinoud 	struct buf *nestbuf;
   6226      1.45   reinoud 	struct udf_mount *ump = udf_node->ump;
   6227      1.45   reinoud 	uint64_t   *mapping;
   6228      1.45   reinoud 	uint64_t    run_start;
   6229      1.45   reinoud 	uint32_t    lb_size;
   6230      1.45   reinoud 	uint32_t    buf_offset, lb_num, rbuflen, rblk;
   6231      1.45   reinoud 	uint32_t    from, lblkno;
   6232      1.45   reinoud 	uint32_t    num_lb;
   6233      1.45   reinoud 	uint8_t    *buf_pos;
   6234      1.45   reinoud 	int error, run_length, isdir, what, s;
   6235      1.45   reinoud 
   6236      1.45   reinoud 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
   6237      1.45   reinoud 
   6238      1.45   reinoud 	from   = buf->b_blkno;
   6239      1.45   reinoud 	num_lb = buf->b_bcount / lb_size;
   6240       1.1   reinoud 
   6241      1.45   reinoud 	isdir  = (udf_node->vnode->v_type == VDIR);
   6242      1.45   reinoud 	what   = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
   6243       1.1   reinoud 
   6244      1.45   reinoud 	/* assure we have enough translation slots */
   6245      1.45   reinoud 	KASSERT(buf->b_bcount / lb_size <= UDF_MAX_MAPPINGS);
   6246      1.45   reinoud 	KASSERT(MAXPHYS / lb_size <= UDF_MAX_MAPPINGS);
   6247       1.1   reinoud 
   6248      1.45   reinoud 	if (num_lb > UDF_MAX_MAPPINGS) {
   6249      1.45   reinoud 		printf("udf_write_filebuf: implementation limit on bufsize\n");
   6250      1.45   reinoud 		buf->b_error  = EIO;
   6251      1.45   reinoud 		biodone(buf);
   6252      1.45   reinoud 		return;
   6253      1.45   reinoud 	}
   6254       1.1   reinoud 
   6255      1.45   reinoud 	mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
   6256       1.1   reinoud 
   6257      1.45   reinoud 	error = 0;
   6258      1.45   reinoud 	DPRINTF(WRITE, ("\ttranslate %d-%d\n", from, num_lb));
   6259      1.45   reinoud 	error = udf_translate_file_extent(udf_node, from, num_lb, mapping);
   6260      1.45   reinoud 	if (error) {
   6261      1.45   reinoud 		buf->b_error  = error;
   6262      1.45   reinoud 		biodone(buf);
   6263      1.45   reinoud 		goto out;
   6264       1.9  christos 	}
   6265      1.45   reinoud 	DPRINTF(WRITE, ("\ttranslate extent went OK\n"));
   6266      1.45   reinoud 
   6267      1.45   reinoud 	/* if its internally mapped, we can write it in the descriptor itself */
   6268      1.45   reinoud 	if (*mapping == UDF_TRANS_INTERN) {
   6269      1.45   reinoud 		/* TODO paranoia check if we ARE going to have enough space */
   6270      1.45   reinoud 		error = udf_write_internal(udf_node, (uint8_t *) buf->b_data);
   6271      1.45   reinoud 		if (error)
   6272      1.45   reinoud 			buf->b_error  = error;
   6273      1.45   reinoud 		biodone(buf);
   6274      1.45   reinoud 		goto out;
   6275       1.9  christos 	}
   6276      1.45   reinoud 	DPRINTF(WRITE, ("\tnot intern\n"));
   6277      1.45   reinoud 
   6278      1.45   reinoud 	/* request write out of data to disc sheduler */
   6279      1.45   reinoud 	buf->b_resid = buf->b_bcount;
   6280      1.45   reinoud 	for (lb_num = 0; lb_num < num_lb; lb_num++) {
   6281      1.45   reinoud 		buf_offset = lb_num * lb_size;
   6282      1.45   reinoud 		buf_pos    = (uint8_t *) buf->b_data + buf_offset;
   6283      1.45   reinoud 		DPRINTF(WRITE, ("\tprocessing rel lb_num %d\n", lb_num));
   6284      1.45   reinoud 
   6285      1.45   reinoud 		/*
   6286      1.45   reinoud 		 * Mappings are not that important here. Just before we write
   6287      1.45   reinoud 		 * the lb_num we late-allocate them when needed and update the
   6288      1.45   reinoud 		 * mapping in the udf_node.
   6289      1.45   reinoud 		 */
   6290      1.45   reinoud 
   6291      1.45   reinoud 		/* XXX why not ignore the mapping altogether ? */
   6292      1.45   reinoud 		/* TODO estimate here how much will be late-allocated */
   6293      1.45   reinoud 		DPRINTF(WRITE, ("\twrite lb_num "
   6294      1.45   reinoud 		    "%"PRIu64, mapping[lb_num]));
   6295      1.45   reinoud 
   6296      1.45   reinoud 		lblkno = from + lb_num;
   6297      1.45   reinoud 		run_start  = mapping[lb_num];
   6298      1.45   reinoud 		run_length = 1;
   6299      1.45   reinoud 		while (lb_num < num_lb-1) {
   6300      1.45   reinoud 			if (mapping[lb_num+1] != mapping[lb_num]+1)
   6301      1.45   reinoud 				if (mapping[lb_num+1] != mapping[lb_num])
   6302      1.45   reinoud 					break;
   6303      1.45   reinoud 			run_length++;
   6304      1.45   reinoud 			lb_num++;
   6305      1.45   reinoud 		}
   6306      1.45   reinoud 		DPRINTF(WRITE, ("+ %d\n", run_length));
   6307       1.1   reinoud 
   6308      1.45   reinoud 		/* nest an iobuf on the master buffer for the extent */
   6309      1.45   reinoud 		rbuflen = run_length * lb_size;
   6310      1.45   reinoud 		rblk = run_start * (lb_size/DEV_BSIZE);
   6311       1.1   reinoud 
   6312      1.45   reinoud #if 0
   6313      1.45   reinoud 		/* if its zero or unmapped, our blknr gets -1 for unmapped */
   6314      1.45   reinoud 		switch (mapping[lb_num]) {
   6315      1.45   reinoud 		case UDF_TRANS_UNMAPPED:
   6316      1.45   reinoud 		case UDF_TRANS_ZERO:
   6317      1.45   reinoud 			rblk = -1;
   6318       1.1   reinoud 			break;
   6319      1.45   reinoud 		default:
   6320      1.45   reinoud 			rblk = run_start * (lb_size/DEV_BSIZE);
   6321       1.1   reinoud 			break;
   6322       1.9  christos 		}
   6323      1.45   reinoud #endif
   6324       1.1   reinoud 
   6325      1.45   reinoud 		nestbuf = getiobuf(NULL, true);
   6326      1.45   reinoud 		nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
   6327      1.45   reinoud 		/* nestbuf is B_ASYNC */
   6328      1.45   reinoud 
   6329      1.45   reinoud 		/* identify this nestbuf */
   6330      1.45   reinoud 		nestbuf->b_lblkno   = lblkno;
   6331      1.45   reinoud 		KASSERT(nestbuf->b_vp == udf_node->vnode);
   6332      1.45   reinoud 
   6333      1.45   reinoud 		/* CD shedules on raw blkno */
   6334      1.45   reinoud 		nestbuf->b_blkno      = rblk;
   6335      1.45   reinoud 		nestbuf->b_proc       = NULL;
   6336      1.45   reinoud 		nestbuf->b_rawblkno   = rblk;
   6337      1.45   reinoud 		nestbuf->b_udf_c_type = what;
   6338      1.45   reinoud 
   6339      1.45   reinoud 		/* increment our outstanding bufs counter */
   6340      1.45   reinoud 		s = splbio();
   6341      1.45   reinoud 			udf_node->outstanding_bufs++;
   6342      1.45   reinoud 		splx(s);
   6343       1.1   reinoud 
   6344      1.45   reinoud 		udf_discstrat_queuebuf(ump, nestbuf);
   6345       1.9  christos 	}
   6346      1.45   reinoud out:
   6347      1.45   reinoud 	/* if we're synchronously writing, wait for the completion */
   6348      1.45   reinoud 	if ((buf->b_flags & B_ASYNC) == 0)
   6349      1.45   reinoud 		biowait(buf);
   6350      1.45   reinoud 
   6351      1.45   reinoud 	DPRINTF(WRITE, ("\tend of write_filebuf\n"));
   6352      1.45   reinoud 	free(mapping, M_TEMP);
   6353      1.45   reinoud 	return;
   6354       1.1   reinoud }
   6355       1.1   reinoud 
   6356       1.1   reinoud /* --------------------------------------------------------------------- */
   6357       1.1   reinoud 
   6358      1.45   reinoud 
   6359