treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / s390 / char / hmcdrv_cache.c
blob1f5bdb23786234450b757ffcbbc85e7b966e0a5f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SE/HMC Drive (Read) Cache Functions
5 * Copyright IBM Corp. 2013
6 * Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
8 */
10 #define KMSG_COMPONENT "hmcdrv"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/mm.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 */
22 /**
23 * struct hmcdrv_cache_entry - file cache (only used on read/dir)
24 * @id: FTP command ID
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)
28 * @fname: file name
29 * @fsize: file size
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];
39 size_t fsize;
40 loff_t ofs;
41 unsigned long timeout;
42 void *content;
43 size_t len;
46 static int hmcdrv_cache_order; /* cache allocated page order */
48 static struct hmcdrv_cache_entry hmcdrv_cache_file = {
49 .fsize = SIZE_MAX,
50 .ofs = -1,
51 .len = 0,
52 .fname = {'\0'}
55 /**
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) */
65 ssize_t len;
67 if ((ftp->id != hmcdrv_cache_file.id) ||
68 strcmp(hmcdrv_cache_file.fname, ftp->fname))
69 return -1;
71 if (ftp->ofs >= hmcdrv_cache_file.fsize) /* EOF ? */
72 return 0;
74 if ((hmcdrv_cache_file.ofs < 0) || /* has content? */
75 time_after(jiffies, hmcdrv_cache_file.timeout))
76 return -1;
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;
83 if (len > ftp->len)
84 len = ftp->len;
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;
91 if ((pos >= 0) &&
92 ((pos + len) <= hmcdrv_cache_file.len)) {
94 memcpy(ftp->buf,
95 hmcdrv_cache_file.content + pos,
96 len);
97 pr_debug("using cached content of '%s', returning %zd/%zd bytes\n",
98 hmcdrv_cache_file.fname, len,
99 hmcdrv_cache_file.fsize);
101 return len;
104 return -1;
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)
117 ssize_t len;
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 */
136 if (len > 0) {
137 pr_debug("caching %zd bytes content for '%s'\n",
138 len, ftp->fname);
140 if (len > ftp->len)
141 len = ftp->len;
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);
148 } else {
149 len = func(ftp, &hmcdrv_cache_file.fsize);
150 hmcdrv_cache_file.ofs = -1; /* invalidate content */
153 if (len > 0) {
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);
164 return len;
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)
180 ssize_t len;
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);
193 if (len >= 0)
194 return len;
196 } else {
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;
207 return len;
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,
222 hmcdrv_cache_order);
224 if (!hmcdrv_cache_file.content) {
225 pr_err("Allocating the requested cache size of %zu bytes failed\n",
226 cachesize);
227 return -ENOMEM;
230 pr_debug("content cache enabled, size is %zu bytes\n",
231 cachesize);
234 hmcdrv_cache_file.len = cachesize;
235 return 0;
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,
245 hmcdrv_cache_order);
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 */