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