xref: /netbsd-src/sys/fs/tmpfs/tmpfs_vnops.c (revision 4817a0b0b8fe9612e8ebe21a9bf2d97b95038a97)
1 /*	$NetBSD: tmpfs_vnops.c,v 1.75 2010/11/30 10:43:04 dholland Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9  * 2005 program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * tmpfs vnode interface.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.75 2010/11/30 10:43:04 dholland Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/dirent.h>
42 #include <sys/fcntl.h>
43 #include <sys/event.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/stat.h>
48 #include <sys/uio.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
51 #include <sys/lockf.h>
52 #include <sys/kauth.h>
53 
54 #include <uvm/uvm.h>
55 
56 #include <miscfs/fifofs/fifo.h>
57 #include <miscfs/genfs/genfs.h>
58 #include <fs/tmpfs/tmpfs_vnops.h>
59 #include <fs/tmpfs/tmpfs.h>
60 
61 /* --------------------------------------------------------------------- */
62 
63 /*
64  * vnode operations vector used for files stored in a tmpfs file system.
65  */
66 int (**tmpfs_vnodeop_p)(void *);
67 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
68 	{ &vop_default_desc,		vn_default_error },
69 	{ &vop_lookup_desc,		tmpfs_lookup },
70 	{ &vop_create_desc,		tmpfs_create },
71 	{ &vop_mknod_desc,		tmpfs_mknod },
72 	{ &vop_open_desc,		tmpfs_open },
73 	{ &vop_close_desc,		tmpfs_close },
74 	{ &vop_access_desc,		tmpfs_access },
75 	{ &vop_getattr_desc,		tmpfs_getattr },
76 	{ &vop_setattr_desc,		tmpfs_setattr },
77 	{ &vop_read_desc,		tmpfs_read },
78 	{ &vop_write_desc,		tmpfs_write },
79 	{ &vop_ioctl_desc,		tmpfs_ioctl },
80 	{ &vop_fcntl_desc,		tmpfs_fcntl },
81 	{ &vop_poll_desc,		tmpfs_poll },
82 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
83 	{ &vop_revoke_desc,		tmpfs_revoke },
84 	{ &vop_mmap_desc,		tmpfs_mmap },
85 	{ &vop_fsync_desc,		tmpfs_fsync },
86 	{ &vop_seek_desc,		tmpfs_seek },
87 	{ &vop_remove_desc,		tmpfs_remove },
88 	{ &vop_link_desc,		tmpfs_link },
89 	{ &vop_rename_desc,		tmpfs_rename },
90 	{ &vop_mkdir_desc,		tmpfs_mkdir },
91 	{ &vop_rmdir_desc,		tmpfs_rmdir },
92 	{ &vop_symlink_desc,		tmpfs_symlink },
93 	{ &vop_readdir_desc,		tmpfs_readdir },
94 	{ &vop_readlink_desc,		tmpfs_readlink },
95 	{ &vop_abortop_desc,		tmpfs_abortop },
96 	{ &vop_inactive_desc,		tmpfs_inactive },
97 	{ &vop_reclaim_desc,		tmpfs_reclaim },
98 	{ &vop_lock_desc,		tmpfs_lock },
99 	{ &vop_unlock_desc,		tmpfs_unlock },
100 	{ &vop_bmap_desc,		tmpfs_bmap },
101 	{ &vop_strategy_desc,		tmpfs_strategy },
102 	{ &vop_print_desc,		tmpfs_print },
103 	{ &vop_pathconf_desc,		tmpfs_pathconf },
104 	{ &vop_islocked_desc,		tmpfs_islocked },
105 	{ &vop_advlock_desc,		tmpfs_advlock },
106 	{ &vop_bwrite_desc,		tmpfs_bwrite },
107 	{ &vop_getpages_desc,		tmpfs_getpages },
108 	{ &vop_putpages_desc,		tmpfs_putpages },
109 	{ NULL, NULL }
110 };
111 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
112 	{ &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
113 
114 /*
115  * tmpfs_lookup: lookup routine.
116  *
117  * Arguments: dvp (directory being searched), vpp (result),
118  * cnp (component name - path).
119  *
120  * => Caller holds a reference and lock on dvp.
121  * => We return looked-up vnode (vpp) locked, with a reference held.
122  */
123 int
124 tmpfs_lookup(void *v)
125 {
126 	struct vop_lookup_args /* {
127 		struct vnode *a_dvp;
128 		struct vnode **a_vpp;
129 		struct componentname *a_cnp;
130 	} */ *ap = v;
131 	struct vnode *dvp = ap->a_dvp, **vpp = ap->a_vpp;
132 	struct componentname *cnp = ap->a_cnp;
133 	struct tmpfs_dirent *de;
134 	struct tmpfs_node *dnode;
135 	int error;
136 
137 	KASSERT(VOP_ISLOCKED(dvp));
138 
139 	dnode = VP_TO_TMPFS_DIR(dvp);
140 	*vpp = NULL;
141 
142 	/* Check accessibility of requested node as a first step. */
143 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
144 	if (error != 0)
145 		goto out;
146 
147 	/*
148 	 * If requesting the last path component on a read-only file system
149 	 * with a write operation, deny it.
150 	 */
151 	if ((cnp->cn_flags & ISLASTCN) &&
152 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
153 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
154 		error = EROFS;
155 		goto out;
156 	}
157 
158 	/*
159 	 * Avoid doing a linear scan of the directory if the requested
160 	 * directory/name couple is already in the cache.
161 	 */
162 	error = cache_lookup(dvp, vpp, cnp);
163 	if (error >= 0)
164 		goto out;
165 
166 	/* We cannot be requesting the parent directory of the root node. */
167 	KASSERT(IMPLIES(dnode->tn_type == VDIR &&
168 	    dnode->tn_spec.tn_dir.tn_parent == dnode,
169 	    !(cnp->cn_flags & ISDOTDOT)));
170 
171 	if (cnp->cn_flags & ISDOTDOT) {
172 		VOP_UNLOCK(dvp);
173 
174 		/* Allocate a new vnode on the matching entry. */
175 		error = tmpfs_alloc_vp(dvp->v_mount,
176 		    dnode->tn_spec.tn_dir.tn_parent, vpp);
177 
178 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
179 		goto done;
180 
181 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
182 		if ((cnp->cn_flags & ISLASTCN) &&
183 		    (cnp->cn_nameiop == RENAME)) {
184 			error = EISDIR;
185 			goto out;
186 		}
187 		vref(dvp);
188 		*vpp = dvp;
189 		error = 0;
190 		goto done;
191 	}
192 
193 	de = tmpfs_dir_lookup(dnode, cnp);
194 	if (de == NULL) {
195 		/*
196 		 * The entry was not found in the directory.  This is valid
197 		 * if we are creating or renaming an entry and are working
198 		 * on the last component of the path name.
199 		 */
200 		if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == CREATE ||
201 		    cnp->cn_nameiop == RENAME)) {
202 			error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
203 			if (error) {
204 				goto out;
205 			}
206 			error = EJUSTRETURN;
207 		} else {
208 			error = ENOENT;
209 		}
210 	} else {
211 		struct tmpfs_node *tnode = de->td_node;
212 
213 		/*
214 		 * If we are not at the last path component and found a
215 		 * non-directory or non-link entry (which may itself be
216 		 * pointing to a directory), raise an error.
217 		 */
218 		if ((tnode->tn_type != VDIR && tnode->tn_type != VLNK) &&
219 		    (cnp->cn_flags & ISLASTCN) == 0) {
220 			error = ENOTDIR;
221 			goto out;
222 		}
223 
224 		/* Check permissions. */
225 		if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == DELETE ||
226 		    cnp->cn_nameiop == RENAME)) {
227 			kauth_action_t action = 0;
228 
229 			/* This is the file-system's decision. */
230 			if ((dnode->tn_mode & S_ISTXT) != 0 &&
231 			    kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
232 			    kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid)
233 				error = EPERM;
234 			else
235 				error = 0;
236 
237 			/* Only bother if we are not already failing it. */
238 			if (!error) {
239 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
240 			}
241 
242 			if (cnp->cn_nameiop == DELETE) {
243 				action |= KAUTH_VNODE_DELETE;
244 			} else {
245 				KASSERT(cnp->cn_nameiop == RENAME);
246 				action |= KAUTH_VNODE_RENAME;
247 			}
248 			error = kauth_authorize_vnode(cnp->cn_cred,
249 			    action, *vpp, dvp, error);
250 			if (error) {
251 				goto out;
252 			}
253 		}
254 		/* Allocate a new vnode on the matching entry. */
255 		error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
256 	}
257 done:
258 	/*
259 	 * Store the result of this lookup in the cache.  Avoid this if the
260 	 * request was for creation, as it does not improve timings on
261 	 * emprical tests.
262 	 */
263 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
264 	    (cnp->cn_flags & ISDOTDOT) == 0)
265 		cache_enter(dvp, *vpp, cnp);
266 
267 out:
268 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
269 	KASSERT(VOP_ISLOCKED(dvp));
270 	return error;
271 }
272 
273 int
274 tmpfs_create(void *v)
275 {
276 	struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
277 	struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
278 	struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
279 	struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
280 
281 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
282 
283 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
284 }
285 /* --------------------------------------------------------------------- */
286 
287 int
288 tmpfs_mknod(void *v)
289 {
290 	struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
291 	struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
292 	struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
293 	struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
294 
295 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
296 	    vap->va_type != VFIFO) {
297 		vput(dvp);
298 		return EINVAL;
299 	}
300 
301 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
302 }
303 
304 /* --------------------------------------------------------------------- */
305 
306 int
307 tmpfs_open(void *v)
308 {
309 	struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
310 	int mode = ((struct vop_open_args *)v)->a_mode;
311 
312 	int error;
313 	struct tmpfs_node *node;
314 
315 	KASSERT(VOP_ISLOCKED(vp));
316 
317 	node = VP_TO_TMPFS_NODE(vp);
318 
319 	/* The file is still active but all its names have been removed
320 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
321 	 * it is about to die. */
322 	if (node->tn_links < 1) {
323 		error = ENOENT;
324 		goto out;
325 	}
326 
327 	/* If the file is marked append-only, deny write requests. */
328 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
329 		error = EPERM;
330 	else
331 		error = 0;
332 
333 out:
334 	KASSERT(VOP_ISLOCKED(vp));
335 
336 	return error;
337 }
338 
339 /* --------------------------------------------------------------------- */
340 
341 int
342 tmpfs_close(void *v)
343 {
344 	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
345 
346 	struct tmpfs_node *node;
347 
348 	KASSERT(VOP_ISLOCKED(vp));
349 
350 	node = VP_TO_TMPFS_NODE(vp);
351 
352 	if (node->tn_links > 0) {
353 		/* Update node times.  No need to do it if the node has
354 		 * been deleted, because it will vanish after we return. */
355 		tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
356 	}
357 
358 	return 0;
359 }
360 
361 /* --------------------------------------------------------------------- */
362 
363 static int
364 tmpfs_check_possible(struct vnode *vp, struct tmpfs_node *node, mode_t mode)
365 {
366 	int error = 0;
367 
368 	switch (vp->v_type) {
369 	case VDIR:
370 		/* FALLTHROUGH */
371 	case VLNK:
372 		/* FALLTHROUGH */
373 	case VREG:
374 		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
375 			error = EROFS;
376 			goto out;
377 		}
378 		break;
379 
380 	case VBLK:
381 		/* FALLTHROUGH */
382 	case VCHR:
383 		/* FALLTHROUGH */
384 	case VSOCK:
385 		/* FALLTHROUGH */
386 	case VFIFO:
387 		break;
388 
389 	default:
390 		error = EINVAL;
391 		goto out;
392 	}
393 
394 	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
395 		error = EPERM;
396 		goto out;
397 	}
398 
399  out:
400 	return error;
401 }
402 
403 static int
404 tmpfs_check_permitted(struct vnode *vp, struct tmpfs_node *node, mode_t mode,
405     kauth_cred_t cred)
406 {
407 
408 	return genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
409 	    node->tn_gid, mode, cred);
410 }
411 
412 int
413 tmpfs_access(void *v)
414 {
415 	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
416 	int mode = ((struct vop_access_args *)v)->a_mode;
417 	kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred;
418 
419 	int error;
420 	struct tmpfs_node *node;
421 
422 	KASSERT(VOP_ISLOCKED(vp));
423 
424 	node = VP_TO_TMPFS_NODE(vp);
425 
426 	error = tmpfs_check_possible(vp, node, mode);
427 	if (error)
428 		goto out;
429 
430 	error = tmpfs_check_permitted(vp, node, mode, cred);
431 
432 	error = kauth_authorize_vnode(cred, kauth_mode_to_action(mode), vp,
433 	    NULL, error);
434 
435 out:
436 	KASSERT(VOP_ISLOCKED(vp));
437 
438 	return error;
439 }
440 
441 /* --------------------------------------------------------------------- */
442 
443 int
444 tmpfs_getattr(void *v)
445 {
446 	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
447 	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
448 
449 	struct tmpfs_node *node;
450 
451 	node = VP_TO_TMPFS_NODE(vp);
452 
453 	vattr_null(vap);
454 
455 	tmpfs_itimes(vp, NULL, NULL, NULL);
456 
457 	vap->va_type = vp->v_type;
458 	vap->va_mode = node->tn_mode;
459 	vap->va_nlink = node->tn_links;
460 	vap->va_uid = node->tn_uid;
461 	vap->va_gid = node->tn_gid;
462 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
463 	vap->va_fileid = node->tn_id;
464 	vap->va_size = node->tn_size;
465 	vap->va_blocksize = PAGE_SIZE;
466 	vap->va_atime = node->tn_atime;
467 	vap->va_mtime = node->tn_mtime;
468 	vap->va_ctime = node->tn_ctime;
469 	vap->va_birthtime = node->tn_birthtime;
470 	vap->va_gen = node->tn_gen;
471 	vap->va_flags = node->tn_flags;
472 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
473 		node->tn_spec.tn_dev.tn_rdev : VNOVAL;
474 	vap->va_bytes = round_page(node->tn_size);
475 	vap->va_filerev = VNOVAL;
476 	vap->va_vaflags = 0;
477 	vap->va_spare = VNOVAL; /* XXX */
478 
479 	return 0;
480 }
481 
482 /* --------------------------------------------------------------------- */
483 
484 #define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
485 /* XXX Should this operation be atomic?  I think it should, but code in
486  * XXX other places (e.g., ufs) doesn't seem to be... */
487 int
488 tmpfs_setattr(void *v)
489 {
490 	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
491 	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
492 	kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred;
493 	struct lwp *l = curlwp;
494 
495 	int error;
496 
497 	KASSERT(VOP_ISLOCKED(vp));
498 
499 	error = 0;
500 
501 	/* Abort if any unsettable attribute is given. */
502 	if (vap->va_type != VNON ||
503 	    vap->va_nlink != VNOVAL ||
504 	    vap->va_fsid != VNOVAL ||
505 	    vap->va_fileid != VNOVAL ||
506 	    vap->va_blocksize != VNOVAL ||
507 	    GOODTIME(&vap->va_ctime) ||
508 	    vap->va_gen != VNOVAL ||
509 	    vap->va_rdev != VNOVAL ||
510 	    vap->va_bytes != VNOVAL)
511 		error = EINVAL;
512 
513 	if (error == 0 && (vap->va_flags != VNOVAL))
514 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
515 
516 	if (error == 0 && (vap->va_size != VNOVAL))
517 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
518 
519 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
520 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
521 
522 	if (error == 0 && (vap->va_mode != VNOVAL))
523 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
524 
525 	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
526 	    || GOODTIME(&vap->va_birthtime)))
527 		if ((error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
528 		    &vap->va_birthtime, vap->va_vaflags, cred, l)) == 0)
529 			return 0;
530 
531 	/* Update the node times.  We give preference to the error codes
532 	 * generated by this function rather than the ones that may arise
533 	 * from tmpfs_update. */
534 	tmpfs_update(vp, NULL, NULL, NULL, 0);
535 
536 	KASSERT(VOP_ISLOCKED(vp));
537 
538 	return error;
539 }
540 
541 /* --------------------------------------------------------------------- */
542 
543 int
544 tmpfs_read(void *v)
545 {
546 	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
547 	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
548 	int ioflag = ((struct vop_read_args *)v)->a_ioflag;
549 
550 	int error;
551 	struct tmpfs_node *node;
552 	struct uvm_object *uobj;
553 
554 	KASSERT(VOP_ISLOCKED(vp));
555 
556 	node = VP_TO_TMPFS_NODE(vp);
557 
558 	if (vp->v_type != VREG) {
559 		error = EISDIR;
560 		goto out;
561 	}
562 
563 	if (uio->uio_offset < 0) {
564 		error = EINVAL;
565 		goto out;
566 	}
567 
568 	node->tn_status |= TMPFS_NODE_ACCESSED;
569 
570 	uobj = node->tn_spec.tn_reg.tn_aobj;
571 	error = 0;
572 	while (error == 0 && uio->uio_resid > 0) {
573 		vsize_t len;
574 
575 		if (node->tn_size <= uio->uio_offset)
576 			break;
577 
578 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
579 		if (len == 0)
580 			break;
581 
582 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
583 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
584 	}
585 
586 out:
587 	KASSERT(VOP_ISLOCKED(vp));
588 
589 	return error;
590 }
591 
592 /* --------------------------------------------------------------------- */
593 
594 int
595 tmpfs_write(void *v)
596 {
597 	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
598 	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
599 	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
600 
601 	bool extended;
602 	int error;
603 	off_t oldsize;
604 	struct tmpfs_node *node;
605 	struct uvm_object *uobj;
606 
607 	KASSERT(VOP_ISLOCKED(vp));
608 
609 	node = VP_TO_TMPFS_NODE(vp);
610 	oldsize = node->tn_size;
611 
612 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
613 		error = EINVAL;
614 		goto out;
615 	}
616 
617 	if (uio->uio_resid == 0) {
618 		error = 0;
619 		goto out;
620 	}
621 
622 	if (ioflag & IO_APPEND)
623 		uio->uio_offset = node->tn_size;
624 
625 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
626 	if (extended) {
627 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
628 		if (error != 0)
629 			goto out;
630 	}
631 
632 	uobj = node->tn_spec.tn_reg.tn_aobj;
633 	error = 0;
634 	while (error == 0 && uio->uio_resid > 0) {
635 		vsize_t len;
636 
637 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
638 		if (len == 0)
639 			break;
640 
641 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
642 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
643 	}
644 
645 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
646 	    (extended ? TMPFS_NODE_CHANGED : 0);
647 
648 	if (error != 0)
649 		(void)tmpfs_reg_resize(vp, oldsize);
650 
651 	VN_KNOTE(vp, NOTE_WRITE);
652 
653 out:
654 	KASSERT(VOP_ISLOCKED(vp));
655 	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
656 	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
657 
658 	return error;
659 }
660 
661 /* --------------------------------------------------------------------- */
662 
663 int
664 tmpfs_fsync(void *v)
665 {
666 	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
667 
668 	KASSERT(VOP_ISLOCKED(vp));
669 
670 	tmpfs_update(vp, NULL, NULL, NULL, 0);
671 
672 	return 0;
673 }
674 
675 /* --------------------------------------------------------------------- */
676 
677 int
678 tmpfs_remove(void *v)
679 {
680 	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
681 	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
682 	struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp);
683 
684 	int error;
685 	struct tmpfs_dirent *de;
686 	struct tmpfs_mount *tmp;
687 	struct tmpfs_node *dnode;
688 	struct tmpfs_node *node;
689 
690 	KASSERT(VOP_ISLOCKED(dvp));
691 	KASSERT(VOP_ISLOCKED(vp));
692 
693 	if (vp->v_type == VDIR) {
694 		error = EPERM;
695 		goto out;
696 	}
697 
698 	dnode = VP_TO_TMPFS_DIR(dvp);
699 	node = VP_TO_TMPFS_NODE(vp);
700 	tmp = VFS_TO_TMPFS(vp->v_mount);
701 	de = tmpfs_dir_lookup(dnode, cnp);
702 	KASSERT(de);
703 	KASSERT(de->td_node == node);
704 
705 	/* Files marked as immutable or append-only cannot be deleted. */
706 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
707 		error = EPERM;
708 		goto out;
709 	}
710 
711 	/* Remove the entry from the directory; as it is a file, we do not
712 	 * have to change the number of hard links of the directory. */
713 	tmpfs_dir_detach(dvp, de);
714 
715 	/* Free the directory entry we just deleted.  Note that the node
716 	 * referred by it will not be removed until the vnode is really
717 	 * reclaimed. */
718 	tmpfs_free_dirent(tmp, de, true);
719 
720 	error = 0;
721 
722 out:
723 	vput(vp);
724 	if (dvp == vp)
725 		vrele(dvp);
726 	else
727 		vput(dvp);
728 
729 	return error;
730 }
731 
732 /* --------------------------------------------------------------------- */
733 
734 int
735 tmpfs_link(void *v)
736 {
737 	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
738 	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
739 	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
740 
741 	int error;
742 	struct tmpfs_dirent *de;
743 	struct tmpfs_node *dnode;
744 	struct tmpfs_node *node;
745 
746 	KASSERT(VOP_ISLOCKED(dvp));
747 	KASSERT(dvp != vp); /* XXX When can this be false? */
748 
749 	dnode = VP_TO_TMPFS_DIR(dvp);
750 	node = VP_TO_TMPFS_NODE(vp);
751 
752 	/* Lock vp because we will need to run tmpfs_update over it, which
753 	 * needs the vnode to be locked. */
754 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
755 
756 	/* XXX: Why aren't the following two tests done by the caller? */
757 
758 	/* Hard links of directories are forbidden. */
759 	if (vp->v_type == VDIR) {
760 		error = EPERM;
761 		goto out;
762 	}
763 
764 	/* Cannot create cross-device links. */
765 	if (dvp->v_mount != vp->v_mount) {
766 		error = EXDEV;
767 		goto out;
768 	}
769 
770 	/* Ensure that we do not overflow the maximum number of links imposed
771 	 * by the system. */
772 	KASSERT(node->tn_links <= LINK_MAX);
773 	if (node->tn_links == LINK_MAX) {
774 		error = EMLINK;
775 		goto out;
776 	}
777 
778 	/* We cannot create links of files marked immutable or append-only. */
779 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
780 		error = EPERM;
781 		goto out;
782 	}
783 
784 	/* Allocate a new directory entry to represent the node. */
785 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
786 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
787 	if (error != 0)
788 		goto out;
789 
790 	/* Insert the new directory entry into the appropriate directory. */
791 	tmpfs_dir_attach(dvp, de);
792 
793 	/* vp link count has changed, so update node times. */
794 	node->tn_status |= TMPFS_NODE_CHANGED;
795 	tmpfs_update(vp, NULL, NULL, NULL, 0);
796 
797 	error = 0;
798 
799 out:
800 	VOP_UNLOCK(vp);
801 	vput(dvp);
802 
803 	return error;
804 }
805 
806 /*
807  * tmpfs_rename: rename routine.
808  *
809  * Arguments: fdvp (from-parent vnode), fvp (from-leaf), tdvp (to-parent)
810  * and tvp (to-leaf), if exists (NULL if not).
811  *
812  * => Caller holds a reference on fdvp and fvp, they are unlocked.
813  *    Note: fdvp and fvp can refer to the same object (i.e. when it is root).
814  *
815  * => Both tdvp and tvp are referenced and locked.  It is our responsibility
816  *    to release the references and unlock them (or destroy).
817  */
818 int
819 tmpfs_rename(void *v)
820 {
821 	struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
822 	struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
823 	struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
824 	struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
825 	struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
826 	struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
827 
828 	char *newname;
829 	int error;
830 	struct tmpfs_dirent *de, *de2;
831 	struct tmpfs_mount *tmp;
832 	struct tmpfs_node *fdnode;
833 	struct tmpfs_node *fnode;
834 	struct tmpfs_node *tnode;
835 	struct tmpfs_node *tdnode;
836 	size_t namelen;
837 
838 	KASSERT(VOP_ISLOCKED(tdvp));
839 	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
840 
841 	newname = NULL;
842 	namelen = 0;
843 	tmp = NULL;
844 
845 	/* Disallow cross-device renames. */
846 	if (fvp->v_mount != tdvp->v_mount ||
847 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
848 		error = EXDEV;
849 		goto out_unlocked;
850 	}
851 
852 	fnode = VP_TO_TMPFS_NODE(fvp);
853 	fdnode = VP_TO_TMPFS_DIR(fdvp);
854 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
855 	tdnode = VP_TO_TMPFS_DIR(tdvp);
856 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
857 
858 	if (fdvp == tvp) {
859 		error = 0;
860 		goto out_unlocked;
861 	}
862 
863 	/* Allocate memory, if necessary, for a new name. */
864 	namelen = tcnp->cn_namelen;
865 	if (tmpfs_strname_neqlen(fcnp, tcnp)) {
866 		newname = tmpfs_strname_alloc(tmp, namelen);
867 		if (newname == NULL) {
868 			error = ENOSPC;
869 			goto out_unlocked;
870 		}
871 	}
872 
873 	/* If we need to move the directory between entries, lock the
874 	 * source so that we can safely operate on it. */
875 
876 	/* XXX: this is a potential locking order violation! */
877 	if (fdnode != tdnode) {
878 		vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
879 	}
880 
881 	/*
882 	 * If the node we were renaming has scarpered, just give up.
883 	 */
884 	de = tmpfs_dir_lookup(fdnode, fcnp);
885 	if (de == NULL || de->td_node != fnode) {
886 		error = ENOENT;
887 		goto out;
888 	}
889 
890 	/* If source and target is the same vnode, remove the source link. */
891 	if (fvp == tvp) {
892 		/*
893 		 * Detach and free the directory entry.  Drops the link
894 		 * count on the node.
895 		 */
896 		tmpfs_dir_detach(fdvp, de);
897 		tmpfs_free_dirent(VFS_TO_TMPFS(fvp->v_mount), de, true);
898 		VN_KNOTE(fdvp, NOTE_WRITE);
899 		goto out_ok;
900 	}
901 
902 	/* If replacing an existing entry, ensure we can do the operation. */
903 	if (tvp != NULL) {
904 		KASSERT(tnode != NULL);
905 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
906 			if (tnode->tn_size > 0) {
907 				error = ENOTEMPTY;
908 				goto out;
909 			}
910 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
911 			error = ENOTDIR;
912 			goto out;
913 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
914 			error = EISDIR;
915 			goto out;
916 		} else {
917 			KASSERT(fnode->tn_type != VDIR &&
918 			        tnode->tn_type != VDIR);
919 		}
920 	}
921 
922 	/* If the node is being moved to another directory, we have to do
923 	 * the move. */
924 	if (fdnode != tdnode) {
925 		/* In case we are moving a directory, we have to adjust its
926 		 * parent to point to the new parent. */
927 		if (de->td_node->tn_type == VDIR) {
928 			struct tmpfs_node *n;
929 
930 			/* Ensure the target directory is not a child of the
931 			 * directory being moved.  Otherwise, we'd end up
932 			 * with stale nodes. */
933 			n = tdnode;
934 			while (n != n->tn_spec.tn_dir.tn_parent) {
935 				if (n == fnode) {
936 					error = EINVAL;
937 					goto out;
938 				}
939 				n = n->tn_spec.tn_dir.tn_parent;
940 			}
941 
942 			/* Adjust the parent pointer. */
943 			TMPFS_VALIDATE_DIR(fnode);
944 			de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
945 
946 			/* As a result of changing the target of the '..'
947 			 * entry, the link count of the source and target
948 			 * directories has to be adjusted. */
949 			fdnode->tn_links--;
950 			tdnode->tn_links++;
951 		}
952 
953 		/* Do the move: just remove the entry from the source directory
954 		 * and insert it into the target one. */
955 		tmpfs_dir_detach(fdvp, de);
956 		tmpfs_dir_attach(tdvp, de);
957 
958 		/* Notify listeners of fdvp about the change in the directory.
959 		 * We can do it at this point because we aren't touching fdvp
960 		 * any more below. */
961 		VN_KNOTE(fdvp, NOTE_WRITE);
962 	}
963 
964 	/* If we are overwriting an entry, we have to remove the old one
965 	 * from the target directory. */
966 	if (tvp != NULL) {
967 		KASSERT(tnode != NULL);
968 
969 		/* Remove the old entry from the target directory.
970 		 * Note! This relies on tmpfs_dir_attach() putting the new
971 		 * node on the end of the target's node list. */
972 		de2 = tmpfs_dir_lookup(tdnode, tcnp);
973 		KASSERT(de2 != NULL);
974 		KASSERT(de2->td_node == tnode);
975 		tmpfs_dir_detach(tdvp, de2);
976 
977 		/* Free the directory entry we just deleted.  Note that the
978 		 * node referred by it will not be removed until the vnode is
979 		 * really reclaimed. */
980 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true);
981 	}
982 
983 	/* If the name has changed, we need to make it effective by changing
984 	 * it in the directory entry. */
985 	if (newname != NULL) {
986 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
987 		KASSERT(tcnp->cn_namelen < 0xffff);
988 
989 		tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
990 		de->td_namelen = (uint16_t)namelen;
991 		memcpy(newname, tcnp->cn_nameptr, namelen);
992 		de->td_name = newname;
993 		newname = NULL;
994 
995 		fnode->tn_status |= TMPFS_NODE_CHANGED;
996 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
997 	}
998  out_ok:
999 	/* Notify listeners of tdvp about the change in the directory (either
1000 	 * because a new entry was added or because one was removed) and
1001 	 * listeners of fvp about the rename. */
1002 	VN_KNOTE(tdvp, NOTE_WRITE);
1003 	VN_KNOTE(fvp, NOTE_RENAME);
1004 
1005 	error = 0;
1006 
1007  out:
1008 	if (fdnode != tdnode)
1009 		VOP_UNLOCK(fdvp);
1010 
1011  out_unlocked:
1012 	/* Release target nodes. */
1013 	if (tdvp == tvp)
1014 		vrele(tdvp);
1015 	else
1016 		vput(tdvp);
1017 	if (tvp != NULL)
1018 		vput(tvp);
1019 
1020 	/* Release source nodes. */
1021 	vrele(fdvp);
1022 	vrele(fvp);
1023 
1024 	if (newname != NULL) {
1025 		tmpfs_strname_free(tmp, newname, namelen);
1026 	}
1027 	return error;
1028 }
1029 
1030 /* --------------------------------------------------------------------- */
1031 
1032 int
1033 tmpfs_mkdir(void *v)
1034 {
1035 	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
1036 	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1037 	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
1038 	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
1039 
1040 	KASSERT(vap->va_type == VDIR);
1041 
1042 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1043 }
1044 
1045 /* --------------------------------------------------------------------- */
1046 
1047 int
1048 tmpfs_rmdir(void *v)
1049 {
1050 	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
1051 	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
1052 	struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp;
1053 
1054 	int error;
1055 	struct tmpfs_dirent *de;
1056 	struct tmpfs_mount *tmp;
1057 	struct tmpfs_node *dnode;
1058 	struct tmpfs_node *node;
1059 
1060 	KASSERT(VOP_ISLOCKED(dvp));
1061 	KASSERT(VOP_ISLOCKED(vp));
1062 
1063 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1064 	dnode = VP_TO_TMPFS_DIR(dvp);
1065 	node = VP_TO_TMPFS_DIR(vp);
1066 	error = 0;
1067 
1068 	/* Directories with more than two entries ('.' and '..') cannot be
1069 	 * removed. */
1070 	if (node->tn_size > 0) {
1071 		error = ENOTEMPTY;
1072 		goto out;
1073 	}
1074 
1075 	/* This invariant holds only if we are not trying to remove "..".
1076 	 * We checked for that above so this is safe now. */
1077 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
1078 
1079 	/* Get the directory entry associated with node (vp). */
1080 	de = tmpfs_dir_lookup(dnode, cnp);
1081 	KASSERT(de);
1082 	KASSERT(de->td_node == node);
1083 
1084 	/* Check flags to see if we are allowed to remove the directory. */
1085 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
1086 		error = EPERM;
1087 		goto out;
1088 	}
1089 
1090 	/* Detach the directory entry from the directory (dnode). */
1091 	tmpfs_dir_detach(dvp, de);
1092 
1093 	node->tn_links--;
1094 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1095 	    TMPFS_NODE_MODIFIED;
1096 	node->tn_spec.tn_dir.tn_parent->tn_links--;
1097 	node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1098 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1099 
1100 	/* Release the parent. */
1101 	cache_purge(dvp); /* XXX Is this needed? */
1102 
1103 	/* Free the directory entry we just deleted.  Note that the node
1104 	 * referred by it will not be removed until the vnode is really
1105 	 * reclaimed. */
1106 	tmpfs_free_dirent(tmp, de, true);
1107 
1108 	KASSERT(node->tn_links == 0);
1109  out:
1110 	/* Release the nodes. */
1111 	vput(dvp);
1112 	vput(vp);
1113 
1114 	return error;
1115 }
1116 
1117 /* --------------------------------------------------------------------- */
1118 
1119 int
1120 tmpfs_symlink(void *v)
1121 {
1122 	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
1123 	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
1124 	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
1125 	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
1126 	char *target = ((struct vop_symlink_args *)v)->a_target;
1127 
1128 	KASSERT(vap->va_type == VLNK);
1129 
1130 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1131 }
1132 
1133 /* --------------------------------------------------------------------- */
1134 
1135 int
1136 tmpfs_readdir(void *v)
1137 {
1138 	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
1139 	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
1140 	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
1141 	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
1142 	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
1143 
1144 	int error;
1145 	off_t startoff;
1146 	off_t cnt;
1147 	struct tmpfs_node *node;
1148 
1149 	KASSERT(VOP_ISLOCKED(vp));
1150 
1151 	/* This operation only makes sense on directory nodes. */
1152 	if (vp->v_type != VDIR) {
1153 		error = ENOTDIR;
1154 		goto out;
1155 	}
1156 
1157 	node = VP_TO_TMPFS_DIR(vp);
1158 
1159 	startoff = uio->uio_offset;
1160 
1161 	cnt = 0;
1162 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1163 		error = tmpfs_dir_getdotdent(node, uio);
1164 		if (error == -1) {
1165 			error = 0;
1166 			goto outok;
1167 		} else if (error != 0)
1168 			goto outok;
1169 		cnt++;
1170 	}
1171 
1172 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1173 		error = tmpfs_dir_getdotdotdent(node, uio);
1174 		if (error == -1) {
1175 			error = 0;
1176 			goto outok;
1177 		} else if (error != 0)
1178 			goto outok;
1179 		cnt++;
1180 	}
1181 
1182 	error = tmpfs_dir_getdents(node, uio, &cnt);
1183 	if (error == -1)
1184 		error = 0;
1185 	KASSERT(error >= 0);
1186 
1187 outok:
1188 	/* This label assumes that startoff has been
1189 	 * initialized.  If the compiler didn't spit out warnings, we'd
1190 	 * simply make this one be 'out' and drop 'outok'. */
1191 
1192 	if (eofflag != NULL)
1193 		*eofflag =
1194 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1195 
1196 	/* Update NFS-related variables. */
1197 	if (error == 0 && cookies != NULL && ncookies != NULL) {
1198 		off_t i;
1199 		off_t off = startoff;
1200 		struct tmpfs_dirent *de = NULL;
1201 
1202 		*ncookies = cnt;
1203 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1204 
1205 		for (i = 0; i < cnt; i++) {
1206 			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
1207 			if (off == TMPFS_DIRCOOKIE_DOT) {
1208 				off = TMPFS_DIRCOOKIE_DOTDOT;
1209 			} else {
1210 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1211 					de = TAILQ_FIRST(&node->tn_spec.
1212 					    tn_dir.tn_dir);
1213 				} else if (de != NULL) {
1214 					de = TAILQ_NEXT(de, td_entries);
1215 				} else {
1216 					de = tmpfs_dir_lookupbycookie(node,
1217 					    off);
1218 					KASSERT(de != NULL);
1219 					de = TAILQ_NEXT(de, td_entries);
1220 				}
1221 				if (de == NULL) {
1222 					off = TMPFS_DIRCOOKIE_EOF;
1223 				} else {
1224 					off = tmpfs_dircookie(de);
1225 				}
1226 			}
1227 
1228 			(*cookies)[i] = off;
1229 		}
1230 		KASSERT(uio->uio_offset == off);
1231 	}
1232 
1233 out:
1234 	KASSERT(VOP_ISLOCKED(vp));
1235 
1236 	return error;
1237 }
1238 
1239 /* --------------------------------------------------------------------- */
1240 
1241 int
1242 tmpfs_readlink(void *v)
1243 {
1244 	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
1245 	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
1246 
1247 	int error;
1248 	struct tmpfs_node *node;
1249 
1250 	KASSERT(VOP_ISLOCKED(vp));
1251 	KASSERT(uio->uio_offset == 0);
1252 	KASSERT(vp->v_type == VLNK);
1253 
1254 	node = VP_TO_TMPFS_NODE(vp);
1255 
1256 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
1257 	    MIN(node->tn_size, uio->uio_resid), uio);
1258 	node->tn_status |= TMPFS_NODE_ACCESSED;
1259 
1260 	KASSERT(VOP_ISLOCKED(vp));
1261 
1262 	return error;
1263 }
1264 
1265 /* --------------------------------------------------------------------- */
1266 
1267 int
1268 tmpfs_inactive(void *v)
1269 {
1270 	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
1271 
1272 	struct tmpfs_node *node;
1273 
1274 	KASSERT(VOP_ISLOCKED(vp));
1275 
1276 	node = VP_TO_TMPFS_NODE(vp);
1277 	*((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
1278 	VOP_UNLOCK(vp);
1279 
1280 	return 0;
1281 }
1282 
1283 /* --------------------------------------------------------------------- */
1284 
1285 int
1286 tmpfs_reclaim(void *v)
1287 {
1288 	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
1289 
1290 	struct tmpfs_mount *tmp;
1291 	struct tmpfs_node *node;
1292 
1293 	node = VP_TO_TMPFS_NODE(vp);
1294 	tmp = VFS_TO_TMPFS(vp->v_mount);
1295 
1296 	cache_purge(vp);
1297 	tmpfs_free_vp(vp);
1298 
1299 	/* If the node referenced by this vnode was deleted by the user,
1300 	 * we must free its associated data structures (now that the vnode
1301 	 * is being reclaimed). */
1302 	if (node->tn_links == 0)
1303 		tmpfs_free_node(tmp, node);
1304 
1305 	KASSERT(vp->v_data == NULL);
1306 
1307 	return 0;
1308 }
1309 
1310 /* --------------------------------------------------------------------- */
1311 
1312 int
1313 tmpfs_print(void *v)
1314 {
1315 	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
1316 
1317 	struct tmpfs_node *node;
1318 
1319 	node = VP_TO_TMPFS_NODE(vp);
1320 
1321 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1322 	    node, node->tn_flags, node->tn_links);
1323 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1324 	    ", status 0x%x",
1325 	    node->tn_mode, node->tn_uid, node->tn_gid,
1326 	    (uintmax_t)node->tn_size, node->tn_status);
1327 	if (vp->v_type == VFIFO)
1328 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
1329 	printf("\n");
1330 
1331 	return 0;
1332 }
1333 
1334 /* --------------------------------------------------------------------- */
1335 
1336 int
1337 tmpfs_pathconf(void *v)
1338 {
1339 	int name = ((struct vop_pathconf_args *)v)->a_name;
1340 	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
1341 
1342 	int error;
1343 
1344 	error = 0;
1345 
1346 	switch (name) {
1347 	case _PC_LINK_MAX:
1348 		*retval = LINK_MAX;
1349 		break;
1350 
1351 	case _PC_NAME_MAX:
1352 		*retval = NAME_MAX;
1353 		break;
1354 
1355 	case _PC_PATH_MAX:
1356 		*retval = PATH_MAX;
1357 		break;
1358 
1359 	case _PC_PIPE_BUF:
1360 		*retval = PIPE_BUF;
1361 		break;
1362 
1363 	case _PC_CHOWN_RESTRICTED:
1364 		*retval = 1;
1365 		break;
1366 
1367 	case _PC_NO_TRUNC:
1368 		*retval = 1;
1369 		break;
1370 
1371 	case _PC_SYNC_IO:
1372 		*retval = 1;
1373 		break;
1374 
1375 	case _PC_FILESIZEBITS:
1376 		*retval = 0; /* XXX Don't know which value should I return. */
1377 		break;
1378 
1379 	default:
1380 		error = EINVAL;
1381 	}
1382 
1383 	return error;
1384 }
1385 
1386 /* --------------------------------------------------------------------- */
1387 
1388 int
1389 tmpfs_advlock(void *v)
1390 {
1391 	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
1392 
1393 	struct tmpfs_node *node;
1394 
1395 	node = VP_TO_TMPFS_NODE(vp);
1396 
1397 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
1398 }
1399 
1400 /* --------------------------------------------------------------------- */
1401 
1402 int
1403 tmpfs_getpages(void *v)
1404 {
1405 	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
1406 	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
1407 	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
1408 	int *count = ((struct vop_getpages_args *)v)->a_count;
1409 	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
1410 	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
1411 	int advice = ((struct vop_getpages_args *)v)->a_advice;
1412 	int flags = ((struct vop_getpages_args *)v)->a_flags;
1413 
1414 	int error;
1415 	int i;
1416 	struct tmpfs_node *node;
1417 	struct uvm_object *uobj;
1418 	int npages = *count;
1419 
1420 	KASSERT(vp->v_type == VREG);
1421 	KASSERT(mutex_owned(&vp->v_interlock));
1422 
1423 	node = VP_TO_TMPFS_NODE(vp);
1424 	uobj = node->tn_spec.tn_reg.tn_aobj;
1425 
1426 	/* We currently don't rely on PGO_PASTEOF. */
1427 
1428 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1429 		if ((flags & PGO_LOCKED) == 0)
1430 			mutex_exit(&vp->v_interlock);
1431 		return EINVAL;
1432 	}
1433 
1434 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1435 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1436 	}
1437 
1438 	if ((flags & PGO_LOCKED) != 0)
1439 		return EBUSY;
1440 
1441 	if ((flags & PGO_NOTIMESTAMP) == 0) {
1442 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1443 			node->tn_status |= TMPFS_NODE_ACCESSED;
1444 
1445 		if ((access_type & VM_PROT_WRITE) != 0)
1446 			node->tn_status |= TMPFS_NODE_MODIFIED;
1447 	}
1448 
1449 	mutex_exit(&vp->v_interlock);
1450 
1451 	/*
1452 	 * Make sure that the array on which we will store the
1453 	 * gotten pages is clean.  Otherwise uao_get (pointed to by
1454 	 * the pgo_get below) gets confused and does not return the
1455 	 * appropriate pages.
1456 	 *
1457 	 * XXX This shall be revisited when kern/32166 is addressed
1458 	 * because the loop to clean m[i] will most likely be redundant
1459 	 * as well as the PGO_ALLPAGES flag.
1460 	 */
1461 	if (m != NULL)
1462 		for (i = 0; i < npages; i++)
1463 			m[i] = NULL;
1464 	mutex_enter(&uobj->vmobjlock);
1465 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
1466 	    access_type, advice, flags | PGO_ALLPAGES);
1467 #if defined(DEBUG)
1468 	{
1469 		/* Make sure that all the pages we return are valid. */
1470 		int dbgi;
1471 		if (error == 0 && m != NULL)
1472 			for (dbgi = 0; dbgi < npages; dbgi++)
1473 				KASSERT(m[dbgi] != NULL);
1474 	}
1475 #endif
1476 
1477 	return error;
1478 }
1479 
1480 /* --------------------------------------------------------------------- */
1481 
1482 int
1483 tmpfs_putpages(void *v)
1484 {
1485 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
1486 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
1487 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
1488 	int flags = ((struct vop_putpages_args *)v)->a_flags;
1489 
1490 	int error;
1491 	struct tmpfs_node *node;
1492 	struct uvm_object *uobj;
1493 
1494 	KASSERT(mutex_owned(&vp->v_interlock));
1495 
1496 	node = VP_TO_TMPFS_NODE(vp);
1497 
1498 	if (vp->v_type != VREG) {
1499 		mutex_exit(&vp->v_interlock);
1500 		return 0;
1501 	}
1502 
1503 	uobj = node->tn_spec.tn_reg.tn_aobj;
1504 	mutex_exit(&vp->v_interlock);
1505 
1506 	mutex_enter(&uobj->vmobjlock);
1507 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1508 
1509 	/* XXX mtime */
1510 
1511 	return error;
1512 }
1513