2 * Copyright (c) International Business Machines Corp., 2006
4 * SPDX-License-Identifier: GPL-2.0+
6 * Author: Artem Bityutskiy (Битюцкий Артём)
10 * This file contains implementation of volume creation, deletion, updating and
15 #include <linux/err.h>
16 #include <linux/slab.h>
17 #include <linux/export.h>
20 #include <ubi_uboot.h>
22 #include <linux/math64.h>
26 static int self_check_volumes(struct ubi_device *ubi);
29 static ssize_t vol_attribute_show(struct device *dev,
30 struct device_attribute *attr, char *buf);
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);
51 * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
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;
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.
62 static ssize_t vol_attribute_show(struct device *dev,
63 struct device_attribute *attr, char *buf)
66 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
67 struct ubi_device *ubi;
69 ubi = ubi_get_device(vol->ubi->ubi_num);
73 spin_lock(&ubi->volumes_lock);
74 if (!ubi->volumes[vol->vol_id]) {
75 spin_unlock(&ubi->volumes_lock);
79 /* Take a reference to prevent volume removal */
81 spin_unlock(&ubi->volumes_lock);
83 if (attr == &attr_vol_reserved_ebs)
84 ret = sprintf(buf, "%d\n", vol->reserved_pebs);
85 else if (attr == &attr_vol_type) {
88 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
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);
106 /* This must be a bug */
109 /* We've done the operation, drop volume and UBI device references */
110 spin_lock(&ubi->volumes_lock);
112 ubi_assert(vol->ref_count >= 0);
113 spin_unlock(&ubi->volumes_lock);
118 static struct attribute *volume_dev_attrs[] = {
119 &attr_vol_reserved_ebs.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,
129 ATTRIBUTE_GROUPS(volume_dev);
132 /* Release method for volume devices */
133 static void vol_release(struct device *dev)
135 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
142 * ubi_create_volume - create volume.
143 * @ubi: UBI device description object
144 * @req: volume creation request
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.
152 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
154 int i, err, vol_id = req->vol_id, do_free = 1;
155 struct ubi_volume *vol;
156 struct ubi_vtbl_record vtbl_rec;
162 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
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]) {
176 if (vol_id == UBI_VOL_NUM_AUTO) {
177 ubi_err(ubi, "out of volume IDs");
181 req->vol_id = vol_id;
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);
188 /* Ensure that this volume does not exist */
190 if (ubi->volumes[vol_id]) {
191 ubi_err(ubi, "volume %d already exists", vol_id);
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)",
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);
210 /* Reserve physical eraseblocks */
211 if (vol->reserved_pebs > ubi->avail_pebs) {
212 ubi_err(ubi, "not enough PEBs, only %d available",
214 if (ubi->corr_peb_count)
215 ubi_err(ubi, "%d PEBs are corrupted and not used",
216 ubi->corr_peb_count);
220 ubi->avail_pebs -= vol->reserved_pebs;
221 ubi->rsvd_pebs += vol->reserved_pebs;
222 spin_unlock(&ubi->volumes_lock);
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);
233 * Finish all pending erases because there may be some LEBs belonging
234 * to the same volume ID.
236 err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
240 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
246 for (i = 0; i < vol->reserved_pebs; i++)
247 vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
249 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
250 vol->used_ebs = vol->reserved_pebs;
251 vol->last_eb_bytes = vol->usable_leb_size;
253 (long long)vol->used_ebs * vol->usable_leb_size;
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)
261 vol->last_eb_bytes = vol->usable_leb_size;
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);
270 ubi_err(ubi, "cannot add character device");
274 vol->dev.release = vol_release;
275 vol->dev.parent = &ubi->dev;
278 vol->dev.class = &ubi_class;
279 vol->dev.groups = volume_dev_groups;
282 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
283 err = device_register(&vol->dev);
285 ubi_err(ubi, "cannot register device");
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;
298 vtbl_rec.vol_type = UBI_VID_STATIC;
299 memcpy(vtbl_rec.name, vol->name, vol->name_len);
301 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
305 spin_lock(&ubi->volumes_lock);
306 ubi->volumes[vol_id] = vol;
308 spin_unlock(&ubi->volumes_lock);
310 ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
311 self_check_volumes(ubi);
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.
320 * Get device reference to prevent the release function from being
321 * called just after sysfs has been closed.
324 get_device(&vol->dev);
325 device_unregister(&vol->dev);
327 cdev_del(&vol->cdev);
332 spin_lock(&ubi->volumes_lock);
333 ubi->rsvd_pebs -= vol->reserved_pebs;
334 ubi->avail_pebs += vol->reserved_pebs;
336 spin_unlock(&ubi->volumes_lock);
340 put_device(&vol->dev);
341 ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
346 * ubi_remove_volume - remove volume.
347 * @desc: volume descriptor
348 * @no_vtbl: do not change volume table if not zero
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
355 int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
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;
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]);
368 spin_lock(&ubi->volumes_lock);
369 if (vol->ref_count > 1) {
371 * The volume is busy, probably someone is reading one of its
377 ubi->volumes[vol_id] = NULL;
378 spin_unlock(&ubi->volumes_lock);
381 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
386 for (i = 0; i < vol->reserved_pebs; i++) {
387 err = ubi_eba_unmap_leb(ubi, vol, i);
392 cdev_del(&vol->cdev);
393 device_unregister(&vol->dev);
395 spin_lock(&ubi->volumes_lock);
396 ubi->rsvd_pebs -= reserved_pebs;
397 ubi->avail_pebs += reserved_pebs;
398 ubi_update_reserved(ubi);
400 spin_unlock(&ubi->volumes_lock);
402 ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
404 self_check_volumes(ubi);
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;
413 spin_unlock(&ubi->volumes_lock);
418 * ubi_resize_volume - re-size volume.
419 * @desc: volume descriptor
420 * @reserved_pebs: new size in physical eraseblocks
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.
426 int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
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;
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);
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);
447 /* If the size is the same, we have nothing to do */
448 if (reserved_pebs == vol->reserved_pebs)
451 new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
455 for (i = 0; i < reserved_pebs; i++)
456 new_mapping[i] = UBI_LEB_UNMAPPED;
458 spin_lock(&ubi->volumes_lock);
459 if (vol->ref_count > 1) {
460 spin_unlock(&ubi->volumes_lock);
464 spin_unlock(&ubi->volumes_lock);
466 /* Reserve physical eraseblocks */
467 pebs = reserved_pebs - vol->reserved_pebs;
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);
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];
485 vol->eba_tbl = new_mapping;
486 spin_unlock(&ubi->volumes_lock);
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);
497 for (i = 0; i < -pebs; i++) {
498 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
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];
509 vol->eba_tbl = new_mapping;
510 spin_unlock(&ubi->volumes_lock);
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;
518 (long long)vol->used_ebs * vol->usable_leb_size;
521 ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
522 self_check_volumes(ubi);
527 spin_lock(&ubi->volumes_lock);
528 ubi->rsvd_pebs -= pebs;
529 ubi->avail_pebs += pebs;
530 spin_unlock(&ubi->volumes_lock);
538 * ubi_rename_volumes - re-name UBI volumes.
539 * @ubi: UBI device description object
540 * @rename_list: list of &struct ubi_rename_entry objects
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
546 int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
549 struct ubi_rename_entry *re;
551 err = ubi_vtbl_rename_volumes(ubi, rename_list);
555 list_for_each_entry(re, rename_list, list) {
557 err = ubi_remove_volume(re->desc, 1);
561 struct ubi_volume *vol = re->desc->vol;
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);
572 self_check_volumes(ubi);
577 * ubi_add_volume - add volume.
578 * @ubi: UBI device description object
579 * @vol: volume description object
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
585 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
587 int err, vol_id = vol->vol_id;
590 dbg_gen("add volume %d", vol_id);
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);
598 ubi_err(ubi, "cannot add character device for volume %d, error %d",
603 vol->dev.release = vol_release;
604 vol->dev.parent = &ubi->dev;
607 vol->dev.class = &ubi_class;
608 vol->dev.groups = volume_dev_groups;
610 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
611 err = device_register(&vol->dev);
615 self_check_volumes(ubi);
619 cdev_del(&vol->cdev);
624 * ubi_free_volume - free volume.
625 * @ubi: UBI device description object
626 * @vol: volume description object
628 * This function frees all resources for volume @vol but does not remove it.
629 * Used only when the UBI device is detached.
631 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
633 dbg_gen("free volume %d", vol->vol_id);
635 ubi->volumes[vol->vol_id] = NULL;
636 cdev_del(&vol->cdev);
637 device_unregister(&vol->dev);
641 * self_check_volume - check volume information.
642 * @ubi: UBI device description object
645 * Returns zero if volume is all right and a a negative error code if not.
647 static int self_check_volume(struct ubi_device *ubi, int vol_id)
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;
655 spin_lock(&ubi->volumes_lock);
656 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
657 vol = ubi->volumes[idx];
661 ubi_err(ubi, "no volume info, but volume exists");
664 spin_unlock(&ubi->volumes_lock);
668 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
670 ubi_err(ubi, "negative values");
673 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
674 ubi_err(ubi, "bad alignment");
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");
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);
690 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
691 vol->vol_type != UBI_STATIC_VOLUME) {
692 ubi_err(ubi, "bad vol_type");
696 if (vol->upd_marker && vol->corrupted) {
697 ubi_err(ubi, "update marker and corrupted simultaneously");
701 if (vol->reserved_pebs > ubi->good_peb_count) {
702 ubi_err(ubi, "too large reserved_pebs");
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);
712 if (vol->name_len > UBI_VOL_NAME_MAX) {
713 ubi_err(ubi, "too long volume name, max is %d",
718 n = strnlen(vol->name, vol->name_len + 1);
719 if (n != vol->name_len) {
720 ubi_err(ubi, "bad name_len %lld", n);
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");
730 if (vol->used_ebs != vol->reserved_pebs) {
731 ubi_err(ubi, "bad used_ebs");
734 if (vol->last_eb_bytes != vol->usable_leb_size) {
735 ubi_err(ubi, "bad last_eb_bytes");
738 if (vol->used_bytes != n) {
739 ubi_err(ubi, "bad used_bytes");
743 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
744 ubi_err(ubi, "bad used_ebs");
747 if (vol->last_eb_bytes < 0 ||
748 vol->last_eb_bytes > vol->usable_leb_size) {
749 ubi_err(ubi, "bad last_eb_bytes");
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");
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;
767 vol_type = UBI_STATIC_VOLUME;
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");
776 spin_unlock(&ubi->volumes_lock);
780 ubi_err(ubi, "self-check failed for volume %d", vol_id);
782 ubi_dump_vol_info(vol);
783 ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
785 spin_unlock(&ubi->volumes_lock);
790 * self_check_volumes - check information about all volumes.
791 * @ubi: UBI device description object
793 * Returns zero if volumes are all right and a a negative error code if not.
795 static int self_check_volumes(struct ubi_device *ubi)
799 if (!ubi_dbg_chk_gen(ubi))
802 for (i = 0; i < ubi->vtbl_slots; i++) {
803 err = self_check_volume(ubi, i);