cache.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* This is mostly new code. Major revisions were made to allow multiple
* file systems to share a common cache. While this consisted primarily
* of including a "devid_t" pointer in the hash functions, I also re-
* organized everything to eliminate much of the duplicated code that
* had existed previously.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/sysmacros.h>
#ifndef ICACHE_SIZE
/*
* These should probably be defined in an architecture-specific header
* file. The values below are analogous to those used in earlier versions
* of this module.
*/
#endif
#define Frst 0 /* Ptr to first element of a chain */
typedef struct cache { /* Generic cache element: */
int dev; /* .. Device file handle */
void *data; /* .. Ptr to associated data */
int size; /* .. Size of cached data */
} cache_t;
typedef struct head { /* Generic cache header: */
int size; /* .. Size of "cache" objects */
int maxblks; /* .. Max number of cached elements */
int count; /* .. Current number of cached elements */
int hits; /* .. Total cache hits */
int searches; /* .. Total searches */
int purges; /* .. Total purges */
} head_t;
/* Constructor for cache headers: */
#define cache_head(h, f, t, n) \
int read_opt; /* Number of times cache was bypassed */
static int x_dev; /* Target device ID saved here! */
static int x_len; /* length of object */
#define LOG2(x) \
(((x) <= 16) ? 4 : /* Yeah, it's ugly. But it works! */ \
(((x) <= 32) ? 5 : /* .. Binary log should be part of */ \
(((x) <= 64) ? 6 : /* .. the language! */ \
(((x) <= 128) ? 7 : 8))))
static cache_t *
{
/*
* Search cache:
*
* The caller pass a pointer to the first "cache" object in the current
* hash chain ["cap"] and a pointer to the corresponding cache header
* ["chp"]. This routine follows the cache chain until it finds an
* entry that matches both the current device [as noted in "x_dev"]
* and the cache-specific comparison ["chp->cmp"].
*
* Returns the address of the matching cache object or null if there
* is none.
*/
while (cap) {
/*
* Check all entries on the cache chain. We expect
* chains to be relatively short, so we use a simple
* linear search.
*/
/*
* Found the entry we're looking for! Move it
* to the front of the cache header's LRU list
* before returing its addres to the caller.
*/
break;
}
}
return (cap);
}
static cache_t *
{
/*
* Reclaim a cache element:
*
* This routine is used to: [a] free the oldest element from
* the cache headed at "chp" and return the address of the
* corresponding "cache_t" struct (iff dev == -1), or [b] free all
* elements on the cache headed at "chp" that belong to the
* indicated "dev"ice.
*/
/*
* We follow the cache's LRU chain from oldest to
* newest member. This ensures that we remove only
* the oldest element when we're called with a
* negative "dev" argument.
*/
/*
* This is one of the (perhaps the only)
* elements we're supposed to free. Remove it
* from both the LRU list and its associated
* hash chain. Then free the data bound the
* the cache_t element and, if "dev" is
* not -1, the element itself!
*/
else
if (dev == -1)
return (cap);
} else {
/*
* Skip this element, it's not one of the
* ones we want to free up.
*/
}
};
return (0);
}
static cache_t *
{
/*
* Install a cache element:
*
* The caller passes the address of cache descriptor ["chp"] and the
* hash chain into which the new element is to be linked ["ccp"]. This
* routine allocates a new cache_t structure (or, if the maximum number
* of elements has already been allocated, reclaims the oldest element
* from the cache), links it into the indicated hash chain, and returns
* its address to the caller.
*/
/*
* We haven't reached the maximum cache size yet.
* Allocate a new "cache_t" struct to be added to the
* cache.
*/
} else {
if (noreclaim)
return (NULL);
/*
* Cache is full. Use the "reclaim_cache" routine to
* remove the oldest element from the cache. This
* will become the cache_t struct associated with the
* new element.
*/
}
}
/*
* The File Cache:
*
* This cache (also known as the inode cache) is used to keep track of all
* files open on a given device. The only special data required to locate
* a cache entry is the file reference number which is file-system dependent
* (for UNIX file systems, it's an inode number).
*/
typedef struct icache { /* Inode cache element: */
int ic_num; /* .. I-node number */
} ic_t;
static int x_inode;
static int /* Cache search predicate: */
cmp_icache(cache_t *p)
{
/* Just check the file number ("x_inode") ... */
}
void *
{
/*
* Search File Cache:
*
* This routine searches the file cache looking for the entry bound to
* the given "dev"ice and file number ["inum"]. If said entry exists,
* it returns the address of the associated file structure. Otherwise
* it returns null.
*/
}
void
{
/*
* Build a File Cache Entry:
*
* This routne installs the "size"-byte file structure at
* "*ip" in the inode cache where it may be retrieved by
* subsequent call to get_icache.
*/
&ic_head, 0);
}
int
{
/*
* Reliably set the icache
*
* This routine is the same as set_icache except that it
* will return 1 if the entry could not be entered into the cache
* without a purge.
*/
&ic_head, 1);
return (1);
return (0);
}
/*
* The Directory Cache:
*
* This cache is designed to speed directory searches. Each entry cor-
* responds to a directory entry that was used in a pathname resolution.
* The idea is that most files used by the boot wil be contained in a hand-
* full of directories, so we can speed searches if we know ahead of time
* just where these directories are.
*/
typedef struct dcache { /* Directory cache objects: */
int dc_inum; /* .. File number */
int dc_pnum; /* .. Parent diretory's file number */
} dc_t;
static char *x_name;
static int x_pnum;
static int
{
/* Check name, length, and parent's file number */
}
int
{
/*
* Search Directory Cache:
*
* This routine searches the directory cache for an entry
* associated with directory number "pnum" from the given
* file system that de-scribes a file of the given "name".
* If we find such an entry, we return the corresponding file
* number, 0 otherwise.
*/
}
void
{
/*
* Build Directory Cache Entry:
*
* This routine creates directory cache entries to be retrieved later
* via "get_dcache". The cache key is composed of three parts: The
* device specifier, the file name ("name"), and the file number of
* the directory containing that name ("pnum"). The data portion of
* the entry consists of the file number ("inum").
*/
/*
* Allocate a buffer for the pathname component, and
* make this the "data" portion of the generalize
* "cache_t" struct. Also fill in the cache-specific
* fields (pnum, inum).
*/
} else {
/*
* Not enough memory to make a copy of the name!
* There's probably not enough to do much else either!
*/
prom_panic("no memory for directory cache");
}
}
int
{
/*
* Reliably set the dcache
*
* This routine is the same as set_dcache except that it
* return 1 if the entry could not be entered into
* the cache without a purge.
*/
&dc_head, 1);
return (1);
/*
* Not enough memory to make a copy of the name!
* There's probably not enough to do much else either!
*/
prom_panic("no memory for directory cache");
/* NOTREACHED */
}
/*
* Allocate a buffer for the pathname component, and
* make this the "data" portion of the generalize
* "cache_t" struct. Also fill in the cache-specific
* fields (pnum, inum).
*/
return (0);
}
/*
* Disk Block Cache:
*/
typedef struct bcache { /* Disk block cache objects: */
unsigned long bc_blk; /* .. The block number */
} bc_t;
static unsigned long x_blkno;
static int
{
/* Check block number, buffer size */
}
{
/*
* Search Disk Block Cache:
*
* This should be getting pretty monotonous by now. Aren't generalized
* subroutines ("objects", if you prefer) great?
*/
}
int
{
/*
* Insert Disk Block Cache Entry:
*
* In this case, we actually read the requested block into a
* dynamically allocated buffer before inserting it into the
* cache. If the read fails, we return a non-zero value.
*
* The search keys for disk blocks are the block number and
* buffer size. The data associated with each entry is the
* corresponding data buffer.
*/
/*
* We were able to succesffully allocate an input
* buffer, now read the data into it.
*/
/*
* I/O error on read. Free the input buffer,
* print an error message, and bail out.
*/
printf("disk read error\n");
return (-1);
}
&bc_head, 0);
} else {
/*
* We could be a bit more convervative here by
* calling "set_cache" before we try to allocate a
* buffer (thereby giving us a chance to re-use a
* previously allocated buffer) but the error recovery
* is a bit trickier, and if we're that short on memory
* we'll have trouble elsewhere anyway!
*/
prom_panic("can't read - no memory");
}
return (0);
}
void
release_cache(int dev)
{
/*
* Reclaim all cache entries:
*
* This routine is called by the file-system's "closeall" method. It
* removes all cache entries associated with that file system from the
* global cache and release any resources bound to said entrires.
*/
}
void
{
/*
* Print some cacheing statistics ...
*/
int j;
for (j = 0; tag[j]; j++) {
/*
* Print statistics maintained in the header
* ("head_t" struct) of each of the above caches.
*/
if (j)
printf("\n");
}
}