mtdpart.c 25.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Simple MTD partitioning layer
 *
D
David Woodhouse 已提交
4 5 6
 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
L
Linus Torvalds 已提交
7
 *
D
David Woodhouse 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
21
 *
22
 */
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/kmod.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
32
#include <linux/err.h>
L
Linus Torvalds 已提交
33

34 35
#include "mtdcore.h"

L
Linus Torvalds 已提交
36 37
/* Our partition linked list */
static LIST_HEAD(mtd_partitions);
38
static DEFINE_MUTEX(mtd_partitions_mutex);
L
Linus Torvalds 已提交
39

40 41 42 43 44 45 46
/**
 * struct mtd_part - our partition node structure
 *
 * @mtd: struct holding partition details
 * @parent: parent mtd - flash device or another partition
 * @offset: partition offset relative to the *flash device*
 */
L
Linus Torvalds 已提交
47 48
struct mtd_part {
	struct mtd_info mtd;
49
	struct mtd_info *parent;
50
	uint64_t offset;
L
Linus Torvalds 已提交
51 52 53 54 55
	struct list_head list;
};

/*
 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
56
 * the pointer to that structure.
L
Linus Torvalds 已提交
57
 */
58 59 60 61
static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
{
	return container_of(mtd, struct mtd_part, mtd);
}
L
Linus Torvalds 已提交
62

63 64

/*
L
Linus Torvalds 已提交
65 66 67 68
 * MTD methods which simply translate the effective address and pass through
 * to the _real_ device.
 */

69 70
static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
		size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
71
{
72
	struct mtd_part *part = mtd_to_part(mtd);
73
	struct mtd_ecc_stats stats;
74 75
	int res;

76 77
	stats = part->parent->ecc_stats;
	res = part->parent->_read(part->parent, from + part->offset, len,
M
Mike Dunn 已提交
78
				  retlen, buf);
79 80
	if (unlikely(mtd_is_eccerr(res)))
		mtd->ecc_stats.failed +=
81
			part->parent->ecc_stats.failed - stats.failed;
82 83
	else
		mtd->ecc_stats.corrected +=
84
			part->parent->ecc_stats.corrected - stats.corrected;
85
	return res;
L
Linus Torvalds 已提交
86 87
}

88 89
static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
		size_t *retlen, void **virt, resource_size_t *phys)
L
Linus Torvalds 已提交
90
{
91
	struct mtd_part *part = mtd_to_part(mtd);
92

93
	return part->parent->_point(part->parent, from + part->offset, len,
M
Mike Dunn 已提交
94
				    retlen, virt, phys);
L
Linus Torvalds 已提交
95
}
96

97
static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
L
Linus Torvalds 已提交
98
{
99
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
100

101
	return part->parent->_unpoint(part->parent, from + part->offset, len);
L
Linus Torvalds 已提交
102 103
}

104 105 106 107 108
static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
					    unsigned long len,
					    unsigned long offset,
					    unsigned long flags)
{
109
	struct mtd_part *part = mtd_to_part(mtd);
110 111

	offset += part->offset;
112
	return part->parent->_get_unmapped_area(part->parent, len, offset,
M
Mike Dunn 已提交
113
						flags);
114 115
}

116
static int part_read_oob(struct mtd_info *mtd, loff_t from,
117
		struct mtd_oob_ops *ops)
L
Linus Torvalds 已提交
118
{
119
	struct mtd_part *part = mtd_to_part(mtd);
120
	int res;
121

L
Linus Torvalds 已提交
122
	if (from >= mtd->size)
123
		return -EINVAL;
124
	if (ops->datbuf && from + ops->len > mtd->size)
125
		return -EINVAL;
126

127 128 129 130 131 132 133
	/*
	 * If OOB is also requested, make sure that we do not read past the end
	 * of this partition.
	 */
	if (ops->oobbuf) {
		size_t len, pages;

134
		len = mtd_oobavail(mtd, ops);
135 136 137 138 139 140
		pages = mtd_div_by_ws(mtd->size, mtd);
		pages -= mtd_div_by_ws(from, mtd);
		if (ops->ooboffs + ops->ooblen > pages * len)
			return -EINVAL;
	}

141
	res = part->parent->_read_oob(part->parent, from + part->offset, ops);
142
	if (unlikely(res)) {
143
		if (mtd_is_bitflip(res))
144
			mtd->ecc_stats.corrected++;
145
		if (mtd_is_eccerr(res))
146 147 148
			mtd->ecc_stats.failed++;
	}
	return res;
L
Linus Torvalds 已提交
149 150
}

