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