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