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