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