hypfs_dbfs.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Hypervisor filesystem for Linux on s390 - debugfs interface
  3. *
  4. * Copyright (C) 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. kref_init(&data->kref);
  17. data->dbfs_file = f;
  18. return data;
  19. }
  20. static void hypfs_dbfs_data_free(struct kref *kref)
  21. {
  22. struct hypfs_dbfs_data *data;
  23. data = container_of(kref, struct hypfs_dbfs_data, kref);
  24. data->dbfs_file->data_free(data->buf_free_ptr);
  25. kfree(data);
  26. }
  27. static void data_free_delayed(struct work_struct *work)
  28. {
  29. struct hypfs_dbfs_data *data;
  30. struct hypfs_dbfs_file *df;
  31. df = container_of(work, struct hypfs_dbfs_file, data_free_work.work);
  32. mutex_lock(&df->lock);
  33. data = df->data;
  34. df->data = NULL;
  35. mutex_unlock(&df->lock);
  36. kref_put(&data->kref, hypfs_dbfs_data_free);
  37. }
  38. static ssize_t dbfs_read(struct file *file, char __user *buf,
  39. size_t size, loff_t *ppos)
  40. {
  41. struct hypfs_dbfs_data *data;
  42. struct hypfs_dbfs_file *df;
  43. ssize_t rc;
  44. if (*ppos != 0)
  45. return 0;
  46. df = file->f_path.dentry->d_inode->i_private;
  47. mutex_lock(&df->lock);
  48. if (!df->data) {
  49. data = hypfs_dbfs_data_alloc(df);
  50. if (!data) {
  51. mutex_unlock(&df->lock);
  52. return -ENOMEM;
  53. }
  54. rc = df->data_create(&data->buf, &data->buf_free_ptr,
  55. &data->size);
  56. if (rc) {
  57. mutex_unlock(&df->lock);
  58. kfree(data);
  59. return rc;
  60. }
  61. df->data = data;
  62. schedule_delayed_work(&df->data_free_work, HZ);
  63. }
  64. data = df->data;
  65. kref_get(&data->kref);
  66. mutex_unlock(&df->lock);
  67. rc = simple_read_from_buffer(buf, size, ppos, data->buf, data->size);
  68. kref_put(&data->kref, hypfs_dbfs_data_free);
  69. return rc;
  70. }
  71. static const struct file_operations dbfs_ops = {
  72. .read = dbfs_read,
  73. .llseek = no_llseek,
  74. };
  75. int hypfs_dbfs_create_file(struct hypfs_dbfs_file *df)
  76. {
  77. df->dentry = debugfs_create_file(df->name, 0400, dbfs_dir, df,
  78. &dbfs_ops);
  79. if (IS_ERR(df->dentry))
  80. return PTR_ERR(df->dentry);
  81. mutex_init(&df->lock);
  82. INIT_DELAYED_WORK(&df->data_free_work, data_free_delayed);
  83. return 0;
  84. }
  85. void hypfs_dbfs_remove_file(struct hypfs_dbfs_file *df)
  86. {
  87. debugfs_remove(df->dentry);
  88. }
  89. int hypfs_dbfs_init(void)
  90. {
  91. dbfs_dir = debugfs_create_dir("s390_hypfs", NULL);
  92. if (IS_ERR(dbfs_dir))
  93. return PTR_ERR(dbfs_dir);
  94. return 0;
  95. }
  96. void hypfs_dbfs_exit(void)
  97. {
  98. debugfs_remove(dbfs_dir);
  99. }