kernel - TMPFS - Stabilization pass, fix assertion in nrmdir
[dragonfly.git] / sys / vfs / tmpfs / tmpfs_vnops.c
CommitLineData
7a2de9a4
MD
1/* $NetBSD: tmpfs_vnops.c,v 1.39 2007/07/23 15:41:01 jmmv Exp $ */
2
3/*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * tmpfs vnode interface.
35 */
36#include <sys/cdefs.h>
37
38#include <sys/kernel.h>
39#include <sys/kern_syscall.h>
40#include <sys/param.h>
41#include <sys/fcntl.h>
42#include <sys/lockf.h>
43#include <sys/priv.h>
44#include <sys/proc.h>
45#include <sys/resourcevar.h>
46#include <sys/sched.h>
47#include <sys/sfbuf.h>
48#include <sys/stat.h>
49#include <sys/systm.h>
50#include <sys/unistd.h>
51#include <sys/vfsops.h>
52#include <sys/vnode.h>
53
54#include <sys/mplock2.h>
55
56#include <vm/vm.h>
57#include <vm/vm_object.h>
58#include <vm/vm_page.h>
59#include <vm/vm_pager.h>
60
61#include <vfs/fifofs/fifo.h>
62#include <vfs/tmpfs/tmpfs_vnops.h>
63#include <vfs/tmpfs/tmpfs.h>
64
65MALLOC_DECLARE(M_TMPFS);
66
67/* --------------------------------------------------------------------- */
68
69static int
70tmpfs_nresolve(struct vop_nresolve_args *v)
71{
72 struct vnode *dvp = v->a_dvp;
73 struct vnode *vp = NULL;
74 struct namecache *ncp = v->a_nch->ncp;
d89ce96a 75 struct tmpfs_node *tnode;
7a2de9a4
MD
76
77 int error;
78 struct tmpfs_dirent *de;
79 struct tmpfs_node *dnode;
80
81 dnode = VP_TO_TMPFS_DIR(dvp);
82
7a2de9a4
MD
83 de = tmpfs_dir_lookup(dnode, NULL, ncp);
84 if (de == NULL) {
d89ce96a 85 error = ENOENT;
7a2de9a4 86 } else {
d89ce96a
MD
87 /*
88 * Allocate a vnode for the node we found.
89 */
7a2de9a4 90 tnode = de->td_node;
7a2de9a4 91 error = tmpfs_alloc_vp(dvp->v_mount, tnode,
d89ce96a
MD
92 LK_EXCLUSIVE | LK_RETRY, &vp);
93 if (error)
7a2de9a4 94 goto out;
d89ce96a 95 KKASSERT(vp);
7a2de9a4
MD
96 }
97
7a2de9a4 98out:
d89ce96a
MD
99 /*
100 * Store the result of this lookup in the cache. Avoid this if the
7a2de9a4 101 * request was for creation, as it does not improve timings on
d89ce96a
MD
102 * emprical tests.
103 */
7a2de9a4
MD
104 if (vp) {
105 vn_unlock(vp);
106 cache_setvp(v->a_nch, vp);
107 vrele(vp);
d89ce96a 108 } else if (error == ENOENT) {
7a2de9a4
MD
109 cache_setvp(v->a_nch, NULL);
110 }
111 return error;
112}
113
114static int
115tmpfs_nlookupdotdot(struct vop_nlookupdotdot_args *v)
116{
117 struct vnode *dvp = v->a_dvp;
118 struct vnode **vpp = v->a_vpp;
119 struct tmpfs_node *dnode = VP_TO_TMPFS_NODE(dvp);
120 struct ucred *cred = v->a_cred;
121 int error;
122
123 *vpp = NULL;
124 /* Check accessibility of requested node as a first step. */
125 error = VOP_ACCESS(dvp, VEXEC, cred);
126 if (error != 0)
127 return error;
128
129 if (dnode->tn_dir.tn_parent != NULL) {
130 /* Allocate a new vnode on the matching entry. */
131 error = tmpfs_alloc_vp(dvp->v_mount, dnode->tn_dir.tn_parent,
132 LK_EXCLUSIVE | LK_RETRY, vpp);
133
134 if (*vpp)
135 vn_unlock(*vpp);
136 }
137
138 return (*vpp == NULL) ? ENOENT : 0;
139}
140
141/* --------------------------------------------------------------------- */
142
143static int
144tmpfs_ncreate(struct vop_ncreate_args *v)
145{
146 struct vnode *dvp = v->a_dvp;
147 struct vnode **vpp = v->a_vpp;
148 struct namecache *ncp = v->a_nch->ncp;
149 struct vattr *vap = v->a_vap;
150 struct ucred *cred = v->a_cred;
151 int error;
152
153 KKASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
154
7a2de9a4
MD
155 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
156 if (error == 0) {
157 cache_setunresolved(v->a_nch);
158 cache_setvp(v->a_nch, *vpp);
159 }
7a2de9a4
MD
160
161 return error;
162}
163/* --------------------------------------------------------------------- */
164
165static int
166tmpfs_nmknod(struct vop_nmknod_args *v)
167{
168 struct vnode *dvp = v->a_dvp;
169 struct vnode **vpp = v->a_vpp;
170 struct namecache *ncp = v->a_nch->ncp;
171 struct vattr *vap = v->a_vap;
172 struct ucred *cred = v->a_cred;
173 int error;
174
175 if (vap->va_type != VBLK && vap->va_type != VCHR &&
176 vap->va_type != VFIFO)
177 return EINVAL;
178
7a2de9a4
MD
179 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
180 if (error == 0) {
181 cache_setunresolved(v->a_nch);
182 cache_setvp(v->a_nch, *vpp);
183 }
7a2de9a4
MD
184
185 return error;
186}
187
188/* --------------------------------------------------------------------- */
189
190static int
191tmpfs_open(struct vop_open_args *v)
192{
193 struct vnode *vp = v->a_vp;
194 int mode = v->a_mode;
195
196 int error;
197 struct tmpfs_node *node;
198
7a2de9a4
MD
199 node = VP_TO_TMPFS_NODE(vp);
200
201 /* The file is still active but all its names have been removed
202 * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
203 * it is about to die. */
204 if (node->tn_links < 1)
205 return (ENOENT);
206
207 /* If the file is marked append-only, deny write requests. */
630e3a33
MD
208 if ((node->tn_flags & APPEND) &&
209 (mode & (FWRITE | O_APPEND)) == FWRITE) {
7a2de9a4 210 error = EPERM;
630e3a33 211 } else {
7a2de9a4
MD
212 return (vop_stdopen(v));
213 }
7a2de9a4
MD
214 return error;
215}
216
217/* --------------------------------------------------------------------- */
218
219static int
220tmpfs_close(struct vop_close_args *v)
221{
222 struct vnode *vp = v->a_vp;
223 struct tmpfs_node *node;
224
225 node = VP_TO_TMPFS_NODE(vp);
226
227 if (node->tn_links > 0) {
228 /* Update node times. No need to do it if the node has
229 * been deleted, because it will vanish after we return. */
230 tmpfs_update(vp);
231 }
232
233 return vop_stdclose(v);
234}
235
236/* --------------------------------------------------------------------- */
237
238int
239tmpfs_access(struct vop_access_args *v)
240{
241 struct vnode *vp = v->a_vp;
242 int error;
243 struct tmpfs_node *node;
244
7a2de9a4
MD
245 node = VP_TO_TMPFS_NODE(vp);
246
247 switch (vp->v_type) {
248 case VDIR:
249 /* FALLTHROUGH */
250 case VLNK:
251 /* FALLTHROUGH */
252 case VREG:
253 if (VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
254 error = EROFS;
255 goto out;
256 }
257 break;
258
259 case VBLK:
260 /* FALLTHROUGH */
261 case VCHR:
262 /* FALLTHROUGH */
263 case VSOCK:
264 /* FALLTHROUGH */
265 case VFIFO:
266 break;
267
268 default:
269 error = EINVAL;
270 goto out;
271 }
272
273 if (VWRITE && node->tn_flags & IMMUTABLE) {
274 error = EPERM;
275 goto out;
276 }
277
278 error = vop_helper_access(v, node->tn_uid, node->tn_gid, node->tn_mode, 0);
279
280out:
281
282 return error;
283}
284
285/* --------------------------------------------------------------------- */
286
287int
288tmpfs_getattr(struct vop_getattr_args *v)
289{
290 struct vnode *vp = v->a_vp;
291 struct vattr *vap = v->a_vap;
7a2de9a4 292 struct tmpfs_node *node;
7a2de9a4
MD
293
294 node = VP_TO_TMPFS_NODE(vp);
295
296 tmpfs_update(vp);
297
298 vap->va_type = vp->v_type;
299 vap->va_mode = node->tn_mode;
300 vap->va_nlink = node->tn_links;
301 vap->va_uid = node->tn_uid;
302 vap->va_gid = node->tn_gid;
303 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
304 vap->va_fileid = node->tn_id;
305 vap->va_size = node->tn_size;
306 vap->va_blocksize = PAGE_SIZE;
307 vap->va_atime.tv_sec = node->tn_atime;
308 vap->va_atime.tv_nsec = node->tn_atimensec;
309 vap->va_mtime.tv_sec = node->tn_mtime;
310 vap->va_mtime.tv_nsec = node->tn_mtimensec;
311 vap->va_ctime.tv_sec = node->tn_ctime;
312 vap->va_ctime.tv_nsec = node->tn_ctimensec;
313 vap->va_gen = node->tn_gen;
314 vap->va_flags = node->tn_flags;
315 if (vp->v_type == VBLK || vp->v_type == VCHR)
316 {
317 vap->va_rmajor = umajor(node->tn_rdev);
318 vap->va_rminor = uminor(node->tn_rdev);
319 }
320 vap->va_bytes = round_page(node->tn_size);
321 vap->va_filerev = 0;
322
7a2de9a4
MD
323 return 0;
324}
325
326/* --------------------------------------------------------------------- */
327
328int
329tmpfs_setattr(struct vop_setattr_args *v)
330{
331 struct vnode *vp = v->a_vp;
332 struct vattr *vap = v->a_vap;
333 struct ucred *cred = v->a_cred;
7a2de9a4 334 int error = 0;
7a2de9a4 335
7a2de9a4
MD
336 if (error == 0 && (vap->va_flags != VNOVAL))
337 error = tmpfs_chflags(vp, vap->va_flags, cred);
338
339 if (error == 0 && (vap->va_size != VNOVAL))
340 error = tmpfs_chsize(vp, vap->va_size, cred);
341
d89ce96a
MD
342 if (error == 0 && (vap->va_uid != (uid_t)VNOVAL ||
343 vap->va_gid != (gid_t)VNOVAL)) {
7a2de9a4 344 error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred);
d89ce96a 345 }
7a2de9a4
MD
346
347 if (error == 0 && (vap->va_mode != (mode_t)VNOVAL))
348 error = tmpfs_chmod(vp, vap->va_mode, cred);
349
350 if (error == 0 && ((vap->va_atime.tv_sec != VNOVAL &&
351 vap->va_atime.tv_nsec != VNOVAL) ||
352 (vap->va_mtime.tv_sec != VNOVAL &&
d89ce96a 353 vap->va_mtime.tv_nsec != VNOVAL) )) {
7a2de9a4 354 error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
d89ce96a
MD
355 vap->va_vaflags, cred);
356 }
7a2de9a4
MD
357
358 /* Update the node times. We give preference to the error codes
359 * generated by this function rather than the ones that may arise
360 * from tmpfs_update. */
361 tmpfs_update(vp);
362
7a2de9a4
MD
363 return error;
364}
365
366/* --------------------------------------------------------------------- */
367
9fc94b5f 368/*
630e3a33
MD
369 * fsync is usually a NOP, but we must take action when unmounting or
370 * when recycling.
9fc94b5f 371 */
7a2de9a4
MD
372static int
373tmpfs_fsync(struct vop_fsync_args *v)
374{
9fc94b5f 375 struct tmpfs_mount *tmp;
630e3a33 376 struct tmpfs_node *node;
7a2de9a4
MD
377 struct vnode *vp = v->a_vp;
378
9fc94b5f 379 tmp = VFS_TO_TMPFS(vp->v_mount);
630e3a33
MD
380 node = VP_TO_TMPFS_NODE(vp);
381
382 tmpfs_update(vp);
383 if (vp->v_type == VREG) {
d4623db3 384 if (vp->v_flag & VRECLAIMED) {
630e3a33
MD
385 if (node->tn_links == 0)
386 tmpfs_truncate(vp, 0);
387 else
388 vfsync(v->a_vp, v->a_waitfor, 1, NULL, NULL);
389 }
9fc94b5f 390 }
7a2de9a4
MD
391 return 0;
392}
393
394/* --------------------------------------------------------------------- */
395
396static int
397tmpfs_read (struct vop_read_args *ap)
398{
399 struct buf *bp;
400 struct vnode *vp = ap->a_vp;
401 struct uio *uio = ap->a_uio;
402 struct tmpfs_node *node;
7a2de9a4 403 off_t base_offset;
9fc94b5f 404 size_t offset;
7a2de9a4
MD
405 size_t len;
406 int got_mplock;
9fc94b5f 407 int error;
7a2de9a4
MD
408
409 error = 0;
410 if (uio->uio_resid == 0) {
411 return error;
412 }
413
414 node = VP_TO_TMPFS_NODE(vp);
415
416 if (uio->uio_offset < 0)
417 return (EINVAL);
418 if (vp->v_type != VREG)
419 return (EINVAL);
420
7a2de9a4
MD
421#ifdef SMP
422 if(curthread->td_mpcount)
423 got_mplock = -1;
424 else
425 got_mplock = 0;
426#else
427 got_mplock = -1;
428#endif
429
430 while (uio->uio_resid > 0 && uio->uio_offset < node->tn_size) {
431 /*
432 * Use buffer cache I/O (via tmpfs_strategy)
433 */
9fc94b5f 434 offset = (size_t)uio->uio_offset & BMASK;
7a2de9a4
MD
435 base_offset = (off_t)uio->uio_offset - offset;
436 bp = getcacheblk(vp, base_offset);
437 if (bp == NULL)
438 {
439 if (got_mplock == 0) {
440 got_mplock = 1;
441 get_mplock();
442 }
443
444 error = bread(vp, base_offset, BSIZE, &bp);
445 if (error) {
446 brelse(bp);
447 kprintf("tmpfs_read bread error %d\n", error);
448 break;
449 }
450 }
451
452 if (got_mplock == 0) {
453 got_mplock = 1;
454 get_mplock();
455 }
456
457 /*
458 * Figure out how many bytes we can actually copy this loop.
459 */
460 len = BSIZE - offset;
461 if (len > uio->uio_resid)
462 len = uio->uio_resid;
463 if (len > node->tn_size - uio->uio_offset)
464 len = (size_t)(node->tn_size - uio->uio_offset);
465
466 error = uiomove((char *)bp->b_data + offset, len, uio);
467 bqrelse(bp);
468 if (error) {
469 kprintf("tmpfs_read uiomove error %d\n", error);
470 break;
471 }
472 }
473
474 if (got_mplock > 0)
475 rel_mplock();
476
477 TMPFS_NODE_LOCK(node);
478 node->tn_status |= TMPFS_NODE_ACCESSED;
479 TMPFS_NODE_UNLOCK(node);
480
7a2de9a4
MD
481 return(error);
482}
483
484static int
485tmpfs_write (struct vop_write_args *ap)
486{
487 struct buf *bp;
488 struct vnode *vp = ap->a_vp;
489 struct uio *uio = ap->a_uio;
490 struct thread *td = uio->uio_td;
491 struct tmpfs_node *node;
492 boolean_t extended;
493 off_t oldsize;
494 int error;
7a2de9a4 495 off_t base_offset;
9fc94b5f 496 size_t offset;
7a2de9a4
MD
497 size_t len;
498 struct rlimit limit;
499 int got_mplock;
500 int trivial = 0;
501
502 error = 0;
503 if (uio->uio_resid == 0) {
504 return error;
505 }
506
507 node = VP_TO_TMPFS_NODE(vp);
508
509 if (vp->v_type != VREG)
510 return (EINVAL);
511
512 oldsize = node->tn_size;
513 if (ap->a_ioflag & IO_APPEND)
514 uio->uio_offset = node->tn_size;
515
516 /*
517 * Check for illegal write offsets.
518 */
519 if (uio->uio_offset + uio->uio_resid >
520 VFS_TO_TMPFS(vp->v_mount)->tm_maxfilesize)
521 return (EFBIG);
522
523 if (vp->v_type == VREG && td != NULL) {
524 error = kern_getrlimit(RLIMIT_FSIZE, &limit);
525 if (error != 0)
526 return error;
527 if (uio->uio_offset + uio->uio_resid > limit.rlim_cur) {
528 ksignal(td->td_proc, SIGXFSZ);
529 return (EFBIG);
530 }
531 }
532
533
534 /*
535 * Extend the file's size if necessary
536 */
9fc94b5f 537 extended = ((uio->uio_offset + uio->uio_resid) > node->tn_size);
7a2de9a4 538
7a2de9a4 539#ifdef SMP
630e3a33 540 if (curthread->td_mpcount) {
7a2de9a4 541 got_mplock = -1;
630e3a33 542 } else {
7a2de9a4
MD
543 got_mplock = 1;
544 get_mplock();
545 }
546#else
630e3a33 547 got_mplock = -1;
7a2de9a4 548#endif
7a2de9a4
MD
549 while (uio->uio_resid > 0) {
550 /*
551 * Use buffer cache I/O (via tmpfs_strategy)
552 */
9fc94b5f 553 offset = (size_t)uio->uio_offset & BMASK;
7a2de9a4
MD
554 base_offset = (off_t)uio->uio_offset - offset;
555 len = BSIZE - offset;
556 if (len > uio->uio_resid)
557 len = uio->uio_resid;
558
559 if ((uio->uio_offset + len) > node->tn_size) {
9fc94b5f 560 trivial = (uio->uio_offset <= node->tn_size);
7a2de9a4
MD
561 error = tmpfs_reg_resize(vp, uio->uio_offset + len, trivial);
562 if (error)
563 break;
564 }
565
9fc94b5f
MD
566 /*
567 * Read to fill in any gaps. Theoretically we could
568 * optimize this if the write covers the entire buffer
569 * and is not a UIO_NOCOPY write, however this can lead
570 * to a security violation exposing random kernel memory
571 * (whatever junk was in the backing VM pages before).
572 *
573 * So just use bread() to do the right thing.
574 */
575 error = bread(vp, base_offset, BSIZE, &bp);
7a2de9a4
MD
576 error = uiomove((char *)bp->b_data + offset, len, uio);
577 if (error) {
578 kprintf("tmpfs_write uiomove error %d\n", error);
579 brelse(bp);
580 break;
581 }
582
583 if (uio->uio_offset > node->tn_size)
584 node->tn_size = uio->uio_offset;
585
586 /*
9fc94b5f 587 * The data has been loaded into the buffer, write it out.
7a2de9a4 588 *
9fc94b5f
MD
589 * We want tmpfs to be able to use all available ram, not
590 * just the buffer cache, so if not explicitly paging we
591 * use buwrite() to leave the buffer clean but mark all the
592 * VM pages valid+dirty.
7a2de9a4 593 *
d89ce96a
MD
594 * When the kernel is paging, either via normal pageout
595 * operation or when cleaning the object during a recycle,
596 * the underlying VM pages are going to get thrown away
597 * so we MUST write them to swap.
598 *
599 * XXX unfortunately this catches msync() system calls too
600 * for the moment.
7a2de9a4 601 */
d89ce96a 602 if (ap->a_ioflag & IO_SYNC) {
9fc94b5f 603 bwrite(bp);
d89ce96a
MD
604 } else if ((ap->a_ioflag & IO_ASYNC) ||
605 (uio->uio_segflg == UIO_NOCOPY)) {
9fc94b5f 606 bawrite(bp);
d89ce96a 607 } else {
9fc94b5f 608 buwrite(bp);
d89ce96a 609 }
9fc94b5f 610
7a2de9a4
MD
611 if (bp->b_error) {
612 kprintf("tmpfs_write bwrite error %d\n", error);
613 break;
614 }
615 }
7a2de9a4
MD
616
617 if (got_mplock > 0)
618 rel_mplock();
619
620 if (error) {
621 if (extended)
622 (void)tmpfs_reg_resize(vp, oldsize, trivial);
623 return error;
624 }
625
626 TMPFS_NODE_LOCK(node);
627 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
628 (extended? TMPFS_NODE_CHANGED : 0);
629
630 if (node->tn_mode & (S_ISUID | S_ISGID)) {
631 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0))
632 node->tn_mode &= ~(S_ISUID | S_ISGID);
633 }
634 TMPFS_NODE_UNLOCK(node);
635
7a2de9a4
MD
636 return(error);
637}
638
639static int
640tmpfs_advlock (struct vop_advlock_args *ap)
641{
642 struct tmpfs_node *node;
643 struct vnode *vp = ap->a_vp;
644
645 node = VP_TO_TMPFS_NODE(vp);
646
647 return (lf_advlock(ap, &node->tn_advlock, node->tn_size));
648}
649
650
651static int
652tmpfs_strategy(struct vop_strategy_args *ap)
653{
654 struct bio *bio = ap->a_bio;
9fc94b5f 655 struct buf *bp = bio->bio_buf;
7a2de9a4
MD
656 struct vnode *vp = ap->a_vp;
657 struct tmpfs_node *node;
658 vm_object_t uobj;
659
9fc94b5f
MD
660 if (vp->v_type != VREG) {
661 bp->b_resid = bp->b_bcount;
662 bp->b_flags |= B_ERROR | B_INVAL;
663 bp->b_error = EINVAL;
664 biodone(bio);
665 return(0);
666 }
7a2de9a4
MD
667
668 node = VP_TO_TMPFS_NODE(vp);
669
670 uobj = node->tn_reg.tn_aobj;
9fc94b5f 671
7a2de9a4 672 /*
9fc94b5f
MD
673 * Call swap_pager_strategy to read or write between the VM
674 * object and the buffer cache.
7a2de9a4
MD
675 */
676 swap_pager_strategy(uobj, bio);
677
678 return 0;
679}
680
681static int
682tmpfs_bmap(struct vop_bmap_args *ap)
683{
684 if (ap->a_doffsetp != NULL)
685 *ap->a_doffsetp = ap->a_loffset;
686 if (ap->a_runp != NULL)
687 *ap->a_runp = 0;
688 if (ap->a_runb != NULL)
689 *ap->a_runb = 0;
690
691 return 0;
692}
9fc94b5f 693
7a2de9a4
MD
694/* --------------------------------------------------------------------- */
695
696static int
697tmpfs_nremove(struct vop_nremove_args *v)
698{
699 struct vnode *dvp = v->a_dvp;
700 struct namecache *ncp = v->a_nch->ncp;
9fc94b5f 701 struct vnode *vp;
7a2de9a4
MD
702 int error;
703 struct tmpfs_dirent *de;
704 struct tmpfs_mount *tmp;
705 struct tmpfs_node *dnode;
706 struct tmpfs_node *node;
707
9fc94b5f
MD
708 /*
709 * We have to acquire the vp from v->a_nch because
710 * we will likely unresolve the namecache entry, and
711 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
712 * sequence to recover space from the file.
713 */
714 error = cache_vref(v->a_nch, v->a_cred, &vp);
715 KKASSERT(error == 0);
7a2de9a4
MD
716
717 if (vp->v_type == VDIR) {
718 error = EISDIR;
719 goto out;
720 }
721
722 dnode = VP_TO_TMPFS_DIR(dvp);
723 node = VP_TO_TMPFS_NODE(vp);
724 tmp = VFS_TO_TMPFS(vp->v_mount);
725 de = tmpfs_dir_lookup(dnode, node, ncp);
726 if (de == NULL) {
727 error = ENOENT;
728 goto out;
729 }
730
731 /* Files marked as immutable or append-only cannot be deleted. */
732 if ((node->tn_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
733 (dnode->tn_flags & APPEND)) {
734 error = EPERM;
735 goto out;
736 }
737
738 /* Remove the entry from the directory; as it is a file, we do not
739 * have to change the number of hard links of the directory. */
22d3b394 740 tmpfs_dir_detach(dnode, de);
7a2de9a4
MD
741
742 /* Free the directory entry we just deleted. Note that the node
743 * referred by it will not be removed until the vnode is really
744 * reclaimed. */
0786baf1 745 tmpfs_free_dirent(tmp, de);
7a2de9a4
MD
746
747 if (node->tn_links > 0) {
748 TMPFS_NODE_LOCK(node);
749 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
750 TMPFS_NODE_MODIFIED;
751 TMPFS_NODE_UNLOCK(node);
752 }
753
754 cache_setunresolved(v->a_nch);
755 cache_setvp(v->a_nch, NULL);
9fc94b5f 756 /*cache_inval_vp(vp, CINV_DESTROY);*/
7a2de9a4
MD
757 error = 0;
758
7a2de9a4 759out:
9fc94b5f 760 vrele(vp);
7a2de9a4
MD
761
762 return error;
763}
764
765/* --------------------------------------------------------------------- */
766
767static int
768tmpfs_nlink(struct vop_nlink_args *v)
769{
770 struct vnode *dvp = v->a_dvp;
771 struct vnode *vp = v->a_vp;
772 struct namecache *ncp = v->a_nch->ncp;
7a2de9a4
MD
773 struct tmpfs_dirent *de;
774 struct tmpfs_node *node;
22d3b394
MD
775 struct tmpfs_node *dnode;
776 int error;
7a2de9a4 777
7a2de9a4
MD
778 KKASSERT(dvp != vp); /* XXX When can this be false? */
779
780 node = VP_TO_TMPFS_NODE(vp);
22d3b394 781 dnode = VP_TO_TMPFS_NODE(dvp);
7a2de9a4
MD
782
783 /* XXX: Why aren't the following two tests done by the caller? */
784
785 /* Hard links of directories are forbidden. */
786 if (vp->v_type == VDIR) {
787 error = EPERM;
788 goto out;
789 }
790
791 /* Cannot create cross-device links. */
792 if (dvp->v_mount != vp->v_mount) {
793 error = EXDEV;
794 goto out;
795 }
796
797 /* Ensure that we do not overflow the maximum number of links imposed
798 * by the system. */
799 KKASSERT(node->tn_links <= LINK_MAX);
800 if (node->tn_links == LINK_MAX) {
801 error = EMLINK;
802 goto out;
803 }
804
805 /* We cannot create links of files marked immutable or append-only. */
806 if (node->tn_flags & (IMMUTABLE | APPEND)) {
807 error = EPERM;
808 goto out;
809 }
810
811 /* Allocate a new directory entry to represent the node. */
812 error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
813 ncp->nc_name, ncp->nc_nlen, &de);
814 if (error != 0)
815 goto out;
816
817 /* Insert the new directory entry into the appropriate directory. */
22d3b394 818 tmpfs_dir_attach(dnode, de);
7a2de9a4
MD
819
820 /* vp link count has changed, so update node times. */
821
822 TMPFS_NODE_LOCK(node);
823 node->tn_status |= TMPFS_NODE_CHANGED;
824 TMPFS_NODE_UNLOCK(node);
825 tmpfs_update(vp);
826
827 cache_setunresolved(v->a_nch);
828 cache_setvp(v->a_nch, vp);
829 error = 0;
830
831out:
7a2de9a4
MD
832 return error;
833}
834
835/* --------------------------------------------------------------------- */
836
837static int
838tmpfs_nrename(struct vop_nrename_args *v)
839{
840 struct vnode *fdvp = v->a_fdvp;
841 struct namecache *fncp = v->a_fnch->ncp;
842 struct vnode *fvp = fncp->nc_vp;
843 struct vnode *tdvp = v->a_tdvp;
844 struct namecache *tncp = v->a_tnch->ncp;
845 struct vnode *tvp = tncp->nc_vp;
7a2de9a4
MD
846 struct tmpfs_dirent *de;
847 struct tmpfs_mount *tmp;
848 struct tmpfs_node *fdnode;
849 struct tmpfs_node *fnode;
850 struct tmpfs_node *tnode;
851 struct tmpfs_node *tdnode;
22d3b394 852 char *newname;
dca262fb 853 char *oldname;
22d3b394 854 int error;
7a2de9a4 855
7a2de9a4
MD
856 tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
857
858 /* Disallow cross-device renames.
859 * XXX Why isn't this done by the caller? */
860 if (fvp->v_mount != tdvp->v_mount ||
861 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
862 error = EXDEV;
863 goto out;
864 }
865
866 tmp = VFS_TO_TMPFS(tdvp->v_mount);
867 tdnode = VP_TO_TMPFS_DIR(tdvp);
868
869 /* If source and target are the same file, there is nothing to do. */
870 if (fvp == tvp) {
871 error = 0;
872 goto out;
873 }
874
7a2de9a4
MD
875 fdnode = VP_TO_TMPFS_DIR(fdvp);
876 fnode = VP_TO_TMPFS_NODE(fvp);
877 de = tmpfs_dir_lookup(fdnode, fnode, fncp);
878
879 /* Avoid manipulating '.' and '..' entries. */
880 if (de == NULL) {
881 error = ENOENT;
882 goto out_locked;
883 }
884 KKASSERT(de->td_node == fnode);
885
dca262fb
MD
886 /*
887 * If replacing an entry in the target directory and that entry
888 * is a directory, it must be empty.
889 *
7a2de9a4 890 * Kern_rename gurantees the destination to be a directory
dca262fb
MD
891 * if the source is one (it does?).
892 */
7a2de9a4
MD
893 if (tvp != NULL) {
894 KKASSERT(tnode != NULL);
895
896 if ((tnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
897 (tdnode->tn_flags & (APPEND | IMMUTABLE))) {
898 error = EPERM;
899 goto out_locked;
900 }
901
902 if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
903 if (tnode->tn_size > 0) {
904 error = ENOTEMPTY;
905 goto out_locked;
906 }
907 } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
908 error = ENOTDIR;
909 goto out_locked;
910 } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
911 error = EISDIR;
912 goto out_locked;
913 } else {
914 KKASSERT(fnode->tn_type != VDIR &&
915 tnode->tn_type != VDIR);
916 }
917 }
918
dca262fb
MD
919 if ((fnode->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
920 (fdnode->tn_flags & (APPEND | IMMUTABLE))) {
7a2de9a4
MD
921 error = EPERM;
922 goto out_locked;
923 }
924
dca262fb
MD
925 /*
926 * Ensure that we have enough memory to hold the new name, if it
927 * has to be changed.
928 */
7a2de9a4
MD
929 if (fncp->nc_nlen != tncp->nc_nlen ||
930 bcmp(fncp->nc_name, tncp->nc_name, fncp->nc_nlen) != 0) {
9fc94b5f 931 newname = kmalloc(tncp->nc_nlen + 1, M_TMPFSNAME, M_WAITOK);
dca262fb
MD
932 bcopy(tncp->nc_name, newname, tncp->nc_nlen);
933 newname[tncp->nc_nlen] = '\0';
934 } else {
7a2de9a4 935 newname = NULL;
dca262fb 936 }
7a2de9a4 937
dca262fb
MD
938 /*
939 * Unlink entry from source directory. Note that the kernel has
940 * already checked for illegal recursion cases (renaming a directory
941 * into a subdirectory of itself).
942 */
943 if (fdnode != tdnode)
944 tmpfs_dir_detach(fdnode, de);
945
946 /*
947 * Handle any name change. Swap with newname, we will
948 * deallocate it at the end.
949 */
950 if (newname != NULL) {
951#if 0
952 TMPFS_NODE_LOCK(fnode);
953 fnode->tn_status |= TMPFS_NODE_CHANGED;
954 TMPFS_NODE_UNLOCK(fnode);
955#endif
956 oldname = de->td_name;
957 de->td_name = newname;
958 de->td_namelen = (uint16_t)tncp->nc_nlen;
959 newname = oldname;
960 }
961
962 /*
963 * Link entry to target directory. If the entry
964 * represents a directory move the parent linkage
965 * as well.
966 */
7a2de9a4 967 if (fdnode != tdnode) {
7a2de9a4 968 if (de->td_node->tn_type == VDIR) {
7a2de9a4 969 TMPFS_VALIDATE_DIR(fnode);
7a2de9a4 970
7a2de9a4 971 TMPFS_NODE_LOCK(tdnode);
7a2de9a4 972 tdnode->tn_links++;
dca262fb
MD
973 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
974 TMPFS_NODE_UNLOCK(tdnode);
7a2de9a4 975
dca262fb
MD
976 TMPFS_NODE_LOCK(fnode);
977 fnode->tn_dir.tn_parent = tdnode;
978 fnode->tn_status |= TMPFS_NODE_CHANGED;
979 TMPFS_NODE_UNLOCK(fnode);
980
981 TMPFS_NODE_LOCK(fdnode);
982 fdnode->tn_links--;
983 fdnode->tn_status |= TMPFS_NODE_MODIFIED;
7a2de9a4 984 TMPFS_NODE_UNLOCK(fdnode);
7a2de9a4 985 }
22d3b394 986 tmpfs_dir_attach(tdnode, de);
dca262fb 987 } else {
7a2de9a4 988 TMPFS_NODE_LOCK(tdnode);
7a2de9a4 989 tdnode->tn_status |= TMPFS_NODE_MODIFIED;
7a2de9a4
MD
990 TMPFS_NODE_UNLOCK(tdnode);
991 }
992
dca262fb
MD
993 /*
994 * If we are overwriting an entry, we have to remove the old one
995 * from the target directory.
996 */
7a2de9a4
MD
997 if (tvp != NULL) {
998 /* Remove the old entry from the target directory. */
999 de = tmpfs_dir_lookup(tdnode, tnode, tncp);
22d3b394 1000 tmpfs_dir_detach(tdnode, de);
7a2de9a4 1001
dca262fb
MD
1002 /*
1003 * Free the directory entry we just deleted. Note that the
7a2de9a4 1004 * node referred by it will not be removed until the vnode is
dca262fb
MD
1005 * really reclaimed.
1006 */
0786baf1 1007 tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de);
9fc94b5f 1008 /*cache_inval_vp(tvp, CINV_DESTROY);*/
7a2de9a4
MD
1009 }
1010
dca262fb
MD
1011 /*
1012 * Finish up
1013 */
1014 if (newname) {
1015 kfree(newname, M_TMPFSNAME);
1016 newname = NULL;
1017 }
7a2de9a4
MD
1018 cache_rename(v->a_fnch, v->a_tnch);
1019 error = 0;
1020
1021out_locked:
630e3a33 1022 ;
7a2de9a4
MD
1023
1024out:
1025 /* Release target nodes. */
1026 /* XXX: I don't understand when tdvp can be the same as tvp, but
1027 * other code takes care of this... */
1028 if (tdvp == tvp)
1029 vrele(tdvp);
7a2de9a4
MD
1030
1031 return error;
1032}
1033
1034/* --------------------------------------------------------------------- */
1035
1036static int
1037tmpfs_nmkdir(struct vop_nmkdir_args *v)
1038{
1039 struct vnode *dvp = v->a_dvp;
1040 struct vnode **vpp = v->a_vpp;
1041 struct namecache *ncp = v->a_nch->ncp;
1042 struct vattr *vap = v->a_vap;
1043 struct ucred *cred = v->a_cred;
1044 int error;
1045
1046 KKASSERT(vap->va_type == VDIR);
1047
7a2de9a4
MD
1048 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, NULL);
1049 if (error == 0) {
1050 cache_setunresolved(v->a_nch);
1051 cache_setvp(v->a_nch, *vpp);
1052 }
7a2de9a4
MD
1053
1054 return error;
1055}
1056
1057/* --------------------------------------------------------------------- */
1058
1059static int
1060tmpfs_nrmdir(struct vop_nrmdir_args *v)
1061{
1062 struct vnode *dvp = v->a_dvp;
1063 struct namecache *ncp = v->a_nch->ncp;
9fc94b5f 1064 struct vnode *vp;
7a2de9a4
MD
1065 struct tmpfs_dirent *de;
1066 struct tmpfs_mount *tmp;
1067 struct tmpfs_node *dnode;
1068 struct tmpfs_node *node;
38e5e604
MD
1069 int error;
1070
1071 /*
1072 * Prevalidate so we don't hit an assertion later
1073 */
1074 if (vp->v_type != VDIR)
1075 return ENOTDIR;
7a2de9a4 1076
9fc94b5f
MD
1077 /*
1078 * We have to acquire the vp from v->a_nch because
1079 * we will likely unresolve the namecache entry, and
1080 * a vrele is needed to trigger the tmpfs_inactive/tmpfs_reclaim
1081 * sequence.
1082 */
1083 error = cache_vref(v->a_nch, v->a_cred, &vp);
1084 KKASSERT(error == 0);
7a2de9a4
MD
1085
1086 tmp = VFS_TO_TMPFS(dvp->v_mount);
1087 dnode = VP_TO_TMPFS_DIR(dvp);
1088 node = VP_TO_TMPFS_DIR(vp);
1089
1090 /* Directories with more than two entries ('.' and '..') cannot be
1091 * removed. */
1092 if (node->tn_size > 0) {
1093 error = ENOTEMPTY;
1094 goto out;
1095 }
1096
1097 if ((dnode->tn_flags & APPEND)
1098 || (node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND))) {
1099 error = EPERM;
1100 goto out;
1101 }
1102
1103 /* This invariant holds only if we are not trying to remove "..".
1104 * We checked for that above so this is safe now. */
1105 KKASSERT(node->tn_dir.tn_parent == dnode);
1106
1107 /* Get the directory entry associated with node (vp). This was
1108 * filled by tmpfs_lookup while looking up the entry. */
1109 de = tmpfs_dir_lookup(dnode, node, ncp);
1110 KKASSERT(TMPFS_DIRENT_MATCHES(de,
1111 ncp->nc_name,
1112 ncp->nc_nlen));
1113
1114 /* Check flags to see if we are allowed to remove the directory. */
b7fe63af
MD
1115 if ((dnode->tn_flags & APPEND) ||
1116 node->tn_flags & (NOUNLINK | IMMUTABLE | APPEND)) {
7a2de9a4
MD
1117 error = EPERM;
1118 goto out;
1119 }
1120
1121
1122 /* Detach the directory entry from the directory (dnode). */
22d3b394 1123 tmpfs_dir_detach(dnode, de);
7a2de9a4
MD
1124
1125 /* No vnode should be allocated for this entry from this point */
1126 TMPFS_NODE_LOCK(node);
1127 TMPFS_ASSERT_ELOCKED(node);
1128 TMPFS_NODE_LOCK(dnode);
1129 TMPFS_ASSERT_ELOCKED(dnode);
1130
0786baf1
MD
1131#if 0
1132 /* handled by tmpfs_free_node */
1133 KKASSERT(node->tn_links > 0);
7a2de9a4
MD
1134 node->tn_links--;
1135 node->tn_dir.tn_parent = NULL;
0786baf1 1136#endif
7a2de9a4
MD
1137 node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1138 TMPFS_NODE_MODIFIED;
1139
0786baf1
MD
1140#if 0
1141 /* handled by tmpfs_free_node */
1142 KKASSERT(dnode->tn_links > 0);
7a2de9a4 1143 dnode->tn_links--;
0786baf1 1144#endif
7a2de9a4
MD
1145 dnode->tn_status |= TMPFS_NODE_ACCESSED | \
1146 TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1147
1148 TMPFS_NODE_UNLOCK(dnode);
1149 TMPFS_NODE_UNLOCK(node);
1150
1151 /* Free the directory entry we just deleted. Note that the node
1152 * referred by it will not be removed until the vnode is really
1153 * reclaimed. */
0786baf1 1154 tmpfs_free_dirent(tmp, de);
7a2de9a4
MD
1155
1156 /* Release the deleted vnode (will destroy the node, notify
1157 * interested parties and clean it from the cache). */
1158
1159 TMPFS_NODE_LOCK(dnode);
1160 dnode->tn_status |= TMPFS_NODE_CHANGED;
1161 TMPFS_NODE_UNLOCK(dnode);
1162 tmpfs_update(dvp);
1163
1164 cache_setunresolved(v->a_nch);
1165 cache_setvp(v->a_nch, NULL);
9fc94b5f 1166 /*cache_inval_vp(vp, CINV_DESTROY);*/
7a2de9a4
MD
1167 error = 0;
1168
1169out:
9fc94b5f 1170 vrele(vp);
7a2de9a4
MD
1171
1172 return error;
1173}
1174
1175/* --------------------------------------------------------------------- */
1176
1177static int
1178tmpfs_nsymlink(struct vop_nsymlink_args *v)
1179{
1180 struct vnode *dvp = v->a_dvp;
1181 struct vnode **vpp = v->a_vpp;
1182 struct namecache *ncp = v->a_nch->ncp;
1183 struct vattr *vap = v->a_vap;
1184 struct ucred *cred = v->a_cred;
1185 char *target = v->a_target;
1186 int error;
1187
7a2de9a4
MD
1188 vap->va_type = VLNK;
1189 error = tmpfs_alloc_file(dvp, vpp, vap, ncp, cred, target);
1190 if (error == 0) {
1191 cache_setunresolved(v->a_nch);
1192 cache_setvp(v->a_nch, *vpp);
1193 }
7a2de9a4
MD
1194
1195 return error;
1196}
1197
1198/* --------------------------------------------------------------------- */
1199
1200static int
1201tmpfs_readdir(struct vop_readdir_args *v)
1202{
1203 struct vnode *vp = v->a_vp;
1204 struct uio *uio = v->a_uio;
1205 int *eofflag = v->a_eofflag;
1206 off_t **cookies = v->a_cookies;
1207 int *ncookies = v->a_ncookies;
22d3b394 1208 struct tmpfs_mount *tmp;
7a2de9a4
MD
1209 int error;
1210 off_t startoff;
1211 off_t cnt = 0;
1212 struct tmpfs_node *node;
1213
1214 /* This operation only makes sense on directory nodes. */
1215 if (vp->v_type != VDIR)
1216 return ENOTDIR;
1217
22d3b394 1218 tmp = VFS_TO_TMPFS(vp->v_mount);
7a2de9a4
MD
1219 node = VP_TO_TMPFS_DIR(vp);
1220 startoff = uio->uio_offset;
1221
1222 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1223 error = tmpfs_dir_getdotdent(node, uio);
1224 if (error != 0)
1225 goto outok;
1226 cnt++;
1227 }
1228
1229 if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
22d3b394 1230 error = tmpfs_dir_getdotdotdent(tmp, node, uio);
7a2de9a4
MD
1231 if (error != 0)
1232 goto outok;
1233 cnt++;
1234 }
1235
1236 error = tmpfs_dir_getdents(node, uio, &cnt);
1237
1238outok:
1239 KKASSERT(error >= -1);
1240
1241 if (error == -1)
1242 error = 0;
1243
1244 if (eofflag != NULL)
1245 *eofflag =
1246 (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1247
1248 /* Update NFS-related variables. */
1249 if (error == 0 && cookies != NULL && ncookies != NULL) {
1250 off_t i;
1251 off_t off = startoff;
1252 struct tmpfs_dirent *de = NULL;
1253
1254 *ncookies = cnt;
1255 *cookies = kmalloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1256
1257 for (i = 0; i < cnt; i++) {
1258 KKASSERT(off != TMPFS_DIRCOOKIE_EOF);
1259 if (off == TMPFS_DIRCOOKIE_DOT) {
1260 off = TMPFS_DIRCOOKIE_DOTDOT;
1261 } else {
1262 if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1263 de = TAILQ_FIRST(&node->tn_dir.tn_dirhead);
1264 } else if (de != NULL) {
1265 de = TAILQ_NEXT(de, td_entries);
1266 } else {
1267 de = tmpfs_dir_lookupbycookie(node,
1268 off);
1269 KKASSERT(de != NULL);
1270 de = TAILQ_NEXT(de, td_entries);
1271 }
1272 if (de == NULL)
1273 off = TMPFS_DIRCOOKIE_EOF;
1274 else
1275 off = tmpfs_dircookie(de);
1276 }
1277
1278 (*cookies)[i] = off;
1279 }
1280 KKASSERT(uio->uio_offset == off);
1281 }
7a2de9a4
MD
1282
1283 return error;
1284}
1285
1286/* --------------------------------------------------------------------- */
1287
1288static int
1289tmpfs_readlink(struct vop_readlink_args *v)
1290{
1291 struct vnode *vp = v->a_vp;
1292 struct uio *uio = v->a_uio;
1293
1294 int error;
1295 struct tmpfs_node *node;
1296
1297 KKASSERT(uio->uio_offset == 0);
1298 KKASSERT(vp->v_type == VLNK);
1299
1300 node = VP_TO_TMPFS_NODE(vp);
1301
1302 error = uiomove(node->tn_link, MIN(node->tn_size, uio->uio_resid),
1303 uio);
1304 TMPFS_NODE_LOCK(node);
1305 node->tn_status |= TMPFS_NODE_ACCESSED;
1306 TMPFS_NODE_UNLOCK(node);
1307
1308 return error;
1309}
1310
1311/* --------------------------------------------------------------------- */
1312
1313static int
1314tmpfs_inactive(struct vop_inactive_args *v)
1315{
1316 struct vnode *vp = v->a_vp;
1317
1318 struct tmpfs_node *node;
1319
7a2de9a4
MD
1320 node = VP_TO_TMPFS_NODE(vp);
1321
9fc94b5f
MD
1322 /*
1323 * Get rid of unreferenced deleted vnodes sooner rather than
1324 * later so the data memory can be recovered immediately.
f96f2f39
MD
1325 *
1326 * We must truncate the vnode to prevent the normal reclamation
1327 * path from flushing the data for the removed file to disk.
9fc94b5f 1328 */
7a2de9a4 1329 TMPFS_NODE_LOCK(node);
b7fe63af
MD
1330 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1331 (node->tn_links == 0 ||
1332 (node->tn_links == 1 && node->tn_type == VDIR &&
1333 node->tn_dir.tn_parent)))
1334 {
9fc94b5f 1335 node->tn_vpstate = TMPFS_VNODE_DOOMED;
7a2de9a4 1336 TMPFS_NODE_UNLOCK(node);
f96f2f39
MD
1337 if (node->tn_type == VREG)
1338 tmpfs_truncate(vp, 0);
7a2de9a4 1339 vrecycle(vp);
9fc94b5f 1340 } else {
7a2de9a4 1341 TMPFS_NODE_UNLOCK(node);
9fc94b5f 1342 }
7a2de9a4
MD
1343
1344 return 0;
1345}
1346
1347/* --------------------------------------------------------------------- */
1348
1349int
1350tmpfs_reclaim(struct vop_reclaim_args *v)
1351{
1352 struct vnode *vp = v->a_vp;
7a2de9a4
MD
1353 struct tmpfs_mount *tmp;
1354 struct tmpfs_node *node;
1355
1356 node = VP_TO_TMPFS_NODE(vp);
1357 tmp = VFS_TO_TMPFS(vp->v_mount);
1358
7a2de9a4
MD
1359 tmpfs_free_vp(vp);
1360
b7fe63af
MD
1361 /*
1362 * If the node referenced by this vnode was deleted by the
1363 * user, we must free its associated data structures now that
1364 * the vnode is being reclaimed.
1365 *
1366 * Directories have an extra link ref.
1367 */
7a2de9a4 1368 TMPFS_NODE_LOCK(node);
b7fe63af
MD
1369 if ((node->tn_vpstate & TMPFS_VNODE_ALLOCATING) == 0 &&
1370 (node->tn_links == 0 ||
1371 (node->tn_links == 1 && node->tn_type == VDIR &&
1372 node->tn_dir.tn_parent)))
1373 {
7a2de9a4 1374 node->tn_vpstate = TMPFS_VNODE_DOOMED;
7a2de9a4 1375 tmpfs_free_node(tmp, node);
0786baf1 1376 /* eats the lock */
9fc94b5f 1377 } else {
7a2de9a4 1378 TMPFS_NODE_UNLOCK(node);
9fc94b5f 1379 }
7a2de9a4
MD
1380
1381 KKASSERT(vp->v_data == NULL);
1382 return 0;
1383}
1384
1385/* --------------------------------------------------------------------- */
1386
1387static int
1388tmpfs_print(struct vop_print_args *v)
1389{
1390 struct vnode *vp = v->a_vp;
1391
1392 struct tmpfs_node *node;
1393
1394 node = VP_TO_TMPFS_NODE(vp);
1395
1396 kprintf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1397 node, node->tn_flags, node->tn_links);
1398 kprintf("\tmode 0%o, owner %d, group %d, size %ju, status 0x%x\n",
1399 node->tn_mode, node->tn_uid, node->tn_gid,
1400 (uintmax_t)node->tn_size, node->tn_status);
1401
1402 if (vp->v_type == VFIFO)
1403 fifo_printinfo(vp);
1404
1405 kprintf("\n");
1406
1407 return 0;
1408}
1409
1410/* --------------------------------------------------------------------- */
1411
1412static int
1413tmpfs_pathconf(struct vop_pathconf_args *v)
1414{
1415 int name = v->a_name;
1416 register_t *retval = v->a_retval;
1417
1418 int error;
1419
1420 error = 0;
1421
1422 switch (name) {
1423 case _PC_LINK_MAX:
1424 *retval = LINK_MAX;
1425 break;
1426
1427 case _PC_NAME_MAX:
1428 *retval = NAME_MAX;
1429 break;
1430
1431 case _PC_PATH_MAX:
1432 *retval = PATH_MAX;
1433 break;
1434
1435 case _PC_PIPE_BUF:
1436 *retval = PIPE_BUF;
1437 break;
1438
1439 case _PC_CHOWN_RESTRICTED:
1440 *retval = 1;
1441 break;
1442
1443 case _PC_NO_TRUNC:
1444 *retval = 1;
1445 break;
1446
1447 case _PC_SYNC_IO:
1448 *retval = 1;
1449 break;
1450
1451 case _PC_FILESIZEBITS:
1452 *retval = 0; /* XXX Don't know which value should I return. */
1453 break;
1454
1455 default:
1456 error = EINVAL;
1457 }
1458
1459 return error;
1460}
1461
1462/* --------------------------------------------------------------------- */
1463
1464/*
1465 * vnode operations vector used for files stored in a tmpfs file system.
1466 */
1467struct vop_ops tmpfs_vnode_vops = {
1468 .vop_default = vop_defaultop,
1469 .vop_getpages = vop_stdgetpages,
1470 .vop_putpages = vop_stdputpages,
1471 .vop_ncreate = tmpfs_ncreate,
1472 .vop_nresolve = tmpfs_nresolve,
1473 .vop_nlookupdotdot = tmpfs_nlookupdotdot,
1474 .vop_nmknod = tmpfs_nmknod,
1475 .vop_open = tmpfs_open,
1476 .vop_close = tmpfs_close,
1477 .vop_access = tmpfs_access,
1478 .vop_getattr = tmpfs_getattr,
1479 .vop_setattr = tmpfs_setattr,
1480 .vop_read = tmpfs_read,
1481 .vop_write = tmpfs_write,
1482 .vop_fsync = tmpfs_fsync,
1483 .vop_nremove = tmpfs_nremove,
1484 .vop_nlink = tmpfs_nlink,
1485 .vop_nrename = tmpfs_nrename,
1486 .vop_nmkdir = tmpfs_nmkdir,
1487 .vop_nrmdir = tmpfs_nrmdir,
1488 .vop_nsymlink = tmpfs_nsymlink,
1489 .vop_readdir = tmpfs_readdir,
1490 .vop_readlink = tmpfs_readlink,
1491 .vop_inactive = tmpfs_inactive,
1492 .vop_reclaim = tmpfs_reclaim,
1493 .vop_print = tmpfs_print,
1494 .vop_pathconf = tmpfs_pathconf,
9fc94b5f 1495 .vop_bmap = tmpfs_bmap,
7a2de9a4
MD
1496 .vop_bmap = (void *)vop_eopnotsupp,
1497 .vop_strategy = tmpfs_strategy,
1498 .vop_advlock = tmpfs_advlock,
1499};