]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * proc/fs/generic.c --- generic routines for the proc-fs | |
3 | * | |
4 | * This file contains generic proc-fs routines for handling | |
5 | * directories and files. | |
6 | * | |
7 | * Copyright (C) 1991, 1992 Linus Torvalds. | |
8 | * Copyright (C) 1997 Theodore Ts'o | |
9 | */ | |
10 | ||
11 | #include <linux/errno.h> | |
12 | #include <linux/time.h> | |
13 | #include <linux/proc_fs.h> | |
14 | #include <linux/stat.h> | |
1025774c | 15 | #include <linux/mm.h> |
1da177e4 | 16 | #include <linux/module.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
87ebdc00 | 18 | #include <linux/printk.h> |
1da177e4 | 19 | #include <linux/mount.h> |
1da177e4 LT |
20 | #include <linux/init.h> |
21 | #include <linux/idr.h> | |
1da177e4 | 22 | #include <linux/bitops.h> |
64a07bd8 | 23 | #include <linux/spinlock.h> |
786d7e16 | 24 | #include <linux/completion.h> |
7c0f6ba6 | 25 | #include <linux/uaccess.h> |
1da177e4 | 26 | |
fee781e6 AB |
27 | #include "internal.h" |
28 | ||
ecf1a3df | 29 | static DEFINE_RWLOCK(proc_subdir_lock); |
64a07bd8 | 30 | |
312ec7e5 | 31 | static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de) |
1da177e4 | 32 | { |
710585d4 ND |
33 | if (len < de->namelen) |
34 | return -1; | |
35 | if (len > de->namelen) | |
36 | return 1; | |
37 | ||
38 | return memcmp(name, de->name, len); | |
39 | } | |
40 | ||
41 | static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir) | |
42 | { | |
410bd5ec DB |
43 | return rb_entry_safe(rb_first_cached(&dir->subdir), |
44 | struct proc_dir_entry, subdir_node); | |
710585d4 ND |
45 | } |
46 | ||
47 | static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir) | |
48 | { | |
2fc1e948 ND |
49 | return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry, |
50 | subdir_node); | |
710585d4 ND |
51 | } |
52 | ||
53 | static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir, | |
54 | const char *name, | |
55 | unsigned int len) | |
56 | { | |
410bd5ec | 57 | struct rb_node *node = dir->subdir.rb_root.rb_node; |
710585d4 ND |
58 | |
59 | while (node) { | |
4e4a7fb7 GT |
60 | struct proc_dir_entry *de = rb_entry(node, |
61 | struct proc_dir_entry, | |
62 | subdir_node); | |
710585d4 ND |
63 | int result = proc_match(len, name, de); |
64 | ||
65 | if (result < 0) | |
66 | node = node->rb_left; | |
67 | else if (result > 0) | |
68 | node = node->rb_right; | |
69 | else | |
70 | return de; | |
71 | } | |
72 | return NULL; | |
73 | } | |
74 | ||
75 | static bool pde_subdir_insert(struct proc_dir_entry *dir, | |
76 | struct proc_dir_entry *de) | |
77 | { | |
410bd5ec DB |
78 | struct rb_root_cached *root = &dir->subdir; |
79 | struct rb_node **new = &root->rb_root.rb_node, *parent = NULL; | |
80 | bool leftmost = true; | |
710585d4 ND |
81 | |
82 | /* Figure out where to put new node */ | |
83 | while (*new) { | |
4e4a7fb7 GT |
84 | struct proc_dir_entry *this = rb_entry(*new, |
85 | struct proc_dir_entry, | |
86 | subdir_node); | |
710585d4 ND |
87 | int result = proc_match(de->namelen, de->name, this); |
88 | ||
89 | parent = *new; | |
90 | if (result < 0) | |
91 | new = &(*new)->rb_left; | |
410bd5ec | 92 | else if (result > 0) { |
710585d4 | 93 | new = &(*new)->rb_right; |
410bd5ec DB |
94 | leftmost = false; |
95 | } else | |
710585d4 ND |
96 | return false; |
97 | } | |
98 | ||
99 | /* Add new node and rebalance tree. */ | |
100 | rb_link_node(&de->subdir_node, parent, new); | |
410bd5ec | 101 | rb_insert_color_cached(&de->subdir_node, root, leftmost); |
710585d4 | 102 | return true; |
1da177e4 LT |
103 | } |
104 | ||
1da177e4 LT |
105 | static int proc_notify_change(struct dentry *dentry, struct iattr *iattr) |
106 | { | |
2b0143b5 | 107 | struct inode *inode = d_inode(dentry); |
1da177e4 | 108 | struct proc_dir_entry *de = PDE(inode); |
5e13dab9 | 109 | struct user_namespace *s_user_ns; |
1da177e4 LT |
110 | int error; |
111 | ||
5e13dab9 EB |
112 | /* Don't let anyone mess with weird proc files */ |
113 | s_user_ns = inode->i_sb->s_user_ns; | |
114 | if (!kuid_has_mapping(s_user_ns, inode->i_uid) || | |
115 | !kgid_has_mapping(s_user_ns, inode->i_gid)) | |
116 | return -EPERM; | |
117 | ||
31051c85 | 118 | error = setattr_prepare(dentry, iattr); |
1da177e4 | 119 | if (error) |
1025774c | 120 | return error; |
1da177e4 | 121 | |
1025774c CH |
122 | setattr_copy(inode, iattr); |
123 | mark_inode_dirty(inode); | |
46f69557 | 124 | |
cdf7e8dd | 125 | proc_set_user(de, inode->i_uid, inode->i_gid); |
1da177e4 | 126 | de->mode = inode->i_mode; |
1025774c | 127 | return 0; |
1da177e4 LT |
128 | } |
129 | ||
a528d35e DH |
130 | static int proc_getattr(const struct path *path, struct kstat *stat, |
131 | u32 request_mask, unsigned int query_flags) | |
2b579bee | 132 | { |
a528d35e | 133 | struct inode *inode = d_inode(path->dentry); |
6bee55f9 | 134 | struct proc_dir_entry *de = PDE(inode); |
2b579bee | 135 | if (de && de->nlink) |
bfe86848 | 136 | set_nlink(inode, de->nlink); |
2b579bee MS |
137 | |
138 | generic_fillattr(inode, stat); | |
139 | return 0; | |
140 | } | |
141 | ||
c5ef1c42 | 142 | static const struct inode_operations proc_file_inode_operations = { |
1da177e4 LT |
143 | .setattr = proc_notify_change, |
144 | }; | |
145 | ||
146 | /* | |
147 | * This function parses a name such as "tty/driver/serial", and | |
148 | * returns the struct proc_dir_entry for "/proc/tty/driver", and | |
149 | * returns "serial" in residual. | |
150 | */ | |
e17a5765 AD |
151 | static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret, |
152 | const char **residual) | |
1da177e4 LT |
153 | { |
154 | const char *cp = name, *next; | |
155 | struct proc_dir_entry *de; | |
312ec7e5 | 156 | unsigned int len; |
1da177e4 | 157 | |
7cee4e00 AD |
158 | de = *ret; |
159 | if (!de) | |
160 | de = &proc_root; | |
161 | ||
1da177e4 LT |
162 | while (1) { |
163 | next = strchr(cp, '/'); | |
164 | if (!next) | |
165 | break; | |
166 | ||
167 | len = next - cp; | |
710585d4 | 168 | de = pde_subdir_find(de, cp, len); |
12bac0d9 AD |
169 | if (!de) { |
170 | WARN(1, "name '%s'\n", name); | |
e17a5765 | 171 | return -ENOENT; |
12bac0d9 | 172 | } |
1da177e4 LT |
173 | cp += len + 1; |
174 | } | |
175 | *residual = cp; | |
176 | *ret = de; | |
e17a5765 AD |
177 | return 0; |
178 | } | |
179 | ||
180 | static int xlate_proc_name(const char *name, struct proc_dir_entry **ret, | |
181 | const char **residual) | |
182 | { | |
183 | int rv; | |
184 | ||
ecf1a3df | 185 | read_lock(&proc_subdir_lock); |
e17a5765 | 186 | rv = __xlate_proc_name(name, ret, residual); |
ecf1a3df | 187 | read_unlock(&proc_subdir_lock); |
e17a5765 | 188 | return rv; |
1da177e4 LT |
189 | } |
190 | ||
9a185409 | 191 | static DEFINE_IDA(proc_inum_ida); |
1da177e4 | 192 | |
67935df4 | 193 | #define PROC_DYNAMIC_FIRST 0xF0000000U |
1da177e4 LT |
194 | |
195 | /* | |
196 | * Return an inode number between PROC_DYNAMIC_FIRST and | |
197 | * 0xffffffff, or zero on failure. | |
198 | */ | |
33d6dce6 | 199 | int proc_alloc_inum(unsigned int *inum) |
1da177e4 | 200 | { |
cde1b693 | 201 | int i; |
1da177e4 | 202 | |
cde1b693 HK |
203 | i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1, |
204 | GFP_KERNEL); | |
205 | if (i < 0) | |
206 | return i; | |
1da177e4 | 207 | |
cde1b693 | 208 | *inum = PROC_DYNAMIC_FIRST + (unsigned int)i; |
33d6dce6 | 209 | return 0; |
1da177e4 LT |
210 | } |
211 | ||
33d6dce6 | 212 | void proc_free_inum(unsigned int inum) |
1da177e4 | 213 | { |
cde1b693 | 214 | ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST); |
1da177e4 LT |
215 | } |
216 | ||
1da177e4 LT |
217 | /* |
218 | * Don't create negative dentries here, return -ENOENT by hand | |
219 | * instead. | |
220 | */ | |
e9720acd PE |
221 | struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir, |
222 | struct dentry *dentry) | |
1da177e4 | 223 | { |
d3d009cb | 224 | struct inode *inode; |
1da177e4 | 225 | |
ecf1a3df | 226 | read_lock(&proc_subdir_lock); |
710585d4 ND |
227 | de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len); |
228 | if (de) { | |
229 | pde_get(de); | |
ecf1a3df | 230 | read_unlock(&proc_subdir_lock); |
710585d4 ND |
231 | inode = proc_get_inode(dir->i_sb, de); |
232 | if (!inode) | |
233 | return ERR_PTR(-ENOMEM); | |
234 | d_set_d_op(dentry, &simple_dentry_operations); | |
235 | d_add(dentry, inode); | |
236 | return NULL; | |
1da177e4 | 237 | } |
ecf1a3df | 238 | read_unlock(&proc_subdir_lock); |
d3d009cb | 239 | return ERR_PTR(-ENOENT); |
1da177e4 LT |
240 | } |
241 | ||
e9720acd | 242 | struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, |
00cd8dd3 | 243 | unsigned int flags) |
e9720acd PE |
244 | { |
245 | return proc_lookup_de(PDE(dir), dir, dentry); | |
246 | } | |
247 | ||
1da177e4 LT |
248 | /* |
249 | * This returns non-zero if at EOF, so that the /proc | |
250 | * root directory can use this and check if it should | |
251 | * continue with the <pid> entries.. | |
252 | * | |
253 | * Note that the VFS-layer doesn't care about the return | |
254 | * value of the readdir() call, as long as it's non-negative | |
255 | * for success.. | |
256 | */ | |
f0c3b509 AV |
257 | int proc_readdir_de(struct proc_dir_entry *de, struct file *file, |
258 | struct dir_context *ctx) | |
1da177e4 | 259 | { |
1da177e4 | 260 | int i; |
f0c3b509 AV |
261 | |
262 | if (!dir_emit_dots(file, ctx)) | |
263 | return 0; | |
264 | ||
ecf1a3df | 265 | read_lock(&proc_subdir_lock); |
710585d4 | 266 | de = pde_subdir_first(de); |
f0c3b509 AV |
267 | i = ctx->pos - 2; |
268 | for (;;) { | |
269 | if (!de) { | |
ecf1a3df | 270 | read_unlock(&proc_subdir_lock); |
f0c3b509 AV |
271 | return 0; |
272 | } | |
273 | if (!i) | |
274 | break; | |
710585d4 | 275 | de = pde_subdir_next(de); |
f0c3b509 | 276 | i--; |
1da177e4 | 277 | } |
f0c3b509 AV |
278 | |
279 | do { | |
280 | struct proc_dir_entry *next; | |
281 | pde_get(de); | |
ecf1a3df | 282 | read_unlock(&proc_subdir_lock); |
f0c3b509 AV |
283 | if (!dir_emit(ctx, de->name, de->namelen, |
284 | de->low_ino, de->mode >> 12)) { | |
285 | pde_put(de); | |
286 | return 0; | |
287 | } | |
ecf1a3df | 288 | read_lock(&proc_subdir_lock); |
f0c3b509 | 289 | ctx->pos++; |
710585d4 | 290 | next = pde_subdir_next(de); |
f0c3b509 AV |
291 | pde_put(de); |
292 | de = next; | |
293 | } while (de); | |
ecf1a3df | 294 | read_unlock(&proc_subdir_lock); |
fd3930f7 | 295 | return 1; |
1da177e4 LT |
296 | } |
297 | ||
f0c3b509 | 298 | int proc_readdir(struct file *file, struct dir_context *ctx) |
e9720acd | 299 | { |
f0c3b509 | 300 | struct inode *inode = file_inode(file); |
e9720acd | 301 | |
f0c3b509 | 302 | return proc_readdir_de(PDE(inode), file, ctx); |
e9720acd PE |
303 | } |
304 | ||
1da177e4 LT |
305 | /* |
306 | * These are the generic /proc directory operations. They | |
307 | * use the in-memory "struct proc_dir_entry" tree to parse | |
308 | * the /proc directory. | |
309 | */ | |
00977a59 | 310 | static const struct file_operations proc_dir_operations = { |
b4df2b92 | 311 | .llseek = generic_file_llseek, |
1da177e4 | 312 | .read = generic_read_dir, |
f50752ea | 313 | .iterate_shared = proc_readdir, |
1da177e4 LT |
314 | }; |
315 | ||
316 | /* | |
317 | * proc directories can do almost nothing.. | |
318 | */ | |
c5ef1c42 | 319 | static const struct inode_operations proc_dir_inode_operations = { |
1da177e4 | 320 | .lookup = proc_lookup, |
2b579bee | 321 | .getattr = proc_getattr, |
1da177e4 LT |
322 | .setattr = proc_notify_change, |
323 | }; | |
324 | ||
325 | static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp) | |
326 | { | |
33d6dce6 | 327 | int ret; |
710585d4 | 328 | |
33d6dce6 EB |
329 | ret = proc_alloc_inum(&dp->low_ino); |
330 | if (ret) | |
331 | return ret; | |
64a07bd8 | 332 | |
ecf1a3df | 333 | write_lock(&proc_subdir_lock); |
99fc06df | 334 | dp->parent = dir; |
b208d54b | 335 | if (pde_subdir_insert(dir, dp) == false) { |
710585d4 ND |
336 | WARN(1, "proc_dir_entry '%s/%s' already registered\n", |
337 | dir->name, dp->name); | |
ecf1a3df | 338 | write_unlock(&proc_subdir_lock); |
b208d54b DB |
339 | proc_free_inum(dp->low_ino); |
340 | return -EEXIST; | |
341 | } | |
ecf1a3df | 342 | write_unlock(&proc_subdir_lock); |
99fc06df | 343 | |
1da177e4 LT |
344 | return 0; |
345 | } | |
346 | ||
2d3a4e36 | 347 | static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, |
1da177e4 | 348 | const char *name, |
d161a13f | 349 | umode_t mode, |
1da177e4 LT |
350 | nlink_t nlink) |
351 | { | |
352 | struct proc_dir_entry *ent = NULL; | |
dbcdb504 AD |
353 | const char *fn; |
354 | struct qstr qstr; | |
1da177e4 | 355 | |
7cee4e00 | 356 | if (xlate_proc_name(name, parent, &fn) != 0) |
1da177e4 | 357 | goto out; |
dbcdb504 AD |
358 | qstr.name = fn; |
359 | qstr.len = strlen(fn); | |
360 | if (qstr.len == 0 || qstr.len >= 256) { | |
361 | WARN(1, "name len %u\n", qstr.len); | |
362 | return NULL; | |
363 | } | |
364 | if (*parent == &proc_root && name_to_int(&qstr) != ~0U) { | |
365 | WARN(1, "create '/proc/%s' by hand\n", qstr.name); | |
366 | return NULL; | |
367 | } | |
eb6d38d5 EB |
368 | if (is_empty_pde(*parent)) { |
369 | WARN(1, "attempt to add to permanently empty directory"); | |
370 | return NULL; | |
371 | } | |
1da177e4 | 372 | |
dbcdb504 | 373 | ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL); |
17baa2a2 | 374 | if (!ent) |
375 | goto out; | |
1da177e4 | 376 | |
dbcdb504 AD |
377 | memcpy(ent->name, fn, qstr.len + 1); |
378 | ent->namelen = qstr.len; | |
1da177e4 LT |
379 | ent->mode = mode; |
380 | ent->nlink = nlink; | |
410bd5ec | 381 | ent->subdir = RB_ROOT_CACHED; |
5a622f2d | 382 | atomic_set(&ent->count, 1); |
786d7e16 | 383 | spin_lock_init(&ent->pde_unload_lock); |
881adb85 | 384 | INIT_LIST_HEAD(&ent->pde_openers); |
c110486f DT |
385 | proc_set_user(ent, (*parent)->uid, (*parent)->gid); |
386 | ||
17baa2a2 | 387 | out: |
1da177e4 LT |
388 | return ent; |
389 | } | |
390 | ||
391 | struct proc_dir_entry *proc_symlink(const char *name, | |
392 | struct proc_dir_entry *parent, const char *dest) | |
393 | { | |
394 | struct proc_dir_entry *ent; | |
395 | ||
2d3a4e36 | 396 | ent = __proc_create(&parent, name, |
1da177e4 LT |
397 | (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1); |
398 | ||
399 | if (ent) { | |
400 | ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL); | |
401 | if (ent->data) { | |
402 | strcpy((char*)ent->data,dest); | |
d443b9fd | 403 | ent->proc_iops = &proc_link_inode_operations; |
1da177e4 LT |
404 | if (proc_register(parent, ent) < 0) { |
405 | kfree(ent->data); | |
406 | kfree(ent); | |
407 | ent = NULL; | |
408 | } | |
409 | } else { | |
410 | kfree(ent); | |
411 | ent = NULL; | |
412 | } | |
413 | } | |
414 | return ent; | |
415 | } | |
587d4a17 | 416 | EXPORT_SYMBOL(proc_symlink); |
1da177e4 | 417 | |
270b5ac2 DH |
418 | struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, |
419 | struct proc_dir_entry *parent, void *data) | |
1da177e4 LT |
420 | { |
421 | struct proc_dir_entry *ent; | |
422 | ||
270b5ac2 DH |
423 | if (mode == 0) |
424 | mode = S_IRUGO | S_IXUGO; | |
425 | ||
2d3a4e36 | 426 | ent = __proc_create(&parent, name, S_IFDIR | mode, 2); |
1da177e4 | 427 | if (ent) { |
270b5ac2 | 428 | ent->data = data; |
d443b9fd AV |
429 | ent->proc_fops = &proc_dir_operations; |
430 | ent->proc_iops = &proc_dir_inode_operations; | |
431 | parent->nlink++; | |
1da177e4 LT |
432 | if (proc_register(parent, ent) < 0) { |
433 | kfree(ent); | |
d443b9fd | 434 | parent->nlink--; |
1da177e4 LT |
435 | ent = NULL; |
436 | } | |
437 | } | |
438 | return ent; | |
439 | } | |
270b5ac2 | 440 | EXPORT_SYMBOL_GPL(proc_mkdir_data); |
1da177e4 | 441 | |
270b5ac2 DH |
442 | struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode, |
443 | struct proc_dir_entry *parent) | |
78e92b99 | 444 | { |
270b5ac2 | 445 | return proc_mkdir_data(name, mode, parent, NULL); |
78e92b99 | 446 | } |
270b5ac2 | 447 | EXPORT_SYMBOL(proc_mkdir_mode); |
78e92b99 | 448 | |
1da177e4 LT |
449 | struct proc_dir_entry *proc_mkdir(const char *name, |
450 | struct proc_dir_entry *parent) | |
451 | { | |
270b5ac2 | 452 | return proc_mkdir_data(name, 0, parent, NULL); |
1da177e4 | 453 | } |
587d4a17 | 454 | EXPORT_SYMBOL(proc_mkdir); |
1da177e4 | 455 | |
eb6d38d5 EB |
456 | struct proc_dir_entry *proc_create_mount_point(const char *name) |
457 | { | |
458 | umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO; | |
459 | struct proc_dir_entry *ent, *parent = NULL; | |
460 | ||
461 | ent = __proc_create(&parent, name, mode, 2); | |
462 | if (ent) { | |
463 | ent->data = NULL; | |
464 | ent->proc_fops = NULL; | |
465 | ent->proc_iops = NULL; | |
d66bb160 | 466 | parent->nlink++; |
eb6d38d5 EB |
467 | if (proc_register(parent, ent) < 0) { |
468 | kfree(ent); | |
469 | parent->nlink--; | |
470 | ent = NULL; | |
471 | } | |
472 | } | |
473 | return ent; | |
474 | } | |
f97df70b | 475 | EXPORT_SYMBOL(proc_create_mount_point); |
eb6d38d5 | 476 | |
d161a13f | 477 | struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, |
59b74351 DL |
478 | struct proc_dir_entry *parent, |
479 | const struct file_operations *proc_fops, | |
480 | void *data) | |
2d3a4e36 AD |
481 | { |
482 | struct proc_dir_entry *pde; | |
b6cdc731 AV |
483 | if ((mode & S_IFMT) == 0) |
484 | mode |= S_IFREG; | |
2d3a4e36 | 485 | |
b6cdc731 AV |
486 | if (!S_ISREG(mode)) { |
487 | WARN_ON(1); /* use proc_mkdir() */ | |
488 | return NULL; | |
2d3a4e36 AD |
489 | } |
490 | ||
d443b9fd AV |
491 | BUG_ON(proc_fops == NULL); |
492 | ||
b6cdc731 AV |
493 | if ((mode & S_IALLUGO) == 0) |
494 | mode |= S_IRUGO; | |
495 | pde = __proc_create(&parent, name, mode, 1); | |
2d3a4e36 AD |
496 | if (!pde) |
497 | goto out; | |
498 | pde->proc_fops = proc_fops; | |
59b74351 | 499 | pde->data = data; |
d443b9fd | 500 | pde->proc_iops = &proc_file_inode_operations; |
2d3a4e36 AD |
501 | if (proc_register(parent, pde) < 0) |
502 | goto out_free; | |
503 | return pde; | |
504 | out_free: | |
505 | kfree(pde); | |
506 | out: | |
507 | return NULL; | |
508 | } | |
587d4a17 | 509 | EXPORT_SYMBOL(proc_create_data); |
271a15ea | 510 | |
855d9765 AD |
511 | struct proc_dir_entry *proc_create(const char *name, umode_t mode, |
512 | struct proc_dir_entry *parent, | |
513 | const struct file_operations *proc_fops) | |
514 | { | |
515 | return proc_create_data(name, mode, parent, proc_fops, NULL); | |
516 | } | |
517 | EXPORT_SYMBOL(proc_create); | |
518 | ||
271a15ea DH |
519 | void proc_set_size(struct proc_dir_entry *de, loff_t size) |
520 | { | |
521 | de->size = size; | |
522 | } | |
523 | EXPORT_SYMBOL(proc_set_size); | |
524 | ||
525 | void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid) | |
526 | { | |
527 | de->uid = uid; | |
528 | de->gid = gid; | |
529 | } | |
530 | EXPORT_SYMBOL(proc_set_user); | |
2d3a4e36 | 531 | |
135d5655 | 532 | static void free_proc_entry(struct proc_dir_entry *de) |
1da177e4 | 533 | { |
33d6dce6 | 534 | proc_free_inum(de->low_ino); |
1da177e4 | 535 | |
fd2cbe48 | 536 | if (S_ISLNK(de->mode)) |
1da177e4 LT |
537 | kfree(de->data); |
538 | kfree(de); | |
539 | } | |
540 | ||
135d5655 AD |
541 | void pde_put(struct proc_dir_entry *pde) |
542 | { | |
543 | if (atomic_dec_and_test(&pde->count)) | |
544 | free_proc_entry(pde); | |
545 | } | |
546 | ||
8ce584c7 AV |
547 | /* |
548 | * Remove a /proc entry and free it if it's not currently in use. | |
549 | */ | |
550 | void remove_proc_entry(const char *name, struct proc_dir_entry *parent) | |
551 | { | |
8ce584c7 AV |
552 | struct proc_dir_entry *de = NULL; |
553 | const char *fn = name; | |
554 | unsigned int len; | |
555 | ||
ecf1a3df | 556 | write_lock(&proc_subdir_lock); |
8ce584c7 | 557 | if (__xlate_proc_name(name, &parent, &fn) != 0) { |
ecf1a3df | 558 | write_unlock(&proc_subdir_lock); |
8ce584c7 AV |
559 | return; |
560 | } | |
561 | len = strlen(fn); | |
562 | ||
710585d4 ND |
563 | de = pde_subdir_find(parent, fn, len); |
564 | if (de) | |
410bd5ec | 565 | rb_erase_cached(&de->subdir_node, &parent->subdir); |
ecf1a3df | 566 | write_unlock(&proc_subdir_lock); |
8ce584c7 AV |
567 | if (!de) { |
568 | WARN(1, "name '%s'\n", name); | |
569 | return; | |
570 | } | |
571 | ||
866ad9a7 | 572 | proc_entry_rundown(de); |
881adb85 | 573 | |
f649d6d3 AD |
574 | if (S_ISDIR(de->mode)) |
575 | parent->nlink--; | |
576 | de->nlink = 0; | |
710585d4 ND |
577 | WARN(pde_subdir_first(de), |
578 | "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n", | |
579 | __func__, de->parent->name, de->name, pde_subdir_first(de)->name); | |
135d5655 | 580 | pde_put(de); |
1da177e4 | 581 | } |
587d4a17 | 582 | EXPORT_SYMBOL(remove_proc_entry); |
8ce584c7 AV |
583 | |
584 | int remove_proc_subtree(const char *name, struct proc_dir_entry *parent) | |
585 | { | |
8ce584c7 AV |
586 | struct proc_dir_entry *root = NULL, *de, *next; |
587 | const char *fn = name; | |
588 | unsigned int len; | |
589 | ||
ecf1a3df | 590 | write_lock(&proc_subdir_lock); |
8ce584c7 | 591 | if (__xlate_proc_name(name, &parent, &fn) != 0) { |
ecf1a3df | 592 | write_unlock(&proc_subdir_lock); |
8ce584c7 AV |
593 | return -ENOENT; |
594 | } | |
595 | len = strlen(fn); | |
596 | ||
710585d4 | 597 | root = pde_subdir_find(parent, fn, len); |
8ce584c7 | 598 | if (!root) { |
ecf1a3df | 599 | write_unlock(&proc_subdir_lock); |
8ce584c7 AV |
600 | return -ENOENT; |
601 | } | |
410bd5ec | 602 | rb_erase_cached(&root->subdir_node, &parent->subdir); |
710585d4 | 603 | |
8ce584c7 AV |
604 | de = root; |
605 | while (1) { | |
710585d4 | 606 | next = pde_subdir_first(de); |
8ce584c7 | 607 | if (next) { |
410bd5ec | 608 | rb_erase_cached(&next->subdir_node, &de->subdir); |
8ce584c7 AV |
609 | de = next; |
610 | continue; | |
611 | } | |
ecf1a3df | 612 | write_unlock(&proc_subdir_lock); |
8ce584c7 | 613 | |
866ad9a7 | 614 | proc_entry_rundown(de); |
8ce584c7 AV |
615 | next = de->parent; |
616 | if (S_ISDIR(de->mode)) | |
617 | next->nlink--; | |
618 | de->nlink = 0; | |
619 | if (de == root) | |
620 | break; | |
621 | pde_put(de); | |
622 | ||
ecf1a3df | 623 | write_lock(&proc_subdir_lock); |
8ce584c7 AV |
624 | de = next; |
625 | } | |
626 | pde_put(root); | |
627 | return 0; | |
628 | } | |
629 | EXPORT_SYMBOL(remove_proc_subtree); | |
4a520d27 DH |
630 | |
631 | void *proc_get_parent_data(const struct inode *inode) | |
632 | { | |
633 | struct proc_dir_entry *de = PDE(inode); | |
634 | return de->parent->data; | |
635 | } | |
636 | EXPORT_SYMBOL_GPL(proc_get_parent_data); | |
a8ca16ea DH |
637 | |
638 | void proc_remove(struct proc_dir_entry *de) | |
639 | { | |
640 | if (de) | |
641 | remove_proc_subtree(de->name, de->parent); | |
642 | } | |
643 | EXPORT_SYMBOL(proc_remove); | |
c30480b9 DH |
644 | |
645 | void *PDE_DATA(const struct inode *inode) | |
646 | { | |
647 | return __PDE_DATA(inode); | |
648 | } | |
649 | EXPORT_SYMBOL(PDE_DATA); |