tmpfs_vnops.c revision 253939
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 253939 2013-08-04 21:07:24Z attilio $");
38
39#include <sys/param.h>
40#include <sys/fcntl.h>
41#include <sys/lockf.h>
42#include <sys/lock.h>
43#include <sys/namei.h>
44#include <sys/priv.h>
45#include <sys/proc.h>
46#include <sys/rwlock.h>
47#include <sys/sched.h>
48#include <sys/sf_buf.h>
49#include <sys/stat.h>
50#include <sys/systm.h>
51#include <sys/sysctl.h>
52#include <sys/unistd.h>
53#include <sys/vnode.h>
54
55#include <vm/vm.h>
56#include <vm/vm_param.h>
57#include <vm/vm_object.h>
58#include <vm/vm_page.h>
59#include <vm/vm_pager.h>
60
61#include <fs/tmpfs/tmpfs_vnops.h>
62#include <fs/tmpfs/tmpfs.h>
63
64SYSCTL_DECL(_vfs_tmpfs);
65
66static volatile int tmpfs_rename_restarts;
67SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
68    __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
69    "Times rename had to restart due to lock contention");
70
71/* --------------------------------------------------------------------- */
72
73static int
74tmpfs_lookup(struct vop_cachedlookup_args *v)
75{
76	struct vnode *dvp = v->a_dvp;
77	struct vnode **vpp = v->a_vpp;
78	struct componentname *cnp = v->a_cnp;
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, cnp->cn_thread);
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	TMPFS_ASSERT_LOCKED(dnode);
98	if (dnode->tn_dir.tn_parent == NULL) {
99		error = ENOENT;
100		goto out;
101	}
102	if (cnp->cn_flags & ISDOTDOT) {
103		int ltype = 0;
104
105		ltype = VOP_ISLOCKED(dvp);
106		vhold(dvp);
107		VOP_UNLOCK(dvp, 0);
108		/* Allocate a new vnode on the matching entry. */
109		error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
110		    cnp->cn_lkflags, vpp);
111
112		vn_lock(dvp, ltype | LK_RETRY);
113		vdrop(dvp);
114	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
115		VREF(dvp);
116		*vpp = dvp;
117		error = 0;
118	} else {
119		de = tmpfs_dir_lookup(dnode, NULL, cnp);
120		if (de != NULL && de->td_node == NULL)
121			cnp->cn_flags |= ISWHITEOUT;
122		if (de == NULL || de->td_node == NULL) {
123			/* The entry was not found in the directory.
124			 * This is OK if we are creating or renaming an
125			 * entry and are working on the last component of
126			 * the path name. */
127			if ((cnp->cn_flags & ISLASTCN) &&
128			    (cnp->cn_nameiop == CREATE || \
129			    cnp->cn_nameiop == RENAME ||
130			    (cnp->cn_nameiop == DELETE &&
131			    cnp->cn_flags & DOWHITEOUT &&
132			    cnp->cn_flags & ISWHITEOUT))) {
133				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
134				    cnp->cn_thread);
135				if (error != 0)
136					goto out;
137
138				/* Keep the component name in the buffer for
139				 * future uses. */
140				cnp->cn_flags |= SAVENAME;
141
142				error = EJUSTRETURN;
143			} else
144				error = ENOENT;
145		} else {
146			struct tmpfs_node *tnode;
147
148			/* The entry was found, so get its associated
149			 * tmpfs_node. */
150			tnode = de->td_node;
151
152			/* If we are not at the last path component and
153			 * found a non-directory or non-link entry (which
154			 * may itself be pointing to a directory), raise
155			 * an error. */
156			if ((tnode->tn_type != VDIR &&
157			    tnode->tn_type != VLNK) &&
158			    !(cnp->cn_flags & ISLASTCN)) {
159				error = ENOTDIR;
160				goto out;
161			}
162
163			/* If we are deleting or renaming the entry, keep
164			 * track of its tmpfs_dirent so that it can be
165			 * easily deleted later. */
166			if ((cnp->cn_flags & ISLASTCN) &&
167			    (cnp->cn_nameiop == DELETE ||
168			    cnp->cn_nameiop == RENAME)) {
169				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
170				    cnp->cn_thread);
171				if (error != 0)
172					goto out;
173
174				/* Allocate a new vnode on the matching entry. */
175				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
176						cnp->cn_lkflags, vpp);
177				if (error != 0)
178					goto out;
179
180				if ((dnode->tn_mode & S_ISTXT) &&
181				  VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
182				  VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
183					error = EPERM;
184					vput(*vpp);
185					*vpp = NULL;
186					goto out;
187				}
188				cnp->cn_flags |= SAVENAME;
189			} else {
190				error = tmpfs_alloc_vp(dvp->v_mount, tnode,
191						cnp->cn_lkflags, vpp);
192			}
193		}
194	}
195
196	/* Store the result of this lookup in the cache.  Avoid this if the
197	 * request was for creation, as it does not improve timings on
198	 * emprical tests. */
199	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
200		cache_enter(dvp, *vpp, cnp);
201
202out:
203	/* If there were no errors, *vpp cannot be null and it must be
204	 * locked. */
205	MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
206
207	return error;
208}
209
210/* --------------------------------------------------------------------- */
211
212static int
213tmpfs_create(struct vop_create_args *v)
214{
215	struct vnode *dvp = v->a_dvp;
216	struct vnode **vpp = v->a_vpp;
217	struct componentname *cnp = v->a_cnp;
218	struct vattr *vap = v->a_vap;
219
220	MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
221
222	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
223}
224/* --------------------------------------------------------------------- */
225
226static int
227tmpfs_mknod(struct vop_mknod_args *v)
228{
229	struct vnode *dvp = v->a_dvp;
230	struct vnode **vpp = v->a_vpp;
231	struct componentname *cnp = v->a_cnp;
232	struct vattr *vap = v->a_vap;
233
234	if (vap->va_type != VBLK && vap->va_type != VCHR &&
235	    vap->va_type != VFIFO)
236		return EINVAL;
237
238	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
239}
240
241/* --------------------------------------------------------------------- */
242
243static int
244tmpfs_open(struct vop_open_args *v)
245{
246	struct vnode *vp = v->a_vp;
247	int mode = v->a_mode;
248
249	int error;
250	struct tmpfs_node *node;
251
252	MPASS(VOP_ISLOCKED(vp));
253
254	node = VP_TO_TMPFS_NODE(vp);
255
256	/* The file is still active but all its names have been removed
257	 * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
258	 * it is about to die. */
259	if (node->tn_links < 1)
260		return (ENOENT);
261
262	/* If the file is marked append-only, deny write requests. */
263	if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
264		error = EPERM;
265	else {
266		error = 0;
267		/* For regular files, the call below is nop. */
268		vnode_create_vobject(vp, node->tn_size, v->a_td);
269	}
270
271	MPASS(VOP_ISLOCKED(vp));
272	return error;
273}
274
275/* --------------------------------------------------------------------- */
276
277static int
278tmpfs_close(struct vop_close_args *v)
279{
280	struct vnode *vp = v->a_vp;
281
282	/* Update node times. */
283	tmpfs_update(vp);
284
285	return (0);
286}
287
288/* --------------------------------------------------------------------- */
289
290int
291tmpfs_access(struct vop_access_args *v)
292{
293	struct vnode *vp = v->a_vp;
294	accmode_t accmode = v->a_accmode;
295	struct ucred *cred = v->a_cred;
296
297	int error;
298	struct tmpfs_node *node;
299
300	MPASS(VOP_ISLOCKED(vp));
301
302	node = VP_TO_TMPFS_NODE(vp);
303
304	switch (vp->v_type) {
305	case VDIR:
306		/* FALLTHROUGH */
307	case VLNK:
308		/* FALLTHROUGH */
309	case VREG:
310		if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
311			error = EROFS;
312			goto out;
313		}
314		break;
315
316	case VBLK:
317		/* FALLTHROUGH */
318	case VCHR:
319		/* FALLTHROUGH */
320	case VSOCK:
321		/* FALLTHROUGH */
322	case VFIFO:
323		break;
324
325	default:
326		error = EINVAL;
327		goto out;
328	}
329
330	if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
331		error = EPERM;
332		goto out;
333	}
334
335	error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
336	    node->tn_gid, accmode, cred, NULL);
337
338out:
339	MPASS(VOP_ISLOCKED(vp));
340
341	return error;
342}
343
344/* --------------------------------------------------------------------- */
345
346int
347tmpfs_getattr(struct vop_getattr_args *v)
348{
349	struct vnode *vp = v->a_vp;
350	struct vattr *vap = v->a_vap;
351
352	struct tmpfs_node *node;
353
354	node = VP_TO_TMPFS_NODE(vp);
355
356	tmpfs_update(vp);
357
358	vap->va_type = vp->v_type;
359	vap->va_mode = node->tn_mode;
360	vap->va_nlink = node->tn_links;
361	vap->va_uid = node->tn_uid;
362	vap->va_gid = node->tn_gid;
363	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
364	vap->va_fileid = node->tn_id;
365	vap->va_size = node->tn_size;
366	vap->va_blocksize = PAGE_SIZE;
367	vap->va_atime = node->tn_atime;
368	vap->va_mtime = node->tn_mtime;
369	vap->va_ctime = node->tn_ctime;
370	vap->va_birthtime = node->tn_birthtime;
371	vap->va_gen = node->tn_gen;
372	vap->va_flags = node->tn_flags;
373	vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
374		node->tn_rdev : NODEV;
375	vap->va_bytes = round_page(node->tn_size);
376	vap->va_filerev = 0;
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 *td = curthread;
392
393	int error;
394
395	MPASS(VOP_ISLOCKED(vp));
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, td);
412
413	if (error == 0 && (vap->va_size != VNOVAL))
414		error = tmpfs_chsize(vp, vap->va_size, cred, td);
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, td);
418
419	if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
420		error = tmpfs_chmod(vp, vap->va_mode, cred, td);
421
422	if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
423	    vap->va_atime.tv_nsec != VNOVAL) ||
424	    (vap->va_mtime.tv_sec != VNOVAL &&
425	    vap->va_mtime.tv_nsec != VNOVAL) ||
426	    (vap->va_birthtime.tv_sec != VNOVAL &&
427	    vap->va_birthtime.tv_nsec != VNOVAL)))
428		error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
429			&vap->va_birthtime, vap->va_vaflags, cred, td);
430
431	/* Update the node times.  We give preference to the error codes
432	 * generated by this function rather than the ones that may arise
433	 * from tmpfs_update. */
434	tmpfs_update(vp);
435
436	MPASS(VOP_ISLOCKED(vp));
437
438	return error;
439}
440
441static int
442tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
443    vm_offset_t offset, size_t tlen, struct uio *uio)
444{
445	vm_page_t	m;
446	int		error, rv;
447
448	VM_OBJECT_WLOCK(tobj);
449
450	/*
451	 * Parallel reads of the page content from disk are prevented
452	 * by VPO_BUSY.
453	 *
454	 * Although the tmpfs vnode lock is held here, it is
455	 * nonetheless safe to sleep waiting for a free page.  The
456	 * pageout daemon does not need to acquire the tmpfs vnode
457	 * lock to page out tobj's pages because tobj is a OBJT_SWAP
458	 * type object.
459	 */
460	m = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY |
461	    VM_ALLOC_NOBUSY);
462	if (m->valid != VM_PAGE_BITS_ALL) {
463		vm_page_busy(m);
464		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
465			rv = vm_pager_get_pages(tobj, &m, 1, 0);
466			m = vm_page_lookup(tobj, idx);
467			if (m == NULL) {
468				printf(
469		    "tmpfs: vm_obj %p idx %jd null lookup rv %d\n",
470				    tobj, idx, rv);
471				VM_OBJECT_WUNLOCK(tobj);
472				return (EIO);
473			}
474			if (rv != VM_PAGER_OK) {
475				printf(
476		    "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n",
477				    tobj, idx, m->valid, rv);
478				vm_page_lock(m);
479				vm_page_free(m);
480				vm_page_unlock(m);
481				VM_OBJECT_WUNLOCK(tobj);
482				return (EIO);
483			}
484		} else
485			vm_page_zero_invalid(m, TRUE);
486		vm_page_wakeup(m);
487	}
488	vm_page_io_start(m);
489	VM_OBJECT_WUNLOCK(tobj);
490	error = uiomove_fromphys(&m, offset, tlen, uio);
491	VM_OBJECT_WLOCK(tobj);
492	vm_page_io_finish(m);
493	VM_OBJECT_WUNLOCK(tobj);
494	vm_page_lock(m);
495	if (m->queue == PQ_NONE) {
496		vm_page_deactivate(m);
497	} else {
498		/* Requeue to maintain LRU ordering. */
499		vm_page_requeue(m);
500	}
501	vm_page_unlock(m);
502
503	return (error);
504}
505
506static int
507tmpfs_read(struct vop_read_args *v)
508{
509	struct vnode *vp = v->a_vp;
510	struct uio *uio = v->a_uio;
511	struct tmpfs_node *node;
512	vm_object_t uobj;
513	size_t len;
514	int resid;
515	int error = 0;
516	vm_pindex_t	idx;
517	vm_offset_t	offset;
518	off_t		addr;
519	size_t		tlen;
520
521	node = VP_TO_TMPFS_NODE(vp);
522
523	if (vp->v_type != VREG) {
524		error = EISDIR;
525		goto out;
526	}
527
528	if (uio->uio_offset < 0) {
529		error = EINVAL;
530		goto out;
531	}
532
533	node->tn_status |= TMPFS_NODE_ACCESSED;
534
535	uobj = node->tn_reg.tn_aobj;
536	while ((resid = uio->uio_resid) > 0) {
537		error = 0;
538		if (node->tn_size <= uio->uio_offset)
539			break;
540		len = MIN(node->tn_size - uio->uio_offset, resid);
541		if (len == 0)
542			break;
543		addr = uio->uio_offset;
544		idx = OFF_TO_IDX(addr);
545		offset = addr & PAGE_MASK;
546		tlen = MIN(PAGE_SIZE - offset, len);
547		error = tmpfs_nocacheread(uobj, idx, offset, tlen, uio);
548		if ((error != 0) || (resid == uio->uio_resid))
549			break;
550	}
551
552out:
553
554	return error;
555}
556
557/* --------------------------------------------------------------------- */
558
559static int
560tmpfs_mappedwrite(vm_object_t tobj, size_t len, struct uio *uio)
561{
562	vm_pindex_t	idx;
563	vm_page_t	tpg;
564	vm_offset_t	offset;
565	off_t		addr;
566	size_t		tlen;
567	int		error, rv;
568
569	error = 0;
570
571	addr = uio->uio_offset;
572	idx = OFF_TO_IDX(addr);
573	offset = addr & PAGE_MASK;
574	tlen = MIN(PAGE_SIZE - offset, len);
575
576	VM_OBJECT_WLOCK(tobj);
577	tpg = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_NOBUSY |
578	    VM_ALLOC_RETRY);
579	if (tpg->valid != VM_PAGE_BITS_ALL) {
580		vm_page_busy(tpg);
581		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
582			rv = vm_pager_get_pages(tobj, &tpg, 1, 0);
583			tpg = vm_page_lookup(tobj, idx);
584			if (tpg == NULL) {
585				printf(
586		    "tmpfs: vm_obj %p idx %jd null lookup rv %d\n",
587				    tobj, idx, rv);
588				VM_OBJECT_WUNLOCK(tobj);
589				return (EIO);
590			}
591			if (rv != VM_PAGER_OK) {
592				printf(
593		    "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n",
594				    tobj, idx, tpg->valid, rv);
595				vm_page_lock(tpg);
596				vm_page_free(tpg);
597				vm_page_unlock(tpg);
598				VM_OBJECT_WUNLOCK(tobj);
599				return (EIO);
600			}
601		} else
602			vm_page_zero_invalid(tpg, TRUE);
603		vm_page_wakeup(tpg);
604	}
605	vm_page_io_start(tpg);
606	VM_OBJECT_WUNLOCK(tobj);
607	error = uiomove_fromphys(&tpg, offset, tlen, uio);
608	VM_OBJECT_WLOCK(tobj);
609	vm_page_io_finish(tpg);
610	if (error == 0)
611		vm_page_dirty(tpg);
612	vm_page_lock(tpg);
613	if (tpg->queue == PQ_NONE) {
614		vm_page_deactivate(tpg);
615	} else {
616		/* Requeue to maintain LRU ordering. */
617		vm_page_requeue(tpg);
618	}
619	vm_page_unlock(tpg);
620	VM_OBJECT_WUNLOCK(tobj);
621
622	return	(error);
623}
624
625static int
626tmpfs_write(struct vop_write_args *v)
627{
628	struct vnode *vp = v->a_vp;
629	struct uio *uio = v->a_uio;
630	int ioflag = v->a_ioflag;
631
632	boolean_t extended;
633	int error = 0;
634	off_t oldsize;
635	struct tmpfs_node *node;
636	vm_object_t uobj;
637	size_t len;
638	int resid;
639
640	node = VP_TO_TMPFS_NODE(vp);
641	oldsize = node->tn_size;
642
643	if (uio->uio_offset < 0 || vp->v_type != VREG) {
644		error = EINVAL;
645		goto out;
646	}
647
648	if (uio->uio_resid == 0) {
649		error = 0;
650		goto out;
651	}
652
653	if (ioflag & IO_APPEND)
654		uio->uio_offset = node->tn_size;
655
656	if (uio->uio_offset + uio->uio_resid >
657	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
658		return (EFBIG);
659
660	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
661		return (EFBIG);
662
663	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
664	if (extended) {
665		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
666		    FALSE);
667		if (error != 0)
668			goto out;
669	}
670
671	uobj = node->tn_reg.tn_aobj;
672	while ((resid = uio->uio_resid) > 0) {
673		if (node->tn_size <= uio->uio_offset)
674			break;
675		len = MIN(node->tn_size - uio->uio_offset, resid);
676		if (len == 0)
677			break;
678		error = tmpfs_mappedwrite(uobj, len, uio);
679		if ((error != 0) || (resid == uio->uio_resid))
680			break;
681	}
682
683	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
684	    (extended ? TMPFS_NODE_CHANGED : 0);
685
686	if (node->tn_mode & (S_ISUID | S_ISGID)) {
687		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
688			node->tn_mode &= ~(S_ISUID | S_ISGID);
689	}
690
691	if (error != 0)
692		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
693
694out:
695	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
696	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
697
698	return error;
699}
700
701/* --------------------------------------------------------------------- */
702
703static int
704tmpfs_fsync(struct vop_fsync_args *v)
705{
706	struct vnode *vp = v->a_vp;
707
708	MPASS(VOP_ISLOCKED(vp));
709
710	tmpfs_update(vp);
711
712	return 0;
713}
714
715/* --------------------------------------------------------------------- */
716
717static int
718tmpfs_remove(struct vop_remove_args *v)
719{
720	struct vnode *dvp = v->a_dvp;
721	struct vnode *vp = v->a_vp;
722
723	int error;
724	struct tmpfs_dirent *de;
725	struct tmpfs_mount *tmp;
726	struct tmpfs_node *dnode;
727	struct tmpfs_node *node;
728
729	MPASS(VOP_ISLOCKED(dvp));
730	MPASS(VOP_ISLOCKED(vp));
731
732	if (vp->v_type == VDIR) {
733		error = EISDIR;
734		goto out;
735	}
736
737	dnode = VP_TO_TMPFS_DIR(dvp);
738	node = VP_TO_TMPFS_NODE(vp);
739	tmp = VFS_TO_TMPFS(vp->v_mount);
740	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
741	MPASS(de != NULL);
742
743	/* Files marked as immutable or append-only cannot be deleted. */
744	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
745	    (dnode->tn_flags & APPEND)) {
746		error = EPERM;
747		goto out;
748	}
749
750	/* Remove the entry from the directory; as it is a file, we do not
751	 * have to change the number of hard links of the directory. */
752	tmpfs_dir_detach(dvp, de);
753	if (v->a_cnp->cn_flags & DOWHITEOUT)
754		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
755
756	/* Free the directory entry we just deleted.  Note that the node
757	 * referred by it will not be removed until the vnode is really
758	 * reclaimed. */
759	tmpfs_free_dirent(tmp, de);
760
761	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
762	error = 0;
763
764out:
765
766	return error;
767}
768
769/* --------------------------------------------------------------------- */
770
771static int
772tmpfs_link(struct vop_link_args *v)
773{
774	struct vnode *dvp = v->a_tdvp;
775	struct vnode *vp = v->a_vp;
776	struct componentname *cnp = v->a_cnp;
777
778	int error;
779	struct tmpfs_dirent *de;
780	struct tmpfs_node *node;
781
782	MPASS(VOP_ISLOCKED(dvp));
783	MPASS(cnp->cn_flags & HASBUF);
784	MPASS(dvp != vp); /* XXX When can this be false? */
785
786	node = VP_TO_TMPFS_NODE(vp);
787
788	/* XXX: Why aren't the following two tests done by the caller? */
789
790	/* Hard links of directories are forbidden. */
791	if (vp->v_type == VDIR) {
792		error = EPERM;
793		goto out;
794	}
795
796	/* Cannot create cross-device links. */
797	if (dvp->v_mount != vp->v_mount) {
798		error = EXDEV;
799		goto out;
800	}
801
802	/* Ensure that we do not overflow the maximum number of links imposed
803	 * by the system. */
804	MPASS(node->tn_links <= LINK_MAX);
805	if (node->tn_links == LINK_MAX) {
806		error = EMLINK;
807		goto out;
808	}
809
810	/* We cannot create links of files marked immutable or append-only. */
811	if (node->tn_flags & (IMMUTABLE | APPEND)) {
812		error = EPERM;
813		goto out;
814	}
815
816	/* Allocate a new directory entry to represent the node. */
817	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
818	    cnp->cn_nameptr, cnp->cn_namelen, &de);
819	if (error != 0)
820		goto out;
821
822	/* Insert the new directory entry into the appropriate directory. */
823	if (cnp->cn_flags & ISWHITEOUT)
824		tmpfs_dir_whiteout_remove(dvp, cnp);
825	tmpfs_dir_attach(dvp, de);
826
827	/* vp link count has changed, so update node times. */
828	node->tn_status |= TMPFS_NODE_CHANGED;
829	tmpfs_update(vp);
830
831	error = 0;
832
833out:
834	return error;
835}
836
837/* --------------------------------------------------------------------- */
838
839/*
840 * We acquire all but fdvp locks using non-blocking acquisitions.  If we
841 * fail to acquire any lock in the path we will drop all held locks,
842 * acquire the new lock in a blocking fashion, and then release it and
843 * restart the rename.  This acquire/release step ensures that we do not
844 * spin on a lock waiting for release.  On error release all vnode locks
845 * and decrement references the way tmpfs_rename() would do.
846 */
847static int
848tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
849    struct vnode *tdvp, struct vnode **tvpp,
850    struct componentname *fcnp, struct componentname *tcnp)
851{
852	struct vnode *nvp;
853	struct mount *mp;
854	struct tmpfs_dirent *de;
855	int error, restarts = 0;
856
857	VOP_UNLOCK(tdvp, 0);
858	if (*tvpp != NULL && *tvpp != tdvp)
859		VOP_UNLOCK(*tvpp, 0);
860	mp = fdvp->v_mount;
861
862relock:
863	restarts += 1;
864	error = vn_lock(fdvp, LK_EXCLUSIVE);
865	if (error)
866		goto releout;
867	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
868		VOP_UNLOCK(fdvp, 0);
869		error = vn_lock(tdvp, LK_EXCLUSIVE);
870		if (error)
871			goto releout;
872		VOP_UNLOCK(tdvp, 0);
873		goto relock;
874	}
875	/*
876	 * Re-resolve fvp to be certain it still exists and fetch the
877	 * correct vnode.
878	 */
879	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
880	if (de == NULL) {
881		VOP_UNLOCK(fdvp, 0);
882		VOP_UNLOCK(tdvp, 0);
883		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
884		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
885			error = EINVAL;
886		else
887			error = ENOENT;
888		goto releout;
889	}
890	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
891	if (error != 0) {
892		VOP_UNLOCK(fdvp, 0);
893		VOP_UNLOCK(tdvp, 0);
894		if (error != EBUSY)
895			goto releout;
896		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
897		if (error != 0)
898			goto releout;
899		VOP_UNLOCK(nvp, 0);
900		/*
901		 * Concurrent rename race.
902		 */
903		if (nvp == tdvp) {
904			vrele(nvp);
905			error = EINVAL;
906			goto releout;
907		}
908		vrele(*fvpp);
909		*fvpp = nvp;
910		goto relock;
911	}
912	vrele(*fvpp);
913	*fvpp = nvp;
914	VOP_UNLOCK(*fvpp, 0);
915	/*
916	 * Re-resolve tvp and acquire the vnode lock if present.
917	 */
918	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
919	/*
920	 * If tvp disappeared we just carry on.
921	 */
922	if (de == NULL && *tvpp != NULL) {
923		vrele(*tvpp);
924		*tvpp = NULL;
925	}
926	/*
927	 * Get the tvp ino if the lookup succeeded.  We may have to restart
928	 * if the non-blocking acquire fails.
929	 */
930	if (de != NULL) {
931		nvp = NULL;
932		error = tmpfs_alloc_vp(mp, de->td_node,
933		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
934		if (*tvpp != NULL)
935			vrele(*tvpp);
936		*tvpp = nvp;
937		if (error != 0) {
938			VOP_UNLOCK(fdvp, 0);
939			VOP_UNLOCK(tdvp, 0);
940			if (error != EBUSY)
941				goto releout;
942			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
943			    &nvp);
944			if (error != 0)
945				goto releout;
946			VOP_UNLOCK(nvp, 0);
947			/*
948			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
949			 */
950			if (nvp == fdvp) {
951				error = ENOTEMPTY;
952				goto releout;
953			}
954			goto relock;
955		}
956	}
957	tmpfs_rename_restarts += restarts;
958
959	return (0);
960
961releout:
962	vrele(fdvp);
963	vrele(*fvpp);
964	vrele(tdvp);
965	if (*tvpp != NULL)
966		vrele(*tvpp);
967	tmpfs_rename_restarts += restarts;
968
969	return (error);
970}
971
972static int
973tmpfs_rename(struct vop_rename_args *v)
974{
975	struct vnode *fdvp = v->a_fdvp;
976	struct vnode *fvp = v->a_fvp;
977	struct componentname *fcnp = v->a_fcnp;
978	struct vnode *tdvp = v->a_tdvp;
979	struct vnode *tvp = v->a_tvp;
980	struct componentname *tcnp = v->a_tcnp;
981	struct mount *mp = NULL;
982
983	char *newname;
984	int error;
985	struct tmpfs_dirent *de;
986	struct tmpfs_mount *tmp;
987	struct tmpfs_node *fdnode;
988	struct tmpfs_node *fnode;
989	struct tmpfs_node *tnode;
990	struct tmpfs_node *tdnode;
991
992	MPASS(VOP_ISLOCKED(tdvp));
993	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
994	MPASS(fcnp->cn_flags & HASBUF);
995	MPASS(tcnp->cn_flags & HASBUF);
996
997	/* Disallow cross-device renames.
998	 * XXX Why isn't this done by the caller? */
999	if (fvp->v_mount != tdvp->v_mount ||
1000	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1001		error = EXDEV;
1002		goto out;
1003	}
1004
1005	/* If source and target are the same file, there is nothing to do. */
1006	if (fvp == tvp) {
1007		error = 0;
1008		goto out;
1009	}
1010
1011	/* If we need to move the directory between entries, lock the
1012	 * source so that we can safely operate on it. */
1013	if (fdvp != tdvp && fdvp != tvp) {
1014		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1015			mp = tdvp->v_mount;
1016			error = vfs_busy(mp, 0);
1017			if (error != 0) {
1018				mp = NULL;
1019				goto out;
1020			}
1021			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1022			    fcnp, tcnp);
1023			if (error != 0) {
1024				vfs_unbusy(mp);
1025				return (error);
1026			}
1027			ASSERT_VOP_ELOCKED(fdvp,
1028			    "tmpfs_rename: fdvp not locked");
1029			ASSERT_VOP_ELOCKED(tdvp,
1030			    "tmpfs_rename: tdvp not locked");
1031			if (tvp != NULL)
1032				ASSERT_VOP_ELOCKED(tvp,
1033				    "tmpfs_rename: tvp not locked");
1034			if (fvp == tvp) {
1035				error = 0;
1036				goto out_locked;
1037			}
1038		}
1039	}
1040
1041	tmp = VFS_TO_TMPFS(tdvp->v_mount);
1042	tdnode = VP_TO_TMPFS_DIR(tdvp);
1043	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1044	fdnode = VP_TO_TMPFS_DIR(fdvp);
1045	fnode = VP_TO_TMPFS_NODE(fvp);
1046	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1047
1048	/* Entry can disappear before we lock fdvp,
1049	 * also avoid manipulating '.' and '..' entries. */
1050	if (de == NULL) {
1051		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1052		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1053			error = EINVAL;
1054		else
1055			error = ENOENT;
1056		goto out_locked;
1057	}
1058	MPASS(de->td_node == fnode);
1059
1060	/* If re-naming a directory to another preexisting directory
1061	 * ensure that the target directory is empty so that its
1062	 * removal causes no side effects.
1063	 * Kern_rename gurantees the destination to be a directory
1064	 * if the source is one. */
1065	if (tvp != NULL) {
1066		MPASS(tnode != NULL);
1067
1068		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1069		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1070			error = EPERM;
1071			goto out_locked;
1072		}
1073
1074		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1075			if (tnode->tn_size > 0) {
1076				error = ENOTEMPTY;
1077				goto out_locked;
1078			}
1079		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1080			error = ENOTDIR;
1081			goto out_locked;
1082		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1083			error = EISDIR;
1084			goto out_locked;
1085		} else {
1086			MPASS(fnode->tn_type != VDIR &&
1087				tnode->tn_type != VDIR);
1088		}
1089	}
1090
1091	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1092	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1093		error = EPERM;
1094		goto out_locked;
1095	}
1096
1097	/* Ensure that we have enough memory to hold the new name, if it
1098	 * has to be changed. */
1099	if (fcnp->cn_namelen != tcnp->cn_namelen ||
1100	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1101		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1102	} else
1103		newname = NULL;
1104
1105	/* If the node is being moved to another directory, we have to do
1106	 * the move. */
1107	if (fdnode != tdnode) {
1108		/* In case we are moving a directory, we have to adjust its
1109		 * parent to point to the new parent. */
1110		if (de->td_node->tn_type == VDIR) {
1111			struct tmpfs_node *n;
1112
1113			/* Ensure the target directory is not a child of the
1114			 * directory being moved.  Otherwise, we'd end up
1115			 * with stale nodes. */
1116			n = tdnode;
1117			/* TMPFS_LOCK garanties that no nodes are freed while
1118			 * traversing the list. Nodes can only be marked as
1119			 * removed: tn_parent == NULL. */
1120			TMPFS_LOCK(tmp);
1121			TMPFS_NODE_LOCK(n);
1122			while (n != n->tn_dir.tn_parent) {
1123				struct tmpfs_node *parent;
1124
1125				if (n == fnode) {
1126					TMPFS_NODE_UNLOCK(n);
1127					TMPFS_UNLOCK(tmp);
1128					error = EINVAL;
1129					if (newname != NULL)
1130						    free(newname, M_TMPFSNAME);
1131					goto out_locked;
1132				}
1133				parent = n->tn_dir.tn_parent;
1134				TMPFS_NODE_UNLOCK(n);
1135				if (parent == NULL) {
1136					n = NULL;
1137					break;
1138				}
1139				TMPFS_NODE_LOCK(parent);
1140				if (parent->tn_dir.tn_parent == NULL) {
1141					TMPFS_NODE_UNLOCK(parent);
1142					n = NULL;
1143					break;
1144				}
1145				n = parent;
1146			}
1147			TMPFS_UNLOCK(tmp);
1148			if (n == NULL) {
1149				error = EINVAL;
1150				if (newname != NULL)
1151					    free(newname, M_TMPFSNAME);
1152				goto out_locked;
1153			}
1154			TMPFS_NODE_UNLOCK(n);
1155
1156			/* Adjust the parent pointer. */
1157			TMPFS_VALIDATE_DIR(fnode);
1158			TMPFS_NODE_LOCK(de->td_node);
1159			de->td_node->tn_dir.tn_parent = tdnode;
1160			TMPFS_NODE_UNLOCK(de->td_node);
1161
1162			/* As a result of changing the target of the '..'
1163			 * entry, the link count of the source and target
1164			 * directories has to be adjusted. */
1165			TMPFS_NODE_LOCK(tdnode);
1166			TMPFS_ASSERT_LOCKED(tdnode);
1167			tdnode->tn_links++;
1168			TMPFS_NODE_UNLOCK(tdnode);
1169
1170			TMPFS_NODE_LOCK(fdnode);
1171			TMPFS_ASSERT_LOCKED(fdnode);
1172			fdnode->tn_links--;
1173			TMPFS_NODE_UNLOCK(fdnode);
1174		}
1175	}
1176
1177	/* Do the move: just remove the entry from the source directory
1178	 * and insert it into the target one. */
1179	tmpfs_dir_detach(fdvp, de);
1180
1181	if (fcnp->cn_flags & DOWHITEOUT)
1182		tmpfs_dir_whiteout_add(fdvp, fcnp);
1183	if (tcnp->cn_flags & ISWHITEOUT)
1184		tmpfs_dir_whiteout_remove(tdvp, tcnp);
1185
1186	/* If the name has changed, we need to make it effective by changing
1187	 * it in the directory entry. */
1188	if (newname != NULL) {
1189		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1190
1191		free(de->ud.td_name, M_TMPFSNAME);
1192		de->ud.td_name = newname;
1193		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1194
1195		fnode->tn_status |= TMPFS_NODE_CHANGED;
1196		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1197	}
1198
1199	/* If we are overwriting an entry, we have to remove the old one
1200	 * from the target directory. */
1201	if (tvp != NULL) {
1202		struct tmpfs_dirent *tde;
1203
1204		/* Remove the old entry from the target directory. */
1205		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1206		tmpfs_dir_detach(tdvp, tde);
1207
1208		/* Free the directory entry we just deleted.  Note that the
1209		 * node referred by it will not be removed until the vnode is
1210		 * really reclaimed. */
1211		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1212	}
1213
1214	tmpfs_dir_attach(tdvp, de);
1215
1216	cache_purge(fvp);
1217	if (tvp != NULL)
1218		cache_purge(tvp);
1219	cache_purge_negative(tdvp);
1220
1221	error = 0;
1222
1223out_locked:
1224	if (fdvp != tdvp && fdvp != tvp)
1225		VOP_UNLOCK(fdvp, 0);
1226
1227out:
1228	/* Release target nodes. */
1229	/* XXX: I don't understand when tdvp can be the same as tvp, but
1230	 * other code takes care of this... */
1231	if (tdvp == tvp)
1232		vrele(tdvp);
1233	else
1234		vput(tdvp);
1235	if (tvp != NULL)
1236		vput(tvp);
1237
1238	/* Release source nodes. */
1239	vrele(fdvp);
1240	vrele(fvp);
1241
1242	if (mp != NULL)
1243		vfs_unbusy(mp);
1244
1245	return error;
1246}
1247
1248/* --------------------------------------------------------------------- */
1249
1250static int
1251tmpfs_mkdir(struct vop_mkdir_args *v)
1252{
1253	struct vnode *dvp = v->a_dvp;
1254	struct vnode **vpp = v->a_vpp;
1255	struct componentname *cnp = v->a_cnp;
1256	struct vattr *vap = v->a_vap;
1257
1258	MPASS(vap->va_type == VDIR);
1259
1260	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1261}
1262
1263/* --------------------------------------------------------------------- */
1264
1265static int
1266tmpfs_rmdir(struct vop_rmdir_args *v)
1267{
1268	struct vnode *dvp = v->a_dvp;
1269	struct vnode *vp = v->a_vp;
1270
1271	int error;
1272	struct tmpfs_dirent *de;
1273	struct tmpfs_mount *tmp;
1274	struct tmpfs_node *dnode;
1275	struct tmpfs_node *node;
1276
1277	MPASS(VOP_ISLOCKED(dvp));
1278	MPASS(VOP_ISLOCKED(vp));
1279
1280	tmp = VFS_TO_TMPFS(dvp->v_mount);
1281	dnode = VP_TO_TMPFS_DIR(dvp);
1282	node = VP_TO_TMPFS_DIR(vp);
1283
1284	/* Directories with more than two entries ('.' and '..') cannot be
1285	 * removed. */
1286	 if (node->tn_size > 0) {
1287		 error = ENOTEMPTY;
1288		 goto out;
1289	 }
1290
1291	if ((dnode->tn_flags & APPEND)
1292	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1293		error = EPERM;
1294		goto out;
1295	}
1296
1297	/* This invariant holds only if we are not trying to remove "..".
1298	  * We checked for that above so this is safe now. */
1299	MPASS(node->tn_dir.tn_parent == dnode);
1300
1301	/* Get the directory entry associated with node (vp).  This was
1302	 * filled by tmpfs_lookup while looking up the entry. */
1303	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1304	MPASS(TMPFS_DIRENT_MATCHES(de,
1305	    v->a_cnp->cn_nameptr,
1306	    v->a_cnp->cn_namelen));
1307
1308	/* Check flags to see if we are allowed to remove the directory. */
1309	if (dnode->tn_flags & APPEND
1310		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1311		error = EPERM;
1312		goto out;
1313	}
1314
1315
1316	/* Detach the directory entry from the directory (dnode). */
1317	tmpfs_dir_detach(dvp, de);
1318	if (v->a_cnp->cn_flags & DOWHITEOUT)
1319		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1320
1321	/* No vnode should be allocated for this entry from this point */
1322	TMPFS_NODE_LOCK(node);
1323	TMPFS_ASSERT_ELOCKED(node);
1324	node->tn_links--;
1325	node->tn_dir.tn_parent = NULL;
1326	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1327	    TMPFS_NODE_MODIFIED;
1328
1329	TMPFS_NODE_UNLOCK(node);
1330
1331	TMPFS_NODE_LOCK(dnode);
1332	TMPFS_ASSERT_ELOCKED(dnode);
1333	dnode->tn_links--;
1334	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1335	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1336	TMPFS_NODE_UNLOCK(dnode);
1337
1338	cache_purge(dvp);
1339	cache_purge(vp);
1340
1341	/* Free the directory entry we just deleted.  Note that the node
1342	 * referred by it will not be removed until the vnode is really
1343	 * reclaimed. */
1344	tmpfs_free_dirent(tmp, de);
1345
1346	/* Release the deleted vnode (will destroy the node, notify
1347	 * interested parties and clean it from the cache). */
1348
1349	dnode->tn_status |= TMPFS_NODE_CHANGED;
1350	tmpfs_update(dvp);
1351
1352	error = 0;
1353
1354out:
1355	return error;
1356}
1357
1358/* --------------------------------------------------------------------- */
1359
1360static int
1361tmpfs_symlink(struct vop_symlink_args *v)
1362{
1363	struct vnode *dvp = v->a_dvp;
1364	struct vnode **vpp = v->a_vpp;
1365	struct componentname *cnp = v->a_cnp;
1366	struct vattr *vap = v->a_vap;
1367	char *target = v->a_target;
1368
1369#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1370	MPASS(vap->va_type == VLNK);
1371#else
1372	vap->va_type = VLNK;
1373#endif
1374
1375	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1376}
1377
1378/* --------------------------------------------------------------------- */
1379
1380static int
1381tmpfs_readdir(struct vop_readdir_args *v)
1382{
1383	struct vnode *vp = v->a_vp;
1384	struct uio *uio = v->a_uio;
1385	int *eofflag = v->a_eofflag;
1386	u_long **cookies = v->a_cookies;
1387	int *ncookies = v->a_ncookies;
1388
1389	int error;
1390	ssize_t startresid;
1391	int cnt = 0;
1392	struct tmpfs_node *node;
1393
1394	/* This operation only makes sense on directory nodes. */
1395	if (vp->v_type != VDIR)
1396		return ENOTDIR;
1397
1398	node = VP_TO_TMPFS_DIR(vp);
1399
1400	startresid = uio->uio_resid;
1401
1402	if (cookies != NULL && ncookies != NULL) {
1403		cnt = howmany(node->tn_size, sizeof(struct tmpfs_dirent)) + 2;
1404		*cookies = malloc(cnt * sizeof(**cookies), M_TEMP, M_WAITOK);
1405		*ncookies = 0;
1406	}
1407
1408	if (cnt == 0)
1409		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1410	else
1411		error = tmpfs_dir_getdents(node, uio, cnt, *cookies, ncookies);
1412
1413	if (error == EJUSTRETURN)
1414		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1415
1416	if (error != 0 && cnt != 0)
1417		free(*cookies, M_TEMP);
1418
1419	if (eofflag != NULL)
1420		*eofflag =
1421		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1422
1423	return error;
1424}
1425
1426/* --------------------------------------------------------------------- */
1427
1428static int
1429tmpfs_readlink(struct vop_readlink_args *v)
1430{
1431	struct vnode *vp = v->a_vp;
1432	struct uio *uio = v->a_uio;
1433
1434	int error;
1435	struct tmpfs_node *node;
1436
1437	MPASS(uio->uio_offset == 0);
1438	MPASS(vp->v_type == VLNK);
1439
1440	node = VP_TO_TMPFS_NODE(vp);
1441
1442	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1443	    uio);
1444	node->tn_status |= TMPFS_NODE_ACCESSED;
1445
1446	return error;
1447}
1448
1449/* --------------------------------------------------------------------- */
1450
1451static int
1452tmpfs_inactive(struct vop_inactive_args *v)
1453{
1454	struct vnode *vp = v->a_vp;
1455
1456	struct tmpfs_node *node;
1457
1458	node = VP_TO_TMPFS_NODE(vp);
1459
1460	if (node->tn_links == 0)
1461		vrecycle(vp);
1462
1463	return 0;
1464}
1465
1466/* --------------------------------------------------------------------- */
1467
1468int
1469tmpfs_reclaim(struct vop_reclaim_args *v)
1470{
1471	struct vnode *vp = v->a_vp;
1472
1473	struct tmpfs_mount *tmp;
1474	struct tmpfs_node *node;
1475
1476	node = VP_TO_TMPFS_NODE(vp);
1477	tmp = VFS_TO_TMPFS(vp->v_mount);
1478
1479	if (vp->v_type == VREG)
1480		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1481	else
1482		vnode_destroy_vobject(vp);
1483	vp->v_object = NULL;
1484	cache_purge(vp);
1485
1486	TMPFS_NODE_LOCK(node);
1487	TMPFS_ASSERT_ELOCKED(node);
1488	tmpfs_free_vp(vp);
1489
1490	/* If the node referenced by this vnode was deleted by the user,
1491	 * we must free its associated data structures (now that the vnode
1492	 * is being reclaimed). */
1493	if (node->tn_links == 0 &&
1494	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1495		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1496		TMPFS_NODE_UNLOCK(node);
1497		tmpfs_free_node(tmp, node);
1498	} else
1499		TMPFS_NODE_UNLOCK(node);
1500
1501	MPASS(vp->v_data == NULL);
1502	return 0;
1503}
1504
1505/* --------------------------------------------------------------------- */
1506
1507static int
1508tmpfs_print(struct vop_print_args *v)
1509{
1510	struct vnode *vp = v->a_vp;
1511
1512	struct tmpfs_node *node;
1513
1514	node = VP_TO_TMPFS_NODE(vp);
1515
1516	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1517	    node, node->tn_flags, node->tn_links);
1518	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1519	    node->tn_mode, node->tn_uid, node->tn_gid,
1520	    (intmax_t)node->tn_size, node->tn_status);
1521
1522	if (vp->v_type == VFIFO)
1523		fifo_printinfo(vp);
1524
1525	printf("\n");
1526
1527	return 0;
1528}
1529
1530/* --------------------------------------------------------------------- */
1531
1532static int
1533tmpfs_pathconf(struct vop_pathconf_args *v)
1534{
1535	int name = v->a_name;
1536	register_t *retval = v->a_retval;
1537
1538	int error;
1539
1540	error = 0;
1541
1542	switch (name) {
1543	case _PC_LINK_MAX:
1544		*retval = LINK_MAX;
1545		break;
1546
1547	case _PC_NAME_MAX:
1548		*retval = NAME_MAX;
1549		break;
1550
1551	case _PC_PATH_MAX:
1552		*retval = PATH_MAX;
1553		break;
1554
1555	case _PC_PIPE_BUF:
1556		*retval = PIPE_BUF;
1557		break;
1558
1559	case _PC_CHOWN_RESTRICTED:
1560		*retval = 1;
1561		break;
1562
1563	case _PC_NO_TRUNC:
1564		*retval = 1;
1565		break;
1566
1567	case _PC_SYNC_IO:
1568		*retval = 1;
1569		break;
1570
1571	case _PC_FILESIZEBITS:
1572		*retval = 0; /* XXX Don't know which value should I return. */
1573		break;
1574
1575	default:
1576		error = EINVAL;
1577	}
1578
1579	return error;
1580}
1581
1582static int
1583tmpfs_vptofh(struct vop_vptofh_args *ap)
1584{
1585	struct tmpfs_fid *tfhp;
1586	struct tmpfs_node *node;
1587
1588	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1589	node = VP_TO_TMPFS_NODE(ap->a_vp);
1590
1591	tfhp->tf_len = sizeof(struct tmpfs_fid);
1592	tfhp->tf_id = node->tn_id;
1593	tfhp->tf_gen = node->tn_gen;
1594
1595	return (0);
1596}
1597
1598static int
1599tmpfs_whiteout(struct vop_whiteout_args *ap)
1600{
1601	struct vnode *dvp = ap->a_dvp;
1602	struct componentname *cnp = ap->a_cnp;
1603	struct tmpfs_dirent *de;
1604
1605	switch (ap->a_flags) {
1606	case LOOKUP:
1607		return (0);
1608	case CREATE:
1609		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1610		if (de != NULL)
1611			return (de->td_node == NULL ? 0 : EEXIST);
1612		return (tmpfs_dir_whiteout_add(dvp, cnp));
1613	case DELETE:
1614		tmpfs_dir_whiteout_remove(dvp, cnp);
1615		return (0);
1616	default:
1617		panic("tmpfs_whiteout: unknown op");
1618	}
1619}
1620
1621/* --------------------------------------------------------------------- */
1622
1623/*
1624 * vnode operations vector used for files stored in a tmpfs file system.
1625 */
1626struct vop_vector tmpfs_vnodeop_entries = {
1627	.vop_default =			&default_vnodeops,
1628	.vop_lookup =			vfs_cache_lookup,
1629	.vop_cachedlookup =		tmpfs_lookup,
1630	.vop_create =			tmpfs_create,
1631	.vop_mknod =			tmpfs_mknod,
1632	.vop_open =			tmpfs_open,
1633	.vop_close =			tmpfs_close,
1634	.vop_access =			tmpfs_access,
1635	.vop_getattr =			tmpfs_getattr,
1636	.vop_setattr =			tmpfs_setattr,
1637	.vop_read =			tmpfs_read,
1638	.vop_write =			tmpfs_write,
1639	.vop_fsync =			tmpfs_fsync,
1640	.vop_remove =			tmpfs_remove,
1641	.vop_link =			tmpfs_link,
1642	.vop_rename =			tmpfs_rename,
1643	.vop_mkdir =			tmpfs_mkdir,
1644	.vop_rmdir =			tmpfs_rmdir,
1645	.vop_symlink =			tmpfs_symlink,
1646	.vop_readdir =			tmpfs_readdir,
1647	.vop_readlink =			tmpfs_readlink,
1648	.vop_inactive =			tmpfs_inactive,
1649	.vop_reclaim =			tmpfs_reclaim,
1650	.vop_print =			tmpfs_print,
1651	.vop_pathconf =			tmpfs_pathconf,
1652	.vop_vptofh =			tmpfs_vptofh,
1653	.vop_whiteout =			tmpfs_whiteout,
1654	.vop_bmap =			VOP_EOPNOTSUPP,
1655};
1656
1657