xref: /dragonfly/sys/vfs/tmpfs/tmpfs_vnops.c (revision 1f8a7fec)
1 /*	$NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9  * 2005 program.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * tmpfs vnode interface.
35  */
36 #include <sys/cdefs.h>
37 
38 #include <sys/kernel.h>
39 #include <sys/kern_syscall.h>
40 #include <sys/param.h>
41 #include <sys/fcntl.h>
42 #include <sys/lockf.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/stat.h>
48 #include <sys/systm.h>
49 #include <sys/unistd.h>
50 #include <sys/vfsops.h>
51 #include <sys/vnode.h>
52 
53 #include <sys/mplock2.h>
54 
55 #include <vm/vm.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_pager.h>
59 #include <vm/swap_pager.h>
60 
61 #include <vfs/fifofs/fifo.h>
62 #include <vfs/tmpfs/tmpfs_vnops.h>
63 #include <vfs/tmpfs/tmpfs.h>
64 
65 MALLOC_DECLARE(M_TMPFS);
66 
67 /* --------------------------------------------------------------------- */
68 
69 static int
70 tmpfs_nresolve(struct vop_nresolve_args *v)
71 {
72 	struct vnode *dvp = v->a_dvp;
73 	struct vnode *vp = NULL;
74 	struct namecache *ncp = v->a_nch->ncp;
75 	struct tmpfs_node *tnode;
76 
77 	int error;
78 	struct tmpfs_dirent *de;
79 	struct tmpfs_node *dnode;
80 
81 	dnode = VP_TO_TMPFS_DIR(dvp);
82 
83 	de = tmpfs_dir_lookup(dnode, NULL, ncp);
84 	if (de == NULL) {
85 		error = ENOENT;
86 	} else {
87 		/*
88 		 * Allocate a vnode for the node we found.
89 		 */
90 		tnode = de->td_node;
91 		error = tmpfs_alloc_vp(dvp->v_mount, tnode,
92 				       LK_EXCLUSIVE | LK_RETRY, &vp);
93 		if (error)
94 			goto out;
95 		KKASSERT(vp);
96 	}
97 
98 out:
99 	/*
100 	 * Store the result of this lookup in the cache.  Avoid this if the
101 	 * request was for creation, as it does not improve timings on
102 	 * emprical tests.
103 	 */
104 	if (vp) {
105 		vn_unlock(vp);
106 		cache_setvp(v->a_nch, vp);
107 		vrele(vp);
108 	} else if (error == ENOENT) {
109 		cache_setvp(v->a_nch, NULL);
110 	}
111 	return error;
112 }
113 
114 static int
115 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
116 {
117 	struct vnode *dvp = v->a_dvp;
118 	struct vnode **vpp = v->a_vpp;
119 	struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
120 	struct ucred *cred = v->a_cred;
121 	int error;
122 
123 	*vpp = NULL;
124 	/* Check accessibility of requested node as a first step. */
125 	error = VOP_ACCESS(dvp, VEXEC, cred);
126 	if (error != 0)
127 		return error;
128 
129 	if (dnode->tn_dir.tn_parent != NULL) {
130 		/* Allocate a new vnode on the matching entry. */
131 		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
132 		    LK_EXCLUSIVE | LK_RETRY, vpp);
133 
134 		if (*vpp)
135 			vn_unlock(*vpp);
136 	}
137 
138 	return (*vpp == NULL) ? ENOENT : 0;
139 }
140 
141 /* --------------------------------------------------------------------- */
142 
143 static int
144 tmpfs_ncreate(struct vop_ncreate_args *v)
145 {
146 	struct vnode *dvp = v->a_dvp;
147 	struct vnode **vpp = v->a_vpp;
148 	struct namecache *ncp = v->a_nch->ncp;
149 	struct vattr *vap = v->a_vap;
150 	struct ucred *cred = v->a_cred;
151 	int error;
152 
153 	KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
154 
155 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
156 	if (error == 0) {
157 		cache_setunresolved(v->a_nch);
158 		cache_setvp(v->a_nch, *vpp);
159 	}
160 
161 	return error;
162 }
163 /* --------------------------------------------------------------------- */
164 
165 static int
166 tmpfs_nmknod(struct vop_nmknod_args *v)
167 {
168 	struct vnode *dvp = v->a_dvp;
169 	struct vnode **vpp = v->a_vpp;
170 	struct namecache *ncp = v->a_nch->ncp;
171 	struct vattr *vap = v->a_vap;
172 	struct ucred *cred = v->a_cred;
173 	int error;
174 
175 	if (vap->va_type != VBLK && vap->va_type != VCHR &&
176 	    vap->va_type != VFIFO)
177 		return EINVAL;
178 
179 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
180 	if (error == 0) {
181 		cache_setunresolved(v->a_nch);
182 		cache_setvp(v->a_nch, *vpp);
183 	}
184 
185 	return error;
186 }
187 
188 /* --------------------------------------------------------------------- */
189 
190 static int
191 tmpfs_open(struct vop_open_args *v)
192 {
193 	struct vnode *vp = v->a_vp;
194 	int mode = v->a_mode;
195 
196 	int error;
197 	struct tmpfs_node *node;
198 
199 	node = VP_TO_TMPFS_NODE(vp);
200 
201 	/* The file is still active but all its names have been removed
202 	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
203 	 * it is about to die. */
204 	if (node->tn_links < 1)
205 		return (ENOENT);
206 
207 	/* If the file is marked append-only, deny write requests. */
208 	if ((node->tn_flags & APPEND) &&
209 	    (mode & (FWRITE | O_APPEND)) == FWRITE) {
210 		error = EPERM;
211 	} else {
212 		return (vop_stdopen(v));
213 	}
214 	return error;
215 }
216 
217 /* --------------------------------------------------------------------- */
218 
219 static int
220 tmpfs_close(struct vop_close_args *v)
221 {
222 	struct vnode *vp = v->a_vp;
223 	struct tmpfs_node *node;
224 
225 	node = VP_TO_TMPFS_NODE(vp);
226 
227 	if (node->tn_links > 0) {
228 		/* Update node times.  No need to do it if the node has
229 		 * been deleted, because it will vanish after we return. */
230 		tmpfs_update(vp);
231 	}
232 
233 	return vop_stdclose(v);
234 }
235 
236 /* --------------------------------------------------------------------- */
237 
238 int
239 tmpfs_access(struct vop_access_args *v)
240 {
241 	struct vnode *vp = v->a_vp;
242 	int error;
243 	struct tmpfs_node *node;
244 
245 	node = VP_TO_TMPFS_NODE(vp);
246 
247 	switch (vp->v_type) {
248 	case VDIR:
249 		/* FALLTHROUGH */
250 	case VLNK:
251 		/* FALLTHROUGH */
252 	case VREG:
253 		if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
254 			error = EROFS;
255 			goto out;
256 		}
257 		break;
258 
259 	case VBLK:
260 		/* FALLTHROUGH */
261 	case VCHR:
262 		/* FALLTHROUGH */
263 	case VSOCK:
264 		/* FALLTHROUGH */
265 	case VFIFO:
266 		break;
267 
268 	default:
269 		error = EINVAL;
270 		goto out;
271 	}
272 
273 	if (VWRITE && node->tn_flags & IMMUTABLE) {
274 		error = EPERM;
275 		goto out;
276 	}
277 
278 	error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
279 
280 out:
281 
282 	return error;
283 }
284 
285 /* --------------------------------------------------------------------- */
286 
287 int
288 tmpfs_getattr(struct vop_getattr_args *v)
289 {
290 	struct vnode *vp = v->a_vp;
291 	struct vattr *vap = v->a_vap;
292 	struct tmpfs_node *node;
293 
294 	node = VP_TO_TMPFS_NODE(vp);
295 
296 	tmpfs_update(vp);
297 
298 	vap->va_type = vp->v_type;
299 	vap->va_mode = node->tn_mode;
300 	vap->va_nlink = node->tn_links;
301 	vap->va_uid = node->tn_uid;
302 	vap->va_gid = node->tn_gid;
303 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
304 	vap->va_fileid = node->tn_id;
305 	vap->va_size = node->tn_size;
306 	vap->va_blocksize = PAGE_SIZE;
307 	vap->va_atime.tv_sec = node->tn_atime;
308 	vap->va_atime.tv_nsec = node->tn_atimensec;
309 	vap->va_mtime.tv_sec = node->tn_mtime;
310 	vap->va_mtime.tv_nsec = node->tn_mtimensec;
311 	vap->va_ctime.tv_sec = node->tn_ctime;
312 	vap->va_ctime.tv_nsec = node->tn_ctimensec;
313 	vap->va_gen = node->tn_gen;
314 	vap->va_flags = node->tn_flags;
315 	if (vp->v_type == VBLK || vp->v_type == VCHR)
316 	{
317 		vap->va_rmajor = umajor(node->tn_rdev);
318 		vap->va_rminor = uminor(node->tn_rdev);
319 	}
320 	vap->va_bytes = round_page(node->tn_size);
321 	vap->va_filerev = 0;
322 
323 	return 0;
324 }
325 
326 /* --------------------------------------------------------------------- */
327 
328 int
329 tmpfs_setattr(struct vop_setattr_args *v)
330 {
331 	struct vnode *vp = v->a_vp;
332 	struct vattr *vap = v->a_vap;
333 	struct ucred *cred = v->a_cred;
334 	int error = 0;
335 
336 	if (error == 0 && (vap->va_flags != VNOVAL))
337 		error = tmpfs_chflags(vp, vap->va_flags, cred);
338 
339 	if (error == 0 && (vap->va_size != VNOVAL))
340 		error = tmpfs_chsize(vp, vap->va_size, cred);
341 
342 	if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
343 			   vap->va_gid != (gid_t)VNOVAL)) {
344 		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
345 	}
346 
347 	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
348 		error = tmpfs_chmod(vp, vap->va_mode, cred);
349 
350 	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
351 	    vap->va_atime.tv_nsec != VNOVAL) ||
352 	    (vap->va_mtime.tv_sec != VNOVAL &&
353 	    vap->va_mtime.tv_nsec != VNOVAL) )) {
354 		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
355 				      vap->va_vaflags, cred);
356 	}
357 
358 	/* Update the node times.  We give preference to the error codes
359 	 * generated by this function rather than the ones that may arise
360 	 * from tmpfs_update. */
361 	tmpfs_update(vp);
362 
363 	return error;
364 }
365 
366 /* --------------------------------------------------------------------- */
367 
368 /*
369  * fsync is usually a NOP, but we must take action when unmounting or
370  * when recycling.
371  */
372 static int
373 tmpfs_fsync(struct vop_fsync_args *v)
374 {
375 	struct tmpfs_mount *tmp;
376 	struct tmpfs_node *node;
377 	struct vnode *vp = v->a_vp;
378 
379 	tmp = VFS_TO_TMPFS(vp->v_mount);
380 	node = VP_TO_TMPFS_NODE(vp);
381 
382 	tmpfs_update(vp);
383 	if (vp->v_type == VREG) {
384 		if (vp->v_flag & VRECLAIMED) {
385 			if (node->tn_links == 0)
386 				tmpfs_truncate(vp, 0);
387 			else
388 				vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
389 		}
390 	}
391 	return 0;
392 }
393 
394 /* --------------------------------------------------------------------- */
395 
396 static int
397 tmpfs_read (struct vop_read_args *ap)
398 {
399 	struct buf *bp;
400 	struct vnode *vp = ap->a_vp;
401 	struct uio *uio = ap->a_uio;
402 	struct tmpfs_node *node;
403 	off_t base_offset;
404 	size_t offset;
405 	size_t len;
406 	int got_mplock;
407 	int error;
408 
409 	error = 0;
410 	if (uio->uio_resid == 0) {
411 		return error;
412 	}
413 
414 	node = VP_TO_TMPFS_NODE(vp);
415 
416 	if (uio->uio_offset < 0)
417 		return (EINVAL);
418 	if (vp->v_type != VREG)
419 		return (EINVAL);
420 
421 #ifdef SMP
422 	if(curthread->td_mpcount)
423 		got_mplock = -1;
424 	else
425 		got_mplock = 0;
426 #else
427 		got_mplock = -1;
428 #endif
429 
430 	while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
431 		/*
432 		 * Use buffer cache I/O (via tmpfs_strategy)
433 		 */
434 		offset = (size_t)uio->uio_offset & BMASK;
435 		base_offset = (off_t)uio->uio_offset - offset;
436 		bp = getcacheblk(vp, base_offset);
437 		if (bp == NULL)
438 		{
439 			if (got_mplock == 0) {
440 				got_mplock = 1;
441 				get_mplock();
442 			}
443 
444 			error = bread(vp, base_offset, BSIZE, &bp);
445 			if (error) {
446 				brelse(bp);
447 				kprintf("tmpfs_read bread error %d\n", error);
448 				break;
449 			}
450 		}
451 
452 		if (got_mplock == 0) {
453 			got_mplock = 1;
454 			get_mplock();
455 		}
456 
457 		/*
458 		 * Figure out how many bytes we can actually copy this loop.
459 		 */
460 		len = BSIZE - offset;
461 		if (len > uio->uio_resid)
462 			len = uio->uio_resid;
463 		if (len > node->tn_size - uio->uio_offset)
464 			len = (size_t)(node->tn_size - uio->uio_offset);
465 
466 		error = uiomove((char *)bp->b_data + offset, len, uio);
467 		bqrelse(bp);
468 		if (error) {
469 			kprintf("tmpfs_read uiomove error %d\n", error);
470 			break;
471 		}
472 	}
473 
474 	if (got_mplock > 0)
475 		rel_mplock();
476 
477 	TMPFS_NODE_LOCK(node);
478 	node->tn_status |= TMPFS_NODE_ACCESSED;
479 	TMPFS_NODE_UNLOCK(node);
480 
481 	return(error);
482 }
483 
484 static int
485 tmpfs_write (struct vop_write_args *ap)
486 {
487 	struct buf *bp;
488 	struct vnode *vp = ap->a_vp;
489 	struct uio *uio = ap->a_uio;
490 	struct thread *td = uio->uio_td;
491 	struct tmpfs_node *node;
492 	boolean_t extended;
493 	off_t oldsize;
494 	int error;
495 	off_t base_offset;
496 	size_t offset;
497 	size_t len;
498 	struct rlimit limit;
499 	int got_mplock;
500 	int trivial = 0;
501 
502 	error = 0;
503 	if (uio->uio_resid == 0) {
504 		return error;
505 	}
506 
507 	node = VP_TO_TMPFS_NODE(vp);
508 
509 	if (vp->v_type != VREG)
510 		return (EINVAL);
511 
512 	oldsize = node->tn_size;
513 	if (ap->a_ioflag & IO_APPEND)
514 		uio->uio_offset = node->tn_size;
515 
516 	/*
517 	 * Check for illegal write offsets.
518 	 */
519 	if (uio->uio_offset + uio->uio_resid >
520 	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
521 		return (EFBIG);
522 
523 	if (vp->v_type == VREG && td != NULL) {
524 		error = kern_getrlimit(RLIMIT_FSIZE, &limit);
525 		if (error != 0)
526 			return error;
527 		if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
528 			ksignal(td->td_proc, SIGXFSZ);
529 			return (EFBIG);
530 		}
531 	}
532 
533 
534 	/*
535 	 * Extend the file's size if necessary
536 	 */
537 	extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
538 
539 #ifdef SMP
540 	if (curthread->td_mpcount) {
541 		got_mplock = -1;
542 	} else {
543 		got_mplock = 1;
544 		get_mplock();
545 	}
546 #else
547 	got_mplock = -1;
548 #endif
549 	while (uio->uio_resid > 0) {
550 		/*
551 		 * Use buffer cache I/O (via tmpfs_strategy)
552 		 */
553 		offset = (size_t)uio->uio_offset & BMASK;
554 		base_offset = (off_t)uio->uio_offset - offset;
555 		len = BSIZE - offset;
556 		if (len > uio->uio_resid)
557 			len = uio->uio_resid;
558 
559 		if ((uio->uio_offset + len) > node->tn_size) {
560 			trivial = (uio->uio_offset <= node->tn_size);
561 			error = tmpfs_reg_resize(vp, uio->uio_offset + len,  trivial);
562 			if (error)
563 				break;
564 		}
565 
566 		/*
567 		 * Read to fill in any gaps.  Theoretically we could
568 		 * optimize this if the write covers the entire buffer
569 		 * and is not a UIO_NOCOPY write, however this can lead
570 		 * to a security violation exposing random kernel memory
571 		 * (whatever junk was in the backing VM pages before).
572 		 *
573 		 * So just use bread() to do the right thing.
574 		 */
575 		error = bread(vp, base_offset, BSIZE, &bp);
576 		error = uiomove((char *)bp->b_data + offset, len, uio);
577 		if (error) {
578 			kprintf("tmpfs_write uiomove error %d\n", error);
579 			brelse(bp);
580 			break;
581 		}
582 
583 		if (uio->uio_offset > node->tn_size)
584 			node->tn_size = uio->uio_offset;
585 
586 		/*
587 		 * The data has been loaded into the buffer, write it out.
588 		 *
589 		 * We want tmpfs to be able to use all available ram, not
590 		 * just the buffer cache, so if not explicitly paging we
591 		 * use buwrite() to leave the buffer clean but mark all the
592 		 * VM pages valid+dirty.
593 		 *
594 		 * When the kernel is paging, either via normal pageout
595 		 * operation or when cleaning the object during a recycle,
596 		 * the underlying VM pages are going to get thrown away
597 		 * so we MUST write them to swap.
598 		 *
599 		 * XXX unfortunately this catches msync() system calls too
600 		 * for the moment.
601 		 */
602 		if (vm_swap_size == 0) {
603 			/*
604 			 * if swap isn't configured yet, force a buwrite() to
605 			 * avoid problems further down the line, due to flushing
606 			 * to swap.
607 			 */
608 			buwrite(bp);
609 		} else {
610 			if (ap->a_ioflag & IO_SYNC) {
611 				bwrite(bp);
612 			} else if ((ap->a_ioflag & IO_ASYNC) ||
613 				 (uio->uio_segflg == UIO_NOCOPY)) {
614 				bawrite(bp);
615 			} else {
616 				buwrite(bp);
617 			}
618 		}
619 
620 		if (bp->b_error) {
621 			kprintf("tmpfs_write bwrite error %d\n", bp->b_error);
622 			break;
623 		}
624 	}
625 
626 	if (got_mplock > 0)
627 		rel_mplock();
628 
629 	if (error) {
630 		if (extended)
631 			(void)tmpfs_reg_resize(vp, oldsize, trivial);
632 		return error;
633 	}
634 
635 	TMPFS_NODE_LOCK(node);
636 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
637 	    (extended? TMPFS_NODE_CHANGED : 0);
638 
639 	if (node->tn_mode & (S_ISUID | S_ISGID)) {
640 		if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
641 			node->tn_mode &= ~(S_ISUID | S_ISGID);
642 	}
643 	TMPFS_NODE_UNLOCK(node);
644 
645 	return(error);
646 }
647 
648 static int
649 tmpfs_advlock (struct vop_advlock_args *ap)
650 {
651 	struct tmpfs_node *node;
652 	struct vnode *vp = ap->a_vp;
653 
654 	node = VP_TO_TMPFS_NODE(vp);
655 
656 	return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
657 }
658 
659 
660 static int
661 tmpfs_strategy(struct vop_strategy_args *ap)
662 {
663 	struct bio *bio = ap->a_bio;
664 	struct buf *bp = bio->bio_buf;
665 	struct vnode *vp = ap->a_vp;
666 	struct tmpfs_node *node;
667 	vm_object_t uobj;
668 
669 	if (vp->v_type != VREG) {
670 		bp->b_resid = bp->b_bcount;
671 		bp->b_flags |= B_ERROR | B_INVAL;
672 		bp->b_error = EINVAL;
673 		biodone(bio);
674 		return(0);
675 	}
676 
677 	node = VP_TO_TMPFS_NODE(vp);
678 
679 	uobj = node->tn_reg.tn_aobj;
680 
681 	/*
682 	 * Call swap_pager_strategy to read or write between the VM
683 	 * object and the buffer cache.
684 	 */
685 	swap_pager_strategy(uobj, bio);
686 
687 	return 0;
688 }
689 
690 static int
691 tmpfs_bmap(struct vop_bmap_args *ap)
692 {
693 	if (ap->a_doffsetp != NULL)
694 		*ap->a_doffsetp = ap->a_loffset;
695 	if (ap->a_runp != NULL)
696 		*ap->a_runp = 0;
697 	if (ap->a_runb != NULL)
698 		*ap->a_runb = 0;
699 
700 	return 0;
701 }
702 
703 /* --------------------------------------------------------------------- */
704 
705 static int
706 tmpfs_nremove(struct vop_nremove_args *v)
707 {
708 	struct vnode *dvp = v->a_dvp;
709 	struct namecache *ncp = v->a_nch->ncp;
710 	struct vnode *vp;
711 	int error;
712 	struct tmpfs_dirent *de;
713 	struct tmpfs_mount *tmp;
714 	struct tmpfs_node *dnode;
715 	struct tmpfs_node *node;
716 
717 	/*
718 	 * We have to acquire the vp from v->a_nch because
719 	 * we will likely unresolve the namecache entry, and
720 	 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
721 	 * sequence to recover space from the file.
722 	 */
723 	error = cache_vref(v->a_nch, v->a_cred, &vp);
724 	KKASSERT(error == 0);
725 
726 	if (vp->v_type == VDIR) {
727 		error = EISDIR;
728 		goto out;
729 	}
730 
731 	dnode = VP_TO_TMPFS_DIR(dvp);
732 	node = VP_TO_TMPFS_NODE(vp);
733 	tmp = VFS_TO_TMPFS(vp->v_mount);
734 	de = tmpfs_dir_lookup(dnode, node, ncp);
735 	if (de == NULL) {
736 		error = ENOENT;
737 		goto out;
738 	}
739 
740 	/* Files marked as immutable or append-only cannot be deleted. */
741 	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
742 	    (dnode->tn_flags & APPEND)) {
743 		error = EPERM;
744 		goto out;
745 	}
746 
747 	/* Remove the entry from the directory; as it is a file, we do not
748 	 * have to change the number of hard links of the directory. */
749 	tmpfs_dir_detach(dnode, de);
750 
751 	/* Free the directory entry we just deleted.  Note that the node
752 	 * referred by it will not be removed until the vnode is really
753 	 * reclaimed. */
754 	tmpfs_free_dirent(tmp, de);
755 
756 	if (node->tn_links > 0) {
757 	        TMPFS_NODE_LOCK(node);
758 		node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
759 	                TMPFS_NODE_MODIFIED;
760 	        TMPFS_NODE_UNLOCK(node);
761 	}
762 
763 	cache_setunresolved(v->a_nch);
764 	cache_setvp(v->a_nch, NULL);
765 	/*cache_inval_vp(vp, CINV_DESTROY);*/
766 	error = 0;
767 
768 out:
769 	vrele(vp);
770 
771 	return error;
772 }
773 
774 /* --------------------------------------------------------------------- */
775 
776 static int
777 tmpfs_nlink(struct vop_nlink_args *v)
778 {
779 	struct vnode *dvp = v->a_dvp;
780 	struct vnode *vp = v->a_vp;
781 	struct namecache *ncp = v->a_nch->ncp;
782 	struct tmpfs_dirent *de;
783 	struct tmpfs_node *node;
784 	struct tmpfs_node *dnode;
785 	int error;
786 
787 	KKASSERT(dvp != vp); /* XXX When can this be false? */
788 
789 	node = VP_TO_TMPFS_NODE(vp);
790 	dnode = VP_TO_TMPFS_NODE(dvp);
791 
792 	/* XXX: Why aren't the following two tests done by the caller? */
793 
794 	/* Hard links of directories are forbidden. */
795 	if (vp->v_type == VDIR) {
796 		error = EPERM;
797 		goto out;
798 	}
799 
800 	/* Cannot create cross-device links. */
801 	if (dvp->v_mount != vp->v_mount) {
802 		error = EXDEV;
803 		goto out;
804 	}
805 
806 	/* Ensure that we do not overflow the maximum number of links imposed
807 	 * by the system. */
808 	KKASSERT(node->tn_links <= LINK_MAX);
809 	if (node->tn_links == LINK_MAX) {
810 		error = EMLINK;
811 		goto out;
812 	}
813 
814 	/* We cannot create links of files marked immutable or append-only. */
815 	if (node->tn_flags & (IMMUTABLE | APPEND)) {
816 		error = EPERM;
817 		goto out;
818 	}
819 
820 	/* Allocate a new directory entry to represent the node. */
821 	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
822 	    ncp->nc_name, ncp->nc_nlen, &de);
823 	if (error != 0)
824 		goto out;
825 
826 	/* Insert the new directory entry into the appropriate directory. */
827 	tmpfs_dir_attach(dnode, de);
828 
829 	/* vp link count has changed, so update node times. */
830 
831 	TMPFS_NODE_LOCK(node);
832 	node->tn_status |= TMPFS_NODE_CHANGED;
833 	TMPFS_NODE_UNLOCK(node);
834 	tmpfs_update(vp);
835 
836 	cache_setunresolved(v->a_nch);
837 	cache_setvp(v->a_nch, vp);
838 	error = 0;
839 
840 out:
841 	return error;
842 }
843 
844 /* --------------------------------------------------------------------- */
845 
846 static int
847 tmpfs_nrename(struct vop_nrename_args *v)
848 {
849 	struct vnode *fdvp = v->a_fdvp;
850 	struct namecache *fncp = v->a_fnch->ncp;
851 	struct vnode *fvp = fncp->nc_vp;
852 	struct vnode *tdvp = v->a_tdvp;
853 	struct namecache *tncp = v->a_tnch->ncp;
854 	struct vnode *tvp = tncp->nc_vp;
855 	struct tmpfs_dirent *de;
856 	struct tmpfs_mount *tmp;
857 	struct tmpfs_node *fdnode;
858 	struct tmpfs_node *fnode;
859 	struct tmpfs_node *tnode;
860 	struct tmpfs_node *tdnode;
861 	char *newname;
862 	char *oldname;
863 	int error;
864 
865 	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
866 
867 	/* Disallow cross-device renames.
868 	 * XXX Why isn't this done by the caller? */
869 	if (fvp->v_mount != tdvp->v_mount ||
870 	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
871 		error = EXDEV;
872 		goto out;
873 	}
874 
875 	tmp = VFS_TO_TMPFS(tdvp->v_mount);
876 	tdnode = VP_TO_TMPFS_DIR(tdvp);
877 
878 	/* If source and target are the same file, there is nothing to do. */
879 	if (fvp == tvp) {
880 		error = 0;
881 		goto out;
882 	}
883 
884 	fdnode = VP_TO_TMPFS_DIR(fdvp);
885 	fnode = VP_TO_TMPFS_NODE(fvp);
886 	de = tmpfs_dir_lookup(fdnode, fnode, fncp);
887 
888 	/* Avoid manipulating '.' and '..' entries. */
889 	if (de == NULL) {
890 		error = ENOENT;
891 		goto out_locked;
892 	}
893 	KKASSERT(de->td_node == fnode);
894 
895 	/*
896 	 * If replacing an entry in the target directory and that entry
897 	 * is a directory, it must be empty.
898 	 *
899 	 * Kern_rename gurantees the destination to be a directory
900 	 * if the source is one (it does?).
901 	 */
902 	if (tvp != NULL) {
903 		KKASSERT(tnode != NULL);
904 
905 		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
906 		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
907 			error = EPERM;
908 			goto out_locked;
909 		}
910 
911 		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
912 			if (tnode->tn_size > 0) {
913 				error = ENOTEMPTY;
914 				goto out_locked;
915 			}
916 		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
917 			error = ENOTDIR;
918 			goto out_locked;
919 		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
920 			error = EISDIR;
921 			goto out_locked;
922 		} else {
923 			KKASSERT(fnode->tn_type != VDIR &&
924 				tnode->tn_type != VDIR);
925 		}
926 	}
927 
928 	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
929 	    (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
930 		error = EPERM;
931 		goto out_locked;
932 	}
933 
934 	/*
935 	 * Ensure that we have enough memory to hold the new name, if it
936 	 * has to be changed.
937 	 */
938 	if (fncp->nc_nlen != tncp->nc_nlen ||
939 	    bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
940 		newname = kmalloc(tncp->nc_nlen + 1, tmp->tm_name_zone,
941 				  M_WAITOK | M_NULLOK);
942 		if (newname == NULL) {
943 			error = ENOSPC;
944 			goto out_locked;
945 		}
946 		bcopy(tncp->nc_name, newname, tncp->nc_nlen);
947 		newname[tncp->nc_nlen] = '\0';
948 	} else {
949 		newname = NULL;
950 	}
951 
952 	/*
953 	 * Unlink entry from source directory.  Note that the kernel has
954 	 * already checked for illegal recursion cases (renaming a directory
955 	 * into a subdirectory of itself).
956 	 */
957 	if (fdnode != tdnode)
958 		tmpfs_dir_detach(fdnode, de);
959 
960 	/*
961 	 * Handle any name change.  Swap with newname, we will
962 	 * deallocate it at the end.
963 	 */
964 	if (newname != NULL) {
965 #if 0
966 		TMPFS_NODE_LOCK(fnode);
967 		fnode->tn_status |= TMPFS_NODE_CHANGED;
968 		TMPFS_NODE_UNLOCK(fnode);
969 #endif
970 		oldname = de->td_name;
971 		de->td_name = newname;
972 		de->td_namelen = (uint16_t)tncp->nc_nlen;
973 		newname = oldname;
974 	}
975 
976 	/*
977 	 * Link entry to target directory.  If the entry
978 	 * represents a directory move the parent linkage
979 	 * as well.
980 	 */
981 	if (fdnode != tdnode) {
982 		if (de->td_node->tn_type == VDIR) {
983 			TMPFS_VALIDATE_DIR(fnode);
984 
985 			TMPFS_NODE_LOCK(tdnode);
986 			tdnode->tn_links++;
987 			tdnode->tn_status |= TMPFS_NODE_MODIFIED;
988 			TMPFS_NODE_UNLOCK(tdnode);
989 
990 			TMPFS_NODE_LOCK(fnode);
991 			fnode->tn_dir.tn_parent = tdnode;
992 			fnode->tn_status |= TMPFS_NODE_CHANGED;
993 			TMPFS_NODE_UNLOCK(fnode);
994 
995 			TMPFS_NODE_LOCK(fdnode);
996 			fdnode->tn_links--;
997 			fdnode->tn_status |= TMPFS_NODE_MODIFIED;
998 			TMPFS_NODE_UNLOCK(fdnode);
999 		}
1000 		tmpfs_dir_attach(tdnode, de);
1001 	} else {
1002 		TMPFS_NODE_LOCK(tdnode);
1003 		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1004 		TMPFS_NODE_UNLOCK(tdnode);
1005 	}
1006 
1007 	/*
1008 	 * If we are overwriting an entry, we have to remove the old one
1009 	 * from the target directory.
1010 	 */
1011 	if (tvp != NULL) {
1012 		/* Remove the old entry from the target directory. */
1013 		de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1014 		tmpfs_dir_detach(tdnode, de);
1015 
1016 		/*
1017 		 * Free the directory entry we just deleted.  Note that the
1018 		 * node referred by it will not be removed until the vnode is
1019 		 * really reclaimed.
1020 		 */
1021 		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
1022 		/*cache_inval_vp(tvp, CINV_DESTROY);*/
1023 	}
1024 
1025 	/*
1026 	 * Finish up
1027 	 */
1028 	if (newname) {
1029 		kfree(newname, tmp->tm_name_zone);
1030 		newname = NULL;
1031 	}
1032 	cache_rename(v->a_fnch, v->a_tnch);
1033 	error = 0;
1034 
1035 out_locked:
1036 	;
1037 
1038 out:
1039 	/* Release target nodes. */
1040 	/* XXX: I don't understand when tdvp can be the same as tvp, but
1041 	 * other code takes care of this... */
1042 	if (tdvp == tvp)
1043 		vrele(tdvp);
1044 
1045 	return error;
1046 }
1047 
1048 /* --------------------------------------------------------------------- */
1049 
1050 static int
1051 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1052 {
1053 	struct vnode *dvp = v->a_dvp;
1054 	struct vnode **vpp = v->a_vpp;
1055 	struct namecache *ncp = v->a_nch->ncp;
1056 	struct vattr *vap = v->a_vap;
1057 	struct ucred *cred = v->a_cred;
1058 	int error;
1059 
1060 	KKASSERT(vap->va_type == VDIR);
1061 
1062 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1063 	if (error == 0) {
1064 		cache_setunresolved(v->a_nch);
1065 		cache_setvp(v->a_nch, *vpp);
1066 	}
1067 
1068 	return error;
1069 }
1070 
1071 /* --------------------------------------------------------------------- */
1072 
1073 static int
1074 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1075 {
1076 	struct vnode *dvp = v->a_dvp;
1077 	struct namecache *ncp = v->a_nch->ncp;
1078 	struct vnode *vp;
1079 	struct tmpfs_dirent *de;
1080 	struct tmpfs_mount *tmp;
1081 	struct tmpfs_node *dnode;
1082 	struct tmpfs_node *node;
1083 	int error;
1084 
1085 	/*
1086 	 * We have to acquire the vp from v->a_nch because
1087 	 * we will likely unresolve the namecache entry, and
1088 	 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1089 	 * sequence.
1090 	 */
1091 	error = cache_vref(v->a_nch, v->a_cred, &vp);
1092 	KKASSERT(error == 0);
1093 
1094 	/*
1095 	 * Prevalidate so we don't hit an assertion later
1096 	 */
1097 	if (vp->v_type != VDIR) {
1098 		error = ENOTDIR;
1099 		goto out;
1100 	}
1101 
1102 	tmp = VFS_TO_TMPFS(dvp->v_mount);
1103 	dnode = VP_TO_TMPFS_DIR(dvp);
1104 	node = VP_TO_TMPFS_DIR(vp);
1105 
1106 	/* Directories with more than two entries ('.' and '..') cannot be
1107 	 * removed. */
1108 	 if (node->tn_size > 0) {
1109 		 error = ENOTEMPTY;
1110 		 goto out;
1111 	 }
1112 
1113 	if ((dnode->tn_flags & APPEND)
1114 	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1115 		error = EPERM;
1116 		goto out;
1117 	}
1118 
1119 	/* This invariant holds only if we are not trying to remove "..".
1120 	  * We checked for that above so this is safe now. */
1121 	KKASSERT(node->tn_dir.tn_parent == dnode);
1122 
1123 	/* Get the directory entry associated with node (vp).  This was
1124 	 * filled by tmpfs_lookup while looking up the entry. */
1125 	de = tmpfs_dir_lookup(dnode, node, ncp);
1126 	KKASSERT(TMPFS_DIRENT_MATCHES(de,
1127 	    ncp->nc_name,
1128 	    ncp->nc_nlen));
1129 
1130 	/* Check flags to see if we are allowed to remove the directory. */
1131 	if ((dnode->tn_flags & APPEND) ||
1132 	    node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1133 		error = EPERM;
1134 		goto out;
1135 	}
1136 
1137 
1138 	/* Detach the directory entry from the directory (dnode). */
1139 	tmpfs_dir_detach(dnode, de);
1140 
1141 	/* No vnode should be allocated for this entry from this point */
1142 	TMPFS_NODE_LOCK(node);
1143 	TMPFS_ASSERT_ELOCKED(node);
1144 	TMPFS_NODE_LOCK(dnode);
1145 	TMPFS_ASSERT_ELOCKED(dnode);
1146 
1147 #if 0
1148 	/* handled by tmpfs_free_node */
1149 	KKASSERT(node->tn_links > 0);
1150 	node->tn_links--;
1151 	node->tn_dir.tn_parent = NULL;
1152 #endif
1153 	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1154 	    TMPFS_NODE_MODIFIED;
1155 
1156 #if 0
1157 	/* handled by tmpfs_free_node */
1158 	KKASSERT(dnode->tn_links > 0);
1159 	dnode->tn_links--;
1160 #endif
1161 	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1162 	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1163 
1164 	TMPFS_NODE_UNLOCK(dnode);
1165 	TMPFS_NODE_UNLOCK(node);
1166 
1167 	/* Free the directory entry we just deleted.  Note that the node
1168 	 * referred by it will not be removed until the vnode is really
1169 	 * reclaimed. */
1170 	tmpfs_free_dirent(tmp, de);
1171 
1172 	/* Release the deleted vnode (will destroy the node, notify
1173 	 * interested parties and clean it from the cache). */
1174 
1175 	TMPFS_NODE_LOCK(dnode);
1176 	dnode->tn_status |= TMPFS_NODE_CHANGED;
1177 	TMPFS_NODE_UNLOCK(dnode);
1178 	tmpfs_update(dvp);
1179 
1180 	cache_setunresolved(v->a_nch);
1181 	cache_setvp(v->a_nch, NULL);
1182 	/*cache_inval_vp(vp, CINV_DESTROY);*/
1183 	error = 0;
1184 
1185 out:
1186 	vrele(vp);
1187 
1188 	return error;
1189 }
1190 
1191 /* --------------------------------------------------------------------- */
1192 
1193 static int
1194 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1195 {
1196 	struct vnode *dvp = v->a_dvp;
1197 	struct vnode **vpp = v->a_vpp;
1198 	struct namecache *ncp = v->a_nch->ncp;
1199 	struct vattr *vap = v->a_vap;
1200 	struct ucred *cred = v->a_cred;
1201 	char *target = v->a_target;
1202 	int error;
1203 
1204 	vap->va_type = VLNK;
1205 	error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1206 	if (error == 0) {
1207 		cache_setunresolved(v->a_nch);
1208 		cache_setvp(v->a_nch, *vpp);
1209 	}
1210 
1211 	return error;
1212 }
1213 
1214 /* --------------------------------------------------------------------- */
1215 
1216 static int
1217 tmpfs_readdir(struct vop_readdir_args *v)
1218 {
1219 	struct vnode *vp = v->a_vp;
1220 	struct uio *uio = v->a_uio;
1221 	int *eofflag = v->a_eofflag;
1222 	off_t **cookies = v->a_cookies;
1223 	int *ncookies = v->a_ncookies;
1224 	struct tmpfs_mount *tmp;
1225 	int error;
1226 	off_t startoff;
1227 	off_t cnt = 0;
1228 	struct tmpfs_node *node;
1229 
1230 	/* This operation only makes sense on directory nodes. */
1231 	if (vp->v_type != VDIR)
1232 		return ENOTDIR;
1233 
1234 	tmp = VFS_TO_TMPFS(vp->v_mount);
1235 	node = VP_TO_TMPFS_DIR(vp);
1236 	startoff = uio->uio_offset;
1237 
1238 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1239 		error = tmpfs_dir_getdotdent(node, uio);
1240 		if (error != 0)
1241 			goto outok;
1242 		cnt++;
1243 	}
1244 
1245 	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1246 		error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1247 		if (error != 0)
1248 			goto outok;
1249 		cnt++;
1250 	}
1251 
1252 	error = tmpfs_dir_getdents(node, uio, &cnt);
1253 
1254 outok:
1255 	KKASSERT(error >= -1);
1256 
1257 	if (error == -1)
1258 		error = 0;
1259 
1260 	if (eofflag != NULL)
1261 		*eofflag =
1262 		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1263 
1264 	/* Update NFS-related variables. */
1265 	if (error == 0 && cookies != NULL && ncookies != NULL) {
1266 		off_t i;
1267 		off_t off = startoff;
1268 		struct tmpfs_dirent *de = NULL;
1269 
1270 		*ncookies = cnt;
1271 		*cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1272 
1273 		for (i = 0; i < cnt; i++) {
1274 			KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1275 			if (off == TMPFS_DIRCOOKIE_DOT) {
1276 				off = TMPFS_DIRCOOKIE_DOTDOT;
1277 			} else {
1278 				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1279 					de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1280 				} else if (de != NULL) {
1281 					de = TAILQ_NEXT(de, td_entries);
1282 				} else {
1283 					de = tmpfs_dir_lookupbycookie(node,
1284 					    off);
1285 					KKASSERT(de != NULL);
1286 					de = TAILQ_NEXT(de, td_entries);
1287 				}
1288 				if (de == NULL)
1289 					off = TMPFS_DIRCOOKIE_EOF;
1290 				else
1291 					off = tmpfs_dircookie(de);
1292 			}
1293 
1294 			(*cookies)[i] = off;
1295 		}
1296 		KKASSERT(uio->uio_offset == off);
1297 	}
1298 
1299 	return error;
1300 }
1301 
1302 /* --------------------------------------------------------------------- */
1303 
1304 static int
1305 tmpfs_readlink(struct vop_readlink_args *v)
1306 {
1307 	struct vnode *vp = v->a_vp;
1308 	struct uio *uio = v->a_uio;
1309 
1310 	int error;
1311 	struct tmpfs_node *node;
1312 
1313 	KKASSERT(uio->uio_offset == 0);
1314 	KKASSERT(vp->v_type == VLNK);
1315 
1316 	node = VP_TO_TMPFS_NODE(vp);
1317 
1318 	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1319 	    uio);
1320 	TMPFS_NODE_LOCK(node);
1321 	node->tn_status |= TMPFS_NODE_ACCESSED;
1322 	TMPFS_NODE_UNLOCK(node);
1323 
1324 	return error;
1325 }
1326 
1327 /* --------------------------------------------------------------------- */
1328 
1329 static int
1330 tmpfs_inactive(struct vop_inactive_args *v)
1331 {
1332 	struct vnode *vp = v->a_vp;
1333 
1334 	struct tmpfs_node *node;
1335 
1336 	node = VP_TO_TMPFS_NODE(vp);
1337 
1338 	/*
1339 	 * Get rid of unreferenced deleted vnodes sooner rather than
1340 	 * later so the data memory can be recovered immediately.
1341 	 *
1342 	 * We must truncate the vnode to prevent the normal reclamation
1343 	 * path from flushing the data for the removed file to disk.
1344 	 */
1345 	TMPFS_NODE_LOCK(node);
1346 	if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1347 	    (node->tn_links == 0 ||
1348 	     (node->tn_links == 1 && node->tn_type == VDIR &&
1349 	      node->tn_dir.tn_parent)))
1350 	{
1351 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1352 		TMPFS_NODE_UNLOCK(node);
1353 		if (node->tn_type == VREG)
1354 			tmpfs_truncate(vp, 0);
1355 		vrecycle(vp);
1356 	} else {
1357 		TMPFS_NODE_UNLOCK(node);
1358 	}
1359 
1360 	return 0;
1361 }
1362 
1363 /* --------------------------------------------------------------------- */
1364 
1365 int
1366 tmpfs_reclaim(struct vop_reclaim_args *v)
1367 {
1368 	struct vnode *vp = v->a_vp;
1369 	struct tmpfs_mount *tmp;
1370 	struct tmpfs_node *node;
1371 
1372 	node = VP_TO_TMPFS_NODE(vp);
1373 	tmp = VFS_TO_TMPFS(vp->v_mount);
1374 
1375 	tmpfs_free_vp(vp);
1376 
1377 	/*
1378 	 * If the node referenced by this vnode was deleted by the
1379 	 * user, we must free its associated data structures now that
1380 	 * the vnode is being reclaimed.
1381 	 *
1382 	 * Directories have an extra link ref.
1383 	 */
1384 	TMPFS_NODE_LOCK(node);
1385 	if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1386 	    (node->tn_links == 0 ||
1387 	     (node->tn_links == 1 && node->tn_type == VDIR &&
1388 	      node->tn_dir.tn_parent)))
1389 	{
1390 		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1391 		tmpfs_free_node(tmp, node);
1392 		/* eats the lock */
1393 	} else {
1394 		TMPFS_NODE_UNLOCK(node);
1395 	}
1396 
1397 	KKASSERT(vp->v_data == NULL);
1398 	return 0;
1399 }
1400 
1401 /* --------------------------------------------------------------------- */
1402 
1403 static int
1404 tmpfs_print(struct vop_print_args *v)
1405 {
1406 	struct vnode *vp = v->a_vp;
1407 
1408 	struct tmpfs_node *node;
1409 
1410 	node = VP_TO_TMPFS_NODE(vp);
1411 
1412 	kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1413 	    node, node->tn_flags, node->tn_links);
1414 	kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1415 	    node->tn_mode, node->tn_uid, node->tn_gid,
1416 	    (uintmax_t)node->tn_size, node->tn_status);
1417 
1418 	if (vp->v_type == VFIFO)
1419 		fifo_printinfo(vp);
1420 
1421 	kprintf("\n");
1422 
1423 	return 0;
1424 }
1425 
1426 /* --------------------------------------------------------------------- */
1427 
1428 static int
1429 tmpfs_pathconf(struct vop_pathconf_args *v)
1430 {
1431 	int name = v->a_name;
1432 	register_t *retval = v->a_retval;
1433 
1434 	int error;
1435 
1436 	error = 0;
1437 
1438 	switch (name) {
1439 	case _PC_LINK_MAX:
1440 		*retval = LINK_MAX;
1441 		break;
1442 
1443 	case _PC_NAME_MAX:
1444 		*retval = NAME_MAX;
1445 		break;
1446 
1447 	case _PC_PATH_MAX:
1448 		*retval = PATH_MAX;
1449 		break;
1450 
1451 	case _PC_PIPE_BUF:
1452 		*retval = PIPE_BUF;
1453 		break;
1454 
1455 	case _PC_CHOWN_RESTRICTED:
1456 		*retval = 1;
1457 		break;
1458 
1459 	case _PC_NO_TRUNC:
1460 		*retval = 1;
1461 		break;
1462 
1463 	case _PC_SYNC_IO:
1464 		*retval = 1;
1465 		break;
1466 
1467 	case _PC_FILESIZEBITS:
1468 		*retval = 0; /* XXX Don't know which value should I return. */
1469 		break;
1470 
1471 	default:
1472 		error = EINVAL;
1473 	}
1474 
1475 	return error;
1476 }
1477 
1478 /* --------------------------------------------------------------------- */
1479 
1480 /*
1481  * vnode operations vector used for files stored in a tmpfs file system.
1482  */
1483 struct vop_ops tmpfs_vnode_vops = {
1484 	.vop_default =			vop_defaultop,
1485 	.vop_getpages = 		vop_stdgetpages,
1486 	.vop_putpages = 		vop_stdputpages,
1487 	.vop_ncreate =			tmpfs_ncreate,
1488 	.vop_nresolve =			tmpfs_nresolve,
1489 	.vop_nlookupdotdot =		tmpfs_nlookupdotdot,
1490 	.vop_nmknod =			tmpfs_nmknod,
1491 	.vop_open =			tmpfs_open,
1492 	.vop_close =			tmpfs_close,
1493 	.vop_access =			tmpfs_access,
1494 	.vop_getattr =			tmpfs_getattr,
1495 	.vop_setattr =			tmpfs_setattr,
1496 	.vop_read =			tmpfs_read,
1497 	.vop_write =			tmpfs_write,
1498 	.vop_fsync =			tmpfs_fsync,
1499 	.vop_nremove =			tmpfs_nremove,
1500 	.vop_nlink =			tmpfs_nlink,
1501 	.vop_nrename =			tmpfs_nrename,
1502 	.vop_nmkdir =			tmpfs_nmkdir,
1503 	.vop_nrmdir =			tmpfs_nrmdir,
1504 	.vop_nsymlink =			tmpfs_nsymlink,
1505 	.vop_readdir =			tmpfs_readdir,
1506 	.vop_readlink =			tmpfs_readlink,
1507 	.vop_inactive =			tmpfs_inactive,
1508 	.vop_reclaim =			tmpfs_reclaim,
1509 	.vop_print =			tmpfs_print,
1510 	.vop_pathconf =			tmpfs_pathconf,
1511 	.vop_bmap =			tmpfs_bmap,
1512 	.vop_strategy =			tmpfs_strategy,
1513 	.vop_advlock =			tmpfs_advlock,
1514 };
1515