generic: Add support for 2.6.39
[openwrt.git] / target / linux / generic / patches-2.6.39 / 065-rootfs_split.patch
1 --- a/drivers/mtd/Kconfig
2 +++ b/drivers/mtd/Kconfig
3 @@ -47,6 +47,16 @@ config MTD_PARTITIONS
4  
5  if MTD_PARTITIONS
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         ---help---
20 --- a/drivers/mtd/mtdpart.c
21 +++ b/drivers/mtd/mtdpart.c
22 @@ -29,6 +29,8 @@
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>
29  
30  /* Our partition linked list */
31 @@ -48,7 +50,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 @@ -636,6 +638,153 @@ int mtd_del_partition(struct mtd_info *m
41  }
42  EXPORT_SYMBOL_GPL(mtd_del_partition);
43  
44 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
45 +#define ROOTFS_SPLIT_NAME "rootfs_data"
46 +#define ROOTFS_REMOVED_NAME "<removed>"
47 +
48 +struct squashfs_super_block {
49 +       __le32 s_magic;
50 +       __le32 pad0[9];
51 +       __le64 bytes_used;
52 +};
53 +
54 +
55 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
56 +{
57 +       struct squashfs_super_block sb;
58 +       int len, ret;
59 +
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);
64 +               return -EINVAL;
65 +       }
66 +
67 +       if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
68 +               printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
69 +                       master->name);
70 +               *split_offset = 0;
71 +               return 0;
72 +       }
73 +
74 +       if (le64_to_cpu((sb.bytes_used)) <= 0) {
75 +               printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
76 +                       master->name);
77 +               *split_offset = 0;
78 +               return 0;
79 +       }
80 +
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;
87 +
88 +       return 0;
89 +}
90 +
91 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, const struct mtd_partition *part)
92 +{
93 +       struct mtd_partition *dpart;
94 +       struct mtd_part *slave = NULL;
95 +       int ret, split_offset = 0;
96 +
97 +       ret = split_squashfs(master, part->offset, &split_offset);
98 +       if (ret)
99 +               return ret;
100 +
101 +       if (split_offset <= 0)
102 +               return 0;
103 +
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);
108 +               return -ENOMEM;
109 +       }
110 +
111 +       memcpy(dpart, part, sizeof(*part));
112 +       dpart->name = (unsigned char *)&dpart[1];
113 +       strcpy(dpart->name, ROOTFS_SPLIT_NAME);
114 +
115 +       dpart->size -= split_offset - dpart->offset;
116 +       dpart->offset = split_offset;
117 +
118 +       if (dpart == NULL)
119 +               return 1;
120 +
121 +       printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%llX, len=%llX \n",
122 +               ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
123 +
124 +       slave = allocate_partition(master, dpart, 0, split_offset);
125 +       if (IS_ERR(slave))
126 +               return PTR_ERR(slave);
127 +       mutex_lock(&mtd_partitions_mutex);
128 +       list_add(&slave->list, &mtd_partitions);
129 +       mutex_unlock(&mtd_partitions_mutex);
130 +
131 +       add_mtd_device(&slave->mtd);
132 +
133 +       rpart->split = &slave->mtd;
134 +
135 +       return 0;
136 +}
137 +
138 +static int refresh_rootfs_split(struct mtd_info *mtd)
139 +{
140 +       struct mtd_partition tpart;
141 +       struct mtd_part *part;
142 +       char *name;
143 +       //int index = 0;
144 +       int offset, size;
145 +       int ret;
146 +
147 +       part = PART(mtd);
148 +
149 +       /* check for the new squashfs offset first */
150 +       ret = split_squashfs(part->master, part->offset, &offset);
151 +       if (ret)
152 +               return ret;
153 +
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;
160 +
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;
165 +
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);
177 +
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;
183 +               part->offset = 0;
184 +               part->mtd.size = 0;
185 +       }
186 +
187 +       return 0;
188 +}
189 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
190 +
191  /*
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 @@ -652,6 +801,9 @@ int add_mtd_partitions(struct mtd_info *
195         struct mtd_part *slave;
196         uint64_t cur_offset = 0;
197         int i;
198 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
199 +       int ret;
200 +#endif
201  
202         printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
203  
204 @@ -666,6 +818,21 @@ int add_mtd_partitions(struct mtd_info *
205  
206                 add_mtd_device(&slave->mtd);
207  
208 +               if (!strcmp(parts[i].name, "rootfs")) {
209 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
210 +                       if (ROOT_DEV == 0) {
211 +                               printk(KERN_NOTICE "mtd: partition \"rootfs\" "
212 +                                       "set to be root filesystem\n");
213 +                               ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
214 +                       }
215 +#endif
216 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
217 +                       ret = split_rootfs_data(master, &slave->mtd, &parts[i]);
218 +                       /* if (ret == 0)
219 +                        *      j++; */
220 +#endif
221 +               }
222 +
223                 cur_offset = slave->offset + slave->mtd.size;
224         }
225  
226 @@ -673,6 +840,32 @@ int add_mtd_partitions(struct mtd_info *
227  }
228  EXPORT_SYMBOL(add_mtd_partitions);
229  
230 +int refresh_mtd_partitions(struct mtd_info *mtd)
231 +{
232 +       int ret = 0;
233 +
234 +       if (IS_PART(mtd)) {
235 +               struct mtd_part *part;
236 +               struct mtd_info *master;
237 +
238 +               part = PART(mtd);
239 +               master = part->master;
240 +               if (master->refresh_device)
241 +                       ret = master->refresh_device(master);
242 +       }
243 +
244 +       if (!ret && mtd->refresh_device)
245 +               ret = mtd->refresh_device(mtd);
246 +
247 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
248 +       if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
249 +               refresh_rootfs_split(mtd);
250 +#endif
251 +
252 +       return 0;
253 +}
254 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
255 +
256  static DEFINE_SPINLOCK(part_parser_lock);
257  static LIST_HEAD(part_parsers);
258  
259 --- a/drivers/mtd/devices/block2mtd.c
260 +++ b/drivers/mtd/devices/block2mtd.c
261 @@ -30,6 +30,8 @@ struct block2mtd_dev {
262         struct block_device *blkdev;
263         struct mtd_info mtd;
264         struct mutex write_mutex;
265 +       rwlock_t bdev_mutex;
266 +       char devname[0];
267  };
268  
269  
270 @@ -82,6 +84,12 @@ static int block2mtd_erase(struct mtd_in
271         size_t len = instr->len;
272         int err;
273  
274 +       read_lock(&dev->bdev_mutex);
275 +       if (!dev->blkdev) {
276 +               err = -EINVAL;
277 +               goto done;
278 +       }
279 +
280         instr->state = MTD_ERASING;
281         mutex_lock(&dev->write_mutex);
282         err = _block2mtd_erase(dev, from, len);
283 @@ -93,6 +101,10 @@ static int block2mtd_erase(struct mtd_in
284                 instr->state = MTD_ERASE_DONE;
285  
286         mtd_erase_callback(instr);
287 +
288 +done:
289 +       read_unlock(&dev->bdev_mutex);
290 +
291         return err;
292  }
293  
294 @@ -104,10 +116,14 @@ static int block2mtd_read(struct mtd_inf
295         struct page *page;
296         int index = from >> PAGE_SHIFT;
297         int offset = from & (PAGE_SIZE-1);
298 -       int cpylen;
299 +       int cpylen, err = 0;
300 +
301 +       read_lock(&dev->bdev_mutex);
302 +       if (!dev->blkdev || (from > mtd->size)) {
303 +               err = -EINVAL;
304 +               goto done;
305 +       }
306  
307 -       if (from > mtd->size)
308 -               return -EINVAL;
309         if (from + len > mtd->size)
310                 len = mtd->size - from;
311  
312 @@ -122,10 +138,14 @@ static int block2mtd_read(struct mtd_inf
313                 len = len - cpylen;
314  
315                 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
316 -               if (!page)
317 -                       return -ENOMEM;
318 -               if (IS_ERR(page))
319 -                       return PTR_ERR(page);
320 +               if (!page) {
321 +                       err = -ENOMEM;
322 +                       goto done;
323 +               }
324 +               if (IS_ERR(page)) {
325 +                       err = PTR_ERR(page);
326 +                       goto done;
327 +               }
328  
329                 memcpy(buf, page_address(page) + offset, cpylen);
330                 page_cache_release(page);
331 @@ -136,7 +156,10 @@ static int block2mtd_read(struct mtd_inf
332                 offset = 0;
333                 index++;
334         }
335 -       return 0;
336 +
337 +done:
338 +       read_unlock(&dev->bdev_mutex);
339 +       return err;
340  }
341  
342  
343 @@ -188,12 +211,22 @@ static int block2mtd_write(struct mtd_in
344                 size_t *retlen, const u_char *buf)
345  {
346         struct block2mtd_dev *dev = mtd->priv;
347 -       int err;
348 +       int err = 0;
349 +
350 +       read_lock(&dev->bdev_mutex);
351 +       if (!dev->blkdev) {
352 +               err = -EINVAL;
353 +               goto done;
354 +       }
355  
356         if (!len)
357 -               return 0;
358 -       if (to >= mtd->size)
359 -               return -ENOSPC;
360 +               goto done;
361 +
362 +       if (to >= mtd->size) {
363 +               err = -ENOSPC;
364 +               goto done;
365 +       }
366 +
367         if (to + len > mtd->size)
368                 len = mtd->size - to;
369  
370 @@ -202,6 +235,9 @@ static int block2mtd_write(struct mtd_in
371         mutex_unlock(&dev->write_mutex);
372         if (err > 0)
373                 err = 0;
374 +
375 +done:
376 +       read_unlock(&dev->bdev_mutex);
377         return err;
378  }
379  
380 @@ -210,33 +246,109 @@ static int block2mtd_write(struct mtd_in
381  static void block2mtd_sync(struct mtd_info *mtd)
382  {
383         struct block2mtd_dev *dev = mtd->priv;
384 +       read_lock(&dev->bdev_mutex);
385 +       if (dev->blkdev)
386         sync_blockdev(dev->blkdev);
387 +       read_unlock(&dev->bdev_mutex);
388 +
389         return;
390  }
391  
392  
393 +static int _open_bdev(struct block2mtd_dev *dev)
394 +{
395 +       const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
396 +       struct block_device *bdev;
397 +
398 +       /* Get a handle on the device */
399 +       bdev = blkdev_get_by_path(dev->devname, mode, dev);
400 +#ifndef MODULE
401 +       if (IS_ERR(bdev)) {
402 +
403 +               /* We might not have rootfs mounted at this point. Try
404 +                  to resolve the device name by other means. */
405 +
406 +               dev_t devt = name_to_dev_t(dev->devname);
407 +               if (devt)
408 +                       bdev = blkdev_get_by_dev(devt, mode, dev);
409 +       }
410 +#endif
411 +
412 +       if (IS_ERR(bdev)) {
413 +               ERROR("error: cannot open device %s", dev->devname);
414 +               return 1;
415 +       }
416 +       dev->blkdev = bdev;
417 +
418 +       if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
419 +               ERROR("attempting to use an MTD device as a block device");
420 +               return 1;
421 +       }
422 +
423 +       return 0;
424 +}
425 +
426 +static void _close_bdev(struct block2mtd_dev *dev)
427 +{
428 +       struct block_device *bdev;
429 +
430 +       if (!dev->blkdev)
431 +               return;
432 +
433 +       bdev = dev->blkdev;
434 +       invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
435 +       blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
436 +       dev->blkdev = NULL;
437 +}
438 +
439  static void block2mtd_free_device(struct block2mtd_dev *dev)
440  {
441         if (!dev)
442                 return;
443  
444         kfree(dev->mtd.name);
445 -
446 -       if (dev->blkdev) {
447 -               invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
448 -                                       0, -1);
449 -               blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
450 -       }
451 -
452 +       _close_bdev(dev);
453         kfree(dev);
454  }
455  
456  
457 -/* FIXME: ensure that mtd->size % erase_size == 0 */
458 -static struct block2mtd_dev *add_device(char *devname, int erase_size, const char *mtdname)
459 +static int block2mtd_refresh(struct mtd_info *mtd)
460  {
461 -       const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
462 +       struct block2mtd_dev *dev = mtd->priv;
463         struct block_device *bdev;
464 +       dev_t devt;
465 +       int err = 0;
466 +
467 +       /* no other mtd function can run at this point */
468 +       write_lock(&dev->bdev_mutex);
469 +
470 +       /* get the device number for the whole disk */
471 +       devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
472 +
473 +       /* close the old block device */
474 +       _close_bdev(dev);
475 +
476 +       /* open the whole disk, issue a partition rescan, then */
477 +       bdev = blkdev_get_by_dev(devt, FMODE_WRITE | FMODE_READ);
478 +       if (!bdev || !bdev->bd_disk)
479 +               err = -EINVAL;
480 +#ifndef CONFIG_MTD_BLOCK2MTD_MODULE
481 +       else
482 +               err = rescan_partitions(bdev->bd_disk, bdev);
483 +#endif
484 +       if (bdev)
485 +               blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
486 +
487 +       /* try to open the partition block device again */
488 +       _open_bdev(dev);
489 +       write_unlock(&dev->bdev_mutex);
490 +
491 +       return err;
492 +}
493 +
494 +/* FIXME: ensure that mtd->size % erase_size == 0 */
495 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
496 +{
497         struct block2mtd_dev *dev;
498         struct mtd_partition *part;
499         char *name;
500 @@ -244,36 +356,17 @@ static struct block2mtd_dev *add_device(
501         if (!devname)
502                 return NULL;
503  
504 -       dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
505 +       dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
506         if (!dev)
507                 return NULL;
508  
509 -       /* Get a handle on the device */
510 -       bdev = blkdev_get_by_path(devname, mode, dev);
511 -#ifndef MODULE
512 -       if (IS_ERR(bdev)) {
513 -
514 -               /* We might not have rootfs mounted at this point. Try
515 -                  to resolve the device name by other means. */
516 +       strcpy(dev->devname, devname);
517  
518 -               dev_t devt = name_to_dev_t(devname);
519 -               if (devt)
520 -                       bdev = blkdev_get_by_dev(devt, mode, dev);
521 -       }
522 -#endif
523 -
524 -       if (IS_ERR(bdev)) {
525 -               ERROR("error: cannot open device %s", devname);
526 +       if (_open_bdev(dev))
527                 goto devinit_err;
528 -       }
529 -       dev->blkdev = bdev;
530 -
531 -       if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
532 -               ERROR("attempting to use an MTD device as a block device");
533 -               goto devinit_err;
534 -       }
535  
536         mutex_init(&dev->write_mutex);
537 +       rwlock_init(&dev->bdev_mutex);
538  
539         /* Setup the MTD structure */
540         /* make the name contain the block device in */
541 @@ -298,6 +391,7 @@ static struct block2mtd_dev *add_device(
542         dev->mtd.read = block2mtd_read;
543         dev->mtd.priv = dev;
544         dev->mtd.owner = THIS_MODULE;
545 +       dev->mtd.refresh_device = block2mtd_refresh;
546  
547         part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
548         part->name = dev->mtd.name;
549 --- a/drivers/mtd/mtdchar.c
550 +++ b/drivers/mtd/mtdchar.c
551 @@ -841,6 +841,13 @@ static int mtd_ioctl(struct file *file, 
552                 file->f_pos = 0;
553                 break;
554         }
555 +#ifdef CONFIG_MTD_PARTITIONS
556 +       case MTDREFRESH:
557 +       {
558 +               ret = refresh_mtd_partitions(mtd);
559 +               break;
560 +       }
561 +#endif
562  
563         case OTPGETREGIONCOUNT:
564         case OTPGETREGIONINFO:
565 --- a/include/linux/mtd/mtd.h
566 +++ b/include/linux/mtd/mtd.h
567 @@ -125,6 +125,7 @@ struct nand_ecclayout {
568         struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
569  };
570  
571 +struct mtd_info;
572  struct mtd_info {
573         u_char type;
574         uint32_t flags;
575 @@ -277,6 +278,9 @@ struct mtd_info {
576         struct device dev;
577         int usecount;
578  
579 +       int (*refresh_device)(struct mtd_info *mtd);
580 +       struct mtd_info *split;
581 +
582         /* If the driver is something smart, like UBI, it may need to maintain
583          * its own reference counting. The below functions are only for driver.
584          * The driver may register its callbacks. These callbacks are not
585 --- a/include/linux/mtd/partitions.h
586 +++ b/include/linux/mtd/partitions.h
587 @@ -34,12 +34,14 @@
588   * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
589   */
590  
591 +struct mtd_partition;
592  struct mtd_partition {
593         char *name;                     /* identifier string */
594         uint64_t size;                  /* partition size */
595         uint64_t offset;                /* offset within the master MTD space */
596         uint32_t mask_flags;            /* master MTD flags to mask out for this partition */
597         struct nand_ecclayout *ecclayout;       /* out of band layout for this partition (NAND only) */
598 +       int (*refresh_partition)(struct mtd_info *);
599  };
600  
601  #define MTDPART_OFS_NXTBLK     (-2)
602 @@ -51,6 +53,7 @@ struct mtd_info;
603  
604  int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
605  int del_mtd_partitions(struct mtd_info *);
606 +int refresh_mtd_partitions(struct mtd_info *);
607  
608  /*
609   * Functions dealing with the various ways of partitioning the space
610 --- a/include/mtd/mtd-abi.h
611 +++ b/include/mtd/mtd-abi.h
612 @@ -127,6 +127,7 @@ struct otp_info {
613  #define MEMWRITEOOB64          _IOWR('M', 21, struct mtd_oob_buf64)
614  #define MEMREADOOB64           _IOWR('M', 22, struct mtd_oob_buf64)
615  #define MEMISLOCKED            _IOR('M', 23, struct erase_info_user)
616 +#define MTDREFRESH             _IO('M', 23)
617  
618  /*
619   * Obsolete legacy interface. Keep it in order not to break userspace