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

1.56    ! thorpej     1: /*     $NetBSD: kernfs_vnops.c,v 1.55 1998/02/10 14:10:32 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.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:  */
1.55      mrg        44:
1.56    ! thorpej    45: #ifndef _LKM
1.55      mrg        46: #include "opt_uvm.h"
1.56    ! thorpej    47: #endif
1.1       cgd        48:
1.14      mycroft    49: #include <sys/param.h>
                     50: #include <sys/systm.h>
                     51: #include <sys/kernel.h>
1.23      mycroft    52: #include <sys/vmmeter.h>
1.14      mycroft    53: #include <sys/types.h>
                     54: #include <sys/time.h>
                     55: #include <sys/proc.h>
1.23      mycroft    56: #include <sys/vnode.h>
                     57: #include <sys/malloc.h>
1.14      mycroft    58: #include <sys/file.h>
                     59: #include <sys/stat.h>
                     60: #include <sys/mount.h>
                     61: #include <sys/namei.h>
                     62: #include <sys/buf.h>
1.23      mycroft    63: #include <sys/dirent.h>
1.28      mycroft    64: #include <sys/msgbuf.h>
1.44      mycroft    65:
                     66: #include <miscfs/genfs/genfs.h>
1.17      cgd        67: #include <miscfs/kernfs/kernfs.h>
1.1       cgd        68:
1.54      mrg        69: #if defined(UVM)
                     70: #include <vm/vm.h>
                     71: #include <uvm/uvm_extern.h>
                     72: #endif
                     73:
1.17      cgd        74: #define KSTRING        256             /* Largest I/O available via this filesystem */
                     75: #define        UIO_MX 32
1.1       cgd        76:
1.23      mycroft    77: #define        READ_MODE       (S_IRUSR|S_IRGRP|S_IROTH)
                     78: #define        WRITE_MODE      (S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)
                     79: #define DIR_MODE       (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
                     80:
1.50      pk         81: struct kern_target kern_targets[] = {
1.1       cgd        82: /* NOTE: The name must be less than UIO_MX-16 chars in length */
1.23      mycroft    83: #define N(s) sizeof(s)-1, s
                     84:      /*        name            data          tag           type  ro/rw */
                     85:      { DT_DIR, N("."),         0,            KTT_NULL,     VDIR, DIR_MODE   },
                     86:      { DT_DIR, N(".."),        0,            KTT_NULL,     VDIR, DIR_MODE   },
                     87:      { DT_REG, N("boottime"),  &boottime.tv_sec, KTT_INT,  VREG, READ_MODE  },
                     88:      { DT_REG, N("copyright"), copyright,    KTT_STRING,   VREG, READ_MODE  },
                     89:      { DT_REG, N("hostname"),  0,            KTT_HOSTNAME, VREG, WRITE_MODE },
                     90:      { DT_REG, N("hz"),        &hz,          KTT_INT,      VREG, READ_MODE  },
                     91:      { DT_REG, N("loadavg"),   0,            KTT_AVENRUN,  VREG, READ_MODE  },
1.28      mycroft    92:      { DT_REG, N("msgbuf"),    0,           KTT_MSGBUF,   VREG, READ_MODE  },
1.54      mrg        93: #if defined(UVM)
                     94:      { DT_REG, N("pagesize"),  &uvmexp.pagesize, KTT_INT,  VREG, READ_MODE  },
                     95: #else
1.23      mycroft    96:      { DT_REG, N("pagesize"),  &cnt.v_page_size, KTT_INT,  VREG, READ_MODE  },
1.54      mrg        97: #endif
1.23      mycroft    98:      { DT_REG, N("physmem"),   &physmem,     KTT_INT,      VREG, READ_MODE  },
1.17      cgd        99: #if 0
1.23      mycroft   100:      { DT_DIR, N("root"),      0,            KTT_NULL,     VDIR, DIR_MODE   },
1.17      cgd       101: #endif
1.23      mycroft   102:      { DT_BLK, N("rootdev"),   &rootdev,     KTT_DEVICE,   VBLK, READ_MODE  },
                    103:      { DT_CHR, N("rrootdev"),  &rrootdev,    KTT_DEVICE,   VCHR, READ_MODE  },
                    104:      { DT_REG, N("time"),      0,            KTT_TIME,     VREG, READ_MODE  },
                    105:      { DT_REG, N("version"),   version,      KTT_STRING,   VREG, READ_MODE  },
                    106: #undef N
1.1       cgd       107: };
1.17      cgd       108: static int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
1.1       cgd       109:
1.41      christos  110: int    kernfs_lookup   __P((void *));
1.44      mycroft   111: #define        kernfs_create   genfs_eopnotsupp
                    112: #define        kernfs_mknod    genfs_eopnotsupp
                    113: #define        kernfs_open     genfs_nullop
                    114: #define        kernfs_close    genfs_nullop
