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