cache.c

Go to the documentation of this file.
00001 /* The file system maintains a buffer cache to reduce the number of disk
00002  * accesses needed.  Whenever a read or write to the disk is done, a check is
00003  * first made to see if the block is in the cache.  This file manages the
00004  * cache.
00005  *
00006  * The entry points into this file are:
00007  *   get_block:   request to fetch a block for reading or writing from cache
00008  *   put_block:   return a block previously requested with get_block
00009  *   alloc_zone:  allocate a new zone (to increase the length of a file)
00010  *   free_zone:   release a zone (when a file is removed)
00011  *   invalidate:  remove all the cache blocks on some device
00012  *
00013  * Private functions:
00014  *   rw_block:    read or write a block from the disk itself
00015  */
00016 
00017 #include "fs.h"
00018 #include <minix/com.h>
00019 #include "buf.h"
00020 #include "file.h"
00021 #include "fproc.h"
00022 #include "super.h"
00023 
00024 FORWARD _PROTOTYPE( void rm_lru, (struct buf *bp) );
00025 FORWARD _PROTOTYPE( int rw_block, (struct buf *, int) );
00026 
00027 /*===========================================================================*
00028  *                              get_block                                    *
00029  *===========================================================================*/
00030 PUBLIC struct buf *get_block(dev, block, only_search)
00031 register dev_t dev;             /* on which device is the block? */
00032 register block_t block;         /* which block is wanted? */
00033 int only_search;                /* if NO_READ, don't read, else act normal */
00034 {
00035 /* Check to see if the requested block is in the block cache.  If so, return
00036  * a pointer to it.  If not, evict some other block and fetch it (unless
00037  * 'only_search' is 1).  All the blocks in the cache that are not in use
00038  * are linked together in a chain, with 'front' pointing to the least recently
00039  * used block and 'rear' to the most recently used block.  If 'only_search' is
00040  * 1, the block being requested will be overwritten in its entirety, so it is
00041  * only necessary to see if it is in the cache; if it is not, any free buffer
00042  * will do.  It is not necessary to actually read the block in from disk.
00043  * If 'only_search' is PREFETCH, the block need not be read from the disk,
00044  * and the device is not to be marked on the block, so callers can tell if
00045  * the block returned is valid.
00046  * In addition to the LRU chain, there is also a hash chain to link together
00047  * blocks whose block numbers end with the same bit strings, for fast lookup.
00048  */
00049 
00050   int b;
00051   register struct buf *bp, *prev_ptr;
00052 
00053   /* Search the hash chain for (dev, block). Do_read() can use 
00054    * get_block(NO_DEV ...) to get an unnamed block to fill with zeros when
00055    * someone wants to read from a hole in a file, in which case this search
00056    * is skipped
00057    */
00058   if (dev != NO_DEV) {
00059         b = (int) block & HASH_MASK;
00060         bp = buf_hash[b];
00061         while (bp != NIL_BUF) {
00062                 if (bp->b_blocknr == block && bp->b_dev == dev) {
00063                         /* Block needed has been found. */
00064                         if (bp->b_count == 0) rm_lru(bp);
00065                         bp->b_count++;  /* record that block is in use */
00066 
00067                         return(bp);
00068                 } else {
00069                         /* This block is not the one sought. */
00070                         bp = bp->b_hash; /* move to next block on hash chain */
00071                 }
00072         }
00073   }
00074 
00075   /* Desired block is not on available chain.  Take oldest block ('front'). */
00076   if ((bp = front) == NIL_BUF) panic(__FILE__,"all buffers in use", NR_BUFS);
00077   rm_lru(bp);
00078 
00079   /* Remove the block that was just taken from its hash chain. */
00080   b = (int) bp->b_blocknr & HASH_MASK;
00081   prev_ptr = buf_hash[b];
00082   if (prev_ptr == bp) {
00083         buf_hash[b] = bp->b_hash;
00084   } else {
00085         /* The block just taken is not on the front of its hash chain. */
00086         while (prev_ptr->b_hash != NIL_BUF)
00087                 if (prev_ptr->b_hash == bp) {
00088                         prev_ptr->b_hash = bp->b_hash;  /* found it */
00089                         break;
00090                 } else {
00091                         prev_ptr = prev_ptr->b_hash;    /* keep looking */
00092                 }
00093   }
00094 
00095   /* If the block taken is dirty, make it clean by writing it to the disk.
00096    * Avoid hysteresis by flushing all other dirty blocks for the same device.
00097    */
00098   if (bp->b_dev != NO_DEV) {
00099         if (bp->b_dirt == DIRTY) flushall(bp->b_dev);
00100 #if ENABLE_CACHE2
00101         put_block2(bp);
00102 #endif
00103   }
00104 
00105   /* Fill in block's parameters and add it to the hash chain where it goes. */
00106   bp->b_dev = dev;              /* fill in device number */
00107   bp->b_blocknr = block;        /* fill in block number */
00108   bp->b_count++;                /* record that block is being used */
00109   b = (int) bp->b_blocknr & HASH_MASK;
00110   bp->b_hash = buf_hash[b];
00111   buf_hash[b] = bp;             /* add to hash list */
00112 
00113   /* Go get the requested block unless searching or prefetching. */
00114   if (dev != NO_DEV) {
00115 #if ENABLE_CACHE2
00116         if (get_block2(bp, only_search)) /* in 2nd level cache */;
00117         else
00118 #endif
00119         if (only_search == PREFETCH) bp->b_dev = NO_DEV;
00120         else
00121         if (only_search == NORMAL) {
00122                 rw_block(bp, READING);
00123         }
00124   }
00125   return(bp);                   /* return the newly acquired block */
00126 }
00127 
00128 /*===========================================================================*
00129  *                              put_block                                    *
00130  *===========================================================================*/
00131 PUBLIC void put_block(bp, block_type)
00132 register struct buf *bp;        /* pointer to the buffer to be released */
00133 int block_type;                 /* INODE_BLOCK, DIRECTORY_BLOCK, or whatever */
00134 {
00135 /* Return a block to the list of available blocks.   Depending on 'block_type'
00136  * it may be put on the front or rear of the LRU chain.  Blocks that are
00137  * expected to be needed again shortly (e.g., partially full data blocks)
00138  * go on the rear; blocks that are unlikely to be needed again shortly
00139  * (e.g., full data blocks) go on the front.  Blocks whose loss can hurt
00140  * the integrity of the file system (e.g., inode blocks) are written to
00141  * disk immediately if they are dirty.
00142  */
00143   if (bp == NIL_BUF) return;    /* it is easier to check here than in caller */
00144 
00145   bp->b_count--;                /* there is one use fewer now */
00146   if (bp->b_count != 0) return; /* block is still in use */
00147 
00148   bufs_in_use--;                /* one fewer block buffers in use */
00149 
00150   /* Put this block back on the LRU chain.  If the ONE_SHOT bit is set in
00151    * 'block_type', the block is not likely to be needed again shortly, so put
00152    * it on the front of the LRU chain where it will be the first one to be
00153    * taken when a free buffer is needed later.
00154    */
00155   if (bp->b_dev == DEV_RAM || (block_type & ONE_SHOT)) {
00156         /* Block probably won't be needed quickly. Put it on front of chain.
00157          * It will be the next block to be evicted from the cache.
00158          */
00159         bp->b_prev = NIL_BUF;
00160         bp->b_next = front;
00161         if (front == NIL_BUF)
00162                 rear = bp;      /* LRU chain was empty */
00163         else
00164                 front->b_prev = bp;
00165         front = bp;
00166   } else {
00167         /* Block probably will be needed quickly.  Put it on rear of chain.
00168          * It will not be evicted from the cache for a long time.
00169          */
00170         bp->b_prev = rear;
00171         bp->b_next = NIL_BUF;
00172         if (rear == NIL_BUF)
00173                 front = bp;
00174         else
00175                 rear->b_next = bp;
00176         rear = bp;
00177   }
00178 
00179   /* Some blocks are so important (e.g., inodes, indirect blocks) that they
00180    * should be written to the disk immediately to avoid messing up the file
00181    * system in the event of a crash.
00182    */
00183   if ((block_type & WRITE_IMMED) && bp->b_dirt==DIRTY && bp->b_dev != NO_DEV) {
00184                 rw_block(bp, WRITING);
00185   } 
00186 }
00187 
00188 /*===========================================================================*
00189  *                              alloc_zone                                   *
00190  *===========================================================================*/
00191 PUBLIC zone_t alloc_zone(dev, z)
00192 dev_t dev;                      /* device where zone wanted */
00193 zone_t z;                       /* try to allocate new zone near this one */
00194 {
00195 /* Allocate a new zone on the indicated device and return its number. */
00196 
00197   int major, minor;
00198   bit_t b, bit;
00199   struct super_block *sp;
00200 
00201   /* Note that the routine alloc_bit() returns 1 for the lowest possible
00202    * zone, which corresponds to sp->s_firstdatazone.  To convert a value
00203    * between the bit number, 'b', used by alloc_bit() and the zone number, 'z',
00204    * stored in the inode, use the formula:
00205    *     z = b + sp->s_firstdatazone - 1
00206    * Alloc_bit() never returns 0, since this is used for NO_BIT (failure).
00207    */
00208   sp = get_super(dev);
00209 
00210   /* If z is 0, skip initial part of the map known to be fully in use. */
00211   if (z == sp->s_firstdatazone) {
00212         bit = sp->s_zsearch;
00213   } else {
00214         bit = (bit_t) z - (sp->s_firstdatazone - 1);
00215   }
00216   b = alloc_bit(sp, ZMAP, bit);
00217   if (b == NO_BIT) {
00218         err_code = ENOSPC;
00219         major = (int) (sp->s_dev >> MAJOR) & BYTE;
00220         minor = (int) (sp->s_dev >> MINOR) & BYTE;
00221         printf("No space on %sdevice %d/%d\n",
00222                 sp->s_dev == root_dev ? "root " : "", major, minor);
00223         return(NO_ZONE);
00224   }
00225   if (z == sp->s_firstdatazone) sp->s_zsearch = b;      /* for next time */
00226   return(sp->s_firstdatazone - 1 + (zone_t) b);
00227 }
00228 
00229 /*===========================================================================*
00230  *                              free_zone                                    *
00231  *===========================================================================*/
00232 PUBLIC void free_zone(dev, numb)
00233 dev_t dev;                              /* device where zone located */
00234 zone_t numb;                            /* zone to be returned */
00235 {
00236 /* Return a zone. */
00237 
00238   register struct super_block *sp;
00239   bit_t bit;
00240 
00241   /* Locate the appropriate super_block and return bit. */
00242   sp = get_super(dev);
00243   if (numb < sp->s_firstdatazone || numb >= sp->s_zones) return;
00244   bit = (bit_t) (numb - (sp->s_firstdatazone - 1));
00245   free_bit(sp, ZMAP, bit);
00246   if (bit < sp->s_zsearch) sp->s_zsearch = bit;
00247 }
00248 
00249 /*===========================================================================*
00250  *                              rw_block                                     *
00251  *===========================================================================*/
00252 PRIVATE int rw_block(bp, rw_flag)
00253 register struct buf *bp;        /* buffer pointer */
00254 int rw_flag;                    /* READING or WRITING */
00255 {
00256 /* Read or write a disk block. This is the only routine in which actual disk
00257  * I/O is invoked. If an error occurs, a message is printed here, but the error
00258  * is not reported to the caller.  If the error occurred while purging a block
00259  * from the cache, it is not clear what the caller could do about it anyway.
00260  */
00261 
00262   int r, op;
00263   off_t pos;
00264   dev_t dev;
00265   int block_size;
00266 
00267   block_size = get_block_size(bp->b_dev);
00268 
00269   if ( (dev = bp->b_dev) != NO_DEV) {
00270         pos = (off_t) bp->b_blocknr * block_size;
00271         op = (rw_flag == READING ? DEV_READ : DEV_WRITE);
00272         r = dev_io(op, dev, FS_PROC_NR, bp->b_data, pos, block_size, 0);
00273         if (r != block_size) {
00274             if (r >= 0) r = END_OF_FILE;
00275             if (r != END_OF_FILE)
00276               printf("Unrecoverable disk error on device %d/%d, block %ld\n",
00277                         (dev>>MAJOR)&BYTE, (dev>>MINOR)&BYTE, bp->b_blocknr);
00278                 bp->b_dev = NO_DEV;     /* invalidate block */
00279 
00280                 /* Report read errors to interested parties. */
00281                 if (rw_flag == READING) rdwt_err = r;
00282         }
00283   }
00284 
00285   bp->b_dirt = CLEAN;
00286 
00287   return OK;
00288 }
00289 
00290 /*===========================================================================*
00291  *                              invalidate                                   *
00292  *===========================================================================*/
00293 PUBLIC void invalidate(device)
00294 dev_t device;                   /* device whose blocks are to be purged */
00295 {
00296 /* Remove all the blocks belonging to some device from the cache. */
00297 
00298   register struct buf *bp;
00299 
00300   for (bp = &buf[0]; bp < &buf[NR_BUFS]; bp++)
00301         if (bp->b_dev == device) bp->b_dev = NO_DEV;
00302 
00303 #if ENABLE_CACHE2
00304   invalidate2(device);
00305 #endif
00306 }
00307 
00308 /*===========================================================================*
00309  *                              flushall                                     *
00310  *===========================================================================*/
00311 PUBLIC void flushall(dev)
00312 dev_t dev;                      /* device to flush */
00313 {
00314 /* Flush all dirty blocks for one device. */
00315 
00316   register struct buf *bp;
00317   static struct buf *dirty[NR_BUFS];    /* static so it isn't on stack */
00318   int ndirty;
00319 
00320   for (bp = &buf[0], ndirty = 0; bp < &buf[NR_BUFS]; bp++)
00321         if (bp->b_dirt == DIRTY && bp->b_dev == dev) dirty[ndirty++] = bp;
00322   rw_scattered(dev, dirty, ndirty, WRITING);
00323 }
00324 
00325 /*===========================================================================*
00326  *                              rw_scattered                                 *
00327  *===========================================================================*/
00328 PUBLIC void rw_scattered(dev, bufq, bufqsize, rw_flag)
00329 dev_t dev;                      /* major-minor device number */
00330 struct buf **bufq;              /* pointer to array of buffers */
00331 int bufqsize;                   /* number of buffers */
00332 int rw_flag;                    /* READING or WRITING */
00333 {
00334 /* Read or write scattered data from a device. */
00335 
00336   register struct buf *bp;
00337   int gap;
00338   register int i;
00339   register iovec_t *iop;
00340   static iovec_t iovec[NR_IOREQS];  /* static so it isn't on stack */
00341   int j, r;
00342   int block_size;
00343 
00344   block_size = get_block_size(dev);
00345 
00346   /* (Shell) sort buffers on b_blocknr. */
00347   gap = 1;
00348   do
00349         gap = 3 * gap + 1;
00350   while (gap <= bufqsize);
00351   while (gap != 1) {
00352         gap /= 3;
00353         for (j = gap; j < bufqsize; j++) {
00354                 for (i = j - gap;
00355                      i >= 0 && bufq[i]->b_blocknr > bufq[i + gap]->b_blocknr;
00356                      i -= gap) {
00357                         bp = bufq[i];
00358                         bufq[i] = bufq[i + gap];
00359                         bufq[i + gap] = bp;
00360                 }
00361         }
00362   }
00363 
00364   /* Set up I/O vector and do I/O.  The result of dev_io is OK if everything
00365    * went fine, otherwise the error code for the first failed transfer.
00366    */  
00367   while (bufqsize > 0) {
00368         for (j = 0, iop = iovec; j < NR_IOREQS && j < bufqsize; j++, iop++) {
00369                 bp = bufq[j];
00370                 if (bp->b_blocknr != bufq[0]->b_blocknr + j) break;
00371                 iop->iov_addr = (vir_bytes) bp->b_data;
00372                 iop->iov_size = block_size;
00373         }
00374         r = dev_io(rw_flag == WRITING ? DEV_SCATTER : DEV_GATHER,
00375                 dev, FS_PROC_NR, iovec,
00376                 (off_t) bufq[0]->b_blocknr * block_size, j, 0);
00377 
00378         /* Harvest the results.  Dev_io reports the first error it may have
00379          * encountered, but we only care if it's the first block that failed.
00380          */
00381         for (i = 0, iop = iovec; i < j; i++, iop++) {
00382                 bp = bufq[i];
00383                 if (iop->iov_size != 0) {
00384                         /* Transfer failed. An error? Do we care? */
00385                         if (r != OK && i == 0) {
00386                                 printf(
00387                                 "fs: I/O error on device %d/%d, block %lu\n",
00388                                         (dev>>MAJOR)&BYTE, (dev>>MINOR)&BYTE,
00389                                         bp->b_blocknr);
00390                                 bp->b_dev = NO_DEV;     /* invalidate block */
00391                         }
00392                         break;
00393                 }
00394                 if (rw_flag == READING) {
00395                         bp->b_dev = dev;        /* validate block */
00396                         put_block(bp, PARTIAL_DATA_BLOCK);
00397                 } else {
00398                         bp->b_dirt = CLEAN;
00399                 }
00400         }
00401         bufq += i;
00402         bufqsize -= i;
00403         if (rw_flag == READING) {
00404                 /* Don't bother reading more than the device is willing to
00405                  * give at this time.  Don't forget to release those extras.
00406                  */
00407                 while (bufqsize > 0) {
00408                         put_block(*bufq++, PARTIAL_DATA_BLOCK);
00409                         bufqsize--;
00410                 }
00411         }
00412         if (rw_flag == WRITING && i == 0) {
00413                 /* We're not making progress, this means we might keep
00414                  * looping. Buffers remain dirty if un-written. Buffers are
00415                  * lost if invalidate()d or LRU-removed while dirty. This
00416                  * is better than keeping unwritable blocks around forever..
00417                  */
00418                 break;
00419         }
00420   }
00421 }
00422 
00423 /*===========================================================================*
00424  *                              rm_lru                                       *
00425  *===========================================================================*/
00426 PRIVATE void rm_lru(bp)
00427 struct buf *bp;
00428 {
00429 /* Remove a block from its LRU chain. */
00430   struct buf *next_ptr, *prev_ptr;
00431 
00432   bufs_in_use++;
00433   next_ptr = bp->b_next;        /* successor on LRU chain */
00434   prev_ptr = bp->b_prev;        /* predecessor on LRU chain */
00435   if (prev_ptr != NIL_BUF)
00436         prev_ptr->b_next = next_ptr;
00437   else
00438         front = next_ptr;       /* this block was at front of chain */
00439 
00440   if (next_ptr != NIL_BUF)
00441         next_ptr->b_prev = prev_ptr;
00442   else
00443         rear = prev_ptr;        /* this block was at rear of chain */
00444 }

Generated on Fri Apr 14 22:57:01 2006 for minix by  doxygen 1.4.6