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
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/slab.h>
35 #include <linux/sched.h>
36 #include <linux/math64.h>
37 #include <linux/module.h>
38 #include <linux/mutex.h>
39 #include <linux/mtd/ubi.h>
40 #include <linux/mtd/mtd.h>
41 #include "ubi-media.h"
43 #define err_msg(fmt, ...) \
44 printk(KERN_DEBUG "gluebi (pid %d): %s: " fmt "\n", \
45 current->pid, __func__, ##__VA_ARGS__)
48 * struct gluebi_device - a gluebi device description data structure.
49 * @mtd: emulated MTD device description object
50 * @refcnt: gluebi device reference count
51 * @desc: UBI volume descriptor
52 * @ubi_num: UBI device number this gluebi device works on
53 * @vol_id: ID of UBI volume this gluebi device works on
54 * @list: link in a list of gluebi devices
56 struct gluebi_device
{
59 struct ubi_volume_desc
*desc
;
62 struct list_head list
;
65 /* List of all gluebi devices */
66 static LIST_HEAD(gluebi_devices
);
67 static DEFINE_MUTEX(devices_mutex
);
70 * find_gluebi_nolock - find a gluebi device.
71 * @ubi_num: UBI device number
74 * This function seraches for gluebi device corresponding to UBI device
75 * @ubi_num and UBI volume @vol_id. Returns the gluebi device description
76 * object in case of success and %NULL in case of failure. The caller has to
77 * have the &devices_mutex locked.
79 static struct gluebi_device
*find_gluebi_nolock(int ubi_num
, int vol_id
)
81 struct gluebi_device
*gluebi
;
83 list_for_each_entry(gluebi
, &gluebi_devices
, list
)
84 if (gluebi
->ubi_num
== ubi_num
&& gluebi
->vol_id
== vol_id
)
90 * gluebi_get_device - get MTD device reference.
91 * @mtd: the MTD device description object
93 * This function is called every time the MTD device is being opened and
94 * implements the MTD get_device() operation. Returns zero in case of success
95 * and a negative error code in case of failure.
97 static int gluebi_get_device(struct mtd_info
*mtd
)
99 struct gluebi_device
*gluebi
;
100 int ubi_mode
= UBI_READONLY
;
102 if (!try_module_get(THIS_MODULE
))
105 if (mtd
->flags
& MTD_WRITEABLE
)
106 ubi_mode
= UBI_READWRITE
;
108 gluebi
= container_of(mtd
, struct gluebi_device
, mtd
);
109 mutex_lock(&devices_mutex
);
110 if (gluebi
->refcnt
> 0) {
112 * The MTD device is already referenced and this is just one
113 * more reference. MTD allows many users to open the same
114 * volume simultaneously and do not distinguish between
115 * readers/writers/exclusive openers as UBI does. So we do not
116 * open the UBI volume again - just increase the reference
117 * counter and return.
120 mutex_unlock(&devices_mutex
);
125 * This is the first reference to this UBI volume via the MTD device
126 * interface. Open the corresponding volume in read-write mode.
128 gluebi
->desc
= ubi_open_volume(gluebi
->ubi_num
, gluebi
->vol_id
,
130 if (IS_ERR(gluebi
->desc
)) {
131 mutex_unlock(&devices_mutex
);
132 module_put(THIS_MODULE
);
133 return PTR_ERR(gluebi
->desc
);
136 mutex_unlock(&devices_mutex
);
141 * gluebi_put_device - put MTD device reference.
142 * @mtd: the MTD device description object
144 * This function is called every time the MTD device is being put. Returns
145 * zero in case of success and a negative error code in case of failure.
147 static void gluebi_put_device(struct mtd_info
*mtd
)
149 struct gluebi_device
*gluebi
;
151 gluebi
= container_of(mtd
, struct gluebi_device
, mtd
);
152 mutex_lock(&devices_mutex
);
154 if (gluebi
->refcnt
== 0)
155 ubi_close_volume(gluebi
->desc
);
156 module_put(THIS_MODULE
);
157 mutex_unlock(&devices_mutex
);
161 * gluebi_read - read operation of emulated MTD devices.
162 * @mtd: MTD device description object
163 * @from: absolute offset from where to read
164 * @len: how many bytes to read
165 * @retlen: count of read bytes is returned here
166 * @buf: buffer to store the read data
168 * This function returns zero in case of success and a negative error code in
171 static int gluebi_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
172 size_t *retlen
, unsigned char *buf
)
174 int err
= 0, lnum
, offs
, total_read
;
175 struct gluebi_device
*gluebi
;
177 gluebi
= container_of(mtd
, struct gluebi_device
, mtd
);
178 lnum
= div_u64_rem(from
, mtd
->erasesize
, &offs
);
181 size_t to_read
= mtd
->erasesize
- offs
;
183 if (to_read
> total_read
)
184 to_read
= total_read
;
186 err
= ubi_read(gluebi
->desc
, lnum
, buf
, offs
, to_read
);
192 total_read
-= to_read
;
196 *retlen
= len
- total_read
;
201 * gluebi_write - write operation of emulated MTD devices.
202 * @mtd: MTD device description object
203 * @to: absolute offset where to write
204 * @len: how many bytes to write
205 * @retlen: count of written bytes is returned here
206 * @buf: buffer with data to write
208 * This function returns zero in case of success and a negative error code in
211 static int gluebi_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
212 size_t *retlen
, const u_char
*buf
)
214 int err
= 0, lnum
, offs
, total_written
;
215 struct gluebi_device
*gluebi
;
217 gluebi
= container_of(mtd
, struct gluebi_device
, mtd
);
218 lnum
= div_u64_rem(to
, mtd
->erasesize
, &offs
);
220 if (len
% mtd
->writesize
|| offs
% mtd
->writesize
)
224 while (total_written
) {
225 size_t to_write
= mtd
->erasesize
- offs
;
227 if (to_write
> total_written
)
228 to_write
= total_written
;
230 err
= ubi_write(gluebi
->desc
, lnum
, buf
, offs
, to_write
);
236 total_written
-= to_write
;
240 *retlen
= len
- total_written
;
245 * gluebi_erase - erase operation of emulated MTD devices.
246 * @mtd: the MTD device description object
247 * @instr: the erase operation description
249 * This function calls the erase callback when finishes. Returns zero in case
250 * of success and a negative error code in case of failure.
252 static int gluebi_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
254 int err
, i
, lnum
, count
;
255 struct gluebi_device
*gluebi
;
257 if (mtd_mod_by_ws(instr
->addr
, mtd
) || mtd_mod_by_ws(instr
->len
, mtd
))
260 lnum
= mtd_div_by_eb(instr
->addr
, mtd
);
261 count
= mtd_div_by_eb(instr
->len
, mtd
);
262 gluebi
= container_of(mtd
, struct gluebi_device
, mtd
);
264 for (i
= 0; i
< count
- 1; i
++) {
265 err
= ubi_leb_unmap(gluebi
->desc
, lnum
+ i
);
270 * MTD erase operations are synchronous, so we have to make sure the
271 * physical eraseblock is wiped out.
273 * Thus, perform leb_erase instead of leb_unmap operation - leb_erase
274 * will wait for the end of operations
276 err
= ubi_leb_erase(gluebi
->desc
, lnum
+ i
);
280 instr
->state
= MTD_ERASE_DONE
;
281 mtd_erase_callback(instr
);
285 instr
->state
= MTD_ERASE_FAILED
;
286 instr
->fail_addr
= (long long)lnum
* mtd
->erasesize
;
291 * gluebi_create - create a gluebi device for an UBI volume.
292 * @di: UBI device description object
293 * @vi: UBI volume description object
295 * This function is called when a new UBI volume is created in order to create
296 * corresponding fake MTD device. Returns zero in case of success and a
297 * negative error code in case of failure.
299 static int gluebi_create(struct ubi_device_info
*di
,
300 struct ubi_volume_info
*vi
)
302 struct gluebi_device
*gluebi
, *g
;
303 struct mtd_info
*mtd
;
305 gluebi
= kzalloc(sizeof(struct gluebi_device
), GFP_KERNEL
);
310 mtd
->name
= kmemdup(vi
->name
, vi
->name_len
+ 1, GFP_KERNEL
);
316 gluebi
->vol_id
= vi
->vol_id
;
317 gluebi
->ubi_num
= vi
->ubi_num
;
318 mtd
->type
= MTD_UBIVOLUME
;
320 mtd
->flags
= MTD_WRITEABLE
;
321 mtd
->owner
= THIS_MODULE
;
322 mtd
->writesize
= di
->min_io_size
;
323 mtd
->erasesize
= vi
->usable_leb_size
;
324 mtd
->_read
= gluebi_read
;
325 mtd
->_write
= gluebi_write
;
326 mtd
->_erase
= gluebi_erase
;
327 mtd
->_get_device
= gluebi_get_device
;
328 mtd
->_put_device
= gluebi_put_device
;
331 * In case of dynamic a volume, MTD device size is just volume size. In
332 * case of a static volume the size is equivalent to the amount of data
335 if (vi
->vol_type
== UBI_DYNAMIC_VOLUME
)
336 mtd
->size
= (unsigned long long)vi
->usable_leb_size
* vi
->size
;
338 mtd
->size
= vi
->used_bytes
;
340 /* Just a sanity check - make sure this gluebi device does not exist */
341 mutex_lock(&devices_mutex
);
342 g
= find_gluebi_nolock(vi
->ubi_num
, vi
->vol_id
);
344 err_msg("gluebi MTD device %d form UBI device %d volume %d "
345 "already exists", g
->mtd
.index
, vi
->ubi_num
,
347 mutex_unlock(&devices_mutex
);
349 if (mtd_device_register(mtd
, NULL
, 0)) {
350 err_msg("cannot add MTD device");
356 mutex_lock(&devices_mutex
);
357 list_add_tail(&gluebi
->list
, &gluebi_devices
);
358 mutex_unlock(&devices_mutex
);
363 * gluebi_remove - remove a gluebi device.
364 * @vi: UBI volume description object
366 * This function is called when an UBI volume is removed and it removes
367 * corresponding fake MTD device. Returns zero in case of success and a
368 * negative error code in case of failure.
370 static int gluebi_remove(struct ubi_volume_info
*vi
)
373 struct mtd_info
*mtd
;
374 struct gluebi_device
*gluebi
;
376 mutex_lock(&devices_mutex
);
377 gluebi
= find_gluebi_nolock(vi
->ubi_num
, vi
->vol_id
);
379 err_msg("got remove notification for unknown UBI device %d "
380 "volume %d", vi
->ubi_num
, vi
->vol_id
);
382 } else if (gluebi
->refcnt
)
385 list_del(&gluebi
->list
);
386 mutex_unlock(&devices_mutex
);
391 err
= mtd_device_unregister(mtd
);
393 err_msg("cannot remove fake MTD device %d, UBI device %d, "
394 "volume %d, error %d", mtd
->index
, gluebi
->ubi_num
,
395 gluebi
->vol_id
, err
);
396 mutex_lock(&devices_mutex
);
397 list_add_tail(&gluebi
->list
, &gluebi_devices
);
398 mutex_unlock(&devices_mutex
);
408 * gluebi_updated - UBI volume was updated notifier.
409 * @vi: volume info structure
411 * This function is called every time an UBI volume is updated. It does nothing
412 * if te volume @vol is dynamic, and changes MTD device size if the
413 * volume is static. This is needed because static volumes cannot be read past
414 * data they contain. This function returns zero in case of success and a
415 * negative error code in case of error.
417 static int gluebi_updated(struct ubi_volume_info
*vi
)
419 struct gluebi_device
*gluebi
;
421 mutex_lock(&devices_mutex
);
422 gluebi
= find_gluebi_nolock(vi
->ubi_num
, vi
->vol_id
);
424 mutex_unlock(&devices_mutex
);
425 err_msg("got update notification for unknown UBI device %d "
426 "volume %d", vi
->ubi_num
, vi
->vol_id
);
430 if (vi
->vol_type
== UBI_STATIC_VOLUME
)
431 gluebi
->mtd
.size
= vi
->used_bytes
;
432 mutex_unlock(&devices_mutex
);
437 * gluebi_resized - UBI volume was re-sized notifier.
438 * @vi: volume info structure
440 * This function is called every time an UBI volume is re-size. It changes the
441 * corresponding fake MTD device size. This function returns zero in case of
442 * success and a negative error code in case of error.
444 static int gluebi_resized(struct ubi_volume_info
*vi
)
446 struct gluebi_device
*gluebi
;
448 mutex_lock(&devices_mutex
);
449 gluebi
= find_gluebi_nolock(vi
->ubi_num
, vi
->vol_id
);
451 mutex_unlock(&devices_mutex
);
452 err_msg("got update notification for unknown UBI device %d "
453 "volume %d", vi
->ubi_num
, vi
->vol_id
);
456 gluebi
->mtd
.size
= vi
->used_bytes
;
457 mutex_unlock(&devices_mutex
);
462 * gluebi_notify - UBI notification handler.
463 * @nb: registered notifier block
464 * @l: notification type
465 * @ptr: pointer to the &struct ubi_notification object
467 static int gluebi_notify(struct notifier_block
*nb
, unsigned long l
,
470 struct ubi_notification
*nt
= ns_ptr
;
473 case UBI_VOLUME_ADDED
:
474 gluebi_create(&nt
->di
, &nt
->vi
);
476 case UBI_VOLUME_REMOVED
:
477 gluebi_remove(&nt
->vi
);
479 case UBI_VOLUME_RESIZED
:
480 gluebi_resized(&nt
->vi
);
482 case UBI_VOLUME_UPDATED
:
483 gluebi_updated(&nt
->vi
);
491 static struct notifier_block gluebi_notifier
= {
492 .notifier_call
= gluebi_notify
,
495 static int __init
ubi_gluebi_init(void)
497 return ubi_register_volume_notifier(&gluebi_notifier
, 0);
500 static void __exit
ubi_gluebi_exit(void)
502 struct gluebi_device
*gluebi
, *g
;
504 list_for_each_entry_safe(gluebi
, g
, &gluebi_devices
, list
) {
506 struct mtd_info
*mtd
= &gluebi
->mtd
;
508 err
= mtd_device_unregister(mtd
);
510 err_msg("error %d while removing gluebi MTD device %d, "
511 "UBI device %d, volume %d - ignoring", err
,
512 mtd
->index
, gluebi
->ubi_num
, gluebi
->vol_id
);
516 ubi_unregister_volume_notifier(&gluebi_notifier
);
519 module_init(ubi_gluebi_init
);
520 module_exit(ubi_gluebi_exit
);
521 MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
522 MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
523 MODULE_LICENSE("GPL");