blob: 2114abbe7c365390a6e01758a1eaaf58c372247e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Park7f88f002008-11-19 16:28:06 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Kyungmin Park7f88f002008-11-19 16:28:06 +01005 * Author: Artem Bityutskiy (Битюцкий Артём)
6 */
7
8/*
9 * This file contains implementation of volume creation, deletion, updating and
10 * resizing.
11 */
12
Heiko Schocherf5895d12014-06-24 10:10:04 +020013#ifndef __UBOOT__
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <dm/devres.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010015#include <linux/err.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020016#include <linux/slab.h>
17#include <linux/export.h>
18#else
19#include <div64.h>
20#include <ubi_uboot.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010021#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +020022#include <linux/math64.h>
Kyungmin Park7f88f002008-11-19 16:28:06 +010023
Kyungmin Park7f88f002008-11-19 16:28:06 +010024#include "ubi.h"
25
Heiko Schocherf5895d12014-06-24 10:10:04 +020026static int self_check_volumes(struct ubi_device *ubi);
Kyungmin Park7f88f002008-11-19 16:28:06 +010027
Heiko Schocherf5895d12014-06-24 10:10:04 +020028#ifndef __UBOOT__
Kyungmin Park7f88f002008-11-19 16:28:06 +010029static 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' */
33static struct device_attribute attr_vol_reserved_ebs =
34 __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
35static struct device_attribute attr_vol_type =
36 __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
37static struct device_attribute attr_vol_name =
38 __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
39static struct device_attribute attr_vol_corrupted =
40 __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
41static struct device_attribute attr_vol_alignment =
42 __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
43static struct device_attribute attr_vol_usable_eb_size =
44 __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
45static struct device_attribute attr_vol_data_bytes =
46 __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
47static 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 */
62static 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}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200117
118static struct attribute *volume_dev_attrs[] = {
119 &attr_vol_reserved_ebs.attr,
120 &attr_vol_type.attr,
121 &attr_vol_name.attr,
122 &attr_vol_corrupted.attr,
123 &attr_vol_alignment.attr,
124 &attr_vol_usable_eb_size.attr,
125 &attr_vol_data_bytes.attr,
126 &attr_vol_upd_marker.attr,
127 NULL
128};
129ATTRIBUTE_GROUPS(volume_dev);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100130#endif
131
132/* Release method for volume devices */
133static void vol_release(struct device *dev)
134{
135 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
136
Heiko Schocherf5895d12014-06-24 10:10:04 +0200137 kfree(vol->eba_tbl);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100138 kfree(vol);
139}
140
Kyungmin Park7f88f002008-11-19 16:28:06 +0100141/**
142 * ubi_create_volume - create volume.
143 * @ubi: UBI device description object
144 * @req: volume creation request
145 *
146 * This function creates volume described by @req. If @req->vol_id id
147 * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
148 * and saves it in @req->vol_id. Returns zero in case of success and a negative
149 * error code in case of failure. Note, the caller has to have the
Heiko Schocherf5895d12014-06-24 10:10:04 +0200150 * @ubi->device_mutex locked.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100151 */
152int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
153{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200154 int i, err, vol_id = req->vol_id, do_free = 1;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100155 struct ubi_volume *vol;
156 struct ubi_vtbl_record vtbl_rec;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100157 dev_t dev;
158
159 if (ubi->ro_mode)
160 return -EROFS;
161
162 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
163 if (!vol)
164 return -ENOMEM;
165
Quentin Schulza88049b2019-09-12 16:41:01 +0200166 if (req->flags & UBI_VOL_SKIP_CRC_CHECK_FLG)
167 vol->skip_check = 1;
168
Kyungmin Park7f88f002008-11-19 16:28:06 +0100169 spin_lock(&ubi->volumes_lock);
170 if (vol_id == UBI_VOL_NUM_AUTO) {
171 /* Find unused volume ID */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200172 dbg_gen("search for vacant volume ID");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100173 for (i = 0; i < ubi->vtbl_slots; i++)
174 if (!ubi->volumes[i]) {
175 vol_id = i;
176 break;
177 }
178
179 if (vol_id == UBI_VOL_NUM_AUTO) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200180 ubi_err(ubi, "out of volume IDs");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100181 err = -ENFILE;
182 goto out_unlock;
183 }
184 req->vol_id = vol_id;
185 }
186
Heiko Schocherf5895d12014-06-24 10:10:04 +0200187 dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
188 ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
Kyungmin Park7f88f002008-11-19 16:28:06 +0100189 (int)req->vol_type, req->name);
190
191 /* Ensure that this volume does not exist */
192 err = -EEXIST;
193 if (ubi->volumes[vol_id]) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200194 ubi_err(ubi, "volume %d already exists", vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100195 goto out_unlock;
196 }
197
198 /* Ensure that the name is unique */
199 for (i = 0; i < ubi->vtbl_slots; i++)
200 if (ubi->volumes[i] &&
201 ubi->volumes[i]->name_len == req->name_len &&
202 !strcmp(ubi->volumes[i]->name, req->name)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200203 ubi_err(ubi, "volume \"%s\" exists (ID %d)",
204 req->name, i);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100205 goto out_unlock;
206 }
207
Wolfgang Denk55334c72008-12-16 01:02:17 +0100208 /* Calculate how many eraseblocks are requested */
Kyungmin Park7f88f002008-11-19 16:28:06 +0100209 vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200210 vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
211 vol->usable_leb_size);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100212
213 /* Reserve physical eraseblocks */
214 if (vol->reserved_pebs > ubi->avail_pebs) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200215 ubi_err(ubi, "not enough PEBs, only %d available",
216 ubi->avail_pebs);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200217 if (ubi->corr_peb_count)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200218 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200219 ubi->corr_peb_count);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100220 err = -ENOSPC;
221 goto out_unlock;
222 }
223 ubi->avail_pebs -= vol->reserved_pebs;
224 ubi->rsvd_pebs += vol->reserved_pebs;
225 spin_unlock(&ubi->volumes_lock);
226
227 vol->vol_id = vol_id;
228 vol->alignment = req->alignment;
229 vol->data_pad = ubi->leb_size % vol->alignment;
230 vol->vol_type = req->vol_type;
231 vol->name_len = req->name_len;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200232 memcpy(vol->name, req->name, vol->name_len);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100233 vol->ubi = ubi;
234
235 /*
236 * Finish all pending erases because there may be some LEBs belonging
237 * to the same volume ID.
238 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200239 err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100240 if (err)
241 goto out_acc;
242
243 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
244 if (!vol->eba_tbl) {
245 err = -ENOMEM;
246 goto out_acc;
247 }
248
249 for (i = 0; i < vol->reserved_pebs; i++)
250 vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
251
252 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
253 vol->used_ebs = vol->reserved_pebs;
254 vol->last_eb_bytes = vol->usable_leb_size;
255 vol->used_bytes =
256 (long long)vol->used_ebs * vol->usable_leb_size;
257 } else {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200258 vol->used_ebs = div_u64_rem(vol->used_bytes,
259 vol->usable_leb_size,
260 &vol->last_eb_bytes);
261 if (vol->last_eb_bytes != 0)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100262 vol->used_ebs += 1;
263 else
264 vol->last_eb_bytes = vol->usable_leb_size;
265 }
266
267 /* Register character device for the volume */
268 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
269 vol->cdev.owner = THIS_MODULE;
270 dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
271 err = cdev_add(&vol->cdev, dev, 1);
272 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200273 ubi_err(ubi, "cannot add character device");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100274 goto out_mapping;
275 }
276
Kyungmin Park7f88f002008-11-19 16:28:06 +0100277 vol->dev.release = vol_release;
278 vol->dev.parent = &ubi->dev;
279 vol->dev.devt = dev;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200280#ifndef __UBOOT__
281 vol->dev.class = &ubi_class;
282 vol->dev.groups = volume_dev_groups;
283#endif
Kyungmin Park7f88f002008-11-19 16:28:06 +0100284
Heiko Schocherf5895d12014-06-24 10:10:04 +0200285 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100286 err = device_register(&vol->dev);
287 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200288 ubi_err(ubi, "cannot register device");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200289 goto out_cdev;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100290 }
291
Kyungmin Park7f88f002008-11-19 16:28:06 +0100292 /* Fill volume table record */
293 memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
294 vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
295 vtbl_rec.alignment = cpu_to_be32(vol->alignment);
296 vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
297 vtbl_rec.name_len = cpu_to_be16(vol->name_len);
298 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
299 vtbl_rec.vol_type = UBI_VID_DYNAMIC;
300 else
301 vtbl_rec.vol_type = UBI_VID_STATIC;
Quentin Schulza88049b2019-09-12 16:41:01 +0200302
303 if (vol->skip_check)
304 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
305
Heiko Schocherf5895d12014-06-24 10:10:04 +0200306 memcpy(vtbl_rec.name, vol->name, vol->name_len);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100307
308 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
309 if (err)
310 goto out_sysfs;
311
312 spin_lock(&ubi->volumes_lock);
313 ubi->volumes[vol_id] = vol;
314 ubi->vol_count += 1;
315 spin_unlock(&ubi->volumes_lock);
316
Heiko Schocherf5895d12014-06-24 10:10:04 +0200317 ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
318 self_check_volumes(ubi);
319 return err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100320
321out_sysfs:
322 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200323 * We have registered our device, we should not free the volume
Kyungmin Park7f88f002008-11-19 16:28:06 +0100324 * description object in this function in case of an error - it is
325 * freed by the release function.
326 *
327 * Get device reference to prevent the release function from being
328 * called just after sysfs has been closed.
329 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200330 do_free = 0;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100331 get_device(&vol->dev);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200332 device_unregister(&vol->dev);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100333out_cdev:
334 cdev_del(&vol->cdev);
335out_mapping:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200336 if (do_free)
337 kfree(vol->eba_tbl);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100338out_acc:
339 spin_lock(&ubi->volumes_lock);
340 ubi->rsvd_pebs -= vol->reserved_pebs;
341 ubi->avail_pebs += vol->reserved_pebs;
342out_unlock:
343 spin_unlock(&ubi->volumes_lock);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200344 if (do_free)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100345 kfree(vol);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200346 else
347 put_device(&vol->dev);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200348 ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100349 return err;
350}
351
352/**
353 * ubi_remove_volume - remove volume.
354 * @desc: volume descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +0200355 * @no_vtbl: do not change volume table if not zero
Kyungmin Park7f88f002008-11-19 16:28:06 +0100356 *
357 * This function removes volume described by @desc. The volume has to be opened
358 * in "exclusive" mode. Returns zero in case of success and a negative error
Heiko Schocherf5895d12014-06-24 10:10:04 +0200359 * code in case of failure. The caller has to have the @ubi->device_mutex
Kyungmin Park7f88f002008-11-19 16:28:06 +0100360 * locked.
361 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200362int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100363{
364 struct ubi_volume *vol = desc->vol;
365 struct ubi_device *ubi = vol->ubi;
366 int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
367
Heiko Schocherf5895d12014-06-24 10:10:04 +0200368 dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100369 ubi_assert(desc->mode == UBI_EXCLUSIVE);
370 ubi_assert(vol == ubi->volumes[vol_id]);
371
372 if (ubi->ro_mode)
373 return -EROFS;
374
375 spin_lock(&ubi->volumes_lock);
376 if (vol->ref_count > 1) {
377 /*
378 * The volume is busy, probably someone is reading one of its
379 * sysfs files.
380 */
381 err = -EBUSY;
382 goto out_unlock;
383 }
384 ubi->volumes[vol_id] = NULL;
385 spin_unlock(&ubi->volumes_lock);
386
Heiko Schocherf5895d12014-06-24 10:10:04 +0200387 if (!no_vtbl) {
388 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
389 if (err)
390 goto out_err;
391 }
Kyungmin Park7f88f002008-11-19 16:28:06 +0100392
393 for (i = 0; i < vol->reserved_pebs; i++) {
394 err = ubi_eba_unmap_leb(ubi, vol, i);
395 if (err)
396 goto out_err;
397 }
398
Kyungmin Park7f88f002008-11-19 16:28:06 +0100399 cdev_del(&vol->cdev);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200400 device_unregister(&vol->dev);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100401
402 spin_lock(&ubi->volumes_lock);
403 ubi->rsvd_pebs -= reserved_pebs;
404 ubi->avail_pebs += reserved_pebs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200405 ubi_update_reserved(ubi);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100406 ubi->vol_count -= 1;
407 spin_unlock(&ubi->volumes_lock);
408
Heiko Schocherf5895d12014-06-24 10:10:04 +0200409 ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
410 if (!no_vtbl)
411 self_check_volumes(ubi);
412
413 return err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100414
415out_err:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200416 ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100417 spin_lock(&ubi->volumes_lock);
418 ubi->volumes[vol_id] = vol;
419out_unlock:
420 spin_unlock(&ubi->volumes_lock);
421 return err;
422}
423
424/**
425 * ubi_resize_volume - re-size volume.
426 * @desc: volume descriptor
427 * @reserved_pebs: new size in physical eraseblocks
428 *
429 * This function re-sizes the volume and returns zero in case of success, and a
430 * negative error code in case of failure. The caller has to have the
Heiko Schocherf5895d12014-06-24 10:10:04 +0200431 * @ubi->device_mutex locked.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100432 */
433int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
434{
435 int i, err, pebs, *new_mapping;
436 struct ubi_volume *vol = desc->vol;
437 struct ubi_device *ubi = vol->ubi;
438 struct ubi_vtbl_record vtbl_rec;
439 int vol_id = vol->vol_id;
440
441 if (ubi->ro_mode)
442 return -EROFS;
443
Heiko Schocherf5895d12014-06-24 10:10:04 +0200444 dbg_gen("re-size device %d, volume %d to from %d to %d PEBs",
445 ubi->ubi_num, vol_id, vol->reserved_pebs, reserved_pebs);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100446
447 if (vol->vol_type == UBI_STATIC_VOLUME &&
448 reserved_pebs < vol->used_ebs) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200449 ubi_err(ubi, "too small size %d, %d LEBs contain data",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100450 reserved_pebs, vol->used_ebs);
451 return -EINVAL;
452 }
453
454 /* If the size is the same, we have nothing to do */
455 if (reserved_pebs == vol->reserved_pebs)
456 return 0;
457
458 new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
459 if (!new_mapping)
460 return -ENOMEM;
461
462 for (i = 0; i < reserved_pebs; i++)
463 new_mapping[i] = UBI_LEB_UNMAPPED;
464
465 spin_lock(&ubi->volumes_lock);
466 if (vol->ref_count > 1) {
467 spin_unlock(&ubi->volumes_lock);
468 err = -EBUSY;
469 goto out_free;
470 }
471 spin_unlock(&ubi->volumes_lock);
472
473 /* Reserve physical eraseblocks */
474 pebs = reserved_pebs - vol->reserved_pebs;
475 if (pebs > 0) {
476 spin_lock(&ubi->volumes_lock);
477 if (pebs > ubi->avail_pebs) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200478 ubi_err(ubi, "not enough PEBs: requested %d, available %d",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100479 pebs, ubi->avail_pebs);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200480 if (ubi->corr_peb_count)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200481 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200482 ubi->corr_peb_count);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100483 spin_unlock(&ubi->volumes_lock);
484 err = -ENOSPC;
485 goto out_free;
486 }
487 ubi->avail_pebs -= pebs;
488 ubi->rsvd_pebs += pebs;
489 for (i = 0; i < vol->reserved_pebs; i++)
490 new_mapping[i] = vol->eba_tbl[i];
491 kfree(vol->eba_tbl);
492 vol->eba_tbl = new_mapping;
493 spin_unlock(&ubi->volumes_lock);
494 }
495
496 /* Change volume table record */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200497 vtbl_rec = ubi->vtbl[vol_id];
Kyungmin Park7f88f002008-11-19 16:28:06 +0100498 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
499 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
500 if (err)
501 goto out_acc;
502
503 if (pebs < 0) {
504 for (i = 0; i < -pebs; i++) {
505 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
506 if (err)
507 goto out_acc;
508 }
509 spin_lock(&ubi->volumes_lock);
510 ubi->rsvd_pebs += pebs;
511 ubi->avail_pebs -= pebs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200512 ubi_update_reserved(ubi);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100513 for (i = 0; i < reserved_pebs; i++)
514 new_mapping[i] = vol->eba_tbl[i];
515 kfree(vol->eba_tbl);
516 vol->eba_tbl = new_mapping;
517 spin_unlock(&ubi->volumes_lock);
518 }
519
520 vol->reserved_pebs = reserved_pebs;
521 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
522 vol->used_ebs = reserved_pebs;
523 vol->last_eb_bytes = vol->usable_leb_size;
524 vol->used_bytes =
525 (long long)vol->used_ebs * vol->usable_leb_size;
526 }
527
Heiko Schocherf5895d12014-06-24 10:10:04 +0200528 ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
529 self_check_volumes(ubi);
530 return err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100531
532out_acc:
533 if (pebs > 0) {
534 spin_lock(&ubi->volumes_lock);
535 ubi->rsvd_pebs -= pebs;
536 ubi->avail_pebs += pebs;
537 spin_unlock(&ubi->volumes_lock);
538 }
539out_free:
540 kfree(new_mapping);
541 return err;
542}
543
544/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200545 * ubi_rename_volumes - re-name UBI volumes.
546 * @ubi: UBI device description object
547 * @rename_list: list of &struct ubi_rename_entry objects
548 *
549 * This function re-names or removes volumes specified in the re-name list.
550 * Returns zero in case of success and a negative error code in case of
551 * failure.
552 */
553int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
554{
555 int err;
556 struct ubi_rename_entry *re;
557
558 err = ubi_vtbl_rename_volumes(ubi, rename_list);
559 if (err)
560 return err;
561
562 list_for_each_entry(re, rename_list, list) {
563 if (re->remove) {
564 err = ubi_remove_volume(re->desc, 1);
565 if (err)
566 break;
567 } else {
568 struct ubi_volume *vol = re->desc->vol;
569
570 spin_lock(&ubi->volumes_lock);
571 vol->name_len = re->new_name_len;
572 memcpy(vol->name, re->new_name, re->new_name_len + 1);
573 spin_unlock(&ubi->volumes_lock);
574 ubi_volume_notify(ubi, vol, UBI_VOLUME_RENAMED);
575 }
576 }
577
578 if (!err)
579 self_check_volumes(ubi);
580 return err;
581}
582
583/**
Kyungmin Park7f88f002008-11-19 16:28:06 +0100584 * ubi_add_volume - add volume.
585 * @ubi: UBI device description object
586 * @vol: volume description object
587 *
588 * This function adds an existing volume and initializes all its data
589 * structures. Returns zero in case of success and a negative error code in
590 * case of failure.
591 */
592int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
593{
594 int err, vol_id = vol->vol_id;
595 dev_t dev;
596
Heiko Schocherf5895d12014-06-24 10:10:04 +0200597 dbg_gen("add volume %d", vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100598
599 /* Register character device for the volume */
600 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
601 vol->cdev.owner = THIS_MODULE;
602 dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
603 err = cdev_add(&vol->cdev, dev, 1);
604 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200605 ubi_err(ubi, "cannot add character device for volume %d, error %d",
Kyungmin Park7f88f002008-11-19 16:28:06 +0100606 vol_id, err);
607 return err;
608 }
609
Kyungmin Park7f88f002008-11-19 16:28:06 +0100610 vol->dev.release = vol_release;
611 vol->dev.parent = &ubi->dev;
612 vol->dev.devt = dev;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200613#ifndef __UBOOT__
614 vol->dev.class = &ubi_class;
615 vol->dev.groups = volume_dev_groups;
616#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200617 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100618 err = device_register(&vol->dev);
619 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200620 goto out_cdev;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100621
Heiko Schocherf5895d12014-06-24 10:10:04 +0200622 self_check_volumes(ubi);
623 return err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100624
Kyungmin Park7f88f002008-11-19 16:28:06 +0100625out_cdev:
626 cdev_del(&vol->cdev);
627 return err;
628}
629
630/**
631 * ubi_free_volume - free volume.
632 * @ubi: UBI device description object
633 * @vol: volume description object
634 *
635 * This function frees all resources for volume @vol but does not remove it.
636 * Used only when the UBI device is detached.
637 */
638void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
639{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200640 dbg_gen("free volume %d", vol->vol_id);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100641
642 ubi->volumes[vol->vol_id] = NULL;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100643 cdev_del(&vol->cdev);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200644 device_unregister(&vol->dev);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100645}
646
Kyungmin Park7f88f002008-11-19 16:28:06 +0100647/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200648 * self_check_volume - check volume information.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100649 * @ubi: UBI device description object
650 * @vol_id: volume ID
Heiko Schocherf5895d12014-06-24 10:10:04 +0200651 *
652 * Returns zero if volume is all right and a a negative error code if not.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100653 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200654static int self_check_volume(struct ubi_device *ubi, int vol_id)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100655{
656 int idx = vol_id2idx(ubi, vol_id);
657 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
658 const struct ubi_volume *vol;
659 long long n;
660 const char *name;
661
662 spin_lock(&ubi->volumes_lock);
663 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
664 vol = ubi->volumes[idx];
665
666 if (!vol) {
667 if (reserved_pebs) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200668 ubi_err(ubi, "no volume info, but volume exists");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100669 goto fail;
670 }
671 spin_unlock(&ubi->volumes_lock);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200672 return 0;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100673 }
674
675 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
676 vol->name_len < 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200677 ubi_err(ubi, "negative values");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100678 goto fail;
679 }
680 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200681 ubi_err(ubi, "bad alignment");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100682 goto fail;
683 }
684
685 n = vol->alignment & (ubi->min_io_size - 1);
686 if (vol->alignment != 1 && n) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200687 ubi_err(ubi, "alignment is not multiple of min I/O unit");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100688 goto fail;
689 }
690
691 n = ubi->leb_size % vol->alignment;
692 if (vol->data_pad != n) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200693 ubi_err(ubi, "bad data_pad, has to be %lld", n);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100694 goto fail;
695 }
696
697 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
698 vol->vol_type != UBI_STATIC_VOLUME) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200699 ubi_err(ubi, "bad vol_type");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100700 goto fail;
701 }
702
703 if (vol->upd_marker && vol->corrupted) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200704 ubi_err(ubi, "update marker and corrupted simultaneously");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100705 goto fail;
706 }
707
708 if (vol->reserved_pebs > ubi->good_peb_count) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200709 ubi_err(ubi, "too large reserved_pebs");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100710 goto fail;
711 }
712
713 n = ubi->leb_size - vol->data_pad;
714 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200715 ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100716 goto fail;
717 }
718
719 if (vol->name_len > UBI_VOL_NAME_MAX) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200720 ubi_err(ubi, "too long volume name, max is %d",
721 UBI_VOL_NAME_MAX);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100722 goto fail;
723 }
724
Kyungmin Park7f88f002008-11-19 16:28:06 +0100725 n = strnlen(vol->name, vol->name_len + 1);
726 if (n != vol->name_len) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200727 ubi_err(ubi, "bad name_len %lld", n);
Kyungmin Park7f88f002008-11-19 16:28:06 +0100728 goto fail;
729 }
730
731 n = (long long)vol->used_ebs * vol->usable_leb_size;
732 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
733 if (vol->corrupted) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200734 ubi_err(ubi, "corrupted dynamic volume");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100735 goto fail;
736 }
737 if (vol->used_ebs != vol->reserved_pebs) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200738 ubi_err(ubi, "bad used_ebs");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100739 goto fail;
740 }
741 if (vol->last_eb_bytes != vol->usable_leb_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200742 ubi_err(ubi, "bad last_eb_bytes");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100743 goto fail;
744 }
745 if (vol->used_bytes != n) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200746 ubi_err(ubi, "bad used_bytes");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100747 goto fail;
748 }
Quentin Schulza88049b2019-09-12 16:41:01 +0200749
750 if (vol->skip_check) {
751 ubi_err(ubi, "bad skip_check");
752 goto fail;
753 }
Kyungmin Park7f88f002008-11-19 16:28:06 +0100754 } else {
755 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200756 ubi_err(ubi, "bad used_ebs");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100757 goto fail;
758 }
759 if (vol->last_eb_bytes < 0 ||
760 vol->last_eb_bytes > vol->usable_leb_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200761 ubi_err(ubi, "bad last_eb_bytes");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100762 goto fail;
763 }
764 if (vol->used_bytes < 0 || vol->used_bytes > n ||
765 vol->used_bytes < n - vol->usable_leb_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200766 ubi_err(ubi, "bad used_bytes");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100767 goto fail;
768 }
769 }
770
771 alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
772 data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
773 name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
774 upd_marker = ubi->vtbl[vol_id].upd_marker;
775 name = &ubi->vtbl[vol_id].name[0];
776 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
777 vol_type = UBI_DYNAMIC_VOLUME;
778 else
779 vol_type = UBI_STATIC_VOLUME;
780
781 if (alignment != vol->alignment || data_pad != vol->data_pad ||
782 upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
Heiko Schocherf5895d12014-06-24 10:10:04 +0200783 name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200784 ubi_err(ubi, "volume info is different");
Kyungmin Park7f88f002008-11-19 16:28:06 +0100785 goto fail;
786 }
787
788 spin_unlock(&ubi->volumes_lock);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200789 return 0;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100790
791fail:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200792 ubi_err(ubi, "self-check failed for volume %d", vol_id);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200793 if (vol)
794 ubi_dump_vol_info(vol);
795 ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
796 dump_stack();
Kyungmin Park7f88f002008-11-19 16:28:06 +0100797 spin_unlock(&ubi->volumes_lock);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200798 return -EINVAL;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100799}
800
801/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200802 * self_check_volumes - check information about all volumes.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100803 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200804 *
805 * Returns zero if volumes are all right and a a negative error code if not.
Kyungmin Park7f88f002008-11-19 16:28:06 +0100806 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200807static int self_check_volumes(struct ubi_device *ubi)
Kyungmin Park7f88f002008-11-19 16:28:06 +0100808{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200809 int i, err = 0;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100810
Heiko Schocherf5895d12014-06-24 10:10:04 +0200811 if (!ubi_dbg_chk_gen(ubi))
812 return 0;
813
814 for (i = 0; i < ubi->vtbl_slots; i++) {
815 err = self_check_volume(ubi, i);
816 if (err)
817 break;
818 }
819
820 return err;
Kyungmin Park7f88f002008-11-19 16:28:06 +0100821}