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