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