GNU Linux-libre 4.14.332-gnu1
[releases.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(unsigned long);
65 static DEFINE_TIMER(pstore_timer, pstore_timefunc, 0, 0);
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         if (__getnstimeofday(&record->time)) {
487                 record->time.tv_sec = 0;
488                 record->time.tv_nsec = 0;
489         }
490 }
491
492 /*
493  * callback from kmsg_dump. (s2,l2) has the most recently
494  * written bytes, older bytes are in (s1,l1). Save as much
495  * as we can from the end of the buffer.
496  */
497 static void pstore_dump(struct kmsg_dumper *dumper,
498                         enum kmsg_dump_reason reason)
499 {
500         unsigned long   total = 0;
501         const char      *why;
502         unsigned int    part = 1;
503         int             ret;
504
505         why = get_reason_str(reason);
506
507         if (down_trylock(&psinfo->buf_lock)) {
508                 /* Failed to acquire lock: give up if we cannot wait. */
509                 if (pstore_cannot_wait(reason)) {
510                         pr_err("dump skipped in %s path: may corrupt error record\n",
511                                 in_nmi() ? "NMI" : why);
512                         return;
513                 }
514                 if (down_interruptible(&psinfo->buf_lock)) {
515                         pr_err("could not grab semaphore?!\n");
516                         return;
517                 }
518         }
519
520         oopscount++;
521         while (total < kmsg_bytes) {
522                 char *dst;
523                 size_t dst_size;
524                 int header_size;
525                 int zipped_len = -1;
526                 size_t dump_size;
527                 struct pstore_record record;
528
529                 pstore_record_init(&record, psinfo);
530                 record.type = PSTORE_TYPE_DMESG;
531                 record.count = oopscount;
532                 record.reason = reason;
533                 record.part = part;
534                 record.buf = psinfo->buf;
535
536                 if (big_oops_buf) {
537                         dst = big_oops_buf;
538                         dst_size = big_oops_buf_sz;
539                 } else {
540                         dst = psinfo->buf;
541                         dst_size = psinfo->bufsize;
542                 }
543
544                 /* Write dump header. */
545                 header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
546                                  oopscount, part);
547                 dst_size -= header_size;
548
549                 /* Write dump contents. */
550                 if (!kmsg_dump_get_buffer(dumper, true, dst + header_size,
551                                           dst_size, &dump_size))
552                         break;
553
554                 if (big_oops_buf) {
555                         zipped_len = pstore_compress(dst, psinfo->buf,
556                                                 header_size + dump_size,
557                                                 psinfo->bufsize);
558
559                         if (zipped_len > 0) {
560                                 record.compressed = true;
561                                 record.size = zipped_len;
562                         } else {
563                                 record.size = copy_kmsg_to_buffer(header_size,
564                                                                   dump_size);
565                         }
566                 } else {
567                         record.size = header_size + dump_size;
568                 }
569
570                 ret = psinfo->write(&record);
571                 if (ret == 0 && reason == KMSG_DUMP_OOPS && pstore_is_mounted())
572                         pstore_new_entry = 1;
573
574                 total += record.size;
575                 part++;
576         }
577
578         up(&psinfo->buf_lock);
579 }
580
581 static struct kmsg_dumper pstore_dumper = {
582         .dump = pstore_dump,
583 };
584
585 /*
586  * Register with kmsg_dump to save last part of console log on panic.
587  */
588 static void pstore_register_kmsg(void)
589 {
590         kmsg_dump_register(&pstore_dumper);
591 }
592
593 static void pstore_unregister_kmsg(void)
594 {
595         kmsg_dump_unregister(&pstore_dumper);
596 }
597
598 #ifdef CONFIG_PSTORE_CONSOLE
599 static void pstore_console_write(struct console *con, const char *s, unsigned c)
600 {
601         struct pstore_record record;
602
603         pstore_record_init(&record, psinfo);
604         record.type = PSTORE_TYPE_CONSOLE;
605
606         record.buf = (char *)s;
607         record.size = c;
608         psinfo->write(&record);
609 }
610
611 static struct console pstore_console = {
612         .name   = "pstore",
613         .write  = pstore_console_write,
614         .flags  = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
615         .index  = -1,
616 };
617
618 static void pstore_register_console(void)
619 {
620         register_console(&pstore_console);
621 }
622
623 static void pstore_unregister_console(void)
624 {
625         unregister_console(&pstore_console);
626 }
627 #else
628 static void pstore_register_console(void) {}
629 static void pstore_unregister_console(void) {}
630 #endif
631
632 static int pstore_write_user_compat(struct pstore_record *record,
633                                     const char __user *buf)
634 {
635         int ret = 0;
636
637         if (record->buf)
638                 return -EINVAL;
639
640         record->buf = memdup_user(buf, record->size);
641         if (unlikely(IS_ERR(record->buf))) {
642                 ret = PTR_ERR(record->buf);
643                 goto out;
644         }
645
646         ret = record->psi->write(record);
647
648         kfree(record->buf);
649 out:
650         record->buf = NULL;
651
652         return unlikely(ret < 0) ? ret : record->size;
653 }
654
655 /*
656  * platform specific persistent storage driver registers with
657  * us here. If pstore is already mounted, call the platform
658  * read function right away to populate the file system. If not
659  * then the pstore mount code will call us later to fill out
660  * the file system.
661  */
662 int pstore_register(struct pstore_info *psi)
663 {
664         struct module *owner = psi->owner;
665
666         if (backend && strcmp(backend, psi->name)) {
667                 pr_warn("ignoring unexpected backend '%s'\n", psi->name);
668                 return -EPERM;
669         }
670
671         /* Sanity check flags. */
672         if (!psi->flags) {
673                 pr_warn("backend '%s' must support at least one frontend\n",
674                         psi->name);
675                 return -EINVAL;
676         }
677
678         /* Check for required functions. */
679         if (!psi->read || !psi->write) {
680                 pr_warn("backend '%s' must implement read() and write()\n",
681                         psi->name);
682                 return -EINVAL;
683         }
684
685         spin_lock(&pstore_lock);
686         if (psinfo) {
687                 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
688                         psinfo->name, psi->name);
689                 spin_unlock(&pstore_lock);
690                 return -EBUSY;
691         }
692
693         if (!psi->write_user)
694                 psi->write_user = pstore_write_user_compat;
695         psinfo = psi;
696         mutex_init(&psinfo->read_mutex);
697         sema_init(&psinfo->buf_lock, 1);
698         spin_unlock(&pstore_lock);
699
700         if (owner && !try_module_get(owner)) {
701                 psinfo = NULL;
702                 return -EINVAL;
703         }
704
705         if (psi->flags & PSTORE_FLAGS_DMESG)
706                 allocate_buf_for_compression();
707
708         if (pstore_is_mounted())
709                 pstore_get_records(0);
710
711         if (psi->flags & PSTORE_FLAGS_DMESG)
712                 pstore_register_kmsg();
713         if (psi->flags & PSTORE_FLAGS_CONSOLE)
714                 pstore_register_console();
715         if (psi->flags & PSTORE_FLAGS_FTRACE)
716                 pstore_register_ftrace();
717         if (psi->flags & PSTORE_FLAGS_PMSG)
718                 pstore_register_pmsg();
719
720         /* Start watching for new records, if desired. */
721         if (pstore_update_ms >= 0) {
722                 pstore_timer.expires = jiffies +
723                         msecs_to_jiffies(pstore_update_ms);
724                 add_timer(&pstore_timer);
725         }
726
727         /*
728          * Update the module parameter backend, so it is visible
729          * through /sys/module/pstore/parameters/backend
730          */
731         backend = psi->name;
732
733         pr_info("Registered %s as persistent store backend\n", psi->name);
734
735         module_put(owner);
736
737         return 0;
738 }
739 EXPORT_SYMBOL_GPL(pstore_register);
740
741 void pstore_unregister(struct pstore_info *psi)
742 {
743         /* Stop timer and make sure all work has finished. */
744         pstore_update_ms = -1;
745         del_timer_sync(&pstore_timer);
746         flush_work(&pstore_work);
747
748         if (psi->flags & PSTORE_FLAGS_PMSG)
749                 pstore_unregister_pmsg();
750         if (psi->flags & PSTORE_FLAGS_FTRACE)
751                 pstore_unregister_ftrace();
752         if (psi->flags & PSTORE_FLAGS_CONSOLE)
753                 pstore_unregister_console();
754         if (psi->flags & PSTORE_FLAGS_DMESG)
755                 pstore_unregister_kmsg();
756
757         free_buf_for_compression();
758
759         psinfo = NULL;
760         backend = NULL;
761 }
762 EXPORT_SYMBOL_GPL(pstore_unregister);
763
764 static void decompress_record(struct pstore_record *record)
765 {
766         int unzipped_len;
767         char *decompressed;
768
769         if (!record->compressed)
770                 return;
771
772         /* Only PSTORE_TYPE_DMESG support compression. */
773         if (record->type != PSTORE_TYPE_DMESG) {
774                 pr_warn("ignored compressed record type %d\n", record->type);
775                 return;
776         }
777
778         /* No compression method has created the common buffer. */
779         if (!big_oops_buf) {
780                 pr_warn("no decompression buffer allocated\n");
781                 return;
782         }
783
784         unzipped_len = pstore_decompress(record->buf, big_oops_buf,
785                                          record->size, big_oops_buf_sz);
786         if (unzipped_len <= 0) {
787                 pr_err("decompression failed: %d\n", unzipped_len);
788                 return;
789         }
790
791         /* Build new buffer for decompressed contents. */
792         decompressed = kmalloc(unzipped_len + record->ecc_notice_size,
793                                GFP_KERNEL);
794         if (!decompressed) {
795                 pr_err("decompression ran out of memory\n");
796                 return;
797         }
798         memcpy(decompressed, big_oops_buf, unzipped_len);
799
800         /* Append ECC notice to decompressed buffer. */
801         memcpy(decompressed + unzipped_len, record->buf + record->size,
802                record->ecc_notice_size);
803
804         /* Swap out compresed contents with decompressed contents. */
805         kfree(record->buf);
806         record->buf = decompressed;
807         record->size = unzipped_len;
808         record->compressed = false;
809 }
810
811 /*
812  * Read all the records from one persistent store backend. Create
813  * files in our filesystem.  Don't warn about -EEXIST errors
814  * when we are re-scanning the backing store looking to add new
815  * error records.
816  */
817 void pstore_get_backend_records(struct pstore_info *psi,
818                                 struct dentry *root, int quiet)
819 {
820         int failed = 0;
821         unsigned int stop_loop = 65536;
822
823         if (!psi || !root)
824                 return;
825
826         mutex_lock(&psi->read_mutex);
827         if (psi->open && psi->open(psi))
828                 goto out;
829
830         /*
831          * Backend callback read() allocates record.buf. decompress_record()
832          * may reallocate record.buf. On success, pstore_mkfile() will keep
833          * the record.buf, so free it only on failure.
834          */
835         for (; stop_loop; stop_loop--) {
836                 struct pstore_record *record;
837                 int rc;
838
839                 record = kzalloc(sizeof(*record), GFP_KERNEL);
840                 if (!record) {
841                         pr_err("out of memory creating record\n");
842                         break;
843                 }
844                 pstore_record_init(record, psi);
845
846                 record->size = psi->read(record);
847
848                 /* No more records left in backend? */
849                 if (record->size <= 0) {
850                         kfree(record);
851                         break;
852                 }
853
854                 decompress_record(record);
855                 rc = pstore_mkfile(root, record);
856                 if (rc) {
857                         /* pstore_mkfile() did not take record, so free it. */
858                         kfree(record->buf);
859                         kfree(record);
860                         if (rc != -EEXIST || !quiet)
861                                 failed++;
862                 }
863         }
864         if (psi->close)
865                 psi->close(psi);
866 out:
867         mutex_unlock(&psi->read_mutex);
868
869         if (failed)
870                 pr_warn("failed to create %d record(s) from '%s'\n",
871                         failed, psi->name);
872         if (!stop_loop)
873                 pr_err("looping? Too many records seen from '%s'\n",
874                         psi->name);
875 }
876
877 static void pstore_dowork(struct work_struct *work)
878 {
879         pstore_get_records(1);
880 }
881
882 static void pstore_timefunc(unsigned long dummy)
883 {
884         if (pstore_new_entry) {
885                 pstore_new_entry = 0;
886                 schedule_work(&pstore_work);
887         }
888
889         if (pstore_update_ms >= 0)
890                 mod_timer(&pstore_timer,
891                           jiffies + msecs_to_jiffies(pstore_update_ms));
892 }
893
894 module_param(backend, charp, 0444);
895 MODULE_PARM_DESC(backend, "Pstore backend to use");