import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
5a0e3ad6 34#include <linux/slab.h>
2ba3d76a 35#include <linux/sched.h>
3013ee31 36#include <linux/math64.h>
2ba3d76a
DP
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"
42
43#define err_msg(fmt, ...) \
e28453bb 44 pr_err("gluebi (pid %d): %s: " fmt "\n", \
2ba3d76a
DP
45 current->pid, __func__, ##__VA_ARGS__)
46
47/**
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
55 */
56struct gluebi_device {
57 struct mtd_info mtd;
58 int refcnt;
59 struct ubi_volume_desc *desc;
60 int ubi_num;
61 int vol_id;
62 struct list_head list;
63};
64
65/* List of all gluebi devices */
66static LIST_HEAD(gluebi_devices);
67static DEFINE_MUTEX(devices_mutex);
68
69/**
70 * find_gluebi_nolock - find a gluebi device.
71 * @ubi_num: UBI device number
72 * @vol_id: volume ID
73 *
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.
78 */
79static struct gluebi_device *find_gluebi_nolock(int ubi_num, int vol_id)
80{
81 struct gluebi_device *gluebi;
82
83 list_for_each_entry(gluebi, &gluebi_devices, list)
84 if (gluebi->ubi_num == ubi_num && gluebi->vol_id == vol_id)
85 return gluebi;
86 return NULL;
87}
801c135c
AB
88
89/**
90 * gluebi_get_device - get MTD device reference.
91 * @mtd: the MTD device description object
92 *
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.
96 */
97static int gluebi_get_device(struct mtd_info *mtd)
98{
2ba3d76a
DP
99 struct gluebi_device *gluebi;
100 int ubi_mode = UBI_READONLY;
801c135c 101
2ba3d76a
DP
102 if (!try_module_get(THIS_MODULE))
103 return -ENODEV;
801c135c 104
2ba3d76a
DP
105 if (mtd->flags & MTD_WRITEABLE)
106 ubi_mode = UBI_READWRITE;
107
108 gluebi = container_of(mtd, struct gluebi_device, mtd);
109 mutex_lock(&devices_mutex);
110 if (gluebi->refcnt > 0) {
801c135c
AB
111 /*
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.
118 */
2ba3d76a
DP
119 gluebi->refcnt += 1;
120 mutex_unlock(&devices_mutex);
801c135c
AB
121 return 0;
122 }
123
124 /*
125 * This is the first reference to this UBI volume via the MTD device
126 * interface. Open the corresponding volume in read-write mode.
127 */
2ba3d76a
DP
128 gluebi->desc = ubi_open_volume(gluebi->ubi_num, gluebi->vol_id,
129 ubi_mode);
130 if (IS_ERR(gluebi->desc)) {
131 mutex_unlock(&devices_mutex);
132 module_put(THIS_MODULE);
133 return PTR_ERR(gluebi->desc);
134 }
135 gluebi->refcnt += 1;
136 mutex_unlock(&devices_mutex);
801c135c
AB
137 return 0;
138}
139
140/**
141 * gluebi_put_device - put MTD device reference.
142 * @mtd: the MTD device description object
143 *
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.
146 */
147static void gluebi_put_device(struct mtd_info *mtd)
148{
2ba3d76a
DP
149 struct gluebi_device *gluebi;
150
151 gluebi = container_of(mtd, struct gluebi_device, mtd);
152 mutex_lock(&devices_mutex);
153 gluebi->refcnt -= 1;
154 if (gluebi->refcnt == 0)
155 ubi_close_volume(gluebi->desc);
156 module_put(THIS_MODULE);
157 mutex_unlock(&devices_mutex);
801c135c
AB
158}
159
160/**
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
167 *
168 * This function returns zero in case of success and a negative error code in
169 * case of failure.
170 */
171static int gluebi_read(struct mtd_info *mtd, loff_t from, size_t len,
172 size_t *retlen, unsigned char *buf)
173{
41c04384 174 int err = 0, lnum, offs, bytes_left;
2ba3d76a 175 struct gluebi_device *gluebi;
801c135c 176
2ba3d76a 177 gluebi = container_of(mtd, struct gluebi_device, mtd);
3013ee31 178 lnum = div_u64_rem(from, mtd->erasesize, &offs);
41c04384
EG
179 bytes_left = len;
180 while (bytes_left) {
801c135c
AB
181 size_t to_read = mtd->erasesize - offs;
182
41c04384
EG
183 if (to_read > bytes_left)
184 to_read = bytes_left;
801c135c 185
6fa3eb70
S
186 //err = ubi_read(gluebi->desc, lnum, buf, offs, to_read);
187 err = ubi_leb_read(gluebi->desc, lnum, buf, offs, to_read, 0);
801c135c
AB
188 if (err)
189 break;
190
191 lnum += 1;
192 offs = 0;
41c04384 193 bytes_left -= to_read;
801c135c
AB
194 buf += to_read;
195 }
196
41c04384 197 *retlen = len - bytes_left;
801c135c
AB
198 return err;
199}
200
201/**
202 * gluebi_write - write operation of emulated MTD devices.
203 * @mtd: MTD device description object
204 * @to: absolute offset where to write
205 * @len: how many bytes to write
206 * @retlen: count of written bytes is returned here
207 * @buf: buffer with data to write
208 *
209 * This function returns zero in case of success and a negative error code in
210 * case of failure.
211 */
212static int gluebi_write(struct mtd_info *mtd, loff_t to, size_t len,
2ba3d76a 213 size_t *retlen, const u_char *buf)
801c135c 214{
41c04384 215 int err = 0, lnum, offs, bytes_left;
2ba3d76a 216 struct gluebi_device *gluebi;
801c135c 217
2ba3d76a 218 gluebi = container_of(mtd, struct gluebi_device, mtd);
3013ee31 219 lnum = div_u64_rem(to, mtd->erasesize, &offs);
801c135c
AB
220
221 if (len % mtd->writesize || offs % mtd->writesize)
222 return -EINVAL;
223
41c04384
EG
224 bytes_left = len;
225 while (bytes_left) {
801c135c
AB
226 size_t to_write = mtd->erasesize - offs;
227
41c04384
EG
228 if (to_write > bytes_left)
229 to_write = bytes_left;
801c135c 230
6fa3eb70 231 //err = ubi_write(gluebi->desc, lnum, buf, offs, to_write);
b36a261e 232 err = ubi_leb_write(gluebi->desc, lnum, buf, offs, to_write);
801c135c
AB
233 if (err)
234 break;
235
236 lnum += 1;
237 offs = 0;
41c04384 238 bytes_left -= to_write;
801c135c
AB
239 buf += to_write;
240 }
241
41c04384 242 *retlen = len - bytes_left;
801c135c
AB
243 return err;
244}
245
246/**
247 * gluebi_erase - erase operation of emulated MTD devices.
248 * @mtd: the MTD device description object
249 * @instr: the erase operation description
250 *
251 * This function calls the erase callback when finishes. Returns zero in case
252 * of success and a negative error code in case of failure.
253 */
254static int gluebi_erase(struct mtd_info *mtd, struct erase_info *instr)
255{
256 int err, i, lnum, count;
2ba3d76a 257 struct gluebi_device *gluebi;
801c135c 258
69423d99 259 if (mtd_mod_by_ws(instr->addr, mtd) || mtd_mod_by_ws(instr->len, mtd))
801c135c
AB
260 return -EINVAL;
261
69423d99
AH
262 lnum = mtd_div_by_eb(instr->addr, mtd);
263 count = mtd_div_by_eb(instr->len, mtd);
2ba3d76a 264 gluebi = container_of(mtd, struct gluebi_device, mtd);
801c135c 265
6fa3eb70
S
266#ifdef MTK_IPOH_SUPPORT
267 for (i = 0; i < count; i++) {
268 err = ubi_leb_unmap(gluebi->desc, lnum + i);
269 if (err)
270 goto out_err;
271 }
272#else
2ba3d76a
DP
273 for (i = 0; i < count - 1; i++) {
274 err = ubi_leb_unmap(gluebi->desc, lnum + i);
801c135c
AB
275 if (err)
276 goto out_err;
277 }
801c135c
AB
278 /*
279 * MTD erase operations are synchronous, so we have to make sure the
280 * physical eraseblock is wiped out.
2ba3d76a
DP
281 *
282 * Thus, perform leb_erase instead of leb_unmap operation - leb_erase
283 * will wait for the end of operations
801c135c 284 */
2ba3d76a 285 err = ubi_leb_erase(gluebi->desc, lnum + i);
801c135c
AB
286 if (err)
287 goto out_err;
6fa3eb70 288#endif
801c135c 289
9c9ec147
AB
290 instr->state = MTD_ERASE_DONE;
291 mtd_erase_callback(instr);
801c135c
AB
292 return 0;
293
294out_err:
295 instr->state = MTD_ERASE_FAILED;
69423d99 296 instr->fail_addr = (long long)lnum * mtd->erasesize;
801c135c
AB
297 return err;
298}
299
300/**
2ba3d76a
DP
301 * gluebi_create - create a gluebi device for an UBI volume.
302 * @di: UBI device description object
303 * @vi: UBI volume description object
801c135c 304 *
2ba3d76a 305 * This function is called when a new UBI volume is created in order to create
801c135c
AB
306 * corresponding fake MTD device. Returns zero in case of success and a
307 * negative error code in case of failure.
308 */
2ba3d76a
DP
309static int gluebi_create(struct ubi_device_info *di,
310 struct ubi_volume_info *vi)
801c135c 311{
2ba3d76a
DP
312 struct gluebi_device *gluebi, *g;
313 struct mtd_info *mtd;
801c135c 314
2ba3d76a
DP
315 gluebi = kzalloc(sizeof(struct gluebi_device), GFP_KERNEL);
316 if (!gluebi)
801c135c
AB
317 return -ENOMEM;
318
2ba3d76a
DP
319 mtd = &gluebi->mtd;
320 mtd->name = kmemdup(vi->name, vi->name_len + 1, GFP_KERNEL);
321 if (!mtd->name) {
322 kfree(gluebi);
323 return -ENOMEM;
324 }
325
326 gluebi->vol_id = vi->vol_id;
c8cc4525 327 gluebi->ubi_num = vi->ubi_num;
801c135c 328 mtd->type = MTD_UBIVOLUME;
2ba3d76a 329 if (!di->ro_mode)
801c135c 330 mtd->flags = MTD_WRITEABLE;
801c135c 331 mtd->owner = THIS_MODULE;
2ba3d76a
DP
332 mtd->writesize = di->min_io_size;
333 mtd->erasesize = vi->usable_leb_size;
3c3c10bb
AB
334 mtd->_read = gluebi_read;
335 mtd->_write = gluebi_write;
336 mtd->_erase = gluebi_erase;
337 mtd->_get_device = gluebi_get_device;
338 mtd->_put_device = gluebi_put_device;
801c135c 339
941dfb07 340 /*
2ba3d76a 341 * In case of dynamic a volume, MTD device size is just volume size. In
941dfb07 342 * case of a static volume the size is equivalent to the amount of data
cbd8a9d2 343 * bytes.
941dfb07 344 */
2ba3d76a
DP
345 if (vi->vol_type == UBI_DYNAMIC_VOLUME)
346 mtd->size = (unsigned long long)vi->usable_leb_size * vi->size;
cbd8a9d2 347 else
2ba3d76a
DP
348 mtd->size = vi->used_bytes;
349
350 /* Just a sanity check - make sure this gluebi device does not exist */
351 mutex_lock(&devices_mutex);
352 g = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
353 if (g)
049333ce
AB
354 err_msg("gluebi MTD device %d form UBI device %d volume %d already exists",
355 g->mtd.index, vi->ubi_num, vi->vol_id);
2ba3d76a 356 mutex_unlock(&devices_mutex);
941dfb07 357
ee0e87b1 358 if (mtd_device_register(mtd, NULL, 0)) {
2ba3d76a 359 err_msg("cannot add MTD device");
801c135c 360 kfree(mtd->name);
2ba3d76a 361 kfree(gluebi);
801c135c
AB
362 return -ENFILE;
363 }
364
2ba3d76a
DP
365 mutex_lock(&devices_mutex);
366 list_add_tail(&gluebi->list, &gluebi_devices);
367 mutex_unlock(&devices_mutex);
801c135c
AB
368 return 0;
369}
370
371/**
2ba3d76a
DP
372 * gluebi_remove - remove a gluebi device.
373 * @vi: UBI volume description object
801c135c 374 *
2ba3d76a 375 * This function is called when an UBI volume is removed and it removes
801c135c
AB
376 * corresponding fake MTD device. Returns zero in case of success and a
377 * negative error code in case of failure.
378 */
2ba3d76a 379static int gluebi_remove(struct ubi_volume_info *vi)
801c135c 380{
2ba3d76a
DP
381 int err = 0;
382 struct mtd_info *mtd;
383 struct gluebi_device *gluebi;
384
385 mutex_lock(&devices_mutex);
386 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
387 if (!gluebi) {
049333ce
AB
388 err_msg("got remove notification for unknown UBI device %d volume %d",
389 vi->ubi_num, vi->vol_id);
2ba3d76a
DP
390 err = -ENOENT;
391 } else if (gluebi->refcnt)
392 err = -EBUSY;
393 else
394 list_del(&gluebi->list);
395 mutex_unlock(&devices_mutex);
396 if (err)
397 return err;
801c135c 398
2ba3d76a 399 mtd = &gluebi->mtd;
ee0e87b1 400 err = mtd_device_unregister(mtd);
2ba3d76a 401 if (err) {
049333ce
AB
402 err_msg("cannot remove fake MTD device %d, UBI device %d, volume %d, error %d",
403 mtd->index, gluebi->ubi_num, gluebi->vol_id, err);
2ba3d76a
DP
404 mutex_lock(&devices_mutex);
405 list_add_tail(&gluebi->list, &gluebi_devices);
406 mutex_unlock(&devices_mutex);
801c135c 407 return err;
2ba3d76a
DP
408 }
409
801c135c 410 kfree(mtd->name);
2ba3d76a 411 kfree(gluebi);
801c135c
AB
412 return 0;
413}
941dfb07
AB
414
415/**
2ba3d76a
DP
416 * gluebi_updated - UBI volume was updated notifier.
417 * @vi: volume info structure
941dfb07 418 *
2ba3d76a
DP
419 * This function is called every time an UBI volume is updated. It does nothing
420 * if te volume @vol is dynamic, and changes MTD device size if the
941dfb07 421 * volume is static. This is needed because static volumes cannot be read past
2ba3d76a
DP
422 * data they contain. This function returns zero in case of success and a
423 * negative error code in case of error.
424 */
425static int gluebi_updated(struct ubi_volume_info *vi)
426{
427 struct gluebi_device *gluebi;
428
429 mutex_lock(&devices_mutex);
430 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
431 if (!gluebi) {
432 mutex_unlock(&devices_mutex);
049333ce
AB
433 err_msg("got update notification for unknown UBI device %d volume %d",
434 vi->ubi_num, vi->vol_id);
2ba3d76a
DP
435 return -ENOENT;
436 }
437
438 if (vi->vol_type == UBI_STATIC_VOLUME)
439 gluebi->mtd.size = vi->used_bytes;
440 mutex_unlock(&devices_mutex);
441 return 0;
442}
443
444/**
445 * gluebi_resized - UBI volume was re-sized notifier.
446 * @vi: volume info structure
447 *
448 * This function is called every time an UBI volume is re-size. It changes the
449 * corresponding fake MTD device size. This function returns zero in case of
450 * success and a negative error code in case of error.
451 */
452static int gluebi_resized(struct ubi_volume_info *vi)
453{
454 struct gluebi_device *gluebi;
455
456 mutex_lock(&devices_mutex);
457 gluebi = find_gluebi_nolock(vi->ubi_num, vi->vol_id);
458 if (!gluebi) {
459 mutex_unlock(&devices_mutex);
049333ce
AB
460 err_msg("got update notification for unknown UBI device %d volume %d",
461 vi->ubi_num, vi->vol_id);
2ba3d76a
DP
462 return -ENOENT;
463 }
464 gluebi->mtd.size = vi->used_bytes;
465 mutex_unlock(&devices_mutex);
466 return 0;
467}
468
469/**
470 * gluebi_notify - UBI notification handler.
471 * @nb: registered notifier block
472 * @l: notification type
473 * @ptr: pointer to the &struct ubi_notification object
941dfb07 474 */
2ba3d76a
DP
475static int gluebi_notify(struct notifier_block *nb, unsigned long l,
476 void *ns_ptr)
941dfb07 477{
2ba3d76a
DP
478 struct ubi_notification *nt = ns_ptr;
479
480 switch (l) {
481 case UBI_VOLUME_ADDED:
482 gluebi_create(&nt->di, &nt->vi);
483 break;
484 case UBI_VOLUME_REMOVED:
485 gluebi_remove(&nt->vi);
486 break;
487 case UBI_VOLUME_RESIZED:
488 gluebi_resized(&nt->vi);
489 break;
490 case UBI_VOLUME_UPDATED:
491 gluebi_updated(&nt->vi);
492 break;
493 default:
494 break;
495 }
496 return NOTIFY_OK;
497}
941dfb07 498
2ba3d76a
DP
499static struct notifier_block gluebi_notifier = {
500 .notifier_call = gluebi_notify,
501};
502
503static int __init ubi_gluebi_init(void)
504{
505 return ubi_register_volume_notifier(&gluebi_notifier, 0);
941dfb07 506}
2ba3d76a
DP
507
508static void __exit ubi_gluebi_exit(void)
509{
510 struct gluebi_device *gluebi, *g;
511
512 list_for_each_entry_safe(gluebi, g, &gluebi_devices, list) {
513 int err;
514 struct mtd_info *mtd = &gluebi->mtd;
515
ee0e87b1 516 err = mtd_device_unregister(mtd);
2ba3d76a 517 if (err)
049333ce
AB
518 err_msg("error %d while removing gluebi MTD device %d, UBI device %d, volume %d - ignoring",
519 err, mtd->index, gluebi->ubi_num,
520 gluebi->vol_id);
2ba3d76a
DP
521 kfree(mtd->name);
522 kfree(gluebi);
523 }
524 ubi_unregister_volume_notifier(&gluebi_notifier);
525}
526
527module_init(ubi_gluebi_init);
528module_exit(ubi_gluebi_exit);
529MODULE_DESCRIPTION("MTD emulation layer over UBI volumes");
530MODULE_AUTHOR("Artem Bityutskiy, Joern Engel");
531MODULE_LICENSE("GPL");