1 #include <linux/init.h>
2 #include <linux/debugfs.h>
3 #include <linux/slab.h>
4 #include <linux/module.h>
8 static struct dentry
*d_xen_debug
;
10 struct dentry
* __init
xen_init_debugfs(void)
13 d_xen_debug
= debugfs_create_dir("xen", NULL
);
16 pr_warning("Could not create 'xen' debugfs directory\n");
28 static int u32_array_open(struct inode
*inode
, struct file
*file
)
30 file
->private_data
= NULL
;
31 return nonseekable_open(inode
, file
);
34 static size_t format_array(char *buf
, size_t bufsize
, const char *fmt
,
35 u32
*array
, unsigned array_size
)
40 for(i
= 0; i
< array_size
; i
++) {
43 len
= snprintf(buf
, bufsize
, fmt
, array
[i
]);
44 len
++; /* ' ' or '\n' */
50 buf
[-1] = (i
== array_size
-1) ? '\n' : ' ';
61 static char *format_array_alloc(const char *fmt
, u32
*array
, unsigned array_size
)
63 size_t len
= format_array(NULL
, 0, fmt
, array
, array_size
);
66 ret
= kmalloc(len
, GFP_KERNEL
);
70 format_array(ret
, len
, fmt
, array
, array_size
);
74 static ssize_t
u32_array_read(struct file
*file
, char __user
*buf
, size_t len
,
77 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
78 struct array_data
*data
= inode
->i_private
;
82 if (file
->private_data
) {
83 kfree(file
->private_data
);
84 file
->private_data
= NULL
;
87 file
->private_data
= format_array_alloc("%u", data
->array
, data
->elements
);
91 if (file
->private_data
)
92 size
= strlen(file
->private_data
);
94 return simple_read_from_buffer(buf
, len
, ppos
, file
->private_data
, size
);
97 static int xen_array_release(struct inode
*inode
, struct file
*file
)
99 kfree(file
->private_data
);
104 static const struct file_operations u32_array_fops
= {
105 .owner
= THIS_MODULE
,
106 .open
= u32_array_open
,
107 .release
= xen_array_release
,
108 .read
= u32_array_read
,
112 struct dentry
*xen_debugfs_create_u32_array(const char *name
, mode_t mode
,
113 struct dentry
*parent
,
114 u32
*array
, unsigned elements
)
116 struct array_data
*data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
122 data
->elements
= elements
;
124 return debugfs_create_file(name
, mode
, parent
, data
, &u32_array_fops
);