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