blob: 96816149368a94ad396a86a2d6622c14d01ae04a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Carsten Otteab640db2009-03-26 15:24:38 +010013#define KMSG_COMPONENT "tape"
Michael Holzheubb509912009-12-18 17:43:21 +010014#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
Carsten Otteab640db2009-03-26 15:24:38 +010015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/blkdev.h>
19#include <linux/interrupt.h>
20#include <linux/buffer_head.h>
Martin Schwidefskyc1637532006-12-08 15:53:57 +010021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/debug.h>
24
25#define TAPE_DBF_AREA tape_core_dbf
26
27#include "tape.h"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define TAPEBLOCK_MAX_SEC 100
30#define TAPEBLOCK_MIN_REQUEUE 3
31
32/*
33 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
34 *
35 * In 2.5/2.6 the block device request function is very likely to be called
36 * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
37 * just call any function that tries to allocate CCW requests from that con-
38 * text since it might sleep. There are two choices to work around this:
39 * a) do not allocate with kmalloc but use its own memory pool
40 * b) take requests from the queue outside that context, knowing that
41 * allocation might sleep
42 */
43
44/*
45 * file operation structure for tape block frontend
46 */
Al Viro4e999af2008-03-02 10:39:59 -050047static int tapeblock_open(struct block_device *, fmode_t);
48static int tapeblock_release(struct gendisk *, fmode_t);
49static int tapeblock_ioctl(struct block_device *, fmode_t, unsigned int,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 unsigned long);
51static int tapeblock_medium_changed(struct gendisk *);
52static int tapeblock_revalidate_disk(struct gendisk *);
53
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070054static const struct block_device_operations tapeblock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .owner = THIS_MODULE,
Al Viro4e999af2008-03-02 10:39:59 -050056 .open = tapeblock_open,
57 .release = tapeblock_release,
Martin Schwidefsky369a4632009-12-07 12:52:04 +010058 .ioctl = tapeblock_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .media_changed = tapeblock_medium_changed,
60 .revalidate_disk = tapeblock_revalidate_disk,
61};
62
63static int tapeblock_major = 0;
64
65static void
66tapeblock_trigger_requeue(struct tape_device *device)
67{
68 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -080069 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return;
71 schedule_work(&device->blk_data.requeue_task);
72}
73
74/*
75 * Post finished request.
76 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010077static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070078__tapeblock_end_request(struct tape_request *ccw_req, void *data)
79{
80 struct tape_device *device;
81 struct request *req;
82
83 DBF_LH(6, "__tapeblock_end_request()\n");
84
85 device = ccw_req->device;
86 req = (struct request *) data;
Tejun Heo40cbbb72009-04-23 11:05:19 +090087 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (ccw_req->rc == 0)
89 /* Update position. */
90 device->blk_data.block_position =
Tejun Heo83096eb2009-05-07 22:24:39 +090091 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 else
93 /* We lost the position information due to an error. */
94 device->blk_data.block_position = -1;
95 device->discipline->free_bread(ccw_req);
96 if (!list_empty(&device->req_queue) ||
Tejun Heo9934c8c2009-05-08 11:54:16 +090097 blk_peek_request(device->blk_data.request_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 tapeblock_trigger_requeue(device);
99}
100
101/*
102 * Feed the tape device CCW queue with requests supplied in a list.
103 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100104static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105tapeblock_start_request(struct tape_device *device, struct request *req)
106{
107 struct tape_request * ccw_req;
108 int rc;
109
110 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
111
112 ccw_req = device->discipline->bread(device, req);
113 if (IS_ERR(ccw_req)) {
114 DBF_EVENT(1, "TBLOCK: bread failed\n");
Tejun Heo40cbbb72009-04-23 11:05:19 +0900115 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return PTR_ERR(ccw_req);
117 }
118 ccw_req->callback = __tapeblock_end_request;
119 ccw_req->callback_data = (void *) req;
120 ccw_req->retries = TAPEBLOCK_RETRIES;
121
122 rc = tape_do_io_async(device, ccw_req);
123 if (rc) {
124 /*
125 * Start/enqueueing failed. No retries in
126 * this case.
127 */
Tejun Heo40cbbb72009-04-23 11:05:19 +0900128 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 device->discipline->free_bread(ccw_req);
130 }
131
132 return rc;
133}
134
135/*
136 * Move requests from the block device request queue to the tape device ccw
137 * queue.
138 */
139static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100140tapeblock_requeue(struct work_struct *work) {
141 struct tape_blk_data * blkdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct tape_device * device;
Jens Axboe165125e2007-07-24 09:28:11 +0200143 struct request_queue * queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int nr_queued;
145 struct request * req;
146 struct list_head * l;
147 int rc;
148
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100149 blkdat = container_of(work, struct tape_blk_data, requeue_task);
150 device = blkdat->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!device)
152 return;
153
154 spin_lock_irq(get_ccwdev_lock(device->cdev));
155 queue = device->blk_data.request_queue;
156
157 /* Count number of requests on ccw queue. */
158 nr_queued = 0;
159 list_for_each(l, &device->req_queue)
160 nr_queued++;
161 spin_unlock(get_ccwdev_lock(device->cdev));
162
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100163 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 while (
165 !blk_queue_plugged(queue) &&
Michael Holzheu03cadd32009-10-14 12:43:45 +0200166 blk_peek_request(queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 nr_queued < TAPEBLOCK_MIN_REQUEUE
168 ) {
Michael Holzheu03cadd32009-10-14 12:43:45 +0200169 req = blk_fetch_request(queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (rq_data_dir(req) == WRITE) {
171 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100172 spin_unlock_irq(&device->blk_data.request_queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900173 blk_end_request_all(req, -EIO);
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100174 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 continue;
176 }
Michael Holzheuf71ad622008-05-30 10:03:25 +0200177 nr_queued++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_unlock_irq(&device->blk_data.request_queue_lock);
179 rc = tapeblock_start_request(device, req);
180 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 spin_unlock_irq(&device->blk_data.request_queue_lock);
183 atomic_set(&device->blk_data.requeue_scheduled, 0);
184}
185
186/*
187 * Tape request queue function. Called from ll_rw_blk.c
188 */
189static void
Jens Axboe165125e2007-07-24 09:28:11 +0200190tapeblock_request_fn(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct tape_device *device;
193
194 device = (struct tape_device *) queue->queuedata;
195 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200196 BUG_ON(device == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 tapeblock_trigger_requeue(device);
198}
199
200/*
201 * This function is called for every new tapedevice
202 */
203int
204tapeblock_setup_device(struct tape_device * device)
205{
206 struct tape_blk_data * blkdat;
207 struct gendisk * disk;
208 int rc;
209
210 blkdat = &device->blk_data;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100211 blkdat->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 spin_lock_init(&blkdat->request_queue_lock);
213 atomic_set(&blkdat->requeue_scheduled, 0);
214
215 blkdat->request_queue = blk_init_queue(
216 tapeblock_request_fn,
217 &blkdat->request_queue_lock
218 );
219 if (!blkdat->request_queue)
220 return -ENOMEM;
221
222 elevator_exit(blkdat->request_queue->elevator);
223 rc = elevator_init(blkdat->request_queue, "noop");
224 if (rc)
225 goto cleanup_queue;
226
Martin K. Petersene1defc42009-05-22 17:17:49 -0400227 blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
229 blk_queue_max_phys_segments(blkdat->request_queue, -1L);
230 blk_queue_max_hw_segments(blkdat->request_queue, -1L);
231 blk_queue_max_segment_size(blkdat->request_queue, -1L);
232 blk_queue_segment_boundary(blkdat->request_queue, -1L);
233
234 disk = alloc_disk(1);
235 if (!disk) {
236 rc = -ENOMEM;
237 goto cleanup_queue;
238 }
239
240 disk->major = tapeblock_major;
241 disk->first_minor = device->first_minor;
242 disk->fops = &tapeblock_fops;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100243 disk->private_data = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 disk->queue = blkdat->request_queue;
245 set_capacity(disk, 0);
246 sprintf(disk->disk_name, "btibm%d",
247 device->first_minor / TAPE_MINORS_PER_DEV);
248
249 blkdat->disk = disk;
250 blkdat->medium_changed = 1;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100251 blkdat->request_queue->queuedata = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 add_disk(disk);
254
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100255 tape_get_device(device);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100256 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return 0;
259
260cleanup_queue:
261 blk_cleanup_queue(blkdat->request_queue);
262 blkdat->request_queue = NULL;
263
264 return rc;
265}
266
267void
268tapeblock_cleanup_device(struct tape_device *device)
269{
270 flush_scheduled_work();
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100271 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (!device->blk_data.disk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto cleanup_queue;
275 }
276
277 del_gendisk(device->blk_data.disk);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100278 device->blk_data.disk->private_data = NULL;
279 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 put_disk(device->blk_data.disk);
281
282 device->blk_data.disk = NULL;
283cleanup_queue:
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100284 device->blk_data.request_queue->queuedata = NULL;
285 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 blk_cleanup_queue(device->blk_data.request_queue);
288 device->blk_data.request_queue = NULL;
289}
290
291/*
292 * Detect number of blocks of the tape.
293 * FIXME: can we extent this to detect the blocks size as well ?
294 */
295static int
296tapeblock_revalidate_disk(struct gendisk *disk)
297{
298 struct tape_device * device;
299 unsigned int nr_of_blks;
300 int rc;
301
302 device = (struct tape_device *) disk->private_data;
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200303 BUG_ON(!device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (!device->blk_data.medium_changed)
306 return 0;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 rc = tape_mtop(device, MTFSFM, 1);
309 if (rc)
310 return rc;
311
312 rc = tape_mtop(device, MTTELL, 1);
313 if (rc < 0)
314 return rc;
315
Michael Holzheu59e36922009-09-11 10:29:07 +0200316 pr_info("%s: Determining the size of the recorded area...\n",
317 dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 DBF_LH(3, "Image file ends at %d\n", rc);
319 nr_of_blks = rc;
320
321 /* This will fail for the first file. Catch the error by checking the
322 * position. */
323 tape_mtop(device, MTBSF, 1);
324
325 rc = tape_mtop(device, MTTELL, 1);
326 if (rc < 0)
327 return rc;
328
329 if (rc > nr_of_blks)
330 return -EINVAL;
331
332 DBF_LH(3, "Image file starts at %d\n", rc);
333 device->bof = rc;
334 nr_of_blks -= rc;
335
Michael Holzheu59e36922009-09-11 10:29:07 +0200336 pr_info("%s: The size of the recorded area is %i blocks\n",
337 dev_name(&device->cdev->dev), nr_of_blks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 set_capacity(device->blk_data.disk,
339 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
340
341 device->blk_data.block_position = 0;
342 device->blk_data.medium_changed = 0;
343 return 0;
344}
345
346static int
347tapeblock_medium_changed(struct gendisk *disk)
348{
349 struct tape_device *device;
350
351 device = (struct tape_device *) disk->private_data;
352 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
353 device, device->blk_data.medium_changed);
354
355 return device->blk_data.medium_changed;
356}
357
358/*
359 * Block frontend tape device open function.
360 */
361static int
Al Viro4e999af2008-03-02 10:39:59 -0500362tapeblock_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Al Viro4e999af2008-03-02 10:39:59 -0500364 struct gendisk * disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct tape_device * device;
366 int rc;
367
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100368 device = tape_get_device(disk->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (device->required_tapemarks) {
371 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
Michael Holzheu59e36922009-09-11 10:29:07 +0200372 pr_warning("%s: Opening the tape failed because of missing "
373 "end-of-file marks\n", dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rc = -EPERM;
375 goto put_device;
376 }
377
378 rc = tape_open(device);
379 if (rc)
380 goto put_device;
381
382 rc = tapeblock_revalidate_disk(disk);
383 if (rc)
384 goto release;
385
386 /*
387 * Note: The reference to <device> is hold until the release function
388 * is called.
389 */
390 tape_state_set(device, TS_BLKUSE);
391 return 0;
392
393release:
394 tape_release(device);
395 put_device:
396 tape_put_device(device);
397 return rc;
398}
399
400/*
401 * Block frontend tape device release function.
402 *
403 * Note: One reference to the tape device was made by the open function. So
404 * we just get the pointer here and release the reference.
405 */
406static int
Al Viro4e999af2008-03-02 10:39:59 -0500407tapeblock_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct tape_device *device = disk->private_data;
410
411 tape_state_set(device, TS_IN_USE);
412 tape_release(device);
413 tape_put_device(device);
414
415 return 0;
416}
417
418/*
419 * Support of some generic block device IOCTLs.
420 */
421static int
422tapeblock_ioctl(
Al Viro4e999af2008-03-02 10:39:59 -0500423 struct block_device * bdev,
424 fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 unsigned int command,
426 unsigned long arg
427) {
428 int rc;
429 int minor;
Al Viro4e999af2008-03-02 10:39:59 -0500430 struct gendisk *disk = bdev->bd_disk;
Peter Oberparleiterf9760692006-04-10 22:53:49 -0700431 struct tape_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 rc = 0;
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200434 BUG_ON(!disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 device = disk->private_data;
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200436 BUG_ON(!device);
Al Viro4e999af2008-03-02 10:39:59 -0500437 minor = MINOR(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command);
440 DBF_LH(6, "device = %d:%d\n", tapeblock_major, minor);
441
442 switch (command) {
443 /* Refuse some IOCTL calls without complaining (mount). */
444 case 0x5310: /* CDROMMULTISESSION */
445 rc = -EINVAL;
446 break;
447 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 rc = -EINVAL;
449 }
450
451 return rc;
452}
453
454/*
455 * Initialize block device frontend.
456 */
457int
458tapeblock_init(void)
459{
460 int rc;
461
462 /* Register the tape major number to the kernel */
463 rc = register_blkdev(tapeblock_major, "tBLK");
464 if (rc < 0)
465 return rc;
466
467 if (tapeblock_major == 0)
468 tapeblock_major = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 0;
470}
471
472/*
473 * Deregister major for block device frontend
474 */
475void
476tapeblock_exit(void)
477{
478 unregister_blkdev(tapeblock_major, "tBLK");
479}