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