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

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

CVSweb <webmaster@jp.NetBSD.org>