Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 11 | #include <linux/cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/time.h> |
| 14 | #include <linux/proc_fs.h> |
| 15 | #include <linux/stat.h> |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 16 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 19 | #include <linux/printk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/idr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 24 | #include <linux/spinlock.h> |
Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 25 | #include <linux/completion.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 26 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Adrian Bunk | fee781e | 2006-01-08 01:04:16 -0800 | [diff] [blame] | 28 | #include "internal.h" |
| 29 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 30 | static DEFINE_RWLOCK(proc_subdir_lock); |
Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 31 | |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 32 | struct kmem_cache *proc_dir_entry_cache __ro_after_init; |
| 33 | |
| 34 | void pde_free(struct proc_dir_entry *pde) |
| 35 | { |
| 36 | if (S_ISLNK(pde->mode)) |
| 37 | kfree(pde->data); |
| 38 | if (pde->name != pde->inline_name) |
| 39 | kfree(pde->name); |
| 40 | kmem_cache_free(proc_dir_entry_cache, pde); |
| 41 | } |
| 42 | |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 43 | static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 45 | if (len < de->namelen) |
| 46 | return -1; |
| 47 | if (len > de->namelen) |
| 48 | return 1; |
| 49 | |
| 50 | return memcmp(name, de->name, len); |
| 51 | } |
| 52 | |
| 53 | static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir) |
| 54 | { |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 55 | return rb_entry_safe(rb_first_cached(&dir->subdir), |
| 56 | struct proc_dir_entry, subdir_node); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir) |
| 60 | { |
Nicolas Dichtel | 2fc1e94 | 2014-12-10 15:45:07 -0800 | [diff] [blame] | 61 | return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry, |
| 62 | subdir_node); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir, |
| 66 | const char *name, |
| 67 | unsigned int len) |
| 68 | { |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 69 | struct rb_node *node = dir->subdir.rb_root.rb_node; |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 70 | |
| 71 | while (node) { |
Geliang Tang | 4e4a7fb | 2017-02-24 15:00:17 -0800 | [diff] [blame] | 72 | struct proc_dir_entry *de = rb_entry(node, |
| 73 | struct proc_dir_entry, |
| 74 | subdir_node); |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 75 | int result = proc_match(name, de, len); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 76 | |
| 77 | if (result < 0) |
| 78 | node = node->rb_left; |
| 79 | else if (result > 0) |
| 80 | node = node->rb_right; |
| 81 | else |
| 82 | return de; |
| 83 | } |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | static bool pde_subdir_insert(struct proc_dir_entry *dir, |
| 88 | struct proc_dir_entry *de) |
| 89 | { |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 90 | struct rb_root_cached *root = &dir->subdir; |
| 91 | struct rb_node **new = &root->rb_root.rb_node, *parent = NULL; |
| 92 | bool leftmost = true; |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 93 | |
| 94 | /* Figure out where to put new node */ |
| 95 | while (*new) { |
Geliang Tang | 4e4a7fb | 2017-02-24 15:00:17 -0800 | [diff] [blame] | 96 | struct proc_dir_entry *this = rb_entry(*new, |
| 97 | struct proc_dir_entry, |
| 98 | subdir_node); |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 99 | int result = proc_match(de->name, this, de->namelen); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 100 | |
| 101 | parent = *new; |
| 102 | if (result < 0) |
| 103 | new = &(*new)->rb_left; |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 104 | else if (result > 0) { |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 105 | new = &(*new)->rb_right; |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 106 | leftmost = false; |
| 107 | } else |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
| 111 | /* Add new node and rebalance tree. */ |
| 112 | rb_link_node(&de->subdir_node, parent, new); |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 113 | rb_insert_color_cached(&de->subdir_node, root, leftmost); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 114 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | static int proc_notify_change(struct dentry *dentry, struct iattr *iattr) |
| 118 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 119 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | struct proc_dir_entry *de = PDE(inode); |
| 121 | int error; |
| 122 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 123 | error = setattr_prepare(dentry, iattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (error) |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 125 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 127 | setattr_copy(inode, iattr); |
| 128 | mark_inode_dirty(inode); |
Marco Stornelli | 46f6955 | 2012-12-15 11:48:48 +0100 | [diff] [blame] | 129 | |
Rui Xiang | cdf7e8d | 2014-01-23 15:55:41 -0800 | [diff] [blame] | 130 | proc_set_user(de, inode->i_uid, inode->i_gid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | de->mode = inode->i_mode; |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 132 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 135 | static int proc_getattr(const struct path *path, struct kstat *stat, |
| 136 | u32 request_mask, unsigned int query_flags) |
Miklos Szeredi | 2b579be | 2005-09-06 15:17:18 -0700 | [diff] [blame] | 137 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 138 | struct inode *inode = d_inode(path->dentry); |
Alexander Kuleshov | 6bee55f | 2015-02-12 15:01:03 -0800 | [diff] [blame] | 139 | struct proc_dir_entry *de = PDE(inode); |
Miklos Szeredi | 2b579be | 2005-09-06 15:17:18 -0700 | [diff] [blame] | 140 | if (de && de->nlink) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 141 | set_nlink(inode, de->nlink); |
Miklos Szeredi | 2b579be | 2005-09-06 15:17:18 -0700 | [diff] [blame] | 142 | |
| 143 | generic_fillattr(inode, stat); |
| 144 | return 0; |
| 145 | } |
| 146 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 147 | static const struct inode_operations proc_file_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .setattr = proc_notify_change, |
| 149 | }; |
| 150 | |
| 151 | /* |
| 152 | * This function parses a name such as "tty/driver/serial", and |
| 153 | * returns the struct proc_dir_entry for "/proc/tty/driver", and |
| 154 | * returns "serial" in residual. |
| 155 | */ |
Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 156 | static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret, |
| 157 | const char **residual) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
| 159 | const char *cp = name, *next; |
| 160 | struct proc_dir_entry *de; |
Alexey Dobriyan | 312ec7e | 2011-03-23 16:42:52 -0700 | [diff] [blame] | 161 | unsigned int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Alexey Dobriyan | 7cee4e0 | 2008-04-29 01:01:40 -0700 | [diff] [blame] | 163 | de = *ret; |
| 164 | if (!de) |
| 165 | de = &proc_root; |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | while (1) { |
| 168 | next = strchr(cp, '/'); |
| 169 | if (!next) |
| 170 | break; |
| 171 | |
| 172 | len = next - cp; |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 173 | de = pde_subdir_find(de, cp, len); |
Alexey Dobriyan | 12bac0d | 2010-03-05 13:44:00 -0800 | [diff] [blame] | 174 | if (!de) { |
| 175 | WARN(1, "name '%s'\n", name); |
Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 176 | return -ENOENT; |
Alexey Dobriyan | 12bac0d | 2010-03-05 13:44:00 -0800 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | cp += len + 1; |
| 179 | } |
| 180 | *residual = cp; |
| 181 | *ret = de; |
Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int xlate_proc_name(const char *name, struct proc_dir_entry **ret, |
| 186 | const char **residual) |
| 187 | { |
| 188 | int rv; |
| 189 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 190 | read_lock(&proc_subdir_lock); |
Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 191 | rv = __xlate_proc_name(name, ret, residual); |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 192 | read_unlock(&proc_subdir_lock); |
Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 193 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Alexey Dobriyan | 9a18540 | 2008-07-26 11:21:37 +0400 | [diff] [blame] | 196 | static DEFINE_IDA(proc_inum_ida); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Alexey Dobriyan | 67935df | 2008-07-26 11:18:28 +0400 | [diff] [blame] | 198 | #define PROC_DYNAMIC_FIRST 0xF0000000U |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Return an inode number between PROC_DYNAMIC_FIRST and |
| 202 | * 0xffffffff, or zero on failure. |
| 203 | */ |
Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 204 | int proc_alloc_inum(unsigned int *inum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Heiner Kallweit | cde1b69 | 2017-07-10 15:50:52 -0700 | [diff] [blame] | 206 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Heiner Kallweit | cde1b69 | 2017-07-10 15:50:52 -0700 | [diff] [blame] | 208 | i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1, |
| 209 | GFP_KERNEL); |
| 210 | if (i < 0) |
| 211 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Heiner Kallweit | cde1b69 | 2017-07-10 15:50:52 -0700 | [diff] [blame] | 213 | *inum = PROC_DYNAMIC_FIRST + (unsigned int)i; |
Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 214 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 217 | void proc_free_inum(unsigned int inum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Heiner Kallweit | cde1b69 | 2017-07-10 15:50:52 -0700 | [diff] [blame] | 219 | ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | * Don't create negative dentries here, return -ENOENT by hand |
| 224 | * instead. |
| 225 | */ |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 226 | struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry, |
| 227 | struct proc_dir_entry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Al Viro | d3d009c | 2013-01-25 20:11:22 -0500 | [diff] [blame] | 229 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 231 | read_lock(&proc_subdir_lock); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 232 | de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len); |
| 233 | if (de) { |
| 234 | pde_get(de); |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 235 | read_unlock(&proc_subdir_lock); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 236 | inode = proc_get_inode(dir->i_sb, de); |
| 237 | if (!inode) |
| 238 | return ERR_PTR(-ENOMEM); |
| 239 | d_set_d_op(dentry, &simple_dentry_operations); |
| 240 | d_add(dentry, inode); |
| 241 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 243 | read_unlock(&proc_subdir_lock); |
Al Viro | d3d009c | 2013-01-25 20:11:22 -0500 | [diff] [blame] | 244 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 247 | struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 248 | unsigned int flags) |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 249 | { |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 250 | return proc_lookup_de(dir, dentry, PDE(dir)); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | /* |
| 254 | * This returns non-zero if at EOF, so that the /proc |
| 255 | * root directory can use this and check if it should |
| 256 | * continue with the <pid> entries.. |
| 257 | * |
| 258 | * Note that the VFS-layer doesn't care about the return |
| 259 | * value of the readdir() call, as long as it's non-negative |
| 260 | * for success.. |
| 261 | */ |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 262 | int proc_readdir_de(struct file *file, struct dir_context *ctx, |
| 263 | struct proc_dir_entry *de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 267 | if (!dir_emit_dots(file, ctx)) |
| 268 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 270 | read_lock(&proc_subdir_lock); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 271 | de = pde_subdir_first(de); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 272 | i = ctx->pos - 2; |
| 273 | for (;;) { |
| 274 | if (!de) { |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 275 | read_unlock(&proc_subdir_lock); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | if (!i) |
| 279 | break; |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 280 | de = pde_subdir_next(de); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 281 | i--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 283 | |
| 284 | do { |
| 285 | struct proc_dir_entry *next; |
| 286 | pde_get(de); |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 287 | read_unlock(&proc_subdir_lock); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 288 | if (!dir_emit(ctx, de->name, de->namelen, |
| 289 | de->low_ino, de->mode >> 12)) { |
| 290 | pde_put(de); |
| 291 | return 0; |
| 292 | } |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 293 | read_lock(&proc_subdir_lock); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 294 | ctx->pos++; |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 295 | next = pde_subdir_next(de); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 296 | pde_put(de); |
| 297 | de = next; |
| 298 | } while (de); |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 299 | read_unlock(&proc_subdir_lock); |
Linus Torvalds | fd3930f | 2013-08-19 16:26:12 -0700 | [diff] [blame] | 300 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 303 | int proc_readdir(struct file *file, struct dir_context *ctx) |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 304 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 305 | struct inode *inode = file_inode(file); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 306 | |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 307 | return proc_readdir_de(file, ctx, PDE(inode)); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | /* |
| 311 | * These are the generic /proc directory operations. They |
| 312 | * use the in-memory "struct proc_dir_entry" tree to parse |
| 313 | * the /proc directory. |
| 314 | */ |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 315 | static const struct file_operations proc_dir_operations = { |
Alexey Dobriyan | b4df2b9 | 2008-10-27 22:48:36 +0300 | [diff] [blame] | 316 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 318 | .iterate_shared = proc_readdir, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | /* |
| 322 | * proc directories can do almost nothing.. |
| 323 | */ |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 324 | static const struct inode_operations proc_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | .lookup = proc_lookup, |
Miklos Szeredi | 2b579be | 2005-09-06 15:17:18 -0700 | [diff] [blame] | 326 | .getattr = proc_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | .setattr = proc_notify_change, |
| 328 | }; |
| 329 | |
| 330 | static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp) |
| 331 | { |
Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 332 | int ret; |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 333 | |
Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 334 | ret = proc_alloc_inum(&dp->low_ino); |
| 335 | if (ret) |
| 336 | return ret; |
Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 337 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 338 | write_lock(&proc_subdir_lock); |
Changli Gao | 99fc06d | 2007-07-15 23:40:09 -0700 | [diff] [blame] | 339 | dp->parent = dir; |
Debabrata Banerjee | b208d54 | 2014-12-10 15:45:04 -0800 | [diff] [blame] | 340 | if (pde_subdir_insert(dir, dp) == false) { |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 341 | WARN(1, "proc_dir_entry '%s/%s' already registered\n", |
| 342 | dir->name, dp->name); |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 343 | write_unlock(&proc_subdir_lock); |
Debabrata Banerjee | b208d54 | 2014-12-10 15:45:04 -0800 | [diff] [blame] | 344 | proc_free_inum(dp->low_ino); |
| 345 | return -EEXIST; |
| 346 | } |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 347 | write_unlock(&proc_subdir_lock); |
Changli Gao | 99fc06d | 2007-07-15 23:40:09 -0700 | [diff] [blame] | 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 352 | static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | const char *name, |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 354 | umode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | nlink_t nlink) |
| 356 | { |
| 357 | struct proc_dir_entry *ent = NULL; |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 358 | const char *fn; |
| 359 | struct qstr qstr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Alexey Dobriyan | 7cee4e0 | 2008-04-29 01:01:40 -0700 | [diff] [blame] | 361 | if (xlate_proc_name(name, parent, &fn) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | goto out; |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 363 | qstr.name = fn; |
| 364 | qstr.len = strlen(fn); |
| 365 | if (qstr.len == 0 || qstr.len >= 256) { |
| 366 | WARN(1, "name len %u\n", qstr.len); |
| 367 | return NULL; |
| 368 | } |
Alexey Dobriyan | b77d70d | 2018-04-10 16:32:11 -0700 | [diff] [blame] | 369 | if (qstr.len == 1 && fn[0] == '.') { |
| 370 | WARN(1, "name '.'\n"); |
| 371 | return NULL; |
| 372 | } |
| 373 | if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') { |
| 374 | WARN(1, "name '..'\n"); |
| 375 | return NULL; |
| 376 | } |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 377 | if (*parent == &proc_root && name_to_int(&qstr) != ~0U) { |
| 378 | WARN(1, "create '/proc/%s' by hand\n", qstr.name); |
| 379 | return NULL; |
| 380 | } |
Eric W. Biederman | eb6d38d | 2015-05-11 16:44:25 -0500 | [diff] [blame] | 381 | if (is_empty_pde(*parent)) { |
| 382 | WARN(1, "attempt to add to permanently empty directory"); |
| 383 | return NULL; |
| 384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 386 | ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL); |
yan | 17baa2a | 2012-10-04 17:15:43 -0700 | [diff] [blame] | 387 | if (!ent) |
| 388 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 390 | if (qstr.len + 1 <= sizeof(ent->inline_name)) { |
| 391 | ent->name = ent->inline_name; |
| 392 | } else { |
| 393 | ent->name = kmalloc(qstr.len + 1, GFP_KERNEL); |
| 394 | if (!ent->name) { |
| 395 | pde_free(ent); |
| 396 | return NULL; |
| 397 | } |
| 398 | } |
| 399 | |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 400 | memcpy(ent->name, fn, qstr.len + 1); |
| 401 | ent->namelen = qstr.len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | ent->mode = mode; |
| 403 | ent->nlink = nlink; |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 404 | ent->subdir = RB_ROOT_CACHED; |
Alexey Dobriyan | 9cdd83e | 2018-04-10 16:32:14 -0700 | [diff] [blame^] | 405 | refcount_set(&ent->refcnt, 1); |
Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 406 | spin_lock_init(&ent->pde_unload_lock); |
Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 407 | INIT_LIST_HEAD(&ent->pde_openers); |
Dmitry Torokhov | c110486 | 2016-08-10 14:36:01 -0700 | [diff] [blame] | 408 | proc_set_user(ent, (*parent)->uid, (*parent)->gid); |
| 409 | |
yan | 17baa2a | 2012-10-04 17:15:43 -0700 | [diff] [blame] | 410 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | return ent; |
| 412 | } |
| 413 | |
| 414 | struct proc_dir_entry *proc_symlink(const char *name, |
| 415 | struct proc_dir_entry *parent, const char *dest) |
| 416 | { |
| 417 | struct proc_dir_entry *ent; |
| 418 | |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 419 | ent = __proc_create(&parent, name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1); |
| 421 | |
| 422 | if (ent) { |
| 423 | ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL); |
| 424 | if (ent->data) { |
| 425 | strcpy((char*)ent->data,dest); |
Al Viro | d443b9f | 2014-12-25 16:47:49 -0500 | [diff] [blame] | 426 | ent->proc_iops = &proc_link_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | if (proc_register(parent, ent) < 0) { |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 428 | pde_free(ent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | ent = NULL; |
| 430 | } |
| 431 | } else { |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 432 | pde_free(ent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | ent = NULL; |
| 434 | } |
| 435 | } |
| 436 | return ent; |
| 437 | } |
Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 438 | EXPORT_SYMBOL(proc_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
David Howells | 270b5ac | 2013-04-12 02:48:30 +0100 | [diff] [blame] | 440 | struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode, |
| 441 | struct proc_dir_entry *parent, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
| 443 | struct proc_dir_entry *ent; |
| 444 | |
David Howells | 270b5ac | 2013-04-12 02:48:30 +0100 | [diff] [blame] | 445 | if (mode == 0) |
| 446 | mode = S_IRUGO | S_IXUGO; |
| 447 | |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 448 | ent = __proc_create(&parent, name, S_IFDIR | mode, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | if (ent) { |
David Howells | 270b5ac | 2013-04-12 02:48:30 +0100 | [diff] [blame] | 450 | ent->data = data; |
Al Viro | d443b9f | 2014-12-25 16:47:49 -0500 | [diff] [blame] | 451 | ent->proc_fops = &proc_dir_operations; |
| 452 | ent->proc_iops = &proc_dir_inode_operations; |
| 453 | parent->nlink++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (proc_register(parent, ent) < 0) { |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 455 | pde_free(ent); |
Al Viro | d443b9f | 2014-12-25 16:47:49 -0500 | [diff] [blame] | 456 | parent->nlink--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | ent = NULL; |
| 458 | } |
| 459 | } |
| 460 | return ent; |
| 461 | } |
David Howells | 270b5ac | 2013-04-12 02:48:30 +0100 | [diff] [blame] | 462 | EXPORT_SYMBOL_GPL(proc_mkdir_data); |
| 463 | |
| 464 | struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode, |
| 465 | struct proc_dir_entry *parent) |
| 466 | { |
| 467 | return proc_mkdir_data(name, mode, parent, NULL); |
| 468 | } |
Alexey Dobriyan | 011159a | 2011-05-14 00:12:48 +0300 | [diff] [blame] | 469 | EXPORT_SYMBOL(proc_mkdir_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | struct proc_dir_entry *proc_mkdir(const char *name, |
| 472 | struct proc_dir_entry *parent) |
| 473 | { |
David Howells | 270b5ac | 2013-04-12 02:48:30 +0100 | [diff] [blame] | 474 | return proc_mkdir_data(name, 0, parent, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 476 | EXPORT_SYMBOL(proc_mkdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Eric W. Biederman | eb6d38d | 2015-05-11 16:44:25 -0500 | [diff] [blame] | 478 | struct proc_dir_entry *proc_create_mount_point(const char *name) |
| 479 | { |
| 480 | umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 481 | struct proc_dir_entry *ent, *parent = NULL; |
| 482 | |
| 483 | ent = __proc_create(&parent, name, mode, 2); |
| 484 | if (ent) { |
| 485 | ent->data = NULL; |
| 486 | ent->proc_fops = NULL; |
| 487 | ent->proc_iops = NULL; |
Takashi Iwai | d66bb16 | 2017-04-28 15:00:15 +0200 | [diff] [blame] | 488 | parent->nlink++; |
Eric W. Biederman | eb6d38d | 2015-05-11 16:44:25 -0500 | [diff] [blame] | 489 | if (proc_register(parent, ent) < 0) { |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 490 | pde_free(ent); |
Eric W. Biederman | eb6d38d | 2015-05-11 16:44:25 -0500 | [diff] [blame] | 491 | parent->nlink--; |
| 492 | ent = NULL; |
| 493 | } |
| 494 | } |
| 495 | return ent; |
| 496 | } |
Seth Forshee | f97df70 | 2016-11-14 11:12:56 +0000 | [diff] [blame] | 497 | EXPORT_SYMBOL(proc_create_mount_point); |
Eric W. Biederman | eb6d38d | 2015-05-11 16:44:25 -0500 | [diff] [blame] | 498 | |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 499 | struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, |
Denis V. Lunev | 59b7435 | 2008-04-29 01:02:00 -0700 | [diff] [blame] | 500 | struct proc_dir_entry *parent, |
| 501 | const struct file_operations *proc_fops, |
| 502 | void *data) |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 503 | { |
| 504 | struct proc_dir_entry *pde; |
Al Viro | b6cdc73 | 2013-03-30 21:20:14 -0400 | [diff] [blame] | 505 | if ((mode & S_IFMT) == 0) |
| 506 | mode |= S_IFREG; |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 507 | |
Al Viro | b6cdc73 | 2013-03-30 21:20:14 -0400 | [diff] [blame] | 508 | if (!S_ISREG(mode)) { |
| 509 | WARN_ON(1); /* use proc_mkdir() */ |
| 510 | return NULL; |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Al Viro | d443b9f | 2014-12-25 16:47:49 -0500 | [diff] [blame] | 513 | BUG_ON(proc_fops == NULL); |
| 514 | |
Al Viro | b6cdc73 | 2013-03-30 21:20:14 -0400 | [diff] [blame] | 515 | if ((mode & S_IALLUGO) == 0) |
| 516 | mode |= S_IRUGO; |
| 517 | pde = __proc_create(&parent, name, mode, 1); |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 518 | if (!pde) |
| 519 | goto out; |
| 520 | pde->proc_fops = proc_fops; |
Denis V. Lunev | 59b7435 | 2008-04-29 01:02:00 -0700 | [diff] [blame] | 521 | pde->data = data; |
Al Viro | d443b9f | 2014-12-25 16:47:49 -0500 | [diff] [blame] | 522 | pde->proc_iops = &proc_file_inode_operations; |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 523 | if (proc_register(parent, pde) < 0) |
| 524 | goto out_free; |
| 525 | return pde; |
| 526 | out_free: |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 527 | pde_free(pde); |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 528 | out: |
| 529 | return NULL; |
| 530 | } |
Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 531 | EXPORT_SYMBOL(proc_create_data); |
David Howells | 271a15e | 2013-04-12 00:38:51 +0100 | [diff] [blame] | 532 | |
Alexey Dobriyan | 855d976 | 2017-09-08 16:13:38 -0700 | [diff] [blame] | 533 | struct proc_dir_entry *proc_create(const char *name, umode_t mode, |
| 534 | struct proc_dir_entry *parent, |
| 535 | const struct file_operations *proc_fops) |
| 536 | { |
| 537 | return proc_create_data(name, mode, parent, proc_fops, NULL); |
| 538 | } |
| 539 | EXPORT_SYMBOL(proc_create); |
| 540 | |
David Howells | 271a15e | 2013-04-12 00:38:51 +0100 | [diff] [blame] | 541 | void proc_set_size(struct proc_dir_entry *de, loff_t size) |
| 542 | { |
| 543 | de->size = size; |
| 544 | } |
| 545 | EXPORT_SYMBOL(proc_set_size); |
| 546 | |
| 547 | void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid) |
| 548 | { |
| 549 | de->uid = uid; |
| 550 | de->gid = gid; |
| 551 | } |
| 552 | EXPORT_SYMBOL(proc_set_user); |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 553 | |
Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 554 | void pde_put(struct proc_dir_entry *pde) |
| 555 | { |
Alexey Dobriyan | 9cdd83e | 2018-04-10 16:32:14 -0700 | [diff] [blame^] | 556 | if (refcount_dec_and_test(&pde->refcnt)) { |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 557 | proc_free_inum(pde->low_ino); |
| 558 | pde_free(pde); |
| 559 | } |
Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 562 | /* |
| 563 | * Remove a /proc entry and free it if it's not currently in use. |
| 564 | */ |
| 565 | void remove_proc_entry(const char *name, struct proc_dir_entry *parent) |
| 566 | { |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 567 | struct proc_dir_entry *de = NULL; |
| 568 | const char *fn = name; |
| 569 | unsigned int len; |
| 570 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 571 | write_lock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 572 | if (__xlate_proc_name(name, &parent, &fn) != 0) { |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 573 | write_unlock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 574 | return; |
| 575 | } |
| 576 | len = strlen(fn); |
| 577 | |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 578 | de = pde_subdir_find(parent, fn, len); |
| 579 | if (de) |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 580 | rb_erase_cached(&de->subdir_node, &parent->subdir); |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 581 | write_unlock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 582 | if (!de) { |
| 583 | WARN(1, "name '%s'\n", name); |
| 584 | return; |
| 585 | } |
| 586 | |
Al Viro | 866ad9a7 | 2013-04-03 19:07:30 -0400 | [diff] [blame] | 587 | proc_entry_rundown(de); |
Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 588 | |
Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 589 | if (S_ISDIR(de->mode)) |
| 590 | parent->nlink--; |
| 591 | de->nlink = 0; |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 592 | WARN(pde_subdir_first(de), |
| 593 | "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n", |
| 594 | __func__, de->parent->name, de->name, pde_subdir_first(de)->name); |
Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 595 | pde_put(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 597 | EXPORT_SYMBOL(remove_proc_entry); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 598 | |
| 599 | int remove_proc_subtree(const char *name, struct proc_dir_entry *parent) |
| 600 | { |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 601 | struct proc_dir_entry *root = NULL, *de, *next; |
| 602 | const char *fn = name; |
| 603 | unsigned int len; |
| 604 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 605 | write_lock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 606 | if (__xlate_proc_name(name, &parent, &fn) != 0) { |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 607 | write_unlock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 608 | return -ENOENT; |
| 609 | } |
| 610 | len = strlen(fn); |
| 611 | |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 612 | root = pde_subdir_find(parent, fn, len); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 613 | if (!root) { |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 614 | write_unlock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 615 | return -ENOENT; |
| 616 | } |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 617 | rb_erase_cached(&root->subdir_node, &parent->subdir); |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 618 | |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 619 | de = root; |
| 620 | while (1) { |
Nicolas Dichtel | 710585d | 2014-12-10 15:45:01 -0800 | [diff] [blame] | 621 | next = pde_subdir_first(de); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 622 | if (next) { |
Davidlohr Bueso | 410bd5e | 2017-09-08 16:15:15 -0700 | [diff] [blame] | 623 | rb_erase_cached(&next->subdir_node, &de->subdir); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 624 | de = next; |
| 625 | continue; |
| 626 | } |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 627 | write_unlock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 628 | |
Al Viro | 866ad9a7 | 2013-04-03 19:07:30 -0400 | [diff] [blame] | 629 | proc_entry_rundown(de); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 630 | next = de->parent; |
| 631 | if (S_ISDIR(de->mode)) |
| 632 | next->nlink--; |
| 633 | de->nlink = 0; |
| 634 | if (de == root) |
| 635 | break; |
| 636 | pde_put(de); |
| 637 | |
Waiman Long | ecf1a3d | 2015-09-09 15:35:57 -0700 | [diff] [blame] | 638 | write_lock(&proc_subdir_lock); |
Al Viro | 8ce584c | 2013-03-30 20:13:46 -0400 | [diff] [blame] | 639 | de = next; |
| 640 | } |
| 641 | pde_put(root); |
| 642 | return 0; |
| 643 | } |
| 644 | EXPORT_SYMBOL(remove_proc_subtree); |
David Howells | 4a520d2 | 2013-04-12 14:06:01 +0100 | [diff] [blame] | 645 | |
| 646 | void *proc_get_parent_data(const struct inode *inode) |
| 647 | { |
| 648 | struct proc_dir_entry *de = PDE(inode); |
| 649 | return de->parent->data; |
| 650 | } |
| 651 | EXPORT_SYMBOL_GPL(proc_get_parent_data); |
David Howells | a8ca16e | 2013-04-12 17:27:28 +0100 | [diff] [blame] | 652 | |
| 653 | void proc_remove(struct proc_dir_entry *de) |
| 654 | { |
| 655 | if (de) |
| 656 | remove_proc_subtree(de->name, de->parent); |
| 657 | } |
| 658 | EXPORT_SYMBOL(proc_remove); |
David Howells | c30480b | 2013-04-12 18:03:36 +0100 | [diff] [blame] | 659 | |
| 660 | void *PDE_DATA(const struct inode *inode) |
| 661 | { |
| 662 | return __PDE_DATA(inode); |
| 663 | } |
| 664 | EXPORT_SYMBOL(PDE_DATA); |