Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:59 dillon 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 <ufs/mfs/mfsnode.h>
50 #include <ufs/mfs/mfs_extern.h>
51
52 static int      mfs_badop __P((struct vop_generic_args *));
53 static int      mfs_bmap __P((struct vop_bmap_args *));
54 static int      mfs_close __P((struct vop_close_args *));
55 static int      mfs_fsync __P((struct vop_fsync_args *));
56 static int      mfs_freeblks __P((struct vop_freeblks_args *));
57 static int      mfs_inactive __P((struct vop_inactive_args *)); /* XXX */
58 static int      mfs_open __P((struct vop_open_args *));
59 static int      mfs_reclaim __P((struct vop_reclaim_args *)); /* XXX */
60 static int      mfs_print __P((struct vop_print_args *)); /* XXX */
61 static int      mfs_strategy __P((struct vop_strategy_args *)); /* XXX */
62 static int      mfs_getpages __P((struct vop_getpages_args *)); /* XXX */
63 /*
64  * mfs vnode operations.
65  */
66 vop_t **mfs_vnodeop_p;
67 static struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
68         { &vop_default_desc,            (vop_t *) mfs_badop },
69         { &vop_bmap_desc,               (vop_t *) mfs_bmap },
70         { &vop_bwrite_desc,             (vop_t *) vop_defaultop },
71         { &vop_close_desc,              (vop_t *) mfs_close },
72         { &vop_createvobject_desc,      (vop_t *) vop_stdcreatevobject },
73         { &vop_destroyvobject_desc,     (vop_t *) vop_stddestroyvobject },
74         { &vop_freeblks_desc,           (vop_t *) mfs_freeblks },
75         { &vop_fsync_desc,              (vop_t *) mfs_fsync },
76         { &vop_getpages_desc,           (vop_t *) mfs_getpages },
77         { &vop_getvobject_desc,         (vop_t *) vop_stdgetvobject },
78         { &vop_inactive_desc,           (vop_t *) mfs_inactive },
79         { &vop_ioctl_desc,              (vop_t *) vop_enotty },
80         { &vop_islocked_desc,           (vop_t *) vop_defaultop },
81         { &vop_lock_desc,               (vop_t *) vop_defaultop },
82         { &vop_open_desc,               (vop_t *) mfs_open },
83         { &vop_print_desc,              (vop_t *) mfs_print },
84         { &vop_reclaim_desc,            (vop_t *) mfs_reclaim },
85         { &vop_strategy_desc,           (vop_t *) mfs_strategy },
86         { &vop_unlock_desc,             (vop_t *) vop_defaultop },
87         { NULL, NULL }
88 };
89 static struct vnodeopv_desc mfs_vnodeop_opv_desc =
90         { &mfs_vnodeop_p, mfs_vnodeop_entries };
91
92 VNODEOP_SET(mfs_vnodeop_opv_desc);
93
94 /*
95  * Vnode Operations.
96  *
97  * Open called to allow memory filesystem to initialize and
98  * validate before actual IO. Record our process identifier
99  * so we can tell when we are doing I/O to ourself.
100  */
101 /* ARGSUSED */
102 static int
103 mfs_open(ap)
104         struct vop_open_args /* {
105                 struct vnode *a_vp;
106                 int  a_mode;
107                 struct ucred *a_cred;
108                 struct proc *a_p;
109         } */ *ap;
110 {
111
112         if (ap->a_vp->v_type != VBLK) {
113                 panic("mfs_open not VBLK");
114                 /* NOTREACHED */
115         }
116         return (0);
117 }
118
119 static int
120 mfs_fsync(ap)
121         struct vop_fsync_args *ap;
122 {
123
124         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_fsync), ap));
125 }
126
127 /*
128  * mfs_freeblks() - hook to allow us to free physical memory.
129  *
130  *      We implement the B_FREEBUF strategy.  We can't just madvise()
131  *      here because we have to do it in the correct order vs other bio
132  *      requests, so we queue it.
133  *
134  *      Note: geteblk() sets B_INVAL.  We leave it set to guarentee buffer
135  *      throw-away on brelse()? XXX
136  */
137
138 static int
139 mfs_freeblks(ap)
140         struct vop_freeblks_args /* {   
141                 struct vnode *a_vp;     
142                 daddr_t a_addr;         
143                 daddr_t a_length;       
144         } */ *ap;
145 {       
146         struct buf *bp;
147         struct vnode *vp;
148
149         if (!vfinddev(ap->a_vp->v_rdev, VBLK, &vp) || vp->v_usecount == 0)
150                 panic("mfs_freeblks: bad dev");
151
152         bp = geteblk(ap->a_length);
153         bp->b_flags |= B_FREEBUF | B_ASYNC;
154         bp->b_dev = ap->a_vp->v_rdev;
155         bp->b_blkno = ap->a_addr;
156         bp->b_offset = dbtob(ap->a_addr);
157         bp->b_bcount = ap->a_length;
158         BUF_KERNPROC(bp);
159         VOP_STRATEGY(vp, bp);
160         return(0);
161 }
162
163 /*
164  * Pass I/O requests to the memory filesystem process.
165  */
166 static int
167 mfs_strategy(ap)
168         struct vop_strategy_args /* {
169                 struct vnode *a_vp;
170                 struct buf *a_bp;
171         } */ *ap;
172 {
173         register struct buf *bp = ap->a_bp;
174         register struct mfsnode *mfsp;
175         struct vnode *vp;
176         struct proc *p = curproc;               /* XXX */
177         int s;
178
179         if (!vfinddev(bp->b_dev, VBLK, &vp) || vp->v_usecount == 0)
180                 panic("mfs_strategy: bad dev");
181         mfsp = VTOMFS(vp);
182
183         /*
184          * splbio required for queueing/dequeueing, in case of forwarded
185          * BPs from bio interrupts (?).  It may not be necessary.
186          */
187
188         s = splbio();
189
190         if (mfsp->mfs_pid == 0) {
191                 /*
192                  * mini-root.  Note: B_FREEBUF not supported at the moment,
193                  * I'm not sure what kind of dataspace b_data is in.
194                  */
195                 caddr_t base;
196
197                 base = mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
198                 if (bp->b_flags & B_FREEBUF)
199                         ;
200                 if (bp->b_flags & B_READ)
201                         bcopy(base, bp->b_data, bp->b_bcount);
202                 else
203                         bcopy(bp->b_data, base, bp->b_bcount);
204                 biodone(bp);
205         } else if (mfsp->mfs_pid == p->p_pid) {
206                 /*
207                  * VOP to self
208                  */
209                 splx(s);
210                 mfs_doio(bp, mfsp);
211                 s = splbio();
212         } else {
213                 /*
214                  * VOP from some other process, queue to MFS process and
215                  * wake it up.
216                  */
217                 bufq_insert_tail(&mfsp->buf_queue, bp);
218                 wakeup((caddr_t)vp);
219         }
220         splx(s);
221         return (0);
222 }
223
224 /*
225  * Memory file system I/O.
226  *
227  * Trivial on the HP since buffer has already been mapping into KVA space.
228  *
229  * Read and Write are handled with a simple copyin and copyout.    
230  *
231  * We also partially support VOP_FREEBLKS() via B_FREEBUF.  We can't implement
232  * completely -- for example, on fragments or inode metadata, but we can
233  * implement it for page-aligned requests.
234  */
235 void
236 mfs_doio(bp, mfsp)
237         register struct buf *bp;
238         struct mfsnode *mfsp;
239 {
240         caddr_t base = mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
241
242         if (bp->b_flags & B_FREEBUF) {
243                 /*
244                  * Implement B_FREEBUF, which allows the filesystem to tell
245                  * a block device when blocks are no longer needed (like when
246                  * a file is deleted).  We use the hook to MADV_FREE the VM.
247                  * This makes an MFS filesystem work as well or better then
248                  * a sun-style swap-mounted filesystem.
249                  */
250                 int bytes = bp->b_bcount;
251
252                 if ((vm_offset_t)base & PAGE_MASK) {
253                         int n = PAGE_SIZE - ((vm_offset_t)base & PAGE_MASK);
254                         bytes -= n;
255                         base += n;
256                 }
257                 if (bytes > 0) {
258                         struct madvise_args uap;
259
260                         bytes &= ~PAGE_MASK;
261                         if (bytes != 0) {
262                                 bzero(&uap, sizeof(uap));
263                                 uap.addr  = base;
264                                 uap.len   = bytes;
265                                 uap.behav = MADV_FREE;
266                                 madvise(curproc, &uap);
267                         }
268                 }
269                 bp->b_error = 0;
270         } else if (bp->b_flags & B_READ) {
271                 /*
272                  * Read data from our 'memory' disk
273                  */
274                 bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
275         } else {
276                 /*
277                  * Write data to our 'memory' disk
278                  */
279                 bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
280         }
281         if (bp->b_error)
282                 bp->b_flags |= B_ERROR;
283         biodone(bp);
284 }
285
286 /*
287  * This is a noop, simply returning what one has been given.
288  */
289 static int
290 mfs_bmap(ap)
291         struct vop_bmap_args /* {
292                 struct vnode *a_vp;
293                 ufs_daddr_t  a_bn;
294                 struct vnode **a_vpp;
295                 ufs_daddr_t *a_bnp;
296                 int *a_runp;
297         } */ *ap;
298 {
299
300         if (ap->a_vpp != NULL)
301                 *ap->a_vpp = ap->a_vp;
302         if (ap->a_bnp != NULL)
303                 *ap->a_bnp = ap->a_bn;
304         if (ap->a_runp != NULL)
305                 *ap->a_runp = 0;
306         return (0);
307 }
308
309 /*
310  * Memory filesystem close routine
311  */
312 /* ARGSUSED */
313 static int
314 mfs_close(ap)
315         struct vop_close_args /* {
316                 struct vnode *a_vp;
317                 int  a_fflag;
318                 struct ucred *a_cred;
319                 struct proc *a_p;
320         } */ *ap;
321 {
322         register struct vnode *vp = ap->a_vp;
323         register struct mfsnode *mfsp = VTOMFS(vp);
324         register struct buf *bp;
325         int error;
326
327         /*
328          * Finish any pending I/O requests.
329          */
330         while ((bp = bufq_first(&mfsp->buf_queue)) != NULL) {
331                 bufq_remove(&mfsp->buf_queue, bp);
332                 mfs_doio(bp, mfsp);
333                 wakeup((caddr_t)bp);
334         }
335         /*
336          * On last close of a memory filesystem
337          * we must invalidate any in core blocks, so that
338          * we can, free up its vnode.
339          */
340         if ((error = vinvalbuf(vp, 1, ap->a_cred, ap->a_p, 0, 0)) != 0)
341                 return (error);
342         /*
343          * There should be no way to have any more uses of this
344          * vnode, so if we find any other uses, it is a panic.
345          */
346         if (vp->v_usecount > 1)
347                 printf("mfs_close: ref count %d > 1\n", vp->v_usecount);
348         if (vp->v_usecount > 1 || (bufq_first(&mfsp->buf_queue) != NULL))
349                 panic("mfs_close");
350         /*
351          * Send a request to the filesystem server to exit.
352          */
353         mfsp->mfs_active = 0;
354         wakeup((caddr_t)vp);
355         return (0);
356 }
357
358 /*
359  * Memory filesystem inactive routine
360  */
361 /* ARGSUSED */
362 static int
363 mfs_inactive(ap)
364         struct vop_inactive_args /* {
365                 struct vnode *a_vp;
366                 struct proc *a_p;
367         } */ *ap;
368 {
369         struct vnode *vp = ap->a_vp;
370         struct mfsnode *mfsp = VTOMFS(vp);
371
372         if (bufq_first(&mfsp->buf_queue) != NULL)
373                 panic("mfs_inactive: not inactive (next buffer %p)",
374                         bufq_first(&mfsp->buf_queue));
375         VOP_UNLOCK(vp, 0, ap->a_p);
376         return (0);
377 }
378
379 /*
380  * Reclaim a memory filesystem devvp so that it can be reused.
381  */
382 static int
383 mfs_reclaim(ap)
384         struct vop_reclaim_args /* {
385                 struct vnode *a_vp;
386         } */ *ap;
387 {
388         register struct vnode *vp = ap->a_vp;
389
390         FREE(vp->v_data, M_MFSNODE);
391         vp->v_data = NULL;
392         return (0);
393 }
394
395 /*
396  * Print out the contents of an mfsnode.
397  */
398 static int
399 mfs_print(ap)
400         struct vop_print_args /* {
401                 struct vnode *a_vp;
402         } */ *ap;
403 {
404         register struct mfsnode *mfsp = VTOMFS(ap->a_vp);
405
406         printf("tag VT_MFS, pid %ld, base %p, size %ld\n",
407             (long)mfsp->mfs_pid, (void *)mfsp->mfs_baseoff, mfsp->mfs_size);
408         return (0);
409 }
410
411 /*
412  * Block device bad operation
413  */
414 static int
415 mfs_badop(struct vop_generic_args *ap)
416 {
417         int i;
418
419         printf("mfs_badop[%s]\n", ap->a_desc->vdesc_name);
420         i = vop_defaultop(ap);
421         printf("mfs_badop[%s] = %d\n", ap->a_desc->vdesc_name,i);
422         return (i);
423 }
424
425
426 static int
427 mfs_getpages(ap)
428         struct vop_getpages_args *ap;
429 {
430
431         return (VOCALL(spec_vnodeop_p, VOFFSET(vop_getpages), ap));
432 }