Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Carsten Otte | ab640db | 2009-03-26 15:24:38 +0100 | [diff] [blame] | 13 | #define KMSG_COMPONENT "tape" |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/buffer_head.h> |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <asm/debug.h> |
| 23 | |
| 24 | #define TAPE_DBF_AREA tape_core_dbf |
| 25 | |
| 26 | #include "tape.h" |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | unsigned long); |
| 50 | static int tapeblock_medium_changed(struct gendisk *); |
| 51 | static int tapeblock_revalidate_disk(struct gendisk *); |
| 52 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 53 | static const struct block_device_operations tapeblock_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | .owner = THIS_MODULE, |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 55 | .open = tapeblock_open, |
| 56 | .release = tapeblock_release, |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 57 | .ioctl = tapeblock_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 68 | if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return; |
| 70 | schedule_work(&device->blk_data.requeue_task); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Post finished request. |
| 75 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 76 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 86 | blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (ccw_req->rc == 0) |
| 88 | /* Update position. */ |
| 89 | device->blk_data.block_position = |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 90 | (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) || |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 96 | blk_peek_request(device->blk_data.request_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | tapeblock_trigger_requeue(device); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Feed the tape device CCW queue with requests supplied in a list. |
| 102 | */ |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 103 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 114 | blk_end_request_all(req, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 127 | blk_end_request_all(req, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 139 | tapeblock_requeue(struct work_struct *work) { |
| 140 | struct tape_blk_data * blkdat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct tape_device * device; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 142 | struct request_queue * queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | int nr_queued; |
| 144 | struct request * req; |
| 145 | struct list_head * l; |
| 146 | int rc; |
| 147 | |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 148 | blkdat = container_of(work, struct tape_blk_data, requeue_task); |
| 149 | device = blkdat->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Frank Munzert | 7a4a1cc | 2008-10-28 11:10:17 +0100 | [diff] [blame] | 162 | spin_lock_irq(&device->blk_data.request_queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | while ( |
| 164 | !blk_queue_plugged(queue) && |
Michael Holzheu | 03cadd3 | 2009-10-14 12:43:45 +0200 | [diff] [blame] | 165 | blk_peek_request(queue) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | nr_queued < TAPEBLOCK_MIN_REQUEUE |
| 167 | ) { |
Michael Holzheu | 03cadd3 | 2009-10-14 12:43:45 +0200 | [diff] [blame] | 168 | req = blk_fetch_request(queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | if (rq_data_dir(req) == WRITE) { |
| 170 | DBF_EVENT(1, "TBLOCK: Rejecting write request\n"); |
Frank Munzert | 7a4a1cc | 2008-10-28 11:10:17 +0100 | [diff] [blame] | 171 | spin_unlock_irq(&device->blk_data.request_queue_lock); |
Tejun Heo | 40cbbb7 | 2009-04-23 11:05:19 +0900 | [diff] [blame] | 172 | blk_end_request_all(req, -EIO); |
Frank Munzert | 7a4a1cc | 2008-10-28 11:10:17 +0100 | [diff] [blame] | 173 | spin_lock_irq(&device->blk_data.request_queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | continue; |
| 175 | } |
Michael Holzheu | f71ad62 | 2008-05-30 10:03:25 +0200 | [diff] [blame] | 176 | nr_queued++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | spin_unlock_irq(&device->blk_data.request_queue_lock); |
| 178 | rc = tapeblock_start_request(device, req); |
| 179 | spin_lock_irq(&device->blk_data.request_queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | spin_unlock_irq(&device->blk_data.request_queue_lock); |
| 182 | atomic_set(&device->blk_data.requeue_scheduled, 0); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Tape request queue function. Called from ll_rw_blk.c |
| 187 | */ |
| 188 | static void |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 189 | tapeblock_request_fn(struct request_queue *queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | struct tape_device *device; |
| 192 | |
| 193 | device = (struct tape_device *) queue->queuedata; |
| 194 | DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device); |
Eric Sesterhenn | 3a8dc89 | 2006-04-01 01:28:11 +0200 | [diff] [blame] | 195 | BUG_ON(device == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | tapeblock_trigger_requeue(device); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * This function is called for every new tapedevice |
| 201 | */ |
| 202 | int |
| 203 | tapeblock_setup_device(struct tape_device * device) |
| 204 | { |
| 205 | struct tape_blk_data * blkdat; |
| 206 | struct gendisk * disk; |
| 207 | int rc; |
| 208 | |
| 209 | blkdat = &device->blk_data; |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 210 | blkdat->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | spin_lock_init(&blkdat->request_queue_lock); |
| 212 | atomic_set(&blkdat->requeue_scheduled, 0); |
| 213 | |
| 214 | blkdat->request_queue = blk_init_queue( |
| 215 | tapeblock_request_fn, |
| 216 | &blkdat->request_queue_lock |
| 217 | ); |
| 218 | if (!blkdat->request_queue) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | elevator_exit(blkdat->request_queue->elevator); |
| 222 | rc = elevator_init(blkdat->request_queue, "noop"); |
| 223 | if (rc) |
| 224 | goto cleanup_queue; |
| 225 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 226 | blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC); |
| 228 | blk_queue_max_phys_segments(blkdat->request_queue, -1L); |
| 229 | blk_queue_max_hw_segments(blkdat->request_queue, -1L); |
| 230 | blk_queue_max_segment_size(blkdat->request_queue, -1L); |
| 231 | blk_queue_segment_boundary(blkdat->request_queue, -1L); |
| 232 | |
| 233 | disk = alloc_disk(1); |
| 234 | if (!disk) { |
| 235 | rc = -ENOMEM; |
| 236 | goto cleanup_queue; |
| 237 | } |
| 238 | |
| 239 | disk->major = tapeblock_major; |
| 240 | disk->first_minor = device->first_minor; |
| 241 | disk->fops = &tapeblock_fops; |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 242 | disk->private_data = tape_get_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | disk->queue = blkdat->request_queue; |
| 244 | set_capacity(disk, 0); |
| 245 | sprintf(disk->disk_name, "btibm%d", |
| 246 | device->first_minor / TAPE_MINORS_PER_DEV); |
| 247 | |
| 248 | blkdat->disk = disk; |
| 249 | blkdat->medium_changed = 1; |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 250 | blkdat->request_queue->queuedata = tape_get_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | add_disk(disk); |
| 253 | |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 254 | tape_get_device(device); |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 255 | INIT_WORK(&blkdat->requeue_task, tapeblock_requeue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | return 0; |
| 258 | |
| 259 | cleanup_queue: |
| 260 | blk_cleanup_queue(blkdat->request_queue); |
| 261 | blkdat->request_queue = NULL; |
| 262 | |
| 263 | return rc; |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | tapeblock_cleanup_device(struct tape_device *device) |
| 268 | { |
| 269 | flush_scheduled_work(); |
Martin Schwidefsky | c163753 | 2006-12-08 15:53:57 +0100 | [diff] [blame] | 270 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | if (!device->blk_data.disk) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | goto cleanup_queue; |
| 274 | } |
| 275 | |
| 276 | del_gendisk(device->blk_data.disk); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 277 | device->blk_data.disk->private_data = NULL; |
| 278 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | put_disk(device->blk_data.disk); |
| 280 | |
| 281 | device->blk_data.disk = NULL; |
| 282 | cleanup_queue: |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 283 | device->blk_data.request_queue->queuedata = NULL; |
| 284 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | blk_cleanup_queue(device->blk_data.request_queue); |
| 287 | device->blk_data.request_queue = NULL; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Detect number of blocks of the tape. |
| 292 | * FIXME: can we extent this to detect the blocks size as well ? |
| 293 | */ |
| 294 | static int |
| 295 | tapeblock_revalidate_disk(struct gendisk *disk) |
| 296 | { |
| 297 | struct tape_device * device; |
| 298 | unsigned int nr_of_blks; |
| 299 | int rc; |
| 300 | |
| 301 | device = (struct tape_device *) disk->private_data; |
Eric Sesterhenn | 3a8dc89 | 2006-04-01 01:28:11 +0200 | [diff] [blame] | 302 | BUG_ON(!device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | if (!device->blk_data.medium_changed) |
| 305 | return 0; |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | rc = tape_mtop(device, MTFSFM, 1); |
| 308 | if (rc) |
| 309 | return rc; |
| 310 | |
| 311 | rc = tape_mtop(device, MTTELL, 1); |
| 312 | if (rc < 0) |
| 313 | return rc; |
| 314 | |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 315 | pr_info("%s: Determining the size of the recorded area...\n", |
| 316 | dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | DBF_LH(3, "Image file ends at %d\n", rc); |
| 318 | nr_of_blks = rc; |
| 319 | |
| 320 | /* This will fail for the first file. Catch the error by checking the |
| 321 | * position. */ |
| 322 | tape_mtop(device, MTBSF, 1); |
| 323 | |
| 324 | rc = tape_mtop(device, MTTELL, 1); |
| 325 | if (rc < 0) |
| 326 | return rc; |
| 327 | |
| 328 | if (rc > nr_of_blks) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | DBF_LH(3, "Image file starts at %d\n", rc); |
| 332 | device->bof = rc; |
| 333 | nr_of_blks -= rc; |
| 334 | |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 335 | pr_info("%s: The size of the recorded area is %i blocks\n", |
| 336 | dev_name(&device->cdev->dev), nr_of_blks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | set_capacity(device->blk_data.disk, |
| 338 | nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512)); |
| 339 | |
| 340 | device->blk_data.block_position = 0; |
| 341 | device->blk_data.medium_changed = 0; |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int |
| 346 | tapeblock_medium_changed(struct gendisk *disk) |
| 347 | { |
| 348 | struct tape_device *device; |
| 349 | |
| 350 | device = (struct tape_device *) disk->private_data; |
| 351 | DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n", |
| 352 | device, device->blk_data.medium_changed); |
| 353 | |
| 354 | return device->blk_data.medium_changed; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Block frontend tape device open function. |
| 359 | */ |
| 360 | static int |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 361 | tapeblock_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 363 | struct gendisk * disk = bdev->bd_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | struct tape_device * device; |
| 365 | int rc; |
| 366 | |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 367 | device = tape_get_device(disk->private_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | if (device->required_tapemarks) { |
| 370 | DBF_EVENT(2, "TBLOCK: missing tapemarks\n"); |
Michael Holzheu | 59e3692 | 2009-09-11 10:29:07 +0200 | [diff] [blame] | 371 | pr_warning("%s: Opening the tape failed because of missing " |
| 372 | "end-of-file marks\n", dev_name(&device->cdev->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | rc = -EPERM; |
| 374 | goto put_device; |
| 375 | } |
| 376 | |
| 377 | rc = tape_open(device); |
| 378 | if (rc) |
| 379 | goto put_device; |
| 380 | |
| 381 | rc = tapeblock_revalidate_disk(disk); |
| 382 | if (rc) |
| 383 | goto release; |
| 384 | |
| 385 | /* |
| 386 | * Note: The reference to <device> is hold until the release function |
| 387 | * is called. |
| 388 | */ |
| 389 | tape_state_set(device, TS_BLKUSE); |
| 390 | return 0; |
| 391 | |
| 392 | release: |
| 393 | tape_release(device); |
| 394 | put_device: |
| 395 | tape_put_device(device); |
| 396 | return rc; |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Block frontend tape device release function. |
| 401 | * |
| 402 | * Note: One reference to the tape device was made by the open function. So |
| 403 | * we just get the pointer here and release the reference. |
| 404 | */ |
| 405 | static int |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 406 | tapeblock_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | struct tape_device *device = disk->private_data; |
| 409 | |
| 410 | tape_state_set(device, TS_IN_USE); |
| 411 | tape_release(device); |
| 412 | tape_put_device(device); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Support of some generic block device IOCTLs. |
| 419 | */ |
| 420 | static int |
| 421 | tapeblock_ioctl( |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 422 | struct block_device * bdev, |
| 423 | fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | unsigned int command, |
| 425 | unsigned long arg |
| 426 | ) { |
| 427 | int rc; |
| 428 | int minor; |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 429 | struct gendisk *disk = bdev->bd_disk; |
Peter Oberparleiter | f976069 | 2006-04-10 22:53:49 -0700 | [diff] [blame] | 430 | struct tape_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | rc = 0; |
Eric Sesterhenn | 3a8dc89 | 2006-04-01 01:28:11 +0200 | [diff] [blame] | 433 | BUG_ON(!disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | device = disk->private_data; |
Eric Sesterhenn | 3a8dc89 | 2006-04-01 01:28:11 +0200 | [diff] [blame] | 435 | BUG_ON(!device); |
Al Viro | 4e999af | 2008-03-02 10:39:59 -0500 | [diff] [blame] | 436 | minor = MINOR(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command); |
| 439 | DBF_LH(6, "device = %d:%d\n", tapeblock_major, minor); |
| 440 | |
| 441 | switch (command) { |
| 442 | /* Refuse some IOCTL calls without complaining (mount). */ |
| 443 | case 0x5310: /* CDROMMULTISESSION */ |
| 444 | rc = -EINVAL; |
| 445 | break; |
| 446 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | rc = -EINVAL; |
| 448 | } |
| 449 | |
| 450 | return rc; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Initialize block device frontend. |
| 455 | */ |
| 456 | int |
| 457 | tapeblock_init(void) |
| 458 | { |
| 459 | int rc; |
| 460 | |
| 461 | /* Register the tape major number to the kernel */ |
| 462 | rc = register_blkdev(tapeblock_major, "tBLK"); |
| 463 | if (rc < 0) |
| 464 | return rc; |
| 465 | |
| 466 | if (tapeblock_major == 0) |
| 467 | tapeblock_major = rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Deregister major for block device frontend |
| 473 | */ |
| 474 | void |
| 475 | tapeblock_exit(void) |
| 476 | { |
| 477 | unregister_blkdev(tapeblock_major, "tBLK"); |
| 478 | } |