1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -53,6 +53,16 @@ config MTD_TESTS
4 should normally be compiled as kernel modules. The modules perform
5 various checks and verifications when loaded.
7 +config MTD_ROOTFS_ROOT_DEV
8 + bool "Automatically set 'rootfs' partition to be root filesystem"
9 + depends on MTD_PARTITIONS
12 +config MTD_ROOTFS_SPLIT
13 + bool "Automatically split 'rootfs' partition for squashfs"
14 + depends on MTD_PARTITIONS
17 config MTD_REDBOOT_PARTS
18 tristate "RedBoot partition table parsing"
19 depends on MTD_PARTITIONS
20 --- a/drivers/mtd/mtdpart.c
21 +++ b/drivers/mtd/mtdpart.c
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/mtd/compatmac.h>
26 +#include <linux/root_dev.h>
27 +#include <linux/magic.h>
29 /* Our partition linked list */
30 static LIST_HEAD(mtd_partitions);
31 @@ -35,7 +37,7 @@ struct mtd_part {
32 * the pointer to that structure with this macro.
34 #define PART(x) ((struct mtd_part *)(x))
36 +#define IS_PART(mtd) (mtd->read == part_read)
39 * MTD methods which simply translate the effective address and pass through
40 @@ -502,6 +504,152 @@ out_register:
44 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
45 +#define ROOTFS_SPLIT_NAME "rootfs_data"
46 +#define ROOTFS_REMOVED_NAME "<removed>"
48 +struct squashfs_super_block {
55 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
57 + struct squashfs_super_block sb;
60 + ret = master->read(master, offset, sizeof(sb), &len, (void *) &sb);
61 + if (ret || (len != sizeof(sb))) {
62 + printk(KERN_ALERT "split_squashfs: error occured while reading "
63 + "from \"%s\"\n", master->name);
67 + if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
68 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
74 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
75 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
81 + len = (u32) le64_to_cpu(sb.bytes_used);
82 + len += (offset & 0x000fffff);
83 + len += (master->erasesize - 1);
84 + len &= ~(master->erasesize - 1);
85 + len -= (offset & 0x000fffff);
86 + *split_offset = offset + len;
91 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
93 + struct mtd_partition *dpart;
94 + struct mtd_part *slave = NULL;
95 + struct mtd_part *spart;
96 + int split_offset = 0;
99 + spart = PART(rpart);
100 + ret = split_squashfs(master, spart->offset, &split_offset);
104 + if (split_offset <= 0)
107 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
108 + if (dpart == NULL) {
109 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
110 + ROOTFS_SPLIT_NAME);
114 + memcpy(dpart, part, sizeof(*part));
115 + dpart->name = (unsigned char *)&dpart[1];
116 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
118 + dpart->size = rpart->size - (split_offset - spart->offset);
119 + dpart->offset = split_offset;
124 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
125 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
127 + slave = add_one_partition(master, dpart, 0, split_offset);
132 + rpart->split = &slave->mtd;
137 +static int refresh_rootfs_split(struct mtd_info *mtd)
139 + struct mtd_partition tpart;
140 + struct mtd_part *part;
148 + /* check for the new squashfs offset first */
149 + ret = split_squashfs(part->master, part->offset, &offset);
153 + if ((offset > 0) && !mtd->split) {
154 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
155 + /* if we don't have a rootfs split partition, create a new one */
156 + tpart.name = (char *) mtd->name;
157 + tpart.size = mtd->size;
158 + tpart.offset = part->offset;
160 + return split_rootfs_data(part->master, &part->mtd, &tpart);
161 + } else if ((offset > 0) && mtd->split) {
162 + /* update the offsets of the existing partition */
163 + size = mtd->size + part->offset - offset;
165 + part = PART(mtd->split);
166 + part->offset = offset;
167 + part->mtd.size = size;
168 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
169 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
170 + (u32) part->offset, (u32) part->mtd.size);
171 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
172 + strcpy(name, ROOTFS_SPLIT_NAME);
173 + part->mtd.name = name;
174 + } else if ((offset <= 0) && mtd->split) {
175 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
177 + /* mark existing partition as removed */
178 + part = PART(mtd->split);
179 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
180 + strcpy(name, ROOTFS_REMOVED_NAME);
181 + part->mtd.name = name;
183 + part->mtd.size = 0;
188 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
191 * This function, given a master MTD object and a partition table, creates
192 * and registers slave MTD objects which are bound to the master according to
193 @@ -517,7 +665,7 @@ int add_mtd_partitions(struct mtd_info *
195 struct mtd_part *slave;
196 uint64_t cur_offset = 0;
200 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
202 @@ -525,6 +673,21 @@ int add_mtd_partitions(struct mtd_info *
203 slave = add_one_partition(master, parts + i, i, cur_offset);
207 + if (!strcmp(parts[i].name, "rootfs")) {
208 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
209 + if (ROOT_DEV == 0) {
210 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
211 + "set to be root filesystem\n");
212 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
215 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
216 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
221 cur_offset = slave->offset + slave->mtd.size;
224 @@ -532,6 +695,32 @@ int add_mtd_partitions(struct mtd_info *
226 EXPORT_SYMBOL(add_mtd_partitions);
228 +int refresh_mtd_partitions(struct mtd_info *mtd)
232 + if (IS_PART(mtd)) {
233 + struct mtd_part *part;
234 + struct mtd_info *master;
237 + master = part->master;
238 + if (master->refresh_device)
239 + ret = master->refresh_device(master);
242 + if (!ret && mtd->refresh_device)
243 + ret = mtd->refresh_device(mtd);
245 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
246 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
247 + refresh_rootfs_split(mtd);
252 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
254 static DEFINE_SPINLOCK(part_parser_lock);
255 static LIST_HEAD(part_parsers);
257 --- a/drivers/mtd/devices/block2mtd.c
258 +++ b/drivers/mtd/devices/block2mtd.c
259 @@ -29,6 +29,8 @@ struct block2mtd_dev {
260 struct block_device *blkdev;
262 struct mutex write_mutex;
263 + rwlock_t bdev_mutex;
268 @@ -81,6 +83,12 @@ static int block2mtd_erase(struct mtd_in
269 size_t len = instr->len;
272 + read_lock(&dev->bdev_mutex);
273 + if (!dev->blkdev) {
278 instr->state = MTD_ERASING;
279 mutex_lock(&dev->write_mutex);
280 err = _block2mtd_erase(dev, from, len);
281 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
283 instr->state = MTD_ERASE_DONE;
284 mtd_erase_callback(instr);
287 + read_unlock(&dev->bdev_mutex);
292 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
294 int index = from >> PAGE_SHIFT;
295 int offset = from & (PAGE_SIZE-1);
297 + int cpylen, err = 0;
299 + read_lock(&dev->bdev_mutex);
300 + if (!dev->blkdev || (from > mtd->size)) {
305 - if (from > mtd->size)
307 if (from + len > mtd->size)
308 len = mtd->size - from;
310 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
313 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
317 - return PTR_ERR(page);
322 + if (IS_ERR(page)) {
323 + err = PTR_ERR(page);
327 memcpy(buf, page_address(page) + offset, cpylen);
328 page_cache_release(page);
329 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
336 + read_unlock(&dev->bdev_mutex);
341 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
342 size_t *retlen, const u_char *buf)
344 struct block2mtd_dev *dev = mtd->priv;
348 + read_lock(&dev->bdev_mutex);
349 + if (!dev->blkdev) {
356 - if (to >= mtd->size)
360 + if (to >= mtd->size) {
365 if (to + len > mtd->size)
366 len = mtd->size - to;
368 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
369 mutex_unlock(&dev->write_mutex);
374 + read_unlock(&dev->bdev_mutex);
378 @@ -210,52 +246,29 @@ static int block2mtd_write(struct mtd_in
379 static void block2mtd_sync(struct mtd_info *mtd)
381 struct block2mtd_dev *dev = mtd->priv;
382 - sync_blockdev(dev->blkdev);
387 -static void block2mtd_free_device(struct block2mtd_dev *dev)
392 - kfree(dev->mtd.name);
395 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
397 - close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
399 + read_lock(&dev->bdev_mutex);
401 + sync_blockdev(dev->blkdev);
402 + read_unlock(&dev->bdev_mutex);
409 -/* FIXME: ensure that mtd->size % erase_size == 0 */
410 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
411 +static int _open_bdev(struct block2mtd_dev *dev)
413 struct block_device *bdev;
414 - struct block2mtd_dev *dev;
415 - struct mtd_partition *part;
421 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
425 /* Get a handle on the device */
426 - bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
427 + bdev = open_bdev_exclusive(dev->devname, FMODE_READ|FMODE_WRITE, NULL);
431 /* We might not have rootfs mounted at this point. Try
432 to resolve the device name by other means. */
434 - dev_t devt = name_to_dev_t(devname);
435 + dev_t devt = name_to_dev_t(dev->devname);
437 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
439 @@ -263,17 +276,98 @@ static struct block2mtd_dev *add_device(
443 - ERROR("error: cannot open device %s", devname);
445 + ERROR("error: cannot open device %s", dev->devname);
450 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
451 ERROR("attempting to use an MTD device as a block device");
459 +static void _close_bdev(struct block2mtd_dev *dev)
461 + struct block_device *bdev;
466 + bdev = dev->blkdev;
467 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
468 + close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
469 + dev->blkdev = NULL;
472 +static void block2mtd_free_device(struct block2mtd_dev *dev)
477 + kfree(dev->mtd.name);
483 +static int block2mtd_refresh(struct mtd_info *mtd)
485 + struct block2mtd_dev *dev = mtd->priv;
486 + struct block_device *bdev;
490 + /* no other mtd function can run at this point */
491 + write_lock(&dev->bdev_mutex);
493 + /* get the device number for the whole disk */
494 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
496 + /* close the old block device */
499 + /* open the whole disk, issue a partition rescan, then */
500 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
501 + if (!bdev || !bdev->bd_disk)
503 +#ifndef CONFIG_MTD_BLOCK2MTD_MODULE
505 + err = rescan_partitions(bdev->bd_disk, bdev);
508 + close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
510 + /* try to open the partition block device again */
512 + write_unlock(&dev->bdev_mutex);
517 +/* FIXME: ensure that mtd->size % erase_size == 0 */
518 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
520 + struct block2mtd_dev *dev;
521 + struct mtd_partition *part;
527 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
531 + strcpy(dev->devname, devname);
533 + if (_open_bdev(dev))
536 mutex_init(&dev->write_mutex);
537 + rwlock_init(&dev->bdev_mutex);
541 @@ -297,6 +391,7 @@ static struct block2mtd_dev *add_device(
542 dev->mtd.read = block2mtd_read;
544 dev->mtd.owner = THIS_MODULE;
545 + dev->mtd.refresh_device = block2mtd_refresh;
547 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
548 part->name = dev->mtd.name;
549 --- a/drivers/mtd/mtdchar.c
550 +++ b/drivers/mtd/mtdchar.c
553 #include <linux/mtd/mtd.h>
554 #include <linux/mtd/compatmac.h>
555 +#include <linux/mtd/partitions.h>
557 #include <asm/uaccess.h>
559 @@ -814,6 +815,13 @@ static int mtd_ioctl(struct inode *inode
563 +#ifdef CONFIG_MTD_PARTITIONS
566 + ret = refresh_mtd_partitions(mtd);
573 --- a/include/linux/mtd/mtd.h
574 +++ b/include/linux/mtd/mtd.h
575 @@ -101,6 +101,7 @@ struct mtd_oob_ops {
583 @@ -241,6 +242,9 @@ struct mtd_info {
587 + int (*refresh_device)(struct mtd_info *mtd);
588 + struct mtd_info *split;
590 /* If the driver is something smart, like UBI, it may need to maintain
591 * its own reference counting. The below functions are only for driver.
592 * The driver may register its callbacks. These callbacks are not
593 --- a/include/linux/mtd/partitions.h
594 +++ b/include/linux/mtd/partitions.h
596 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
599 +struct mtd_partition;
600 struct mtd_partition {
601 char *name; /* identifier string */
602 uint64_t size; /* partition size */
603 uint64_t offset; /* offset within the master MTD space */
604 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
605 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only)*/
606 + int (*refresh_partition)(struct mtd_info *);
609 #define MTDPART_OFS_NXTBLK (-2)
610 @@ -51,6 +53,7 @@ struct mtd_info;
612 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
613 int del_mtd_partitions(struct mtd_info *);
614 +int refresh_mtd_partitions(struct mtd_info *);
617 * Functions dealing with the various ways of partitioning the space
618 --- a/include/mtd/mtd-abi.h
619 +++ b/include/mtd/mtd-abi.h
620 @@ -110,6 +110,7 @@ struct otp_info {
621 #define MEMERASE64 _IOW('M', 20, struct erase_info_user64)
622 #define MEMWRITEOOB64 _IOWR('M', 21, struct mtd_oob_buf64)
623 #define MEMREADOOB64 _IOWR('M', 22, struct mtd_oob_buf64)
624 +#define MTDREFRESH _IO('M', 50)
627 * Obsolete legacy interface. Keep it in order not to break userspace