MPSAFE - tsleep_interlock, BUF/BIO, cluster, swap_pager.
[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.37 2007/08/13 17:31:56 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_open =             mfs_open,
86         .vop_print =            mfs_print,
87         .vop_reclaim =          mfs_reclaim,
88         .vop_strategy =         mfs_strategy,
89 };
90
91 struct vop_ops *mfs_vnode_vops_p = &mfs_vnode_vops;
92
93 VNODEOP_SET(mfs_vnode_vops);
94
95 /*
96  * Vnode Operations.
97  *
98  * Open called to allow memory filesystem to initialize and
99  * validate before actual IO. Record our process identifier
100  * so we can tell when we are doing I/O to ourself.
101  *
102  * NOTE: new device sequencing.  mounts check the device reference count
103  * before calling open, so we must associate the device in open and 
104  * disassociate it in close rather then faking it when we created the vnode.
105  *
106  * mfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
107  *          struct file *a_fp)
108  */
109 /* ARGSUSED */
110 static int
111 mfs_open(struct vop_open_args *ap)
112 {
113         struct vnode *vp = ap->a_vp;
114
115         if (vp->v_type != VCHR)
116                 panic("mfs_open not VCHR");
117         v_associate_rdev(vp, get_dev(vp->v_umajor, vp->v_uminor));
118         return (vop_stdopen(ap));
119 }
120
121 static int
122 mfs_fsync(struct vop_fsync_args *ap)
123 {
124         return (VOCALL(&spec_vnode_vops, &ap->a_head));
125 }
126
127 /*
128  * mfs_freeblks() - hook to allow us to free physical memory.
129  *
130  *      We implement the BUF_CMD_FREEBLKS 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  * mfs_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
138  */
139 static int
140 mfs_freeblks(struct vop_freeblks_args *ap)
141 {       
142         struct buf *bp;
143         struct vnode *vp = ap->a_vp;
144
145         bp = geteblk(ap->a_length);
146         bp->b_cmd = BUF_CMD_FREEBLKS;
147         bp->b_bio1.bio_offset = ap->a_offset;
148         bp->b_bcount = ap->a_length;
149         BUF_KERNPROC(bp);
150         vn_strategy(vp, &bp->b_bio1);
151         return(0);
152 }
153
154 /*
155  * Pass I/O requests to the memory filesystem process.
156  *
157  * mfs_strategy(struct vnode *a_vp, struct bio *a_bio)
158  */
159 static int
160 mfs_strategy(struct vop_strategy_args *ap)
161 {
162         struct bio *bio = ap->a_bio;
163         struct buf *bp = bio->bio_buf;
164         struct mfsnode *mfsp;
165         struct thread *td = curthread;          /* XXX */
166
167         mfsp = ap->a_vp->v_rdev->si_drv1;
168         if (mfsp == NULL) {
169                 bp->b_error = ENXIO;
170                 bp->b_flags |= B_ERROR;
171                 biodone(bio);
172                 return(0);
173         }
174
175         /*
176          * splbio required for queueing/dequeueing, in case of forwarded
177          * BPs from bio interrupts (?).  It may not be necessary.
178          */
179
180         crit_enter();
181
182         if (mfsp->mfs_td == NULL) {
183                 /*
184                  * mini-root.  Note: BUF_CMD_FREEBLKS not supported at the
185                  * moment, since we do not know what kind of dataspace
186                  * b_data is in.
187                  */
188                 caddr_t base;
189
190                 base = mfsp->mfs_baseoff + bio->bio_offset;
191                 switch(bp->b_cmd) {
192                 case BUF_CMD_FREEBLKS:
193                         break;
194                 case BUF_CMD_READ:
195                         bcopy(base, bp->b_data, bp->b_bcount);
196                         break;
197                 case BUF_CMD_WRITE:
198                         bcopy(bp->b_data, base, bp->b_bcount);
199                         break;
200                 default:
201                         panic("mfs: bad b_cmd %d\n", bp->b_cmd);
202                 }
203                 biodone(bio);
204         } else if (mfsp->mfs_td == td) {
205                 /*
206                  * VOP to self
207                  */
208                 crit_exit();
209                 mfs_doio(bio, mfsp);
210                 crit_enter();
211         } else {
212                 /*
213                  * VOP from some other process, queue to MFS process and
214                  * wake it up.
215                  */
216                 bioq_insert_tail(&mfsp->bio_queue, bio);
217                 wakeup((caddr_t)mfsp);
218         }
219         crit_exit();
220         return (0);
221 }
222
223 /*
224  * Memory file system I/O.
225  *
226  * Trivial on the HP since buffer has already been mapping into KVA space.
227  *
228  * Read and Write are handled with a simple copyin and copyout.    
229  *
230  * We also partially support VOP_FREEBLKS().  We can't implement
231  * completely -- for example, on fragments or inode metadata, but we can
232  * implement it for page-aligned requests.
233  */
234 void
235 mfs_doio(struct bio *bio, struct mfsnode *mfsp)
236 {
237         struct buf *bp = bio->bio_buf;
238         caddr_t base = mfsp->mfs_baseoff + bio->bio_offset;
239         int bytes;
240
241         switch(bp->b_cmd) {
242         case BUF_CMD_FREEBLKS:
243                 /*
244                  * Implement FREEBLKS, 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                 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                                 sys_madvise(&uap);
267                         }
268                 }
269                 bp->b_error = 0;
270                 break;
271         case BUF_CMD_READ:
272                 /*
273                  * Read data from our 'memory' disk
274                  */
275                 bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
276                 break;
277         case BUF_CMD_WRITE:
278                 /*
279                  * Write data to our 'memory' disk
280                  */
281                 bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
282                 break;
283         default:
284                 panic("mfs: bad b_cmd %d\n", bp->b_cmd);
285         }
286         if (bp->b_error)
287                 bp->b_flags |= B_ERROR;
288         biodone(bio);
289 }
290
291 /*
292  * This is a noop, simply returning what one has been given.
293  *
294  * mfs_bmap(struct vnode *a_vp, off_t a_loffset,
295  *          off_t *a_doffsetp, int *a_runp, int *a_runb)
296  */
297 static int
298 mfs_bmap(struct vop_bmap_args *ap)
299 {
300         if (ap->a_doffsetp != NULL)
301                 *ap->a_doffsetp = ap->a_loffset;
302         if (ap->a_runp != NULL)
303                 *ap->a_runp = 0;
304         if (ap->a_runb != NULL)
305                 *ap->a_runb = 0;
306         return (0);
307 }
308
309 /*
310  * Memory filesystem close routine
311  *
312  * mfs_close(struct vnode *a_vp, int a_fflag)
313  */
314 /* ARGSUSED */
315 static int
316 mfs_close(struct vop_close_args *ap)
317 {
318         struct vnode *vp = ap->a_vp;
319         struct mfsnode *mfsp = VTOMFS(vp);
320         struct bio *bio;
321         int error = 0;
322
323         /*
324          * Finish any pending I/O requests.
325          */
326         while ((bio = bioq_first(&mfsp->bio_queue)) != NULL) {
327                 bioq_remove(&mfsp->bio_queue, bio);
328                 mfs_doio(bio, mfsp);
329                 wakeup((caddr_t)bio->bio_buf);
330         }
331
332         /*
333          * We really only care about the last close
334          */
335         if (vp->v_opencount > 1)
336                 goto done;
337
338         /*
339          * Synchronize any remaining buffers and then destroy them.
340          */
341         if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0)
342                 goto done;
343
344         /*
345          * Get rid of the pseudo-backing object.  Since the object is
346          * not directly memory mapped, we don't have to worry about 
347          * synchronizing it.
348          */
349         if (vp->v_object)
350                 vm_pager_deallocate(vp->v_object);
351
352         /*
353          * There should be no way to have any more uses of this
354          * vnode, so if we find any other uses, it is a panic.
355          */
356         if (vp->v_sysref.refcnt > 1)
357                 kprintf("mfs_close: ref count %d > 1\n", vp->v_sysref.refcnt);
358         if (vp->v_sysref.refcnt > 1 || (bioq_first(&mfsp->bio_queue) != NULL))
359                 panic("mfs_close");
360         /*
361          * Send a request to the filesystem server to exit.
362          */
363         mfsp->mfs_active = 0;
364         v_release_rdev(vp);
365         if (mfsp->mfs_dev) {
366                 destroy_dev(mfsp->mfs_dev);
367                 mfsp->mfs_dev = NULL;
368         }
369         wakeup((caddr_t)mfsp);
370 done:
371         vop_stdclose(ap);
372         return (error);
373 }
374
375 /*
376  * Memory filesystem inactive routine
377  *
378  * mfs_inactive(struct vnode *a_vp)
379  */
380 /* ARGSUSED */
381 static int
382 mfs_inactive(struct vop_inactive_args *ap)
383 {
384         struct vnode *vp = ap->a_vp;
385         struct mfsnode *mfsp = VTOMFS(vp);
386
387         if (bioq_first(&mfsp->bio_queue) != NULL)
388                 panic("mfs_inactive: not inactive (next buffer %p)",
389                         bioq_first(&mfsp->bio_queue));
390         return (0);
391 }
392
393 /*
394  * Reclaim a memory filesystem devvp so that it can be reused.
395  *
396  * mfs_reclaim(struct vnode *a_vp)
397  */
398 static int
399 mfs_reclaim(struct vop_reclaim_args *ap)
400 {
401         struct vnode *vp = ap->a_vp;
402
403         FREE(vp->v_data, M_MFSNODE);
404         vp->v_data = NULL;
405         return (0);
406 }
407
408 /*
409  * Print out the contents of an mfsnode.
410  *
411  * mfs_print(struct vnode *a_vp)
412  */
413 static int
414 mfs_print(struct vop_print_args *ap)
415 {
416         struct mfsnode *mfsp = VTOMFS(ap->a_vp);
417
418         kprintf("tag VT_MFS, td %p, base %p, size %ld\n",
419             mfsp->mfs_td, (void *)mfsp->mfs_baseoff, mfsp->mfs_size);
420         return (0);
421 }
422
423 /*
424  * Block device bad operation
425  */
426 static int
427 mfs_badop(struct vop_generic_args *ap)
428 {
429         int i;
430
431         kprintf("mfs_badop[%s]\n", ap->a_desc->sd_name);
432         i = vop_defaultop(ap);
433         kprintf("mfs_badop[%s] = %d\n", ap->a_desc->sd_name, i);
434         return (i);
435 }
436
437 static int
438 mfs_getpages(struct vop_getpages_args *ap)
439 {
440         return (VOCALL(&spec_vnode_vops, &ap->a_head));
441 }