perf session: Keep file mmaped instead of malloc/memcpy
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / mtdpart.c
blob79e3689f1e16901f07e5ee770bcde218814d621d
1 /*
2 * Simple MTD partitioning layer
4 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/kmod.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/err.h>
34 /* Our partition linked list */
35 static LIST_HEAD(mtd_partitions);
36 static DEFINE_MUTEX(mtd_partitions_mutex);
38 /* Our partition node structure */
39 struct mtd_part {
40 struct mtd_info mtd;
41 struct mtd_info *master;
42 uint64_t offset;
43 struct list_head list;
47 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
48 * the pointer to that structure with this macro.
50 #define PART(x) ((struct mtd_part *)(x))
54 * MTD methods which simply translate the effective address and pass through
55 * to the _real_ device.
58 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
59 size_t *retlen, u_char *buf)
61 struct mtd_part *part = PART(mtd);
62 struct mtd_ecc_stats stats;
63 int res;
65 stats = part->master->ecc_stats;
67 if (from >= mtd->size)
68 len = 0;
69 else if (from + len > mtd->size)
70 len = mtd->size - from;
71 res = part->master->read(part->master, from + part->offset,
72 len, retlen, buf);
73 if (unlikely(res)) {
74 if (res == -EUCLEAN)
75 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
76 if (res == -EBADMSG)
77 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
79 return res;
82 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
83 size_t *retlen, void **virt, resource_size_t *phys)
85 struct mtd_part *part = PART(mtd);
86 if (from >= mtd->size)
87 len = 0;
88 else if (from + len > mtd->size)
89 len = mtd->size - from;
90 return part->master->point (part->master, from + part->offset,
91 len, retlen, virt, phys);
94 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
96 struct mtd_part *part = PART(mtd);
98 part->master->unpoint(part->master, from + part->offset, len);
101 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
102 unsigned long len,
103 unsigned long offset,
104 unsigned long flags)
106 struct mtd_part *part = PART(mtd);
108 offset += part->offset;
109 return part->master->get_unmapped_area(part->master, len, offset,
110 flags);
113 static int part_read_oob(struct mtd_info *mtd, loff_t from,
114 struct mtd_oob_ops *ops)
116 struct mtd_part *part = PART(mtd);
117 int res;
119 if (from >= mtd->size)
120 return -EINVAL;
121 if (ops->datbuf && from + ops->len > mtd->size)
122 return -EINVAL;
123 res = part->master->read_oob(part->master, from + part->offset, ops);
125 if (unlikely(res)) {
126 if (res == -EUCLEAN)
127 mtd->ecc_stats.corrected++;
128 if (res == -EBADMSG)
129 mtd->ecc_stats.failed++;
131 return res;
134 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
135 size_t len, size_t *retlen, u_char *buf)
137 struct mtd_part *part = PART(mtd);
138 return part->master->read_user_prot_reg(part->master, from,
139 len, retlen, buf);
142 static int part_get_user_prot_info(struct mtd_info *mtd,
143 struct otp_info *buf, size_t len)
145 struct mtd_part *part = PART(mtd);
146 return part->master->get_user_prot_info(part->master, buf, len);
149 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
150 size_t len, size_t *retlen, u_char *buf)
152 struct mtd_part *part = PART(mtd);
153 return part->master->read_fact_prot_reg(part->master, from,
154 len, retlen, buf);
157 static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
158 size_t len)
160 struct mtd_part *part = PART(mtd);
161 return part->master->get_fact_prot_info(part->master, buf, len);
164 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
165 size_t *retlen, const u_char *buf)
167 struct mtd_part *part = PART(mtd);
168 if (!(mtd->flags & MTD_WRITEABLE))
169 return -EROFS;
170 if (to >= mtd->size)
171 len = 0;
172 else if (to + len > mtd->size)
173 len = mtd->size - to;
174 return part->master->write(part->master, to + part->offset,
175 len, retlen, buf);
178 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
179 size_t *retlen, const u_char *buf)
181 struct mtd_part *part = PART(mtd);
182 if (!(mtd->flags & MTD_WRITEABLE))
183 return -EROFS;
184 if (to >= mtd->size)
185 len = 0;
186 else if (to + len > mtd->size)
187 len = mtd->size - to;
188 return part->master->panic_write(part->master, to + part->offset,
189 len, retlen, buf);
192 static int part_write_oob(struct mtd_info *mtd, loff_t to,
193 struct mtd_oob_ops *ops)
195 struct mtd_part *part = PART(mtd);
197 if (!(mtd->flags & MTD_WRITEABLE))
198 return -EROFS;
200 if (to >= mtd->size)
201 return -EINVAL;
202 if (ops->datbuf && to + ops->len > mtd->size)
203 return -EINVAL;
204 return part->master->write_oob(part->master, to + part->offset, ops);
207 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
208 size_t len, size_t *retlen, u_char *buf)
210 struct mtd_part *part = PART(mtd);
211 return part->master->write_user_prot_reg(part->master, from,
212 len, retlen, buf);
215 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
216 size_t len)
218 struct mtd_part *part = PART(mtd);
219 return part->master->lock_user_prot_reg(part->master, from, len);
222 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
223 unsigned long count, loff_t to, size_t *retlen)
225 struct mtd_part *part = PART(mtd);
226 if (!(mtd->flags & MTD_WRITEABLE))
227 return -EROFS;
228 return part->master->writev(part->master, vecs, count,
229 to + part->offset, retlen);
232 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
234 struct mtd_part *part = PART(mtd);
235 int ret;
236 if (!(mtd->flags & MTD_WRITEABLE))
237 return -EROFS;
238 if (instr->addr >= mtd->size)
239 return -EINVAL;
240 instr->addr += part->offset;
241 ret = part->master->erase(part->master, instr);
242 if (ret) {
243 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
244 instr->fail_addr -= part->offset;
245 instr->addr -= part->offset;
247 return ret;
250 void mtd_erase_callback(struct erase_info *instr)
252 if (instr->mtd->erase == part_erase) {
253 struct mtd_part *part = PART(instr->mtd);
255 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
256 instr->fail_addr -= part->offset;
257 instr->addr -= part->offset;
259 if (instr->callback)
260 instr->callback(instr);
262 EXPORT_SYMBOL_GPL(mtd_erase_callback);
264 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
266 struct mtd_part *part = PART(mtd);
267 if ((len + ofs) > mtd->size)
268 return -EINVAL;
269 return part->master->lock(part->master, ofs + part->offset, len);
272 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
274 struct mtd_part *part = PART(mtd);
275 if ((len + ofs) > mtd->size)
276 return -EINVAL;
277 return part->master->unlock(part->master, ofs + part->offset, len);
280 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
282 struct mtd_part *part = PART(mtd);
283 if ((len + ofs) > mtd->size)
284 return -EINVAL;
285 return part->master->is_locked(part->master, ofs + part->offset, len);
288 static void part_sync(struct mtd_info *mtd)
290 struct mtd_part *part = PART(mtd);
291 part->master->sync(part->master);
294 static int part_suspend(struct mtd_info *mtd)
296 struct mtd_part *part = PART(mtd);
297 return part->master->suspend(part->master);
300 static void part_resume(struct mtd_info *mtd)
302 struct mtd_part *part = PART(mtd);
303 part->master->resume(part->master);
306 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
308 struct mtd_part *part = PART(mtd);
309 if (ofs >= mtd->size)
310 return -EINVAL;
311 ofs += part->offset;
312 return part->master->block_isbad(part->master, ofs);
315 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
317 struct mtd_part *part = PART(mtd);
318 int res;
320 if (!(mtd->flags & MTD_WRITEABLE))
321 return -EROFS;
322 if (ofs >= mtd->size)
323 return -EINVAL;
324 ofs += part->offset;
325 res = part->master->block_markbad(part->master, ofs);
326 if (!res)
327 mtd->ecc_stats.badblocks++;
328 return res;
331 static inline void free_partition(struct mtd_part *p)
333 kfree(p->mtd.name);
334 kfree(p);
338 * This function unregisters and destroy all slave MTD objects which are
339 * attached to the given master MTD object.
342 int del_mtd_partitions(struct mtd_info *master)
344 struct mtd_part *slave, *next;
345 int ret, err = 0;
347 mutex_lock(&mtd_partitions_mutex);
348 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
349 if (slave->master == master) {
350 ret = del_mtd_device(&slave->mtd);
351 if (ret < 0) {
352 err = ret;
353 continue;
355 list_del(&slave->list);
356 free_partition(slave);
358 mutex_unlock(&mtd_partitions_mutex);
360 return err;
362 EXPORT_SYMBOL(del_mtd_partitions);
364 static struct mtd_part *allocate_partition(struct mtd_info *master,
365 const struct mtd_partition *part, int partno,
366 uint64_t cur_offset)
368 struct mtd_part *slave;
369 char *name;
371 /* allocate the partition structure */
372 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
373 name = kstrdup(part->name, GFP_KERNEL);
374 if (!name || !slave) {
375 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
376 master->name);
377 kfree(name);
378 kfree(slave);
379 return ERR_PTR(-ENOMEM);
382 /* set up the MTD object for this partition */
383 slave->mtd.type = master->type;
384 slave->mtd.flags = master->flags & ~part->mask_flags;
385 slave->mtd.size = part->size;
386 slave->mtd.writesize = master->writesize;
387 slave->mtd.oobsize = master->oobsize;
388 slave->mtd.oobavail = master->oobavail;
389 slave->mtd.subpage_sft = master->subpage_sft;
391 slave->mtd.name = name;
392 slave->mtd.owner = master->owner;
393 slave->mtd.backing_dev_info = master->backing_dev_info;
395 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
396 * to have the same data be in two different partitions.
398 slave->mtd.dev.parent = master->dev.parent;
400 slave->mtd.read = part_read;
401 slave->mtd.write = part_write;
403 if (master->panic_write)
404 slave->mtd.panic_write = part_panic_write;
406 if (master->point && master->unpoint) {
407 slave->mtd.point = part_point;
408 slave->mtd.unpoint = part_unpoint;
411 if (master->get_unmapped_area)
412 slave->mtd.get_unmapped_area = part_get_unmapped_area;
413 if (master->read_oob)
414 slave->mtd.read_oob = part_read_oob;
415 if (master->write_oob)
416 slave->mtd.write_oob = part_write_oob;
417 if (master->read_user_prot_reg)
418 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
419 if (master->read_fact_prot_reg)
420 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
421 if (master->write_user_prot_reg)
422 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
423 if (master->lock_user_prot_reg)
424 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
425 if (master->get_user_prot_info)
426 slave->mtd.get_user_prot_info = part_get_user_prot_info;
427 if (master->get_fact_prot_info)
428 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
429 if (master->sync)
430 slave->mtd.sync = part_sync;
431 if (!partno && !master->dev.class && master->suspend && master->resume) {
432 slave->mtd.suspend = part_suspend;
433 slave->mtd.resume = part_resume;
435 if (master->writev)
436 slave->mtd.writev = part_writev;
437 if (master->lock)
438 slave->mtd.lock = part_lock;
439 if (master->unlock)
440 slave->mtd.unlock = part_unlock;
441 if (master->is_locked)
442 slave->mtd.is_locked = part_is_locked;
443 if (master->block_isbad)
444 slave->mtd.block_isbad = part_block_isbad;
445 if (master->block_markbad)
446 slave->mtd.block_markbad = part_block_markbad;
447 slave->mtd.erase = part_erase;
448 slave->master = master;
449 slave->offset = part->offset;
451 if (slave->offset == MTDPART_OFS_APPEND)
452 slave->offset = cur_offset;
453 if (slave->offset == MTDPART_OFS_NXTBLK) {
454 slave->offset = cur_offset;
455 if (mtd_mod_by_eb(cur_offset, master) != 0) {
456 /* Round up to next erasesize */
457 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
458 printk(KERN_NOTICE "Moving partition %d: "
459 "0x%012llx -> 0x%012llx\n", partno,
460 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
463 if (slave->mtd.size == MTDPART_SIZ_FULL)
464 slave->mtd.size = master->size - slave->offset;
466 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
467 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
469 /* let's do some sanity checks */
470 if (slave->offset >= master->size) {
471 /* let's register it anyway to preserve ordering */
472 slave->offset = 0;
473 slave->mtd.size = 0;
474 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
475 part->name);
476 goto out_register;
478 if (slave->offset + slave->mtd.size > master->size) {
479 slave->mtd.size = master->size - slave->offset;
480 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
481 part->name, master->name, (unsigned long long)slave->mtd.size);
483 if (master->numeraseregions > 1) {
484 /* Deal with variable erase size stuff */
485 int i, max = master->numeraseregions;
486 u64 end = slave->offset + slave->mtd.size;
487 struct mtd_erase_region_info *regions = master->eraseregions;
489 /* Find the first erase regions which is part of this
490 * partition. */
491 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
493 /* The loop searched for the region _behind_ the first one */
494 if (i > 0)
495 i--;
497 /* Pick biggest erasesize */
498 for (; i < max && regions[i].offset < end; i++) {
499 if (slave->mtd.erasesize < regions[i].erasesize) {
500 slave->mtd.erasesize = regions[i].erasesize;
503 BUG_ON(slave->mtd.erasesize == 0);
504 } else {
505 /* Single erase size */
506 slave->mtd.erasesize = master->erasesize;
509 if ((slave->mtd.flags & MTD_WRITEABLE) &&
510 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
511 /* Doesn't start on a boundary of major erase size */
512 /* FIXME: Let it be writable if it is on a boundary of
513 * _minor_ erase size though */
514 slave->mtd.flags &= ~MTD_WRITEABLE;
515 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
516 part->name);
518 if ((slave->mtd.flags & MTD_WRITEABLE) &&
519 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
520 slave->mtd.flags &= ~MTD_WRITEABLE;
521 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
522 part->name);
525 slave->mtd.ecclayout = master->ecclayout;
526 if (master->block_isbad) {
527 uint64_t offs = 0;
529 while (offs < slave->mtd.size) {
530 if (master->block_isbad(master,
531 offs + slave->offset))
532 slave->mtd.ecc_stats.badblocks++;
533 offs += slave->mtd.erasesize;
537 out_register:
538 return slave;
541 int mtd_add_partition(struct mtd_info *master, char *name,
542 long long offset, long long length)
544 struct mtd_partition part;
545 struct mtd_part *p, *new;
546 uint64_t start, end;
547 int ret = 0;
549 /* the direct offset is expected */
550 if (offset == MTDPART_OFS_APPEND ||
551 offset == MTDPART_OFS_NXTBLK)
552 return -EINVAL;
554 if (length == MTDPART_SIZ_FULL)
555 length = master->size - offset;
557 if (length <= 0)
558 return -EINVAL;
560 part.name = name;
561 part.size = length;
562 part.offset = offset;
563 part.mask_flags = 0;
564 part.ecclayout = NULL;
566 new = allocate_partition(master, &part, -1, offset);
567 if (IS_ERR(new))
568 return PTR_ERR(new);
570 start = offset;
571 end = offset + length;
573 mutex_lock(&mtd_partitions_mutex);
574 list_for_each_entry(p, &mtd_partitions, list)
575 if (p->master == master) {
576 if ((start >= p->offset) &&
577 (start < (p->offset + p->mtd.size)))
578 goto err_inv;
580 if ((end >= p->offset) &&
581 (end < (p->offset + p->mtd.size)))
582 goto err_inv;
585 list_add(&new->list, &mtd_partitions);
586 mutex_unlock(&mtd_partitions_mutex);
588 add_mtd_device(&new->mtd);
590 return ret;
591 err_inv:
592 mutex_unlock(&mtd_partitions_mutex);
593 free_partition(new);
594 return -EINVAL;
596 EXPORT_SYMBOL_GPL(mtd_add_partition);
598 int mtd_del_partition(struct mtd_info *master, int partno)
600 struct mtd_part *slave, *next;
601 int ret = -EINVAL;
603 mutex_lock(&mtd_partitions_mutex);
604 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
605 if ((slave->master == master) &&
606 (slave->mtd.index == partno)) {
607 ret = del_mtd_device(&slave->mtd);
608 if (ret < 0)
609 break;
611 list_del(&slave->list);
612 free_partition(slave);
613 break;
615 mutex_unlock(&mtd_partitions_mutex);
617 return ret;
619 EXPORT_SYMBOL_GPL(mtd_del_partition);
622 * This function, given a master MTD object and a partition table, creates
623 * and registers slave MTD objects which are bound to the master according to
624 * the partition definitions.
626 * We don't register the master, or expect the caller to have done so,
627 * for reasons of data integrity.
630 int add_mtd_partitions(struct mtd_info *master,
631 const struct mtd_partition *parts,
632 int nbparts)
634 struct mtd_part *slave;
635 uint64_t cur_offset = 0;
636 int i;
638 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
640 for (i = 0; i < nbparts; i++) {
641 slave = allocate_partition(master, parts + i, i, cur_offset);
642 if (IS_ERR(slave))
643 return PTR_ERR(slave);
645 mutex_lock(&mtd_partitions_mutex);
646 list_add(&slave->list, &mtd_partitions);
647 mutex_unlock(&mtd_partitions_mutex);
649 add_mtd_device(&slave->mtd);
651 cur_offset = slave->offset + slave->mtd.size;
654 return 0;
656 EXPORT_SYMBOL(add_mtd_partitions);
658 static DEFINE_SPINLOCK(part_parser_lock);
659 static LIST_HEAD(part_parsers);
661 static struct mtd_part_parser *get_partition_parser(const char *name)
663 struct mtd_part_parser *p, *ret = NULL;
665 spin_lock(&part_parser_lock);
667 list_for_each_entry(p, &part_parsers, list)
668 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
669 ret = p;
670 break;
673 spin_unlock(&part_parser_lock);
675 return ret;
678 int register_mtd_parser(struct mtd_part_parser *p)
680 spin_lock(&part_parser_lock);
681 list_add(&p->list, &part_parsers);
682 spin_unlock(&part_parser_lock);
684 return 0;
686 EXPORT_SYMBOL_GPL(register_mtd_parser);
688 int deregister_mtd_parser(struct mtd_part_parser *p)
690 spin_lock(&part_parser_lock);
691 list_del(&p->list);
692 spin_unlock(&part_parser_lock);
693 return 0;
695 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
697 int parse_mtd_partitions(struct mtd_info *master, const char **types,
698 struct mtd_partition **pparts, unsigned long origin)
700 struct mtd_part_parser *parser;
701 int ret = 0;
703 for ( ; ret <= 0 && *types; types++) {
704 parser = get_partition_parser(*types);
705 if (!parser && !request_module("%s", *types))
706 parser = get_partition_parser(*types);
707 if (!parser) {
708 printk(KERN_NOTICE "%s partition parsing not available\n",
709 *types);
710 continue;
712 ret = (*parser->parse_fn)(master, pparts, origin);
713 if (ret > 0) {
714 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
715 ret, parser->name, master->name);
717 put_partition_parser(parser);
719 return ret;
721 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
723 int mtd_is_master(struct mtd_info *mtd)
725 struct mtd_part *part;
726 int nopart = 0;
728 mutex_lock(&mtd_partitions_mutex);
729 list_for_each_entry(part, &mtd_partitions, list)
730 if (&part->mtd == mtd) {
731 nopart = 1;
732 break;
734 mutex_unlock(&mtd_partitions_mutex);
736 return nopart;
738 EXPORT_SYMBOL_GPL(mtd_is_master);