block.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. ubi_warn("block: empty 'block=' parameter - ignored\n");
  98. return 0;
  99. }
  100. if (len == UBIBLOCK_PARAM_LEN) {
  101. ubi_err("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. ubi_err("%s: error %d while reading from LEB %d (offset %d, "
  161. "length %d)", dev->gd->disk_name, ret, leb, offset,
  162. len);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. static int ubiblock_read(struct ubiblock *dev, char *buffer,
  168. sector_t sec, int len)
  169. {
  170. int ret, leb, offset;
  171. int bytes_left = len;
  172. int to_read = len;
  173. u64 pos = sec << 9;
  174. /* Get LEB:offset address to read from */
  175. offset = do_div(pos, dev->leb_size);
  176. leb = pos;
  177. while (bytes_left) {
  178. /*
  179. * We can only read one LEB at a time. Therefore if the read
  180. * length is larger than one LEB size, we split the operation.
  181. */
  182. if (offset + to_read > dev->leb_size)
  183. to_read = dev->leb_size - offset;
  184. ret = ubiblock_read_to_buf(dev, buffer, leb, offset, to_read);
  185. if (ret)
  186. return ret;
  187. buffer += to_read;
  188. bytes_left -= to_read;
  189. to_read = bytes_left;
  190. leb += 1;
  191. offset = 0;
  192. }
  193. return 0;
  194. }
  195. static int do_ubiblock_request(struct ubiblock *dev, struct request *req)
  196. {
  197. int len, ret;
  198. sector_t sec;
  199. if (req->cmd_type != REQ_TYPE_FS)
  200. return -EIO;
  201. if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
  202. get_capacity(req->rq_disk))
  203. return -EIO;
  204. if (rq_data_dir(req) != READ)
  205. return -ENOSYS; /* Write not implemented */
  206. sec = blk_rq_pos(req);
  207. len = blk_rq_cur_bytes(req);
  208. /*
  209. * Let's prevent the device from being removed while we're doing I/O
  210. * work. Notice that this means we serialize all the I/O operations,
  211. * but it's probably of no impact given the NAND core serializes
  212. * flash access anyway.
  213. */
  214. mutex_lock(&dev->dev_mutex);
  215. ret = ubiblock_read(dev, bio_data(req->bio), sec, len);
  216. mutex_unlock(&dev->dev_mutex);
  217. return ret;
  218. }
  219. static void ubiblock_do_work(struct work_struct *work)
  220. {
  221. struct ubiblock *dev =
  222. container_of(work, struct ubiblock, work);
  223. struct request_queue *rq = dev->rq;
  224. struct request *req;
  225. int res;
  226. spin_lock_irq(rq->queue_lock);
  227. req = blk_fetch_request(rq);
  228. while (req) {
  229. spin_unlock_irq(rq->queue_lock);
  230. res = do_ubiblock_request(dev, req);
  231. spin_lock_irq(rq->queue_lock);
  232. /*
  233. * If we're done with this request,
  234. * we need to fetch a new one
  235. */
  236. if (!__blk_end_request_cur(req, res))
  237. req = blk_fetch_request(rq);
  238. }
  239. spin_unlock_irq(rq->queue_lock);
  240. }
  241. static void ubiblock_request(struct request_queue *rq)
  242. {
  243. struct ubiblock *dev;
  244. struct request *req;
  245. dev = rq->queuedata;
  246. if (!dev)
  247. while ((req = blk_fetch_request(rq)) != NULL)
  248. __blk_end_request_all(req, -ENODEV);
  249. else
  250. queue_work(dev->wq, &dev->work);
  251. }
  252. static int ubiblock_open(struct block_device *bdev, fmode_t mode)
  253. {
  254. struct ubiblock *dev = bdev->bd_disk->private_data;
  255. int ret;
  256. mutex_lock(&dev->dev_mutex);
  257. if (dev->refcnt > 0) {
  258. /*
  259. * The volume is already open, just increase the reference
  260. * counter.
  261. */
  262. goto out_done;
  263. }
  264. /*
  265. * We want users to be aware they should only mount us as read-only.
  266. * It's just a paranoid check, as write requests will get rejected
  267. * in any case.
  268. */
  269. if (mode & FMODE_WRITE) {
  270. ret = -EPERM;
  271. goto out_unlock;
  272. }
  273. dev->desc = ubi_open_volume(dev->ubi_num, dev->vol_id, UBI_READONLY);
  274. if (IS_ERR(dev->desc)) {
  275. ubi_err("%s failed to open ubi volume %d_%d",
  276. dev->gd->disk_name, dev->ubi_num, dev->vol_id);
  277. ret = PTR_ERR(dev->desc);
  278. dev->desc = NULL;
  279. goto out_unlock;
  280. }
  281. out_done:
  282. dev->refcnt++;
  283. mutex_unlock(&dev->dev_mutex);
  284. return 0;
  285. out_unlock:
  286. mutex_unlock(&dev->dev_mutex);
  287. return ret;
  288. }
  289. static void ubiblock_release(struct gendisk *gd, fmode_t mode)
  290. {
  291. struct ubiblock *dev = gd->private_data;
  292. mutex_lock(&dev->dev_mutex);
  293. dev->refcnt--;
  294. if (dev->refcnt == 0) {
  295. ubi_close_volume(dev->desc);
  296. dev->desc = NULL;
  297. }
  298. mutex_unlock(&dev->dev_mutex);
  299. }
  300. static int ubiblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  301. {
  302. /* Some tools might require this information */
  303. geo->heads = 1;
  304. geo->cylinders = 1;
  305. geo->sectors = get_capacity(bdev->bd_disk);
  306. geo->start = 0;
  307. return 0;
  308. }
  309. static const struct block_device_operations ubiblock_ops = {
  310. .owner = THIS_MODULE,
  311. .open = ubiblock_open,
  312. .release = ubiblock_release,
  313. .getgeo = ubiblock_getgeo,
  314. };
  315. int ubiblock_create(struct ubi_volume_info *vi)
  316. {
  317. struct ubiblock *dev;
  318. struct gendisk *gd;
  319. u64 disk_capacity = vi->used_bytes >> 9;
  320. int ret;
  321. if ((sector_t)disk_capacity != disk_capacity)
  322. return -EFBIG;
  323. /* Check that the volume isn't already handled */
  324. mutex_lock(&devices_mutex);
  325. if (find_dev_nolock(vi->ubi_num, vi->vol_id)) {
  326. mutex_unlock(&devices_mutex);
  327. return -EEXIST;
  328. }
  329. mutex_unlock(&devices_mutex);
  330. dev = kzalloc(sizeof(struct ubiblock), GFP_KERNEL);
  331. if (!dev)
  332. return -ENOMEM;
  333. mutex_init(&dev->dev_mutex);
  334. dev->ubi_num = vi->ubi_num;
  335. dev->vol_id = vi->vol_id;
  336. dev->leb_size = vi->usable_leb_size;
  337. /* Initialize the gendisk of this ubiblock device */
  338. gd = alloc_disk(1);
  339. if (!gd) {
  340. ubi_err("block: alloc_disk failed");
  341. ret = -ENODEV;
  342. goto out_free_dev;
  343. }
  344. gd->fops = &ubiblock_ops;
  345. gd->major = ubiblock_major;
  346. gd->first_minor = dev->ubi_num * UBI_MAX_VOLUMES + dev->vol_id;
  347. gd->private_data = dev;
  348. sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
  349. set_capacity(gd, disk_capacity);
  350. dev->gd = gd;
  351. spin_lock_init(&dev->queue_lock);
  352. dev->rq = blk_init_queue(ubiblock_request, &dev->queue_lock);
  353. if (!dev->rq) {
  354. ubi_err("block: blk_init_queue failed");
  355. ret = -ENODEV;
  356. goto out_put_disk;
  357. }
  358. dev->rq->queuedata = dev;
  359. dev->gd->queue = dev->rq;
  360. /*
  361. * Create one workqueue per volume (per registered block device).
  362. * Rembember workqueues are cheap, they're not threads.
  363. */
  364. dev->wq = alloc_workqueue("%s", 0, 0, gd->disk_name);
  365. if (!dev->wq) {
  366. ret = -ENOMEM;
  367. goto out_free_queue;
  368. }
  369. INIT_WORK(&dev->work, ubiblock_do_work);
  370. mutex_lock(&devices_mutex);
  371. list_add_tail(&dev->list, &ubiblock_devices);
  372. mutex_unlock(&devices_mutex);
  373. /* Must be the last step: anyone can call file ops from now on */
  374. add_disk(dev->gd);
  375. ubi_msg("%s created from ubi%d:%d(%s)",
  376. dev->gd->disk_name, dev->ubi_num, dev->vol_id, vi->name);
  377. return 0;
  378. out_free_queue:
  379. blk_cleanup_queue(dev->rq);
  380. out_put_disk:
  381. put_disk(dev->gd);
  382. out_free_dev:
  383. kfree(dev);
  384. return ret;
  385. }
  386. static void ubiblock_cleanup(struct ubiblock *dev)
  387. {
  388. del_gendisk(dev->gd);
  389. blk_cleanup_queue(dev->rq);
  390. ubi_msg("%s released", dev->gd->disk_name);
  391. put_disk(dev->gd);
  392. }
  393. int ubiblock_remove(struct ubi_volume_info *vi)
  394. {
  395. struct ubiblock *dev;
  396. mutex_lock(&devices_mutex);
  397. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  398. if (!dev) {
  399. mutex_unlock(&devices_mutex);
  400. return -ENODEV;
  401. }
  402. /* Found a device, let's lock it so we can check if it's busy */
  403. mutex_lock(&dev->dev_mutex);
  404. if (dev->refcnt > 0) {
  405. mutex_unlock(&dev->dev_mutex);
  406. mutex_unlock(&devices_mutex);
  407. return -EBUSY;
  408. }
  409. /* Remove from device list */
  410. list_del(&dev->list);
  411. mutex_unlock(&devices_mutex);
  412. /* Flush pending work and stop this workqueue */
  413. destroy_workqueue(dev->wq);
  414. ubiblock_cleanup(dev);
  415. mutex_unlock(&dev->dev_mutex);
  416. kfree(dev);
  417. return 0;
  418. }
  419. static int ubiblock_resize(struct ubi_volume_info *vi)
  420. {
  421. struct ubiblock *dev;
  422. u64 disk_capacity = vi->used_bytes >> 9;
  423. /*
  424. * Need to lock the device list until we stop using the device,
  425. * otherwise the device struct might get released in
  426. * 'ubiblock_remove()'.
  427. */
  428. mutex_lock(&devices_mutex);
  429. dev = find_dev_nolock(vi->ubi_num, vi->vol_id);
  430. if (!dev) {
  431. mutex_unlock(&devices_mutex);
  432. return -ENODEV;
  433. }
  434. if ((sector_t)disk_capacity != disk_capacity) {
  435. mutex_unlock(&devices_mutex);
  436. ubi_warn("%s: the volume is too big (%d LEBs), cannot resize",
  437. dev->gd->disk_name, vi->size);
  438. return -EFBIG;
  439. }
  440. mutex_lock(&dev->dev_mutex);
  441. if (get_capacity(dev->gd) != disk_capacity) {
  442. set_capacity(dev->gd, disk_capacity);
  443. ubi_msg("%s resized to %lld bytes", dev->gd->disk_name,
  444. vi->used_bytes);
  445. }
  446. mutex_unlock(&dev->dev_mutex);
  447. mutex_unlock(&devices_mutex);
  448. return 0;
  449. }
  450. static int ubiblock_notify(struct notifier_block *nb,
  451. unsigned long notification_type, void *ns_ptr)
  452. {
  453. struct ubi_notification *nt = ns_ptr;
  454. switch (notification_type) {
  455. case UBI_VOLUME_ADDED:
  456. /*
  457. * We want to enforce explicit block device creation for
  458. * volumes, so when a volume is added we do nothing.
  459. */
  460. break;
  461. case UBI_VOLUME_REMOVED:
  462. ubiblock_remove(&nt->vi);
  463. break;
  464. case UBI_VOLUME_RESIZED:
  465. ubiblock_resize(&nt->vi);
  466. break;
  467. case UBI_VOLUME_UPDATED:
  468. /*
  469. * If the volume is static, a content update might mean the
  470. * size (i.e. used_bytes) was also changed.
  471. */
  472. if (nt->vi.vol_type == UBI_STATIC_VOLUME)
  473. ubiblock_resize(&nt->vi);
  474. break;
  475. default:
  476. break;
  477. }
  478. return NOTIFY_OK;
  479. }
  480. static struct notifier_block ubiblock_notifier = {
  481. .notifier_call = ubiblock_notify,
  482. };
  483. static struct ubi_volume_desc * __init
  484. open_volume_desc(const char *name, int ubi_num, int vol_id)
  485. {
  486. if (ubi_num == -1)
  487. /* No ubi num, name must be a vol device path */
  488. return ubi_open_volume_path(name, UBI_READONLY);
  489. else if (vol_id == -1)
  490. /* No vol_id, must be vol_name */
  491. return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
  492. else
  493. return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
  494. }
  495. static int __init ubiblock_create_from_param(void)
  496. {
  497. int i, ret;
  498. struct ubiblock_param *p;
  499. struct ubi_volume_desc *desc;
  500. struct ubi_volume_info vi;
  501. for (i = 0; i < ubiblock_devs; i++) {
  502. p = &ubiblock_param[i];
  503. desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
  504. if (IS_ERR(desc)) {
  505. ubi_err("block: can't open volume, err=%ld\n",
  506. PTR_ERR(desc));
  507. ret = PTR_ERR(desc);
  508. break;
  509. }
  510. ubi_get_volume_info(desc, &vi);
  511. ubi_close_volume(desc);
  512. ret = ubiblock_create(&vi);
  513. if (ret) {
  514. ubi_err("block: can't add '%s' volume, err=%d\n",
  515. vi.name, ret);
  516. break;
  517. }
  518. }
  519. return ret;
  520. }
  521. static void ubiblock_remove_all(void)
  522. {
  523. struct ubiblock *next;
  524. struct ubiblock *dev;
  525. list_for_each_entry_safe(dev, next, &ubiblock_devices, list) {
  526. /* Flush pending work and stop workqueue */
  527. destroy_workqueue(dev->wq);
  528. /* The module is being forcefully removed */
  529. WARN_ON(dev->desc);
  530. /* Remove from device list */
  531. list_del(&dev->list);
  532. ubiblock_cleanup(dev);
  533. kfree(dev);
  534. }
  535. }
  536. int __init ubiblock_init(void)
  537. {
  538. int ret;
  539. ubiblock_major = register_blkdev(0, "ubiblock");
  540. if (ubiblock_major < 0)
  541. return ubiblock_major;
  542. /* Attach block devices from 'block=' module param */
  543. ret = ubiblock_create_from_param();
  544. if (ret)
  545. goto err_remove;
  546. /*
  547. * Block devices are only created upon user requests, so we ignore
  548. * existing volumes.
  549. */
  550. ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
  551. if (ret)
  552. goto err_unreg;
  553. return 0;
  554. err_unreg:
  555. unregister_blkdev(ubiblock_major, "ubiblock");
  556. err_remove:
  557. ubiblock_remove_all();
  558. return ret;
  559. }
  560. void __exit ubiblock_exit(void)
  561. {
  562. ubi_unregister_volume_notifier(&ubiblock_notifier);
  563. ubiblock_remove_all();
  564. unregister_blkdev(ubiblock_major, "ubiblock");
  565. }