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