Home | History | Annotate | Line # | Download | only in udf
udf_vnops.c revision 1.93
      1 /* $NetBSD: udf_vnops.c,v 1.93 2014/07/29 11:10:12 reinoud Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2006, 2008 Reinoud Zandijk
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  * Generic parts are derived from software contributed to The NetBSD Foundation
     28  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
     29  * 2005 program.
     30  *
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.93 2014/07/29 11:10:12 reinoud Exp $");
     36 #endif /* not lint */
     37 
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/namei.h>
     42 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
     43 #include <sys/kernel.h>
     44 #include <sys/file.h>		/* define FWRITE ... */
     45 #include <sys/stat.h>
     46 #include <sys/buf.h>
     47 #include <sys/proc.h>
     48 #include <sys/mount.h>
     49 #include <sys/vnode.h>
     50 #include <sys/signalvar.h>
     51 #include <sys/malloc.h>
     52 #include <sys/dirent.h>
     53 #include <sys/lockf.h>
     54 #include <sys/kauth.h>
     55 
     56 #include <miscfs/genfs/genfs.h>
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <fs/udf/ecma167-udf.h>
     60 #include <fs/udf/udf_mount.h>
     61 #include <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, sectors;
    468 
    469 	DPRINTF(STRATEGY, ("udf_strategy called\n"));
    470 
    471 	/* check if we ought to be here */
    472 	if (vp->v_type == VBLK || vp->v_type == VCHR)
    473 		panic("udf_strategy: spec");
    474 
    475 	/* only filebuffers ought to be read/write by this, no descriptors */
    476 	assert(bp->b_blkno >= 0);
    477 
    478 	/* get sector size */
    479 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    480 
    481 	/* calculate length to fetch/store in sectors */
    482 	sectors = bp->b_bcount / lb_size;
    483 	assert(bp->b_bcount > 0);
    484 
    485 	/* NEVER assume later that this buffer is already translated */
    486 	/* bp->b_lblkno = bp->b_blkno; */
    487 
    488 	/* check assertions: we OUGHT to always get multiples of this */
    489 	assert(sectors * lb_size == bp->b_bcount);
    490 	__USE(sectors);
    491 
    492 	/* issue buffer */
    493 	if (bp->b_flags & B_READ) {
    494 		DPRINTF(STRATEGY, ("\tread vp %p buf %p (blk no %"PRIu64")"
    495 		    ", for %d sectors\n",
    496 		    vp, bp, bp->b_blkno, sectors));
    497 
    498 		/* read buffer from the udf_node, translate vtop on the way*/
    499 		udf_read_filebuf(udf_node, bp);
    500 	} else {
    501 		DPRINTF(STRATEGY, ("\twrite vp %p buf %p (blk no %"PRIu64")"
    502 		    ", for %d sectors\n",
    503 		    vp, bp, bp->b_blkno, sectors));
    504 
    505 		/* write buffer to the udf_node, translate vtop on the way*/
    506 		udf_write_filebuf(udf_node, bp);
    507 	}
    508 
    509 	return bp->b_error;
    510 }
    511 
    512 /* --------------------------------------------------------------------- */
    513 
    514 int
    515 udf_readdir(void *v)
    516 {
    517 	struct vop_readdir_args /* {
    518 		struct vnode *a_vp;
    519 		struct uio *a_uio;
    520 		kauth_cred_t a_cred;
    521 		int *a_eofflag;
    522 		off_t **a_cookies;
    523 		int *a_ncookies;
    524 	} */ *ap = v;
    525 	struct uio *uio = ap->a_uio;
    526 	struct vnode *vp = ap->a_vp;
    527 	struct udf_node *udf_node = VTOI(vp);
    528 	struct file_entry    *fe;
    529 	struct extfile_entry *efe;
    530 	struct fileid_desc *fid;
    531 	struct dirent *dirent;
    532 	uint64_t file_size, diroffset, transoffset;
    533 	uint32_t lb_size;
    534 	int error;
    535 
    536 	DPRINTF(READDIR, ("udf_readdir called\n"));
    537 
    538 	/* This operation only makes sense on directory nodes. */
    539 	if (vp->v_type != VDIR)
    540 		return ENOTDIR;
    541 
    542 	/* get directory filesize */
    543 	if (udf_node->fe) {
    544 		fe = udf_node->fe;
    545 		file_size = udf_rw64(fe->inf_len);
    546 	} else {
    547 		assert(udf_node->efe);
    548 		efe = udf_node->efe;
    549 		file_size = udf_rw64(efe->inf_len);
    550 	}
    551 
    552 	dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK | M_ZERO);
    553 
    554 	/*
    555 	 * Add `.' pseudo entry if at offset zero since its not in the fid
    556 	 * stream
    557 	 */
    558 	if (uio->uio_offset == 0) {
    559 		DPRINTF(READDIR, ("\t'.' inserted\n"));
    560 		strcpy(dirent->d_name, ".");
    561 		dirent->d_fileno = udf_get_node_id(&udf_node->loc);
    562 		dirent->d_type = DT_DIR;
    563 		dirent->d_namlen = strlen(dirent->d_name);
    564 		dirent->d_reclen = _DIRENT_SIZE(dirent);
    565 		uiomove(dirent, _DIRENT_SIZE(dirent), uio);
    566 
    567 		/* mark with magic value that we have done the dummy */
    568 		uio->uio_offset = UDF_DIRCOOKIE_DOT;
    569 	}
    570 
    571 	/* we are called just as long as we keep on pushing data in */
    572 	error = 0;
    573 	if (uio->uio_offset < file_size) {
    574 		/* allocate temporary space for fid */
    575 		lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
    576 		fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
    577 
    578 		if (uio->uio_offset == UDF_DIRCOOKIE_DOT)
    579 			uio->uio_offset = 0;
    580 
    581 		diroffset   = uio->uio_offset;
    582 		transoffset = diroffset;
    583 		while (diroffset < file_size) {
    584 			DPRINTF(READDIR, ("\tread in fid stream\n"));
    585 			/* transfer a new fid/dirent */
    586 			error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
    587 			DPRINTFIF(READDIR, error, ("read error in read fid "
    588 			    "stream : %d\n", error));
    589 			if (error)
    590 				break;
    591 
    592 			/*
    593 			 * If there isn't enough space in the uio to return a
    594 			 * whole dirent, break off read
    595 			 */
    596 			if (uio->uio_resid < _DIRENT_SIZE(dirent))
    597 				break;
    598 
    599 			/* remember the last entry we transfered */
    600 			transoffset = diroffset;
    601 
    602 			/* skip deleted entries */
    603 			if (fid->file_char & UDF_FILE_CHAR_DEL)
    604 				continue;
    605 
    606 			/* skip not visible files */
    607 			if (fid->file_char & UDF_FILE_CHAR_VIS)
    608 				continue;
    609 
    610 			/* copy dirent to the caller */
    611 			DPRINTF(READDIR, ("\tread dirent `%s', type %d\n",
    612 			    dirent->d_name, dirent->d_type));
    613 			uiomove(dirent, _DIRENT_SIZE(dirent), uio);
    614 		}
    615 
    616 		/* pass on last transfered offset */
    617 		uio->uio_offset = transoffset;
    618 		free(fid, M_UDFTEMP);
    619 	}
    620 
    621 	if (ap->a_eofflag)
    622 		*ap->a_eofflag = (uio->uio_offset >= file_size);
    623 
    624 #ifdef DEBUG
    625 	if (udf_verbose & UDF_DEBUG_READDIR) {
    626 		printf("returning offset %d\n", (uint32_t) uio->uio_offset);
    627 		if (ap->a_eofflag)
    628 			printf("returning EOF ? %d\n", *ap->a_eofflag);
    629 		if (error)
    630 			printf("readdir returning error %d\n", error);
    631 	}
    632 #endif
    633 
    634 	free(dirent, M_UDFTEMP);
    635 	return error;
    636 }
    637 
    638 /* --------------------------------------------------------------------- */
    639 
    640 int
    641 udf_lookup(void *v)
    642 {
    643 	struct vop_lookup_v2_args /* {
    644 		struct vnode *a_dvp;
    645 		struct vnode **a_vpp;
    646 		struct componentname *a_cnp;
    647 	} */ *ap = v;
    648 	struct vnode *dvp = ap->a_dvp;
    649 	struct vnode **vpp = ap->a_vpp;
    650 	struct componentname *cnp = ap->a_cnp;
    651 	struct udf_node  *dir_node, *res_node;
    652 	struct udf_mount *ump;
    653 	struct long_ad    icb_loc;
    654 	mode_t mode;
    655 	uid_t d_uid;
    656 	gid_t d_gid;
    657 	const char *name;
    658 	int namelen, nameiop, islastcn, mounted_ro;
    659 	int error, found;
    660 
    661 	dir_node = VTOI(dvp);
    662 	ump = dir_node->ump;
    663 	*vpp = NULL;
    664 
    665 	DPRINTF(LOOKUP, ("udf_lookup called, lookup `%s`\n",
    666 		cnp->cn_nameptr));
    667 
    668 	/* simplify/clarification flags */
    669 	nameiop     = cnp->cn_nameiop;
    670 	islastcn    = cnp->cn_flags & ISLASTCN;
    671 	mounted_ro  = dvp->v_mount->mnt_flag & MNT_RDONLY;
    672 
    673 	/* check exec/dirread permissions first */
    674 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
    675 	if (error)
    676 		return error;
    677 
    678 	DPRINTF(LOOKUP, ("\taccess ok\n"));
    679 
    680 	/*
    681 	 * If requesting a modify on the last path element on a read-only
    682 	 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
    683 	 */
    684 	if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
    685 		return EROFS;
    686 
    687 	DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
    688 	    cnp->cn_nameptr));
    689 	/* look in the namecache */
    690 	if (cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
    691 			 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
    692 		return *vpp == NULLVP ? ENOENT : 0;
    693 	}
    694 
    695 	DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
    696 
    697 	/*
    698 	 * Obviously, the file is not (anymore) in the namecache, we have to
    699 	 * search for it. There are three basic cases: '.', '..' and others.
    700 	 *
    701 	 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
    702 	 */
    703 	error = 0;
    704 	if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
    705 		DPRINTF(LOOKUP, ("\tlookup '.'\n"));
    706 		/* special case 1 '.' */
    707 		if (islastcn && cnp->cn_nameiop == RENAME) {
    708 			error = EISDIR;
    709 			goto out;
    710 		}
    711 		vref(dvp);
    712 		*vpp = dvp;
    713 		/* done */
    714 		goto done;
    715 	} else if (cnp->cn_flags & ISDOTDOT) {
    716 		/* special case 2 '..' */
    717 		DPRINTF(LOOKUP, ("\tlookup '..'\n"));
    718 
    719 		if (islastcn && cnp->cn_nameiop == RENAME) {
    720 			error = EINVAL;
    721 			goto out;
    722 		}
    723 
    724 		/* get our node */
    725 		name    = "..";
    726 		namelen = 2;
    727 		error = udf_lookup_name_in_dir(dvp, name, namelen,
    728 				&icb_loc, &found);
    729 		if (error)
    730 			goto out;
    731 		if (!found)
    732 			error = ENOENT;
    733 
    734 		/* first unlock parent */
    735 		VOP_UNLOCK(dvp);
    736 
    737 		if (error == 0) {
    738 			DPRINTF(LOOKUP, ("\tfound '..'\n"));
    739 			/* try to create/reuse the node */
    740 			error = udf_get_node(ump, &icb_loc, &res_node);
    741 
    742 			if (!error) {
    743 				DPRINTF(LOOKUP,
    744 					("\tnode retrieved/created OK\n"));
    745 				*vpp = res_node->vnode;
    746 			}
    747 		}
    748 
    749 		/* try to relock parent */
    750 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    751 		goto out;
    752 	}
    753 
    754 	/* all other files */
    755 	DPRINTF(LOOKUP, ("\tlookup file/dir in directory\n"));
    756 
    757 	/* lookup filename in the directory; location icb_loc */
    758 	name    = cnp->cn_nameptr;
    759 	namelen = cnp->cn_namelen;
    760 	error = udf_lookup_name_in_dir(dvp, name, namelen,
    761 			&icb_loc, &found);
    762 	if (error)
    763 		goto out;
    764 	if (!found) {
    765 		DPRINTF(LOOKUP, ("\tNOT found\n"));
    766 		/*
    767 		 * The entry was not found in the directory.  This is
    768 		 * valid if we are creating or renaming an entry and
    769 		 * are working on the last component of the path name.
    770 		 */
    771 		if (islastcn && (cnp->cn_nameiop == CREATE ||
    772 				 cnp->cn_nameiop == RENAME)) {
    773 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    774 			if (error) {
    775 				goto out;
    776 			}
    777 			error = EJUSTRETURN;
    778 		} else {
    779 			error = ENOENT;
    780 		}
    781 		/* done */
    782 		goto done;
    783 	}
    784 
    785 	/*
    786 	 * XXX NOTE tmpfs has a test here that tests that intermediate
    787 	 * components i.e. not the last one ought to be either a directory or
    788 	 * a link. It seems to function well without this code.
    789 	 */
    790 
    791 	/* try to create/reuse the node */
    792 	error = udf_get_node(ump, &icb_loc, &res_node);
    793 	if (error)
    794 		goto out;
    795 
    796 	/* check permissions */
    797 	if (islastcn && (cnp->cn_nameiop == DELETE ||
    798 			 cnp->cn_nameiop == RENAME)  ) {
    799 		error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
    800 		if (error) {
    801 			vput(res_node->vnode);
    802 			goto out;
    803 		}
    804 
    805 		/*
    806 		 * Check if the directory has its sticky bit set. If so, ask
    807 		 * for clearance since only the owner of a file or directory
    808 		 * can remove/rename from taht directory.
    809 		 */
    810 		mode = udf_getaccessmode(dir_node);
    811 		if ((mode & S_ISTXT) != 0) {
    812 			udf_getownership(dir_node, &d_uid, &d_gid);
    813 			error = kauth_authorize_vnode(cnp->cn_cred,
    814 			    KAUTH_VNODE_DELETE, res_node->vnode,
    815 			    dir_node->vnode, genfs_can_sticky(cnp->cn_cred,
    816 			    d_uid, d_uid));
    817 			if (error) {
    818 				error = EPERM;
    819 				vput(res_node->vnode);
    820 				goto out;
    821 			}
    822 		}
    823 	}
    824 
    825 	*vpp = res_node->vnode;
    826 
    827 done:
    828 	/*
    829 	 * Store result in the cache if requested. If we are creating a file,
    830 	 * the file might not be found and thus putting it into the namecache
    831 	 * might be seen as negative caching.
    832 	 */
    833 	if (nameiop != CREATE)
    834 		cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
    835 			    cnp->cn_flags);
    836 
    837 out:
    838 	if (error == 0 && *vpp != dvp)
    839 		VOP_UNLOCK(*vpp);
    840 	DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error));
    841 
    842 	return error;
    843 }
    844 
    845 /* --------------------------------------------------------------------- */
    846 
    847 int
    848 udf_getattr(void *v)
    849 {
    850 	struct vop_getattr_args /* {
    851 		struct vnode *a_vp;
    852 		struct vattr *a_vap;
    853 		kauth_cred_t a_cred;
    854 		struct lwp   *a_l;
    855 	} */ *ap = v;
    856 	struct vnode *vp = ap->a_vp;
    857 	struct udf_node *udf_node = VTOI(vp);
    858 	struct udf_mount *ump = udf_node->ump;
    859 	struct file_entry    *fe  = udf_node->fe;
    860 	struct extfile_entry *efe = udf_node->efe;
    861 	struct filetimes_extattr_entry *ft_extattr;
    862 	struct device_extattr_entry *devattr;
    863 	struct vattr *vap = ap->a_vap;
    864 	struct timestamp *atime, *mtime, *attrtime, *creatime;
    865 	uint64_t filesize, blkssize;
    866 	uint32_t nlink;
    867 	uint32_t offset, a_l;
    868 	uint8_t *filedata;
    869 	uid_t uid;
    870 	gid_t gid;
    871 	int error;
    872 
    873 	DPRINTF(CALL, ("udf_getattr called\n"));
    874 
    875 	/* update times before we returning values */
    876 	udf_itimes(udf_node, NULL, NULL, NULL);
    877 
    878 	/* get descriptor information */
    879 	if (fe) {
    880 		nlink    = udf_rw16(fe->link_cnt);
    881 		uid      = (uid_t)udf_rw32(fe->uid);
    882 		gid      = (gid_t)udf_rw32(fe->gid);
    883 		filesize = udf_rw64(fe->inf_len);
    884 		blkssize = udf_rw64(fe->logblks_rec);
    885 		atime    = &fe->atime;
    886 		mtime    = &fe->mtime;
    887 		attrtime = &fe->attrtime;
    888 		filedata = fe->data;
    889 
    890 		/* initial guess */
    891 		creatime = mtime;
    892 
    893 		/* check our extended attribute if present */
    894 		error = udf_extattr_search_intern(udf_node,
    895 			UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
    896 		if (!error) {
    897 			ft_extattr = (struct filetimes_extattr_entry *)
    898 				(filedata + offset);
    899 			if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
    900 				creatime = &ft_extattr->times[0];
    901 		}
    902 	} else {
    903 		assert(udf_node->efe);
    904 		nlink    = udf_rw16(efe->link_cnt);
    905 		uid      = (uid_t)udf_rw32(efe->uid);
    906 		gid      = (gid_t)udf_rw32(efe->gid);
    907 		filesize = udf_rw64(efe->inf_len);	/* XXX or obj_size? */
    908 		blkssize = udf_rw64(efe->logblks_rec);
    909 		atime    = &efe->atime;
    910 		mtime    = &efe->mtime;
    911 		attrtime = &efe->attrtime;
    912 		creatime = &efe->ctime;
    913 		filedata = efe->data;
    914 	}
    915 
    916 	/* do the uid/gid translation game */
    917 	if (uid == (uid_t) -1)
    918 		uid = ump->mount_args.anon_uid;
    919 	if (gid == (gid_t) -1)
    920 		gid = ump->mount_args.anon_gid;
    921 
    922 	/* fill in struct vattr with values from the node */
    923 	vattr_null(vap);
    924 	vap->va_type      = vp->v_type;
    925 	vap->va_mode      = udf_getaccessmode(udf_node);
    926 	vap->va_nlink     = nlink;
    927 	vap->va_uid       = uid;
    928 	vap->va_gid       = gid;
    929 	vap->va_fsid      = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
    930 	vap->va_fileid    = udf_get_node_id(&udf_node->loc);   /* inode hash XXX */
    931 	vap->va_size      = filesize;
    932 	vap->va_blocksize = udf_node->ump->discinfo.sector_size;  /* wise? */
    933 
    934 	/*
    935 	 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add
    936 	 * 1 to the link count if its a directory we're requested attributes
    937 	 * of.
    938 	 */
    939 	if (vap->va_type == VDIR)
    940 		vap->va_nlink++;
    941 
    942 	/* access times */
    943 	udf_timestamp_to_timespec(ump, atime,    &vap->va_atime);
    944 	udf_timestamp_to_timespec(ump, mtime,    &vap->va_mtime);
    945 	udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime);
    946 	udf_timestamp_to_timespec(ump, creatime, &vap->va_birthtime);
    947 
    948 	vap->va_gen       = 1;		/* no multiple generations yes (!?) */
    949 	vap->va_flags     = 0;		/* no flags */
    950 	vap->va_bytes     = blkssize * udf_node->ump->discinfo.sector_size;
    951 	vap->va_filerev   = 1;		/* TODO file revision numbers? */
    952 	vap->va_vaflags   = 0;
    953 	/* TODO get vaflags from the extended attributes? */
    954 
    955 	if ((vap->va_type == VBLK) || (vap->va_type == VCHR)) {
    956 		error = udf_extattr_search_intern(udf_node,
    957 				UDF_DEVICESPEC_ATTR_NO, "",
    958 				&offset, &a_l);
    959 		/* if error, deny access */
    960 		if (error || (filedata == NULL)) {
    961 			vap->va_mode = 0;	/* or v_type = VNON?  */
    962 		} else {
    963 			devattr = (struct device_extattr_entry *)
    964 				filedata + offset;
    965 			vap->va_rdev = makedev(
    966 				udf_rw32(devattr->major),
    967 				udf_rw32(devattr->minor)
    968 				);
    969 			/* TODO we could check the implementator */
    970 		}
    971 	}
    972 
    973 	return 0;
    974 }
    975 
    976 /* --------------------------------------------------------------------- */
    977 
    978 static int
    979 udf_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
    980 	  kauth_cred_t cred)
    981 {
    982 	struct udf_node  *udf_node = VTOI(vp);
    983 	uid_t uid;
    984 	gid_t gid;
    985 	int error;
    986 
    987 #ifdef notyet
    988 	/* TODO get vaflags from the extended attributes? */
    989 	/* Immutable or append-only files cannot be modified, either. */
    990 	if (udf_node->flags & (IMMUTABLE | APPEND))
    991 		return EPERM;
    992 #endif
    993 
    994 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
    995 		return EROFS;
    996 
    997 	/* retrieve old values */
    998 	udf_getownership(udf_node, &uid, &gid);
    999 
   1000 	/* only one could be specified */
   1001 	if (new_uid == VNOVAL)
   1002 		new_uid = uid;
   1003 	if (new_gid == VNOVAL)
   1004 		new_gid = gid;
   1005 
   1006 	/* check if we can fit it in an 32 bits */
   1007 	if ((uid_t) ((uint32_t) new_uid) != new_uid)
   1008 		return EINVAL;
   1009 	if ((gid_t) ((uint32_t) new_gid) != new_gid)
   1010 		return EINVAL;
   1011 
   1012 	/* check permissions */
   1013 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP,
   1014 	    vp, NULL, genfs_can_chown(cred, uid, gid, new_uid, new_gid));
   1015 	if (error)
   1016 		return (error);
   1017 
   1018 	/* change the ownership */
   1019 	udf_setownership(udf_node, new_uid, new_gid);
   1020 
   1021 	/* mark node changed */
   1022 	udf_node->i_flags |= IN_CHANGE;
   1023 
   1024 	return 0;
   1025 }
   1026 
   1027 
   1028 static int
   1029 udf_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
   1030 {
   1031 	struct udf_node  *udf_node = VTOI(vp);
   1032 	uid_t uid;
   1033 	gid_t gid;
   1034 	int error;
   1035 
   1036 #ifdef notyet
   1037 	/* TODO get vaflags from the extended attributes? */
   1038 	/* Immutable or append-only files cannot be modified, either. */
   1039 	if (udf_node->flags & (IMMUTABLE | APPEND))
   1040 		return EPERM;
   1041 #endif
   1042 
   1043 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1044 		return EROFS;
   1045 
   1046 	/* retrieve uid/gid values */
   1047 	udf_getownership(udf_node, &uid, &gid);
   1048 
   1049 	/* check permissions */
   1050 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
   1051 	    NULL, genfs_can_chmod(vp->v_type, cred, uid, gid, mode));
   1052 	if (error)
   1053 		return (error);
   1054 
   1055 	/* change mode */
   1056 	udf_setaccessmode(udf_node, mode);
   1057 
   1058 	/* mark node changed */
   1059 	udf_node->i_flags |= IN_CHANGE;
   1060 
   1061 	return 0;
   1062 }
   1063 
   1064 
   1065 /* exported */
   1066 int
   1067 udf_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
   1068 {
   1069 	struct udf_node  *udf_node = VTOI(vp);
   1070 	int error, extended;
   1071 
   1072 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1073 		return EROFS;
   1074 
   1075 	/* Decide whether this is a valid operation based on the file type. */
   1076 	switch (vp->v_type) {
   1077 	case VDIR:
   1078 		return EISDIR;
   1079 	case VREG:
   1080 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1081 			return EROFS;
   1082 		break;
   1083 	case VBLK:
   1084 		/* FALLTHROUGH */
   1085 	case VCHR:
   1086 		/* FALLTHROUGH */
   1087 	case VFIFO:
   1088 		/* Allow modifications of special files even if in the file
   1089 		 * system is mounted read-only (we are not modifying the
   1090 		 * files themselves, but the objects they represent). */
   1091 		return 0;
   1092 	default:
   1093 		/* Anything else is unsupported. */
   1094 		return EOPNOTSUPP;
   1095 	}
   1096 
   1097 #if notyet
   1098 	/* TODO get vaflags from the extended attributes? */
   1099 	/* Immutable or append-only files cannot be modified, either. */
   1100 	if (node->flags & (IMMUTABLE | APPEND))
   1101 		return EPERM;
   1102 #endif
   1103 
   1104 	/* resize file to the requested size */
   1105 	error = udf_resize_node(udf_node, newsize, &extended);
   1106 
   1107 	if (error == 0) {
   1108 		/* mark change */
   1109 		udf_node->i_flags |= IN_CHANGE | IN_MODIFY;
   1110 		if (vp->v_mount->mnt_flag & MNT_RELATIME)
   1111 			udf_node->i_flags |= IN_ACCESS;
   1112 		VN_KNOTE(vp, NOTE_ATTRIB | (extended ? NOTE_EXTEND : 0));
   1113 		udf_update(vp, NULL, NULL, NULL, 0);
   1114 	}
   1115 
   1116 	return error;
   1117 }
   1118 
   1119 
   1120 static int
   1121 udf_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
   1122 {
   1123 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1124 		return EROFS;
   1125 
   1126 	/*
   1127 	 * XXX we can't do this yet, as its not described in the standard yet
   1128 	 */
   1129 
   1130 	return EOPNOTSUPP;
   1131 }
   1132 
   1133 
   1134 static int
   1135 udf_chtimes(struct vnode *vp,
   1136 	struct timespec *atime, struct timespec *mtime,
   1137 	struct timespec *birthtime, int setattrflags,
   1138 	kauth_cred_t cred)
   1139 {
   1140 	struct udf_node  *udf_node = VTOI(vp);
   1141 	uid_t uid;
   1142 	gid_t gid;
   1143 	int error;
   1144 
   1145 #ifdef notyet
   1146 	/* TODO get vaflags from the extended attributes? */
   1147 	/* Immutable or append-only files cannot be modified, either. */
   1148 	if (udf_node->flags & (IMMUTABLE | APPEND))
   1149 		return EPERM;
   1150 #endif
   1151 
   1152 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1153 		return EROFS;
   1154 
   1155 	/* retrieve uid/gid values */
   1156 	udf_getownership(udf_node, &uid, &gid);
   1157 
   1158 	/* check permissions */
   1159 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
   1160 	    NULL, genfs_can_chtimes(vp, setattrflags, uid, cred));
   1161 	if (error)
   1162 		return (error);
   1163 
   1164 	/* update node flags depending on what times are passed */
   1165 	if (atime->tv_sec != VNOVAL)
   1166 		if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
   1167 			udf_node->i_flags |= IN_ACCESS;
   1168 	if ((mtime->tv_sec != VNOVAL) || (birthtime->tv_sec != VNOVAL)) {
   1169 		udf_node->i_flags |= IN_CHANGE | IN_UPDATE;
   1170 		if (vp->v_mount->mnt_flag & MNT_RELATIME)
   1171 			udf_node->i_flags |= IN_ACCESS;
   1172 	}
   1173 
   1174 	return udf_update(vp, atime, mtime, birthtime, 0);
   1175 }
   1176 
   1177 
   1178 int
   1179 udf_setattr(void *v)
   1180 {
   1181 	struct vop_setattr_args /* {
   1182 		struct vnode *a_vp;
   1183 		struct vattr *a_vap;
   1184 		kauth_cred_t a_cred;
   1185 		struct lwp   *a_l;
   1186 	} */ *ap = v;
   1187 	struct vnode *vp = ap->a_vp;
   1188 /*	struct udf_node  *udf_node = VTOI(vp); */
   1189 /*	struct udf_mount *ump = udf_node->ump; */
   1190 	kauth_cred_t cred = ap->a_cred;
   1191 	struct vattr *vap = ap->a_vap;
   1192 	int error;
   1193 
   1194 	DPRINTF(CALL, ("udf_setattr called\n"));
   1195 
   1196 	/* Abort if any unsettable attribute is given. */
   1197 	error = 0;
   1198 	if (vap->va_type != VNON ||
   1199 	    vap->va_nlink != VNOVAL ||
   1200 	    vap->va_fsid != VNOVAL ||
   1201 	    vap->va_fileid != VNOVAL ||
   1202 	    vap->va_blocksize != VNOVAL ||
   1203 #ifdef notyet
   1204 	    /* checks are debated */
   1205 	    vap->va_ctime.tv_sec != VNOVAL ||
   1206 	    vap->va_ctime.tv_nsec != VNOVAL ||
   1207 	    vap->va_birthtime.tv_sec != VNOVAL ||
   1208 	    vap->va_birthtime.tv_nsec != VNOVAL ||
   1209 #endif
   1210 	    vap->va_gen != VNOVAL ||
   1211 	    vap->va_rdev != VNOVAL ||
   1212 	    vap->va_bytes != VNOVAL)
   1213 		error = EINVAL;
   1214 
   1215 	DPRINTF(ATTR, ("setattr changing:\n"));
   1216 	if (error == 0 && (vap->va_flags != VNOVAL)) {
   1217 		DPRINTF(ATTR, ("\tchflags\n"));
   1218 	 	error = udf_chflags(vp, vap->va_flags, cred);
   1219 	}
   1220 
   1221 	if (error == 0 && (vap->va_size != VNOVAL)) {
   1222 		DPRINTF(ATTR, ("\tchsize\n"));
   1223 		error = udf_chsize(vp, vap->va_size, cred);
   1224 	}
   1225 
   1226 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) {
   1227 		DPRINTF(ATTR, ("\tchown\n"));
   1228 		error = udf_chown(vp, vap->va_uid, vap->va_gid, cred);
   1229 	}
   1230 
   1231 	if (error == 0 && (vap->va_mode != VNOVAL)) {
   1232 		DPRINTF(ATTR, ("\tchmod\n"));
   1233 		error = udf_chmod(vp, vap->va_mode, cred);
   1234 	}
   1235 
   1236 	if (error == 0 &&
   1237 	    ((vap->va_atime.tv_sec != VNOVAL &&
   1238 	      vap->va_atime.tv_nsec != VNOVAL)   ||
   1239 	     (vap->va_mtime.tv_sec != VNOVAL &&
   1240 	      vap->va_mtime.tv_nsec != VNOVAL))
   1241 	    ) {
   1242 		DPRINTF(ATTR, ("\tchtimes\n"));
   1243 		error = udf_chtimes(vp, &vap->va_atime, &vap->va_mtime,
   1244 		    &vap->va_birthtime, vap->va_vaflags, cred);
   1245 	}
   1246 	VN_KNOTE(vp, NOTE_ATTRIB);
   1247 
   1248 	return error;
   1249 }
   1250 
   1251 /* --------------------------------------------------------------------- */
   1252 
   1253 /*
   1254  * Return POSIX pathconf information for UDF file systems.
   1255  */
   1256 int
   1257 udf_pathconf(void *v)
   1258 {
   1259 	struct vop_pathconf_args /* {
   1260 		struct vnode *a_vp;
   1261 		int a_name;
   1262 		register_t *a_retval;
   1263 	} */ *ap = v;
   1264 	uint32_t bits;
   1265 
   1266 	DPRINTF(CALL, ("udf_pathconf called\n"));
   1267 
   1268 	switch (ap->a_name) {
   1269 	case _PC_LINK_MAX:
   1270 		*ap->a_retval = (1<<16)-1;	/* 16 bits */
   1271 		return 0;
   1272 	case _PC_NAME_MAX:
   1273 		*ap->a_retval = UDF_MAXNAMLEN;
   1274 		return 0;
   1275 	case _PC_PATH_MAX:
   1276 		*ap->a_retval = PATH_MAX;
   1277 		return 0;
   1278 	case _PC_PIPE_BUF:
   1279 		*ap->a_retval = PIPE_BUF;
   1280 		return 0;
   1281 	case _PC_CHOWN_RESTRICTED:
   1282 		*ap->a_retval = 1;
   1283 		return 0;
   1284 	case _PC_NO_TRUNC:
   1285 		*ap->a_retval = 1;
   1286 		return 0;
   1287 	case _PC_SYNC_IO:
   1288 		*ap->a_retval = 0;     /* synchronised is off for performance */
   1289 		return 0;
   1290 	case _PC_FILESIZEBITS:
   1291 		/* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
   1292 		bits = 64; /* XXX ought to deliver 65 */
   1293 #if 0
   1294 		if (udf_node)
   1295 			bits = 64 * vp->v_mount->mnt_dev_bshift;
   1296 #endif
   1297 		*ap->a_retval = bits;
   1298 		return 0;
   1299 	}
   1300 
   1301 	return EINVAL;
   1302 }
   1303 
   1304 
   1305 /* --------------------------------------------------------------------- */
   1306 
   1307 int
   1308 udf_open(void *v)
   1309 {
   1310 	struct vop_open_args /* {
   1311 		struct vnode *a_vp;
   1312 		int a_mode;
   1313 		kauth_cred_t a_cred;
   1314 		struct proc *a_p;
   1315 	} */ *ap = v;
   1316 	int flags;
   1317 
   1318 	DPRINTF(CALL, ("udf_open called\n"));
   1319 
   1320 	/*
   1321 	 * Files marked append-only must be opened for appending.
   1322 	 * TODO: get chflags(2) flags from extened attribute.
   1323 	 */
   1324 	flags = 0;
   1325 	if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
   1326 		return (EPERM);
   1327 
   1328 	return 0;
   1329 }
   1330 
   1331 
   1332 /* --------------------------------------------------------------------- */
   1333 
   1334 int
   1335 udf_close(void *v)
   1336 {
   1337 	struct vop_close_args /* {
   1338 		struct vnode *a_vp;
   1339 		int a_fflag;
   1340 		kauth_cred_t a_cred;
   1341 		struct proc *a_p;
   1342 	} */ *ap = v;
   1343 	struct vnode *vp = ap->a_vp;
   1344 	struct udf_node *udf_node = VTOI(vp);
   1345 	int async = vp->v_mount->mnt_flag & MNT_ASYNC;
   1346 	int error;
   1347 
   1348 	DPRINTF(CALL, ("udf_close called\n"));
   1349 	udf_node = udf_node;	/* shut up gcc */
   1350 
   1351 	if (!async && (vp->v_type != VDIR)) {
   1352 		mutex_enter(vp->v_interlock);
   1353 		error = VOP_PUTPAGES(vp, 0, 0, PGO_CLEANIT);
   1354 		if (error)
   1355 			return error;
   1356 	}
   1357 
   1358 	mutex_enter(vp->v_interlock);
   1359 		if (vp->v_usecount > 1)
   1360 			udf_itimes(udf_node, NULL, NULL, NULL);
   1361 	mutex_exit(vp->v_interlock);
   1362 
   1363 	return 0;
   1364 }
   1365 
   1366 
   1367 /* --------------------------------------------------------------------- */
   1368 
   1369 static int
   1370 udf_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
   1371 {
   1372 	int flags;
   1373 
   1374 	/* check if we are allowed to write */
   1375 	switch (vap->va_type) {
   1376 	case VDIR:
   1377 	case VLNK:
   1378 	case VREG:
   1379 		/*
   1380 		 * normal nodes: check if we're on a read-only mounted
   1381 		 * filingsystem and bomb out if we're trying to write.
   1382 		 */
   1383 		if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
   1384 			return EROFS;
   1385 		break;
   1386 	case VBLK:
   1387 	case VCHR:
   1388 	case VSOCK:
   1389 	case VFIFO:
   1390 		/*
   1391 		 * special nodes: even on read-only mounted filingsystems
   1392 		 * these are allowed to be written to if permissions allow.
   1393 		 */
   1394 		break;
   1395 	default:
   1396 		/* no idea what this is */
   1397 		return EINVAL;
   1398 	}
   1399 
   1400 	/* noone may write immutable files */
   1401 	/* TODO: get chflags(2) flags from extened attribute. */
   1402 	flags = 0;
   1403 	if ((mode & VWRITE) && (flags & IMMUTABLE))
   1404 		return EPERM;
   1405 
   1406 	return 0;
   1407 }
   1408 
   1409 static int
   1410 udf_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
   1411     kauth_cred_t cred)
   1412 {
   1413 	/* ask the generic genfs_can_access to advice on security */
   1414 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode,
   1415 	    vp->v_type, vap->va_mode), vp, NULL, genfs_can_access(vp->v_type,
   1416 	    vap->va_mode, vap->va_uid, vap->va_gid, mode, cred));
   1417 }
   1418 
   1419 int
   1420 udf_access(void *v)
   1421 {
   1422 	struct vop_access_args /* {
   1423 		struct vnode *a_vp;
   1424 		int a_mode;
   1425 		kauth_cred_t a_cred;
   1426 		struct proc *a_p;
   1427 	} */ *ap = v;
   1428 	struct vnode    *vp   = ap->a_vp;
   1429 	mode_t	         mode = ap->a_mode;
   1430 	kauth_cred_t     cred = ap->a_cred;
   1431 	/* struct udf_node *udf_node = VTOI(vp); */
   1432 	struct vattr vap;
   1433 	int error;
   1434 
   1435 	DPRINTF(CALL, ("udf_access called\n"));
   1436 
   1437 	error = VOP_GETATTR(vp, &vap, NULL);
   1438 	if (error)
   1439 		return error;
   1440 
   1441 	error = udf_check_possible(vp, &vap, mode);
   1442 	if (error)
   1443 		return error;
   1444 
   1445 	error = udf_check_permitted(vp, &vap, mode, cred);
   1446 
   1447 	return error;
   1448 }
   1449 
   1450 /* --------------------------------------------------------------------- */
   1451 
   1452 int
   1453 udf_create(void *v)
   1454 {
   1455 	struct vop_create_v3_args /* {
   1456 		struct vnode *a_dvp;
   1457 		struct vnode **a_vpp;
   1458 		struct componentname *a_cnp;
   1459 		struct vattr *a_vap;
   1460 	} */ *ap = v;
   1461 	struct vnode  *dvp = ap->a_dvp;
   1462 	struct vnode **vpp = ap->a_vpp;
   1463 	struct vattr  *vap  = ap->a_vap;
   1464 	struct componentname *cnp = ap->a_cnp;
   1465 	int error;
   1466 
   1467 	DPRINTF(CALL, ("udf_create called\n"));
   1468 	error = udf_create_node(dvp, vpp, vap, cnp);
   1469 
   1470 	return error;
   1471 }
   1472 
   1473 /* --------------------------------------------------------------------- */
   1474 
   1475 int
   1476 udf_mknod(void *v)
   1477 {
   1478 	struct vop_mknod_v3_args /* {
   1479 		struct vnode *a_dvp;
   1480 		struct vnode **a_vpp;
   1481 		struct componentname *a_cnp;
   1482 		struct vattr *a_vap;
   1483 	} */ *ap = v;
   1484 	struct vnode  *dvp = ap->a_dvp;
   1485 	struct vnode **vpp = ap->a_vpp;
   1486 	struct vattr  *vap  = ap->a_vap;
   1487 	struct componentname *cnp = ap->a_cnp;
   1488 	int error;
   1489 
   1490 	DPRINTF(CALL, ("udf_mknod called\n"));
   1491 	error = udf_create_node(dvp, vpp, vap, cnp);
   1492 
   1493 	return error;
   1494 }
   1495 
   1496 /* --------------------------------------------------------------------- */
   1497 
   1498 int
   1499 udf_mkdir(void *v)
   1500 {
   1501 	struct vop_mkdir_v3_args /* {
   1502 		struct vnode *a_dvp;
   1503 		struct vnode **a_vpp;
   1504 		struct componentname *a_cnp;
   1505 		struct vattr *a_vap;
   1506 	} */ *ap = v;
   1507 	struct vnode  *dvp = ap->a_dvp;
   1508 	struct vnode **vpp = ap->a_vpp;
   1509 	struct vattr  *vap  = ap->a_vap;
   1510 	struct componentname *cnp = ap->a_cnp;
   1511 	int error;
   1512 
   1513 	DPRINTF(CALL, ("udf_mkdir called\n"));
   1514 	error = udf_create_node(dvp, vpp, vap, cnp);
   1515 
   1516 	return error;
   1517 }
   1518 
   1519 /* --------------------------------------------------------------------- */
   1520 
   1521 static int
   1522 udf_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
   1523 {
   1524 	struct udf_node *udf_node, *dir_node;
   1525 	struct vattr vap;
   1526 	int error;
   1527 
   1528 	DPRINTF(CALL, ("udf_link called\n"));
   1529 	KASSERT(dvp != vp);
   1530 	KASSERT(vp->v_type != VDIR);
   1531 	KASSERT(dvp->v_mount == vp->v_mount);
   1532 
   1533 	/* get attributes */
   1534 	dir_node = VTOI(dvp);
   1535 	udf_node = VTOI(vp);
   1536 
   1537 	error = VOP_GETATTR(vp, &vap, FSCRED);
   1538 	if (error) {
   1539 		VOP_UNLOCK(vp);
   1540 		return error;
   1541 	}
   1542 
   1543 	/* check link count overflow */
   1544 	if (vap.va_nlink >= (1<<16)-1) {	/* uint16_t */
   1545 		VOP_UNLOCK(vp);
   1546 		return EMLINK;
   1547 	}
   1548 
   1549 	error = udf_dir_attach(dir_node->ump, dir_node, udf_node, &vap, cnp);
   1550 	if (error)
   1551 		VOP_UNLOCK(vp);
   1552 	return error;
   1553 }
   1554 
   1555 int
   1556 udf_link(void *v)
   1557 {
   1558 	struct vop_link_args /* {
   1559 		struct vnode *a_dvp;
   1560 		struct vnode *a_vp;
   1561 		struct componentname *a_cnp;
   1562 	} */ *ap = v;
   1563 	struct vnode *dvp = ap->a_dvp;
   1564 	struct vnode *vp  = ap->a_vp;
   1565 	struct componentname *cnp = ap->a_cnp;
   1566 	int error;
   1567 
   1568 	error = udf_do_link(dvp, vp, cnp);
   1569 	if (error)
   1570 		VOP_ABORTOP(dvp, cnp);
   1571 
   1572 	VN_KNOTE(vp, NOTE_LINK);
   1573 	VN_KNOTE(dvp, NOTE_WRITE);
   1574 	vput(dvp);
   1575 
   1576 	return error;
   1577 }
   1578 
   1579 /* --------------------------------------------------------------------- */
   1580 
   1581 static int
   1582 udf_do_symlink(struct udf_node *udf_node, char *target)
   1583 {
   1584 	struct pathcomp pathcomp;
   1585 	uint8_t *pathbuf, *pathpos, *compnamepos;
   1586 	char *mntonname;
   1587 	int pathlen, len, compnamelen, mntonnamelen;
   1588 	int error;
   1589 
   1590 	/* process `target' to an UDF structure */
   1591 	pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
   1592 	pathpos = pathbuf;
   1593 	pathlen = 0;
   1594 
   1595 	if (*target == '/') {
   1596 		/* symlink starts from the root */
   1597 		len = UDF_PATH_COMP_SIZE;
   1598 		memset(&pathcomp, 0, len);
   1599 		pathcomp.type = UDF_PATH_COMP_ROOT;
   1600 
   1601 		/* check if its mount-point relative! */
   1602 		mntonname    = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
   1603 		mntonnamelen = strlen(mntonname);
   1604 		if (strlen(target) >= mntonnamelen) {
   1605 			if (strncmp(target, mntonname, mntonnamelen) == 0) {
   1606 				pathcomp.type = UDF_PATH_COMP_MOUNTROOT;
   1607 				target += mntonnamelen;
   1608 			}
   1609 		} else {
   1610 			target++;
   1611 		}
   1612 
   1613 		memcpy(pathpos, &pathcomp, len);
   1614 		pathpos += len;
   1615 		pathlen += len;
   1616 	}
   1617 
   1618 	error = 0;
   1619 	while (*target) {
   1620 		/* ignore multiple '/' */
   1621 		while (*target == '/') {
   1622 			target++;
   1623 		}
   1624 		if (!*target)
   1625 			break;
   1626 
   1627 		/* extract component name */
   1628 		compnamelen = 0;
   1629 		compnamepos = target;
   1630 		while ((*target) && (*target != '/')) {
   1631 			target++;
   1632 			compnamelen++;
   1633 		}
   1634 
   1635 		/* just trunc if too long ?? (security issue) */
   1636 		if (compnamelen >= 127) {
   1637 			error = ENAMETOOLONG;
   1638 			break;
   1639 		}
   1640 
   1641 		/* convert unix name to UDF name */
   1642 		len = sizeof(struct pathcomp);
   1643 		memset(&pathcomp, 0, len);
   1644 		pathcomp.type = UDF_PATH_COMP_NAME;
   1645 		len = UDF_PATH_COMP_SIZE;
   1646 
   1647 		if ((compnamelen == 2) && (strncmp(compnamepos, "..", 2) == 0))
   1648 			pathcomp.type = UDF_PATH_COMP_PARENTDIR;
   1649 		if ((compnamelen == 1) && (*compnamepos == '.'))
   1650 			pathcomp.type = UDF_PATH_COMP_CURDIR;
   1651 
   1652 		if (pathcomp.type == UDF_PATH_COMP_NAME) {
   1653 			unix_to_udf_name(
   1654 				(char *) &pathcomp.ident, &pathcomp.l_ci,
   1655 				compnamepos, compnamelen,
   1656 				&udf_node->ump->logical_vol->desc_charset);
   1657 			len = UDF_PATH_COMP_SIZE + pathcomp.l_ci;
   1658 		}
   1659 
   1660 		if (pathlen + len >= UDF_SYMLINKBUFLEN) {
   1661 			error = ENAMETOOLONG;
   1662 			break;
   1663 		}
   1664 
   1665 		memcpy(pathpos, &pathcomp, len);
   1666 		pathpos += len;
   1667 		pathlen += len;
   1668 	}
   1669 
   1670 	if (error) {
   1671 		/* aparently too big */
   1672 		free(pathbuf, M_UDFTEMP);
   1673 		return error;
   1674 	}
   1675 
   1676 	error = udf_grow_node(udf_node, pathlen);
   1677 	if (error) {
   1678 		/* failed to pregrow node */
   1679 		free(pathbuf, M_UDFTEMP);
   1680 		return error;
   1681 	}
   1682 
   1683 	/* write out structure on the new file */
   1684 	error = vn_rdwr(UIO_WRITE, udf_node->vnode,
   1685 		pathbuf, pathlen, 0,
   1686 		UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
   1687 		FSCRED, NULL, NULL);
   1688 
   1689 	/* return status of symlink contents writeout */
   1690 	free(pathbuf, M_UDFTEMP);
   1691 	return error;
   1692 }
   1693 
   1694 
   1695 int
   1696 udf_symlink(void *v)
   1697 {
   1698 	struct vop_symlink_v3_args /* {
   1699 		struct vnode *a_dvp;
   1700 		struct vnode **a_vpp;
   1701 		struct componentname *a_cnp;
   1702 		struct vattr *a_vap;
   1703 		char *a_target;
   1704 	} */ *ap = v;
   1705 	struct vnode  *dvp = ap->a_dvp;
   1706 	struct vnode **vpp = ap->a_vpp;
   1707 	struct vattr  *vap  = ap->a_vap;
   1708 	struct componentname *cnp = ap->a_cnp;
   1709 	struct udf_node *dir_node;
   1710 	struct udf_node *udf_node;
   1711 	int error;
   1712 
   1713 	DPRINTF(CALL, ("udf_symlink called\n"));
   1714 	DPRINTF(CALL, ("\tlinking to `%s`\n",  ap->a_target));
   1715 	error = udf_create_node(dvp, vpp, vap, cnp);
   1716 	KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
   1717 	if (!error) {
   1718 		dir_node = VTOI(dvp);
   1719 		udf_node = VTOI(*vpp);
   1720 		KASSERT(udf_node);
   1721 		error = udf_do_symlink(udf_node, ap->a_target);
   1722 		if (error) {
   1723 			/* remove node */
   1724 			udf_shrink_node(udf_node, 0);
   1725 			udf_dir_detach(udf_node->ump, dir_node, udf_node, cnp);
   1726 		}
   1727 	}
   1728 	return error;
   1729 }
   1730 
   1731 /* --------------------------------------------------------------------- */
   1732 
   1733 int
   1734 udf_readlink(void *v)
   1735 {
   1736 	struct vop_readlink_args /* {
   1737 		struct vnode *a_vp;
   1738 		struct uio *a_uio;
   1739 		kauth_cred_t a_cred;
   1740 	} */ *ap = v;
   1741 	struct vnode *vp = ap->a_vp;
   1742 	struct uio *uio = ap->a_uio;
   1743 	kauth_cred_t cred = ap->a_cred;
   1744 	struct udf_node *udf_node;
   1745 	struct pathcomp pathcomp;
   1746 	struct vattr vattr;
   1747 	uint8_t *pathbuf, *targetbuf, *tmpname;
   1748 	uint8_t *pathpos, *targetpos;
   1749 	char *mntonname;
   1750 	int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
   1751 	int first, error;
   1752 
   1753 	DPRINTF(CALL, ("udf_readlink called\n"));
   1754 
   1755 	udf_node = VTOI(vp);
   1756 	error = VOP_GETATTR(vp, &vattr, cred);
   1757 	if (error)
   1758 		return error;
   1759 
   1760 	/* claim temporary buffers for translation */
   1761 	pathbuf   = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
   1762 	targetbuf = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
   1763 	tmpname   = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
   1764 	memset(pathbuf, 0, UDF_SYMLINKBUFLEN);
   1765 	memset(targetbuf, 0, PATH_MAX);
   1766 
   1767 	/* read contents of file in our temporary buffer */
   1768 	error = vn_rdwr(UIO_READ, udf_node->vnode,
   1769 		pathbuf, vattr.va_size, 0,
   1770 		UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
   1771 		FSCRED, NULL, NULL);
   1772 	if (error) {
   1773 		/* failed to read in symlink contents */
   1774 		free(pathbuf, M_UDFTEMP);
   1775 		free(targetbuf, M_UDFTEMP);
   1776 		free(tmpname, M_UDFTEMP);
   1777 		return error;
   1778 	}
   1779 
   1780 	/* convert to a unix path */
   1781 	pathpos   = pathbuf;
   1782 	pathlen   = 0;
   1783 	targetpos = targetbuf;
   1784 	targetlen = PATH_MAX;
   1785 	mntonname    = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
   1786 	mntonnamelen = strlen(mntonname);
   1787 
   1788 	error = 0;
   1789 	first = 1;
   1790 	while (vattr.va_size - pathlen >= UDF_PATH_COMP_SIZE) {
   1791 		len = UDF_PATH_COMP_SIZE;
   1792 		memcpy(&pathcomp, pathpos, len);
   1793 		l_ci = pathcomp.l_ci;
   1794 		switch (pathcomp.type) {
   1795 		case UDF_PATH_COMP_ROOT :
   1796 			/* XXX should check for l_ci; bugcompatible now */
   1797 			if ((targetlen < 1) || !first) {
   1798 				error = EINVAL;
   1799 				break;
   1800 			}
   1801 			*targetpos++ = '/'; targetlen--;
   1802 			break;
   1803 		case UDF_PATH_COMP_MOUNTROOT :
   1804 			/* XXX what should it be if l_ci > 0 ? [4/48.16.1.2] */
   1805 			if (l_ci || (targetlen < mntonnamelen+1) || !first) {
   1806 				error = EINVAL;
   1807 				break;
   1808 			}
   1809 			memcpy(targetpos, mntonname, mntonnamelen);
   1810 			targetpos += mntonnamelen; targetlen -= mntonnamelen;
   1811 			if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
   1812 				/* more follows, so must be directory */
   1813 				*targetpos++ = '/'; targetlen--;
   1814 			}
   1815 			break;
   1816 		case UDF_PATH_COMP_PARENTDIR :
   1817 			/* XXX should check for l_ci; bugcompatible now */
   1818 			if (targetlen < 3) {
   1819 				error = EINVAL;
   1820 				break;
   1821 			}
   1822 			*targetpos++ = '.'; targetlen--;
   1823 			*targetpos++ = '.'; targetlen--;
   1824 			*targetpos++ = '/'; targetlen--;
   1825 			break;
   1826 		case UDF_PATH_COMP_CURDIR :
   1827 			/* XXX should check for l_ci; bugcompatible now */
   1828 			if (targetlen < 2) {
   1829 				error = EINVAL;
   1830 				break;
   1831 			}
   1832 			*targetpos++ = '.'; targetlen--;
   1833 			*targetpos++ = '/'; targetlen--;
   1834 			break;
   1835 		case UDF_PATH_COMP_NAME :
   1836 			if (l_ci == 0) {
   1837 				error = EINVAL;
   1838 				break;
   1839 			}
   1840 			memset(tmpname, 0, PATH_MAX);
   1841 			memcpy(&pathcomp, pathpos, len + l_ci);
   1842 			udf_to_unix_name(tmpname, MAXPATHLEN,
   1843 				pathcomp.ident, l_ci,
   1844 				&udf_node->ump->logical_vol->desc_charset);
   1845 			namelen = strlen(tmpname);
   1846 			if (targetlen < namelen + 1) {
   1847 				error = EINVAL;
   1848 				break;
   1849 			}
   1850 			memcpy(targetpos, tmpname, namelen);
   1851 			targetpos += namelen; targetlen -= namelen;
   1852 			if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
   1853 				/* more follows, so must be directory */
   1854 				*targetpos++ = '/'; targetlen--;
   1855 			}
   1856 			break;
   1857 		default :
   1858 			error = EINVAL;
   1859 			break;
   1860 		}
   1861 		first = 0;
   1862 		if (error)
   1863 			break;
   1864 		pathpos += UDF_PATH_COMP_SIZE + l_ci;
   1865 		pathlen += UDF_PATH_COMP_SIZE + l_ci;
   1866 
   1867 	}
   1868 	/* all processed? */
   1869 	if (vattr.va_size - pathlen > 0)
   1870 		error = EINVAL;
   1871 
   1872 	/* uiomove() to destination */
   1873 	if (!error)
   1874 		uiomove(targetbuf, PATH_MAX - targetlen, uio);
   1875 
   1876 	free(pathbuf, M_UDFTEMP);
   1877 	free(targetbuf, M_UDFTEMP);
   1878 	free(tmpname, M_UDFTEMP);
   1879 
   1880 	return error;
   1881 }
   1882 
   1883 /* --------------------------------------------------------------------- */
   1884 
   1885 /*
   1886  * udf_rename() moved to udf_rename.c
   1887  */
   1888 
   1889 /* --------------------------------------------------------------------- */
   1890 
   1891 int
   1892 udf_remove(void *v)
   1893 {
   1894 	struct vop_remove_args /* {
   1895 		struct vnode *a_dvp;
   1896 		struct vnode *a_vp;
   1897 		struct componentname *a_cnp;
   1898 	} */ *ap = v;
   1899 	struct vnode *dvp = ap->a_dvp;
   1900 	struct vnode *vp  = ap->a_vp;
   1901 	struct componentname *cnp = ap->a_cnp;
   1902 	struct udf_node *dir_node = VTOI(dvp);
   1903 	struct udf_node *udf_node = VTOI(vp);
   1904 	struct udf_mount *ump = dir_node->ump;
   1905 	int error;
   1906 
   1907 	DPRINTF(CALL, ("udf_remove called\n"));
   1908 	if (vp->v_type != VDIR) {
   1909 		error = udf_dir_detach(ump, dir_node, udf_node, cnp);
   1910 		DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
   1911 	} else {
   1912 		DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
   1913 		error = EPERM;
   1914 	}
   1915 
   1916 	if (error == 0) {
   1917 		VN_KNOTE(vp, NOTE_DELETE);
   1918 		VN_KNOTE(dvp, NOTE_WRITE);
   1919 	}
   1920 
   1921 	if (dvp == vp)
   1922 		vrele(vp);
   1923 	else
   1924 		vput(vp);
   1925 	vput(dvp);
   1926 
   1927 	return error;
   1928 }
   1929 
   1930 /* --------------------------------------------------------------------- */
   1931 
   1932 int
   1933 udf_rmdir(void *v)
   1934 {
   1935 	struct vop_rmdir_args /* {
   1936 		struct vnode *a_dvp;
   1937 		struct vnode *a_vp;
   1938 		struct componentname *a_cnp;
   1939 	} */ *ap = v;
   1940 	struct vnode *vp = ap->a_vp;
   1941 	struct vnode *dvp = ap->a_dvp;
   1942 	struct componentname *cnp = ap->a_cnp;
   1943 	struct udf_node *dir_node = VTOI(dvp);
   1944 	struct udf_node *udf_node = VTOI(vp);
   1945 	struct udf_mount *ump = dir_node->ump;
   1946 	int error, isempty;
   1947 
   1948 	DPRINTF(NOTIMPL, ("udf_rmdir '%s' called\n", cnp->cn_nameptr));
   1949 
   1950 	/* don't allow '.' to be deleted */
   1951 	if (dir_node == udf_node) {
   1952 		vrele(dvp);
   1953 		vput(vp);
   1954 		return EINVAL;
   1955 	}
   1956 
   1957 	/* make sure our `leaf' node's hash is populated */
   1958 	dirhash_get(&udf_node->dir_hash);
   1959 	error = udf_dirhash_fill(udf_node);
   1960 	if (error) {
   1961 		dirhash_put(udf_node->dir_hash);
   1962 		return error;
   1963 	}
   1964 
   1965 	/* check to see if the directory is empty */
   1966 	isempty = dirhash_dir_isempty(udf_node->dir_hash);
   1967 	dirhash_put(udf_node->dir_hash);
   1968 
   1969 	if (!isempty) {
   1970 		vput(dvp);
   1971 		vput(vp);
   1972 		return ENOTEMPTY;
   1973 	}
   1974 
   1975 	/* detach the node from the directory, udf_node is an empty dir here */
   1976 	error = udf_dir_detach(ump, dir_node, udf_node, cnp);
   1977 	if (error == 0) {
   1978 		cache_purge(vp);
   1979 //		cache_purge(dvp);	/* XXX from msdosfs, why? */
   1980 		/*
   1981 		 * Bug alert: we need to remove '..' from the detaching
   1982 		 * udf_node so further lookups of this are not possible. This
   1983 		 * prevents a process in a deleted directory from going to its
   1984 		 * deleted parent. Since `udf_node' is garanteed to be empty
   1985 		 * here, trunc it so no fids are there.
   1986 		 */
   1987 		dirhash_purge(&udf_node->dir_hash);
   1988 		udf_shrink_node(udf_node, 0);
   1989 		VN_KNOTE(vp, NOTE_DELETE);
   1990 	}
   1991 	DPRINTFIF(NODE, error, ("\tgot error removing dir\n"));
   1992 
   1993 	/* unput the nodes and exit */
   1994 	vput(dvp);
   1995 	vput(vp);
   1996 
   1997 	return error;
   1998 }
   1999 
   2000 /* --------------------------------------------------------------------- */
   2001 
   2002 int
   2003 udf_fsync(void *v)
   2004 {
   2005 	struct vop_fsync_args /* {
   2006 		struct vnode *a_vp;
   2007 		kauth_cred_t a_cred;
   2008 		int a_flags;
   2009 		off_t offlo;
   2010 		off_t offhi;
   2011 		struct proc *a_p;
   2012 	} */ *ap = v;
   2013 	struct vnode *vp = ap->a_vp;
   2014 	struct udf_node *udf_node = VTOI(vp);
   2015 	int error, flags, wait;
   2016 
   2017 	DPRINTF(SYNC, ("udf_fsync called on %p : %s, %s\n",
   2018 		udf_node,
   2019 		(ap->a_flags & FSYNC_WAIT)     ? "wait":"no wait",
   2020 		(ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
   2021 
   2022 	/* flush data and wait for it when requested */
   2023 	wait = (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
   2024 	error = vflushbuf(vp, ap->a_flags);
   2025 	if (error)
   2026 		return error;
   2027 
   2028 	if (udf_node == NULL) {
   2029 		printf("udf_fsync() called on NULL udf_node!\n");
   2030 		return 0;
   2031 	}
   2032 	if (vp->v_tag != VT_UDF) {
   2033 		printf("udf_fsync() called on node not tagged as UDF node!\n");
   2034 		return 0;
   2035 	}
   2036 
   2037 	/* set our times */
   2038 	udf_itimes(udf_node, NULL, NULL, NULL);
   2039 
   2040 	/* if called when mounted readonly, never write back */
   2041 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   2042 		return 0;
   2043 
   2044 	/* if only data is requested, return */
   2045 	if (ap->a_flags & FSYNC_DATAONLY)
   2046 		return 0;
   2047 
   2048 	/* check if the node is dirty 'enough'*/
   2049 	flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
   2050 	if (flags == 0)
   2051 		return 0;
   2052 
   2053 	/* if we don't have to wait, check for IO pending */
   2054 	if (!wait) {
   2055 		if (vp->v_numoutput > 0) {
   2056 			DPRINTF(SYNC, ("udf_fsync %p, rejecting on v_numoutput\n", udf_node));
   2057 			return 0;
   2058 		}
   2059 		if (udf_node->outstanding_bufs > 0) {
   2060 			DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_bufs\n", udf_node));
   2061 			return 0;
   2062 		}
   2063 		if (udf_node->outstanding_nodedscr > 0) {
   2064 			DPRINTF(SYNC, ("udf_fsync %p, rejecting on outstanding_nodedscr\n", udf_node));
   2065 			return 0;
   2066 		}
   2067 	}
   2068 
   2069 	/* wait until vp->v_numoutput reaches zero i.e. is finished */
   2070 	if (wait) {
   2071 		DPRINTF(SYNC, ("udf_fsync %p, waiting\n", udf_node));
   2072 		mutex_enter(vp->v_interlock);
   2073 		while (vp->v_numoutput) {
   2074 			DPRINTF(SYNC, ("udf_fsync %p, v_numoutput %d\n", udf_node, vp->v_numoutput));
   2075 			cv_timedwait(&vp->v_cv, vp->v_interlock, hz/8);
   2076 		}
   2077 		mutex_exit(vp->v_interlock);
   2078 		DPRINTF(SYNC, ("udf_fsync %p, fin wait\n", udf_node));
   2079 	}
   2080 
   2081 	/* write out node and wait for it if requested */
   2082 	DPRINTF(SYNC, ("udf_fsync %p, writeout node\n", udf_node));
   2083 	error = udf_writeout_node(udf_node, wait);
   2084 	if (error)
   2085 		return error;
   2086 
   2087 	/* TODO/XXX if ap->a_flags & FSYNC_CACHE, we ought to do a disc sync */
   2088 
   2089 	return 0;
   2090 }
   2091 
   2092 /* --------------------------------------------------------------------- */
   2093 
   2094 int
   2095 udf_advlock(void *v)
   2096 {
   2097 	struct vop_advlock_args /* {
   2098 		struct vnode *a_vp;
   2099 		void *a_id;
   2100 		int a_op;
   2101 		struct flock *a_fl;
   2102 		int a_flags;
   2103 	} */ *ap = v;
   2104 	struct vnode *vp = ap->a_vp;
   2105 	struct udf_node *udf_node = VTOI(vp);
   2106 	struct file_entry    *fe;
   2107 	struct extfile_entry *efe;
   2108 	uint64_t file_size;
   2109 
   2110 	DPRINTF(LOCKING, ("udf_advlock called\n"));
   2111 
   2112 	/* get directory filesize */
   2113 	if (udf_node->fe) {
   2114 		fe = udf_node->fe;
   2115 		file_size = udf_rw64(fe->inf_len);
   2116 	} else {
   2117 		assert(udf_node->efe);
   2118 		efe = udf_node->efe;
   2119 		file_size = udf_rw64(efe->inf_len);
   2120 	}
   2121 
   2122 	return lf_advlock(ap, &udf_node->lockf, file_size);
   2123 }
   2124 
   2125 /* --------------------------------------------------------------------- */
   2126 
   2127 /* Global vfs vnode data structures for udfs */
   2128 int (**udf_vnodeop_p)(void *);
   2129 
   2130 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
   2131 	{ &vop_default_desc, vn_default_error },
   2132 	{ &vop_lookup_desc, udf_lookup },	/* lookup */
   2133 	{ &vop_create_desc, udf_create },	/* create */
   2134 	{ &vop_mknod_desc, udf_mknod },		/* mknod */	/* TODO */
   2135 	{ &vop_open_desc, udf_open },		/* open */
   2136 	{ &vop_close_desc, udf_close },		/* close */
   2137 	{ &vop_access_desc, udf_access },	/* access */
   2138 	{ &vop_getattr_desc, udf_getattr },	/* getattr */
   2139 	{ &vop_setattr_desc, udf_setattr },	/* setattr */	/* TODO chflags */
   2140 	{ &vop_read_desc, udf_read },		/* read */
   2141 	{ &vop_write_desc, udf_write },		/* write */	/* WRITE */
   2142 	{ &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
   2143 	{ &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
   2144 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
   2145 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
   2146 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
   2147 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
   2148 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
   2149 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
   2150 	{ &vop_fsync_desc, udf_fsync },		/* fsync */
   2151 	{ &vop_seek_desc, genfs_seek },		/* seek */
   2152 	{ &vop_remove_desc, udf_remove },	/* remove */
   2153 	{ &vop_link_desc, udf_link },		/* link */	/* TODO */
   2154 	{ &vop_rename_desc, udf_rename },	/* rename */ 	/* TODO */
   2155 	{ &vop_mkdir_desc, udf_mkdir },		/* mkdir */
   2156 	{ &vop_rmdir_desc, udf_rmdir },		/* rmdir */
   2157 	{ &vop_symlink_desc, udf_symlink },	/* symlink */	/* TODO */
   2158 	{ &vop_readdir_desc, udf_readdir },	/* readdir */
   2159 	{ &vop_readlink_desc, udf_readlink },	/* readlink */	/* TEST ME */
   2160 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
   2161 	{ &vop_inactive_desc, udf_inactive },	/* inactive */
   2162 	{ &vop_reclaim_desc, udf_reclaim },	/* reclaim */
   2163 	{ &vop_lock_desc, genfs_lock },		/* lock */
   2164 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
   2165 	{ &vop_bmap_desc, udf_trivial_bmap },	/* bmap */	/* 1:1 bmap */
   2166 	{ &vop_strategy_desc, udf_vfsstrategy },/* strategy */
   2167 /*	{ &vop_print_desc, udf_print },	*/	/* print */
   2168 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
   2169 	{ &vop_pathconf_desc, udf_pathconf },	/* pathconf */
   2170 	{ &vop_advlock_desc, udf_advlock },	/* advlock */	/* TEST ME */
   2171 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
   2172 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
   2173 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
   2174 	{ NULL, NULL }
   2175 };
   2176 
   2177 
   2178 const struct vnodeopv_desc udf_vnodeop_opv_desc = {
   2179 	&udf_vnodeop_p, udf_vnodeop_entries
   2180 };
   2181