vmlogrdr.c 22 KB

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