1 // SPDX-License-Identifier: GPL-2.0
3 #define dev_fmt(fmt) "mtdoops-pstore: " fmt
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/pstore_blk.h>
8 #include <linux/mtd/mtd.h>
9 #include <linux/bitops.h>
11 static struct mtdpstore_context {
13 struct pstore_blk_config info;
14 struct pstore_device_info dev;
16 unsigned long *rmmap; /* removed bit map */
17 unsigned long *usedmap; /* used bit map */
19 * used for panic write
20 * As there are no block_isbad for panic case, we should keep this
21 * status before panic to ensure panic_write not failed.
23 unsigned long *badmap; /* bad block bit map */
26 static int mtdpstore_block_isbad(struct mtdpstore_context *cxt, loff_t off)
29 struct mtd_info *mtd = cxt->mtd;
32 off = ALIGN_DOWN(off, mtd->erasesize);
33 blknum = div_u64(off, mtd->erasesize);
35 if (test_bit(blknum, cxt->badmap))
37 ret = mtd_block_isbad(mtd, off);
39 dev_err(&mtd->dev, "mtd_block_isbad failed, aborting\n");
42 set_bit(blknum, cxt->badmap);
48 static inline int mtdpstore_panic_block_isbad(struct mtdpstore_context *cxt,
51 struct mtd_info *mtd = cxt->mtd;
54 off = ALIGN_DOWN(off, mtd->erasesize);
55 blknum = div_u64(off, mtd->erasesize);
56 return test_bit(blknum, cxt->badmap);
59 static inline void mtdpstore_mark_used(struct mtdpstore_context *cxt,
62 struct mtd_info *mtd = cxt->mtd;
63 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
65 dev_dbg(&mtd->dev, "mark zone %llu used\n", zonenum);
66 set_bit(zonenum, cxt->usedmap);
69 static inline void mtdpstore_mark_unused(struct mtdpstore_context *cxt,
72 struct mtd_info *mtd = cxt->mtd;
73 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
75 dev_dbg(&mtd->dev, "mark zone %llu unused\n", zonenum);
76 clear_bit(zonenum, cxt->usedmap);
79 static inline void mtdpstore_block_mark_unused(struct mtdpstore_context *cxt,
82 struct mtd_info *mtd = cxt->mtd;
83 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
86 off = ALIGN_DOWN(off, mtd->erasesize);
87 zonenum = div_u64(off, cxt->info.kmsg_size);
89 dev_dbg(&mtd->dev, "mark zone %llu unused\n", zonenum);
90 clear_bit(zonenum, cxt->usedmap);
96 static inline int mtdpstore_is_used(struct mtdpstore_context *cxt, loff_t off)
98 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
99 u64 blknum = div_u64(off, cxt->mtd->erasesize);
101 if (test_bit(blknum, cxt->badmap))
103 return test_bit(zonenum, cxt->usedmap);
106 static int mtdpstore_block_is_used(struct mtdpstore_context *cxt,
109 struct mtd_info *mtd = cxt->mtd;
110 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
113 off = ALIGN_DOWN(off, mtd->erasesize);
114 zonenum = div_u64(off, cxt->info.kmsg_size);
115 while (zonecnt > 0) {
116 if (test_bit(zonenum, cxt->usedmap))
124 static int mtdpstore_is_empty(struct mtdpstore_context *cxt, char *buf,
127 struct mtd_info *mtd = cxt->mtd;
131 sz = min_t(uint32_t, size, mtd->writesize / 4);
132 for (i = 0; i < sz; i++) {
133 if (buf[i] != (char)0xFF)
139 static void mtdpstore_mark_removed(struct mtdpstore_context *cxt, loff_t off)
141 struct mtd_info *mtd = cxt->mtd;
142 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
144 dev_dbg(&mtd->dev, "mark zone %llu removed\n", zonenum);
145 set_bit(zonenum, cxt->rmmap);
148 static void mtdpstore_block_clear_removed(struct mtdpstore_context *cxt,
151 struct mtd_info *mtd = cxt->mtd;
152 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
155 off = ALIGN_DOWN(off, mtd->erasesize);
156 zonenum = div_u64(off, cxt->info.kmsg_size);
157 while (zonecnt > 0) {
158 clear_bit(zonenum, cxt->rmmap);
164 static int mtdpstore_block_is_removed(struct mtdpstore_context *cxt,
167 struct mtd_info *mtd = cxt->mtd;
168 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
171 off = ALIGN_DOWN(off, mtd->erasesize);
172 zonenum = div_u64(off, cxt->info.kmsg_size);
173 while (zonecnt > 0) {
174 if (test_bit(zonenum, cxt->rmmap))
182 static int mtdpstore_erase_do(struct mtdpstore_context *cxt, loff_t off)
184 struct mtd_info *mtd = cxt->mtd;
185 struct erase_info erase;
188 off = ALIGN_DOWN(off, cxt->mtd->erasesize);
189 dev_dbg(&mtd->dev, "try to erase off 0x%llx\n", off);
190 erase.len = cxt->mtd->erasesize;
192 ret = mtd_erase(cxt->mtd, &erase);
194 mtdpstore_block_clear_removed(cxt, off);
196 dev_err(&mtd->dev, "erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
197 (unsigned long long)erase.addr,
198 (unsigned long long)erase.len, cxt->info.device);
203 * called while removing file
205 * Avoiding over erasing, do erase block only when the whole block is unused.
206 * If the block contains valid log, do erase lazily on flush_removed() when
209 static ssize_t mtdpstore_erase(size_t size, loff_t off)
211 struct mtdpstore_context *cxt = &oops_cxt;
213 if (mtdpstore_block_isbad(cxt, off))
216 mtdpstore_mark_unused(cxt, off);
218 /* If the block still has valid data, mtdpstore do erase lazily */
219 if (likely(mtdpstore_block_is_used(cxt, off))) {
220 mtdpstore_mark_removed(cxt, off);
224 /* all zones are unused, erase it */
225 return mtdpstore_erase_do(cxt, off);
229 * What is security for mtdpstore?
230 * As there is no erase for panic case, we should ensure at least one zone
231 * is writable. Otherwise, panic write will fail.
232 * If zone is used, write operation will return -ENOMSG, which means that
233 * pstore/blk will try one by one until gets an empty zone. So, it is not
234 * needed to ensure the next zone is empty, but at least one.
236 static int mtdpstore_security(struct mtdpstore_context *cxt, loff_t off)
239 struct mtd_info *mtd = cxt->mtd;
240 u32 zonenum = (u32)div_u64(off, cxt->info.kmsg_size);
241 u32 zonecnt = (u32)div_u64(cxt->mtd->size, cxt->info.kmsg_size);
242 u32 blkcnt = (u32)div_u64(cxt->mtd->size, cxt->mtd->erasesize);
243 u32 erasesize = cxt->mtd->erasesize;
245 for (i = 0; i < zonecnt; i++) {
246 u32 num = (zonenum + i) % zonecnt;
248 /* found empty zone */
249 if (!test_bit(num, cxt->usedmap))
253 /* If there is no any empty zone, we have no way but to do erase */
255 div64_u64_rem(off + erasesize, cxt->mtd->size, (u64 *)&off);
257 if (mtdpstore_block_isbad(cxt, off))
260 ret = mtdpstore_erase_do(cxt, off);
262 mtdpstore_block_mark_unused(cxt, off);
268 dev_err(&mtd->dev, "all blocks bad!\n");
269 dev_dbg(&mtd->dev, "end security\n");
273 static ssize_t mtdpstore_write(const char *buf, size_t size, loff_t off)
275 struct mtdpstore_context *cxt = &oops_cxt;
276 struct mtd_info *mtd = cxt->mtd;
280 if (mtdpstore_block_isbad(cxt, off))
283 /* zone is used, please try next one */
284 if (mtdpstore_is_used(cxt, off))
287 dev_dbg(&mtd->dev, "try to write off 0x%llx size %zu\n", off, size);
288 ret = mtd_write(cxt->mtd, off, size, &retlen, (u_char *)buf);
289 if (ret < 0 || retlen != size) {
290 dev_err(&mtd->dev, "write failure at %lld (%zu of %zu written), err %d\n",
291 off, retlen, size, ret);
294 mtdpstore_mark_used(cxt, off);
296 mtdpstore_security(cxt, off);
300 static inline bool mtdpstore_is_io_error(int ret)
302 return ret < 0 && !mtd_is_bitflip(ret) && !mtd_is_eccerr(ret);
306 * All zones will be read as pstore/blk will read zone one by one when do
309 static ssize_t mtdpstore_read(char *buf, size_t size, loff_t off)
311 struct mtdpstore_context *cxt = &oops_cxt;
312 struct mtd_info *mtd = cxt->mtd;
316 if (mtdpstore_block_isbad(cxt, off))
319 dev_dbg(&mtd->dev, "try to read off 0x%llx size %zu\n", off, size);
320 for (done = 0, retlen = 0; done < size; done += retlen) {
323 ret = mtd_read(cxt->mtd, off + done, size - done, &retlen,
324 (u_char *)buf + done);
325 if (mtdpstore_is_io_error(ret)) {
326 dev_err(&mtd->dev, "read failure at %lld (%zu of %zu read), err %d\n",
327 off + done, retlen, size - done, ret);
328 /* the zone may be broken, try next one */
333 * ECC error. The impact on log data is so small. Maybe we can
334 * still read it and try to understand. So mtdpstore just hands
335 * over what it gets and user can judge whether the data is
338 if (mtd_is_eccerr(ret)) {
339 dev_err(&mtd->dev, "ecc error at %lld (%zu of %zu read), err %d\n",
340 off + done, retlen, size - done, ret);
341 /* driver may not set retlen when ecc error */
342 retlen = retlen == 0 ? size - done : retlen;
346 if (mtdpstore_is_empty(cxt, buf, size))
347 mtdpstore_mark_unused(cxt, off);
349 mtdpstore_mark_used(cxt, off);
351 mtdpstore_security(cxt, off);
355 static ssize_t mtdpstore_panic_write(const char *buf, size_t size, loff_t off)
357 struct mtdpstore_context *cxt = &oops_cxt;
358 struct mtd_info *mtd = cxt->mtd;
362 if (mtdpstore_panic_block_isbad(cxt, off))
365 /* zone is used, please try next one */
366 if (mtdpstore_is_used(cxt, off))
369 ret = mtd_panic_write(cxt->mtd, off, size, &retlen, (u_char *)buf);
370 if (ret < 0 || size != retlen) {
371 dev_err(&mtd->dev, "panic write failure at %lld (%zu of %zu read), err %d\n",
372 off, retlen, size, ret);
375 mtdpstore_mark_used(cxt, off);
380 static void mtdpstore_notify_add(struct mtd_info *mtd)
383 struct mtdpstore_context *cxt = &oops_cxt;
384 struct pstore_blk_config *info = &cxt->info;
385 unsigned long longcnt;
387 if (!strcmp(mtd->name, info->device))
388 cxt->index = mtd->index;
390 if (mtd->index != cxt->index || cxt->index < 0)
393 dev_dbg(&mtd->dev, "found matching MTD device %s\n", mtd->name);
395 if (mtd->size < info->kmsg_size * 2) {
396 dev_err(&mtd->dev, "MTD partition %d not big enough\n",
401 * kmsg_size must be aligned to 4096 Bytes, which is limited by
402 * psblk. The default value of kmsg_size is 64KB. If kmsg_size
403 * is larger than erasesize, some errors will occur since mtdpsotre
406 if (mtd->erasesize < info->kmsg_size) {
407 dev_err(&mtd->dev, "eraseblock size of MTD partition %d too small\n",
411 if (unlikely(info->kmsg_size % mtd->writesize)) {
412 dev_err(&mtd->dev, "record size %lu KB must align to write size %d KB\n",
413 info->kmsg_size / 1024,
414 mtd->writesize / 1024);
418 longcnt = BITS_TO_LONGS(div_u64(mtd->size, info->kmsg_size));
419 cxt->rmmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
420 cxt->usedmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
422 longcnt = BITS_TO_LONGS(div_u64(mtd->size, mtd->erasesize));
423 cxt->badmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
425 cxt->dev.total_size = mtd->size;
426 /* just support dmesg right now */
427 cxt->dev.flags = PSTORE_FLAGS_DMESG;
428 cxt->dev.read = mtdpstore_read;
429 cxt->dev.write = mtdpstore_write;
430 cxt->dev.erase = mtdpstore_erase;
431 cxt->dev.panic_write = mtdpstore_panic_write;
433 ret = register_pstore_device(&cxt->dev);
435 dev_err(&mtd->dev, "mtd%d register to psblk failed\n",
440 dev_info(&mtd->dev, "Attached to MTD device %d\n", mtd->index);
443 static int mtdpstore_flush_removed_do(struct mtdpstore_context *cxt,
444 loff_t off, size_t size)
446 struct mtd_info *mtd = cxt->mtd;
450 struct erase_info erase;
452 buf = kmalloc(mtd->erasesize, GFP_KERNEL);
456 /* 1st. read to cache */
457 ret = mtd_read(mtd, off, mtd->erasesize, &retlen, buf);
458 if (mtdpstore_is_io_error(ret))
461 /* 2nd. erase block */
462 erase.len = mtd->erasesize;
464 ret = mtd_erase(mtd, &erase);
468 /* 3rd. write back */
470 unsigned int zonesize = cxt->info.kmsg_size;
472 /* there is valid data on block, write back */
473 if (mtdpstore_is_used(cxt, off)) {
474 ret = mtd_write(mtd, off, zonesize, &retlen, buf);
476 dev_err(&mtd->dev, "write failure at %lld (%zu of %u written), err %d\n",
477 off, retlen, zonesize, ret);
481 size -= min_t(unsigned int, zonesize, size);
490 * What does mtdpstore_flush_removed() do?
491 * When user remove any log file on pstore filesystem, mtdpstore should do
492 * something to ensure log file removed. If the whole block is no longer used,
493 * it's nice to erase the block. However if the block still contains valid log,
494 * what mtdpstore can do is to erase and write the valid log back.
496 static int mtdpstore_flush_removed(struct mtdpstore_context *cxt)
498 struct mtd_info *mtd = cxt->mtd;
501 u32 blkcnt = (u32)div_u64(mtd->size, mtd->erasesize);
503 for (off = 0; blkcnt > 0; blkcnt--, off += mtd->erasesize) {
504 ret = mtdpstore_block_isbad(cxt, off);
508 ret = mtdpstore_block_is_removed(cxt, off);
512 ret = mtdpstore_flush_removed_do(cxt, off, mtd->erasesize);
519 static void mtdpstore_notify_remove(struct mtd_info *mtd)
521 struct mtdpstore_context *cxt = &oops_cxt;
523 if (mtd->index != cxt->index || cxt->index < 0)
526 mtdpstore_flush_removed(cxt);
528 unregister_pstore_device(&cxt->dev);
536 static struct mtd_notifier mtdpstore_notifier = {
537 .add = mtdpstore_notify_add,
538 .remove = mtdpstore_notify_remove,
541 static int __init mtdpstore_init(void)
544 struct mtdpstore_context *cxt = &oops_cxt;
545 struct pstore_blk_config *info = &cxt->info;
547 ret = pstore_blk_get_config(info);
551 if (strlen(info->device) == 0) {
552 pr_err("mtd device must be supplied (device name is empty)\n");
555 if (!info->kmsg_size) {
556 pr_err("no backend enabled (kmsg_size is 0)\n");
560 /* Setup the MTD device to use */
561 ret = kstrtoint((char *)info->device, 0, &cxt->index);
565 register_mtd_user(&mtdpstore_notifier);
568 module_init(mtdpstore_init);
570 static void __exit mtdpstore_exit(void)
572 unregister_mtd_user(&mtdpstore_notifier);
574 module_exit(mtdpstore_exit);
576 MODULE_LICENSE("GPL");
577 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
578 MODULE_DESCRIPTION("MTD backend for pstore/blk");