1 /* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * tmpfs vnode interface.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/fcntl.h>
41 #include <sys/lockf.h>
42 #include <sys/namei.h>
45 #include <sys/sched.h>
46 #include <sys/sf_buf.h>
48 #include <sys/systm.h>
49 #include <sys/unistd.h>
50 #include <sys/vnode.h>
53 #include <vm/vm_object.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_pager.h>
57 #include <machine/_inttypes.h>
59 #include <fs/fifofs/fifo.h>
60 #include <fs/tmpfs/tmpfs_vnops.h>
61 #include <fs/tmpfs/tmpfs.h>
63 /* --------------------------------------------------------------------- */
66 tmpfs_lookup(struct vop_cachedlookup_args *v)
68 struct vnode *dvp = v->a_dvp;
69 struct vnode **vpp = v->a_vpp;
70 struct componentname *cnp = v->a_cnp;
73 struct tmpfs_dirent *de;
74 struct tmpfs_node *dnode;
76 dnode = VP_TO_TMPFS_DIR(dvp);
79 /* Check accessibility of requested node as a first step. */
80 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
84 /* We cannot be requesting the parent directory of the root node. */
85 MPASS(IMPLIES(dnode->tn_type == VDIR &&
86 dnode->tn_dir.tn_parent == dnode,
87 !(cnp->cn_flags & ISDOTDOT)));
89 TMPFS_ASSERT_LOCKED(dnode);
90 if (dnode->tn_dir.tn_parent == NULL) {
94 if (cnp->cn_flags & ISDOTDOT) {
97 ltype = VOP_ISLOCKED(dvp);
100 /* Allocate a new vnode on the matching entry. */
101 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
102 cnp->cn_lkflags, vpp);
104 vn_lock(dvp, ltype | LK_RETRY);
106 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
111 de = tmpfs_dir_lookup(dnode, NULL, cnp);
112 if (de != NULL && de->td_node == NULL)
113 cnp->cn_flags |= ISWHITEOUT;
114 if (de == NULL || de->td_node == NULL) {
115 /* The entry was not found in the directory.
116 * This is OK if we are creating or renaming an
117 * entry and are working on the last component of
119 if ((cnp->cn_flags & ISLASTCN) &&
120 (cnp->cn_nameiop == CREATE || \
121 cnp->cn_nameiop == RENAME ||
122 (cnp->cn_nameiop == DELETE &&
123 cnp->cn_flags & DOWHITEOUT &&
124 cnp->cn_flags & ISWHITEOUT))) {
125 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
130 /* Keep the component name in the buffer for
132 cnp->cn_flags |= SAVENAME;
138 struct tmpfs_node *tnode;
140 /* The entry was found, so get its associated
144 /* If we are not at the last path component and
145 * found a non-directory or non-link entry (which
146 * may itself be pointing to a directory), raise
148 if ((tnode->tn_type != VDIR &&
149 tnode->tn_type != VLNK) &&
150 !(cnp->cn_flags & ISLASTCN)) {
155 /* If we are deleting or renaming the entry, keep
156 * track of its tmpfs_dirent so that it can be
157 * easily deleted later. */
158 if ((cnp->cn_flags & ISLASTCN) &&
159 (cnp->cn_nameiop == DELETE ||
160 cnp->cn_nameiop == RENAME)) {
161 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
166 /* Allocate a new vnode on the matching entry. */
167 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
168 cnp->cn_lkflags, vpp);
172 if ((dnode->tn_mode & S_ISTXT) &&
173 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
174 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
180 cnp->cn_flags |= SAVENAME;
182 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
183 cnp->cn_lkflags, vpp);
188 /* Store the result of this lookup in the cache. Avoid this if the
189 * request was for creation, as it does not improve timings on
191 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
192 cache_enter(dvp, *vpp, cnp);
195 /* If there were no errors, *vpp cannot be null and it must be
197 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
202 /* --------------------------------------------------------------------- */
205 tmpfs_create(struct vop_create_args *v)
207 struct vnode *dvp = v->a_dvp;
208 struct vnode **vpp = v->a_vpp;
209 struct componentname *cnp = v->a_cnp;
210 struct vattr *vap = v->a_vap;
212 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
214 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
216 /* --------------------------------------------------------------------- */
219 tmpfs_mknod(struct vop_mknod_args *v)
221 struct vnode *dvp = v->a_dvp;
222 struct vnode **vpp = v->a_vpp;
223 struct componentname *cnp = v->a_cnp;
224 struct vattr *vap = v->a_vap;
226 if (vap->va_type != VBLK && vap->va_type != VCHR &&
227 vap->va_type != VFIFO)
230 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
233 /* --------------------------------------------------------------------- */
236 tmpfs_open(struct vop_open_args *v)
238 struct vnode *vp = v->a_vp;
239 int mode = v->a_mode;
242 struct tmpfs_node *node;
244 MPASS(VOP_ISLOCKED(vp));
246 node = VP_TO_TMPFS_NODE(vp);
248 /* The file is still active but all its names have been removed
249 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
250 * it is about to die. */
251 if (node->tn_links < 1)
254 /* If the file is marked append-only, deny write requests. */
255 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
259 vnode_create_vobject(vp, node->tn_size, v->a_td);
262 MPASS(VOP_ISLOCKED(vp));
266 /* --------------------------------------------------------------------- */
269 tmpfs_close(struct vop_close_args *v)
271 struct vnode *vp = v->a_vp;
273 MPASS(VOP_ISLOCKED(vp));
275 /* Update node times. */
281 /* --------------------------------------------------------------------- */
284 tmpfs_access(struct vop_access_args *v)
286 struct vnode *vp = v->a_vp;
287 accmode_t accmode = v->a_accmode;
288 struct ucred *cred = v->a_cred;
291 struct tmpfs_node *node;
293 MPASS(VOP_ISLOCKED(vp));
295 node = VP_TO_TMPFS_NODE(vp);
297 switch (vp->v_type) {
303 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
323 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
328 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
329 node->tn_gid, accmode, cred, NULL);
332 MPASS(VOP_ISLOCKED(vp));
337 /* --------------------------------------------------------------------- */
340 tmpfs_getattr(struct vop_getattr_args *v)
342 struct vnode *vp = v->a_vp;
343 struct vattr *vap = v->a_vap;
345 struct tmpfs_node *node;
347 node = VP_TO_TMPFS_NODE(vp);
351 vap->va_type = vp->v_type;
352 vap->va_mode = node->tn_mode;
353 vap->va_nlink = node->tn_links;
354 vap->va_uid = node->tn_uid;
355 vap->va_gid = node->tn_gid;
356 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
357 vap->va_fileid = node->tn_id;
358 vap->va_size = node->tn_size;
359 vap->va_blocksize = PAGE_SIZE;
360 vap->va_atime = node->tn_atime;
361 vap->va_mtime = node->tn_mtime;
362 vap->va_ctime = node->tn_ctime;
363 vap->va_birthtime = node->tn_birthtime;
364 vap->va_gen = node->tn_gen;
365 vap->va_flags = node->tn_flags;
366 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
367 node->tn_rdev : NODEV;
368 vap->va_bytes = round_page(node->tn_size);
374 /* --------------------------------------------------------------------- */
376 /* XXX Should this operation be atomic? I think it should, but code in
377 * XXX other places (e.g., ufs) doesn't seem to be... */
379 tmpfs_setattr(struct vop_setattr_args *v)
381 struct vnode *vp = v->a_vp;
382 struct vattr *vap = v->a_vap;
383 struct ucred *cred = v->a_cred;
384 struct thread *td = curthread;
388 MPASS(VOP_ISLOCKED(vp));
392 /* Abort if any unsettable attribute is given. */
393 if (vap->va_type != VNON ||
394 vap->va_nlink != VNOVAL ||
395 vap->va_fsid != VNOVAL ||
396 vap->va_fileid != VNOVAL ||
397 vap->va_blocksize != VNOVAL ||
398 vap->va_gen != VNOVAL ||
399 vap->va_rdev != VNOVAL ||
400 vap->va_bytes != VNOVAL)
403 if (error == 0 && (vap->va_flags != VNOVAL))
404 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
406 if (error == 0 && (vap->va_size != VNOVAL))
407 error = tmpfs_chsize(vp, vap->va_size, cred, td);
409 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
410 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
412 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
413 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
415 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
416 vap->va_atime.tv_nsec != VNOVAL) ||
417 (vap->va_mtime.tv_sec != VNOVAL &&
418 vap->va_mtime.tv_nsec != VNOVAL) ||
419 (vap->va_birthtime.tv_sec != VNOVAL &&
420 vap->va_birthtime.tv_nsec != VNOVAL)))
421 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
422 &vap->va_birthtime, vap->va_vaflags, cred, td);
424 /* Update the node times. We give preference to the error codes
425 * generated by this function rather than the ones that may arise
426 * from tmpfs_update. */
429 MPASS(VOP_ISLOCKED(vp));
434 /* --------------------------------------------------------------------- */
436 tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
437 vm_offset_t offset, size_t tlen, struct uio *uio)
442 VM_OBJECT_LOCK(tobj);
443 vm_object_pip_add(tobj, 1);
444 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
445 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
446 if (m->valid != VM_PAGE_BITS_ALL) {
447 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
448 error = vm_pager_get_pages(tobj, &m, 1, 0);
450 printf("tmpfs get pages from pager error [read]\n");
454 vm_page_zero_invalid(m, TRUE);
456 VM_OBJECT_UNLOCK(tobj);
457 error = uiomove_fromphys(&m, offset, tlen, uio);
458 VM_OBJECT_LOCK(tobj);
461 vm_page_unwire(m, TRUE);
464 vm_object_pip_subtract(tobj, 1);
465 VM_OBJECT_UNLOCK(tobj);
471 tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
472 vm_offset_t offset, size_t tlen, void *buf)
483 uio.uio_resid = tlen;
484 uio.uio_rw = UIO_READ;
485 uio.uio_segflg = UIO_SYSSPACE;
486 uio.uio_td = curthread;
488 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
492 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
503 addr = uio->uio_offset;
504 idx = OFF_TO_IDX(addr);
505 offset = addr & PAGE_MASK;
506 tlen = MIN(PAGE_SIZE - offset, len);
508 if ((vobj == NULL) ||
509 (vobj->resident_page_count == 0 && vobj->cache == NULL))
512 VM_OBJECT_LOCK(vobj);
514 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
515 vm_page_is_valid(m, offset, tlen)) {
516 if ((m->oflags & VPO_BUSY) != 0) {
518 * Reference the page before unlocking and sleeping so
519 * that the page daemon is less likely to reclaim it.
521 vm_page_reference(m);
522 vm_page_sleep(m, "tmfsmr");
526 VM_OBJECT_UNLOCK(vobj);
527 error = uiomove_fromphys(&m, offset, tlen, uio);
528 VM_OBJECT_LOCK(vobj);
530 VM_OBJECT_UNLOCK(vobj);
532 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
534 ("unexpected offset in tmpfs_mappedread for sendfile"));
535 if ((m->oflags & VPO_BUSY) != 0) {
537 * Reference the page before unlocking and sleeping so
538 * that the page daemon is less likely to reclaim it.
540 vm_page_reference(m);
541 vm_page_sleep(m, "tmfsmr");
545 VM_OBJECT_UNLOCK(vobj);
547 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
548 ma = (char *)sf_buf_kva(sf);
549 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
551 if (tlen != PAGE_SIZE)
552 bzero(ma + tlen, PAGE_SIZE - tlen);
553 uio->uio_offset += tlen;
554 uio->uio_resid -= tlen;
558 VM_OBJECT_LOCK(vobj);
560 m->valid = VM_PAGE_BITS_ALL;
562 VM_OBJECT_UNLOCK(vobj);
565 VM_OBJECT_UNLOCK(vobj);
567 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
573 tmpfs_read(struct vop_read_args *v)
575 struct vnode *vp = v->a_vp;
576 struct uio *uio = v->a_uio;
578 struct tmpfs_node *node;
585 node = VP_TO_TMPFS_NODE(vp);
587 if (vp->v_type != VREG) {
592 if (uio->uio_offset < 0) {
597 node->tn_status |= TMPFS_NODE_ACCESSED;
599 uobj = node->tn_reg.tn_aobj;
600 while ((resid = uio->uio_resid) > 0) {
602 if (node->tn_size <= uio->uio_offset)
604 len = MIN(node->tn_size - uio->uio_offset, resid);
607 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
608 if ((error != 0) || (resid == uio->uio_resid))
617 /* --------------------------------------------------------------------- */
620 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
631 addr = uio->uio_offset;
632 idx = OFF_TO_IDX(addr);
633 offset = addr & PAGE_MASK;
634 tlen = MIN(PAGE_SIZE - offset, len);
636 if ((vobj == NULL) ||
637 (vobj->resident_page_count == 0 && vobj->cache == NULL)) {
642 VM_OBJECT_LOCK(vobj);
644 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
645 vm_page_is_valid(vpg, offset, tlen)) {
646 if ((vpg->oflags & VPO_BUSY) != 0) {
648 * Reference the page before unlocking and sleeping so
649 * that the page daemon is less likely to reclaim it.
651 vm_page_reference(vpg);
652 vm_page_sleep(vpg, "tmfsmw");
656 vm_page_undirty(vpg);
657 VM_OBJECT_UNLOCK(vobj);
658 error = uiomove_fromphys(&vpg, offset, tlen, uio);
660 if (__predict_false(vobj->cache != NULL))
661 vm_page_cache_free(vobj, idx, idx + 1);
662 VM_OBJECT_UNLOCK(vobj);
666 VM_OBJECT_LOCK(tobj);
667 vm_object_pip_add(tobj, 1);
668 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
669 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
670 if (tpg->valid != VM_PAGE_BITS_ALL) {
671 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
672 error = vm_pager_get_pages(tobj, &tpg, 1, 0);
674 printf("tmpfs get pages from pager error [write]\n");
678 vm_page_zero_invalid(tpg, TRUE);
680 VM_OBJECT_UNLOCK(tobj);
682 error = uiomove_fromphys(&tpg, offset, tlen, uio);
684 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
685 pmap_copy_page(vpg, tpg);
687 VM_OBJECT_LOCK(tobj);
690 VM_OBJECT_LOCK(vobj);
692 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
693 ("parts of tpg invalid"));
697 vm_page_unwire(tpg, TRUE);
703 VM_OBJECT_UNLOCK(vobj);
704 vm_object_pip_subtract(tobj, 1);
705 VM_OBJECT_UNLOCK(tobj);
711 tmpfs_write(struct vop_write_args *v)
713 struct vnode *vp = v->a_vp;
714 struct uio *uio = v->a_uio;
715 int ioflag = v->a_ioflag;
720 struct tmpfs_node *node;
725 node = VP_TO_TMPFS_NODE(vp);
726 oldsize = node->tn_size;
728 if (uio->uio_offset < 0 || vp->v_type != VREG) {
733 if (uio->uio_resid == 0) {
738 if (ioflag & IO_APPEND)
739 uio->uio_offset = node->tn_size;
741 if (uio->uio_offset + uio->uio_resid >
742 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
745 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
748 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
750 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
755 uobj = node->tn_reg.tn_aobj;
756 while ((resid = uio->uio_resid) > 0) {
757 if (node->tn_size <= uio->uio_offset)
759 len = MIN(node->tn_size - uio->uio_offset, resid);
762 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
763 if ((error != 0) || (resid == uio->uio_resid))
767 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
768 (extended ? TMPFS_NODE_CHANGED : 0);
770 if (node->tn_mode & (S_ISUID | S_ISGID)) {
771 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
772 node->tn_mode &= ~(S_ISUID | S_ISGID);
776 (void)tmpfs_reg_resize(vp, oldsize);
779 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
780 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
785 /* --------------------------------------------------------------------- */
788 tmpfs_fsync(struct vop_fsync_args *v)
790 struct vnode *vp = v->a_vp;
792 MPASS(VOP_ISLOCKED(vp));
799 /* --------------------------------------------------------------------- */
802 tmpfs_remove(struct vop_remove_args *v)
804 struct vnode *dvp = v->a_dvp;
805 struct vnode *vp = v->a_vp;
808 struct tmpfs_dirent *de;
809 struct tmpfs_mount *tmp;
810 struct tmpfs_node *dnode;
811 struct tmpfs_node *node;
813 MPASS(VOP_ISLOCKED(dvp));
814 MPASS(VOP_ISLOCKED(vp));
816 if (vp->v_type == VDIR) {
821 dnode = VP_TO_TMPFS_DIR(dvp);
822 node = VP_TO_TMPFS_NODE(vp);
823 tmp = VFS_TO_TMPFS(vp->v_mount);
824 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
827 /* Files marked as immutable or append-only cannot be deleted. */
828 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
829 (dnode->tn_flags & APPEND)) {
834 /* Remove the entry from the directory; as it is a file, we do not
835 * have to change the number of hard links of the directory. */
836 tmpfs_dir_detach(dvp, de);
837 if (v->a_cnp->cn_flags & DOWHITEOUT)
838 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
840 /* Free the directory entry we just deleted. Note that the node
841 * referred by it will not be removed until the vnode is really
843 tmpfs_free_dirent(tmp, de, TRUE);
845 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
853 /* --------------------------------------------------------------------- */
856 tmpfs_link(struct vop_link_args *v)
858 struct vnode *dvp = v->a_tdvp;
859 struct vnode *vp = v->a_vp;
860 struct componentname *cnp = v->a_cnp;
863 struct tmpfs_dirent *de;
864 struct tmpfs_node *node;
866 MPASS(VOP_ISLOCKED(dvp));
867 MPASS(cnp->cn_flags & HASBUF);
868 MPASS(dvp != vp); /* XXX When can this be false? */
870 node = VP_TO_TMPFS_NODE(vp);
872 /* XXX: Why aren't the following two tests done by the caller? */
874 /* Hard links of directories are forbidden. */
875 if (vp->v_type == VDIR) {
880 /* Cannot create cross-device links. */
881 if (dvp->v_mount != vp->v_mount) {
886 /* Ensure that we do not overflow the maximum number of links imposed
888 MPASS(node->tn_links <= LINK_MAX);
889 if (node->tn_links == LINK_MAX) {
894 /* We cannot create links of files marked immutable or append-only. */
895 if (node->tn_flags & (IMMUTABLE | APPEND)) {
900 /* Allocate a new directory entry to represent the node. */
901 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
902 cnp->cn_nameptr, cnp->cn_namelen, &de);
906 /* Insert the new directory entry into the appropriate directory. */
907 if (cnp->cn_flags & ISWHITEOUT)
908 tmpfs_dir_whiteout_remove(dvp, cnp);
909 tmpfs_dir_attach(dvp, de);
911 /* vp link count has changed, so update node times. */
912 node->tn_status |= TMPFS_NODE_CHANGED;
921 /* --------------------------------------------------------------------- */
924 tmpfs_rename(struct vop_rename_args *v)
926 struct vnode *fdvp = v->a_fdvp;
927 struct vnode *fvp = v->a_fvp;
928 struct componentname *fcnp = v->a_fcnp;
929 struct vnode *tdvp = v->a_tdvp;
930 struct vnode *tvp = v->a_tvp;
931 struct componentname *tcnp = v->a_tcnp;
935 struct tmpfs_dirent *de;
936 struct tmpfs_mount *tmp;
937 struct tmpfs_node *fdnode;
938 struct tmpfs_node *fnode;
939 struct tmpfs_node *tnode;
940 struct tmpfs_node *tdnode;
942 MPASS(VOP_ISLOCKED(tdvp));
943 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
944 MPASS(fcnp->cn_flags & HASBUF);
945 MPASS(tcnp->cn_flags & HASBUF);
947 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
949 /* Disallow cross-device renames.
950 * XXX Why isn't this done by the caller? */
951 if (fvp->v_mount != tdvp->v_mount ||
952 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
957 tmp = VFS_TO_TMPFS(tdvp->v_mount);
958 tdnode = VP_TO_TMPFS_DIR(tdvp);
960 /* If source and target are the same file, there is nothing to do. */
966 /* If we need to move the directory between entries, lock the
967 * source so that we can safely operate on it. */
969 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
973 fdnode = VP_TO_TMPFS_DIR(fdvp);
974 fnode = VP_TO_TMPFS_NODE(fvp);
975 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
977 /* Entry can disappear before we lock fdvp,
978 * also avoid manipulating '.' and '..' entries. */
980 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
981 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
987 MPASS(de->td_node == fnode);
989 /* If re-naming a directory to another preexisting directory
990 * ensure that the target directory is empty so that its
991 * removal causes no side effects.
992 * Kern_rename gurantees the destination to be a directory
993 * if the source is one. */
995 MPASS(tnode != NULL);
997 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
998 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
1003 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
1004 if (tnode->tn_size > 0) {
1008 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1011 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1015 MPASS(fnode->tn_type != VDIR &&
1016 tnode->tn_type != VDIR);
1020 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1021 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1026 /* Ensure that we have enough memory to hold the new name, if it
1027 * has to be changed. */
1028 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1029 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1030 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1034 /* If the node is being moved to another directory, we have to do
1036 if (fdnode != tdnode) {
1037 /* In case we are moving a directory, we have to adjust its
1038 * parent to point to the new parent. */
1039 if (de->td_node->tn_type == VDIR) {
1040 struct tmpfs_node *n;
1042 /* Ensure the target directory is not a child of the
1043 * directory being moved. Otherwise, we'd end up
1044 * with stale nodes. */
1046 /* TMPFS_LOCK garanties that no nodes are freed while
1047 * traversing the list. Nodes can only be marked as
1048 * removed: tn_parent == NULL. */
1051 while (n != n->tn_dir.tn_parent) {
1052 struct tmpfs_node *parent;
1055 TMPFS_NODE_UNLOCK(n);
1058 if (newname != NULL)
1059 free(newname, M_TMPFSNAME);
1062 parent = n->tn_dir.tn_parent;
1063 TMPFS_NODE_UNLOCK(n);
1064 if (parent == NULL) {
1068 TMPFS_NODE_LOCK(parent);
1069 if (parent->tn_dir.tn_parent == NULL) {
1070 TMPFS_NODE_UNLOCK(parent);
1079 if (newname != NULL)
1080 free(newname, M_TMPFSNAME);
1083 TMPFS_NODE_UNLOCK(n);
1085 /* Adjust the parent pointer. */
1086 TMPFS_VALIDATE_DIR(fnode);
1087 TMPFS_NODE_LOCK(de->td_node);
1088 de->td_node->tn_dir.tn_parent = tdnode;
1089 TMPFS_NODE_UNLOCK(de->td_node);
1091 /* As a result of changing the target of the '..'
1092 * entry, the link count of the source and target
1093 * directories has to be adjusted. */
1094 TMPFS_NODE_LOCK(tdnode);
1095 TMPFS_ASSERT_LOCKED(tdnode);
1097 TMPFS_NODE_UNLOCK(tdnode);
1099 TMPFS_NODE_LOCK(fdnode);
1100 TMPFS_ASSERT_LOCKED(fdnode);
1102 TMPFS_NODE_UNLOCK(fdnode);
1105 /* Do the move: just remove the entry from the source directory
1106 * and insert it into the target one. */
1107 tmpfs_dir_detach(fdvp, de);
1108 if (fcnp->cn_flags & DOWHITEOUT)
1109 tmpfs_dir_whiteout_add(fdvp, fcnp);
1110 if (tcnp->cn_flags & ISWHITEOUT)
1111 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1112 tmpfs_dir_attach(tdvp, de);
1115 /* If the name has changed, we need to make it effective by changing
1116 * it in the directory entry. */
1117 if (newname != NULL) {
1118 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1120 free(de->td_name, M_TMPFSNAME);
1121 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1122 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1123 de->td_name = newname;
1125 fnode->tn_status |= TMPFS_NODE_CHANGED;
1126 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1129 /* If we are overwriting an entry, we have to remove the old one
1130 * from the target directory. */
1132 /* Remove the old entry from the target directory. */
1133 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1134 tmpfs_dir_detach(tdvp, de);
1136 /* Free the directory entry we just deleted. Note that the
1137 * node referred by it will not be removed until the vnode is
1138 * really reclaimed. */
1139 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1145 if (fdnode != tdnode)
1146 VOP_UNLOCK(fdvp, 0);
1149 /* Release target nodes. */
1150 /* XXX: I don't understand when tdvp can be the same as tvp, but
1151 * other code takes care of this... */
1159 /* Release source nodes. */
1166 /* --------------------------------------------------------------------- */
1169 tmpfs_mkdir(struct vop_mkdir_args *v)
1171 struct vnode *dvp = v->a_dvp;
1172 struct vnode **vpp = v->a_vpp;
1173 struct componentname *cnp = v->a_cnp;
1174 struct vattr *vap = v->a_vap;
1176 MPASS(vap->va_type == VDIR);
1178 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1181 /* --------------------------------------------------------------------- */
1184 tmpfs_rmdir(struct vop_rmdir_args *v)
1186 struct vnode *dvp = v->a_dvp;
1187 struct vnode *vp = v->a_vp;
1190 struct tmpfs_dirent *de;
1191 struct tmpfs_mount *tmp;
1192 struct tmpfs_node *dnode;
1193 struct tmpfs_node *node;
1195 MPASS(VOP_ISLOCKED(dvp));
1196 MPASS(VOP_ISLOCKED(vp));
1198 tmp = VFS_TO_TMPFS(dvp->v_mount);
1199 dnode = VP_TO_TMPFS_DIR(dvp);
1200 node = VP_TO_TMPFS_DIR(vp);
1202 /* Directories with more than two entries ('.' and '..') cannot be
1204 if (node->tn_size > 0) {
1209 if ((dnode->tn_flags & APPEND)
1210 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1215 /* This invariant holds only if we are not trying to remove "..".
1216 * We checked for that above so this is safe now. */
1217 MPASS(node->tn_dir.tn_parent == dnode);
1219 /* Get the directory entry associated with node (vp). This was
1220 * filled by tmpfs_lookup while looking up the entry. */
1221 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1222 MPASS(TMPFS_DIRENT_MATCHES(de,
1223 v->a_cnp->cn_nameptr,
1224 v->a_cnp->cn_namelen));
1226 /* Check flags to see if we are allowed to remove the directory. */
1227 if (dnode->tn_flags & APPEND
1228 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1234 /* Detach the directory entry from the directory (dnode). */
1235 tmpfs_dir_detach(dvp, de);
1236 if (v->a_cnp->cn_flags & DOWHITEOUT)
1237 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1239 /* No vnode should be allocated for this entry from this point */
1240 TMPFS_NODE_LOCK(node);
1241 TMPFS_ASSERT_ELOCKED(node);
1243 node->tn_dir.tn_parent = NULL;
1244 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1245 TMPFS_NODE_MODIFIED;
1247 TMPFS_NODE_UNLOCK(node);
1249 TMPFS_NODE_LOCK(dnode);
1250 TMPFS_ASSERT_ELOCKED(dnode);
1252 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1253 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1254 TMPFS_NODE_UNLOCK(dnode);
1259 /* Free the directory entry we just deleted. Note that the node
1260 * referred by it will not be removed until the vnode is really
1262 tmpfs_free_dirent(tmp, de, TRUE);
1264 /* Release the deleted vnode (will destroy the node, notify
1265 * interested parties and clean it from the cache). */
1267 dnode->tn_status |= TMPFS_NODE_CHANGED;
1276 /* --------------------------------------------------------------------- */
1279 tmpfs_symlink(struct vop_symlink_args *v)
1281 struct vnode *dvp = v->a_dvp;
1282 struct vnode **vpp = v->a_vpp;
1283 struct componentname *cnp = v->a_cnp;
1284 struct vattr *vap = v->a_vap;
1285 char *target = v->a_target;
1287 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1288 MPASS(vap->va_type == VLNK);
1290 vap->va_type = VLNK;
1293 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1296 /* --------------------------------------------------------------------- */
1299 tmpfs_readdir(struct vop_readdir_args *v)
1301 struct vnode *vp = v->a_vp;
1302 struct uio *uio = v->a_uio;
1303 int *eofflag = v->a_eofflag;
1304 u_long **cookies = v->a_cookies;
1305 int *ncookies = v->a_ncookies;
1310 struct tmpfs_node *node;
1312 /* This operation only makes sense on directory nodes. */
1313 if (vp->v_type != VDIR)
1316 node = VP_TO_TMPFS_DIR(vp);
1318 startoff = uio->uio_offset;
1320 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1321 error = tmpfs_dir_getdotdent(node, uio);
1327 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1328 error = tmpfs_dir_getdotdotdent(node, uio);
1334 error = tmpfs_dir_getdents(node, uio, &cnt);
1340 error = (cnt != 0) ? 0 : EINVAL;
1342 if (eofflag != NULL)
1344 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1346 /* Update NFS-related variables. */
1347 if (error == 0 && cookies != NULL && ncookies != NULL) {
1349 off_t off = startoff;
1350 struct tmpfs_dirent *de = NULL;
1353 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1355 for (i = 0; i < cnt; i++) {
1356 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1357 if (off == TMPFS_DIRCOOKIE_DOT) {
1358 off = TMPFS_DIRCOOKIE_DOTDOT;
1360 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1361 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1362 } else if (de != NULL) {
1363 de = TAILQ_NEXT(de, td_entries);
1365 de = tmpfs_dir_lookupbycookie(node,
1368 de = TAILQ_NEXT(de, td_entries);
1371 off = TMPFS_DIRCOOKIE_EOF;
1373 off = tmpfs_dircookie(de);
1376 (*cookies)[i] = off;
1378 MPASS(uio->uio_offset == off);
1384 /* --------------------------------------------------------------------- */
1387 tmpfs_readlink(struct vop_readlink_args *v)
1389 struct vnode *vp = v->a_vp;
1390 struct uio *uio = v->a_uio;
1393 struct tmpfs_node *node;
1395 MPASS(uio->uio_offset == 0);
1396 MPASS(vp->v_type == VLNK);
1398 node = VP_TO_TMPFS_NODE(vp);
1400 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1402 node->tn_status |= TMPFS_NODE_ACCESSED;
1407 /* --------------------------------------------------------------------- */
1410 tmpfs_inactive(struct vop_inactive_args *v)
1412 struct vnode *vp = v->a_vp;
1413 struct thread *l = v->a_td;
1415 struct tmpfs_node *node;
1417 MPASS(VOP_ISLOCKED(vp));
1419 node = VP_TO_TMPFS_NODE(vp);
1421 if (node->tn_links == 0)
1427 /* --------------------------------------------------------------------- */
1430 tmpfs_reclaim(struct vop_reclaim_args *v)
1432 struct vnode *vp = v->a_vp;
1434 struct tmpfs_mount *tmp;
1435 struct tmpfs_node *node;
1437 node = VP_TO_TMPFS_NODE(vp);
1438 tmp = VFS_TO_TMPFS(vp->v_mount);
1440 vnode_destroy_vobject(vp);
1443 TMPFS_NODE_LOCK(node);
1444 TMPFS_ASSERT_ELOCKED(node);
1447 /* If the node referenced by this vnode was deleted by the user,
1448 * we must free its associated data structures (now that the vnode
1449 * is being reclaimed). */
1450 if (node->tn_links == 0 &&
1451 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1452 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1453 TMPFS_NODE_UNLOCK(node);
1454 tmpfs_free_node(tmp, node);
1456 TMPFS_NODE_UNLOCK(node);
1458 MPASS(vp->v_data == NULL);
1462 /* --------------------------------------------------------------------- */
1465 tmpfs_print(struct vop_print_args *v)
1467 struct vnode *vp = v->a_vp;
1469 struct tmpfs_node *node;
1471 node = VP_TO_TMPFS_NODE(vp);
1473 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1474 node, node->tn_flags, node->tn_links);
1475 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1477 node->tn_mode, node->tn_uid, node->tn_gid,
1478 (uintmax_t)node->tn_size, node->tn_status);
1480 if (vp->v_type == VFIFO)
1488 /* --------------------------------------------------------------------- */
1491 tmpfs_pathconf(struct vop_pathconf_args *v)
1493 int name = v->a_name;
1494 register_t *retval = v->a_retval;
1517 case _PC_CHOWN_RESTRICTED:
1529 case _PC_FILESIZEBITS:
1530 *retval = 0; /* XXX Don't know which value should I return. */
1541 tmpfs_vptofh(struct vop_vptofh_args *ap)
1543 struct tmpfs_fid *tfhp;
1544 struct tmpfs_node *node;
1546 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1547 node = VP_TO_TMPFS_NODE(ap->a_vp);
1549 tfhp->tf_len = sizeof(struct tmpfs_fid);
1550 tfhp->tf_id = node->tn_id;
1551 tfhp->tf_gen = node->tn_gen;
1557 tmpfs_whiteout(struct vop_whiteout_args *ap)
1559 struct vnode *dvp = ap->a_dvp;
1560 struct componentname *cnp = ap->a_cnp;
1561 struct tmpfs_dirent *de;
1563 switch (ap->a_flags) {
1567 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1569 return (de->td_node == NULL ? 0 : EEXIST);
1570 return (tmpfs_dir_whiteout_add(dvp, cnp));
1572 tmpfs_dir_whiteout_remove(dvp, cnp);
1575 panic("tmpfs_whiteout: unknown op");
1579 /* --------------------------------------------------------------------- */
1582 * vnode operations vector used for files stored in a tmpfs file system.
1584 struct vop_vector tmpfs_vnodeop_entries = {
1585 .vop_default = &default_vnodeops,
1586 .vop_lookup = vfs_cache_lookup,
1587 .vop_cachedlookup = tmpfs_lookup,
1588 .vop_create = tmpfs_create,
1589 .vop_mknod = tmpfs_mknod,
1590 .vop_open = tmpfs_open,
1591 .vop_close = tmpfs_close,
1592 .vop_access = tmpfs_access,
1593 .vop_getattr = tmpfs_getattr,
1594 .vop_setattr = tmpfs_setattr,
1595 .vop_read = tmpfs_read,
1596 .vop_write = tmpfs_write,
1597 .vop_fsync = tmpfs_fsync,
1598 .vop_remove = tmpfs_remove,
1599 .vop_link = tmpfs_link,
1600 .vop_rename = tmpfs_rename,
1601 .vop_mkdir = tmpfs_mkdir,
1602 .vop_rmdir = tmpfs_rmdir,
1603 .vop_symlink = tmpfs_symlink,
1604 .vop_readdir = tmpfs_readdir,
1605 .vop_readlink = tmpfs_readlink,
1606 .vop_inactive = tmpfs_inactive,
1607 .vop_reclaim = tmpfs_reclaim,
1608 .vop_print = tmpfs_print,
1609 .vop_pathconf = tmpfs_pathconf,
1610 .vop_vptofh = tmpfs_vptofh,
1611 .vop_whiteout = tmpfs_whiteout,
1612 .vop_bmap = VOP_EOPNOTSUPP,