Annotation of Net2/sys/buf.h, revision 1.1.1.2

1.1       root        1: /*
                      2:  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by the University of
                     16:  *     California, Berkeley and its contributors.
                     17:  * 4. Neither the name of the University nor the names of its contributors
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  *
                     33:  *     @(#)buf.h       7.11 (Berkeley) 5/9/90
1.1.1.2 ! root       34:  *
        !            35:  * PATCHES MAGIC                LEVEL   PATCH THAT GOT US HERE
        !            36:  * --------------------         -----   ----------------------
        !            37:  * CURRENT PATCH LEVEL:         1       00007
        !            38:  * --------------------         -----   ----------------------
        !            39:  *
        !            40:  * 20 Aug 92   David Greenman          Fixed buffer cache hash index
        !            41:  *                                     calculation
1.1       root       42:  */
                     43: 
                     44: /*
                     45:  * The header for buffers in the buffer pool and otherwise used
                     46:  * to describe a block i/o request is given here.
                     47:  *
                     48:  * Each buffer in the pool is usually doubly linked into 2 lists:
                     49:  * hashed into a chain by <dev,blkno> so it can be located in the cache,
                     50:  * and (usually) on (one of several) queues.  These lists are circular and
                     51:  * doubly linked for easy removal.
                     52:  *
                     53:  * There are currently three queues for buffers:
                     54:  *     one for buffers which must be kept permanently (super blocks)
                     55:  *     one for buffers containing ``useful'' information (the cache)
                     56:  *     one for buffers containing ``non-useful'' information
                     57:  *             (and empty buffers, pushed onto the front)
                     58:  * The latter two queues contain the buffers which are available for
                     59:  * reallocation, are kept in lru order.  When not on one of these queues,
                     60:  * the buffers are ``checked out'' to drivers which use the available list
                     61:  * pointers to keep track of them in their i/o active queues.
                     62:  */
                     63: 
                     64: /*
                     65:  * Bufhd structures used at the head of the hashed buffer queues.
                     66:  * We only need three words for these, so this abbreviated
                     67:  * definition saves some space.
                     68:  */
                     69: struct bufhd
                     70: {
                     71:        long    b_flags;                /* see defines below */
                     72:        struct  buf *b_forw, *b_back;   /* fwd/bkwd pointer in chain */
                     73: };
                     74: struct buf
                     75: {
                     76:        long    b_flags;                /* too much goes here to describe */
                     77:        struct  buf *b_forw, *b_back;   /* hash chain (2 way street) */
                     78:        struct  buf *av_forw, *av_back; /* position on free list if not BUSY */
                     79:        struct  buf *b_blockf, **b_blockb;/* associated vnode */
                     80: #define        b_actf  av_forw                 /* alternate names for driver queue */
                     81: #define        b_actl  av_back                 /*    head - isn't history wonderful */
                     82:        long    b_bcount;               /* transfer count */
                     83:        long    b_bufsize;              /* size of allocated buffer */
                     84: #define        b_active b_bcount               /* driver queue head: drive active */
                     85:        short   b_error;                /* returned after I/O */
                     86:        dev_t   b_dev;                  /* major+minor device name */
                     87:        union {
                     88:            caddr_t b_addr;             /* low order core address */
                     89:            int *b_words;               /* words for clearing */
                     90:            struct fs *b_fs;            /* superblocks */
                     91:            struct csum *b_cs;          /* superblock summary information */
                     92:            struct cg *b_cg;            /* cylinder group block */
                     93:            struct dinode *b_dino;      /* ilist */
                     94:            daddr_t *b_daddr;           /* indirect block */
                     95:        } b_un;
                     96:        daddr_t b_lblkno;               /* logical block number */
                     97:        daddr_t b_blkno;                /* block # on device */
                     98:        long    b_resid;                /* words not transferred after error */
                     99: #define        b_errcnt b_resid                /* while i/o in progress: # retries */
                    100:        struct  proc *b_proc;           /* proc doing physical or swap I/O */
                    101:        int     (*b_iodone)();          /* function called by iodone */
                    102:        struct  vnode *b_vp;            /* vnode for dev */
                    103:        int     b_pfcent;               /* center page when swapping cluster */
                    104:        struct  ucred *b_rcred;         /* ref to read credentials */
                    105:        struct  ucred *b_wcred;         /* ref to write credendtials */
                    106:        int     b_dirtyoff;             /* offset in buffer of dirty region */
                    107:        int     b_dirtyend;             /* offset of end of dirty region */
                    108:        caddr_t b_saveaddr;             /* original b_addr for PHYSIO */
                    109: };
                    110: 
                    111: #define        BQUEUES         4               /* number of free buffer queues */
                    112: 
                    113: #define        BQ_LOCKED       0               /* super-blocks &c */
                    114: #define        BQ_LRU          1               /* lru, useful buffers */
                    115: #define        BQ_AGE          2               /* rubbish */
                    116: #define        BQ_EMPTY        3               /* buffer headers with no memory */
                    117: 
                    118: #ifdef KERNEL
                    119: #define        BUFHSZ  512
                    120: #define RND    (MAXBSIZE/DEV_BSIZE)
