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