1/* $NetBSD: tmpfs_subr.c,v 1.101 2015/10/29 16:19:44 leot Exp $ */
2
3/*
4 * Copyright (c) 2005-2013 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, and by Mindaugas Rasiukevicius.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Efficient memory file system: interfaces for inode and directory entry
35 * construction, destruction and manipulation.
36 *
37 * Reference counting
38 *
39 * The link count of inode (tmpfs_node_t::tn_links) is used as a
40 * reference counter. However, it has slightly different semantics.
41 *
42 * For directories - link count represents directory entries, which
43 * refer to the directories. In other words, it represents the count
44 * of sub-directories. It also takes into account the virtual '.'
45 * entry (which has no real entry in the list). For files - link count
46 * represents the hard links. Since only empty directories can be
47 * removed - link count aligns the reference counting requirements
48 * enough. Note: to check whether directory is not empty, the inode
49 * size (tmpfs_node_t::tn_size) can be used.
50 *
51 * The inode itself, as an object, gathers its first reference when
52 * directory entry is attached via tmpfs_dir_attach(9). For instance,
53 * after regular tmpfs_create(), a file would have a link count of 1,
54 * while directory after tmpfs_mkdir() would have 2 (due to '.').
55 *
56 * Reclamation
57 *
58 * It should be noted that tmpfs inodes rely on a combination of vnode
59 * reference counting and link counting. That is, an inode can only be
60 * destroyed if its associated vnode is inactive. The destruction is
61 * done on vnode reclamation i.e. tmpfs_reclaim(). It should be noted
62 * that tmpfs_node_t::tn_links being 0 is a destruction criterion.
63 *
64 * If an inode has references within the file system (tn_links > 0) and
65 * its inactive vnode gets reclaimed/recycled - then the association is
66 * broken in tmpfs_reclaim(). In such case, an inode will always pass
67 * tmpfs_lookup() and thus vcache_get() to associate a new vnode.
68 *
69 * Lock order
70 *
71 * vnode_t::v_vlock ->
72 * vnode_t::v_interlock
73 */
74
75#include <sys/cdefs.h>
76__KERNEL_RCSID(0, "$NetBSD: tmpfs_subr.c,v 1.101 2015/10/29 16:19:44 leot Exp $");
77
78#include <sys/param.h>
79#include <sys/cprng.h>
80#include <sys/dirent.h>
81#include <sys/event.h>
82#include <sys/kmem.h>
83#include <sys/mount.h>
84#include <sys/namei.h>
85#include <sys/time.h>
86#include <sys/stat.h>
87#include <sys/systm.h>
88#include <sys/vnode.h>
89#include <sys/kauth.h>
90#include <sys/atomic.h>
91
92#include <uvm/uvm.h>
93
94#include <miscfs/specfs/specdev.h>
95#include <miscfs/genfs/genfs.h>
96#include <fs/tmpfs/tmpfs.h>
97#include <fs/tmpfs/tmpfs_fifoops.h>
98#include <fs/tmpfs/tmpfs_specops.h>
99#include <fs/tmpfs/tmpfs_vnops.h>
100
101static void tmpfs_dir_putseq(tmpfs_node_t *, tmpfs_dirent_t *);
102
103/*
104 * Initialize vnode with tmpfs node.
105 */
106static void
107tmpfs_init_vnode(struct vnode *vp, tmpfs_node_t *node)
108{
109 kmutex_t *slock;
110
111 KASSERT(node->tn_vnode == NULL);
112
113 /* Share the interlock with the node. */
114 if (node->tn_type == VREG) {
115 slock = node->tn_spec.tn_reg.tn_aobj->vmobjlock;
116 mutex_obj_hold(slock);
117 uvm_obj_setlock(&vp->v_uobj, slock);
118 }
119
120 vp->v_tag = VT_TMPFS;
121 vp->v_type = node->tn_type;
122
123 /* Type-specific initialization. */
124 switch (vp->v_type) {
125 case VBLK:
126 case VCHR:
127 vp->v_op = tmpfs_specop_p;
128 spec_node_init(vp, node->tn_spec.tn_dev.tn_rdev);
129 break;
130 case VFIFO:
131 vp->v_op = tmpfs_fifoop_p;
132 break;
133 case VDIR:
134 if (node->tn_spec.tn_dir.tn_parent == node)
135 vp->v_vflag |= VV_ROOT;
136 /* FALLTHROUGH */
137 case VLNK:
138 case VREG:
139 case VSOCK:
140 vp->v_op = tmpfs_vnodeop_p;
141 break;
142 default:
143 panic("bad node type %d", vp->v_type);
144 break;
145 }
146
147 vp->v_data = node;
148 node->tn_vnode = vp;
149 uvm_vnp_setsize(vp, node->tn_size);
150}
151
152/*
153 * tmpfs_loadvnode: initialise a vnode for a specified inode.
154 */
155int
156tmpfs_loadvnode(struct mount *mp, struct vnode *vp,
157 const void *key, size_t key_len, const void **new_key)
158{
159 tmpfs_node_t *node;
160
161 KASSERT(key_len == sizeof(node));
162 memcpy(&node, key, key_len);
163
164 if (node->tn_links == 0)
165 return ENOENT;
166
167 tmpfs_init_vnode(vp, node);
168
169 *new_key = &vp->v_data;
170
171 return 0;
172}
173
174/*
175 * tmpfs_newvnode: allocate a new inode of a specified type and
176 * attach the vonode.
177 */
178int
179tmpfs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp,
180 struct vattr *vap, kauth_cred_t cred,
181 size_t *key_len, const void **new_key)
182{
183 tmpfs_mount_t *tmp = VFS_TO_TMPFS(mp);
184 tmpfs_node_t *node, *dnode;
185
186 if (dvp != NULL) {
187 KASSERT(VOP_ISLOCKED(dvp));
188 dnode = VP_TO_TMPFS_DIR(dvp);
189 if (dnode->tn_links == 0)
190 return ENOENT;
191 if (vap->va_type == VDIR) {
192 /* Check for maximum links limit. */
193 if (dnode->tn_links == LINK_MAX)
194 return EMLINK;
195 KASSERT(dnode->tn_links < LINK_MAX);
196 }
197 } else
198 dnode = NULL;
199
200 node = tmpfs_node_get(tmp);
201 if (node == NULL)
202 return ENOSPC;
203
204 /* Initially, no references and no associations. */
205 node->tn_links = 0;
206 node->tn_vnode = NULL;
207 node->tn_holdcount = 0;
208 node->tn_dirent_hint = NULL;
209
210 /*
211 * XXX Where the pool is backed by a map larger than (4GB *
212 * sizeof(*node)), this may produce duplicate inode numbers
213 * for applications that do not understand 64-bit ino_t.
214 */
215 node->tn_id = (ino_t)((uintptr_t)node / sizeof(*node));
216 /*
217 * Make sure the generation number is not zero.
218 * tmpfs_inactive() uses generation zero to mark dead nodes.
219 */
220 do {
221 node->tn_gen = TMPFS_NODE_GEN_MASK & cprng_fast32();
222 } while (node->tn_gen == 0);
223
224 /* Generic initialization. */
225 KASSERT((int)vap->va_type != VNOVAL);
226 node->tn_type = vap->va_type;
227 node->tn_size = 0;
228 node->tn_flags = 0;
229 node->tn_lockf = NULL;
230
231 vfs_timestamp(&node->tn_atime);
232 node->tn_birthtime = node->tn_atime;
233 node->tn_ctime = node->tn_atime;
234 node->tn_mtime = node->tn_atime;
235
236 if (dvp == NULL) {
237 KASSERT(vap->va_uid != VNOVAL && vap->va_gid != VNOVAL);
238 node->tn_uid = vap->va_uid;
239 node->tn_gid = vap->va_gid;
240 vp->v_vflag |= VV_ROOT;
241 } else {
242 KASSERT(dnode != NULL);
243 node->tn_uid = kauth_cred_geteuid(cred);
244 node->tn_gid = dnode->tn_gid;
245 }
246 KASSERT(vap->va_mode != VNOVAL);
247 node->tn_mode = vap->va_mode;
248
249 /* Type-specific initialization. */
250 switch (node->tn_type) {
251 case VBLK:
252 case VCHR:
253 /* Character/block special device. */
254 KASSERT(vap->va_rdev != VNOVAL);
255 node->tn_spec.tn_dev.tn_rdev = vap->va_rdev;
256 break;
257 case VDIR:
258 /* Directory. */
259 TAILQ_INIT(&node->tn_spec.tn_dir.tn_dir);
260 node->tn_spec.tn_dir.tn_parent = NULL;
261 node->tn_spec.tn_dir.tn_seq_arena = NULL;
262 node->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
263 node->tn_spec.tn_dir.tn_readdir_lastp = NULL;
264
265 /* Extra link count for the virtual '.' entry. */
266 node->tn_links++;
267 break;
268 case VFIFO:
269 case VSOCK:
270 break;
271 case VLNK:
272 node->tn_size = 0;
273 node->tn_spec.tn_lnk.tn_link = NULL;
274 break;
275 case VREG:
276 /* Regular file. Create an underlying UVM object. */
277 node->tn_spec.tn_reg.tn_aobj =
278 uao_create(INT32_MAX - PAGE_SIZE, 0);
279 node->tn_spec.tn_reg.tn_aobj_pages = 0;
280 break;
281 default:
282 panic("bad node type %d", vp->v_type);
283 break;
284 }
285
286 tmpfs_init_vnode(vp, node);
287
288 mutex_enter(&tmp->tm_lock);
289 LIST_INSERT_HEAD(&tmp->tm_nodes, node, tn_entries);
290 mutex_exit(&tmp->tm_lock);
291
292 *key_len = sizeof(vp->v_data);
293 *new_key = &vp->v_data;
294
295 return 0;
296}
297
298/*
299 * tmpfs_free_node: remove the inode from a list in the mount point and
300 * destroy the inode structures.
301 */
302void
303tmpfs_free_node(tmpfs_mount_t *tmp, tmpfs_node_t *node)
304{
305 size_t objsz;
306 uint32_t hold;
307
308 mutex_enter(&tmp->tm_lock);
309 hold = atomic_or_32_nv(&node->tn_holdcount, TMPFS_NODE_RECLAIMED);
310 /* Defer destruction to last thread holding this node. */
311 if (hold != TMPFS_NODE_RECLAIMED) {
312 mutex_exit(&tmp->tm_lock);
313 return;
314 }
315 LIST_REMOVE(node, tn_entries);
316 mutex_exit(&tmp->tm_lock);
317
318 switch (node->tn_type) {
319 case VLNK:
320 if (node->tn_size > 0) {
321 tmpfs_strname_free(tmp, node->tn_spec.tn_lnk.tn_link,
322 node->tn_size);
323 }
324 break;
325 case VREG:
326 /*
327 * Calculate the size of inode data, decrease the used-memory
328 * counter, and destroy the unerlying UVM object (if any).
329 */
330 objsz = PAGE_SIZE * node->tn_spec.tn_reg.tn_aobj_pages;
331 if (objsz != 0) {
332 tmpfs_mem_decr(tmp, objsz);
333 }
334 if (node->tn_spec.tn_reg.tn_aobj != NULL) {
335 uao_detach(node->tn_spec.tn_reg.tn_aobj);
336 }
337 break;
338 case VDIR:
339 KASSERT(node->tn_size == 0);
340 KASSERT(node->tn_spec.tn_dir.tn_seq_arena == NULL);
341 KASSERT(TAILQ_EMPTY(&node->tn_spec.tn_dir.tn_dir));
342 KASSERT(node->tn_spec.tn_dir.tn_parent == NULL ||
343 node == tmp->tm_root);
344 break;
345 default:
346 break;
347 }
348 KASSERT(node->tn_vnode == NULL);
349 KASSERT(node->tn_links == 0);
350
351 tmpfs_node_put(tmp, node);
352}
353
354/*
355 * tmpfs_construct_node: allocate a new file of specified type and adds it
356 * into the parent directory.
357 *
358 * => Credentials of the caller are used.
359 */
360int
361tmpfs_construct_node(vnode_t *dvp, vnode_t **vpp, struct vattr *vap,
362 struct componentname *cnp, char *target)
363{
364 tmpfs_mount_t *tmp = VFS_TO_TMPFS(dvp->v_mount);
365 tmpfs_node_t *dnode = VP_TO_TMPFS_DIR(dvp), *node;
366 tmpfs_dirent_t *de, *wde;
367 char *slink = NULL;
368 int ssize = 0;
369 int error;
370
371 /* Allocate symlink target. */
372 if (target != NULL) {
373 KASSERT(vap->va_type == VLNK);
374 ssize = strlen(target);
375 KASSERT(ssize < MAXPATHLEN);
376 if (ssize > 0) {
377 slink = tmpfs_strname_alloc(tmp, ssize);
378 if (slink == NULL)
379 return ENOSPC;
380 memcpy(slink, target, ssize);
381 }
382 }
383
384 /* Allocate a directory entry that points to the new file. */
385 error = tmpfs_alloc_dirent(tmp, cnp->cn_nameptr, cnp->cn_namelen, &de);
386 if (error) {
387 if (slink != NULL)
388 tmpfs_strname_free(tmp, slink, ssize);
389 return error;
390 }
391
392 /* Allocate a vnode that represents the new file. */
393 error = vcache_new(dvp->v_mount, dvp, vap, cnp->cn_cred, vpp);
394 if (error) {
395 if (slink != NULL)
396 tmpfs_strname_free(tmp, slink, ssize);
397 tmpfs_free_dirent(tmp, de);
398 return error;
399 }
400 error = vn_lock(*vpp, LK_EXCLUSIVE);
401 if (error) {
402 vrele(*vpp);
403 *vpp = NULL;
404 if (slink != NULL)
405 tmpfs_strname_free(tmp, slink, ssize);
406 tmpfs_free_dirent(tmp, de);
407 return error;
408 }
409
410 node = VP_TO_TMPFS_NODE(*vpp);
411
412 if (slink != NULL) {
413 node->tn_spec.tn_lnk.tn_link = slink;
414 node->tn_size = ssize;
415 }
416
417 /* Remove whiteout before adding the new entry. */
418 if (cnp->cn_flags & ISWHITEOUT) {
419 wde = tmpfs_dir_lookup(dnode, cnp);
420 KASSERT(wde != NULL && wde->td_node == TMPFS_NODE_WHITEOUT);
421 tmpfs_dir_detach(dnode, wde);
422 tmpfs_free_dirent(tmp, wde);
423 }
424
425 /* Associate inode and attach the entry into the directory. */
426 tmpfs_dir_attach(dnode, de, node);
427
428 /* Make node opaque if requested. */
429 if (cnp->cn_flags & ISWHITEOUT)
430 node->tn_flags |= UF_OPAQUE;
431
432 /* Update the parent's timestamps. */
433 tmpfs_update(dvp, TMPFS_UPDATE_MTIME | TMPFS_UPDATE_CTIME);
434
435 VOP_UNLOCK(*vpp);
436
437 return 0;
438}
439
440/*
441 * tmpfs_alloc_dirent: allocates a new directory entry for the inode.
442 * The directory entry contains a path name component.
443 */
444int
445tmpfs_alloc_dirent(tmpfs_mount_t *tmp, const char *name, uint16_t len,
446 tmpfs_dirent_t **de)
447{
448 tmpfs_dirent_t *nde;
449
450 nde = tmpfs_dirent_get(tmp);
451 if (nde == NULL)
452 return ENOSPC;
453
454 nde->td_name = tmpfs_strname_alloc(tmp, len);
455 if (nde->td_name == NULL) {
456 tmpfs_dirent_put(tmp, nde);
457 return ENOSPC;
458 }
459 nde->td_namelen = len;
460 memcpy(nde->td_name, name, len);
461 nde->td_seq = TMPFS_DIRSEQ_NONE;
462 nde->td_node = NULL; /* for asserts */
463
464 *de = nde;
465 return 0;
466}
467
468/*
469 * tmpfs_free_dirent: free a directory entry.
470 */
471void
472tmpfs_free_dirent(tmpfs_mount_t *tmp, tmpfs_dirent_t *de)
473{
474 KASSERT(de->td_node == NULL);
475 KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
476 tmpfs_strname_free(tmp, de->td_name, de->td_namelen);
477 tmpfs_dirent_put(tmp, de);
478}
479
480/*
481 * tmpfs_dir_attach: associate directory entry with a specified inode,
482 * and attach the entry into the directory, specified by vnode.
483 *
484 * => Increases link count on the associated node.
485 * => Increases link count on directory node if our node is VDIR.
486 * => It is caller's responsibility to check for the LINK_MAX limit.
487 * => Triggers kqueue events here.
488 */
489void
490tmpfs_dir_attach(tmpfs_node_t *dnode, tmpfs_dirent_t *de, tmpfs_node_t *node)
491{
492 vnode_t *dvp = dnode->tn_vnode;
493 int events = NOTE_WRITE;
494
495 KASSERT(dvp != NULL);
496 KASSERT(VOP_ISLOCKED(dvp));
497
498 /* Get a new sequence number. */
499 KASSERT(de->td_seq == TMPFS_DIRSEQ_NONE);
500 de->td_seq = tmpfs_dir_getseq(dnode, de);
501
502 /* Associate directory entry and the inode. */
503 de->td_node = node;
504 if (node != TMPFS_NODE_WHITEOUT) {
505 KASSERT(node->tn_links < LINK_MAX);
506 node->tn_links++;
507
508 /* Save the hint (might overwrite). */
509 node->tn_dirent_hint = de;
510 } else if ((dnode->tn_gen & TMPFS_WHITEOUT_BIT) == 0) {
511 /* Flag that there are whiteout entries. */
512 atomic_or_32(&dnode->tn_gen, TMPFS_WHITEOUT_BIT);
513 }
514
515 /* Insert the entry to the directory (parent of inode). */
516 TAILQ_INSERT_TAIL(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
517 dnode->tn_size += sizeof(tmpfs_dirent_t);
518 uvm_vnp_setsize(dvp, dnode->tn_size);
519
520 if (node != TMPFS_NODE_WHITEOUT && node->tn_type == VDIR) {
521 /* Set parent. */
522 KASSERT(node->tn_spec.tn_dir.tn_parent == NULL);
523 node->tn_spec.tn_dir.tn_parent = dnode;
524
525 /* Increase the link count of parent. */
526 KASSERT(dnode->tn_links < LINK_MAX);
527 dnode->tn_links++;
528 events |= NOTE_LINK;
529
530 TMPFS_VALIDATE_DIR(node);
531 }
532 VN_KNOTE(dvp, events);
533}
534
535/*
536 * tmpfs_dir_detach: disassociate directory entry and its inode,
537 * and detach the entry from the directory, specified by vnode.
538 *
539 * => Decreases link count on the associated node.
540 * => Decreases the link count on directory node, if our node is VDIR.
541 * => Triggers kqueue events here.
542 *
543 * => Note: dvp and vp may be NULL only if called by tmpfs_unmount().
544 */
545void
546tmpfs_dir_detach(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
547{
548 tmpfs_node_t *node = de->td_node;
549 vnode_t *vp, *dvp = dnode->tn_vnode;
550 int events = NOTE_WRITE;
551
552 KASSERT(dvp == NULL || VOP_ISLOCKED(dvp));
553
554 if (__predict_true(node != TMPFS_NODE_WHITEOUT)) {
555 /* Deassociate the inode and entry. */
556 node->tn_dirent_hint = NULL;
557
558 KASSERT(node->tn_links > 0);
559 node->tn_links--;
560
561 if ((vp = node->tn_vnode) != NULL) {
562 KASSERT(VOP_ISLOCKED(vp));
563 VN_KNOTE(vp, node->tn_links ? NOTE_LINK : NOTE_DELETE);
564 }
565
566 /* If directory - decrease the link count of parent. */
567 if (node->tn_type == VDIR) {
568 KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
569 node->tn_spec.tn_dir.tn_parent = NULL;
570
571 KASSERT(dnode->tn_links > 0);
572 dnode->tn_links--;
573 events |= NOTE_LINK;
574 }
575 }
576 de->td_node = NULL;
577
578 /* Remove the entry from the directory. */
579 if (dnode->tn_spec.tn_dir.tn_readdir_lastp == de) {
580 dnode->tn_spec.tn_dir.tn_readdir_lastp = NULL;
581 }
582 TAILQ_REMOVE(&dnode->tn_spec.tn_dir.tn_dir, de, td_entries);
583 dnode->tn_size -= sizeof(tmpfs_dirent_t);
584 tmpfs_dir_putseq(dnode, de);
585
586 if (dvp) {
587 uvm_vnp_setsize(dvp, dnode->tn_size);
588 VN_KNOTE(dvp, events);
589 }
590}
591
592/*
593 * tmpfs_dir_lookup: find a directory entry in the specified inode.
594 *
595 * Note that the . and .. components are not allowed as they do not
596 * physically exist within directories.
597 */
598tmpfs_dirent_t *
599tmpfs_dir_lookup(tmpfs_node_t *node, struct componentname *cnp)
600{
601 const char *name = cnp->cn_nameptr;
602 const uint16_t nlen = cnp->cn_namelen;
603 tmpfs_dirent_t *de;
604
605 KASSERT(VOP_ISLOCKED(node->tn_vnode));
606 KASSERT(nlen != 1 || !(name[0] == '.'));
607 KASSERT(nlen != 2 || !(name[0] == '.' && name[1] == '.'));
608 TMPFS_VALIDATE_DIR(node);
609
610 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
611 if (de->td_namelen != nlen)
612 continue;
613 if (memcmp(de->td_name, name, nlen) != 0)
614 continue;
615 break;
616 }
617 return de;
618}
619
620/*
621 * tmpfs_dir_cached: get a cached directory entry if it is valid. Used to
622 * avoid unnecessary tmpfs_dir_lookup().
623 *
624 * => The vnode must be locked.
625 */
626tmpfs_dirent_t *
627tmpfs_dir_cached(tmpfs_node_t *node)
628{
629 tmpfs_dirent_t *de = node->tn_dirent_hint;
630
631 KASSERT(VOP_ISLOCKED(node->tn_vnode));
632
633 if (de == NULL) {
634 return NULL;
635 }
636 KASSERT(de->td_node == node);
637
638 /*
639 * Directories always have a valid hint. For files, check if there
640 * are any hard links. If there are - hint might be invalid.
641 */
642 return (node->tn_type != VDIR && node->tn_links > 1) ? NULL : de;
643}
644
645/*
646 * tmpfs_dir_getseq: get a per-directory sequence number for the entry.
647 *
648 * => Shall not be larger than 2^31 for linux32 compatibility.
649 */
650uint32_t
651tmpfs_dir_getseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
652{
653 uint32_t seq = de->td_seq;
654 vmem_t *seq_arena;
655 vmem_addr_t off;
656 int error __diagused;
657
658 TMPFS_VALIDATE_DIR(dnode);
659
660 if (__predict_true(seq != TMPFS_DIRSEQ_NONE)) {
661 /* Already set. */
662 KASSERT(seq >= TMPFS_DIRSEQ_START);
663 return seq;
664 }
665
666 /*
667 * The "." and ".." and the end-of-directory have reserved numbers.
668 * The other sequence numbers are allocated as following:
669 *
670 * - The first half of the 2^31 is assigned incrementally.
671 *
672 * - If that range is exceeded, then the second half of 2^31
673 * is used, but managed by vmem(9).
674 */
675
676 seq = dnode->tn_spec.tn_dir.tn_next_seq;
677 KASSERT(seq >= TMPFS_DIRSEQ_START);
678
679 if (__predict_true(seq < TMPFS_DIRSEQ_END)) {
680 /* First half: just increment and return. */
681 dnode->tn_spec.tn_dir.tn_next_seq++;
682 return seq;
683 }
684
685 /*
686 * First half exceeded, use the second half. May need to create
687 * vmem(9) arena for the directory first.
688 */
689 if ((seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena) == NULL) {
690 seq_arena = vmem_create("tmpfscoo", 0,
691 TMPFS_DIRSEQ_END - 1, 1, NULL, NULL, NULL, 0,
692 VM_SLEEP, IPL_NONE);
693 dnode->tn_spec.tn_dir.tn_seq_arena = seq_arena;
694 KASSERT(seq_arena != NULL);
695 }
696 error = vmem_alloc(seq_arena, 1, VM_SLEEP | VM_BESTFIT, &off);
697 KASSERT(error == 0);
698
699 KASSERT(off < TMPFS_DIRSEQ_END);
700 seq = off | TMPFS_DIRSEQ_END;
701 return seq;
702}
703
704static void
705tmpfs_dir_putseq(tmpfs_node_t *dnode, tmpfs_dirent_t *de)
706{
707 vmem_t *seq_arena = dnode->tn_spec.tn_dir.tn_seq_arena;
708 uint32_t seq = de->td_seq;
709
710 TMPFS_VALIDATE_DIR(dnode);
711
712 if (seq == TMPFS_DIRSEQ_NONE || seq < TMPFS_DIRSEQ_END) {
713 /* First half (or no sequence number set yet). */
714 KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
715 } else {
716 /* Second half. */
717 KASSERT(seq_arena != NULL);
718 KASSERT(seq >= TMPFS_DIRSEQ_END);
719 seq &= ~TMPFS_DIRSEQ_END;
720 vmem_free(seq_arena, seq, 1);
721 }
722 de->td_seq = TMPFS_DIRSEQ_NONE;
723
724 /* Empty? We can reset. */
725 if (seq_arena && dnode->tn_size == 0) {
726 dnode->tn_spec.tn_dir.tn_seq_arena = NULL;
727 dnode->tn_spec.tn_dir.tn_next_seq = TMPFS_DIRSEQ_START;
728 vmem_destroy(seq_arena);
729 }
730}
731
732/*
733 * tmpfs_dir_lookupbyseq: lookup a directory entry by the sequence number.
734 */
735tmpfs_dirent_t *
736tmpfs_dir_lookupbyseq(tmpfs_node_t *node, off_t seq)
737{
738 tmpfs_dirent_t *de = node->tn_spec.tn_dir.tn_readdir_lastp;
739
740 TMPFS_VALIDATE_DIR(node);
741
742 /*
743 * First, check the cache. If does not match - perform a lookup.
744 */
745 if (de && de->td_seq == seq) {
746 KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
747 KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
748 return de;
749 }
750 TAILQ_FOREACH(de, &node->tn_spec.tn_dir.tn_dir, td_entries) {
751 KASSERT(de->td_seq >= TMPFS_DIRSEQ_START);
752 KASSERT(de->td_seq != TMPFS_DIRSEQ_NONE);
753 if (de->td_seq == seq)
754 return de;
755 }
756 return NULL;
757}
758
759/*
760 * tmpfs_dir_getdotents: helper function for tmpfs_readdir() to get the
761 * dot meta entries, that is, "." or "..". Copy it to the UIO space.
762 */
763static int
764tmpfs_dir_getdotents(tmpfs_node_t *node, struct dirent *dp, struct uio *uio)
765{
766 tmpfs_dirent_t *de;
767 off_t next = 0;
768 int error;
769
770 switch (uio->uio_offset) {
771 case TMPFS_DIRSEQ_DOT:
772 dp->d_fileno = node->tn_id;
773 strlcpy(dp->d_name, ".", sizeof(dp->d_name));
774 next = TMPFS_DIRSEQ_DOTDOT;
775 break;
776 case TMPFS_DIRSEQ_DOTDOT:
777 dp->d_fileno = node->tn_spec.tn_dir.tn_parent->tn_id;
778 strlcpy(dp->d_name, "..", sizeof(dp->d_name));
779 de = TAILQ_FIRST(&node->tn_spec.tn_dir.tn_dir);
780 next = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
781 break;
782 default:
783 KASSERT(false);
784 }
785 dp->d_type = DT_DIR;
786 dp->d_namlen = strlen(dp->d_name);
787 dp->d_reclen = _DIRENT_SIZE(dp);
788
789 if (dp->d_reclen > uio->uio_resid) {
790 return EJUSTRETURN;
791 }
792 if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
793 return error;
794 }
795
796 uio->uio_offset = next;
797 return error;
798}
799
800/*
801 * tmpfs_dir_getdents: helper function for tmpfs_readdir.
802 *
803 * => Returns as much directory entries as can fit in the uio space.
804 * => The read starts at uio->uio_offset.
805 */
806int
807tmpfs_dir_getdents(tmpfs_node_t *node, struct uio *uio, off_t *cntp)
808{
809 tmpfs_dirent_t *de;
810 struct dirent dent;
811 int error = 0;
812
813 KASSERT(VOP_ISLOCKED(node->tn_vnode));
814 TMPFS_VALIDATE_DIR(node);
815
816 /*
817 * First check for the "." and ".." cases.
818 * Note: tmpfs_dir_getdotents() will "seek" for us.
819 */
820 memset(&dent, 0, sizeof(dent));
821
822 if (uio->uio_offset == TMPFS_DIRSEQ_DOT) {
823 if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) {
824 goto done;
825 }
826 (*cntp)++;
827 }
828 if (uio->uio_offset == TMPFS_DIRSEQ_DOTDOT) {
829 if ((error = tmpfs_dir_getdotents(node, &dent, uio)) != 0) {
830 goto done;
831 }
832 (*cntp)++;
833 }
834
835 /* Done if we reached the end. */
836 if (uio->uio_offset == TMPFS_DIRSEQ_EOF) {
837 goto done;
838 }
839
840 /* Locate the directory entry given by the given sequence number. */
841 de = tmpfs_dir_lookupbyseq(node, uio->uio_offset);
842 if (de == NULL) {
843 error = EINVAL;
844 goto done;
845 }
846
847 /*
848 * Read as many entries as possible; i.e., until we reach the end
849 * of the directory or we exhaust UIO space.
850 */
851 do {
852 if (de->td_node == TMPFS_NODE_WHITEOUT) {
853 dent.d_fileno = 1;
854 dent.d_type = DT_WHT;
855 } else {
856 dent.d_fileno = de->td_node->tn_id;
857 dent.d_type = vtype2dt(de->td_node->tn_type);
858 }
859 dent.d_namlen = de->td_namelen;
860 KASSERT(de->td_namelen < sizeof(dent.d_name));
861 memcpy(dent.d_name, de->td_name, de->td_namelen);
862 dent.d_name[de->td_namelen] = '\0';
863 dent.d_reclen = _DIRENT_SIZE(&dent);
864
865 if (dent.d_reclen > uio->uio_resid) {
866 /* Exhausted UIO space. */
867 error = EJUSTRETURN;
868 break;
869 }
870
871 /* Copy out the directory entry and continue. */
872 error = uiomove(&dent, dent.d_reclen, uio);
873 if (error) {
874 break;
875 }
876 (*cntp)++;
877 de = TAILQ_NEXT(de, td_entries);
878
879 } while (uio->uio_resid > 0 && de);
880
881 /* Cache the last entry or clear and mark EOF. */
882 uio->uio_offset = de ? tmpfs_dir_getseq(node, de) : TMPFS_DIRSEQ_EOF;
883 node->tn_spec.tn_dir.tn_readdir_lastp = de;
884done:
885 tmpfs_update(node->tn_vnode, TMPFS_UPDATE_ATIME);
886
887 if (error == EJUSTRETURN) {
888 /* Exhausted UIO space - just return. */
889 error = 0;
890 }
891 KASSERT(error >= 0);
892 return error;
893}
894
895/*
896 * tmpfs_reg_resize: resize the underlying UVM object associated with the
897 * specified regular file.
898 */
899int
900tmpfs_reg_resize(struct vnode *vp, off_t newsize)
901{
902 tmpfs_mount_t *tmp = VFS_TO_TMPFS(vp->v_mount);
903 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
904 struct uvm_object *uobj = node->tn_spec.tn_reg.tn_aobj;
905 size_t newpages, oldpages;
906 off_t oldsize;
907
908 KASSERT(vp->v_type == VREG);
909 KASSERT(newsize >= 0);
910
911 oldsize = node->tn_size;
912 oldpages = round_page(oldsize) >> PAGE_SHIFT;
913 newpages = round_page(newsize) >> PAGE_SHIFT;
914 KASSERT(oldpages == node->tn_spec.tn_reg.tn_aobj_pages);
915
916 if (newpages > oldpages) {
917 /* Increase the used-memory counter if getting extra pages. */
918 if (!tmpfs_mem_incr(tmp, (newpages - oldpages) << PAGE_SHIFT)) {
919 return ENOSPC;
920 }
921 } else if (newsize < oldsize) {
922 size_t zerolen;
923
924 zerolen = MIN(round_page(newsize), node->tn_size) - newsize;
925 ubc_zerorange(uobj, newsize, zerolen, UBC_UNMAP_FLAG(vp));
926 }
927
928 node->tn_spec.tn_reg.tn_aobj_pages = newpages;
929 node->tn_size = newsize;
930 uvm_vnp_setsize(vp, newsize);
931
932 /*
933 * Free "backing store".
934 */
935 if (newpages < oldpages) {
936 KASSERT(uobj->vmobjlock == vp->v_interlock);
937
938 mutex_enter(uobj->vmobjlock);
939 uao_dropswap_range(uobj, newpages, oldpages);
940 mutex_exit(uobj->vmobjlock);
941
942 /* Decrease the used-memory counter. */
943 tmpfs_mem_decr(tmp, (oldpages - newpages) << PAGE_SHIFT);
944 }
945 if (newsize > oldsize) {
946 VN_KNOTE(vp, NOTE_EXTEND);
947 }
948 return 0;
949}
950
951/*
952 * tmpfs_chflags: change flags of the given vnode.
953 */
954int
955tmpfs_chflags(vnode_t *vp, int flags, kauth_cred_t cred, lwp_t *l)
956{
957 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
958 kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS;
959 int error;
960 bool changing_sysflags = false;
961
962 KASSERT(VOP_ISLOCKED(vp));
963
964 /* Disallow this operation if the file system is mounted read-only. */
965 if (vp->v_mount->mnt_flag & MNT_RDONLY)
966 return EROFS;
967
968 /*
969 * If the new flags have non-user flags that are different than
970 * those on the node, we need special permission to change them.
971 */
972 if ((flags & SF_SETTABLE) != (node->tn_flags & SF_SETTABLE)) {
973 action |= KAUTH_VNODE_WRITE_SYSFLAGS;
974 changing_sysflags = true;
975 }
976
977 /*
978 * Indicate that this node's flags have system attributes in them if
979 * that's the case.
980 */
981 if (node->tn_flags & (SF_IMMUTABLE | SF_APPEND)) {
982 action |= KAUTH_VNODE_HAS_SYSFLAGS;
983 }
984
985 error = kauth_authorize_vnode(cred, action, vp, NULL,
986 genfs_can_chflags(cred, vp->v_type, node->tn_uid,
987 changing_sysflags));
988 if (error)
989 return error;
990
991 /*
992 * Set the flags. If we're not setting non-user flags, be careful not
993 * to overwrite them.
994 *
995 * XXX: Can't we always assign here? if the system flags are different,
996 * the code above should catch attempts to change them without
997 * proper permissions, and if we're here it means it's okay to
998 * change them...
999 */
1000 if (!changing_sysflags) {
1001 /* Clear all user-settable flags and re-set them. */
1002 node->tn_flags &= SF_SETTABLE;
1003 node->tn_flags |= (flags & UF_SETTABLE);
1004 } else {
1005 node->tn_flags = flags;
1006 }
1007 tmpfs_update(vp, TMPFS_UPDATE_CTIME);
1008 VN_KNOTE(vp, NOTE_ATTRIB);
1009 return 0;
1010}
1011
1012/*
1013 * tmpfs_chmod: change access mode on the given vnode.
1014 */
1015int
1016tmpfs_chmod(vnode_t *vp, mode_t mode, kauth_cred_t cred, lwp_t *l)
1017{
1018 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1019 int error;
1020
1021 KASSERT(VOP_ISLOCKED(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 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
1032 NULL, genfs_can_chmod(vp->v_type, cred, node->tn_uid, node->tn_gid, mode));
1033 if (error) {
1034 return error;
1035 }
1036 node->tn_mode = (mode & ALLPERMS);
1037 tmpfs_update(vp, TMPFS_UPDATE_CTIME);
1038 VN_KNOTE(vp, NOTE_ATTRIB);
1039 return 0;
1040}
1041
1042/*
1043 * tmpfs_chown: change ownership of the given vnode.
1044 *
1045 * => At least one of uid or gid must be different than VNOVAL.
1046 * => Attribute is unchanged for VNOVAL case.
1047 */
1048int
1049tmpfs_chown(vnode_t *vp, uid_t uid, gid_t gid, kauth_cred_t cred, lwp_t *l)
1050{
1051 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1052 int error;
1053
1054 KASSERT(VOP_ISLOCKED(vp));
1055
1056 /* Assign default values if they are unknown. */
1057 KASSERT(uid != VNOVAL || gid != VNOVAL);
1058 if (uid == VNOVAL) {
1059 uid = node->tn_uid;
1060 }
1061 if (gid == VNOVAL) {
1062 gid = node->tn_gid;
1063 }
1064
1065 /* Disallow this operation if the file system is mounted read-only. */
1066 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1067 return EROFS;
1068
1069 /* Immutable or append-only files cannot be modified, either. */
1070 if (node->tn_flags & (IMMUTABLE | APPEND))
1071 return EPERM;
1072
1073 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
1074 NULL, genfs_can_chown(cred, node->tn_uid, node->tn_gid, uid,
1075 gid));
1076 if (error) {
1077 return error;
1078 }
1079 node->tn_uid = uid;
1080 node->tn_gid = gid;
1081 tmpfs_update(vp, TMPFS_UPDATE_CTIME);
1082 VN_KNOTE(vp, NOTE_ATTRIB);
1083 return 0;
1084}
1085
1086/*
1087 * tmpfs_chsize: change size of the given vnode.
1088 */
1089int
1090tmpfs_chsize(vnode_t *vp, u_quad_t size, kauth_cred_t cred, lwp_t *l)
1091{
1092 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1093 const off_t length = size;
1094 int error;
1095
1096 KASSERT(VOP_ISLOCKED(vp));
1097
1098 /* Decide whether this is a valid operation based on the file type. */
1099 switch (vp->v_type) {
1100 case VDIR:
1101 return EISDIR;
1102 case VREG:
1103 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1104 return EROFS;
1105 }
1106 break;
1107 case VBLK:
1108 case VCHR:
1109 case VFIFO:
1110 /*
1111 * Allow modifications of special files even if in the file
1112 * system is mounted read-only (we are not modifying the
1113 * files themselves, but the objects they represent).
1114 */
1115 return 0;
1116 default:
1117 return EOPNOTSUPP;
1118 }
1119
1120 /* Immutable or append-only files cannot be modified, either. */
1121 if (node->tn_flags & (IMMUTABLE | APPEND)) {
1122 return EPERM;
1123 }
1124
1125 if (length < 0) {
1126 return EINVAL;
1127 }
1128 if (node->tn_size == length) {
1129 return 0;
1130 }
1131
1132 /* Note: tmpfs_reg_resize() will raise NOTE_EXTEND and NOTE_ATTRIB. */
1133 if ((error = tmpfs_reg_resize(vp, length)) != 0) {
1134 return error;
1135 }
1136 tmpfs_update(vp, TMPFS_UPDATE_CTIME | TMPFS_UPDATE_MTIME);
1137 return 0;
1138}
1139
1140/*
1141 * tmpfs_chtimes: change access and modification times for vnode.
1142 */
1143int
1144tmpfs_chtimes(vnode_t *vp, const struct timespec *atime,
1145 const struct timespec *mtime, const struct timespec *btime,
1146 int vaflags, kauth_cred_t cred, lwp_t *l)
1147{
1148 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1149 int error;
1150
1151 KASSERT(VOP_ISLOCKED(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 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, NULL,
1162 genfs_can_chtimes(vp, vaflags, node->tn_uid, cred));
1163 if (error)
1164 return error;
1165
1166 if (atime->tv_sec != VNOVAL) {
1167 node->tn_atime = *atime;
1168 }
1169 if (mtime->tv_sec != VNOVAL) {
1170 node->tn_mtime = *mtime;
1171 }
1172 if (btime->tv_sec != VNOVAL) {
1173 node->tn_birthtime = *btime;
1174 }
1175 VN_KNOTE(vp, NOTE_ATTRIB);
1176 return 0;
1177}
1178
1179/*
1180 * tmpfs_update: update the timestamps as indicated by the flags.
1181 */
1182void
1183tmpfs_update(vnode_t *vp, unsigned tflags)
1184{
1185 tmpfs_node_t *node = VP_TO_TMPFS_NODE(vp);
1186 struct timespec nowtm;
1187
1188 if (tflags == 0) {
1189 return;
1190 }
1191 vfs_timestamp(&nowtm);
1192
1193 if (tflags & TMPFS_UPDATE_ATIME) {
1194 node->tn_atime = nowtm;
1195 }
1196 if (tflags & TMPFS_UPDATE_MTIME) {
1197 node->tn_mtime = nowtm;
1198 }
1199 if (tflags & TMPFS_UPDATE_CTIME) {
1200 node->tn_ctime = nowtm;
1201 }
1202}
1203