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