]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/pstore/platform.c
ca307c62420561b576e5142212f10f7538596c34
[mirror_ubuntu-bionic-kernel.git] / fs / pstore / platform.c
1 /*
2 * Persistent Storage - platform driver interface parts.
3 *
4 * Copyright (C) 2007-2008 Google, Inc.
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
21 #define pr_fmt(fmt) "pstore: " fmt
22
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>
28 #include <linux/console.h>
29 #include <linux/module.h>
30 #include <linux/pstore.h>
31 #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
32 #include <linux/zlib.h>
33 #endif
34 #ifdef CONFIG_PSTORE_LZO_COMPRESS
35 #include <linux/lzo.h>
36 #endif
37 #ifdef CONFIG_PSTORE_LZ4_COMPRESS
38 #include <linux/lz4.h>
39 #endif
40 #include <linux/string.h>
41 #include <linux/timer.h>
42 #include <linux/slab.h>
43 #include <linux/uaccess.h>
44 #include <linux/hardirq.h>
45 #include <linux/jiffies.h>
46 #include <linux/workqueue.h>
47
48 #include "internal.h"
49
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 */
55 static int pstore_update_ms = -1;
56 module_param_named(update_ms, pstore_update_ms, int, 0600);
57 MODULE_PARM_DESC(update_ms, "milliseconds before pstore updates its content "
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)");
61
62 static int pstore_new_entry;
63
64 static void pstore_timefunc(struct timer_list *);
65 static DEFINE_TIMER(pstore_timer, pstore_timefunc);
66
67 static void pstore_dowork(struct work_struct *);
68 static DECLARE_WORK(pstore_work, pstore_dowork);
69
70 /*
71 * pstore_lock just protects "psinfo" during
72 * calls to pstore_register()
73 */
74 static DEFINE_SPINLOCK(pstore_lock);
75 struct pstore_info *psinfo;
76
77 static char *backend;
78
79 /* Compression parameters */
80 #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
81 #define COMPR_LEVEL 6
82 #define WINDOW_BITS 12
83 #define MEM_LEVEL 4
84 static struct z_stream_s stream;
85 #else
86 static unsigned char *workspace;
87 #endif
88
89 struct pstore_zbackend {
90 int (*compress)(const void *in, void *out, size_t inlen, size_t outlen);
91 int (*decompress)(void *in, void *out, size_t inlen, size_t outlen);
92 void (*allocate)(void);
93 void (*free)(void);
94
95 const char *name;
96 };
97
98 static char *big_oops_buf;
99 static size_t big_oops_buf_sz;
100
101 /* How much of the console log to snapshot */
102 unsigned long kmsg_bytes = PSTORE_DEFAULT_KMSG_BYTES;
103
104 void pstore_set_kmsg_bytes(int bytes)
105 {
106 kmsg_bytes = bytes;
107 }
108
109 /* Tag each group of saved records with a sequence number */
110 static int oopscount;
111
112 static const char *get_reason_str(enum kmsg_dump_reason reason)
113 {
114 switch (reason) {
115 case KMSG_DUMP_PANIC:
116 return "Panic";
117 case KMSG_DUMP_OOPS:
118 return "Oops";
119 case KMSG_DUMP_EMERG:
120 return "Emergency";
121 case KMSG_DUMP_RESTART:
122 return "Restart";
123 case KMSG_DUMP_HALT:
124 return "Halt";
125 case KMSG_DUMP_POWEROFF:
126 return "Poweroff";
127 default:
128 return "Unknown";
129 }
130 }
131
132 /*
133 * Should pstore_dump() wait for a concurrent pstore_dump()? If
134 * not, the current pstore_dump() will report a failure to dump
135 * and return.
136 */
137 static bool pstore_cannot_wait(enum kmsg_dump_reason reason)
138 {
139 /* In NMI path, pstore shouldn't block regardless of reason. */
140 if (in_nmi())
141 return true;
142
143 switch (reason) {
144 /* In panic case, other cpus are stopped by smp_send_stop(). */
145 case KMSG_DUMP_PANIC:
146 /* Emergency restart shouldn't be blocked. */
147 case KMSG_DUMP_EMERG:
148 return true;
149 default:
150 return false;
151 }
152 }
153
154 #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
155 /* Derived from logfs_compress() */
156 static int compress_zlib(const void *in, void *out, size_t inlen, size_t outlen)
157 {
158 int err, ret;
159
160 ret = -EIO;
161 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
162 MEM_LEVEL, Z_DEFAULT_STRATEGY);
163 if (err != Z_OK)
164 goto error;
165
166 stream.next_in = in;
167 stream.avail_in = inlen;
168 stream.total_in = 0;
169 stream.next_out = out;
170 stream.avail_out = outlen;
171 stream.total_out = 0;
172
173 err = zlib_deflate(&stream, Z_FINISH);
174 if (err != Z_STREAM_END)
175 goto error;
176
177 err = zlib_deflateEnd(&stream);
178 if (err != Z_OK)
179 goto error;
180
181 if (stream.total_out >= stream.total_in)
182 goto error;
183
184 ret = stream.total_out;
185 error:
186 return ret;
187 }
188
189 /* Derived from logfs_uncompress */
190 static int decompress_zlib(void *in, void *out, size_t inlen, size_t outlen)
191 {
192 int err, ret;
193
194 ret = -EIO;
195 err = zlib_inflateInit2(&stream, WINDOW_BITS);
196 if (err != Z_OK)
197 goto error;
198
199 stream.next_in = in;
200 stream.avail_in = inlen;
201 stream.total_in = 0;
202 stream.next_out = out;
203 stream.avail_out = outlen;
204 stream.total_out = 0;
205
206 err = zlib_inflate(&stream, Z_FINISH);
207 if (err != Z_STREAM_END)
208 goto error;
209
210 err = zlib_inflateEnd(&stream);
211 if (err != Z_OK)
212 goto error;
213
214 ret = stream.total_out;
215 error:
216 return ret;
217 }
218
219 static void allocate_zlib(void)
220 {
221 size_t size;
222 size_t cmpr;
223
224 switch (psinfo->bufsize) {
225 /* buffer range for efivars */
226 case 1000 ... 2000:
227 cmpr = 56;
228 break;
229 case 2001 ... 3000:
230 cmpr = 54;
231 break;
232 case 3001 ... 3999:
233 cmpr = 52;
234 break;
235 /* buffer range for nvram, erst */
236 case 4000 ... 10000:
237 cmpr = 45;
238 break;
239 default:
240 cmpr = 60;
241 break;
242 }
243
244 big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr;
245 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
246 if (big_oops_buf) {
247 size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
248 zlib_inflate_workspacesize());
249 stream.workspace = kmalloc(size, GFP_KERNEL);
250 if (!stream.workspace) {
251 pr_err("No memory for compression workspace; skipping compression\n");
252 kfree(big_oops_buf);
253 big_oops_buf = NULL;
254 }
255 } else {
256 pr_err("No memory for uncompressed data; skipping compression\n");
257 stream.workspace = NULL;
258 }
259
260 }
261
262 static void free_zlib(void)
263 {
264 kfree(stream.workspace);
265 stream.workspace = NULL;
266 kfree(big_oops_buf);
267 big_oops_buf = NULL;
268 big_oops_buf_sz = 0;
269 }
270
271 static const struct pstore_zbackend backend_zlib = {
272 .compress = compress_zlib,
273 .decompress = decompress_zlib,
274 .allocate = allocate_zlib,
275 .free = free_zlib,
276 .name = "zlib",
277 };
278 #endif
279
280 #ifdef CONFIG_PSTORE_LZO_COMPRESS
281 static int compress_lzo(const void *in, void *out, size_t inlen, size_t outlen)
282 {
283 int ret;
284
285 ret = lzo1x_1_compress(in, inlen, out, &outlen, workspace);
286 if (ret != LZO_E_OK) {
287 pr_err("lzo_compress error, ret = %d!\n", ret);
288 return -EIO;
289 }
290
291 return outlen;
292 }
293
294 static int decompress_lzo(void *in, void *out, size_t inlen, size_t outlen)
295 {
296 int ret;
297
298 ret = lzo1x_decompress_safe(in, inlen, out, &outlen);
299 if (ret != LZO_E_OK) {
300 pr_err("lzo_decompress error, ret = %d!\n", ret);
301 return -EIO;
302 }
303
304 return outlen;
305 }
306
307 static void allocate_lzo(void)
308 {
309 big_oops_buf_sz = lzo1x_worst_compress(psinfo->bufsize);
310 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
311 if (big_oops_buf) {
312 workspace = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
313 if (!workspace) {
314 pr_err("No memory for compression workspace; skipping compression\n");
315 kfree(big_oops_buf);
316 big_oops_buf = NULL;
317 }
318 } else {
319 pr_err("No memory for uncompressed data; skipping compression\n");
320 workspace = NULL;
321 }
322 }
323
324 static void free_lzo(void)
325 {
326 kfree(workspace);
327 kfree(big_oops_buf);
328 big_oops_buf = NULL;
329 big_oops_buf_sz = 0;
330 }
331
332 static const struct pstore_zbackend backend_lzo = {
333 .compress = compress_lzo,
334 .decompress = decompress_lzo,
335 .allocate = allocate_lzo,
336 .free = free_lzo,
337 .name = "lzo",
338 };
339 #endif
340
341 #ifdef CONFIG_PSTORE_LZ4_COMPRESS
342 static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen)
343 {
344 int ret;
345
346 ret = LZ4_compress_default(in, out, inlen, outlen, workspace);
347 if (!ret) {
348 pr_err("LZ4_compress_default error; compression failed!\n");
349 return -EIO;
350 }
351
352 return ret;
353 }
354
355 static int decompress_lz4(void *in, void *out, size_t inlen, size_t outlen)
356 {
357 int ret;
358
359 ret = LZ4_decompress_safe(in, out, inlen, outlen);
360 if (ret < 0) {
361 /*
362 * LZ4_decompress_safe will return an error code
363 * (< 0) if decompression failed
364 */
365 pr_err("LZ4_decompress_safe error, ret = %d!\n", ret);
366 return -EIO;
367 }
368
369 return ret;
370 }
371
372 static void allocate_lz4(void)
373 {
374 big_oops_buf_sz = LZ4_compressBound(psinfo->bufsize);
375 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
376 if (big_oops_buf) {
377 workspace = kmalloc(LZ4_MEM_COMPRESS, GFP_KERNEL);
378 if (!workspace) {
379 pr_err("No memory for compression workspace; skipping compression\n");
380 kfree(big_oops_buf);
381 big_oops_buf = NULL;
382 }
383 } else {
384 pr_err("No memory for uncompressed data; skipping compression\n");
385 workspace = NULL;
386 }
387 }
388
389 static void free_lz4(void)
390 {
391 kfree(workspace);
392 kfree(big_oops_buf);
393 big_oops_buf = NULL;
394 big_oops_buf_sz = 0;
395 }
396
397 static const struct pstore_zbackend backend_lz4 = {
398 .compress = compress_lz4,
399 .decompress = decompress_lz4,
400 .allocate = allocate_lz4,
401 .free = free_lz4,
402 .name = "lz4",
403 };
404 #endif
405
406 static const struct pstore_zbackend *zbackend =
407 #if defined(CONFIG_PSTORE_ZLIB_COMPRESS)
408 &backend_zlib;
409 #elif defined(CONFIG_PSTORE_LZO_COMPRESS)
410 &backend_lzo;
411 #elif defined(CONFIG_PSTORE_LZ4_COMPRESS)
412 &backend_lz4;
413 #else
414 NULL;
415 #endif
416
417 static int pstore_compress(const void *in, void *out,
418 size_t inlen, size_t outlen)
419 {
420 if (zbackend)
421 return zbackend->compress(in, out, inlen, outlen);
422 else
423 return -EIO;
424 }
425
426 static int pstore_decompress(void *in, void *out, size_t inlen, size_t outlen)
427 {
428 if (zbackend)
429 return zbackend->decompress(in, out, inlen, outlen);
430 else
431 return -EIO;
432 }
433
434 static void allocate_buf_for_compression(void)
435 {
436 if (zbackend) {
437 pr_info("using %s compression\n", zbackend->name);
438 zbackend->allocate();
439 } else {
440 pr_err("allocate compression buffer error!\n");
441 }
442 }
443
444 static void free_buf_for_compression(void)
445 {
446 if (zbackend)
447 zbackend->free();
448 else
449 pr_err("free compression buffer error!\n");
450 }
451
452 /*
453 * Called when compression fails, since the printk buffer
454 * would be fetched for compression calling it again when
455 * compression fails would have moved the iterator of
456 * printk buffer which results in fetching old contents.
457 * Copy the recent messages from big_oops_buf to psinfo->buf
458 */
459 static size_t copy_kmsg_to_buffer(int hsize, size_t len)
460 {
461 size_t total_len;
462 size_t diff;
463
464 total_len = hsize + len;
465
466 if (total_len > psinfo->bufsize) {
467 diff = total_len - psinfo->bufsize + hsize;
468 memcpy(psinfo->buf, big_oops_buf, hsize);
469 memcpy(psinfo->buf + hsize, big_oops_buf + diff,
470 psinfo->bufsize - hsize);
471 total_len = psinfo->bufsize;
472 } else
473 memcpy(psinfo->buf, big_oops_buf, total_len);
474
475 return total_len;
476 }
477
478 void pstore_record_init(struct pstore_record *record,
479 struct pstore_info *psinfo)
480 {
481 memset(record, 0, sizeof(*record));
482
483 record->psi = psinfo;
484
485 /* Report zeroed timestamp if called before timekeeping has resumed. */
486 record->time = ns_to_timespec(ktime_get_real_fast_ns());
487 }
488
489 /*
490 * callback from kmsg_dump. (s2,l2) has the most recently
491 * written bytes, older bytes are in (s1,l1). Save as much
492 * as we can from the end of the buffer.
493 */
494 static void pstore_dump(struct kmsg_dumper *dumper,
495 enum kmsg_dump_reason reason)
496 {
497 unsigned long total = 0;
498 const char *why;
499 unsigned int part = 1;
500 int ret;
501
502 why = get_reason_str(reason);
503
504 if (down_trylock(&psinfo->buf_lock)) {
505 /* Failed to acquire lock: give up if we cannot wait. */
506 if (pstore_cannot_wait(reason)) {
507 pr_err("dump skipped in %s path: may corrupt error record\n",
508 in_nmi() ? "NMI" : why);
509 return;
510 }
511 if (down_interruptible(&psinfo->buf_lock)) {
512 pr_err("could not grab semaphore?!\n");
513 return;
514 }
515 }
516
517 oopscount++;
518 while (total < kmsg_bytes) {
519 char *dst;
520 size_t dst_size;
521 int header_size;
522 int zipped_len = -1;
523 size_t dump_size;
524 struct pstore_record record;
525
526 pstore_record_init(&record, psinfo);
527 record.type = PSTORE_TYPE_DMESG;
528 record.count = oopscount;
529 record.reason = reason;
530 record.part = part;
531 record.buf = psinfo->buf;
532
533 if (big_oops_buf) {
534 dst = big_oops_buf;
535 dst_size = big_oops_buf_sz;
536 } else {
537 dst = psinfo->buf;
538 dst_size = psinfo->bufsize;
539 }
540
541 /* Write dump header. */
542 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
543 oopscount, part);
544 dst_size -= header_size;
545
546 /* Write dump contents. */
547 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
548 dst_size, &dump_size))
549 break;
550
551 if (big_oops_buf) {
552 zipped_len = pstore_compress(dst, psinfo->buf,
553 header_size + dump_size,
554 psinfo->bufsize);
555
556 if (zipped_len > 0) {
557 record.compressed = true;
558 record.size = zipped_len;
559 } else {
560 record.size = copy_kmsg_to_buffer(header_size,
561 dump_size);
562 }
563 } else {
564 record.size = header_size + dump_size;
565 }
566
567 ret = psinfo->write(&record);
568 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
569 pstore_new_entry = 1;
570
571 total += record.size;
572 part++;
573 }
574
575 up(&psinfo->buf_lock);
576 }
577
578 static struct kmsg_dumper pstore_dumper = {
579 .dump = pstore_dump,
580 };
581
582 /*
583 * Register with kmsg_dump to save last part of console log on panic.
584 */
585 static void pstore_register_kmsg(void)
586 {
587 kmsg_dump_register(&pstore_dumper);
588 }
589
590 static void pstore_unregister_kmsg(void)
591 {
592 kmsg_dump_unregister(&pstore_dumper);
593 }
594
595 #ifdef CONFIG_PSTORE_CONSOLE
596 static void pstore_console_write(struct console *con, const char *s, unsigned c)
597 {
598 struct pstore_record record;
599
600 pstore_record_init(&record, psinfo);
601 record.type = PSTORE_TYPE_CONSOLE;
602
603 record.buf = (char *)s;
604 record.size = c;
605 psinfo->write(&record);
606 }
607
608 static struct console pstore_console = {
609 .name = "pstore",
610 .write = pstore_console_write,
611 .flags = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
612 .index = -1,
613 };
614
615 static void pstore_register_console(void)
616 {
617 register_console(&pstore_console);
618 }
619
620 static void pstore_unregister_console(void)
621 {
622 unregister_console(&pstore_console);
623 }
624 #else
625 static void pstore_register_console(void) {}
626 static void pstore_unregister_console(void) {}
627 #endif
628
629 static int pstore_write_user_compat(struct pstore_record *record,
630 const char __user *buf)
631 {
632 int ret = 0;
633
634 if (record->buf)
635 return -EINVAL;
636
637 record->buf = memdup_user(buf, record->size);
638 if (IS_ERR(record->buf)) {
639 ret = PTR_ERR(record->buf);
640 goto out;
641 }
642
643 ret = record->psi->write(record);
644
645 kfree(record->buf);
646 out:
647 record->buf = NULL;
648
649 return unlikely(ret < 0) ? ret : record->size;
650 }
651
652 /*
653 * platform specific persistent storage driver registers with
654 * us here. If pstore is already mounted, call the platform
655 * read function right away to populate the file system. If not
656 * then the pstore mount code will call us later to fill out
657 * the file system.
658 */
659 int pstore_register(struct pstore_info *psi)
660 {
661 struct module *owner = psi->owner;
662
663 if (backend && strcmp(backend, psi->name)) {
664 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
665 return -EPERM;
666 }
667
668 /* Sanity check flags. */
669 if (!psi->flags) {
670 pr_warn("backend '%s' must support at least one frontend\n",
671 psi->name);
672 return -EINVAL;
673 }
674
675 /* Check for required functions. */
676 if (!psi->read || !psi->write) {
677 pr_warn("backend '%s' must implement read() and write()\n",
678 psi->name);
679 return -EINVAL;
680 }
681
682 spin_lock(&pstore_lock);
683 if (psinfo) {
684 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
685 psinfo->name, psi->name);
686 spin_unlock(&pstore_lock);
687 return -EBUSY;
688 }
689
690 if (!psi->write_user)
691 psi->write_user = pstore_write_user_compat;
692 psinfo = psi;
693 mutex_init(&psinfo->read_mutex);
694 sema_init(&psinfo->buf_lock, 1);
695 spin_unlock(&pstore_lock);
696
697 if (owner && !try_module_get(owner)) {
698 psinfo = NULL;
699 return -EINVAL;
700 }
701
702 allocate_buf_for_compression();
703
704 if (pstore_is_mounted())
705 pstore_get_records(0);
706
707 if (psi->flags & PSTORE_FLAGS_DMESG)
708 pstore_register_kmsg();
709 if (psi->flags & PSTORE_FLAGS_CONSOLE)
710 pstore_register_console();
711 if (psi->flags & PSTORE_FLAGS_FTRACE)
712 pstore_register_ftrace();
713 if (psi->flags & PSTORE_FLAGS_PMSG)
714 pstore_register_pmsg();
715
716 /* Start watching for new records, if desired. */
717 if (pstore_update_ms >= 0) {
718 pstore_timer.expires = jiffies +
719 msecs_to_jiffies(pstore_update_ms);
720 add_timer(&pstore_timer);
721 }
722
723 /*
724 * Update the module parameter backend, so it is visible
725 * through /sys/module/pstore/parameters/backend
726 */
727 backend = psi->name;
728
729 pr_info("Registered %s as persistent store backend\n", psi->name);
730
731 module_put(owner);
732
733 return 0;
734 }
735 EXPORT_SYMBOL_GPL(pstore_register);
736
737 void pstore_unregister(struct pstore_info *psi)
738 {
739 /* Stop timer and make sure all work has finished. */
740 pstore_update_ms = -1;
741 del_timer_sync(&pstore_timer);
742 flush_work(&pstore_work);
743
744 if (psi->flags & PSTORE_FLAGS_PMSG)
745 pstore_unregister_pmsg();
746 if (psi->flags & PSTORE_FLAGS_FTRACE)
747 pstore_unregister_ftrace();
748 if (psi->flags & PSTORE_FLAGS_CONSOLE)
749 pstore_unregister_console();
750 if (psi->flags & PSTORE_FLAGS_DMESG)
751 pstore_unregister_kmsg();
752
753 free_buf_for_compression();
754
755 psinfo = NULL;
756 backend = NULL;
757 }
758 EXPORT_SYMBOL_GPL(pstore_unregister);
759
760 static void decompress_record(struct pstore_record *record)
761 {
762 int unzipped_len;
763 char *decompressed;
764
765 if (!record->compressed)
766 return;
767
768 /* Only PSTORE_TYPE_DMESG support compression. */
769 if (record->type != PSTORE_TYPE_DMESG) {
770 pr_warn("ignored compressed record type %d\n", record->type);
771 return;
772 }
773
774 /* No compression method has created the common buffer. */
775 if (!big_oops_buf) {
776 pr_warn("no decompression buffer allocated\n");
777 return;
778 }
779
780 unzipped_len = pstore_decompress(record->buf, big_oops_buf,
781 record->size, big_oops_buf_sz);
782 if (unzipped_len <= 0) {
783 pr_err("decompression failed: %d\n", unzipped_len);
784 return;
785 }
786
787 /* Build new buffer for decompressed contents. */
788 decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
789 GFP_KERNEL);
790 if (!decompressed) {
791 pr_err("decompression ran out of memory\n");
792 return;
793 }
794 memcpy(decompressed, big_oops_buf, unzipped_len);
795
796 /* Append ECC notice to decompressed buffer. */
797 memcpy(decompressed + unzipped_len, record->buf + record->size,
798 record->ecc_notice_size);
799
800 /* Swap out compresed contents with decompressed contents. */
801 kfree(record->buf);
802 record->buf = decompressed;
803 record->size = unzipped_len;
804 record->compressed = false;
805 }
806
807 /*
808 * Read all the records from one persistent store backend. Create
809 * files in our filesystem. Don't warn about -EEXIST errors
810 * when we are re-scanning the backing store looking to add new
811 * error records.
812 */
813 void pstore_get_backend_records(struct pstore_info *psi,
814 struct dentry *root, int quiet)
815 {
816 int failed = 0;
817 unsigned int stop_loop = 65536;
818
819 if (!psi || !root)
820 return;
821
822 mutex_lock(&psi->read_mutex);
823 if (psi->open && psi->open(psi))
824 goto out;
825
826 /*
827 * Backend callback read() allocates record.buf. decompress_record()
828 * may reallocate record.buf. On success, pstore_mkfile() will keep
829 * the record.buf, so free it only on failure.
830 */
831 for (; stop_loop; stop_loop--) {
832 struct pstore_record *record;
833 int rc;
834
835 record = kzalloc(sizeof(*record), GFP_KERNEL);
836 if (!record) {
837 pr_err("out of memory creating record\n");
838 break;
839 }
840 pstore_record_init(record, psi);
841
842 record->size = psi->read(record);
843
844 /* No more records left in backend? */
845 if (record->size <= 0) {
846 kfree(record);
847 break;
848 }
849
850 decompress_record(record);
851 rc = pstore_mkfile(root, record);
852 if (rc) {
853 /* pstore_mkfile() did not take record, so free it. */
854 kfree(record->buf);
855 kfree(record);
856 if (rc != -EEXIST || !quiet)
857 failed++;
858 }
859 }
860 if (psi->close)
861 psi->close(psi);
862 out:
863 mutex_unlock(&psi->read_mutex);
864
865 if (failed)
866 pr_warn("failed to create %d record(s) from '%s'\n",
867 failed, psi->name);
868 if (!stop_loop)
869 pr_err("looping? Too many records seen from '%s'\n",
870 psi->name);
871 }
872
873 static void pstore_dowork(struct work_struct *work)
874 {
875 pstore_get_records(1);
876 }
877
878 static void pstore_timefunc(struct timer_list *unused)
879 {
880 if (pstore_new_entry) {
881 pstore_new_entry = 0;
882 schedule_work(&pstore_work);
883 }
884
885 if (pstore_update_ms >= 0)
886 mod_timer(&pstore_timer,
887 jiffies + msecs_to_jiffies(pstore_update_ms));
888 }
889
890 module_param(backend, charp, 0444);
891 MODULE_PARM_DESC(backend, "Pstore backend to use");