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