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