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