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