| 1 |
/* -*- linux-c -*- |
| 2 |
* |
| 3 |
* debugfs functions |
| 4 |
* Copyright (C) 2009 Red Hat Inc. |
| 5 |
* |
| 6 |
* This file is part of systemtap, and is free software. You can |
| 7 |
* redistribute it and/or modify it under the terms of the GNU General |
| 8 |
* Public License (GPL); either version 2, or (at your option) any |
| 9 |
* later version. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/debugfs.h> |
| 13 |
#include "transport.h" |
| 14 |
#include "../uidgid_compatibility.h" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
/* Always returns zero, we just push all messages on the _stp_ctl_ready_q. */ |
| 20 |
inline static int _stp_debugfs_ctl_write_fs(int type, void *data, unsigned len) |
| 21 |
{ |
| 22 |
return 0; |
| 23 |
} |
| 24 |
|
| 25 |
static struct dentry *__stp_debugfs_root_dir = NULL; // DEBUGFS/systemtap/ |
| 26 |
static struct dentry *__stp_debugfs_module_dir = NULL; // DEBUGFS/systemtap/MODULE/ |
| 27 |
static struct dentry *_stp_cmd_file = NULL; // DEBUGFS/systemtap/MODULE/.cmd |
| 28 |
|
| 29 |
|
| 30 |
static int _stp_debugfs_register_ctl_channel_fs(void) |
| 31 |
{ |
| 32 |
struct dentry *module_dir = _stp_debugfs_get_module_dir(); |
| 33 |
if (module_dir == NULL) { |
| 34 |
errk("no module directory found.\n"); |
| 35 |
return -1; |
| 36 |
} |
| 37 |
|
| 38 |
/* create [debugfs]/systemtap/module_name/.cmd */ |
| 39 |
_stp_cmd_file = debugfs_create_file(".cmd", 0600, module_dir, |
| 40 |
NULL, &_stp_ctl_fops_cmd); |
| 41 |
if (_stp_cmd_file == NULL) { |
| 42 |
errk("Error creating systemtap debugfs entries.\n"); |
| 43 |
return -1; |
| 44 |
} |
| 45 |
else if (IS_ERR(_stp_cmd_file)) { |
| 46 |
_stp_cmd_file = NULL; |
| 47 |
errk("Error creating systemtap debugfs entries: %ld\n", |
| 48 |
-PTR_ERR(_stp_cmd_file)); |
| 49 |
return -1; |
| 50 |
} |
| 51 |
|
| 52 |
_stp_cmd_file->d_inode->i_uid = KUIDT_INIT(_stp_uid); |
| 53 |
_stp_cmd_file->d_inode->i_gid = KGIDT_INIT(_stp_gid); |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
static void _stp_debugfs_unregister_ctl_channel_fs(void) |
| 59 |
{ |
| 60 |
if (_stp_cmd_file) |
| 61 |
debugfs_remove(_stp_cmd_file); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
static int _stp_debugfs_transport_fs_init(const char *module_name) |
| 66 |
{ |
| 67 |
struct dentry *root_dir; |
| 68 |
|
| 69 |
dbug_trans(1, "entry\n"); |
| 70 |
if (module_name == NULL) |
| 71 |
return -1; |
| 72 |
|
| 73 |
if (!_stp_lock_transport_dir()) { |
| 74 |
errk("Couldn't lock transport directory.\n"); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
root_dir = _stp_debugfs_get_root_dir(); |
| 79 |
if (root_dir == NULL) { |
| 80 |
_stp_unlock_transport_dir(); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
|
| 84 |
__stp_debugfs_module_dir = debugfs_create_dir(module_name, root_dir); |
| 85 |
if (!__stp_debugfs_module_dir) { |
| 86 |
errk("Could not create module directory \"%s\"\n", |
| 87 |
module_name); |
| 88 |
_stp_debugfs_remove_root_dir(); |
| 89 |
_stp_unlock_transport_dir(); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
else if (IS_ERR(__stp_debugfs_module_dir)) { |
| 93 |
errk("Could not create module directory \"%s\", error %ld\n", |
| 94 |
module_name, -PTR_ERR(__stp_debugfs_module_dir)); |
| 95 |
_stp_debugfs_remove_root_dir(); |
| 96 |
_stp_unlock_transport_dir(); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
if (_stp_transport_data_fs_init() != 0) { |
| 101 |
debugfs_remove(__stp_debugfs_module_dir); |
| 102 |
__stp_debugfs_module_dir = NULL; |
| 103 |
_stp_debugfs_remove_root_dir(); |
| 104 |
_stp_unlock_transport_dir(); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
_stp_unlock_transport_dir(); |
| 108 |
dbug_trans(1, "returning 0\n"); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
static void _stp_debugfs_transport_fs_close(void) |
| 113 |
{ |
| 114 |
dbug_trans(1, "stp_transport_fs_close\n"); |
| 115 |
_stp_transport_data_fs_close(); |
| 116 |
if (__stp_debugfs_module_dir) { |
| 117 |
if (!_stp_lock_transport_dir()) { |
| 118 |
errk("Couldn't lock transport directory.\n"); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
debugfs_remove(__stp_debugfs_module_dir); |
| 123 |
__stp_debugfs_module_dir = NULL; |
| 124 |
|
| 125 |
_stp_debugfs_remove_root_dir(); |
| 126 |
_stp_unlock_transport_dir(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
static struct dentry *_stp_lockfile = NULL; |
| 132 |
|
| 133 |
static int _stp_lock_transport_dir(void) |
| 134 |
{ |
| 135 |
int numtries = 0; |
| 136 |
|
| 137 |
while ((_stp_lockfile = debugfs_create_dir("systemtap_lock", NULL)) == NULL) { |
| 138 |
if (numtries++ >= 50) |
| 139 |
return 0; |
| 140 |
msleep(50); |
| 141 |
} |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
static void _stp_unlock_transport_dir(void) |
| 146 |
{ |
| 147 |
if (_stp_lockfile) { |
| 148 |
debugfs_remove(_stp_lockfile); |
| 149 |
_stp_lockfile = NULL; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/* _stp_debugfs_get_root_dir() - creates root directory or returns |
| 154 |
* a pointer to it if it already exists. |
| 155 |
* |
| 156 |
* The caller *must* lock the transport directory. |
| 157 |
*/ |
| 158 |
|
| 159 |
static struct dentry *_stp_debugfs_get_root_dir(void) |
| 160 |
{ |
| 161 |
struct file_system_type *fs; |
| 162 |
struct super_block *sb; |
| 163 |
const char *name = "systemtap"; |
| 164 |
|
| 165 |
if (__stp_debugfs_root_dir != NULL) { |
| 166 |
return __stp_debugfs_root_dir; |
| 167 |
} |
| 168 |
|
| 169 |
fs = get_fs_type("debugfs"); |
| 170 |
if (!fs) { |
| 171 |
errk("Couldn't find debugfs filesystem.\n"); |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
|
| 175 |
__stp_debugfs_root_dir = debugfs_create_dir(name, NULL); |
| 176 |
if (__stp_debugfs_root_dir == ERR_PTR(-EEXIST)) /* some kernels signal duplication this way */ |
| 177 |
__stp_debugfs_root_dir = NULL; |
| 178 |
if (!__stp_debugfs_root_dir) { |
| 179 |
/* Couldn't create it because it is already there, so |
| 180 |
* find it. */ |
| 181 |
#ifdef STAPCONF_FS_SUPERS_HLIST |
| 182 |
sb = hlist_entry(fs->fs_supers.first, struct super_block, |
| 183 |
s_instances); |
| 184 |
#else |
| 185 |
sb = list_entry(fs->fs_supers.next, struct super_block, |
| 186 |
s_instances); |
| 187 |
#endif |
| 188 |
_stp_lock_inode(sb->s_root->d_inode); |
| 189 |
#ifdef STAPCONF_LOOKUP_NOPERM |
| 190 |
__stp_debugfs_root_dir = lookup_noperm(&QSTR(name), sb->s_root); |
| 191 |
#else |
| 192 |
__stp_debugfs_root_dir = lookup_one_len(name, sb->s_root, |
| 193 |
strlen(name)); |
| 194 |
#endif |
| 195 |
_stp_unlock_inode(sb->s_root->d_inode); |
| 196 |
if (!IS_ERR(__stp_debugfs_root_dir)) |
| 197 |
dput(__stp_debugfs_root_dir); |
| 198 |
else { |
| 199 |
__stp_debugfs_root_dir = NULL; |
| 200 |
errk("Could not create or find transport directory.\n"); |
| 201 |
} |
| 202 |
} |
| 203 |
else if (IS_ERR(__stp_debugfs_root_dir)) { |
| 204 |
__stp_debugfs_root_dir = NULL; |
| 205 |
errk("Could not create root directory \"%s\", error %ld\n", name, |
| 206 |
-PTR_ERR(__stp_debugfs_root_dir)); |
| 207 |
} |
| 208 |
|
| 209 |
return __stp_debugfs_root_dir; |
| 210 |
} |
| 211 |
|
| 212 |
/* _stp_debugfs_remove_root_dir() - removes root directory (if empty) |
| 213 |
* |
| 214 |
* The caller *must* lock the transport directory. |
| 215 |
*/ |
| 216 |
|
| 217 |
static void _stp_debugfs_remove_root_dir(void) |
| 218 |
{ |
| 219 |
if (__stp_debugfs_root_dir) { |
| 220 |
if (simple_empty(__stp_debugfs_root_dir)) { |
| 221 |
debugfs_remove(__stp_debugfs_root_dir); |
| 222 |
} |
| 223 |
__stp_debugfs_root_dir = NULL; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// this is used by relay_v2 to place the traceN relayfs files. |
| 228 |
static struct dentry *_stp_debugfs_get_module_dir(void) |
| 229 |
{ |
| 230 |
return __stp_debugfs_module_dir; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
// relay_v2 callbacks for creating per-cpu files |
| 235 |
|
| 236 |
static int __stp_debugfs_relay_remove_buf_file_callback(struct dentry *dentry) |
| 237 |
{ |
| 238 |
debugfs_remove(dentry); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
static struct dentry * |
| 244 |
__stp_debugfs_relay_create_buf_file_callback(const char *filename, |
| 245 |
struct dentry *parent, |
| 246 |
#ifdef STAPCONF_RELAY_UMODE_T |
| 247 |
umode_t mode, |
| 248 |
#else |
| 249 |
int mode, |
| 250 |
#endif |
| 251 |
struct rchan_buf *buf, |
| 252 |
int *is_global) |
| 253 |
{ |
| 254 |
struct dentry *file = debugfs_create_file(filename, mode, parent, buf, |
| 255 |
&relay_file_operations_w_owner); |
| 256 |
/* |
| 257 |
* Here's what 'is_global' does (from linux/relay.h): |
| 258 |
* |
| 259 |
* Setting the is_global outparam to a non-zero value will |
| 260 |
* cause relay_open() to create a single global buffer rather |
| 261 |
* than the default set of per-cpu buffers. |
| 262 |
*/ |
| 263 |
if (is_global) |
| 264 |
*is_global = 0; |
| 265 |
|
| 266 |
if (IS_ERR(file)) { |
| 267 |
file = NULL; |
| 268 |
} |
| 269 |
else if (file) { |
| 270 |
file->d_inode->i_uid = KUIDT_INIT(_stp_uid); |
| 271 |
file->d_inode->i_gid = KGIDT_INIT(_stp_gid); |
| 272 |
} |
| 273 |
return file; |
| 274 |
} |
| 275 |
|