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

1.63    ! mrg         1: /*     $NetBSD: kernfs_vnops.c,v 1.62 1998/08/13 10:06:33 kleink 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.44      mycroft   102: #define        kernfs_create   genfs_eopnotsupp
                    103: #define        kernfs_mknod    genfs_eopnotsupp
                    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
                    117: #define        kernfs_remove   genfs_eopnotsupp
1.41      christos  118: int    kernfs_link     __P((void *));
1.44      mycroft   119: #define        kernfs_rename   genfs_eopnotsupp
                    120: #define        kernfs_mkdir    genfs_eopnotsupp
                    121: #define        kernfs_rmdir    genfs_eopnotsupp
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.57      fvdl      128: #define        kernfs_lock     genfs_nolock
                    129: #define        kernfs_unlock   genfs_nounlock
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.57      fvdl      133: #define        kernfs_islocked genfs_noislocked
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.23      mycroft   329:        int error, i;
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;
                    338:
                    339:        if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
                    340:                return (EROFS);
                    341:
1.57      fvdl      342:        VOP_UNLOCK(dvp, 0);
1.23      mycroft   343:        if (cnp->cn_namelen == 1 && *pname == '.') {
                    344:                *vpp = dvp;
1.1       cgd       345:                VREF(dvp);
1.57      fvdl      346:                vn_lock(dvp, LK_SHARED | LK_RETRY);
1.1       cgd       347:                return (0);
                    348:        }
1.13      cgd       349:
1.17      cgd       350: #if 0
1.60      perry     351:        if (cnp->cn_namelen == 4 && memcmp(pname, "root", 4) == 0) {
1.23      mycroft   352:                *vpp = rootdir;
1.17      cgd       353:                VREF(rootdir);
1.57      fvdl      354:                vn_lock(rootdir, LK_SHARED | LK_RETRY);
1.1       cgd       355:                return (0);
                    356:        }
1.13      cgd       357: #endif
1.25      mycroft   358:
1.35      mycroft   359:        for (kt = kern_targets, i = 0; i < nkern_targets; kt++, i++) {
1.26      mycroft   360:                if (cnp->cn_namelen == kt->kt_namlen &&
1.60      perry     361:                    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
1.35      mycroft   362:                        goto found;
1.1       cgd       363:        }
                    364:
                    365: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  366:        printf("kernfs_lookup: i = %d, failed", i);
1.1       cgd       367: #endif
                    368:
1.57      fvdl      369:        vn_lock(dvp, LK_SHARED | LK_RETRY);
1.35      mycroft   370:        return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
1.23      mycroft   371:
1.35      mycroft   372: found:
1.23      mycroft   373:        if (kt->kt_tag == KTT_DEVICE) {
                    374:                dev_t *dp = kt->kt_data;
1.24      mycroft   375:        loop:
1.57      fvdl      376:                if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp)) {
                    377:                        vn_lock(dvp, LK_SHARED | LK_RETRY);
1.24      mycroft   378:                        return (ENOENT);
1.57      fvdl      379:                }
1.23      mycroft   380:                *vpp = fvp;
1.57      fvdl      381:                if (vget(fvp, LK_EXCLUSIVE))
1.24      mycroft   382:                        goto loop;
1.23      mycroft   383:                return (0);
                    384:        }
1.1       cgd       385:
                    386: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  387:        printf("kernfs_lookup: allocate new vnode\n");
1.1       cgd       388: #endif
1.41      christos  389:        error = getnewvnode(VT_KERNFS, dvp->v_mount, kernfs_vnodeop_p, &fvp);
1.57      fvdl      390:        if (error) {
                    391:                vn_lock(dvp, LK_SHARED | LK_RETRY);
1.23      mycroft   392:                return (error);
1.57      fvdl      393:        }
1.23      mycroft   394:
                    395:        MALLOC(fvp->v_data, void *, sizeof(struct kernfs_node), M_TEMP,
                    396:            M_WAITOK);
                    397:        VTOKERN(fvp)->kf_kt = kt;
                    398:        fvp->v_type = kt->kt_vtype;
1.57      fvdl      399:        vn_lock(fvp, LK_SHARED | LK_RETRY);
1.23      mycroft   400:        *vpp = fvp;
                    401:
1.1       cgd       402: #ifdef KERNFS_DIAGNOSTIC
1.51      christos  403:        printf("kernfs_lookup: newvp = %p\n", fvp);
1.1       cgd       404: #endif
                    405:        return (0);
                    406: }
                    407:
1.28      mycroft   408: int
1.41      christos  409: kernfs_access(v)
                    410:        void *v;
                    411: {
1.23      mycroft   412:        struct vop_access_args /* {
                    413:                struct vnode *a_vp;
1.34      mycroft   414:                int a_mode;
1.23      mycroft   415:                struct ucred *a_cred;
                    416:                struct proc *a_p;
1.41      christos  417:        } */ *ap = v;
1.26      mycroft   418:        struct vnode *vp = ap->a_vp;
1.49      mycroft   419:        mode_t mode;
1.17      cgd       420:
1.49      mycroft   421:        if (vp->v_flag & VROOT) {
                    422:                mode = DIR_MODE;
                    423:        } else {
                    424:                struct kern_target *kt = VTOKERN(vp)->kf_kt;
                    425:                mode = kt->kt_mode;
                    426:        }
                    427:
                    428:        return (vaccess(vp->v_type, mode, (uid_t)0, (gid_t)0, ap->a_mode,
                    429:            ap->a_cred));
