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