1.41      christos  115: int    kernfs_access   __P((void *));
                    116: int    kernfs_getattr  __P((void *));
                    117: int    kernfs_setattr  __P((void *));
                    118: int    kernfs_read     __P((void *));
                    119: int    kernfs_write    __P((void *));
1.44      mycroft   120: #define        kernfs_ioctl    genfs_eopnotsupp
1.45      mycroft   121: #define        kernfs_poll     genfs_poll
1.44      mycroft   122: #define        kernfs_mmap     genfs_eopnotsupp
                    123: #define        kernfs_fsync    genfs_nullop
                    124: #define        kernfs_seek     genfs_nullop
                    125: #define        kernfs_remove   genfs_eopnotsupp
1.41      christos  126: int    kernfs_link     __P((void *));
1.44      mycroft   127: #define        kernfs_rename   genfs_eopnotsupp
                    128: #define        kernfs_mkdir    genfs_eopnotsupp
                    129: #define        kernfs_rmdir    genfs_eopnotsupp
1.41      christos  130: int    kernfs_symlink  __P((void *));
                    131: int    kernfs_readdir  __P((void *));
1.44      mycroft   132: #define        kernfs_readlink genfs_eopnotsupp
                    133: #define        kernfs_abortop  genfs_abortop
1.41      christos  134: int    kernfs_inactive __P((void *));
                    135: int    kernfs_reclaim  __P((void *));
1.44      mycroft   136: #define        kernfs_lock     genfs_nullop
                    137: #define        kernfs_unlock   genfs_nullop
                    138: #define        kernfs_bmap     genfs_badop
                    139: #define        kernfs_strategy genfs_badop
1.41      christos  140: int    kernfs_print    __P((void *));
1.44      mycroft   141: #define        kernfs_islocked genfs_nullop
1.41      christos  142: int    kernfs_pathconf __P((void *));
1.44      mycroft   143: #define        kernfs_advlock  genfs_eopnotsupp
                    144: #define        kernfs_blkatoff genfs_eopnotsupp
                    145: #define        kernfs_valloc   genfs_eopnotsupp
                    146: #define        kernfs_vfree    genfs_nullop
                    147: #define        kernfs_truncate genfs_eopnotsupp
                    148: #define        kernfs_update   genfs_nullop
                    149: #define        kernfs_bwrite   genfs_eopnotsupp
1.41      christos  150:
                    151: int    kernfs_xread __P((struct kern_target *, int, char **, int));
                    152: int    kernfs_xwrite __P((struct kern_target *, char *, int));
                    153:
                    154: int (**kernfs_vnodeop_p) __P((void *));
                    155: struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
                    156:        { &vop_default_desc, vn_default_error },
1.44      mycroft   157:        { &vop_lookup_desc, kernfs_lookup },            /* lookup */
                    158:        { &vop_create_desc, kernfs_create },            /* create */
                    159:        { &vop_mknod_desc, kernfs_mknod },              /* mknod */
                    160:        { &vop_open_desc, kernfs_open },                /* open */
                    161:        { &vop_close_desc, kernfs_close },              /* close */
                    162:        { &vop_access_desc, kernfs_access },            /* access */
                    163:        { &vop_getattr_desc, kernfs_getattr },          /* getattr */
                    164:        { &vop_setattr_desc, kernfs_setattr },          /* setattr */
                    165:        { &vop_read_desc, kernfs_read },                /* read */
                    166:        { &vop_write_desc, kernfs_write },              /* write */
                    167:        { &vop_ioctl_desc, kernfs_ioctl },              /* ioctl */
