hypfs_dbfs.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Hypervisor filesystem for Linux on s390 - debugfs interface
  3. *
  4. * Copyright IBM Corp. 2010
  5. * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
  6. */
  7. #include <linux/slab.h>
  8. #include "hypfs.h"
  9. static struct dentry *dbfs_dir;
  10. static struct hypfs_dbfs_data *hypfs_dbfs_data_alloc(struct hypfs_dbfs_file *f)
  11. {
  12. struct hypfs_dbfs_data *data;
  13. data = kmalloc(sizeof(*data), GFP_KERNEL);
  14. if (!data)
  15. return NULL;
  16. data->dbfs_file = f;
  17. return data;
  18. }
  19. static void hypfs_dbfs_data_free(struct hypfs_dbfs_data *data)
  20. {
  21. data->dbfs_file->data_free(data->buf_free_ptr);
  22. kfree(data);
  23. }
  24. static ssize_t dbfs_read(struct file *file, char __user *buf,
  25. size_t size, loff_t *ppos)
  26. {
  27. struct hypfs_dbfs_data *data;
  28. struct hypfs_dbfs_file *df;
  29. ssize_t rc;
  30. if (*ppos != 0)
  31. return 0;
  32. df = file_inode(file)->i_private;
  33. mutex_lock(&df->lock);
  34. data = hypfs_dbfs_data_alloc(df);
  35. if (!data) {
  36. mutex_unlock(&df->lock);
  37. return -ENOMEM;
  38. }
  39. rc = df->data_create(&data->buf, &data->buf_free_ptr, &data->size);
  40. if (rc) {
  41. mutex_unlock(&df->lock);
  42. kfree(data);
  43. return rc;
  44. }
  45. mutex_unlock(&df->lock);
  46. rc = simple_read_from_buffer(buf, size, ppos, data->buf, data->size);
  47. hypfs_dbfs_data_free(data);
  48. return rc;
  49. }
  50. static long dbfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  51. {
  52. struct hypfs_dbfs_file *df = file_inode(file)->i_private;
  53. long rc;
  54. mutex_lock(&df->lock);
  55. if (df->unlocked_ioctl)
  56. rc = df->unlocked_ioctl(file, cmd, arg);
  57. else
  58. rc = -ENOTTY;
  59. mutex_unlock(&df->lock);
  60. return rc;
  61. }
  62. static const struct file_operations dbfs_ops = {
  63. .read = dbfs_read,
  64. .llseek = no_llseek,
  65. .unlocked_ioctl = dbfs_ioctl,
  66. };
  67. int hypfs_dbfs_create_file(struct hypfs_dbfs_file *df)
  68. {
  69. df->dentry = debugfs_create_file(df->name, 0400, dbfs_dir, df,
  70. &dbfs_ops);
  71. if (IS_ERR(df->dentry))
  72. return PTR_ERR(df->dentry);
  73. mutex_init(&df->lock);
  74. return 0;
  75. }
  76. void hypfs_dbfs_remove_file(struct hypfs_dbfs_file *df)
  77. {
  78. debugfs_remove(df->dentry);
  79. }
  80. int hypfs_dbfs_init(void)
  81. {
  82. dbfs_dir = debugfs_create_dir("s390_hypfs", NULL);
  83. return PTR_ERR_OR_ZERO(dbfs_dir);
  84. }
  85. void hypfs_dbfs_exit(void)
  86. {
  87. debugfs_remove(dbfs_dir);
  88. }