block.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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, ret);
  268. }
  269. static int 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. if (req->cmd_type != REQ_TYPE_FS)
  276. return BLK_MQ_RQ_QUEUE_ERROR;
  277. if (rq_data_dir(req) != READ)
  278. return BLK_MQ_RQ_QUEUE_ERROR; /* Write not implemented */
  279. ubi_sgl_init(&pdu->usgl);
  280. queue_work(dev->wq, &pdu->work);
  281. return BLK_MQ_RQ_QUEUE_OK;
  282. }
  283. static int ubiblock_init_request(void *data, struct request *req,
  284. unsigned int hctx_idx,
  285. unsigned int request_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 struct blk_mq_ops ubiblock_mq_ops = {
  294. .queue_rq = ubiblock_queue_rq,
  295. .init_request = ubiblock_init_request,
  296. .map_queue = blk_mq_map_queue,
  297. };
  298. int ubiblock_create(struct ubi_volume_info *vi)
  299. {
  300. struct ubiblock *dev;
  301. struct gendisk *gd;
  302. u64 disk_capacity = vi->used_bytes >> 9;
  303. int ret;
  304. if ((sector_t)disk_capacity != disk_capacity)
  305. return -EFBIG;
  306. /* Check that the volume isn't already handled */
  307. mutex_lock(&devices_mutex);
  308. if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
  309. ret = -EEXIST;
  310. goto out_unlock;
  311. }
  312. dev = kzalloc(sizeof(struct ubiblock), GFP_KERNEL);
  313. if (!dev) {
  314. ret = -ENOMEM;
  315. goto out_unlock;
  316. }
  317. mutex_init(&dev->dev_mutex);
  318. dev->ubi_num = vi->ubi_num;
  319. dev->vol_id = vi->vol_id;
  320. dev->leb_size = vi->usable_leb_size;
  321. /* Initialize the gendisk of this ubiblock device */
  322. gd = alloc_disk(1);
  323. if (!gd) {
  324. pr_err("UBI: block: alloc_disk failed");
  325. ret = -ENODEV;
  326. goto out_free_dev;
  327. }
  328. gd->fops = &ubiblock_ops;
  329. gd->major = ubiblock_major;
  330. gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
  331. if (gd->first_minor < 0) {
  332. dev_err(disk_to_dev(gd),
  333. "block: dynamic minor allocation failed");
  334. ret = -ENODEV;
  335. goto out_put_disk;
  336. }
  337. gd->private_data = dev;
  338. sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
  339. set_capacity(gd, disk_capacity);
  340. dev->gd = gd;
  341. dev->tag_set.ops = &ubiblock_mq_ops;
  342. dev->tag_set.queue_depth = 64;
  343. dev->tag_set.numa_node = NUMA_NO_NODE;
  344. dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  345. dev->tag_set.cmd_size = sizeof(struct ubiblock_pdu);
  346. dev->tag_set.driver_data = dev;
  347. dev->tag_set.nr_hw_queues = 1;
  348. ret = blk_mq_alloc_tag_set(&dev->tag_set);
  349. if (ret) {
  350. dev_err(disk_to_dev(dev->gd), "blk_mq_alloc_tag_set failed");
  351. goto out_remove_minor;
  352. }
  353. dev->rq = blk_mq_init_queue(&dev->tag_set);
  354. if (IS_ERR(dev->rq)) {
  355. dev_err(disk_to_dev(gd), "blk_mq_init_queue failed");
  356. ret = PTR_ERR(dev->rq);
  357. goto out_free_tags;
  358. }
  359. blk_queue_max_segments(dev->rq, UBI_MAX_SG_COUNT);
  360. dev->rq->queuedata = dev;
  361. dev->gd->queue = dev->rq;
  362. /*
  363. * Create one workqueue per volume (per registered block device).
  364. * Rembember workqueues are cheap, they're not threads.
  365. */
  366. dev->wq = alloc_workqueue("%s", 0, 0, gd->disk_name);
  367. if (!dev->wq) {
  368. ret = -ENOMEM;
  369. goto out_free_queue;
  370. }
  371. list_add_tail(&dev->list, &ubiblock_devices);
  372. /* Must be the last step: anyone can call file ops from now on */
  373. add_disk(dev->gd);
  374. dev_info(disk_to_dev(dev->gd), "created from ubi%d:%d(%s)",
  375. dev->ubi_num, dev->vol_id, vi->name);
  376. mutex_unlock(&devices_mutex);
  377. return 0;
  378. out_free_queue:
  379. blk_cleanup_queue(dev->rq);
  380. out_free_tags:
  381. blk_mq_free_tag_set(&dev->tag_set);
  382. out_remove_minor:
  383. idr_remove(&ubiblock_minor_idr, gd->first_minor);
  384. out_put_disk:
  385. put_disk(dev->gd);
  386. out_free_dev:
  387. kfree(dev);
  388. out_unlock:
  389. mutex_unlock(&devices_mutex);
  390. return ret;
  391. }
  392. static void ubiblock_cleanup(struct ubiblock *dev)
  393. {
  394. /* Stop new requests to arrive */
  395. del_gendisk(dev->gd);
  396. /* Flush pending work */
  397. destroy_workqueue(dev->wq);
  398. /* Finally destroy the blk queue */
  399. blk_cleanup_queue(dev->rq);
  400. blk_mq_free_tag_set(&dev->tag_set);
  401. dev_info(disk_to_dev(dev->gd), "released");
  402. idr_remove(&ubiblock_minor_idr, dev->gd->first_minor);
  403. put_disk(dev->gd);
  404. }
  405. int ubiblock_remove(struct ubi_volume_info *vi)
  406. {
  407. struct ubiblock *dev;
  408. int ret;
  409. mutex_lock(&devices_mutex);
  410. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  411. if (!dev) {
  412. ret = -ENODEV;
  413. goto out_unlock;
  414. }
  415. /* Found a device, let's lock it so we can check if it's busy */
  416. mutex_lock(&dev->dev_mutex);
  417. if (dev->refcnt > 0) {
  418. ret = -EBUSY;
  419. goto out_unlock_dev;
  420. }
  421. /* Remove from device list */
  422. list_del(&dev->list);
  423. ubiblock_cleanup(dev);
  424. mutex_unlock(&dev->dev_mutex);
  425. mutex_unlock(&devices_mutex);
  426. kfree(dev);
  427. return 0;
  428. out_unlock_dev:
  429. mutex_unlock(&dev->dev_mutex);
  430. out_unlock:
  431. mutex_unlock(&devices_mutex);
  432. return ret;
  433. }
  434. static int ubiblock_resize(struct ubi_volume_info *vi)
  435. {
  436. struct ubiblock *dev;
  437. u64 disk_capacity = vi->used_bytes >> 9;
  438. /*
  439. * Need to lock the device list until we stop using the device,
  440. * otherwise the device struct might get released in
  441. * 'ubiblock_remove()'.
  442. */
  443. mutex_lock(&devices_mutex);
  444. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  445. if (!dev) {
  446. mutex_unlock(&devices_mutex);
  447. return -ENODEV;
  448. }
  449. if ((sector_t)disk_capacity != disk_capacity) {
  450. mutex_unlock(&devices_mutex);
  451. dev_warn(disk_to_dev(dev->gd), "the volume is too big (%d LEBs), cannot resize",
  452. vi->size);
  453. return -EFBIG;
  454. }
  455. mutex_lock(&dev->dev_mutex);
  456. if (get_capacity(dev->gd) != disk_capacity) {
  457. set_capacity(dev->gd, disk_capacity);
  458. dev_info(disk_to_dev(dev->gd), "resized to %lld bytes",
  459. vi->used_bytes);
  460. }
  461. mutex_unlock(&dev->dev_mutex);
  462. mutex_unlock(&devices_mutex);
  463. return 0;
  464. }
  465. static int ubiblock_notify(struct notifier_block *nb,
  466. unsigned long notification_type, void *ns_ptr)
  467. {
  468. struct ubi_notification *nt = ns_ptr;
  469. switch (notification_type) {
  470. case UBI_VOLUME_ADDED:
  471. /*
  472. * We want to enforce explicit block device creation for
  473. * volumes, so when a volume is added we do nothing.
  474. */
  475. break;
  476. case UBI_VOLUME_REMOVED:
  477. ubiblock_remove(&nt->vi);
  478. break;
  479. case UBI_VOLUME_RESIZED:
  480. ubiblock_resize(&nt->vi);
  481. break;
  482. case UBI_VOLUME_UPDATED:
  483. /*
  484. * If the volume is static, a content update might mean the
  485. * size (i.e. used_bytes) was also changed.
  486. */
  487. if (nt->vi.vol_type == UBI_STATIC_VOLUME)
  488. ubiblock_resize(&nt->vi);
  489. break;
  490. default:
  491. break;
  492. }
  493. return NOTIFY_OK;
  494. }
  495. static struct notifier_block ubiblock_notifier = {
  496. .notifier_call = ubiblock_notify,
  497. };
  498. static struct ubi_volume_desc * __init
  499. open_volume_desc(const char *name, int ubi_num, int vol_id)
  500. {
  501. if (ubi_num == -1)
  502. /* No ubi num, name must be a vol device path */
  503. return ubi_open_volume_path(name, UBI_READONLY);
  504. else if (vol_id == -1)
  505. /* No vol_id, must be vol_name */
  506. return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
  507. else
  508. return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
  509. }
  510. static void __init ubiblock_create_from_param(void)
  511. {
  512. int i, ret = 0;
  513. struct ubiblock_param *p;
  514. struct ubi_volume_desc *desc;
  515. struct ubi_volume_info vi;
  516. /*
  517. * If there is an error creating one of the ubiblocks, continue on to
  518. * create the following ubiblocks. This helps in a circumstance where
  519. * the kernel command-line specifies multiple block devices and some
  520. * may be broken, but we still want the working ones to come up.
  521. */
  522. for (i = 0; i < ubiblock_devs; i++) {
  523. p = &ubiblock_param[i];
  524. desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
  525. if (IS_ERR(desc)) {
  526. pr_err(
  527. "UBI: block: can't open volume on ubi%d_%d, err=%ld",
  528. p->ubi_num, p->vol_id, PTR_ERR(desc));
  529. continue;
  530. }
  531. ubi_get_volume_info(desc, &vi);
  532. ubi_close_volume(desc);
  533. ret = ubiblock_create(&vi);
  534. if (ret) {
  535. pr_err(
  536. "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d",
  537. vi.name, p->ubi_num, p->vol_id, ret);
  538. continue;
  539. }
  540. }
  541. }
  542. static void ubiblock_remove_all(void)
  543. {
  544. struct ubiblock *next;
  545. struct ubiblock *dev;
  546. mutex_lock(&devices_mutex);
  547. list_for_each_entry_safe(dev, next, &ubiblock_devices, list) {
  548. /* The module is being forcefully removed */
  549. WARN_ON(dev->desc);
  550. /* Remove from device list */
  551. list_del(&dev->list);
  552. ubiblock_cleanup(dev);
  553. kfree(dev);
  554. }
  555. mutex_unlock(&devices_mutex);
  556. }
  557. int __init ubiblock_init(void)
  558. {
  559. int ret;
  560. ubiblock_major = register_blkdev(0, "ubiblock");
  561. if (ubiblock_major < 0)
  562. return ubiblock_major;
  563. /*
  564. * Attach block devices from 'block=' module param.
  565. * Even if one block device in the param list fails to come up,
  566. * still allow the module to load and leave any others up.
  567. */
  568. ubiblock_create_from_param();
  569. /*
  570. * Block devices are only created upon user requests, so we ignore
  571. * existing volumes.
  572. */
  573. ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
  574. if (ret)
  575. goto err_unreg;
  576. return 0;
  577. err_unreg:
  578. unregister_blkdev(ubiblock_major, "ubiblock");
  579. ubiblock_remove_all();
  580. return ret;
  581. }
  582. void __exit ubiblock_exit(void)
  583. {
  584. ubi_unregister_volume_notifier(&ubiblock_notifier);
  585. ubiblock_remove_all();
  586. unregister_blkdev(ubiblock_major, "ubiblock");
  587. }