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