mtd_blkdevs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/freezer.h>
  17. #include <linux/spinlock.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. struct mtd_blkcore_priv {
  26. struct task_struct *thread;
  27. struct request_queue *rq;
  28. spinlock_t queue_lock;
  29. };
  30. static int do_blktrans_request(struct mtd_blktrans_ops *tr,
  31. struct mtd_blktrans_dev *dev,
  32. struct request *req)
  33. {
  34. unsigned long block, nsect;
  35. char *buf;
  36. block = req->sector << 9 >> tr->blkshift;
  37. nsect = req->current_nr_sectors << 9 >> tr->blkshift;
  38. buf = req->buffer;
  39. if (!blk_fs_request(req))
  40. return 0;
  41. if (req->sector + req->current_nr_sectors > get_capacity(req->rq_disk))
  42. return 0;
  43. switch(rq_data_dir(req)) {
  44. case READ:
  45. for (; nsect > 0; nsect--, block++, buf += tr->blksize)
  46. if (tr->readsect(dev, block, buf))
  47. return 0;
  48. return 1;
  49. case WRITE:
  50. if (!tr->writesect)
  51. return 0;
  52. for (; nsect > 0; nsect--, block++, buf += tr->blksize)
  53. if (tr->writesect(dev, block, buf))
  54. return 0;
  55. return 1;
  56. default:
  57. printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
  58. return 0;
  59. }
  60. }
  61. static int mtd_blktrans_thread(void *arg)
  62. {
  63. struct mtd_blktrans_ops *tr = arg;
  64. struct request_queue *rq = tr->blkcore_priv->rq;
  65. /* we might get involved when memory gets low, so use PF_MEMALLOC */
  66. current->flags |= PF_MEMALLOC;
  67. spin_lock_irq(rq->queue_lock);
  68. while (!kthread_should_stop()) {
  69. struct request *req;
  70. struct mtd_blktrans_dev *dev;
  71. int res = 0;
  72. req = elv_next_request(rq);
  73. if (!req) {
  74. set_current_state(TASK_INTERRUPTIBLE);
  75. spin_unlock_irq(rq->queue_lock);
  76. schedule();
  77. spin_lock_irq(rq->queue_lock);
  78. continue;
  79. }
  80. dev = req->rq_disk->private_data;
  81. tr = dev->tr;
  82. spin_unlock_irq(rq->queue_lock);
  83. mutex_lock(&dev->lock);
  84. res = do_blktrans_request(tr, dev, req);
  85. mutex_unlock(&dev->lock);
  86. spin_lock_irq(rq->queue_lock);
  87. end_request(req, res);
  88. }
  89. spin_unlock_irq(rq->queue_lock);
  90. return 0;
  91. }
  92. static void mtd_blktrans_request(struct request_queue *rq)
  93. {
  94. struct mtd_blktrans_ops *tr = rq->queuedata;
  95. wake_up_process(tr->blkcore_priv->thread);
  96. }
  97. static int blktrans_open(struct inode *i, struct file *f)
  98. {
  99. struct mtd_blktrans_dev *dev;
  100. struct mtd_blktrans_ops *tr;
  101. int ret = -ENODEV;
  102. dev = i->i_bdev->bd_disk->private_data;
  103. tr = dev->tr;
  104. if (!try_module_get(dev->mtd->owner))
  105. goto out;
  106. if (!try_module_get(tr->owner))
  107. goto out_tr;
  108. /* FIXME: Locking. A hot pluggable device can go away
  109. (del_mtd_device can be called for it) without its module
  110. being unloaded. */
  111. dev->mtd->usecount++;
  112. ret = 0;
  113. if (tr->open && (ret = tr->open(dev))) {
  114. dev->mtd->usecount--;
  115. module_put(dev->mtd->owner);
  116. out_tr:
  117. module_put(tr->owner);
  118. }
  119. out:
  120. return ret;
  121. }
  122. static int blktrans_release(struct inode *i, struct file *f)
  123. {
  124. struct mtd_blktrans_dev *dev;
  125. struct mtd_blktrans_ops *tr;
  126. int ret = 0;
  127. dev = i->i_bdev->bd_disk->private_data;
  128. tr = dev->tr;
  129. if (tr->release)
  130. ret = tr->release(dev);
  131. if (!ret) {
  132. dev->mtd->usecount--;
  133. module_put(dev->mtd->owner);
  134. module_put(tr->owner);
  135. }
  136. return ret;
  137. }
  138. static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  139. {
  140. struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data;
  141. if (dev->tr->getgeo)
  142. return dev->tr->getgeo(dev, geo);
  143. return -ENOTTY;
  144. }
  145. static int blktrans_ioctl(struct inode *inode, struct file *file,
  146. unsigned int cmd, unsigned long arg)
  147. {
  148. struct mtd_blktrans_dev *dev = inode->i_bdev->bd_disk->private_data;
  149. struct mtd_blktrans_ops *tr = dev->tr;
  150. switch (cmd) {
  151. case BLKFLSBUF:
  152. if (tr->flush)
  153. return tr->flush(dev);
  154. /* The core code did the work, we had nothing to do. */
  155. return 0;
  156. default:
  157. return -ENOTTY;
  158. }
  159. }
  160. static struct block_device_operations mtd_blktrans_ops = {
  161. .owner = THIS_MODULE,
  162. .open = blktrans_open,
  163. .release = blktrans_release,
  164. .ioctl = blktrans_ioctl,
  165. .getgeo = blktrans_getgeo,
  166. };
  167. int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
  168. {
  169. struct mtd_blktrans_ops *tr = new->tr;
  170. struct mtd_blktrans_dev *d;
  171. int last_devnum = -1;
  172. struct gendisk *gd;
  173. if (mutex_trylock(&mtd_table_mutex)) {
  174. mutex_unlock(&mtd_table_mutex);
  175. BUG();
  176. }
  177. list_for_each_entry(d, &tr->devs, list) {
  178. if (new->devnum == -1) {
  179. /* Use first free number */
  180. if (d->devnum != last_devnum+1) {
  181. /* Found a free devnum. Plug it in here */
  182. new->devnum = last_devnum+1;
  183. list_add_tail(&new->list, &d->list);
  184. goto added;
  185. }
  186. } else if (d->devnum == new->devnum) {
  187. /* Required number taken */
  188. return -EBUSY;
  189. } else if (d->devnum > new->devnum) {
  190. /* Required number was free */
  191. list_add_tail(&new->list, &d->list);
  192. goto added;
  193. }
  194. last_devnum = d->devnum;
  195. }
  196. if (new->devnum == -1)
  197. new->devnum = last_devnum+1;
  198. if ((new->devnum << tr->part_bits) > 256) {
  199. return -EBUSY;
  200. }
  201. list_add_tail(&new->list, &tr->devs);
  202. added:
  203. mutex_init(&new->lock);
  204. if (!tr->writesect)
  205. new->readonly = 1;
  206. gd = alloc_disk(1 << tr->part_bits);
  207. if (!gd) {
  208. list_del(&new->list);
  209. return -ENOMEM;
  210. }
  211. gd->major = tr->major;
  212. gd->first_minor = (new->devnum) << tr->part_bits;
  213. gd->fops = &mtd_blktrans_ops;
  214. if (tr->part_bits)
  215. if (new->devnum < 26)
  216. snprintf(gd->disk_name, sizeof(gd->disk_name),
  217. "%s%c", tr->name, 'a' + new->devnum);
  218. else
  219. snprintf(gd->disk_name, sizeof(gd->disk_name),
  220. "%s%c%c", tr->name,
  221. 'a' - 1 + new->devnum / 26,
  222. 'a' + new->devnum % 26);
  223. else
  224. snprintf(gd->disk_name, sizeof(gd->disk_name),
  225. "%s%d", tr->name, new->devnum);
  226. /* 2.5 has capacity in units of 512 bytes while still
  227. having BLOCK_SIZE_BITS set to 10. Just to keep us amused. */
  228. set_capacity(gd, (new->size * tr->blksize) >> 9);
  229. gd->private_data = new;
  230. new->blkcore_priv = gd;
  231. gd->queue = tr->blkcore_priv->rq;
  232. if (new->readonly)
  233. set_disk_ro(gd, 1);
  234. add_disk(gd);
  235. return 0;
  236. }
  237. int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
  238. {
  239. if (mutex_trylock(&mtd_table_mutex)) {
  240. mutex_unlock(&mtd_table_mutex);
  241. BUG();
  242. }
  243. list_del(&old->list);
  244. del_gendisk(old->blkcore_priv);
  245. put_disk(old->blkcore_priv);
  246. return 0;
  247. }
  248. static void blktrans_notify_remove(struct mtd_info *mtd)
  249. {
  250. struct mtd_blktrans_ops *tr;
  251. struct mtd_blktrans_dev *dev, *next;
  252. list_for_each_entry(tr, &blktrans_majors, list)
  253. list_for_each_entry_safe(dev, next, &tr->devs, list)
  254. if (dev->mtd == mtd)
  255. tr->remove_dev(dev);
  256. }
  257. static void blktrans_notify_add(struct mtd_info *mtd)
  258. {
  259. struct mtd_blktrans_ops *tr;
  260. if (mtd->type == MTD_ABSENT)
  261. return;
  262. list_for_each_entry(tr, &blktrans_majors, list)
  263. tr->add_mtd(tr, mtd);
  264. }
  265. static struct mtd_notifier blktrans_notifier = {
  266. .add = blktrans_notify_add,
  267. .remove = blktrans_notify_remove,
  268. };
  269. int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
  270. {
  271. int ret, i;
  272. /* Register the notifier if/when the first device type is
  273. registered, to prevent the link/init ordering from fucking
  274. us over. */
  275. if (!blktrans_notifier.list.next)
  276. register_mtd_user(&blktrans_notifier);
  277. tr->blkcore_priv = kzalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
  278. if (!tr->blkcore_priv)
  279. return -ENOMEM;
  280. mutex_lock(&mtd_table_mutex);
  281. ret = register_blkdev(tr->major, tr->name);
  282. if (ret) {
  283. printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
  284. tr->name, tr->major, ret);
  285. kfree(tr->blkcore_priv);
  286. mutex_unlock(&mtd_table_mutex);
  287. return ret;
  288. }
  289. spin_lock_init(&tr->blkcore_priv->queue_lock);
  290. tr->blkcore_priv->rq = blk_init_queue(mtd_blktrans_request, &tr->blkcore_priv->queue_lock);
  291. if (!tr->blkcore_priv->rq) {
  292. unregister_blkdev(tr->major, tr->name);
  293. kfree(tr->blkcore_priv);
  294. mutex_unlock(&mtd_table_mutex);
  295. return -ENOMEM;
  296. }
  297. tr->blkcore_priv->rq->queuedata = tr;
  298. blk_queue_hardsect_size(tr->blkcore_priv->rq, tr->blksize);
  299. tr->blkshift = ffs(tr->blksize) - 1;
  300. tr->blkcore_priv->thread = kthread_run(mtd_blktrans_thread, tr,
  301. "%sd", tr->name);
  302. if (IS_ERR(tr->blkcore_priv->thread)) {
  303. blk_cleanup_queue(tr->blkcore_priv->rq);
  304. unregister_blkdev(tr->major, tr->name);
  305. kfree(tr->blkcore_priv);
  306. mutex_unlock(&mtd_table_mutex);
  307. return PTR_ERR(tr->blkcore_priv->thread);
  308. }
  309. INIT_LIST_HEAD(&tr->devs);
  310. list_add(&tr->list, &blktrans_majors);
  311. for (i=0; i<MAX_MTD_DEVICES; i++) {
  312. if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)
  313. tr->add_mtd(tr, mtd_table[i]);
  314. }
  315. mutex_unlock(&mtd_table_mutex);
  316. return 0;
  317. }
  318. int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
  319. {
  320. struct mtd_blktrans_dev *dev, *next;
  321. mutex_lock(&mtd_table_mutex);
  322. /* Clean up the kernel thread */
  323. kthread_stop(tr->blkcore_priv->thread);
  324. /* Remove it from the list of active majors */
  325. list_del(&tr->list);
  326. list_for_each_entry_safe(dev, next, &tr->devs, list)
  327. tr->remove_dev(dev);
  328. blk_cleanup_queue(tr->blkcore_priv->rq);
  329. unregister_blkdev(tr->major, tr->name);
  330. mutex_unlock(&mtd_table_mutex);
  331. kfree(tr->blkcore_priv);
  332. BUG_ON(!list_empty(&tr->devs));
  333. return 0;
  334. }
  335. static void __exit mtd_blktrans_exit(void)
  336. {
  337. /* No race here -- if someone's currently in register_mtd_blktrans
  338. we're screwed anyway. */
  339. if (blktrans_notifier.list.next)
  340. unregister_mtd_user(&blktrans_notifier);
  341. }
  342. module_exit(mtd_blktrans_exit);
  343. EXPORT_SYMBOL_GPL(register_mtd_blktrans);
  344. EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
  345. EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
  346. EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
  347. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  348. MODULE_LICENSE("GPL");
  349. MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");