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