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