kernfs_vnops.c revision 1.92 1 /* $NetBSD: kernfs_vnops.c,v 1.92 2003/09/10 00:45:22 dan Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software donated to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95
35 */
36
37 /*
38 * Kernel parameter filesystem (/kern)
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.92 2003/09/10 00:45:22 dan Exp $");
43
44 #ifdef _KERNEL_OPT
45 #include "opt_ipsec.h"
46 #endif
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/vmmeter.h>
52 #include <sys/time.h>
53 #include <sys/proc.h>
54 #include <sys/vnode.h>
55 #include <sys/malloc.h>
56 #include <sys/file.h>
57 #include <sys/stat.h>
58 #include <sys/mount.h>
59 #include <sys/namei.h>
60 #include <sys/buf.h>
61 #include <sys/dirent.h>
62 #include <sys/msgbuf.h>
63
64 #include <miscfs/genfs/genfs.h>
65 #include <miscfs/kernfs/kernfs.h>
66
67 #ifdef IPSEC
68 #include <sys/mbuf.h>
69 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet6/ipsec.h>
72 #include <netkey/key.h>
73 #endif
74
75 #include <uvm/uvm_extern.h>
76
77 #define KSTRING 256 /* Largest I/O available via this filesystem */
78 #define UIO_MX 32
79
80 #define READ_MODE (S_IRUSR|S_IRGRP|S_IROTH)
81 #define WRITE_MODE (S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)
82 #define DIR_MODE (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
83 #define UDIR_MODE (S_IRUSR|S_IXUSR)
84
85 #define N(s) sizeof(s)-1, s
86 const struct kern_target kern_targets[] = {
87 /* NOTE: The name must be less than UIO_MX-16 chars in length */
88 /* name data tag type ro/rw */
89 { DT_DIR, N("."), 0, Pkern, VDIR, DIR_MODE },
90 { DT_DIR, N(".."), 0, Proot, VDIR, DIR_MODE },
91 { DT_REG, N("boottime"), &boottime.tv_sec, Pint, VREG, READ_MODE },
92 /* XXX cast away const */
93 { DT_REG, N("copyright"), (void *)copyright,
94 Pstring, VREG, READ_MODE },
95 { DT_REG, N("hostname"), 0, Phostname, VREG, WRITE_MODE },
96 { DT_REG, N("hz"), &hz, Pint, VREG, READ_MODE },
97 #ifdef IPSEC
98 { DT_DIR, N("ipsecsa"), 0, Pipsecsadir, VDIR, UDIR_MODE },
99 { DT_DIR, N("ipsecsp"), 0, Pipsecspdir, VDIR, UDIR_MODE },
100 #endif
101 { DT_REG, N("loadavg"), 0, Pavenrun, VREG, READ_MODE },
102 { DT_REG, N("msgbuf"), 0, Pmsgbuf, VREG, READ_MODE },
103 { DT_REG, N("pagesize"), &uvmexp.pagesize, Pint, VREG, READ_MODE },
104 { DT_REG, N("physmem"), &physmem, Pint, VREG, READ_MODE },
105 #if 0
106 { DT_DIR, N("root"), 0, Pnull, VDIR, DIR_MODE },
107 #endif
108 { DT_BLK, N("rootdev"), &rootdev, Pdevice, VBLK, READ_MODE },
109 { DT_CHR, N("rrootdev"), &rrootdev, Pdevice, VCHR, READ_MODE },
110 { DT_REG, N("time"), 0, Ptime, VREG, READ_MODE },
111 /* XXX cast away const */
112 { DT_REG, N("version"), (void *)version,
113 Pstring, VREG, READ_MODE },
114 };
115 #ifdef IPSEC
116 const struct kern_target ipsecsa_targets[] = {
117 /* NOTE: The name must be less than UIO_MX-16 chars in length */
118 /* name data tag type ro/rw */
119 { DT_DIR, N("."), 0, Pipsecsadir, VDIR, DIR_MODE },
120 { DT_DIR, N(".."), 0, Pkern, VDIR, DIR_MODE },
121 };
122 const struct kern_target ipsecsp_targets[] = {
123 /* NOTE: The name must be less than UIO_MX-16 chars in length */
124 /* name data tag type ro/rw */
125 { DT_DIR, N("."), 0, Pipsecspdir, VDIR, DIR_MODE },
126 { DT_DIR, N(".."), 0, Pkern, VDIR, DIR_MODE },
127 };
128 #endif
129 #undef N
130 int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
131 #ifdef IPSEC
132 int nipsecsa_targets = sizeof(ipsecsa_targets) / sizeof(ipsecsa_targets[0]);
133 int nipsecsp_targets = sizeof(ipsecsp_targets) / sizeof(ipsecsp_targets[0]);
134 #endif
135
136
137 int kernfs_lookup __P((void *));
138 #define kernfs_create genfs_eopnotsupp
139 #define kernfs_mknod genfs_eopnotsupp
140 int kernfs_open __P((void *));
141 int kernfs_close __P((void *));
142 int kernfs_access __P((void *));
143 int kernfs_getattr __P((void *));
144 int kernfs_setattr __P((void *));
145 int kernfs_read __P((void *));
146 int kernfs_write __P((void *));
147 #define kernfs_fcntl genfs_fcntl
148 #define kernfs_ioctl genfs_enoioctl
149 #define kernfs_poll genfs_poll
150 #define kernfs_revoke genfs_revoke
151 #define kernfs_fsync genfs_nullop
152 #define kernfs_seek genfs_nullop
153 #define kernfs_remove genfs_eopnotsupp
154 int kernfs_link __P((void *));
155 #define kernfs_rename genfs_eopnotsupp
156 #define kernfs_mkdir genfs_eopnotsupp
157 #define kernfs_rmdir genfs_eopnotsupp
158 int kernfs_symlink __P((void *));
159 int kernfs_readdir __P((void *));
160 #define kernfs_readlink genfs_eopnotsupp
161 #define kernfs_abortop genfs_abortop
162 int kernfs_inactive __P((void *));
163 int kernfs_reclaim __P((void *));
164 #define kernfs_lock genfs_lock
165 #define kernfs_unlock genfs_unlock
166 #define kernfs_bmap genfs_badop
167 #define kernfs_strategy genfs_badop
168 int kernfs_print __P((void *));
169 #define kernfs_islocked genfs_islocked
170 int kernfs_pathconf __P((void *));
171 #define kernfs_advlock genfs_einval
172 #define kernfs_blkatoff genfs_eopnotsupp
173 #define kernfs_valloc genfs_eopnotsupp
174 #define kernfs_vfree genfs_nullop
175 #define kernfs_truncate genfs_eopnotsupp
176 #define kernfs_update genfs_nullop
177 #define kernfs_bwrite genfs_eopnotsupp
178 #define kernfs_putpages genfs_putpages
179
180 static int kernfs_xread __P((struct kernfs_node *, int, char **, size_t, size_t *));
181 static int kernfs_xwrite __P((const struct kernfs_node *, char *, size_t));
182
183 int (**kernfs_vnodeop_p) __P((void *));
184 const struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
185 { &vop_default_desc, vn_default_error },
186 { &vop_lookup_desc, kernfs_lookup }, /* lookup */
187 { &vop_create_desc, kernfs_create }, /* create */
188 { &vop_mknod_desc, kernfs_mknod }, /* mknod */
189 { &vop_open_desc, kernfs_open }, /* open */
190 { &vop_close_desc, kernfs_close }, /* close */
191 { &vop_access_desc, kernfs_access }, /* access */
192 { &vop_getattr_desc, kernfs_getattr }, /* getattr */
193 { &vop_setattr_desc, kernfs_setattr }, /* setattr */
194 { &vop_read_desc, kernfs_read }, /* read */
195 { &vop_write_desc, kernfs_write }, /* write */
196 { &vop_fcntl_desc, kernfs_fcntl }, /* fcntl */
197 { &vop_ioctl_desc, kernfs_ioctl }, /* ioctl */
198 { &vop_poll_desc, kernfs_poll }, /* poll */
199 { &vop_revoke_desc, kernfs_revoke }, /* revoke */
200 { &vop_fsync_desc, kernfs_fsync }, /* fsync */
201 { &vop_seek_desc, kernfs_seek }, /* seek */
202 { &vop_remove_desc, kernfs_remove }, /* remove */
203 { &vop_link_desc, kernfs_link }, /* link */
204 { &vop_rename_desc, kernfs_rename }, /* rename */
205 { &vop_mkdir_desc, kernfs_mkdir }, /* mkdir */
206 { &vop_rmdir_desc, kernfs_rmdir }, /* rmdir */
207 { &vop_symlink_desc, kernfs_symlink }, /* symlink */
208 { &vop_readdir_desc, kernfs_readdir }, /* readdir */
209 { &vop_readlink_desc, kernfs_readlink }, /* readlink */
210 { &vop_abortop_desc, kernfs_abortop }, /* abortop */
211 { &vop_inactive_desc, kernfs_inactive }, /* inactive */
212 { &vop_reclaim_desc, kernfs_reclaim }, /* reclaim */
213 { &vop_lock_desc, kernfs_lock }, /* lock */
214 { &vop_unlock_desc, kernfs_unlock }, /* unlock */
215 { &vop_bmap_desc, kernfs_bmap }, /* bmap */
216 { &vop_strategy_desc, kernfs_strategy }, /* strategy */
217 { &vop_print_desc, kernfs_print }, /* print */
218 { &vop_islocked_desc, kernfs_islocked }, /* islocked */
219 { &vop_pathconf_desc, kernfs_pathconf }, /* pathconf */
220 { &vop_advlock_desc, kernfs_advlock }, /* advlock */
221 { &vop_blkatoff_desc, kernfs_blkatoff }, /* blkatoff */
222 { &vop_valloc_desc, kernfs_valloc }, /* valloc */
223 { &vop_vfree_desc, kernfs_vfree }, /* vfree */
224 { &vop_truncate_desc, kernfs_truncate }, /* truncate */
225 { &vop_update_desc, kernfs_update }, /* update */
226 { &vop_bwrite_desc, kernfs_bwrite }, /* bwrite */
227 { &vop_putpages_desc, kernfs_putpages }, /* putpages */
228 { NULL, NULL }
229 };
230 const struct vnodeopv_desc kernfs_vnodeop_opv_desc =
231 { &kernfs_vnodeop_p, kernfs_vnodeop_entries };
232
233 static int
234 kernfs_xread(kfs, off, bufp, len, wrlen)
235 struct kernfs_node *kfs;
236 int off;
237 char **bufp;
238 size_t len;
239 size_t *wrlen;
240 {
241 const struct kern_target *kt;
242 #ifdef IPSEC
243 struct mbuf *m;
244 #endif
245
246 kt = kfs->kfs_kt;
247
248 switch (kfs->kfs_type) {
249 case Ptime: {
250 struct timeval tv;
251
252 microtime(&tv);
253 snprintf(*bufp, len, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
254 break;
255 }
256
257 case Pint: {
258 int *ip = kt->kt_data;
259
260 snprintf(*bufp, len, "%d\n", *ip);
261 break;
262 }
263
264 case Pstring: {
265 char *cp = kt->kt_data;
266
267 *bufp = cp;
268 break;
269 }
270
271 case Pmsgbuf: {
272 long n;
273
274 /*
275 * deal with cases where the message buffer has
276 * become corrupted.
277 */
278 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
279 msgbufenabled = 0;
280 return (ENXIO);
281 }
282
283 /*
284 * Note that reads of /kern/msgbuf won't necessarily yield
285 * consistent results, if the message buffer is modified
286 * while the read is in progress. The worst that can happen
287 * is that incorrect data will be read. There's no way
288 * that this can crash the system unless the values in the
289 * message buffer header are corrupted, but that'll cause
290 * the system to die anyway.
291 */
292 if (off >= msgbufp->msg_bufs) {
293 *wrlen = 0;
294 return (0);
295 }
296 n = msgbufp->msg_bufx + off;
297 if (n >= msgbufp->msg_bufs)
298 n -= msgbufp->msg_bufs;
299 len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
300 *bufp = msgbufp->msg_bufc + n;
301 *wrlen = len;
302 return (0);
303 }
304
305 case Phostname: {
306 char *cp = hostname;
307 int xlen = hostnamelen;
308
309 if (xlen >= (len - 2))
310 return (EINVAL);
311
312 memcpy(*bufp, cp, xlen);
313 (*bufp)[xlen] = '\n';
314 (*bufp)[xlen+1] = '\0';
315 len = strlen(*bufp);
316 break;
317 }
318
319 case Pavenrun:
320 averunnable.fscale = FSCALE;
321 snprintf(*bufp, len, "%d %d %d %ld\n",
322 averunnable.ldavg[0], averunnable.ldavg[1],
323 averunnable.ldavg[2], averunnable.fscale);
324 break;
325
326 #ifdef IPSEC
327 case Pipsecsa:
328 /*
329 * Note that SA configuration could be changed during the
330 * read operation, resulting in garbled output.
331 */
332 m = key_setdumpsa_spi(htonl(kfs->kfs_value));
333 if (!m)
334 return (ENOBUFS);
335 if (off >= m->m_pkthdr.len) {
336 *wrlen = 0;
337 m_freem(m);
338 return (0);
339 }
340 if (len > m->m_pkthdr.len - off)
341 len = m->m_pkthdr.len - off;
342 m_copydata(m, off, len, *bufp);
343 *wrlen = len;
344 m_freem(m);
345 return (0);
346
347 case Pipsecsp:
348 /*
349 * Note that SP configuration could be changed during the
350 * read operation, resulting in garbled output.
351 */
352 if (!kfs->kfs_v) {
353 struct secpolicy *sp;
354
355 sp = key_getspbyid(kfs->kfs_value);
356 if (sp)
357 kfs->kfs_v = sp;
358 else
359 return (ENOENT);
360 }
361 m = key_setdumpsp((struct secpolicy *)kfs->kfs_v,
362 SADB_X_SPDGET, 0, 0);
363 if (!m)
364 return (ENOBUFS);
365 if (off >= m->m_pkthdr.len) {
366 *wrlen = 0;
367 m_freem(m);
368 return (0);
369 }
370 if (len > m->m_pkthdr.len - off)
371 len = m->m_pkthdr.len - off;
372 m_copydata(m, off, len, *bufp);
373 *wrlen = len;
374 m_freem(m);
375 return (0);
376 #endif
377
378 default:
379 *wrlen = 0;
380 return (0);
381 }
382
383 len = strlen(*bufp);
384 if (len <= off)
385 *wrlen = 0;
386 else {
387 *bufp += off;
388 *wrlen = len - off;
389 }
390 return (0);
391 }
392
393 static int
394 kernfs_xwrite(kfs, buf, len)
395 const struct kernfs_node *kfs;
396 char *buf;
397 size_t len;
398 {
399
400 switch (kfs->kfs_type) {
401 case Phostname:
402 if (buf[len-1] == '\n')
403 --len;
404 memcpy(hostname, buf, len);
405 hostname[len] = '\0';
406 hostnamelen = (size_t) len;
407 return (0);
408
409 default:
410 return (EIO);
411 }
412 }
413
414
415 /*
416 * vp is the current namei directory
417 * ndp is the name to locate in that directory...
418 */
419 int
420 kernfs_lookup(v)
421 void *v;
422 {
423 struct vop_lookup_args /* {
424 struct vnode * a_dvp;
425 struct vnode ** a_vpp;
426 struct componentname * a_cnp;
427 } */ *ap = v;
428 struct componentname *cnp = ap->a_cnp;
429 struct vnode **vpp = ap->a_vpp;
430 struct vnode *dvp = ap->a_dvp;
431 const char *pname = cnp->cn_nameptr;
432 const struct kernfs_node *kfs;
433 const struct kern_target *kt;
434 int error, i, wantpunlock;
435 #ifdef IPSEC
436 char *ep;
437 u_int32_t id;
438 #endif
439
440 *vpp = NULLVP;
441 cnp->cn_flags &= ~PDIRUNLOCK;
442
443 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
444 return (EROFS);
445
446 if (cnp->cn_namelen == 1 && *pname == '.') {
447 *vpp = dvp;
448 VREF(dvp);
449 return (0);
450 }
451
452 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
453 kfs = VTOKERN(dvp);
454 switch (kfs->kfs_type) {
455 case Pkern:
456 /*
457 * Shouldn't get here with .. in the root node.
458 */
459 if (cnp->cn_flags & ISDOTDOT)
460 return (EIO);
461
462 for (i = 0; i < nkern_targets; i++) {
463 kt = &kern_targets[i];
464 if (cnp->cn_namelen == kt->kt_namlen &&
465 memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
466 goto found;
467 }
468 break;
469
470 found:
471 error = kernfs_allocvp(dvp->v_mount, vpp, kt->kt_tag, kt, 0);
472 if ((error == 0) && wantpunlock) {
473 VOP_UNLOCK(dvp, 0);
474 cnp->cn_flags |= PDIRUNLOCK;
475 }
476 return (error);
477
478 #ifdef IPSEC
479 case Pipsecsadir:
480 for (i = 0; i < nipsecsa_targets; i++) {
481 kt = &ipsecsa_targets[i];
482 if (cnp->cn_namelen == kt->kt_namlen &&
483 memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) {
484 error = kernfs_allocvp(dvp->v_mount, vpp,
485 kt->kt_tag, kt, 0);
486 if ((error == 0) && wantpunlock) {
487 VOP_UNLOCK(dvp, 0);
488 cnp->cn_flags |= PDIRUNLOCK;
489 }
490 return (error);
491 }
492 }
493
494 ep = NULL;
495 id = strtoul(pname, &ep, 10);
496 if (!ep || *ep || ep == pname)
497 break;
498
499 error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsa, NULL, id);
500 if ((error == 0) && wantpunlock) {
501 VOP_UNLOCK(dvp, 0);
502 cnp->cn_flags |= PDIRUNLOCK;
503 }
504 return (error);
505
506 case Pipsecspdir:
507 for (i = 0; i < nipsecsp_targets; i++) {
508 kt = &ipsecsp_targets[i];
509 if (cnp->cn_namelen == kt->kt_namlen &&
510 memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) {
511 error = kernfs_allocvp(dvp->v_mount, vpp,
512 kt->kt_tag, kt, 0);
513 if ((error == 0) && wantpunlock) {
514 VOP_UNLOCK(dvp, 0);
515 cnp->cn_flags |= PDIRUNLOCK;
516 }
517 return (error);
518 }
519 }
520
521 ep = NULL;
522 id = strtoul(pname, &ep, 10);
523 if (!ep || *ep || ep == pname)
524 break;
525
526 error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsp, NULL, id);
527 if ((error == 0) && wantpunlock) {
528 VOP_UNLOCK(dvp, 0);
529 cnp->cn_flags |= PDIRUNLOCK;
530 }
531 return (error);
532 #endif
533
534 default:
535 return (ENOTDIR);
536 }
537
538 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
539 }
540
541 int
542 kernfs_open(v)
543 void *v;
544 {
545 struct vop_open_args /* {
546 struct vnode *a_vp;
547 int a_mode;
548 struct ucred *a_cred;
549 struct proc *a_p;
550 } */ *ap = v;
551 struct kernfs_node *kfs = VTOKERN(ap->a_vp);
552 #ifdef IPSEC
553 struct mbuf *m;
554 struct secpolicy *sp;
555 #endif
556
557 switch (kfs->kfs_type) {
558 #ifdef IPSEC
559 case Pipsecsa:
560 m = key_setdumpsa_spi(htonl(kfs->kfs_value));
561 if (m) {
562 m_freem(m);
563 return (0);
564 } else
565 return (ENOENT);
566
567 case Pipsecsp:
568 sp = key_getspbyid(kfs->kfs_value);
569 if (sp) {
570 kfs->kfs_v = sp;
571 return (0);
572 } else
573 return (ENOENT);
574 #endif
575
576 default:
577 return (0);
578 }
579 }
580
581 int
582 kernfs_close(v)
583 void *v;
584 {
585 struct vop_close_args /* {
586 struct vnode *a_vp;
587 int a_fflag;
588 struct ucred *a_cred;
589 struct proc *a_p;
590 } */ *ap = v;
591 struct kernfs_node *kfs = VTOKERN(ap->a_vp);
592
593 switch (kfs->kfs_type) {
594 #ifdef IPSEC
595 case Pipsecsp:
596 key_freesp((struct secpolicy *)kfs->kfs_v);
597 break;
598 #endif
599
600 default:
601 break;
602 }
603
604 return (0);
605 }
606
607 int
608 kernfs_access(v)
609 void *v;
610 {
611 struct vop_access_args /* {
612 struct vnode *a_vp;
613 int a_mode;
614 struct ucred *a_cred;
615 struct proc *a_p;
616 } */ *ap = v;
617 struct vattr va;
618 int error;
619
620 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
621 return (error);
622
623 return (vaccess(va.va_type, va.va_mode, va.va_uid, va.va_gid,
624 ap->a_mode, ap->a_cred));
625 }
626
627 int
628 kernfs_getattr(v)
629 void *v;
630 {
631 struct vop_getattr_args /* {
632 struct vnode *a_vp;
633 struct vattr *a_vap;
634 struct ucred *a_cred;
635 struct proc *a_p;
636 } */ *ap = v;
637 struct kernfs_node *kfs = VTOKERN(ap->a_vp);
638 struct vattr *vap = ap->a_vap;
639 int error = 0;
640 char strbuf[KSTRING], *buf;
641 size_t nread, total;
642
643 VATTR_NULL(vap);
644 vap->va_type = ap->a_vp->v_type;
645 vap->va_uid = 0;
646 vap->va_gid = 0;
647 vap->va_mode = kfs->kfs_mode;
648 vap->va_fileid = kfs->kfs_fileno;
649 vap->va_flags = 0;
650 vap->va_size = 0;
651 vap->va_blocksize = DEV_BSIZE;
652 /*
653 * Make all times be current TOD, except for the "boottime" node.
654 * Avoid microtime(9), it's slow.
655 * We don't guard the read from time(9) with splclock(9) since we
656 * don't actually need to be THAT sure the access is atomic.
657 */
658 if (kfs->kfs_kt->kt_namlen == 8 &&
659 !memcmp(kfs->kfs_kt->kt_name, "boottime", 8)) {
660 TIMEVAL_TO_TIMESPEC(&boottime, &vap->va_ctime);
661 } else {
662 TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime);
663 }
664 vap->va_atime = vap->va_mtime = vap->va_ctime;
665 vap->va_gen = 0;
666 vap->va_flags = 0;
667 vap->va_rdev = 0;
668 vap->va_bytes = 0;
669
670 switch (kfs->kfs_type) {
671 case Pkern:
672 vap->va_nlink = 4;
673 vap->va_bytes = vap->va_size = DEV_BSIZE;
674 break;
675
676 case Proot:
677 vap->va_nlink = 1;
678 vap->va_bytes = vap->va_size = DEV_BSIZE;
679 break;
680
681 case Pnull:
682 case Ptime:
683 case Pint:
684 case Pstring:
685 case Phostname:
686 case Pavenrun:
687 case Pdevice:
688 case Pmsgbuf:
689 #ifdef IPSEC
690 case Pipsecsa:
691 case Pipsecsp:
692 #endif
693 vap->va_nlink = 1;
694 total = 0;
695 do {
696 buf = strbuf;
697 error = kernfs_xread(kfs, total, &buf,
698 sizeof(strbuf), &nread);
699 total += nread;
700 } while (error == 0 && nread != 0);
701 vap->va_bytes = vap->va_size = total;
702 break;
703
704 #ifdef IPSEC
705 case Pipsecsadir:
706 case Pipsecspdir:
707 vap->va_nlink = 2;
708 vap->va_bytes = vap->va_size = DEV_BSIZE;
709 break;
710 #endif
711
712 default:
713 error = EINVAL;
714 break;
715 }
716
717 return (error);
718 }
719
720 /*ARGSUSED*/
721 int
722 kernfs_setattr(v)
723 void *v;
724 {
725
726 /*
727 * Silently ignore attribute changes.
728 * This allows for open with truncate to have no
729 * effect until some data is written. I want to
730 * do it this way because all writes are atomic.
731 */
732 return (0);
733 }
734
735 int
736 kernfs_read(v)
737 void *v;
738 {
739 struct vop_read_args /* {
740 struct vnode *a_vp;
741 struct uio *a_uio;
742 int a_ioflag;
743 struct ucred *a_cred;
744 } */ *ap = v;
745 struct uio *uio = ap->a_uio;
746 struct kernfs_node *kfs = VTOKERN(ap->a_vp);
747 char strbuf[KSTRING], *buf;
748 off_t off;
749 size_t len;
750 int error;
751
752 if (ap->a_vp->v_type == VDIR)
753 return (EOPNOTSUPP);
754
755 off = uio->uio_offset;
756 buf = strbuf;
757 if ((error = kernfs_xread(kfs, off, &buf, sizeof(strbuf), &len)) == 0)
758 error = uiomove(buf, len, uio);
759 return (error);
760 }
761
762 int
763 kernfs_write(v)
764 void *v;
765 {
766 struct vop_write_args /* {
767 struct vnode *a_vp;
768 struct uio *a_uio;
769 int a_ioflag;
770 struct ucred *a_cred;
771 } */ *ap = v;
772 struct kernfs_node *kfs = VTOKERN(ap->a_vp);
773 struct uio *uio = ap->a_uio;
774 int error, xlen;
775 char strbuf[KSTRING];
776
777 if (uio->uio_offset != 0)
778 return (EINVAL);
779
780 xlen = min(uio->uio_resid, KSTRING-1);
781 if ((error = uiomove(strbuf, xlen, uio)) != 0)
782 return (error);
783
784 if (uio->uio_resid != 0)
785 return (EIO);
786
787 strbuf[xlen] = '\0';
788 xlen = strlen(strbuf);
789 return (kernfs_xwrite(kfs, strbuf, xlen));
790 }
791
792 int
793 kernfs_readdir(v)
794 void *v;
795 {
796 struct vop_readdir_args /* {
797 struct vnode *a_vp;
798 struct uio *a_uio;
799 struct ucred *a_cred;
800 int *a_eofflag;
801 off_t **a_cookies;
802 int a_*ncookies;
803 } */ *ap = v;
804 struct uio *uio = ap->a_uio;
805 struct dirent d;
806 struct kernfs_node *kfs = VTOKERN(ap->a_vp);
807 const struct kern_target *kt;
808 off_t i;
809 int error;
810 off_t *cookies = NULL;
811 int ncookies = 0, n;
812 #ifdef IPSEC
813 struct secasvar *sav, *sav2;
814 struct secpolicy *sp;
815 #endif
816
817 if (uio->uio_resid < UIO_MX)
818 return (EINVAL);
819 if (uio->uio_offset < 0)
820 return (EINVAL);
821
822 error = 0;
823 i = uio->uio_offset;
824 memset(&d, 0, sizeof(d));
825 d.d_reclen = UIO_MX;
826 ncookies = uio->uio_resid / UIO_MX;
827
828 switch (kfs->kfs_type) {
829 case Pkern:
830 if (i >= nkern_targets)
831 return (0);
832
833 if (ap->a_ncookies) {
834 ncookies = min(ncookies, (nkern_targets - i));
835 cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
836 M_WAITOK);
837 *ap->a_cookies = cookies;
838 }
839
840 n = 0;
841 for (; i < nkern_targets && uio->uio_resid >= UIO_MX; i++) {
842 kt = &kern_targets[i];
843 d.d_namlen = kt->kt_namlen;
844 if (i < 2)
845 d.d_fileno = KERNFS_FILENO(&kern_targets[0],
846 kern_targets[0].kt_tag, 0);
847 else
848 d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
849 memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
850 d.d_type = kt->kt_type;
851 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
852 break;
853 if (cookies)
854 *cookies++ = i + 1;
855 n++;
856 }
857 ncookies = n;
858 break;
859
860 case Proot:
861 if (i >= 2)
862 return 0;
863
864 if (ap->a_ncookies) {
865 ncookies = min(ncookies, (2 - i));
866 cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
867 M_WAITOK);
868 *ap->a_cookies = cookies;
869 }
870
871 n = 0;
872 for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
873 kt = &kern_targets[i];
874 d.d_namlen = kt->kt_namlen;
875 d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
876 memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
877 d.d_type = kt->kt_type;
878 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
879 break;
880 if (cookies)
881 *cookies++ = i + 1;
882 n++;
883 }
884 ncookies = n;
885 break;
886
887 #ifdef IPSEC
888 case Pipsecsadir:
889 /* count SA in the system */
890 n = 0;
891 TAILQ_FOREACH(sav, &satailq, tailq) {
892 for (sav2 = TAILQ_FIRST(&satailq);
893 sav2 != sav;
894 sav2 = TAILQ_NEXT(sav2, tailq)) {
895 if (sav->spi == sav2->spi) {
896 /* multiple SA with same SPI */
897 break;
898 }
899 }
900 if (sav == sav2 || sav->spi != sav2->spi)
901 n++;
902 }
903
904 if (i >= nipsecsa_targets + n)
905 return (0);
906
907 if (ap->a_ncookies) {
908 ncookies = min(ncookies, (n - i));
909 cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
910 M_WAITOK);
911 *ap->a_cookies = cookies;
912 }
913
914 n = 0;
915 for (; i < nipsecsa_targets && uio->uio_resid >= UIO_MX; i++) {
916 kt = &ipsecsa_targets[i];
917 d.d_namlen = kt->kt_namlen;
918 d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
919 memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
920 d.d_type = kt->kt_type;
921 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
922 break;
923 if (cookies)
924 *cookies++ = i + 1;
925 n++;
926 }
927 if (error) {
928 ncookies = n;
929 break;
930 }
931
932 TAILQ_FOREACH(sav, &satailq, tailq) {
933 for (sav2 = TAILQ_FIRST(&satailq);
934 sav2 != sav;
935 sav2 = TAILQ_NEXT(sav2, tailq)) {
936 if (sav->spi == sav2->spi) {
937 /* multiple SA with same SPI */
938 break;
939 }
940 }
941 if (sav != sav2 && sav->spi == sav2->spi)
942 continue;
943 if (uio->uio_resid < UIO_MX)
944 break;
945 d.d_fileno = KERNFS_FILENO(kfs->kfs_kt, kfs->kfs_type,
946 kfs->kfs_cookie);
947 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
948 "%u", ntohl(sav->spi));
949 d.d_type = DT_REG;
950 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
951 break;
952 if (cookies)
953 *cookies++ = i + 1;
954 n++;
955 i++;
956 }
957 ncookies = n;
958 break;
959
960 case Pipsecspdir:
961 /* count SP in the system */
962 n = 0;
963 TAILQ_FOREACH(sp, &sptailq, tailq)
964 n++;
965
966 if (i >= 2 + n)
967 return (0);
968
969 if (ap->a_ncookies) {
970 ncookies = min(ncookies, (n - i));
971 cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
972 M_WAITOK);
973 *ap->a_cookies = cookies;
974 }
975
976 n = 0;
977 for (; i < nipsecsp_targets && uio->uio_resid >= UIO_MX; i++) {
978 kt = &ipsecsp_targets[i];
979 d.d_namlen = kt->kt_namlen;
980 d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
981 memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
982 d.d_type = kt->kt_type;
983 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
984 break;
985 if (cookies)
986 *cookies++ = i + 1;
987 n++;
988 }
989 if (error) {
990 ncookies = n;
991 break;
992 }
993
994 TAILQ_FOREACH(sp, &sptailq, tailq) {
995 if (uio->uio_resid < UIO_MX)
996 break;
997 d.d_fileno = KERNFS_FILENO(kfs->kfs_kt, kfs->kfs_type,
998 kfs->kfs_cookie);
999 d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
1000 "%u", sp->id);
1001 d.d_type = DT_REG;
1002 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1003 break;
1004 if (cookies)
1005 *cookies++ = i + 1;
1006 n++;
1007 i++;
1008 }
1009 ncookies = n;
1010 break;
1011 #endif
1012
1013 default:
1014 error = ENOTDIR;
1015 break;
1016 }
1017
1018 if (ap->a_ncookies) {
1019 if (error) {
1020 if (cookies)
1021 free(*ap->a_cookies, M_TEMP);
1022 *ap->a_ncookies = 0;
1023 *ap->a_cookies = NULL;
1024 } else
1025 *ap->a_ncookies = ncookies;
1026 }
1027
1028 uio->uio_offset = i;
1029 return (error);
1030 }
1031
1032 int
1033 kernfs_inactive(v)
1034 void *v;
1035 {
1036 struct vop_inactive_args /* {
1037 struct vnode *a_vp;
1038 struct proc *a_p;
1039 } */ *ap = v;
1040 struct vnode *vp = ap->a_vp;
1041 const struct kernfs_node *kfs = VTOKERN(ap->a_vp);
1042 #ifdef IPSEC
1043 struct mbuf *m;
1044 struct secpolicy *sp;
1045 #endif
1046
1047 VOP_UNLOCK(vp, 0);
1048 switch (kfs->kfs_type) {
1049 #ifdef IPSEC
1050 case Pipsecsa:
1051 m = key_setdumpsa_spi(htonl(kfs->kfs_value));
1052 if (m)
1053 m_freem(m);
1054 else
1055 vgone(vp);
1056 break;
1057 case Pipsecsp:
1058 sp = key_getspbyid(kfs->kfs_value);
1059 if (sp)
1060 key_freesp(sp);
1061 else {
1062 /* should never happen as we hold a refcnt */
1063 vgone(vp);
1064 }
1065 break;
1066 #endif
1067 default:
1068 break;
1069 }
1070 return (0);
1071 }
1072
1073 int
1074 kernfs_reclaim(v)
1075 void *v;
1076 {
1077 struct vop_reclaim_args /* {
1078 struct vnode *a_vp;
1079 } */ *ap = v;
1080
1081 return (kernfs_freevp(ap->a_vp));
1082 }
1083
1084 /*
1085 * Return POSIX pathconf information applicable to special devices.
1086 */
1087 int
1088 kernfs_pathconf(v)
1089 void *v;
1090 {
1091 struct vop_pathconf_args /* {
1092 struct vnode *a_vp;
1093 int a_name;
1094 register_t *a_retval;
1095 } */ *ap = v;
1096
1097 switch (ap->a_name) {
1098 case _PC_LINK_MAX:
1099 *ap->a_retval = LINK_MAX;
1100 return (0);
1101 case _PC_MAX_CANON:
1102 *ap->a_retval = MAX_CANON;
1103 return (0);
1104 case _PC_MAX_INPUT:
1105 *ap->a_retval = MAX_INPUT;
1106 return (0);
1107 case _PC_PIPE_BUF:
1108 *ap->a_retval = PIPE_BUF;
1109 return (0);
1110 case _PC_CHOWN_RESTRICTED:
1111 *ap->a_retval = 1;
1112 return (0);
1113 case _PC_VDISABLE:
1114 *ap->a_retval = _POSIX_VDISABLE;
1115 return (0);
1116 case _PC_SYNC_IO:
1117 *ap->a_retval = 1;
1118 return (0);
1119 default:
1120 return (EINVAL);
1121 }
1122 /* NOTREACHED */
1123 }
1124
1125 /*
1126 * Print out the contents of a /dev/fd vnode.
1127 */
1128 /* ARGSUSED */
1129 int
1130 kernfs_print(v)
1131 void *v;
1132 {
1133
1134 printf("tag VT_KERNFS, kernfs vnode\n");
1135 return (0);
1136 }
1137
1138 int
1139 kernfs_link(v)
1140 void *v;
1141 {
1142 struct vop_link_args /* {
1143 struct vnode *a_dvp;
1144 struct vnode *a_vp;
1145 struct componentname *a_cnp;
1146 } */ *ap = v;
1147
1148 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1149 vput(ap->a_dvp);
1150 return (EROFS);
1151 }
1152
1153 int
1154 kernfs_symlink(v)
1155 void *v;
1156 {
1157 struct vop_symlink_args /* {
1158 struct vnode *a_dvp;
1159 struct vnode **a_vpp;
1160 struct componentname *a_cnp;
1161 struct vattr *a_vap;
1162 char *a_target;
1163 } */ *ap = v;
1164
1165 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1166 vput(ap->a_dvp);
1167 return (EROFS);
1168 }
1169