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