added directory for generic patches for linux kernel 2.6.29 - added ported patch...
[openwrt/staging/dedeckeh.git] / target / linux / generic-2.6 / patches-2.6.29 / 065-rootfs_split.patch
1 diff -ruN linux-2.6.29-rc3.orig/drivers/mtd/devices/block2mtd.c linux-2.6.29-rc3/drivers/mtd/devices/block2mtd.c
2 --- linux-2.6.29-rc3.orig/drivers/mtd/devices/block2mtd.c 2009-03-09 05:13:51.000000000 +0100
3 +++ linux-2.6.29-rc3/drivers/mtd/devices/block2mtd.c 2009-03-09 18:18:49.000000000 +0100
4 @@ -28,6 +28,8 @@
5 struct block_device *blkdev;
6 struct mtd_info mtd;
7 struct mutex write_mutex;
8 + rwlock_t bdev_mutex;
9 + char devname[0];
10 };
11
12
13 @@ -80,6 +82,12 @@
14 size_t len = instr->len;
15 int err;
16
17 + read_lock(&dev->bdev_mutex);
18 + if (!dev->blkdev) {
19 + err = -EINVAL;
20 + goto done;
21 + }
22 +
23 instr->state = MTD_ERASING;
24 mutex_lock(&dev->write_mutex);
25 err = _block2mtd_erase(dev, from, len);
26 @@ -92,6 +100,10 @@
27
28 instr->state = MTD_ERASE_DONE;
29 mtd_erase_callback(instr);
30 +
31 +done:
32 + read_unlock(&dev->bdev_mutex);
33 +
34 return err;
35 }
36
37 @@ -103,10 +115,14 @@
38 struct page *page;
39 int index = from >> PAGE_SHIFT;
40 int offset = from & (PAGE_SIZE-1);
41 - int cpylen;
42 + int cpylen, err = 0;
43 +
44 + read_lock(&dev->bdev_mutex);
45 + if (!dev->blkdev || (from > mtd->size)) {
46 + err = -EINVAL;
47 + goto done;
48 + }
49
50 - if (from > mtd->size)
51 - return -EINVAL;
52 if (from + len > mtd->size)
53 len = mtd->size - from;
54
55 @@ -121,10 +137,14 @@
56 len = len - cpylen;
57
58 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
59 - if (!page)
60 - return -ENOMEM;
61 - if (IS_ERR(page))
62 - return PTR_ERR(page);
63 + if (!page) {
64 + err = -ENOMEM;
65 + goto done;
66 + }
67 + if (IS_ERR(page)) {
68 + err = PTR_ERR(page);
69 + goto done;
70 + }
71
72 memcpy(buf, page_address(page) + offset, cpylen);
73 page_cache_release(page);
74 @@ -135,7 +155,10 @@
75 offset = 0;
76 index++;
77 }
78 - return 0;
79 +
80 +done:
81 + read_unlock(&dev->bdev_mutex);
82 + return err;
83 }
84
85
86 @@ -187,12 +210,22 @@
87 size_t *retlen, const u_char *buf)
88 {
89 struct block2mtd_dev *dev = mtd->priv;
90 - int err;
91 + int err = 0;
92 +
93 + read_lock(&dev->bdev_mutex);
94 + if (!dev->blkdev) {
95 + err = -EINVAL;
96 + goto done;
97 + }
98
99 if (!len)
100 - return 0;
101 - if (to >= mtd->size)
102 - return -ENOSPC;
103 + goto done;
104 +
105 + if (to >= mtd->size) {
106 + err = -ENOSPC;
107 + goto done;
108 + }
109 +
110 if (to + len > mtd->size)
111 len = mtd->size - to;
112
113 @@ -201,6 +234,9 @@
114 mutex_unlock(&dev->write_mutex);
115 if (err > 0)
116 err = 0;
117 +
118 +done:
119 + read_unlock(&dev->bdev_mutex);
120 return err;
121 }
122
123 @@ -209,30 +245,15 @@
124 static void block2mtd_sync(struct mtd_info *mtd)
125 {
126 struct block2mtd_dev *dev = mtd->priv;
127 - sync_blockdev(dev->blkdev);
128 - return;
129 -}
130 -
131 + read_lock(&dev->bdev_mutex);
132 + if (dev->blkdev)
133 + sync_blockdev(dev->blkdev);
134 + read_unlock(&dev->bdev_mutex);
135
136 -static void block2mtd_free_device(struct block2mtd_dev *dev)
137 -{
138 - if (!dev)
139 - return;
140 -
141 - kfree(dev->mtd.name);
142 -
143 - if (dev->blkdev) {
144 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
145 - 0, -1);
146 - close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
147 - }
148 -
149 - kfree(dev);
150 + return;
151 }
152
153 -
154 -/* FIXME: ensure that mtd->size % erase_size == 0 */
155 -static struct block2mtd_dev *add_device(char *devname, int erase_size)
156 +static int _open_bdev(struct block2mtd_dev *dev)
157 {
158 struct block_device *bdev;
159 struct block2mtd_dev *dev;
160 @@ -246,14 +267,14 @@
161 return NULL;
162
163 /* Get a handle on the device */
164 - bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
165 + bdev = open_bdev_exclusive(dev->devname, FMODE_READ|FMODE_WRITE, NULL);
166 #ifndef MODULE
167 if (IS_ERR(bdev)) {
168
169 /* We might not have rootfs mounted at this point. Try
170 to resolve the device name by other means. */
171
172 - dev_t devt = name_to_dev_t(devname);
173 + dev_t devt = name_to_dev_t(dev->devname);
174 if (devt) {
175 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
176 }
177 @@ -261,17 +282,97 @@
178 #endif
179
180 if (IS_ERR(bdev)) {
181 - ERROR("error: cannot open device %s", devname);
182 - goto devinit_err;
183 + ERROR("error: cannot open device %s", dev->devname);
184 + return 1;
185 }
186 dev->blkdev = bdev;
187
188 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
189 ERROR("attempting to use an MTD device as a block device");
190 - goto devinit_err;
191 + return 1;
192 }
193
194 + return 0;
195 +}
196 +
197 +static void _close_bdev(struct block2mtd_dev *dev)
198 +{
199 + struct block_device *bdev;
200 +
201 + if (!dev->blkdev)
202 + return;
203 +
204 + bdev = dev->blkdev;
205 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
206 + close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
207 + dev->blkdev = NULL;
208 +}
209 +
210 +static void block2mtd_free_device(struct block2mtd_dev *dev)
211 +{
212 + if (!dev)
213 + return;
214 +
215 + kfree(dev->mtd.name);
216 + _close_bdev(dev);
217 + kfree(dev);
218 +}
219 +
220 +
221 +static int block2mtd_refresh(struct mtd_info *mtd)
222 +{
223 + struct block2mtd_dev *dev = mtd->priv;
224 + struct block_device *bdev;
225 + dev_t devt;
226 + int err = 0;
227 +
228 + /* no other mtd function can run at this point */
229 + write_lock(&dev->bdev_mutex);
230 +
231 + /* get the device number for the whole disk */
232 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
233 +
234 + /* close the old block device */
235 + _close_bdev(dev);
236 +
237 + /* open the whole disk, issue a partition rescan, then */
238 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
239 + if (!bdev || !bdev->bd_disk)
240 + err = -EINVAL;
241 + else {
242 + err = rescan_partitions(bdev->bd_disk, bdev);
243 + }
244 + if (bdev)
245 + close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
246 +
247 + /* try to open the partition block device again */
248 + _open_bdev(dev);
249 + write_unlock(&dev->bdev_mutex);
250 +
251 + return err;
252 +}
253 +
254 +/* FIXME: ensure that mtd->size % erase_size == 0 */
255 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
256 +{
257 + struct block2mtd_dev *dev;
258 + struct mtd_partition *part;
259 + char *name;
260 +
261 + if (!devname)
262 + return NULL;
263 +
264 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
265 + if (!dev)
266 + return NULL;
267 +
268 + strcpy(dev->devname, devname);
269 +
270 + if (_open_bdev(dev))
271 + goto devinit_err;
272 +
273 mutex_init(&dev->write_mutex);
274 + rwlock_init(&dev->bdev_mutex);
275
276 /* Setup the MTD structure */
277 /* make the name contain the block device in */
278 @@ -295,6 +396,7 @@
279 dev->mtd.read = block2mtd_read;
280 dev->mtd.priv = dev;
281 dev->mtd.owner = THIS_MODULE;
282 + dev->mtd.refresh_device = block2mtd_refresh;
283
284 if (add_mtd_device(&dev->mtd)) {
285 /* Device didnt get added, so free the entry */
286 diff -ruN linux-2.6.29-rc3.orig/drivers/mtd/Kconfig linux-2.6.29-rc3/drivers/mtd/Kconfig
287 --- linux-2.6.29-rc3.orig/drivers/mtd/Kconfig 2009-03-09 05:13:51.000000000 +0100
288 +++ linux-2.6.29-rc3/drivers/mtd/Kconfig 2009-03-09 18:10:48.000000000 +0100
289 @@ -53,6 +53,16 @@
290 should normally be compiled as kernel modules. The modules perform
291 various checks and verifications when loaded.
292
293 +config MTD_ROOTFS_ROOT_DEV
294 + bool "Automatically set 'rootfs' partition to be root filesystem"
295 + depends on MTD_PARTITIONS
296 + default y
297 +
298 +config MTD_ROOTFS_SPLIT
299 + bool "Automatically split 'rootfs' partition for squashfs"
300 + depends on MTD_PARTITIONS
301 + default y
302 +
303 config MTD_REDBOOT_PARTS
304 tristate "RedBoot partition table parsing"
305 depends on MTD_PARTITIONS
306 diff -ruN linux-2.6.29-rc3.orig/drivers/mtd/mtdchar.c linux-2.6.29-rc3/drivers/mtd/mtdchar.c
307 --- linux-2.6.29-rc3.orig/drivers/mtd/mtdchar.c 2009-03-09 05:13:51.000000000 +0100
308 +++ linux-2.6.29-rc3/drivers/mtd/mtdchar.c 2009-03-09 18:10:48.000000000 +0100
309 @@ -16,6 +16,7 @@
310
311 #include <linux/mtd/mtd.h>
312 #include <linux/mtd/compatmac.h>
313 +#include <linux/mtd/partitions.h>
314
315 #include <asm/uaccess.h>
316
317 @@ -773,6 +774,13 @@
318 file->f_pos = 0;
319 break;
320 }
321 +#ifdef CONFIG_MTD_PARTITIONS
322 + case MTDREFRESH:
323 + {
324 + ret = refresh_mtd_partitions(mtd);
325 + break;
326 + }
327 +#endif
328
329 default:
330 ret = -ENOTTY;
331 diff -ruN linux-2.6.29-rc3.orig/drivers/mtd/mtdpart.c linux-2.6.29-rc3/drivers/mtd/mtdpart.c
332 --- linux-2.6.29-rc3.orig/drivers/mtd/mtdpart.c 2009-03-09 05:13:51.000000000 +0100
333 +++ linux-2.6.29-rc3/drivers/mtd/mtdpart.c 2009-03-09 18:29:58.000000000 +0100
334 @@ -18,6 +18,8 @@
335 #include <linux/mtd/mtd.h>
336 #include <linux/mtd/partitions.h>
337 #include <linux/mtd/compatmac.h>
338 +#include <linux/squashfs_fs.h>
339 +#include <linux/root_dev.h>
340
341 /* Our partition linked list */
342 static LIST_HEAD(mtd_partitions);
343 @@ -37,7 +39,7 @@
344 * the pointer to that structure with this macro.
345 */
346 #define PART(x) ((struct mtd_part *)(x))
347 -
348 +#define IS_PART(mtd) (mtd->read == part_read)
349
350 /*
351 * MTD methods which simply translate the effective address and pass through
352 @@ -489,6 +491,148 @@
353 return slave;
354 }
355
356 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
357 +#define ROOTFS_SPLIT_NAME "rootfs_data"
358 +#define ROOTFS_REMOVED_NAME "<removed>"
359 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
360 +{
361 + char buf[512];
362 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
363 + int len, ret;
364 +
365 + ret = master->read(master, offset, sizeof(*sb), &len, buf);
366 + if (ret || (len != sizeof(*sb))) {
367 + printk(KERN_ALERT "split_squashfs: error occured while reading "
368 + "from \"%s\"\n", master->name);
369 + return -EINVAL;
370 + }
371 +
372 + if (*((u32 *) buf) != SQUASHFS_MAGIC) {
373 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
374 + master->name);
375 + *split_offset = 0;
376 + return 0;
377 + }
378 +
379 + if (sb->bytes_used <= 0) {
380 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
381 + master->name);
382 + *split_offset = 0;
383 + return 0;
384 + }
385 +
386 + len = (u32) sb->bytes_used;
387 + len += (offset & 0x000fffff);
388 + len += (master->erasesize - 1);
389 + len &= ~(master->erasesize - 1);
390 + len -= (offset & 0x000fffff);
391 + *split_offset = offset + len;
392 +
393 + return 0;
394 +}
395 +
396 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part,
397 + int index)
398 +{
399 + struct mtd_partition *dpart;
400 + struct mtd_part *slave = NULL;
401 + int split_offset = 0;
402 + int ret;
403 +
404 + ret = split_squashfs(master, part->offset, &split_offset);
405 + if (ret)
406 + return ret;
407 +
408 + if (split_offset <= 0)
409 + return 0;
410 +
411 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
412 + if (dpart == NULL) {
413 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
414 + ROOTFS_SPLIT_NAME);
415 + return -ENOMEM;
416 + }
417 +
418 + memcpy(dpart, part, sizeof(*part));
419 + dpart->name = (unsigned char *)&dpart[1];
420 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
421 +
422 + dpart->size -= split_offset - dpart->offset;
423 + dpart->offset = split_offset;
424 +
425 + if (dpart == NULL)
426 + return 1;
427 +
428 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%X, len=%X \n",
429 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
430 +
431 + slave = add_one_partition(master, dpart, index, split_offset);
432 + if (!slave) {
433 + kfree(dpart);
434 + return -ENOMEM;
435 + }
436 + rpart->split = &slave->mtd;
437 +
438 + return 0;
439 +}
440 +
441 +static int refresh_rootfs_split(struct mtd_info *mtd)
442 +{
443 + struct mtd_partition tpart;
444 + struct mtd_part *part;
445 + char *name;
446 + int index = 0;
447 + int offset, size;
448 + int ret;
449 +
450 + part = PART(mtd);
451 +
452 + /* check for the new squashfs offset first */
453 + ret = split_squashfs(part->master, part->offset, &offset);
454 + if (ret)
455 + return ret;
456 +
457 + if ((offset > 0) && !mtd->split) {
458 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
459 + /* if we don't have a rootfs split partition, create a new one */
460 + tpart.name = (char *) mtd->name;
461 + tpart.size = mtd->size;
462 + tpart.offset = part->offset;
463 +
464 + /* find the index of the last partition */
465 + if (!list_empty(&mtd_partitions))
466 + index = list_first_entry(&mtd_partitions, struct mtd_part, list)->index + 1;
467 +
468 + return split_rootfs_data(part->master, &part->mtd, &tpart, index);
469 + } else if ((offset > 0) && mtd->split) {
470 + /* update the offsets of the existing partition */
471 + size = mtd->size + part->offset - offset;
472 +
473 + part = PART(mtd->split);
474 + part->offset = offset;
475 + part->mtd.size = size;
476 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
477 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
478 + part->offset, part->mtd.size);
479 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
480 + strcpy(name, ROOTFS_SPLIT_NAME);
481 + part->mtd.name = name;
482 + } else if ((offset <= 0) && mtd->split) {
483 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
484 +
485 + /* mark existing partition as removed */
486 + part = PART(mtd->split);
487 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
488 + strcpy(name, ROOTFS_REMOVED_NAME);
489 + part->mtd.name = name;
490 + part->offset = 0;
491 + part->mtd.size = 0;
492 + }
493 +
494 + return 0;
495 +}
496 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
497 +
498 /*
499 * This function, given a master MTD object and a partition table, creates
500 * and registers slave MTD objects which are bound to the master according to
501 @@ -502,14 +646,29 @@
502 {
503 struct mtd_part *slave;
504 uint64_t cur_offset = 0;
505 - int i;
506 + int i, j, ret;
507
508 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
509
510 - for (i = 0; i < nbparts; i++) {
511 - slave = add_one_partition(master, parts + i, i, cur_offset);
512 + for (i = 0, j = 0; i < nbparts; i++) {
513 + slave = add_one_partition(master, parts + i, j++, cur_offset);
514 if (!slave)
515 return -ENOMEM;
516 +
517 + if (!strcmp(parts[i].name, "rootfs") && slave->registered) {
518 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
519 + if (ROOT_DEV == 0) {
520 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
521 + "set to be root filesystem\n");
522 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
523 + }
524 +#endif
525 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
526 + ret = split_rootfs_data(master, &slave->mtd, &parts[i], j);
527 + if (ret == 0)
528 + j++;
529 +#endif
530 + }
531 cur_offset = slave->offset + slave->mtd.size;
532 }
533
534 @@ -517,6 +676,32 @@
535 }
536 EXPORT_SYMBOL(add_mtd_partitions);
537
538 +int refresh_mtd_partitions(struct mtd_info *mtd)
539 +{
540 + int ret = 0;
541 +
542 + if (IS_PART(mtd)) {
543 + struct mtd_part *part;
544 + struct mtd_info *master;
545 +
546 + part = PART(mtd);
547 + master = part->master;
548 + if (master->refresh_device)
549 + ret = master->refresh_device(master);
550 + }
551 +
552 + if (!ret && mtd->refresh_device)
553 + ret = mtd->refresh_device(mtd);
554 +
555 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
556 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
557 + refresh_rootfs_split(mtd);
558 +#endif
559 +
560 + return 0;
561 +}
562 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
563 +
564 static DEFINE_SPINLOCK(part_parser_lock);
565 static LIST_HEAD(part_parsers);
566
567 diff -ruN linux-2.6.29-rc3.orig/include/linux/mtd/mtd.h linux-2.6.29-rc3/include/linux/mtd/mtd.h
568 --- linux-2.6.29-rc3.orig/include/linux/mtd/mtd.h 2009-03-09 05:13:58.000000000 +0100
569 +++ linux-2.6.29-rc3/include/linux/mtd/mtd.h 2009-03-09 18:10:48.000000000 +0100
570 @@ -100,6 +100,7 @@
571 uint8_t *oobbuf;
572 };
573
574 +struct mtd_info;
575 struct mtd_info {
576 u_char type;
577 uint32_t flags;
578 @@ -225,6 +226,9 @@
579 struct module *owner;
580 int usecount;
581
582 + int (*refresh_device)(struct mtd_info *mtd);
583 + struct mtd_info *split;
584 +
585 /* If the driver is something smart, like UBI, it may need to maintain
586 * its own reference counting. The below functions are only for driver.
587 * The driver may register its callbacks. These callbacks are not
588 diff -ruN linux-2.6.29-rc3.orig/include/linux/mtd/partitions.h linux-2.6.29-rc3/include/linux/mtd/partitions.h
589 --- linux-2.6.29-rc3.orig/include/linux/mtd/partitions.h 2009-03-09 05:13:58.000000000 +0100
590 +++ linux-2.6.29-rc3/include/linux/mtd/partitions.h 2009-03-09 18:10:48.000000000 +0100
591 @@ -34,6 +34,7 @@
592 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
593 */
594
595 +struct mtd_partition;
596 struct mtd_partition {
597 char *name; /* identifier string */
598 uint64_t size; /* partition size */
599 @@ -41,6 +42,7 @@
600 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
601 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only)*/
602 struct mtd_info **mtdp; /* pointer to store the MTD object */
603 + int (*refresh_partition)(struct mtd_info *);
604 };
605
606 #define MTDPART_OFS_NXTBLK (-2)
607 @@ -50,6 +52,7 @@
608
609 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
610 int del_mtd_partitions(struct mtd_info *);
611 +int refresh_mtd_partitions(struct mtd_info *);
612
613 /*
614 * Functions dealing with the various ways of partitioning the space
615 diff -ruN linux-2.6.29-rc3.orig/include/mtd/mtd-abi.h linux-2.6.29-rc3/include/mtd/mtd-abi.h
616 --- linux-2.6.29-rc3.orig/include/mtd/mtd-abi.h 2009-03-09 05:13:57.000000000 +0100
617 +++ linux-2.6.29-rc3/include/mtd/mtd-abi.h 2009-03-09 18:10:48.000000000 +0100
618 @@ -93,6 +93,7 @@
619 #define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout)
620 #define ECCGETSTATS _IOR('M', 18, struct mtd_ecc_stats)
621 #define MTDFILEMODE _IO('M', 19)
622 +#define MTDREFRESH _IO('M', 23)
623
624 /*
625 * Obsolete legacy interface. Keep it in order not to break userspace