UBI: simplify error handling
[linux-block.git] / drivers / mtd / ubi / vmt.c
CommitLineData
801c135c
AB
1/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём)
19 */
20
21/*
22 * This file contains implementation of volume creation, deletion, updating and
23 * resizing.
24 */
25
26#include <linux/err.h>
27#include <asm/div64.h>
28#include "ubi.h"
29
30#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
31static void paranoid_check_volumes(struct ubi_device *ubi);
32#else
33#define paranoid_check_volumes(ubi)
34#endif
35
36static ssize_t vol_attribute_show(struct device *dev,
37 struct device_attribute *attr, char *buf);
38
39/* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
8bc22961 40static struct device_attribute attr_vol_reserved_ebs =
801c135c 41 __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
8bc22961 42static struct device_attribute attr_vol_type =
801c135c 43 __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
8bc22961 44static struct device_attribute attr_vol_name =
801c135c 45 __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
8bc22961 46static struct device_attribute attr_vol_corrupted =
801c135c 47 __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
8bc22961 48static struct device_attribute attr_vol_alignment =
801c135c 49 __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
8bc22961 50static struct device_attribute attr_vol_usable_eb_size =
801c135c 51 __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
8bc22961 52static struct device_attribute attr_vol_data_bytes =
801c135c 53 __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
8bc22961 54static struct device_attribute attr_vol_upd_marker =
801c135c
AB
55 __ATTR(upd_marker, S_IRUGO, vol_attribute_show, NULL);
56
57/*
58 * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
59 *
60 * Consider a situation:
61 * A. process 1 opens a sysfs file related to volume Y, say
62 * /<sysfs>/class/ubi/ubiX_Y/reserved_ebs;
63 * B. process 2 removes volume Y;
64 * C. process 1 starts reading the /<sysfs>/class/ubi/ubiX_Y/reserved_ebs file;
65 *
66 * What we want to do in a situation like that is to return error when the file
67 * is read. This is done by means of the 'removed' flag and the 'vol_lock' of
68 * the UBI volume description object.
69 */
70static ssize_t vol_attribute_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
72{
54b2c8f9 73 int ret = -ENODEV;
801c135c
AB
74 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
75
76 spin_lock(&vol->ubi->volumes_lock);
77 if (vol->removed) {
78 spin_unlock(&vol->ubi->volumes_lock);
54b2c8f9 79 return ret;
801c135c 80 }
732aeacf 81
8bc22961 82 if (attr == &attr_vol_reserved_ebs)
801c135c 83 ret = sprintf(buf, "%d\n", vol->reserved_pebs);
8bc22961 84 else if (attr == &attr_vol_type) {
801c135c 85 const char *tp;
8bc22961
AB
86
87 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
88 tp = "dynamic";
89 else
90 tp = "static";
801c135c 91 ret = sprintf(buf, "%s\n", tp);
8bc22961 92 } else if (attr == &attr_vol_name)
801c135c 93 ret = sprintf(buf, "%s\n", vol->name);
8bc22961 94 else if (attr == &attr_vol_corrupted)
801c135c 95 ret = sprintf(buf, "%d\n", vol->corrupted);
8bc22961 96 else if (attr == &attr_vol_alignment)
801c135c 97 ret = sprintf(buf, "%d\n", vol->alignment);
732aeacf 98 else if (attr == &attr_vol_usable_eb_size)
801c135c 99 ret = sprintf(buf, "%d\n", vol->usable_leb_size);
732aeacf 100 else if (attr == &attr_vol_data_bytes)
801c135c 101 ret = sprintf(buf, "%lld\n", vol->used_bytes);
8bc22961 102 else if (attr == &attr_vol_upd_marker)
801c135c
AB
103 ret = sprintf(buf, "%d\n", vol->upd_marker);
104 else
105 BUG();
106 spin_unlock(&vol->ubi->volumes_lock);
107 return ret;
108}
109
110/* Release method for volume devices */
111static void vol_release(struct device *dev)
112{
113 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
732aeacf 114
801c135c
AB
115 kfree(vol);
116}
117
118/**
119 * volume_sysfs_init - initialize sysfs for new volume.
120 * @ubi: UBI device description object
121 * @vol: volume description object
122 *
123 * This function returns zero in case of success and a negative error code in
124 * case of failure.
125 *
126 * Note, this function does not free allocated resources in case of failure -
127 * the caller does it. This is because this would cause release() here and the
128 * caller would oops.
129 */
130static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol)
131{
132 int err;
133
8bc22961 134 err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);
801c135c
AB
135 if (err)
136 return err;
8bc22961 137 err = device_create_file(&vol->dev, &attr_vol_type);
801c135c
AB
138 if (err)
139 return err;
8bc22961 140 err = device_create_file(&vol->dev, &attr_vol_name);
801c135c
AB
141 if (err)
142 return err;
8bc22961 143 err = device_create_file(&vol->dev, &attr_vol_corrupted);
801c135c
AB
144 if (err)
145 return err;
8bc22961 146 err = device_create_file(&vol->dev, &attr_vol_alignment);
801c135c
AB
147 if (err)
148 return err;
8bc22961 149 err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);
801c135c
AB
150 if (err)
151 return err;
8bc22961 152 err = device_create_file(&vol->dev, &attr_vol_data_bytes);
801c135c
AB
153 if (err)
154 return err;
8bc22961 155 err = device_create_file(&vol->dev, &attr_vol_upd_marker);
fc75a1e1 156 return err;
801c135c
AB
157}
158
159/**
160 * volume_sysfs_close - close sysfs for a volume.
161 * @vol: volume description object
162 */
163static void volume_sysfs_close(struct ubi_volume *vol)
164{
8bc22961
AB
165 device_remove_file(&vol->dev, &attr_vol_upd_marker);
166 device_remove_file(&vol->dev, &attr_vol_data_bytes);
167 device_remove_file(&vol->dev, &attr_vol_usable_eb_size);
168 device_remove_file(&vol->dev, &attr_vol_alignment);
169 device_remove_file(&vol->dev, &attr_vol_corrupted);
170 device_remove_file(&vol->dev, &attr_vol_name);
171 device_remove_file(&vol->dev, &attr_vol_type);
172 device_remove_file(&vol->dev, &attr_vol_reserved_ebs);
801c135c
AB
173 device_unregister(&vol->dev);
174}
175
176/**
177 * ubi_create_volume - create volume.
178 * @ubi: UBI device description object
179 * @req: volume creation request
180 *
181 * This function creates volume described by @req. If @req->vol_id id
182 * %UBI_VOL_NUM_AUTO, this function automatically assigne ID to the new volume
183 * and saves it in @req->vol_id. Returns zero in case of success and a negative
184 * error code in case of failure.
185 */
186int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
187{
fc75a1e1 188 int i, err, vol_id = req->vol_id, dont_free = 0;
801c135c
AB
189 struct ubi_volume *vol;
190 struct ubi_vtbl_record vtbl_rec;
191 uint64_t bytes;
49dfc299 192 dev_t dev;
801c135c
AB
193
194 if (ubi->ro_mode)
195 return -EROFS;
196
197 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
198 if (!vol)
199 return -ENOMEM;
200
cae0a771 201 mutex_lock(&ubi->volumes_mutex);
801c135c 202 spin_lock(&ubi->volumes_lock);
801c135c
AB
203 if (vol_id == UBI_VOL_NUM_AUTO) {
204 /* Find unused volume ID */
205 dbg_msg("search for vacant volume ID");
206 for (i = 0; i < ubi->vtbl_slots; i++)
207 if (!ubi->volumes[i]) {
208 vol_id = i;
209 break;
210 }
211
212 if (vol_id == UBI_VOL_NUM_AUTO) {
213 dbg_err("out of volume IDs");
214 err = -ENFILE;
215 goto out_unlock;
216 }
217 req->vol_id = vol_id;
218 }
219
220 dbg_msg("volume ID %d, %llu bytes, type %d, name %s",
221 vol_id, (unsigned long long)req->bytes,
222 (int)req->vol_type, req->name);
223
224 /* Ensure that this volume does not exist */
225 err = -EEXIST;
226 if (ubi->volumes[vol_id]) {
227 dbg_err("volume %d already exists", vol_id);
228 goto out_unlock;
229 }
230
231 /* Ensure that the name is unique */
232 for (i = 0; i < ubi->vtbl_slots; i++)
233 if (ubi->volumes[i] &&
234 ubi->volumes[i]->name_len == req->name_len &&
94784d91 235 !strcmp(ubi->volumes[i]->name, req->name)) {
801c135c
AB
236 dbg_err("volume \"%s\" exists (ID %d)", req->name, i);
237 goto out_unlock;
238 }
239
240 /* Calculate how many eraseblocks are requested */
241 vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
242 bytes = req->bytes;
243 if (do_div(bytes, vol->usable_leb_size))
244 vol->reserved_pebs = 1;
245 vol->reserved_pebs += bytes;
246
247 /* Reserve physical eraseblocks */
248 if (vol->reserved_pebs > ubi->avail_pebs) {
249 dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);
801c135c
AB
250 err = -ENOSPC;
251 goto out_unlock;
252 }
253 ubi->avail_pebs -= vol->reserved_pebs;
254 ubi->rsvd_pebs += vol->reserved_pebs;
255
256 vol->vol_id = vol_id;
257 vol->alignment = req->alignment;
258 vol->data_pad = ubi->leb_size % vol->alignment;
259 vol->vol_type = req->vol_type;
260 vol->name_len = req->name_len;
261 memcpy(vol->name, req->name, vol->name_len + 1);
262 vol->exclusive = 1;
263 vol->ubi = ubi;
264 ubi->volumes[vol_id] = vol;
265 spin_unlock(&ubi->volumes_lock);
266
267 /*
268 * Finish all pending erases because there may be some LEBs belonging
269 * to the same volume ID.
270 */
271 err = ubi_wl_flush(ubi);
272 if (err)
273 goto out_acc;
274
275 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
276 if (!vol->eba_tbl) {
277 err = -ENOMEM;
278 goto out_acc;
279 }
280
281 for (i = 0; i < vol->reserved_pebs; i++)
282 vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
283
284 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
285 vol->used_ebs = vol->reserved_pebs;
286 vol->last_eb_bytes = vol->usable_leb_size;
d08c3b78
VA
287 vol->used_bytes =
288 (long long)vol->used_ebs * vol->usable_leb_size;
801c135c
AB
289 } else {
290 bytes = vol->used_bytes;
291 vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
292 vol->used_ebs = bytes;
293 if (vol->last_eb_bytes)
294 vol->used_ebs += 1;
295 else
296 vol->last_eb_bytes = vol->usable_leb_size;
297 }
298
299 /* Register character device for the volume */
300 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
301 vol->cdev.owner = THIS_MODULE;
49dfc299
AB
302 dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
303 err = cdev_add(&vol->cdev, dev, 1);
801c135c 304 if (err) {
01f7b309 305 ubi_err("cannot add character device");
801c135c
AB
306 goto out_mapping;
307 }
308
309 err = ubi_create_gluebi(ubi, vol);
310 if (err)
311 goto out_cdev;
312
313 vol->dev.release = vol_release;
314 vol->dev.parent = &ubi->dev;
49dfc299 315 vol->dev.devt = dev;
801c135c 316 vol->dev.class = ubi_class;
fc75a1e1 317
801c135c
AB
318 sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
319 err = device_register(&vol->dev);
01f7b309
AB
320 if (err) {
321 ubi_err("cannot register device");
801c135c 322 goto out_gluebi;
01f7b309 323 }
801c135c
AB
324
325 err = volume_sysfs_init(ubi, vol);
326 if (err)
327 goto out_sysfs;
328
329 /* Fill volume table record */
330 memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
3261ebd7
CH
331 vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
332 vtbl_rec.alignment = cpu_to_be32(vol->alignment);
333 vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
334 vtbl_rec.name_len = cpu_to_be16(vol->name_len);
801c135c
AB
335 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
336 vtbl_rec.vol_type = UBI_VID_DYNAMIC;
337 else
338 vtbl_rec.vol_type = UBI_VID_STATIC;
339 memcpy(vtbl_rec.name, vol->name, vol->name_len + 1);
340
341 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
342 if (err)
343 goto out_sysfs;
344
345 spin_lock(&ubi->volumes_lock);
346 ubi->vol_count += 1;
347 vol->exclusive = 0;
348 spin_unlock(&ubi->volumes_lock);
349
350 paranoid_check_volumes(ubi);
cae0a771 351 mutex_unlock(&ubi->volumes_mutex);
801c135c
AB
352 return 0;
353
fc75a1e1
AB
354out_sysfs:
355 /*
356 * We have degistered our device, we should not free the volume*
357 * description object in this function in case of an error - it is
358 * freed by the release function.
359 *
360 * Get device reference to prevent the release function from being
361 * called just after sysfs has been closed.
362 */
363 dont_free = 1;
364 get_device(&vol->dev);
365 volume_sysfs_close(vol);
801c135c 366out_gluebi:
fc75a1e1 367 ubi_destroy_gluebi(vol);
801c135c
AB
368out_cdev:
369 cdev_del(&vol->cdev);
370out_mapping:
371 kfree(vol->eba_tbl);
372out_acc:
373 spin_lock(&ubi->volumes_lock);
374 ubi->rsvd_pebs -= vol->reserved_pebs;
375 ubi->avail_pebs += vol->reserved_pebs;
94784d91 376 ubi->volumes[vol_id] = NULL;
801c135c
AB
377out_unlock:
378 spin_unlock(&ubi->volumes_lock);
cae0a771 379 mutex_unlock(&ubi->volumes_mutex);
fc75a1e1
AB
380 if (dont_free)
381 put_device(&vol->dev);
382 else
383 kfree(vol);
01f7b309 384 ubi_err("cannot create volume %d, error %d", vol_id, err);
801c135c
AB
385 return err;
386}
387
388/**
389 * ubi_remove_volume - remove volume.
390 * @desc: volume descriptor
391 *
392 * This function removes volume described by @desc. The volume has to be opened
393 * in "exclusive" mode. Returns zero in case of success and a negative error
394 * code in case of failure.
395 */
396int ubi_remove_volume(struct ubi_volume_desc *desc)
397{
398 struct ubi_volume *vol = desc->vol;
399 struct ubi_device *ubi = vol->ubi;
400 int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
401
402 dbg_msg("remove UBI volume %d", vol_id);
403 ubi_assert(desc->mode == UBI_EXCLUSIVE);
404 ubi_assert(vol == ubi->volumes[vol_id]);
405
406 if (ubi->ro_mode)
407 return -EROFS;
408
cae0a771 409 mutex_lock(&ubi->volumes_mutex);
801c135c
AB
410 err = ubi_destroy_gluebi(vol);
411 if (err)
cae0a771 412 goto out;
801c135c
AB
413
414 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
415 if (err)
cae0a771 416 goto out;
801c135c
AB
417
418 for (i = 0; i < vol->reserved_pebs; i++) {
89b96b69 419 err = ubi_eba_unmap_leb(ubi, vol, i);
801c135c 420 if (err)
cae0a771 421 goto out;
801c135c
AB
422 }
423
424 spin_lock(&ubi->volumes_lock);
425 vol->removed = 1;
426 ubi->volumes[vol_id] = NULL;
427 spin_unlock(&ubi->volumes_lock);
428
429 kfree(vol->eba_tbl);
430 vol->eba_tbl = NULL;
431 cdev_del(&vol->cdev);
432 volume_sysfs_close(vol);
801c135c
AB
433
434 spin_lock(&ubi->volumes_lock);
435 ubi->rsvd_pebs -= reserved_pebs;
436 ubi->avail_pebs += reserved_pebs;
437 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
438 if (i > 0) {
439 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
440 ubi->avail_pebs -= i;
441 ubi->rsvd_pebs += i;
442 ubi->beb_rsvd_pebs += i;
443 if (i > 0)
444 ubi_msg("reserve more %d PEBs", i);
445 }
446 ubi->vol_count -= 1;
447 spin_unlock(&ubi->volumes_lock);
448
449 paranoid_check_volumes(ubi);
cae0a771
AB
450out:
451 mutex_unlock(&ubi->volumes_mutex);
452 return err;
801c135c
AB
453}
454
455/**
456 * ubi_resize_volume - re-size volume.
457 * @desc: volume descriptor
458 * @reserved_pebs: new size in physical eraseblocks
459 *
460 * This function returns zero in case of success, and a negative error code in
461 * case of failure.
462 */
463int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
464{
465 int i, err, pebs, *new_mapping;
466 struct ubi_volume *vol = desc->vol;
467 struct ubi_device *ubi = vol->ubi;
468 struct ubi_vtbl_record vtbl_rec;
469 int vol_id = vol->vol_id;
470
471 if (ubi->ro_mode)
472 return -EROFS;
473
474 dbg_msg("re-size volume %d to from %d to %d PEBs",
475 vol_id, vol->reserved_pebs, reserved_pebs);
476 ubi_assert(desc->mode == UBI_EXCLUSIVE);
477 ubi_assert(vol == ubi->volumes[vol_id]);
478
479 if (vol->vol_type == UBI_STATIC_VOLUME &&
480 reserved_pebs < vol->used_ebs) {
481 dbg_err("too small size %d, %d LEBs contain data",
482 reserved_pebs, vol->used_ebs);
483 return -EINVAL;
484 }
485
486 /* If the size is the same, we have nothing to do */
487 if (reserved_pebs == vol->reserved_pebs)
488 return 0;
489
490 new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
491 if (!new_mapping)
492 return -ENOMEM;
493
494 for (i = 0; i < reserved_pebs; i++)
495 new_mapping[i] = UBI_LEB_UNMAPPED;
496
497 /* Reserve physical eraseblocks */
cae0a771 498 mutex_lock(&ubi->volumes_mutex);
801c135c
AB
499 pebs = reserved_pebs - vol->reserved_pebs;
500 if (pebs > 0) {
501 spin_lock(&ubi->volumes_lock);
502 if (pebs > ubi->avail_pebs) {
503 dbg_err("not enough PEBs: requested %d, available %d",
504 pebs, ubi->avail_pebs);
505 spin_unlock(&ubi->volumes_lock);
506 err = -ENOSPC;
507 goto out_free;
508 }
509 ubi->avail_pebs -= pebs;
510 ubi->rsvd_pebs += pebs;
511 for (i = 0; i < vol->reserved_pebs; i++)
512 new_mapping[i] = vol->eba_tbl[i];
513 kfree(vol->eba_tbl);
514 vol->eba_tbl = new_mapping;
515 spin_unlock(&ubi->volumes_lock);
516 }
517
518 /* Change volume table record */
519 memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
3261ebd7 520 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
801c135c
AB
521 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
522 if (err)
523 goto out_acc;
524
525 if (pebs < 0) {
526 for (i = 0; i < -pebs; i++) {
89b96b69 527 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
801c135c
AB
528 if (err)
529 goto out_acc;
530 }
531 spin_lock(&ubi->volumes_lock);
532 ubi->rsvd_pebs += pebs;
533 ubi->avail_pebs -= pebs;
534 pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
535 if (pebs > 0) {
536 pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
537 ubi->avail_pebs -= pebs;
538 ubi->rsvd_pebs += pebs;
539 ubi->beb_rsvd_pebs += pebs;
540 if (pebs > 0)
541 ubi_msg("reserve more %d PEBs", pebs);
542 }
543 for (i = 0; i < reserved_pebs; i++)
544 new_mapping[i] = vol->eba_tbl[i];
545 kfree(vol->eba_tbl);
546 vol->eba_tbl = new_mapping;
547 spin_unlock(&ubi->volumes_lock);
548 }
549
550 vol->reserved_pebs = reserved_pebs;
551 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
552 vol->used_ebs = reserved_pebs;
553 vol->last_eb_bytes = vol->usable_leb_size;
d08c3b78
VA
554 vol->used_bytes =
555 (long long)vol->used_ebs * vol->usable_leb_size;
801c135c
AB
556 }
557
558 paranoid_check_volumes(ubi);
cae0a771 559 mutex_unlock(&ubi->volumes_mutex);
801c135c
AB
560 return 0;
561
562out_acc:
563 if (pebs > 0) {
564 spin_lock(&ubi->volumes_lock);
565 ubi->rsvd_pebs -= pebs;
566 ubi->avail_pebs += pebs;
567 spin_unlock(&ubi->volumes_lock);
568 }
569out_free:
570 kfree(new_mapping);
cae0a771 571 mutex_unlock(&ubi->volumes_mutex);
801c135c
AB
572 return err;
573}
574
575/**
576 * ubi_add_volume - add volume.
577 * @ubi: UBI device description object
89b96b69 578 * @vol: volume description object
801c135c
AB
579 *
580 * This function adds an existin volume and initializes all its data
581 * structures. Returnes zero in case of success and a negative error code in
582 * case of failure.
583 */
89b96b69 584int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
801c135c 585{
89b96b69 586 int err, vol_id = vol->vol_id;
49dfc299 587 dev_t dev;
801c135c
AB
588
589 dbg_msg("add volume %d", vol_id);
590 ubi_dbg_dump_vol_info(vol);
591 ubi_assert(vol);
592
593 /* Register character device for the volume */
594 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
595 vol->cdev.owner = THIS_MODULE;
49dfc299
AB
596 dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
597 err = cdev_add(&vol->cdev, dev, 1);
801c135c 598 if (err) {
01f7b309
AB
599 ubi_err("cannot add character device for volume %d, error %d",
600 vol_id, err);
801c135c
AB
601 return err;
602 }
603
604 err = ubi_create_gluebi(ubi, vol);
605 if (err)
606 goto out_cdev;
607
608 vol->dev.release = vol_release;
609 vol->dev.parent = &ubi->dev;
49dfc299 610 vol->dev.devt = dev;
801c135c
AB
611 vol->dev.class = ubi_class;
612 sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
613 err = device_register(&vol->dev);
614 if (err)
615 goto out_gluebi;
616
617 err = volume_sysfs_init(ubi, vol);
618 if (err) {
619 cdev_del(&vol->cdev);
620 err = ubi_destroy_gluebi(vol);
621 volume_sysfs_close(vol);
622 return err;
623 }
624
625 paranoid_check_volumes(ubi);
626 return 0;
627
628out_gluebi:
629 err = ubi_destroy_gluebi(vol);
630out_cdev:
631 cdev_del(&vol->cdev);
632 return err;
633}
634
635/**
636 * ubi_free_volume - free volume.
637 * @ubi: UBI device description object
89b96b69 638 * @vol: volume description object
801c135c 639 *
89b96b69 640 * This function frees all resources for volume @vol but does not remove it.
801c135c
AB
641 * Used only when the UBI device is detached.
642 */
89b96b69 643void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
801c135c
AB
644{
645 int err;
801c135c 646
89b96b69 647 dbg_msg("free volume %d", vol->vol_id);
801c135c
AB
648 ubi_assert(vol);
649
650 vol->removed = 1;
651 err = ubi_destroy_gluebi(vol);
89b96b69 652 ubi->volumes[vol->vol_id] = NULL;
801c135c
AB
653 cdev_del(&vol->cdev);
654 volume_sysfs_close(vol);
655}
656
657#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
658
659/**
660 * paranoid_check_volume - check volume information.
661 * @ubi: UBI device description object
662 * @vol_id: volume ID
663 */
b89044bf 664static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
801c135c
AB
665{
666 int idx = vol_id2idx(ubi, vol_id);
667 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
b89044bf 668 const struct ubi_volume *vol;
801c135c
AB
669 long long n;
670 const char *name;
671
b89044bf 672 spin_lock(&ubi->volumes_lock);
3261ebd7 673 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
b89044bf 674 vol = ubi->volumes[idx];
801c135c
AB
675
676 if (!vol) {
677 if (reserved_pebs) {
678 ubi_err("no volume info, but volume exists");
679 goto fail;
680 }
b89044bf
AB
681 spin_unlock(&ubi->volumes_lock);
682 return;
683 }
684
685 if (vol->exclusive) {
686 /*
687 * The volume may be being created at the moment, do not check
688 * it (e.g., it may be in the middle of ubi_create_volume().
689 */
690 spin_unlock(&ubi->volumes_lock);
801c135c
AB
691 return;
692 }
693
694 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
695 vol->name_len < 0) {
696 ubi_err("negative values");
697 goto fail;
698 }
699 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
700 ubi_err("bad alignment");
701 goto fail;
702 }
703
704 n = vol->alignment % ubi->min_io_size;
705 if (vol->alignment != 1 && n) {
706 ubi_err("alignment is not multiple of min I/O unit");
707 goto fail;
708 }
709
710 n = ubi->leb_size % vol->alignment;
711 if (vol->data_pad != n) {
712 ubi_err("bad data_pad, has to be %lld", n);
713 goto fail;
714 }
715
716 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
717 vol->vol_type != UBI_STATIC_VOLUME) {
718 ubi_err("bad vol_type");
719 goto fail;
720 }
721
722 if (vol->upd_marker != 0 && vol->upd_marker != 1) {
723 ubi_err("bad upd_marker");
724 goto fail;
725 }
726
727 if (vol->upd_marker && vol->corrupted) {
728 dbg_err("update marker and corrupted simultaneously");
729 goto fail;
730 }
731
732 if (vol->reserved_pebs > ubi->good_peb_count) {
733 ubi_err("too large reserved_pebs");
734 goto fail;
735 }
736
737 n = ubi->leb_size - vol->data_pad;
738 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
739 ubi_err("bad usable_leb_size, has to be %lld", n);
740 goto fail;
741 }
742
743 if (vol->name_len > UBI_VOL_NAME_MAX) {
744 ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
745 goto fail;
746 }
747
748 if (!vol->name) {
749 ubi_err("NULL volume name");
750 goto fail;
751 }
752
753 n = strnlen(vol->name, vol->name_len + 1);
754 if (n != vol->name_len) {
755 ubi_err("bad name_len %lld", n);
756 goto fail;
757 }
758
d08c3b78 759 n = (long long)vol->used_ebs * vol->usable_leb_size;
801c135c
AB
760 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
761 if (vol->corrupted != 0) {
762 ubi_err("corrupted dynamic volume");
763 goto fail;
764 }
765 if (vol->used_ebs != vol->reserved_pebs) {
766 ubi_err("bad used_ebs");
767 goto fail;
768 }
769 if (vol->last_eb_bytes != vol->usable_leb_size) {
770 ubi_err("bad last_eb_bytes");
771 goto fail;
772 }
773 if (vol->used_bytes != n) {
774 ubi_err("bad used_bytes");
775 goto fail;
776 }
777 } else {
778 if (vol->corrupted != 0 && vol->corrupted != 1) {
779 ubi_err("bad corrupted");
780 goto fail;
781 }
782 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
783 ubi_err("bad used_ebs");
784 goto fail;
785 }
786 if (vol->last_eb_bytes < 0 ||
787 vol->last_eb_bytes > vol->usable_leb_size) {
788 ubi_err("bad last_eb_bytes");
789 goto fail;
790 }
791 if (vol->used_bytes < 0 || vol->used_bytes > n ||
792 vol->used_bytes < n - vol->usable_leb_size) {
793 ubi_err("bad used_bytes");
794 goto fail;
795 }
796 }
797
3261ebd7
CH
798 alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
799 data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
800 name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
801c135c
AB
801 upd_marker = ubi->vtbl[vol_id].upd_marker;
802 name = &ubi->vtbl[vol_id].name[0];
803 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
804 vol_type = UBI_DYNAMIC_VOLUME;
805 else
806 vol_type = UBI_STATIC_VOLUME;
807
808 if (alignment != vol->alignment || data_pad != vol->data_pad ||
809 upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
810 name_len!= vol->name_len || strncmp(name, vol->name, name_len)) {
811 ubi_err("volume info is different");
812 goto fail;
813 }
814
b89044bf 815 spin_unlock(&ubi->volumes_lock);
801c135c
AB
816 return;
817
818fail:
94784d91 819 ubi_err("paranoid check failed for volume %d", vol_id);
801c135c
AB
820 ubi_dbg_dump_vol_info(vol);
821 ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
b89044bf 822 spin_unlock(&ubi->volumes_lock);
801c135c
AB
823 BUG();
824}
825
826/**
827 * paranoid_check_volumes - check information about all volumes.
828 * @ubi: UBI device description object
829 */
830static void paranoid_check_volumes(struct ubi_device *ubi)
831{
832 int i;
833
801c135c
AB
834 for (i = 0; i < ubi->vtbl_slots; i++)
835 paranoid_check_volume(ubi, i);
801c135c
AB
836}
837#endif