xref: /openbmc/u-boot/drivers/mtd/ubi/vmt.c (revision c6af2e7d)
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation;  either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Artem Bityutskiy (Битюцкий Артём)
19  */
20 
21 /*
22  * This file contains implementation of volume creation, deletion, updating and
23  * resizing.
24  */
25 
26 #ifdef UBI_LINUX
27 #include <linux/err.h>
28 #include <asm/div64.h>
29 #endif
30 
31 #include <ubi_uboot.h>
32 #include "ubi.h"
33 
34 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
35 static void paranoid_check_volumes(struct ubi_device *ubi);
36 #else
37 #define paranoid_check_volumes(ubi)
38 #endif
39 
40 #ifdef UBI_LINUX
41 static ssize_t vol_attribute_show(struct device *dev,
42 				  struct device_attribute *attr, char *buf);
43 
44 /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
45 static struct device_attribute attr_vol_reserved_ebs =
46 	__ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
47 static struct device_attribute attr_vol_type =
48 	__ATTR(type, S_IRUGO, vol_attribute_show, NULL);
49 static struct device_attribute attr_vol_name =
50 	__ATTR(name, S_IRUGO, vol_attribute_show, NULL);
51 static struct device_attribute attr_vol_corrupted =
52 	__ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
53 static struct device_attribute attr_vol_alignment =
54 	__ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
55 static struct device_attribute attr_vol_usable_eb_size =
56 	__ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
57 static struct device_attribute attr_vol_data_bytes =
58 	__ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
59 static struct device_attribute attr_vol_upd_marker =
60 	__ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);
61 
62 /*
63  * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
64  *
65  * Consider a situation:
66  * A. process 1 opens a sysfs file related to volume Y, say
67  *    /<sysfs>/class/ubi/ubiX_Y/reserved_ebs;
68  * B. process 2 removes volume Y;
69  * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
70  *
71  * In this situation, this function will return %-ENODEV because it will find
72  * out that the volume was removed from the @ubi->volumes array.
73  */
74 static ssize_t vol_attribute_show(struct device *dev,
75 				  struct device_attribute *attr, char *buf)
76 {
77 	int ret;
78 	struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
79 	struct ubi_device *ubi;
80 
81 	ubi = ubi_get_device(vol->ubi->ubi_num);
82 	if (!ubi)
83 		return -ENODEV;
84 
85 	spin_lock(&ubi->volumes_lock);
86 	if (!ubi->volumes[vol->vol_id]) {
87 		spin_unlock(&ubi->volumes_lock);
88 		ubi_put_device(ubi);
89 		return -ENODEV;
90 	}
91 	/* Take a reference to prevent volume removal */
92 	vol->ref_count += 1;
93 	spin_unlock(&ubi->volumes_lock);
94 
95 	if (attr == &attr_vol_reserved_ebs)
96 		ret = sprintf(buf, "%d\n", vol->reserved_pebs);
97 	else if (attr == &attr_vol_type) {
98 		const char *tp;
99 
100 		if (vol->vol_type == UBI_DYNAMIC_VOLUME)
101 			tp = "dynamic";
102 		else
103 			tp = "static";
104 		ret = sprintf(buf, "%s\n", tp);
105 	} else if (attr == &attr_vol_name)
106 		ret = sprintf(buf, "%s\n", vol->name);
107 	else if (attr == &attr_vol_corrupted)
108 		ret = sprintf(buf, "%d\n", vol->corrupted);
109 	else if (attr == &attr_vol_alignment)
110 		ret = sprintf(buf, "%d\n", vol->alignment);
111 	else if (attr == &attr_vol_usable_eb_size)
112 		ret = sprintf(buf, "%d\n", vol->usable_leb_size);
113 	else if (attr == &attr_vol_data_bytes)
114 		ret = sprintf(buf, "%lld\n", vol->used_bytes);
115 	else if (attr == &attr_vol_upd_marker)
116 		ret = sprintf(buf, "%d\n", vol->upd_marker);
117 	else
118 		/* This must be a bug */
119 		ret = -EINVAL;
120 
121 	/* We've done the operation, drop volume and UBI device references */
122 	spin_lock(&ubi->volumes_lock);
123 	vol->ref_count -= 1;
124 	ubi_assert(vol->ref_count >= 0);
125 	spin_unlock(&ubi->volumes_lock);
126 	ubi_put_device(ubi);
127 	return ret;
128 }
129 #endif
130 
131 /* Release method for volume devices */
132 static void vol_release(struct device *dev)
133 {
134 	struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
135 
136 	kfree(vol);
137 }
138 
139 #ifdef UBI_LINUX
140 /**
141  * volume_sysfs_init - initialize sysfs for new volume.
142  * @ubi: UBI device description object
143  * @vol: volume description object
144  *
145  * This function returns zero in case of success and a negative error code in
146  * case of failure.
147  *
148  * Note, this function does not free allocated resources in case of failure -
149  * the caller does it. This is because this would cause release() here and the
150  * caller would oops.
151  */
152 static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol)
153 {
154 	int err;
155 
156 	err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);
157 	if (err)
158 		return err;
159 	err = device_create_file(&vol->dev, &attr_vol_type);
160 	if (err)
161 		return err;
162 	err = device_create_file(&vol->dev, &attr_vol_name);
163 	if (err)
164 		return err;
165 	err = device_create_file(&vol->dev, &attr_vol_corrupted);
166 	if (err)
167 		return err;
168 	err = device_create_file(&vol->dev, &attr_vol_alignment);
169 	if (err)
170 		return err;
171 	err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);
172 	if (err)
173 		return err;
174 	err = device_create_file(&vol->dev, &attr_vol_data_bytes);
175 	if (err)
176 		return err;
177 	err = device_create_file(&vol->dev, &attr_vol_upd_marker);
178 	return err;
179 }
180 
181 /**
182  * volume_sysfs_close - close sysfs for a volume.
183  * @vol: volume description object
184  */
185 static void volume_sysfs_close(struct ubi_volume *vol)
186 {
187 	device_remove_file(&vol->dev, &attr_vol_upd_marker);
188 	device_remove_file(&vol->dev, &attr_vol_data_bytes);
189 	device_remove_file(&vol->dev, &attr_vol_usable_eb_size);
190 	device_remove_file(&vol->dev, &attr_vol_alignment);
191 	device_remove_file(&vol->dev, &attr_vol_corrupted);
192 	device_remove_file(&vol->dev, &attr_vol_name);
193 	device_remove_file(&vol->dev, &attr_vol_type);
194 	device_remove_file(&vol->dev, &attr_vol_reserved_ebs);
195 	device_unregister(&vol->dev);
196 }
197 #endif
198 
199 /**
200  * ubi_create_volume - create volume.
201  * @ubi: UBI device description object
202  * @req: volume creation request
203  *
204  * This function creates volume described by @req. If @req->vol_id id
205  * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
206  * and saves it in @req->vol_id. Returns zero in case of success and a negative
207  * error code in case of failure. Note, the caller has to have the
208  * @ubi->volumes_mutex locked.
209  */
210 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
211 {
212 	int i, err, vol_id = req->vol_id, dont_free = 0;
213 	struct ubi_volume *vol;
214 	struct ubi_vtbl_record vtbl_rec;
215 	uint64_t bytes;
216 	dev_t dev;
217 
218 	if (ubi->ro_mode)
219 		return -EROFS;
220 
221 	vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
222 	if (!vol)
223 		return -ENOMEM;
224 
225 	spin_lock(&ubi->volumes_lock);
226 	if (vol_id == UBI_VOL_NUM_AUTO) {
227 		/* Find unused volume ID */
228 		dbg_msg("search for vacant volume ID");
229 		for (i = 0; i < ubi->vtbl_slots; i++)
230 			if (!ubi->volumes[i]) {
231 				vol_id = i;
232 				break;
233 			}
234 
235 		if (vol_id == UBI_VOL_NUM_AUTO) {
236 			dbg_err("out of volume IDs");
237 			err = -ENFILE;
238 			goto out_unlock;
239 		}
240 		req->vol_id = vol_id;
241 	}
242 
243 	dbg_msg("volume ID %d, %llu bytes, type %d, name %s",
244 		vol_id, (unsigned long long)req->bytes,
245 		(int)req->vol_type, req->name);
246 
247 	/* Ensure that this volume does not exist */
248 	err = -EEXIST;
249 	if (ubi->volumes[vol_id]) {
250 		dbg_err("volume %d already exists", vol_id);
251 		goto out_unlock;
252 	}
253 
254 	/* Ensure that the name is unique */
255 	for (i = 0; i < ubi->vtbl_slots; i++)
256 		if (ubi->volumes[i] &&
257 		    ubi->volumes[i]->name_len == req->name_len &&
258 		    !strcmp(ubi->volumes[i]->name, req->name)) {
259 			dbg_err("volume \"%s\" exists (ID %d)", req->name, i);
260 			goto out_unlock;
261 		}
262 
263 	/* Calculate how many eraseblocks are requested */
264 	vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
265 	bytes = req->bytes;
266 	if (do_div(bytes, vol->usable_leb_size))
267 		vol->reserved_pebs = 1;
268 	vol->reserved_pebs += bytes;
269 
270 	/* Reserve physical eraseblocks */
271 	if (vol->reserved_pebs > ubi->avail_pebs) {
272 		dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);
273 		err = -ENOSPC;
274 		goto out_unlock;
275 	}
276 	ubi->avail_pebs -= vol->reserved_pebs;
277 	ubi->rsvd_pebs += vol->reserved_pebs;
278 	spin_unlock(&ubi->volumes_lock);
279 
280 	vol->vol_id    = vol_id;
281 	vol->alignment = req->alignment;
282 	vol->data_pad  = ubi->leb_size % vol->alignment;
283 	vol->vol_type  = req->vol_type;
284 	vol->name_len  = req->name_len;
285 	memcpy(vol->name, req->name, vol->name_len + 1);
286 	vol->ubi = ubi;
287 
288 	/*
289 	 * Finish all pending erases because there may be some LEBs belonging
290 	 * to the same volume ID.
291 	 */
292 	err = ubi_wl_flush(ubi);
293 	if (err)
294 		goto out_acc;
295 
296 	vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
297 	if (!vol->eba_tbl) {
298 		err = -ENOMEM;
299 		goto out_acc;
300 	}
301 
302 	for (i = 0; i < vol->reserved_pebs; i++)
303 		vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
304 
305 	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
306 		vol->used_ebs = vol->reserved_pebs;
307 		vol->last_eb_bytes = vol->usable_leb_size;
308 		vol->used_bytes =
309 			(long long)vol->used_ebs * vol->usable_leb_size;
310 	} else {
311 		bytes = vol->used_bytes;
312 		vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
313 		vol->used_ebs = bytes;
314 		if (vol->last_eb_bytes)
315 			vol->used_ebs += 1;
316 		else
317 			vol->last_eb_bytes = vol->usable_leb_size;
318 	}
319 
320 	/* Register character device for the volume */
321 	cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
322 	vol->cdev.owner = THIS_MODULE;
323 	dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
324 	err = cdev_add(&vol->cdev, dev, 1);
325 	if (err) {
326 		ubi_err("cannot add character device");
327 		goto out_mapping;
328 	}
329 
330 	err = ubi_create_gluebi(ubi, vol);
331 	if (err)
332 		goto out_cdev;
333 
334 	vol->dev.release = vol_release;
335 	vol->dev.parent = &ubi->dev;
336 	vol->dev.devt = dev;
337 	vol->dev.class = ubi_class;
338 
339 	sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
340 	err = device_register(&vol->dev);
341 	if (err) {
342 		ubi_err("cannot register device");
343 		goto out_gluebi;
344 	}
345 
346 	err = volume_sysfs_init(ubi, vol);
347 	if (err)
348 		goto out_sysfs;
349 
350 	/* Fill volume table record */
351 	memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
352 	vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
353 	vtbl_rec.alignment     = cpu_to_be32(vol->alignment);
354 	vtbl_rec.data_pad      = cpu_to_be32(vol->data_pad);
355 	vtbl_rec.name_len      = cpu_to_be16(vol->name_len);
356 	if (vol->vol_type == UBI_DYNAMIC_VOLUME)
357 		vtbl_rec.vol_type = UBI_VID_DYNAMIC;
358 	else
359 		vtbl_rec.vol_type = UBI_VID_STATIC;
360 	memcpy(vtbl_rec.name, vol->name, vol->name_len + 1);
361 
362 	err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
363 	if (err)
364 		goto out_sysfs;
365 
366 	spin_lock(&ubi->volumes_lock);
367 	ubi->volumes[vol_id] = vol;
368 	ubi->vol_count += 1;
369 	spin_unlock(&ubi->volumes_lock);
370 
371 	paranoid_check_volumes(ubi);
372 	return 0;
373 
374 out_sysfs:
375 	/*
376 	 * We have registered our device, we should not free the volume*
377 	 * description object in this function in case of an error - it is
378 	 * freed by the release function.
379 	 *
380 	 * Get device reference to prevent the release function from being
381 	 * called just after sysfs has been closed.
382 	 */
383 	dont_free = 1;
384 	get_device(&vol->dev);
385 	volume_sysfs_close(vol);
386 out_gluebi:
387 	if (ubi_destroy_gluebi(vol))
388 		dbg_err("cannot destroy gluebi for volume %d:%d",
389 			ubi->ubi_num, vol_id);
390 out_cdev:
391 	cdev_del(&vol->cdev);
392 out_mapping:
393 	kfree(vol->eba_tbl);
394 out_acc:
395 	spin_lock(&ubi->volumes_lock);
396 	ubi->rsvd_pebs -= vol->reserved_pebs;
397 	ubi->avail_pebs += vol->reserved_pebs;
398 out_unlock:
399 	spin_unlock(&ubi->volumes_lock);
400 	if (dont_free)
401 		put_device(&vol->dev);
402 	else
403 		kfree(vol);
404 	ubi_err("cannot create volume %d, error %d", vol_id, err);
405 	return err;
406 }
407 
408 /**
409  * ubi_remove_volume - remove volume.
410  * @desc: volume descriptor
411  *
412  * This function removes volume described by @desc. The volume has to be opened
413  * in "exclusive" mode. Returns zero in case of success and a negative error
414  * code in case of failure. The caller has to have the @ubi->volumes_mutex
415  * locked.
416  */
417 int ubi_remove_volume(struct ubi_volume_desc *desc)
418 {
419 	struct ubi_volume *vol = desc->vol;
420 	struct ubi_device *ubi = vol->ubi;
421 	int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
422 
423 	dbg_msg("remove UBI volume %d", vol_id);
424 	ubi_assert(desc->mode == UBI_EXCLUSIVE);
425 	ubi_assert(vol == ubi->volumes[vol_id]);
426 
427 	if (ubi->ro_mode)
428 		return -EROFS;
429 
430 	spin_lock(&ubi->volumes_lock);
431 	if (vol->ref_count > 1) {
432 		/*
433 		 * The volume is busy, probably someone is reading one of its
434 		 * sysfs files.
435 		 */
436 		err = -EBUSY;
437 		goto out_unlock;
438 	}
439 	ubi->volumes[vol_id] = NULL;
440 	spin_unlock(&ubi->volumes_lock);
441 
442 	err = ubi_destroy_gluebi(vol);
443 	if (err)
444 		goto out_err;
445 
446 	err = ubi_change_vtbl_record(ubi, vol_id, NULL);
447 	if (err)
448 		goto out_err;
449 
450 	for (i = 0; i < vol->reserved_pebs; i++) {
451 		err = ubi_eba_unmap_leb(ubi, vol, i);
452 		if (err)
453 			goto out_err;
454 	}
455 
456 	kfree(vol->eba_tbl);
457 	vol->eba_tbl = NULL;
458 	cdev_del(&vol->cdev);
459 	volume_sysfs_close(vol);
460 
461 	spin_lock(&ubi->volumes_lock);
462 	ubi->rsvd_pebs -= reserved_pebs;
463 	ubi->avail_pebs += reserved_pebs;
464 	i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
465 	if (i > 0) {
466 		i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
467 		ubi->avail_pebs -= i;
468 		ubi->rsvd_pebs += i;
469 		ubi->beb_rsvd_pebs += i;
470 		if (i > 0)
471 			ubi_msg("reserve more %d PEBs", i);
472 	}
473 	ubi->vol_count -= 1;
474 	spin_unlock(&ubi->volumes_lock);
475 
476 	paranoid_check_volumes(ubi);
477 	return 0;
478 
479 out_err:
480 	ubi_err("cannot remove volume %d, error %d", vol_id, err);
481 	spin_lock(&ubi->volumes_lock);
482 	ubi->volumes[vol_id] = vol;
483 out_unlock:
484 	spin_unlock(&ubi->volumes_lock);
485 	return err;
486 }
487 
488 /**
489  * ubi_resize_volume - re-size volume.
490  * @desc: volume descriptor
491  * @reserved_pebs: new size in physical eraseblocks
492  *
493  * This function re-sizes the volume and returns zero in case of success, and a
494  * negative error code in case of failure. The caller has to have the
495  * @ubi->volumes_mutex locked.
496  */
497 int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
498 {
499 	int i, err, pebs, *new_mapping;
500 	struct ubi_volume *vol = desc->vol;
501 	struct ubi_device *ubi = vol->ubi;
502 	struct ubi_vtbl_record vtbl_rec;
503 	int vol_id = vol->vol_id;
504 
505 	if (ubi->ro_mode)
506 		return -EROFS;
507 
508 	dbg_msg("re-size volume %d to from %d to %d PEBs",
509 		vol_id, vol->reserved_pebs, reserved_pebs);
510 
511 	if (vol->vol_type == UBI_STATIC_VOLUME &&
512 	    reserved_pebs < vol->used_ebs) {
513 		dbg_err("too small size %d, %d LEBs contain data",
514 			reserved_pebs, vol->used_ebs);
515 		return -EINVAL;
516 	}
517 
518 	/* If the size is the same, we have nothing to do */
519 	if (reserved_pebs == vol->reserved_pebs)
520 		return 0;
521 
522 	new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
523 	if (!new_mapping)
524 		return -ENOMEM;
525 
526 	for (i = 0; i < reserved_pebs; i++)
527 		new_mapping[i] = UBI_LEB_UNMAPPED;
528 
529 	spin_lock(&ubi->volumes_lock);
530 	if (vol->ref_count > 1) {
531 		spin_unlock(&ubi->volumes_lock);
532 		err = -EBUSY;
533 		goto out_free;
534 	}
535 	spin_unlock(&ubi->volumes_lock);
536 
537 	/* Reserve physical eraseblocks */
538 	pebs = reserved_pebs - vol->reserved_pebs;
539 	if (pebs > 0) {
540 		spin_lock(&ubi->volumes_lock);
541 		if (pebs > ubi->avail_pebs) {
542 			dbg_err("not enough PEBs: requested %d, available %d",
543 				pebs, ubi->avail_pebs);
544 			spin_unlock(&ubi->volumes_lock);
545 			err = -ENOSPC;
546 			goto out_free;
547 		}
548 		ubi->avail_pebs -= pebs;
549 		ubi->rsvd_pebs += pebs;
550 		for (i = 0; i < vol->reserved_pebs; i++)
551 			new_mapping[i] = vol->eba_tbl[i];
552 		kfree(vol->eba_tbl);
553 		vol->eba_tbl = new_mapping;
554 		spin_unlock(&ubi->volumes_lock);
555 	}
556 
557 	/* Change volume table record */
558 	memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
559 	vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
560 	err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
561 	if (err)
562 		goto out_acc;
563 
564 	if (pebs < 0) {
565 		for (i = 0; i < -pebs; i++) {
566 			err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
567 			if (err)
568 				goto out_acc;
569 		}
570 		spin_lock(&ubi->volumes_lock);
571 		ubi->rsvd_pebs += pebs;
572 		ubi->avail_pebs -= pebs;
573 		pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
574 		if (pebs > 0) {
575 			pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
576 			ubi->avail_pebs -= pebs;
577 			ubi->rsvd_pebs += pebs;
578 			ubi->beb_rsvd_pebs += pebs;
579 			if (pebs > 0)
580 				ubi_msg("reserve more %d PEBs", pebs);
581 		}
582 		for (i = 0; i < reserved_pebs; i++)
583 			new_mapping[i] = vol->eba_tbl[i];
584 		kfree(vol->eba_tbl);
585 		vol->eba_tbl = new_mapping;
586 		spin_unlock(&ubi->volumes_lock);
587 	}
588 
589 	vol->reserved_pebs = reserved_pebs;
590 	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
591 		vol->used_ebs = reserved_pebs;
592 		vol->last_eb_bytes = vol->usable_leb_size;
593 		vol->used_bytes =
594 			(long long)vol->used_ebs * vol->usable_leb_size;
595 	}
596 
597 	paranoid_check_volumes(ubi);
598 	return 0;
599 
600 out_acc:
601 	if (pebs > 0) {
602 		spin_lock(&ubi->volumes_lock);
603 		ubi->rsvd_pebs -= pebs;
604 		ubi->avail_pebs += pebs;
605 		spin_unlock(&ubi->volumes_lock);
606 	}
607 out_free:
608 	kfree(new_mapping);
609 	return err;
610 }
611 
612 /**
613  * ubi_add_volume - add volume.
614  * @ubi: UBI device description object
615  * @vol: volume description object
616  *
617  * This function adds an existing volume and initializes all its data
618  * structures. Returns zero in case of success and a negative error code in
619  * case of failure.
620  */
621 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
622 {
623 	int err, vol_id = vol->vol_id;
624 	dev_t dev;
625 
626 	dbg_msg("add volume %d", vol_id);
627 	ubi_dbg_dump_vol_info(vol);
628 
629 	/* Register character device for the volume */
630 	cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
631 	vol->cdev.owner = THIS_MODULE;
632 	dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
633 	err = cdev_add(&vol->cdev, dev, 1);
634 	if (err) {
635 		ubi_err("cannot add character device for volume %d, error %d",
636 			vol_id, err);
637 		return err;
638 	}
639 
640 	err = ubi_create_gluebi(ubi, vol);
641 	if (err)
642 		goto out_cdev;
643 
644 	vol->dev.release = vol_release;
645 	vol->dev.parent = &ubi->dev;
646 	vol->dev.devt = dev;
647 	vol->dev.class = ubi_class;
648 	sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
649 	err = device_register(&vol->dev);
650 	if (err)
651 		goto out_gluebi;
652 
653 	err = volume_sysfs_init(ubi, vol);
654 	if (err) {
655 		cdev_del(&vol->cdev);
656 		err = ubi_destroy_gluebi(vol);
657 		volume_sysfs_close(vol);
658 		return err;
659 	}
660 
661 	paranoid_check_volumes(ubi);
662 	return 0;
663 
664 out_gluebi:
665 	err = ubi_destroy_gluebi(vol);
666 out_cdev:
667 	cdev_del(&vol->cdev);
668 	return err;
669 }
670 
671 /**
672  * ubi_free_volume - free volume.
673  * @ubi: UBI device description object
674  * @vol: volume description object
675  *
676  * This function frees all resources for volume @vol but does not remove it.
677  * Used only when the UBI device is detached.
678  */
679 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
680 {
681 	dbg_msg("free volume %d", vol->vol_id);
682 
683 	ubi->volumes[vol->vol_id] = NULL;
684 	ubi_destroy_gluebi(vol);
685 	cdev_del(&vol->cdev);
686 	volume_sysfs_close(vol);
687 }
688 
689 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
690 
691 /**
692  * paranoid_check_volume - check volume information.
693  * @ubi: UBI device description object
694  * @vol_id: volume ID
695  */
696 static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
697 {
698 	int idx = vol_id2idx(ubi, vol_id);
699 	int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
700 	const struct ubi_volume *vol;
701 	long long n;
702 	const char *name;
703 
704 	spin_lock(&ubi->volumes_lock);
705 	reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
706 	vol = ubi->volumes[idx];
707 
708 	if (!vol) {
709 		if (reserved_pebs) {
710 			ubi_err("no volume info, but volume exists");
711 			goto fail;
712 		}
713 		spin_unlock(&ubi->volumes_lock);
714 		return;
715 	}
716 
717 	if (vol->exclusive) {
718 		/*
719 		 * The volume may be being created at the moment, do not check
720 		 * it (e.g., it may be in the middle of ubi_create_volume().
721 		 */
722 		spin_unlock(&ubi->volumes_lock);
723 		return;
724 	}
725 
726 	if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
727 	    vol->name_len < 0) {
728 		ubi_err("negative values");
729 		goto fail;
730 	}
731 	if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
732 		ubi_err("bad alignment");
733 		goto fail;
734 	}
735 
736 	n = vol->alignment & (ubi->min_io_size - 1);
737 	if (vol->alignment != 1 && n) {
738 		ubi_err("alignment is not multiple of min I/O unit");
739 		goto fail;
740 	}
741 
742 	n = ubi->leb_size % vol->alignment;
743 	if (vol->data_pad != n) {
744 		ubi_err("bad data_pad, has to be %lld", n);
745 		goto fail;
746 	}
747 
748 	if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
749 	    vol->vol_type != UBI_STATIC_VOLUME) {
750 		ubi_err("bad vol_type");
751 		goto fail;
752 	}
753 
754 	if (vol->upd_marker && vol->corrupted) {
755 		dbg_err("update marker and corrupted simultaneously");
756 		goto fail;
757 	}
758 
759 	if (vol->reserved_pebs > ubi->good_peb_count) {
760 		ubi_err("too large reserved_pebs");
761 		goto fail;
762 	}
763 
764 	n = ubi->leb_size - vol->data_pad;
765 	if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
766 		ubi_err("bad usable_leb_size, has to be %lld", n);
767 		goto fail;
768 	}
769 
770 	if (vol->name_len > UBI_VOL_NAME_MAX) {
771 		ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
772 		goto fail;
773 	}
774 
775 	if (!vol->name) {
776 		ubi_err("NULL volume name");
777 		goto fail;
778 	}
779 
780 	n = strnlen(vol->name, vol->name_len + 1);
781 	if (n != vol->name_len) {
782 		ubi_err("bad name_len %lld", n);
783 		goto fail;
784 	}
785 
786 	n = (long long)vol->used_ebs * vol->usable_leb_size;
787 	if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
788 		if (vol->corrupted) {
789 			ubi_err("corrupted dynamic volume");
790 			goto fail;
791 		}
792 		if (vol->used_ebs != vol->reserved_pebs) {
793 			ubi_err("bad used_ebs");
794 			goto fail;
795 		}
796 		if (vol->last_eb_bytes != vol->usable_leb_size) {
797 			ubi_err("bad last_eb_bytes");
798 			goto fail;
799 		}
800 		if (vol->used_bytes != n) {
801 			ubi_err("bad used_bytes");
802 			goto fail;
803 		}
804 	} else {
805 		if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
806 			ubi_err("bad used_ebs");
807 			goto fail;
808 		}
809 		if (vol->last_eb_bytes < 0 ||
810 		    vol->last_eb_bytes > vol->usable_leb_size) {
811 			ubi_err("bad last_eb_bytes");
812 			goto fail;
813 		}
814 		if (vol->used_bytes < 0 || vol->used_bytes > n ||
815 		    vol->used_bytes < n - vol->usable_leb_size) {
816 			ubi_err("bad used_bytes");
817 			goto fail;
818 		}
819 	}
820 
821 	alignment  = be32_to_cpu(ubi->vtbl[vol_id].alignment);
822 	data_pad   = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
823 	name_len   = be16_to_cpu(ubi->vtbl[vol_id].name_len);
824 	upd_marker = ubi->vtbl[vol_id].upd_marker;
825 	name       = &ubi->vtbl[vol_id].name[0];
826 	if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
827 		vol_type = UBI_DYNAMIC_VOLUME;
828 	else
829 		vol_type = UBI_STATIC_VOLUME;
830 
831 	if (alignment != vol->alignment || data_pad != vol->data_pad ||
832 	    upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
833 	    name_len!= vol->name_len || strncmp(name, vol->name, name_len)) {
834 		ubi_err("volume info is different");
835 		goto fail;
836 	}
837 
838 	spin_unlock(&ubi->volumes_lock);
839 	return;
840 
841 fail:
842 	ubi_err("paranoid check failed for volume %d", vol_id);
843 	ubi_dbg_dump_vol_info(vol);
844 	ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
845 	spin_unlock(&ubi->volumes_lock);
846 	BUG();
847 }
848 
849 /**
850  * paranoid_check_volumes - check information about all volumes.
851  * @ubi: UBI device description object
852  */
853 static void paranoid_check_volumes(struct ubi_device *ubi)
854 {
855 	int i;
856 
857 	for (i = 0; i < ubi->vtbl_slots; i++)
858 		paranoid_check_volume(ubi, i);
859 }
860 #endif
861