blob: f8ab08f9df00a3eec4acf8f284a83b59410fed72 [file] [log] [blame]
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkc91a7192008-11-19 16:28:06 +01005 *
6 * Author: Artem Bityutskiy (Битюцкий Артём)
7 */
8
9/*
10 * This file contains implementation of volume creation, deletion, updating and
11 * resizing.
12 */
13
Heiko Schocherff94bc42014-06-24 10:10:04 +020014#ifndef __UBOOT__
Kyungmin Parkc91a7192008-11-19 16:28:06 +010015#include <linux/err.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020016#include <linux/slab.h>
17#include <linux/export.h>
18#else
19#include <div64.h>
Kyungmin Parkc91a7192008-11-19 16:28:06 +010020#include <ubi_uboot.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020021#endif
22#include <linux/math64.h>
23
Kyungmin Parkc91a7192008-11-19 16:28:06 +010024#include "ubi.h"
25
Heiko Schocherff94bc42014-06-24 10:10:04 +020026static int self_check_volumes(struct ubi_device *ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +010027
Heiko Schocherff94bc42014-06-24 10:10:04 +020028#ifndef __UBOOT__
Kyungmin Parkc91a7192008-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 Schocher0195a7b2015-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 Parkc91a7192008-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 Schocherff94bc42014-06-24 10:10:04 +0200137 kfree(vol->eba_tbl);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100138 kfree(vol);
139}
140
Kyungmin Parkc91a7192008-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 Schocherff94bc42014-06-24 10:10:04 +0200150 * @ubi->device_mutex locked.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100151 */
152int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
153{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200154 int i, err, vol_id = req->vol_id, do_free = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100155 struct ubi_volume *vol;
156 struct ubi_vtbl_record vtbl_rec;
Kyungmin Parkc91a7192008-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
166 spin_lock(&ubi->volumes_lock);
167 if (vol_id == UBI_VOL_NUM_AUTO) {
168 /* Find unused volume ID */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200169 dbg_gen("search for vacant volume ID");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100170 for (i = 0; i < ubi->vtbl_slots; i++)
171 if (!ubi->volumes[i]) {
172 vol_id = i;
173 break;
174 }
175
176 if (vol_id == UBI_VOL_NUM_AUTO) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200177 ubi_err(ubi, "out of volume IDs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100178 err = -ENFILE;
179 goto out_unlock;
180 }
181 req->vol_id = vol_id;
182 }
183
Heiko Schocherff94bc42014-06-24 10:10:04 +0200184 dbg_gen("create device %d, volume %d, %llu bytes, type %d, name %s",
185 ubi->ubi_num, vol_id, (unsigned long long)req->bytes,
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100186 (int)req->vol_type, req->name);
187
188 /* Ensure that this volume does not exist */
189 err = -EEXIST;
190 if (ubi->volumes[vol_id]) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200191 ubi_err(ubi, "volume %d already exists", vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100192 goto out_unlock;
193 }
194
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)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200200 ubi_err(ubi, "volume \"%s\" exists (ID %d)",
201 req->name, i);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100202 goto out_unlock;
203 }
204
Wolfgang Denk455ae7e2008-12-16 01:02:17 +0100205 /* Calculate how many eraseblocks are requested */
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100206 vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200207 vol->reserved_pebs = div_u64(req->bytes + vol->usable_leb_size - 1,
208 vol->usable_leb_size);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100209
210 /* Reserve physical eraseblocks */
211 if (vol->reserved_pebs > ubi->avail_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200212 ubi_err(ubi, "not enough PEBs, only %d available",
213 ubi->avail_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200214 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200215 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200216 ubi->corr_peb_count);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100217 err = -ENOSPC;
218 goto out_unlock;
219 }
220 ubi->avail_pebs -= vol->reserved_pebs;
221 ubi->rsvd_pebs += vol->reserved_pebs;
222 spin_unlock(&ubi->volumes_lock);
223
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;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200229 memcpy(vol->name, req->name, vol->name_len);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100230 vol->ubi = ubi;
231
232 /*
233 * Finish all pending erases because there may be some LEBs belonging
234 * to the same volume ID.
235 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200236 err = ubi_wl_flush(ubi, vol_id, UBI_ALL);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100237 if (err)
238 goto out_acc;
239
240 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
241 if (!vol->eba_tbl) {
242 err = -ENOMEM;
243 goto out_acc;
244 }
245
246 for (i = 0; i < vol->reserved_pebs; i++)
247 vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
248
249 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
250 vol->used_ebs = vol->reserved_pebs;
251 vol->last_eb_bytes = vol->usable_leb_size;
252 vol->used_bytes =
253 (long long)vol->used_ebs * vol->usable_leb_size;
254 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200255 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)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100259 vol->used_ebs += 1;
260 else
261 vol->last_eb_bytes = vol->usable_leb_size;
262 }
263
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);
269 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200270 ubi_err(ubi, "cannot add character device");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100271 goto out_mapping;
272 }
273
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100274 vol->dev.release = vol_release;
275 vol->dev.parent = &ubi->dev;
276 vol->dev.devt = dev;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200277#ifndef __UBOOT__
278 vol->dev.class = &ubi_class;
279 vol->dev.groups = volume_dev_groups;
280#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100281
Heiko Schocherff94bc42014-06-24 10:10:04 +0200282 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100283 err = device_register(&vol->dev);
284 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200285 ubi_err(ubi, "cannot register device");
Heiko Schocherff94bc42014-06-24 10:10:04 +0200286 goto out_cdev;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100287 }
288
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100289 /* 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;
297 else
298 vtbl_rec.vol_type = UBI_VID_STATIC;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200299 memcpy(vtbl_rec.name, vol->name, vol->name_len);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100300
301 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
302 if (err)
303 goto out_sysfs;
304
305 spin_lock(&ubi->volumes_lock);
306 ubi->volumes[vol_id] = vol;
307 ubi->vol_count += 1;
308 spin_unlock(&ubi->volumes_lock);
309
Heiko Schocherff94bc42014-06-24 10:10:04 +0200310 ubi_volume_notify(ubi, vol, UBI_VOLUME_ADDED);
311 self_check_volumes(ubi);
312 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100313
314out_sysfs:
315 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200316 * We have registered our device, we should not free the volume
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100317 * description object in this function in case of an error - it is
318 * freed by the release function.
319 *
320 * Get device reference to prevent the release function from being
321 * called just after sysfs has been closed.
322 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200323 do_free = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100324 get_device(&vol->dev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200325 device_unregister(&vol->dev);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100326out_cdev:
327 cdev_del(&vol->cdev);
328out_mapping:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200329 if (do_free)
330 kfree(vol->eba_tbl);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100331out_acc:
332 spin_lock(&ubi->volumes_lock);
333 ubi->rsvd_pebs -= vol->reserved_pebs;
334 ubi->avail_pebs += vol->reserved_pebs;
335out_unlock:
336 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200337 if (do_free)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100338 kfree(vol);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200339 else
340 put_device(&vol->dev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200341 ubi_err(ubi, "cannot create volume %d, error %d", vol_id, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100342 return err;
343}
344
345/**
346 * ubi_remove_volume - remove volume.
347 * @desc: volume descriptor
Heiko Schocherff94bc42014-06-24 10:10:04 +0200348 * @no_vtbl: do not change volume table if not zero
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100349 *
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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200352 * code in case of failure. The caller has to have the @ubi->device_mutex
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100353 * locked.
354 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200355int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100356{
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;
360
Heiko Schocherff94bc42014-06-24 10:10:04 +0200361 dbg_gen("remove device %d, volume %d", ubi->ubi_num, vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100362 ubi_assert(desc->mode == UBI_EXCLUSIVE);
363 ubi_assert(vol == ubi->volumes[vol_id]);
364
365 if (ubi->ro_mode)
366 return -EROFS;
367
368 spin_lock(&ubi->volumes_lock);
369 if (vol->ref_count > 1) {
370 /*
371 * The volume is busy, probably someone is reading one of its
372 * sysfs files.
373 */
374 err = -EBUSY;
375 goto out_unlock;
376 }
377 ubi->volumes[vol_id] = NULL;
378 spin_unlock(&ubi->volumes_lock);
379
Heiko Schocherff94bc42014-06-24 10:10:04 +0200380 if (!no_vtbl) {
381 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
382 if (err)
383 goto out_err;
384 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100385
386 for (i = 0; i < vol->reserved_pebs; i++) {
387 err = ubi_eba_unmap_leb(ubi, vol, i);
388 if (err)
389 goto out_err;
390 }
391
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100392 cdev_del(&vol->cdev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200393 device_unregister(&vol->dev);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100394
395 spin_lock(&ubi->volumes_lock);
396 ubi->rsvd_pebs -= reserved_pebs;
397 ubi->avail_pebs += reserved_pebs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200398 ubi_update_reserved(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100399 ubi->vol_count -= 1;
400 spin_unlock(&ubi->volumes_lock);
401
Heiko Schocherff94bc42014-06-24 10:10:04 +0200402 ubi_volume_notify(ubi, vol, UBI_VOLUME_REMOVED);
403 if (!no_vtbl)
404 self_check_volumes(ubi);
405
406 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100407
408out_err:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200409 ubi_err(ubi, "cannot remove volume %d, error %d", vol_id, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100410 spin_lock(&ubi->volumes_lock);
411 ubi->volumes[vol_id] = vol;
412out_unlock:
413 spin_unlock(&ubi->volumes_lock);
414 return err;
415}
416
417/**
418 * ubi_resize_volume - re-size volume.
419 * @desc: volume descriptor
420 * @reserved_pebs: new size in physical eraseblocks
421 *
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
Heiko Schocherff94bc42014-06-24 10:10:04 +0200424 * @ubi->device_mutex locked.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100425 */
426int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
427{
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;
433
434 if (ubi->ro_mode)
435 return -EROFS;
436
Heiko Schocherff94bc42014-06-24 10:10:04 +0200437 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);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100439
440 if (vol->vol_type == UBI_STATIC_VOLUME &&
441 reserved_pebs < vol->used_ebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200442 ubi_err(ubi, "too small size %d, %d LEBs contain data",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100443 reserved_pebs, vol->used_ebs);
444 return -EINVAL;
445 }
446
447 /* If the size is the same, we have nothing to do */
448 if (reserved_pebs == vol->reserved_pebs)
449 return 0;
450
451 new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
452 if (!new_mapping)
453 return -ENOMEM;
454
455 for (i = 0; i < reserved_pebs; i++)
456 new_mapping[i] = UBI_LEB_UNMAPPED;
457
458 spin_lock(&ubi->volumes_lock);
459 if (vol->ref_count > 1) {
460 spin_unlock(&ubi->volumes_lock);
461 err = -EBUSY;
462 goto out_free;
463 }
464 spin_unlock(&ubi->volumes_lock);
465
466 /* Reserve physical eraseblocks */
467 pebs = reserved_pebs - vol->reserved_pebs;
468 if (pebs > 0) {
469 spin_lock(&ubi->volumes_lock);
470 if (pebs > ubi->avail_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200471 ubi_err(ubi, "not enough PEBs: requested %d, available %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100472 pebs, ubi->avail_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200473 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200474 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200475 ubi->corr_peb_count);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100476 spin_unlock(&ubi->volumes_lock);
477 err = -ENOSPC;
478 goto out_free;
479 }
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];
484 kfree(vol->eba_tbl);
485 vol->eba_tbl = new_mapping;
486 spin_unlock(&ubi->volumes_lock);
487 }
488
489 /* Change volume table record */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200490 vtbl_rec = ubi->vtbl[vol_id];
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100491 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
492 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
493 if (err)
494 goto out_acc;
495
496 if (pebs < 0) {
497 for (i = 0; i < -pebs; i++) {
498 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
499 if (err)
500 goto out_acc;
501 }
502 spin_lock(&ubi->volumes_lock);
503 ubi->rsvd_pebs += pebs;
504 ubi->avail_pebs -= pebs;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200505 ubi_update_reserved(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100506 for (i = 0; i < reserved_pebs; i++)
507 new_mapping[i] = vol->eba_tbl[i];
508 kfree(vol->eba_tbl);
509 vol->eba_tbl = new_mapping;
510 spin_unlock(&ubi->volumes_lock);
511 }
512
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;
517 vol->used_bytes =
518 (long long)vol->used_ebs * vol->usable_leb_size;
519 }
520
Heiko Schocherff94bc42014-06-24 10:10:04 +0200521 ubi_volume_notify(ubi, vol, UBI_VOLUME_RESIZED);
522 self_check_volumes(ubi);
523 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100524
525out_acc:
526 if (pebs > 0) {
527 spin_lock(&ubi->volumes_lock);
528 ubi->rsvd_pebs -= pebs;
529 ubi->avail_pebs += pebs;
530 spin_unlock(&ubi->volumes_lock);
531 }
532out_free:
533 kfree(new_mapping);
534 return err;
535}
536
537/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200538 * ubi_rename_volumes - re-name UBI volumes.
539 * @ubi: UBI device description object
540 * @rename_list: list of &struct ubi_rename_entry objects
541 *
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
544 * failure.
545 */
546int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list)
547{
548 int err;
549 struct ubi_rename_entry *re;
550
551 err = ubi_vtbl_rename_volumes(ubi, rename_list);
552 if (err)
553 return err;
554
555 list_for_each_entry(re, rename_list, list) {
556 if (re->remove) {
557 err = ubi_remove_volume(re->desc, 1);
558 if (err)
559 break;
560 } else {
561 struct ubi_volume *vol = re->desc->vol;
562
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);
568 }
569 }
570
571 if (!err)
572 self_check_volumes(ubi);
573 return err;
574}
575
576/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100577 * ubi_add_volume - add volume.
578 * @ubi: UBI device description object
579 * @vol: volume description object
580 *
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
583 * case of failure.
584 */
585int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
586{
587 int err, vol_id = vol->vol_id;
588 dev_t dev;
589
Heiko Schocherff94bc42014-06-24 10:10:04 +0200590 dbg_gen("add volume %d", vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100591
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);
597 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200598 ubi_err(ubi, "cannot add character device for volume %d, error %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100599 vol_id, err);
600 return err;
601 }
602
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100603 vol->dev.release = vol_release;
604 vol->dev.parent = &ubi->dev;
605 vol->dev.devt = dev;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200606#ifndef __UBOOT__
607 vol->dev.class = &ubi_class;
608 vol->dev.groups = volume_dev_groups;
609#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200610 dev_set_name(&vol->dev, "%s_%d", ubi->ubi_name, vol->vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100611 err = device_register(&vol->dev);
612 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200613 goto out_cdev;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100614
Heiko Schocherff94bc42014-06-24 10:10:04 +0200615 self_check_volumes(ubi);
616 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100617
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100618out_cdev:
619 cdev_del(&vol->cdev);
620 return err;
621}
622
623/**
624 * ubi_free_volume - free volume.
625 * @ubi: UBI device description object
626 * @vol: volume description object
627 *
628 * This function frees all resources for volume @vol but does not remove it.
629 * Used only when the UBI device is detached.
630 */
631void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
632{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200633 dbg_gen("free volume %d", vol->vol_id);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100634
635 ubi->volumes[vol->vol_id] = NULL;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100636 cdev_del(&vol->cdev);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200637 device_unregister(&vol->dev);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100638}
639
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100640/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200641 * self_check_volume - check volume information.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100642 * @ubi: UBI device description object
643 * @vol_id: volume ID
Heiko Schocherff94bc42014-06-24 10:10:04 +0200644 *
645 * Returns zero if volume is all right and a a negative error code if not.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100646 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200647static int self_check_volume(struct ubi_device *ubi, int vol_id)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100648{
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;
652 long long n;
653 const char *name;
654
655 spin_lock(&ubi->volumes_lock);
656 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
657 vol = ubi->volumes[idx];
658
659 if (!vol) {
660 if (reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200661 ubi_err(ubi, "no volume info, but volume exists");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100662 goto fail;
663 }
664 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200665 return 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100666 }
667
668 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
669 vol->name_len < 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200670 ubi_err(ubi, "negative values");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100671 goto fail;
672 }
673 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200674 ubi_err(ubi, "bad alignment");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100675 goto fail;
676 }
677
678 n = vol->alignment & (ubi->min_io_size - 1);
679 if (vol->alignment != 1 && n) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200680 ubi_err(ubi, "alignment is not multiple of min I/O unit");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100681 goto fail;
682 }
683
684 n = ubi->leb_size % vol->alignment;
685 if (vol->data_pad != n) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200686 ubi_err(ubi, "bad data_pad, has to be %lld", n);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100687 goto fail;
688 }
689
690 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
691 vol->vol_type != UBI_STATIC_VOLUME) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200692 ubi_err(ubi, "bad vol_type");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100693 goto fail;
694 }
695
696 if (vol->upd_marker && vol->corrupted) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200697 ubi_err(ubi, "update marker and corrupted simultaneously");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100698 goto fail;
699 }
700
701 if (vol->reserved_pebs > ubi->good_peb_count) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200702 ubi_err(ubi, "too large reserved_pebs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100703 goto fail;
704 }
705
706 n = ubi->leb_size - vol->data_pad;
707 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200708 ubi_err(ubi, "bad usable_leb_size, has to be %lld", n);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100709 goto fail;
710 }
711
712 if (vol->name_len > UBI_VOL_NAME_MAX) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200713 ubi_err(ubi, "too long volume name, max is %d",
714 UBI_VOL_NAME_MAX);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100715 goto fail;
716 }
717
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100718 n = strnlen(vol->name, vol->name_len + 1);
719 if (n != vol->name_len) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200720 ubi_err(ubi, "bad name_len %lld", n);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100721 goto fail;
722 }
723
724 n = (long long)vol->used_ebs * vol->usable_leb_size;
725 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
726 if (vol->corrupted) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200727 ubi_err(ubi, "corrupted dynamic volume");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100728 goto fail;
729 }
730 if (vol->used_ebs != vol->reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200731 ubi_err(ubi, "bad used_ebs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100732 goto fail;
733 }
734 if (vol->last_eb_bytes != vol->usable_leb_size) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200735 ubi_err(ubi, "bad last_eb_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100736 goto fail;
737 }
738 if (vol->used_bytes != n) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200739 ubi_err(ubi, "bad used_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100740 goto fail;
741 }
742 } else {
743 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200744 ubi_err(ubi, "bad used_ebs");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100745 goto fail;
746 }
747 if (vol->last_eb_bytes < 0 ||
748 vol->last_eb_bytes > vol->usable_leb_size) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200749 ubi_err(ubi, "bad last_eb_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100750 goto fail;
751 }
752 if (vol->used_bytes < 0 || vol->used_bytes > n ||
753 vol->used_bytes < n - vol->usable_leb_size) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200754 ubi_err(ubi, "bad used_bytes");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100755 goto fail;
756 }
757 }
758
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;
766 else
767 vol_type = UBI_STATIC_VOLUME;
768
769 if (alignment != vol->alignment || data_pad != vol->data_pad ||
770 upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
Heiko Schocherff94bc42014-06-24 10:10:04 +0200771 name_len != vol->name_len || strncmp(name, vol->name, name_len)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200772 ubi_err(ubi, "volume info is different");
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100773 goto fail;
774 }
775
776 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200777 return 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100778
779fail:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200780 ubi_err(ubi, "self-check failed for volume %d", vol_id);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200781 if (vol)
782 ubi_dump_vol_info(vol);
783 ubi_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
784 dump_stack();
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100785 spin_unlock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200786 return -EINVAL;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100787}
788
789/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200790 * self_check_volumes - check information about all volumes.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100791 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200792 *
793 * Returns zero if volumes are all right and a a negative error code if not.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100794 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200795static int self_check_volumes(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100796{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200797 int i, err = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100798
Heiko Schocherff94bc42014-06-24 10:10:04 +0200799 if (!ubi_dbg_chk_gen(ubi))
800 return 0;
801
802 for (i = 0; i < ubi->vtbl_slots; i++) {
803 err = self_check_volume(ubi, i);
804 if (err)
805 break;
806 }
807
808 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100809}