Skip to content

Commit 335426c

Browse files
WeiXiong Liaokees
authored andcommitted
pstore/zone: Provide way to skip "broken" zone for MTD devices
One requirement to support MTD devices in pstore/zone is having a way to declare certain regions as broken. Add this support to pstore/zone. The MTD driver should return -ENOMSG when encountering a bad region, which tells pstore/zone to skip and try the next one. Signed-off-by: WeiXiong Liao <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/ Co-developed-by: Colin Ian King <[email protected]> Signed-off-by: Colin Ian King <[email protected]> Link: //lore.kernel.org/lkml/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 649304c commit 335426c

File tree

4 files changed

+71
-19
lines changed

4 files changed

+71
-19
lines changed

fs/pstore/blk.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ struct bdev_info {
109109
* means error.
110110
* @write: The same as @read, but the following error number:
111111
* -EBUSY means try to write again later.
112+
* -ENOMSG means to try next zone.
112113
* @panic_write:The write operation only used for panic case. It's optional
113-
* if you do not care panic log. The parameters and return value
114-
* are the same as @read.
114+
* if you do not care panic log. The parameters are relative
115+
* value to storage.
116+
* On success, the number of bytes should be returned, others
117+
* excluding -ENOMSG mean error. -ENOMSG means to try next zone.
115118
*/
116119
struct pstore_device_info {
117120
unsigned long total_size;
@@ -337,6 +340,9 @@ static ssize_t psblk_blk_panic_write(const char *buf, size_t size,
337340
/* size and off must align to SECTOR_SIZE for block device */
338341
ret = blkdev_panic_write(buf, off >> SECTOR_SHIFT,
339342
size >> SECTOR_SHIFT);
343+
/* try next zone */
344+
if (ret == -ENOMSG)
345+
return ret;
340346
return ret ? -EIO : size;
341347
}
342348

fs/pstore/zone.c

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ static int psz_zone_write(struct pstore_zone *zone,
249249

250250
return 0;
251251
dirty:
252+
/* no need to mark dirty if going to try next zone */
253+
if (wcnt == -ENOMSG)
254+
return -ENOMSG;
252255
atomic_set(&zone->dirty, true);
253256
/* flush dirty zones nicely */
254257
if (wcnt == -EBUSY && !is_on_panic())
@@ -391,7 +394,11 @@ static int psz_kmsg_recover_meta(struct psz_context *cxt)
391394
return -EINVAL;
392395

393396
rcnt = info->read((char *)buf, len, zone->off);
394-
if (rcnt != len) {
397+
if (rcnt == -ENOMSG) {
398+
pr_debug("%s with id %lu may be broken, skip\n",
399+
zone->name, i);
400+
continue;
401+
} else if (rcnt != len) {
395402
pr_err("read %s with id %lu failed\n", zone->name, i);
396403
return (int)rcnt < 0 ? (int)rcnt : -EIO;
397404
}
@@ -725,24 +732,58 @@ static void psz_write_kmsg_hdr(struct pstore_zone *zone,
725732
hdr->counter = 0;
726733
}
727734

735+
/*
736+
* In case zone is broken, which may occur to MTD device, we try each zones,
737+
* start at cxt->kmsg_write_cnt.
738+
*/
728739
static inline int notrace psz_kmsg_write_record(struct psz_context *cxt,
729740
struct pstore_record *record)
730741
{
731742
size_t size, hlen;
732743
struct pstore_zone *zone;
733-
unsigned int zonenum;
744+
unsigned int i;
734745

735-
zonenum = cxt->kmsg_write_cnt;
736-
zone = cxt->kpszs[zonenum];
737-
if (unlikely(!zone))
738-
return -ENOSPC;
739-
cxt->kmsg_write_cnt = (zonenum + 1) % cxt->kmsg_max_cnt;
746+
for (i = 0; i < cxt->kmsg_max_cnt; i++) {
747+
unsigned int zonenum, len;
748+
int ret;
740749

741-
pr_debug("write %s to zone id %d\n", zone->name, zonenum);
742-
psz_write_kmsg_hdr(zone, record);
743-
hlen = sizeof(struct psz_kmsg_header);
744-
size = min_t(size_t, record->size, zone->buffer_size - hlen);
745-
return psz_zone_write(zone, FLUSH_ALL, record->buf, size, hlen);
750+
zonenum = (cxt->kmsg_write_cnt + i) % cxt->kmsg_max_cnt;
751+
zone = cxt->kpszs[zonenum];
752+
if (unlikely(!zone))
753+
return -ENOSPC;
754+
755+
/* avoid destroying old data, allocate a new one */
756+
len = zone->buffer_size + sizeof(*zone->buffer);
757+
zone->oldbuf = zone->buffer;
758+
zone->buffer = kzalloc(len, GFP_KERNEL);
759+
if (!zone->buffer) {
760+
zone->buffer = zone->oldbuf;
761+
return -ENOMEM;
762+
}
763+
zone->buffer->sig = zone->oldbuf->sig;
764+
765+
pr_debug("write %s to zone id %d\n", zone->name, zonenum);
766+
psz_write_kmsg_hdr(zone, record);
767+
hlen = sizeof(struct psz_kmsg_header);
768+
size = min_t(size_t, record->size, zone->buffer_size - hlen);
769+
ret = psz_zone_write(zone, FLUSH_ALL, record->buf, size, hlen);
770+
if (likely(!ret || ret != -ENOMSG)) {
771+
cxt->kmsg_write_cnt = zonenum + 1;
772+
cxt->kmsg_write_cnt %= cxt->kmsg_max_cnt;
773+
/* no need to try next zone, free last zone buffer */
774+
kfree(zone->oldbuf);
775+
zone->oldbuf = NULL;
776+
return ret;
777+
}
778+
779+
pr_debug("zone %u may be broken, try next dmesg zone\n",
780+
zonenum);
781+
kfree(zone->buffer);
782+
zone->buffer = zone->oldbuf;
783+
zone->oldbuf = NULL;
784+
}
785+
786+
return -EBUSY;
746787
}
747788

748789
static int notrace psz_kmsg_write(struct psz_context *cxt,

include/linux/pstore_blk.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* @start_sect: start sector to block device
1515
* @sects: sectors count on buf
1616
*
17-
* Return: On success, zero should be returned. Others mean error.
17+
* Return: On success, zero should be returned. Others excluding -ENOMSG
18+
* mean error. -ENOMSG means to try next zone.
1819
*
1920
* Panic write to block device must be aligned to SECTOR_SIZE.
2021
*/

include/linux/pstore_zone.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ typedef ssize_t (*pstore_zone_write_op)(const char *, size_t, loff_t);
2323
* @read: The general read operation. Both of the function parameters
2424
* @size and @offset are relative value to storage.
2525
* On success, the number of bytes should be returned, others
26-
* means error.
27-
* @write: The same as @read, but -EBUSY means try to write again later.
26+
* mean error.
27+
* @write: The same as @read, but the following error number:
28+
* -EBUSY means try to write again later.
29+
* -ENOMSG means to try next zone.
2830
* @panic_write:The write operation only used for panic case. It's optional
29-
* if you do not care panic log. The parameters and return value
30-
* are the same as @read.
31+
* if you do not care panic log. The parameters are relative
32+
* value to storage.
33+
* On success, the number of bytes should be returned, others
34+
* excluding -ENOMSG mean error. -ENOMSG means to try next zone.
3135
*/
3236
struct pstore_zone_info {
3337
struct module *owner;

0 commit comments

Comments
 (0)