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