Home | History | Annotate | Line # | Download | only in udf
udf_vnops.c revision 1.23
      1 /* $NetBSD: udf_vnops.c,v 1.23 2008/06/24 15:57:13 reinoud Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2006, 2008 Reinoud Zandijk
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  * Generic parts are derived from software contributed to The NetBSD Foundation
     28  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
     29  * 2005 program.
     30  *
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.23 2008/06/24 15:57:13 reinoud Exp $");
     36 #endif /* not lint */
     37 
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/namei.h>
     42 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
     43 #include <sys/kernel.h>
     44 #include <sys/file.h>		/* define FWRITE ... */
     45 #include <sys/stat.h>
     46 #include <sys/buf.h>
     47 #include <sys/proc.h>
     48 #include <sys/mount.h>
     49 #include <sys/vnode.h>
     50 #include <sys/signalvar.h>
     51 #include <sys/malloc.h>
     52 #include <sys/dirent.h>
     53 #include <sys/lockf.h>
     54 #include <sys/kauth.h>
     55 
     56 #include <miscfs/genfs/genfs.h>
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <fs/udf/ecma167-udf.h>
     60 #include <fs/udf/udf_mount.h>
     61 #include "udf.h"
     62 #include "udf_subr.h"
     63 #include "udf_bswap.h"
     64 
     65 
     66 #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
     67 
     68 
     69 /* externs */
     70 extern int prtactive;
     71 
     72 /* implementations of vnode functions; table follows at end */
     73 /* --------------------------------------------------------------------- */
     74 
     75 int
     76 udf_inactive(void *v)
     77 {
     78 	struct vop_inactive_args /* {
     79 		struct vnode *a_vp;
     80 		bool         *a_recycle;
     81 	} */ *ap = v;
     82 	struct vnode *vp = ap->a_vp;
     83 	struct udf_node *udf_node = VTOI(vp);
     84 	int refcnt;
     85 
     86 	DPRINTF(NODE, ("udf_inactive called for udf_node %p\n", VTOI(vp)));
     87 
     88 	if (udf_node == NULL) {
     89 		DPRINTF(NODE, ("udf_inactive: inactive NULL UDF node\n"));
     90 		VOP_UNLOCK(vp, 0);
     91 		return 0;
     92 	}
     93 
     94 	/*
     95 	 * Optionally flush metadata to disc. If the file has not been
     96 	 * referenced anymore in a directory we ought to free up the resources
     97 	 * on disc if applicable.
     98 	 */
     99 	if (udf_node->fe) {
    100 		refcnt = udf_rw16(udf_node->fe->link_cnt);
    101 	} else {
    102 		assert(udf_node->efe);
    103 		refcnt = udf_rw16(udf_node->efe->link_cnt);
    104 	}
    105 
    106 	if ((refcnt == 0) && (vp->v_vflag & VV_SYSTEM))
    107 		DPRINTF(VOLUMES, ("UDF_INACTIVE deleting VV_SYSTEM\n"));
    108 
    109 	*ap->a_recycle = false;
    110 	if ((refcnt == 0) && ((vp->v_vflag & VV_SYSTEM) == 0)) {
    111 	 	/* remove this file's allocation */
    112 		DPRINTF(NODE, ("udf_inactive deleting unlinked file\n"));
    113 		*ap->a_recycle = true;
    114 		udf_delete_node(udf_node);
    115 		VOP_UNLOCK(vp, 0);
    116 		vrecycle(vp, NULL, curlwp);
    117 		return 0;
    118 	}
    119 
    120 	/* write out its node */
    121 	if (udf_node->i_flags & (IN_CHANGE | IN_UPDATE | IN_MODIFIED))
    122 		udf_update(vp, NULL, NULL, NULL, 0);
    123 	VOP_UNLOCK(vp, 0);
    124 
    125 	return 0;
    126 }
    127 
    128 /* --------------------------------------------------------------------- */
    129 
    130 int udf_sync(struct mount *mp, int waitfor, kauth_cred_t cred, struct lwp *lwp);
    131 
    132 int
    133 udf_reclaim(void *v)
    134 {
    135 	struct vop_reclaim_args /* {
    136 		struct vnode *a_vp;
    137 	} */ *ap = v;
    138 	struct vnode *vp = ap->a_vp;
    139 	struct udf_node *udf_node = VTOI(vp);
    140 
    141 	DPRINTF(NODE, ("udf_reclaim called for node %p\n", udf_node));
    142 	if (prtactive && vp->v_usecount > 1)
    143 		vprint("udf_reclaim(): pushing active", vp);
    144 
    145 	if (udf_node == NULL) {
    146 		DPRINTF(NODE, ("udf_reclaim(): null udfnode\n"));
    147 		return 0;
    148 	}
    149 
    150 	/* update note for closure */
    151 	udf_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
    152 
    153 	/* async check to see if all node descriptors are written out */
    154 	while ((volatile int) udf_node->outstanding_nodedscr > 0) {
    155 		vprint("udf_reclaim(): waiting for writeout\n", vp);
    156 		tsleep(&udf_node->outstanding_nodedscr, PRIBIO, "recl wait", hz/8);
    157 	}
    158 
    159 	/* purge old data from namei */
    160 	cache_purge(vp);
    161 
    162 	/* dispose all node knowledge */
    163 	udf_dispose_node(udf_node);
    164 
    165 	return 0;
    166 }
    167 
    168 /* --------------------------------------------------------------------- */
    169 
    170 int
    171 udf_read(void *v)
    172 {
    173 	struct vop_read_args /* {
    174 		struct vnode *a_vp;
    175 		struct uio *a_uio;
    176 		int a_ioflag;
    177 		kauth_cred_t a_cred;
    178 	} */ *ap = v;
    179 	struct vnode *vp     = ap->a_vp;
    180 	struct uio   *uio    = ap->a_uio;
    181 	int           ioflag = ap->a_ioflag;
    182 	int           advice = IO_ADV_DECODE(ap->a_ioflag);
    183 	struct uvm_object    *uobj;
    184 	struct udf_node      *udf_node = VTOI(vp);
    185 	struct file_entry    *fe;
    186 	struct extfile_entry *efe;
    187 	uint64_t file_size;
    188 	vsize_t len;
    189 	void *win;
    190 	int error;
    191 	int flags;
    192 
    193 	/*
    194 	 * XXX reading from extended attributes not yet implemented. FreeBSD
    195 	 * has it in mind to forward the IO_EXT read call to the
    196 	 * VOP_READEXTATTR().
    197 	 */
    198 
    199 	DPRINTF(READ, ("udf_read called\n"));
    200 
    201 	/* can this happen? some filingsystems have this check */
    202 	if (uio->uio_offset < 0)
    203 		return EINVAL;
    204 	if (uio->uio_resid == 0)
    205 		return 0;
    206 
    207 	/* protect against rogue programs reading raw directories and links */
    208 	if ((ioflag & IO_ALTSEMANTICS) == 0) {
    209 		if (vp->v_type == VDIR)
    210 			return EISDIR;
    211 		/* all but regular files just give EINVAL */
    212 		if (vp->v_type != VREG)
    213 			return EINVAL;
    214 	}
    215 
    216 	assert(udf_node);
    217 	assert(udf_node->fe || udf_node->efe);
    218 
    219 	/* get file/directory filesize */
    220 	if (udf_node->fe) {
    221 		fe = udf_node->fe;
    222 		file_size = udf_rw64(fe->inf_len);
    223 	} else {
    224 		assert(udf_node->efe);
    225 		efe = udf_node->efe;
    226 		file_size = udf_rw64(efe->inf_len);
    227 	}
    228 
    229 	/* read contents using buffercache */
    230 	uobj = &vp->v_uobj;
    231 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    232 	error = 0;
    233 	while (uio->uio_resid > 0) {
    234 		/* reached end? */
    235 		if (file_size <= uio->uio_offset)
    236 			break;
    237 
    238 		/* maximise length to file extremity */
    239 		len = MIN(file_size - uio->uio_offset, uio->uio_resid);
    240 		if (len == 0)
    241 			break;
    242 
    243 		/* ubc, here we come, prepare to trap */
    244 		win = ubc_alloc(uobj, uio->uio_offset, &len,
    245 				advice, UBC_READ);
    246 		error = uiomove(win, len, uio);
    247 		ubc_release(win, flags);
    248 		if (error)
    249 			break;
    250 	}
    251 
    252 	/* note access time unless not requested */
    253 	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
    254 		udf_node->i_flags |= IN_ACCESS;
    255 		if ((ioflag & IO_SYNC) == IO_SYNC)
    256 			error = udf_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
    257 	}
    258 
    259 	return error;
    260 }
    261 
    262 /* --------------------------------------------------------------------- */
    263 
    264 int
    265 udf_write(void *v)
    266 {
    267 	struct vop_write_args /* {
    268 		struct vnode *a_vp;
    269 		struct uio *a_uio;
    270 		int a_ioflag;
    271 		kauth_cred_t a_cred;
    272 	} */ *ap = v;
    273 	struct vnode *vp     = ap->a_vp;
    274 	struct uio   *uio    = ap->a_uio;
    275 	int           ioflag = ap->a_ioflag;
    276 	int           advice = IO_ADV_DECODE(ap->a_ioflag);
    277 	struct uvm_object    *uobj;
    278 	struct udf_node      *udf_node = VTOI(vp);
    279 	struct file_entry    *fe;
    280 	struct extfile_entry *efe;
    281 	void *win;
    282 	uint64_t file_size, old_size;
    283 	vsize_t len;
    284 	int error;
    285 	int flags, resid, extended;
    286 
    287 	/*
    288 	 * XXX writing to extended attributes not yet implemented. FreeBSD has
    289 	 * it in mind to forward the IO_EXT read call to the
    290 	 * VOP_READEXTATTR().
    291 	 */
    292 
    293 	DPRINTF(WRITE, ("udf_write called\n"));
    294 
    295 	/* can this happen? some filingsystems have this check */
    296 	if (uio->uio_offset < 0)
    297 		return EINVAL;
    298 	if (uio->uio_resid == 0)
    299 		return 0;
    300 
    301 	/* protect against rogue programs writing raw directories or links */
    302 	if ((ioflag & IO_ALTSEMANTICS) == 0) {
    303 		if (vp->v_type == VDIR)
    304 			return EISDIR;
    305 		/* all but regular files just give EINVAL for now */
    306 		if (vp->v_type != VREG)
    307 			return EINVAL;
    308 	}
    309 
    310 	assert(udf_node);
    311 	assert(udf_node->fe || udf_node->efe);
    312 
    313 	/* get file/directory filesize */
    314 	if (udf_node->fe) {
    315 		fe = udf_node->fe;
    316 		file_size = udf_rw64(fe->inf_len);
    317 	} else {
    318 		assert(udf_node->efe);
    319 		efe = udf_node->efe;
    320 		file_size = udf_rw64(efe->inf_len);
    321 	}
    322 	old_size = file_size;
    323 
    324 	/* if explicitly asked to append, uio_offset can be wrong? */
    325 	if (ioflag & IO_APPEND)
    326 		uio->uio_offset = file_size;
    327 
    328 	extended = (uio->uio_offset + uio->uio_resid > file_size);
    329 	if (extended) {
    330 		DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n",
    331 			file_size, uio->uio_offset + uio->uio_resid));
    332 		error = udf_grow_node(udf_node, uio->uio_offset + uio->uio_resid);
    333 		if (error)
    334 			return error;
    335 		file_size = uio->uio_offset + uio->uio_resid;
    336 	}
    337 
    338 	/* write contents using buffercache */
    339 	uobj = &vp->v_uobj;
    340 	flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
    341 	resid = uio->uio_resid;
    342 	error = 0;
    343 
    344 	uvm_vnp_setwritesize(vp, file_size);
    345 	while (uio->uio_resid > 0) {
    346 		/* maximise length to file extremity */
    347 		len = MIN(file_size - uio->uio_offset, uio->uio_resid);
    348 		if (len == 0)
    349 			break;
    350 
    351 		/* ubc, here we come, prepare to trap */
    352 		win = ubc_alloc(uobj, uio->uio_offset, &len,
    353 				advice, UBC_WRITE);
    354 		error = uiomove(win, len, uio);
    355 		ubc_release(win, flags);
    356 		if (error)
    357 			break;
    358 	}
    359 	uvm_vnp_setsize(vp, file_size);
    360 
    361 	/* mark node changed and request update */
    362 	udf_node->i_flags |= IN_CHANGE | IN_UPDATE;
    363 
    364 	/*
    365 	 * XXX TODO FFS has code here to reset setuid & setgid when we're not
    366 	 * the superuser as a precaution against tampering.
    367 	 */
    368 
    369 	/* if we wrote a thing, note write action on vnode */
    370 	if (resid > uio->uio_resid)
    371 		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
    372 
    373 	if (error) {
    374 		/* bring back file size to its former size */
    375 		/* take notice of its errors? */
    376 		(void) udf_chsize(vp, (u_quad_t) old_size, NOCRED);
    377 
    378 		/* roll back uio */
    379 		uio->uio_offset -= resid - uio->uio_resid;
    380 		uio->uio_resid = resid;
    381 	} else {
    382 		/* if we write and we're synchronous, update node */
    383 		if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC))
    384 			error = udf_update(vp, NULL, NULL, NULL, UPDATE_WAIT);
    385 	}
    386 
    387 	return error;
    388 }
    389 
    390 
    391 /* --------------------------------------------------------------------- */
    392 
    393 /*
    394  * `Special' bmap functionality that translates all incomming requests to
    395  * translate to vop_strategy() calls with the same blocknumbers effectively
    396  * not translating at all.
    397  */
    398 
    399 int
    400 udf_trivial_bmap(void *v)
    401 {
    402 	struct vop_bmap_args /* {
    403 		struct vnode *a_vp;
    404 		daddr_t a_bn;
    405 		struct vnode **a_vpp;
    406 		daddr_t *a_bnp;
    407 		int *a_runp;
    408 	} */ *ap = v;
    409 	struct vnode  *vp  = ap->a_vp;	/* our node	*/
    410 	struct vnode **vpp = ap->a_vpp;	/* return node	*/
    411 	daddr_t *bnp  = ap->a_bnp;	/* translated	*/
    412 	daddr_t  bn   = ap->a_bn;	/* origional	*/
    413 	int     *runp = ap->a_runp;
    414 	struct udf_node *udf_node = VTOI(vp);
    415 	uint32_t lb_size;
    416 
    417 	/* get logical block size */
    418 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    419 
    420 	/* could return `-1' to indicate holes/zeros */
    421 	/* translate 1:1 */
    422 	*bnp = bn;
    423 
    424 	/* set the vnode to read the data from with strategy on itself */
    425 	if (vpp)
    426 		*vpp = vp;
    427 
    428 	/* set runlength of maximum block size */
    429 	if (runp)
    430 		*runp = MAXPHYS / lb_size;	/* or with -1 ? */
    431 
    432 	/* return success */
    433 	return 0;
    434 }
    435 
    436 /* --------------------------------------------------------------------- */
    437 
    438 int
    439 udf_vfsstrategy(void *v)
    440 {
    441 	struct vop_strategy_args /* {
    442 		struct vnode *a_vp;
    443 		struct buf *a_bp;
    444 	} */ *ap = v;
    445 	struct vnode *vp = ap->a_vp;
    446 	struct buf   *bp = ap->a_bp;
    447 	struct udf_node *udf_node = VTOI(vp);
    448 	uint32_t lb_size, from, sectors;
    449 	int error;
    450 
    451 	DPRINTF(STRATEGY, ("udf_strategy called\n"));
    452 
    453 	/* check if we ought to be here */
    454 	if (vp->v_type == VBLK || vp->v_type == VCHR)
    455 		panic("udf_strategy: spec");
    456 
    457 	/* only filebuffers ought to be read/write by this, no descriptors */
    458 	assert(bp->b_blkno >= 0);
    459 
    460 	/* get sector size */
    461 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    462 
    463 	/* calculate sector to start from */
    464 	from = bp->b_blkno;
    465 
    466 	/* calculate length to fetch/store in sectors */
    467 	sectors = bp->b_bcount / lb_size;
    468 	assert(bp->b_bcount > 0);
    469 
    470 	/* NEVER assume later that this buffer is already translated */
    471 	/* bp->b_lblkno = bp->b_blkno; */
    472 
    473 	/* check assertions: we OUGHT to always get multiples of this */
    474 	assert(sectors * lb_size == bp->b_bcount);
    475 
    476 	/* issue buffer */
    477 	error = 0;
    478 	if (bp->b_flags & B_READ) {
    479 		DPRINTF(STRATEGY, ("\tread vp %p buf %p (blk no %"PRIu64")"
    480 		    ", sector %d for %d sectors\n",
    481 		    vp, bp, bp->b_blkno, from, sectors));
    482 
    483 		/* read buffer from the udf_node, translate vtop on the way*/
    484 		udf_read_filebuf(udf_node, bp);
    485 	} else {
    486 		DPRINTF(STRATEGY, ("\twrite vp %p buf %p (blk no %"PRIu64")"
    487 		    ", sector %d for %d sectors\n",
    488 		    vp, bp, bp->b_blkno, from, sectors));
    489 
    490 		/* write buffer to the udf_node, translate vtop on the way*/
    491 		udf_write_filebuf(udf_node, bp);
    492 	}
    493 
    494 	return bp->b_error;
    495 }
    496 
    497 /* --------------------------------------------------------------------- */
    498 
    499 int
    500 udf_readdir(void *v)
    501 {
    502 	struct vop_readdir_args /* {
    503 		struct vnode *a_vp;
    504 		struct uio *a_uio;
    505 		kauth_cred_t a_cred;
    506 		int *a_eofflag;
    507 		off_t **a_cookies;
    508 		int *a_ncookies;
    509 	} */ *ap = v;
    510 	struct uio *uio = ap->a_uio;
    511 	struct vnode *vp = ap->a_vp;
    512 	struct udf_node *udf_node = VTOI(vp);
    513 	struct file_entry    *fe;
    514 	struct extfile_entry *efe;
    515 	struct fileid_desc *fid;
    516 	struct dirent *dirent;
    517 	uint64_t file_size, diroffset, transoffset;
    518 	uint32_t lb_size;
    519 	int error;
    520 
    521 	DPRINTF(READDIR, ("udf_readdir called\n"));
    522 
    523 	/* This operation only makes sense on directory nodes. */
    524 	if (vp->v_type != VDIR)
    525 		return ENOTDIR;
    526 
    527 	/* get directory filesize */
    528 	if (udf_node->fe) {
    529 		fe = udf_node->fe;
    530 		file_size = udf_rw64(fe->inf_len);
    531 	} else {
    532 		assert(udf_node->efe);
    533 		efe = udf_node->efe;
    534 		file_size = udf_rw64(efe->inf_len);
    535 	}
    536 
    537 	dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK | M_ZERO);
    538 
    539 	/*
    540 	 * Add `.' pseudo entry if at offset zero since its not in the fid
    541 	 * stream
    542 	 */
    543 	if (uio->uio_offset == 0) {
    544 		DPRINTF(READDIR, ("\t'.' inserted\n"));
    545 		strcpy(dirent->d_name, ".");
    546 		dirent->d_fileno = udf_calchash(&udf_node->loc);
    547 		dirent->d_type = DT_DIR;
    548 		dirent->d_namlen = strlen(dirent->d_name);
    549 		dirent->d_reclen = _DIRENT_SIZE(dirent);
    550 		uiomove(dirent, _DIRENT_SIZE(dirent), uio);
    551 
    552 		/* mark with magic value that we have done the dummy */
    553 		uio->uio_offset = UDF_DIRCOOKIE_DOT;
    554 	}
    555 
    556 	/* we are called just as long as we keep on pushing data in */
    557 	error = 0;
    558 	if ((uio->uio_offset < file_size) &&
    559 	    (uio->uio_resid >= sizeof(struct dirent))) {
    560 		/* allocate temporary space for fid */
    561 		lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    562 		fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
    563 
    564 		if (uio->uio_offset == UDF_DIRCOOKIE_DOT)
    565 			uio->uio_offset = 0;
    566 
    567 		diroffset   = uio->uio_offset;
    568 		transoffset = diroffset;
    569 		while (diroffset < file_size) {
    570 			DPRINTF(READDIR, ("\tread in fid stream\n"));
    571 			/* transfer a new fid/dirent */
    572 			error = udf_read_fid_stream(vp, &diroffset,
    573 				    fid, dirent);
    574 			DPRINTFIF(READDIR, error, ("read error in read fid "
    575 			    "stream : %d\n", error));
    576 			if (error)
    577 				break;
    578 
    579 			/*
    580 			 * If there isn't enough space in the uio to return a
    581 			 * whole dirent, break off read
    582 			 */
    583 			if (uio->uio_resid < _DIRENT_SIZE(dirent))
    584 				break;
    585 
    586 			/* remember the last entry we transfered */
    587 			transoffset = diroffset;
    588 
    589 			/* skip deleted entries */
    590 			if (fid->file_char & UDF_FILE_CHAR_DEL)
    591 				continue;
    592 
    593 			/* skip not visible files */
    594 			if (fid->file_char & UDF_FILE_CHAR_VIS)
    595 				continue;
    596 
    597 			/* copy dirent to the caller */
    598 			DPRINTF(READDIR, ("\tread dirent `%s', type %d\n",
    599 			    dirent->d_name, dirent->d_type));
    600 			uiomove(dirent, _DIRENT_SIZE(dirent), uio);
    601 		}
    602 
    603 		/* pass on last transfered offset */
    604 		uio->uio_offset = transoffset;
    605 		free(fid, M_UDFTEMP);
    606 	}
    607 
    608 	if (ap->a_eofflag)
    609 		*ap->a_eofflag = (uio->uio_offset == file_size);
    610 
    611 #ifdef DEBUG
    612 	if (udf_verbose & UDF_DEBUG_READDIR) {
    613 		printf("returning offset %d\n", (uint32_t) uio->uio_offset);
    614 		if (ap->a_eofflag)
    615 			printf("returning EOF ? %d\n", *ap->a_eofflag);
    616 		if (error)
    617 			printf("readdir returning error %d\n", error);
    618 	}
    619 #endif
    620 
    621 	free(dirent, M_UDFTEMP);
    622 	return error;
    623 }
    624 
    625 /* --------------------------------------------------------------------- */
    626 
    627 int
    628 udf_lookup(void *v)
    629 {
    630 	struct vop_lookup_args /* {
    631 		struct vnode *a_dvp;
    632 		struct vnode **a_vpp;
    633 		struct componentname *a_cnp;
    634 	} */ *ap = v;
    635 	struct vnode *dvp = ap->a_dvp;
    636 	struct vnode **vpp = ap->a_vpp;
    637 	struct componentname *cnp = ap->a_cnp;
    638 	struct udf_node  *dir_node, *res_node;
    639 	struct udf_mount *ump;
    640 	struct long_ad    icb_loc;
    641 	const char *name;
    642 	int namelen, nameiop, islastcn, mounted_ro;
    643 	int vnodetp;
    644 	int error, found;
    645 
    646 	dir_node = VTOI(dvp);
    647 	ump = dir_node->ump;
    648 	*vpp = NULL;
    649 
    650 	DPRINTF(LOOKUP, ("udf_lookup called\n"));
    651 
    652 	/* simplify/clarification flags */
    653 	nameiop     = cnp->cn_nameiop;
    654 	islastcn    = cnp->cn_flags & ISLASTCN;
    655 	mounted_ro  = dvp->v_mount->mnt_flag & MNT_RDONLY;
    656 
    657 	/* check exec/dirread permissions first */
    658 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    659 	if (error)
    660 		return error;
    661 
    662 	DPRINTF(LOOKUP, ("\taccess ok\n"));
    663 
    664 	/*
    665 	 * If requesting a modify on the last path element on a read-only
    666 	 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
    667 	 */
    668 	if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
    669 		return EROFS;
    670 
    671 	DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
    672 	    cnp->cn_nameptr));
    673 	/* look in the nami cache; returns 0 on success!! */
    674 	error = cache_lookup(dvp, vpp, cnp);
    675 	if (error >= 0)
    676 		return error;
    677 
    678 	DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
    679 
    680 	/*
    681 	 * Obviously, the file is not (anymore) in the namecache, we have to
    682 	 * search for it. There are three basic cases: '.', '..' and others.
    683 	 *
    684 	 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
    685 	 */
    686 	error = 0;
    687 	if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
    688 		DPRINTF(LOOKUP, ("\tlookup '.'\n"));
    689 		/* special case 1 '.' */
    690 		VREF(dvp);
    691 		*vpp = dvp;
    692 		/* done */
    693 	} else if (cnp->cn_flags & ISDOTDOT) {
    694 		/* special case 2 '..' */
    695 		DPRINTF(LOOKUP, ("\tlookup '..'\n"));
    696 
    697 		/* first unlock parent */
    698 		VOP_UNLOCK(dvp, 0);
    699 
    700 		/* get our node */
    701 		name    = "..";
    702 		namelen = 2;
    703 		found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc);
    704 		if (!found)
    705 			error = ENOENT;
    706 
    707 		if (error == 0) {
    708 			DPRINTF(LOOKUP, ("\tfound '..'\n"));
    709 			/* try to create/reuse the node */
    710 			error = udf_get_node(ump, &icb_loc, &res_node);
    711 
    712 			if (!error) {
    713 			DPRINTF(LOOKUP, ("\tnode retrieved/created OK\n"));
    714 				*vpp = res_node->vnode;
    715 			}
    716 		}
    717 
    718 		/* try to relock parent */
    719 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    720 	} else {
    721 		DPRINTF(LOOKUP, ("\tlookup file\n"));
    722 		/* all other files */
    723 		/* lookup filename in the directory; location icb_loc */
    724 		name    = cnp->cn_nameptr;
    725 		namelen = cnp->cn_namelen;
    726 		found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc);
    727 		if (!found) {
    728 			DPRINTF(LOOKUP, ("\tNOT found\n"));
    729 			/*
    730 			 * UGH, didn't find name. If we're creating or
    731 			 * renaming on the last name this is OK and we ought
    732 			 * to return EJUSTRETURN if its allowed to be created.
    733 			 */
    734 			error = ENOENT;
    735 			if (islastcn &&
    736 				(nameiop == CREATE || nameiop == RENAME))
    737 					error = 0;
    738 			if (!error) {
    739 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    740 				if (!error) {
    741 					/* keep the component name */
    742 					cnp->cn_flags |= SAVENAME;
    743 					error = EJUSTRETURN;
    744 				}
    745 			}
    746 			/* done */
    747 		} else {
    748 			/* try to create/reuse the node */
    749 			error = udf_get_node(ump, &icb_loc, &res_node);
    750 			if (!error) {
    751 				/*
    752 				 * If we are not at the last path component
    753 				 * and found a non-directory or non-link entry
    754 				 * (which may itself be pointing to a
    755 				 * directory), raise an error.
    756 				 */
    757 				vnodetp = res_node->vnode->v_type;
    758 				if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
    759 					if (!islastcn)
    760 						error = ENOTDIR;
    761 				}
    762 
    763 			}
    764 			if (!error) {
    765 				*vpp = res_node->vnode;
    766 			}
    767 		}
    768 	}
    769 
    770 	/*
    771 	 * Store result in the cache if requested. If we are creating a file,
    772 	 * the file might not be found and thus putting it into the namecache
    773 	 * might be seen as negative caching.
    774 	 */
    775 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
    776 		cache_enter(dvp, *vpp, cnp);
    777 
    778 	DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error));
    779 
    780 	return error;
    781 }
    782 
    783 /* --------------------------------------------------------------------- */
    784 
    785 int
    786 udf_getattr(void *v)
    787 {
    788 	struct vop_getattr_args /* {
    789 		struct vnode *a_vp;
    790 		struct vattr *a_vap;
    791 		kauth_cred_t a_cred;
    792 		struct lwp   *a_l;
    793 	} */ *ap = v;
    794 	struct vnode *vp = ap->a_vp;
    795 	struct udf_node *udf_node = VTOI(vp);
    796 	struct udf_mount *ump = udf_node->ump;
    797 	struct file_entry    *fe  = udf_node->fe;
    798 	struct extfile_entry *efe = udf_node->efe;
    799 	struct filetimes_extattr_entry *ft_extattr;
    800 	struct device_extattr_entry *devattr;
    801 	struct vattr *vap = ap->a_vap;
    802 	struct timestamp *atime, *mtime, *attrtime, *creatime;
    803 	uint64_t filesize, blkssize;
    804 	uint32_t nlink;
    805 	uint32_t offset, a_l;
    806 	uint8_t *filedata;
    807 	uid_t uid;
    808 	gid_t gid;
    809 	int error;
    810 
    811 	DPRINTF(CALL, ("udf_getattr called\n"));
    812 
    813 	/* update times before we returning values */
    814 	udf_itimes(udf_node, NULL, NULL, NULL);
    815 
    816 	/* get descriptor information */
    817 	if (fe) {
    818 		nlink    = udf_rw16(fe->link_cnt);
    819 		uid      = (uid_t)udf_rw32(fe->uid);
    820 		gid      = (gid_t)udf_rw32(fe->gid);
    821 		filesize = udf_rw64(fe->inf_len);
    822 		blkssize = udf_rw64(fe->logblks_rec);
    823 		atime    = &fe->atime;
    824 		mtime    = &fe->mtime;
    825 		attrtime = &fe->attrtime;
    826 		filedata = fe->data;
    827 
    828 		/* initial guess */
    829 		creatime = mtime;
    830 
    831 		/* check our extended attribute if present */
    832 		error = udf_extattr_search_intern(udf_node,
    833 			UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
    834 		if (!error) {
    835 			ft_extattr = (struct filetimes_extattr_entry *)
    836 				(filedata + offset);
    837 			if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
    838 				creatime = &ft_extattr->times[0];
    839 		}
    840 	} else {
    841 		assert(udf_node->efe);
    842 		nlink    = udf_rw16(efe->link_cnt);
    843 		uid      = (uid_t)udf_rw32(efe->uid);
    844 		gid      = (gid_t)udf_rw32(efe->gid);
    845 		filesize = udf_rw64(efe->inf_len);	/* XXX or obj_size? */
    846 		blkssize = udf_rw64(efe->logblks_rec);
    847 		atime    = &efe->atime;
    848 		mtime    = &efe->mtime;
    849 		attrtime = &efe->attrtime;
    850 		creatime = &efe->ctime;
    851 		filedata = efe->data;
    852 	}
    853 
    854 	/* do the uid/gid translation game */
    855 	if ((uid == (uid_t) -1) && (gid == (gid_t) -1)) {
    856 		uid = ump->mount_args.anon_uid;
    857 		gid = ump->mount_args.anon_gid;
    858 	}
    859 
    860 	/* fill in struct vattr with values from the node */
    861 	VATTR_NULL(vap);
    862 	vap->va_type      = vp->v_type;
    863 	vap->va_mode      = udf_getaccessmode(udf_node);
    864 	vap->va_nlink     = nlink;
    865 	vap->va_uid       = uid;
    866 	vap->va_gid       = gid;
    867 	vap->va_fsid      = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    868 	vap->va_fileid    = udf_calchash(&udf_node->loc);   /* inode hash XXX */
    869 	vap->va_size      = filesize;
    870 	vap->va_blocksize = udf_node->ump->discinfo.sector_size;  /* wise? */
    871 
    872 	/*
    873 	 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add
    874 	 * 1 to the link count if its a directory we're requested attributes
    875 	 * of.
    876 	 */
    877 	if (vap->va_type == VDIR)
    878 		vap->va_nlink++;
    879 
    880 	/* access times */
    881 	udf_timestamp_to_timespec(ump, atime,    &vap->va_atime);
    882 	udf_timestamp_to_timespec(ump, mtime,    &vap->va_mtime);
    883 	udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime);
    884 	udf_timestamp_to_timespec(ump, creatime, &vap->va_birthtime);
    885 
    886 	vap->va_gen       = 1;		/* no multiple generations yes (!?) */
    887 	vap->va_flags     = 0;		/* no flags */
    888 	vap->va_bytes     = blkssize * udf_node->ump->discinfo.sector_size;
    889 	vap->va_filerev   = 1;		/* TODO file revision numbers? */
    890 	vap->va_vaflags   = 0;
    891 	/* TODO get vaflags from the extended attributes? */
    892 
    893 	if ((vap->va_type == VBLK) || (vap->va_type == VCHR)) {
    894 		error = udf_extattr_search_intern(udf_node,
    895 				UDF_DEVICESPEC_ATTR_NO, "",
    896 				&offset, &a_l);
    897 		/* if error, deny access */
    898 		if (error || (filedata == NULL)) {
    899 			vap->va_mode = 0;	/* or v_type = VNON?  */
    900 		} else {
    901 			devattr = (struct device_extattr_entry *)
    902 				filedata + offset;
    903 			vap->va_rdev = makedev(
    904 				udf_rw32(devattr->major),
    905 				udf_rw32(devattr->minor)
    906 				);
    907 			/* TODO we could check the implementator */
    908 		}
    909 	}
    910 
    911 	return 0;
    912 }
    913 
    914 /* --------------------------------------------------------------------- */
    915 
    916 static int
    917 udf_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
    918 	  kauth_cred_t cred)
    919 {
    920 	struct udf_node  *udf_node = VTOI(vp);
    921 	uid_t euid, uid;
    922 	gid_t egid, gid;
    923 	int issuperuser, ismember;
    924 	int error;
    925 
    926 #ifdef notyet
    927 	/* TODO get vaflags from the extended attributes? */
    928 	/* Immutable or append-only files cannot be modified, either. */
    929 	if (udf_node->flags & (IMMUTABLE | APPEND))
    930 		return EPERM;
    931 #endif
    932 
    933 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    934 		return EROFS;
    935 
    936 	/* retrieve old values */
    937 	udf_getownership(udf_node, &uid, &gid);
    938 
    939 	/* only one could be specified */
    940 	if (new_uid == VNOVAL)
    941 		new_uid = uid;
    942 	if (new_gid == VNOVAL)
    943 		new_gid = gid;
    944 
    945 	/* check if we can fit it in an 32 bits */
    946 	if ((uid_t) ((uint32_t) uid) != uid)
    947 		return EINVAL;
    948 	if ((gid_t) ((uint32_t) gid) != gid)
    949 		return EINVAL;
    950 
    951 	/*
    952 	 * If we don't own the file, are trying to change the owner of the
    953 	 * file, or are not a member of the target group, the caller's
    954 	 * credentials must imply super-user privilege or the call fails.
    955 	 */
    956 
    957 	/* check permissions */
    958 	euid  = kauth_cred_geteuid(cred);
    959 	egid  = kauth_cred_getegid(cred);
    960 	if ((error = kauth_cred_ismember_gid(cred, new_gid, &ismember)))
    961 		return error;
    962 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
    963 	issuperuser = (error == 0);
    964 
    965 	if (!issuperuser) {
    966 		if ((new_uid != uid) || (euid != uid))
    967 			return EPERM;
    968 		if ((new_gid != gid) && !(egid == new_gid || ismember))
    969 			return EPERM;
    970 	}
    971 
    972 	/* change the ownership */
    973 	udf_setownership(udf_node, new_uid, new_gid);
    974 
    975 	/* mark node changed */
    976 	udf_node->i_flags |= IN_CHANGE;
    977 
    978 	return 0;
    979 }
    980 
    981 
    982 static int
    983 udf_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
    984 {
    985 	struct udf_node  *udf_node = VTOI(vp);
    986 	uid_t euid, uid;
    987 	gid_t egid, gid;
    988 	int issuperuser, ismember;
    989 	int error;
    990 
    991 #ifdef notyet
    992 	/* TODO get vaflags from the extended attributes? */
    993 	/* Immutable or append-only files cannot be modified, either. */
    994 	if (udf_node->flags & (IMMUTABLE | APPEND))
    995 		return EPERM;
    996 #endif
    997 
    998 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    999 		return EROFS;
   1000 
   1001 	/* retrieve uid/gid values */
   1002 	udf_getownership(udf_node, &uid, &gid);
   1003 
   1004 	/* check permissions */
   1005 	euid  = kauth_cred_geteuid(cred);
   1006 	egid  = kauth_cred_getegid(cred);
   1007 	if ((error = kauth_cred_ismember_gid(cred, gid, &ismember)))
   1008 		return error;
   1009 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
   1010 	issuperuser = (error == 0);
   1011 
   1012 	if ((euid != uid) && !issuperuser)
   1013 		return EPERM;
   1014 	if (euid != 0) {
   1015 		if (vp->v_type != VDIR && (mode & S_ISTXT))
   1016 			return EFTYPE;
   1017 
   1018 		if ((!ismember) && (mode & S_ISGID))
   1019 			return EPERM;
   1020 	}
   1021 
   1022 	/* change mode */
   1023 	udf_setaccessmode(udf_node, mode);
   1024 
   1025 	/* mark node changed */
   1026 	udf_node->i_flags |= IN_CHANGE;
   1027 
   1028 	return 0;
   1029 }
   1030 
   1031 
   1032 /* exported */
   1033 int
   1034 udf_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
   1035 {
   1036 	struct udf_node  *udf_node = VTOI(vp);
   1037 	int error, extended;
   1038 
   1039 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1040 		return EROFS;
   1041 
   1042 	/* Decide whether this is a valid operation based on the file type. */
   1043 	switch (vp->v_type) {
   1044 	case VDIR:
   1045 		return EISDIR;
   1046 	case VREG:
   1047 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1048 			return EROFS;
   1049 		break;
   1050 	case VBLK:
   1051 		/* FALLTHROUGH */
   1052 	case VCHR:
   1053 		/* FALLTHROUGH */
   1054 	case VFIFO:
   1055 		/* Allow modifications of special files even if in the file
   1056 		 * system is mounted read-only (we are not modifying the
   1057 		 * files themselves, but the objects they represent). */
   1058 		return 0;
   1059 	default:
   1060 		/* Anything else is unsupported. */
   1061 		return EOPNOTSUPP;
   1062 	}
   1063 
   1064 #if notyet
   1065 	/* TODO get vaflags from the extended attributes? */
   1066 	/* Immutable or append-only files cannot be modified, either. */
   1067 	if (node->flags & (IMMUTABLE | APPEND))
   1068 		return EPERM;
   1069 #endif
   1070 
   1071 	/* resize file to the requested size */
   1072 	error = udf_resize_node(udf_node, newsize, &extended);
   1073 
   1074 	if (error == 0) {
   1075 		/* mark change */
   1076 		udf_node->i_flags |= IN_CHANGE | IN_MODIFY;
   1077 		VN_KNOTE(vp, NOTE_ATTRIB | (extended ? NOTE_EXTEND : 0));
   1078 		udf_update(vp, NULL, NULL, NULL, 0);
   1079 	}
   1080 
   1081 	return error;
   1082 }
   1083 
   1084 
   1085 static int
   1086 udf_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
   1087 {
   1088 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1089 		return EROFS;
   1090 
   1091 	/* XXX we can't do this yet XXX */
   1092 
   1093 	return EINVAL;
   1094 }
   1095 
   1096 
   1097 static int
   1098 udf_chtimes(struct vnode *vp,
   1099 	struct timespec *atime, struct timespec *mtime,
   1100 	struct timespec *birthtime, int setattrflags,
   1101 	kauth_cred_t cred)
   1102 {
   1103 	struct udf_node  *udf_node = VTOI(vp);
   1104 	uid_t euid, uid;
   1105 	gid_t gid;
   1106 	int issuperuser;
   1107 	int error;
   1108 
   1109 #ifdef notyet
   1110 	/* TODO get vaflags from the extended attributes? */
   1111 	/* Immutable or append-only files cannot be modified, either. */
   1112 	if (udf_node->flags & (IMMUTABLE | APPEND))
   1113 		return EPERM;
   1114 #endif
   1115 
   1116 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1117 		return EROFS;
   1118 
   1119 	/* retrieve uid/gid values */
   1120 	udf_getownership(udf_node, &uid, &gid);
   1121 
   1122 	/* check permissions */
   1123 	euid  = kauth_cred_geteuid(cred);
   1124 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
   1125 	issuperuser = (error == 0);
   1126 
   1127 	if (!issuperuser) {
   1128 		if (euid != uid)
   1129 			return EPERM;
   1130 		if ((setattrflags & VA_UTIMES_NULL) == 0)
   1131 			return error;
   1132 		error = VOP_ACCESS(vp, VWRITE, cred);
   1133 		if (error)
   1134 			return error;
   1135 	}
   1136 
   1137 	/* update node flags depending on what times are passed */
   1138 	if (atime->tv_sec != VNOVAL)
   1139 		if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
   1140 			udf_node->i_flags |= IN_ACCESS;
   1141 	if ((mtime->tv_sec != VNOVAL) || (birthtime->tv_sec != VNOVAL))
   1142 		udf_node->i_flags |= IN_CHANGE | IN_UPDATE;
   1143 
   1144 	return udf_update(vp, atime, mtime, birthtime, 0);
   1145 }
   1146 
   1147 
   1148 int
   1149 udf_setattr(void *v)
   1150 {
   1151 	struct vop_setattr_args /* {
   1152 		struct vnode *a_vp;
   1153 		struct vattr *a_vap;
   1154 		kauth_cred_t a_cred;
   1155 		struct lwp   *a_l;
   1156 	} */ *ap = v;
   1157 	struct vnode *vp = ap->a_vp;
   1158 /*	struct udf_node  *udf_node = VTOI(vp); */
   1159 /*	struct udf_mount *ump = udf_node->ump; */
   1160 	kauth_cred_t cred = ap->a_cred;
   1161 	struct vattr *vap = ap->a_vap;
   1162 	int error;
   1163 
   1164 	DPRINTF(CALL, ("udf_setattr called\n"));
   1165 
   1166 	/* Abort if any unsettable attribute is given. */
   1167 	error = 0;
   1168 	if (vap->va_type != VNON ||
   1169 	    vap->va_nlink != VNOVAL ||
   1170 	    vap->va_fsid != VNOVAL ||
   1171 	    vap->va_fileid != VNOVAL ||
   1172 	    vap->va_blocksize != VNOVAL ||
   1173 #ifdef notyet
   1174 	    /* checks are debated */
   1175 	    vap->va_ctime.tv_sec != VNOVAL ||
   1176 	    vap->va_ctime.tv_nsec != VNOVAL ||
   1177 	    vap->va_birthtime.tv_sec != VNOVAL ||
   1178 	    vap->va_birthtime.tv_nsec != VNOVAL ||
   1179 #endif
   1180 	    vap->va_gen != VNOVAL ||
   1181 	    vap->va_rdev != VNOVAL ||
   1182 	    vap->va_bytes != VNOVAL)
   1183 		error = EINVAL;
   1184 
   1185 	DPRINTF(ATTR, ("setattr changing:\n"));
   1186 	if (error == 0 && (vap->va_flags != VNOVAL)) {
   1187 		DPRINTF(ATTR, ("\tchflags\n"));
   1188 	 	error = udf_chflags(vp, vap->va_flags, cred);
   1189 	}
   1190 
   1191 	if (error == 0 && (vap->va_size != VNOVAL)) {
   1192 		DPRINTF(ATTR, ("\tchsize\n"));
   1193 		error = udf_chsize(vp, vap->va_size, cred);
   1194 	}
   1195 
   1196 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) {
   1197 		DPRINTF(ATTR, ("\tchown\n"));
   1198 		error = udf_chown(vp, vap->va_uid, vap->va_gid, cred);
   1199 	}
   1200 
   1201 	if (error == 0 && (vap->va_mode != VNOVAL)) {
   1202 		DPRINTF(ATTR, ("\tchmod\n"));
   1203 		error = udf_chmod(vp, vap->va_mode, cred);
   1204 	}
   1205 
   1206 	if (error == 0 &&
   1207 	    ((vap->va_atime.tv_sec != VNOVAL &&
   1208 	      vap->va_atime.tv_nsec != VNOVAL)   ||
   1209 	     (vap->va_mtime.tv_sec != VNOVAL &&
   1210 	      vap->va_mtime.tv_nsec != VNOVAL))
   1211 	    ) {
   1212 		DPRINTF(ATTR, ("\tchtimes\n"));
   1213 		error = udf_chtimes(vp, &vap->va_atime, &vap->va_mtime,
   1214 		    &vap->va_birthtime, vap->va_vaflags, cred);
   1215 	}
   1216 	VN_KNOTE(vp, NOTE_ATTRIB);
   1217 
   1218 	return error;
   1219 }
   1220 
   1221 /* --------------------------------------------------------------------- */
   1222 
   1223 /*
   1224  * Return POSIX pathconf information for UDF file systems.
   1225  */
   1226 int
   1227 udf_pathconf(void *v)
   1228 {
   1229 	struct vop_pathconf_args /* {
   1230 		struct vnode *a_vp;
   1231 		int a_name;
   1232 		register_t *a_retval;
   1233 	} */ *ap = v;
   1234 	uint32_t bits;
   1235 
   1236 	DPRINTF(CALL, ("udf_pathconf called\n"));
   1237 
   1238 	switch (ap->a_name) {
   1239 	case _PC_LINK_MAX:
   1240 		*ap->a_retval = (1<<16)-1;	/* 16 bits */
   1241 		return 0;
   1242 	case _PC_NAME_MAX:
   1243 		*ap->a_retval = NAME_MAX;
   1244 		return 0;
   1245 	case _PC_PATH_MAX:
   1246 		*ap->a_retval = PATH_MAX;
   1247 		return 0;
   1248 	case _PC_PIPE_BUF:
   1249 		*ap->a_retval = PIPE_BUF;
   1250 		return 0;
   1251 	case _PC_CHOWN_RESTRICTED:
   1252 		*ap->a_retval = 1;
   1253 		return 0;
   1254 	case _PC_NO_TRUNC:
   1255 		*ap->a_retval = 1;
   1256 		return 0;
   1257 	case _PC_SYNC_IO:
   1258 		*ap->a_retval = 0;     /* synchronised is off for performance */
   1259 		return 0;
   1260 	case _PC_FILESIZEBITS:
   1261 		/* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
   1262 		bits = 64; /* XXX ought to deliver 65 */
   1263 #if 0
   1264 		if (udf_node)
   1265 			bits = 64 * vp->v_mount->mnt_dev_bshift;
   1266 #endif
   1267 		*ap->a_retval = bits;
   1268 		return 0;
   1269 	}
   1270 
   1271 	return EINVAL;
   1272 }
   1273 
   1274 
   1275 /* --------------------------------------------------------------------- */
   1276 
   1277 int
   1278 udf_open(void *v)
   1279 {
   1280 	struct vop_open_args /* {
   1281 		struct vnode *a_vp;
   1282 		int a_mode;
   1283 		kauth_cred_t a_cred;
   1284 		struct proc *a_p;
   1285 	} */ *ap = v;
   1286 	int flags;
   1287 
   1288 	DPRINTF(CALL, ("udf_open called\n"));
   1289 
   1290 	/*
   1291 	 * Files marked append-only must be opened for appending.
   1292 	 * TODO: get chflags(2) flags from extened attribute.
   1293 	 */
   1294 	flags = 0;
   1295 	if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
   1296 		return (EPERM);
   1297 
   1298 	return 0;
   1299 }
   1300 
   1301 
   1302 /* --------------------------------------------------------------------- */
   1303 
   1304 int
   1305 udf_close(void *v)
   1306 {
   1307 	struct vop_close_args /* {
   1308 		struct vnode *a_vp;
   1309 		int a_fflag;
   1310 		kauth_cred_t a_cred;
   1311 		struct proc *a_p;
   1312 	} */ *ap = v;
   1313 	struct vnode *vp = ap->a_vp;
   1314 	struct udf_node *udf_node = VTOI(vp);
   1315 
   1316 	DPRINTF(CALL, ("udf_close called\n"));
   1317 	udf_node = udf_node;	/* shut up gcc */
   1318 
   1319 	mutex_enter(&vp->v_interlock);
   1320 		if (vp->v_usecount > 1)
   1321 			udf_itimes(udf_node, NULL, NULL, NULL);
   1322 	mutex_exit(&vp->v_interlock);
   1323 
   1324 	return 0;
   1325 }
   1326 
   1327 
   1328 /* --------------------------------------------------------------------- */
   1329 
   1330 int
   1331 udf_access(void *v)
   1332 {
   1333 	struct vop_access_args /* {
   1334 		struct vnode *a_vp;
   1335 		int a_mode;
   1336 		kauth_cred_t a_cred;
   1337 		struct proc *a_p;
   1338 	} */ *ap = v;
   1339 	struct vnode    *vp   = ap->a_vp;
   1340 	mode_t	         mode = ap->a_mode;
   1341 	kauth_cred_t     cred = ap->a_cred;
   1342 	/* struct udf_node *udf_node = VTOI(vp); */
   1343 	struct vattr vap;
   1344 	int flags;
   1345 	int error;
   1346 
   1347 	DPRINTF(CALL, ("udf_access called\n"));
   1348 
   1349 	error = VOP_GETATTR(vp, &vap, NULL);
   1350 	if (error)
   1351 		return error;
   1352 
   1353 	/* check if we are allowed to write */
   1354 	switch (vap.va_type) {
   1355 	case VDIR:
   1356 	case VLNK:
   1357 	case VREG:
   1358 		/*
   1359 		 * normal nodes: check if we're on a read-only mounted
   1360 		 * filingsystem and bomb out if we're trying to write.
   1361 		 */
   1362 		if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
   1363 			return EROFS;
   1364 		break;
   1365 	case VBLK:
   1366 	case VCHR:
   1367 	case VSOCK:
   1368 	case VFIFO:
   1369 		/*
   1370 		 * special nodes: even on read-only mounted filingsystems
   1371 		 * these are allowed to be written to if permissions allow.
   1372 		 */
   1373 		break;
   1374 	default:
   1375 		/* no idea what this is */
   1376 		return EINVAL;
   1377 	}
   1378 
   1379 	/* noone may write immutable files */
   1380 	/* TODO: get chflags(2) flags from extened attribute. */
   1381 	flags = 0;
   1382 	if ((mode & VWRITE) && (flags & IMMUTABLE))
   1383 		return EPERM;
   1384 
   1385 	/* ask the generic vaccess to advice on security */
   1386 	return vaccess(vp->v_type,
   1387 			vap.va_mode, vap.va_uid, vap.va_gid,
   1388 			mode, cred);
   1389 }
   1390 
   1391 /* --------------------------------------------------------------------- */
   1392 
   1393 int
   1394 udf_create(void *v)
   1395 {
   1396 	struct vop_create_args /* {
   1397 		struct vnode *a_dvp;
   1398 		struct vnode **a_vpp;
   1399 		struct componentname *a_cnp;
   1400 		struct vattr *a_vap;
   1401 	} */ *ap = v;
   1402 	struct vnode  *dvp = ap->a_dvp;
   1403 	struct vnode **vpp = ap->a_vpp;
   1404 	struct vattr  *vap  = ap->a_vap;
   1405 	struct componentname *cnp = ap->a_cnp;
   1406 	int error;
   1407 
   1408 	DPRINTF(CALL, ("udf_create called\n"));
   1409 	error = udf_create_node(dvp, vpp, vap, cnp);
   1410 
   1411 	if (error || !(cnp->cn_flags & SAVESTART))
   1412 		PNBUF_PUT(cnp->cn_pnbuf);
   1413 	vput(dvp);
   1414 	return error;
   1415 }
   1416 
   1417 /* --------------------------------------------------------------------- */
   1418 
   1419 int
   1420 udf_mknod(void *v)
   1421 {
   1422 	struct vop_mknod_args /* {
   1423 		struct vnode *a_dvp;
   1424 		struct vnode **a_vpp;
   1425 		struct componentname *a_cnp;
   1426 		struct vattr *a_vap;
   1427 	} */ *ap = v;
   1428 	struct vnode  *dvp = ap->a_dvp;
   1429 	struct vnode **vpp = ap->a_vpp;
   1430 	struct vattr  *vap  = ap->a_vap;
   1431 	struct componentname *cnp = ap->a_cnp;
   1432 	int error;
   1433 
   1434 	DPRINTF(CALL, ("udf_mknod called\n"));
   1435 	error = udf_create_node(dvp, vpp, vap, cnp);
   1436 
   1437 	if (error || !(cnp->cn_flags & SAVESTART))
   1438 		PNBUF_PUT(cnp->cn_pnbuf);
   1439 	vput(dvp);
   1440 	return error;
   1441 }
   1442 
   1443 /* --------------------------------------------------------------------- */
   1444 
   1445 int
   1446 udf_mkdir(void *v)
   1447 {
   1448 	struct vop_mkdir_args /* {
   1449 		struct vnode *a_dvp;
   1450 		struct vnode **a_vpp;
   1451 		struct componentname *a_cnp;
   1452 		struct vattr *a_vap;
   1453 	} */ *ap = v;
   1454 	struct vnode  *dvp = ap->a_dvp;
   1455 	struct vnode **vpp = ap->a_vpp;
   1456 	struct vattr  *vap  = ap->a_vap;
   1457 	struct componentname *cnp = ap->a_cnp;
   1458 	int error;
   1459 
   1460 	DPRINTF(CALL, ("udf_mkdir called\n"));
   1461 	error = udf_create_node(dvp, vpp, vap, cnp);
   1462 
   1463 	if (error || !(cnp->cn_flags & SAVESTART))
   1464 		PNBUF_PUT(cnp->cn_pnbuf);
   1465 	vput(dvp);
   1466 	return error;
   1467 }
   1468 
   1469 /* --------------------------------------------------------------------- */
   1470 
   1471 static int
   1472 udf_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
   1473 {
   1474 	struct udf_node *udf_node, *dir_node;
   1475 	struct vattr vap;
   1476 	int error;
   1477 
   1478 	DPRINTF(CALL, ("udf_link called\n"));
   1479 	error = 0;
   1480 
   1481 	/* some quick checks */
   1482 	if (vp->v_type == VDIR)
   1483 		return EPERM;		/* can't link a directory */
   1484 	if (dvp->v_mount != vp->v_mount)
   1485 		return EXDEV;		/* can't link across devices */
   1486 	if (dvp == vp)
   1487 		return EPERM;		/* can't be the same */
   1488 
   1489 	/* lock node */
   1490 	error = vn_lock(vp, LK_EXCLUSIVE);
   1491 	if (error)
   1492 		return error;
   1493 
   1494 	/* get attributes */
   1495 	dir_node = VTOI(dvp);
   1496 	udf_node = VTOI(vp);
   1497 
   1498 	error = VOP_GETATTR(vp, &vap, FSCRED);
   1499 	if (error)
   1500 		return error;
   1501 
   1502 	/* check link count overflow */
   1503 	if (vap.va_nlink >= (1<<16)-1)	/* uint16_t */
   1504 		return EMLINK;
   1505 
   1506 	return udf_dir_attach(dir_node->ump, dir_node, udf_node, &vap, cnp);
   1507 }
   1508 
   1509 int
   1510 udf_link(void *v)
   1511 {
   1512 	struct vop_link_args /* {
   1513 		struct vnode *a_dvp;
   1514 		struct vnode *a_vp;
   1515 		struct componentname *a_cnp;
   1516 	} */ *ap = v;
   1517 	struct vnode *dvp = ap->a_dvp;
   1518 	struct vnode *vp  = ap->a_vp;
   1519 	struct componentname *cnp = ap->a_cnp;
   1520 	int error;
   1521 
   1522 	error = udf_do_link(dvp, vp, cnp);
   1523 	if (error)
   1524 		VOP_ABORTOP(dvp, cnp);
   1525 
   1526 	if ((vp != dvp) && (VOP_ISLOCKED(vp) == LK_EXCLUSIVE))
   1527 		VOP_UNLOCK(vp, 0);
   1528 
   1529 	VN_KNOTE(vp, NOTE_LINK);
   1530 	VN_KNOTE(dvp, NOTE_WRITE);
   1531 	vput(dvp);
   1532 
   1533 	return error;
   1534 }
   1535 
   1536 /* --------------------------------------------------------------------- */
   1537 
   1538 static int
   1539 udf_do_symlink(struct udf_node *udf_node, char *target)
   1540 {
   1541 	struct pathcomp pathcomp;
   1542 	uint8_t *pathbuf, *pathpos, *compnamepos;
   1543 	char *mntonname;
   1544 	int pathlen, len, compnamelen, mntonnamelen;
   1545 	int error;
   1546 
   1547 	/* process `target' to an UDF structure */
   1548 	pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
   1549 	pathpos = pathbuf;
   1550 	pathlen = 0;
   1551 
   1552 	if (*target == '/') {
   1553 		/* symlink starts from the root */
   1554 		len = UDF_PATH_COMP_SIZE;
   1555 		memset(&pathcomp, 0, len);
   1556 		pathcomp.type = UDF_PATH_COMP_ROOT;
   1557 
   1558 		/* check if its mount-point relative! */
   1559 		mntonname    = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
   1560 		mntonnamelen = strlen(mntonname);
   1561 		if (strlen(target) >= mntonnamelen) {
   1562 			if (strncmp(target, mntonname, mntonnamelen) == 0) {
   1563 				pathcomp.type = UDF_PATH_COMP_MOUNTROOT;
   1564 				target += mntonnamelen;
   1565 			}
   1566 		} else {
   1567 			target++;
   1568 		}
   1569 
   1570 		memcpy(pathpos, &pathcomp, len);
   1571 		pathpos += len;
   1572 		pathlen += len;
   1573 	}
   1574 
   1575 	error = 0;
   1576 	while (*target) {
   1577 		/* ignore multiple '/' */
   1578 		while (*target == '/') {
   1579 			target++;
   1580 		}
   1581 		if (!*target)
   1582 			break;
   1583 
   1584 		/* extract component name */
   1585 		compnamelen = 0;
   1586 		compnamepos = target;
   1587 		while ((*target) && (*target != '/')) {
   1588 			target++;
   1589 			compnamelen++;
   1590 		}
   1591 
   1592 		/* just trunc if too long ?? (security issue) */
   1593 		if (compnamelen >= 127) {
   1594 			error = ENAMETOOLONG;
   1595 			break;
   1596 		}
   1597 
   1598 		/* convert unix name to UDF name */
   1599 		len = sizeof(struct pathcomp);
   1600 		memset(&pathcomp, 0, len);
   1601 		pathcomp.type = UDF_PATH_COMP_NAME;
   1602 		len = UDF_PATH_COMP_SIZE;
   1603 
   1604 		if ((compnamelen == 2) && (strncmp(compnamepos, "..", 2) == 0))
   1605 			pathcomp.type = UDF_PATH_COMP_PARENTDIR;
   1606 		if ((compnamelen == 1) && (*compnamepos == '.'))
   1607 			pathcomp.type = UDF_PATH_COMP_CURDIR;
   1608 
   1609 		if (pathcomp.type == UDF_PATH_COMP_NAME) {
   1610 			unix_to_udf_name(
   1611 				(char *) &pathcomp.ident, &pathcomp.l_ci,
   1612 				compnamepos, compnamelen,
   1613 				&udf_node->ump->logical_vol->desc_charset);
   1614 			len = UDF_PATH_COMP_SIZE + pathcomp.l_ci;
   1615 		}
   1616 
   1617 		if (pathlen + len >= UDF_SYMLINKBUFLEN) {
   1618 			error = ENAMETOOLONG;
   1619 			break;
   1620 		}
   1621 
   1622 		memcpy(pathpos, &pathcomp, len);
   1623 		pathpos += len;
   1624 		pathlen += len;
   1625 	}
   1626 
   1627 	if (error) {
   1628 		/* aparently too big */
   1629 		free(pathbuf, M_UDFTEMP);
   1630 		return error;
   1631 	}
   1632 
   1633 	error = udf_grow_node(udf_node, pathlen);
   1634 	if (error) {
   1635 		/* failed to pregrow node */
   1636 		free(pathbuf, M_UDFTEMP);
   1637 		return error;
   1638 	}
   1639 
   1640 	/* write out structure on the new file */
   1641 	error = vn_rdwr(UIO_WRITE, udf_node->vnode,
   1642 		pathbuf, pathlen, 0,
   1643 		UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
   1644 		FSCRED, NULL, NULL);
   1645 	if (error) {
   1646 		/* failed to write out symlink contents */
   1647 		free(pathbuf, M_UDFTEMP);
   1648 		return error;
   1649 	}
   1650 
   1651 	return 0;
   1652 }
   1653 
   1654 
   1655 int
   1656 udf_symlink(void *v)
   1657 {
   1658 	struct vop_symlink_args /* {
   1659 		struct vnode *a_dvp;
   1660 		struct vnode **a_vpp;
   1661 		struct componentname *a_cnp;
   1662 		struct vattr *a_vap;
   1663 		char *a_target;
   1664 	} */ *ap = v;
   1665 	struct vnode  *dvp = ap->a_dvp;
   1666 	struct vnode **vpp = ap->a_vpp;
   1667 	struct vattr  *vap  = ap->a_vap;
   1668 	struct componentname *cnp = ap->a_cnp;
   1669 	struct udf_node *dir_node;
   1670 	struct udf_node *udf_node;
   1671 	int error;
   1672 
   1673 	DPRINTF(CALL, ("udf_symlink called\n"));
   1674 	DPRINTF(CALL, ("\tlinking to `%s`\n",  ap->a_target));
   1675 	error = udf_create_node(dvp, vpp, vap, cnp);
   1676 	KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
   1677 	if (!error) {
   1678 		dir_node = VTOI(dvp);
   1679 		udf_node = VTOI(*vpp);
   1680 		KASSERT(udf_node);
   1681 		error = udf_do_symlink(udf_node, ap->a_target);
   1682 		if (error) {
   1683 			/* remove node */
   1684 			udf_shrink_node(udf_node, 0);
   1685 			udf_dir_detach(udf_node->ump, dir_node, udf_node, cnp);
   1686 		}
   1687 	}
   1688 	if (error || !(cnp->cn_flags & SAVESTART))
   1689 		PNBUF_PUT(cnp->cn_pnbuf);
   1690 	vput(dvp);
   1691 	return error;
   1692 }
   1693 
   1694 /* --------------------------------------------------------------------- */
   1695 
   1696 int
   1697 udf_readlink(void *v)
   1698 {
   1699 	struct vop_readlink_args /* {
   1700 		struct vnode *a_vp;
   1701 		struct uio *a_uio;
   1702 		kauth_cred_t a_cred;
   1703 	} */ *ap = v;
   1704 	struct vnode *vp = ap->a_vp;
   1705 	struct uio *uio = ap->a_uio;
   1706 	kauth_cred_t cred = ap->a_cred;
   1707 	struct udf_node *udf_node;
   1708 	struct pathcomp pathcomp;
   1709 	struct vattr vattr;
   1710 	uint8_t *pathbuf, *targetbuf, *tmpname;
   1711 	uint8_t *pathpos, *targetpos;
   1712 	char *mntonname;
   1713 	int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
   1714 	int first, error;
   1715 
   1716 	DPRINTF(CALL, ("udf_readlink called\n"));
   1717 
   1718 	udf_node = VTOI(vp);
   1719 	error = VOP_GETATTR(vp, &vattr, cred);
   1720 	if (error)
   1721 		return error;
   1722 
   1723 	/* claim temporary buffers for translation */
   1724 	pathbuf   = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
   1725 	targetbuf = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
   1726 	tmpname   = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
   1727 	memset(pathbuf, 0, UDF_SYMLINKBUFLEN);
   1728 	memset(targetbuf, 0, PATH_MAX);
   1729 
   1730 	/* read contents of file in our temporary buffer */
   1731 	error = vn_rdwr(UIO_READ, udf_node->vnode,
   1732 		pathbuf, vattr.va_size, 0,
   1733 		UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
   1734 		FSCRED, NULL, NULL);
   1735 	if (error) {
   1736 		/* failed to read in symlink contents */
   1737 		free(pathbuf, M_UDFTEMP);
   1738 		free(targetbuf, M_UDFTEMP);
   1739 		free(tmpname, M_UDFTEMP);
   1740 		return error;
   1741 	}
   1742 
   1743 	/* convert to a unix path */
   1744 	pathpos   = pathbuf;
   1745 	pathlen   = 0;
   1746 	targetpos = targetbuf;
   1747 	targetlen = PATH_MAX;
   1748 	mntonname    = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
   1749 	mntonnamelen = strlen(mntonname);
   1750 
   1751 	error = 0;
   1752 	first = 1;
   1753 	while (vattr.va_size - pathlen >= UDF_PATH_COMP_SIZE) {
   1754 		len = UDF_PATH_COMP_SIZE;
   1755 		memcpy(&pathcomp, pathpos, len);
   1756 		l_ci = pathcomp.l_ci;
   1757 		switch (pathcomp.type) {
   1758 		case UDF_PATH_COMP_ROOT :
   1759 			/* XXX should check for l_ci; bugcompatible now */
   1760 			if ((targetlen < 1) || !first) {
   1761 				error = EINVAL;
   1762 				break;
   1763 			}
   1764 			*targetpos++ = '/'; targetlen--;
   1765 			break;
   1766 		case UDF_PATH_COMP_MOUNTROOT :
   1767 			/* XXX what should it be if l_ci > 0 ? [4/48.16.1.2] */
   1768 			if (l_ci || (targetlen < mntonnamelen+1) || !first) {
   1769 				error = EINVAL;
   1770 				break;
   1771 			}
   1772 			memcpy(targetpos, mntonname, mntonnamelen);
   1773 			targetpos += mntonnamelen; targetlen -= mntonnamelen;
   1774 			if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
   1775 				/* more follows, so must be directory */
   1776 				*targetpos++ = '/'; targetlen--;
   1777 			}
   1778 			break;
   1779 		case UDF_PATH_COMP_PARENTDIR :
   1780 			/* XXX should check for l_ci; bugcompatible now */
   1781 			if (targetlen < 3) {
   1782 				error = EINVAL;
   1783 				break;
   1784 			}
   1785 			*targetpos++ = '.'; targetlen--;
   1786 			*targetpos++ = '.'; targetlen--;
   1787 			*targetpos++ = '/'; targetlen--;
   1788 			break;
   1789 		case UDF_PATH_COMP_CURDIR :
   1790 			/* XXX should check for l_ci; bugcompatible now */
   1791 			if (targetlen < 2) {
   1792 				error = EINVAL;
   1793 				break;
   1794 			}
   1795 			*targetpos++ = '.'; targetlen--;
   1796 			*targetpos++ = '/'; targetlen--;
   1797 			break;
   1798 		case UDF_PATH_COMP_NAME :
   1799 			if (l_ci == 0) {
   1800 				error = EINVAL;
   1801 				break;
   1802 			}
   1803 			memset(tmpname, 0, PATH_MAX);
   1804 			memcpy(&pathcomp, pathpos, len + l_ci);
   1805 			udf_to_unix_name(tmpname, MAXPATHLEN,
   1806 				pathcomp.ident, l_ci,
   1807 				&udf_node->ump->logical_vol->desc_charset);
   1808 			namelen = strlen(tmpname);
   1809 			if (targetlen < namelen + 1) {
   1810 				error = EINVAL;
   1811 				break;
   1812 			}
   1813 			memcpy(targetpos, tmpname, namelen);
   1814 			targetpos += namelen; targetlen -= namelen;
   1815 			if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
   1816 				/* more follows, so must be directory */
   1817 				*targetpos++ = '/'; targetlen--;
   1818 			}
   1819 			break;
   1820 		default :
   1821 			error = EINVAL;
   1822 			break;
   1823 		}
   1824 		first = 0;
   1825 		if (error)
   1826 			break;
   1827 		pathpos += UDF_PATH_COMP_SIZE + l_ci;
   1828 		pathlen += UDF_PATH_COMP_SIZE + l_ci;
   1829 
   1830 	}
   1831 	/* all processed? */
   1832 	if (vattr.va_size - pathlen > 0)
   1833 		error = EINVAL;
   1834 
   1835 	/* uiomove() to destination */
   1836 	if (!error)
   1837 		uiomove(targetbuf, PATH_MAX - targetlen, uio);
   1838 
   1839 	free(pathbuf, M_UDFTEMP);
   1840 	free(targetbuf, M_UDFTEMP);
   1841 	free(tmpname, M_UDFTEMP);
   1842 
   1843 	return error;
   1844 }
   1845 
   1846 /* --------------------------------------------------------------------- */
   1847 
   1848 /* note: i tried to follow the logics of the tmpfs rename code */
   1849 int
   1850 udf_rename(void *v)
   1851 {
   1852 	struct vop_rename_args /* {
   1853 		struct vnode *a_fdvp;
   1854 		struct vnode *a_fvp;
   1855 		struct componentname *a_fcnp;
   1856 		struct vnode *a_tdvp;
   1857 		struct vnode *a_tvp;
   1858 		struct componentname *a_tcnp;
   1859 	} */ *ap = v;
   1860 	struct vnode *tvp = ap->a_tvp;
   1861 	struct vnode *tdvp = ap->a_tdvp;
   1862 	struct vnode *fvp = ap->a_fvp;
   1863 	struct vnode *fdvp = ap->a_fdvp;
   1864 	struct componentname *tcnp = ap->a_tcnp;
   1865 	struct componentname *fcnp = ap->a_fcnp;
   1866 	struct udf_node *fnode, *fdnode, *tnode, *tdnode;
   1867 	struct vattr vap;
   1868 	int error;
   1869 
   1870 	DPRINTF(CALL, ("udf_rename called\n"));
   1871 
   1872 	/* disallow cross-device renames */
   1873 	if (fvp->v_mount != tdvp->v_mount ||
   1874 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
   1875 		error = EXDEV;
   1876 		goto out_unlocked;
   1877 	}
   1878 
   1879 	fnode  = VTOI(fvp);
   1880 	fdnode = VTOI(fdvp);
   1881 	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
   1882 	tdnode = VTOI(tdvp);
   1883 
   1884 	/* dont allow directories for now */
   1885 	if (fvp->v_type == VDIR) {
   1886 		error = EINVAL;
   1887 		goto out_unlocked;
   1888 	}
   1889 
   1890 	/* do we need to delete the old entry? */
   1891 	if (tvp)
   1892 		udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
   1893 
   1894 	/* create new directory entry */
   1895 	error = VOP_GETATTR(fvp, &vap, FSCRED);
   1896 	KASSERT(error == 0);
   1897 
   1898 	error = udf_dir_attach(tdnode->ump, tdnode, fnode, &vap, tcnp);
   1899 	if (error)
   1900 		goto out_unlocked;
   1901 
   1902 	/* unlink old directory entry */
   1903 	error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
   1904 	if (error)
   1905 		udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
   1906 
   1907 #if 0
   1908 out:
   1909         if (fdnode != tdnode)
   1910                 VOP_UNLOCK(fdvp, 0);
   1911 #endif
   1912 
   1913 out_unlocked:
   1914 	VOP_ABORTOP(tdvp, tcnp);
   1915 	if (tdvp == tvp)
   1916 		vrele(tdvp);
   1917 	else
   1918 		vput(tdvp);
   1919 	if (tvp)
   1920 		vput(tvp);
   1921 	VOP_ABORTOP(fdvp, fcnp);
   1922 
   1923 	/* release source nodes. */
   1924 	vrele(fdvp);
   1925 	vrele(fvp);
   1926 
   1927 	return error;
   1928 }
   1929 
   1930 /* --------------------------------------------------------------------- */
   1931 
   1932 int
   1933 udf_remove(void *v)
   1934 {
   1935 	struct vop_remove_args /* {
   1936 		struct vnode *a_dvp;
   1937 		struct vnode *a_vp;
   1938 		struct componentname *a_cnp;
   1939 	} */ *ap = v;
   1940 	struct vnode *dvp = ap->a_dvp;
   1941 	struct vnode *vp  = ap->a_vp;
   1942 	struct componentname *cnp = ap->a_cnp;
   1943 	struct udf_node *dir_node = VTOI(dvp);;
   1944 	struct udf_node *udf_node = VTOI(vp);
   1945 	struct udf_mount *ump = dir_node->ump;
   1946 	int error;
   1947 
   1948 	DPRINTF(CALL, ("udf_remove called\n"));
   1949 	if (vp->v_type != VDIR) {
   1950 		error = udf_dir_detach(ump, dir_node, udf_node, cnp);
   1951 		DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1952 	} else {
   1953 		DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
   1954 		error = EPERM;
   1955 	}
   1956 
   1957 	if (error == 0) {
   1958 		VN_KNOTE(vp, NOTE_DELETE);
   1959 		VN_KNOTE(dvp, NOTE_WRITE);
   1960 	}
   1961 
   1962 	if (dvp == vp)
   1963 		vrele(vp);
   1964 	else
   1965 		vput(vp);
   1966 	vput(dvp);
   1967 
   1968 	return error;
   1969 }
   1970 
   1971 /* --------------------------------------------------------------------- */
   1972 
   1973 int
   1974 udf_rmdir(void *v)
   1975 {
   1976 	struct vop_rmdir_args /* {
   1977 		struct vnode *a_dvp;
   1978 		struct vnode *a_vp;
   1979 		struct componentname *a_cnp;
   1980 	} */ *ap = v;
   1981 	struct vnode *vp = ap->a_vp;
   1982 	struct vnode *dvp = ap->a_dvp;
   1983 	struct componentname *cnp = ap->a_cnp;
   1984 	struct udf_node *dir_node = VTOI(dvp);;
   1985 	struct udf_node *udf_node = VTOI(vp);
   1986 	struct udf_mount *ump = dir_node->ump;
   1987 	int refcnt, error;
   1988 
   1989 	DPRINTF(NOTIMPL, ("udf_rmdir called\n"));
   1990 
   1991 	/* don't allow '.' to be deleted */
   1992 	if (dir_node == udf_node) {
   1993 		vrele(dvp);
   1994 		vput(vp);
   1995 		return EINVAL;
   1996 	}
   1997 
   1998 	/* check to see if the directory is empty */
   1999 	error = 0;
   2000 	if (dir_node->fe) {
   2001 		refcnt = udf_rw16(udf_node->fe->link_cnt);
   2002 	} else {
   2003 		refcnt = udf_rw16(udf_node->efe->link_cnt);
   2004 	}
   2005 	if (refcnt > 1) {
   2006 		/* NOT empty */
   2007 		vput(dvp);
   2008 		vput(vp);
   2009 		return ENOTEMPTY;
   2010 	}
   2011 
   2012 	/* detach the node from the directory */
   2013 	error = udf_dir_detach(ump, dir_node, udf_node, cnp);
   2014 	if (error == 0) {
   2015 		cache_purge(vp);
   2016 //		cache_purge(dvp);	/* XXX from msdosfs, why? */
   2017 		VN_KNOTE(vp, NOTE_DELETE);
   2018 	}
   2019 	DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   2020 
   2021 	/* unput the nodes and exit */
   2022 	vput(dvp);
   2023 	vput(vp);
   2024 
   2025 	return error;
   2026 }
   2027 
   2028 /* --------------------------------------------------------------------- */
   2029 
   2030 int
   2031 udf_fsync(void *v)
   2032 {
   2033 	struct vop_fsync_args /* {
   2034 		struct vnode *a_vp;
   2035 		kauth_cred_t a_cred;
   2036 		int a_flags;
   2037 		off_t offlo;
   2038 		off_t offhi;
   2039 		struct proc *a_p;
   2040 	} */ *ap = v;
   2041 	struct vnode *vp = ap->a_vp;
   2042 	struct udf_node *udf_node = VTOI(vp);
   2043 	int error, flags, wait;
   2044 
   2045 	DPRINTF(STRATEGY, ("udf_fsync called : %s, %s\n",
   2046 		(ap->a_flags & FSYNC_WAIT)     ? "wait":"no wait",
   2047 		(ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
   2048 
   2049 	/* flush data and wait for it when requested */
   2050 	wait = (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
   2051 	vflushbuf(vp, wait);
   2052 
   2053 	/* set our times */
   2054 	udf_itimes(udf_node, NULL, NULL, NULL);
   2055 
   2056 	/* if called when mounted readonly, never write back */
   2057 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   2058 		return 0;
   2059 
   2060 	/* if only data is requested, return */
   2061 	if (ap->a_flags & FSYNC_DATAONLY)
   2062 		return 0;
   2063 
   2064 	/* check if the node is dirty 'enough'*/
   2065 	flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
   2066 	if (flags == 0)
   2067 		return 0;
   2068 
   2069 	/* if we don't have to wait, check for IO pending */
   2070 	if (!wait) {
   2071 		if (vp->v_numoutput > 0) {
   2072 			DPRINTF(NODE, ("udf_fsync: rejecting on v_numoutput\n"));
   2073 			return 0;
   2074 		}
   2075 		if (udf_node->outstanding_bufs > 0) {
   2076 			DPRINTF(NODE, ("udf_fsync: rejecting on outstanding_bufs\n"));
   2077 			return 0;
   2078 		}
   2079 		if (udf_node->outstanding_nodedscr > 0) {
   2080 			DPRINTF(NODE, ("udf_fsync: rejecting on outstanding_nodedscr\n"));
   2081 			return 0;
   2082 		}
   2083 	}
   2084 
   2085 	/* wait until vp->v_numoutput reaches zero i.e. is finished */
   2086 	if (wait) {
   2087 		DPRINTF(SYNC, ("udf_fsync, waiting\n"));
   2088 		mutex_enter(&vp->v_interlock);
   2089 		while (vp->v_numoutput) {
   2090 			cv_timedwait(&vp->v_cv, &vp->v_interlock, hz/8);
   2091 		}
   2092 		mutex_exit(&vp->v_interlock);
   2093 		DPRINTF(SYNC, ("udf_fsync: fin wait\n"));
   2094 	}
   2095 
   2096 	/* write out node and wait for it if requested */
   2097 	error = udf_writeout_node(udf_node, wait);
   2098 	if (error)
   2099 		return error;
   2100 
   2101 	/* TODO/XXX if ap->a_flags & FSYNC_CACHE, we ought to do a disc sync */
   2102 
   2103 	return 0;
   2104 }
   2105 
   2106 /* --------------------------------------------------------------------- */
   2107 
   2108 int
   2109 udf_advlock(void *v)
   2110 {
   2111 	struct vop_advlock_args /* {
   2112 		struct vnode *a_vp;
   2113 		void *a_id;
   2114 		int a_op;
   2115 		struct flock *a_fl;
   2116 		int a_flags;
   2117 	} */ *ap = v;
   2118 	struct vnode *vp = ap->a_vp;
   2119 	struct udf_node *udf_node = VTOI(vp);
   2120 	struct file_entry    *fe;
   2121 	struct extfile_entry *efe;
   2122 	uint64_t file_size;
   2123 
   2124 	DPRINTF(LOCKING, ("udf_advlock called\n"));
   2125 
   2126 	/* get directory filesize */
   2127 	if (udf_node->fe) {
   2128 		fe = udf_node->fe;
   2129 		file_size = udf_rw64(fe->inf_len);
   2130 	} else {
   2131 		assert(udf_node->efe);
   2132 		efe = udf_node->efe;
   2133 		file_size = udf_rw64(efe->inf_len);
   2134 	}
   2135 
   2136 	return lf_advlock(ap, &udf_node->lockf, file_size);
   2137 }
   2138 
   2139 /* --------------------------------------------------------------------- */
   2140 
   2141 
   2142 /* Global vfs vnode data structures for udfs */
   2143 int (**udf_vnodeop_p) __P((void *));
   2144 
   2145 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
   2146 	{ &vop_default_desc, vn_default_error },
   2147 	{ &vop_lookup_desc, udf_lookup },	/* lookup */
   2148 	{ &vop_create_desc, udf_create },	/* create */
   2149 	{ &vop_mknod_desc, udf_mknod },		/* mknod */	/* TODO */
   2150 	{ &vop_open_desc, udf_open },		/* open */
   2151 	{ &vop_close_desc, udf_close },		/* close */
   2152 	{ &vop_access_desc, udf_access },	/* access */
   2153 	{ &vop_getattr_desc, udf_getattr },	/* getattr */
   2154 	{ &vop_setattr_desc, udf_setattr },	/* setattr */	/* TODO chflags */
   2155 	{ &vop_read_desc, udf_read },		/* read */
   2156 	{ &vop_write_desc, udf_write },		/* write */	/* WRITE */
   2157 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
   2158 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
   2159 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
   2160 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
   2161 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
   2162 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
   2163 	{ &vop_fsync_desc, udf_fsync },		/* fsync */
   2164 	{ &vop_seek_desc, genfs_seek },		/* seek */
   2165 	{ &vop_remove_desc, udf_remove },	/* remove */
   2166 	{ &vop_link_desc, udf_link },		/* link */	/* TODO */
   2167 	{ &vop_rename_desc, udf_rename },	/* rename */ 	/* TODO */
   2168 	{ &vop_mkdir_desc, udf_mkdir },		/* mkdir */
   2169 	{ &vop_rmdir_desc, udf_rmdir },		/* rmdir */
   2170 	{ &vop_symlink_desc, udf_symlink },	/* symlink */	/* TODO */
   2171 	{ &vop_readdir_desc, udf_readdir },	/* readdir */
   2172 	{ &vop_readlink_desc, udf_readlink },	/* readlink */	/* TEST ME */
   2173 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
   2174 	{ &vop_inactive_desc, udf_inactive },	/* inactive */
   2175 	{ &vop_reclaim_desc, udf_reclaim },	/* reclaim */
   2176 	{ &vop_lock_desc, genfs_lock },		/* lock */
   2177 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
   2178 	{ &vop_bmap_desc, udf_trivial_bmap },	/* bmap */	/* 1:1 bmap */
   2179 	{ &vop_strategy_desc, udf_vfsstrategy },/* strategy */
   2180 /*	{ &vop_print_desc, udf_print },	*/	/* print */
   2181 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
   2182 	{ &vop_pathconf_desc, udf_pathconf },	/* pathconf */
   2183 	{ &vop_advlock_desc, udf_advlock },	/* advlock */	/* TEST ME */
   2184 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
   2185 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
   2186 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
   2187 	{ NULL, NULL }
   2188 };
   2189 
   2190 
   2191 const struct vnodeopv_desc udf_vnodeop_opv_desc = {
   2192 	&udf_vnodeop_p, udf_vnodeop_entries
   2193 };
   2194 
   2195