tmpfs_subr.c revision 1.8 1 /* $NetBSD: tmpfs_subr.c,v 1.8 2005/09/23 15:36:15 jmmv Exp $ */
2
3 /*
4 * Copyright (c) 2005 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.8 2005/09/23 15:36:15 jmmv 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
59 #include <uvm/uvm.h>
60
61 #include <miscfs/specfs/specdev.h>
62 #include <fs/tmpfs/tmpfs.h>
63 #include <fs/tmpfs/tmpfs_fifoops.h>
64 #include <fs/tmpfs/tmpfs_specops.h>
65 #include <fs/tmpfs/tmpfs_vnops.h>
66
67 /* --------------------------------------------------------------------- */
68
69 /*
70 * Allocates a new node of type 'type' inside the 'tmp' mount point, with
71 * its owner set to 'uid', its group to 'gid' and its mode set to 'mode',
72 * using the credentials of the process 'p'.
73 *
74 * If the node type is set to 'VDIR', then the parent parameter must point
75 * to the parent directory of the node being created. It may only be NULL
76 * while allocating the root node.
77 *
78 * If the node type is set to 'VBLK' or 'VCHR', then the rdev parameter
79 * specifies the device the node represents.
80 *
81 * If the node type is set to 'VLNK', then the parameter target specifies
82 * the file name of the target file for the symbolic link that is being
83 * created.
84 *
85 * Note that new nodes are retrieved from the available list if it has
86 * items or, if it is empty, from the node pool as long as there is enough
87 * space to create them.
88 *
89 * Returns zero on success or an appropriate error code on failure.
90 */
91 int
92 tmpfs_alloc_node(struct tmpfs_mount *tmp, enum vtype type,
93 uid_t uid, gid_t gid, mode_t mode, struct tmpfs_node *parent,
94 char *target, dev_t rdev, struct proc *p, struct tmpfs_node **node)
95 {
96 struct tmpfs_node *nnode;
97
98 /* If the root directory of the 'tmp' file system is not yet
99 * allocated, this must be the request to do it. */
100 KASSERT(IMPLIES(tmp->tm_root == NULL, parent == NULL && type == VDIR));
101
102 KASSERT(IFF(type == VLNK, target != NULL));
103 KASSERT(IFF(type == VBLK || type == VCHR, rdev != VNOVAL));
104
105 KASSERT(uid != VNOVAL && gid != VNOVAL && mode != VNOVAL);
106
107 nnode = NULL;
108 if (LIST_EMPTY(&tmp->tm_nodes_avail)) {
109 KASSERT(tmp->tm_nodes_last <= tmp->tm_nodes_max);
110 if (tmp->tm_nodes_last == tmp->tm_nodes_max)
111 return ENOSPC;
112
113 nnode =
114 (struct tmpfs_node *)TMPFS_POOL_GET(&tmp->tm_node_pool, 0);
115 if (nnode == NULL)
116 return ENOSPC;
117 nnode->tn_id = tmp->tm_nodes_last++;
118 nnode->tn_gen = 0;
119 } else {
120 nnode = LIST_FIRST(&tmp->tm_nodes_avail);
121 LIST_REMOVE(nnode, tn_entries);
122 nnode->tn_gen++;
123 }
124 KASSERT(nnode != NULL);
125 LIST_INSERT_HEAD(&tmp->tm_nodes_used, nnode, tn_entries);
126
127 /* Generic initialization. */
128 nnode->tn_type = type;
129 nnode->tn_size = 0;
130 nnode->tn_status = 0;
131 nnode->tn_flags = 0;
132 nnode->tn_links = 0;
133 (void)nanotime(&nnode->tn_atime);
134 nnode->tn_birthtime = nnode->tn_ctime = nnode->tn_mtime =
135 nnode->tn_atime;
136 nnode->tn_uid = uid;
137 nnode->tn_gid = gid;
138 nnode->tn_mode = mode;
139 nnode->tn_vnode = NULL;
140
141 /* Type-specific initialization. */
142 switch (nnode->tn_type) {
143 case VBLK:
144 case VCHR:
145 nnode->tn_rdev = rdev;
146 break;
147
148 case VDIR:
149 TAILQ_INIT(&nnode->tn_dir);
150 nnode->tn_parent = (parent == NULL) ? nnode : parent;
151 nnode->tn_readdir_lastn = 0;
152 nnode->tn_readdir_lastp = NULL;
153 nnode->tn_links++;
154 nnode->tn_parent->tn_links++;
155 break;
156
157 case VFIFO:
158 /* FALLTHROUGH */
159 case VSOCK:
160 break;
161
162 case VLNK:
163 KASSERT(strlen(target) < MAXPATHLEN);
164 nnode->tn_size = strlen(target);
165 nnode->tn_link = tmpfs_str_pool_get(&tmp->tm_str_pool,
166 nnode->tn_size, 0);
167 if (nnode->tn_link == NULL) {
168 nnode->tn_type = VNON;
169 tmpfs_free_node(tmp, nnode);
170 return ENOSPC;
171 }
172 memcpy(nnode->tn_link, target, nnode->tn_size);
173 break;
174
175 case VREG:
176 nnode->tn_aobj = uao_create(INT32_MAX - PAGE_SIZE, 0);
177 nnode->tn_aobj_pages = 0;
178 break;
179
180 default:
181 KASSERT(0);
182 }
183
184 *node = nnode;
185 return 0;
186 }
187
188 /* --------------------------------------------------------------------- */
189
190 /*
191 * Destroys the node pointed to by node from the file system 'tmp'.
192 * If the node does not belong to the given mount point, the results are
193 * unpredicted.
194 *
195 * If the node references a directory; no entries are allowed because
196 * their removal could need a recursive algorithm, something forbidden in
197 * kernel space. Furthermore, there is not need to provide such
198 * functionality (recursive removal) because the only primitives offered
199 * to the user are the removal of empty directories and the deletion of
200 * individual files.
201 *
202 * Note that nodes are not really deleted; in fact, when a node has been
203 * allocated, it cannot be deleted during the whole life of the file
204 * system. Instead, they are moved to the available list and remain there
205 * until reused.
206 */
207 void
208 tmpfs_free_node(struct tmpfs_mount *tmp, struct tmpfs_node *node)
209 {
210 ino_t id;
211 unsigned long gen;
212 size_t pages;
213
214 switch (node->tn_type) {
215 case VNON:
216 /* Do not do anything. VNON is provided to let the
217 * allocation routine clean itself easily by avoiding
218 * duplicating code in it. */
219 /* FALLTHROUGH */
220 case VBLK:
221 /* FALLTHROUGH */
222 case VCHR:
223 /* FALLTHROUGH */
224 case VDIR:
225 /* FALLTHROUGH */
226 case VFIFO:
227 /* FALLTHROUGH */
228 case VSOCK:
229 pages = 0;
230 break;
231
232 case VLNK:
233 tmpfs_str_pool_put(&tmp->tm_str_pool, node->tn_link,
234 node->tn_size);
235 pages = 0;
236 break;
237
238 case VREG:
239 if (node->tn_aobj != NULL)
240 uao_detach(node->tn_aobj);
241 pages = node->tn_aobj_pages;
242 break;
243
244 default:
245 KASSERT(0);
246 pages = 0; /* Shut up gcc when !DIAGNOSTIC. */
247 break;
248 }
249
250 tmp->tm_pages_used -= pages;
251
252 LIST_REMOVE(node, tn_entries);
253 id = node->tn_id;
254 gen = node->tn_gen;
255 memset(node, 0, sizeof(struct tmpfs_node));
256 node->tn_id = id;
257 node->tn_type = VNON;
258 node->tn_gen = gen;
259 LIST_INSERT_HEAD(&tmp->tm_nodes_avail, node, tn_entries);
260 }
261
262 /* --------------------------------------------------------------------- */
263
264 /*
265 * Allocates a new directory entry for the node node with a name of name.
266 * The new directory entry is returned in *de.
267 *
268 * The link count of node is increased by one to reflect the new object
269 * referencing it.
270 *
271 * Returns zero on success or an appropriate error code on failure.
272 */
273 int
274 tmpfs_alloc_dirent(struct tmpfs_mount *tmp, struct tmpfs_node *node,
275 const char *name, uint16_t len, struct tmpfs_dirent **de)
276 {
277 struct tmpfs_dirent *nde;
278
279 nde = (struct tmpfs_dirent *)TMPFS_POOL_GET(&tmp->tm_dirent_pool, 0);
280 if (nde == NULL)
281 return ENOSPC;
282
283 nde->td_name = tmpfs_str_pool_get(&tmp->tm_str_pool, len, 0);
284 if (nde->td_name == NULL) {
285 TMPFS_POOL_PUT(&tmp->tm_dirent_pool, nde);
286 return ENOSPC;
287 }
288 nde->td_namelen = len;
289 memcpy(nde->td_name, name, len);
290 nde->td_node = node;
291
292 node->tn_links++;
293 *de = nde;
294
295 return 0;
296 }
297
298 /* --------------------------------------------------------------------- */
299
300 /*
301 * Frees a directory entry. It is the caller's responsibility to destroy
302 * the node referenced by it if needed.
303 *
304 * The link count of node is decreased by one to reflect the removal of an
305 * object that referenced it. This only happens if 'node_exists' is true;
306 * otherwise the function will not access the node referred to by the
307 * directory entry, as it may already have been released from the outside.
308 */
309 void
310 tmpfs_free_dirent(struct tmpfs_mount *tmp, struct tmpfs_dirent *de,
311 boolean_t node_exists)
312 {
313 if (node_exists) {
314 struct tmpfs_node *node;
315
316 node = de->td_node;
317
318 KASSERT(node->tn_links > 0);
319 node->tn_links--;
320 }
321
322 tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name, de->td_namelen);
323 TMPFS_POOL_PUT(&tmp->tm_dirent_pool, de);
324 }
325
326 /* --------------------------------------------------------------------- */
327
328 /*
329 * Allocates a new vnode for the node node or returns a new reference to
330 * an existing one if the node had already a vnode referencing it. The
331 * resulting locked vnode is returned in *vpp.
332 *
333 * Returns zero on success or an appropriate error code on failure.
334 */
335 int
336 tmpfs_alloc_vp(struct mount *mp, struct tmpfs_node *node, struct vnode **vpp)
337 {
338 int error;
339 struct vnode *nvp;
340 struct vnode *vp;
341
342 vp = NULL;
343
344 if (node->tn_vnode != NULL) {
345 vp = node->tn_vnode;
346 vget(vp, LK_EXCLUSIVE | LK_RETRY);
347 error = 0;
348 goto out;
349 }
350
351 /* Get a new vnode and associate it with our node. */
352 error = getnewvnode(VT_TMPFS, mp, tmpfs_vnodeop_p, &vp);
353 if (error != 0)
354 goto out;
355 KASSERT(vp != NULL);
356
357 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
358 if (error != 0) {
359 vp->v_data = NULL;
360 ungetnewvnode(vp);
361 vp = NULL;
362 goto out;
363 }
364
365 vp->v_data = node;
366 vp->v_type = node->tn_type;
367
368 /* Type-specific initialization. */
369 switch (node->tn_type) {
370 case VBLK:
371 /* FALLTHROUGH */
372 case VCHR:
373 vp->v_op = tmpfs_specop_p;
374 nvp = checkalias(vp, node->tn_rdev, mp);
375 if (nvp != NULL) {
376 /* Discard unneeded vnode, but save its inode. */
377 nvp->v_data = vp->v_data;
378 vp->v_data = NULL;
379
380 /* XXX spec_vnodeops has no locking, so we have to
381 * do it explicitly. */
382 VOP_UNLOCK(vp, 0);
383 vp->v_op = spec_vnodeop_p;
384 vp->v_flag &= ~VLOCKSWORK;
385 vrele(vp);
386 vgone(vp);
387
388 /* Reinitialize aliased node. */
389 vp = nvp;
390 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
391 if (error != 0) {
392 vp->v_data = NULL;
393 vp = NULL;
394 goto out;
395 }
396 }
397 break;
398
399 case VDIR:
400 vp->v_flag = node->tn_parent == node ? VROOT : 0;
401 break;
402
403 case VFIFO:
404 vp->v_op = tmpfs_fifoop_p;
405 break;
406
407 case VLNK:
408 /* FALLTHROUGH */
409 case VREG:
410 /* FALLTHROUGH */
411 case VSOCK:
412 break;
413
414 default:
415 KASSERT(0);
416 }
417
418 uvm_vnp_setsize(vp, node->tn_size);
419
420 error = 0;
421
422 out:
423 *vpp = node->tn_vnode = vp;
424
425 KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
426 KASSERT(*vpp == node->tn_vnode);
427
428 return error;
429 }
430
431 /* --------------------------------------------------------------------- */
432
433 /*
434 * Destroys the association between the vnode vp and the node it
435 * references.
436 */
437 void
438 tmpfs_free_vp(struct vnode *vp)
439 {
440 struct tmpfs_node *node;
441
442 node = VP_TO_TMPFS_NODE(vp);
443
444 node->tn_vnode = NULL;
445 vp->v_data = NULL;
446 }
447
448 /* --------------------------------------------------------------------- */
449
450 /* Allocates a new file of type 'type' and adds it to the parent directory
451 * 'dvp'; this addition is done using the component name given in 'cnp'.
452 * The ownership of the new file is automatically assigned based on the
453 * credentials of the caller (through 'cnp'), the group is set based on
454 * the parent directory and the mode is determined from the 'vap' argument.
455 * If successful, *vpp holds a vnode to the newly created file and zero
456 * is returned. Otherwise *vpp is NULL and the function returns an
457 * appropriate error code .*/
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 KASSERT(cnp->cn_flags & HASBUF);
471
472 tmp = VFS_TO_TMPFS(dvp->v_mount);
473 dnode = VP_TO_TMPFS_DIR(dvp);
474 *vpp = NULL;
475
476 /* If the entry we are creating is a directory, we cannot overflow
477 * the number of links of its parent, because it will get a new
478 * link. */
479 if (vap->va_type == VDIR) {
480 /* Ensure that we do not overflow the maximum number of links
481 * imposed by the system. */
482 KASSERT(dnode->tn_links <= LINK_MAX);
483 if (dnode->tn_links == LINK_MAX) {
484 error = EMLINK;
485 goto out;
486 }
487
488 parent = dnode;
489 } else
490 parent = NULL;
491
492 /* Allocate a node that represents the new file. */
493 error = tmpfs_alloc_node(tmp, vap->va_type, cnp->cn_cred->cr_uid,
494 dnode->tn_gid, vap->va_mode, parent, target, vap->va_rdev,
495 cnp->cn_proc, &node);
496 if (error != 0)
497 goto out;
498
499 /* Allocate a directory entry that points to the new file. */
500 error = tmpfs_alloc_dirent(tmp, node, cnp->cn_nameptr, cnp->cn_namelen,
501 &de);
502 if (error != 0) {
503 tmpfs_free_node(tmp, node);
504 goto out;
505 }
506
507 /* Allocate a vnode for the new file. */
508 error = tmpfs_alloc_vp(dvp->v_mount, node, vpp);
509 if (error != 0) {
510 tmpfs_free_dirent(tmp, de, TRUE);
511 tmpfs_free_node(tmp, node);
512 goto out;
513 }
514
515 /* Now that all required items are allocated, we can proceed to
516 * insert the new node into the directory, an operation that
517 * cannot fail. */
518 tmpfs_dir_attach(dvp, de);
519 VN_KNOTE(dvp, NOTE_WRITE);
520
521 out:
522 if (error != 0 || !(cnp->cn_flags & SAVESTART))
523 PNBUF_PUT(cnp->cn_pnbuf);
524 vput(dvp);
525
526 KASSERT(!VOP_ISLOCKED(dvp));
527 KASSERT(IFF(error == 0, *vpp != NULL));
528
529 return error;
530 }
531
532 /* --------------------------------------------------------------------- */
533
534 /*
535 * Attaches the directory entry de to the directory represented by vp.
536 * Note that this does not change the link count of the node pointed by
537 * the directory entry, as this is done by tmpfs_alloc_dirent.
538 */
539 void
540 tmpfs_dir_attach(struct vnode *vp, struct tmpfs_dirent *de)
541 {
542 struct tmpfs_node *dnode;
543
544 dnode = VP_TO_TMPFS_DIR(vp);
545
546 TAILQ_INSERT_TAIL(&dnode->tn_dir, de, td_entries);
547 dnode->tn_size += sizeof(struct tmpfs_dirent);
548 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
549 TMPFS_NODE_MODIFIED;
550 uvm_vnp_setsize(vp, dnode->tn_size);
551 }
552
553 /* --------------------------------------------------------------------- */
554
555 /*
556 * Detaches the directory entry de from the directory represented by vp.
557 * Note that this does not change the link count of the node pointed by
558 * the directory entry, as this is done by tmpfs_free_dirent.
559 */
560 void
561 tmpfs_dir_detach(struct vnode *vp, struct tmpfs_dirent *de)
562 {
563 struct tmpfs_node *dnode;
564
565 KASSERT(VOP_ISLOCKED(vp));
566
567 dnode = VP_TO_TMPFS_DIR(vp);
568
569 if (dnode->tn_readdir_lastp == de) {
570 dnode->tn_readdir_lastn = 0;
571 dnode->tn_readdir_lastp = NULL;
572 }
573
574 TAILQ_REMOVE(&dnode->tn_dir, de, td_entries);
575 dnode->tn_size -= sizeof(struct tmpfs_dirent);
576 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
577 TMPFS_NODE_MODIFIED;
578 uvm_vnp_setsize(vp, dnode->tn_size);
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 boolean_t found;
595 struct tmpfs_dirent *de;
596
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 found = 0;
605 TAILQ_FOREACH(de, &node->tn_dir, td_entries) {
606 KASSERT(cnp->cn_namelen < 0xffff);
607 if (de->td_namelen == (uint16_t)cnp->cn_namelen &&
608 memcmp(de->td_name, cnp->cn_nameptr, de->td_namelen) == 0) {
609 found = 1;
610 break;
611 }
612 }
613
614 return found ? de : NULL;
615 }
616
617 /* --------------------------------------------------------------------- */
618
619 /* Helper function for tmpfs_readdir. Creates a '.' entry for the given
620 * directory and returns it in the uio space. The function returns 0
621 * on success, -1 if there was not enough space in the uio structure to
622 * hold the directory entry or an appropriate error code if another
623 * error happens. */
624 int
625 tmpfs_dir_getdotdent(struct tmpfs_node *node, struct uio *uio)
626 {
627 int error;
628 struct dirent dent;
629
630 TMPFS_VALIDATE_DIR(node);
631 KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOT);
632
633 dent.d_fileno = node->tn_id;
634 dent.d_type = DT_DIR;
635 dent.d_namlen = 1;
636 dent.d_name[0] = '.';
637 dent.d_name[1] = '\0';
638 dent.d_reclen = _DIRENT_SIZE(&dent);
639
640 if (dent.d_reclen > uio->uio_resid)
641 error = -1;
642 else {
643 error = uiomove(&dent, dent.d_reclen, uio);
644 if (error == 0)
645 uio->uio_offset = TMPFS_DIRCOOKIE_DOTDOT;
646 }
647
648 node->tn_status |= TMPFS_NODE_ACCESSED;
649
650 return error;
651 }
652
653 /* --------------------------------------------------------------------- */
654
655 /* Helper function for tmpfs_readdir. Creates a '..' entry for the given
656 * directory and returns it in the uio space. The function returns 0
657 * on success, -1 if there was not enough space in the uio structure to
658 * hold the directory entry or an appropriate error code if another
659 * error happens. */
660 int
661 tmpfs_dir_getdotdotdent(struct tmpfs_node *node, struct uio *uio)
662 {
663 int error;
664 struct dirent dent;
665
666 TMPFS_VALIDATE_DIR(node);
667 KASSERT(uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT);
668
669 dent.d_fileno = node->tn_parent->tn_id;
670 dent.d_type = DT_DIR;
671 dent.d_namlen = 2;
672 dent.d_name[0] = '.';
673 dent.d_name[1] = '.';
674 dent.d_name[2] = '\0';
675 dent.d_reclen = _DIRENT_SIZE(&dent);
676
677 if (dent.d_reclen > uio->uio_resid)
678 error = -1;
679 else {
680 error = uiomove(&dent, dent.d_reclen, uio);
681 if (error == 0) {
682 struct tmpfs_dirent *de;
683
684 de = TAILQ_FIRST(&node->tn_dir);
685 if (de == NULL)
686 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
687 else
688 uio->uio_offset = TMPFS_DIRCOOKIE(de);
689 }
690 }
691
692 node->tn_status |= TMPFS_NODE_ACCESSED;
693
694 return error;
695 }
696
697 /* --------------------------------------------------------------------- */
698
699 /* lookup a directory entry by cookie */
700 struct tmpfs_dirent *
701 tmpfs_dir_lookupbycookie(struct tmpfs_node *node, off_t cookie)
702 {
703 struct tmpfs_dirent *de;
704
705 if (cookie == node->tn_readdir_lastn &&
706 node->tn_readdir_lastp != NULL) {
707 return node->tn_readdir_lastp;
708 }
709
710 TAILQ_FOREACH(de, &node->tn_dir, td_entries) {
711 if (TMPFS_DIRCOOKIE(de) == cookie) {
712 break;
713 }
714 }
715
716 return de;
717 }
718
719 /* --------------------------------------------------------------------- */
720
721 /* Helper function for tmpfs_readdir. Returns as much directory entries
722 * as can fit in the uio space. The read starts at uio->uio_offset.
723 * The function returns 0 on success, -1 if there was not enough space
724 * in the uio structure to hold the directory entry or an appropriate
725 * error code if another error happens. */
726 int
727 tmpfs_dir_getdents(struct tmpfs_node *node, struct uio *uio, off_t *cntp)
728 {
729 int error;
730 off_t startcookie;
731 struct tmpfs_dirent *de;
732
733 TMPFS_VALIDATE_DIR(node);
734
735 /* Locate the first directory entry we have to return. We have cached
736 * the last readdir in the node, so use those values if appropriate.
737 * Otherwise do a linear scan to find the requested entry. */
738 startcookie = uio->uio_offset;
739 KASSERT(startcookie != TMPFS_DIRCOOKIE_DOT);
740 KASSERT(startcookie != TMPFS_DIRCOOKIE_DOTDOT);
741 if (startcookie == TMPFS_DIRCOOKIE_EOF) {
742 return 0;
743 } else {
744 de = tmpfs_dir_lookupbycookie(node, startcookie);
745 }
746 if (de == NULL) {
747 return EINVAL;
748 }
749
750 /* Read as much entries as possible; i.e., until we reach the end of
751 * the directory or we exhaust uio space. */
752 do {
753 struct dirent d;
754
755 /* Create a dirent structure representing the current
756 * tmpfs_node and fill it. */
757 d.d_fileno = de->td_node->tn_id;
758 switch (de->td_node->tn_type) {
759 case VBLK:
760 d.d_type = DT_BLK;
761 break;
762
763 case VCHR:
764 d.d_type = DT_CHR;
765 break;
766
767 case VDIR:
768 d.d_type = DT_DIR;
769 break;
770
771 case VFIFO:
772 d.d_type = DT_FIFO;
773 break;
774
775 case VLNK:
776 d.d_type = DT_LNK;
777 break;
778
779 case VREG:
780 d.d_type = DT_REG;
781 break;
782
783 case VSOCK:
784 d.d_type = DT_SOCK;
785 break;
786
787 default:
788 KASSERT(0);
789 }
790 d.d_namlen = de->td_namelen;
791 KASSERT(de->td_namelen < sizeof(d.d_name));
792 (void)memcpy(d.d_name, de->td_name, de->td_namelen);
793 d.d_name[de->td_namelen] = '\0';
794 d.d_reclen = _DIRENT_SIZE(&d);
795
796 /* Stop reading if the directory entry we are treating is
797 * bigger than the amount of data that can be returned. */
798 if (d.d_reclen > uio->uio_resid) {
799 error = -1;
800 break;
801 }
802
803 /* Copy the new dirent structure into the output buffer and
804 * advance pointers. */
805 error = uiomove(&d, d.d_reclen, uio);
806
807 (*cntp)++;
808 de = TAILQ_NEXT(de, td_entries);
809 } while (error == 0 && uio->uio_resid > 0 && de != NULL);
810
811 /* Update the offset and cache. */
812 if (de == NULL) {
813 uio->uio_offset = TMPFS_DIRCOOKIE_EOF;
814 node->tn_readdir_lastn = 0;
815 node->tn_readdir_lastp = NULL;
816 } else {
817 node->tn_readdir_lastn = uio->uio_offset = TMPFS_DIRCOOKIE(de);
818 node->tn_readdir_lastp = de;
819 }
820
821 node->tn_status |= TMPFS_NODE_ACCESSED;
822
823 return error;
824 }
825
826 /* --------------------------------------------------------------------- */
827
828 /*
829 * Resizes the aobj associated to the regular file pointed to by vp to
830 * the size newsize. 'vp' must point to a vnode that represents a regular
831 * file. 'newsize' must be positive.
832 *
833 * Returns zero on success or an appropriate error code on failure.
834 */
835 int
836 tmpfs_reg_resize(struct vnode *vp, off_t newsize)
837 {
838 int error;
839 size_t newpages, oldpages;
840 struct tmpfs_mount *tmp;
841 struct tmpfs_node *node;
842
843 KASSERT(vp->v_type == VREG);
844 KASSERT(newsize >= 0);
845
846 node = VP_TO_TMPFS_NODE(vp);
847 tmp = VFS_TO_TMPFS(vp->v_mount);
848
849 /* Convert the old and new sizes to the number of pages needed to
850 * store them. It may happen that we do not need to do anything
851 * because the last allocated page can accommodate the change on
852 * its own. */
853 oldpages = round_page(node->tn_size) / PAGE_SIZE;
854 KASSERT(oldpages == node->tn_aobj_pages);
855 newpages = round_page(newsize) / PAGE_SIZE;
856
857 if (newpages > oldpages &&
858 newpages - oldpages > TMPFS_PAGES_AVAIL(tmp)) {
859 error = ENOSPC;
860 goto out;
861 }
862
863 node->tn_aobj_pages = newpages;
864
865 tmp->tm_pages_used += (newpages - oldpages);
866 node->tn_size = newsize;
867 uvm_vnp_setsize(vp, newsize);
868
869 error = 0;
870
871 out:
872 return error;
873 }
874
875 /* --------------------------------------------------------------------- */
876
877 /* Returns information about the number of available memory pages,
878 * including physical and virtual ones.
879 *
880 * If 'total' is TRUE, the value returned is the total amount of memory
881 * pages configured for the system (either in use or free).
882 * If it is FALSE, the value returned is the amount of free memory pages.
883 *
884 * Remember to remove TMPFS_PAGES_RESERVED from the returned value to avoid
885 * excessive memory usage.
886 *
887 * XXX: This function is used every time TMPFS_PAGES_MAX is called to gather
888 * the amount of free memory, something that happens during _each_
889 * object allocation. The time it takes to run this function so many
890 * times is not negligible, so this value should be stored as an
891 * aggregate somewhere, possibly within UVM (we cannot do it ourselves
892 * because we can't get notifications on memory usage changes). */
893 size_t
894 tmpfs_mem_info(boolean_t total)
895 {
896 int i, sec;
897 register_t retval;
898 size_t size;
899 struct swapent *sep;
900
901 sec = uvmexp.nswapdev;
902 sep = (struct swapent *)malloc(sizeof(struct swapent) * sec, M_TEMP,
903 M_WAITOK);
904 KASSERT(sep != NULL);
905 uvm_swap_stats(SWAP_STATS, sep, sec, &retval);
906 KASSERT(retval == sec);
907
908 size = 0;
909 if (total) {
910 for (i = 0; i < sec; i++)
911 size += dbtob(sep[i].se_nblks) / PAGE_SIZE;
912 } else {
913 for (i = 0; i < sec; i++)
914 size += dbtob(sep[i].se_nblks - sep[i].se_inuse) /
915 PAGE_SIZE;
916 }
917 size += uvmexp.free;
918
919 free(sep, M_TEMP);
920
921 return size;
922 }
923
924 /* --------------------------------------------------------------------- */
925
926 /* Change flags of the given vnode.
927 * Caller should execute VOP_UPDATE on vp after a successful execution.
928 * The vnode must be locked on entry and remain locked on exit. */
929 int
930 tmpfs_chflags(struct vnode *vp, int flags, struct ucred *cred, struct proc *p)
931 {
932 int error;
933 struct tmpfs_node *node;
934
935 KASSERT(VOP_ISLOCKED(vp));
936
937 node = VP_TO_TMPFS_NODE(vp);
938
939 /* Disallow this operation if the file system is mounted read-only. */
940 if (vp->v_mount->mnt_flag & MNT_RDONLY)
941 return EROFS;
942
943 /* XXX: The following comes from UFS code, and can be found in
944 * several other file systems. Shouldn't this be centralized
945 * somewhere? */
946 if (cred->cr_uid != node->tn_uid &&
947 (error = suser(cred, &p->p_acflag)))
948 return error;
949 if (cred->cr_uid == 0) {
950 /* The super-user is only allowed to change flags if the file
951 * wasn't protected before and the securelevel is zero. */
952 if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) &&
953 securelevel > 0)
954 return EPERM;
955 node->tn_flags = flags;
956 } else {
957 /* Regular users can change flags provided they only want to
958 * change user-specific ones, not those reserved for the
959 * super-user. */
960 if ((node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) ||
961 (flags & UF_SETTABLE) != flags)
962 return EPERM;
963 if ((node->tn_flags & SF_SETTABLE) != (flags & SF_SETTABLE))
964 return EPERM;
965 node->tn_flags &= SF_SETTABLE;
966 node->tn_flags |= (flags & UF_SETTABLE);
967 }
968
969 node->tn_status |= TMPFS_NODE_CHANGED;
970 VN_KNOTE(vp, NOTE_ATTRIB);
971
972 KASSERT(VOP_ISLOCKED(vp));
973
974 return 0;
975 }
976
977 /* --------------------------------------------------------------------- */
978
979 /* Change access mode on the given vnode.
980 * Caller should execute VOP_UPDATE on vp after a successful execution.
981 * The vnode must be locked on entry and remain locked on exit. */
982 int
983 tmpfs_chmod(struct vnode *vp, mode_t mode, struct ucred *cred, struct proc *p)
984 {
985 int error;
986 struct tmpfs_node *node;
987
988 KASSERT(VOP_ISLOCKED(vp));
989
990 node = VP_TO_TMPFS_NODE(vp);
991
992 /* Disallow this operation if the file system is mounted read-only. */
993 if (vp->v_mount->mnt_flag & MNT_RDONLY)
994 return EROFS;
995
996 /* Immutable or append-only files cannot be modified, either. */
997 if (node->tn_flags & (IMMUTABLE | APPEND))
998 return EPERM;
999
1000 /* XXX: The following comes from UFS code, and can be found in
1001 * several other file systems. Shouldn't this be centralized
1002 * somewhere? */
1003 if (cred->cr_uid != node->tn_uid &&
1004 (error = suser(cred, &p->p_acflag)))
1005 return error;
1006 if (cred->cr_uid != 0) {
1007 if (vp->v_type != VDIR && (mode & S_ISTXT))
1008 return EFTYPE;
1009
1010 if (!groupmember(node->tn_gid, cred) && (mode & S_ISGID))
1011 return EPERM;
1012 }
1013
1014 node->tn_mode = (mode & ALLPERMS);
1015
1016 node->tn_status |= TMPFS_NODE_CHANGED;
1017 VN_KNOTE(vp, NOTE_ATTRIB);
1018
1019 KASSERT(VOP_ISLOCKED(vp));
1020
1021 return 0;
1022 }
1023
1024 /* --------------------------------------------------------------------- */
1025
1026 /* Change ownership of the given vnode. At least one of uid or gid must
1027 * be different than VNOVAL. If one is set to that value, the attribute
1028 * is unchanged.
1029 * Caller should execute VOP_UPDATE on vp after a successful execution.
1030 * The vnode must be locked on entry and remain locked on exit. */
1031 int
1032 tmpfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
1033 struct proc *p)
1034 {
1035 int error;
1036 struct tmpfs_node *node;
1037
1038 KASSERT(VOP_ISLOCKED(vp));
1039
1040 node = VP_TO_TMPFS_NODE(vp);
1041
1042 /* Assign default values if they are unknown. */
1043 KASSERT(uid != VNOVAL || gid != VNOVAL);
1044 if (uid == VNOVAL)
1045 uid = node->tn_uid;
1046 if (gid == VNOVAL)
1047 gid = node->tn_gid;
1048 KASSERT(uid != VNOVAL && gid != VNOVAL);
1049
1050 /* Disallow this operation if the file system is mounted read-only. */
1051 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1052 return EROFS;
1053
1054 /* Immutable or append-only files cannot be modified, either. */
1055 if (node->tn_flags & (IMMUTABLE | APPEND))
1056 return EPERM;
1057
1058 /* XXX: The following comes from UFS code, and can be found in
1059 * several other file systems. Shouldn't this be centralized
1060 * somewhere? */
1061 if ((cred->cr_uid != node->tn_uid || uid != node->tn_uid ||
1062 (gid != node->tn_gid && !(cred->cr_gid == node->tn_gid ||
1063 groupmember(gid, cred)))) &&
1064 ((error = suser(cred, &p->p_acflag)) != 0))
1065 return error;
1066
1067 node->tn_uid = uid;
1068 node->tn_gid = gid;
1069
1070 node->tn_status |= TMPFS_NODE_CHANGED;
1071 VN_KNOTE(vp, NOTE_ATTRIB);
1072
1073 KASSERT(VOP_ISLOCKED(vp));
1074
1075 return 0;
1076 }
1077
1078 /* --------------------------------------------------------------------- */
1079
1080 /* Change size of the given vnode.
1081 * Caller should execute VOP_UPDATE on vp after a successful execution.
1082 * The vnode must be locked on entry and remain locked on exit. */
1083 int
1084 tmpfs_chsize(struct vnode *vp, u_quad_t size, struct ucred *cred,
1085 struct proc *p)
1086 {
1087 int error;
1088 struct tmpfs_node *node;
1089
1090 KASSERT(VOP_ISLOCKED(vp));
1091
1092 node = VP_TO_TMPFS_NODE(vp);
1093
1094 /* Decide whether this is a valid operation based on the file type. */
1095 error = 0;
1096 switch (vp->v_type) {
1097 case VDIR:
1098 return EISDIR;
1099
1100 case VLNK:
1101 /* FALLTHROUGH */
1102 case VREG:
1103 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1104 return EROFS;
1105 break;
1106
1107 case VBLK:
1108 /* FALLTHROUGH */
1109 case VCHR:
1110 /* FALLTHROUGH */
1111 case VSOCK:
1112 /* FALLTHROUGH */
1113 case VFIFO:
1114 /* Allow modifications of special files even if in the file
1115 * system is mounted read-only (we are not modifying the
1116 * files themselves, but the objects they represent). */
1117 break;
1118
1119 default:
1120 /* Anything else is unsupported. */
1121 return EINVAL;
1122 }
1123
1124 /* Immutable or append-only files cannot be modified, either. */
1125 if (node->tn_flags & (IMMUTABLE | APPEND))
1126 return EPERM;
1127
1128 error = VOP_TRUNCATE(vp, size, 0, cred, p);
1129 /* tmpfs_truncate will raise the NOTE_EXTEND and NOTE_ATTRIB kevents
1130 * for us, as will update tn_status; no need to do that here. */
1131
1132 KASSERT(VOP_ISLOCKED(vp));
1133
1134 return error;
1135 }
1136
1137 /* --------------------------------------------------------------------- */
1138
1139 /* Change access and modification times of the given vnode.
1140 * Caller should execute VOP_UPDATE on vp after a successful execution.
1141 * The vnode must be locked on entry and remain locked on exit. */
1142 int
1143 tmpfs_chtimes(struct vnode *vp, struct timespec *atime, struct timespec *mtime,
1144 int vaflags, struct ucred *cred, struct proc *p)
1145 {
1146 int error;
1147 struct tmpfs_node *node;
1148
1149 KASSERT(VOP_ISLOCKED(vp));
1150
1151 node = VP_TO_TMPFS_NODE(vp);
1152
1153 /* Disallow this operation if the file system is mounted read-only. */
1154 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1155 return EROFS;
1156
1157 /* Immutable or append-only files cannot be modified, either. */
1158 if (node->tn_flags & (IMMUTABLE | APPEND))
1159 return EPERM;
1160
1161 /* XXX: The following comes from UFS code, and can be found in
1162 * several other file systems. Shouldn't this be centralized
1163 * somewhere? */
1164 if (cred->cr_uid != node->tn_uid &&
1165 (error = suser(cred, &p->p_acflag)) &&
1166 ((vaflags & VA_UTIMES_NULL) == 0 ||
1167 (error = VOP_ACCESS(vp, VWRITE, cred, p))))
1168 return error;
1169
1170 if (atime->tv_sec != VNOVAL && atime->tv_nsec != VNOVAL)
1171 node->tn_status |= TMPFS_NODE_ACCESSED;
1172
1173 if (mtime->tv_sec != VNOVAL && mtime->tv_nsec != VNOVAL)
1174 node->tn_status |= TMPFS_NODE_MODIFIED;
1175
1176 error = VOP_UPDATE(vp, atime, mtime, 0);
1177
1178 KASSERT(VOP_ISLOCKED(vp));
1179
1180 return error;
1181 }
1182