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