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