tape_block.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/smp_lock.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/kernel.h>
  21. #include <asm/debug.h>
  22. #define TAPE_DBF_AREA tape_core_dbf
  23. #include "tape.h"
  24. #define TAPEBLOCK_MAX_SEC 100
  25. #define TAPEBLOCK_MIN_REQUEUE 3
  26. /*
  27. * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
  28. *
  29. * In 2.5/2.6 the block device request function is very likely to be called
  30. * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
  31. * just call any function that tries to allocate CCW requests from that con-
  32. * text since it might sleep. There are two choices to work around this:
  33. * a) do not allocate with kmalloc but use its own memory pool
  34. * b) take requests from the queue outside that context, knowing that
  35. * allocation might sleep
  36. */
  37. /*
  38. * file operation structure for tape block frontend
  39. */
  40. static int tapeblock_open(struct block_device *, fmode_t);
  41. static int tapeblock_release(struct gendisk *, fmode_t);
  42. static int tapeblock_medium_changed(struct gendisk *);
  43. static int tapeblock_revalidate_disk(struct gendisk *);
  44. static const struct block_device_operations tapeblock_fops = {
  45. .owner = THIS_MODULE,
  46. .open = tapeblock_open,
  47. .release = tapeblock_release,
  48. .media_changed = tapeblock_medium_changed,
  49. .revalidate_disk = tapeblock_revalidate_disk,
  50. };
  51. static int tapeblock_major = 0;
  52. static void
  53. tapeblock_trigger_requeue(struct tape_device *device)
  54. {
  55. /* Protect against rescheduling. */
  56. if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
  57. return;
  58. schedule_work(&device->blk_data.requeue_task);
  59. }
  60. /*
  61. * Post finished request.
  62. */
  63. static void
  64. __tapeblock_end_request(struct tape_request *ccw_req, void *data)
  65. {
  66. struct tape_device *device;
  67. struct request *req;
  68. DBF_LH(6, "__tapeblock_end_request()\n");
  69. device = ccw_req->device;
  70. req = (struct request *) data;
  71. blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
  72. if (ccw_req->rc == 0)
  73. /* Update position. */
  74. device->blk_data.block_position =
  75. (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
  76. else
  77. /* We lost the position information due to an error. */
  78. device->blk_data.block_position = -1;
  79. device->discipline->free_bread(ccw_req);
  80. if (!list_empty(&device->req_queue) ||
  81. blk_peek_request(device->blk_data.request_queue))
  82. tapeblock_trigger_requeue(device);
  83. }
  84. /*
  85. * Feed the tape device CCW queue with requests supplied in a list.
  86. */
  87. static int
  88. tapeblock_start_request(struct tape_device *device, struct request *req)
  89. {
  90. struct tape_request * ccw_req;
  91. int rc;
  92. DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
  93. ccw_req = device->discipline->bread(device, req);
  94. if (IS_ERR(ccw_req)) {
  95. DBF_EVENT(1, "TBLOCK: bread failed\n");
  96. blk_end_request_all(req, -EIO);
  97. return PTR_ERR(ccw_req);
  98. }
  99. ccw_req->callback = __tapeblock_end_request;
  100. ccw_req->callback_data = (void *) req;
  101. ccw_req->retries = TAPEBLOCK_RETRIES;
  102. rc = tape_do_io_async(device, ccw_req);
  103. if (rc) {
  104. /*
  105. * Start/enqueueing failed. No retries in
  106. * this case.
  107. */
  108. blk_end_request_all(req, -EIO);
  109. device->discipline->free_bread(ccw_req);
  110. }
  111. return rc;
  112. }
  113. /*
  114. * Move requests from the block device request queue to the tape device ccw
  115. * queue.
  116. */
  117. static void
  118. tapeblock_requeue(struct work_struct *work) {
  119. struct tape_blk_data * blkdat;
  120. struct tape_device * device;
  121. struct request_queue * queue;
  122. int nr_queued;
  123. struct request * req;
  124. struct list_head * l;
  125. int rc;
  126. blkdat = container_of(work, struct tape_blk_data, requeue_task);
  127. device = blkdat->device;
  128. if (!device)
  129. return;
  130. spin_lock_irq(get_ccwdev_lock(device->cdev));
  131. queue = device->blk_data.request_queue;
  132. /* Count number of requests on ccw queue. */
  133. nr_queued = 0;
  134. list_for_each(l, &device->req_queue)
  135. nr_queued++;
  136. spin_unlock(get_ccwdev_lock(device->cdev));
  137. spin_lock_irq(&device->blk_data.request_queue_lock);
  138. while (
  139. !blk_queue_plugged(queue) &&
  140. blk_peek_request(queue) &&
  141. nr_queued < TAPEBLOCK_MIN_REQUEUE
  142. ) {
  143. req = blk_fetch_request(queue);
  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. rc = elevator_change(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. lock_kernel();
  304. device = tape_get_device(disk->private_data);
  305. if (device->required_tapemarks) {
  306. DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
  307. pr_warning("%s: Opening the tape failed because of missing "
  308. "end-of-file marks\n", dev_name(&device->cdev->dev));
  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. unlock_kernel();
  324. return 0;
  325. release:
  326. tape_release(device);
  327. put_device:
  328. tape_put_device(device);
  329. unlock_kernel();
  330. return rc;
  331. }
  332. /*
  333. * Block frontend tape device release function.
  334. *
  335. * Note: One reference to the tape device was made by the open function. So
  336. * we just get the pointer here and release the reference.
  337. */
  338. static int
  339. tapeblock_release(struct gendisk *disk, fmode_t mode)
  340. {
  341. struct tape_device *device = disk->private_data;
  342. lock_kernel();
  343. tape_state_set(device, TS_IN_USE);
  344. tape_release(device);
  345. tape_put_device(device);
  346. unlock_kernel();
  347. return 0;
  348. }
  349. /*
  350. * Initialize block device frontend.
  351. */
  352. int
  353. tapeblock_init(void)
  354. {
  355. int rc;
  356. /* Register the tape major number to the kernel */
  357. rc = register_blkdev(tapeblock_major, "tBLK");
  358. if (rc < 0)
  359. return rc;
  360. if (tapeblock_major == 0)
  361. tapeblock_major = rc;
  362. return 0;
  363. }
  364. /*
  365. * Deregister major for block device frontend
  366. */
  367. void
  368. tapeblock_exit(void)
  369. {
  370. unregister_blkdev(tapeblock_major, "tBLK");
  371. }