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