151 152
static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len, size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
153
{
154
	struct mtd_part *part = mtd_to_part(mtd);
155
	return part->parent->_read_user_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
156
						 retlen, buf);
L
Linus Torvalds 已提交
157 158
}

159 160
static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
161
{
162
	struct mtd_part *part = mtd_to_part(mtd);
163
	return part->parent->_get_user_prot_info(part->parent, len, retlen,
164
						 buf);
165 166
}

167 168
static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len, size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
169
{
170
	struct mtd_part *part = mtd_to_part(mtd);
171
	return part->parent->_read_fact_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
172
						 retlen, buf);
L
Linus Torvalds 已提交
173 174
}

175 176
static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
177
{
178
	struct mtd_part *part = mtd_to_part(mtd);
179
	return part->parent->_get_fact_prot_info(part->parent, len, retlen,
180
						 buf);
181 182
}

183 184
static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
L
Linus Torvalds 已提交
185
{
186
	struct mtd_part *part = mtd_to_part(mtd);
187
	return part->parent->_write(part->parent, to + part->offset, len,
M
Mike Dunn 已提交
188
				    retlen, buf);
L
Linus Torvalds 已提交
189 190
}

191 192
static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
193
{
194
	struct mtd_part *part = mtd_to_part(mtd);
195
	return part->parent->_panic_write(part->parent, to + part->offset, len,
M
Mike Dunn 已提交
196
					  retlen, buf);
197 198
}

199
static int part_write_oob(struct mtd_info *mtd, loff_t to,
200
		struct mtd_oob_ops *ops)
L
Linus Torvalds 已提交
201
{
202
	struct mtd_part *part = mtd_to_part(mtd);
203

L
Linus Torvalds 已提交
204
	if (to >= mtd->size)
205
		return -EINVAL;
206
	if (ops->datbuf && to + ops->len > mtd->size)
207
		return -EINVAL;
208
	return part->parent->_write_oob(part->parent, to + part->offset, ops);
L
Linus Torvalds 已提交
209 210
}

211 212
static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len, size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
213
{
214
	struct mtd_part *part = mtd_to_part(mtd);
215
	return part->parent->_write_user_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
216
						  retlen, buf);
L
Linus Torvalds 已提交
217 218
}

219 220
static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len)
221
{
222
	struct mtd_part *part = mtd_to_part(mtd);
223
	return part->parent->_lock_user_prot_reg(part->parent, from, len);
224 225
}

226 227
static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
		unsigned long count, loff_t to, size_t *retlen)
L
Linus Torvalds 已提交
228
{
229
	struct mtd_part *part = mtd_to_part(mtd);
230
	return part->parent->_writev(part->parent, vecs, count,
M
Mike Dunn 已提交
231
				     to + part->offset, retlen);
L
Linus Torvalds 已提交
232 233
}

