make etrax serial driver register its device nodes, so we get a console
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.25 / 065-rootfs_split.patch
1 Index: linux-2.6.25.1/drivers/mtd/Kconfig
2 ===================================================================
3 --- linux-2.6.25.1.orig/drivers/mtd/Kconfig
4 +++ linux-2.6.25.1/drivers/mtd/Kconfig
5 @@ -47,6 +47,16 @@ config MTD_PARTITIONS
6 devices. Partitioning on NFTL 'devices' is a different - that's the
7 'normal' form of partitioning used on a block device.
8
9 +config MTD_ROOTFS_ROOT_DEV
10 + bool "Automatically set 'rootfs' partition to be root filesystem"
11 + depends on MTD_PARTITIONS
12 + default y
13 +
14 +config MTD_ROOTFS_SPLIT
15 + bool "Automatically split 'rootfs' partition for squashfs"
16 + depends on MTD_PARTITIONS
17 + default y
18 +
19 config MTD_REDBOOT_PARTS
20 tristate "RedBoot partition table parsing"
21 depends on MTD_PARTITIONS
22 Index: linux-2.6.25.1/drivers/mtd/mtdpart.c
23 ===================================================================
24 --- linux-2.6.25.1.orig/drivers/mtd/mtdpart.c
25 +++ linux-2.6.25.1/drivers/mtd/mtdpart.c
26 @@ -20,6 +20,8 @@
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/mtd/compatmac.h>
30 +#include <linux/squashfs_fs.h>
31 +#include <linux/root_dev.h>
32
33 /* Our partition linked list */
34 static LIST_HEAD(mtd_partitions);
35 @@ -39,7 +41,7 @@ struct mtd_part {
36 * the pointer to that structure with this macro.
37 */
38 #define PART(x) ((struct mtd_part *)(x))
39 -
40 +#define IS_PART(mtd) (mtd->read == part_read)
41
42 /*
43 * MTD methods which simply translate the effective address and pass through
44 @@ -322,6 +324,316 @@ int del_mtd_partitions(struct mtd_info *
45 return 0;
46 }
47
48 +static u_int32_t cur_offset = 0;
49 +static int add_one_partition(struct mtd_info *master, const struct mtd_partition *part,
50 + int i, struct mtd_part **slp)
51 +{
52 + struct mtd_part *slave;
53 +
54 + /* allocate the partition structure */
55 + slave = kzalloc (sizeof(*slave), GFP_KERNEL);
56 + if (!slave) {
57 + printk ("memory allocation error while creating partitions for \"%s\"\n",
58 + master->name);
59 + del_mtd_partitions(master);
60 + return -ENOMEM;
61 + }
62 + list_add(&slave->list, &mtd_partitions);
63 +
64 + /* set up the MTD object for this partition */
65 + slave->mtd.type = master->type;
66 + slave->mtd.flags = master->flags & ~part->mask_flags;
67 + slave->mtd.size = part->size;
68 + slave->mtd.writesize = master->writesize;
69 + slave->mtd.oobsize = master->oobsize;
70 + slave->mtd.oobavail = master->oobavail;
71 + slave->mtd.subpage_sft = master->subpage_sft;
72 +
73 + slave->mtd.name = part->name;
74 + slave->mtd.owner = master->owner;
75 +
76 + slave->mtd.read = part_read;
77 + slave->mtd.write = part_write;
78 +
79 + if (master->panic_write)
80 + slave->mtd.panic_write = part_panic_write;
81 +
82 + slave->mtd.refresh_device = part->refresh_partition;
83 +
84 + if(master->point && master->unpoint){
85 + slave->mtd.point = part_point;
86 + slave->mtd.unpoint = part_unpoint;
87 + }
88 +
89 + if (master->read_oob)
90 + slave->mtd.read_oob = part_read_oob;
91 + if (master->write_oob)
92 + slave->mtd.write_oob = part_write_oob;
93 + if(master->read_user_prot_reg)
94 + slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
95 + if(master->read_fact_prot_reg)
96 + slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
97 + if(master->write_user_prot_reg)
98 + slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
99 + if(master->lock_user_prot_reg)
100 + slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
101 + if(master->get_user_prot_info)
102 + slave->mtd.get_user_prot_info = part_get_user_prot_info;
103 + if(master->get_fact_prot_info)
104 + slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
105 + if (master->sync)
106 + slave->mtd.sync = part_sync;
107 + if (!i && master->suspend && master->resume) {
108 + slave->mtd.suspend = part_suspend;
109 + slave->mtd.resume = part_resume;
110 + }
111 + if (master->writev)
112 + slave->mtd.writev = part_writev;
113 + if (master->lock)
114 + slave->mtd.lock = part_lock;
115 + if (master->unlock)
116 + slave->mtd.unlock = part_unlock;
117 + if (master->block_isbad)
118 + slave->mtd.block_isbad = part_block_isbad;
119 + if (master->block_markbad)
120 + slave->mtd.block_markbad = part_block_markbad;
121 + slave->mtd.erase = part_erase;
122 + slave->master = master;
123 + slave->offset = part->offset;
124 + slave->index = i;
125 +
126 + if (slave->offset == MTDPART_OFS_APPEND)
127 + slave->offset = cur_offset;
128 + if (slave->offset == MTDPART_OFS_NXTBLK) {
129 + slave->offset = cur_offset;
130 + if ((cur_offset % master->erasesize) != 0) {
131 + /* Round up to next erasesize */
132 + slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
133 + printk(KERN_NOTICE "Moving partition %d: "
134 + "0x%08x -> 0x%08x\n", i,
135 + cur_offset, slave->offset);
136 + }
137 + }
138 + if (slave->mtd.size == MTDPART_SIZ_FULL)
139 + slave->mtd.size = master->size - slave->offset;
140 + cur_offset = slave->offset + slave->mtd.size;
141 +
142 + printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
143 + slave->offset + slave->mtd.size, slave->mtd.name);
144 +
145 + /* let's do some sanity checks */
146 + if (slave->offset >= master->size) {
147 + /* let's register it anyway to preserve ordering */
148 + slave->offset = 0;
149 + slave->mtd.size = 0;
150 + printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
151 + part->name);
152 + }
153 + if (slave->offset + slave->mtd.size > master->size) {
154 + slave->mtd.size = master->size - slave->offset;
155 + printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
156 + part->name, master->name, slave->mtd.size);
157 + }
158 + if (master->numeraseregions>1) {
159 + /* Deal with variable erase size stuff */
160 + int i;
161 + struct mtd_erase_region_info *regions = master->eraseregions;
162 +
163 + /* Find the first erase regions which is part of this partition. */
164 + for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
165 + ;
166 +
167 + for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
168 + if (slave->mtd.erasesize < regions[i].erasesize) {
169 + slave->mtd.erasesize = regions[i].erasesize;
170 + }
171 + }
172 + } else {
173 + /* Single erase size */
174 + slave->mtd.erasesize = master->erasesize;
175 + }
176 +
177 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
178 + (slave->offset % slave->mtd.erasesize)) {
179 + /* Doesn't start on a boundary of major erase size */
180 + /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
181 + slave->mtd.flags &= ~MTD_WRITEABLE;
182 + printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
183 + part->name);
184 + }
185 + if ((slave->mtd.flags & MTD_WRITEABLE) &&
186 + (slave->mtd.size % slave->mtd.erasesize)) {
187 + slave->mtd.flags &= ~MTD_WRITEABLE;
188 + printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
189 + part->name);
190 + }
191 +
192 + slave->mtd.ecclayout = master->ecclayout;
193 + if (master->block_isbad) {
194 + uint32_t offs = 0;
195 +
196 + while(offs < slave->mtd.size) {
197 + if (master->block_isbad(master,
198 + offs + slave->offset))
199 + slave->mtd.ecc_stats.badblocks++;
200 + offs += slave->mtd.erasesize;
201 + }
202 + }
203 +
204 + if(part->mtdp)
205 + { /* store the object pointer (caller may or may not register it */
206 + *part->mtdp = &slave->mtd;
207 + slave->registered = 0;
208 + }
209 + else
210 + {
211 + /* register our partition */
212 + add_mtd_device(&slave->mtd);
213 + slave->registered = 1;
214 + }
215 +
216 + if (slp)
217 + *slp = slave;
218 +
219 + return 0;
220 +}
221 +
222 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
223 +#define ROOTFS_SPLIT_NAME "rootfs_data"
224 +#define ROOTFS_REMOVED_NAME "<removed>"
225 +static int split_squashfs(struct mtd_info *master, int offset, int *split_offset)
226 +{
227 + char buf[512];
228 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
229 + int len, ret;
230 +
231 + ret = master->read(master, offset, sizeof(*sb), &len, buf);
232 + if (ret || (len != sizeof(*sb))) {
233 + printk(KERN_ALERT "split_squashfs: error occured while reading "
234 + "from \"%s\"\n", master->name);
235 + return -EINVAL;
236 + }
237 +
238 + if (*((u32 *) buf) != SQUASHFS_MAGIC) {
239 + printk(KERN_ALERT "split_squashfs: no squashfs found in \"%s\"\n",
240 + master->name);
241 + *split_offset = 0;
242 + return 0;
243 + }
244 +
245 + if (sb->bytes_used <= 0) {
246 + printk(KERN_ALERT "split_squashfs: squashfs is empty in \"%s\"\n",
247 + master->name);
248 + *split_offset = 0;
249 + return 0;
250 + }
251 +
252 + len = (u32) sb->bytes_used;
253 + len += (offset & 0x000fffff);
254 + len += (master->erasesize - 1);
255 + len &= ~(master->erasesize - 1);
256 + len -= (offset & 0x000fffff);
257 + *split_offset = offset + len;
258 +
259 + return 0;
260 +}
261 +
262 +static int split_rootfs_data(struct mtd_info *master, struct mtd_info *rpart, struct mtd_partition *part,
263 + int index)
264 +{
265 + struct mtd_partition *dpart;
266 + struct mtd_part *slave = NULL;
267 + int split_offset = 0;
268 + int ret;
269 +
270 + ret = split_squashfs(master, part->offset, &split_offset);
271 + if (ret)
272 + return ret;
273 +
274 + if (split_offset <= 0)
275 + return 0;
276 +
277 + dpart = kmalloc(sizeof(*part)+sizeof(ROOTFS_SPLIT_NAME)+1, GFP_KERNEL);
278 + if (dpart == NULL) {
279 + printk(KERN_INFO "split_squashfs: no memory for partition \"%s\"\n",
280 + ROOTFS_SPLIT_NAME);
281 + return -ENOMEM;
282 + }
283 +
284 + memcpy(dpart, part, sizeof(*part));
285 + dpart->name = (unsigned char *)&dpart[1];
286 + strcpy(dpart->name, ROOTFS_SPLIT_NAME);
287 +
288 + dpart->size -= split_offset - dpart->offset;
289 + dpart->offset = split_offset;
290 +
291 + if (dpart == NULL)
292 + return 1;
293 +
294 + printk(KERN_INFO "mtd: partition \"%s\" created automatically, ofs=%X, len=%X \n",
295 + ROOTFS_SPLIT_NAME, dpart->offset, dpart->size);
296 +
297 + ret = add_one_partition(master, dpart, index, &slave);
298 + if (ret)
299 + kfree(dpart);
300 + else if (slave)
301 + rpart->split = &slave->mtd;
302 +
303 + return ret;
304 +}
305 +
306 +static int refresh_rootfs_split(struct mtd_info *mtd)
307 +{
308 + struct mtd_partition tpart;
309 + struct mtd_part *part;
310 + int index = 0;
311 + int offset, size;
312 + int ret;
313 +
314 + part = PART(mtd);
315 +
316 + /* check for the new squashfs offset first */
317 + ret = split_squashfs(part->master, part->offset, &offset);
318 + if (ret)
319 + return ret;
320 +
321 + if ((offset > 0) && !mtd->split) {
322 + printk(KERN_INFO "%s: creating new split partition for \"%s\"\n", __func__, mtd->name);
323 + /* if we don't have a rootfs split partition, create a new one */
324 + tpart.name = mtd->name;
325 + tpart.size = mtd->size;
326 + tpart.offset = part->offset;
327 +
328 + /* find the index of the last partition */
329 + if (!list_empty(&mtd_partitions))
330 + index = list_first_entry(&mtd_partitions, struct mtd_part, list)->index + 1;
331 +
332 + return split_rootfs_data(part->master, &part->mtd, &tpart, index);
333 + } else if ((offset > 0) && mtd->split) {
334 + /* update the offsets of the existing partition */
335 + size = mtd->size + part->offset - offset;
336 +
337 + part = PART(mtd->split);
338 + part->offset = offset;
339 + part->mtd.size = size;
340 + printk(KERN_INFO "%s: %s partition \"" ROOTFS_SPLIT_NAME "\", offset: 0x%06x (0x%06x)\n",
341 + __func__, (!strcmp(part->mtd.name, ROOTFS_SPLIT_NAME) ? "updating" : "creating"),
342 + part->offset, part->mtd.size);
343 + strcpy(part->mtd.name, ROOTFS_SPLIT_NAME);
344 + } else if ((offset <= 0) && mtd->split) {
345 + printk(KERN_INFO "%s: removing partition \"%s\"\n", __func__, mtd->split->name);
346 +
347 + /* mark existing partition as removed */
348 + part = PART(mtd->split);
349 + strcpy(part->mtd.name, ROOTFS_REMOVED_NAME);
350 + part->offset = 0;
351 + part->mtd.size = 0;
352 + }
353 +
354 + return 0;
355 +}
356 +#endif /* CONFIG_MTD_ROOTFS_SPLIT */
357 +
358 /*
359 * This function, given a master MTD object and a partition table, creates
360 * and registers slave MTD objects which are bound to the master according to
361 @@ -334,171 +646,31 @@ int add_mtd_partitions(struct mtd_info *
362 int nbparts)
363 {
364 struct mtd_part *slave;
365 - u_int32_t cur_offset = 0;
366 - int i;
367 + struct mtd_partition *part;
368 + int i, j, ret = 0;
369
370 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
371
372 - for (i = 0; i < nbparts; i++) {
373 -
374 - /* allocate the partition structure */
375 - slave = kzalloc (sizeof(*slave), GFP_KERNEL);
376 - if (!slave) {
377 - printk ("memory allocation error while creating partitions for \"%s\"\n",
378 - master->name);
379 - del_mtd_partitions(master);
380 - return -ENOMEM;
381 - }
382 - list_add(&slave->list, &mtd_partitions);
383 -
384 - /* set up the MTD object for this partition */
385 - slave->mtd.type = master->type;
386 - slave->mtd.flags = master->flags & ~parts[i].mask_flags;
387 - slave->mtd.size = parts[i].size;
388 - slave->mtd.writesize = master->writesize;
389 - slave->mtd.oobsize = master->oobsize;
390 - slave->mtd.oobavail = master->oobavail;
391 - slave->mtd.subpage_sft = master->subpage_sft;
392 -
393 - slave->mtd.name = parts[i].name;
394 - slave->mtd.owner = master->owner;
395 -
396 - slave->mtd.read = part_read;
397 - slave->mtd.write = part_write;
398 -
399 - if (master->panic_write)
400 - slave->mtd.panic_write = part_panic_write;
401 -
402 - if(master->point && master->unpoint){
403 - slave->mtd.point = part_point;
404 - slave->mtd.unpoint = part_unpoint;
405 - }
406 -
407 - if (master->read_oob)
408 - slave->mtd.read_oob = part_read_oob;
409 - if (master->write_oob)
410 - slave->mtd.write_oob = part_write_oob;
411 - if(master->read_user_prot_reg)
412 - slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
413 - if(master->read_fact_prot_reg)
414 - slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
415 - if(master->write_user_prot_reg)
416 - slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
417 - if(master->lock_user_prot_reg)
418 - slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
419 - if(master->get_user_prot_info)
420 - slave->mtd.get_user_prot_info = part_get_user_prot_info;
421 - if(master->get_fact_prot_info)
422 - slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
423 - if (master->sync)
424 - slave->mtd.sync = part_sync;
425 - if (!i && master->suspend && master->resume) {
426 - slave->mtd.suspend = part_suspend;
427 - slave->mtd.resume = part_resume;
428 - }
429 - if (master->writev)
430 - slave->mtd.writev = part_writev;
431 - if (master->lock)
432 - slave->mtd.lock = part_lock;
433 - if (master->unlock)
434 - slave->mtd.unlock = part_unlock;
435 - if (master->block_isbad)
436 - slave->mtd.block_isbad = part_block_isbad;
437 - if (master->block_markbad)
438 - slave->mtd.block_markbad = part_block_markbad;
439 - slave->mtd.erase = part_erase;
440 - slave->master = master;
441 - slave->offset = parts[i].offset;
442 - slave->index = i;
443 -
444 - if (slave->offset == MTDPART_OFS_APPEND)
445 - slave->offset = cur_offset;
446 - if (slave->offset == MTDPART_OFS_NXTBLK) {
447 - slave->offset = cur_offset;
448 - if ((cur_offset % master->erasesize) != 0) {
449 - /* Round up to next erasesize */
450 - slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
451 - printk(KERN_NOTICE "Moving partition %d: "
452 - "0x%08x -> 0x%08x\n", i,
453 - cur_offset, slave->offset);
454 - }
455 - }
456 - if (slave->mtd.size == MTDPART_SIZ_FULL)
457 - slave->mtd.size = master->size - slave->offset;
458 - cur_offset = slave->offset + slave->mtd.size;
459 -
460 - printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
461 - slave->offset + slave->mtd.size, slave->mtd.name);
462 -
463 - /* let's do some sanity checks */
464 - if (slave->offset >= master->size) {
465 - /* let's register it anyway to preserve ordering */
466 - slave->offset = 0;
467 - slave->mtd.size = 0;
468 - printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
469 - parts[i].name);
470 - }
471 - if (slave->offset + slave->mtd.size > master->size) {
472 - slave->mtd.size = master->size - slave->offset;
473 - printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
474 - parts[i].name, master->name, slave->mtd.size);
475 - }
476 - if (master->numeraseregions>1) {
477 - /* Deal with variable erase size stuff */
478 - int i;
479 - struct mtd_erase_region_info *regions = master->eraseregions;
480 -
481 - /* Find the first erase regions which is part of this partition. */
482 - for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
483 - ;
484 -
485 - for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
486 - if (slave->mtd.erasesize < regions[i].erasesize) {
487 - slave->mtd.erasesize = regions[i].erasesize;
488 - }
489 + for (i = 0, j = 0; i < nbparts; i++) {
490 + part = (struct mtd_partition *) &parts[i];
491 + ret = add_one_partition(master, part, j, &slave);
492 + if (ret)
493 + return ret;
494 + j++;
495 +
496 + if (strcmp(part->name, "rootfs") == 0 && slave->registered) {
497 +#ifdef CONFIG_MTD_ROOTFS_ROOT_DEV
498 + if (ROOT_DEV == 0) {
499 + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
500 + "set to be root filesystem\n");
501 + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, slave->mtd.index);
502 }
503 - } else {
504 - /* Single erase size */
505 - slave->mtd.erasesize = master->erasesize;
506 - }
507 -
508 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
509 - (slave->offset % slave->mtd.erasesize)) {
510 - /* Doesn't start on a boundary of major erase size */
511 - /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
512 - slave->mtd.flags &= ~MTD_WRITEABLE;
513 - printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
514 - parts[i].name);
515 - }
516 - if ((slave->mtd.flags & MTD_WRITEABLE) &&
517 - (slave->mtd.size % slave->mtd.erasesize)) {
518 - slave->mtd.flags &= ~MTD_WRITEABLE;
519 - printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
520 - parts[i].name);
521 - }
522 -
523 - slave->mtd.ecclayout = master->ecclayout;
524 - if (master->block_isbad) {
525 - uint32_t offs = 0;
526 -
527 - while(offs < slave->mtd.size) {
528 - if (master->block_isbad(master,
529 - offs + slave->offset))
530 - slave->mtd.ecc_stats.badblocks++;
531 - offs += slave->mtd.erasesize;
532 - }
533 - }
534 -
535 - if(parts[i].mtdp)
536 - { /* store the object pointer (caller may or may not register it */
537 - *parts[i].mtdp = &slave->mtd;
538 - slave->registered = 0;
539 - }
540 - else
541 - {
542 - /* register our partition */
543 - add_mtd_device(&slave->mtd);
544 - slave->registered = 1;
545 +#endif
546 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
547 + ret = split_rootfs_data(master, &slave->mtd, part, j);
548 + if (ret == 0)
549 + j++;
550 +#endif
551 }
552 }
553
554 @@ -574,6 +746,32 @@ int parse_mtd_partitions(struct mtd_info
555 return ret;
556 }
557
558 +int refresh_mtd_partitions(struct mtd_info *mtd)
559 +{
560 + int ret = 0;
561 +
562 + if (IS_PART(mtd)) {
563 + struct mtd_part *part;
564 + struct mtd_info *master;
565 +
566 + part = PART(mtd);
567 + master = part->master;
568 + if (master->refresh_device)
569 + ret = master->refresh_device(master);
570 + }
571 +
572 + if (!ret && mtd->refresh_device)
573 + ret = mtd->refresh_device(mtd);
574 +
575 +#ifdef CONFIG_MTD_ROOTFS_SPLIT
576 + if (!ret && IS_PART(mtd) && !strcmp(mtd->name, "rootfs"))
577 + refresh_rootfs_split(mtd);
578 +#endif
579 +
580 + return 0;
581 +}
582 +
583 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
584 +EXPORT_SYMBOL_GPL(refresh_mtd_partitions);
585 EXPORT_SYMBOL_GPL(register_mtd_parser);
586 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
587 Index: linux-2.6.25.1/drivers/mtd/devices/block2mtd.c
588 ===================================================================
589 --- linux-2.6.25.1.orig/drivers/mtd/devices/block2mtd.c
590 +++ linux-2.6.25.1/drivers/mtd/devices/block2mtd.c
591 @@ -34,6 +34,8 @@ struct block2mtd_dev {
592 struct block_device *blkdev;
593 struct mtd_info mtd;
594 struct mutex write_mutex;
595 + rwlock_t bdev_mutex;
596 + char devname[0];
597 };
598
599
600 @@ -86,6 +88,12 @@ static int block2mtd_erase(struct mtd_in
601 size_t len = instr->len;
602 int err;
603
604 + read_lock(&dev->bdev_mutex);
605 + if (!dev->blkdev) {
606 + err = -EINVAL;
607 + goto done;
608 + }
609 +
610 instr->state = MTD_ERASING;
611 mutex_lock(&dev->write_mutex);
612 err = _block2mtd_erase(dev, from, len);
613 @@ -98,6 +106,10 @@ static int block2mtd_erase(struct mtd_in
614
615 instr->state = MTD_ERASE_DONE;
616 mtd_erase_callback(instr);
617 +
618 +done:
619 + read_unlock(&dev->bdev_mutex);
620 +
621 return err;
622 }
623
624 @@ -109,10 +121,14 @@ static int block2mtd_read(struct mtd_inf
625 struct page *page;
626 int index = from >> PAGE_SHIFT;
627 int offset = from & (PAGE_SIZE-1);
628 - int cpylen;
629 + int cpylen, err = 0;
630 +
631 + read_lock(&dev->bdev_mutex);
632 + if (!dev->blkdev || (from > mtd->size)) {
633 + err = -EINVAL;
634 + goto done;
635 + }
636
637 - if (from > mtd->size)
638 - return -EINVAL;
639 if (from + len > mtd->size)
640 len = mtd->size - from;
641
642 @@ -127,10 +143,14 @@ static int block2mtd_read(struct mtd_inf
643 len = len - cpylen;
644
645 page = page_read(dev->blkdev->bd_inode->i_mapping, index);
646 - if (!page)
647 - return -ENOMEM;
648 - if (IS_ERR(page))
649 - return PTR_ERR(page);
650 + if (!page) {
651 + err = -ENOMEM;
652 + goto done;
653 + }
654 + if (IS_ERR(page)) {
655 + err = PTR_ERR(page);
656 + goto done;
657 + }
658
659 memcpy(buf, page_address(page) + offset, cpylen);
660 page_cache_release(page);
661 @@ -141,7 +161,10 @@ static int block2mtd_read(struct mtd_inf
662 offset = 0;
663 index++;
664 }
665 - return 0;
666 +
667 +done:
668 + read_unlock(&dev->bdev_mutex);
669 + return err;
670 }
671
672
673 @@ -193,12 +216,22 @@ static int block2mtd_write(struct mtd_in
674 size_t *retlen, const u_char *buf)
675 {
676 struct block2mtd_dev *dev = mtd->priv;
677 - int err;
678 + int err = 0;
679 +
680 + read_lock(&dev->bdev_mutex);
681 + if (!dev->blkdev) {
682 + err = -EINVAL;
683 + goto done;
684 + }
685
686 if (!len)
687 - return 0;
688 - if (to >= mtd->size)
689 - return -ENOSPC;
690 + goto done;
691 +
692 + if (to >= mtd->size) {
693 + err = -ENOSPC;
694 + goto done;
695 + }
696 +
697 if (to + len > mtd->size)
698 len = mtd->size - to;
699
700 @@ -207,6 +240,9 @@ static int block2mtd_write(struct mtd_in
701 mutex_unlock(&dev->write_mutex);
702 if (err > 0)
703 err = 0;
704 +
705 +done:
706 + read_unlock(&dev->bdev_mutex);
707 return err;
708 }
709
710 @@ -215,51 +251,29 @@ static int block2mtd_write(struct mtd_in
711 static void block2mtd_sync(struct mtd_info *mtd)
712 {
713 struct block2mtd_dev *dev = mtd->priv;
714 - sync_blockdev(dev->blkdev);
715 - return;
716 -}
717 -
718 -
719 -static void block2mtd_free_device(struct block2mtd_dev *dev)
720 -{
721 - if (!dev)
722 - return;
723 -
724 - kfree(dev->mtd.name);
725
726 - if (dev->blkdev) {
727 - invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
728 - 0, -1);
729 - close_bdev_excl(dev->blkdev);
730 - }
731 + read_lock(&dev->bdev_mutex);
732 + if (dev->blkdev)
733 + sync_blockdev(dev->blkdev);
734 + read_unlock(&dev->bdev_mutex);
735
736 - kfree(dev);
737 + return;
738 }
739
740
741 -/* FIXME: ensure that mtd->size % erase_size == 0 */
742 -static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
743 +static int _open_bdev(struct block2mtd_dev *dev)
744 {
745 struct block_device *bdev;
746 - struct block2mtd_dev *dev;
747 - struct mtd_partition *part;
748 -
749 - if (!devname)
750 - return NULL;
751 -
752 - dev = kzalloc(sizeof(struct block2mtd_dev), GFP_KERNEL);
753 - if (!dev)
754 - return NULL;
755
756 /* Get a handle on the device */
757 - bdev = open_bdev_excl(devname, O_RDWR, NULL);
758 + bdev = open_bdev_excl(dev->devname, O_RDWR, NULL);
759 #ifndef MODULE
760 if (IS_ERR(bdev)) {
761
762 /* We might not have rootfs mounted at this point. Try
763 to resolve the device name by other means. */
764
765 - dev_t devt = name_to_dev_t(devname);
766 + dev_t devt = name_to_dev_t(dev->devname);
767 if (devt) {
768 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
769 }
770 @@ -267,17 +281,96 @@ static struct block2mtd_dev *add_device(
771 #endif
772
773 if (IS_ERR(bdev)) {
774 - ERROR("error: cannot open device %s", devname);
775 - goto devinit_err;
776 + ERROR("error: cannot open device %s", dev->devname);
777 + return 1;
778 }
779 dev->blkdev = bdev;
780
781 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
782 ERROR("attempting to use an MTD device as a block device");
783 - goto devinit_err;
784 + return 1;
785 }
786
787 + return 0;
788 +}
789 +
790 +static void _close_bdev(struct block2mtd_dev *dev)
791 +{
792 + struct block_device *bdev;
793 +
794 + if (!dev->blkdev)
795 + return;
796 +
797 + bdev = dev->blkdev;
798 + invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 0, -1);
799 + close_bdev_excl(dev->blkdev);
800 + dev->blkdev = NULL;
801 +}
802 +
803 +static void block2mtd_free_device(struct block2mtd_dev *dev)
804 +{
805 + if (!dev)
806 + return;
807 +
808 + kfree(dev->mtd.name);
809 + _close_bdev(dev);
810 + kfree(dev);
811 +}
812 +
813 +
814 +static int block2mtd_refresh(struct mtd_info *mtd)
815 +{
816 + struct block2mtd_dev *dev = mtd->priv;
817 + struct block_device *bdev;
818 + dev_t devt;
819 + int err = 0;
820 +
821 + /* no other mtd function can run at this point */
822 + write_lock(&dev->bdev_mutex);
823 +
824 + /* get the device number for the whole disk */
825 + devt = MKDEV(MAJOR(dev->blkdev->bd_dev), 0);
826 +
827 + /* close the old block device */
828 + _close_bdev(dev);
829 +
830 + /* open the whole disk, issue a partition rescan, then */
831 + bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ);
832 + if (!bdev || !bdev->bd_disk)
833 + err = -EINVAL;
834 + else {
835 + err = rescan_partitions(bdev->bd_disk, bdev);
836 + }
837 + if (bdev)
838 + close_bdev_excl(bdev);
839 +
840 + /* try to open the partition block device again */
841 + _open_bdev(dev);
842 + write_unlock(&dev->bdev_mutex);
843 +
844 + return err;
845 +}
846 +
847 +/* FIXME: ensure that mtd->size % erase_size == 0 */
848 +static struct block2mtd_dev *add_device(char *devname, int erase_size, char *mtdname)
849 +{
850 + struct block2mtd_dev *dev;
851 + struct mtd_partition *part;
852 +
853 + if (!devname)
854 + return NULL;
855 +
856 + dev = kzalloc(sizeof(struct block2mtd_dev) + strlen(devname) + 1, GFP_KERNEL);
857 + if (!dev)
858 + return NULL;
859 +
860 + strcpy(dev->devname, devname);
861 +
862 + if (_open_bdev(dev))
863 + goto devinit_err;
864 +
865 mutex_init(&dev->write_mutex);
866 + rwlock_init(&dev->bdev_mutex);
867
868 /* Setup the MTD structure */
869 /* make the name contain the block device in */
870 @@ -304,6 +397,7 @@ static struct block2mtd_dev *add_device(
871 dev->mtd.read = block2mtd_read;
872 dev->mtd.priv = dev;
873 dev->mtd.owner = THIS_MODULE;
874 + dev->mtd.refresh_device = block2mtd_refresh;
875
876 part = kzalloc(sizeof(struct mtd_partition), GFP_KERNEL);
877 part->name = dev->mtd.name;
878 Index: linux-2.6.25.1/drivers/mtd/mtdchar.c
879 ===================================================================
880 --- linux-2.6.25.1.orig/drivers/mtd/mtdchar.c
881 +++ linux-2.6.25.1/drivers/mtd/mtdchar.c
882 @@ -17,6 +17,7 @@
883
884 #include <linux/mtd/mtd.h>
885 #include <linux/mtd/compatmac.h>
886 +#include <linux/mtd/partitions.h>
887
888 #include <asm/uaccess.h>
889
890 @@ -756,6 +757,13 @@ static int mtd_ioctl(struct inode *inode
891 file->f_pos = 0;
892 break;
893 }
894 +#ifdef CONFIG_MTD_PARTITIONS
895 + case MTDREFRESH:
896 + {
897 + ret = refresh_mtd_partitions(mtd);
898 + break;
899 + }
900 +#endif
901
902 default:
903 ret = -ENOTTY;
904 Index: linux-2.6.25.1/include/linux/mtd/mtd.h
905 ===================================================================
906 --- linux-2.6.25.1.orig/include/linux/mtd/mtd.h
907 +++ linux-2.6.25.1/include/linux/mtd/mtd.h
908 @@ -98,6 +98,7 @@ struct mtd_oob_ops {
909 uint8_t *oobbuf;
910 };
911
912 +struct mtd_info;
913 struct mtd_info {
914 u_char type;
915 u_int32_t flags;
916 @@ -211,6 +212,9 @@ struct mtd_info {
917 struct module *owner;
918 int usecount;
919
920 + int (*refresh_device)(struct mtd_info *mtd);
921 + struct mtd_info *split;
922 +
923 /* If the driver is something smart, like UBI, it may need to maintain
924 * its own reference counting. The below functions are only for driver.
925 * The driver may register its callbacks. These callbacks are not
926 Index: linux-2.6.25.1/include/linux/mtd/partitions.h
927 ===================================================================
928 --- linux-2.6.25.1.orig/include/linux/mtd/partitions.h
929 +++ linux-2.6.25.1/include/linux/mtd/partitions.h
930 @@ -36,6 +36,7 @@
931 * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
932 */
933
934 +struct mtd_partition;
935 struct mtd_partition {
936 char *name; /* identifier string */
937 u_int32_t size; /* partition size */
938 @@ -43,6 +44,7 @@ struct mtd_partition {
939 u_int32_t mask_flags; /* master MTD flags to mask out for this partition */
940 struct nand_ecclayout *ecclayout; /* out of band layout for this partition (NAND only)*/
941 struct mtd_info **mtdp; /* pointer to store the MTD object */
942 + int (*refresh_partition)(struct mtd_info *);
943 };
944
945 #define MTDPART_OFS_NXTBLK (-2)
946 @@ -52,6 +54,7 @@ struct mtd_partition {
947
948 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
949 int del_mtd_partitions(struct mtd_info *);
950 +int refresh_mtd_partitions(struct mtd_info *);
951
952 /*
953 * Functions dealing with the various ways of partitioning the space
954 Index: linux-2.6.25.1/include/mtd/mtd-abi.h
955 ===================================================================
956 --- linux-2.6.25.1.orig/include/mtd/mtd-abi.h
957 +++ linux-2.6.25.1/include/mtd/mtd-abi.h
958 @@ -95,6 +95,7 @@ struct otp_info {
959 #define ECCGETLAYOUT _IOR('M', 17, struct nand_ecclayout)
960 #define ECCGETSTATS _IOR('M', 18, struct mtd_ecc_stats)
961 #define MTDFILEMODE _IO('M', 19)
962 +#define MTDREFRESH _IO('M', 23)
963
964 /*
965 * Obsolete legacy interface. Keep it in order not to break userspace
This page took 0.081365 seconds and 5 git commands to generate.