1.45      mycroft   168:        { &vop_poll_desc, kernfs_poll },                /* poll */
1.44      mycroft   169:        { &vop_mmap_desc, kernfs_mmap },                /* mmap */
                    170:        { &vop_fsync_desc, kernfs_fsync },              /* fsync */
                    171:        { &vop_seek_desc, kernfs_seek },                /* seek */
                    172:        { &vop_remove_desc, kernfs_remove },            /* remove */
                    173:        { &vop_link_desc, kernfs_link },                /* link */
                    174:        { &vop_rename_desc, kernfs_rename },            /* rename */
                    175:        { &vop_mkdir_desc, kernfs_mkdir },              /* mkdir */
                    176:        { &vop_rmdir_desc, kernfs_rmdir },              /* rmdir */
                    177:        { &vop_symlink_desc, kernfs_symlink },          /* symlink */
                    178:        { &vop_readdir_desc, kernfs_readdir },          /* readdir */
                    179:        { &vop_readlink_desc, kernfs_readlink },        /* readlink */
                    180:        { &vop_abortop_desc, kernfs_abortop },          /* abortop */
                    181:        { &vop_inactive_desc, kernfs_inactive },        /* inactive */
                    182:        { &vop_reclaim_desc, kernfs_reclaim },          /* reclaim */
                    183:        { &vop_lock_desc, kernfs_lock },                /* lock */
                    184:        { &vop_unlock_desc, kernfs_unlock },            /* unlock */
                    185:        { &vop_bmap_desc, kernfs_bmap },                /* bmap */
                    186:        { &vop_strategy_desc, kernfs_strategy },        /* strategy */
                    187:        { &vop_print_desc, kernfs_print },              /* print */
                    188:        { &vop_islocked_desc, kernfs_islocked },        /* islocked */
                    189:        { &vop_pathconf_desc, kernfs_pathconf },        /* pathconf */
                    190:        { &vop_advlock_desc, kernfs_advlock },          /* advlock */
                    191:        { &vop_blkatoff_desc, kernfs_blkatoff },        /* blkatoff */
                    192:        { &vop_valloc_desc, kernfs_valloc },            /* valloc */
                    193:        { &vop_vfree_desc, kernfs_vfree },              /* vfree */
                    194:        { &vop_truncate_desc, kernfs_truncate },        /* truncate */
                    195:        { &vop_update_desc, kernfs_update },            /* update */
                    196:        { &vop_bwrite_desc, kernfs_bwrite },            /* bwrite */
1.41      christos  197:        { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
                    198: };
                    199: struct vnodeopv_desc kernfs_vnodeop_opv_desc =
                    200:        { &kernfs_vnodeop_p, kernfs_vnodeop_entries };
                    201:
1.28      mycroft   202: int
                    203: kernfs_xread(kt, off, bufp, len)
1.17      cgd       204:        struct kern_target *kt;
1.28      mycroft   205:        int off;
                    206:        char **bufp;
