Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * character device frontend for tape device driver |
| 4 | * |
| 5 | * S390 and zSeries version |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 6 | * Copyright IBM Corp. 2001, 2006 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
| 8 | * Michael Holzheu <holzheu@de.ibm.com> |
| 9 | * Tuan Ngo-Anh <ngoanh@de.ibm.com> |
| 10 | * Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 11 | */ |
| 12 | |
Michael Holzheu | bb50991 | 2009-12-18 17:43:21 +0100 | [diff] [blame] | 13 | #define KMSG_COMPONENT "tape" |
| 14 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/proc_fs.h> |
| 19 | #include <linux/mtio.h> |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 20 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 22 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define TAPE_DBF_AREA tape_core_dbf |
| 25 | |
| 26 | #include "tape.h" |
| 27 | #include "tape_std.h" |
| 28 | #include "tape_class.h" |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #define TAPECHAR_MAJOR 0 /* get dynamic major */ |
| 31 | |
| 32 | /* |
| 33 | * file operation structure for tape character frontend |
| 34 | */ |
| 35 | static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *); |
| 36 | static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *); |
| 37 | static int tapechar_open(struct inode *,struct file *); |
| 38 | static int tapechar_release(struct inode *,struct file *); |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 39 | static long tapechar_ioctl(struct file *, unsigned int, unsigned long); |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_COMPAT |
| 41 | static long tapechar_compat_ioctl(struct file *, unsigned int, unsigned long); |
| 42 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 44 | static const struct file_operations tape_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | .owner = THIS_MODULE, |
| 47 | .read = tapechar_read, |
| 48 | .write = tapechar_write, |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 49 | .unlocked_ioctl = tapechar_ioctl, |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 50 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 51 | .compat_ioctl = tapechar_compat_ioctl, |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 52 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | .open = tapechar_open, |
| 54 | .release = tapechar_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 55 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static int tapechar_major = TAPECHAR_MAJOR; |
| 59 | |
| 60 | /* |
| 61 | * This function is called for every new tapedevice |
| 62 | */ |
| 63 | int |
| 64 | tapechar_setup_device(struct tape_device * device) |
| 65 | { |
| 66 | char device_name[20]; |
| 67 | |
| 68 | sprintf(device_name, "ntibm%i", device->first_minor / 2); |
| 69 | device->nt = register_tape_dev( |
| 70 | &device->cdev->dev, |
| 71 | MKDEV(tapechar_major, device->first_minor), |
| 72 | &tape_fops, |
| 73 | device_name, |
| 74 | "non-rewinding" |
| 75 | ); |
| 76 | device_name[0] = 'r'; |
| 77 | device->rt = register_tape_dev( |
| 78 | &device->cdev->dev, |
| 79 | MKDEV(tapechar_major, device->first_minor + 1), |
| 80 | &tape_fops, |
| 81 | device_name, |
| 82 | "rewinding" |
| 83 | ); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | tapechar_cleanup_device(struct tape_device *device) |
| 90 | { |
Michael Holzheu | 92bf435 | 2008-04-17 07:46:05 +0200 | [diff] [blame] | 91 | unregister_tape_dev(&device->cdev->dev, device->rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | device->rt = NULL; |
Michael Holzheu | 92bf435 | 2008-04-17 07:46:05 +0200 | [diff] [blame] | 93 | unregister_tape_dev(&device->cdev->dev, device->nt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | device->nt = NULL; |
| 95 | } |
| 96 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 97 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | tapechar_check_idalbuffer(struct tape_device *device, size_t block_size) |
| 99 | { |
| 100 | struct idal_buffer *new; |
| 101 | |
| 102 | if (device->char_data.idal_buf != NULL && |
| 103 | device->char_data.idal_buf->size == block_size) |
| 104 | return 0; |
| 105 | |
| 106 | if (block_size > MAX_BLOCKSIZE) { |
| 107 | DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n", |
| 108 | block_size, MAX_BLOCKSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return -EINVAL; |
| 110 | } |
| 111 | |
| 112 | /* The current idal buffer is not correct. Allocate a new one. */ |
| 113 | new = idal_buffer_alloc(block_size, 0); |
Julien Brunel | 0983e56 | 2008-08-21 19:46:30 +0200 | [diff] [blame] | 114 | if (IS_ERR(new)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return -ENOMEM; |
| 116 | |
| 117 | if (device->char_data.idal_buf != NULL) |
| 118 | idal_buffer_free(device->char_data.idal_buf); |
| 119 | |
| 120 | device->char_data.idal_buf = new; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Tape device read function |
| 127 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 128 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos) |
| 130 | { |
| 131 | struct tape_device *device; |
| 132 | struct tape_request *request; |
| 133 | size_t block_size; |
| 134 | int rc; |
| 135 | |
| 136 | DBF_EVENT(6, "TCHAR:read\n"); |
| 137 | device = (struct tape_device *) filp->private_data; |
| 138 | |
| 139 | /* |
| 140 | * If the tape isn't terminated yet, do it now. And since we then |
| 141 | * are at the end of the tape there wouldn't be anything to read |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 142 | * anyways. So we return immediately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | */ |
| 144 | if(device->required_tapemarks) { |
| 145 | return tape_std_terminate_write(device); |
| 146 | } |
| 147 | |
| 148 | /* Find out block size to use */ |
| 149 | if (device->char_data.block_size != 0) { |
| 150 | if (count < device->char_data.block_size) { |
| 151 | DBF_EVENT(3, "TCHAR:read smaller than block " |
| 152 | "size was requested\n"); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | block_size = device->char_data.block_size; |
| 156 | } else { |
| 157 | block_size = count; |
| 158 | } |
| 159 | |
| 160 | rc = tapechar_check_idalbuffer(device, block_size); |
| 161 | if (rc) |
| 162 | return rc; |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size); |
| 165 | /* Let the discipline build the ccw chain. */ |
| 166 | request = device->discipline->read_block(device, block_size); |
| 167 | if (IS_ERR(request)) |
| 168 | return PTR_ERR(request); |
| 169 | /* Execute it. */ |
| 170 | rc = tape_do_io(device, request); |
| 171 | if (rc == 0) { |
| 172 | rc = block_size - request->rescnt; |
| 173 | DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | /* Copy data from idal buffer to user space. */ |
| 175 | if (idal_buffer_to_user(device->char_data.idal_buf, |
| 176 | data, rc) != 0) |
| 177 | rc = -EFAULT; |
| 178 | } |
| 179 | tape_free_request(request); |
| 180 | return rc; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Tape device write function |
| 185 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 186 | static ssize_t |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos) |
| 188 | { |
| 189 | struct tape_device *device; |
| 190 | struct tape_request *request; |
| 191 | size_t block_size; |
| 192 | size_t written; |
| 193 | int nblocks; |
| 194 | int i, rc; |
| 195 | |
| 196 | DBF_EVENT(6, "TCHAR:write\n"); |
| 197 | device = (struct tape_device *) filp->private_data; |
| 198 | /* Find out block size and number of blocks */ |
| 199 | if (device->char_data.block_size != 0) { |
| 200 | if (count < device->char_data.block_size) { |
| 201 | DBF_EVENT(3, "TCHAR:write smaller than block " |
| 202 | "size was requested\n"); |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | block_size = device->char_data.block_size; |
| 206 | nblocks = count / block_size; |
| 207 | } else { |
| 208 | block_size = count; |
| 209 | nblocks = 1; |
| 210 | } |
| 211 | |
| 212 | rc = tapechar_check_idalbuffer(device, block_size); |
| 213 | if (rc) |
| 214 | return rc; |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size); |
| 217 | DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks); |
| 218 | /* Let the discipline build the ccw chain. */ |
| 219 | request = device->discipline->write_block(device, block_size); |
| 220 | if (IS_ERR(request)) |
| 221 | return PTR_ERR(request); |
| 222 | rc = 0; |
| 223 | written = 0; |
| 224 | for (i = 0; i < nblocks; i++) { |
| 225 | /* Copy data from user space to idal buffer. */ |
| 226 | if (idal_buffer_from_user(device->char_data.idal_buf, |
| 227 | data, block_size)) { |
| 228 | rc = -EFAULT; |
| 229 | break; |
| 230 | } |
| 231 | rc = tape_do_io(device, request); |
| 232 | if (rc) |
| 233 | break; |
| 234 | DBF_EVENT(6, "TCHAR:wbytes: %lx\n", |
| 235 | block_size - request->rescnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | written += block_size - request->rescnt; |
| 237 | if (request->rescnt != 0) |
| 238 | break; |
| 239 | data += block_size; |
| 240 | } |
| 241 | tape_free_request(request); |
| 242 | if (rc == -ENOSPC) { |
| 243 | /* |
| 244 | * Ok, the device has no more space. It has NOT written |
| 245 | * the block. |
| 246 | */ |
| 247 | if (device->discipline->process_eov) |
| 248 | device->discipline->process_eov(device); |
| 249 | if (written > 0) |
| 250 | rc = 0; |
| 251 | |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * After doing a write we always need two tapemarks to correctly |
| 256 | * terminate the tape (one to terminate the file, the second to |
| 257 | * flag the end of recorded data. |
| 258 | * Since process_eov positions the tape in front of the written |
| 259 | * tapemark it doesn't hurt to write two marks again. |
| 260 | */ |
| 261 | if (!rc) |
| 262 | device->required_tapemarks = 2; |
| 263 | |
| 264 | return rc ? rc : written; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Character frontend tape device open function. |
| 269 | */ |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 270 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | tapechar_open (struct inode *inode, struct file *filp) |
| 272 | { |
| 273 | struct tape_device *device; |
| 274 | int minor, rc; |
| 275 | |
| 276 | DBF_EVENT(6, "TCHAR:open: %i:%i\n", |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 277 | imajor(file_inode(filp)), |
| 278 | iminor(file_inode(filp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 280 | if (imajor(file_inode(filp)) != tapechar_major) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return -ENODEV; |
| 282 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 283 | minor = iminor(file_inode(filp)); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 284 | device = tape_find_device(minor / TAPE_MINORS_PER_DEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (IS_ERR(device)) { |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 286 | DBF_EVENT(3, "TCHAR:open: tape_find_device() failed\n"); |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 287 | return PTR_ERR(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | rc = tape_open(device); |
| 291 | if (rc == 0) { |
| 292 | filp->private_data = device; |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 293 | stream_open(inode, filp); |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 294 | } else |
Jonathan Corbet | 764a4a8 | 2008-05-15 10:01:17 -0600 | [diff] [blame] | 295 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | return rc; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Character frontend tape device release function. |
| 302 | */ |
| 303 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 304 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | tapechar_release(struct inode *inode, struct file *filp) |
| 306 | { |
| 307 | struct tape_device *device; |
| 308 | |
| 309 | DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode)); |
| 310 | device = (struct tape_device *) filp->private_data; |
| 311 | |
| 312 | /* |
| 313 | * If this is the rewinding tape minor then rewind. In that case we |
| 314 | * write all required tapemarks. Otherwise only one to terminate the |
| 315 | * file. |
| 316 | */ |
| 317 | if ((iminor(inode) & 1) != 0) { |
| 318 | if (device->required_tapemarks) |
| 319 | tape_std_terminate_write(device); |
| 320 | tape_mtop(device, MTREW, 1); |
| 321 | } else { |
| 322 | if (device->required_tapemarks > 1) { |
| 323 | if (tape_mtop(device, MTWEOF, 1) == 0) |
| 324 | device->required_tapemarks--; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (device->char_data.idal_buf != NULL) { |
| 329 | idal_buffer_free(device->char_data.idal_buf); |
| 330 | device->char_data.idal_buf = NULL; |
| 331 | } |
| 332 | tape_release(device); |
Martin Schwidefsky | 8fd138c | 2009-12-07 12:52:03 +0100 | [diff] [blame] | 333 | filp->private_data = NULL; |
| 334 | tape_put_device(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Tape device io controls. |
| 341 | */ |
| 342 | static int |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 343 | __tapechar_ioctl(struct tape_device *device, |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 344 | unsigned int no, void __user *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | int rc; |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | if (no == MTIOCTOP) { |
| 349 | struct mtop op; |
| 350 | |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 351 | if (copy_from_user(&op, data, sizeof(op)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return -EFAULT; |
| 353 | if (op.mt_count < 0) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | /* |
| 357 | * Operations that change tape position should write final |
| 358 | * tapemarks. |
| 359 | */ |
| 360 | switch (op.mt_op) { |
| 361 | case MTFSF: |
| 362 | case MTBSF: |
| 363 | case MTFSR: |
| 364 | case MTBSR: |
| 365 | case MTREW: |
| 366 | case MTOFFL: |
| 367 | case MTEOM: |
| 368 | case MTRETEN: |
| 369 | case MTBSFM: |
| 370 | case MTFSFM: |
| 371 | case MTSEEK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (device->required_tapemarks) |
| 373 | tape_std_terminate_write(device); |
| 374 | default: |
| 375 | ; |
| 376 | } |
| 377 | rc = tape_mtop(device, op.mt_op, op.mt_count); |
| 378 | |
| 379 | if (op.mt_op == MTWEOF && rc == 0) { |
| 380 | if (op.mt_count > device->required_tapemarks) |
| 381 | device->required_tapemarks = 0; |
| 382 | else |
| 383 | device->required_tapemarks -= op.mt_count; |
| 384 | } |
| 385 | return rc; |
| 386 | } |
| 387 | if (no == MTIOCPOS) { |
| 388 | /* MTIOCPOS: query the tape position. */ |
| 389 | struct mtpos pos; |
| 390 | |
| 391 | rc = tape_mtop(device, MTTELL, 1); |
| 392 | if (rc < 0) |
| 393 | return rc; |
| 394 | pos.mt_blkno = rc; |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 395 | return put_user_mtpos(data, &pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | if (no == MTIOCGET) { |
| 398 | /* MTIOCGET: query the tape drive status. */ |
| 399 | struct mtget get; |
| 400 | |
| 401 | memset(&get, 0, sizeof(get)); |
| 402 | get.mt_type = MT_ISUNKNOWN; |
| 403 | get.mt_resid = 0 /* device->devstat.rescnt */; |
Stefan Haberland | 9fc98ad | 2014-09-16 11:02:24 +0200 | [diff] [blame] | 404 | get.mt_dsreg = |
| 405 | ((device->char_data.block_size << MT_ST_BLKSIZE_SHIFT) |
| 406 | & MT_ST_BLKSIZE_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | /* FIXME: mt_gstat, mt_erreg, mt_fileno */ |
| 408 | get.mt_gstat = 0; |
| 409 | get.mt_erreg = 0; |
| 410 | get.mt_fileno = 0; |
| 411 | get.mt_gstat = device->tape_generic_status; |
| 412 | |
| 413 | if (device->medium_state == MS_LOADED) { |
| 414 | rc = tape_mtop(device, MTTELL, 1); |
| 415 | |
| 416 | if (rc < 0) |
| 417 | return rc; |
| 418 | |
| 419 | if (rc == 0) |
| 420 | get.mt_gstat |= GMT_BOT(~0); |
| 421 | |
| 422 | get.mt_blkno = rc; |
| 423 | } |
| 424 | |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 425 | return put_user_mtget(data, &get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | /* Try the discipline ioctl function. */ |
| 428 | if (device->discipline->ioctl_fn == NULL) |
| 429 | return -EINVAL; |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 430 | return device->discipline->ioctl_fn(device, no, (unsigned long)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 433 | static long |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 434 | tapechar_ioctl(struct file *filp, unsigned int no, unsigned long data) |
| 435 | { |
| 436 | struct tape_device *device; |
| 437 | long rc; |
| 438 | |
| 439 | DBF_EVENT(6, "TCHAR:ioct\n"); |
| 440 | |
| 441 | device = (struct tape_device *) filp->private_data; |
| 442 | mutex_lock(&device->mutex); |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 443 | rc = __tapechar_ioctl(device, no, (void __user *)data); |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 444 | mutex_unlock(&device->mutex); |
| 445 | return rc; |
| 446 | } |
| 447 | |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 448 | #ifdef CONFIG_COMPAT |
Martin Schwidefsky | 369a463 | 2009-12-07 12:52:04 +0100 | [diff] [blame] | 449 | static long |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 450 | tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data) |
| 451 | { |
| 452 | struct tape_device *device = filp->private_data; |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 453 | long rc; |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 454 | |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 455 | if (no == MTIOCPOS32) |
| 456 | no = MTIOCPOS; |
| 457 | else if (no == MTIOCGET32) |
| 458 | no = MTIOCGET; |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 459 | |
Arnd Bergmann | 1207045d | 2018-09-07 16:49:43 +0200 | [diff] [blame] | 460 | mutex_lock(&device->mutex); |
| 461 | rc = __tapechar_ioctl(device, no, compat_ptr(data)); |
| 462 | mutex_unlock(&device->mutex); |
| 463 | return rc; |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 464 | } |
Heiko Carstens | c540607 | 2010-01-13 20:44:44 +0100 | [diff] [blame] | 465 | #endif /* CONFIG_COMPAT */ |
Christoph Hellwig | f042e0f | 2006-01-09 20:52:07 -0800 | [diff] [blame] | 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* |
| 468 | * Initialize character device frontend. |
| 469 | */ |
| 470 | int |
| 471 | tapechar_init (void) |
| 472 | { |
| 473 | dev_t dev; |
| 474 | |
| 475 | if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0) |
| 476 | return -1; |
| 477 | |
| 478 | tapechar_major = MAJOR(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * cleanup |
| 485 | */ |
| 486 | void |
| 487 | tapechar_exit(void) |
| 488 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | unregister_chrdev_region(MKDEV(tapechar_major, 0), 256); |
| 490 | } |