1 Index: linux-2.6.38-rc6/drivers/mtd/Kconfig
2 ===================================================================
3 --- linux-2.6.38-rc6.orig/drivers/mtd/Kconfig 2011-02-28 16:14:12.668466637 +0100
4 +++ linux-2.6.38-rc6/drivers/mtd/Kconfig 2011-02-28 16:26:55.463734599 +0100
5 @@ -55,6 +55,16 @@ config MTD_PARTITIONS
9 +config MTD_ROOTFS_ROOT_DEV
10 + bool "Automatically set 'rootfs' partition to be root filesystem"
11 + depends on MTD_PARTITIONS
14 +config MTD_ROOTFS_SPLIT
15 + bool "Automatically split 'rootfs' partition for squashfs"
16 + depends on MTD_PARTITIONS
19 config MTD_REDBOOT_PARTS
20 tristate "RedBoot partition table parsing"
22 Index: linux-2.6.38-rc6/drivers/mtd/mtdpart.c
23 ===================================================================
24 --- linux-2.6.38-rc6.orig/drivers/mtd/mtdpart.c 2011-02-28 16:14:12.686466221 +0100
25 +++ linux-2.6.38-rc6/drivers/mtd/mtdpart.c 2011-02-28 16:28:41.399156164 +0100
27 #include <linux/kmod.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/partitions.h>
30 +#include <linux/root_dev.h>
31 +#include <linux/magic.h>
32 #include <linux/err.h>
34 /* Our partition linked list */
35 @@ -48,7 +50,7 @@ struct mtd_part {
36 * the pointer to that structure with this macro.
38 #define PART(x) ((struct mtd_part *)(x))
40 +#define IS_PART(mtd) (mtd->read == part_read)
43 * MTD methods which simply translate the effective address and pass through
44 @@ -636,6 +638,153 @@ int mtd_del_partition(struct mtd_info *m
46 EXPORT_SYMBOL_GPL(mtd_del_partition);
48 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
49 +#define ROOTFS_SPLIT_NAME "rootfs_data"
50 +#define ROOTFS_REMOVED_NAME "<removed>"
52 +struct squashfs_super_block {
59 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
61 + struct squashfs_super_block sb;
64 + ret = master->read(master, offset, sizeof(sb), &len, (void *) &sb);
65 + if (ret || (len != sizeof(sb))) {
66 + printk(KERN_ALERT "split_squashfs: error occured while reading "
67 + "from \"%s\"\n", master->name);
71 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
72 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
78 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
79 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
85 + len = (u32) le64_to_cpu(sb.bytes_used);
86 + len += (offset & 0x000fffff);
87 + len += (master->erasesize - 1);
88 + len &= ~(master->erasesize - 1);
89 + len -= (offset & 0x000fffff);
90 + *split_offset = offset + len;
95 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
97 + struct mtd_partition *dpart;
98 + struct mtd_part *slave = NULL;
99 + int ret, split_offset = 0;
101 + ret = split_squashfs(master, part->offset, &split_offset);
105 + if (split_offset <= 0)
108 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
109 + if (dpart == NULL) {
110 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
111 + ROOTFS_SPLIT_NAME);
115 + memcpy(dpart, part, sizeof(*part));
116 + dpart->name = (unsigned char *)&dpart[1];
117 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
119 + dpart->size -= split_offset - dpart->offset;
120 + dpart->offset = split_offset;
125 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
126 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
128 + slave = allocate_partition(master, dpart, 0, split_offset);
130 + return PTR_ERR(slave);
131 + mutex_lock(&mtd_partitions_mutex);
132 + list_add(&slave->list, &mtd_partitions);
133 + mutex_unlock(&mtd_partitions_mutex);
135 + add_mtd_device(&slave->mtd);
137 + rpart->split = &slave->mtd;
142 +static int refresh_rootfs_split(struct mtd_info *mtd)
144 + struct mtd_partition tpart;
145 + struct mtd_part *part;
153 + /* check for the new squashfs offset first */
154 + ret = split_squashfs(part->master, part->offset, &offset);
158 + if ((offset > 0) && !mtd->split) {
159 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
160 + /* if we don't have a rootfs split partition, create a new one */
161 + tpart.name = (char *) mtd->name;
162 + tpart.size = mtd->size;
163 + tpart.offset = part->offset;
165 + return split_rootfs_data(part->master, &part->mtd, &tpart);
166 + } else if ((offset > 0) && mtd->split) {
167 + /* update the offsets of the existing partition */
168 + size = mtd->size + part->offset - offset;
170 + part = PART(mtd->split);
171 + part->offset = offset;
172 + part->mtd.size = size;
173 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
174 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
175 + (u32) part->offset, (u32) part->mtd.size);
176 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
177 + strcpy(name, ROOTFS_SPLIT_NAME);
178 + part->mtd.name = name;
179 + } else if ((offset <= 0) && mtd->split) {
180 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
182 + /* mark existing partition as removed */
183 + part = PART(mtd->split);
184 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
185 + strcpy(name, ROOTFS_REMOVED_NAME);
186 + part->mtd.name = name;
188 + part->mtd.size = 0;
193 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
196 * This function, given a master MTD object and a partition table, creates
197 * and registers slave MTD objects which are bound to the master according to
198 @@ -652,6 +801,9 @@ int add_mtd_partitions(struct mtd_info *
199 struct mtd_part *slave;
200 uint64_t cur_offset = 0;
202 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
206 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
208 @@ -666,6 +818,21 @@ int add_mtd_partitions(struct mtd_info *
210 add_mtd_device(&slave->mtd);
212 + if (!strcmp(parts[i].name, "rootfs")) {
213 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
214 + if (ROOT_DEV == 0) {
215 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
216 + "set to be root filesystem\n");
217 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
220 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
221 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
227 cur_offset = slave->offset + slave->mtd.size;
230 @@ -673,6 +840,32 @@ int add_mtd_partitions(struct mtd_info *
232 EXPORT_SYMBOL(add_mtd_partitions);
234 +int refresh_mtd_partitions(struct mtd_info *mtd)
238 + if (IS_PART(mtd)) {
239 + struct mtd_part *part;
240 + struct mtd_info *master;
243 + master = part->master;
244 + if (master->refresh_device)
245 + ret = master->refresh_device(master);
248 + if (!ret && mtd->refresh_device)
249 + ret = mtd->refresh_device(mtd);
251 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
252 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
253 + refresh_rootfs_split(mtd);
258 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
260 static DEFINE_SPINLOCK(part_parser_lock);
261 static LIST_HEAD(part_parsers);
263 Index: linux-2.6.38-rc6/drivers/mtd/devices/block2mtd.c
264 ===================================================================
265 --- linux-2.6.38-rc6.orig/drivers/mtd/devices/block2mtd.c 2011-02-28 16:15:07.477184360 +0100
266 +++ linux-2.6.38-rc6/drivers/mtd/devices/block2mtd.c 2011-02-28 16:26:55.919727057 +0100
267 @@ -30,6 +30,8 @@ struct block2mtd_dev {
268 struct block_device *blkdev;
270 struct mutex write_mutex;
271 + rwlock_t bdev_mutex;
276 @@ -82,6 +84,12 @@ static int block2mtd_erase(struct mtd_in
277 size_t len = instr->len;
280 + read_lock(&dev->bdev_mutex);
281 + if (!dev->blkdev) {
286 instr->state = MTD_ERASING;
287 mutex_lock(&dev->write_mutex);
288 err = _block2mtd_erase(dev, from, len);
289 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
290 instr->state = MTD_ERASE_DONE;
292 mtd_erase_callback(instr);
295 + read_unlock(&dev->bdev_mutex);
300 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
302 int index = from >> PAGE_SHIFT;
303 int offset = from & (PAGE_SIZE-1);
305 + int cpylen, err = 0;
307 + read_lock(&dev->bdev_mutex);
308 + if (!dev->blkdev || (from > mtd->size)) {
313 - if (from > mtd->size)
315 if (from + len > mtd->size)
316 len = mtd->size - from;
318 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
321 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
325 - return PTR_ERR(page);
330 + if (IS_ERR(page)) {
331 + err = PTR_ERR(page);
335 memcpy(buf, page_address(page) + offset, cpylen);
336 page_cache_release(page);
337 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
344 + read_unlock(&dev->bdev_mutex);
349 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
350 size_t *retlen, const u_char *buf)
352 struct block2mtd_dev *dev = mtd->priv;
356 + read_lock(&dev->bdev_mutex);
357 + if (!dev->blkdev) {
364 - if (to >= mtd->size)
368 + if (to >= mtd->size) {
373 if (to + len > mtd->size)
374 len = mtd->size - to;
376 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
377 mutex_unlock(&dev->write_mutex);
382 + read_unlock(&dev->bdev_mutex);
386 @@ -210,33 +246,109 @@ static int block2mtd_write(struct mtd_in
387 static void block2mtd_sync(struct mtd_info *mtd)
389 struct block2mtd_dev *dev = mtd->priv;
390 + read_lock(&dev->bdev_mutex);
392 sync_blockdev(dev->blkdev);
393 + read_unlock(&dev->bdev_mutex);
399 +static int _open_bdev(struct block2mtd_dev *dev)
401 + const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
402 + struct block_device *bdev;
404 + /* Get a handle on the device */
405 + bdev = blkdev_get_by_path(dev->devname, mode, dev);
407 + if (IS_ERR(bdev)) {
409 + /* We might not have rootfs mounted at this point. Try
410 + to resolve the device name by other means. */
412 + dev_t devt = name_to_dev_t(dev->devname);
414 + bdev = blkdev_get_by_dev(devt, mode, dev);
418 + if (IS_ERR(bdev)) {
419 + ERROR("error: cannot open device %s", dev->devname);
422 + dev->blkdev = bdev;
424 + if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
425 + ERROR("attempting to use an MTD device as a block device");
432 +static void _close_bdev(struct block2mtd_dev *dev)
434 + struct block_device *bdev;
439 + bdev = dev->blkdev;
440 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
441 + blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
442 + dev->blkdev = NULL;
445 static void block2mtd_free_device(struct block2mtd_dev *dev)
450 kfree(dev->mtd.name);
453 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
455 - blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
463 -/* FIXME: ensure that mtd->size % erase_size == 0 */
464 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
465 +static int block2mtd_refresh(struct mtd_info *mtd)
467 - const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
468 + struct block2mtd_dev *dev = mtd->priv;
469 struct block_device *bdev;
473 + /* no other mtd function can run at this point */
474 + write_lock(&dev->bdev_mutex);
476 + /* get the device number for the whole disk */
477 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
479 + /* close the old block device */
482 + /* open the whole disk, issue a partition rescan, then */
483 + bdev = blkdev_get_by_dev(devt, FMODE_WRITE | FMODE_READ);
484 + if (!bdev || !bdev->bd_disk)
486 +#ifndef CONFIG_MTD_BLOCK2MTD_MODULE
488 + err = rescan_partitions(bdev->bd_disk, bdev);
491 + blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
493 + /* try to open the partition block device again */
495 + write_unlock(&dev->bdev_mutex);
500 +/* FIXME: ensure that mtd->size % erase_size == 0 */
501 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
503 struct block2mtd_dev *dev;
504 struct mtd_partition *part;
506 @@ -244,36 +356,17 @@ static struct block2mtd_dev *add_device(
510 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
511 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
515 - /* Get a handle on the device */
516 - bdev = blkdev_get_by_path(devname, mode, dev);
518 - if (IS_ERR(bdev)) {
520 - /* We might not have rootfs mounted at this point. Try
521 - to resolve the device name by other means. */
522 + strcpy(dev->devname, devname);
524 - dev_t devt = name_to_dev_t(devname);
526 - bdev = blkdev_get_by_dev(devt, mode, dev);
530 - if (IS_ERR(bdev)) {
531 - ERROR("error: cannot open device %s", devname);
532 + if (_open_bdev(dev))
535 - dev->blkdev = bdev;
537 - if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
538 - ERROR("attempting to use an MTD device as a block device");
542 mutex_init(&dev->write_mutex);
543 + rwlock_init(&dev->bdev_mutex);
545 /* Setup the MTD structure */
546 /* make the name contain the block device in */
547 @@ -298,6 +391,7 @@ static struct block2mtd_dev *add_device(
548 dev->mtd.read = block2mtd_read;
550 dev->mtd.owner = THIS_MODULE;
551 + dev->mtd.refresh_device = block2mtd_refresh;
553 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
554 part->name = dev->mtd.name;
555 Index: linux-2.6.38-rc6/drivers/mtd/mtdchar.c
556 ===================================================================
557 --- linux-2.6.38-rc6.orig/drivers/mtd/mtdchar.c 2011-02-28 16:14:12.652467007 +0100
558 +++ linux-2.6.38-rc6/drivers/mtd/mtdchar.c 2011-02-28 16:15:07.503183744 +0100
559 @@ -841,6 +841,13 @@ static int mtd_ioctl(struct file *file,
563 +#ifdef CONFIG_MTD_PARTITIONS
566 + ret = refresh_mtd_partitions(mtd);
571 case OTPGETREGIONCOUNT:
572 case OTPGETREGIONINFO:
573 Index: linux-2.6.38-rc6/include/linux/mtd/mtd.h
574 ===================================================================
575 --- linux-2.6.38-rc6.orig/include/linux/mtd/mtd.h 2011-02-28 16:14:12.613467908 +0100
576 +++ linux-2.6.38-rc6/include/linux/mtd/mtd.h 2011-02-28 16:26:51.223804561 +0100
577 @@ -125,6 +125,7 @@ struct nand_ecclayout {
578 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
585 @@ -277,6 +278,9 @@ struct mtd_info {
589 + int (*refresh_device)(struct mtd_info *mtd);
590 + struct mtd_info *split;
592 /* If the driver is something smart, like UBI, it may need to maintain
593 * its own reference counting. The below functions are only for driver.
594 * The driver may register its callbacks. These callbacks are not
595 Index: linux-2.6.38-rc6/include/linux/mtd/partitions.h
596 ===================================================================
597 --- linux-2.6.38-rc6.orig/include/linux/mtd/partitions.h 2011-02-28 16:14:12.635467400 +0100
598 +++ linux-2.6.38-rc6/include/linux/mtd/partitions.h 2011-02-28 16:26:55.382735938 +0100
600 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
603 +struct mtd_partition;
604 struct mtd_partition {
605 char *name; /* identifier string */
606 uint64_t size; /* partition size */
607 uint64_t offset; /* offset within the master MTD space */
608 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
609 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
610 + int (*refresh_partition)(struct mtd_info *);
613 #define MTDPART_OFS_NXTBLK (-2)
614 @@ -51,6 +53,7 @@ struct mtd_info;
616 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
617 int del_mtd_partitions(struct mtd_info *);
618 +int refresh_mtd_partitions(struct mtd_info *);
621 * Functions dealing with the various ways of partitioning the space
622 Index: linux-2.6.38-rc6/include/mtd/mtd-abi.h
623 ===================================================================
624 --- linux-2.6.38-rc6.orig/include/mtd/mtd-abi.h 2011-02-28 16:14:12.596468301 +0100
625 +++ linux-2.6.38-rc6/include/mtd/mtd-abi.h 2011-02-28 16:15:07.505183696 +0100
626 @@ -127,6 +127,7 @@ struct otp_info {
627 #define MEMWRITEOOB64 _IOWR('M', 21, struct mtd_oob_buf64)
628 #define MEMREADOOB64 _IOWR('M', 22, struct mtd_oob_buf64)
629 #define MEMISLOCKED _IOR('M', 23, struct erase_info_user)
630 +#define MTDREFRESH _IO('M', 23)
633 * Obsolete legacy interface. Keep it in order not to break userspace