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