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