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