vmlogrdr.c 21 KB

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