Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #define dev_fmt(fmt) "mtdoops-pstore: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/pstore_blk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) static struct mtdpstore_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 	struct pstore_blk_config info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	struct pstore_device_info dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	struct mtd_info *mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	unsigned long *rmmap;		/* removed bit map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	unsigned long *usedmap;		/* used bit map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	 * used for panic write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	 * As there are no block_isbad for panic case, we should keep this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	 * status before panic to ensure panic_write not failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	unsigned long *badmap;		/* bad block bit map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) } oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static int mtdpstore_block_isbad(struct mtdpstore_context *cxt, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u64 blknum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	off = ALIGN_DOWN(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	blknum = div_u64(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (test_bit(blknum, cxt->badmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	ret = mtd_block_isbad(mtd, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		dev_err(&mtd->dev, "mtd_block_isbad failed, aborting\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		set_bit(blknum, cxt->badmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static inline int mtdpstore_panic_block_isbad(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	u64 blknum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	off = ALIGN_DOWN(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	blknum = div_u64(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return test_bit(blknum, cxt->badmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static inline void mtdpstore_mark_used(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u64 zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	dev_dbg(&mtd->dev, "mark zone %llu used\n", zonenum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	set_bit(zonenum, cxt->usedmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static inline void mtdpstore_mark_unused(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u64 zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	dev_dbg(&mtd->dev, "mark zone %llu unused\n", zonenum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	clear_bit(zonenum, cxt->usedmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static inline void mtdpstore_block_mark_unused(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u64 zonenum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	off = ALIGN_DOWN(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	while (zonecnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		dev_dbg(&mtd->dev, "mark zone %llu unused\n", zonenum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		clear_bit(zonenum, cxt->usedmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		zonenum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		zonecnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) static inline int mtdpstore_is_used(struct mtdpstore_context *cxt, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	u64 zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	u64 blknum = div_u64(off, cxt->mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (test_bit(blknum, cxt->badmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return test_bit(zonenum, cxt->usedmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int mtdpstore_block_is_used(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	u64 zonenum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	off = ALIGN_DOWN(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	while (zonecnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		if (test_bit(zonenum, cxt->usedmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		zonenum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		zonecnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int mtdpstore_is_empty(struct mtdpstore_context *cxt, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	size_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	sz = min_t(uint32_t, size, mtd->writesize / 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	for (i = 0; i < sz; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		if (buf[i] != (char)0xFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void mtdpstore_mark_removed(struct mtdpstore_context *cxt, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	u64 zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	dev_dbg(&mtd->dev, "mark zone %llu removed\n", zonenum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	set_bit(zonenum, cxt->rmmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static void mtdpstore_block_clear_removed(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	u64 zonenum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	off = ALIGN_DOWN(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	while (zonecnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		clear_bit(zonenum, cxt->rmmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		zonenum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		zonecnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int mtdpstore_block_is_removed(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	u32 zonecnt = mtd->erasesize / cxt->info.kmsg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	u64 zonenum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	off = ALIGN_DOWN(off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	zonenum = div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	while (zonecnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		if (test_bit(zonenum, cxt->rmmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		zonenum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		zonecnt--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int mtdpstore_erase_do(struct mtdpstore_context *cxt, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct erase_info erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	off = ALIGN_DOWN(off, cxt->mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	dev_dbg(&mtd->dev, "try to erase off 0x%llx\n", off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	erase.len = cxt->mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	erase.addr = off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	ret = mtd_erase(cxt->mtd, &erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		mtdpstore_block_clear_removed(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		dev_err(&mtd->dev, "erase of region [0x%llx, 0x%llx] on \"%s\" failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		       (unsigned long long)erase.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		       (unsigned long long)erase.len, cxt->info.device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * called while removing file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * Avoiding over erasing, do erase block only when the whole block is unused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * If the block contains valid log, do erase lazily on flush_removed() when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * unregister.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static ssize_t mtdpstore_erase(size_t size, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	struct mtdpstore_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (mtdpstore_block_isbad(cxt, off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	mtdpstore_mark_unused(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	/* If the block still has valid data, mtdpstore do erase lazily */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (likely(mtdpstore_block_is_used(cxt, off))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		mtdpstore_mark_removed(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	/* all zones are unused, erase it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return mtdpstore_erase_do(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * What is security for mtdpstore?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * As there is no erase for panic case, we should ensure at least one zone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  * is writable. Otherwise, panic write will fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * If zone is used, write operation will return -ENOMSG, which means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * pstore/blk will try one by one until gets an empty zone. So, it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * needed to ensure the next zone is empty, but at least one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int mtdpstore_security(struct mtdpstore_context *cxt, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	int ret = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	u32 zonenum = (u32)div_u64(off, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	u32 zonecnt = (u32)div_u64(cxt->mtd->size, cxt->info.kmsg_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	u32 blkcnt = (u32)div_u64(cxt->mtd->size, cxt->mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	u32 erasesize = cxt->mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	for (i = 0; i < zonecnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		u32 num = (zonenum + i) % zonecnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		/* found empty zone */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		if (!test_bit(num, cxt->usedmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	/* If there is no any empty zone, we have no way but to do erase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	while (blkcnt--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		div64_u64_rem(off + erasesize, cxt->mtd->size, (u64 *)&off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (mtdpstore_block_isbad(cxt, off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		ret = mtdpstore_erase_do(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			mtdpstore_block_mark_unused(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		dev_err(&mtd->dev, "all blocks bad!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	dev_dbg(&mtd->dev, "end security\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static ssize_t mtdpstore_write(const char *buf, size_t size, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct mtdpstore_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (mtdpstore_block_isbad(cxt, off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return -ENOMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	/* zone is used, please try next one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (mtdpstore_is_used(cxt, off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		return -ENOMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	dev_dbg(&mtd->dev, "try to write off 0x%llx size %zu\n", off, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	ret = mtd_write(cxt->mtd, off, size, &retlen, (u_char *)buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	if (ret < 0 || retlen != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		dev_err(&mtd->dev, "write failure at %lld (%zu of %zu written), err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				off, retlen, size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	mtdpstore_mark_used(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	mtdpstore_security(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static inline bool mtdpstore_is_io_error(int ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return ret < 0 && !mtd_is_bitflip(ret) && !mtd_is_eccerr(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * All zones will be read as pstore/blk will read zone one by one when do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * recover.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static ssize_t mtdpstore_read(char *buf, size_t size, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct mtdpstore_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	size_t retlen, done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (mtdpstore_block_isbad(cxt, off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		return -ENOMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	dev_dbg(&mtd->dev, "try to read off 0x%llx size %zu\n", off, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	for (done = 0, retlen = 0; done < size; done += retlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		retlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		ret = mtd_read(cxt->mtd, off + done, size - done, &retlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 				(u_char *)buf + done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		if (mtdpstore_is_io_error(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			dev_err(&mtd->dev, "read failure at %lld (%zu of %zu read), err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 					off + done, retlen, size - done, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			/* the zone may be broken, try next one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			return -ENOMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		 * ECC error. The impact on log data is so small. Maybe we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		 * still read it and try to understand. So mtdpstore just hands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		 * over what it gets and user can judge whether the data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		 * valid or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		if (mtd_is_eccerr(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			dev_err(&mtd->dev, "ecc error at %lld (%zu of %zu read), err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 					off + done, retlen, size - done, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			/* driver may not set retlen when ecc error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			retlen = retlen == 0 ? size - done : retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (mtdpstore_is_empty(cxt, buf, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		mtdpstore_mark_unused(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		mtdpstore_mark_used(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	mtdpstore_security(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static ssize_t mtdpstore_panic_write(const char *buf, size_t size, loff_t off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct mtdpstore_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (mtdpstore_panic_block_isbad(cxt, off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		return -ENOMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	/* zone is used, please try next one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (mtdpstore_is_used(cxt, off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		return -ENOMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	ret = mtd_panic_write(cxt->mtd, off, size, &retlen, (u_char *)buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	if (ret < 0 || size != retlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		dev_err(&mtd->dev, "panic write failure at %lld (%zu of %zu read), err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				off, retlen, size, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	mtdpstore_mark_used(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static void mtdpstore_notify_add(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct mtdpstore_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	struct pstore_blk_config *info = &cxt->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	unsigned long longcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (!strcmp(mtd->name, info->device))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		cxt->index = mtd->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (mtd->index != cxt->index || cxt->index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	dev_dbg(&mtd->dev, "found matching MTD device %s\n", mtd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (mtd->size < info->kmsg_size * 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		dev_err(&mtd->dev, "MTD partition %d not big enough\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 				mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	 * kmsg_size must be aligned to 4096 Bytes, which is limited by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	 * psblk. The default value of kmsg_size is 64KB. If kmsg_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	 * is larger than erasesize, some errors will occur since mtdpsotre
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	 * is designed on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (mtd->erasesize < info->kmsg_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		dev_err(&mtd->dev, "eraseblock size of MTD partition %d too small\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 				mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (unlikely(info->kmsg_size % mtd->writesize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		dev_err(&mtd->dev, "record size %lu KB must align to write size %d KB\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 				info->kmsg_size / 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 				mtd->writesize / 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	longcnt = BITS_TO_LONGS(div_u64(mtd->size, info->kmsg_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	cxt->rmmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	cxt->usedmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	longcnt = BITS_TO_LONGS(div_u64(mtd->size, mtd->erasesize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	cxt->badmap = kcalloc(longcnt, sizeof(long), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	cxt->dev.total_size = mtd->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	/* just support dmesg right now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	cxt->dev.flags = PSTORE_FLAGS_DMESG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	cxt->dev.read = mtdpstore_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	cxt->dev.write = mtdpstore_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	cxt->dev.erase = mtdpstore_erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	cxt->dev.panic_write = mtdpstore_panic_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	ret = register_pstore_device(&cxt->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		dev_err(&mtd->dev, "mtd%d register to psblk failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 				mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	cxt->mtd = mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	dev_info(&mtd->dev, "Attached to MTD device %d\n", mtd->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) static int mtdpstore_flush_removed_do(struct mtdpstore_context *cxt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		loff_t off, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	u_char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	size_t retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct erase_info erase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	buf = kmalloc(mtd->erasesize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	/* 1st. read to cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	ret = mtd_read(mtd, off, mtd->erasesize, &retlen, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (mtdpstore_is_io_error(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	/* 2nd. erase block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	erase.len = mtd->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	erase.addr = off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	ret = mtd_erase(mtd, &erase);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	/* 3rd. write back */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	while (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		unsigned int zonesize = cxt->info.kmsg_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		/* there is valid data on block, write back */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		if (mtdpstore_is_used(cxt, off)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			ret = mtd_write(mtd, off, zonesize, &retlen, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 				dev_err(&mtd->dev, "write failure at %lld (%zu of %u written), err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 						off, retlen, zonesize, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		off += zonesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		size -= min_t(unsigned int, zonesize, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)  * What does mtdpstore_flush_removed() do?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * When user remove any log file on pstore filesystem, mtdpstore should do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  * something to ensure log file removed. If the whole block is no longer used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  * it's nice to erase the block. However if the block still contains valid log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)  * what mtdpstore can do is to erase and write the valid log back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static int mtdpstore_flush_removed(struct mtdpstore_context *cxt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	struct mtd_info *mtd = cxt->mtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	loff_t off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	u32 blkcnt = (u32)div_u64(mtd->size, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	for (off = 0; blkcnt > 0; blkcnt--, off += mtd->erasesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		ret = mtdpstore_block_isbad(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		ret = mtdpstore_block_is_removed(cxt, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		ret = mtdpstore_flush_removed_do(cxt, off, mtd->erasesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static void mtdpstore_notify_remove(struct mtd_info *mtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	struct mtdpstore_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (mtd->index != cxt->index || cxt->index < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	mtdpstore_flush_removed(cxt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	unregister_pstore_device(&cxt->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	kfree(cxt->badmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	kfree(cxt->usedmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	kfree(cxt->rmmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	cxt->mtd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	cxt->index = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static struct mtd_notifier mtdpstore_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	.add	= mtdpstore_notify_add,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	.remove	= mtdpstore_notify_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static int __init mtdpstore_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	struct mtdpstore_context *cxt = &oops_cxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	struct pstore_blk_config *info = &cxt->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	ret = pstore_blk_get_config(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (strlen(info->device) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		pr_err("mtd device must be supplied (device name is empty)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	if (!info->kmsg_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		pr_err("no backend enabled (kmsg_size is 0)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	/* Setup the MTD device to use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	ret = kstrtoint((char *)info->device, 0, &cxt->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		cxt->index = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	register_mtd_user(&mtdpstore_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) module_init(mtdpstore_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static void __exit mtdpstore_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	unregister_mtd_user(&mtdpstore_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) module_exit(mtdpstore_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) MODULE_AUTHOR("WeiXiong Liao <liaoweixiong@allwinnertech.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) MODULE_DESCRIPTION("MTD backend for pstore/blk");