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