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 if (cnp->cn_flags & ISDOTDOT) {
92 ltype = VOP_ISLOCKED(dvp);
95 /* Allocate a new vnode on the matching entry. */
96 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
97 cnp->cn_lkflags, vpp);
99 vn_lock(dvp, ltype | LK_RETRY);
101 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
106 de = tmpfs_dir_lookup(dnode, NULL, cnp);
107 if (de != NULL && de->td_node == NULL)
108 cnp->cn_flags |= ISWHITEOUT;
109 if (de == NULL || de->td_node == NULL) {
110 /* The entry was not found in the directory.
111 * This is OK if we are creating or renaming an
112 * entry and are working on the last component of
114 if ((cnp->cn_flags & ISLASTCN) &&
115 (cnp->cn_nameiop == CREATE || \
116 cnp->cn_nameiop == RENAME ||
117 (cnp->cn_nameiop == DELETE &&
118 cnp->cn_flags & DOWHITEOUT &&
119 cnp->cn_flags & ISWHITEOUT))) {
120 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
125 /* Keep the component name in the buffer for
127 cnp->cn_flags |= SAVENAME;
133 struct tmpfs_node *tnode;
135 /* The entry was found, so get its associated
139 /* If we are not at the last path component and
140 * found a non-directory or non-link entry (which
141 * may itself be pointing to a directory), raise
143 if ((tnode->tn_type != VDIR &&
144 tnode->tn_type != VLNK) &&
145 !(cnp->cn_flags & ISLASTCN)) {
150 /* If we are deleting or renaming the entry, keep
151 * track of its tmpfs_dirent so that it can be
152 * easily deleted later. */
153 if ((cnp->cn_flags & ISLASTCN) &&
154 (cnp->cn_nameiop == DELETE ||
155 cnp->cn_nameiop == RENAME)) {
156 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
161 /* Allocate a new vnode on the matching entry. */
162 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
163 cnp->cn_lkflags, vpp);
167 if ((dnode->tn_mode & S_ISTXT) &&
168 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
169 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
175 cnp->cn_flags |= SAVENAME;
177 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
178 cnp->cn_lkflags, vpp);
183 /* Store the result of this lookup in the cache. Avoid this if the
184 * request was for creation, as it does not improve timings on
186 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
187 cache_enter(dvp, *vpp, cnp);
190 /* If there were no errors, *vpp cannot be null and it must be
192 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
197 /* --------------------------------------------------------------------- */
200 tmpfs_create(struct vop_create_args *v)
202 struct vnode *dvp = v->a_dvp;
203 struct vnode **vpp = v->a_vpp;
204 struct componentname *cnp = v->a_cnp;
205 struct vattr *vap = v->a_vap;
207 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
209 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
211 /* --------------------------------------------------------------------- */
214 tmpfs_mknod(struct vop_mknod_args *v)
216 struct vnode *dvp = v->a_dvp;
217 struct vnode **vpp = v->a_vpp;
218 struct componentname *cnp = v->a_cnp;
219 struct vattr *vap = v->a_vap;
221 if (vap->va_type != VBLK && vap->va_type != VCHR &&
222 vap->va_type != VFIFO)
225 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
228 /* --------------------------------------------------------------------- */
231 tmpfs_open(struct vop_open_args *v)
233 struct vnode *vp = v->a_vp;
234 int mode = v->a_mode;
237 struct tmpfs_node *node;
239 MPASS(VOP_ISLOCKED(vp));
241 node = VP_TO_TMPFS_NODE(vp);
243 /* The file is still active but all its names have been removed
244 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
245 * it is about to die. */
246 if (node->tn_links < 1)
249 /* If the file is marked append-only, deny write requests. */
250 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
254 vnode_create_vobject(vp, node->tn_size, v->a_td);
257 MPASS(VOP_ISLOCKED(vp));
261 /* --------------------------------------------------------------------- */
264 tmpfs_close(struct vop_close_args *v)
266 struct vnode *vp = v->a_vp;
268 MPASS(VOP_ISLOCKED(vp));
270 /* Update node times. */
276 /* --------------------------------------------------------------------- */
279 tmpfs_access(struct vop_access_args *v)
281 struct vnode *vp = v->a_vp;
282 accmode_t accmode = v->a_accmode;
283 struct ucred *cred = v->a_cred;
286 struct tmpfs_node *node;
288 MPASS(VOP_ISLOCKED(vp));
290 node = VP_TO_TMPFS_NODE(vp);
292 switch (vp->v_type) {
298 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
318 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
323 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
324 node->tn_gid, accmode, cred, NULL);
327 MPASS(VOP_ISLOCKED(vp));
332 /* --------------------------------------------------------------------- */
335 tmpfs_getattr(struct vop_getattr_args *v)
337 struct vnode *vp = v->a_vp;
338 struct vattr *vap = v->a_vap;
340 struct tmpfs_node *node;
342 node = VP_TO_TMPFS_NODE(vp);
346 vap->va_type = vp->v_type;
347 vap->va_mode = node->tn_mode;
348 vap->va_nlink = node->tn_links;
349 vap->va_uid = node->tn_uid;
350 vap->va_gid = node->tn_gid;
351 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
352 vap->va_fileid = node->tn_id;
353 vap->va_size = node->tn_size;
354 vap->va_blocksize = PAGE_SIZE;
355 vap->va_atime = node->tn_atime;
356 vap->va_mtime = node->tn_mtime;
357 vap->va_ctime = node->tn_ctime;
358 vap->va_birthtime = node->tn_birthtime;
359 vap->va_gen = node->tn_gen;
360 vap->va_flags = node->tn_flags;
361 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
362 node->tn_rdev : NODEV;
363 vap->va_bytes = round_page(node->tn_size);
369 /* --------------------------------------------------------------------- */
371 /* XXX Should this operation be atomic? I think it should, but code in
372 * XXX other places (e.g., ufs) doesn't seem to be... */
374 tmpfs_setattr(struct vop_setattr_args *v)
376 struct vnode *vp = v->a_vp;
377 struct vattr *vap = v->a_vap;
378 struct ucred *cred = v->a_cred;
379 struct thread *td = curthread;
383 MPASS(VOP_ISLOCKED(vp));
387 /* Abort if any unsettable attribute is given. */
388 if (vap->va_type != VNON ||
389 vap->va_nlink != VNOVAL ||
390 vap->va_fsid != VNOVAL ||
391 vap->va_fileid != VNOVAL ||
392 vap->va_blocksize != VNOVAL ||
393 vap->va_gen != VNOVAL ||
394 vap->va_rdev != VNOVAL ||
395 vap->va_bytes != VNOVAL)
398 if (error == 0 && (vap->va_flags != VNOVAL))
399 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
401 if (error == 0 && (vap->va_size != VNOVAL))
402 error = tmpfs_chsize(vp, vap->va_size, cred, td);
404 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
405 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
407 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
408 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
410 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
411 vap->va_atime.tv_nsec != VNOVAL) ||
412 (vap->va_mtime.tv_sec != VNOVAL &&
413 vap->va_mtime.tv_nsec != VNOVAL) ||
414 (vap->va_birthtime.tv_sec != VNOVAL &&
415 vap->va_birthtime.tv_nsec != VNOVAL)))
416 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
417 &vap->va_birthtime, vap->va_vaflags, cred, td);
419 /* Update the node times. We give preference to the error codes
420 * generated by this function rather than the ones that may arise
421 * from tmpfs_update. */
424 MPASS(VOP_ISLOCKED(vp));
429 /* --------------------------------------------------------------------- */
431 tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
432 vm_offset_t offset, size_t tlen, struct uio *uio)
437 VM_OBJECT_LOCK(tobj);
438 vm_object_pip_add(tobj, 1);
439 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
440 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
441 if (m->valid != VM_PAGE_BITS_ALL) {
442 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
443 error = vm_pager_get_pages(tobj, &m, 1, 0);
445 printf("tmpfs get pages from pager error [read]\n");
449 vm_page_zero_invalid(m, TRUE);
451 VM_OBJECT_UNLOCK(tobj);
452 error = uiomove_fromphys(&m, offset, tlen, uio);
453 VM_OBJECT_LOCK(tobj);
455 vm_page_lock_queues();
456 vm_page_unwire(m, TRUE);
457 vm_page_unlock_queues();
459 vm_object_pip_subtract(tobj, 1);
460 VM_OBJECT_UNLOCK(tobj);
466 tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
467 vm_offset_t offset, size_t tlen, void *buf)
478 uio.uio_resid = tlen;
479 uio.uio_rw = UIO_READ;
480 uio.uio_segflg = UIO_SYSSPACE;
481 uio.uio_td = curthread;
483 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
487 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
498 addr = uio->uio_offset;
499 idx = OFF_TO_IDX(addr);
500 offset = addr & PAGE_MASK;
501 tlen = MIN(PAGE_SIZE - offset, len);
503 if ((vobj == NULL) || (vobj->resident_page_count == 0))
506 VM_OBJECT_LOCK(vobj);
508 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
509 vm_page_is_valid(m, offset, tlen)) {
510 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
513 VM_OBJECT_UNLOCK(vobj);
514 error = uiomove_fromphys(&m, offset, tlen, uio);
515 VM_OBJECT_LOCK(vobj);
517 VM_OBJECT_UNLOCK(vobj);
519 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
521 ("unexpected offset in tmpfs_mappedread for sendfile"));
522 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
525 VM_OBJECT_UNLOCK(vobj);
527 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
528 ma = (char *)sf_buf_kva(sf);
529 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
531 if (tlen != PAGE_SIZE)
532 bzero(ma + tlen, PAGE_SIZE - tlen);
533 uio->uio_offset += tlen;
534 uio->uio_resid -= tlen;
538 VM_OBJECT_LOCK(vobj);
540 m->valid = VM_PAGE_BITS_ALL;
542 VM_OBJECT_UNLOCK(vobj);
545 VM_OBJECT_UNLOCK(vobj);
547 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
553 tmpfs_read(struct vop_read_args *v)
555 struct vnode *vp = v->a_vp;
556 struct uio *uio = v->a_uio;
558 struct tmpfs_node *node;
565 node = VP_TO_TMPFS_NODE(vp);
567 if (vp->v_type != VREG) {
572 if (uio->uio_offset < 0) {
577 node->tn_status |= TMPFS_NODE_ACCESSED;
579 uobj = node->tn_reg.tn_aobj;
580 while ((resid = uio->uio_resid) > 0) {
582 if (node->tn_size <= uio->uio_offset)
584 len = MIN(node->tn_size - uio->uio_offset, resid);
587 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
588 if ((error != 0) || (resid == uio->uio_resid))
597 /* --------------------------------------------------------------------- */
600 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
611 addr = uio->uio_offset;
612 idx = OFF_TO_IDX(addr);
613 offset = addr & PAGE_MASK;
614 tlen = MIN(PAGE_SIZE - offset, len);
616 if ((vobj == NULL) || (vobj->resident_page_count == 0)) {
621 VM_OBJECT_LOCK(vobj);
623 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
624 vm_page_is_valid(vpg, offset, tlen)) {
625 if (vm_page_sleep_if_busy(vpg, FALSE, "tmfsmw"))
628 vm_page_lock_queues();
629 vm_page_undirty(vpg);
630 vm_page_unlock_queues();
631 VM_OBJECT_UNLOCK(vobj);
632 error = uiomove_fromphys(&vpg, offset, tlen, uio);
634 VM_OBJECT_UNLOCK(vobj);
638 VM_OBJECT_LOCK(tobj);
639 vm_object_pip_add(tobj, 1);
640 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
641 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
642 if (tpg->valid != VM_PAGE_BITS_ALL) {
643 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
644 error = vm_pager_get_pages(tobj, &tpg, 1, 0);
646 printf("tmpfs get pages from pager error [write]\n");
650 vm_page_zero_invalid(tpg, TRUE);
652 VM_OBJECT_UNLOCK(tobj);
654 error = uiomove_fromphys(&tpg, offset, tlen, uio);
656 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
657 pmap_copy_page(vpg, tpg);
659 VM_OBJECT_LOCK(tobj);
662 VM_OBJECT_LOCK(vobj);
663 vm_page_lock_queues();
665 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
666 ("parts of tpg invalid"));
669 vm_page_unwire(tpg, TRUE);
670 vm_page_unlock_queues();
675 VM_OBJECT_UNLOCK(vobj);
676 vm_object_pip_subtract(tobj, 1);
677 VM_OBJECT_UNLOCK(tobj);
683 tmpfs_write(struct vop_write_args *v)
685 struct vnode *vp = v->a_vp;
686 struct uio *uio = v->a_uio;
687 int ioflag = v->a_ioflag;
688 struct thread *td = uio->uio_td;
693 struct tmpfs_node *node;
698 node = VP_TO_TMPFS_NODE(vp);
699 oldsize = node->tn_size;
701 if (uio->uio_offset < 0 || vp->v_type != VREG) {
706 if (uio->uio_resid == 0) {
711 if (ioflag & IO_APPEND)
712 uio->uio_offset = node->tn_size;
714 if (uio->uio_offset + uio->uio_resid >
715 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
718 if (vn_rlimit_fsize(vp, uio, td))
721 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
723 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
728 uobj = node->tn_reg.tn_aobj;
729 while ((resid = uio->uio_resid) > 0) {
730 if (node->tn_size <= uio->uio_offset)
732 len = MIN(node->tn_size - uio->uio_offset, resid);
735 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
736 if ((error != 0) || (resid == uio->uio_resid))
740 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
741 (extended ? TMPFS_NODE_CHANGED : 0);
743 if (node->tn_mode & (S_ISUID | S_ISGID)) {
744 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
745 node->tn_mode &= ~(S_ISUID | S_ISGID);
749 (void)tmpfs_reg_resize(vp, oldsize);
752 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
753 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
758 /* --------------------------------------------------------------------- */
761 tmpfs_fsync(struct vop_fsync_args *v)
763 struct vnode *vp = v->a_vp;
765 MPASS(VOP_ISLOCKED(vp));
772 /* --------------------------------------------------------------------- */
775 tmpfs_remove(struct vop_remove_args *v)
777 struct vnode *dvp = v->a_dvp;
778 struct vnode *vp = v->a_vp;
781 struct tmpfs_dirent *de;
782 struct tmpfs_mount *tmp;
783 struct tmpfs_node *dnode;
784 struct tmpfs_node *node;
786 MPASS(VOP_ISLOCKED(dvp));
787 MPASS(VOP_ISLOCKED(vp));
789 if (vp->v_type == VDIR) {
794 dnode = VP_TO_TMPFS_DIR(dvp);
795 node = VP_TO_TMPFS_NODE(vp);
796 tmp = VFS_TO_TMPFS(vp->v_mount);
797 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
800 /* Files marked as immutable or append-only cannot be deleted. */
801 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
802 (dnode->tn_flags & APPEND)) {
807 /* Remove the entry from the directory; as it is a file, we do not
808 * have to change the number of hard links of the directory. */
809 tmpfs_dir_detach(dvp, de);
810 if (v->a_cnp->cn_flags & DOWHITEOUT)
811 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
813 /* Free the directory entry we just deleted. Note that the node
814 * referred by it will not be removed until the vnode is really
816 tmpfs_free_dirent(tmp, de, TRUE);
818 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
826 /* --------------------------------------------------------------------- */
829 tmpfs_link(struct vop_link_args *v)
831 struct vnode *dvp = v->a_tdvp;
832 struct vnode *vp = v->a_vp;
833 struct componentname *cnp = v->a_cnp;
836 struct tmpfs_dirent *de;
837 struct tmpfs_node *node;
839 MPASS(VOP_ISLOCKED(dvp));
840 MPASS(cnp->cn_flags & HASBUF);
841 MPASS(dvp != vp); /* XXX When can this be false? */
843 node = VP_TO_TMPFS_NODE(vp);
845 /* XXX: Why aren't the following two tests done by the caller? */
847 /* Hard links of directories are forbidden. */
848 if (vp->v_type == VDIR) {
853 /* Cannot create cross-device links. */
854 if (dvp->v_mount != vp->v_mount) {
859 /* Ensure that we do not overflow the maximum number of links imposed
861 MPASS(node->tn_links <= LINK_MAX);
862 if (node->tn_links == LINK_MAX) {
867 /* We cannot create links of files marked immutable or append-only. */
868 if (node->tn_flags & (IMMUTABLE | APPEND)) {
873 /* Allocate a new directory entry to represent the node. */
874 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
875 cnp->cn_nameptr, cnp->cn_namelen, &de);
879 /* Insert the new directory entry into the appropriate directory. */
880 if (cnp->cn_flags & ISWHITEOUT)
881 tmpfs_dir_whiteout_remove(dvp, cnp);
882 tmpfs_dir_attach(dvp, de);
884 /* vp link count has changed, so update node times. */
885 node->tn_status |= TMPFS_NODE_CHANGED;
894 /* --------------------------------------------------------------------- */
897 tmpfs_rename(struct vop_rename_args *v)
899 struct vnode *fdvp = v->a_fdvp;
900 struct vnode *fvp = v->a_fvp;
901 struct componentname *fcnp = v->a_fcnp;
902 struct vnode *tdvp = v->a_tdvp;
903 struct vnode *tvp = v->a_tvp;
904 struct componentname *tcnp = v->a_tcnp;
908 struct tmpfs_dirent *de;
909 struct tmpfs_node *fdnode;
910 struct tmpfs_node *fnode;
911 struct tmpfs_node *tnode;
912 struct tmpfs_node *tdnode;
914 MPASS(VOP_ISLOCKED(tdvp));
915 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
916 MPASS(fcnp->cn_flags & HASBUF);
917 MPASS(tcnp->cn_flags & HASBUF);
919 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
921 /* Disallow cross-device renames.
922 * XXX Why isn't this done by the caller? */
923 if (fvp->v_mount != tdvp->v_mount ||
924 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
929 tdnode = VP_TO_TMPFS_DIR(tdvp);
931 /* If source and target are the same file, there is nothing to do. */
937 /* If we need to move the directory between entries, lock the
938 * source so that we can safely operate on it. */
940 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
944 fdnode = VP_TO_TMPFS_DIR(fdvp);
945 fnode = VP_TO_TMPFS_NODE(fvp);
946 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
948 /* Entry can disappear before we lock fdvp,
949 * also avoid manipulating '.' and '..' entries. */
951 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
952 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
958 MPASS(de->td_node == fnode);
960 /* If re-naming a directory to another preexisting directory
961 * ensure that the target directory is empty so that its
962 * removal causes no side effects.
963 * Kern_rename gurantees the destination to be a directory
964 * if the source is one. */
966 MPASS(tnode != NULL);
968 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
969 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
974 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
975 if (tnode->tn_size > 0) {
979 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
982 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
986 MPASS(fnode->tn_type != VDIR &&
987 tnode->tn_type != VDIR);
991 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
992 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
997 /* Ensure that we have enough memory to hold the new name, if it
998 * has to be changed. */
999 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1000 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1001 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1005 /* If the node is being moved to another directory, we have to do
1007 if (fdnode != tdnode) {
1008 /* In case we are moving a directory, we have to adjust its
1009 * parent to point to the new parent. */
1010 if (de->td_node->tn_type == VDIR) {
1011 struct tmpfs_node *n;
1013 /* Ensure the target directory is not a child of the
1014 * directory being moved. Otherwise, we'd end up
1015 * with stale nodes. */
1017 while (n != n->tn_dir.tn_parent) {
1020 if (newname != NULL)
1021 free(newname, M_TMPFSNAME);
1024 n = n->tn_dir.tn_parent;
1027 /* Adjust the parent pointer. */
1028 TMPFS_VALIDATE_DIR(fnode);
1029 de->td_node->tn_dir.tn_parent = tdnode;
1031 /* As a result of changing the target of the '..'
1032 * entry, the link count of the source and target
1033 * directories has to be adjusted. */
1038 /* Do the move: just remove the entry from the source directory
1039 * and insert it into the target one. */
1040 tmpfs_dir_detach(fdvp, de);
1041 if (fcnp->cn_flags & DOWHITEOUT)
1042 tmpfs_dir_whiteout_add(fdvp, fcnp);
1043 if (tcnp->cn_flags & ISWHITEOUT)
1044 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1045 tmpfs_dir_attach(tdvp, de);
1048 /* If the name has changed, we need to make it effective by changing
1049 * it in the directory entry. */
1050 if (newname != NULL) {
1051 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1053 free(de->td_name, M_TMPFSNAME);
1054 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1055 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1056 de->td_name = newname;
1058 fnode->tn_status |= TMPFS_NODE_CHANGED;
1059 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1062 /* If we are overwriting an entry, we have to remove the old one
1063 * from the target directory. */
1065 /* Remove the old entry from the target directory. */
1066 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1067 tmpfs_dir_detach(tdvp, de);
1069 /* Free the directory entry we just deleted. Note that the
1070 * node referred by it will not be removed until the vnode is
1071 * really reclaimed. */
1072 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1081 if (fdnode != tdnode)
1082 VOP_UNLOCK(fdvp, 0);
1085 /* Release target nodes. */
1086 /* XXX: I don't understand when tdvp can be the same as tvp, but
1087 * other code takes care of this... */
1095 /* Release source nodes. */
1102 /* --------------------------------------------------------------------- */
1105 tmpfs_mkdir(struct vop_mkdir_args *v)
1107 struct vnode *dvp = v->a_dvp;
1108 struct vnode **vpp = v->a_vpp;
1109 struct componentname *cnp = v->a_cnp;
1110 struct vattr *vap = v->a_vap;
1112 MPASS(vap->va_type == VDIR);
1114 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1117 /* --------------------------------------------------------------------- */
1120 tmpfs_rmdir(struct vop_rmdir_args *v)
1122 struct vnode *dvp = v->a_dvp;
1123 struct vnode *vp = v->a_vp;
1126 struct tmpfs_dirent *de;
1127 struct tmpfs_mount *tmp;
1128 struct tmpfs_node *dnode;
1129 struct tmpfs_node *node;
1131 MPASS(VOP_ISLOCKED(dvp));
1132 MPASS(VOP_ISLOCKED(vp));
1134 tmp = VFS_TO_TMPFS(dvp->v_mount);
1135 dnode = VP_TO_TMPFS_DIR(dvp);
1136 node = VP_TO_TMPFS_DIR(vp);
1138 /* Directories with more than two entries ('.' and '..') cannot be
1140 if (node->tn_size > 0) {
1145 if ((dnode->tn_flags & APPEND)
1146 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1151 /* This invariant holds only if we are not trying to remove "..".
1152 * We checked for that above so this is safe now. */
1153 MPASS(node->tn_dir.tn_parent == dnode);
1155 /* Get the directory entry associated with node (vp). This was
1156 * filled by tmpfs_lookup while looking up the entry. */
1157 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1158 MPASS(TMPFS_DIRENT_MATCHES(de,
1159 v->a_cnp->cn_nameptr,
1160 v->a_cnp->cn_namelen));
1162 /* Check flags to see if we are allowed to remove the directory. */
1163 if (dnode->tn_flags & APPEND
1164 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1169 /* Detach the directory entry from the directory (dnode). */
1170 tmpfs_dir_detach(dvp, de);
1171 if (v->a_cnp->cn_flags & DOWHITEOUT)
1172 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1175 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1176 TMPFS_NODE_MODIFIED;
1177 node->tn_dir.tn_parent->tn_links--;
1178 node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1179 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1184 /* Free the directory entry we just deleted. Note that the node
1185 * referred by it will not be removed until the vnode is really
1187 tmpfs_free_dirent(tmp, de, TRUE);
1189 /* Release the deleted vnode (will destroy the node, notify
1190 * interested parties and clean it from the cache). */
1192 dnode->tn_status |= TMPFS_NODE_CHANGED;
1201 /* --------------------------------------------------------------------- */
1204 tmpfs_symlink(struct vop_symlink_args *v)
1206 struct vnode *dvp = v->a_dvp;
1207 struct vnode **vpp = v->a_vpp;
1208 struct componentname *cnp = v->a_cnp;
1209 struct vattr *vap = v->a_vap;
1210 char *target = v->a_target;
1212 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1213 MPASS(vap->va_type == VLNK);
1215 vap->va_type = VLNK;
1218 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1221 /* --------------------------------------------------------------------- */
1224 tmpfs_readdir(struct vop_readdir_args *v)
1226 struct vnode *vp = v->a_vp;
1227 struct uio *uio = v->a_uio;
1228 int *eofflag = v->a_eofflag;
1229 u_long **cookies = v->a_cookies;
1230 int *ncookies = v->a_ncookies;
1235 struct tmpfs_node *node;
1237 /* This operation only makes sense on directory nodes. */
1238 if (vp->v_type != VDIR)
1241 node = VP_TO_TMPFS_DIR(vp);
1243 startoff = uio->uio_offset;
1245 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1246 error = tmpfs_dir_getdotdent(node, uio);
1252 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1253 error = tmpfs_dir_getdotdotdent(node, uio);
1259 error = tmpfs_dir_getdents(node, uio, &cnt);
1265 error = (cnt != 0) ? 0 : EINVAL;
1267 if (eofflag != NULL)
1269 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1271 /* Update NFS-related variables. */
1272 if (error == 0 && cookies != NULL && ncookies != NULL) {
1274 off_t off = startoff;
1275 struct tmpfs_dirent *de = NULL;
1278 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1280 for (i = 0; i < cnt; i++) {
1281 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1282 if (off == TMPFS_DIRCOOKIE_DOT) {
1283 off = TMPFS_DIRCOOKIE_DOTDOT;
1285 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1286 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1287 } else if (de != NULL) {
1288 de = TAILQ_NEXT(de, td_entries);
1290 de = tmpfs_dir_lookupbycookie(node,
1293 de = TAILQ_NEXT(de, td_entries);
1296 off = TMPFS_DIRCOOKIE_EOF;
1298 off = tmpfs_dircookie(de);
1301 (*cookies)[i] = off;
1303 MPASS(uio->uio_offset == off);
1309 /* --------------------------------------------------------------------- */
1312 tmpfs_readlink(struct vop_readlink_args *v)
1314 struct vnode *vp = v->a_vp;
1315 struct uio *uio = v->a_uio;
1318 struct tmpfs_node *node;
1320 MPASS(uio->uio_offset == 0);
1321 MPASS(vp->v_type == VLNK);
1323 node = VP_TO_TMPFS_NODE(vp);
1325 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1327 node->tn_status |= TMPFS_NODE_ACCESSED;
1332 /* --------------------------------------------------------------------- */
1335 tmpfs_inactive(struct vop_inactive_args *v)
1337 struct vnode *vp = v->a_vp;
1338 struct thread *l = v->a_td;
1340 struct tmpfs_node *node;
1342 MPASS(VOP_ISLOCKED(vp));
1344 node = VP_TO_TMPFS_NODE(vp);
1346 if (node->tn_links == 0)
1352 /* --------------------------------------------------------------------- */
1355 tmpfs_reclaim(struct vop_reclaim_args *v)
1357 struct vnode *vp = v->a_vp;
1359 struct tmpfs_mount *tmp;
1360 struct tmpfs_node *node;
1362 node = VP_TO_TMPFS_NODE(vp);
1363 tmp = VFS_TO_TMPFS(vp->v_mount);
1365 vnode_destroy_vobject(vp);
1369 /* If the node referenced by this vnode was deleted by the user,
1370 * we must free its associated data structures (now that the vnode
1371 * is being reclaimed). */
1372 if (node->tn_links == 0)
1373 tmpfs_free_node(tmp, node);
1375 MPASS(vp->v_data == NULL);
1379 /* --------------------------------------------------------------------- */
1382 tmpfs_print(struct vop_print_args *v)
1384 struct vnode *vp = v->a_vp;
1386 struct tmpfs_node *node;
1388 node = VP_TO_TMPFS_NODE(vp);
1390 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1391 node, node->tn_flags, node->tn_links);
1392 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1394 node->tn_mode, node->tn_uid, node->tn_gid,
1395 (uintmax_t)node->tn_size, node->tn_status);
1397 if (vp->v_type == VFIFO)
1405 /* --------------------------------------------------------------------- */
1408 tmpfs_pathconf(struct vop_pathconf_args *v)
1410 int name = v->a_name;
1411 register_t *retval = v->a_retval;
1434 case _PC_CHOWN_RESTRICTED:
1446 case _PC_FILESIZEBITS:
1447 *retval = 0; /* XXX Don't know which value should I return. */
1458 tmpfs_vptofh(struct vop_vptofh_args *ap)
1460 struct tmpfs_fid *tfhp;
1461 struct tmpfs_node *node;
1463 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1464 node = VP_TO_TMPFS_NODE(ap->a_vp);
1466 tfhp->tf_len = sizeof(struct tmpfs_fid);
1467 tfhp->tf_id = node->tn_id;
1468 tfhp->tf_gen = node->tn_gen;
1474 tmpfs_whiteout(struct vop_whiteout_args *ap)
1476 struct vnode *dvp = ap->a_dvp;
1477 struct componentname *cnp = ap->a_cnp;
1478 struct tmpfs_dirent *de;
1480 switch (ap->a_flags) {
1484 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1486 return (de->td_node == NULL ? 0 : EEXIST);
1487 return (tmpfs_dir_whiteout_add(dvp, cnp));
1489 tmpfs_dir_whiteout_remove(dvp, cnp);
1492 panic("tmpfs_whiteout: unknown op");
1496 /* --------------------------------------------------------------------- */
1499 * vnode operations vector used for files stored in a tmpfs file system.
1501 struct vop_vector tmpfs_vnodeop_entries = {
1502 .vop_default = &default_vnodeops,
1503 .vop_lookup = vfs_cache_lookup,
1504 .vop_cachedlookup = tmpfs_lookup,
1505 .vop_create = tmpfs_create,
1506 .vop_mknod = tmpfs_mknod,
1507 .vop_open = tmpfs_open,
1508 .vop_close = tmpfs_close,
1509 .vop_access = tmpfs_access,
1510 .vop_getattr = tmpfs_getattr,
1511 .vop_setattr = tmpfs_setattr,
1512 .vop_read = tmpfs_read,
1513 .vop_write = tmpfs_write,
1514 .vop_fsync = tmpfs_fsync,
1515 .vop_remove = tmpfs_remove,
1516 .vop_link = tmpfs_link,
1517 .vop_rename = tmpfs_rename,
1518 .vop_mkdir = tmpfs_mkdir,
1519 .vop_rmdir = tmpfs_rmdir,
1520 .vop_symlink = tmpfs_symlink,
1521 .vop_readdir = tmpfs_readdir,
1522 .vop_readlink = tmpfs_readlink,
1523 .vop_inactive = tmpfs_inactive,
1524 .vop_reclaim = tmpfs_reclaim,
1525 .vop_print = tmpfs_print,
1526 .vop_pathconf = tmpfs_pathconf,
1527 .vop_vptofh = tmpfs_vptofh,
1528 .vop_whiteout = tmpfs_whiteout,
1529 .vop_bmap = VOP_EOPNOTSUPP,