234
static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
L
Linus Torvalds 已提交
235
{
236
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
237
	int ret;
238

L
Linus Torvalds 已提交
239
	instr->addr += part->offset;
240
	ret = part->parent->_erase(part->parent, instr);
241
	if (ret) {
242
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
243 244 245
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
L
Linus Torvalds 已提交
246 247 248 249 250
	return ret;
}

void mtd_erase_callback(struct erase_info *instr)
{
251
	if (instr->mtd->_erase == part_erase) {
252
		struct mtd_part *part = mtd_to_part(instr->mtd);
L
Linus Torvalds 已提交
253

254
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
L
Linus Torvalds 已提交
255 256 257 258 259 260 261 262
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
	if (instr->callback)
		instr->callback(instr);
}
EXPORT_SYMBOL_GPL(mtd_erase_callback);

263
static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
264
{
265
	struct mtd_part *part = mtd_to_part(mtd);
266
	return part->parent->_lock(part->parent, ofs + part->offset, len);
L
Linus Torvalds 已提交
267 268
}

269
static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
270
{
271
	struct mtd_part *part = mtd_to_part(mtd);
272
	return part->parent->_unlock(part->parent, ofs + part->offset, len);
L
Linus Torvalds 已提交
273 274
}

275 276
static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
{
277
	struct mtd_part *part = mtd_to_part(mtd);
278
	return part->parent->_is_locked(part->parent, ofs + part->offset, len);
279 280
}

L
Linus Torvalds 已提交
281 282
static void part_sync(struct mtd_info *mtd)
{
283
	struct mtd_part *part = mtd_to_part(mtd);
284
	part->parent->_sync(part->parent);
L
Linus Torvalds 已提交
285 286 287 288
}

static int part_suspend(struct mtd_info *mtd)
{
289
	struct mtd_part *part = mtd_to_part(mtd);
290
	return part->parent->_suspend(part->parent);
L
Linus Torvalds 已提交
291 292 293 294
}

static void part_resume(struct mtd_info *mtd)
{
295
	struct mtd_part *part = mtd_to_part(mtd);
296
	part->parent->_resume(part->parent);
L
Linus Torvalds 已提交
297 298
}

299 300
static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
{
301
	struct mtd_part *part = mtd_to_part(mtd);
302
	ofs += part->offset;
303
	return part->parent->_block_isreserved(part->parent, ofs);
304 305
}

306
static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
307
{
308
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
309
	ofs += part->offset;
310
	return part->parent->_block_isbad(part->parent, ofs);
L
Linus Torvalds 已提交
311 312
}

313
static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
314
{
315
	struct mtd_part *part = mtd_to_part(mtd);
316 317
	int res;

L
Linus Torvalds 已提交
318
	ofs += part->offset;
319
	res = part->parent->_block_markbad(part->parent, ofs);
320 321 322
	if (!res)
		mtd->ecc_stats.badblocks++;
	return res;
L
Linus Torvalds 已提交
323 324
}

325 326 327
static int part_get_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
328
	return part->parent->_get_device(part->parent);
329 330 331 332 333
}

static void part_put_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
334
	part->parent->_put_device(part->parent);
335 336
}

337 338 339 340 341
static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
			      struct mtd_oob_region *oobregion)
{
	struct mtd_part *part = mtd_to_part(mtd);

342
	return mtd_ooblayout_ecc(part->parent, section, oobregion);
343 344 345 346 347 348 349
}

static int part_ooblayout_free(struct mtd_info *mtd, int section,
			       struct mtd_oob_region *oobregion)
{
	struct mtd_part *part = mtd_to_part(mtd);

350
	return mtd_ooblayout_free(part->parent, section, oobregion);
351 352 353 354 355 356 357
}

static const struct mtd_ooblayout_ops part_ooblayout_ops = {
	.ecc = part_ooblayout_ecc,
	.free = part_ooblayout_free,
};

358 359 360 361
static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
{
	struct mtd_part *part = mtd_to_part(mtd);

362
	return part->parent->_max_bad_blocks(part->parent,
363 364 365
					     ofs + part->offset, len);
}

366 367 368 369 370 371
static inline void free_partition(struct mtd_part *p)
{
	kfree(p->mtd.name);
	kfree(p);
}

372
static struct mtd_part *allocate_partition(struct mtd_info *parent,
373 374
			const struct mtd_partition *part, int partno,
			uint64_t cur_offset)
