xref: /netbsd-src/sys/fs/udf/udf_vnops.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /* $NetBSD: udf_vnops.c,v 1.30 2008/07/17 19:10:22 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.30 2008/07/17 19:10:22 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, 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, 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, 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, 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 		/* get our node */
698 		name    = "..";
699 		namelen = 2;
700 		error = udf_lookup_name_in_dir(dvp, name, namelen,
701 				&icb_loc, &found);
702 		if (error)
703 			goto out;
704 		if (!found)
705 			error = ENOENT;
706 
707 		/* first unlock parent */
708 		VOP_UNLOCK(dvp, 0);
709 
710 		if (error == 0) {
711 			DPRINTF(LOOKUP, ("\tfound '..'\n"));
712 			/* try to create/reuse the node */
713 			error = udf_get_node(ump, &icb_loc, &res_node);
714 
715 			if (!error) {
716 				DPRINTF(LOOKUP,
717 					("\tnode retrieved/created OK\n"));
718 				*vpp = res_node->vnode;
719 			}
720 		}
721 
722 		/* try to relock parent */
723 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
724 	} else {
725 		DPRINTF(LOOKUP, ("\tlookup file\n"));
726 		/* all other files */
727 		/* lookup filename in the directory; location icb_loc */
728 		name    = cnp->cn_nameptr;
729 		namelen = cnp->cn_namelen;
730 		error = udf_lookup_name_in_dir(dvp, name, namelen,
731 				&icb_loc, &found);
732 		if (error)
733 			goto out;
734 		if (!found) {
735 			DPRINTF(LOOKUP, ("\tNOT found\n"));
736 			/*
737 			 * UGH, didn't find name. If we're creating or
738 			 * renaming on the last name this is OK and we ought
739 			 * to return EJUSTRETURN if its allowed to be created.
740 			 */
741 			error = ENOENT;
742 			if (islastcn &&
743 				(nameiop == CREATE || nameiop == RENAME))
744 					error = 0;
745 			if (!error) {
746 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
747 				if (!error) {
748 					/* keep the component name */
749 					cnp->cn_flags |= SAVENAME;
750 					error = EJUSTRETURN;
751 				}
752 			}
753 			/* done */
754 		} else {
755 			/* try to create/reuse the node */
756 			error = udf_get_node(ump, &icb_loc, &res_node);
757 			if (!error) {
758 				/*
759 				 * If we are not at the last path component
760 				 * and found a non-directory or non-link entry
761 				 * (which may itself be pointing to a
762 				 * directory), raise an error.
763 				 */
764 				vnodetp = res_node->vnode->v_type;
765 				if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
766 					if (!islastcn)
767 						error = ENOTDIR;
768 				}
769 
770 			}
771 			if (!error) {
772 				*vpp = res_node->vnode;
773 			}
774 		}
775 	}
776 
777 out:
778 	/*
779 	 * Store result in the cache if requested. If we are creating a file,
780 	 * the file might not be found and thus putting it into the namecache
781 	 * might be seen as negative caching.
782 	 */
783 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
784 		cache_enter(dvp, *vpp, cnp);
785 
786 	DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error));
787 
788 	return error;
789 }
790 
791 /* --------------------------------------------------------------------- */
792 
793 int
794 udf_getattr(void *v)
795 {
796 	struct vop_getattr_args /* {
797 		struct vnode *a_vp;
798 		struct vattr *a_vap;
799 		kauth_cred_t a_cred;
800 		struct lwp   *a_l;
801 	} */ *ap = v;
802 	struct vnode *vp = ap->a_vp;
803 	struct udf_node *udf_node = VTOI(vp);
804 	struct udf_mount *ump = udf_node->ump;
805 	struct file_entry    *fe  = udf_node->fe;
806 	struct extfile_entry *efe = udf_node->efe;
807 	struct filetimes_extattr_entry *ft_extattr;
808 	struct device_extattr_entry *devattr;
809 	struct vattr *vap = ap->a_vap;
810 	struct timestamp *atime, *mtime, *attrtime, *creatime;
811 	uint64_t filesize, blkssize;
812 	uint32_t nlink;
813 	uint32_t offset, a_l;
814 	uint8_t *filedata;
815 	uid_t uid;
816 	gid_t gid;
817 	int error;
818 
819 	DPRINTF(CALL, ("udf_getattr called\n"));
820 
821 	/* update times before we returning values */
822 	udf_itimes(udf_node, NULL, NULL, NULL);
823 
824 	/* get descriptor information */
825 	if (fe) {
826 		nlink    = udf_rw16(fe->link_cnt);
827 		uid      = (uid_t)udf_rw32(fe->uid);
828 		gid      = (gid_t)udf_rw32(fe->gid);
829 		filesize = udf_rw64(fe->inf_len);
830 		blkssize = udf_rw64(fe->logblks_rec);
831 		atime    = &fe->atime;
832 		mtime    = &fe->mtime;
833 		attrtime = &fe->attrtime;
834 		filedata = fe->data;
835 
836 		/* initial guess */
837 		creatime = mtime;
838 
839 		/* check our extended attribute if present */
840 		error = udf_extattr_search_intern(udf_node,
841 			UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
842 		if (!error) {
843 			ft_extattr = (struct filetimes_extattr_entry *)
844 				(filedata + offset);
845 			if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
846 				creatime = &ft_extattr->times[0];
847 		}
848 	} else {
849 		assert(udf_node->efe);
850 		nlink    = udf_rw16(efe->link_cnt);
851 		uid      = (uid_t)udf_rw32(efe->uid);
852 		gid      = (gid_t)udf_rw32(efe->gid);
853 		filesize = udf_rw64(efe->inf_len);	/* XXX or obj_size? */
854 		blkssize = udf_rw64(efe->logblks_rec);
855 		atime    = &efe->atime;
856 		mtime    = &efe->mtime;
857 		attrtime = &efe->attrtime;
858 		creatime = &efe->ctime;
859 		filedata = efe->data;
860 	}
861 
862 	/* do the uid/gid translation game */
863 	if ((uid == (uid_t) -1) && (gid == (gid_t) -1)) {
864 		uid = ump->mount_args.anon_uid;
865 		gid = ump->mount_args.anon_gid;
866 	}
867 
868 	/* fill in struct vattr with values from the node */
869 	VATTR_NULL(vap);
870 	vap->va_type      = vp->v_type;
871 	vap->va_mode      = udf_getaccessmode(udf_node);
872 	vap->va_nlink     = nlink;
873 	vap->va_uid       = uid;
874 	vap->va_gid       = gid;
875 	vap->va_fsid      = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
876 	vap->va_fileid    = udf_calchash(&udf_node->loc);   /* inode hash XXX */
877 	vap->va_size      = filesize;
878 	vap->va_blocksize = udf_node->ump->discinfo.sector_size;  /* wise? */
879 
880 	/*
881 	 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add
882 	 * 1 to the link count if its a directory we're requested attributes
883 	 * of.
884 	 */
885 	if (vap->va_type == VDIR)
886 		vap->va_nlink++;
887 
888 	/* access times */
889 	udf_timestamp_to_timespec(ump, atime,    &vap->va_atime);
890 	udf_timestamp_to_timespec(ump, mtime,    &vap->va_mtime);
891 	udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime);
892 	udf_timestamp_to_timespec(ump, creatime, &vap->va_birthtime);
893 
894 	vap->va_gen       = 1;		/* no multiple generations yes (!?) */
895 	vap->va_flags     = 0;		/* no flags */
896 	vap->va_bytes     = blkssize * udf_node->ump->discinfo.sector_size;
897 	vap->va_filerev   = 1;		/* TODO file revision numbers? */
898 	vap->va_vaflags   = 0;
899 	/* TODO get vaflags from the extended attributes? */
900 
901 	if ((vap->va_type == VBLK) || (vap->va_type == VCHR)) {
902 		error = udf_extattr_search_intern(udf_node,
903 				UDF_DEVICESPEC_ATTR_NO, "",
904 				&offset, &a_l);
905 		/* if error, deny access */
906 		if (error || (filedata == NULL)) {
907 			vap->va_mode = 0;	/* or v_type = VNON?  */
908 		} else {
909 			devattr = (struct device_extattr_entry *)
910 				filedata + offset;
911 			vap->va_rdev = makedev(
912 				udf_rw32(devattr->major),
913 				udf_rw32(devattr->minor)
914 				);
915 			/* TODO we could check the implementator */
916 		}
917 	}
918 
919 	return 0;
920 }
921 
922 /* --------------------------------------------------------------------- */
923 
924 static int
925 udf_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid,
926 	  kauth_cred_t cred)
927 {
928 	struct udf_node  *udf_node = VTOI(vp);
929 	uid_t euid, uid;
930 	gid_t egid, gid;
931 	int issuperuser, ismember;
932 	int error;
933 
934 #ifdef notyet
935 	/* TODO get vaflags from the extended attributes? */
936 	/* Immutable or append-only files cannot be modified, either. */
937 	if (udf_node->flags & (IMMUTABLE | APPEND))
938 		return EPERM;
939 #endif
940 
941 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
942 		return EROFS;
943 
944 	/* retrieve old values */
945 	udf_getownership(udf_node, &uid, &gid);
946 
947 	/* only one could be specified */
948 	if (new_uid == VNOVAL)
949 		new_uid = uid;
950 	if (new_gid == VNOVAL)
951 		new_gid = gid;
952 
953 	/* check if we can fit it in an 32 bits */
954 	if ((uid_t) ((uint32_t) uid) != uid)
955 		return EINVAL;
956 	if ((gid_t) ((uint32_t) gid) != gid)
957 		return EINVAL;
958 
959 	/*
960 	 * If we don't own the file, are trying to change the owner of the
961 	 * file, or are not a member of the target group, the caller's
962 	 * credentials must imply super-user privilege or the call fails.
963 	 */
964 
965 	/* check permissions */
966 	euid  = kauth_cred_geteuid(cred);
967 	egid  = kauth_cred_getegid(cred);
968 	if ((error = kauth_cred_ismember_gid(cred, new_gid, &ismember)))
969 		return error;
970 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
971 	issuperuser = (error == 0);
972 
973 	if (!issuperuser) {
974 		if ((new_uid != uid) || (euid != uid))
975 			return EPERM;
976 		if ((new_gid != gid) && !(egid == new_gid || ismember))
977 			return EPERM;
978 	}
979 
980 	/* change the ownership */
981 	udf_setownership(udf_node, new_uid, new_gid);
982 
983 	/* mark node changed */
984 	udf_node->i_flags |= IN_CHANGE;
985 
986 	return 0;
987 }
988 
989 
990 static int
991 udf_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred)
992 {
993 	struct udf_node  *udf_node = VTOI(vp);
994 	uid_t euid, uid;
995 	gid_t egid, gid;
996 	int issuperuser, ismember;
997 	int error;
998 
999 #ifdef notyet
1000 	/* TODO get vaflags from the extended attributes? */
1001 	/* Immutable or append-only files cannot be modified, either. */
1002 	if (udf_node->flags & (IMMUTABLE | APPEND))
1003 		return EPERM;
1004 #endif
1005 
1006 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1007 		return EROFS;
1008 
1009 	/* retrieve uid/gid values */
1010 	udf_getownership(udf_node, &uid, &gid);
1011 
1012 	/* check permissions */
1013 	euid  = kauth_cred_geteuid(cred);
1014 	egid  = kauth_cred_getegid(cred);
1015 	if ((error = kauth_cred_ismember_gid(cred, gid, &ismember)))
1016 		return error;
1017 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
1018 	issuperuser = (error == 0);
1019 
1020 	if ((euid != uid) && !issuperuser)
1021 		return EPERM;
1022 	if (euid != 0) {
1023 		if (vp->v_type != VDIR && (mode & S_ISTXT))
1024 			return EFTYPE;
1025 
1026 		if ((!ismember) && (mode & S_ISGID))
1027 			return EPERM;
1028 	}
1029 
1030 	/* change mode */
1031 	udf_setaccessmode(udf_node, mode);
1032 
1033 	/* mark node changed */
1034 	udf_node->i_flags |= IN_CHANGE;
1035 
1036 	return 0;
1037 }
1038 
1039 
1040 /* exported */
1041 int
1042 udf_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred)
1043 {
1044 	struct udf_node  *udf_node = VTOI(vp);
1045 	int error, extended;
1046 
1047 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1048 		return EROFS;
1049 
1050 	/* Decide whether this is a valid operation based on the file type. */
1051 	switch (vp->v_type) {
1052 	case VDIR:
1053 		return EISDIR;
1054 	case VREG:
1055 		if (vp->v_mount->mnt_flag & MNT_RDONLY)
1056 			return EROFS;
1057 		break;
1058 	case VBLK:
1059 		/* FALLTHROUGH */
1060 	case VCHR:
1061 		/* FALLTHROUGH */
1062 	case VFIFO:
1063 		/* Allow modifications of special files even if in the file
1064 		 * system is mounted read-only (we are not modifying the
1065 		 * files themselves, but the objects they represent). */
1066 		return 0;
1067 	default:
1068 		/* Anything else is unsupported. */
1069 		return EOPNOTSUPP;
1070 	}
1071 
1072 #if notyet
1073 	/* TODO get vaflags from the extended attributes? */
1074 	/* Immutable or append-only files cannot be modified, either. */
1075 	if (node->flags & (IMMUTABLE | APPEND))
1076 		return EPERM;
1077 #endif
1078 
1079 	/* resize file to the requested size */
1080 	error = udf_resize_node(udf_node, newsize, &extended);
1081 
1082 	if (error == 0) {
1083 		/* mark change */
1084 		udf_node->i_flags |= IN_CHANGE | IN_MODIFY;
1085 		VN_KNOTE(vp, NOTE_ATTRIB | (extended ? NOTE_EXTEND : 0));
1086 		udf_update(vp, NULL, NULL, NULL, 0);
1087 	}
1088 
1089 	return error;
1090 }
1091 
1092 
1093 static int
1094 udf_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred)
1095 {
1096 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1097 		return EROFS;
1098 
1099 	/* XXX we can't do this yet XXX */
1100 
1101 	return EINVAL;
1102 }
1103 
1104 
1105 static int
1106 udf_chtimes(struct vnode *vp,
1107 	struct timespec *atime, struct timespec *mtime,
1108 	struct timespec *birthtime, int setattrflags,
1109 	kauth_cred_t cred)
1110 {
1111 	struct udf_node  *udf_node = VTOI(vp);
1112 	uid_t euid, uid;
1113 	gid_t gid;
1114 	int issuperuser;
1115 	int error;
1116 
1117 #ifdef notyet
1118 	/* TODO get vaflags from the extended attributes? */
1119 	/* Immutable or append-only files cannot be modified, either. */
1120 	if (udf_node->flags & (IMMUTABLE | APPEND))
1121 		return EPERM;
1122 #endif
1123 
1124 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
1125 		return EROFS;
1126 
1127 	/* retrieve uid/gid values */
1128 	udf_getownership(udf_node, &uid, &gid);
1129 
1130 	/* check permissions */
1131 	euid  = kauth_cred_geteuid(cred);
1132 	error = kauth_authorize_generic(cred, KAUTH_GENERIC_ISSUSER, NULL);
1133 	issuperuser = (error == 0);
1134 
1135 	if (!issuperuser) {
1136 		if (euid != uid)
1137 			return EPERM;
1138 		if ((setattrflags & VA_UTIMES_NULL) == 0) {
1139 			error = VOP_ACCESS(vp, VWRITE, cred);
1140 			if (error)
1141 				return error;
1142 		}
1143 	}
1144 
1145 	/* update node flags depending on what times are passed */
1146 	if (atime->tv_sec != VNOVAL)
1147 		if (!(vp->v_mount->mnt_flag & MNT_NOATIME))
1148 			udf_node->i_flags |= IN_ACCESS;
1149 	if ((mtime->tv_sec != VNOVAL) || (birthtime->tv_sec != VNOVAL))
1150 		udf_node->i_flags |= IN_CHANGE | IN_UPDATE;
1151 
1152 	return udf_update(vp, atime, mtime, birthtime, 0);
1153 }
1154 
1155 
1156 int
1157 udf_setattr(void *v)
1158 {
1159 	struct vop_setattr_args /* {
1160 		struct vnode *a_vp;
1161 		struct vattr *a_vap;
1162 		kauth_cred_t a_cred;
1163 		struct lwp   *a_l;
1164 	} */ *ap = v;
1165 	struct vnode *vp = ap->a_vp;
1166 /*	struct udf_node  *udf_node = VTOI(vp); */
1167 /*	struct udf_mount *ump = udf_node->ump; */
1168 	kauth_cred_t cred = ap->a_cred;
1169 	struct vattr *vap = ap->a_vap;
1170 	int error;
1171 
1172 	DPRINTF(CALL, ("udf_setattr called\n"));
1173 
1174 	/* Abort if any unsettable attribute is given. */
1175 	error = 0;
1176 	if (vap->va_type != VNON ||
1177 	    vap->va_nlink != VNOVAL ||
1178 	    vap->va_fsid != VNOVAL ||
1179 	    vap->va_fileid != VNOVAL ||
1180 	    vap->va_blocksize != VNOVAL ||
1181 #ifdef notyet
1182 	    /* checks are debated */
1183 	    vap->va_ctime.tv_sec != VNOVAL ||
1184 	    vap->va_ctime.tv_nsec != VNOVAL ||
1185 	    vap->va_birthtime.tv_sec != VNOVAL ||
1186 	    vap->va_birthtime.tv_nsec != VNOVAL ||
1187 #endif
1188 	    vap->va_gen != VNOVAL ||
1189 	    vap->va_rdev != VNOVAL ||
1190 	    vap->va_bytes != VNOVAL)
1191 		error = EINVAL;
1192 
1193 	DPRINTF(ATTR, ("setattr changing:\n"));
1194 	if (error == 0 && (vap->va_flags != VNOVAL)) {
1195 		DPRINTF(ATTR, ("\tchflags\n"));
1196 	 	error = udf_chflags(vp, vap->va_flags, cred);
1197 	}
1198 
1199 	if (error == 0 && (vap->va_size != VNOVAL)) {
1200 		DPRINTF(ATTR, ("\tchsize\n"));
1201 		error = udf_chsize(vp, vap->va_size, cred);
1202 	}
1203 
1204 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) {
1205 		DPRINTF(ATTR, ("\tchown\n"));
1206 		error = udf_chown(vp, vap->va_uid, vap->va_gid, cred);
1207 	}
1208 
1209 	if (error == 0 && (vap->va_mode != VNOVAL)) {
1210 		DPRINTF(ATTR, ("\tchmod\n"));
1211 		error = udf_chmod(vp, vap->va_mode, cred);
1212 	}
1213 
1214 	if (error == 0 &&
1215 	    ((vap->va_atime.tv_sec != VNOVAL &&
1216 	      vap->va_atime.tv_nsec != VNOVAL)   ||
1217 	     (vap->va_mtime.tv_sec != VNOVAL &&
1218 	      vap->va_mtime.tv_nsec != VNOVAL))
1219 	    ) {
1220 		DPRINTF(ATTR, ("\tchtimes\n"));
1221 		error = udf_chtimes(vp, &vap->va_atime, &vap->va_mtime,
1222 		    &vap->va_birthtime, vap->va_vaflags, cred);
1223 	}
1224 	VN_KNOTE(vp, NOTE_ATTRIB);
1225 
1226 	return error;
1227 }
1228 
1229 /* --------------------------------------------------------------------- */
1230 
1231 /*
1232  * Return POSIX pathconf information for UDF file systems.
1233  */
1234 int
1235 udf_pathconf(void *v)
1236 {
1237 	struct vop_pathconf_args /* {
1238 		struct vnode *a_vp;
1239 		int a_name;
1240 		register_t *a_retval;
1241 	} */ *ap = v;
1242 	uint32_t bits;
1243 
1244 	DPRINTF(CALL, ("udf_pathconf called\n"));
1245 
1246 	switch (ap->a_name) {
1247 	case _PC_LINK_MAX:
1248 		*ap->a_retval = (1<<16)-1;	/* 16 bits */
1249 		return 0;
1250 	case _PC_NAME_MAX:
1251 		*ap->a_retval = NAME_MAX;
1252 		return 0;
1253 	case _PC_PATH_MAX:
1254 		*ap->a_retval = PATH_MAX;
1255 		return 0;
1256 	case _PC_PIPE_BUF:
1257 		*ap->a_retval = PIPE_BUF;
1258 		return 0;
1259 	case _PC_CHOWN_RESTRICTED:
1260 		*ap->a_retval = 1;
1261 		return 0;
1262 	case _PC_NO_TRUNC:
1263 		*ap->a_retval = 1;
1264 		return 0;
1265 	case _PC_SYNC_IO:
1266 		*ap->a_retval = 0;     /* synchronised is off for performance */
1267 		return 0;
1268 	case _PC_FILESIZEBITS:
1269 		/* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */
1270 		bits = 64; /* XXX ought to deliver 65 */
1271 #if 0
1272 		if (udf_node)
1273 			bits = 64 * vp->v_mount->mnt_dev_bshift;
1274 #endif
1275 		*ap->a_retval = bits;
1276 		return 0;
1277 	}
1278 
1279 	return EINVAL;
1280 }
1281 
1282 
1283 /* --------------------------------------------------------------------- */
1284 
1285 int
1286 udf_open(void *v)
1287 {
1288 	struct vop_open_args /* {
1289 		struct vnode *a_vp;
1290 		int a_mode;
1291 		kauth_cred_t a_cred;
1292 		struct proc *a_p;
1293 	} */ *ap = v;
1294 	int flags;
1295 
1296 	DPRINTF(CALL, ("udf_open called\n"));
1297 
1298 	/*
1299 	 * Files marked append-only must be opened for appending.
1300 	 * TODO: get chflags(2) flags from extened attribute.
1301 	 */
1302 	flags = 0;
1303 	if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
1304 		return (EPERM);
1305 
1306 	return 0;
1307 }
1308 
1309 
1310 /* --------------------------------------------------------------------- */
1311 
1312 int
1313 udf_close(void *v)
1314 {
1315 	struct vop_close_args /* {
1316 		struct vnode *a_vp;
1317 		int a_fflag;
1318 		kauth_cred_t a_cred;
1319 		struct proc *a_p;
1320 	} */ *ap = v;
1321 	struct vnode *vp = ap->a_vp;
1322 	struct udf_node *udf_node = VTOI(vp);
1323 
1324 	DPRINTF(CALL, ("udf_close called\n"));
1325 	udf_node = udf_node;	/* shut up gcc */
1326 
1327 	mutex_enter(&vp->v_interlock);
1328 		if (vp->v_usecount > 1)
1329 			udf_itimes(udf_node, NULL, NULL, NULL);
1330 	mutex_exit(&vp->v_interlock);
1331 
1332 	return 0;
1333 }
1334 
1335 
1336 /* --------------------------------------------------------------------- */
1337 
1338 int
1339 udf_access(void *v)
1340 {
1341 	struct vop_access_args /* {
1342 		struct vnode *a_vp;
1343 		int a_mode;
1344 		kauth_cred_t a_cred;
1345 		struct proc *a_p;
1346 	} */ *ap = v;
1347 	struct vnode    *vp   = ap->a_vp;
1348 	mode_t	         mode = ap->a_mode;
1349 	kauth_cred_t     cred = ap->a_cred;
1350 	/* struct udf_node *udf_node = VTOI(vp); */
1351 	struct vattr vap;
1352 	int flags;
1353 	int error;
1354 
1355 	DPRINTF(CALL, ("udf_access called\n"));
1356 
1357 	error = VOP_GETATTR(vp, &vap, NULL);
1358 	if (error)
1359 		return error;
1360 
1361 	/* check if we are allowed to write */
1362 	switch (vap.va_type) {
1363 	case VDIR:
1364 	case VLNK:
1365 	case VREG:
1366 		/*
1367 		 * normal nodes: check if we're on a read-only mounted
1368 		 * filingsystem and bomb out if we're trying to write.
1369 		 */
1370 		if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
1371 			return EROFS;
1372 		break;
1373 	case VBLK:
1374 	case VCHR:
1375 	case VSOCK:
1376 	case VFIFO:
1377 		/*
1378 		 * special nodes: even on read-only mounted filingsystems
1379 		 * these are allowed to be written to if permissions allow.
1380 		 */
1381 		break;
1382 	default:
1383 		/* no idea what this is */
1384 		return EINVAL;
1385 	}
1386 
1387 	/* noone may write immutable files */
1388 	/* TODO: get chflags(2) flags from extened attribute. */
1389 	flags = 0;
1390 	if ((mode & VWRITE) && (flags & IMMUTABLE))
1391 		return EPERM;
1392 
1393 	/* ask the generic vaccess to advice on security */
1394 	return vaccess(vp->v_type,
1395 			vap.va_mode, vap.va_uid, vap.va_gid,
1396 			mode, cred);
1397 }
1398 
1399 /* --------------------------------------------------------------------- */
1400 
1401 int
1402 udf_create(void *v)
1403 {
1404 	struct vop_create_args /* {
1405 		struct vnode *a_dvp;
1406 		struct vnode **a_vpp;
1407 		struct componentname *a_cnp;
1408 		struct vattr *a_vap;
1409 	} */ *ap = v;
1410 	struct vnode  *dvp = ap->a_dvp;
1411 	struct vnode **vpp = ap->a_vpp;
1412 	struct vattr  *vap  = ap->a_vap;
1413 	struct componentname *cnp = ap->a_cnp;
1414 	int error;
1415 
1416 	DPRINTF(CALL, ("udf_create called\n"));
1417 	error = udf_create_node(dvp, vpp, vap, cnp);
1418 
1419 	if (error || !(cnp->cn_flags & SAVESTART))
1420 		PNBUF_PUT(cnp->cn_pnbuf);
1421 	vput(dvp);
1422 	return error;
1423 }
1424 
1425 /* --------------------------------------------------------------------- */
1426 
1427 int
1428 udf_mknod(void *v)
1429 {
1430 	struct vop_mknod_args /* {
1431 		struct vnode *a_dvp;
1432 		struct vnode **a_vpp;
1433 		struct componentname *a_cnp;
1434 		struct vattr *a_vap;
1435 	} */ *ap = v;
1436 	struct vnode  *dvp = ap->a_dvp;
1437 	struct vnode **vpp = ap->a_vpp;
1438 	struct vattr  *vap  = ap->a_vap;
1439 	struct componentname *cnp = ap->a_cnp;
1440 	int error;
1441 
1442 	DPRINTF(CALL, ("udf_mknod called\n"));
1443 	error = udf_create_node(dvp, vpp, vap, cnp);
1444 
1445 	if (error || !(cnp->cn_flags & SAVESTART))
1446 		PNBUF_PUT(cnp->cn_pnbuf);
1447 	vput(dvp);
1448 	return error;
1449 }
1450 
1451 /* --------------------------------------------------------------------- */
1452 
1453 int
1454 udf_mkdir(void *v)
1455 {
1456 	struct vop_mkdir_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_mkdir called\n"));
1469 	error = udf_create_node(dvp, vpp, vap, cnp);
1470 
1471 	if (error || !(cnp->cn_flags & SAVESTART))
1472 		PNBUF_PUT(cnp->cn_pnbuf);
1473 	vput(dvp);
1474 	return error;
1475 }
1476 
1477 /* --------------------------------------------------------------------- */
1478 
1479 static int
1480 udf_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
1481 {
1482 	struct udf_node *udf_node, *dir_node;
1483 	struct vattr vap;
1484 	int error;
1485 
1486 	DPRINTF(CALL, ("udf_link called\n"));
1487 	error = 0;
1488 
1489 	/* some quick checks */
1490 	if (vp->v_type == VDIR)
1491 		return EPERM;		/* can't link a directory */
1492 	if (dvp->v_mount != vp->v_mount)
1493 		return EXDEV;		/* can't link across devices */
1494 	if (dvp == vp)
1495 		return EPERM;		/* can't be the same */
1496 
1497 	/* lock node */
1498 	error = vn_lock(vp, LK_EXCLUSIVE);
1499 	if (error)
1500 		return error;
1501 
1502 	/* get attributes */
1503 	dir_node = VTOI(dvp);
1504 	udf_node = VTOI(vp);
1505 
1506 	error = VOP_GETATTR(vp, &vap, FSCRED);
1507 	if (error)
1508 		return error;
1509 
1510 	/* check link count overflow */
1511 	if (vap.va_nlink >= (1<<16)-1)	/* uint16_t */
1512 		return EMLINK;
1513 
1514 	return udf_dir_attach(dir_node->ump, dir_node, udf_node, &vap, cnp);
1515 }
1516 
1517 int
1518 udf_link(void *v)
1519 {
1520 	struct vop_link_args /* {
1521 		struct vnode *a_dvp;
1522 		struct vnode *a_vp;
1523 		struct componentname *a_cnp;
1524 	} */ *ap = v;
1525 	struct vnode *dvp = ap->a_dvp;
1526 	struct vnode *vp  = ap->a_vp;
1527 	struct componentname *cnp = ap->a_cnp;
1528 	int error;
1529 
1530 	error = udf_do_link(dvp, vp, cnp);
1531 	if (error)
1532 		VOP_ABORTOP(dvp, cnp);
1533 
1534 	if ((vp != dvp) && (VOP_ISLOCKED(vp) == LK_EXCLUSIVE))
1535 		VOP_UNLOCK(vp, 0);
1536 
1537 	VN_KNOTE(vp, NOTE_LINK);
1538 	VN_KNOTE(dvp, NOTE_WRITE);
1539 	vput(dvp);
1540 
1541 	return error;
1542 }
1543 
1544 /* --------------------------------------------------------------------- */
1545 
1546 static int
1547 udf_do_symlink(struct udf_node *udf_node, char *target)
1548 {
1549 	struct pathcomp pathcomp;
1550 	uint8_t *pathbuf, *pathpos, *compnamepos;
1551 	char *mntonname;
1552 	int pathlen, len, compnamelen, mntonnamelen;
1553 	int error;
1554 
1555 	/* process `target' to an UDF structure */
1556 	pathbuf = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
1557 	pathpos = pathbuf;
1558 	pathlen = 0;
1559 
1560 	if (*target == '/') {
1561 		/* symlink starts from the root */
1562 		len = UDF_PATH_COMP_SIZE;
1563 		memset(&pathcomp, 0, len);
1564 		pathcomp.type = UDF_PATH_COMP_ROOT;
1565 
1566 		/* check if its mount-point relative! */
1567 		mntonname    = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
1568 		mntonnamelen = strlen(mntonname);
1569 		if (strlen(target) >= mntonnamelen) {
1570 			if (strncmp(target, mntonname, mntonnamelen) == 0) {
1571 				pathcomp.type = UDF_PATH_COMP_MOUNTROOT;
1572 				target += mntonnamelen;
1573 			}
1574 		} else {
1575 			target++;
1576 		}
1577 
1578 		memcpy(pathpos, &pathcomp, len);
1579 		pathpos += len;
1580 		pathlen += len;
1581 	}
1582 
1583 	error = 0;
1584 	while (*target) {
1585 		/* ignore multiple '/' */
1586 		while (*target == '/') {
1587 			target++;
1588 		}
1589 		if (!*target)
1590 			break;
1591 
1592 		/* extract component name */
1593 		compnamelen = 0;
1594 		compnamepos = target;
1595 		while ((*target) && (*target != '/')) {
1596 			target++;
1597 			compnamelen++;
1598 		}
1599 
1600 		/* just trunc if too long ?? (security issue) */
1601 		if (compnamelen >= 127) {
1602 			error = ENAMETOOLONG;
1603 			break;
1604 		}
1605 
1606 		/* convert unix name to UDF name */
1607 		len = sizeof(struct pathcomp);
1608 		memset(&pathcomp, 0, len);
1609 		pathcomp.type = UDF_PATH_COMP_NAME;
1610 		len = UDF_PATH_COMP_SIZE;
1611 
1612 		if ((compnamelen == 2) && (strncmp(compnamepos, "..", 2) == 0))
1613 			pathcomp.type = UDF_PATH_COMP_PARENTDIR;
1614 		if ((compnamelen == 1) && (*compnamepos == '.'))
1615 			pathcomp.type = UDF_PATH_COMP_CURDIR;
1616 
1617 		if (pathcomp.type == UDF_PATH_COMP_NAME) {
1618 			unix_to_udf_name(
1619 				(char *) &pathcomp.ident, &pathcomp.l_ci,
1620 				compnamepos, compnamelen,
1621 				&udf_node->ump->logical_vol->desc_charset);
1622 			len = UDF_PATH_COMP_SIZE + pathcomp.l_ci;
1623 		}
1624 
1625 		if (pathlen + len >= UDF_SYMLINKBUFLEN) {
1626 			error = ENAMETOOLONG;
1627 			break;
1628 		}
1629 
1630 		memcpy(pathpos, &pathcomp, len);
1631 		pathpos += len;
1632 		pathlen += len;
1633 	}
1634 
1635 	if (error) {
1636 		/* aparently too big */
1637 		free(pathbuf, M_UDFTEMP);
1638 		return error;
1639 	}
1640 
1641 	error = udf_grow_node(udf_node, pathlen);
1642 	if (error) {
1643 		/* failed to pregrow node */
1644 		free(pathbuf, M_UDFTEMP);
1645 		return error;
1646 	}
1647 
1648 	/* write out structure on the new file */
1649 	error = vn_rdwr(UIO_WRITE, udf_node->vnode,
1650 		pathbuf, pathlen, 0,
1651 		UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
1652 		FSCRED, NULL, NULL);
1653 	if (error) {
1654 		/* failed to write out symlink contents */
1655 		free(pathbuf, M_UDFTEMP);
1656 		return error;
1657 	}
1658 
1659 	return 0;
1660 }
1661 
1662 
1663 int
1664 udf_symlink(void *v)
1665 {
1666 	struct vop_symlink_args /* {
1667 		struct vnode *a_dvp;
1668 		struct vnode **a_vpp;
1669 		struct componentname *a_cnp;
1670 		struct vattr *a_vap;
1671 		char *a_target;
1672 	} */ *ap = v;
1673 	struct vnode  *dvp = ap->a_dvp;
1674 	struct vnode **vpp = ap->a_vpp;
1675 	struct vattr  *vap  = ap->a_vap;
1676 	struct componentname *cnp = ap->a_cnp;
1677 	struct udf_node *dir_node;
1678 	struct udf_node *udf_node;
1679 	int error;
1680 
1681 	DPRINTF(CALL, ("udf_symlink called\n"));
1682 	DPRINTF(CALL, ("\tlinking to `%s`\n",  ap->a_target));
1683 	error = udf_create_node(dvp, vpp, vap, cnp);
1684 	KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL))));
1685 	if (!error) {
1686 		dir_node = VTOI(dvp);
1687 		udf_node = VTOI(*vpp);
1688 		KASSERT(udf_node);
1689 		error = udf_do_symlink(udf_node, ap->a_target);
1690 		if (error) {
1691 			/* remove node */
1692 			udf_shrink_node(udf_node, 0);
1693 			udf_dir_detach(udf_node->ump, dir_node, udf_node, cnp);
1694 		}
1695 	}
1696 	if (error || !(cnp->cn_flags & SAVESTART))
1697 		PNBUF_PUT(cnp->cn_pnbuf);
1698 	vput(dvp);
1699 	return error;
1700 }
1701 
1702 /* --------------------------------------------------------------------- */
1703 
1704 int
1705 udf_readlink(void *v)
1706 {
1707 	struct vop_readlink_args /* {
1708 		struct vnode *a_vp;
1709 		struct uio *a_uio;
1710 		kauth_cred_t a_cred;
1711 	} */ *ap = v;
1712 	struct vnode *vp = ap->a_vp;
1713 	struct uio *uio = ap->a_uio;
1714 	kauth_cred_t cred = ap->a_cred;
1715 	struct udf_node *udf_node;
1716 	struct pathcomp pathcomp;
1717 	struct vattr vattr;
1718 	uint8_t *pathbuf, *targetbuf, *tmpname;
1719 	uint8_t *pathpos, *targetpos;
1720 	char *mntonname;
1721 	int pathlen, targetlen, namelen, mntonnamelen, len, l_ci;
1722 	int first, error;
1723 
1724 	DPRINTF(CALL, ("udf_readlink called\n"));
1725 
1726 	udf_node = VTOI(vp);
1727 	error = VOP_GETATTR(vp, &vattr, cred);
1728 	if (error)
1729 		return error;
1730 
1731 	/* claim temporary buffers for translation */
1732 	pathbuf   = malloc(UDF_SYMLINKBUFLEN, M_UDFTEMP, M_WAITOK);
1733 	targetbuf = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
1734 	tmpname   = malloc(PATH_MAX+1, M_UDFTEMP, M_WAITOK);
1735 	memset(pathbuf, 0, UDF_SYMLINKBUFLEN);
1736 	memset(targetbuf, 0, PATH_MAX);
1737 
1738 	/* read contents of file in our temporary buffer */
1739 	error = vn_rdwr(UIO_READ, udf_node->vnode,
1740 		pathbuf, vattr.va_size, 0,
1741 		UIO_SYSSPACE, IO_NODELOCKED | IO_ALTSEMANTICS,
1742 		FSCRED, NULL, NULL);
1743 	if (error) {
1744 		/* failed to read in symlink contents */
1745 		free(pathbuf, M_UDFTEMP);
1746 		free(targetbuf, M_UDFTEMP);
1747 		free(tmpname, M_UDFTEMP);
1748 		return error;
1749 	}
1750 
1751 	/* convert to a unix path */
1752 	pathpos   = pathbuf;
1753 	pathlen   = 0;
1754 	targetpos = targetbuf;
1755 	targetlen = PATH_MAX;
1756 	mntonname    = udf_node->ump->vfs_mountp->mnt_stat.f_mntonname;
1757 	mntonnamelen = strlen(mntonname);
1758 
1759 	error = 0;
1760 	first = 1;
1761 	while (vattr.va_size - pathlen >= UDF_PATH_COMP_SIZE) {
1762 		len = UDF_PATH_COMP_SIZE;
1763 		memcpy(&pathcomp, pathpos, len);
1764 		l_ci = pathcomp.l_ci;
1765 		switch (pathcomp.type) {
1766 		case UDF_PATH_COMP_ROOT :
1767 			/* XXX should check for l_ci; bugcompatible now */
1768 			if ((targetlen < 1) || !first) {
1769 				error = EINVAL;
1770 				break;
1771 			}
1772 			*targetpos++ = '/'; targetlen--;
1773 			break;
1774 		case UDF_PATH_COMP_MOUNTROOT :
1775 			/* XXX what should it be if l_ci > 0 ? [4/48.16.1.2] */
1776 			if (l_ci || (targetlen < mntonnamelen+1) || !first) {
1777 				error = EINVAL;
1778 				break;
1779 			}
1780 			memcpy(targetpos, mntonname, mntonnamelen);
1781 			targetpos += mntonnamelen; targetlen -= mntonnamelen;
1782 			if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
1783 				/* more follows, so must be directory */
1784 				*targetpos++ = '/'; targetlen--;
1785 			}
1786 			break;
1787 		case UDF_PATH_COMP_PARENTDIR :
1788 			/* XXX should check for l_ci; bugcompatible now */
1789 			if (targetlen < 3) {
1790 				error = EINVAL;
1791 				break;
1792 			}
1793 			*targetpos++ = '.'; targetlen--;
1794 			*targetpos++ = '.'; targetlen--;
1795 			*targetpos++ = '/'; targetlen--;
1796 			break;
1797 		case UDF_PATH_COMP_CURDIR :
1798 			/* XXX should check for l_ci; bugcompatible now */
1799 			if (targetlen < 2) {
1800 				error = EINVAL;
1801 				break;
1802 			}
1803 			*targetpos++ = '.'; targetlen--;
1804 			*targetpos++ = '/'; targetlen--;
1805 			break;
1806 		case UDF_PATH_COMP_NAME :
1807 			if (l_ci == 0) {
1808 				error = EINVAL;
1809 				break;
1810 			}
1811 			memset(tmpname, 0, PATH_MAX);
1812 			memcpy(&pathcomp, pathpos, len + l_ci);
1813 			udf_to_unix_name(tmpname, MAXPATHLEN,
1814 				pathcomp.ident, l_ci,
1815 				&udf_node->ump->logical_vol->desc_charset);
1816 			namelen = strlen(tmpname);
1817 			if (targetlen < namelen + 1) {
1818 				error = EINVAL;
1819 				break;
1820 			}
1821 			memcpy(targetpos, tmpname, namelen);
1822 			targetpos += namelen; targetlen -= namelen;
1823 			if (vattr.va_size-pathlen > UDF_PATH_COMP_SIZE+l_ci) {
1824 				/* more follows, so must be directory */
1825 				*targetpos++ = '/'; targetlen--;
1826 			}
1827 			break;
1828 		default :
1829 			error = EINVAL;
1830 			break;
1831 		}
1832 		first = 0;
1833 		if (error)
1834 			break;
1835 		pathpos += UDF_PATH_COMP_SIZE + l_ci;
1836 		pathlen += UDF_PATH_COMP_SIZE + l_ci;
1837 
1838 	}
1839 	/* all processed? */
1840 	if (vattr.va_size - pathlen > 0)
1841 		error = EINVAL;
1842 
1843 	/* uiomove() to destination */
1844 	if (!error)
1845 		uiomove(targetbuf, PATH_MAX - targetlen, uio);
1846 
1847 	free(pathbuf, M_UDFTEMP);
1848 	free(targetbuf, M_UDFTEMP);
1849 	free(tmpname, M_UDFTEMP);
1850 
1851 	return error;
1852 }
1853 
1854 /* --------------------------------------------------------------------- */
1855 
1856 /* note: i tried to follow the logics of the tmpfs rename code */
1857 int
1858 udf_rename(void *v)
1859 {
1860 	struct vop_rename_args /* {
1861 		struct vnode *a_fdvp;
1862 		struct vnode *a_fvp;
1863 		struct componentname *a_fcnp;
1864 		struct vnode *a_tdvp;
1865 		struct vnode *a_tvp;
1866 		struct componentname *a_tcnp;
1867 	} */ *ap = v;
1868 	struct vnode *tvp = ap->a_tvp;
1869 	struct vnode *tdvp = ap->a_tdvp;
1870 	struct vnode *fvp = ap->a_fvp;
1871 	struct vnode *fdvp = ap->a_fdvp;
1872 	struct componentname *tcnp = ap->a_tcnp;
1873 	struct componentname *fcnp = ap->a_fcnp;
1874 	struct udf_node *fnode, *fdnode, *tnode, *tdnode;
1875 	struct vattr fvap, tvap;
1876 	int error;
1877 
1878 	DPRINTF(CALL, ("udf_rename called\n"));
1879 
1880 	/* disallow cross-device renames */
1881 	if (fvp->v_mount != tdvp->v_mount ||
1882 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1883 		error = EXDEV;
1884 		goto out_unlocked;
1885 	}
1886 
1887 	fnode  = VTOI(fvp);
1888 	fdnode = VTOI(fdvp);
1889 	tnode  = (tvp == NULL) ? NULL : VTOI(tvp);
1890 	tdnode = VTOI(tdvp);
1891 
1892 	/* lock our source dir */
1893 	if (fdnode != tdnode) {
1894 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1895 		if (error != 0)
1896 			goto out_unlocked;
1897 	}
1898 
1899 	/* get info about the node to be moved */
1900 	error = VOP_GETATTR(fvp, &fvap, FSCRED);
1901 	KASSERT(error == 0);
1902 
1903 	/* check when to delete the old already existing entry */
1904 	if (tvp) {
1905 		/* get info about the node to be moved to */
1906 		error = VOP_GETATTR(fvp, &tvap, FSCRED);
1907 		KASSERT(error == 0);
1908 
1909 		/* if both dirs, make sure the destination is empty */
1910 		if (fvp->v_type == VDIR && tvp->v_type == VDIR) {
1911 			if (tvap.va_nlink > 2) {
1912 				error = ENOTEMPTY;
1913 				goto out;
1914 			}
1915 		}
1916 		/* if moving dir, make sure destination is dir too */
1917 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1918 			error = ENOTDIR;
1919 			goto out;
1920 		}
1921 		/* if we're moving a non-directory, make sure dest is no dir */
1922 		if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1923 			error = EISDIR;
1924 			goto out;
1925 		}
1926 	}
1927 
1928 	/* dont allow renaming directories acros directory for now */
1929 	if (fdnode != tdnode) {
1930 		if (fvp->v_type == VDIR) {
1931 			error = EINVAL;
1932 			goto out;
1933 		}
1934 	}
1935 
1936 	/* remove existing entry if present */
1937 	if (tvp)
1938 		udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
1939 
1940 	/* create new directory entry for the node */
1941 	error = udf_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
1942 	if (error)
1943 		goto out;
1944 
1945 	/* unlink old directory entry for the node, if failing, unattach new */
1946 	error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
1947 	if (error)
1948 		udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
1949 
1950 out:
1951         if (fdnode != tdnode)
1952                 VOP_UNLOCK(fdvp, 0);
1953 
1954 out_unlocked:
1955 	VOP_ABORTOP(tdvp, tcnp);
1956 	if (tdvp == tvp)
1957 		vrele(tdvp);
1958 	else
1959 		vput(tdvp);
1960 	if (tvp)
1961 		vput(tvp);
1962 	VOP_ABORTOP(fdvp, fcnp);
1963 
1964 	/* release source nodes. */
1965 	vrele(fdvp);
1966 	vrele(fvp);
1967 
1968 	return error;
1969 }
1970 
1971 /* --------------------------------------------------------------------- */
1972 
1973 int
1974 udf_remove(void *v)
1975 {
1976 	struct vop_remove_args /* {
1977 		struct vnode *a_dvp;
1978 		struct vnode *a_vp;
1979 		struct componentname *a_cnp;
1980 	} */ *ap = v;
1981 	struct vnode *dvp = ap->a_dvp;
1982 	struct vnode *vp  = ap->a_vp;
1983 	struct componentname *cnp = ap->a_cnp;
1984 	struct udf_node *dir_node = VTOI(dvp);;
1985 	struct udf_node *udf_node = VTOI(vp);
1986 	struct udf_mount *ump = dir_node->ump;
1987 	int error;
1988 
1989 	DPRINTF(CALL, ("udf_remove called\n"));
1990 	if (vp->v_type != VDIR) {
1991 		error = udf_dir_detach(ump, dir_node, udf_node, cnp);
1992 		DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
1993 	} else {
1994 		DPRINTF(NODE, ("\tis a directory: perm. denied\n"));
1995 		error = EPERM;
1996 	}
1997 
1998 	if (error == 0) {
1999 		VN_KNOTE(vp, NOTE_DELETE);
2000 		VN_KNOTE(dvp, NOTE_WRITE);
2001 	}
2002 
2003 	if (dvp == vp)
2004 		vrele(vp);
2005 	else
2006 		vput(vp);
2007 	vput(dvp);
2008 
2009 	return error;
2010 }
2011 
2012 /* --------------------------------------------------------------------- */
2013 
2014 int
2015 udf_rmdir(void *v)
2016 {
2017 	struct vop_rmdir_args /* {
2018 		struct vnode *a_dvp;
2019 		struct vnode *a_vp;
2020 		struct componentname *a_cnp;
2021 	} */ *ap = v;
2022 	struct vnode *vp = ap->a_vp;
2023 	struct vnode *dvp = ap->a_dvp;
2024 	struct componentname *cnp = ap->a_cnp;
2025 	struct udf_node *dir_node = VTOI(dvp);;
2026 	struct udf_node *udf_node = VTOI(vp);
2027 	struct udf_mount *ump = dir_node->ump;
2028 	int refcnt, error;
2029 
2030 	DPRINTF(NOTIMPL, ("udf_rmdir called\n"));
2031 
2032 	/* don't allow '.' to be deleted */
2033 	if (dir_node == udf_node) {
2034 		vrele(dvp);
2035 		vput(vp);
2036 		return EINVAL;
2037 	}
2038 
2039 	/* check to see if the directory is empty */
2040 	error = 0;
2041 	if (dir_node->fe) {
2042 		refcnt = udf_rw16(udf_node->fe->link_cnt);
2043 	} else {
2044 		refcnt = udf_rw16(udf_node->efe->link_cnt);
2045 	}
2046 	if (refcnt > 1) {
2047 		/* NOT empty */
2048 		vput(dvp);
2049 		vput(vp);
2050 		return ENOTEMPTY;
2051 	}
2052 
2053 	/* detach the node from the directory */
2054 	error = udf_dir_detach(ump, dir_node, udf_node, cnp);
2055 	if (error == 0) {
2056 		cache_purge(vp);
2057 //		cache_purge(dvp);	/* XXX from msdosfs, why? */
2058 		VN_KNOTE(vp, NOTE_DELETE);
2059 	}
2060 	DPRINTFIF(NODE, error, ("\tgot error removing file\n"));
2061 
2062 	/* unput the nodes and exit */
2063 	vput(dvp);
2064 	vput(vp);
2065 
2066 	return error;
2067 }
2068 
2069 /* --------------------------------------------------------------------- */
2070 
2071 int
2072 udf_fsync(void *v)
2073 {
2074 	struct vop_fsync_args /* {
2075 		struct vnode *a_vp;
2076 		kauth_cred_t a_cred;
2077 		int a_flags;
2078 		off_t offlo;
2079 		off_t offhi;
2080 		struct proc *a_p;
2081 	} */ *ap = v;
2082 	struct vnode *vp = ap->a_vp;
2083 	struct udf_node *udf_node = VTOI(vp);
2084 	int error, flags, wait;
2085 
2086 	DPRINTF(STRATEGY, ("udf_fsync called : %s, %s\n",
2087 		(ap->a_flags & FSYNC_WAIT)     ? "wait":"no wait",
2088 		(ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete"));
2089 
2090 	/* flush data and wait for it when requested */
2091 	wait = (ap->a_flags & FSYNC_WAIT) ? UPDATE_WAIT : 0;
2092 	vflushbuf(vp, wait);
2093 
2094 	if (udf_node == NULL) {
2095 		printf("udf_fsync() called on NULL udf_node!\n");
2096 		return 0;
2097 	}
2098 	if (vp->v_tag != VT_UDF) {
2099 		printf("udf_fsync() called on node not tagged as UDF node!\n");
2100 		return 0;
2101 	}
2102 
2103 	/* set our times */
2104 	udf_itimes(udf_node, NULL, NULL, NULL);
2105 
2106 	/* if called when mounted readonly, never write back */
2107 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
2108 		return 0;
2109 
2110 	/* if only data is requested, return */
2111 	if (ap->a_flags & FSYNC_DATAONLY)
2112 		return 0;
2113 
2114 	/* check if the node is dirty 'enough'*/
2115 	flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
2116 	if (flags == 0)
2117 		return 0;
2118 
2119 	/* if we don't have to wait, check for IO pending */
2120 	if (!wait) {
2121 		if (vp->v_numoutput > 0) {
2122 			DPRINTF(NODE, ("udf_fsync: rejecting on v_numoutput\n"));
2123 			return 0;
2124 		}
2125 		if (udf_node->outstanding_bufs > 0) {
2126 			DPRINTF(NODE, ("udf_fsync: rejecting on outstanding_bufs\n"));
2127 			return 0;
2128 		}
2129 		if (udf_node->outstanding_nodedscr > 0) {
2130 			DPRINTF(NODE, ("udf_fsync: rejecting on outstanding_nodedscr\n"));
2131 			return 0;
2132 		}
2133 	}
2134 
2135 	/* wait until vp->v_numoutput reaches zero i.e. is finished */
2136 	if (wait) {
2137 		DPRINTF(SYNC, ("udf_fsync, waiting\n"));
2138 		mutex_enter(&vp->v_interlock);
2139 		while (vp->v_numoutput) {
2140 			cv_timedwait(&vp->v_cv, &vp->v_interlock, hz/8);
2141 		}
2142 		mutex_exit(&vp->v_interlock);
2143 		DPRINTF(SYNC, ("udf_fsync: fin wait\n"));
2144 	}
2145 
2146 	/* write out node and wait for it if requested */
2147 	error = udf_writeout_node(udf_node, wait);
2148 	if (error)
2149 		return error;
2150 
2151 	/* TODO/XXX if ap->a_flags & FSYNC_CACHE, we ought to do a disc sync */
2152 
2153 	return 0;
2154 }
2155 
2156 /* --------------------------------------------------------------------- */
2157 
2158 int
2159 udf_advlock(void *v)
2160 {
2161 	struct vop_advlock_args /* {
2162 		struct vnode *a_vp;
2163 		void *a_id;
2164 		int a_op;
2165 		struct flock *a_fl;
2166 		int a_flags;
2167 	} */ *ap = v;
2168 	struct vnode *vp = ap->a_vp;
2169 	struct udf_node *udf_node = VTOI(vp);
2170 	struct file_entry    *fe;
2171 	struct extfile_entry *efe;
2172 	uint64_t file_size;
2173 
2174 	DPRINTF(LOCKING, ("udf_advlock called\n"));
2175 
2176 	/* get directory filesize */
2177 	if (udf_node->fe) {
2178 		fe = udf_node->fe;
2179 		file_size = udf_rw64(fe->inf_len);
2180 	} else {
2181 		assert(udf_node->efe);
2182 		efe = udf_node->efe;
2183 		file_size = udf_rw64(efe->inf_len);
2184 	}
2185 
2186 	return lf_advlock(ap, &udf_node->lockf, file_size);
2187 }
2188 
2189 /* --------------------------------------------------------------------- */
2190 
2191 
2192 /* Global vfs vnode data structures for udfs */
2193 int (**udf_vnodeop_p) __P((void *));
2194 
2195 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
2196 	{ &vop_default_desc, vn_default_error },
2197 	{ &vop_lookup_desc, udf_lookup },	/* lookup */
2198 	{ &vop_create_desc, udf_create },	/* create */
2199 	{ &vop_mknod_desc, udf_mknod },		/* mknod */	/* TODO */
2200 	{ &vop_open_desc, udf_open },		/* open */
2201 	{ &vop_close_desc, udf_close },		/* close */
2202 	{ &vop_access_desc, udf_access },	/* access */
2203 	{ &vop_getattr_desc, udf_getattr },	/* getattr */
2204 	{ &vop_setattr_desc, udf_setattr },	/* setattr */	/* TODO chflags */
2205 	{ &vop_read_desc, udf_read },		/* read */
2206 	{ &vop_write_desc, udf_write },		/* write */	/* WRITE */
2207 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
2208 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
2209 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
2210 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
2211 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
2212 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
2213 	{ &vop_fsync_desc, udf_fsync },		/* fsync */
2214 	{ &vop_seek_desc, genfs_seek },		/* seek */
2215 	{ &vop_remove_desc, udf_remove },	/* remove */
2216 	{ &vop_link_desc, udf_link },		/* link */	/* TODO */
2217 	{ &vop_rename_desc, udf_rename },	/* rename */ 	/* TODO */
2218 	{ &vop_mkdir_desc, udf_mkdir },		/* mkdir */
2219 	{ &vop_rmdir_desc, udf_rmdir },		/* rmdir */
2220 	{ &vop_symlink_desc, udf_symlink },	/* symlink */	/* TODO */
2221 	{ &vop_readdir_desc, udf_readdir },	/* readdir */
2222 	{ &vop_readlink_desc, udf_readlink },	/* readlink */	/* TEST ME */
2223 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
2224 	{ &vop_inactive_desc, udf_inactive },	/* inactive */
2225 	{ &vop_reclaim_desc, udf_reclaim },	/* reclaim */
2226 	{ &vop_lock_desc, genfs_lock },		/* lock */
2227 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
2228 	{ &vop_bmap_desc, udf_trivial_bmap },	/* bmap */	/* 1:1 bmap */
2229 	{ &vop_strategy_desc, udf_vfsstrategy },/* strategy */
2230 /*	{ &vop_print_desc, udf_print },	*/	/* print */
2231 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
2232 	{ &vop_pathconf_desc, udf_pathconf },	/* pathconf */
2233 	{ &vop_advlock_desc, udf_advlock },	/* advlock */	/* TEST ME */
2234 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
2235 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
2236 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
2237 	{ NULL, NULL }
2238 };
2239 
2240 
2241 const struct vnodeopv_desc udf_vnodeop_opv_desc = {
2242 	&udf_vnodeop_p, udf_vnodeop_entries
2243 };
2244 
2245