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