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