1.23      mycroft   430: }
                    431:
1.41      christos  432: int
                    433: kernfs_getattr(v)
                    434:        void *v;
                    435: {
1.23      mycroft   436:        struct vop_getattr_args /* {
                    437:                struct vnode *a_vp;
                    438:                struct vattr *a_vap;
                    439:                struct ucred *a_cred;
                    440:                struct proc *a_p;
1.41      christos  441:        } */ *ap = v;
1.23      mycroft   442:        struct vnode *vp = ap->a_vp;
                    443:        struct vattr *vap = ap->a_vap;
1.36      cgd       444:        struct timeval tv;
1.1       cgd       445:        int error = 0;
1.28      mycroft   446:        char strbuf[KSTRING], *buf;
1.1       cgd       447:
1.60      perry     448:        memset((caddr_t) vap, 0, sizeof(*vap));
1.1       cgd       449:        vattr_null(vap);
1.17      cgd       450:        vap->va_uid = 0;
                    451:        vap->va_gid = 0;
1.1       cgd       452:        vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1.23      mycroft   453:        vap->va_size = 0;
1.1       cgd       454:        vap->va_blocksize = DEV_BSIZE;
1.36      cgd       455:        microtime(&tv);
                    456:        TIMEVAL_TO_TIMESPEC(&tv, &vap->va_atime);
1.1       cgd       457:        vap->va_mtime = vap->va_atime;
                    458:        vap->va_ctime = vap->va_ctime;
                    459:        vap->va_gen = 0;
                    460:        vap->va_flags = 0;
                    461:        vap->va_rdev = 0;
                    462:        vap->va_bytes = 0;
                    463:
                    464:        if (vp->v_flag & VROOT) {
                    465: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  466:                printf("kernfs_getattr: stat rootdir\n");
1.1       cgd       467: #endif
1.17      cgd       468:                vap->va_type = VDIR;
1.23      mycroft   469:                vap->va_mode = DIR_MODE;
1.1       cgd       470:                vap->va_nlink = 2;
                    471:                vap->va_fileid = 2;
                    472:                vap->va_size = DEV_BSIZE;
                    473:        } else {
1.23      mycroft   474:                struct kern_target *kt = VTOKERN(vp)->kf_kt;
1.28      mycroft   475:                int nbytes, total;
1.1       cgd       476: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  477:                printf("kernfs_getattr: stat target %s\n", kt->kt_name);
1.1       cgd       478: #endif
1.17      cgd       479:                vap->va_type = kt->kt_vtype;
1.23      mycroft   480:                vap->va_mode = kt->kt_mode;
1.1       cgd       481:                vap->va_nlink = 1;
1.57      fvdl      482:                vap->va_fileid = 1 + (kt - kern_targets);
1.28      mycroft   483:                total = 0;
                    484:                while (buf = strbuf,
                    485:                       nbytes = kernfs_xread(kt, total, &buf, sizeof(strbuf)))
                    486:                        total += nbytes;
                    487:                vap->va_size = total;
1.1       cgd       488:        }
                    489:
                    490: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  491:        printf("kernfs_getattr: return error %d\n", error);
1.1       cgd       492: #endif
                    493:        return (error);
                    494: }
                    495:
