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