1.1       cgd       207:        int len;
                    208: {
                    209:
                    210:        switch (kt->kt_tag) {
                    211:        case KTT_TIME: {
                    212:                struct timeval tv;
1.28      mycroft   213:
1.1       cgd       214:                microtime(&tv);
1.47      christos  215:                sprintf(*bufp, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
1.1       cgd       216:                break;
                    217:        }
                    218:
                    219:        case KTT_INT: {
                    220:                int *ip = kt->kt_data;
1.28      mycroft   221:
1.47      christos  222:                sprintf(*bufp, "%d\n", *ip);
1.1       cgd       223:                break;
                    224:        }
                    225:
                    226:        case KTT_STRING: {
                    227:                char *cp = kt->kt_data;
                    228:
1.28      mycroft   229:                *bufp = cp;
                    230:                break;
                    231:        }
1.1       cgd       232:
1.28      mycroft   233:        case KTT_MSGBUF: {
                    234:                long n;
                    235:
1.52      leo       236:                /*
                    237:                 * deal with cases where the message buffer has
                    238:                 * become corrupted.
                    239:                 */
                    240:                if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
                    241:                        msgbufenabled = 0;
                    242:                        return (ENXIO);
                    243:                }
                    244:
                    245:                /*
                    246:                 * Note that reads of /kern/msgbuf won't necessarily yield
                    247:                 * consistent results, if the message buffer is modified
                    248:                 * while the read is in progress.  The worst that can happen
                    249:                 * is that incorrect data will be read.  There's no way
                    250:                 * that this can crash the system unless the values in the
                    251:                 * message buffer header are corrupted, but that'll cause
                    252:                 * the system to die anyway.
                    253:                 */
                    254:                if (off >= msgbufp->msg_bufs)
1.28      mycroft   255:                        return (0);
                    256:                n = msgbufp->msg_bufx + off;
1.52      leo       257:                if (n >= msgbufp->msg_bufs)
                    258:                        n -= msgbufp->msg_bufs;
                    259:                len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
1.28      mycroft   260:                *bufp = msgbufp->msg_bufc + n;
                    261:                return (len);
1.1       cgd       262:        }
                    263:
                    264:        case KTT_HOSTNAME: {
                    265:                char *cp = hostname;
                    266:                int xlen = hostnamelen;
                    267:
1.17      cgd       268:                if (xlen >= (len-2))
1.1       cgd       269:                        return (EINVAL);
                    270:
1.28      mycroft   271:                bcopy(cp, *bufp, xlen);
                    272:                (*bufp)[xlen] = '\n';
                    273:                (*bufp)[xlen+1] = '\0';
1.1       cgd       274:                break;
                    275:        }
                    276:
                    277:        case KTT_AVENRUN:
1.31      mycroft   278:                averunnable.fscale = FSCALE;
1.47      christos  279:                sprintf(*bufp, "%d %d %d %ld\n",
1.23      mycroft   280:                    averunnable.ldavg[0], averunnable.ldavg[1],
                    281:                    averunnable.ldavg[2], averunnable.fscale);
1.1       cgd       282:                break;
                    283:
                    284:        default:
1.28      mycroft   285:                return (0);
1.1       cgd       286:        }
                    287:
1.28      mycroft   288:        len = strlen(*bufp);
                    289:        if (len <= off)
                    290:                return (0);
                    291:        *bufp += off;
                    292:        return (len - off);
1.1       cgd       293: }
                    294:
1.28      mycroft   295: int
1.1       cgd       296: kernfs_xwrite(kt, buf, len)
1.17      cgd       297:        struct kern_target *kt;
1.1       cgd       298:        char *buf;
                    299:        int len;
                    300: {
1.23      mycroft   301:
1.1       cgd       302:        switch (kt->kt_tag) {
1.23      mycroft   303:        case KTT_HOSTNAME:
1.1       cgd       304:                if (buf[len-1] == '\n')
                    305:                        --len;
                    306:                bcopy(buf, hostname, len);
1.17      cgd       307:                hostname[len] = '\0';
1.6       cgd       308:                hostnamelen = len;
1.1       cgd       309:                return (0);
                    310:
                    311:        default:
                    312:                return (EIO);
                    313:        }
                    314: }
                    315:
1.17      cgd       316:
                    317: /*
1.1       cgd       318:  * vp is the current namei directory
                    319:  * ndp is the name to locate in that directory...
                    320:  */
1.41      christos  321: int
                    322: kernfs_lookup(v)
                    323:        void *v;
                    324: {
1.23      mycroft   325:        struct vop_lookup_args /* {
                    326:                struct vnode * a_dvp;
                    327:                struct vnode ** a_vpp;
                    328:                struct componentname * a_cnp;
1.41      christos  329:        } */ *ap = v;
1.23      mycroft   330:        struct componentname *cnp = ap->a_cnp;
                    331:        struct vnode **vpp = ap->a_vpp;
                    332:        struct vnode *dvp = ap->a_dvp;
1.48      cgd       333:        const char *pname = cnp->cn_nameptr;
1.23      mycroft   334:        struct kern_target *kt;
1.1       cgd       335:        struct vnode *fvp;
1.23      mycroft   336:        int error, i;
1.1       cgd       337:
                    338: #ifdef KERNFS_DIAGNOSTIC
1.51      christos  339:        printf("kernfs_lookup(%p)\n", ap);
                    340:        printf("kernfs_lookup(dp = %p, vpp = %p, cnp = %p)\n", dvp, vpp, ap->a_cnp);
1.47      christos  341:        printf("kernfs_lookup(%s)\n", pname);
1.1       cgd       342: #endif
1.23      mycroft   343:
1.35      mycroft   344:        *vpp = NULLVP;
                    345:
                    346:        if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
                    347:                return (EROFS);
                    348:
1.23      mycroft   349:        if (cnp->cn_namelen == 1 && *pname == '.') {
                    350:                *vpp = dvp;
1.1       cgd       351:                VREF(dvp);
                    352:                /*VOP_LOCK(dvp);*/
                    353:                return (0);
                    354:        }
1.13      cgd       355:
1.17      cgd       356: #if 0
1.23      mycroft   357:        if (cnp->cn_namelen == 4 && bcmp(pname, "root", 4) == 0) {
                    358:                *vpp = rootdir;
1.17      cgd       359:                VREF(rootdir);
1.1       cgd       360:                VOP_LOCK(rootdir);
                    361:                return (0);
                    362:        }
1.13      cgd       363: #endif
1.25      mycroft   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.35      mycroft   367:                    bcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
                    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.23      mycroft   381:                if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
1.24      mycroft   382:                        return (ENOENT);
1.23      mycroft   383:                *vpp = fvp;
1.24      mycroft   384:                if (vget(fvp, 1))
                    385:                        goto loop;
1.23      mycroft   386:                return (0);
                    387:        }
