]> git.ipfire.org Git - people/ms/linux.git/blame - fs/pstore/zone.c
pstore/zone,blk: Add support for pmsg frontend
[people/ms/linux.git] / fs / pstore / zone.c
CommitLineData
d26c3321
WL
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Provide a pstore intermediate backend, organized into kernel memory
4 * allocated zones that are then mapped and flushed into a single
5 * contiguous region on a storage backend of some kind (block, mtd, etc).
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/mount.h>
14#include <linux/printk.h>
15#include <linux/fs.h>
16#include <linux/pstore_zone.h>
17#include <linux/kdev_t.h>
18#include <linux/device.h>
19#include <linux/namei.h>
20#include <linux/fcntl.h>
21#include <linux/uio.h>
22#include <linux/writeback.h>
23#include "internal.h"
24
25/**
26 * struct psz_head - header of zone to flush to storage
27 *
28 * @sig: signature to indicate header (PSZ_SIG xor PSZONE-type value)
29 * @datalen: length of data in @data
0dc06826 30 * @start: offset into @data where the beginning of the stored bytes begin
d26c3321
WL
31 * @data: zone data.
32 */
33struct psz_buffer {
34#define PSZ_SIG (0x43474244) /* DBGC */
35 uint32_t sig;
36 atomic_t datalen;
0dc06826 37 atomic_t start;
d26c3321
WL
38 uint8_t data[];
39};
40
41/**
42 * struct psz_kmsg_header - kmsg dump-specific header to flush to storage
43 *
44 * @magic: magic num for kmsg dump header
45 * @time: kmsg dump trigger time
46 * @compressed: whether conpressed
47 * @counter: kmsg dump counter
48 * @reason: the kmsg dump reason (e.g. oops, panic, etc)
49 * @data: pointer to log data
50 *
51 * This is a sub-header for a kmsg dump, trailing after &psz_buffer.
52 */
53struct psz_kmsg_header {
54#define PSTORE_KMSG_HEADER_MAGIC 0x4dfc3ae5 /* Just a random number */
55 uint32_t magic;
56 struct timespec64 time;
57 bool compressed;
58 uint32_t counter;
59 enum kmsg_dump_reason reason;
60 uint8_t data[];
61};
62
63/**
64 * struct pstore_zone - single stored buffer
65 *
66 * @off: zone offset of storage
67 * @type: front-end type for this zone
68 * @name: front-end name for this zone
69 * @buffer: pointer to data buffer managed by this zone
70 * @oldbuf: pointer to old data buffer
71 * @buffer_size: bytes in @buffer->data
72 * @should_recover: whether this zone should recover from storage
73 * @dirty: whether the data in @buffer dirty
74 *
75 * zone structure in memory.
76 */
77struct pstore_zone {
78 loff_t off;
79 const char *name;
80 enum pstore_type_id type;
81
82 struct psz_buffer *buffer;
83 struct psz_buffer *oldbuf;
84 size_t buffer_size;
85 bool should_recover;
86 atomic_t dirty;
87};
88
89/**
90 * struct psz_context - all about running state of pstore/zone
91 *
92 * @kpszs: kmsg dump storage zones
0dc06826 93 * @ppsz: pmsg storage zone
d26c3321
WL
94 * @kmsg_max_cnt: max count of @kpszs
95 * @kmsg_read_cnt: counter of total read kmsg dumps
96 * @kmsg_write_cnt: counter of total kmsg dump writes
0dc06826 97 * @pmsg_read_cnt: counter of total read pmsg zone
d26c3321
WL
98 * @oops_counter: counter of oops dumps
99 * @panic_counter: counter of panic dumps
100 * @recovered: whether finished recovering data from storage
101 * @on_panic: whether panic is happening
102 * @pstore_zone_info_lock: lock to @pstore_zone_info
103 * @pstore_zone_info: information from backend
104 * @pstore: structure for pstore
105 */
106struct psz_context {
107 struct pstore_zone **kpszs;
0dc06826 108 struct pstore_zone *ppsz;
d26c3321
WL
109 unsigned int kmsg_max_cnt;
110 unsigned int kmsg_read_cnt;
111 unsigned int kmsg_write_cnt;
0dc06826 112 unsigned int pmsg_read_cnt;
d26c3321
WL
113 /*
114 * These counters should be calculated during recovery.
115 * It records the oops/panic times after crashes rather than boots.
116 */
117 unsigned int oops_counter;
118 unsigned int panic_counter;
119 atomic_t recovered;
120 atomic_t on_panic;
121
122 /*
123 * pstore_zone_info_lock protects this entire structure during calls
124 * to register_pstore_zone()/unregister_pstore_zone().
125 */
126 struct mutex pstore_zone_info_lock;
127 struct pstore_zone_info *pstore_zone_info;
128 struct pstore_info pstore;
129};
130static struct psz_context pstore_zone_cxt;
131
132/**
133 * enum psz_flush_mode - flush mode for psz_zone_write()
134 *
135 * @FLUSH_NONE: do not flush to storage but update data on memory
136 * @FLUSH_PART: just flush part of data including meta data to storage
137 * @FLUSH_META: just flush meta data of zone to storage
138 * @FLUSH_ALL: flush all of zone
139 */
140enum psz_flush_mode {
141 FLUSH_NONE = 0,
142 FLUSH_PART,
143 FLUSH_META,
144 FLUSH_ALL,
145};
146
147static inline int buffer_datalen(struct pstore_zone *zone)
148{
149 return atomic_read(&zone->buffer->datalen);
150}
151
0dc06826
WL
152static inline int buffer_start(struct pstore_zone *zone)
153{
154 return atomic_read(&zone->buffer->start);
155}
156
d26c3321
WL
157static inline bool is_on_panic(void)
158{
159 return atomic_read(&pstore_zone_cxt.on_panic);
160}
161
0dc06826 162static ssize_t psz_zone_read_buffer(struct pstore_zone *zone, char *buf,
d26c3321
WL
163 size_t len, unsigned long off)
164{
0dc06826 165 if (!buf || !zone || !zone->buffer)
d26c3321
WL
166 return -EINVAL;
167 if (off > zone->buffer_size)
168 return -EINVAL;
169 len = min_t(size_t, len, zone->buffer_size - off);
170 memcpy(buf, zone->buffer->data + off, len);
171 return len;
172}
173
0dc06826
WL
174static int psz_zone_read_oldbuf(struct pstore_zone *zone, char *buf,
175 size_t len, unsigned long off)
176{
177 if (!buf || !zone || !zone->oldbuf)
178 return -EINVAL;
179 if (off > zone->buffer_size)
180 return -EINVAL;
181 len = min_t(size_t, len, zone->buffer_size - off);
182 memcpy(buf, zone->oldbuf->data + off, len);
183 return 0;
184}
185
d26c3321
WL
186static int psz_zone_write(struct pstore_zone *zone,
187 enum psz_flush_mode flush_mode, const char *buf,
188 size_t len, unsigned long off)
189{
190 struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
191 ssize_t wcnt = 0;
192 ssize_t (*writeop)(const char *buf, size_t bytes, loff_t pos);
193 size_t wlen;
194
195 if (off > zone->buffer_size)
196 return -EINVAL;
197
198 wlen = min_t(size_t, len, zone->buffer_size - off);
199 if (buf && wlen) {
200 memcpy(zone->buffer->data + off, buf, wlen);
201 atomic_set(&zone->buffer->datalen, wlen + off);
202 }
203
204 /* avoid to damage old records */
205 if (!is_on_panic() && !atomic_read(&pstore_zone_cxt.recovered))
206 goto dirty;
207
208 writeop = is_on_panic() ? info->panic_write : info->write;
209 if (!writeop)
210 goto dirty;
211
212 switch (flush_mode) {
213 case FLUSH_NONE:
214 if (unlikely(buf && wlen))
215 goto dirty;
216 return 0;
217 case FLUSH_PART:
218 wcnt = writeop((const char *)zone->buffer->data + off, wlen,
219 zone->off + sizeof(*zone->buffer) + off);
220 if (wcnt != wlen)
221 goto dirty;
222 fallthrough;
223 case FLUSH_META:
224 wlen = sizeof(struct psz_buffer);
225 wcnt = writeop((const char *)zone->buffer, wlen, zone->off);
226 if (wcnt != wlen)
227 goto dirty;
228 break;
229 case FLUSH_ALL:
230 wlen = zone->buffer_size + sizeof(*zone->buffer);
231 wcnt = writeop((const char *)zone->buffer, wlen, zone->off);
232 if (wcnt != wlen)
233 goto dirty;
234 break;
235 }
236
237 return 0;
238dirty:
239 atomic_set(&zone->dirty, true);
240 return -EBUSY;
241}
242
243static int psz_flush_dirty_zone(struct pstore_zone *zone)
244{
245 int ret;
246
247 if (unlikely(!zone))
248 return -EINVAL;
249
250 if (unlikely(!atomic_read(&pstore_zone_cxt.recovered)))
251 return -EBUSY;
252
253 if (!atomic_xchg(&zone->dirty, false))
254 return 0;
255
256 ret = psz_zone_write(zone, FLUSH_ALL, NULL, 0, 0);
257 if (ret)
258 atomic_set(&zone->dirty, true);
259 return ret;
260}
261
262static int psz_flush_dirty_zones(struct pstore_zone **zones, unsigned int cnt)
263{
264 int i, ret;
265 struct pstore_zone *zone;
266
267 if (!zones)
268 return -EINVAL;
269
270 for (i = 0; i < cnt; i++) {
271 zone = zones[i];
272 if (!zone)
273 return -EINVAL;
274 ret = psz_flush_dirty_zone(zone);
275 if (ret)
276 return ret;
277 }
278 return 0;
279}
280
281static int psz_move_zone(struct pstore_zone *old, struct pstore_zone *new)
282{
283 const char *data = (const char *)old->buffer->data;
284 int ret;
285
286 ret = psz_zone_write(new, FLUSH_ALL, data, buffer_datalen(old), 0);
287 if (ret) {
288 atomic_set(&new->buffer->datalen, 0);
289 atomic_set(&new->dirty, false);
290 return ret;
291 }
292 atomic_set(&old->buffer->datalen, 0);
293 return 0;
294}
295
296static int psz_kmsg_recover_data(struct psz_context *cxt)
297{
298 struct pstore_zone_info *info = cxt->pstore_zone_info;
299 struct pstore_zone *zone = NULL;
300 struct psz_buffer *buf;
301 unsigned long i;
302 ssize_t rcnt;
303
304 if (!info->read)
305 return -EINVAL;
306
307 for (i = 0; i < cxt->kmsg_max_cnt; i++) {
308 zone = cxt->kpszs[i];
309 if (unlikely(!zone))
310 return -EINVAL;
311 if (atomic_read(&zone->dirty)) {
312 unsigned int wcnt = cxt->kmsg_write_cnt;
313 struct pstore_zone *new = cxt->kpszs[wcnt];
314 int ret;
315
316 ret = psz_move_zone(zone, new);
317 if (ret) {
318 pr_err("move zone from %lu to %d failed\n",
319 i, wcnt);
320 return ret;
321 }
322 cxt->kmsg_write_cnt = (wcnt + 1) % cxt->kmsg_max_cnt;
323 }
324 if (!zone->should_recover)
325 continue;
326 buf = zone->buffer;
327 rcnt = info->read((char *)buf, zone->buffer_size + sizeof(*buf),
328 zone->off);
329 if (rcnt != zone->buffer_size + sizeof(*buf))
330 return (int)rcnt < 0 ? (int)rcnt : -EIO;
331 }
332 return 0;
333}
334
335static int psz_kmsg_recover_meta(struct psz_context *cxt)
336{
337 struct pstore_zone_info *info = cxt->pstore_zone_info;
338 struct pstore_zone *zone;
339 size_t rcnt, len;
340 struct psz_buffer *buf;
341 struct psz_kmsg_header *hdr;
342 struct timespec64 time = { };
343 unsigned long i;
344 /*
345 * Recover may on panic, we can't allocate any memory by kmalloc.
346 * So, we use local array instead.
347 */
348 char buffer_header[sizeof(*buf) + sizeof(*hdr)] = {0};
349
350 if (!info->read)
351 return -EINVAL;
352
353 len = sizeof(*buf) + sizeof(*hdr);
354 buf = (struct psz_buffer *)buffer_header;
355 for (i = 0; i < cxt->kmsg_max_cnt; i++) {
356 zone = cxt->kpszs[i];
357 if (unlikely(!zone))
358 return -EINVAL;
359
360 rcnt = info->read((char *)buf, len, zone->off);
361 if (rcnt != len) {
362 pr_err("read %s with id %lu failed\n", zone->name, i);
363 return (int)rcnt < 0 ? (int)rcnt : -EIO;
364 }
365
366 if (buf->sig != zone->buffer->sig) {
367 pr_debug("no valid data in kmsg dump zone %lu\n", i);
368 continue;
369 }
370
371 if (zone->buffer_size < atomic_read(&buf->datalen)) {
372 pr_info("found overtop zone: %s: id %lu, off %lld, size %zu\n",
373 zone->name, i, zone->off,
374 zone->buffer_size);
375 continue;
376 }
377
378 hdr = (struct psz_kmsg_header *)buf->data;
379 if (hdr->magic != PSTORE_KMSG_HEADER_MAGIC) {
380 pr_info("found invalid zone: %s: id %lu, off %lld, size %zu\n",
381 zone->name, i, zone->off,
382 zone->buffer_size);
383 continue;
384 }
385
386 /*
387 * we get the newest zone, and the next one must be the oldest
388 * or unused zone, because we do write one by one like a circle.
389 */
390 if (hdr->time.tv_sec >= time.tv_sec) {
391 time.tv_sec = hdr->time.tv_sec;
392 cxt->kmsg_write_cnt = (i + 1) % cxt->kmsg_max_cnt;
393 }
394
395 if (hdr->reason == KMSG_DUMP_OOPS)
396 cxt->oops_counter =
397 max(cxt->oops_counter, hdr->counter);
398 else if (hdr->reason == KMSG_DUMP_PANIC)
399 cxt->panic_counter =
400 max(cxt->panic_counter, hdr->counter);
401
402 if (!atomic_read(&buf->datalen)) {
403 pr_debug("found erased zone: %s: id %lu, off %lld, size %zu, datalen %d\n",
404 zone->name, i, zone->off,
405 zone->buffer_size,
406 atomic_read(&buf->datalen));
407 continue;
408 }
409
410 if (!is_on_panic())
411 zone->should_recover = true;
412 pr_debug("found nice zone: %s: id %lu, off %lld, size %zu, datalen %d\n",
413 zone->name, i, zone->off,
414 zone->buffer_size, atomic_read(&buf->datalen));
415 }
416
417 return 0;
418}
419
420static int psz_kmsg_recover(struct psz_context *cxt)
421{
422 int ret;
423
424 if (!cxt->kpszs)
425 return 0;
426
427 ret = psz_kmsg_recover_meta(cxt);
428 if (ret)
429 goto recover_fail;
430
431 ret = psz_kmsg_recover_data(cxt);
432 if (ret)
433 goto recover_fail;
434
435 return 0;
436recover_fail:
437 pr_debug("psz_recover_kmsg failed\n");
438 return ret;
439}
440
0dc06826
WL
441static int psz_recover_zone(struct psz_context *cxt, struct pstore_zone *zone)
442{
443 struct pstore_zone_info *info = cxt->pstore_zone_info;
444 struct psz_buffer *oldbuf, tmpbuf;
445 int ret = 0;
446 char *buf;
447 ssize_t rcnt, len, start, off;
448
449 if (!zone || zone->oldbuf)
450 return 0;
451
452 if (is_on_panic()) {
453 /* save data as much as possible */
454 psz_flush_dirty_zone(zone);
455 return 0;
456 }
457
458 if (unlikely(!info->read))
459 return -EINVAL;
460
461 len = sizeof(struct psz_buffer);
462 rcnt = info->read((char *)&tmpbuf, len, zone->off);
463 if (rcnt != len) {
464 pr_debug("read zone %s failed\n", zone->name);
465 return (int)rcnt < 0 ? (int)rcnt : -EIO;
466 }
467
468 if (tmpbuf.sig != zone->buffer->sig) {
469 pr_debug("no valid data in zone %s\n", zone->name);
470 return 0;
471 }
472
473 if (zone->buffer_size < atomic_read(&tmpbuf.datalen) ||
474 zone->buffer_size < atomic_read(&tmpbuf.start)) {
475 pr_info("found overtop zone: %s: off %lld, size %zu\n",
476 zone->name, zone->off, zone->buffer_size);
477 /* just keep going */
478 return 0;
479 }
480
481 if (!atomic_read(&tmpbuf.datalen)) {
482 pr_debug("found erased zone: %s: off %lld, size %zu, datalen %d\n",
483 zone->name, zone->off, zone->buffer_size,
484 atomic_read(&tmpbuf.datalen));
485 return 0;
486 }
487
488 pr_debug("found nice zone: %s: off %lld, size %zu, datalen %d\n",
489 zone->name, zone->off, zone->buffer_size,
490 atomic_read(&tmpbuf.datalen));
491
492 len = atomic_read(&tmpbuf.datalen) + sizeof(*oldbuf);
493 oldbuf = kzalloc(len, GFP_KERNEL);
494 if (!oldbuf)
495 return -ENOMEM;
496
497 memcpy(oldbuf, &tmpbuf, sizeof(*oldbuf));
498 buf = (char *)oldbuf + sizeof(*oldbuf);
499 len = atomic_read(&oldbuf->datalen);
500 start = atomic_read(&oldbuf->start);
501 off = zone->off + sizeof(*oldbuf);
502
503 /* get part of data */
504 rcnt = info->read(buf, len - start, off + start);
505 if (rcnt != len - start) {
506 pr_err("read zone %s failed\n", zone->name);
507 ret = (int)rcnt < 0 ? (int)rcnt : -EIO;
508 goto free_oldbuf;
509 }
510
511 /* get the rest of data */
512 rcnt = info->read(buf + len - start, start, off);
513 if (rcnt != start) {
514 pr_err("read zone %s failed\n", zone->name);
515 ret = (int)rcnt < 0 ? (int)rcnt : -EIO;
516 goto free_oldbuf;
517 }
518
519 zone->oldbuf = oldbuf;
520 psz_flush_dirty_zone(zone);
521 return 0;
522
523free_oldbuf:
524 kfree(oldbuf);
525 return ret;
526}
527
d26c3321
WL
528/**
529 * psz_recovery() - recover data from storage
530 * @cxt: the context of pstore/zone
531 *
532 * recovery means reading data back from storage after rebooting
533 *
534 * Return: 0 on success, others on failure.
535 */
536static inline int psz_recovery(struct psz_context *cxt)
537{
538 int ret;
539
540 if (atomic_read(&cxt->recovered))
541 return 0;
542
543 ret = psz_kmsg_recover(cxt);
0dc06826
WL
544 if (ret)
545 goto out;
d26c3321 546
0dc06826
WL
547 ret = psz_recover_zone(cxt, cxt->ppsz);
548
549out:
d26c3321
WL
550 if (unlikely(ret))
551 pr_err("recover failed\n");
552 else {
553 pr_debug("recover end!\n");
554 atomic_set(&cxt->recovered, 1);
555 }
556 return ret;
557}
558
559static int psz_pstore_open(struct pstore_info *psi)
560{
561 struct psz_context *cxt = psi->data;
562
563 cxt->kmsg_read_cnt = 0;
0dc06826 564 cxt->pmsg_read_cnt = 0;
d26c3321
WL
565 return 0;
566}
567
0dc06826
WL
568static inline bool psz_old_ok(struct pstore_zone *zone)
569{
570 if (zone && zone->oldbuf && atomic_read(&zone->oldbuf->datalen))
571 return true;
572 return false;
573}
574
d26c3321
WL
575static inline bool psz_ok(struct pstore_zone *zone)
576{
577 if (zone && zone->buffer && buffer_datalen(zone))
578 return true;
579 return false;
580}
581
582static inline int psz_kmsg_erase(struct psz_context *cxt,
583 struct pstore_zone *zone, struct pstore_record *record)
584{
585 struct psz_buffer *buffer = zone->buffer;
586 struct psz_kmsg_header *hdr =
587 (struct psz_kmsg_header *)buffer->data;
588
589 if (unlikely(!psz_ok(zone)))
590 return 0;
591 /* this zone is already updated, no need to erase */
592 if (record->count != hdr->counter)
593 return 0;
594
595 atomic_set(&zone->buffer->datalen, 0);
596 return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
597}
598
0dc06826
WL
599static inline int psz_record_erase(struct psz_context *cxt,
600 struct pstore_zone *zone)
601{
602 if (unlikely(!psz_old_ok(zone)))
603 return 0;
604
605 kfree(zone->oldbuf);
606 zone->oldbuf = NULL;
607 /*
608 * if there are new data in zone buffer, that means the old data
609 * are already invalid. It is no need to flush 0 (erase) to
610 * block device.
611 */
612 if (!buffer_datalen(zone))
613 return psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
614 psz_flush_dirty_zone(zone);
615 return 0;
616}
617
d26c3321
WL
618static int psz_pstore_erase(struct pstore_record *record)
619{
620 struct psz_context *cxt = record->psi->data;
621
622 switch (record->type) {
623 case PSTORE_TYPE_DMESG:
624 if (record->id >= cxt->kmsg_max_cnt)
625 return -EINVAL;
626 return psz_kmsg_erase(cxt, cxt->kpszs[record->id], record);
0dc06826
WL
627 case PSTORE_TYPE_PMSG:
628 return psz_record_erase(cxt, cxt->ppsz);
d26c3321
WL
629 default:
630 return -EINVAL;
631 }
632}
633
634static void psz_write_kmsg_hdr(struct pstore_zone *zone,
635 struct pstore_record *record)
636{
637 struct psz_context *cxt = record->psi->data;
638 struct psz_buffer *buffer = zone->buffer;
639 struct psz_kmsg_header *hdr =
640 (struct psz_kmsg_header *)buffer->data;
641
642 hdr->magic = PSTORE_KMSG_HEADER_MAGIC;
643 hdr->compressed = record->compressed;
644 hdr->time.tv_sec = record->time.tv_sec;
645 hdr->time.tv_nsec = record->time.tv_nsec;
646 hdr->reason = record->reason;
647 if (hdr->reason == KMSG_DUMP_OOPS)
648 hdr->counter = ++cxt->oops_counter;
649 else if (hdr->reason == KMSG_DUMP_PANIC)
650 hdr->counter = ++cxt->panic_counter;
651 else
652 hdr->counter = 0;
653}
654
655static inline int notrace psz_kmsg_write_record(struct psz_context *cxt,
656 struct pstore_record *record)
657{
658 size_t size, hlen;
659 struct pstore_zone *zone;
660 unsigned int zonenum;
661
662 zonenum = cxt->kmsg_write_cnt;
663 zone = cxt->kpszs[zonenum];
664 if (unlikely(!zone))
665 return -ENOSPC;
666 cxt->kmsg_write_cnt = (zonenum + 1) % cxt->kmsg_max_cnt;
667
668 pr_debug("write %s to zone id %d\n", zone->name, zonenum);
669 psz_write_kmsg_hdr(zone, record);
670 hlen = sizeof(struct psz_kmsg_header);
671 size = min_t(size_t, record->size, zone->buffer_size - hlen);
672 return psz_zone_write(zone, FLUSH_ALL, record->buf, size, hlen);
673}
674
675static int notrace psz_kmsg_write(struct psz_context *cxt,
676 struct pstore_record *record)
677{
678 int ret;
679
680 /*
681 * Explicitly only take the first part of any new crash.
682 * If our buffer is larger than kmsg_bytes, this can never happen,
683 * and if our buffer is smaller than kmsg_bytes, we don't want the
684 * report split across multiple records.
685 */
686 if (record->part != 1)
687 return -ENOSPC;
688
689 if (!cxt->kpszs)
690 return -ENOSPC;
691
692 ret = psz_kmsg_write_record(cxt, record);
693 if (!ret) {
694 pr_debug("try to flush other dirty zones\n");
695 psz_flush_dirty_zones(cxt->kpszs, cxt->kmsg_max_cnt);
696 }
697
698 /* always return 0 as we had handled it on buffer */
699 return 0;
700}
701
0dc06826
WL
702static int notrace psz_record_write(struct pstore_zone *zone,
703 struct pstore_record *record)
704{
705 size_t start, rem;
706 bool is_full_data = false;
707 char *buf;
708 int cnt;
709
710 if (!zone || !record)
711 return -ENOSPC;
712
713 if (atomic_read(&zone->buffer->datalen) >= zone->buffer_size)
714 is_full_data = true;
715
716 cnt = record->size;
717 buf = record->buf;
718 if (unlikely(cnt > zone->buffer_size)) {
719 buf += cnt - zone->buffer_size;
720 cnt = zone->buffer_size;
721 }
722
723 start = buffer_start(zone);
724 rem = zone->buffer_size - start;
725 if (unlikely(rem < cnt)) {
726 psz_zone_write(zone, FLUSH_PART, buf, rem, start);
727 buf += rem;
728 cnt -= rem;
729 start = 0;
730 is_full_data = true;
731 }
732
733 atomic_set(&zone->buffer->start, cnt + start);
734 psz_zone_write(zone, FLUSH_PART, buf, cnt, start);
735
736 /**
737 * psz_zone_write will set datalen as start + cnt.
738 * It work if actual data length lesser than buffer size.
739 * If data length greater than buffer size, pmsg will rewrite to
740 * beginning of zone, which make buffer->datalen wrongly.
741 * So we should reset datalen as buffer size once actual data length
742 * greater than buffer size.
743 */
744 if (is_full_data) {
745 atomic_set(&zone->buffer->datalen, zone->buffer_size);
746 psz_zone_write(zone, FLUSH_META, NULL, 0, 0);
747 }
748 return 0;
749}
750
d26c3321
WL
751static int notrace psz_pstore_write(struct pstore_record *record)
752{
753 struct psz_context *cxt = record->psi->data;
754
755 if (record->type == PSTORE_TYPE_DMESG &&
756 record->reason == KMSG_DUMP_PANIC)
757 atomic_set(&cxt->on_panic, 1);
758
759 switch (record->type) {
760 case PSTORE_TYPE_DMESG:
761 return psz_kmsg_write(cxt, record);
0dc06826
WL
762 case PSTORE_TYPE_PMSG:
763 return psz_record_write(cxt->ppsz, record);
d26c3321
WL
764 default:
765 return -EINVAL;
766 }
767}
768
769static struct pstore_zone *psz_read_next_zone(struct psz_context *cxt)
770{
771 struct pstore_zone *zone = NULL;
772
773 while (cxt->kmsg_read_cnt < cxt->kmsg_max_cnt) {
774 zone = cxt->kpszs[cxt->kmsg_read_cnt++];
775 if (psz_ok(zone))
776 return zone;
777 }
778
0dc06826
WL
779 if (cxt->pmsg_read_cnt == 0) {
780 cxt->pmsg_read_cnt++;
781 zone = cxt->ppsz;
782 if (psz_old_ok(zone))
783 return zone;
784 }
785
d26c3321
WL
786 return NULL;
787}
788
789static int psz_kmsg_read_hdr(struct pstore_zone *zone,
790 struct pstore_record *record)
791{
792 struct psz_buffer *buffer = zone->buffer;
793 struct psz_kmsg_header *hdr =
794 (struct psz_kmsg_header *)buffer->data;
795
796 if (hdr->magic != PSTORE_KMSG_HEADER_MAGIC)
797 return -EINVAL;
798 record->compressed = hdr->compressed;
799 record->time.tv_sec = hdr->time.tv_sec;
800 record->time.tv_nsec = hdr->time.tv_nsec;
801 record->reason = hdr->reason;
802 record->count = hdr->counter;
803 return 0;
804}
805
806static ssize_t psz_kmsg_read(struct pstore_zone *zone,
807 struct pstore_record *record)
808{
809 ssize_t size, hlen = 0;
810
811 size = buffer_datalen(zone);
812 /* Clear and skip this kmsg dump record if it has no valid header */
813 if (psz_kmsg_read_hdr(zone, record)) {
814 atomic_set(&zone->buffer->datalen, 0);
815 atomic_set(&zone->dirty, 0);
816 return -ENOMSG;
817 }
818 size -= sizeof(struct psz_kmsg_header);
819
820 if (!record->compressed) {
821 char *buf = kasprintf(GFP_KERNEL, "%s: Total %d times\n",
822 kmsg_dump_reason_str(record->reason),
823 record->count);
824 hlen = strlen(buf);
825 record->buf = krealloc(buf, hlen + size, GFP_KERNEL);
826 if (!record->buf) {
827 kfree(buf);
828 return -ENOMEM;
829 }
830 } else {
831 record->buf = kmalloc(size, GFP_KERNEL);
832 if (!record->buf)
833 return -ENOMEM;
834 }
835
0dc06826 836 size = psz_zone_read_buffer(zone, record->buf + hlen, size,
d26c3321
WL
837 sizeof(struct psz_kmsg_header));
838 if (unlikely(size < 0)) {
839 kfree(record->buf);
840 return -ENOMSG;
841 }
842
843 return size + hlen;
844}
845
0dc06826
WL
846static ssize_t psz_record_read(struct pstore_zone *zone,
847 struct pstore_record *record)
848{
849 size_t len;
850 struct psz_buffer *buf;
851
852 if (!zone || !record)
853 return -ENOSPC;
854
855 buf = (struct psz_buffer *)zone->oldbuf;
856 if (!buf)
857 return -ENOMSG;
858
859 len = atomic_read(&buf->datalen);
860 record->buf = kmalloc(len, GFP_KERNEL);
861 if (!record->buf)
862 return -ENOMEM;
863
864 if (unlikely(psz_zone_read_oldbuf(zone, record->buf, len, 0))) {
865 kfree(record->buf);
866 return -ENOMSG;
867 }
868
869 return len;
870}
871
d26c3321
WL
872static ssize_t psz_pstore_read(struct pstore_record *record)
873{
874 struct psz_context *cxt = record->psi->data;
875 ssize_t (*readop)(struct pstore_zone *zone,
876 struct pstore_record *record);
877 struct pstore_zone *zone;
878 ssize_t ret;
879
880 /* before read, we must recover from storage */
881 ret = psz_recovery(cxt);
882 if (ret)
883 return ret;
884
885next_zone:
886 zone = psz_read_next_zone(cxt);
887 if (!zone)
888 return 0;
889
890 record->type = zone->type;
891 switch (record->type) {
892 case PSTORE_TYPE_DMESG:
893 readop = psz_kmsg_read;
894 record->id = cxt->kmsg_read_cnt - 1;
895 break;
0dc06826
WL
896 case PSTORE_TYPE_PMSG:
897 readop = psz_record_read;
898 break;
d26c3321
WL
899 default:
900 goto next_zone;
901 }
902
903 ret = readop(zone, record);
904 if (ret == -ENOMSG)
905 goto next_zone;
906 return ret;
907}
908
909static struct psz_context pstore_zone_cxt = {
910 .pstore_zone_info_lock =
911 __MUTEX_INITIALIZER(pstore_zone_cxt.pstore_zone_info_lock),
912 .recovered = ATOMIC_INIT(0),
913 .on_panic = ATOMIC_INIT(0),
914 .pstore = {
915 .owner = THIS_MODULE,
916 .open = psz_pstore_open,
917 .read = psz_pstore_read,
918 .write = psz_pstore_write,
919 .erase = psz_pstore_erase,
920 },
921};
922
923static void psz_free_zone(struct pstore_zone **pszone)
924{
925 struct pstore_zone *zone = *pszone;
926
927 if (!zone)
928 return;
929
930 kfree(zone->buffer);
931 kfree(zone);
932 *pszone = NULL;
933}
934
935static void psz_free_zones(struct pstore_zone ***pszones, unsigned int *cnt)
936{
937 struct pstore_zone **zones = *pszones;
938
939 if (!zones)
940 return;
941
942 while (*cnt > 0) {
943 (*cnt)--;
944 psz_free_zone(&(zones[*cnt]));
945 }
946 kfree(zones);
947 *pszones = NULL;
948}
949
950static void psz_free_all_zones(struct psz_context *cxt)
951{
952 if (cxt->kpszs)
953 psz_free_zones(&cxt->kpszs, &cxt->kmsg_max_cnt);
0dc06826
WL
954 if (cxt->ppsz)
955 psz_free_zone(&cxt->ppsz);
d26c3321
WL
956}
957
958static struct pstore_zone *psz_init_zone(enum pstore_type_id type,
959 loff_t *off, size_t size)
960{
961 struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
962 struct pstore_zone *zone;
963 const char *name = pstore_type_to_name(type);
964
965 if (!size)
966 return NULL;
967
968 if (*off + size > info->total_size) {
969 pr_err("no room for %s (0x%zx@0x%llx over 0x%lx)\n",
970 name, size, *off, info->total_size);
971 return ERR_PTR(-ENOMEM);
972 }
973
974 zone = kzalloc(sizeof(struct pstore_zone), GFP_KERNEL);
975 if (!zone)
976 return ERR_PTR(-ENOMEM);
977
978 zone->buffer = kmalloc(size, GFP_KERNEL);
979 if (!zone->buffer) {
980 kfree(zone);
981 return ERR_PTR(-ENOMEM);
982 }
983 memset(zone->buffer, 0xFF, size);
984 zone->off = *off;
985 zone->name = name;
986 zone->type = type;
987 zone->buffer_size = size - sizeof(struct psz_buffer);
988 zone->buffer->sig = type ^ PSZ_SIG;
0dc06826 989 zone->oldbuf = NULL;
d26c3321
WL
990 atomic_set(&zone->dirty, 0);
991 atomic_set(&zone->buffer->datalen, 0);
0dc06826 992 atomic_set(&zone->buffer->start, 0);
d26c3321
WL
993
994 *off += size;
995
996 pr_debug("pszone %s: off 0x%llx, %zu header, %zu data\n", zone->name,
997 zone->off, sizeof(*zone->buffer), zone->buffer_size);
998 return zone;
999}
1000
1001static struct pstore_zone **psz_init_zones(enum pstore_type_id type,
1002 loff_t *off, size_t total_size, ssize_t record_size,
1003 unsigned int *cnt)
1004{
1005 struct pstore_zone_info *info = pstore_zone_cxt.pstore_zone_info;
1006 struct pstore_zone **zones, *zone;
1007 const char *name = pstore_type_to_name(type);
1008 int c, i;
1009
1010 *cnt = 0;
1011 if (!total_size || !record_size)
1012 return NULL;
1013
1014 if (*off + total_size > info->total_size) {
1015 pr_err("no room for zones %s (0x%zx@0x%llx over 0x%lx)\n",
1016 name, total_size, *off, info->total_size);
1017 return ERR_PTR(-ENOMEM);
1018 }
1019
1020 c = total_size / record_size;
1021 zones = kcalloc(c, sizeof(*zones), GFP_KERNEL);
1022 if (!zones) {
1023 pr_err("allocate for zones %s failed\n", name);
1024 return ERR_PTR(-ENOMEM);
1025 }
1026 memset(zones, 0, c * sizeof(*zones));
1027
1028 for (i = 0; i < c; i++) {
1029 zone = psz_init_zone(type, off, record_size);
1030 if (!zone || IS_ERR(zone)) {
1031 pr_err("initialize zones %s failed\n", name);
1032 psz_free_zones(&zones, &i);
1033 return (void *)zone;
1034 }
1035 zones[i] = zone;
1036 }
1037
1038 *cnt = c;
1039 return zones;
1040}
1041
1042static int psz_alloc_zones(struct psz_context *cxt)
1043{
1044 struct pstore_zone_info *info = cxt->pstore_zone_info;
1045 loff_t off = 0;
1046 int err;
0dc06826
WL
1047 size_t off_size = 0;
1048
1049 off_size += info->pmsg_size;
1050 cxt->ppsz = psz_init_zone(PSTORE_TYPE_PMSG, &off, info->pmsg_size);
1051 if (IS_ERR(cxt->ppsz)) {
1052 err = PTR_ERR(cxt->ppsz);
1053 cxt->ppsz = NULL;
1054 goto free_out;
1055 }
d26c3321 1056
0dc06826
WL
1057 cxt->kpszs = psz_init_zones(PSTORE_TYPE_DMESG, &off,
1058 info->total_size - off_size,
d26c3321
WL
1059 info->kmsg_size, &cxt->kmsg_max_cnt);
1060 if (IS_ERR(cxt->kpszs)) {
1061 err = PTR_ERR(cxt->kpszs);
1062 cxt->kpszs = NULL;
0dc06826 1063 goto free_out;
d26c3321
WL
1064 }
1065
1066 return 0;
0dc06826
WL
1067free_out:
1068 psz_free_all_zones(cxt);
d26c3321
WL
1069 return err;
1070}
1071
1072/**
1073 * register_pstore_zone() - register to pstore/zone
1074 *
1075 * @info: back-end driver information. See &struct pstore_zone_info.
1076 *
1077 * Only one back-end at one time.
1078 *
1079 * Return: 0 on success, others on failure.
1080 */
1081int register_pstore_zone(struct pstore_zone_info *info)
1082{
1083 int err = -EINVAL;
1084 struct psz_context *cxt = &pstore_zone_cxt;
1085
1086 if (info->total_size < 4096) {
1087 pr_warn("total_size must be >= 4096\n");
1088 return -EINVAL;
1089 }
1090
0dc06826 1091 if (!info->kmsg_size && !info->pmsg_size) {
d26c3321
WL
1092 pr_warn("at least one record size must be non-zero\n");
1093 return -EINVAL;
1094 }
1095
1096 if (!info->name || !info->name[0])
1097 return -EINVAL;
1098
1099#define check_size(name, size) { \
1100 if (info->name > 0 && info->name < (size)) { \
1101 pr_err(#name " must be over %d\n", (size)); \
1102 return -EINVAL; \
1103 } \
1104 if (info->name & (size - 1)) { \
1105 pr_err(#name " must be a multiple of %d\n", \
1106 (size)); \
1107 return -EINVAL; \
1108 } \
1109 }
1110
1111 check_size(total_size, 4096);
1112 check_size(kmsg_size, SECTOR_SIZE);
0dc06826 1113 check_size(pmsg_size, SECTOR_SIZE);
d26c3321
WL
1114
1115#undef check_size
1116
1117 /*
1118 * the @read and @write must be applied.
1119 * if no @read, pstore may mount failed.
1120 * if no @write, pstore do not support to remove record file.
1121 */
1122 if (!info->read || !info->write) {
1123 pr_err("no valid general read/write interface\n");
1124 return -EINVAL;
1125 }
1126
1127 mutex_lock(&cxt->pstore_zone_info_lock);
1128 if (cxt->pstore_zone_info) {
1129 pr_warn("'%s' already loaded: ignoring '%s'\n",
1130 cxt->pstore_zone_info->name, info->name);
1131 mutex_unlock(&cxt->pstore_zone_info_lock);
1132 return -EBUSY;
1133 }
1134 cxt->pstore_zone_info = info;
1135
1136 pr_debug("register %s with properties:\n", info->name);
1137 pr_debug("\ttotal size : %ld Bytes\n", info->total_size);
1138 pr_debug("\tkmsg size : %ld Bytes\n", info->kmsg_size);
0dc06826 1139 pr_debug("\tpmsg size : %ld Bytes\n", info->pmsg_size);
d26c3321
WL
1140
1141 err = psz_alloc_zones(cxt);
1142 if (err) {
1143 pr_err("alloc zones failed\n");
1144 goto fail_out;
1145 }
1146
1147 if (info->kmsg_size) {
1148 cxt->pstore.bufsize = cxt->kpszs[0]->buffer_size -
1149 sizeof(struct psz_kmsg_header);
1150 cxt->pstore.buf = kzalloc(cxt->pstore.bufsize, GFP_KERNEL);
1151 if (!cxt->pstore.buf) {
1152 err = -ENOMEM;
1153 goto fail_free;
1154 }
1155 }
1156 cxt->pstore.data = cxt;
1157
1158 pr_info("registered %s as backend for", info->name);
1159 cxt->pstore.max_reason = info->max_reason;
1160 cxt->pstore.name = info->name;
1161 if (info->kmsg_size) {
1162 cxt->pstore.flags |= PSTORE_FLAGS_DMESG;
1163 pr_cont(" kmsg(%s",
1164 kmsg_dump_reason_str(cxt->pstore.max_reason));
1165 if (cxt->pstore_zone_info->panic_write)
1166 pr_cont(",panic_write");
1167 pr_cont(")");
1168 }
0dc06826
WL
1169 if (info->pmsg_size) {
1170 cxt->pstore.flags |= PSTORE_FLAGS_PMSG;
1171 pr_cont(" pmsg");
1172 }
d26c3321
WL
1173 pr_cont("\n");
1174
1175 err = pstore_register(&cxt->pstore);
1176 if (err) {
1177 pr_err("registering with pstore failed\n");
1178 goto fail_free;
1179 }
1180 mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
1181
1182 return 0;
1183
1184fail_free:
1185 kfree(cxt->pstore.buf);
1186 cxt->pstore.buf = NULL;
1187 cxt->pstore.bufsize = 0;
1188 psz_free_all_zones(cxt);
1189fail_out:
1190 pstore_zone_cxt.pstore_zone_info = NULL;
1191 mutex_unlock(&pstore_zone_cxt.pstore_zone_info_lock);
1192 return err;
1193}
1194EXPORT_SYMBOL_GPL(register_pstore_zone);
1195
1196/**
1197 * unregister_pstore_zone() - unregister to pstore/zone
1198 *
1199 * @info: back-end driver information. See struct pstore_zone_info.
1200 */
1201void unregister_pstore_zone(struct pstore_zone_info *info)
1202{
1203 struct psz_context *cxt = &pstore_zone_cxt;
1204
1205 mutex_lock(&cxt->pstore_zone_info_lock);
1206 if (!cxt->pstore_zone_info) {
1207 mutex_unlock(&cxt->pstore_zone_info_lock);
1208 return;
1209 }
1210
1211 /* Stop incoming writes from pstore. */
1212 pstore_unregister(&cxt->pstore);
1213
1214 /* Clean up allocations. */
1215 kfree(cxt->pstore.buf);
1216 cxt->pstore.buf = NULL;
1217 cxt->pstore.bufsize = 0;
1218 cxt->pstore_zone_info = NULL;
1219
1220 psz_free_all_zones(cxt);
1221
1222 /* Clear counters and zone state. */
1223 cxt->oops_counter = 0;
1224 cxt->panic_counter = 0;
1225 atomic_set(&cxt->recovered, 0);
1226 atomic_set(&cxt->on_panic, 0);
1227
1228 mutex_unlock(&cxt->pstore_zone_info_lock);
1229}
1230EXPORT_SYMBOL_GPL(unregister_pstore_zone);
1231
1232MODULE_LICENSE("GPL");
1233MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
1234MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
1235MODULE_DESCRIPTION("Storage Manager for pstore/blk");