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>
38 #include <sys/kernel.h>
39 #include <sys/param.h>
40 #include <sys/namei.h>
43 #include <sys/spinlock2.h>
45 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 #include <sys/vmmeter.h>
49 #include <sys/mplock2.h>
52 #include <vm/vm_object.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_pager.h>
55 #include <vm/vm_extern.h>
57 #include <vfs/tmpfs/tmpfs.h>
58 #include <vfs/tmpfs/tmpfs_fifoops.h>
59 #include <vfs/tmpfs/tmpfs_vnops.h>
61 static ino_t t_ino
= 2;
62 static struct spinlock ino_lock
;
63 static ino_t
tmpfs_fetch_ino(void);
65 /* --------------------------------------------------------------------- */
68 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
69 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
70 * using the credentials of the process 'p'.
72 * If the node type is set to 'VDIR', then the parent parameter must point
73 * to the parent directory of the node being created. It may only be NULL
74 * while allocating the root node.
76 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
77 * specifies the device the node represents.
79 * If the node type is set to 'VLNK', then the parameter target specifies
80 * the file name of the target file for the symbolic link that is being
83 * Note that new nodes are retrieved from the available list if it has
84 * items or, if it is empty, from the node pool as long as there is enough
85 * space to create them.
87 * Returns zero on success or an appropriate error code on failure.
90 tmpfs_alloc_node(struct tmpfs_mount
*tmp
, enum vtype type
,
91 uid_t uid
, gid_t gid
, mode_t mode
, struct tmpfs_node
*parent
,
92 char *target
, int rmajor
, int rminor
, struct tmpfs_node
**node
)
94 struct tmpfs_node
*nnode
;
98 /* If the root directory of the 'tmp' file system is not yet
99 * allocated, this must be the request to do it. */
100 KKASSERT(IMPLIES(tmp
->tm_root
== NULL
, parent
== NULL
&& type
== VDIR
));
102 KKASSERT(IFF(type
== VLNK
, target
!= NULL
));
103 KKASSERT(IFF(type
== VBLK
|| type
== VCHR
, rmajor
!= VNOVAL
));
105 if (tmp
->tm_nodes_inuse
> tmp
->tm_nodes_max
)
108 nnode
= (struct tmpfs_node
*)objcache_get(tmp
->tm_node_pool
, M_WAITOK
);
110 /* Generic initialization. */
111 nnode
->tn_type
= type
;
113 nnode
->tn_ctime
= nnode
->tn_mtime
= nnode
->tn_atime
115 nnode
->tn_ctimensec
= nnode
->tn_mtimensec
= nnode
->tn_atimensec
119 nnode
->tn_mode
= mode
;
120 nnode
->tn_id
= tmpfs_fetch_ino();
122 /* Type-specific initialization. */
123 switch (nnode
->tn_type
) {
126 rdev
= makeudev(rmajor
, rminor
);
127 if (rdev
== NOUDEV
) {
130 nnode
->tn_rdev
= rdev
;
134 TAILQ_INIT(&nnode
->tn_dir
.tn_dirhead
);
135 KKASSERT(parent
!= nnode
);
136 KKASSERT(IMPLIES(parent
== NULL
, tmp
->tm_root
== NULL
));
137 nnode
->tn_dir
.tn_parent
= (parent
== NULL
) ? nnode
: parent
;
138 nnode
->tn_dir
.tn_readdir_lastn
= 0;
139 nnode
->tn_dir
.tn_readdir_lastp
= NULL
;
142 TMPFS_NODE_LOCK(nnode
->tn_dir
.tn_parent
);
143 nnode
->tn_dir
.tn_parent
->tn_links
++;
144 TMPFS_NODE_UNLOCK(nnode
->tn_dir
.tn_parent
);
153 KKASSERT((strlen(target
) +1) < MAXPATHLEN
);
154 nnode
->tn_size
= strlen(target
) +1;
155 nnode
->tn_link
= kmalloc(nnode
->tn_size
, M_TMPFSNAME
,
157 bcopy(target
, nnode
->tn_link
, nnode
->tn_size
);
158 nnode
->tn_link
[nnode
->tn_size
] = '\0';
162 nnode
->tn_reg
.tn_aobj
=
163 vm_pager_allocate(OBJT_SWAP
, NULL
, 0, VM_PROT_DEFAULT
, 0);
164 nnode
->tn_reg
.tn_aobj_pages
= 0;
169 panic("tmpfs_alloc_node: type %p %d", nnode
, (int)nnode
->tn_type
);
172 TMPFS_NODE_LOCK(nnode
);
173 LIST_INSERT_HEAD(&tmp
->tm_nodes_used
, nnode
, tn_entries
);
175 tmp
->tm_nodes_inuse
++;
177 TMPFS_NODE_UNLOCK(nnode
);
183 /* --------------------------------------------------------------------- */
186 * Destroys the node pointed to by node from the file system 'tmp'.
187 * If the node does not belong to the given mount point, the results are
190 * If the node references a directory; no entries are allowed because
191 * their removal could need a recursive algorithm, something forbidden in
192 * kernel space. Furthermore, there is not need to provide such
193 * functionality (recursive removal) because the only primitives offered
194 * to the user are the removal of empty directories and the deletion of
197 * Note that nodes are not really deleted; in fact, when a node has been
198 * allocated, it cannot be deleted during the whole life of the file
199 * system. Instead, they are moved to the available list and remain there
203 tmpfs_free_node(struct tmpfs_mount
*tmp
, struct tmpfs_node
*node
)
207 TMPFS_NODE_LOCK(node
);
210 TMPFS_ASSERT_ELOCKED(node
);
211 KKASSERT(node
->tn_vnode
== NULL
);
212 KKASSERT((node
->tn_vpstate
& TMPFS_VNODE_ALLOCATING
) == 0);
215 LIST_REMOVE(node
, tn_entries
);
217 tmp
->tm_nodes_inuse
--;
220 switch (node
->tn_type
) {
222 /* Do not do anything. VNON is provided to let the
223 * allocation routine clean itself easily by avoiding
224 * duplicating code in it. */
234 TMPFS_NODE_LOCK(node
->tn_dir
.tn_parent
);
235 node
->tn_dir
.tn_parent
->tn_links
--;
236 TMPFS_NODE_UNLOCK(node
->tn_dir
.tn_parent
);
244 kfree(node
->tn_link
, M_TMPFSNAME
);
248 if (node
->tn_reg
.tn_aobj
!= NULL
)
249 vm_pager_deallocate(node
->tn_reg
.tn_aobj
);
250 node
->tn_reg
.tn_aobj
= NULL
;
251 pages
= node
->tn_reg
.tn_aobj_pages
;
255 panic("tmpfs_free_node: type %p %d", node
, (int)node
->tn_type
);
258 objcache_put(tmp
->tm_node_pool
, node
);
261 tmp
->tm_pages_used
-= pages
;
263 TMPFS_NODE_UNLOCK(node
);
266 /* --------------------------------------------------------------------- */
269 * Allocates a new directory entry for the node node with a name of name.
270 * The new directory entry is returned in *de.
272 * The link count of node is increased by one to reflect the new object
275 * Returns zero on success or an appropriate error code on failure.
278 tmpfs_alloc_dirent(struct tmpfs_mount
*tmp
, struct tmpfs_node
*node
,
279 const char *name
, uint16_t len
, struct tmpfs_dirent
**de
)
281 struct tmpfs_dirent
*nde
;
284 nde
= (struct tmpfs_dirent
*)objcache_get(tmp
->tm_dirent_pool
, M_WAITOK
);
285 nde
->td_name
= kmalloc(len
, M_TMPFSNAME
, M_WAITOK
);
286 nde
->td_namelen
= len
;
287 bcopy(name
, nde
->td_name
, len
);
288 nde
->td_name
[len
] = '\0';
292 TMPFS_NODE_LOCK(node
);
294 TMPFS_NODE_UNLOCK(node
);
301 /* --------------------------------------------------------------------- */
304 * Frees a directory entry. It is the caller's responsibility to destroy
305 * the node referenced by it if needed.
307 * The link count of node is decreased by one to reflect the removal of an
308 * object that referenced it. This only happens if 'node_exists' is true;
309 * otherwise the function will not access the node referred to by the
310 * directory entry, as it may already have been released from the outside.
313 tmpfs_free_dirent(struct tmpfs_mount
*tmp
, struct tmpfs_dirent
*de
,
314 boolean_t node_exists
)
317 struct tmpfs_node
*node
;
321 TMPFS_NODE_LOCK(node
);
322 TMPFS_ASSERT_ELOCKED(node
);
323 KKASSERT(node
->tn_links
> 0);
325 TMPFS_NODE_UNLOCK(node
);
328 kfree(de
->td_name
, M_TMPFSNAME
);
329 objcache_put(tmp
->tm_dirent_pool
, de
);
332 /* --------------------------------------------------------------------- */
335 * Allocates a new vnode for the node node or returns a new reference to
336 * an existing one if the node had already a vnode referencing it. The
337 * resulting locked vnode is returned in *vpp.
339 * Returns zero on success or an appropriate error code on failure.
342 tmpfs_alloc_vp(struct mount
*mp
, struct tmpfs_node
*node
, int lkflag
,
349 TMPFS_NODE_LOCK(node
);
350 if ((vp
= node
->tn_vnode
) != NULL
) {
351 KKASSERT((node
->tn_vpstate
& TMPFS_VNODE_DOOMED
) == 0);
352 TMPFS_NODE_UNLOCK(node
);
353 (void) vget(vp
, lkflag
| LK_EXCLUSIVE
| LK_RETRY
);
356 * Make sure the vnode is still there after
357 * getting the interlock to avoid racing a free.
359 if (node
->tn_vnode
== NULL
|| node
->tn_vnode
!= vp
) {
367 if ((node
->tn_vpstate
& TMPFS_VNODE_DOOMED
) ||
368 (node
->tn_type
== VDIR
&& node
->tn_dir
.tn_parent
== NULL
)) {
369 TMPFS_NODE_UNLOCK(node
);
376 * otherwise lock the vp list while we call getnewvnode
377 * since that can block.
379 if (node
->tn_vpstate
& TMPFS_VNODE_ALLOCATING
) {
380 node
->tn_vpstate
|= TMPFS_VNODE_WANT
;
381 error
= tsleep((caddr_t
) &node
->tn_vpstate
,
382 PINTERLOCKED
| PCATCH
, "tmpfs_alloc_vp", 0);
383 TMPFS_NODE_UNLOCK(node
);
389 node
->tn_vpstate
|= TMPFS_VNODE_ALLOCATING
;
391 TMPFS_NODE_UNLOCK(node
);
393 /* Get a new vnode and associate it with our node. */
394 error
= getnewvnode(VT_TMPFS
, mp
, &vp
, VLKTIMEOUT
, LK_CANRECURSE
);
397 KKASSERT(vp
!= NULL
);
400 vp
->v_type
= node
->tn_type
;
402 /* Type-specific initialization. */
403 switch (node
->tn_type
) {
411 vinitvmio(vp
, node
->tn_size
);
414 if (node
->tn_size
>= vp
->v_mount
->mnt_maxsymlinklen
)
415 vinitvmio(vp
, node
->tn_size
);
418 vp
->v_ops
= &mp
->mnt_vn_fifo_ops
;
421 vinitvmio(vp
, node
->tn_size
);
422 KKASSERT(node
->tn_dir
.tn_parent
!= NULL
);
426 panic("tmpfs_alloc_vp: type %p %d", node
, (int)node
->tn_type
);
432 TMPFS_NODE_LOCK(node
);
434 KKASSERT(node
->tn_vpstate
& TMPFS_VNODE_ALLOCATING
);
435 node
->tn_vpstate
&= ~TMPFS_VNODE_ALLOCATING
;
438 if (node
->tn_vpstate
& TMPFS_VNODE_WANT
) {
439 node
->tn_vpstate
&= ~TMPFS_VNODE_WANT
;
440 TMPFS_NODE_UNLOCK(node
);
441 wakeup((caddr_t
) &node
->tn_vpstate
);
443 TMPFS_NODE_UNLOCK(node
);
448 KKASSERT(IFF(error
== 0, *vpp
!= NULL
&& vn_islocked(*vpp
)));
450 TMPFS_NODE_LOCK(node
);
451 KKASSERT(*vpp
== node
->tn_vnode
);
452 TMPFS_NODE_UNLOCK(node
);
458 /* --------------------------------------------------------------------- */
461 * Destroys the association between the vnode vp and the node it
465 tmpfs_free_vp(struct vnode
*vp
)
467 struct tmpfs_node
*node
;
469 node
= VP_TO_TMPFS_NODE(vp
);
471 TMPFS_NODE_LOCK(node
);
472 KKASSERT(lockcount(TMPFS_NODE_MTX(node
)) > 0);
473 node
->tn_vnode
= NULL
;
474 TMPFS_NODE_UNLOCK(node
);
478 /* --------------------------------------------------------------------- */
481 * Allocates a new file of type 'type' and adds it to the parent directory
482 * 'dvp'; this addition is done using the component name given in 'cnp'.
483 * The ownership of the new file is automatically assigned based on the
484 * credentials of the caller (through 'cnp'), the group is set based on
485 * the parent directory and the mode is determined from the 'vap' argument.
486 * If successful, *vpp holds a vnode to the newly created file and zero
487 * is returned. Otherwise *vpp is NULL and the function returns an
488 * appropriate error code.
491 tmpfs_alloc_file(struct vnode
*dvp
, struct vnode
**vpp
, struct vattr
*vap
,
492 struct namecache
*ncp
, struct ucred
*cred
, char *target
)
495 struct tmpfs_dirent
*de
;
496 struct tmpfs_mount
*tmp
;
497 struct tmpfs_node
*dnode
;
498 struct tmpfs_node
*node
;
499 struct tmpfs_node
*parent
;
501 tmp
= VFS_TO_TMPFS(dvp
->v_mount
);
502 dnode
= VP_TO_TMPFS_DIR(dvp
);
505 /* If the entry we are creating is a directory, we cannot overflow
506 * the number of links of its parent, because it will get a new
508 if (vap
->va_type
== VDIR
) {
509 /* Ensure that we do not overflow the maximum number of links
510 * imposed by the system. */
511 KKASSERT(dnode
->tn_links
<= LINK_MAX
);
512 if (dnode
->tn_links
== LINK_MAX
) {
517 KKASSERT(parent
!= NULL
);
521 /* Allocate a node that represents the new file. */
522 error
= tmpfs_alloc_node(tmp
, vap
->va_type
, cred
->cr_uid
,
523 dnode
->tn_gid
, vap
->va_mode
, parent
, target
, vap
->va_rmajor
, vap
->va_rminor
, &node
);
527 /* Allocate a directory entry that points to the new file. */
528 error
= tmpfs_alloc_dirent(tmp
, node
, ncp
->nc_name
, ncp
->nc_nlen
,
531 tmpfs_free_node(tmp
, node
);
535 /* Allocate a vnode for the new file. */
536 error
= tmpfs_alloc_vp(dvp
->v_mount
, node
, LK_EXCLUSIVE
, vpp
);
538 tmpfs_free_dirent(tmp
, de
, TRUE
);
539 tmpfs_free_node(tmp
, node
);
543 /* Now that all required items are allocated, we can proceed to
544 * insert the new node into the directory, an operation that
546 tmpfs_dir_attach(dvp
, de
);
551 /* --------------------------------------------------------------------- */
554 * Attaches the directory entry de to the directory represented by vp.
555 * Note that this does not change the link count of the node pointed by
556 * the directory entry, as this is done by tmpfs_alloc_dirent.
559 tmpfs_dir_attach(struct vnode
*vp
, struct tmpfs_dirent
*de
)
561 struct tmpfs_node
*dnode
;
563 dnode
= VP_TO_TMPFS_DIR(vp
);
566 TAILQ_INSERT_TAIL(&dnode
->tn_dir
.tn_dirhead
, de
, td_entries
);
569 TMPFS_NODE_LOCK(dnode
);
570 TMPFS_ASSERT_ELOCKED(dnode
);
571 dnode
->tn_size
+= sizeof(struct tmpfs_dirent
);
572 dnode
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_CHANGED
| \
574 TMPFS_NODE_UNLOCK(dnode
);
577 /* --------------------------------------------------------------------- */
580 * Detaches the directory entry de from the directory represented by vp.
581 * Note that this does not change the link count of the node pointed by
582 * the directory entry, as this is done by tmpfs_free_dirent.
585 tmpfs_dir_detach(struct vnode
*vp
, struct tmpfs_dirent
*de
)
587 struct tmpfs_node
*dnode
;
589 dnode
= VP_TO_TMPFS_DIR(vp
);
593 if (dnode
->tn_dir
.tn_readdir_lastp
== de
) {
594 dnode
->tn_dir
.tn_readdir_lastn
= 0;
595 dnode
->tn_dir
.tn_readdir_lastp
= NULL
;
598 TAILQ_REMOVE(&dnode
->tn_dir
.tn_dirhead
, de
, td_entries
);
601 TMPFS_NODE_LOCK(dnode
);
602 TMPFS_ASSERT_ELOCKED(dnode
);
603 dnode
->tn_size
-= sizeof(struct tmpfs_dirent
);
604 dnode
->tn_status
|= TMPFS_NODE_ACCESSED
| TMPFS_NODE_CHANGED
| \
606 TMPFS_NODE_UNLOCK(dnode
);
609 /* --------------------------------------------------------------------- */
612 * Looks for a directory entry in the directory represented by node.
613 * 'ncp' describes the name of the entry to look for. Note that the .
614 * and .. components are not allowed as they do not physically exist
615 * within directories.
617 * Returns a pointer to the entry when found, otherwise NULL.
619 struct tmpfs_dirent
*
620 tmpfs_dir_lookup(struct tmpfs_node
*node
, struct tmpfs_node
*f
,
621 struct namecache
*ncp
)
624 struct tmpfs_dirent
*de
;
625 int len
= ncp
->nc_nlen
;
627 TMPFS_VALIDATE_DIR(node
);
630 TAILQ_FOREACH(de
, &node
->tn_dir
.tn_dirhead
, td_entries
) {
631 if (f
!= NULL
&& de
->td_node
!= f
)
633 if (len
== de
->td_namelen
) {
634 if (!memcmp(ncp
->nc_name
, de
->td_name
, len
)) {
641 TMPFS_NODE_LOCK(node
);
642 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
643 TMPFS_NODE_UNLOCK(node
);
645 return found
? de
: NULL
;
648 /* --------------------------------------------------------------------- */
651 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
652 * directory and returns it in the uio space. The function returns 0
653 * on success, -1 if there was not enough space in the uio structure to
654 * hold the directory entry or an appropriate error code if another
658 tmpfs_dir_getdotdent(struct tmpfs_node
*node
, struct uio
*uio
)
664 TMPFS_VALIDATE_DIR(node
);
665 KKASSERT(uio
->uio_offset
== TMPFS_DIRCOOKIE_DOT
);
667 dent
.d_ino
= node
->tn_id
;
668 dent
.d_type
= DT_DIR
;
670 dent
.d_name
[0] = '.';
671 dent
.d_name
[1] = '\0';
672 dirsize
= _DIRENT_DIRSIZ(&dent
);
674 if (dirsize
> uio
->uio_resid
)
677 error
= uiomove((caddr_t
)&dent
, dirsize
, uio
);
679 uio
->uio_offset
= TMPFS_DIRCOOKIE_DOTDOT
;
682 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
687 /* --------------------------------------------------------------------- */
690 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
691 * directory and returns it in the uio space. The function returns 0
692 * on success, -1 if there was not enough space in the uio structure to
693 * hold the directory entry or an appropriate error code if another
697 tmpfs_dir_getdotdotdent(struct tmpfs_node
*node
, struct uio
*uio
)
703 TMPFS_VALIDATE_DIR(node
);
704 KKASSERT(uio
->uio_offset
== TMPFS_DIRCOOKIE_DOTDOT
);
707 * Return ENOENT if the current node is already removed.
709 if (node
->tn_dir
.tn_parent
== NULL
) {
713 TMPFS_NODE_LOCK(node
->tn_dir
.tn_parent
);
714 dent
.d_ino
= node
->tn_dir
.tn_parent
->tn_id
;
715 TMPFS_NODE_UNLOCK(node
->tn_dir
.tn_parent
);
717 dent
.d_type
= DT_DIR
;
719 dent
.d_name
[0] = '.';
720 dent
.d_name
[1] = '.';
721 dent
.d_name
[2] = '\0';
722 dirsize
= _DIRENT_DIRSIZ(&dent
);
724 if (dirsize
> uio
->uio_resid
)
727 error
= uiomove((caddr_t
)&dent
, dirsize
, uio
);
729 struct tmpfs_dirent
*de
;
731 de
= TAILQ_FIRST(&node
->tn_dir
.tn_dirhead
);
733 uio
->uio_offset
= TMPFS_DIRCOOKIE_EOF
;
735 uio
->uio_offset
= tmpfs_dircookie(de
);
739 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
744 /* --------------------------------------------------------------------- */
747 * Lookup a directory entry by its associated cookie.
749 struct tmpfs_dirent
*
750 tmpfs_dir_lookupbycookie(struct tmpfs_node
*node
, off_t cookie
)
752 struct tmpfs_dirent
*de
;
754 if (cookie
== node
->tn_dir
.tn_readdir_lastn
&&
755 node
->tn_dir
.tn_readdir_lastp
!= NULL
) {
756 return node
->tn_dir
.tn_readdir_lastp
;
759 TAILQ_FOREACH(de
, &node
->tn_dir
.tn_dirhead
, td_entries
) {
760 if (tmpfs_dircookie(de
) == cookie
) {
768 /* --------------------------------------------------------------------- */
771 * Helper function for tmpfs_readdir. Returns as much directory entries
772 * as can fit in the uio space. The read starts at uio->uio_offset.
773 * The function returns 0 on success, -1 if there was not enough space
774 * in the uio structure to hold the directory entry or an appropriate
775 * error code if another error happens.
778 tmpfs_dir_getdents(struct tmpfs_node
*node
, struct uio
*uio
, off_t
*cntp
)
782 struct tmpfs_dirent
*de
;
784 TMPFS_VALIDATE_DIR(node
);
786 /* Locate the first directory entry we have to return. We have cached
787 * the last readdir in the node, so use those values if appropriate.
788 * Otherwise do a linear scan to find the requested entry. */
789 startcookie
= uio
->uio_offset
;
790 KKASSERT(startcookie
!= TMPFS_DIRCOOKIE_DOT
);
791 KKASSERT(startcookie
!= TMPFS_DIRCOOKIE_DOTDOT
);
792 if (startcookie
== TMPFS_DIRCOOKIE_EOF
) {
795 de
= tmpfs_dir_lookupbycookie(node
, startcookie
);
801 /* Read as much entries as possible; i.e., until we reach the end of
802 * the directory or we exhaust uio space. */
807 /* Create a dirent structure representing the current
808 * tmpfs_node and fill it. */
809 d
.d_ino
= de
->td_node
->tn_id
;
810 switch (de
->td_node
->tn_type
) {
840 panic("tmpfs_dir_getdents: type %p %d",
841 de
->td_node
, (int)de
->td_node
->tn_type
);
843 d
.d_namlen
= de
->td_namelen
;
844 KKASSERT(de
->td_namelen
< sizeof(d
.d_name
));
845 bcopy(de
->td_name
, d
.d_name
, d
.d_namlen
);
846 d
.d_name
[d
.d_namlen
] = '\0';
847 reclen
= _DIRENT_RECLEN(d
.d_namlen
);
849 /* Stop reading if the directory entry we are treating is
850 * bigger than the amount of data that can be returned. */
851 if (reclen
> uio
->uio_resid
) {
856 /* Copy the new dirent structure into the output buffer and
857 * advance pointers. */
858 error
= uiomove((caddr_t
)&d
, reclen
, uio
);
861 de
= TAILQ_NEXT(de
, td_entries
);
862 } while (error
== 0 && uio
->uio_resid
> 0 && de
!= NULL
);
864 /* Update the offset and cache. */
866 uio
->uio_offset
= TMPFS_DIRCOOKIE_EOF
;
867 node
->tn_dir
.tn_readdir_lastn
= 0;
868 node
->tn_dir
.tn_readdir_lastp
= NULL
;
870 node
->tn_dir
.tn_readdir_lastn
= uio
->uio_offset
= tmpfs_dircookie(de
);
871 node
->tn_dir
.tn_readdir_lastp
= de
;
873 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
878 /* --------------------------------------------------------------------- */
881 * Resizes the aobj associated to the regular file pointed to by vp to
882 * the size newsize. 'vp' must point to a vnode that represents a regular
883 * file. 'newsize' must be positive.
885 * pass trivial as 1 when buf content will be overwritten, otherwise set 0
888 * Returns zero on success or an appropriate error code on failure.
891 tmpfs_reg_resize(struct vnode
*vp
, off_t newsize
, int trivial
)
894 size_t newpages
, oldpages
;
895 struct tmpfs_mount
*tmp
;
896 struct tmpfs_node
*node
;
898 int biosize
= vp
->v_mount
->mnt_stat
.f_iosize
;
901 KKASSERT(vp
->v_type
== VREG
);
902 KKASSERT(newsize
>= 0);
905 node
= VP_TO_TMPFS_NODE(vp
);
906 tmp
= VFS_TO_TMPFS(vp
->v_mount
);
908 /* Convert the old and new sizes to the number of pages needed to
909 * store them. It may happen that we do not need to do anything
910 * because the last allocated page can accommodate the change on
912 oldsize
= node
->tn_size
;
913 oldpages
= round_page(oldsize
) / PAGE_SIZE
;
914 KKASSERT(oldpages
== node
->tn_reg
.tn_aobj_pages
);
915 newpages
= round_page(newsize
) / PAGE_SIZE
;
917 if (newpages
> oldpages
&&
918 newpages
- oldpages
> TMPFS_PAGES_AVAIL(tmp
)) {
924 tmp
->tm_pages_used
+= (newpages
- oldpages
);
927 TMPFS_NODE_LOCK(node
);
928 node
->tn_reg
.tn_aobj_pages
= newpages
;
929 node
->tn_size
= newsize
;
930 TMPFS_NODE_UNLOCK(node
);
932 if (newsize
< oldsize
)
933 error
= nvtruncbuf(vp
, newsize
, biosize
, -1);
935 error
= nvextendbuf(vp
, oldsize
, newsize
, biosize
, biosize
,
942 /* --------------------------------------------------------------------- */
945 * Change flags of the given vnode.
946 * Caller should execute tmpfs_update on vp after a successful execution.
947 * The vnode must be locked on entry and remain locked on exit.
950 tmpfs_chflags(struct vnode
*vp
, int flags
, struct ucred
*cred
)
953 struct tmpfs_node
*node
;
956 KKASSERT(vn_islocked(vp
));
958 node
= VP_TO_TMPFS_NODE(vp
);
960 /* Disallow this operation if the file system is mounted read-only. */
961 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
964 fmode
= FFLAGS(node
->tn_flags
);
966 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
968 if (fmode
& (FWRITE
| O_TRUNC
)) {
969 if (vp
->v_type
== VDIR
) {
972 error
= vn_writechk(vp
, NULL
);
981 error
= VOP_ACCESS(vp
, mode
, cred
);
986 * Unprivileged processes are not permitted to unset system
987 * flags, or modify flags if any system flags are set.
989 TMPFS_NODE_LOCK(node
);
990 if (!priv_check_cred(cred
, PRIV_VFS_SYSFLAGS
, 0)) {
993 & (SF_NOUNLINK
| SF_IMMUTABLE
| SF_APPEND
)) {
994 error
= securelevel_gt(cred
, 0);
998 /* Snapshot flag cannot be set or cleared */
999 if (((flags
& SF_SNAPSHOT
) != 0 &&
1000 (node
->tn_flags
& SF_SNAPSHOT
) == 0) ||
1001 ((flags
& SF_SNAPSHOT
) == 0 &&
1002 (node
->tn_flags
& SF_SNAPSHOT
) != 0))
1005 node
->tn_flags
= flags
;
1008 & (SF_NOUNLINK
| SF_IMMUTABLE
| SF_APPEND
) ||
1009 (flags
& UF_SETTABLE
) != flags
)
1011 node
->tn_flags
&= SF_SETTABLE
;
1012 node
->tn_flags
|= (flags
& UF_SETTABLE
);
1014 node
->tn_status
|= TMPFS_NODE_CHANGED
;
1015 TMPFS_NODE_UNLOCK(node
);
1017 KKASSERT(vn_islocked(vp
));
1022 /* --------------------------------------------------------------------- */
1025 * Change access mode on the given vnode.
1026 * Caller should execute tmpfs_update on vp after a successful execution.
1027 * The vnode must be locked on entry and remain locked on exit.
1030 tmpfs_chmod(struct vnode
*vp
, mode_t mode
, struct ucred
*cred
)
1033 struct tmpfs_node
*node
;
1036 KKASSERT(vn_islocked(vp
));
1038 node
= VP_TO_TMPFS_NODE(vp
);
1040 /* Disallow this operation if the file system is mounted read-only. */
1041 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1044 /* Immutable or append-only files cannot be modified, either. */
1045 if (node
->tn_flags
& (IMMUTABLE
| APPEND
))
1048 fmode
= FFLAGS(node
->tn_flags
);
1050 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
1052 if (fmode
& (FWRITE
| O_TRUNC
)) {
1053 if (vp
->v_type
== VDIR
) {
1056 error
= vn_writechk(vp
, NULL
);
1065 error
= VOP_ACCESS(vp
, accmode
, cred
);
1071 * Privileged processes may set the sticky bit on non-directories,
1072 * as well as set the setgid bit on a file with a group that the
1073 * process is not a member of.
1075 if (vp
->v_type
!= VDIR
&& (mode
& S_ISTXT
)) {
1076 if (priv_check_cred(cred
, PRIV_VFS_STICKYFILE
, 0))
1079 if (!groupmember(node
->tn_gid
, cred
) && (mode
& S_ISGID
)) {
1080 error
= priv_check_cred(cred
, PRIV_VFS_SETGID
, 0);
1086 TMPFS_NODE_LOCK(node
);
1087 node
->tn_mode
&= ~ALLPERMS
;
1088 node
->tn_mode
|= mode
& ALLPERMS
;
1090 node
->tn_status
|= TMPFS_NODE_CHANGED
;
1091 TMPFS_NODE_UNLOCK(node
);
1093 KKASSERT(vn_islocked(vp
));
1098 /* --------------------------------------------------------------------- */
1101 * Change ownership of the given vnode. At least one of uid or gid must
1102 * be different than VNOVAL. If one is set to that value, the attribute
1104 * Caller should execute tmpfs_update on vp after a successful execution.
1105 * The vnode must be locked on entry and remain locked on exit.
1108 tmpfs_chown(struct vnode
*vp
, uid_t uid
, gid_t gid
, struct ucred
*cred
)
1111 struct tmpfs_node
*node
;
1116 KKASSERT(vn_islocked(vp
));
1118 node
= VP_TO_TMPFS_NODE(vp
);
1120 /* Assign default values if they are unknown. */
1121 KKASSERT(uid
!= VNOVAL
|| gid
!= VNOVAL
);
1126 KKASSERT(uid
!= VNOVAL
&& gid
!= VNOVAL
);
1128 /* Disallow this operation if the file system is mounted read-only. */
1129 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1132 /* Immutable or append-only files cannot be modified, either. */
1133 if (node
->tn_flags
& (IMMUTABLE
| APPEND
))
1136 fmode
= FFLAGS(node
->tn_flags
);
1138 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
1140 if (fmode
& (FWRITE
| O_TRUNC
)) {
1141 if (vp
->v_type
== VDIR
) {
1144 error
= vn_writechk(vp
, NULL
);
1153 error
= VOP_ACCESS(vp
, mode
, cred
);
1159 * To change the owner of a file, or change the group of a file to a
1160 * group of which we are not a member, the caller must have
1163 if ((uid
!= node
->tn_uid
||
1164 (gid
!= node
->tn_gid
&& !groupmember(gid
, cred
))) &&
1165 (error
= priv_check_cred(cred
, PRIV_VFS_CHOWN
, 0)))
1168 ogid
= node
->tn_gid
;
1169 ouid
= node
->tn_uid
;
1171 TMPFS_NODE_LOCK(node
);
1175 node
->tn_status
|= TMPFS_NODE_CHANGED
;
1177 if ((node
->tn_mode
& (S_ISUID
| S_ISGID
)) && (ouid
!= uid
|| ogid
!= gid
)) {
1178 if (priv_check_cred(cred
, PRIV_VFS_RETAINSUGID
, 0))
1179 node
->tn_mode
&= ~(S_ISUID
| S_ISGID
);
1181 TMPFS_NODE_UNLOCK(node
);
1183 KKASSERT(vn_islocked(vp
));
1188 /* --------------------------------------------------------------------- */
1191 * Change size of the given vnode.
1192 * Caller should execute tmpfs_update on vp after a successful execution.
1193 * The vnode must be locked on entry and remain locked on exit.
1196 tmpfs_chsize(struct vnode
*vp
, u_quad_t size
, struct ucred
*cred
)
1199 struct tmpfs_node
*node
;
1201 KKASSERT(vn_islocked(vp
));
1203 node
= VP_TO_TMPFS_NODE(vp
);
1205 /* Decide whether this is a valid operation based on the file type. */
1207 switch (vp
->v_type
) {
1212 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1221 /* Allow modifications of special files even if in the file
1222 * system is mounted read-only (we are not modifying the
1223 * files themselves, but the objects they represent). */
1227 /* Anything else is unsupported. */
1231 /* Immutable or append-only files cannot be modified, either. */
1232 if (node
->tn_flags
& (IMMUTABLE
| APPEND
))
1235 error
= tmpfs_truncate(vp
, size
);
1236 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1237 * for us, as will update tn_status; no need to do that here. */
1239 KKASSERT(vn_islocked(vp
));
1244 /* --------------------------------------------------------------------- */
1247 * Change access and modification times of the given vnode.
1248 * Caller should execute tmpfs_update on vp after a successful execution.
1249 * The vnode must be locked on entry and remain locked on exit.
1252 tmpfs_chtimes(struct vnode
*vp
, struct timespec
*atime
, struct timespec
*mtime
,
1253 int vaflags
, struct ucred
*cred
)
1256 struct tmpfs_node
*node
;
1259 KKASSERT(vn_islocked(vp
));
1261 node
= VP_TO_TMPFS_NODE(vp
);
1263 /* Disallow this operation if the file system is mounted read-only. */
1264 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1267 /* Immutable or append-only files cannot be modified, either. */
1268 if (node
->tn_flags
& (IMMUTABLE
| APPEND
))
1271 /* Determine if the user have proper privilege to update time. */
1272 fmode
= FFLAGS(node
->tn_flags
);
1274 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
1276 if (fmode
& (FWRITE
| O_TRUNC
)) {
1277 if (vp
->v_type
== VDIR
) {
1280 error
= vn_writechk(vp
, NULL
);
1290 if (vaflags
& VA_UTIMES_NULL
) {
1291 error
= VOP_ACCESS(vp
, mode
, cred
);
1293 error
= VOP_ACCESS(vp
, VWRITE
, cred
);
1295 error
= VOP_ACCESS(vp
, mode
, cred
);
1300 TMPFS_NODE_LOCK(node
);
1301 if (atime
->tv_sec
!= VNOVAL
&& atime
->tv_nsec
!= VNOVAL
)
1302 node
->tn_status
|= TMPFS_NODE_ACCESSED
;
1304 if (mtime
->tv_sec
!= VNOVAL
&& mtime
->tv_nsec
!= VNOVAL
)
1305 node
->tn_status
|= TMPFS_NODE_MODIFIED
;
1307 TMPFS_NODE_UNLOCK(node
);
1309 tmpfs_itimes(vp
, atime
, mtime
);
1311 KKASSERT(vn_islocked(vp
));
1316 /* --------------------------------------------------------------------- */
1317 /* Sync timestamps */
1319 tmpfs_itimes(struct vnode
*vp
, const struct timespec
*acc
,
1320 const struct timespec
*mod
)
1322 struct tmpfs_node
*node
;
1323 struct timespec now
;
1325 node
= VP_TO_TMPFS_NODE(vp
);
1327 if ((node
->tn_status
& (TMPFS_NODE_ACCESSED
| TMPFS_NODE_MODIFIED
|
1328 TMPFS_NODE_CHANGED
)) == 0)
1331 vfs_timestamp(&now
);
1333 TMPFS_NODE_LOCK(node
);
1334 if (node
->tn_status
& TMPFS_NODE_ACCESSED
) {
1337 node
->tn_atime
= acc
->tv_sec
;
1338 node
->tn_atimensec
= acc
->tv_nsec
;
1340 if (node
->tn_status
& TMPFS_NODE_MODIFIED
) {
1343 node
->tn_mtime
= mod
->tv_sec
;
1344 node
->tn_mtimensec
= mod
->tv_nsec
;
1346 if (node
->tn_status
& TMPFS_NODE_CHANGED
) {
1347 node
->tn_ctime
= now
.tv_sec
;
1348 node
->tn_ctimensec
= now
.tv_nsec
;
1351 ~(TMPFS_NODE_ACCESSED
| TMPFS_NODE_MODIFIED
| TMPFS_NODE_CHANGED
);
1352 TMPFS_NODE_UNLOCK(node
);
1355 /* --------------------------------------------------------------------- */
1358 tmpfs_update(struct vnode
*vp
)
1361 tmpfs_itimes(vp
, NULL
, NULL
);
1364 /* --------------------------------------------------------------------- */
1367 tmpfs_truncate(struct vnode
*vp
, off_t length
)
1370 struct tmpfs_node
*node
;
1372 node
= VP_TO_TMPFS_NODE(vp
);
1379 if (node
->tn_size
== length
) {
1384 if (length
> VFS_TO_TMPFS(vp
->v_mount
)->tm_maxfilesize
)
1388 error
= tmpfs_reg_resize(vp
, length
, 1);
1391 TMPFS_NODE_LOCK(node
);
1392 node
->tn_status
|= TMPFS_NODE_CHANGED
| TMPFS_NODE_MODIFIED
;
1393 TMPFS_NODE_UNLOCK(node
);
1402 /* --------------------------------------------------------------------- */
1405 tmpfs_fetch_ino(void)
1409 spin_lock_wr(&ino_lock
);
1411 spin_unlock_wr(&ino_lock
);