Debian Codegraph @

tapset/linux/dentry.stp

raw
1 // dentry tapset
2 // Copyright (c) 2009-2015 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9
10 %{
11 // Make sure the filesystem "magic" constants we use are defined.
12 #include <linux/magic.h>
13 #ifndef PIPEFS_MAGIC
14 #define PIPEFS_MAGIC 0x50495045
15 #endif
16 #ifndef SOCKFS_MAGIC
17 #define SOCKFS_MAGIC 0x534F434B
18 #endif
19 #ifndef ANON_INODE_FS_MAGIC
20 #define ANON_INODE_FS_MAGIC 0x09041934
21 #endif
22 #ifndef NSFS_MAGIC
23 #define NSFS_MAGIC 0x6e736673
24 #endif
25 %}
26
27 @__private30 function __dentry_IS_ROOT:long(dentry:long)
28 {
29 return (@cast(dentry, "dentry", "kernel")->d_parent == dentry)
30 }
31
32
33 @__private30 function __dentry_prepend:string(dentry:long,name:string)
34 {
35 dname = d_name(dentry)
36
37 /*
38 * In case we are following down a mount point trigger, we can get
39 * multiple instances of a root mount.
40 */
41 c = substr(name, strlen(name)-1, strlen(name)-1)
42 if (dname == "/" && c == "/")
43 return name;
44
45 if (name == "") {
46 return dname;
47 } else {
48 return sprintf("%s/%s", dname, name);
49 }
50 }
51
52
53
54 /**
55 * sfunction d_name - get the dirent name
56 *
57 * Returns the dirent name (path basename).
58 * @dentry: Pointer to dentry.
59 */
60 function d_name:string(dentry:long)
61 {
62 s = & @cast(dentry, "dentry", "kernel")->d_name;
63 return kernel_string_n(s->name, s->len);
64 }
65
66
67 @__private30 function __inode_first_dentry:long(inode:long)
68 {
69 /* i_dentry is an hlist_head on 3.6+, or a list_head before that. */
70 d_alias = @choose_defined(
71 @cast(inode, "struct inode", "kernel")->i_dentry->first,
72 @cast(inode, "struct inode", "kernel")->i_dentry->next)
73
74 if (@type_member_defined("struct dentry", d_alias)) {
75 return & @module_container_of(d_alias, "kernel<linux/dcache.h>", "struct dentry", d_alias)
76 }
77 else {
78 return & @module_container_of(d_alias, "kernel<linux/dcache.h>", "struct dentry", d_u->d_alias)
79 }
80 }
81
82
83 @__private30 function __inode_vfsmount:long(inode:long)
84 {
85 /* s_mounts was added in kernel 3.6, commit b3d9b7a3c. */
86 if (@type_member_defined("struct super_block", s_mounts)) {
87 mnt_ns = @cast(task_current(), "struct task_struct", "kernel<linux/sched.h>")->nsproxy->mnt_ns
88 sb = @cast(inode, "struct inode", "kernel")->i_sb
89
90 /* Look for the mount which matches the current namespace */
91 head = &sb->s_mounts
92 for (pos = head->next; pos != head; pos = pos->next) {
93 mount = & @container_of(pos, "struct mount", mnt_instance)
94 if (mount->mnt_ns == mnt_ns)
95 return & mount->mnt
96 }
97 }
98 return 0
99 }
100
101
102 /**
103 * sfunction inode_name - get the inode name
104 *
105 * Returns the first path basename associated with the given inode.
106 * @inode: Pointer to inode.
107 */
108 function inode_name:string(inode:long)
109 {
110 return reverse_path_walk(__inode_first_dentry(inode))
111 }
112
113
114 /**
115 * sfunction inode_path - get the path to an inode
116 *
117 * Returns the full path associated with the given inode.
118 * @inode: Pointer to inode.
119 */
120 function inode_path:string(inode:long)
121 {
122 dentry = __inode_first_dentry(inode)
123 vfsmount = __inode_vfsmount(inode)
124 if (vfsmount != 0)
125 return task_dentry_path(task_current(), dentry, vfsmount)
126
127 /* This is not really a full path... */
128 return reverse_path_walk(dentry)
129 }
130
131
132 /**
133 * sfunction reverse_path_walk - get the full dirent path
134 *
135 * Returns the path name (partial path to mount point).
136 * @dentry: Pointer to dentry.
137 */
138 function reverse_path_walk:string(dentry:long)
139 {
140 while(1) {
141 name = __dentry_prepend(dentry, name);
142 dentry = @cast(dentry, "dentry", "kernel")->d_parent;
143 if (__dentry_IS_ROOT(dentry))
144 return name;
145 }
146 }
147
148
149 /**
150 * sfunction real_mount - get the 'struct mount' pointer
151 *
152 * Returns the 'struct mount' pointer value for a 'struct vfsmount'
153 * pointer.
154 * @vfsmnt: Pointer to 'struct vfsmount'
155 */
156 function real_mount:long(vfsmnt:long)
157 {
158 if (@type_member_defined("mount", mnt_parent)) {
159 /*
160 * The following is the script language equivalent of:
161 *
162 * return container_of(vfsmnt, struct mount, mnt);
163 *
164 * We can't do the above because 'struct mount' is
165 * defined in a private header (in fs/mount.h). But,
166 * we can do the script language equivalent (because
167 * we've got dwarf info).
168 *
169 * More spelled out in C, the above would look like:
170 *
171 * return (vfsmnt - offsetof(struct mount, mnt));
172 *
173 * but here we're also making sure it won't wrap around.
174 */
175 offset = @offsetof("mount", mnt)
176 if (vfsmnt < 0 || vfsmnt > offset)
177 return (vfsmnt - offset)
178 }
179 return 0
180 }
181
182 /**
183 * sfunction task_dentry_path - get the full dentry path
184 *
185 * Returns the full dirent name (full path to the root), like
186 * the kernel d_path function.
187 * @task: task_struct pointer.
188 * @dentry: direntry pointer.
189 * @vfsmnt: vfsmnt pointer.
190 */
191 function task_dentry_path:string(task:long,dentry:long,vfsmnt:long)
192 {
193 /*
194 * Note that this function is based on __d_path() in RHEL6-era kernels
195 * and prepend_path() in RHEL7+ kernels.
196 */
197 /*
198 * There are various synthetic filesystems that never get
199 * mounted. Filesystems needing to implement special "root
200 * names" do so with dentry->d_op->d_dname(). Unfortunately,
201 * it isn't really safe for us to call
202 * dentry->d_op->d_dname(). We can't really validate the
203 * function pointer or know that it can be called safely in
204 * the current context.
205 *
206 * Some pseudo inodes are mountable. When they are mounted,
207 * dentry == vfsmnt->mnt_root. In that case, we'll just go
208 * ahead and handle them normally.
209 */
210 dentry = & @cast(dentry, "dentry", "kernel")
211 vfsmnt = & @cast(vfsmnt, "vfsmount", "kernel")
212
213 if (@type_member_defined("dentry", d_op->d_dname)
214 && dentry->d_op && dentry->d_op->d_dname
215 && (!__dentry_IS_ROOT(dentry) || dentry != vfsmnt->mnt_root)) {
216 // The following code hits the majority of synthetic
217 // filesystems that need special handling. As
218 // mentioned earlier, we'd like to call d_dname()
219 // here, but it isn't safe.
220 if (vfsmnt->mnt_sb->s_magic == @const("SOCKFS_MAGIC")) {
221 return sprintf("socket:[%lu]", dentry->d_inode->i_ino)
222 }
223 else if (vfsmnt->mnt_sb->s_magic == @const("PIPEFS_MAGIC")) {
224 return sprintf("pipe:[%lu]", dentry->d_inode->i_ino)
225 }
226 else if (vfsmnt->mnt_sb->s_magic
227 == @const("ANON_INODE_FS_MAGIC")) {
228 return sprintf("ANON_INODE:%s", d_name(dentry))
229 }
230 else if (vfsmnt->mnt_sb->s_magic == @const("NSFS_MAGIC")) {
231 ns_ops = &@cast(dentry->d_fsdata, "proc_ns_operations", "kernel")
232 return sprintf("%s:[%lu]", kernel_string(ns_ops->name),
233 dentry->d_inode->i_ino)
234 }
235 return sprintf("UNKNOWN:[%p]", dentry)
236 }
237
238 # Handle old-school vs. new-school fs_structs.
239 if (@type_member_defined("fs_struct", rootmnt)) {
240 root_dentry = & @cast(task, "task_struct", "kernel")->fs->root
241 root_vfsmnt = & @cast(task, "task_struct", "kernel")->fs->rootmnt
242 }
243 else {
244 root_dentry = @cast(task, "task_struct", "kernel")->fs->root->dentry
245 root_vfsmnt = @cast(task, "task_struct", "kernel")->fs->root->mnt
246 }
247
248 if (@type_member_defined("mount", mnt_parent)) {
249 mnt = &@cast(real_mount(vfsmnt), "mount", "kernel")
250 if (mnt == 0)
251 return "<unknown>"
252 }
253
254 # If we've found the right dentry/vfsmnt, we're done.
255 while (dentry != root_dentry || vfsmnt == root_vfsmnt) {
256 if (dentry == vfsmnt->mnt_root || __dentry_IS_ROOT(dentry)) {
257 /* Escaped? */
258 if (dentry != vfsmnt->mnt_root) {
259 return "<unknown>"
260 }
261
262 /* RHEL7+ kernels */
263 if (! @type_member_defined("vfsmount", mnt_parent)) {
264 /* Global root? */
265 if (mnt != mnt->mnt_parent) {
266 dentry = mnt->mnt_mountpoint
267 vfsmnt = & mnt->mnt_parent->mnt
268 mnt = mnt->mnt_parent
269 if (mnt == 0)
270 return "<unknown>"
271 continue
272 }
273 break
274 }
275 }
276 name = __dentry_prepend(dentry, name)
277 dentry = dentry->d_parent
278 }
279
280 return sprintf("/%s", name);
281 }
282
283
284
285 /**
286 * sfunction d_path - get the full nameidata path
287 *
288 * Returns the full dirent name (full path to the root), like
289 * the kernel d_path function.
290 * @nd: Pointer to nameidata.
291 */
292 function d_path:string(nd:long)
293 {
294 dentry = @choose_defined(@cast(nd,"nameidata", "kernel")->path->dentry,
295 @cast(nd,"nameidata", "kernel")->dentry)
296 vfsmnt = @choose_defined(@cast(nd,"nameidata", "kernel")->path->mnt,
297 @cast(nd,"nameidata", "kernel")->mnt)
298
299 return sprintf("%s/", task_dentry_path(task_current(), dentry, vfsmnt))
300 }
301
302
303
304 /**
305 * sfunction fullpath_struct_nameidata - get the full nameidata path
306 *
307 * Returns the full dirent name (full path to the root), like
308 * the kernel (and systemtap-tapset) d_path function, with a "/".
309 * @nd: Pointer to "struct nameidata".
310 */
311 function fullpath_struct_nameidata(nd)
312 {
313 return d_path(nd)
314 }
315
316
317 /**
318 * sfunction fullpath_struct_path - get the full path
319 *
320 * Returns the full dirent name (full path to the root), like
321 * the kernel d_path function.
322 * @path: Pointer to "struct path".
323 */
324 function fullpath_struct_path:string(path:long)
325 {
326 return task_dentry_path(task_current(),
327 @cast(path,"path","kernel:nfs:kernel<linux/path.h>")->dentry,
328 @cast(path,"path","kernel:nfs:kernel<linux/path.h>")->mnt)
329 }
330
331 /**
332 * sfunction fullpath_struct_file - get the full path
333 *
334 * Returns the full dirent name (full path to the root), like
335 * the kernel d_path function.
336 * @task: task_struct pointer.
337 * @file: Pointer to "struct file".
338 */
339 function fullpath_struct_file:string(task:long, file:long)
340 {
341 return task_dentry_path(task,
342 @choose_defined(@cast(file, "file", "kernel")->f_path->dentry,
343 @cast(file, "file", "kernel")->f_dentry),
344 @choose_defined(@cast(file, "file", "kernel")->f_path->mnt,
345 @cast(file, "file", "kernel")->f_vfsmnt))
346 }