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