2 * Copyright (c) International Business Machines Corp., 2006
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.
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.
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
18 * Author: Artem Bityutskiy (Битюцкий Артём)
22 * This file contains implementation of volume creation, deletion, updating and
26 #include <linux/err.h>
27 #include <asm/div64.h>
30 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
31 static void paranoid_check_volumes(struct ubi_device
*ubi
);
33 #define paranoid_check_volumes(ubi)
36 static ssize_t
vol_attribute_show(struct device
*dev
,
37 struct device_attribute
*attr
, char *buf
);
39 /* Device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX_Y' */
40 static struct device_attribute vol_reserved_ebs
=
41 __ATTR(reserved_ebs
, S_IRUGO
, vol_attribute_show
, NULL
);
42 static struct device_attribute vol_type
=
43 __ATTR(type
, S_IRUGO
, vol_attribute_show
, NULL
);
44 static struct device_attribute vol_name
=
45 __ATTR(name
, S_IRUGO
, vol_attribute_show
, NULL
);
46 static struct device_attribute vol_corrupted
=
47 __ATTR(corrupted
, S_IRUGO
, vol_attribute_show
, NULL
);
48 static struct device_attribute vol_alignment
=
49 __ATTR(alignment
, S_IRUGO
, vol_attribute_show
, NULL
);
50 static struct device_attribute vol_usable_eb_size
=
51 __ATTR(usable_eb_size
, S_IRUGO
, vol_attribute_show
, NULL
);
52 static struct device_attribute vol_data_bytes
=
53 __ATTR(data_bytes
, S_IRUGO
, vol_attribute_show
, NULL
);
54 static struct device_attribute vol_upd_marker
=
55 __ATTR(upd_marker
, S_IRUGO
, vol_attribute_show
, NULL
);
58 * "Show" method for files in '/<sysfs>/class/ubi/ubiX_Y/'.
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;
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.
70 static ssize_t
vol_attribute_show(struct device
*dev
,
71 struct device_attribute
*attr
, char *buf
)
74 struct ubi_volume
*vol
= container_of(dev
, struct ubi_volume
, dev
);
76 spin_lock(&vol
->ubi
->volumes_lock
);
78 spin_unlock(&vol
->ubi
->volumes_lock
);
81 if (attr
== &vol_reserved_ebs
)
82 ret
= sprintf(buf
, "%d\n", vol
->reserved_pebs
);
83 else if (attr
== &vol_type
) {
85 tp
= vol
->vol_type
== UBI_DYNAMIC_VOLUME
? "dynamic" : "static";
86 ret
= sprintf(buf
, "%s\n", tp
);
87 } else if (attr
== &vol_name
)
88 ret
= sprintf(buf
, "%s\n", vol
->name
);
89 else if (attr
== &vol_corrupted
)
90 ret
= sprintf(buf
, "%d\n", vol
->corrupted
);
91 else if (attr
== &vol_alignment
)
92 ret
= sprintf(buf
, "%d\n", vol
->alignment
);
93 else if (attr
== &vol_usable_eb_size
) {
94 ret
= sprintf(buf
, "%d\n", vol
->usable_leb_size
);
95 } else if (attr
== &vol_data_bytes
)
96 ret
= sprintf(buf
, "%lld\n", vol
->used_bytes
);
97 else if (attr
== &vol_upd_marker
)
98 ret
= sprintf(buf
, "%d\n", vol
->upd_marker
);
101 spin_unlock(&vol
->ubi
->volumes_lock
);
105 /* Release method for volume devices */
106 static void vol_release(struct device
*dev
)
108 struct ubi_volume
*vol
= container_of(dev
, struct ubi_volume
, dev
);
109 ubi_assert(vol
->removed
);
114 * volume_sysfs_init - initialize sysfs for new volume.
115 * @ubi: UBI device description object
116 * @vol: volume description object
118 * This function returns zero in case of success and a negative error code in
121 * Note, this function does not free allocated resources in case of failure -
122 * the caller does it. This is because this would cause release() here and the
125 static int volume_sysfs_init(struct ubi_device
*ubi
, struct ubi_volume
*vol
)
129 err
= device_create_file(&vol
->dev
, &vol_reserved_ebs
);
132 err
= device_create_file(&vol
->dev
, &vol_type
);
135 err
= device_create_file(&vol
->dev
, &vol_name
);
138 err
= device_create_file(&vol
->dev
, &vol_corrupted
);
141 err
= device_create_file(&vol
->dev
, &vol_alignment
);
144 err
= device_create_file(&vol
->dev
, &vol_usable_eb_size
);
147 err
= device_create_file(&vol
->dev
, &vol_data_bytes
);
150 err
= device_create_file(&vol
->dev
, &vol_upd_marker
);
157 * volume_sysfs_close - close sysfs for a volume.
158 * @vol: volume description object
160 static void volume_sysfs_close(struct ubi_volume
*vol
)
162 device_remove_file(&vol
->dev
, &vol_upd_marker
);
163 device_remove_file(&vol
->dev
, &vol_data_bytes
);
164 device_remove_file(&vol
->dev
, &vol_usable_eb_size
);
165 device_remove_file(&vol
->dev
, &vol_alignment
);
166 device_remove_file(&vol
->dev
, &vol_corrupted
);
167 device_remove_file(&vol
->dev
, &vol_name
);
168 device_remove_file(&vol
->dev
, &vol_type
);
169 device_remove_file(&vol
->dev
, &vol_reserved_ebs
);
170 device_unregister(&vol
->dev
);
174 * ubi_create_volume - create volume.
175 * @ubi: UBI device description object
176 * @req: volume creation request
178 * This function creates volume described by @req. If @req->vol_id id
179 * %UBI_VOL_NUM_AUTO, this function automatically assigne ID to the new volume
180 * and saves it in @req->vol_id. Returns zero in case of success and a negative
181 * error code in case of failure.
183 int ubi_create_volume(struct ubi_device
*ubi
, struct ubi_mkvol_req
*req
)
185 int i
, err
, vol_id
= req
->vol_id
;
186 struct ubi_volume
*vol
;
187 struct ubi_vtbl_record vtbl_rec
;
193 vol
= kzalloc(sizeof(struct ubi_volume
), GFP_KERNEL
);
197 spin_lock(&ubi
->volumes_lock
);
199 if (vol_id
== UBI_VOL_NUM_AUTO
) {
200 /* Find unused volume ID */
201 dbg_msg("search for vacant volume ID");
202 for (i
= 0; i
< ubi
->vtbl_slots
; i
++)
203 if (!ubi
->volumes
[i
]) {
208 if (vol_id
== UBI_VOL_NUM_AUTO
) {
209 dbg_err("out of volume IDs");
213 req
->vol_id
= vol_id
;
216 dbg_msg("volume ID %d, %llu bytes, type %d, name %s",
217 vol_id
, (unsigned long long)req
->bytes
,
218 (int)req
->vol_type
, req
->name
);
220 /* Ensure that this volume does not exist */
222 if (ubi
->volumes
[vol_id
]) {
223 dbg_err("volume %d already exists", vol_id
);
227 /* Ensure that the name is unique */
228 for (i
= 0; i
< ubi
->vtbl_slots
; i
++)
229 if (ubi
->volumes
[i
] &&
230 ubi
->volumes
[i
]->name_len
== req
->name_len
&&
231 !strcmp(ubi
->volumes
[i
]->name
, req
->name
)) {
232 dbg_err("volume \"%s\" exists (ID %d)", req
->name
, i
);
236 /* Calculate how many eraseblocks are requested */
237 vol
->usable_leb_size
= ubi
->leb_size
- ubi
->leb_size
% req
->alignment
;
239 if (do_div(bytes
, vol
->usable_leb_size
))
240 vol
->reserved_pebs
= 1;
241 vol
->reserved_pebs
+= bytes
;
243 /* Reserve physical eraseblocks */
244 if (vol
->reserved_pebs
> ubi
->avail_pebs
) {
245 dbg_err("not enough PEBs, only %d available", ubi
->avail_pebs
);
249 ubi
->avail_pebs
-= vol
->reserved_pebs
;
250 ubi
->rsvd_pebs
+= vol
->reserved_pebs
;
252 vol
->vol_id
= vol_id
;
253 vol
->alignment
= req
->alignment
;
254 vol
->data_pad
= ubi
->leb_size
% vol
->alignment
;
255 vol
->vol_type
= req
->vol_type
;
256 vol
->name_len
= req
->name_len
;
257 memcpy(vol
->name
, req
->name
, vol
->name_len
+ 1);
260 ubi
->volumes
[vol_id
] = vol
;
261 spin_unlock(&ubi
->volumes_lock
);
264 * Finish all pending erases because there may be some LEBs belonging
265 * to the same volume ID.
267 err
= ubi_wl_flush(ubi
);
271 vol
->eba_tbl
= kmalloc(vol
->reserved_pebs
* sizeof(int), GFP_KERNEL
);
277 for (i
= 0; i
< vol
->reserved_pebs
; i
++)
278 vol
->eba_tbl
[i
] = UBI_LEB_UNMAPPED
;
280 if (vol
->vol_type
== UBI_DYNAMIC_VOLUME
) {
281 vol
->used_ebs
= vol
->reserved_pebs
;
282 vol
->last_eb_bytes
= vol
->usable_leb_size
;
284 (long long)vol
->used_ebs
* vol
->usable_leb_size
;
286 bytes
= vol
->used_bytes
;
287 vol
->last_eb_bytes
= do_div(bytes
, vol
->usable_leb_size
);
288 vol
->used_ebs
= bytes
;
289 if (vol
->last_eb_bytes
)
292 vol
->last_eb_bytes
= vol
->usable_leb_size
;
295 /* Register character device for the volume */
296 cdev_init(&vol
->cdev
, &ubi_vol_cdev_operations
);
297 vol
->cdev
.owner
= THIS_MODULE
;
298 err
= cdev_add(&vol
->cdev
, MKDEV(ubi
->major
, vol_id
+ 1), 1);
300 ubi_err("cannot add character device for volume %d", vol_id
);
304 err
= ubi_create_gluebi(ubi
, vol
);
308 vol
->dev
.release
= vol_release
;
309 vol
->dev
.parent
= &ubi
->dev
;
310 vol
->dev
.devt
= MKDEV(ubi
->major
, vol
->vol_id
+ 1);
311 vol
->dev
.class = ubi_class
;
312 sprintf(&vol
->dev
.bus_id
[0], "%s_%d", ubi
->ubi_name
, vol
->vol_id
);
313 err
= device_register(&vol
->dev
);
317 err
= volume_sysfs_init(ubi
, vol
);
321 /* Fill volume table record */
322 memset(&vtbl_rec
, 0, sizeof(struct ubi_vtbl_record
));
323 vtbl_rec
.reserved_pebs
= cpu_to_be32(vol
->reserved_pebs
);
324 vtbl_rec
.alignment
= cpu_to_be32(vol
->alignment
);
325 vtbl_rec
.data_pad
= cpu_to_be32(vol
->data_pad
);
326 vtbl_rec
.name_len
= cpu_to_be16(vol
->name_len
);
327 if (vol
->vol_type
== UBI_DYNAMIC_VOLUME
)
328 vtbl_rec
.vol_type
= UBI_VID_DYNAMIC
;
330 vtbl_rec
.vol_type
= UBI_VID_STATIC
;
331 memcpy(vtbl_rec
.name
, vol
->name
, vol
->name_len
+ 1);
333 err
= ubi_change_vtbl_record(ubi
, vol_id
, &vtbl_rec
);
337 spin_lock(&ubi
->volumes_lock
);
340 spin_unlock(&ubi
->volumes_lock
);
342 paranoid_check_volumes(ubi
);
346 err
= ubi_destroy_gluebi(vol
);
348 cdev_del(&vol
->cdev
);
352 spin_lock(&ubi
->volumes_lock
);
353 ubi
->rsvd_pebs
-= vol
->reserved_pebs
;
354 ubi
->avail_pebs
+= vol
->reserved_pebs
;
355 ubi
->volumes
[vol_id
] = NULL
;
357 spin_unlock(&ubi
->volumes_lock
);
362 * We are registered, so @vol is destroyed in the release function and
363 * we have to de-initialize differently.
366 err
= ubi_destroy_gluebi(vol
);
367 cdev_del(&vol
->cdev
);
369 spin_lock(&ubi
->volumes_lock
);
370 ubi
->rsvd_pebs
-= vol
->reserved_pebs
;
371 ubi
->avail_pebs
+= vol
->reserved_pebs
;
372 ubi
->volumes
[vol_id
] = NULL
;
373 spin_unlock(&ubi
->volumes_lock
);
374 volume_sysfs_close(vol
);
379 * ubi_remove_volume - remove volume.
380 * @desc: volume descriptor
382 * This function removes volume described by @desc. The volume has to be opened
383 * in "exclusive" mode. Returns zero in case of success and a negative error
384 * code in case of failure.
386 int ubi_remove_volume(struct ubi_volume_desc
*desc
)
388 struct ubi_volume
*vol
= desc
->vol
;
389 struct ubi_device
*ubi
= vol
->ubi
;
390 int i
, err
, vol_id
= vol
->vol_id
, reserved_pebs
= vol
->reserved_pebs
;
392 dbg_msg("remove UBI volume %d", vol_id
);
393 ubi_assert(desc
->mode
== UBI_EXCLUSIVE
);
394 ubi_assert(vol
== ubi
->volumes
[vol_id
]);
399 err
= ubi_destroy_gluebi(vol
);
403 err
= ubi_change_vtbl_record(ubi
, vol_id
, NULL
);
407 for (i
= 0; i
< vol
->reserved_pebs
; i
++) {
408 err
= ubi_eba_unmap_leb(ubi
, vol_id
, i
);
413 spin_lock(&ubi
->volumes_lock
);
415 ubi
->volumes
[vol_id
] = NULL
;
416 spin_unlock(&ubi
->volumes_lock
);
420 cdev_del(&vol
->cdev
);
421 volume_sysfs_close(vol
);
424 spin_lock(&ubi
->volumes_lock
);
425 ubi
->rsvd_pebs
-= reserved_pebs
;
426 ubi
->avail_pebs
+= reserved_pebs
;
427 i
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
;
429 i
= ubi
->avail_pebs
>= i
? i
: ubi
->avail_pebs
;
430 ubi
->avail_pebs
-= i
;
432 ubi
->beb_rsvd_pebs
+= i
;
434 ubi_msg("reserve more %d PEBs", i
);
437 spin_unlock(&ubi
->volumes_lock
);
439 paranoid_check_volumes(ubi
);
440 module_put(THIS_MODULE
);
445 * ubi_resize_volume - re-size volume.
446 * @desc: volume descriptor
447 * @reserved_pebs: new size in physical eraseblocks
449 * This function returns zero in case of success, and a negative error code in
452 int ubi_resize_volume(struct ubi_volume_desc
*desc
, int reserved_pebs
)
454 int i
, err
, pebs
, *new_mapping
;
455 struct ubi_volume
*vol
= desc
->vol
;
456 struct ubi_device
*ubi
= vol
->ubi
;
457 struct ubi_vtbl_record vtbl_rec
;
458 int vol_id
= vol
->vol_id
;
463 dbg_msg("re-size volume %d to from %d to %d PEBs",
464 vol_id
, vol
->reserved_pebs
, reserved_pebs
);
465 ubi_assert(desc
->mode
== UBI_EXCLUSIVE
);
466 ubi_assert(vol
== ubi
->volumes
[vol_id
]);
468 if (vol
->vol_type
== UBI_STATIC_VOLUME
&&
469 reserved_pebs
< vol
->used_ebs
) {
470 dbg_err("too small size %d, %d LEBs contain data",
471 reserved_pebs
, vol
->used_ebs
);
475 /* If the size is the same, we have nothing to do */
476 if (reserved_pebs
== vol
->reserved_pebs
)
479 new_mapping
= kmalloc(reserved_pebs
* sizeof(int), GFP_KERNEL
);
483 for (i
= 0; i
< reserved_pebs
; i
++)
484 new_mapping
[i
] = UBI_LEB_UNMAPPED
;
486 /* Reserve physical eraseblocks */
487 pebs
= reserved_pebs
- vol
->reserved_pebs
;
489 spin_lock(&ubi
->volumes_lock
);
490 if (pebs
> ubi
->avail_pebs
) {
491 dbg_err("not enough PEBs: requested %d, available %d",
492 pebs
, ubi
->avail_pebs
);
493 spin_unlock(&ubi
->volumes_lock
);
497 ubi
->avail_pebs
-= pebs
;
498 ubi
->rsvd_pebs
+= pebs
;
499 for (i
= 0; i
< vol
->reserved_pebs
; i
++)
500 new_mapping
[i
] = vol
->eba_tbl
[i
];
502 vol
->eba_tbl
= new_mapping
;
503 spin_unlock(&ubi
->volumes_lock
);
506 /* Change volume table record */
507 memcpy(&vtbl_rec
, &ubi
->vtbl
[vol_id
], sizeof(struct ubi_vtbl_record
));
508 vtbl_rec
.reserved_pebs
= cpu_to_be32(reserved_pebs
);
509 err
= ubi_change_vtbl_record(ubi
, vol_id
, &vtbl_rec
);
514 for (i
= 0; i
< -pebs
; i
++) {
515 err
= ubi_eba_unmap_leb(ubi
, vol_id
, reserved_pebs
+ i
);
519 spin_lock(&ubi
->volumes_lock
);
520 ubi
->rsvd_pebs
+= pebs
;
521 ubi
->avail_pebs
-= pebs
;
522 pebs
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
;
524 pebs
= ubi
->avail_pebs
>= pebs
? pebs
: ubi
->avail_pebs
;
525 ubi
->avail_pebs
-= pebs
;
526 ubi
->rsvd_pebs
+= pebs
;
527 ubi
->beb_rsvd_pebs
+= pebs
;
529 ubi_msg("reserve more %d PEBs", pebs
);
531 for (i
= 0; i
< reserved_pebs
; i
++)
532 new_mapping
[i
] = vol
->eba_tbl
[i
];
534 vol
->eba_tbl
= new_mapping
;
535 spin_unlock(&ubi
->volumes_lock
);
538 vol
->reserved_pebs
= reserved_pebs
;
539 if (vol
->vol_type
== UBI_DYNAMIC_VOLUME
) {
540 vol
->used_ebs
= reserved_pebs
;
541 vol
->last_eb_bytes
= vol
->usable_leb_size
;
543 (long long)vol
->used_ebs
* vol
->usable_leb_size
;
546 paranoid_check_volumes(ubi
);
551 spin_lock(&ubi
->volumes_lock
);
552 ubi
->rsvd_pebs
-= pebs
;
553 ubi
->avail_pebs
+= pebs
;
554 spin_unlock(&ubi
->volumes_lock
);
562 * ubi_add_volume - add volume.
563 * @ubi: UBI device description object
566 * This function adds an existin volume and initializes all its data
567 * structures. Returnes zero in case of success and a negative error code in
570 int ubi_add_volume(struct ubi_device
*ubi
, int vol_id
)
573 struct ubi_volume
*vol
= ubi
->volumes
[vol_id
];
575 dbg_msg("add volume %d", vol_id
);
576 ubi_dbg_dump_vol_info(vol
);
579 /* Register character device for the volume */
580 cdev_init(&vol
->cdev
, &ubi_vol_cdev_operations
);
581 vol
->cdev
.owner
= THIS_MODULE
;
582 err
= cdev_add(&vol
->cdev
, MKDEV(ubi
->major
, vol
->vol_id
+ 1), 1);
584 ubi_err("cannot add character device for volume %d", vol_id
);
588 err
= ubi_create_gluebi(ubi
, vol
);
592 vol
->dev
.release
= vol_release
;
593 vol
->dev
.parent
= &ubi
->dev
;
594 vol
->dev
.devt
= MKDEV(ubi
->major
, vol
->vol_id
+ 1);
595 vol
->dev
.class = ubi_class
;
596 sprintf(&vol
->dev
.bus_id
[0], "%s_%d", ubi
->ubi_name
, vol
->vol_id
);
597 err
= device_register(&vol
->dev
);
601 err
= volume_sysfs_init(ubi
, vol
);
603 cdev_del(&vol
->cdev
);
604 err
= ubi_destroy_gluebi(vol
);
605 volume_sysfs_close(vol
);
609 paranoid_check_volumes(ubi
);
613 err
= ubi_destroy_gluebi(vol
);
615 cdev_del(&vol
->cdev
);
620 * ubi_free_volume - free volume.
621 * @ubi: UBI device description object
624 * This function frees all resources for volume @vol_id but does not remove it.
625 * Used only when the UBI device is detached.
627 void ubi_free_volume(struct ubi_device
*ubi
, int vol_id
)
630 struct ubi_volume
*vol
= ubi
->volumes
[vol_id
];
632 dbg_msg("free volume %d", vol_id
);
636 err
= ubi_destroy_gluebi(vol
);
637 ubi
->volumes
[vol_id
] = NULL
;
638 cdev_del(&vol
->cdev
);
639 volume_sysfs_close(vol
);
642 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
645 * paranoid_check_volume - check volume information.
646 * @ubi: UBI device description object
649 static void paranoid_check_volume(struct ubi_device
*ubi
, int vol_id
)
651 int idx
= vol_id2idx(ubi
, vol_id
);
652 int reserved_pebs
, alignment
, data_pad
, vol_type
, name_len
, upd_marker
;
653 const struct ubi_volume
*vol
;
657 spin_lock(&ubi
->volumes_lock
);
658 reserved_pebs
= be32_to_cpu(ubi
->vtbl
[vol_id
].reserved_pebs
);
659 vol
= ubi
->volumes
[idx
];
663 ubi_err("no volume info, but volume exists");
666 spin_unlock(&ubi
->volumes_lock
);
670 if (vol
->exclusive
) {
672 * The volume may be being created at the moment, do not check
673 * it (e.g., it may be in the middle of ubi_create_volume().
675 spin_unlock(&ubi
->volumes_lock
);
679 if (vol
->reserved_pebs
< 0 || vol
->alignment
< 0 || vol
->data_pad
< 0 ||
681 ubi_err("negative values");
684 if (vol
->alignment
> ubi
->leb_size
|| vol
->alignment
== 0) {
685 ubi_err("bad alignment");
689 n
= vol
->alignment
% ubi
->min_io_size
;
690 if (vol
->alignment
!= 1 && n
) {
691 ubi_err("alignment is not multiple of min I/O unit");
695 n
= ubi
->leb_size
% vol
->alignment
;
696 if (vol
->data_pad
!= n
) {
697 ubi_err("bad data_pad, has to be %lld", n
);
701 if (vol
->vol_type
!= UBI_DYNAMIC_VOLUME
&&
702 vol
->vol_type
!= UBI_STATIC_VOLUME
) {
703 ubi_err("bad vol_type");
707 if (vol
->upd_marker
!= 0 && vol
->upd_marker
!= 1) {
708 ubi_err("bad upd_marker");
712 if (vol
->upd_marker
&& vol
->corrupted
) {
713 dbg_err("update marker and corrupted simultaneously");
717 if (vol
->reserved_pebs
> ubi
->good_peb_count
) {
718 ubi_err("too large reserved_pebs");
722 n
= ubi
->leb_size
- vol
->data_pad
;
723 if (vol
->usable_leb_size
!= ubi
->leb_size
- vol
->data_pad
) {
724 ubi_err("bad usable_leb_size, has to be %lld", n
);
728 if (vol
->name_len
> UBI_VOL_NAME_MAX
) {
729 ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX
);
734 ubi_err("NULL volume name");
738 n
= strnlen(vol
->name
, vol
->name_len
+ 1);
739 if (n
!= vol
->name_len
) {
740 ubi_err("bad name_len %lld", n
);
744 n
= (long long)vol
->used_ebs
* vol
->usable_leb_size
;
745 if (vol
->vol_type
== UBI_DYNAMIC_VOLUME
) {
746 if (vol
->corrupted
!= 0) {
747 ubi_err("corrupted dynamic volume");
750 if (vol
->used_ebs
!= vol
->reserved_pebs
) {
751 ubi_err("bad used_ebs");
754 if (vol
->last_eb_bytes
!= vol
->usable_leb_size
) {
755 ubi_err("bad last_eb_bytes");
758 if (vol
->used_bytes
!= n
) {
759 ubi_err("bad used_bytes");
763 if (vol
->corrupted
!= 0 && vol
->corrupted
!= 1) {
764 ubi_err("bad corrupted");
767 if (vol
->used_ebs
< 0 || vol
->used_ebs
> vol
->reserved_pebs
) {
768 ubi_err("bad used_ebs");
771 if (vol
->last_eb_bytes
< 0 ||
772 vol
->last_eb_bytes
> vol
->usable_leb_size
) {
773 ubi_err("bad last_eb_bytes");
776 if (vol
->used_bytes
< 0 || vol
->used_bytes
> n
||
777 vol
->used_bytes
< n
- vol
->usable_leb_size
) {
778 ubi_err("bad used_bytes");
783 alignment
= be32_to_cpu(ubi
->vtbl
[vol_id
].alignment
);
784 data_pad
= be32_to_cpu(ubi
->vtbl
[vol_id
].data_pad
);
785 name_len
= be16_to_cpu(ubi
->vtbl
[vol_id
].name_len
);
786 upd_marker
= ubi
->vtbl
[vol_id
].upd_marker
;
787 name
= &ubi
->vtbl
[vol_id
].name
[0];
788 if (ubi
->vtbl
[vol_id
].vol_type
== UBI_VID_DYNAMIC
)
789 vol_type
= UBI_DYNAMIC_VOLUME
;
791 vol_type
= UBI_STATIC_VOLUME
;
793 if (alignment
!= vol
->alignment
|| data_pad
!= vol
->data_pad
||
794 upd_marker
!= vol
->upd_marker
|| vol_type
!= vol
->vol_type
||
795 name_len
!= vol
->name_len
|| strncmp(name
, vol
->name
, name_len
)) {
796 ubi_err("volume info is different");
800 spin_unlock(&ubi
->volumes_lock
);
804 ubi_err("paranoid check failed for volume %d", vol_id
);
805 ubi_dbg_dump_vol_info(vol
);
806 ubi_dbg_dump_vtbl_record(&ubi
->vtbl
[vol_id
], vol_id
);
807 spin_unlock(&ubi
->volumes_lock
);
812 * paranoid_check_volumes - check information about all volumes.
813 * @ubi: UBI device description object
815 static void paranoid_check_volumes(struct ubi_device
*ubi
)
819 mutex_lock(&ubi
->vtbl_mutex
);
820 for (i
= 0; i
< ubi
->vtbl_slots
; i
++)
821 paranoid_check_volume(ubi
, i
);
822 mutex_unlock(&ubi
->vtbl_mutex
);