1.1.1.2 ! root      121: /* 20 Aug 92   BUFHASH*/
1.1       root      122: #if    ((BUFHSZ&(BUFHSZ-1)) == 0)
                    123: #define        BUFHASH(dvp, dblkno)    \
1.1.1.2 ! root      124:        ((struct buf *)&bufhash[((int)(dvp)/sizeof(struct vnode)+(int)(dblkno))&(BUFHSZ-1)])
1.1       root      125: #else
                    126: #define        BUFHASH(dvp, dblkno)    \
1.1.1.2 ! root      127:        ((struct buf *)&bufhash[((int)(dvp)/sizeof(struct vnode)+(int)(dblkno)) % BUFHSZ])
1.1       root      128: #endif
                    129: 
                    130: struct buf *buf;               /* the buffer pool itself */
                    131: char   *buffers;
                    132: int    nbuf;                   /* number of buffer headers */
                    133: int    bufpages;               /* number of memory pages in the buffer pool */
                    134: struct buf *swbuf;             /* swap I/O headers */
                    135: int    nswbuf;
                    136: struct bufhd bufhash[BUFHSZ];  /* heads of hash lists */
                    137: struct buf bfreelist[BQUEUES]; /* heads of available lists */
                    138: struct buf bswlist;            /* head of free swap header list */
                    139: struct buf *bclnlist;          /* head of cleaned page list */
                    140: 
1.1.1.2 ! root      141: void bufinit();
        !           142: int bread(struct vnode *, daddr_t, int, struct ucred *, struct buf **);
        !           143: int breada(struct vnode *, daddr_t, int, daddr_t, int, struct ucred *,
        !           144:        struct buf **);
        !           145: int bwrite(struct buf *);
        !           146: void bawrite(struct buf *);
        !           147: void bdwrite(struct buf *);
        !           148: void brelse(struct buf *);
        !           149: struct buf *incore(struct vnode *, daddr_t);
        !           150: struct buf *getblk(struct vnode *, daddr_t, int);
        !           151: struct buf *geteblk(int);
        !           152: int biowait(struct buf *);
        !           153: int biodone(struct buf *);
        !           154: void allocbuf(struct buf *, int);
