tmpfs_subr.c revision 1.56.4.5 1 /* $NetBSD: tmpfs_subr.c,v 1.56.4.5 2011/05/19 03:43:02 rmind Exp $ */
2
3 /*
4 * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
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.
10 *
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.
19 *
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.
31 */
32
33 /*
34 * Efficient memory file system supporting functions.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.56.4.5 2011/05/19 03:43:02 rmind Exp $");
39
40 #include <sys/param.h>
41 #include <sys/dirent.h>
42 #include <sys/event.h>
43 #include <sys/kmem.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #include <sys/systm.h>
49 #include <sys/swap.h>
50 #include <sys/vnode.h>
51 #include <sys/kauth.h>
52 #include <sys/proc.h>
53 #include <sys/atomic.h>
54
55 #include <uvm/uvm.h>
56
57 #include <miscfs/specfs/specdev.h>
58 #include <miscfs/genfs/genfs.h>
59 #include <fs/tmpfs/tmpfs.h>
60 #include <fs/tmpfs/tmpfs_fifoops.h>
61 #include <fs/tmpfs/tmpfs_specops.h>
62 #include <fs/tmpfs/tmpfs_vnops.h>
63
64 /* --------------------------------------------------------------------- */
65
66 /*
67 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
68 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
69 * using the credentials of the process 'p'.
70 *
71 * If the node type is set to 'VDIR', then the parent parameter must point
72 * to the parent directory of the node being created. It may only be NULL
73 * while allocating the root node.
74 *
75 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
76 * specifies the device the node represents.
77 *
78 * If the node type is set to 'VLNK', then the parameter target specifies
79 * the file name of the target file for the symbolic link that is being
80 * created.
81 *
82 * Note that new nodes are retrieved from the available list if it has
83 * items or, if it is empty, from the node pool as long as there is enough
84 * space to create them.
85 *
86 * Returns zero on success or an appropriate error code on failure.
87 */
88 int
89 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
90 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
91 char *target, dev_t rdev, struct tmpfs_node **node)
92 {
93 struct tmpfs_node *nnode;
94
95 /* If the root directory of the 'tmp' file system is not yet
96 * allocated, this must be the request to do it. */
97 KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
98
99 KASSERT(IFF(type == VLNK, target != NULL));
100 KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
101
102 KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
103
104 nnode = NULL;
105 if (atomic_inc_uint_nv(&tmp->tm_nodes_cnt) >= tmp->tm_nodes_max) {
106 atomic_dec_uint(&tmp->tm_nodes_cnt);
107 return ENOSPC;
108 }
109
110 nnode = tmpfs_node_get(tmp);
111 if (nnode == NULL) {
112 atomic_dec_uint(&tmp->tm_nodes_cnt);
113 return ENOSPC;
114 }
115
116 /*
117 * XXX Where the pool is backed by a map larger than (4GB *
118 * sizeof(*nnode)), this may produce duplicate inode numbers
119 * for applications that do not understand 64-bit ino_t.
120 */
121 nnode->tn_id = (ino_t)((uintptr_t)nnode / sizeof(*nnode));
122 nnode->tn_gen = arc4random();
123
124 /* Generic initialization. */
125 nnode->tn_type = type;
126 nnode->tn_size = 0;
127 nnode->tn_status = 0;
128 nnode->tn_flags = 0;
129 nnode->tn_links = 0;
130
131 vfs_timestamp(&nnode->tn_atime);
132 nnode->tn_birthtime = nnode->tn_atime;
133 nnode->tn_ctime = nnode->tn_atime;
134 nnode->tn_mtime = nnode->tn_atime;
135
136 nnode->tn_uid = uid;
137 nnode->tn_gid = gid;
138 nnode->tn_mode = mode;
139 nnode->tn_lockf = NULL;
140 nnode->tn_vnode = NULL;
141
142 /* Type-specific initialization. */
143 switch (nnode->tn_type) {
144 case VBLK:
145 case VCHR:
146 nnode->tn_spec.tn_dev.tn_rdev = rdev;
147 break;
148
149 case VDIR:
150 TAILQ_INIT(&nnode->tn_spec.tn_dir.tn_dir);
151 nnode->tn_spec.tn_dir.tn_parent =
152 (parent == NULL) ? nnode : parent;
153 nnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
154 nnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
155 nnode->tn_links++;
156 break;
157
158 case VFIFO:
159 /* FALLTHROUGH */
160 case VSOCK:
161 break;
162
163 case VLNK:
164 KASSERT(strlen(target) < MAXPATHLEN);
165 nnode->tn_size = strlen(target);
166 if (nnode->tn_size == 0) {
167 nnode->tn_spec.tn_lnk.tn_link = NULL;
168 break;
169 }
170 nnode->tn_spec.tn_lnk.tn_link =
171 tmpfs_strname_alloc(tmp, nnode->tn_size);
172 if (nnode->tn_spec.tn_lnk.tn_link == NULL) {
173 atomic_dec_uint(&tmp->tm_nodes_cnt);
174 tmpfs_node_put(tmp, nnode);
175 return ENOSPC;
176 }
177 memcpy(nnode->tn_spec.tn_lnk.tn_link, target, nnode->tn_size);
178 break;
179
180 case VREG:
181 nnode->tn_spec.tn_reg.tn_aobj =
182 uao_create(INT32_MAX - PAGE_SIZE, 0);
183 nnode->tn_spec.tn_reg.tn_aobj_pages = 0;
184 break;
185
186 default:
187 KASSERT(0);
188 }
189
190 mutex_init(&nnode->tn_vlock, MUTEX_DEFAULT, IPL_NONE);
191
192 mutex_enter(&tmp->tm_lock);
193 LIST_INSERT_HEAD(&tmp->tm_nodes, nnode, tn_entries);
194 mutex_exit(&tmp->tm_lock);
195
196 *node = nnode;
197 return 0;
198 }
199
200 /* --------------------------------------------------------------------- */
201
202 /*
203 * Destroys the node pointed to by node from the file system 'tmp'.
204 * If the node does not belong to the given mount point, the results are
205 * unpredicted.
206 *
207 * If the node references a directory; no entries are allowed because
208 * their removal could need a recursive algorithm, something forbidden in
209 * kernel space. Furthermore, there is not need to provide such
210 * functionality (recursive removal) because the only primitives offered
211 * to the user are the removal of empty directories and the deletion of
212 * individual files.
213 *
214 * Note that nodes are not really deleted; in fact, when a node has been
215 * allocated, it cannot be deleted during the whole life of the file
216 * system. Instead, they are moved to the available list and remain there
217 * until reused.
218 */
219 void
220 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
221 {
222 size_t objsz;
223
224 mutex_enter(&tmp->tm_lock);
225 LIST_REMOVE(node, tn_entries);
226 mutex_exit(&tmp->tm_lock);
227 atomic_dec_uint(&tmp->tm_nodes_cnt);
228
229 switch (node->tn_type) {
230 case VLNK:
231 if (node->tn_size > 0)
232 tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link,
233 node->tn_size);
234 break;
235 case VREG:
236 /*
237 * Calculate the size of node data, decrease the used-memory
238 * counter, and destroy the memory object (if any).
239 */
240 objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages;
241 if (objsz != 0) {
242 tmpfs_mem_decr(tmp, objsz);
243 }
244 if (node->tn_spec.tn_reg.tn_aobj != NULL) {
245 uao_detach(node->tn_spec.tn_reg.tn_aobj);
246 }
247 break;
248 default:
249 break;
250 }
251
252 mutex_destroy(&node->tn_vlock);
253 tmpfs_node_put(tmp, node);
254 }
255
256 /* --------------------------------------------------------------------- */
257
258 /*
259 * Allocates a new directory entry for the node node with a name of name.
260 * The new directory entry is returned in *de.
261 *
262 * The link count of node is increased by one to reflect the new object
263 * referencing it. This takes care of notifying kqueue listeners about
264 * this change.
265 *
266 * Returns zero on success or an appropriate error code on failure.
267 */
268 int
269 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
270 const char *name, uint16_t len, struct tmpfs_dirent **de)
271 {
272 struct tmpfs_dirent *nde;
273
274 nde = tmpfs_dirent_get(tmp);
275 if (nde == NULL)
276 return ENOSPC;
277
278 nde->td_name = tmpfs_strname_alloc(tmp, len);
279 if (nde->td_name == NULL) {
280 tmpfs_dirent_put(tmp, nde);
281 return ENOSPC;
282 }
283 nde->td_namelen = len;
284 memcpy(nde->td_name, name, len);
285 nde->td_node = node;
286
287 if (node != TMPFS_NODE_WHITEOUT) {
288 node->tn_links++;
289 if (node->tn_links > 1 && node->tn_vnode != NULL)
290 VN_KNOTE(node->tn_vnode, NOTE_LINK);
291 }
292 *de = nde;
293
294 return 0;
295 }
296
297 /* --------------------------------------------------------------------- */
298
299 /*
300 * Frees a directory entry. It is the caller's responsibility to destroy
301 * the node referenced by it if needed.
302 *
303 * The link count of node is decreased by one to reflect the removal of an
304 * object that referenced it. This only happens if 'node_exists' is true;
305 * otherwise the function will not access the node referred to by the
306 * directory entry, as it may already have been released from the outside.
307 *
308 * Interested parties (kqueue) are notified of the link count change; note
309 * that this can include both the node pointed to by the directory entry
310 * as well as its parent.
311 */
312 void
313 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
314 bool node_exists)
315 {
316 if (node_exists && de->td_node != TMPFS_NODE_WHITEOUT) {
317 struct tmpfs_node *node;
318
319 node = de->td_node;
320
321 KASSERT(node->tn_links > 0);
322 node->tn_links--;
323 if (node->tn_vnode != NULL)
324 VN_KNOTE(node->tn_vnode, node->tn_links == 0 ?
325 NOTE_DELETE : NOTE_LINK);
326 if (node->tn_type == VDIR)
327 VN_KNOTE(node->tn_spec.tn_dir.tn_parent->tn_vnode,
328 NOTE_LINK);
329 }
330
331 tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
332 tmpfs_dirent_put(tmp, de);
333 }
334
335 /* --------------------------------------------------------------------- */
336
337 /*
338 * Allocates a new vnode for the node node or returns a new reference to
339 * an existing one if the node had already a vnode referencing it. The
340 * resulting locked vnode is returned in *vpp.
341 *
342 * Returns zero on success or an appropriate error code on failure.
343 */
344 int
345 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp)
346 {
347 int error;
348 struct vnode *vp;
349
350 /* If there is already a vnode, then lock it. */
351 for (;;) {
352 mutex_enter(&node->tn_vlock);
353 if ((vp = node->tn_vnode) != NULL) {
354 mutex_enter(vp->v_interlock);
355 mutex_exit(&node->tn_vlock);
356 error = vget(vp, LK_EXCLUSIVE);
357 if (error == ENOENT) {
358 /* vnode was reclaimed. */
359 continue;
360 }
361 *vpp = vp;
362 return error;
363 }
364 break;
365 }
366
367 /* Get a new vnode and associate it with our node. */
368 error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, NULL, &vp);
369 if (error != 0) {
370 mutex_exit(&node->tn_vlock);
371 return error;
372 }
373
374 /* Set UVM object to use vnode_t::v_interlock (share it). */
375 uvm_obj_setlock(node->tn_spec.tn_reg.tn_aobj, vp->v_interlock);
376
377 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
378 if (error != 0) {
379 mutex_exit(&node->tn_vlock);
380 ungetnewvnode(vp);
381 return error;
382 }
383
384 vp->v_type = node->tn_type;
385
386 /* Type-specific initialization. */
387 switch (node->tn_type) {
388 case VBLK:
389 /* FALLTHROUGH */
390 case VCHR:
391 vp->v_op = tmpfs_specop_p;
392 spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
393 break;
394
395 case VDIR:
396 vp->v_vflag |= node->tn_spec.tn_dir.tn_parent == node ?
397 VV_ROOT : 0;
398 break;
399
400 case VFIFO:
401 vp->v_op = tmpfs_fifoop_p;
402 break;
403
404 case VLNK:
405 /* FALLTHROUGH */
406 case VREG:
407 /* FALLTHROUGH */
408 case VSOCK:
409 break;
410
411 default:
412 KASSERT(0);
413 }
414
415 uvm_vnp_setsize(vp, node->tn_size);
416 vp->v_data = node;
417 node->tn_vnode = vp;
418 mutex_exit(&node->tn_vlock);
419 *vpp = vp;
420
421 KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
422 KASSERT(*vpp == node->tn_vnode);
423
424 return error;
425 }
426
427 /* --------------------------------------------------------------------- */
428
429 /*
430 * Destroys the association between the vnode vp and the node it
431 * references.
432 */
433 void
434 tmpfs_free_vp(struct vnode *vp)
435 {
436 struct tmpfs_node *node;
437
438 node = VP_TO_TMPFS_NODE(vp);
439
440 mutex_enter(&node->tn_vlock);
441 node->tn_vnode = NULL;
442 mutex_exit(&node->tn_vlock);
443 vp->v_data = NULL;
444 }
445
446 /* --------------------------------------------------------------------- */
447
448 /*
449 * Allocates a new file of type 'type' and adds it to the parent directory
450 * 'dvp'; this addition is done using the component name given in 'cnp'.
451 * The ownership of the new file is automatically assigned based on the
452 * credentials of the caller (through 'cnp'), the group is set based on
453 * the parent directory and the mode is determined from the 'vap' argument.
454 * If successful, *vpp holds a vnode to the newly created file and zero
455 * is returned. Otherwise *vpp is NULL and the function returns an
456 * appropriate error code.
457 */
458 int
459 tmpfs_alloc_file(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
460 struct componentname *cnp, char *target)
461 {
462 int error;
463 struct tmpfs_dirent *de;
464 struct tmpfs_mount *tmp;
465 struct tmpfs_node *dnode;
466 struct tmpfs_node *node;
467 struct tmpfs_node *parent;
468
469 KASSERT(VOP_ISLOCKED(dvp));
470
471 tmp = VFS_TO_TMPFS(dvp->v_mount);
472 dnode = VP_TO_TMPFS_DIR(dvp);
473 *vpp = NULL;
474
475 /* If the entry we are creating is a directory, we cannot overflow
476 * the number of links of its parent, because it will get a new
477 * link. */
478 if (vap->va_type == VDIR) {
479 /* Ensure that we do not overflow the maximum number of links
480 * imposed by the system. */
481 KASSERT(dnode->tn_links <= LINK_MAX);
482 if (dnode->tn_links == LINK_MAX) {
483 error = EMLINK;
484 goto out;
485 }
486
487 parent = dnode;
488 } else
489 parent = NULL;
490
491 /* Allocate a node that represents the new file. */
492 error = tmpfs_alloc_node(tmp, vap->va_type, kauth_cred_geteuid(cnp->cn_cred),
493 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev, &node);
494 if (error != 0)
495 goto out;
496
497 /* Allocate a directory entry that points to the new file. */
498 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
499 &de);
500 if (error != 0) {
501 tmpfs_free_node(tmp, node);
502 goto out;
503 }
504
505 /* Allocate a vnode for the new file. */
506 error = tmpfs_alloc_vp(dvp->v_mount, node, vpp);
507 if (error != 0) {
508 tmpfs_free_dirent(tmp, de, true);
509 tmpfs_free_node(tmp, node);
510 goto out;
511 }
512
513 /* Now that all required items are allocated, we can proceed to
514 * insert the new node into the directory, an operation that
515 * cannot fail. */
516 tmpfs_dir_attach(dvp, de);
517 if (vap->va_type == VDIR) {
518 VN_KNOTE(dvp, NOTE_LINK);
519 dnode->tn_links++;
520 KASSERT(dnode->tn_links <= LINK_MAX);
521 }
522
523 out:
524 vput(dvp);
525
526 KASSERT(IFF(error == 0, *vpp != NULL));
527
528 return error;
529 }
530
531 /* --------------------------------------------------------------------- */
532
533 /*
534 * Attaches the directory entry de to the directory represented by vp.
535 * Note that this does not change the link count of the node pointed by
536 * the directory entry, as this is done by tmpfs_alloc_dirent.
537 *
538 * As the "parent" directory changes, interested parties are notified of
539 * a write to it.
540 */
541 void
542 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
543 {
544 struct tmpfs_node *dnode;
545
546 KASSERT(VOP_ISLOCKED(vp));
547 dnode = VP_TO_TMPFS_DIR(vp);
548
549 TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
550 dnode->tn_size += sizeof(struct tmpfs_dirent);
551 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
552 TMPFS_NODE_MODIFIED;
553 uvm_vnp_setsize(vp, dnode->tn_size);
554
555 VN_KNOTE(vp, NOTE_WRITE);
556 }
557
558 /* --------------------------------------------------------------------- */
559
560 /*
561 * Detaches the directory entry de from the directory represented by vp.
562 * Note that this does not change the link count of the node pointed by
563 * the directory entry, as this is done by tmpfs_free_dirent.
564 *
565 * As the "parent" directory changes, interested parties are notified of
566 * a write to it.
567 */
568 void
569 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
570 {
571 struct tmpfs_node *dnode;
572
573 KASSERT(VOP_ISLOCKED(vp));
574 dnode = VP_TO_TMPFS_DIR(vp);
575
576 if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
577 dnode->tn_spec.tn_dir.tn_readdir_lastn = 0;
578 dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
579 }
580
581 TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
582 dnode->tn_size -= sizeof(struct tmpfs_dirent);
583 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
584 TMPFS_NODE_MODIFIED;
585 uvm_vnp_setsize(vp, dnode->tn_size);
586
587 VN_KNOTE(vp, NOTE_WRITE);
588 }
589
590 /* --------------------------------------------------------------------- */
591
592 /*
593 * Looks for a directory entry in the directory represented by node.
594 * 'cnp' describes the name of the entry to look for. Note that the .
595 * and .. components are not allowed as they do not physically exist
596 * within directories.
597 *
598 * Returns a pointer to the entry when found, otherwise NULL.
599 */
600 struct tmpfs_dirent *
601 tmpfs_dir_lookup(struct tmpfs_node *node, struct componentname *cnp)
602 {
603 struct tmpfs_dirent *de;
604
605 KASSERT(VOP_ISLOCKED(node->tn_vnode));
606 KASSERT(IMPLIES(cnp->cn_namelen == 1, cnp->cn_nameptr[0] != '.'));
607 KASSERT(IMPLIES(cnp->cn_namelen == 2, !(cnp->cn_nameptr[0] == '.' &&
608 cnp->cn_nameptr[1] == '.')));
609 TMPFS_VALIDATE_DIR(node);
610
611 node->tn_status |= TMPFS_NODE_ACCESSED;
612
613 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
614 KASSERT(cnp->cn_namelen < 0xffff);
615 if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
616 memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
617 break;
618 }
619 }
620
621 return de;
622 }
623
624 /* --------------------------------------------------------------------- */
625
626 /*
627 * Helper function for tmpfs_readdir. Creates a '.' entry for the given
628 * directory and returns it in the uio space. The function returns 0
629 * on success, -1 if there was not enough space in the uio structure to
630 * hold the directory entry or an appropriate error code if another
631 * error happens.
632 */
633 int
634 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
635 {
636 int error;
637 struct dirent *dentp;
638
639 TMPFS_VALIDATE_DIR(node);
640 KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
641
642 dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
643
644 dentp->d_fileno = node->tn_id;
645 dentp->d_type = DT_DIR;
646 dentp->d_namlen = 1;
647 dentp->d_name[0] = '.';
648 dentp->d_name[1] = '\0';
649 dentp->d_reclen = _DIRENT_SIZE(dentp);
650
651 if (dentp->d_reclen > uio->uio_resid)
652 error = -1;
653 else {
654 error = uiomove(dentp, dentp->d_reclen, uio);
655 if (error == 0)
656 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
657 }
658
659 node->tn_status |= TMPFS_NODE_ACCESSED;
660
661 kmem_free(dentp, sizeof(struct dirent));
662 return error;
663 }
664
665 /* --------------------------------------------------------------------- */
666
667 /*
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.
673 */
674 int
675 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
676 {
677 int error;
678 struct dirent *dentp;
679
680 TMPFS_VALIDATE_DIR(node);
681 KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
682
683 dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
684
685 dentp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
686 dentp->d_type = DT_DIR;
687 dentp->d_namlen = 2;
688 dentp->d_name[0] = '.';
689 dentp->d_name[1] = '.';
690 dentp->d_name[2] = '\0';
691 dentp->d_reclen = _DIRENT_SIZE(dentp);
692
693 if (dentp->d_reclen > uio->uio_resid)
694 error = -1;
695 else {
696 error = uiomove(dentp, dentp->d_reclen, uio);
697 if (error == 0) {
698 struct tmpfs_dirent *de;
699
700 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
701 if (de == NULL)
702 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
703 else
704 uio->uio_offset = tmpfs_dircookie(de);
705 }
706 }
707
708 node->tn_status |= TMPFS_NODE_ACCESSED;
709
710 kmem_free(dentp, sizeof(struct dirent));
711 return error;
712 }
713
714 /* --------------------------------------------------------------------- */
715
716 /*
717 * Lookup a directory entry by its associated cookie.
718 */
719 struct tmpfs_dirent *
720 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
721 {
722 struct tmpfs_dirent *de;
723
724 KASSERT(VOP_ISLOCKED(node->tn_vnode));
725
726 if (cookie == node->tn_spec.tn_dir.tn_readdir_lastn &&
727 node->tn_spec.tn_dir.tn_readdir_lastp != NULL) {
728 return node->tn_spec.tn_dir.tn_readdir_lastp;
729 }
730
731 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
732 if (tmpfs_dircookie(de) == cookie) {
733 break;
734 }
735 }
736
737 return de;
738 }
739
740 /* --------------------------------------------------------------------- */
741
742 /*
743 * Helper function for tmpfs_readdir. Returns as much directory entries
744 * as can fit in the uio space. The read starts at uio->uio_offset.
745 * The function returns 0 on success, -1 if there was not enough space
746 * in the uio structure to hold the directory entry or an appropriate
747 * error code if another error happens.
748 */
749 int
750 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
751 {
752 int error;
753 off_t startcookie;
754 struct dirent *dentp;
755 struct tmpfs_dirent *de;
756
757 KASSERT(VOP_ISLOCKED(node->tn_vnode));
758 TMPFS_VALIDATE_DIR(node);
759
760 /* Locate the first directory entry we have to return. We have cached
761 * the last readdir in the node, so use those values if appropriate.
762 * Otherwise do a linear scan to find the requested entry. */
763 startcookie = uio->uio_offset;
764 KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
765 KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
766 if (startcookie == TMPFS_DIRCOOKIE_EOF) {
767 return 0;
768 } else {
769 de = tmpfs_dir_lookupbycookie(node, startcookie);
770 }
771 if (de == NULL) {
772 return EINVAL;
773 }
774
775 dentp = kmem_alloc(sizeof(struct dirent), KM_SLEEP);
776
777 /* Read as much entries as possible; i.e., until we reach the end of
778 * the directory or we exhaust uio space. */
779 do {
780 /* Create a dirent structure representing the current
781 * tmpfs_node and fill it. */
782 if (de->td_node == TMPFS_NODE_WHITEOUT) {
783 dentp->d_fileno = 1;
784 dentp->d_type = DT_WHT;
785 } else {
786 dentp->d_fileno = de->td_node->tn_id;
787 switch (de->td_node->tn_type) {
788 case VBLK:
789 dentp->d_type = DT_BLK;
790 break;
791
792 case VCHR:
793 dentp->d_type = DT_CHR;
794 break;
795
796 case VDIR:
797 dentp->d_type = DT_DIR;
798 break;
799
800 case VFIFO:
801 dentp->d_type = DT_FIFO;
802 break;
803
804 case VLNK:
805 dentp->d_type = DT_LNK;
806 break;
807
808 case VREG:
809 dentp->d_type = DT_REG;
810 break;
811
812 case VSOCK:
813 dentp->d_type = DT_SOCK;
814 break;
815
816 default:
817 KASSERT(0);
818 }
819 }
820 dentp->d_namlen = de->td_namelen;
821 KASSERT(de->td_namelen < sizeof(dentp->d_name));
822 (void)memcpy(dentp->d_name, de->td_name, de->td_namelen);
823 dentp->d_name[de->td_namelen] = '\0';
824 dentp->d_reclen = _DIRENT_SIZE(dentp);
825
826 /* Stop reading if the directory entry we are treating is
827 * bigger than the amount of data that can be returned. */
828 if (dentp->d_reclen > uio->uio_resid) {
829 error = -1;
830 break;
831 }
832
833 /* Copy the new dirent structure into the output buffer and
834 * advance pointers. */
835 error = uiomove(dentp, dentp->d_reclen, uio);
836
837 (*cntp)++;
838 de = TAILQ_NEXT(de, td_entries);
839 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
840
841 /* Update the offset and cache. */
842 if (de == NULL) {
843 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
844 node->tn_spec.tn_dir.tn_readdir_lastn = 0;
845 node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
846 } else {
847 node->tn_spec.tn_dir.tn_readdir_lastn = uio->uio_offset =
848 tmpfs_dircookie(de);
849 node->tn_spec.tn_dir.tn_readdir_lastp = de;
850 }
851
852 node->tn_status |= TMPFS_NODE_ACCESSED;
853
854 kmem_free(dentp, sizeof(struct dirent));
855 return error;
856 }
857
858 /* --------------------------------------------------------------------- */
859
860 /*
861 * Resizes the aobj associated to the regular file pointed to by vp to
862 * the size newsize. 'vp' must point to a vnode that represents a regular
863 * file. 'newsize' must be positive.
864 *
865 * If the file is extended, the appropriate kevent is raised. This does
866 * not rise a write event though because resizing is not the same as
867 * writing.
868 *
869 * Returns zero on success or an appropriate error code on failure.
870 */
871 int
872 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
873 {
874 size_t newpages, oldpages;
875 struct tmpfs_mount *tmp;
876 struct tmpfs_node *node;
877 off_t oldsize;
878
879 KASSERT(vp->v_type == VREG);
880 KASSERT(newsize >= 0);
881
882 node = VP_TO_TMPFS_NODE(vp);
883 tmp = VFS_TO_TMPFS(vp->v_mount);
884
885 oldsize = node->tn_size;
886 oldpages = round_page(oldsize) >> PAGE_SHIFT;
887 newpages = round_page(newsize) >> PAGE_SHIFT;
888 KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
889
890 if (newpages > oldpages) {
891 /* Increase the used-memory counter if getting extra pages. */
892 if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
893 return ENOSPC;
894 }
895 } else if (newsize < oldsize) {
896 int zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
897
898 /* Zero out the truncated part of the last page. */
899 uvm_vnp_zerorange(vp, newsize, zerolen);
900 }
901
902 node->tn_spec.tn_reg.tn_aobj_pages = newpages;
903 node->tn_size = newsize;
904 uvm_vnp_setsize(vp, newsize);
905
906 /*
907 * Free "backing store".
908 */
909 if (newpages < oldpages) {
910 struct uvm_object *uobj;
911
912 uobj = node->tn_spec.tn_reg.tn_aobj;
913 KASSERT(uobj->vmobjlock == vp->v_interlock);
914
915 mutex_enter(uobj->vmobjlock);
916 uao_dropswap_range(uobj, newpages, oldpages);
917 mutex_exit(uobj->vmobjlock);
918
919 /* Decrease the used-memory counter. */
920 tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
921 }
922
923 if (newsize > oldsize)
924 VN_KNOTE(vp, NOTE_EXTEND);
925
926 return 0;
927 }
928
929 /*
930 * Change flags of the given vnode.
931 * Caller should execute tmpfs_update on vp after a successful execution.
932 * The vnode must be locked on entry and remain locked on exit.
933 */
934 int
935 tmpfs_chflags(struct vnode *vp, int flags, kauth_cred_t cred, struct lwp *l)
936 {
937 int error;
938 struct tmpfs_node *node;
939 kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
940 int fs_decision = 0;
941
942 KASSERT(VOP_ISLOCKED(vp));
943
944 node = VP_TO_TMPFS_NODE(vp);
945
946 /* Disallow this operation if the file system is mounted read-only. */
947 if (vp->v_mount->mnt_flag & MNT_RDONLY)
948 return EROFS;
949
950 if (kauth_cred_geteuid(cred) != node->tn_uid)
951 fs_decision = EACCES;
952
953 /*
954 * If the new flags have non-user flags that are different than
955 * those on the node, we need special permission to change them.
956 */
957 if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
958 action |= KAUTH_VNODE_WRITE_SYSFLAGS;
959 if (!fs_decision)
960 fs_decision = EPERM;
961 }
962
963 /*
964 * Indicate that this node's flags have system attributes in them if
965 * that's the case.
966 */
967 if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
968 action |= KAUTH_VNODE_HAS_SYSFLAGS;
969 }
970
971 error = kauth_authorize_vnode(cred, action, vp, NULL, fs_decision);
972 if (error)
973 return error;
974
975 /*
976 * Set the flags. If we're not setting non-user flags, be careful not
977 * to overwrite them.
978 *
979 * XXX: Can't we always assign here? if the system flags are different,
980 * the code above should catch attempts to change them without
981 * proper permissions, and if we're here it means it's okay to
982 * change them...
983 */
984 if (action & KAUTH_VNODE_WRITE_SYSFLAGS) {
985 node->tn_flags = flags;
986 } else {
987 /* Clear all user-settable flags and re-set them. */
988 node->tn_flags &= SF_SETTABLE;
989 node->tn_flags |= (flags & UF_SETTABLE);
990 }
991
992 node->tn_status |= TMPFS_NODE_CHANGED;
993 VN_KNOTE(vp, NOTE_ATTRIB);
994
995 KASSERT(VOP_ISLOCKED(vp));
996
997 return 0;
998 }
999
1000 /* --------------------------------------------------------------------- */
1001
1002 /*
1003 * Change access mode on the given vnode.
1004 * Caller should execute tmpfs_update on vp after a successful execution.
1005 * The vnode must be locked on entry and remain locked on exit.
1006 */
1007 int
1008 tmpfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred, struct lwp *l)
1009 {
1010 int error;
1011 struct tmpfs_node *node;
1012
1013 KASSERT(VOP_ISLOCKED(vp));
1014
1015 node = VP_TO_TMPFS_NODE(vp);
1016
1017 /* Disallow this operation if the file system is mounted read-only. */
1018 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1019 return EROFS;
1020
1021 /* Immutable or append-only files cannot be modified, either. */
1022 if (node->tn_flags & (IMMUTABLE | APPEND))
1023 return EPERM;
1024
1025 error = genfs_can_chmod(vp, cred, node->tn_uid, node->tn_gid,
1026 mode);
1027
1028 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
1029 NULL, error);
1030 if (error)
1031 return (error);
1032
1033 node->tn_mode = (mode & ALLPERMS);
1034
1035 node->tn_status |= TMPFS_NODE_CHANGED;
1036 VN_KNOTE(vp, NOTE_ATTRIB);
1037
1038 KASSERT(VOP_ISLOCKED(vp));
1039
1040 return 0;
1041 }
1042
1043 /* --------------------------------------------------------------------- */
1044
1045 /*
1046 * Change ownership of the given vnode. At least one of uid or gid must
1047 * be different than VNOVAL. If one is set to that value, the attribute
1048 * is unchanged.
1049 * Caller should execute tmpfs_update on vp after a successful execution.
1050 * The vnode must be locked on entry and remain locked on exit.
1051 */
1052 int
1053 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred,
1054 struct lwp *l)
1055 {
1056 int error;
1057 struct tmpfs_node *node;
1058
1059 KASSERT(VOP_ISLOCKED(vp));
1060
1061 node = VP_TO_TMPFS_NODE(vp);
1062
1063 /* Assign default values if they are unknown. */
1064 KASSERT(uid != VNOVAL || gid != VNOVAL);
1065 if (uid == VNOVAL)
1066 uid = node->tn_uid;
1067 if (gid == VNOVAL)
1068 gid = node->tn_gid;
1069 KASSERT(uid != VNOVAL && gid != VNOVAL);
1070
1071 /* Disallow this operation if the file system is mounted read-only. */
1072 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1073 return EROFS;
1074
1075 /* Immutable or append-only files cannot be modified, either. */
1076 if (node->tn_flags & (IMMUTABLE | APPEND))
1077 return EPERM;
1078
1079 error = genfs_can_chown(vp, cred, node->tn_uid, node->tn_gid, uid,
1080 gid);
1081
1082 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
1083 NULL, error);
1084 if (error)
1085 return (error);
1086
1087 node->tn_uid = uid;
1088 node->tn_gid = gid;
1089
1090 node->tn_status |= TMPFS_NODE_CHANGED;
1091 VN_KNOTE(vp, NOTE_ATTRIB);
1092
1093 KASSERT(VOP_ISLOCKED(vp));
1094
1095 return 0;
1096 }
1097
1098 /* --------------------------------------------------------------------- */
1099
1100 /*
1101 * Change size of the given vnode.
1102 * Caller should execute tmpfs_update on vp after a successful execution.
1103 * The vnode must be locked on entry and remain locked on exit.
1104 */
1105 int
1106 tmpfs_chsize(struct vnode *vp, u_quad_t size, kauth_cred_t cred,
1107 struct lwp *l)
1108 {
1109 int error;
1110 struct tmpfs_node *node;
1111
1112 KASSERT(VOP_ISLOCKED(vp));
1113
1114 node = VP_TO_TMPFS_NODE(vp);
1115
1116 /* Decide whether this is a valid operation based on the file type. */
1117 error = 0;
1118 switch (vp->v_type) {
1119 case VDIR:
1120 return EISDIR;
1121
1122 case VREG:
1123 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1124 return EROFS;
1125 break;
1126
1127 case VBLK:
1128 /* FALLTHROUGH */
1129 case VCHR:
1130 /* FALLTHROUGH */
1131 case VFIFO:
1132 /* Allow modifications of special files even if in the file
1133 * system is mounted read-only (we are not modifying the
1134 * files themselves, but the objects they represent). */
1135 return 0;
1136
1137 default:
1138 /* Anything else is unsupported. */
1139 return EOPNOTSUPP;
1140 }
1141
1142 /* Immutable or append-only files cannot be modified, either. */
1143 if (node->tn_flags & (IMMUTABLE | APPEND))
1144 return EPERM;
1145
1146 error = tmpfs_truncate(vp, size);
1147 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1148 * for us, as will update tn_status; no need to do that here. */
1149
1150 KASSERT(VOP_ISLOCKED(vp));
1151
1152 return error;
1153 }
1154
1155 /* --------------------------------------------------------------------- */
1156
1157 /*
1158 * Change access and modification times of the given vnode.
1159 * Caller should execute tmpfs_update on vp after a successful execution.
1160 * The vnode must be locked on entry and remain locked on exit.
1161 */
1162 int
1163 tmpfs_chtimes(struct vnode *vp, const struct timespec *atime,
1164 const struct timespec *mtime, const struct timespec *btime,
1165 int vaflags, kauth_cred_t cred, struct lwp *l)
1166 {
1167 int error;
1168 struct tmpfs_node *node;
1169
1170 KASSERT(VOP_ISLOCKED(vp));
1171
1172 node = VP_TO_TMPFS_NODE(vp);
1173
1174 /* Disallow this operation if the file system is mounted read-only. */
1175 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1176 return EROFS;
1177
1178 /* Immutable or append-only files cannot be modified, either. */
1179 if (node->tn_flags & (IMMUTABLE | APPEND))
1180 return EPERM;
1181
1182 error = genfs_can_chtimes(vp, vaflags, node->tn_uid, cred);
1183
1184 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
1185 error);
1186 if (error)
1187 return (error);
1188
1189 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1190 node->tn_status |= TMPFS_NODE_ACCESSED;
1191
1192 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1193 node->tn_status |= TMPFS_NODE_MODIFIED;
1194
1195 if (btime->tv_sec == VNOVAL && btime->tv_nsec == VNOVAL)
1196 btime = NULL;
1197
1198 tmpfs_update(vp, atime, mtime, btime, 0);
1199 VN_KNOTE(vp, NOTE_ATTRIB);
1200
1201 KASSERT(VOP_ISLOCKED(vp));
1202
1203 return 0;
1204 }
1205
1206 /* --------------------------------------------------------------------- */
1207
1208 /* Sync timestamps */
1209 void
1210 tmpfs_itimes(struct vnode *vp, const struct timespec *acc,
1211 const struct timespec *mod, const struct timespec *birth)
1212 {
1213 struct tmpfs_node *node;
1214 struct timespec nowtm;
1215
1216 node = VP_TO_TMPFS_NODE(vp);
1217
1218 if ((node->tn_status & (TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
1219 TMPFS_NODE_CHANGED)) == 0)
1220 return;
1221
1222 if (birth != NULL) {
1223 node->tn_birthtime = *birth;
1224 }
1225 vfs_timestamp(&nowtm);
1226
1227 if (node->tn_status & TMPFS_NODE_ACCESSED) {
1228 node->tn_atime = acc ? *acc : nowtm;
1229 }
1230 if (node->tn_status & TMPFS_NODE_MODIFIED) {
1231 node->tn_mtime = mod ? *mod : nowtm;
1232 }
1233 if (node->tn_status & TMPFS_NODE_CHANGED) {
1234 node->tn_ctime = nowtm;
1235 }
1236
1237 node->tn_status &=
1238 ~(TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED | TMPFS_NODE_CHANGED);
1239 }
1240
1241 /* --------------------------------------------------------------------- */
1242
1243 void
1244 tmpfs_update(struct vnode *vp, const struct timespec *acc,
1245 const struct timespec *mod, const struct timespec *birth, int flags)
1246 {
1247
1248 struct tmpfs_node *node;
1249
1250 KASSERT(VOP_ISLOCKED(vp));
1251
1252 node = VP_TO_TMPFS_NODE(vp);
1253
1254 #if 0
1255 if (flags & UPDATE_CLOSE)
1256 ; /* XXX Need to do anything special? */
1257 #endif
1258
1259 tmpfs_itimes(vp, acc, mod, birth);
1260
1261 KASSERT(VOP_ISLOCKED(vp));
1262 }
1263
1264 /* --------------------------------------------------------------------- */
1265
1266 int
1267 tmpfs_truncate(struct vnode *vp, off_t length)
1268 {
1269 bool extended;
1270 int error;
1271 struct tmpfs_node *node;
1272
1273 node = VP_TO_TMPFS_NODE(vp);
1274 extended = length > node->tn_size;
1275
1276 if (length < 0) {
1277 error = EINVAL;
1278 goto out;
1279 }
1280
1281 if (node->tn_size == length) {
1282 error = 0;
1283 goto out;
1284 }
1285
1286 error = tmpfs_reg_resize(vp, length);
1287 if (error == 0)
1288 node->tn_status |= TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1289
1290 out:
1291 tmpfs_update(vp, NULL, NULL, NULL, 0);
1292
1293 return error;
1294 }
1295