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

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

CVSweb <webmaster@jp.NetBSD.org>