blob: ec2dd3c65c43bc92c656af60ef8bec934370e3f9 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
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' */
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030040static struct device_attribute attr_vol_reserved_ebs =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040041 __ATTR(reserved_ebs, S_IRUGO, vol_attribute_show, NULL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030042static struct device_attribute attr_vol_type =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040043 __ATTR(type, S_IRUGO, vol_attribute_show, NULL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030044static struct device_attribute attr_vol_name =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040045 __ATTR(name, S_IRUGO, vol_attribute_show, NULL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030046static struct device_attribute attr_vol_corrupted =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040047 __ATTR(corrupted, S_IRUGO, vol_attribute_show, NULL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030048static struct device_attribute attr_vol_alignment =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040049 __ATTR(alignment, S_IRUGO, vol_attribute_show, NULL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030050static struct device_attribute attr_vol_usable_eb_size =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040051 __ATTR(usable_eb_size, S_IRUGO, vol_attribute_show, NULL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030052static struct device_attribute attr_vol_data_bytes =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040053 __ATTR(data_bytes, S_IRUGO, vol_attribute_show, NULL);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030054static struct device_attribute attr_vol_upd_marker =
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040055 __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{
Jesper Juhl54b2c8f2007-12-13 23:53:08 +010073 int ret = -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040074 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);
Jesper Juhl54b2c8f2007-12-13 23:53:08 +010079 return ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040080 }
Artem Bityutskiy732aeac2007-12-15 15:09:07 +020081
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030082 if (attr == &attr_vol_reserved_ebs)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040083 ret = sprintf(buf, "%d\n", vol->reserved_pebs);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030084 else if (attr == &attr_vol_type) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040085 const char *tp;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030086
87 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
88 tp = "dynamic";
89 else
90 tp = "static";
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040091 ret = sprintf(buf, "%s\n", tp);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030092 } else if (attr == &attr_vol_name)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040093 ret = sprintf(buf, "%s\n", vol->name);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030094 else if (attr == &attr_vol_corrupted)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040095 ret = sprintf(buf, "%d\n", vol->corrupted);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030096 else if (attr == &attr_vol_alignment)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040097 ret = sprintf(buf, "%d\n", vol->alignment);
Artem Bityutskiy732aeac2007-12-15 15:09:07 +020098 else if (attr == &attr_vol_usable_eb_size)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040099 ret = sprintf(buf, "%d\n", vol->usable_leb_size);
Artem Bityutskiy732aeac2007-12-15 15:09:07 +0200100 else if (attr == &attr_vol_data_bytes)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400101 ret = sprintf(buf, "%lld\n", vol->used_bytes);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300102 else if (attr == &attr_vol_upd_marker)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400103 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);
Artem Bityutskiy732aeac2007-12-15 15:09:07 +0200114
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400115 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
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300135 err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400136 if (err)
137 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300138 err = device_create_file(&vol->dev, &attr_vol_type);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400139 if (err)
140 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300141 err = device_create_file(&vol->dev, &attr_vol_name);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400142 if (err)
143 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300144 err = device_create_file(&vol->dev, &attr_vol_corrupted);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400145 if (err)
146 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300147 err = device_create_file(&vol->dev, &attr_vol_alignment);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400148 if (err)
149 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300150 err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400151 if (err)
152 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300153 err = device_create_file(&vol->dev, &attr_vol_data_bytes);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400154 if (err)
155 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300156 err = device_create_file(&vol->dev, &attr_vol_upd_marker);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400157 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{
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300168 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);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400176 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;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200195 dev_t dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400196
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
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200204 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400205 spin_lock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400206 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 &&
Artem Bityutskiy94784d92007-06-18 12:06:30 +0300238 !strcmp(ubi->volumes[i]->name, req->name)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400239 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);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400253 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;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300290 vol->used_bytes =
291 (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400292 } 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;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200305 dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
306 err = cdev_add(&vol->cdev, dev, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400307 if (err) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200308 ubi_err("cannot add character device");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400309 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;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200318 vol->dev.devt = dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400319 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);
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200322 if (err) {
323 ubi_err("cannot register device");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400324 goto out_gluebi;
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200325 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400326
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));
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300333 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);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400337 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);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200353 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400354 return 0;
355
356out_gluebi:
357 err = ubi_destroy_gluebi(vol);
358out_cdev:
359 cdev_del(&vol->cdev);
360out_mapping:
361 kfree(vol->eba_tbl);
362out_acc:
363 spin_lock(&ubi->volumes_lock);
364 ubi->rsvd_pebs -= vol->reserved_pebs;
365 ubi->avail_pebs += vol->reserved_pebs;
Artem Bityutskiy94784d92007-06-18 12:06:30 +0300366 ubi->volumes[vol_id] = NULL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400367out_unlock:
368 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200369 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400370 kfree(vol);
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200371 ubi_err("cannot create volume %d, error %d", vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400372 return err;
373
374 /*
375 * We are registered, so @vol is destroyed in the release function and
376 * we have to de-initialize differently.
377 */
378out_sysfs:
379 err = ubi_destroy_gluebi(vol);
380 cdev_del(&vol->cdev);
381 kfree(vol->eba_tbl);
382 spin_lock(&ubi->volumes_lock);
383 ubi->rsvd_pebs -= vol->reserved_pebs;
384 ubi->avail_pebs += vol->reserved_pebs;
Artem Bityutskiy94784d92007-06-18 12:06:30 +0300385 ubi->volumes[vol_id] = NULL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400386 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200387 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400388 volume_sysfs_close(vol);
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200389 ubi_err("cannot create volume %d, error %d", vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400390 return err;
391}
392
393/**
394 * ubi_remove_volume - remove volume.
395 * @desc: volume descriptor
396 *
397 * This function removes volume described by @desc. The volume has to be opened
398 * in "exclusive" mode. Returns zero in case of success and a negative error
399 * code in case of failure.
400 */
401int ubi_remove_volume(struct ubi_volume_desc *desc)
402{
403 struct ubi_volume *vol = desc->vol;
404 struct ubi_device *ubi = vol->ubi;
405 int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
406
407 dbg_msg("remove UBI volume %d", vol_id);
408 ubi_assert(desc->mode == UBI_EXCLUSIVE);
409 ubi_assert(vol == ubi->volumes[vol_id]);
410
411 if (ubi->ro_mode)
412 return -EROFS;
413
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200414 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400415 err = ubi_destroy_gluebi(vol);
416 if (err)
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200417 goto out;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400418
419 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
420 if (err)
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200421 goto out;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400422
423 for (i = 0; i < vol->reserved_pebs; i++) {
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200424 err = ubi_eba_unmap_leb(ubi, vol, i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400425 if (err)
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200426 goto out;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400427 }
428
429 spin_lock(&ubi->volumes_lock);
430 vol->removed = 1;
431 ubi->volumes[vol_id] = NULL;
432 spin_unlock(&ubi->volumes_lock);
433
434 kfree(vol->eba_tbl);
435 vol->eba_tbl = NULL;
436 cdev_del(&vol->cdev);
437 volume_sysfs_close(vol);
438 kfree(desc);
439
440 spin_lock(&ubi->volumes_lock);
441 ubi->rsvd_pebs -= reserved_pebs;
442 ubi->avail_pebs += reserved_pebs;
443 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
444 if (i > 0) {
445 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
446 ubi->avail_pebs -= i;
447 ubi->rsvd_pebs += i;
448 ubi->beb_rsvd_pebs += i;
449 if (i > 0)
450 ubi_msg("reserve more %d PEBs", i);
451 }
452 ubi->vol_count -= 1;
453 spin_unlock(&ubi->volumes_lock);
454
455 paranoid_check_volumes(ubi);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200456 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400457 module_put(THIS_MODULE);
458 return 0;
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200459
460out:
461 mutex_unlock(&ubi->volumes_mutex);
462 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400463}
464
465/**
466 * ubi_resize_volume - re-size volume.
467 * @desc: volume descriptor
468 * @reserved_pebs: new size in physical eraseblocks
469 *
470 * This function returns zero in case of success, and a negative error code in
471 * case of failure.
472 */
473int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
474{
475 int i, err, pebs, *new_mapping;
476 struct ubi_volume *vol = desc->vol;
477 struct ubi_device *ubi = vol->ubi;
478 struct ubi_vtbl_record vtbl_rec;
479 int vol_id = vol->vol_id;
480
481 if (ubi->ro_mode)
482 return -EROFS;
483
484 dbg_msg("re-size volume %d to from %d to %d PEBs",
485 vol_id, vol->reserved_pebs, reserved_pebs);
486 ubi_assert(desc->mode == UBI_EXCLUSIVE);
487 ubi_assert(vol == ubi->volumes[vol_id]);
488
489 if (vol->vol_type == UBI_STATIC_VOLUME &&
490 reserved_pebs < vol->used_ebs) {
491 dbg_err("too small size %d, %d LEBs contain data",
492 reserved_pebs, vol->used_ebs);
493 return -EINVAL;
494 }
495
496 /* If the size is the same, we have nothing to do */
497 if (reserved_pebs == vol->reserved_pebs)
498 return 0;
499
500 new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
501 if (!new_mapping)
502 return -ENOMEM;
503
504 for (i = 0; i < reserved_pebs; i++)
505 new_mapping[i] = UBI_LEB_UNMAPPED;
506
507 /* Reserve physical eraseblocks */
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200508 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400509 pebs = reserved_pebs - vol->reserved_pebs;
510 if (pebs > 0) {
511 spin_lock(&ubi->volumes_lock);
512 if (pebs > ubi->avail_pebs) {
513 dbg_err("not enough PEBs: requested %d, available %d",
514 pebs, ubi->avail_pebs);
515 spin_unlock(&ubi->volumes_lock);
516 err = -ENOSPC;
517 goto out_free;
518 }
519 ubi->avail_pebs -= pebs;
520 ubi->rsvd_pebs += pebs;
521 for (i = 0; i < vol->reserved_pebs; i++)
522 new_mapping[i] = vol->eba_tbl[i];
523 kfree(vol->eba_tbl);
524 vol->eba_tbl = new_mapping;
525 spin_unlock(&ubi->volumes_lock);
526 }
527
528 /* Change volume table record */
529 memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300530 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400531 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
532 if (err)
533 goto out_acc;
534
535 if (pebs < 0) {
536 for (i = 0; i < -pebs; i++) {
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200537 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400538 if (err)
539 goto out_acc;
540 }
541 spin_lock(&ubi->volumes_lock);
542 ubi->rsvd_pebs += pebs;
543 ubi->avail_pebs -= pebs;
544 pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
545 if (pebs > 0) {
546 pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
547 ubi->avail_pebs -= pebs;
548 ubi->rsvd_pebs += pebs;
549 ubi->beb_rsvd_pebs += pebs;
550 if (pebs > 0)
551 ubi_msg("reserve more %d PEBs", pebs);
552 }
553 for (i = 0; i < reserved_pebs; i++)
554 new_mapping[i] = vol->eba_tbl[i];
555 kfree(vol->eba_tbl);
556 vol->eba_tbl = new_mapping;
557 spin_unlock(&ubi->volumes_lock);
558 }
559
560 vol->reserved_pebs = reserved_pebs;
561 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
562 vol->used_ebs = reserved_pebs;
563 vol->last_eb_bytes = vol->usable_leb_size;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300564 vol->used_bytes =
565 (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400566 }
567
568 paranoid_check_volumes(ubi);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200569 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400570 return 0;
571
572out_acc:
573 if (pebs > 0) {
574 spin_lock(&ubi->volumes_lock);
575 ubi->rsvd_pebs -= pebs;
576 ubi->avail_pebs += pebs;
577 spin_unlock(&ubi->volumes_lock);
578 }
579out_free:
580 kfree(new_mapping);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200581 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400582 return err;
583}
584
585/**
586 * ubi_add_volume - add volume.
587 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200588 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400589 *
590 * This function adds an existin volume and initializes all its data
591 * structures. Returnes zero in case of success and a negative error code in
592 * case of failure.
593 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200594int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400595{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200596 int err, vol_id = vol->vol_id;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200597 dev_t dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400598
599 dbg_msg("add volume %d", vol_id);
600 ubi_dbg_dump_vol_info(vol);
601 ubi_assert(vol);
602
603 /* Register character device for the volume */
604 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
605 vol->cdev.owner = THIS_MODULE;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200606 dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
607 err = cdev_add(&vol->cdev, dev, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400608 if (err) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200609 ubi_err("cannot add character device for volume %d, error %d",
610 vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400611 return err;
612 }
613
614 err = ubi_create_gluebi(ubi, vol);
615 if (err)
616 goto out_cdev;
617
618 vol->dev.release = vol_release;
619 vol->dev.parent = &ubi->dev;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200620 vol->dev.devt = dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400621 vol->dev.class = ubi_class;
622 sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
623 err = device_register(&vol->dev);
624 if (err)
625 goto out_gluebi;
626
627 err = volume_sysfs_init(ubi, vol);
628 if (err) {
629 cdev_del(&vol->cdev);
630 err = ubi_destroy_gluebi(vol);
631 volume_sysfs_close(vol);
632 return err;
633 }
634
635 paranoid_check_volumes(ubi);
636 return 0;
637
638out_gluebi:
639 err = ubi_destroy_gluebi(vol);
640out_cdev:
641 cdev_del(&vol->cdev);
642 return err;
643}
644
645/**
646 * ubi_free_volume - free volume.
647 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200648 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400649 *
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200650 * This function frees all resources for volume @vol but does not remove it.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400651 * Used only when the UBI device is detached.
652 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200653void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400654{
655 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400656
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200657 dbg_msg("free volume %d", vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400658 ubi_assert(vol);
659
660 vol->removed = 1;
661 err = ubi_destroy_gluebi(vol);
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200662 ubi->volumes[vol->vol_id] = NULL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400663 cdev_del(&vol->cdev);
664 volume_sysfs_close(vol);
665}
666
667#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
668
669/**
670 * paranoid_check_volume - check volume information.
671 * @ubi: UBI device description object
672 * @vol_id: volume ID
673 */
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300674static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400675{
676 int idx = vol_id2idx(ubi, vol_id);
677 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300678 const struct ubi_volume *vol;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400679 long long n;
680 const char *name;
681
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300682 spin_lock(&ubi->volumes_lock);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300683 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300684 vol = ubi->volumes[idx];
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400685
686 if (!vol) {
687 if (reserved_pebs) {
688 ubi_err("no volume info, but volume exists");
689 goto fail;
690 }
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300691 spin_unlock(&ubi->volumes_lock);
692 return;
693 }
694
695 if (vol->exclusive) {
696 /*
697 * The volume may be being created at the moment, do not check
698 * it (e.g., it may be in the middle of ubi_create_volume().
699 */
700 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400701 return;
702 }
703
704 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
705 vol->name_len < 0) {
706 ubi_err("negative values");
707 goto fail;
708 }
709 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
710 ubi_err("bad alignment");
711 goto fail;
712 }
713
714 n = vol->alignment % ubi->min_io_size;
715 if (vol->alignment != 1 && n) {
716 ubi_err("alignment is not multiple of min I/O unit");
717 goto fail;
718 }
719
720 n = ubi->leb_size % vol->alignment;
721 if (vol->data_pad != n) {
722 ubi_err("bad data_pad, has to be %lld", n);
723 goto fail;
724 }
725
726 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
727 vol->vol_type != UBI_STATIC_VOLUME) {
728 ubi_err("bad vol_type");
729 goto fail;
730 }
731
732 if (vol->upd_marker != 0 && vol->upd_marker != 1) {
733 ubi_err("bad upd_marker");
734 goto fail;
735 }
736
737 if (vol->upd_marker && vol->corrupted) {
738 dbg_err("update marker and corrupted simultaneously");
739 goto fail;
740 }
741
742 if (vol->reserved_pebs > ubi->good_peb_count) {
743 ubi_err("too large reserved_pebs");
744 goto fail;
745 }
746
747 n = ubi->leb_size - vol->data_pad;
748 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
749 ubi_err("bad usable_leb_size, has to be %lld", n);
750 goto fail;
751 }
752
753 if (vol->name_len > UBI_VOL_NAME_MAX) {
754 ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
755 goto fail;
756 }
757
758 if (!vol->name) {
759 ubi_err("NULL volume name");
760 goto fail;
761 }
762
763 n = strnlen(vol->name, vol->name_len + 1);
764 if (n != vol->name_len) {
765 ubi_err("bad name_len %lld", n);
766 goto fail;
767 }
768
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300769 n = (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400770 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
771 if (vol->corrupted != 0) {
772 ubi_err("corrupted dynamic volume");
773 goto fail;
774 }
775 if (vol->used_ebs != vol->reserved_pebs) {
776 ubi_err("bad used_ebs");
777 goto fail;
778 }
779 if (vol->last_eb_bytes != vol->usable_leb_size) {
780 ubi_err("bad last_eb_bytes");
781 goto fail;
782 }
783 if (vol->used_bytes != n) {
784 ubi_err("bad used_bytes");
785 goto fail;
786 }
787 } else {
788 if (vol->corrupted != 0 && vol->corrupted != 1) {
789 ubi_err("bad corrupted");
790 goto fail;
791 }
792 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
793 ubi_err("bad used_ebs");
794 goto fail;
795 }
796 if (vol->last_eb_bytes < 0 ||
797 vol->last_eb_bytes > vol->usable_leb_size) {
798 ubi_err("bad last_eb_bytes");
799 goto fail;
800 }
801 if (vol->used_bytes < 0 || vol->used_bytes > n ||
802 vol->used_bytes < n - vol->usable_leb_size) {
803 ubi_err("bad used_bytes");
804 goto fail;
805 }
806 }
807
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300808 alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
809 data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
810 name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400811 upd_marker = ubi->vtbl[vol_id].upd_marker;
812 name = &ubi->vtbl[vol_id].name[0];
813 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
814 vol_type = UBI_DYNAMIC_VOLUME;
815 else
816 vol_type = UBI_STATIC_VOLUME;
817
818 if (alignment != vol->alignment || data_pad != vol->data_pad ||
819 upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
820 name_len!= vol->name_len || strncmp(name, vol->name, name_len)) {
821 ubi_err("volume info is different");
822 goto fail;
823 }
824
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300825 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400826 return;
827
828fail:
Artem Bityutskiy94784d92007-06-18 12:06:30 +0300829 ubi_err("paranoid check failed for volume %d", vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400830 ubi_dbg_dump_vol_info(vol);
831 ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300832 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833 BUG();
834}
835
836/**
837 * paranoid_check_volumes - check information about all volumes.
838 * @ubi: UBI device description object
839 */
840static void paranoid_check_volumes(struct ubi_device *ubi)
841{
842 int i;
843
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400844 for (i = 0; i < ubi->vtbl_slots; i++)
845 paranoid_check_volume(ubi, i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400846}
847#endif