First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / mtd / ubi / gluebi.c
blobb5e478fa26612b188aa664541503457b2e58d2af
1 /*
2 * Copyright (c) International Business Machines Corp., 2006
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.
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.
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
18 * Author: Artem Bityutskiy (Битюцкий Артём), Joern Engel
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.
27 * Gluebi emulates MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
28 * size (@mtd->writesize) is equivalent to the UBI minimal I/O unit. The
29 * eraseblock size is equivalent to the logical eraseblock size of the volume.
32 #include <linux/err.h>
33 #include <linux/list.h>
34 #include <linux/sched.h>
35 #include <linux/math64.h>
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"
42 #define err_msg(fmt, ...) \
43 printk(KERN_DEBUG "gluebi (pid %d): %s: " fmt "\n", \
44 current->pid, __func__, ##__VA_ARGS__)
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
55 struct 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;
64 /* List of all gluebi devices */
65 static LIST_HEAD(gluebi_devices);
66 static DEFINE_MUTEX(devices_mutex);
68 /**
69 * find_gluebi_nolock - find a gluebi device.
70 * @ubi_num: UBI device number
71 * @vol_id: volume ID
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.
78 static struct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
80 struct gluebi_device *gluebi;
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;
88 /**
89 * gluebi_get_device - get MTD device reference.
90 * @mtd: the MTD device description object
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.
96 static int gluebi_get_device(struct mtd_info *mtd)
98 struct gluebi_device *gluebi;
99 int ubi_mode = UBI_READONLY;
101 if (!try_module_get(THIS_MODULE))
102 return -ENODEV;
104 if (mtd->flags & MTD_WRITEABLE)
105 ubi_mode = UBI_READWRITE;
107 gluebi = container_of(mtd, struct gluebi_device, mtd);
108 mutex_lock(&devices_mutex);
109 if (gluebi->refcnt > 0) {
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.
118 gluebi->refcnt += 1;
119 mutex_unlock(&devices_mutex);
120 return 0;
124 * This is the first reference to this UBI volume via the MTD device
125 * interface. Open the corresponding volume in read-write mode.
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);
134 gluebi->refcnt += 1;
135 mutex_unlock(&devices_mutex);
136 return 0;
140 * gluebi_put_device - put MTD device reference.
141 * @mtd: the MTD device description object
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.
146 static void gluebi_put_device(struct mtd_info *mtd)
148 struct gluebi_device *gluebi;
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);
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
167 * This function returns zero in case of success and a negative error code in
168 * case of failure.
170 static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
171 size_t *retlen, unsigned char *buf)
173 int err = 0, lnum, offs, total_read;
174 struct gluebi_device *gluebi;
176 if (len < 0 || from < 0 || from + len > mtd->size)
177 return -EINVAL;
179 gluebi = container_of(mtd, struct gluebi_device, mtd);
181 lnum = div_u64_rem(from, mtd->erasesize, &offs);
182 total_read = len;
183 while (total_read) {
184 size_t to_read = mtd->erasesize - offs;
186 if (to_read > total_read)
187 to_read = total_read;
189 err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
190 if (err)
191 break;
193 lnum += 1;
194 offs = 0;
195 total_read -= to_read;
196 buf += to_read;
199 *retlen = len - total_read;
200 return err;
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
211 * This function returns zero in case of success and a negative error code in
212 * case of failure.
214 static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
215 size_t *retlen, const u_char *buf)
217 int err = 0, lnum, offs, total_written;
218 struct gluebi_device *gluebi;
220 if (len < 0 || to < 0 || len + to > mtd->size)
221 return -EINVAL;
223 gluebi = container_of(mtd, struct gluebi_device, mtd);
225 if (!(mtd->flags & MTD_WRITEABLE))
226 return -EROFS;
228 lnum = div_u64_rem(to, mtd->erasesize, &offs);
230 if (len % mtd->writesize || offs % mtd->writesize)
231 return -EINVAL;
233 total_written = len;
234 while (total_written) {
235 size_t to_write = mtd->erasesize - offs;
237 if (to_write > total_written)
238 to_write = total_written;
240 err = ubi_write(gluebi->desc, lnum, buf, offs, to_write);
241 if (err)
242 break;
244 lnum += 1;
245 offs = 0;
246 total_written -= to_write;
247 buf += to_write;
250 *retlen = len - total_written;
251 return err;
255 * gluebi_erase - erase operation of emulated MTD devices.
256 * @mtd: the MTD device description object
257 * @instr: the erase operation description
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.
262 static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
264 int err, i, lnum, count;
265 struct gluebi_device *gluebi;
267 if (instr->addr < 0 || instr->addr > mtd->size - mtd->erasesize)
268 return -EINVAL;
269 if (instr->len < 0 || instr->addr + instr->len > mtd->size)
270 return -EINVAL;
271 if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
272 return -EINVAL;
274 lnum = mtd_div_by_eb(instr->addr, mtd);
275 count = mtd_div_by_eb(instr->len, mtd);
277 gluebi = container_of(mtd, struct gluebi_device, mtd);
279 if (!(mtd->flags & MTD_WRITEABLE))
280 return -EROFS;
282 for (i = 0; i < count - 1; i++) {
283 err = ubi_leb_unmap(gluebi->desc, lnum + i);
284 if (err)
285 goto out_err;
288 * MTD erase operations are synchronous, so we have to make sure the
289 * physical eraseblock is wiped out.
291 * Thus, perform leb_erase instead of leb_unmap operation - leb_erase
292 * will wait for the end of operations
294 err = ubi_leb_erase(gluebi->desc, lnum + i);
295 if (err)
296 goto out_err;
298 instr->state = MTD_ERASE_DONE;
299 mtd_erase_callback(instr);
300 return 0;
302 out_err:
303 instr->state = MTD_ERASE_FAILED;
304 instr->fail_addr = (long long)lnum * mtd->erasesize;
305 return err;
309 * gluebi_create - create a gluebi device for an UBI volume.
310 * @di: UBI device description object
311 * @vi: UBI volume description object
313 * This function is called when a new UBI volume is created in order to create
314 * corresponding fake MTD device. Returns zero in case of success and a
315 * negative error code in case of failure.
317 static int gluebi_create(struct ubi_device_info *di,
318 struct ubi_volume_info *vi)
320 struct gluebi_device *gluebi, *g;
321 struct mtd_info *mtd;
323 gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL);
324 if (!gluebi)
325 return -ENOMEM;
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;
334 gluebi->vol_id = vi->vol_id;
335 gluebi->ubi_num = vi->ubi_num;
336 mtd->type = MTD_UBIVOLUME;
337 if (!di->ro_mode)
338 mtd->flags = MTD_WRITEABLE;
339 mtd->owner = THIS_MODULE;
340 mtd->writesize = di->min_io_size;
341 mtd->erasesize = vi->usable_leb_size;
342 mtd->read = gluebi_read;
343 mtd->write = gluebi_write;
344 mtd->erase = gluebi_erase;
345 mtd->get_device = gluebi_get_device;
346 mtd->put_device = gluebi_put_device;
349 * In case of dynamic a volume, MTD device size is just volume size. In
350 * case of a static volume the size is equivalent to the amount of data
351 * bytes.
353 if (vi->vol_type == UBI_DYNAMIC_VOLUME)
354 mtd->size = (unsigned long long)vi->usable_leb_size * vi->size;
355 else
356 mtd->size = vi->used_bytes;
358 /* Just a sanity check - make sure this gluebi device does not exist */
359 mutex_lock(&devices_mutex);
360 g = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
361 if (g)
362 err_msg("gluebi MTD device %d form UBI device %d volume %d "
363 "already exists", g->mtd.index, vi->ubi_num,
364 vi->vol_id);
365 mutex_unlock(&devices_mutex);
367 if (add_mtd_device(mtd)) {
368 err_msg("cannot add MTD device");
369 kfree(mtd->name);
370 kfree(gluebi);
371 return -ENFILE;
374 mutex_lock(&devices_mutex);
375 list_add_tail(&gluebi->list, &gluebi_devices);
376 mutex_unlock(&devices_mutex);
377 return 0;
381 * gluebi_remove - remove a gluebi device.
382 * @vi: UBI volume description object
384 * This function is called when an UBI volume is removed and it removes
385 * corresponding fake MTD device. Returns zero in case of success and a
386 * negative error code in case of failure.
388 static int gluebi_remove(struct ubi_volume_info *vi)
390 int err = 0;
391 struct mtd_info *mtd;
392 struct gluebi_device *gluebi;
394 mutex_lock(&devices_mutex);
395 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
396 if (!gluebi) {
397 err_msg("got remove notification for unknown UBI device %d "
398 "volume %d", vi->ubi_num, vi->vol_id);
399 err = -ENOENT;
400 } else if (gluebi->refcnt)
401 err = -EBUSY;
402 else
403 list_del(&gluebi->list);
404 mutex_unlock(&devices_mutex);
405 if (err)
406 return err;
408 mtd = &gluebi->mtd;
409 err = del_mtd_device(mtd);
410 if (err) {
411 err_msg("cannot remove fake MTD device %d, UBI device %d, "
412 "volume %d, error %d", mtd->index, gluebi->ubi_num,
413 gluebi->vol_id, err);
414 mutex_lock(&devices_mutex);
415 list_add_tail(&gluebi->list, &gluebi_devices);
416 mutex_unlock(&devices_mutex);
417 return err;
420 kfree(mtd->name);
421 kfree(gluebi);
422 return 0;
426 * gluebi_updated - UBI volume was updated notifier.
427 * @vi: volume info structure
429 * This function is called every time an UBI volume is updated. It does nothing
430 * if te volume @vol is dynamic, and changes MTD device size if the
431 * volume is static. This is needed because static volumes cannot be read past
432 * data they contain. This function returns zero in case of success and a
433 * negative error code in case of error.
435 static int gluebi_updated(struct ubi_volume_info *vi)
437 struct gluebi_device *gluebi;
439 mutex_lock(&devices_mutex);
440 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
441 if (!gluebi) {
442 mutex_unlock(&devices_mutex);
443 err_msg("got update notification for unknown UBI device %d "
444 "volume %d", vi->ubi_num, vi->vol_id);
445 return -ENOENT;
448 if (vi->vol_type == UBI_STATIC_VOLUME)
449 gluebi->mtd.size = vi->used_bytes;
450 mutex_unlock(&devices_mutex);
451 return 0;
455 * gluebi_resized - UBI volume was re-sized notifier.
456 * @vi: volume info structure
458 * This function is called every time an UBI volume is re-size. It changes the
459 * corresponding fake MTD device size. This function returns zero in case of
460 * success and a negative error code in case of error.
462 static int gluebi_resized(struct ubi_volume_info *vi)
464 struct gluebi_device *gluebi;
466 mutex_lock(&devices_mutex);
467 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
468 if (!gluebi) {
469 mutex_unlock(&devices_mutex);
470 err_msg("got update notification for unknown UBI device %d "
471 "volume %d", vi->ubi_num, vi->vol_id);
472 return -ENOENT;
474 gluebi->mtd.size = vi->used_bytes;
475 mutex_unlock(&devices_mutex);
476 return 0;
480 * gluebi_notify - UBI notification handler.
481 * @nb: registered notifier block
482 * @l: notification type
483 * @ptr: pointer to the &struct ubi_notification object
485 static int gluebi_notify(struct notifier_block *nb, unsigned long l,
486 void *ns_ptr)
488 struct ubi_notification *nt = ns_ptr;
490 switch (l) {
491 case UBI_VOLUME_ADDED:
492 gluebi_create(&nt->di, &nt->vi);
493 break;
494 case UBI_VOLUME_REMOVED:
495 gluebi_remove(&nt->vi);
496 break;
497 case UBI_VOLUME_RESIZED:
498 gluebi_resized(&nt->vi);
499 break;
500 case UBI_VOLUME_UPDATED:
501 gluebi_updated(&nt->vi);
502 break;
503 default:
504 break;
506 return NOTIFY_OK;
509 static struct notifier_block gluebi_notifier = {
510 .notifier_call = gluebi_notify,
513 static int __init ubi_gluebi_init(void)
515 return ubi_register_volume_notifier(&gluebi_notifier, 0);
518 static void __exit ubi_gluebi_exit(void)
520 struct gluebi_device *gluebi, *g;
522 list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) {
523 int err;
524 struct mtd_info *mtd = &gluebi->mtd;
526 err = del_mtd_device(mtd);
527 if (err)
528 err_msg("error %d while removing gluebi MTD device %d, "
529 "UBI device %d, volume %d - ignoring", err,
530 mtd->index, gluebi->ubi_num, gluebi->vol_id);
531 kfree(mtd->name);
532 kfree(gluebi);
534 ubi_unregister_volume_notifier(&gluebi_notifier);
537 module_init(ubi_gluebi_init);
538 module_exit(ubi_gluebi_exit);
539 MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
540 MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
541 MODULE_LICENSE("GPL");