xref: /netbsd-src/sys/fs/udf/udf_vnops.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /* $NetBSD: udf_vnops.c,v 1.17 2008/01/25 14:32:14 ad Exp $ */
2 
3 /*
4  * Copyright (c) 2006 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *          This product includes software developed for the
18  *          NetBSD Project.  See http://www.NetBSD.org/ for
19  *          information about NetBSD.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __KERNEL_RCSID(0, "$NetBSD: udf_vnops.c,v 1.17 2008/01/25 14:32:14 ad Exp $");
40 #endif /* not lint */
41 
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/resourcevar.h>	/* defines plimit structure in proc struct */
47 #include <sys/kernel.h>
48 #include <sys/file.h>		/* define FWRITE ... */
49 #include <sys/stat.h>
50 #include <sys/buf.h>
51 #include <sys/proc.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/signalvar.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57 #include <sys/lockf.h>
58 
59 #include <miscfs/genfs/genfs.h>
60 #include <uvm/uvm_extern.h>
61 
62 #include <fs/udf/ecma167-udf.h>
63 #include <fs/udf/udf_mount.h>
64 #include "udf.h"
65 #include "udf_subr.h"
66 #include "udf_bswap.h"
67 
68 
69 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
70 
71 
72 /* externs */
73 extern int prtactive;
74 
75 
76 /* implementations of vnode functions; table follows at end */
77 /* --------------------------------------------------------------------- */
78 
79 int
80 udf_inactive(void *v)
81 {
82 	struct vop_inactive_args /* {
83 		struct vnode *a_vp;
84 		struct proc *a_p;
85 	} */ *ap = v;
86 	struct vnode *vp = ap->a_vp;
87 
88 	VOP_UNLOCK(vp, 0);
89 
90 	DPRINTF(LOCKING, ("udf_inactive called for node %p\n", VTOI(vp)));
91 
92 	/*
93 	 * Optionally flush metadata to disc. If the file has not been
94 	 * referenced anymore in a directory we ought to free up the resources
95 	 * on disc if applicable.
96 	 */
97 
98 	return 0;
99 }
100 
101 /* --------------------------------------------------------------------- */
102 
103 int
104 udf_reclaim(void *v)
105 {
106 	struct vop_reclaim_args /* {
107 		struct vnode *a_vp;
108 	} */ *ap = v;
109 	struct vnode *vp = ap->a_vp;
110 	struct udf_node *node = VTOI(vp);
111 
112 	if (prtactive && vp->v_usecount > 1)
113 		vprint("udf_reclaim(): pushing active", vp);
114 
115 	/* purge old data from namei */
116 	cache_purge(vp);
117 
118 	DPRINTF(LOCKING, ("udf_reclaim called for node %p\n", node));
119 	/* dispose all node knowledge */
120 	udf_dispose_node(node);
121 
122 	return 0;
123 }
124 
125 /* --------------------------------------------------------------------- */
126 
127 int
128 udf_read(void *v)
129 {
130 	struct vop_read_args /* {
131 		struct vnode *a_vp;
132 		struct uio *a_uio;
133 		int a_ioflag;
134 		kauth_cred_t a_cred;
135 	} */ *ap = v;
136 	struct vnode *vp     = ap->a_vp;
137 	struct uio   *uio    = ap->a_uio;
138 	int           ioflag = ap->a_ioflag;
139 	struct uvm_object    *uobj;
140 	struct udf_node      *udf_node = VTOI(vp);
141 	struct file_entry    *fe;
142 	struct extfile_entry *efe;
143 	uint64_t file_size;
144 	vsize_t len;
145 	void *win;
146 	int error;
147 	int flags;
148 
149 	/* XXX how to deal with xtended attributes (files) */
150 
151 	DPRINTF(READ, ("udf_read called\n"));
152 
153 	/* can this happen? some filingsystems have this check */
154 	if (uio->uio_offset < 0)
155 		return EINVAL;
156 
157 	assert(udf_node);
158 	assert(udf_node->fe || udf_node->efe);
159 
160 	/* TODO set access time */
161 
162 	/* get directory filesize */
163 	if (udf_node->fe) {
164 		fe = udf_node->fe;
165 		file_size = udf_rw64(fe->inf_len);
166 	} else {
167 		assert(udf_node->efe);
168 		efe = udf_node->efe;
169 		file_size = udf_rw64(efe->inf_len);
170 	}
171 
172 	if (vp->v_type == VDIR) {
173 		/* protect against rogue programs reading raw directories */
174 		if ((ioflag & IO_ALTSEMANTICS) == 0)
175 			return EISDIR;
176 	}
177 	if (vp->v_type == VREG || vp->v_type == VDIR) {
178 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
179 
180 		/* read contents using buffercache */
181 		uobj = &vp->v_uobj;
182 		flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
183 		error = 0;
184 		while (uio->uio_resid > 0) {
185 			/* reached end? */
186 			if (file_size <= uio->uio_offset)
187 				break;
188 
189 			/* maximise length to file extremity */
190 			len = MIN(file_size - uio->uio_offset, uio->uio_resid);
191 			if (len == 0)
192 				break;
193 
194 			/* ubc, here we come, prepare to trap */
195 			win = ubc_alloc(uobj, uio->uio_offset, &len,
196 					advice, UBC_READ);
197 			error = uiomove(win, len, uio);
198 			ubc_release(win, flags);
199 			if (error)
200 				break;
201 		}
202 		/* TODO note access time */
203 		return error;
204 	}
205 
206 	return EINVAL;
207 }
208 
209 /* --------------------------------------------------------------------- */
210 
211 int
212 udf_write(void *v)
213 {
214 	struct vop_write_args /* {
215 		struct vnode *a_vp;
216 		struct uio *a_uio;
217 		int a_ioflag;
218 		kauth_cred_t a_cred;
219 	} */ *ap = v;
220 
221 	DPRINTF(NOTIMPL, ("udf_write called\n"));
222 	ap = ap;	/* shut up gcc */
223 
224 	/* TODO implement writing */
225 	return EROFS;
226 }
227 
228 
229 /* --------------------------------------------------------------------- */
230 
231 /*
232  * `Special' bmap functionality that translates all incomming requests to
233  * translate to vop_strategy() calls with the same blocknumbers effectively
234  * not translating at all.
235  */
236 
237 int
238 udf_trivial_bmap(void *v)
239 {
240 	struct vop_bmap_args /* {
241 		struct vnode *a_vp;
242 		daddr_t a_bn;
243 		struct vnode **a_vpp;
244 		daddr_t *a_bnp;
245 		int *a_runp;
246 	} */ *ap = v;
247 	struct vnode  *vp  = ap->a_vp;	/* our node	*/
248 	struct vnode **vpp = ap->a_vpp;	/* return node	*/
249 	daddr_t *bnp  = ap->a_bnp;	/* translated	*/
250 	daddr_t  bn   = ap->a_bn;	/* origional	*/
251 	int     *runp = ap->a_runp;
252 	struct udf_node *udf_node = VTOI(vp);
253 	uint32_t lb_size;
254 
255 	/* get logical block size */
256 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
257 
258 	/* could return `-1' to indicate holes/zeros */
259 	/* translate 1:1 */
260 	*bnp = bn;
261 
262 	/* set the vnode to read the data from with strategy on itself */
263 	if (vpp)
264 		*vpp = vp;
265 
266 	/* set runlength of maximum block size */
267 	if (runp)
268 		*runp = MAXPHYS / lb_size;	/* or with -1 ? */
269 
270 	/* return success */
271 	return 0;
272 }
273 
274 /* --------------------------------------------------------------------- */
275 
276 int
277 udf_strategy(void *v)
278 {
279 	struct vop_strategy_args /* {
280 		struct vnode *a_vp;
281 		struct buf *a_bp;
282 	} */ *ap = v;
283 	struct vnode *vp = ap->a_vp;
284 	struct buf   *bp = ap->a_bp;
285 	struct udf_node *udf_node = VTOI(vp);
286 	uint32_t lb_size, from, sectors;
287 	int error;
288 
289 	DPRINTF(STRATEGY, ("udf_strategy called\n"));
290 
291 	/* check if we ought to be here */
292 	if (vp->v_type == VBLK || vp->v_type == VCHR)
293 		panic("udf_strategy: spec");
294 
295 	/* only filebuffers ought to be read by this, no descriptors */
296 	assert(bp->b_blkno >= 0);
297 
298 	/* get sector size */
299 	lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
300 
301 	/* calculate sector to start from */
302 	from = bp->b_blkno;
303 
304 	/* calculate length to fetch/store in sectors */
305 	sectors = bp->b_bcount / lb_size;
306 	assert(bp->b_bcount > 0);
307 
308 	/* NEVER assume later that this buffer is already translated */
309 	/* bp->b_lblkno = bp->b_blkno; */
310 
311 	DPRINTF(STRATEGY, ("\tread vp %p buf %p (blk no %"PRIu64")"
312 	    ", sector %d for %d sectors\n",
313 	    vp, bp, bp->b_blkno, from, sectors));
314 
315 	/* check assertions: we OUGHT to always get multiples of this */
316 	assert(sectors * lb_size == bp->b_bcount);
317 
318 	/* determine mode */
319 	error = 0;
320 	if (bp->b_flags & B_READ) {
321 		/* read buffer from the udf_node, translate vtop on the way*/
322 		udf_read_filebuf(udf_node, bp);
323 		return bp->b_error;
324 	}
325 
326 	printf("udf_strategy: can't write yet\n");
327 	return ENOTSUP;
328 }
329 
330 /* --------------------------------------------------------------------- */
331 
332 int
333 udf_readdir(void *v)
334 {
335 	struct vop_readdir_args /* {
336 		struct vnode *a_vp;
337 		struct uio *a_uio;
338 		kauth_cred_t a_cred;
339 		int *a_eofflag;
340 		off_t **a_cookies;
341 		int *a_ncookies;
342 	} */ *ap = v;
343 	struct uio *uio = ap->a_uio;
344 	struct vnode *vp = ap->a_vp;
345 	struct udf_node *udf_node = VTOI(vp);
346 	struct file_entry    *fe;
347 	struct extfile_entry *efe;
348 	struct fileid_desc *fid;
349 	struct dirent *dirent;
350 	uint64_t file_size, diroffset, transoffset;
351 	uint32_t lb_size;
352 	int error;
353 
354 	DPRINTF(READDIR, ("udf_readdir called\n"));
355 
356 	/* This operation only makes sense on directory nodes. */
357 	if (vp->v_type != VDIR)
358 		return ENOTDIR;
359 
360 	/* get directory filesize */
361 	if (udf_node->fe) {
362 		fe = udf_node->fe;
363 		file_size = udf_rw64(fe->inf_len);
364 	} else {
365 		assert(udf_node->efe);
366 		efe = udf_node->efe;
367 		file_size = udf_rw64(efe->inf_len);
368 	}
369 
370 	dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK | M_ZERO);
371 
372 	/*
373 	 * Add `.' pseudo entry if at offset zero since its not in the fid
374 	 * stream
375 	 */
376 	if (uio->uio_offset == 0) {
377 		DPRINTF(READDIR, ("\t'.' inserted\n"));
378 		strcpy(dirent->d_name, ".");
379 		dirent->d_fileno = udf_calchash(&udf_node->loc);
380 		dirent->d_type = DT_DIR;
381 		dirent->d_namlen = strlen(dirent->d_name);
382 		dirent->d_reclen = _DIRENT_SIZE(dirent);
383 		uiomove(dirent, _DIRENT_SIZE(dirent), uio);
384 
385 		/* mark with magic value that we have done the dummy */
386 		uio->uio_offset = UDF_DIRCOOKIE_DOT;
387 	}
388 
389 	/* we are called just as long as we keep on pushing data in */
390 	error = 0;
391 	if ((uio->uio_offset < file_size) &&
392 	    (uio->uio_resid >= sizeof(struct dirent))) {
393 		/* allocate temporary space for fid */
394 		lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
395 		fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
396 
397 		if (uio->uio_offset == UDF_DIRCOOKIE_DOT)
398 			uio->uio_offset = 0;
399 
400 		diroffset   = uio->uio_offset;
401 		transoffset = diroffset;
402 		while (diroffset < file_size) {
403 			DPRINTF(READDIR, ("\tread in fid stream\n"));
404 			/* transfer a new fid/dirent */
405 			error = udf_read_fid_stream(vp, &diroffset,
406 				    fid, dirent);
407 			DPRINTFIF(READDIR, error, ("read error in read fid "
408 			    "stream : %d\n", error));
409 			if (error)
410 				break;
411 
412 			/*
413 			 * If there isn't enough space in the uio to return a
414 			 * whole dirent, break off read
415 			 */
416 			if (uio->uio_resid < _DIRENT_SIZE(dirent))
417 				break;
418 
419 			/* remember the last entry we transfered */
420 			transoffset = diroffset;
421 
422 			/* skip deleted entries */
423 			if (fid->file_char & UDF_FILE_CHAR_DEL)
424 				continue;
425 
426 			/* skip not visible files */
427 			if (fid->file_char & UDF_FILE_CHAR_VIS)
428 				continue;
429 
430 			/* copy dirent to the caller */
431 			DPRINTF(READDIR, ("\tread dirent `%s', type %d\n",
432 			    dirent->d_name, dirent->d_type));
433 			uiomove(dirent, _DIRENT_SIZE(dirent), uio);
434 		}
435 
436 		/* pass on last transfered offset */
437 		uio->uio_offset = transoffset;
438 		free(fid, M_UDFTEMP);
439 	}
440 
441 	if (ap->a_eofflag)
442 		*ap->a_eofflag = (uio->uio_offset == file_size);
443 
444 #ifdef DEBUG
445 	if (udf_verbose & UDF_DEBUG_READDIR) {
446 		printf("returning offset %d\n", (uint32_t) uio->uio_offset);
447 		if (ap->a_eofflag)
448 			printf("returning EOF ? %d\n", *ap->a_eofflag);
449 		if (error)
450 			printf("readdir returning error %d\n", error);
451 	}
452 #endif
453 
454 	free(dirent, M_UDFTEMP);
455 	return error;
456 }
457 
458 /* --------------------------------------------------------------------- */
459 
460 int
461 udf_lookup(void *v)
462 {
463 	struct vop_lookup_args /* {
464 		struct vnode *a_dvp;
465 		struct vnode **a_vpp;
466 		struct componentname *a_cnp;
467 	} */ *ap = v;
468 	struct vnode *dvp = ap->a_dvp;
469 	struct vnode **vpp = ap->a_vpp;
470 	struct componentname *cnp = ap->a_cnp;
471 	struct udf_node  *dir_node, *res_node;
472 	struct udf_mount *ump;
473 	struct long_ad    icb_loc;
474 	const char *name;
475 	int namelen, nameiop, islastcn, mounted_ro;
476 	int vnodetp;
477 	int error, found;
478 
479 	dir_node = VTOI(dvp);
480 	ump = dir_node->ump;
481 	*vpp = NULL;
482 
483 	DPRINTF(LOOKUP, ("udf_lookup called\n"));
484 
485 	/* simplify/clarification flags */
486 	nameiop     = cnp->cn_nameiop;
487 	islastcn    = cnp->cn_flags & ISLASTCN;
488 	mounted_ro  = dvp->v_mount->mnt_flag & MNT_RDONLY;
489 
490 	/* check exec/dirread permissions first */
491 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
492 	if (error)
493 		return error;
494 
495 	DPRINTF(LOOKUP, ("\taccess ok\n"));
496 
497 	/*
498 	 * If requesting a modify on the last path element on a read-only
499 	 * filingsystem, reject lookup; XXX why is this repeated in every FS ?
500 	 */
501 	if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
502 		return EROFS;
503 
504 	DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n",
505 	    cnp->cn_nameptr));
506 	/* look in the nami cache; returns 0 on success!! */
507 	error = cache_lookup(dvp, vpp, cnp);
508 	if (error >= 0)
509 		return error;
510 
511 	DPRINTF(LOOKUP, ("\tNOT found in cache\n"));
512 
513 	/*
514 	 * Obviously, the file is not (anymore) in the namecache, we have to
515 	 * search for it. There are three basic cases: '.', '..' and others.
516 	 *
517 	 * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
518 	 */
519 	error = 0;
520 	if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
521 		DPRINTF(LOOKUP, ("\tlookup '.'\n"));
522 		/* special case 1 '.' */
523 		VREF(dvp);
524 		*vpp = dvp;
525 		/* done */
526 	} else if (cnp->cn_flags & ISDOTDOT) {
527 		/* special case 2 '..' */
528 		DPRINTF(LOOKUP, ("\tlookup '..'\n"));
529 
530 		/* first unlock parent */
531 		VOP_UNLOCK(dvp, 0);
532 
533 		/* get our node */
534 		name    = "..";
535 		namelen = 2;
536 		found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc);
537 		if (!found)
538 			error = ENOENT;
539 
540 		if (!error) {
541 			DPRINTF(LOOKUP, ("\tfound '..'\n"));
542 			/* try to create/reuse the node */
543 			error = udf_get_node(ump, &icb_loc, &res_node);
544 
545 			if (!error) {
546 			DPRINTF(LOOKUP, ("\tnode retrieved/created OK\n"));
547 				*vpp = res_node->vnode;
548 			}
549 		}
550 
551 		/* try to relock parent */
552 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
553 	} else {
554 		DPRINTF(LOOKUP, ("\tlookup file\n"));
555 		/* all other files */
556 		/* lookup filename in the directory; location icb_loc */
557 		name    = cnp->cn_nameptr;
558 		namelen = cnp->cn_namelen;
559 		found = udf_lookup_name_in_dir(dvp, name, namelen, &icb_loc);
560 		if (!found) {
561 			DPRINTF(LOOKUP, ("\tNOT found\n"));
562 			/*
563 			 * UGH, didn't find name. If we're creating or naming
564 			 * on the last name this is OK and we ought to return
565 			 * EJUSTRETURN if its allowed to be created.
566 			 */
567 			error = ENOENT;
568 			if (islastcn &&
569 				(nameiop == CREATE || nameiop == RENAME))
570 					error = 0;
571 			if (!error) {
572 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
573 				if (!error) {
574 					/* keep the component name */
575 					cnp->cn_flags |= SAVENAME;
576 					error = EJUSTRETURN;
577 				}
578 			}
579 			/* done */
580 		} else {
581 			/* try to create/reuse the node */
582 			error = udf_get_node(ump, &icb_loc, &res_node);
583 			if (!error) {
584 				/*
585 				 * If we are not at the last path component
586 				 * and found a non-directory or non-link entry
587 				 * (which may itself be pointing to a
588 				 * directory), raise an error.
589 				 */
590 				vnodetp = res_node->vnode->v_type;
591 				if ((vnodetp != VDIR) && (vnodetp != VLNK)) {
592 					if (!islastcn)
593 						error = ENOTDIR;
594 				}
595 
596 			}
597 			if (!error) {
598 				*vpp = res_node->vnode;
599 			}
600 		}
601 	}
602 
603 	/*
604 	 * Store result in the cache if requested. If we are creating a file,
605 	 * the file might not be found and thus putting it into the namecache
606 	 * might be seen as negative caching.
607 	 */
608 	if (cnp->cn_flags & MAKEENTRY)
609 		cache_enter(dvp, *vpp, cnp);
610 
611 	DPRINTFIF(LOOKUP, error, ("udf_lookup returing error %d\n", error));
612 
613 	return error;
614 }
615 
616 /* --------------------------------------------------------------------- */
617 
618 int
619 udf_getattr(void *v)
620 {
621 	struct vop_getattr_args /* {
622 		struct vnode *a_vp;
623 		struct vattr *a_vap;
624 		kauth_cred_t a_cred;
625 		struct proc *a_p;
626 	} */ *ap = v;
627 	struct vnode *vp = ap->a_vp;
628 	struct udf_node *udf_node = VTOI(vp);
629 	struct udf_mount *ump = udf_node->ump;
630 	struct vattr *vap = ap->a_vap;
631 	struct file_entry *fe;
632 	struct extfile_entry *efe;
633 	struct timestamp *atime, *mtime, *attrtime;
634 	uint32_t nlink;
635 	uint64_t filesize, blkssize;
636 	uid_t uid;
637 	gid_t gid;
638 
639 	DPRINTF(CALL, ("udf_getattr called\n"));
640 
641 	/* get descriptor information */
642 	if (udf_node->fe) {
643 		fe = udf_node->fe;
644 		nlink    = udf_rw16(fe->link_cnt);
645 		uid      = (uid_t)udf_rw32(fe->uid);
646 		gid      = (gid_t)udf_rw32(fe->gid);
647 		filesize = udf_rw64(fe->inf_len);
648 		blkssize = udf_rw64(fe->logblks_rec);
649 		atime    = &fe->atime;
650 		mtime    = &fe->mtime;
651 		attrtime = &fe->attrtime;
652 	} else {
653 		assert(udf_node->efe);
654 		efe = udf_node->efe;
655 		nlink    = udf_rw16(efe->link_cnt);
656 		uid      = (uid_t)udf_rw32(efe->uid);
657 		gid      = (gid_t)udf_rw32(efe->gid);
658 		filesize = udf_rw64(efe->inf_len);	/* XXX or obj_size? */
659 		blkssize = udf_rw64(efe->logblks_rec);
660 		atime    = &efe->atime;
661 		mtime    = &efe->mtime;
662 		attrtime = &efe->attrtime;
663 	}
664 
665 	/* do the uid/gid translation game */
666 	if ((uid == (uid_t) -1) && (gid == (gid_t) -1)) {
667 		uid = ump->mount_args.anon_uid;
668 		gid = ump->mount_args.anon_gid;
669 	}
670 
671 	/* fill in struct vattr with values from the node */
672 	VATTR_NULL(vap);
673 	vap->va_type      = vp->v_type;
674 	vap->va_mode      = udf_getaccessmode(udf_node);
675 	vap->va_nlink     = nlink;
676 	vap->va_uid       = uid;
677 	vap->va_gid       = gid;
678 	vap->va_fsid      = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
679 	vap->va_fileid    = udf_calchash(&udf_node->loc);   /* inode hash XXX */
680 	vap->va_size      = filesize;
681 	vap->va_blocksize = udf_node->ump->discinfo.sector_size;  /* wise? */
682 
683 	/*
684 	 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to add
685 	 * 1 to the link count if its a directory we're requested attributes
686 	 * of.
687 	 */
688 	if (vap->va_type == VDIR)
689 		vap->va_nlink++;
690 
691 	/* access times */
692 	udf_timestamp_to_timespec(ump, atime,    &vap->va_atime);
693 	udf_timestamp_to_timespec(ump, mtime,    &vap->va_mtime);
694 	udf_timestamp_to_timespec(ump, attrtime, &vap->va_ctime);
695 
696 	vap->va_gen       = 1;		/* no multiple generations yes (!?) */
697 	vap->va_flags     = 0;		/* no flags */
698 	vap->va_rdev      = udf_node->rdev;
699 	vap->va_bytes     = blkssize * udf_node->ump->discinfo.sector_size;
700 	vap->va_filerev   = 1;		/* TODO file revision numbers? */
701 	vap->va_vaflags   = 0;		/* TODO which va_vaflags? */
702 
703 	return 0;
704 }
705 
706 /* --------------------------------------------------------------------- */
707 
708 int
709 udf_setattr(void *v)
710 {
711 	struct vop_setattr_args /* {
712 		struct vnode *a_vp;
713 		struct vattr *a_vap;
714 		kauth_cred_t a_cred;
715 		struct proc *a_p;
716 	} */ *ap = v;
717 	struct vnode *vp = ap->a_vp;
718 	struct udf_node  *udf_node = VTOI(vp);
719 	struct udf_mount *ump = udf_node->ump;
720 	struct vattr *vap = ap->a_vap;
721 	uid_t uid, nobody_uid;
722 	gid_t gid, nobody_gid;
723 
724 	/* shut up gcc for now */
725 	ap = ap;
726 	vp = vp;
727 	uid = 0;	/* XXX gcc */
728 	gid = 0;	/* XXX gcc */
729 	udf_node = udf_node;
730 
731 	DPRINTF(NOTIMPL, ("udf_setattr called\n"));
732 
733 	/*
734 	 * BUG-ALERT: UDF doesn't count '.' as an entry, so we'll have to
735 	 * subtract 1 to the link count if its a directory we're setting
736 	 * attributes on. See getattr.
737 	 */
738 	if (vap->va_type == VDIR)
739 		vap->va_nlink--;
740 
741 	/* do the uid/gid translation game */
742 	nobody_uid = ump->mount_args.nobody_uid;
743 	nobody_gid = ump->mount_args.nobody_gid;
744 	if ((uid == nobody_uid) && (gid == nobody_gid)) {
745 		uid = (uid_t) -1;
746 		gid = (gid_t) -1;
747 	}
748 
749 	/* TODO implement setattr!! NOT IMPLEMENTED yet */
750 	return 0;
751 }
752 
753 /* --------------------------------------------------------------------- */
754 
755 /*
756  * Return POSIX pathconf information for UDF file systems.
757  */
758 int
759 udf_pathconf(void *v)
760 {
761 	struct vop_pathconf_args /* {
762 		struct vnode *a_vp;
763 		int a_name;
764 		register_t *a_retval;
765 	} */ *ap = v;
766 	struct vnode *vp = ap->a_vp;
767 	struct udf_node *udf_node = VTOI(vp);
768 	uint32_t bits;
769 
770 	DPRINTF(CALL, ("udf_pathconf called\n"));
771 
772 	switch (ap->a_name) {
773 	case _PC_LINK_MAX:
774 		*ap->a_retval = (1<<16)-1;	/* 16 bits */
775 		return 0;
776 	case _PC_NAME_MAX:
777 		*ap->a_retval = NAME_MAX;
778 		return 0;
779 	case _PC_PATH_MAX:
780 		*ap->a_retval = PATH_MAX;
781 		return 0;
782 	case _PC_PIPE_BUF:
783 		*ap->a_retval = PIPE_BUF;
784 		return 0;
785 	case _PC_CHOWN_RESTRICTED:
786 		*ap->a_retval = 1;
787 		return 0;
788 	case _PC_NO_TRUNC:
789 		*ap->a_retval = 1;
790 		return 0;
791 	case _PC_SYNC_IO:
792 		*ap->a_retval = 0;     /* synchronised is off for performance */
793 		return 0;
794 	case _PC_FILESIZEBITS:
795 		bits = 32;
796 		if (udf_node)
797 			bits = 32 * vp->v_mount->mnt_dev_bshift;
798 		*ap->a_retval = bits;
799 		return 0;
800 	}
801 
802 	return EINVAL;
803 }
804 
805 
806 /* --------------------------------------------------------------------- */
807 
808 int
809 udf_open(void *v)
810 {
811 	struct vop_open_args /* {
812 		struct vnode *a_vp;
813 		int a_mode;
814 		kauth_cred_t a_cred;
815 		struct proc *a_p;
816 	} */ *ap = v;
817 
818 	DPRINTF(CALL, ("udf_open called\n"));
819 	ap = 0;		/* XXX gcc */
820 
821 	return 0;
822 }
823 
824 
825 /* --------------------------------------------------------------------- */
826 
827 int
828 udf_close(void *v)
829 {
830 	struct vop_close_args /* {
831 		struct vnode *a_vp;
832 		int a_fflag;
833 		kauth_cred_t a_cred;
834 		struct proc *a_p;
835 	} */ *ap = v;
836 	struct vnode *vp = ap->a_vp;
837 	struct udf_node *udf_node = VTOI(vp);
838 
839 	DPRINTF(CALL, ("udf_close called\n"));
840 	udf_node = udf_node;	/* shut up gcc */
841 
842 	mutex_enter(&vp->v_interlock);
843 		if (vp->v_usecount > 1) {
844 			/* TODO update times */
845 		}
846 	mutex_exit(&vp->v_interlock);
847 
848 	return 0;
849 }
850 
851 
852 /* --------------------------------------------------------------------- */
853 
854 int
855 udf_access(void *v)
856 {
857 	struct vop_access_args /* {
858 		struct vnode *a_vp;
859 		int a_mode;
860 		kauth_cred_t a_cred;
861 		struct proc *a_p;
862 	} */ *ap = v;
863 	struct vnode    *vp   = ap->a_vp;
864 	mode_t	         mode = ap->a_mode;
865 	kauth_cred_t    cred = ap->a_cred;
866 	struct udf_node *udf_node = VTOI(vp);
867 	struct file_entry    *fe;
868 	struct extfile_entry *efe;
869 	mode_t   node_mode;
870 	uid_t uid;
871 	gid_t gid;
872 
873 	DPRINTF(CALL, ("udf_access called\n"));
874 
875 	/* get access mode to compare to */
876 	node_mode = udf_getaccessmode(udf_node);
877 
878 	/* get uid/gid pair */
879 	if (udf_node->fe) {
880 		fe = udf_node->fe;
881 		uid = (uid_t)udf_rw32(fe->uid);
882 		gid = (gid_t)udf_rw32(fe->gid);
883 	} else {
884 		assert(udf_node->efe);
885 		efe = udf_node->efe;
886 		uid = (uid_t)udf_rw32(efe->uid);
887 		gid = (gid_t)udf_rw32(efe->gid);
888 	}
889 
890 	/* check if we are allowed to write */
891 	switch (vp->v_type) {
892 	case VDIR:
893 	case VLNK:
894 	case VREG:
895 		/*
896 		 * normal nodes: check if we're on a read-only mounted
897 		 * filingsystem and bomb out if we're trying to write.
898 		 */
899 		if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
900 			return EROFS;
901 		break;
902 	case VBLK:
903 	case VCHR:
904 	case VSOCK:
905 	case VFIFO:
906 		/*
907 		 * special nodes: even on read-only mounted filingsystems
908 		 * these are allowed to be written to if permissions allow.
909 		 */
910 		break;
911 	default:
912 		/* no idea what this is */
913 		return EINVAL;
914 	}
915 
916 	/* TODO support for chflags checking i.e. IMMUTABLE flag */
917 
918 	/* ask the generic vaccess to advice on security */
919 	return vaccess(vp->v_type, node_mode, uid, gid, mode, cred);
920 }
921 
922 /* --------------------------------------------------------------------- */
923 
924 int
925 udf_create(void *v)
926 {
927 	struct vop_create_args /* {
928 		struct vnode *a_dvp;
929 		struct vnode **a_vpp;
930 		struct componentname *a_cnp;
931 		struct vattr *a_vap;
932 	} */ *ap = v;
933 	struct componentname *cnp = ap->a_cnp;
934 
935 	DPRINTF(NOTIMPL, ("udf_create called\n"));
936 
937 	/* error out */
938 	PNBUF_PUT(cnp->cn_pnbuf);
939 	vput(ap->a_dvp);
940 	return ENOTSUP;
941 }
942 
943 /* --------------------------------------------------------------------- */
944 
945 int
946 udf_mknod(void *v)
947 {
948 	struct vop_mknod_args /* {
949 		struct vnode *a_dvp;
950 		struct vnode **a_vpp;
951 		struct componentname *a_cnp;
952 		struct vattr *a_vap;
953 	} */ *ap = v;
954 
955 	DPRINTF(NOTIMPL, ("udf_mknod called\n"));
956 
957 	/* error out */
958 	PNBUF_PUT(ap->a_cnp->cn_pnbuf);
959 	vput(ap->a_dvp);
960 	return ENOTSUP;
961 }
962 
963 /* --------------------------------------------------------------------- */
964 
965 int
966 udf_remove(void *v)
967 {
968 	struct vop_remove_args /* {
969 		struct vnode *a_dvp;
970 		struct vnode *a_vp;
971 		struct componentname *a_cnp;
972 	} */ *ap = v;
973 	struct vnode *dvp = ap->a_dvp;
974 	struct vnode *vp  = ap->a_vp;
975 
976 	DPRINTF(NOTIMPL, ("udf_remove called\n"));
977 
978 	/* error out */
979 	vput(dvp);
980 	vput(vp);
981 
982 	return ENOTSUP;
983 }
984 
985 /* --------------------------------------------------------------------- */
986 
987 int
988 udf_link(void *v)
989 {
990 	struct vop_link_args /* {
991 		struct vnode *a_dvp;
992 		struct vnode *a_vp;
993 		struct componentname *a_cnp;
994 	} */ *ap = v;
995 	struct vnode *dvp = ap->a_dvp;
996 	struct vnode *vp  = ap->a_vp;
997 	struct componentname *cnp = ap->a_cnp;
998 
999 	DPRINTF(NOTIMPL, ("udf_link called\n"));
1000 
1001 	/* error out */
1002 	/* XXX or just VOP_ABORTOP(dvp, a_cnp); ? */
1003 	if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
1004 		VOP_UNLOCK(vp, 0);
1005 	PNBUF_PUT(cnp->cn_pnbuf);
1006 	vput(dvp);
1007 
1008 	return ENOTSUP;
1009 }
1010 
1011 /* --------------------------------------------------------------------- */
1012 
1013 int
1014 udf_symlink(void *v)
1015 {
1016 	struct vop_symlink_args /* {
1017 		struct vnode *a_dvp;
1018 		struct vnode **a_vpp;
1019 		struct componentname *a_cnp;
1020 		struct vattr *a_vap;
1021 		char *a_target;
1022 	} */ *ap = v;
1023 	struct vnode *dvp = ap->a_dvp;
1024 	struct componentname *cnp = ap->a_cnp;
1025 
1026 	DPRINTF(NOTIMPL, ("udf_symlink called\n"));
1027 
1028 	/* error out */
1029 	VOP_ABORTOP(dvp, cnp);
1030 	vput(dvp);
1031 
1032 	return ENOTSUP;
1033 }
1034 
1035 /* --------------------------------------------------------------------- */
1036 
1037 int
1038 udf_readlink(void *v)
1039 {
1040 	struct vop_readlink_args /* {
1041 		struct vnode *a_vp;
1042 		struct uio *a_uio;
1043 		kauth_cred_t a_cred;
1044 	} */ *ap = v;
1045 #ifdef notyet
1046 	struct vnode *vp = ap->a_vp;
1047 	struct uio *uio = ap->a_uio;
1048 	kauth_cred_t cred = ap->a_cred;
1049 #endif
1050 
1051 	ap = ap;	/* shut up gcc */
1052 
1053 	DPRINTF(NOTIMPL, ("udf_readlink called\n"));
1054 
1055 	/* TODO read `file' contents and process pathcomponents into a path */
1056 	return ENOTSUP;
1057 }
1058 
1059 /* --------------------------------------------------------------------- */
1060 
1061 int
1062 udf_rename(void *v)
1063 {
1064 	struct vop_rename_args /* {
1065 		struct vnode *a_fdvp;
1066 		struct vnode *a_fvp;
1067 		struct componentname *a_fcnp;
1068 		struct vnode *a_tdvp;
1069 		struct vnode *a_tvp;
1070 		struct componentname *a_tcnp;
1071 	} */ *ap = v;
1072 	struct vnode *tvp = ap->a_tvp;
1073 	struct vnode *tdvp = ap->a_tdvp;
1074 	struct vnode *fvp = ap->a_fvp;
1075 	struct vnode *fdvp = ap->a_fdvp;
1076 
1077 	DPRINTF(NOTIMPL, ("udf_rename called\n"));
1078 
1079 	/* error out */
1080 	if (tdvp == tvp)
1081 		vrele(tdvp);
1082 	else
1083 		vput(tdvp);
1084 	if (tvp != NULL)
1085 		vput(tvp);
1086 
1087 	/* release source nodes. */
1088 	vrele(fdvp);
1089 	vrele(fvp);
1090 
1091 	return ENOTSUP;
1092 }
1093 
1094 /* --------------------------------------------------------------------- */
1095 
1096 int
1097 udf_mkdir(void *v)
1098 {
1099 	struct vop_mkdir_args /* {
1100 		struct vnode *a_dvp;
1101 		struct vnode **a_vpp;
1102 		struct componentname *a_cnp;
1103 		struct vattr *a_vap;
1104 	} */ *ap = v;
1105 	struct vnode *dvp = ap->a_dvp;
1106 	struct componentname *cnp = ap->a_cnp;
1107 
1108 	DPRINTF(NOTIMPL, ("udf_mkdir called\n"));
1109 
1110 	/* error out */
1111 	PNBUF_PUT(cnp->cn_pnbuf);
1112 	vput(dvp);
1113 
1114 	return ENOTSUP;
1115 }
1116 
1117 /* --------------------------------------------------------------------- */
1118 
1119 int
1120 udf_rmdir(void *v)
1121 {
1122 	struct vop_rmdir_args /* {
1123 		struct vnode *a_dvp;
1124 		struct vnode *a_vp;
1125 		struct componentname *a_cnp;
1126 	} */ *ap = v;
1127 	struct vnode *vp = ap->a_vp;
1128 	struct vnode *dvp = ap->a_dvp;
1129 	struct componentname *cnp = ap->a_cnp;
1130 	int error;
1131 
1132 	cnp = cnp;	/* shut up gcc */
1133 
1134 	DPRINTF(NOTIMPL, ("udf_rmdir called\n"));
1135 
1136 	error = ENOTSUP;
1137 	/* error out */
1138 	if (error != 0) {
1139 		vput(dvp);
1140 		vput(vp);
1141 	}
1142 
1143 	return error;
1144 }
1145 
1146 /* --------------------------------------------------------------------- */
1147 
1148 int
1149 udf_fsync(void *v)
1150 {
1151 	struct vop_fsync_args /* {
1152 		struct vnode *a_vp;
1153 		kauth_cred_t a_cred;
1154 		int a_flags;
1155 		off_t offlo;
1156 		off_t offhi;
1157 		struct proc *a_p;
1158 	} */ *ap = v;
1159 	struct vnode *vp = ap->a_vp;
1160 
1161 	DPRINTF(NOTIMPL, ("udf_fsync called\n"));
1162 
1163 	vp = vp;	/* shut up gcc */
1164 
1165 	return 0;
1166 }
1167 
1168 /* --------------------------------------------------------------------- */
1169 
1170 int
1171 udf_advlock(void *v)
1172 {
1173 	struct vop_advlock_args /* {
1174 		struct vnode *a_vp;
1175 		void *a_id;
1176 		int a_op;
1177 		struct flock *a_fl;
1178 		int a_flags;
1179 	} */ *ap = v;
1180 	struct vnode *vp = ap->a_vp;
1181 	struct udf_node *udf_node = VTOI(vp);
1182 	struct file_entry    *fe;
1183 	struct extfile_entry *efe;
1184 	uint64_t file_size;
1185 
1186 	DPRINTF(LOCKING, ("udf_advlock called\n"));
1187 
1188 	/* get directory filesize */
1189 	if (udf_node->fe) {
1190 		fe = udf_node->fe;
1191 		file_size = udf_rw64(fe->inf_len);
1192 	} else {
1193 		assert(udf_node->efe);
1194 		efe = udf_node->efe;
1195 		file_size = udf_rw64(efe->inf_len);
1196 	}
1197 
1198 	return lf_advlock(ap, &udf_node->lockf, file_size);
1199 }
1200 
1201 /* --------------------------------------------------------------------- */
1202 
1203 
1204 /* Global vfs vnode data structures for udfs */
1205 int (**udf_vnodeop_p) __P((void *));
1206 
1207 const struct vnodeopv_entry_desc udf_vnodeop_entries[] = {
1208 	{ &vop_default_desc, vn_default_error },
1209 	{ &vop_lookup_desc, udf_lookup },	/* lookup */
1210 	{ &vop_create_desc, udf_create },	/* create */	/* TODO */
1211 	{ &vop_mknod_desc, udf_mknod },		/* mknod */	/* TODO */
1212 	{ &vop_open_desc, udf_open },		/* open */
1213 	{ &vop_close_desc, udf_close },		/* close */
1214 	{ &vop_access_desc, udf_access },	/* access */
1215 	{ &vop_getattr_desc, udf_getattr },	/* getattr */
1216 	{ &vop_setattr_desc, udf_setattr },	/* setattr */	/* TODO */
1217 	{ &vop_read_desc, udf_read },		/* read */
1218 	{ &vop_write_desc, udf_write },		/* write */	/* WRITE */
1219 	{ &vop_fcntl_desc, genfs_fcntl },	/* fcntl */	/* TODO? */
1220 	{ &vop_ioctl_desc, genfs_enoioctl },	/* ioctl */	/* TODO? */
1221 	{ &vop_poll_desc, genfs_poll },		/* poll */	/* TODO/OK? */
1222 	{ &vop_kqfilter_desc, genfs_kqfilter },	/* kqfilter */	/* ? */
1223 	{ &vop_revoke_desc, genfs_revoke },	/* revoke */	/* TODO? */
1224 	{ &vop_mmap_desc, genfs_mmap },		/* mmap */	/* OK? */
1225 	{ &vop_fsync_desc, udf_fsync },		/* fsync */	/* TODO */
1226 	{ &vop_seek_desc, genfs_seek },		/* seek */
1227 	{ &vop_remove_desc, udf_remove },	/* remove */ 	/* TODO */
1228 	{ &vop_link_desc, udf_link },		/* link */	/* TODO */
1229 	{ &vop_rename_desc, udf_rename },	/* rename */ 	/* TODO */
1230 	{ &vop_mkdir_desc, udf_mkdir },		/* mkdir */ 	/* TODO */
1231 	{ &vop_rmdir_desc, udf_rmdir },		/* rmdir */	/* TODO */
1232 	{ &vop_symlink_desc, udf_symlink },	/* symlink */	/* TODO */
1233 	{ &vop_readdir_desc, udf_readdir },	/* readdir */
1234 	{ &vop_readlink_desc, udf_readlink },	/* readlink */	/* TEST ME */
1235 	{ &vop_abortop_desc, genfs_abortop },	/* abortop */	/* TODO/OK? */
1236 	{ &vop_inactive_desc, udf_inactive },	/* inactive */
1237 	{ &vop_reclaim_desc, udf_reclaim },	/* reclaim */
1238 	{ &vop_lock_desc, genfs_lock },		/* lock */
1239 	{ &vop_unlock_desc, genfs_unlock },	/* unlock */
1240 	{ &vop_bmap_desc, udf_trivial_bmap },	/* bmap */	/* 1:1 bmap */
1241 	{ &vop_strategy_desc, udf_strategy },	/* strategy */
1242 /*	{ &vop_print_desc, udf_print },	*/	/* print */
1243 	{ &vop_islocked_desc, genfs_islocked },	/* islocked */
1244 	{ &vop_pathconf_desc, udf_pathconf },	/* pathconf */
1245 	{ &vop_advlock_desc, udf_advlock },	/* advlock */	/* TEST ME */
1246 	{ &vop_bwrite_desc, vn_bwrite },	/* bwrite */	/* ->strategy */
1247 	{ &vop_getpages_desc, genfs_getpages },	/* getpages */
1248 	{ &vop_putpages_desc, genfs_putpages },	/* putpages */
1249 	{ NULL, NULL }
1250 };
1251 
1252 
1253 const struct vnodeopv_desc udf_vnodeop_opv_desc = {
1254 	&udf_vnodeop_p, udf_vnodeop_entries
1255 };
1256 
1257