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