400-rootfs_split.patch 9.0 KB

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