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