File: debugfs.c

package info (click to toggle)
systemtap 5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,552 kB
  • sloc: cpp: 81,117; ansic: 54,933; xml: 49,795; exp: 43,595; sh: 11,526; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (275 lines) | stat: -rw-r--r-- 7,105 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* -*- linux-c -*-
 *
 * debugfs functions
 * Copyright (C) 2009 Red Hat Inc.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

#include <linux/debugfs.h>
#include "transport.h"
#include "../uidgid_compatibility.h"




/* Always returns zero, we just push all messages on the _stp_ctl_ready_q.  */
inline static int _stp_debugfs_ctl_write_fs(int type, void *data, unsigned len)
{
	return 0;
}

static struct dentry *__stp_debugfs_root_dir = NULL;    // DEBUGFS/systemtap/
static struct dentry *__stp_debugfs_module_dir = NULL;  // DEBUGFS/systemtap/MODULE/
static struct dentry *_stp_cmd_file = NULL;             // DEBUGFS/systemtap/MODULE/.cmd


static int _stp_debugfs_register_ctl_channel_fs(void)
{
	struct dentry *module_dir = _stp_debugfs_get_module_dir();
	if (module_dir == NULL) {
		errk("no module directory found.\n");
		return -1;
	}

	/* create [debugfs]/systemtap/module_name/.cmd  */
	_stp_cmd_file = debugfs_create_file(".cmd", 0600, module_dir,
					    NULL, &_stp_ctl_fops_cmd);
	if (_stp_cmd_file == NULL) {
		errk("Error creating systemtap debugfs entries.\n");
		return -1;
	}
	else if (IS_ERR(_stp_cmd_file)) {
		_stp_cmd_file = NULL;
		errk("Error creating systemtap debugfs entries: %ld\n",
		     -PTR_ERR(_stp_cmd_file));
		return -1;
	}

	_stp_cmd_file->d_inode->i_uid = KUIDT_INIT(_stp_uid);
	_stp_cmd_file->d_inode->i_gid = KGIDT_INIT(_stp_gid);

	return 0;
}

static void _stp_debugfs_unregister_ctl_channel_fs(void)
{
	if (_stp_cmd_file)
		debugfs_remove(_stp_cmd_file);
}


static int _stp_debugfs_transport_fs_init(const char *module_name)
{
	struct dentry *root_dir;
    
	dbug_trans(1, "entry\n");
	if (module_name == NULL)
		return -1;

	if (!_stp_lock_transport_dir()) {
		errk("Couldn't lock transport directory.\n");
		return -1;
	}

	root_dir = _stp_debugfs_get_root_dir();
	if (root_dir == NULL) {
		_stp_unlock_transport_dir();
		return -1;
	}

        __stp_debugfs_module_dir = debugfs_create_dir(module_name, root_dir);
        if (!__stp_debugfs_module_dir) {
		errk("Could not create module directory \"%s\"\n",
		     module_name);
		_stp_debugfs_remove_root_dir();
		_stp_unlock_transport_dir();
		return -1;
	}
	else if (IS_ERR(__stp_debugfs_module_dir)) {
		errk("Could not create module directory \"%s\", error %ld\n",
		     module_name, -PTR_ERR(__stp_debugfs_module_dir));
		_stp_debugfs_remove_root_dir();
		_stp_unlock_transport_dir();
		return -1;
	}

	if (_stp_transport_data_fs_init() != 0) {
		debugfs_remove(__stp_debugfs_module_dir);
		__stp_debugfs_module_dir = NULL;
		_stp_debugfs_remove_root_dir();
		_stp_unlock_transport_dir();
		return -1;
	}
	_stp_unlock_transport_dir();
	dbug_trans(1, "returning 0\n");
	return 0;
}

static void _stp_debugfs_transport_fs_close(void)
{
	dbug_trans(1, "stp_transport_fs_close\n");
	_stp_transport_data_fs_close();
	if (__stp_debugfs_module_dir) {
		if (!_stp_lock_transport_dir()) {
			errk("Couldn't lock transport directory.\n");
			return;
		}

		debugfs_remove(__stp_debugfs_module_dir);
		__stp_debugfs_module_dir = NULL;

		_stp_debugfs_remove_root_dir();
		_stp_unlock_transport_dir();
	}
}


static struct dentry *_stp_lockfile = NULL;