375
{
376 377
	int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize:
							    parent->erasesize;
378
	struct mtd_part *slave;
379
	u32 remainder;
380
	char *name;
381
	u64 tmp;
382 383

	/* allocate the partition structure */
384
	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
385 386
	name = kstrdup(part->name, GFP_KERNEL);
	if (!name || !slave) {
387
		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
388
		       parent->name);
389 390 391
		kfree(name);
		kfree(slave);
		return ERR_PTR(-ENOMEM);
392 393 394
	}

	/* set up the MTD object for this partition */
395 396
	slave->mtd.type = parent->type;
	slave->mtd.flags = parent->flags & ~part->mask_flags;
397
	slave->mtd.size = part->size;
398 399 400 401 402 403
	slave->mtd.writesize = parent->writesize;
	slave->mtd.writebufsize = parent->writebufsize;
	slave->mtd.oobsize = parent->oobsize;
	slave->mtd.oobavail = parent->oobavail;
	slave->mtd.subpage_sft = parent->subpage_sft;
	slave->mtd.pairing = parent->pairing;
404

405
	slave->mtd.name = name;
406
	slave->mtd.owner = parent->owner;
407

408 409 410 411 412 413 414
	/* NOTE: Historically, we didn't arrange MTDs as a tree out of
	 * concern for showing the same data in multiple partitions.
	 * However, it is very useful to have the master node present,
	 * so the MTD_PARTITIONED_MASTER option allows that. The master
	 * will have device nodes etc only if this is set, so make the
	 * parent conditional on that option. Note, this is a way to
	 * distinguish between the master and the partition in sysfs.
D
David Brownell 已提交
415
	 */
416
	slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
417 418
				&parent->dev :
				parent->dev.parent;
419
	slave->mtd.dev.of_node = part->of_node;
D
David Brownell 已提交
420

421 422
	slave->mtd._read = part_read;
	slave->mtd._write = part_write;
423

424
	if (parent->_panic_write)
425
		slave->mtd._panic_write = part_panic_write;
426

427
	if (parent->_point && parent->_unpoint) {
428 429
		slave->mtd._point = part_point;
		slave->mtd._unpoint = part_unpoint;
430 431
	}

432
	if (parent->_get_unmapped_area)
433
		slave->mtd._get_unmapped_area = part_get_unmapped_area;
434
	if (parent->_read_oob)
435
		slave->mtd._read_oob = part_read_oob;
436
	if (parent->_write_oob)
437
		slave->mtd._write_oob = part_write_oob;
438
	if (parent->_read_user_prot_reg)
439
		slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
440
	if (parent->_read_fact_prot_reg)
441
		slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
442
	if (parent->_write_user_prot_reg)
443
		slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
444
	if (parent->_lock_user_prot_reg)
445
		slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
446
	if (parent->_get_user_prot_info)
447
		slave->mtd._get_user_prot_info = part_get_user_prot_info;
448
	if (parent->_get_fact_prot_info)
449
		slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
450
	if (parent->_sync)
451
		slave->mtd._sync = part_sync;
452 453
	if (!partno && !parent->dev.class && parent->_suspend &&
	    parent->_resume) {
454 455
			slave->mtd._suspend = part_suspend;
			slave->mtd._resume = part_resume;
456
	}
457
	if (parent->_writev)
458
		slave->mtd._writev = part_writev;
459
	if (parent->_lock)
460
		slave->mtd._lock = part_lock;
461
	if (parent->_unlock)
462
		slave->mtd._unlock = part_unlock;
463
	if (parent->_is_locked)
464
		slave->mtd._is_locked = part_is_locked;
465
	if (parent->_block_isreserved)
466
		slave->mtd._block_isreserved = part_block_isreserved;
467
	if (parent->_block_isbad)
468
		slave->mtd._block_isbad = part_block_isbad;
469
	if (parent->_block_markbad)
470
		slave->mtd._block_markbad = part_block_markbad;
471
	if (parent->_max_bad_blocks)
