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