1
2
3
4
5
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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
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
56
57
58
59
60
61static ssize_t hmcdrv_cache_get(const struct hmcdrv_ftp_cmdspec *ftp)
62{
63 loff_t pos;
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)
71 return 0;
72
73 if ((hmcdrv_cache_file.ofs < 0) ||
74 time_after(jiffies, hmcdrv_cache_file.timeout))
75 return -1;
76
77
78
79
80 len = hmcdrv_cache_file.fsize - ftp->ofs;
81
82 if (len > ftp->len)
83 len = ftp->len;
84
85
86
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
108
109
110
111
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
119
120
121
122
123 if ((ftp->len > 0) && (hmcdrv_cache_file.len >= ftp->len)) {
124
125
126
127
128
129 struct hmcdrv_ftp_cmdspec cftp = *ftp;
130 cftp.buf = hmcdrv_cache_file.content;
131 cftp.len = hmcdrv_cache_file.len;
132
133 len = func(&cftp, &hmcdrv_cache_file.fsize);
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;
150 }
151
152 if (len > 0) {
153
154
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
168
169
170
171
172
173
174
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) ||
182 (ftp->id == HMCDRV_FTP_NLIST) ||
183 (ftp->id == HMCDRV_FTP_GET)) {
184
185 len = hmcdrv_cache_get(ftp);
186
187 if (len >= 0)
188 return len;
189
190 len = hmcdrv_cache_do(ftp, func);
191
192 if (len >= 0)
193 return len;
194
195 } else {
196 len = func(ftp, NULL);
197 }
198
199
200
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
211
212
213
214
215int hmcdrv_cache_startup(size_t cachesize)
216{
217 if (cachesize > 0) {
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
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;
252}
253