1.41      christos  496: /*ARGSUSED*/
                    497: int
                    498: kernfs_setattr(v)
                    499:        void *v;
1.1       cgd       500: {
                    501:        /*
1.17      cgd       502:         * Silently ignore attribute changes.
                    503:         * This allows for open with truncate to have no
                    504:         * effect until some data is written.  I want to
                    505:         * do it this way because all writes are atomic.
1.1       cgd       506:         */
1.17      cgd       507:        return (0);
1.1       cgd       508: }
                    509:
1.28      mycroft   510: int
1.41      christos  511: kernfs_read(v)
                    512:        void *v;
                    513: {
1.23      mycroft   514:        struct vop_read_args /* {
                    515:                struct vnode *a_vp;
                    516:                struct uio *a_uio;
                    517:                int  a_ioflag;
                    518:                struct ucred *a_cred;
1.41      christos  519:        } */ *ap = v;
1.23      mycroft   520:        struct vnode *vp = ap->a_vp;
                    521:        struct uio *uio = ap->a_uio;
                    522:        struct kern_target *kt;
1.28      mycroft   523:        char strbuf[KSTRING], *buf;
                    524:        int off, len;
                    525:        int error;
1.23      mycroft   526:
                    527:        if (vp->v_type == VDIR)
                    528:                return (EOPNOTSUPP);
                    529:
                    530:        kt = VTOKERN(vp)->kf_kt;
                    531:
1.1       cgd       532: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  533:        printf("kern_read %s\n", kt->kt_name);
1.1       cgd       534: #endif
1.18      cgd       535:
1.28      mycroft   536:        off = uio->uio_offset;
                    537: #if 0
                    538:        while (buf = strbuf,
                    539: #else
                    540:        if (buf = strbuf,
                    541: #endif
                    542:            len = kernfs_xread(kt, off, &buf, sizeof(strbuf))) {
1.41      christos  543:                if ((error = uiomove(buf, len, uio)) != 0)
1.28      mycroft   544:                        return (error);
                    545:                off += len;
                    546:        }
                    547:        return (0);
1.1       cgd       548: }
                    549:
1.28      mycroft   550: int
1.41      christos  551: kernfs_write(v)
                    552:        void *v;
                    553: {
1.23      mycroft   554:        struct vop_write_args /* {
                    555:                struct vnode *a_vp;
                    556:                struct uio *a_uio;
                    557:                int  a_ioflag;
                    558:                struct ucred *a_cred;
1.41      christos  559:        } */ *ap = v;
1.23      mycroft   560:        struct vnode *vp = ap->a_vp;
                    561:        struct uio *uio = ap->a_uio;
                    562:        struct kern_target *kt;
                    563:        int error, xlen;
1.1       cgd       564:        char strbuf[KSTRING];
1.23      mycroft   565:
                    566:        if (vp->v_type == VDIR)
                    567:                return (EOPNOTSUPP);
                    568:
                    569:        kt = VTOKERN(vp)->kf_kt;
1.1       cgd       570:
                    571:        if (uio->uio_offset != 0)
                    572:                return (EINVAL);
                    573:
                    574:        xlen = min(uio->uio_resid, KSTRING-1);
1.41      christos  575:        if ((error = uiomove(strbuf, xlen, uio)) != 0)
1.1       cgd       576:                return (error);
                    577:
                    578:        if (uio->uio_resid != 0)
                    579:                return (EIO);
                    580:
                    581:        strbuf[xlen] = '\0';
1.17      cgd       582:        xlen = strlen(strbuf);
1.1       cgd       583:        return (kernfs_xwrite(kt, strbuf, xlen));
                    584: }
                    585:
1.41      christos  586: int
                    587: kernfs_readdir(v)
                    588:        void *v;
                    589: {
1.23      mycroft   590:        struct vop_readdir_args /* {
                    591:                struct vnode *a_vp;
                    592:                struct uio *a_uio;
                    593:                struct ucred *a_cred;
1.26      mycroft   594:                int *a_eofflag;
1.57      fvdl      595:                off_t **a_cookies;
                    596:                int a_*ncookies;
1.41      christos  597:        } */ *ap = v;
1.23      mycroft   598:        struct uio *uio = ap->a_uio;
1.37      mycroft   599:        struct dirent d;
1.26      mycroft   600:        struct kern_target *kt;
1.1       cgd       601:        int i;
                    602:        int error;
1.57      fvdl      603:        off_t *cookies = NULL;
                    604:        int ncookies = 0, nc = 0;
1.1       cgd       605:
1.23      mycroft   606:        if (ap->a_vp->v_type != VDIR)
                    607:                return (ENOTDIR);
                    608:
1.37      mycroft   609:        if (uio->uio_resid < UIO_MX)
                    610:                return (EINVAL);
1.38      mycroft   611:        if (uio->uio_offset < 0)
1.37      mycroft   612:                return (EINVAL);
1.26      mycroft   613:
1.1       cgd       614:        error = 0;
1.38      mycroft   615:        i = uio->uio_offset;
1.60      perry     616:        memset((caddr_t)&d, 0, UIO_MX);
1.37      mycroft   617:        d.d_reclen = UIO_MX;
                    618:
1.57      fvdl      619:        if (ap->a_ncookies) {
                    620:                nc = uio->uio_resid / UIO_MX;
                    621:                nc = min(nc, (nkern_targets - i));
                    622:                MALLOC(cookies, off_t *, nc * sizeof(off_t), M_TEMP,
                    623:                    M_WAITOK);
                    624:                *ap->a_cookies = cookies;
                    625:        }
                    626:
1.23      mycroft   627:        for (kt = &kern_targets[i];
                    628:             uio->uio_resid >= UIO_MX && i < nkern_targets; kt++, i++) {
1.1       cgd       629: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  630:                printf("kernfs_readdir: i = %d\n", i);
1.1       cgd       631: #endif
1.26      mycroft   632:
1.23      mycroft   633:                if (kt->kt_tag == KTT_DEVICE) {
1.26      mycroft   634:                        dev_t *dp = kt->kt_data;
1.23      mycroft   635:                        struct vnode *fvp;
1.26      mycroft   636:
1.23      mycroft   637:                        if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
                    638:                                continue;
                    639:                }
1.26      mycroft   640:
1.37      mycroft   641:                d.d_fileno = i + 3;
                    642:                d.d_namlen = kt->kt_namlen;
1.60      perry     643:                memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
1.37      mycroft   644:                d.d_type = kt->kt_type;
1.26      mycroft   645:
1.41      christos  646:                if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1.1       cgd       647:                        break;
1.57      fvdl      648:                if (cookies) {
1.38      mycroft   649:                        *cookies++ = i + 1;
1.57      fvdl      650:                        ncookies++;
                    651:                }
                    652:        }
                    653:
                    654:        if (ap->a_ncookies) {
                    655:                if (error) {
                    656:                        FREE(*ap->a_cookies, M_TEMP);
                    657:                        *ap->a_ncookies = 0;
                    658:                        *ap->a_cookies = NULL;
                    659:                } else
                    660:                        *ap->a_ncookies = ncookies;
1.1       cgd       661:        }
                    662:
1.38      mycroft   663:        uio->uio_offset = i;
1.1       cgd       664:        return (error);
                    665: }
                    666:
1.41      christos  667: int
                    668: kernfs_inactive(v)
                    669:        void *v;
                    670: {
1.23      mycroft   671:        struct vop_inactive_args /* {
                    672:                struct vnode *a_vp;
1.57      fvdl      673:                struct proc *a_p;
1.41      christos  674:        } */ *ap = v;
1.23      mycroft   675:        struct vnode *vp = ap->a_vp;
                    676:
                    677: #ifdef KERNFS_DIAGNOSTIC
1.51      christos  678:        printf("kernfs_inactive(%p)\n", vp);
1.23      mycroft   679: #endif
1.1       cgd       680:        /*
                    681:         * Clear out the v_type field to avoid
                    682:         * nasty things happening in vgone().
                    683:         */
1.57      fvdl      684:        VOP_UNLOCK(vp, 0);
1.1       cgd       685:        vp->v_type = VNON;
1.23      mycroft   686:        return (0);
                    687: }
                    688:
1.41      christos  689: int
                    690: kernfs_reclaim(v)
                    691:        void *v;
                    692: {
1.23      mycroft   693:        struct vop_reclaim_args /* {
                    694:                struct vnode *a_vp;
1.41      christos  695:        } */ *ap = v;
1.23      mycroft   696:        struct vnode *vp = ap->a_vp;
                    697:
1.1       cgd       698: #ifdef KERNFS_DIAGNOSTIC
1.51      christos  699:        printf("kernfs_reclaim(%p)\n", vp);
1.1       cgd       700: #endif
1.23      mycroft   701:        if (vp->v_data) {
                    702:                FREE(vp->v_data, M_TEMP);
                    703:                vp->v_data = 0;
                    704:        }
1.1       cgd       705:        return (0);
                    706: }
                    707:
                    708: /*
1.23      mycroft   709:  * Return POSIX pathconf information applicable to special devices.
                    710:  */
1.41      christos  711: int
                    712: kernfs_pathconf(v)
                    713:        void *v;
                    714: {
1.23      mycroft   715:        struct vop_pathconf_args /* {
                    716:                struct vnode *a_vp;
                    717:                int a_name;
1.29      cgd       718:                register_t *a_retval;
1.41      christos  719:        } */ *ap = v;
1.23      mycroft   720:
                    721:        switch (ap->a_name) {
                    722:        case _PC_LINK_MAX:
                    723:                *ap->a_retval = LINK_MAX;
                    724:                return (0);
                    725:        case _PC_MAX_CANON:
                    726:                *ap->a_retval = MAX_CANON;
                    727:                return (0);
                    728:        case _PC_MAX_INPUT:
                    729:                *ap->a_retval = MAX_INPUT;
                    730:                return (0);
                    731:        case _PC_PIPE_BUF:
                    732:                *ap->a_retval = PIPE_BUF;
                    733:                return (0);
                    734:        case _PC_CHOWN_RESTRICTED:
                    735:                *ap->a_retval = 1;
                    736:                return (0);
                    737:        case _PC_VDISABLE:
                    738:                *ap->a_retval = _POSIX_VDISABLE;
1.59      kleink    739:                return (0);
                    740:        case _PC_SYNC_IO:
                    741:                *ap->a_retval = 1;
1.23      mycroft   742:                return (0);
                    743:        default:
                    744:                return (EINVAL);
                    745:        }
                    746:        /* NOTREACHED */
                    747: }
                    748:
                    749: /*
                    750:  * Print out the contents of a /dev/fd vnode.
1.1       cgd       751:  */
                    752: /* ARGSUSED */
1.41      christos  753: int
                    754: kernfs_print(v)
                    755:        void *v;
1.23      mycroft   756: {
                    757:
1.47      christos  758:        printf("tag VT_KERNFS, kernfs vnode\n");
1.23      mycroft   759:        return (0);
                    760: }
                    761:
1.40      mycroft   762: int
1.41      christos  763: kernfs_link(v)
                    764:        void *v;
                    765: {
1.40      mycroft   766:        struct vop_link_args /* {
                    767:                struct vnode *a_dvp;
                    768:                struct vnode *a_vp;
                    769:                struct componentname *a_cnp;
1.41      christos  770:        } */ *ap = v;
1.40      mycroft   771:
                    772:        VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
                    773:        vput(ap->a_dvp);
                    774:        return (EROFS);
                    775: }
                    776:
                    777: int
1.41      christos  778: kernfs_symlink(v)
                    779:        void *v;
                    780: {
1.40      mycroft   781:        struct vop_symlink_args /* {
                    782:                struct vnode *a_dvp;
                    783:                struct vnode **a_vpp;
                    784:                struct componentname *a_cnp;
                    785:                struct vattr *a_vap;
                    786:                char *a_target;
1.41      christos  787:        } */ *ap = v;
1.40      mycroft   788:
                    789:        VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
                    790:        vput(ap->a_dvp);
                    791:        return (EROFS);
1.1       cgd       792: }

CVSweb <webmaster@jp.NetBSD.org>