xref: /dflybsd-src/sys/vfs/tmpfs/tmpfs_vnops.c (revision 498432c875b11af5696001bb2a7413ed7c0bf334)
1 /*-
2  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
7  * 2005 program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $
31  */
32 
33 /*
34  * tmpfs vnode interface.
35  */
36 
37 #include <sys/kernel.h>
38 #include <sys/kern_syscall.h>
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/lockf.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/resourcevar.h>
45 #include <sys/sched.h>
46 #include <sys/stat.h>
47 #include <sys/systm.h>
48 #include <sys/unistd.h>
49 #include <sys/vfsops.h>
50 #include <sys/vnode.h>
51 
52 #include <sys/mplock2.h>
53 
54 #include <vm/vm.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_pager.h>
58 #include <vm/swap_pager.h>
59 
60 #include <vfs/fifofs/fifo.h>
61 #include <vfs/tmpfs/tmpfs_vnops.h>
62 #include <vfs/tmpfs/tmpfs.h>
63 
64 MALLOC_DECLARE(M_TMPFS);
65 
66 static __inline
67 void
68 tmpfs_knote(struct vnode *vp, int flags)
69 {
70 	if (flags)
71 		KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
72 }
73 
74 
75 /* --------------------------------------------------------------------- */
76 
77 static int
78 tmpfs_nresolve(struct vop_nresolve_args *v)
79 {
80 	struct vnode *dvp = v->a_dvp;
81 	struct vnode *vp = NULL;
82 	struct namecache *ncp = v->a_nch->ncp;
83 	struct tmpfs_node *tnode;
84 
85 	int error;
86 	struct tmpfs_dirent *de;
87 	struct tmpfs_node *dnode;
88 
89 	dnode = VP_TO_TMPFS_DIR(dvp);
90 
91 	de = tmpfs_dir_lookup(dnode, NULL, ncp);
92 	if (de == NULL) {
93 		error = ENOENT;
94 	} else {
95 		/*
96 		 * Allocate a vnode for the node we found.
97 		 */
98 		tnode = de->td_node;
99 		error = tmpfs_alloc_vp(dvp->v_mount, tnode,
100 				       LK_EXCLUSIVE | LK_RETRY, &vp);
101 		if (error)
102 			goto out;
103 		KKASSERT(vp);
104 	}
105 
106 out:
107 	/*
108 	 * Store the result of this lookup in the cache.  Avoid this if the
109 	 * request was for creation, as it does not improve timings on
110 	 * emprical tests.
111 	 */
112 	if (vp) {
113 		vn_unlock(vp);
114 		cache_setvp(v->a_nch, vp);
115 		vrele(vp);
116 	} else if (error == ENOENT) {
117 		cache_setvp(v->a_nch, NULL);
118 	}
119 	return error;
120 }
121 
122 static int
123 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
124 {
125 	struct vnode *dvp = v->a_dvp;
126 	struct vnode **vpp = v->a_vpp;
127 	struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
128 	struct ucred *cred = v->a_cred;
129 	int error;
130 
131 	*vpp = NULL;
132 	/* Check accessibility of requested node as a first step. */
133 	error = VOP_ACCESS(dvp, VEXEC, cred);
134 	if (error != 0)
135 		return error;
136 
137 	if (dnode->tn_dir.tn_parent != NULL) {
138 		/* Allocate a new vnode on the matching entry. */
139 		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
140 		    LK_EXCLUSIVE | LK_RETRY, vpp);
141 
142 		if (*vpp)
143 			vn_unlock(*vpp);
144 	}
145 
146 	return (*vpp == NULL) ? ENOENT : 0;
147 }
148 
149 /* --------------------------------------------------------------------- */
150 
151 static int
152 tmpfs_ncreate(struct vop_ncreate_args *v)
153 {
154 	struct vnode *dvp = v->a_dvp;
155 	struct vnode **vpp = v->a_vpp;
156 	struct namecache *ncp = v->a_nch->ncp;
157 	struct vattr *vap = v->a_vap;
158 	struct ucred *cred = v->a_cred;
159 	int error;
160 
161 	KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
162 
163 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
164 	if (error == 0) {
165 		cache_setunresolved(v->a_nch);
166 		cache_setvp(v->a_nch, *vpp);
167 		tmpfs_knote(dvp, NOTE_WRITE);
168 	}
169 
170 	return error;
171 }
172 /* --------------------------------------------------------------------- */
173 
174 static int
175 tmpfs_nmknod(struct vop_nmknod_args *v)
176 {
177 	struct vnode *dvp = v->a_dvp;
178 	struct vnode **vpp = v->a_vpp;
179 	struct namecache *ncp = v->a_nch->ncp;
180 	struct vattr *vap = v->a_vap;
181 	struct ucred *cred = v->a_cred;
182 	int error;
183 
184 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
185 	    vap->va_type != VFIFO)
186 		return EINVAL;
187 
188 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
189 	if (error == 0) {
190 		cache_setunresolved(v->a_nch);
191 		cache_setvp(v->a_nch, *vpp);
192 		tmpfs_knote(dvp, NOTE_WRITE);
193 	}
194 
195 	return error;
196 }
197 
198 /* --------------------------------------------------------------------- */
199 
200 static int
201 tmpfs_open(struct vop_open_args *v)
202 {
203 	struct vnode *vp = v->a_vp;
204 	int mode = v->a_mode;
205 
206 	int error;
207 	struct tmpfs_node *node;
208 
209 	node = VP_TO_TMPFS_NODE(vp);
210 
211 	/* The file is still active but all its names have been removed
212 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
213 	 * it is about to die. */
214 	if (node->tn_links < 1)
215 		return (ENOENT);
216 
217 	/* If the file is marked append-only, deny write requests. */
218 	if ((node->tn_flags & APPEND) &&
219 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
220 		error = EPERM;
221 	} else {
222 		return (vop_stdopen(v));
223 	}
224 	return error;
225 }
226 
227 /* --------------------------------------------------------------------- */
228 
229 static int
230 tmpfs_close(struct vop_close_args *v)
231 {
232 	struct vnode *vp = v->a_vp;
233 	struct tmpfs_node *node;
234 
235 	node = VP_TO_TMPFS_NODE(vp);
236 
237 	if (node->tn_links > 0) {
238 		/* Update node times.  No need to do it if the node has
239 		 * been deleted, because it will vanish after we return. */
240 		tmpfs_update(vp);
241 	}
242 
243 	return vop_stdclose(v);
244 }
245 
246 /* --------------------------------------------------------------------- */
247 
248 int
249 tmpfs_access(struct vop_access_args *v)
250 {
251 	struct vnode *vp = v->a_vp;
252 	int error;
253 	struct tmpfs_node *node;
254 
255 	node = VP_TO_TMPFS_NODE(vp);
256 
257 	switch (vp->v_type) {
258 	case VDIR:
259 		/* FALLTHROUGH */
260 	case VLNK:
261 		/* FALLTHROUGH */
262 	case VREG:
263 		if ((v->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
264 			error = EROFS;
265 			goto out;
266 		}
267 		break;
268 
269 	case VBLK:
270 		/* FALLTHROUGH */
271 	case VCHR:
272 		/* FALLTHROUGH */
273 	case VSOCK:
274 		/* FALLTHROUGH */
275 	case VFIFO:
276 		break;
277 
278 	default:
279 		error = EINVAL;
280 		goto out;
281 	}
282 
283 	if ((v->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) {
284 		error = EPERM;
285 		goto out;
286 	}
287 
288 	error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
289 
290 out:
291 
292 	return error;
293 }
294 
295 /* --------------------------------------------------------------------- */
296 
297 int
298 tmpfs_getattr(struct vop_getattr_args *v)
299 {
300 	struct vnode *vp = v->a_vp;
301 	struct vattr *vap = v->a_vap;
302 	struct tmpfs_node *node;
303 
304 	node = VP_TO_TMPFS_NODE(vp);
305 
306 	tmpfs_update(vp);
307 
308 	vap->va_type = vp->v_type;
309 	vap->va_mode = node->tn_mode;
310 	vap->va_nlink = node->tn_links;
311 	vap->va_uid = node->tn_uid;
312 	vap->va_gid = node->tn_gid;
313 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
314 	vap->va_fileid = node->tn_id;
315 	vap->va_size = node->tn_size;
316 	vap->va_blocksize = PAGE_SIZE;
317 	vap->va_atime.tv_sec = node->tn_atime;
318 	vap->va_atime.tv_nsec = node->tn_atimensec;
319 	vap->va_mtime.tv_sec = node->tn_mtime;
320 	vap->va_mtime.tv_nsec = node->tn_mtimensec;
321 	vap->va_ctime.tv_sec = node->tn_ctime;
322 	vap->va_ctime.tv_nsec = node->tn_ctimensec;
323 	vap->va_gen = node->tn_gen;
324 	vap->va_flags = node->tn_flags;
325 	if (vp->v_type == VBLK || vp->v_type == VCHR)
326 	{
327 		vap->va_rmajor = umajor(node->tn_rdev);
328 		vap->va_rminor = uminor(node->tn_rdev);
329 	}
330 	vap->va_bytes = round_page(node->tn_size);
331 	vap->va_filerev = 0;
332 
333 	return 0;
334 }
335 
336 /* --------------------------------------------------------------------- */
337 
338 int
339 tmpfs_setattr(struct vop_setattr_args *v)
340 {
341 	struct vnode *vp = v->a_vp;
342 	struct vattr *vap = v->a_vap;
343 	struct ucred *cred = v->a_cred;
344 	struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
345 	int error = 0;
346 	int kflags = 0;
347 
348 	if (error == 0 && (vap->va_flags != VNOVAL)) {
349 		error = tmpfs_chflags(vp, vap->va_flags, cred);
350 		kflags |= NOTE_ATTRIB;
351 	}
352 
353 	if (error == 0 && (vap->va_size != VNOVAL)) {
354 		if (vap->va_size > node->tn_size)
355 			kflags |= NOTE_WRITE | NOTE_EXTEND;
356 		else
357 			kflags |= NOTE_WRITE;
358 		error = tmpfs_chsize(vp, vap->va_size, cred);
359 	}
360 
361 	if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
362 			   vap->va_gid != (gid_t)VNOVAL)) {
363 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
364 		kflags |= NOTE_ATTRIB;
365 	}
366 
367 	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
368 		error = tmpfs_chmod(vp, vap->va_mode, cred);
369 		kflags |= NOTE_ATTRIB;
370 	}
371 
372 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
373 	    vap->va_atime.tv_nsec != VNOVAL) ||
374 	    (vap->va_mtime.tv_sec != VNOVAL &&
375 	    vap->va_mtime.tv_nsec != VNOVAL) )) {
376 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
377 				      vap->va_vaflags, cred);
378 		kflags |= NOTE_ATTRIB;
379 	}
380 
381 	/* Update the node times.  We give preference to the error codes
382 	 * generated by this function rather than the ones that may arise
383 	 * from tmpfs_update. */
384 	tmpfs_update(vp);
385 	tmpfs_knote(vp, kflags);
386 
387 	return error;
388 }
389 
390 /* --------------------------------------------------------------------- */
391 
392 /*
393  * fsync is usually a NOP, but we must take action when unmounting or
394  * when recycling.
395  */
396 static int
397 tmpfs_fsync(struct vop_fsync_args *v)
398 {
399 	struct tmpfs_mount *tmp;
400 	struct tmpfs_node *node;
401 	struct vnode *vp = v->a_vp;
402 
403 	tmp = VFS_TO_TMPFS(vp->v_mount);
404 	node = VP_TO_TMPFS_NODE(vp);
405 
406 	tmpfs_update(vp);
407 	if (vp->v_type == VREG) {
408 		if (vp->v_flag & VRECLAIMED) {
409 			if (node->tn_links == 0)
410 				tmpfs_truncate(vp, 0);
411 			else
412 				vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
413 		}
414 	}
415 	return 0;
416 }
417 
418 /* --------------------------------------------------------------------- */
419 
420 static int
421 tmpfs_read (struct vop_read_args *ap)
422 {
423 	struct buf *bp;
424 	struct vnode *vp = ap->a_vp;
425 	struct uio *uio = ap->a_uio;
426 	struct tmpfs_node *node;
427 	off_t base_offset;
428 	size_t offset;
429 	size_t len;
430 	int error;
431 
432 	error = 0;
433 	if (uio->uio_resid == 0) {
434 		return error;
435 	}
436 
437 	node = VP_TO_TMPFS_NODE(vp);
438 
439 	if (uio->uio_offset < 0)
440 		return (EINVAL);
441 	if (vp->v_type != VREG)
442 		return (EINVAL);
443 
444 	while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
445 		/*
446 		 * Use buffer cache I/O (via tmpfs_strategy)
447 		 */
448 		offset = (size_t)uio->uio_offset & BMASK;
449 		base_offset = (off_t)uio->uio_offset - offset;
450 		bp = getcacheblk(vp, base_offset);
451 		if (bp == NULL)
452 		{
453 			error = bread(vp, base_offset, BSIZE, &bp);
454 			if (error) {
455 				brelse(bp);
456 				kprintf("tmpfs_read bread error %d\n", error);
457 				break;
458 			}
459 		}
460 
461 		/*
462 		 * Figure out how many bytes we can actually copy this loop.
463 		 */
464 		len = BSIZE - offset;
465 		if (len > uio->uio_resid)
466 			len = uio->uio_resid;
467 		if (len > node->tn_size - uio->uio_offset)
468 			len = (size_t)(node->tn_size - uio->uio_offset);
469 
470 		error = uiomove((char *)bp->b_data + offset, len, uio);
471 		bqrelse(bp);
472 		if (error) {
473 			kprintf("tmpfs_read uiomove error %d\n", error);
474 			break;
475 		}
476 	}
477 
478 	TMPFS_NODE_LOCK(node);
479 	node->tn_status |= TMPFS_NODE_ACCESSED;
480 	TMPFS_NODE_UNLOCK(node);
481 
482 	return(error);
483 }
484 
485 static int
486 tmpfs_write (struct vop_write_args *ap)
487 {
488 	struct buf *bp;
489 	struct vnode *vp = ap->a_vp;
490 	struct uio *uio = ap->a_uio;
491 	struct thread *td = uio->uio_td;
492 	struct tmpfs_node *node;
493 	boolean_t extended;
494 	off_t oldsize;
495 	int error;
496 	off_t base_offset;
497 	size_t offset;
498 	size_t len;
499 	struct rlimit limit;
500 	int got_mplock;
501 	int trivial = 0;
502 	int kflags = 0;
503 
504 	error = 0;
505 	if (uio->uio_resid == 0) {
506 		return error;
507 	}
508 
509 	node = VP_TO_TMPFS_NODE(vp);
510 
511 	if (vp->v_type != VREG)
512 		return (EINVAL);
513 
514 	oldsize = node->tn_size;
515 	if (ap->a_ioflag & IO_APPEND)
516 		uio->uio_offset = node->tn_size;
517 
518 	/*
519 	 * Check for illegal write offsets.
520 	 */
521 	if (uio->uio_offset + uio->uio_resid >
522 	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
523 		return (EFBIG);
524 
525 	if (vp->v_type == VREG && td != NULL) {
526 		error = kern_getrlimit(RLIMIT_FSIZE, &limit);
527 		if (error != 0)
528 			return error;
529 		if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
530 			ksignal(td->td_proc, SIGXFSZ);
531 			return (EFBIG);
532 		}
533 	}
534 
535 
536 	/*
537 	 * Extend the file's size if necessary
538 	 */
539 	extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
540 
541 #ifdef SMP
542 	if (curthread->td_mpcount) {
543 		got_mplock = -1;
544 	} else {
545 		got_mplock = 1;
546 		get_mplock();
547 	}
548 #else
549 	got_mplock = -1;
550 #endif
551 	while (uio->uio_resid > 0) {
552 		/*
553 		 * Use buffer cache I/O (via tmpfs_strategy)
554 		 */
555 		offset = (size_t)uio->uio_offset & BMASK;
556 		base_offset = (off_t)uio->uio_offset - offset;
557 		len = BSIZE - offset;
558 		if (len > uio->uio_resid)
559 			len = uio->uio_resid;
560 
561 		if ((uio->uio_offset + len) > node->tn_size) {
562 			trivial = (uio->uio_offset <= node->tn_size);
563 			error = tmpfs_reg_resize(vp, uio->uio_offset + len,  trivial);
564 			if (error)
565 				break;
566 		}
567 
568 		/*
569 		 * Read to fill in any gaps.  Theoretically we could
570 		 * optimize this if the write covers the entire buffer
571 		 * and is not a UIO_NOCOPY write, however this can lead
572 		 * to a security violation exposing random kernel memory
573 		 * (whatever junk was in the backing VM pages before).
574 		 *
575 		 * So just use bread() to do the right thing.
576 		 */
577 		error = bread(vp, base_offset, BSIZE, &bp);
578 		error = uiomove((char *)bp->b_data + offset, len, uio);
579 		if (error) {
580 			kprintf("tmpfs_write uiomove error %d\n", error);
581 			brelse(bp);
582 			break;
583 		}
584 
585 		if (uio->uio_offset > node->tn_size) {
586 			node->tn_size = uio->uio_offset;
587 			kflags |= NOTE_EXTEND;
588 		}
589 		kflags |= NOTE_WRITE;
590 
591 		/*
592 		 * The data has been loaded into the buffer, write it out.
593 		 *
594 		 * We want tmpfs to be able to use all available ram, not
595 		 * just the buffer cache, so if not explicitly paging we
596 		 * use buwrite() to leave the buffer clean but mark all the
597 		 * VM pages valid+dirty.
598 		 *
599 		 * When the kernel is paging, either via normal pageout
600 		 * operation or when cleaning the object during a recycle,
601 		 * the underlying VM pages are going to get thrown away
602 		 * so we MUST write them to swap.
603 		 *
604 		 * XXX unfortunately this catches msync() system calls too
605 		 * for the moment.
606 		 */
607 		if (vm_swap_size == 0) {
608 			/*
609 			 * if swap isn't configured yet, force a buwrite() to
610 			 * avoid problems further down the line, due to flushing
611 			 * to swap.
612 			 */
613 			buwrite(bp);
614 		} else {
615 			if (ap->a_ioflag & IO_SYNC) {
616 				bwrite(bp);
617 			} else if ((ap->a_ioflag & IO_ASYNC) ||
618 				 (uio->uio_segflg == UIO_NOCOPY)) {
619 				bawrite(bp);
620 			} else {
621 				buwrite(bp);
622 			}
623 		}
624 
625 		if (bp->b_error) {
626 			kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
627 			break;
628 		}
629 	}
630 
631 	if (got_mplock > 0)
632 		rel_mplock();
633 
634 	if (error) {
635 		if (extended) {
636 			(void)tmpfs_reg_resize(vp, oldsize, trivial);
637 			kflags &= ~NOTE_EXTEND;
638 		}
639 		goto done;
640 	}
641 
642 	TMPFS_NODE_LOCK(node);
643 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
644 	    (extended? TMPFS_NODE_CHANGED : 0);
645 
646 	if (node->tn_mode & (S_ISUID | S_ISGID)) {
647 		if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
648 			node->tn_mode &= ~(S_ISUID | S_ISGID);
649 	}
650 	TMPFS_NODE_UNLOCK(node);
651 done:
652 
653 	tmpfs_knote(vp, kflags);
654 	return(error);
655 }
656 
657 static int
658 tmpfs_advlock (struct vop_advlock_args *ap)
659 {
660 	struct tmpfs_node *node;
661 	struct vnode *vp = ap->a_vp;
662 
663 	node = VP_TO_TMPFS_NODE(vp);
664 
665 	return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
666 }
667 
668 static int
669 tmpfs_strategy(struct vop_strategy_args *ap)
670 {
671 	struct bio *bio = ap->a_bio;
672 	struct buf *bp = bio->bio_buf;
673 	struct vnode *vp = ap->a_vp;
674 	struct tmpfs_node *node;
675 	vm_object_t uobj;
676 
677 	if (vp->v_type != VREG) {
678 		bp->b_resid = bp->b_bcount;
679 		bp->b_flags |= B_ERROR | B_INVAL;
680 		bp->b_error = EINVAL;
681 		biodone(bio);
682 		return(0);
683 	}
684 
685 	node = VP_TO_TMPFS_NODE(vp);
686 
687 	uobj = node->tn_reg.tn_aobj;
688 
689 	/*
690 	 * Call swap_pager_strategy to read or write between the VM
691 	 * object and the buffer cache.
692 	 */
693 	swap_pager_strategy(uobj, bio);
694 
695 	return 0;
696 }
697 
698 static int
699 tmpfs_bmap(struct vop_bmap_args *ap)
700 {
701 	if (ap->a_doffsetp != NULL)
702 		*ap->a_doffsetp = ap->a_loffset;
703 	if (ap->a_runp != NULL)
704 		*ap->a_runp = 0;
705 	if (ap->a_runb != NULL)
706 		*ap->a_runb = 0;
707 
708 	return 0;
709 }
710 
711 /* --------------------------------------------------------------------- */
712 
713 static int
714 tmpfs_nremove(struct vop_nremove_args *v)
715 {
716 	struct vnode *dvp = v->a_dvp;
717 	struct namecache *ncp = v->a_nch->ncp;
718 	struct vnode *vp;
719 	int error;
720 	struct tmpfs_dirent *de;
721 	struct tmpfs_mount *tmp;
722 	struct tmpfs_node *dnode;
723 	struct tmpfs_node *node;
724 
725 	/*
726 	 * We have to acquire the vp from v->a_nch because
727 	 * we will likely unresolve the namecache entry, and
728 	 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
729 	 * sequence to recover space from the file.
730 	 */
731 	error = cache_vref(v->a_nch, v->a_cred, &vp);
732 	KKASSERT(error == 0);
733 
734 	if (vp->v_type == VDIR) {
735 		error = EISDIR;
736 		goto out;
737 	}
738 
739 	dnode = VP_TO_TMPFS_DIR(dvp);
740 	node = VP_TO_TMPFS_NODE(vp);
741 	tmp = VFS_TO_TMPFS(vp->v_mount);
742 	de = tmpfs_dir_lookup(dnode, node, ncp);
743 	if (de == NULL) {
744 		error = ENOENT;
745 		goto out;
746 	}
747 
748 	/* Files marked as immutable or append-only cannot be deleted. */
749 	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
750 	    (dnode->tn_flags & APPEND)) {
751 		error = EPERM;
752 		goto out;
753 	}
754 
755 	/* Remove the entry from the directory; as it is a file, we do not
756 	 * have to change the number of hard links of the directory. */
757 	tmpfs_dir_detach(dnode, de);
758 
759 	/* Free the directory entry we just deleted.  Note that the node
760 	 * referred by it will not be removed until the vnode is really
761 	 * reclaimed. */
762 	tmpfs_free_dirent(tmp, de);
763 
764 	if (node->tn_links > 0) {
765 	        TMPFS_NODE_LOCK(node);
766 		node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
767 	                TMPFS_NODE_MODIFIED;
768 	        TMPFS_NODE_UNLOCK(node);
769 	}
770 
771 	cache_setunresolved(v->a_nch);
772 	cache_setvp(v->a_nch, NULL);
773 	tmpfs_knote(vp, NOTE_DELETE);
774 	/*cache_inval_vp(vp, CINV_DESTROY);*/
775 	tmpfs_knote(dvp, NOTE_WRITE);
776 	error = 0;
777 
778 out:
779 	vrele(vp);
780 
781 	return error;
782 }
783 
784 /* --------------------------------------------------------------------- */
785 
786 static int
787 tmpfs_nlink(struct vop_nlink_args *v)
788 {
789 	struct vnode *dvp = v->a_dvp;
790 	struct vnode *vp = v->a_vp;
791 	struct namecache *ncp = v->a_nch->ncp;
792 	struct tmpfs_dirent *de;
793 	struct tmpfs_node *node;
794 	struct tmpfs_node *dnode;
795 	int error;
796 
797 	KKASSERT(dvp != vp); /* XXX When can this be false? */
798 
799 	node = VP_TO_TMPFS_NODE(vp);
800 	dnode = VP_TO_TMPFS_NODE(dvp);
801 
802 	/* XXX: Why aren't the following two tests done by the caller? */
803 
804 	/* Hard links of directories are forbidden. */
805 	if (vp->v_type == VDIR) {
806 		error = EPERM;
807 		goto out;
808 	}
809 
810 	/* Cannot create cross-device links. */
811 	if (dvp->v_mount != vp->v_mount) {
812 		error = EXDEV;
813 		goto out;
814 	}
815 
816 	/* Ensure that we do not overflow the maximum number of links imposed
817 	 * by the system. */
818 	KKASSERT(node->tn_links <= LINK_MAX);
819 	if (node->tn_links == LINK_MAX) {
820 		error = EMLINK;
821 		goto out;
822 	}
823 
824 	/* We cannot create links of files marked immutable or append-only. */
825 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
826 		error = EPERM;
827 		goto out;
828 	}
829 
830 	/* Allocate a new directory entry to represent the node. */
831 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
832 	    ncp->nc_name, ncp->nc_nlen, &de);
833 	if (error != 0)
834 		goto out;
835 
836 	/* Insert the new directory entry into the appropriate directory. */
837 	tmpfs_dir_attach(dnode, de);
838 
839 	/* vp link count has changed, so update node times. */
840 
841 	TMPFS_NODE_LOCK(node);
842 	node->tn_status |= TMPFS_NODE_CHANGED;
843 	TMPFS_NODE_UNLOCK(node);
844 	tmpfs_update(vp);
845 
846 	tmpfs_knote(vp, NOTE_LINK);
847 	cache_setunresolved(v->a_nch);
848 	cache_setvp(v->a_nch, vp);
849 	tmpfs_knote(dvp, NOTE_WRITE);
850 	error = 0;
851 
852 out:
853 	return error;
854 }
855 
856 /* --------------------------------------------------------------------- */
857 
858 static int
859 tmpfs_nrename(struct vop_nrename_args *v)
860 {
861 	struct vnode *fdvp = v->a_fdvp;
862 	struct namecache *fncp = v->a_fnch->ncp;
863 	struct vnode *fvp = fncp->nc_vp;
864 	struct vnode *tdvp = v->a_tdvp;
865 	struct namecache *tncp = v->a_tnch->ncp;
866 	struct vnode *tvp = tncp->nc_vp;
867 	struct tmpfs_dirent *de;
868 	struct tmpfs_mount *tmp;
869 	struct tmpfs_node *fdnode;
870 	struct tmpfs_node *fnode;
871 	struct tmpfs_node *tnode;
872 	struct tmpfs_node *tdnode;
873 	char *newname;
874 	char *oldname;
875 	int error;
876 
877 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
878 
879 	/* Disallow cross-device renames.
880 	 * XXX Why isn't this done by the caller? */
881 	if (fvp->v_mount != tdvp->v_mount ||
882 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
883 		error = EXDEV;
884 		goto out;
885 	}
886 
887 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
888 	tdnode = VP_TO_TMPFS_DIR(tdvp);
889 
890 	/* If source and target are the same file, there is nothing to do. */
891 	if (fvp == tvp) {
892 		error = 0;
893 		goto out;
894 	}
895 
896 	fdnode = VP_TO_TMPFS_DIR(fdvp);
897 	fnode = VP_TO_TMPFS_NODE(fvp);
898 	de = tmpfs_dir_lookup(fdnode, fnode, fncp);
899 
900 	/* Avoid manipulating '.' and '..' entries. */
901 	if (de == NULL) {
902 		error = ENOENT;
903 		goto out_locked;
904 	}
905 	KKASSERT(de->td_node == fnode);
906 
907 	/*
908 	 * If replacing an entry in the target directory and that entry
909 	 * is a directory, it must be empty.
910 	 *
911 	 * Kern_rename gurantees the destination to be a directory
912 	 * if the source is one (it does?).
913 	 */
914 	if (tvp != NULL) {
915 		KKASSERT(tnode != NULL);
916 
917 		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
918 		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
919 			error = EPERM;
920 			goto out_locked;
921 		}
922 
923 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
924 			if (tnode->tn_size > 0) {
925 				error = ENOTEMPTY;
926 				goto out_locked;
927 			}
928 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
929 			error = ENOTDIR;
930 			goto out_locked;
931 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
932 			error = EISDIR;
933 			goto out_locked;
934 		} else {
935 			KKASSERT(fnode->tn_type != VDIR &&
936 				tnode->tn_type != VDIR);
937 		}
938 	}
939 
940 	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
941 	    (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
942 		error = EPERM;
943 		goto out_locked;
944 	}
945 
946 	/*
947 	 * Ensure that we have enough memory to hold the new name, if it
948 	 * has to be changed.
949 	 */
950 	if (fncp->nc_nlen != tncp->nc_nlen ||
951 	    bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
952 		newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
953 				  M_WAITOK | M_NULLOK);
954 		if (newname == NULL) {
955 			error = ENOSPC;
956 			goto out_locked;
957 		}
958 		bcopy(tncp->nc_name, newname, tncp->nc_nlen);
959 		newname[tncp->nc_nlen] = '\0';
960 	} else {
961 		newname = NULL;
962 	}
963 
964 	/*
965 	 * Unlink entry from source directory.  Note that the kernel has
966 	 * already checked for illegal recursion cases (renaming a directory
967 	 * into a subdirectory of itself).
968 	 */
969 	if (fdnode != tdnode)
970 		tmpfs_dir_detach(fdnode, de);
971 
972 	/*
973 	 * Handle any name change.  Swap with newname, we will
974 	 * deallocate it at the end.
975 	 */
976 	if (newname != NULL) {
977 #if 0
978 		TMPFS_NODE_LOCK(fnode);
979 		fnode->tn_status |= TMPFS_NODE_CHANGED;
980 		TMPFS_NODE_UNLOCK(fnode);
981 #endif
982 		oldname = de->td_name;
983 		de->td_name = newname;
984 		de->td_namelen = (uint16_t)tncp->nc_nlen;
985 		newname = oldname;
986 	}
987 
988 	/*
989 	 * Link entry to target directory.  If the entry
990 	 * represents a directory move the parent linkage
991 	 * as well.
992 	 */
993 	if (fdnode != tdnode) {
994 		if (de->td_node->tn_type == VDIR) {
995 			TMPFS_VALIDATE_DIR(fnode);
996 
997 			TMPFS_NODE_LOCK(tdnode);
998 			tdnode->tn_links++;
999 			tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1000 			TMPFS_NODE_UNLOCK(tdnode);
1001 
1002 			TMPFS_NODE_LOCK(fnode);
1003 			fnode->tn_dir.tn_parent = tdnode;
1004 			fnode->tn_status |= TMPFS_NODE_CHANGED;
1005 			TMPFS_NODE_UNLOCK(fnode);
1006 
1007 			TMPFS_NODE_LOCK(fdnode);
1008 			fdnode->tn_links--;
1009 			fdnode->tn_status |= TMPFS_NODE_MODIFIED;
1010 			TMPFS_NODE_UNLOCK(fdnode);
1011 		}
1012 		tmpfs_dir_attach(tdnode, de);
1013 	} else {
1014 		TMPFS_NODE_LOCK(tdnode);
1015 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1016 		TMPFS_NODE_UNLOCK(tdnode);
1017 	}
1018 
1019 	/*
1020 	 * If we are overwriting an entry, we have to remove the old one
1021 	 * from the target directory.
1022 	 */
1023 	if (tvp != NULL) {
1024 		/* Remove the old entry from the target directory. */
1025 		de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1026 		tmpfs_dir_detach(tdnode, de);
1027 		tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1028 
1029 		/*
1030 		 * Free the directory entry we just deleted.  Note that the
1031 		 * node referred by it will not be removed until the vnode is
1032 		 * really reclaimed.
1033 		 */
1034 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1035 		/*cache_inval_vp(tvp, CINV_DESTROY);*/
1036 	}
1037 
1038 	/*
1039 	 * Finish up
1040 	 */
1041 	if (newname) {
1042 		kfree(newname, tmp->tm_name_zone);
1043 		newname = NULL;
1044 	}
1045 	cache_rename(v->a_fnch, v->a_tnch);
1046 	tmpfs_knote(v->a_fdvp, NOTE_WRITE);
1047 	tmpfs_knote(v->a_tdvp, NOTE_WRITE);
1048 	if (fnode->tn_vnode)
1049 		tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1050 	error = 0;
1051 
1052 out_locked:
1053 	;
1054 
1055 out:
1056 	/* Release target nodes. */
1057 	/* XXX: I don't understand when tdvp can be the same as tvp, but
1058 	 * other code takes care of this... */
1059 	if (tdvp == tvp)
1060 		vrele(tdvp);
1061 
1062 	return error;
1063 }
1064 
1065 /* --------------------------------------------------------------------- */
1066 
1067 static int
1068 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1069 {
1070 	struct vnode *dvp = v->a_dvp;
1071 	struct vnode **vpp = v->a_vpp;
1072 	struct namecache *ncp = v->a_nch->ncp;
1073 	struct vattr *vap = v->a_vap;
1074 	struct ucred *cred = v->a_cred;
1075 	int error;
1076 
1077 	KKASSERT(vap->va_type == VDIR);
1078 
1079 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1080 	if (error == 0) {
1081 		cache_setunresolved(v->a_nch);
1082 		cache_setvp(v->a_nch, *vpp);
1083 		tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1084 	}
1085 
1086 	return error;
1087 }
1088 
1089 /* --------------------------------------------------------------------- */
1090 
1091 static int
1092 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1093 {
1094 	struct vnode *dvp = v->a_dvp;
1095 	struct namecache *ncp = v->a_nch->ncp;
1096 	struct vnode *vp;
1097 	struct tmpfs_dirent *de;
1098 	struct tmpfs_mount *tmp;
1099 	struct tmpfs_node *dnode;
1100 	struct tmpfs_node *node;
1101 	int error;
1102 
1103 	/*
1104 	 * We have to acquire the vp from v->a_nch because
1105 	 * we will likely unresolve the namecache entry, and
1106 	 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1107 	 * sequence.
1108 	 */
1109 	error = cache_vref(v->a_nch, v->a_cred, &vp);
1110 	KKASSERT(error == 0);
1111 
1112 	/*
1113 	 * Prevalidate so we don't hit an assertion later
1114 	 */
1115 	if (vp->v_type != VDIR) {
1116 		error = ENOTDIR;
1117 		goto out;
1118 	}
1119 
1120 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1121 	dnode = VP_TO_TMPFS_DIR(dvp);
1122 	node = VP_TO_TMPFS_DIR(vp);
1123 
1124 	/* Directories with more than two entries ('.' and '..') cannot be
1125 	 * removed. */
1126 	 if (node->tn_size > 0) {
1127 		 error = ENOTEMPTY;
1128 		 goto out;
1129 	 }
1130 
1131 	if ((dnode->tn_flags & APPEND)
1132 	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1133 		error = EPERM;
1134 		goto out;
1135 	}
1136 
1137 	/* This invariant holds only if we are not trying to remove "..".
1138 	  * We checked for that above so this is safe now. */
1139 	KKASSERT(node->tn_dir.tn_parent == dnode);
1140 
1141 	/* Get the directory entry associated with node (vp).  This was
1142 	 * filled by tmpfs_lookup while looking up the entry. */
1143 	de = tmpfs_dir_lookup(dnode, node, ncp);
1144 	KKASSERT(TMPFS_DIRENT_MATCHES(de,
1145 	    ncp->nc_name,
1146 	    ncp->nc_nlen));
1147 
1148 	/* Check flags to see if we are allowed to remove the directory. */
1149 	if ((dnode->tn_flags & APPEND) ||
1150 	    node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1151 		error = EPERM;
1152 		goto out;
1153 	}
1154 
1155 
1156 	/* Detach the directory entry from the directory (dnode). */
1157 	tmpfs_dir_detach(dnode, de);
1158 
1159 	/* No vnode should be allocated for this entry from this point */
1160 	TMPFS_NODE_LOCK(node);
1161 	TMPFS_ASSERT_ELOCKED(node);
1162 	TMPFS_NODE_LOCK(dnode);
1163 	TMPFS_ASSERT_ELOCKED(dnode);
1164 
1165 #if 0
1166 	/* handled by tmpfs_free_node */
1167 	KKASSERT(node->tn_links > 0);
1168 	node->tn_links--;
1169 	node->tn_dir.tn_parent = NULL;
1170 #endif
1171 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1172 	    TMPFS_NODE_MODIFIED;
1173 
1174 #if 0
1175 	/* handled by tmpfs_free_node */
1176 	KKASSERT(dnode->tn_links > 0);
1177 	dnode->tn_links--;
1178 #endif
1179 	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1180 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1181 
1182 	TMPFS_NODE_UNLOCK(dnode);
1183 	TMPFS_NODE_UNLOCK(node);
1184 
1185 	/* Free the directory entry we just deleted.  Note that the node
1186 	 * referred by it will not be removed until the vnode is really
1187 	 * reclaimed. */
1188 	tmpfs_free_dirent(tmp, de);
1189 
1190 	/* Release the deleted vnode (will destroy the node, notify
1191 	 * interested parties and clean it from the cache). */
1192 
1193 	TMPFS_NODE_LOCK(dnode);
1194 	dnode->tn_status |= TMPFS_NODE_CHANGED;
1195 	TMPFS_NODE_UNLOCK(dnode);
1196 	tmpfs_update(dvp);
1197 
1198 	cache_setunresolved(v->a_nch);
1199 	cache_setvp(v->a_nch, NULL);
1200 	/*cache_inval_vp(vp, CINV_DESTROY);*/
1201 	tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1202 	error = 0;
1203 
1204 out:
1205 	vrele(vp);
1206 
1207 	return error;
1208 }
1209 
1210 /* --------------------------------------------------------------------- */
1211 
1212 static int
1213 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1214 {
1215 	struct vnode *dvp = v->a_dvp;
1216 	struct vnode **vpp = v->a_vpp;
1217 	struct namecache *ncp = v->a_nch->ncp;
1218 	struct vattr *vap = v->a_vap;
1219 	struct ucred *cred = v->a_cred;
1220 	char *target = v->a_target;
1221 	int error;
1222 
1223 	vap->va_type = VLNK;
1224 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1225 	if (error == 0) {
1226 		tmpfs_knote(*vpp, NOTE_WRITE);
1227 		cache_setunresolved(v->a_nch);
1228 		cache_setvp(v->a_nch, *vpp);
1229 	}
1230 
1231 	return error;
1232 }
1233 
1234 /* --------------------------------------------------------------------- */
1235 
1236 static int
1237 tmpfs_readdir(struct vop_readdir_args *v)
1238 {
1239 	struct vnode *vp = v->a_vp;
1240 	struct uio *uio = v->a_uio;
1241 	int *eofflag = v->a_eofflag;
1242 	off_t **cookies = v->a_cookies;
1243 	int *ncookies = v->a_ncookies;
1244 	struct tmpfs_mount *tmp;
1245 	int error;
1246 	off_t startoff;
1247 	off_t cnt = 0;
1248 	struct tmpfs_node *node;
1249 
1250 	/* This operation only makes sense on directory nodes. */
1251 	if (vp->v_type != VDIR)
1252 		return ENOTDIR;
1253 
1254 	tmp = VFS_TO_TMPFS(vp->v_mount);
1255 	node = VP_TO_TMPFS_DIR(vp);
1256 	startoff = uio->uio_offset;
1257 
1258 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1259 		error = tmpfs_dir_getdotdent(node, uio);
1260 		if (error != 0)
1261 			goto outok;
1262 		cnt++;
1263 	}
1264 
1265 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1266 		error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1267 		if (error != 0)
1268 			goto outok;
1269 		cnt++;
1270 	}
1271 
1272 	error = tmpfs_dir_getdents(node, uio, &cnt);
1273 
1274 outok:
1275 	KKASSERT(error >= -1);
1276 
1277 	if (error == -1)
1278 		error = 0;
1279 
1280 	if (eofflag != NULL)
1281 		*eofflag =
1282 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1283 
1284 	/* Update NFS-related variables. */
1285 	if (error == 0 && cookies != NULL && ncookies != NULL) {
1286 		off_t i;
1287 		off_t off = startoff;
1288 		struct tmpfs_dirent *de = NULL;
1289 
1290 		*ncookies = cnt;
1291 		*cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1292 
1293 		for (i = 0; i < cnt; i++) {
1294 			KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1295 			if (off == TMPFS_DIRCOOKIE_DOT) {
1296 				off = TMPFS_DIRCOOKIE_DOTDOT;
1297 			} else {
1298 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1299 					de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1300 				} else if (de != NULL) {
1301 					de = TAILQ_NEXT(de, td_entries);
1302 				} else {
1303 					de = tmpfs_dir_lookupbycookie(node,
1304 					    off);
1305 					KKASSERT(de != NULL);
1306 					de = TAILQ_NEXT(de, td_entries);
1307 				}
1308 				if (de == NULL)
1309 					off = TMPFS_DIRCOOKIE_EOF;
1310 				else
1311 					off = tmpfs_dircookie(de);
1312 			}
1313 
1314 			(*cookies)[i] = off;
1315 		}
1316 		KKASSERT(uio->uio_offset == off);
1317 	}
1318 
1319 	return error;
1320 }
1321 
1322 /* --------------------------------------------------------------------- */
1323 
1324 static int
1325 tmpfs_readlink(struct vop_readlink_args *v)
1326 {
1327 	struct vnode *vp = v->a_vp;
1328 	struct uio *uio = v->a_uio;
1329 
1330 	int error;
1331 	struct tmpfs_node *node;
1332 
1333 	KKASSERT(uio->uio_offset == 0);
1334 	KKASSERT(vp->v_type == VLNK);
1335 
1336 	node = VP_TO_TMPFS_NODE(vp);
1337 
1338 	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1339 	    uio);
1340 	TMPFS_NODE_LOCK(node);
1341 	node->tn_status |= TMPFS_NODE_ACCESSED;
1342 	TMPFS_NODE_UNLOCK(node);
1343 
1344 	return error;
1345 }
1346 
1347 /* --------------------------------------------------------------------- */
1348 
1349 static int
1350 tmpfs_inactive(struct vop_inactive_args *v)
1351 {
1352 	struct vnode *vp = v->a_vp;
1353 
1354 	struct tmpfs_node *node;
1355 
1356 	node = VP_TO_TMPFS_NODE(vp);
1357 
1358 	/*
1359 	 * Get rid of unreferenced deleted vnodes sooner rather than
1360 	 * later so the data memory can be recovered immediately.
1361 	 *
1362 	 * We must truncate the vnode to prevent the normal reclamation
1363 	 * path from flushing the data for the removed file to disk.
1364 	 */
1365 	TMPFS_NODE_LOCK(node);
1366 	if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1367 	    (node->tn_links == 0 ||
1368 	     (node->tn_links == 1 && node->tn_type == VDIR &&
1369 	      node->tn_dir.tn_parent)))
1370 	{
1371 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1372 		TMPFS_NODE_UNLOCK(node);
1373 		if (node->tn_type == VREG)
1374 			tmpfs_truncate(vp, 0);
1375 		vrecycle(vp);
1376 	} else {
1377 		TMPFS_NODE_UNLOCK(node);
1378 	}
1379 
1380 	return 0;
1381 }
1382 
1383 /* --------------------------------------------------------------------- */
1384 
1385 int
1386 tmpfs_reclaim(struct vop_reclaim_args *v)
1387 {
1388 	struct vnode *vp = v->a_vp;
1389 	struct tmpfs_mount *tmp;
1390 	struct tmpfs_node *node;
1391 
1392 	node = VP_TO_TMPFS_NODE(vp);
1393 	tmp = VFS_TO_TMPFS(vp->v_mount);
1394 
1395 	tmpfs_free_vp(vp);
1396 
1397 	/*
1398 	 * If the node referenced by this vnode was deleted by the
1399 	 * user, we must free its associated data structures now that
1400 	 * the vnode is being reclaimed.
1401 	 *
1402 	 * Directories have an extra link ref.
1403 	 */
1404 	TMPFS_NODE_LOCK(node);
1405 	if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1406 	    (node->tn_links == 0 ||
1407 	     (node->tn_links == 1 && node->tn_type == VDIR &&
1408 	      node->tn_dir.tn_parent)))
1409 	{
1410 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1411 		tmpfs_free_node(tmp, node);
1412 		/* eats the lock */
1413 	} else {
1414 		TMPFS_NODE_UNLOCK(node);
1415 	}
1416 
1417 	KKASSERT(vp->v_data == NULL);
1418 	return 0;
1419 }
1420 
1421 /* --------------------------------------------------------------------- */
1422 
1423 static int
1424 tmpfs_print(struct vop_print_args *v)
1425 {
1426 	struct vnode *vp = v->a_vp;
1427 
1428 	struct tmpfs_node *node;
1429 
1430 	node = VP_TO_TMPFS_NODE(vp);
1431 
1432 	kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1433 	    node, node->tn_flags, node->tn_links);
1434 	kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1435 	    node->tn_mode, node->tn_uid, node->tn_gid,
1436 	    (uintmax_t)node->tn_size, node->tn_status);
1437 
1438 	if (vp->v_type == VFIFO)
1439 		fifo_printinfo(vp);
1440 
1441 	kprintf("\n");
1442 
1443 	return 0;
1444 }
1445 
1446 /* --------------------------------------------------------------------- */
1447 
1448 static int
1449 tmpfs_pathconf(struct vop_pathconf_args *v)
1450 {
1451 	int name = v->a_name;
1452 	register_t *retval = v->a_retval;
1453 
1454 	int error;
1455 
1456 	error = 0;
1457 
1458 	switch (name) {
1459 	case _PC_LINK_MAX:
1460 		*retval = LINK_MAX;
1461 		break;
1462 
1463 	case _PC_NAME_MAX:
1464 		*retval = NAME_MAX;
1465 		break;
1466 
1467 	case _PC_PATH_MAX:
1468 		*retval = PATH_MAX;
1469 		break;
1470 
1471 	case _PC_PIPE_BUF:
1472 		*retval = PIPE_BUF;
1473 		break;
1474 
1475 	case _PC_CHOWN_RESTRICTED:
1476 		*retval = 1;
1477 		break;
1478 
1479 	case _PC_NO_TRUNC:
1480 		*retval = 1;
1481 		break;
1482 
1483 	case _PC_SYNC_IO:
1484 		*retval = 1;
1485 		break;
1486 
1487 	case _PC_FILESIZEBITS:
1488 		*retval = 0; /* XXX Don't know which value should I return. */
1489 		break;
1490 
1491 	default:
1492 		error = EINVAL;
1493 	}
1494 
1495 	return error;
1496 }
1497 
1498 /************************************************************************
1499  *                          KQFILTER OPS                                *
1500  ************************************************************************/
1501 
1502 static void filt_tmpfsdetach(struct knote *kn);
1503 static int filt_tmpfsread(struct knote *kn, long hint);
1504 static int filt_tmpfswrite(struct knote *kn, long hint);
1505 static int filt_tmpfsvnode(struct knote *kn, long hint);
1506 
1507 static struct filterops tmpfsread_filtops =
1508 	{ FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsread };
1509 static struct filterops tmpfswrite_filtops =
1510 	{ FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfswrite };
1511 static struct filterops tmpfsvnode_filtops =
1512 	{ FILTEROP_ISFD, NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1513 
1514 static int
1515 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1516 {
1517 	struct vnode *vp = ap->a_vp;
1518 	struct knote *kn = ap->a_kn;
1519 
1520 	switch (kn->kn_filter) {
1521 	case EVFILT_READ:
1522 		kn->kn_fop = &tmpfsread_filtops;
1523 		break;
1524 	case EVFILT_WRITE:
1525 		kn->kn_fop = &tmpfswrite_filtops;
1526 		break;
1527 	case EVFILT_VNODE:
1528 		kn->kn_fop = &tmpfsvnode_filtops;
1529 		break;
1530 	default:
1531 		return (EOPNOTSUPP);
1532 	}
1533 
1534 	kn->kn_hook = (caddr_t)vp;
1535 
1536 	knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1537 
1538 	return(0);
1539 }
1540 
1541 static void
1542 filt_tmpfsdetach(struct knote *kn)
1543 {
1544 	struct vnode *vp = (void *)kn->kn_hook;
1545 
1546 	knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1547 }
1548 
1549 static int
1550 filt_tmpfsread(struct knote *kn, long hint)
1551 {
1552 	struct vnode *vp = (void *)kn->kn_hook;
1553 	struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
1554 	off_t off;
1555 
1556 	if (hint == NOTE_REVOKE) {
1557 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1558 		return(1);
1559 	}
1560 	off = node->tn_size - kn->kn_fp->f_offset;
1561 	kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1562 	if (kn->kn_sfflags & NOTE_OLDAPI)
1563 		return(1);
1564 
1565 	/*
1566 	 * Handle possible MP race interlock on filter check/write
1567 	 */
1568 	if (kn->kn_data == 0) {
1569 		get_mplock();
1570 		kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1571 		rel_mplock();
1572 	}
1573 	return (kn->kn_data != 0);
1574 }
1575 
1576 static int
1577 filt_tmpfswrite(struct knote *kn, long hint)
1578 {
1579 	if (hint == NOTE_REVOKE)
1580 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1581 	kn->kn_data = 0;
1582 	return (1);
1583 }
1584 
1585 static int
1586 filt_tmpfsvnode(struct knote *kn, long hint)
1587 {
1588 	if (kn->kn_sfflags & hint)
1589 		kn->kn_fflags |= hint;
1590 	if (hint == NOTE_REVOKE) {
1591 		kn->kn_flags |= EV_EOF;
1592 		return (1);
1593 	}
1594 	return (kn->kn_fflags != 0);
1595 }
1596 
1597 
1598 /* --------------------------------------------------------------------- */
1599 
1600 /*
1601  * vnode operations vector used for files stored in a tmpfs file system.
1602  */
1603 struct vop_ops tmpfs_vnode_vops = {
1604 	.vop_default =			vop_defaultop,
1605 	.vop_getpages = 		vop_stdgetpages,
1606 	.vop_putpages = 		vop_stdputpages,
1607 	.vop_ncreate =			tmpfs_ncreate,
1608 	.vop_nresolve =			tmpfs_nresolve,
1609 	.vop_nlookupdotdot =		tmpfs_nlookupdotdot,
1610 	.vop_nmknod =			tmpfs_nmknod,
1611 	.vop_open =			tmpfs_open,
1612 	.vop_close =			tmpfs_close,
1613 	.vop_access =			tmpfs_access,
1614 	.vop_getattr =			tmpfs_getattr,
1615 	.vop_setattr =			tmpfs_setattr,
1616 	.vop_read =			tmpfs_read,
1617 	.vop_write =			tmpfs_write,
1618 	.vop_fsync =			tmpfs_fsync,
1619 	.vop_nremove =			tmpfs_nremove,
1620 	.vop_nlink =			tmpfs_nlink,
1621 	.vop_nrename =			tmpfs_nrename,
1622 	.vop_nmkdir =			tmpfs_nmkdir,
1623 	.vop_nrmdir =			tmpfs_nrmdir,
1624 	.vop_nsymlink =			tmpfs_nsymlink,
1625 	.vop_readdir =			tmpfs_readdir,
1626 	.vop_readlink =			tmpfs_readlink,
1627 	.vop_inactive =			tmpfs_inactive,
1628 	.vop_reclaim =			tmpfs_reclaim,
1629 	.vop_print =			tmpfs_print,
1630 	.vop_pathconf =			tmpfs_pathconf,
1631 	.vop_bmap =			tmpfs_bmap,
1632 	.vop_strategy =			tmpfs_strategy,
1633 	.vop_advlock =			tmpfs_advlock,
1634 	.vop_kqfilter =			tmpfs_kqfilter
1635 };
1636