hypfs_dbfs.c 2.0 KB

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