1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -53,6 +53,16 @@ config MTD_PARTITIONS
4 devices. Partitioning on NFTL 'devices' is a different - that's the
5 'normal' form of partitioning used on a block device.
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/kmod.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
26 +#include <linux/root_dev.h>
27 +#include <linux/magic.h>
28 #include <linux/err.h>
30 /* Our partition linked list */
31 @@ -48,7 +50,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 @@ -618,6 +620,164 @@ int mtd_del_partition(struct mtd_info *m
42 EXPORT_SYMBOL_GPL(mtd_del_partition);
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 + int split_offset = 0;
98 + ret = split_squashfs(master, part->offset, &split_offset);
102 + if (split_offset <= 0)
105 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
106 + if (dpart == NULL) {
107 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
108 + ROOTFS_SPLIT_NAME);
112 + memcpy(dpart, part, sizeof(*part));
113 + dpart->name = (unsigned char *)&dpart[1];
114 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
116 + dpart->size -= split_offset - dpart->offset;
117 + dpart->offset = split_offset;
122 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
123 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
125 + slave = add_one_partition(master, dpart, 0, split_offset);
130 + if (!strcmp(parts[i].name, "rootfs")) {
131 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
132 + if (ROOT_DEV == 0) {
133 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
134 + "set to be root filesystem\n");
135 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
138 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
139 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
144 + rpart->split = &slave->mtd;
149 +static int refresh_rootfs_split(struct mtd_info *mtd)
151 + struct mtd_partition tpart;
152 + struct mtd_part *part;
160 + /* check for the new squashfs offset first */
161 + ret = split_squashfs(part->master, part->offset, &offset);
165 + if ((offset > 0) && !mtd->split) {
166 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
167 + /* if we don't have a rootfs split partition, create a new one */
168 + tpart.name = (char *) mtd->name;
169 + tpart.size = mtd->size;
170 + tpart.offset = part->offset;
172 + return split_rootfs_data(part->master, &part->mtd, &tpart);
173 + } else if ((offset > 0) && mtd->split) {
174 + /* update the offsets of the existing partition */
175 + size = mtd->size + part->offset - offset;
177 + part = PART(mtd->split);
178 + part->offset = offset;
179 + part->mtd.size = size;
180 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
181 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
182 + (u32) part->offset, (u32) part->mtd.size);
183 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
184 + strcpy(name, ROOTFS_SPLIT_NAME);
185 + part->mtd.name = name;
186 + } else if ((offset <= 0) && mtd->split) {
187 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
189 + /* mark existing partition as removed */
190 + part = PART(mtd->split);
191 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
192 + strcpy(name, ROOTFS_REMOVED_NAME);
193 + part->mtd.name = name;
195 + part->mtd.size = 0;
200 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
203 * This function, given a master MTD object and a partition table, creates
204 * and registers slave MTD objects which are bound to the master according to
205 @@ -633,7 +793,7 @@ int add_mtd_partitions(struct mtd_info *
207 struct mtd_part *slave;
208 uint64_t cur_offset = 0;
212 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
214 @@ -655,6 +815,32 @@ int add_mtd_partitions(struct mtd_info *
216 EXPORT_SYMBOL(add_mtd_partitions);
218 +int refresh_mtd_partitions(struct mtd_info *mtd)
222 + if (IS_PART(mtd)) {
223 + struct mtd_part *part;
224 + struct mtd_info *master;
227 + master = part->master;
228 + if (master->refresh_device)
229 + ret = master->refresh_device(master);
232 + if (!ret && mtd->refresh_device)
233 + ret = mtd->refresh_device(mtd);
235 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
236 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
237 + refresh_rootfs_split(mtd);
242 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
244 static DEFINE_SPINLOCK(part_parser_lock);
245 static LIST_HEAD(part_parsers);
247 --- a/drivers/mtd/devices/block2mtd.c
248 +++ b/drivers/mtd/devices/block2mtd.c
249 @@ -30,6 +30,8 @@ struct block2mtd_dev {
250 struct block_device *blkdev;
252 struct mutex write_mutex;
253 + rwlock_t bdev_mutex;
258 @@ -82,6 +84,12 @@ static int block2mtd_erase(struct mtd_in
259 size_t len = instr->len;
262 + read_lock(&dev->bdev_mutex);
263 + if (!dev->blkdev) {
268 instr->state = MTD_ERASING;
269 mutex_lock(&dev->write_mutex);
270 err = _block2mtd_erase(dev, from, len);
271 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
272 instr->state = MTD_ERASE_DONE;
274 mtd_erase_callback(instr);
277 + read_unlock(&dev->bdev_mutex);
282 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
284 int index = from >> PAGE_SHIFT;
285 int offset = from & (PAGE_SIZE-1);
287 + int cpylen, err = 0;
289 + read_lock(&dev->bdev_mutex);
290 + if (!dev->blkdev || (from > mtd->size)) {
295 - if (from > mtd->size)
297 if (from + len > mtd->size)
298 len = mtd->size - from;
300 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
303 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
307 - return PTR_ERR(page);
312 + if (IS_ERR(page)) {
313 + err = PTR_ERR(page);
317 memcpy(buf, page_address(page) + offset, cpylen);
318 page_cache_release(page);
319 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
326 + read_unlock(&dev->bdev_mutex);
331 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
332 size_t *retlen, const u_char *buf)
334 struct block2mtd_dev *dev = mtd->priv;
338 + read_lock(&dev->bdev_mutex);
339 + if (!dev->blkdev) {
346 - if (to >= mtd->size)
350 + if (to >= mtd->size) {
355 if (to + len > mtd->size)
356 len = mtd->size - to;
358 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
359 mutex_unlock(&dev->write_mutex);
364 + read_unlock(&dev->bdev_mutex);
368 @@ -210,52 +246,29 @@ static int block2mtd_write(struct mtd_in
369 static void block2mtd_sync(struct mtd_info *mtd)
371 struct block2mtd_dev *dev = mtd->priv;
372 - sync_blockdev(dev->blkdev);
377 -static void block2mtd_free_device(struct block2mtd_dev *dev)
382 - kfree(dev->mtd.name);
385 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
387 - close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
389 + read_lock(&dev->bdev_mutex);
391 + sync_blockdev(dev->blkdev);
392 + read_unlock(&dev->bdev_mutex);
399 -/* FIXME: ensure that mtd->size % erase_size == 0 */
400 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
401 +static int _open_bdev(struct block2mtd_dev *dev)
403 struct block_device *bdev;
404 - struct block2mtd_dev *dev;
405 - struct mtd_partition *part;
411 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
415 /* Get a handle on the device */
416 - bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
417 + bdev = open_bdev_exclusive(dev->devname, FMODE_READ|FMODE_WRITE, NULL);
421 /* We might not have rootfs mounted at this point. Try
422 to resolve the device name by other means. */
424 - dev_t devt = name_to_dev_t(devname);
425 + dev_t devt = name_to_dev_t(dev->devname);
427 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
429 @@ -263,17 +276,98 @@ static struct block2mtd_dev *add_device(
433 - ERROR("error: cannot open device %s", devname);
435 + ERROR("error: cannot open device %s", dev->devname);
440 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
441 ERROR("attempting to use an MTD device as a block device");
449 +static void _close_bdev(struct block2mtd_dev *dev)
451 + struct block_device *bdev;
456 + bdev = dev->blkdev;
457 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
458 + close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
459 + dev->blkdev = NULL;
462 +static void block2mtd_free_device(struct block2mtd_dev *dev)
467 + kfree(dev->mtd.name);
473 +static int block2mtd_refresh(struct mtd_info *mtd)
475 + struct block2mtd_dev *dev = mtd->priv;
476 + struct block_device *bdev;
480 + /* no other mtd function can run at this point */
481 + write_lock(&dev->bdev_mutex);
483 + /* get the device number for the whole disk */
484 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
486 + /* close the old block device */
489 + /* open the whole disk, issue a partition rescan, then */
490 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
491 + if (!bdev || !bdev->bd_disk)
493 +#ifndef CONFIG_MTD_BLOCK2MTD_MODULE
495 + err = rescan_partitions(bdev->bd_disk, bdev);
498 + close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
500 + /* try to open the partition block device again */
502 + write_unlock(&dev->bdev_mutex);
507 +/* FIXME: ensure that mtd->size % erase_size == 0 */
508 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
510 + struct block2mtd_dev *dev;
511 + struct mtd_partition *part;
517 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
521 + strcpy(dev->devname, devname);
523 + if (_open_bdev(dev))
526 mutex_init(&dev->write_mutex);
527 + rwlock_init(&dev->bdev_mutex);
529 /* Setup the MTD structure */
530 /* make the name contain the block device in */
531 @@ -298,6 +392,7 @@ static struct block2mtd_dev *add_device(
532 dev->mtd.read = block2mtd_read;
534 dev->mtd.owner = THIS_MODULE;
535 + dev->mtd.refresh_device = block2mtd_refresh;
537 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
538 part->name = dev->mtd.name;
539 --- a/drivers/mtd/mtdchar.c
540 +++ b/drivers/mtd/mtdchar.c
541 @@ -841,6 +841,13 @@ static int mtd_ioctl(struct file *file,
545 +#ifdef CONFIG_MTD_PARTITIONS
548 + ret = refresh_mtd_partitions(mtd);
553 case OTPGETREGIONCOUNT:
554 case OTPGETREGIONINFO:
555 --- a/include/linux/mtd/mtd.h
556 +++ b/include/linux/mtd/mtd.h
557 @@ -125,6 +125,7 @@ struct nand_ecclayout {
558 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
565 @@ -266,6 +267,9 @@ struct mtd_info {
569 + int (*refresh_device)(struct mtd_info *mtd);
570 + struct mtd_info *split;
572 /* If the driver is something smart, like UBI, it may need to maintain
573 * its own reference counting. The below functions are only for driver.
574 * The driver may register its callbacks. These callbacks are not
575 --- a/include/linux/mtd/partitions.h
576 +++ b/include/linux/mtd/partitions.h
578 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
581 +struct mtd_partition;
582 struct mtd_partition {
583 char *name; /* identifier string */
584 uint64_t size; /* partition size */
585 uint64_t offset; /* offset within the master MTD space */
586 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
587 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
588 + int (*refresh_partition)(struct mtd_info *);
591 #define MTDPART_OFS_NXTBLK (-2)
592 @@ -51,6 +53,7 @@ struct mtd_info;
594 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
595 int del_mtd_partitions(struct mtd_info *);
596 +int refresh_mtd_partitions(struct mtd_info *);
599 * Functions dealing with the various ways of partitioning the space
600 --- a/include/mtd/mtd-abi.h
601 +++ b/include/mtd/mtd-abi.h
602 @@ -127,6 +127,7 @@ struct otp_info {
603 #define MEMWRITEOOB64 _IOWR('M', 21, struct mtd_oob_buf64)
604 #define MEMREADOOB64 _IOWR('M', 22, struct mtd_oob_buf64)
605 #define MEMISLOCKED _IOR('M', 23, struct erase_info_user)
606 +#define MTDREFRESH _IO('M', 23)
609 * Obsolete legacy interface. Keep it in order not to break userspace