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