kernel - TMPFS - Initial port of NetBSD's tmpfs
[dragonfly.git] / sys / vfs / 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
38 #include <sys/kernel.h>
39 #include <sys/kern_syscall.h>
40 #include <sys/param.h>
41 #include <sys/fcntl.h>
42 #include <sys/lockf.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/resourcevar.h>
46 #include <sys/sched.h>
47 #include <sys/sfbuf.h>
48 #include <sys/stat.h>
49 #include <sys/systm.h>
50 #include <sys/unistd.h>
51 #include <sys/vfsops.h>
52 #include <sys/vnode.h>
53
54 #include <sys/mplock2.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_page.h>
59 #include <vm/vm_pager.h>
60
61 #include <vfs/fifofs/fifo.h>
62 #include <vfs/tmpfs/tmpfs_vnops.h>
63 #include <vfs/tmpfs/tmpfs.h>
64
65 MALLOC_DECLARE(M_TMPFS);
66
67 /* --------------------------------------------------------------------- */
68
69 static int
70 tmpfs_nresolve(struct vop_nresolve_args *v)
71 {
72         struct vnode *dvp = v->a_dvp;
73         struct vnode *vp = NULL;
74         struct namecache *ncp = v->a_nch->ncp;
75         struct ucred *cred = v->a_cred;
76
77         int error;
78         struct tmpfs_dirent *de;
79         struct tmpfs_node *dnode;
80
81         dnode = VP_TO_TMPFS_DIR(dvp);
82
83         if (!vn_islocked(dvp));
84                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
85
86         /* Check accessibility of requested node as a first step. */
87         error = VOP_ACCESS(dvp, VEXEC, cred);
88         if (error != 0)
89                 goto out;
90
91         if (dnode->tn_dir.tn_parent == NULL) {
92                 error = ENOENT;
93                 goto out;
94         }
95
96         de = tmpfs_dir_lookup(dnode, NULL, ncp);
97         if (de == NULL) {
98                 /* The entry was not found in the directory.
99                  * This is OK if we are creating or renaming an
100                  * entry and are working on the last component of
101                  * the path name. */
102                 error = VOP_ACCESS(dvp, VWRITE, cred);
103                 if (error != 0)
104                         goto out;
105                 else {
106                         error = ENOENT;
107                         goto out;
108                 }
109         } else {
110                 struct tmpfs_node *tnode;
111
112                 /* The entry was found, so get its associated
113                  * tmpfs_node. */
114                 tnode = de->td_node;
115
116                 /* If we are not at the last path component and
117                  * found a non-directory or non-link entry (which
118                  * may itself be pointing to a directory), raise
119                  * an error. */
120                 if (tnode->tn_links > 1 &&
121                     tnode->tn_type != VDIR && tnode->tn_type != VLNK) {
122                         error = ENOTDIR;
123                         goto out;
124                 }
125
126                 error = VOP_ACCESS(dvp, VWRITE, cred);
127                 if (error != 0)
128                         goto out;
129
130                 /* Allocate a new vnode on the matching entry. */
131                 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
132                                 LK_EXCLUSIVE | LK_RETRY, &vp);
133                 if (error != 0)
134                         goto out;
135
136                 if ((dnode->tn_mode & S_ISTXT) &&
137                     VOP_ACCESS(vp, VWRITE, cred)) {
138                         error = EPERM;
139                         vp = NULL;
140                         goto out;
141                 }
142         }
143
144         KKASSERT(vp);
145
146 out:
147         vn_unlock(dvp);
148         /* Store the result of this lookup in the cache.  Avoid this if the
149          * request was for creation, as it does not improve timings on
150          * emprical tests. */
151         if (vp) {
152                 vn_unlock(vp);
153                 cache_setvp(v->a_nch, vp);
154                 vrele(vp);
155         }
156         if (error == ENOENT) {
157                 cache_setvp(v->a_nch, NULL);
158         }
159         return error;
160 }
161
162 static int
163 tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
164 {
165         struct vnode *dvp = v->a_dvp;
166         struct vnode **vpp = v->a_vpp;
167         struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
168         struct ucred *cred = v->a_cred;
169         int error;
170
171         *vpp = NULL;
172         /* Check accessibility of requested node as a first step. */
173         error = VOP_ACCESS(dvp, VEXEC, cred);
174         if (error != 0)
175                 return error;
176
177         if (dnode->tn_dir.tn_parent != NULL) {
178                 /* Allocate a new vnode on the matching entry. */
179                 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
180                     LK_EXCLUSIVE | LK_RETRY, vpp);
181
182                 if (*vpp)
183                         vn_unlock(*vpp);
184         }
185
186         return (*vpp == NULL) ? ENOENT : 0;
187 }
188
189 /* --------------------------------------------------------------------- */
190
191 static int
192 tmpfs_ncreate(struct vop_ncreate_args *v)
193 {
194         struct vnode *dvp = v->a_dvp;
195         struct vnode **vpp = v->a_vpp;
196         struct namecache *ncp = v->a_nch->ncp;
197         struct vattr *vap = v->a_vap;
198         struct ucred *cred = v->a_cred;
199         int error;
200
201         KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
202
203         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
204         error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
205         if (error == 0) {
206                 cache_setunresolved(v->a_nch);
207                 cache_setvp(v->a_nch, *vpp);
208         }
209         vn_unlock(dvp);
210
211         return error;
212 }
213 /* --------------------------------------------------------------------- */
214
215 static int
216 tmpfs_nmknod(struct vop_nmknod_args *v)
217 {
218         struct vnode *dvp = v->a_dvp;
219         struct vnode **vpp = v->a_vpp;
220         struct namecache *ncp = v->a_nch->ncp;
221         struct vattr *vap = v->a_vap;
222         struct ucred *cred = v->a_cred;
223         int error;
224
225         if (vap->va_type != VBLK && vap->va_type != VCHR &&
226             vap->va_type != VFIFO)
227                 return EINVAL;
228
229         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
230         error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
231         if (error == 0) {
232                 cache_setunresolved(v->a_nch);
233                 cache_setvp(v->a_nch, *vpp);
234         }
235         vn_unlock(dvp);
236
237         return error;
238 }
239
240 /* --------------------------------------------------------------------- */
241
242 static int
243 tmpfs_open(struct vop_open_args *v)
244 {
245         struct vnode *vp = v->a_vp;
246         int mode = v->a_mode;
247
248         int error;
249         struct tmpfs_node *node;
250
251         KKASSERT(vn_islocked(vp));
252
253         node = VP_TO_TMPFS_NODE(vp);
254
255         /* The file is still active but all its names have been removed
256          * (e.g. by a "rmdir $(pwd)").  It cannot be opened any more as
257          * it is about to die. */
258         if (node->tn_links < 1)
259                 return (ENOENT);
260
261         /* If the file is marked append-only, deny write requests. */
262         if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
263                 error = EPERM;
264         else {
265                 return (vop_stdopen(v));
266         }
267
268         KKASSERT(vn_islocked(vp));
269         return error;
270 }
271
272 /* --------------------------------------------------------------------- */
273
274 static int
275 tmpfs_close(struct vop_close_args *v)
276 {
277         struct vnode *vp = v->a_vp;
278         struct tmpfs_node *node;
279
280         node = VP_TO_TMPFS_NODE(vp);
281
282         if (node->tn_links > 0) {
283                 /* Update node times.  No need to do it if the node has
284                  * been deleted, because it will vanish after we return. */
285                 tmpfs_update(vp);
286         }
287
288         return vop_stdclose(v);
289 }
290
291 /* --------------------------------------------------------------------- */
292
293 int
294 tmpfs_access(struct vop_access_args *v)
295 {
296         struct vnode *vp = v->a_vp;
297         int error;
298         struct tmpfs_node *node;
299
300         KKASSERT(vn_islocked(vp));
301
302         node = VP_TO_TMPFS_NODE(vp);
303
304         switch (vp->v_type) {
305         case VDIR:
306                 /* FALLTHROUGH */
307         case VLNK:
308                 /* FALLTHROUGH */
309         case VREG:
310                 if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
311                         error = EROFS;
312                         goto out;
313                 }
314                 break;
315
316         case VBLK:
317                 /* FALLTHROUGH */
318         case VCHR:
319                 /* FALLTHROUGH */
320         case VSOCK:
321                 /* FALLTHROUGH */
322         case VFIFO:
323                 break;
324
325         default:
326                 error = EINVAL;
327                 goto out;
328         }
329
330         if (VWRITE && node->tn_flags & IMMUTABLE) {
331                 error = EPERM;
332                 goto out;
333         }
334
335         error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
336
337 out:
338
339         return error;
340 }
341
342 /* --------------------------------------------------------------------- */
343
344 int
345 tmpfs_getattr(struct vop_getattr_args *v)
346 {
347         struct vnode *vp = v->a_vp;
348         struct vattr *vap = v->a_vap;
349
350         struct tmpfs_node *node;
351         int needunlock = 0;
352
353         if(!vn_islocked(vp)) {
354                 needunlock = 1;
355                 vn_lock(vp, LK_SHARED | LK_RETRY);
356         }
357
358         node = VP_TO_TMPFS_NODE(vp);
359
360         tmpfs_update(vp);
361
362         vap->va_type = vp->v_type;
363         vap->va_mode = node->tn_mode;
364         vap->va_nlink = node->tn_links;
365         vap->va_uid = node->tn_uid;
366         vap->va_gid = node->tn_gid;
367         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
368         vap->va_fileid = node->tn_id;
369         vap->va_size = node->tn_size;
370         vap->va_blocksize = PAGE_SIZE;
371         vap->va_atime.tv_sec = node->tn_atime;
372         vap->va_atime.tv_nsec = node->tn_atimensec;
373         vap->va_mtime.tv_sec = node->tn_mtime;
374         vap->va_mtime.tv_nsec = node->tn_mtimensec;
375         vap->va_ctime.tv_sec = node->tn_ctime;
376         vap->va_ctime.tv_nsec = node->tn_ctimensec;
377         vap->va_gen = node->tn_gen;
378         vap->va_flags = node->tn_flags;
379         if (vp->v_type == VBLK || vp->v_type == VCHR)
380         {
381                 vap->va_rmajor = umajor(node->tn_rdev);
382                 vap->va_rminor = uminor(node->tn_rdev);
383         }
384         vap->va_bytes = round_page(node->tn_size);
385         vap->va_filerev = 0;
386
387         if (needunlock)
388                 vn_unlock(vp);
389
390         return 0;
391 }
392
393 /* --------------------------------------------------------------------- */
394
395 int
396 tmpfs_setattr(struct vop_setattr_args *v)
397 {
398         struct vnode *vp = v->a_vp;
399         struct vattr *vap = v->a_vap;
400         struct ucred *cred = v->a_cred;
401
402         int error = 0;
403         int needunlock = 0;
404
405         if(!vn_islocked(vp)) {
406                 needunlock = 1;
407                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
408         }
409
410         /* Abort if any unsettable attribute is given. */
411         if (vap->va_type != VNON ||
412             vap->va_nlink != VNOVAL ||
413             vap->va_fsid != VNOVAL ||
414             vap->va_fileid != VNOVAL ||
415             vap->va_blocksize != VNOVAL ||
416             vap->va_gen != VNOVAL ||
417             vap->va_rmajor != VNOVAL ||
418             vap->va_bytes != VNOVAL)
419                 error = EINVAL;
420
421         if (error == 0 && (vap->va_flags != VNOVAL))
422                 error = tmpfs_chflags(vp, vap->va_flags, cred);
423
424         if (error == 0 && (vap->va_size != VNOVAL))
425                 error = tmpfs_chsize(vp, vap->va_size, cred);
426
427         if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
428                 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
429
430         if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
431                 error = tmpfs_chmod(vp, vap->va_mode, cred);
432
433         if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
434             vap->va_atime.tv_nsec != VNOVAL) ||
435             (vap->va_mtime.tv_sec != VNOVAL &&
436             vap->va_mtime.tv_nsec != VNOVAL) ))
437                 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
438                         vap->va_vaflags, cred);
439
440         /* Update the node times.  We give preference to the error codes
441          * generated by this function rather than the ones that may arise
442          * from tmpfs_update. */
443         tmpfs_update(vp);
444
445         if (needunlock)
446                 vn_unlock(vp);
447
448         return error;
449 }
450
451 /* --------------------------------------------------------------------- */
452
453 static int
454 tmpfs_fsync(struct vop_fsync_args *v)
455 {
456         struct vnode *vp = v->a_vp;
457
458         tmpfs_update(vp);
459
460
461         return 0;
462 }
463
464 /* --------------------------------------------------------------------- */
465
466 static int
467 tmpfs_read (struct vop_read_args *ap)
468 {
469         struct buf *bp;
470         struct vnode *vp = ap->a_vp;
471         struct uio *uio = ap->a_uio;
472         struct tmpfs_node *node;
473         int error;
474         off_t offset;
475         off_t base_offset;
476         size_t len;
477         int got_mplock;
478
479         error = 0;
480         if (uio->uio_resid == 0) {
481                 return error;
482         }
483
484         node = VP_TO_TMPFS_NODE(vp);
485
486         if (uio->uio_offset < 0)
487                 return (EINVAL);
488         if (vp->v_type != VREG)
489                 return (EINVAL);
490
491         vn_lock(vp, LK_SHARED | LK_RETRY);
492
493 #ifdef SMP
494         if(curthread->td_mpcount)
495                 got_mplock = -1;
496         else
497                 got_mplock = 0;
498 #else
499                 got_mplock = -1;
500 #endif
501
502         while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
503                 /*
504                  * Use buffer cache I/O (via tmpfs_strategy)
505                  */
506                 offset = (off_t)uio->uio_offset & BMASK;
507                 base_offset = (off_t)uio->uio_offset - offset;
508                 bp = getcacheblk(vp, base_offset);
509                 if (bp == NULL)
510                 {
511                         if (got_mplock == 0) {
512                                 got_mplock = 1;
513                                 get_mplock();
514                         }
515
516                         error = bread(vp, base_offset, BSIZE, &bp);
517                         if (error) {
518                                 brelse(bp);
519                                 kprintf("tmpfs_read bread error %d\n", error);
520                                 break;
521                         }
522                 }
523
524                 if (got_mplock == 0) {
525                         got_mplock = 1;
526                         get_mplock();
527                 }
528
529                 /*
530                  * Figure out how many bytes we can actually copy this loop.
531                  */
532                 len = BSIZE - offset;
533                 if (len > uio->uio_resid)
534                         len = uio->uio_resid;
535                 if (len > node->tn_size - uio->uio_offset)
536                         len = (size_t)(node->tn_size - uio->uio_offset);
537
538                 error = uiomove((char *)bp->b_data + offset, len, uio);
539                 bqrelse(bp);
540                 if (error) {
541                         kprintf("tmpfs_read uiomove error %d\n", error);
542                         break;
543                 }
544         }
545
546         if (got_mplock > 0)
547                 rel_mplock();
548
549         TMPFS_NODE_LOCK(node);
550         node->tn_status |= TMPFS_NODE_ACCESSED;
551         TMPFS_NODE_UNLOCK(node);
552
553         vn_unlock(vp);
554
555         return(error);
556 }
557
558 static int
559 tmpfs_write (struct vop_write_args *ap)
560 {
561         struct buf *bp;
562         struct vnode *vp = ap->a_vp;
563         struct uio *uio = ap->a_uio;
564         struct thread *td = uio->uio_td;
565         struct tmpfs_node *node;
566         boolean_t extended;
567         off_t oldsize;
568         int error;
569         off_t offset;
570         off_t base_offset;
571         size_t len;
572         struct rlimit limit;
573         int got_mplock;
574         int trivial = 0;
575
576         error = 0;
577         if (uio->uio_resid == 0) {
578                 return error;
579         }
580
581         node = VP_TO_TMPFS_NODE(vp);
582
583         if (vp->v_type != VREG)
584                 return (EINVAL);
585
586         oldsize = node->tn_size;
587         if (ap->a_ioflag & IO_APPEND)
588                 uio->uio_offset = node->tn_size;
589
590         /*
591          * Check for illegal write offsets.
592          */
593         if (uio->uio_offset + uio->uio_resid >
594           VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
595                 return (EFBIG);
596
597         if (vp->v_type == VREG && td != NULL) {
598                 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
599                 if (error != 0)
600                         return error;
601                 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
602                         ksignal(td->td_proc, SIGXFSZ);
603                         return (EFBIG);
604                 }
605         }
606
607
608         /*
609          * Extend the file's size if necessary
610          */
611         extended = (uio->uio_offset + uio->uio_resid) > node->tn_size;
612
613         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
614 #ifdef SMP
615         if(curthread->td_mpcount)
616                 got_mplock = -1;
617         else {
618                 got_mplock = 1;
619                 get_mplock();
620         }
621 #else
622                 got_mplock = -1;
623 #endif
624         crit_enter();
625         while (uio->uio_resid > 0) {
626                 /*
627                  * Use buffer cache I/O (via tmpfs_strategy)
628                  */
629                 offset = (off_t)uio->uio_offset & BMASK;
630                 base_offset = (off_t)uio->uio_offset - offset;
631                 len = BSIZE - offset;
632                 if (len > uio->uio_resid)
633                         len = uio->uio_resid;
634
635                 if ((uio->uio_offset + len) > node->tn_size) {
636                         trivial = uio->uio_offset <= node->tn_size;
637                         error = tmpfs_reg_resize(vp, uio->uio_offset + len,  trivial);
638                         if (error)
639                                 break;
640                 }
641
642                 bp = getblk(vp, base_offset, BSIZE, GETBLK_BHEAVY, 0);
643                 vfs_bio_clrbuf(bp);
644
645                 error = uiomove((char *)bp->b_data + offset, len, uio);
646                 if (error) {
647                         kprintf("tmpfs_write uiomove error %d\n", error);
648                         brelse(bp);
649                         break;
650                 }
651
652                 if (uio->uio_offset > node->tn_size)
653                         node->tn_size = uio->uio_offset;
654
655                 /*
656                  * The data has been loaded into the buffer, write it out. (via tmpfs_strategy)
657                  *
658                  * call bdwrite() because we don't care about storage io flag (ap->a_ioflag) for a swap I/O
659                  * maybe bawrite() for IO_DIRECT, bwrite() for IO_SYNC
660                  *
661                  * XXX: need to implement tmpfs_bmap() for a dirty bit handling of bdwrite()
662                  */
663                 bdwrite(bp);
664                 if (bp->b_error) {
665                         kprintf("tmpfs_write bwrite error %d\n", error);
666                         break;
667                 }
668         }
669         crit_exit();
670
671         if (got_mplock > 0)
672                 rel_mplock();
673
674         if (error) {
675                 if (extended)
676                         (void)tmpfs_reg_resize(vp, oldsize, trivial);
677                 return error;
678         }
679
680         TMPFS_NODE_LOCK(node);
681         node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
682             (extended? TMPFS_NODE_CHANGED : 0);
683
684         if (node->tn_mode & (S_ISUID | S_ISGID)) {
685                 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
686                         node->tn_mode &= ~(S_ISUID | S_ISGID);
687         }
688         TMPFS_NODE_UNLOCK(node);
689
690         vn_unlock(vp);
691
692         return(error);
693 }
694
695 static int
696 tmpfs_advlock (struct vop_advlock_args *ap)
697 {
698         struct tmpfs_node *node;
699         struct vnode *vp = ap->a_vp;
700
701         node = VP_TO_TMPFS_NODE(vp);
702
703         return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
704 }
705
706
707 static int
708 tmpfs_strategy(struct vop_strategy_args *ap)
709 {
710         struct bio *bio = ap->a_bio;
711         struct vnode *vp = ap->a_vp;
712         struct tmpfs_node *node;
713         vm_object_t uobj;
714
715         if (vp->v_type != VREG)
716                 return EINVAL;
717
718         node = VP_TO_TMPFS_NODE(vp);
719
720         uobj = node->tn_reg.tn_aobj;
721         /*
722          * call swap_pager_strategy to store vm object into swap device
723          */
724         swap_pager_strategy(uobj, bio);
725
726         return 0;
727 }
728
729 static int
730 tmpfs_bmap(struct vop_bmap_args *ap)
731 {
732         if (ap->a_doffsetp != NULL)
733                 *ap->a_doffsetp = ap->a_loffset;
734         if (ap->a_runp != NULL)
735                 *ap->a_runp = 0;
736         if (ap->a_runb != NULL)
737                 *ap->a_runb = 0;
738
739         return 0;
740 }
741 /* --------------------------------------------------------------------- */
742
743 static int
744 tmpfs_nremove(struct vop_nremove_args *v)
745 {
746         struct vnode *dvp = v->a_dvp;
747         struct namecache *ncp = v->a_nch->ncp;
748         struct vnode *vp = ncp->nc_vp;
749         int error;
750         struct tmpfs_dirent *de;
751         struct tmpfs_mount *tmp;
752         struct tmpfs_node *dnode;
753         struct tmpfs_node *node;
754
755         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
756         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
757
758         if (vp->v_type == VDIR) {
759                 error = EISDIR;
760                 goto out;
761         }
762
763         dnode = VP_TO_TMPFS_DIR(dvp);
764         node = VP_TO_TMPFS_NODE(vp);
765         tmp = VFS_TO_TMPFS(vp->v_mount);
766         de = tmpfs_dir_lookup(dnode, node, ncp);
767         if (de == NULL) {
768                 error = ENOENT;
769                 goto out;
770         }
771
772         /* Files marked as immutable or append-only cannot be deleted. */
773         if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
774             (dnode->tn_flags & APPEND)) {
775                 error = EPERM;
776                 goto out;
777         }
778
779         /* Remove the entry from the directory; as it is a file, we do not
780          * have to change the number of hard links of the directory. */
781         tmpfs_dir_detach(dvp, de);
782
783         /* Free the directory entry we just deleted.  Note that the node
784          * referred by it will not be removed until the vnode is really
785          * reclaimed. */
786         tmpfs_free_dirent(tmp, de, TRUE);
787
788         if (node->tn_links > 0) {
789                 TMPFS_NODE_LOCK(node);
790                 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
791                         TMPFS_NODE_MODIFIED;
792                 TMPFS_NODE_UNLOCK(node);
793         }
794
795         cache_setunresolved(v->a_nch);
796         cache_setvp(v->a_nch, NULL);
797         cache_inval_vp(vp, CINV_DESTROY);
798         error = 0;
799
800
801 out:
802         vn_unlock(vp);
803         vn_unlock(dvp);
804
805         return error;
806 }
807
808 /* --------------------------------------------------------------------- */
809
810 static int
811 tmpfs_nlink(struct vop_nlink_args *v)
812 {
813         struct vnode *dvp = v->a_dvp;
814         struct vnode *vp = v->a_vp;
815         struct namecache *ncp = v->a_nch->ncp;
816
817         int error;
818         struct tmpfs_dirent *de;
819         struct tmpfs_node *node;
820
821         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
822         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
823
824
825         KKASSERT(dvp != vp); /* XXX When can this be false? */
826
827         node = VP_TO_TMPFS_NODE(vp);
828
829         /* XXX: Why aren't the following two tests done by the caller? */
830
831         /* Hard links of directories are forbidden. */
832         if (vp->v_type == VDIR) {
833                 error = EPERM;
834                 goto out;
835         }
836
837         /* Cannot create cross-device links. */
838         if (dvp->v_mount != vp->v_mount) {
839                 error = EXDEV;
840                 goto out;
841         }
842
843         /* Ensure that we do not overflow the maximum number of links imposed
844          * by the system. */
845         KKASSERT(node->tn_links <= LINK_MAX);
846         if (node->tn_links == LINK_MAX) {
847                 error = EMLINK;
848                 goto out;
849         }
850
851         /* We cannot create links of files marked immutable or append-only. */
852         if (node->tn_flags & (IMMUTABLE | APPEND)) {
853                 error = EPERM;
854                 goto out;
855         }
856
857         /* Allocate a new directory entry to represent the node. */
858         error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
859             ncp->nc_name, ncp->nc_nlen, &de);
860         if (error != 0)
861                 goto out;
862
863         /* Insert the new directory entry into the appropriate directory. */
864         tmpfs_dir_attach(dvp, de);
865
866         /* vp link count has changed, so update node times. */
867
868         TMPFS_NODE_LOCK(node);
869         node->tn_status |= TMPFS_NODE_CHANGED;
870         TMPFS_NODE_UNLOCK(node);
871         tmpfs_update(vp);
872
873         cache_setunresolved(v->a_nch);
874         cache_setvp(v->a_nch, vp);
875         error = 0;
876
877 out:
878         vn_unlock(vp);
879         vn_unlock(dvp);
880
881         return error;
882 }
883
884 /* --------------------------------------------------------------------- */
885
886 static int
887 tmpfs_nrename(struct vop_nrename_args *v)
888 {
889         struct vnode *fdvp = v->a_fdvp;
890         struct namecache *fncp = v->a_fnch->ncp;
891         struct vnode *fvp = fncp->nc_vp;
892         struct vnode *tdvp = v->a_tdvp;
893         struct namecache *tncp = v->a_tnch->ncp;
894         struct vnode *tvp = tncp->nc_vp;
895
896         char *newname;
897         int error;
898         struct tmpfs_dirent *de;
899         struct tmpfs_mount *tmp;
900         struct tmpfs_node *fdnode;
901         struct tmpfs_node *fnode;
902         struct tmpfs_node *tnode;
903         struct tmpfs_node *tdnode;
904
905         vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
906         if(tvp != NULL && tdvp != tvp)
907                 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
908
909         tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
910
911         /* Disallow cross-device renames.
912          * XXX Why isn't this done by the caller? */
913         if (fvp->v_mount != tdvp->v_mount ||
914             (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
915                 error = EXDEV;
916                 goto out;
917         }
918
919         tmp = VFS_TO_TMPFS(tdvp->v_mount);
920         tdnode = VP_TO_TMPFS_DIR(tdvp);
921
922         /* If source and target are the same file, there is nothing to do. */
923         if (fvp == tvp) {
924                 error = 0;
925                 goto out;
926         }
927
928         /* If we need to move the directory between entries, lock the
929          * source so that we can safely operate on it. */
930         if (tdvp != fdvp) {
931                 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
932                 if (error != 0)
933                         goto out;
934         }
935         fdnode = VP_TO_TMPFS_DIR(fdvp);
936         fnode = VP_TO_TMPFS_NODE(fvp);
937         de = tmpfs_dir_lookup(fdnode, fnode, fncp);
938
939         /* Avoid manipulating '.' and '..' entries. */
940         if (de == NULL) {
941                 error = ENOENT;
942                 goto out_locked;
943         }
944         KKASSERT(de->td_node == fnode);
945
946         /* If re-naming a directory to another preexisting directory
947          * ensure that the target directory is empty so that its
948          * removal causes no side effects.
949          * Kern_rename gurantees the destination to be a directory
950          * if the source is one. */
951         if (tvp != NULL) {
952                 KKASSERT(tnode != NULL);
953
954                 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
955                     (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
956                         error = EPERM;
957                         goto out_locked;
958                 }
959
960                 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
961                         if (tnode->tn_size > 0) {
962                                 error = ENOTEMPTY;
963                                 goto out_locked;
964                         }
965                 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
966                         error = ENOTDIR;
967                         goto out_locked;
968                 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
969                         error = EISDIR;
970                         goto out_locked;
971                 } else {
972                         KKASSERT(fnode->tn_type != VDIR &&
973                                 tnode->tn_type != VDIR);
974                 }
975         }
976
977         if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))
978             || (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
979                 error = EPERM;
980                 goto out_locked;
981         }
982
983         /* Ensure that we have enough memory to hold the new name, if it
984          * has to be changed. */
985         if (fncp->nc_nlen != tncp->nc_nlen ||
986             bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
987                 newname = kmalloc(tncp->nc_nlen, M_TMPFSNAME, M_WAITOK);
988         } else
989                 newname = NULL;
990
991         /* If the node is being moved to another directory, we have to do
992          * the move. */
993         if (fdnode != tdnode) {
994                 /* In case we are moving a directory, we have to adjust its
995                  * parent to point to the new parent. */
996                 if (de->td_node->tn_type == VDIR) {
997                         struct tmpfs_node *n;
998
999                         /* Ensure the target directory is not a child of the
1000                          * directory being moved.  Otherwise, we'd end up
1001                          * with stale nodes. */
1002                         n = tdnode;
1003                         /* TMPFS_LOCK garanties that no nodes are freed while
1004                          * traversing the list. Nodes can only be marked as
1005                          * removed: tn_parent == NULL. */
1006                         TMPFS_LOCK(tmp);
1007                         TMPFS_NODE_LOCK(n);
1008                         while (n != n->tn_dir.tn_parent) {
1009                                 struct tmpfs_node *parent;
1010
1011                                 if (n == fnode) {
1012                                         TMPFS_NODE_UNLOCK(n);
1013                                         TMPFS_UNLOCK(tmp);
1014                                         error = EINVAL;
1015                                         if (newname != NULL)
1016                                                     kfree(newname, M_TMPFSNAME);
1017                                         goto out_locked;
1018                                 }
1019                                 parent = n->tn_dir.tn_parent;
1020                                 if (parent == NULL) {
1021                                         n = NULL;
1022                                         break;
1023                                 }
1024                                 TMPFS_NODE_LOCK(parent);
1025                                 if (parent->tn_dir.tn_parent == NULL) {
1026                                         TMPFS_NODE_UNLOCK(parent);
1027                                         n = NULL;
1028                                         break;
1029                                 }
1030                                 n = parent;
1031                         }
1032                         TMPFS_NODE_UNLOCK(n);
1033                         TMPFS_UNLOCK(tmp);
1034                         if (n == NULL) {
1035                                 error = EINVAL;
1036                                 if (newname != NULL)
1037                                             kfree(newname, M_TMPFSNAME);
1038                                 goto out_locked;
1039                         }
1040
1041                         /* Adjust the parent pointer. */
1042                         TMPFS_VALIDATE_DIR(fnode);
1043                         TMPFS_NODE_LOCK(de->td_node);
1044                         de->td_node->tn_dir.tn_parent = tdnode;
1045
1046                         /* As a result of changing the target of the '..'
1047                          * entry, the link count of the source and target
1048                          * directories has to be adjusted. */
1049                         TMPFS_NODE_LOCK(tdnode);
1050                         TMPFS_ASSERT_LOCKED(tdnode);
1051                         TMPFS_NODE_LOCK(fdnode);
1052                         TMPFS_ASSERT_LOCKED(fdnode);
1053
1054                         tdnode->tn_links++;
1055                         fdnode->tn_links--;
1056
1057                         TMPFS_NODE_UNLOCK(fdnode);
1058                         TMPFS_NODE_UNLOCK(tdnode);
1059                         TMPFS_NODE_UNLOCK(de->td_node);
1060                 }
1061
1062                 /* Do the move: just remove the entry from the source directory
1063                  * and insert it into the target one. */
1064                 tmpfs_dir_detach(fdvp, de);
1065                 tmpfs_dir_attach(tdvp, de);
1066         }
1067
1068         /* If the name has changed, we need to make it effective by changing
1069          * it in the directory entry. */
1070         if (newname != NULL) {
1071
1072                 kfree(de->td_name, M_TMPFSNAME);
1073                 de->td_namelen = (uint16_t)tncp->nc_nlen;
1074                 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
1075                 newname[tncp->nc_nlen] = '\0';
1076                 de->td_name = newname;
1077
1078                 TMPFS_NODE_LOCK(tdnode);
1079                 TMPFS_NODE_LOCK(fdnode);
1080
1081                 fnode->tn_status |= TMPFS_NODE_CHANGED;
1082                 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
1083
1084                 TMPFS_NODE_UNLOCK(fdnode);
1085                 TMPFS_NODE_UNLOCK(tdnode);
1086         }
1087
1088         /* If we are overwriting an entry, we have to remove the old one
1089          * from the target directory. */
1090         if (tvp != NULL) {
1091                 /* Remove the old entry from the target directory. */
1092                 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
1093                 tmpfs_dir_detach(tdvp, de);
1094
1095                 /* Free the directory entry we just deleted.  Note that the
1096                  * node referred by it will not be removed until the vnode is
1097                  * really reclaimed. */
1098                 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de, TRUE);
1099
1100                 cache_inval_vp(tvp, CINV_DESTROY);
1101         }
1102
1103         cache_rename(v->a_fnch, v->a_tnch);
1104         error = 0;
1105
1106 out_locked:
1107         if (fdnode != tdnode)
1108                 vn_unlock(fdvp);
1109
1110 out:
1111         /* Release target nodes. */
1112         /* XXX: I don't understand when tdvp can be the same as tvp, but
1113          * other code takes care of this... */
1114         if (tdvp == tvp)
1115                 vrele(tdvp);
1116         else {
1117                 if (tvp != NULL)
1118                         vn_unlock(tvp);
1119                 vn_unlock(tdvp);
1120         }
1121
1122         return error;
1123 }
1124
1125 /* --------------------------------------------------------------------- */
1126
1127 static int
1128 tmpfs_nmkdir(struct vop_nmkdir_args *v)
1129 {
1130         struct vnode *dvp = v->a_dvp;
1131         struct vnode **vpp = v->a_vpp;
1132         struct namecache *ncp = v->a_nch->ncp;
1133         struct vattr *vap = v->a_vap;
1134         struct ucred *cred = v->a_cred;
1135         int error;
1136
1137         KKASSERT(vap->va_type == VDIR);
1138
1139         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1140         error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1141         if (error == 0) {
1142                 cache_setunresolved(v->a_nch);
1143                 cache_setvp(v->a_nch, *vpp);
1144         }
1145         vn_unlock(dvp);
1146
1147         return error;
1148 }
1149
1150 /* --------------------------------------------------------------------- */
1151
1152 static int
1153 tmpfs_nrmdir(struct vop_nrmdir_args *v)
1154 {
1155         struct vnode *dvp = v->a_dvp;
1156         struct namecache *ncp = v->a_nch->ncp;
1157         struct vnode *vp = ncp->nc_vp;
1158
1159         int error;
1160         struct tmpfs_dirent *de;
1161         struct tmpfs_mount *tmp;
1162         struct tmpfs_node *dnode;
1163         struct tmpfs_node *node;
1164
1165         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1166         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1167
1168         tmp = VFS_TO_TMPFS(dvp->v_mount);
1169         dnode = VP_TO_TMPFS_DIR(dvp);
1170         node = VP_TO_TMPFS_DIR(vp);
1171
1172         /* Directories with more than two entries ('.' and '..') cannot be
1173          * removed. */
1174          if (node->tn_size > 0) {
1175                  error = ENOTEMPTY;
1176                  goto out;
1177          }
1178
1179         if ((dnode->tn_flags & APPEND)
1180             || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1181                 error = EPERM;
1182                 goto out;
1183         }
1184
1185         /* This invariant holds only if we are not trying to remove "..".
1186           * We checked for that above so this is safe now. */
1187         KKASSERT(node->tn_dir.tn_parent == dnode);
1188
1189         /* Get the directory entry associated with node (vp).  This was
1190          * filled by tmpfs_lookup while looking up the entry. */
1191         de = tmpfs_dir_lookup(dnode, node, ncp);
1192         KKASSERT(TMPFS_DIRENT_MATCHES(de,
1193             ncp->nc_name,
1194             ncp->nc_nlen));
1195
1196         /* Check flags to see if we are allowed to remove the directory. */
1197         if (dnode->tn_flags & APPEND
1198                 || node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
1199                 error = EPERM;
1200                 goto out;
1201         }
1202
1203
1204         /* Detach the directory entry from the directory (dnode). */
1205         tmpfs_dir_detach(dvp, de);
1206
1207         /* No vnode should be allocated for this entry from this point */
1208         TMPFS_NODE_LOCK(node);
1209         TMPFS_ASSERT_ELOCKED(node);
1210         TMPFS_NODE_LOCK(dnode);
1211         TMPFS_ASSERT_ELOCKED(dnode);
1212
1213         node->tn_links--;
1214         node->tn_dir.tn_parent = NULL;
1215         node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1216             TMPFS_NODE_MODIFIED;
1217
1218         dnode->tn_links--;
1219         dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1220             TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1221
1222         TMPFS_NODE_UNLOCK(dnode);
1223         TMPFS_NODE_UNLOCK(node);
1224
1225         /* Free the directory entry we just deleted.  Note that the node
1226          * referred by it will not be removed until the vnode is really
1227          * reclaimed. */
1228         tmpfs_free_dirent(tmp, de, TRUE);
1229
1230         /* Release the deleted vnode (will destroy the node, notify
1231          * interested parties and clean it from the cache). */
1232
1233         TMPFS_NODE_LOCK(dnode);
1234         dnode->tn_status |= TMPFS_NODE_CHANGED;
1235         TMPFS_NODE_UNLOCK(dnode);
1236         tmpfs_update(dvp);
1237
1238         cache_setunresolved(v->a_nch);
1239         cache_setvp(v->a_nch, NULL);
1240         cache_inval_vp(vp, CINV_DESTROY);
1241         error = 0;
1242
1243 out:
1244         vn_unlock(vp);
1245         vn_unlock(dvp);
1246
1247         return error;
1248 }
1249
1250 /* --------------------------------------------------------------------- */
1251
1252 static int
1253 tmpfs_nsymlink(struct vop_nsymlink_args *v)
1254 {
1255         struct vnode *dvp = v->a_dvp;
1256         struct vnode **vpp = v->a_vpp;
1257         struct namecache *ncp = v->a_nch->ncp;
1258         struct vattr *vap = v->a_vap;
1259         struct ucred *cred = v->a_cred;
1260         char *target = v->a_target;
1261         int error;
1262
1263         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1264         vap->va_type = VLNK;
1265         error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1266         if (error == 0) {
1267                 cache_setunresolved(v->a_nch);
1268                 cache_setvp(v->a_nch, *vpp);
1269         }
1270         vn_unlock(dvp);
1271
1272         return error;
1273 }
1274
1275 /* --------------------------------------------------------------------- */
1276
1277 static int
1278 tmpfs_readdir(struct vop_readdir_args *v)
1279 {
1280         struct vnode *vp = v->a_vp;
1281         struct uio *uio = v->a_uio;
1282         int *eofflag = v->a_eofflag;
1283         off_t **cookies = v->a_cookies;
1284         int *ncookies = v->a_ncookies;
1285
1286         int error;
1287         off_t startoff;
1288         off_t cnt = 0;
1289         struct tmpfs_node *node;
1290
1291         /* This operation only makes sense on directory nodes. */
1292         if (vp->v_type != VDIR)
1293                 return ENOTDIR;
1294
1295         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1296
1297         node = VP_TO_TMPFS_DIR(vp);
1298         startoff = uio->uio_offset;
1299
1300         if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1301                 error = tmpfs_dir_getdotdent(node, uio);
1302                 if (error != 0)
1303                         goto outok;
1304                 cnt++;
1305         }
1306
1307         if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1308                 error = tmpfs_dir_getdotdotdent(node, uio);
1309                 if (error != 0)
1310                         goto outok;
1311                 cnt++;
1312         }
1313
1314         error = tmpfs_dir_getdents(node, uio, &cnt);
1315
1316 outok:
1317         KKASSERT(error >= -1);
1318
1319         if (error == -1)
1320                 error = 0;
1321
1322         if (eofflag != NULL)
1323                 *eofflag =
1324                     (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1325
1326         /* Update NFS-related variables. */
1327         if (error == 0 && cookies != NULL && ncookies != NULL) {
1328                 off_t i;
1329                 off_t off = startoff;
1330                 struct tmpfs_dirent *de = NULL;
1331
1332                 *ncookies = cnt;
1333                 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1334
1335                 for (i = 0; i < cnt; i++) {
1336                         KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1337                         if (off == TMPFS_DIRCOOKIE_DOT) {
1338                                 off = TMPFS_DIRCOOKIE_DOTDOT;
1339                         } else {
1340                                 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1341                                         de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1342                                 } else if (de != NULL) {
1343                                         de = TAILQ_NEXT(de, td_entries);
1344                                 } else {
1345                                         de = tmpfs_dir_lookupbycookie(node,
1346                                             off);
1347                                         KKASSERT(de != NULL);
1348                                         de = TAILQ_NEXT(de, td_entries);
1349                                 }
1350                                 if (de == NULL)
1351                                         off = TMPFS_DIRCOOKIE_EOF;
1352                                 else
1353                                         off = tmpfs_dircookie(de);
1354                         }
1355
1356                         (*cookies)[i] = off;
1357                 }
1358                 KKASSERT(uio->uio_offset == off);
1359         }
1360         vn_unlock(vp);
1361
1362         return error;
1363 }
1364
1365 /* --------------------------------------------------------------------- */
1366
1367 static int
1368 tmpfs_readlink(struct vop_readlink_args *v)
1369 {
1370         struct vnode *vp = v->a_vp;
1371         struct uio *uio = v->a_uio;
1372
1373         int error;
1374         struct tmpfs_node *node;
1375
1376         KKASSERT(uio->uio_offset == 0);
1377         KKASSERT(vp->v_type == VLNK);
1378
1379         node = VP_TO_TMPFS_NODE(vp);
1380
1381         error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1382             uio);
1383         TMPFS_NODE_LOCK(node);
1384         node->tn_status |= TMPFS_NODE_ACCESSED;
1385         TMPFS_NODE_UNLOCK(node);
1386
1387         return error;
1388 }
1389
1390 /* --------------------------------------------------------------------- */
1391
1392 static int
1393 tmpfs_inactive(struct vop_inactive_args *v)
1394 {
1395         struct vnode *vp = v->a_vp;
1396
1397         struct tmpfs_node *node;
1398
1399         KKASSERT(vn_islocked(vp));
1400
1401         node = VP_TO_TMPFS_NODE(vp);
1402
1403         TMPFS_NODE_LOCK(node);
1404         if (node->tn_links == 0 &&
1405             (node->tn_vpstate & TMPFS_VNODE_DOOMED)) {
1406                 TMPFS_NODE_UNLOCK(node);
1407                 vrecycle(vp);
1408         }
1409         else
1410                 TMPFS_NODE_UNLOCK(node);
1411
1412         return 0;
1413 }
1414
1415 /* --------------------------------------------------------------------- */
1416
1417 int
1418 tmpfs_reclaim(struct vop_reclaim_args *v)
1419 {
1420         struct vnode *vp = v->a_vp;
1421
1422         struct tmpfs_mount *tmp;
1423         struct tmpfs_node *node;
1424
1425         node = VP_TO_TMPFS_NODE(vp);
1426         tmp = VFS_TO_TMPFS(vp->v_mount);
1427
1428         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1429         tmpfs_free_vp(vp);
1430
1431         /* If the node referenced by this vnode was deleted by the user,
1432          * we must free its associated data structures (now that the vnode
1433          * is being reclaimed). */
1434         TMPFS_NODE_LOCK(node);
1435         if (node->tn_links == 0 &&
1436             (node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0) {
1437                 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1438                 TMPFS_NODE_UNLOCK(node);
1439                 tmpfs_free_node(tmp, node);
1440         }
1441         else
1442                 TMPFS_NODE_UNLOCK(node);
1443
1444         vn_unlock(vp);
1445
1446         KKASSERT(vp->v_data == NULL);
1447         return 0;
1448 }
1449
1450 /* --------------------------------------------------------------------- */
1451
1452 static int
1453 tmpfs_print(struct vop_print_args *v)
1454 {
1455         struct vnode *vp = v->a_vp;
1456
1457         struct tmpfs_node *node;
1458
1459         node = VP_TO_TMPFS_NODE(vp);
1460
1461         kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1462             node, node->tn_flags, node->tn_links);
1463         kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1464             node->tn_mode, node->tn_uid, node->tn_gid,
1465             (uintmax_t)node->tn_size, node->tn_status);
1466
1467         if (vp->v_type == VFIFO)
1468                 fifo_printinfo(vp);
1469
1470         kprintf("\n");
1471
1472         return 0;
1473 }
1474
1475 /* --------------------------------------------------------------------- */
1476
1477 static int
1478 tmpfs_pathconf(struct vop_pathconf_args *v)
1479 {
1480         int name = v->a_name;
1481         register_t *retval = v->a_retval;
1482
1483         int error;
1484
1485         error = 0;
1486
1487         switch (name) {
1488         case _PC_LINK_MAX:
1489                 *retval = LINK_MAX;
1490                 break;
1491
1492         case _PC_NAME_MAX:
1493                 *retval = NAME_MAX;
1494                 break;
1495
1496         case _PC_PATH_MAX:
1497                 *retval = PATH_MAX;
1498                 break;
1499
1500         case _PC_PIPE_BUF:
1501                 *retval = PIPE_BUF;
1502                 break;
1503
1504         case _PC_CHOWN_RESTRICTED:
1505                 *retval = 1;
1506                 break;
1507
1508         case _PC_NO_TRUNC:
1509                 *retval = 1;
1510                 break;
1511
1512         case _PC_SYNC_IO:
1513                 *retval = 1;
1514                 break;
1515
1516         case _PC_FILESIZEBITS:
1517                 *retval = 0; /* XXX Don't know which value should I return. */
1518                 break;
1519
1520         default:
1521                 error = EINVAL;
1522         }
1523
1524         return error;
1525 }
1526
1527 /* --------------------------------------------------------------------- */
1528
1529 /*
1530  * vnode operations vector used for files stored in a tmpfs file system.
1531  */
1532 struct vop_ops tmpfs_vnode_vops = {
1533         .vop_default =                  vop_defaultop,
1534         .vop_getpages =                 vop_stdgetpages,
1535         .vop_putpages =                 vop_stdputpages,
1536         .vop_ncreate =                  tmpfs_ncreate,
1537         .vop_nresolve =                 tmpfs_nresolve,
1538         .vop_nlookupdotdot =            tmpfs_nlookupdotdot,
1539         .vop_nmknod =                   tmpfs_nmknod,
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_nremove =                  tmpfs_nremove,
1549         .vop_nlink =                    tmpfs_nlink,
1550         .vop_nrename =                  tmpfs_nrename,
1551         .vop_nmkdir =                   tmpfs_nmkdir,
1552         .vop_nrmdir =                   tmpfs_nrmdir,
1553         .vop_nsymlink =                 tmpfs_nsymlink,
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_bmap =                     tmpfs_bmap,
1561         .vop_bmap =                     (void *)vop_eopnotsupp,
1562         .vop_strategy =                 tmpfs_strategy,
1563         .vop_advlock =                  tmpfs_advlock,
1564 };