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