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 @@ -37,7 +39,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 @@ -512,6 +514,156 @@ 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)
58 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
61 + ret = master->read(master, offset, sizeof(*sb), &len, buf);
62 + if (ret || (len != sizeof(*sb))) {
63 + printk(KERN_ALERT "split_squashfs: error occured while reading "
64 + "from \"%s\"\n", master->name);
68 + if (*((u32 *) buf) != SQUASHFS_MAGIC) {
69 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
75 + if (sb->bytes_used <= 0) {
76 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
82 + len = (u32) sb->bytes_used;
83 + len += (offset & 0x000fffff);
84 + len += (master->erasesize - 1);
85 + len &= ~(master->erasesize - 1);
86 + len -= (offset & 0x000fffff);
87 + *split_offset = offset + len;
92 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part,
95 + struct mtd_partition *dpart;
96 + struct mtd_part *slave = NULL;
97 + int split_offset = 0;
100 + ret = split_squashfs(master, part->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 -= split_offset - dpart->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, index, 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 + /* find the index of the last partition */
161 + if (!list_empty(&mtd_partitions))
162 + index = list_first_entry(&mtd_partitions, struct mtd_part, list)->index + 1;
164 + return split_rootfs_data(part->master, &part->mtd, &tpart, index);
165 + } else if ((offset > 0) && mtd->split) {
166 + /* update the offsets of the existing partition */
167 + size = mtd->size + part->offset - offset;
169 + part = PART(mtd->split);
170 + part->offset = offset;
171 + part->mtd.size = size;
172 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
173 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
174 + (u32) part->offset, (u32) part->mtd.size);
175 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
176 + strcpy(name, ROOTFS_SPLIT_NAME);
177 + part->mtd.name = name;
178 + } else if ((offset <= 0) && mtd->split) {
179 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
181 + /* mark existing partition as removed */
182 + part = PART(mtd->split);
183 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
184 + strcpy(name, ROOTFS_REMOVED_NAME);
185 + part->mtd.name = name;
187 + part->mtd.size = 0;
192 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
195 * This function, given a master MTD object and a partition table, creates
196 * and registers slave MTD objects which are bound to the master according to
197 @@ -527,14 +679,29 @@ int add_mtd_partitions(struct mtd_info *
199 struct mtd_part *slave;
200 uint64_t cur_offset = 0;
204 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
206 - for (i = 0; i < nbparts; i++) {
207 - slave = add_one_partition(master, parts + i, i, cur_offset);
208 + for (i = 0, j = 0; i < nbparts; i++) {
209 + slave = add_one_partition(master, parts + i, j++, cur_offset);
213 + if (!strcmp(parts[i].name, "rootfs") && slave->registered) {
214 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
215 + if (ROOT_DEV == 0) {
216 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
217 + "set to be root filesystem\n");
218 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
221 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
222 + ret = split_rootfs_data(master, &slave->mtd, &parts[i], j);
227 cur_offset = slave->offset + slave->mtd.size;
230 @@ -542,6 +709,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 --- a/drivers/mtd/devices/block2mtd.c
264 +++ b/drivers/mtd/devices/block2mtd.c
265 @@ -29,6 +29,8 @@ struct block2mtd_dev {
266 struct block_device *blkdev;
268 struct mutex write_mutex;
269 + rwlock_t bdev_mutex;
274 @@ -81,6 +83,12 @@ static int block2mtd_erase(struct mtd_in
275 size_t len = instr->len;
278 + read_lock(&dev->bdev_mutex);
279 + if (!dev->blkdev) {
284 instr->state = MTD_ERASING;
285 mutex_lock(&dev->write_mutex);
286 err = _block2mtd_erase(dev, from, len);
287 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
289 instr->state = MTD_ERASE_DONE;
290 mtd_erase_callback(instr);
293 + read_unlock(&dev->bdev_mutex);
298 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
300 int index = from >> PAGE_SHIFT;
301 int offset = from & (PAGE_SIZE-1);
303 + int cpylen, err = 0;
305 + read_lock(&dev->bdev_mutex);
306 + if (!dev->blkdev || (from > mtd->size)) {
311 - if (from > mtd->size)
313 if (from + len > mtd->size)
314 len = mtd->size - from;
316 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
319 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
323 - return PTR_ERR(page);
328 + if (IS_ERR(page)) {
329 + err = PTR_ERR(page);
333 memcpy(buf, page_address(page) + offset, cpylen);
334 page_cache_release(page);
335 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
342 + read_unlock(&dev->bdev_mutex);
347 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
348 size_t *retlen, const u_char *buf)
350 struct block2mtd_dev *dev = mtd->priv;
354 + read_lock(&dev->bdev_mutex);
355 + if (!dev->blkdev) {
362 - if (to >= mtd->size)
366 + if (to >= mtd->size) {
371 if (to + len > mtd->size)
372 len = mtd->size - to;
374 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
375 mutex_unlock(&dev->write_mutex);
380 + read_unlock(&dev->bdev_mutex);
384 @@ -210,52 +246,29 @@ static int block2mtd_write(struct mtd_in
385 static void block2mtd_sync(struct mtd_info *mtd)
387 struct block2mtd_dev *dev = mtd->priv;
388 - sync_blockdev(dev->blkdev);
393 -static void block2mtd_free_device(struct block2mtd_dev *dev)
398 - kfree(dev->mtd.name);
401 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
403 - close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
405 + read_lock(&dev->bdev_mutex);
407 + sync_blockdev(dev->blkdev);
408 + read_unlock(&dev->bdev_mutex);
415 -/* FIXME: ensure that mtd->size % erase_size == 0 */
416 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
417 +static int _open_bdev(struct block2mtd_dev *dev)
419 struct block_device *bdev;
420 - struct block2mtd_dev *dev;
421 - struct mtd_partition *part;
427 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
431 /* Get a handle on the device */
432 - bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
433 + bdev = open_bdev_exclusive(dev->devname, FMODE_READ|FMODE_WRITE, NULL);
437 /* We might not have rootfs mounted at this point. Try
438 to resolve the device name by other means. */
440 - dev_t devt = name_to_dev_t(devname);
441 + dev_t devt = name_to_dev_t(dev->devname);
443 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
445 @@ -263,17 +276,97 @@ static struct block2mtd_dev *add_device(
449 - ERROR("error: cannot open device %s", devname);
451 + ERROR("error: cannot open device %s", dev->devname);
456 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
457 ERROR("attempting to use an MTD device as a block device");
465 +static void _close_bdev(struct block2mtd_dev *dev)
467 + struct block_device *bdev;
472 + bdev = dev->blkdev;
473 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
474 + close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
475 + dev->blkdev = NULL;
478 +static void block2mtd_free_device(struct block2mtd_dev *dev)
483 + kfree(dev->mtd.name);
489 +static int block2mtd_refresh(struct mtd_info *mtd)
491 + struct block2mtd_dev *dev = mtd->priv;
492 + struct block_device *bdev;
496 + /* no other mtd function can run at this point */
497 + write_lock(&dev->bdev_mutex);
499 + /* get the device number for the whole disk */
500 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
502 + /* close the old block device */
505 + /* open the whole disk, issue a partition rescan, then */
506 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
507 + if (!bdev || !bdev->bd_disk)
510 + err = rescan_partitions(bdev->bd_disk, bdev);
513 + close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
515 + /* try to open the partition block device again */
517 + write_unlock(&dev->bdev_mutex);
522 +/* FIXME: ensure that mtd->size % erase_size == 0 */
523 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
525 + struct block2mtd_dev *dev;
526 + struct mtd_partition *part;
532 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
536 + strcpy(dev->devname, devname);
538 + if (_open_bdev(dev))
541 mutex_init(&dev->write_mutex);
542 + rwlock_init(&dev->bdev_mutex);
546 @@ -297,6 +390,7 @@ static struct block2mtd_dev *add_device(
547 dev->mtd.read = block2mtd_read;
549 dev->mtd.owner = THIS_MODULE;
550 + dev->mtd.refresh_device = block2mtd_refresh;
552 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
553 part->name = dev->mtd.name;
554 --- a/drivers/mtd/mtdchar.c
555 +++ b/drivers/mtd/mtdchar.c
558 #include <linux/mtd/mtd.h>
559 #include <linux/mtd/compatmac.h>
560 +#include <linux/mtd/partitions.h>
562 #include <asm/uaccess.h>
564 @@ -750,6 +751,13 @@ static int mtd_ioctl(struct inode *inode
568 +#ifdef CONFIG_MTD_PARTITIONS
571 + ret = refresh_mtd_partitions(mtd);
578 --- a/include/linux/mtd/mtd.h
579 +++ b/include/linux/mtd/mtd.h
580 @@ -101,6 +101,7 @@ struct mtd_oob_ops {
588 @@ -241,6 +242,9 @@ struct mtd_info {
592 + int (*refresh_device)(struct mtd_info *mtd);
593 + struct mtd_info *split;
595 /* If the driver is something smart, like UBI, it may need to maintain
596 * its own reference counting. The below functions are only for driver.
597 * The driver may register its callbacks. These callbacks are not
598 --- a/include/linux/mtd/partitions.h
599 +++ b/include/linux/mtd/partitions.h
601 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
604 +struct mtd_partition;
605 struct mtd_partition {
606 char *name; /* identifier string */
607 uint64_t size; /* partition size */
608 @@ -41,6 +42,7 @@ struct mtd_partition {
609 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
610 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only)*/
611 struct mtd_info **mtdp; /* pointer to store the MTD object */
612 + int (*refresh_partition)(struct mtd_info *);
615 #define MTDPART_OFS_NXTBLK (-2)
616 @@ -50,6 +52,7 @@ struct mtd_partition {
618 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
619 int del_mtd_partitions(struct mtd_info *);
620 +int refresh_mtd_partitions(struct mtd_info *);
623 * Functions dealing with the various ways of partitioning the space
624 --- a/include/mtd/mtd-abi.h
625 +++ b/include/mtd/mtd-abi.h
626 @@ -95,6 +95,7 @@ struct otp_info {
627 #define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout)
628 #define ECCGETSTATS _IOR('M', 18, struct mtd_ecc_stats)
629 #define MTDFILEMODE _IO('M', 19)
630 +#define MTDREFRESH _IO('M', 23)
633 * Obsolete legacy interface. Keep it in order not to break userspace