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