blob: 0bfedd0debd697f6dc44fed4b18b0533e4bfa0c9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkc91a7192008-11-19 16:28:06 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Kyungmin Parkc91a7192008-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 Schocherff94bc42014-06-24 10:10:04 +020013#ifndef __UBOOT__
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass61b29b82020-02-03 07:36:15 -070015#include <dm/devres.h>
Kyungmin Parkc91a7192008-11-19 16:28:06 +010016#include <linux/err.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020017#include <linux/slab.h>
18#include <linux/export.h>
19#else
20#include <div64.h>
Kyungmin Parkc91a7192008-11-19 16:28:06 +010021#include <ubi_uboot.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020022#endif
23#include <linux/math64.h>
24
Kyungmin Parkc91a7192008-11-19 16:28:06 +010025#include "ubi.h"
26
Heiko Schocherff94bc42014-06-24 10:10:04 +020027static int self_check_volumes(struct ubi_device *ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +010028
Heiko Schocherff94bc42014-06-24 10:10:04 +020029#ifndef __UBOOT__
Kyungmin Parkc91a7192008-11-19 16:28:06 +010030static 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' */
34static struct device_attribute attr_vol_reserved_ebs =
35 __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
36static struct device_attribute attr_vol_type =
37 __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
38static struct device_attribute attr_vol_name =
39 __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
40static struct device_attribute attr_vol_corrupted =
41 __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
42static struct device_attribute attr_vol_alignment =
43 __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
44static struct device_attribute attr_vol_usable_eb_size =
45 __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
46static struct device_attribute attr_vol_data_bytes =
47 __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
48static 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 */
63static 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}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200118
119static 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};
130ATTRIBUTE_GROUPS(volume_dev);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100131#endif
132
133/* Release method for volume devices */
134static void vol_release(struct device *dev)
135{
136 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
137
Heiko Schocherff94bc42014-06-24 10:10:04 +0200138 kfree(vol->eba_tbl);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100139 kfree(vol);
140}
141
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100142/**
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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200151 * @ubi->device_mutex locked.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100152 */
153int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
154{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200155 int i, err, vol_id = req->vol_id, do_free = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100156 struct ubi_volume *vol;
157 struct ubi_vtbl_record vtbl_rec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100158 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
Quentin Schulz386f20c2019-09-12 16:41:01 +0200167 if (req->flags & UBI_VOL_SKIP_CRC_CHECK_FLG)
168 vol->skip_check = 1;
169
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100170 spin_lock(&ubi->volumes_lock);
171 if (vol_id == UBI_VOL_NUM_AUTO) {
172 /* Find unused volume ID */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200173 dbg_gen("search for vacant volume ID");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100174 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) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200181 ubi_err(ubi, "out of volume IDs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100182 err = -ENFILE;
183 goto out_unlock;
184 }
185 req->vol_id = vol_id;
186 }
187
Heiko Schocherff94bc42014-06-24 10:10:04 +0200188 dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
189 ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100190 (int)req->vol_type, req->name);
191
192 /* Ensure that this volume does not exist */
193 err = -EEXIST;
194 if (ubi->volumes[vol_id]) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200195 ubi_err(ubi, "volume %d already exists", vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100196 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)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200204 ubi_err(ubi, "volume \"%s\" exists (ID %d)",
205 req->name, i);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100206 goto out_unlock;
207 }
208
Wolfgang Denk455ae7e2008-12-16 01:02:17 +0100209 /* Calculate how many eraseblocks are requested */
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100210 vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200211 vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
212 vol->usable_leb_size);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100213
214 /* Reserve physical eraseblocks */
215 if (vol->reserved_pebs > ubi->avail_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200216 ubi_err(ubi, "not enough PEBs, only %d available",
217 ubi->avail_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200218 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200219 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200220 ubi->corr_peb_count);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100221 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;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200233 memcpy(vol->name, req->name, vol->name_len);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100234 vol->ubi = ubi;
235
236 /*
237 * Finish all pending erases because there may be some LEBs belonging
238 * to the same volume ID.
239 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200240 err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100241 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 {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200259 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)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100263 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) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200274 ubi_err(ubi, "cannot add character device");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100275 goto out_mapping;
276 }
277
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100278 vol->dev.release = vol_release;
279 vol->dev.parent = &ubi->dev;
280 vol->dev.devt = dev;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200281#ifndef __UBOOT__
282 vol->dev.class = &ubi_class;
283 vol->dev.groups = volume_dev_groups;
284#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100285
Heiko Schocherff94bc42014-06-24 10:10:04 +0200286 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100287 err = device_register(&vol->dev);
288 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200289 ubi_err(ubi, "cannot register device");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200290 goto out_cdev;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100291 }
292
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100293 /* 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;
Quentin Schulz386f20c2019-09-12 16:41:01 +0200303
304 if (vol->skip_check)
305 vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
306
Heiko Schocherff94bc42014-06-24 10:10:04 +0200307 memcpy(vtbl_rec.name, vol->name, vol->name_len);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100308
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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200318 ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
319 self_check_volumes(ubi);
320 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100321
322out_sysfs:
323 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200324 * We have registered our device, we should not free the volume
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100325 * 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 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200331 do_free = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100332 get_device(&vol->dev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200333 device_unregister(&vol->dev);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100334out_cdev:
335 cdev_del(&vol->cdev);
336out_mapping:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200337 if (do_free)
338 kfree(vol->eba_tbl);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100339out_acc:
340 spin_lock(&ubi->volumes_lock);
341 ubi->rsvd_pebs -= vol->reserved_pebs;
342 ubi->avail_pebs += vol->reserved_pebs;
343out_unlock:
344 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200345 if (do_free)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100346 kfree(vol);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200347 else
348 put_device(&vol->dev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200349 ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100350 return err;
351}
352
353/**
354 * ubi_remove_volume - remove volume.
355 * @desc: volume descriptor
Heiko Schocherff94bc42014-06-24 10:10:04 +0200356 * @no_vtbl: do not change volume table if not zero
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100357 *
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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200360 * code in case of failure. The caller has to have the @ubi->device_mutex
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100361 * locked.
362 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200363int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100364{
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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200369 dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100370 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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200388 if (!no_vtbl) {
389 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
390 if (err)
391 goto out_err;
392 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100393
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
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100400 cdev_del(&vol->cdev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200401 device_unregister(&vol->dev);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100402
403 spin_lock(&ubi->volumes_lock);
404 ubi->rsvd_pebs -= reserved_pebs;
405 ubi->avail_pebs += reserved_pebs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200406 ubi_update_reserved(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100407 ubi->vol_count -= 1;
408 spin_unlock(&ubi->volumes_lock);
409
Heiko Schocherff94bc42014-06-24 10:10:04 +0200410 ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
411 if (!no_vtbl)
412 self_check_volumes(ubi);
413
414 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100415
416out_err:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200417 ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100418 spin_lock(&ubi->volumes_lock);
419 ubi->volumes[vol_id] = vol;
420out_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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200432 * @ubi->device_mutex locked.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100433 */
434int 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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200445 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);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100447
448 if (vol->vol_type == UBI_STATIC_VOLUME &&
449 reserved_pebs < vol->used_ebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200450 ubi_err(ubi, "too small size %d, %d LEBs contain data",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100451 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) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200479 ubi_err(ubi, "not enough PEBs: requested %d, available %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100480 pebs, ubi->avail_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200481 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200482 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200483 ubi->corr_peb_count);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100484 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 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200498 vtbl_rec = ubi->vtbl[vol_id];
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100499 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;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200513 ubi_update_reserved(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100514 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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200529 ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
530 self_check_volumes(ubi);
531 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100532
533out_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 }
540out_free:
541 kfree(new_mapping);
542 return err;
543}
544
545/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200546 * 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 */
554int 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/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100585 * 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 */
593int 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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200598 dbg_gen("add volume %d", vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100599
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) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200606 ubi_err(ubi, "cannot add character device for volume %d, error %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100607 vol_id, err);
608 return err;
609 }
610
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100611 vol->dev.release = vol_release;
612 vol->dev.parent = &ubi->dev;
613 vol->dev.devt = dev;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200614#ifndef __UBOOT__
615 vol->dev.class = &ubi_class;
616 vol->dev.groups = volume_dev_groups;
617#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200618 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100619 err = device_register(&vol->dev);
620 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200621 goto out_cdev;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100622
Heiko Schocherff94bc42014-06-24 10:10:04 +0200623 self_check_volumes(ubi);
624 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100625
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100626out_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 */
639void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
640{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200641 dbg_gen("free volume %d", vol->vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100642
643 ubi->volumes[vol->vol_id] = NULL;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100644 cdev_del(&vol->cdev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200645 device_unregister(&vol->dev);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100646}
647
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100648/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200649 * self_check_volume - check volume information.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100650 * @ubi: UBI device description object
651 * @vol_id: volume ID
Heiko Schocherff94bc42014-06-24 10:10:04 +0200652 *
653 * Returns zero if volume is all right and a a negative error code if not.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100654 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200655static int self_check_volume(struct ubi_device *ubi, int vol_id)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100656{
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) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200669 ubi_err(ubi, "no volume info, but volume exists");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100670 goto fail;
671 }
672 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200673 return 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100674 }
675
676 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
677 vol->name_len < 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200678 ubi_err(ubi, "negative values");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100679 goto fail;
680 }
681 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200682 ubi_err(ubi, "bad alignment");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100683 goto fail;
684 }
685
686 n = vol->alignment & (ubi->min_io_size - 1);
687 if (vol->alignment != 1 && n) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200688 ubi_err(ubi, "alignment is not multiple of min I/O unit");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100689 goto fail;
690 }
691
692 n = ubi->leb_size % vol->alignment;
693 if (vol->data_pad != n) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200694 ubi_err(ubi, "bad data_pad, has to be %lld", n);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100695 goto fail;
696 }
697
698 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
699 vol->vol_type != UBI_STATIC_VOLUME) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200700 ubi_err(ubi, "bad vol_type");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100701 goto fail;
702 }
703
704 if (vol->upd_marker && vol->corrupted) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200705 ubi_err(ubi, "update marker and corrupted simultaneously");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100706 goto fail;
707 }
708
709 if (vol->reserved_pebs > ubi->good_peb_count) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200710 ubi_err(ubi, "too large reserved_pebs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100711 goto fail;
712 }
713
714 n = ubi->leb_size - vol->data_pad;
715 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200716 ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100717 goto fail;
718 }
719
720 if (vol->name_len > UBI_VOL_NAME_MAX) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200721 ubi_err(ubi, "too long volume name, max is %d",
722 UBI_VOL_NAME_MAX);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100723 goto fail;
724 }
725
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100726 n = strnlen(vol->name, vol->name_len + 1);
727 if (n != vol->name_len) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200728 ubi_err(ubi, "bad name_len %lld", n);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100729 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) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200735 ubi_err(ubi, "corrupted dynamic volume");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100736 goto fail;
737 }
738 if (vol->used_ebs != vol->reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200739 ubi_err(ubi, "bad used_ebs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100740 goto fail;
741 }
742 if (vol->last_eb_bytes != vol->usable_leb_size) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200743 ubi_err(ubi, "bad last_eb_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100744 goto fail;
745 }
746 if (vol->used_bytes != n) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200747 ubi_err(ubi, "bad used_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100748 goto fail;
749 }
Quentin Schulz386f20c2019-09-12 16:41:01 +0200750
751 if (vol->skip_check) {
752 ubi_err(ubi, "bad skip_check");
753 goto fail;
754 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100755 } else {
756 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200757 ubi_err(ubi, "bad used_ebs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100758 goto fail;
759 }
760 if (vol->last_eb_bytes < 0 ||
761 vol->last_eb_bytes > vol->usable_leb_size) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200762 ubi_err(ubi, "bad last_eb_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100763 goto fail;
764 }
765 if (vol->used_bytes < 0 || vol->used_bytes > n ||
766 vol->used_bytes < n - vol->usable_leb_size) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200767 ubi_err(ubi, "bad used_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100768 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 ||
Heiko Schocherff94bc42014-06-24 10:10:04 +0200784 name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200785 ubi_err(ubi, "volume info is different");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100786 goto fail;
787 }
788
789 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200790 return 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100791
792fail:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200793 ubi_err(ubi, "self-check failed for volume %d", vol_id);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200794 if (vol)
795 ubi_dump_vol_info(vol);
796 ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
797 dump_stack();
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100798 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200799 return -EINVAL;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100800}
801
802/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200803 * self_check_volumes - check information about all volumes.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100804 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200805 *
806 * Returns zero if volumes are all right and a a negative error code if not.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100807 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200808static int self_check_volumes(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100809{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200810 int i, err = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100811
Heiko Schocherff94bc42014-06-24 10:10:04 +0200812 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;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100822}