472
		slave->mtd._max_bad_blocks = part_max_bad_blocks;
473

474
	if (parent->_get_device)
475
		slave->mtd._get_device = part_get_device;
476
	if (parent->_put_device)
477 478
		slave->mtd._put_device = part_put_device;

479
	slave->mtd._erase = part_erase;
480
	slave->parent = parent;
481 482 483 484 485
	slave->offset = part->offset;

	if (slave->offset == MTDPART_OFS_APPEND)
		slave->offset = cur_offset;
	if (slave->offset == MTDPART_OFS_NXTBLK) {
486
		tmp = cur_offset;
487
		slave->offset = cur_offset;
488 489 490
		remainder = do_div(tmp, wr_alignment);
		if (remainder) {
			slave->offset += wr_alignment - remainder;
491
			printk(KERN_NOTICE "Moving partition %d: "
492 493
			       "0x%012llx -> 0x%012llx\n", partno,
			       (unsigned long long)cur_offset, (unsigned long long)slave->offset);
494 495
		}
	}
496 497
	if (slave->offset == MTDPART_OFS_RETAIN) {
		slave->offset = cur_offset;
498 499
		if (parent->size - slave->offset >= slave->mtd.size) {
			slave->mtd.size = parent->size - slave->offset
500 501 502
							- slave->mtd.size;
		} else {
			printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
503
				part->name, parent->size - slave->offset,
504 505 506 507 508
				slave->mtd.size);
			/* register to preserve ordering */
			goto out_register;
		}
	}
509
	if (slave->mtd.size == MTDPART_SIZ_FULL)
510
		slave->mtd.size = parent->size - slave->offset;
511

512 513
	printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
		(unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
514 515

	/* let's do some sanity checks */
516
	if (slave->offset >= parent->size) {
517
		/* let's register it anyway to preserve ordering */
518 519
		slave->offset = 0;
		slave->mtd.size = 0;
520
		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
521
			part->name);
522
		goto out_register;
523
	}
524 525
	if (slave->offset + slave->mtd.size > parent->size) {
		slave->mtd.size = parent->size - slave->offset;
526
		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
527
			part->name, parent->name, (unsigned long long)slave->mtd.size);
528
	}
529
	if (parent->numeraseregions > 1) {
530
		/* Deal with variable erase size stuff */
531
		int i, max = parent->numeraseregions;
532
		u64 end = slave->offset + slave->mtd.size;
533
		struct mtd_erase_region_info *regions = parent->eraseregions;
534

535 536 537
		/* Find the first erase regions which is part of this
		 * partition. */
		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
538
			;
539
		/* The loop searched for the region _behind_ the first one */
540 541
		if (i > 0)
			i--;
542

543 544
		/* Pick biggest erasesize */
		for (; i < max && regions[i].offset < end; i++) {
545 546 547 548
			if (slave->mtd.erasesize < regions[i].erasesize) {
				slave->mtd.erasesize = regions[i].erasesize;
			}
		}
549
		BUG_ON(slave->mtd.erasesize == 0);
550 551
	} else {
		/* Single erase size */
552
		slave->mtd.erasesize = parent->erasesize;
553 554
	}

555 556 557
	tmp = slave->offset;
	remainder = do_div(tmp, wr_alignment);
	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
558
		/* Doesn't start on a boundary of major erase size */
559 560
		/* FIXME: Let it be writable if it is on a boundary of
		 * _minor_ erase size though */
561
		slave->mtd.flags &= ~MTD_WRITEABLE;
562
		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
563 564
			part->name);
	}
565 566 567 568

	tmp = slave->mtd.size;
	remainder = do_div(tmp, wr_alignment);
	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
569
		slave->mtd.flags &= ~MTD_WRITEABLE;
570
		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
571 572 573
			part->name);
	}

574
	mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
575 576 577
	slave->mtd.ecc_step_size = parent->ecc_step_size;
	slave->mtd.ecc_strength = parent->ecc_strength;
	slave->mtd.bitflip_threshold = parent->bitflip_threshold;
