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

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

CVSweb <webmaster@jp.NetBSD.org>