mfs_vnops.c revision 1.62 1 1.62 dholland /* $NetBSD: mfs_vnops.c,v 1.62 2021/06/29 22:34:10 dholland Exp $ */
2 1.22 thorpej
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1989, 1993
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.34 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 mycroft * may be used to endorse or promote products derived from this software
17 1.1 mycroft * without specific prior written permission.
18 1.1 mycroft *
19 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 mycroft * SUCH DAMAGE.
30 1.1 mycroft *
31 1.13 fvdl * @(#)mfs_vnops.c 8.11 (Berkeley) 5/22/95
32 1.1 mycroft */
33 1.28 lukem
34 1.28 lukem #include <sys/cdefs.h>
35 1.62 dholland __KERNEL_RCSID(0, "$NetBSD: mfs_vnops.c,v 1.62 2021/06/29 22:34:10 dholland Exp $");
36 1.1 mycroft
37 1.1 mycroft #include <sys/param.h>
38 1.1 mycroft #include <sys/systm.h>
39 1.1 mycroft #include <sys/time.h>
40 1.1 mycroft #include <sys/kernel.h>
41 1.1 mycroft #include <sys/proc.h>
42 1.1 mycroft #include <sys/buf.h>
43 1.37 yamt #include <sys/bufq.h>
44 1.1 mycroft #include <sys/vnode.h>
45 1.49 ad #include <sys/kmem.h>
46 1.1 mycroft
47 1.9 mycroft #include <miscfs/genfs/genfs.h>
48 1.1 mycroft #include <miscfs/specfs/specdev.h>
49 1.1 mycroft
50 1.23 thorpej #include <machine/vmparam.h>
51 1.23 thorpej
52 1.1 mycroft #include <ufs/mfs/mfsnode.h>
53 1.1 mycroft #include <ufs/mfs/mfs_extern.h>
54 1.1 mycroft
55 1.1 mycroft /*
56 1.1 mycroft * mfs vnode operations.
57 1.1 mycroft */
58 1.39 xtraeme int (**mfs_vnodeop_p)(void *);
59 1.27 jdolecek const struct vnodeopv_entry_desc mfs_vnodeop_entries[] = {
60 1.1 mycroft { &vop_default_desc, vn_default_error },
61 1.62 dholland { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
62 1.1 mycroft { &vop_lookup_desc, mfs_lookup }, /* lookup */
63 1.1 mycroft { &vop_create_desc, mfs_create }, /* create */
64 1.1 mycroft { &vop_mknod_desc, mfs_mknod }, /* mknod */
65 1.1 mycroft { &vop_open_desc, mfs_open }, /* open */
66 1.1 mycroft { &vop_close_desc, mfs_close }, /* close */
67 1.1 mycroft { &vop_access_desc, mfs_access }, /* access */
68 1.61 christos { &vop_accessx_desc, genfs_accessx }, /* accessx */
69 1.1 mycroft { &vop_getattr_desc, mfs_getattr }, /* getattr */
70 1.1 mycroft { &vop_setattr_desc, mfs_setattr }, /* setattr */
71 1.1 mycroft { &vop_read_desc, mfs_read }, /* read */
72 1.1 mycroft { &vop_write_desc, mfs_write }, /* write */
73 1.55 dholland { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
74 1.55 dholland { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
75 1.1 mycroft { &vop_ioctl_desc, mfs_ioctl }, /* ioctl */
76 1.10 mycroft { &vop_poll_desc, mfs_poll }, /* poll */
77 1.13 fvdl { &vop_revoke_desc, mfs_revoke }, /* revoke */
78 1.1 mycroft { &vop_mmap_desc, mfs_mmap }, /* mmap */
79 1.52 christos { &vop_fsync_desc, spec_fsync }, /* fsync */
80 1.1 mycroft { &vop_seek_desc, mfs_seek }, /* seek */
81 1.1 mycroft { &vop_remove_desc, mfs_remove }, /* remove */
82 1.1 mycroft { &vop_link_desc, mfs_link }, /* link */
83 1.1 mycroft { &vop_rename_desc, mfs_rename }, /* rename */
84 1.1 mycroft { &vop_mkdir_desc, mfs_mkdir }, /* mkdir */
85 1.1 mycroft { &vop_rmdir_desc, mfs_rmdir }, /* rmdir */
86 1.1 mycroft { &vop_symlink_desc, mfs_symlink }, /* symlink */
87 1.1 mycroft { &vop_readdir_desc, mfs_readdir }, /* readdir */
88 1.1 mycroft { &vop_readlink_desc, mfs_readlink }, /* readlink */
89 1.1 mycroft { &vop_abortop_desc, mfs_abortop }, /* abortop */
90 1.1 mycroft { &vop_inactive_desc, mfs_inactive }, /* inactive */
91 1.1 mycroft { &vop_reclaim_desc, mfs_reclaim }, /* reclaim */
92 1.48 ad { &vop_lock_desc, genfs_nolock }, /* lock */
93 1.48 ad { &vop_unlock_desc, genfs_nounlock }, /* unlock */
94 1.1 mycroft { &vop_bmap_desc, mfs_bmap }, /* bmap */
95 1.1 mycroft { &vop_strategy_desc, mfs_strategy }, /* strategy */
96 1.1 mycroft { &vop_print_desc, mfs_print }, /* print */
97 1.1 mycroft { &vop_islocked_desc, mfs_islocked }, /* islocked */
98 1.1 mycroft { &vop_pathconf_desc, mfs_pathconf }, /* pathconf */
99 1.1 mycroft { &vop_advlock_desc, mfs_advlock }, /* advlock */
100 1.1 mycroft { &vop_bwrite_desc, mfs_bwrite }, /* bwrite */
101 1.29 chs { &vop_putpages_desc, mfs_putpages }, /* putpages */
102 1.29 chs { NULL, NULL }
103 1.1 mycroft };
104 1.27 jdolecek const struct vnodeopv_desc mfs_vnodeop_opv_desc =
105 1.1 mycroft { &mfs_vnodeop_p, mfs_vnodeop_entries };
106 1.1 mycroft
107 1.1 mycroft /*
108 1.1 mycroft * Vnode Operations.
109 1.1 mycroft *
110 1.1 mycroft * Open called to allow memory filesystem to initialize and
111 1.1 mycroft * validate before actual IO. Record our process identifier
112 1.1 mycroft * so we can tell when we are doing I/O to ourself.
113 1.1 mycroft */
114 1.1 mycroft /* ARGSUSED */
115 1.1 mycroft int
116 1.39 xtraeme mfs_open(void *v)
117 1.6 christos {
118 1.1 mycroft struct vop_open_args /* {
119 1.1 mycroft struct vnode *a_vp;
120 1.1 mycroft int a_mode;
121 1.43 elad kauth_cred_t a_cred;
122 1.6 christos } */ *ap = v;
123 1.1 mycroft
124 1.1 mycroft if (ap->a_vp->v_type != VBLK) {
125 1.1 mycroft panic("mfs_ioctl not VBLK");
126 1.1 mycroft /* NOTREACHED */
127 1.1 mycroft }
128 1.1 mycroft return (0);
129 1.1 mycroft }
130 1.1 mycroft
131 1.1 mycroft /*
132 1.1 mycroft * Pass I/O requests to the memory filesystem process.
133 1.1 mycroft */
134 1.1 mycroft int
135 1.39 xtraeme mfs_strategy(void *v)
136 1.6 christos {
137 1.1 mycroft struct vop_strategy_args /* {
138 1.36 hannken struct vnode *a_vp;
139 1.1 mycroft struct buf *a_bp;
140 1.6 christos } */ *ap = v;
141 1.36 hannken struct vnode *vp = ap->a_vp;
142 1.20 augustss struct buf *bp = ap->a_bp;
143 1.20 augustss struct mfsnode *mfsp;
144 1.1 mycroft
145 1.60 ad if (vp->v_type != VBLK || vrefcnt(vp) == 0)
146 1.1 mycroft panic("mfs_strategy: bad dev");
147 1.1 mycroft mfsp = VTOMFS(vp);
148 1.23 thorpej /* check for mini-root access */
149 1.23 thorpej if (mfsp->mfs_proc == NULL) {
150 1.44 christos void *base;
151 1.22 thorpej
152 1.44 christos base = (char *)mfsp->mfs_baseoff + (bp->b_blkno << DEV_BSHIFT);
153 1.1 mycroft if (bp->b_flags & B_READ)
154 1.14 perry memcpy(bp->b_data, base, bp->b_bcount);
155 1.1 mycroft else
156 1.14 perry memcpy(base, bp->b_data, bp->b_bcount);
157 1.25 thorpej bp->b_resid = 0;
158 1.23 thorpej biodone(bp);
159 1.48 ad } else if (mfsp->mfs_proc == curproc) {
160 1.23 thorpej mfs_doio(bp, mfsp->mfs_baseoff);
161 1.24 sommerfe } else if (doing_shutdown) {
162 1.38 perry /*
163 1.24 sommerfe * bitbucket I/O during shutdown.
164 1.24 sommerfe * Note that reads should *not* happen here, but..
165 1.24 sommerfe */
166 1.24 sommerfe if (bp->b_flags & B_READ)
167 1.24 sommerfe printf("warning: mfs read during shutdown\n");
168 1.25 thorpej bp->b_resid = 0;
169 1.24 sommerfe biodone(bp);
170 1.1 mycroft } else {
171 1.49 ad mutex_enter(&mfs_lock);
172 1.53 yamt bufq_put(mfsp->mfs_buflist, bp);
173 1.48 ad cv_broadcast(&mfsp->mfs_cv);
174 1.49 ad mutex_exit(&mfs_lock);
175 1.23 thorpej }
176 1.23 thorpej return (0);
177 1.23 thorpej }
178 1.22 thorpej
179 1.23 thorpej /*
180 1.23 thorpej * Memory file system I/O.
181 1.23 thorpej */
182 1.23 thorpej void
183 1.44 christos mfs_doio(struct buf *bp, void *base)
184 1.23 thorpej {
185 1.48 ad
186 1.44 christos base = (char *)base + (bp->b_blkno << DEV_BSHIFT);
187 1.23 thorpej if (bp->b_flags & B_READ)
188 1.23 thorpej bp->b_error = copyin(base, bp->b_data, bp->b_bcount);
189 1.23 thorpej else
190 1.23 thorpej bp->b_error = copyout(bp->b_data, base, bp->b_bcount);
191 1.45 ad if (bp->b_error == 0)
192 1.16 chs bp->b_resid = 0;
193 1.1 mycroft biodone(bp);
194 1.1 mycroft }
195 1.1 mycroft
196 1.1 mycroft /*
197 1.1 mycroft * This is a noop, simply returning what one has been given.
198 1.1 mycroft */
199 1.1 mycroft int
200 1.39 xtraeme mfs_bmap(void *v)
201 1.6 christos {
202 1.1 mycroft struct vop_bmap_args /* {
203 1.1 mycroft struct vnode *a_vp;
204 1.1 mycroft daddr_t a_bn;
205 1.1 mycroft struct vnode **a_vpp;
206 1.1 mycroft daddr_t *a_bnp;
207 1.1 mycroft int *a_runp;
208 1.6 christos } */ *ap = v;
209 1.1 mycroft
210 1.1 mycroft if (ap->a_vpp != NULL)
211 1.1 mycroft *ap->a_vpp = ap->a_vp;
212 1.1 mycroft if (ap->a_bnp != NULL)
213 1.1 mycroft *ap->a_bnp = ap->a_bn;
214 1.13 fvdl if (ap->a_runp != NULL)
215 1.13 fvdl *ap->a_runp = 0;
216 1.1 mycroft return (0);
217 1.1 mycroft }
218 1.1 mycroft
219 1.1 mycroft /*
220 1.1 mycroft * Memory filesystem close routine
221 1.1 mycroft */
222 1.1 mycroft /* ARGSUSED */
223 1.1 mycroft int
224 1.39 xtraeme mfs_close(void *v)
225 1.6 christos {
226 1.1 mycroft struct vop_close_args /* {
227 1.1 mycroft struct vnode *a_vp;
228 1.1 mycroft int a_fflag;
229 1.43 elad kauth_cred_t a_cred;
230 1.6 christos } */ *ap = v;
231 1.20 augustss struct vnode *vp = ap->a_vp;
232 1.20 augustss struct mfsnode *mfsp = VTOMFS(vp);
233 1.23 thorpej struct buf *bp;
234 1.1 mycroft int error;
235 1.1 mycroft
236 1.1 mycroft /*
237 1.23 thorpej * Finish any pending I/O requests.
238 1.23 thorpej */
239 1.49 ad mutex_enter(&mfs_lock);
240 1.53 yamt while ((bp = bufq_get(mfsp->mfs_buflist)) != NULL) {
241 1.49 ad mutex_exit(&mfs_lock);
242 1.23 thorpej mfs_doio(bp, mfsp->mfs_baseoff);
243 1.49 ad mutex_enter(&mfs_lock);
244 1.23 thorpej }
245 1.49 ad mutex_exit(&mfs_lock);
246 1.23 thorpej /*
247 1.1 mycroft * On last close of a memory filesystem
248 1.1 mycroft * we must invalidate any in core blocks, so that
249 1.1 mycroft * we can, free up its vnode.
250 1.1 mycroft */
251 1.46 pooka if ((error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0)) != 0)
252 1.1 mycroft return (error);
253 1.1 mycroft /*
254 1.1 mycroft * There should be no way to have any more uses of this
255 1.1 mycroft * vnode, so if we find any other uses, it is a panic.
256 1.1 mycroft */
257 1.53 yamt if (bufq_peek(mfsp->mfs_buflist) != NULL)
258 1.1 mycroft panic("mfs_close");
259 1.1 mycroft /*
260 1.1 mycroft * Send a request to the filesystem server to exit.
261 1.1 mycroft */
262 1.49 ad mutex_enter(&mfs_lock);
263 1.30 hannken mfsp->mfs_shutdown = 1;
264 1.48 ad cv_broadcast(&mfsp->mfs_cv);
265 1.49 ad mutex_exit(&mfs_lock);
266 1.1 mycroft return (0);
267 1.1 mycroft }
268 1.1 mycroft
269 1.1 mycroft /*
270 1.1 mycroft * Memory filesystem inactive routine
271 1.1 mycroft */
272 1.1 mycroft /* ARGSUSED */
273 1.1 mycroft int
274 1.39 xtraeme mfs_inactive(void *v)
275 1.6 christos {
276 1.57 riastrad struct vop_inactive_v2_args /* {
277 1.1 mycroft struct vnode *a_vp;
278 1.6 christos } */ *ap = v;
279 1.13 fvdl struct vnode *vp = ap->a_vp;
280 1.13 fvdl struct mfsnode *mfsp = VTOMFS(vp);
281 1.1 mycroft
282 1.53 yamt if (bufq_peek(mfsp->mfs_buflist) != NULL)
283 1.8 christos panic("mfs_inactive: not inactive (mfs_buflist %p)",
284 1.53 yamt bufq_peek(mfsp->mfs_buflist));
285 1.56 hannken
286 1.56 hannken return VOCALL(spec_vnodeop_p, VOFFSET(vop_inactive), ap);
287 1.1 mycroft }
288 1.1 mycroft
289 1.1 mycroft /*
290 1.1 mycroft * Reclaim a memory filesystem devvp so that it can be reused.
291 1.1 mycroft */
292 1.1 mycroft int
293 1.39 xtraeme mfs_reclaim(void *v)
294 1.6 christos {
295 1.58 riastrad struct vop_reclaim_v2_args /* {
296 1.1 mycroft struct vnode *a_vp;
297 1.6 christos } */ *ap = v;
298 1.20 augustss struct vnode *vp = ap->a_vp;
299 1.48 ad struct mfsnode *mfsp = VTOMFS(vp);
300 1.49 ad int refcnt;
301 1.48 ad
302 1.49 ad mutex_enter(&mfs_lock);
303 1.49 ad vp->v_data = NULL;
304 1.49 ad refcnt = --mfsp->mfs_refcnt;
305 1.49 ad mutex_exit(&mfs_lock);
306 1.49 ad
307 1.49 ad if (refcnt == 0) {
308 1.49 ad bufq_free(mfsp->mfs_buflist);
309 1.49 ad cv_destroy(&mfsp->mfs_cv);
310 1.49 ad kmem_free(mfsp, sizeof(*mfsp));
311 1.49 ad }
312 1.1 mycroft
313 1.56 hannken return VOCALL(spec_vnodeop_p, VOFFSET(vop_reclaim), ap);
314 1.1 mycroft }
315 1.1 mycroft
316 1.1 mycroft /*
317 1.1 mycroft * Print out the contents of an mfsnode.
318 1.1 mycroft */
319 1.1 mycroft int
320 1.39 xtraeme mfs_print(void *v)
321 1.6 christos {
322 1.1 mycroft struct vop_print_args /* {
323 1.1 mycroft struct vnode *a_vp;
324 1.6 christos } */ *ap = v;
325 1.20 augustss struct mfsnode *mfsp = VTOMFS(ap->a_vp);
326 1.1 mycroft
327 1.21 thorpej printf("tag VT_MFS, pid %d, base %p, size %ld\n",
328 1.21 thorpej (mfsp->mfs_proc != NULL) ? mfsp->mfs_proc->p_pid : 0,
329 1.8 christos mfsp->mfs_baseoff, mfsp->mfs_size);
330 1.1 mycroft return (0);
331 1.1 mycroft }
332