kernfs_vnops.c revision 1.55 1 1.55 mrg /* $NetBSD: kernfs_vnops.c,v 1.55 1998/02/10 14:10:32 mrg Exp $ */
2 1.27 cgd
3 1.1 cgd /*
4 1.23 mycroft * Copyright (c) 1992, 1993
5 1.23 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.17 cgd * This code is derived from software donated to Berkeley by
8 1.1 cgd * Jan-Simon Pendry.
9 1.1 cgd *
10 1.2 cgd * Redistribution and use in source and binary forms, with or without
11 1.2 cgd * modification, are permitted provided that the following conditions
12 1.2 cgd * are met:
13 1.2 cgd * 1. Redistributions of source code must retain the above copyright
14 1.2 cgd * notice, this list of conditions and the following disclaimer.
15 1.2 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 cgd * notice, this list of conditions and the following disclaimer in the
17 1.2 cgd * documentation and/or other materials provided with the distribution.
18 1.2 cgd * 3. All advertising materials mentioning features or use of this software
19 1.2 cgd * must display the following acknowledgement:
20 1.17 cgd * This product includes software developed by the University of
21 1.17 cgd * California, Berkeley and its contributors.
22 1.2 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.2 cgd * may be used to endorse or promote products derived from this software
24 1.2 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.2 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.2 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.2 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.2 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.2 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.2 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.2 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.2 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.2 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.2 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.2 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.27 cgd * @(#)kernfs_vnops.c 8.9 (Berkeley) 6/15/94
39 1.1 cgd */
40 1.1 cgd
41 1.1 cgd /*
42 1.23 mycroft * Kernel parameter filesystem (/kern)
43 1.1 cgd */
44 1.55 mrg
45 1.55 mrg #include "opt_uvm.h"
46 1.1 cgd
47 1.14 mycroft #include <sys/param.h>
48 1.14 mycroft #include <sys/systm.h>
49 1.14 mycroft #include <sys/kernel.h>
50 1.23 mycroft #include <sys/vmmeter.h>
51 1.14 mycroft #include <sys/types.h>
52 1.14 mycroft #include <sys/time.h>
53 1.14 mycroft #include <sys/proc.h>
54 1.23 mycroft #include <sys/vnode.h>
55 1.23 mycroft #include <sys/malloc.h>
56 1.14 mycroft #include <sys/file.h>
57 1.14 mycroft #include <sys/stat.h>
58 1.14 mycroft #include <sys/mount.h>
59 1.14 mycroft #include <sys/namei.h>
60 1.14 mycroft #include <sys/buf.h>
61 1.23 mycroft #include <sys/dirent.h>
62 1.28 mycroft #include <sys/msgbuf.h>
63 1.44 mycroft
64 1.44 mycroft #include <miscfs/genfs/genfs.h>
65 1.17 cgd #include <miscfs/kernfs/kernfs.h>
66 1.1 cgd
67 1.54 mrg #if defined(UVM)
68 1.54 mrg #include <vm/vm.h>
69 1.54 mrg #include <uvm/uvm_extern.h>
70 1.54 mrg #endif
71 1.54 mrg
72 1.17 cgd #define KSTRING 256 /* Largest I/O available via this filesystem */
73 1.17 cgd #define UIO_MX 32
74 1.1 cgd
75 1.23 mycroft #define READ_MODE (S_IRUSR|S_IRGRP|S_IROTH)
76 1.23 mycroft #define WRITE_MODE (S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)
77 1.23 mycroft #define DIR_MODE (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
78 1.23 mycroft
79 1.50 pk struct kern_target kern_targets[] = {
80 1.1 cgd /* NOTE: The name must be less than UIO_MX-16 chars in length */
81 1.23 mycroft #define N(s) sizeof(s)-1, s
82 1.23 mycroft /* name data tag type ro/rw */
83 1.23 mycroft { DT_DIR, N("."), 0, KTT_NULL, VDIR, DIR_MODE },
84 1.23 mycroft { DT_DIR, N(".."), 0, KTT_NULL, VDIR, DIR_MODE },
85 1.23 mycroft { DT_REG, N("boottime"), &boottime.tv_sec, KTT_INT, VREG, READ_MODE },
86 1.23 mycroft { DT_REG, N("copyright"), copyright, KTT_STRING, VREG, READ_MODE },
87 1.23 mycroft { DT_REG, N("hostname"), 0, KTT_HOSTNAME, VREG, WRITE_MODE },
88 1.23 mycroft { DT_REG, N("hz"), &hz, KTT_INT, VREG, READ_MODE },
89 1.23 mycroft { DT_REG, N("loadavg"), 0, KTT_AVENRUN, VREG, READ_MODE },
90 1.28 mycroft { DT_REG, N("msgbuf"), 0, KTT_MSGBUF, VREG, READ_MODE },
91 1.54 mrg #if defined(UVM)
92 1.54 mrg { DT_REG, N("pagesize"), &uvmexp.pagesize, KTT_INT, VREG, READ_MODE },
93 1.54 mrg #else
94 1.23 mycroft { DT_REG, N("pagesize"), &cnt.v_page_size, KTT_INT, VREG, READ_MODE },
95 1.54 mrg #endif
96 1.23 mycroft { DT_REG, N("physmem"), &physmem, KTT_INT, VREG, READ_MODE },
97 1.17 cgd #if 0
98 1.23 mycroft { DT_DIR, N("root"), 0, KTT_NULL, VDIR, DIR_MODE },
99 1.17 cgd #endif
100 1.23 mycroft { DT_BLK, N("rootdev"), &rootdev, KTT_DEVICE, VBLK, READ_MODE },
101 1.23 mycroft { DT_CHR, N("rrootdev"), &rrootdev, KTT_DEVICE, VCHR, READ_MODE },
102 1.23 mycroft { DT_REG, N("time"), 0, KTT_TIME, VREG, READ_MODE },
103 1.23 mycroft { DT_REG, N("version"), version, KTT_STRING, VREG, READ_MODE },
104 1.23 mycroft #undef N
105 1.1 cgd };
106 1.17 cgd static int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
107 1.1 cgd
108 1.41 christos int kernfs_lookup __P((void *));
109 1.44 mycroft #define kernfs_create genfs_eopnotsupp
110 1.44 mycroft #define kernfs_mknod genfs_eopnotsupp
111 1.44 mycroft #define kernfs_open genfs_nullop
112 1.44 mycroft #define kernfs_close genfs_nullop
113 1.41 christos int kernfs_access __P((void *));
114 1.41 christos int kernfs_getattr __P((void *));
115 1.41 christos int kernfs_setattr __P((void *));
116 1.41 christos int kernfs_read __P((void *));
117 1.41 christos int kernfs_write __P((void *));
118 1.44 mycroft #define kernfs_ioctl genfs_eopnotsupp
119 1.45 mycroft #define kernfs_poll genfs_poll
120 1.44 mycroft #define kernfs_mmap genfs_eopnotsupp
121 1.44 mycroft #define kernfs_fsync genfs_nullop
122 1.44 mycroft #define kernfs_seek genfs_nullop
123 1.44 mycroft #define kernfs_remove genfs_eopnotsupp
124 1.41 christos int kernfs_link __P((void *));
125 1.44 mycroft #define kernfs_rename genfs_eopnotsupp
126 1.44 mycroft #define kernfs_mkdir genfs_eopnotsupp
127 1.44 mycroft #define kernfs_rmdir genfs_eopnotsupp
128 1.41 christos int kernfs_symlink __P((void *));
129 1.41 christos int kernfs_readdir __P((void *));
130 1.44 mycroft #define kernfs_readlink genfs_eopnotsupp
131 1.44 mycroft #define kernfs_abortop genfs_abortop
132 1.41 christos int kernfs_inactive __P((void *));
133 1.41 christos int kernfs_reclaim __P((void *));
134 1.44 mycroft #define kernfs_lock genfs_nullop
135 1.44 mycroft #define kernfs_unlock genfs_nullop
136 1.44 mycroft #define kernfs_bmap genfs_badop
137 1.44 mycroft #define kernfs_strategy genfs_badop
138 1.41 christos int kernfs_print __P((void *));
139 1.44 mycroft #define kernfs_islocked genfs_nullop
140 1.41 christos int kernfs_pathconf __P((void *));
141 1.44 mycroft #define kernfs_advlock genfs_eopnotsupp
142 1.44 mycroft #define kernfs_blkatoff genfs_eopnotsupp
143 1.44 mycroft #define kernfs_valloc genfs_eopnotsupp
144 1.44 mycroft #define kernfs_vfree genfs_nullop
145 1.44 mycroft #define kernfs_truncate genfs_eopnotsupp
146 1.44 mycroft #define kernfs_update genfs_nullop
147 1.44 mycroft #define kernfs_bwrite genfs_eopnotsupp
148 1.41 christos
149 1.41 christos int kernfs_xread __P((struct kern_target *, int, char **, int));
150 1.41 christos int kernfs_xwrite __P((struct kern_target *, char *, int));
151 1.41 christos
152 1.41 christos int (**kernfs_vnodeop_p) __P((void *));
153 1.41 christos struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
154 1.41 christos { &vop_default_desc, vn_default_error },
155 1.44 mycroft { &vop_lookup_desc, kernfs_lookup }, /* lookup */
156 1.44 mycroft { &vop_create_desc, kernfs_create }, /* create */
157 1.44 mycroft { &vop_mknod_desc, kernfs_mknod }, /* mknod */
158 1.44 mycroft { &vop_open_desc, kernfs_open }, /* open */
159 1.44 mycroft { &vop_close_desc, kernfs_close }, /* close */
160 1.44 mycroft { &vop_access_desc, kernfs_access }, /* access */
161 1.44 mycroft { &vop_getattr_desc, kernfs_getattr }, /* getattr */
162 1.44 mycroft { &vop_setattr_desc, kernfs_setattr }, /* setattr */
163 1.44 mycroft { &vop_read_desc, kernfs_read }, /* read */
164 1.44 mycroft { &vop_write_desc, kernfs_write }, /* write */
165 1.44 mycroft { &vop_ioctl_desc, kernfs_ioctl }, /* ioctl */
166 1.45 mycroft { &vop_poll_desc, kernfs_poll }, /* poll */
167 1.44 mycroft { &vop_mmap_desc, kernfs_mmap }, /* mmap */
168 1.44 mycroft { &vop_fsync_desc, kernfs_fsync }, /* fsync */
169 1.44 mycroft { &vop_seek_desc, kernfs_seek }, /* seek */
170 1.44 mycroft { &vop_remove_desc, kernfs_remove }, /* remove */
171 1.44 mycroft { &vop_link_desc, kernfs_link }, /* link */
172 1.44 mycroft { &vop_rename_desc, kernfs_rename }, /* rename */
173 1.44 mycroft { &vop_mkdir_desc, kernfs_mkdir }, /* mkdir */
174 1.44 mycroft { &vop_rmdir_desc, kernfs_rmdir }, /* rmdir */
175 1.44 mycroft { &vop_symlink_desc, kernfs_symlink }, /* symlink */
176 1.44 mycroft { &vop_readdir_desc, kernfs_readdir }, /* readdir */
177 1.44 mycroft { &vop_readlink_desc, kernfs_readlink }, /* readlink */
178 1.44 mycroft { &vop_abortop_desc, kernfs_abortop }, /* abortop */
179 1.44 mycroft { &vop_inactive_desc, kernfs_inactive }, /* inactive */
180 1.44 mycroft { &vop_reclaim_desc, kernfs_reclaim }, /* reclaim */
181 1.44 mycroft { &vop_lock_desc, kernfs_lock }, /* lock */
182 1.44 mycroft { &vop_unlock_desc, kernfs_unlock }, /* unlock */
183 1.44 mycroft { &vop_bmap_desc, kernfs_bmap }, /* bmap */
184 1.44 mycroft { &vop_strategy_desc, kernfs_strategy }, /* strategy */
185 1.44 mycroft { &vop_print_desc, kernfs_print }, /* print */
186 1.44 mycroft { &vop_islocked_desc, kernfs_islocked }, /* islocked */
187 1.44 mycroft { &vop_pathconf_desc, kernfs_pathconf }, /* pathconf */
188 1.44 mycroft { &vop_advlock_desc, kernfs_advlock }, /* advlock */
189 1.44 mycroft { &vop_blkatoff_desc, kernfs_blkatoff }, /* blkatoff */
190 1.44 mycroft { &vop_valloc_desc, kernfs_valloc }, /* valloc */
191 1.44 mycroft { &vop_vfree_desc, kernfs_vfree }, /* vfree */
192 1.44 mycroft { &vop_truncate_desc, kernfs_truncate }, /* truncate */
193 1.44 mycroft { &vop_update_desc, kernfs_update }, /* update */
194 1.44 mycroft { &vop_bwrite_desc, kernfs_bwrite }, /* bwrite */
195 1.41 christos { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
196 1.41 christos };
197 1.41 christos struct vnodeopv_desc kernfs_vnodeop_opv_desc =
198 1.41 christos { &kernfs_vnodeop_p, kernfs_vnodeop_entries };
199 1.41 christos
200 1.28 mycroft int
201 1.28 mycroft kernfs_xread(kt, off, bufp, len)
202 1.17 cgd struct kern_target *kt;
203 1.28 mycroft int off;
204 1.28 mycroft char **bufp;
205 1.1 cgd int len;
206 1.1 cgd {
207 1.1 cgd
208 1.1 cgd switch (kt->kt_tag) {
209 1.1 cgd case KTT_TIME: {
210 1.1 cgd struct timeval tv;
211 1.28 mycroft
212 1.1 cgd microtime(&tv);
213 1.47 christos sprintf(*bufp, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
214 1.1 cgd break;
215 1.1 cgd }
216 1.1 cgd
217 1.1 cgd case KTT_INT: {
218 1.1 cgd int *ip = kt->kt_data;
219 1.28 mycroft
220 1.47 christos sprintf(*bufp, "%d\n", *ip);
221 1.1 cgd break;
222 1.1 cgd }
223 1.1 cgd
224 1.1 cgd case KTT_STRING: {
225 1.1 cgd char *cp = kt->kt_data;
226 1.1 cgd
227 1.28 mycroft *bufp = cp;
228 1.28 mycroft break;
229 1.28 mycroft }
230 1.1 cgd
231 1.28 mycroft case KTT_MSGBUF: {
232 1.28 mycroft long n;
233 1.28 mycroft
234 1.52 leo /*
235 1.52 leo * deal with cases where the message buffer has
236 1.52 leo * become corrupted.
237 1.52 leo */
238 1.52 leo if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
239 1.52 leo msgbufenabled = 0;
240 1.52 leo return (ENXIO);
241 1.52 leo }
242 1.52 leo
243 1.52 leo /*
244 1.52 leo * Note that reads of /kern/msgbuf won't necessarily yield
245 1.52 leo * consistent results, if the message buffer is modified
246 1.52 leo * while the read is in progress. The worst that can happen
247 1.52 leo * is that incorrect data will be read. There's no way
248 1.52 leo * that this can crash the system unless the values in the
249 1.52 leo * message buffer header are corrupted, but that'll cause
250 1.52 leo * the system to die anyway.
251 1.52 leo */
252 1.52 leo if (off >= msgbufp->msg_bufs)
253 1.28 mycroft return (0);
254 1.28 mycroft n = msgbufp->msg_bufx + off;
255 1.52 leo if (n >= msgbufp->msg_bufs)
256 1.52 leo n -= msgbufp->msg_bufs;
257 1.52 leo len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
258 1.28 mycroft *bufp = msgbufp->msg_bufc + n;
259 1.28 mycroft return (len);
260 1.1 cgd }
261 1.1 cgd
262 1.1 cgd case KTT_HOSTNAME: {
263 1.1 cgd char *cp = hostname;
264 1.1 cgd int xlen = hostnamelen;
265 1.1 cgd
266 1.17 cgd if (xlen >= (len-2))
267 1.1 cgd return (EINVAL);
268 1.1 cgd
269 1.28 mycroft bcopy(cp, *bufp, xlen);
270 1.28 mycroft (*bufp)[xlen] = '\n';
271 1.28 mycroft (*bufp)[xlen+1] = '\0';
272 1.1 cgd break;
273 1.1 cgd }
274 1.1 cgd
275 1.1 cgd case KTT_AVENRUN:
276 1.31 mycroft averunnable.fscale = FSCALE;
277 1.47 christos sprintf(*bufp, "%d %d %d %ld\n",
278 1.23 mycroft averunnable.ldavg[0], averunnable.ldavg[1],
279 1.23 mycroft averunnable.ldavg[2], averunnable.fscale);
280 1.1 cgd break;
281 1.1 cgd
282 1.1 cgd default:
283 1.28 mycroft return (0);
284 1.1 cgd }
285 1.1 cgd
286 1.28 mycroft len = strlen(*bufp);
287 1.28 mycroft if (len <= off)
288 1.28 mycroft return (0);
289 1.28 mycroft *bufp += off;
290 1.28 mycroft return (len - off);
291 1.1 cgd }
292 1.1 cgd
293 1.28 mycroft int
294 1.1 cgd kernfs_xwrite(kt, buf, len)
295 1.17 cgd struct kern_target *kt;
296 1.1 cgd char *buf;
297 1.1 cgd int len;
298 1.1 cgd {
299 1.23 mycroft
300 1.1 cgd switch (kt->kt_tag) {
301 1.23 mycroft case KTT_HOSTNAME:
302 1.1 cgd if (buf[len-1] == '\n')
303 1.1 cgd --len;
304 1.1 cgd bcopy(buf, hostname, len);
305 1.17 cgd hostname[len] = '\0';
306 1.6 cgd hostnamelen = len;
307 1.1 cgd return (0);
308 1.1 cgd
309 1.1 cgd default:
310 1.1 cgd return (EIO);
311 1.1 cgd }
312 1.1 cgd }
313 1.1 cgd
314 1.17 cgd
315 1.17 cgd /*
316 1.1 cgd * vp is the current namei directory
317 1.1 cgd * ndp is the name to locate in that directory...
318 1.1 cgd */
319 1.41 christos int
320 1.41 christos kernfs_lookup(v)
321 1.41 christos void *v;
322 1.41 christos {
323 1.23 mycroft struct vop_lookup_args /* {
324 1.23 mycroft struct vnode * a_dvp;
325 1.23 mycroft struct vnode ** a_vpp;
326 1.23 mycroft struct componentname * a_cnp;
327 1.41 christos } */ *ap = v;
328 1.23 mycroft struct componentname *cnp = ap->a_cnp;
329 1.23 mycroft struct vnode **vpp = ap->a_vpp;
330 1.23 mycroft struct vnode *dvp = ap->a_dvp;
331 1.48 cgd const char *pname = cnp->cn_nameptr;
332 1.23 mycroft struct kern_target *kt;
333 1.1 cgd struct vnode *fvp;
334 1.23 mycroft int error, i;
335 1.1 cgd
336 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
337 1.51 christos printf("kernfs_lookup(%p)\n", ap);
338 1.51 christos printf("kernfs_lookup(dp = %p, vpp = %p, cnp = %p)\n", dvp, vpp, ap->a_cnp);
339 1.47 christos printf("kernfs_lookup(%s)\n", pname);
340 1.1 cgd #endif
341 1.23 mycroft
342 1.35 mycroft *vpp = NULLVP;
343 1.35 mycroft
344 1.35 mycroft if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
345 1.35 mycroft return (EROFS);
346 1.35 mycroft
347 1.23 mycroft if (cnp->cn_namelen == 1 && *pname == '.') {
348 1.23 mycroft *vpp = dvp;
349 1.1 cgd VREF(dvp);
350 1.1 cgd /*VOP_LOCK(dvp);*/
351 1.1 cgd return (0);
352 1.1 cgd }
353 1.13 cgd
354 1.17 cgd #if 0
355 1.23 mycroft if (cnp->cn_namelen == 4 && bcmp(pname, "root", 4) == 0) {
356 1.23 mycroft *vpp = rootdir;
357 1.17 cgd VREF(rootdir);
358 1.1 cgd VOP_LOCK(rootdir);
359 1.1 cgd return (0);
360 1.1 cgd }
361 1.13 cgd #endif
362 1.25 mycroft
363 1.35 mycroft for (kt = kern_targets, i = 0; i < nkern_targets; kt++, i++) {
364 1.26 mycroft if (cnp->cn_namelen == kt->kt_namlen &&
365 1.35 mycroft bcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
366 1.35 mycroft goto found;
367 1.1 cgd }
368 1.1 cgd
369 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
370 1.47 christos printf("kernfs_lookup: i = %d, failed", i);
371 1.1 cgd #endif
372 1.1 cgd
373 1.35 mycroft return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
374 1.23 mycroft
375 1.35 mycroft found:
376 1.23 mycroft if (kt->kt_tag == KTT_DEVICE) {
377 1.23 mycroft dev_t *dp = kt->kt_data;
378 1.24 mycroft loop:
379 1.23 mycroft if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
380 1.24 mycroft return (ENOENT);
381 1.23 mycroft *vpp = fvp;
382 1.24 mycroft if (vget(fvp, 1))
383 1.24 mycroft goto loop;
384 1.23 mycroft return (0);
385 1.23 mycroft }
386 1.1 cgd
387 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
388 1.47 christos printf("kernfs_lookup: allocate new vnode\n");
389 1.1 cgd #endif
390 1.41 christos error = getnewvnode(VT_KERNFS, dvp->v_mount, kernfs_vnodeop_p, &fvp);
391 1.41 christos if (error)
392 1.23 mycroft return (error);
393 1.23 mycroft
394 1.23 mycroft MALLOC(fvp->v_data, void *, sizeof(struct kernfs_node), M_TEMP,
395 1.23 mycroft M_WAITOK);
396 1.23 mycroft VTOKERN(fvp)->kf_kt = kt;
397 1.23 mycroft fvp->v_type = kt->kt_vtype;
398 1.23 mycroft *vpp = fvp;
399 1.23 mycroft
400 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
401 1.51 christos printf("kernfs_lookup: newvp = %p\n", fvp);
402 1.1 cgd #endif
403 1.1 cgd return (0);
404 1.1 cgd }
405 1.1 cgd
406 1.28 mycroft int
407 1.41 christos kernfs_access(v)
408 1.41 christos void *v;
409 1.41 christos {
410 1.23 mycroft struct vop_access_args /* {
411 1.23 mycroft struct vnode *a_vp;
412 1.34 mycroft int a_mode;
413 1.23 mycroft struct ucred *a_cred;
414 1.23 mycroft struct proc *a_p;
415 1.41 christos } */ *ap = v;
416 1.26 mycroft struct vnode *vp = ap->a_vp;
417 1.49 mycroft mode_t mode;
418 1.17 cgd
419 1.49 mycroft if (vp->v_flag & VROOT) {
420 1.49 mycroft mode = DIR_MODE;
421 1.49 mycroft } else {
422 1.49 mycroft struct kern_target *kt = VTOKERN(vp)->kf_kt;
423 1.49 mycroft mode = kt->kt_mode;
424 1.49 mycroft }
425 1.49 mycroft
426 1.49 mycroft return (vaccess(vp->v_type, mode, (uid_t)0, (gid_t)0, ap->a_mode,
427 1.49 mycroft ap->a_cred));
428 1.23 mycroft }
429 1.23 mycroft
430 1.41 christos int
431 1.41 christos kernfs_getattr(v)
432 1.41 christos void *v;
433 1.41 christos {
434 1.23 mycroft struct vop_getattr_args /* {
435 1.23 mycroft struct vnode *a_vp;
436 1.23 mycroft struct vattr *a_vap;
437 1.23 mycroft struct ucred *a_cred;
438 1.23 mycroft struct proc *a_p;
439 1.41 christos } */ *ap = v;
440 1.23 mycroft struct vnode *vp = ap->a_vp;
441 1.23 mycroft struct vattr *vap = ap->a_vap;
442 1.36 cgd struct timeval tv;
443 1.1 cgd int error = 0;
444 1.28 mycroft char strbuf[KSTRING], *buf;
445 1.1 cgd
446 1.1 cgd bzero((caddr_t) vap, sizeof(*vap));
447 1.1 cgd vattr_null(vap);
448 1.17 cgd vap->va_uid = 0;
449 1.17 cgd vap->va_gid = 0;
450 1.1 cgd vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
451 1.23 mycroft vap->va_size = 0;
452 1.1 cgd vap->va_blocksize = DEV_BSIZE;
453 1.36 cgd microtime(&tv);
454 1.36 cgd TIMEVAL_TO_TIMESPEC(&tv, &vap->va_atime);
455 1.1 cgd vap->va_mtime = vap->va_atime;
456 1.1 cgd vap->va_ctime = vap->va_ctime;
457 1.1 cgd vap->va_gen = 0;
458 1.1 cgd vap->va_flags = 0;
459 1.1 cgd vap->va_rdev = 0;
460 1.1 cgd vap->va_bytes = 0;
461 1.1 cgd
462 1.1 cgd if (vp->v_flag & VROOT) {
463 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
464 1.47 christos printf("kernfs_getattr: stat rootdir\n");
465 1.1 cgd #endif
466 1.17 cgd vap->va_type = VDIR;
467 1.23 mycroft vap->va_mode = DIR_MODE;
468 1.1 cgd vap->va_nlink = 2;
469 1.1 cgd vap->va_fileid = 2;
470 1.1 cgd vap->va_size = DEV_BSIZE;
471 1.1 cgd } else {
472 1.23 mycroft struct kern_target *kt = VTOKERN(vp)->kf_kt;
473 1.28 mycroft int nbytes, total;
474 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
475 1.47 christos printf("kernfs_getattr: stat target %s\n", kt->kt_name);
476 1.1 cgd #endif
477 1.17 cgd vap->va_type = kt->kt_vtype;
478 1.23 mycroft vap->va_mode = kt->kt_mode;
479 1.1 cgd vap->va_nlink = 1;
480 1.39 mycroft vap->va_fileid = 3 + (kt - kern_targets);
481 1.28 mycroft total = 0;
482 1.28 mycroft while (buf = strbuf,
483 1.28 mycroft nbytes = kernfs_xread(kt, total, &buf, sizeof(strbuf)))
484 1.28 mycroft total += nbytes;
485 1.28 mycroft vap->va_size = total;
486 1.1 cgd }
487 1.1 cgd
488 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
489 1.47 christos printf("kernfs_getattr: return error %d\n", error);
490 1.1 cgd #endif
491 1.1 cgd return (error);
492 1.1 cgd }
493 1.1 cgd
494 1.41 christos /*ARGSUSED*/
495 1.41 christos int
496 1.41 christos kernfs_setattr(v)
497 1.41 christos void *v;
498 1.1 cgd {
499 1.1 cgd /*
500 1.17 cgd * Silently ignore attribute changes.
501 1.17 cgd * This allows for open with truncate to have no
502 1.17 cgd * effect until some data is written. I want to
503 1.17 cgd * do it this way because all writes are atomic.
504 1.1 cgd */
505 1.17 cgd return (0);
506 1.1 cgd }
507 1.1 cgd
508 1.28 mycroft int
509 1.41 christos kernfs_read(v)
510 1.41 christos void *v;
511 1.41 christos {
512 1.23 mycroft struct vop_read_args /* {
513 1.23 mycroft struct vnode *a_vp;
514 1.23 mycroft struct uio *a_uio;
515 1.23 mycroft int a_ioflag;
516 1.23 mycroft struct ucred *a_cred;
517 1.41 christos } */ *ap = v;
518 1.23 mycroft struct vnode *vp = ap->a_vp;
519 1.23 mycroft struct uio *uio = ap->a_uio;
520 1.23 mycroft struct kern_target *kt;
521 1.28 mycroft char strbuf[KSTRING], *buf;
522 1.28 mycroft int off, len;
523 1.28 mycroft int error;
524 1.23 mycroft
525 1.23 mycroft if (vp->v_type == VDIR)
526 1.23 mycroft return (EOPNOTSUPP);
527 1.23 mycroft
528 1.23 mycroft kt = VTOKERN(vp)->kf_kt;
529 1.23 mycroft
530 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
531 1.47 christos printf("kern_read %s\n", kt->kt_name);
532 1.1 cgd #endif
533 1.18 cgd
534 1.28 mycroft off = uio->uio_offset;
535 1.28 mycroft #if 0
536 1.28 mycroft while (buf = strbuf,
537 1.28 mycroft #else
538 1.28 mycroft if (buf = strbuf,
539 1.28 mycroft #endif
540 1.28 mycroft len = kernfs_xread(kt, off, &buf, sizeof(strbuf))) {
541 1.41 christos if ((error = uiomove(buf, len, uio)) != 0)
542 1.28 mycroft return (error);
543 1.28 mycroft off += len;
544 1.28 mycroft }
545 1.28 mycroft return (0);
546 1.1 cgd }
547 1.1 cgd
548 1.28 mycroft int
549 1.41 christos kernfs_write(v)
550 1.41 christos void *v;
551 1.41 christos {
552 1.23 mycroft struct vop_write_args /* {
553 1.23 mycroft struct vnode *a_vp;
554 1.23 mycroft struct uio *a_uio;
555 1.23 mycroft int a_ioflag;
556 1.23 mycroft struct ucred *a_cred;
557 1.41 christos } */ *ap = v;
558 1.23 mycroft struct vnode *vp = ap->a_vp;
559 1.23 mycroft struct uio *uio = ap->a_uio;
560 1.23 mycroft struct kern_target *kt;
561 1.23 mycroft int error, xlen;
562 1.1 cgd char strbuf[KSTRING];
563 1.23 mycroft
564 1.23 mycroft if (vp->v_type == VDIR)
565 1.23 mycroft return (EOPNOTSUPP);
566 1.23 mycroft
567 1.23 mycroft kt = VTOKERN(vp)->kf_kt;
568 1.1 cgd
569 1.1 cgd if (uio->uio_offset != 0)
570 1.1 cgd return (EINVAL);
571 1.1 cgd
572 1.1 cgd xlen = min(uio->uio_resid, KSTRING-1);
573 1.41 christos if ((error = uiomove(strbuf, xlen, uio)) != 0)
574 1.1 cgd return (error);
575 1.1 cgd
576 1.1 cgd if (uio->uio_resid != 0)
577 1.1 cgd return (EIO);
578 1.1 cgd
579 1.1 cgd strbuf[xlen] = '\0';
580 1.17 cgd xlen = strlen(strbuf);
581 1.1 cgd return (kernfs_xwrite(kt, strbuf, xlen));
582 1.1 cgd }
583 1.1 cgd
584 1.41 christos int
585 1.41 christos kernfs_readdir(v)
586 1.41 christos void *v;
587 1.41 christos {
588 1.23 mycroft struct vop_readdir_args /* {
589 1.23 mycroft struct vnode *a_vp;
590 1.23 mycroft struct uio *a_uio;
591 1.23 mycroft struct ucred *a_cred;
592 1.26 mycroft int *a_eofflag;
593 1.53 fvdl off_t *a_cookies;
594 1.26 mycroft int a_ncookies;
595 1.41 christos } */ *ap = v;
596 1.23 mycroft struct uio *uio = ap->a_uio;
597 1.37 mycroft struct dirent d;
598 1.26 mycroft struct kern_target *kt;
599 1.1 cgd int i;
600 1.1 cgd int error;
601 1.53 fvdl off_t *cookies = ap->a_cookies;
602 1.37 mycroft int ncookies = ap->a_ncookies;
603 1.1 cgd
604 1.23 mycroft if (ap->a_vp->v_type != VDIR)
605 1.23 mycroft return (ENOTDIR);
606 1.23 mycroft
607 1.37 mycroft if (uio->uio_resid < UIO_MX)
608 1.37 mycroft return (EINVAL);
609 1.38 mycroft if (uio->uio_offset < 0)
610 1.37 mycroft return (EINVAL);
611 1.26 mycroft
612 1.1 cgd error = 0;
613 1.38 mycroft i = uio->uio_offset;
614 1.37 mycroft bzero((caddr_t)&d, UIO_MX);
615 1.37 mycroft d.d_reclen = UIO_MX;
616 1.37 mycroft
617 1.23 mycroft for (kt = &kern_targets[i];
618 1.23 mycroft uio->uio_resid >= UIO_MX && i < nkern_targets; kt++, i++) {
619 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
620 1.47 christos printf("kernfs_readdir: i = %d\n", i);
621 1.1 cgd #endif
622 1.26 mycroft
623 1.23 mycroft if (kt->kt_tag == KTT_DEVICE) {
624 1.26 mycroft dev_t *dp = kt->kt_data;
625 1.23 mycroft struct vnode *fvp;
626 1.26 mycroft
627 1.23 mycroft if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
628 1.23 mycroft continue;
629 1.23 mycroft }
630 1.26 mycroft
631 1.37 mycroft d.d_fileno = i + 3;
632 1.37 mycroft d.d_namlen = kt->kt_namlen;
633 1.37 mycroft bcopy(kt->kt_name, d.d_name, kt->kt_namlen + 1);
634 1.37 mycroft d.d_type = kt->kt_type;
635 1.26 mycroft
636 1.41 christos if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
637 1.1 cgd break;
638 1.37 mycroft if (ncookies-- > 0)
639 1.38 mycroft *cookies++ = i + 1;
640 1.1 cgd }
641 1.1 cgd
642 1.38 mycroft uio->uio_offset = i;
643 1.1 cgd return (error);
644 1.1 cgd }
645 1.1 cgd
646 1.41 christos int
647 1.41 christos kernfs_inactive(v)
648 1.41 christos void *v;
649 1.41 christos {
650 1.23 mycroft struct vop_inactive_args /* {
651 1.23 mycroft struct vnode *a_vp;
652 1.41 christos } */ *ap = v;
653 1.23 mycroft struct vnode *vp = ap->a_vp;
654 1.23 mycroft
655 1.23 mycroft #ifdef KERNFS_DIAGNOSTIC
656 1.51 christos printf("kernfs_inactive(%p)\n", vp);
657 1.23 mycroft #endif
658 1.1 cgd /*
659 1.1 cgd * Clear out the v_type field to avoid
660 1.1 cgd * nasty things happening in vgone().
661 1.1 cgd */
662 1.1 cgd vp->v_type = VNON;
663 1.23 mycroft return (0);
664 1.23 mycroft }
665 1.23 mycroft
666 1.41 christos int
667 1.41 christos kernfs_reclaim(v)
668 1.41 christos void *v;
669 1.41 christos {
670 1.23 mycroft struct vop_reclaim_args /* {
671 1.23 mycroft struct vnode *a_vp;
672 1.41 christos } */ *ap = v;
673 1.23 mycroft struct vnode *vp = ap->a_vp;
674 1.23 mycroft
675 1.1 cgd #ifdef KERNFS_DIAGNOSTIC
676 1.51 christos printf("kernfs_reclaim(%p)\n", vp);
677 1.1 cgd #endif
678 1.23 mycroft if (vp->v_data) {
679 1.23 mycroft FREE(vp->v_data, M_TEMP);
680 1.23 mycroft vp->v_data = 0;
681 1.23 mycroft }
682 1.1 cgd return (0);
683 1.1 cgd }
684 1.1 cgd
685 1.1 cgd /*
686 1.23 mycroft * Return POSIX pathconf information applicable to special devices.
687 1.23 mycroft */
688 1.41 christos int
689 1.41 christos kernfs_pathconf(v)
690 1.41 christos void *v;
691 1.41 christos {
692 1.23 mycroft struct vop_pathconf_args /* {
693 1.23 mycroft struct vnode *a_vp;
694 1.23 mycroft int a_name;
695 1.29 cgd register_t *a_retval;
696 1.41 christos } */ *ap = v;
697 1.23 mycroft
698 1.23 mycroft switch (ap->a_name) {
699 1.23 mycroft case _PC_LINK_MAX:
700 1.23 mycroft *ap->a_retval = LINK_MAX;
701 1.23 mycroft return (0);
702 1.23 mycroft case _PC_MAX_CANON:
703 1.23 mycroft *ap->a_retval = MAX_CANON;
704 1.23 mycroft return (0);
705 1.23 mycroft case _PC_MAX_INPUT:
706 1.23 mycroft *ap->a_retval = MAX_INPUT;
707 1.23 mycroft return (0);
708 1.23 mycroft case _PC_PIPE_BUF:
709 1.23 mycroft *ap->a_retval = PIPE_BUF;
710 1.23 mycroft return (0);
711 1.23 mycroft case _PC_CHOWN_RESTRICTED:
712 1.23 mycroft *ap->a_retval = 1;
713 1.23 mycroft return (0);
714 1.23 mycroft case _PC_VDISABLE:
715 1.23 mycroft *ap->a_retval = _POSIX_VDISABLE;
716 1.23 mycroft return (0);
717 1.23 mycroft default:
718 1.23 mycroft return (EINVAL);
719 1.23 mycroft }
720 1.23 mycroft /* NOTREACHED */
721 1.23 mycroft }
722 1.23 mycroft
723 1.23 mycroft /*
724 1.23 mycroft * Print out the contents of a /dev/fd vnode.
725 1.1 cgd */
726 1.1 cgd /* ARGSUSED */
727 1.41 christos int
728 1.41 christos kernfs_print(v)
729 1.41 christos void *v;
730 1.23 mycroft {
731 1.23 mycroft
732 1.47 christos printf("tag VT_KERNFS, kernfs vnode\n");
733 1.23 mycroft return (0);
734 1.23 mycroft }
735 1.23 mycroft
736 1.40 mycroft int
737 1.41 christos kernfs_link(v)
738 1.41 christos void *v;
739 1.41 christos {
740 1.40 mycroft struct vop_link_args /* {
741 1.40 mycroft struct vnode *a_dvp;
742 1.40 mycroft struct vnode *a_vp;
743 1.40 mycroft struct componentname *a_cnp;
744 1.41 christos } */ *ap = v;
745 1.40 mycroft
746 1.40 mycroft VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
747 1.40 mycroft vput(ap->a_dvp);
748 1.40 mycroft return (EROFS);
749 1.40 mycroft }
750 1.40 mycroft
751 1.40 mycroft int
752 1.41 christos kernfs_symlink(v)
753 1.41 christos void *v;
754 1.41 christos {
755 1.40 mycroft struct vop_symlink_args /* {
756 1.40 mycroft struct vnode *a_dvp;
757 1.40 mycroft struct vnode **a_vpp;
758 1.40 mycroft struct componentname *a_cnp;
759 1.40 mycroft struct vattr *a_vap;
760 1.40 mycroft char *a_target;
761 1.41 christos } */ *ap = v;
762 1.40 mycroft
763 1.40 mycroft VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
764 1.40 mycroft vput(ap->a_dvp);
765 1.40 mycroft return (EROFS);
766 1.1 cgd }
767