0001
0002
0003 #define dev_fmt(fmt) "mtdoops-pstore: " fmt
0004
0005 #include <linux/kernel.h>
0006 #include <linux/module.h>
0007 #include <linux/pstore_blk.h>
0008 #include <linux/mtd/mtd.h>
0009 #include <linux/bitops.h>
0010 #include <linux/slab.h>
0011
0012 static struct mtdpstore_context {
0013 int index;
0014 struct pstore_blk_config info;
0015 struct pstore_device_info dev;
0016 struct mtd_info *mtd;
0017 unsigned long *rmmap;
0018 unsigned long *usedmap;
0019
0020
0021
0022
0023
0024 unsigned long *badmap;
0025 } oops_cxt;
0026
0027 static int mtdpstore_block_isbad(struct mtdpstore_context *cxt, loff_t off)
0028 {
0029 int ret;
0030 struct mtd_info *mtd = cxt->mtd;
0031 u64 blknum;
0032
0033 off = ALIGN_DOWN(off, mtd->erasesize);
0034 blknum = div_u64(off, mtd->erasesize);
0035
0036 if (test_bit(blknum, cxt->badmap))
0037 return true;
0038 ret = mtd_block_isbad(mtd, off);
0039 if (ret < 0) {
0040 dev_err(&mtd->dev, "mtd_block_isbad failed, aborting\n");
0041 return ret;
0042 } else if (ret > 0) {
0043 set_bit(blknum, cxt->badmap);
0044 return true;
0045 }
0046 return false;
0047 }
0048
0049 static inline int mtdpstore_panic_block_isbad(struct mtdpstore_context *cxt,
0050 loff_t off)
0051 {
0052 struct mtd_info *mtd = cxt->mtd;
0053 u64 blknum;
0054
0055 off = ALIGN_DOWN(off, mtd->erasesize);
0056 blknum = div_u64(off, mtd->erasesize);
0057 return test_bit(blknum, cxt->badmap);
0058 }
0059
0060 static inline void mtdpstore_mark_used(struct mtdpstore_context *cxt,
0061 loff_t off)
0062 {
0063 struct mtd_info *mtd = cxt->mtd;
0064 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
0065
0066 dev_dbg(&mtd->dev, "mark zone %llu used\n", zonenum);
0067 set_bit(zonenum, cxt->usedmap);
0068 }
0069
0070 static inline void mtdpstore_mark_unused(struct mtdpstore_context *cxt,
0071 loff_t off)
0072 {
0073 struct mtd_info *mtd = cxt->mtd;
0074 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
0075
0076 dev_dbg(&mtd->dev, "mark zone %llu unused\n", zonenum);
0077 clear_bit(zonenum, cxt->usedmap);
0078 }
0079
0080 static inline void mtdpstore_block_mark_unused(struct mtdpstore_context *cxt,
0081 loff_t off)
0082 {
0083 struct mtd_info *mtd = cxt->mtd;
0084 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
0085 u64 zonenum;
0086
0087 off = ALIGN_DOWN(off, mtd->erasesize);
0088 zonenum = div_u64(off, cxt->info.kmsg_size);
0089 while (zonecnt > 0) {
0090 dev_dbg(&mtd->dev, "mark zone %llu unused\n", zonenum);
0091 clear_bit(zonenum, cxt->usedmap);
0092 zonenum++;
0093 zonecnt--;
0094 }
0095 }
0096
0097 static inline int mtdpstore_is_used(struct mtdpstore_context *cxt, loff_t off)
0098 {
0099 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
0100 u64 blknum = div_u64(off, cxt->mtd->erasesize);
0101
0102 if (test_bit(blknum, cxt->badmap))
0103 return true;
0104 return test_bit(zonenum, cxt->usedmap);
0105 }
0106
0107 static int mtdpstore_block_is_used(struct mtdpstore_context *cxt,
0108 loff_t off)
0109 {
0110 struct mtd_info *mtd = cxt->mtd;
0111 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
0112 u64 zonenum;
0113
0114 off = ALIGN_DOWN(off, mtd->erasesize);
0115 zonenum = div_u64(off, cxt->info.kmsg_size);
0116 while (zonecnt > 0) {
0117 if (test_bit(zonenum, cxt->usedmap))
0118 return true;
0119 zonenum++;
0120 zonecnt--;
0121 }
0122 return false;
0123 }
0124
0125 static int mtdpstore_is_empty(struct mtdpstore_context *cxt, char *buf,
0126 size_t size)
0127 {
0128 struct mtd_info *mtd = cxt->mtd;
0129 size_t sz;
0130 int i;
0131
0132 sz = min_t(uint32_t, size, mtd->writesize / 4);
0133 for (i = 0; i < sz; i++) {
0134 if (buf[i] != (char)0xFF)
0135 return false;
0136 }
0137 return true;
0138 }
0139
0140 static void mtdpstore_mark_removed(struct mtdpstore_context *cxt, loff_t off)
0141 {
0142 struct mtd_info *mtd = cxt->mtd;
0143 u64 zonenum = div_u64(off, cxt->info.kmsg_size);
0144
0145 dev_dbg(&mtd->dev, "mark zone %llu removed\n", zonenum);
0146 set_bit(zonenum, cxt->rmmap);
0147 }
0148
0149 static void mtdpstore_block_clear_removed(struct mtdpstore_context *cxt,
0150 loff_t off)
0151 {
0152 struct mtd_info *mtd = cxt->mtd;
0153 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
0154 u64 zonenum;
0155
0156 off = ALIGN_DOWN(off, mtd->erasesize);
0157 zonenum = div_u64(off, cxt->info.kmsg_size);
0158 while (zonecnt > 0) {
0159 clear_bit(zonenum, cxt->rmmap);
0160 zonenum++;
0161 zonecnt--;
0162 }
0163 }
0164
0165 static int mtdpstore_block_is_removed(struct mtdpstore_context *cxt,
0166 loff_t off)
0167 {
0168 struct mtd_info *mtd = cxt->mtd;
0169 u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
0170 u64 zonenum;
0171
0172 off = ALIGN_DOWN(off, mtd->erasesize);
0173 zonenum = div_u64(off, cxt->info.kmsg_size);
0174 while (zonecnt > 0) {
0175 if (test_bit(zonenum, cxt->rmmap))
0176 return true;
0177 zonenum++;
0178 zonecnt--;
0179 }
0180 return false;
0181 }
0182
0183 static int mtdpstore_erase_do(struct mtdpstore_context *cxt, loff_t off)
0184 {
0185 struct mtd_info *mtd = cxt->mtd;
0186 struct erase_info erase;
0187 int ret;
0188
0189 off = ALIGN_DOWN(off, cxt->mtd->erasesize);
0190 dev_dbg(&mtd->dev, "try to erase off 0x%llx\n", off);
0191 erase.len = cxt->mtd->erasesize;
0192 erase.addr = off;
0193 ret = mtd_erase(cxt->mtd, &erase);
0194 if (!ret)
0195 mtdpstore_block_clear_removed(cxt, off);
0196 else
0197 dev_err(&mtd->dev, "erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
0198 (unsigned long long)erase.addr,
0199 (unsigned long long)erase.len, cxt->info.device);
0200 return ret;
0201 }
0202
0203
0204
0205
0206
0207
0208
0209
0210 static ssize_t mtdpstore_erase(size_t size, loff_t off)
0211 {
0212 struct mtdpstore_context *cxt = &oops_cxt;
0213
0214 if (mtdpstore_block_isbad(cxt, off))
0215 return -EIO;
0216
0217 mtdpstore_mark_unused(cxt, off);
0218
0219
0220 if (likely(mtdpstore_block_is_used(cxt, off))) {
0221 mtdpstore_mark_removed(cxt, off);
0222 return 0;
0223 }
0224
0225
0226 return mtdpstore_erase_do(cxt, off);
0227 }
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237 static int mtdpstore_security(struct mtdpstore_context *cxt, loff_t off)
0238 {
0239 int ret = 0, i;
0240 struct mtd_info *mtd = cxt->mtd;
0241 u32 zonenum = (u32)div_u64(off, cxt->info.kmsg_size);
0242 u32 zonecnt = (u32)div_u64(cxt->mtd->size, cxt->info.kmsg_size);
0243 u32 blkcnt = (u32)div_u64(cxt->mtd->size, cxt->mtd->erasesize);
0244 u32 erasesize = cxt->mtd->erasesize;
0245
0246 for (i = 0; i < zonecnt; i++) {
0247 u32 num = (zonenum + i) % zonecnt;
0248
0249
0250 if (!test_bit(num, cxt->usedmap))
0251 return 0;
0252 }
0253
0254
0255 while (blkcnt--) {
0256 div64_u64_rem(off + erasesize, cxt->mtd->size, (u64 *)&off);
0257
0258 if (mtdpstore_block_isbad(cxt, off))
0259 continue;
0260
0261 ret = mtdpstore_erase_do(cxt, off);
0262 if (!ret) {
0263 mtdpstore_block_mark_unused(cxt, off);
0264 break;
0265 }
0266 }
0267
0268 if (ret)
0269 dev_err(&mtd->dev, "all blocks bad!\n");
0270 dev_dbg(&mtd->dev, "end security\n");
0271 return ret;
0272 }
0273
0274 static ssize_t mtdpstore_write(const char *buf, size_t size, loff_t off)
0275 {
0276 struct mtdpstore_context *cxt = &oops_cxt;
0277 struct mtd_info *mtd = cxt->mtd;
0278 size_t retlen;
0279 int ret;
0280
0281 if (mtdpstore_block_isbad(cxt, off))
0282 return -ENOMSG;
0283
0284
0285 if (mtdpstore_is_used(cxt, off))
0286 return -ENOMSG;
0287
0288 dev_dbg(&mtd->dev, "try to write off 0x%llx size %zu\n", off, size);
0289 ret = mtd_write(cxt->mtd, off, size, &retlen, (u_char *)buf);
0290 if (ret < 0 || retlen != size) {
0291 dev_err(&mtd->dev, "write failure at %lld (%zu of %zu written), err %d\n",
0292 off, retlen, size, ret);
0293 return -EIO;
0294 }
0295 mtdpstore_mark_used(cxt, off);
0296
0297 mtdpstore_security(cxt, off);
0298 return retlen;
0299 }
0300
0301 static inline bool mtdpstore_is_io_error(int ret)
0302 {
0303 return ret < 0 && !mtd_is_bitflip(ret) && !mtd_is_eccerr(ret);
0304 }
0305
0306
0307
0308
0309
0310 static ssize_t mtdpstore_read(char *buf, size_t size, loff_t off)
0311 {
0312 struct mtdpstore_context *cxt = &oops_cxt;
0313 struct mtd_info *mtd = cxt->mtd;
0314 size_t retlen, done;
0315 int ret;
0316
0317 if (mtdpstore_block_isbad(cxt, off))
0318 return -ENOMSG;
0319
0320 dev_dbg(&mtd->dev, "try to read off 0x%llx size %zu\n", off, size);
0321 for (done = 0, retlen = 0; done < size; done += retlen) {
0322 retlen = 0;
0323
0324 ret = mtd_read(cxt->mtd, off + done, size - done, &retlen,
0325 (u_char *)buf + done);
0326 if (mtdpstore_is_io_error(ret)) {
0327 dev_err(&mtd->dev, "read failure at %lld (%zu of %zu read), err %d\n",
0328 off + done, retlen, size - done, ret);
0329
0330 return -ENOMSG;
0331 }
0332
0333
0334
0335
0336
0337
0338
0339 if (mtd_is_eccerr(ret)) {
0340 dev_err(&mtd->dev, "ecc error at %lld (%zu of %zu read), err %d\n",
0341 off + done, retlen, size - done, ret);
0342
0343 retlen = retlen == 0 ? size - done : retlen;
0344 }
0345 }
0346
0347 if (mtdpstore_is_empty(cxt, buf, size))
0348 mtdpstore_mark_unused(cxt, off);
0349 else
0350 mtdpstore_mark_used(cxt, off);
0351
0352 mtdpstore_security(cxt, off);
0353 return retlen;
0354 }
0355
0356 static ssize_t mtdpstore_panic_write(const char *buf, size_t size, loff_t off)
0357 {
0358 struct mtdpstore_context *cxt = &oops_cxt;
0359 struct mtd_info *mtd = cxt->mtd;
0360 size_t retlen;
0361 int ret;
0362
0363 if (mtdpstore_panic_block_isbad(cxt, off))
0364 return -ENOMSG;
0365
0366
0367 if (mtdpstore_is_used(cxt, off))
0368 return -ENOMSG;
0369
0370 ret = mtd_panic_write(cxt->mtd, off, size, &retlen, (u_char *)buf);
0371 if (ret < 0 || size != retlen) {
0372 dev_err(&mtd->dev, "panic write failure at %lld (%zu of %zu read), err %d\n",
0373 off, retlen, size, ret);
0374 return -EIO;
0375 }
0376 mtdpstore_mark_used(cxt, off);
0377
0378 return retlen;
0379 }
0380
0381 static void mtdpstore_notify_add(struct mtd_info *mtd)
0382 {
0383 int ret;
0384 struct mtdpstore_context *cxt = &oops_cxt;
0385 struct pstore_blk_config *info = &cxt->info;
0386 unsigned long longcnt;
0387
0388 if (!strcmp(mtd->name, info->device))
0389 cxt->index = mtd->index;
0390
0391 if (mtd->index != cxt->index || cxt->index < 0)
0392 return;
0393
0394 dev_dbg(&mtd->dev, "found matching MTD device %s\n", mtd->name);
0395
0396 if (mtd->size < info->kmsg_size * 2) {
0397 dev_err(&mtd->dev, "MTD partition %d not big enough\n",
0398 mtd->index);
0399 return;
0400 }
0401
0402
0403
0404
0405
0406
0407 if (mtd->erasesize < info->kmsg_size) {
0408 dev_err(&mtd->dev, "eraseblock size of MTD partition %d too small\n",
0409 mtd->index);
0410 return;
0411 }
0412 if (unlikely(info->kmsg_size % mtd->writesize)) {
0413 dev_err(&mtd->dev, "record size %lu KB must align to write size %d KB\n",
0414 info->kmsg_size / 1024,
0415 mtd->writesize / 1024);
0416 return;
0417 }
0418
0419 longcnt = BITS_TO_LONGS(div_u64(mtd->size, info->kmsg_size));
0420 cxt->rmmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
0421 cxt->usedmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
0422
0423 longcnt = BITS_TO_LONGS(div_u64(mtd->size, mtd->erasesize));
0424 cxt->badmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
0425
0426
0427 cxt->dev.flags = PSTORE_FLAGS_DMESG;
0428 cxt->dev.zone.read = mtdpstore_read;
0429 cxt->dev.zone.write = mtdpstore_write;
0430 cxt->dev.zone.erase = mtdpstore_erase;
0431 cxt->dev.zone.panic_write = mtdpstore_panic_write;
0432 cxt->dev.zone.total_size = mtd->size;
0433
0434 ret = register_pstore_device(&cxt->dev);
0435 if (ret) {
0436 dev_err(&mtd->dev, "mtd%d register to psblk failed\n",
0437 mtd->index);
0438 return;
0439 }
0440 cxt->mtd = mtd;
0441 dev_info(&mtd->dev, "Attached to MTD device %d\n", mtd->index);
0442 }
0443
0444 static int mtdpstore_flush_removed_do(struct mtdpstore_context *cxt,
0445 loff_t off, size_t size)
0446 {
0447 struct mtd_info *mtd = cxt->mtd;
0448 u_char *buf;
0449 int ret;
0450 size_t retlen;
0451 struct erase_info erase;
0452
0453 buf = kmalloc(mtd->erasesize, GFP_KERNEL);
0454 if (!buf)
0455 return -ENOMEM;
0456
0457
0458 ret = mtd_read(mtd, off, mtd->erasesize, &retlen, buf);
0459 if (mtdpstore_is_io_error(ret))
0460 goto free;
0461
0462
0463 erase.len = mtd->erasesize;
0464 erase.addr = off;
0465 ret = mtd_erase(mtd, &erase);
0466 if (ret)
0467 goto free;
0468
0469
0470 while (size) {
0471 unsigned int zonesize = cxt->info.kmsg_size;
0472
0473
0474 if (mtdpstore_is_used(cxt, off)) {
0475 ret = mtd_write(mtd, off, zonesize, &retlen, buf);
0476 if (ret)
0477 dev_err(&mtd->dev, "write failure at %lld (%zu of %u written), err %d\n",
0478 off, retlen, zonesize, ret);
0479 }
0480
0481 off += zonesize;
0482 size -= min_t(unsigned int, zonesize, size);
0483 }
0484
0485 free:
0486 kfree(buf);
0487 return ret;
0488 }
0489
0490
0491
0492
0493
0494
0495
0496
0497 static int mtdpstore_flush_removed(struct mtdpstore_context *cxt)
0498 {
0499 struct mtd_info *mtd = cxt->mtd;
0500 int ret;
0501 loff_t off;
0502 u32 blkcnt = (u32)div_u64(mtd->size, mtd->erasesize);
0503
0504 for (off = 0; blkcnt > 0; blkcnt--, off += mtd->erasesize) {
0505 ret = mtdpstore_block_isbad(cxt, off);
0506 if (ret)
0507 continue;
0508
0509 ret = mtdpstore_block_is_removed(cxt, off);
0510 if (!ret)
0511 continue;
0512
0513 ret = mtdpstore_flush_removed_do(cxt, off, mtd->erasesize);
0514 if (ret)
0515 return ret;
0516 }
0517 return 0;
0518 }
0519
0520 static void mtdpstore_notify_remove(struct mtd_info *mtd)
0521 {
0522 struct mtdpstore_context *cxt = &oops_cxt;
0523
0524 if (mtd->index != cxt->index || cxt->index < 0)
0525 return;
0526
0527 mtdpstore_flush_removed(cxt);
0528
0529 unregister_pstore_device(&cxt->dev);
0530 kfree(cxt->badmap);
0531 kfree(cxt->usedmap);
0532 kfree(cxt->rmmap);
0533 cxt->mtd = NULL;
0534 cxt->index = -1;
0535 }
0536
0537 static struct mtd_notifier mtdpstore_notifier = {
0538 .add = mtdpstore_notify_add,
0539 .remove = mtdpstore_notify_remove,
0540 };
0541
0542 static int __init mtdpstore_init(void)
0543 {
0544 int ret;
0545 struct mtdpstore_context *cxt = &oops_cxt;
0546 struct pstore_blk_config *info = &cxt->info;
0547
0548 ret = pstore_blk_get_config(info);
0549 if (unlikely(ret))
0550 return ret;
0551
0552 if (strlen(info->device) == 0) {
0553 pr_err("mtd device must be supplied (device name is empty)\n");
0554 return -EINVAL;
0555 }
0556 if (!info->kmsg_size) {
0557 pr_err("no backend enabled (kmsg_size is 0)\n");
0558 return -EINVAL;
0559 }
0560
0561
0562 ret = kstrtoint((char *)info->device, 0, &cxt->index);
0563 if (ret)
0564 cxt->index = -1;
0565
0566 register_mtd_user(&mtdpstore_notifier);
0567 return 0;
0568 }
0569 module_init(mtdpstore_init);
0570
0571 static void __exit mtdpstore_exit(void)
0572 {
0573 unregister_mtd_user(&mtdpstore_notifier);
0574 }
0575 module_exit(mtdpstore_exit);
0576
0577 MODULE_LICENSE("GPL");
0578 MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
0579 MODULE_DESCRIPTION("MTD backend for pstore/blk");