block.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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/vmalloc.h>
  44. #include <linux/mtd/ubi.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/hdreg.h>
  48. #include <asm/div64.h>
  49. #include "ubi-media.h"
  50. #include "ubi.h"
  51. /* Maximum number of supported devices */
  52. #define UBIBLOCK_MAX_DEVICES 32
  53. /* Maximum length of the 'block=' parameter */
  54. #define UBIBLOCK_PARAM_LEN 63
  55. /* Maximum number of comma-separated items in the 'block=' parameter */
  56. #define UBIBLOCK_PARAM_COUNT 2
  57. struct ubiblock_param {
  58. int ubi_num;
  59. int vol_id;
  60. char name[UBIBLOCK_PARAM_LEN+1];
  61. };
  62. /* Numbers of elements set in the @ubiblock_param array */
  63. static int ubiblock_devs __initdata;
  64. /* MTD devices specification parameters */
  65. static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata;
  66. struct ubiblock {
  67. struct ubi_volume_desc *desc;
  68. int ubi_num;
  69. int vol_id;
  70. int refcnt;
  71. int leb_size;
  72. struct gendisk *gd;
  73. struct request_queue *rq;
  74. struct workqueue_struct *wq;
  75. struct work_struct work;
  76. struct mutex dev_mutex;
  77. spinlock_t queue_lock;
  78. struct list_head list;
  79. };
  80. /* Linked list of all ubiblock instances */
  81. static LIST_HEAD(ubiblock_devices);
  82. static DEFINE_MUTEX(devices_mutex);
  83. static int ubiblock_major;
  84. static int __init ubiblock_set_param(const char *val,
  85. const struct kernel_param *kp)
  86. {
  87. int i, ret;
  88. size_t len;
  89. struct ubiblock_param *param;
  90. char buf[UBIBLOCK_PARAM_LEN];
  91. char *pbuf = &buf[0];
  92. char *tokens[UBIBLOCK_PARAM_COUNT];
  93. if (!val)
  94. return -EINVAL;
  95. len = strnlen(val, UBIBLOCK_PARAM_LEN);
  96. if (len == 0) {
  97. pr_warn("UBI: block: empty 'block=' parameter - ignored\n");
  98. return 0;
  99. }
  100. if (len == UBIBLOCK_PARAM_LEN) {
  101. pr_err("UBI: block: parameter \"%s\" is too long, max. is %d\n",
  102. val, UBIBLOCK_PARAM_LEN);
  103. return -EINVAL;
  104. }
  105. strcpy(buf, val);
  106. /* Get rid of the final newline */
  107. if (buf[len - 1] == '\n')
  108. buf[len - 1] = '\0';
  109. for (i = 0; i < UBIBLOCK_PARAM_COUNT; i++)
  110. tokens[i] = strsep(&pbuf, ",");
  111. param = &ubiblock_param[ubiblock_devs];
  112. if (tokens[1]) {
  113. /* Two parameters: can be 'ubi, vol_id' or 'ubi, vol_name' */
  114. ret = kstrtoint(tokens[0], 10, &param->ubi_num);
  115. if (ret < 0)
  116. return -EINVAL;
  117. /* Second param can be a number or a name */
  118. ret = kstrtoint(tokens[1], 10, &param->vol_id);
  119. if (ret < 0) {
  120. param->vol_id = -1;
  121. strcpy(param->name, tokens[1]);
  122. }
  123. } else {
  124. /* One parameter: must be device path */
  125. strcpy(param->name, tokens[0]);
  126. param->ubi_num = -1;
  127. param->vol_id = -1;
  128. }
  129. ubiblock_devs++;
  130. return 0;
  131. }
  132. static struct kernel_param_ops ubiblock_param_ops = {
  133. .set = ubiblock_set_param,
  134. };
  135. module_param_cb(block, &ubiblock_param_ops, NULL, 0);
  136. MODULE_PARM_DESC(block, "Attach block devices to UBI volumes. Parameter format: block=<path|dev,num|dev,name>.\n"
  137. "Multiple \"block\" parameters may be specified.\n"
  138. "UBI volumes may be specified by their number, name, or path to the device node.\n"
  139. "Examples\n"
  140. "Using the UBI volume path:\n"
  141. "ubi.block=/dev/ubi0_0\n"
  142. "Using the UBI device, and the volume name:\n"
  143. "ubi.block=0,rootfs\n"
  144. "Using both UBI device number and UBI volume number:\n"
  145. "ubi.block=0,0\n");
  146. static struct ubiblock *find_dev_nolock(int ubi_num, int vol_id)
  147. {
  148. struct ubiblock *dev;
  149. list_for_each_entry(dev, &ubiblock_devices, list)
  150. if (dev->ubi_num == ubi_num && dev->vol_id == vol_id)
  151. return dev;
  152. return NULL;
  153. }
  154. static int ubiblock_read_to_buf(struct ubiblock *dev, char *buffer,
  155. int leb, int offset, int len)
  156. {
  157. int ret;
  158. ret = ubi_read(dev->desc, leb, buffer, offset, len);
  159. if (ret) {
  160. dev_err(disk_to_dev(dev->gd), "%d while reading from LEB %d (offset %d, length %d)",
  161. ret, leb, offset, len);
  162. return ret;
  163. }
  164. return 0;
  165. }
  166. static int ubiblock_read(struct ubiblock *dev, char *buffer,
  167. sector_t sec, int len)
  168. {
  169. int ret, leb, offset;
  170. int bytes_left = len;
  171. int to_read = len;
  172. u64 pos = sec << 9;
  173. /* Get LEB:offset address to read from */
  174. offset = do_div(pos, dev->leb_size);
  175. leb = pos;
  176. while (bytes_left) {
  177. /*
  178. * We can only read one LEB at a time. Therefore if the read
  179. * length is larger than one LEB size, we split the operation.
  180. */
  181. if (offset + to_read > dev->leb_size)
  182. to_read = dev->leb_size - offset;
  183. ret = ubiblock_read_to_buf(dev, buffer, leb, offset, to_read);
  184. if (ret)
  185. return ret;
  186. buffer += to_read;
  187. bytes_left -= to_read;
  188. to_read = bytes_left;
  189. leb += 1;
  190. offset = 0;
  191. }
  192. return 0;
  193. }
  194. static int do_ubiblock_request(struct ubiblock *dev, struct request *req)
  195. {
  196. int len, ret;
  197. sector_t sec;
  198. if (req->cmd_type != REQ_TYPE_FS)
  199. return -EIO;
  200. if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
  201. get_capacity(req->rq_disk))
  202. return -EIO;
  203. if (rq_data_dir(req) != READ)
  204. return -ENOSYS; /* Write not implemented */
  205. sec = blk_rq_pos(req);
  206. len = blk_rq_cur_bytes(req);
  207. /*
  208. * Let's prevent the device from being removed while we're doing I/O
  209. * work. Notice that this means we serialize all the I/O operations,
  210. * but it's probably of no impact given the NAND core serializes
  211. * flash access anyway.
  212. */
  213. mutex_lock(&dev->dev_mutex);
  214. ret = ubiblock_read(dev, bio_data(req->bio), sec, len);
  215. mutex_unlock(&dev->dev_mutex);
  216. return ret;
  217. }
  218. static void ubiblock_do_work(struct work_struct *work)
  219. {
  220. struct ubiblock *dev =
  221. container_of(work, struct ubiblock, work);
  222. struct request_queue *rq = dev->rq;
  223. struct request *req;
  224. int res;
  225. spin_lock_irq(rq->queue_lock);
  226. req = blk_fetch_request(rq);
  227. while (req) {
  228. spin_unlock_irq(rq->queue_lock);
  229. res = do_ubiblock_request(dev, req);
  230. spin_lock_irq(rq->queue_lock);
  231. /*
  232. * If we're done with this request,
  233. * we need to fetch a new one
  234. */
  235. if (!__blk_end_request_cur(req, res))
  236. req = blk_fetch_request(rq);
  237. }
  238. spin_unlock_irq(rq->queue_lock);
  239. }
  240. static void ubiblock_request(struct request_queue *rq)
  241. {
  242. struct ubiblock *dev;
  243. struct request *req;
  244. dev = rq->queuedata;
  245. if (!dev)
  246. while ((req = blk_fetch_request(rq)) != NULL)
  247. __blk_end_request_all(req, -ENODEV);
  248. else
  249. queue_work(dev->wq, &dev->work);
  250. }
  251. static int ubiblock_open(struct block_device *bdev, fmode_t mode)
  252. {
  253. struct ubiblock *dev = bdev->bd_disk->private_data;
  254. int ret;
  255. mutex_lock(&dev->dev_mutex);
  256. if (dev->refcnt > 0) {
  257. /*
  258. * The volume is already open, just increase the reference
  259. * counter.
  260. */
  261. goto out_done;
  262. }
  263. /*
  264. * We want users to be aware they should only mount us as read-only.
  265. * It's just a paranoid check, as write requests will get rejected
  266. * in any case.
  267. */
  268. if (mode & FMODE_WRITE) {
  269. ret = -EPERM;
  270. goto out_unlock;
  271. }
  272. dev->desc = ubi_open_volume(dev->ubi_num, dev->vol_id, UBI_READONLY);
  273. if (IS_ERR(dev->desc)) {
  274. dev_err(disk_to_dev(dev->gd), "failed to open ubi volume %d_%d",
  275. dev->ubi_num, dev->vol_id);
  276. ret = PTR_ERR(dev->desc);
  277. dev->desc = NULL;
  278. goto out_unlock;
  279. }
  280. out_done:
  281. dev->refcnt++;
  282. mutex_unlock(&dev->dev_mutex);
  283. return 0;
  284. out_unlock:
  285. mutex_unlock(&dev->dev_mutex);
  286. return ret;
  287. }
  288. static void ubiblock_release(struct gendisk *gd, fmode_t mode)
  289. {
  290. struct ubiblock *dev = gd->private_data;
  291. mutex_lock(&dev->dev_mutex);
  292. dev->refcnt--;
  293. if (dev->refcnt == 0) {
  294. ubi_close_volume(dev->desc);
  295. dev->desc = NULL;
  296. }
  297. mutex_unlock(&dev->dev_mutex);
  298. }
  299. static int ubiblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  300. {
  301. /* Some tools might require this information */
  302. geo->heads = 1;
  303. geo->cylinders = 1;
  304. geo->sectors = get_capacity(bdev->bd_disk);
  305. geo->start = 0;
  306. return 0;
  307. }
  308. static const struct block_device_operations ubiblock_ops = {
  309. .owner = THIS_MODULE,
  310. .open = ubiblock_open,
  311. .release = ubiblock_release,
  312. .getgeo = ubiblock_getgeo,
  313. };
  314. int ubiblock_create(struct ubi_volume_info *vi)
  315. {
  316. struct ubiblock *dev;
  317. struct gendisk *gd;
  318. u64 disk_capacity = vi->used_bytes >> 9;
  319. int ret;
  320. if ((sector_t)disk_capacity != disk_capacity)
  321. return -EFBIG;
  322. /* Check that the volume isn't already handled */
  323. mutex_lock(&devices_mutex);
  324. if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
  325. mutex_unlock(&devices_mutex);
  326. return -EEXIST;
  327. }
  328. mutex_unlock(&devices_mutex);
  329. dev = kzalloc(sizeof(struct ubiblock), GFP_KERNEL);
  330. if (!dev)
  331. return -ENOMEM;
  332. mutex_init(&dev->dev_mutex);
  333. dev->ubi_num = vi->ubi_num;
  334. dev->vol_id = vi->vol_id;
  335. dev->leb_size = vi->usable_leb_size;
  336. /* Initialize the gendisk of this ubiblock device */
  337. gd = alloc_disk(1);
  338. if (!gd) {
  339. pr_err("UBI: block: alloc_disk failed");
  340. ret = -ENODEV;
  341. goto out_free_dev;
  342. }
  343. gd->fops = &ubiblock_ops;
  344. gd->major = ubiblock_major;
  345. gd->first_minor = dev->ubi_num * UBI_MAX_VOLUMES + dev->vol_id;
  346. gd->private_data = dev;
  347. sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
  348. set_capacity(gd, disk_capacity);
  349. dev->gd = gd;
  350. spin_lock_init(&dev->queue_lock);
  351. dev->rq = blk_init_queue(ubiblock_request, &dev->queue_lock);
  352. if (!dev->rq) {
  353. dev_err(disk_to_dev(gd), "blk_init_queue failed");
  354. ret = -ENODEV;
  355. goto out_put_disk;
  356. }
  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. INIT_WORK(&dev->work, ubiblock_do_work);
  369. mutex_lock(&devices_mutex);
  370. list_add_tail(&dev->list, &ubiblock_devices);
  371. mutex_unlock(&devices_mutex);
  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. return 0;
  377. out_free_queue:
  378. blk_cleanup_queue(dev->rq);
  379. out_put_disk:
  380. put_disk(dev->gd);
  381. out_free_dev:
  382. kfree(dev);
  383. return ret;
  384. }
  385. static void ubiblock_cleanup(struct ubiblock *dev)
  386. {
  387. del_gendisk(dev->gd);
  388. blk_cleanup_queue(dev->rq);
  389. dev_info(disk_to_dev(dev->gd), "released");
  390. put_disk(dev->gd);
  391. }
  392. int ubiblock_remove(struct ubi_volume_info *vi)
  393. {
  394. struct ubiblock *dev;
  395. mutex_lock(&devices_mutex);
  396. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  397. if (!dev) {
  398. mutex_unlock(&devices_mutex);
  399. return -ENODEV;
  400. }
  401. /* Found a device, let's lock it so we can check if it's busy */
  402. mutex_lock(&dev->dev_mutex);
  403. if (dev->refcnt > 0) {
  404. mutex_unlock(&dev->dev_mutex);
  405. mutex_unlock(&devices_mutex);
  406. return -EBUSY;
  407. }
  408. /* Remove from device list */
  409. list_del(&dev->list);
  410. mutex_unlock(&devices_mutex);
  411. /* Flush pending work and stop this workqueue */
  412. destroy_workqueue(dev->wq);
  413. ubiblock_cleanup(dev);
  414. mutex_unlock(&dev->dev_mutex);
  415. kfree(dev);
  416. return 0;
  417. }
  418. static int ubiblock_resize(struct ubi_volume_info *vi)
  419. {
  420. struct ubiblock *dev;
  421. u64 disk_capacity = vi->used_bytes >> 9;
  422. /*
  423. * Need to lock the device list until we stop using the device,
  424. * otherwise the device struct might get released in
  425. * 'ubiblock_remove()'.
  426. */
  427. mutex_lock(&devices_mutex);
  428. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  429. if (!dev) {
  430. mutex_unlock(&devices_mutex);
  431. return -ENODEV;
  432. }
  433. if ((sector_t)disk_capacity != disk_capacity) {
  434. mutex_unlock(&devices_mutex);
  435. dev_warn(disk_to_dev(dev->gd), "the volume is too big (%d LEBs), cannot resize",
  436. vi->size);
  437. return -EFBIG;
  438. }
  439. mutex_lock(&dev->dev_mutex);
  440. if (get_capacity(dev->gd) != disk_capacity) {
  441. set_capacity(dev->gd, disk_capacity);
  442. dev_info(disk_to_dev(dev->gd), "resized to %lld bytes",
  443. vi->used_bytes);
  444. }
  445. mutex_unlock(&dev->dev_mutex);
  446. mutex_unlock(&devices_mutex);
  447. return 0;
  448. }
  449. static int ubiblock_notify(struct notifier_block *nb,
  450. unsigned long notification_type, void *ns_ptr)
  451. {
  452. struct ubi_notification *nt = ns_ptr;
  453. switch (notification_type) {
  454. case UBI_VOLUME_ADDED:
  455. /*
  456. * We want to enforce explicit block device creation for
  457. * volumes, so when a volume is added we do nothing.
  458. */
  459. break;
  460. case UBI_VOLUME_REMOVED:
  461. ubiblock_remove(&nt->vi);
  462. break;
  463. case UBI_VOLUME_RESIZED:
  464. ubiblock_resize(&nt->vi);
  465. break;
  466. case UBI_VOLUME_UPDATED:
  467. /*
  468. * If the volume is static, a content update might mean the
  469. * size (i.e. used_bytes) was also changed.
  470. */
  471. if (nt->vi.vol_type == UBI_STATIC_VOLUME)
  472. ubiblock_resize(&nt->vi);
  473. break;
  474. default:
  475. break;
  476. }
  477. return NOTIFY_OK;
  478. }
  479. static struct notifier_block ubiblock_notifier = {
  480. .notifier_call = ubiblock_notify,
  481. };
  482. static struct ubi_volume_desc * __init
  483. open_volume_desc(const char *name, int ubi_num, int vol_id)
  484. {
  485. if (ubi_num == -1)
  486. /* No ubi num, name must be a vol device path */
  487. return ubi_open_volume_path(name, UBI_READONLY);
  488. else if (vol_id == -1)
  489. /* No vol_id, must be vol_name */
  490. return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
  491. else
  492. return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
  493. }
  494. static int __init ubiblock_create_from_param(void)
  495. {
  496. int i, ret;
  497. struct ubiblock_param *p;
  498. struct ubi_volume_desc *desc;
  499. struct ubi_volume_info vi;
  500. for (i = 0; i < ubiblock_devs; i++) {
  501. p = &ubiblock_param[i];
  502. desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
  503. if (IS_ERR(desc)) {
  504. pr_err("UBI: block: can't open volume, err=%ld\n",
  505. PTR_ERR(desc));
  506. ret = PTR_ERR(desc);
  507. break;
  508. }
  509. ubi_get_volume_info(desc, &vi);
  510. ubi_close_volume(desc);
  511. ret = ubiblock_create(&vi);
  512. if (ret) {
  513. pr_err("UBI: block: can't add '%s' volume, err=%d\n",
  514. vi.name, ret);
  515. break;
  516. }
  517. }
  518. return ret;
  519. }
  520. static void ubiblock_remove_all(void)
  521. {
  522. struct ubiblock *next;
  523. struct ubiblock *dev;
  524. list_for_each_entry_safe(dev, next, &ubiblock_devices, list) {
  525. /* Flush pending work and stop workqueue */
  526. destroy_workqueue(dev->wq);
  527. /* The module is being forcefully removed */
  528. WARN_ON(dev->desc);
  529. /* Remove from device list */
  530. list_del(&dev->list);
  531. ubiblock_cleanup(dev);
  532. kfree(dev);
  533. }
  534. }
  535. int __init ubiblock_init(void)
  536. {
  537. int ret;
  538. ubiblock_major = register_blkdev(0, "ubiblock");
  539. if (ubiblock_major < 0)
  540. return ubiblock_major;
  541. /* Attach block devices from 'block=' module param */
  542. ret = ubiblock_create_from_param();
  543. if (ret)
  544. goto err_remove;
  545. /*
  546. * Block devices are only created upon user requests, so we ignore
  547. * existing volumes.
  548. */
  549. ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
  550. if (ret)
  551. goto err_unreg;
  552. return 0;
  553. err_unreg:
  554. unregister_blkdev(ubiblock_major, "ubiblock");
  555. err_remove:
  556. ubiblock_remove_all();
  557. return ret;
  558. }
  559. void __exit ubiblock_exit(void)
  560. {
  561. ubi_unregister_volume_notifier(&ubiblock_notifier);
  562. ubiblock_remove_all();
  563. unregister_blkdev(ubiblock_major, "ubiblock");
  564. }