vmlogrdr.c 21 KB

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