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