tape_block.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #include <linux/fs.h>
  13. #include <linux/module.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/buffer_head.h>
  17. #include <linux/kernel.h>
  18. #include <asm/debug.h>
  19. #define TAPE_DBF_AREA tape_core_dbf
  20. #include "tape.h"
  21. #define PRINTK_HEADER "TAPE_BLOCK: "
  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 inode *, struct file *);
  39. static int tapeblock_release(struct inode *, struct file *);
  40. static int tapeblock_ioctl(struct inode *, struct file *, 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. .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 request *req, int uptodate)
  66. {
  67. if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
  68. BUG();
  69. end_that_request_last(req, uptodate);
  70. }
  71. static void
  72. __tapeblock_end_request(struct tape_request *ccw_req, void *data)
  73. {
  74. struct tape_device *device;
  75. struct request *req;
  76. DBF_LH(6, "__tapeblock_end_request()\n");
  77. device = ccw_req->device;
  78. req = (struct request *) data;
  79. tapeblock_end_request(req, ccw_req->rc == 0);
  80. if (ccw_req->rc == 0)
  81. /* Update position. */
  82. device->blk_data.block_position =
  83. (req->sector + req->nr_sectors) >> TAPEBLOCK_HSEC_S2B;
  84. else
  85. /* We lost the position information due to an error. */
  86. device->blk_data.block_position = -1;
  87. device->discipline->free_bread(ccw_req);
  88. if (!list_empty(&device->req_queue) ||
  89. elv_next_request(device->blk_data.request_queue))
  90. tapeblock_trigger_requeue(device);
  91. }
  92. /*
  93. * Feed the tape device CCW queue with requests supplied in a list.
  94. */
  95. static int
  96. tapeblock_start_request(struct tape_device *device, struct request *req)
  97. {
  98. struct tape_request * ccw_req;
  99. int rc;
  100. DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
  101. ccw_req = device->discipline->bread(device, req);
  102. if (IS_ERR(ccw_req)) {
  103. DBF_EVENT(1, "TBLOCK: bread failed\n");
  104. tapeblock_end_request(req, 0);
  105. return PTR_ERR(ccw_req);
  106. }
  107. ccw_req->callback = __tapeblock_end_request;
  108. ccw_req->callback_data = (void *) req;
  109. ccw_req->retries = TAPEBLOCK_RETRIES;
  110. rc = tape_do_io_async(device, ccw_req);
  111. if (rc) {
  112. /*
  113. * Start/enqueueing failed. No retries in
  114. * this case.
  115. */
  116. tapeblock_end_request(req, 0);
  117. device->discipline->free_bread(ccw_req);
  118. }
  119. return rc;
  120. }
  121. /*
  122. * Move requests from the block device request queue to the tape device ccw
  123. * queue.
  124. */
  125. static void
  126. tapeblock_requeue(struct work_struct *work) {
  127. struct tape_blk_data * blkdat;
  128. struct tape_device * device;
  129. request_queue_t * queue;
  130. int nr_queued;
  131. struct request * req;
  132. struct list_head * l;
  133. int rc;
  134. blkdat = container_of(work, struct tape_blk_data, requeue_task);
  135. device = blkdat->device;
  136. if (!device)
  137. return;
  138. spin_lock_irq(get_ccwdev_lock(device->cdev));
  139. queue = device->blk_data.request_queue;
  140. /* Count number of requests on ccw queue. */
  141. nr_queued = 0;
  142. list_for_each(l, &device->req_queue)
  143. nr_queued++;
  144. spin_unlock(get_ccwdev_lock(device->cdev));
  145. spin_lock(&device->blk_data.request_queue_lock);
  146. while (
  147. !blk_queue_plugged(queue) &&
  148. elv_next_request(queue) &&
  149. nr_queued < TAPEBLOCK_MIN_REQUEUE
  150. ) {
  151. req = elv_next_request(queue);
  152. if (rq_data_dir(req) == WRITE) {
  153. DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
  154. blkdev_dequeue_request(req);
  155. tapeblock_end_request(req, 0);
  156. continue;
  157. }
  158. spin_unlock_irq(&device->blk_data.request_queue_lock);
  159. rc = tapeblock_start_request(device, req);
  160. spin_lock_irq(&device->blk_data.request_queue_lock);
  161. blkdev_dequeue_request(req);
  162. nr_queued++;
  163. }
  164. spin_unlock_irq(&device->blk_data.request_queue_lock);
  165. atomic_set(&device->blk_data.requeue_scheduled, 0);
  166. }
  167. /*
  168. * Tape request queue function. Called from ll_rw_blk.c
  169. */
  170. static void
  171. tapeblock_request_fn(request_queue_t *queue)
  172. {
  173. struct tape_device *device;
  174. device = (struct tape_device *) queue->queuedata;
  175. DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
  176. BUG_ON(device == NULL);
  177. tapeblock_trigger_requeue(device);
  178. }
  179. /*
  180. * This function is called for every new tapedevice
  181. */
  182. int
  183. tapeblock_setup_device(struct tape_device * device)
  184. {
  185. struct tape_blk_data * blkdat;
  186. struct gendisk * disk;
  187. int rc;
  188. blkdat = &device->blk_data;
  189. blkdat->device = device;
  190. spin_lock_init(&blkdat->request_queue_lock);
  191. atomic_set(&blkdat->requeue_scheduled, 0);
  192. blkdat->request_queue = blk_init_queue(
  193. tapeblock_request_fn,
  194. &blkdat->request_queue_lock
  195. );
  196. if (!blkdat->request_queue)
  197. return -ENOMEM;
  198. elevator_exit(blkdat->request_queue->elevator);
  199. rc = elevator_init(blkdat->request_queue, "noop");
  200. if (rc)
  201. goto cleanup_queue;
  202. blk_queue_hardsect_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
  203. blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
  204. blk_queue_max_phys_segments(blkdat->request_queue, -1L);
  205. blk_queue_max_hw_segments(blkdat->request_queue, -1L);
  206. blk_queue_max_segment_size(blkdat->request_queue, -1L);
  207. blk_queue_segment_boundary(blkdat->request_queue, -1L);
  208. disk = alloc_disk(1);
  209. if (!disk) {
  210. rc = -ENOMEM;
  211. goto cleanup_queue;
  212. }
  213. disk->major = tapeblock_major;
  214. disk->first_minor = device->first_minor;
  215. disk->fops = &tapeblock_fops;
  216. disk->private_data = tape_get_device_reference(device);
  217. disk->queue = blkdat->request_queue;
  218. set_capacity(disk, 0);
  219. sprintf(disk->disk_name, "btibm%d",
  220. device->first_minor / TAPE_MINORS_PER_DEV);
  221. blkdat->disk = disk;
  222. blkdat->medium_changed = 1;
  223. blkdat->request_queue->queuedata = tape_get_device_reference(device);
  224. add_disk(disk);
  225. tape_get_device_reference(device);
  226. INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
  227. return 0;
  228. cleanup_queue:
  229. blk_cleanup_queue(blkdat->request_queue);
  230. blkdat->request_queue = NULL;
  231. return rc;
  232. }
  233. void
  234. tapeblock_cleanup_device(struct tape_device *device)
  235. {
  236. flush_scheduled_work();
  237. tape_put_device(device);
  238. if (!device->blk_data.disk) {
  239. PRINT_ERR("(%s): No gendisk to clean up!\n",
  240. device->cdev->dev.bus_id);
  241. goto cleanup_queue;
  242. }
  243. del_gendisk(device->blk_data.disk);
  244. device->blk_data.disk->private_data =
  245. tape_put_device(device->blk_data.disk->private_data);
  246. put_disk(device->blk_data.disk);
  247. device->blk_data.disk = NULL;
  248. cleanup_queue:
  249. device->blk_data.request_queue->queuedata = tape_put_device(device);
  250. blk_cleanup_queue(device->blk_data.request_queue);
  251. device->blk_data.request_queue = NULL;
  252. }
  253. /*
  254. * Detect number of blocks of the tape.
  255. * FIXME: can we extent this to detect the blocks size as well ?
  256. */
  257. static int
  258. tapeblock_revalidate_disk(struct gendisk *disk)
  259. {
  260. struct tape_device * device;
  261. unsigned int nr_of_blks;
  262. int rc;
  263. device = (struct tape_device *) disk->private_data;
  264. BUG_ON(!device);
  265. if (!device->blk_data.medium_changed)
  266. return 0;
  267. PRINT_INFO("Detecting media size...\n");
  268. rc = tape_mtop(device, MTFSFM, 1);
  269. if (rc)
  270. return rc;
  271. rc = tape_mtop(device, MTTELL, 1);
  272. if (rc < 0)
  273. return rc;
  274. DBF_LH(3, "Image file ends at %d\n", rc);
  275. nr_of_blks = rc;
  276. /* This will fail for the first file. Catch the error by checking the
  277. * position. */
  278. tape_mtop(device, MTBSF, 1);
  279. rc = tape_mtop(device, MTTELL, 1);
  280. if (rc < 0)
  281. return rc;
  282. if (rc > nr_of_blks)
  283. return -EINVAL;
  284. DBF_LH(3, "Image file starts at %d\n", rc);
  285. device->bof = rc;
  286. nr_of_blks -= rc;
  287. PRINT_INFO("Found %i blocks on media\n", nr_of_blks);
  288. set_capacity(device->blk_data.disk,
  289. nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
  290. device->blk_data.block_position = 0;
  291. device->blk_data.medium_changed = 0;
  292. return 0;
  293. }
  294. static int
  295. tapeblock_medium_changed(struct gendisk *disk)
  296. {
  297. struct tape_device *device;
  298. device = (struct tape_device *) disk->private_data;
  299. DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
  300. device, device->blk_data.medium_changed);
  301. return device->blk_data.medium_changed;
  302. }
  303. /*
  304. * Block frontend tape device open function.
  305. */
  306. static int
  307. tapeblock_open(struct inode *inode, struct file *filp)
  308. {
  309. struct gendisk * disk;
  310. struct tape_device * device;
  311. int rc;
  312. disk = inode->i_bdev->bd_disk;
  313. device = tape_get_device_reference(disk->private_data);
  314. if (device->required_tapemarks) {
  315. DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
  316. PRINT_ERR("TBLOCK: Refusing to open tape with missing"
  317. " end of file marks.\n");
  318. rc = -EPERM;
  319. goto put_device;
  320. }
  321. rc = tape_open(device);
  322. if (rc)
  323. goto put_device;
  324. rc = tapeblock_revalidate_disk(disk);
  325. if (rc)
  326. goto release;
  327. /*
  328. * Note: The reference to <device> is hold until the release function
  329. * is called.
  330. */
  331. tape_state_set(device, TS_BLKUSE);
  332. return 0;
  333. release:
  334. tape_release(device);
  335. put_device:
  336. tape_put_device(device);
  337. return rc;
  338. }
  339. /*
  340. * Block frontend tape device release function.
  341. *
  342. * Note: One reference to the tape device was made by the open function. So
  343. * we just get the pointer here and release the reference.
  344. */
  345. static int
  346. tapeblock_release(struct inode *inode, struct file *filp)
  347. {
  348. struct gendisk *disk = inode->i_bdev->bd_disk;
  349. struct tape_device *device = disk->private_data;
  350. tape_state_set(device, TS_IN_USE);
  351. tape_release(device);
  352. tape_put_device(device);
  353. return 0;
  354. }
  355. /*
  356. * Support of some generic block device IOCTLs.
  357. */
  358. static int
  359. tapeblock_ioctl(
  360. struct inode * inode,
  361. struct file * file,
  362. unsigned int command,
  363. unsigned long arg
  364. ) {
  365. int rc;
  366. int minor;
  367. struct gendisk *disk;
  368. struct tape_device *device;
  369. rc = 0;
  370. disk = inode->i_bdev->bd_disk;
  371. BUG_ON(!disk);
  372. device = disk->private_data;
  373. BUG_ON(!device);
  374. minor = iminor(inode);
  375. DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command);
  376. DBF_LH(6, "device = %d:%d\n", tapeblock_major, minor);
  377. switch (command) {
  378. /* Refuse some IOCTL calls without complaining (mount). */
  379. case 0x5310: /* CDROMMULTISESSION */
  380. rc = -EINVAL;
  381. break;
  382. default:
  383. PRINT_WARN("invalid ioctl 0x%x\n", command);
  384. rc = -EINVAL;
  385. }
  386. return rc;
  387. }
  388. /*
  389. * Initialize block device frontend.
  390. */
  391. int
  392. tapeblock_init(void)
  393. {
  394. int rc;
  395. /* Register the tape major number to the kernel */
  396. rc = register_blkdev(tapeblock_major, "tBLK");
  397. if (rc < 0)
  398. return rc;
  399. if (tapeblock_major == 0)
  400. tapeblock_major = rc;
  401. PRINT_INFO("tape gets major %d for block device\n", tapeblock_major);
  402. return 0;
  403. }
  404. /*
  405. * Deregister major for block device frontend
  406. */
  407. void
  408. tapeblock_exit(void)
  409. {
  410. unregister_blkdev(tapeblock_major, "tBLK");
  411. }