hypfs_dbfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if (mutex_lock_interruptible(&df->lock))
  35. return -ERESTARTSYS;
  36. data = hypfs_dbfs_data_alloc(df);
  37. if (!data) {
  38. mutex_unlock(&df->lock);
  39. return -ENOMEM;
  40. }
  41. rc = df->data_create(&data->buf, &data->buf_free_ptr, &data->size);
  42. if (rc) {
  43. mutex_unlock(&df->lock);
  44. kfree(data);
  45. return rc;
  46. }
  47. mutex_unlock(&df->lock);
  48. rc = simple_read_from_buffer(buf, size, ppos, data->buf, data->size);
  49. hypfs_dbfs_data_free(data);
  50. return rc;
  51. }
  52. static long dbfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  53. {
  54. struct hypfs_dbfs_file *df = file_inode(file)->i_private;
  55. long rc;
  56. mutex_lock(&df->lock);
  57. if (df->unlocked_ioctl)
  58. rc = df->unlocked_ioctl(file, cmd, arg);
  59. else
  60. rc = -ENOTTY;
  61. mutex_unlock(&df->lock);
  62. return rc;
  63. }
  64. static const struct file_operations dbfs_ops = {
  65. .read = dbfs_read,
  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. static int __init hypfs_dbfs_init(void)
  79. {
  80. int rc = -ENODATA;
  81. dbfs_dir = debugfs_create_dir("s390_hypfs", NULL);
  82. if (hypfs_diag_init())
  83. goto fail_dbfs_exit;
  84. if (hypfs_vm_init())
  85. goto fail_hypfs_diag_exit;
  86. hypfs_sprp_init();
  87. if (hypfs_diag0c_init())
  88. goto fail_hypfs_sprp_exit;
  89. rc = hypfs_fs_init();
  90. if (rc)
  91. goto fail_hypfs_diag0c_exit;
  92. return 0;
  93. fail_hypfs_diag0c_exit:
  94. hypfs_diag0c_exit();
  95. fail_hypfs_sprp_exit:
  96. hypfs_sprp_exit();
  97. hypfs_vm_exit();
  98. fail_hypfs_diag_exit:
  99. hypfs_diag_exit();
  100. pr_err("Initialization of hypfs failed with rc=%i\n", rc);
  101. fail_dbfs_exit:
  102. debugfs_remove(dbfs_dir);
  103. return rc;
  104. }
  105. device_initcall(hypfs_dbfs_init)