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/caps.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
tmpfs_knote(struct vnode * vp,int flags)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
tmpfs_nresolve(struct vop_nresolve_args * ap)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
tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args * ap)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
tmpfs_ncreate(struct vop_ncreate_args * ap)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
tmpfs_nmknod(struct vop_nmknod_args * ap)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
tmpfs_open(struct vop_open_args * ap)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
tmpfs_close(struct vop_close_args * ap)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
tmpfs_access(struct vop_access_args * ap)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
tmpfs_getattr(struct vop_getattr_args * ap)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
tmpfs_getattr_lite(struct vop_getattr_lite_args * ap)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
tmpfs_setattr(struct vop_setattr_args * ap)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
tmpfs_fsync(struct vop_fsync_args * ap)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
tmpfs_read(struct vop_read_args * ap)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
tmpfs_write(struct vop_write_args * ap)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_paging_min_dnc(vm_page_free_hysteresis))
856 bp->b_flags |= B_DIRECT | B_TTC;
857 if (vm_pages_needed || vm_paging_start(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_paging_min() ||
870 ((vm_pages_needed || vm_paging_start(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 (caps_priv_check(ap->a_cred, SYSCAP_NOVFS_RETAINSUGID))
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
tmpfs_advlock(struct vop_advlock_args * ap)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
tmpfs_strategy(struct vop_strategy_args * ap)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
tmpfs_strategy_done(struct bio * bio)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
tmpfs_bmap(struct vop_bmap_args * ap)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
tmpfs_nremove(struct vop_nremove_args * ap)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
tmpfs_nlink(struct vop_nlink_args * ap)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
tmpfs_nrename(struct vop_nrename_args * ap)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
tmpfs_nmkdir(struct vop_nmkdir_args * ap)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
tmpfs_nrmdir(struct vop_nrmdir_args * ap)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(vp, NOTE_DELETE);
1602 tmpfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
1603 vrele(vp);
1604 return 0;
1605
1606 out_locked:
1607 TMPFS_NODE_UNLOCK(node);
1608 TMPFS_NODE_UNLOCK(dnode);
1609
1610 out:
1611 vrele(vp);
1612
1613 return error;
1614 }
1615
1616 /* --------------------------------------------------------------------- */
1617
1618 static int
tmpfs_nsymlink(struct vop_nsymlink_args * ap)1619 tmpfs_nsymlink(struct vop_nsymlink_args *ap)
1620 {
1621 struct vnode *dvp = ap->a_dvp;
1622 struct vnode **vpp = ap->a_vpp;
1623 struct namecache *ncp = ap->a_nch->ncp;
1624 struct vattr *vap = ap->a_vap;
1625 struct ucred *cred = ap->a_cred;
1626 char *target = ap->a_target;
1627 int error;
1628
1629 vap->va_type = VLNK;
1630 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1631 if (error == 0) {
1632 tmpfs_knote(*vpp, NOTE_WRITE);
1633 cache_setunresolved(ap->a_nch);
1634 cache_setvp(ap->a_nch, *vpp);
1635 }
1636 return error;
1637 }
1638
1639 /* --------------------------------------------------------------------- */
1640
1641 static int
tmpfs_readdir(struct vop_readdir_args * ap)1642 tmpfs_readdir(struct vop_readdir_args *ap)
1643 {
1644 struct vnode *vp = ap->a_vp;
1645 struct uio *uio = ap->a_uio;
1646 int *eofflag = ap->a_eofflag;
1647 off_t **cookies = ap->a_cookies;
1648 int *ncookies = ap->a_ncookies;
1649 struct tmpfs_mount *tmp;
1650 int error;
1651 off_t startoff;
1652 off_t cnt = 0;
1653 struct tmpfs_node *node;
1654
1655 /* This operation only makes sense on directory nodes. */
1656 if (vp->v_type != VDIR) {
1657 return ENOTDIR;
1658 }
1659
1660 tmp = VFS_TO_TMPFS(vp->v_mount);
1661 node = VP_TO_TMPFS_DIR(vp);
1662 startoff = uio->uio_offset;
1663
1664 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1665 error = tmpfs_dir_getdotdent(node, uio);
1666 if (error && error != EINVAL) {
1667 TMPFS_NODE_LOCK_SH(node);
1668 goto outok;
1669 }
1670 cnt++;
1671 }
1672
1673 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1674 /* may lock parent, cannot hold node lock */
1675 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
1676 if (error && error != EINVAL) {
1677 TMPFS_NODE_LOCK_SH(node);
1678 goto outok;
1679 }
1680 cnt++;
1681 }
1682
1683 TMPFS_NODE_LOCK_SH(node);
1684 error = tmpfs_dir_getdents(node, uio, &cnt);
1685
1686 outok:
1687 KKASSERT(error >= -1);
1688
1689 if (error == -1)
1690 error = 0;
1691
1692 if (eofflag != NULL)
1693 *eofflag =
1694 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1695
1696 /* Update NFS-related variables. */
1697 if (error == 0 && cookies != NULL && ncookies != NULL) {
1698 off_t i;
1699 off_t off = startoff;
1700 struct tmpfs_dirent *de = NULL;
1701
1702 *ncookies = cnt;
1703 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1704
1705 for (i = 0; i < cnt; i++) {
1706 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1707 if (off == TMPFS_DIRCOOKIE_DOT) {
1708 off = TMPFS_DIRCOOKIE_DOTDOT;
1709 } else {
1710 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1711 de = RB_MIN(tmpfs_dirtree_cookie,
1712 &node->tn_dir.tn_cookietree);
1713 } else if (de != NULL) {
1714 de = RB_NEXT(tmpfs_dirtree_cookie,
1715 &node->tn_dir.tn_cookietree, de);
1716 } else {
1717 de = tmpfs_dir_lookupbycookie(node, off,
1718 1);
1719 KKASSERT(de != NULL);
1720 de = RB_NEXT(tmpfs_dirtree_cookie,
1721 &node->tn_dir.tn_cookietree, de);
1722 }
1723 if (de == NULL)
1724 off = TMPFS_DIRCOOKIE_EOF;
1725 else
1726 off = tmpfs_dircookie(de);
1727 }
1728 (*cookies)[i] = off;
1729 }
1730 KKASSERT(uio->uio_offset == off);
1731 }
1732 TMPFS_NODE_UNLOCK(node);
1733
1734 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1735 TMPFS_NODE_LOCK(node);
1736 node->tn_status |= TMPFS_NODE_ACCESSED;
1737 TMPFS_NODE_UNLOCK(node);
1738 }
1739 return error;
1740 }
1741
1742 /* --------------------------------------------------------------------- */
1743
1744 static int
tmpfs_readlink(struct vop_readlink_args * ap)1745 tmpfs_readlink(struct vop_readlink_args *ap)
1746 {
1747 struct vnode *vp = ap->a_vp;
1748 struct uio *uio = ap->a_uio;
1749 int error;
1750 struct tmpfs_node *node;
1751
1752 KKASSERT(uio->uio_offset == 0);
1753 KKASSERT(vp->v_type == VLNK);
1754
1755 node = VP_TO_TMPFS_NODE(vp);
1756 TMPFS_NODE_LOCK_SH(node);
1757 error = uiomove(node->tn_link,
1758 MIN(node->tn_size, uio->uio_resid), uio);
1759 TMPFS_NODE_UNLOCK(node);
1760 if ((node->tn_status & TMPFS_NODE_ACCESSED) == 0) {
1761 TMPFS_NODE_LOCK(node);
1762 node->tn_status |= TMPFS_NODE_ACCESSED;
1763 TMPFS_NODE_UNLOCK(node);
1764 }
1765 return error;
1766 }
1767
1768 /* --------------------------------------------------------------------- */
1769
1770 static int
tmpfs_inactive(struct vop_inactive_args * ap)1771 tmpfs_inactive(struct vop_inactive_args *ap)
1772 {
1773 struct vnode *vp = ap->a_vp;
1774 struct tmpfs_node *node;
1775 struct mount *mp;
1776
1777 mp = vp->v_mount;
1778 lwkt_gettoken(&mp->mnt_token);
1779 node = VP_TO_TMPFS_NODE(vp);
1780
1781 /*
1782 * Degenerate case
1783 */
1784 if (node == NULL) {
1785 vrecycle(vp);
1786 lwkt_reltoken(&mp->mnt_token);
1787 return(0);
1788 }
1789
1790 /*
1791 * Get rid of unreferenced deleted vnodes sooner rather than
1792 * later so the data memory can be recovered immediately.
1793 *
1794 * We must truncate the vnode to prevent the normal reclamation
1795 * path from flushing the data for the removed file to disk.
1796 */
1797 TMPFS_NODE_LOCK(node);
1798 if (node->tn_links == 0) {
1799 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1800 TMPFS_NODE_UNLOCK(node);
1801 if (node->tn_type == VREG)
1802 tmpfs_truncate(vp, 0);
1803 vrecycle(vp);
1804 } else {
1805 /*
1806 * We must retain any VM pages belonging to the vnode's
1807 * object as the vnode will destroy the object during a
1808 * later reclaim. We call vinvalbuf(V_SAVE) to clean
1809 * out the buffer cache.
1810 *
1811 * On DragonFlyBSD, vnodes are not immediately deactivated
1812 * on the 1->0 refs, so this is a relatively optimal
1813 * operation. We have to do this in tmpfs_inactive()
1814 * because the pages will have already been thrown away
1815 * at the time tmpfs_reclaim() is called.
1816 */
1817 if (node->tn_type == VREG &&
1818 node->tn_reg.tn_pages_in_aobj == 0) {
1819 vinvalbuf(vp, V_SAVE, 0, 0);
1820 KKASSERT(RB_EMPTY(&vp->v_rbdirty_tree));
1821 KKASSERT(RB_EMPTY(&vp->v_rbclean_tree));
1822 tmpfs_move_pages(vp->v_object, node->tn_reg.tn_aobj,
1823 TMPFS_MOVF_DEACTIVATE);
1824 node->tn_reg.tn_pages_in_aobj = 1;
1825 }
1826
1827 TMPFS_NODE_UNLOCK(node);
1828 }
1829 lwkt_reltoken(&mp->mnt_token);
1830
1831 return 0;
1832 }
1833
1834 /* --------------------------------------------------------------------- */
1835
1836 int
tmpfs_reclaim(struct vop_reclaim_args * ap)1837 tmpfs_reclaim(struct vop_reclaim_args *ap)
1838 {
1839 struct vnode *vp = ap->a_vp;
1840 struct tmpfs_mount *tmp;
1841 struct tmpfs_node *node;
1842 struct mount *mp;
1843
1844 mp = vp->v_mount;
1845 lwkt_gettoken(&mp->mnt_token);
1846
1847 node = VP_TO_TMPFS_NODE(vp);
1848 tmp = VFS_TO_TMPFS(vp->v_mount);
1849 KKASSERT(mp == tmp->tm_mount);
1850
1851 TMPFS_NODE_LOCK(node);
1852 KKASSERT(node->tn_vnode == vp);
1853 node->tn_vnode = NULL;
1854 vp->v_data = NULL;
1855
1856 /*
1857 * If the node referenced by this vnode was deleted by the
1858 * user, we must free its associated data structures now that
1859 * the vnode is being reclaimed.
1860 *
1861 * Directories have an extra link ref.
1862 */
1863 if (node->tn_links == 0) {
1864 node->tn_vpstate = TMPFS_VNODE_DOOMED;
1865 tmpfs_free_node(tmp, node);
1866 /* eats the lock */
1867 } else {
1868 TMPFS_NODE_UNLOCK(node);
1869 }
1870 lwkt_reltoken(&mp->mnt_token);
1871
1872 KKASSERT(vp->v_data == NULL);
1873 return 0;
1874 }
1875
1876 /* --------------------------------------------------------------------- */
1877
1878 static int
tmpfs_mountctl(struct vop_mountctl_args * ap)1879 tmpfs_mountctl(struct vop_mountctl_args *ap)
1880 {
1881 struct tmpfs_mount *tmp;
1882 struct mount *mp;
1883 int rc;
1884
1885 mp = ap->a_head.a_ops->head.vv_mount;
1886 lwkt_gettoken(&mp->mnt_token);
1887
1888 switch (ap->a_op) {
1889 case (MOUNTCTL_SET_EXPORT):
1890 tmp = (struct tmpfs_mount *) mp->mnt_data;
1891
1892 if (ap->a_ctllen != sizeof(struct export_args))
1893 rc = (EINVAL);
1894 else
1895 rc = vfs_export(mp, &tmp->tm_export,
1896 (const struct export_args *) ap->a_ctl);
1897 break;
1898 default:
1899 rc = vop_stdmountctl(ap);
1900 break;
1901 }
1902
1903 lwkt_reltoken(&mp->mnt_token);
1904 return (rc);
1905 }
1906
1907 /* --------------------------------------------------------------------- */
1908
1909 static int
tmpfs_print(struct vop_print_args * ap)1910 tmpfs_print(struct vop_print_args *ap)
1911 {
1912 struct vnode *vp = ap->a_vp;
1913
1914 struct tmpfs_node *node;
1915
1916 node = VP_TO_TMPFS_NODE(vp);
1917
1918 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1919 node, node->tn_flags, node->tn_links);
1920 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1921 node->tn_mode, node->tn_uid, node->tn_gid,
1922 (uintmax_t)node->tn_size, node->tn_status);
1923
1924 if (vp->v_type == VFIFO)
1925 fifo_printinfo(vp);
1926
1927 kprintf("\n");
1928
1929 return 0;
1930 }
1931
1932 /* --------------------------------------------------------------------- */
1933
1934 static int
tmpfs_pathconf(struct vop_pathconf_args * ap)1935 tmpfs_pathconf(struct vop_pathconf_args *ap)
1936 {
1937 struct vnode *vp = ap->a_vp;
1938 int name = ap->a_name;
1939 register_t *retval = ap->a_retval;
1940 struct tmpfs_mount *tmp;
1941 int error;
1942
1943 error = 0;
1944
1945 switch (name) {
1946 case _PC_CHOWN_RESTRICTED:
1947 *retval = 1;
1948 break;
1949
1950 case _PC_FILESIZEBITS:
1951 tmp = VFS_TO_TMPFS(vp->v_mount);
1952 *retval = max(32, flsll(tmp->tm_pages_max * PAGE_SIZE) + 1);
1953 break;
1954
1955 case _PC_LINK_MAX:
1956 *retval = LINK_MAX;
1957 break;
1958
1959 case _PC_NAME_MAX:
1960 *retval = NAME_MAX;
1961 break;
1962
1963 case _PC_NO_TRUNC:
1964 *retval = 1;
1965 break;
1966
1967 case _PC_PATH_MAX:
1968 *retval = PATH_MAX;
1969 break;
1970
1971 case _PC_PIPE_BUF:
1972 *retval = PIPE_BUF;
1973 break;
1974
1975 case _PC_SYNC_IO:
1976 *retval = 1;
1977 break;
1978
1979 case _PC_2_SYMLINKS:
1980 *retval = 1;
1981 break;
1982
1983 default:
1984 error = EINVAL;
1985 }
1986
1987 return error;
1988 }
1989
1990 /************************************************************************
1991 * KQFILTER OPS *
1992 ************************************************************************/
1993
1994 static void filt_tmpfsdetach(struct knote *kn);
1995 static int filt_tmpfsread(struct knote *kn, long hint);
1996 static int filt_tmpfswrite(struct knote *kn, long hint);
1997 static int filt_tmpfsvnode(struct knote *kn, long hint);
1998
1999 static struct filterops tmpfsread_filtops =
2000 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2001 NULL, filt_tmpfsdetach, filt_tmpfsread };
2002 static struct filterops tmpfswrite_filtops =
2003 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2004 NULL, filt_tmpfsdetach, filt_tmpfswrite };
2005 static struct filterops tmpfsvnode_filtops =
2006 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2007 NULL, filt_tmpfsdetach, filt_tmpfsvnode };
2008
2009 static int
tmpfs_kqfilter(struct vop_kqfilter_args * ap)2010 tmpfs_kqfilter (struct vop_kqfilter_args *ap)
2011 {
2012 struct vnode *vp = ap->a_vp;
2013 struct knote *kn = ap->a_kn;
2014
2015 switch (kn->kn_filter) {
2016 case EVFILT_READ:
2017 kn->kn_fop = &tmpfsread_filtops;
2018 break;
2019 case EVFILT_WRITE:
2020 kn->kn_fop = &tmpfswrite_filtops;
2021 break;
2022 case EVFILT_VNODE:
2023 kn->kn_fop = &tmpfsvnode_filtops;
2024 break;
2025 default:
2026 return (EOPNOTSUPP);
2027 }
2028
2029 kn->kn_hook = (caddr_t)vp;
2030
2031 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2032
2033 return(0);
2034 }
2035
2036 static void
filt_tmpfsdetach(struct knote * kn)2037 filt_tmpfsdetach(struct knote *kn)
2038 {
2039 struct vnode *vp = (void *)kn->kn_hook;
2040
2041 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2042 }
2043
2044 static int
filt_tmpfsread(struct knote * kn,long hint)2045 filt_tmpfsread(struct knote *kn, long hint)
2046 {
2047 struct vnode *vp = (void *)kn->kn_hook;
2048 struct tmpfs_node *node = VP_TO_TMPFS_NODE(vp);
2049 off_t off;
2050
2051 if (hint == NOTE_REVOKE) {
2052 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2053 return(1);
2054 }
2055
2056 /*
2057 * Interlock against MP races when performing this function.
2058 */
2059 TMPFS_NODE_LOCK_SH(node);
2060 off = node->tn_size - kn->kn_fp->f_offset;
2061 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2062 if (kn->kn_sfflags & NOTE_OLDAPI) {
2063 TMPFS_NODE_UNLOCK(node);
2064 return(1);
2065 }
2066 if (kn->kn_data == 0) {
2067 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2068 }
2069 TMPFS_NODE_UNLOCK(node);
2070 return (kn->kn_data != 0);
2071 }
2072
2073 static int
filt_tmpfswrite(struct knote * kn,long hint)2074 filt_tmpfswrite(struct knote *kn, long hint)
2075 {
2076 if (hint == NOTE_REVOKE)
2077 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2078 kn->kn_data = 0;
2079 return (1);
2080 }
2081
2082 static int
filt_tmpfsvnode(struct knote * kn,long hint)2083 filt_tmpfsvnode(struct knote *kn, long hint)
2084 {
2085 if (kn->kn_sfflags & hint)
2086 kn->kn_fflags |= hint;
2087 if (hint == NOTE_REVOKE) {
2088 kn->kn_flags |= (EV_EOF | EV_NODATA);
2089 return (1);
2090 }
2091 return (kn->kn_fflags != 0);
2092 }
2093
2094 /*
2095 * Helper to move VM pages between objects
2096 *
2097 * NOTE: The vm_page_rename() dirties the page, so we can clear the
2098 * PG_NEED_COMMIT flag. If the pages are being moved into tn_aobj,
2099 * the pageout daemon will be able to page them out.
2100 */
2101 static int
tmpfs_move_pages_callback(vm_page_t p,void * data)2102 tmpfs_move_pages_callback(vm_page_t p, void *data)
2103 {
2104 struct rb_vm_page_scan_info *info = data;
2105 vm_pindex_t pindex;
2106
2107 /*
2108 * Take control of the page
2109 */
2110 pindex = p->pindex;
2111 if (vm_page_busy_try(p, TRUE)) {
2112 vm_page_sleep_busy(p, TRUE, "tpgmov");
2113 info->error = -1;
2114 return -1;
2115 }
2116 if (p->object != info->object || p->pindex != pindex) {
2117 vm_page_wakeup(p);
2118 info->error = -1;
2119 return -1;
2120 }
2121
2122 /*
2123 * Make sure the page is not mapped. These flags might also still be
2124 * set heuristically even if we know the page is not mapped and must
2125 * be properly cleaned up.
2126 */
2127 if (__predict_false((p->flags & (PG_MAPPED|PG_WRITEABLE)) != 0))
2128 vm_page_protect(p, VM_PROT_NONE);
2129
2130 /*
2131 * Free or rename the page as appropriate
2132 */
2133 if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) &&
2134 (p->flags & PG_SWAPPED) &&
2135 (p->flags & PG_NEED_COMMIT) == 0 &&
2136 p->dirty == 0) {
2137 /*
2138 * If the page in the backing aobj was paged out to swap
2139 * it will be clean and it is better to free it rather
2140 * than re-dirty it. We will assume that the page was
2141 * paged out to swap for a reason!
2142 *
2143 * This helps avoid unnecessary swap thrashing on the page.
2144 */
2145 vm_page_free(p);
2146 } else if ((info->pagerflags & TMPFS_MOVF_FROMBACKING) == 0 &&
2147 (p->flags & PG_NEED_COMMIT) == 0 &&
2148 p->dirty == 0) {
2149 /*
2150 * If the page associated with the vnode was cleaned via
2151 * a tmpfs_strategy() call, it exists as a swap block in
2152 * aobj and it is again better to free it rather than
2153 * re-dirty it. We will assume that the page was
2154 * paged out to swap for a reason!
2155 *
2156 * This helps avoid unnecessary swap thrashing on the page.
2157 */
2158 vm_page_free(p);
2159 } else {
2160 /*
2161 * Rename the page, which will also ensure that it is flagged
2162 * as dirty and check whether a swap block association exists
2163 * in the target object or not, setting appropriate flags if
2164 * it does.
2165 */
2166 vm_page_rename(p, info->dest_object, pindex);
2167 vm_page_clear_commit(p);
2168 if (info->pagerflags & TMPFS_MOVF_DEACTIVATE)
2169 vm_page_deactivate(p);
2170 vm_page_wakeup(p);
2171 /* page automaticaly made dirty */
2172 }
2173
2174 return 0;
2175 }
2176
2177 static
2178 void
tmpfs_move_pages(vm_object_t src,vm_object_t dst,int movflags)2179 tmpfs_move_pages(vm_object_t src, vm_object_t dst, int movflags)
2180 {
2181 struct rb_vm_page_scan_info info;
2182
2183 vm_object_hold(src);
2184 vm_object_hold(dst);
2185 info.object = src;
2186 info.dest_object = dst;
2187 info.pagerflags = movflags;
2188 do {
2189 if (src->paging_in_progress)
2190 vm_object_pip_wait(src, "objtfs");
2191 info.error = 1;
2192 vm_page_rb_tree_RB_SCAN(&src->rb_memq, NULL,
2193 tmpfs_move_pages_callback, &info);
2194 } while (info.error < 0 || !RB_EMPTY(&src->rb_memq) ||
2195 src->paging_in_progress);
2196 vm_object_drop(dst);
2197 vm_object_drop(src);
2198 }
2199
2200 /* --------------------------------------------------------------------- */
2201
2202 /*
2203 * vnode operations vector used for files stored in a tmpfs file system.
2204 */
2205 struct vop_ops tmpfs_vnode_vops = {
2206 .vop_default = vop_defaultop,
2207 .vop_getpages = vop_stdgetpages,
2208 .vop_putpages = vop_stdputpages,
2209 .vop_ncreate = tmpfs_ncreate,
2210 .vop_nresolve = tmpfs_nresolve,
2211 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
2212 .vop_nmknod = tmpfs_nmknod,
2213 .vop_open = tmpfs_open,
2214 .vop_close = tmpfs_close,
2215 .vop_access = tmpfs_access,
2216 .vop_getattr = tmpfs_getattr,
2217 .vop_getattr_lite = tmpfs_getattr_lite,
2218 .vop_setattr = tmpfs_setattr,
2219 .vop_read = tmpfs_read,
2220 .vop_write = tmpfs_write,
2221 .vop_fsync = tmpfs_fsync,
2222 .vop_mountctl = tmpfs_mountctl,
2223 .vop_nremove = tmpfs_nremove,
2224 .vop_nlink = tmpfs_nlink,
2225 .vop_nrename = tmpfs_nrename,
2226 .vop_nmkdir = tmpfs_nmkdir,
2227 .vop_nrmdir = tmpfs_nrmdir,
2228 .vop_nsymlink = tmpfs_nsymlink,
2229 .vop_readdir = tmpfs_readdir,
2230 .vop_readlink = tmpfs_readlink,
2231 .vop_inactive = tmpfs_inactive,
2232 .vop_reclaim = tmpfs_reclaim,
2233 .vop_print = tmpfs_print,
2234 .vop_pathconf = tmpfs_pathconf,
2235 .vop_bmap = tmpfs_bmap,
2236 .vop_strategy = tmpfs_strategy,
2237 .vop_advlock = tmpfs_advlock,
2238 .vop_kqfilter = tmpfs_kqfilter
2239 };
2240