mtd_blkdevs.c 13 KB

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