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/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 @@ -503,6 +505,150 @@ 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 + 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 + rpart->split = &slave->mtd;
135 +static int refresh_rootfs_split(struct mtd_info *mtd)
137 + struct mtd_partition tpart;
138 + struct mtd_part *part;
146 + /* check for the new squashfs offset first */
147 + ret = split_squashfs(part->master, part->offset, &offset);
151 + if ((offset > 0) && !mtd->split) {
152 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
153 + /* if we don't have a rootfs split partition, create a new one */
154 + tpart.name = (char *) mtd->name;
155 + tpart.size = mtd->size;
156 + tpart.offset = part->offset;
158 + return split_rootfs_data(part->master, &part->mtd, &tpart);
159 + } else if ((offset > 0) && mtd->split) {
160 + /* update the offsets of the existing partition */
161 + size = mtd->size + part->offset - offset;
163 + part = PART(mtd->split);
164 + part->offset = offset;
165 + part->mtd.size = size;
166 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
167 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
168 + (u32) part->offset, (u32) part->mtd.size);
169 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
170 + strcpy(name, ROOTFS_SPLIT_NAME);
171 + part->mtd.name = name;
172 + } else if ((offset <= 0) && mtd->split) {
173 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
175 + /* mark existing partition as removed */
176 + part = PART(mtd->split);
177 + name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
178 + strcpy(name, ROOTFS_REMOVED_NAME);
179 + part->mtd.name = name;
181 + part->mtd.size = 0;
186 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
189 * This function, given a master MTD object and a partition table, creates
190 * and registers slave MTD objects which are bound to the master according to
191 @@ -518,7 +664,7 @@ int add_mtd_partitions(struct mtd_info *
193 struct mtd_part *slave;
194 uint64_t cur_offset = 0;
198 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
200 @@ -526,6 +672,21 @@ int add_mtd_partitions(struct mtd_info *
201 slave = add_one_partition(master, parts + i, i, cur_offset);
205 + if (!strcmp(parts[i].name, "rootfs")) {
206 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
207 + if (ROOT_DEV == 0) {
208 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
209 + "set to be root filesystem\n");
210 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
213 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
214 + ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
219 cur_offset = slave->offset + slave->mtd.size;
222 @@ -533,6 +694,32 @@ int add_mtd_partitions(struct mtd_info *
224 EXPORT_SYMBOL(add_mtd_partitions);
226 +int refresh_mtd_partitions(struct mtd_info *mtd)
230 + if (IS_PART(mtd)) {
231 + struct mtd_part *part;
232 + struct mtd_info *master;
235 + master = part->master;
236 + if (master->refresh_device)
237 + ret = master->refresh_device(master);
240 + if (!ret && mtd->refresh_device)
241 + ret = mtd->refresh_device(mtd);
243 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
244 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
245 + refresh_rootfs_split(mtd);
250 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
252 static DEFINE_SPINLOCK(part_parser_lock);
253 static LIST_HEAD(part_parsers);
255 --- a/drivers/mtd/devices/block2mtd.c
256 +++ b/drivers/mtd/devices/block2mtd.c
257 @@ -29,6 +29,8 @@ struct block2mtd_dev {
258 struct block_device *blkdev;
260 struct mutex write_mutex;
261 + rwlock_t bdev_mutex;
266 @@ -81,6 +83,12 @@ static int block2mtd_erase(struct mtd_in
267 size_t len = instr->len;
270 + read_lock(&dev->bdev_mutex);
271 + if (!dev->blkdev) {
276 instr->state = MTD_ERASING;
277 mutex_lock(&dev->write_mutex);
278 err = _block2mtd_erase(dev, from, len);
279 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
281 instr->state = MTD_ERASE_DONE;
282 mtd_erase_callback(instr);
285 + read_unlock(&dev->bdev_mutex);
290 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
292 int index = from >> PAGE_SHIFT;
293 int offset = from & (PAGE_SIZE-1);
295 + int cpylen, err = 0;
297 + read_lock(&dev->bdev_mutex);
298 + if (!dev->blkdev || (from > mtd->size)) {
303 - if (from > mtd->size)
305 if (from + len > mtd->size)
306 len = mtd->size - from;
308 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
311 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
315 - return PTR_ERR(page);
320 + if (IS_ERR(page)) {
321 + err = PTR_ERR(page);
325 memcpy(buf, page_address(page) + offset, cpylen);
326 page_cache_release(page);
327 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
334 + read_unlock(&dev->bdev_mutex);
339 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
340 size_t *retlen, const u_char *buf)
342 struct block2mtd_dev *dev = mtd->priv;
346 + read_lock(&dev->bdev_mutex);
347 + if (!dev->blkdev) {
354 - if (to >= mtd->size)
358 + if (to >= mtd->size) {
363 if (to + len > mtd->size)
364 len = mtd->size - to;
366 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
367 mutex_unlock(&dev->write_mutex);
372 + read_unlock(&dev->bdev_mutex);
376 @@ -210,52 +246,29 @@ static int block2mtd_write(struct mtd_in
377 static void block2mtd_sync(struct mtd_info *mtd)
379 struct block2mtd_dev *dev = mtd->priv;
380 - sync_blockdev(dev->blkdev);
385 -static void block2mtd_free_device(struct block2mtd_dev *dev)
390 - kfree(dev->mtd.name);
393 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
395 - close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
397 + read_lock(&dev->bdev_mutex);
399 + sync_blockdev(dev->blkdev);
400 + read_unlock(&dev->bdev_mutex);
407 -/* FIXME: ensure that mtd->size % erase_size == 0 */
408 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
409 +static int _open_bdev(struct block2mtd_dev *dev)
411 struct block_device *bdev;
412 - struct block2mtd_dev *dev;
413 - struct mtd_partition *part;
419 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
423 /* Get a handle on the device */
424 - bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL);
425 + bdev = open_bdev_exclusive(dev->devname, FMODE_READ|FMODE_WRITE, NULL);
429 /* We might not have rootfs mounted at this point. Try
430 to resolve the device name by other means. */
432 - dev_t devt = name_to_dev_t(devname);
433 + dev_t devt = name_to_dev_t(dev->devname);
435 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
437 @@ -263,17 +276,97 @@ static struct block2mtd_dev *add_device(
441 - ERROR("error: cannot open device %s", devname);
443 + ERROR("error: cannot open device %s", dev->devname);
448 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
449 ERROR("attempting to use an MTD device as a block device");
457 +static void _close_bdev(struct block2mtd_dev *dev)
459 + struct block_device *bdev;
464 + bdev = dev->blkdev;
465 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
466 + close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE);
467 + dev->blkdev = NULL;
470 +static void block2mtd_free_device(struct block2mtd_dev *dev)
475 + kfree(dev->mtd.name);
481 +static int block2mtd_refresh(struct mtd_info *mtd)
483 + struct block2mtd_dev *dev = mtd->priv;
484 + struct block_device *bdev;
488 + /* no other mtd function can run at this point */
489 + write_lock(&dev->bdev_mutex);
491 + /* get the device number for the whole disk */
492 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
494 + /* close the old block device */
497 + /* open the whole disk, issue a partition rescan, then */
498 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
499 + if (!bdev || !bdev->bd_disk)
502 + err = rescan_partitions(bdev->bd_disk, bdev);
505 + close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE);
507 + /* try to open the partition block device again */
509 + write_unlock(&dev->bdev_mutex);
514 +/* FIXME: ensure that mtd->size % erase_size == 0 */
515 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
517 + struct block2mtd_dev *dev;
518 + struct mtd_partition *part;
524 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
528 + strcpy(dev->devname, devname);
530 + if (_open_bdev(dev))
533 mutex_init(&dev->write_mutex);
534 + rwlock_init(&dev->bdev_mutex);
538 @@ -297,6 +390,7 @@ static struct block2mtd_dev *add_device(
539 dev->mtd.read = block2mtd_read;
541 dev->mtd.owner = THIS_MODULE;
542 + dev->mtd.refresh_device = block2mtd_refresh;
544 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
545 part->name = dev->mtd.name;
546 --- a/drivers/mtd/mtdchar.c
547 +++ b/drivers/mtd/mtdchar.c
550 #include <linux/mtd/mtd.h>
551 #include <linux/mtd/compatmac.h>
552 +#include <linux/mtd/partitions.h>
554 #include <asm/uaccess.h>
556 @@ -814,6 +815,13 @@ static int mtd_ioctl(struct inode *inode
560 +#ifdef CONFIG_MTD_PARTITIONS
563 + ret = refresh_mtd_partitions(mtd);
570 --- a/include/linux/mtd/mtd.h
571 +++ b/include/linux/mtd/mtd.h
572 @@ -101,6 +101,7 @@ struct mtd_oob_ops {
580 @@ -241,6 +242,9 @@ struct mtd_info {
584 + int (*refresh_device)(struct mtd_info *mtd);
585 + struct mtd_info *split;
587 /* If the driver is something smart, like UBI, it may need to maintain
588 * its own reference counting. The below functions are only for driver.
589 * The driver may register its callbacks. These callbacks are not
590 --- a/include/linux/mtd/partitions.h
591 +++ b/include/linux/mtd/partitions.h
593 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
596 +struct mtd_partition;
597 struct mtd_partition {
598 char *name; /* identifier string */
599 uint64_t size; /* partition size */
600 uint64_t offset; /* offset within the master MTD space */
601 uint32_t mask_flags; /* master MTD flags to mask out for this partition */
602 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only)*/
603 + int (*refresh_partition)(struct mtd_info *);
606 #define MTDPART_OFS_NXTBLK (-2)
607 @@ -51,6 +53,7 @@ struct mtd_info;
609 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
610 int del_mtd_partitions(struct mtd_info *);
611 +int refresh_mtd_partitions(struct mtd_info *);
614 * Functions dealing with the various ways of partitioning the space
615 --- a/include/mtd/mtd-abi.h
616 +++ b/include/mtd/mtd-abi.h
617 @@ -110,6 +110,7 @@ struct otp_info {
618 #define MEMERASE64 _IOW('M', 20, struct erase_info_user64)
619 #define MEMWRITEOOB64 _IOWR('M', 21, struct mtd_oob_buf64)
620 #define MEMREADOOB64 _IOWR('M', 22, struct mtd_oob_buf64)
621 +#define MTDREFRESH _IO('M', 23)
624 * Obsolete legacy interface. Keep it in order not to break userspace