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