578

579
	if (parent->_block_isbad) {
580
		uint64_t offs = 0;
581

582
		while (offs < slave->mtd.size) {
583
			if (mtd_block_isreserved(parent, offs + slave->offset))
584
				slave->mtd.ecc_stats.bbtblocks++;
585
			else if (mtd_block_isbad(parent, offs + slave->offset))
586 587 588 589 590
				slave->mtd.ecc_stats.badblocks++;
			offs += slave->mtd.erasesize;
		}
	}

591
out_register:
592 593 594
	return slave;
}

595 596 597 598
static ssize_t mtd_partition_offset_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct mtd_info *mtd = dev_get_drvdata(dev);
599
	struct mtd_part *part = mtd_to_part(mtd);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
}

static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);

static const struct attribute *mtd_partition_attrs[] = {
	&dev_attr_offset.attr,
	NULL
};

static int mtd_add_partition_attrs(struct mtd_part *new)
{
	int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
	if (ret)
		printk(KERN_WARNING
		       "mtd: failed to create partition attrs, err=%d\n", ret);
	return ret;
}

619
int mtd_add_partition(struct mtd_info *parent, const char *name,
620 621 622
		      long long offset, long long length)
{
	struct mtd_partition part;
623
	struct mtd_part *new;
624 625 626 627 628 629 630 631
	int ret = 0;

	/* the direct offset is expected */
	if (offset == MTDPART_OFS_APPEND ||
	    offset == MTDPART_OFS_NXTBLK)
		return -EINVAL;

	if (length == MTDPART_SIZ_FULL)
632
		length = parent->size - offset;
633 634 635 636

	if (length <= 0)
		return -EINVAL;

637
	memset(&part, 0, sizeof(part));
638 639 640 641
	part.name = name;
	part.size = length;
	part.offset = offset;

642
	new = allocate_partition(parent, &part, -1, offset);
643 644 645 646 647 648 649 650 651
	if (IS_ERR(new))
		return PTR_ERR(new);

	mutex_lock(&mtd_partitions_mutex);
	list_add(&new->list, &mtd_partitions);
	mutex_unlock(&mtd_partitions_mutex);

	add_mtd_device(&new->mtd);

652 653
	mtd_add_partition_attrs(new);

654 655 656 657
	return ret;
}
EXPORT_SYMBOL_GPL(mtd_add_partition);

658 659 660 661 662 663 664 665 666
/**
 * __mtd_del_partition - delete MTD partition
 *
 * @priv: internal MTD struct for partition to be deleted
 *
 * This function must be called with the partitions mutex locked.
 */
static int __mtd_del_partition(struct mtd_part *priv)
{
667
	struct mtd_part *child, *next;
668 669
	int err;

670 671 672 673 674 675 676 677
	list_for_each_entry_safe(child, next, &mtd_partitions, list) {
		if (child->parent == &priv->mtd) {
			err = __mtd_del_partition(child);
			if (err)
				return err;
		}
	}

678 679
	sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);

680 681 682 683 684 685 686 687 688 689 690 691
	err = del_mtd_device(&priv->mtd);
	if (err)
		return err;

	list_del(&priv->list);
	free_partition(priv);

	return 0;
}

/*
 * This function unregisters and destroy all slave MTD objects which are
692
 * attached to the given MTD object.
693
 */
694
int del_mtd_partitions(struct mtd_info *mtd)
695 696 697 698 699 700
{
	struct mtd_part *slave, *next;
	int ret, err = 0;

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
701
		if (slave->parent == mtd) {
702 703 704 705 706 707 708 709 710
			ret = __mtd_del_partition(slave);
			if (ret < 0)
				err = ret;
		}
	mutex_unlock(&mtd_partitions_mutex);

	return err;
}

