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 struct tmpfs_node *node;
270 MPASS(VOP_ISLOCKED(vp));
272 node = VP_TO_TMPFS_NODE(vp);
274 if (node->tn_links > 0) {
275 /* Update node times. No need to do it if the node has
276 * been deleted, because it will vanish after we return. */
283 /* --------------------------------------------------------------------- */
286 tmpfs_access(struct vop_access_args *v)
288 struct vnode *vp = v->a_vp;
289 accmode_t accmode = v->a_accmode;
290 struct ucred *cred = v->a_cred;
293 struct tmpfs_node *node;
295 MPASS(VOP_ISLOCKED(vp));
297 node = VP_TO_TMPFS_NODE(vp);
299 switch (vp->v_type) {
305 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
325 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
330 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
331 node->tn_gid, accmode, cred, NULL);
334 MPASS(VOP_ISLOCKED(vp));
339 /* --------------------------------------------------------------------- */
342 tmpfs_getattr(struct vop_getattr_args *v)
344 struct vnode *vp = v->a_vp;
345 struct vattr *vap = v->a_vap;
347 struct tmpfs_node *node;
349 node = VP_TO_TMPFS_NODE(vp);
353 vap->va_type = vp->v_type;
354 vap->va_mode = node->tn_mode;
355 vap->va_nlink = node->tn_links;
356 vap->va_uid = node->tn_uid;
357 vap->va_gid = node->tn_gid;
358 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
359 vap->va_fileid = node->tn_id;
360 vap->va_size = node->tn_size;
361 vap->va_blocksize = PAGE_SIZE;
362 vap->va_atime = node->tn_atime;
363 vap->va_mtime = node->tn_mtime;
364 vap->va_ctime = node->tn_ctime;
365 vap->va_birthtime = node->tn_birthtime;
366 vap->va_gen = node->tn_gen;
367 vap->va_flags = node->tn_flags;
368 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
369 node->tn_rdev : NODEV;
370 vap->va_bytes = round_page(node->tn_size);
376 /* --------------------------------------------------------------------- */
378 /* XXX Should this operation be atomic? I think it should, but code in
379 * XXX other places (e.g., ufs) doesn't seem to be... */
381 tmpfs_setattr(struct vop_setattr_args *v)
383 struct vnode *vp = v->a_vp;
384 struct vattr *vap = v->a_vap;
385 struct ucred *cred = v->a_cred;
386 struct thread *td = curthread;
390 MPASS(VOP_ISLOCKED(vp));
394 /* Abort if any unsettable attribute is given. */
395 if (vap->va_type != VNON ||
396 vap->va_nlink != VNOVAL ||
397 vap->va_fsid != VNOVAL ||
398 vap->va_fileid != VNOVAL ||
399 vap->va_blocksize != VNOVAL ||
400 vap->va_gen != VNOVAL ||
401 vap->va_rdev != VNOVAL ||
402 vap->va_bytes != VNOVAL)
405 if (error == 0 && (vap->va_flags != VNOVAL))
406 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
408 if (error == 0 && (vap->va_size != VNOVAL))
409 error = tmpfs_chsize(vp, vap->va_size, cred, td);
411 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
412 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
414 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
415 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
417 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
418 vap->va_atime.tv_nsec != VNOVAL) ||
419 (vap->va_mtime.tv_sec != VNOVAL &&
420 vap->va_mtime.tv_nsec != VNOVAL) ||
421 (vap->va_birthtime.tv_sec != VNOVAL &&
422 vap->va_birthtime.tv_nsec != VNOVAL)))
423 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
424 &vap->va_birthtime, vap->va_vaflags, cred, td);
426 /* Update the node times. We give preference to the error codes
427 * generated by this function rather than the ones that may arise
428 * from tmpfs_update. */
431 MPASS(VOP_ISLOCKED(vp));
436 /* --------------------------------------------------------------------- */
438 tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
439 vm_offset_t offset, size_t tlen, struct uio *uio)
444 VM_OBJECT_LOCK(tobj);
445 vm_object_pip_add(tobj, 1);
446 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
447 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
448 if (m->valid != VM_PAGE_BITS_ALL) {
449 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
450 error = vm_pager_get_pages(tobj, &m, 1, 0);
452 printf("tmpfs get pages from pager error [read]\n");
456 vm_page_zero_invalid(m, TRUE);
458 VM_OBJECT_UNLOCK(tobj);
459 error = uiomove_fromphys(&m, offset, tlen, uio);
460 VM_OBJECT_LOCK(tobj);
462 vm_page_lock_queues();
463 vm_page_unwire(m, TRUE);
464 vm_page_unlock_queues();
466 vm_object_pip_subtract(tobj, 1);
467 VM_OBJECT_UNLOCK(tobj);
473 tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
474 vm_offset_t offset, size_t tlen, void *buf)
485 uio.uio_resid = tlen;
486 uio.uio_rw = UIO_READ;
487 uio.uio_segflg = UIO_SYSSPACE;
488 uio.uio_td = curthread;
490 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
494 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
505 addr = uio->uio_offset;
506 idx = OFF_TO_IDX(addr);
507 offset = addr & PAGE_MASK;
508 tlen = MIN(PAGE_SIZE - offset, len);
510 if ((vobj == NULL) || (vobj->resident_page_count == 0))
513 VM_OBJECT_LOCK(vobj);
515 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
516 vm_page_is_valid(m, offset, tlen)) {
517 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
520 VM_OBJECT_UNLOCK(vobj);
521 error = uiomove_fromphys(&m, offset, tlen, uio);
522 VM_OBJECT_LOCK(vobj);
524 VM_OBJECT_UNLOCK(vobj);
526 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
528 ("unexpected offset in tmpfs_mappedread for sendfile"));
529 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
532 VM_OBJECT_UNLOCK(vobj);
534 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
535 ma = (char *)sf_buf_kva(sf);
536 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
538 if (tlen != PAGE_SIZE)
539 bzero(ma + tlen, PAGE_SIZE - tlen);
540 uio->uio_offset += tlen;
541 uio->uio_resid -= tlen;
545 VM_OBJECT_LOCK(vobj);
547 m->valid = VM_PAGE_BITS_ALL;
549 VM_OBJECT_UNLOCK(vobj);
552 VM_OBJECT_UNLOCK(vobj);
554 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
560 tmpfs_read(struct vop_read_args *v)
562 struct vnode *vp = v->a_vp;
563 struct uio *uio = v->a_uio;
565 struct tmpfs_node *node;
572 node = VP_TO_TMPFS_NODE(vp);
574 if (vp->v_type != VREG) {
579 if (uio->uio_offset < 0) {
584 node->tn_status |= TMPFS_NODE_ACCESSED;
586 uobj = node->tn_reg.tn_aobj;
587 while ((resid = uio->uio_resid) > 0) {
589 if (node->tn_size <= uio->uio_offset)
591 len = MIN(node->tn_size - uio->uio_offset, resid);
594 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
595 if ((error != 0) || (resid == uio->uio_resid))
604 /* --------------------------------------------------------------------- */
607 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
618 addr = uio->uio_offset;
619 idx = OFF_TO_IDX(addr);
620 offset = addr & PAGE_MASK;
621 tlen = MIN(PAGE_SIZE - offset, len);
623 if ((vobj == NULL) || (vobj->resident_page_count == 0)) {
628 VM_OBJECT_LOCK(vobj);
630 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
631 vm_page_is_valid(vpg, offset, tlen)) {
632 if (vm_page_sleep_if_busy(vpg, FALSE, "tmfsmw"))
635 vm_page_lock_queues();
636 vm_page_undirty(vpg);
637 vm_page_unlock_queues();
638 VM_OBJECT_UNLOCK(vobj);
639 error = uiomove_fromphys(&vpg, offset, tlen, uio);
641 VM_OBJECT_UNLOCK(vobj);
645 VM_OBJECT_LOCK(tobj);
646 vm_object_pip_add(tobj, 1);
647 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
648 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
649 if (tpg->valid != VM_PAGE_BITS_ALL) {
650 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
651 error = vm_pager_get_pages(tobj, &tpg, 1, 0);
653 printf("tmpfs get pages from pager error [write]\n");
657 vm_page_zero_invalid(tpg, TRUE);
659 VM_OBJECT_UNLOCK(tobj);
661 error = uiomove_fromphys(&tpg, offset, tlen, uio);
663 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
664 pmap_copy_page(vpg, tpg);
666 VM_OBJECT_LOCK(tobj);
669 VM_OBJECT_LOCK(vobj);
670 vm_page_lock_queues();
672 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
673 ("parts of tpg invalid"));
676 vm_page_unwire(tpg, TRUE);
677 vm_page_unlock_queues();
682 VM_OBJECT_UNLOCK(vobj);
683 vm_object_pip_subtract(tobj, 1);
684 VM_OBJECT_UNLOCK(tobj);
690 tmpfs_write(struct vop_write_args *v)
692 struct vnode *vp = v->a_vp;
693 struct uio *uio = v->a_uio;
694 int ioflag = v->a_ioflag;
695 struct thread *td = uio->uio_td;
700 struct tmpfs_node *node;
705 node = VP_TO_TMPFS_NODE(vp);
706 oldsize = node->tn_size;
708 if (uio->uio_offset < 0 || vp->v_type != VREG) {
713 if (uio->uio_resid == 0) {
718 if (ioflag & IO_APPEND)
719 uio->uio_offset = node->tn_size;
721 if (uio->uio_offset + uio->uio_resid >
722 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
725 if (vn_rlimit_fsize(vp, uio, td))
728 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
730 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
735 uobj = node->tn_reg.tn_aobj;
736 while ((resid = uio->uio_resid) > 0) {
737 if (node->tn_size <= uio->uio_offset)
739 len = MIN(node->tn_size - uio->uio_offset, resid);
742 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
743 if ((error != 0) || (resid == uio->uio_resid))
747 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
748 (extended ? TMPFS_NODE_CHANGED : 0);
750 if (node->tn_mode & (S_ISUID | S_ISGID)) {
751 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
752 node->tn_mode &= ~(S_ISUID | S_ISGID);
756 (void)tmpfs_reg_resize(vp, oldsize);
759 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
760 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
765 /* --------------------------------------------------------------------- */
768 tmpfs_fsync(struct vop_fsync_args *v)
770 struct vnode *vp = v->a_vp;
772 MPASS(VOP_ISLOCKED(vp));
779 /* --------------------------------------------------------------------- */
782 tmpfs_remove(struct vop_remove_args *v)
784 struct vnode *dvp = v->a_dvp;
785 struct vnode *vp = v->a_vp;
788 struct tmpfs_dirent *de;
789 struct tmpfs_mount *tmp;
790 struct tmpfs_node *dnode;
791 struct tmpfs_node *node;
793 MPASS(VOP_ISLOCKED(dvp));
794 MPASS(VOP_ISLOCKED(vp));
796 if (vp->v_type == VDIR) {
801 dnode = VP_TO_TMPFS_DIR(dvp);
802 node = VP_TO_TMPFS_NODE(vp);
803 tmp = VFS_TO_TMPFS(vp->v_mount);
804 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
807 /* Files marked as immutable or append-only cannot be deleted. */
808 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
809 (dnode->tn_flags & APPEND)) {
814 /* Remove the entry from the directory; as it is a file, we do not
815 * have to change the number of hard links of the directory. */
816 tmpfs_dir_detach(dvp, de);
817 if (v->a_cnp->cn_flags & DOWHITEOUT)
818 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
820 /* Free the directory entry we just deleted. Note that the node
821 * referred by it will not be removed until the vnode is really
823 tmpfs_free_dirent(tmp, de, TRUE);
825 if (node->tn_links > 0)
826 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
834 /* --------------------------------------------------------------------- */
837 tmpfs_link(struct vop_link_args *v)
839 struct vnode *dvp = v->a_tdvp;
840 struct vnode *vp = v->a_vp;
841 struct componentname *cnp = v->a_cnp;
844 struct tmpfs_dirent *de;
845 struct tmpfs_node *node;
847 MPASS(VOP_ISLOCKED(dvp));
848 MPASS(cnp->cn_flags & HASBUF);
849 MPASS(dvp != vp); /* XXX When can this be false? */
851 node = VP_TO_TMPFS_NODE(vp);
853 /* XXX: Why aren't the following two tests done by the caller? */
855 /* Hard links of directories are forbidden. */
856 if (vp->v_type == VDIR) {
861 /* Cannot create cross-device links. */
862 if (dvp->v_mount != vp->v_mount) {
867 /* Ensure that we do not overflow the maximum number of links imposed
869 MPASS(node->tn_links <= LINK_MAX);
870 if (node->tn_links == LINK_MAX) {
875 /* We cannot create links of files marked immutable or append-only. */
876 if (node->tn_flags & (IMMUTABLE | APPEND)) {
881 /* Allocate a new directory entry to represent the node. */
882 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
883 cnp->cn_nameptr, cnp->cn_namelen, &de);
887 /* Insert the new directory entry into the appropriate directory. */
888 if (cnp->cn_flags & ISWHITEOUT)
889 tmpfs_dir_whiteout_remove(dvp, cnp);
890 tmpfs_dir_attach(dvp, de);
892 /* vp link count has changed, so update node times. */
893 node->tn_status |= TMPFS_NODE_CHANGED;
902 /* --------------------------------------------------------------------- */
905 tmpfs_rename(struct vop_rename_args *v)
907 struct vnode *fdvp = v->a_fdvp;
908 struct vnode *fvp = v->a_fvp;
909 struct componentname *fcnp = v->a_fcnp;
910 struct vnode *tdvp = v->a_tdvp;
911 struct vnode *tvp = v->a_tvp;
912 struct componentname *tcnp = v->a_tcnp;
916 struct tmpfs_dirent *de;
917 struct tmpfs_node *fdnode;
918 struct tmpfs_node *fnode;
919 struct tmpfs_node *tnode;
920 struct tmpfs_node *tdnode;
922 MPASS(VOP_ISLOCKED(tdvp));
923 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
924 MPASS(fcnp->cn_flags & HASBUF);
925 MPASS(tcnp->cn_flags & HASBUF);
927 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
929 /* Disallow cross-device renames.
930 * XXX Why isn't this done by the caller? */
931 if (fvp->v_mount != tdvp->v_mount ||
932 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
937 tdnode = VP_TO_TMPFS_DIR(tdvp);
939 /* If source and target are the same file, there is nothing to do. */
945 /* If we need to move the directory between entries, lock the
946 * source so that we can safely operate on it. */
948 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
952 fdnode = VP_TO_TMPFS_DIR(fdvp);
953 fnode = VP_TO_TMPFS_NODE(fvp);
954 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
956 /* Entry can disappear before we lock fdvp,
957 * also avoid manipulating '.' and '..' entries. */
959 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
960 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
966 MPASS(de->td_node == fnode);
968 /* If re-naming a directory to another preexisting directory
969 * ensure that the target directory is empty so that its
970 * removal causes no side effects.
971 * Kern_rename gurantees the destination to be a directory
972 * if the source is one. */
974 MPASS(tnode != NULL);
976 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
977 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
982 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
983 if (tnode->tn_size > 0) {
987 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
990 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
994 MPASS(fnode->tn_type != VDIR &&
995 tnode->tn_type != VDIR);
999 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1000 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1005 /* Ensure that we have enough memory to hold the new name, if it
1006 * has to be changed. */
1007 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1008 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1009 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1013 /* If the node is being moved to another directory, we have to do
1015 if (fdnode != tdnode) {
1016 /* In case we are moving a directory, we have to adjust its
1017 * parent to point to the new parent. */
1018 if (de->td_node->tn_type == VDIR) {
1019 struct tmpfs_node *n;
1021 /* Ensure the target directory is not a child of the
1022 * directory being moved. Otherwise, we'd end up
1023 * with stale nodes. */
1025 while (n != n->tn_dir.tn_parent) {
1028 if (newname != NULL)
1029 free(newname, M_TMPFSNAME);
1032 n = n->tn_dir.tn_parent;
1035 /* Adjust the parent pointer. */
1036 TMPFS_VALIDATE_DIR(fnode);
1037 de->td_node->tn_dir.tn_parent = tdnode;
1039 /* As a result of changing the target of the '..'
1040 * entry, the link count of the source and target
1041 * directories has to be adjusted. */
1046 /* Do the move: just remove the entry from the source directory
1047 * and insert it into the target one. */
1048 tmpfs_dir_detach(fdvp, de);
1049 if (fcnp->cn_flags & DOWHITEOUT)
1050 tmpfs_dir_whiteout_add(fdvp, fcnp);
1051 if (tcnp->cn_flags & ISWHITEOUT)
1052 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1053 tmpfs_dir_attach(tdvp, de);
1056 /* If the name has changed, we need to make it effective by changing
1057 * it in the directory entry. */
1058 if (newname != NULL) {
1059 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1061 free(de->td_name, M_TMPFSNAME);
1062 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1063 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1064 de->td_name = newname;
1066 fnode->tn_status |= TMPFS_NODE_CHANGED;
1067 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1070 /* If we are overwriting an entry, we have to remove the old one
1071 * from the target directory. */
1073 /* Remove the old entry from the target directory. */
1074 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1075 tmpfs_dir_detach(tdvp, de);
1077 /* Free the directory entry we just deleted. Note that the
1078 * node referred by it will not be removed until the vnode is
1079 * really reclaimed. */
1080 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1087 if (fdnode != tdnode)
1088 VOP_UNLOCK(fdvp, 0);
1091 /* Release target nodes. */
1092 /* XXX: I don't understand when tdvp can be the same as tvp, but
1093 * other code takes care of this... */
1101 /* Release source nodes. */
1108 /* --------------------------------------------------------------------- */
1111 tmpfs_mkdir(struct vop_mkdir_args *v)
1113 struct vnode *dvp = v->a_dvp;
1114 struct vnode **vpp = v->a_vpp;
1115 struct componentname *cnp = v->a_cnp;
1116 struct vattr *vap = v->a_vap;
1118 MPASS(vap->va_type == VDIR);
1120 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1123 /* --------------------------------------------------------------------- */
1126 tmpfs_rmdir(struct vop_rmdir_args *v)
1128 struct vnode *dvp = v->a_dvp;
1129 struct vnode *vp = v->a_vp;
1132 struct tmpfs_dirent *de;
1133 struct tmpfs_mount *tmp;
1134 struct tmpfs_node *dnode;
1135 struct tmpfs_node *node;
1137 MPASS(VOP_ISLOCKED(dvp));
1138 MPASS(VOP_ISLOCKED(vp));
1140 tmp = VFS_TO_TMPFS(dvp->v_mount);
1141 dnode = VP_TO_TMPFS_DIR(dvp);
1142 node = VP_TO_TMPFS_DIR(vp);
1144 /* Directories with more than two entries ('.' and '..') cannot be
1146 if (node->tn_size > 0) {
1151 if ((dnode->tn_flags & APPEND)
1152 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1157 /* This invariant holds only if we are not trying to remove "..".
1158 * We checked for that above so this is safe now. */
1159 MPASS(node->tn_dir.tn_parent == dnode);
1161 /* Get the directory entry associated with node (vp). This was
1162 * filled by tmpfs_lookup while looking up the entry. */
1163 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1164 MPASS(TMPFS_DIRENT_MATCHES(de,
1165 v->a_cnp->cn_nameptr,
1166 v->a_cnp->cn_namelen));
1168 /* Check flags to see if we are allowed to remove the directory. */
1169 if (dnode->tn_flags & APPEND
1170 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1175 /* Detach the directory entry from the directory (dnode). */
1176 tmpfs_dir_detach(dvp, de);
1177 if (v->a_cnp->cn_flags & DOWHITEOUT)
1178 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1181 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1182 TMPFS_NODE_MODIFIED;
1183 node->tn_dir.tn_parent->tn_links--;
1184 node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1185 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1190 /* Free the directory entry we just deleted. Note that the node
1191 * referred by it will not be removed until the vnode is really
1193 tmpfs_free_dirent(tmp, de, TRUE);
1195 /* Release the deleted vnode (will destroy the node, notify
1196 * interested parties and clean it from the cache). */
1198 dnode->tn_status |= TMPFS_NODE_CHANGED;
1207 /* --------------------------------------------------------------------- */
1210 tmpfs_symlink(struct vop_symlink_args *v)
1212 struct vnode *dvp = v->a_dvp;
1213 struct vnode **vpp = v->a_vpp;
1214 struct componentname *cnp = v->a_cnp;
1215 struct vattr *vap = v->a_vap;
1216 char *target = v->a_target;
1218 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1219 MPASS(vap->va_type == VLNK);
1221 vap->va_type = VLNK;
1224 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1227 /* --------------------------------------------------------------------- */
1230 tmpfs_readdir(struct vop_readdir_args *v)
1232 struct vnode *vp = v->a_vp;
1233 struct uio *uio = v->a_uio;
1234 int *eofflag = v->a_eofflag;
1235 u_long **cookies = v->a_cookies;
1236 int *ncookies = v->a_ncookies;
1241 struct tmpfs_node *node;
1243 /* This operation only makes sense on directory nodes. */
1244 if (vp->v_type != VDIR)
1247 node = VP_TO_TMPFS_DIR(vp);
1249 startoff = uio->uio_offset;
1251 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1252 error = tmpfs_dir_getdotdent(node, uio);
1258 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1259 error = tmpfs_dir_getdotdotdent(node, uio);
1265 error = tmpfs_dir_getdents(node, uio, &cnt);
1271 error = (cnt != 0) ? 0 : EINVAL;
1273 if (eofflag != NULL)
1275 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1277 /* Update NFS-related variables. */
1278 if (error == 0 && cookies != NULL && ncookies != NULL) {
1280 off_t off = startoff;
1281 struct tmpfs_dirent *de = NULL;
1284 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1286 for (i = 0; i < cnt; i++) {
1287 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1288 if (off == TMPFS_DIRCOOKIE_DOT) {
1289 off = TMPFS_DIRCOOKIE_DOTDOT;
1291 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1292 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1293 } else if (de != NULL) {
1294 de = TAILQ_NEXT(de, td_entries);
1296 de = tmpfs_dir_lookupbycookie(node,
1299 de = TAILQ_NEXT(de, td_entries);
1302 off = TMPFS_DIRCOOKIE_EOF;
1304 off = tmpfs_dircookie(de);
1307 (*cookies)[i] = off;
1309 MPASS(uio->uio_offset == off);
1315 /* --------------------------------------------------------------------- */
1318 tmpfs_readlink(struct vop_readlink_args *v)
1320 struct vnode *vp = v->a_vp;
1321 struct uio *uio = v->a_uio;
1324 struct tmpfs_node *node;
1326 MPASS(uio->uio_offset == 0);
1327 MPASS(vp->v_type == VLNK);
1329 node = VP_TO_TMPFS_NODE(vp);
1331 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1333 node->tn_status |= TMPFS_NODE_ACCESSED;
1338 /* --------------------------------------------------------------------- */
1341 tmpfs_inactive(struct vop_inactive_args *v)
1343 struct vnode *vp = v->a_vp;
1344 struct thread *l = v->a_td;
1346 struct tmpfs_node *node;
1348 MPASS(VOP_ISLOCKED(vp));
1350 node = VP_TO_TMPFS_NODE(vp);
1352 if (node->tn_links == 0)
1358 /* --------------------------------------------------------------------- */
1361 tmpfs_reclaim(struct vop_reclaim_args *v)
1363 struct vnode *vp = v->a_vp;
1365 struct tmpfs_mount *tmp;
1366 struct tmpfs_node *node;
1368 node = VP_TO_TMPFS_NODE(vp);
1369 tmp = VFS_TO_TMPFS(vp->v_mount);
1371 vnode_destroy_vobject(vp);
1375 /* If the node referenced by this vnode was deleted by the user,
1376 * we must free its associated data structures (now that the vnode
1377 * is being reclaimed). */
1378 if (node->tn_links == 0)
1379 tmpfs_free_node(tmp, node);
1381 MPASS(vp->v_data == NULL);
1385 /* --------------------------------------------------------------------- */
1388 tmpfs_print(struct vop_print_args *v)
1390 struct vnode *vp = v->a_vp;
1392 struct tmpfs_node *node;
1394 node = VP_TO_TMPFS_NODE(vp);
1396 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1397 node, node->tn_flags, node->tn_links);
1398 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1400 node->tn_mode, node->tn_uid, node->tn_gid,
1401 (uintmax_t)node->tn_size, node->tn_status);
1403 if (vp->v_type == VFIFO)
1411 /* --------------------------------------------------------------------- */
1414 tmpfs_pathconf(struct vop_pathconf_args *v)
1416 int name = v->a_name;
1417 register_t *retval = v->a_retval;
1440 case _PC_CHOWN_RESTRICTED:
1452 case _PC_FILESIZEBITS:
1453 *retval = 0; /* XXX Don't know which value should I return. */
1464 tmpfs_vptofh(struct vop_vptofh_args *ap)
1466 struct tmpfs_fid *tfhp;
1467 struct tmpfs_node *node;
1469 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1470 node = VP_TO_TMPFS_NODE(ap->a_vp);
1472 tfhp->tf_len = sizeof(struct tmpfs_fid);
1473 tfhp->tf_id = node->tn_id;
1474 tfhp->tf_gen = node->tn_gen;
1480 tmpfs_whiteout(struct vop_whiteout_args *ap)
1482 struct vnode *dvp = ap->a_dvp;
1483 struct componentname *cnp = ap->a_cnp;
1484 struct tmpfs_dirent *de;
1486 switch (ap->a_flags) {
1490 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1492 return (de->td_node == NULL ? 0 : EEXIST);
1493 return (tmpfs_dir_whiteout_add(dvp, cnp));
1495 tmpfs_dir_whiteout_remove(dvp, cnp);
1498 panic("tmpfs_whiteout: unknown op");
1502 /* --------------------------------------------------------------------- */
1505 * vnode operations vector used for files stored in a tmpfs file system.
1507 struct vop_vector tmpfs_vnodeop_entries = {
1508 .vop_default = &default_vnodeops,
1509 .vop_lookup = vfs_cache_lookup,
1510 .vop_cachedlookup = tmpfs_lookup,
1511 .vop_create = tmpfs_create,
1512 .vop_mknod = tmpfs_mknod,
1513 .vop_open = tmpfs_open,
1514 .vop_close = tmpfs_close,
1515 .vop_access = tmpfs_access,
1516 .vop_getattr = tmpfs_getattr,
1517 .vop_setattr = tmpfs_setattr,
1518 .vop_read = tmpfs_read,
1519 .vop_write = tmpfs_write,
1520 .vop_fsync = tmpfs_fsync,
1521 .vop_remove = tmpfs_remove,
1522 .vop_link = tmpfs_link,
1523 .vop_rename = tmpfs_rename,
1524 .vop_mkdir = tmpfs_mkdir,
1525 .vop_rmdir = tmpfs_rmdir,
1526 .vop_symlink = tmpfs_symlink,
1527 .vop_readdir = tmpfs_readdir,
1528 .vop_readlink = tmpfs_readlink,
1529 .vop_inactive = tmpfs_inactive,
1530 .vop_reclaim = tmpfs_reclaim,
1531 .vop_print = tmpfs_print,
1532 .vop_pathconf = tmpfs_pathconf,
1533 .vop_vptofh = tmpfs_vptofh,
1534 .vop_whiteout = tmpfs_whiteout,
1535 .vop_bmap = VOP_EOPNOTSUPP,