100-rootfs_split.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. --- a/drivers/mtd/Kconfig
  2. +++ b/drivers/mtd/Kconfig
  3. @@ -52,6 +52,14 @@ config MTD_TESTS
  4. WARNING: some of the tests will ERASE entire MTD device which they
  5. test. Do not use these tests unless you really know what you do.
  6. +config MTD_ROOTFS_ROOT_DEV
  7. + bool "Automatically set 'rootfs' partition to be root filesystem"
  8. + default y
  9. +
  10. +config MTD_ROOTFS_SPLIT
  11. + bool "Automatically split 'rootfs' partition for squashfs"
  12. + default y
  13. +
  14. config MTD_REDBOOT_PARTS
  15. tristate "RedBoot partition table parsing"
  16. ---help---
  17. --- a/drivers/mtd/mtdpart.c
  18. +++ b/drivers/mtd/mtdpart.c
  19. @@ -29,6 +29,8 @@
  20. #include <linux/kmod.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/partitions.h>
  23. +#include <linux/root_dev.h>
  24. +#include <linux/magic.h>
  25. #include <linux/err.h>
  26. #include "mtdcore.h"
  27. @@ -52,7 +54,7 @@ struct mtd_part {
  28. * the pointer to that structure with this macro.
  29. */
  30. #define PART(x) ((struct mtd_part *)(x))
  31. -
  32. +#define IS_PART(mtd) (mtd->_read == part_read)
  33. /*
  34. * MTD methods which simply translate the effective address and pass through
  35. @@ -705,6 +707,144 @@ int mtd_del_partition(struct mtd_info *m
  36. }
  37. EXPORT_SYMBOL_GPL(mtd_del_partition);
  38. +#ifdef CONFIG_MTD_ROOTFS_SPLIT
  39. +#define ROOTFS_SPLIT_NAME "rootfs_data"
  40. +#define ROOTFS_REMOVED_NAME "<removed>"
  41. +
  42. +struct squashfs_super_block {
  43. + __le32 s_magic;
  44. + __le32 pad0[9];
  45. + __le64 bytes_used;
  46. +};
  47. +
  48. +
  49. +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
  50. +{
  51. + struct squashfs_super_block sb;
  52. + int len, ret;
  53. +
  54. + ret = mtd_read(master, offset, sizeof(sb), &len, (void *) &sb);
  55. + if (ret || (len != sizeof(sb))) {
  56. + printk(KERN_ALERT "split_squashfs: error occured while reading "
  57. + "from \"%s\"\n", master->name);
  58. + return -EINVAL;
  59. + }
  60. +
  61. + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
  62. + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
  63. + master->name);
  64. + *split_offset = 0;
  65. + return 0;
  66. + }
  67. +
  68. + if (le64_to_cpu((sb.bytes_used)) <= 0) {
  69. + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
  70. + master->name);
  71. + *split_offset = 0;
  72. + return 0;
  73. + }
  74. +
  75. + len = (u32) le64_to_cpu(sb.bytes_used);
  76. + len += (offset & 0x000fffff);
  77. + len += (master->erasesize - 1);
  78. + len &= ~(master->erasesize - 1);
  79. + len -= (offset & 0x000fffff);
  80. + *split_offset = offset + len;
  81. +
  82. + return 0;
  83. +}
  84. +
  85. +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
  86. +{
  87. + struct mtd_partition dpart;
  88. + struct mtd_part *slave = NULL;
  89. + struct mtd_part *spart;
  90. + int ret, split_offset = 0;
  91. +
  92. + spart = PART(rpart);
  93. + ret = split_squashfs(master, spart->offset, &split_offset);
  94. + if (ret)
  95. + return ret;
  96. +
  97. + if (split_offset <= 0)
  98. + return 0;
  99. +
  100. + memcpy(&dpart, part, sizeof(dpart));
  101. + dpart.name = ROOTFS_SPLIT_NAME;
  102. +
  103. + dpart.size = rpart->size - (split_offset - spart->offset);
  104. + dpart.offset = split_offset;
  105. +
  106. + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
  107. + ROOTFS_SPLIT_NAME, dpart.offset, dpart.size);
  108. +
  109. + slave = allocate_partition(master, &dpart, 0, split_offset);
  110. + if (IS_ERR(slave))
  111. + return PTR_ERR(slave);
  112. + mutex_lock(&mtd_partitions_mutex);
  113. + list_add(&slave->list, &mtd_partitions);
  114. + mutex_unlock(&mtd_partitions_mutex);
  115. +
  116. + add_mtd_device(&slave->mtd);
  117. +
  118. + rpart->split = &slave->mtd;
  119. +
  120. + return 0;
  121. +}
  122. +
  123. +static int refresh_rootfs_split(struct mtd_info *mtd)
  124. +{
  125. + struct mtd_partition tpart;
  126. + struct mtd_part *part;
  127. + char *name;
  128. + //int index = 0;
  129. + int offset, size;
  130. + int ret;
  131. +
  132. + part = PART(mtd);
  133. +
  134. + /* check for the new squashfs offset first */
  135. + ret = split_squashfs(part->master, part->offset, &offset);
  136. + if (ret)
  137. + return ret;
  138. +
  139. + if ((offset > 0) && !mtd->split) {
  140. + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
  141. + /* if we don't have a rootfs split partition, create a new one */
  142. + tpart.name = (char *) mtd->name;
  143. + tpart.size = mtd->size;
  144. + tpart.offset = part->offset;
  145. +
  146. + return split_rootfs_data(part->master, &part->mtd, &tpart);
  147. + } else if ((offset > 0) && mtd->split) {
  148. + /* update the offsets of the existing partition */
  149. + size = mtd->size + part->offset - offset;
  150. +
  151. + part = PART(mtd->split);
  152. + part->offset = offset;
  153. + part->mtd.size = size;
  154. + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
  155. + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
  156. + (u32) part->offset, (u32) part->mtd.size);
  157. + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
  158. + strcpy(name, ROOTFS_SPLIT_NAME);
  159. + part->mtd.name = name;
  160. + } else if ((offset <= 0) && mtd->split) {
  161. + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
  162. +
  163. + /* mark existing partition as removed */
  164. + part = PART(mtd->split);
  165. + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
  166. + strcpy(name, ROOTFS_REMOVED_NAME);
  167. + part->mtd.name = name;
  168. + part->offset = 0;
  169. + part->mtd.size = 0;
  170. + }
  171. +
  172. + return 0;
  173. +}
  174. +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
  175. +
  176. /*
  177. * This function, given a master MTD object and a partition table, creates
  178. * and registers slave MTD objects which are bound to the master according to
  179. @@ -721,6 +861,9 @@ int add_mtd_partitions(struct mtd_info *
  180. struct mtd_part *slave;
  181. uint64_t cur_offset = 0;
  182. int i;
  183. +#ifdef CONFIG_MTD_ROOTFS_SPLIT
  184. + int ret;
  185. +#endif
  186. printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
  187. @@ -735,12 +878,53 @@ int add_mtd_partitions(struct mtd_info *
  188. add_mtd_device(&slave->mtd);
  189. + if (!strcmp(parts[i].name, "rootfs")) {
  190. +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
  191. + if (ROOT_DEV == 0) {
  192. + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
  193. + "set to be root filesystem\n");
  194. + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
  195. + }
  196. +#endif
  197. +#ifdef CONFIG_MTD_ROOTFS_SPLIT
  198. + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
  199. + /* if (ret == 0)
  200. + * j++; */
  201. +#endif
  202. + }
  203. +
  204. cur_offset = slave->offset + slave->mtd.size;
  205. }
  206. return 0;
  207. }
  208. +int mtd_device_refresh(struct mtd_info *mtd)
  209. +{
  210. + int ret = 0;
  211. +
  212. + if (IS_PART(mtd)) {
  213. + struct mtd_part *part;
  214. + struct mtd_info *master;
  215. +
  216. + part = PART(mtd);
  217. + master = part->master;
  218. + if (master->refresh_device)
  219. + ret = master->refresh_device(master);
  220. + }
  221. +
  222. + if (!ret && mtd->refresh_device)
  223. + ret = mtd->refresh_device(mtd);
  224. +
  225. +#ifdef CONFIG_MTD_ROOTFS_SPLIT
  226. + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
  227. + refresh_rootfs_split(mtd);
  228. +#endif
  229. +
  230. + return 0;
  231. +}
  232. +EXPORT_SYMBOL_GPL(mtd_device_refresh);
  233. +
  234. static DEFINE_SPINLOCK(part_parser_lock);
  235. static LIST_HEAD(part_parsers);
  236. --- a/drivers/mtd/mtdchar.c
  237. +++ b/drivers/mtd/mtdchar.c
  238. @@ -1008,6 +1008,12 @@ static int mtdchar_ioctl(struct file *fi
  239. break;
  240. }
  241. + case MTDREFRESH:
  242. + {
  243. + ret = mtd_device_refresh(mtd);
  244. + break;
  245. + }
  246. +
  247. default:
  248. ret = -ENOTTY;
  249. }
  250. --- a/include/linux/mtd/mtd.h
  251. +++ b/include/linux/mtd/mtd.h
  252. @@ -115,6 +115,7 @@ struct nand_ecclayout {
  253. struct module; /* only needed for owner field in mtd_info */
  254. +struct mtd_info;
  255. struct mtd_info {
  256. u_char type;
  257. uint32_t flags;
  258. @@ -231,6 +232,9 @@ struct mtd_info {
  259. int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
  260. int (*_suspend) (struct mtd_info *mtd);
  261. void (*_resume) (struct mtd_info *mtd);
  262. + int (*refresh_device)(struct mtd_info *mtd);
  263. + struct mtd_info *split;
  264. +
  265. /*
  266. * If the driver is something smart, like UBI, it may need to maintain
  267. * its own reference counting. The below functions are only for driver.
  268. @@ -397,6 +401,7 @@ extern int mtd_device_parse_register(str
  269. int defnr_parts);
  270. #define mtd_device_register(master, parts, nr_parts) \
  271. mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
  272. +extern int mtd_device_refresh(struct mtd_info *master);
  273. extern int mtd_device_unregister(struct mtd_info *master);
  274. extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
  275. extern int __get_mtd_device(struct mtd_info *mtd);
  276. --- a/include/linux/mtd/partitions.h
  277. +++ b/include/linux/mtd/partitions.h
  278. @@ -37,12 +37,14 @@
  279. */
  280. struct mtd_info;
  281. +struct mtd_partition;
  282. struct mtd_partition {
  283. const char *name; /* identifier string */
  284. uint64_t size; /* partition size */
  285. uint64_t offset; /* offset within the master MTD space */
  286. uint32_t mask_flags; /* master MTD flags to mask out for this partition */
  287. struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
  288. + int (*refresh_partition)(struct mtd_info *);
  289. };
  290. #define MTDPART_OFS_RETAIN (-3)
  291. --- a/include/uapi/mtd/mtd-abi.h
  292. +++ b/include/uapi/mtd/mtd-abi.h
  293. @@ -203,6 +203,7 @@ struct otp_info {
  294. * without OOB, e.g., NOR flash.
  295. */
  296. #define MEMWRITE _IOWR('M', 24, struct mtd_write_req)
  297. +#define MTDREFRESH _IO('M', 50)
  298. /*
  299. * Obsolete legacy interface. Keep it in order not to break userspace