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