block.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Copyright (c) 2014 Ezequiel Garcia
  3. * Copyright (c) 2011 Free Electrons
  4. *
  5. * Driver parameter handling strongly based on drivers/mtd/ubi/build.c
  6. * Copyright (c) International Business Machines Corp., 2006
  7. * Copyright (c) Nokia Corporation, 2007
  8. * Authors: Artem Bityutskiy, Frank Haverkamp
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, version 2.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU General Public License for more details.
  18. */
  19. /*
  20. * Read-only block devices on top of UBI volumes
  21. *
  22. * A simple implementation to allow a block device to be layered on top of a
  23. * UBI volume. The implementation is provided by creating a static 1-to-1
  24. * mapping between the block device and the UBI volume.
  25. *
  26. * The addressed byte is obtained from the addressed block sector, which is
  27. * mapped linearly into the corresponding LEB:
  28. *
  29. * LEB number = addressed byte / LEB size
  30. *
  31. * This feature is compiled in the UBI core, and adds a 'block' parameter
  32. * to allow early creation of block devices on top of UBI volumes. Runtime
  33. * block creation/removal for UBI volumes is provided through two UBI ioctls:
  34. * UBI_IOCVOLCRBLK and UBI_IOCVOLRMBLK.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/err.h>
  39. #include <linux/kernel.h>
  40. #include <linux/list.h>
  41. #include <linux/mutex.h>
  42. #include <linux/slab.h>
  43. #include <linux/mtd/ubi.h>
  44. #include <linux/workqueue.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/blk-mq.h>
  47. #include <linux/hdreg.h>
  48. #include <linux/scatterlist.h>
  49. #include <linux/idr.h>
  50. #include <asm/div64.h>
  51. #include "ubi-media.h"
  52. #include "ubi.h"
  53. /* Maximum number of supported devices */
  54. #define UBIBLOCK_MAX_DEVICES 32
  55. /* Maximum length of the 'block=' parameter */
  56. #define UBIBLOCK_PARAM_LEN 63
  57. /* Maximum number of comma-separated items in the 'block=' parameter */
  58. #define UBIBLOCK_PARAM_COUNT 2
  59. struct ubiblock_param {
  60. int ubi_num;
  61. int vol_id;
  62. char name[UBIBLOCK_PARAM_LEN+1];
  63. };
  64. struct ubiblock_pdu {
  65. struct work_struct work;
  66. struct ubi_sgl usgl;
  67. };
  68. /* Numbers of elements set in the @ubiblock_param array */
  69. static int ubiblock_devs __initdata;
  70. /* MTD devices specification parameters */
  71. static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata;
  72. struct ubiblock {
  73. struct ubi_volume_desc *desc;
  74. int ubi_num;
  75. int vol_id;
  76. int refcnt;
  77. int leb_size;
  78. struct gendisk *gd;
  79. struct request_queue *rq;
  80. struct workqueue_struct *wq;
  81. struct mutex dev_mutex;
  82. struct list_head list;
  83. struct blk_mq_tag_set tag_set;
  84. };
  85. /* Linked list of all ubiblock instances */
  86. static LIST_HEAD(ubiblock_devices);
  87. static DEFINE_MUTEX(devices_mutex);
  88. static int ubiblock_major;
  89. static int __init ubiblock_set_param(const char *val,
  90. const struct kernel_param *kp)
  91. {
  92. int i, ret;
  93. size_t len;
  94. struct ubiblock_param *param;
  95. char buf[UBIBLOCK_PARAM_LEN];
  96. char *pbuf = &buf[0];
  97. char *tokens[UBIBLOCK_PARAM_COUNT];
  98. if (!val)
  99. return -EINVAL;
  100. len = strnlen(val, UBIBLOCK_PARAM_LEN);
  101. if (len == 0) {
  102. pr_warn("UBI: block: empty 'block=' parameter - ignored\n");
  103. return 0;
  104. }
  105. if (len == UBIBLOCK_PARAM_LEN) {
  106. pr_err("UBI: block: parameter \"%s\" is too long, max. is %d\n",
  107. val, UBIBLOCK_PARAM_LEN);
  108. return -EINVAL;
  109. }
  110. strcpy(buf, val);
  111. /* Get rid of the final newline */
  112. if (buf[len - 1] == '\n')
  113. buf[len - 1] = '\0';
  114. for (i = 0; i < UBIBLOCK_PARAM_COUNT; i++)
  115. tokens[i] = strsep(&pbuf, ",");
  116. param = &ubiblock_param[ubiblock_devs];
  117. if (tokens[1]) {
  118. /* Two parameters: can be 'ubi, vol_id' or 'ubi, vol_name' */
  119. ret = kstrtoint(tokens[0], 10, &param->ubi_num);
  120. if (ret < 0)
  121. return -EINVAL;
  122. /* Second param can be a number or a name */
  123. ret = kstrtoint(tokens[1], 10, &param->vol_id);
  124. if (ret < 0) {
  125. param->vol_id = -1;
  126. strcpy(param->name, tokens[1]);
  127. }
  128. } else {
  129. /* One parameter: must be device path */
  130. strcpy(param->name, tokens[0]);
  131. param->ubi_num = -1;
  132. param->vol_id = -1;
  133. }
  134. ubiblock_devs++;
  135. return 0;
  136. }
  137. static const struct kernel_param_ops ubiblock_param_ops = {
  138. .set = ubiblock_set_param,
  139. };
  140. module_param_cb(block, &ubiblock_param_ops, NULL, 0);
  141. MODULE_PARM_DESC(block, "Attach block devices to UBI volumes. Parameter format: block=<path|dev,num|dev,name>.\n"
  142. "Multiple \"block\" parameters may be specified.\n"
  143. "UBI volumes may be specified by their number, name, or path to the device node.\n"
  144. "Examples\n"
  145. "Using the UBI volume path:\n"
  146. "ubi.block=/dev/ubi0_0\n"
  147. "Using the UBI device, and the volume name:\n"
  148. "ubi.block=0,rootfs\n"
  149. "Using both UBI device number and UBI volume number:\n"
  150. "ubi.block=0,0\n");
  151. static struct ubiblock *find_dev_nolock(int ubi_num, int vol_id)
  152. {
  153. struct ubiblock *dev;
  154. list_for_each_entry(dev, &ubiblock_devices, list)
  155. if (dev->ubi_num == ubi_num && dev->vol_id == vol_id)
  156. return dev;
  157. return NULL;
  158. }
  159. static int ubiblock_read(struct ubiblock_pdu *pdu)
  160. {
  161. int ret, leb, offset, bytes_left, to_read;
  162. u64 pos;
  163. struct request *req = blk_mq_rq_from_pdu(pdu);
  164. struct ubiblock *dev = req->q->queuedata;
  165. to_read = blk_rq_bytes(req);
  166. pos = blk_rq_pos(req) << 9;
  167. /* Get LEB:offset address to read from */
  168. offset = do_div(pos, dev->leb_size);
  169. leb = pos;
  170. bytes_left = to_read;
  171. while (bytes_left) {
  172. /*
  173. * We can only read one LEB at a time. Therefore if the read
  174. * length is larger than one LEB size, we split the operation.
  175. */
  176. if (offset + to_read > dev->leb_size)
  177. to_read = dev->leb_size - offset;
  178. ret = ubi_read_sg(dev->desc, leb, &pdu->usgl, offset, to_read);
  179. if (ret < 0)
  180. return ret;
  181. bytes_left -= to_read;
  182. to_read = bytes_left;
  183. leb += 1;
  184. offset = 0;
  185. }
  186. return 0;
  187. }
  188. static int ubiblock_open(struct block_device *bdev, fmode_t mode)
  189. {
  190. struct ubiblock *dev = bdev->bd_disk->private_data;
  191. int ret;
  192. mutex_lock(&dev->dev_mutex);
  193. if (dev->refcnt > 0) {
  194. /*
  195. * The volume is already open, just increase the reference
  196. * counter.
  197. */
  198. goto out_done;
  199. }
  200. /*
  201. * We want users to be aware they should only mount us as read-only.
  202. * It's just a paranoid check, as write requests will get rejected
  203. * in any case.
  204. */
  205. if (mode & FMODE_WRITE) {
  206. ret = -EPERM;
  207. goto out_unlock;
  208. }
  209. dev->desc = ubi_open_volume(dev->ubi_num, dev->vol_id, UBI_READONLY);
  210. if (IS_ERR(dev->desc)) {
  211. dev_err(disk_to_dev(dev->gd), "failed to open ubi volume %d_%d",
  212. dev->ubi_num, dev->vol_id);
  213. ret = PTR_ERR(dev->desc);
  214. dev->desc = NULL;
  215. goto out_unlock;
  216. }
  217. out_done:
  218. dev->refcnt++;
  219. mutex_unlock(&dev->dev_mutex);
  220. return 0;
  221. out_unlock:
  222. mutex_unlock(&dev->dev_mutex);
  223. return ret;
  224. }
  225. static void ubiblock_release(struct gendisk *gd, fmode_t mode)
  226. {
  227. struct ubiblock *dev = gd->private_data;
  228. mutex_lock(&dev->dev_mutex);
  229. dev->refcnt--;
  230. if (dev->refcnt == 0) {
  231. ubi_close_volume(dev->desc);
  232. dev->desc = NULL;
  233. }
  234. mutex_unlock(&dev->dev_mutex);
  235. }
  236. static int ubiblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  237. {
  238. /* Some tools might require this information */
  239. geo->heads = 1;
  240. geo->cylinders = 1;
  241. geo->sectors = get_capacity(bdev->bd_disk);
  242. geo->start = 0;
  243. return 0;
  244. }
  245. static const struct block_device_operations ubiblock_ops = {
  246. .owner = THIS_MODULE,
  247. .open = ubiblock_open,
  248. .release = ubiblock_release,
  249. .getgeo = ubiblock_getgeo,
  250. };
  251. static void ubiblock_do_work(struct work_struct *work)
  252. {
  253. int ret;
  254. struct ubiblock_pdu *pdu = container_of(work, struct ubiblock_pdu, work);
  255. struct request *req = blk_mq_rq_from_pdu(pdu);
  256. blk_mq_start_request(req);
  257. /*
  258. * It is safe to ignore the return value of blk_rq_map_sg() because
  259. * the number of sg entries is limited to UBI_MAX_SG_COUNT
  260. * and ubi_read_sg() will check that limit.
  261. */
  262. blk_rq_map_sg(req->q, req, pdu->usgl.sg);
  263. ret = ubiblock_read(pdu);
  264. rq_flush_dcache_pages(req);
  265. blk_mq_end_request(req, ret);
  266. }
  267. static int ubiblock_queue_rq(struct blk_mq_hw_ctx *hctx,
  268. const struct blk_mq_queue_data *bd)
  269. {
  270. struct request *req = bd->rq;
  271. struct ubiblock *dev = hctx->queue->queuedata;
  272. struct ubiblock_pdu *pdu = blk_mq_rq_to_pdu(req);
  273. if (req->cmd_type != REQ_TYPE_FS)
  274. return BLK_MQ_RQ_QUEUE_ERROR;
  275. if (rq_data_dir(req) != READ)
  276. return BLK_MQ_RQ_QUEUE_ERROR; /* Write not implemented */
  277. ubi_sgl_init(&pdu->usgl);
  278. queue_work(dev->wq, &pdu->work);
  279. return BLK_MQ_RQ_QUEUE_OK;
  280. }
  281. static int ubiblock_init_request(void *data, struct request *req,
  282. unsigned int hctx_idx,
  283. unsigned int request_idx,
  284. unsigned int numa_node)
  285. {
  286. struct ubiblock_pdu *pdu = blk_mq_rq_to_pdu(req);
  287. sg_init_table(pdu->usgl.sg, UBI_MAX_SG_COUNT);
  288. INIT_WORK(&pdu->work, ubiblock_do_work);
  289. return 0;
  290. }
  291. static struct blk_mq_ops ubiblock_mq_ops = {
  292. .queue_rq = ubiblock_queue_rq,
  293. .init_request = ubiblock_init_request,
  294. };
  295. static DEFINE_IDR(ubiblock_minor_idr);
  296. int ubiblock_create(struct ubi_volume_info *vi)
  297. {
  298. struct ubiblock *dev;
  299. struct gendisk *gd;
  300. u64 disk_capacity = vi->used_bytes >> 9;
  301. int ret;
  302. if ((sector_t)disk_capacity != disk_capacity)
  303. return -EFBIG;
  304. /* Check that the volume isn't already handled */
  305. mutex_lock(&devices_mutex);
  306. if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
  307. mutex_unlock(&devices_mutex);
  308. return -EEXIST;
  309. }
  310. mutex_unlock(&devices_mutex);
  311. dev = kzalloc(sizeof(struct ubiblock), GFP_KERNEL);
  312. if (!dev)
  313. return -ENOMEM;
  314. mutex_init(&dev->dev_mutex);
  315. dev->ubi_num = vi->ubi_num;
  316. dev->vol_id = vi->vol_id;
  317. dev->leb_size = vi->usable_leb_size;
  318. /* Initialize the gendisk of this ubiblock device */
  319. gd = alloc_disk(1);
  320. if (!gd) {
  321. pr_err("UBI: block: alloc_disk failed");
  322. ret = -ENODEV;
  323. goto out_free_dev;
  324. }
  325. gd->fops = &ubiblock_ops;
  326. gd->major = ubiblock_major;
  327. gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
  328. if (gd->first_minor < 0) {
  329. dev_err(disk_to_dev(gd),
  330. "block: dynamic minor allocation failed");
  331. ret = -ENODEV;
  332. goto out_put_disk;
  333. }
  334. gd->private_data = dev;
  335. sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
  336. set_capacity(gd, disk_capacity);
  337. dev->gd = gd;
  338. dev->tag_set.ops = &ubiblock_mq_ops;
  339. dev->tag_set.queue_depth = 64;
  340. dev->tag_set.numa_node = NUMA_NO_NODE;
  341. dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  342. dev->tag_set.cmd_size = sizeof(struct ubiblock_pdu);
  343. dev->tag_set.driver_data = dev;
  344. dev->tag_set.nr_hw_queues = 1;
  345. ret = blk_mq_alloc_tag_set(&dev->tag_set);
  346. if (ret) {
  347. dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
  348. goto out_remove_minor;
  349. }
  350. dev->rq = blk_mq_init_queue(&dev->tag_set);
  351. if (IS_ERR(dev->rq)) {
  352. dev_err(disk_to_dev(gd), "blk_mq_init_queue failed");
  353. ret = PTR_ERR(dev->rq);
  354. goto out_free_tags;
  355. }
  356. blk_queue_max_segments(dev->rq, UBI_MAX_SG_COUNT);
  357. dev->rq->queuedata = dev;
  358. dev->gd->queue = dev->rq;
  359. /*
  360. * Create one workqueue per volume (per registered block device).
  361. * Rembember workqueues are cheap, they're not threads.
  362. */
  363. dev->wq = alloc_workqueue("%s", 0, 0, gd->disk_name);
  364. if (!dev->wq) {
  365. ret = -ENOMEM;
  366. goto out_free_queue;
  367. }
  368. mutex_lock(&devices_mutex);
  369. list_add_tail(&dev->list, &ubiblock_devices);
  370. mutex_unlock(&devices_mutex);
  371. /* Must be the last step: anyone can call file ops from now on */
  372. add_disk(dev->gd);
  373. dev_info(disk_to_dev(dev->gd), "created from ubi%d:%d(%s)",
  374. dev->ubi_num, dev->vol_id, vi->name);
  375. return 0;
  376. out_free_queue:
  377. blk_cleanup_queue(dev->rq);
  378. out_free_tags:
  379. blk_mq_free_tag_set(&dev->tag_set);
  380. out_remove_minor:
  381. idr_remove(&ubiblock_minor_idr, gd->first_minor);
  382. out_put_disk:
  383. put_disk(dev->gd);
  384. out_free_dev:
  385. kfree(dev);
  386. return ret;
  387. }
  388. static void ubiblock_cleanup(struct ubiblock *dev)
  389. {
  390. /* Stop new requests to arrive */
  391. del_gendisk(dev->gd);
  392. /* Flush pending work */
  393. destroy_workqueue(dev->wq);
  394. /* Finally destroy the blk queue */
  395. blk_cleanup_queue(dev->rq);
  396. blk_mq_free_tag_set(&dev->tag_set);
  397. dev_info(disk_to_dev(dev->gd), "released");
  398. idr_remove(&ubiblock_minor_idr, dev->gd->first_minor);
  399. put_disk(dev->gd);
  400. }
  401. int ubiblock_remove(struct ubi_volume_info *vi)
  402. {
  403. struct ubiblock *dev;
  404. mutex_lock(&devices_mutex);
  405. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  406. if (!dev) {
  407. mutex_unlock(&devices_mutex);
  408. return -ENODEV;
  409. }
  410. /* Found a device, let's lock it so we can check if it's busy */
  411. mutex_lock(&dev->dev_mutex);
  412. if (dev->refcnt > 0) {
  413. mutex_unlock(&dev->dev_mutex);
  414. mutex_unlock(&devices_mutex);
  415. return -EBUSY;
  416. }
  417. /* Remove from device list */
  418. list_del(&dev->list);
  419. mutex_unlock(&devices_mutex);
  420. ubiblock_cleanup(dev);
  421. mutex_unlock(&dev->dev_mutex);
  422. kfree(dev);
  423. return 0;
  424. }
  425. static int ubiblock_resize(struct ubi_volume_info *vi)
  426. {
  427. struct ubiblock *dev;
  428. u64 disk_capacity = vi->used_bytes >> 9;
  429. /*
  430. * Need to lock the device list until we stop using the device,
  431. * otherwise the device struct might get released in
  432. * 'ubiblock_remove()'.
  433. */
  434. mutex_lock(&devices_mutex);
  435. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  436. if (!dev) {
  437. mutex_unlock(&devices_mutex);
  438. return -ENODEV;
  439. }
  440. if ((sector_t)disk_capacity != disk_capacity) {
  441. mutex_unlock(&devices_mutex);
  442. dev_warn(disk_to_dev(dev->gd), "the volume is too big (%d LEBs), cannot resize",
  443. vi->size);
  444. return -EFBIG;
  445. }
  446. mutex_lock(&dev->dev_mutex);
  447. if (get_capacity(dev->gd) != disk_capacity) {
  448. set_capacity(dev->gd, disk_capacity);
  449. dev_info(disk_to_dev(dev->gd), "resized to %lld bytes",
  450. vi->used_bytes);
  451. }
  452. mutex_unlock(&dev->dev_mutex);
  453. mutex_unlock(&devices_mutex);
  454. return 0;
  455. }
  456. static int ubiblock_notify(struct notifier_block *nb,
  457. unsigned long notification_type, void *ns_ptr)
  458. {
  459. struct ubi_notification *nt = ns_ptr;
  460. switch (notification_type) {
  461. case UBI_VOLUME_ADDED:
  462. /*
  463. * We want to enforce explicit block device creation for
  464. * volumes, so when a volume is added we do nothing.
  465. */
  466. break;
  467. case UBI_VOLUME_REMOVED:
  468. ubiblock_remove(&nt->vi);
  469. break;
  470. case UBI_VOLUME_RESIZED:
  471. ubiblock_resize(&nt->vi);
  472. break;
  473. case UBI_VOLUME_UPDATED:
  474. /*
  475. * If the volume is static, a content update might mean the
  476. * size (i.e. used_bytes) was also changed.
  477. */
  478. if (nt->vi.vol_type == UBI_STATIC_VOLUME)
  479. ubiblock_resize(&nt->vi);
  480. break;
  481. default:
  482. break;
  483. }
  484. return NOTIFY_OK;
  485. }
  486. static struct notifier_block ubiblock_notifier = {
  487. .notifier_call = ubiblock_notify,
  488. };
  489. static struct ubi_volume_desc * __init
  490. open_volume_desc(const char *name, int ubi_num, int vol_id)
  491. {
  492. if (ubi_num == -1)
  493. /* No ubi num, name must be a vol device path */
  494. return ubi_open_volume_path(name, UBI_READONLY);
  495. else if (vol_id == -1)
  496. /* No vol_id, must be vol_name */
  497. return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
  498. else
  499. return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
  500. }
  501. static void __init ubiblock_create_from_param(void)
  502. {
  503. int i, ret = 0;
  504. struct ubiblock_param *p;
  505. struct ubi_volume_desc *desc;
  506. struct ubi_volume_info vi;
  507. /*
  508. * If there is an error creating one of the ubiblocks, continue on to
  509. * create the following ubiblocks. This helps in a circumstance where
  510. * the kernel command-line specifies multiple block devices and some
  511. * may be broken, but we still want the working ones to come up.
  512. */
  513. for (i = 0; i < ubiblock_devs; i++) {
  514. p = &ubiblock_param[i];
  515. desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
  516. if (IS_ERR(desc)) {
  517. pr_err(
  518. "UBI: block: can't open volume on ubi%d_%d, err=%ld",
  519. p->ubi_num, p->vol_id, PTR_ERR(desc));
  520. continue;
  521. }
  522. ubi_get_volume_info(desc, &vi);
  523. ubi_close_volume(desc);
  524. ret = ubiblock_create(&vi);
  525. if (ret) {
  526. pr_err(
  527. "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d",
  528. vi.name, p->ubi_num, p->vol_id, ret);
  529. continue;
  530. }
  531. }
  532. }
  533. static void ubiblock_remove_all(void)
  534. {
  535. struct ubiblock *next;
  536. struct ubiblock *dev;
  537. list_for_each_entry_safe(dev, next, &ubiblock_devices, list) {
  538. /* The module is being forcefully removed */
  539. WARN_ON(dev->desc);
  540. /* Remove from device list */
  541. list_del(&dev->list);
  542. ubiblock_cleanup(dev);
  543. kfree(dev);
  544. }
  545. }
  546. int __init ubiblock_init(void)
  547. {
  548. int ret;
  549. ubiblock_major = register_blkdev(0, "ubiblock");
  550. if (ubiblock_major < 0)
  551. return ubiblock_major;
  552. /*
  553. * Attach block devices from 'block=' module param.
  554. * Even if one block device in the param list fails to come up,
  555. * still allow the module to load and leave any others up.
  556. */
  557. ubiblock_create_from_param();
  558. /*
  559. * Block devices are only created upon user requests, so we ignore
  560. * existing volumes.
  561. */
  562. ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
  563. if (ret)
  564. goto err_unreg;
  565. return 0;
  566. err_unreg:
  567. unregister_blkdev(ubiblock_major, "ubiblock");
  568. ubiblock_remove_all();
  569. return ret;
  570. }
  571. void __exit ubiblock_exit(void)
  572. {
  573. ubi_unregister_volume_notifier(&ubiblock_notifier);
  574. ubiblock_remove_all();
  575. unregister_blkdev(ubiblock_major, "ubiblock");
  576. }