tape_block.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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_medium_changed(struct gendisk *);
  42. static int tapeblock_revalidate_disk(struct gendisk *);
  43. static const struct block_device_operations tapeblock_fops = {
  44. .owner = THIS_MODULE,
  45. .open = tapeblock_open,
  46. .release = tapeblock_release,
  47. .media_changed = tapeblock_medium_changed,
  48. .revalidate_disk = tapeblock_revalidate_disk,
  49. };
  50. static int tapeblock_major = 0;
  51. static void
  52. tapeblock_trigger_requeue(struct tape_device *device)
  53. {
  54. /* Protect against rescheduling. */
  55. if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
  56. return;
  57. schedule_work(&device->blk_data.requeue_task);
  58. }
  59. /*
  60. * Post finished request.
  61. */
  62. static void
  63. __tapeblock_end_request(struct tape_request *ccw_req, void *data)
  64. {
  65. struct tape_device *device;
  66. struct request *req;
  67. DBF_LH(6, "__tapeblock_end_request()\n");
  68. device = ccw_req->device;
  69. req = (struct request *) data;
  70. blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
  71. if (ccw_req->rc == 0)
  72. /* Update position. */
  73. device->blk_data.block_position =
  74. (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
  75. else
  76. /* We lost the position information due to an error. */
  77. device->blk_data.block_position = -1;
  78. device->discipline->free_bread(ccw_req);
  79. if (!list_empty(&device->req_queue) ||
  80. blk_peek_request(device->blk_data.request_queue))
  81. tapeblock_trigger_requeue(device);
  82. }
  83. /*
  84. * Feed the tape device CCW queue with requests supplied in a list.
  85. */
  86. static int
  87. tapeblock_start_request(struct tape_device *device, struct request *req)
  88. {
  89. struct tape_request * ccw_req;
  90. int rc;
  91. DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
  92. ccw_req = device->discipline->bread(device, req);
  93. if (IS_ERR(ccw_req)) {
  94. DBF_EVENT(1, "TBLOCK: bread failed\n");
  95. blk_end_request_all(req, -EIO);
  96. return PTR_ERR(ccw_req);
  97. }
  98. ccw_req->callback = __tapeblock_end_request;
  99. ccw_req->callback_data = (void *) req;
  100. ccw_req->retries = TAPEBLOCK_RETRIES;
  101. rc = tape_do_io_async(device, ccw_req);
  102. if (rc) {
  103. /*
  104. * Start/enqueueing failed. No retries in
  105. * this case.
  106. */
  107. blk_end_request_all(req, -EIO);
  108. device->discipline->free_bread(ccw_req);
  109. }
  110. return rc;
  111. }
  112. /*
  113. * Move requests from the block device request queue to the tape device ccw
  114. * queue.
  115. */
  116. static void
  117. tapeblock_requeue(struct work_struct *work) {
  118. struct tape_blk_data * blkdat;
  119. struct tape_device * device;
  120. struct request_queue * queue;
  121. int nr_queued;
  122. struct request * req;
  123. struct list_head * l;
  124. int rc;
  125. blkdat = container_of(work, struct tape_blk_data, requeue_task);
  126. device = blkdat->device;
  127. if (!device)
  128. return;
  129. spin_lock_irq(get_ccwdev_lock(device->cdev));
  130. queue = device->blk_data.request_queue;
  131. /* Count number of requests on ccw queue. */
  132. nr_queued = 0;
  133. list_for_each(l, &device->req_queue)
  134. nr_queued++;
  135. spin_unlock(get_ccwdev_lock(device->cdev));
  136. spin_lock_irq(&device->blk_data.request_queue_lock);
  137. while (
  138. !blk_queue_plugged(queue) &&
  139. blk_peek_request(queue) &&
  140. nr_queued < TAPEBLOCK_MIN_REQUEUE
  141. ) {
  142. req = blk_fetch_request(queue);
  143. if (rq_data_dir(req) == WRITE) {
  144. DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
  145. spin_unlock_irq(&device->blk_data.request_queue_lock);
  146. blk_end_request_all(req, -EIO);
  147. spin_lock_irq(&device->blk_data.request_queue_lock);
  148. continue;
  149. }
  150. nr_queued++;
  151. spin_unlock_irq(&device->blk_data.request_queue_lock);
  152. rc = tapeblock_start_request(device, req);
  153. spin_lock_irq(&device->blk_data.request_queue_lock);
  154. }
  155. spin_unlock_irq(&device->blk_data.request_queue_lock);
  156. atomic_set(&device->blk_data.requeue_scheduled, 0);
  157. }
  158. /*
  159. * Tape request queue function. Called from ll_rw_blk.c
  160. */
  161. static void
  162. tapeblock_request_fn(struct request_queue *queue)
  163. {
  164. struct tape_device *device;
  165. device = (struct tape_device *) queue->queuedata;
  166. DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
  167. BUG_ON(device == NULL);
  168. tapeblock_trigger_requeue(device);
  169. }
  170. /*
  171. * This function is called for every new tapedevice
  172. */
  173. int
  174. tapeblock_setup_device(struct tape_device * device)
  175. {
  176. struct tape_blk_data * blkdat;
  177. struct gendisk * disk;
  178. int rc;
  179. blkdat = &device->blk_data;
  180. blkdat->device = device;
  181. spin_lock_init(&blkdat->request_queue_lock);
  182. atomic_set(&blkdat->requeue_scheduled, 0);
  183. blkdat->request_queue = blk_init_queue(
  184. tapeblock_request_fn,
  185. &blkdat->request_queue_lock
  186. );
  187. if (!blkdat->request_queue)
  188. return -ENOMEM;
  189. elevator_exit(blkdat->request_queue->elevator);
  190. rc = elevator_init(blkdat->request_queue, "noop");
  191. if (rc)
  192. goto cleanup_queue;
  193. blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
  194. blk_queue_max_hw_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
  195. blk_queue_max_segments(blkdat->request_queue, -1L);
  196. blk_queue_max_segment_size(blkdat->request_queue, -1L);
  197. blk_queue_segment_boundary(blkdat->request_queue, -1L);
  198. disk = alloc_disk(1);
  199. if (!disk) {
  200. rc = -ENOMEM;
  201. goto cleanup_queue;
  202. }
  203. disk->major = tapeblock_major;
  204. disk->first_minor = device->first_minor;
  205. disk->fops = &tapeblock_fops;
  206. disk->private_data = tape_get_device(device);
  207. disk->queue = blkdat->request_queue;
  208. set_capacity(disk, 0);
  209. sprintf(disk->disk_name, "btibm%d",
  210. device->first_minor / TAPE_MINORS_PER_DEV);
  211. blkdat->disk = disk;
  212. blkdat->medium_changed = 1;
  213. blkdat->request_queue->queuedata = tape_get_device(device);
  214. add_disk(disk);
  215. tape_get_device(device);
  216. INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
  217. return 0;
  218. cleanup_queue:
  219. blk_cleanup_queue(blkdat->request_queue);
  220. blkdat->request_queue = NULL;
  221. return rc;
  222. }
  223. void
  224. tapeblock_cleanup_device(struct tape_device *device)
  225. {
  226. flush_scheduled_work();
  227. tape_put_device(device);
  228. if (!device->blk_data.disk) {
  229. goto cleanup_queue;
  230. }
  231. del_gendisk(device->blk_data.disk);
  232. device->blk_data.disk->private_data = NULL;
  233. tape_put_device(device);
  234. put_disk(device->blk_data.disk);
  235. device->blk_data.disk = NULL;
  236. cleanup_queue:
  237. device->blk_data.request_queue->queuedata = NULL;
  238. tape_put_device(device);
  239. blk_cleanup_queue(device->blk_data.request_queue);
  240. device->blk_data.request_queue = NULL;
  241. }
  242. /*
  243. * Detect number of blocks of the tape.
  244. * FIXME: can we extent this to detect the blocks size as well ?
  245. */
  246. static int
  247. tapeblock_revalidate_disk(struct gendisk *disk)
  248. {
  249. struct tape_device * device;
  250. unsigned int nr_of_blks;
  251. int rc;
  252. device = (struct tape_device *) disk->private_data;
  253. BUG_ON(!device);
  254. if (!device->blk_data.medium_changed)
  255. return 0;
  256. rc = tape_mtop(device, MTFSFM, 1);
  257. if (rc)
  258. return rc;
  259. rc = tape_mtop(device, MTTELL, 1);
  260. if (rc < 0)
  261. return rc;
  262. pr_info("%s: Determining the size of the recorded area...\n",
  263. dev_name(&device->cdev->dev));
  264. DBF_LH(3, "Image file ends at %d\n", rc);
  265. nr_of_blks = rc;
  266. /* This will fail for the first file. Catch the error by checking the
  267. * position. */
  268. tape_mtop(device, MTBSF, 1);
  269. rc = tape_mtop(device, MTTELL, 1);
  270. if (rc < 0)
  271. return rc;
  272. if (rc > nr_of_blks)
  273. return -EINVAL;
  274. DBF_LH(3, "Image file starts at %d\n", rc);
  275. device->bof = rc;
  276. nr_of_blks -= rc;
  277. pr_info("%s: The size of the recorded area is %i blocks\n",
  278. dev_name(&device->cdev->dev), nr_of_blks);
  279. set_capacity(device->blk_data.disk,
  280. nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
  281. device->blk_data.block_position = 0;
  282. device->blk_data.medium_changed = 0;
  283. return 0;
  284. }
  285. static int
  286. tapeblock_medium_changed(struct gendisk *disk)
  287. {
  288. struct tape_device *device;
  289. device = (struct tape_device *) disk->private_data;
  290. DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
  291. device, device->blk_data.medium_changed);
  292. return device->blk_data.medium_changed;
  293. }
  294. /*
  295. * Block frontend tape device open function.
  296. */
  297. static int
  298. tapeblock_open(struct block_device *bdev, fmode_t mode)
  299. {
  300. struct gendisk * disk = bdev->bd_disk;
  301. struct tape_device * device;
  302. int rc;
  303. device = tape_get_device(disk->private_data);
  304. if (device->required_tapemarks) {
  305. DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
  306. pr_warning("%s: Opening the tape failed because of missing "
  307. "end-of-file marks\n", dev_name(&device->cdev->dev));
  308. rc = -EPERM;
  309. goto put_device;
  310. }
  311. rc = tape_open(device);
  312. if (rc)
  313. goto put_device;
  314. rc = tapeblock_revalidate_disk(disk);
  315. if (rc)
  316. goto release;
  317. /*
  318. * Note: The reference to <device> is hold until the release function
  319. * is called.
  320. */
  321. tape_state_set(device, TS_BLKUSE);
  322. return 0;
  323. release:
  324. tape_release(device);
  325. put_device:
  326. tape_put_device(device);
  327. return rc;
  328. }
  329. /*
  330. * Block frontend tape device release function.
  331. *
  332. * Note: One reference to the tape device was made by the open function. So
  333. * we just get the pointer here and release the reference.
  334. */
  335. static int
  336. tapeblock_release(struct gendisk *disk, fmode_t mode)
  337. {
  338. struct tape_device *device = disk->private_data;
  339. tape_state_set(device, TS_IN_USE);
  340. tape_release(device);
  341. tape_put_device(device);
  342. return 0;
  343. }
  344. /*
  345. * Initialize block device frontend.
  346. */
  347. int
  348. tapeblock_init(void)
  349. {
  350. int rc;
  351. /* Register the tape major number to the kernel */
  352. rc = register_blkdev(tapeblock_major, "tBLK");
  353. if (rc < 0)
  354. return rc;
  355. if (tapeblock_major == 0)
  356. tapeblock_major = rc;
  357. return 0;
  358. }
  359. /*
  360. * Deregister major for block device frontend
  361. */
  362. void
  363. tapeblock_exit(void)
  364. {
  365. unregister_blkdev(tapeblock_major, "tBLK");
  366. }