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