711
int mtd_del_partition(struct mtd_info *mtd, int partno)
712 713 714 715 716 717
{
	struct mtd_part *slave, *next;
	int ret = -EINVAL;

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
718
		if ((slave->parent == mtd) &&
719
		    (slave->mtd.index == partno)) {
720
			ret = __mtd_del_partition(slave);
721 722 723 724 725 726 727 728
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(mtd_del_partition);

L
Linus Torvalds 已提交
729 730 731 732
/*
 * This function, given a master MTD object and a partition table, creates
 * and registers slave MTD objects which are bound to the master according to
 * the partition definitions.
D
David Brownell 已提交
733
 *
734 735
 * For historical reasons, this function's caller only registers the master
 * if the MTD_PARTITIONED_MASTER config option is set.
L
Linus Torvalds 已提交
736 737
 */

738
int add_mtd_partitions(struct mtd_info *master,
L
Linus Torvalds 已提交
739 740 741 742
		       const struct mtd_partition *parts,
		       int nbparts)
{
	struct mtd_part *slave;
743
	uint64_t cur_offset = 0;
L
Linus Torvalds 已提交
744 745
	int i;

746
	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
L
Linus Torvalds 已提交
747 748

	for (i = 0; i < nbparts; i++) {
749
		slave = allocate_partition(master, parts + i, i, cur_offset);
750 751
		if (IS_ERR(slave)) {
			del_mtd_partitions(master);
752
			return PTR_ERR(slave);
753
		}
754 755 756 757 758 759

		mutex_lock(&mtd_partitions_mutex);
		list_add(&slave->list, &mtd_partitions);
		mutex_unlock(&mtd_partitions_mutex);

		add_mtd_device(&slave->mtd);
760
		mtd_add_partition_attrs(slave);
761

L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770
		cur_offset = slave->offset + slave->mtd.size;
	}

	return 0;
}

static DEFINE_SPINLOCK(part_parser_lock);
static LIST_HEAD(part_parsers);

771
static struct mtd_part_parser *mtd_part_parser_get(const char *name)
L
Linus Torvalds 已提交
772
{
773
	struct mtd_part_parser *p, *ret = NULL;
L
Linus Torvalds 已提交
774

775
	spin_lock(&part_parser_lock);
L
Linus Torvalds 已提交
776

777
	list_for_each_entry(p, &part_parsers, list)
L
Linus Torvalds 已提交
778 779 780 781
		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
			ret = p;
			break;
		}
782

L
Linus Torvalds 已提交
783 784 785 786 787
	spin_unlock(&part_parser_lock);

	return ret;
}

788 789 790 791
static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
{
	module_put(p->owner);
}
792

793 794 795 796 797 798 799 800 801 802
/*
 * Many partition parsers just expected the core to kfree() all their data in
 * one chunk. Do that by default.
 */
static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
					    int nr_parts)
{
	kfree(pparts);
}

803
int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
L
Linus Torvalds 已提交
804
{
805 806
	p->owner = owner;

807 808 809
	if (!p->cleanup)
		p->cleanup = &mtd_part_parser_cleanup_default;

L
Linus Torvalds 已提交
810 811 812
	spin_lock(&part_parser_lock);
	list_add(&p->list, &part_parsers);
	spin_unlock(&part_parser_lock);
813 814

	return 0;
L
Linus Torvalds 已提交
815
}
816
EXPORT_SYMBOL_GPL(__register_mtd_parser);
L
Linus Torvalds 已提交
817

818
void deregister_mtd_parser(struct mtd_part_parser *p)
L
Linus Torvalds 已提交
819 820 821 822 823
{
	spin_lock(&part_parser_lock);
	list_del(&p->list);
	spin_unlock(&part_parser_lock);
}
824
EXPORT_SYMBOL_GPL(deregister_mtd_parser);
L
Linus Torvalds 已提交
825

826 827 828 829
/*
 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
 * are changing this array!
 */
