50e9e2fd86e39c79c9b40bc47bfcdd41505c1baf
[openwrt/staging/luka.git] / target / linux / generic / patches-3.14 / 040-UBI-R-O-block-driver-on-top-of-UBI-volumes.patch
1 From 9d54c8a33eec78289b1b3f6e10874719c27ce0a7 Mon Sep 17 00:00:00 2001
2 From: Ezequiel Garcia <ezequiel.garcia@free-electrons.com>
3 Date: Tue, 25 Feb 2014 13:25:22 -0300
4 Subject: [PATCH] UBI: R/O block driver on top of UBI volumes
5
6 This commit introduces read-only block device emulation on top of UBI volumes.
7
8 Given UBI takes care of wear leveling and bad block management it's possible
9 to add a thin layer to enable block device access to UBI volumes.
10 This allows to use a block-oriented filesystem on a flash device.
11
12 The UBI block devices are meant to be used in conjunction with any
13 regular, block-oriented file system (e.g. ext4), although it's primarily
14 targeted at read-only file systems, such as squashfs.
15
16 Block devices are created upon user request through new ioctls:
17 UBI_IOCVOLATTBLK to attach and UBI_IOCVOLDETBLK to detach.
18 Also, a new UBI module parameter is added 'ubi.block'. This parameter is
19 needed in order to attach a block device on boot-up time, allowing to
20 mount the rootfs on a ubiblock device.
21 For instance, you could have these kernel parameters:
22
23 ubi.mtd=5 ubi.block=0,0 root=/dev/ubiblock0_0
24
25 Or, if you compile ubi as a module:
26
27 $ modprobe ubi mtd=/dev/mtd5 block=/dev/ubi0_0
28
29 Artem: amend commentaries and massage the patch a little bit.
30
31 Signed-off-by: Ezequiel Garcia <ezequiel.garcia@free-electrons.com>
32 Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
33 ---
34 drivers/mtd/ubi/Kconfig | 15 +
35 drivers/mtd/ubi/Makefile | 1 +
36 drivers/mtd/ubi/block.c | 646 ++++++++++++++++++++++++++++++++++++++++++++
37 drivers/mtd/ubi/build.c | 11 +
38 drivers/mtd/ubi/cdev.c | 20 ++
39 drivers/mtd/ubi/ubi.h | 14 +
40 include/uapi/mtd/ubi-user.h | 11 +
41 7 files changed, 718 insertions(+)
42 create mode 100644 drivers/mtd/ubi/block.c
43
44 diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig
45 index 36663af..783fb18 100644
46 --- a/drivers/mtd/ubi/Kconfig
47 +++ b/drivers/mtd/ubi/Kconfig
48 @@ -87,4 +87,19 @@ config MTD_UBI_GLUEBI
49 work on top of UBI. Do not enable this unless you use legacy
50 software.
51
52 +config MTD_UBI_BLOCK
53 + bool "Read-only block devices on top of UBI volumes"
54 + default n
55 + help
56 + This option enables read-only UBI block devices support. UBI block
57 + devices will be layered on top of UBI volumes, which means that the
58 + UBI driver will transparently handle things like bad eraseblocks and
59 + bit-flips. You can put any block-oriented file system on top of UBI
60 + volumes in read-only mode (e.g., ext4), but it is probably most
61 + practical for read-only file systems, like squashfs.
62 +
63 + When selected, this feature will be built in the UBI driver.
64 +
65 + If in doubt, say "N".
66 +
67 endif # MTD_UBI
68 diff --git a/drivers/mtd/ubi/Makefile b/drivers/mtd/ubi/Makefile
69 index b46b0c97..4e3c3d7 100644
70 --- a/drivers/mtd/ubi/Makefile
71 +++ b/drivers/mtd/ubi/Makefile
72 @@ -3,5 +3,6 @@ obj-$(CONFIG_MTD_UBI) += ubi.o
73 ubi-y += vtbl.o vmt.o upd.o build.o cdev.o kapi.o eba.o io.o wl.o attach.o
74 ubi-y += misc.o debug.o
75 ubi-$(CONFIG_MTD_UBI_FASTMAP) += fastmap.o
76 +ubi-$(CONFIG_MTD_UBI_BLOCK) += block.o
77
78 obj-$(CONFIG_MTD_UBI_GLUEBI) += gluebi.o
79 diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c
80 new file mode 100644
81 index 0000000..cea7d1c
82 --- /dev/null
83 +++ b/drivers/mtd/ubi/block.c
84 @@ -0,0 +1,646 @@
85 +/*
86 + * Copyright (c) 2014 Ezequiel Garcia
87 + * Copyright (c) 2011 Free Electrons
88 + *
89 + * Driver parameter handling strongly based on drivers/mtd/ubi/build.c
90 + * Copyright (c) International Business Machines Corp., 2006
91 + * Copyright (c) Nokia Corporation, 2007
92 + * Authors: Artem Bityutskiy, Frank Haverkamp
93 + *
94 + * This program is free software; you can redistribute it and/or modify
95 + * it under the terms of the GNU General Public License as published by
96 + * the Free Software Foundation, version 2.
97 + *
98 + * This program is distributed in the hope that it will be useful,
99 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
100 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
101 + * the GNU General Public License for more details.
102 + */
103 +
104 +/*
105 + * Read-only block devices on top of UBI volumes
106 + *
107 + * A simple implementation to allow a block device to be layered on top of a
108 + * UBI volume. The implementation is provided by creating a static 1-to-1
109 + * mapping between the block device and the UBI volume.
110 + *
111 + * The addressed byte is obtained from the addressed block sector, which is
112 + * mapped linearly into the corresponding LEB:
113 + *
114 + * LEB number = addressed byte / LEB size
115 + *
116 + * This feature is compiled in the UBI core, and adds a new 'block' parameter
117 + * to allow early block device attaching. Runtime block attach/detach for UBI
118 + * volumes is provided through two new UBI ioctls: UBI_IOCVOLATTBLK and
119 + * UBI_IOCVOLDETBLK.
120 + */
121 +
122 +#include <linux/module.h>
123 +#include <linux/init.h>
124 +#include <linux/err.h>
125 +#include <linux/kernel.h>
126 +#include <linux/list.h>
127 +#include <linux/mutex.h>
128 +#include <linux/slab.h>
129 +#include <linux/vmalloc.h>
130 +#include <linux/mtd/ubi.h>
131 +#include <linux/workqueue.h>
132 +#include <linux/blkdev.h>
133 +#include <linux/hdreg.h>
134 +#include <asm/div64.h>
135 +
136 +#include "ubi-media.h"
137 +#include "ubi.h"
138 +
139 +/* Maximum number of supported devices */
140 +#define UBIBLOCK_MAX_DEVICES 32
141 +
142 +/* Maximum length of the 'block=' parameter */
143 +#define UBIBLOCK_PARAM_LEN 63
144 +
145 +/* Maximum number of comma-separated items in the 'block=' parameter */
146 +#define UBIBLOCK_PARAM_COUNT 2
147 +
148 +struct ubiblock_param {
149 + int ubi_num;
150 + int vol_id;
151 + char name[UBIBLOCK_PARAM_LEN+1];
152 +};
153 +
154 +/* Numbers of elements set in the @ubiblock_param array */
155 +static int ubiblock_devs __initdata;
156 +
157 +/* MTD devices specification parameters */
158 +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata;
159 +
160 +struct ubiblock {
161 + struct ubi_volume_desc *desc;
162 + int ubi_num;
163 + int vol_id;
164 + int refcnt;
165 + int leb_size;
166 +
167 + struct gendisk *gd;
168 + struct request_queue *rq;
169 +
170 + struct workqueue_struct *wq;
171 + struct work_struct work;
172 +
173 + struct mutex dev_mutex;
174 + spinlock_t queue_lock;
175 + struct list_head list;
176 +};
177 +
178 +/* Linked list of all ubiblock instances */
179 +static LIST_HEAD(ubiblock_devices);
180 +static DEFINE_MUTEX(devices_mutex);
181 +static int ubiblock_major;
182 +
183 +static int __init ubiblock_set_param(const char *val,
184 + const struct kernel_param *kp)
185 +{
186 + int i, ret;
187 + size_t len;
188 + struct ubiblock_param *param;
189 + char buf[UBIBLOCK_PARAM_LEN];
190 + char *pbuf = &buf[0];
191 + char *tokens[UBIBLOCK_PARAM_COUNT];
192 +
193 + if (!val)
194 + return -EINVAL;
195 +
196 + len = strnlen(val, UBIBLOCK_PARAM_LEN);
197 + if (len == 0) {
198 + ubi_warn("block: empty 'block=' parameter - ignored\n");
199 + return 0;
200 + }
201 +
202 + if (len == UBIBLOCK_PARAM_LEN) {
203 + ubi_err("block: parameter \"%s\" is too long, max. is %d\n",
204 + val, UBIBLOCK_PARAM_LEN);
205 + return -EINVAL;
206 + }
207 +
208 + strcpy(buf, val);
209 +
210 + /* Get rid of the final newline */
211 + if (buf[len - 1] == '\n')
212 + buf[len - 1] = '\0';
213 +
214 + for (i = 0; i < UBIBLOCK_PARAM_COUNT; i++)
215 + tokens[i] = strsep(&pbuf, ",");
216 +
217 + param = &ubiblock_param[ubiblock_devs];
218 + if (tokens[1]) {
219 + /* Two parameters: can be 'ubi, vol_id' or 'ubi, vol_name' */
220 + ret = kstrtoint(tokens[0], 10, &param->ubi_num);
221 + if (ret < 0)
222 + return -EINVAL;
223 +
224 + /* Second param can be a number or a name */
225 + ret = kstrtoint(tokens[1], 10, &param->vol_id);
226 + if (ret < 0) {
227 + param->vol_id = -1;
228 + strcpy(param->name, tokens[1]);
229 + }
230 +
231 + } else {
232 + /* One parameter: must be device path */
233 + strcpy(param->name, tokens[0]);
234 + param->ubi_num = -1;
235 + param->vol_id = -1;
236 + }
237 +
238 + ubiblock_devs++;
239 +
240 + return 0;
241 +}
242 +
243 +static const struct kernel_param_ops ubiblock_param_ops = {
244 + .set = ubiblock_set_param,
245 +};
246 +module_param_cb(block, &ubiblock_param_ops, NULL, 0);
247 +MODULE_PARM_DESC(block, "Attach block devices to UBI volumes. Parameter format: block=<path|dev,num|dev,name>.\n"
248 + "Multiple \"block\" parameters may be specified.\n"
249 + "UBI volumes may be specified by their number, name, or path to the device node.\n"
250 + "Examples\n"
251 + "Using the UBI volume path:\n"
252 + "ubi.block=/dev/ubi0_0\n"
253 + "Using the UBI device, and the volume name:\n"
254 + "ubi.block=0,rootfs\n"
255 + "Using both UBI device number and UBI volume number:\n"
256 + "ubi.block=0,0\n");
257 +
258 +static struct ubiblock *find_dev_nolock(int ubi_num, int vol_id)
259 +{
260 + struct ubiblock *dev;
261 +
262 + list_for_each_entry(dev, &ubiblock_devices, list)
263 + if (dev->ubi_num == ubi_num && dev->vol_id == vol_id)
264 + return dev;
265 + return NULL;
266 +}
267 +
268 +static int ubiblock_read_to_buf(struct ubiblock *dev, char *buffer,
269 + int leb, int offset, int len)
270 +{
271 + int ret;
272 +
273 + ret = ubi_read(dev->desc, leb, buffer, offset, len);
274 + if (ret) {
275 + ubi_err("%s ubi_read error %d",
276 + dev->gd->disk_name, ret);
277 + return ret;
278 + }
279 + return 0;
280 +}
281 +
282 +static int ubiblock_read(struct ubiblock *dev, char *buffer,
283 + sector_t sec, int len)
284 +{
285 + int ret, leb, offset;
286 + int bytes_left = len;
287 + int to_read = len;
288 + loff_t pos = sec << 9;
289 +
290 + /* Get LEB:offset address to read from */
291 + offset = do_div(pos, dev->leb_size);
292 + leb = pos;
293 +
294 + while (bytes_left) {
295 + /*
296 + * We can only read one LEB at a time. Therefore if the read
297 + * length is larger than one LEB size, we split the operation.
298 + */
299 + if (offset + to_read > dev->leb_size)
300 + to_read = dev->leb_size - offset;
301 +
302 + ret = ubiblock_read_to_buf(dev, buffer, leb, offset, to_read);
303 + if (ret)
304 + return ret;
305 +
306 + buffer += to_read;
307 + bytes_left -= to_read;
308 + to_read = bytes_left;
309 + leb += 1;
310 + offset = 0;
311 + }
312 + return 0;
313 +}
314 +
315 +static int do_ubiblock_request(struct ubiblock *dev, struct request *req)
316 +{
317 + int len, ret;
318 + sector_t sec;
319 +
320 + if (req->cmd_type != REQ_TYPE_FS)
321 + return -EIO;
322 +
323 + if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
324 + get_capacity(req->rq_disk))
325 + return -EIO;
326 +
327 + if (rq_data_dir(req) != READ)
328 + return -ENOSYS; /* Write not implemented */
329 +
330 + sec = blk_rq_pos(req);
331 + len = blk_rq_cur_bytes(req);
332 +
333 + /*
334 + * Let's prevent the device from being removed while we're doing I/O
335 + * work. Notice that this means we serialize all the I/O operations,
336 + * but it's probably of no impact given the NAND core serializes
337 + * flash access anyway.
338 + */
339 + mutex_lock(&dev->dev_mutex);
340 + ret = ubiblock_read(dev, req->buffer, sec, len);
341 + mutex_unlock(&dev->dev_mutex);
342 +
343 + return ret;
344 +}
345 +
346 +static void ubiblock_do_work(struct work_struct *work)
347 +{
348 + struct ubiblock *dev =
349 + container_of(work, struct ubiblock, work);
350 + struct request_queue *rq = dev->rq;
351 + struct request *req;
352 + int res;
353 +
354 + spin_lock_irq(rq->queue_lock);
355 +
356 + req = blk_fetch_request(rq);
357 + while (req) {
358 +
359 + spin_unlock_irq(rq->queue_lock);
360 + res = do_ubiblock_request(dev, req);
361 + spin_lock_irq(rq->queue_lock);
362 +
363 + /*
364 + * If we're done with this request,
365 + * we need to fetch a new one
366 + */
367 + if (!__blk_end_request_cur(req, res))
368 + req = blk_fetch_request(rq);
369 + }
370 +
371 + spin_unlock_irq(rq->queue_lock);
372 +}
373 +
374 +static void ubiblock_request(struct request_queue *rq)
375 +{
376 + struct ubiblock *dev;
377 + struct request *req;
378 +
379 + dev = rq->queuedata;
380 +
381 + if (!dev)
382 + while ((req = blk_fetch_request(rq)) != NULL)
383 + __blk_end_request_all(req, -ENODEV);
384 + else
385 + queue_work(dev->wq, &dev->work);
386 +}
387 +
388 +static int ubiblock_open(struct block_device *bdev, fmode_t mode)
389 +{
390 + struct ubiblock *dev = bdev->bd_disk->private_data;
391 + int ret;
392 +
393 + mutex_lock(&dev->dev_mutex);
394 + if (dev->refcnt > 0) {
395 + /*
396 + * The volume is already open, just increase the reference
397 + * counter.
398 + */
399 + goto out_done;
400 + }
401 +
402 + /*
403 + * We want users to be aware they should only mount us as read-only.
404 + * It's just a paranoid check, as write requests will get rejected
405 + * in any case.
406 + */
407 + if (mode & FMODE_WRITE) {
408 + ret = -EPERM;
409 + goto out_unlock;
410 + }
411 +
412 + dev->desc = ubi_open_volume(dev->ubi_num, dev->vol_id, UBI_READONLY);
413 + if (IS_ERR(dev->desc)) {
414 + ubi_err("%s failed to open ubi volume %d_%d",
415 + dev->gd->disk_name, dev->ubi_num, dev->vol_id);
416 + ret = PTR_ERR(dev->desc);
417 + dev->desc = NULL;
418 + goto out_unlock;
419 + }
420 +
421 +out_done:
422 + dev->refcnt++;
423 + mutex_unlock(&dev->dev_mutex);
424 + return 0;
425 +
426 +out_unlock:
427 + mutex_unlock(&dev->dev_mutex);
428 + return ret;
429 +}
430 +
431 +static void ubiblock_release(struct gendisk *gd, fmode_t mode)
432 +{
433 + struct ubiblock *dev = gd->private_data;
434 +
435 + mutex_lock(&dev->dev_mutex);
436 + dev->refcnt--;
437 + if (dev->refcnt == 0) {
438 + ubi_close_volume(dev->desc);
439 + dev->desc = NULL;
440 + }
441 + mutex_unlock(&dev->dev_mutex);
442 +}
443 +
444 +static int ubiblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
445 +{
446 + /* Some tools might require this information */
447 + geo->heads = 1;
448 + geo->cylinders = 1;
449 + geo->sectors = get_capacity(bdev->bd_disk);
450 + geo->start = 0;
451 + return 0;
452 +}
453 +
454 +static const struct block_device_operations ubiblock_ops = {
455 + .owner = THIS_MODULE,
456 + .open = ubiblock_open,
457 + .release = ubiblock_release,
458 + .getgeo = ubiblock_getgeo,
459 +};
460 +
461 +int ubiblock_add(struct ubi_volume_info *vi)
462 +{
463 + struct ubiblock *dev;
464 + struct gendisk *gd;
465 + int disk_capacity;
466 + int ret;
467 +
468 + /* Check that the volume isn't already handled */
469 + mutex_lock(&devices_mutex);
470 + if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
471 + mutex_unlock(&devices_mutex);
472 + return -EEXIST;
473 + }
474 + mutex_unlock(&devices_mutex);
475 +
476 + dev = kzalloc(sizeof(struct ubiblock), GFP_KERNEL);
477 + if (!dev)
478 + return -ENOMEM;
479 +
480 + mutex_init(&dev->dev_mutex);
481 +
482 + dev->ubi_num = vi->ubi_num;
483 + dev->vol_id = vi->vol_id;
484 + dev->leb_size = vi->usable_leb_size;
485 +
486 + /* Initialize the gendisk of this ubiblock device */
487 + gd = alloc_disk(1);
488 + if (!gd) {
489 + ubi_err("block: alloc_disk failed");
490 + ret = -ENODEV;
491 + goto out_free_dev;
492 + }
493 +
494 + gd->fops = &ubiblock_ops;
495 + gd->major = ubiblock_major;
496 + gd->first_minor = dev->ubi_num * UBI_MAX_VOLUMES + dev->vol_id;
497 + gd->private_data = dev;
498 + sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
499 + disk_capacity = (vi->size * vi->usable_leb_size) >> 9;
500 + set_capacity(gd, disk_capacity);
501 + dev->gd = gd;
502 +
503 + spin_lock_init(&dev->queue_lock);
504 + dev->rq = blk_init_queue(ubiblock_request, &dev->queue_lock);
505 + if (!dev->rq) {
506 + ubi_err("block: blk_init_queue failed");
507 + ret = -ENODEV;
508 + goto out_put_disk;
509 + }
510 +
511 + dev->rq->queuedata = dev;
512 + dev->gd->queue = dev->rq;
513 +
514 + /*
515 + * Create one workqueue per volume (per registered block device).
516 + * Rembember workqueues are cheap, they're not threads.
517 + */
518 + dev->wq = alloc_workqueue(gd->disk_name, 0, 0);
519 + if (!dev->wq)
520 + goto out_free_queue;
521 + INIT_WORK(&dev->work, ubiblock_do_work);
522 +
523 + mutex_lock(&devices_mutex);
524 + list_add_tail(&dev->list, &ubiblock_devices);
525 + mutex_unlock(&devices_mutex);
526 +
527 + /* Must be the last step: anyone can call file ops from now on */
528 + add_disk(dev->gd);
529 + ubi_msg("%s created from ubi%d:%d(%s)",
530 + dev->gd->disk_name, dev->ubi_num, dev->vol_id, vi->name);
531 + return 0;
532 +
533 +out_free_queue:
534 + blk_cleanup_queue(dev->rq);
535 +out_put_disk:
536 + put_disk(dev->gd);
537 +out_free_dev:
538 + kfree(dev);
539 +
540 + return ret;
541 +}
542 +
543 +static void ubiblock_cleanup(struct ubiblock *dev)
544 +{
545 + del_gendisk(dev->gd);
546 + blk_cleanup_queue(dev->rq);
547 + ubi_msg("%s released", dev->gd->disk_name);
548 + put_disk(dev->gd);
549 +}
550 +
551 +int ubiblock_del(struct ubi_volume_info *vi)
552 +{
553 + struct ubiblock *dev;
554 +
555 + mutex_lock(&devices_mutex);
556 + dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
557 + if (!dev) {
558 + mutex_unlock(&devices_mutex);
559 + return -ENODEV;
560 + }
561 +
562 + /* Found a device, let's lock it so we can check if it's busy */
563 + mutex_lock(&dev->dev_mutex);
564 + if (dev->refcnt > 0) {
565 + mutex_unlock(&dev->dev_mutex);
566 + mutex_unlock(&devices_mutex);
567 + return -EBUSY;
568 + }
569 +
570 + /* Remove from device list */
571 + list_del(&dev->list);
572 + mutex_unlock(&devices_mutex);
573 +
574 + /* Flush pending work and stop this workqueue */
575 + destroy_workqueue(dev->wq);
576 +
577 + ubiblock_cleanup(dev);
578 + mutex_unlock(&dev->dev_mutex);
579 + kfree(dev);
580 + return 0;
581 +}
582 +
583 +static void ubiblock_resize(struct ubi_volume_info *vi)
584 +{
585 + struct ubiblock *dev;
586 + int disk_capacity;
587 +
588 + /*
589 + * Need to lock the device list until we stop using the device,
590 + * otherwise the device struct might get released in 'ubiblock_del()'.
591 + */
592 + mutex_lock(&devices_mutex);
593 + dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
594 + if (!dev) {
595 + mutex_unlock(&devices_mutex);
596 + return;
597 + }
598 +
599 + mutex_lock(&dev->dev_mutex);
600 + disk_capacity = (vi->size * vi->usable_leb_size) >> 9;
601 + set_capacity(dev->gd, disk_capacity);
602 + ubi_msg("%s resized to %d LEBs", dev->gd->disk_name, vi->size);
603 + mutex_unlock(&dev->dev_mutex);
604 + mutex_unlock(&devices_mutex);
605 +}
606 +
607 +static int ubiblock_notify(struct notifier_block *nb,
608 + unsigned long notification_type, void *ns_ptr)
609 +{
610 + struct ubi_notification *nt = ns_ptr;
611 +
612 + switch (notification_type) {
613 + case UBI_VOLUME_ADDED:
614 + /*
615 + * We want to enforce explicit block device attaching for
616 + * volumes, so when a volume is added we do nothing.
617 + */
618 + break;
619 + case UBI_VOLUME_REMOVED:
620 + ubiblock_del(&nt->vi);
621 + break;
622 + case UBI_VOLUME_RESIZED:
623 + ubiblock_resize(&nt->vi);
624 + break;
625 + default:
626 + break;
627 + }
628 + return NOTIFY_OK;
629 +}
630 +
631 +static struct notifier_block ubiblock_notifier = {
632 + .notifier_call = ubiblock_notify,
633 +};
634 +
635 +static struct ubi_volume_desc * __init
636 +open_volume_desc(const char *name, int ubi_num, int vol_id)
637 +{
638 + if (ubi_num == -1)
639 + /* No ubi num, name must be a vol device path */
640 + return ubi_open_volume_path(name, UBI_READONLY);
641 + else if (vol_id == -1)
642 + /* No vol_id, must be vol_name */
643 + return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
644 + else
645 + return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
646 +}
647 +
648 +static int __init ubiblock_attach_from_param(void)
649 +{
650 + int i, ret;
651 + struct ubiblock_param *p;
652 + struct ubi_volume_desc *desc;
653 + struct ubi_volume_info vi;
654 +
655 + for (i = 0; i < ubiblock_devs; i++) {
656 + p = &ubiblock_param[i];
657 +
658 + desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
659 + if (IS_ERR(desc)) {
660 + ubi_err("block: can't open volume, err=%ld\n",
661 + PTR_ERR(desc));
662 + ret = PTR_ERR(desc);
663 + break;
664 + }
665 +
666 + ubi_get_volume_info(desc, &vi);
667 + ubi_close_volume(desc);
668 +
669 + ret = ubiblock_add(&vi);
670 + if (ret) {
671 + ubi_err("block: can't add '%s' volume, err=%d\n",
672 + vi.name, ret);
673 + break;
674 + }
675 + }
676 + return ret;
677 +}
678 +
679 +static void ubiblock_detach_all(void)
680 +{
681 + struct ubiblock *next;
682 + struct ubiblock *dev;
683 +
684 + list_for_each_entry_safe(dev, next, &ubiblock_devices, list) {
685 + /* Flush pending work and stop workqueue */
686 + destroy_workqueue(dev->wq);
687 + /* The module is being forcefully removed */
688 + WARN_ON(dev->desc);
689 + /* Remove from device list */
690 + list_del(&dev->list);
691 + ubiblock_cleanup(dev);
692 + kfree(dev);
693 + }
694 +}
695 +
696 +int __init ubiblock_init(void)
697 +{
698 + int ret;
699 +
700 + ubiblock_major = register_blkdev(0, "ubiblock");
701 + if (ubiblock_major < 0)
702 + return ubiblock_major;
703 +
704 + /* Attach block devices from 'block=' module param */
705 + ret = ubiblock_attach_from_param();
706 + if (ret)
707 + goto err_detach;
708 +
709 + /*
710 + * Block devices needs to be attached to volumes explicitly
711 + * upon user request. So we ignore existing volumes.
712 + */
713 + ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
714 + if (ret)
715 + goto err_unreg;
716 + return 0;
717 +
718 +err_unreg:
719 + unregister_blkdev(ubiblock_major, "ubiblock");
720 +err_detach:
721 + ubiblock_detach_all();
722 + return ret;
723 +}
724 +
725 +void __exit ubiblock_exit(void)
726 +{
727 + ubi_unregister_volume_notifier(&ubiblock_notifier);
728 + ubiblock_detach_all();
729 + unregister_blkdev(ubiblock_major, "ubiblock");
730 +}
731 diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
732 index 57deae9..6e30a3c 100644
733 --- a/drivers/mtd/ubi/build.c
734 +++ b/drivers/mtd/ubi/build.c
735 @@ -1298,6 +1298,15 @@ static int __init ubi_init(void)
736 }
737 }
738
739 + err = ubiblock_init();
740 + if (err) {
741 + ubi_err("block: cannot initialize, error %d", err);
742 +
743 + /* See comment above re-ubi_is_module(). */
744 + if (ubi_is_module())
745 + goto out_detach;
746 + }
747 +
748 return 0;
749
750 out_detach:
751 @@ -1326,6 +1335,8 @@ static void __exit ubi_exit(void)
752 {
753 int i;
754
755 + ubiblock_exit();
756 +
757 for (i = 0; i < UBI_MAX_DEVICES; i++)
758 if (ubi_devices[i]) {
759 mutex_lock(&ubi_devices_mutex);
760 diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c
761 index 8ca49f2..39d3774 100644
762 --- a/drivers/mtd/ubi/cdev.c
763 +++ b/drivers/mtd/ubi/cdev.c
764 @@ -561,6 +561,26 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
765 break;
766 }
767
768 + /* Attach a block device to an UBI volume */
769 + case UBI_IOCVOLATTBLK:
770 + {
771 + struct ubi_volume_info vi;
772 +
773 + ubi_get_volume_info(desc, &vi);
774 + err = ubiblock_add(&vi);
775 + break;
776 + }
777 +
778 + /* Dettach a block device from an UBI volume */
779 + case UBI_IOCVOLDETBLK:
780 + {
781 + struct ubi_volume_info vi;
782 +
783 + ubi_get_volume_info(desc, &vi);
784 + err = ubiblock_del(&vi);
785 + break;
786 + }
787 +
788 default:
789 err = -ENOTTY;
790 break;
791 diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
792 index 8ea6297..e76ff98 100644
793 --- a/drivers/mtd/ubi/ubi.h
794 +++ b/drivers/mtd/ubi/ubi.h
795 @@ -864,6 +864,20 @@ int ubi_update_fastmap(struct ubi_device *ubi);
796 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
797 int fm_anchor);
798
799 +/* block.c */
800 +#ifdef CONFIG_MTD_UBI_BLOCK
801 +int ubiblock_init(void);
802 +void ubiblock_exit(void);
803 +int ubiblock_add(struct ubi_volume_info *vi);
804 +int ubiblock_del(struct ubi_volume_info *vi);
805 +#else
806 +static inline int ubiblock_init(void) { return 0; }
807 +static inline void ubiblock_exit(void) {}
808 +static inline int ubiblock_add(struct ubi_volume_info *vi) { return -ENOTTY; }
809 +static inline int ubiblock_del(struct ubi_volume_info *vi) { return -ENOTTY; }
810 +#endif
811 +
812 +
813 /*
814 * ubi_rb_for_each_entry - walk an RB-tree.
815 * @rb: a pointer to type 'struct rb_node' to use as a loop counter
816 diff --git a/include/uapi/mtd/ubi-user.h b/include/uapi/mtd/ubi-user.h
817 index 723c324..b98585a 100644
818 --- a/include/uapi/mtd/ubi-user.h
819 +++ b/include/uapi/mtd/ubi-user.h
820 @@ -134,6 +134,13 @@
821 * used. A pointer to a &struct ubi_set_vol_prop_req object is expected to be
822 * passed. The object describes which property should be set, and to which value
823 * it should be set.
824 + *
825 + * Block devices on UBI volumes
826 + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
827 + *
828 + * To attach or detach a block device from an UBI volume the %UBI_IOCVOLATTBLK
829 + * and %UBI_IOCVOLDETBLK ioctl commands should be used, respectively.
830 + * These commands take no arguments.
831 */
832
833 /*
834 @@ -191,6 +198,10 @@
835 /* Set an UBI volume property */
836 #define UBI_IOCSETVOLPROP _IOW(UBI_VOL_IOC_MAGIC, 6, \
837 struct ubi_set_vol_prop_req)
838 +/* Attach a block device to an UBI volume */
839 +#define UBI_IOCVOLATTBLK _IO(UBI_VOL_IOC_MAGIC, 7)
840 +/* Detach a block device from an UBI volume */
841 +#define UBI_IOCVOLDETBLK _IO(UBI_VOL_IOC_MAGIC, 8)
842
843 /* Maximum MTD device name length supported by UBI */
844 #define MAX_UBI_MTD_NAME_LEN 127
845 --
846 1.9.2
847