mtd_blkdevs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. mutex_lock(&mtd_table_mutex);
  178. if (dev->open)
  179. goto unlock;
  180. kref_get(&dev->ref);
  181. __module_get(dev->tr->owner);
  182. if (!dev->mtd)
  183. goto unlock;
  184. if (dev->tr->open) {
  185. ret = dev->tr->open(dev);
  186. if (ret)
  187. goto error_put;
  188. }
  189. ret = __get_mtd_device(dev->mtd);
  190. if (ret)
  191. goto error_release;
  192. dev->file_mode = mode;
  193. unlock:
  194. dev->open++;
  195. mutex_unlock(&mtd_table_mutex);
  196. mutex_unlock(&dev->lock);
  197. blktrans_dev_put(dev);
  198. return ret;
  199. error_release:
  200. if (dev->tr->release)
  201. dev->tr->release(dev);
  202. error_put:
  203. module_put(dev->tr->owner);
  204. kref_put(&dev->ref, blktrans_dev_release);
  205. mutex_unlock(&mtd_table_mutex);
  206. mutex_unlock(&dev->lock);
  207. blktrans_dev_put(dev);
  208. return ret;
  209. }
  210. static int blktrans_release(struct gendisk *disk, fmode_t mode)
  211. {
  212. struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
  213. int ret = 0;
  214. if (!dev)
  215. return ret;
  216. mutex_lock(&dev->lock);
  217. mutex_lock(&mtd_table_mutex);
  218. if (--dev->open)
  219. goto unlock;
  220. kref_put(&dev->ref, blktrans_dev_release);
  221. module_put(dev->tr->owner);
  222. if (dev->mtd) {
  223. ret = dev->tr->release ? dev->tr->release(dev) : 0;
  224. __put_mtd_device(dev->mtd);
  225. }
  226. unlock:
  227. mutex_unlock(&mtd_table_mutex);
  228. mutex_unlock(&dev->lock);
  229. blktrans_dev_put(dev);
  230. return ret;
  231. }
  232. static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  233. {
  234. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  235. int ret = -ENXIO;
  236. if (!dev)
  237. return ret;
  238. mutex_lock(&dev->lock);
  239. if (!dev->mtd)
  240. goto unlock;
  241. ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0;
  242. unlock:
  243. mutex_unlock(&dev->lock);
  244. blktrans_dev_put(dev);
  245. return ret;
  246. }
  247. static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
  248. unsigned int cmd, unsigned long arg)
  249. {
  250. struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
  251. int ret = -ENXIO;
  252. if (!dev)
  253. return ret;
  254. mutex_lock(&dev->lock);
  255. if (!dev->mtd)
  256. goto unlock;
  257. switch (cmd) {
  258. case BLKFLSBUF:
  259. ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
  260. break;
  261. default:
  262. ret = -ENOTTY;
  263. }
  264. unlock:
  265. mutex_unlock(&dev->lock);
  266. blktrans_dev_put(dev);
  267. return ret;
  268. }
  269. static const struct block_device_operations mtd_blktrans_ops = {
  270. .owner = THIS_MODULE,
  271. .open = blktrans_open,
  272. .release = blktrans_release,
  273. .ioctl = blktrans_ioctl,
  274. .getgeo = blktrans_getgeo,
  275. };
  276. int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
  277. {
  278. struct mtd_blktrans_ops *tr = new->tr;
  279. struct mtd_blktrans_dev *d;
  280. int last_devnum = -1;
  281. struct gendisk *gd;
  282. int ret;
  283. if (mutex_trylock(&mtd_table_mutex)) {
  284. mutex_unlock(&mtd_table_mutex);
  285. BUG();
  286. }
  287. mutex_lock(&blktrans_ref_mutex);
  288. list_for_each_entry(d, &tr->devs, list) {
  289. if (new->devnum == -1) {
  290. /* Use first free number */
  291. if (d->devnum != last_devnum+1) {
  292. /* Found a free devnum. Plug it in here */
  293. new->devnum = last_devnum+1;
  294. list_add_tail(&new->list, &d->list);
  295. goto added;
  296. }
  297. } else if (d->devnum == new->devnum) {
  298. /* Required number taken */
  299. mutex_unlock(&blktrans_ref_mutex);
  300. return -EBUSY;
  301. } else if (d->devnum > new->devnum) {
  302. /* Required number was free */
  303. list_add_tail(&new->list, &d->list);
  304. goto added;
  305. }
  306. last_devnum = d->devnum;
  307. }
  308. ret = -EBUSY;
  309. if (new->devnum == -1)
  310. new->devnum = last_devnum+1;
  311. /* Check that the device and any partitions will get valid
  312. * minor numbers and that the disk naming code below can cope
  313. * with this number. */
  314. if (new->devnum > (MINORMASK >> tr->part_bits) ||
  315. (tr->part_bits && new->devnum >= 27 * 26)) {
  316. mutex_unlock(&blktrans_ref_mutex);
  317. goto error1;
  318. }
  319. list_add_tail(&new->list, &tr->devs);
  320. added:
  321. mutex_unlock(&blktrans_ref_mutex);
  322. mutex_init(&new->lock);
  323. kref_init(&new->ref);
  324. if (!tr->writesect)
  325. new->readonly = 1;
  326. /* Create gendisk */
  327. ret = -ENOMEM;
  328. gd = alloc_disk(1 << tr->part_bits);
  329. if (!gd)
  330. goto error2;
  331. new->disk = gd;
  332. gd->private_data = new;
  333. gd->major = tr->major;
  334. gd->first_minor = (new->devnum) << tr->part_bits;
  335. gd->fops = &mtd_blktrans_ops;
  336. if (tr->part_bits)
  337. if (new->devnum < 26)
  338. snprintf(gd->disk_name, sizeof(gd->disk_name),
  339. "%s%c", tr->name, 'a' + new->devnum);
  340. else
  341. snprintf(gd->disk_name, sizeof(gd->disk_name),
  342. "%s%c%c", tr->name,
  343. 'a' - 1 + new->devnum / 26,
  344. 'a' + new->devnum % 26);
  345. else
  346. snprintf(gd->disk_name, sizeof(gd->disk_name),
  347. "%s%d", tr->name, new->devnum);
  348. set_capacity(gd, (new->size * tr->blksize) >> 9);
  349. /* Create the request queue */
  350. spin_lock_init(&new->queue_lock);
  351. new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
  352. if (!new->rq)
  353. goto error3;
  354. new->rq->queuedata = new;
  355. /*
  356. * Empirical measurements revealed that read ahead values larger than
  357. * 4 slowed down boot time, so start out with this small value.
  358. */
  359. new->rq->backing_dev_info.ra_pages = (4 * 1024) / PAGE_CACHE_SIZE;
  360. blk_queue_logical_block_size(new->rq, tr->blksize);
  361. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, new->rq);
  362. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, new->rq);
  363. if (tr->discard) {
  364. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, new->rq);
  365. new->rq->limits.max_discard_sectors = UINT_MAX;
  366. }
  367. gd->queue = new->rq;
  368. /* Create processing thread */
  369. /* TODO: workqueue ? */
  370. new->thread = kthread_run(mtd_blktrans_thread, new,
  371. "%s%d", tr->name, new->mtd->index);
  372. if (IS_ERR(new->thread)) {
  373. ret = PTR_ERR(new->thread);
  374. goto error4;
  375. }
  376. gd->driverfs_dev = &new->mtd->dev;
  377. if (new->readonly)
  378. set_disk_ro(gd, 1);
  379. add_disk(gd);
  380. if (new->disk_attributes) {
  381. ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
  382. new->disk_attributes);
  383. WARN_ON(ret);
  384. }
  385. return 0;
  386. error4:
  387. blk_cleanup_queue(new->rq);
  388. error3:
  389. put_disk(new->disk);
  390. error2:
  391. list_del(&new->list);
  392. error1:
  393. return ret;
  394. }
  395. int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
  396. {
  397. unsigned long flags;
  398. if (mutex_trylock(&mtd_table_mutex)) {
  399. mutex_unlock(&mtd_table_mutex);
  400. BUG();
  401. }
  402. if (old->disk_attributes)
  403. sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
  404. old->disk_attributes);
  405. /* Stop new requests to arrive */
  406. del_gendisk(old->disk);
  407. /* Stop the thread */
  408. kthread_stop(old->thread);
  409. /* Kill current requests */
  410. spin_lock_irqsave(&old->queue_lock, flags);
  411. old->rq->queuedata = NULL;
  412. blk_start_queue(old->rq);
  413. spin_unlock_irqrestore(&old->queue_lock, flags);
  414. /* If the device is currently open, tell trans driver to close it,
  415. then put mtd device, and don't touch it again */
  416. mutex_lock(&old->lock);
  417. if (old->open) {
  418. if (old->tr->release)
  419. old->tr->release(old);
  420. __put_mtd_device(old->mtd);
  421. }
  422. old->mtd = NULL;
  423. mutex_unlock(&old->lock);
  424. blktrans_dev_put(old);
  425. return 0;
  426. }
  427. static void blktrans_notify_remove(struct mtd_info *mtd)
  428. {
  429. struct mtd_blktrans_ops *tr;
  430. struct mtd_blktrans_dev *dev, *next;
  431. list_for_each_entry(tr, &blktrans_majors, list)
  432. list_for_each_entry_safe(dev, next, &tr->devs, list)
  433. if (dev->mtd == mtd)
  434. tr->remove_dev(dev);
  435. }
  436. static void blktrans_notify_add(struct mtd_info *mtd)
  437. {
  438. struct mtd_blktrans_ops *tr;
  439. if (mtd->type == MTD_ABSENT)
  440. return;
  441. list_for_each_entry(tr, &blktrans_majors, list)
  442. tr->add_mtd(tr, mtd);
  443. }
  444. static struct mtd_notifier blktrans_notifier = {
  445. .add = blktrans_notify_add,
  446. .remove = blktrans_notify_remove,
  447. };
  448. int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
  449. {
  450. struct mtd_info *mtd;
  451. int ret;
  452. /* Register the notifier if/when the first device type is
  453. registered, to prevent the link/init ordering from fucking
  454. us over. */
  455. if (!blktrans_notifier.list.next)
  456. register_mtd_user(&blktrans_notifier);
  457. mutex_lock(&mtd_table_mutex);
  458. ret = register_blkdev(tr->major, tr->name);
  459. if (ret < 0) {
  460. printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
  461. tr->name, tr->major, ret);
  462. mutex_unlock(&mtd_table_mutex);
  463. return ret;
  464. }
  465. if (ret)
  466. tr->major = ret;
  467. tr->blkshift = ffs(tr->blksize) - 1;
  468. INIT_LIST_HEAD(&tr->devs);
  469. list_add(&tr->list, &blktrans_majors);
  470. mtd_for_each_device(mtd)
  471. if (mtd->type != MTD_ABSENT)
  472. tr->add_mtd(tr, mtd);
  473. mutex_unlock(&mtd_table_mutex);
  474. return 0;
  475. }
  476. int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
  477. {
  478. struct mtd_blktrans_dev *dev, *next;
  479. mutex_lock(&mtd_table_mutex);
  480. /* Remove it from the list of active majors */
  481. list_del(&tr->list);
  482. list_for_each_entry_safe(dev, next, &tr->devs, list)
  483. tr->remove_dev(dev);
  484. unregister_blkdev(tr->major, tr->name);
  485. mutex_unlock(&mtd_table_mutex);
  486. BUG_ON(!list_empty(&tr->devs));
  487. return 0;
  488. }
  489. static void __exit mtd_blktrans_exit(void)
  490. {
  491. /* No race here -- if someone's currently in register_mtd_blktrans
  492. we're screwed anyway. */
  493. if (blktrans_notifier.list.next)
  494. unregister_mtd_user(&blktrans_notifier);
  495. }
  496. module_exit(mtd_blktrans_exit);
  497. EXPORT_SYMBOL_GPL(register_mtd_blktrans);
  498. EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
  499. EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
  500. EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
  501. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  502. MODULE_LICENSE("GPL");
  503. MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");