xref: /freebsd-src/sys/fs/tmpfs/tmpfs_vnops.c (revision b2db760808f74bb53c232900091c9da801ebbfcc)
1 /*	$NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9  * 2005 program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * tmpfs vnode interface.
35  */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/lockf.h>
42 #include <sys/namei.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/sched.h>
46 #include <sys/sf_buf.h>
47 #include <sys/stat.h>
48 #include <sys/systm.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
51 
52 #include <vm/vm.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_pager.h>
56 
57 #include <machine/_inttypes.h>
58 
59 #include <fs/fifofs/fifo.h>
60 #include <fs/tmpfs/tmpfs_vnops.h>
61 #include <fs/tmpfs/tmpfs.h>
62 
63 /* --------------------------------------------------------------------- */
64 
65 static int
66 tmpfs_lookup(struct vop_cachedlookup_args *v)
67 {
68 	struct vnode *dvp = v->a_dvp;
69 	struct vnode **vpp = v->a_vpp;
70 	struct componentname *cnp = v->a_cnp;
71 
72 	int error;
73 	struct tmpfs_dirent *de;
74 	struct tmpfs_node *dnode;
75 
76 	dnode = VP_TO_TMPFS_DIR(dvp);
77 	*vpp = NULLVP;
78 
79 	/* Check accessibility of requested node as a first step. */
80 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
81 	if (error != 0)
82 		goto out;
83 
84 	/* We cannot be requesting the parent directory of the root node. */
85 	MPASS(IMPLIES(dnode->tn_type == VDIR &&
86 	    dnode->tn_dir.tn_parent == dnode,
87 	    !(cnp->cn_flags & ISDOTDOT)));
88 
89 	TMPFS_ASSERT_LOCKED(dnode);
90 	if (dnode->tn_dir.tn_parent == NULL) {
91 		error = ENOENT;
92 		goto out;
93 	}
94 	if (cnp->cn_flags & ISDOTDOT) {
95 		int ltype = 0;
96 
97 		ltype = VOP_ISLOCKED(dvp);
98 		vhold(dvp);
99 		VOP_UNLOCK(dvp, 0);
100 		/* Allocate a new vnode on the matching entry. */
101 		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
102 		    cnp->cn_lkflags, vpp);
103 
104 		vn_lock(dvp, ltype | LK_RETRY);
105 		vdrop(dvp);
106 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
107 		VREF(dvp);
108 		*vpp = dvp;
109 		error = 0;
110 	} else {
111 		de = tmpfs_dir_lookup(dnode, NULL, cnp);
112 		if (de == NULL) {
113 			/* The entry was not found in the directory.
114 			 * This is OK if we are creating or renaming an
115 			 * entry and are working on the last component of
116 			 * the path name. */
117 			if ((cnp->cn_flags & ISLASTCN) &&
118 			    (cnp->cn_nameiop == CREATE || \
119 			    cnp->cn_nameiop == RENAME)) {
120 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
121 				    cnp->cn_thread);
122 				if (error != 0)
123 					goto out;
124 
125 				/* Keep the component name in the buffer for
126 				 * future uses. */
127 				cnp->cn_flags |= SAVENAME;
128 
129 				error = EJUSTRETURN;
130 			} else
131 				error = ENOENT;
132 		} else {
133 			struct tmpfs_node *tnode;
134 
135 			/* The entry was found, so get its associated
136 			 * tmpfs_node. */
137 			tnode = de->td_node;
138 
139 			/* If we are not at the last path component and
140 			 * found a non-directory or non-link entry (which
141 			 * may itself be pointing to a directory), raise
142 			 * an error. */
143 			if ((tnode->tn_type != VDIR &&
144 			    tnode->tn_type != VLNK) &&
145 			    !(cnp->cn_flags & ISLASTCN)) {
146 				error = ENOTDIR;
147 				goto out;
148 			}
149 
150 			/* If we are deleting or renaming the entry, keep
151 			 * track of its tmpfs_dirent so that it can be
152 			 * easily deleted later. */
153 			if ((cnp->cn_flags & ISLASTCN) &&
154 			    (cnp->cn_nameiop == DELETE ||
155 			    cnp->cn_nameiop == RENAME)) {
156 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
157 				    cnp->cn_thread);
158 				if (error != 0)
159 					goto out;
160 
161 				/* Allocate a new vnode on the matching entry. */
162 				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
163 						cnp->cn_lkflags, vpp);
164 				if (error != 0)
165 					goto out;
166 
167 				if ((dnode->tn_mode & S_ISTXT) &&
168 				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
169 				  VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
170 					error = EPERM;
171 					vput(*vpp);
172 					*vpp = NULL;
173 					goto out;
174 				}
175 				cnp->cn_flags |= SAVENAME;
176 			} else {
177 				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
178 						cnp->cn_lkflags, vpp);
179 			}
180 		}
181 	}
182 
183 	/* Store the result of this lookup in the cache.  Avoid this if the
184 	 * request was for creation, as it does not improve timings on
185 	 * emprical tests. */
186 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
187 		cache_enter(dvp, *vpp, cnp);
188 
189 out:
190 	/* If there were no errors, *vpp cannot be null and it must be
191 	 * locked. */
192 	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
193 
194 	return error;
195 }
196 
197 /* --------------------------------------------------------------------- */
198 
199 static int
200 tmpfs_create(struct vop_create_args *v)
201 {
202 	struct vnode *dvp = v->a_dvp;
203 	struct vnode **vpp = v->a_vpp;
204 	struct componentname *cnp = v->a_cnp;
205 	struct vattr *vap = v->a_vap;
206 
207 	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
208 
209 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
210 }
211 /* --------------------------------------------------------------------- */
212 
213 static int
214 tmpfs_mknod(struct vop_mknod_args *v)
215 {
216 	struct vnode *dvp = v->a_dvp;
217 	struct vnode **vpp = v->a_vpp;
218 	struct componentname *cnp = v->a_cnp;
219 	struct vattr *vap = v->a_vap;
220 
221 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
222 	    vap->va_type != VFIFO)
223 		return EINVAL;
224 
225 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
226 }
227 
228 /* --------------------------------------------------------------------- */
229 
230 static int
231 tmpfs_open(struct vop_open_args *v)
232 {
233 	struct vnode *vp = v->a_vp;
234 	int mode = v->a_mode;
235 
236 	int error;
237 	struct tmpfs_node *node;
238 
239 	MPASS(VOP_ISLOCKED(vp));
240 
241 	node = VP_TO_TMPFS_NODE(vp);
242 
243 	/* The file is still active but all its names have been removed
244 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
245 	 * it is about to die. */
246 	if (node->tn_links < 1)
247 		return (ENOENT);
248 
249 	/* If the file is marked append-only, deny write requests. */
250 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
251 		error = EPERM;
252 	else {
253 		error = 0;
254 		vnode_create_vobject(vp, node->tn_size, v->a_td);
255 	}
256 
257 	MPASS(VOP_ISLOCKED(vp));
258 	return error;
259 }
260 
261 /* --------------------------------------------------------------------- */
262 
263 static int
264 tmpfs_close(struct vop_close_args *v)
265 {
266 	struct vnode *vp = v->a_vp;
267 
268 	struct tmpfs_node *node;
269 
270 	MPASS(VOP_ISLOCKED(vp));
271 
272 	node = VP_TO_TMPFS_NODE(vp);
273 
274 	if (node->tn_links > 0) {
275 		/* Update node times.  No need to do it if the node has
276 		 * been deleted, because it will vanish after we return. */
277 		tmpfs_update(vp);
278 	}
279 
280 	return 0;
281 }
282 
283 /* --------------------------------------------------------------------- */
284 
285 int
286 tmpfs_access(struct vop_access_args *v)
287 {
288 	struct vnode *vp = v->a_vp;
289 	accmode_t accmode = v->a_accmode;
290 	struct ucred *cred = v->a_cred;
291 
292 	int error;
293 	struct tmpfs_node *node;
294 
295 	MPASS(VOP_ISLOCKED(vp));
296 
297 	node = VP_TO_TMPFS_NODE(vp);
298 
299 	switch (vp->v_type) {
300 	case VDIR:
301 		/* FALLTHROUGH */
302 	case VLNK:
303 		/* FALLTHROUGH */
304 	case VREG:
305 		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
306 			error = EROFS;
307 			goto out;
308 		}
309 		break;
310 
311 	case VBLK:
312 		/* FALLTHROUGH */
313 	case VCHR:
314 		/* FALLTHROUGH */
315 	case VSOCK:
316 		/* FALLTHROUGH */
317 	case VFIFO:
318 		break;
319 
320 	default:
321 		error = EINVAL;
322 		goto out;
323 	}
324 
325 	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
326 		error = EPERM;
327 		goto out;
328 	}
329 
330 	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
331 	    node->tn_gid, accmode, cred, NULL);
332 
333 out:
334 	MPASS(VOP_ISLOCKED(vp));
335 
336 	return error;
337 }
338 
339 /* --------------------------------------------------------------------- */
340 
341 int
342 tmpfs_getattr(struct vop_getattr_args *v)
343 {
344 	struct vnode *vp = v->a_vp;
345 	struct vattr *vap = v->a_vap;
346 
347 	struct tmpfs_node *node;
348 
349 	node = VP_TO_TMPFS_NODE(vp);
350 
351 	tmpfs_update(vp);
352 
353 	vap->va_type = vp->v_type;
354 	vap->va_mode = node->tn_mode;
355 	vap->va_nlink = node->tn_links;
356 	vap->va_uid = node->tn_uid;
357 	vap->va_gid = node->tn_gid;
358 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
359 	vap->va_fileid = node->tn_id;
360 	vap->va_size = node->tn_size;
361 	vap->va_blocksize = PAGE_SIZE;
362 	vap->va_atime = node->tn_atime;
363 	vap->va_mtime = node->tn_mtime;
364 	vap->va_ctime = node->tn_ctime;
365 	vap->va_birthtime = node->tn_birthtime;
366 	vap->va_gen = node->tn_gen;
367 	vap->va_flags = node->tn_flags;
368 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
369 		node->tn_rdev : NODEV;
370 	vap->va_bytes = round_page(node->tn_size);
371 	vap->va_filerev = 0;
372 
373 	return 0;
374 }
375 
376 /* --------------------------------------------------------------------- */
377 
378 /* XXX Should this operation be atomic?  I think it should, but code in
379  * XXX other places (e.g., ufs) doesn't seem to be... */
380 int
381 tmpfs_setattr(struct vop_setattr_args *v)
382 {
383 	struct vnode *vp = v->a_vp;
384 	struct vattr *vap = v->a_vap;
385 	struct ucred *cred = v->a_cred;
386 	struct thread *td = curthread;
387 
388 	int error;
389 
390 	MPASS(VOP_ISLOCKED(vp));
391 
392 	error = 0;
393 
394 	/* Abort if any unsettable attribute is given. */
395 	if (vap->va_type != VNON ||
396 	    vap->va_nlink != VNOVAL ||
397 	    vap->va_fsid != VNOVAL ||
398 	    vap->va_fileid != VNOVAL ||
399 	    vap->va_blocksize != VNOVAL ||
400 	    vap->va_gen != VNOVAL ||
401 	    vap->va_rdev != VNOVAL ||
402 	    vap->va_bytes != VNOVAL)
403 		error = EINVAL;
404 
405 	if (error == 0 && (vap->va_flags != VNOVAL))
406 		error = tmpfs_chflags(vp, vap->va_flags, cred, td);
407 
408 	if (error == 0 && (vap->va_size != VNOVAL))
409 		error = tmpfs_chsize(vp, vap->va_size, cred, td);
410 
411 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
412 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
413 
414 	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
415 		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
416 
417 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
418 	    vap->va_atime.tv_nsec != VNOVAL) ||
419 	    (vap->va_mtime.tv_sec != VNOVAL &&
420 	    vap->va_mtime.tv_nsec != VNOVAL) ||
421 	    (vap->va_birthtime.tv_sec != VNOVAL &&
422 	    vap->va_birthtime.tv_nsec != VNOVAL)))
423 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
424 			&vap->va_birthtime, vap->va_vaflags, cred, td);
425 
426 	/* Update the node times.  We give preference to the error codes
427 	 * generated by this function rather than the ones that may arise
428 	 * from tmpfs_update. */
429 	tmpfs_update(vp);
430 
431 	MPASS(VOP_ISLOCKED(vp));
432 
433 	return error;
434 }
435 
436 /* --------------------------------------------------------------------- */
437 static int
438 tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
439     vm_offset_t offset, size_t tlen, struct uio *uio)
440 {
441 	vm_page_t	m;
442 	int		error;
443 
444 	VM_OBJECT_LOCK(tobj);
445 	vm_object_pip_add(tobj, 1);
446 	m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
447 	    VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
448 	if (m->valid != VM_PAGE_BITS_ALL) {
449 		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
450 			error = vm_pager_get_pages(tobj, &m, 1, 0);
451 			if (error != 0) {
452 				printf("tmpfs get pages from pager error [read]\n");
453 				goto out;
454 			}
455 		} else
456 			vm_page_zero_invalid(m, TRUE);
457 	}
458 	VM_OBJECT_UNLOCK(tobj);
459 	error = uiomove_fromphys(&m, offset, tlen, uio);
460 	VM_OBJECT_LOCK(tobj);
461 out:
462 	vm_page_lock(m);
463 	vm_page_unwire(m, TRUE);
464 	vm_page_unlock(m);
465 	vm_page_wakeup(m);
466 	vm_object_pip_subtract(tobj, 1);
467 	VM_OBJECT_UNLOCK(tobj);
468 
469 	return (error);
470 }
471 
472 static __inline int
473 tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
474     vm_offset_t offset, size_t tlen, void *buf)
475 {
476 	struct uio uio;
477 	struct iovec iov;
478 
479 	uio.uio_iovcnt = 1;
480 	uio.uio_iov = &iov;
481 	iov.iov_base = buf;
482 	iov.iov_len = tlen;
483 
484 	uio.uio_offset = 0;
485 	uio.uio_resid = tlen;
486 	uio.uio_rw = UIO_READ;
487 	uio.uio_segflg = UIO_SYSSPACE;
488 	uio.uio_td = curthread;
489 
490 	return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
491 }
492 
493 static int
494 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
495 {
496 	struct sf_buf	*sf;
497 	vm_pindex_t	idx;
498 	vm_page_t	m;
499 	vm_offset_t	offset;
500 	off_t		addr;
501 	size_t		tlen;
502 	char		*ma;
503 	int		error;
504 
505 	addr = uio->uio_offset;
506 	idx = OFF_TO_IDX(addr);
507 	offset = addr & PAGE_MASK;
508 	tlen = MIN(PAGE_SIZE - offset, len);
509 
510 	if ((vobj == NULL) ||
511 	    (vobj->resident_page_count == 0 && vobj->cache == NULL))
512 		goto nocache;
513 
514 	VM_OBJECT_LOCK(vobj);
515 lookupvpg:
516 	if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
517 	    vm_page_is_valid(m, offset, tlen)) {
518 		if ((m->oflags & VPO_BUSY) != 0) {
519 			/*
520 			 * Reference the page before unlocking and sleeping so
521 			 * that the page daemon is less likely to reclaim it.
522 			 */
523 			vm_page_lock_queues();
524 			vm_page_flag_set(m, PG_REFERENCED);
525 			vm_page_sleep(m, "tmfsmr");
526 			goto lookupvpg;
527 		}
528 		vm_page_busy(m);
529 		VM_OBJECT_UNLOCK(vobj);
530 		error = uiomove_fromphys(&m, offset, tlen, uio);
531 		VM_OBJECT_LOCK(vobj);
532 		vm_page_wakeup(m);
533 		VM_OBJECT_UNLOCK(vobj);
534 		return	(error);
535 	} else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
536 		if ((m->oflags & VPO_BUSY) != 0) {
537 			/*
538 			 * Reference the page before unlocking and sleeping so
539 			 * that the page daemon is less likely to reclaim it.
540 			 */
541 			vm_page_lock_queues();
542 			vm_page_flag_set(m, PG_REFERENCED);
543 			vm_page_sleep(m, "tmfsmr");
544 			goto lookupvpg;
545 		}
546 		vm_page_busy(m);
547 		VM_OBJECT_UNLOCK(vobj);
548 		sched_pin();
549 		sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
550 		ma = (char *)sf_buf_kva(sf);
551 		error = tmpfs_nocacheread_buf(tobj, idx, offset, tlen,
552 		    ma + offset);
553 		if (error == 0) {
554 			uio->uio_offset += tlen;
555 			uio->uio_resid -= tlen;
556 		}
557 		sf_buf_free(sf);
558 		sched_unpin();
559 		VM_OBJECT_LOCK(vobj);
560 		vm_page_wakeup(m);
561 		VM_OBJECT_UNLOCK(vobj);
562 		return	(error);
563 	}
564 	VM_OBJECT_UNLOCK(vobj);
565 nocache:
566 	error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
567 
568 	return	(error);
569 }
570 
571 static int
572 tmpfs_read(struct vop_read_args *v)
573 {
574 	struct vnode *vp = v->a_vp;
575 	struct uio *uio = v->a_uio;
576 
577 	struct tmpfs_node *node;
578 	vm_object_t uobj;
579 	size_t len;
580 	int resid;
581 
582 	int error = 0;
583 
584 	node = VP_TO_TMPFS_NODE(vp);
585 
586 	if (vp->v_type != VREG) {
587 		error = EISDIR;
588 		goto out;
589 	}
590 
591 	if (uio->uio_offset < 0) {
592 		error = EINVAL;
593 		goto out;
594 	}
595 
596 	node->tn_status |= TMPFS_NODE_ACCESSED;
597 
598 	uobj = node->tn_reg.tn_aobj;
599 	while ((resid = uio->uio_resid) > 0) {
600 		error = 0;
601 		if (node->tn_size <= uio->uio_offset)
602 			break;
603 		len = MIN(node->tn_size - uio->uio_offset, resid);
604 		if (len == 0)
605 			break;
606 		error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
607 		if ((error != 0) || (resid == uio->uio_resid))
608 			break;
609 	}
610 
611 out:
612 
613 	return error;
614 }
615 
616 /* --------------------------------------------------------------------- */
617 
618 static int
619 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
620 {
621 	vm_pindex_t	idx;
622 	vm_page_t	vpg, tpg;
623 	vm_offset_t	offset;
624 	off_t		addr;
625 	size_t		tlen;
626 	int		error;
627 
628 	error = 0;
629 
630 	addr = uio->uio_offset;
631 	idx = OFF_TO_IDX(addr);
632 	offset = addr & PAGE_MASK;
633 	tlen = MIN(PAGE_SIZE - offset, len);
634 
635 	if ((vobj == NULL) ||
636 	    (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
637 		vpg = NULL;
638 		goto nocache;
639 	}
640 
641 	VM_OBJECT_LOCK(vobj);
642 lookupvpg:
643 	if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
644 	    vm_page_is_valid(vpg, offset, tlen)) {
645 		if ((vpg->oflags & VPO_BUSY) != 0) {
646 			/*
647 			 * Reference the page before unlocking and sleeping so
648 			 * that the page daemon is less likely to reclaim it.
649 			 */
650 			vm_page_lock_queues();
651 			vm_page_flag_set(vpg, PG_REFERENCED);
652 			vm_page_sleep(vpg, "tmfsmw");
653 			goto lookupvpg;
654 		}
655 		vm_page_busy(vpg);
656 		vm_page_undirty(vpg);
657 		VM_OBJECT_UNLOCK(vobj);
658 		error = uiomove_fromphys(&vpg, offset, tlen, uio);
659 	} else {
660 		if (__predict_false(vobj->cache != NULL))
661 			vm_page_cache_free(vobj, idx, idx + 1);
662 		VM_OBJECT_UNLOCK(vobj);
663 		vpg = NULL;
664 	}
665 nocache:
666 	VM_OBJECT_LOCK(tobj);
667 	vm_object_pip_add(tobj, 1);
668 	tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
669 	    VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
670 	if (tpg->valid != VM_PAGE_BITS_ALL) {
671 		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
672 			error = vm_pager_get_pages(tobj, &tpg, 1, 0);
673 			if (error != 0) {
674 				printf("tmpfs get pages from pager error [write]\n");
675 				goto out;
676 			}
677 		} else
678 			vm_page_zero_invalid(tpg, TRUE);
679 	}
680 	VM_OBJECT_UNLOCK(tobj);
681 	if (vpg == NULL)
682 		error = uiomove_fromphys(&tpg, offset, tlen, uio);
683 	else {
684 		KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
685 		pmap_copy_page(vpg, tpg);
686 	}
687 	VM_OBJECT_LOCK(tobj);
688 out:
689 	if (vobj != NULL)
690 		VM_OBJECT_LOCK(vobj);
691 	if (error == 0) {
692 		KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
693 		    ("parts of tpg invalid"));
694 		vm_page_dirty(tpg);
695 	}
696 	vm_page_lock(tpg);
697 	vm_page_unwire(tpg, TRUE);
698 	vm_page_unlock(tpg);
699 	vm_page_wakeup(tpg);
700 	if (vpg != NULL)
701 		vm_page_wakeup(vpg);
702 	if (vobj != NULL)
703 		VM_OBJECT_UNLOCK(vobj);
704 	vm_object_pip_subtract(tobj, 1);
705 	VM_OBJECT_UNLOCK(tobj);
706 
707 	return	(error);
708 }
709 
710 static int
711 tmpfs_write(struct vop_write_args *v)
712 {
713 	struct vnode *vp = v->a_vp;
714 	struct uio *uio = v->a_uio;
715 	int ioflag = v->a_ioflag;
716 
717 	boolean_t extended;
718 	int error = 0;
719 	off_t oldsize;
720 	struct tmpfs_node *node;
721 	vm_object_t uobj;
722 	size_t len;
723 	int resid;
724 
725 	node = VP_TO_TMPFS_NODE(vp);
726 	oldsize = node->tn_size;
727 
728 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
729 		error = EINVAL;
730 		goto out;
731 	}
732 
733 	if (uio->uio_resid == 0) {
734 		error = 0;
735 		goto out;
736 	}
737 
738 	if (ioflag & IO_APPEND)
739 		uio->uio_offset = node->tn_size;
740 
741 	if (uio->uio_offset + uio->uio_resid >
742 	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
743 		return (EFBIG);
744 
745 	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
746 		return (EFBIG);
747 
748 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
749 	if (extended) {
750 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
751 		if (error != 0)
752 			goto out;
753 	}
754 
755 	uobj = node->tn_reg.tn_aobj;
756 	while ((resid = uio->uio_resid) > 0) {
757 		if (node->tn_size <= uio->uio_offset)
758 			break;
759 		len = MIN(node->tn_size - uio->uio_offset, resid);
760 		if (len == 0)
761 			break;
762 		error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
763 		if ((error != 0) || (resid == uio->uio_resid))
764 			break;
765 	}
766 
767 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
768 	    (extended ? TMPFS_NODE_CHANGED : 0);
769 
770 	if (node->tn_mode & (S_ISUID | S_ISGID)) {
771 		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
772 			node->tn_mode &= ~(S_ISUID | S_ISGID);
773 	}
774 
775 	if (error != 0)
776 		(void)tmpfs_reg_resize(vp, oldsize);
777 
778 out:
779 	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
780 	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
781 
782 	return error;
783 }
784 
785 /* --------------------------------------------------------------------- */
786 
787 static int
788 tmpfs_fsync(struct vop_fsync_args *v)
789 {
790 	struct vnode *vp = v->a_vp;
791 
792 	MPASS(VOP_ISLOCKED(vp));
793 
794 	tmpfs_update(vp);
795 
796 	return 0;
797 }
798 
799 /* --------------------------------------------------------------------- */
800 
801 static int
802 tmpfs_remove(struct vop_remove_args *v)
803 {
804 	struct vnode *dvp = v->a_dvp;
805 	struct vnode *vp = v->a_vp;
806 
807 	int error;
808 	struct tmpfs_dirent *de;
809 	struct tmpfs_mount *tmp;
810 	struct tmpfs_node *dnode;
811 	struct tmpfs_node *node;
812 
813 	MPASS(VOP_ISLOCKED(dvp));
814 	MPASS(VOP_ISLOCKED(vp));
815 
816 	if (vp->v_type == VDIR) {
817 		error = EISDIR;
818 		goto out;
819 	}
820 
821 	dnode = VP_TO_TMPFS_DIR(dvp);
822 	node = VP_TO_TMPFS_NODE(vp);
823 	tmp = VFS_TO_TMPFS(vp->v_mount);
824 	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
825 	MPASS(de != NULL);
826 
827 	/* Files marked as immutable or append-only cannot be deleted. */
828 	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
829 	    (dnode->tn_flags & APPEND)) {
830 		error = EPERM;
831 		goto out;
832 	}
833 
834 	/* Remove the entry from the directory; as it is a file, we do not
835 	 * have to change the number of hard links of the directory. */
836 	tmpfs_dir_detach(dvp, de);
837 
838 	/* Free the directory entry we just deleted.  Note that the node
839 	 * referred by it will not be removed until the vnode is really
840 	 * reclaimed. */
841 	tmpfs_free_dirent(tmp, de, TRUE);
842 
843 	if (node->tn_links > 0)
844 		node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
845 	    TMPFS_NODE_MODIFIED;
846 	error = 0;
847 
848 out:
849 
850 	return error;
851 }
852 
853 /* --------------------------------------------------------------------- */
854 
855 static int
856 tmpfs_link(struct vop_link_args *v)
857 {
858 	struct vnode *dvp = v->a_tdvp;
859 	struct vnode *vp = v->a_vp;
860 	struct componentname *cnp = v->a_cnp;
861 
862 	int error;
863 	struct tmpfs_dirent *de;
864 	struct tmpfs_node *node;
865 
866 	MPASS(VOP_ISLOCKED(dvp));
867 	MPASS(cnp->cn_flags & HASBUF);
868 	MPASS(dvp != vp); /* XXX When can this be false? */
869 
870 	node = VP_TO_TMPFS_NODE(vp);
871 
872 	/* XXX: Why aren't the following two tests done by the caller? */
873 
874 	/* Hard links of directories are forbidden. */
875 	if (vp->v_type == VDIR) {
876 		error = EPERM;
877 		goto out;
878 	}
879 
880 	/* Cannot create cross-device links. */
881 	if (dvp->v_mount != vp->v_mount) {
882 		error = EXDEV;
883 		goto out;
884 	}
885 
886 	/* Ensure that we do not overflow the maximum number of links imposed
887 	 * by the system. */
888 	MPASS(node->tn_links <= LINK_MAX);
889 	if (node->tn_links == LINK_MAX) {
890 		error = EMLINK;
891 		goto out;
892 	}
893 
894 	/* We cannot create links of files marked immutable or append-only. */
895 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
896 		error = EPERM;
897 		goto out;
898 	}
899 
900 	/* Allocate a new directory entry to represent the node. */
901 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
902 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
903 	if (error != 0)
904 		goto out;
905 
906 	/* Insert the new directory entry into the appropriate directory. */
907 	tmpfs_dir_attach(dvp, de);
908 
909 	/* vp link count has changed, so update node times. */
910 	node->tn_status |= TMPFS_NODE_CHANGED;
911 	tmpfs_update(vp);
912 
913 	error = 0;
914 
915 out:
916 	return error;
917 }
918 
919 /* --------------------------------------------------------------------- */
920 
921 static int
922 tmpfs_rename(struct vop_rename_args *v)
923 {
924 	struct vnode *fdvp = v->a_fdvp;
925 	struct vnode *fvp = v->a_fvp;
926 	struct componentname *fcnp = v->a_fcnp;
927 	struct vnode *tdvp = v->a_tdvp;
928 	struct vnode *tvp = v->a_tvp;
929 	struct componentname *tcnp = v->a_tcnp;
930 
931 	char *newname;
932 	int error;
933 	struct tmpfs_dirent *de;
934 	struct tmpfs_mount *tmp;
935 	struct tmpfs_node *fdnode;
936 	struct tmpfs_node *fnode;
937 	struct tmpfs_node *tnode;
938 	struct tmpfs_node *tdnode;
939 
940 	MPASS(VOP_ISLOCKED(tdvp));
941 	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
942 	MPASS(fcnp->cn_flags & HASBUF);
943 	MPASS(tcnp->cn_flags & HASBUF);
944 
945   	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
946 
947 	/* Disallow cross-device renames.
948 	 * XXX Why isn't this done by the caller? */
949 	if (fvp->v_mount != tdvp->v_mount ||
950 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
951 		error = EXDEV;
952 		goto out;
953 	}
954 
955 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
956 	tdnode = VP_TO_TMPFS_DIR(tdvp);
957 
958 	/* If source and target are the same file, there is nothing to do. */
959 	if (fvp == tvp) {
960 		error = 0;
961 		goto out;
962 	}
963 
964 	/* If we need to move the directory between entries, lock the
965 	 * source so that we can safely operate on it. */
966 	if (tdvp != fdvp) {
967 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
968 		if (error != 0)
969 			goto out;
970 	}
971 	fdnode = VP_TO_TMPFS_DIR(fdvp);
972 	fnode = VP_TO_TMPFS_NODE(fvp);
973 	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
974 
975 	/* Avoid manipulating '.' and '..' entries. */
976 	if (de == NULL) {
977 		MPASS(fvp->v_type == VDIR);
978 		error = EINVAL;
979 		goto out_locked;
980 	}
981 	MPASS(de->td_node == fnode);
982 
983 	/* If re-naming a directory to another preexisting directory
984 	 * ensure that the target directory is empty so that its
985 	 * removal causes no side effects.
986 	 * Kern_rename gurantees the destination to be a directory
987 	 * if the source is one. */
988 	if (tvp != NULL) {
989 		MPASS(tnode != NULL);
990 
991 		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
992 		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
993 			error = EPERM;
994 			goto out_locked;
995 		}
996 
997 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
998 			if (tnode->tn_size > 0) {
999 				error = ENOTEMPTY;
1000 				goto out_locked;
1001 			}
1002 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1003 			error = ENOTDIR;
1004 			goto out_locked;
1005 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1006 			error = EISDIR;
1007 			goto out_locked;
1008 		} else {
1009 			MPASS(fnode->tn_type != VDIR &&
1010 				tnode->tn_type != VDIR);
1011 		}
1012 	}
1013 
1014 	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1015 	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1016 		error = EPERM;
1017 		goto out_locked;
1018 	}
1019 
1020 	/* Ensure that we have enough memory to hold the new name, if it
1021 	 * has to be changed. */
1022 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
1023 	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1024 		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1025 	} else
1026 		newname = NULL;
1027 
1028 	/* If the node is being moved to another directory, we have to do
1029 	 * the move. */
1030 	if (fdnode != tdnode) {
1031 		/* In case we are moving a directory, we have to adjust its
1032 		 * parent to point to the new parent. */
1033 		if (de->td_node->tn_type == VDIR) {
1034 			struct tmpfs_node *n;
1035 
1036 			/* Ensure the target directory is not a child of the
1037 			 * directory being moved.  Otherwise, we'd end up
1038 			 * with stale nodes. */
1039 			n = tdnode;
1040 			/* TMPFS_LOCK garanties that no nodes are freed while
1041 			 * traversing the list. Nodes can only be marked as
1042 			 * removed: tn_parent == NULL. */
1043 			TMPFS_LOCK(tmp);
1044 			TMPFS_NODE_LOCK(n);
1045 			while (n != n->tn_dir.tn_parent) {
1046 				struct tmpfs_node *parent;
1047 
1048 				if (n == fnode) {
1049 					TMPFS_NODE_UNLOCK(n);
1050 					TMPFS_UNLOCK(tmp);
1051 					error = EINVAL;
1052 					if (newname != NULL)
1053 						    free(newname, M_TMPFSNAME);
1054 					goto out_locked;
1055 				}
1056 				parent = n->tn_dir.tn_parent;
1057 				TMPFS_NODE_UNLOCK(n);
1058 				if (parent == NULL) {
1059 					n = NULL;
1060 					break;
1061 				}
1062 				TMPFS_NODE_LOCK(parent);
1063 				if (parent->tn_dir.tn_parent == NULL) {
1064 					TMPFS_NODE_UNLOCK(parent);
1065 					n = NULL;
1066 					break;
1067 				}
1068 				n = parent;
1069 			}
1070 			TMPFS_UNLOCK(tmp);
1071 			if (n == NULL) {
1072 				error = EINVAL;
1073 				if (newname != NULL)
1074 					    free(newname, M_TMPFSNAME);
1075 				goto out_locked;
1076 			}
1077 			TMPFS_NODE_UNLOCK(n);
1078 
1079 			/* Adjust the parent pointer. */
1080 			TMPFS_VALIDATE_DIR(fnode);
1081 			TMPFS_NODE_LOCK(de->td_node);
1082 			de->td_node->tn_dir.tn_parent = tdnode;
1083 			TMPFS_NODE_UNLOCK(de->td_node);
1084 
1085 			/* As a result of changing the target of the '..'
1086 			 * entry, the link count of the source and target
1087 			 * directories has to be adjusted. */
1088 			TMPFS_NODE_LOCK(tdnode);
1089 			TMPFS_ASSERT_LOCKED(tdnode);
1090 			tdnode->tn_links++;
1091 			TMPFS_NODE_UNLOCK(tdnode);
1092 
1093 			TMPFS_NODE_LOCK(fdnode);
1094 			TMPFS_ASSERT_LOCKED(fdnode);
1095 			fdnode->tn_links--;
1096 			TMPFS_NODE_UNLOCK(fdnode);
1097 		}
1098 
1099 		/* Do the move: just remove the entry from the source directory
1100 		 * and insert it into the target one. */
1101 		tmpfs_dir_detach(fdvp, de);
1102 		tmpfs_dir_attach(tdvp, de);
1103 	}
1104 
1105 	/* If the name has changed, we need to make it effective by changing
1106 	 * it in the directory entry. */
1107 	if (newname != NULL) {
1108 		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1109 
1110 		free(de->td_name, M_TMPFSNAME);
1111 		de->td_namelen = (uint16_t)tcnp->cn_namelen;
1112 		memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1113 		de->td_name = newname;
1114 
1115 		fnode->tn_status |= TMPFS_NODE_CHANGED;
1116 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1117 	}
1118 
1119 	/* If we are overwriting an entry, we have to remove the old one
1120 	 * from the target directory. */
1121 	if (tvp != NULL) {
1122 		/* Remove the old entry from the target directory. */
1123 		de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1124 		tmpfs_dir_detach(tdvp, de);
1125 
1126 		/* Free the directory entry we just deleted.  Note that the
1127 		 * node referred by it will not be removed until the vnode is
1128 		 * really reclaimed. */
1129 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1130 	}
1131 
1132 	error = 0;
1133 
1134 out_locked:
1135 	if (fdnode != tdnode)
1136 		VOP_UNLOCK(fdvp, 0);
1137 
1138 out:
1139 	/* Release target nodes. */
1140 	/* XXX: I don't understand when tdvp can be the same as tvp, but
1141 	 * other code takes care of this... */
1142 	if (tdvp == tvp)
1143 		vrele(tdvp);
1144 	else
1145 		vput(tdvp);
1146 	if (tvp != NULL)
1147 		vput(tvp);
1148 
1149 	/* Release source nodes. */
1150 	vrele(fdvp);
1151 	vrele(fvp);
1152 
1153 	return error;
1154 }
1155 
1156 /* --------------------------------------------------------------------- */
1157 
1158 static int
1159 tmpfs_mkdir(struct vop_mkdir_args *v)
1160 {
1161 	struct vnode *dvp = v->a_dvp;
1162 	struct vnode **vpp = v->a_vpp;
1163 	struct componentname *cnp = v->a_cnp;
1164 	struct vattr *vap = v->a_vap;
1165 
1166 	MPASS(vap->va_type == VDIR);
1167 
1168 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1169 }
1170 
1171 /* --------------------------------------------------------------------- */
1172 
1173 static int
1174 tmpfs_rmdir(struct vop_rmdir_args *v)
1175 {
1176 	struct vnode *dvp = v->a_dvp;
1177 	struct vnode *vp = v->a_vp;
1178 
1179 	int error;
1180 	struct tmpfs_dirent *de;
1181 	struct tmpfs_mount *tmp;
1182 	struct tmpfs_node *dnode;
1183 	struct tmpfs_node *node;
1184 
1185 	MPASS(VOP_ISLOCKED(dvp));
1186 	MPASS(VOP_ISLOCKED(vp));
1187 
1188 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1189 	dnode = VP_TO_TMPFS_DIR(dvp);
1190 	node = VP_TO_TMPFS_DIR(vp);
1191 
1192 	/* Directories with more than two entries ('.' and '..') cannot be
1193 	 * removed. */
1194 	 if (node->tn_size > 0) {
1195 		 error = ENOTEMPTY;
1196 		 goto out;
1197 	 }
1198 
1199 	if ((dnode->tn_flags & APPEND)
1200 	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1201 		error = EPERM;
1202 		goto out;
1203 	}
1204 
1205 	/* This invariant holds only if we are not trying to remove "..".
1206 	  * We checked for that above so this is safe now. */
1207 	MPASS(node->tn_dir.tn_parent == dnode);
1208 
1209 	/* Get the directory entry associated with node (vp).  This was
1210 	 * filled by tmpfs_lookup while looking up the entry. */
1211 	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1212 	MPASS(TMPFS_DIRENT_MATCHES(de,
1213 	    v->a_cnp->cn_nameptr,
1214 	    v->a_cnp->cn_namelen));
1215 
1216 	/* Check flags to see if we are allowed to remove the directory. */
1217 	if (dnode->tn_flags & APPEND
1218 		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1219 		error = EPERM;
1220 		goto out;
1221 	}
1222 
1223 
1224 	/* Detach the directory entry from the directory (dnode). */
1225 	tmpfs_dir_detach(dvp, de);
1226 
1227 	/* No vnode should be allocated for this entry from this point */
1228 	TMPFS_NODE_LOCK(node);
1229 	TMPFS_ASSERT_ELOCKED(node);
1230 	node->tn_links--;
1231 	node->tn_dir.tn_parent = NULL;
1232 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1233 	    TMPFS_NODE_MODIFIED;
1234 
1235 	TMPFS_NODE_UNLOCK(node);
1236 
1237 	TMPFS_NODE_LOCK(dnode);
1238 	TMPFS_ASSERT_ELOCKED(dnode);
1239 	dnode->tn_links--;
1240 	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1241 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1242 	TMPFS_NODE_UNLOCK(dnode);
1243 
1244 	cache_purge(dvp);
1245 	cache_purge(vp);
1246 
1247 	/* Free the directory entry we just deleted.  Note that the node
1248 	 * referred by it will not be removed until the vnode is really
1249 	 * reclaimed. */
1250 	tmpfs_free_dirent(tmp, de, TRUE);
1251 
1252 	/* Release the deleted vnode (will destroy the node, notify
1253 	 * interested parties and clean it from the cache). */
1254 
1255 	dnode->tn_status |= TMPFS_NODE_CHANGED;
1256 	tmpfs_update(dvp);
1257 
1258 	error = 0;
1259 
1260 out:
1261 	return error;
1262 }
1263 
1264 /* --------------------------------------------------------------------- */
1265 
1266 static int
1267 tmpfs_symlink(struct vop_symlink_args *v)
1268 {
1269 	struct vnode *dvp = v->a_dvp;
1270 	struct vnode **vpp = v->a_vpp;
1271 	struct componentname *cnp = v->a_cnp;
1272 	struct vattr *vap = v->a_vap;
1273 	char *target = v->a_target;
1274 
1275 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1276 	MPASS(vap->va_type == VLNK);
1277 #else
1278 	vap->va_type = VLNK;
1279 #endif
1280 
1281 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1282 }
1283 
1284 /* --------------------------------------------------------------------- */
1285 
1286 static int
1287 tmpfs_readdir(struct vop_readdir_args *v)
1288 {
1289 	struct vnode *vp = v->a_vp;
1290 	struct uio *uio = v->a_uio;
1291 	int *eofflag = v->a_eofflag;
1292 	u_long **cookies = v->a_cookies;
1293 	int *ncookies = v->a_ncookies;
1294 
1295 	int error;
1296 	off_t startoff;
1297 	off_t cnt = 0;
1298 	struct tmpfs_node *node;
1299 
1300 	/* This operation only makes sense on directory nodes. */
1301 	if (vp->v_type != VDIR)
1302 		return ENOTDIR;
1303 
1304 	node = VP_TO_TMPFS_DIR(vp);
1305 
1306 	startoff = uio->uio_offset;
1307 
1308 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1309 		error = tmpfs_dir_getdotdent(node, uio);
1310 		if (error != 0)
1311 			goto outok;
1312 		cnt++;
1313 	}
1314 
1315 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1316 		error = tmpfs_dir_getdotdotdent(node, uio);
1317 		if (error != 0)
1318 			goto outok;
1319 		cnt++;
1320 	}
1321 
1322 	error = tmpfs_dir_getdents(node, uio, &cnt);
1323 
1324 outok:
1325 	MPASS(error >= -1);
1326 
1327 	if (error == -1)
1328 		error = 0;
1329 
1330 	if (eofflag != NULL)
1331 		*eofflag =
1332 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1333 
1334 	/* Update NFS-related variables. */
1335 	if (error == 0 && cookies != NULL && ncookies != NULL) {
1336 		off_t i;
1337 		off_t off = startoff;
1338 		struct tmpfs_dirent *de = NULL;
1339 
1340 		*ncookies = cnt;
1341 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1342 
1343 		for (i = 0; i < cnt; i++) {
1344 			MPASS(off != TMPFS_DIRCOOKIE_EOF);
1345 			if (off == TMPFS_DIRCOOKIE_DOT) {
1346 				off = TMPFS_DIRCOOKIE_DOTDOT;
1347 			} else {
1348 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1349 					de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1350 				} else if (de != NULL) {
1351 					de = TAILQ_NEXT(de, td_entries);
1352 				} else {
1353 					de = tmpfs_dir_lookupbycookie(node,
1354 					    off);
1355 					MPASS(de != NULL);
1356 					de = TAILQ_NEXT(de, td_entries);
1357 				}
1358 				if (de == NULL)
1359 					off = TMPFS_DIRCOOKIE_EOF;
1360 				else
1361 					off = tmpfs_dircookie(de);
1362 			}
1363 
1364 			(*cookies)[i] = off;
1365 		}
1366 		MPASS(uio->uio_offset == off);
1367 	}
1368 
1369 	return error;
1370 }
1371 
1372 /* --------------------------------------------------------------------- */
1373 
1374 static int
1375 tmpfs_readlink(struct vop_readlink_args *v)
1376 {
1377 	struct vnode *vp = v->a_vp;
1378 	struct uio *uio = v->a_uio;
1379 
1380 	int error;
1381 	struct tmpfs_node *node;
1382 
1383 	MPASS(uio->uio_offset == 0);
1384 	MPASS(vp->v_type == VLNK);
1385 
1386 	node = VP_TO_TMPFS_NODE(vp);
1387 
1388 	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1389 	    uio);
1390 	node->tn_status |= TMPFS_NODE_ACCESSED;
1391 
1392 	return error;
1393 }
1394 
1395 /* --------------------------------------------------------------------- */
1396 
1397 static int
1398 tmpfs_inactive(struct vop_inactive_args *v)
1399 {
1400 	struct vnode *vp = v->a_vp;
1401 	struct thread *l = v->a_td;
1402 
1403 	struct tmpfs_node *node;
1404 
1405 	MPASS(VOP_ISLOCKED(vp));
1406 
1407 	node = VP_TO_TMPFS_NODE(vp);
1408 
1409 	if (node->tn_links == 0)
1410 		vrecycle(vp, l);
1411 
1412 	return 0;
1413 }
1414 
1415 /* --------------------------------------------------------------------- */
1416 
1417 int
1418 tmpfs_reclaim(struct vop_reclaim_args *v)
1419 {
1420 	struct vnode *vp = v->a_vp;
1421 
1422 	struct tmpfs_mount *tmp;
1423 	struct tmpfs_node *node;
1424 
1425 	node = VP_TO_TMPFS_NODE(vp);
1426 	tmp = VFS_TO_TMPFS(vp->v_mount);
1427 
1428 	vnode_destroy_vobject(vp);
1429 	cache_purge(vp);
1430 
1431 	TMPFS_NODE_LOCK(node);
1432 	TMPFS_ASSERT_ELOCKED(node);
1433 	tmpfs_free_vp(vp);
1434 
1435 	/* If the node referenced by this vnode was deleted by the user,
1436 	 * we must free its associated data structures (now that the vnode
1437 	 * is being reclaimed). */
1438 	if (node->tn_links == 0 &&
1439 	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1440 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1441 		TMPFS_NODE_UNLOCK(node);
1442 		tmpfs_free_node(tmp, node);
1443 	} else
1444 		TMPFS_NODE_UNLOCK(node);
1445 
1446 	MPASS(vp->v_data == NULL);
1447 	return 0;
1448 }
1449 
1450 /* --------------------------------------------------------------------- */
1451 
1452 static int
1453 tmpfs_print(struct vop_print_args *v)
1454 {
1455 	struct vnode *vp = v->a_vp;
1456 
1457 	struct tmpfs_node *node;
1458 
1459 	node = VP_TO_TMPFS_NODE(vp);
1460 
1461 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1462 	    node, node->tn_flags, node->tn_links);
1463 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1464 	    ", status 0x%x\n",
1465 	    node->tn_mode, node->tn_uid, node->tn_gid,
1466 	    (uintmax_t)node->tn_size, node->tn_status);
1467 
1468 	if (vp->v_type == VFIFO)
1469 		fifo_printinfo(vp);
1470 
1471 	printf("\n");
1472 
1473 	return 0;
1474 }
1475 
1476 /* --------------------------------------------------------------------- */
1477 
1478 static int
1479 tmpfs_pathconf(struct vop_pathconf_args *v)
1480 {
1481 	int name = v->a_name;
1482 	register_t *retval = v->a_retval;
1483 
1484 	int error;
1485 
1486 	error = 0;
1487 
1488 	switch (name) {
1489 	case _PC_LINK_MAX:
1490 		*retval = LINK_MAX;
1491 		break;
1492 
1493 	case _PC_NAME_MAX:
1494 		*retval = NAME_MAX;
1495 		break;
1496 
1497 	case _PC_PATH_MAX:
1498 		*retval = PATH_MAX;
1499 		break;
1500 
1501 	case _PC_PIPE_BUF:
1502 		*retval = PIPE_BUF;
1503 		break;
1504 
1505 	case _PC_CHOWN_RESTRICTED:
1506 		*retval = 1;
1507 		break;
1508 
1509 	case _PC_NO_TRUNC:
1510 		*retval = 1;
1511 		break;
1512 
1513 	case _PC_SYNC_IO:
1514 		*retval = 1;
1515 		break;
1516 
1517 	case _PC_FILESIZEBITS:
1518 		*retval = 0; /* XXX Don't know which value should I return. */
1519 		break;
1520 
1521 	default:
1522 		error = EINVAL;
1523 	}
1524 
1525 	return error;
1526 }
1527 
1528 static int
1529 tmpfs_vptofh(struct vop_vptofh_args *ap)
1530 {
1531 	struct tmpfs_fid *tfhp;
1532 	struct tmpfs_node *node;
1533 
1534 	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1535 	node = VP_TO_TMPFS_NODE(ap->a_vp);
1536 
1537 	tfhp->tf_len = sizeof(struct tmpfs_fid);
1538 	tfhp->tf_id = node->tn_id;
1539 	tfhp->tf_gen = node->tn_gen;
1540 
1541 	return (0);
1542 }
1543 
1544 /* --------------------------------------------------------------------- */
1545 
1546 /*
1547  * vnode operations vector used for files stored in a tmpfs file system.
1548  */
1549 struct vop_vector tmpfs_vnodeop_entries = {
1550 	.vop_default =			&default_vnodeops,
1551 	.vop_lookup =			vfs_cache_lookup,
1552 	.vop_cachedlookup =		tmpfs_lookup,
1553 	.vop_create =			tmpfs_create,
1554 	.vop_mknod =			tmpfs_mknod,
1555 	.vop_open =			tmpfs_open,
1556 	.vop_close =			tmpfs_close,
1557 	.vop_access =			tmpfs_access,
1558 	.vop_getattr =			tmpfs_getattr,
1559 	.vop_setattr =			tmpfs_setattr,
1560 	.vop_read =			tmpfs_read,
1561 	.vop_write =			tmpfs_write,
1562 	.vop_fsync =			tmpfs_fsync,
1563 	.vop_remove =			tmpfs_remove,
1564 	.vop_link =			tmpfs_link,
1565 	.vop_rename =			tmpfs_rename,
1566 	.vop_mkdir =			tmpfs_mkdir,
1567 	.vop_rmdir =			tmpfs_rmdir,
1568 	.vop_symlink =			tmpfs_symlink,
1569 	.vop_readdir =			tmpfs_readdir,
1570 	.vop_readlink =			tmpfs_readlink,
1571 	.vop_inactive =			tmpfs_inactive,
1572 	.vop_reclaim =			tmpfs_reclaim,
1573 	.vop_print =			tmpfs_print,
1574 	.vop_pathconf =			tmpfs_pathconf,
1575 	.vop_vptofh =			tmpfs_vptofh,
1576 	.vop_bmap =			VOP_EOPNOTSUPP,
1577 };
1578 
1579