vmlogrdr.c 22 KB

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