xref: /linux/drivers/s390/char/vmlogrdr.c (revision 367b8112fe2ea5c39a7bb4d263dcdd9b612fae18)
1 /*
2  * drivers/s390/char/vmlogrdr.c
3  *	character device driver for reading z/VM system service records
4  *
5  *
6  *	Copyright 2004 IBM Corporation
7  *	character device driver for reading z/VM system service records,
8  *	Version 1.0
9  *	Author(s): Xenia Tkatschow <xenia@us.ibm.com>
10  *		   Stefan Weinhuber <wein@de.ibm.com>
11  *
12  */
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/interrupt.h>
18 #include <linux/spinlock.h>
19 #include <asm/atomic.h>
20 #include <asm/uaccess.h>
21 #include <asm/cpcmd.h>
22 #include <asm/debug.h>
23 #include <asm/ebcdic.h>
24 #include <net/iucv/iucv.h>
25 #include <linux/kmod.h>
26 #include <linux/cdev.h>
27 #include <linux/device.h>
28 #include <linux/smp_lock.h>
29 #include <linux/string.h>
30 
31 
32 
33 MODULE_AUTHOR
34 	("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
35 	 "                            Stefan Weinhuber (wein@de.ibm.com)");
36 MODULE_DESCRIPTION ("Character device driver for reading z/VM "
37 		    "system service records.");
38 MODULE_LICENSE("GPL");
39 
40 
41 /*
42  * The size of the buffer for iucv data transfer is one page,
43  * but in addition to the data we read from iucv we also
44  * place an integer and some characters into that buffer,
45  * so the maximum size for record data is a little less then
46  * one page.
47  */
48 #define NET_BUFFER_SIZE	(PAGE_SIZE - sizeof(int) - sizeof(FENCE))
49 
50 /*
51  * The elements that are concurrently accessed by bottom halves are
52  * connection_established, iucv_path_severed, local_interrupt_buffer
53  * and receive_ready. The first three can be protected by
54  * priv_lock.  receive_ready is atomic, so it can be incremented and
55  * decremented without holding a lock.
56  * The variable dev_in_use needs to be protected by the lock, since
57  * it's a flag used by open to make sure that the device is opened only
58  * by one user at the same time.
59  */
60 struct vmlogrdr_priv_t {
61 	char system_service[8];
62 	char internal_name[8];
63 	char recording_name[8];
64 	struct iucv_path *path;
65 	int connection_established;
66 	int iucv_path_severed;
67 	struct iucv_message local_interrupt_buffer;
68 	atomic_t receive_ready;
69 	int minor_num;
70 	char * buffer;
71 	char * current_position;
72 	int remaining;
73 	ulong residual_length;
74 	int buffer_free;
75 	int dev_in_use; /* 1: already opened, 0: not opened*/
76 	spinlock_t priv_lock;
77 	struct device  *device;
78 	struct device  *class_device;
79 	int autorecording;
80 	int autopurge;
81 };
82 
83 
84 /*
85  * File operation structure for vmlogrdr devices
86  */
87 static int vmlogrdr_open(struct inode *, struct file *);
88 static int vmlogrdr_release(struct inode *, struct file *);
89 static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
90 			      size_t count, loff_t * ppos);
91 
92 static const struct file_operations vmlogrdr_fops = {
93 	.owner   = THIS_MODULE,
94 	.open    = vmlogrdr_open,
95 	.release = vmlogrdr_release,
96 	.read    = vmlogrdr_read,
97 };
98 
99 
100 static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
101 static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
102 static void vmlogrdr_iucv_message_pending(struct iucv_path *,
103 					  struct iucv_message *);
104 
105 
106 static struct iucv_handler vmlogrdr_iucv_handler = {
107 	.path_complete	 = vmlogrdr_iucv_path_complete,
108 	.path_severed	 = vmlogrdr_iucv_path_severed,
109 	.message_pending = vmlogrdr_iucv_message_pending,
110 };
111 
112 
113 static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
114 static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
115 
116 /*
117  * pointer to system service private structure
118  * minor number 0 --> logrec
119  * minor number 1 --> account
120  * minor number 2 --> symptom
121  */
122 
123 static struct vmlogrdr_priv_t sys_ser[] = {
124 	{ .system_service = "*LOGREC ",
125 	  .internal_name  = "logrec",
126 	  .recording_name = "EREP",
127 	  .minor_num      = 0,
128 	  .buffer_free    = 1,
129 	  .priv_lock	  = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
130 	  .autorecording  = 1,
131 	  .autopurge      = 1,
132 	},
133 	{ .system_service = "*ACCOUNT",
134 	  .internal_name  = "account",
135 	  .recording_name = "ACCOUNT",
136 	  .minor_num      = 1,
137 	  .buffer_free    = 1,
138 	  .priv_lock	  = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
139 	  .autorecording  = 1,
140 	  .autopurge      = 1,
141 	},
142 	{ .system_service = "*SYMPTOM",
143 	  .internal_name  = "symptom",
144 	  .recording_name = "SYMPTOM",
145 	  .minor_num      = 2,
146 	  .buffer_free    = 1,
147 	  .priv_lock	  = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
148 	  .autorecording  = 1,
149 	  .autopurge      = 1,
150 	}
151 };
152 
153 #define MAXMINOR  (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
154 
155 static char FENCE[] = {"EOR"};
156 static int vmlogrdr_major = 0;
157 static struct cdev  *vmlogrdr_cdev = NULL;
158 static int recording_class_AB;
159 
160 
161 static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
162 {
163 	struct vmlogrdr_priv_t * logptr = path->private;
164 
165 	spin_lock(&logptr->priv_lock);
166 	logptr->connection_established = 1;
167 	spin_unlock(&logptr->priv_lock);
168 	wake_up(&conn_wait_queue);
169 }
170 
171 
172 static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
173 {
174 	struct vmlogrdr_priv_t * logptr = path->private;
175 	u8 reason = (u8) ipuser[8];
176 
177 	printk (KERN_ERR "vmlogrdr: connection severed with"
178 		" reason %i\n", reason);
179 
180 	iucv_path_sever(path, NULL);
181 	kfree(path);
182 	logptr->path = NULL;
183 
184 	spin_lock(&logptr->priv_lock);
185 	logptr->connection_established = 0;
186 	logptr->iucv_path_severed = 1;
187 	spin_unlock(&logptr->priv_lock);
188 
189 	wake_up(&conn_wait_queue);
190 	/* just in case we're sleeping waiting for a record */
191 	wake_up_interruptible(&read_wait_queue);
192 }
193 
194 
195 static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
196 					  struct iucv_message *msg)
197 {
198 	struct vmlogrdr_priv_t * logptr = path->private;
199 
200 	/*
201 	 * This function is the bottom half so it should be quick.
202 	 * Copy the external interrupt data into our local eib and increment
203 	 * the usage count
204 	 */
205 	spin_lock(&logptr->priv_lock);
206 	memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
207 	atomic_inc(&logptr->receive_ready);
208 	spin_unlock(&logptr->priv_lock);
209 	wake_up_interruptible(&read_wait_queue);
210 }
211 
212 
213 static int vmlogrdr_get_recording_class_AB(void)
214 {
215 	char cp_command[]="QUERY COMMAND RECORDING ";
216 	char cp_response[80];
217 	char *tail;
218 	int len,i;
219 
220 	cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
221 	len = strnlen(cp_response,sizeof(cp_response));
222 	// now the parsing
223 	tail=strnchr(cp_response,len,'=');
224 	if (!tail)
225 		return 0;
226 	tail++;
227 	if (!strncmp("ANY",tail,3))
228 		return 1;
229 	if (!strncmp("NONE",tail,4))
230 		return 0;
231 	/*
232 	 * expect comma separated list of classes here, if one of them
233 	 * is A or B return 1 otherwise 0
234 	 */
235         for (i=tail-cp_response; i<len; i++)
236 		if ( cp_response[i]=='A' || cp_response[i]=='B' )
237 			return 1;
238 	return 0;
239 }
240 
241 
242 static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
243 			      int action, int purge)
244 {
245 
246 	char cp_command[80];
247 	char cp_response[160];
248 	char *onoff, *qid_string;
249 
250 	memset(cp_command, 0x00, sizeof(cp_command));
251 	memset(cp_response, 0x00, sizeof(cp_response));
252 
253         onoff = ((action == 1) ? "ON" : "OFF");
254 	qid_string = ((recording_class_AB == 1) ? " QID * " : "");
255 
256         /*
257 	 * The recording commands needs to be called with option QID
258 	 * for guests that have previlege classes A or B.
259 	 * Purging has to be done as separate step, because recording
260 	 * can't be switched on as long as records are on the queue.
261 	 * Doing both at the same time doesn't work.
262 	 */
263 
264 	if (purge) {
265 		snprintf(cp_command, sizeof(cp_command),
266 			 "RECORDING %s PURGE %s",
267 			 logptr->recording_name,
268 			 qid_string);
269 
270 		cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
271 	}
272 
273 	memset(cp_command, 0x00, sizeof(cp_command));
274 	memset(cp_response, 0x00, sizeof(cp_response));
275 	snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
276 		logptr->recording_name,
277 		onoff,
278 		qid_string);
279 
280 	cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
281 	/* The recording command will usually answer with 'Command complete'
282 	 * on success, but when the specific service was never connected
283 	 * before then there might be an additional informational message
284 	 * 'HCPCRC8072I Recording entry not found' before the
285          * 'Command complete'. So I use strstr rather then the strncmp.
286 	 */
287 	if (strstr(cp_response,"Command complete"))
288 		return 0;
289 	else
290 		return -EIO;
291 
292 }
293 
294 
295 static int vmlogrdr_open (struct inode *inode, struct file *filp)
296 {
297 	int dev_num = 0;
298 	struct vmlogrdr_priv_t * logptr = NULL;
299 	int connect_rc = 0;
300 	int ret;
301 
302 	dev_num = iminor(inode);
303 	if (dev_num > MAXMINOR)
304 		return -ENODEV;
305 	logptr = &sys_ser[dev_num];
306 
307 	/*
308 	 * only allow for blocking reads to be open
309 	 */
310 	if (filp->f_flags & O_NONBLOCK)
311 		return -ENOSYS;
312 
313 	/* Besure this device hasn't already been opened */
314 	lock_kernel();
315 	spin_lock_bh(&logptr->priv_lock);
316 	if (logptr->dev_in_use)	{
317 		spin_unlock_bh(&logptr->priv_lock);
318 		unlock_kernel();
319 		return -EBUSY;
320 	}
321 	logptr->dev_in_use = 1;
322 	logptr->connection_established = 0;
323 	logptr->iucv_path_severed = 0;
324 	atomic_set(&logptr->receive_ready, 0);
325 	logptr->buffer_free = 1;
326 	spin_unlock_bh(&logptr->priv_lock);
327 
328 	/* set the file options */
329 	filp->private_data = logptr;
330 	filp->f_op = &vmlogrdr_fops;
331 
332 	/* start recording for this service*/
333 	if (logptr->autorecording) {
334 		ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
335 		if (ret)
336 			printk (KERN_WARNING "vmlogrdr: failed to start "
337 				"recording automatically\n");
338 	}
339 
340 	/* create connection to the system service */
341 	logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
342 	if (!logptr->path)
343 		goto out_dev;
344 	connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
345 				       logptr->system_service, NULL, NULL,
346 				       logptr);
347 	if (connect_rc) {
348 		printk (KERN_ERR "vmlogrdr: iucv connection to %s "
349 			"failed with rc %i \n", logptr->system_service,
350 			connect_rc);
351 		goto out_path;
352 	}
353 
354 	/* We've issued the connect and now we must wait for a
355 	 * ConnectionComplete or ConnectinSevered Interrupt
356 	 * before we can continue to process.
357 	 */
358 	wait_event(conn_wait_queue, (logptr->connection_established)
359 		   || (logptr->iucv_path_severed));
360 	if (logptr->iucv_path_severed)
361 		goto out_record;
362  	ret = nonseekable_open(inode, filp);
363 	unlock_kernel();
364 	return ret;
365 
366 out_record:
367 	if (logptr->autorecording)
368 		vmlogrdr_recording(logptr,0,logptr->autopurge);
369 out_path:
370 	kfree(logptr->path);	/* kfree(NULL) is ok. */
371 	logptr->path = NULL;
372 out_dev:
373 	logptr->dev_in_use = 0;
374 	unlock_kernel();
375 	return -EIO;
376 }
377 
378 
379 static int vmlogrdr_release (struct inode *inode, struct file *filp)
380 {
381 	int ret;
382 
383 	struct vmlogrdr_priv_t * logptr = filp->private_data;
384 
385 	iucv_path_sever(logptr->path, NULL);
386 	kfree(logptr->path);
387 	logptr->path = NULL;
388 	if (logptr->autorecording) {
389 		ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
390 		if (ret)
391 			printk (KERN_WARNING "vmlogrdr: failed to stop "
392 				"recording automatically\n");
393 	}
394 	logptr->dev_in_use = 0;
395 
396 	return 0;
397 }
398 
399 
400 static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
401 {
402 	int rc, *temp;
403 	/* we need to keep track of two data sizes here:
404 	 * The number of bytes we need to receive from iucv and
405 	 * the total number of bytes we actually write into the buffer.
406 	 */
407 	int user_data_count, iucv_data_count;
408 	char * buffer;
409 
410 	if (atomic_read(&priv->receive_ready)) {
411 		spin_lock_bh(&priv->priv_lock);
412 		if (priv->residual_length){
413 			/* receive second half of a record */
414 			iucv_data_count = priv->residual_length;
415 			user_data_count = 0;
416 			buffer = priv->buffer;
417 		} else {
418 			/* receive a new record:
419 			 * We need to return the total length of the record
420                          * + size of FENCE in the first 4 bytes of the buffer.
421 		         */
422 			iucv_data_count = priv->local_interrupt_buffer.length;
423 			user_data_count = sizeof(int);
424 			temp = (int*)priv->buffer;
425 			*temp= iucv_data_count + sizeof(FENCE);
426 			buffer = priv->buffer + sizeof(int);
427 		}
428 		/*
429 		 * If the record is bigger then our buffer, we receive only
430 		 * a part of it. We can get the rest later.
431 		 */
432 		if (iucv_data_count > NET_BUFFER_SIZE)
433 			iucv_data_count = NET_BUFFER_SIZE;
434 		rc = iucv_message_receive(priv->path,
435 					  &priv->local_interrupt_buffer,
436 					  0, buffer, iucv_data_count,
437 					  &priv->residual_length);
438 		spin_unlock_bh(&priv->priv_lock);
439 		/* An rc of 5 indicates that the record was bigger then
440 		 * the buffer, which is OK for us. A 9 indicates that the
441 		 * record was purged befor we could receive it.
442 		 */
443 		if (rc == 5)
444 			rc = 0;
445 		if (rc == 9)
446 			atomic_set(&priv->receive_ready, 0);
447 	} else {
448 		rc = 1;
449 	}
450 	if (!rc) {
451 		priv->buffer_free = 0;
452  		user_data_count += iucv_data_count;
453 		priv->current_position = priv->buffer;
454 		if (priv->residual_length == 0){
455 			/* the whole record has been captured,
456 			 * now add the fence */
457 			atomic_dec(&priv->receive_ready);
458 			buffer = priv->buffer + user_data_count;
459 			memcpy(buffer, FENCE, sizeof(FENCE));
460 			user_data_count += sizeof(FENCE);
461 		}
462 		priv->remaining = user_data_count;
463 	}
464 
465 	return rc;
466 }
467 
468 
469 static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
470 			     size_t count, loff_t * ppos)
471 {
472 	int rc;
473 	struct vmlogrdr_priv_t * priv = filp->private_data;
474 
475 	while (priv->buffer_free) {
476 		rc = vmlogrdr_receive_data(priv);
477 		if (rc) {
478 			rc = wait_event_interruptible(read_wait_queue,
479 					atomic_read(&priv->receive_ready));
480 			if (rc)
481 				return rc;
482 		}
483 	}
484 	/* copy only up to end of record */
485 	if (count > priv->remaining)
486 		count = priv->remaining;
487 
488 	if (copy_to_user(data, priv->current_position, count))
489 		return -EFAULT;
490 
491 	*ppos += count;
492 	priv->current_position += count;
493 	priv->remaining -= count;
494 
495 	/* if all data has been transferred, set buffer free */
496 	if (priv->remaining == 0)
497 		priv->buffer_free = 1;
498 
499 	return count;
500 }
501 
502 static ssize_t vmlogrdr_autopurge_store(struct device * dev,
503 					struct device_attribute *attr,
504 					const char * buf, size_t count)
505 {
506 	struct vmlogrdr_priv_t *priv = dev->driver_data;
507 	ssize_t ret = count;
508 
509 	switch (buf[0]) {
510 	case '0':
511 		priv->autopurge=0;
512 		break;
513 	case '1':
514 		priv->autopurge=1;
515 		break;
516 	default:
517 		ret = -EINVAL;
518 	}
519 	return ret;
520 }
521 
522 
523 static ssize_t vmlogrdr_autopurge_show(struct device *dev,
524 				       struct device_attribute *attr,
525 				       char *buf)
526 {
527 	struct vmlogrdr_priv_t *priv = dev->driver_data;
528 	return sprintf(buf, "%u\n", priv->autopurge);
529 }
530 
531 
532 static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
533 		   vmlogrdr_autopurge_store);
534 
535 
536 static ssize_t vmlogrdr_purge_store(struct device * dev,
537 				    struct device_attribute *attr,
538 				    const char * buf, size_t count)
539 {
540 
541 	char cp_command[80];
542 	char cp_response[80];
543 	struct vmlogrdr_priv_t *priv = dev->driver_data;
544 
545 	if (buf[0] != '1')
546 		return -EINVAL;
547 
548 	memset(cp_command, 0x00, sizeof(cp_command));
549 	memset(cp_response, 0x00, sizeof(cp_response));
550 
551         /*
552 	 * The recording command needs to be called with option QID
553 	 * for guests that have previlege classes A or B.
554 	 * Other guests will not recognize the command and we have to
555 	 * issue the same command without the QID parameter.
556 	 */
557 
558 	if (recording_class_AB)
559 		snprintf(cp_command, sizeof(cp_command),
560 			 "RECORDING %s PURGE QID * ",
561 			 priv->recording_name);
562 	else
563 		snprintf(cp_command, sizeof(cp_command),
564 			 "RECORDING %s PURGE ",
565 			 priv->recording_name);
566 
567 	cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
568 
569 	return count;
570 }
571 
572 
573 static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
574 
575 
576 static ssize_t vmlogrdr_autorecording_store(struct device *dev,
577 					    struct device_attribute *attr,
578 					    const char *buf, size_t count)
579 {
580 	struct vmlogrdr_priv_t *priv = dev->driver_data;
581 	ssize_t ret = count;
582 
583 	switch (buf[0]) {
584 	case '0':
585 		priv->autorecording=0;
586 		break;
587 	case '1':
588 		priv->autorecording=1;
589 		break;
590 	default:
591 		ret = -EINVAL;
592 	}
593 	return ret;
594 }
595 
596 
597 static ssize_t vmlogrdr_autorecording_show(struct device *dev,
598 					   struct device_attribute *attr,
599 					   char *buf)
600 {
601 	struct vmlogrdr_priv_t *priv = dev->driver_data;
602 	return sprintf(buf, "%u\n", priv->autorecording);
603 }
604 
605 
606 static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
607 		   vmlogrdr_autorecording_store);
608 
609 
610 static ssize_t vmlogrdr_recording_store(struct device * dev,
611 					struct device_attribute *attr,
612 					const char * buf, size_t count)
613 {
614 	struct vmlogrdr_priv_t *priv = dev->driver_data;
615 	ssize_t ret;
616 
617 	switch (buf[0]) {
618 	case '0':
619 		ret = vmlogrdr_recording(priv,0,0);
620 		break;
621 	case '1':
622 		ret = vmlogrdr_recording(priv,1,0);
623 		break;
624 	default:
625 		ret = -EINVAL;
626 	}
627 	if (ret)
628 		return ret;
629 	else
630 		return count;
631 
632 }
633 
634 
635 static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
636 
637 
638 static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
639 					      char *buf)
640 {
641 
642 	char cp_command[] = "QUERY RECORDING ";
643 	int len;
644 
645 	cpcmd(cp_command, buf, 4096, NULL);
646 	len = strlen(buf);
647 	return len;
648 }
649 
650 
651 static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
652 		   NULL);
653 
654 static struct attribute *vmlogrdr_attrs[] = {
655 	&dev_attr_autopurge.attr,
656 	&dev_attr_purge.attr,
657 	&dev_attr_autorecording.attr,
658 	&dev_attr_recording.attr,
659 	NULL,
660 };
661 
662 static struct attribute_group vmlogrdr_attr_group = {
663 	.attrs = vmlogrdr_attrs,
664 };
665 
666 static struct class *vmlogrdr_class;
667 static struct device_driver vmlogrdr_driver = {
668 	.name = "vmlogrdr",
669 	.bus  = &iucv_bus,
670 };
671 
672 
673 static int vmlogrdr_register_driver(void)
674 {
675 	int ret;
676 
677 	/* Register with iucv driver */
678 	ret = iucv_register(&vmlogrdr_iucv_handler, 1);
679 	if (ret)
680 		goto out;
681 
682 	ret = driver_register(&vmlogrdr_driver);
683 	if (ret)
684 		goto out_iucv;
685 
686 	ret = driver_create_file(&vmlogrdr_driver,
687 				 &driver_attr_recording_status);
688 	if (ret)
689 		goto out_driver;
690 
691 	vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
692 	if (IS_ERR(vmlogrdr_class)) {
693 		ret = PTR_ERR(vmlogrdr_class);
694 		vmlogrdr_class = NULL;
695 		goto out_attr;
696 	}
697 	return 0;
698 
699 out_attr:
700 	driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
701 out_driver:
702 	driver_unregister(&vmlogrdr_driver);
703 out_iucv:
704 	iucv_unregister(&vmlogrdr_iucv_handler, 1);
705 out:
706 	return ret;
707 }
708 
709 
710 static void vmlogrdr_unregister_driver(void)
711 {
712 	class_destroy(vmlogrdr_class);
713 	vmlogrdr_class = NULL;
714 	driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
715 	driver_unregister(&vmlogrdr_driver);
716 	iucv_unregister(&vmlogrdr_iucv_handler, 1);
717 }
718 
719 
720 static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
721 {
722 	struct device *dev;
723 	int ret;
724 
725 	dev = kzalloc(sizeof(struct device), GFP_KERNEL);
726 	if (dev) {
727 		dev_set_name(dev, priv->internal_name);
728 		dev->bus = &iucv_bus;
729 		dev->parent = iucv_root;
730 		dev->driver = &vmlogrdr_driver;
731 		/*
732 		 * The release function could be called after the
733 		 * module has been unloaded. It's _only_ task is to
734 		 * free the struct. Therefore, we specify kfree()
735 		 * directly here. (Probably a little bit obfuscating
736 		 * but legitime ...).
737 		 */
738 		dev->release = (void (*)(struct device *))kfree;
739 	} else
740 		return -ENOMEM;
741 	ret = device_register(dev);
742 	if (ret)
743 		return ret;
744 
745 	ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
746 	if (ret) {
747 		device_unregister(dev);
748 		return ret;
749 	}
750 	priv->class_device = device_create(vmlogrdr_class, dev,
751 					   MKDEV(vmlogrdr_major,
752 						 priv->minor_num),
753 					   priv, "%s", dev_name(dev));
754 	if (IS_ERR(priv->class_device)) {
755 		ret = PTR_ERR(priv->class_device);
756 		priv->class_device=NULL;
757 		sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
758 		device_unregister(dev);
759 		return ret;
760 	}
761 	priv->device = dev;
762 	return 0;
763 }
764 
765 
766 static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
767 {
768 	device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
769 	if (priv->device != NULL) {
770 		sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
771 		device_unregister(priv->device);
772 		priv->device=NULL;
773 	}
774 	return 0;
775 }
776 
777 
778 static int vmlogrdr_register_cdev(dev_t dev)
779 {
780 	int rc = 0;
781 	vmlogrdr_cdev = cdev_alloc();
782 	if (!vmlogrdr_cdev) {
783 		return -ENOMEM;
784 	}
785 	vmlogrdr_cdev->owner = THIS_MODULE;
786 	vmlogrdr_cdev->ops = &vmlogrdr_fops;
787 	vmlogrdr_cdev->dev = dev;
788 	rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
789 	if (!rc)
790 		return 0;
791 
792 	// cleanup: cdev is not fully registered, no cdev_del here!
793 	kobject_put(&vmlogrdr_cdev->kobj);
794 	vmlogrdr_cdev=NULL;
795 	return rc;
796 }
797 
798 
799 static void vmlogrdr_cleanup(void)
800 {
801         int i;
802 
803 	if (vmlogrdr_cdev) {
804 		cdev_del(vmlogrdr_cdev);
805 		vmlogrdr_cdev=NULL;
806 	}
807 	for (i=0; i < MAXMINOR; ++i ) {
808 		vmlogrdr_unregister_device(&sys_ser[i]);
809 		free_page((unsigned long)sys_ser[i].buffer);
810 	}
811 	vmlogrdr_unregister_driver();
812 	if (vmlogrdr_major) {
813 		unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
814 		vmlogrdr_major=0;
815 	}
816 }
817 
818 
819 static int __init vmlogrdr_init(void)
820 {
821 	int rc;
822 	int i;
823 	dev_t dev;
824 
825 	if (! MACHINE_IS_VM) {
826 		printk (KERN_ERR "vmlogrdr: not running under VM, "
827 				"driver not loaded.\n");
828 		return -ENODEV;
829 	}
830 
831         recording_class_AB = vmlogrdr_get_recording_class_AB();
832 
833 	rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
834 	if (rc)
835 		return rc;
836 	vmlogrdr_major = MAJOR(dev);
837 
838 	rc=vmlogrdr_register_driver();
839 	if (rc)
840 		goto cleanup;
841 
842 	for (i=0; i < MAXMINOR; ++i ) {
843 		sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
844 		if (!sys_ser[i].buffer) {
845 			rc = -ENOMEM;
846 			break;
847 		}
848 		sys_ser[i].current_position = sys_ser[i].buffer;
849 		rc=vmlogrdr_register_device(&sys_ser[i]);
850 		if (rc)
851 			break;
852 	}
853 	if (rc)
854 		goto cleanup;
855 
856 	rc = vmlogrdr_register_cdev(dev);
857 	if (rc)
858 		goto cleanup;
859 	return 0;
860 
861 cleanup:
862 	vmlogrdr_cleanup();
863 	return rc;
864 }
865 
866 
867 static void __exit vmlogrdr_exit(void)
868 {
869 	vmlogrdr_cleanup();
870 	return;
871 }
872 
873 
874 module_init(vmlogrdr_init);
875 module_exit(vmlogrdr_exit);
876