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