[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.64

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

CVSweb <webmaster@jp.NetBSD.org>