mtd_blkdevs.c 12 KB

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