vmlogrdr.c 21 KB

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