4610d3c01a2b6bd78e6451f43c98236c0c26c436
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.22 / 060-rootfs_split.patch
1 diff -urN linux-2.6.21.1.old/drivers/mtd/Kconfig linux-2.6.21.1.dev/drivers/mtd/Kconfig
2 --- linux-2.6.21.1.old/drivers/mtd/Kconfig 2007-04-27 23:49:26.000000000 +0200
3 +++ linux-2.6.21.1.dev/drivers/mtd/Kconfig 2007-05-26 19:58:42.320520952 +0200
4 @@ -47,6 +47,11 @@
5 devices. Partitioning on NFTL 'devices' is a different - that's the
6 'normal' form of partitioning used on a block device.
7
8 +config MTD_SPLIT_ROOTFS
9 + bool "Automatically split rootfs partition for squashfs"
10 + depends on MTD_PARTITIONS
11 + default y
12 +
13 config MTD_REDBOOT_PARTS
14 tristate "RedBoot partition table parsing"
15 depends on MTD_PARTITIONS
16 diff -urN linux-2.6.21.1.old/drivers/mtd/mtdpart.c linux-2.6.21.1.dev/drivers/mtd/mtdpart.c
17 --- linux-2.6.21.1.old/drivers/mtd/mtdpart.c 2007-04-27 23:49:26.000000000 +0200
18 +++ linux-2.6.21.1.dev/drivers/mtd/mtdpart.c 2007-05-26 19:58:42.331519280 +0200
19 @@ -20,6 +20,8 @@
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/mtd/compatmac.h>
23 +#include <linux/squashfs_fs.h>
24 +#include <linux/root_dev.h>
25
26 /* Our partition linked list */
27 static LIST_HEAD(mtd_partitions);
28 @@ -308,6 +310,171 @@
29 return 0;
30 }
31
32 +static u_int32_t cur_offset = 0;
33 +static int add_mtd_partition(struct mtd_info *master, const struct mtd_partition *part, int i)
34 +{
35 + struct mtd_part *slave;
36 +
37 + /* allocate the partition structure */
38 + slave = kzalloc (sizeof(*slave), GFP_KERNEL);
39 + if (!slave) {
40 + printk ("memory allocation error while creating partitions for \"%s\"\n",
41 + master->name);
42 + del_mtd_partitions(master);
43 + return -ENOMEM;
44 + }
45 + list_add(&slave->list, &mtd_partitions);
46 +
47 + /* set up the MTD object for this partition */
48 + slave->mtd.type = master->type;
49 + slave->mtd.flags = master->flags & ~part->mask_flags;
50 + slave->mtd.size = part->size;
51 + slave->mtd.writesize = master->writesize;
52 + slave->mtd.oobsize = master->oobsize;
53 + slave->mtd.oobavail = master->oobavail;
54 + slave->mtd.subpage_sft = master->subpage_sft;
55 +
56 + slave->mtd.name = part->name;
57 + slave->mtd.owner = master->owner;
58 +
59 + slave->mtd.read = part_read;
60 + slave->mtd.write = part_write;
61 +
62 + if(master->point && master->unpoint){
63 + slave->mtd.point = part_point;
64 + slave->mtd.unpoint = part_unpoint;
65 + }
66 +
67 + if (master->read_oob)
68 + slave->mtd.read_oob = part_read_oob;
69 + if (master->write_oob)
70 + slave->mtd.write_oob = part_write_oob;
71 + if(master->read_user_prot_reg)
72 + slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
73 + if(master->read_fact_prot_reg)
74 + slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
75 + if(master->write_user_prot_reg)
76 + slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
77 + if(master->lock_user_prot_reg)
78 + slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
79 + if(master->get_user_prot_info)
80 + slave->mtd.get_user_prot_info = part_get_user_prot_info;
81 + if(master->get_fact_prot_info)
82 + slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
83 + if (master->sync)
84 + slave->mtd.sync = part_sync;
85 + if (!i && master->suspend && master->resume) {
86 + slave->mtd.suspend = part_suspend;
87 + slave->mtd.resume = part_resume;
88 + }
89 + if (master->writev)
90 + slave->mtd.writev = part_writev;
91 + if (master->lock)
92 + slave->mtd.lock = part_lock;
93 + if (master->unlock)
94 + slave->mtd.unlock = part_unlock;
95 + if (master->block_isbad)
96 + slave->mtd.block_isbad = part_block_isbad;
97 + if (master->block_markbad)
98 + slave->mtd.block_markbad = part_block_markbad;
99 + slave->mtd.erase = part_erase;
100 + slave->master = master;
101 + slave->offset = part->offset;
102 + slave->index = i;
103 +
104 + if (slave->offset == MTDPART_OFS_APPEND)
105 + slave->offset = cur_offset;
106 + if (slave->offset == MTDPART_OFS_NXTBLK) {
107 + slave->offset = cur_offset;
108 + if ((cur_offset % master->erasesize) != 0) {
109 + /* Round up to next erasesize */
110 + slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
111 + printk(KERN_NOTICE "Moving partition %d: "
112 + "0x%08x -> 0x%08x\n", i,
113 + cur_offset, slave->offset);
114 + }
115 + }
116 + if (slave->mtd.size == MTDPART_SIZ_FULL)
117 + slave->mtd.size = master->size - slave->offset;
118 + cur_offset = slave->offset + slave->mtd.size;
119 +
120 + printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
121 + slave->offset + slave->mtd.size, slave->mtd.name);
122 +
123 + /* let's do some sanity checks */
124 + if (slave->offset >= master->size) {
125 + /* let's register it anyway to preserve ordering */
126 + slave->offset = 0;
127 + slave->mtd.size = 0;
128 + printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
129 + part->name);
130 + }
131 + if (slave->offset + slave->mtd.size > master->size) {
132 + slave->mtd.size = master->size - slave->offset;
133 + printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
134 + part->name, master->name, slave->mtd.size);
135 + }
136 + if (master->numeraseregions>1) {
137 + /* Deal with variable erase size stuff */
138 + int i;
139 + struct mtd_erase_region_info *regions = master->eraseregions;
140 +
141 + /* Find the first erase regions which is part of this partition. */
142 + for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
143 + ;
144 +
145 + for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
146 + if (slave->mtd.erasesize < regions[i].erasesize) {
147 + slave->mtd.erasesize = regions[i].erasesize;
148 + }
149 + }
150 + } else {
151 + /* Single erase size */
152 + slave->mtd.erasesize = master->erasesize;
153 + }
154 +
155 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
156 + (slave->offset % slave->mtd.erasesize)) {
157 + /* Doesn't start on a boundary of major erase size */
158 + /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
159 + slave->mtd.flags &= ~MTD_WRITEABLE;
160 + printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
161 + part->name);
162 + }
163 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
164 + (slave->mtd.size % slave->mtd.erasesize)) {
165 + slave->mtd.flags &= ~MTD_WRITEABLE;
166 + printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
167 + part->name);
168 + }
169 +
170 + slave->mtd.ecclayout = master->ecclayout;
171 + if (master->block_isbad) {
172 + uint32_t offs = 0;
173 +
174 + while(offs < slave->mtd.size) {
175 + if (master->block_isbad(master,
176 + offs + slave->offset))
177 + slave->mtd.ecc_stats.badblocks++;
178 + offs += slave->mtd.erasesize;
179 + }
180 + }
181 +
182 + if(part->mtdp)
183 + { /* store the object pointer (caller may or may not register it */
184 + *part->mtdp = &slave->mtd;
185 + slave->registered = 0;
186 + }
187 + else
188 + {
189 + /* register our partition */
190 + add_mtd_device(&slave->mtd);
191 + slave->registered = 1;
192 + }
193 +
194 + return 0;
195 +}
196 +
197 /*
198 * This function, given a master MTD object and a partition table, creates
199 * and registers slave MTD objects which are bound to the master according to
200 @@ -319,169 +487,53 @@
201 const struct mtd_partition *parts,
202 int nbparts)
203 {
204 - struct mtd_part *slave;
205 - u_int32_t cur_offset = 0;
206 - int i;
207 + struct mtd_partition *part;
208 + int i, ret = 0;
209
210 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
211
212 for (i = 0; i < nbparts; i++) {
213 + part = (struct mtd_partition *) &parts[i];
214 + ret = add_mtd_partition(master, part, i);
215 + if (ret)
216 + return ret;
217 + if (strcmp(part->name, "rootfs") == 0) {
218 +#ifdef CONFIG_MTD_SPLIT_ROOTFS
219 + int len;
220 + char buf[512];
221 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
222 +#define ROOTFS_SPLIT_NAME "rootfs_data"
223 + if ((master->read(master, part->offset, sizeof(struct squashfs_super_block), &len, buf) == 0) &&
224 + (len == sizeof(struct squashfs_super_block)) &&
225 + (*((u32 *) buf) == SQUASHFS_MAGIC) &&
226 + (sb->bytes_used > 0)) {
227 +
228 +
229 + part = kmalloc(sizeof(struct mtd_partition), GFP_KERNEL);
230 + memcpy(part, &parts[i], sizeof(struct mtd_partition));
231 +
232 + part->name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
233 + strcpy(part->name, ROOTFS_SPLIT_NAME);
234 +
235 + len = (u32) sb->bytes_used;
236 + len += (part->offset & 0x000fffff);
237 + len += (master->erasesize - 1);
238 + len &= ~(master->erasesize - 1);
239 + len -= (part->offset & 0x000fffff);
240 + part->offset += len;
241 + part->size -= len;
242 +
243 + if (master->erasesize <= part->size)
244 + ret = add_mtd_partition(master, part, i + 1);
245 + else
246 + kfree(part->name);
247 + if (ret)
248 + return ret;
249
250 - /* allocate the partition structure */
251 - slave = kzalloc (sizeof(*slave), GFP_KERNEL);
252 - if (!slave) {
253 - printk ("memory allocation error while creating partitions for \"%s\"\n",
254 - master->name);
255 - del_mtd_partitions(master);
256 - return -ENOMEM;
257 - }
258 - list_add(&slave->list, &mtd_partitions);
259 -
260 - /* set up the MTD object for this partition */
261 - slave->mtd.type = master->type;
262 - slave->mtd.flags = master->flags & ~parts[i].mask_flags;
263 - slave->mtd.size = parts[i].size;
264 - slave->mtd.writesize = master->writesize;
265 - slave->mtd.oobsize = master->oobsize;
266 - slave->mtd.oobavail = master->oobavail;
267 - slave->mtd.subpage_sft = master->subpage_sft;
268 -
269 - slave->mtd.name = parts[i].name;
270 - slave->mtd.owner = master->owner;
271 -
272 - slave->mtd.read = part_read;
273 - slave->mtd.write = part_write;
274 -
275 - if(master->point && master->unpoint){
276 - slave->mtd.point = part_point;
277 - slave->mtd.unpoint = part_unpoint;
278 - }
279 -
280 - if (master->read_oob)
281 - slave->mtd.read_oob = part_read_oob;
282 - if (master->write_oob)
283 - slave->mtd.write_oob = part_write_oob;
284 - if(master->read_user_prot_reg)
285 - slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
286 - if(master->read_fact_prot_reg)
287 - slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
288 - if(master->write_user_prot_reg)
289 - slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
290 - if(master->lock_user_prot_reg)
291 - slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
292 - if(master->get_user_prot_info)
293 - slave->mtd.get_user_prot_info = part_get_user_prot_info;
294 - if(master->get_fact_prot_info)
295 - slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
296 - if (master->sync)
297 - slave->mtd.sync = part_sync;
298 - if (!i && master->suspend && master->resume) {
299 - slave->mtd.suspend = part_suspend;
300 - slave->mtd.resume = part_resume;
301 - }
302 - if (master->writev)
303 - slave->mtd.writev = part_writev;
304 - if (master->lock)
305 - slave->mtd.lock = part_lock;
306 - if (master->unlock)
307 - slave->mtd.unlock = part_unlock;
308 - if (master->block_isbad)
309 - slave->mtd.block_isbad = part_block_isbad;
310 - if (master->block_markbad)
311 - slave->mtd.block_markbad = part_block_markbad;
312 - slave->mtd.erase = part_erase;
313 - slave->master = master;
314 - slave->offset = parts[i].offset;
315 - slave->index = i;
316 -
317 - if (slave->offset == MTDPART_OFS_APPEND)
318 - slave->offset = cur_offset;
319 - if (slave->offset == MTDPART_OFS_NXTBLK) {
320 - slave->offset = cur_offset;
321 - if ((cur_offset % master->erasesize) != 0) {
322 - /* Round up to next erasesize */
323 - slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
324 - printk(KERN_NOTICE "Moving partition %d: "
325 - "0x%08x -> 0x%08x\n", i,
326 - cur_offset, slave->offset);
327 + kfree(part);
328 }
329 - }
330 - if (slave->mtd.size == MTDPART_SIZ_FULL)
331 - slave->mtd.size = master->size - slave->offset;
332 - cur_offset = slave->offset + slave->mtd.size;
333 -
334 - printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
335 - slave->offset + slave->mtd.size, slave->mtd.name);
336 -
337 - /* let's do some sanity checks */
338 - if (slave->offset >= master->size) {
339 - /* let's register it anyway to preserve ordering */
340 - slave->offset = 0;
341 - slave->mtd.size = 0;
342 - printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
343 - parts[i].name);
344 - }
345 - if (slave->offset + slave->mtd.size > master->size) {
346 - slave->mtd.size = master->size - slave->offset;
347 - printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
348 - parts[i].name, master->name, slave->mtd.size);
349 - }
350 - if (master->numeraseregions>1) {
351 - /* Deal with variable erase size stuff */
352 - int i;
353 - struct mtd_erase_region_info *regions = master->eraseregions;
354 -
355 - /* Find the first erase regions which is part of this partition. */
356 - for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
357 - ;
358 -
359 - for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
360 - if (slave->mtd.erasesize < regions[i].erasesize) {
361 - slave->mtd.erasesize = regions[i].erasesize;
362 - }
363 - }
364 - } else {
365 - /* Single erase size */
366 - slave->mtd.erasesize = master->erasesize;
367 - }
368 -
369 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
370 - (slave->offset % slave->mtd.erasesize)) {
371 - /* Doesn't start on a boundary of major erase size */
372 - /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
373 - slave->mtd.flags &= ~MTD_WRITEABLE;
374 - printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
375 - parts[i].name);
376 - }
377 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
378 - (slave->mtd.size % slave->mtd.erasesize)) {
379 - slave->mtd.flags &= ~MTD_WRITEABLE;
380 - printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
381 - parts[i].name);
382 - }
383 -
384 - slave->mtd.ecclayout = master->ecclayout;
385 - if (master->block_isbad) {
386 - uint32_t offs = 0;
387 -
388 - while(offs < slave->mtd.size) {
389 - if (master->block_isbad(master,
390 - offs + slave->offset))
391 - slave->mtd.ecc_stats.badblocks++;
392 - offs += slave->mtd.erasesize;
393 - }
394 - }
395 -
396 - if(parts[i].mtdp)
397 - { /* store the object pointer (caller may or may not register it */
398 - *parts[i].mtdp = &slave->mtd;
399 - slave->registered = 0;
400 - }
401 - else
402 - {
403 - /* register our partition */
404 - add_mtd_device(&slave->mtd);
405 - slave->registered = 1;
406 +#endif /* CONFIG_MTD_SPLIT_ROOTFS */
407 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, i);
408 }
409 }
410