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