1 /* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */
4 * Copyright (c) 2005 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 * Efficient memory file system supporting functions.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/namei.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
46 #include <sys/vnode.h>
47 #include <sys/vmmeter.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_page.h>
52 #include <vm/vm_pager.h>
53 #include <vm/vm_extern.h>
55 #include <fs/tmpfs/tmpfs.h>
56 #include <fs/tmpfs/tmpfs_fifoops.h>
57 #include <fs/tmpfs/tmpfs_vnops.h>
59 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "tmpfs file system");
61 /* --------------------------------------------------------------------- */
64 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
65 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
66 * using the credentials of the process 'p'.
68 * If the node type is set to 'VDIR', then the parent parameter must point
69 * to the parent directory of the node being created. It may only be NULL
70 * while allocating the root node.
72 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
73 * specifies the device the node represents.
75 * If the node type is set to 'VLNK', then the parameter target specifies
76 * the file name of the target file for the symbolic link that is being
79 * Note that new nodes are retrieved from the available list if it has
80 * items or, if it is empty, from the node pool as long as there is enough
81 * space to create them.
83 * Returns zero on success or an appropriate error code on failure.
86 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
87 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
88 char *target, dev_t rdev, struct tmpfs_node **node)
90 struct tmpfs_node *nnode;
92 /* If the root directory of the 'tmp' file system is not yet
93 * allocated, this must be the request to do it. */
94 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
96 MPASS(IFF(type == VLNK, target != NULL));
97 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
99 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
102 nnode = (struct tmpfs_node *)uma_zalloc_arg(
103 tmp->tm_node_pool, tmp, M_WAITOK);
105 /* Generic initialization. */
106 nnode->tn_type = type;
107 vfs_timestamp(&nnode->tn_atime);
108 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
112 nnode->tn_mode = mode;
113 nnode->tn_id = alloc_unr(tmp->tm_ino_unr);
115 /* Type-specific initialization. */
116 switch (nnode->tn_type) {
119 nnode->tn_rdev = rdev;
123 TAILQ_INIT(&nnode->tn_dir.tn_dirhead);
124 MPASS(parent != nnode);
125 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
126 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
127 nnode->tn_dir.tn_readdir_lastn = 0;
128 nnode->tn_dir.tn_readdir_lastp = NULL;
130 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
131 nnode->tn_dir.tn_parent->tn_links++;
132 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
141 MPASS(strlen(target) < MAXPATHLEN);
142 nnode->tn_size = strlen(target);
143 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
145 memcpy(nnode->tn_link, target, nnode->tn_size);
149 nnode->tn_reg.tn_aobj =
150 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
151 NULL /* XXXKIB - tmpfs needs swap reservation */);
152 nnode->tn_reg.tn_aobj_pages = 0;
156 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
160 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
161 tmp->tm_nodes_inuse++;
168 /* --------------------------------------------------------------------- */
171 * Destroys the node pointed to by node from the file system 'tmp'.
172 * If the node does not belong to the given mount point, the results are
175 * If the node references a directory; no entries are allowed because
176 * their removal could need a recursive algorithm, something forbidden in
177 * kernel space. Furthermore, there is not need to provide such
178 * functionality (recursive removal) because the only primitives offered
179 * to the user are the removal of empty directories and the deletion of
182 * Note that nodes are not really deleted; in fact, when a node has been
183 * allocated, it cannot be deleted during the whole life of the file
184 * system. Instead, they are moved to the available list and remain there
188 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
193 TMPFS_NODE_LOCK(node);
194 MPASS(node->tn_vnode == NULL);
195 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
196 TMPFS_NODE_UNLOCK(node);
200 LIST_REMOVE(node, tn_entries);
201 tmp->tm_nodes_inuse--;
204 switch (node->tn_type) {
206 /* Do not do anything. VNON is provided to let the
207 * allocation routine clean itself easily by avoiding
208 * duplicating code in it. */
222 free(node->tn_link, M_TMPFSNAME);
226 if (node->tn_reg.tn_aobj != NULL)
227 vm_object_deallocate(node->tn_reg.tn_aobj);
228 pages = node->tn_reg.tn_aobj_pages;
232 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
235 free_unr(tmp->tm_ino_unr, node->tn_id);
236 uma_zfree(tmp->tm_node_pool, node);
239 tmp->tm_pages_used -= pages;
243 /* --------------------------------------------------------------------- */
246 * Allocates a new directory entry for the node node with a name of name.
247 * The new directory entry is returned in *de.
249 * The link count of node is increased by one to reflect the new object
252 * Returns zero on success or an appropriate error code on failure.
255 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
256 const char *name, uint16_t len, struct tmpfs_dirent **de)
258 struct tmpfs_dirent *nde;
260 nde = (struct tmpfs_dirent *)uma_zalloc(
261 tmp->tm_dirent_pool, M_WAITOK);
262 nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
263 nde->td_namelen = len;
264 memcpy(nde->td_name, name, len);
275 /* --------------------------------------------------------------------- */
278 * Frees a directory entry. It is the caller's responsibility to destroy
279 * the node referenced by it if needed.
281 * The link count of node is decreased by one to reflect the removal of an
282 * object that referenced it. This only happens if 'node_exists' is true;
283 * otherwise the function will not access the node referred to by the
284 * directory entry, as it may already have been released from the outside.
287 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
288 boolean_t node_exists)
291 struct tmpfs_node *node;
295 MPASS(node->tn_links > 0);
300 free(de->td_name, M_TMPFSNAME);
301 uma_zfree(tmp->tm_dirent_pool, de);
304 /* --------------------------------------------------------------------- */
307 * Allocates a new vnode for the node node or returns a new reference to
308 * an existing one if the node had already a vnode referencing it. The
309 * resulting locked vnode is returned in *vpp.
311 * Returns zero on success or an appropriate error code on failure.
314 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
321 TMPFS_NODE_LOCK(node);
322 if ((vp = node->tn_vnode) != NULL) {
323 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
325 TMPFS_NODE_UNLOCK(node);
326 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
333 * Make sure the vnode is still there after
334 * getting the interlock to avoid racing a free.
336 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
344 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
345 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
346 TMPFS_NODE_UNLOCK(node);
353 * otherwise lock the vp list while we call getnewvnode
354 * since that can block.
356 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
357 node->tn_vpstate |= TMPFS_VNODE_WANT;
358 error = msleep((caddr_t) &node->tn_vpstate,
359 TMPFS_NODE_MTX(node), PDROP | PCATCH,
360 "tmpfs_alloc_vp", 0);
366 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
368 TMPFS_NODE_UNLOCK(node);
370 /* Get a new vnode and associate it with our node. */
371 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
376 (void) vn_lock(vp, lkflag | LK_RETRY);
379 vp->v_type = node->tn_type;
381 /* Type-specific initialization. */
382 switch (node->tn_type) {
394 vp->v_op = &tmpfs_fifoop_entries;
397 MPASS(node->tn_dir.tn_parent != NULL);
398 if (node->tn_dir.tn_parent == node)
399 vp->v_vflag |= VV_ROOT;
403 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
406 vnode_pager_setsize(vp, node->tn_size);
407 error = insmntque(vp, mp);
412 TMPFS_NODE_LOCK(node);
414 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
415 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
418 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
419 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
420 TMPFS_NODE_UNLOCK(node);
421 wakeup((caddr_t) &node->tn_vpstate);
423 TMPFS_NODE_UNLOCK(node);
430 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
431 TMPFS_NODE_LOCK(node);
432 MPASS(*vpp == node->tn_vnode);
433 TMPFS_NODE_UNLOCK(node);
440 /* --------------------------------------------------------------------- */
443 * Destroys the association between the vnode vp and the node it
447 tmpfs_free_vp(struct vnode *vp)
449 struct tmpfs_node *node;
451 node = VP_TO_TMPFS_NODE(vp);
453 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);
454 node->tn_vnode = NULL;
458 /* --------------------------------------------------------------------- */
461 * Allocates a new file of type 'type' and adds it to the parent directory
462 * 'dvp'; this addition is done using the component name given in 'cnp'.
463 * The ownership of the new file is automatically assigned based on the
464 * credentials of the caller (through 'cnp'), the group is set based on
465 * the parent directory and the mode is determined from the 'vap' argument.
466 * If successful, *vpp holds a vnode to the newly created file and zero
467 * is returned. Otherwise *vpp is NULL and the function returns an
468 * appropriate error code.
471 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
472 struct componentname *cnp, char *target)
475 struct tmpfs_dirent *de;
476 struct tmpfs_mount *tmp;
477 struct tmpfs_node *dnode;
478 struct tmpfs_node *node;
479 struct tmpfs_node *parent;
481 MPASS(VOP_ISLOCKED(dvp));
482 MPASS(cnp->cn_flags & HASBUF);
484 tmp = VFS_TO_TMPFS(dvp->v_mount);
485 dnode = VP_TO_TMPFS_DIR(dvp);
488 /* If the entry we are creating is a directory, we cannot overflow
489 * the number of links of its parent, because it will get a new
491 if (vap->va_type == VDIR) {
492 /* Ensure that we do not overflow the maximum number of links
493 * imposed by the system. */
494 MPASS(dnode->tn_links <= LINK_MAX);
495 if (dnode->tn_links == LINK_MAX) {
501 MPASS(parent != NULL);
505 /* Allocate a node that represents the new file. */
506 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
507 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
511 /* Allocate a directory entry that points to the new file. */
512 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
515 tmpfs_free_node(tmp, node);
519 /* Allocate a vnode for the new file. */
520 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
522 tmpfs_free_dirent(tmp, de, TRUE);
523 tmpfs_free_node(tmp, node);
527 /* Now that all required items are allocated, we can proceed to
528 * insert the new node into the directory, an operation that
530 if (cnp->cn_flags & ISWHITEOUT)
531 tmpfs_dir_whiteout_remove(dvp, cnp);
532 tmpfs_dir_attach(dvp, de);
539 /* --------------------------------------------------------------------- */
542 * Attaches the directory entry de to the directory represented by vp.
543 * Note that this does not change the link count of the node pointed by
544 * the directory entry, as this is done by tmpfs_alloc_dirent.
547 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
549 struct tmpfs_node *dnode;
551 ASSERT_VOP_ELOCKED(vp, __func__);
552 dnode = VP_TO_TMPFS_DIR(vp);
553 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries);
554 dnode->tn_size += sizeof(struct tmpfs_dirent);
555 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
559 /* --------------------------------------------------------------------- */
562 * Detaches the directory entry de from the directory represented by vp.
563 * Note that this does not change the link count of the node pointed by
564 * the directory entry, as this is done by tmpfs_free_dirent.
567 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
569 struct tmpfs_node *dnode;
571 ASSERT_VOP_ELOCKED(vp, __func__);
572 dnode = VP_TO_TMPFS_DIR(vp);
574 if (dnode->tn_dir.tn_readdir_lastp == de) {
575 dnode->tn_dir.tn_readdir_lastn = 0;
576 dnode->tn_dir.tn_readdir_lastp = NULL;
579 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries);
580 dnode->tn_size -= sizeof(struct tmpfs_dirent);
581 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
585 /* --------------------------------------------------------------------- */
588 * Looks for a directory entry in the directory represented by node.
589 * 'cnp' describes the name of the entry to look for. Note that the .
590 * and .. components are not allowed as they do not physically exist
591 * within directories.
593 * Returns a pointer to the entry when found, otherwise NULL.
595 struct tmpfs_dirent *
596 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
597 struct componentname *cnp)
600 struct tmpfs_dirent *de;
602 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
603 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
604 cnp->cn_nameptr[1] == '.')));
605 TMPFS_VALIDATE_DIR(node);
608 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
609 if (f != NULL && de->td_node != f)
611 MPASS(cnp->cn_namelen < 0xffff);
612 if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
613 bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
618 node->tn_status |= TMPFS_NODE_ACCESSED;
620 return found ? de : NULL;
623 /* --------------------------------------------------------------------- */
626 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
627 * directory and returns it in the uio space. The function returns 0
628 * on success, -1 if there was not enough space in the uio structure to
629 * hold the directory entry or an appropriate error code if another
633 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
638 TMPFS_VALIDATE_DIR(node);
639 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
641 dent.d_fileno = node->tn_id;
642 dent.d_type = DT_DIR;
644 dent.d_name[0] = '.';
645 dent.d_name[1] = '\0';
646 dent.d_reclen = GENERIC_DIRSIZ(&dent);
648 if (dent.d_reclen > uio->uio_resid)
651 error = uiomove(&dent, dent.d_reclen, uio);
653 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
656 node->tn_status |= TMPFS_NODE_ACCESSED;
661 /* --------------------------------------------------------------------- */
664 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
665 * directory and returns it in the uio space. The function returns 0
666 * on success, -1 if there was not enough space in the uio structure to
667 * hold the directory entry or an appropriate error code if another
671 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
676 TMPFS_VALIDATE_DIR(node);
677 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
680 * Return ENOENT if the current node is already removed.
682 TMPFS_ASSERT_LOCKED(node);
683 if (node->tn_dir.tn_parent == NULL) {
687 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
688 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
689 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
691 dent.d_type = DT_DIR;
693 dent.d_name[0] = '.';
694 dent.d_name[1] = '.';
695 dent.d_name[2] = '\0';
696 dent.d_reclen = GENERIC_DIRSIZ(&dent);
698 if (dent.d_reclen > uio->uio_resid)
701 error = uiomove(&dent, dent.d_reclen, uio);
703 struct tmpfs_dirent *de;
705 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
707 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
709 uio->uio_offset = tmpfs_dircookie(de);
713 node->tn_status |= TMPFS_NODE_ACCESSED;
718 /* --------------------------------------------------------------------- */
721 * Lookup a directory entry by its associated cookie.
723 struct tmpfs_dirent *
724 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
726 struct tmpfs_dirent *de;
728 if (cookie == node->tn_dir.tn_readdir_lastn &&
729 node->tn_dir.tn_readdir_lastp != NULL) {
730 return node->tn_dir.tn_readdir_lastp;
733 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
734 if (tmpfs_dircookie(de) == cookie) {
742 /* --------------------------------------------------------------------- */
745 * Helper function for tmpfs_readdir. Returns as much directory entries
746 * as can fit in the uio space. The read starts at uio->uio_offset.
747 * The function returns 0 on success, -1 if there was not enough space
748 * in the uio structure to hold the directory entry or an appropriate
749 * error code if another error happens.
752 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
756 struct tmpfs_dirent *de;
758 TMPFS_VALIDATE_DIR(node);
760 /* Locate the first directory entry we have to return. We have cached
761 * the last readdir in the node, so use those values if appropriate.
762 * Otherwise do a linear scan to find the requested entry. */
763 startcookie = uio->uio_offset;
764 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT);
765 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
766 if (startcookie == TMPFS_DIRCOOKIE_EOF) {
769 de = tmpfs_dir_lookupbycookie(node, startcookie);
775 /* Read as much entries as possible; i.e., until we reach the end of
776 * the directory or we exhaust uio space. */
780 /* Create a dirent structure representing the current
781 * tmpfs_node and fill it. */
782 if (de->td_node == NULL) {
786 d.d_fileno = de->td_node->tn_id;
787 switch (de->td_node->tn_type) {
817 panic("tmpfs_dir_getdents: type %p %d",
818 de->td_node, (int)de->td_node->tn_type);
821 d.d_namlen = de->td_namelen;
822 MPASS(de->td_namelen < sizeof(d.d_name));
823 (void)memcpy(d.d_name, de->td_name, de->td_namelen);
824 d.d_name[de->td_namelen] = '\0';
825 d.d_reclen = GENERIC_DIRSIZ(&d);
827 /* Stop reading if the directory entry we are treating is
828 * bigger than the amount of data that can be returned. */
829 if (d.d_reclen > uio->uio_resid) {
834 /* Copy the new dirent structure into the output buffer and
835 * advance pointers. */
836 error = uiomove(&d, d.d_reclen, uio);
839 de = TAILQ_NEXT(de, td_entries);
841 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
843 /* Update the offset and cache. */
845 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
846 node->tn_dir.tn_readdir_lastn = 0;
847 node->tn_dir.tn_readdir_lastp = NULL;
849 node->tn_dir.tn_readdir_lastn = uio->uio_offset = tmpfs_dircookie(de);
850 node->tn_dir.tn_readdir_lastp = de;
853 node->tn_status |= TMPFS_NODE_ACCESSED;
858 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
860 struct tmpfs_dirent *de;
863 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
864 cnp->cn_nameptr, cnp->cn_namelen, &de);
867 tmpfs_dir_attach(dvp, de);
872 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
874 struct tmpfs_dirent *de;
876 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
877 MPASS(de != NULL && de->td_node == NULL);
878 tmpfs_dir_detach(dvp, de);
879 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de, TRUE);
882 /* --------------------------------------------------------------------- */
885 * Resizes the aobj associated to the regular file pointed to by vp to
886 * the size newsize. 'vp' must point to a vnode that represents a regular
887 * file. 'newsize' must be positive.
889 * Returns zero on success or an appropriate error code on failure.
892 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
895 size_t newpages, oldpages;
896 struct tmpfs_mount *tmp;
897 struct tmpfs_node *node;
900 MPASS(vp->v_type == VREG);
903 node = VP_TO_TMPFS_NODE(vp);
904 tmp = VFS_TO_TMPFS(vp->v_mount);
906 /* Convert the old and new sizes to the number of pages needed to
907 * store them. It may happen that we do not need to do anything
908 * because the last allocated page can accommodate the change on
910 oldsize = node->tn_size;
911 oldpages = round_page(oldsize) / PAGE_SIZE;
912 MPASS(oldpages == node->tn_reg.tn_aobj_pages);
913 newpages = round_page(newsize) / PAGE_SIZE;
915 if (newpages > oldpages &&
916 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) {
921 node->tn_reg.tn_aobj_pages = newpages;
924 tmp->tm_pages_used += (newpages - oldpages);
927 node->tn_size = newsize;
928 vnode_pager_setsize(vp, newsize);
929 if (newsize < oldsize) {
930 size_t zerolen = round_page(newsize) - newsize;
931 vm_object_t uobj = node->tn_reg.tn_aobj;
935 * free "backing store"
937 VM_OBJECT_LOCK(uobj);
938 if (newpages < oldpages) {
939 swap_pager_freespace(uobj,
940 newpages, oldpages - newpages);
941 vm_object_page_remove(uobj,
942 OFF_TO_IDX(newsize + PAGE_MASK), 0, FALSE);
946 * zero out the truncated part of the last page.
950 m = vm_page_grab(uobj, OFF_TO_IDX(newsize),
951 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
952 pmap_zero_page_area(m, PAGE_SIZE - zerolen,
956 VM_OBJECT_UNLOCK(uobj);
966 /* --------------------------------------------------------------------- */
969 * Change flags of the given vnode.
970 * Caller should execute tmpfs_update on vp after a successful execution.
971 * The vnode must be locked on entry and remain locked on exit.
974 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p)
977 struct tmpfs_node *node;
979 MPASS(VOP_ISLOCKED(vp));
981 node = VP_TO_TMPFS_NODE(vp);
983 /* Disallow this operation if the file system is mounted read-only. */
984 if (vp->v_mount->mnt_flag & MNT_RDONLY)
988 * Callers may only modify the file flags on objects they
989 * have VADMIN rights for.
991 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
994 * Unprivileged processes are not permitted to unset system
995 * flags, or modify flags if any system flags are set.
997 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
999 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1000 error = securelevel_gt(cred, 0);
1004 /* Snapshot flag cannot be set or cleared */
1005 if (((flags & SF_SNAPSHOT) != 0 &&
1006 (node->tn_flags & SF_SNAPSHOT) == 0) ||
1007 ((flags & SF_SNAPSHOT) == 0 &&
1008 (node->tn_flags & SF_SNAPSHOT) != 0))
1010 node->tn_flags = flags;
1013 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1014 (flags & UF_SETTABLE) != flags)
1016 node->tn_flags &= SF_SETTABLE;
1017 node->tn_flags |= (flags & UF_SETTABLE);
1019 node->tn_status |= TMPFS_NODE_CHANGED;
1021 MPASS(VOP_ISLOCKED(vp));
1026 /* --------------------------------------------------------------------- */
1029 * Change access mode on the given vnode.
1030 * Caller should execute tmpfs_update on vp after a successful execution.
1031 * The vnode must be locked on entry and remain locked on exit.
1034 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1037 struct tmpfs_node *node;
1039 MPASS(VOP_ISLOCKED(vp));
1041 node = VP_TO_TMPFS_NODE(vp);
1043 /* Disallow this operation if the file system is mounted read-only. */
1044 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1047 /* Immutable or append-only files cannot be modified, either. */
1048 if (node->tn_flags & (IMMUTABLE | APPEND))
1052 * To modify the permissions on a file, must possess VADMIN
1055 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1059 * Privileged processes may set the sticky bit on non-directories,
1060 * as well as set the setgid bit on a file with a group that the
1061 * process is not a member of.
1063 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1064 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1067 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1068 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1074 node->tn_mode &= ~ALLPERMS;
1075 node->tn_mode |= mode & ALLPERMS;
1077 node->tn_status |= TMPFS_NODE_CHANGED;
1079 MPASS(VOP_ISLOCKED(vp));
1084 /* --------------------------------------------------------------------- */
1087 * Change ownership of the given vnode. At least one of uid or gid must
1088 * be different than VNOVAL. If one is set to that value, the attribute
1090 * Caller should execute tmpfs_update on vp after a successful execution.
1091 * The vnode must be locked on entry and remain locked on exit.
1094 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1098 struct tmpfs_node *node;
1102 MPASS(VOP_ISLOCKED(vp));
1104 node = VP_TO_TMPFS_NODE(vp);
1106 /* Assign default values if they are unknown. */
1107 MPASS(uid != VNOVAL || gid != VNOVAL);
1112 MPASS(uid != VNOVAL && gid != VNOVAL);
1114 /* Disallow this operation if the file system is mounted read-only. */
1115 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1118 /* Immutable or append-only files cannot be modified, either. */
1119 if (node->tn_flags & (IMMUTABLE | APPEND))
1123 * To modify the ownership of a file, must possess VADMIN for that
1126 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1130 * To change the owner of a file, or change the group of a file to a
1131 * group of which we are not a member, the caller must have
1134 if ((uid != node->tn_uid ||
1135 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1136 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1139 ogid = node->tn_gid;
1140 ouid = node->tn_uid;
1145 node->tn_status |= TMPFS_NODE_CHANGED;
1147 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1148 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1149 node->tn_mode &= ~(S_ISUID | S_ISGID);
1152 MPASS(VOP_ISLOCKED(vp));
1157 /* --------------------------------------------------------------------- */
1160 * Change size of the given vnode.
1161 * Caller should execute tmpfs_update on vp after a successful execution.
1162 * The vnode must be locked on entry and remain locked on exit.
1165 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1169 struct tmpfs_node *node;
1171 MPASS(VOP_ISLOCKED(vp));
1173 node = VP_TO_TMPFS_NODE(vp);
1175 /* Decide whether this is a valid operation based on the file type. */
1177 switch (vp->v_type) {
1182 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1191 /* Allow modifications of special files even if in the file
1192 * system is mounted read-only (we are not modifying the
1193 * files themselves, but the objects they represent). */
1197 /* Anything else is unsupported. */
1201 /* Immutable or append-only files cannot be modified, either. */
1202 if (node->tn_flags & (IMMUTABLE | APPEND))
1205 error = tmpfs_truncate(vp, size);
1206 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1207 * for us, as will update tn_status; no need to do that here. */
1209 MPASS(VOP_ISLOCKED(vp));
1214 /* --------------------------------------------------------------------- */
1217 * Change access and modification times of the given vnode.
1218 * Caller should execute tmpfs_update on vp after a successful execution.
1219 * The vnode must be locked on entry and remain locked on exit.
1222 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1223 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l)
1226 struct tmpfs_node *node;
1228 MPASS(VOP_ISLOCKED(vp));
1230 node = VP_TO_TMPFS_NODE(vp);
1232 /* Disallow this operation if the file system is mounted read-only. */
1233 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1236 /* Immutable or append-only files cannot be modified, either. */
1237 if (node->tn_flags & (IMMUTABLE | APPEND))
1240 /* Determine if the user have proper privilege to update time. */
1241 if (vaflags & VA_UTIMES_NULL) {
1242 error = VOP_ACCESS(vp, VADMIN, cred, l);
1244 error = VOP_ACCESS(vp, VWRITE, cred, l);
1246 error = VOP_ACCESS(vp, VADMIN, cred, l);
1250 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1251 node->tn_status |= TMPFS_NODE_ACCESSED;
1253 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1254 node->tn_status |= TMPFS_NODE_MODIFIED;
1256 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1257 node->tn_status |= TMPFS_NODE_MODIFIED;
1259 tmpfs_itimes(vp, atime, mtime);
1261 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1262 node->tn_birthtime = *birthtime;
1263 MPASS(VOP_ISLOCKED(vp));
1268 /* --------------------------------------------------------------------- */
1269 /* Sync timestamps */
1271 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1272 const struct timespec *mod)
1274 struct tmpfs_node *node;
1275 struct timespec now;
1277 node = VP_TO_TMPFS_NODE(vp);
1279 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1280 TMPFS_NODE_CHANGED)) == 0)
1283 vfs_timestamp(&now);
1284 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1287 node->tn_atime = *acc;
1289 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1292 node->tn_mtime = *mod;
1294 if (node->tn_status & TMPFS_NODE_CHANGED) {
1295 node->tn_ctime = now;
1298 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1301 /* --------------------------------------------------------------------- */
1304 tmpfs_update(struct vnode *vp)
1307 tmpfs_itimes(vp, NULL, NULL);
1310 /* --------------------------------------------------------------------- */
1313 tmpfs_truncate(struct vnode *vp, off_t length)
1316 struct tmpfs_node *node;
1318 node = VP_TO_TMPFS_NODE(vp);
1325 if (node->tn_size == length) {
1330 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1333 error = tmpfs_reg_resize(vp, length);
1335 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;