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