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_param.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_pageout.h>
54 #include <vm/vm_pager.h>
55 #include <vm/vm_extern.h>
57 #include <fs/tmpfs/tmpfs.h>
58 #include <fs/tmpfs/tmpfs_fifoops.h>
59 #include <fs/tmpfs/tmpfs_vnops.h>
61 SYSCTL_NODE(_vfs, OID_AUTO, tmpfs, CTLFLAG_RW, 0, "tmpfs file system");
63 static long tmpfs_pages_reserved = TMPFS_PAGES_MINRESERVED;
66 sysctl_mem_reserved(SYSCTL_HANDLER_ARGS)
71 pages = *(long *)arg1;
72 bytes = pages * PAGE_SIZE;
74 error = sysctl_handle_long(oidp, &bytes, 0, req);
75 if (error || !req->newptr)
78 pages = bytes / PAGE_SIZE;
79 if (pages < TMPFS_PAGES_MINRESERVED)
82 *(long *)arg1 = pages;
86 SYSCTL_PROC(_vfs_tmpfs, OID_AUTO, memory_reserved, CTLTYPE_LONG|CTLFLAG_RW,
87 &tmpfs_pages_reserved, 0, sysctl_mem_reserved, "L",
88 "Amount of available memory and swap below which tmpfs growth stops");
95 avail = swap_pager_avail + cnt.v_free_count + cnt.v_cache_count -
97 if (__predict_false(avail < 0))
103 tmpfs_pages_used(struct tmpfs_mount *tmp)
105 const size_t node_size = sizeof(struct tmpfs_node) +
106 sizeof(struct tmpfs_dirent);
109 meta_pages = howmany((uintmax_t)tmp->tm_nodes_inuse * node_size,
111 return (meta_pages + tmp->tm_pages_used);
115 tmpfs_pages_check_avail(struct tmpfs_mount *tmp, size_t req_pages)
117 if (tmpfs_mem_avail() < req_pages)
120 if (tmp->tm_pages_max != SIZE_MAX &&
121 tmp->tm_pages_max < req_pages + tmpfs_pages_used(tmp))
127 /* --------------------------------------------------------------------- */
130 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
131 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
132 * using the credentials of the process 'p'.
134 * If the node type is set to 'VDIR', then the parent parameter must point
135 * to the parent directory of the node being created. It may only be NULL
136 * while allocating the root node.
138 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
139 * specifies the device the node represents.
141 * If the node type is set to 'VLNK', then the parameter target specifies
142 * the file name of the target file for the symbolic link that is being
145 * Note that new nodes are retrieved from the available list if it has
146 * items or, if it is empty, from the node pool as long as there is enough
147 * space to create them.
149 * Returns zero on success or an appropriate error code on failure.
152 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
153 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
154 char *target, dev_t rdev, struct tmpfs_node **node)
156 struct tmpfs_node *nnode;
158 /* If the root directory of the 'tmp' file system is not yet
159 * allocated, this must be the request to do it. */
160 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
162 MPASS(IFF(type == VLNK, target != NULL));
163 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
165 if (tmp->tm_nodes_inuse >= tmp->tm_nodes_max)
167 if (tmpfs_pages_check_avail(tmp, 1) == 0)
170 nnode = (struct tmpfs_node *)uma_zalloc_arg(
171 tmp->tm_node_pool, tmp, M_WAITOK);
173 /* Generic initialization. */
174 nnode->tn_type = type;
175 vfs_timestamp(&nnode->tn_atime);
176 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
180 nnode->tn_mode = mode;
181 nnode->tn_id = alloc_unr(tmp->tm_ino_unr);
183 /* Type-specific initialization. */
184 switch (nnode->tn_type) {
187 nnode->tn_rdev = rdev;
191 TAILQ_INIT(&nnode->tn_dir.tn_dirhead);
192 MPASS(parent != nnode);
193 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
194 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
195 nnode->tn_dir.tn_readdir_lastn = 0;
196 nnode->tn_dir.tn_readdir_lastp = NULL;
198 TMPFS_NODE_LOCK(nnode->tn_dir.tn_parent);
199 nnode->tn_dir.tn_parent->tn_links++;
200 TMPFS_NODE_UNLOCK(nnode->tn_dir.tn_parent);
209 MPASS(strlen(target) < MAXPATHLEN);
210 nnode->tn_size = strlen(target);
211 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
213 memcpy(nnode->tn_link, target, nnode->tn_size);
217 nnode->tn_reg.tn_aobj =
218 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
219 NULL /* XXXKIB - tmpfs needs swap reservation */);
223 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
227 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
228 tmp->tm_nodes_inuse++;
235 /* --------------------------------------------------------------------- */
238 * Destroys the node pointed to by node from the file system 'tmp'.
239 * If the node does not belong to the given mount point, the results are
242 * If the node references a directory; no entries are allowed because
243 * their removal could need a recursive algorithm, something forbidden in
244 * kernel space. Furthermore, there is not need to provide such
245 * functionality (recursive removal) because the only primitives offered
246 * to the user are the removal of empty directories and the deletion of
249 * Note that nodes are not really deleted; in fact, when a node has been
250 * allocated, it cannot be deleted during the whole life of the file
251 * system. Instead, they are moved to the available list and remain there
255 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
260 TMPFS_NODE_LOCK(node);
261 MPASS(node->tn_vnode == NULL);
262 MPASS((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
263 TMPFS_NODE_UNLOCK(node);
267 LIST_REMOVE(node, tn_entries);
268 tmp->tm_nodes_inuse--;
271 switch (node->tn_type) {
273 /* Do not do anything. VNON is provided to let the
274 * allocation routine clean itself easily by avoiding
275 * duplicating code in it. */
289 free(node->tn_link, M_TMPFSNAME);
293 uobj = node->tn_reg.tn_aobj;
296 tmp->tm_pages_used -= uobj->size;
298 vm_object_deallocate(uobj);
303 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
306 free_unr(tmp->tm_ino_unr, node->tn_id);
307 uma_zfree(tmp->tm_node_pool, node);
310 /* --------------------------------------------------------------------- */
313 * Allocates a new directory entry for the node node with a name of name.
314 * The new directory entry is returned in *de.
316 * The link count of node is increased by one to reflect the new object
319 * Returns zero on success or an appropriate error code on failure.
322 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
323 const char *name, uint16_t len, struct tmpfs_dirent **de)
325 struct tmpfs_dirent *nde;
327 nde = (struct tmpfs_dirent *)uma_zalloc(
328 tmp->tm_dirent_pool, M_WAITOK);
329 nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
330 nde->td_namelen = len;
331 memcpy(nde->td_name, name, len);
342 /* --------------------------------------------------------------------- */
345 * Frees a directory entry. It is the caller's responsibility to destroy
346 * the node referenced by it if needed.
348 * The link count of node is decreased by one to reflect the removal of an
349 * object that referenced it. This only happens if 'node_exists' is true;
350 * otherwise the function will not access the node referred to by the
351 * directory entry, as it may already have been released from the outside.
354 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
355 boolean_t node_exists)
358 struct tmpfs_node *node;
362 MPASS(node->tn_links > 0);
367 free(de->td_name, M_TMPFSNAME);
368 uma_zfree(tmp->tm_dirent_pool, de);
371 /* --------------------------------------------------------------------- */
374 * Allocates a new vnode for the node node or returns a new reference to
375 * an existing one if the node had already a vnode referencing it. The
376 * resulting locked vnode is returned in *vpp.
378 * Returns zero on success or an appropriate error code on failure.
381 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
388 TMPFS_NODE_LOCK(node);
390 if ((vp = node->tn_vnode) != NULL) {
391 MPASS((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
393 if ((node->tn_type == VDIR && node->tn_dir.tn_parent == NULL) ||
394 ((vp->v_iflag & VI_DOOMED) != 0 &&
395 (lkflag & LK_NOWAIT) != 0)) {
397 TMPFS_NODE_UNLOCK(node);
402 if ((vp->v_iflag & VI_DOOMED) != 0) {
404 node->tn_vpstate |= TMPFS_VNODE_WRECLAIM;
405 while ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0) {
406 msleep(&node->tn_vnode, TMPFS_NODE_MTX(node),
411 TMPFS_NODE_UNLOCK(node);
412 error = vget(vp, lkflag | LK_INTERLOCK, curthread);
421 * Make sure the vnode is still there after
422 * getting the interlock to avoid racing a free.
424 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
432 if ((node->tn_vpstate & TMPFS_VNODE_DOOMED) ||
433 (node->tn_type == VDIR && node->tn_dir.tn_parent == NULL)) {
434 TMPFS_NODE_UNLOCK(node);
441 * otherwise lock the vp list while we call getnewvnode
442 * since that can block.
444 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
445 node->tn_vpstate |= TMPFS_VNODE_WANT;
446 error = msleep((caddr_t) &node->tn_vpstate,
447 TMPFS_NODE_MTX(node), PDROP | PCATCH,
448 "tmpfs_alloc_vp", 0);
454 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
456 TMPFS_NODE_UNLOCK(node);
458 /* Get a new vnode and associate it with our node. */
459 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
464 (void) vn_lock(vp, lkflag | LK_RETRY);
467 vp->v_type = node->tn_type;
469 /* Type-specific initialization. */
470 switch (node->tn_type) {
482 vp->v_op = &tmpfs_fifoop_entries;
485 MPASS(node->tn_dir.tn_parent != NULL);
486 if (node->tn_dir.tn_parent == node)
487 vp->v_vflag |= VV_ROOT;
491 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
494 vnode_pager_setsize(vp, node->tn_size);
495 error = insmntque(vp, mp);
500 TMPFS_NODE_LOCK(node);
502 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
503 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
506 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
507 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
508 TMPFS_NODE_UNLOCK(node);
509 wakeup((caddr_t) &node->tn_vpstate);
511 TMPFS_NODE_UNLOCK(node);
518 MPASS(*vpp != NULL && VOP_ISLOCKED(*vpp));
519 TMPFS_NODE_LOCK(node);
520 MPASS(*vpp == node->tn_vnode);
521 TMPFS_NODE_UNLOCK(node);
528 /* --------------------------------------------------------------------- */
531 * Destroys the association between the vnode vp and the node it
535 tmpfs_free_vp(struct vnode *vp)
537 struct tmpfs_node *node;
539 node = VP_TO_TMPFS_NODE(vp);
541 mtx_assert(TMPFS_NODE_MTX(node), MA_OWNED);
542 node->tn_vnode = NULL;
543 if ((node->tn_vpstate & TMPFS_VNODE_WRECLAIM) != 0)
544 wakeup(&node->tn_vnode);
545 node->tn_vpstate &= ~TMPFS_VNODE_WRECLAIM;
549 /* --------------------------------------------------------------------- */
552 * Allocates a new file of type 'type' and adds it to the parent directory
553 * 'dvp'; this addition is done using the component name given in 'cnp'.
554 * The ownership of the new file is automatically assigned based on the
555 * credentials of the caller (through 'cnp'), the group is set based on
556 * the parent directory and the mode is determined from the 'vap' argument.
557 * If successful, *vpp holds a vnode to the newly created file and zero
558 * is returned. Otherwise *vpp is NULL and the function returns an
559 * appropriate error code.
562 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
563 struct componentname *cnp, char *target)
566 struct tmpfs_dirent *de;
567 struct tmpfs_mount *tmp;
568 struct tmpfs_node *dnode;
569 struct tmpfs_node *node;
570 struct tmpfs_node *parent;
572 MPASS(VOP_ISLOCKED(dvp));
573 MPASS(cnp->cn_flags & HASBUF);
575 tmp = VFS_TO_TMPFS(dvp->v_mount);
576 dnode = VP_TO_TMPFS_DIR(dvp);
579 /* If the entry we are creating is a directory, we cannot overflow
580 * the number of links of its parent, because it will get a new
582 if (vap->va_type == VDIR) {
583 /* Ensure that we do not overflow the maximum number of links
584 * imposed by the system. */
585 MPASS(dnode->tn_links <= LINK_MAX);
586 if (dnode->tn_links == LINK_MAX) {
592 MPASS(parent != NULL);
596 /* Allocate a node that represents the new file. */
597 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
598 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
602 /* Allocate a directory entry that points to the new file. */
603 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
606 tmpfs_free_node(tmp, node);
610 /* Allocate a vnode for the new file. */
611 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
613 tmpfs_free_dirent(tmp, de, TRUE);
614 tmpfs_free_node(tmp, node);
618 /* Now that all required items are allocated, we can proceed to
619 * insert the new node into the directory, an operation that
621 if (cnp->cn_flags & ISWHITEOUT)
622 tmpfs_dir_whiteout_remove(dvp, cnp);
623 tmpfs_dir_attach(dvp, de);
630 /* --------------------------------------------------------------------- */
633 * Attaches the directory entry de to the directory represented by vp.
634 * Note that this does not change the link count of the node pointed by
635 * the directory entry, as this is done by tmpfs_alloc_dirent.
638 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
640 struct tmpfs_node *dnode;
642 ASSERT_VOP_ELOCKED(vp, __func__);
643 dnode = VP_TO_TMPFS_DIR(vp);
644 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries);
645 dnode->tn_size += sizeof(struct tmpfs_dirent);
646 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
650 /* --------------------------------------------------------------------- */
653 * Detaches the directory entry de from the directory represented by vp.
654 * Note that this does not change the link count of the node pointed by
655 * the directory entry, as this is done by tmpfs_free_dirent.
658 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
660 struct tmpfs_node *dnode;
662 ASSERT_VOP_ELOCKED(vp, __func__);
663 dnode = VP_TO_TMPFS_DIR(vp);
665 if (dnode->tn_dir.tn_readdir_lastp == de) {
666 dnode->tn_dir.tn_readdir_lastn = 0;
667 dnode->tn_dir.tn_readdir_lastp = NULL;
670 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries);
671 dnode->tn_size -= sizeof(struct tmpfs_dirent);
672 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
676 /* --------------------------------------------------------------------- */
679 * Looks for a directory entry in the directory represented by node.
680 * 'cnp' describes the name of the entry to look for. Note that the .
681 * and .. components are not allowed as they do not physically exist
682 * within directories.
684 * Returns a pointer to the entry when found, otherwise NULL.
686 struct tmpfs_dirent *
687 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
688 struct componentname *cnp)
691 struct tmpfs_dirent *de;
693 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
694 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
695 cnp->cn_nameptr[1] == '.')));
696 TMPFS_VALIDATE_DIR(node);
699 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
700 if (f != NULL && de->td_node != f)
702 MPASS(cnp->cn_namelen < 0xffff);
703 if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
704 bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
709 node->tn_status |= TMPFS_NODE_ACCESSED;
711 return found ? de : NULL;
714 /* --------------------------------------------------------------------- */
717 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
718 * directory and returns it in the uio space. The function returns 0
719 * on success, -1 if there was not enough space in the uio structure to
720 * hold the directory entry or an appropriate error code if another
724 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
729 TMPFS_VALIDATE_DIR(node);
730 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
732 dent.d_fileno = node->tn_id;
733 dent.d_type = DT_DIR;
735 dent.d_name[0] = '.';
736 dent.d_name[1] = '\0';
737 dent.d_reclen = GENERIC_DIRSIZ(&dent);
739 if (dent.d_reclen > uio->uio_resid)
742 error = uiomove(&dent, dent.d_reclen, uio);
744 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
747 node->tn_status |= TMPFS_NODE_ACCESSED;
752 /* --------------------------------------------------------------------- */
755 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
756 * directory and returns it in the uio space. The function returns 0
757 * on success, -1 if there was not enough space in the uio structure to
758 * hold the directory entry or an appropriate error code if another
762 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
767 TMPFS_VALIDATE_DIR(node);
768 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
771 * Return ENOENT if the current node is already removed.
773 TMPFS_ASSERT_LOCKED(node);
774 if (node->tn_dir.tn_parent == NULL) {
778 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
779 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
780 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
782 dent.d_type = DT_DIR;
784 dent.d_name[0] = '.';
785 dent.d_name[1] = '.';
786 dent.d_name[2] = '\0';
787 dent.d_reclen = GENERIC_DIRSIZ(&dent);
789 if (dent.d_reclen > uio->uio_resid)
792 error = uiomove(&dent, dent.d_reclen, uio);
794 struct tmpfs_dirent *de;
796 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
798 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
800 uio->uio_offset = tmpfs_dircookie(de);
804 node->tn_status |= TMPFS_NODE_ACCESSED;
809 /* --------------------------------------------------------------------- */
812 * Lookup a directory entry by its associated cookie.
814 struct tmpfs_dirent *
815 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
817 struct tmpfs_dirent *de;
819 if (cookie == node->tn_dir.tn_readdir_lastn &&
820 node->tn_dir.tn_readdir_lastp != NULL) {
821 return node->tn_dir.tn_readdir_lastp;
824 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
825 if (tmpfs_dircookie(de) == cookie) {
833 /* --------------------------------------------------------------------- */
836 * Helper function for tmpfs_readdir. Returns as much directory entries
837 * as can fit in the uio space. The read starts at uio->uio_offset.
838 * The function returns 0 on success, -1 if there was not enough space
839 * in the uio structure to hold the directory entry or an appropriate
840 * error code if another error happens.
843 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
847 struct tmpfs_dirent *de;
849 TMPFS_VALIDATE_DIR(node);
851 /* Locate the first directory entry we have to return. We have cached
852 * the last readdir in the node, so use those values if appropriate.
853 * Otherwise do a linear scan to find the requested entry. */
854 startcookie = uio->uio_offset;
855 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT);
856 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
857 if (startcookie == TMPFS_DIRCOOKIE_EOF) {
860 de = tmpfs_dir_lookupbycookie(node, startcookie);
866 /* Read as much entries as possible; i.e., until we reach the end of
867 * the directory or we exhaust uio space. */
871 /* Create a dirent structure representing the current
872 * tmpfs_node and fill it. */
873 if (de->td_node == NULL) {
877 d.d_fileno = de->td_node->tn_id;
878 switch (de->td_node->tn_type) {
908 panic("tmpfs_dir_getdents: type %p %d",
909 de->td_node, (int)de->td_node->tn_type);
912 d.d_namlen = de->td_namelen;
913 MPASS(de->td_namelen < sizeof(d.d_name));
914 (void)memcpy(d.d_name, de->td_name, de->td_namelen);
915 d.d_name[de->td_namelen] = '\0';
916 d.d_reclen = GENERIC_DIRSIZ(&d);
918 /* Stop reading if the directory entry we are treating is
919 * bigger than the amount of data that can be returned. */
920 if (d.d_reclen > uio->uio_resid) {
925 /* Copy the new dirent structure into the output buffer and
926 * advance pointers. */
927 error = uiomove(&d, d.d_reclen, uio);
930 de = TAILQ_NEXT(de, td_entries);
932 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
934 /* Update the offset and cache. */
936 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
937 node->tn_dir.tn_readdir_lastn = 0;
938 node->tn_dir.tn_readdir_lastp = NULL;
940 node->tn_dir.tn_readdir_lastn = uio->uio_offset = tmpfs_dircookie(de);
941 node->tn_dir.tn_readdir_lastp = de;
944 node->tn_status |= TMPFS_NODE_ACCESSED;
949 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
951 struct tmpfs_dirent *de;
954 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
955 cnp->cn_nameptr, cnp->cn_namelen, &de);
958 tmpfs_dir_attach(dvp, de);
963 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
965 struct tmpfs_dirent *de;
967 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
968 MPASS(de != NULL && de->td_node == NULL);
969 tmpfs_dir_detach(dvp, de);
970 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de, TRUE);
973 /* --------------------------------------------------------------------- */
976 * Resizes the aobj associated with the regular file pointed to by 'vp' to the
977 * size 'newsize'. 'vp' must point to a vnode that represents a regular file.
978 * 'newsize' must be positive.
980 * Returns zero on success or an appropriate error code on failure.
983 tmpfs_reg_resize(struct vnode *vp, off_t newsize, boolean_t ignerr)
985 struct tmpfs_mount *tmp;
986 struct tmpfs_node *node;
989 vm_pindex_t idx, newpages, oldpages;
993 MPASS(vp->v_type == VREG);
996 node = VP_TO_TMPFS_NODE(vp);
997 uobj = node->tn_reg.tn_aobj;
998 tmp = VFS_TO_TMPFS(vp->v_mount);
1001 * Convert the old and new sizes to the number of pages needed to
1002 * store them. It may happen that we do not need to do anything
1003 * because the last allocated page can accommodate the change on
1006 oldsize = node->tn_size;
1007 oldpages = OFF_TO_IDX(oldsize + PAGE_MASK);
1008 MPASS(oldpages == uobj->size);
1009 newpages = OFF_TO_IDX(newsize + PAGE_MASK);
1010 if (newpages > oldpages &&
1011 tmpfs_pages_check_avail(tmp, newpages - oldpages) == 0)
1014 VM_OBJECT_LOCK(uobj);
1015 if (newsize < oldsize) {
1017 * Zero the truncated part of the last page.
1019 base = newsize & PAGE_MASK;
1021 idx = OFF_TO_IDX(newsize);
1023 m = vm_page_lookup(uobj, idx);
1025 if ((m->oflags & VPO_BUSY) != 0 ||
1027 vm_page_sleep(m, "tmfssz");
1030 MPASS(m->valid == VM_PAGE_BITS_ALL);
1031 } else if (vm_pager_has_page(uobj, idx, NULL, NULL)) {
1032 m = vm_page_alloc(uobj, idx, VM_ALLOC_NORMAL);
1034 VM_OBJECT_UNLOCK(uobj);
1036 VM_OBJECT_LOCK(uobj);
1038 } else if (m->valid != VM_PAGE_BITS_ALL) {
1040 rv = vm_pager_get_pages(uobj, ma, 1, 0);
1041 m = vm_page_lookup(uobj, idx);
1043 /* A cached page was reactivated. */
1046 if (rv == VM_PAGER_OK) {
1047 vm_page_deactivate(m);
1056 VM_OBJECT_UNLOCK(uobj);
1062 pmap_zero_page_area(m, base, PAGE_SIZE - base);
1064 vm_pager_page_unswapped(m);
1069 * Release any swap space and free any whole pages.
1071 if (newpages < oldpages) {
1072 swap_pager_freespace(uobj, newpages, oldpages -
1074 vm_object_page_remove(uobj, newpages, 0, 0);
1077 uobj->size = newpages;
1078 VM_OBJECT_UNLOCK(uobj);
1081 tmp->tm_pages_used += (newpages - oldpages);
1084 node->tn_size = newsize;
1085 vnode_pager_setsize(vp, newsize);
1089 /* --------------------------------------------------------------------- */
1092 * Change flags of the given vnode.
1093 * Caller should execute tmpfs_update on vp after a successful execution.
1094 * The vnode must be locked on entry and remain locked on exit.
1097 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p)
1100 struct tmpfs_node *node;
1102 MPASS(VOP_ISLOCKED(vp));
1104 node = VP_TO_TMPFS_NODE(vp);
1106 /* Disallow this operation if the file system is mounted read-only. */
1107 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1111 * Callers may only modify the file flags on objects they
1112 * have VADMIN rights for.
1114 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1117 * Unprivileged processes are not permitted to unset system
1118 * flags, or modify flags if any system flags are set.
1120 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
1122 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1123 error = securelevel_gt(cred, 0);
1127 /* Snapshot flag cannot be set or cleared */
1128 if (((flags & SF_SNAPSHOT) != 0 &&
1129 (node->tn_flags & SF_SNAPSHOT) == 0) ||
1130 ((flags & SF_SNAPSHOT) == 0 &&
1131 (node->tn_flags & SF_SNAPSHOT) != 0))
1133 node->tn_flags = flags;
1136 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1137 (flags & UF_SETTABLE) != flags)
1139 node->tn_flags &= SF_SETTABLE;
1140 node->tn_flags |= (flags & UF_SETTABLE);
1142 node->tn_status |= TMPFS_NODE_CHANGED;
1144 MPASS(VOP_ISLOCKED(vp));
1149 /* --------------------------------------------------------------------- */
1152 * Change access mode on the given vnode.
1153 * Caller should execute tmpfs_update on vp after a successful execution.
1154 * The vnode must be locked on entry and remain locked on exit.
1157 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1160 struct tmpfs_node *node;
1162 MPASS(VOP_ISLOCKED(vp));
1164 node = VP_TO_TMPFS_NODE(vp);
1166 /* Disallow this operation if the file system is mounted read-only. */
1167 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1170 /* Immutable or append-only files cannot be modified, either. */
1171 if (node->tn_flags & (IMMUTABLE | APPEND))
1175 * To modify the permissions on a file, must possess VADMIN
1178 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1182 * Privileged processes may set the sticky bit on non-directories,
1183 * as well as set the setgid bit on a file with a group that the
1184 * process is not a member of.
1186 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1187 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1190 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1191 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1197 node->tn_mode &= ~ALLPERMS;
1198 node->tn_mode |= mode & ALLPERMS;
1200 node->tn_status |= TMPFS_NODE_CHANGED;
1202 MPASS(VOP_ISLOCKED(vp));
1207 /* --------------------------------------------------------------------- */
1210 * Change ownership of the given vnode. At least one of uid or gid must
1211 * be different than VNOVAL. If one is set to that value, the attribute
1213 * Caller should execute tmpfs_update on vp after a successful execution.
1214 * The vnode must be locked on entry and remain locked on exit.
1217 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1221 struct tmpfs_node *node;
1225 MPASS(VOP_ISLOCKED(vp));
1227 node = VP_TO_TMPFS_NODE(vp);
1229 /* Assign default values if they are unknown. */
1230 MPASS(uid != VNOVAL || gid != VNOVAL);
1235 MPASS(uid != VNOVAL && gid != VNOVAL);
1237 /* Disallow this operation if the file system is mounted read-only. */
1238 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1241 /* Immutable or append-only files cannot be modified, either. */
1242 if (node->tn_flags & (IMMUTABLE | APPEND))
1246 * To modify the ownership of a file, must possess VADMIN for that
1249 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1253 * To change the owner of a file, or change the group of a file to a
1254 * group of which we are not a member, the caller must have
1257 if ((uid != node->tn_uid ||
1258 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1259 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1262 ogid = node->tn_gid;
1263 ouid = node->tn_uid;
1268 node->tn_status |= TMPFS_NODE_CHANGED;
1270 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1271 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1272 node->tn_mode &= ~(S_ISUID | S_ISGID);
1275 MPASS(VOP_ISLOCKED(vp));
1280 /* --------------------------------------------------------------------- */
1283 * Change size of the given vnode.
1284 * Caller should execute tmpfs_update on vp after a successful execution.
1285 * The vnode must be locked on entry and remain locked on exit.
1288 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1292 struct tmpfs_node *node;
1294 MPASS(VOP_ISLOCKED(vp));
1296 node = VP_TO_TMPFS_NODE(vp);
1298 /* Decide whether this is a valid operation based on the file type. */
1300 switch (vp->v_type) {
1305 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1314 /* Allow modifications of special files even if in the file
1315 * system is mounted read-only (we are not modifying the
1316 * files themselves, but the objects they represent). */
1320 /* Anything else is unsupported. */
1324 /* Immutable or append-only files cannot be modified, either. */
1325 if (node->tn_flags & (IMMUTABLE | APPEND))
1328 error = tmpfs_truncate(vp, size);
1329 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1330 * for us, as will update tn_status; no need to do that here. */
1332 MPASS(VOP_ISLOCKED(vp));
1337 /* --------------------------------------------------------------------- */
1340 * Change access and modification times of the given vnode.
1341 * Caller should execute tmpfs_update on vp after a successful execution.
1342 * The vnode must be locked on entry and remain locked on exit.
1345 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1346 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l)
1349 struct tmpfs_node *node;
1351 MPASS(VOP_ISLOCKED(vp));
1353 node = VP_TO_TMPFS_NODE(vp);
1355 /* Disallow this operation if the file system is mounted read-only. */
1356 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1359 /* Immutable or append-only files cannot be modified, either. */
1360 if (node->tn_flags & (IMMUTABLE | APPEND))
1363 /* Determine if the user have proper privilege to update time. */
1364 if (vaflags & VA_UTIMES_NULL) {
1365 error = VOP_ACCESS(vp, VADMIN, cred, l);
1367 error = VOP_ACCESS(vp, VWRITE, cred, l);
1369 error = VOP_ACCESS(vp, VADMIN, cred, l);
1373 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1374 node->tn_status |= TMPFS_NODE_ACCESSED;
1376 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1377 node->tn_status |= TMPFS_NODE_MODIFIED;
1379 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1380 node->tn_status |= TMPFS_NODE_MODIFIED;
1382 tmpfs_itimes(vp, atime, mtime);
1384 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1385 node->tn_birthtime = *birthtime;
1386 MPASS(VOP_ISLOCKED(vp));
1391 /* --------------------------------------------------------------------- */
1392 /* Sync timestamps */
1394 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1395 const struct timespec *mod)
1397 struct tmpfs_node *node;
1398 struct timespec now;
1400 node = VP_TO_TMPFS_NODE(vp);
1402 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1403 TMPFS_NODE_CHANGED)) == 0)
1406 vfs_timestamp(&now);
1407 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1410 node->tn_atime = *acc;
1412 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1415 node->tn_mtime = *mod;
1417 if (node->tn_status & TMPFS_NODE_CHANGED) {
1418 node->tn_ctime = now;
1421 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1424 /* --------------------------------------------------------------------- */
1427 tmpfs_update(struct vnode *vp)
1430 tmpfs_itimes(vp, NULL, NULL);
1433 /* --------------------------------------------------------------------- */
1436 tmpfs_truncate(struct vnode *vp, off_t length)
1439 struct tmpfs_node *node;
1441 node = VP_TO_TMPFS_NODE(vp);
1448 if (node->tn_size == length) {
1453 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1456 error = tmpfs_reg_resize(vp, length, FALSE);
1458 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;