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/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/sf_buf.h>
49 #include <sys/systm.h>
50 #include <sys/unistd.h>
51 #include <sys/vnode.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_pager.h>
58 #include <machine/_inttypes.h>
60 #include <fs/fifofs/fifo.h>
61 #include <fs/tmpfs/tmpfs_vnops.h>
62 #include <fs/tmpfs/tmpfs.h>
64 /* --------------------------------------------------------------------- */
67 tmpfs_lookup(struct vop_cachedlookup_args *v)
69 struct vnode *dvp = v->a_dvp;
70 struct vnode **vpp = v->a_vpp;
71 struct componentname *cnp = v->a_cnp;
74 struct tmpfs_dirent *de;
75 struct tmpfs_node *dnode;
77 dnode = VP_TO_TMPFS_DIR(dvp);
80 /* Check accessibility of requested node as a first step. */
81 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
85 /* We cannot be requesting the parent directory of the root node. */
86 MPASS(IMPLIES(dnode->tn_type == VDIR &&
87 dnode->tn_dir.tn_parent == dnode,
88 !(cnp->cn_flags & ISDOTDOT)));
90 if (cnp->cn_flags & ISDOTDOT) {
93 ltype = VOP_ISLOCKED(dvp);
96 /* Allocate a new vnode on the matching entry. */
97 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
98 cnp->cn_lkflags, vpp);
100 vn_lock(dvp, ltype | LK_RETRY);
102 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
107 de = tmpfs_dir_lookup(dnode, NULL, cnp);
108 if (de != NULL && de->td_node == NULL)
109 cnp->cn_flags |= ISWHITEOUT;
110 if (de == NULL || de->td_node == NULL) {
111 /* The entry was not found in the directory.
112 * This is OK if we are creating or renaming an
113 * entry and are working on the last component of
115 if ((cnp->cn_flags & ISLASTCN) &&
116 (cnp->cn_nameiop == CREATE || \
117 cnp->cn_nameiop == RENAME ||
118 (cnp->cn_nameiop == DELETE &&
119 cnp->cn_flags & DOWHITEOUT &&
120 cnp->cn_flags & ISWHITEOUT))) {
121 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
126 /* Keep the component name in the buffer for
128 cnp->cn_flags |= SAVENAME;
134 struct tmpfs_node *tnode;
136 /* The entry was found, so get its associated
140 /* If we are not at the last path component and
141 * found a non-directory or non-link entry (which
142 * may itself be pointing to a directory), raise
144 if ((tnode->tn_type != VDIR &&
145 tnode->tn_type != VLNK) &&
146 !(cnp->cn_flags & ISLASTCN)) {
151 /* If we are deleting or renaming the entry, keep
152 * track of its tmpfs_dirent so that it can be
153 * easily deleted later. */
154 if ((cnp->cn_flags & ISLASTCN) &&
155 (cnp->cn_nameiop == DELETE ||
156 cnp->cn_nameiop == RENAME)) {
157 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
162 /* Allocate a new vnode on the matching entry. */
163 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
164 cnp->cn_lkflags, vpp);
168 if ((dnode->tn_mode & S_ISTXT) &&
169 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred, cnp->cn_thread) &&
170 VOP_ACCESS(*vpp, VADMIN, cnp->cn_cred, cnp->cn_thread)) {
176 cnp->cn_flags |= SAVENAME;
178 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
179 cnp->cn_lkflags, vpp);
184 /* Store the result of this lookup in the cache. Avoid this if the
185 * request was for creation, as it does not improve timings on
187 if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE)
188 cache_enter(dvp, *vpp, cnp);
191 /* If there were no errors, *vpp cannot be null and it must be
193 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
198 /* --------------------------------------------------------------------- */
201 tmpfs_create(struct vop_create_args *v)
203 struct vnode *dvp = v->a_dvp;
204 struct vnode **vpp = v->a_vpp;
205 struct componentname *cnp = v->a_cnp;
206 struct vattr *vap = v->a_vap;
208 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
210 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
212 /* --------------------------------------------------------------------- */
215 tmpfs_mknod(struct vop_mknod_args *v)
217 struct vnode *dvp = v->a_dvp;
218 struct vnode **vpp = v->a_vpp;
219 struct componentname *cnp = v->a_cnp;
220 struct vattr *vap = v->a_vap;
222 if (vap->va_type != VBLK && vap->va_type != VCHR &&
223 vap->va_type != VFIFO)
226 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
229 /* --------------------------------------------------------------------- */
232 tmpfs_open(struct vop_open_args *v)
234 struct vnode *vp = v->a_vp;
235 int mode = v->a_mode;
238 struct tmpfs_node *node;
240 MPASS(VOP_ISLOCKED(vp));
242 node = VP_TO_TMPFS_NODE(vp);
244 /* The file is still active but all its names have been removed
245 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
246 * it is about to die. */
247 if (node->tn_links < 1)
250 /* If the file is marked append-only, deny write requests. */
251 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
255 vnode_create_vobject(vp, node->tn_size, v->a_td);
258 MPASS(VOP_ISLOCKED(vp));
262 /* --------------------------------------------------------------------- */
265 tmpfs_close(struct vop_close_args *v)
267 struct vnode *vp = v->a_vp;
269 struct tmpfs_node *node;
271 MPASS(VOP_ISLOCKED(vp));
273 node = VP_TO_TMPFS_NODE(vp);
275 if (node->tn_links > 0) {
276 /* Update node times. No need to do it if the node has
277 * been deleted, because it will vanish after we return. */
284 /* --------------------------------------------------------------------- */
287 tmpfs_access(struct vop_access_args *v)
289 struct vnode *vp = v->a_vp;
290 accmode_t accmode = v->a_accmode;
291 struct ucred *cred = v->a_cred;
294 struct tmpfs_node *node;
296 MPASS(VOP_ISLOCKED(vp));
298 node = VP_TO_TMPFS_NODE(vp);
300 switch (vp->v_type) {
306 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
326 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
331 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
332 node->tn_gid, accmode, cred, NULL);
335 MPASS(VOP_ISLOCKED(vp));
340 /* --------------------------------------------------------------------- */
343 tmpfs_getattr(struct vop_getattr_args *v)
345 struct vnode *vp = v->a_vp;
346 struct vattr *vap = v->a_vap;
348 struct tmpfs_node *node;
350 node = VP_TO_TMPFS_NODE(vp);
354 vap->va_type = vp->v_type;
355 vap->va_mode = node->tn_mode;
356 vap->va_nlink = node->tn_links;
357 vap->va_uid = node->tn_uid;
358 vap->va_gid = node->tn_gid;
359 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
360 vap->va_fileid = node->tn_id;
361 vap->va_size = node->tn_size;
362 vap->va_blocksize = PAGE_SIZE;
363 vap->va_atime = node->tn_atime;
364 vap->va_mtime = node->tn_mtime;
365 vap->va_ctime = node->tn_ctime;
366 vap->va_birthtime = node->tn_birthtime;
367 vap->va_gen = node->tn_gen;
368 vap->va_flags = node->tn_flags;
369 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
370 node->tn_rdev : NODEV;
371 vap->va_bytes = round_page(node->tn_size);
377 /* --------------------------------------------------------------------- */
379 /* XXX Should this operation be atomic? I think it should, but code in
380 * XXX other places (e.g., ufs) doesn't seem to be... */
382 tmpfs_setattr(struct vop_setattr_args *v)
384 struct vnode *vp = v->a_vp;
385 struct vattr *vap = v->a_vap;
386 struct ucred *cred = v->a_cred;
387 struct thread *td = curthread;
391 MPASS(VOP_ISLOCKED(vp));
395 /* Abort if any unsettable attribute is given. */
396 if (vap->va_type != VNON ||
397 vap->va_nlink != VNOVAL ||
398 vap->va_fsid != VNOVAL ||
399 vap->va_fileid != VNOVAL ||
400 vap->va_blocksize != VNOVAL ||
401 vap->va_gen != VNOVAL ||
402 vap->va_rdev != VNOVAL ||
403 vap->va_bytes != VNOVAL)
406 if (error == 0 && (vap->va_flags != VNOVAL))
407 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
409 if (error == 0 && (vap->va_size != VNOVAL))
410 error = tmpfs_chsize(vp, vap->va_size, cred, td);
412 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
413 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
415 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
416 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
418 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
419 vap->va_atime.tv_nsec != VNOVAL) ||
420 (vap->va_mtime.tv_sec != VNOVAL &&
421 vap->va_mtime.tv_nsec != VNOVAL) ||
422 (vap->va_birthtime.tv_sec != VNOVAL &&
423 vap->va_birthtime.tv_nsec != VNOVAL)))
424 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
425 &vap->va_birthtime, vap->va_vaflags, cred, td);
427 /* Update the node times. We give preference to the error codes
428 * generated by this function rather than the ones that may arise
429 * from tmpfs_update. */
432 MPASS(VOP_ISLOCKED(vp));
437 /* --------------------------------------------------------------------- */
439 tmpfs_nocacheread(vm_object_t tobj, vm_pindex_t idx,
440 vm_offset_t offset, size_t tlen, struct uio *uio)
445 VM_OBJECT_LOCK(tobj);
446 vm_object_pip_add(tobj, 1);
447 m = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
448 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
449 if (m->valid != VM_PAGE_BITS_ALL) {
450 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
451 error = vm_pager_get_pages(tobj, &m, 1, 0);
453 printf("tmpfs get pages from pager error [read]\n");
457 vm_page_zero_invalid(m, TRUE);
459 VM_OBJECT_UNLOCK(tobj);
460 error = uiomove_fromphys(&m, offset, tlen, uio);
461 VM_OBJECT_LOCK(tobj);
463 vm_page_lock_queues();
464 vm_page_unwire(m, TRUE);
465 vm_page_unlock_queues();
467 vm_object_pip_subtract(tobj, 1);
468 VM_OBJECT_UNLOCK(tobj);
474 tmpfs_nocacheread_buf(vm_object_t tobj, vm_pindex_t idx,
475 vm_offset_t offset, size_t tlen, void *buf)
486 uio.uio_resid = tlen;
487 uio.uio_rw = UIO_READ;
488 uio.uio_segflg = UIO_SYSSPACE;
489 uio.uio_td = curthread;
491 return (tmpfs_nocacheread(tobj, idx, offset, tlen, &uio));
495 tmpfs_mappedread(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
506 addr = uio->uio_offset;
507 idx = OFF_TO_IDX(addr);
508 offset = addr & PAGE_MASK;
509 tlen = MIN(PAGE_SIZE - offset, len);
511 if ((vobj == NULL) || (vobj->resident_page_count == 0))
514 VM_OBJECT_LOCK(vobj);
516 if (((m = vm_page_lookup(vobj, idx)) != NULL) &&
517 vm_page_is_valid(m, offset, tlen)) {
518 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
521 VM_OBJECT_UNLOCK(vobj);
522 error = uiomove_fromphys(&m, offset, tlen, uio);
523 VM_OBJECT_LOCK(vobj);
525 VM_OBJECT_UNLOCK(vobj);
527 } else if (m != NULL && uio->uio_segflg == UIO_NOCOPY) {
529 ("unexpected offset in tmpfs_mappedread for sendfile"));
530 if (vm_page_sleep_if_busy(m, FALSE, "tmfsmr"))
533 VM_OBJECT_UNLOCK(vobj);
535 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
536 ma = (char *)sf_buf_kva(sf);
537 error = tmpfs_nocacheread_buf(tobj, idx, 0, tlen, ma);
539 if (tlen != PAGE_SIZE)
540 bzero(ma + tlen, PAGE_SIZE - tlen);
541 uio->uio_offset += tlen;
542 uio->uio_resid -= tlen;
546 VM_OBJECT_LOCK(vobj);
548 m->valid = VM_PAGE_BITS_ALL;
550 VM_OBJECT_UNLOCK(vobj);
553 VM_OBJECT_UNLOCK(vobj);
555 error = tmpfs_nocacheread(tobj, idx, offset, tlen, uio);
561 tmpfs_read(struct vop_read_args *v)
563 struct vnode *vp = v->a_vp;
564 struct uio *uio = v->a_uio;
566 struct tmpfs_node *node;
573 node = VP_TO_TMPFS_NODE(vp);
575 if (vp->v_type != VREG) {
580 if (uio->uio_offset < 0) {
585 node->tn_status |= TMPFS_NODE_ACCESSED;
587 uobj = node->tn_reg.tn_aobj;
588 while ((resid = uio->uio_resid) > 0) {
590 if (node->tn_size <= uio->uio_offset)
592 len = MIN(node->tn_size - uio->uio_offset, resid);
595 error = tmpfs_mappedread(vp->v_object, uobj, len, uio);
596 if ((error != 0) || (resid == uio->uio_resid))
605 /* --------------------------------------------------------------------- */
608 tmpfs_mappedwrite(vm_object_t vobj, vm_object_t tobj, size_t len, struct uio *uio)
619 addr = uio->uio_offset;
620 idx = OFF_TO_IDX(addr);
621 offset = addr & PAGE_MASK;
622 tlen = MIN(PAGE_SIZE - offset, len);
624 if ((vobj == NULL) || (vobj->resident_page_count == 0)) {
629 VM_OBJECT_LOCK(vobj);
631 if (((vpg = vm_page_lookup(vobj, idx)) != NULL) &&
632 vm_page_is_valid(vpg, offset, tlen)) {
633 if (vm_page_sleep_if_busy(vpg, FALSE, "tmfsmw"))
636 vm_page_lock_queues();
637 vm_page_undirty(vpg);
638 vm_page_unlock_queues();
639 VM_OBJECT_UNLOCK(vobj);
640 error = uiomove_fromphys(&vpg, offset, tlen, uio);
642 VM_OBJECT_UNLOCK(vobj);
646 VM_OBJECT_LOCK(tobj);
647 vm_object_pip_add(tobj, 1);
648 tpg = vm_page_grab(tobj, idx, VM_ALLOC_WIRED |
649 VM_ALLOC_ZERO | VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
650 if (tpg->valid != VM_PAGE_BITS_ALL) {
651 if (vm_pager_has_page(tobj, idx, NULL, NULL)) {
652 error = vm_pager_get_pages(tobj, &tpg, 1, 0);
654 printf("tmpfs get pages from pager error [write]\n");
658 vm_page_zero_invalid(tpg, TRUE);
660 VM_OBJECT_UNLOCK(tobj);
662 error = uiomove_fromphys(&tpg, offset, tlen, uio);
664 KASSERT(vpg->valid == VM_PAGE_BITS_ALL, ("parts of vpg invalid"));
665 pmap_copy_page(vpg, tpg);
667 VM_OBJECT_LOCK(tobj);
670 VM_OBJECT_LOCK(vobj);
671 vm_page_lock_queues();
673 KASSERT(tpg->valid == VM_PAGE_BITS_ALL,
674 ("parts of tpg invalid"));
677 vm_page_unwire(tpg, TRUE);
678 vm_page_unlock_queues();
683 VM_OBJECT_UNLOCK(vobj);
684 vm_object_pip_subtract(tobj, 1);
685 VM_OBJECT_UNLOCK(tobj);
691 tmpfs_write(struct vop_write_args *v)
693 struct vnode *vp = v->a_vp;
694 struct uio *uio = v->a_uio;
695 int ioflag = v->a_ioflag;
696 struct thread *td = uio->uio_td;
701 struct tmpfs_node *node;
706 node = VP_TO_TMPFS_NODE(vp);
707 oldsize = node->tn_size;
709 if (uio->uio_offset < 0 || vp->v_type != VREG) {
714 if (uio->uio_resid == 0) {
719 if (ioflag & IO_APPEND)
720 uio->uio_offset = node->tn_size;
722 if (uio->uio_offset + uio->uio_resid >
723 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
726 if (vp->v_type == VREG && td != NULL) {
727 PROC_LOCK(td->td_proc);
728 if (uio->uio_offset + uio->uio_resid >
729 lim_cur(td->td_proc, RLIMIT_FSIZE)) {
730 psignal(td->td_proc, SIGXFSZ);
731 PROC_UNLOCK(td->td_proc);
734 PROC_UNLOCK(td->td_proc);
737 extended = uio->uio_offset + uio->uio_resid > node->tn_size;
739 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
744 uobj = node->tn_reg.tn_aobj;
745 while ((resid = uio->uio_resid) > 0) {
746 if (node->tn_size <= uio->uio_offset)
748 len = MIN(node->tn_size - uio->uio_offset, resid);
751 error = tmpfs_mappedwrite(vp->v_object, uobj, len, uio);
752 if ((error != 0) || (resid == uio->uio_resid))
756 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
757 (extended ? TMPFS_NODE_CHANGED : 0);
759 if (node->tn_mode & (S_ISUID | S_ISGID)) {
760 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
761 node->tn_mode &= ~(S_ISUID | S_ISGID);
765 (void)tmpfs_reg_resize(vp, oldsize);
768 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
769 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
774 /* --------------------------------------------------------------------- */
777 tmpfs_fsync(struct vop_fsync_args *v)
779 struct vnode *vp = v->a_vp;
781 MPASS(VOP_ISLOCKED(vp));
788 /* --------------------------------------------------------------------- */
791 tmpfs_remove(struct vop_remove_args *v)
793 struct vnode *dvp = v->a_dvp;
794 struct vnode *vp = v->a_vp;
797 struct tmpfs_dirent *de;
798 struct tmpfs_mount *tmp;
799 struct tmpfs_node *dnode;
800 struct tmpfs_node *node;
802 MPASS(VOP_ISLOCKED(dvp));
803 MPASS(VOP_ISLOCKED(vp));
805 if (vp->v_type == VDIR) {
810 dnode = VP_TO_TMPFS_DIR(dvp);
811 node = VP_TO_TMPFS_NODE(vp);
812 tmp = VFS_TO_TMPFS(vp->v_mount);
813 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
816 /* Files marked as immutable or append-only cannot be deleted. */
817 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
818 (dnode->tn_flags & APPEND)) {
823 /* Remove the entry from the directory; as it is a file, we do not
824 * have to change the number of hard links of the directory. */
825 tmpfs_dir_detach(dvp, de);
826 if (v->a_cnp->cn_flags & DOWHITEOUT)
827 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
829 /* Free the directory entry we just deleted. Note that the node
830 * referred by it will not be removed until the vnode is really
832 tmpfs_free_dirent(tmp, de, TRUE);
834 if (node->tn_links > 0)
835 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
844 /* --------------------------------------------------------------------- */
847 tmpfs_link(struct vop_link_args *v)
849 struct vnode *dvp = v->a_tdvp;
850 struct vnode *vp = v->a_vp;
851 struct componentname *cnp = v->a_cnp;
854 struct tmpfs_dirent *de;
855 struct tmpfs_node *node;
857 MPASS(VOP_ISLOCKED(dvp));
858 MPASS(cnp->cn_flags & HASBUF);
859 MPASS(dvp != vp); /* XXX When can this be false? */
861 node = VP_TO_TMPFS_NODE(vp);
863 /* XXX: Why aren't the following two tests done by the caller? */
865 /* Hard links of directories are forbidden. */
866 if (vp->v_type == VDIR) {
871 /* Cannot create cross-device links. */
872 if (dvp->v_mount != vp->v_mount) {
877 /* Ensure that we do not overflow the maximum number of links imposed
879 MPASS(node->tn_links <= LINK_MAX);
880 if (node->tn_links == LINK_MAX) {
885 /* We cannot create links of files marked immutable or append-only. */
886 if (node->tn_flags & (IMMUTABLE | APPEND)) {
891 /* Allocate a new directory entry to represent the node. */
892 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
893 cnp->cn_nameptr, cnp->cn_namelen, &de);
897 /* Insert the new directory entry into the appropriate directory. */
898 if (cnp->cn_flags & ISWHITEOUT)
899 tmpfs_dir_whiteout_remove(dvp, cnp);
900 tmpfs_dir_attach(dvp, de);
902 /* vp link count has changed, so update node times. */
903 node->tn_status |= TMPFS_NODE_CHANGED;
912 /* --------------------------------------------------------------------- */
915 tmpfs_rename(struct vop_rename_args *v)
917 struct vnode *fdvp = v->a_fdvp;
918 struct vnode *fvp = v->a_fvp;
919 struct componentname *fcnp = v->a_fcnp;
920 struct vnode *tdvp = v->a_tdvp;
921 struct vnode *tvp = v->a_tvp;
922 struct componentname *tcnp = v->a_tcnp;
926 struct tmpfs_dirent *de;
927 struct tmpfs_node *fdnode;
928 struct tmpfs_node *fnode;
929 struct tmpfs_node *tnode;
930 struct tmpfs_node *tdnode;
932 MPASS(VOP_ISLOCKED(tdvp));
933 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
934 MPASS(fcnp->cn_flags & HASBUF);
935 MPASS(tcnp->cn_flags & HASBUF);
937 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
939 /* Disallow cross-device renames.
940 * XXX Why isn't this done by the caller? */
941 if (fvp->v_mount != tdvp->v_mount ||
942 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
947 tdnode = VP_TO_TMPFS_DIR(tdvp);
949 /* If source and target are the same file, there is nothing to do. */
955 /* If we need to move the directory between entries, lock the
956 * source so that we can safely operate on it. */
958 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
962 fdnode = VP_TO_TMPFS_DIR(fdvp);
963 fnode = VP_TO_TMPFS_NODE(fvp);
964 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
966 /* Entry can disappear before we lock fdvp,
967 * also avoid manipulating '.' and '..' entries. */
969 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
970 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
976 MPASS(de->td_node == fnode);
978 /* If re-naming a directory to another preexisting directory
979 * ensure that the target directory is empty so that its
980 * removal causes no side effects.
981 * Kern_rename gurantees the destination to be a directory
982 * if the source is one. */
984 MPASS(tnode != NULL);
986 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
987 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
992 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
993 if (tnode->tn_size > 0) {
997 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
1000 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
1004 MPASS(fnode->tn_type != VDIR &&
1005 tnode->tn_type != VDIR);
1009 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
1010 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
1015 /* Ensure that we have enough memory to hold the new name, if it
1016 * has to be changed. */
1017 if (fcnp->cn_namelen != tcnp->cn_namelen ||
1018 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
1019 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
1023 /* If the node is being moved to another directory, we have to do
1025 if (fdnode != tdnode) {
1026 /* In case we are moving a directory, we have to adjust its
1027 * parent to point to the new parent. */
1028 if (de->td_node->tn_type == VDIR) {
1029 struct tmpfs_node *n;
1031 /* Ensure the target directory is not a child of the
1032 * directory being moved. Otherwise, we'd end up
1033 * with stale nodes. */
1035 while (n != n->tn_dir.tn_parent) {
1038 if (newname != NULL)
1039 free(newname, M_TMPFSNAME);
1042 n = n->tn_dir.tn_parent;
1045 /* Adjust the parent pointer. */
1046 TMPFS_VALIDATE_DIR(fnode);
1047 de->td_node->tn_dir.tn_parent = tdnode;
1049 /* As a result of changing the target of the '..'
1050 * entry, the link count of the source and target
1051 * directories has to be adjusted. */
1056 /* Do the move: just remove the entry from the source directory
1057 * and insert it into the target one. */
1058 tmpfs_dir_detach(fdvp, de);
1059 if (fcnp->cn_flags & DOWHITEOUT)
1060 tmpfs_dir_whiteout_add(fdvp, fcnp);
1061 if (tcnp->cn_flags & ISWHITEOUT)
1062 tmpfs_dir_whiteout_remove(tdvp, tcnp);
1063 tmpfs_dir_attach(tdvp, de);
1066 /* If the name has changed, we need to make it effective by changing
1067 * it in the directory entry. */
1068 if (newname != NULL) {
1069 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
1071 free(de->td_name, M_TMPFSNAME);
1072 de->td_namelen = (uint16_t)tcnp->cn_namelen;
1073 memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
1074 de->td_name = newname;
1076 fnode->tn_status |= TMPFS_NODE_CHANGED;
1077 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1080 /* If we are overwriting an entry, we have to remove the old one
1081 * from the target directory. */
1083 /* Remove the old entry from the target directory. */
1084 de = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1085 tmpfs_dir_detach(tdvp, de);
1087 /* Free the directory entry we just deleted. Note that the
1088 * node referred by it will not be removed until the vnode is
1089 * really reclaimed. */
1090 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1096 if (fdnode != tdnode)
1097 VOP_UNLOCK(fdvp, 0);
1100 /* Release target nodes. */
1101 /* XXX: I don't understand when tdvp can be the same as tvp, but
1102 * other code takes care of this... */
1110 /* Release source nodes. */
1117 /* --------------------------------------------------------------------- */
1120 tmpfs_mkdir(struct vop_mkdir_args *v)
1122 struct vnode *dvp = v->a_dvp;
1123 struct vnode **vpp = v->a_vpp;
1124 struct componentname *cnp = v->a_cnp;
1125 struct vattr *vap = v->a_vap;
1127 MPASS(vap->va_type == VDIR);
1129 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1132 /* --------------------------------------------------------------------- */
1135 tmpfs_rmdir(struct vop_rmdir_args *v)
1137 struct vnode *dvp = v->a_dvp;
1138 struct vnode *vp = v->a_vp;
1141 struct tmpfs_dirent *de;
1142 struct tmpfs_mount *tmp;
1143 struct tmpfs_node *dnode;
1144 struct tmpfs_node *node;
1146 MPASS(VOP_ISLOCKED(dvp));
1147 MPASS(VOP_ISLOCKED(vp));
1149 tmp = VFS_TO_TMPFS(dvp->v_mount);
1150 dnode = VP_TO_TMPFS_DIR(dvp);
1151 node = VP_TO_TMPFS_DIR(vp);
1153 /* Directories with more than two entries ('.' and '..') cannot be
1155 if (node->tn_size > 0) {
1160 if ((dnode->tn_flags & APPEND)
1161 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1166 /* This invariant holds only if we are not trying to remove "..".
1167 * We checked for that above so this is safe now. */
1168 MPASS(node->tn_dir.tn_parent == dnode);
1170 /* Get the directory entry associated with node (vp). This was
1171 * filled by tmpfs_lookup while looking up the entry. */
1172 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1173 MPASS(TMPFS_DIRENT_MATCHES(de,
1174 v->a_cnp->cn_nameptr,
1175 v->a_cnp->cn_namelen));
1177 /* Check flags to see if we are allowed to remove the directory. */
1178 if (dnode->tn_flags & APPEND
1179 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1184 /* Detach the directory entry from the directory (dnode). */
1185 tmpfs_dir_detach(dvp, de);
1186 if (v->a_cnp->cn_flags & DOWHITEOUT)
1187 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1190 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1191 TMPFS_NODE_MODIFIED;
1192 node->tn_dir.tn_parent->tn_links--;
1193 node->tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1194 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1199 /* Free the directory entry we just deleted. Note that the node
1200 * referred by it will not be removed until the vnode is really
1202 tmpfs_free_dirent(tmp, de, TRUE);
1204 /* Release the deleted vnode (will destroy the node, notify
1205 * interested parties and clean it from the cache). */
1207 dnode->tn_status |= TMPFS_NODE_CHANGED;
1216 /* --------------------------------------------------------------------- */
1219 tmpfs_symlink(struct vop_symlink_args *v)
1221 struct vnode *dvp = v->a_dvp;
1222 struct vnode **vpp = v->a_vpp;
1223 struct componentname *cnp = v->a_cnp;
1224 struct vattr *vap = v->a_vap;
1225 char *target = v->a_target;
1227 #ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1228 MPASS(vap->va_type == VLNK);
1230 vap->va_type = VLNK;
1233 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1236 /* --------------------------------------------------------------------- */
1239 tmpfs_readdir(struct vop_readdir_args *v)
1241 struct vnode *vp = v->a_vp;
1242 struct uio *uio = v->a_uio;
1243 int *eofflag = v->a_eofflag;
1244 u_long **cookies = v->a_cookies;
1245 int *ncookies = v->a_ncookies;
1250 struct tmpfs_node *node;
1252 /* This operation only makes sense on directory nodes. */
1253 if (vp->v_type != VDIR)
1256 node = VP_TO_TMPFS_DIR(vp);
1258 startoff = uio->uio_offset;
1260 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1261 error = tmpfs_dir_getdotdent(node, uio);
1267 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1268 error = tmpfs_dir_getdotdotdent(node, uio);
1274 error = tmpfs_dir_getdents(node, uio, &cnt);
1282 if (eofflag != NULL)
1284 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1286 /* Update NFS-related variables. */
1287 if (error == 0 && cookies != NULL && ncookies != NULL) {
1289 off_t off = startoff;
1290 struct tmpfs_dirent *de = NULL;
1293 *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1295 for (i = 0; i < cnt; i++) {
1296 MPASS(off != TMPFS_DIRCOOKIE_EOF);
1297 if (off == TMPFS_DIRCOOKIE_DOT) {
1298 off = TMPFS_DIRCOOKIE_DOTDOT;
1300 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1301 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1302 } else if (de != NULL) {
1303 de = TAILQ_NEXT(de, td_entries);
1305 de = tmpfs_dir_lookupbycookie(node,
1308 de = TAILQ_NEXT(de, td_entries);
1311 off = TMPFS_DIRCOOKIE_EOF;
1313 off = tmpfs_dircookie(de);
1316 (*cookies)[i] = off;
1318 MPASS(uio->uio_offset == off);
1324 /* --------------------------------------------------------------------- */
1327 tmpfs_readlink(struct vop_readlink_args *v)
1329 struct vnode *vp = v->a_vp;
1330 struct uio *uio = v->a_uio;
1333 struct tmpfs_node *node;
1335 MPASS(uio->uio_offset == 0);
1336 MPASS(vp->v_type == VLNK);
1338 node = VP_TO_TMPFS_NODE(vp);
1340 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1342 node->tn_status |= TMPFS_NODE_ACCESSED;
1347 /* --------------------------------------------------------------------- */
1350 tmpfs_inactive(struct vop_inactive_args *v)
1352 struct vnode *vp = v->a_vp;
1353 struct thread *l = v->a_td;
1355 struct tmpfs_node *node;
1357 MPASS(VOP_ISLOCKED(vp));
1359 node = VP_TO_TMPFS_NODE(vp);
1361 if (node->tn_links == 0)
1367 /* --------------------------------------------------------------------- */
1370 tmpfs_reclaim(struct vop_reclaim_args *v)
1372 struct vnode *vp = v->a_vp;
1374 struct tmpfs_mount *tmp;
1375 struct tmpfs_node *node;
1377 node = VP_TO_TMPFS_NODE(vp);
1378 tmp = VFS_TO_TMPFS(vp->v_mount);
1380 vnode_destroy_vobject(vp);
1384 /* If the node referenced by this vnode was deleted by the user,
1385 * we must free its associated data structures (now that the vnode
1386 * is being reclaimed). */
1387 if (node->tn_links == 0)
1388 tmpfs_free_node(tmp, node);
1390 MPASS(vp->v_data == NULL);
1394 /* --------------------------------------------------------------------- */
1397 tmpfs_print(struct vop_print_args *v)
1399 struct vnode *vp = v->a_vp;
1401 struct tmpfs_node *node;
1403 node = VP_TO_TMPFS_NODE(vp);
1405 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1406 node, node->tn_flags, node->tn_links);
1407 printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1409 node->tn_mode, node->tn_uid, node->tn_gid,
1410 (uintmax_t)node->tn_size, node->tn_status);
1412 if (vp->v_type == VFIFO)
1420 /* --------------------------------------------------------------------- */
1423 tmpfs_pathconf(struct vop_pathconf_args *v)
1425 int name = v->a_name;
1426 register_t *retval = v->a_retval;
1449 case _PC_CHOWN_RESTRICTED:
1461 case _PC_FILESIZEBITS:
1462 *retval = 0; /* XXX Don't know which value should I return. */
1473 tmpfs_vptofh(struct vop_vptofh_args *ap)
1475 struct tmpfs_fid *tfhp;
1476 struct tmpfs_node *node;
1478 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1479 node = VP_TO_TMPFS_NODE(ap->a_vp);
1481 tfhp->tf_len = sizeof(struct tmpfs_fid);
1482 tfhp->tf_id = node->tn_id;
1483 tfhp->tf_gen = node->tn_gen;
1489 tmpfs_whiteout(struct vop_whiteout_args *ap)
1491 struct vnode *dvp = ap->a_dvp;
1492 struct componentname *cnp = ap->a_cnp;
1493 struct tmpfs_dirent *de;
1495 switch (ap->a_flags) {
1499 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1501 return (de->td_node == NULL ? 0 : EEXIST);
1502 return (tmpfs_dir_whiteout_add(dvp, cnp));
1504 tmpfs_dir_whiteout_remove(dvp, cnp);
1507 panic("tmpfs_whiteout: unknown op");
1511 /* --------------------------------------------------------------------- */
1514 * vnode operations vector used for files stored in a tmpfs file system.
1516 struct vop_vector tmpfs_vnodeop_entries = {
1517 .vop_default = &default_vnodeops,
1518 .vop_lookup = vfs_cache_lookup,
1519 .vop_cachedlookup = tmpfs_lookup,
1520 .vop_create = tmpfs_create,
1521 .vop_mknod = tmpfs_mknod,
1522 .vop_open = tmpfs_open,
1523 .vop_close = tmpfs_close,
1524 .vop_access = tmpfs_access,
1525 .vop_getattr = tmpfs_getattr,
1526 .vop_setattr = tmpfs_setattr,
1527 .vop_read = tmpfs_read,
1528 .vop_write = tmpfs_write,
1529 .vop_fsync = tmpfs_fsync,
1530 .vop_remove = tmpfs_remove,
1531 .vop_link = tmpfs_link,
1532 .vop_rename = tmpfs_rename,
1533 .vop_mkdir = tmpfs_mkdir,
1534 .vop_rmdir = tmpfs_rmdir,
1535 .vop_symlink = tmpfs_symlink,
1536 .vop_readdir = tmpfs_readdir,
1537 .vop_readlink = tmpfs_readlink,
1538 .vop_inactive = tmpfs_inactive,
1539 .vop_reclaim = tmpfs_reclaim,
1540 .vop_print = tmpfs_print,
1541 .vop_pathconf = tmpfs_pathconf,
1542 .vop_vptofh = tmpfs_vptofh,
1543 .vop_whiteout = tmpfs_whiteout,
1544 .vop_bmap = VOP_EOPNOTSUPP,