block.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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_IDR(ubiblock_minor_idr);
  88. /* Protects ubiblock_devices and ubiblock_minor_idr */
  89. static DEFINE_MUTEX(devices_mutex);
  90. static int ubiblock_major;
  91. static int __init ubiblock_set_param(const char *val,
  92. const struct kernel_param *kp)
  93. {
  94. int i, ret;
  95. size_t len;
  96. struct ubiblock_param *param;
  97. char buf[UBIBLOCK_PARAM_LEN];
  98. char *pbuf = &buf[0];
  99. char *tokens[UBIBLOCK_PARAM_COUNT];
  100. if (!val)
  101. return -EINVAL;
  102. len = strnlen(val, UBIBLOCK_PARAM_LEN);
  103. if (len == 0) {
  104. pr_warn("UBI: block: empty 'block=' parameter - ignored\n");
  105. return 0;
  106. }
  107. if (len == UBIBLOCK_PARAM_LEN) {
  108. pr_err("UBI: block: parameter \"%s\" is too long, max. is %d\n",
  109. val, UBIBLOCK_PARAM_LEN);
  110. return -EINVAL;
  111. }
  112. strcpy(buf, val);
  113. /* Get rid of the final newline */
  114. if (buf[len - 1] == '\n')
  115. buf[len - 1] = '\0';
  116. for (i = 0; i < UBIBLOCK_PARAM_COUNT; i++)
  117. tokens[i] = strsep(&pbuf, ",");
  118. param = &ubiblock_param[ubiblock_devs];
  119. if (tokens[1]) {
  120. /* Two parameters: can be 'ubi, vol_id' or 'ubi, vol_name' */
  121. ret = kstrtoint(tokens[0], 10, &param->ubi_num);
  122. if (ret < 0)
  123. return -EINVAL;
  124. /* Second param can be a number or a name */
  125. ret = kstrtoint(tokens[1], 10, &param->vol_id);
  126. if (ret < 0) {
  127. param->vol_id = -1;
  128. strcpy(param->name, tokens[1]);
  129. }
  130. } else {
  131. /* One parameter: must be device path */
  132. strcpy(param->name, tokens[0]);
  133. param->ubi_num = -1;
  134. param->vol_id = -1;
  135. }
  136. ubiblock_devs++;
  137. return 0;
  138. }
  139. static const struct kernel_param_ops ubiblock_param_ops = {
  140. .set = ubiblock_set_param,
  141. };
  142. module_param_cb(block, &ubiblock_param_ops, NULL, 0);
  143. MODULE_PARM_DESC(block, "Attach block devices to UBI volumes. Parameter format: block=<path|dev,num|dev,name>.\n"
  144. "Multiple \"block\" parameters may be specified.\n"
  145. "UBI volumes may be specified by their number, name, or path to the device node.\n"
  146. "Examples\n"
  147. "Using the UBI volume path:\n"
  148. "ubi.block=/dev/ubi0_0\n"
  149. "Using the UBI device, and the volume name:\n"
  150. "ubi.block=0,rootfs\n"
  151. "Using both UBI device number and UBI volume number:\n"
  152. "ubi.block=0,0\n");
  153. static struct ubiblock *find_dev_nolock(int ubi_num, int vol_id)
  154. {
  155. struct ubiblock *dev;
  156. list_for_each_entry(dev, &ubiblock_devices, list)
  157. if (dev->ubi_num == ubi_num && dev->vol_id == vol_id)
  158. return dev;
  159. return NULL;
  160. }
  161. static int ubiblock_read(struct ubiblock_pdu *pdu)
  162. {
  163. int ret, leb, offset, bytes_left, to_read;
  164. u64 pos;
  165. struct request *req = blk_mq_rq_from_pdu(pdu);
  166. struct ubiblock *dev = req->q->queuedata;
  167. to_read = blk_rq_bytes(req);
  168. pos = blk_rq_pos(req) << 9;
  169. /* Get LEB:offset address to read from */
  170. offset = do_div(pos, dev->leb_size);
  171. leb = pos;
  172. bytes_left = to_read;
  173. while (bytes_left) {
  174. /*
  175. * We can only read one LEB at a time. Therefore if the read
  176. * length is larger than one LEB size, we split the operation.
  177. */
  178. if (offset + to_read > dev->leb_size)
  179. to_read = dev->leb_size - offset;
  180. ret = ubi_read_sg(dev->desc, leb, &pdu->usgl, offset, to_read);
  181. if (ret < 0)
  182. return ret;
  183. bytes_left -= to_read;
  184. to_read = bytes_left;
  185. leb += 1;
  186. offset = 0;
  187. }
  188. return 0;
  189. }
  190. static int ubiblock_open(struct block_device *bdev, fmode_t mode)
  191. {
  192. struct ubiblock *dev = bdev->bd_disk->private_data;
  193. int ret;
  194. mutex_lock(&dev->dev_mutex);
  195. if (dev->refcnt > 0) {
  196. /*
  197. * The volume is already open, just increase the reference
  198. * counter.
  199. */
  200. goto out_done;
  201. }
  202. /*
  203. * We want users to be aware they should only mount us as read-only.
  204. * It's just a paranoid check, as write requests will get rejected
  205. * in any case.
  206. */
  207. if (mode & FMODE_WRITE) {
  208. ret = -EROFS;
  209. goto out_unlock;
  210. }
  211. dev->desc = ubi_open_volume(dev->ubi_num, dev->vol_id, UBI_READONLY);
  212. if (IS_ERR(dev->desc)) {
  213. dev_err(disk_to_dev(dev->gd), "failed to open ubi volume %d_%d",
  214. dev->ubi_num, dev->vol_id);
  215. ret = PTR_ERR(dev->desc);
  216. dev->desc = NULL;
  217. goto out_unlock;
  218. }
  219. out_done:
  220. dev->refcnt++;
  221. mutex_unlock(&dev->dev_mutex);
  222. return 0;
  223. out_unlock:
  224. mutex_unlock(&dev->dev_mutex);
  225. return ret;
  226. }
  227. static void ubiblock_release(struct gendisk *gd, fmode_t mode)
  228. {
  229. struct ubiblock *dev = gd->private_data;
  230. mutex_lock(&dev->dev_mutex);
  231. dev->refcnt--;
  232. if (dev->refcnt == 0) {
  233. ubi_close_volume(dev->desc);
  234. dev->desc = NULL;
  235. }
  236. mutex_unlock(&dev->dev_mutex);
  237. }
  238. static int ubiblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  239. {
  240. /* Some tools might require this information */
  241. geo->heads = 1;
  242. geo->cylinders = 1;
  243. geo->sectors = get_capacity(bdev->bd_disk);
  244. geo->start = 0;
  245. return 0;
  246. }
  247. static const struct block_device_operations ubiblock_ops = {
  248. .owner = THIS_MODULE,
  249. .open = ubiblock_open,
  250. .release = ubiblock_release,
  251. .getgeo = ubiblock_getgeo,
  252. };
  253. static void ubiblock_do_work(struct work_struct *work)
  254. {
  255. int ret;
  256. struct ubiblock_pdu *pdu = container_of(work, struct ubiblock_pdu, work);
  257. struct request *req = blk_mq_rq_from_pdu(pdu);
  258. blk_mq_start_request(req);
  259. /*
  260. * It is safe to ignore the return value of blk_rq_map_sg() because
  261. * the number of sg entries is limited to UBI_MAX_SG_COUNT
  262. * and ubi_read_sg() will check that limit.
  263. */
  264. blk_rq_map_sg(req->q, req, pdu->usgl.sg);
  265. ret = ubiblock_read(pdu);
  266. rq_flush_dcache_pages(req);
  267. blk_mq_end_request(req, errno_to_blk_status(ret));
  268. }
  269. static blk_status_t ubiblock_queue_rq(struct blk_mq_hw_ctx *hctx,
  270. const struct blk_mq_queue_data *bd)
  271. {
  272. struct request *req = bd->rq;
  273. struct ubiblock *dev = hctx->queue->queuedata;
  274. struct ubiblock_pdu *pdu = blk_mq_rq_to_pdu(req);
  275. switch (req_op(req)) {
  276. case REQ_OP_READ:
  277. ubi_sgl_init(&pdu->usgl);
  278. queue_work(dev->wq, &pdu->work);
  279. return BLK_STS_OK;
  280. default:
  281. return BLK_STS_IOERR;
  282. }
  283. }
  284. static int ubiblock_init_request(struct blk_mq_tag_set *set,
  285. struct request *req, unsigned int hctx_idx,
  286. unsigned int numa_node)
  287. {
  288. struct ubiblock_pdu *pdu = blk_mq_rq_to_pdu(req);
  289. sg_init_table(pdu->usgl.sg, UBI_MAX_SG_COUNT);
  290. INIT_WORK(&pdu->work, ubiblock_do_work);
  291. return 0;
  292. }
  293. static const struct blk_mq_ops ubiblock_mq_ops = {
  294. .queue_rq = ubiblock_queue_rq,
  295. .init_request = ubiblock_init_request,
  296. };
  297. int ubiblock_create(struct ubi_volume_info *vi)
  298. {
  299. struct ubiblock *dev;
  300. struct gendisk *gd;
  301. u64 disk_capacity = vi->used_bytes >> 9;
  302. int ret;
  303. if ((sector_t)disk_capacity != disk_capacity)
  304. return -EFBIG;
  305. /* Check that the volume isn't already handled */
  306. mutex_lock(&devices_mutex);
  307. if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
  308. ret = -EEXIST;
  309. goto out_unlock;
  310. }
  311. dev = kzalloc(sizeof(struct ubiblock), GFP_KERNEL);
  312. if (!dev) {
  313. ret = -ENOMEM;
  314. goto out_unlock;
  315. }
  316. mutex_init(&dev->dev_mutex);
  317. dev->ubi_num = vi->ubi_num;
  318. dev->vol_id = vi->vol_id;
  319. dev->leb_size = vi->usable_leb_size;
  320. /* Initialize the gendisk of this ubiblock device */
  321. gd = alloc_disk(1);
  322. if (!gd) {
  323. pr_err("UBI: block: alloc_disk failed\n");
  324. ret = -ENODEV;
  325. goto out_free_dev;
  326. }
  327. gd->fops = &ubiblock_ops;
  328. gd->major = ubiblock_major;
  329. gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
  330. if (gd->first_minor < 0) {
  331. dev_err(disk_to_dev(gd),
  332. "block: dynamic minor allocation failed");
  333. ret = -ENODEV;
  334. goto out_put_disk;
  335. }
  336. gd->private_data = dev;
  337. sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
  338. set_capacity(gd, disk_capacity);
  339. dev->gd = gd;
  340. dev->tag_set.ops = &ubiblock_mq_ops;
  341. dev->tag_set.queue_depth = 64;
  342. dev->tag_set.numa_node = NUMA_NO_NODE;
  343. dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  344. dev->tag_set.cmd_size = sizeof(struct ubiblock_pdu);
  345. dev->tag_set.driver_data = dev;
  346. dev->tag_set.nr_hw_queues = 1;
  347. ret = blk_mq_alloc_tag_set(&dev->tag_set);
  348. if (ret) {
  349. dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
  350. goto out_remove_minor;
  351. }
  352. dev->rq = blk_mq_init_queue(&dev->tag_set);
  353. if (IS_ERR(dev->rq)) {
  354. dev_err(disk_to_dev(gd), "blk_mq_init_queue failed");
  355. ret = PTR_ERR(dev->rq);
  356. goto out_free_tags;
  357. }
  358. blk_queue_max_segments(dev->rq, UBI_MAX_SG_COUNT);
  359. dev->rq->queuedata = dev;
  360. dev->gd->queue = dev->rq;
  361. /*
  362. * Create one workqueue per volume (per registered block device).
  363. * Rembember workqueues are cheap, they're not threads.
  364. */
  365. dev->wq = alloc_workqueue("%s", 0, 0, gd->disk_name);
  366. if (!dev->wq) {
  367. ret = -ENOMEM;
  368. goto out_free_queue;
  369. }
  370. list_add_tail(&dev->list, &ubiblock_devices);
  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. mutex_unlock(&devices_mutex);
  376. return 0;
  377. out_free_queue:
  378. blk_cleanup_queue(dev->rq);
  379. out_free_tags:
  380. blk_mq_free_tag_set(&dev->tag_set);
  381. out_remove_minor:
  382. idr_remove(&ubiblock_minor_idr, gd->first_minor);
  383. out_put_disk:
  384. put_disk(dev->gd);
  385. out_free_dev:
  386. kfree(dev);
  387. out_unlock:
  388. mutex_unlock(&devices_mutex);
  389. return ret;
  390. }
  391. static void ubiblock_cleanup(struct ubiblock *dev)
  392. {
  393. /* Stop new requests to arrive */
  394. del_gendisk(dev->gd);
  395. /* Flush pending work */
  396. destroy_workqueue(dev->wq);
  397. /* Finally destroy the blk queue */
  398. blk_cleanup_queue(dev->rq);
  399. blk_mq_free_tag_set(&dev->tag_set);
  400. dev_info(disk_to_dev(dev->gd), "released");
  401. idr_remove(&ubiblock_minor_idr, dev->gd->first_minor);
  402. put_disk(dev->gd);
  403. }
  404. int ubiblock_remove(struct ubi_volume_info *vi)
  405. {
  406. struct ubiblock *dev;
  407. int ret;
  408. mutex_lock(&devices_mutex);
  409. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  410. if (!dev) {
  411. ret = -ENODEV;
  412. goto out_unlock;
  413. }
  414. /* Found a device, let's lock it so we can check if it's busy */
  415. mutex_lock(&dev->dev_mutex);
  416. if (dev->refcnt > 0) {
  417. ret = -EBUSY;
  418. goto out_unlock_dev;
  419. }
  420. /* Remove from device list */
  421. list_del(&dev->list);
  422. ubiblock_cleanup(dev);
  423. mutex_unlock(&dev->dev_mutex);
  424. mutex_unlock(&devices_mutex);
  425. kfree(dev);
  426. return 0;
  427. out_unlock_dev:
  428. mutex_unlock(&dev->dev_mutex);
  429. out_unlock:
  430. mutex_unlock(&devices_mutex);
  431. return ret;
  432. }
  433. static int ubiblock_resize(struct ubi_volume_info *vi)
  434. {
  435. struct ubiblock *dev;
  436. u64 disk_capacity = vi->used_bytes >> 9;
  437. /*
  438. * Need to lock the device list until we stop using the device,
  439. * otherwise the device struct might get released in
  440. * 'ubiblock_remove()'.
  441. */
  442. mutex_lock(&devices_mutex);
  443. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  444. if (!dev) {
  445. mutex_unlock(&devices_mutex);
  446. return -ENODEV;
  447. }
  448. if ((sector_t)disk_capacity != disk_capacity) {
  449. mutex_unlock(&devices_mutex);
  450. dev_warn(disk_to_dev(dev->gd), "the volume is too big (%d LEBs), cannot resize",
  451. vi->size);
  452. return -EFBIG;
  453. }
  454. mutex_lock(&dev->dev_mutex);
  455. if (get_capacity(dev->gd) != disk_capacity) {
  456. set_capacity(dev->gd, disk_capacity);
  457. dev_info(disk_to_dev(dev->gd), "resized to %lld bytes",
  458. vi->used_bytes);
  459. }
  460. mutex_unlock(&dev->dev_mutex);
  461. mutex_unlock(&devices_mutex);
  462. return 0;
  463. }
  464. static int ubiblock_notify(struct notifier_block *nb,
  465. unsigned long notification_type, void *ns_ptr)
  466. {
  467. struct ubi_notification *nt = ns_ptr;
  468. switch (notification_type) {
  469. case UBI_VOLUME_ADDED:
  470. /*
  471. * We want to enforce explicit block device creation for
  472. * volumes, so when a volume is added we do nothing.
  473. */
  474. break;
  475. case UBI_VOLUME_REMOVED:
  476. ubiblock_remove(&nt->vi);
  477. break;
  478. case UBI_VOLUME_RESIZED:
  479. ubiblock_resize(&nt->vi);
  480. break;
  481. case UBI_VOLUME_UPDATED:
  482. /*
  483. * If the volume is static, a content update might mean the
  484. * size (i.e. used_bytes) was also changed.
  485. */
  486. if (nt->vi.vol_type == UBI_STATIC_VOLUME)
  487. ubiblock_resize(&nt->vi);
  488. break;
  489. default:
  490. break;
  491. }
  492. return NOTIFY_OK;
  493. }
  494. static struct notifier_block ubiblock_notifier = {
  495. .notifier_call = ubiblock_notify,
  496. };
  497. static struct ubi_volume_desc * __init
  498. open_volume_desc(const char *name, int ubi_num, int vol_id)
  499. {
  500. if (ubi_num == -1)
  501. /* No ubi num, name must be a vol device path */
  502. return ubi_open_volume_path(name, UBI_READONLY);
  503. else if (vol_id == -1)
  504. /* No vol_id, must be vol_name */
  505. return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
  506. else
  507. return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
  508. }
  509. static void __init ubiblock_create_from_param(void)
  510. {
  511. int i, ret = 0;
  512. struct ubiblock_param *p;
  513. struct ubi_volume_desc *desc;
  514. struct ubi_volume_info vi;
  515. /*
  516. * If there is an error creating one of the ubiblocks, continue on to
  517. * create the following ubiblocks. This helps in a circumstance where
  518. * the kernel command-line specifies multiple block devices and some
  519. * may be broken, but we still want the working ones to come up.
  520. */
  521. for (i = 0; i < ubiblock_devs; i++) {
  522. p = &ubiblock_param[i];
  523. desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
  524. if (IS_ERR(desc)) {
  525. pr_err(
  526. "UBI: block: can't open volume on ubi%d_%d, err=%ld\n",
  527. p->ubi_num, p->vol_id, PTR_ERR(desc));
  528. continue;
  529. }
  530. ubi_get_volume_info(desc, &vi);
  531. ubi_close_volume(desc);
  532. ret = ubiblock_create(&vi);
  533. if (ret) {
  534. pr_err(
  535. "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n",
  536. vi.name, p->ubi_num, p->vol_id, ret);
  537. continue;
  538. }
  539. }
  540. }
  541. static void ubiblock_remove_all(void)
  542. {
  543. struct ubiblock *next;
  544. struct ubiblock *dev;
  545. mutex_lock(&devices_mutex);
  546. list_for_each_entry_safe(dev, next, &ubiblock_devices, list) {
  547. /* The module is being forcefully removed */
  548. WARN_ON(dev->desc);
  549. /* Remove from device list */
  550. list_del(&dev->list);
  551. ubiblock_cleanup(dev);
  552. kfree(dev);
  553. }
  554. mutex_unlock(&devices_mutex);
  555. }
  556. int __init ubiblock_init(void)
  557. {
  558. int ret;
  559. ubiblock_major = register_blkdev(0, "ubiblock");
  560. if (ubiblock_major < 0)
  561. return ubiblock_major;
  562. /*
  563. * Attach block devices from 'block=' module param.
  564. * Even if one block device in the param list fails to come up,
  565. * still allow the module to load and leave any others up.
  566. */
  567. ubiblock_create_from_param();
  568. /*
  569. * Block devices are only created upon user requests, so we ignore
  570. * existing volumes.
  571. */
  572. ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
  573. if (ret)
  574. goto err_unreg;
  575. return 0;
  576. err_unreg:
  577. unregister_blkdev(ubiblock_major, "ubiblock");
  578. ubiblock_remove_all();
  579. return ret;
  580. }
  581. void __exit ubiblock_exit(void)
  582. {
  583. ubi_unregister_volume_notifier(&ubiblock_notifier);
  584. ubiblock_remove_all();
  585. unregister_blkdev(ubiblock_major, "ubiblock");
  586. }