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