6fb86b9b4131e9dc20caafa6bfa287e1d9dcfafc
[oweals/openwrt.git] / target / linux / generic-2.6 / patches-2.6.27 / 065-rootfs_split.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -45,6 +45,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.
6  
7 +config MTD_ROOTFS_ROOT_DEV
8 +       bool "Automatically set 'rootfs' partition to be root filesystem"
9 +       depends on MTD_PARTITIONS
10 +       default y
11 +
12 +config MTD_ROOTFS_SPLIT
13 +       bool "Automatically split 'rootfs' partition for squashfs"
14 +       depends on MTD_PARTITIONS
15 +       default y
16 +
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
22 @@ -18,6 +18,8 @@
23  #include <linux/mtd/mtd.h>
24  #include <linux/mtd/partitions.h>
25  #include <linux/mtd/compatmac.h>
26 +#include <linux/squashfs_fs.h>
27 +#include <linux/root_dev.h>
28  
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.
33   */
34  #define PART(x)  ((struct mtd_part *)(x))
35 -
36 +#define IS_PART(mtd) (mtd->read == part_read)
37  
38  /*
39   * MTD methods which simply translate the effective address and pass through
40 @@ -489,6 +491,148 @@ out_register:
41         return slave;
42  }
43  
44 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
45 +#define ROOTFS_SPLIT_NAME "rootfs_data"
46 +#define ROOTFS_REMOVED_NAME "<removed>"
47 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
48 +{
49 +       char buf[512];
50 +       struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
51 +       int len, ret;
52 +
53 +       ret = master->read(master, offset, sizeof(*sb), &len, buf);
54 +       if (ret || (len != sizeof(*sb))) {
55 +               printk(KERN_ALERT "split_squashfs: error occured while reading "
56 +                       "from \"%s\"\n", master->name);
57 +               return -EINVAL;
58 +       }
59 +
60 +       if (*((u32 *) buf) != SQUASHFS_MAGIC) {
61 +               printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
62 +                       master->name);
63 +               *split_offset = 0;
64 +               return 0;
65 +       }
66 +
67 +       if (sb->bytes_used <= 0) {
68 +               printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
69 +                       master->name);
70 +               *split_offset = 0;
71 +               return 0;
72 +       }
73 +
74 +       len = (u32) sb->bytes_used;
75 +       len += (offset & 0x000fffff);
76 +       len +=  (master->erasesize - 1);
77 +       len &= ~(master->erasesize - 1);
78 +       len -= (offset & 0x000fffff);
79 +       *split_offset = offset + len;
80 +
81 +       return 0;
82 +}
83 +
84 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part,
85 +               int index)
86 +{
87 +       struct mtd_partition *dpart;
88 +       struct mtd_part *slave = NULL;
89 +       int split_offset = 0;
90 +       int ret;
91 +
92 +       ret = split_squashfs(master, part->offset, &split_offset);
93 +       if (ret)
94 +               return ret;
95 +
96 +       if (split_offset <= 0)
97 +               return 0;
98 +
99 +       dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
100 +       if (dpart == NULL) {
101 +               printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
102 +                       ROOTFS_SPLIT_NAME);
103 +               return -ENOMEM;
104 +       }
105 +
106 +       memcpy(dpart, part, sizeof(*part));
107 +       dpart->name = (unsigned char *)&dpart[1];
108 +       strcpy(dpart->name, ROOTFS_SPLIT_NAME);
109 +
110 +       dpart->size -= split_offset - dpart->offset;
111 +       dpart->offset = split_offset;
112 +
113 +       if (dpart == NULL)
114 +               return 1;
115 +
116 +       printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%X, len=%X \n",
117 +               ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
118 +
119 +       slave = add_one_partition(master, dpart, index, split_offset);
120 +       if (!slave) {
121 +               kfree(dpart);
122 +               return -ENOMEM;
123 +       }
124 +       rpart->split = &slave->mtd;
125 +
126 +       return 0;
127 +}
128 +
129 +static int refresh_rootfs_split(struct mtd_info *mtd)
130 +{
131 +       struct mtd_partition tpart;
132 +       struct mtd_part *part;
133 +       char *name;
134 +       int index = 0;
135 +       int offset, size;
136 +       int ret;
137 +
138 +       part = PART(mtd);
139 +
140 +       /* check for the new squashfs offset first */
141 +       ret = split_squashfs(part->master, part->offset, &offset);
142 +       if (ret)
143 +               return ret;
144 +
145 +       if ((offset > 0) && !mtd->split) {
146 +               printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
147 +               /* if we don't have a rootfs split partition, create a new one */
148 +               tpart.name = (char *) mtd->name;
149 +               tpart.size = mtd->size;
150 +               tpart.offset = part->offset;
151 +
152 +               /* find the index of the last partition */
153 +               if (!list_empty(&mtd_partitions))
154 +                       index = list_first_entry(&mtd_partitions, struct mtd_part, list)->index + 1;
155 +
156 +               return split_rootfs_data(part->master, &part->mtd, &tpart, index);
157 +       } else if ((offset > 0) && mtd->split) {
158 +               /* update the offsets of the existing partition */
159 +               size = mtd->size + part->offset - offset;
160 +
161 +               part = PART(mtd->split);
162 +               part->offset = offset;
163 +               part->mtd.size = size;
164 +               printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
165 +                       __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
166 +                       part->offset, part->mtd.size);
167 +               name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
168 +               strcpy(name, ROOTFS_SPLIT_NAME);
169 +               part->mtd.name = name;
170 +       } else if ((offset <= 0) && mtd->split) {
171 +               printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
172 +
173 +               /* mark existing partition as removed */
174 +               part = PART(mtd->split);
175 +               name = kmalloc(sizeof(ROOTFS_SPLIT_NAME) + 1, GFP_KERNEL);
176 +               strcpy(name, ROOTFS_REMOVED_NAME);
177 +               part->mtd.name = name;
178 +               part->offset = 0;
179 +               part->mtd.size = 0;
180 +       }
181 +
182 +       return 0;
183 +}
184 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
185 +
186  /*
187   * This function, given a master MTD object and a partition table, creates
188   * and registers slave MTD objects which are bound to the master according to
189 @@ -502,14 +646,29 @@ int add_mtd_partitions(struct mtd_info *
190  {
191         struct mtd_part *slave;
192         u_int32_t cur_offset = 0;
193 -       int i;
194 +       int i, j, ret;
195  
196         printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
197  
198 -       for (i = 0; i < nbparts; i++) {
199 -               slave = add_one_partition(master, parts + i, i, cur_offset);
200 +       for (i = 0, j = 0; i < nbparts; i++) {
201 +               slave = add_one_partition(master, parts + i, j++, cur_offset);
202                 if (!slave)
203                         return -ENOMEM;
204 +
205 +               if (!strcmp(parts[i].name, "rootfs") && slave->registered) {
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);
211 +                       }
212 +#endif
213 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
214 +                       ret = split_rootfs_data(master, &slave->mtd, &parts[i], j);
215 +                       if (ret == 0)
216 +                               j++;
217 +#endif
218 +               }
219                 cur_offset = slave->offset + slave->mtd.size;
220         }
221  
222 @@ -517,6 +676,32 @@ int add_mtd_partitions(struct mtd_info *
223  }
224  EXPORT_SYMBOL(add_mtd_partitions);
225  
226 +int refresh_mtd_partitions(struct mtd_info *mtd)
227 +{
228 +       int ret = 0;
229 +
230 +       if (IS_PART(mtd)) {
231 +               struct mtd_part *part;
232 +               struct mtd_info *master;
233 +
234 +               part = PART(mtd);
235 +               master = part->master;
236 +               if (master->refresh_device)
237 +                       ret = master->refresh_device(master);
238 +       }
239 +
240 +       if (!ret && mtd->refresh_device)
241 +               ret = mtd->refresh_device(mtd);
242 +
243 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
244 +       if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
245 +               refresh_rootfs_split(mtd);
246 +#endif
247 +
248 +       return 0;
249 +}
250 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
251 +
252  static DEFINE_SPINLOCK(part_parser_lock);
253  static LIST_HEAD(part_parsers);
254  
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;
259         struct mtd_info mtd;
260         struct mutex write_mutex;
261 +       rwlock_t bdev_mutex;
262 +       char devname[0];
263  };
264  
265  
266 @@ -81,6 +83,12 @@ static int block2mtd_erase(struct mtd_in
267         size_t len = instr->len;
268         int err;
269  
270 +       read_lock(&dev->bdev_mutex);
271 +       if (!dev->blkdev) {
272 +               err = -EINVAL;
273 +               goto done;
274 +       }
275 +
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
280  
281         instr->state = MTD_ERASE_DONE;
282         mtd_erase_callback(instr);
283 +
284 +done:
285 +       read_unlock(&dev->bdev_mutex);
286 +
287         return err;
288  }
289  
290 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
291         struct page *page;
292         int index = from >> PAGE_SHIFT;
293         int offset = from & (PAGE_SIZE-1);
294 -       int cpylen;
295 +       int cpylen, err = 0;
296 +
297 +       read_lock(&dev->bdev_mutex);
298 +       if (!dev->blkdev || (from > mtd->size)) {
299 +               err = -EINVAL;
300 +               goto done;
301 +       }
302  
303 -       if (from > mtd->size)
304 -               return -EINVAL;
305         if (from + len > mtd->size)
306                 len = mtd->size - from;
307  
308 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
309                 len = len - cpylen;
310  
311                 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
312 -               if (!page)
313 -                       return -ENOMEM;
314 -               if (IS_ERR(page))
315 -                       return PTR_ERR(page);
316 +               if (!page) {
317 +                       err = -ENOMEM;
318 +                       goto done;
319 +               }
320 +               if (IS_ERR(page)) {
321 +                       err = PTR_ERR(page);
322 +                       goto done;
323 +               }
324  
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
328                 offset = 0;
329                 index++;
330         }
331 -       return 0;
332 +
333 +done:
334 +       read_unlock(&dev->bdev_mutex);
335 +       return err;
336  }
337  
338  
339 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
340                 size_t *retlen, const u_char *buf)
341  {
342         struct block2mtd_dev *dev = mtd->priv;
343 -       int err;
344 +       int err = 0;
345 +
346 +       read_lock(&dev->bdev_mutex);
347 +       if (!dev->blkdev) {
348 +               err = -EINVAL;
349 +               goto done;
350 +       }
351  
352         if (!len)
353 -               return 0;
354 -       if (to >= mtd->size)
355 -               return -ENOSPC;
356 +               goto done;
357 +
358 +       if (to >= mtd->size) {
359 +               err = -ENOSPC;
360 +               goto done;
361 +       }
362 +
363         if (to + len > mtd->size)
364                 len = mtd->size - to;
365  
366 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
367         mutex_unlock(&dev->write_mutex);
368         if (err > 0)
369                 err = 0;
370 +
371 +done:
372 +       read_unlock(&dev->bdev_mutex);
373         return err;
374  }
375  
376 @@ -210,32 +246,109 @@ static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
377  static void block2mtd_sync(struct mtd_info *mtd)
378  {
379         struct block2mtd_dev *dev = mtd->priv;
380 -       sync_blockdev(dev->blkdev);
381 +
382 +       read_lock(&dev->bdev_mutex);
383 +       if (dev->blkdev)
384 +               sync_blockdev(dev->blkdev);
385 +       read_unlock(&dev->bdev_mutex);
386 +
387         return;
388  }
389  
390  
391 +static int _open_bdev(struct block2mtd_dev *dev)
392 +{
393 +       struct block_device *bdev;
394 +
395 +       /* Get a handle on the device */
396 +       bdev = open_bdev_excl(dev->devname, O_RDWR, NULL);
397 +#ifndef MODULE
398 +       if (IS_ERR(bdev)) {
399 +
400 +               /* We might not have rootfs mounted at this point. Try
401 +                  to resolve the device name by other means. */
402 +
403 +               dev_t devt = name_to_dev_t(dev->devname);
404 +               if (devt) {
405 +                       bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
406 +               }
407 +       }
408 +#endif
409 +
410 +       if (IS_ERR(bdev)) {
411 +               ERROR("error: cannot open device %s", dev->devname);
412 +               return 1;
413 +       }
414 +       dev->blkdev = bdev;
415 +
416 +       if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
417 +               ERROR("attempting to use an MTD device as a block device");
418 +               return 1;
419 +       }
420 +
421 +       return 0;
422 +}
423 +
424 +static void _close_bdev(struct block2mtd_dev *dev)
425 +{
426 +       struct block_device *bdev;
427 +
428 +       if (!dev->blkdev)
429 +               return;
430 +
431 +       bdev = dev->blkdev;
432 +       invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
433 +       close_bdev_excl(dev->blkdev);
434 +       dev->blkdev = NULL;
435 +}
436 +
437  static void block2mtd_free_device(struct block2mtd_dev *dev)
438  {
439         if (!dev)
440                 return;
441  
442         kfree(dev->mtd.name);
443 +       _close_bdev(dev);
444 +       kfree(dev);
445 +}
446 +
447 +
448 +static int block2mtd_refresh(struct mtd_info *mtd)
449 +{
450 +       struct block2mtd_dev *dev = mtd->priv;
451 +       struct block_device *bdev;
452 +       dev_t devt;
453 +       int err = 0;
454 +
455 +       /* no other mtd function can run at this point */
456 +       write_lock(&dev->bdev_mutex);
457 +
458 +       /* get the device number for the whole disk */
459 +       devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
460  
461 -       if (dev->blkdev) {
462 -               invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
463 -                                       0, -1);
464 -               close_bdev_excl(dev->blkdev);
465 +       /* close the old block device */
466 +       _close_bdev(dev);
467 +
468 +       /* open the whole disk, issue a partition rescan, then */
469 +       bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
470 +       if (!bdev || !bdev->bd_disk)
471 +               err = -EINVAL;
472 +       else {
473 +               err = rescan_partitions(bdev->bd_disk, bdev);
474         }
475 +       if (bdev)
476 +               close_bdev_excl(bdev);
477  
478 -       kfree(dev);
479 -}
480 +       /* try to open the partition block device again */
481 +       _open_bdev(dev);
482 +       write_unlock(&dev->bdev_mutex);
483  
484 +       return err;
485 +}
486  
487  /* FIXME: ensure that mtd->size % erase_size == 0 */
488 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
489 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
490  {
491 -       struct block_device *bdev;
492         struct block2mtd_dev *dev;
493         struct mtd_partition *part;
494         char *name;
495 @@ -243,37 +356,17 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size, const cha
496         if (!devname)
497                 return NULL;
498  
499 -       dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
500 +       dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
501         if (!dev)
502                 return NULL;
503  
504 -       /* Get a handle on the device */
505 -       bdev = open_bdev_excl(devname, O_RDWR, NULL);
506 -#ifndef MODULE
507 -       if (IS_ERR(bdev)) {
508 -
509 -               /* We might not have rootfs mounted at this point. Try
510 -                  to resolve the device name by other means. */
511 +       strcpy(dev->devname, devname);
512  
513 -               dev_t devt = name_to_dev_t(devname);
514 -               if (devt) {
515 -                       bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
516 -               }
517 -       }
518 -#endif
519 -
520 -       if (IS_ERR(bdev)) {
521 -               ERROR("error: cannot open device %s", devname);
522 +       if (_open_bdev(dev))
523                 goto devinit_err;
524 -       }
525 -       dev->blkdev = bdev;
526 -
527 -       if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
528 -               ERROR("attempting to use an MTD device as a block device");
529 -               goto devinit_err;
530 -       }
531  
532         mutex_init(&dev->write_mutex);
533 +       rwlock_init(&dev->bdev_mutex);
534  
535         if (!mtdname)
536                 mtdname = devname;
537 @@ -297,6 +408,7 @@ static struct block2mtd_dev *add_device(
538         dev->mtd.read = block2mtd_read;
539         dev->mtd.priv = dev;
540         dev->mtd.owner = THIS_MODULE;
541 +       dev->mtd.refresh_device = block2mtd_refresh;
542  
543         part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
544         part->name = dev->mtd.name;
545 --- a/drivers/mtd/mtdchar.c
546 +++ b/drivers/mtd/mtdchar.c
547 @@ -16,6 +16,7 @@
548  
549  #include <linux/mtd/mtd.h>
550  #include <linux/mtd/compatmac.h>
551 +#include <linux/mtd/partitions.h>
552  
553  #include <asm/uaccess.h>
554  
555 @@ -771,6 +772,13 @@ static int mtd_ioctl(struct inode *inode
556                 file->f_pos = 0;
557                 break;
558         }
559 +#ifdef CONFIG_MTD_PARTITIONS
560 +       case MTDREFRESH:
561 +       {
562 +               ret = refresh_mtd_partitions(mtd);
563 +               break;
564 +       }
565 +#endif
566  
567         default:
568                 ret = -ENOTTY;
569 --- a/include/linux/mtd/mtd.h
570 +++ b/include/linux/mtd/mtd.h
571 @@ -96,6 +96,7 @@ struct mtd_oob_ops {
572         uint8_t         *oobbuf;
573  };
574  
575 +struct mtd_info;
576  struct mtd_info {
577         u_char type;
578         u_int32_t flags;
579 @@ -211,6 +212,9 @@ struct mtd_info {
580         struct module *owner;
581         int usecount;
582  
583 +       int (*refresh_device)(struct mtd_info *mtd);
584 +       struct mtd_info *split;
585 +
586         /* If the driver is something smart, like UBI, it may need to maintain
587          * its own reference counting. The below functions are only for driver.
588          * The driver may register its callbacks. These callbacks are not
589 --- a/include/linux/mtd/partitions.h
590 +++ b/include/linux/mtd/partitions.h
591 @@ -34,6 +34,7 @@
592   * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
593   */
594  
595 +struct mtd_partition;
596  struct mtd_partition {
597         char *name;                     /* identifier string */
598         u_int32_t size;                 /* partition size */
599 @@ -41,6 +42,7 @@ struct mtd_partition {
600         u_int32_t mask_flags;           /* master MTD flags to mask out for this partition */
601         struct nand_ecclayout *ecclayout;       /* out of band layout for this partition (NAND only)*/
602         struct mtd_info **mtdp;         /* pointer to store the MTD object */
603 +       int (*refresh_partition)(struct mtd_info *);
604  };
605  
606  #define MTDPART_OFS_NXTBLK     (-2)
607 @@ -50,6 +52,7 @@ struct mtd_partition {
608  
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 *);
612  
613  /*
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 @@ -93,6 +93,7 @@ struct otp_info {
618  #define ECCGETLAYOUT           _IOR('M', 17, struct nand_ecclayout)
619  #define ECCGETSTATS            _IOR('M', 18, struct mtd_ecc_stats)
620  #define MTDFILEMODE            _IO('M', 19)
621 +#define MTDREFRESH             _IO('M', 23)
622  
623  /*
624   * Obsolete legacy interface. Keep it in order not to break userspace