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,155 @@ 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,
94 + struct mtd_partition *dpart;
95 + struct mtd_part *slave = NULL;
96 + int split_offset = 0;
99 + ret = split_squashfs(master, part->offset, &split_offset);
103 + if (split_offset <= 0)
106 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
107 + if (dpart == NULL) {
108 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
109 + ROOTFS_SPLIT_NAME);
113 + memcpy(dpart, part, sizeof(*part));
114 + dpart->name = (unsigned char *)&dpart[1];
115 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
117 + dpart->size -= split_offset - dpart->offset;
118 + dpart->offset = split_offset;
123 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
124 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
126 + slave = add_one_partition(master, dpart, index, split_offset);
131 + rpart->split = &slave->mtd;
136 +static int refresh_rootfs_split(struct mtd_info *mtd)
138 + struct mtd_partition tpart;
139 + struct mtd_part *part;
147 + /* check for the new squashfs offset first */
148 + ret = split_squashfs(part->master, part->offset, &offset);
152 + if ((offset > 0) && !mtd->split) {
153 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
154 + /* if we don't have a rootfs split partition, create a new one */
155 + tpart.name = (char *) mtd->name;
156 + tpart.size = mtd->size;
157 + tpart.offset = part->offset;
159 + /* find the index of the last partition */
160 + if (!list_empty(&mtd_partitions))
161 + index = list_first_entry(&mtd_partitions, struct mtd_part, list)->index + 1;
163 + return split_rootfs_data(part->master, &part->mtd, &tpart, index);
164 + } else if ((offset > 0) && mtd->split) {
165 + /* update the offsets of the existing partition */
166 + size = mtd->size + part->offset - offset;
168 + part = PART(mtd->split);
169 + part->offset = offset;
170 + part->mtd.size = size;
171 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
172 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
173 + (u32) part->offset, (u32) part->mtd.size);
174 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
175 + strcpy(name, ROOTFS_SPLIT_NAME);
176 + part->mtd.name = name;
177 + } else if ((offset <= 0) && mtd->split) {
178 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
180 + /* mark existing partition as removed */
181 + part = PART(mtd->split);
182 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
183 + strcpy(name, ROOTFS_REMOVED_NAME);
184 + part->mtd.name = name;
186 + part->mtd.size = 0;
191 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
194 * This function, given a master MTD object and a partition table, creates
195 * and registers slave MTD objects which are bound to the master according to
196 @@ -527,14 +678,29 @@ int add_mtd_partitions(struct mtd_info *
198 struct mtd_part *slave;
199 uint64_t cur_offset = 0;
203 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
205 - for (i = 0; i < nbparts; i++) {
206 - slave = add_one_partition(master, parts + i, i, cur_offset);
207 + for (i = 0, j = 0; i < nbparts; i++) {
208 + slave = add_one_partition(master, parts + i, j++, cur_offset);
212 + if (!strcmp(parts[i].name, "rootfs") && slave->registered) {
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], j);
226 cur_offset = slave->offset + slave->mtd.size;
229 @@ -542,6 +708,32 @@ int add_mtd_partitions(struct mtd_info *
231 EXPORT_SYMBOL(add_mtd_partitions);
233 +int refresh_mtd_partitions(struct mtd_info *mtd)
237 + if (IS_PART(mtd)) {
238 + struct mtd_part *part;
239 + struct mtd_info *master;
242 + master = part->master;
243 + if (master->refresh_device)
244 + ret = master->refresh_device(master);
247 + if (!ret && mtd->refresh_device)
248 + ret = mtd->refresh_device(mtd);
250 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
251 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
252 + refresh_rootfs_split(mtd);
257 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
259 static DEFINE_SPINLOCK(part_parser_lock);
260 static LIST_HEAD(part_parsers);
262 --- a/drivers/mtd/devices/block2mtd.c
263 +++ b/drivers/mtd/devices/block2mtd.c
264 @@ -29,6 +29,8 @@ struct block2mtd_dev {
265 struct block_device *blkdev;
267 struct mutex write_mutex;
268 + rwlock_t bdev_mutex;
273 @@ -81,6 +83,12 @@ static int block2mtd_erase(struct mtd_in
274 size_t len = instr->len;
277 + read_lock(&dev->bdev_mutex);
278 + if (!dev->blkdev) {
283 instr->state = MTD_ERASING;
284 mutex_lock(&dev->write_mutex);
285 err = _block2mtd_erase(dev, from, len);
286 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
288 instr->state = MTD_ERASE_DONE;
289 mtd_erase_callback(instr);
292 + read_unlock(&dev->bdev_mutex);
297 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
299 int index = from >> PAGE_SHIFT;
300 int offset = from & (PAGE_SIZE-1);
302 + int cpylen, err = 0;
304 + read_lock(&dev->bdev_mutex);
305 + if (!dev->blkdev || (from > mtd->size)) {
310 - if (from > mtd->size)
312 if (from + len > mtd->size)
313 len = mtd->size - from;
315 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
318 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
322 - return PTR_ERR(page);
327 + if (IS_ERR(page)) {
328 + err = PTR_ERR(page);
332 memcpy(buf, page_address(page) + offset, cpylen);
333 page_cache_release(page);
334 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
341 + read_unlock(&dev->bdev_mutex);
346 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
347 size_t *retlen, const u_char *buf)
349 struct block2mtd_dev *dev = mtd->priv;
353 + read_lock(&dev->bdev_mutex);
354 + if (!dev->blkdev) {
361 - if (to >= mtd->size)
365 + if (to >= mtd->size) {
370 if (to + len > mtd->size)
371 len = mtd->size - to;
373 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
374 mutex_unlock(&dev->write_mutex);
379 + read_unlock(&dev->bdev_mutex);
383 @@ -210,52 +246,29 @@ static int block2mtd_write(struct mtd_in
384 static void block2mtd_sync(struct mtd_info *mtd)
386 struct block2mtd_dev *dev = mtd->priv;
387 - sync_blockdev(dev->blkdev);
392 -static void block2mtd_free_device(struct block2mtd_dev *dev)
397 - kfree(dev->mtd.name);
400 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
402 - close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
404 + read_lock(&dev->bdev_mutex);
406 + sync_blockdev(dev->blkdev);
407 + read_unlock(&dev->bdev_mutex);
414 -/* FIXME: ensure that mtd->size % erase_size == 0 */
415 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
416 +static int _open_bdev(struct block2mtd_dev *dev)
418 struct block_device *bdev;
419 - struct block2mtd_dev *dev;
420 - struct mtd_partition *part;
426 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
430 /* Get a handle on the device */
431 - bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
432 + bdev = open_bdev_exclusive(dev->devname, FMODE_READ|FMODE_WRITE, NULL);
436 /* We might not have rootfs mounted at this point. Try
437 to resolve the device name by other means. */
439 - dev_t devt = name_to_dev_t(devname);
440 + dev_t devt = name_to_dev_t(dev->devname);
442 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
444 @@ -263,17 +276,97 @@ static struct block2mtd_dev *add_device(
448 - ERROR("error: cannot open device %s", devname);
450 + ERROR("error: cannot open device %s", dev->devname);
455 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
456 ERROR("attempting to use an MTD device as a block device");
464 +static void _close_bdev(struct block2mtd_dev *dev)
466 + struct block_device *bdev;
471 + bdev = dev->blkdev;
472 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
473 + close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
474 + dev->blkdev = NULL;
477 +static void block2mtd_free_device(struct block2mtd_dev *dev)
482 + kfree(dev->mtd.name);
488 +static int block2mtd_refresh(struct mtd_info *mtd)
490 + struct block2mtd_dev *dev = mtd->priv;
491 + struct block_device *bdev;
495 + /* no other mtd function can run at this point */
496 + write_lock(&dev->bdev_mutex);
498 + /* get the device number for the whole disk */
499 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
501 + /* close the old block device */
504 + /* open the whole disk, issue a partition rescan, then */
505 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
506 + if (!bdev || !bdev->bd_disk)
509 + err = rescan_partitions(bdev->bd_disk, bdev);
512 + close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
514 + /* try to open the partition block device again */
516 + write_unlock(&dev->bdev_mutex);
521 +/* FIXME: ensure that mtd->size % erase_size == 0 */
522 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
524 + struct block2mtd_dev *dev;
525 + struct mtd_partition *part;
531 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
535 + strcpy(dev->devname, devname);
537 + if (_open_bdev(dev))
540 mutex_init(&dev->write_mutex);
541 + rwlock_init(&dev->bdev_mutex);
545 @@ -297,6 +390,7 @@ static struct block2mtd_dev *add_device(
546 dev->mtd.read = block2mtd_read;
548 dev->mtd.owner = THIS_MODULE;
549 + dev->mtd.refresh_device = block2mtd_refresh;
551 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
552 part->name = dev->mtd.name;
553 --- a/drivers/mtd/mtdchar.c
554 +++ b/drivers/mtd/mtdchar.c
557 #include <linux/mtd/mtd.h>
558 #include <linux/mtd/compatmac.h>
559 +#include <linux/mtd/partitions.h>
561 #include <asm/uaccess.h>
563 @@ -750,6 +751,13 @@ static int mtd_ioctl(struct inode *inode
567 +#ifdef CONFIG_MTD_PARTITIONS
570 + ret = refresh_mtd_partitions(mtd);
577 --- a/include/linux/mtd/mtd.h
578 +++ b/include/linux/mtd/mtd.h
579 @@ -101,6 +101,7 @@ struct mtd_oob_ops {
587 @@ -241,6 +242,9 @@ struct mtd_info {
591 + int (*refresh_device)(struct mtd_info *mtd);
592 + struct mtd_info *split;
594 /* If the driver is something smart, like UBI, it may need to maintain
595 * its own reference counting. The below functions are only for driver.
596 * The driver may register its callbacks. These callbacks are not
597 --- a/include/linux/mtd/partitions.h
598 +++ b/include/linux/mtd/partitions.h
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 @@ -41,6 +42,7 @@ struct mtd_partition {
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 struct mtd_info **mtdp; /* pointer to store the MTD object */
611 + int (*refresh_partition)(struct mtd_info *);
614 #define MTDPART_OFS_NXTBLK (-2)
615 @@ -50,6 +52,7 @@ struct mtd_partition {
617 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
618 int del_mtd_partitions(struct mtd_info *);
619 +int refresh_mtd_partitions(struct mtd_info *);
622 * Functions dealing with the various ways of partitioning the space
623 --- a/include/mtd/mtd-abi.h
624 +++ b/include/mtd/mtd-abi.h
625 @@ -95,6 +95,7 @@ struct otp_info {
626 #define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout)
627 #define ECCGETSTATS _IOR('M', 18, struct mtd_ecc_stats)
628 #define MTDFILEMODE _IO('M', 19)
629 +#define MTDREFRESH _IO('M', 23)
632 * Obsolete legacy interface. Keep it in order not to break userspace