tmpfs_vnops.c revision 171070
1/*	$NetBSD: tmpfs_vnops.c,v 1.35 2007/01/04 15:42:37 elad Exp $	*/
2
3/*
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *        This product includes software developed by the NetBSD
22 *        Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * tmpfs vnode interface.
42 */
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sys/fs/tmpfs/tmpfs_vnops.c 171070 2007-06-28 02:39:31Z delphij $");
45
46#include <sys/param.h>
47#include <sys/fcntl.h>
48#include <sys/lockf.h>
49#include <sys/namei.h>
50#include <sys/priv.h>
51#include <sys/proc.h>
52#include <sys/resourcevar.h>
53#include <sys/stat.h>
54#include <sys/systm.h>
55#include <sys/unistd.h>
56#include <sys/vnode.h>
57
58#include <vm/vm.h>
59#include <vm/vm_object.h>
60#include <vm/vm_page.h>
61#include <vm/vm_pager.h>
62#include <sys/sched.h>
63#include <sys/sf_buf.h>
64#include <machine/_inttypes.h>
65
66#include <fs/fifofs/fifo.h>
67#include <fs/tmpfs/tmpfs_vnops.h>
68#include <fs/tmpfs/tmpfs.h>
69
70/* --------------------------------------------------------------------- */
71
72static int
73tmpfs_lookup(struct vop_cachedlookup_args *v)
74{
75	struct vnode *dvp = v->a_dvp;
76	struct vnode **vpp = v->a_vpp;
77	struct componentname *cnp = v->a_cnp;
78	struct thread *td = cnp->cn_thread;
79
80	int error;
81	struct tmpfs_dirent *de;
82	struct tmpfs_node *dnode;
83
84	dnode = VP_TO_TMPFS_DIR(dvp);
85	*vpp = NULLVP;
86
87	/* Check accessibility of requested node as a first step. */
88	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td);
89	if (error != 0)
90		goto out;
91
92	/* We cannot be requesting the parent directory of the root node. */
93	MPASS(IMPLIES(dnode->tn_type == VDIR &&
94	    dnode->tn_dir.tn_parent == dnode,
95	    !(cnp->cn_flags & ISDOTDOT)));
96
97	if (cnp->cn_flags & ISDOTDOT) {
98		VOP_UNLOCK(dvp, 0, td);
99
100		/* Allocate a new vnode on the matching entry. */
101		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent, vpp, td);
102
103		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
104
105		dnode->tn_dir.tn_parent->tn_lookup_dirent = NULL;
106	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
107		VREF(dvp);
108		*vpp = dvp;
109		dnode->tn_lookup_dirent = NULL;
110		error = 0;
111	} else {
112		de = tmpfs_dir_lookup(dnode, 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, vpp, td);
164				if (error != 0)
165					goto out;
166
167				if ((dnode->tn_mode & S_ISTXT) &&
168				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
169				  VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
170					error = EPERM;
171					vput(*vpp);
172					*vpp = NULL;
173					goto out;
174				}
175				tnode->tn_lookup_dirent = de;
176				cnp->cn_flags |= SAVENAME;
177			}
178			else
179				error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp, td);
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, td)));
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, v->a_td));
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, v->a_td));
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, v->a_td));
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	int mode = v->a_mode;
291	struct ucred *cred = v->a_cred;
292
293	int error;
294	struct tmpfs_node *node;
295
296	MPASS(VOP_ISLOCKED(vp, v->a_td));
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 (mode & 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 (mode & 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, mode, cred, NULL);
333
334out:
335	MPASS(VOP_ISLOCKED(vp, v->a_td));
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	VATTR_NULL(vap);
353
354	tmpfs_update(vp);
355
356	vap->va_type = vp->v_type;
357	vap->va_mode = node->tn_mode;
358	vap->va_nlink = node->tn_links;
359	vap->va_uid = node->tn_uid;
360	vap->va_gid = node->tn_gid;
361	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
362	vap->va_fileid = node->tn_id;
363	vap->va_size = node->tn_size;
364	vap->va_blocksize = PAGE_SIZE;
365	vap->va_atime = node->tn_atime;
366	vap->va_mtime = node->tn_mtime;
367	vap->va_ctime = node->tn_ctime;
368	vap->va_birthtime = node->tn_birthtime;
369	vap->va_gen = node->tn_gen;
370	vap->va_flags = node->tn_flags;
371	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
372		node->tn_rdev : VNOVAL;
373	vap->va_bytes = round_page(node->tn_size);
374	vap->va_filerev = VNOVAL;
375	vap->va_vaflags = 0;
376	vap->va_spare = VNOVAL; /* XXX */
377
378	return 0;
379}
380
381/* --------------------------------------------------------------------- */
382
383/* XXX Should this operation be atomic?  I think it should, but code in
384 * XXX other places (e.g., ufs) doesn't seem to be... */
385int
386tmpfs_setattr(struct vop_setattr_args *v)
387{
388	struct vnode *vp = v->a_vp;
389	struct vattr *vap = v->a_vap;
390	struct ucred *cred = v->a_cred;
391	struct thread *l = v->a_td;
392
393	int error;
394
395	MPASS(VOP_ISLOCKED(vp, l));
396
397	error = 0;
398
399	/* Abort if any unsettable attribute is given. */
400	if (vap->va_type != VNON ||
401	    vap->va_nlink != VNOVAL ||
402	    vap->va_fsid != VNOVAL ||
403	    vap->va_fileid != VNOVAL ||
404	    vap->va_blocksize != VNOVAL ||
405	    vap->va_gen != VNOVAL ||
406	    vap->va_rdev != VNOVAL ||
407	    vap->va_bytes != VNOVAL)
408		error = EINVAL;
409
410	if (error == 0 && (vap->va_flags != VNOVAL))
411		error = tmpfs_chflags(vp, vap->va_flags, cred, l);
412
413	if (error == 0 && (vap->va_size != VNOVAL))
414		error = tmpfs_chsize(vp, vap->va_size, cred, l);
415
416	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
417		error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred,
418		    l);
419
420	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
421		error = tmpfs_chmod(vp, vap->va_mode, cred, l);
422
423	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
424	    vap->va_atime.tv_nsec != VNOVAL) ||
425	    (vap->va_mtime.tv_sec != VNOVAL &&
426	    vap->va_mtime.tv_nsec != VNOVAL) ||
427	    (vap->va_birthtime.tv_sec != VNOVAL &&
428	    vap->va_birthtime.tv_nsec != VNOVAL)))
429		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
430			&vap->va_birthtime, vap->va_vaflags, cred, l);
431
432	/* Update the node times.  We give preference to the error codes
433	 * generated by this function rather than the ones that may arise
434	 * from tmpfs_update. */
435	tmpfs_update(vp);
436
437	MPASS(VOP_ISLOCKED(vp, l));
438
439	return error;
440}
441
442/* --------------------------------------------------------------------- */
443static int
444tmpfs_uio_xfer(struct tmpfs_mount *tmp, struct tmpfs_node *node,
445    struct uio *uio, vm_object_t uobj)
446{
447	struct sf_buf *sf;
448	vm_pindex_t idx;
449	vm_offset_t d;
450	vm_page_t m;
451	size_t len;
452	int error = 0;
453
454	/* uobj - locked by caller */
455
456	VM_OBJECT_LOCK(uobj);
457	vm_object_pip_add(uobj, 1);
458	while (error == 0 && uio->uio_resid > 0) {
459		if (node->tn_size <= uio->uio_offset)
460			break;
461
462		len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
463		if (len == 0)
464			break;
465
466		idx = OFF_TO_IDX(uio->uio_offset);
467		d = uio->uio_offset - IDX_TO_OFF(idx);
468		len = MIN(len, (PAGE_SIZE - d));
469		m = vm_page_grab(uobj, idx, VM_ALLOC_WIRED | VM_ALLOC_ZERO |
470				VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
471		if (uio->uio_rw == UIO_READ && m->valid != VM_PAGE_BITS_ALL)
472			vm_page_zero_invalid(m, TRUE);
473		VM_OBJECT_UNLOCK(uobj);
474		sched_pin();
475		sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
476		error = uiomove((void *)(sf_buf_kva(sf) + d), len, uio);
477		sf_buf_free(sf);
478		sched_unpin();
479		VM_OBJECT_LOCK(uobj);
480		vm_page_lock_queues();
481		if (error == 0 && uio->uio_rw == UIO_WRITE) {
482			vm_page_set_validclean(m, d, len);
483			vm_page_zero_invalid(m, TRUE);
484			vm_page_dirty(m);
485		}
486		vm_page_unwire(m, 0);
487		vm_page_activate(m);
488		vm_page_wakeup(m);
489		vm_page_unlock_queues();
490	}
491	vm_object_pip_subtract(uobj, 1);
492	VM_OBJECT_UNLOCK(uobj);
493	return error;
494}
495
496static int
497tmpfs_read(struct vop_read_args *v)
498{
499	struct vnode *vp = v->a_vp;
500	struct uio *uio = v->a_uio;
501
502	struct tmpfs_node *node;
503	vm_object_t uobj;
504
505	int error;
506
507	node = VP_TO_TMPFS_NODE(vp);
508
509	if (vp->v_type != VREG) {
510		error = EISDIR;
511		goto out;
512	}
513
514	if (uio->uio_offset < 0) {
515		error = EINVAL;
516		goto out;
517	}
518
519	node->tn_status |= TMPFS_NODE_ACCESSED;
520
521	uobj = node->tn_reg.tn_aobj;
522	error = tmpfs_uio_xfer(VFS_TO_TMPFS(vp->v_mount), node, uio, uobj);
523
524out:
525
526	return error;
527}
528
529/* --------------------------------------------------------------------- */
530
531static int
532tmpfs_write(struct vop_write_args *v)
533{
534	struct vnode *vp = v->a_vp;
535	struct uio *uio = v->a_uio;
536	int ioflag = v->a_ioflag;
537	struct thread *td = uio->uio_td;
538
539	boolean_t extended;
540	int error;
541	off_t oldsize;
542	struct tmpfs_node *node;
543	vm_object_t uobj;
544
545	node = VP_TO_TMPFS_NODE(vp);
546	oldsize = node->tn_size;
547
548	if (uio->uio_offset < 0 || vp->v_type != VREG) {
549		error = EINVAL;
550		goto out;
551	}
552
553	if (uio->uio_resid == 0) {
554		error = 0;
555		goto out;
556	}
557
558	if (ioflag & IO_APPEND)
559		uio->uio_offset = node->tn_size;
560
561	if (uio->uio_offset + uio->uio_resid >
562	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
563		return (EFBIG);
564
565	if (vp->v_type == VREG && td != NULL) {
566		PROC_LOCK(td->td_proc);
567		if (uio->uio_offset + uio->uio_resid >
568		  lim_cur(td->td_proc, RLIMIT_FSIZE)) {
569			psignal(td->td_proc, SIGXFSZ);
570			PROC_UNLOCK(td->td_proc);
571			return (EFBIG);
572		}
573		PROC_UNLOCK(td->td_proc);
574	}
575
576	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
577	if (extended) {
578		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
579		if (error != 0)
580			goto out;
581	}
582
583	uobj = node->tn_reg.tn_aobj;
584	error = tmpfs_uio_xfer(VFS_TO_TMPFS(vp->v_mount), node, uio, uobj);
585
586	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
587	    (extended ? TMPFS_NODE_CHANGED : 0);
588
589	if (node->tn_mode & (S_ISUID | S_ISGID)) {
590		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
591			node->tn_mode &= ~(S_ISUID | S_ISGID);
592	}
593
594	if (error != 0)
595		(void)tmpfs_reg_resize(vp, oldsize);
596
597out:
598	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
599	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
600
601	return error;
602}
603
604/* --------------------------------------------------------------------- */
605
606static int
607tmpfs_fsync(struct vop_fsync_args *v)
608{
609	struct vnode *vp = v->a_vp;
610
611	MPASS(VOP_ISLOCKED(vp, v->a_td));
612
613	tmpfs_update(vp);
614
615	return 0;
616}
617
618/* --------------------------------------------------------------------- */
619
620static int
621tmpfs_remove(struct vop_remove_args *v)
622{
623	struct vnode *dvp = v->a_dvp;
624	struct vnode *vp = v->a_vp;
625
626	int error;
627	struct tmpfs_dirent *de;
628	struct tmpfs_mount *tmp;
629	struct tmpfs_node *dnode;
630	struct tmpfs_node *node;
631
632	MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread));
633	MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread));
634
635	if (vp->v_type == VDIR) {
636		error = EISDIR;
637		goto out;
638	}
639
640	dnode = VP_TO_TMPFS_DIR(dvp);
641	node = VP_TO_TMPFS_NODE(vp);
642	tmp = VFS_TO_TMPFS(vp->v_mount);
643	de = node->tn_lookup_dirent;
644	MPASS(de != NULL);
645
646	/* Files marked as immutable or append-only cannot be deleted. */
647	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
648	    (dnode->tn_flags & APPEND)) {
649		error = EPERM;
650		goto out;
651	}
652
653	/* Remove the entry from the directory; as it is a file, we do not
654	 * have to change the number of hard links of the directory. */
655	tmpfs_dir_detach(dvp, de);
656
657	/* Free the directory entry we just deleted.  Note that the node
658	 * referred by it will not be removed until the vnode is really
659	 * reclaimed. */
660	tmpfs_free_dirent(tmp, de, TRUE);
661
662	if (node->tn_links > 0)
663		node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
664	    TMPFS_NODE_MODIFIED;
665	error = 0;
666
667out:
668
669	return error;
670}
671
672/* --------------------------------------------------------------------- */
673
674static int
675tmpfs_link(struct vop_link_args *v)
676{
677	struct vnode *dvp = v->a_tdvp;
678	struct vnode *vp = v->a_vp;
679	struct componentname *cnp = v->a_cnp;
680
681	int error;
682	struct tmpfs_dirent *de;
683	struct tmpfs_node *dnode;
684	struct tmpfs_node *node;
685
686	MPASS(VOP_ISLOCKED(dvp, cnp->cn_thread));
687	MPASS(cnp->cn_flags & HASBUF);
688	MPASS(dvp != vp); /* XXX When can this be false? */
689
690	dnode = VP_TO_TMPFS_DIR(dvp);
691	node = VP_TO_TMPFS_NODE(vp);
692
693	/* XXX: Why aren't the following two tests done by the caller? */
694
695	/* Hard links of directories are forbidden. */
696	if (vp->v_type == VDIR) {
697		error = EPERM;
698		goto out;
699	}
700
701	/* Cannot create cross-device links. */
702	if (dvp->v_mount != vp->v_mount) {
703		error = EXDEV;
704		goto out;
705	}
706
707	/* Ensure that we do not overflow the maximum number of links imposed
708	 * by the system. */
709	MPASS(node->tn_links <= LINK_MAX);
710	if (node->tn_links == LINK_MAX) {
711		error = EMLINK;
712		goto out;
713	}
714
715	/* We cannot create links of files marked immutable or append-only. */
716	if (node->tn_flags & (IMMUTABLE | APPEND)) {
717		error = EPERM;
718		goto out;
719	}
720
721	/* Allocate a new directory entry to represent the node. */
722	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
723	    cnp->cn_nameptr, cnp->cn_namelen, &de);
724	if (error != 0)
725		goto out;
726
727	/* Insert the new directory entry into the appropriate directory. */
728	tmpfs_dir_attach(dvp, de);
729
730	/* vp link count has changed, so update node times. */
731	node->tn_status |= TMPFS_NODE_CHANGED;
732	tmpfs_update(vp);
733
734	error = 0;
735
736out:
737	return error;
738}
739
740/* --------------------------------------------------------------------- */
741
742static int
743tmpfs_rename(struct vop_rename_args *v)
744{
745	struct vnode *fdvp = v->a_fdvp;
746	struct vnode *fvp = v->a_fvp;
747	struct componentname *fcnp = v->a_fcnp;
748	struct vnode *tdvp = v->a_tdvp;
749	struct vnode *tvp = v->a_tvp;
750	struct componentname *tcnp = v->a_tcnp;
751	struct tmpfs_node *tnode = 0; /* pacify gcc */
752
753	char *newname;
754	int error;
755	struct tmpfs_dirent *de;
756	struct tmpfs_mount *tmp;
757	struct tmpfs_node *fdnode;
758	struct tmpfs_node *fnode;
759	struct tmpfs_node *tdnode;
760
761	MPASS(VOP_ISLOCKED(tdvp, tcnp->cn_thread));
762	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp, tcnp->cn_thread)));
763	MPASS(fcnp->cn_flags & HASBUF);
764	MPASS(tcnp->cn_flags & HASBUF);
765
766	fdnode = VP_TO_TMPFS_DIR(fdvp);
767	fnode = VP_TO_TMPFS_NODE(fvp);
768	de = fnode->tn_lookup_dirent;
769
770	/* Disallow cross-device renames.
771	 * XXX Why isn't this done by the caller? */
772	if (fvp->v_mount != tdvp->v_mount ||
773	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
774		error = EXDEV;
775		goto out;
776	}
777
778	tmp = VFS_TO_TMPFS(tdvp->v_mount);
779	tdnode = VP_TO_TMPFS_DIR(tdvp);
780
781	/* If source and target are the same file, there is nothing to do. */
782	if (fvp == tvp) {
783		error = 0;
784		goto out;
785	}
786
787	/* Avoid manipulating '.' and '..' entries. */
788	if (de == NULL) {
789		MPASS(fvp->v_type == VDIR);
790		error = EINVAL;
791		goto out;
792	}
793	MPASS(de->td_node == fnode);
794
795	/* If re-naming a directory to another preexisting directory
796	 * ensure that the target directory is empty so that its
797	 * removal causes no side effects.
798	 * Kern_rename gurantees the destination to be a directory
799	 * if the source is one. */
800	if (tvp != NULL) {
801		tnode = VP_TO_TMPFS_NODE(tvp);
802
803		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
804		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
805			error = EPERM;
806			goto out;
807		}
808
809		if ((de->td_node->tn_type == VDIR) && (tnode->tn_size > 0)) {
810			error = ENOTEMPTY;
811			goto out;
812		}
813	}
814
815	/* If we need to move the directory between entries, lock the
816	 * source so that we can safely operate on it. */
817	if (fdnode != tdnode) {
818		error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, tcnp->cn_thread);
819		if (error != 0)
820			goto out;
821	}
822
823	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
824	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
825		error = EPERM;
826		goto out_locked;
827	}
828
829	/* Ensure that we have enough memory to hold the new name, if it
830	 * has to be changed. */
831	if (fcnp->cn_namelen != tcnp->cn_namelen ||
832	    memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
833		newname = tmpfs_str_zone_alloc(&tmp->tm_str_pool, M_WAITOK,
834		    tcnp->cn_namelen);
835		if (newname == NULL) {
836			error = ENOSPC;
837			goto out_locked;
838		}
839	} else
840		newname = NULL;
841
842	/* If the node is being moved to another directory, we have to do
843	 * the move. */
844	if (fdnode != tdnode) {
845		/* In case we are moving a directory, we have to adjust its
846		 * parent to point to the new parent. */
847		if (de->td_node->tn_type == VDIR) {
848			struct tmpfs_node *n;
849
850			/* Ensure the target directory is not a child of the
851			 * directory being moved.  Otherwise, we'd end up
852			 * with stale nodes. */
853			n = tdnode;
854			while (n != n->tn_dir.tn_parent) {
855				if (n == fnode) {
856					error = EINVAL;
857					if (newname != NULL)
858						tmpfs_str_zone_free(&tmp->tm_str_pool,
859						    newname, tcnp->cn_namelen);
860					goto out_locked;
861				}
862				n = n->tn_dir.tn_parent;
863			}
864
865			/* Adjust the parent pointer. */
866			TMPFS_VALIDATE_DIR(fnode);
867			de->td_node->tn_dir.tn_parent = tdnode;
868
869			/* As a result of changing the target of the '..'
870			 * entry, the link count of the source and target
871			 * directories has to be adjusted. */
872			fdnode->tn_links--;
873			tdnode->tn_links++;
874		}
875
876		/* Do the move: just remove the entry from the source directory
877		 * and insert it into the target one. */
878		tmpfs_dir_detach(fdvp, de);
879		tmpfs_dir_attach(tdvp, de);
880	}
881
882	/* If the name has changed, we need to make it effective by changing
883	 * it in the directory entry. */
884	if (newname != NULL) {
885		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
886
887		tmpfs_str_zone_free(&tmp->tm_str_pool, de->td_name,
888		    de->td_namelen);
889		de->td_namelen = (uint16_t)tcnp->cn_namelen;
890		memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
891		de->td_name = newname;
892
893		fnode->tn_status |= TMPFS_NODE_CHANGED;
894		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
895	}
896
897	/* If we are overwriting an entry, we have to remove the old one
898	 * from the target directory. */
899	if (tvp != NULL) {
900		/* Remove the old entry from the target directory. */
901		de = tnode->tn_lookup_dirent;
902		tmpfs_dir_detach(tdvp, de);
903
904		/* Free the directory entry we just deleted.  Note that the
905		 * node referred by it will not be removed until the vnode is
906		 * really reclaimed. */
907		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
908	}
909
910	error = 0;
911
912out_locked:
913	if (fdnode != tdnode)
914		VOP_UNLOCK(fdvp, 0, tcnp->cn_thread);
915
916out:
917	/* Release target nodes. */
918	/* XXX: I don't understand when tdvp can be the same as tvp, but
919	 * other code takes care of this... */
920	if (tdvp == tvp)
921		vrele(tdvp);
922	else
923		vput(tdvp);
924	if (tvp != NULL)
925		vput(tvp);
926
927	/* Release source nodes. */
928	vrele(fdvp);
929	vrele(fvp);
930
931	return error;
932}
933
934/* --------------------------------------------------------------------- */
935
936static int
937tmpfs_mkdir(struct vop_mkdir_args *v)
938{
939	struct vnode *dvp = v->a_dvp;
940	struct vnode **vpp = v->a_vpp;
941	struct componentname *cnp = v->a_cnp;
942	struct vattr *vap = v->a_vap;
943
944	MPASS(vap->va_type == VDIR);
945
946	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
947}
948
949/* --------------------------------------------------------------------- */
950
951static int
952tmpfs_rmdir(struct vop_rmdir_args *v)
953{
954	struct vnode *dvp = v->a_dvp;
955	struct vnode *vp = v->a_vp;
956
957	int error;
958	struct tmpfs_dirent *de;
959	struct tmpfs_mount *tmp;
960	struct tmpfs_node *dnode;
961	struct tmpfs_node *node;
962
963	MPASS(VOP_ISLOCKED(dvp, v->a_cnp->cn_thread));
964	MPASS(VOP_ISLOCKED(vp, v->a_cnp->cn_thread));
965
966	tmp = VFS_TO_TMPFS(dvp->v_mount);
967	dnode = VP_TO_TMPFS_DIR(dvp);
968	node = VP_TO_TMPFS_DIR(vp);
969
970	/* Directories with more than two entries ('.' and '..') cannot be
971	 * removed. */
972	 if (node->tn_size > 0) {
973		 error = ENOTEMPTY;
974		 goto out;
975	 }
976
977	if ((dnode->tn_flags & APPEND)
978	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
979		error = EPERM;
980		goto out;
981	}
982
983	/* This invariant holds only if we are not trying to remove "..".
984	  * We checked for that above so this is safe now. */
985	MPASS(node->tn_dir.tn_parent == dnode);
986
987	/* Get the directory entry associated with node (vp).  This was
988	 * filled by tmpfs_lookup while looking up the entry. */
989	de = node->tn_lookup_dirent;
990	MPASS(TMPFS_DIRENT_MATCHES(de,
991	    v->a_cnp->cn_nameptr,
992	    v->a_cnp->cn_namelen));
993
994	/* Check flags to see if we are allowed to remove the directory. */
995	if (dnode->tn_flags & APPEND
996		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
997		error = EPERM;
998		goto out;
999	}
1000
1001	/* Detach the directory entry from the directory (dnode). */
1002	tmpfs_dir_detach(dvp, de);
1003
1004	node->tn_links--;
1005	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1006	    TMPFS_NODE_MODIFIED;
1007	node->tn_dir.tn_parent->tn_links--;
1008	node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1009	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1010
1011	cache_purge(dvp);
1012	cache_purge(vp);
1013
1014	/* Free the directory entry we just deleted.  Note that the node
1015	 * referred by it will not be removed until the vnode is really
1016	 * reclaimed. */
1017	tmpfs_free_dirent(tmp, de, TRUE);
1018
1019	/* Release the deleted vnode (will destroy the node, notify
1020	 * interested parties and clean it from the cache). */
1021
1022	dnode->tn_status |= TMPFS_NODE_CHANGED;
1023	tmpfs_update(dvp);
1024
1025	error = 0;
1026
1027out:
1028	return error;
1029}
1030
1031/* --------------------------------------------------------------------- */
1032
1033static int
1034tmpfs_symlink(struct vop_symlink_args *v)
1035{
1036	struct vnode *dvp = v->a_dvp;
1037	struct vnode **vpp = v->a_vpp;
1038	struct componentname *cnp = v->a_cnp;
1039	struct vattr *vap = v->a_vap;
1040	char *target = v->a_target;
1041
1042#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1043	MPASS(vap->va_type == VLNK);
1044#else
1045	vap->va_type = VLNK;
1046#endif
1047
1048	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1049}
1050
1051/* --------------------------------------------------------------------- */
1052
1053static int
1054tmpfs_readdir(struct vop_readdir_args *v)
1055{
1056	struct vnode *vp = v->a_vp;
1057	struct uio *uio = v->a_uio;
1058	int *eofflag = v->a_eofflag;
1059	u_long **cookies = v->a_cookies;
1060	int *ncookies = v->a_ncookies;
1061
1062	int error;
1063	off_t startoff;
1064	off_t cnt;
1065	struct tmpfs_node *node;
1066
1067	/* This operation only makes sense on directory nodes. */
1068	if (vp->v_type != VDIR) {
1069		error = ENOTDIR;
1070		goto out;
1071	}
1072
1073	node = VP_TO_TMPFS_DIR(vp);
1074
1075	startoff = uio->uio_offset;
1076
1077	cnt = 0;
1078	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1079		error = tmpfs_dir_getdotdent(node, uio);
1080		if (error == -1) {
1081			error = 0;
1082			goto outok;
1083		} else if (error != 0)
1084			goto outok;
1085		cnt++;
1086	}
1087
1088	if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1089		error = tmpfs_dir_getdotdotdent(node, uio);
1090		if (error == -1) {
1091			error = 0;
1092			goto outok;
1093		} else if (error != 0)
1094			goto outok;
1095		cnt++;
1096	}
1097
1098	error = tmpfs_dir_getdents(node, uio, &cnt);
1099	if (error == -1)
1100		error = 0;
1101	MPASS(error >= 0);
1102
1103outok:
1104	/* This label assumes that startoff has been
1105	 * initialized.  If the compiler didn't spit out warnings, we'd
1106	 * simply make this one be 'out' and drop 'outok'. */
1107
1108	if (eofflag != NULL)
1109		*eofflag =
1110		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1111
1112	/* Update NFS-related variables. */
1113	if (error == 0 && cookies != NULL && ncookies != NULL) {
1114		off_t i;
1115		off_t off = startoff;
1116		struct tmpfs_dirent *de = NULL;
1117
1118		*ncookies = cnt;
1119		*cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1120
1121		for (i = 0; i < cnt; i++) {
1122			MPASS(off != TMPFS_DIRCOOKIE_EOF);
1123			if (off == TMPFS_DIRCOOKIE_DOT) {
1124				off = TMPFS_DIRCOOKIE_DOTDOT;
1125			} else {
1126				if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1127					de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1128				} else if (de != NULL) {
1129					de = TAILQ_NEXT(de, td_entries);
1130				} else {
1131					de = tmpfs_dir_lookupbycookie(node,
1132					    off);
1133					MPASS(de != NULL);
1134					de = TAILQ_NEXT(de, td_entries);
1135				}
1136				if (de == NULL) {
1137					off = TMPFS_DIRCOOKIE_EOF;
1138				} else {
1139					off = TMPFS_DIRCOOKIE(de);
1140				}
1141			}
1142
1143			(*cookies)[i] = off;
1144		}
1145		MPASS(uio->uio_offset == off);
1146	}
1147
1148out:
1149	return error;
1150}
1151
1152/* --------------------------------------------------------------------- */
1153
1154static int
1155tmpfs_readlink(struct vop_readlink_args *v)
1156{
1157	struct vnode *vp = v->a_vp;
1158	struct uio *uio = v->a_uio;
1159
1160	int error;
1161	struct tmpfs_node *node;
1162
1163	MPASS(uio->uio_offset == 0);
1164	MPASS(vp->v_type == VLNK);
1165
1166	node = VP_TO_TMPFS_NODE(vp);
1167
1168	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1169	    uio);
1170	node->tn_status |= TMPFS_NODE_ACCESSED;
1171
1172	return error;
1173}
1174
1175/* --------------------------------------------------------------------- */
1176
1177static int
1178tmpfs_inactive(struct vop_inactive_args *v)
1179{
1180	struct vnode *vp = v->a_vp;
1181	struct thread *l = v->a_td;
1182
1183	struct tmpfs_node *node;
1184
1185	MPASS(VOP_ISLOCKED(vp, l));
1186
1187	node = VP_TO_TMPFS_NODE(vp);
1188
1189	if (node->tn_links == 0)
1190		vrecycle(vp, l);
1191
1192	return 0;
1193}
1194
1195/* --------------------------------------------------------------------- */
1196
1197int
1198tmpfs_reclaim(struct vop_reclaim_args *v)
1199{
1200	struct vnode *vp = v->a_vp;
1201
1202	struct tmpfs_mount *tmp;
1203	struct tmpfs_node *node;
1204
1205	node = VP_TO_TMPFS_NODE(vp);
1206	tmp = VFS_TO_TMPFS(vp->v_mount);
1207
1208	vnode_destroy_vobject(vp);
1209	cache_purge(vp);
1210	tmpfs_free_vp(vp);
1211
1212	/* If the node referenced by this vnode was deleted by the user,
1213	 * we must free its associated data structures (now that the vnode
1214	 * is being reclaimed). */
1215	if (node->tn_links == 0)
1216		tmpfs_free_node(tmp, node);
1217
1218	MPASS(vp->v_data == NULL);
1219	return 0;
1220}
1221
1222/* --------------------------------------------------------------------- */
1223
1224static int
1225tmpfs_print(struct vop_print_args *v)
1226{
1227	struct vnode *vp = v->a_vp;
1228
1229	struct tmpfs_node *node;
1230
1231	node = VP_TO_TMPFS_NODE(vp);
1232
1233	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1234	    node, node->tn_flags, node->tn_links);
1235	printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1236	    ", status 0x%x\n",
1237	    node->tn_mode, node->tn_uid, node->tn_gid,
1238	    (uintmax_t)node->tn_size, node->tn_status);
1239
1240	if (vp->v_type == VFIFO)
1241		fifo_printinfo(vp);
1242
1243	printf("\n");
1244
1245	return 0;
1246}
1247
1248/* --------------------------------------------------------------------- */
1249
1250static int
1251tmpfs_pathconf(struct vop_pathconf_args *v)
1252{
1253	int name = v->a_name;
1254	register_t *retval = v->a_retval;
1255
1256	int error;
1257
1258	error = 0;
1259
1260	switch (name) {
1261	case _PC_LINK_MAX:
1262		*retval = LINK_MAX;
1263		break;
1264
1265	case _PC_NAME_MAX:
1266		*retval = NAME_MAX;
1267		break;
1268
1269	case _PC_PATH_MAX:
1270		*retval = PATH_MAX;
1271		break;
1272
1273	case _PC_PIPE_BUF:
1274		*retval = PIPE_BUF;
1275		break;
1276
1277	case _PC_CHOWN_RESTRICTED:
1278		*retval = 1;
1279		break;
1280
1281	case _PC_NO_TRUNC:
1282		*retval = 1;
1283		break;
1284
1285	case _PC_SYNC_IO:
1286		*retval = 1;
1287		break;
1288
1289	case _PC_FILESIZEBITS:
1290		*retval = 0; /* XXX Don't know which value should I return. */
1291		break;
1292
1293	default:
1294		error = EINVAL;
1295	}
1296
1297	return error;
1298}
1299
1300/* --------------------------------------------------------------------- */
1301
1302static int
1303tmpfs_advlock(struct vop_advlock_args *v)
1304{
1305	struct vnode *vp = v->a_vp;
1306
1307	struct tmpfs_node *node;
1308
1309	node = VP_TO_TMPFS_NODE(vp);
1310
1311	return lf_advlock(v, &node->tn_lockf, node->tn_size);
1312}
1313
1314/* --------------------------------------------------------------------- */
1315
1316static int
1317tmpfs_vptofh(struct vop_vptofh_args *ap)
1318{
1319	struct tmpfs_fid *tfhp;
1320	struct tmpfs_node *node;
1321
1322	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1323	node = VP_TO_TMPFS_NODE(ap->a_vp);
1324
1325	tfhp->tf_len = sizeof(struct tmpfs_fid);
1326	tfhp->tf_id = node->tn_id;
1327	tfhp->tf_gen = node->tn_gen;
1328
1329	return (0);
1330}
1331
1332/* --------------------------------------------------------------------- */
1333
1334/*
1335 * vnode operations vector used for files stored in a tmpfs file system.
1336 */
1337struct vop_vector tmpfs_vnodeop_entries = {
1338	.vop_default =			&default_vnodeops,
1339	.vop_lookup =			vfs_cache_lookup,
1340	.vop_cachedlookup =		tmpfs_lookup,
1341	.vop_create =			tmpfs_create,
1342	.vop_mknod =			tmpfs_mknod,
1343	.vop_open =			tmpfs_open,
1344	.vop_close =			tmpfs_close,
1345	.vop_access =			tmpfs_access,
1346	.vop_getattr =			tmpfs_getattr,
1347	.vop_setattr =			tmpfs_setattr,
1348	.vop_read =			tmpfs_read,
1349	.vop_write =			tmpfs_write,
1350	.vop_fsync =			tmpfs_fsync,
1351	.vop_remove =			tmpfs_remove,
1352	.vop_link =			tmpfs_link,
1353	.vop_rename =			tmpfs_rename,
1354	.vop_mkdir =			tmpfs_mkdir,
1355	.vop_rmdir =			tmpfs_rmdir,
1356	.vop_symlink =			tmpfs_symlink,
1357	.vop_readdir =			tmpfs_readdir,
1358	.vop_readlink =			tmpfs_readlink,
1359	.vop_inactive =			tmpfs_inactive,
1360	.vop_reclaim =			tmpfs_reclaim,
1361	.vop_print =			tmpfs_print,
1362	.vop_pathconf =			tmpfs_pathconf,
1363	.vop_advlock =			tmpfs_advlock,
1364	.vop_vptofh =			tmpfs_vptofh,
1365	.vop_bmap =			VOP_EOPNOTSUPP,
1366};
1367
1368