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/vnode.h>
46 #include <sys/vmmeter.h>
49 #include <vm/vm_object.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_pager.h>
52 #include <vm/vm_extern.h>
54 #include <fs/tmpfs/tmpfs.h>
55 #include <fs/tmpfs/tmpfs_fifoops.h>
56 #include <fs/tmpfs/tmpfs_vnops.h>
58 /* --------------------------------------------------------------------- */
61 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
62 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
63 * using the credentials of the process 'p'.
65 * If the node type is set to 'VDIR', then the parent parameter must point
66 * to the parent directory of the node being created. It may only be NULL
67 * while allocating the root node.
69 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
70 * specifies the device the node represents.
72 * If the node type is set to 'VLNK', then the parameter target specifies
73 * the file name of the target file for the symbolic link that is being
76 * Note that new nodes are retrieved from the available list if it has
77 * items or, if it is empty, from the node pool as long as there is enough
78 * space to create them.
80 * Returns zero on success or an appropriate error code on failure.
83 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
84 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
85 char *target, dev_t rdev, struct tmpfs_node **node)
87 struct tmpfs_node *nnode;
89 /* If the root directory of the 'tmp' file system is not yet
90 * allocated, this must be the request to do it. */
91 MPASS(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
93 MPASS(IFF(type == VLNK, target != NULL));
94 MPASS(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
96 if (tmp->tm_nodes_inuse > tmp->tm_nodes_max)
99 nnode = (struct tmpfs_node *)uma_zalloc_arg(
100 tmp->tm_node_pool, tmp, M_WAITOK);
102 /* Generic initialization. */
103 nnode->tn_type = type;
104 vfs_timestamp(&nnode->tn_atime);
105 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
109 nnode->tn_mode = mode;
110 nnode->tn_id = alloc_unr(tmp->tm_ino_unr);
112 /* Type-specific initialization. */
113 switch (nnode->tn_type) {
116 nnode->tn_rdev = rdev;
120 TAILQ_INIT(&nnode->tn_dir.tn_dirhead);
121 MPASS(parent != nnode);
122 MPASS(IMPLIES(parent == NULL, tmp->tm_root == NULL));
123 nnode->tn_dir.tn_parent = (parent == NULL) ? nnode : parent;
124 nnode->tn_dir.tn_readdir_lastn = 0;
125 nnode->tn_dir.tn_readdir_lastp = NULL;
127 nnode->tn_dir.tn_parent->tn_links++;
136 MPASS(strlen(target) < MAXPATHLEN);
137 nnode->tn_size = strlen(target);
138 nnode->tn_link = malloc(nnode->tn_size, M_TMPFSNAME,
140 memcpy(nnode->tn_link, target, nnode->tn_size);
144 nnode->tn_reg.tn_aobj =
145 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0,
146 NULL /* XXXKIB - tmpfs needs swap reservation */);
147 nnode->tn_reg.tn_aobj_pages = 0;
151 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
155 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
156 tmp->tm_nodes_inuse++;
163 /* --------------------------------------------------------------------- */
166 * Destroys the node pointed to by node from the file system 'tmp'.
167 * If the node does not belong to the given mount point, the results are
170 * If the node references a directory; no entries are allowed because
171 * their removal could need a recursive algorithm, something forbidden in
172 * kernel space. Furthermore, there is not need to provide such
173 * functionality (recursive removal) because the only primitives offered
174 * to the user are the removal of empty directories and the deletion of
177 * Note that nodes are not really deleted; in fact, when a node has been
178 * allocated, it cannot be deleted during the whole life of the file
179 * system. Instead, they are moved to the available list and remain there
183 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
188 TMPFS_NODE_LOCK(node);
189 MPASS(node->tn_vnode == NULL);
190 TMPFS_NODE_UNLOCK(node);
194 LIST_REMOVE(node, tn_entries);
195 tmp->tm_nodes_inuse--;
198 switch (node->tn_type) {
200 /* Do not do anything. VNON is provided to let the
201 * allocation routine clean itself easily by avoiding
202 * duplicating code in it. */
216 free(node->tn_link, M_TMPFSNAME);
220 if (node->tn_reg.tn_aobj != NULL)
221 vm_object_deallocate(node->tn_reg.tn_aobj);
222 pages = node->tn_reg.tn_aobj_pages;
226 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
229 free_unr(tmp->tm_ino_unr, node->tn_id);
230 uma_zfree(tmp->tm_node_pool, node);
233 tmp->tm_pages_used -= pages;
237 /* --------------------------------------------------------------------- */
240 * Allocates a new directory entry for the node node with a name of name.
241 * The new directory entry is returned in *de.
243 * The link count of node is increased by one to reflect the new object
246 * Returns zero on success or an appropriate error code on failure.
249 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
250 const char *name, uint16_t len, struct tmpfs_dirent **de)
252 struct tmpfs_dirent *nde;
254 nde = (struct tmpfs_dirent *)uma_zalloc(
255 tmp->tm_dirent_pool, M_WAITOK);
256 nde->td_name = malloc(len, M_TMPFSNAME, M_WAITOK);
257 nde->td_namelen = len;
258 memcpy(nde->td_name, name, len);
269 /* --------------------------------------------------------------------- */
272 * Frees a directory entry. It is the caller's responsibility to destroy
273 * the node referenced by it if needed.
275 * The link count of node is decreased by one to reflect the removal of an
276 * object that referenced it. This only happens if 'node_exists' is true;
277 * otherwise the function will not access the node referred to by the
278 * directory entry, as it may already have been released from the outside.
281 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
282 boolean_t node_exists)
285 struct tmpfs_node *node;
289 MPASS(node->tn_links > 0);
294 free(de->td_name, M_TMPFSNAME);
295 uma_zfree(tmp->tm_dirent_pool, de);
298 /* --------------------------------------------------------------------- */
301 * Allocates a new vnode for the node node or returns a new reference to
302 * an existing one if the node had already a vnode referencing it. The
303 * resulting locked vnode is returned in *vpp.
305 * Returns zero on success or an appropriate error code on failure.
308 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
315 TMPFS_NODE_LOCK(node);
316 if ((vp = node->tn_vnode) != NULL) {
318 TMPFS_NODE_UNLOCK(node);
320 (void) vget(vp, lkflag | LK_INTERLOCK | LK_RETRY, curthread);
324 * Make sure the vnode is still there after
325 * getting the interlock to avoid racing a free.
327 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
336 * otherwise lock the vp list while we call getnewvnode
337 * since that can block.
339 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
340 node->tn_vpstate |= TMPFS_VNODE_WANT;
341 error = msleep((caddr_t) &node->tn_vpstate,
342 TMPFS_NODE_MTX(node), PDROP | PCATCH,
343 "tmpfs_alloc_vp", 0);
349 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
351 TMPFS_NODE_UNLOCK(node);
353 /* Get a new vnode and associate it with our node. */
354 error = getnewvnode("tmpfs", mp, &tmpfs_vnodeop_entries, &vp);
359 (void) vn_lock(vp, lkflag | LK_RETRY);
362 vp->v_type = node->tn_type;
364 /* Type-specific initialization. */
365 switch (node->tn_type) {
377 vp->v_op = &tmpfs_fifoop_entries;
380 if (node->tn_dir.tn_parent == node)
381 vp->v_vflag |= VV_ROOT;
385 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
388 vnode_pager_setsize(vp, node->tn_size);
389 error = insmntque(vp, mp);
394 TMPFS_NODE_LOCK(node);
396 MPASS(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
397 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
400 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
401 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
402 TMPFS_NODE_UNLOCK(node);
403 wakeup((caddr_t) &node->tn_vpstate);
405 TMPFS_NODE_UNLOCK(node);
410 MPASS(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
412 TMPFS_NODE_LOCK(node);
413 MPASS(*vpp == node->tn_vnode);
414 TMPFS_NODE_UNLOCK(node);
420 /* --------------------------------------------------------------------- */
423 * Destroys the association between the vnode vp and the node it
427 tmpfs_free_vp(struct vnode *vp)
429 struct tmpfs_node *node;
431 node = VP_TO_TMPFS_NODE(vp);
433 TMPFS_NODE_LOCK(node);
434 node->tn_vnode = NULL;
436 TMPFS_NODE_UNLOCK(node);
439 /* --------------------------------------------------------------------- */
442 * Allocates a new file of type 'type' and adds it to the parent directory
443 * 'dvp'; this addition is done using the component name given in 'cnp'.
444 * The ownership of the new file is automatically assigned based on the
445 * credentials of the caller (through 'cnp'), the group is set based on
446 * the parent directory and the mode is determined from the 'vap' argument.
447 * If successful, *vpp holds a vnode to the newly created file and zero
448 * is returned. Otherwise *vpp is NULL and the function returns an
449 * appropriate error code.
452 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
453 struct componentname *cnp, char *target)
456 struct tmpfs_dirent *de;
457 struct tmpfs_mount *tmp;
458 struct tmpfs_node *dnode;
459 struct tmpfs_node *node;
460 struct tmpfs_node *parent;
462 MPASS(VOP_ISLOCKED(dvp));
463 MPASS(cnp->cn_flags & HASBUF);
465 tmp = VFS_TO_TMPFS(dvp->v_mount);
466 dnode = VP_TO_TMPFS_DIR(dvp);
469 /* If the entry we are creating is a directory, we cannot overflow
470 * the number of links of its parent, because it will get a new
472 if (vap->va_type == VDIR) {
473 /* Ensure that we do not overflow the maximum number of links
474 * imposed by the system. */
475 MPASS(dnode->tn_links <= LINK_MAX);
476 if (dnode->tn_links == LINK_MAX) {
482 MPASS(parent != NULL);
486 /* Allocate a node that represents the new file. */
487 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
488 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
492 /* Allocate a directory entry that points to the new file. */
493 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
496 tmpfs_free_node(tmp, node);
500 /* Allocate a vnode for the new file. */
501 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
503 tmpfs_free_dirent(tmp, de, TRUE);
504 tmpfs_free_node(tmp, node);
508 /* Now that all required items are allocated, we can proceed to
509 * insert the new node into the directory, an operation that
511 if (cnp->cn_flags & ISWHITEOUT)
512 tmpfs_dir_whiteout_remove(dvp, cnp);
513 tmpfs_dir_attach(dvp, de);
520 /* --------------------------------------------------------------------- */
523 * Attaches the directory entry de to the directory represented by vp.
524 * Note that this does not change the link count of the node pointed by
525 * the directory entry, as this is done by tmpfs_alloc_dirent.
528 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
530 struct tmpfs_node *dnode;
532 ASSERT_VOP_ELOCKED(vp, __func__);
533 dnode = VP_TO_TMPFS_DIR(vp);
534 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries);
535 dnode->tn_size += sizeof(struct tmpfs_dirent);
536 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
540 /* --------------------------------------------------------------------- */
543 * Detaches the directory entry de from the directory represented by vp.
544 * Note that this does not change the link count of the node pointed by
545 * the directory entry, as this is done by tmpfs_free_dirent.
548 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
550 struct tmpfs_node *dnode;
552 ASSERT_VOP_ELOCKED(vp, __func__);
553 dnode = VP_TO_TMPFS_DIR(vp);
555 if (dnode->tn_dir.tn_readdir_lastp == de) {
556 dnode->tn_dir.tn_readdir_lastn = 0;
557 dnode->tn_dir.tn_readdir_lastp = NULL;
560 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries);
561 dnode->tn_size -= sizeof(struct tmpfs_dirent);
562 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
566 /* --------------------------------------------------------------------- */
569 * Looks for a directory entry in the directory represented by node.
570 * 'cnp' describes the name of the entry to look for. Note that the .
571 * and .. components are not allowed as they do not physically exist
572 * within directories.
574 * Returns a pointer to the entry when found, otherwise NULL.
576 struct tmpfs_dirent *
577 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
578 struct componentname *cnp)
581 struct tmpfs_dirent *de;
583 MPASS(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
584 MPASS(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
585 cnp->cn_nameptr[1] == '.')));
586 TMPFS_VALIDATE_DIR(node);
589 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
590 if (f != NULL && de->td_node != f)
592 MPASS(cnp->cn_namelen < 0xffff);
593 if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
594 bcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
599 node->tn_status |= TMPFS_NODE_ACCESSED;
601 return found ? de : NULL;
604 /* --------------------------------------------------------------------- */
607 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
608 * directory and returns it in the uio space. The function returns 0
609 * on success, -1 if there was not enough space in the uio structure to
610 * hold the directory entry or an appropriate error code if another
614 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
619 TMPFS_VALIDATE_DIR(node);
620 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
622 dent.d_fileno = node->tn_id;
623 dent.d_type = DT_DIR;
625 dent.d_name[0] = '.';
626 dent.d_name[1] = '\0';
627 dent.d_reclen = GENERIC_DIRSIZ(&dent);
629 if (dent.d_reclen > uio->uio_resid)
632 error = uiomove(&dent, dent.d_reclen, uio);
634 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
637 node->tn_status |= TMPFS_NODE_ACCESSED;
642 /* --------------------------------------------------------------------- */
645 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
646 * directory and returns it in the uio space. The function returns 0
647 * on success, -1 if there was not enough space in the uio structure to
648 * hold the directory entry or an appropriate error code if another
652 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
657 TMPFS_VALIDATE_DIR(node);
658 MPASS(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
660 dent.d_fileno = node->tn_dir.tn_parent->tn_id;
661 dent.d_type = DT_DIR;
663 dent.d_name[0] = '.';
664 dent.d_name[1] = '.';
665 dent.d_name[2] = '\0';
666 dent.d_reclen = GENERIC_DIRSIZ(&dent);
668 if (dent.d_reclen > uio->uio_resid)
671 error = uiomove(&dent, dent.d_reclen, uio);
673 struct tmpfs_dirent *de;
675 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
677 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
679 uio->uio_offset = tmpfs_dircookie(de);
683 node->tn_status |= TMPFS_NODE_ACCESSED;
688 /* --------------------------------------------------------------------- */
691 * Lookup a directory entry by its associated cookie.
693 struct tmpfs_dirent *
694 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
696 struct tmpfs_dirent *de;
698 if (cookie == node->tn_dir.tn_readdir_lastn &&
699 node->tn_dir.tn_readdir_lastp != NULL) {
700 return node->tn_dir.tn_readdir_lastp;
703 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
704 if (tmpfs_dircookie(de) == cookie) {
712 /* --------------------------------------------------------------------- */
715 * Helper function for tmpfs_readdir. Returns as much directory entries
716 * as can fit in the uio space. The read starts at uio->uio_offset.
717 * The function returns 0 on success, -1 if there was not enough space
718 * in the uio structure to hold the directory entry or an appropriate
719 * error code if another error happens.
722 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
726 struct tmpfs_dirent *de;
728 TMPFS_VALIDATE_DIR(node);
730 /* Locate the first directory entry we have to return. We have cached
731 * the last readdir in the node, so use those values if appropriate.
732 * Otherwise do a linear scan to find the requested entry. */
733 startcookie = uio->uio_offset;
734 MPASS(startcookie != TMPFS_DIRCOOKIE_DOT);
735 MPASS(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
736 if (startcookie == TMPFS_DIRCOOKIE_EOF) {
739 de = tmpfs_dir_lookupbycookie(node, startcookie);
745 /* Read as much entries as possible; i.e., until we reach the end of
746 * the directory or we exhaust uio space. */
750 /* Create a dirent structure representing the current
751 * tmpfs_node and fill it. */
752 if (de->td_node == NULL) {
756 d.d_fileno = de->td_node->tn_id;
757 switch (de->td_node->tn_type) {
787 panic("tmpfs_dir_getdents: type %p %d",
788 de->td_node, (int)de->td_node->tn_type);
791 d.d_namlen = de->td_namelen;
792 MPASS(de->td_namelen < sizeof(d.d_name));
793 (void)memcpy(d.d_name, de->td_name, de->td_namelen);
794 d.d_name[de->td_namelen] = '\0';
795 d.d_reclen = GENERIC_DIRSIZ(&d);
797 /* Stop reading if the directory entry we are treating is
798 * bigger than the amount of data that can be returned. */
799 if (d.d_reclen > uio->uio_resid) {
804 /* Copy the new dirent structure into the output buffer and
805 * advance pointers. */
806 error = uiomove(&d, d.d_reclen, uio);
809 de = TAILQ_NEXT(de, td_entries);
810 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
812 /* Update the offset and cache. */
814 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
815 node->tn_dir.tn_readdir_lastn = 0;
816 node->tn_dir.tn_readdir_lastp = NULL;
818 node->tn_dir.tn_readdir_lastn = uio->uio_offset = tmpfs_dircookie(de);
819 node->tn_dir.tn_readdir_lastp = de;
822 node->tn_status |= TMPFS_NODE_ACCESSED;
827 tmpfs_dir_whiteout_add(struct vnode *dvp, struct componentname *cnp)
829 struct tmpfs_dirent *de;
832 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(dvp->v_mount), NULL,
833 cnp->cn_nameptr, cnp->cn_namelen, &de);
836 tmpfs_dir_attach(dvp, de);
841 tmpfs_dir_whiteout_remove(struct vnode *dvp, struct componentname *cnp)
843 struct tmpfs_dirent *de;
845 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
846 MPASS(de != NULL && de->td_node == NULL);
847 tmpfs_dir_detach(dvp, de);
848 tmpfs_free_dirent(VFS_TO_TMPFS(dvp->v_mount), de, TRUE);
851 /* --------------------------------------------------------------------- */
854 * Resizes the aobj associated to the regular file pointed to by vp to
855 * the size newsize. 'vp' must point to a vnode that represents a regular
856 * file. 'newsize' must be positive.
858 * Returns zero on success or an appropriate error code on failure.
861 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
864 size_t newpages, oldpages;
865 struct tmpfs_mount *tmp;
866 struct tmpfs_node *node;
869 MPASS(vp->v_type == VREG);
872 node = VP_TO_TMPFS_NODE(vp);
873 tmp = VFS_TO_TMPFS(vp->v_mount);
875 /* Convert the old and new sizes to the number of pages needed to
876 * store them. It may happen that we do not need to do anything
877 * because the last allocated page can accommodate the change on
879 oldsize = node->tn_size;
880 oldpages = round_page(oldsize) / PAGE_SIZE;
881 MPASS(oldpages == node->tn_reg.tn_aobj_pages);
882 newpages = round_page(newsize) / PAGE_SIZE;
884 if (newpages > oldpages &&
885 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) {
890 node->tn_reg.tn_aobj_pages = newpages;
893 tmp->tm_pages_used += (newpages - oldpages);
896 node->tn_size = newsize;
897 vnode_pager_setsize(vp, newsize);
898 if (newsize < oldsize) {
899 size_t zerolen = round_page(newsize) - newsize;
900 vm_object_t uobj = node->tn_reg.tn_aobj;
904 * free "backing store"
906 VM_OBJECT_LOCK(uobj);
907 if (newpages < oldpages) {
908 swap_pager_freespace(uobj,
909 newpages, oldpages - newpages);
910 vm_object_page_remove(uobj,
911 OFF_TO_IDX(newsize + PAGE_MASK), 0, FALSE);
915 * zero out the truncated part of the last page.
919 m = vm_page_grab(uobj, OFF_TO_IDX(newsize),
920 VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
921 pmap_zero_page_area(m, PAGE_SIZE - zerolen,
925 VM_OBJECT_UNLOCK(uobj);
935 /* --------------------------------------------------------------------- */
938 * Change flags of the given vnode.
939 * Caller should execute tmpfs_update on vp after a successful execution.
940 * The vnode must be locked on entry and remain locked on exit.
943 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct thread *p)
946 struct tmpfs_node *node;
948 MPASS(VOP_ISLOCKED(vp));
950 node = VP_TO_TMPFS_NODE(vp);
952 /* Disallow this operation if the file system is mounted read-only. */
953 if (vp->v_mount->mnt_flag & MNT_RDONLY)
957 * Callers may only modify the file flags on objects they
958 * have VADMIN rights for.
960 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
963 * Unprivileged processes are not permitted to unset system
964 * flags, or modify flags if any system flags are set.
966 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
968 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
969 error = securelevel_gt(cred, 0);
973 /* Snapshot flag cannot be set or cleared */
974 if (((flags & SF_SNAPSHOT) != 0 &&
975 (node->tn_flags & SF_SNAPSHOT) == 0) ||
976 ((flags & SF_SNAPSHOT) == 0 &&
977 (node->tn_flags & SF_SNAPSHOT) != 0))
979 node->tn_flags = flags;
982 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
983 (flags & UF_SETTABLE) != flags)
985 node->tn_flags &= SF_SETTABLE;
986 node->tn_flags |= (flags & UF_SETTABLE);
988 node->tn_status |= TMPFS_NODE_CHANGED;
990 MPASS(VOP_ISLOCKED(vp));
995 /* --------------------------------------------------------------------- */
998 * Change access mode on the given vnode.
999 * Caller should execute tmpfs_update on vp after a successful execution.
1000 * The vnode must be locked on entry and remain locked on exit.
1003 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct thread *p)
1006 struct tmpfs_node *node;
1008 MPASS(VOP_ISLOCKED(vp));
1010 node = VP_TO_TMPFS_NODE(vp);
1012 /* Disallow this operation if the file system is mounted read-only. */
1013 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1016 /* Immutable or append-only files cannot be modified, either. */
1017 if (node->tn_flags & (IMMUTABLE | APPEND))
1021 * To modify the permissions on a file, must possess VADMIN
1024 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1028 * Privileged processes may set the sticky bit on non-directories,
1029 * as well as set the setgid bit on a file with a group that the
1030 * process is not a member of.
1032 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1033 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1036 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1037 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1043 node->tn_mode &= ~ALLPERMS;
1044 node->tn_mode |= mode & ALLPERMS;
1046 node->tn_status |= TMPFS_NODE_CHANGED;
1048 MPASS(VOP_ISLOCKED(vp));
1053 /* --------------------------------------------------------------------- */
1056 * Change ownership of the given vnode. At least one of uid or gid must
1057 * be different than VNOVAL. If one is set to that value, the attribute
1059 * Caller should execute tmpfs_update on vp after a successful execution.
1060 * The vnode must be locked on entry and remain locked on exit.
1063 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1067 struct tmpfs_node *node;
1071 MPASS(VOP_ISLOCKED(vp));
1073 node = VP_TO_TMPFS_NODE(vp);
1075 /* Assign default values if they are unknown. */
1076 MPASS(uid != VNOVAL || gid != VNOVAL);
1081 MPASS(uid != VNOVAL && gid != VNOVAL);
1083 /* Disallow this operation if the file system is mounted read-only. */
1084 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1087 /* Immutable or append-only files cannot be modified, either. */
1088 if (node->tn_flags & (IMMUTABLE | APPEND))
1092 * To modify the ownership of a file, must possess VADMIN for that
1095 if ((error = VOP_ACCESS(vp, VADMIN, cred, p)))
1099 * To change the owner of a file, or change the group of a file to a
1100 * group of which we are not a member, the caller must have
1103 if ((uid != node->tn_uid ||
1104 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1105 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1108 ogid = node->tn_gid;
1109 ouid = node->tn_uid;
1114 node->tn_status |= TMPFS_NODE_CHANGED;
1116 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1117 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1118 node->tn_mode &= ~(S_ISUID | S_ISGID);
1121 MPASS(VOP_ISLOCKED(vp));
1126 /* --------------------------------------------------------------------- */
1129 * Change size of the given vnode.
1130 * Caller should execute tmpfs_update on vp after a successful execution.
1131 * The vnode must be locked on entry and remain locked on exit.
1134 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1138 struct tmpfs_node *node;
1140 MPASS(VOP_ISLOCKED(vp));
1142 node = VP_TO_TMPFS_NODE(vp);
1144 /* Decide whether this is a valid operation based on the file type. */
1146 switch (vp->v_type) {
1151 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1160 /* Allow modifications of special files even if in the file
1161 * system is mounted read-only (we are not modifying the
1162 * files themselves, but the objects they represent). */
1166 /* Anything else is unsupported. */
1170 /* Immutable or append-only files cannot be modified, either. */
1171 if (node->tn_flags & (IMMUTABLE | APPEND))
1174 error = tmpfs_truncate(vp, size);
1175 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1176 * for us, as will update tn_status; no need to do that here. */
1178 MPASS(VOP_ISLOCKED(vp));
1183 /* --------------------------------------------------------------------- */
1186 * Change access and modification times of the given vnode.
1187 * Caller should execute tmpfs_update on vp after a successful execution.
1188 * The vnode must be locked on entry and remain locked on exit.
1191 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1192 struct timespec *birthtime, int vaflags, struct ucred *cred, struct thread *l)
1195 struct tmpfs_node *node;
1197 MPASS(VOP_ISLOCKED(vp));
1199 node = VP_TO_TMPFS_NODE(vp);
1201 /* Disallow this operation if the file system is mounted read-only. */
1202 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1205 /* Immutable or append-only files cannot be modified, either. */
1206 if (node->tn_flags & (IMMUTABLE | APPEND))
1209 /* Determine if the user have proper privilege to update time. */
1210 if (vaflags & VA_UTIMES_NULL) {
1211 error = VOP_ACCESS(vp, VADMIN, cred, l);
1213 error = VOP_ACCESS(vp, VWRITE, cred, l);
1215 error = VOP_ACCESS(vp, VADMIN, cred, l);
1219 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1220 node->tn_status |= TMPFS_NODE_ACCESSED;
1222 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1223 node->tn_status |= TMPFS_NODE_MODIFIED;
1225 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1226 node->tn_status |= TMPFS_NODE_MODIFIED;
1228 tmpfs_itimes(vp, atime, mtime);
1230 if (birthtime->tv_nsec != VNOVAL && birthtime->tv_nsec != VNOVAL)
1231 node->tn_birthtime = *birthtime;
1232 MPASS(VOP_ISLOCKED(vp));
1237 /* --------------------------------------------------------------------- */
1238 /* Sync timestamps */
1240 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1241 const struct timespec *mod)
1243 struct tmpfs_node *node;
1244 struct timespec now;
1246 node = VP_TO_TMPFS_NODE(vp);
1248 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1249 TMPFS_NODE_CHANGED)) == 0)
1252 vfs_timestamp(&now);
1253 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1256 node->tn_atime = *acc;
1258 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1261 node->tn_mtime = *mod;
1263 if (node->tn_status & TMPFS_NODE_CHANGED) {
1264 node->tn_ctime = now;
1267 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1270 /* --------------------------------------------------------------------- */
1273 tmpfs_update(struct vnode *vp)
1276 tmpfs_itimes(vp, NULL, NULL);
1279 /* --------------------------------------------------------------------- */
1282 tmpfs_truncate(struct vnode *vp, off_t length)
1285 struct tmpfs_node *node;
1287 node = VP_TO_TMPFS_NODE(vp);
1294 if (node->tn_size == length) {
1299 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1302 error = tmpfs_reg_resize(vp, length);
1304 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;