tmpfs_vnops.c revision 250310
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 250310 2013-05-06 21:04:42Z kib $");
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	 * The kern_sendfile() code calls vn_rdwr() with the page
452	 * soft-busied.  Ignore the soft-busy state here. Parallel
453	 * reads of the page content from disk are prevented by
454	 * VPO_BUSY.
455	 *
456	 * Although the tmpfs vnode lock is held here, it is
457	 * nonetheless safe to sleep waiting for a free page.  The
458	 * pageout daemon does not need to acquire the tmpfs vnode
459	 * lock to page out tobj's pages because tobj is a OBJT_SWAP
460	 * type object.
461	 */
462	m = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY |
463	    VM_ALLOC_IGN_SBUSY);
464	if (m->valid != VM_PAGE_BITS_ALL) {
465		if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
466			rv = vm_pager_get_pages(tobj, &m, 1, 0);
467			m = vm_page_lookup(tobj, idx);
468			if (m == NULL) {
469				printf(
470		    "tmpfs: vm_obj %p idx %jd null lookup rv %d\n",
471				    tobj, idx, rv);
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	}
487	vm_page_lock(m);
488	vm_page_hold(m);
489	vm_page_wakeup(m);
490	vm_page_unlock(m);
491	VM_OBJECT_WUNLOCK(tobj);
492	error = uiomove_fromphys(&m, offset, tlen, uio);
493	VM_OBJECT_WLOCK(tobj);
494	vm_page_lock(m);
495	vm_page_unhold(m);
496	if (m->queue == PQ_NONE) {
497		vm_page_deactivate(m);
498	} else {
499		/* Requeue to maintain LRU ordering. */
500		vm_page_requeue(m);
501	}
502	vm_page_unlock(m);
503	VM_OBJECT_WUNLOCK(tobj);
504
505	return (error);
506}
507
508static int
509tmpfs_read(struct vop_read_args *v)
510{
511	struct vnode *vp = v->a_vp;
512	struct uio *uio = v->a_uio;
513	struct tmpfs_node *node;
514	vm_object_t uobj;
515	size_t len;
516	int resid;
517	int error = 0;
518	vm_pindex_t	idx;
519	vm_offset_t	offset;
520	off_t		addr;
521	size_t		tlen;
522
523	node = VP_TO_TMPFS_NODE(vp);
524
525	if (vp->v_type != VREG) {
526		error = EISDIR;
527		goto out;
528	}
529
530	if (uio->uio_offset < 0) {
531		error = EINVAL;
532		goto out;
533	}
534
535	node->tn_status |= TMPFS_NODE_ACCESSED;
536
537	uobj = node->tn_reg.tn_aobj;
538	while ((resid = uio->uio_resid) > 0) {
539		error = 0;
540		if (node->tn_size <= uio->uio_offset)
541			break;
542		len = MIN(node->tn_size - uio->uio_offset, resid);
543		if (len == 0)
544			break;
545		addr = uio->uio_offset;
546		idx = OFF_TO_IDX(addr);
547		offset = addr & PAGE_MASK;
548		tlen = MIN(PAGE_SIZE - offset, len);
549		error = tmpfs_nocacheread(uobj, idx, offset, tlen, uio);
550		if ((error != 0) || (resid == uio->uio_resid))
551			break;
552	}
553
554out:
555
556	return error;
557}
558
559/* --------------------------------------------------------------------- */
560
561static int
562tmpfs_mappedwrite(vm_object_t tobj, size_t len, struct uio *uio)
563{
564	vm_pindex_t	idx;
565	vm_page_t	tpg;
566	vm_offset_t	offset;
567	off_t		addr;
568	size_t		tlen;
569	int		error, rv;
570
571	error = 0;
572
573	addr = uio->uio_offset;
574	idx = OFF_TO_IDX(addr);
575	offset = addr & PAGE_MASK;
576	tlen = MIN(PAGE_SIZE - offset, len);
577
578	VM_OBJECT_WLOCK(tobj);
579	tpg = vm_page_grab(tobj, idx, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
580	if (tpg->valid != VM_PAGE_BITS_ALL) {
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				return (EIO);
589			}
590			if (rv != VM_PAGER_OK) {
591				printf(
592		    "tmpfs: vm_obj %p idx %jd valid %x pager error %d\n",
593				    tobj, idx, tpg->valid, rv);
594				vm_page_lock(tpg);
595				vm_page_free(tpg);
596				vm_page_unlock(tpg);
597				VM_OBJECT_WUNLOCK(tobj);
598				return (EIO);
599			}
600		} else
601			vm_page_zero_invalid(tpg, TRUE);
602	}
603	vm_page_lock(tpg);
604	vm_page_hold(tpg);
605	vm_page_wakeup(tpg);
606	vm_page_unlock(tpg);
607	VM_OBJECT_WUNLOCK(tobj);
608	error = uiomove_fromphys(&tpg, offset, tlen, uio);
609	VM_OBJECT_WLOCK(tobj);
610	if (error == 0)
611		vm_page_dirty(tpg);
612	vm_page_lock(tpg);
613	vm_page_unhold(tpg);
614	if (tpg->queue == PQ_NONE) {
615		vm_page_deactivate(tpg);
616	} else {
617		/* Requeue to maintain LRU ordering. */
618		vm_page_requeue(tpg);
619	}
620	vm_page_unlock(tpg);
621	VM_OBJECT_WUNLOCK(tobj);
622
623	return	(error);
624}
625
626static int
627tmpfs_write(struct vop_write_args *v)
628{
629	struct vnode *vp = v->a_vp;
630	struct uio *uio = v->a_uio;
631	int ioflag = v->a_ioflag;
632
633	boolean_t extended;
634	int error = 0;
635	off_t oldsize;
636	struct tmpfs_node *node;
637	vm_object_t uobj;
638	size_t len;
639	int resid;
640
641	node = VP_TO_TMPFS_NODE(vp);
642	oldsize = node->tn_size;
643
644	if (uio->uio_offset < 0 || vp->v_type != VREG) {
645		error = EINVAL;
646		goto out;
647	}
648
649	if (uio->uio_resid == 0) {
650		error = 0;
651		goto out;
652	}
653
654	if (ioflag & IO_APPEND)
655		uio->uio_offset = node->tn_size;
656
657	if (uio->uio_offset + uio->uio_resid >
658	  VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
659		return (EFBIG);
660
661	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
662		return (EFBIG);
663
664	extended = uio->uio_offset + uio->uio_resid > node->tn_size;
665	if (extended) {
666		error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
667		    FALSE);
668		if (error != 0)
669			goto out;
670	}
671
672	uobj = node->tn_reg.tn_aobj;
673	while ((resid = uio->uio_resid) > 0) {
674		if (node->tn_size <= uio->uio_offset)
675			break;
676		len = MIN(node->tn_size - uio->uio_offset, resid);
677		if (len == 0)
678			break;
679		error = tmpfs_mappedwrite(uobj, len, uio);
680		if ((error != 0) || (resid == uio->uio_resid))
681			break;
682	}
683
684	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
685	    (extended ? TMPFS_NODE_CHANGED : 0);
686
687	if (node->tn_mode & (S_ISUID | S_ISGID)) {
688		if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
689			node->tn_mode &= ~(S_ISUID | S_ISGID);
690	}
691
692	if (error != 0)
693		(void)tmpfs_reg_resize(vp, oldsize, TRUE);
694
695out:
696	MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
697	MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
698
699	return error;
700}
701
702/* --------------------------------------------------------------------- */
703
704static int
705tmpfs_fsync(struct vop_fsync_args *v)
706{
707	struct vnode *vp = v->a_vp;
708
709	MPASS(VOP_ISLOCKED(vp));
710
711	tmpfs_update(vp);
712
713	return 0;
714}
715
716/* --------------------------------------------------------------------- */
717
718static int
719tmpfs_remove(struct vop_remove_args *v)
720{
721	struct vnode *dvp = v->a_dvp;
722	struct vnode *vp = v->a_vp;
723
724	int error;
725	struct tmpfs_dirent *de;
726	struct tmpfs_mount *tmp;
727	struct tmpfs_node *dnode;
728	struct tmpfs_node *node;
729
730	MPASS(VOP_ISLOCKED(dvp));
731	MPASS(VOP_ISLOCKED(vp));
732
733	if (vp->v_type == VDIR) {
734		error = EISDIR;
735		goto out;
736	}
737
738	dnode = VP_TO_TMPFS_DIR(dvp);
739	node = VP_TO_TMPFS_NODE(vp);
740	tmp = VFS_TO_TMPFS(vp->v_mount);
741	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
742	MPASS(de != NULL);
743
744	/* Files marked as immutable or append-only cannot be deleted. */
745	if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
746	    (dnode->tn_flags & APPEND)) {
747		error = EPERM;
748		goto out;
749	}
750
751	/* Remove the entry from the directory; as it is a file, we do not
752	 * have to change the number of hard links of the directory. */
753	tmpfs_dir_detach(dvp, de);
754	if (v->a_cnp->cn_flags & DOWHITEOUT)
755		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
756
757	/* Free the directory entry we just deleted.  Note that the node
758	 * referred by it will not be removed until the vnode is really
759	 * reclaimed. */
760	tmpfs_free_dirent(tmp, de);
761
762	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
763	error = 0;
764
765out:
766
767	return error;
768}
769
770/* --------------------------------------------------------------------- */
771
772static int
773tmpfs_link(struct vop_link_args *v)
774{
775	struct vnode *dvp = v->a_tdvp;
776	struct vnode *vp = v->a_vp;
777	struct componentname *cnp = v->a_cnp;
778
779	int error;
780	struct tmpfs_dirent *de;
781	struct tmpfs_node *node;
782
783	MPASS(VOP_ISLOCKED(dvp));
784	MPASS(cnp->cn_flags & HASBUF);
785	MPASS(dvp != vp); /* XXX When can this be false? */
786
787	node = VP_TO_TMPFS_NODE(vp);
788
789	/* XXX: Why aren't the following two tests done by the caller? */
790
791	/* Hard links of directories are forbidden. */
792	if (vp->v_type == VDIR) {
793		error = EPERM;
794		goto out;
795	}
796
797	/* Cannot create cross-device links. */
798	if (dvp->v_mount != vp->v_mount) {
799		error = EXDEV;
800		goto out;
801	}
802
803	/* Ensure that we do not overflow the maximum number of links imposed
804	 * by the system. */
805	MPASS(node->tn_links <= LINK_MAX);
806	if (node->tn_links == LINK_MAX) {
807		error = EMLINK;
808		goto out;
809	}
810
811	/* We cannot create links of files marked immutable or append-only. */
812	if (node->tn_flags & (IMMUTABLE | APPEND)) {
813		error = EPERM;
814		goto out;
815	}
816
817	/* Allocate a new directory entry to represent the node. */
818	error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
819	    cnp->cn_nameptr, cnp->cn_namelen, &de);
820	if (error != 0)
821		goto out;
822
823	/* Insert the new directory entry into the appropriate directory. */
824	if (cnp->cn_flags & ISWHITEOUT)
825		tmpfs_dir_whiteout_remove(dvp, cnp);
826	tmpfs_dir_attach(dvp, de);
827
828	/* vp link count has changed, so update node times. */
829	node->tn_status |= TMPFS_NODE_CHANGED;
830	tmpfs_update(vp);
831
832	error = 0;
833
834out:
835	return error;
836}
837
838/* --------------------------------------------------------------------- */
839
840/*
841 * We acquire all but fdvp locks using non-blocking acquisitions.  If we
842 * fail to acquire any lock in the path we will drop all held locks,
843 * acquire the new lock in a blocking fashion, and then release it and
844 * restart the rename.  This acquire/release step ensures that we do not
845 * spin on a lock waiting for release.  On error release all vnode locks
846 * and decrement references the way tmpfs_rename() would do.
847 */
848static int
849tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
850    struct vnode *tdvp, struct vnode **tvpp,
851    struct componentname *fcnp, struct componentname *tcnp)
852{
853	struct vnode *nvp;
854	struct mount *mp;
855	struct tmpfs_dirent *de;
856	int error, restarts = 0;
857
858	VOP_UNLOCK(tdvp, 0);
859	if (*tvpp != NULL && *tvpp != tdvp)
860		VOP_UNLOCK(*tvpp, 0);
861	mp = fdvp->v_mount;
862
863relock:
864	restarts += 1;
865	error = vn_lock(fdvp, LK_EXCLUSIVE);
866	if (error)
867		goto releout;
868	if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
869		VOP_UNLOCK(fdvp, 0);
870		error = vn_lock(tdvp, LK_EXCLUSIVE);
871		if (error)
872			goto releout;
873		VOP_UNLOCK(tdvp, 0);
874		goto relock;
875	}
876	/*
877	 * Re-resolve fvp to be certain it still exists and fetch the
878	 * correct vnode.
879	 */
880	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
881	if (de == NULL) {
882		VOP_UNLOCK(fdvp, 0);
883		VOP_UNLOCK(tdvp, 0);
884		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
885		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
886			error = EINVAL;
887		else
888			error = ENOENT;
889		goto releout;
890	}
891	error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
892	if (error != 0) {
893		VOP_UNLOCK(fdvp, 0);
894		VOP_UNLOCK(tdvp, 0);
895		if (error != EBUSY)
896			goto releout;
897		error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
898		if (error != 0)
899			goto releout;
900		VOP_UNLOCK(nvp, 0);
901		/*
902		 * Concurrent rename race.
903		 */
904		if (nvp == tdvp) {
905			vrele(nvp);
906			error = EINVAL;
907			goto releout;
908		}
909		vrele(*fvpp);
910		*fvpp = nvp;
911		goto relock;
912	}
913	vrele(*fvpp);
914	*fvpp = nvp;
915	VOP_UNLOCK(*fvpp, 0);
916	/*
917	 * Re-resolve tvp and acquire the vnode lock if present.
918	 */
919	de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
920	/*
921	 * If tvp disappeared we just carry on.
922	 */
923	if (de == NULL && *tvpp != NULL) {
924		vrele(*tvpp);
925		*tvpp = NULL;
926	}
927	/*
928	 * Get the tvp ino if the lookup succeeded.  We may have to restart
929	 * if the non-blocking acquire fails.
930	 */
931	if (de != NULL) {
932		nvp = NULL;
933		error = tmpfs_alloc_vp(mp, de->td_node,
934		    LK_EXCLUSIVE | LK_NOWAIT, &nvp);
935		if (*tvpp != NULL)
936			vrele(*tvpp);
937		*tvpp = nvp;
938		if (error != 0) {
939			VOP_UNLOCK(fdvp, 0);
940			VOP_UNLOCK(tdvp, 0);
941			if (error != EBUSY)
942				goto releout;
943			error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
944			    &nvp);
945			if (error != 0)
946				goto releout;
947			VOP_UNLOCK(nvp, 0);
948			/*
949			 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
950			 */
951			if (nvp == fdvp) {
952				error = ENOTEMPTY;
953				goto releout;
954			}
955			goto relock;
956		}
957	}
958	tmpfs_rename_restarts += restarts;
959
960	return (0);
961
962releout:
963	vrele(fdvp);
964	vrele(*fvpp);
965	vrele(tdvp);
966	if (*tvpp != NULL)
967		vrele(*tvpp);
968	tmpfs_rename_restarts += restarts;
969
970	return (error);
971}
972
973static int
974tmpfs_rename(struct vop_rename_args *v)
975{
976	struct vnode *fdvp = v->a_fdvp;
977	struct vnode *fvp = v->a_fvp;
978	struct componentname *fcnp = v->a_fcnp;
979	struct vnode *tdvp = v->a_tdvp;
980	struct vnode *tvp = v->a_tvp;
981	struct componentname *tcnp = v->a_tcnp;
982	struct mount *mp = NULL;
983
984	char *newname;
985	int error;
986	struct tmpfs_dirent *de;
987	struct tmpfs_mount *tmp;
988	struct tmpfs_node *fdnode;
989	struct tmpfs_node *fnode;
990	struct tmpfs_node *tnode;
991	struct tmpfs_node *tdnode;
992
993	MPASS(VOP_ISLOCKED(tdvp));
994	MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
995	MPASS(fcnp->cn_flags & HASBUF);
996	MPASS(tcnp->cn_flags & HASBUF);
997
998	/* Disallow cross-device renames.
999	 * XXX Why isn't this done by the caller? */
1000	if (fvp->v_mount != tdvp->v_mount ||
1001	    (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
1002		error = EXDEV;
1003		goto out;
1004	}
1005
1006	/* If source and target are the same file, there is nothing to do. */
1007	if (fvp == tvp) {
1008		error = 0;
1009		goto out;
1010	}
1011
1012	/* If we need to move the directory between entries, lock the
1013	 * source so that we can safely operate on it. */
1014	if (fdvp != tdvp && fdvp != tvp) {
1015		if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1016			mp = tdvp->v_mount;
1017			error = vfs_busy(mp, 0);
1018			if (error != 0) {
1019				mp = NULL;
1020				goto out;
1021			}
1022			error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
1023			    fcnp, tcnp);
1024			if (error != 0) {
1025				vfs_unbusy(mp);
1026				return (error);
1027			}
1028			ASSERT_VOP_ELOCKED(fdvp,
1029			    "tmpfs_rename: fdvp not locked");
1030			ASSERT_VOP_ELOCKED(tdvp,
1031			    "tmpfs_rename: tdvp not locked");
1032			if (tvp != NULL)
1033				ASSERT_VOP_ELOCKED(tvp,
1034				    "tmpfs_rename: tvp not locked");
1035			if (fvp == tvp) {
1036				error = 0;
1037				goto out_locked;
1038			}
1039		}
1040	}
1041
1042	tmp = VFS_TO_TMPFS(tdvp->v_mount);
1043	tdnode = VP_TO_TMPFS_DIR(tdvp);
1044	tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
1045	fdnode = VP_TO_TMPFS_DIR(fdvp);
1046	fnode = VP_TO_TMPFS_NODE(fvp);
1047	de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
1048
1049	/* Entry can disappear before we lock fdvp,
1050	 * also avoid manipulating '.' and '..' entries. */
1051	if (de == NULL) {
1052		if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
1053		    (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
1054			error = EINVAL;
1055		else
1056			error = ENOENT;
1057		goto out_locked;
1058	}
1059	MPASS(de->td_node == fnode);
1060
1061	/* If re-naming a directory to another preexisting directory
1062	 * ensure that the target directory is empty so that its
1063	 * removal causes no side effects.
1064	 * Kern_rename gurantees the destination to be a directory
1065	 * if the source is one. */
1066	if (tvp != NULL) {
1067		MPASS(tnode != NULL);
1068
1069		if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1070		    (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1071			error = EPERM;
1072			goto out_locked;
1073		}
1074
1075		if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1076			if (tnode->tn_size > 0) {
1077				error = ENOTEMPTY;
1078				goto out_locked;
1079			}
1080		} else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1081			error = ENOTDIR;
1082			goto out_locked;
1083		} else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1084			error = EISDIR;
1085			goto out_locked;
1086		} else {
1087			MPASS(fnode->tn_type != VDIR &&
1088				tnode->tn_type != VDIR);
1089		}
1090	}
1091
1092	if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1093	    || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1094		error = EPERM;
1095		goto out_locked;
1096	}
1097
1098	/* Ensure that we have enough memory to hold the new name, if it
1099	 * has to be changed. */
1100	if (fcnp->cn_namelen != tcnp->cn_namelen ||
1101	    bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1102		newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1103	} else
1104		newname = NULL;
1105
1106	/* If the node is being moved to another directory, we have to do
1107	 * the move. */
1108	if (fdnode != tdnode) {
1109		/* In case we are moving a directory, we have to adjust its
1110		 * parent to point to the new parent. */
1111		if (de->td_node->tn_type == VDIR) {
1112			struct tmpfs_node *n;
1113
1114			/* Ensure the target directory is not a child of the
1115			 * directory being moved.  Otherwise, we'd end up
1116			 * with stale nodes. */
1117			n = tdnode;
1118			/* TMPFS_LOCK garanties that no nodes are freed while
1119			 * traversing the list. Nodes can only be marked as
1120			 * removed: tn_parent == NULL. */
1121			TMPFS_LOCK(tmp);
1122			TMPFS_NODE_LOCK(n);
1123			while (n != n->tn_dir.tn_parent) {
1124				struct tmpfs_node *parent;
1125
1126				if (n == fnode) {
1127					TMPFS_NODE_UNLOCK(n);
1128					TMPFS_UNLOCK(tmp);
1129					error = EINVAL;
1130					if (newname != NULL)
1131						    free(newname, M_TMPFSNAME);
1132					goto out_locked;
1133				}
1134				parent = n->tn_dir.tn_parent;
1135				TMPFS_NODE_UNLOCK(n);
1136				if (parent == NULL) {
1137					n = NULL;
1138					break;
1139				}
1140				TMPFS_NODE_LOCK(parent);
1141				if (parent->tn_dir.tn_parent == NULL) {
1142					TMPFS_NODE_UNLOCK(parent);
1143					n = NULL;
1144					break;
1145				}
1146				n = parent;
1147			}
1148			TMPFS_UNLOCK(tmp);
1149			if (n == NULL) {
1150				error = EINVAL;
1151				if (newname != NULL)
1152					    free(newname, M_TMPFSNAME);
1153				goto out_locked;
1154			}
1155			TMPFS_NODE_UNLOCK(n);
1156
1157			/* Adjust the parent pointer. */
1158			TMPFS_VALIDATE_DIR(fnode);
1159			TMPFS_NODE_LOCK(de->td_node);
1160			de->td_node->tn_dir.tn_parent = tdnode;
1161			TMPFS_NODE_UNLOCK(de->td_node);
1162
1163			/* As a result of changing the target of the '..'
1164			 * entry, the link count of the source and target
1165			 * directories has to be adjusted. */
1166			TMPFS_NODE_LOCK(tdnode);
1167			TMPFS_ASSERT_LOCKED(tdnode);
1168			tdnode->tn_links++;
1169			TMPFS_NODE_UNLOCK(tdnode);
1170
1171			TMPFS_NODE_LOCK(fdnode);
1172			TMPFS_ASSERT_LOCKED(fdnode);
1173			fdnode->tn_links--;
1174			TMPFS_NODE_UNLOCK(fdnode);
1175		}
1176	}
1177
1178	/* Do the move: just remove the entry from the source directory
1179	 * and insert it into the target one. */
1180	tmpfs_dir_detach(fdvp, de);
1181
1182	if (fcnp->cn_flags & DOWHITEOUT)
1183		tmpfs_dir_whiteout_add(fdvp, fcnp);
1184	if (tcnp->cn_flags & ISWHITEOUT)
1185		tmpfs_dir_whiteout_remove(tdvp, tcnp);
1186
1187	/* If the name has changed, we need to make it effective by changing
1188	 * it in the directory entry. */
1189	if (newname != NULL) {
1190		MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1191
1192		free(de->ud.td_name, M_TMPFSNAME);
1193		de->ud.td_name = newname;
1194		tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
1195
1196		fnode->tn_status |= TMPFS_NODE_CHANGED;
1197		tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1198	}
1199
1200	/* If we are overwriting an entry, we have to remove the old one
1201	 * from the target directory. */
1202	if (tvp != NULL) {
1203		struct tmpfs_dirent *tde;
1204
1205		/* Remove the old entry from the target directory. */
1206		tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1207		tmpfs_dir_detach(tdvp, tde);
1208
1209		/* Free the directory entry we just deleted.  Note that the
1210		 * node referred by it will not be removed until the vnode is
1211		 * really reclaimed. */
1212		tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1213	}
1214
1215	tmpfs_dir_attach(tdvp, de);
1216
1217	cache_purge(fvp);
1218	if (tvp != NULL)
1219		cache_purge(tvp);
1220	cache_purge_negative(tdvp);
1221
1222	error = 0;
1223
1224out_locked:
1225	if (fdvp != tdvp && fdvp != tvp)
1226		VOP_UNLOCK(fdvp, 0);
1227
1228out:
1229	/* Release target nodes. */
1230	/* XXX: I don't understand when tdvp can be the same as tvp, but
1231	 * other code takes care of this... */
1232	if (tdvp == tvp)
1233		vrele(tdvp);
1234	else
1235		vput(tdvp);
1236	if (tvp != NULL)
1237		vput(tvp);
1238
1239	/* Release source nodes. */
1240	vrele(fdvp);
1241	vrele(fvp);
1242
1243	if (mp != NULL)
1244		vfs_unbusy(mp);
1245
1246	return error;
1247}
1248
1249/* --------------------------------------------------------------------- */
1250
1251static int
1252tmpfs_mkdir(struct vop_mkdir_args *v)
1253{
1254	struct vnode *dvp = v->a_dvp;
1255	struct vnode **vpp = v->a_vpp;
1256	struct componentname *cnp = v->a_cnp;
1257	struct vattr *vap = v->a_vap;
1258
1259	MPASS(vap->va_type == VDIR);
1260
1261	return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1262}
1263
1264/* --------------------------------------------------------------------- */
1265
1266static int
1267tmpfs_rmdir(struct vop_rmdir_args *v)
1268{
1269	struct vnode *dvp = v->a_dvp;
1270	struct vnode *vp = v->a_vp;
1271
1272	int error;
1273	struct tmpfs_dirent *de;
1274	struct tmpfs_mount *tmp;
1275	struct tmpfs_node *dnode;
1276	struct tmpfs_node *node;
1277
1278	MPASS(VOP_ISLOCKED(dvp));
1279	MPASS(VOP_ISLOCKED(vp));
1280
1281	tmp = VFS_TO_TMPFS(dvp->v_mount);
1282	dnode = VP_TO_TMPFS_DIR(dvp);
1283	node = VP_TO_TMPFS_DIR(vp);
1284
1285	/* Directories with more than two entries ('.' and '..') cannot be
1286	 * removed. */
1287	 if (node->tn_size > 0) {
1288		 error = ENOTEMPTY;
1289		 goto out;
1290	 }
1291
1292	if ((dnode->tn_flags & APPEND)
1293	    || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1294		error = EPERM;
1295		goto out;
1296	}
1297
1298	/* This invariant holds only if we are not trying to remove "..".
1299	  * We checked for that above so this is safe now. */
1300	MPASS(node->tn_dir.tn_parent == dnode);
1301
1302	/* Get the directory entry associated with node (vp).  This was
1303	 * filled by tmpfs_lookup while looking up the entry. */
1304	de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1305	MPASS(TMPFS_DIRENT_MATCHES(de,
1306	    v->a_cnp->cn_nameptr,
1307	    v->a_cnp->cn_namelen));
1308
1309	/* Check flags to see if we are allowed to remove the directory. */
1310	if (dnode->tn_flags & APPEND
1311		|| node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1312		error = EPERM;
1313		goto out;
1314	}
1315
1316
1317	/* Detach the directory entry from the directory (dnode). */
1318	tmpfs_dir_detach(dvp, de);
1319	if (v->a_cnp->cn_flags & DOWHITEOUT)
1320		tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1321
1322	/* No vnode should be allocated for this entry from this point */
1323	TMPFS_NODE_LOCK(node);
1324	TMPFS_ASSERT_ELOCKED(node);
1325	node->tn_links--;
1326	node->tn_dir.tn_parent = NULL;
1327	node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1328	    TMPFS_NODE_MODIFIED;
1329
1330	TMPFS_NODE_UNLOCK(node);
1331
1332	TMPFS_NODE_LOCK(dnode);
1333	TMPFS_ASSERT_ELOCKED(dnode);
1334	dnode->tn_links--;
1335	dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1336	    TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1337	TMPFS_NODE_UNLOCK(dnode);
1338
1339	cache_purge(dvp);
1340	cache_purge(vp);
1341
1342	/* Free the directory entry we just deleted.  Note that the node
1343	 * referred by it will not be removed until the vnode is really
1344	 * reclaimed. */
1345	tmpfs_free_dirent(tmp, de);
1346
1347	/* Release the deleted vnode (will destroy the node, notify
1348	 * interested parties and clean it from the cache). */
1349
1350	dnode->tn_status |= TMPFS_NODE_CHANGED;
1351	tmpfs_update(dvp);
1352
1353	error = 0;
1354
1355out:
1356	return error;
1357}
1358
1359/* --------------------------------------------------------------------- */
1360
1361static int
1362tmpfs_symlink(struct vop_symlink_args *v)
1363{
1364	struct vnode *dvp = v->a_dvp;
1365	struct vnode **vpp = v->a_vpp;
1366	struct componentname *cnp = v->a_cnp;
1367	struct vattr *vap = v->a_vap;
1368	char *target = v->a_target;
1369
1370#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1371	MPASS(vap->va_type == VLNK);
1372#else
1373	vap->va_type = VLNK;
1374#endif
1375
1376	return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1377}
1378
1379/* --------------------------------------------------------------------- */
1380
1381static int
1382tmpfs_readdir(struct vop_readdir_args *v)
1383{
1384	struct vnode *vp = v->a_vp;
1385	struct uio *uio = v->a_uio;
1386	int *eofflag = v->a_eofflag;
1387	u_long **cookies = v->a_cookies;
1388	int *ncookies = v->a_ncookies;
1389
1390	int error;
1391	ssize_t startresid;
1392	int cnt = 0;
1393	struct tmpfs_node *node;
1394
1395	/* This operation only makes sense on directory nodes. */
1396	if (vp->v_type != VDIR)
1397		return ENOTDIR;
1398
1399	node = VP_TO_TMPFS_DIR(vp);
1400
1401	startresid = uio->uio_resid;
1402
1403	if (cookies != NULL && ncookies != NULL) {
1404		cnt = howmany(node->tn_size, sizeof(struct tmpfs_dirent)) + 2;
1405		*cookies = malloc(cnt * sizeof(**cookies), M_TEMP, M_WAITOK);
1406		*ncookies = 0;
1407	}
1408
1409	if (cnt == 0)
1410		error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1411	else
1412		error = tmpfs_dir_getdents(node, uio, cnt, *cookies, ncookies);
1413
1414	if (error == EJUSTRETURN)
1415		error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1416
1417	if (error != 0 && cnt != 0)
1418		free(*cookies, M_TEMP);
1419
1420	if (eofflag != NULL)
1421		*eofflag =
1422		    (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1423
1424	return error;
1425}
1426
1427/* --------------------------------------------------------------------- */
1428
1429static int
1430tmpfs_readlink(struct vop_readlink_args *v)
1431{
1432	struct vnode *vp = v->a_vp;
1433	struct uio *uio = v->a_uio;
1434
1435	int error;
1436	struct tmpfs_node *node;
1437
1438	MPASS(uio->uio_offset == 0);
1439	MPASS(vp->v_type == VLNK);
1440
1441	node = VP_TO_TMPFS_NODE(vp);
1442
1443	error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1444	    uio);
1445	node->tn_status |= TMPFS_NODE_ACCESSED;
1446
1447	return error;
1448}
1449
1450/* --------------------------------------------------------------------- */
1451
1452static int
1453tmpfs_inactive(struct vop_inactive_args *v)
1454{
1455	struct vnode *vp = v->a_vp;
1456
1457	struct tmpfs_node *node;
1458
1459	node = VP_TO_TMPFS_NODE(vp);
1460
1461	if (node->tn_links == 0)
1462		vrecycle(vp);
1463
1464	return 0;
1465}
1466
1467/* --------------------------------------------------------------------- */
1468
1469int
1470tmpfs_reclaim(struct vop_reclaim_args *v)
1471{
1472	struct vnode *vp = v->a_vp;
1473
1474	struct tmpfs_mount *tmp;
1475	struct tmpfs_node *node;
1476
1477	node = VP_TO_TMPFS_NODE(vp);
1478	tmp = VFS_TO_TMPFS(vp->v_mount);
1479
1480	if (vp->v_type == VREG)
1481		tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1482	else
1483		vnode_destroy_vobject(vp);
1484	vp->v_object = NULL;
1485	cache_purge(vp);
1486
1487	TMPFS_NODE_LOCK(node);
1488	TMPFS_ASSERT_ELOCKED(node);
1489	tmpfs_free_vp(vp);
1490
1491	/* If the node referenced by this vnode was deleted by the user,
1492	 * we must free its associated data structures (now that the vnode
1493	 * is being reclaimed). */
1494	if (node->tn_links == 0 &&
1495	    (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1496		node->tn_vpstate = TMPFS_VNODE_DOOMED;
1497		TMPFS_NODE_UNLOCK(node);
1498		tmpfs_free_node(tmp, node);
1499	} else
1500		TMPFS_NODE_UNLOCK(node);
1501
1502	MPASS(vp->v_data == NULL);
1503	return 0;
1504}
1505
1506/* --------------------------------------------------------------------- */
1507
1508static int
1509tmpfs_print(struct vop_print_args *v)
1510{
1511	struct vnode *vp = v->a_vp;
1512
1513	struct tmpfs_node *node;
1514
1515	node = VP_TO_TMPFS_NODE(vp);
1516
1517	printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1518	    node, node->tn_flags, node->tn_links);
1519	printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1520	    node->tn_mode, node->tn_uid, node->tn_gid,
1521	    (intmax_t)node->tn_size, node->tn_status);
1522
1523	if (vp->v_type == VFIFO)
1524		fifo_printinfo(vp);
1525
1526	printf("\n");
1527
1528	return 0;
1529}
1530
1531/* --------------------------------------------------------------------- */
1532
1533static int
1534tmpfs_pathconf(struct vop_pathconf_args *v)
1535{
1536	int name = v->a_name;
1537	register_t *retval = v->a_retval;
1538
1539	int error;
1540
1541	error = 0;
1542
1543	switch (name) {
1544	case _PC_LINK_MAX:
1545		*retval = LINK_MAX;
1546		break;
1547
1548	case _PC_NAME_MAX:
1549		*retval = NAME_MAX;
1550		break;
1551
1552	case _PC_PATH_MAX:
1553		*retval = PATH_MAX;
1554		break;
1555
1556	case _PC_PIPE_BUF:
1557		*retval = PIPE_BUF;
1558		break;
1559
1560	case _PC_CHOWN_RESTRICTED:
1561		*retval = 1;
1562		break;
1563
1564	case _PC_NO_TRUNC:
1565		*retval = 1;
1566		break;
1567
1568	case _PC_SYNC_IO:
1569		*retval = 1;
1570		break;
1571
1572	case _PC_FILESIZEBITS:
1573		*retval = 0; /* XXX Don't know which value should I return. */
1574		break;
1575
1576	default:
1577		error = EINVAL;
1578	}
1579
1580	return error;
1581}
1582
1583static int
1584tmpfs_vptofh(struct vop_vptofh_args *ap)
1585{
1586	struct tmpfs_fid *tfhp;
1587	struct tmpfs_node *node;
1588
1589	tfhp = (struct tmpfs_fid *)ap->a_fhp;
1590	node = VP_TO_TMPFS_NODE(ap->a_vp);
1591
1592	tfhp->tf_len = sizeof(struct tmpfs_fid);
1593	tfhp->tf_id = node->tn_id;
1594	tfhp->tf_gen = node->tn_gen;
1595
1596	return (0);
1597}
1598
1599static int
1600tmpfs_whiteout(struct vop_whiteout_args *ap)
1601{
1602	struct vnode *dvp = ap->a_dvp;
1603	struct componentname *cnp = ap->a_cnp;
1604	struct tmpfs_dirent *de;
1605
1606	switch (ap->a_flags) {
1607	case LOOKUP:
1608		return (0);
1609	case CREATE:
1610		de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1611		if (de != NULL)
1612			return (de->td_node == NULL ? 0 : EEXIST);
1613		return (tmpfs_dir_whiteout_add(dvp, cnp));
1614	case DELETE:
1615		tmpfs_dir_whiteout_remove(dvp, cnp);
1616		return (0);
1617	default:
1618		panic("tmpfs_whiteout: unknown op");
1619	}
1620}
1621
1622/* --------------------------------------------------------------------- */
1623
1624/*
1625 * vnode operations vector used for files stored in a tmpfs file system.
1626 */
1627struct vop_vector tmpfs_vnodeop_entries = {
1628	.vop_default =			&default_vnodeops,
1629	.vop_lookup =			vfs_cache_lookup,
1630	.vop_cachedlookup =		tmpfs_lookup,
1631	.vop_create =			tmpfs_create,
1632	.vop_mknod =			tmpfs_mknod,
1633	.vop_open =			tmpfs_open,
1634	.vop_close =			tmpfs_close,
1635	.vop_access =			tmpfs_access,
1636	.vop_getattr =			tmpfs_getattr,
1637	.vop_setattr =			tmpfs_setattr,
1638	.vop_read =			tmpfs_read,
1639	.vop_write =			tmpfs_write,
1640	.vop_fsync =			tmpfs_fsync,
1641	.vop_remove =			tmpfs_remove,
1642	.vop_link =			tmpfs_link,
1643	.vop_rename =			tmpfs_rename,
1644	.vop_mkdir =			tmpfs_mkdir,
1645	.vop_rmdir =			tmpfs_rmdir,
1646	.vop_symlink =			tmpfs_symlink,
1647	.vop_readdir =			tmpfs_readdir,
1648	.vop_readlink =			tmpfs_readlink,
1649	.vop_inactive =			tmpfs_inactive,
1650	.vop_reclaim =			tmpfs_reclaim,
1651	.vop_print =			tmpfs_print,
1652	.vop_pathconf =			tmpfs_pathconf,
1653	.vop_vptofh =			tmpfs_vptofh,
1654	.vop_whiteout =			tmpfs_whiteout,
1655	.vop_bmap =			VOP_EOPNOTSUPP,
1656};
1657
1658