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