1.1       cgd       388:
                    389: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  390:        printf("kernfs_lookup: allocate new vnode\n");
1.1       cgd       391: #endif
1.41      christos  392:        error = getnewvnode(VT_KERNFS, dvp->v_mount, kernfs_vnodeop_p, &fvp);
                    393:        if (error)
1.23      mycroft   394:                return (error);
                    395:
                    396:        MALLOC(fvp->v_data, void *, sizeof(struct kernfs_node), M_TEMP,
                    397:            M_WAITOK);
                    398:        VTOKERN(fvp)->kf_kt = kt;
                    399:        fvp->v_type = kt->kt_vtype;
                    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:
                    448:        bzero((caddr_t) vap, sizeof(*vap));
                    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.39      mycroft   482:                vap->va_fileid = 3 + (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.53      fvdl      595:                off_t *a_cookies;
1.26      mycroft   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.53      fvdl      603:        off_t *cookies = ap->a_cookies;
1.37      mycroft   604:        int ncookies = ap->a_ncookies;
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.37      mycroft   616:        bzero((caddr_t)&d, UIO_MX);
                    617:        d.d_reclen = UIO_MX;
                    618:
1.23      mycroft   619:        for (kt = &kern_targets[i];
                    620:             uio->uio_resid >= UIO_MX && i < nkern_targets; kt++, i++) {
1.1       cgd       621: #ifdef KERNFS_DIAGNOSTIC
1.47      christos  622:                printf("kernfs_readdir: i = %d\n", i);
1.1       cgd       623: #endif
1.26      mycroft   624:
1.23      mycroft   625:                if (kt->kt_tag == KTT_DEVICE) {
1.26      mycroft   626:                        dev_t *dp = kt->kt_data;
1.23      mycroft   627:                        struct vnode *fvp;
1.26      mycroft   628:
1.23      mycroft   629:                        if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
                    630:                                continue;
                    631:                }
1.26      mycroft   632:
1.37      mycroft   633:                d.d_fileno = i + 3;
                    634:                d.d_namlen = kt->kt_namlen;
                    635:                bcopy(kt->kt_name, d.d_name, kt->kt_namlen + 1);
                    636:                d.d_type = kt->kt_type;
1.26      mycroft   637:
1.41      christos  638:                if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
1.1       cgd       639:                        break;
1.37      mycroft   640:                if (ncookies-- > 0)
1.38      mycroft   641:                        *cookies++ = i + 1;
1.1       cgd       642:        }
                    643:
1.38      mycroft   644:        uio->uio_offset = i;
1.1       cgd       645:        return (error);
                    646: }
                    647:
1.41      christos  648: int
                    649: kernfs_inactive(v)
                    650:        void *v;
                    651: {
1.23      mycroft   652:        struct vop_inactive_args /* {
                    653:                struct vnode *a_vp;
1.41      christos  654:        } */ *ap = v;
1.23      mycroft   655:        struct vnode *vp = ap->a_vp;
                    656:
                    657: #ifdef KERNFS_DIAGNOSTIC
1.51      christos  658:        printf("kernfs_inactive(%p)\n", vp);
1.23      mycroft   659: #endif
1.1       cgd       660:        /*
                    661:         * Clear out the v_type field to avoid
                    662:         * nasty things happening in vgone().
                    663:         */
                    664:        vp->v_type = VNON;
1.23      mycroft   665:        return (0);
                    666: }
                    667:
