1 // SPDX-License-Identifier: GPL-2.0
3 * SE/HMC Drive (Read) Cache Functions
5 * Copyright IBM Corp. 2013
6 * Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
10 #define KMSG_COMPONENT "hmcdrv"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kernel.h>
15 #include <linux/jiffies.h>
17 #include "hmcdrv_ftp.h"
18 #include "hmcdrv_cache.h"
20 #define HMCDRV_CACHE_TIMEOUT 30 /* aging timeout in seconds */
23 * struct hmcdrv_cache_entry - file cache (only used on read/dir)
25 * @content: kernel-space buffer, 4k aligned
26 * @len: size of @content cache (0 if caching disabled)
27 * @ofs: start of content within file (-1 if no cached content)
30 * @timeout: cache timeout in jiffies
32 * Notice that the first three members (id, fname, fsize) are cached on all
33 * read/dir requests. But content is cached only under some preconditions.
34 * Uncached content is signalled by a negative value of @ofs.
36 struct hmcdrv_cache_entry
{
37 enum hmcdrv_ftp_cmdid id
;
38 char fname
[HMCDRV_FTP_FIDENT_MAX
];
41 unsigned long timeout
;
46 static int hmcdrv_cache_order
; /* cache allocated page order */
48 static struct hmcdrv_cache_entry hmcdrv_cache_file
= {
56 * hmcdrv_cache_get() - looks for file data/content in read cache
57 * @ftp: pointer to FTP command specification
59 * Return: number of bytes read from cache or a negative number if nothing
60 * in content cache (for the file/cmd specified in @ftp)
62 static ssize_t
hmcdrv_cache_get(const struct hmcdrv_ftp_cmdspec
*ftp
)
64 loff_t pos
; /* position in cache (signed) */
67 if ((ftp
->id
!= hmcdrv_cache_file
.id
) ||
68 strcmp(hmcdrv_cache_file
.fname
, ftp
->fname
))
71 if (ftp
->ofs
>= hmcdrv_cache_file
.fsize
) /* EOF ? */
74 if ((hmcdrv_cache_file
.ofs
< 0) || /* has content? */
75 time_after(jiffies
, hmcdrv_cache_file
.timeout
))
78 /* there seems to be cached content - calculate the maximum number
79 * of bytes that can be returned (regarding file size and offset)
81 len
= hmcdrv_cache_file
.fsize
- ftp
->ofs
;
86 /* check if the requested chunk falls into our cache (which starts
87 * at offset 'hmcdrv_cache_file.ofs' in the file of interest)
89 pos
= ftp
->ofs
- hmcdrv_cache_file
.ofs
;
92 ((pos
+ len
) <= hmcdrv_cache_file
.len
)) {
95 hmcdrv_cache_file
.content
+ pos
,
97 pr_debug("using cached content of '%s', returning %zd/%zd bytes\n",
98 hmcdrv_cache_file
.fname
, len
,
99 hmcdrv_cache_file
.fsize
);
108 * hmcdrv_cache_do() - do a HMC drive CD/DVD transfer with cache update
109 * @ftp: pointer to FTP command specification
110 * @func: FTP transfer function to be used
112 * Return: number of bytes read/written or a (negative) error code
114 static ssize_t
hmcdrv_cache_do(const struct hmcdrv_ftp_cmdspec
*ftp
,
115 hmcdrv_cache_ftpfunc func
)
119 /* only cache content if the read/dir cache really exists
120 * (hmcdrv_cache_file.len > 0), is large enough to handle the
121 * request (hmcdrv_cache_file.len >= ftp->len) and there is a need
122 * to do so (ftp->len > 0)
124 if ((ftp
->len
> 0) && (hmcdrv_cache_file
.len
>= ftp
->len
)) {
126 /* because the cache is not located at ftp->buf, we have to
127 * assemble a new HMC drive FTP cmd specification (pointing
128 * to our cache, and using the increased size)
130 struct hmcdrv_ftp_cmdspec cftp
= *ftp
; /* make a copy */
131 cftp
.buf
= hmcdrv_cache_file
.content
; /* and update */
132 cftp
.len
= hmcdrv_cache_file
.len
; /* buffer data */
134 len
= func(&cftp
, &hmcdrv_cache_file
.fsize
); /* now do */
137 pr_debug("caching %zd bytes content for '%s'\n",
143 hmcdrv_cache_file
.ofs
= ftp
->ofs
;
144 hmcdrv_cache_file
.timeout
= jiffies
+
145 HMCDRV_CACHE_TIMEOUT
* HZ
;
146 memcpy(ftp
->buf
, hmcdrv_cache_file
.content
, len
);
149 len
= func(ftp
, &hmcdrv_cache_file
.fsize
);
150 hmcdrv_cache_file
.ofs
= -1; /* invalidate content */
154 /* cache some file info (FTP command, file name and file
155 * size) unconditionally
157 strlcpy(hmcdrv_cache_file
.fname
, ftp
->fname
,
158 HMCDRV_FTP_FIDENT_MAX
);
159 hmcdrv_cache_file
.id
= ftp
->id
;
160 pr_debug("caching cmd %d, file size %zu for '%s'\n",
161 ftp
->id
, hmcdrv_cache_file
.fsize
, ftp
->fname
);
168 * hmcdrv_cache_cmd() - perform a cached HMC drive CD/DVD transfer
169 * @ftp: pointer to FTP command specification
170 * @func: FTP transfer function to be used
172 * Attention: Notice that this function is not reentrant - so the caller
173 * must ensure exclusive execution.
175 * Return: number of bytes read/written or a (negative) error code
177 ssize_t
hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec
*ftp
,
178 hmcdrv_cache_ftpfunc func
)
182 if ((ftp
->id
== HMCDRV_FTP_DIR
) || /* read cache */
183 (ftp
->id
== HMCDRV_FTP_NLIST
) ||
184 (ftp
->id
== HMCDRV_FTP_GET
)) {
186 len
= hmcdrv_cache_get(ftp
);
188 if (len
>= 0) /* got it from cache ? */
189 return len
; /* yes */
191 len
= hmcdrv_cache_do(ftp
, func
);
197 len
= func(ftp
, NULL
); /* simply do original command */
200 /* invalidate the (read) cache in case there was a write operation
201 * or an error on read/dir
203 hmcdrv_cache_file
.id
= HMCDRV_FTP_NOOP
;
204 hmcdrv_cache_file
.fsize
= LLONG_MAX
;
205 hmcdrv_cache_file
.ofs
= -1;
211 * hmcdrv_cache_startup() - startup of HMC drive cache
212 * @cachesize: cache size
214 * Return: 0 on success, else a (negative) error code
216 int hmcdrv_cache_startup(size_t cachesize
)
218 if (cachesize
> 0) { /* perform caching ? */
219 hmcdrv_cache_order
= get_order(cachesize
);
220 hmcdrv_cache_file
.content
=
221 (void *) __get_free_pages(GFP_KERNEL
| GFP_DMA
,
224 if (!hmcdrv_cache_file
.content
) {
225 pr_err("Allocating the requested cache size of %zu bytes failed\n",
230 pr_debug("content cache enabled, size is %zu bytes\n",
234 hmcdrv_cache_file
.len
= cachesize
;
239 * hmcdrv_cache_shutdown() - shutdown of HMC drive cache
241 void hmcdrv_cache_shutdown(void)
243 if (hmcdrv_cache_file
.content
) {
244 free_pages((unsigned long) hmcdrv_cache_file
.content
,
246 hmcdrv_cache_file
.content
= NULL
;
249 hmcdrv_cache_file
.id
= HMCDRV_FTP_NOOP
;
250 hmcdrv_cache_file
.fsize
= LLONG_MAX
;
251 hmcdrv_cache_file
.ofs
= -1;
252 hmcdrv_cache_file
.len
= 0; /* no cache */