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