static int _stp_lock_transport_dir(void)
{
	int numtries = 0;

	while ((_stp_lockfile = debugfs_create_dir("systemtap_lock", NULL)) == NULL) {
		if (numtries++ >= 50)
			return 0;
		msleep(50);
	}
	return 1;
}

static void _stp_unlock_transport_dir(void)
{
	if (_stp_lockfile) {
		debugfs_remove(_stp_lockfile);
		_stp_lockfile = NULL;
	}
}

/* _stp_debugfs_get_root_dir() - creates root directory or returns
 * a pointer to it if it already exists.
 *
 * The caller *must* lock the transport directory.
 */

static struct dentry *_stp_debugfs_get_root_dir(void)
{
	struct file_system_type *fs;
	struct super_block *sb;
	const char *name = "systemtap";

	if (__stp_debugfs_root_dir != NULL) {
		return __stp_debugfs_root_dir;
	}

	fs = get_fs_type("debugfs");
	if (!fs) {
		errk("Couldn't find debugfs filesystem.\n");
		return NULL;
	}

	__stp_debugfs_root_dir = debugfs_create_dir(name, NULL);
	if (__stp_debugfs_root_dir == ERR_PTR(-EEXIST)) /* some kernels signal duplication this way */
	  __stp_debugfs_root_dir = NULL;
	if (!__stp_debugfs_root_dir) {
		/* Couldn't create it because it is already there, so
		 * find it. */
#ifdef STAPCONF_FS_SUPERS_HLIST
		sb = hlist_entry(fs->fs_supers.first, struct super_block,
	 			 s_instances);
#else
		sb = list_entry(fs->fs_supers.next, struct super_block,
				s_instances);
#endif
		_stp_lock_inode(sb->s_root->d_inode);
#ifdef STAPCONF_LOOKUP_NOPERM
		__stp_debugfs_root_dir = lookup_noperm(&QSTR(name), sb->s_root);
#else
		__stp_debugfs_root_dir = lookup_one_len(name, sb->s_root,
                                                       strlen(name));
#endif
		_stp_unlock_inode(sb->s_root->d_inode);
		if (!IS_ERR(__stp_debugfs_root_dir))
			dput(__stp_debugfs_root_dir);
		else {
			__stp_debugfs_root_dir = NULL;
			errk("Could not create or find transport directory.\n");
		}
	}
	else if (IS_ERR(__stp_debugfs_root_dir)) {
	    __stp_debugfs_root_dir = NULL;
	    errk("Could not create root directory \"%s\", error %ld\n", name,
		 -PTR_ERR(__stp_debugfs_root_dir));
	}

	return __stp_debugfs_root_dir;
}

/* _stp_debugfs_remove_root_dir() - removes root directory (if empty)
 *
 * The caller *must* lock the transport directory.
 */

static void _stp_debugfs_remove_root_dir(void)
{
	if (__stp_debugfs_root_dir) {
		if (simple_empty(__stp_debugfs_root_dir)) {
			debugfs_remove(__stp_debugfs_root_dir);
		}
		__stp_debugfs_root_dir = NULL;
	}
}

// this is used by relay_v2 to place the traceN relayfs files.
static struct dentry *_stp_debugfs_get_module_dir(void)
{
        return __stp_debugfs_module_dir;
}


// relay_v2 callbacks for creating per-cpu files

static int __stp_debugfs_relay_remove_buf_file_callback(struct dentry *dentry)
{
	debugfs_remove(dentry);
	return 0;
}


static struct dentry *
__stp_debugfs_relay_create_buf_file_callback(const char *filename,
                                            struct dentry *parent,
#ifdef STAPCONF_RELAY_UMODE_T
                                            umode_t mode,
#else
                                            int mode,
#endif
                                            struct rchan_buf *buf,
                                            int *is_global)
{
	struct dentry *file = debugfs_create_file(filename, mode, parent, buf,
	                                          &relay_file_operations_w_owner);
	/*
	 * Here's what 'is_global' does (from linux/relay.h):
	 *
	 * Setting the is_global outparam to a non-zero value will
	 * cause relay_open() to create a single global buffer rather
	 * than the default set of per-cpu buffers.
	 */
	if (is_global)
		*is_global = 0;

	if (IS_ERR(file)) {
		file = NULL;
	}
	else if (file) {
		file->d_inode->i_uid = KUIDT_INIT(_stp_uid);
		file->d_inode->i_gid = KGIDT_INIT(_stp_gid);
	}
	return file;
}