tape_block.c 11 KB

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