177b14db5SEric W. Biederman /* 277b14db5SEric W. Biederman * /proc/sys support 377b14db5SEric W. Biederman */ 41e0edd3fSAlexey Dobriyan #include <linux/init.h> 577b14db5SEric W. Biederman #include <linux/sysctl.h> 6f1ecf068SLucas De Marchi #include <linux/poll.h> 777b14db5SEric W. Biederman #include <linux/proc_fs.h> 887ebdc00SAndrew Morton #include <linux/printk.h> 977b14db5SEric W. Biederman #include <linux/security.h> 1040401530SAl Viro #include <linux/sched.h> 1134286d66SNick Piggin #include <linux/namei.h> 1240401530SAl Viro #include <linux/mm.h> 131f87f0b5SEric W. Biederman #include <linux/module.h> 1477b14db5SEric W. Biederman #include "internal.h" 1577b14db5SEric W. Biederman 16d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations; 1777b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations; 1803a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations; 199043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations; 209043476fSAl Viro static const struct inode_operations proc_sys_dir_operations; 2177b14db5SEric W. Biederman 22f9bd6733SEric W. Biederman /* Support for permanently empty directories */ 23f9bd6733SEric W. Biederman 24f9bd6733SEric W. Biederman struct ctl_table sysctl_mount_point[] = { 25f9bd6733SEric W. Biederman { } 26f9bd6733SEric W. Biederman }; 27f9bd6733SEric W. Biederman 28f9bd6733SEric W. Biederman static bool is_empty_dir(struct ctl_table_header *head) 29f9bd6733SEric W. Biederman { 30f9bd6733SEric W. Biederman return head->ctl_table[0].child == sysctl_mount_point; 31f9bd6733SEric W. Biederman } 32f9bd6733SEric W. Biederman 33f9bd6733SEric W. Biederman static void set_empty_dir(struct ctl_dir *dir) 34f9bd6733SEric W. Biederman { 35f9bd6733SEric W. Biederman dir->header.ctl_table[0].child = sysctl_mount_point; 36f9bd6733SEric W. Biederman } 37f9bd6733SEric W. Biederman 38f9bd6733SEric W. Biederman static void clear_empty_dir(struct ctl_dir *dir) 39f9bd6733SEric W. Biederman 40f9bd6733SEric W. Biederman { 41f9bd6733SEric W. Biederman dir->header.ctl_table[0].child = NULL; 42f9bd6733SEric W. Biederman } 43f9bd6733SEric W. Biederman 44f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll) 45f1ecf068SLucas De Marchi { 46f1ecf068SLucas De Marchi if (!poll) 47f1ecf068SLucas De Marchi return; 48f1ecf068SLucas De Marchi 49f1ecf068SLucas De Marchi atomic_inc(&poll->event); 50f1ecf068SLucas De Marchi wake_up_interruptible(&poll->wait); 51f1ecf068SLucas De Marchi } 52f1ecf068SLucas De Marchi 53a194558eSEric W. Biederman static struct ctl_table root_table[] = { 54a194558eSEric W. Biederman { 55a194558eSEric W. Biederman .procname = "", 567ec66d06SEric W. Biederman .mode = S_IFDIR|S_IRUGO|S_IXUGO, 57a194558eSEric W. Biederman }, 58a194558eSEric W. Biederman { } 59a194558eSEric W. Biederman }; 600e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = { 610e47c99dSEric W. Biederman .default_set.dir.header = { 621f87f0b5SEric W. Biederman {{.count = 1, 63938aaa4fSEric W. Biederman .nreg = 1, 64ac13ac6fSEric W. Biederman .ctl_table = root_table }}, 650e47c99dSEric W. Biederman .ctl_table_arg = root_table, 661f87f0b5SEric W. Biederman .root = &sysctl_table_root, 671f87f0b5SEric W. Biederman .set = &sysctl_table_root.default_set, 687ec66d06SEric W. Biederman }, 691f87f0b5SEric W. Biederman }; 701f87f0b5SEric W. Biederman 711f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 721f87f0b5SEric W. Biederman 737ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header); 740e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 7513bcc6a2SEric W. Biederman struct ctl_table **pentry); 760e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head); 770e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header); 787ec66d06SEric W. Biederman 796980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir) 806980128fSEric W. Biederman { 816980128fSEric W. Biederman if (dir->header.parent) 826980128fSEric W. Biederman sysctl_print_dir(dir->header.parent); 8387ebdc00SAndrew Morton pr_cont("%s/", dir->header.ctl_table[0].procname); 846980128fSEric W. Biederman } 856980128fSEric W. Biederman 86076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 87076c3eedSEric W. Biederman { 88076c3eedSEric W. Biederman int minlen; 89076c3eedSEric W. Biederman int cmp; 90076c3eedSEric W. Biederman 91076c3eedSEric W. Biederman minlen = len1; 92076c3eedSEric W. Biederman if (minlen > len2) 93076c3eedSEric W. Biederman minlen = len2; 94076c3eedSEric W. Biederman 95076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 96076c3eedSEric W. Biederman if (cmp == 0) 97076c3eedSEric W. Biederman cmp = len1 - len2; 98076c3eedSEric W. Biederman return cmp; 99076c3eedSEric W. Biederman } 100076c3eedSEric W. Biederman 10160f126d9SEric W. Biederman /* Called under sysctl_lock */ 102076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 1030e47c99dSEric W. Biederman struct ctl_dir *dir, const char *name, int namelen) 104076c3eedSEric W. Biederman { 105076c3eedSEric W. Biederman struct ctl_table_header *head; 106076c3eedSEric W. Biederman struct ctl_table *entry; 107ac13ac6fSEric W. Biederman struct rb_node *node = dir->root.rb_node; 108076c3eedSEric W. Biederman 109ac13ac6fSEric W. Biederman while (node) 110ac13ac6fSEric W. Biederman { 111ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 112ac13ac6fSEric W. Biederman const char *procname; 113ac13ac6fSEric W. Biederman int cmp; 114ac13ac6fSEric W. Biederman 115ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 116ac13ac6fSEric W. Biederman head = ctl_node->header; 117ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 118ac13ac6fSEric W. Biederman procname = entry->procname; 119ac13ac6fSEric W. Biederman 120ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, procname, strlen(procname)); 121ac13ac6fSEric W. Biederman if (cmp < 0) 122ac13ac6fSEric W. Biederman node = node->rb_left; 123ac13ac6fSEric W. Biederman else if (cmp > 0) 124ac13ac6fSEric W. Biederman node = node->rb_right; 125ac13ac6fSEric W. Biederman else { 126076c3eedSEric W. Biederman *phead = head; 127076c3eedSEric W. Biederman return entry; 128076c3eedSEric W. Biederman } 129076c3eedSEric W. Biederman } 130076c3eedSEric W. Biederman return NULL; 131076c3eedSEric W. Biederman } 132076c3eedSEric W. Biederman 133ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) 134ac13ac6fSEric W. Biederman { 135ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 136ac13ac6fSEric W. Biederman struct rb_node **p = &head->parent->root.rb_node; 137ac13ac6fSEric W. Biederman struct rb_node *parent = NULL; 138ac13ac6fSEric W. Biederman const char *name = entry->procname; 139ac13ac6fSEric W. Biederman int namelen = strlen(name); 140ac13ac6fSEric W. Biederman 141ac13ac6fSEric W. Biederman while (*p) { 142ac13ac6fSEric W. Biederman struct ctl_table_header *parent_head; 143ac13ac6fSEric W. Biederman struct ctl_table *parent_entry; 144ac13ac6fSEric W. Biederman struct ctl_node *parent_node; 145ac13ac6fSEric W. Biederman const char *parent_name; 146ac13ac6fSEric W. Biederman int cmp; 147ac13ac6fSEric W. Biederman 148ac13ac6fSEric W. Biederman parent = *p; 149ac13ac6fSEric W. Biederman parent_node = rb_entry(parent, struct ctl_node, node); 150ac13ac6fSEric W. Biederman parent_head = parent_node->header; 151ac13ac6fSEric W. Biederman parent_entry = &parent_head->ctl_table[parent_node - parent_head->node]; 152ac13ac6fSEric W. Biederman parent_name = parent_entry->procname; 153ac13ac6fSEric W. Biederman 154ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, parent_name, strlen(parent_name)); 155ac13ac6fSEric W. Biederman if (cmp < 0) 156ac13ac6fSEric W. Biederman p = &(*p)->rb_left; 157ac13ac6fSEric W. Biederman else if (cmp > 0) 158ac13ac6fSEric W. Biederman p = &(*p)->rb_right; 159ac13ac6fSEric W. Biederman else { 16087ebdc00SAndrew Morton pr_err("sysctl duplicate entry: "); 161ac13ac6fSEric W. Biederman sysctl_print_dir(head->parent); 16287ebdc00SAndrew Morton pr_cont("/%s\n", entry->procname); 163ac13ac6fSEric W. Biederman return -EEXIST; 164ac13ac6fSEric W. Biederman } 165ac13ac6fSEric W. Biederman } 166ac13ac6fSEric W. Biederman 167ac13ac6fSEric W. Biederman rb_link_node(node, parent, p); 168ea5272f5SMichel Lespinasse rb_insert_color(node, &head->parent->root); 169ac13ac6fSEric W. Biederman return 0; 170ac13ac6fSEric W. Biederman } 171ac13ac6fSEric W. Biederman 172ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) 173ac13ac6fSEric W. Biederman { 174ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 175ac13ac6fSEric W. Biederman 176ac13ac6fSEric W. Biederman rb_erase(node, &head->parent->root); 177ac13ac6fSEric W. Biederman } 178ac13ac6fSEric W. Biederman 179e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head, 180e0d04529SEric W. Biederman struct ctl_table_root *root, struct ctl_table_set *set, 181ac13ac6fSEric W. Biederman struct ctl_node *node, struct ctl_table *table) 182e0d04529SEric W. Biederman { 1837ec66d06SEric W. Biederman head->ctl_table = table; 184e0d04529SEric W. Biederman head->ctl_table_arg = table; 185e0d04529SEric W. Biederman head->used = 0; 186e0d04529SEric W. Biederman head->count = 1; 187e0d04529SEric W. Biederman head->nreg = 1; 188e0d04529SEric W. Biederman head->unregistering = NULL; 189e0d04529SEric W. Biederman head->root = root; 190e0d04529SEric W. Biederman head->set = set; 191e0d04529SEric W. Biederman head->parent = NULL; 192ac13ac6fSEric W. Biederman head->node = node; 193d6cffbbeSKonstantin Khlebnikov INIT_LIST_HEAD(&head->inodes); 194ac13ac6fSEric W. Biederman if (node) { 195ac13ac6fSEric W. Biederman struct ctl_table *entry; 1964c199a93SMichel Lespinasse for (entry = table; entry->procname; entry++, node++) 197ac13ac6fSEric W. Biederman node->header = head; 198ac13ac6fSEric W. Biederman } 199ac13ac6fSEric W. Biederman } 200e0d04529SEric W. Biederman 2018425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head) 2028425d6aaSEric W. Biederman { 203ac13ac6fSEric W. Biederman struct ctl_table *entry; 204ac13ac6fSEric W. Biederman for (entry = head->ctl_table; entry->procname; entry++) 205ac13ac6fSEric W. Biederman erase_entry(head, entry); 2068425d6aaSEric W. Biederman } 2078425d6aaSEric W. Biederman 2080e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) 2098425d6aaSEric W. Biederman { 210ac13ac6fSEric W. Biederman struct ctl_table *entry; 2110e47c99dSEric W. Biederman int err; 2120e47c99dSEric W. Biederman 213f9bd6733SEric W. Biederman /* Is this a permanently empty directory? */ 214f9bd6733SEric W. Biederman if (is_empty_dir(&dir->header)) 215f9bd6733SEric W. Biederman return -EROFS; 216f9bd6733SEric W. Biederman 217f9bd6733SEric W. Biederman /* Am I creating a permanently empty directory? */ 218f9bd6733SEric W. Biederman if (header->ctl_table == sysctl_mount_point) { 219f9bd6733SEric W. Biederman if (!RB_EMPTY_ROOT(&dir->root)) 220f9bd6733SEric W. Biederman return -EINVAL; 221f9bd6733SEric W. Biederman set_empty_dir(dir); 222f9bd6733SEric W. Biederman } 223f9bd6733SEric W. Biederman 2240e47c99dSEric W. Biederman dir->header.nreg++; 2257ec66d06SEric W. Biederman header->parent = dir; 2260e47c99dSEric W. Biederman err = insert_links(header); 2270e47c99dSEric W. Biederman if (err) 2280e47c99dSEric W. Biederman goto fail_links; 229ac13ac6fSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 230ac13ac6fSEric W. Biederman err = insert_entry(header, entry); 231ac13ac6fSEric W. Biederman if (err) 232ac13ac6fSEric W. Biederman goto fail; 233ac13ac6fSEric W. Biederman } 2340e47c99dSEric W. Biederman return 0; 235ac13ac6fSEric W. Biederman fail: 236ac13ac6fSEric W. Biederman erase_header(header); 237ac13ac6fSEric W. Biederman put_links(header); 2380e47c99dSEric W. Biederman fail_links: 239f9bd6733SEric W. Biederman if (header->ctl_table == sysctl_mount_point) 240f9bd6733SEric W. Biederman clear_empty_dir(dir); 2410e47c99dSEric W. Biederman header->parent = NULL; 2420e47c99dSEric W. Biederman drop_sysctl_table(&dir->header); 2430e47c99dSEric W. Biederman return err; 2448425d6aaSEric W. Biederman } 2458425d6aaSEric W. Biederman 2461f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2471f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 2481f87f0b5SEric W. Biederman { 2491f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2501f87f0b5SEric W. Biederman return 0; 2511f87f0b5SEric W. Biederman p->used++; 2521f87f0b5SEric W. Biederman return 1; 2531f87f0b5SEric W. Biederman } 2541f87f0b5SEric W. Biederman 2551f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2561f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 2571f87f0b5SEric W. Biederman { 2581f87f0b5SEric W. Biederman if (!--p->used) 2591f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2601f87f0b5SEric W. Biederman complete(p->unregistering); 2611f87f0b5SEric W. Biederman } 2621f87f0b5SEric W. Biederman 263d6cffbbeSKonstantin Khlebnikov /* called under sysctl_lock */ 264d6cffbbeSKonstantin Khlebnikov static void proc_sys_prune_dcache(struct ctl_table_header *head) 265d6cffbbeSKonstantin Khlebnikov { 266d6cffbbeSKonstantin Khlebnikov struct inode *inode, *prev = NULL; 267d6cffbbeSKonstantin Khlebnikov struct proc_inode *ei; 268d6cffbbeSKonstantin Khlebnikov 269ace0c791SEric W. Biederman rcu_read_lock(); 270ace0c791SEric W. Biederman list_for_each_entry_rcu(ei, &head->inodes, sysctl_inodes) { 271d6cffbbeSKonstantin Khlebnikov inode = igrab(&ei->vfs_inode); 272d6cffbbeSKonstantin Khlebnikov if (inode) { 273ace0c791SEric W. Biederman rcu_read_unlock(); 274d6cffbbeSKonstantin Khlebnikov iput(prev); 275d6cffbbeSKonstantin Khlebnikov prev = inode; 276d6cffbbeSKonstantin Khlebnikov d_prune_aliases(inode); 277ace0c791SEric W. Biederman rcu_read_lock(); 278d6cffbbeSKonstantin Khlebnikov } 279d6cffbbeSKonstantin Khlebnikov } 280ace0c791SEric W. Biederman rcu_read_unlock(); 281d6cffbbeSKonstantin Khlebnikov iput(prev); 282d6cffbbeSKonstantin Khlebnikov } 283d6cffbbeSKonstantin Khlebnikov 2841f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 2851f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 2861f87f0b5SEric W. Biederman { 2871f87f0b5SEric W. Biederman /* 2881f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 2891f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 2901f87f0b5SEric W. Biederman */ 2911f87f0b5SEric W. Biederman if (unlikely(p->used)) { 2921f87f0b5SEric W. Biederman struct completion wait; 2931f87f0b5SEric W. Biederman init_completion(&wait); 2941f87f0b5SEric W. Biederman p->unregistering = &wait; 2951f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2961f87f0b5SEric W. Biederman wait_for_completion(&wait); 2971f87f0b5SEric W. Biederman } else { 2981f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 2991f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 300ace0c791SEric W. Biederman spin_unlock(&sysctl_lock); 3011f87f0b5SEric W. Biederman } 3021f87f0b5SEric W. Biederman /* 303d6cffbbeSKonstantin Khlebnikov * Prune dentries for unregistered sysctls: namespaced sysctls 304d6cffbbeSKonstantin Khlebnikov * can have duplicate names and contaminate dcache very badly. 305d6cffbbeSKonstantin Khlebnikov */ 306d6cffbbeSKonstantin Khlebnikov proc_sys_prune_dcache(p); 307d6cffbbeSKonstantin Khlebnikov /* 3081f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 3091f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 3101f87f0b5SEric W. Biederman */ 311ace0c791SEric W. Biederman spin_lock(&sysctl_lock); 3128425d6aaSEric W. Biederman erase_header(p); 3131f87f0b5SEric W. Biederman } 3141f87f0b5SEric W. Biederman 3151f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 3161f87f0b5SEric W. Biederman { 317ab4a1f24SPrasad Joshi BUG_ON(!head); 3181f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3191f87f0b5SEric W. Biederman if (!use_table(head)) 3201f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 3211f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3221f87f0b5SEric W. Biederman return head; 3231f87f0b5SEric W. Biederman } 3241f87f0b5SEric W. Biederman 3251f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 3261f87f0b5SEric W. Biederman { 3271f87f0b5SEric W. Biederman if (!head) 3281f87f0b5SEric W. Biederman return; 3291f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 3301f87f0b5SEric W. Biederman unuse_table(head); 3311f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 3321f87f0b5SEric W. Biederman } 3331f87f0b5SEric W. Biederman 3341f87f0b5SEric W. Biederman static struct ctl_table_set * 33513bcc6a2SEric W. Biederman lookup_header_set(struct ctl_table_root *root) 3361f87f0b5SEric W. Biederman { 3371f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 3381f87f0b5SEric W. Biederman if (root->lookup) 33913bcc6a2SEric W. Biederman set = root->lookup(root); 3401f87f0b5SEric W. Biederman return set; 3411f87f0b5SEric W. Biederman } 3421f87f0b5SEric W. Biederman 343076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 3447ec66d06SEric W. Biederman struct ctl_dir *dir, 345076c3eedSEric W. Biederman const char *name, int namelen) 346076c3eedSEric W. Biederman { 347076c3eedSEric W. Biederman struct ctl_table_header *head; 348076c3eedSEric W. Biederman struct ctl_table *entry; 349076c3eedSEric W. Biederman 350076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 3510e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 352076c3eedSEric W. Biederman if (entry && use_table(head)) 353076c3eedSEric W. Biederman *phead = head; 354076c3eedSEric W. Biederman else 355076c3eedSEric W. Biederman entry = NULL; 356076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 357076c3eedSEric W. Biederman return entry; 358076c3eedSEric W. Biederman } 359076c3eedSEric W. Biederman 360ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node) 3611f87f0b5SEric W. Biederman { 362ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 3631f87f0b5SEric W. Biederman 364ac13ac6fSEric W. Biederman for (;node; node = rb_next(node)) { 365ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 366ac13ac6fSEric W. Biederman if (use_table(ctl_node->header)) 367ac13ac6fSEric W. Biederman return ctl_node; 3681f87f0b5SEric W. Biederman } 3691f87f0b5SEric W. Biederman return NULL; 3701f87f0b5SEric W. Biederman } 3711f87f0b5SEric W. Biederman 3727ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir, 3736a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 3741f87f0b5SEric W. Biederman { 375ac13ac6fSEric W. Biederman struct ctl_table_header *head = NULL; 3767ec66d06SEric W. Biederman struct ctl_table *entry = NULL; 377ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 3786a75ce16SEric W. Biederman 3796a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 380ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_first(&dir->root)); 3816a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 382ac13ac6fSEric W. Biederman if (ctl_node) { 383ac13ac6fSEric W. Biederman head = ctl_node->header; 384ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 385ac13ac6fSEric W. Biederman } 3866a75ce16SEric W. Biederman *phead = head; 3876a75ce16SEric W. Biederman *pentry = entry; 3886a75ce16SEric W. Biederman } 3896a75ce16SEric W. Biederman 3907ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) 3916a75ce16SEric W. Biederman { 3926a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 3936a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 394ac13ac6fSEric W. Biederman struct ctl_node *ctl_node = &head->node[entry - head->ctl_table]; 3956a75ce16SEric W. Biederman 3966a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 3976a75ce16SEric W. Biederman unuse_table(head); 398ac13ac6fSEric W. Biederman 399ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_next(&ctl_node->node)); 4006a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 401ac13ac6fSEric W. Biederman head = NULL; 402ac13ac6fSEric W. Biederman if (ctl_node) { 403ac13ac6fSEric W. Biederman head = ctl_node->header; 404ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 4056a75ce16SEric W. Biederman } 4066a75ce16SEric W. Biederman *phead = head; 4076a75ce16SEric W. Biederman *pentry = entry; 4081f87f0b5SEric W. Biederman } 4091f87f0b5SEric W. Biederman 4101f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root) 4111f87f0b5SEric W. Biederman { 4121f87f0b5SEric W. Biederman } 4131f87f0b5SEric W. Biederman 4141f87f0b5SEric W. Biederman /* 4151f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 4161f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 4171f87f0b5SEric W. Biederman */ 4181f87f0b5SEric W. Biederman 4191f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 4201f87f0b5SEric W. Biederman { 421091bd3eaSEric W. Biederman if (uid_eq(current_euid(), GLOBAL_ROOT_UID)) 4221f87f0b5SEric W. Biederman mode >>= 6; 423091bd3eaSEric W. Biederman else if (in_egroup_p(GLOBAL_ROOT_GID)) 4241f87f0b5SEric W. Biederman mode >>= 3; 4251f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 4261f87f0b5SEric W. Biederman return 0; 4271f87f0b5SEric W. Biederman return -EACCES; 4281f87f0b5SEric W. Biederman } 4291f87f0b5SEric W. Biederman 43073f7ef43SEric W. Biederman static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op) 4311f87f0b5SEric W. Biederman { 43273f7ef43SEric W. Biederman struct ctl_table_root *root = head->root; 4331f87f0b5SEric W. Biederman int mode; 4341f87f0b5SEric W. Biederman 4351f87f0b5SEric W. Biederman if (root->permissions) 43673f7ef43SEric W. Biederman mode = root->permissions(head, table); 4371f87f0b5SEric W. Biederman else 4381f87f0b5SEric W. Biederman mode = table->mode; 4391f87f0b5SEric W. Biederman 4401f87f0b5SEric W. Biederman return test_perm(mode, op); 4411f87f0b5SEric W. Biederman } 4421f87f0b5SEric W. Biederman 4439043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 4449043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 44577b14db5SEric W. Biederman { 446e79c6a4fSDmitry Torokhov struct ctl_table_root *root = head->root; 44777b14db5SEric W. Biederman struct inode *inode; 4489043476fSAl Viro struct proc_inode *ei; 44977b14db5SEric W. Biederman 4509043476fSAl Viro inode = new_inode(sb); 45177b14db5SEric W. Biederman if (!inode) 45277b14db5SEric W. Biederman goto out; 45377b14db5SEric W. Biederman 45485fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 45585fe4025SChristoph Hellwig 45677b14db5SEric W. Biederman ei = PROC_I(inode); 4579043476fSAl Viro 458d6cffbbeSKonstantin Khlebnikov spin_lock(&sysctl_lock); 459ace0c791SEric W. Biederman if (unlikely(head->unregistering)) { 460ace0c791SEric W. Biederman spin_unlock(&sysctl_lock); 461ace0c791SEric W. Biederman iput(inode); 462ace0c791SEric W. Biederman inode = NULL; 463ace0c791SEric W. Biederman goto out; 464ace0c791SEric W. Biederman } 465ace0c791SEric W. Biederman ei->sysctl = head; 466ace0c791SEric W. Biederman ei->sysctl_entry = table; 467ace0c791SEric W. Biederman list_add_rcu(&ei->sysctl_inodes, &head->inodes); 468d6cffbbeSKonstantin Khlebnikov head->count++; 469d6cffbbeSKonstantin Khlebnikov spin_unlock(&sysctl_lock); 470d6cffbbeSKonstantin Khlebnikov 471078cd827SDeepa Dinamani inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); 4729043476fSAl Viro inode->i_mode = table->mode; 4737ec66d06SEric W. Biederman if (!S_ISDIR(table->mode)) { 4749043476fSAl Viro inode->i_mode |= S_IFREG; 47577b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 47677b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 4779043476fSAl Viro } else { 4789043476fSAl Viro inode->i_mode |= S_IFDIR; 4799043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 4809043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 481f9bd6733SEric W. Biederman if (is_empty_dir(head)) 482f9bd6733SEric W. Biederman make_empty_dir_inode(inode); 4839043476fSAl Viro } 484e79c6a4fSDmitry Torokhov 485e79c6a4fSDmitry Torokhov if (root->set_ownership) 486e79c6a4fSDmitry Torokhov root->set_ownership(head, table, &inode->i_uid, &inode->i_gid); 487e79c6a4fSDmitry Torokhov 48877b14db5SEric W. Biederman out: 48977b14db5SEric W. Biederman return inode; 49077b14db5SEric W. Biederman } 49177b14db5SEric W. Biederman 492d6cffbbeSKonstantin Khlebnikov void proc_sys_evict_inode(struct inode *inode, struct ctl_table_header *head) 493d6cffbbeSKonstantin Khlebnikov { 494d6cffbbeSKonstantin Khlebnikov spin_lock(&sysctl_lock); 495ace0c791SEric W. Biederman list_del_rcu(&PROC_I(inode)->sysctl_inodes); 496d6cffbbeSKonstantin Khlebnikov if (!--head->count) 497d6cffbbeSKonstantin Khlebnikov kfree_rcu(head, rcu); 498d6cffbbeSKonstantin Khlebnikov spin_unlock(&sysctl_lock); 499d6cffbbeSKonstantin Khlebnikov } 500d6cffbbeSKonstantin Khlebnikov 50181324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 50277b14db5SEric W. Biederman { 5033cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 5043cc3e046SEric W. Biederman if (!head) 5050e47c99dSEric W. Biederman head = &sysctl_table_root.default_set.dir.header; 5063cc3e046SEric W. Biederman return sysctl_head_grab(head); 50777b14db5SEric W. Biederman } 50877b14db5SEric W. Biederman 50977b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 51000cd8dd3SAl Viro unsigned int flags) 51177b14db5SEric W. Biederman { 5129043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 5139043476fSAl Viro struct ctl_table_header *h = NULL; 514dc12e909SAl Viro const struct qstr *name = &dentry->d_name; 5159043476fSAl Viro struct ctl_table *p; 51677b14db5SEric W. Biederman struct inode *inode; 5179043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 5187ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 5190e47c99dSEric W. Biederman int ret; 52077b14db5SEric W. Biederman 5219043476fSAl Viro if (IS_ERR(head)) 5229043476fSAl Viro return ERR_CAST(head); 5239043476fSAl Viro 5247ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 5259043476fSAl Viro 5267ec66d06SEric W. Biederman p = lookup_entry(&h, ctl_dir, name->name, name->len); 5279043476fSAl Viro if (!p) 52877b14db5SEric W. Biederman goto out; 52977b14db5SEric W. Biederman 5304e757320SEric W. Biederman if (S_ISLNK(p->mode)) { 53113bcc6a2SEric W. Biederman ret = sysctl_follow_link(&h, &p); 5320e47c99dSEric W. Biederman err = ERR_PTR(ret); 5330e47c99dSEric W. Biederman if (ret) 5340e47c99dSEric W. Biederman goto out; 5354e757320SEric W. Biederman } 5360e47c99dSEric W. Biederman 53777b14db5SEric W. Biederman err = ERR_PTR(-ENOMEM); 5389043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 53977b14db5SEric W. Biederman if (!inode) 54077b14db5SEric W. Biederman goto out; 54177b14db5SEric W. Biederman 54277b14db5SEric W. Biederman err = NULL; 543fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 54477b14db5SEric W. Biederman d_add(dentry, inode); 54577b14db5SEric W. Biederman 54677b14db5SEric W. Biederman out: 5476bf61045SFrancesco Ruggeri if (h) 5486bf61045SFrancesco Ruggeri sysctl_head_finish(h); 54977b14db5SEric W. Biederman sysctl_head_finish(head); 55077b14db5SEric W. Biederman return err; 55177b14db5SEric W. Biederman } 55277b14db5SEric W. Biederman 5537708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 5547708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 55577b14db5SEric W. Biederman { 556496ad9aaSAl Viro struct inode *inode = file_inode(filp); 5579043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 5589043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 5592a2da53bSDavid Howells ssize_t error; 5602a2da53bSDavid Howells size_t res; 56177b14db5SEric W. Biederman 5629043476fSAl Viro if (IS_ERR(head)) 5639043476fSAl Viro return PTR_ERR(head); 56477b14db5SEric W. Biederman 56577b14db5SEric W. Biederman /* 56677b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 56777b14db5SEric W. Biederman * and won't be until we finish. 56877b14db5SEric W. Biederman */ 56977b14db5SEric W. Biederman error = -EPERM; 57073f7ef43SEric W. Biederman if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ)) 57177b14db5SEric W. Biederman goto out; 57277b14db5SEric W. Biederman 5739043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 5749043476fSAl Viro error = -EINVAL; 5759043476fSAl Viro if (!table->proc_handler) 5769043476fSAl Viro goto out; 5779043476fSAl Viro 57877b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 57977b14db5SEric W. Biederman res = count; 5808d65af78SAlexey Dobriyan error = table->proc_handler(table, write, buf, &res, ppos); 58177b14db5SEric W. Biederman if (!error) 58277b14db5SEric W. Biederman error = res; 58377b14db5SEric W. Biederman out: 58477b14db5SEric W. Biederman sysctl_head_finish(head); 58577b14db5SEric W. Biederman 58677b14db5SEric W. Biederman return error; 58777b14db5SEric W. Biederman } 58877b14db5SEric W. Biederman 5897708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 5907708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 5917708bfb1SPavel Emelyanov { 5927708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 5937708bfb1SPavel Emelyanov } 5947708bfb1SPavel Emelyanov 59577b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 59677b14db5SEric W. Biederman size_t count, loff_t *ppos) 59777b14db5SEric W. Biederman { 5987708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 59977b14db5SEric W. Biederman } 60077b14db5SEric W. Biederman 601f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 602f1ecf068SLucas De Marchi { 6034e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 604f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 605f1ecf068SLucas De Marchi 6064e474a00SLucas De Marchi /* sysctl was unregistered */ 6074e474a00SLucas De Marchi if (IS_ERR(head)) 6084e474a00SLucas De Marchi return PTR_ERR(head); 6094e474a00SLucas De Marchi 610f1ecf068SLucas De Marchi if (table->poll) 611f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 612f1ecf068SLucas De Marchi 6134e474a00SLucas De Marchi sysctl_head_finish(head); 6144e474a00SLucas De Marchi 615f1ecf068SLucas De Marchi return 0; 616f1ecf068SLucas De Marchi } 617f1ecf068SLucas De Marchi 618f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) 619f1ecf068SLucas De Marchi { 620496ad9aaSAl Viro struct inode *inode = file_inode(filp); 6214e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 622f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 623f1ecf068SLucas De Marchi unsigned int ret = DEFAULT_POLLMASK; 6244e474a00SLucas De Marchi unsigned long event; 6254e474a00SLucas De Marchi 6264e474a00SLucas De Marchi /* sysctl was unregistered */ 6274e474a00SLucas De Marchi if (IS_ERR(head)) 6284e474a00SLucas De Marchi return POLLERR | POLLHUP; 629f1ecf068SLucas De Marchi 630f1ecf068SLucas De Marchi if (!table->proc_handler) 631f1ecf068SLucas De Marchi goto out; 632f1ecf068SLucas De Marchi 633f1ecf068SLucas De Marchi if (!table->poll) 634f1ecf068SLucas De Marchi goto out; 635f1ecf068SLucas De Marchi 6364e474a00SLucas De Marchi event = (unsigned long)filp->private_data; 637f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 638f1ecf068SLucas De Marchi 639f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 640f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 641f1ecf068SLucas De Marchi ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; 642f1ecf068SLucas De Marchi } 643f1ecf068SLucas De Marchi 644f1ecf068SLucas De Marchi out: 6454e474a00SLucas De Marchi sysctl_head_finish(head); 6464e474a00SLucas De Marchi 647f1ecf068SLucas De Marchi return ret; 648f1ecf068SLucas De Marchi } 64977b14db5SEric W. Biederman 650f0c3b509SAl Viro static bool proc_sys_fill_cache(struct file *file, 651f0c3b509SAl Viro struct dir_context *ctx, 6529043476fSAl Viro struct ctl_table_header *head, 6539043476fSAl Viro struct ctl_table *table) 65477b14db5SEric W. Biederman { 655f0c3b509SAl Viro struct dentry *child, *dir = file->f_path.dentry; 65677b14db5SEric W. Biederman struct inode *inode; 65777b14db5SEric W. Biederman struct qstr qname; 65877b14db5SEric W. Biederman ino_t ino = 0; 65977b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 66077b14db5SEric W. Biederman 66177b14db5SEric W. Biederman qname.name = table->procname; 66277b14db5SEric W. Biederman qname.len = strlen(table->procname); 6638387ff25SLinus Torvalds qname.hash = full_name_hash(dir, qname.name, qname.len); 66477b14db5SEric W. Biederman 66577b14db5SEric W. Biederman child = d_lookup(dir, &qname); 66677b14db5SEric W. Biederman if (!child) { 66776aab3abSAl Viro DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); 66876aab3abSAl Viro child = d_alloc_parallel(dir, &qname, &wq); 66976aab3abSAl Viro if (IS_ERR(child)) 67076aab3abSAl Viro return false; 67176aab3abSAl Viro if (d_in_lookup(child)) { 6729043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 6739043476fSAl Viro if (!inode) { 67476aab3abSAl Viro d_lookup_done(child); 6759043476fSAl Viro dput(child); 676f0c3b509SAl Viro return false; 67776aab3abSAl Viro } 678fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 6799043476fSAl Viro d_add(child, inode); 68077b14db5SEric W. Biederman } 68177b14db5SEric W. Biederman } 6822b0143b5SDavid Howells inode = d_inode(child); 68377b14db5SEric W. Biederman ino = inode->i_ino; 68477b14db5SEric W. Biederman type = inode->i_mode >> 12; 68577b14db5SEric W. Biederman dput(child); 686f0c3b509SAl Viro return dir_emit(ctx, qname.name, qname.len, ino, type); 6879043476fSAl Viro } 6889043476fSAl Viro 689f0c3b509SAl Viro static bool proc_sys_link_fill_cache(struct file *file, 690f0c3b509SAl Viro struct dir_context *ctx, 6910e47c99dSEric W. Biederman struct ctl_table_header *head, 6920e47c99dSEric W. Biederman struct ctl_table *table) 6930e47c99dSEric W. Biederman { 694f0c3b509SAl Viro bool ret = true; 6950e47c99dSEric W. Biederman head = sysctl_head_grab(head); 6960e47c99dSEric W. Biederman 6974e757320SEric W. Biederman if (S_ISLNK(table->mode)) { 6980e47c99dSEric W. Biederman /* It is not an error if we can not follow the link ignore it */ 69913bcc6a2SEric W. Biederman int err = sysctl_follow_link(&head, &table); 7000e47c99dSEric W. Biederman if (err) 7010e47c99dSEric W. Biederman goto out; 7024e757320SEric W. Biederman } 7030e47c99dSEric W. Biederman 704f0c3b509SAl Viro ret = proc_sys_fill_cache(file, ctx, head, table); 7050e47c99dSEric W. Biederman out: 7060e47c99dSEric W. Biederman sysctl_head_finish(head); 7070e47c99dSEric W. Biederman return ret; 7080e47c99dSEric W. Biederman } 7090e47c99dSEric W. Biederman 710e5eea098SJoe Perches static int scan(struct ctl_table_header *head, struct ctl_table *table, 7119043476fSAl Viro unsigned long *pos, struct file *file, 712f0c3b509SAl Viro struct dir_context *ctx) 7139043476fSAl Viro { 714f0c3b509SAl Viro bool res; 7159043476fSAl Viro 716f0c3b509SAl Viro if ((*pos)++ < ctx->pos) 717f0c3b509SAl Viro return true; 7189043476fSAl Viro 7190e47c99dSEric W. Biederman if (unlikely(S_ISLNK(table->mode))) 720f0c3b509SAl Viro res = proc_sys_link_fill_cache(file, ctx, head, table); 7210e47c99dSEric W. Biederman else 722f0c3b509SAl Viro res = proc_sys_fill_cache(file, ctx, head, table); 7239043476fSAl Viro 724f0c3b509SAl Viro if (res) 725f0c3b509SAl Viro ctx->pos = *pos; 7266a75ce16SEric W. Biederman 7276a75ce16SEric W. Biederman return res; 72877b14db5SEric W. Biederman } 72977b14db5SEric W. Biederman 730f0c3b509SAl Viro static int proc_sys_readdir(struct file *file, struct dir_context *ctx) 73177b14db5SEric W. Biederman { 732f0c3b509SAl Viro struct ctl_table_header *head = grab_header(file_inode(file)); 7339043476fSAl Viro struct ctl_table_header *h = NULL; 7346a75ce16SEric W. Biederman struct ctl_table *entry; 7357ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 73677b14db5SEric W. Biederman unsigned long pos; 73777b14db5SEric W. Biederman 7389043476fSAl Viro if (IS_ERR(head)) 7399043476fSAl Viro return PTR_ERR(head); 7409043476fSAl Viro 7417ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 74277b14db5SEric W. Biederman 743f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 74493362fa4SZhou Chengming goto out; 745f0c3b509SAl Viro 74677b14db5SEric W. Biederman pos = 2; 74777b14db5SEric W. Biederman 7487ec66d06SEric W. Biederman for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { 749f0c3b509SAl Viro if (!scan(h, entry, &pos, file, ctx)) { 7509043476fSAl Viro sysctl_head_finish(h); 7519043476fSAl Viro break; 75277b14db5SEric W. Biederman } 75377b14db5SEric W. Biederman } 75493362fa4SZhou Chengming out: 75577b14db5SEric W. Biederman sysctl_head_finish(head); 756f0c3b509SAl Viro return 0; 75777b14db5SEric W. Biederman } 75877b14db5SEric W. Biederman 75910556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 76077b14db5SEric W. Biederman { 76177b14db5SEric W. Biederman /* 76277b14db5SEric W. Biederman * sysctl entries that are not writeable, 76377b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 76477b14db5SEric W. Biederman */ 765f696a365SMiklos Szeredi struct ctl_table_header *head; 766f696a365SMiklos Szeredi struct ctl_table *table; 76777b14db5SEric W. Biederman int error; 76877b14db5SEric W. Biederman 769f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 770f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 771f696a365SMiklos Szeredi return -EACCES; 772f696a365SMiklos Szeredi 773f696a365SMiklos Szeredi head = grab_header(inode); 7749043476fSAl Viro if (IS_ERR(head)) 7759043476fSAl Viro return PTR_ERR(head); 77677b14db5SEric W. Biederman 777f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 7789043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 7799043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 7809043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 78173f7ef43SEric W. Biederman error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK); 7829043476fSAl Viro 78377b14db5SEric W. Biederman sysctl_head_finish(head); 78477b14db5SEric W. Biederman return error; 78577b14db5SEric W. Biederman } 78677b14db5SEric W. Biederman 78777b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 78877b14db5SEric W. Biederman { 7892b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 79077b14db5SEric W. Biederman int error; 79177b14db5SEric W. Biederman 79277b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 79377b14db5SEric W. Biederman return -EPERM; 79477b14db5SEric W. Biederman 79531051c85SJan Kara error = setattr_prepare(dentry, attr); 7961025774cSChristoph Hellwig if (error) 79777b14db5SEric W. Biederman return error; 7981025774cSChristoph Hellwig 7991025774cSChristoph Hellwig setattr_copy(inode, attr); 8001025774cSChristoph Hellwig mark_inode_dirty(inode); 8011025774cSChristoph Hellwig return 0; 80277b14db5SEric W. Biederman } 80377b14db5SEric W. Biederman 8049043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 8059043476fSAl Viro { 8062b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 8079043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 8089043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 8099043476fSAl Viro 8109043476fSAl Viro if (IS_ERR(head)) 8119043476fSAl Viro return PTR_ERR(head); 8129043476fSAl Viro 8139043476fSAl Viro generic_fillattr(inode, stat); 8149043476fSAl Viro if (table) 8159043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 8169043476fSAl Viro 8179043476fSAl Viro sysctl_head_finish(head); 8189043476fSAl Viro return 0; 8199043476fSAl Viro } 8209043476fSAl Viro 82177b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 822f1ecf068SLucas De Marchi .open = proc_sys_open, 823f1ecf068SLucas De Marchi .poll = proc_sys_poll, 82477b14db5SEric W. Biederman .read = proc_sys_read, 82577b14db5SEric W. Biederman .write = proc_sys_write, 8266038f373SArnd Bergmann .llseek = default_llseek, 8279043476fSAl Viro }; 8289043476fSAl Viro 8299043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 830887df078SPavel Emelyanov .read = generic_read_dir, 831f50752eaSAl Viro .iterate_shared = proc_sys_readdir, 8323222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 83377b14db5SEric W. Biederman }; 83477b14db5SEric W. Biederman 83503a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 8369043476fSAl Viro .permission = proc_sys_permission, 8379043476fSAl Viro .setattr = proc_sys_setattr, 8389043476fSAl Viro .getattr = proc_sys_getattr, 8399043476fSAl Viro }; 8409043476fSAl Viro 8419043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 84277b14db5SEric W. Biederman .lookup = proc_sys_lookup, 84377b14db5SEric W. Biederman .permission = proc_sys_permission, 84477b14db5SEric W. Biederman .setattr = proc_sys_setattr, 8459043476fSAl Viro .getattr = proc_sys_getattr, 84677b14db5SEric W. Biederman }; 84777b14db5SEric W. Biederman 8480b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags) 84977b14db5SEric W. Biederman { 8500b728e19SAl Viro if (flags & LOOKUP_RCU) 85134286d66SNick Piggin return -ECHILD; 8522b0143b5SDavid Howells return !PROC_I(d_inode(dentry))->sysctl->unregistering; 8539043476fSAl Viro } 8549043476fSAl Viro 855fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 8569043476fSAl Viro { 8572b0143b5SDavid Howells return !!PROC_I(d_inode(dentry))->sysctl->unregistering; 8589043476fSAl Viro } 8599043476fSAl Viro 8601f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 8611f87f0b5SEric W. Biederman { 8621f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 8631f87f0b5SEric W. Biederman int res; 8641f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 8651f87f0b5SEric W. Biederman if (p->unregistering) 8661f87f0b5SEric W. Biederman res = 0; 8671f87f0b5SEric W. Biederman else if (!set->is_seen) 8681f87f0b5SEric W. Biederman res = 1; 8691f87f0b5SEric W. Biederman else 8701f87f0b5SEric W. Biederman res = set->is_seen(set); 8711f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 8721f87f0b5SEric W. Biederman return res; 8731f87f0b5SEric W. Biederman } 8741f87f0b5SEric W. Biederman 8756fa67e70SAl Viro static int proc_sys_compare(const struct dentry *dentry, 876621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 8779043476fSAl Viro { 878dfef6dcdSAl Viro struct ctl_table_header *head; 879da53be12SLinus Torvalds struct inode *inode; 880da53be12SLinus Torvalds 88131e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 88231e6b01fSNick Piggin * that inode here can be NULL */ 883dfef6dcdSAl Viro /* AV: can it, indeed? */ 8842b0143b5SDavid Howells inode = d_inode_rcu(dentry); 88531e6b01fSNick Piggin if (!inode) 886dfef6dcdSAl Viro return 1; 887621e155aSNick Piggin if (name->len != len) 8889043476fSAl Viro return 1; 889621e155aSNick Piggin if (memcmp(name->name, str, len)) 8909043476fSAl Viro return 1; 891dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 892dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 89377b14db5SEric W. Biederman } 89477b14db5SEric W. Biederman 895d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 89677b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 8979043476fSAl Viro .d_delete = proc_sys_delete, 8989043476fSAl Viro .d_compare = proc_sys_compare, 89977b14db5SEric W. Biederman }; 90077b14db5SEric W. Biederman 9010e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir, 9027ec66d06SEric W. Biederman const char *name, int namelen) 9031f87f0b5SEric W. Biederman { 9047ec66d06SEric W. Biederman struct ctl_table_header *head; 9057ec66d06SEric W. Biederman struct ctl_table *entry; 9061f87f0b5SEric W. Biederman 9070e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 9087ec66d06SEric W. Biederman if (!entry) 9097ec66d06SEric W. Biederman return ERR_PTR(-ENOENT); 91051f72f4aSEric W. Biederman if (!S_ISDIR(entry->mode)) 9117ec66d06SEric W. Biederman return ERR_PTR(-ENOTDIR); 91251f72f4aSEric W. Biederman return container_of(head, struct ctl_dir, header); 9131f87f0b5SEric W. Biederman } 9141f87f0b5SEric W. Biederman 9157ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set, 9167ec66d06SEric W. Biederman const char *name, int namelen) 9171f87f0b5SEric W. Biederman { 9187ec66d06SEric W. Biederman struct ctl_table *table; 9197ec66d06SEric W. Biederman struct ctl_dir *new; 920ac13ac6fSEric W. Biederman struct ctl_node *node; 9217ec66d06SEric W. Biederman char *new_name; 9221f87f0b5SEric W. Biederman 923ac13ac6fSEric W. Biederman new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) + 924ac13ac6fSEric W. Biederman sizeof(struct ctl_table)*2 + namelen + 1, 925ac13ac6fSEric W. Biederman GFP_KERNEL); 9267ec66d06SEric W. Biederman if (!new) 9277ec66d06SEric W. Biederman return NULL; 9287ec66d06SEric W. Biederman 929ac13ac6fSEric W. Biederman node = (struct ctl_node *)(new + 1); 930ac13ac6fSEric W. Biederman table = (struct ctl_table *)(node + 1); 9317ec66d06SEric W. Biederman new_name = (char *)(table + 2); 9327ec66d06SEric W. Biederman memcpy(new_name, name, namelen); 9337ec66d06SEric W. Biederman new_name[namelen] = '\0'; 9347ec66d06SEric W. Biederman table[0].procname = new_name; 9357ec66d06SEric W. Biederman table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; 936ac13ac6fSEric W. Biederman init_header(&new->header, set->dir.header.root, set, node, table); 9377ec66d06SEric W. Biederman 9387ec66d06SEric W. Biederman return new; 9391f87f0b5SEric W. Biederman } 9401f87f0b5SEric W. Biederman 94160f126d9SEric W. Biederman /** 94260f126d9SEric W. Biederman * get_subdir - find or create a subdir with the specified name. 94360f126d9SEric W. Biederman * @dir: Directory to create the subdirectory in 94460f126d9SEric W. Biederman * @name: The name of the subdirectory to find or create 94560f126d9SEric W. Biederman * @namelen: The length of name 94660f126d9SEric W. Biederman * 94760f126d9SEric W. Biederman * Takes a directory with an elevated reference count so we know that 94860f126d9SEric W. Biederman * if we drop the lock the directory will not go away. Upon success 94960f126d9SEric W. Biederman * the reference is moved from @dir to the returned subdirectory. 95060f126d9SEric W. Biederman * Upon error an error code is returned and the reference on @dir is 95160f126d9SEric W. Biederman * simply dropped. 95260f126d9SEric W. Biederman */ 9530e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir, 9540e47c99dSEric W. Biederman const char *name, int namelen) 9557ec66d06SEric W. Biederman { 9560e47c99dSEric W. Biederman struct ctl_table_set *set = dir->header.set; 9577ec66d06SEric W. Biederman struct ctl_dir *subdir, *new = NULL; 9580eb97f38SEric W. Biederman int err; 9597ec66d06SEric W. Biederman 9607ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 9610e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 9627ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 9637ec66d06SEric W. Biederman goto found; 9647ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 9657ec66d06SEric W. Biederman goto failed; 9667ec66d06SEric W. Biederman 9677ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 9687ec66d06SEric W. Biederman new = new_dir(set, name, namelen); 9697ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 9707ec66d06SEric W. Biederman subdir = ERR_PTR(-ENOMEM); 9717ec66d06SEric W. Biederman if (!new) 9727ec66d06SEric W. Biederman goto failed; 9737ec66d06SEric W. Biederman 97460f126d9SEric W. Biederman /* Was the subdir added while we dropped the lock? */ 9750e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 9767ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 9777ec66d06SEric W. Biederman goto found; 9787ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 9797ec66d06SEric W. Biederman goto failed; 9807ec66d06SEric W. Biederman 98160f126d9SEric W. Biederman /* Nope. Use the our freshly made directory entry. */ 9820eb97f38SEric W. Biederman err = insert_header(dir, &new->header); 9830eb97f38SEric W. Biederman subdir = ERR_PTR(err); 9840eb97f38SEric W. Biederman if (err) 9850e47c99dSEric W. Biederman goto failed; 9867ec66d06SEric W. Biederman subdir = new; 9877ec66d06SEric W. Biederman found: 9887ec66d06SEric W. Biederman subdir->header.nreg++; 9897ec66d06SEric W. Biederman failed: 990a1c83681SViresh Kumar if (IS_ERR(subdir)) { 99187ebdc00SAndrew Morton pr_err("sysctl could not get directory: "); 9926980128fSEric W. Biederman sysctl_print_dir(dir); 99387ebdc00SAndrew Morton pr_cont("/%*.*s %ld\n", 9947ec66d06SEric W. Biederman namelen, namelen, name, PTR_ERR(subdir)); 9951f87f0b5SEric W. Biederman } 9967ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 9977ec66d06SEric W. Biederman if (new) 9987ec66d06SEric W. Biederman drop_sysctl_table(&new->header); 9997ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 10007ec66d06SEric W. Biederman return subdir; 10011f87f0b5SEric W. Biederman } 10021f87f0b5SEric W. Biederman 10030e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) 10040e47c99dSEric W. Biederman { 10050e47c99dSEric W. Biederman struct ctl_dir *parent; 10060e47c99dSEric W. Biederman const char *procname; 10070e47c99dSEric W. Biederman if (!dir->header.parent) 10080e47c99dSEric W. Biederman return &set->dir; 10090e47c99dSEric W. Biederman parent = xlate_dir(set, dir->header.parent); 10100e47c99dSEric W. Biederman if (IS_ERR(parent)) 10110e47c99dSEric W. Biederman return parent; 10120e47c99dSEric W. Biederman procname = dir->header.ctl_table[0].procname; 10130e47c99dSEric W. Biederman return find_subdir(parent, procname, strlen(procname)); 10140e47c99dSEric W. Biederman } 10150e47c99dSEric W. Biederman 10160e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 101713bcc6a2SEric W. Biederman struct ctl_table **pentry) 10180e47c99dSEric W. Biederman { 10190e47c99dSEric W. Biederman struct ctl_table_header *head; 10200e47c99dSEric W. Biederman struct ctl_table_root *root; 10210e47c99dSEric W. Biederman struct ctl_table_set *set; 10220e47c99dSEric W. Biederman struct ctl_table *entry; 10230e47c99dSEric W. Biederman struct ctl_dir *dir; 10240e47c99dSEric W. Biederman int ret; 10250e47c99dSEric W. Biederman 10260e47c99dSEric W. Biederman ret = 0; 10270e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 10280e47c99dSEric W. Biederman root = (*pentry)->data; 102913bcc6a2SEric W. Biederman set = lookup_header_set(root); 10300e47c99dSEric W. Biederman dir = xlate_dir(set, (*phead)->parent); 10310e47c99dSEric W. Biederman if (IS_ERR(dir)) 10320e47c99dSEric W. Biederman ret = PTR_ERR(dir); 10330e47c99dSEric W. Biederman else { 10340e47c99dSEric W. Biederman const char *procname = (*pentry)->procname; 10350e47c99dSEric W. Biederman head = NULL; 10360e47c99dSEric W. Biederman entry = find_entry(&head, dir, procname, strlen(procname)); 10370e47c99dSEric W. Biederman ret = -ENOENT; 10380e47c99dSEric W. Biederman if (entry && use_table(head)) { 10390e47c99dSEric W. Biederman unuse_table(*phead); 10400e47c99dSEric W. Biederman *phead = head; 10410e47c99dSEric W. Biederman *pentry = entry; 10420e47c99dSEric W. Biederman ret = 0; 10430e47c99dSEric W. Biederman } 10440e47c99dSEric W. Biederman } 10450e47c99dSEric W. Biederman 10460e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 10470e47c99dSEric W. Biederman return ret; 10480e47c99dSEric W. Biederman } 10490e47c99dSEric W. Biederman 10507c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 10517c60c48fSEric W. Biederman { 10527c60c48fSEric W. Biederman struct va_format vaf; 10537c60c48fSEric W. Biederman va_list args; 10547c60c48fSEric W. Biederman 10557c60c48fSEric W. Biederman va_start(args, fmt); 10567c60c48fSEric W. Biederman vaf.fmt = fmt; 10577c60c48fSEric W. Biederman vaf.va = &args; 10587c60c48fSEric W. Biederman 105987ebdc00SAndrew Morton pr_err("sysctl table check failed: %s/%s %pV\n", 10607c60c48fSEric W. Biederman path, table->procname, &vaf); 10617c60c48fSEric W. Biederman 10627c60c48fSEric W. Biederman va_end(args); 10637c60c48fSEric W. Biederman return -EINVAL; 10647c60c48fSEric W. Biederman } 10657c60c48fSEric W. Biederman 10667c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 10677c60c48fSEric W. Biederman { 10687c60c48fSEric W. Biederman int err = 0; 10697c60c48fSEric W. Biederman for (; table->procname; table++) { 10707c60c48fSEric W. Biederman if (table->child) 10717c60c48fSEric W. Biederman err = sysctl_err(path, table, "Not a file"); 10727c60c48fSEric W. Biederman 10731f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 10741f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 10751f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 10761f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 10771f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 10781f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 10791f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 10801f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 10811f87f0b5SEric W. Biederman if (!table->data) 10827c60c48fSEric W. Biederman err = sysctl_err(path, table, "No data"); 10831f87f0b5SEric W. Biederman if (!table->maxlen) 10847c60c48fSEric W. Biederman err = sysctl_err(path, table, "No maxlen"); 10851f87f0b5SEric W. Biederman } 10861f87f0b5SEric W. Biederman if (!table->proc_handler) 10877c60c48fSEric W. Biederman err = sysctl_err(path, table, "No proc_handler"); 10887c60c48fSEric W. Biederman 10897c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 10907c60c48fSEric W. Biederman err = sysctl_err(path, table, "bogus .mode 0%o", 10917c60c48fSEric W. Biederman table->mode); 10921f87f0b5SEric W. Biederman } 10937c60c48fSEric W. Biederman return err; 10941f87f0b5SEric W. Biederman } 10951f87f0b5SEric W. Biederman 10960e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table, 10970e47c99dSEric W. Biederman struct ctl_table_root *link_root) 10980e47c99dSEric W. Biederman { 10990e47c99dSEric W. Biederman struct ctl_table *link_table, *entry, *link; 11000e47c99dSEric W. Biederman struct ctl_table_header *links; 1101ac13ac6fSEric W. Biederman struct ctl_node *node; 11020e47c99dSEric W. Biederman char *link_name; 11030e47c99dSEric W. Biederman int nr_entries, name_bytes; 11040e47c99dSEric W. Biederman 11050e47c99dSEric W. Biederman name_bytes = 0; 11060e47c99dSEric W. Biederman nr_entries = 0; 11070e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 11080e47c99dSEric W. Biederman nr_entries++; 11090e47c99dSEric W. Biederman name_bytes += strlen(entry->procname) + 1; 11100e47c99dSEric W. Biederman } 11110e47c99dSEric W. Biederman 11120e47c99dSEric W. Biederman links = kzalloc(sizeof(struct ctl_table_header) + 1113ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries + 11140e47c99dSEric W. Biederman sizeof(struct ctl_table)*(nr_entries + 1) + 11150e47c99dSEric W. Biederman name_bytes, 11160e47c99dSEric W. Biederman GFP_KERNEL); 11170e47c99dSEric W. Biederman 11180e47c99dSEric W. Biederman if (!links) 11190e47c99dSEric W. Biederman return NULL; 11200e47c99dSEric W. Biederman 1121ac13ac6fSEric W. Biederman node = (struct ctl_node *)(links + 1); 1122ac13ac6fSEric W. Biederman link_table = (struct ctl_table *)(node + nr_entries); 11230e47c99dSEric W. Biederman link_name = (char *)&link_table[nr_entries + 1]; 11240e47c99dSEric W. Biederman 11250e47c99dSEric W. Biederman for (link = link_table, entry = table; entry->procname; link++, entry++) { 11260e47c99dSEric W. Biederman int len = strlen(entry->procname) + 1; 11270e47c99dSEric W. Biederman memcpy(link_name, entry->procname, len); 11280e47c99dSEric W. Biederman link->procname = link_name; 11290e47c99dSEric W. Biederman link->mode = S_IFLNK|S_IRWXUGO; 11300e47c99dSEric W. Biederman link->data = link_root; 11310e47c99dSEric W. Biederman link_name += len; 11320e47c99dSEric W. Biederman } 1133ac13ac6fSEric W. Biederman init_header(links, dir->header.root, dir->header.set, node, link_table); 11340e47c99dSEric W. Biederman links->nreg = nr_entries; 11350e47c99dSEric W. Biederman 11360e47c99dSEric W. Biederman return links; 11370e47c99dSEric W. Biederman } 11380e47c99dSEric W. Biederman 11390e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir, 11400e47c99dSEric W. Biederman struct ctl_table *table, struct ctl_table_root *link_root) 11410e47c99dSEric W. Biederman { 11420e47c99dSEric W. Biederman struct ctl_table_header *head; 11430e47c99dSEric W. Biederman struct ctl_table *entry, *link; 11440e47c99dSEric W. Biederman 11450e47c99dSEric W. Biederman /* Are there links available for every entry in table? */ 11460e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 11470e47c99dSEric W. Biederman const char *procname = entry->procname; 11480e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 11490e47c99dSEric W. Biederman if (!link) 11500e47c99dSEric W. Biederman return false; 11510e47c99dSEric W. Biederman if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) 11520e47c99dSEric W. Biederman continue; 11530e47c99dSEric W. Biederman if (S_ISLNK(link->mode) && (link->data == link_root)) 11540e47c99dSEric W. Biederman continue; 11550e47c99dSEric W. Biederman return false; 11560e47c99dSEric W. Biederman } 11570e47c99dSEric W. Biederman 11580e47c99dSEric W. Biederman /* The checks passed. Increase the registration count on the links */ 11590e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 11600e47c99dSEric W. Biederman const char *procname = entry->procname; 11610e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 11620e47c99dSEric W. Biederman head->nreg++; 11630e47c99dSEric W. Biederman } 11640e47c99dSEric W. Biederman return true; 11650e47c99dSEric W. Biederman } 11660e47c99dSEric W. Biederman 11670e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head) 11680e47c99dSEric W. Biederman { 11690e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 11700e47c99dSEric W. Biederman struct ctl_dir *core_parent = NULL; 11710e47c99dSEric W. Biederman struct ctl_table_header *links; 11720e47c99dSEric W. Biederman int err; 11730e47c99dSEric W. Biederman 11740e47c99dSEric W. Biederman if (head->set == root_set) 11750e47c99dSEric W. Biederman return 0; 11760e47c99dSEric W. Biederman 11770e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, head->parent); 11780e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 11790e47c99dSEric W. Biederman return 0; 11800e47c99dSEric W. Biederman 11810e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) 11820e47c99dSEric W. Biederman return 0; 11830e47c99dSEric W. Biederman 11840e47c99dSEric W. Biederman core_parent->header.nreg++; 11850e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 11860e47c99dSEric W. Biederman 11870e47c99dSEric W. Biederman links = new_links(core_parent, head->ctl_table, head->root); 11880e47c99dSEric W. Biederman 11890e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 11900e47c99dSEric W. Biederman err = -ENOMEM; 11910e47c99dSEric W. Biederman if (!links) 11920e47c99dSEric W. Biederman goto out; 11930e47c99dSEric W. Biederman 11940e47c99dSEric W. Biederman err = 0; 11950e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) { 11960e47c99dSEric W. Biederman kfree(links); 11970e47c99dSEric W. Biederman goto out; 11980e47c99dSEric W. Biederman } 11990e47c99dSEric W. Biederman 12000e47c99dSEric W. Biederman err = insert_header(core_parent, links); 12010e47c99dSEric W. Biederman if (err) 12020e47c99dSEric W. Biederman kfree(links); 12030e47c99dSEric W. Biederman out: 12040e47c99dSEric W. Biederman drop_sysctl_table(&core_parent->header); 12050e47c99dSEric W. Biederman return err; 12060e47c99dSEric W. Biederman } 12070e47c99dSEric W. Biederman 12081f87f0b5SEric W. Biederman /** 1209f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 121060a47a2eSEric W. Biederman * @set: Sysctl tree to register on 12111f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 12121f87f0b5SEric W. Biederman * @table: the top-level table structure 12131f87f0b5SEric W. Biederman * 12141f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 12151f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 12161f87f0b5SEric W. Biederman * 12171f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 12181f87f0b5SEric W. Biederman * 12191f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 12201f87f0b5SEric W. Biederman * enter a sysctl file 12211f87f0b5SEric W. Biederman * 12221f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 12231f87f0b5SEric W. Biederman * 12241f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 12251f87f0b5SEric W. Biederman * 1226f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 12271f87f0b5SEric W. Biederman * 1228f728019bSEric W. Biederman * child - must be %NULL. 12291f87f0b5SEric W. Biederman * 12301f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 12311f87f0b5SEric W. Biederman * 12321f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 12331f87f0b5SEric W. Biederman * 12341f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 12351f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 12361f87f0b5SEric W. Biederman * 1237f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 1238f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 12391f87f0b5SEric W. Biederman * 12401f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 12411f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 12421f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 12431f87f0b5SEric W. Biederman * 12441f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 12451f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 12461f87f0b5SEric W. Biederman * 12471f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 12481f87f0b5SEric W. Biederman * to the table header on success. 12491f87f0b5SEric W. Biederman */ 12506e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 125160a47a2eSEric W. Biederman struct ctl_table_set *set, 12526e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 12531f87f0b5SEric W. Biederman { 125460a47a2eSEric W. Biederman struct ctl_table_root *root = set->dir.header.root; 12551f87f0b5SEric W. Biederman struct ctl_table_header *header; 12566e9d5164SEric W. Biederman const char *name, *nextname; 12577ec66d06SEric W. Biederman struct ctl_dir *dir; 1258ac13ac6fSEric W. Biederman struct ctl_table *entry; 1259ac13ac6fSEric W. Biederman struct ctl_node *node; 1260ac13ac6fSEric W. Biederman int nr_entries = 0; 12611f87f0b5SEric W. Biederman 1262ac13ac6fSEric W. Biederman for (entry = table; entry->procname; entry++) 1263ac13ac6fSEric W. Biederman nr_entries++; 1264ac13ac6fSEric W. Biederman 1265ac13ac6fSEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header) + 1266ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries, GFP_KERNEL); 12671f87f0b5SEric W. Biederman if (!header) 12681f87f0b5SEric W. Biederman return NULL; 12691f87f0b5SEric W. Biederman 1270ac13ac6fSEric W. Biederman node = (struct ctl_node *)(header + 1); 1271ac13ac6fSEric W. Biederman init_header(header, root, set, node, table); 12727c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 12737c60c48fSEric W. Biederman goto fail; 12748d6ecfccSEric W. Biederman 12751f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 12760e47c99dSEric W. Biederman dir = &set->dir; 127760f126d9SEric W. Biederman /* Reference moved down the diretory tree get_subdir */ 12787ec66d06SEric W. Biederman dir->header.nreg++; 12797ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 12807ec66d06SEric W. Biederman 12817ec66d06SEric W. Biederman /* Find the directory for the ctl_table */ 12827ec66d06SEric W. Biederman for (name = path; name; name = nextname) { 12837ec66d06SEric W. Biederman int namelen; 12847ec66d06SEric W. Biederman nextname = strchr(name, '/'); 12857ec66d06SEric W. Biederman if (nextname) { 12867ec66d06SEric W. Biederman namelen = nextname - name; 12877ec66d06SEric W. Biederman nextname++; 12887ec66d06SEric W. Biederman } else { 12897ec66d06SEric W. Biederman namelen = strlen(name); 12907ec66d06SEric W. Biederman } 12917ec66d06SEric W. Biederman if (namelen == 0) 12921f87f0b5SEric W. Biederman continue; 12937ec66d06SEric W. Biederman 12940e47c99dSEric W. Biederman dir = get_subdir(dir, name, namelen); 12957ec66d06SEric W. Biederman if (IS_ERR(dir)) 12967ec66d06SEric W. Biederman goto fail; 12971f87f0b5SEric W. Biederman } 12980e47c99dSEric W. Biederman 12997ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 13000e47c99dSEric W. Biederman if (insert_header(dir, header)) 13010e47c99dSEric W. Biederman goto fail_put_dir_locked; 13020e47c99dSEric W. Biederman 13037ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 13041f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 13051f87f0b5SEric W. Biederman 13061f87f0b5SEric W. Biederman return header; 13070e47c99dSEric W. Biederman 13087ec66d06SEric W. Biederman fail_put_dir_locked: 13097ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 13107c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 13117c60c48fSEric W. Biederman fail: 13127c60c48fSEric W. Biederman kfree(header); 13137c60c48fSEric W. Biederman dump_stack(); 13147c60c48fSEric W. Biederman return NULL; 13151f87f0b5SEric W. Biederman } 13161f87f0b5SEric W. Biederman 1317fea478d4SEric W. Biederman /** 1318fea478d4SEric W. Biederman * register_sysctl - register a sysctl table 1319fea478d4SEric W. Biederman * @path: The path to the directory the sysctl table is in. 1320fea478d4SEric W. Biederman * @table: the table structure 1321fea478d4SEric W. Biederman * 1322fea478d4SEric W. Biederman * Register a sysctl table. @table should be a filled in ctl_table 1323fea478d4SEric W. Biederman * array. A completely 0 filled entry terminates the table. 1324fea478d4SEric W. Biederman * 1325fea478d4SEric W. Biederman * See __register_sysctl_table for more details. 1326fea478d4SEric W. Biederman */ 1327fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table) 1328fea478d4SEric W. Biederman { 1329fea478d4SEric W. Biederman return __register_sysctl_table(&sysctl_table_root.default_set, 1330fea478d4SEric W. Biederman path, table); 1331fea478d4SEric W. Biederman } 1332fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl); 1333fea478d4SEric W. Biederman 13346e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 13356e9d5164SEric W. Biederman { 13366e9d5164SEric W. Biederman int namelen; 13376e9d5164SEric W. Biederman namelen = strlen(name); 13386e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 13396e9d5164SEric W. Biederman return NULL; 13406e9d5164SEric W. Biederman memcpy(pos, name, namelen); 13416e9d5164SEric W. Biederman pos[namelen] = '/'; 13426e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 13436e9d5164SEric W. Biederman pos += namelen + 1; 13446e9d5164SEric W. Biederman return pos; 13456e9d5164SEric W. Biederman } 13466e9d5164SEric W. Biederman 1347f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1348f728019bSEric W. Biederman { 1349f728019bSEric W. Biederman int has_files = 0; 1350f728019bSEric W. Biederman int nr_subheaders = 0; 1351f728019bSEric W. Biederman struct ctl_table *entry; 1352f728019bSEric W. Biederman 1353f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1354f728019bSEric W. Biederman if (!table || !table->procname) 1355f728019bSEric W. Biederman return 1; 1356f728019bSEric W. Biederman 1357f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1358f728019bSEric W. Biederman if (entry->child) 1359f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1360f728019bSEric W. Biederman else 1361f728019bSEric W. Biederman has_files = 1; 1362f728019bSEric W. Biederman } 1363f728019bSEric W. Biederman return nr_subheaders + has_files; 1364f728019bSEric W. Biederman } 1365f728019bSEric W. Biederman 1366f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 136760a47a2eSEric W. Biederman struct ctl_table_header ***subheader, struct ctl_table_set *set, 1368f728019bSEric W. Biederman struct ctl_table *table) 1369f728019bSEric W. Biederman { 1370f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1371f728019bSEric W. Biederman struct ctl_table *entry, *files; 1372f728019bSEric W. Biederman int nr_files = 0; 1373f728019bSEric W. Biederman int nr_dirs = 0; 1374f728019bSEric W. Biederman int err = -ENOMEM; 1375f728019bSEric W. Biederman 1376f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1377f728019bSEric W. Biederman if (entry->child) 1378f728019bSEric W. Biederman nr_dirs++; 1379f728019bSEric W. Biederman else 1380f728019bSEric W. Biederman nr_files++; 1381f728019bSEric W. Biederman } 1382f728019bSEric W. Biederman 1383f728019bSEric W. Biederman files = table; 1384f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1385f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1386f728019bSEric W. Biederman struct ctl_table *new; 1387f728019bSEric W. Biederman files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), 1388f728019bSEric W. Biederman GFP_KERNEL); 1389f728019bSEric W. Biederman if (!files) 1390f728019bSEric W. Biederman goto out; 1391f728019bSEric W. Biederman 1392f728019bSEric W. Biederman ctl_table_arg = files; 1393f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1394f728019bSEric W. Biederman if (entry->child) 1395f728019bSEric W. Biederman continue; 1396f728019bSEric W. Biederman *new = *entry; 1397f728019bSEric W. Biederman new++; 1398f728019bSEric W. Biederman } 1399f728019bSEric W. Biederman } 1400f728019bSEric W. Biederman 1401f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1402f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1403f728019bSEric W. Biederman struct ctl_table_header *header; 140460a47a2eSEric W. Biederman header = __register_sysctl_table(set, path, files); 1405f728019bSEric W. Biederman if (!header) { 1406f728019bSEric W. Biederman kfree(ctl_table_arg); 1407f728019bSEric W. Biederman goto out; 1408f728019bSEric W. Biederman } 1409f728019bSEric W. Biederman 1410f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1411f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1412f728019bSEric W. Biederman **subheader = header; 1413f728019bSEric W. Biederman (*subheader)++; 1414f728019bSEric W. Biederman } 1415f728019bSEric W. Biederman 1416f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1417f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1418f728019bSEric W. Biederman char *child_pos; 1419f728019bSEric W. Biederman 1420f728019bSEric W. Biederman if (!entry->child) 1421f728019bSEric W. Biederman continue; 1422f728019bSEric W. Biederman 1423f728019bSEric W. Biederman err = -ENAMETOOLONG; 1424f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1425f728019bSEric W. Biederman if (!child_pos) 1426f728019bSEric W. Biederman goto out; 1427f728019bSEric W. Biederman 1428f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 142960a47a2eSEric W. Biederman set, entry->child); 1430f728019bSEric W. Biederman pos[0] = '\0'; 1431f728019bSEric W. Biederman if (err) 1432f728019bSEric W. Biederman goto out; 1433f728019bSEric W. Biederman } 1434f728019bSEric W. Biederman err = 0; 1435f728019bSEric W. Biederman out: 1436f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1437f728019bSEric W. Biederman return err; 1438f728019bSEric W. Biederman } 1439f728019bSEric W. Biederman 14406e9d5164SEric W. Biederman /** 14416e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 144260a47a2eSEric W. Biederman * @set: Sysctl tree to register on 14436e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 14446e9d5164SEric W. Biederman * @table: the top-level table structure 14456e9d5164SEric W. Biederman * 14466e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 14476e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 14486e9d5164SEric W. Biederman * 14496e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 14506e9d5164SEric W. Biederman */ 14516e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 145260a47a2eSEric W. Biederman struct ctl_table_set *set, 14536e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 14546e9d5164SEric W. Biederman { 1455ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1456f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1457f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 14586e9d5164SEric W. Biederman const struct ctl_path *component; 14596e9d5164SEric W. Biederman char *new_path, *pos; 14606e9d5164SEric W. Biederman 14616e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 14626e9d5164SEric W. Biederman if (!new_path) 14636e9d5164SEric W. Biederman return NULL; 14646e9d5164SEric W. Biederman 14656e9d5164SEric W. Biederman pos[0] = '\0'; 14666e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 14676e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 14686e9d5164SEric W. Biederman if (!pos) 14696e9d5164SEric W. Biederman goto out; 14706e9d5164SEric W. Biederman } 1471ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1472ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1473ec6a5266SEric W. Biederman if (!pos) 1474ec6a5266SEric W. Biederman goto out; 1475ec6a5266SEric W. Biederman table = table->child; 1476ec6a5266SEric W. Biederman } 1477f728019bSEric W. Biederman if (nr_subheaders == 1) { 147860a47a2eSEric W. Biederman header = __register_sysctl_table(set, new_path, table); 1479ec6a5266SEric W. Biederman if (header) 1480ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1481f728019bSEric W. Biederman } else { 1482f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1483f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1484f728019bSEric W. Biederman if (!header) 1485f728019bSEric W. Biederman goto out; 1486f728019bSEric W. Biederman 1487f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1488f728019bSEric W. Biederman subheader = subheaders; 1489f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1490f728019bSEric W. Biederman 1491f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 149260a47a2eSEric W. Biederman set, table)) 1493f728019bSEric W. Biederman goto err_register_leaves; 1494f728019bSEric W. Biederman } 1495f728019bSEric W. Biederman 14966e9d5164SEric W. Biederman out: 14976e9d5164SEric W. Biederman kfree(new_path); 14986e9d5164SEric W. Biederman return header; 1499f728019bSEric W. Biederman 1500f728019bSEric W. Biederman err_register_leaves: 1501f728019bSEric W. Biederman while (subheader > subheaders) { 1502f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1503f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1504f728019bSEric W. Biederman unregister_sysctl_table(subh); 1505f728019bSEric W. Biederman kfree(table); 1506f728019bSEric W. Biederman } 1507f728019bSEric W. Biederman kfree(header); 1508f728019bSEric W. Biederman header = NULL; 1509f728019bSEric W. Biederman goto out; 15106e9d5164SEric W. Biederman } 15116e9d5164SEric W. Biederman 15121f87f0b5SEric W. Biederman /** 15131f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 15141f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 15151f87f0b5SEric W. Biederman * @table: the top-level table structure 15161f87f0b5SEric W. Biederman * 15171f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 15181f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 15191f87f0b5SEric W. Biederman * 15201f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 15211f87f0b5SEric W. Biederman */ 15221f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 15231f87f0b5SEric W. Biederman struct ctl_table *table) 15241f87f0b5SEric W. Biederman { 152560a47a2eSEric W. Biederman return __register_sysctl_paths(&sysctl_table_root.default_set, 15261f87f0b5SEric W. Biederman path, table); 15271f87f0b5SEric W. Biederman } 15281f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 15291f87f0b5SEric W. Biederman 15301f87f0b5SEric W. Biederman /** 15311f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 15321f87f0b5SEric W. Biederman * @table: the top-level table structure 15331f87f0b5SEric W. Biederman * 15341f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 15351f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 15361f87f0b5SEric W. Biederman * 15371f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 15381f87f0b5SEric W. Biederman */ 15391f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 15401f87f0b5SEric W. Biederman { 15411f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 15421f87f0b5SEric W. Biederman 15431f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 15441f87f0b5SEric W. Biederman } 15451f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 15461f87f0b5SEric W. Biederman 15470e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header) 15480e47c99dSEric W. Biederman { 15490e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 15500e47c99dSEric W. Biederman struct ctl_table_root *root = header->root; 15510e47c99dSEric W. Biederman struct ctl_dir *parent = header->parent; 15520e47c99dSEric W. Biederman struct ctl_dir *core_parent; 15530e47c99dSEric W. Biederman struct ctl_table *entry; 15540e47c99dSEric W. Biederman 15550e47c99dSEric W. Biederman if (header->set == root_set) 15560e47c99dSEric W. Biederman return; 15570e47c99dSEric W. Biederman 15580e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, parent); 15590e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 15600e47c99dSEric W. Biederman return; 15610e47c99dSEric W. Biederman 15620e47c99dSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 15630e47c99dSEric W. Biederman struct ctl_table_header *link_head; 15640e47c99dSEric W. Biederman struct ctl_table *link; 15650e47c99dSEric W. Biederman const char *name = entry->procname; 15660e47c99dSEric W. Biederman 15670e47c99dSEric W. Biederman link = find_entry(&link_head, core_parent, name, strlen(name)); 15680e47c99dSEric W. Biederman if (link && 15690e47c99dSEric W. Biederman ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || 15700e47c99dSEric W. Biederman (S_ISLNK(link->mode) && (link->data == root)))) { 15710e47c99dSEric W. Biederman drop_sysctl_table(link_head); 15720e47c99dSEric W. Biederman } 15730e47c99dSEric W. Biederman else { 157487ebdc00SAndrew Morton pr_err("sysctl link missing during unregister: "); 15750e47c99dSEric W. Biederman sysctl_print_dir(parent); 157687ebdc00SAndrew Morton pr_cont("/%s\n", name); 15770e47c99dSEric W. Biederman } 15780e47c99dSEric W. Biederman } 15790e47c99dSEric W. Biederman } 15800e47c99dSEric W. Biederman 1581938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1582938aaa4fSEric W. Biederman { 15837ec66d06SEric W. Biederman struct ctl_dir *parent = header->parent; 15847ec66d06SEric W. Biederman 1585938aaa4fSEric W. Biederman if (--header->nreg) 1586938aaa4fSEric W. Biederman return; 1587938aaa4fSEric W. Biederman 15880e47c99dSEric W. Biederman put_links(header); 1589938aaa4fSEric W. Biederman start_unregistering(header); 1590938aaa4fSEric W. Biederman if (!--header->count) 1591938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 15927ec66d06SEric W. Biederman 15937ec66d06SEric W. Biederman if (parent) 15947ec66d06SEric W. Biederman drop_sysctl_table(&parent->header); 1595938aaa4fSEric W. Biederman } 1596938aaa4fSEric W. Biederman 15971f87f0b5SEric W. Biederman /** 15981f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 15991f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 16001f87f0b5SEric W. Biederman * 16011f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 16021f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 16031f87f0b5SEric W. Biederman */ 16041f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 16051f87f0b5SEric W. Biederman { 1606f728019bSEric W. Biederman int nr_subheaders; 16071f87f0b5SEric W. Biederman might_sleep(); 16081f87f0b5SEric W. Biederman 16091f87f0b5SEric W. Biederman if (header == NULL) 16101f87f0b5SEric W. Biederman return; 16111f87f0b5SEric W. Biederman 1612f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1613f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1614f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1615f728019bSEric W. Biederman int i; 1616f728019bSEric W. Biederman 1617f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1618f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1619f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1620f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1621f728019bSEric W. Biederman unregister_sysctl_table(subh); 1622f728019bSEric W. Biederman kfree(table); 1623f728019bSEric W. Biederman } 1624f728019bSEric W. Biederman kfree(header); 1625f728019bSEric W. Biederman return; 1626f728019bSEric W. Biederman } 1627f728019bSEric W. Biederman 16281f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1629938aaa4fSEric W. Biederman drop_sysctl_table(header); 16301f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 16311f87f0b5SEric W. Biederman } 16321f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 16331f87f0b5SEric W. Biederman 16340e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set, 16359eb47c26SEric W. Biederman struct ctl_table_root *root, 16361f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 16371f87f0b5SEric W. Biederman { 16381347440dSDan Carpenter memset(set, 0, sizeof(*set)); 16390e47c99dSEric W. Biederman set->is_seen = is_seen; 1640ac13ac6fSEric W. Biederman init_header(&set->dir.header, root, set, NULL, root_table); 16411f87f0b5SEric W. Biederman } 16421f87f0b5SEric W. Biederman 164397324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 164497324cd8SEric W. Biederman { 1645ac13ac6fSEric W. Biederman WARN_ON(!RB_EMPTY_ROOT(&set->dir.root)); 164697324cd8SEric W. Biederman } 16471f87f0b5SEric W. Biederman 16481e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 164977b14db5SEric W. Biederman { 1650e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1651e1675231SAlexey Dobriyan 165277b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 16539043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 16549043476fSAl Viro proc_sys_root->proc_fops = &proc_sys_dir_file_operations; 165577b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1656de4e83bdSEric W. Biederman 1657de4e83bdSEric W. Biederman return sysctl_init(); 165877b14db5SEric W. Biederman } 1659