1.41      christos  668: int
                    669: kernfs_reclaim(v)
                    670:        void *v;
                    671: {
1.23      mycroft   672:        struct vop_reclaim_args /* {
                    673:                struct vnode *a_vp;
1.41      christos  674:        } */ *ap = v;
1.23      mycroft   675:        struct vnode *vp = ap->a_vp;
                    676:
1.1       cgd       677: #ifdef KERNFS_DIAGNOSTIC
1.51      christos  678:        printf("kernfs_reclaim(%p)\n", vp);
1.1       cgd       679: #endif
1.23      mycroft   680:        if (vp->v_data) {
                    681:                FREE(vp->v_data, M_TEMP);
                    682:                vp->v_data = 0;
                    683:        }
1.1       cgd       684:        return (0);
                    685: }
                    686:
                    687: /*
1.23      mycroft   688:  * Return POSIX pathconf information applicable to special devices.
                    689:  */
1.41      christos  690: int
                    691: kernfs_pathconf(v)
                    692:        void *v;
                    693: {
1.23      mycroft   694:        struct vop_pathconf_args /* {
                    695:                struct vnode *a_vp;
                    696:                int a_name;
1.29      cgd       697:                register_t *a_retval;
1.41      christos  698:        } */ *ap = v;
1.23      mycroft   699:
                    700:        switch (ap->a_name) {
                    701:        case _PC_LINK_MAX:
                    702:                *ap->a_retval = LINK_MAX;
                    703:                return (0);
                    704:        case _PC_MAX_CANON:
                    705:                *ap->a_retval = MAX_CANON;
                    706:                return (0);
                    707:        case _PC_MAX_INPUT:
                    708:                *ap->a_retval = MAX_INPUT;
                    709:                return (0);
                    710:        case _PC_PIPE_BUF:
                    711:                *ap->a_retval = PIPE_BUF;
                    712:                return (0);
                    713:        case _PC_CHOWN_RESTRICTED:
                    714:                *ap->a_retval = 1;
                    715:                return (0);
                    716:        case _PC_VDISABLE:
                    717:                *ap->a_retval = _POSIX_VDISABLE;
                    718:                return (0);
                    719:        default:
                    720:                return (EINVAL);
                    721:        }
                    722:        /* NOTREACHED */
                    723: }
                    724:
                    725: /*
                    726:  * Print out the contents of a /dev/fd vnode.
1.1       cgd       727:  */
                    728: /* ARGSUSED */
1.41      christos  729: int
                    730: kernfs_print(v)
                    731:        void *v;
1.23      mycroft   732: {
                    733:
1.47      christos  734:        printf("tag VT_KERNFS, kernfs vnode\n");
1.23      mycroft   735:        return (0);
                    736: }
                    737:
1.40      mycroft   738: int
1.41      christos  739: kernfs_link(v)
                    740:        void *v;
                    741: {
1.40      mycroft   742:        struct vop_link_args /* {
                    743:                struct vnode *a_dvp;
                    744:                struct vnode *a_vp;
                    745:                struct componentname *a_cnp;
1.41      christos  746:        } */ *ap = v;
1.40      mycroft   747:
                    748:        VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
                    749:        vput(ap->a_dvp);
                    750:        return (EROFS);
                    751: }
                    752:
                    753: int
1.41      christos  754: kernfs_symlink(v)
                    755:        void *v;
                    756: {
1.40      mycroft   757:        struct vop_symlink_args /* {
                    758:                struct vnode *a_dvp;
                    759:                struct vnode **a_vpp;
                    760:                struct componentname *a_cnp;
                    761:                struct vattr *a_vap;
                    762:                char *a_target;
1.41      christos  763:        } */ *ap = v;
1.40      mycroft   764:
                    765:        VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
                    766:        vput(ap->a_dvp);
                    767:        return (EROFS);
1.1       cgd       768: }

CVSweb <webmaster@jp.NetBSD.org>