xref: /netbsd-src/sys/fs/tmpfs/tmpfs_vnops.c (revision 4e1f96faf8dda38f639610fb3e7474dee1c26ab2)
1 /*	$NetBSD: tmpfs_vnops.c,v 1.60 2009/06/23 19:36:39 elad Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006, 2007 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 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.60 2009/06/23 19:36:39 elad Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/dirent.h>
42 #include <sys/fcntl.h>
43 #include <sys/event.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/stat.h>
48 #include <sys/uio.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
51 #include <sys/lockf.h>
52 #include <sys/kauth.h>
53 
54 #include <uvm/uvm.h>
55 
56 #include <miscfs/fifofs/fifo.h>
57 #include <miscfs/genfs/genfs.h>
58 #include <fs/tmpfs/tmpfs_vnops.h>
59 #include <fs/tmpfs/tmpfs.h>
60 
61 /* --------------------------------------------------------------------- */
62 
63 /*
64  * vnode operations vector used for files stored in a tmpfs file system.
65  */
66 int (**tmpfs_vnodeop_p)(void *);
67 const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
68 	{ &vop_default_desc,		vn_default_error },
69 	{ &vop_lookup_desc,		tmpfs_lookup },
70 	{ &vop_create_desc,		tmpfs_create },
71 	{ &vop_mknod_desc,		tmpfs_mknod },
72 	{ &vop_open_desc,		tmpfs_open },
73 	{ &vop_close_desc,		tmpfs_close },
74 	{ &vop_access_desc,		tmpfs_access },
75 	{ &vop_getattr_desc,		tmpfs_getattr },
76 	{ &vop_setattr_desc,		tmpfs_setattr },
77 	{ &vop_read_desc,		tmpfs_read },
78 	{ &vop_write_desc,		tmpfs_write },
79 	{ &vop_ioctl_desc,		tmpfs_ioctl },
80 	{ &vop_fcntl_desc,		tmpfs_fcntl },
81 	{ &vop_poll_desc,		tmpfs_poll },
82 	{ &vop_kqfilter_desc,		tmpfs_kqfilter },
83 	{ &vop_revoke_desc,		tmpfs_revoke },
84 	{ &vop_mmap_desc,		tmpfs_mmap },
85 	{ &vop_fsync_desc,		tmpfs_fsync },
86 	{ &vop_seek_desc,		tmpfs_seek },
87 	{ &vop_remove_desc,		tmpfs_remove },
88 	{ &vop_link_desc,		tmpfs_link },
89 	{ &vop_rename_desc,		tmpfs_rename },
90 	{ &vop_mkdir_desc,		tmpfs_mkdir },
91 	{ &vop_rmdir_desc,		tmpfs_rmdir },
92 	{ &vop_symlink_desc,		tmpfs_symlink },
93 	{ &vop_readdir_desc,		tmpfs_readdir },
94 	{ &vop_readlink_desc,		tmpfs_readlink },
95 	{ &vop_abortop_desc,		tmpfs_abortop },
96 	{ &vop_inactive_desc,		tmpfs_inactive },
97 	{ &vop_reclaim_desc,		tmpfs_reclaim },
98 	{ &vop_lock_desc,		tmpfs_lock },
99 	{ &vop_unlock_desc,		tmpfs_unlock },
100 	{ &vop_bmap_desc,		tmpfs_bmap },
101 	{ &vop_strategy_desc,		tmpfs_strategy },
102 	{ &vop_print_desc,		tmpfs_print },
103 	{ &vop_pathconf_desc,		tmpfs_pathconf },
104 	{ &vop_islocked_desc,		tmpfs_islocked },
105 	{ &vop_advlock_desc,		tmpfs_advlock },
106 	{ &vop_bwrite_desc,		tmpfs_bwrite },
107 	{ &vop_getpages_desc,		tmpfs_getpages },
108 	{ &vop_putpages_desc,		tmpfs_putpages },
109 	{ NULL, NULL }
110 };
111 const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
112 	{ &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
113 
114 /* --------------------------------------------------------------------- */
115 
116 int
117 tmpfs_lookup(void *v)
118 {
119 	struct vnode *dvp = ((struct vop_lookup_args *)v)->a_dvp;
120 	struct vnode **vpp = ((struct vop_lookup_args *)v)->a_vpp;
121 	struct componentname *cnp = ((struct vop_lookup_args *)v)->a_cnp;
122 
123 	int error;
124 	struct tmpfs_dirent *de;
125 	struct tmpfs_node *dnode;
126 
127 	KASSERT(VOP_ISLOCKED(dvp));
128 
129 	dnode = VP_TO_TMPFS_DIR(dvp);
130 	*vpp = NULL;
131 
132 	/* Check accessibility of requested node as a first step. */
133 	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
134 	if (error != 0)
135 		goto out;
136 
137 	/* If requesting the last path component on a read-only file system
138 	 * with a write operation, deny it. */
139 	if ((cnp->cn_flags & ISLASTCN) &&
140 	    (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
141 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
142 		error = EROFS;
143 		goto out;
144 	}
145 
146 	/* Avoid doing a linear scan of the directory if the requested
147 	 * directory/name couple is already in the cache. */
148 	error = cache_lookup(dvp, vpp, cnp);
149 	if (error >= 0)
150 		goto out;
151 
152 	/* We cannot be requesting the parent directory of the root node. */
153 	KASSERT(IMPLIES(dnode->tn_type == VDIR &&
154 	    dnode->tn_spec.tn_dir.tn_parent == dnode,
155 	    !(cnp->cn_flags & ISDOTDOT)));
156 
157 	if (cnp->cn_flags & ISDOTDOT) {
158 		VOP_UNLOCK(dvp, 0);
159 
160 		/* Allocate a new vnode on the matching entry. */
161 		error = tmpfs_alloc_vp(dvp->v_mount,
162 		    dnode->tn_spec.tn_dir.tn_parent, vpp);
163 
164 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
165 	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
166 		VREF(dvp);
167 		*vpp = dvp;
168 		error = 0;
169 	} else {
170 		de = tmpfs_dir_lookup(dnode, cnp);
171 		if (de == NULL) {
172 			/* The entry was not found in the directory.
173 			 * This is OK iff we are creating or renaming an
174 			 * entry and are working on the last component of
175 			 * the path name. */
176 			if ((cnp->cn_flags & ISLASTCN) &&
177 			    (cnp->cn_nameiop == CREATE || \
178 			    cnp->cn_nameiop == RENAME)) {
179 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
180 				if (error != 0)
181 					goto out;
182 
183 				/* Keep the component name in the buffer for
184 				 * future uses. */
185 				cnp->cn_flags |= SAVENAME;
186 
187 				error = EJUSTRETURN;
188 			} else
189 				error = ENOENT;
190 		} else {
191 			struct tmpfs_node *tnode;
192 
193 			/* The entry was found, so get its associated
194 			 * tmpfs_node. */
195 			tnode = de->td_node;
196 
197 			/* If we are not at the last path component and
198 			 * found a non-directory or non-link entry (which
199 			 * may itself be pointing to a directory), raise
200 			 * an error. */
201 			if ((tnode->tn_type != VDIR &&
202 			    tnode->tn_type != VLNK) &&
203 			    !(cnp->cn_flags & ISLASTCN)) {
204 				error = ENOTDIR;
205 				goto out;
206 			}
207 
208 			/* Check permissions */
209 			if ((cnp->cn_flags & ISLASTCN) &&
210 			    (cnp->cn_nameiop == DELETE ||
211 			    cnp->cn_nameiop == RENAME)) {
212 				if ((dnode->tn_mode & S_ISTXT) != 0 &&
213 				    kauth_authorize_generic(cnp->cn_cred,
214 				     KAUTH_GENERIC_ISSUSER, NULL) != 0 &&
215 				    kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
216 				    kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid)
217 					return EPERM;
218 				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
219 				if (error != 0)
220 					goto out;
221 				cnp->cn_flags |= SAVENAME;
222 			} else
223 				de = NULL;
224 
225 			/* Allocate a new vnode on the matching entry. */
226 			error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
227 		}
228 	}
229 
230 	/* Store the result of this lookup in the cache.  Avoid this if the
231 	 * request was for creation, as it does not improve timings on
232 	 * emprical tests. */
233 	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
234 	    (cnp->cn_flags & ISDOTDOT) == 0)
235 		cache_enter(dvp, *vpp, cnp);
236 
237 out:
238 	/* If there were no errors, *vpp cannot be null and it must be
239 	 * locked. */
240 	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
241 
242 	/* dvp must always be locked. */
243 	KASSERT(VOP_ISLOCKED(dvp));
244 
245 	return error;
246 }
247 
248 /* --------------------------------------------------------------------- */
249 
250 int
251 tmpfs_create(void *v)
252 {
253 	struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
254 	struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
255 	struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
256 	struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
257 
258 	KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
259 
260 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
261 }
262 /* --------------------------------------------------------------------- */
263 
264 int
265 tmpfs_mknod(void *v)
266 {
267 	struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
268 	struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
269 	struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
270 	struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
271 
272 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
273 	    vap->va_type != VFIFO) {
274 		vput(dvp);
275 		return EINVAL;
276 	}
277 
278 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
279 }
280 
281 /* --------------------------------------------------------------------- */
282 
283 int
284 tmpfs_open(void *v)
285 {
286 	struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
287 	int mode = ((struct vop_open_args *)v)->a_mode;
288 
289 	int error;
290 	struct tmpfs_node *node;
291 
292 	KASSERT(VOP_ISLOCKED(vp));
293 
294 	node = VP_TO_TMPFS_NODE(vp);
295 
296 	/* The file is still active but all its names have been removed
297 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
298 	 * it is about to die. */
299 	if (node->tn_links < 1) {
300 		error = ENOENT;
301 		goto out;
302 	}
303 
304 	/* If the file is marked append-only, deny write requests. */
305 	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
306 		error = EPERM;
307 	else
308 		error = 0;
309 
310 out:
311 	KASSERT(VOP_ISLOCKED(vp));
312 
313 	return error;
314 }
315 
316 /* --------------------------------------------------------------------- */
317 
318 int
319 tmpfs_close(void *v)
320 {
321 	struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
322 
323 	struct tmpfs_node *node;
324 
325 	KASSERT(VOP_ISLOCKED(vp));
326 
327 	node = VP_TO_TMPFS_NODE(vp);
328 
329 	if (node->tn_links > 0) {
330 		/* Update node times.  No need to do it if the node has
331 		 * been deleted, because it will vanish after we return. */
332 		tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
333 	}
334 
335 	return 0;
336 }
337 
338 /* --------------------------------------------------------------------- */
339 
340 int
341 tmpfs_access(void *v)
342 {
343 	struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
344 	int mode = ((struct vop_access_args *)v)->a_mode;
345 	kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred;
346 
347 	int error;
348 	struct tmpfs_node *node;
349 
350 	KASSERT(VOP_ISLOCKED(vp));
351 
352 	node = VP_TO_TMPFS_NODE(vp);
353 
354 	switch (vp->v_type) {
355 	case VDIR:
356 		/* FALLTHROUGH */
357 	case VLNK:
358 		/* FALLTHROUGH */
359 	case VREG:
360 		if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
361 			error = EROFS;
362 			goto out;
363 		}
364 		break;
365 
366 	case VBLK:
367 		/* FALLTHROUGH */
368 	case VCHR:
369 		/* FALLTHROUGH */
370 	case VSOCK:
371 		/* FALLTHROUGH */
372 	case VFIFO:
373 		break;
374 
375 	default:
376 		error = EINVAL;
377 		goto out;
378 	}
379 
380 	if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
381 		error = EPERM;
382 		goto out;
383 	}
384 
385 	error = genfs_can_access(vp->v_type, node->tn_mode, node->tn_uid,
386 	    node->tn_gid, mode, cred);
387 
388 out:
389 	KASSERT(VOP_ISLOCKED(vp));
390 
391 	return error;
392 }
393 
394 /* --------------------------------------------------------------------- */
395 
396 int
397 tmpfs_getattr(void *v)
398 {
399 	struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
400 	struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
401 
402 	struct tmpfs_node *node;
403 
404 	node = VP_TO_TMPFS_NODE(vp);
405 
406 	VATTR_NULL(vap);
407 
408 	tmpfs_itimes(vp, NULL, NULL, NULL);
409 
410 	vap->va_type = vp->v_type;
411 	vap->va_mode = node->tn_mode;
412 	vap->va_nlink = node->tn_links;
413 	vap->va_uid = node->tn_uid;
414 	vap->va_gid = node->tn_gid;
415 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
416 	vap->va_fileid = node->tn_id;
417 	vap->va_size = node->tn_size;
418 	vap->va_blocksize = PAGE_SIZE;
419 	vap->va_atime = node->tn_atime;
420 	vap->va_mtime = node->tn_mtime;
421 	vap->va_ctime = node->tn_ctime;
422 	vap->va_birthtime = node->tn_birthtime;
423 	vap->va_gen = node->tn_gen;
424 	vap->va_flags = node->tn_flags;
425 	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
426 		node->tn_spec.tn_dev.tn_rdev : VNOVAL;
427 	vap->va_bytes = round_page(node->tn_size);
428 	vap->va_filerev = VNOVAL;
429 	vap->va_vaflags = 0;
430 	vap->va_spare = VNOVAL; /* XXX */
431 
432 	return 0;
433 }
434 
435 /* --------------------------------------------------------------------- */
436 
437 #define GOODTIME(tv)	((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
438 /* XXX Should this operation be atomic?  I think it should, but code in
439  * XXX other places (e.g., ufs) doesn't seem to be... */
440 int
441 tmpfs_setattr(void *v)
442 {
443 	struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
444 	struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
445 	kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred;
446 	struct lwp *l = curlwp;
447 
448 	int error;
449 
450 	KASSERT(VOP_ISLOCKED(vp));
451 
452 	error = 0;
453 
454 	/* Abort if any unsettable attribute is given. */
455 	if (vap->va_type != VNON ||
456 	    vap->va_nlink != VNOVAL ||
457 	    vap->va_fsid != VNOVAL ||
458 	    vap->va_fileid != VNOVAL ||
459 	    vap->va_blocksize != VNOVAL ||
460 	    GOODTIME(&vap->va_ctime) ||
461 	    vap->va_gen != VNOVAL ||
462 	    vap->va_rdev != VNOVAL ||
463 	    vap->va_bytes != VNOVAL)
464 		error = EINVAL;
465 
466 	if (error == 0 && (vap->va_flags != VNOVAL))
467 		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
468 
469 	if (error == 0 && (vap->va_size != VNOVAL))
470 		error = tmpfs_chsize(vp, vap->va_size, cred, l);
471 
472 	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
473 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
474 
475 	if (error == 0 && (vap->va_mode != VNOVAL))
476 		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
477 
478 	if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
479 	    || GOODTIME(&vap->va_birthtime)))
480 		if ((error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
481 		    &vap->va_birthtime, vap->va_vaflags, cred, l)) == 0)
482 			return 0;
483 
484 	/* Update the node times.  We give preference to the error codes
485 	 * generated by this function rather than the ones that may arise
486 	 * from tmpfs_update. */
487 	tmpfs_update(vp, NULL, NULL, NULL, 0);
488 
489 	KASSERT(VOP_ISLOCKED(vp));
490 
491 	return error;
492 }
493 
494 /* --------------------------------------------------------------------- */
495 
496 int
497 tmpfs_read(void *v)
498 {
499 	struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
500 	struct uio *uio = ((struct vop_read_args *)v)->a_uio;
501 	int ioflag = ((struct vop_read_args *)v)->a_ioflag;
502 
503 	int error;
504 	struct tmpfs_node *node;
505 	struct uvm_object *uobj;
506 
507 	KASSERT(VOP_ISLOCKED(vp));
508 
509 	node = VP_TO_TMPFS_NODE(vp);
510 
511 	if (vp->v_type != VREG) {
512 		error = EISDIR;
513 		goto out;
514 	}
515 
516 	if (uio->uio_offset < 0) {
517 		error = EINVAL;
518 		goto out;
519 	}
520 
521 	node->tn_status |= TMPFS_NODE_ACCESSED;
522 
523 	uobj = node->tn_spec.tn_reg.tn_aobj;
524 	error = 0;
525 	while (error == 0 && uio->uio_resid > 0) {
526 		vsize_t len;
527 
528 		if (node->tn_size <= uio->uio_offset)
529 			break;
530 
531 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
532 		if (len == 0)
533 			break;
534 
535 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
536 		    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
537 	}
538 
539 out:
540 	KASSERT(VOP_ISLOCKED(vp));
541 
542 	return error;
543 }
544 
545 /* --------------------------------------------------------------------- */
546 
547 int
548 tmpfs_write(void *v)
549 {
550 	struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
551 	struct uio *uio = ((struct vop_write_args *)v)->a_uio;
552 	int ioflag = ((struct vop_write_args *)v)->a_ioflag;
553 
554 	bool extended;
555 	int error;
556 	off_t oldsize;
557 	struct tmpfs_node *node;
558 	struct uvm_object *uobj;
559 
560 	KASSERT(VOP_ISLOCKED(vp));
561 
562 	node = VP_TO_TMPFS_NODE(vp);
563 	oldsize = node->tn_size;
564 
565 	if (uio->uio_offset < 0 || vp->v_type != VREG) {
566 		error = EINVAL;
567 		goto out;
568 	}
569 
570 	if (uio->uio_resid == 0) {
571 		error = 0;
572 		goto out;
573 	}
574 
575 	if (ioflag & IO_APPEND)
576 		uio->uio_offset = node->tn_size;
577 
578 	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
579 	if (extended) {
580 		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
581 		if (error != 0)
582 			goto out;
583 	}
584 
585 	uobj = node->tn_spec.tn_reg.tn_aobj;
586 	error = 0;
587 	while (error == 0 && uio->uio_resid > 0) {
588 		vsize_t len;
589 
590 		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
591 		if (len == 0)
592 			break;
593 
594 		error = ubc_uiomove(uobj, uio, len, IO_ADV_DECODE(ioflag),
595 		    UBC_WRITE | UBC_UNMAP_FLAG(vp));
596 	}
597 
598 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
599 	    (extended ? TMPFS_NODE_CHANGED : 0);
600 
601 	if (error != 0)
602 		(void)tmpfs_reg_resize(vp, oldsize);
603 
604 	VN_KNOTE(vp, NOTE_WRITE);
605 
606 out:
607 	KASSERT(VOP_ISLOCKED(vp));
608 	KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
609 	KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
610 
611 	return error;
612 }
613 
614 /* --------------------------------------------------------------------- */
615 
616 int
617 tmpfs_fsync(void *v)
618 {
619 	struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
620 
621 	KASSERT(VOP_ISLOCKED(vp));
622 
623 	tmpfs_update(vp, NULL, NULL, NULL, 0);
624 
625 	return 0;
626 }
627 
628 /* --------------------------------------------------------------------- */
629 
630 int
631 tmpfs_remove(void *v)
632 {
633 	struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
634 	struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
635 	struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp);
636 
637 	int error;
638 	struct tmpfs_dirent *de;
639 	struct tmpfs_mount *tmp;
640 	struct tmpfs_node *dnode;
641 	struct tmpfs_node *node;
642 
643 	KASSERT(VOP_ISLOCKED(dvp));
644 	KASSERT(VOP_ISLOCKED(vp));
645 
646 	if (vp->v_type == VDIR) {
647 		error = EPERM;
648 		goto out;
649 	}
650 
651 	dnode = VP_TO_TMPFS_DIR(dvp);
652 	node = VP_TO_TMPFS_NODE(vp);
653 	tmp = VFS_TO_TMPFS(vp->v_mount);
654 	de = tmpfs_dir_lookup(dnode, cnp);
655 	KASSERT(de);
656 	KASSERT(de->td_node == node);
657 
658 	/* Files marked as immutable or append-only cannot be deleted. */
659 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
660 		error = EPERM;
661 		goto out;
662 	}
663 
664 	/* Remove the entry from the directory; as it is a file, we do not
665 	 * have to change the number of hard links of the directory. */
666 	tmpfs_dir_detach(dvp, de);
667 
668 	/* Free the directory entry we just deleted.  Note that the node
669 	 * referred by it will not be removed until the vnode is really
670 	 * reclaimed. */
671 	tmpfs_free_dirent(tmp, de, true);
672 
673 	error = 0;
674 
675 out:
676 	vput(vp);
677 	if (dvp == vp)
678 		vrele(dvp);
679 	else
680 		vput(dvp);
681 	PNBUF_PUT(cnp->cn_pnbuf);
682 
683 	return error;
684 }
685 
686 /* --------------------------------------------------------------------- */
687 
688 int
689 tmpfs_link(void *v)
690 {
691 	struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
692 	struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
693 	struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
694 
695 	int error;
696 	struct tmpfs_dirent *de;
697 	struct tmpfs_node *dnode;
698 	struct tmpfs_node *node;
699 
700 	KASSERT(VOP_ISLOCKED(dvp));
701 	KASSERT(cnp->cn_flags & HASBUF);
702 	KASSERT(dvp != vp); /* XXX When can this be false? */
703 
704 	dnode = VP_TO_TMPFS_DIR(dvp);
705 	node = VP_TO_TMPFS_NODE(vp);
706 
707 	/* Lock vp because we will need to run tmpfs_update over it, which
708 	 * needs the vnode to be locked. */
709 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
710 	if (error != 0)
711 		goto out1;
712 
713 	/* XXX: Why aren't the following two tests done by the caller? */
714 
715 	/* Hard links of directories are forbidden. */
716 	if (vp->v_type == VDIR) {
717 		error = EPERM;
718 		goto out;
719 	}
720 
721 	/* Cannot create cross-device links. */
722 	if (dvp->v_mount != vp->v_mount) {
723 		error = EXDEV;
724 		goto out;
725 	}
726 
727 	/* Ensure that we do not overflow the maximum number of links imposed
728 	 * by the system. */
729 	KASSERT(node->tn_links <= LINK_MAX);
730 	if (node->tn_links == LINK_MAX) {
731 		error = EMLINK;
732 		goto out;
733 	}
734 
735 	/* We cannot create links of files marked immutable or append-only. */
736 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
737 		error = EPERM;
738 		goto out;
739 	}
740 
741 	/* Allocate a new directory entry to represent the node. */
742 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
743 	    cnp->cn_nameptr, cnp->cn_namelen, &de);
744 	if (error != 0)
745 		goto out;
746 
747 	/* Insert the new directory entry into the appropriate directory. */
748 	tmpfs_dir_attach(dvp, de);
749 
750 	/* vp link count has changed, so update node times. */
751 	node->tn_status |= TMPFS_NODE_CHANGED;
752 	tmpfs_update(vp, NULL, NULL, NULL, 0);
753 
754 	error = 0;
755 
756 out:
757 	VOP_UNLOCK(vp, 0);
758 out1:
759 	PNBUF_PUT(cnp->cn_pnbuf);
760 
761 	vput(dvp);
762 
763 	return error;
764 }
765 
766 /* --------------------------------------------------------------------- */
767 
768 int
769 tmpfs_rename(void *v)
770 {
771 	struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
772 	struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
773 	struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
774 	struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
775 	struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
776 	struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
777 
778 	char *newname;
779 	int error;
780 	struct tmpfs_dirent *de, *de2;
781 	struct tmpfs_mount *tmp;
782 	struct tmpfs_node *fdnode;
783 	struct tmpfs_node *fnode;
784 	struct tmpfs_node *tnode;
785 	struct tmpfs_node *tdnode;
786 	size_t namelen;
787 
788 	KASSERT(VOP_ISLOCKED(tdvp));
789 	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
790 	KASSERT(fcnp->cn_flags & HASBUF);
791 	KASSERT(tcnp->cn_flags & HASBUF);
792 
793 	newname = NULL;
794 	namelen = 0;
795 	tmp = NULL;
796 
797 	/* Disallow cross-device renames. */
798 	if (fvp->v_mount != tdvp->v_mount ||
799 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
800 		error = EXDEV;
801 		goto out_unlocked;
802 	}
803 
804 	fnode = VP_TO_TMPFS_NODE(fvp);
805 	fdnode = VP_TO_TMPFS_DIR(fdvp);
806 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
807 	tdnode = VP_TO_TMPFS_DIR(tdvp);
808 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
809 
810 	if (fdvp == tvp) {
811 		error = 0;
812 		goto out_unlocked;
813 	}
814 
815 	/* If we need to move the directory between entries, lock the
816 	 * source so that we can safely operate on it. */
817 
818 	/* XXX: this is a potential locking order violation! */
819 	if (fdnode != tdnode) {
820 		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
821 		if (error != 0)
822 			goto out_unlocked;
823 	}
824 
825 	/*
826 	 * If the node we were renaming has scarpered, just give up.
827 	 */
828 	de = tmpfs_dir_lookup(fdnode, fcnp);
829 	if (de == NULL || de->td_node != fnode) {
830 		error = ENOENT;
831 		goto out;
832 	}
833 
834 	/* If source and target are the same file, there is nothing to do. */
835 	if (fvp == tvp) {
836 		error = 0;
837 		goto out;
838 	}
839 
840 	/* If replacing an existing entry, ensure we can do the operation. */
841 	if (tvp != NULL) {
842 		KASSERT(tnode != NULL);
843 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
844 			if (tnode->tn_size > 0) {
845 				error = ENOTEMPTY;
846 				goto out;
847 			}
848 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
849 			error = ENOTDIR;
850 			goto out;
851 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
852 			error = EISDIR;
853 			goto out;
854 		} else {
855 			KASSERT(fnode->tn_type != VDIR &&
856 			        tnode->tn_type != VDIR);
857 		}
858 	}
859 
860 	/* Ensure that we have enough memory to hold the new name, if it
861 	 * has to be changed. */
862 	namelen = tcnp->cn_namelen;
863 	if (fcnp->cn_namelen != tcnp->cn_namelen ||
864 	    memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
865 		newname = tmpfs_str_pool_get(&tmp->tm_str_pool, namelen, 0);
866 		if (newname == NULL) {
867 			error = ENOSPC;
868 			goto out;
869 		}
870 	}
871 
872 	/* If the node is being moved to another directory, we have to do
873 	 * the move. */
874 	if (fdnode != tdnode) {
875 		/* In case we are moving a directory, we have to adjust its
876 		 * parent to point to the new parent. */
877 		if (de->td_node->tn_type == VDIR) {
878 			struct tmpfs_node *n;
879 
880 			/* Ensure the target directory is not a child of the
881 			 * directory being moved.  Otherwise, we'd end up
882 			 * with stale nodes. */
883 			n = tdnode;
884 			while (n != n->tn_spec.tn_dir.tn_parent) {
885 				if (n == fnode) {
886 					error = EINVAL;
887 					goto out;
888 				}
889 				n = n->tn_spec.tn_dir.tn_parent;
890 			}
891 
892 			/* Adjust the parent pointer. */
893 			TMPFS_VALIDATE_DIR(fnode);
894 			de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
895 
896 			/* As a result of changing the target of the '..'
897 			 * entry, the link count of the source and target
898 			 * directories has to be adjusted. */
899 			fdnode->tn_links--;
900 			tdnode->tn_links++;
901 		}
902 
903 		/* Do the move: just remove the entry from the source directory
904 		 * and insert it into the target one. */
905 		tmpfs_dir_detach(fdvp, de);
906 		tmpfs_dir_attach(tdvp, de);
907 
908 		/* Notify listeners of fdvp about the change in the directory.
909 		 * We can do it at this point because we aren't touching fdvp
910 		 * any more below. */
911 		VN_KNOTE(fdvp, NOTE_WRITE);
912 	}
913 
914 	/* If we are overwriting an entry, we have to remove the old one
915 	 * from the target directory. */
916 	if (tvp != NULL) {
917 		KASSERT(tnode != NULL);
918 
919 		/* Remove the old entry from the target directory.
920 		 * Note! This relies on tmpfs_dir_attach() putting the new
921 		 * node on the end of the target's node list. */
922 		de2 = tmpfs_dir_lookup(tdnode, tcnp);
923 		KASSERT(de2 != NULL);
924 		KASSERT(de2->td_node == tnode);
925 		tmpfs_dir_detach(tdvp, de2);
926 
927 		/* Free the directory entry we just deleted.  Note that the
928 		 * node referred by it will not be removed until the vnode is
929 		 * really reclaimed. */
930 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true);
931 	}
932 
933 	/* If the name has changed, we need to make it effective by changing
934 	 * it in the directory entry. */
935 	if (newname != NULL) {
936 		KASSERT(tcnp->cn_namelen < MAXNAMLEN);
937 		KASSERT(tcnp->cn_namelen < 0xffff);
938 
939 		tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
940 		    de->td_namelen);
941 		de->td_namelen = (uint16_t)namelen;
942 		memcpy(newname, tcnp->cn_nameptr, namelen);
943 		de->td_name = newname;
944 		newname = NULL;
945 
946 		fnode->tn_status |= TMPFS_NODE_CHANGED;
947 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
948 	}
949 
950 	/* Notify listeners of tdvp about the change in the directory (either
951 	 * because a new entry was added or because one was removed) and
952 	 * listeners of fvp about the rename. */
953 	VN_KNOTE(tdvp, NOTE_WRITE);
954 	VN_KNOTE(fvp, NOTE_RENAME);
955 
956 	error = 0;
957 
958  out:
959 	if (fdnode != tdnode)
960 		VOP_UNLOCK(fdvp, 0);
961 
962  out_unlocked:
963 	/* Release target nodes. */
964 	if (tdvp == tvp)
965 		vrele(tdvp);
966 	else
967 		vput(tdvp);
968 	if (tvp != NULL)
969 		vput(tvp);
970 
971 	/* Release source nodes. */
972 	vrele(fdvp);
973 	vrele(fvp);
974 
975 	if (newname != NULL)
976 		tmpfs_str_pool_put(&tmp->tm_str_pool, newname, namelen);
977 
978 	return error;
979 }
980 
981 /* --------------------------------------------------------------------- */
982 
983 int
984 tmpfs_mkdir(void *v)
985 {
986 	struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
987 	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
988 	struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
989 	struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
990 
991 	KASSERT(vap->va_type == VDIR);
992 
993 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
994 }
995 
996 /* --------------------------------------------------------------------- */
997 
998 int
999 tmpfs_rmdir(void *v)
1000 {
1001 	struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
1002 	struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
1003 	struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp;
1004 
1005 	int error;
1006 	struct tmpfs_dirent *de;
1007 	struct tmpfs_mount *tmp;
1008 	struct tmpfs_node *dnode;
1009 	struct tmpfs_node *node;
1010 
1011 	KASSERT(VOP_ISLOCKED(dvp));
1012 	KASSERT(VOP_ISLOCKED(vp));
1013 
1014 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1015 	dnode = VP_TO_TMPFS_DIR(dvp);
1016 	node = VP_TO_TMPFS_DIR(vp);
1017 	error = 0;
1018 
1019 	/* Directories with more than two entries ('.' and '..') cannot be
1020 	 * removed. */
1021 	if (node->tn_size > 0) {
1022 		error = ENOTEMPTY;
1023 		goto out;
1024 	}
1025 
1026 	/* This invariant holds only if we are not trying to remove "..".
1027 	 * We checked for that above so this is safe now. */
1028 	KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
1029 
1030 	/* Get the directory entry associated with node (vp). */
1031 	de = tmpfs_dir_lookup(dnode, cnp);
1032 	KASSERT(de);
1033 	KASSERT(de->td_node == node);
1034 
1035 	/* Check flags to see if we are allowed to remove the directory. */
1036 	if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
1037 		error = EPERM;
1038 		goto out;
1039 	}
1040 
1041 	/* Detach the directory entry from the directory (dnode). */
1042 	tmpfs_dir_detach(dvp, de);
1043 
1044 	node->tn_links--;
1045 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1046 	    TMPFS_NODE_MODIFIED;
1047 	node->tn_spec.tn_dir.tn_parent->tn_links--;
1048 	node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1049 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1050 
1051 	/* Release the parent. */
1052 	cache_purge(dvp); /* XXX Is this needed? */
1053 
1054 	/* Free the directory entry we just deleted.  Note that the node
1055 	 * referred by it will not be removed until the vnode is really
1056 	 * reclaimed. */
1057 	tmpfs_free_dirent(tmp, de, true);
1058 
1059 	KASSERT(node->tn_links == 0);
1060  out:
1061 	/* Release the nodes. */
1062 	vput(dvp);
1063 	vput(vp);
1064 	PNBUF_PUT(cnp->cn_pnbuf);
1065 
1066 	return error;
1067 }
1068 
1069 /* --------------------------------------------------------------------- */
1070 
1071 int
1072 tmpfs_symlink(void *v)
1073 {
1074 	struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
1075 	struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
1076 	struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
1077 	struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
1078 	char *target = ((struct vop_symlink_args *)v)->a_target;
1079 
1080 	KASSERT(vap->va_type == VLNK);
1081 
1082 	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1083 }
1084 
1085 /* --------------------------------------------------------------------- */
1086 
1087 int
1088 tmpfs_readdir(void *v)
1089 {
1090 	struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
1091 	struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
1092 	int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
1093 	off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
1094 	int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
1095 
1096 	int error;
1097 	off_t startoff;
1098 	off_t cnt;
1099 	struct tmpfs_node *node;
1100 
1101 	KASSERT(VOP_ISLOCKED(vp));
1102 
1103 	/* This operation only makes sense on directory nodes. */
1104 	if (vp->v_type != VDIR) {
1105 		error = ENOTDIR;
1106 		goto out;
1107 	}
1108 
1109 	node = VP_TO_TMPFS_DIR(vp);
1110 
1111 	startoff = uio->uio_offset;
1112 
1113 	cnt = 0;
1114 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1115 		error = tmpfs_dir_getdotdent(node, uio);
1116 		if (error == -1) {
1117 			error = 0;
1118 			goto outok;
1119 		} else if (error != 0)
1120 			goto outok;
1121 		cnt++;
1122 	}
1123 
1124 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1125 		error = tmpfs_dir_getdotdotdent(node, uio);
1126 		if (error == -1) {
1127 			error = 0;
1128 			goto outok;
1129 		} else if (error != 0)
1130 			goto outok;
1131 		cnt++;
1132 	}
1133 
1134 	error = tmpfs_dir_getdents(node, uio, &cnt);
1135 	if (error == -1)
1136 		error = 0;
1137 	KASSERT(error >= 0);
1138 
1139 outok:
1140 	/* This label assumes that startoff has been
1141 	 * initialized.  If the compiler didn't spit out warnings, we'd
1142 	 * simply make this one be 'out' and drop 'outok'. */
1143 
1144 	if (eofflag != NULL)
1145 		*eofflag =
1146 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1147 
1148 	/* Update NFS-related variables. */
1149 	if (error == 0 && cookies != NULL && ncookies != NULL) {
1150 		off_t i;
1151 		off_t off = startoff;
1152 		struct tmpfs_dirent *de = NULL;
1153 
1154 		*ncookies = cnt;
1155 		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1156 
1157 		for (i = 0; i < cnt; i++) {
1158 			KASSERT(off != TMPFS_DIRCOOKIE_EOF);
1159 			if (off == TMPFS_DIRCOOKIE_DOT) {
1160 				off = TMPFS_DIRCOOKIE_DOTDOT;
1161 			} else {
1162 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1163 					de = TAILQ_FIRST(&node->tn_spec.
1164 					    tn_dir.tn_dir);
1165 				} else if (de != NULL) {
1166 					de = TAILQ_NEXT(de, td_entries);
1167 				} else {
1168 					de = tmpfs_dir_lookupbycookie(node,
1169 					    off);
1170 					KASSERT(de != NULL);
1171 					de = TAILQ_NEXT(de, td_entries);
1172 				}
1173 				if (de == NULL) {
1174 					off = TMPFS_DIRCOOKIE_EOF;
1175 				} else {
1176 					off = tmpfs_dircookie(de);
1177 				}
1178 			}
1179 
1180 			(*cookies)[i] = off;
1181 		}
1182 		KASSERT(uio->uio_offset == off);
1183 	}
1184 
1185 out:
1186 	KASSERT(VOP_ISLOCKED(vp));
1187 
1188 	return error;
1189 }
1190 
1191 /* --------------------------------------------------------------------- */
1192 
1193 int
1194 tmpfs_readlink(void *v)
1195 {
1196 	struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
1197 	struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
1198 
1199 	int error;
1200 	struct tmpfs_node *node;
1201 
1202 	KASSERT(VOP_ISLOCKED(vp));
1203 	KASSERT(uio->uio_offset == 0);
1204 	KASSERT(vp->v_type == VLNK);
1205 
1206 	node = VP_TO_TMPFS_NODE(vp);
1207 
1208 	error = uiomove(node->tn_spec.tn_lnk.tn_link,
1209 	    MIN(node->tn_size, uio->uio_resid), uio);
1210 	node->tn_status |= TMPFS_NODE_ACCESSED;
1211 
1212 	KASSERT(VOP_ISLOCKED(vp));
1213 
1214 	return error;
1215 }
1216 
1217 /* --------------------------------------------------------------------- */
1218 
1219 int
1220 tmpfs_inactive(void *v)
1221 {
1222 	struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
1223 
1224 	struct tmpfs_node *node;
1225 
1226 	KASSERT(VOP_ISLOCKED(vp));
1227 
1228 	node = VP_TO_TMPFS_NODE(vp);
1229 	*((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
1230 	VOP_UNLOCK(vp, 0);
1231 
1232 	return 0;
1233 }
1234 
1235 /* --------------------------------------------------------------------- */
1236 
1237 int
1238 tmpfs_reclaim(void *v)
1239 {
1240 	struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
1241 
1242 	struct tmpfs_mount *tmp;
1243 	struct tmpfs_node *node;
1244 
1245 	node = VP_TO_TMPFS_NODE(vp);
1246 	tmp = VFS_TO_TMPFS(vp->v_mount);
1247 
1248 	cache_purge(vp);
1249 	tmpfs_free_vp(vp);
1250 
1251 	/* If the node referenced by this vnode was deleted by the user,
1252 	 * we must free its associated data structures (now that the vnode
1253 	 * is being reclaimed). */
1254 	if (node->tn_links == 0)
1255 		tmpfs_free_node(tmp, node);
1256 
1257 	KASSERT(vp->v_data == NULL);
1258 
1259 	return 0;
1260 }
1261 
1262 /* --------------------------------------------------------------------- */
1263 
1264 int
1265 tmpfs_print(void *v)
1266 {
1267 	struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
1268 
1269 	struct tmpfs_node *node;
1270 
1271 	node = VP_TO_TMPFS_NODE(vp);
1272 
1273 	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1274 	    node, node->tn_flags, node->tn_links);
1275 	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1276 	    ", status 0x%x\n",
1277 	    node->tn_mode, node->tn_uid, node->tn_gid,
1278 	    (uintmax_t)node->tn_size, node->tn_status);
1279 	if (vp->v_type == VFIFO)
1280 		fifo_printinfo(vp);
1281 	printf("\n");
1282 
1283 	return 0;
1284 }
1285 
1286 /* --------------------------------------------------------------------- */
1287 
1288 int
1289 tmpfs_pathconf(void *v)
1290 {
1291 	int name = ((struct vop_pathconf_args *)v)->a_name;
1292 	register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
1293 
1294 	int error;
1295 
1296 	error = 0;
1297 
1298 	switch (name) {
1299 	case _PC_LINK_MAX:
1300 		*retval = LINK_MAX;
1301 		break;
1302 
1303 	case _PC_NAME_MAX:
1304 		*retval = NAME_MAX;
1305 		break;
1306 
1307 	case _PC_PATH_MAX:
1308 		*retval = PATH_MAX;
1309 		break;
1310 
1311 	case _PC_PIPE_BUF:
1312 		*retval = PIPE_BUF;
1313 		break;
1314 
1315 	case _PC_CHOWN_RESTRICTED:
1316 		*retval = 1;
1317 		break;
1318 
1319 	case _PC_NO_TRUNC:
1320 		*retval = 1;
1321 		break;
1322 
1323 	case _PC_SYNC_IO:
1324 		*retval = 1;
1325 		break;
1326 
1327 	case _PC_FILESIZEBITS:
1328 		*retval = 0; /* XXX Don't know which value should I return. */
1329 		break;
1330 
1331 	default:
1332 		error = EINVAL;
1333 	}
1334 
1335 	return error;
1336 }
1337 
1338 /* --------------------------------------------------------------------- */
1339 
1340 int
1341 tmpfs_advlock(void *v)
1342 {
1343 	struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
1344 
1345 	struct tmpfs_node *node;
1346 
1347 	node = VP_TO_TMPFS_NODE(vp);
1348 
1349 	return lf_advlock(v, &node->tn_lockf, node->tn_size);
1350 }
1351 
1352 /* --------------------------------------------------------------------- */
1353 
1354 int
1355 tmpfs_getpages(void *v)
1356 {
1357 	struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
1358 	voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
1359 	struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
1360 	int *count = ((struct vop_getpages_args *)v)->a_count;
1361 	int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
1362 	vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
1363 	int advice = ((struct vop_getpages_args *)v)->a_advice;
1364 	int flags = ((struct vop_getpages_args *)v)->a_flags;
1365 
1366 	int error;
1367 	int i;
1368 	struct tmpfs_node *node;
1369 	struct uvm_object *uobj;
1370 	int npages = *count;
1371 
1372 	KASSERT(vp->v_type == VREG);
1373 	KASSERT(mutex_owned(&vp->v_interlock));
1374 
1375 	node = VP_TO_TMPFS_NODE(vp);
1376 	uobj = node->tn_spec.tn_reg.tn_aobj;
1377 
1378 	/* We currently don't rely on PGO_PASTEOF. */
1379 
1380 	if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1381 		if ((flags & PGO_LOCKED) == 0)
1382 			mutex_exit(&vp->v_interlock);
1383 		return EINVAL;
1384 	}
1385 
1386 	if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1387 		npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1388 	}
1389 
1390 	if ((flags & PGO_LOCKED) != 0)
1391 		return EBUSY;
1392 
1393 	if ((flags & PGO_NOTIMESTAMP) == 0) {
1394 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1395 			node->tn_status |= TMPFS_NODE_ACCESSED;
1396 
1397 		if ((access_type & VM_PROT_WRITE) != 0)
1398 			node->tn_status |= TMPFS_NODE_MODIFIED;
1399 	}
1400 
1401 	mutex_exit(&vp->v_interlock);
1402 
1403 	/*
1404 	 * Make sure that the array on which we will store the
1405 	 * gotten pages is clean.  Otherwise uao_get (pointed to by
1406 	 * the pgo_get below) gets confused and does not return the
1407 	 * appropriate pages.
1408 	 *
1409 	 * XXX This shall be revisited when kern/32166 is addressed
1410 	 * because the loop to clean m[i] will most likely be redundant
1411 	 * as well as the PGO_ALLPAGES flag.
1412 	 */
1413 	if (m != NULL)
1414 		for (i = 0; i < npages; i++)
1415 			m[i] = NULL;
1416 	mutex_enter(&uobj->vmobjlock);
1417 	error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
1418 	    access_type, advice, flags | PGO_ALLPAGES);
1419 #if defined(DEBUG)
1420 	{
1421 		/* Make sure that all the pages we return are valid. */
1422 		int dbgi;
1423 		if (error == 0 && m != NULL)
1424 			for (dbgi = 0; dbgi < npages; dbgi++)
1425 				KASSERT(m[dbgi] != NULL);
1426 	}
1427 #endif
1428 
1429 	return error;
1430 }
1431 
1432 /* --------------------------------------------------------------------- */
1433 
1434 int
1435 tmpfs_putpages(void *v)
1436 {
1437 	struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
1438 	voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
1439 	voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
1440 	int flags = ((struct vop_putpages_args *)v)->a_flags;
1441 
1442 	int error;
1443 	struct tmpfs_node *node;
1444 	struct uvm_object *uobj;
1445 
1446 	KASSERT(mutex_owned(&vp->v_interlock));
1447 
1448 	node = VP_TO_TMPFS_NODE(vp);
1449 
1450 	if (vp->v_type != VREG) {
1451 		mutex_exit(&vp->v_interlock);
1452 		return 0;
1453 	}
1454 
1455 	uobj = node->tn_spec.tn_reg.tn_aobj;
1456 	mutex_exit(&vp->v_interlock);
1457 
1458 	mutex_enter(&uobj->vmobjlock);
1459 	error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1460 
1461 	/* XXX mtime */
1462 
1463 	return error;
1464 }
1465