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