linux/drivers/s390/char/hmcdrv_cache.c
<<
>>
Prefs
   1/*
   2 *    SE/HMC Drive (Read) Cache Functions
   3 *
   4 *    Copyright IBM Corp. 2013
   5 *    Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
   6 *
   7 */
   8
   9#define KMSG_COMPONENT "hmcdrv"
  10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11
  12#include <linux/kernel.h>
  13#include <linux/mm.h>
  14#include <linux/jiffies.h>
  15
  16#include "hmcdrv_ftp.h"
  17#include "hmcdrv_cache.h"
  18
  19#define HMCDRV_CACHE_TIMEOUT            30 /* aging timeout in seconds */
  20
  21/**
  22 * struct hmcdrv_cache_entry - file cache (only used on read/dir)
  23 * @id: FTP command ID
  24 * @content: kernel-space buffer, 4k aligned
  25 * @len: size of @content cache (0 if caching disabled)
  26 * @ofs: start of content within file (-1 if no cached content)
  27 * @fname: file name
  28 * @fsize: file size
  29 * @timeout: cache timeout in jiffies
  30 *
  31 * Notice that the first three members (id, fname, fsize) are cached on all
  32 * read/dir requests. But content is cached only under some preconditions.
  33 * Uncached content is signalled by a negative value of @ofs.
  34 */
  35struct hmcdrv_cache_entry {
  36        enum hmcdrv_ftp_cmdid id;
  37        char fname[HMCDRV_FTP_FIDENT_MAX];
  38        size_t fsize;
  39        loff_t ofs;
  40        unsigned long timeout;
  41        void *content;
  42        size_t len;
  43};
  44
  45static int hmcdrv_cache_order; /* cache allocated page order */
  46
  47static struct hmcdrv_cache_entry hmcdrv_cache_file = {
  48        .fsize = SIZE_MAX,
  49        .ofs = -1,
  50        .len = 0,
  51        .fname = {'\0'}
  52};
  53
  54/**
  55 * hmcdrv_cache_get() - looks for file data/content in read cache
  56 * @ftp: pointer to FTP command specification
  57 *
  58 * Return: number of bytes read from cache or a negative number if nothing
  59 * in content cache (for the file/cmd specified in @ftp)
  60 */
  61static ssize_t hmcdrv_cache_get(const struct hmcdrv_ftp_cmdspec *ftp)
  62{
  63        loff_t pos; /* position in cache (signed) */
  64        ssize_t len;
  65
  66        if ((ftp->id != hmcdrv_cache_file.id) ||
  67            strcmp(hmcdrv_cache_file.fname, ftp->fname))
  68                return -1;
  69
  70        if (ftp->ofs >= hmcdrv_cache_file.fsize) /* EOF ? */
  71                return 0;
  72
  73        if ((hmcdrv_cache_file.ofs < 0) || /* has content? */
  74            time_after(jiffies, hmcdrv_cache_file.timeout))
  75                return -1;
  76
  77        /* there seems to be cached content - calculate the maximum number
  78         * of bytes that can be returned (regarding file size and offset)
  79         */
  80        len = hmcdrv_cache_file.fsize - ftp->ofs;
  81
  82        if (len > ftp->len)
  83                len = ftp->len;
  84
  85        /* check if the requested chunk falls into our cache (which starts
  86         * at offset 'hmcdrv_cache_file.ofs' in the file of interest)
  87         */
  88        pos = ftp->ofs - hmcdrv_cache_file.ofs;
  89
  90        if ((pos >= 0) &&
  91            ((pos + len) <= hmcdrv_cache_file.len)) {
  92
  93                memcpy(ftp->buf,
  94                       hmcdrv_cache_file.content + pos,
  95                       len);
  96                pr_debug("using cached content of '%s', returning %zd/%zd bytes\n",
  97                         hmcdrv_cache_file.fname, len,
  98                         hmcdrv_cache_file.fsize);
  99
 100                return len;
 101        }
 102
 103        return -1;
 104}
 105
 106/**
 107 * hmcdrv_cache_do() - do a HMC drive CD/DVD transfer with cache update
 108 * @ftp: pointer to FTP command specification
 109 * @func: FTP transfer function to be used
 110 *
 111 * Return: number of bytes read/written or a (negative) error code
 112 */
 113static ssize_t hmcdrv_cache_do(const struct hmcdrv_ftp_cmdspec *ftp,
 114                               hmcdrv_cache_ftpfunc func)
 115{
 116        ssize_t len;
 117
 118        /* only cache content if the read/dir cache really exists
 119         * (hmcdrv_cache_file.len > 0), is large enough to handle the
 120         * request (hmcdrv_cache_file.len >= ftp->len) and there is a need
 121         * to do so (ftp->len > 0)
 122         */
 123        if ((ftp->len > 0) && (hmcdrv_cache_file.len >= ftp->len)) {
 124
 125                /* because the cache is not located at ftp->buf, we have to
 126                 * assemble a new HMC drive FTP cmd specification (pointing
 127                 * to our cache, and using the increased size)
 128                 */
 129                struct hmcdrv_ftp_cmdspec cftp = *ftp; /* make a copy */
 130                cftp.buf = hmcdrv_cache_file.content;  /* and update */
 131                cftp.len = hmcdrv_cache_file.len;      /* buffer data */
 132
 133                len = func(&cftp, &hmcdrv_cache_file.fsize); /* now do */
 134
 135                if (len > 0) {
 136                        pr_debug("caching %zd bytes content for '%s'\n",
 137                                 len, ftp->fname);
 138
 139                        if (len > ftp->len)
 140                                len = ftp->len;
 141
 142                        hmcdrv_cache_file.ofs = ftp->ofs;
 143                        hmcdrv_cache_file.timeout = jiffies +
 144                                HMCDRV_CACHE_TIMEOUT * HZ;
 145                        memcpy(ftp->buf, hmcdrv_cache_file.content, len);
 146                }
 147        } else {
 148                len = func(ftp, &hmcdrv_cache_file.fsize);
 149                hmcdrv_cache_file.ofs = -1; /* invalidate content */
 150        }
 151
 152        if (len > 0) {
 153                /* cache some file info (FTP command, file name and file
 154                 * size) unconditionally
 155                 */
 156                strlcpy(hmcdrv_cache_file.fname, ftp->fname,
 157                        HMCDRV_FTP_FIDENT_MAX);
 158                hmcdrv_cache_file.id = ftp->id;
 159                pr_debug("caching cmd %d, file size %zu for '%s'\n",
 160                         ftp->id, hmcdrv_cache_file.fsize, ftp->fname);
 161        }
 162
 163        return len;
 164}
 165
 166/**
 167 * hmcdrv_cache_cmd() - perform a cached HMC drive CD/DVD transfer
 168 * @ftp: pointer to FTP command specification
 169 * @func: FTP transfer function to be used
 170 *
 171 * Attention: Notice that this function is not reentrant - so the caller
 172 * must ensure exclusive execution.
 173 *
 174 * Return: number of bytes read/written or a (negative) error code
 175 */
 176ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp,
 177                         hmcdrv_cache_ftpfunc func)
 178{
 179        ssize_t len;
 180
 181        if ((ftp->id == HMCDRV_FTP_DIR) || /* read cache */
 182            (ftp->id == HMCDRV_FTP_NLIST) ||
 183            (ftp->id == HMCDRV_FTP_GET)) {
 184
 185                len = hmcdrv_cache_get(ftp);
 186
 187                if (len >= 0) /* got it from cache ? */
 188                        return len; /* yes */
 189
 190                len = hmcdrv_cache_do(ftp, func);
 191
 192                if (len >= 0)
 193                        return len;
 194
 195        } else {
 196                len = func(ftp, NULL); /* simply do original command */
 197        }
 198
 199        /* invalidate the (read) cache in case there was a write operation
 200         * or an error on read/dir
 201         */
 202        hmcdrv_cache_file.id = HMCDRV_FTP_NOOP;
 203        hmcdrv_cache_file.fsize = LLONG_MAX;
 204        hmcdrv_cache_file.ofs = -1;
 205
 206        return len;
 207}
 208
 209/**
 210 * hmcdrv_cache_startup() - startup of HMC drive cache
 211 * @cachesize: cache size
 212 *
 213 * Return: 0 on success, else a (negative) error code
 214 */
 215int hmcdrv_cache_startup(size_t cachesize)
 216{
 217        if (cachesize > 0) { /* perform caching ? */
 218                hmcdrv_cache_order = get_order(cachesize);
 219                hmcdrv_cache_file.content =
 220                        (void *) __get_free_pages(GFP_KERNEL | GFP_DMA,
 221                                                  hmcdrv_cache_order);
 222
 223                if (!hmcdrv_cache_file.content) {
 224                        pr_err("Allocating the requested cache size of %zu bytes failed\n",
 225                               cachesize);
 226                        return -ENOMEM;
 227                }
 228
 229                pr_debug("content cache enabled, size is %zu bytes\n",
 230                         cachesize);
 231        }
 232
 233        hmcdrv_cache_file.len = cachesize;
 234        return 0;
 235}
 236
 237/**
 238 * hmcdrv_cache_shutdown() - shutdown of HMC drive cache
 239 */
 240void hmcdrv_cache_shutdown(void)
 241{
 242        if (hmcdrv_cache_file.content) {
 243                free_pages((unsigned long) hmcdrv_cache_file.content,
 244                           hmcdrv_cache_order);
 245                hmcdrv_cache_file.content = NULL;
 246        }
 247
 248        hmcdrv_cache_file.id = HMCDRV_FTP_NOOP;
 249        hmcdrv_cache_file.fsize = LLONG_MAX;
 250        hmcdrv_cache_file.ofs = -1;
 251        hmcdrv_cache_file.len = 0; /* no cache */
 252}
 253