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