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

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

CVSweb <webmaster@jp.NetBSD.org>