411-mtd-partial_eraseblock_write.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. From: Felix Fietkau <nbd@nbd.name>
  2. Subject: mtd: implement write support for partitions covering only a part of an eraseblock (buffer data that would otherwise be erased)
  3. lede-commit: 87a8e8ac1067f58ba831c4aae443f3655c31cd80
  4. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  5. ---
  6. drivers/mtd/mtdpart.c | 90 ++++++++++++++++++++++++++++++++++++++++++++-----
  7. include/linux/mtd/mtd.h | 4 +++
  8. 2 files changed, 85 insertions(+), 9 deletions(-)
  9. --- a/drivers/mtd/mtdpart.c
  10. +++ b/drivers/mtd/mtdpart.c
  11. @@ -37,6 +37,8 @@
  12. #include "mtdcore.h"
  13. #include "mtdsplit/mtdsplit.h"
  14. +#define MTD_ERASE_PARTIAL 0x8000 /* partition only covers parts of an erase block */
  15. +
  16. /* Our partition linked list */
  17. static LIST_HEAD(mtd_partitions);
  18. static DEFINE_MUTEX(mtd_partitions_mutex);
  19. @@ -242,13 +244,61 @@ static int part_erase(struct mtd_info *m
  20. struct mtd_part *part = mtd_to_part(mtd);
  21. int ret;
  22. +
  23. + instr->partial_start = false;
  24. + if (mtd->flags & MTD_ERASE_PARTIAL) {
  25. + size_t readlen = 0;
  26. + u64 mtd_ofs;
  27. +
  28. + instr->erase_buf = kmalloc(part->parent->erasesize, GFP_ATOMIC);
  29. + if (!instr->erase_buf)
  30. + return -ENOMEM;
  31. +
  32. + mtd_ofs = part->offset + instr->addr;
  33. + instr->erase_buf_ofs = do_div(mtd_ofs, part->parent->erasesize);
  34. +
  35. + if (instr->erase_buf_ofs > 0) {
  36. + instr->addr -= instr->erase_buf_ofs;
  37. + ret = mtd_read(part->parent,
  38. + instr->addr + part->offset,
  39. + part->parent->erasesize,
  40. + &readlen, instr->erase_buf);
  41. +
  42. + instr->len += instr->erase_buf_ofs;
  43. + instr->partial_start = true;
  44. + } else {
  45. + mtd_ofs = part->offset + part->mtd.size;
  46. + instr->erase_buf_ofs = part->parent->erasesize -
  47. + do_div(mtd_ofs, part->parent->erasesize);
  48. +
  49. + if (instr->erase_buf_ofs > 0) {
  50. + instr->len += instr->erase_buf_ofs;
  51. + ret = mtd_read(part->parent,
  52. + part->offset + instr->addr +
  53. + instr->len - part->parent->erasesize,
  54. + part->parent->erasesize, &readlen,
  55. + instr->erase_buf);
  56. + } else {
  57. + ret = 0;
  58. + }
  59. + }
  60. + if (ret < 0) {
  61. + kfree(instr->erase_buf);
  62. + return ret;
  63. + }
  64. +
  65. + }
  66. +
  67. instr->addr += part->offset;
  68. ret = part->parent->_erase(part->parent, instr);
  69. if (ret) {
  70. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  71. instr->fail_addr -= part->offset;
  72. instr->addr -= part->offset;
  73. + if (mtd->flags & MTD_ERASE_PARTIAL)
  74. + kfree(instr->erase_buf);
  75. }
  76. +
  77. return ret;
  78. }
  79. @@ -256,6 +306,25 @@ void mtd_erase_callback(struct erase_inf
  80. {
  81. if (instr->mtd->_erase == part_erase) {
  82. struct mtd_part *part = mtd_to_part(instr->mtd);
  83. + size_t wrlen = 0;
  84. +
  85. + if (instr->mtd->flags & MTD_ERASE_PARTIAL) {
  86. + if (instr->partial_start) {
  87. + part->parent->_write(part->parent,
  88. + instr->addr, instr->erase_buf_ofs,
  89. + &wrlen, instr->erase_buf);
  90. + instr->addr += instr->erase_buf_ofs;
  91. + } else {
  92. + instr->len -= instr->erase_buf_ofs;
  93. + part->parent->_write(part->parent,
  94. + instr->addr + instr->len,
  95. + instr->erase_buf_ofs, &wrlen,
  96. + instr->erase_buf +
  97. + part->parent->erasesize -
  98. + instr->erase_buf_ofs);
  99. + }
  100. + kfree(instr->erase_buf);
  101. + }
  102. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  103. instr->fail_addr -= part->offset;
  104. @@ -562,19 +631,22 @@ static struct mtd_part *allocate_partiti
  105. remainder = do_div(tmp, wr_alignment);
  106. if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
  107. /* Doesn't start on a boundary of major erase size */
  108. - /* FIXME: Let it be writable if it is on a boundary of
  109. - * _minor_ erase size though */
  110. - slave->mtd.flags &= ~MTD_WRITEABLE;
  111. - printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
  112. - part->name);
  113. + slave->mtd.flags |= MTD_ERASE_PARTIAL;
  114. + if (((u32)slave->mtd.size) > parent->erasesize)
  115. + slave->mtd.flags &= ~MTD_WRITEABLE;
  116. + else
  117. + slave->mtd.erasesize = slave->mtd.size;
  118. }
  119. - tmp = slave->mtd.size;
  120. + tmp = slave->offset + slave->mtd.size;
  121. remainder = do_div(tmp, wr_alignment);
  122. if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
  123. - slave->mtd.flags &= ~MTD_WRITEABLE;
  124. - printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
  125. - part->name);
  126. + slave->mtd.flags |= MTD_ERASE_PARTIAL;
  127. +
  128. + if ((u32)slave->mtd.size > parent->erasesize)
  129. + slave->mtd.flags &= ~MTD_WRITEABLE;
  130. + else
  131. + slave->mtd.erasesize = slave->mtd.size;
  132. }
  133. mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
  134. --- a/include/linux/mtd/mtd.h
  135. +++ b/include/linux/mtd/mtd.h
  136. @@ -55,6 +55,10 @@ struct erase_info {
  137. u_long priv;
  138. u_char state;
  139. struct erase_info *next;
  140. +
  141. + u8 *erase_buf;
  142. + u32 erase_buf_ofs;
  143. + bool partial_start;
  144. };
  145. struct mtd_erase_region_info {