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