2 * SE/HMC Drive (Read) Cache Functions
4 * Copyright IBM Corp. 2013
5 * Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
9 #define KMSG_COMPONENT "hmcdrv"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kernel.h>
14 #include <linux/jiffies.h>
16 #include "hmcdrv_ftp.h"
17 #include "hmcdrv_cache.h"
19 #define HMCDRV_CACHE_TIMEOUT 30 /* aging timeout in seconds */
22 * struct hmcdrv_cache_entry - file cache (only used on read/dir)
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)
29 * @timeout: cache timeout in jiffies
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.
35 struct hmcdrv_cache_entry
{
36 enum hmcdrv_ftp_cmdid id
;
37 char fname
[HMCDRV_FTP_FIDENT_MAX
];
40 unsigned long timeout
;
45 static int hmcdrv_cache_order
; /* cache allocated page order */
47 static struct hmcdrv_cache_entry hmcdrv_cache_file
= {
55 * hmcdrv_cache_get() - looks for file data/content in read cache
56 * @ftp: pointer to FTP command specification
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)
61 static ssize_t
hmcdrv_cache_get(const struct hmcdrv_ftp_cmdspec
*ftp
)
63 loff_t pos
; /* position in cache (signed) */
66 if ((ftp
->id
!= hmcdrv_cache_file
.id
) ||
67 strcmp(hmcdrv_cache_file
.fname
, ftp
->fname
))
70 if (ftp
->ofs
>= hmcdrv_cache_file
.fsize
) /* EOF ? */
73 if ((hmcdrv_cache_file
.ofs
< 0) || /* has content? */
74 time_after(jiffies
, hmcdrv_cache_file
.timeout
))
77 /* there seems to be cached content - calculate the maximum number
78 * of bytes that can be returned (regarding file size and offset)
80 len
= hmcdrv_cache_file
.fsize
- ftp
->ofs
;
85 /* check if the requested chunk falls into our cache (which starts
86 * at offset 'hmcdrv_cache_file.ofs' in the file of interest)
88 pos
= ftp
->ofs
- hmcdrv_cache_file
.ofs
;
91 ((pos
+ len
) <= hmcdrv_cache_file
.len
)) {
94 hmcdrv_cache_file
.content
+ pos
,
96 pr_debug("using cached content of '%s', returning %zd/%zd bytes\n",
97 hmcdrv_cache_file
.fname
, len
,
98 hmcdrv_cache_file
.fsize
);
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
111 * Return: number of bytes read/written or a (negative) error code
113 static ssize_t
hmcdrv_cache_do(const struct hmcdrv_ftp_cmdspec
*ftp
,
114 hmcdrv_cache_ftpfunc func
)
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)
123 if ((ftp
->len
> 0) && (hmcdrv_cache_file
.len
>= ftp
->len
)) {
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)
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 */
133 len
= func(&cftp
, &hmcdrv_cache_file
.fsize
); /* now do */
136 pr_debug("caching %zd bytes content for '%s'\n",
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
);
148 len
= func(ftp
, &hmcdrv_cache_file
.fsize
);
149 hmcdrv_cache_file
.ofs
= -1; /* invalidate content */
153 /* cache some file info (FTP command, file name and file
154 * size) unconditionally
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
);
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
171 * Attention: Notice that this function is not reentrant - so the caller
172 * must ensure exclusive execution.
174 * Return: number of bytes read/written or a (negative) error code
176 ssize_t
hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec
*ftp
,
177 hmcdrv_cache_ftpfunc func
)
181 if ((ftp
->id
== HMCDRV_FTP_DIR
) || /* read cache */
182 (ftp
->id
== HMCDRV_FTP_NLIST
) ||
183 (ftp
->id
== HMCDRV_FTP_GET
)) {
185 len
= hmcdrv_cache_get(ftp
);
187 if (len
>= 0) /* got it from cache ? */
188 return len
; /* yes */
190 len
= hmcdrv_cache_do(ftp
, func
);
196 len
= func(ftp
, NULL
); /* simply do original command */
199 /* invalidate the (read) cache in case there was a write operation
200 * or an error on read/dir
202 hmcdrv_cache_file
.id
= HMCDRV_FTP_NOOP
;
203 hmcdrv_cache_file
.fsize
= LLONG_MAX
;
204 hmcdrv_cache_file
.ofs
= -1;
210 * hmcdrv_cache_startup() - startup of HMC drive cache
211 * @cachesize: cache size
213 * Return: 0 on success, else a (negative) error code
215 int hmcdrv_cache_startup(size_t cachesize
)
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
,
223 if (!hmcdrv_cache_file
.content
) {
224 pr_err("Allocating the requested cache size of %zu bytes failed\n",
229 pr_debug("content cache enabled, size is %zu bytes\n",
233 hmcdrv_cache_file
.len
= cachesize
;
238 * hmcdrv_cache_shutdown() - shutdown of HMC drive cache
240 void hmcdrv_cache_shutdown(void)
242 if (hmcdrv_cache_file
.content
) {
243 free_pages((unsigned long) hmcdrv_cache_file
.content
,
245 hmcdrv_cache_file
.content
= NULL
;
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 */