Deleted Added
sdiff udiff text old ( 313094 ) new ( 313095 )
full compact
1/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
2
3/*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
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 * tmpfs vnode interface.
35 */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: stable/10/sys/fs/tmpfs/tmpfs_vnops.c 313094 2017-02-02 13:38:01Z kib $");
38
39#include <sys/param.h>
40#include <sys/fcntl.h>
41#include <sys/lockf.h>
42#include <sys/lock.h>
43#include <sys/namei.h>
44#include <sys/priv.h>
45#include <sys/proc.h>
46#include <sys/rwlock.h>
47#include <sys/sched.h>
48#include <sys/stat.h>
49#include <sys/systm.h>
50#include <sys/sysctl.h>
51#include <sys/unistd.h>
52#include <sys/vnode.h>
53
54#include <vm/vm.h>
55#include <vm/vm_param.h>
56#include <vm/vm_object.h>
57#include <vm/vm_page.h>
58#include <vm/vm_pager.h>
59
60#include <fs/tmpfs/tmpfs_vnops.h>
61#include <fs/tmpfs/tmpfs.h>
62
63SYSCTL_DECL(_vfs_tmpfs);
64
65static volatile int tmpfs_rename_restarts;
66SYSCTL_INT(_vfs_tmpfs, OID_AUTO, rename_restarts, CTLFLAG_RD,
67 __DEVOLATILE(int *, &tmpfs_rename_restarts), 0,
68 "Times rename had to restart due to lock contention");
69
70static int
71tmpfs_vn_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
72 struct vnode **rvp)
73{
74
75 return (tmpfs_alloc_vp(mp, arg, lkflags, rvp));
76}
77
78static int
79tmpfs_lookup(struct vop_cachedlookup_args *v)
80{
81 struct vnode *dvp = v->a_dvp;
82 struct vnode **vpp = v->a_vpp;
83 struct componentname *cnp = v->a_cnp;
84 struct tmpfs_dirent *de;
85 struct tmpfs_node *dnode, *pnode;
86 struct tmpfs_mount *tm;
87 int error;
88
89 dnode = VP_TO_TMPFS_DIR(dvp);
90 *vpp = NULLVP;
91
92 /* Check accessibility of requested node as a first step. */
93 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
94 if (error != 0)
95 goto out;
96
97 /* We cannot be requesting the parent directory of the root node. */
98 MPASS(IMPLIES(dnode->tn_type == VDIR &&
99 dnode->tn_dir.tn_parent == dnode,
100 !(cnp->cn_flags & ISDOTDOT)));
101
102 TMPFS_ASSERT_LOCKED(dnode);
103 if (dnode->tn_dir.tn_parent == NULL) {
104 error = ENOENT;
105 goto out;
106 }
107 if (cnp->cn_flags & ISDOTDOT) {
108 tm = VFS_TO_TMPFS(dvp->v_mount);
109 pnode = dnode->tn_dir.tn_parent;
110 tmpfs_ref_node(pnode);
111 error = vn_vget_ino_gen(dvp, tmpfs_vn_get_ino_alloc,
112 pnode, cnp->cn_lkflags, vpp);
113 tmpfs_free_node(tm, pnode);
114 if (error != 0)
115 goto out;
116 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
117 VREF(dvp);
118 *vpp = dvp;
119 error = 0;
120 } else {
121 de = tmpfs_dir_lookup(dnode, NULL, cnp);
122 if (de != NULL && de->td_node == NULL)
123 cnp->cn_flags |= ISWHITEOUT;
124 if (de == NULL || de->td_node == NULL) {
125 /*
126 * The entry was not found in the directory.
127 * This is OK if we are creating or renaming an
128 * entry and are working on the last component of
129 * the path name.
130 */
131 if ((cnp->cn_flags & ISLASTCN) &&
132 (cnp->cn_nameiop == CREATE || \
133 cnp->cn_nameiop == RENAME ||
134 (cnp->cn_nameiop == DELETE &&
135 cnp->cn_flags & DOWHITEOUT &&
136 cnp->cn_flags & ISWHITEOUT))) {
137 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
138 cnp->cn_thread);
139 if (error != 0)
140 goto out;
141
142 /*
143 * Keep the component name in the buffer for
144 * future uses.
145 */
146 cnp->cn_flags |= SAVENAME;
147
148 error = EJUSTRETURN;
149 } else
150 error = ENOENT;
151 } else {
152 struct tmpfs_node *tnode;
153
154 /*
155 * The entry was found, so get its associated
156 * tmpfs_node.
157 */
158 tnode = de->td_node;
159
160 /*
161 * If we are not at the last path component and
162 * found a non-directory or non-link entry (which
163 * may itself be pointing to a directory), raise
164 * an error.
165 */
166 if ((tnode->tn_type != VDIR &&
167 tnode->tn_type != VLNK) &&
168 !(cnp->cn_flags & ISLASTCN)) {
169 error = ENOTDIR;
170 goto out;
171 }
172
173 /*
174 * If we are deleting or renaming the entry, keep
175 * track of its tmpfs_dirent so that it can be
176 * easily deleted later.
177 */
178 if ((cnp->cn_flags & ISLASTCN) &&
179 (cnp->cn_nameiop == DELETE ||
180 cnp->cn_nameiop == RENAME)) {
181 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred,
182 cnp->cn_thread);
183 if (error != 0)
184 goto out;
185
186 /* Allocate a new vnode on the matching entry. */
187 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
188 cnp->cn_lkflags, vpp);
189 if (error != 0)
190 goto out;
191
192 if ((dnode->tn_mode & S_ISTXT) &&
193 VOP_ACCESS(dvp, VADMIN, cnp->cn_cred,
194 cnp->cn_thread) && VOP_ACCESS(*vpp, VADMIN,
195 cnp->cn_cred, cnp->cn_thread)) {
196 error = EPERM;
197 vput(*vpp);
198 *vpp = NULL;
199 goto out;
200 }
201 cnp->cn_flags |= SAVENAME;
202 } else {
203 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
204 cnp->cn_lkflags, vpp);
205 if (error != 0)
206 goto out;
207 }
208 }
209 }
210
211 /*
212 * Store the result of this lookup in the cache. Avoid this if the
213 * request was for creation, as it does not improve timings on
214 * emprical tests.
215 */
216 if ((cnp->cn_flags & MAKEENTRY) != 0)
217 cache_enter(dvp, *vpp, cnp);
218
219out:
220 /*
221 * If there were no errors, *vpp cannot be null and it must be
222 * locked.
223 */
224 MPASS(IFF(error == 0, *vpp != NULLVP && VOP_ISLOCKED(*vpp)));
225
226 return (error);
227}
228
229static int
230tmpfs_create(struct vop_create_args *v)
231{
232 struct vnode *dvp = v->a_dvp;
233 struct vnode **vpp = v->a_vpp;
234 struct componentname *cnp = v->a_cnp;
235 struct vattr *vap = v->a_vap;
236 int error;
237
238 MPASS(vap->va_type == VREG || vap->va_type == VSOCK);
239
240 error = tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
241 if (error == 0 && (cnp->cn_flags & MAKEENTRY) != 0)
242 cache_enter(dvp, *vpp, cnp);
243 return (error);
244}
245
246static int
247tmpfs_mknod(struct vop_mknod_args *v)
248{
249 struct vnode *dvp = v->a_dvp;
250 struct vnode **vpp = v->a_vpp;
251 struct componentname *cnp = v->a_cnp;
252 struct vattr *vap = v->a_vap;
253
254 if (vap->va_type != VBLK && vap->va_type != VCHR &&
255 vap->va_type != VFIFO)
256 return EINVAL;
257
258 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
259}
260
261static int
262tmpfs_open(struct vop_open_args *v)
263{
264 struct vnode *vp = v->a_vp;
265 int mode = v->a_mode;
266
267 int error;
268 struct tmpfs_node *node;
269
270 MPASS(VOP_ISLOCKED(vp));
271
272 node = VP_TO_TMPFS_NODE(vp);
273
274 /* The file is still active but all its names have been removed
275 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
276 * it is about to die. */
277 if (node->tn_links < 1)
278 return (ENOENT);
279
280 /* If the file is marked append-only, deny write requests. */
281 if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
282 error = EPERM;
283 else {
284 error = 0;
285 /* For regular files, the call below is nop. */
286 KASSERT(vp->v_type != VREG || (node->tn_reg.tn_aobj->flags &
287 OBJ_DEAD) == 0, ("dead object"));
288 vnode_create_vobject(vp, node->tn_size, v->a_td);
289 }
290
291 MPASS(VOP_ISLOCKED(vp));
292 return error;
293}
294
295static int
296tmpfs_close(struct vop_close_args *v)
297{
298 struct vnode *vp = v->a_vp;
299
300 /* Update node times. */
301 tmpfs_update(vp);
302
303 return (0);
304}
305
306int
307tmpfs_access(struct vop_access_args *v)
308{
309 struct vnode *vp = v->a_vp;
310 accmode_t accmode = v->a_accmode;
311 struct ucred *cred = v->a_cred;
312
313 int error;
314 struct tmpfs_node *node;
315
316 MPASS(VOP_ISLOCKED(vp));
317
318 node = VP_TO_TMPFS_NODE(vp);
319
320 switch (vp->v_type) {
321 case VDIR:
322 /* FALLTHROUGH */
323 case VLNK:
324 /* FALLTHROUGH */
325 case VREG:
326 if (accmode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
327 error = EROFS;
328 goto out;
329 }
330 break;
331
332 case VBLK:
333 /* FALLTHROUGH */
334 case VCHR:
335 /* FALLTHROUGH */
336 case VSOCK:
337 /* FALLTHROUGH */
338 case VFIFO:
339 break;
340
341 default:
342 error = EINVAL;
343 goto out;
344 }
345
346 if (accmode & VWRITE && node->tn_flags & IMMUTABLE) {
347 error = EPERM;
348 goto out;
349 }
350
351 error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
352 node->tn_gid, accmode, cred, NULL);
353
354out:
355 MPASS(VOP_ISLOCKED(vp));
356
357 return error;
358}
359
360int
361tmpfs_getattr(struct vop_getattr_args *v)
362{
363 struct vnode *vp = v->a_vp;
364 struct vattr *vap = v->a_vap;
365
366 struct tmpfs_node *node;
367
368 node = VP_TO_TMPFS_NODE(vp);
369
370 tmpfs_update(vp);
371
372 vap->va_type = vp->v_type;
373 vap->va_mode = node->tn_mode;
374 vap->va_nlink = node->tn_links;
375 vap->va_uid = node->tn_uid;
376 vap->va_gid = node->tn_gid;
377 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
378 vap->va_fileid = node->tn_id;
379 vap->va_size = node->tn_size;
380 vap->va_blocksize = PAGE_SIZE;
381 vap->va_atime = node->tn_atime;
382 vap->va_mtime = node->tn_mtime;
383 vap->va_ctime = node->tn_ctime;
384 vap->va_birthtime = node->tn_birthtime;
385 vap->va_gen = node->tn_gen;
386 vap->va_flags = node->tn_flags;
387 vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
388 node->tn_rdev : NODEV;
389 vap->va_bytes = round_page(node->tn_size);
390 vap->va_filerev = 0;
391
392 return 0;
393}
394
395int
396tmpfs_setattr(struct vop_setattr_args *v)
397{
398 struct vnode *vp = v->a_vp;
399 struct vattr *vap = v->a_vap;
400 struct ucred *cred = v->a_cred;
401 struct thread *td = curthread;
402
403 int error;
404
405 MPASS(VOP_ISLOCKED(vp));
406
407 error = 0;
408
409 /* Abort if any unsettable attribute is given. */
410 if (vap->va_type != VNON ||
411 vap->va_nlink != VNOVAL ||
412 vap->va_fsid != VNOVAL ||
413 vap->va_fileid != VNOVAL ||
414 vap->va_blocksize != VNOVAL ||
415 vap->va_gen != VNOVAL ||
416 vap->va_rdev != VNOVAL ||
417 vap->va_bytes != VNOVAL)
418 error = EINVAL;
419
420 if (error == 0 && (vap->va_flags != VNOVAL))
421 error = tmpfs_chflags(vp, vap->va_flags, cred, td);
422
423 if (error == 0 && (vap->va_size != VNOVAL))
424 error = tmpfs_chsize(vp, vap->va_size, cred, td);
425
426 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
427 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
428
429 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
430 error = tmpfs_chmod(vp, vap->va_mode, cred, td);
431
432 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
433 vap->va_atime.tv_nsec != VNOVAL) ||
434 (vap->va_mtime.tv_sec != VNOVAL &&
435 vap->va_mtime.tv_nsec != VNOVAL) ||
436 (vap->va_birthtime.tv_sec != VNOVAL &&
437 vap->va_birthtime.tv_nsec != VNOVAL)))
438 error = tmpfs_chtimes(vp, vap, cred, td);
439
440 /* Update the node times. We give preference to the error codes
441 * generated by this function rather than the ones that may arise
442 * from tmpfs_update. */
443 tmpfs_update(vp);
444
445 MPASS(VOP_ISLOCKED(vp));
446
447 return error;
448}
449
450static int
451tmpfs_read(struct vop_read_args *v)
452{
453 struct vnode *vp;
454 struct uio *uio;
455 struct tmpfs_node *node;
456
457 vp = v->a_vp;
458 if (vp->v_type != VREG)
459 return (EISDIR);
460 uio = v->a_uio;
461 if (uio->uio_offset < 0)
462 return (EINVAL);
463 node = VP_TO_TMPFS_NODE(vp);
464 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
465 return (uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio));
466}
467
468static int
469tmpfs_write(struct vop_write_args *v)
470{
471 struct vnode *vp;
472 struct uio *uio;
473 struct tmpfs_node *node;
474 off_t oldsize;
475 int error, ioflag;
476
477 vp = v->a_vp;
478 uio = v->a_uio;
479 ioflag = v->a_ioflag;
480 error = 0;
481 node = VP_TO_TMPFS_NODE(vp);
482 oldsize = node->tn_size;
483
484 if (uio->uio_offset < 0 || vp->v_type != VREG)
485 return (EINVAL);
486 if (uio->uio_resid == 0)
487 return (0);
488 if (ioflag & IO_APPEND)
489 uio->uio_offset = node->tn_size;
490 if (uio->uio_offset + uio->uio_resid >
491 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
492 return (EFBIG);
493 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
494 return (EFBIG);
495 if (uio->uio_offset + uio->uio_resid > node->tn_size) {
496 error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid,
497 FALSE);
498 if (error != 0)
499 goto out;
500 }
501
502 error = uiomove_object(node->tn_reg.tn_aobj, node->tn_size, uio);
503 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
504 TMPFS_NODE_CHANGED;
505 if (node->tn_mode & (S_ISUID | S_ISGID)) {
506 if (priv_check_cred(v->a_cred, PRIV_VFS_RETAINSUGID, 0))
507 node->tn_mode &= ~(S_ISUID | S_ISGID);
508 }
509 if (error != 0)
510 (void)tmpfs_reg_resize(vp, oldsize, TRUE);
511
512out:
513 MPASS(IMPLIES(error == 0, uio->uio_resid == 0));
514 MPASS(IMPLIES(error != 0, oldsize == node->tn_size));
515
516 return (error);
517}
518
519static int
520tmpfs_fsync(struct vop_fsync_args *v)
521{
522 struct vnode *vp = v->a_vp;
523
524 MPASS(VOP_ISLOCKED(vp));
525
526 tmpfs_check_mtime(vp);
527 tmpfs_update(vp);
528
529 return 0;
530}
531
532static int
533tmpfs_remove(struct vop_remove_args *v)
534{
535 struct vnode *dvp = v->a_dvp;
536 struct vnode *vp = v->a_vp;
537
538 int error;
539 struct tmpfs_dirent *de;
540 struct tmpfs_mount *tmp;
541 struct tmpfs_node *dnode;
542 struct tmpfs_node *node;
543
544 MPASS(VOP_ISLOCKED(dvp));
545 MPASS(VOP_ISLOCKED(vp));
546
547 if (vp->v_type == VDIR) {
548 error = EISDIR;
549 goto out;
550 }
551
552 dnode = VP_TO_TMPFS_DIR(dvp);
553 node = VP_TO_TMPFS_NODE(vp);
554 tmp = VFS_TO_TMPFS(vp->v_mount);
555 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
556 MPASS(de != NULL);
557
558 /* Files marked as immutable or append-only cannot be deleted. */
559 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
560 (dnode->tn_flags & APPEND)) {
561 error = EPERM;
562 goto out;
563 }
564
565 /* Remove the entry from the directory; as it is a file, we do not
566 * have to change the number of hard links of the directory. */
567 tmpfs_dir_detach(dvp, de);
568 if (v->a_cnp->cn_flags & DOWHITEOUT)
569 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
570
571 /* Free the directory entry we just deleted. Note that the node
572 * referred by it will not be removed until the vnode is really
573 * reclaimed. */
574 tmpfs_free_dirent(tmp, de);
575
576 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED;
577 error = 0;
578
579out:
580
581 return error;
582}
583
584static int
585tmpfs_link(struct vop_link_args *v)
586{
587 struct vnode *dvp = v->a_tdvp;
588 struct vnode *vp = v->a_vp;
589 struct componentname *cnp = v->a_cnp;
590
591 int error;
592 struct tmpfs_dirent *de;
593 struct tmpfs_node *node;
594
595 MPASS(VOP_ISLOCKED(dvp));
596 MPASS(cnp->cn_flags & HASBUF);
597 MPASS(dvp != vp); /* XXX When can this be false? */
598 node = VP_TO_TMPFS_NODE(vp);
599
600 /* Ensure that we do not overflow the maximum number of links imposed
601 * by the system. */
602 MPASS(node->tn_links <= LINK_MAX);
603 if (node->tn_links == LINK_MAX) {
604 error = EMLINK;
605 goto out;
606 }
607
608 /* We cannot create links of files marked immutable or append-only. */
609 if (node->tn_flags & (IMMUTABLE | APPEND)) {
610 error = EPERM;
611 goto out;
612 }
613
614 /* Allocate a new directory entry to represent the node. */
615 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
616 cnp->cn_nameptr, cnp->cn_namelen, &de);
617 if (error != 0)
618 goto out;
619
620 /* Insert the new directory entry into the appropriate directory. */
621 if (cnp->cn_flags & ISWHITEOUT)
622 tmpfs_dir_whiteout_remove(dvp, cnp);
623 tmpfs_dir_attach(dvp, de);
624
625 /* vp link count has changed, so update node times. */
626 node->tn_status |= TMPFS_NODE_CHANGED;
627 tmpfs_update(vp);
628
629 error = 0;
630
631out:
632 return error;
633}
634
635/*
636 * We acquire all but fdvp locks using non-blocking acquisitions. If we
637 * fail to acquire any lock in the path we will drop all held locks,
638 * acquire the new lock in a blocking fashion, and then release it and
639 * restart the rename. This acquire/release step ensures that we do not
640 * spin on a lock waiting for release. On error release all vnode locks
641 * and decrement references the way tmpfs_rename() would do.
642 */
643static int
644tmpfs_rename_relock(struct vnode *fdvp, struct vnode **fvpp,
645 struct vnode *tdvp, struct vnode **tvpp,
646 struct componentname *fcnp, struct componentname *tcnp)
647{
648 struct vnode *nvp;
649 struct mount *mp;
650 struct tmpfs_dirent *de;
651 int error, restarts = 0;
652
653 VOP_UNLOCK(tdvp, 0);
654 if (*tvpp != NULL && *tvpp != tdvp)
655 VOP_UNLOCK(*tvpp, 0);
656 mp = fdvp->v_mount;
657
658relock:
659 restarts += 1;
660 error = vn_lock(fdvp, LK_EXCLUSIVE);
661 if (error)
662 goto releout;
663 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
664 VOP_UNLOCK(fdvp, 0);
665 error = vn_lock(tdvp, LK_EXCLUSIVE);
666 if (error)
667 goto releout;
668 VOP_UNLOCK(tdvp, 0);
669 goto relock;
670 }
671 /*
672 * Re-resolve fvp to be certain it still exists and fetch the
673 * correct vnode.
674 */
675 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(fdvp), NULL, fcnp);
676 if (de == NULL) {
677 VOP_UNLOCK(fdvp, 0);
678 VOP_UNLOCK(tdvp, 0);
679 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
680 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
681 error = EINVAL;
682 else
683 error = ENOENT;
684 goto releout;
685 }
686 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE | LK_NOWAIT, &nvp);
687 if (error != 0) {
688 VOP_UNLOCK(fdvp, 0);
689 VOP_UNLOCK(tdvp, 0);
690 if (error != EBUSY)
691 goto releout;
692 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE, &nvp);
693 if (error != 0)
694 goto releout;
695 VOP_UNLOCK(nvp, 0);
696 /*
697 * Concurrent rename race.
698 */
699 if (nvp == tdvp) {
700 vrele(nvp);
701 error = EINVAL;
702 goto releout;
703 }
704 vrele(*fvpp);
705 *fvpp = nvp;
706 goto relock;
707 }
708 vrele(*fvpp);
709 *fvpp = nvp;
710 VOP_UNLOCK(*fvpp, 0);
711 /*
712 * Re-resolve tvp and acquire the vnode lock if present.
713 */
714 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(tdvp), NULL, tcnp);
715 /*
716 * If tvp disappeared we just carry on.
717 */
718 if (de == NULL && *tvpp != NULL) {
719 vrele(*tvpp);
720 *tvpp = NULL;
721 }
722 /*
723 * Get the tvp ino if the lookup succeeded. We may have to restart
724 * if the non-blocking acquire fails.
725 */
726 if (de != NULL) {
727 nvp = NULL;
728 error = tmpfs_alloc_vp(mp, de->td_node,
729 LK_EXCLUSIVE | LK_NOWAIT, &nvp);
730 if (*tvpp != NULL)
731 vrele(*tvpp);
732 *tvpp = nvp;
733 if (error != 0) {
734 VOP_UNLOCK(fdvp, 0);
735 VOP_UNLOCK(tdvp, 0);
736 if (error != EBUSY)
737 goto releout;
738 error = tmpfs_alloc_vp(mp, de->td_node, LK_EXCLUSIVE,
739 &nvp);
740 if (error != 0)
741 goto releout;
742 VOP_UNLOCK(nvp, 0);
743 /*
744 * fdvp contains fvp, thus tvp (=fdvp) is not empty.
745 */
746 if (nvp == fdvp) {
747 error = ENOTEMPTY;
748 goto releout;
749 }
750 goto relock;
751 }
752 }
753 tmpfs_rename_restarts += restarts;
754
755 return (0);
756
757releout:
758 vrele(fdvp);
759 vrele(*fvpp);
760 vrele(tdvp);
761 if (*tvpp != NULL)
762 vrele(*tvpp);
763 tmpfs_rename_restarts += restarts;
764
765 return (error);
766}
767
768static int
769tmpfs_rename(struct vop_rename_args *v)
770{
771 struct vnode *fdvp = v->a_fdvp;
772 struct vnode *fvp = v->a_fvp;
773 struct componentname *fcnp = v->a_fcnp;
774 struct vnode *tdvp = v->a_tdvp;
775 struct vnode *tvp = v->a_tvp;
776 struct componentname *tcnp = v->a_tcnp;
777 struct mount *mp = NULL;
778
779 char *newname;
780 int error;
781 struct tmpfs_dirent *de;
782 struct tmpfs_mount *tmp;
783 struct tmpfs_node *fdnode;
784 struct tmpfs_node *fnode;
785 struct tmpfs_node *tnode;
786 struct tmpfs_node *tdnode;
787
788 MPASS(VOP_ISLOCKED(tdvp));
789 MPASS(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp)));
790 MPASS(fcnp->cn_flags & HASBUF);
791 MPASS(tcnp->cn_flags & HASBUF);
792
793 /* Disallow cross-device renames.
794 * XXX Why isn't this done by the caller? */
795 if (fvp->v_mount != tdvp->v_mount ||
796 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
797 error = EXDEV;
798 goto out;
799 }
800
801 /* If source and target are the same file, there is nothing to do. */
802 if (fvp == tvp) {
803 error = 0;
804 goto out;
805 }
806
807 /* If we need to move the directory between entries, lock the
808 * source so that we can safely operate on it. */
809 if (fdvp != tdvp && fdvp != tvp) {
810 if (vn_lock(fdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
811 mp = tdvp->v_mount;
812 error = vfs_busy(mp, 0);
813 if (error != 0) {
814 mp = NULL;
815 goto out;
816 }
817 error = tmpfs_rename_relock(fdvp, &fvp, tdvp, &tvp,
818 fcnp, tcnp);
819 if (error != 0) {
820 vfs_unbusy(mp);
821 return (error);
822 }
823 ASSERT_VOP_ELOCKED(fdvp,
824 "tmpfs_rename: fdvp not locked");
825 ASSERT_VOP_ELOCKED(tdvp,
826 "tmpfs_rename: tdvp not locked");
827 if (tvp != NULL)
828 ASSERT_VOP_ELOCKED(tvp,
829 "tmpfs_rename: tvp not locked");
830 if (fvp == tvp) {
831 error = 0;
832 goto out_locked;
833 }
834 }
835 }
836
837 tmp = VFS_TO_TMPFS(tdvp->v_mount);
838 tdnode = VP_TO_TMPFS_DIR(tdvp);
839 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
840 fdnode = VP_TO_TMPFS_DIR(fdvp);
841 fnode = VP_TO_TMPFS_NODE(fvp);
842 de = tmpfs_dir_lookup(fdnode, fnode, fcnp);
843
844 /* Entry can disappear before we lock fdvp,
845 * also avoid manipulating '.' and '..' entries. */
846 if (de == NULL) {
847 if ((fcnp->cn_flags & ISDOTDOT) != 0 ||
848 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.'))
849 error = EINVAL;
850 else
851 error = ENOENT;
852 goto out_locked;
853 }
854 MPASS(de->td_node == fnode);
855
856 /* If re-naming a directory to another preexisting directory
857 * ensure that the target directory is empty so that its
858 * removal causes no side effects.
859 * Kern_rename gurantees the destination to be a directory
860 * if the source is one. */
861 if (tvp != NULL) {
862 MPASS(tnode != NULL);
863
864 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
865 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
866 error = EPERM;
867 goto out_locked;
868 }
869
870 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
871 if (tnode->tn_size > 0) {
872 error = ENOTEMPTY;
873 goto out_locked;
874 }
875 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
876 error = ENOTDIR;
877 goto out_locked;
878 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
879 error = EISDIR;
880 goto out_locked;
881 } else {
882 MPASS(fnode->tn_type != VDIR &&
883 tnode->tn_type != VDIR);
884 }
885 }
886
887 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
888 || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
889 error = EPERM;
890 goto out_locked;
891 }
892
893 /* Ensure that we have enough memory to hold the new name, if it
894 * has to be changed. */
895 if (fcnp->cn_namelen != tcnp->cn_namelen ||
896 bcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
897 newname = malloc(tcnp->cn_namelen, M_TMPFSNAME, M_WAITOK);
898 } else
899 newname = NULL;
900
901 /* If the node is being moved to another directory, we have to do
902 * the move. */
903 if (fdnode != tdnode) {
904 /* In case we are moving a directory, we have to adjust its
905 * parent to point to the new parent. */
906 if (de->td_node->tn_type == VDIR) {
907 struct tmpfs_node *n;
908
909 /* Ensure the target directory is not a child of the
910 * directory being moved. Otherwise, we'd end up
911 * with stale nodes. */
912 n = tdnode;
913 /* TMPFS_LOCK garanties that no nodes are freed while
914 * traversing the list. Nodes can only be marked as
915 * removed: tn_parent == NULL. */
916 TMPFS_LOCK(tmp);
917 TMPFS_NODE_LOCK(n);
918 while (n != n->tn_dir.tn_parent) {
919 struct tmpfs_node *parent;
920
921 if (n == fnode) {
922 TMPFS_NODE_UNLOCK(n);
923 TMPFS_UNLOCK(tmp);
924 error = EINVAL;
925 if (newname != NULL)
926 free(newname, M_TMPFSNAME);
927 goto out_locked;
928 }
929 parent = n->tn_dir.tn_parent;
930 TMPFS_NODE_UNLOCK(n);
931 if (parent == NULL) {
932 n = NULL;
933 break;
934 }
935 TMPFS_NODE_LOCK(parent);
936 if (parent->tn_dir.tn_parent == NULL) {
937 TMPFS_NODE_UNLOCK(parent);
938 n = NULL;
939 break;
940 }
941 n = parent;
942 }
943 TMPFS_UNLOCK(tmp);
944 if (n == NULL) {
945 error = EINVAL;
946 if (newname != NULL)
947 free(newname, M_TMPFSNAME);
948 goto out_locked;
949 }
950 TMPFS_NODE_UNLOCK(n);
951
952 /* Adjust the parent pointer. */
953 TMPFS_VALIDATE_DIR(fnode);
954 TMPFS_NODE_LOCK(de->td_node);
955 de->td_node->tn_dir.tn_parent = tdnode;
956 TMPFS_NODE_UNLOCK(de->td_node);
957
958 /* As a result of changing the target of the '..'
959 * entry, the link count of the source and target
960 * directories has to be adjusted. */
961 TMPFS_NODE_LOCK(tdnode);
962 TMPFS_ASSERT_LOCKED(tdnode);
963 tdnode->tn_links++;
964 TMPFS_NODE_UNLOCK(tdnode);
965
966 TMPFS_NODE_LOCK(fdnode);
967 TMPFS_ASSERT_LOCKED(fdnode);
968 fdnode->tn_links--;
969 TMPFS_NODE_UNLOCK(fdnode);
970 }
971 }
972
973 /* Do the move: just remove the entry from the source directory
974 * and insert it into the target one. */
975 tmpfs_dir_detach(fdvp, de);
976
977 if (fcnp->cn_flags & DOWHITEOUT)
978 tmpfs_dir_whiteout_add(fdvp, fcnp);
979 if (tcnp->cn_flags & ISWHITEOUT)
980 tmpfs_dir_whiteout_remove(tdvp, tcnp);
981
982 /* If the name has changed, we need to make it effective by changing
983 * it in the directory entry. */
984 if (newname != NULL) {
985 MPASS(tcnp->cn_namelen <= MAXNAMLEN);
986
987 free(de->ud.td_name, M_TMPFSNAME);
988 de->ud.td_name = newname;
989 tmpfs_dirent_init(de, tcnp->cn_nameptr, tcnp->cn_namelen);
990
991 fnode->tn_status |= TMPFS_NODE_CHANGED;
992 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
993 }
994
995 /* If we are overwriting an entry, we have to remove the old one
996 * from the target directory. */
997 if (tvp != NULL) {
998 struct tmpfs_dirent *tde;
999
1000 /* Remove the old entry from the target directory. */
1001 tde = tmpfs_dir_lookup(tdnode, tnode, tcnp);
1002 tmpfs_dir_detach(tdvp, tde);
1003
1004 /* Free the directory entry we just deleted. Note that the
1005 * node referred by it will not be removed until the vnode is
1006 * really reclaimed. */
1007 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), tde);
1008 }
1009
1010 tmpfs_dir_attach(tdvp, de);
1011
1012 cache_purge(fvp);
1013 if (tvp != NULL)
1014 cache_purge(tvp);
1015 cache_purge_negative(tdvp);
1016
1017 error = 0;
1018
1019out_locked:
1020 if (fdvp != tdvp && fdvp != tvp)
1021 VOP_UNLOCK(fdvp, 0);
1022
1023out:
1024 /* Release target nodes. */
1025 /* XXX: I don't understand when tdvp can be the same as tvp, but
1026 * other code takes care of this... */
1027 if (tdvp == tvp)
1028 vrele(tdvp);
1029 else
1030 vput(tdvp);
1031 if (tvp != NULL)
1032 vput(tvp);
1033
1034 /* Release source nodes. */
1035 vrele(fdvp);
1036 vrele(fvp);
1037
1038 if (mp != NULL)
1039 vfs_unbusy(mp);
1040
1041 return error;
1042}
1043
1044static int
1045tmpfs_mkdir(struct vop_mkdir_args *v)
1046{
1047 struct vnode *dvp = v->a_dvp;
1048 struct vnode **vpp = v->a_vpp;
1049 struct componentname *cnp = v->a_cnp;
1050 struct vattr *vap = v->a_vap;
1051
1052 MPASS(vap->va_type == VDIR);
1053
1054 return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1055}
1056
1057static int
1058tmpfs_rmdir(struct vop_rmdir_args *v)
1059{
1060 struct vnode *dvp = v->a_dvp;
1061 struct vnode *vp = v->a_vp;
1062
1063 int error;
1064 struct tmpfs_dirent *de;
1065 struct tmpfs_mount *tmp;
1066 struct tmpfs_node *dnode;
1067 struct tmpfs_node *node;
1068
1069 MPASS(VOP_ISLOCKED(dvp));
1070 MPASS(VOP_ISLOCKED(vp));
1071
1072 tmp = VFS_TO_TMPFS(dvp->v_mount);
1073 dnode = VP_TO_TMPFS_DIR(dvp);
1074 node = VP_TO_TMPFS_DIR(vp);
1075
1076 /* Directories with more than two entries ('.' and '..') cannot be
1077 * removed. */
1078 if (node->tn_size > 0) {
1079 error = ENOTEMPTY;
1080 goto out;
1081 }
1082
1083 if ((dnode->tn_flags & APPEND)
1084 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1085 error = EPERM;
1086 goto out;
1087 }
1088
1089 /* This invariant holds only if we are not trying to remove "..".
1090 * We checked for that above so this is safe now. */
1091 MPASS(node->tn_dir.tn_parent == dnode);
1092
1093 /* Get the directory entry associated with node (vp). This was
1094 * filled by tmpfs_lookup while looking up the entry. */
1095 de = tmpfs_dir_lookup(dnode, node, v->a_cnp);
1096 MPASS(TMPFS_DIRENT_MATCHES(de,
1097 v->a_cnp->cn_nameptr,
1098 v->a_cnp->cn_namelen));
1099
1100 /* Check flags to see if we are allowed to remove the directory. */
1101 if ((dnode->tn_flags & APPEND) != 0 ||
1102 (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) != 0) {
1103 error = EPERM;
1104 goto out;
1105 }
1106
1107
1108 /* Detach the directory entry from the directory (dnode). */
1109 tmpfs_dir_detach(dvp, de);
1110 if (v->a_cnp->cn_flags & DOWHITEOUT)
1111 tmpfs_dir_whiteout_add(dvp, v->a_cnp);
1112
1113 /* No vnode should be allocated for this entry from this point */
1114 TMPFS_NODE_LOCK(node);
1115 node->tn_links--;
1116 node->tn_dir.tn_parent = NULL;
1117 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1118 TMPFS_NODE_MODIFIED;
1119
1120 TMPFS_NODE_UNLOCK(node);
1121
1122 TMPFS_NODE_LOCK(dnode);
1123 dnode->tn_links--;
1124 dnode->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED |
1125 TMPFS_NODE_MODIFIED;
1126 TMPFS_NODE_UNLOCK(dnode);
1127
1128 cache_purge(dvp);
1129 cache_purge(vp);
1130
1131 /* Free the directory entry we just deleted. Note that the node
1132 * referred by it will not be removed until the vnode is really
1133 * reclaimed. */
1134 tmpfs_free_dirent(tmp, de);
1135
1136 /* Release the deleted vnode (will destroy the node, notify
1137 * interested parties and clean it from the cache). */
1138
1139 dnode->tn_status |= TMPFS_NODE_CHANGED;
1140 tmpfs_update(dvp);
1141
1142 error = 0;
1143
1144out:
1145 return error;
1146}
1147
1148static int
1149tmpfs_symlink(struct vop_symlink_args *v)
1150{
1151 struct vnode *dvp = v->a_dvp;
1152 struct vnode **vpp = v->a_vpp;
1153 struct componentname *cnp = v->a_cnp;
1154 struct vattr *vap = v->a_vap;
1155 char *target = v->a_target;
1156
1157#ifdef notyet /* XXX FreeBSD BUG: kern_symlink is not setting VLNK */
1158 MPASS(vap->va_type == VLNK);
1159#else
1160 vap->va_type = VLNK;
1161#endif
1162
1163 return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1164}
1165
1166static int
1167tmpfs_readdir(struct vop_readdir_args *v)
1168{
1169 struct vnode *vp = v->a_vp;
1170 struct uio *uio = v->a_uio;
1171 int *eofflag = v->a_eofflag;
1172 u_long **cookies = v->a_cookies;
1173 int *ncookies = v->a_ncookies;
1174
1175 int error;
1176 ssize_t startresid;
1177 int maxcookies;
1178 struct tmpfs_node *node;
1179
1180 /* This operation only makes sense on directory nodes. */
1181 if (vp->v_type != VDIR)
1182 return ENOTDIR;
1183
1184 maxcookies = 0;
1185 node = VP_TO_TMPFS_DIR(vp);
1186
1187 startresid = uio->uio_resid;
1188
1189 /* Allocate cookies for NFS and compat modules. */
1190 if (cookies != NULL && ncookies != NULL) {
1191 maxcookies = howmany(node->tn_size,
1192 sizeof(struct tmpfs_dirent)) + 2;
1193 *cookies = malloc(maxcookies * sizeof(**cookies), M_TEMP,
1194 M_WAITOK);
1195 *ncookies = 0;
1196 }
1197
1198 if (cookies == NULL)
1199 error = tmpfs_dir_getdents(node, uio, 0, NULL, NULL);
1200 else
1201 error = tmpfs_dir_getdents(node, uio, maxcookies, *cookies,
1202 ncookies);
1203
1204 /* Buffer was filled without hitting EOF. */
1205 if (error == EJUSTRETURN)
1206 error = (uio->uio_resid != startresid) ? 0 : EINVAL;
1207
1208 if (error != 0 && cookies != NULL && ncookies != NULL) {
1209 free(*cookies, M_TEMP);
1210 *cookies = NULL;
1211 *ncookies = 0;
1212 }
1213
1214 if (eofflag != NULL)
1215 *eofflag =
1216 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1217
1218 return error;
1219}
1220
1221static int
1222tmpfs_readlink(struct vop_readlink_args *v)
1223{
1224 struct vnode *vp = v->a_vp;
1225 struct uio *uio = v->a_uio;
1226
1227 int error;
1228 struct tmpfs_node *node;
1229
1230 MPASS(uio->uio_offset == 0);
1231 MPASS(vp->v_type == VLNK);
1232
1233 node = VP_TO_TMPFS_NODE(vp);
1234
1235 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1236 uio);
1237 tmpfs_set_status(node, TMPFS_NODE_ACCESSED);
1238
1239 return (error);
1240}
1241
1242static int
1243tmpfs_inactive(struct vop_inactive_args *v)
1244{
1245 struct vnode *vp;
1246 struct tmpfs_node *node;
1247
1248 vp = v->a_vp;
1249 node = VP_TO_TMPFS_NODE(vp);
1250 if (node->tn_links == 0)
1251 vrecycle(vp);
1252 else
1253 tmpfs_check_mtime(vp);
1254 return (0);
1255}
1256
1257int
1258tmpfs_reclaim(struct vop_reclaim_args *v)
1259{
1260 struct vnode *vp = v->a_vp;
1261
1262 struct tmpfs_mount *tmp;
1263 struct tmpfs_node *node;
1264
1265 node = VP_TO_TMPFS_NODE(vp);
1266 tmp = VFS_TO_TMPFS(vp->v_mount);
1267
1268 if (vp->v_type == VREG)
1269 tmpfs_destroy_vobject(vp, node->tn_reg.tn_aobj);
1270 else
1271 vnode_destroy_vobject(vp);
1272 vp->v_object = NULL;
1273 cache_purge(vp);
1274
1275 TMPFS_NODE_LOCK(node);
1276 tmpfs_free_vp(vp);
1277
1278 /* If the node referenced by this vnode was deleted by the user,
1279 * we must free its associated data structures (now that the vnode
1280 * is being reclaimed). */
1281 if (node->tn_links == 0 &&
1282 (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1283 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1284 TMPFS_NODE_UNLOCK(node);
1285 tmpfs_free_node(tmp, node);
1286 } else
1287 TMPFS_NODE_UNLOCK(node);
1288
1289 MPASS(vp->v_data == NULL);
1290 return 0;
1291}
1292
1293static int
1294tmpfs_print(struct vop_print_args *v)
1295{
1296 struct vnode *vp = v->a_vp;
1297
1298 struct tmpfs_node *node;
1299
1300 node = VP_TO_TMPFS_NODE(vp);
1301
1302 printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%lx, links %d\n",
1303 node, node->tn_flags, node->tn_links);
1304 printf("\tmode 0%o, owner %d, group %d, size %jd, status 0x%x\n",
1305 node->tn_mode, node->tn_uid, node->tn_gid,
1306 (intmax_t)node->tn_size, node->tn_status);
1307
1308 if (vp->v_type == VFIFO)
1309 fifo_printinfo(vp);
1310
1311 printf("\n");
1312
1313 return 0;
1314}
1315
1316static int
1317tmpfs_pathconf(struct vop_pathconf_args *v)
1318{
1319 int name = v->a_name;
1320 register_t *retval = v->a_retval;
1321
1322 int error;
1323
1324 error = 0;
1325
1326 switch (name) {
1327 case _PC_LINK_MAX:
1328 *retval = LINK_MAX;
1329 break;
1330
1331 case _PC_NAME_MAX:
1332 *retval = NAME_MAX;
1333 break;
1334
1335 case _PC_PATH_MAX:
1336 *retval = PATH_MAX;
1337 break;
1338
1339 case _PC_PIPE_BUF:
1340 *retval = PIPE_BUF;
1341 break;
1342
1343 case _PC_CHOWN_RESTRICTED:
1344 *retval = 1;
1345 break;
1346
1347 case _PC_NO_TRUNC:
1348 *retval = 1;
1349 break;
1350
1351 case _PC_SYNC_IO:
1352 *retval = 1;
1353 break;
1354
1355 case _PC_FILESIZEBITS:
1356 *retval = 0; /* XXX Don't know which value should I return. */
1357 break;
1358
1359 default:
1360 error = EINVAL;
1361 }
1362
1363 return error;
1364}
1365
1366static int
1367tmpfs_vptofh(struct vop_vptofh_args *ap)
1368{
1369 struct tmpfs_fid *tfhp;
1370 struct tmpfs_node *node;
1371
1372 tfhp = (struct tmpfs_fid *)ap->a_fhp;
1373 node = VP_TO_TMPFS_NODE(ap->a_vp);
1374
1375 tfhp->tf_len = sizeof(struct tmpfs_fid);
1376 tfhp->tf_id = node->tn_id;
1377 tfhp->tf_gen = node->tn_gen;
1378
1379 return (0);
1380}
1381
1382static int
1383tmpfs_whiteout(struct vop_whiteout_args *ap)
1384{
1385 struct vnode *dvp = ap->a_dvp;
1386 struct componentname *cnp = ap->a_cnp;
1387 struct tmpfs_dirent *de;
1388
1389 switch (ap->a_flags) {
1390 case LOOKUP:
1391 return (0);
1392 case CREATE:
1393 de = tmpfs_dir_lookup(VP_TO_TMPFS_DIR(dvp), NULL, cnp);
1394 if (de != NULL)
1395 return (de->td_node == NULL ? 0 : EEXIST);
1396 return (tmpfs_dir_whiteout_add(dvp, cnp));
1397 case DELETE:
1398 tmpfs_dir_whiteout_remove(dvp, cnp);
1399 return (0);
1400 default:
1401 panic("tmpfs_whiteout: unknown op");
1402 }
1403}
1404
1405static int
1406tmpfs_vptocnp_dir(struct tmpfs_node *tn, struct tmpfs_node *tnp,
1407 struct tmpfs_dirent **pde)
1408{
1409 struct tmpfs_dir_cursor dc;
1410 struct tmpfs_dirent *de;
1411
1412 for (de = tmpfs_dir_first(tnp, &dc); de != NULL;
1413 de = tmpfs_dir_next(tnp, &dc)) {
1414 if (de->td_node == tn) {
1415 *pde = de;
1416 return (0);
1417 }
1418 }
1419 return (ENOENT);
1420}
1421
1422static int
1423tmpfs_vptocnp_fill(struct vnode *vp, struct tmpfs_node *tn,
1424 struct tmpfs_node *tnp, char *buf, int *buflen, struct vnode **dvp)
1425{
1426 struct tmpfs_dirent *de;
1427 int error, i;
1428
1429 error = vn_vget_ino_gen(vp, tmpfs_vn_get_ino_alloc, tnp, LK_SHARED,
1430 dvp);
1431 if (error != 0)
1432 return (error);
1433 error = tmpfs_vptocnp_dir(tn, tnp, &de);
1434 if (error == 0) {
1435 i = *buflen;
1436 i -= de->td_namelen;
1437 if (i < 0) {
1438 error = ENOMEM;
1439 } else {
1440 bcopy(de->ud.td_name, buf + i, de->td_namelen);
1441 *buflen = i;
1442 }
1443 }
1444 if (error == 0) {
1445 if (vp != *dvp)
1446 VOP_UNLOCK(*dvp, 0);
1447 } else {
1448 if (vp != *dvp)
1449 vput(*dvp);
1450 else
1451 vrele(vp);
1452 }
1453 return (error);
1454}
1455
1456static int
1457tmpfs_vptocnp(struct vop_vptocnp_args *ap)
1458{
1459 struct vnode *vp, **dvp;
1460 struct tmpfs_node *tn, *tnp, *tnp1;
1461 struct tmpfs_dirent *de;
1462 struct tmpfs_mount *tm;
1463 char *buf;
1464 int *buflen;
1465 int error;
1466
1467 vp = ap->a_vp;
1468 dvp = ap->a_vpp;
1469 buf = ap->a_buf;
1470 buflen = ap->a_buflen;
1471
1472 tm = VFS_TO_TMPFS(vp->v_mount);
1473 tn = VP_TO_TMPFS_NODE(vp);
1474 if (tn->tn_type == VDIR) {
1475 tnp = tn->tn_dir.tn_parent;
1476 if (tnp == NULL)
1477 return (ENOENT);
1478 tmpfs_ref_node(tnp);
1479 error = tmpfs_vptocnp_fill(vp, tn, tn->tn_dir.tn_parent, buf,
1480 buflen, dvp);
1481 tmpfs_free_node(tm, tnp);
1482 return (error);
1483 }
1484restart:
1485 TMPFS_LOCK(tm);
1486 LIST_FOREACH_SAFE(tnp, &tm->tm_nodes_used, tn_entries, tnp1) {
1487 if (tnp->tn_type != VDIR)
1488 continue;
1489 TMPFS_NODE_LOCK(tnp);
1490 tmpfs_ref_node_locked(tnp);
1491
1492 /*
1493 * tn_vnode cannot be instantiated while we hold the
1494 * node lock, so the directory cannot be changed while
1495 * we iterate over it. Do this to avoid instantiating
1496 * vnode for directories which cannot point to our
1497 * node.
1498 */
1499 error = tnp->tn_vnode == NULL ? tmpfs_vptocnp_dir(tn, tnp,
1500 &de) : 0;
1501
1502 if (error == 0) {
1503 TMPFS_NODE_UNLOCK(tnp);
1504 TMPFS_UNLOCK(tm);
1505 error = tmpfs_vptocnp_fill(vp, tn, tnp, buf, buflen,
1506 dvp);
1507 if (error == 0) {
1508 tmpfs_free_node(tm, tnp);
1509 return (0);
1510 }
1511 if ((vp->v_iflag & VI_DOOMED) != 0) {
1512 tmpfs_free_node(tm, tnp);
1513 return (ENOENT);
1514 }
1515 TMPFS_LOCK(tm);
1516 TMPFS_NODE_LOCK(tnp);
1517 }
1518 if (tmpfs_free_node_locked(tm, tnp, false)) {
1519 goto restart;
1520 } else {
1521 KASSERT(tnp->tn_refcount > 0,
1522 ("node %p refcount zero", tnp));
1523 tnp1 = LIST_NEXT(tnp, tn_entries);
1524 TMPFS_NODE_UNLOCK(tnp);
1525 }
1526 }
1527 TMPFS_UNLOCK(tm);
1528 return (ENOENT);
1529}
1530
1531/*
1532 * Vnode operations vector used for files stored in a tmpfs file system.
1533 */
1534struct vop_vector tmpfs_vnodeop_entries = {
1535 .vop_default = &default_vnodeops,
1536 .vop_lookup = vfs_cache_lookup,
1537 .vop_cachedlookup = tmpfs_lookup,
1538 .vop_create = tmpfs_create,
1539 .vop_mknod = tmpfs_mknod,
1540 .vop_open = tmpfs_open,
1541 .vop_close = tmpfs_close,
1542 .vop_access = tmpfs_access,
1543 .vop_getattr = tmpfs_getattr,
1544 .vop_setattr = tmpfs_setattr,
1545 .vop_read = tmpfs_read,
1546 .vop_write = tmpfs_write,
1547 .vop_fsync = tmpfs_fsync,
1548 .vop_remove = tmpfs_remove,
1549 .vop_link = tmpfs_link,
1550 .vop_rename = tmpfs_rename,
1551 .vop_mkdir = tmpfs_mkdir,
1552 .vop_rmdir = tmpfs_rmdir,
1553 .vop_symlink = tmpfs_symlink,
1554 .vop_readdir = tmpfs_readdir,
1555 .vop_readlink = tmpfs_readlink,
1556 .vop_inactive = tmpfs_inactive,
1557 .vop_reclaim = tmpfs_reclaim,
1558 .vop_print = tmpfs_print,
1559 .vop_pathconf = tmpfs_pathconf,
1560 .vop_vptofh = tmpfs_vptofh,
1561 .vop_whiteout = tmpfs_whiteout,
1562 .vop_bmap = VOP_EOPNOTSUPP,
1563 .vop_vptocnp = tmpfs_vptocnp,
1564};
1565