blob: 18ef1e1da496144df2b00f3444e730bb6c9a3c51 [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
192 * error code in case of failure.
193 */
194int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
195{
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200196 int i, err, vol_id = req->vol_id, dont_free = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400197 struct ubi_volume *vol;
198 struct ubi_vtbl_record vtbl_rec;
199 uint64_t bytes;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200200 dev_t dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400201
202 if (ubi->ro_mode)
203 return -EROFS;
204
205 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
206 if (!vol)
207 return -ENOMEM;
208
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200209 mutex_lock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400210 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);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200359 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400360 return 0;
361
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200362out_sysfs:
363 /*
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200364 * We have registered our device, we should not free the volume*
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200365 * description object in this function in case of an error - it is
366 * freed by the release function.
367 *
368 * Get device reference to prevent the release function from being
369 * called just after sysfs has been closed.
370 */
371 dont_free = 1;
372 get_device(&vol->dev);
373 volume_sysfs_close(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400374out_gluebi:
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200375 ubi_destroy_gluebi(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400376out_cdev:
377 cdev_del(&vol->cdev);
378out_mapping:
379 kfree(vol->eba_tbl);
380out_acc:
381 spin_lock(&ubi->volumes_lock);
382 ubi->rsvd_pebs -= vol->reserved_pebs;
383 ubi->avail_pebs += vol->reserved_pebs;
384out_unlock:
385 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200386 mutex_unlock(&ubi->volumes_mutex);
Artem Bityutskiyfc75a1e2007-12-17 14:02:09 +0200387 if (dont_free)
388 put_device(&vol->dev);
389 else
390 kfree(vol);
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200391 ubi_err("cannot create volume %d, error %d", vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400392 return err;
393}
394
395/**
396 * ubi_remove_volume - remove volume.
397 * @desc: volume descriptor
398 *
399 * This function removes volume described by @desc. The volume has to be opened
400 * in "exclusive" mode. Returns zero in case of success and a negative error
401 * code in case of failure.
402 */
403int ubi_remove_volume(struct ubi_volume_desc *desc)
404{
405 struct ubi_volume *vol = desc->vol;
406 struct ubi_device *ubi = vol->ubi;
407 int i, err, vol_id = vol->vol_id, reserved_pebs = vol->reserved_pebs;
408
409 dbg_msg("remove UBI volume %d", vol_id);
410 ubi_assert(desc->mode == UBI_EXCLUSIVE);
411 ubi_assert(vol == ubi->volumes[vol_id]);
412
413 if (ubi->ro_mode)
414 return -EROFS;
415
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200416 mutex_lock(&ubi->volumes_mutex);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200417 spin_lock(&ubi->volumes_lock);
418 if (vol->ref_count > 1) {
419 /*
420 * The volume is busy, probably someone is reading one of its
421 * sysfs files.
422 */
423 err = -EBUSY;
424 goto out_unlock;
425 }
426 ubi->volumes[vol_id] = NULL;
427 spin_unlock(&ubi->volumes_lock);
428
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400429 err = ubi_destroy_gluebi(vol);
430 if (err)
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200431 goto out_err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400432
433 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
434 if (err)
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200435 goto out_err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400436
437 for (i = 0; i < vol->reserved_pebs; i++) {
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200438 err = ubi_eba_unmap_leb(ubi, vol, i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400439 if (err)
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200440 goto out_err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400441 }
442
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400443 kfree(vol->eba_tbl);
444 vol->eba_tbl = NULL;
445 cdev_del(&vol->cdev);
446 volume_sysfs_close(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400447
448 spin_lock(&ubi->volumes_lock);
449 ubi->rsvd_pebs -= reserved_pebs;
450 ubi->avail_pebs += reserved_pebs;
451 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
452 if (i > 0) {
453 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
454 ubi->avail_pebs -= i;
455 ubi->rsvd_pebs += i;
456 ubi->beb_rsvd_pebs += i;
457 if (i > 0)
458 ubi_msg("reserve more %d PEBs", i);
459 }
460 ubi->vol_count -= 1;
461 spin_unlock(&ubi->volumes_lock);
462
463 paranoid_check_volumes(ubi);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200464 mutex_unlock(&ubi->volumes_mutex);
465 return 0;
466
467out_err:
468 ubi_err("cannot remove volume %d, error %d", vol_id, err);
469 spin_lock(&ubi->volumes_lock);
470 ubi->volumes[vol_id] = vol;
471out_unlock:
472 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200473 mutex_unlock(&ubi->volumes_mutex);
474 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400475}
476
477/**
478 * ubi_resize_volume - re-size volume.
479 * @desc: volume descriptor
480 * @reserved_pebs: new size in physical eraseblocks
481 *
482 * This function returns zero in case of success, and a negative error code in
483 * case of failure.
484 */
485int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
486{
487 int i, err, pebs, *new_mapping;
488 struct ubi_volume *vol = desc->vol;
489 struct ubi_device *ubi = vol->ubi;
490 struct ubi_vtbl_record vtbl_rec;
491 int vol_id = vol->vol_id;
492
493 if (ubi->ro_mode)
494 return -EROFS;
495
496 dbg_msg("re-size volume %d to from %d to %d PEBs",
497 vol_id, vol->reserved_pebs, reserved_pebs);
498 ubi_assert(desc->mode == UBI_EXCLUSIVE);
499 ubi_assert(vol == ubi->volumes[vol_id]);
500
501 if (vol->vol_type == UBI_STATIC_VOLUME &&
502 reserved_pebs < vol->used_ebs) {
503 dbg_err("too small size %d, %d LEBs contain data",
504 reserved_pebs, vol->used_ebs);
505 return -EINVAL;
506 }
507
508 /* If the size is the same, we have nothing to do */
509 if (reserved_pebs == vol->reserved_pebs)
510 return 0;
511
512 new_mapping = kmalloc(reserved_pebs * sizeof(int), GFP_KERNEL);
513 if (!new_mapping)
514 return -ENOMEM;
515
516 for (i = 0; i < reserved_pebs; i++)
517 new_mapping[i] = UBI_LEB_UNMAPPED;
518
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200519 mutex_lock(&ubi->volumes_mutex);
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200520 spin_lock(&ubi->volumes_lock);
521 if (vol->ref_count > 1) {
522 spin_unlock(&ubi->volumes_lock);
523 err = -EBUSY;
524 goto out_free;
525 }
526 spin_unlock(&ubi->volumes_lock);
527
528
529 /* Reserve physical eraseblocks */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400530 pebs = reserved_pebs - vol->reserved_pebs;
531 if (pebs > 0) {
532 spin_lock(&ubi->volumes_lock);
533 if (pebs > ubi->avail_pebs) {
534 dbg_err("not enough PEBs: requested %d, available %d",
535 pebs, ubi->avail_pebs);
536 spin_unlock(&ubi->volumes_lock);
537 err = -ENOSPC;
538 goto out_free;
539 }
540 ubi->avail_pebs -= pebs;
541 ubi->rsvd_pebs += pebs;
542 for (i = 0; i < vol->reserved_pebs; i++)
543 new_mapping[i] = vol->eba_tbl[i];
544 kfree(vol->eba_tbl);
545 vol->eba_tbl = new_mapping;
546 spin_unlock(&ubi->volumes_lock);
547 }
548
549 /* Change volume table record */
550 memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300551 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400552 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
553 if (err)
554 goto out_acc;
555
556 if (pebs < 0) {
557 for (i = 0; i < -pebs; i++) {
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200558 err = ubi_eba_unmap_leb(ubi, vol, reserved_pebs + i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400559 if (err)
560 goto out_acc;
561 }
562 spin_lock(&ubi->volumes_lock);
563 ubi->rsvd_pebs += pebs;
564 ubi->avail_pebs -= pebs;
565 pebs = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
566 if (pebs > 0) {
567 pebs = ubi->avail_pebs >= pebs ? pebs : ubi->avail_pebs;
568 ubi->avail_pebs -= pebs;
569 ubi->rsvd_pebs += pebs;
570 ubi->beb_rsvd_pebs += pebs;
571 if (pebs > 0)
572 ubi_msg("reserve more %d PEBs", pebs);
573 }
574 for (i = 0; i < reserved_pebs; i++)
575 new_mapping[i] = vol->eba_tbl[i];
576 kfree(vol->eba_tbl);
577 vol->eba_tbl = new_mapping;
578 spin_unlock(&ubi->volumes_lock);
579 }
580
581 vol->reserved_pebs = reserved_pebs;
582 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
583 vol->used_ebs = reserved_pebs;
584 vol->last_eb_bytes = vol->usable_leb_size;
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300585 vol->used_bytes =
586 (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400587 }
588
589 paranoid_check_volumes(ubi);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200590 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400591 return 0;
592
593out_acc:
594 if (pebs > 0) {
595 spin_lock(&ubi->volumes_lock);
596 ubi->rsvd_pebs -= pebs;
597 ubi->avail_pebs += pebs;
598 spin_unlock(&ubi->volumes_lock);
599 }
600out_free:
601 kfree(new_mapping);
Artem Bityutskiycae0a772007-12-17 12:46:48 +0200602 mutex_unlock(&ubi->volumes_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400603 return err;
604}
605
606/**
607 * ubi_add_volume - add volume.
608 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200609 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400610 *
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200611 * This function adds an existing volume and initializes all its data
612 * structures. Returns zero in case of success and a negative error code in
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400613 * case of failure.
614 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200615int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400616{
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200617 int err, vol_id = vol->vol_id;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200618 dev_t dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400619
620 dbg_msg("add volume %d", vol_id);
621 ubi_dbg_dump_vol_info(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400622
623 /* Register character device for the volume */
624 cdev_init(&vol->cdev, &ubi_vol_cdev_operations);
625 vol->cdev.owner = THIS_MODULE;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200626 dev = MKDEV(MAJOR(ubi->cdev.dev), vol->vol_id + 1);
627 err = cdev_add(&vol->cdev, dev, 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400628 if (err) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200629 ubi_err("cannot add character device for volume %d, error %d",
630 vol_id, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400631 return err;
632 }
633
634 err = ubi_create_gluebi(ubi, vol);
635 if (err)
636 goto out_cdev;
637
638 vol->dev.release = vol_release;
639 vol->dev.parent = &ubi->dev;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200640 vol->dev.devt = dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400641 vol->dev.class = ubi_class;
642 sprintf(&vol->dev.bus_id[0], "%s_%d", ubi->ubi_name, vol->vol_id);
643 err = device_register(&vol->dev);
644 if (err)
645 goto out_gluebi;
646
647 err = volume_sysfs_init(ubi, vol);
648 if (err) {
649 cdev_del(&vol->cdev);
650 err = ubi_destroy_gluebi(vol);
651 volume_sysfs_close(vol);
652 return err;
653 }
654
655 paranoid_check_volumes(ubi);
656 return 0;
657
658out_gluebi:
659 err = ubi_destroy_gluebi(vol);
660out_cdev:
661 cdev_del(&vol->cdev);
662 return err;
663}
664
665/**
666 * ubi_free_volume - free volume.
667 * @ubi: UBI device description object
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200668 * @vol: volume description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400669 *
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200670 * This function frees all resources for volume @vol but does not remove it.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400671 * Used only when the UBI device is detached.
672 */
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200673void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400674{
675 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400676
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200677 dbg_msg("free volume %d", vol->vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400678
Artem Bityutskiy89b96b62007-12-16 20:00:38 +0200679 ubi->volumes[vol->vol_id] = NULL;
Artem Bityutskiyd05c77a2007-12-17 15:42:57 +0200680 err = ubi_destroy_gluebi(vol);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400681 cdev_del(&vol->cdev);
682 volume_sysfs_close(vol);
683}
684
685#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
686
687/**
688 * paranoid_check_volume - check volume information.
689 * @ubi: UBI device description object
690 * @vol_id: volume ID
691 */
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300692static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400693{
694 int idx = vol_id2idx(ubi, vol_id);
695 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300696 const struct ubi_volume *vol;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400697 long long n;
698 const char *name;
699
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300700 spin_lock(&ubi->volumes_lock);
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300701 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300702 vol = ubi->volumes[idx];
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400703
704 if (!vol) {
705 if (reserved_pebs) {
706 ubi_err("no volume info, but volume exists");
707 goto fail;
708 }
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300709 spin_unlock(&ubi->volumes_lock);
710 return;
711 }
712
713 if (vol->exclusive) {
714 /*
715 * The volume may be being created at the moment, do not check
716 * it (e.g., it may be in the middle of ubi_create_volume().
717 */
718 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400719 return;
720 }
721
722 if (vol->reserved_pebs < 0 || vol->alignment < 0 || vol->data_pad < 0 ||
723 vol->name_len < 0) {
724 ubi_err("negative values");
725 goto fail;
726 }
727 if (vol->alignment > ubi->leb_size || vol->alignment == 0) {
728 ubi_err("bad alignment");
729 goto fail;
730 }
731
732 n = vol->alignment % ubi->min_io_size;
733 if (vol->alignment != 1 && n) {
734 ubi_err("alignment is not multiple of min I/O unit");
735 goto fail;
736 }
737
738 n = ubi->leb_size % vol->alignment;
739 if (vol->data_pad != n) {
740 ubi_err("bad data_pad, has to be %lld", n);
741 goto fail;
742 }
743
744 if (vol->vol_type != UBI_DYNAMIC_VOLUME &&
745 vol->vol_type != UBI_STATIC_VOLUME) {
746 ubi_err("bad vol_type");
747 goto fail;
748 }
749
750 if (vol->upd_marker != 0 && vol->upd_marker != 1) {
751 ubi_err("bad upd_marker");
752 goto fail;
753 }
754
755 if (vol->upd_marker && vol->corrupted) {
756 dbg_err("update marker and corrupted simultaneously");
757 goto fail;
758 }
759
760 if (vol->reserved_pebs > ubi->good_peb_count) {
761 ubi_err("too large reserved_pebs");
762 goto fail;
763 }
764
765 n = ubi->leb_size - vol->data_pad;
766 if (vol->usable_leb_size != ubi->leb_size - vol->data_pad) {
767 ubi_err("bad usable_leb_size, has to be %lld", n);
768 goto fail;
769 }
770
771 if (vol->name_len > UBI_VOL_NAME_MAX) {
772 ubi_err("too long volume name, max is %d", UBI_VOL_NAME_MAX);
773 goto fail;
774 }
775
776 if (!vol->name) {
777 ubi_err("NULL volume name");
778 goto fail;
779 }
780
781 n = strnlen(vol->name, vol->name_len + 1);
782 if (n != vol->name_len) {
783 ubi_err("bad name_len %lld", n);
784 goto fail;
785 }
786
Vinit Agnihotrid08c3b72007-07-10 13:04:59 +0300787 n = (long long)vol->used_ebs * vol->usable_leb_size;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400788 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
789 if (vol->corrupted != 0) {
790 ubi_err("corrupted dynamic volume");
791 goto fail;
792 }
793 if (vol->used_ebs != vol->reserved_pebs) {
794 ubi_err("bad used_ebs");
795 goto fail;
796 }
797 if (vol->last_eb_bytes != vol->usable_leb_size) {
798 ubi_err("bad last_eb_bytes");
799 goto fail;
800 }
801 if (vol->used_bytes != n) {
802 ubi_err("bad used_bytes");
803 goto fail;
804 }
805 } else {
806 if (vol->corrupted != 0 && vol->corrupted != 1) {
807 ubi_err("bad corrupted");
808 goto fail;
809 }
810 if (vol->used_ebs < 0 || vol->used_ebs > vol->reserved_pebs) {
811 ubi_err("bad used_ebs");
812 goto fail;
813 }
814 if (vol->last_eb_bytes < 0 ||
815 vol->last_eb_bytes > vol->usable_leb_size) {
816 ubi_err("bad last_eb_bytes");
817 goto fail;
818 }
819 if (vol->used_bytes < 0 || vol->used_bytes > n ||
820 vol->used_bytes < n - vol->usable_leb_size) {
821 ubi_err("bad used_bytes");
822 goto fail;
823 }
824 }
825
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300826 alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
827 data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
828 name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400829 upd_marker = ubi->vtbl[vol_id].upd_marker;
830 name = &ubi->vtbl[vol_id].name[0];
831 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
832 vol_type = UBI_DYNAMIC_VOLUME;
833 else
834 vol_type = UBI_STATIC_VOLUME;
835
836 if (alignment != vol->alignment || data_pad != vol->data_pad ||
837 upd_marker != vol->upd_marker || vol_type != vol->vol_type ||
838 name_len!= vol->name_len || strncmp(name, vol->name, name_len)) {
839 ubi_err("volume info is different");
840 goto fail;
841 }
842
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300843 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400844 return;
845
846fail:
Artem Bityutskiy94784d92007-06-18 12:06:30 +0300847 ubi_err("paranoid check failed for volume %d", vol_id);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400848 ubi_dbg_dump_vol_info(vol);
849 ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
Artem Bityutskiyb89044b2007-06-18 16:29:30 +0300850 spin_unlock(&ubi->volumes_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400851 BUG();
852}
853
854/**
855 * paranoid_check_volumes - check information about all volumes.
856 * @ubi: UBI device description object
857 */
858static void paranoid_check_volumes(struct ubi_device *ubi)
859{
860 int i;
861
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400862 for (i = 0; i < ubi->vtbl_slots; i++)
863 paranoid_check_volume(ubi, i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400864}
865#endif