#define HMCDRV_CACHE_TIMEOUT 30 /* aging timeout in seconds */
/** * struct hmcdrv_cache_entry - file cache (only used on read/dir) * @id: FTP command ID * @content: kernel-space buffer, 4k aligned * @len: size of @content cache (0 if caching disabled) * @ofs: start of content within file (-1 if no cached content) * @fname: file name * @fsize: file size * @timeout: cache timeout in jiffies * * Notice that the first three members (id, fname, fsize) are cached on all * read/dir requests. But content is cached only under some preconditions. * Uncached content is signalled by a negative value of @ofs.
*/ struct hmcdrv_cache_entry { enum hmcdrv_ftp_cmdid id; char fname[HMCDRV_FTP_FIDENT_MAX];
size_t fsize;
loff_t ofs; unsignedlong timeout; void *content;
size_t len;
};
staticint hmcdrv_cache_order; /* cache allocated page order */
/** * hmcdrv_cache_get() - looks for file data/content in read cache * @ftp: pointer to FTP command specification * * Return: number of bytes read from cache or a negative number if nothing * in content cache (for the file/cmd specified in @ftp)
*/ static ssize_t hmcdrv_cache_get(conststruct hmcdrv_ftp_cmdspec *ftp)
{
loff_t pos; /* position in cache (signed) */
ssize_t len;
if ((ftp->id != hmcdrv_cache_file.id) ||
strcmp(hmcdrv_cache_file.fname, ftp->fname)) return -1;
if (ftp->ofs >= hmcdrv_cache_file.fsize) /* EOF ? */ return 0;
if ((hmcdrv_cache_file.ofs < 0) || /* has content? */
time_after(jiffies, hmcdrv_cache_file.timeout)) return -1;
/* there seems to be cached content - calculate the maximum number * of bytes that can be returned (regarding file size and offset)
*/
len = hmcdrv_cache_file.fsize - ftp->ofs;
if (len > ftp->len)
len = ftp->len;
/* check if the requested chunk falls into our cache (which starts * at offset 'hmcdrv_cache_file.ofs' in the file of interest)
*/
pos = ftp->ofs - hmcdrv_cache_file.ofs;
/** * hmcdrv_cache_do() - do a HMC drive CD/DVD transfer with cache update * @ftp: pointer to FTP command specification * @func: FTP transfer function to be used * * Return: number of bytes read/written or a (negative) error code
*/ static ssize_t hmcdrv_cache_do(conststruct hmcdrv_ftp_cmdspec *ftp,
hmcdrv_cache_ftpfunc func)
{
ssize_t len;
/* only cache content if the read/dir cache really exists * (hmcdrv_cache_file.len > 0), is large enough to handle the * request (hmcdrv_cache_file.len >= ftp->len) and there is a need * to do so (ftp->len > 0)
*/ if ((ftp->len > 0) && (hmcdrv_cache_file.len >= ftp->len)) {
/* because the cache is not located at ftp->buf, we have to * assemble a new HMC drive FTP cmd specification (pointing * to our cache, and using the increased size)
*/ struct hmcdrv_ftp_cmdspec cftp = *ftp; /* make a copy */
cftp.buf = hmcdrv_cache_file.content; /* and update */
cftp.len = hmcdrv_cache_file.len; /* buffer data */
len = func(&cftp, &hmcdrv_cache_file.fsize); /* now do */
if (len > 0) {
pr_debug("caching %zd bytes content for '%s'\n",
len, ftp->fname);
if (len > 0) { /* cache some file info (FTP command, file name and file * size) unconditionally
*/
strscpy(hmcdrv_cache_file.fname, ftp->fname,
HMCDRV_FTP_FIDENT_MAX);
hmcdrv_cache_file.id = ftp->id;
pr_debug("caching cmd %d, file size %zu for '%s'\n",
ftp->id, hmcdrv_cache_file.fsize, ftp->fname);
}
return len;
}
/** * hmcdrv_cache_cmd() - perform a cached HMC drive CD/DVD transfer * @ftp: pointer to FTP command specification * @func: FTP transfer function to be used * * Attention: Notice that this function is not reentrant - so the caller * must ensure exclusive execution. * * Return: number of bytes read/written or a (negative) error code
*/
ssize_t hmcdrv_cache_cmd(conststruct hmcdrv_ftp_cmdspec *ftp,
hmcdrv_cache_ftpfunc func)
{
ssize_t len;
if (len >= 0) /* got it from cache ? */ return len; /* yes */
len = hmcdrv_cache_do(ftp, func);
if (len >= 0) return len;
} else {
len = func(ftp, NULL); /* simply do original command */
}
/* invalidate the (read) cache in case there was a write operation * or an error on read/dir
*/
hmcdrv_cache_file.id = HMCDRV_FTP_NOOP;
hmcdrv_cache_file.fsize = LLONG_MAX;
hmcdrv_cache_file.ofs = -1;
return len;
}
/** * hmcdrv_cache_startup() - startup of HMC drive cache * @cachesize: cache size * * Return: 0 on success, else a (negative) error code
*/ int hmcdrv_cache_startup(size_t cachesize)
{ if (cachesize > 0) { /* perform caching ? */
hmcdrv_cache_order = get_order(cachesize);
hmcdrv_cache_file.content =
(void *) __get_free_pages(GFP_KERNEL | GFP_DMA,
hmcdrv_cache_order);
if (!hmcdrv_cache_file.content) {
pr_err("Allocating the requested cache size of %zu bytes failed\n",
cachesize); return -ENOMEM;
}
pr_debug("content cache enabled, size is %zu bytes\n",
cachesize);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.