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