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>
10 #include <linux/slab.h>
12 static struct mtdpstore_context
{
14 struct pstore_blk_config info
;
15 struct pstore_device_info dev
;
17 unsigned long *rmmap
; /* removed bit map */
18 unsigned long *usedmap
; /* used bit map */
20 * used for panic write
21 * As there are no block_isbad for panic case, we should keep this
22 * status before panic to ensure panic_write not failed.
24 unsigned long *badmap
; /* bad block bit map */
27 static int mtdpstore_block_isbad(struct mtdpstore_context
*cxt
, loff_t off
)
30 struct mtd_info
*mtd
= cxt
->mtd
;
33 off
= ALIGN_DOWN(off
, mtd
->erasesize
);
34 blknum
= div_u64(off
, mtd
->erasesize
);
36 if (test_bit(blknum
, cxt
->badmap
))
38 ret
= mtd_block_isbad(mtd
, off
);
40 dev_err(&mtd
->dev
, "mtd_block_isbad failed, aborting\n");
43 set_bit(blknum
, cxt
->badmap
);
49 static inline int mtdpstore_panic_block_isbad(struct mtdpstore_context
*cxt
,
52 struct mtd_info
*mtd
= cxt
->mtd
;
55 off
= ALIGN_DOWN(off
, mtd
->erasesize
);
56 blknum
= div_u64(off
, mtd
->erasesize
);
57 return test_bit(blknum
, cxt
->badmap
);
60 static inline void mtdpstore_mark_used(struct mtdpstore_context
*cxt
,
63 struct mtd_info
*mtd
= cxt
->mtd
;
64 u64 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
66 dev_dbg(&mtd
->dev
, "mark zone %llu used\n", zonenum
);
67 set_bit(zonenum
, cxt
->usedmap
);
70 static inline void mtdpstore_mark_unused(struct mtdpstore_context
*cxt
,
73 struct mtd_info
*mtd
= cxt
->mtd
;
74 u64 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
76 dev_dbg(&mtd
->dev
, "mark zone %llu unused\n", zonenum
);
77 clear_bit(zonenum
, cxt
->usedmap
);
80 static inline void mtdpstore_block_mark_unused(struct mtdpstore_context
*cxt
,
83 struct mtd_info
*mtd
= cxt
->mtd
;
84 u32 zonecnt
= mtd
->erasesize
/ cxt
->info
.kmsg_size
;
87 off
= ALIGN_DOWN(off
, mtd
->erasesize
);
88 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
90 dev_dbg(&mtd
->dev
, "mark zone %llu unused\n", zonenum
);
91 clear_bit(zonenum
, cxt
->usedmap
);
97 static inline int mtdpstore_is_used(struct mtdpstore_context
*cxt
, loff_t off
)
99 u64 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
100 u64 blknum
= div_u64(off
, cxt
->mtd
->erasesize
);
102 if (test_bit(blknum
, cxt
->badmap
))
104 return test_bit(zonenum
, cxt
->usedmap
);
107 static int mtdpstore_block_is_used(struct mtdpstore_context
*cxt
,
110 struct mtd_info
*mtd
= cxt
->mtd
;
111 u32 zonecnt
= mtd
->erasesize
/ cxt
->info
.kmsg_size
;
114 off
= ALIGN_DOWN(off
, mtd
->erasesize
);
115 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
116 while (zonecnt
> 0) {
117 if (test_bit(zonenum
, cxt
->usedmap
))
125 static int mtdpstore_is_empty(struct mtdpstore_context
*cxt
, char *buf
,
128 struct mtd_info
*mtd
= cxt
->mtd
;
132 sz
= min_t(uint32_t, size
, mtd
->writesize
/ 4);
133 for (i
= 0; i
< sz
; i
++) {
134 if (buf
[i
] != (char)0xFF)
140 static void mtdpstore_mark_removed(struct mtdpstore_context
*cxt
, loff_t off
)
142 struct mtd_info
*mtd
= cxt
->mtd
;
143 u64 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
145 dev_dbg(&mtd
->dev
, "mark zone %llu removed\n", zonenum
);
146 set_bit(zonenum
, cxt
->rmmap
);
149 static void mtdpstore_block_clear_removed(struct mtdpstore_context
*cxt
,
152 struct mtd_info
*mtd
= cxt
->mtd
;
153 u32 zonecnt
= mtd
->erasesize
/ cxt
->info
.kmsg_size
;
156 off
= ALIGN_DOWN(off
, mtd
->erasesize
);
157 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
158 while (zonecnt
> 0) {
159 clear_bit(zonenum
, cxt
->rmmap
);
165 static int mtdpstore_block_is_removed(struct mtdpstore_context
*cxt
,
168 struct mtd_info
*mtd
= cxt
->mtd
;
169 u32 zonecnt
= mtd
->erasesize
/ cxt
->info
.kmsg_size
;
172 off
= ALIGN_DOWN(off
, mtd
->erasesize
);
173 zonenum
= div_u64(off
, cxt
->info
.kmsg_size
);
174 while (zonecnt
> 0) {
175 if (test_bit(zonenum
, cxt
->rmmap
))
183 static int mtdpstore_erase_do(struct mtdpstore_context
*cxt
, loff_t off
)
185 struct mtd_info
*mtd
= cxt
->mtd
;
186 struct erase_info erase
;
189 off
= ALIGN_DOWN(off
, cxt
->mtd
->erasesize
);
190 dev_dbg(&mtd
->dev
, "try to erase off 0x%llx\n", off
);
191 erase
.len
= cxt
->mtd
->erasesize
;
193 ret
= mtd_erase(cxt
->mtd
, &erase
);
195 mtdpstore_block_clear_removed(cxt
, off
);
197 dev_err(&mtd
->dev
, "erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
198 (unsigned long long)erase
.addr
,
199 (unsigned long long)erase
.len
, cxt
->info
.device
);
204 * called while removing file
206 * Avoiding over erasing, do erase block only when the whole block is unused.
207 * If the block contains valid log, do erase lazily on flush_removed() when
210 static ssize_t
mtdpstore_erase(size_t size
, loff_t off
)
212 struct mtdpstore_context
*cxt
= &oops_cxt
;
214 if (mtdpstore_block_isbad(cxt
, off
))
217 mtdpstore_mark_unused(cxt
, off
);
219 /* If the block still has valid data, mtdpstore do erase lazily */
220 if (likely(mtdpstore_block_is_used(cxt
, off
))) {
221 mtdpstore_mark_removed(cxt
, off
);
225 /* all zones are unused, erase it */
226 return mtdpstore_erase_do(cxt
, off
);
230 * What is security for mtdpstore?
231 * As there is no erase for panic case, we should ensure at least one zone
232 * is writable. Otherwise, panic write will fail.
233 * If zone is used, write operation will return -ENOMSG, which means that
234 * pstore/blk will try one by one until gets an empty zone. So, it is not
235 * needed to ensure the next zone is empty, but at least one.
237 static int mtdpstore_security(struct mtdpstore_context
*cxt
, loff_t off
)
240 struct mtd_info
*mtd
= cxt
->mtd
;
241 u32 zonenum
= (u32
)div_u64(off
, cxt
->info
.kmsg_size
);
242 u32 zonecnt
= (u32
)div_u64(cxt
->mtd
->size
, cxt
->info
.kmsg_size
);
243 u32 blkcnt
= (u32
)div_u64(cxt
->mtd
->size
, cxt
->mtd
->erasesize
);
244 u32 erasesize
= cxt
->mtd
->erasesize
;
246 for (i
= 0; i
< zonecnt
; i
++) {
247 u32 num
= (zonenum
+ i
) % zonecnt
;
249 /* found empty zone */
250 if (!test_bit(num
, cxt
->usedmap
))
254 /* If there is no any empty zone, we have no way but to do erase */
256 div64_u64_rem(off
+ erasesize
, cxt
->mtd
->size
, (u64
*)&off
);
258 if (mtdpstore_block_isbad(cxt
, off
))
261 ret
= mtdpstore_erase_do(cxt
, off
);
263 mtdpstore_block_mark_unused(cxt
, off
);
269 dev_err(&mtd
->dev
, "all blocks bad!\n");
270 dev_dbg(&mtd
->dev
, "end security\n");
274 static ssize_t
mtdpstore_write(const char *buf
, size_t size
, loff_t off
)
276 struct mtdpstore_context
*cxt
= &oops_cxt
;
277 struct mtd_info
*mtd
= cxt
->mtd
;
281 if (mtdpstore_block_isbad(cxt
, off
))
284 /* zone is used, please try next one */
285 if (mtdpstore_is_used(cxt
, off
))
288 dev_dbg(&mtd
->dev
, "try to write off 0x%llx size %zu\n", off
, size
);
289 ret
= mtd_write(cxt
->mtd
, off
, size
, &retlen
, (u_char
*)buf
);
290 if (ret
< 0 || retlen
!= size
) {
291 dev_err(&mtd
->dev
, "write failure at %lld (%zu of %zu written), err %d\n",
292 off
, retlen
, size
, ret
);
295 mtdpstore_mark_used(cxt
, off
);
297 mtdpstore_security(cxt
, off
);
301 static inline bool mtdpstore_is_io_error(int ret
)
303 return ret
< 0 && !mtd_is_bitflip(ret
) && !mtd_is_eccerr(ret
);
307 * All zones will be read as pstore/blk will read zone one by one when do
310 static ssize_t
mtdpstore_read(char *buf
, size_t size
, loff_t off
)
312 struct mtdpstore_context
*cxt
= &oops_cxt
;
313 struct mtd_info
*mtd
= cxt
->mtd
;
317 if (mtdpstore_block_isbad(cxt
, off
))
320 dev_dbg(&mtd
->dev
, "try to read off 0x%llx size %zu\n", off
, size
);
321 for (done
= 0, retlen
= 0; done
< size
; done
+= retlen
) {
324 ret
= mtd_read(cxt
->mtd
, off
+ done
, size
- done
, &retlen
,
325 (u_char
*)buf
+ done
);
326 if (mtdpstore_is_io_error(ret
)) {
327 dev_err(&mtd
->dev
, "read failure at %lld (%zu of %zu read), err %d\n",
328 off
+ done
, retlen
, size
- done
, ret
);
329 /* the zone may be broken, try next one */
334 * ECC error. The impact on log data is so small. Maybe we can
335 * still read it and try to understand. So mtdpstore just hands
336 * over what it gets and user can judge whether the data is
339 if (mtd_is_eccerr(ret
)) {
340 dev_err(&mtd
->dev
, "ecc error at %lld (%zu of %zu read), err %d\n",
341 off
+ done
, retlen
, size
- done
, ret
);
342 /* driver may not set retlen when ecc error */
343 retlen
= retlen
== 0 ? size
- done
: retlen
;
347 if (mtdpstore_is_empty(cxt
, buf
, size
))
348 mtdpstore_mark_unused(cxt
, off
);
350 mtdpstore_mark_used(cxt
, off
);
352 mtdpstore_security(cxt
, off
);
356 static ssize_t
mtdpstore_panic_write(const char *buf
, size_t size
, loff_t off
)
358 struct mtdpstore_context
*cxt
= &oops_cxt
;
359 struct mtd_info
*mtd
= cxt
->mtd
;
363 if (mtdpstore_panic_block_isbad(cxt
, off
))
366 /* zone is used, please try next one */
367 if (mtdpstore_is_used(cxt
, off
))
370 ret
= mtd_panic_write(cxt
->mtd
, off
, size
, &retlen
, (u_char
*)buf
);
371 if (ret
< 0 || size
!= retlen
) {
372 dev_err(&mtd
->dev
, "panic write failure at %lld (%zu of %zu read), err %d\n",
373 off
, retlen
, size
, ret
);
376 mtdpstore_mark_used(cxt
, off
);
381 static void mtdpstore_notify_add(struct mtd_info
*mtd
)
384 struct mtdpstore_context
*cxt
= &oops_cxt
;
385 struct pstore_blk_config
*info
= &cxt
->info
;
386 unsigned long longcnt
;
388 if (!strcmp(mtd
->name
, info
->device
))
389 cxt
->index
= mtd
->index
;
391 if (mtd
->index
!= cxt
->index
|| cxt
->index
< 0)
394 dev_dbg(&mtd
->dev
, "found matching MTD device %s\n", mtd
->name
);
396 if (mtd
->size
< info
->kmsg_size
* 2) {
397 dev_err(&mtd
->dev
, "MTD partition %d not big enough\n",
402 * kmsg_size must be aligned to 4096 Bytes, which is limited by
403 * psblk. The default value of kmsg_size is 64KB. If kmsg_size
404 * is larger than erasesize, some errors will occur since mtdpsotre
407 if (mtd
->erasesize
< info
->kmsg_size
) {
408 dev_err(&mtd
->dev
, "eraseblock size of MTD partition %d too small\n",
412 if (unlikely(info
->kmsg_size
% mtd
->writesize
)) {
413 dev_err(&mtd
->dev
, "record size %lu KB must align to write size %d KB\n",
414 info
->kmsg_size
/ 1024,
415 mtd
->writesize
/ 1024);
419 longcnt
= BITS_TO_LONGS(div_u64(mtd
->size
, info
->kmsg_size
));
420 cxt
->rmmap
= kcalloc(longcnt
, sizeof(long), GFP_KERNEL
);
421 cxt
->usedmap
= kcalloc(longcnt
, sizeof(long), GFP_KERNEL
);
423 longcnt
= BITS_TO_LONGS(div_u64(mtd
->size
, mtd
->erasesize
));
424 cxt
->badmap
= kcalloc(longcnt
, sizeof(long), GFP_KERNEL
);
426 cxt
->dev
.total_size
= mtd
->size
;
427 /* just support dmesg right now */
428 cxt
->dev
.flags
= PSTORE_FLAGS_DMESG
;
429 cxt
->dev
.read
= mtdpstore_read
;
430 cxt
->dev
.write
= mtdpstore_write
;
431 cxt
->dev
.erase
= mtdpstore_erase
;
432 cxt
->dev
.panic_write
= mtdpstore_panic_write
;
434 ret
= register_pstore_device(&cxt
->dev
);
436 dev_err(&mtd
->dev
, "mtd%d register to psblk failed\n",
441 dev_info(&mtd
->dev
, "Attached to MTD device %d\n", mtd
->index
);
444 static int mtdpstore_flush_removed_do(struct mtdpstore_context
*cxt
,
445 loff_t off
, size_t size
)
447 struct mtd_info
*mtd
= cxt
->mtd
;
451 struct erase_info erase
;
453 buf
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
457 /* 1st. read to cache */
458 ret
= mtd_read(mtd
, off
, mtd
->erasesize
, &retlen
, buf
);
459 if (mtdpstore_is_io_error(ret
))
462 /* 2nd. erase block */
463 erase
.len
= mtd
->erasesize
;
465 ret
= mtd_erase(mtd
, &erase
);
469 /* 3rd. write back */
471 unsigned int zonesize
= cxt
->info
.kmsg_size
;
473 /* there is valid data on block, write back */
474 if (mtdpstore_is_used(cxt
, off
)) {
475 ret
= mtd_write(mtd
, off
, zonesize
, &retlen
, buf
);
477 dev_err(&mtd
->dev
, "write failure at %lld (%zu of %u written), err %d\n",
478 off
, retlen
, zonesize
, ret
);
482 size
-= min_t(unsigned int, zonesize
, size
);
491 * What does mtdpstore_flush_removed() do?
492 * When user remove any log file on pstore filesystem, mtdpstore should do
493 * something to ensure log file removed. If the whole block is no longer used,
494 * it's nice to erase the block. However if the block still contains valid log,
495 * what mtdpstore can do is to erase and write the valid log back.
497 static int mtdpstore_flush_removed(struct mtdpstore_context
*cxt
)
499 struct mtd_info
*mtd
= cxt
->mtd
;
502 u32 blkcnt
= (u32
)div_u64(mtd
->size
, mtd
->erasesize
);
504 for (off
= 0; blkcnt
> 0; blkcnt
--, off
+= mtd
->erasesize
) {
505 ret
= mtdpstore_block_isbad(cxt
, off
);
509 ret
= mtdpstore_block_is_removed(cxt
, off
);
513 ret
= mtdpstore_flush_removed_do(cxt
, off
, mtd
->erasesize
);
520 static void mtdpstore_notify_remove(struct mtd_info
*mtd
)
522 struct mtdpstore_context
*cxt
= &oops_cxt
;
524 if (mtd
->index
!= cxt
->index
|| cxt
->index
< 0)
527 mtdpstore_flush_removed(cxt
);
529 unregister_pstore_device(&cxt
->dev
);
537 static struct mtd_notifier mtdpstore_notifier
= {
538 .add
= mtdpstore_notify_add
,
539 .remove
= mtdpstore_notify_remove
,
542 static int __init
mtdpstore_init(void)
545 struct mtdpstore_context
*cxt
= &oops_cxt
;
546 struct pstore_blk_config
*info
= &cxt
->info
;
548 ret
= pstore_blk_get_config(info
);
552 if (strlen(info
->device
) == 0) {
553 pr_err("mtd device must be supplied (device name is empty)\n");
556 if (!info
->kmsg_size
) {
557 pr_err("no backend enabled (kmsg_size is 0)\n");
561 /* Setup the MTD device to use */
562 ret
= kstrtoint((char *)info
->device
, 0, &cxt
->index
);
566 register_mtd_user(&mtdpstore_notifier
);
569 module_init(mtdpstore_init
);
571 static void __exit
mtdpstore_exit(void)
573 unregister_mtd_user(&mtdpstore_notifier
);
575 module_exit(mtdpstore_exit
);
577 MODULE_LICENSE("GPL");
578 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
579 MODULE_DESCRIPTION("MTD backend for pstore/blk");