mtd_blkdevs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Interface to Linux block layer for MTD 'translation layers'.
  3. *
  4. * Copyright © 2003-2010 David Woodhouse <dwmw2@infradead.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <linux/list.h>
  25. #include <linux/fs.h>
  26. #include <linux/mtd/blktrans.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/blkpg.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/hdreg.h>
  32. #include <linux/mutex.h>
  33. #include <linux/uaccess.h>
  34. #include "mtdcore.h"
  35. static LIST_HEAD(blktrans_majors);
  36. static DEFINE_MUTEX(blktrans_ref_mutex);
  37. static void blktrans_dev_release(struct kref *kref)
  38. {
  39. struct mtd_blktrans_dev *dev =
  40. container_of(kref, struct mtd_blktrans_dev, ref);
  41. dev->disk->private_data = NULL;
  42. blk_cleanup_queue(dev->rq);
  43. put_disk(dev->disk);
  44. list_del(&dev->list);
  45. kfree(dev);
  46. }
  47. static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
  48. {
  49. struct mtd_blktrans_dev *dev;
  50. mutex_lock(&blktrans_ref_mutex);
  51. dev = disk->private_data;
  52. if (!dev)
  53. goto unlock;
  54. kref_get(&dev->ref);
  55. unlock:
  56. mutex_unlock(&blktrans_ref_mutex);
  57. return dev;
  58. }
  59. static void blktrans_dev_put(struct mtd_blktrans_dev *dev)
  60. {
  61. mutex_lock(&blktrans_ref_mutex);
  62. kref_put(&dev->ref, blktrans_dev_release);
  63. mutex_unlock(&blktrans_ref_mutex);
  64. }
  65. static blk_status_t do_blktrans_request(struct mtd_blktrans_ops *tr,
  66. struct mtd_blktrans_dev *dev,
  67. struct request *req)
  68. {
  69. unsigned long block, nsect;
  70. char *buf;
  71. block = blk_rq_pos(req) << 9 >> tr->blkshift;
  72. nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
  73. if (req_op(req) == REQ_OP_FLUSH) {
  74. if (tr->flush(dev))
  75. return BLK_STS_IOERR;
  76. return BLK_STS_OK;
  77. }
  78. if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
  79. get_capacity(req->rq_disk))
  80. return BLK_STS_IOERR;
  81. switch (req_op(req)) {
  82. case REQ_OP_DISCARD:
  83. if (tr->discard(dev, block, nsect))
  84. return BLK_STS_IOERR;
  85. return BLK_STS_OK;
  86. case REQ_OP_READ:
  87. buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
  88. for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
  89. if (tr->readsect(dev, block, buf)) {
  90. kunmap(bio_page(req->bio));
  91. return BLK_STS_IOERR;
  92. }
  93. }
  94. kunmap(bio_page(req->bio));
  95. rq_flush_dcache_pages(req);
  96. return BLK_STS_OK;
  97. case REQ_OP_WRITE:
  98. if (!tr->writesect)
  99. return BLK_STS_IOERR;
  100. rq_flush_dcache_pages(req);
  101. buf = kmap(bio_page(req->bio)) + bio_offset(req->bio);
  102. for (; nsect > 0; nsect--, block++, buf += tr->blksize) {
  103. if (tr->writesect(dev, block, buf)) {
  104. kunmap(bio_page(req->bio));
  105. return BLK_STS_IOERR;
  106. }
  107. }
  108. kunmap(bio_page(req->bio));
  109. return BLK_STS_OK;
  110. default:
  111. return BLK_STS_IOERR;
  112. }
  113. }
  114. int mtd_blktrans_cease_background(struct mtd_blktrans_dev *dev)
  115. {
  116. return dev->bg_stop;
  117. }
  118. EXPORT_SYMBOL_GPL(mtd_blktrans_cease_background);
  119. static void mtd_blktrans_work(struct work_struct *work)
  120. {
  121. struct mtd_blktrans_dev *dev =
  122. container_of(work, struct mtd_blktrans_dev, work);
  123. struct mtd_blktrans_ops *tr = dev->tr;
  124. struct request_queue *rq = dev->rq;
  125. struct request *req = NULL;
  126. int background_done = 0;
  127. spin_lock_irq(rq->queue_lock);
  128. while (1) {
  129. blk_status_t res;
  130. dev->bg_stop = false;
  131. if (!req && !(req = blk_fetch_request(rq))) {
  132. if (tr->background && !background_done) {
  133. spin_unlock_irq(rq->queue_lock);
  134. mutex_lock(&dev->lock);
  135. tr->background(dev);
  136. mutex_unlock(&dev->lock);
  137. spin_lock_irq(rq->queue_lock);
  138. /*
  139. * Do background processing just once per idle
  140. * period.
  141. */
  142. background_done = !dev->bg_stop;
  143. continue;
  144. }
  145. break;
  146. }
  147. spin_unlock_irq(rq->queue_lock);
  148. mutex_lock(&dev->lock);
  149. res = do_blktrans_request(dev->tr, dev, req);
  150. mutex_unlock(&dev->lock);
  151. spin_lock_irq(rq->queue_lock);
  152. if (!__blk_end_request_cur(req, res))
  153. req = NULL;
  154. background_done = 0;
  155. }
  156. spin_unlock_irq(rq->queue_lock);
  157. }
  158. static void mtd_blktrans_request(struct request_queue *rq)
  159. {
  160. struct mtd_blktrans_dev *dev;
  161. struct request *req = NULL;
  162. dev = rq->queuedata;
  163. if (!dev)
  164. while ((req = blk_fetch_request(rq)) != NULL)
  165. __blk_end_request_all(req, BLK_STS_IOERR);
  166. else
  167. queue_work(dev->wq, &dev->work);
  168. }
  169. static int blktrans_open(struct block_device *bdev, fmode_t mode)
  170. {
  171. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  172. int ret = 0;
  173. if (!dev)
  174. return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
  175. mutex_lock(&mtd_table_mutex);
  176. mutex_lock(&dev->lock);
  177. if (dev->open)
  178. goto unlock;
  179. kref_get(&dev->ref);
  180. __module_get(dev->tr->owner);
  181. if (!dev->mtd)
  182. goto unlock;
  183. if (dev->tr->open) {
  184. ret = dev->tr->open(dev);
  185. if (ret)
  186. goto error_put;
  187. }
  188. ret = __get_mtd_device(dev->mtd);
  189. if (ret)
  190. goto error_release;
  191. dev->file_mode = mode;
  192. unlock:
  193. dev->open++;
  194. mutex_unlock(&dev->lock);
  195. mutex_unlock(&mtd_table_mutex);
  196. blktrans_dev_put(dev);
  197. return ret;
  198. error_release:
  199. if (dev->tr->release)
  200. dev->tr->release(dev);
  201. error_put:
  202. module_put(dev->tr->owner);
  203. kref_put(&dev->ref, blktrans_dev_release);
  204. mutex_unlock(&dev->lock);
  205. mutex_unlock(&mtd_table_mutex);
  206. blktrans_dev_put(dev);
  207. return ret;
  208. }
  209. static void blktrans_release(struct gendisk *disk, fmode_t mode)
  210. {
  211. struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
  212. if (!dev)
  213. return;
  214. mutex_lock(&mtd_table_mutex);
  215. mutex_lock(&dev->lock);
  216. if (--dev->open)
  217. goto unlock;
  218. kref_put(&dev->ref, blktrans_dev_release);
  219. module_put(dev->tr->owner);
  220. if (dev->mtd) {
  221. if (dev->tr->release)
  222. dev->tr->release(dev);
  223. __put_mtd_device(dev->mtd);
  224. }
  225. unlock:
  226. mutex_unlock(&dev->lock);
  227. mutex_unlock(&mtd_table_mutex);
  228. blktrans_dev_put(dev);
  229. }
  230. static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  231. {
  232. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  233. int ret = -ENXIO;
  234. if (!dev)
  235. return ret;
  236. mutex_lock(&dev->lock);
  237. if (!dev->mtd)
  238. goto unlock;
  239. ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : -ENOTTY;
  240. unlock:
  241. mutex_unlock(&dev->lock);
  242. blktrans_dev_put(dev);
  243. return ret;
  244. }
  245. static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
  246. unsigned int cmd, unsigned long arg)
  247. {
  248. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  249. int ret = -ENXIO;
  250. if (!dev)
  251. return ret;
  252. mutex_lock(&dev->lock);
  253. if (!dev->mtd)
  254. goto unlock;
  255. switch (cmd) {
  256. case BLKFLSBUF:
  257. ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
  258. break;
  259. default:
  260. ret = -ENOTTY;
  261. }
  262. unlock:
  263. mutex_unlock(&dev->lock);
  264. blktrans_dev_put(dev);
  265. return ret;
  266. }
  267. static const struct block_device_operations mtd_block_ops = {
  268. .owner = THIS_MODULE,
  269. .open = blktrans_open,
  270. .release = blktrans_release,
  271. .ioctl = blktrans_ioctl,
  272. .getgeo = blktrans_getgeo,
  273. };
  274. int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
  275. {
  276. struct mtd_blktrans_ops *tr = new->tr;
  277. struct mtd_blktrans_dev *d;
  278. int last_devnum = -1;
  279. struct gendisk *gd;
  280. int ret;
  281. if (mutex_trylock(&mtd_table_mutex)) {
  282. mutex_unlock(&mtd_table_mutex);
  283. BUG();
  284. }
  285. mutex_lock(&blktrans_ref_mutex);
  286. list_for_each_entry(d, &tr->devs, list) {
  287. if (new->devnum == -1) {
  288. /* Use first free number */
  289. if (d->devnum != last_devnum+1) {
  290. /* Found a free devnum. Plug it in here */
  291. new->devnum = last_devnum+1;
  292. list_add_tail(&new->list, &d->list);
  293. goto added;
  294. }
  295. } else if (d->devnum == new->devnum) {
  296. /* Required number taken */
  297. mutex_unlock(&blktrans_ref_mutex);
  298. return -EBUSY;
  299. } else if (d->devnum > new->devnum) {
  300. /* Required number was free */
  301. list_add_tail(&new->list, &d->list);
  302. goto added;
  303. }
  304. last_devnum = d->devnum;
  305. }
  306. ret = -EBUSY;
  307. if (new->devnum == -1)
  308. new->devnum = last_devnum+1;
  309. /* Check that the device and any partitions will get valid
  310. * minor numbers and that the disk naming code below can cope
  311. * with this number. */
  312. if (new->devnum > (MINORMASK >> tr->part_bits) ||
  313. (tr->part_bits && new->devnum >= 27 * 26)) {
  314. mutex_unlock(&blktrans_ref_mutex);
  315. goto error1;
  316. }
  317. list_add_tail(&new->list, &tr->devs);
  318. added:
  319. mutex_unlock(&blktrans_ref_mutex);
  320. mutex_init(&new->lock);
  321. kref_init(&new->ref);
  322. if (!tr->writesect)
  323. new->readonly = 1;
  324. /* Create gendisk */
  325. ret = -ENOMEM;
  326. gd = alloc_disk(1 << tr->part_bits);
  327. if (!gd)
  328. goto error2;
  329. new->disk = gd;
  330. gd->private_data = new;
  331. gd->major = tr->major;
  332. gd->first_minor = (new->devnum) << tr->part_bits;
  333. gd->fops = &mtd_block_ops;
  334. if (tr->part_bits)
  335. if (new->devnum < 26)
  336. snprintf(gd->disk_name, sizeof(gd->disk_name),
  337. "%s%c", tr->name, 'a' + new->devnum);
  338. else
  339. snprintf(gd->disk_name, sizeof(gd->disk_name),
  340. "%s%c%c", tr->name,
  341. 'a' - 1 + new->devnum / 26,
  342. 'a' + new->devnum % 26);
  343. else
  344. snprintf(gd->disk_name, sizeof(gd->disk_name),
  345. "%s%d", tr->name, new->devnum);
  346. set_capacity(gd, ((u64)new->size * tr->blksize) >> 9);
  347. /* Create the request queue */
  348. spin_lock_init(&new->queue_lock);
  349. new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
  350. if (!new->rq)
  351. goto error3;
  352. if (tr->flush)
  353. blk_queue_write_cache(new->rq, true, false);
  354. new->rq->queuedata = new;
  355. blk_queue_logical_block_size(new->rq, tr->blksize);
  356. blk_queue_flag_set(QUEUE_FLAG_NONROT, new->rq);
  357. blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, new->rq);
  358. if (tr->discard) {
  359. blk_queue_flag_set(QUEUE_FLAG_DISCARD, new->rq);
  360. blk_queue_max_discard_sectors(new->rq, UINT_MAX);
  361. }
  362. gd->queue = new->rq;
  363. /* Create processing workqueue */
  364. new->wq = alloc_workqueue("%s%d", 0, 0,
  365. tr->name, new->mtd->index);
  366. if (!new->wq)
  367. goto error4;
  368. INIT_WORK(&new->work, mtd_blktrans_work);
  369. if (new->readonly)
  370. set_disk_ro(gd, 1);
  371. device_add_disk(&new->mtd->dev, gd);
  372. if (new->disk_attributes) {
  373. ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
  374. new->disk_attributes);
  375. WARN_ON(ret);
  376. }
  377. return 0;
  378. error4:
  379. blk_cleanup_queue(new->rq);
  380. error3:
  381. put_disk(new->disk);
  382. error2:
  383. list_del(&new->list);
  384. error1:
  385. return ret;
  386. }
  387. int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
  388. {
  389. unsigned long flags;
  390. if (mutex_trylock(&mtd_table_mutex)) {
  391. mutex_unlock(&mtd_table_mutex);
  392. BUG();
  393. }
  394. if (old->disk_attributes)
  395. sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
  396. old->disk_attributes);
  397. /* Stop new requests to arrive */
  398. del_gendisk(old->disk);
  399. /* Stop workqueue. This will perform any pending request. */
  400. destroy_workqueue(old->wq);
  401. /* Kill current requests */
  402. spin_lock_irqsave(&old->queue_lock, flags);
  403. old->rq->queuedata = NULL;
  404. blk_start_queue(old->rq);
  405. spin_unlock_irqrestore(&old->queue_lock, flags);
  406. /* If the device is currently open, tell trans driver to close it,
  407. then put mtd device, and don't touch it again */
  408. mutex_lock(&old->lock);
  409. if (old->open) {
  410. if (old->tr->release)
  411. old->tr->release(old);
  412. __put_mtd_device(old->mtd);
  413. }
  414. old->mtd = NULL;
  415. mutex_unlock(&old->lock);
  416. blktrans_dev_put(old);
  417. return 0;
  418. }
  419. static void blktrans_notify_remove(struct mtd_info *mtd)
  420. {
  421. struct mtd_blktrans_ops *tr;
  422. struct mtd_blktrans_dev *dev, *next;
  423. list_for_each_entry(tr, &blktrans_majors, list)
  424. list_for_each_entry_safe(dev, next, &tr->devs, list)
  425. if (dev->mtd == mtd)
  426. tr->remove_dev(dev);
  427. }
  428. static void blktrans_notify_add(struct mtd_info *mtd)
  429. {
  430. struct mtd_blktrans_ops *tr;
  431. if (mtd->type == MTD_ABSENT)
  432. return;
  433. list_for_each_entry(tr, &blktrans_majors, list)
  434. tr->add_mtd(tr, mtd);
  435. }
  436. static struct mtd_notifier blktrans_notifier = {
  437. .add = blktrans_notify_add,
  438. .remove = blktrans_notify_remove,
  439. };
  440. int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
  441. {
  442. struct mtd_info *mtd;
  443. int ret;
  444. /* Register the notifier if/when the first device type is
  445. registered, to prevent the link/init ordering from fucking
  446. us over. */
  447. if (!blktrans_notifier.list.next)
  448. register_mtd_user(&blktrans_notifier);
  449. mutex_lock(&mtd_table_mutex);
  450. ret = register_blkdev(tr->major, tr->name);
  451. if (ret < 0) {
  452. printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
  453. tr->name, tr->major, ret);
  454. mutex_unlock(&mtd_table_mutex);
  455. return ret;
  456. }
  457. if (ret)
  458. tr->major = ret;
  459. tr->blkshift = ffs(tr->blksize) - 1;
  460. INIT_LIST_HEAD(&tr->devs);
  461. list_add(&tr->list, &blktrans_majors);
  462. mtd_for_each_device(mtd)
  463. if (mtd->type != MTD_ABSENT)
  464. tr->add_mtd(tr, mtd);
  465. mutex_unlock(&mtd_table_mutex);
  466. return 0;
  467. }
  468. int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
  469. {
  470. struct mtd_blktrans_dev *dev, *next;
  471. mutex_lock(&mtd_table_mutex);
  472. /* Remove it from the list of active majors */
  473. list_del(&tr->list);
  474. list_for_each_entry_safe(dev, next, &tr->devs, list)
  475. tr->remove_dev(dev);
  476. unregister_blkdev(tr->major, tr->name);
  477. mutex_unlock(&mtd_table_mutex);
  478. BUG_ON(!list_empty(&tr->devs));
  479. return 0;
  480. }
  481. static void __exit mtd_blktrans_exit(void)
  482. {
  483. /* No race here -- if someone's currently in register_mtd_blktrans
  484. we're screwed anyway. */
  485. if (blktrans_notifier.list.next)
  486. unregister_mtd_user(&blktrans_notifier);
  487. }
  488. module_exit(mtd_blktrans_exit);
  489. EXPORT_SYMBOL_GPL(register_mtd_blktrans);
  490. EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
  491. EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
  492. EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
  493. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  494. MODULE_LICENSE("GPL");
  495. MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");