blob: d101328a29806a067da1eaef90c3a04f5ed8a651 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/vmlogrdr.c
3 * character device driver for reading z/VM system service records
4 *
5 *
Martin Schwidefskyc9101c52007-02-08 13:40:41 -08006 * Copyright 2004 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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>
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080024#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kmod.h>
26#include <linux/cdev.h>
27#include <linux/device.h>
Jonathan Corbet764a4a82008-05-15 10:01:17 -060028#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/string.h>
30
31
32
33MODULE_AUTHOR
34 ("(C) 2004 IBM Corporation by Xenia Tkatschow (xenia@us.ibm.com)\n"
35 " Stefan Weinhuber (wein@de.ibm.com)");
36MODULE_DESCRIPTION ("Character device driver for reading z/VM "
37 "system service records.");
38MODULE_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 */
60struct vmlogrdr_priv_t {
61 char system_service[8];
62 char internal_name[8];
63 char recording_name[8];
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080064 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int connection_established;
66 int iucv_path_severed;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -080067 struct iucv_message local_interrupt_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 atomic_t receive_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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;
Cornelia Huck7f021ce2007-10-22 12:52:42 +020078 struct device *class_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int autorecording;
80 int autopurge;
81};
82
83
84/*
85 * File operation structure for vmlogrdr devices
86 */
87static int vmlogrdr_open(struct inode *, struct file *);
88static int vmlogrdr_release(struct inode *, struct file *);
Heiko Carstensd2c993d2006-07-12 16:41:55 +020089static ssize_t vmlogrdr_read (struct file *filp, char __user *data,
90 size_t count, loff_t * ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080092static const struct file_operations vmlogrdr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 .owner = THIS_MODULE,
94 .open = vmlogrdr_open,
95 .release = vmlogrdr_release,
96 .read = vmlogrdr_read,
97};
98
99
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800100static void vmlogrdr_iucv_path_complete(struct iucv_path *, u8 ipuser[16]);
101static void vmlogrdr_iucv_path_severed(struct iucv_path *, u8 ipuser[16]);
102static void vmlogrdr_iucv_message_pending(struct iucv_path *,
103 struct iucv_message *);
104
105
106static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
112
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100113static DECLARE_WAIT_QUEUE_HEAD(conn_wait_queue);
114static DECLARE_WAIT_QUEUE_HEAD(read_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
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
123static 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,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200129 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[0].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .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,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200138 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[1].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .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,
Milind Arun Choudharycb629a02007-04-27 16:02:01 +0200147 .priv_lock = __SPIN_LOCK_UNLOCKED(sys_ser[2].priv_lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .autorecording = 1,
149 .autopurge = 1,
150 }
151};
152
153#define MAXMINOR (sizeof(sys_ser)/sizeof(struct vmlogrdr_priv_t))
154
155static char FENCE[] = {"EOR"};
156static int vmlogrdr_major = 0;
157static struct cdev *vmlogrdr_cdev = NULL;
158static int recording_class_AB;
159
160
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800161static void vmlogrdr_iucv_path_complete(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800163 struct vmlogrdr_priv_t * logptr = path->private;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 spin_lock(&logptr->priv_lock);
166 logptr->connection_established = 1;
167 spin_unlock(&logptr->priv_lock);
168 wake_up(&conn_wait_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800172static void vmlogrdr_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800174 struct vmlogrdr_priv_t * logptr = path->private;
175 u8 reason = (u8) ipuser[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 printk (KERN_ERR "vmlogrdr: connection severed with"
178 " reason %i\n", reason);
179
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800180 iucv_path_sever(path, NULL);
181 kfree(path);
182 logptr->path = NULL;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 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
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800195static void vmlogrdr_iucv_message_pending(struct iucv_path *path,
196 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800198 struct vmlogrdr_priv_t * logptr = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
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);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800206 memcpy(&logptr->local_interrupt_buffer, msg, sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 atomic_inc(&logptr->receive_ready);
208 spin_unlock(&logptr->priv_lock);
209 wake_up_interruptible(&read_wait_queue);
210}
211
212
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800213static int vmlogrdr_get_recording_class_AB(void)
214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 char cp_command[]="QUERY COMMAND RECORDING ";
216 char cp_response[80];
217 char *tail;
218 int len,i;
219
220 printk (KERN_DEBUG "vmlogrdr: query command: %s\n", cp_command);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700221 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 printk (KERN_DEBUG "vmlogrdr: response: %s", cp_response);
223 len = strnlen(cp_response,sizeof(cp_response));
224 // now the parsing
225 tail=strnchr(cp_response,len,'=');
226 if (!tail)
227 return 0;
228 tail++;
229 if (!strncmp("ANY",tail,3))
230 return 1;
231 if (!strncmp("NONE",tail,4))
232 return 0;
233 /*
234 * expect comma separated list of classes here, if one of them
235 * is A or B return 1 otherwise 0
236 */
237 for (i=tail-cp_response; i<len; i++)
238 if ( cp_response[i]=='A' || cp_response[i]=='B' )
239 return 1;
240 return 0;
241}
242
243
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800244static int vmlogrdr_recording(struct vmlogrdr_priv_t * logptr,
245 int action, int purge)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 char cp_command[80];
249 char cp_response[160];
250 char *onoff, *qid_string;
251
252 memset(cp_command, 0x00, sizeof(cp_command));
253 memset(cp_response, 0x00, sizeof(cp_response));
254
255 onoff = ((action == 1) ? "ON" : "OFF");
256 qid_string = ((recording_class_AB == 1) ? " QID * " : "");
257
258 /*
259 * The recording commands needs to be called with option QID
260 * for guests that have previlege classes A or B.
261 * Purging has to be done as separate step, because recording
262 * can't be switched on as long as records are on the queue.
263 * Doing both at the same time doesn't work.
264 */
265
266 if (purge) {
267 snprintf(cp_command, sizeof(cp_command),
268 "RECORDING %s PURGE %s",
269 logptr->recording_name,
270 qid_string);
271
272 printk (KERN_DEBUG "vmlogrdr: recording command: %s\n",
273 cp_command);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700274 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 printk (KERN_DEBUG "vmlogrdr: recording response: %s",
276 cp_response);
277 }
278
279 memset(cp_command, 0x00, sizeof(cp_command));
280 memset(cp_response, 0x00, sizeof(cp_response));
281 snprintf(cp_command, sizeof(cp_command), "RECORDING %s %s %s",
282 logptr->recording_name,
283 onoff,
284 qid_string);
285
286 printk (KERN_DEBUG "vmlogrdr: recording command: %s\n", cp_command);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700287 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 printk (KERN_DEBUG "vmlogrdr: recording response: %s",
289 cp_response);
290 /* The recording command will usually answer with 'Command complete'
291 * on success, but when the specific service was never connected
292 * before then there might be an additional informational message
293 * 'HCPCRC8072I Recording entry not found' before the
294 * 'Command complete'. So I use strstr rather then the strncmp.
295 */
296 if (strstr(cp_response,"Command complete"))
297 return 0;
298 else
299 return -EIO;
300
301}
302
303
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800304static int vmlogrdr_open (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int dev_num = 0;
307 struct vmlogrdr_priv_t * logptr = NULL;
308 int connect_rc = 0;
309 int ret;
310
311 dev_num = iminor(inode);
312 if (dev_num > MAXMINOR)
313 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 logptr = &sys_ser[dev_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /*
317 * only allow for blocking reads to be open
318 */
319 if (filp->f_flags & O_NONBLOCK)
320 return -ENOSYS;
321
322 /* Besure this device hasn't already been opened */
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600323 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 spin_lock_bh(&logptr->priv_lock);
325 if (logptr->dev_in_use) {
326 spin_unlock_bh(&logptr->priv_lock);
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600327 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800330 logptr->dev_in_use = 1;
331 logptr->connection_established = 0;
332 logptr->iucv_path_severed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 atomic_set(&logptr->receive_ready, 0);
334 logptr->buffer_free = 1;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800335 spin_unlock_bh(&logptr->priv_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* set the file options */
338 filp->private_data = logptr;
339 filp->f_op = &vmlogrdr_fops;
340
341 /* start recording for this service*/
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800342 if (logptr->autorecording) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ret = vmlogrdr_recording(logptr,1,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800344 if (ret)
345 printk (KERN_WARNING "vmlogrdr: failed to start "
346 "recording automatically\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /* create connection to the system service */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800350 logptr->path = iucv_path_alloc(10, 0, GFP_KERNEL);
351 if (!logptr->path)
352 goto out_dev;
353 connect_rc = iucv_path_connect(logptr->path, &vmlogrdr_iucv_handler,
354 logptr->system_service, NULL, NULL,
355 logptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (connect_rc) {
357 printk (KERN_ERR "vmlogrdr: iucv connection to %s "
358 "failed with rc %i \n", logptr->system_service,
359 connect_rc);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800360 goto out_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
363 /* We've issued the connect and now we must wait for a
364 * ConnectionComplete or ConnectinSevered Interrupt
365 * before we can continue to process.
366 */
367 wait_event(conn_wait_queue, (logptr->connection_established)
368 || (logptr->iucv_path_severed));
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800369 if (logptr->iucv_path_severed)
370 goto out_record;
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600371 ret = nonseekable_open(inode, filp);
372 unlock_kernel();
373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800375out_record:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (logptr->autorecording)
377 vmlogrdr_recording(logptr,0,logptr->autopurge);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800378out_path:
379 kfree(logptr->path); /* kfree(NULL) is ok. */
380 logptr->path = NULL;
381out_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 logptr->dev_in_use = 0;
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600383 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800388static int vmlogrdr_release (struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 int ret;
391
392 struct vmlogrdr_priv_t * logptr = filp->private_data;
393
Ursula Braun66b494a2007-04-27 16:01:52 +0200394 iucv_path_sever(logptr->path, NULL);
395 kfree(logptr->path);
396 logptr->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (logptr->autorecording) {
398 ret = vmlogrdr_recording(logptr,0,logptr->autopurge);
399 if (ret)
400 printk (KERN_WARNING "vmlogrdr: failed to stop "
401 "recording automatically\n");
402 }
403 logptr->dev_in_use = 0;
404
405 return 0;
406}
407
408
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800409static int vmlogrdr_receive_data(struct vmlogrdr_priv_t *priv)
410{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int rc, *temp;
412 /* we need to keep track of two data sizes here:
413 * The number of bytes we need to receive from iucv and
414 * the total number of bytes we actually write into the buffer.
415 */
416 int user_data_count, iucv_data_count;
417 char * buffer;
418
419 if (atomic_read(&priv->receive_ready)) {
420 spin_lock_bh(&priv->priv_lock);
421 if (priv->residual_length){
422 /* receive second half of a record */
423 iucv_data_count = priv->residual_length;
424 user_data_count = 0;
425 buffer = priv->buffer;
426 } else {
427 /* receive a new record:
428 * We need to return the total length of the record
429 * + size of FENCE in the first 4 bytes of the buffer.
430 */
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800431 iucv_data_count = priv->local_interrupt_buffer.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 user_data_count = sizeof(int);
433 temp = (int*)priv->buffer;
434 *temp= iucv_data_count + sizeof(FENCE);
435 buffer = priv->buffer + sizeof(int);
436 }
437 /*
438 * If the record is bigger then our buffer, we receive only
439 * a part of it. We can get the rest later.
440 */
441 if (iucv_data_count > NET_BUFFER_SIZE)
442 iucv_data_count = NET_BUFFER_SIZE;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800443 rc = iucv_message_receive(priv->path,
444 &priv->local_interrupt_buffer,
445 0, buffer, iucv_data_count,
446 &priv->residual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_unlock_bh(&priv->priv_lock);
448 /* An rc of 5 indicates that the record was bigger then
449 * the buffer, which is OK for us. A 9 indicates that the
450 * record was purged befor we could receive it.
451 */
452 if (rc == 5)
453 rc = 0;
454 if (rc == 9)
455 atomic_set(&priv->receive_ready, 0);
456 } else {
457 rc = 1;
458 }
459 if (!rc) {
460 priv->buffer_free = 0;
461 user_data_count += iucv_data_count;
462 priv->current_position = priv->buffer;
463 if (priv->residual_length == 0){
464 /* the whole record has been captured,
465 * now add the fence */
466 atomic_dec(&priv->receive_ready);
467 buffer = priv->buffer + user_data_count;
468 memcpy(buffer, FENCE, sizeof(FENCE));
469 user_data_count += sizeof(FENCE);
470 }
471 priv->remaining = user_data_count;
472 }
473
474 return rc;
475}
476
477
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800478static ssize_t vmlogrdr_read(struct file *filp, char __user *data,
479 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 int rc;
482 struct vmlogrdr_priv_t * priv = filp->private_data;
483
484 while (priv->buffer_free) {
485 rc = vmlogrdr_receive_data(priv);
486 if (rc) {
487 rc = wait_event_interruptible(read_wait_queue,
488 atomic_read(&priv->receive_ready));
489 if (rc)
490 return rc;
491 }
492 }
493 /* copy only up to end of record */
494 if (count > priv->remaining)
495 count = priv->remaining;
496
497 if (copy_to_user(data, priv->current_position, count))
498 return -EFAULT;
499
500 *ppos += count;
501 priv->current_position += count;
502 priv->remaining -= count;
503
504 /* if all data has been transferred, set buffer free */
505 if (priv->remaining == 0)
506 priv->buffer_free = 1;
507
508 return count;
509}
510
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800511static ssize_t vmlogrdr_autopurge_store(struct device * dev,
512 struct device_attribute *attr,
513 const char * buf, size_t count)
514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct vmlogrdr_priv_t *priv = dev->driver_data;
516 ssize_t ret = count;
517
518 switch (buf[0]) {
519 case '0':
520 priv->autopurge=0;
521 break;
522 case '1':
523 priv->autopurge=1;
524 break;
525 default:
526 ret = -EINVAL;
527 }
528 return ret;
529}
530
531
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800532static ssize_t vmlogrdr_autopurge_show(struct device *dev,
533 struct device_attribute *attr,
534 char *buf)
535{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 struct vmlogrdr_priv_t *priv = dev->driver_data;
537 return sprintf(buf, "%u\n", priv->autopurge);
538}
539
540
541static DEVICE_ATTR(autopurge, 0644, vmlogrdr_autopurge_show,
542 vmlogrdr_autopurge_store);
543
544
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800545static ssize_t vmlogrdr_purge_store(struct device * dev,
546 struct device_attribute *attr,
547 const char * buf, size_t count)
548{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 char cp_command[80];
551 char cp_response[80];
552 struct vmlogrdr_priv_t *priv = dev->driver_data;
553
554 if (buf[0] != '1')
555 return -EINVAL;
556
557 memset(cp_command, 0x00, sizeof(cp_command));
558 memset(cp_response, 0x00, sizeof(cp_response));
559
560 /*
561 * The recording command needs to be called with option QID
562 * for guests that have previlege classes A or B.
563 * Other guests will not recognize the command and we have to
564 * issue the same command without the QID parameter.
565 */
566
567 if (recording_class_AB)
568 snprintf(cp_command, sizeof(cp_command),
569 "RECORDING %s PURGE QID * ",
570 priv->recording_name);
571 else
572 snprintf(cp_command, sizeof(cp_command),
573 "RECORDING %s PURGE ",
574 priv->recording_name);
575
576 printk (KERN_DEBUG "vmlogrdr: recording command: %s\n", cp_command);
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700577 cpcmd(cp_command, cp_response, sizeof(cp_response), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 printk (KERN_DEBUG "vmlogrdr: recording response: %s",
579 cp_response);
580
581 return count;
582}
583
584
585static DEVICE_ATTR(purge, 0200, NULL, vmlogrdr_purge_store);
586
587
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800588static ssize_t vmlogrdr_autorecording_store(struct device *dev,
589 struct device_attribute *attr,
590 const char *buf, size_t count)
591{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct vmlogrdr_priv_t *priv = dev->driver_data;
593 ssize_t ret = count;
594
595 switch (buf[0]) {
596 case '0':
597 priv->autorecording=0;
598 break;
599 case '1':
600 priv->autorecording=1;
601 break;
602 default:
603 ret = -EINVAL;
604 }
605 return ret;
606}
607
608
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800609static ssize_t vmlogrdr_autorecording_show(struct device *dev,
610 struct device_attribute *attr,
611 char *buf)
612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct vmlogrdr_priv_t *priv = dev->driver_data;
614 return sprintf(buf, "%u\n", priv->autorecording);
615}
616
617
618static DEVICE_ATTR(autorecording, 0644, vmlogrdr_autorecording_show,
619 vmlogrdr_autorecording_store);
620
621
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800622static ssize_t vmlogrdr_recording_store(struct device * dev,
623 struct device_attribute *attr,
624 const char * buf, size_t count)
625{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct vmlogrdr_priv_t *priv = dev->driver_data;
627 ssize_t ret;
628
629 switch (buf[0]) {
630 case '0':
631 ret = vmlogrdr_recording(priv,0,0);
632 break;
633 case '1':
634 ret = vmlogrdr_recording(priv,1,0);
635 break;
636 default:
637 ret = -EINVAL;
638 }
639 if (ret)
640 return ret;
641 else
642 return count;
643
644}
645
646
647static DEVICE_ATTR(recording, 0200, NULL, vmlogrdr_recording_store);
648
649
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800650static ssize_t vmlogrdr_recording_status_show(struct device_driver *driver,
651 char *buf)
652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 char cp_command[] = "QUERY RECORDING ";
655 int len;
656
Christian Borntraeger6b979de2005-06-25 14:55:32 -0700657 cpcmd(cp_command, buf, 4096, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 len = strlen(buf);
659 return len;
660}
661
662
663static DRIVER_ATTR(recording_status, 0444, vmlogrdr_recording_status_show,
664 NULL);
665
666static struct attribute *vmlogrdr_attrs[] = {
667 &dev_attr_autopurge.attr,
668 &dev_attr_purge.attr,
669 &dev_attr_autorecording.attr,
670 &dev_attr_recording.attr,
671 NULL,
672};
673
674static struct attribute_group vmlogrdr_attr_group = {
675 .attrs = vmlogrdr_attrs,
676};
677
gregkh@suse.de56b22932005-03-23 10:01:41 -0800678static struct class *vmlogrdr_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static struct device_driver vmlogrdr_driver = {
680 .name = "vmlogrdr",
681 .bus = &iucv_bus,
682};
683
684
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800685static int vmlogrdr_register_driver(void)
686{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int ret;
688
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800689 /* Register with iucv driver */
690 ret = iucv_register(&vmlogrdr_iucv_handler, 1);
691 if (ret) {
Joe Perchesceb3dfb2008-01-26 14:11:10 +0100692 printk (KERN_ERR "vmlogrdr: failed to register with "
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800693 "iucv driver\n");
694 goto out;
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ret = driver_register(&vmlogrdr_driver);
698 if (ret) {
699 printk(KERN_ERR "vmlogrdr: failed to register driver.\n");
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800700 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 ret = driver_create_file(&vmlogrdr_driver,
704 &driver_attr_recording_status);
705 if (ret) {
706 printk(KERN_ERR "vmlogrdr: failed to add driver attribute.\n");
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800707 goto out_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
gregkh@suse.de56b22932005-03-23 10:01:41 -0800710 vmlogrdr_class = class_create(THIS_MODULE, "vmlogrdr");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (IS_ERR(vmlogrdr_class)) {
712 printk(KERN_ERR "vmlogrdr: failed to create class.\n");
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800713 ret = PTR_ERR(vmlogrdr_class);
714 vmlogrdr_class = NULL;
715 goto out_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 return 0;
718
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800719out_attr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800721out_driver:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800723out_iucv:
724 iucv_unregister(&vmlogrdr_iucv_handler, 1);
725out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return ret;
727}
728
729
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800730static void vmlogrdr_unregister_driver(void)
731{
gregkh@suse.de56b22932005-03-23 10:01:41 -0800732 class_destroy(vmlogrdr_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 vmlogrdr_class = NULL;
734 driver_remove_file(&vmlogrdr_driver, &driver_attr_recording_status);
735 driver_unregister(&vmlogrdr_driver);
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800736 iucv_unregister(&vmlogrdr_iucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800740static int vmlogrdr_register_device(struct vmlogrdr_priv_t *priv)
741{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct device *dev;
743 int ret;
744
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800745 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 snprintf(dev->bus_id, BUS_ID_SIZE, "%s",
748 priv->internal_name);
749 dev->bus = &iucv_bus;
750 dev->parent = iucv_root;
751 dev->driver = &vmlogrdr_driver;
752 /*
753 * The release function could be called after the
754 * module has been unloaded. It's _only_ task is to
755 * free the struct. Therefore, we specify kfree()
756 * directly here. (Probably a little bit obfuscating
757 * but legitime ...).
758 */
759 dev->release = (void (*)(struct device *))kfree;
760 } else
761 return -ENOMEM;
762 ret = device_register(dev);
763 if (ret)
764 return ret;
765
766 ret = sysfs_create_group(&dev->kobj, &vmlogrdr_attr_group);
767 if (ret) {
768 device_unregister(dev);
769 return ret;
770 }
Cornelia Huck7f021ce2007-10-22 12:52:42 +0200771 priv->class_device = device_create(vmlogrdr_class, dev,
772 MKDEV(vmlogrdr_major,
773 priv->minor_num),
774 "%s", dev->bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (IS_ERR(priv->class_device)) {
776 ret = PTR_ERR(priv->class_device);
777 priv->class_device=NULL;
778 sysfs_remove_group(&dev->kobj, &vmlogrdr_attr_group);
779 device_unregister(dev);
780 return ret;
781 }
782 dev->driver_data = priv;
783 priv->device = dev;
784 return 0;
785}
786
787
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800788static int vmlogrdr_unregister_device(struct vmlogrdr_priv_t *priv)
789{
Cornelia Huck7f021ce2007-10-22 12:52:42 +0200790 device_destroy(vmlogrdr_class, MKDEV(vmlogrdr_major, priv->minor_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (priv->device != NULL) {
792 sysfs_remove_group(&priv->device->kobj, &vmlogrdr_attr_group);
793 device_unregister(priv->device);
794 priv->device=NULL;
795 }
796 return 0;
797}
798
799
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800800static int vmlogrdr_register_cdev(dev_t dev)
801{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 int rc = 0;
803 vmlogrdr_cdev = cdev_alloc();
804 if (!vmlogrdr_cdev) {
805 return -ENOMEM;
806 }
807 vmlogrdr_cdev->owner = THIS_MODULE;
808 vmlogrdr_cdev->ops = &vmlogrdr_fops;
809 vmlogrdr_cdev->dev = dev;
810 rc = cdev_add(vmlogrdr_cdev, vmlogrdr_cdev->dev, MAXMINOR);
811 if (!rc)
812 return 0;
813
814 // cleanup: cdev is not fully registered, no cdev_del here!
815 kobject_put(&vmlogrdr_cdev->kobj);
816 vmlogrdr_cdev=NULL;
817 return rc;
818}
819
820
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800821static void vmlogrdr_cleanup(void)
822{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 int i;
Martin Schwidefskyc9101c52007-02-08 13:40:41 -0800824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (vmlogrdr_cdev) {
826 cdev_del(vmlogrdr_cdev);
827 vmlogrdr_cdev=NULL;
828 }
829 for (i=0; i < MAXMINOR; ++i ) {
830 vmlogrdr_unregister_device(&sys_ser[i]);
831 free_page((unsigned long)sys_ser[i].buffer);
832 }
833 vmlogrdr_unregister_driver();
834 if (vmlogrdr_major) {
835 unregister_chrdev_region(MKDEV(vmlogrdr_major, 0), MAXMINOR);
836 vmlogrdr_major=0;
837 }
838}
839
840
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200841static int __init vmlogrdr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 int rc;
844 int i;
845 dev_t dev;
846
847 if (! MACHINE_IS_VM) {
848 printk (KERN_ERR "vmlogrdr: not running under VM, "
849 "driver not loaded.\n");
850 return -ENODEV;
851 }
852
853 recording_class_AB = vmlogrdr_get_recording_class_AB();
854
855 rc = alloc_chrdev_region(&dev, 0, MAXMINOR, "vmlogrdr");
856 if (rc)
857 return rc;
858 vmlogrdr_major = MAJOR(dev);
859
860 rc=vmlogrdr_register_driver();
861 if (rc)
862 goto cleanup;
863
864 for (i=0; i < MAXMINOR; ++i ) {
865 sys_ser[i].buffer = (char *) get_zeroed_page(GFP_KERNEL);
866 if (!sys_ser[i].buffer) {
Marcin Slusarz3cb2cea2008-05-15 16:52:32 +0200867 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 break;
869 }
870 sys_ser[i].current_position = sys_ser[i].buffer;
871 rc=vmlogrdr_register_device(&sys_ser[i]);
872 if (rc)
873 break;
874 }
875 if (rc)
876 goto cleanup;
877
878 rc = vmlogrdr_register_cdev(dev);
879 if (rc)
880 goto cleanup;
881 printk (KERN_INFO "vmlogrdr: driver loaded\n");
882 return 0;
883
884cleanup:
885 vmlogrdr_cleanup();
886 printk (KERN_ERR "vmlogrdr: driver not loaded.\n");
887 return rc;
888}
889
890
Christian Borntraegerf60d8912007-07-10 11:24:22 +0200891static void __exit vmlogrdr_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 vmlogrdr_cleanup();
894 printk (KERN_INFO "vmlogrdr: driver unloaded\n");
895 return;
896}
897
898
899module_init(vmlogrdr_init);
900module_exit(vmlogrdr_exit);