xref: /dflybsd-src/sys/vfs/tmpfs/tmpfs_vnops.c (revision 108fbfcb638dfba95b469dc6dce6190c73a0a932)
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/uio.h>
41 #include <sys/fcntl.h>
42 #include <sys/lockf.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/stat.h>
48 #include <sys/systm.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/vfsops.h>
52 #include <sys/vnode.h>
53 #include <sys/mountctl.h>
54 
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pageout.h>
60 #include <vm/vm_pager.h>
61 #include <vm/swap_pager.h>
62 
63 #include <sys/buf2.h>
64 #include <vm/vm_page2.h>
65 
66 #include <vfs/fifofs/fifo.h>
67 #include <vfs/tmpfs/tmpfs_vnops.h>
68 #include "tmpfs.h"
69 
70 static void tmpfs_strategy_done(struct bio *bio);
71 static void tmpfs_move_pages(vm_object_t src, vm_object_t dst, int movflags);
72 
73 /*
74  * bufcache_mode:
75  *	0	Normal page queue operation on flush.  Try to keep in memory,
76  *		but run through the buffer cache if the system is under memory
77  *		pressure.
78  *
79  *	1	Be a bit more aggressive running writes through the buffer
80  *		cache when the system is under memory pressure.
81  *
82  *	2	Always page to swap (not recommended).
83  */
84 __read_mostly static int tmpfs_cluster_rd_enable = 1;
85 __read_mostly static int tmpfs_cluster_wr_enable = 1;
86 __read_mostly int tmpfs_bufcache_mode = 1;
87 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "TMPFS filesystem");
88 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, cluster_rd_enable, CTLFLAG_RW,
89 		&tmpfs_cluster_rd_enable, 0, "");
90 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, cluster_wr_enable, CTLFLAG_RW,
91 		&tmpfs_cluster_wr_enable, 0, "");
92 SYSCTL_INT(_vfs_tmpfs, OID_AUTO, bufcache_mode, CTLFLAG_RW,
93 		&tmpfs_bufcache_mode, 0, "");
94 
95 #define TMPFS_MOVF_FROMBACKING	0x0001
96 #define TMPFS_MOVF_DEACTIVATE	0x0002
97 
98 
99 static __inline
100 void
101 tmpfs_knote(struct vnode *vp, int flags)
102 {
103 	if (flags)
104 		KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
105 }
106 
107 
108 /* --------------------------------------------------------------------- */
109 
110 static int
111 tmpfs_nresolve(struct vop_nresolve_args *ap)
112 {
113 	struct vnode *dvp = ap->a_dvp;
114 	struct vnode *vp = NULL;
115 	struct namecache *ncp = ap->a_nch->ncp;
116 	struct tmpfs_node *tnode;
117 	struct tmpfs_dirent *de;
118 	struct tmpfs_node *dnode;
119 	int error;
120 
121 	dnode = VP_TO_TMPFS_DIR(dvp);
122 
123 	TMPFS_NODE_LOCK_SH(dnode);
124 loop:
125 	de = tmpfs_dir_lookup(dnode, NULL, ncp);
126 	if (de == NULL) {
127 		error = ENOENT;
128 	} else {
129 		/*
130 		 * Allocate a vnode for the node we found.  Use
131 		 * tmpfs_alloc_vp()'s deadlock handling mode.
132 		 */
133 		tnode = de->td_node;
134 		error = tmpfs_alloc_vp(dvp->v_mount, dnode, tnode,
135 				       LK_EXCLUSIVE | LK_RETRY, &vp);
136 		if (error == EAGAIN)
137 			goto loop;
138 		if (error)
139 			goto out;
140 		KKASSERT(vp);
141 	}
142 
143 out:
144 	TMPFS_NODE_UNLOCK(dnode);
145 
146 	if ((dnode->tn_status & TMPFS_NODE_ACCESSED) == 0) {
147 		TMPFS_NODE_LOCK(dnode);
148 		dnode->tn_status |= TMPFS_NODE_ACCESSED;
149 		TMPFS_NODE_UNLOCK(dnode);
150 	}
151 
152 	/*
153 	 * Store the result of this lookup in the cache.  Avoid this if the
154 	 * request was for creation, as it does not improve timings on
155 	 * emprical tests.
156 	 */
157 	if (vp) {
158 		vn_unlock(vp);
159 		cache_setvp(ap->a_nch, vp);
160 		vrele(vp);
161 	} else if (error == ENOENT) {
162 		cache_setvp(ap->a_nch, NULL);
163 	}
164 	return (error);
165 }
166 
167 static int
168 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
169 {
170 	struct vnode *dvp = ap->a_dvp;
171 	struct vnode **vpp = ap->a_vpp;
172 	struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
173 	struct ucred *cred = ap->a_cred;
174 	int error;
175 
176 	*vpp = NULL;
177 
178 	/* Check accessibility of requested node as a first step. */
179 	error = VOP_ACCESS(dvp, VEXEC, cred);
180 	if (error != 0)
181 		return error;
182 
183 	if (dnode->tn_dir.tn_parent != NULL) {
184 		/* Allocate a new vnode on the matching entry. */
185 		error = tmpfs_alloc_vp(dvp->v_mount,
186 				       NULL, dnode->tn_dir.tn_parent,
187 				       LK_EXCLUSIVE | LK_RETRY, vpp);
188 
189 		if (*vpp)
190 			vn_unlock(*vpp);
191 	}
192 	return (*vpp == NULL) ? ENOENT : 0;
193 }
194 
195 /* --------------------------------------------------------------------- */
196 
197 static int
198 tmpfs_ncreate(struct vop_ncreate_args *ap)
199 {
200 	struct vnode *dvp = ap->a_dvp;
201 	struct vnode **vpp = ap->a_vpp;
202 	struct namecache *ncp = ap->a_nch->ncp;
203 	struct vattr *vap = ap->a_vap;
204 	struct ucred *cred = ap->a_cred;
205 	int error;
206 
207 	KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
208 
209 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
210 	if (error == 0) {
211 		cache_setunresolved(ap->a_nch);
212 		cache_setvp(ap->a_nch, *vpp);
213 		tmpfs_knote(dvp, NOTE_WRITE);
214 	}
215 	return (error);
216 }
217 /* --------------------------------------------------------------------- */
218 
219 static int
220 tmpfs_nmknod(struct vop_nmknod_args *ap)
221 {
222 	struct vnode *dvp = ap->a_dvp;
223 	struct vnode **vpp = ap->a_vpp;
224 	struct namecache *ncp = ap->a_nch->ncp;
225 	struct vattr *vap = ap->a_vap;
226 	struct ucred *cred = ap->a_cred;
227 	int error;
228 
229 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
230 	    vap->va_type != VFIFO) {
231 		return (EINVAL);
232 	}
233 
234 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
235 	if (error == 0) {
236 		cache_setunresolved(ap->a_nch);
237 		cache_setvp(ap->a_nch, *vpp);
238 		tmpfs_knote(dvp, NOTE_WRITE);
239 	}
240 	return error;
241 }
242 
243 /* --------------------------------------------------------------------- */
244 
245 static int
246 tmpfs_open(struct vop_open_args *ap)
247 {
248 	struct vnode *vp = ap->a_vp;
249 	int mode = ap->a_mode;
250 	struct tmpfs_node *node;
251 	int error;
252 
253 	node = VP_TO_TMPFS_NODE(vp);
254 
255 #if 0
256 	/* The file is still active but all its names have been removed
257 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
258 	 * it is about to die. */
259 	if (node->tn_links < 1)
260 		return (ENOENT);
261 #endif
262 
263 	/* If the file is marked append-only, deny write requests. */
264 	if ((node->tn_flags & APPEND) &&
265 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
266 		error = EPERM;
267 	} else {
268 		if (node->tn_reg.tn_pages_in_aobj) {
269 			TMPFS_NODE_LOCK(node);
270 			if (node->tn_reg.tn_pages_in_aobj) {
271 				tmpfs_move_pages(node->tn_reg.tn_aobj,
272 						 vp->v_object,
273 						 TMPFS_MOVF_FROMBACKING);
274 				node->tn_reg.tn_pages_in_aobj = 0;
275 			}
276 			TMPFS_NODE_UNLOCK(node);
277 		}
278 		error = vop_stdopen(ap);
279 	}
280 
281 	return (error);
282 }
283 
284 /* --------------------------------------------------------------------- */
285 
286 static int
287 tmpfs_close(struct vop_close_args *ap)
288 {
289 	struct vnode *vp = ap->a_vp;
290 	struct tmpfs_node *node;
291 	int error;
292 
293 	node = VP_TO_TMPFS_NODE(vp);
294 
295 	if (node->tn_links > 0) {
296 		/*
297 		 * Update node times.  No need to do it if the node has
298 		 * been deleted, because it will vanish after we return.
299 		 */
300 		tmpfs_update(vp);
301 	}
302 
303 	error = vop_stdclose(ap);
304 
305 	return (error);
306 }
307 
308 /* --------------------------------------------------------------------- */
309 
310 int
311 tmpfs_access(struct vop_access_args *ap)
312 {
313 	struct vnode *vp = ap->a_vp;
314 	int error;
315 	struct tmpfs_node *node;
316 
317 	node = VP_TO_TMPFS_NODE(vp);
318 
319 	switch (vp->v_type) {
320 	case VDIR:
321 		/* FALLTHROUGH */
322 	case VLNK:
323 		/* FALLTHROUGH */
324 	case VREG:
325 		if ((ap->a_mode & VWRITE) &&
326 	            (vp->v_mount->mnt_flag & MNT_RDONLY)) {
327 			error = EROFS;
328 			goto out;
329 		}
330 		break;
331 
332 	case VBLK:
333 		/* FALLTHROUGH */
334 	case VCHR:
335 		/* FALLTHROUGH */
336 	case VSOCK:
337 		/* FALLTHROUGH */
338 	case VFIFO:
339 		break;
340 
341 	default:
342 		error = EINVAL;
343 		goto out;
344 	}
345 
346 	if ((ap->a_mode & VWRITE) && (node->tn_flags & IMMUTABLE)) {
347 		error = EPERM;
348 		goto out;
349 	}
350 
351 	error = vop_helper_access(ap, node->tn_uid, node->tn_gid,
352 			          node->tn_mode, 0);
353 out:
354 	return error;
355 }
356 
357 /* --------------------------------------------------------------------- */
358 
359 int
360 tmpfs_getattr(struct vop_getattr_args *ap)
361 {
362 	struct vnode *vp = ap->a_vp;
363 	struct vattr *vap = ap->a_vap;
364 	struct tmpfs_node *node;
365 
366 	node = VP_TO_TMPFS_NODE(vp);
367 
368 	tmpfs_update(vp);
369 
370 	vap->va_type = vp->v_type;
371 	vap->va_mode = node->tn_mode;
372 	vap->va_nlink = node->tn_links;
373 	vap->va_uid = node->tn_uid;
374 	vap->va_gid = node->tn_gid;
375 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
376 	vap->va_fileid = node->tn_id;
377 	vap->va_size = node->tn_size;
378 	vap->va_blocksize = PAGE_SIZE;
379 	vap->va_atime.tv_sec = node->tn_atime;
380 	vap->va_atime.tv_nsec = node->tn_atimensec;
381 	vap->va_mtime.tv_sec = node->tn_mtime;
382 	vap->va_mtime.tv_nsec = node->tn_mtimensec;
383 	vap->va_ctime.tv_sec = node->tn_ctime;
384 	vap->va_ctime.tv_nsec = node->tn_ctimensec;
385 	vap->va_gen = node->tn_gen;
386 	vap->va_flags = node->tn_flags;
387 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
388 		vap->va_rmajor = umajor(node->tn_rdev);
389 		vap->va_rminor = uminor(node->tn_rdev);
390 	}
391 	vap->va_bytes = round_page(node->tn_size);
392 	vap->va_filerev = 0;
393 
394 	return 0;
395 }
396 
397 /* --------------------------------------------------------------------- */
398 
399 int
400 tmpfs_getattr_quick(struct vop_getattr_args *ap)
401 {
402 	struct vnode *vp = ap->a_vp;
403 	struct vattr *vap = ap->a_vap;
404 	struct tmpfs_node *node;
405 
406 	node = VP_TO_TMPFS_NODE(vp);
407 
408 	tmpfs_update(vp);
409 
410 	vap->va_type = vp->v_type;
411 	vap->va_mode = node->tn_mode;
412 	vap->va_nlink = node->tn_links;
413 	vap->va_uid = node->tn_uid;
414 	vap->va_gid = node->tn_gid;
415 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
416 	vap->va_fileid = node->tn_id;
417 	vap->va_size = node->tn_size;
418 	vap->va_blocksize = PAGE_SIZE;
419 	vap->va_gen = node->tn_gen;
420 	vap->va_flags = node->tn_flags;
421 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
422 		vap->va_rmajor = umajor(node->tn_rdev);
423 		vap->va_rminor = uminor(node->tn_rdev);
424 	}
425 	vap->va_bytes = -1;
426 	vap->va_filerev = 0;
427 
428 	return 0;
429 }
430 
431 
432 /* --------------------------------------------------------------------- */
433 
434 int
435 tmpfs_setattr(struct vop_setattr_args *ap)
436 {
437 	struct vnode *vp = ap->a_vp;
438 	struct vattr *vap = ap->a_vap;
439 	struct ucred *cred = ap->a_cred;
440 	struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
441 	int error = 0;
442 	int kflags = 0;
443 
444 	TMPFS_NODE_LOCK(node);
445 	if (error == 0 && (vap->va_flags != VNOVAL)) {
446 		error = tmpfs_chflags(vp, vap->va_flags, cred);
447 		kflags |= NOTE_ATTRIB;
448 	}
449 
450 	if (error == 0 && (vap->va_size != VNOVAL)) {
451 		/* restore any saved pages before proceeding */
452 		if (node->tn_reg.tn_pages_in_aobj) {
453 			tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
454 					 TMPFS_MOVF_FROMBACKING |
455 					 TMPFS_MOVF_DEACTIVATE);
456 			node->tn_reg.tn_pages_in_aobj = 0;
457 		}
458 		if (vap->va_size > node->tn_size)
459 			kflags |= NOTE_WRITE | NOTE_EXTEND;
460 		else
461 			kflags |= NOTE_WRITE;
462 		error = tmpfs_chsize(vp, vap->va_size, cred);
463 	}
464 
465 	if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
466 			   vap->va_gid != (gid_t)VNOVAL)) {
467 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
468 		kflags |= NOTE_ATTRIB;
469 	}
470 
471 	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL)) {
472 		error = tmpfs_chmod(vp, vap->va_mode, cred);
473 		kflags |= NOTE_ATTRIB;
474 	}
475 
476 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
477 	    vap->va_atime.tv_nsec != VNOVAL) ||
478 	    (vap->va_mtime.tv_sec != VNOVAL &&
479 	    vap->va_mtime.tv_nsec != VNOVAL) )) {
480 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
481 				      vap->va_vaflags, cred);
482 		kflags |= NOTE_ATTRIB;
483 	}
484 
485 	/*
486 	 * Update the node times.  We give preference to the error codes
487 	 * generated by this function rather than the ones that may arise
488 	 * from tmpfs_update.
489 	 */
490 	tmpfs_update(vp);
491 	TMPFS_NODE_UNLOCK(node);
492 	tmpfs_knote(vp, kflags);
493 
494 	return (error);
495 }
496 
497 /* --------------------------------------------------------------------- */
498 
499 /*
500  * fsync is usually a NOP, but we must take action when unmounting or
501  * when recycling.
502  */
503 static int
504 tmpfs_fsync(struct vop_fsync_args *ap)
505 {
506 	struct tmpfs_node *node;
507 	struct vnode *vp = ap->a_vp;
508 
509 	node = VP_TO_TMPFS_NODE(vp);
510 
511 	/*
512 	 * tmpfs vnodes typically remain dirty, avoid long syncer scans
513 	 * by forcing removal from the syncer list.
514 	 */
515 	vn_syncer_remove(vp, 1);
516 
517 	tmpfs_update(vp);
518 	if (vp->v_type == VREG) {
519 		if (vp->v_flag & VRECLAIMED) {
520 			if (node->tn_links == 0)
521 				tmpfs_truncate(vp, 0);
522 			else
523 				vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
524 		}
525 	}
526 
527 	return 0;
528 }
529 
530 /* --------------------------------------------------------------------- */
531 
532 static int
533 tmpfs_read(struct vop_read_args *ap)
534 {
535 	struct buf *bp;
536 	struct vnode *vp = ap->a_vp;
537 	struct uio *uio = ap->a_uio;
538 	struct tmpfs_node *node;
539 	off_t base_offset;
540 	size_t offset;
541 	size_t len;
542 	size_t resid;
543 	int error;
544 	int seqcount;
545 
546 	/*
547 	 * Check the basics
548 	 */
549 	if (uio->uio_offset < 0)
550 		return (EINVAL);
551 	if (vp->v_type != VREG)
552 		return (EINVAL);
553 
554 	/*
555 	 * Extract node, try to shortcut the operation through
556 	 * the VM page cache, allowing us to avoid buffer cache
557 	 * overheads.
558 	 */
559 	node = VP_TO_TMPFS_NODE(vp);
560         resid = uio->uio_resid;
561 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
562         error = vop_helper_read_shortcut(ap);
563         if (error)
564                 return error;
565         if (uio->uio_resid == 0) {
566 		if (resid)
567 			goto finished;
568 		return error;
569 	}
570 
571 	/*
572 	 * restore any saved pages before proceeding
573 	 */
574 	if (node->tn_reg.tn_pages_in_aobj) {
575 		TMPFS_NODE_LOCK(node);
576 		if (node->tn_reg.tn_pages_in_aobj) {
577 			tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
578 					 TMPFS_MOVF_FROMBACKING);
579 			node->tn_reg.tn_pages_in_aobj = 0;
580 		}
581 		TMPFS_NODE_UNLOCK(node);
582 	}
583 
584 	/*
585 	 * Fall-through to our normal read code.
586 	 */
587 	while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
588 		/*
589 		 * Use buffer cache I/O (via tmpfs_strategy)
590 		 */
591 		offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
592 		base_offset = (off_t)uio->uio_offset - offset;
593 		bp = getcacheblk(vp, base_offset,
594 				 node->tn_blksize, GETBLK_KVABIO);
595 		if (bp == NULL) {
596 			if (tmpfs_cluster_rd_enable) {
597 				error = cluster_readx(vp, node->tn_size,
598 						     base_offset,
599 						     node->tn_blksize,
600 						     B_NOTMETA | B_KVABIO,
601 						     uio->uio_resid,
602 						     seqcount * MAXBSIZE,
603 						     &bp);
604 			} else {
605 				error = bread_kvabio(vp, base_offset,
606 						     node->tn_blksize, &bp);
607 			}
608 			if (error) {
609 				brelse(bp);
610 				kprintf("tmpfs_read bread error %d\n", error);
611 				break;
612 			}
613 
614 			/*
615 			 * tmpfs pretty much fiddles directly with the VM
616 			 * system, don't let it exhaust it or we won't play
617 			 * nice with other processes.
618 			 *
619 			 * Only do this if the VOP is coming from a normal
620 			 * read/write.  The VM system handles the case for
621 			 * UIO_NOCOPY.
622 			 */
623 			if (uio->uio_segflg != UIO_NOCOPY)
624 				vm_wait_nominal();
625 		}
626 		bp->b_flags |= B_CLUSTEROK;
627 		bkvasync(bp);
628 
629 		/*
630 		 * Figure out how many bytes we can actually copy this loop.
631 		 */
632 		len = node->tn_blksize - offset;
633 		if (len > uio->uio_resid)
634 			len = uio->uio_resid;
635 		if (len > node->tn_size - uio->uio_offset)
636 			len = (size_t)(node->tn_size - uio->uio_offset);
637 
638 		error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
639 		bqrelse(bp);
640 		if (error) {
641 			kprintf("tmpfs_read uiomove error %d\n", error);
642 			break;
643 		}
644 	}
645 
646 finished:
647 	if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
648 		TMPFS_NODE_LOCK(node);
649 		node->tn_status |= TMPFS_NODE_ACCESSED;
650 		TMPFS_NODE_UNLOCK(node);
651 	}
652 	return (error);
653 }
654 
655 static int
656 tmpfs_write(struct vop_write_args *ap)
657 {
658 	struct buf *bp;
659 	struct vnode *vp = ap->a_vp;
660 	struct uio *uio = ap->a_uio;
661 	struct thread *td = uio->uio_td;
662 	struct tmpfs_node *node;
663 	boolean_t extended;
664 	off_t oldsize;
665 	int error;
666 	off_t base_offset;
667 	size_t offset;
668 	size_t len;
669 	struct rlimit limit;
670 	int trivial = 0;
671 	int kflags = 0;
672 	int seqcount;
673 
674 	error = 0;
675 	if (uio->uio_resid == 0) {
676 		return error;
677 	}
678 
679 	node = VP_TO_TMPFS_NODE(vp);
680 
681 	if (vp->v_type != VREG)
682 		return (EINVAL);
683 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
684 
685 	TMPFS_NODE_LOCK(node);
686 
687 	/*
688 	 * restore any saved pages before proceeding
689 	 */
690 	if (node->tn_reg.tn_pages_in_aobj) {
691 		tmpfs_move_pages(node->tn_reg.tn_aobj, vp->v_object,
692 				 TMPFS_MOVF_FROMBACKING);
693 		node->tn_reg.tn_pages_in_aobj = 0;
694 	}
695 
696 	oldsize = node->tn_size;
697 	if (ap->a_ioflag & IO_APPEND)
698 		uio->uio_offset = node->tn_size;
699 
700 	/*
701 	 * Check for illegal write offsets.
702 	 */
703 	if (uio->uio_offset + uio->uio_resid >
704 	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize) {
705 		error = EFBIG;
706 		goto done;
707 	}
708 
709 	/*
710 	 * NOTE: Ignore if UIO does not come from a user thread (e.g. VN).
711 	 */
712 	if (vp->v_type == VREG && td != NULL && td->td_lwp != NULL) {
713 		error = kern_getrlimit(RLIMIT_FSIZE, &limit);
714 		if (error)
715 			goto done;
716 		if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
717 			ksignal(td->td_proc, SIGXFSZ);
718 			error = EFBIG;
719 			goto done;
720 		}
721 	}
722 
723 	/*
724 	 * Extend the file's size if necessary
725 	 */
726 	extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
727 
728 	while (uio->uio_resid > 0) {
729 		/*
730 		 * Don't completely blow out running buffer I/O
731 		 * when being hit from the pageout daemon.
732 		 */
733 		if (uio->uio_segflg == UIO_NOCOPY &&
734 		    (ap->a_ioflag & IO_RECURSE) == 0) {
735 			bwillwrite(node->tn_blksize);
736 		}
737 
738 		/*
739 		 * Use buffer cache I/O (via tmpfs_strategy)
740 		 *
741 		 * Calculate the maximum bytes we can write to the buffer at
742 		 * this offset (after resizing).
743 		 */
744 		offset = (size_t)uio->uio_offset & TMPFS_BLKMASK64;
745 		base_offset = (off_t)uio->uio_offset - offset;
746 		len = uio->uio_resid;
747 		if (len > TMPFS_BLKSIZE - offset)
748 			len = TMPFS_BLKSIZE - offset;
749 
750 		if ((uio->uio_offset + len) > node->tn_size) {
751 			trivial = (uio->uio_offset <= node->tn_size);
752 			error = tmpfs_reg_resize(vp, uio->uio_offset + len,
753 						 trivial);
754 			if (error)
755 				break;
756 		}
757 
758 		/*
759 		 * Read to fill in any gaps.  Theoretically we could
760 		 * optimize this if the write covers the entire buffer
761 		 * and is not a UIO_NOCOPY write, however this can lead
762 		 * to a security violation exposing random kernel memory
763 		 * (whatever junk was in the backing VM pages before).
764 		 *
765 		 * So just use bread() to do the right thing.
766 		 */
767 		error = bread_kvabio(vp, base_offset, node->tn_blksize, &bp);
768 		bkvasync(bp);
769 		error = uiomovebp(bp, (char *)bp->b_data + offset, len, uio);
770 		if (error) {
771 			kprintf("tmpfs_write uiomove error %d\n", error);
772 			brelse(bp);
773 			break;
774 		}
775 
776 		if (uio->uio_offset > node->tn_size) {
777 			node->tn_size = uio->uio_offset;
778 			kflags |= NOTE_EXTEND;
779 		}
780 		kflags |= NOTE_WRITE;
781 
782 		/*
783 		 * UIO_NOCOPY is a sensitive state due to potentially being
784 		 * issued from the pageout daemon while in a low-memory
785 		 * situation.  However, in order to cluster the I/O nicely
786 		 * (e.g. 64KB+ writes instead of 16KB writes), we still try
787 		 * to follow the same semantics that any other filesystem
788 		 * might use.
789 		 *
790 		 * For the normal case we buwrite(), dirtying the underlying
791 		 * VM pages instead of dirtying the buffer and releasing the
792 		 * buffer as a clean buffer.  This allows tmpfs to use
793 		 * essentially all available memory to cache file data.
794 		 * If we used bdwrite() the buffer cache would wind up
795 		 * flushing the data to swap too quickly.
796 		 *
797 		 * But because tmpfs can seriously load the VM system we
798 		 * fall-back to using bdwrite() when free memory starts
799 		 * to get low.  This shifts the load away from the VM system
800 		 * and makes tmpfs act more like a normal filesystem with
801 		 * regards to disk activity.
802 		 *
803 		 * tmpfs pretty much fiddles directly with the VM
804 		 * system, don't let it exhaust it or we won't play
805 		 * nice with other processes.  Only do this if the
806 		 * VOP is coming from a normal read/write.  The VM system
807 		 * handles the case for UIO_NOCOPY.
808 		 */
809 		bp->b_flags |= B_CLUSTEROK;
810 		if (uio->uio_segflg == UIO_NOCOPY) {
811 			/*
812 			 * Flush from the pageout daemon, deal with potentially
813 			 * very heavy tmpfs write activity causing long stalls
814 			 * in the pageout daemon before pages get to free/cache.
815 			 *
816 			 * We have to be careful not to bypass the page queues
817 			 * entirely or we can cause write-read thrashing and
818 			 * delay the paging of data that is more pageable then
819 			 * our current data.
820 			 *
821 			 * (a) Under severe pressure setting B_DIRECT will
822 			 *     cause a buffer release to try to free the
823 			 *     underlying pages.
824 			 *
825 			 * (b) Under modest memory pressure the B_AGE flag
826 			 *     we retire the buffer and its underlying pages
827 			 *     more quickly than normal.
828 			 *
829 			 *     We could also force this by setting B_NOTMETA
830 			 *     but that might have other unintended side-
831 			 *     effects (e.g. setting PG_NOTMETA on the VM page).
832 			 *
833 			 * (c) For the pageout->putpages->generic_putpages->
834 			 *     UIO_NOCOPY-write (here), issuing an immediate
835 			 *     write prevents any real clustering from
836 			 *     happening because the buffers probably aren't
837 			 *     (yet) marked dirty, or lost due to prior use
838 			 *     of buwrite().  Try to use the normal
839 			 *     cluster_write() mechanism for performance.
840 			 *
841 			 * Hopefully this will unblock the VM system more
842 			 * quickly under extreme tmpfs write load.
843 			 */
844 			if (tmpfs_bufcache_mode >= 1) {
845 				if (vm_page_count_min(vm_page_free_hysteresis))
846 					bp->b_flags |= B_DIRECT | B_TTC;
847 				if (vm_pages_needed || vm_paging_needed(0))
848 					bp->b_flags |= B_AGE;
849 			}
850 			bp->b_act_count = 0;	/* buffer->deactivate pgs */
851 			if (tmpfs_cluster_wr_enable &&
852 			    (ap->a_ioflag & (IO_SYNC | IO_DIRECT)) == 0) {
853 				cluster_write(bp, node->tn_size,
854 					      node->tn_blksize, seqcount);
855 			} else {
856 				cluster_awrite(bp);
857 			}
858 		} else if (vm_pages_needed || vm_paging_needed(0) ||
859 			   tmpfs_bufcache_mode >= 2) {
860 			/*
861 			 * If the pageout daemon is running we cycle the
862 			 * write through the buffer cache normally to
863 			 * pipeline the flush, thus avoiding adding any
864 			 * more memory pressure to the pageout daemon.
865 			 */
866 			bp->b_act_count = 0;	/* buffer->deactivate pgs */
867 			if (tmpfs_cluster_wr_enable) {
868 				cluster_write(bp, node->tn_size,
869 					      node->tn_blksize, seqcount);
870 			} else {
871 				bdwrite(bp);
872 			}
873 		} else {
874 			/*
875 			 * Otherwise run the buffer directly through to the
876 			 * backing VM store, leaving the buffer clean so
877 			 * buffer limits do not force early flushes to swap.
878 			 */
879 			buwrite(bp);
880 			/*vm_wait_nominal();*/
881 		}
882 
883 		if (bp->b_error) {
884 			kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
885 			break;
886 		}
887 	}
888 
889 	if (error) {
890 		if (extended) {
891 			(void)tmpfs_reg_resize(vp, oldsize, trivial);
892 			kflags &= ~NOTE_EXTEND;
893 		}
894 		goto done;
895 	}
896 
897 	/*
898 	 * Currently we don't set the mtime on files modified via mmap()
899 	 * because we can't tell the difference between those modifications
900 	 * and an attempt by the pageout daemon to flush tmpfs pages to
901 	 * swap.
902 	 *
903 	 * This is because in order to defer flushes as long as possible
904 	 * buwrite() works by marking the underlying VM pages dirty in
905 	 * order to be able to dispose of the buffer cache buffer without
906 	 * flushing it.
907 	 */
908 	if (uio->uio_segflg == UIO_NOCOPY) {
909 		if (vp->v_flag & VLASTWRITETS) {
910 			node->tn_mtime = vp->v_lastwrite_ts.tv_sec;
911 			node->tn_mtimensec = vp->v_lastwrite_ts.tv_nsec;
912 		}
913 	} else {
914 		node->tn_status |= TMPFS_NODE_MODIFIED;
915 		vclrflags(vp, VLASTWRITETS);
916 	}
917 
918 	if (extended)
919 		node->tn_status |= TMPFS_NODE_CHANGED;
920 
921 	if (node->tn_mode & (S_ISUID | S_ISGID)) {
922 		if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
923 			node->tn_mode &= ~(S_ISUID | S_ISGID);
924 	}
925 done:
926 	TMPFS_NODE_UNLOCK(node);
927 	if (kflags)
928 		tmpfs_knote(vp, kflags);
929 
930 	return(error);
931 }
932 
933 static int
934 tmpfs_advlock(struct vop_advlock_args *ap)
935 {
936 	struct tmpfs_node *node;
937 	struct vnode *vp = ap->a_vp;
938 	int error;
939 
940 	node = VP_TO_TMPFS_NODE(vp);
941 	error = (lf_advlock(ap, &node->tn_advlock, node->tn_size));
942 
943 	return (error);
944 }
945 
946 /*
947  * The strategy function is typically only called when memory pressure
948  * forces the system to attempt to pageout pages.  It can also be called
949  * by [n]vtruncbuf() when a truncation cuts a page in half.  Normal write
950  * operations
951  *
952  * We set VKVABIO for VREG files so bp->b_data may not be synchronized to
953  * our cpu.  swap_pager_strategy() is all we really use, and it directly
954  * supports this.
955  */
956 static int
957 tmpfs_strategy(struct vop_strategy_args *ap)
958 {
959 	struct bio *bio = ap->a_bio;
960 	struct bio *nbio;
961 	struct buf *bp = bio->bio_buf;
962 	struct vnode *vp = ap->a_vp;
963 	struct tmpfs_node *node;
964 	vm_object_t uobj;
965 	vm_page_t m;
966 	int i;
967 
968 	if (vp->v_type != VREG) {
969 		bp->b_resid = bp->b_bcount;
970 		bp->b_flags |= B_ERROR | B_INVAL;
971 		bp->b_error = EINVAL;
972 		biodone(bio);
973 		return(0);
974 	}
975 
976 	node = VP_TO_TMPFS_NODE(vp);
977 
978 	uobj = node->tn_reg.tn_aobj;
979 
980 	/*
981 	 * Don't bother flushing to swap if there is no swap, just
982 	 * ensure that the pages are marked as needing a commit (still).
983 	 */
984 	if (bp->b_cmd == BUF_CMD_WRITE && vm_swap_size == 0) {
985 		for (i = 0; i < bp->b_xio.xio_npages; ++i) {
986 			m = bp->b_xio.xio_pages[i];
987 			vm_page_need_commit(m);
988 		}
989 		bp->b_resid = 0;
990 		bp->b_error = 0;
991 		biodone(bio);
992 	} else {
993 #if 0
994 		/*
995 		 * XXX removed, this does not work well because under heavy
996 		 * filesystem loads it often
997 		 * forces the data to be read right back in again after
998 		 * being written due to bypassing normal LRU operation.
999 		 *
1000 		 * Tell the buffer cache to try to recycle the pages
1001 		 * to PQ_CACHE on release.
1002 		 */
1003 		if (tmpfs_bufcache_mode >= 2 ||
1004 		    (tmpfs_bufcache_mode == 1 && vm_paging_needed(0))) {
1005 			bp->b_flags |= B_TTC;
1006 		}
1007 #endif
1008 		nbio = push_bio(bio);
1009 		nbio->bio_done = tmpfs_strategy_done;
1010 		nbio->bio_offset = bio->bio_offset;
1011 		swap_pager_strategy(uobj, nbio);
1012 	}
1013 	return 0;
1014 }
1015 
1016 /*
1017  * If we were unable to commit the pages to swap make sure they are marked
1018  * as needing a commit (again).  If we were, clear the flag to allow the
1019  * pages to be freed.
1020  *
1021  * Do not error-out the buffer.  In particular, vinvalbuf() needs to
1022  * always work.
1023  */
1024 static void
1025 tmpfs_strategy_done(struct bio *bio)
1026 {
1027 	struct buf *bp;
1028 	vm_page_t m;
1029 	int i;
1030 
1031 	bp = bio->bio_buf;
1032 
1033 	if (bp->b_flags & B_ERROR) {
1034 		bp->b_flags &= ~B_ERROR;
1035 		bp->b_error = 0;
1036 		bp->b_resid = 0;
1037 		for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1038 			m = bp->b_xio.xio_pages[i];
1039 			vm_page_need_commit(m);
1040 		}
1041 	} else {
1042 		for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1043 			m = bp->b_xio.xio_pages[i];
1044 			vm_page_clear_commit(m);
1045 		}
1046 	}
1047 	bio = pop_bio(bio);
1048 	biodone(bio);
1049 }
1050 
1051 /*
1052  * To make write clustering work well make the backing store look
1053  * contiguous to the cluster_*() code.  The swap_strategy() function
1054  * will take it from there.
1055  *
1056  * Use MAXBSIZE-sized chunks as a micro-optimization to make random
1057  * flushes leave full-sized gaps.
1058  */
1059 static int
1060 tmpfs_bmap(struct vop_bmap_args *ap)
1061 {
1062 	if (ap->a_doffsetp != NULL)
1063 		*ap->a_doffsetp = ap->a_loffset;
1064 	if (ap->a_runp != NULL)
1065 		*ap->a_runp = MAXBSIZE - (ap->a_loffset & (MAXBSIZE - 1));
1066 	if (ap->a_runb != NULL)
1067 		*ap->a_runb = ap->a_loffset & (MAXBSIZE - 1);
1068 
1069 	return 0;
1070 }
1071 
1072 /* --------------------------------------------------------------------- */
1073 
1074 static int
1075 tmpfs_nremove(struct vop_nremove_args *ap)
1076 {
1077 	struct vnode *dvp = ap->a_dvp;
1078 	struct namecache *ncp = ap->a_nch->ncp;
1079 	struct vnode *vp;
1080 	int error;
1081 	struct tmpfs_dirent *de;
1082 	struct tmpfs_mount *tmp;
1083 	struct tmpfs_node *dnode;
1084 	struct tmpfs_node *node;
1085 
1086 	/*
1087 	 * We have to acquire the vp from ap->a_nch because we will likely
1088 	 * unresolve the namecache entry, and a vrele/vput is needed to
1089 	 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1090 	 *
1091 	 * We have to use vget to clear any inactive state on the vnode,
1092 	 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1093 	 * will not get called when we release it.
1094 	 */
1095 	error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp);
1096 	KKASSERT(vp->v_mount == dvp->v_mount);
1097 	KKASSERT(error == 0);
1098 	vn_unlock(vp);
1099 
1100 	if (vp->v_type == VDIR) {
1101 		error = EISDIR;
1102 		goto out2;
1103 	}
1104 
1105 	dnode = VP_TO_TMPFS_DIR(dvp);
1106 	node = VP_TO_TMPFS_NODE(vp);
1107 	tmp = VFS_TO_TMPFS(vp->v_mount);
1108 
1109 	TMPFS_NODE_LOCK(dnode);
1110 	TMPFS_NODE_LOCK(node);
1111 	de = tmpfs_dir_lookup(dnode, node, ncp);
1112 	if (de == NULL) {
1113 		error = ENOENT;
1114 		TMPFS_NODE_UNLOCK(node);
1115 		TMPFS_NODE_UNLOCK(dnode);
1116 		goto out;
1117 	}
1118 
1119 	/* Files marked as immutable or append-only cannot be deleted. */
1120 	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
1121 	    (dnode->tn_flags & APPEND)) {
1122 		error = EPERM;
1123 		TMPFS_NODE_UNLOCK(node);
1124 		TMPFS_NODE_UNLOCK(dnode);
1125 		goto out;
1126 	}
1127 
1128 	/* Remove the entry from the directory; as it is a file, we do not
1129 	 * have to change the number of hard links of the directory. */
1130 	tmpfs_dir_detach(dnode, de);
1131 	TMPFS_NODE_UNLOCK(dnode);
1132 
1133 	/* Free the directory entry we just deleted.  Note that the node
1134 	 * referred by it will not be removed until the vnode is really
1135 	 * reclaimed. */
1136 	tmpfs_free_dirent(tmp, de);
1137 
1138 	if (node->tn_links > 0)
1139 		node->tn_status |= TMPFS_NODE_CHANGED;
1140 	TMPFS_NODE_UNLOCK(node);
1141 
1142 	cache_unlink(ap->a_nch);
1143 	tmpfs_knote(vp, NOTE_DELETE);
1144 	error = 0;
1145 
1146 out:
1147 	if (error == 0)
1148 		tmpfs_knote(dvp, NOTE_WRITE);
1149 out2:
1150 	vrele(vp);
1151 
1152 	return error;
1153 }
1154 
1155 /* --------------------------------------------------------------------- */
1156 
1157 static int
1158 tmpfs_nlink(struct vop_nlink_args *ap)
1159 {
1160 	struct vnode *dvp = ap->a_dvp;
1161 	struct vnode *vp = ap->a_vp;
1162 	struct namecache *ncp = ap->a_nch->ncp;
1163 	struct tmpfs_dirent *de;
1164 	struct tmpfs_node *node;
1165 	struct tmpfs_node *dnode;
1166 	int error;
1167 
1168 	KKASSERT(dvp != vp); /* XXX When can this be false? */
1169 
1170 	node = VP_TO_TMPFS_NODE(vp);
1171 	dnode = VP_TO_TMPFS_NODE(dvp);
1172 	TMPFS_NODE_LOCK(dnode);
1173 
1174 	/* XXX: Why aren't the following two tests done by the caller? */
1175 
1176 	/* Hard links of directories are forbidden. */
1177 	if (vp->v_type == VDIR) {
1178 		error = EPERM;
1179 		goto out;
1180 	}
1181 
1182 	/* Cannot create cross-device links. */
1183 	if (dvp->v_mount != vp->v_mount) {
1184 		error = EXDEV;
1185 		goto out;
1186 	}
1187 
1188 	/* Ensure that we do not overflow the maximum number of links imposed
1189 	 * by the system. */
1190 	KKASSERT(node->tn_links <= LINK_MAX);
1191 	if (node->tn_links >= LINK_MAX) {
1192 		error = EMLINK;
1193 		goto out;
1194 	}
1195 
1196 	/* We cannot create links of files marked immutable or append-only. */
1197 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
1198 		error = EPERM;
1199 		goto out;
1200 	}
1201 
1202 	/* Allocate a new directory entry to represent the node. */
1203 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
1204 				   ncp->nc_name, ncp->nc_nlen, &de);
1205 	if (error != 0)
1206 		goto out;
1207 
1208 	/* Insert the new directory entry into the appropriate directory. */
1209 	tmpfs_dir_attach(dnode, de);
1210 
1211 	/* vp link count has changed, so update node times. */
1212 
1213 	TMPFS_NODE_LOCK(node);
1214 	node->tn_status |= TMPFS_NODE_CHANGED;
1215 	TMPFS_NODE_UNLOCK(node);
1216 	tmpfs_update(vp);
1217 
1218 	tmpfs_knote(vp, NOTE_LINK);
1219 	cache_setunresolved(ap->a_nch);
1220 	cache_setvp(ap->a_nch, vp);
1221 	error = 0;
1222 
1223 out:
1224 	TMPFS_NODE_UNLOCK(dnode);
1225 	if (error == 0)
1226 		tmpfs_knote(dvp, NOTE_WRITE);
1227 	return error;
1228 }
1229 
1230 /* --------------------------------------------------------------------- */
1231 
1232 static int
1233 tmpfs_nrename(struct vop_nrename_args *ap)
1234 {
1235 	struct vnode *fdvp = ap->a_fdvp;
1236 	struct namecache *fncp = ap->a_fnch->ncp;
1237 	struct vnode *fvp = fncp->nc_vp;
1238 	struct vnode *tdvp = ap->a_tdvp;
1239 	struct namecache *tncp = ap->a_tnch->ncp;
1240 	struct vnode *tvp;
1241 	struct tmpfs_dirent *de, *tde;
1242 	struct tmpfs_mount *tmp;
1243 	struct tmpfs_node *fdnode;
1244 	struct tmpfs_node *tdnode;
1245 	struct tmpfs_node *fnode;
1246 	struct tmpfs_node *tnode;
1247 	char *newname;
1248 	char *oldname;
1249 	int error;
1250 
1251 	KKASSERT(fdvp->v_mount == fvp->v_mount);
1252 
1253 	/*
1254 	 * Because tvp can get overwritten we have to vget it instead of
1255 	 * just vref or use it, otherwise it's VINACTIVE flag may not get
1256 	 * cleared and the node won't get destroyed.
1257 	 */
1258 	error = cache_vget(ap->a_tnch, ap->a_cred, LK_SHARED, &tvp);
1259 	if (error == 0) {
1260 		tnode = VP_TO_TMPFS_NODE(tvp);
1261 		vn_unlock(tvp);
1262 	} else {
1263 		tnode = NULL;
1264 	}
1265 
1266 	/* Disallow cross-device renames.
1267 	 * XXX Why isn't this done by the caller? */
1268 	if (fvp->v_mount != tdvp->v_mount ||
1269 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1270 		error = EXDEV;
1271 		goto out;
1272 	}
1273 
1274 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
1275 	tdnode = VP_TO_TMPFS_DIR(tdvp);
1276 
1277 	/* If source and target are the same file, there is nothing to do. */
1278 	if (fvp == tvp) {
1279 		error = 0;
1280 		goto out;
1281 	}
1282 
1283 	fdnode = VP_TO_TMPFS_DIR(fdvp);
1284 	fnode = VP_TO_TMPFS_NODE(fvp);
1285 
1286 	tmpfs_lock4(fdnode, tdnode, fnode, tnode);
1287 
1288 	de = tmpfs_dir_lookup(fdnode, fnode, fncp);
1289 
1290 	/* Avoid manipulating '.' and '..' entries. */
1291 	if (de == NULL) {
1292 		error = ENOENT;
1293 		goto out_locked;
1294 	}
1295 	KKASSERT(de->td_node == fnode);
1296 
1297 	/*
1298 	 * If replacing an entry in the target directory and that entry
1299 	 * is a directory, it must be empty.
1300 	 *
1301 	 * Kern_rename gurantees the destination to be a directory
1302 	 * if the source is one (it does?).
1303 	 */
1304 	if (tvp != NULL) {
1305 		KKASSERT(tnode != NULL);
1306 
1307 		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1308 		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1309 			error = EPERM;
1310 			goto out_locked;
1311 		}
1312 
1313 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1314 			if (tnode->tn_size > 0) {
1315 				error = ENOTEMPTY;
1316 				goto out_locked;
1317 			}
1318 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1319 			error = ENOTDIR;
1320 			goto out_locked;
1321 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1322 			error = EISDIR;
1323 			goto out_locked;
1324 		} else {
1325 			KKASSERT(fnode->tn_type != VDIR &&
1326 				tnode->tn_type != VDIR);
1327 		}
1328 	}
1329 
1330 	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1331 	    (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1332 		error = EPERM;
1333 		goto out_locked;
1334 	}
1335 
1336 	/*
1337 	 * Ensure that we have enough memory to hold the new name, if it
1338 	 * has to be changed.
1339 	 */
1340 	if (fncp->nc_nlen != tncp->nc_nlen ||
1341 	    bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
1342 		newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
1343 				  M_WAITOK | M_NULLOK);
1344 		if (newname == NULL) {
1345 			error = ENOSPC;
1346 			goto out_locked;
1347 		}
1348 		bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1349 		newname[tncp->nc_nlen] = '\0';
1350 	} else {
1351 		newname = NULL;
1352 	}
1353 
1354 	/*
1355 	 * Unlink entry from source directory.  Note that the kernel has
1356 	 * already checked for illegal recursion cases (renaming a directory
1357 	 * into a subdirectory of itself).
1358 	 */
1359 	if (fdnode != tdnode) {
1360 		tmpfs_dir_detach(fdnode, de);
1361 	} else {
1362 		/* XXX depend on namecache lock */
1363 		KKASSERT(de == tmpfs_dir_lookup(fdnode, fnode, fncp));
1364 		RB_REMOVE(tmpfs_dirtree, &fdnode->tn_dir.tn_dirtree, de);
1365 		RB_REMOVE(tmpfs_dirtree_cookie,
1366 			  &fdnode->tn_dir.tn_cookietree, de);
1367 	}
1368 
1369 	/*
1370 	 * Handle any name change.  Swap with newname, we will
1371 	 * deallocate it at the end.
1372 	 */
1373 	if (newname != NULL) {
1374 		oldname = de->td_name;
1375 		de->td_name = newname;
1376 		de->td_namelen = (uint16_t)tncp->nc_nlen;
1377 		newname = oldname;
1378 	}
1379 
1380 	/*
1381 	 * If we are overwriting an entry, we have to remove the old one
1382 	 * from the target directory.
1383 	 */
1384 	if (tvp != NULL) {
1385 		/* Remove the old entry from the target directory. */
1386 		tde = tmpfs_dir_lookup(tdnode, tnode, tncp);
1387 		tmpfs_dir_detach(tdnode, tde);
1388 		tmpfs_knote(tdnode->tn_vnode, NOTE_DELETE);
1389 
1390 		/*
1391 		 * Free the directory entry we just deleted.  Note that the
1392 		 * node referred by it will not be removed until the vnode is
1393 		 * really reclaimed.
1394 		 */
1395 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1396 		/*cache_inval_vp(tvp, CINV_DESTROY);*/
1397 	}
1398 
1399 	/*
1400 	 * Link entry to target directory.  If the entry
1401 	 * represents a directory move the parent linkage
1402 	 * as well.
1403 	 */
1404 	if (fdnode != tdnode) {
1405 		if (de->td_node->tn_type == VDIR) {
1406 			TMPFS_VALIDATE_DIR(fnode);
1407 		}
1408 		tmpfs_dir_attach(tdnode, de);
1409 	} else {
1410 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1411 		RB_INSERT(tmpfs_dirtree, &tdnode->tn_dir.tn_dirtree, de);
1412 		RB_INSERT(tmpfs_dirtree_cookie,
1413 			  &tdnode->tn_dir.tn_cookietree, de);
1414 	}
1415 	tmpfs_unlock4(fdnode, tdnode, fnode, tnode);
1416 
1417 	/*
1418 	 * Finish up
1419 	 */
1420 	if (newname) {
1421 		kfree(newname, tmp->tm_name_zone);
1422 		newname = NULL;
1423 	}
1424 	cache_rename(ap->a_fnch, ap->a_tnch);
1425 	tmpfs_knote(ap->a_fdvp, NOTE_WRITE);
1426 	tmpfs_knote(ap->a_tdvp, NOTE_WRITE);
1427 	if (fnode->tn_vnode)
1428 		tmpfs_knote(fnode->tn_vnode, NOTE_RENAME);
1429 	if (tvp)
1430 		vrele(tvp);
1431 	return 0;
1432 
1433 out_locked:
1434 	tmpfs_unlock4(fdnode, tdnode, fnode, tnode);
1435 out:
1436 	if (tvp)
1437 		vrele(tvp);
1438 	return error;
1439 }
1440 
1441 /* --------------------------------------------------------------------- */
1442 
1443 static int
1444 tmpfs_nmkdir(struct vop_nmkdir_args *ap)
1445 {
1446 	struct vnode *dvp = ap->a_dvp;
1447 	struct vnode **vpp = ap->a_vpp;
1448 	struct namecache *ncp = ap->a_nch->ncp;
1449 	struct vattr *vap = ap->a_vap;
1450 	struct ucred *cred = ap->a_cred;
1451 	int error;
1452 
1453 	KKASSERT(vap->va_type == VDIR);
1454 
1455 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1456 	if (error == 0) {
1457 		cache_setunresolved(ap->a_nch);
1458 		cache_setvp(ap->a_nch, *vpp);
1459 		tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1460 	}
1461 	return error;
1462 }
1463 
1464 /* --------------------------------------------------------------------- */
1465 
1466 static int
1467 tmpfs_nrmdir(struct vop_nrmdir_args *ap)
1468 {
1469 	struct vnode *dvp = ap->a_dvp;
1470 	struct namecache *ncp = ap->a_nch->ncp;
1471 	struct vnode *vp;
1472 	struct tmpfs_dirent *de;
1473 	struct tmpfs_mount *tmp;
1474 	struct tmpfs_node *dnode;
1475 	struct tmpfs_node *node;
1476 	int error;
1477 
1478 	/*
1479 	 * We have to acquire the vp from ap->a_nch because we will likely
1480 	 * unresolve the namecache entry, and a vrele/vput is needed to
1481 	 * trigger the tmpfs_inactive/tmpfs_reclaim sequence.
1482 	 *
1483 	 * We have to use vget to clear any inactive state on the vnode,
1484 	 * otherwise the vnode may remain inactive and thus tmpfs_inactive
1485 	 * will not get called when we release it.
1486 	 */
1487 	error = cache_vget(ap->a_nch, ap->a_cred, LK_SHARED, &vp);
1488 	KKASSERT(error == 0);
1489 	vn_unlock(vp);
1490 
1491 	/*
1492 	 * Prevalidate so we don't hit an assertion later
1493 	 */
1494 	if (vp->v_type != VDIR) {
1495 		error = ENOTDIR;
1496 		goto out;
1497 	}
1498 
1499 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1500 	dnode = VP_TO_TMPFS_DIR(dvp);
1501 	node = VP_TO_TMPFS_DIR(vp);
1502 
1503 	/*
1504 	 *
1505 	 */
1506 	TMPFS_NODE_LOCK(dnode);
1507 	TMPFS_NODE_LOCK(node);
1508 
1509 	/*
1510 	 * Only empty directories can be removed.
1511 	 */
1512 	if (node->tn_size > 0) {
1513 		error = ENOTEMPTY;
1514 		goto out_locked;
1515 	}
1516 
1517 	if ((dnode->tn_flags & APPEND)
1518 	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1519 		error = EPERM;
1520 		goto out_locked;
1521 	}
1522 
1523 	/*
1524 	 * This invariant holds only if we are not trying to
1525 	 * remove "..".  We checked for that above so this is safe now.
1526 	 */
1527 	KKASSERT(node->tn_dir.tn_parent == dnode);
1528 
1529 	/*
1530 	 * Get the directory entry associated with node (vp)
1531 	 */
1532 	de = tmpfs_dir_lookup(dnode, node, ncp);
1533 	KKASSERT(TMPFS_DIRENT_MATCHES(de, ncp->nc_name, ncp->nc_nlen));
1534 
1535 	/* Check flags to see if we are allowed to remove the directory. */
1536 	if ((dnode->tn_flags & APPEND) ||
1537 	    node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1538 		error = EPERM;
1539 		goto out_locked;
1540 	}
1541 
1542 	/* Detach the directory entry from the directory (dnode). */
1543 	tmpfs_dir_detach(dnode, de);
1544 
1545 	/*
1546 	 * Must set parent linkage to NULL (tested by ncreate to disallow
1547 	 * the creation of new files/dirs in a deleted directory)
1548 	 */
1549 	node->tn_status |= TMPFS_NODE_CHANGED;
1550 
1551 	dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1552 			    TMPFS_NODE_MODIFIED;
1553 
1554 	/* Free the directory entry we just deleted.  Note that the node
1555 	 * referred by it will not be removed until the vnode is really
1556 	 * reclaimed. */
1557 	tmpfs_free_dirent(tmp, de);
1558 
1559 	/* Release the deleted vnode (will destroy the node, notify
1560 	 * interested parties and clean it from the cache). */
1561 
1562 	dnode->tn_status |= TMPFS_NODE_CHANGED;
1563 
1564 	TMPFS_NODE_UNLOCK(node);
1565 	TMPFS_NODE_UNLOCK(dnode);
1566 
1567 	tmpfs_update(dvp);
1568 	cache_unlink(ap->a_nch);
1569 	tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1570 	vrele(vp);
1571 	return 0;
1572 
1573 out_locked:
1574 	TMPFS_NODE_UNLOCK(node);
1575 	TMPFS_NODE_UNLOCK(dnode);
1576 
1577 out:
1578 	vrele(vp);
1579 
1580 	return error;
1581 }
1582 
1583 /* --------------------------------------------------------------------- */
1584 
1585 static int
1586 tmpfs_nsymlink(struct vop_nsymlink_args *ap)
1587 {
1588 	struct vnode *dvp = ap->a_dvp;
1589 	struct vnode **vpp = ap->a_vpp;
1590 	struct namecache *ncp = ap->a_nch->ncp;
1591 	struct vattr *vap = ap->a_vap;
1592 	struct ucred *cred = ap->a_cred;
1593 	char *target = ap->a_target;
1594 	int error;
1595 
1596 	vap->va_type = VLNK;
1597 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1598 	if (error == 0) {
1599 		tmpfs_knote(*vpp, NOTE_WRITE);
1600 		cache_setunresolved(ap->a_nch);
1601 		cache_setvp(ap->a_nch, *vpp);
1602 	}
1603 	return error;
1604 }
1605 
1606 /* --------------------------------------------------------------------- */
1607 
1608 static int
1609 tmpfs_readdir(struct vop_readdir_args *ap)
1610 {
1611 	struct vnode *vp = ap->a_vp;
1612 	struct uio *uio = ap->a_uio;
1613 	int *eofflag = ap->a_eofflag;
1614 	off_t **cookies = ap->a_cookies;
1615 	int *ncookies = ap->a_ncookies;
1616 	struct tmpfs_mount *tmp;
1617 	int error;
1618 	off_t startoff;
1619 	off_t cnt = 0;
1620 	struct tmpfs_node *node;
1621 
1622 	/* This operation only makes sense on directory nodes. */
1623 	if (vp->v_type != VDIR) {
1624 		return ENOTDIR;
1625 	}
1626 
1627 	tmp = VFS_TO_TMPFS(vp->v_mount);
1628 	node = VP_TO_TMPFS_DIR(vp);
1629 	startoff = uio->uio_offset;
1630 
1631 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1632 		error = tmpfs_dir_getdotdent(node, uio);
1633 		if (error != 0) {
1634 			TMPFS_NODE_LOCK_SH(node);
1635 			goto outok;
1636 		}
1637 		cnt++;
1638 	}
1639 
1640 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1641 		/* may lock parent, cannot hold node lock */
1642 		error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1643 		if (error != 0) {
1644 			TMPFS_NODE_LOCK_SH(node);
1645 			goto outok;
1646 		}
1647 		cnt++;
1648 	}
1649 
1650 	TMPFS_NODE_LOCK_SH(node);
1651 	error = tmpfs_dir_getdents(node, uio, &cnt);
1652 
1653 outok:
1654 	KKASSERT(error >= -1);
1655 
1656 	if (error == -1)
1657 		error = 0;
1658 
1659 	if (eofflag != NULL)
1660 		*eofflag =
1661 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1662 
1663 	/* Update NFS-related variables. */
1664 	if (error == 0 && cookies != NULL && ncookies != NULL) {
1665 		off_t i;
1666 		off_t off = startoff;
1667 		struct tmpfs_dirent *de = NULL;
1668 
1669 		*ncookies = cnt;
1670 		*cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1671 
1672 		for (i = 0; i < cnt; i++) {
1673 			KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1674 			if (off == TMPFS_DIRCOOKIE_DOT) {
1675 				off = TMPFS_DIRCOOKIE_DOTDOT;
1676 			} else {
1677 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1678 					de = RB_MIN(tmpfs_dirtree_cookie,
1679 						&node->tn_dir.tn_cookietree);
1680 				} else if (de != NULL) {
1681 					de = RB_NEXT(tmpfs_dirtree_cookie,
1682 					       &node->tn_dir.tn_cookietree, de);
1683 				} else {
1684 					de = tmpfs_dir_lookupbycookie(node,
1685 								      off);
1686 					KKASSERT(de != NULL);
1687 					de = RB_NEXT(tmpfs_dirtree_cookie,
1688 					       &node->tn_dir.tn_cookietree, de);
1689 				}
1690 				if (de == NULL)
1691 					off = TMPFS_DIRCOOKIE_EOF;
1692 				else
1693 					off = tmpfs_dircookie(de);
1694 			}
1695 			(*cookies)[i] = off;
1696 		}
1697 		KKASSERT(uio->uio_offset == off);
1698 	}
1699 	TMPFS_NODE_UNLOCK(node);
1700 
1701 	if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1702 		TMPFS_NODE_LOCK(node);
1703 		node->tn_status |= TMPFS_NODE_ACCESSED;
1704 		TMPFS_NODE_UNLOCK(node);
1705 	}
1706 	return error;
1707 }
1708 
1709 /* --------------------------------------------------------------------- */
1710 
1711 static int
1712 tmpfs_readlink(struct vop_readlink_args *ap)
1713 {
1714 	struct vnode *vp = ap->a_vp;
1715 	struct uio *uio = ap->a_uio;
1716 	int error;
1717 	struct tmpfs_node *node;
1718 
1719 	KKASSERT(uio->uio_offset == 0);
1720 	KKASSERT(vp->v_type == VLNK);
1721 
1722 	node = VP_TO_TMPFS_NODE(vp);
1723 	TMPFS_NODE_LOCK_SH(node);
1724 	error = uiomove(node->tn_link,
1725 			MIN(node->tn_size, uio->uio_resid), uio);
1726 	TMPFS_NODE_UNLOCK(node);
1727 	if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1728 		TMPFS_NODE_LOCK(node);
1729 		node->tn_status |= TMPFS_NODE_ACCESSED;
1730 		TMPFS_NODE_UNLOCK(node);
1731 	}
1732 	return error;
1733 }
1734 
1735 /* --------------------------------------------------------------------- */
1736 
1737 static int
1738 tmpfs_inactive(struct vop_inactive_args *ap)
1739 {
1740 	struct vnode *vp = ap->a_vp;
1741 	struct tmpfs_node *node;
1742 	struct mount *mp;
1743 
1744 	mp = vp->v_mount;
1745 	lwkt_gettoken(&mp->mnt_token);
1746 	node = VP_TO_TMPFS_NODE(vp);
1747 
1748 	/*
1749 	 * Degenerate case
1750 	 */
1751 	if (node == NULL) {
1752 		vrecycle(vp);
1753 		lwkt_reltoken(&mp->mnt_token);
1754 		return(0);
1755 	}
1756 
1757 	/*
1758 	 * Get rid of unreferenced deleted vnodes sooner rather than
1759 	 * later so the data memory can be recovered immediately.
1760 	 *
1761 	 * We must truncate the vnode to prevent the normal reclamation
1762 	 * path from flushing the data for the removed file to disk.
1763 	 */
1764 	TMPFS_NODE_LOCK(node);
1765 	if (node->tn_links == 0) {
1766 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1767 		TMPFS_NODE_UNLOCK(node);
1768 		if (node->tn_type == VREG)
1769 			tmpfs_truncate(vp, 0);
1770 		vrecycle(vp);
1771 	} else {
1772 		/*
1773 		 * We must retain any VM pages belonging to the vnode's
1774 		 * object as the vnode will destroy the object during a
1775 		 * later reclaim.  We call vinvalbuf(V_SAVE) to clean
1776 		 * out the buffer cache.
1777 		 *
1778 		 * On DragonFlyBSD, vnodes are not immediately deactivated
1779 		 * on the 1->0 refs, so this is a relatively optimal
1780 		 * operation.  We have to do this in tmpfs_inactive()
1781 		 * because the pages will have already been thrown away
1782 		 * at the time tmpfs_reclaim() is called.
1783 		 */
1784 		if (node->tn_type == VREG &&
1785 		    node->tn_reg.tn_pages_in_aobj == 0) {
1786 			vinvalbuf(vp, V_SAVE, 0, 0);
1787 			KKASSERT(RB_EMPTY(&vp->v_rbdirty_tree));
1788 			KKASSERT(RB_EMPTY(&vp->v_rbclean_tree));
1789 			tmpfs_move_pages(vp->v_object, node->tn_reg.tn_aobj,
1790 					 TMPFS_MOVF_DEACTIVATE);
1791 			node->tn_reg.tn_pages_in_aobj = 1;
1792 		}
1793 
1794 		TMPFS_NODE_UNLOCK(node);
1795 	}
1796 	lwkt_reltoken(&mp->mnt_token);
1797 
1798 	return 0;
1799 }
1800 
1801 /* --------------------------------------------------------------------- */
1802 
1803 int
1804 tmpfs_reclaim(struct vop_reclaim_args *ap)
1805 {
1806 	struct vnode *vp = ap->a_vp;
1807 	struct tmpfs_mount *tmp;
1808 	struct tmpfs_node *node;
1809 	struct mount *mp;
1810 
1811 	mp = vp->v_mount;
1812 	lwkt_gettoken(&mp->mnt_token);
1813 
1814 	node = VP_TO_TMPFS_NODE(vp);
1815 	tmp = VFS_TO_TMPFS(vp->v_mount);
1816 	KKASSERT(mp == tmp->tm_mount);
1817 
1818         TMPFS_NODE_LOCK(node);
1819 	KKASSERT(node->tn_vnode == vp);
1820         node->tn_vnode = NULL;
1821         vp->v_data = NULL;
1822 
1823 	/*
1824 	 * If the node referenced by this vnode was deleted by the
1825 	 * user, we must free its associated data structures now that
1826 	 * the vnode is being reclaimed.
1827 	 *
1828 	 * Directories have an extra link ref.
1829 	 */
1830 	if (node->tn_links == 0) {
1831 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1832 		tmpfs_free_node(tmp, node);
1833 		/* eats the lock */
1834 	} else {
1835 		TMPFS_NODE_UNLOCK(node);
1836 	}
1837 	lwkt_reltoken(&mp->mnt_token);
1838 
1839 	KKASSERT(vp->v_data == NULL);
1840 	return 0;
1841 }
1842 
1843 /* --------------------------------------------------------------------- */
1844 
1845 static int
1846 tmpfs_mountctl(struct vop_mountctl_args *ap)
1847 {
1848 	struct tmpfs_mount *tmp;
1849 	struct mount *mp;
1850 	int rc;
1851 
1852 	mp = ap->a_head.a_ops->head.vv_mount;
1853 	lwkt_gettoken(&mp->mnt_token);
1854 
1855 	switch (ap->a_op) {
1856 	case (MOUNTCTL_SET_EXPORT):
1857 		tmp = (struct tmpfs_mount *) mp->mnt_data;
1858 
1859 		if (ap->a_ctllen != sizeof(struct export_args))
1860 			rc = (EINVAL);
1861 		else
1862 			rc = vfs_export(mp, &tmp->tm_export,
1863 					(const struct export_args *) ap->a_ctl);
1864 		break;
1865 	default:
1866 		rc = vop_stdmountctl(ap);
1867 		break;
1868 	}
1869 
1870 	lwkt_reltoken(&mp->mnt_token);
1871 	return (rc);
1872 }
1873 
1874 /* --------------------------------------------------------------------- */
1875 
1876 static int
1877 tmpfs_print(struct vop_print_args *ap)
1878 {
1879 	struct vnode *vp = ap->a_vp;
1880 
1881 	struct tmpfs_node *node;
1882 
1883 	node = VP_TO_TMPFS_NODE(vp);
1884 
1885 	kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1886 	    node, node->tn_flags, node->tn_links);
1887 	kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1888 	    node->tn_mode, node->tn_uid, node->tn_gid,
1889 	    (uintmax_t)node->tn_size, node->tn_status);
1890 
1891 	if (vp->v_type == VFIFO)
1892 		fifo_printinfo(vp);
1893 
1894 	kprintf("\n");
1895 
1896 	return 0;
1897 }
1898 
1899 /* --------------------------------------------------------------------- */
1900 
1901 static int
1902 tmpfs_pathconf(struct vop_pathconf_args *ap)
1903 {
1904 	struct vnode *vp = ap->a_vp;
1905 	int name = ap->a_name;
1906 	register_t *retval = ap->a_retval;
1907 	struct tmpfs_mount *tmp;
1908 	int error;
1909 
1910 	error = 0;
1911 
1912 	switch (name) {
1913 	case _PC_CHOWN_RESTRICTED:
1914 		*retval = 1;
1915 		break;
1916 
1917 	case _PC_FILESIZEBITS:
1918 		tmp = VFS_TO_TMPFS(vp->v_mount);
1919 		*retval = max(32, flsll(tmp->tm_pages_max * PAGE_SIZE) + 1);
1920 		break;
1921 
1922 	case _PC_LINK_MAX:
1923 		*retval = LINK_MAX;
1924 		break;
1925 
1926 	case _PC_NAME_MAX:
1927 		*retval = NAME_MAX;
1928 		break;
1929 
1930 	case _PC_NO_TRUNC:
1931 		*retval = 1;
1932 		break;
1933 
1934 	case _PC_PATH_MAX:
1935 		*retval = PATH_MAX;
1936 		break;
1937 
1938 	case _PC_PIPE_BUF:
1939 		*retval = PIPE_BUF;
1940 		break;
1941 
1942 	case _PC_SYNC_IO:
1943 		*retval = 1;
1944 		break;
1945 
1946 	case _PC_2_SYMLINKS:
1947 		*retval = 1;
1948 		break;
1949 
1950 	default:
1951 		error = EINVAL;
1952 	}
1953 
1954 	return error;
1955 }
1956 
1957 /************************************************************************
1958  *                          KQFILTER OPS                                *
1959  ************************************************************************/
1960 
1961 static void filt_tmpfsdetach(struct knote *kn);
1962 static int filt_tmpfsread(struct knote *kn, long hint);
1963 static int filt_tmpfswrite(struct knote *kn, long hint);
1964 static int filt_tmpfsvnode(struct knote *kn, long hint);
1965 
1966 static struct filterops tmpfsread_filtops =
1967 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
1968 	  NULL, filt_tmpfsdetach, filt_tmpfsread };
1969 static struct filterops tmpfswrite_filtops =
1970 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
1971 	  NULL, filt_tmpfsdetach, filt_tmpfswrite };
1972 static struct filterops tmpfsvnode_filtops =
1973 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
1974 	  NULL, filt_tmpfsdetach, filt_tmpfsvnode };
1975 
1976 static int
1977 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
1978 {
1979 	struct vnode *vp = ap->a_vp;
1980 	struct knote *kn = ap->a_kn;
1981 
1982 	switch (kn->kn_filter) {
1983 	case EVFILT_READ:
1984 		kn->kn_fop = &tmpfsread_filtops;
1985 		break;
1986 	case EVFILT_WRITE:
1987 		kn->kn_fop = &tmpfswrite_filtops;
1988 		break;
1989 	case EVFILT_VNODE:
1990 		kn->kn_fop = &tmpfsvnode_filtops;
1991 		break;
1992 	default:
1993 		return (EOPNOTSUPP);
1994 	}
1995 
1996 	kn->kn_hook = (caddr_t)vp;
1997 
1998 	knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1999 
2000 	return(0);
2001 }
2002 
2003 static void
2004 filt_tmpfsdetach(struct knote *kn)
2005 {
2006 	struct vnode *vp = (void *)kn->kn_hook;
2007 
2008 	knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2009 }
2010 
2011 static int
2012 filt_tmpfsread(struct knote *kn, long hint)
2013 {
2014 	struct vnode *vp = (void *)kn->kn_hook;
2015 	struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
2016 	off_t off;
2017 
2018 	if (hint == NOTE_REVOKE) {
2019 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2020 		return(1);
2021 	}
2022 
2023 	/*
2024 	 * Interlock against MP races when performing this function.
2025 	 */
2026 	TMPFS_NODE_LOCK_SH(node);
2027 	off = node->tn_size - kn->kn_fp->f_offset;
2028 	kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2029 	if (kn->kn_sfflags & NOTE_OLDAPI) {
2030 		TMPFS_NODE_UNLOCK(node);
2031 		return(1);
2032 	}
2033 	if (kn->kn_data == 0) {
2034 		kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2035 	}
2036 	TMPFS_NODE_UNLOCK(node);
2037 	return (kn->kn_data != 0);
2038 }
2039 
2040 static int
2041 filt_tmpfswrite(struct knote *kn, long hint)
2042 {
2043 	if (hint == NOTE_REVOKE)
2044 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2045 	kn->kn_data = 0;
2046 	return (1);
2047 }
2048 
2049 static int
2050 filt_tmpfsvnode(struct knote *kn, long hint)
2051 {
2052 	if (kn->kn_sfflags & hint)
2053 		kn->kn_fflags |= hint;
2054 	if (hint == NOTE_REVOKE) {
2055 		kn->kn_flags |= (EV_EOF | EV_NODATA);
2056 		return (1);
2057 	}
2058 	return (kn->kn_fflags != 0);
2059 }
2060 
2061 /*
2062  * Helper to move VM pages between objects
2063  *
2064  * NOTE: The vm_page_rename() dirties the page, so we can clear the
2065  *	 PG_NEED_COMMIT flag.  If the pages are being moved into tn_aobj,
2066  *	 the pageout daemon will be able to page them out.
2067  */
2068 static int
2069 tmpfs_move_pages_callback(vm_page_t p, void *data)
2070 {
2071 	struct rb_vm_page_scan_info *info = data;
2072 	vm_pindex_t pindex;
2073 
2074 	pindex = p->pindex;
2075 	if (vm_page_busy_try(p, TRUE)) {
2076 		vm_page_sleep_busy(p, TRUE, "tpgmov");
2077 		info->error = -1;
2078 		return -1;
2079 	}
2080 	if (p->object != info->object || p->pindex != pindex) {
2081 		vm_page_wakeup(p);
2082 		info->error = -1;
2083 		return -1;
2084 	}
2085 
2086 	if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) &&
2087 	    (p->flags & PG_SWAPPED) &&
2088 	    (p->flags & PG_NEED_COMMIT) == 0 &&
2089 	    p->dirty == 0) {
2090 		/*
2091 		 * If the page in the backing aobj was paged out to swap
2092 		 * it will be clean and it is better to free it rather
2093 		 * than re-dirty it.  We will assume that the page was
2094 		 * paged out to swap for a reason!
2095 		 *
2096 		 * This helps avoid unnecessary swap thrashing on the page.
2097 		 */
2098 		vm_page_free(p);
2099 	} else if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) == 0 &&
2100 		   (p->flags & PG_NEED_COMMIT) == 0 &&
2101 		   p->dirty == 0) {
2102 		/*
2103 		 * If the page associated with the vnode was cleaned via
2104 		 * a tmpfs_strategy() call, it exists as a swap block in
2105 		 * aobj and it is again better to free it rather than
2106 		 * re-dirty it.  We will assume that the page was
2107 		 * paged out to swap for a reason!
2108 		 *
2109 		 * This helps avoid unnecessary swap thrashing on the page.
2110 		 */
2111 		vm_page_free(p);
2112 	} else {
2113 		/*
2114 		 * Rename the page, which will also ensure that it is flagged
2115 		 * as dirty and check whether a swap block association exists
2116 		 * in the target object or not, setting appropriate flags if
2117 		 * it does.
2118 		 */
2119 		vm_page_rename(p, info->dest_object, pindex);
2120 		vm_page_clear_commit(p);
2121 		if (info->pagerflags & TMPFS_MOVF_DEACTIVATE)
2122 			vm_page_deactivate(p);
2123 		vm_page_wakeup(p);
2124 		/* page automaticaly made dirty */
2125 	}
2126 
2127 	return 0;
2128 }
2129 
2130 static
2131 void
2132 tmpfs_move_pages(vm_object_t src, vm_object_t dst, int movflags)
2133 {
2134 	struct rb_vm_page_scan_info info;
2135 
2136 	vm_object_hold(src);
2137 	vm_object_hold(dst);
2138 	info.object = src;
2139 	info.dest_object = dst;
2140 	info.pagerflags = movflags;
2141 	do {
2142 		if (src->paging_in_progress)
2143 			vm_object_pip_wait(src, "objtfs");
2144 		info.error = 1;
2145 		vm_page_rb_tree_RB_SCAN(&src->rb_memq, NULL,
2146 					tmpfs_move_pages_callback, &info);
2147 	} while (info.error < 0 || !RB_EMPTY(&src->rb_memq) ||
2148 		 src->paging_in_progress);
2149 	vm_object_drop(dst);
2150 	vm_object_drop(src);
2151 }
2152 
2153 /* --------------------------------------------------------------------- */
2154 
2155 /*
2156  * vnode operations vector used for files stored in a tmpfs file system.
2157  */
2158 struct vop_ops tmpfs_vnode_vops = {
2159 	.vop_default =			vop_defaultop,
2160 	.vop_getpages = 		vop_stdgetpages,
2161 	.vop_putpages = 		vop_stdputpages,
2162 	.vop_ncreate =			tmpfs_ncreate,
2163 	.vop_nresolve =			tmpfs_nresolve,
2164 	.vop_nlookupdotdot =		tmpfs_nlookupdotdot,
2165 	.vop_nmknod =			tmpfs_nmknod,
2166 	.vop_open =			tmpfs_open,
2167 	.vop_close =			tmpfs_close,
2168 	.vop_access =			tmpfs_access,
2169 	.vop_getattr =			tmpfs_getattr,
2170 	.vop_getattr_quick =		tmpfs_getattr_quick,
2171 	.vop_setattr =			tmpfs_setattr,
2172 	.vop_read =			tmpfs_read,
2173 	.vop_write =			tmpfs_write,
2174 	.vop_fsync =			tmpfs_fsync,
2175 	.vop_mountctl =			tmpfs_mountctl,
2176 	.vop_nremove =			tmpfs_nremove,
2177 	.vop_nlink =			tmpfs_nlink,
2178 	.vop_nrename =			tmpfs_nrename,
2179 	.vop_nmkdir =			tmpfs_nmkdir,
2180 	.vop_nrmdir =			tmpfs_nrmdir,
2181 	.vop_nsymlink =			tmpfs_nsymlink,
2182 	.vop_readdir =			tmpfs_readdir,
2183 	.vop_readlink =			tmpfs_readlink,
2184 	.vop_inactive =			tmpfs_inactive,
2185 	.vop_reclaim =			tmpfs_reclaim,
2186 	.vop_print =			tmpfs_print,
2187 	.vop_pathconf =			tmpfs_pathconf,
2188 	.vop_bmap =			tmpfs_bmap,
2189 	.vop_strategy =			tmpfs_strategy,
2190 	.vop_advlock =			tmpfs_advlock,
2191 	.vop_kqfilter =			tmpfs_kqfilter
2192 };
2193