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