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