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