Linux 2.6.31-rc3
[deliverable/linux.git] / drivers / mtd / ubi / gluebi.c
CommitLineData
801c135c
AB
1/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Artem Bityutskiy (Битюцкий Артём), Joern Engel
19 */
20
21/*
2ba3d76a
DP
22 * This is a small driver which implements fake MTD devices on top of UBI
23 * volumes. This sounds strange, but it is in fact quite useful to make
24 * MTD-oriented software (including all the legacy software) work on top of
25 * UBI.
801c135c
AB
26 *
27 * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
2ba3d76a 28 * size (@mtd->writesize) is equivalent to the UBI minimal I/O unit. The
801c135c
AB
29 * eraseblock size is equivalent to the logical eraseblock size of the volume.
30 */
31
2ba3d76a
DP
32#include <linux/err.h>
33#include <linux/list.h>
34#include <linux/sched.h>
3013ee31 35#include <linux/math64.h>
2ba3d76a
DP
36#include <linux/module.h>
37#include <linux/mutex.h>
38#include <linux/mtd/ubi.h>
39#include <linux/mtd/mtd.h>
40#include "ubi-media.h"
41
42#define err_msg(fmt, ...) \
43 printk(KERN_DEBUG "gluebi (pid %d): %s: " fmt "\n", \
44 current->pid, __func__, ##__VA_ARGS__)
45
46/**
47 * struct gluebi_device - a gluebi device description data structure.
48 * @mtd: emulated MTD device description object
49 * @refcnt: gluebi device reference count
50 * @desc: UBI volume descriptor
51 * @ubi_num: UBI device number this gluebi device works on
52 * @vol_id: ID of UBI volume this gluebi device works on
53 * @list: link in a list of gluebi devices
54 */
55struct gluebi_device {
56 struct mtd_info mtd;
57 int refcnt;
58 struct ubi_volume_desc *desc;
59 int ubi_num;
60 int vol_id;
61 struct list_head list;
62};
63
64/* List of all gluebi devices */
65static LIST_HEAD(gluebi_devices);
66static DEFINE_MUTEX(devices_mutex);
67
68/**
69 * find_gluebi_nolock - find a gluebi device.
70 * @ubi_num: UBI device number
71 * @vol_id: volume ID
72 *
73 * This function seraches for gluebi device corresponding to UBI device
74 * @ubi_num and UBI volume @vol_id. Returns the gluebi device description
75 * object in case of success and %NULL in case of failure. The caller has to
76 * have the &devices_mutex locked.
77 */
78static struct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
79{
80 struct gluebi_device *gluebi;
81
82 list_for_each_entry(gluebi, &gluebi_devices, list)
83 if (gluebi->ubi_num == ubi_num && gluebi->vol_id == vol_id)
84 return gluebi;
85 return NULL;
86}
801c135c
AB
87
88/**
89 * gluebi_get_device - get MTD device reference.
90 * @mtd: the MTD device description object
91 *
92 * This function is called every time the MTD device is being opened and
93 * implements the MTD get_device() operation. Returns zero in case of success
94 * and a negative error code in case of failure.
95 */
96static int gluebi_get_device(struct mtd_info *mtd)
97{
2ba3d76a
DP
98 struct gluebi_device *gluebi;
99 int ubi_mode = UBI_READONLY;
801c135c 100
2ba3d76a
DP
101 if (!try_module_get(THIS_MODULE))
102 return -ENODEV;
801c135c 103
2ba3d76a
DP
104 if (mtd->flags & MTD_WRITEABLE)
105 ubi_mode = UBI_READWRITE;
106
107 gluebi = container_of(mtd, struct gluebi_device, mtd);
108 mutex_lock(&devices_mutex);
109 if (gluebi->refcnt > 0) {
801c135c
AB
110 /*
111 * The MTD device is already referenced and this is just one
112 * more reference. MTD allows many users to open the same
113 * volume simultaneously and do not distinguish between
114 * readers/writers/exclusive openers as UBI does. So we do not
115 * open the UBI volume again - just increase the reference
116 * counter and return.
117 */
2ba3d76a
DP
118 gluebi->refcnt += 1;
119 mutex_unlock(&devices_mutex);
801c135c
AB
120 return 0;
121 }
122
123 /*
124 * This is the first reference to this UBI volume via the MTD device
125 * interface. Open the corresponding volume in read-write mode.
126 */
2ba3d76a
DP
127 gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
128 ubi_mode);
129 if (IS_ERR(gluebi->desc)) {
130 mutex_unlock(&devices_mutex);
131 module_put(THIS_MODULE);
132 return PTR_ERR(gluebi->desc);
133 }
134 gluebi->refcnt += 1;
135 mutex_unlock(&devices_mutex);
801c135c
AB
136 return 0;
137}
138
139/**
140 * gluebi_put_device - put MTD device reference.
141 * @mtd: the MTD device description object
142 *
143 * This function is called every time the MTD device is being put. Returns
144 * zero in case of success and a negative error code in case of failure.
145 */
146static void gluebi_put_device(struct mtd_info *mtd)
147{
2ba3d76a
DP
148 struct gluebi_device *gluebi;
149
150 gluebi = container_of(mtd, struct gluebi_device, mtd);
151 mutex_lock(&devices_mutex);
152 gluebi->refcnt -= 1;
153 if (gluebi->refcnt == 0)
154 ubi_close_volume(gluebi->desc);
155 module_put(THIS_MODULE);
156 mutex_unlock(&devices_mutex);
801c135c
AB
157}
158
159/**
160 * gluebi_read - read operation of emulated MTD devices.
161 * @mtd: MTD device description object
162 * @from: absolute offset from where to read
163 * @len: how many bytes to read
164 * @retlen: count of read bytes is returned here
165 * @buf: buffer to store the read data
166 *
167 * This function returns zero in case of success and a negative error code in
168 * case of failure.
169 */
170static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
171 size_t *retlen, unsigned char *buf)
172{
173 int err = 0, lnum, offs, total_read;
2ba3d76a 174 struct gluebi_device *gluebi;
801c135c
AB
175
176 if (len < 0 || from < 0 || from + len > mtd->size)
177 return -EINVAL;
178
2ba3d76a 179 gluebi = container_of(mtd, struct gluebi_device, mtd);
801c135c 180
3013ee31 181 lnum = div_u64_rem(from, mtd->erasesize, &offs);
801c135c
AB
182 total_read = len;
183 while (total_read) {
184 size_t to_read = mtd->erasesize - offs;
185
186 if (to_read > total_read)
187 to_read = total_read;
188
2ba3d76a 189 err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
801c135c
AB
190 if (err)
191 break;
192
193 lnum += 1;
194 offs = 0;
195 total_read -= to_read;
196 buf += to_read;
197 }
198
199 *retlen = len - total_read;
200 return err;
201}
202
203/**
204 * gluebi_write - write operation of emulated MTD devices.
205 * @mtd: MTD device description object
206 * @to: absolute offset where to write
207 * @len: how many bytes to write
208 * @retlen: count of written bytes is returned here
209 * @buf: buffer with data to write
210 *
211 * This function returns zero in case of success and a negative error code in
212 * case of failure.
213 */
214static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
2ba3d76a 215 size_t *retlen, const u_char *buf)
801c135c
AB
216{
217 int err = 0, lnum, offs, total_written;
2ba3d76a 218 struct gluebi_device *gluebi;
801c135c
AB
219
220 if (len < 0 || to < 0 || len + to > mtd->size)
221 return -EINVAL;
222
2ba3d76a 223 gluebi = container_of(mtd, struct gluebi_device, mtd);
801c135c 224
2ba3d76a 225 if (!(mtd->flags & MTD_WRITEABLE))
801c135c
AB
226 return -EROFS;
227
3013ee31 228 lnum = div_u64_rem(to, mtd->erasesize, &offs);
801c135c
AB
229
230 if (len % mtd->writesize || offs % mtd->writesize)
231 return -EINVAL;
232
233 total_written = len;
234 while (total_written) {
235 size_t to_write = mtd->erasesize - offs;
236
237 if (to_write > total_written)
238 to_write = total_written;
239
2ba3d76a 240 err = ubi_write(gluebi->desc, lnum, buf, offs, to_write);
801c135c
AB
241 if (err)
242 break;
243
244 lnum += 1;
245 offs = 0;
246 total_written -= to_write;
247 buf += to_write;
248 }
249
250 *retlen = len - total_written;
251 return err;
252}
253
254/**
255 * gluebi_erase - erase operation of emulated MTD devices.
256 * @mtd: the MTD device description object
257 * @instr: the erase operation description
258 *
259 * This function calls the erase callback when finishes. Returns zero in case
260 * of success and a negative error code in case of failure.
261 */
262static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
263{
264 int err, i, lnum, count;
2ba3d76a 265 struct gluebi_device *gluebi;
801c135c
AB
266
267 if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize)
268 return -EINVAL;
801c135c
AB
269 if (instr->len < 0 || instr->addr + instr->len > mtd->size)
270 return -EINVAL;
69423d99 271 if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
801c135c
AB
272 return -EINVAL;
273
69423d99
AH
274 lnum = mtd_div_by_eb(instr->addr, mtd);
275 count = mtd_div_by_eb(instr->len, mtd);
801c135c 276
2ba3d76a 277 gluebi = container_of(mtd, struct gluebi_device, mtd);
801c135c 278
2ba3d76a 279 if (!(mtd->flags & MTD_WRITEABLE))
801c135c
AB
280 return -EROFS;
281
2ba3d76a
DP
282 for (i = 0; i < count - 1; i++) {
283 err = ubi_leb_unmap(gluebi->desc, lnum + i);
801c135c
AB
284 if (err)
285 goto out_err;
286 }
801c135c
AB
287 /*
288 * MTD erase operations are synchronous, so we have to make sure the
289 * physical eraseblock is wiped out.
2ba3d76a
DP
290 *
291 * Thus, perform leb_erase instead of leb_unmap operation - leb_erase
292 * will wait for the end of operations
801c135c 293 */
2ba3d76a 294 err = ubi_leb_erase(gluebi->desc, lnum + i);
801c135c
AB
295 if (err)
296 goto out_err;
297
9c9ec147
AB
298 instr->state = MTD_ERASE_DONE;
299 mtd_erase_callback(instr);
801c135c
AB
300 return 0;
301
302out_err:
303 instr->state = MTD_ERASE_FAILED;
69423d99 304 instr->fail_addr = (long long)lnum * mtd->erasesize;
801c135c
AB
305 return err;
306}
307
308/**
2ba3d76a
DP
309 * gluebi_create - create a gluebi device for an UBI volume.
310 * @di: UBI device description object
311 * @vi: UBI volume description object
801c135c 312 *
2ba3d76a 313 * This function is called when a new UBI volume is created in order to create
801c135c
AB
314 * corresponding fake MTD device. Returns zero in case of success and a
315 * negative error code in case of failure.
316 */
2ba3d76a
DP
317static int gluebi_create(struct ubi_device_info *di,
318 struct ubi_volume_info *vi)
801c135c 319{
2ba3d76a
DP
320 struct gluebi_device *gluebi, *g;
321 struct mtd_info *mtd;
801c135c 322
2ba3d76a
DP
323 gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL);
324 if (!gluebi)
801c135c
AB
325 return -ENOMEM;
326
2ba3d76a
DP
327 mtd = &gluebi->mtd;
328 mtd->name = kmemdup(vi->name, vi->name_len + 1, GFP_KERNEL);
329 if (!mtd->name) {
330 kfree(gluebi);
331 return -ENOMEM;
332 }
333
334 gluebi->vol_id = vi->vol_id;
801c135c 335 mtd->type = MTD_UBIVOLUME;
2ba3d76a 336 if (!di->ro_mode)
801c135c 337 mtd->flags = MTD_WRITEABLE;
801c135c 338 mtd->owner = THIS_MODULE;
2ba3d76a
DP
339 mtd->writesize = di->min_io_size;
340 mtd->erasesize = vi->usable_leb_size;
801c135c
AB
341 mtd->read = gluebi_read;
342 mtd->write = gluebi_write;
343 mtd->erase = gluebi_erase;
344 mtd->get_device = gluebi_get_device;
345 mtd->put_device = gluebi_put_device;
346
941dfb07 347 /*
2ba3d76a 348 * In case of dynamic a volume, MTD device size is just volume size. In
941dfb07 349 * case of a static volume the size is equivalent to the amount of data
cbd8a9d2 350 * bytes.
941dfb07 351 */
2ba3d76a
DP
352 if (vi->vol_type == UBI_DYNAMIC_VOLUME)
353 mtd->size = (unsigned long long)vi->usable_leb_size * vi->size;
cbd8a9d2 354 else
2ba3d76a
DP
355 mtd->size = vi->used_bytes;
356
357 /* Just a sanity check - make sure this gluebi device does not exist */
358 mutex_lock(&devices_mutex);
359 g = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
360 if (g)
361 err_msg("gluebi MTD device %d form UBI device %d volume %d "
362 "already exists", g->mtd.index, vi->ubi_num,
363 vi->vol_id);
364 mutex_unlock(&devices_mutex);
941dfb07 365
801c135c 366 if (add_mtd_device(mtd)) {
2ba3d76a 367 err_msg("cannot add MTD device");
801c135c 368 kfree(mtd->name);
2ba3d76a 369 kfree(gluebi);
801c135c
AB
370 return -ENFILE;
371 }
372
2ba3d76a
DP
373 mutex_lock(&devices_mutex);
374 list_add_tail(&gluebi->list, &gluebi_devices);
375 mutex_unlock(&devices_mutex);
801c135c
AB
376 return 0;
377}
378
379/**
2ba3d76a
DP
380 * gluebi_remove - remove a gluebi device.
381 * @vi: UBI volume description object
801c135c 382 *
2ba3d76a 383 * This function is called when an UBI volume is removed and it removes
801c135c
AB
384 * corresponding fake MTD device. Returns zero in case of success and a
385 * negative error code in case of failure.
386 */
2ba3d76a 387static int gluebi_remove(struct ubi_volume_info *vi)
801c135c 388{
2ba3d76a
DP
389 int err = 0;
390 struct mtd_info *mtd;
391 struct gluebi_device *gluebi;
392
393 mutex_lock(&devices_mutex);
394 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
395 if (!gluebi) {
396 err_msg("got remove notification for unknown UBI device %d "
397 "volume %d", vi->ubi_num, vi->vol_id);
398 err = -ENOENT;
399 } else if (gluebi->refcnt)
400 err = -EBUSY;
401 else
402 list_del(&gluebi->list);
403 mutex_unlock(&devices_mutex);
404 if (err)
405 return err;
801c135c 406
2ba3d76a 407 mtd = &gluebi->mtd;
801c135c 408 err = del_mtd_device(mtd);
2ba3d76a
DP
409 if (err) {
410 err_msg("cannot remove fake MTD device %d, UBI device %d, "
411 "volume %d, error %d", mtd->index, gluebi->ubi_num,
412 gluebi->vol_id, err);
413 mutex_lock(&devices_mutex);
414 list_add_tail(&gluebi->list, &gluebi_devices);
415 mutex_unlock(&devices_mutex);
801c135c 416 return err;
2ba3d76a
DP
417 }
418
801c135c 419 kfree(mtd->name);
2ba3d76a 420 kfree(gluebi);
801c135c
AB
421 return 0;
422}
941dfb07
AB
423
424/**
2ba3d76a
DP
425 * gluebi_updated - UBI volume was updated notifier.
426 * @vi: volume info structure
941dfb07 427 *
2ba3d76a
DP
428 * This function is called every time an UBI volume is updated. It does nothing
429 * if te volume @vol is dynamic, and changes MTD device size if the
941dfb07 430 * volume is static. This is needed because static volumes cannot be read past
2ba3d76a
DP
431 * data they contain. This function returns zero in case of success and a
432 * negative error code in case of error.
433 */
434static int gluebi_updated(struct ubi_volume_info *vi)
435{
436 struct gluebi_device *gluebi;
437
438 mutex_lock(&devices_mutex);
439 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
440 if (!gluebi) {
441 mutex_unlock(&devices_mutex);
442 err_msg("got update notification for unknown UBI device %d "
443 "volume %d", vi->ubi_num, vi->vol_id);
444 return -ENOENT;
445 }
446
447 if (vi->vol_type == UBI_STATIC_VOLUME)
448 gluebi->mtd.size = vi->used_bytes;
449 mutex_unlock(&devices_mutex);
450 return 0;
451}
452
453/**
454 * gluebi_resized - UBI volume was re-sized notifier.
455 * @vi: volume info structure
456 *
457 * This function is called every time an UBI volume is re-size. It changes the
458 * corresponding fake MTD device size. This function returns zero in case of
459 * success and a negative error code in case of error.
460 */
461static int gluebi_resized(struct ubi_volume_info *vi)
462{
463 struct gluebi_device *gluebi;
464
465 mutex_lock(&devices_mutex);
466 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
467 if (!gluebi) {
468 mutex_unlock(&devices_mutex);
469 err_msg("got update notification for unknown UBI device %d "
470 "volume %d", vi->ubi_num, vi->vol_id);
471 return -ENOENT;
472 }
473 gluebi->mtd.size = vi->used_bytes;
474 mutex_unlock(&devices_mutex);
475 return 0;
476}
477
478/**
479 * gluebi_notify - UBI notification handler.
480 * @nb: registered notifier block
481 * @l: notification type
482 * @ptr: pointer to the &struct ubi_notification object
941dfb07 483 */
2ba3d76a
DP
484static int gluebi_notify(struct notifier_block *nb, unsigned long l,
485 void *ns_ptr)
941dfb07 486{
2ba3d76a
DP
487 struct ubi_notification *nt = ns_ptr;
488
489 switch (l) {
490 case UBI_VOLUME_ADDED:
491 gluebi_create(&nt->di, &nt->vi);
492 break;
493 case UBI_VOLUME_REMOVED:
494 gluebi_remove(&nt->vi);
495 break;
496 case UBI_VOLUME_RESIZED:
497 gluebi_resized(&nt->vi);
498 break;
499 case UBI_VOLUME_UPDATED:
500 gluebi_updated(&nt->vi);
501 break;
502 default:
503 break;
504 }
505 return NOTIFY_OK;
506}
941dfb07 507
2ba3d76a
DP
508static struct notifier_block gluebi_notifier = {
509 .notifier_call = gluebi_notify,
510};
511
512static int __init ubi_gluebi_init(void)
513{
514 return ubi_register_volume_notifier(&gluebi_notifier, 0);
941dfb07 515}
2ba3d76a
DP
516
517static void __exit ubi_gluebi_exit(void)
518{
519 struct gluebi_device *gluebi, *g;
520
521 list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) {
522 int err;
523 struct mtd_info *mtd = &gluebi->mtd;
524
525 err = del_mtd_device(mtd);
526 if (err)
527 err_msg("error %d while removing gluebi MTD device %d, "
528 "UBI device %d, volume %d - ignoring", err,
529 mtd->index, gluebi->ubi_num, gluebi->vol_id);
530 kfree(mtd->name);
531 kfree(gluebi);
532 }
533 ubi_unregister_volume_notifier(&gluebi_notifier);
534}
535
536module_init(ubi_gluebi_init);
537module_exit(ubi_gluebi_exit);
538MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
539MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
540MODULE_LICENSE("GPL");
This page took 0.34553 seconds and 5 git commands to generate.