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> 877b14db5SEric W. Biederman #include <linux/security.h> 940401530SAl Viro #include <linux/sched.h> 1034286d66SNick Piggin #include <linux/namei.h> 1140401530SAl Viro #include <linux/mm.h> 1277b14db5SEric W. Biederman #include "internal.h" 1377b14db5SEric W. Biederman 14d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations; 1577b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations; 1603a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations; 179043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations; 189043476fSAl Viro static const struct inode_operations proc_sys_dir_operations; 1977b14db5SEric W. Biederman 20f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll) 21f1ecf068SLucas De Marchi { 22f1ecf068SLucas De Marchi if (!poll) 23f1ecf068SLucas De Marchi return; 24f1ecf068SLucas De Marchi 25f1ecf068SLucas De Marchi atomic_inc(&poll->event); 26f1ecf068SLucas De Marchi wake_up_interruptible(&poll->wait); 27f1ecf068SLucas De Marchi } 28f1ecf068SLucas De Marchi 299043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 309043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 3177b14db5SEric W. Biederman { 3277b14db5SEric W. Biederman struct inode *inode; 339043476fSAl Viro struct proc_inode *ei; 3477b14db5SEric W. Biederman 359043476fSAl Viro inode = new_inode(sb); 3677b14db5SEric W. Biederman if (!inode) 3777b14db5SEric W. Biederman goto out; 3877b14db5SEric W. Biederman 3985fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 4085fe4025SChristoph Hellwig 419043476fSAl Viro sysctl_head_get(head); 4277b14db5SEric W. Biederman ei = PROC_I(inode); 439043476fSAl Viro ei->sysctl = head; 449043476fSAl Viro ei->sysctl_entry = table; 459043476fSAl Viro 4677b14db5SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 479043476fSAl Viro inode->i_mode = table->mode; 489043476fSAl Viro if (!table->child) { 499043476fSAl Viro inode->i_mode |= S_IFREG; 5077b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 5177b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 529043476fSAl Viro } else { 539043476fSAl Viro inode->i_mode |= S_IFDIR; 546d6b77f1SMiklos Szeredi clear_nlink(inode); 559043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 569043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 579043476fSAl Viro } 5877b14db5SEric W. Biederman out: 5977b14db5SEric W. Biederman return inode; 6077b14db5SEric W. Biederman } 6177b14db5SEric W. Biederman 629043476fSAl Viro static struct ctl_table *find_in_table(struct ctl_table *p, struct qstr *name) 6377b14db5SEric W. Biederman { 6477b14db5SEric W. Biederman int len; 652315ffa0SEric W. Biederman for ( ; p->procname; p++) { 6677b14db5SEric W. Biederman 679043476fSAl Viro if (!p->procname) 6877b14db5SEric W. Biederman continue; 6977b14db5SEric W. Biederman 709043476fSAl Viro len = strlen(p->procname); 7177b14db5SEric W. Biederman if (len != name->len) 7277b14db5SEric W. Biederman continue; 7377b14db5SEric W. Biederman 749043476fSAl Viro if (memcmp(p->procname, name->name, len) != 0) 7577b14db5SEric W. Biederman continue; 7677b14db5SEric W. Biederman 7777b14db5SEric W. Biederman /* I have a match */ 789043476fSAl Viro return p; 7977b14db5SEric W. Biederman } 8077b14db5SEric W. Biederman return NULL; 8177b14db5SEric W. Biederman } 8277b14db5SEric W. Biederman 8381324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 8477b14db5SEric W. Biederman { 859043476fSAl Viro if (PROC_I(inode)->sysctl) 869043476fSAl Viro return sysctl_head_grab(PROC_I(inode)->sysctl); 879043476fSAl Viro else 889043476fSAl Viro return sysctl_head_next(NULL); 8977b14db5SEric W. Biederman } 9077b14db5SEric W. Biederman 9177b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 9277b14db5SEric W. Biederman struct nameidata *nd) 9377b14db5SEric W. Biederman { 949043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 959043476fSAl Viro struct ctl_table *table = PROC_I(dir)->sysctl_entry; 969043476fSAl Viro struct ctl_table_header *h = NULL; 979043476fSAl Viro struct qstr *name = &dentry->d_name; 989043476fSAl Viro struct ctl_table *p; 9977b14db5SEric W. Biederman struct inode *inode; 1009043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 10177b14db5SEric W. Biederman 1029043476fSAl Viro if (IS_ERR(head)) 1039043476fSAl Viro return ERR_CAST(head); 1049043476fSAl Viro 1059043476fSAl Viro if (table && !table->child) { 1069043476fSAl Viro WARN_ON(1); 1079043476fSAl Viro goto out; 1089043476fSAl Viro } 1099043476fSAl Viro 1109043476fSAl Viro table = table ? table->child : head->ctl_table; 1119043476fSAl Viro 1129043476fSAl Viro p = find_in_table(table, name); 1139043476fSAl Viro if (!p) { 1149043476fSAl Viro for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) { 1159043476fSAl Viro if (h->attached_to != table) 1169043476fSAl Viro continue; 1179043476fSAl Viro p = find_in_table(h->attached_by, name); 1189043476fSAl Viro if (p) 1199043476fSAl Viro break; 1209043476fSAl Viro } 1219043476fSAl Viro } 1229043476fSAl Viro 1239043476fSAl Viro if (!p) 12477b14db5SEric W. Biederman goto out; 12577b14db5SEric W. Biederman 12677b14db5SEric W. Biederman err = ERR_PTR(-ENOMEM); 1279043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 1289043476fSAl Viro if (h) 1299043476fSAl Viro sysctl_head_finish(h); 1309043476fSAl Viro 13177b14db5SEric W. Biederman if (!inode) 13277b14db5SEric W. Biederman goto out; 13377b14db5SEric W. Biederman 13477b14db5SEric W. Biederman err = NULL; 135fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 13677b14db5SEric W. Biederman d_add(dentry, inode); 13777b14db5SEric W. Biederman 13877b14db5SEric W. Biederman out: 13977b14db5SEric W. Biederman sysctl_head_finish(head); 14077b14db5SEric W. Biederman return err; 14177b14db5SEric W. Biederman } 14277b14db5SEric W. Biederman 1437708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 1447708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 14577b14db5SEric W. Biederman { 1469043476fSAl Viro struct inode *inode = filp->f_path.dentry->d_inode; 1479043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 1489043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 1492a2da53bSDavid Howells ssize_t error; 1502a2da53bSDavid Howells size_t res; 15177b14db5SEric W. Biederman 1529043476fSAl Viro if (IS_ERR(head)) 1539043476fSAl Viro return PTR_ERR(head); 15477b14db5SEric W. Biederman 15577b14db5SEric W. Biederman /* 15677b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 15777b14db5SEric W. Biederman * and won't be until we finish. 15877b14db5SEric W. Biederman */ 15977b14db5SEric W. Biederman error = -EPERM; 160d7321cd6SPavel Emelyanov if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) 16177b14db5SEric W. Biederman goto out; 16277b14db5SEric W. Biederman 1639043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 1649043476fSAl Viro error = -EINVAL; 1659043476fSAl Viro if (!table->proc_handler) 1669043476fSAl Viro goto out; 1679043476fSAl Viro 16877b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 16977b14db5SEric W. Biederman res = count; 1708d65af78SAlexey Dobriyan error = table->proc_handler(table, write, buf, &res, ppos); 17177b14db5SEric W. Biederman if (!error) 17277b14db5SEric W. Biederman error = res; 17377b14db5SEric W. Biederman out: 17477b14db5SEric W. Biederman sysctl_head_finish(head); 17577b14db5SEric W. Biederman 17677b14db5SEric W. Biederman return error; 17777b14db5SEric W. Biederman } 17877b14db5SEric W. Biederman 1797708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 1807708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 1817708bfb1SPavel Emelyanov { 1827708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 1837708bfb1SPavel Emelyanov } 1847708bfb1SPavel Emelyanov 18577b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 18677b14db5SEric W. Biederman size_t count, loff_t *ppos) 18777b14db5SEric W. Biederman { 1887708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 18977b14db5SEric W. Biederman } 19077b14db5SEric W. Biederman 191f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 192f1ecf068SLucas De Marchi { 193f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 194f1ecf068SLucas De Marchi 195f1ecf068SLucas De Marchi if (table->poll) 196f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 197f1ecf068SLucas De Marchi 198f1ecf068SLucas De Marchi return 0; 199f1ecf068SLucas De Marchi } 200f1ecf068SLucas De Marchi 201f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) 202f1ecf068SLucas De Marchi { 203f1ecf068SLucas De Marchi struct inode *inode = filp->f_path.dentry->d_inode; 204f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 205f1ecf068SLucas De Marchi unsigned long event = (unsigned long)filp->private_data; 206f1ecf068SLucas De Marchi unsigned int ret = DEFAULT_POLLMASK; 207f1ecf068SLucas De Marchi 208f1ecf068SLucas De Marchi if (!table->proc_handler) 209f1ecf068SLucas De Marchi goto out; 210f1ecf068SLucas De Marchi 211f1ecf068SLucas De Marchi if (!table->poll) 212f1ecf068SLucas De Marchi goto out; 213f1ecf068SLucas De Marchi 214f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 215f1ecf068SLucas De Marchi 216f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 217f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 218f1ecf068SLucas De Marchi ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; 219f1ecf068SLucas De Marchi } 220f1ecf068SLucas De Marchi 221f1ecf068SLucas De Marchi out: 222f1ecf068SLucas De Marchi return ret; 223f1ecf068SLucas De Marchi } 22477b14db5SEric W. Biederman 22577b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent, 2269043476fSAl Viro filldir_t filldir, 2279043476fSAl Viro struct ctl_table_header *head, 2289043476fSAl Viro struct ctl_table *table) 22977b14db5SEric W. Biederman { 23077b14db5SEric W. Biederman struct dentry *child, *dir = filp->f_path.dentry; 23177b14db5SEric W. Biederman struct inode *inode; 23277b14db5SEric W. Biederman struct qstr qname; 23377b14db5SEric W. Biederman ino_t ino = 0; 23477b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 23577b14db5SEric W. Biederman 23677b14db5SEric W. Biederman qname.name = table->procname; 23777b14db5SEric W. Biederman qname.len = strlen(table->procname); 23877b14db5SEric W. Biederman qname.hash = full_name_hash(qname.name, qname.len); 23977b14db5SEric W. Biederman 24077b14db5SEric W. Biederman child = d_lookup(dir, &qname); 24177b14db5SEric W. Biederman if (!child) { 2429043476fSAl Viro child = d_alloc(dir, &qname); 2439043476fSAl Viro if (child) { 2449043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 2459043476fSAl Viro if (!inode) { 2469043476fSAl Viro dput(child); 2479043476fSAl Viro return -ENOMEM; 2489043476fSAl Viro } else { 249fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 2509043476fSAl Viro d_add(child, inode); 25177b14db5SEric W. Biederman } 2529043476fSAl Viro } else { 2539043476fSAl Viro return -ENOMEM; 25477b14db5SEric W. Biederman } 25577b14db5SEric W. Biederman } 25677b14db5SEric W. Biederman inode = child->d_inode; 25777b14db5SEric W. Biederman ino = inode->i_ino; 25877b14db5SEric W. Biederman type = inode->i_mode >> 12; 25977b14db5SEric W. Biederman dput(child); 2609043476fSAl Viro return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); 2619043476fSAl Viro } 2629043476fSAl Viro 2639043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table, 2649043476fSAl Viro unsigned long *pos, struct file *file, 2659043476fSAl Viro void *dirent, filldir_t filldir) 2669043476fSAl Viro { 2679043476fSAl Viro 2682315ffa0SEric W. Biederman for (; table->procname; table++, (*pos)++) { 2699043476fSAl Viro int res; 2709043476fSAl Viro 2719043476fSAl Viro /* Can't do anything without a proc name */ 2729043476fSAl Viro if (!table->procname) 2739043476fSAl Viro continue; 2749043476fSAl Viro 2759043476fSAl Viro if (*pos < file->f_pos) 2769043476fSAl Viro continue; 2779043476fSAl Viro 2789043476fSAl Viro res = proc_sys_fill_cache(file, dirent, filldir, head, table); 2799043476fSAl Viro if (res) 2809043476fSAl Viro return res; 2819043476fSAl Viro 2829043476fSAl Viro file->f_pos = *pos + 1; 2839043476fSAl Viro } 2849043476fSAl Viro return 0; 28577b14db5SEric W. Biederman } 28677b14db5SEric W. Biederman 28777b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) 28877b14db5SEric W. Biederman { 2899043476fSAl Viro struct dentry *dentry = filp->f_path.dentry; 29077b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 2919043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 2929043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 2939043476fSAl Viro struct ctl_table_header *h = NULL; 29477b14db5SEric W. Biederman unsigned long pos; 2959043476fSAl Viro int ret = -EINVAL; 29677b14db5SEric W. Biederman 2979043476fSAl Viro if (IS_ERR(head)) 2989043476fSAl Viro return PTR_ERR(head); 2999043476fSAl Viro 3009043476fSAl Viro if (table && !table->child) { 3019043476fSAl Viro WARN_ON(1); 30277b14db5SEric W. Biederman goto out; 3039043476fSAl Viro } 3049043476fSAl Viro 3059043476fSAl Viro table = table ? table->child : head->ctl_table; 30677b14db5SEric W. Biederman 30777b14db5SEric W. Biederman ret = 0; 30877b14db5SEric W. Biederman /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ 30977b14db5SEric W. Biederman if (filp->f_pos == 0) { 31077b14db5SEric W. Biederman if (filldir(dirent, ".", 1, filp->f_pos, 31177b14db5SEric W. Biederman inode->i_ino, DT_DIR) < 0) 31277b14db5SEric W. Biederman goto out; 31377b14db5SEric W. Biederman filp->f_pos++; 31477b14db5SEric W. Biederman } 31577b14db5SEric W. Biederman if (filp->f_pos == 1) { 31677b14db5SEric W. Biederman if (filldir(dirent, "..", 2, filp->f_pos, 31777b14db5SEric W. Biederman parent_ino(dentry), DT_DIR) < 0) 31877b14db5SEric W. Biederman goto out; 31977b14db5SEric W. Biederman filp->f_pos++; 32077b14db5SEric W. Biederman } 32177b14db5SEric W. Biederman pos = 2; 32277b14db5SEric W. Biederman 3239043476fSAl Viro ret = scan(head, table, &pos, filp, dirent, filldir); 3249043476fSAl Viro if (ret) 32577b14db5SEric W. Biederman goto out; 3269043476fSAl Viro 3279043476fSAl Viro for (h = sysctl_head_next(NULL); h; h = sysctl_head_next(h)) { 3289043476fSAl Viro if (h->attached_to != table) 3299043476fSAl Viro continue; 3309043476fSAl Viro ret = scan(h, h->attached_by, &pos, filp, dirent, filldir); 3319043476fSAl Viro if (ret) { 3329043476fSAl Viro sysctl_head_finish(h); 3339043476fSAl Viro break; 33477b14db5SEric W. Biederman } 33577b14db5SEric W. Biederman } 33677b14db5SEric W. Biederman ret = 1; 33777b14db5SEric W. Biederman out: 33877b14db5SEric W. Biederman sysctl_head_finish(head); 33977b14db5SEric W. Biederman return ret; 34077b14db5SEric W. Biederman } 34177b14db5SEric W. Biederman 34210556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 34377b14db5SEric W. Biederman { 34477b14db5SEric W. Biederman /* 34577b14db5SEric W. Biederman * sysctl entries that are not writeable, 34677b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 34777b14db5SEric W. Biederman */ 348f696a365SMiklos Szeredi struct ctl_table_header *head; 349f696a365SMiklos Szeredi struct ctl_table *table; 35077b14db5SEric W. Biederman int error; 35177b14db5SEric W. Biederman 352f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 353f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 354f696a365SMiklos Szeredi return -EACCES; 355f696a365SMiklos Szeredi 356f696a365SMiklos Szeredi head = grab_header(inode); 3579043476fSAl Viro if (IS_ERR(head)) 3589043476fSAl Viro return PTR_ERR(head); 35977b14db5SEric W. Biederman 360f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 3619043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 3629043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 3639043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 3641fc0f78cSAl Viro error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); 3659043476fSAl Viro 36677b14db5SEric W. Biederman sysctl_head_finish(head); 36777b14db5SEric W. Biederman return error; 36877b14db5SEric W. Biederman } 36977b14db5SEric W. Biederman 37077b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 37177b14db5SEric W. Biederman { 37277b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 37377b14db5SEric W. Biederman int error; 37477b14db5SEric W. Biederman 37577b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 37677b14db5SEric W. Biederman return -EPERM; 37777b14db5SEric W. Biederman 37877b14db5SEric W. Biederman error = inode_change_ok(inode, attr); 3791025774cSChristoph Hellwig if (error) 38077b14db5SEric W. Biederman return error; 3811025774cSChristoph Hellwig 3821025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 3831025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 3841025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 3851025774cSChristoph Hellwig if (error) 3861025774cSChristoph Hellwig return error; 3871025774cSChristoph Hellwig } 3881025774cSChristoph Hellwig 3891025774cSChristoph Hellwig setattr_copy(inode, attr); 3901025774cSChristoph Hellwig mark_inode_dirty(inode); 3911025774cSChristoph Hellwig return 0; 39277b14db5SEric W. Biederman } 39377b14db5SEric W. Biederman 3949043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 3959043476fSAl Viro { 3969043476fSAl Viro struct inode *inode = dentry->d_inode; 3979043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 3989043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 3999043476fSAl Viro 4009043476fSAl Viro if (IS_ERR(head)) 4019043476fSAl Viro return PTR_ERR(head); 4029043476fSAl Viro 4039043476fSAl Viro generic_fillattr(inode, stat); 4049043476fSAl Viro if (table) 4059043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 4069043476fSAl Viro 4079043476fSAl Viro sysctl_head_finish(head); 4089043476fSAl Viro return 0; 4099043476fSAl Viro } 4109043476fSAl Viro 41177b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 412f1ecf068SLucas De Marchi .open = proc_sys_open, 413f1ecf068SLucas De Marchi .poll = proc_sys_poll, 41477b14db5SEric W. Biederman .read = proc_sys_read, 41577b14db5SEric W. Biederman .write = proc_sys_write, 4166038f373SArnd Bergmann .llseek = default_llseek, 4179043476fSAl Viro }; 4189043476fSAl Viro 4199043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 420887df078SPavel Emelyanov .read = generic_read_dir, 42177b14db5SEric W. Biederman .readdir = proc_sys_readdir, 4223222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 42377b14db5SEric W. Biederman }; 42477b14db5SEric W. Biederman 42503a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 4269043476fSAl Viro .permission = proc_sys_permission, 4279043476fSAl Viro .setattr = proc_sys_setattr, 4289043476fSAl Viro .getattr = proc_sys_getattr, 4299043476fSAl Viro }; 4309043476fSAl Viro 4319043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 43277b14db5SEric W. Biederman .lookup = proc_sys_lookup, 43377b14db5SEric W. Biederman .permission = proc_sys_permission, 43477b14db5SEric W. Biederman .setattr = proc_sys_setattr, 4359043476fSAl Viro .getattr = proc_sys_getattr, 43677b14db5SEric W. Biederman }; 43777b14db5SEric W. Biederman 43877b14db5SEric W. Biederman static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) 43977b14db5SEric W. Biederman { 44034286d66SNick Piggin if (nd->flags & LOOKUP_RCU) 44134286d66SNick Piggin return -ECHILD; 4429043476fSAl Viro return !PROC_I(dentry->d_inode)->sysctl->unregistering; 4439043476fSAl Viro } 4449043476fSAl Viro 445fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 4469043476fSAl Viro { 4479043476fSAl Viro return !!PROC_I(dentry->d_inode)->sysctl->unregistering; 4489043476fSAl Viro } 4499043476fSAl Viro 450621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent, 451621e155aSNick Piggin const struct inode *pinode, 452621e155aSNick Piggin const struct dentry *dentry, const struct inode *inode, 453621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 4549043476fSAl Viro { 455dfef6dcdSAl Viro struct ctl_table_header *head; 45631e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 45731e6b01fSNick Piggin * that inode here can be NULL */ 458dfef6dcdSAl Viro /* AV: can it, indeed? */ 45931e6b01fSNick Piggin if (!inode) 460dfef6dcdSAl Viro return 1; 461621e155aSNick Piggin if (name->len != len) 4629043476fSAl Viro return 1; 463621e155aSNick Piggin if (memcmp(name->name, str, len)) 4649043476fSAl Viro return 1; 465dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 466dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 46777b14db5SEric W. Biederman } 46877b14db5SEric W. Biederman 469d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 47077b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 4719043476fSAl Viro .d_delete = proc_sys_delete, 4729043476fSAl Viro .d_compare = proc_sys_compare, 47377b14db5SEric W. Biederman }; 47477b14db5SEric W. Biederman 4751e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 47677b14db5SEric W. Biederman { 477e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 478e1675231SAlexey Dobriyan 47977b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 4809043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 4819043476fSAl Viro proc_sys_root->proc_fops = &proc_sys_dir_file_operations; 48277b14db5SEric W. Biederman proc_sys_root->nlink = 0; 48377b14db5SEric W. Biederman return 0; 48477b14db5SEric W. Biederman } 485