Commit | Line | Data |
---|---|---|
ca01d6dd TL |
1 | /* |
2 | * Persistent Storage - platform driver interface parts. | |
3 | * | |
f29e5956 | 4 | * Copyright (C) 2007-2008 Google, Inc. |
ca01d6dd TL |
5 | * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | ||
ef748853 FF |
21 | #define pr_fmt(fmt) "pstore: " fmt |
22 | ||
ca01d6dd TL |
23 | #include <linux/atomic.h> |
24 | #include <linux/types.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/init.h> | |
27 | #include <linux/kmsg_dump.h> | |
f29e5956 | 28 | #include <linux/console.h> |
ca01d6dd TL |
29 | #include <linux/module.h> |
30 | #include <linux/pstore.h> | |
58eb5b67 | 31 | #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) |
8cfc8ddc GT |
32 | #include <linux/lzo.h> |
33 | #endif | |
58eb5b67 | 34 | #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) |
8cfc8ddc GT |
35 | #include <linux/lz4.h> |
36 | #endif | |
1021bcf4 GT |
37 | #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) |
38 | #include <linux/zstd.h> | |
39 | #endif | |
cb3bee03 | 40 | #include <linux/crypto.h> |
ca01d6dd | 41 | #include <linux/string.h> |
6dda9266 | 42 | #include <linux/timer.h> |
ca01d6dd TL |
43 | #include <linux/slab.h> |
44 | #include <linux/uaccess.h> | |
a3f5f075 | 45 | #include <linux/jiffies.h> |
6dda9266 | 46 | #include <linux/workqueue.h> |
ca01d6dd TL |
47 | |
48 | #include "internal.h" | |
49 | ||
6dda9266 LT |
50 | /* |
51 | * We defer making "oops" entries appear in pstore - see | |
52 | * whether the system is actually still running well enough | |
53 | * to let someone see the entry | |
54 | */ | |
521f7288 | 55 | static int pstore_update_ms = -1; |
a3f5f075 AV |
56 | module_param_named(update_ms, pstore_update_ms, int, 0600); |
57 | MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content " | |
521f7288 AV |
58 | "(default is -1, which means runtime updates are disabled; " |
59 | "enabling this option is not safe, it may lead to further " | |
60 | "corruption on Oopses)"); | |
6dda9266 LT |
61 | |
62 | static int pstore_new_entry; | |
63 | ||
24ed960a | 64 | static void pstore_timefunc(struct timer_list *); |
1d27e3e2 | 65 | static DEFINE_TIMER(pstore_timer, pstore_timefunc); |
6dda9266 LT |
66 | |
67 | static void pstore_dowork(struct work_struct *); | |
68 | static DECLARE_WORK(pstore_work, pstore_dowork); | |
69 | ||
ca01d6dd TL |
70 | /* |
71 | * pstore_lock just protects "psinfo" during | |
72 | * calls to pstore_register() | |
73 | */ | |
74 | static DEFINE_SPINLOCK(pstore_lock); | |
060287b8 | 75 | struct pstore_info *psinfo; |
ca01d6dd | 76 | |
dee28e72 | 77 | static char *backend; |
fe1d4758 KC |
78 | static char *compress = |
79 | #ifdef CONFIG_PSTORE_COMPRESS_DEFAULT | |
80 | CONFIG_PSTORE_COMPRESS_DEFAULT; | |
81 | #else | |
82 | NULL; | |
83 | #endif | |
dee28e72 | 84 | |
b0aad7a9 | 85 | /* Compression parameters */ |
cb3bee03 | 86 | static struct crypto_comp *tfm; |
8cfc8ddc GT |
87 | |
88 | struct pstore_zbackend { | |
cb3bee03 | 89 | int (*zbufsize)(size_t size); |
8cfc8ddc GT |
90 | const char *name; |
91 | }; | |
b0aad7a9 AB |
92 | |
93 | static char *big_oops_buf; | |
94 | static size_t big_oops_buf_sz; | |
95 | ||
366f7e7a | 96 | /* How much of the console log to snapshot */ |
349d7438 | 97 | unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES; |
ca01d6dd | 98 | |
366f7e7a | 99 | void pstore_set_kmsg_bytes(int bytes) |
ca01d6dd | 100 | { |
366f7e7a | 101 | kmsg_bytes = bytes; |
ca01d6dd TL |
102 | } |
103 | ||
ca01d6dd TL |
104 | /* Tag each group of saved records with a sequence number */ |
105 | static int oopscount; | |
106 | ||
381b872c SA |
107 | static const char *get_reason_str(enum kmsg_dump_reason reason) |
108 | { | |
109 | switch (reason) { | |
110 | case KMSG_DUMP_PANIC: | |
111 | return "Panic"; | |
112 | case KMSG_DUMP_OOPS: | |
113 | return "Oops"; | |
114 | case KMSG_DUMP_EMERG: | |
115 | return "Emergency"; | |
116 | case KMSG_DUMP_RESTART: | |
117 | return "Restart"; | |
118 | case KMSG_DUMP_HALT: | |
119 | return "Halt"; | |
120 | case KMSG_DUMP_POWEROFF: | |
121 | return "Poweroff"; | |
122 | default: | |
123 | return "Unknown"; | |
124 | } | |
125 | } | |
9f6af27f | 126 | |
9f244e9c SA |
127 | bool pstore_cannot_block_path(enum kmsg_dump_reason reason) |
128 | { | |
129 | /* | |
130 | * In case of NMI path, pstore shouldn't be blocked | |
131 | * regardless of reason. | |
132 | */ | |
133 | if (in_nmi()) | |
134 | return true; | |
135 | ||
136 | switch (reason) { | |
137 | /* In panic case, other cpus are stopped by smp_send_stop(). */ | |
138 | case KMSG_DUMP_PANIC: | |
139 | /* Emergency restart shouldn't be blocked by spin lock. */ | |
140 | case KMSG_DUMP_EMERG: | |
141 | return true; | |
142 | default: | |
143 | return false; | |
144 | } | |
145 | } | |
146 | EXPORT_SYMBOL_GPL(pstore_cannot_block_path); | |
147 | ||
58eb5b67 | 148 | #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) |
cb3bee03 | 149 | static int zbufsize_deflate(size_t size) |
adb42f5e | 150 | { |
7de8fe2f AB |
151 | size_t cmpr; |
152 | ||
cb3bee03 | 153 | switch (size) { |
7de8fe2f AB |
154 | /* buffer range for efivars */ |
155 | case 1000 ... 2000: | |
156 | cmpr = 56; | |
157 | break; | |
158 | case 2001 ... 3000: | |
159 | cmpr = 54; | |
160 | break; | |
161 | case 3001 ... 3999: | |
162 | cmpr = 52; | |
163 | break; | |
164 | /* buffer range for nvram, erst */ | |
165 | case 4000 ... 10000: | |
166 | cmpr = 45; | |
167 | break; | |
168 | default: | |
169 | cmpr = 60; | |
170 | break; | |
171 | } | |
b0aad7a9 | 172 | |
cb3bee03 | 173 | return (size * 100) / cmpr; |
8cfc8ddc | 174 | } |
8cfc8ddc GT |
175 | #endif |
176 | ||
58eb5b67 | 177 | #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) |
cb3bee03 | 178 | static int zbufsize_lzo(size_t size) |
8cfc8ddc | 179 | { |
cb3bee03 | 180 | return lzo1x_worst_compress(size); |
8cfc8ddc | 181 | } |
8cfc8ddc GT |
182 | #endif |
183 | ||
58eb5b67 | 184 | #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) |
cb3bee03 | 185 | static int zbufsize_lz4(size_t size) |
8cfc8ddc | 186 | { |
cb3bee03 | 187 | return LZ4_compressBound(size); |
8cfc8ddc | 188 | } |
239b7161 GT |
189 | #endif |
190 | ||
58eb5b67 | 191 | #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) |
cb3bee03 | 192 | static int zbufsize_842(size_t size) |
239b7161 | 193 | { |
55597406 | 194 | return size; |
239b7161 | 195 | } |
8cfc8ddc GT |
196 | #endif |
197 | ||
1021bcf4 GT |
198 | #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) |
199 | static int zbufsize_zstd(size_t size) | |
200 | { | |
201 | return ZSTD_compressBound(size); | |
202 | } | |
203 | #endif | |
204 | ||
fe1d4758 KC |
205 | static const struct pstore_zbackend *zbackend __ro_after_init; |
206 | ||
207 | static const struct pstore_zbackend zbackends[] = { | |
58eb5b67 | 208 | #if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS) |
fe1d4758 | 209 | { |
cb3bee03 GT |
210 | .zbufsize = zbufsize_deflate, |
211 | .name = "deflate", | |
fe1d4758 KC |
212 | }, |
213 | #endif | |
58eb5b67 | 214 | #if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS) |
fe1d4758 | 215 | { |
cb3bee03 | 216 | .zbufsize = zbufsize_lzo, |
fe1d4758 KC |
217 | .name = "lzo", |
218 | }, | |
219 | #endif | |
58eb5b67 | 220 | #if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) |
fe1d4758 | 221 | { |
cb3bee03 | 222 | .zbufsize = zbufsize_lz4, |
fe1d4758 KC |
223 | .name = "lz4", |
224 | }, | |
225 | #endif | |
58eb5b67 | 226 | #if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS) |
fe1d4758 | 227 | { |
cb3bee03 | 228 | .zbufsize = zbufsize_lz4, |
fe1d4758 KC |
229 | .name = "lz4hc", |
230 | }, | |
8cfc8ddc | 231 | #endif |
58eb5b67 | 232 | #if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS) |
fe1d4758 | 233 | { |
cb3bee03 | 234 | .zbufsize = zbufsize_842, |
fe1d4758 KC |
235 | .name = "842", |
236 | }, | |
1021bcf4 GT |
237 | #endif |
238 | #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) | |
239 | { | |
240 | .zbufsize = zbufsize_zstd, | |
241 | .name = "zstd", | |
242 | }, | |
fe1d4758 KC |
243 | #endif |
244 | { } | |
245 | }; | |
8cfc8ddc GT |
246 | |
247 | static int pstore_compress(const void *in, void *out, | |
cb3bee03 | 248 | unsigned int inlen, unsigned int outlen) |
8cfc8ddc | 249 | { |
cb3bee03 GT |
250 | int ret; |
251 | ||
252 | ret = crypto_comp_compress(tfm, in, inlen, out, &outlen); | |
253 | if (ret) { | |
254 | pr_err("crypto_comp_compress failed, ret = %d!\n", ret); | |
255 | return ret; | |
256 | } | |
257 | ||
258 | return outlen; | |
8cfc8ddc GT |
259 | } |
260 | ||
cb3bee03 GT |
261 | static int pstore_decompress(void *in, void *out, |
262 | unsigned int inlen, unsigned int outlen) | |
8cfc8ddc | 263 | { |
cb3bee03 GT |
264 | int ret; |
265 | ||
266 | ret = crypto_comp_decompress(tfm, in, inlen, out, &outlen); | |
267 | if (ret) { | |
268 | pr_err("crypto_comp_decompress failed, ret = %d!\n", ret); | |
269 | return ret; | |
270 | } | |
271 | ||
272 | return outlen; | |
8cfc8ddc GT |
273 | } |
274 | ||
275 | static void allocate_buf_for_compression(void) | |
276 | { | |
e698aaf3 | 277 | if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend) |
cb3bee03 GT |
278 | return; |
279 | ||
280 | if (!crypto_has_comp(zbackend->name, 0, 0)) { | |
281 | pr_err("No %s compression\n", zbackend->name); | |
282 | return; | |
283 | } | |
284 | ||
285 | big_oops_buf_sz = zbackend->zbufsize(psinfo->bufsize); | |
286 | if (big_oops_buf_sz <= 0) | |
287 | return; | |
288 | ||
289 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); | |
290 | if (!big_oops_buf) { | |
8cfc8ddc | 291 | pr_err("allocate compression buffer error!\n"); |
cb3bee03 GT |
292 | return; |
293 | } | |
294 | ||
295 | tfm = crypto_alloc_comp(zbackend->name, 0, 0); | |
296 | if (IS_ERR_OR_NULL(tfm)) { | |
297 | kfree(big_oops_buf); | |
298 | big_oops_buf = NULL; | |
299 | pr_err("crypto_alloc_comp() failed!\n"); | |
300 | return; | |
8cfc8ddc GT |
301 | } |
302 | } | |
303 | ||
304 | static void free_buf_for_compression(void) | |
305 | { | |
e698aaf3 | 306 | if (IS_ENABLED(CONFIG_PSTORE_COMPRESS) && !IS_ERR_OR_NULL(tfm)) |
cb3bee03 GT |
307 | crypto_free_comp(tfm); |
308 | kfree(big_oops_buf); | |
309 | big_oops_buf = NULL; | |
310 | big_oops_buf_sz = 0; | |
ee1d2674 GT |
311 | } |
312 | ||
b0aad7a9 AB |
313 | /* |
314 | * Called when compression fails, since the printk buffer | |
315 | * would be fetched for compression calling it again when | |
316 | * compression fails would have moved the iterator of | |
317 | * printk buffer which results in fetching old contents. | |
318 | * Copy the recent messages from big_oops_buf to psinfo->buf | |
319 | */ | |
320 | static size_t copy_kmsg_to_buffer(int hsize, size_t len) | |
321 | { | |
322 | size_t total_len; | |
323 | size_t diff; | |
324 | ||
325 | total_len = hsize + len; | |
326 | ||
327 | if (total_len > psinfo->bufsize) { | |
328 | diff = total_len - psinfo->bufsize + hsize; | |
329 | memcpy(psinfo->buf, big_oops_buf, hsize); | |
330 | memcpy(psinfo->buf + hsize, big_oops_buf + diff, | |
331 | psinfo->bufsize - hsize); | |
332 | total_len = psinfo->bufsize; | |
333 | } else | |
334 | memcpy(psinfo->buf, big_oops_buf, total_len); | |
335 | ||
336 | return total_len; | |
337 | } | |
338 | ||
e581ca81 KC |
339 | void pstore_record_init(struct pstore_record *record, |
340 | struct pstore_info *psinfo) | |
341 | { | |
342 | memset(record, 0, sizeof(*record)); | |
343 | ||
344 | record->psi = psinfo; | |
c7f3c595 KC |
345 | |
346 | /* Report zeroed timestamp if called before timekeeping has resumed. */ | |
7aaa822e | 347 | record->time = ns_to_timespec64(ktime_get_real_fast_ns()); |
e581ca81 KC |
348 | } |
349 | ||
ca01d6dd TL |
350 | /* |
351 | * callback from kmsg_dump. (s2,l2) has the most recently | |
352 | * written bytes, older bytes are in (s1,l1). Save as much | |
353 | * as we can from the end of the buffer. | |
354 | */ | |
355 | static void pstore_dump(struct kmsg_dumper *dumper, | |
e2ae715d | 356 | enum kmsg_dump_reason reason) |
ca01d6dd | 357 | { |
e2ae715d | 358 | unsigned long total = 0; |
381b872c | 359 | const char *why; |
b94fdd07 | 360 | unsigned int part = 1; |
abd4d558 | 361 | unsigned long flags = 0; |
98e44fda | 362 | int is_locked; |
e2ae715d | 363 | int ret; |
ca01d6dd | 364 | |
381b872c | 365 | why = get_reason_str(reason); |
9f6af27f | 366 | |
9f244e9c SA |
367 | if (pstore_cannot_block_path(reason)) { |
368 | is_locked = spin_trylock_irqsave(&psinfo->buf_lock, flags); | |
369 | if (!is_locked) { | |
370 | pr_err("pstore dump routine blocked in %s path, may corrupt error record\n" | |
371 | , in_nmi() ? "NMI" : why); | |
959217c8 | 372 | return; |
9f244e9c | 373 | } |
98e44fda | 374 | } else { |
abd4d558 | 375 | spin_lock_irqsave(&psinfo->buf_lock, flags); |
98e44fda NK |
376 | is_locked = 1; |
377 | } | |
ca01d6dd TL |
378 | oopscount++; |
379 | while (total < kmsg_bytes) { | |
e2ae715d | 380 | char *dst; |
76cc9580 KC |
381 | size_t dst_size; |
382 | int header_size; | |
b0aad7a9 | 383 | int zipped_len = -1; |
76cc9580 | 384 | size_t dump_size; |
e581ca81 KC |
385 | struct pstore_record record; |
386 | ||
387 | pstore_record_init(&record, psinfo); | |
388 | record.type = PSTORE_TYPE_DMESG; | |
389 | record.count = oopscount; | |
390 | record.reason = reason; | |
391 | record.part = part; | |
392 | record.buf = psinfo->buf; | |
e2ae715d | 393 | |
f0e2efcf | 394 | if (big_oops_buf && is_locked) { |
b0aad7a9 | 395 | dst = big_oops_buf; |
76cc9580 | 396 | dst_size = big_oops_buf_sz; |
235f6d15 NK |
397 | } else { |
398 | dst = psinfo->buf; | |
76cc9580 | 399 | dst_size = psinfo->bufsize; |
235f6d15 NK |
400 | } |
401 | ||
76cc9580 KC |
402 | /* Write dump header. */ |
403 | header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, | |
404 | oopscount, part); | |
405 | dst_size -= header_size; | |
ca01d6dd | 406 | |
76cc9580 KC |
407 | /* Write dump contents. */ |
408 | if (!kmsg_dump_get_buffer(dumper, true, dst + header_size, | |
409 | dst_size, &dump_size)) | |
235f6d15 | 410 | break; |
b0aad7a9 | 411 | |
235f6d15 | 412 | if (big_oops_buf && is_locked) { |
b0aad7a9 | 413 | zipped_len = pstore_compress(dst, psinfo->buf, |
76cc9580 KC |
414 | header_size + dump_size, |
415 | psinfo->bufsize); | |
b0aad7a9 AB |
416 | |
417 | if (zipped_len > 0) { | |
76cc9580 KC |
418 | record.compressed = true; |
419 | record.size = zipped_len; | |
b0aad7a9 | 420 | } else { |
76cc9580 KC |
421 | record.size = copy_kmsg_to_buffer(header_size, |
422 | dump_size); | |
b0aad7a9 AB |
423 | } |
424 | } else { | |
76cc9580 | 425 | record.size = header_size + dump_size; |
b0aad7a9 | 426 | } |
ca01d6dd | 427 | |
76cc9580 | 428 | ret = psinfo->write(&record); |
b238b8fa | 429 | if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted()) |
6dda9266 | 430 | pstore_new_entry = 1; |
e2ae715d | 431 | |
76cc9580 | 432 | total += record.size; |
56280682 | 433 | part++; |
ca01d6dd | 434 | } |
98e44fda | 435 | if (is_locked) |
abd4d558 | 436 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
ca01d6dd TL |
437 | } |
438 | ||
439 | static struct kmsg_dumper pstore_dumper = { | |
440 | .dump = pstore_dump, | |
441 | }; | |
442 | ||
306e5c2a GT |
443 | /* |
444 | * Register with kmsg_dump to save last part of console log on panic. | |
445 | */ | |
18730411 GT |
446 | static void pstore_register_kmsg(void) |
447 | { | |
448 | kmsg_dump_register(&pstore_dumper); | |
449 | } | |
450 | ||
ee1d2674 GT |
451 | static void pstore_unregister_kmsg(void) |
452 | { | |
453 | kmsg_dump_unregister(&pstore_dumper); | |
454 | } | |
455 | ||
f29e5956 AV |
456 | #ifdef CONFIG_PSTORE_CONSOLE |
457 | static void pstore_console_write(struct console *con, const char *s, unsigned c) | |
458 | { | |
459 | const char *e = s + c; | |
460 | ||
461 | while (s < e) { | |
e581ca81 | 462 | struct pstore_record record; |
f29e5956 AV |
463 | unsigned long flags; |
464 | ||
e581ca81 KC |
465 | pstore_record_init(&record, psinfo); |
466 | record.type = PSTORE_TYPE_CONSOLE; | |
467 | ||
f29e5956 AV |
468 | if (c > psinfo->bufsize) |
469 | c = psinfo->bufsize; | |
80c9d03c CL |
470 | |
471 | if (oops_in_progress) { | |
472 | if (!spin_trylock_irqsave(&psinfo->buf_lock, flags)) | |
473 | break; | |
474 | } else { | |
475 | spin_lock_irqsave(&psinfo->buf_lock, flags); | |
476 | } | |
b10b4711 KC |
477 | record.buf = (char *)s; |
478 | record.size = c; | |
4c9ec219 | 479 | psinfo->write(&record); |
f29e5956 AV |
480 | spin_unlock_irqrestore(&psinfo->buf_lock, flags); |
481 | s += c; | |
482 | c = e - s; | |
483 | } | |
484 | } | |
485 | ||
486 | static struct console pstore_console = { | |
487 | .name = "pstore", | |
488 | .write = pstore_console_write, | |
489 | .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME, | |
490 | .index = -1, | |
491 | }; | |
492 | ||
493 | static void pstore_register_console(void) | |
494 | { | |
495 | register_console(&pstore_console); | |
496 | } | |
ee1d2674 GT |
497 | |
498 | static void pstore_unregister_console(void) | |
499 | { | |
500 | unregister_console(&pstore_console); | |
501 | } | |
f29e5956 AV |
502 | #else |
503 | static void pstore_register_console(void) {} | |
ee1d2674 | 504 | static void pstore_unregister_console(void) {} |
f29e5956 AV |
505 | #endif |
506 | ||
4c9ec219 KC |
507 | static int pstore_write_user_compat(struct pstore_record *record, |
508 | const char __user *buf) | |
5bf6d1b9 | 509 | { |
30800d99 KC |
510 | int ret = 0; |
511 | ||
512 | if (record->buf) | |
513 | return -EINVAL; | |
514 | ||
077090af | 515 | record->buf = memdup_user(buf, record->size); |
dfd6fa39 | 516 | if (IS_ERR(record->buf)) { |
077090af | 517 | ret = PTR_ERR(record->buf); |
30800d99 | 518 | goto out; |
5bf6d1b9 | 519 | } |
30800d99 KC |
520 | |
521 | ret = record->psi->write(record); | |
522 | ||
30800d99 | 523 | kfree(record->buf); |
077090af | 524 | out: |
30800d99 KC |
525 | record->buf = NULL; |
526 | ||
527 | return unlikely(ret < 0) ? ret : record->size; | |
5bf6d1b9 MS |
528 | } |
529 | ||
ca01d6dd TL |
530 | /* |
531 | * platform specific persistent storage driver registers with | |
532 | * us here. If pstore is already mounted, call the platform | |
533 | * read function right away to populate the file system. If not | |
534 | * then the pstore mount code will call us later to fill out | |
535 | * the file system. | |
ca01d6dd TL |
536 | */ |
537 | int pstore_register(struct pstore_info *psi) | |
538 | { | |
539 | struct module *owner = psi->owner; | |
540 | ||
0d7cd09a KC |
541 | if (backend && strcmp(backend, psi->name)) { |
542 | pr_warn("ignoring unexpected backend '%s'\n", psi->name); | |
8e48b1a8 | 543 | return -EPERM; |
0d7cd09a | 544 | } |
8e48b1a8 | 545 | |
4c9ec219 KC |
546 | /* Sanity check flags. */ |
547 | if (!psi->flags) { | |
548 | pr_warn("backend '%s' must support at least one frontend\n", | |
549 | psi->name); | |
550 | return -EINVAL; | |
551 | } | |
552 | ||
553 | /* Check for required functions. */ | |
554 | if (!psi->read || !psi->write) { | |
555 | pr_warn("backend '%s' must implement read() and write()\n", | |
556 | psi->name); | |
557 | return -EINVAL; | |
558 | } | |
559 | ||
ca01d6dd TL |
560 | spin_lock(&pstore_lock); |
561 | if (psinfo) { | |
0d7cd09a KC |
562 | pr_warn("backend '%s' already loaded: ignoring '%s'\n", |
563 | psinfo->name, psi->name); | |
ca01d6dd TL |
564 | spin_unlock(&pstore_lock); |
565 | return -EBUSY; | |
566 | } | |
dee28e72 | 567 | |
4c9ec219 KC |
568 | if (!psi->write_user) |
569 | psi->write_user = pstore_write_user_compat; | |
ca01d6dd | 570 | psinfo = psi; |
f6f82851 | 571 | mutex_init(&psinfo->read_mutex); |
ca01d6dd TL |
572 | spin_unlock(&pstore_lock); |
573 | ||
574 | if (owner && !try_module_get(owner)) { | |
575 | psinfo = NULL; | |
576 | return -EINVAL; | |
577 | } | |
578 | ||
b0aad7a9 AB |
579 | allocate_buf_for_compression(); |
580 | ||
ca01d6dd | 581 | if (pstore_is_mounted()) |
6dda9266 | 582 | pstore_get_records(0); |
ca01d6dd | 583 | |
c950fd6f NK |
584 | if (psi->flags & PSTORE_FLAGS_DMESG) |
585 | pstore_register_kmsg(); | |
586 | if (psi->flags & PSTORE_FLAGS_CONSOLE) | |
df36ac1b | 587 | pstore_register_console(); |
c950fd6f | 588 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
df36ac1b | 589 | pstore_register_ftrace(); |
c950fd6f | 590 | if (psi->flags & PSTORE_FLAGS_PMSG) |
9d5438f4 | 591 | pstore_register_pmsg(); |
ca01d6dd | 592 | |
6330d553 | 593 | /* Start watching for new records, if desired. */ |
a3f5f075 AV |
594 | if (pstore_update_ms >= 0) { |
595 | pstore_timer.expires = jiffies + | |
596 | msecs_to_jiffies(pstore_update_ms); | |
597 | add_timer(&pstore_timer); | |
598 | } | |
6dda9266 | 599 | |
42222c2a WL |
600 | /* |
601 | * Update the module parameter backend, so it is visible | |
602 | * through /sys/module/pstore/parameters/backend | |
603 | */ | |
604 | backend = psi->name; | |
605 | ||
ef748853 | 606 | pr_info("Registered %s as persistent store backend\n", psi->name); |
8e48b1a8 | 607 | |
1344dd86 KC |
608 | module_put(owner); |
609 | ||
ca01d6dd TL |
610 | return 0; |
611 | } | |
612 | EXPORT_SYMBOL_GPL(pstore_register); | |
613 | ||
ee1d2674 GT |
614 | void pstore_unregister(struct pstore_info *psi) |
615 | { | |
6330d553 KC |
616 | /* Stop timer and make sure all work has finished. */ |
617 | pstore_update_ms = -1; | |
618 | del_timer_sync(&pstore_timer); | |
619 | flush_work(&pstore_work); | |
620 | ||
c950fd6f | 621 | if (psi->flags & PSTORE_FLAGS_PMSG) |
a1db8060 | 622 | pstore_unregister_pmsg(); |
c950fd6f | 623 | if (psi->flags & PSTORE_FLAGS_FTRACE) |
a1db8060 | 624 | pstore_unregister_ftrace(); |
c950fd6f | 625 | if (psi->flags & PSTORE_FLAGS_CONSOLE) |
a1db8060 | 626 | pstore_unregister_console(); |
c950fd6f NK |
627 | if (psi->flags & PSTORE_FLAGS_DMESG) |
628 | pstore_unregister_kmsg(); | |
ee1d2674 GT |
629 | |
630 | free_buf_for_compression(); | |
631 | ||
632 | psinfo = NULL; | |
633 | backend = NULL; | |
634 | } | |
635 | EXPORT_SYMBOL_GPL(pstore_unregister); | |
636 | ||
634f8f51 KC |
637 | static void decompress_record(struct pstore_record *record) |
638 | { | |
639 | int unzipped_len; | |
7e8cc8dc | 640 | char *decompressed; |
634f8f51 | 641 | |
4a16d1cb AK |
642 | if (!record->compressed) |
643 | return; | |
644 | ||
634f8f51 | 645 | /* Only PSTORE_TYPE_DMESG support compression. */ |
4a16d1cb | 646 | if (record->type != PSTORE_TYPE_DMESG) { |
634f8f51 KC |
647 | pr_warn("ignored compressed record type %d\n", record->type); |
648 | return; | |
649 | } | |
650 | ||
651 | /* No compression method has created the common buffer. */ | |
652 | if (!big_oops_buf) { | |
653 | pr_warn("no decompression buffer allocated\n"); | |
654 | return; | |
655 | } | |
656 | ||
657 | unzipped_len = pstore_decompress(record->buf, big_oops_buf, | |
658 | record->size, big_oops_buf_sz); | |
7e8cc8dc | 659 | if (unzipped_len <= 0) { |
634f8f51 | 660 | pr_err("decompression failed: %d\n", unzipped_len); |
7e8cc8dc KC |
661 | return; |
662 | } | |
663 | ||
664 | /* Build new buffer for decompressed contents. */ | |
665 | decompressed = kmalloc(unzipped_len + record->ecc_notice_size, | |
666 | GFP_KERNEL); | |
667 | if (!decompressed) { | |
668 | pr_err("decompression ran out of memory\n"); | |
669 | return; | |
670 | } | |
671 | memcpy(decompressed, big_oops_buf, unzipped_len); | |
672 | ||
673 | /* Append ECC notice to decompressed buffer. */ | |
674 | memcpy(decompressed + unzipped_len, record->buf + record->size, | |
675 | record->ecc_notice_size); | |
676 | ||
677 | /* Swap out compresed contents with decompressed contents. */ | |
678 | kfree(record->buf); | |
679 | record->buf = decompressed; | |
680 | record->size = unzipped_len; | |
681 | record->compressed = false; | |
634f8f51 KC |
682 | } |
683 | ||
ca01d6dd | 684 | /* |
3a7d2fd1 | 685 | * Read all the records from one persistent store backend. Create |
6dda9266 LT |
686 | * files in our filesystem. Don't warn about -EEXIST errors |
687 | * when we are re-scanning the backing store looking to add new | |
688 | * error records. | |
ca01d6dd | 689 | */ |
3a7d2fd1 KC |
690 | void pstore_get_backend_records(struct pstore_info *psi, |
691 | struct dentry *root, int quiet) | |
ca01d6dd | 692 | { |
2a2b0acf | 693 | int failed = 0; |
656de42e | 694 | unsigned int stop_loop = 65536; |
ca01d6dd | 695 | |
3a7d2fd1 | 696 | if (!psi || !root) |
ca01d6dd TL |
697 | return; |
698 | ||
f6f82851 | 699 | mutex_lock(&psi->read_mutex); |
2174f6df | 700 | if (psi->open && psi->open(psi)) |
06cf91b4 CG |
701 | goto out; |
702 | ||
1dfff7dd KC |
703 | /* |
704 | * Backend callback read() allocates record.buf. decompress_record() | |
705 | * may reallocate record.buf. On success, pstore_mkfile() will keep | |
706 | * the record.buf, so free it only on failure. | |
707 | */ | |
656de42e | 708 | for (; stop_loop; stop_loop--) { |
2a2b0acf KC |
709 | struct pstore_record *record; |
710 | int rc; | |
711 | ||
712 | record = kzalloc(sizeof(*record), GFP_KERNEL); | |
713 | if (!record) { | |
714 | pr_err("out of memory creating record\n"); | |
715 | break; | |
716 | } | |
e581ca81 | 717 | pstore_record_init(record, psi); |
2a2b0acf KC |
718 | |
719 | record->size = psi->read(record); | |
720 | ||
721 | /* No more records left in backend? */ | |
f6525b96 DA |
722 | if (record->size <= 0) { |
723 | kfree(record); | |
2a2b0acf | 724 | break; |
f6525b96 | 725 | } |
2a2b0acf KC |
726 | |
727 | decompress_record(record); | |
3a7d2fd1 | 728 | rc = pstore_mkfile(root, record); |
1dfff7dd | 729 | if (rc) { |
83f70f07 | 730 | /* pstore_mkfile() did not take record, so free it. */ |
2a2b0acf | 731 | kfree(record->buf); |
83f70f07 | 732 | kfree(record); |
1dfff7dd KC |
733 | if (rc != -EEXIST || !quiet) |
734 | failed++; | |
735 | } | |
ca01d6dd | 736 | } |
2174f6df KC |
737 | if (psi->close) |
738 | psi->close(psi); | |
06cf91b4 | 739 | out: |
f6f82851 | 740 | mutex_unlock(&psi->read_mutex); |
ca01d6dd TL |
741 | |
742 | if (failed) | |
656de42e | 743 | pr_warn("failed to create %d record(s) from '%s'\n", |
ef748853 | 744 | failed, psi->name); |
656de42e KC |
745 | if (!stop_loop) |
746 | pr_err("looping? Too many records seen from '%s'\n", | |
747 | psi->name); | |
ca01d6dd TL |
748 | } |
749 | ||
6dda9266 LT |
750 | static void pstore_dowork(struct work_struct *work) |
751 | { | |
752 | pstore_get_records(1); | |
753 | } | |
754 | ||
24ed960a | 755 | static void pstore_timefunc(struct timer_list *unused) |
6dda9266 LT |
756 | { |
757 | if (pstore_new_entry) { | |
758 | pstore_new_entry = 0; | |
759 | schedule_work(&pstore_work); | |
760 | } | |
761 | ||
6330d553 KC |
762 | if (pstore_update_ms >= 0) |
763 | mod_timer(&pstore_timer, | |
764 | jiffies + msecs_to_jiffies(pstore_update_ms)); | |
6dda9266 LT |
765 | } |
766 | ||
fe1d4758 KC |
767 | void __init pstore_choose_compression(void) |
768 | { | |
769 | const struct pstore_zbackend *step; | |
770 | ||
771 | if (!compress) | |
772 | return; | |
773 | ||
774 | for (step = zbackends; step->name; step++) { | |
775 | if (!strcmp(compress, step->name)) { | |
776 | zbackend = step; | |
777 | pr_info("using %s compression\n", zbackend->name); | |
778 | return; | |
779 | } | |
780 | } | |
781 | } | |
782 | ||
783 | module_param(compress, charp, 0444); | |
784 | MODULE_PARM_DESC(compress, "Pstore compression to use"); | |
785 | ||
dee28e72 MG |
786 | module_param(backend, charp, 0444); |
787 | MODULE_PARM_DESC(backend, "Pstore backend to use"); |