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