2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/sysproto.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>
56 #include <sys/thread2.h>
59 #include "mfs_extern.h"
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 */
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)
76 static struct vop_ops mfs_vnode_vops = {
77 .vop_default = mfs_badop,
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,
86 .vop_print = mfs_print,
87 .vop_reclaim = mfs_reclaim,
88 .vop_strategy = mfs_strategy,
91 struct vop_ops *mfs_vnode_vops_p = &mfs_vnode_vops;
93 VNODEOP_SET(mfs_vnode_vops);
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.
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.
106 * mfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
111 mfs_open(struct vop_open_args *ap)
113 struct vnode *vp = ap->a_vp;
115 if (vp->v_type != VCHR)
116 panic("mfs_open not VCHR");
119 //v_associate_rdev(vp, get_dev(vp->v_umajor, vp->v_uminor));
120 return (vop_stdopen(ap));
124 mfs_fsync(struct vop_fsync_args *ap)
126 return (VOCALL(&spec_vnode_vops, &ap->a_head));
130 * mfs_freeblks() - hook to allow us to free physical memory.
132 * We implement the BUF_CMD_FREEBLKS strategy. We can't just madvise()
133 * here because we have to do it in the correct order vs other bio
134 * requests, so we queue it.
136 * Note: geteblk() sets B_INVAL. We leave it set to guarentee buffer
137 * throw-away on brelse()? XXX
139 * mfs_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
142 mfs_freeblks(struct vop_freeblks_args *ap)
145 struct vnode *vp = ap->a_vp;
147 bp = geteblk(ap->a_length);
148 bp->b_cmd = BUF_CMD_FREEBLKS;
149 bp->b_bio1.bio_offset = ap->a_offset;
150 bp->b_bcount = ap->a_length;
152 vn_strategy(vp, &bp->b_bio1);
157 * Pass I/O requests to the memory filesystem process.
159 * mfs_strategy(struct vnode *a_vp, struct bio *a_bio)
162 mfs_strategy(struct vop_strategy_args *ap)
164 struct bio *bio = ap->a_bio;
165 struct buf *bp = bio->bio_buf;
166 struct mfsnode *mfsp;
167 struct thread *td = curthread; /* XXX */
169 mfsp = ap->a_vp->v_rdev->si_drv1;
172 bp->b_flags |= B_ERROR;
178 * splbio required for queueing/dequeueing, in case of forwarded
179 * BPs from bio interrupts (?). It may not be necessary.
184 if (mfsp->mfs_td == NULL) {
186 * mini-root. Note: BUF_CMD_FREEBLKS not supported at the
187 * moment, since we do not know what kind of dataspace
192 base = mfsp->mfs_baseoff + bio->bio_offset;
194 case BUF_CMD_FREEBLKS:
197 bcopy(base, bp->b_data, bp->b_bcount);
200 bcopy(bp->b_data, base, bp->b_bcount);
203 panic("mfs: bad b_cmd %d\n", bp->b_cmd);
206 } else if (mfsp->mfs_td == td) {
215 * VOP from some other process, queue to MFS process and
218 bioq_insert_tail(&mfsp->bio_queue, bio);
219 wakeup((caddr_t)mfsp);
226 * Memory file system I/O.
228 * Trivial on the HP since buffer has already been mapping into KVA space.
230 * Read and Write are handled with a simple copyin and copyout.
232 * We also partially support VOP_FREEBLKS(). We can't implement
233 * completely -- for example, on fragments or inode metadata, but we can
234 * implement it for page-aligned requests.
237 mfs_doio(struct bio *bio, struct mfsnode *mfsp)
239 struct buf *bp = bio->bio_buf;
240 caddr_t base = mfsp->mfs_baseoff + bio->bio_offset;
244 case BUF_CMD_FREEBLKS:
246 * Implement FREEBLKS, which allows the filesystem to tell
247 * a block device when blocks are no longer needed (like when
248 * a file is deleted). We use the hook to MADV_FREE the VM.
249 * This makes an MFS filesystem work as well or better then
250 * a sun-style swap-mounted filesystem.
252 bytes = bp->b_bcount;
254 if ((vm_offset_t)base & PAGE_MASK) {
255 int n = PAGE_SIZE - ((vm_offset_t)base & PAGE_MASK);
260 struct madvise_args uap;
264 bzero(&uap, sizeof(uap));
267 uap.behav = MADV_FREE;
275 * Read data from our 'memory' disk
277 bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
281 * Write data to our 'memory' disk
283 bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
286 panic("mfs: bad b_cmd %d\n", bp->b_cmd);
289 bp->b_flags |= B_ERROR;
294 * This is a noop, simply returning what one has been given.
296 * mfs_bmap(struct vnode *a_vp, off_t a_loffset,
297 * off_t *a_doffsetp, int *a_runp, int *a_runb)
300 mfs_bmap(struct vop_bmap_args *ap)
302 if (ap->a_doffsetp != NULL)
303 *ap->a_doffsetp = ap->a_loffset;
304 if (ap->a_runp != NULL)
306 if (ap->a_runb != NULL)
312 * Memory filesystem close routine
314 * mfs_close(struct vnode *a_vp, int a_fflag)
318 mfs_close(struct vop_close_args *ap)
320 struct vnode *vp = ap->a_vp;
321 struct mfsnode *mfsp = VTOMFS(vp);
326 * Finish any pending I/O requests.
328 while ((bio = bioq_first(&mfsp->bio_queue)) != NULL) {
329 bioq_remove(&mfsp->bio_queue, bio);
331 wakeup((caddr_t)bio->bio_buf);
335 * We really only care about the last close
337 if (vp->v_opencount > 1)
341 * Synchronize any remaining buffers and then destroy them.
343 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0)
347 * Get rid of the pseudo-backing object. Since the object is
348 * not directly memory mapped, we don't have to worry about
352 vm_pager_deallocate(vp->v_object);
355 * There should be no way to have any more uses of this
356 * vnode, so if we find any other uses, it is a panic.
358 if (vp->v_sysref.refcnt > 1)
359 kprintf("mfs_close: ref count %d > 1\n", vp->v_sysref.refcnt);
360 if (vp->v_sysref.refcnt > 1 || (bioq_first(&mfsp->bio_queue) != NULL))
363 * Send a request to the filesystem server to exit.
365 mfsp->mfs_active = 0;
368 destroy_dev(mfsp->mfs_dev);
369 mfsp->mfs_dev = NULL;
371 wakeup((caddr_t)mfsp);
378 * Memory filesystem inactive routine
380 * mfs_inactive(struct vnode *a_vp)
384 mfs_inactive(struct vop_inactive_args *ap)
386 struct vnode *vp = ap->a_vp;
387 struct mfsnode *mfsp = VTOMFS(vp);
389 if (bioq_first(&mfsp->bio_queue) != NULL)
390 panic("mfs_inactive: not inactive (next buffer %p)",
391 bioq_first(&mfsp->bio_queue));
396 * Reclaim a memory filesystem devvp so that it can be reused.
398 * mfs_reclaim(struct vnode *a_vp)
401 mfs_reclaim(struct vop_reclaim_args *ap)
403 struct vnode *vp = ap->a_vp;
405 FREE(vp->v_data, M_MFSNODE);
411 * Print out the contents of an mfsnode.
413 * mfs_print(struct vnode *a_vp)
416 mfs_print(struct vop_print_args *ap)
418 struct mfsnode *mfsp = VTOMFS(ap->a_vp);
420 kprintf("tag VT_MFS, td %p, base %p, size %ld\n",
421 mfsp->mfs_td, (void *)mfsp->mfs_baseoff, mfsp->mfs_size);
426 * Block device bad operation
429 mfs_badop(struct vop_generic_args *ap)
433 kprintf("mfs_badop[%s]\n", ap->a_desc->sd_name);
434 i = vop_defaultop(ap);
435 kprintf("mfs_badop[%s] = %d\n", ap->a_desc->sd_name, i);
440 mfs_getpages(struct vop_getpages_args *ap)
442 return (VOCALL(&spec_vnode_vops, &ap->a_head));