mtd_blkdevs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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/init.h>
  33. #include <linux/mutex.h>
  34. #include <linux/kthread.h>
  35. #include <asm/uaccess.h>
  36. #include "mtdcore.h"
  37. static LIST_HEAD(blktrans_majors);
  38. static DEFINE_MUTEX(blktrans_ref_mutex);
  39. static void blktrans_dev_release(struct kref *kref)
  40. {
  41. struct mtd_blktrans_dev *dev =
  42. container_of(kref, struct mtd_blktrans_dev, ref);
  43. dev->disk->private_data = NULL;
  44. blk_cleanup_queue(dev->rq);
  45. put_disk(dev->disk);
  46. list_del(&dev->list);
  47. kfree(dev);
  48. }
  49. static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
  50. {
  51. struct mtd_blktrans_dev *dev;
  52. mutex_lock(&blktrans_ref_mutex);
  53. dev = disk->private_data;
  54. if (!dev)
  55. goto unlock;
  56. kref_get(&dev->ref);
  57. unlock:
  58. mutex_unlock(&blktrans_ref_mutex);
  59. return dev;
  60. }
  61. static void blktrans_dev_put(struct mtd_blktrans_dev *dev)
  62. {
  63. mutex_lock(&blktrans_ref_mutex);
  64. kref_put(&dev->ref, blktrans_dev_release);
  65. mutex_unlock(&blktrans_ref_mutex);
  66. }
  67. static int do_blktrans_request(struct mtd_blktrans_ops *tr,
  68. struct mtd_blktrans_dev *dev,
  69. struct request *req)
  70. {
  71. unsigned long block, nsect;
  72. char *buf;
  73. block = blk_rq_pos(req) << 9 >> tr->blkshift;
  74. nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
  75. buf = req->buffer;
  76. if (req->cmd_type != REQ_TYPE_FS)
  77. return -EIO;
  78. if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
  79. get_capacity(req->rq_disk))
  80. return -EIO;
  81. if (req->cmd_flags & REQ_DISCARD)
  82. return tr->discard(dev, block, nsect);
  83. switch(rq_data_dir(req)) {
  84. case READ:
  85. for (; nsect > 0; nsect--, block++, buf += tr->blksize)
  86. if (tr->readsect(dev, block, buf))
  87. return -EIO;
  88. rq_flush_dcache_pages(req);
  89. return 0;
  90. case WRITE:
  91. if (!tr->writesect)
  92. return -EIO;
  93. rq_flush_dcache_pages(req);
  94. for (; nsect > 0; nsect--, block++, buf += tr->blksize)
  95. if (tr->writesect(dev, block, buf))
  96. return -EIO;
  97. return 0;
  98. default:
  99. printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
  100. return -EIO;
  101. }
  102. }
  103. int mtd_blktrans_cease_background(struct mtd_blktrans_dev *dev)
  104. {
  105. if (kthread_should_stop())
  106. return 1;
  107. return dev->bg_stop;
  108. }
  109. EXPORT_SYMBOL_GPL(mtd_blktrans_cease_background);
  110. static int mtd_blktrans_thread(void *arg)
  111. {
  112. struct mtd_blktrans_dev *dev = arg;
  113. struct mtd_blktrans_ops *tr = dev->tr;
  114. struct request_queue *rq = dev->rq;
  115. struct request *req = NULL;
  116. int background_done = 0;
  117. spin_lock_irq(rq->queue_lock);
  118. while (!kthread_should_stop()) {
  119. int res;
  120. dev->bg_stop = false;
  121. if (!req && !(req = blk_fetch_request(rq))) {
  122. if (tr->background && !background_done) {
  123. spin_unlock_irq(rq->queue_lock);
  124. mutex_lock(&dev->lock);
  125. tr->background(dev);
  126. mutex_unlock(&dev->lock);
  127. spin_lock_irq(rq->queue_lock);
  128. /*
  129. * Do background processing just once per idle
  130. * period.
  131. */
  132. background_done = !dev->bg_stop;
  133. continue;
  134. }
  135. set_current_state(TASK_INTERRUPTIBLE);
  136. if (kthread_should_stop())
  137. set_current_state(TASK_RUNNING);
  138. spin_unlock_irq(rq->queue_lock);
  139. schedule();
  140. spin_lock_irq(rq->queue_lock);
  141. continue;
  142. }
  143. spin_unlock_irq(rq->queue_lock);
  144. mutex_lock(&dev->lock);
  145. res = do_blktrans_request(dev->tr, dev, req);
  146. mutex_unlock(&dev->lock);
  147. spin_lock_irq(rq->queue_lock);
  148. if (!__blk_end_request_cur(req, res))
  149. req = NULL;
  150. background_done = 0;
  151. }
  152. if (req)
  153. __blk_end_request_all(req, -EIO);
  154. spin_unlock_irq(rq->queue_lock);
  155. return 0;
  156. }
  157. static void mtd_blktrans_request(struct request_queue *rq)
  158. {
  159. struct mtd_blktrans_dev *dev;
  160. struct request *req = NULL;
  161. dev = rq->queuedata;
  162. if (!dev)
  163. while ((req = blk_fetch_request(rq)) != NULL)
  164. __blk_end_request_all(req, -ENODEV);
  165. else {
  166. dev->bg_stop = true;
  167. wake_up_process(dev->thread);
  168. }
  169. }
  170. static int blktrans_open(struct block_device *bdev, fmode_t mode)
  171. {
  172. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  173. int ret = 0;
  174. if (!dev)
  175. return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
  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. unlock:
  192. dev->open++;
  193. mutex_unlock(&dev->lock);
  194. blktrans_dev_put(dev);
  195. return ret;
  196. error_release:
  197. if (dev->tr->release)
  198. dev->tr->release(dev);
  199. error_put:
  200. module_put(dev->tr->owner);
  201. kref_put(&dev->ref, blktrans_dev_release);
  202. mutex_unlock(&dev->lock);
  203. blktrans_dev_put(dev);
  204. return ret;
  205. }
  206. static int blktrans_release(struct gendisk *disk, fmode_t mode)
  207. {
  208. struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
  209. int ret = 0;
  210. if (!dev)
  211. return ret;
  212. mutex_lock(&dev->lock);
  213. if (--dev->open)
  214. goto unlock;
  215. kref_put(&dev->ref, blktrans_dev_release);
  216. module_put(dev->tr->owner);
  217. if (dev->mtd) {
  218. ret = dev->tr->release ? dev->tr->release(dev) : 0;
  219. __put_mtd_device(dev->mtd);
  220. }
  221. unlock:
  222. mutex_unlock(&dev->lock);
  223. blktrans_dev_put(dev);
  224. return ret;
  225. }
  226. static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  227. {
  228. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  229. int ret = -ENXIO;
  230. if (!dev)
  231. return ret;
  232. mutex_lock(&dev->lock);
  233. if (!dev->mtd)
  234. goto unlock;
  235. ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0;
  236. unlock:
  237. mutex_unlock(&dev->lock);
  238. blktrans_dev_put(dev);
  239. return ret;
  240. }
  241. static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
  242. unsigned int cmd, unsigned long arg)
  243. {
  244. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  245. int ret = -ENXIO;
  246. if (!dev)
  247. return ret;
  248. mutex_lock(&dev->lock);
  249. if (!dev->mtd)
  250. goto unlock;
  251. switch (cmd) {
  252. case BLKFLSBUF:
  253. ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
  254. break;
  255. default:
  256. ret = -ENOTTY;
  257. }
  258. unlock:
  259. mutex_unlock(&dev->lock);
  260. blktrans_dev_put(dev);
  261. return ret;
  262. }
  263. static const struct block_device_operations mtd_blktrans_ops = {
  264. .owner = THIS_MODULE,
  265. .open = blktrans_open,
  266. .release = blktrans_release,
  267. .ioctl = blktrans_ioctl,
  268. .getgeo = blktrans_getgeo,
  269. };
  270. int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
  271. {
  272. struct mtd_blktrans_ops *tr = new->tr;
  273. struct mtd_blktrans_dev *d;
  274. int last_devnum = -1;
  275. struct gendisk *gd;
  276. int ret;
  277. if (mutex_trylock(&mtd_table_mutex)) {
  278. mutex_unlock(&mtd_table_mutex);
  279. BUG();
  280. }
  281. mutex_lock(&blktrans_ref_mutex);
  282. list_for_each_entry(d, &tr->devs, list) {
  283. if (new->devnum == -1) {
  284. /* Use first free number */
  285. if (d->devnum != last_devnum+1) {
  286. /* Found a free devnum. Plug it in here */
  287. new->devnum = last_devnum+1;
  288. list_add_tail(&new->list, &d->list);
  289. goto added;
  290. }
  291. } else if (d->devnum == new->devnum) {
  292. /* Required number taken */
  293. mutex_unlock(&blktrans_ref_mutex);
  294. return -EBUSY;
  295. } else if (d->devnum > new->devnum) {
  296. /* Required number was free */
  297. list_add_tail(&new->list, &d->list);
  298. goto added;
  299. }
  300. last_devnum = d->devnum;
  301. }
  302. ret = -EBUSY;
  303. if (new->devnum == -1)
  304. new->devnum = last_devnum+1;
  305. /* Check that the device and any partitions will get valid
  306. * minor numbers and that the disk naming code below can cope
  307. * with this number. */
  308. if (new->devnum > (MINORMASK >> tr->part_bits) ||
  309. (tr->part_bits && new->devnum >= 27 * 26)) {
  310. mutex_unlock(&blktrans_ref_mutex);
  311. goto error1;
  312. }
  313. list_add_tail(&new->list, &tr->devs);
  314. added:
  315. mutex_unlock(&blktrans_ref_mutex);
  316. mutex_init(&new->lock);
  317. kref_init(&new->ref);
  318. if (!tr->writesect)
  319. new->readonly = 1;
  320. /* Create gendisk */
  321. ret = -ENOMEM;
  322. gd = alloc_disk(1 << tr->part_bits);
  323. if (!gd)
  324. goto error2;
  325. new->disk = gd;
  326. gd->private_data = new;
  327. gd->major = tr->major;
  328. gd->first_minor = (new->devnum) << tr->part_bits;
  329. gd->fops = &mtd_blktrans_ops;
  330. if (tr->part_bits)
  331. if (new->devnum < 26)
  332. snprintf(gd->disk_name, sizeof(gd->disk_name),
  333. "%s%c", tr->name, 'a' + new->devnum);
  334. else
  335. snprintf(gd->disk_name, sizeof(gd->disk_name),
  336. "%s%c%c", tr->name,
  337. 'a' - 1 + new->devnum / 26,
  338. 'a' + new->devnum % 26);
  339. else
  340. snprintf(gd->disk_name, sizeof(gd->disk_name),
  341. "%s%d", tr->name, new->devnum);
  342. set_capacity(gd, (new->size * tr->blksize) >> 9);
  343. /* Create the request queue */
  344. spin_lock_init(&new->queue_lock);
  345. new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
  346. if (!new->rq)
  347. goto error3;
  348. new->rq->queuedata = new;
  349. blk_queue_logical_block_size(new->rq, tr->blksize);
  350. if (tr->discard) {
  351. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, new->rq);
  352. new->rq->limits.max_discard_sectors = UINT_MAX;
  353. }
  354. gd->queue = new->rq;
  355. /* Create processing thread */
  356. /* TODO: workqueue ? */
  357. new->thread = kthread_run(mtd_blktrans_thread, new,
  358. "%s%d", tr->name, new->mtd->index);
  359. if (IS_ERR(new->thread)) {
  360. ret = PTR_ERR(new->thread);
  361. goto error4;
  362. }
  363. gd->driverfs_dev = &new->mtd->dev;
  364. if (new->readonly)
  365. set_disk_ro(gd, 1);
  366. add_disk(gd);
  367. if (new->disk_attributes) {
  368. ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
  369. new->disk_attributes);
  370. WARN_ON(ret);
  371. }
  372. return 0;
  373. error4:
  374. blk_cleanup_queue(new->rq);
  375. error3:
  376. put_disk(new->disk);
  377. error2:
  378. list_del(&new->list);
  379. error1:
  380. return ret;
  381. }
  382. int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
  383. {
  384. unsigned long flags;
  385. if (mutex_trylock(&mtd_table_mutex)) {
  386. mutex_unlock(&mtd_table_mutex);
  387. BUG();
  388. }
  389. if (old->disk_attributes)
  390. sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
  391. old->disk_attributes);
  392. /* Stop new requests to arrive */
  393. del_gendisk(old->disk);
  394. /* Stop the thread */
  395. kthread_stop(old->thread);
  396. /* Kill current requests */
  397. spin_lock_irqsave(&old->queue_lock, flags);
  398. old->rq->queuedata = NULL;
  399. blk_start_queue(old->rq);
  400. spin_unlock_irqrestore(&old->queue_lock, flags);
  401. /* If the device is currently open, tell trans driver to close it,
  402. then put mtd device, and don't touch it again */
  403. mutex_lock(&old->lock);
  404. if (old->open) {
  405. if (old->tr->release)
  406. old->tr->release(old);
  407. __put_mtd_device(old->mtd);
  408. }
  409. old->mtd = NULL;
  410. mutex_unlock(&old->lock);
  411. blktrans_dev_put(old);
  412. return 0;
  413. }
  414. static void blktrans_notify_remove(struct mtd_info *mtd)
  415. {
  416. struct mtd_blktrans_ops *tr;
  417. struct mtd_blktrans_dev *dev, *next;
  418. list_for_each_entry(tr, &blktrans_majors, list)
  419. list_for_each_entry_safe(dev, next, &tr->devs, list)
  420. if (dev->mtd == mtd)
  421. tr->remove_dev(dev);
  422. }
  423. static void blktrans_notify_add(struct mtd_info *mtd)
  424. {
  425. struct mtd_blktrans_ops *tr;
  426. if (mtd->type == MTD_ABSENT)
  427. return;
  428. list_for_each_entry(tr, &blktrans_majors, list)
  429. tr->add_mtd(tr, mtd);
  430. }
  431. static struct mtd_notifier blktrans_notifier = {
  432. .add = blktrans_notify_add,
  433. .remove = blktrans_notify_remove,
  434. };
  435. int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
  436. {
  437. struct mtd_info *mtd;
  438. int ret;
  439. /* Register the notifier if/when the first device type is
  440. registered, to prevent the link/init ordering from fucking
  441. us over. */
  442. if (!blktrans_notifier.list.next)
  443. register_mtd_user(&blktrans_notifier);
  444. mutex_lock(&mtd_table_mutex);
  445. ret = register_blkdev(tr->major, tr->name);
  446. if (ret < 0) {
  447. printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
  448. tr->name, tr->major, ret);
  449. mutex_unlock(&mtd_table_mutex);
  450. return ret;
  451. }
  452. if (ret)
  453. tr->major = ret;
  454. tr->blkshift = ffs(tr->blksize) - 1;
  455. INIT_LIST_HEAD(&tr->devs);
  456. list_add(&tr->list, &blktrans_majors);
  457. mtd_for_each_device(mtd)
  458. if (mtd->type != MTD_ABSENT)
  459. tr->add_mtd(tr, mtd);
  460. mutex_unlock(&mtd_table_mutex);
  461. return 0;
  462. }
  463. int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
  464. {
  465. struct mtd_blktrans_dev *dev, *next;
  466. mutex_lock(&mtd_table_mutex);
  467. /* Remove it from the list of active majors */
  468. list_del(&tr->list);
  469. list_for_each_entry_safe(dev, next, &tr->devs, list)
  470. tr->remove_dev(dev);
  471. unregister_blkdev(tr->major, tr->name);
  472. mutex_unlock(&mtd_table_mutex);
  473. BUG_ON(!list_empty(&tr->devs));
  474. return 0;
  475. }
  476. static void __exit mtd_blktrans_exit(void)
  477. {
  478. /* No race here -- if someone's currently in register_mtd_blktrans
  479. we're screwed anyway. */
  480. if (blktrans_notifier.list.next)
  481. unregister_mtd_user(&blktrans_notifier);
  482. }
  483. module_exit(mtd_blktrans_exit);
  484. EXPORT_SYMBOL_GPL(register_mtd_blktrans);
  485. EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
  486. EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
  487. EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
  488. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  489. MODULE_LICENSE("GPL");
  490. MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");