Merge from vendor branch LIBPCAP:
[dragonfly.git] / sys / vfs / mfs / mfs_vnops.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)mfs_vnops.c 8.11 (Berkeley) 5/22/95
34  * $FreeBSD: src/sys/ufs/mfs/mfs_vnops.c,v 1.47.2.1 2001/05/22 02:06:43 bp Exp $
35  * $DragonFly: src/sys/vfs/mfs/mfs_vnops.c,v 1.12 2004/04/15 00:59:41 cpressey Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/buf.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/sysproto.h>
46 #include <sys/mman.h>
47 #include <sys/conf.h>
48
49 #include <sys/buf2.h>
50
51 #include "mfsnode.h"
52 #include "mfs_extern.h"
53
54 static int      mfs_badop (struct vop_generic_args *);
55 static int      mfs_bmap (struct vop_bmap_args *);
56 static int      mfs_close (struct vop_close_args *);
57 static int      mfs_fsync (struct vop_fsync_args *);
58 static int      mfs_freeblks (struct vop_freeblks_args *);
59 static int      mfs_inactive (struct vop_inactive_args *); /* XXX */
60 static int      mfs_open (struct vop_open_args *);
61 static int      mfs_reclaim (struct vop_reclaim_args *); /* XXX */
62 static int      mfs_print (struct vop_print_args *); /* XXX */
63 static int      mfs_strategy (struct vop_strategy_args *); /* XXX */
64 static int      mfs_getpages (struct vop_getpages_args *); /* XXX */
65 /*
66  * mfs vnode operations.
67  */
68 vop_t **mfs_vnodeop_p;
69 static struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
70         { &vop_default_desc,            (vop_t *) mfs_badop },
71         { &vop_bmap_desc,               (vop_t *) mfs_bmap },
72         { &vop_bwrite_desc,             (vop_t *) vop_defaultop },
73         { &vop_close_desc,              (vop_t *) mfs_close },
74         { &vop_createvobject_desc,      (vop_t *) vop_stdcreatevobject },
75         { &vop_destroyvobject_desc,     (vop_t *) vop_stddestroyvobject },
76         { &vop_freeblks_desc,           (vop_t *) mfs_freeblks },
77         { &vop_fsync_desc,              (vop_t *) mfs_fsync },
78         { &vop_getpages_desc,           (vop_t *) mfs_getpages },
79         { &vop_getvobject_desc,         (vop_t *) vop_stdgetvobject },
80         { &vop_inactive_desc,           (vop_t *) mfs_inactive },
81         { &vop_ioctl_desc,              (vop_t *) vop_enotty },
82         { &vop_islocked_desc,           (vop_t *) vop_defaultop },
83         { &vop_lock_desc,               (vop_t *) vop_defaultop },
84         { &vop_open_desc,               (vop_t *) mfs_open },
85         { &vop_print_desc,              (vop_t *) mfs_print },
86         { &vop_reclaim_desc,            (vop_t *) mfs_reclaim },
87         { &vop_strategy_desc,           (vop_t *) mfs_strategy },
88         { &vop_unlock_desc,             (vop_t *) vop_defaultop },
89         { NULL, NULL }
90 };
91 static struct vnodeopv_desc mfs_vnodeop_opv_desc =
92         { &mfs_vnodeop_p, mfs_vnodeop_entries };
93
94 VNODEOP_SET(mfs_vnodeop_opv_desc);
95
96 /*
97  * Vnode Operations.
98  *
99  * Open called to allow memory filesystem to initialize and
100  * validate before actual IO. Record our process identifier
101  * so we can tell when we are doing I/O to ourself.
102  *
103  * mfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
104  *          struct thread *a_td)
105  */
106 /* ARGSUSED */
107 static int
108 mfs_open(struct vop_open_args *ap)
109 {
110         if (ap->a_vp->v_type != VCHR) {
111                 panic("mfs_open not VCHR");
112                 /* NOTREACHED */
113         }
114         return (0);
115 }
116
117 static int
118 mfs_fsync(struct vop_fsync_args *ap)
119 {
120         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_fsync), ap));
121 }
122
123 /*
124  * mfs_freeblks() - hook to allow us to free physical memory.
125  *
126  *      We implement the B_FREEBUF strategy.  We can't just madvise()
127  *      here because we have to do it in the correct order vs other bio
128  *      requests, so we queue it.
129  *
130  *      Note: geteblk() sets B_INVAL.  We leave it set to guarentee buffer
131  *      throw-away on brelse()? XXX
132  *
133  * mfs_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
134  */
135 static int
136 mfs_freeblks(struct vop_freeblks_args *ap)
137 {       
138         struct buf *bp;
139         struct vnode *vp;
140
141         if (!vfinddev(ap->a_vp->v_rdev, VCHR, &vp) || vp->v_usecount == 0)
142                 panic("mfs_freeblks: bad dev");
143
144         bp = geteblk(ap->a_length);
145         bp->b_flags |= B_FREEBUF | B_ASYNC;
146         bp->b_dev = ap->a_vp->v_rdev;
147         bp->b_blkno = ap->a_addr;
148         bp->b_offset = dbtob(ap->a_addr);
149         bp->b_bcount = ap->a_length;
150         BUF_KERNPROC(bp);
151         VOP_STRATEGY(vp, bp);
152         return(0);
153 }
154
155 /*
156  * Pass I/O requests to the memory filesystem process.
157  *
158  * mfs_strategy(struct vnode *a_vp, struct buf *a_bp)
159  */
160 static int
161 mfs_strategy(struct vop_strategy_args *ap)
162 {
163         struct buf *bp = ap->a_bp;
164         struct mfsnode *mfsp;
165         struct vnode *vp;
166         struct thread *td = curthread;          /* XXX */
167         int s;
168
169         if (!vfinddev(bp->b_dev, VCHR, &vp) || vp->v_usecount == 0)
170                 panic("mfs_strategy: bad dev");
171         mfsp = VTOMFS(vp);
172
173         /*
174          * splbio required for queueing/dequeueing, in case of forwarded
175          * BPs from bio interrupts (?).  It may not be necessary.
176          */
177
178         s = splbio();
179
180         if (mfsp->mfs_td == NULL) {
181                 /*
182                  * mini-root.  Note: B_FREEBUF not supported at the moment,
183                  * I'm not sure what kind of dataspace b_data is in.
184                  */
185                 caddr_t base;
186
187                 base = mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
188                 if (bp->b_flags & B_FREEBUF)
189                         ;
190                 if (bp->b_flags & B_READ)
191                         bcopy(base, bp->b_data, bp->b_bcount);
192                 else
193                         bcopy(bp->b_data, base, bp->b_bcount);
194                 biodone(bp);
195         } else if (mfsp->mfs_td == td) {
196                 /*
197                  * VOP to self
198                  */
199                 splx(s);
200                 mfs_doio(bp, mfsp);
201                 s = splbio();
202         } else {
203                 /*
204                  * VOP from some other process, queue to MFS process and
205                  * wake it up.
206                  */
207                 bufq_insert_tail(&mfsp->buf_queue, bp);
208                 wakeup((caddr_t)vp);
209         }
210         splx(s);
211         return (0);
212 }
213
214 /*
215  * Memory file system I/O.
216  *
217  * Trivial on the HP since buffer has already been mapping into KVA space.
218  *
219  * Read and Write are handled with a simple copyin and copyout.    
220  *
221  * We also partially support VOP_FREEBLKS() via B_FREEBUF.  We can't implement
222  * completely -- for example, on fragments or inode metadata, but we can
223  * implement it for page-aligned requests.
224  */
225 void
226 mfs_doio(struct buf *bp, struct mfsnode *mfsp)
227 {
228         caddr_t base = mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
229
230         if (bp->b_flags & B_FREEBUF) {
231                 /*
232                  * Implement B_FREEBUF, which allows the filesystem to tell
233                  * a block device when blocks are no longer needed (like when
234                  * a file is deleted).  We use the hook to MADV_FREE the VM.
235                  * This makes an MFS filesystem work as well or better then
236                  * a sun-style swap-mounted filesystem.
237                  */
238                 int bytes = bp->b_bcount;
239
240                 if ((vm_offset_t)base & PAGE_MASK) {
241                         int n = PAGE_SIZE - ((vm_offset_t)base & PAGE_MASK);
242                         bytes -= n;
243                         base += n;
244                 }
245                 if (bytes > 0) {
246                         struct madvise_args uap;
247
248                         bytes &= ~PAGE_MASK;
249                         if (bytes != 0) {
250                                 bzero(&uap, sizeof(uap));
251                                 uap.addr  = base;
252                                 uap.len   = bytes;
253                                 uap.behav = MADV_FREE;
254                                 madvise(&uap);
255                         }
256                 }
257                 bp->b_error = 0;
258         } else if (bp->b_flags & B_READ) {
259                 /*
260                  * Read data from our 'memory' disk
261                  */
262                 bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
263         } else {
264                 /*
265                  * Write data to our 'memory' disk
266                  */
267                 bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
268         }
269         if (bp->b_error)
270                 bp->b_flags |= B_ERROR;
271         biodone(bp);
272 }
273
274 /*
275  * This is a noop, simply returning what one has been given.
276  *
277  * mfs_bmap(struct vnode *a_vp, ufs_daddr_t a_bn, struct vnode **a_vpp,
278  *          ufs_daddr_t *a_bnp, int *a_runp)
279  */
280 static int
281 mfs_bmap(struct vop_bmap_args *ap)
282 {
283         if (ap->a_vpp != NULL)
284                 *ap->a_vpp = ap->a_vp;
285         if (ap->a_bnp != NULL)
286                 *ap->a_bnp = ap->a_bn;
287         if (ap->a_runp != NULL)
288                 *ap->a_runp = 0;
289         return (0);
290 }
291
292 /*
293  * Memory filesystem close routine
294  *
295  * mfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
296  *           struct thread *a_td)
297  */
298 /* ARGSUSED */
299 static int
300 mfs_close(struct vop_close_args *ap)
301 {
302         struct vnode *vp = ap->a_vp;
303         struct mfsnode *mfsp = VTOMFS(vp);
304         struct buf *bp;
305         int error;
306
307         /*
308          * Finish any pending I/O requests.
309          */
310         while ((bp = bufq_first(&mfsp->buf_queue)) != NULL) {
311                 bufq_remove(&mfsp->buf_queue, bp);
312                 mfs_doio(bp, mfsp);
313                 wakeup((caddr_t)bp);
314         }
315         /*
316          * On last close of a memory filesystem
317          * we must invalidate any in core blocks, so that
318          * we can, free up its vnode.
319          */
320         if ((error = vinvalbuf(vp, 1, ap->a_td, 0, 0)) != 0)
321                 return (error);
322         /*
323          * There should be no way to have any more uses of this
324          * vnode, so if we find any other uses, it is a panic.
325          */
326         if (vp->v_usecount > 1)
327                 printf("mfs_close: ref count %d > 1\n", vp->v_usecount);
328         if (vp->v_usecount > 1 || (bufq_first(&mfsp->buf_queue) != NULL))
329                 panic("mfs_close");
330         /*
331          * Send a request to the filesystem server to exit.
332          */
333         mfsp->mfs_active = 0;
334         wakeup((caddr_t)vp);
335         return (0);
336 }
337
338 /*
339  * Memory filesystem inactive routine
340  *
341  * mfs_inactive(struct vnode *a_vp, struct thread *a_td)
342  */
343 /* ARGSUSED */
344 static int
345 mfs_inactive(struct vop_inactive_args *ap)
346 {
347         struct vnode *vp = ap->a_vp;
348         struct mfsnode *mfsp = VTOMFS(vp);
349
350         if (bufq_first(&mfsp->buf_queue) != NULL)
351                 panic("mfs_inactive: not inactive (next buffer %p)",
352                         bufq_first(&mfsp->buf_queue));
353         VOP_UNLOCK(vp, NULL, 0, ap->a_td);
354         return (0);
355 }
356
357 /*
358  * Reclaim a memory filesystem devvp so that it can be reused.
359  *
360  * mfs_reclaim(struct vnode *a_vp)
361  */
362 static int
363 mfs_reclaim(struct vop_reclaim_args *ap)
364 {
365         struct vnode *vp = ap->a_vp;
366
367         FREE(vp->v_data, M_MFSNODE);
368         vp->v_data = NULL;
369         return (0);
370 }
371
372 /*
373  * Print out the contents of an mfsnode.
374  *
375  * mfs_print(struct vnode *a_vp)
376  */
377 static int
378 mfs_print(struct vop_print_args *ap)
379 {
380         struct mfsnode *mfsp = VTOMFS(ap->a_vp);
381
382         printf("tag VT_MFS, td %p, base %p, size %ld\n",
383             mfsp->mfs_td, (void *)mfsp->mfs_baseoff, mfsp->mfs_size);
384         return (0);
385 }
386
387 /*
388  * Block device bad operation
389  */
390 static int
391 mfs_badop(struct vop_generic_args *ap)
392 {
393         int i;
394
395         printf("mfs_badop[%s]\n", ap->a_desc->vdesc_name);
396         i = vop_defaultop(ap);
397         printf("mfs_badop[%s] = %d\n", ap->a_desc->vdesc_name,i);
398         return (i);
399 }
400
401 static int
402 mfs_getpages(struct vop_getpages_args *ap)
403 {
404         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_getpages), ap));
405 }