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