block: convert to pos and nr_sectors accessors
[deliverable/linux.git] / drivers / s390 / char / tape_block.c
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
13 #define KMSG_COMPONENT "tape"
14
15 #include <linux/fs.h>
16 #include <linux/module.h>
17 #include <linux/blkdev.h>
18 #include <linux/interrupt.h>
19 #include <linux/buffer_head.h>
20 #include <linux/kernel.h>
21
22 #include <asm/debug.h>
23
24 #define TAPE_DBF_AREA tape_core_dbf
25
26 #include "tape.h"
27
28 #define TAPEBLOCK_MAX_SEC 100
29 #define TAPEBLOCK_MIN_REQUEUE 3
30
31 /*
32 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
33 *
34 * In 2.5/2.6 the block device request function is very likely to be called
35 * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
36 * just call any function that tries to allocate CCW requests from that con-
37 * text since it might sleep. There are two choices to work around this:
38 * a) do not allocate with kmalloc but use its own memory pool
39 * b) take requests from the queue outside that context, knowing that
40 * allocation might sleep
41 */
42
43 /*
44 * file operation structure for tape block frontend
45 */
46 static int tapeblock_open(struct block_device *, fmode_t);
47 static int tapeblock_release(struct gendisk *, fmode_t);
48 static int tapeblock_ioctl(struct block_device *, fmode_t, unsigned int,
49 unsigned long);
50 static int tapeblock_medium_changed(struct gendisk *);
51 static int tapeblock_revalidate_disk(struct gendisk *);
52
53 static struct block_device_operations tapeblock_fops = {
54 .owner = THIS_MODULE,
55 .open = tapeblock_open,
56 .release = tapeblock_release,
57 .locked_ioctl = tapeblock_ioctl,
58 .media_changed = tapeblock_medium_changed,
59 .revalidate_disk = tapeblock_revalidate_disk,
60 };
61
62 static int tapeblock_major = 0;
63
64 static void
65 tapeblock_trigger_requeue(struct tape_device *device)
66 {
67 /* Protect against rescheduling. */
68 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
69 return;
70 schedule_work(&device->blk_data.requeue_task);
71 }
72
73 /*
74 * Post finished request.
75 */
76 static void
77 __tapeblock_end_request(struct tape_request *ccw_req, void *data)
78 {
79 struct tape_device *device;
80 struct request *req;
81
82 DBF_LH(6, "__tapeblock_end_request()\n");
83
84 device = ccw_req->device;
85 req = (struct request *) data;
86 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
87 if (ccw_req->rc == 0)
88 /* Update position. */
89 device->blk_data.block_position =
90 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
91 else
92 /* We lost the position information due to an error. */
93 device->blk_data.block_position = -1;
94 device->discipline->free_bread(ccw_req);
95 if (!list_empty(&device->req_queue) ||
96 elv_next_request(device->blk_data.request_queue))
97 tapeblock_trigger_requeue(device);
98 }
99
100 /*
101 * Feed the tape device CCW queue with requests supplied in a list.
102 */
103 static int
104 tapeblock_start_request(struct tape_device *device, struct request *req)
105 {
106 struct tape_request * ccw_req;
107 int rc;
108
109 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
110
111 ccw_req = device->discipline->bread(device, req);
112 if (IS_ERR(ccw_req)) {
113 DBF_EVENT(1, "TBLOCK: bread failed\n");
114 blk_end_request_all(req, -EIO);
115 return PTR_ERR(ccw_req);
116 }
117 ccw_req->callback = __tapeblock_end_request;
118 ccw_req->callback_data = (void *) req;
119 ccw_req->retries = TAPEBLOCK_RETRIES;
120
121 rc = tape_do_io_async(device, ccw_req);
122 if (rc) {
123 /*
124 * Start/enqueueing failed. No retries in
125 * this case.
126 */
127 blk_end_request_all(req, -EIO);
128 device->discipline->free_bread(ccw_req);
129 }
130
131 return rc;
132 }
133
134 /*
135 * Move requests from the block device request queue to the tape device ccw
136 * queue.
137 */
138 static void
139 tapeblock_requeue(struct work_struct *work) {
140 struct tape_blk_data * blkdat;
141 struct tape_device * device;
142 struct request_queue * queue;
143 int nr_queued;
144 struct request * req;
145 struct list_head * l;
146 int rc;
147
148 blkdat = container_of(work, struct tape_blk_data, requeue_task);
149 device = blkdat->device;
150 if (!device)
151 return;
152
153 spin_lock_irq(get_ccwdev_lock(device->cdev));
154 queue = device->blk_data.request_queue;
155
156 /* Count number of requests on ccw queue. */
157 nr_queued = 0;
158 list_for_each(l, &device->req_queue)
159 nr_queued++;
160 spin_unlock(get_ccwdev_lock(device->cdev));
161
162 spin_lock_irq(&device->blk_data.request_queue_lock);
163 while (
164 !blk_queue_plugged(queue) &&
165 elv_next_request(queue) &&
166 nr_queued < TAPEBLOCK_MIN_REQUEUE
167 ) {
168 req = elv_next_request(queue);
169 if (rq_data_dir(req) == WRITE) {
170 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
171 blkdev_dequeue_request(req);
172 spin_unlock_irq(&device->blk_data.request_queue_lock);
173 blk_end_request_all(req, -EIO);
174 spin_lock_irq(&device->blk_data.request_queue_lock);
175 continue;
176 }
177 blkdev_dequeue_request(req);
178 nr_queued++;
179 spin_unlock_irq(&device->blk_data.request_queue_lock);
180 rc = tapeblock_start_request(device, req);
181 spin_lock_irq(&device->blk_data.request_queue_lock);
182 }
183 spin_unlock_irq(&device->blk_data.request_queue_lock);
184 atomic_set(&device->blk_data.requeue_scheduled, 0);
185 }
186
187 /*
188 * Tape request queue function. Called from ll_rw_blk.c
189 */
190 static void
191 tapeblock_request_fn(struct request_queue *queue)
192 {
193 struct tape_device *device;
194
195 device = (struct tape_device *) queue->queuedata;
196 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
197 BUG_ON(device == NULL);
198 tapeblock_trigger_requeue(device);
199 }
200
201 /*
202 * This function is called for every new tapedevice
203 */
204 int
205 tapeblock_setup_device(struct tape_device * device)
206 {
207 struct tape_blk_data * blkdat;
208 struct gendisk * disk;
209 int rc;
210
211 blkdat = &device->blk_data;
212 blkdat->device = device;
213 spin_lock_init(&blkdat->request_queue_lock);
214 atomic_set(&blkdat->requeue_scheduled, 0);
215
216 blkdat->request_queue = blk_init_queue(
217 tapeblock_request_fn,
218 &blkdat->request_queue_lock
219 );
220 if (!blkdat->request_queue)
221 return -ENOMEM;
222
223 elevator_exit(blkdat->request_queue->elevator);
224 rc = elevator_init(blkdat->request_queue, "noop");
225 if (rc)
226 goto cleanup_queue;
227
228 blk_queue_hardsect_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
229 blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
230 blk_queue_max_phys_segments(blkdat->request_queue, -1L);
231 blk_queue_max_hw_segments(blkdat->request_queue, -1L);
232 blk_queue_max_segment_size(blkdat->request_queue, -1L);
233 blk_queue_segment_boundary(blkdat->request_queue, -1L);
234
235 disk = alloc_disk(1);
236 if (!disk) {
237 rc = -ENOMEM;
238 goto cleanup_queue;
239 }
240
241 disk->major = tapeblock_major;
242 disk->first_minor = device->first_minor;
243 disk->fops = &tapeblock_fops;
244 disk->private_data = tape_get_device_reference(device);
245 disk->queue = blkdat->request_queue;
246 set_capacity(disk, 0);
247 sprintf(disk->disk_name, "btibm%d",
248 device->first_minor / TAPE_MINORS_PER_DEV);
249
250 blkdat->disk = disk;
251 blkdat->medium_changed = 1;
252 blkdat->request_queue->queuedata = tape_get_device_reference(device);
253
254 add_disk(disk);
255
256 tape_get_device_reference(device);
257 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
258
259 return 0;
260
261 cleanup_queue:
262 blk_cleanup_queue(blkdat->request_queue);
263 blkdat->request_queue = NULL;
264
265 return rc;
266 }
267
268 void
269 tapeblock_cleanup_device(struct tape_device *device)
270 {
271 flush_scheduled_work();
272 tape_put_device(device);
273
274 if (!device->blk_data.disk) {
275 goto cleanup_queue;
276 }
277
278 del_gendisk(device->blk_data.disk);
279 device->blk_data.disk->private_data =
280 tape_put_device(device->blk_data.disk->private_data);
281 put_disk(device->blk_data.disk);
282
283 device->blk_data.disk = NULL;
284 cleanup_queue:
285 device->blk_data.request_queue->queuedata = tape_put_device(device);
286
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 */
295 static int
296 tapeblock_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;
303 BUG_ON(!device);
304
305 if (!device->blk_data.medium_changed)
306 return 0;
307
308 dev_info(&device->cdev->dev, "Determining the size of the recorded "
309 "area...\n");
310 rc = tape_mtop(device, MTFSFM, 1);
311 if (rc)
312 return rc;
313
314 rc = tape_mtop(device, MTTELL, 1);
315 if (rc < 0)
316 return rc;
317
318 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
336 dev_info(&device->cdev->dev, "The size of the recorded area is %i "
337 "blocks\n", nr_of_blks);
338 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
346 static int
347 tapeblock_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 */
361 static int
362 tapeblock_open(struct block_device *bdev, fmode_t mode)
363 {
364 struct gendisk * disk = bdev->bd_disk;
365 struct tape_device * device;
366 int rc;
367
368 device = tape_get_device_reference(disk->private_data);
369
370 if (device->required_tapemarks) {
371 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
372 dev_warn(&device->cdev->dev, "Opening the tape failed because"
373 " of missing end-of-file marks\n");
374 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
393 release:
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 */
406 static int
407 tapeblock_release(struct gendisk *disk, fmode_t mode)
408 {
409 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 */
421 static int
422 tapeblock_ioctl(
423 struct block_device * bdev,
424 fmode_t mode,
425 unsigned int command,
426 unsigned long arg
427 ) {
428 int rc;
429 int minor;
430 struct gendisk *disk = bdev->bd_disk;
431 struct tape_device *device;
432
433 rc = 0;
434 BUG_ON(!disk);
435 device = disk->private_data;
436 BUG_ON(!device);
437 minor = MINOR(bdev->bd_dev);
438
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:
448 rc = -EINVAL;
449 }
450
451 return rc;
452 }
453
454 /*
455 * Initialize block device frontend.
456 */
457 int
458 tapeblock_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;
469 return 0;
470 }
471
472 /*
473 * Deregister major for block device frontend
474 */
475 void
476 tapeblock_exit(void)
477 {
478 unregister_blkdev(tapeblock_major, "tBLK");
479 }
This page took 0.053091 seconds and 5 git commands to generate.