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