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