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,153 @@ 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 ret, split_offset = 0;
97 + ret = split_squashfs(master, part->offset, &split_offset);
101 + if (split_offset <= 0)
104 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
105 + if (dpart == NULL) {
106 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
107 + ROOTFS_SPLIT_NAME);
111 + memcpy(dpart, part, sizeof(*part));
112 + dpart->name = (unsigned char *)&dpart[1];
113 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
115 + dpart->size -= split_offset - dpart->offset;
116 + dpart->offset = split_offset;
121 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
122 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
124 + slave = allocate_partition(master, dpart, 0, split_offset);
126 + return PTR_ERR(slave);
127 + mutex_lock(&mtd_partitions_mutex);
128 + list_add(&slave->list, &mtd_partitions);
129 + mutex_unlock(&mtd_partitions_mutex);
131 + add_mtd_device(&slave->mtd);
133 + rpart->split = &slave->mtd;
138 +static int refresh_rootfs_split(struct mtd_info *mtd)
140 + struct mtd_partition tpart;
141 + struct mtd_part *part;
149 + /* check for the new squashfs offset first */
150 + ret = split_squashfs(part->master, part->offset, &offset);
154 + if ((offset > 0) && !mtd->split) {
155 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
156 + /* if we don't have a rootfs split partition, create a new one */
157 + tpart.name = (char *) mtd->name;
158 + tpart.size = mtd->size;
159 + tpart.offset = part->offset;
161 + return split_rootfs_data(part->master, &part->mtd, &tpart);
162 + } else if ((offset > 0) && mtd->split) {
163 + /* update the offsets of the existing partition */
164 + size = mtd->size + part->offset - offset;
166 + part = PART(mtd->split);
167 + part->offset = offset;
168 + part->mtd.size = size;
169 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
170 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
171 + (u32) part->offset, (u32) part->mtd.size);
172 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
173 + strcpy(name, ROOTFS_SPLIT_NAME);
174 + part->mtd.name = name;
175 + } else if ((offset <= 0) && mtd->split) {
176 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
178 + /* mark existing partition as removed */
179 + part = PART(mtd->split);
180 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
181 + strcpy(name, ROOTFS_REMOVED_NAME);
182 + part->mtd.name = name;
184 + part->mtd.size = 0;
189 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
192 * This function, given a master MTD object and a partition table, creates
193 * and registers slave MTD objects which are bound to the master according to
194 @@ -633,7 +782,7 @@ int add_mtd_partitions(struct mtd_info *
196 struct mtd_part *slave;
197 uint64_t cur_offset = 0;
201 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
203 @@ -648,6 +797,21 @@ int add_mtd_partitions(struct mtd_info *
205 add_mtd_device(&slave->mtd);
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]);
222 cur_offset = slave->offset + slave->mtd.size;
225 @@ -655,6 +819,32 @@ int add_mtd_partitions(struct mtd_info *
227 EXPORT_SYMBOL(add_mtd_partitions);
229 +int refresh_mtd_partitions(struct mtd_info *mtd)
233 + if (IS_PART(mtd)) {
234 + struct mtd_part *part;
235 + struct mtd_info *master;
238 + master = part->master;
239 + if (master->refresh_device)
240 + ret = master->refresh_device(master);
243 + if (!ret && mtd->refresh_device)
244 + ret = mtd->refresh_device(mtd);
246 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
247 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
248 + refresh_rootfs_split(mtd);
253 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
255 static DEFINE_SPINLOCK(part_parser_lock);
256 static LIST_HEAD(part_parsers);
258 --- a/drivers/mtd/devices/block2mtd.c
259 +++ b/drivers/mtd/devices/block2mtd.c
260 @@ -30,6 +30,8 @@ struct block2mtd_dev {
261 struct block_device *blkdev;
263 struct mutex write_mutex;
264 + rwlock_t bdev_mutex;
269 @@ -82,6 +84,12 @@ static int block2mtd_erase(struct mtd_in
270 size_t len = instr->len;
273 + read_lock(&dev->bdev_mutex);
274 + if (!dev->blkdev) {
279 instr->state = MTD_ERASING;
280 mutex_lock(&dev->write_mutex);
281 err = _block2mtd_erase(dev, from, len);
282 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
283 instr->state = MTD_ERASE_DONE;
285 mtd_erase_callback(instr);
288 + read_unlock(&dev->bdev_mutex);
293 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
295 int index = from >> PAGE_SHIFT;
296 int offset = from & (PAGE_SIZE-1);
298 + int cpylen, err = 0;
300 + read_lock(&dev->bdev_mutex);
301 + if (!dev->blkdev || (from > mtd->size)) {
306 - if (from > mtd->size)
308 if (from + len > mtd->size)
309 len = mtd->size - from;
311 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
314 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
318 - return PTR_ERR(page);
323 + if (IS_ERR(page)) {
324 + err = PTR_ERR(page);
328 memcpy(buf, page_address(page) + offset, cpylen);
329 page_cache_release(page);
330 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
337 + read_unlock(&dev->bdev_mutex);
342 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
343 size_t *retlen, const u_char *buf)
345 struct block2mtd_dev *dev = mtd->priv;
349 + read_lock(&dev->bdev_mutex);
350 + if (!dev->blkdev) {
357 - if (to >= mtd->size)
361 + if (to >= mtd->size) {
366 if (to + len > mtd->size)
367 len = mtd->size - to;
369 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
370 mutex_unlock(&dev->write_mutex);
375 + read_unlock(&dev->bdev_mutex);
379 @@ -210,52 +246,29 @@ static int block2mtd_write(struct mtd_in
380 static void block2mtd_sync(struct mtd_info *mtd)
382 struct block2mtd_dev *dev = mtd->priv;
383 - sync_blockdev(dev->blkdev);
388 -static void block2mtd_free_device(struct block2mtd_dev *dev)
393 - kfree(dev->mtd.name);
396 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
398 - close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
400 + read_lock(&dev->bdev_mutex);
402 + sync_blockdev(dev->blkdev);
403 + read_unlock(&dev->bdev_mutex);
410 -/* FIXME: ensure that mtd->size % erase_size == 0 */
411 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
412 +static int _open_bdev(struct block2mtd_dev *dev)
414 struct block_device *bdev;
415 - struct block2mtd_dev *dev;
416 - struct mtd_partition *part;
422 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
426 /* Get a handle on the device */
427 - bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
428 + bdev = open_bdev_exclusive(dev->devname, FMODE_READ|FMODE_WRITE, NULL);
432 /* We might not have rootfs mounted at this point. Try
433 to resolve the device name by other means. */
435 - dev_t devt = name_to_dev_t(devname);
436 + dev_t devt = name_to_dev_t(dev->devname);
438 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
440 @@ -263,17 +276,98 @@ static struct block2mtd_dev *add_device(
444 - ERROR("error: cannot open device %s", devname);
446 + ERROR("error: cannot open device %s", dev->devname);
451 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
452 ERROR("attempting to use an MTD device as a block device");
460 +static void _close_bdev(struct block2mtd_dev *dev)
462 + struct block_device *bdev;
467 + bdev = dev->blkdev;
468 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
469 + close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
470 + dev->blkdev = NULL;
473 +static void block2mtd_free_device(struct block2mtd_dev *dev)
478 + kfree(dev->mtd.name);
484 +static int block2mtd_refresh(struct mtd_info *mtd)
486 + struct block2mtd_dev *dev = mtd->priv;
487 + struct block_device *bdev;
491 + /* no other mtd function can run at this point */
492 + write_lock(&dev->bdev_mutex);
494 + /* get the device number for the whole disk */
495 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
497 + /* close the old block device */
500 + /* open the whole disk, issue a partition rescan, then */
501 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
502 + if (!bdev || !bdev->bd_disk)
504 +#ifndef CONFIG_MTD_BLOCK2MTD_MODULE
506 + err = rescan_partitions(bdev->bd_disk, bdev);
509 + close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
511 + /* try to open the partition block device again */
513 + write_unlock(&dev->bdev_mutex);
518 +/* FIXME: ensure that mtd->size % erase_size == 0 */
519 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
521 + struct block2mtd_dev *dev;
522 + struct mtd_partition *part;
528 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
532 + strcpy(dev->devname, devname);
534 + if (_open_bdev(dev))
537 mutex_init(&dev->write_mutex);
538 + rwlock_init(&dev->bdev_mutex);
540 /* Setup the MTD structure */
541 /* make the name contain the block device in */
542 @@ -298,6 +392,7 @@ static struct block2mtd_dev *add_device(
543 dev->mtd.read = block2mtd_read;
545 dev->mtd.owner = THIS_MODULE;
546 + dev->mtd.refresh_device = block2mtd_refresh;
548 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
549 part->name = dev->mtd.name;
550 --- a/drivers/mtd/mtdchar.c
551 +++ b/drivers/mtd/mtdchar.c
552 @@ -841,6 +841,13 @@ static int mtd_ioctl(struct file *file,
556 +#ifdef CONFIG_MTD_PARTITIONS
559 + ret = refresh_mtd_partitions(mtd);
564 case OTPGETREGIONCOUNT:
565 case OTPGETREGIONINFO:
566 --- a/include/linux/mtd/mtd.h
567 +++ b/include/linux/mtd/mtd.h
568 @@ -125,6 +125,7 @@ struct nand_ecclayout {
569 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
576 @@ -266,6 +267,9 @@ struct mtd_info {
580 + int (*refresh_device)(struct mtd_info *mtd);
581 + struct mtd_info *split;
583 /* If the driver is something smart, like UBI, it may need to maintain
584 * its own reference counting. The below functions are only for driver.
585 * The driver may register its callbacks. These callbacks are not
586 --- a/include/linux/mtd/partitions.h
587 +++ b/include/linux/mtd/partitions.h
589 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
592 +struct mtd_partition;
593 struct mtd_partition {
594 char *name; /* identifier string */
595 uint64_t size; /* partition size */
596 uint64_t offset; /* offset within the master MTD space */
597 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
598 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only) */
599 + int (*refresh_partition)(struct mtd_info *);
602 #define MTDPART_OFS_NXTBLK (-2)
603 @@ -51,6 +53,7 @@ struct mtd_info;
605 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
606 int del_mtd_partitions(struct mtd_info *);
607 +int refresh_mtd_partitions(struct mtd_info *);
610 * Functions dealing with the various ways of partitioning the space
611 --- a/include/mtd/mtd-abi.h
612 +++ b/include/mtd/mtd-abi.h
613 @@ -127,6 +127,7 @@ struct otp_info {
614 #define MEMWRITEOOB64 _IOWR('M', 21, struct mtd_oob_buf64)
615 #define MEMREADOOB64 _IOWR('M', 22, struct mtd_oob_buf64)
616 #define MEMISLOCKED _IOR('M', 23, struct erase_info_user)
617 +#define MTDREFRESH _IO('M', 23)
620 * Obsolete legacy interface. Keep it in order not to break userspace