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