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