blob: 47fe1055c714a34c1348b3a734705a408aff081b [file] [log] [blame]
Michael Holzheu2fcb3682011-01-05 12:47:43 +01001/*
2 * Hypervisor filesystem for Linux on s390 - debugfs interface
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2010
Michael Holzheu2fcb3682011-01-05 12:47:43 +01005 * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
6 */
7
8#include <linux/slab.h>
9#include "hypfs.h"
10
11static struct dentry *dbfs_dir;
12
13static struct hypfs_dbfs_data *hypfs_dbfs_data_alloc(struct hypfs_dbfs_file *f)
14{
15 struct hypfs_dbfs_data *data;
16
17 data = kmalloc(sizeof(*data), GFP_KERNEL);
18 if (!data)
19 return NULL;
20 kref_init(&data->kref);
21 data->dbfs_file = f;
22 return data;
23}
24
25static void hypfs_dbfs_data_free(struct kref *kref)
26{
27 struct hypfs_dbfs_data *data;
28
29 data = container_of(kref, struct hypfs_dbfs_data, kref);
30 data->dbfs_file->data_free(data->buf_free_ptr);
31 kfree(data);
32}
33
34static void data_free_delayed(struct work_struct *work)
35{
36 struct hypfs_dbfs_data *data;
37 struct hypfs_dbfs_file *df;
38
39 df = container_of(work, struct hypfs_dbfs_file, data_free_work.work);
40 mutex_lock(&df->lock);
41 data = df->data;
42 df->data = NULL;
43 mutex_unlock(&df->lock);
44 kref_put(&data->kref, hypfs_dbfs_data_free);
45}
46
47static ssize_t dbfs_read(struct file *file, char __user *buf,
48 size_t size, loff_t *ppos)
49{
50 struct hypfs_dbfs_data *data;
51 struct hypfs_dbfs_file *df;
52 ssize_t rc;
53
54 if (*ppos != 0)
55 return 0;
56
Al Viro496ad9a2013-01-23 17:07:38 -050057 df = file_inode(file)->i_private;
Michael Holzheu2fcb3682011-01-05 12:47:43 +010058 mutex_lock(&df->lock);
59 if (!df->data) {
60 data = hypfs_dbfs_data_alloc(df);
61 if (!data) {
62 mutex_unlock(&df->lock);
63 return -ENOMEM;
64 }
65 rc = df->data_create(&data->buf, &data->buf_free_ptr,
66 &data->size);
67 if (rc) {
68 mutex_unlock(&df->lock);
69 kfree(data);
70 return rc;
71 }
72 df->data = data;
73 schedule_delayed_work(&df->data_free_work, HZ);
74 }
75 data = df->data;
76 kref_get(&data->kref);
77 mutex_unlock(&df->lock);
78
79 rc = simple_read_from_buffer(buf, size, ppos, data->buf, data->size);
80 kref_put(&data->kref, hypfs_dbfs_data_free);
81 return rc;
82}
83
Martin Schwidefsky07be0382014-01-24 09:18:52 +010084static long dbfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
85{
Al Viroa4555892014-10-21 20:11:25 -040086 struct hypfs_dbfs_file *df = file_inode(file)->i_private;
Martin Schwidefsky07be0382014-01-24 09:18:52 +010087 long rc;
88
Martin Schwidefsky07be0382014-01-24 09:18:52 +010089 mutex_lock(&df->lock);
90 if (df->unlocked_ioctl)
91 rc = df->unlocked_ioctl(file, cmd, arg);
92 else
93 rc = -ENOTTY;
94 mutex_unlock(&df->lock);
95 return rc;
96}
97
Michael Holzheu2fcb3682011-01-05 12:47:43 +010098static const struct file_operations dbfs_ops = {
99 .read = dbfs_read,
100 .llseek = no_llseek,
Martin Schwidefsky07be0382014-01-24 09:18:52 +0100101 .unlocked_ioctl = dbfs_ioctl,
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100102};
103
104int hypfs_dbfs_create_file(struct hypfs_dbfs_file *df)
105{
106 df->dentry = debugfs_create_file(df->name, 0400, dbfs_dir, df,
107 &dbfs_ops);
108 if (IS_ERR(df->dentry))
109 return PTR_ERR(df->dentry);
110 mutex_init(&df->lock);
111 INIT_DELAYED_WORK(&df->data_free_work, data_free_delayed);
112 return 0;
113}
114
115void hypfs_dbfs_remove_file(struct hypfs_dbfs_file *df)
116{
117 debugfs_remove(df->dentry);
118}
119
120int hypfs_dbfs_init(void)
121{
122 dbfs_dir = debugfs_create_dir("s390_hypfs", NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930123 return PTR_ERR_OR_ZERO(dbfs_dir);
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100124}
125
126void hypfs_dbfs_exit(void)
127{
128 debugfs_remove(dbfs_dir);
129}