tape_block.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * drivers/s390/char/tape_block.c
  3. * block device frontend for tape device driver
  4. *
  5. * S390 and zSeries version
  6. * Copyright (C) 2001,2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Carsten Otte <cotte@de.ibm.com>
  8. * Tuan Ngo-Anh <ngoanh@de.ibm.com>
  9. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  10. * Stefan Bader <shbader@de.ibm.com>
  11. */
  12. #define KMSG_COMPONENT "tape"
  13. #include <linux/fs.h>
  14. #include <linux/module.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/buffer_head.h>
  18. #include <linux/kernel.h>
  19. #include <asm/debug.h>
  20. #define TAPE_DBF_AREA tape_core_dbf
  21. #include "tape.h"
  22. #define TAPEBLOCK_MAX_SEC 100
  23. #define TAPEBLOCK_MIN_REQUEUE 3
  24. /*
  25. * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
  26. *
  27. * In 2.5/2.6 the block device request function is very likely to be called
  28. * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
  29. * just call any function that tries to allocate CCW requests from that con-
  30. * text since it might sleep. There are two choices to work around this:
  31. * a) do not allocate with kmalloc but use its own memory pool
  32. * b) take requests from the queue outside that context, knowing that
  33. * allocation might sleep
  34. */
  35. /*
  36. * file operation structure for tape block frontend
  37. */
  38. static int tapeblock_open(struct block_device *, fmode_t);
  39. static int tapeblock_release(struct gendisk *, fmode_t);
  40. static int tapeblock_ioctl(struct block_device *, fmode_t, unsigned int,
  41. unsigned long);
  42. static int tapeblock_medium_changed(struct gendisk *);
  43. static int tapeblock_revalidate_disk(struct gendisk *);
  44. static struct block_device_operations tapeblock_fops = {
  45. .owner = THIS_MODULE,
  46. .open = tapeblock_open,
  47. .release = tapeblock_release,
  48. .locked_ioctl = tapeblock_ioctl,
  49. .media_changed = tapeblock_medium_changed,
  50. .revalidate_disk = tapeblock_revalidate_disk,
  51. };
  52. static int tapeblock_major = 0;
  53. static void
  54. tapeblock_trigger_requeue(struct tape_device *device)
  55. {
  56. /* Protect against rescheduling. */
  57. if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
  58. return;
  59. schedule_work(&device->blk_data.requeue_task);
  60. }
  61. /*
  62. * Post finished request.
  63. */
  64. static void
  65. __tapeblock_end_request(struct tape_request *ccw_req, void *data)
  66. {
  67. struct tape_device *device;
  68. struct request *req;
  69. DBF_LH(6, "__tapeblock_end_request()\n");
  70. device = ccw_req->device;
  71. req = (struct request *) data;
  72. blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
  73. if (ccw_req->rc == 0)
  74. /* Update position. */
  75. device->blk_data.block_position =
  76. (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
  77. else
  78. /* We lost the position information due to an error. */
  79. device->blk_data.block_position = -1;
  80. device->discipline->free_bread(ccw_req);
  81. if (!list_empty(&device->req_queue) ||
  82. blk_peek_request(device->blk_data.request_queue))
  83. tapeblock_trigger_requeue(device);
  84. }
  85. /*
  86. * Feed the tape device CCW queue with requests supplied in a list.
  87. */
  88. static int
  89. tapeblock_start_request(struct tape_device *device, struct request *req)
  90. {
  91. struct tape_request * ccw_req;
  92. int rc;
  93. DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
  94. ccw_req = device->discipline->bread(device, req);
  95. if (IS_ERR(ccw_req)) {
  96. DBF_EVENT(1, "TBLOCK: bread failed\n");
  97. blk_end_request_all(req, -EIO);
  98. return PTR_ERR(ccw_req);
  99. }
  100. ccw_req->callback = __tapeblock_end_request;
  101. ccw_req->callback_data = (void *) req;
  102. ccw_req->retries = TAPEBLOCK_RETRIES;
  103. rc = tape_do_io_async(device, ccw_req);
  104. if (rc) {
  105. /*
  106. * Start/enqueueing failed. No retries in
  107. * this case.
  108. */
  109. blk_end_request_all(req, -EIO);
  110. device->discipline->free_bread(ccw_req);
  111. }
  112. return rc;
  113. }
  114. /*
  115. * Move requests from the block device request queue to the tape device ccw
  116. * queue.
  117. */
  118. static void
  119. tapeblock_requeue(struct work_struct *work) {
  120. struct tape_blk_data * blkdat;
  121. struct tape_device * device;
  122. struct request_queue * queue;
  123. int nr_queued;
  124. struct request * req;
  125. struct list_head * l;
  126. int rc;
  127. blkdat = container_of(work, struct tape_blk_data, requeue_task);
  128. device = blkdat->device;
  129. if (!device)
  130. return;
  131. spin_lock_irq(get_ccwdev_lock(device->cdev));
  132. queue = device->blk_data.request_queue;
  133. /* Count number of requests on ccw queue. */
  134. nr_queued = 0;
  135. list_for_each(l, &device->req_queue)
  136. nr_queued++;
  137. spin_unlock(get_ccwdev_lock(device->cdev));
  138. spin_lock_irq(&device->blk_data.request_queue_lock);
  139. while (
  140. !blk_queue_plugged(queue) &&
  141. (req = blk_fetch_request(queue)) &&
  142. nr_queued < TAPEBLOCK_MIN_REQUEUE
  143. ) {
  144. if (rq_data_dir(req) == WRITE) {
  145. DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
  146. spin_unlock_irq(&device->blk_data.request_queue_lock);
  147. blk_end_request_all(req, -EIO);
  148. spin_lock_irq(&device->blk_data.request_queue_lock);
  149. continue;
  150. }
  151. nr_queued++;
  152. spin_unlock_irq(&device->blk_data.request_queue_lock);
  153. rc = tapeblock_start_request(device, req);
  154. spin_lock_irq(&device->blk_data.request_queue_lock);
  155. }
  156. spin_unlock_irq(&device->blk_data.request_queue_lock);
  157. atomic_set(&device->blk_data.requeue_scheduled, 0);
  158. }
  159. /*
  160. * Tape request queue function. Called from ll_rw_blk.c
  161. */
  162. static void
  163. tapeblock_request_fn(struct request_queue *queue)
  164. {
  165. struct tape_device *device;
  166. device = (struct tape_device *) queue->queuedata;
  167. DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
  168. BUG_ON(device == NULL);
  169. tapeblock_trigger_requeue(device);
  170. }
  171. /*
  172. * This function is called for every new tapedevice
  173. */
  174. int
  175. tapeblock_setup_device(struct tape_device * device)
  176. {
  177. struct tape_blk_data * blkdat;
  178. struct gendisk * disk;
  179. int rc;
  180. blkdat = &device->blk_data;
  181. blkdat->device = device;
  182. spin_lock_init(&blkdat->request_queue_lock);
  183. atomic_set(&blkdat->requeue_scheduled, 0);
  184. blkdat->request_queue = blk_init_queue(
  185. tapeblock_request_fn,
  186. &blkdat->request_queue_lock
  187. );
  188. if (!blkdat->request_queue)
  189. return -ENOMEM;
  190. elevator_exit(blkdat->request_queue->elevator);
  191. rc = elevator_init(blkdat->request_queue, "noop");
  192. if (rc)
  193. goto cleanup_queue;
  194. blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
  195. blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
  196. blk_queue_max_phys_segments(blkdat->request_queue, -1L);
  197. blk_queue_max_hw_segments(blkdat->request_queue, -1L);
  198. blk_queue_max_segment_size(blkdat->request_queue, -1L);
  199. blk_queue_segment_boundary(blkdat->request_queue, -1L);
  200. disk = alloc_disk(1);
  201. if (!disk) {
  202. rc = -ENOMEM;
  203. goto cleanup_queue;
  204. }
  205. disk->major = tapeblock_major;
  206. disk->first_minor = device->first_minor;
  207. disk->fops = &tapeblock_fops;
  208. disk->private_data = tape_get_device_reference(device);
  209. disk->queue = blkdat->request_queue;
  210. set_capacity(disk, 0);
  211. sprintf(disk->disk_name, "btibm%d",
  212. device->first_minor / TAPE_MINORS_PER_DEV);
  213. blkdat->disk = disk;
  214. blkdat->medium_changed = 1;
  215. blkdat->request_queue->queuedata = tape_get_device_reference(device);
  216. add_disk(disk);
  217. tape_get_device_reference(device);
  218. INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
  219. return 0;
  220. cleanup_queue:
  221. blk_cleanup_queue(blkdat->request_queue);
  222. blkdat->request_queue = NULL;
  223. return rc;
  224. }
  225. void
  226. tapeblock_cleanup_device(struct tape_device *device)
  227. {
  228. flush_scheduled_work();
  229. tape_put_device(device);
  230. if (!device->blk_data.disk) {
  231. goto cleanup_queue;
  232. }
  233. del_gendisk(device->blk_data.disk);
  234. device->blk_data.disk->private_data =
  235. tape_put_device(device->blk_data.disk->private_data);
  236. put_disk(device->blk_data.disk);
  237. device->blk_data.disk = NULL;
  238. cleanup_queue:
  239. device->blk_data.request_queue->queuedata = tape_put_device(device);
  240. blk_cleanup_queue(device->blk_data.request_queue);
  241. device->blk_data.request_queue = NULL;
  242. }
  243. /*
  244. * Detect number of blocks of the tape.
  245. * FIXME: can we extent this to detect the blocks size as well ?
  246. */
  247. static int
  248. tapeblock_revalidate_disk(struct gendisk *disk)
  249. {
  250. struct tape_device * device;
  251. unsigned int nr_of_blks;
  252. int rc;
  253. device = (struct tape_device *) disk->private_data;
  254. BUG_ON(!device);
  255. if (!device->blk_data.medium_changed)
  256. return 0;
  257. dev_info(&device->cdev->dev, "Determining the size of the recorded "
  258. "area...\n");
  259. rc = tape_mtop(device, MTFSFM, 1);
  260. if (rc)
  261. return rc;
  262. rc = tape_mtop(device, MTTELL, 1);
  263. if (rc < 0)
  264. return rc;
  265. DBF_LH(3, "Image file ends at %d\n", rc);
  266. nr_of_blks = rc;
  267. /* This will fail for the first file. Catch the error by checking the
  268. * position. */
  269. tape_mtop(device, MTBSF, 1);
  270. rc = tape_mtop(device, MTTELL, 1);
  271. if (rc < 0)
  272. return rc;
  273. if (rc > nr_of_blks)
  274. return -EINVAL;
  275. DBF_LH(3, "Image file starts at %d\n", rc);
  276. device->bof = rc;
  277. nr_of_blks -= rc;
  278. dev_info(&device->cdev->dev, "The size of the recorded area is %i "
  279. "blocks\n", nr_of_blks);
  280. set_capacity(device->blk_data.disk,
  281. nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
  282. device->blk_data.block_position = 0;
  283. device->blk_data.medium_changed = 0;
  284. return 0;
  285. }
  286. static int
  287. tapeblock_medium_changed(struct gendisk *disk)
  288. {
  289. struct tape_device *device;
  290. device = (struct tape_device *) disk->private_data;
  291. DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
  292. device, device->blk_data.medium_changed);
  293. return device->blk_data.medium_changed;
  294. }
  295. /*
  296. * Block frontend tape device open function.
  297. */
  298. static int
  299. tapeblock_open(struct block_device *bdev, fmode_t mode)
  300. {
  301. struct gendisk * disk = bdev->bd_disk;
  302. struct tape_device * device;
  303. int rc;
  304. device = tape_get_device_reference(disk->private_data);
  305. if (device->required_tapemarks) {
  306. DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
  307. dev_warn(&device->cdev->dev, "Opening the tape failed because"
  308. " of missing end-of-file marks\n");
  309. rc = -EPERM;
  310. goto put_device;
  311. }
  312. rc = tape_open(device);
  313. if (rc)
  314. goto put_device;
  315. rc = tapeblock_revalidate_disk(disk);
  316. if (rc)
  317. goto release;
  318. /*
  319. * Note: The reference to <device> is hold until the release function
  320. * is called.
  321. */
  322. tape_state_set(device, TS_BLKUSE);
  323. return 0;
  324. release:
  325. tape_release(device);
  326. put_device:
  327. tape_put_device(device);
  328. return rc;
  329. }
  330. /*
  331. * Block frontend tape device release function.
  332. *
  333. * Note: One reference to the tape device was made by the open function. So
  334. * we just get the pointer here and release the reference.
  335. */
  336. static int
  337. tapeblock_release(struct gendisk *disk, fmode_t mode)
  338. {
  339. struct tape_device *device = disk->private_data;
  340. tape_state_set(device, TS_IN_USE);
  341. tape_release(device);
  342. tape_put_device(device);
  343. return 0;
  344. }
  345. /*
  346. * Support of some generic block device IOCTLs.
  347. */
  348. static int
  349. tapeblock_ioctl(
  350. struct block_device * bdev,
  351. fmode_t mode,
  352. unsigned int command,
  353. unsigned long arg
  354. ) {
  355. int rc;
  356. int minor;
  357. struct gendisk *disk = bdev->bd_disk;
  358. struct tape_device *device;
  359. rc = 0;
  360. BUG_ON(!disk);
  361. device = disk->private_data;
  362. BUG_ON(!device);
  363. minor = MINOR(bdev->bd_dev);
  364. DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command);
  365. DBF_LH(6, "device = %d:%d\n", tapeblock_major, minor);
  366. switch (command) {
  367. /* Refuse some IOCTL calls without complaining (mount). */
  368. case 0x5310: /* CDROMMULTISESSION */
  369. rc = -EINVAL;
  370. break;
  371. default:
  372. rc = -EINVAL;
  373. }
  374. return rc;
  375. }
  376. /*
  377. * Initialize block device frontend.
  378. */
  379. int
  380. tapeblock_init(void)
  381. {
  382. int rc;
  383. /* Register the tape major number to the kernel */
  384. rc = register_blkdev(tapeblock_major, "tBLK");
  385. if (rc < 0)
  386. return rc;
  387. if (tapeblock_major == 0)
  388. tapeblock_major = rc;
  389. return 0;
  390. }
  391. /*
  392. * Deregister major for block device frontend
  393. */
  394. void
  395. tapeblock_exit(void)
  396. {
  397. unregister_blkdev(tapeblock_major, "tBLK");
  398. }