830
static const char * const default_mtd_part_types[] = {
831 832 833 834
	"cmdlinepart",
	"ofpart",
	NULL
};
835

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
static int mtd_part_do_parse(struct mtd_part_parser *parser,
			     struct mtd_info *master,
			     struct mtd_partitions *pparts,
			     struct mtd_part_parser_data *data)
{
	int ret;

	ret = (*parser->parse_fn)(master, &pparts->parts, data);
	pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
	if (ret <= 0)
		return ret;

	pr_notice("%d %s partitions found on MTD device %s\n", ret,
		  parser->name, master->name);

	pparts->nr_parts = ret;
	pparts->parser = parser;

	return ret;
}

857 858 859 860
/**
 * parse_mtd_partitions - parse MTD partitions
 * @master: the master partition (describes whole MTD device)
 * @types: names of partition parsers to try or %NULL
861
 * @pparts: info about partitions found is returned here
862
 * @data: MTD partition parser-specific data
863 864 865 866
 *
 * This function tries to find partition on MTD device @master. It uses MTD
 * partition parsers, specified in @types. However, if @types is %NULL, then
 * the default list of parsers is used. The default list contains only the
867
 * "cmdlinepart" and "ofpart" parsers ATM.
868 869
 * Note: If there are more then one parser in @types, the kernel only takes the
 * partitions parsed out by the first parser.
870 871 872
 *
 * This function may return:
 * o a negative error code in case of failure
873
 * o zero otherwise, and @pparts will describe the partitions, number of
874 875 876
 *   partitions, and the parser which parsed them. Caller must release
 *   resources with mtd_part_parser_cleanup() when finished with the returned
 *   data.
877
 */
878
int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
879
			 struct mtd_partitions *pparts,
880
			 struct mtd_part_parser_data *data)
L
Linus Torvalds 已提交
881 882
{
	struct mtd_part_parser *parser;
883
	int ret, err = 0;
884

885 886 887
	if (!types)
		types = default_mtd_part_types;

888
	for ( ; *types; types++) {
889
		pr_debug("%s: parsing partitions %s\n", master->name, *types);
890
		parser = mtd_part_parser_get(*types);
L
Linus Torvalds 已提交
891
		if (!parser && !request_module("%s", *types))
892
			parser = mtd_part_parser_get(*types);
893 894
		pr_debug("%s: got parser %s\n", master->name,
			 parser ? parser->name : NULL);
895
		if (!parser)
L
Linus Torvalds 已提交
896
			continue;
897 898 899
		ret = mtd_part_do_parse(parser, master, pparts, data);
		/* Found partitions! */
		if (ret > 0)
900
			return 0;
901
		mtd_part_parser_put(parser);
902 903 904 905 906 907
		/*
		 * Stash the first error we see; only report it if no parser
		 * succeeds
		 */
		if (ret < 0 && !err)
			err = ret;
L
Linus Torvalds 已提交
908
	}
909
	return err;
L
Linus Torvalds 已提交
910
}
911

912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
void mtd_part_parser_cleanup(struct mtd_partitions *parts)
{
	const struct mtd_part_parser *parser;

	if (!parts)
		return;

	parser = parts->parser;
	if (parser) {
		if (parser->cleanup)
			parser->cleanup(parts->parts, parts->nr_parts);

		mtd_part_parser_put(parser);
	}
}

928
int mtd_is_partition(const struct mtd_info *mtd)
929 930
{
	struct mtd_part *part;
931
	int ispart = 0;
932 933 934 935

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry(part, &mtd_partitions, list)
		if (&part->mtd == mtd) {
936
			ispart = 1;
937 938 939 940
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

941
	return ispart;
942
}
943
EXPORT_SYMBOL_GPL(mtd_is_partition);
944 945 946 947 948 949 950

/* Returns the size of the entire flash chip */
uint64_t mtd_get_device_size(const struct mtd_info *mtd)
{
	if (!mtd_is_partition(mtd))
		return mtd->size;

951
	return mtd_get_device_size(mtd_to_part(mtd)->parent);
952 953
}
EXPORT_SYMBOL_GPL(mtd_get_device_size);