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