1.1       root      155: 
                    156: #endif
                    157: 
                    158: /*
                    159:  * These flags are kept in b_flags.
                    160:  */
                    161: #define        B_WRITE         0x000000        /* non-read pseudo-flag */
                    162: #define        B_READ          0x000001        /* read when I/O occurs */
                    163: #define        B_DONE          0x000002        /* transaction finished */
                    164: #define        B_ERROR         0x000004        /* transaction aborted */
                    165: #define        B_BUSY          0x000008        /* not on av_forw/back list */
                    166: #define        B_PHYS          0x000010        /* physical IO */
                    167: #define        B_XXX           0x000020        /* was B_MAP, alloc UNIBUS on pdp-11 */
                    168: #define        B_WANTED        0x000040        /* issue wakeup when BUSY goes off */
                    169: #define        B_AGE           0x000080        /* delayed write for correct aging */
                    170: #define        B_ASYNC         0x000100        /* don't wait for I/O completion */
                    171: #define        B_DELWRI        0x000200        /* write at exit of avail list */
                    172: #define        B_TAPE          0x000400        /* this is a magtape (no bdwrite) */
1.1.1.2 ! root      173: #define        B_VMPAGE        0x000800        /* buffer from virtual memory */
        !           174: #define        B_MALLOC        0x001000        /* buffer from malloc space */
1.1       root      175: #define        B_DIRTY         0x002000        /* dirty page to be pushed out async */
                    176: #define        B_PGIN          0x004000        /* pagein op, so swap() can count it */
                    177: #define        B_CACHE         0x008000        /* did bread find us in the cache ? */
                    178: #define        B_INVAL         0x010000        /* does not contain valid info  */
                    179: #define        B_LOCKED        0x020000        /* locked in core (not reusable) */
                    180: #define        B_HEAD          0x040000        /* a buffer header, not a buffer */
                    181: #define        B_BAD           0x100000        /* bad block revectoring in progress */
                    182: #define        B_CALL          0x200000        /* call b_iodone from iodone */
                    183: #define        B_RAW           0x400000        /* set by physio for raw transfers */
                    184: #define        B_NOCACHE       0x800000        /* do not cache block after use */
                    185: 
                    186: /*
                    187:  * Insq/Remq for the buffer hash lists.
                    188:  */
                    189: #define        bremhash(bp) { \
                    190:        (bp)->b_back->b_forw = (bp)->b_forw; \
                    191:        (bp)->b_forw->b_back = (bp)->b_back; \
                    192: }
                    193: #define        binshash(bp, dp) { \
                    194:        (bp)->b_forw = (dp)->b_forw; \
                    195:        (bp)->b_back = (dp); \
                    196:        (dp)->b_forw->b_back = (bp); \
                    197:        (dp)->b_forw = (bp); \
                    198: }
                    199: 
                    200: /*
                    201:  * Insq/Remq for the buffer free lists.
                    202:  */
                    203: #define        bremfree(bp) { \
                    204:        (bp)->av_back->av_forw = (bp)->av_forw; \
                    205:        (bp)->av_forw->av_back = (bp)->av_back; \
                    206: }
                    207: #define        binsheadfree(bp, dp) { \
                    208:        (dp)->av_forw->av_back = (bp); \
                    209:        (bp)->av_forw = (dp)->av_forw; \
                    210:        (dp)->av_forw = (bp); \
                    211:        (bp)->av_back = (dp); \
                    212: }
                    213: #define        binstailfree(bp, dp) { \
                    214:        (dp)->av_back->av_forw = (bp); \
                    215:        (bp)->av_back = (dp)->av_back; \
                    216:        (dp)->av_back = (bp); \
                    217:        (bp)->av_forw = (dp); \
                    218: }
                    219: 
                    220: #define        iodone  biodone
                    221: #define        iowait  biowait
                    222: 
                    223: /*
                    224:  * Zero out a buffer's data portion.
                    225:  */
                    226: #define        clrbuf(bp) { \
1.1.1.2 ! root      227:        bzero((bp)->b_un.b_addr, (unsigned)(bp)->b_bcount); \
1.1       root      228:        (bp)->b_resid = 0; \
                    229: }
                    230: #define B_CLRBUF       0x1     /* request allocated buffer be cleared */
                    231: #define B_SYNC         0x2     /* do all allocations synchronously */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.