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