tape_block.c 11 KB

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