blob: 3ed63dc37386daaff1decb868f7902f31dbeb8f7 [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 *
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +020066 * In this situation, this function will return %-ENODEV because it will find
67 * out that the volume was removed from the @ubi->volumes array.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040068 */
69static ssize_t vol_attribute_show(struct device *dev,
70 struct device_attribute *attr, char *buf)
71{
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +020072 int ret;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040073 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +020074 struct ubi_device *ubi = vol->ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040075
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +020076 spin_lock(&ubi->volumes_lock);
77 if (!ubi->volumes[vol->vol_id]) {
78 spin_unlock(&ubi->volumes_lock);
79 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040080 }
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +020081 /* Take a reference to prevent volume removal */
82 vol->ref_count += 1;
83 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiy732aeac2007-12-15 15:09:07 +020084
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030085 if (attr == &attr_vol_reserved_ebs)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040086 ret = sprintf(buf, "%d\n", vol->reserved_pebs);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030087 else if (attr == &attr_vol_type) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040088 const char *tp;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030089
90 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
91 tp = "dynamic";
92 else
93 tp = "static";
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040094 ret = sprintf(buf, "%s\n", tp);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030095 } else if (attr == &attr_vol_name)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040096 ret = sprintf(buf, "%s\n", vol->name);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030097 else if (attr == &attr_vol_corrupted)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040098 ret = sprintf(buf, "%d\n", vol->corrupted);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +030099 else if (attr == &attr_vol_alignment)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400100 ret = sprintf(buf, "%d\n", vol->alignment);
Artem Bityutskiy732aeac2007-12-15 15:09:07 +0200101 else if (attr == &attr_vol_usable_eb_size)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400102 ret = sprintf(buf, "%d\n", vol->usable_leb_size);
Artem Bityutskiy732aeac2007-12-15 15:09:07 +0200103 else if (attr == &attr_vol_data_bytes)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400104 ret = sprintf(buf, "%lld\n", vol->used_bytes);
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300105 else if (attr == &attr_vol_upd_marker)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400106 ret = sprintf(buf, "%d\n", vol->upd_marker);
107 else
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200108 /* This must be a bug */
109 ret = -EINVAL;
110
111 spin_lock(&ubi->volumes_lock);
112 vol->ref_count -= 1;
113 ubi_assert(vol->ref_count >= 0);
114 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400115 return ret;
116}
117
118/* Release method for volume devices */
119static void vol_release(struct device *dev)
120{
121 struct ubi_volume *vol = container_of(dev, struct ubi_volume, dev);
Artem Bityutskiy732aeac2007-12-15 15:09:07 +0200122
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400123 kfree(vol);
124}
125
126/**
127 * volume_sysfs_init - initialize sysfs for new volume.
128 * @ubi: UBI device description object
129 * @vol: volume description object
130 *
131 * This function returns zero in case of success and a negative error code in
132 * case of failure.
133 *
134 * Note, this function does not free allocated resources in case of failure -
135 * the caller does it. This is because this would cause release() here and the
136 * caller would oops.
137 */
138static int volume_sysfs_init(struct ubi_device *ubi, struct ubi_volume *vol)
139{
140 int err;
141
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300142 err = device_create_file(&vol->dev, &attr_vol_reserved_ebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400143 if (err)
144 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300145 err = device_create_file(&vol->dev, &attr_vol_type);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400146 if (err)
147 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300148 err = device_create_file(&vol->dev, &attr_vol_name);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400149 if (err)
150 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300151 err = device_create_file(&vol->dev, &attr_vol_corrupted);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400152 if (err)
153 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300154 err = device_create_file(&vol->dev, &attr_vol_alignment);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400155 if (err)
156 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300157 err = device_create_file(&vol->dev, &attr_vol_usable_eb_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400158 if (err)
159 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300160 err = device_create_file(&vol->dev, &attr_vol_data_bytes);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400161 if (err)
162 return err;
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300163 err = device_create_file(&vol->dev, &attr_vol_upd_marker);
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200164 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400165}
166
167/**
168 * volume_sysfs_close - close sysfs for a volume.
169 * @vol: volume description object
170 */
171static void volume_sysfs_close(struct ubi_volume *vol)
172{
Artem Bityutskiy8bc22962007-07-22 15:25:02 +0300173 device_remove_file(&vol->dev, &attr_vol_upd_marker);
174 device_remove_file(&vol->dev, &attr_vol_data_bytes);
175 device_remove_file(&vol->dev, &attr_vol_usable_eb_size);
176 device_remove_file(&vol->dev, &attr_vol_alignment);
177 device_remove_file(&vol->dev, &attr_vol_corrupted);
178 device_remove_file(&vol->dev, &attr_vol_name);
179 device_remove_file(&vol->dev, &attr_vol_type);
180 device_remove_file(&vol->dev, &attr_vol_reserved_ebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400181 device_unregister(&vol->dev);
182}
183
184/**
185 * ubi_create_volume - create volume.
186 * @ubi: UBI device description object
187 * @req: volume creation request
188 *
189 * This function creates volume described by @req. If @req->vol_id id
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200190 * %UBI_VOL_NUM_AUTO, this function automatically assign ID to the new volume
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400191 * and saves it in @req->vol_id. Returns zero in case of success and a negative
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200192 * error code in case of failure. Note, the caller has to have the
193 * @ubi->volumes_mutex locked.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400194 */
195int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
196{
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200197 int i, err, vol_id = req->vol_id, dont_free = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400198 struct ubi_volume *vol;
199 struct ubi_vtbl_record vtbl_rec;
200 uint64_t bytes;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200201 dev_t dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400202
203 if (ubi->ro_mode)
204 return -EROFS;
205
206 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
207 if (!vol)
208 return -ENOMEM;
209
210 spin_lock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400211 if (vol_id == UBI_VOL_NUM_AUTO) {
212 /* Find unused volume ID */
213 dbg_msg("search for vacant volume ID");
214 for (i = 0; i < ubi->vtbl_slots; i++)
215 if (!ubi->volumes[i]) {
216 vol_id = i;
217 break;
218 }
219
220 if (vol_id == UBI_VOL_NUM_AUTO) {
221 dbg_err("out of volume IDs");
222 err = -ENFILE;
223 goto out_unlock;
224 }
225 req->vol_id = vol_id;
226 }
227
228 dbg_msg("volume ID %d, %llu bytes, type %d, name %s",
229 vol_id, (unsigned long long)req->bytes,
230 (int)req->vol_type, req->name);
231
232 /* Ensure that this volume does not exist */
233 err = -EEXIST;
234 if (ubi->volumes[vol_id]) {
235 dbg_err("volume %d already exists", vol_id);
236 goto out_unlock;
237 }
238
239 /* Ensure that the name is unique */
240 for (i = 0; i < ubi->vtbl_slots; i++)
241 if (ubi->volumes[i] &&
242 ubi->volumes[i]->name_len == req->name_len &&
Artem Bityutskiy94784d92007-06-18 12:06:30 +0300243 !strcmp(ubi->volumes[i]->name, req->name)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400244 dbg_err("volume \"%s\" exists (ID %d)", req->name, i);
245 goto out_unlock;
246 }
247
248 /* Calculate how many eraseblocks are requested */
249 vol->usable_leb_size = ubi->leb_size - ubi->leb_size % req->alignment;
250 bytes = req->bytes;
251 if (do_div(bytes, vol->usable_leb_size))
252 vol->reserved_pebs = 1;
253 vol->reserved_pebs += bytes;
254
255 /* Reserve physical eraseblocks */
256 if (vol->reserved_pebs > ubi->avail_pebs) {
257 dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400258 err = -ENOSPC;
259 goto out_unlock;
260 }
261 ubi->avail_pebs -= vol->reserved_pebs;
262 ubi->rsvd_pebs += vol->reserved_pebs;
263
264 vol->vol_id = vol_id;
265 vol->alignment = req->alignment;
266 vol->data_pad = ubi->leb_size % vol->alignment;
267 vol->vol_type = req->vol_type;
268 vol->name_len = req->name_len;
269 memcpy(vol->name, req->name, vol->name_len + 1);
270 vol->exclusive = 1;
271 vol->ubi = ubi;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400272 spin_unlock(&ubi->volumes_lock);
273
274 /*
275 * Finish all pending erases because there may be some LEBs belonging
276 * to the same volume ID.
277 */
278 err = ubi_wl_flush(ubi);
279 if (err)
280 goto out_acc;
281
282 vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int), GFP_KERNEL);
283 if (!vol->eba_tbl) {
284 err = -ENOMEM;
285 goto out_acc;
286 }
287
288 for (i = 0; i < vol->reserved_pebs; i++)
289 vol->eba_tbl[i] = UBI_LEB_UNMAPPED;
290
291 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
292 vol->used_ebs = vol->reserved_pebs;
293 vol->last_eb_bytes = vol->usable_leb_size;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300294 vol->used_bytes =
295 (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400296 } else {
297 bytes = vol->used_bytes;
298 vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
299 vol->used_ebs = bytes;
300 if (vol->last_eb_bytes)
301 vol->used_ebs += 1;
302 else
303 vol->last_eb_bytes = vol->usable_leb_size;
304 }
305
306 /* Register character device for the volume */
307 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
308 vol->cdev.owner = THIS_MODULE;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200309 dev = MKDEV(MAJOR(ubi->cdev.dev), vol_id + 1);
310 err = cdev_add(&vol->cdev, dev, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400311 if (err) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200312 ubi_err("cannot add character device");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400313 goto out_mapping;
314 }
315
316 err = ubi_create_gluebi(ubi, vol);
317 if (err)
318 goto out_cdev;
319
320 vol->dev.release = vol_release;
321 vol->dev.parent = &ubi->dev;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200322 vol->dev.devt = dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400323 vol->dev.class = ubi_class;
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200324
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400325 sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
326 err = device_register(&vol->dev);
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200327 if (err) {
328 ubi_err("cannot register device");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329 goto out_gluebi;
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200330 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400331
332 err = volume_sysfs_init(ubi, vol);
333 if (err)
334 goto out_sysfs;
335
336 /* Fill volume table record */
337 memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300338 vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
339 vtbl_rec.alignment = cpu_to_be32(vol->alignment);
340 vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
341 vtbl_rec.name_len = cpu_to_be16(vol->name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400342 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
343 vtbl_rec.vol_type = UBI_VID_DYNAMIC;
344 else
345 vtbl_rec.vol_type = UBI_VID_STATIC;
346 memcpy(vtbl_rec.name, vol->name, vol->name_len + 1);
347
348 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
349 if (err)
350 goto out_sysfs;
351
352 spin_lock(&ubi->volumes_lock);
353 ubi->vol_count += 1;
354 vol->exclusive = 0;
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200355 ubi->volumes[vol_id] = vol;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400356 spin_unlock(&ubi->volumes_lock);
357
358 paranoid_check_volumes(ubi);
359 return 0;
360
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200361out_sysfs:
362 /*
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200363 * We have registered our device, we should not free the volume*
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200364 * description object in this function in case of an error - it is
365 * freed by the release function.
366 *
367 * Get device reference to prevent the release function from being
368 * called just after sysfs has been closed.
369 */
370 dont_free = 1;
371 get_device(&vol->dev);
372 volume_sysfs_close(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400373out_gluebi:
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200374 ubi_destroy_gluebi(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400375out_cdev:
376 cdev_del(&vol->cdev);
377out_mapping:
378 kfree(vol->eba_tbl);
379out_acc:
380 spin_lock(&ubi->volumes_lock);
381 ubi->rsvd_pebs -= vol->reserved_pebs;
382 ubi->avail_pebs += vol->reserved_pebs;
383out_unlock:
384 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200385 if (dont_free)
386 put_device(&vol->dev);
387 else
388 kfree(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
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200399 * code in case of failure. The caller has to have the @ubi->volumes_mutex
400 * locked.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400401 */
402int ubi_remove_volume(struct ubi_volume_desc *desc)
403{
404 struct ubi_volume *vol = desc->vol;
405 struct ubi_device *ubi = vol->ubi;
406 int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
407
408 dbg_msg("remove UBI volume %d", vol_id);
409 ubi_assert(desc->mode == UBI_EXCLUSIVE);
410 ubi_assert(vol == ubi->volumes[vol_id]);
411
412 if (ubi->ro_mode)
413 return -EROFS;
414
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200415 spin_lock(&ubi->volumes_lock);
416 if (vol->ref_count > 1) {
417 /*
418 * The volume is busy, probably someone is reading one of its
419 * sysfs files.
420 */
421 err = -EBUSY;
422 goto out_unlock;
423 }
424 ubi->volumes[vol_id] = NULL;
425 spin_unlock(&ubi->volumes_lock);
426
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400427 err = ubi_destroy_gluebi(vol);
428 if (err)
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200429 goto out_err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400430
431 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
432 if (err)
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200433 goto out_err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400434
435 for (i = 0; i < vol->reserved_pebs; i++) {
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200436 err = ubi_eba_unmap_leb(ubi, vol, i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400437 if (err)
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200438 goto out_err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400439 }
440
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400441 kfree(vol->eba_tbl);
442 vol->eba_tbl = NULL;
443 cdev_del(&vol->cdev);
444 volume_sysfs_close(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400445
446 spin_lock(&ubi->volumes_lock);
447 ubi->rsvd_pebs -= reserved_pebs;
448 ubi->avail_pebs += reserved_pebs;
449 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
450 if (i > 0) {
451 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
452 ubi->avail_pebs -= i;
453 ubi->rsvd_pebs += i;
454 ubi->beb_rsvd_pebs += i;
455 if (i > 0)
456 ubi_msg("reserve more %d PEBs", i);
457 }
458 ubi->vol_count -= 1;
459 spin_unlock(&ubi->volumes_lock);
460
461 paranoid_check_volumes(ubi);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200462 return 0;
463
464out_err:
465 ubi_err("cannot remove volume %d, error %d", vol_id, err);
466 spin_lock(&ubi->volumes_lock);
467 ubi->volumes[vol_id] = vol;
468out_unlock:
469 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200470 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400471}
472
473/**
474 * ubi_resize_volume - re-size volume.
475 * @desc: volume descriptor
476 * @reserved_pebs: new size in physical eraseblocks
477 *
Artem Bityutskiy40e4d0c2007-12-17 17:08:55 +0200478 * This function re-sizes the volume and returns zero in case of success, and a
479 * negative error code in case of failure. The caller has to have the
480 * @ubi->volumes_mutex locked.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400481 */
482int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
483{
484 int i, err, pebs, *new_mapping;
485 struct ubi_volume *vol = desc->vol;
486 struct ubi_device *ubi = vol->ubi;
487 struct ubi_vtbl_record vtbl_rec;
488 int vol_id = vol->vol_id;
489
490 if (ubi->ro_mode)
491 return -EROFS;
492
493 dbg_msg("re-size volume %d to from %d to %d PEBs",
494 vol_id, vol->reserved_pebs, reserved_pebs);
495 ubi_assert(desc->mode == UBI_EXCLUSIVE);
496 ubi_assert(vol == ubi->volumes[vol_id]);
497
498 if (vol->vol_type == UBI_STATIC_VOLUME &&
499 reserved_pebs < vol->used_ebs) {
500 dbg_err("too small size %d, %d LEBs contain data",
501 reserved_pebs, vol->used_ebs);
502 return -EINVAL;
503 }
504
505 /* If the size is the same, we have nothing to do */
506 if (reserved_pebs == vol->reserved_pebs)
507 return 0;
508
509 new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
510 if (!new_mapping)
511 return -ENOMEM;
512
513 for (i = 0; i < reserved_pebs; i++)
514 new_mapping[i] = UBI_LEB_UNMAPPED;
515
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200516 spin_lock(&ubi->volumes_lock);
517 if (vol->ref_count > 1) {
518 spin_unlock(&ubi->volumes_lock);
519 err = -EBUSY;
520 goto out_free;
521 }
522 spin_unlock(&ubi->volumes_lock);
523
524
525 /* Reserve physical eraseblocks */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400526 pebs = reserved_pebs - vol->reserved_pebs;
527 if (pebs > 0) {
528 spin_lock(&ubi->volumes_lock);
529 if (pebs > ubi->avail_pebs) {
530 dbg_err("not enough PEBs: requested %d, available %d",
531 pebs, ubi->avail_pebs);
532 spin_unlock(&ubi->volumes_lock);
533 err = -ENOSPC;
534 goto out_free;
535 }
536 ubi->avail_pebs -= pebs;
537 ubi->rsvd_pebs += pebs;
538 for (i = 0; i < vol->reserved_pebs; i++)
539 new_mapping[i] = vol->eba_tbl[i];
540 kfree(vol->eba_tbl);
541 vol->eba_tbl = new_mapping;
542 spin_unlock(&ubi->volumes_lock);
543 }
544
545 /* Change volume table record */
546 memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300547 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400548 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
549 if (err)
550 goto out_acc;
551
552 if (pebs < 0) {
553 for (i = 0; i < -pebs; i++) {
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200554 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400555 if (err)
556 goto out_acc;
557 }
558 spin_lock(&ubi->volumes_lock);
559 ubi->rsvd_pebs += pebs;
560 ubi->avail_pebs -= pebs;
561 pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
562 if (pebs > 0) {
563 pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
564 ubi->avail_pebs -= pebs;
565 ubi->rsvd_pebs += pebs;
566 ubi->beb_rsvd_pebs += pebs;
567 if (pebs > 0)
568 ubi_msg("reserve more %d PEBs", pebs);
569 }
570 for (i = 0; i < reserved_pebs; i++)
571 new_mapping[i] = vol->eba_tbl[i];
572 kfree(vol->eba_tbl);
573 vol->eba_tbl = new_mapping;
574 spin_unlock(&ubi->volumes_lock);
575 }
576
577 vol->reserved_pebs = reserved_pebs;
578 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
579 vol->used_ebs = reserved_pebs;
580 vol->last_eb_bytes = vol->usable_leb_size;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300581 vol->used_bytes =
582 (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400583 }
584
585 paranoid_check_volumes(ubi);
586 return 0;
587
588out_acc:
589 if (pebs > 0) {
590 spin_lock(&ubi->volumes_lock);
591 ubi->rsvd_pebs -= pebs;
592 ubi->avail_pebs += pebs;
593 spin_unlock(&ubi->volumes_lock);
594 }
595out_free:
596 kfree(new_mapping);
597 return err;
598}
599
600/**
601 * ubi_add_volume - add volume.
602 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200603 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400604 *
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200605 * This function adds an existing volume and initializes all its data
606 * structures. Returns zero in case of success and a negative error code in
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400607 * case of failure.
608 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200609int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400610{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200611 int err, vol_id = vol->vol_id;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200612 dev_t dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400613
614 dbg_msg("add volume %d", vol_id);
615 ubi_dbg_dump_vol_info(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400616
617 /* Register character device for the volume */
618 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
619 vol->cdev.owner = THIS_MODULE;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200620 dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
621 err = cdev_add(&vol->cdev, dev, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400622 if (err) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200623 ubi_err("cannot add character device for volume %d, error %d",
624 vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400625 return err;
626 }
627
628 err = ubi_create_gluebi(ubi, vol);
629 if (err)
630 goto out_cdev;
631
632 vol->dev.release = vol_release;
633 vol->dev.parent = &ubi->dev;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200634 vol->dev.devt = dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400635 vol->dev.class = ubi_class;
636 sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
637 err = device_register(&vol->dev);
638 if (err)
639 goto out_gluebi;
640
641 err = volume_sysfs_init(ubi, vol);
642 if (err) {
643 cdev_del(&vol->cdev);
644 err = ubi_destroy_gluebi(vol);
645 volume_sysfs_close(vol);
646 return err;
647 }
648
649 paranoid_check_volumes(ubi);
650 return 0;
651
652out_gluebi:
653 err = ubi_destroy_gluebi(vol);
654out_cdev:
655 cdev_del(&vol->cdev);
656 return err;
657}
658
659/**
660 * ubi_free_volume - free volume.
661 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200662 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400663 *
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200664 * This function frees all resources for volume @vol but does not remove it.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400665 * Used only when the UBI device is detached.
666 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200667void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400668{
669 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400670
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200671 dbg_msg("free volume %d", vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400672
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200673 ubi->volumes[vol->vol_id] = NULL;
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200674 err = ubi_destroy_gluebi(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400675 cdev_del(&vol->cdev);
676 volume_sysfs_close(vol);
677}
678
679#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
680
681/**
682 * paranoid_check_volume - check volume information.
683 * @ubi: UBI device description object
684 * @vol_id: volume ID
685 */
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300686static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400687{
688 int idx = vol_id2idx(ubi, vol_id);
689 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300690 const struct ubi_volume *vol;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400691 long long n;
692 const char *name;
693
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300694 spin_lock(&ubi->volumes_lock);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300695 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300696 vol = ubi->volumes[idx];
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400697
698 if (!vol) {
699 if (reserved_pebs) {
700 ubi_err("no volume info, but volume exists");
701 goto fail;
702 }
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300703 spin_unlock(&ubi->volumes_lock);
704 return;
705 }
706
707 if (vol->exclusive) {
708 /*
709 * The volume may be being created at the moment, do not check
710 * it (e.g., it may be in the middle of ubi_create_volume().
711 */
712 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400713 return;
714 }
715
716 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
717 vol->name_len < 0) {
718 ubi_err("negative values");
719 goto fail;
720 }
721 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
722 ubi_err("bad alignment");
723 goto fail;
724 }
725
726 n = vol->alignment % ubi->min_io_size;
727 if (vol->alignment != 1 && n) {
728 ubi_err("alignment is not multiple of min I/O unit");
729 goto fail;
730 }
731
732 n = ubi->leb_size % vol->alignment;
733 if (vol->data_pad != n) {
734 ubi_err("bad data_pad, has to be %lld", n);
735 goto fail;
736 }
737
738 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
739 vol->vol_type != UBI_STATIC_VOLUME) {
740 ubi_err("bad vol_type");
741 goto fail;
742 }
743
744 if (vol->upd_marker != 0 && vol->upd_marker != 1) {
745 ubi_err("bad upd_marker");
746 goto fail;
747 }
748
749 if (vol->upd_marker && vol->corrupted) {
750 dbg_err("update marker and corrupted simultaneously");
751 goto fail;
752 }
753
754 if (vol->reserved_pebs > ubi->good_peb_count) {
755 ubi_err("too large reserved_pebs");
756 goto fail;
757 }
758
759 n = ubi->leb_size - vol->data_pad;
760 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
761 ubi_err("bad usable_leb_size, has to be %lld", n);
762 goto fail;
763 }
764
765 if (vol->name_len > UBI_VOL_NAME_MAX) {
766 ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
767 goto fail;
768 }
769
770 if (!vol->name) {
771 ubi_err("NULL volume name");
772 goto fail;
773 }
774
775 n = strnlen(vol->name, vol->name_len + 1);
776 if (n != vol->name_len) {
777 ubi_err("bad name_len %lld", n);
778 goto fail;
779 }
780
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300781 n = (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400782 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
783 if (vol->corrupted != 0) {
784 ubi_err("corrupted dynamic volume");
785 goto fail;
786 }
787 if (vol->used_ebs != vol->reserved_pebs) {
788 ubi_err("bad used_ebs");
789 goto fail;
790 }
791 if (vol->last_eb_bytes != vol->usable_leb_size) {
792 ubi_err("bad last_eb_bytes");
793 goto fail;
794 }
795 if (vol->used_bytes != n) {
796 ubi_err("bad used_bytes");
797 goto fail;
798 }
799 } else {
800 if (vol->corrupted != 0 && vol->corrupted != 1) {
801 ubi_err("bad corrupted");
802 goto fail;
803 }
804 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
805 ubi_err("bad used_ebs");
806 goto fail;
807 }
808 if (vol->last_eb_bytes < 0 ||
809 vol->last_eb_bytes > vol->usable_leb_size) {
810 ubi_err("bad last_eb_bytes");
811 goto fail;
812 }
813 if (vol->used_bytes < 0 || vol->used_bytes > n ||
814 vol->used_bytes < n - vol->usable_leb_size) {
815 ubi_err("bad used_bytes");
816 goto fail;
817 }
818 }
819
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300820 alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
821 data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
822 name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400823 upd_marker = ubi->vtbl[vol_id].upd_marker;
824 name = &ubi->vtbl[vol_id].name[0];
825 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
826 vol_type = UBI_DYNAMIC_VOLUME;
827 else
828 vol_type = UBI_STATIC_VOLUME;
829
830 if (alignment != vol->alignment || data_pad != vol->data_pad ||
831 upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
832 name_len!= vol->name_len || strncmp(name, vol->name, name_len)) {
833 ubi_err("volume info is different");
834 goto fail;
835 }
836
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300837 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400838 return;
839
840fail:
Artem Bityutskiy94784d92007-06-18 12:06:30 +0300841 ubi_err("paranoid check failed for volume %d", vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400842 ubi_dbg_dump_vol_info(vol);
843 ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300844 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400845 BUG();
846}
847
848/**
849 * paranoid_check_volumes - check information about all volumes.
850 * @ubi: UBI device description object
851 */
852static void paranoid_check_volumes(struct ubi_device *ubi)
853{
854 int i;
855
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400856 for (i = 0; i < ubi->vtbl_slots; i++)
857 paranoid_check_volume(ubi, i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400858}
859#endif