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