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