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