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