2 * Persistent Storage - platform driver interface parts.
4 * Copyright (C) 2007-2008 Google, Inc.
5 * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
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.
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.
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
21 #define pr_fmt(fmt) "pstore: " fmt
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>
34 #ifdef CONFIG_PSTORE_LZO_COMPRESS
35 #include <linux/lzo.h>
37 #ifdef CONFIG_PSTORE_LZ4_COMPRESS
38 #include <linux/lz4.h>
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>
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
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)");
62 static int pstore_new_entry
;
64 static void pstore_timefunc(unsigned long);
65 static DEFINE_TIMER(pstore_timer
, pstore_timefunc
, 0, 0);
67 static void pstore_dowork(struct work_struct
*);
68 static DECLARE_WORK(pstore_work
, pstore_dowork
);
71 * pstore_lock just protects "psinfo" during
72 * calls to pstore_register()
74 static DEFINE_SPINLOCK(pstore_lock
);
75 struct pstore_info
*psinfo
;
79 /* Compression parameters */
80 #ifdef CONFIG_PSTORE_ZLIB_COMPRESS
82 #define WINDOW_BITS 12
84 static struct z_stream_s stream
;
86 static unsigned char *workspace
;
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);
98 static char *big_oops_buf
;
99 static size_t big_oops_buf_sz
;
101 /* How much of the console log to snapshot */
102 unsigned long kmsg_bytes
= PSTORE_DEFAULT_KMSG_BYTES
;
104 void pstore_set_kmsg_bytes(int bytes
)
109 /* Tag each group of saved records with a sequence number */
110 static int oopscount
;
112 static const char *get_reason_str(enum kmsg_dump_reason reason
)
115 case KMSG_DUMP_PANIC
:
119 case KMSG_DUMP_EMERG
:
121 case KMSG_DUMP_RESTART
:
125 case KMSG_DUMP_POWEROFF
:
133 * Should pstore_dump() wait for a concurrent pstore_dump()? If
134 * not, the current pstore_dump() will report a failure to dump
137 static bool pstore_cannot_wait(enum kmsg_dump_reason reason
)
139 /* In NMI path, pstore shouldn't block regardless of 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
:
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
)
161 err
= zlib_deflateInit2(&stream
, COMPR_LEVEL
, Z_DEFLATED
, WINDOW_BITS
,
162 MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
167 stream
.avail_in
= inlen
;
169 stream
.next_out
= out
;
170 stream
.avail_out
= outlen
;
171 stream
.total_out
= 0;
173 err
= zlib_deflate(&stream
, Z_FINISH
);
174 if (err
!= Z_STREAM_END
)
177 err
= zlib_deflateEnd(&stream
);
181 if (stream
.total_out
>= stream
.total_in
)
184 ret
= stream
.total_out
;
189 /* Derived from logfs_uncompress */
190 static int decompress_zlib(void *in
, void *out
, size_t inlen
, size_t outlen
)
195 err
= zlib_inflateInit2(&stream
, WINDOW_BITS
);
200 stream
.avail_in
= inlen
;
202 stream
.next_out
= out
;
203 stream
.avail_out
= outlen
;
204 stream
.total_out
= 0;
206 err
= zlib_inflate(&stream
, Z_FINISH
);
207 if (err
!= Z_STREAM_END
)
210 err
= zlib_inflateEnd(&stream
);
214 ret
= stream
.total_out
;
219 static void allocate_zlib(void)
224 switch (psinfo
->bufsize
) {
225 /* buffer range for efivars */
235 /* buffer range for nvram, erst */
244 big_oops_buf_sz
= (psinfo
->bufsize
* 100) / cmpr
;
245 big_oops_buf
= kmalloc(big_oops_buf_sz
, GFP_KERNEL
);
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");
256 pr_err("No memory for uncompressed data; skipping compression\n");
257 stream
.workspace
= NULL
;
262 static void free_zlib(void)
264 kfree(stream
.workspace
);
265 stream
.workspace
= NULL
;
271 static const struct pstore_zbackend backend_zlib
= {
272 .compress
= compress_zlib
,
273 .decompress
= decompress_zlib
,
274 .allocate
= allocate_zlib
,
280 #ifdef CONFIG_PSTORE_LZO_COMPRESS
281 static int compress_lzo(const void *in
, void *out
, size_t inlen
, size_t outlen
)
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
);
294 static int decompress_lzo(void *in
, void *out
, size_t inlen
, size_t outlen
)
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
);
307 static void allocate_lzo(void)
309 big_oops_buf_sz
= lzo1x_worst_compress(psinfo
->bufsize
);
310 big_oops_buf
= kmalloc(big_oops_buf_sz
, GFP_KERNEL
);
312 workspace
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
314 pr_err("No memory for compression workspace; skipping compression\n");
319 pr_err("No memory for uncompressed data; skipping compression\n");
324 static void free_lzo(void)
332 static const struct pstore_zbackend backend_lzo
= {
333 .compress
= compress_lzo
,
334 .decompress
= decompress_lzo
,
335 .allocate
= allocate_lzo
,
341 #ifdef CONFIG_PSTORE_LZ4_COMPRESS
342 static int compress_lz4(const void *in
, void *out
, size_t inlen
, size_t outlen
)
346 ret
= LZ4_compress_default(in
, out
, inlen
, outlen
, workspace
);
348 pr_err("LZ4_compress_default error; compression failed!\n");
355 static int decompress_lz4(void *in
, void *out
, size_t inlen
, size_t outlen
)
359 ret
= LZ4_decompress_safe(in
, out
, inlen
, outlen
);
362 * LZ4_decompress_safe will return an error code
363 * (< 0) if decompression failed
365 pr_err("LZ4_decompress_safe error, ret = %d!\n", ret
);
372 static void allocate_lz4(void)
374 big_oops_buf_sz
= LZ4_compressBound(psinfo
->bufsize
);
375 big_oops_buf
= kmalloc(big_oops_buf_sz
, GFP_KERNEL
);
377 workspace
= kmalloc(LZ4_MEM_COMPRESS
, GFP_KERNEL
);
379 pr_err("No memory for compression workspace; skipping compression\n");
384 pr_err("No memory for uncompressed data; skipping compression\n");
389 static void free_lz4(void)
397 static const struct pstore_zbackend backend_lz4
= {
398 .compress
= compress_lz4
,
399 .decompress
= decompress_lz4
,
400 .allocate
= allocate_lz4
,
406 static const struct pstore_zbackend
*zbackend
=
407 #if defined(CONFIG_PSTORE_ZLIB_COMPRESS)
409 #elif defined(CONFIG_PSTORE_LZO_COMPRESS)
411 #elif defined(CONFIG_PSTORE_LZ4_COMPRESS)
417 static int pstore_compress(const void *in
, void *out
,
418 size_t inlen
, size_t outlen
)
421 return zbackend
->compress(in
, out
, inlen
, outlen
);
426 static int pstore_decompress(void *in
, void *out
, size_t inlen
, size_t outlen
)
429 return zbackend
->decompress(in
, out
, inlen
, outlen
);
434 static void allocate_buf_for_compression(void)
437 pr_info("using %s compression\n", zbackend
->name
);
438 zbackend
->allocate();
440 pr_err("allocate compression buffer error!\n");
444 static void free_buf_for_compression(void)
449 pr_err("free compression buffer error!\n");
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
459 static size_t copy_kmsg_to_buffer(int hsize
, size_t len
)
464 total_len
= hsize
+ len
;
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
;
473 memcpy(psinfo
->buf
, big_oops_buf
, total_len
);
478 void pstore_record_init(struct pstore_record
*record
,
479 struct pstore_info
*psinfo
)
481 memset(record
, 0, sizeof(*record
));
483 record
->psi
= psinfo
;
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;
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.
497 static void pstore_dump(struct kmsg_dumper
*dumper
,
498 enum kmsg_dump_reason reason
)
500 unsigned long total
= 0;
502 unsigned int part
= 1;
505 why
= get_reason_str(reason
);
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
);
514 if (down_interruptible(&psinfo
->buf_lock
)) {
515 pr_err("could not grab semaphore?!\n");
521 while (total
< kmsg_bytes
) {
527 struct pstore_record record
;
529 pstore_record_init(&record
, psinfo
);
530 record
.type
= PSTORE_TYPE_DMESG
;
531 record
.count
= oopscount
;
532 record
.reason
= reason
;
534 record
.buf
= psinfo
->buf
;
538 dst_size
= big_oops_buf_sz
;
541 dst_size
= psinfo
->bufsize
;
544 /* Write dump header. */
545 header_size
= snprintf(dst
, dst_size
, "%s#%d Part%u\n", why
,
547 dst_size
-= header_size
;
549 /* Write dump contents. */
550 if (!kmsg_dump_get_buffer(dumper
, true, dst
+ header_size
,
551 dst_size
, &dump_size
))
555 zipped_len
= pstore_compress(dst
, psinfo
->buf
,
556 header_size
+ dump_size
,
559 if (zipped_len
> 0) {
560 record
.compressed
= true;
561 record
.size
= zipped_len
;
563 record
.size
= copy_kmsg_to_buffer(header_size
,
567 record
.size
= header_size
+ dump_size
;
570 ret
= psinfo
->write(&record
);
571 if (ret
== 0 && reason
== KMSG_DUMP_OOPS
&& pstore_is_mounted())
572 pstore_new_entry
= 1;
574 total
+= record
.size
;
578 up(&psinfo
->buf_lock
);
581 static struct kmsg_dumper pstore_dumper
= {
586 * Register with kmsg_dump to save last part of console log on panic.
588 static void pstore_register_kmsg(void)
590 kmsg_dump_register(&pstore_dumper
);
593 static void pstore_unregister_kmsg(void)
595 kmsg_dump_unregister(&pstore_dumper
);
598 #ifdef CONFIG_PSTORE_CONSOLE
599 static void pstore_console_write(struct console
*con
, const char *s
, unsigned c
)
601 struct pstore_record record
;
603 pstore_record_init(&record
, psinfo
);
604 record
.type
= PSTORE_TYPE_CONSOLE
;
606 record
.buf
= (char *)s
;
608 psinfo
->write(&record
);
611 static struct console pstore_console
= {
613 .write
= pstore_console_write
,
614 .flags
= CON_PRINTBUFFER
| CON_ENABLED
| CON_ANYTIME
,
618 static void pstore_register_console(void)
620 register_console(&pstore_console
);
623 static void pstore_unregister_console(void)
625 unregister_console(&pstore_console
);
628 static void pstore_register_console(void) {}
629 static void pstore_unregister_console(void) {}
632 static int pstore_write_user_compat(struct pstore_record
*record
,
633 const char __user
*buf
)
640 record
->buf
= memdup_user(buf
, record
->size
);
641 if (unlikely(IS_ERR(record
->buf
))) {
642 ret
= PTR_ERR(record
->buf
);
646 ret
= record
->psi
->write(record
);
652 return unlikely(ret
< 0) ? ret
: record
->size
;
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
662 int pstore_register(struct pstore_info
*psi
)
664 struct module
*owner
= psi
->owner
;
666 if (backend
&& strcmp(backend
, psi
->name
)) {
667 pr_warn("ignoring unexpected backend '%s'\n", psi
->name
);
671 /* Sanity check flags. */
673 pr_warn("backend '%s' must support at least one frontend\n",
678 /* Check for required functions. */
679 if (!psi
->read
|| !psi
->write
) {
680 pr_warn("backend '%s' must implement read() and write()\n",
685 spin_lock(&pstore_lock
);
687 pr_warn("backend '%s' already loaded: ignoring '%s'\n",
688 psinfo
->name
, psi
->name
);
689 spin_unlock(&pstore_lock
);
693 if (!psi
->write_user
)
694 psi
->write_user
= pstore_write_user_compat
;
696 mutex_init(&psinfo
->read_mutex
);
697 sema_init(&psinfo
->buf_lock
, 1);
698 spin_unlock(&pstore_lock
);
700 if (owner
&& !try_module_get(owner
)) {
705 if (psi
->flags
& PSTORE_FLAGS_DMESG
)
706 allocate_buf_for_compression();
708 if (pstore_is_mounted())
709 pstore_get_records(0);
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();
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
);
728 * Update the module parameter backend, so it is visible
729 * through /sys/module/pstore/parameters/backend
733 pr_info("Registered %s as persistent store backend\n", psi
->name
);
739 EXPORT_SYMBOL_GPL(pstore_register
);
741 void pstore_unregister(struct pstore_info
*psi
)
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
);
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();
757 free_buf_for_compression();
762 EXPORT_SYMBOL_GPL(pstore_unregister
);
764 static void decompress_record(struct pstore_record
*record
)
769 if (!record
->compressed
)
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
);
778 /* No compression method has created the common buffer. */
780 pr_warn("no decompression buffer allocated\n");
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
);
791 /* Build new buffer for decompressed contents. */
792 decompressed
= kmalloc(unzipped_len
+ record
->ecc_notice_size
,
795 pr_err("decompression ran out of memory\n");
798 memcpy(decompressed
, big_oops_buf
, unzipped_len
);
800 /* Append ECC notice to decompressed buffer. */
801 memcpy(decompressed
+ unzipped_len
, record
->buf
+ record
->size
,
802 record
->ecc_notice_size
);
804 /* Swap out compresed contents with decompressed contents. */
806 record
->buf
= decompressed
;
807 record
->size
= unzipped_len
;
808 record
->compressed
= false;
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
817 void pstore_get_backend_records(struct pstore_info
*psi
,
818 struct dentry
*root
, int quiet
)
821 unsigned int stop_loop
= 65536;
826 mutex_lock(&psi
->read_mutex
);
827 if (psi
->open
&& psi
->open(psi
))
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.
835 for (; stop_loop
; stop_loop
--) {
836 struct pstore_record
*record
;
839 record
= kzalloc(sizeof(*record
), GFP_KERNEL
);
841 pr_err("out of memory creating record\n");
844 pstore_record_init(record
, psi
);
846 record
->size
= psi
->read(record
);
848 /* No more records left in backend? */
849 if (record
->size
<= 0) {
854 decompress_record(record
);
855 rc
= pstore_mkfile(root
, record
);
857 /* pstore_mkfile() did not take record, so free it. */
860 if (rc
!= -EEXIST
|| !quiet
)
867 mutex_unlock(&psi
->read_mutex
);
870 pr_warn("failed to create %d record(s) from '%s'\n",
873 pr_err("looping? Too many records seen from '%s'\n",
877 static void pstore_dowork(struct work_struct
*work
)
879 pstore_get_records(1);
882 static void pstore_timefunc(unsigned long dummy
)
884 if (pstore_new_entry
) {
885 pstore_new_entry
= 0;
886 schedule_work(&pstore_work
);
889 if (pstore_update_ms
>= 0)
890 mod_timer(&pstore_timer
,
891 jiffies
+ msecs_to_jiffies(pstore_update_ms
));
894 module_param(backend
, charp
, 0444);
895 MODULE_PARM_DESC(backend
, "Pstore backend to use");