This source file includes following definitions.
- hmcdrv_cache_get
- hmcdrv_cache_do
- hmcdrv_cache_cmd
- hmcdrv_cache_startup
- hmcdrv_cache_shutdown
1
2
3
4
5
6
7
8
9
10 #define KMSG_COMPONENT "hmcdrv"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/jiffies.h>
16
17 #include "hmcdrv_ftp.h"
18 #include "hmcdrv_cache.h"
19
20 #define HMCDRV_CACHE_TIMEOUT 30
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
44 };
45
46 static int hmcdrv_cache_order;
47
48 static struct hmcdrv_cache_entry hmcdrv_cache_file = {
49 .fsize = SIZE_MAX,
50 .ofs = -1,
51 .len = 0,
52 .fname = {'\0'}
53 };
54
55
56
57
58
59
60
61
62 static ssize_t hmcdrv_cache_get(const struct hmcdrv_ftp_cmdspec *ftp)
63 {
64 loff_t pos;
65 ssize_t len;
66
67 if ((ftp->id != hmcdrv_cache_file.id) ||
68 strcmp(hmcdrv_cache_file.fname, ftp->fname))
69 return -1;
70
71 if (ftp->ofs >= hmcdrv_cache_file.fsize)
72 return 0;
73
74 if ((hmcdrv_cache_file.ofs < 0) ||
75 time_after(jiffies, hmcdrv_cache_file.timeout))
76 return -1;
77
78
79
80
81 len = hmcdrv_cache_file.fsize - ftp->ofs;
82
83 if (len > ftp->len)
84 len = ftp->len;
85
86
87
88
89 pos = ftp->ofs - hmcdrv_cache_file.ofs;
90
91 if ((pos >= 0) &&
92 ((pos + len) <= hmcdrv_cache_file.len)) {
93
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);
100
101 return len;
102 }
103
104 return -1;
105 }
106
107
108
109
110
111
112
113
114 static ssize_t hmcdrv_cache_do(const struct hmcdrv_ftp_cmdspec *ftp,
115 hmcdrv_cache_ftpfunc func)
116 {
117 ssize_t len;
118
119
120
121
122
123
124 if ((ftp->len > 0) && (hmcdrv_cache_file.len >= ftp->len)) {
125
126
127
128
129
130 struct hmcdrv_ftp_cmdspec cftp = *ftp;
131 cftp.buf = hmcdrv_cache_file.content;
132 cftp.len = hmcdrv_cache_file.len;
133
134 len = func(&cftp, &hmcdrv_cache_file.fsize);
135
136 if (len > 0) {
137 pr_debug("caching %zd bytes content for '%s'\n",
138 len, ftp->fname);
139
140 if (len > ftp->len)
141 len = ftp->len;
142
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);
147 }
148 } else {
149 len = func(ftp, &hmcdrv_cache_file.fsize);
150 hmcdrv_cache_file.ofs = -1;
151 }
152
153 if (len > 0) {
154
155
156
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);
162 }
163
164 return len;
165 }
166
167
168
169
170
171
172
173
174
175
176
177 ssize_t hmcdrv_cache_cmd(const struct hmcdrv_ftp_cmdspec *ftp,
178 hmcdrv_cache_ftpfunc func)
179 {
180 ssize_t len;
181
182 if ((ftp->id == HMCDRV_FTP_DIR) ||
183 (ftp->id == HMCDRV_FTP_NLIST) ||
184 (ftp->id == HMCDRV_FTP_GET)) {
185
186 len = hmcdrv_cache_get(ftp);
187
188 if (len >= 0)
189 return len;
190
191 len = hmcdrv_cache_do(ftp, func);
192
193 if (len >= 0)
194 return len;
195
196 } else {
197 len = func(ftp, NULL);
198 }
199
200
201
202
203 hmcdrv_cache_file.id = HMCDRV_FTP_NOOP;
204 hmcdrv_cache_file.fsize = LLONG_MAX;
205 hmcdrv_cache_file.ofs = -1;
206
207 return len;
208 }
209
210
211
212
213
214
215
216 int hmcdrv_cache_startup(size_t cachesize)
217 {
218 if (cachesize > 0) {
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);
223
224 if (!hmcdrv_cache_file.content) {
225 pr_err("Allocating the requested cache size of %zu bytes failed\n",
226 cachesize);
227 return -ENOMEM;
228 }
229
230 pr_debug("content cache enabled, size is %zu bytes\n",
231 cachesize);
232 }
233
234 hmcdrv_cache_file.len = cachesize;
235 return 0;
236 }
237
238
239
240
241 void hmcdrv_cache_shutdown(void)
242 {
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;
247 }
248
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;
253 }