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