kernel - TMPFS - Bug fixing pass - vinitvmio(), umount, readdir
[dragonfly.git] / sys / vfs / tmpfs / tmpfs_subr.c
blobe575d990be4cf9452443383f405faae16e97c6ff
1 /* $NetBSD: tmpfs_subr.c,v 1.35 2007/07/09 21:10:50 ad Exp $ */
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
9 * 2005 program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/spinlock2.h>
44 #include <sys/stat.h>
45 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 #include <sys/vmmeter.h>
49 #include <sys/mplock2.h>
51 #include <vm/vm.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
81 * created.
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.
89 int
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;
95 struct timespec ts;
96 udev_t rdev;
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)
106 return (ENOSPC);
108 nnode = (struct tmpfs_node *)objcache_get(tmp->tm_node_pool, M_WAITOK);
110 /* Generic initialization. */
111 nnode->tn_type = type;
112 vfs_timestamp(&ts);
113 nnode->tn_ctime = nnode->tn_mtime = nnode->tn_atime
114 = ts.tv_sec;
115 nnode->tn_ctimensec = nnode->tn_mtimensec = nnode->tn_atimensec
116 = ts.tv_nsec;
117 nnode->tn_uid = uid;
118 nnode->tn_gid = gid;
119 nnode->tn_mode = mode;
120 nnode->tn_id = tmpfs_fetch_ino();
122 /* Type-specific initialization. */
123 switch (nnode->tn_type) {
124 case VBLK:
125 case VCHR:
126 rdev = makeudev(rmajor, rminor);
127 if (rdev == NOUDEV) {
128 return(EINVAL);
130 nnode->tn_rdev = rdev;
131 break;
133 case VDIR:
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;
138 nnode->tn_dir.tn_readdir_lastn = 0;
139 nnode->tn_dir.tn_readdir_lastp = NULL;
140 nnode->tn_links++;
141 nnode->tn_size = 0;
142 if (parent) {
143 TMPFS_NODE_LOCK(parent);
144 parent->tn_links++;
145 TMPFS_NODE_UNLOCK(parent);
147 break;
149 case VFIFO:
150 /* FALLTHROUGH */
151 case VSOCK:
152 break;
154 case VLNK:
155 KKASSERT((strlen(target) +1) < MAXPATHLEN);
156 nnode->tn_size = strlen(target);
157 nnode->tn_link = kmalloc(nnode->tn_size + 1, M_TMPFSNAME,
158 M_WAITOK);
159 bcopy(target, nnode->tn_link, nnode->tn_size);
160 nnode->tn_link[nnode->tn_size] = '\0';
161 break;
163 case VREG:
164 nnode->tn_reg.tn_aobj =
165 vm_pager_allocate(OBJT_SWAP, NULL, 0, VM_PROT_DEFAULT, 0);
166 nnode->tn_reg.tn_aobj_pages = 0;
167 nnode->tn_size = 0;
168 break;
170 default:
171 panic("tmpfs_alloc_node: type %p %d", nnode, (int)nnode->tn_type);
174 TMPFS_NODE_LOCK(nnode);
175 TMPFS_LOCK(tmp);
176 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
177 tmp->tm_nodes_inuse++;
178 TMPFS_UNLOCK(tmp);
179 TMPFS_NODE_UNLOCK(nnode);
181 *node = nnode;
182 return 0;
185 /* --------------------------------------------------------------------- */
188 * Destroys the node pointed to by node from the file system 'tmp'.
189 * If the node does not belong to the given mount point, the results are
190 * unpredicted.
192 * If the node references a directory; no entries are allowed because
193 * their removal could need a recursive algorithm, something forbidden in
194 * kernel space. Furthermore, there is not need to provide such
195 * functionality (recursive removal) because the only primitives offered
196 * to the user are the removal of empty directories and the deletion of
197 * individual files.
199 * Note that nodes are not really deleted; in fact, when a node has been
200 * allocated, it cannot be deleted during the whole life of the file
201 * system. Instead, they are moved to the available list and remain there
202 * until reused.
204 void
205 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
207 size_t pages = 0;
209 #ifdef INVARIANTS
210 TMPFS_ASSERT_ELOCKED(node);
211 KKASSERT(node->tn_vnode == NULL);
212 KKASSERT((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0);
213 #endif
215 TMPFS_LOCK(tmp);
216 LIST_REMOVE(node, tn_entries);
217 tmp->tm_nodes_inuse--;
218 TMPFS_UNLOCK(tmp);
219 TMPFS_NODE_UNLOCK(node);
221 switch (node->tn_type) {
222 case VNON:
223 /* Do not do anything. VNON is provided to let the
224 * allocation routine clean itself easily by avoiding
225 * duplicating code in it. */
226 /* FALLTHROUGH */
227 case VBLK:
228 /* FALLTHROUGH */
229 case VCHR:
230 /* FALLTHROUGH */
231 break;
232 case VDIR:
234 * The parent link can be NULL if this is the root
235 * node.
237 node->tn_links--;
238 node->tn_size = 0;
239 KKASSERT(node->tn_dir.tn_parent || node == tmp->tm_root);
240 if (node->tn_dir.tn_parent) {
241 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
242 node->tn_dir.tn_parent->tn_links--;
245 * If the parent directory has no more links and
246 * no vnode ref nothing is going to come along
247 * and clean it up unless we do it here.
249 if (node->tn_dir.tn_parent->tn_links == 0 &&
250 node->tn_dir.tn_parent->tn_vnode == NULL) {
251 tmpfs_free_node(tmp, node->tn_dir.tn_parent);
252 /* eats parent lock */
253 } else {
254 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
256 node->tn_dir.tn_parent = NULL;
260 * If the root node is being destroyed don't leave a
261 * dangling pointer in tmpfs_mount.
263 if (node == tmp->tm_root)
264 tmp->tm_root = NULL;
265 break;
266 case VFIFO:
267 /* FALLTHROUGH */
268 case VSOCK:
269 break;
271 case VLNK:
272 kfree(node->tn_link, M_TMPFSNAME);
273 node->tn_link = NULL;
274 break;
276 case VREG:
277 if (node->tn_reg.tn_aobj != NULL)
278 vm_pager_deallocate(node->tn_reg.tn_aobj);
279 node->tn_reg.tn_aobj = NULL;
280 pages = node->tn_reg.tn_aobj_pages;
281 break;
283 default:
284 panic("tmpfs_free_node: type %p %d", node, (int)node->tn_type);
288 * Clean up fields for the next allocation. The objcache only ctors
289 * new allocations.
291 tmpfs_node_ctor(node, NULL, 0);
292 objcache_put(tmp->tm_node_pool, node);
293 /* node is now invalid */
295 TMPFS_LOCK(tmp);
296 tmp->tm_pages_used -= pages;
297 TMPFS_UNLOCK(tmp);
300 /* --------------------------------------------------------------------- */
303 * Allocates a new directory entry for the node node with a name of name.
304 * The new directory entry is returned in *de.
306 * The link count of node is increased by one to reflect the new object
307 * referencing it.
309 * Returns zero on success or an appropriate error code on failure.
312 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
313 const char *name, uint16_t len, struct tmpfs_dirent **de)
315 struct tmpfs_dirent *nde;
318 nde = (struct tmpfs_dirent *)objcache_get(tmp->tm_dirent_pool, M_WAITOK);
319 nde->td_name = kmalloc(len + 1, M_TMPFSNAME, M_WAITOK);
320 nde->td_namelen = len;
321 bcopy(name, nde->td_name, len);
322 nde->td_name[len] = '\0';
324 nde->td_node = node;
326 TMPFS_NODE_LOCK(node);
327 node->tn_links++;
328 TMPFS_NODE_UNLOCK(node);
330 *de = nde;
332 return 0;
335 /* --------------------------------------------------------------------- */
338 * Frees a directory entry. It is the caller's responsibility to destroy
339 * the node referenced by it if needed.
341 * The link count of node is decreased by one to reflect the removal of an
342 * object that referenced it. This only happens if 'node_exists' is true;
343 * otherwise the function will not access the node referred to by the
344 * directory entry, as it may already have been released from the outside.
346 void
347 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de)
349 struct tmpfs_node *node;
351 node = de->td_node;
353 TMPFS_NODE_LOCK(node);
354 TMPFS_ASSERT_ELOCKED(node);
355 KKASSERT(node->tn_links > 0);
356 node->tn_links--;
357 TMPFS_NODE_UNLOCK(node);
359 kfree(de->td_name, M_TMPFSNAME);
360 de->td_namelen = 0;
361 de->td_name = NULL;
362 de->td_node = NULL;
363 objcache_put(tmp->tm_dirent_pool, de);
366 /* --------------------------------------------------------------------- */
369 * Allocates a new vnode for the node node or returns a new reference to
370 * an existing one if the node had already a vnode referencing it. The
371 * resulting locked vnode is returned in *vpp.
373 * Returns zero on success or an appropriate error code on failure.
376 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, int lkflag,
377 struct vnode **vpp)
379 int error = 0;
380 struct vnode *vp;
382 loop:
383 TMPFS_NODE_LOCK(node);
384 if ((vp = node->tn_vnode) != NULL) {
385 KKASSERT((node->tn_vpstate & TMPFS_VNODE_DOOMED) == 0);
386 TMPFS_NODE_UNLOCK(node);
387 (void) vget(vp, lkflag | LK_EXCLUSIVE | LK_RETRY);
390 * Make sure the vnode is still there after
391 * getting the interlock to avoid racing a free.
393 if (node->tn_vnode == NULL || node->tn_vnode != vp) {
394 vput(vp);
395 goto loop;
398 goto out;
402 * This should never happen.
404 if (node->tn_vpstate & TMPFS_VNODE_DOOMED) {
405 TMPFS_NODE_UNLOCK(node);
406 error = ENOENT;
407 vp = NULL;
408 goto out;
412 * otherwise lock the vp list while we call getnewvnode
413 * since that can block.
415 if (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) {
416 node->tn_vpstate |= TMPFS_VNODE_WANT;
417 error = tsleep((caddr_t) &node->tn_vpstate,
418 PINTERLOCKED | PCATCH, "tmpfs_alloc_vp", 0);
419 TMPFS_NODE_UNLOCK(node);
420 if (error)
421 return error;
423 goto loop;
424 } else
425 node->tn_vpstate |= TMPFS_VNODE_ALLOCATING;
427 TMPFS_NODE_UNLOCK(node);
429 /* Get a new vnode and associate it with our node. */
430 error = getnewvnode(VT_TMPFS, mp, &vp, VLKTIMEOUT, LK_CANRECURSE);
431 if (error != 0)
432 goto unlock;
433 KKASSERT(vp != NULL);
435 vp->v_data = node;
436 vp->v_type = node->tn_type;
438 /* Type-specific initialization. */
439 switch (node->tn_type) {
440 case VBLK:
441 /* FALLTHROUGH */
442 case VCHR:
443 /* FALLTHROUGH */
444 case VSOCK:
445 break;
446 case VREG:
447 vinitvmio(vp, (node->tn_size + BMASK) & ~(off_t)BMASK);
448 break;
449 case VLNK:
450 break;
451 case VFIFO:
452 vp->v_ops = &mp->mnt_vn_fifo_ops;
453 break;
454 case VDIR:
455 break;
457 default:
458 panic("tmpfs_alloc_vp: type %p %d", node, (int)node->tn_type);
461 insmntque(vp, mp);
463 unlock:
464 TMPFS_NODE_LOCK(node);
466 KKASSERT(node->tn_vpstate & TMPFS_VNODE_ALLOCATING);
467 node->tn_vpstate &= ~TMPFS_VNODE_ALLOCATING;
468 node->tn_vnode = vp;
470 if (node->tn_vpstate & TMPFS_VNODE_WANT) {
471 node->tn_vpstate &= ~TMPFS_VNODE_WANT;
472 TMPFS_NODE_UNLOCK(node);
473 wakeup((caddr_t) &node->tn_vpstate);
474 } else
475 TMPFS_NODE_UNLOCK(node);
477 out:
478 *vpp = vp;
480 KKASSERT(IFF(error == 0, *vpp != NULL && vn_islocked(*vpp)));
481 #ifdef INVARIANTS
482 TMPFS_NODE_LOCK(node);
483 KKASSERT(*vpp == node->tn_vnode);
484 TMPFS_NODE_UNLOCK(node);
485 #endif
487 return error;
490 /* --------------------------------------------------------------------- */
493 * Destroys the association between the vnode vp and the node it
494 * references.
496 void
497 tmpfs_free_vp(struct vnode *vp)
499 struct tmpfs_node *node;
501 node = VP_TO_TMPFS_NODE(vp);
503 TMPFS_NODE_LOCK(node);
504 KKASSERT(lockcount(TMPFS_NODE_MTX(node)) > 0);
505 node->tn_vnode = NULL;
506 TMPFS_NODE_UNLOCK(node);
507 vp->v_data = NULL;
510 /* --------------------------------------------------------------------- */
513 * Allocates a new file of type 'type' and adds it to the parent directory
514 * 'dvp'; this addition is done using the component name given in 'cnp'.
515 * The ownership of the new file is automatically assigned based on the
516 * credentials of the caller (through 'cnp'), the group is set based on
517 * the parent directory and the mode is determined from the 'vap' argument.
518 * If successful, *vpp holds a vnode to the newly created file and zero
519 * is returned. Otherwise *vpp is NULL and the function returns an
520 * appropriate error code.
523 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
524 struct namecache *ncp, struct ucred *cred, char *target)
526 int error;
527 struct tmpfs_dirent *de;
528 struct tmpfs_mount *tmp;
529 struct tmpfs_node *dnode;
530 struct tmpfs_node *node;
531 struct tmpfs_node *parent;
533 tmp = VFS_TO_TMPFS(dvp->v_mount);
534 dnode = VP_TO_TMPFS_DIR(dvp);
535 *vpp = NULL;
537 /* If the entry we are creating is a directory, we cannot overflow
538 * the number of links of its parent, because it will get a new
539 * link. */
540 if (vap->va_type == VDIR) {
541 /* Ensure that we do not overflow the maximum number of links
542 * imposed by the system. */
543 KKASSERT(dnode->tn_links <= LINK_MAX);
544 if (dnode->tn_links == LINK_MAX) {
545 return EMLINK;
548 parent = dnode;
549 KKASSERT(parent != NULL);
550 } else
551 parent = NULL;
553 /* Allocate a node that represents the new file. */
554 error = tmpfs_alloc_node(tmp, vap->va_type, cred->cr_uid,
555 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rmajor, vap->va_rminor, &node);
556 if (error != 0)
557 return error;
558 TMPFS_NODE_LOCK(node);
560 /* Allocate a directory entry that points to the new file. */
561 error = tmpfs_alloc_dirent(tmp, node, ncp->nc_name, ncp->nc_nlen, &de);
562 if (error != 0) {
563 tmpfs_free_node(tmp, node);
564 /* eats node lock */
565 return error;
568 /* Allocate a vnode for the new file. */
569 error = tmpfs_alloc_vp(dvp->v_mount, node, LK_EXCLUSIVE, vpp);
570 if (error != 0) {
571 tmpfs_free_dirent(tmp, de);
572 tmpfs_free_node(tmp, node);
573 /* eats node lock */
574 return error;
577 /* Now that all required items are allocated, we can proceed to
578 * insert the new node into the directory, an operation that
579 * cannot fail. */
580 tmpfs_dir_attach(dnode, de);
581 TMPFS_NODE_UNLOCK(node);
583 return error;
586 /* --------------------------------------------------------------------- */
589 * Attaches the directory entry de to the directory represented by vp.
590 * Note that this does not change the link count of the node pointed by
591 * the directory entry, as this is done by tmpfs_alloc_dirent.
593 void
594 tmpfs_dir_attach(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
596 TMPFS_NODE_LOCK(dnode);
597 TAILQ_INSERT_TAIL(&dnode->tn_dir.tn_dirhead, de, td_entries);
599 TMPFS_ASSERT_ELOCKED(dnode);
600 dnode->tn_size += sizeof(struct tmpfs_dirent);
601 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
602 TMPFS_NODE_MODIFIED;
603 TMPFS_NODE_UNLOCK(dnode);
606 /* --------------------------------------------------------------------- */
609 * Detaches the directory entry de from the directory represented by vp.
610 * Note that this does not change the link count of the node pointed by
611 * the directory entry, as this is done by tmpfs_free_dirent.
613 void
614 tmpfs_dir_detach(struct tmpfs_node *dnode, struct tmpfs_dirent *de)
616 TMPFS_NODE_LOCK(dnode);
617 if (dnode->tn_dir.tn_readdir_lastp == de) {
618 dnode->tn_dir.tn_readdir_lastn = 0;
619 dnode->tn_dir.tn_readdir_lastp = NULL;
621 TAILQ_REMOVE(&dnode->tn_dir.tn_dirhead, de, td_entries);
623 TMPFS_ASSERT_ELOCKED(dnode);
624 dnode->tn_size -= sizeof(struct tmpfs_dirent);
625 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
626 TMPFS_NODE_MODIFIED;
627 TMPFS_NODE_UNLOCK(dnode);
630 /* --------------------------------------------------------------------- */
633 * Looks for a directory entry in the directory represented by node.
634 * 'ncp' describes the name of the entry to look for. Note that the .
635 * and .. components are not allowed as they do not physically exist
636 * within directories.
638 * Returns a pointer to the entry when found, otherwise NULL.
640 struct tmpfs_dirent *
641 tmpfs_dir_lookup(struct tmpfs_node *node, struct tmpfs_node *f,
642 struct namecache *ncp)
644 struct tmpfs_dirent *de;
645 int len = ncp->nc_nlen;
647 TMPFS_VALIDATE_DIR(node);
649 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
650 if (f != NULL && de->td_node != f)
651 continue;
652 if (len == de->td_namelen) {
653 if (!memcmp(ncp->nc_name, de->td_name, len))
654 break;
658 TMPFS_NODE_LOCK(node);
659 node->tn_status |= TMPFS_NODE_ACCESSED;
660 TMPFS_NODE_UNLOCK(node);
662 return de;
665 /* --------------------------------------------------------------------- */
668 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
669 * directory and returns it in the uio space. The function returns 0
670 * on success, -1 if there was not enough space in the uio structure to
671 * hold the directory entry or an appropriate error code if another
672 * error happens.
675 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
677 int error;
678 struct dirent dent;
679 int dirsize;
681 TMPFS_VALIDATE_DIR(node);
682 KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
684 dent.d_ino = node->tn_id;
685 dent.d_type = DT_DIR;
686 dent.d_namlen = 1;
687 dent.d_name[0] = '.';
688 dent.d_name[1] = '\0';
689 dirsize = _DIRENT_DIRSIZ(&dent);
691 if (dirsize > uio->uio_resid)
692 error = -1;
693 else {
694 error = uiomove((caddr_t)&dent, dirsize, uio);
695 if (error == 0)
696 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
699 node->tn_status |= TMPFS_NODE_ACCESSED;
701 return error;
704 /* --------------------------------------------------------------------- */
707 * Helper function for tmpfs_readdir. Creates a '..' entry for the given
708 * directory and returns it in the uio space. The function returns 0
709 * on success, -1 if there was not enough space in the uio structure to
710 * hold the directory entry or an appropriate error code if another
711 * error happens.
714 tmpfs_dir_getdotdotdent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
715 struct uio *uio)
717 int error;
718 struct dirent dent;
719 int dirsize;
721 TMPFS_VALIDATE_DIR(node);
722 KKASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
724 if (node->tn_dir.tn_parent) {
725 TMPFS_NODE_LOCK(node->tn_dir.tn_parent);
726 dent.d_ino = node->tn_dir.tn_parent->tn_id;
727 TMPFS_NODE_UNLOCK(node->tn_dir.tn_parent);
728 } else {
729 dent.d_ino = tmp->tm_root->tn_id;
732 dent.d_type = DT_DIR;
733 dent.d_namlen = 2;
734 dent.d_name[0] = '.';
735 dent.d_name[1] = '.';
736 dent.d_name[2] = '\0';
737 dirsize = _DIRENT_DIRSIZ(&dent);
739 if (dirsize > uio->uio_resid)
740 error = -1;
741 else {
742 error = uiomove((caddr_t)&dent, dirsize, uio);
743 if (error == 0) {
744 struct tmpfs_dirent *de;
746 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
747 if (de == NULL)
748 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
749 else
750 uio->uio_offset = tmpfs_dircookie(de);
754 node->tn_status |= TMPFS_NODE_ACCESSED;
756 return error;
759 /* --------------------------------------------------------------------- */
762 * Lookup a directory entry by its associated cookie.
764 struct tmpfs_dirent *
765 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
767 struct tmpfs_dirent *de;
769 if (cookie == node->tn_dir.tn_readdir_lastn &&
770 node->tn_dir.tn_readdir_lastp != NULL) {
771 return node->tn_dir.tn_readdir_lastp;
774 TAILQ_FOREACH(de, &node->tn_dir.tn_dirhead, td_entries) {
775 if (tmpfs_dircookie(de) == cookie) {
776 break;
780 return de;
783 /* --------------------------------------------------------------------- */
786 * Helper function for tmpfs_readdir. Returns as much directory entries
787 * as can fit in the uio space. The read starts at uio->uio_offset.
788 * The function returns 0 on success, -1 if there was not enough space
789 * in the uio structure to hold the directory entry or an appropriate
790 * error code if another error happens.
793 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
795 int error;
796 off_t startcookie;
797 struct tmpfs_dirent *de;
799 TMPFS_VALIDATE_DIR(node);
801 /* Locate the first directory entry we have to return. We have cached
802 * the last readdir in the node, so use those values if appropriate.
803 * Otherwise do a linear scan to find the requested entry. */
804 startcookie = uio->uio_offset;
805 KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
806 KKASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
807 if (startcookie == TMPFS_DIRCOOKIE_EOF) {
808 return 0;
809 } else {
810 de = tmpfs_dir_lookupbycookie(node, startcookie);
812 if (de == NULL) {
813 return EINVAL;
816 /* Read as much entries as possible; i.e., until we reach the end of
817 * the directory or we exhaust uio space. */
818 do {
819 struct dirent d;
820 int reclen;
822 /* Create a dirent structure representing the current
823 * tmpfs_node and fill it. */
824 d.d_ino = de->td_node->tn_id;
825 switch (de->td_node->tn_type) {
826 case VBLK:
827 d.d_type = DT_BLK;
828 break;
830 case VCHR:
831 d.d_type = DT_CHR;
832 break;
834 case VDIR:
835 d.d_type = DT_DIR;
836 break;
838 case VFIFO:
839 d.d_type = DT_FIFO;
840 break;
842 case VLNK:
843 d.d_type = DT_LNK;
844 break;
846 case VREG:
847 d.d_type = DT_REG;
848 break;
850 case VSOCK:
851 d.d_type = DT_SOCK;
852 break;
854 default:
855 panic("tmpfs_dir_getdents: type %p %d",
856 de->td_node, (int)de->td_node->tn_type);
858 d.d_namlen = de->td_namelen;
859 KKASSERT(de->td_namelen < sizeof(d.d_name));
860 bcopy(de->td_name, d.d_name, d.d_namlen);
861 d.d_name[d.d_namlen] = '\0';
862 reclen = _DIRENT_RECLEN(d.d_namlen);
864 /* Stop reading if the directory entry we are treating is
865 * bigger than the amount of data that can be returned. */
866 if (reclen > uio->uio_resid) {
867 error = -1;
868 break;
871 /* Copy the new dirent structure into the output buffer and
872 * advance pointers. */
873 error = uiomove((caddr_t)&d, reclen, uio);
875 (*cntp)++;
876 de = TAILQ_NEXT(de, td_entries);
877 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
879 /* Update the offset and cache. */
880 if (de == NULL) {
881 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
882 node->tn_dir.tn_readdir_lastn = 0;
883 node->tn_dir.tn_readdir_lastp = NULL;
884 } else {
885 node->tn_dir.tn_readdir_lastn = uio->uio_offset = tmpfs_dircookie(de);
886 node->tn_dir.tn_readdir_lastp = de;
888 node->tn_status |= TMPFS_NODE_ACCESSED;
890 return error;
893 /* --------------------------------------------------------------------- */
896 * Resizes the aobj associated to the regular file pointed to by vp to
897 * the size newsize. 'vp' must point to a vnode that represents a regular
898 * file. 'newsize' must be positive.
900 * pass trivial as 1 when buf content will be overwritten, otherwise set 0
901 * to be zero filled.
903 * Returns zero on success or an appropriate error code on failure.
906 tmpfs_reg_resize(struct vnode *vp, off_t newsize, int trivial)
908 int error;
909 size_t newpages, oldpages;
910 struct tmpfs_mount *tmp;
911 struct tmpfs_node *node;
912 off_t oldsize;
914 #ifdef INVARIANTS
915 KKASSERT(vp->v_type == VREG);
916 KKASSERT(newsize >= 0);
917 #endif
919 node = VP_TO_TMPFS_NODE(vp);
920 tmp = VFS_TO_TMPFS(vp->v_mount);
922 /* Convert the old and new sizes to the number of pages needed to
923 * store them. It may happen that we do not need to do anything
924 * because the last allocated page can accommodate the change on
925 * its own. */
926 oldsize = node->tn_size;
927 oldpages = round_page(oldsize) / PAGE_SIZE;
928 KKASSERT(oldpages == node->tn_reg.tn_aobj_pages);
929 newpages = round_page(newsize) / PAGE_SIZE;
931 if (newpages > oldpages &&
932 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) {
933 error = ENOSPC;
934 goto out;
937 TMPFS_LOCK(tmp);
938 tmp->tm_pages_used += (newpages - oldpages);
939 TMPFS_UNLOCK(tmp);
941 TMPFS_NODE_LOCK(node);
942 node->tn_reg.tn_aobj_pages = newpages;
943 node->tn_size = newsize;
944 TMPFS_NODE_UNLOCK(node);
947 * When adjusting the vnode filesize and its VM object we must
948 * also adjust our backing VM object (aobj). The blocksize
949 * used must match the block sized we use for the buffer cache.
951 * The backing VM object contains no VM pages, only swap
952 * assignments.
954 if (newsize < oldsize) {
955 vm_pindex_t osize;
956 vm_pindex_t nsize;
957 vm_object_t aobj;
959 error = nvtruncbuf(vp, newsize, BSIZE, -1);
960 aobj = node->tn_reg.tn_aobj;
961 if (aobj) {
962 osize = aobj->size;
963 nsize = vp->v_object->size;
964 if (nsize < osize) {
965 aobj->size = osize;
966 swap_pager_freespace(aobj, nsize,
967 osize - nsize);
970 } else {
971 vm_object_t aobj;
973 error = nvextendbuf(vp, oldsize, newsize, BSIZE, BSIZE,
974 -1, -1, trivial);
975 aobj = node->tn_reg.tn_aobj;
976 if (aobj)
977 aobj->size = vp->v_object->size;
980 out:
981 return error;
984 /* --------------------------------------------------------------------- */
987 * Change flags of the given vnode.
988 * Caller should execute tmpfs_update on vp after a successful execution.
989 * The vnode must be locked on entry and remain locked on exit.
992 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred)
994 int error;
995 struct tmpfs_node *node;
996 int fmode, mode;
998 KKASSERT(vn_islocked(vp));
1000 node = VP_TO_TMPFS_NODE(vp);
1002 /* Disallow this operation if the file system is mounted read-only. */
1003 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1004 return EROFS;
1006 fmode = FFLAGS(node->tn_flags);
1007 mode = 0;
1008 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
1009 return EINVAL;
1010 if (fmode & (FWRITE | O_TRUNC)) {
1011 if (vp->v_type == VDIR) {
1012 return EISDIR;
1014 error = vn_writechk(vp, NULL);
1015 if (error)
1016 return (error);
1018 mode |= VWRITE;
1020 if (fmode & FREAD)
1021 mode |= VREAD;
1022 if (mode) {
1023 error = VOP_ACCESS(vp, mode, cred);
1024 if (error)
1025 return (error);
1028 * Unprivileged processes are not permitted to unset system
1029 * flags, or modify flags if any system flags are set.
1031 TMPFS_NODE_LOCK(node);
1032 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
1033 #if 0
1034 if (node->tn_flags
1035 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
1036 error = securelevel_gt(cred, 0);
1037 if (error)
1038 return (error);
1040 /* Snapshot flag cannot be set or cleared */
1041 if (((flags & SF_SNAPSHOT) != 0 &&
1042 (node->tn_flags & SF_SNAPSHOT) == 0) ||
1043 ((flags & SF_SNAPSHOT) == 0 &&
1044 (node->tn_flags & SF_SNAPSHOT) != 0))
1045 return (EPERM);
1046 #endif
1047 node->tn_flags = flags;
1048 } else {
1049 if (node->tn_flags
1050 & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
1051 (flags & UF_SETTABLE) != flags)
1052 return (EPERM);
1053 node->tn_flags &= SF_SETTABLE;
1054 node->tn_flags |= (flags & UF_SETTABLE);
1056 node->tn_status |= TMPFS_NODE_CHANGED;
1057 TMPFS_NODE_UNLOCK(node);
1059 KKASSERT(vn_islocked(vp));
1061 return 0;
1064 /* --------------------------------------------------------------------- */
1067 * Change access mode on the given vnode.
1068 * Caller should execute tmpfs_update on vp after a successful execution.
1069 * The vnode must be locked on entry and remain locked on exit.
1072 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred)
1074 int error;
1075 struct tmpfs_node *node;
1076 int fmode, accmode;
1078 KKASSERT(vn_islocked(vp));
1080 node = VP_TO_TMPFS_NODE(vp);
1082 /* Disallow this operation if the file system is mounted read-only. */
1083 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1084 return EROFS;
1086 /* Immutable or append-only files cannot be modified, either. */
1087 if (node->tn_flags & (IMMUTABLE | APPEND))
1088 return EPERM;
1090 fmode = FFLAGS(node->tn_flags);
1091 accmode = 0;
1092 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
1093 return EINVAL;
1094 if (fmode & (FWRITE | O_TRUNC)) {
1095 if (vp->v_type == VDIR) {
1096 return EISDIR;
1098 error = vn_writechk(vp, NULL);
1099 if (error)
1100 return (error);
1102 accmode |= VWRITE;
1104 if (fmode & FREAD)
1105 accmode |= VREAD;
1106 if (accmode) {
1107 error = VOP_ACCESS(vp, accmode, cred);
1108 if (error)
1109 return (error);
1113 * Privileged processes may set the sticky bit on non-directories,
1114 * as well as set the setgid bit on a file with a group that the
1115 * process is not a member of.
1117 if (vp->v_type != VDIR && (mode & S_ISTXT)) {
1118 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
1119 return (EFTYPE);
1121 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID)) {
1122 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
1123 if (error)
1124 return (error);
1128 TMPFS_NODE_LOCK(node);
1129 node->tn_mode &= ~ALLPERMS;
1130 node->tn_mode |= mode & ALLPERMS;
1132 node->tn_status |= TMPFS_NODE_CHANGED;
1133 TMPFS_NODE_UNLOCK(node);
1135 KKASSERT(vn_islocked(vp));
1137 return 0;
1140 /* --------------------------------------------------------------------- */
1143 * Change ownership of the given vnode. At least one of uid or gid must
1144 * be different than VNOVAL. If one is set to that value, the attribute
1145 * is unchanged.
1146 * Caller should execute tmpfs_update on vp after a successful execution.
1147 * The vnode must be locked on entry and remain locked on exit.
1150 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred)
1152 int error;
1153 struct tmpfs_node *node;
1154 uid_t ouid;
1155 gid_t ogid;
1156 int fmode, mode;
1158 KKASSERT(vn_islocked(vp));
1160 node = VP_TO_TMPFS_NODE(vp);
1162 /* Assign default values if they are unknown. */
1163 KKASSERT(uid != VNOVAL || gid != VNOVAL);
1164 if (uid == VNOVAL)
1165 uid = node->tn_uid;
1166 if (gid == VNOVAL)
1167 gid = node->tn_gid;
1168 KKASSERT(uid != VNOVAL && gid != VNOVAL);
1170 /* Disallow this operation if the file system is mounted read-only. */
1171 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1172 return EROFS;
1174 /* Immutable or append-only files cannot be modified, either. */
1175 if (node->tn_flags & (IMMUTABLE | APPEND))
1176 return EPERM;
1178 fmode = FFLAGS(node->tn_flags);
1179 mode = 0;
1180 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
1181 return EINVAL;
1182 if (fmode & (FWRITE | O_TRUNC)) {
1183 if (vp->v_type == VDIR) {
1184 return EISDIR;
1186 error = vn_writechk(vp, NULL);
1187 if (error)
1188 return (error);
1190 mode |= VWRITE;
1192 if (fmode & FREAD)
1193 mode |= VREAD;
1194 if (mode) {
1195 error = VOP_ACCESS(vp, mode, cred);
1196 if (error)
1197 return (error);
1201 * To change the owner of a file, or change the group of a file to a
1202 * group of which we are not a member, the caller must have
1203 * privilege.
1205 if ((uid != node->tn_uid ||
1206 (gid != node->tn_gid && !groupmember(gid, cred))) &&
1207 (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
1208 return (error);
1210 ogid = node->tn_gid;
1211 ouid = node->tn_uid;
1213 TMPFS_NODE_LOCK(node);
1214 node->tn_uid = uid;
1215 node->tn_gid = gid;
1217 node->tn_status |= TMPFS_NODE_CHANGED;
1219 if ((node->tn_mode & (S_ISUID | S_ISGID)) && (ouid != uid || ogid != gid)) {
1220 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
1221 node->tn_mode &= ~(S_ISUID | S_ISGID);
1223 TMPFS_NODE_UNLOCK(node);
1225 KKASSERT(vn_islocked(vp));
1227 return 0;
1230 /* --------------------------------------------------------------------- */
1233 * Change size of the given vnode.
1234 * Caller should execute tmpfs_update on vp after a successful execution.
1235 * The vnode must be locked on entry and remain locked on exit.
1238 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred)
1240 int error;
1241 struct tmpfs_node *node;
1243 KKASSERT(vn_islocked(vp));
1245 node = VP_TO_TMPFS_NODE(vp);
1247 /* Decide whether this is a valid operation based on the file type. */
1248 error = 0;
1249 switch (vp->v_type) {
1250 case VDIR:
1251 return EISDIR;
1253 case VREG:
1254 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1255 return EROFS;
1256 break;
1258 case VBLK:
1259 /* FALLTHROUGH */
1260 case VCHR:
1261 /* FALLTHROUGH */
1262 case VFIFO:
1263 /* Allow modifications of special files even if in the file
1264 * system is mounted read-only (we are not modifying the
1265 * files themselves, but the objects they represent). */
1266 return 0;
1268 default:
1269 /* Anything else is unsupported. */
1270 return EOPNOTSUPP;
1273 /* Immutable or append-only files cannot be modified, either. */
1274 if (node->tn_flags & (IMMUTABLE | APPEND))
1275 return EPERM;
1277 error = tmpfs_truncate(vp, size);
1278 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1279 * for us, as will update tn_status; no need to do that here. */
1281 KKASSERT(vn_islocked(vp));
1283 return error;
1286 /* --------------------------------------------------------------------- */
1289 * Change access and modification times of the given vnode.
1290 * Caller should execute tmpfs_update on vp after a successful execution.
1291 * The vnode must be locked on entry and remain locked on exit.
1294 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1295 int vaflags, struct ucred *cred)
1297 int error;
1298 struct tmpfs_node *node;
1299 int fmode, mode;
1301 KKASSERT(vn_islocked(vp));
1303 node = VP_TO_TMPFS_NODE(vp);
1305 /* Disallow this operation if the file system is mounted read-only. */
1306 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1307 return EROFS;
1309 /* Immutable or append-only files cannot be modified, either. */
1310 if (node->tn_flags & (IMMUTABLE | APPEND))
1311 return EPERM;
1313 /* Determine if the user have proper privilege to update time. */
1314 fmode = FFLAGS(node->tn_flags);
1315 mode = 0;
1316 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
1317 return EINVAL;
1318 if (fmode & (FWRITE | O_TRUNC)) {
1319 if (vp->v_type == VDIR) {
1320 return EISDIR;
1322 error = vn_writechk(vp, NULL);
1323 if (error)
1324 return (error);
1326 mode |= VWRITE;
1328 if (fmode & FREAD)
1329 mode |= VREAD;
1331 if (mode) {
1332 if (vaflags & VA_UTIMES_NULL) {
1333 error = VOP_ACCESS(vp, mode, cred);
1334 if (error)
1335 error = VOP_ACCESS(vp, VWRITE, cred);
1336 } else
1337 error = VOP_ACCESS(vp, mode, cred);
1338 if (error)
1339 return (error);
1342 TMPFS_NODE_LOCK(node);
1343 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1344 node->tn_status |= TMPFS_NODE_ACCESSED;
1346 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1347 node->tn_status |= TMPFS_NODE_MODIFIED;
1349 TMPFS_NODE_UNLOCK(node);
1351 tmpfs_itimes(vp, atime, mtime);
1353 KKASSERT(vn_islocked(vp));
1355 return 0;
1358 /* --------------------------------------------------------------------- */
1359 /* Sync timestamps */
1360 void
1361 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1362 const struct timespec *mod)
1364 struct tmpfs_node *node;
1365 struct timespec now;
1367 node = VP_TO_TMPFS_NODE(vp);
1369 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1370 TMPFS_NODE_CHANGED)) == 0)
1371 return;
1373 vfs_timestamp(&now);
1375 TMPFS_NODE_LOCK(node);
1376 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1377 if (acc == NULL)
1378 acc = &now;
1379 node->tn_atime = acc->tv_sec;
1380 node->tn_atimensec = acc->tv_nsec;
1382 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1383 if (mod == NULL)
1384 mod = &now;
1385 node->tn_mtime = mod->tv_sec;
1386 node->tn_mtimensec = mod->tv_nsec;
1388 if (node->tn_status & TMPFS_NODE_CHANGED) {
1389 node->tn_ctime = now.tv_sec;
1390 node->tn_ctimensec = now.tv_nsec;
1392 node->tn_status &=
1393 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1394 TMPFS_NODE_UNLOCK(node);
1397 /* --------------------------------------------------------------------- */
1399 void
1400 tmpfs_update(struct vnode *vp)
1403 tmpfs_itimes(vp, NULL, NULL);
1406 /* --------------------------------------------------------------------- */
1409 tmpfs_truncate(struct vnode *vp, off_t length)
1411 int error;
1412 struct tmpfs_node *node;
1414 node = VP_TO_TMPFS_NODE(vp);
1416 if (length < 0) {
1417 error = EINVAL;
1418 goto out;
1421 if (node->tn_size == length) {
1422 error = 0;
1423 goto out;
1426 if (length > VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
1427 return (EFBIG);
1430 error = tmpfs_reg_resize(vp, length, 1);
1432 if (error == 0) {
1433 TMPFS_NODE_LOCK(node);
1434 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1435 TMPFS_NODE_UNLOCK(node);
1438 out:
1439 tmpfs_update(vp);
1441 return error;
1444 /* --------------------------------------------------------------------- */
1446 static ino_t
1447 tmpfs_fetch_ino(void)
1449 ino_t ret;
1451 spin_lock_wr(&ino_lock);
1452 ret = t_ino++;
1453 spin_unlock_wr(&ino_lock);
1455 return ret;