[BACK]Return to kernfs_vnops.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / miscfs / kernfs

Annotation of src/sys/miscfs/kernfs/kernfs_vnops.c, Revision 1.40

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

CVSweb <webmaster@jp.NetBSD.org>