hypfs_dbfs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/security.h>
  9. #include <linux/slab.h>
  10. #include "hypfs.h"
  11. static struct dentry *dbfs_dir;
  12. static struct hypfs_dbfs_data *hypfs_dbfs_data_alloc(struct hypfs_dbfs_file *f)
  13. {
  14. struct hypfs_dbfs_data *data;
  15. data = kmalloc_obj(*data);
  16. if (!data)
  17. return NULL;
  18. data->dbfs_file = f;
  19. return data;
  20. }
  21. static void hypfs_dbfs_data_free(struct hypfs_dbfs_data *data)
  22. {
  23. data->dbfs_file->data_free(data->buf_free_ptr);
  24. kfree(data);
  25. }
  26. static ssize_t dbfs_read(struct file *file, char __user *buf,
  27. size_t size, loff_t *ppos)
  28. {
  29. struct hypfs_dbfs_data *data;
  30. struct hypfs_dbfs_file *df;
  31. ssize_t rc;
  32. if (*ppos != 0)
  33. return 0;
  34. df = file_inode(file)->i_private;
  35. if (mutex_lock_interruptible(&df->lock))
  36. return -ERESTARTSYS;
  37. data = hypfs_dbfs_data_alloc(df);
  38. if (!data) {
  39. mutex_unlock(&df->lock);
  40. return -ENOMEM;
  41. }
  42. rc = df->data_create(&data->buf, &data->buf_free_ptr, &data->size);
  43. if (rc) {
  44. mutex_unlock(&df->lock);
  45. kfree(data);
  46. return rc;
  47. }
  48. mutex_unlock(&df->lock);
  49. rc = simple_read_from_buffer(buf, size, ppos, data->buf, data->size);
  50. hypfs_dbfs_data_free(data);
  51. return rc;
  52. }
  53. static long dbfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  54. {
  55. struct hypfs_dbfs_file *df = file_inode(file)->i_private;
  56. long rc;
  57. mutex_lock(&df->lock);
  58. rc = df->unlocked_ioctl(file, cmd, arg);
  59. mutex_unlock(&df->lock);
  60. return rc;
  61. }
  62. static const struct file_operations dbfs_ops_ioctl = {
  63. .read = dbfs_read,
  64. .unlocked_ioctl = dbfs_ioctl,
  65. };
  66. static const struct file_operations dbfs_ops = {
  67. .read = dbfs_read,
  68. };
  69. void hypfs_dbfs_create_file(struct hypfs_dbfs_file *df)
  70. {
  71. const struct file_operations *fops = &dbfs_ops;
  72. if (df->unlocked_ioctl && !security_locked_down(LOCKDOWN_DEBUGFS))
  73. fops = &dbfs_ops_ioctl;
  74. df->dentry = debugfs_create_file(df->name, 0400, dbfs_dir, df, fops);
  75. mutex_init(&df->lock);
  76. }
  77. void hypfs_dbfs_remove_file(struct hypfs_dbfs_file *df)
  78. {
  79. debugfs_remove(df->dentry);
  80. }
  81. static int __init hypfs_dbfs_init(void)
  82. {
  83. int rc = -ENODATA;
  84. dbfs_dir = debugfs_create_dir("s390_hypfs", NULL);
  85. if (hypfs_diag_init())
  86. goto fail_dbfs_exit;
  87. if (hypfs_vm_init())
  88. goto fail_hypfs_diag_exit;
  89. hypfs_sprp_init();
  90. if (hypfs_diag0c_init())
  91. goto fail_hypfs_sprp_exit;
  92. rc = hypfs_fs_init();
  93. if (rc)
  94. goto fail_hypfs_diag0c_exit;
  95. return 0;
  96. fail_hypfs_diag0c_exit:
  97. hypfs_diag0c_exit();
  98. fail_hypfs_sprp_exit:
  99. hypfs_sprp_exit();
  100. hypfs_vm_exit();
  101. fail_hypfs_diag_exit:
  102. hypfs_diag_exit();
  103. pr_err("Initialization of hypfs failed with rc=%i\n", rc);
  104. fail_dbfs_exit:
  105. debugfs_remove(dbfs_dir);
  106. return rc;
  107. }
  108. device_initcall(hypfs_dbfs_init)