| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * proc/fs/generic.c --- generic routines for the proc-fs | 
|  | 3 | * | 
|  | 4 | * This file contains generic proc-fs routines for handling | 
|  | 5 | * directories and files. | 
|  | 6 | * | 
|  | 7 | * Copyright (C) 1991, 1992 Linus Torvalds. | 
|  | 8 | * Copyright (C) 1997 Theodore Ts'o | 
|  | 9 | */ | 
|  | 10 |  | 
|  | 11 | #include <linux/errno.h> | 
|  | 12 | #include <linux/time.h> | 
|  | 13 | #include <linux/proc_fs.h> | 
|  | 14 | #include <linux/stat.h> | 
| Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 15 | #include <linux/mm.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> | 
| Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 18 | #include <linux/printk.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mount.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> | 
|  | 21 | #include <linux/idr.h> | 
|  | 22 | #include <linux/namei.h> | 
|  | 23 | #include <linux/bitops.h> | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 24 | #include <linux/spinlock.h> | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 25 | #include <linux/completion.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/uaccess.h> | 
|  | 27 |  | 
| Adrian Bunk | fee781e | 2006-01-08 01:04:16 -0800 | [diff] [blame] | 28 | #include "internal.h" | 
|  | 29 |  | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 30 | DEFINE_SPINLOCK(proc_subdir_lock); | 
|  | 31 |  | 
| Alexey Dobriyan | 312ec7e | 2011-03-23 16:42:52 -0700 | [diff] [blame] | 32 | static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { | 
|  | 34 | if (de->namelen != len) | 
|  | 35 | return 0; | 
|  | 36 | return !memcmp(name, de->name, len); | 
|  | 37 | } | 
|  | 38 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* buffer size is one page but our output routines use some slack for overruns */ | 
|  | 40 | #define PROC_BLOCK_SIZE	(PAGE_SIZE - 1024) | 
|  | 41 |  | 
|  | 42 | static ssize_t | 
| Alexey Dobriyan | 3dec7f5 | 2009-02-20 17:04:33 +0300 | [diff] [blame] | 43 | __proc_file_read(struct file *file, char __user *buf, size_t nbytes, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | loff_t *ppos) | 
|  | 45 | { | 
| Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 46 | struct inode * inode = file_inode(file); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | char 	*page; | 
|  | 48 | ssize_t	retval=0; | 
|  | 49 | int	eof=0; | 
|  | 50 | ssize_t	n, count; | 
|  | 51 | char	*start; | 
|  | 52 | struct proc_dir_entry * dp; | 
| Linus Torvalds | 8b90db0 | 2005-12-30 08:39:10 -0800 | [diff] [blame] | 53 | unsigned long long pos; | 
|  | 54 |  | 
|  | 55 | /* | 
|  | 56 | * Gaah, please just use "seq_file" instead. The legacy /proc | 
|  | 57 | * interfaces cut loff_t down to off_t for reads, and ignore | 
|  | 58 | * the offset entirely for writes.. | 
|  | 59 | */ | 
|  | 60 | pos = *ppos; | 
|  | 61 | if (pos > MAX_NON_LFS) | 
|  | 62 | return 0; | 
|  | 63 | if (nbytes > MAX_NON_LFS - pos) | 
|  | 64 | nbytes = MAX_NON_LFS - pos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 |  | 
|  | 66 | dp = PDE(inode); | 
| Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 67 | if (!(page = (char*) __get_free_page(GFP_TEMPORARY))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return -ENOMEM; | 
|  | 69 |  | 
|  | 70 | while ((nbytes > 0) && !eof) { | 
|  | 71 | count = min_t(size_t, PROC_BLOCK_SIZE, nbytes); | 
|  | 72 |  | 
|  | 73 | start = NULL; | 
| Alexey Dobriyan | 8731f14 | 2008-04-29 01:01:58 -0700 | [diff] [blame] | 74 | if (dp->read_proc) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* | 
|  | 76 | * How to be a proc read function | 
|  | 77 | * ------------------------------ | 
|  | 78 | * Prototype: | 
|  | 79 | *    int f(char *buffer, char **start, off_t offset, | 
|  | 80 | *          int count, int *peof, void *dat) | 
|  | 81 | * | 
|  | 82 | * Assume that the buffer is "count" bytes in size. | 
|  | 83 | * | 
|  | 84 | * If you know you have supplied all the data you | 
|  | 85 | * have, set *peof. | 
|  | 86 | * | 
|  | 87 | * You have three ways to return data: | 
|  | 88 | * 0) Leave *start = NULL.  (This is the default.) | 
|  | 89 | *    Put the data of the requested offset at that | 
|  | 90 | *    offset within the buffer.  Return the number (n) | 
|  | 91 | *    of bytes there are from the beginning of the | 
|  | 92 | *    buffer up to the last byte of data.  If the | 
|  | 93 | *    number of supplied bytes (= n - offset) is | 
|  | 94 | *    greater than zero and you didn't signal eof | 
|  | 95 | *    and the reader is prepared to take more data | 
|  | 96 | *    you will be called again with the requested | 
|  | 97 | *    offset advanced by the number of bytes | 
|  | 98 | *    absorbed.  This interface is useful for files | 
|  | 99 | *    no larger than the buffer. | 
|  | 100 | * 1) Set *start = an unsigned long value less than | 
|  | 101 | *    the buffer address but greater than zero. | 
|  | 102 | *    Put the data of the requested offset at the | 
|  | 103 | *    beginning of the buffer.  Return the number of | 
|  | 104 | *    bytes of data placed there.  If this number is | 
|  | 105 | *    greater than zero and you didn't signal eof | 
|  | 106 | *    and the reader is prepared to take more data | 
|  | 107 | *    you will be called again with the requested | 
|  | 108 | *    offset advanced by *start.  This interface is | 
|  | 109 | *    useful when you have a large file consisting | 
|  | 110 | *    of a series of blocks which you want to count | 
|  | 111 | *    and return as wholes. | 
|  | 112 | *    (Hack by Paul.Russell@rustcorp.com.au) | 
|  | 113 | * 2) Set *start = an address within the buffer. | 
|  | 114 | *    Put the data of the requested offset at *start. | 
|  | 115 | *    Return the number of bytes of data placed there. | 
|  | 116 | *    If this number is greater than zero and you | 
|  | 117 | *    didn't signal eof and the reader is prepared to | 
|  | 118 | *    take more data you will be called again with the | 
|  | 119 | *    requested offset advanced by the number of bytes | 
|  | 120 | *    absorbed. | 
|  | 121 | */ | 
|  | 122 | n = dp->read_proc(page, &start, *ppos, | 
|  | 123 | count, &eof, dp->data); | 
|  | 124 | } else | 
|  | 125 | break; | 
|  | 126 |  | 
|  | 127 | if (n == 0)   /* end of file */ | 
|  | 128 | break; | 
|  | 129 | if (n < 0) {  /* error */ | 
|  | 130 | if (retval == 0) | 
|  | 131 | retval = n; | 
|  | 132 | break; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | if (start == NULL) { | 
| Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 136 | if (n > PAGE_SIZE)	/* Apparent buffer overflow */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | n = PAGE_SIZE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | n -= *ppos; | 
|  | 139 | if (n <= 0) | 
|  | 140 | break; | 
|  | 141 | if (n > count) | 
|  | 142 | n = count; | 
|  | 143 | start = page + *ppos; | 
|  | 144 | } else if (start < page) { | 
| Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 145 | if (n > PAGE_SIZE)	/* Apparent buffer overflow */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | n = PAGE_SIZE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (n > count) { | 
|  | 148 | /* | 
|  | 149 | * Don't reduce n because doing so might | 
|  | 150 | * cut off part of a data block. | 
|  | 151 | */ | 
| Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 152 | pr_warn("proc_file_read: count exceeded\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } | 
|  | 154 | } else /* start >= page */ { | 
|  | 155 | unsigned long startoff = (unsigned long)(start - page); | 
| Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 156 | if (n > (PAGE_SIZE - startoff))	/* buffer overflow? */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | n = PAGE_SIZE - startoff; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (n > count) | 
|  | 159 | n = count; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | n -= copy_to_user(buf, start < page ? page : start, n); | 
|  | 163 | if (n == 0) { | 
|  | 164 | if (retval == 0) | 
|  | 165 | retval = -EFAULT; | 
|  | 166 | break; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | *ppos += start < page ? (unsigned long)start : n; | 
|  | 170 | nbytes -= n; | 
|  | 171 | buf += n; | 
|  | 172 | retval += n; | 
|  | 173 | } | 
|  | 174 | free_page((unsigned long) page); | 
|  | 175 | return retval; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | static ssize_t | 
| Alexey Dobriyan | 3dec7f5 | 2009-02-20 17:04:33 +0300 | [diff] [blame] | 179 | proc_file_read(struct file *file, char __user *buf, size_t nbytes, | 
|  | 180 | loff_t *ppos) | 
|  | 181 | { | 
| Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 182 | struct proc_dir_entry *pde = PDE(file_inode(file)); | 
| Alexey Dobriyan | 3dec7f5 | 2009-02-20 17:04:33 +0300 | [diff] [blame] | 183 | ssize_t rv = -EIO; | 
|  | 184 |  | 
|  | 185 | spin_lock(&pde->pde_unload_lock); | 
|  | 186 | if (!pde->proc_fops) { | 
|  | 187 | spin_unlock(&pde->pde_unload_lock); | 
|  | 188 | return rv; | 
|  | 189 | } | 
|  | 190 | pde->pde_users++; | 
|  | 191 | spin_unlock(&pde->pde_unload_lock); | 
|  | 192 |  | 
|  | 193 | rv = __proc_file_read(file, buf, nbytes, ppos); | 
|  | 194 |  | 
|  | 195 | pde_users_dec(pde); | 
|  | 196 | return rv; | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | static ssize_t | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | proc_file_write(struct file *file, const char __user *buffer, | 
|  | 201 | size_t count, loff_t *ppos) | 
|  | 202 | { | 
| Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 203 | struct proc_dir_entry *pde = PDE(file_inode(file)); | 
| Alexey Dobriyan | 3dec7f5 | 2009-02-20 17:04:33 +0300 | [diff] [blame] | 204 | ssize_t rv = -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 |  | 
| Alexey Dobriyan | 3dec7f5 | 2009-02-20 17:04:33 +0300 | [diff] [blame] | 206 | if (pde->write_proc) { | 
|  | 207 | spin_lock(&pde->pde_unload_lock); | 
|  | 208 | if (!pde->proc_fops) { | 
|  | 209 | spin_unlock(&pde->pde_unload_lock); | 
|  | 210 | return rv; | 
|  | 211 | } | 
|  | 212 | pde->pde_users++; | 
|  | 213 | spin_unlock(&pde->pde_unload_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 |  | 
| Alexey Dobriyan | 3dec7f5 | 2009-02-20 17:04:33 +0300 | [diff] [blame] | 215 | /* FIXME: does this routine need ppos?  probably... */ | 
|  | 216 | rv = pde->write_proc(file, buffer, count, pde->data); | 
|  | 217 | pde_users_dec(pde); | 
|  | 218 | } | 
|  | 219 | return rv; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
|  | 222 |  | 
|  | 223 | static loff_t | 
|  | 224 | proc_file_lseek(struct file *file, loff_t offset, int orig) | 
|  | 225 | { | 
| Linus Torvalds | 8b90db0 | 2005-12-30 08:39:10 -0800 | [diff] [blame] | 226 | loff_t retval = -EINVAL; | 
|  | 227 | switch (orig) { | 
|  | 228 | case 1: | 
|  | 229 | offset += file->f_pos; | 
|  | 230 | /* fallthrough */ | 
|  | 231 | case 0: | 
|  | 232 | if (offset < 0 || offset > MAX_NON_LFS) | 
|  | 233 | break; | 
|  | 234 | file->f_pos = retval = offset; | 
|  | 235 | } | 
|  | 236 | return retval; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
| Alexey Dobriyan | 76df0c2 | 2008-02-08 04:18:27 -0800 | [diff] [blame] | 239 | static const struct file_operations proc_file_operations = { | 
|  | 240 | .llseek		= proc_file_lseek, | 
|  | 241 | .read		= proc_file_read, | 
|  | 242 | .write		= proc_file_write, | 
|  | 243 | }; | 
|  | 244 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | static int proc_notify_change(struct dentry *dentry, struct iattr *iattr) | 
|  | 246 | { | 
|  | 247 | struct inode *inode = dentry->d_inode; | 
|  | 248 | struct proc_dir_entry *de = PDE(inode); | 
|  | 249 | int error; | 
|  | 250 |  | 
|  | 251 | error = inode_change_ok(inode, iattr); | 
|  | 252 | if (error) | 
| Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 253 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 |  | 
| Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 255 | setattr_copy(inode, iattr); | 
|  | 256 | mark_inode_dirty(inode); | 
| Marco Stornelli | 46f6955 | 2012-12-15 11:48:48 +0100 | [diff] [blame] | 257 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | de->uid = inode->i_uid; | 
|  | 259 | de->gid = inode->i_gid; | 
|  | 260 | de->mode = inode->i_mode; | 
| Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 261 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| Miklos Szeredi | 2b579be | 2005-09-06 15:17:18 -0700 | [diff] [blame] | 264 | static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry, | 
|  | 265 | struct kstat *stat) | 
|  | 266 | { | 
|  | 267 | struct inode *inode = dentry->d_inode; | 
|  | 268 | struct proc_dir_entry *de = PROC_I(inode)->pde; | 
|  | 269 | if (de && de->nlink) | 
| Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 270 | set_nlink(inode, de->nlink); | 
| Miklos Szeredi | 2b579be | 2005-09-06 15:17:18 -0700 | [diff] [blame] | 271 |  | 
|  | 272 | generic_fillattr(inode, stat); | 
|  | 273 | return 0; | 
|  | 274 | } | 
|  | 275 |  | 
| Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 276 | static const struct inode_operations proc_file_inode_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | .setattr	= proc_notify_change, | 
|  | 278 | }; | 
|  | 279 |  | 
|  | 280 | /* | 
|  | 281 | * This function parses a name such as "tty/driver/serial", and | 
|  | 282 | * returns the struct proc_dir_entry for "/proc/tty/driver", and | 
|  | 283 | * returns "serial" in residual. | 
|  | 284 | */ | 
| Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 285 | static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret, | 
|  | 286 | const char **residual) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { | 
|  | 288 | const char     		*cp = name, *next; | 
|  | 289 | struct proc_dir_entry	*de; | 
| Alexey Dobriyan | 312ec7e | 2011-03-23 16:42:52 -0700 | [diff] [blame] | 290 | unsigned int		len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 |  | 
| Alexey Dobriyan | 7cee4e00 | 2008-04-29 01:01:40 -0700 | [diff] [blame] | 292 | de = *ret; | 
|  | 293 | if (!de) | 
|  | 294 | de = &proc_root; | 
|  | 295 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | while (1) { | 
|  | 297 | next = strchr(cp, '/'); | 
|  | 298 | if (!next) | 
|  | 299 | break; | 
|  | 300 |  | 
|  | 301 | len = next - cp; | 
|  | 302 | for (de = de->subdir; de ; de = de->next) { | 
|  | 303 | if (proc_match(len, cp, de)) | 
|  | 304 | break; | 
|  | 305 | } | 
| Alexey Dobriyan | 12bac0d | 2010-03-05 13:44:00 -0800 | [diff] [blame] | 306 | if (!de) { | 
|  | 307 | WARN(1, "name '%s'\n", name); | 
| Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 308 | return -ENOENT; | 
| Alexey Dobriyan | 12bac0d | 2010-03-05 13:44:00 -0800 | [diff] [blame] | 309 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | cp += len + 1; | 
|  | 311 | } | 
|  | 312 | *residual = cp; | 
|  | 313 | *ret = de; | 
| Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 314 | return 0; | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | static int xlate_proc_name(const char *name, struct proc_dir_entry **ret, | 
|  | 318 | const char **residual) | 
|  | 319 | { | 
|  | 320 | int rv; | 
|  | 321 |  | 
|  | 322 | spin_lock(&proc_subdir_lock); | 
|  | 323 | rv = __xlate_proc_name(name, ret, residual); | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 324 | spin_unlock(&proc_subdir_lock); | 
| Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 325 | return rv; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } | 
|  | 327 |  | 
| Alexey Dobriyan | 9a18540 | 2008-07-26 11:21:37 +0400 | [diff] [blame] | 328 | static DEFINE_IDA(proc_inum_ida); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */ | 
|  | 330 |  | 
| Alexey Dobriyan | 67935df | 2008-07-26 11:18:28 +0400 | [diff] [blame] | 331 | #define PROC_DYNAMIC_FIRST 0xF0000000U | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 |  | 
|  | 333 | /* | 
|  | 334 | * Return an inode number between PROC_DYNAMIC_FIRST and | 
|  | 335 | * 0xffffffff, or zero on failure. | 
|  | 336 | */ | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 337 | int proc_alloc_inum(unsigned int *inum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { | 
| Alexey Dobriyan | 67935df | 2008-07-26 11:18:28 +0400 | [diff] [blame] | 339 | unsigned int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | int error; | 
|  | 341 |  | 
|  | 342 | retry: | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 343 | if (!ida_pre_get(&proc_inum_ida, GFP_KERNEL)) | 
|  | 344 | return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 |  | 
| Eric W. Biederman | dfb2ea4 | 2012-12-21 20:38:00 -0800 | [diff] [blame] | 346 | spin_lock_irq(&proc_inum_lock); | 
| Alexey Dobriyan | 9a18540 | 2008-07-26 11:21:37 +0400 | [diff] [blame] | 347 | error = ida_get_new(&proc_inum_ida, &i); | 
| Eric W. Biederman | dfb2ea4 | 2012-12-21 20:38:00 -0800 | [diff] [blame] | 348 | spin_unlock_irq(&proc_inum_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (error == -EAGAIN) | 
|  | 350 | goto retry; | 
|  | 351 | else if (error) | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 352 | return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 |  | 
| Alexey Dobriyan | 67935df | 2008-07-26 11:18:28 +0400 | [diff] [blame] | 354 | if (i > UINT_MAX - PROC_DYNAMIC_FIRST) { | 
| Eric W. Biederman | dfb2ea4 | 2012-12-21 20:38:00 -0800 | [diff] [blame] | 355 | spin_lock_irq(&proc_inum_lock); | 
| Alexey Dobriyan | 9a18540 | 2008-07-26 11:21:37 +0400 | [diff] [blame] | 356 | ida_remove(&proc_inum_ida, i); | 
| Eric W. Biederman | dfb2ea4 | 2012-12-21 20:38:00 -0800 | [diff] [blame] | 357 | spin_unlock_irq(&proc_inum_lock); | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 358 | return -ENOSPC; | 
| Alexey Dobriyan | 67935df | 2008-07-26 11:18:28 +0400 | [diff] [blame] | 359 | } | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 360 | *inum = PROC_DYNAMIC_FIRST + i; | 
|  | 361 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } | 
|  | 363 |  | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 364 | void proc_free_inum(unsigned int inum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | { | 
| Eric W. Biederman | dfb2ea4 | 2012-12-21 20:38:00 -0800 | [diff] [blame] | 366 | unsigned long flags; | 
|  | 367 | spin_lock_irqsave(&proc_inum_lock, flags); | 
| Alexey Dobriyan | 9a18540 | 2008-07-26 11:21:37 +0400 | [diff] [blame] | 368 | ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST); | 
| Eric W. Biederman | dfb2ea4 | 2012-12-21 20:38:00 -0800 | [diff] [blame] | 369 | spin_unlock_irqrestore(&proc_inum_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } | 
|  | 371 |  | 
| Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 372 | static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { | 
|  | 374 | nd_set_link(nd, PDE(dentry->d_inode)->data); | 
| Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 375 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } | 
|  | 377 |  | 
| Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 378 | static const struct inode_operations proc_link_inode_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | .readlink	= generic_readlink, | 
|  | 380 | .follow_link	= proc_follow_link, | 
|  | 381 | }; | 
|  | 382 |  | 
|  | 383 | /* | 
|  | 384 | * As some entries in /proc are volatile, we want to | 
|  | 385 | * get rid of unused dentries.  This could be made | 
|  | 386 | * smarter: we could keep a "volatile" flag in the | 
|  | 387 | * inode to indicate which ones to keep. | 
|  | 388 | */ | 
| Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 389 | static int proc_delete_dentry(const struct dentry * dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { | 
|  | 391 | return 1; | 
|  | 392 | } | 
|  | 393 |  | 
| Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 394 | static const struct dentry_operations proc_dentry_operations = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { | 
|  | 396 | .d_delete	= proc_delete_dentry, | 
|  | 397 | }; | 
|  | 398 |  | 
|  | 399 | /* | 
|  | 400 | * Don't create negative dentries here, return -ENOENT by hand | 
|  | 401 | * instead. | 
|  | 402 | */ | 
| Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 403 | struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir, | 
|  | 404 | struct dentry *dentry) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { | 
| Al Viro | d3d009c | 2013-01-25 20:11:22 -0500 | [diff] [blame] | 406 | struct inode *inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 |  | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 408 | spin_lock(&proc_subdir_lock); | 
| Alexey Dobriyan | 5e971dc | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 409 | for (de = de->subdir; de ; de = de->next) { | 
|  | 410 | if (de->namelen != dentry->d_name.len) | 
|  | 411 | continue; | 
|  | 412 | if (!memcmp(dentry->d_name.name, de->name, de->namelen)) { | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 413 | pde_get(de); | 
| Alexey Dobriyan | 5e971dc | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 414 | spin_unlock(&proc_subdir_lock); | 
| Alexey Dobriyan | 6d1b6e4 | 2011-01-12 17:00:33 -0800 | [diff] [blame] | 415 | inode = proc_get_inode(dir->i_sb, de); | 
| Al Viro | d3d009c | 2013-01-25 20:11:22 -0500 | [diff] [blame] | 416 | if (!inode) | 
|  | 417 | return ERR_PTR(-ENOMEM); | 
|  | 418 | d_set_d_op(dentry, &proc_dentry_operations); | 
|  | 419 | d_add(dentry, inode); | 
|  | 420 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } | 
|  | 422 | } | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 423 | spin_unlock(&proc_subdir_lock); | 
| Al Viro | d3d009c | 2013-01-25 20:11:22 -0500 | [diff] [blame] | 424 | return ERR_PTR(-ENOENT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } | 
|  | 426 |  | 
| Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 427 | struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry, | 
| Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 428 | unsigned int flags) | 
| Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 429 | { | 
|  | 430 | return proc_lookup_de(PDE(dir), dir, dentry); | 
|  | 431 | } | 
|  | 432 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | /* | 
|  | 434 | * This returns non-zero if at EOF, so that the /proc | 
|  | 435 | * root directory can use this and check if it should | 
|  | 436 | * continue with the <pid> entries.. | 
|  | 437 | * | 
|  | 438 | * Note that the VFS-layer doesn't care about the return | 
|  | 439 | * value of the readdir() call, as long as it's non-negative | 
|  | 440 | * for success.. | 
|  | 441 | */ | 
| Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 442 | int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent, | 
|  | 443 | filldir_t filldir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | unsigned int ino; | 
|  | 446 | int i; | 
| Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 447 | struct inode *inode = file_inode(filp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | int ret = 0; | 
|  | 449 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | ino = inode->i_ino; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | i = filp->f_pos; | 
|  | 452 | switch (i) { | 
|  | 453 | case 0: | 
|  | 454 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) | 
|  | 455 | goto out; | 
|  | 456 | i++; | 
|  | 457 | filp->f_pos++; | 
|  | 458 | /* fall through */ | 
|  | 459 | case 1: | 
|  | 460 | if (filldir(dirent, "..", 2, i, | 
| Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 461 | parent_ino(filp->f_path.dentry), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | DT_DIR) < 0) | 
|  | 463 | goto out; | 
|  | 464 | i++; | 
|  | 465 | filp->f_pos++; | 
|  | 466 | /* fall through */ | 
|  | 467 | default: | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 468 | spin_lock(&proc_subdir_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | de = de->subdir; | 
|  | 470 | i -= 2; | 
|  | 471 | for (;;) { | 
|  | 472 | if (!de) { | 
|  | 473 | ret = 1; | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 474 | spin_unlock(&proc_subdir_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | goto out; | 
|  | 476 | } | 
|  | 477 | if (!i) | 
|  | 478 | break; | 
|  | 479 | de = de->next; | 
|  | 480 | i--; | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | do { | 
| Darrick J. Wong | 59cd0cb | 2007-05-08 00:25:47 -0700 | [diff] [blame] | 484 | struct proc_dir_entry *next; | 
|  | 485 |  | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 486 | /* filldir passes info to user space */ | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 487 | pde_get(de); | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 488 | spin_unlock(&proc_subdir_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | if (filldir(dirent, de->name, de->namelen, filp->f_pos, | 
| Darrick J. Wong | 59cd0cb | 2007-05-08 00:25:47 -0700 | [diff] [blame] | 490 | de->low_ino, de->mode >> 12) < 0) { | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 491 | pde_put(de); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | goto out; | 
| Darrick J. Wong | 59cd0cb | 2007-05-08 00:25:47 -0700 | [diff] [blame] | 493 | } | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 494 | spin_lock(&proc_subdir_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | filp->f_pos++; | 
| Darrick J. Wong | 59cd0cb | 2007-05-08 00:25:47 -0700 | [diff] [blame] | 496 | next = de->next; | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 497 | pde_put(de); | 
| Darrick J. Wong | 59cd0cb | 2007-05-08 00:25:47 -0700 | [diff] [blame] | 498 | de = next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } while (de); | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 500 | spin_unlock(&proc_subdir_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } | 
|  | 502 | ret = 1; | 
| Alexey Dobriyan | b4df2b9 | 2008-10-27 22:48:36 +0300 | [diff] [blame] | 503 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | return ret; | 
|  | 505 | } | 
|  | 506 |  | 
| Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 507 | int proc_readdir(struct file *filp, void *dirent, filldir_t filldir) | 
|  | 508 | { | 
| Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 509 | struct inode *inode = file_inode(filp); | 
| Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 510 |  | 
|  | 511 | return proc_readdir_de(PDE(inode), filp, dirent, filldir); | 
|  | 512 | } | 
|  | 513 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | /* | 
|  | 515 | * These are the generic /proc directory operations. They | 
|  | 516 | * use the in-memory "struct proc_dir_entry" tree to parse | 
|  | 517 | * the /proc directory. | 
|  | 518 | */ | 
| Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 519 | static const struct file_operations proc_dir_operations = { | 
| Alexey Dobriyan | b4df2b9 | 2008-10-27 22:48:36 +0300 | [diff] [blame] | 520 | .llseek			= generic_file_llseek, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | .read			= generic_read_dir, | 
|  | 522 | .readdir		= proc_readdir, | 
|  | 523 | }; | 
|  | 524 |  | 
|  | 525 | /* | 
|  | 526 | * proc directories can do almost nothing.. | 
|  | 527 | */ | 
| Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 528 | static const struct inode_operations proc_dir_inode_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | .lookup		= proc_lookup, | 
| Miklos Szeredi | 2b579be | 2005-09-06 15:17:18 -0700 | [diff] [blame] | 530 | .getattr	= proc_getattr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | .setattr	= proc_notify_change, | 
|  | 532 | }; | 
|  | 533 |  | 
|  | 534 | static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp) | 
|  | 535 | { | 
| Zhang Rui | 94413d8 | 2008-02-08 04:18:29 -0800 | [diff] [blame] | 536 | struct proc_dir_entry *tmp; | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 537 | int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 |  | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 539 | ret = proc_alloc_inum(&dp->low_ino); | 
|  | 540 | if (ret) | 
|  | 541 | return ret; | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 542 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | if (S_ISDIR(dp->mode)) { | 
|  | 544 | if (dp->proc_iops == NULL) { | 
|  | 545 | dp->proc_fops = &proc_dir_operations; | 
|  | 546 | dp->proc_iops = &proc_dir_inode_operations; | 
|  | 547 | } | 
|  | 548 | dir->nlink++; | 
|  | 549 | } else if (S_ISLNK(dp->mode)) { | 
|  | 550 | if (dp->proc_iops == NULL) | 
|  | 551 | dp->proc_iops = &proc_link_inode_operations; | 
|  | 552 | } else if (S_ISREG(dp->mode)) { | 
|  | 553 | if (dp->proc_fops == NULL) | 
|  | 554 | dp->proc_fops = &proc_file_operations; | 
|  | 555 | if (dp->proc_iops == NULL) | 
|  | 556 | dp->proc_iops = &proc_file_inode_operations; | 
|  | 557 | } | 
| Changli Gao | 99fc06d | 2007-07-15 23:40:09 -0700 | [diff] [blame] | 558 |  | 
|  | 559 | spin_lock(&proc_subdir_lock); | 
| Zhang Rui | 94413d8 | 2008-02-08 04:18:29 -0800 | [diff] [blame] | 560 |  | 
|  | 561 | for (tmp = dir->subdir; tmp; tmp = tmp->next) | 
|  | 562 | if (strcmp(tmp->name, dp->name) == 0) { | 
| Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 563 | WARN(1, "proc_dir_entry '%s/%s' already registered\n", | 
| Alexey Dobriyan | 665020c | 2008-09-13 02:33:06 -0700 | [diff] [blame] | 564 | dir->name, dp->name); | 
| Zhang Rui | 94413d8 | 2008-02-08 04:18:29 -0800 | [diff] [blame] | 565 | break; | 
|  | 566 | } | 
|  | 567 |  | 
| Changli Gao | 99fc06d | 2007-07-15 23:40:09 -0700 | [diff] [blame] | 568 | dp->next = dir->subdir; | 
|  | 569 | dp->parent = dir; | 
|  | 570 | dir->subdir = dp; | 
|  | 571 | spin_unlock(&proc_subdir_lock); | 
|  | 572 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | return 0; | 
|  | 574 | } | 
|  | 575 |  | 
| Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 576 | static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | const char *name, | 
| Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 578 | umode_t mode, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | nlink_t nlink) | 
|  | 580 | { | 
|  | 581 | struct proc_dir_entry *ent = NULL; | 
|  | 582 | const char *fn = name; | 
| Alexey Dobriyan | 312ec7e | 2011-03-23 16:42:52 -0700 | [diff] [blame] | 583 | unsigned int len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 |  | 
|  | 585 | /* make sure name is valid */ | 
| yan | 17baa2a | 2012-10-04 17:15:43 -0700 | [diff] [blame] | 586 | if (!name || !strlen(name)) | 
|  | 587 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 |  | 
| Alexey Dobriyan | 7cee4e00 | 2008-04-29 01:01:40 -0700 | [diff] [blame] | 589 | if (xlate_proc_name(name, parent, &fn) != 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | goto out; | 
|  | 591 |  | 
|  | 592 | /* At this point there must not be any '/' characters beyond *fn */ | 
|  | 593 | if (strchr(fn, '/')) | 
|  | 594 | goto out; | 
|  | 595 |  | 
|  | 596 | len = strlen(fn); | 
|  | 597 |  | 
| yan | 17baa2a | 2012-10-04 17:15:43 -0700 | [diff] [blame] | 598 | ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL); | 
|  | 599 | if (!ent) | 
|  | 600 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 |  | 
| David Howells | 09570f9 | 2011-07-27 21:47:03 +0300 | [diff] [blame] | 602 | memcpy(ent->name, fn, len + 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | ent->namelen = len; | 
|  | 604 | ent->mode = mode; | 
|  | 605 | ent->nlink = nlink; | 
| Alexey Dobriyan | 5a622f2 | 2007-12-04 23:45:28 -0800 | [diff] [blame] | 606 | atomic_set(&ent->count, 1); | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 607 | spin_lock_init(&ent->pde_unload_lock); | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 608 | INIT_LIST_HEAD(&ent->pde_openers); | 
| yan | 17baa2a | 2012-10-04 17:15:43 -0700 | [diff] [blame] | 609 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | return ent; | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | struct proc_dir_entry *proc_symlink(const char *name, | 
|  | 614 | struct proc_dir_entry *parent, const char *dest) | 
|  | 615 | { | 
|  | 616 | struct proc_dir_entry *ent; | 
|  | 617 |  | 
| Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 618 | ent = __proc_create(&parent, name, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1); | 
|  | 620 |  | 
|  | 621 | if (ent) { | 
|  | 622 | ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL); | 
|  | 623 | if (ent->data) { | 
|  | 624 | strcpy((char*)ent->data,dest); | 
|  | 625 | if (proc_register(parent, ent) < 0) { | 
|  | 626 | kfree(ent->data); | 
|  | 627 | kfree(ent); | 
|  | 628 | ent = NULL; | 
|  | 629 | } | 
|  | 630 | } else { | 
|  | 631 | kfree(ent); | 
|  | 632 | ent = NULL; | 
|  | 633 | } | 
|  | 634 | } | 
|  | 635 | return ent; | 
|  | 636 | } | 
| Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 637 | EXPORT_SYMBOL(proc_symlink); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 |  | 
| Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 639 | struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | struct proc_dir_entry *parent) | 
|  | 641 | { | 
|  | 642 | struct proc_dir_entry *ent; | 
|  | 643 |  | 
| Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 644 | ent = __proc_create(&parent, name, S_IFDIR | mode, 2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (ent) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (proc_register(parent, ent) < 0) { | 
|  | 647 | kfree(ent); | 
|  | 648 | ent = NULL; | 
|  | 649 | } | 
|  | 650 | } | 
|  | 651 | return ent; | 
|  | 652 | } | 
| Alexey Dobriyan | 011159a | 2011-05-14 00:12:48 +0300 | [diff] [blame] | 653 | EXPORT_SYMBOL(proc_mkdir_mode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 |  | 
| Denis V. Lunev | 78e92b9 | 2008-05-02 04:12:41 -0700 | [diff] [blame] | 655 | struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name, | 
|  | 656 | struct proc_dir_entry *parent) | 
|  | 657 | { | 
|  | 658 | struct proc_dir_entry *ent; | 
|  | 659 |  | 
|  | 660 | ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2); | 
|  | 661 | if (ent) { | 
|  | 662 | ent->data = net; | 
|  | 663 | if (proc_register(parent, ent) < 0) { | 
|  | 664 | kfree(ent); | 
|  | 665 | ent = NULL; | 
|  | 666 | } | 
|  | 667 | } | 
|  | 668 | return ent; | 
|  | 669 | } | 
|  | 670 | EXPORT_SYMBOL_GPL(proc_net_mkdir); | 
|  | 671 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | struct proc_dir_entry *proc_mkdir(const char *name, | 
|  | 673 | struct proc_dir_entry *parent) | 
|  | 674 | { | 
|  | 675 | return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent); | 
|  | 676 | } | 
| Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 677 | EXPORT_SYMBOL(proc_mkdir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 |  | 
| Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 679 | struct proc_dir_entry *create_proc_entry(const char *name, umode_t mode, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | struct proc_dir_entry *parent) | 
|  | 681 | { | 
|  | 682 | struct proc_dir_entry *ent; | 
|  | 683 | nlink_t nlink; | 
|  | 684 |  | 
|  | 685 | if (S_ISDIR(mode)) { | 
|  | 686 | if ((mode & S_IALLUGO) == 0) | 
|  | 687 | mode |= S_IRUGO | S_IXUGO; | 
|  | 688 | nlink = 2; | 
|  | 689 | } else { | 
|  | 690 | if ((mode & S_IFMT) == 0) | 
|  | 691 | mode |= S_IFREG; | 
|  | 692 | if ((mode & S_IALLUGO) == 0) | 
|  | 693 | mode |= S_IRUGO; | 
|  | 694 | nlink = 1; | 
|  | 695 | } | 
|  | 696 |  | 
| Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 697 | ent = __proc_create(&parent, name, mode, nlink); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | if (ent) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | if (proc_register(parent, ent) < 0) { | 
|  | 700 | kfree(ent); | 
|  | 701 | ent = NULL; | 
|  | 702 | } | 
|  | 703 | } | 
|  | 704 | return ent; | 
|  | 705 | } | 
| Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 706 | EXPORT_SYMBOL(create_proc_entry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 |  | 
| Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 708 | struct proc_dir_entry *proc_create_data(const char *name, umode_t mode, | 
| Denis V. Lunev | 59b7435 | 2008-04-29 01:02:00 -0700 | [diff] [blame] | 709 | struct proc_dir_entry *parent, | 
|  | 710 | const struct file_operations *proc_fops, | 
|  | 711 | void *data) | 
| Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 712 | { | 
|  | 713 | struct proc_dir_entry *pde; | 
|  | 714 | nlink_t nlink; | 
|  | 715 |  | 
|  | 716 | if (S_ISDIR(mode)) { | 
|  | 717 | if ((mode & S_IALLUGO) == 0) | 
|  | 718 | mode |= S_IRUGO | S_IXUGO; | 
|  | 719 | nlink = 2; | 
|  | 720 | } else { | 
|  | 721 | if ((mode & S_IFMT) == 0) | 
|  | 722 | mode |= S_IFREG; | 
|  | 723 | if ((mode & S_IALLUGO) == 0) | 
|  | 724 | mode |= S_IRUGO; | 
|  | 725 | nlink = 1; | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | pde = __proc_create(&parent, name, mode, nlink); | 
|  | 729 | if (!pde) | 
|  | 730 | goto out; | 
|  | 731 | pde->proc_fops = proc_fops; | 
| Denis V. Lunev | 59b7435 | 2008-04-29 01:02:00 -0700 | [diff] [blame] | 732 | pde->data = data; | 
| Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 733 | if (proc_register(parent, pde) < 0) | 
|  | 734 | goto out_free; | 
|  | 735 | return pde; | 
|  | 736 | out_free: | 
|  | 737 | kfree(pde); | 
|  | 738 | out: | 
|  | 739 | return NULL; | 
|  | 740 | } | 
| Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 741 | EXPORT_SYMBOL(proc_create_data); | 
| Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 742 |  | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 743 | static void free_proc_entry(struct proc_dir_entry *de) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { | 
| Eric W. Biederman | 33d6dce | 2011-06-17 13:33:20 -0700 | [diff] [blame] | 745 | proc_free_inum(de->low_ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 |  | 
| Alexey Dobriyan | fd2cbe4 | 2008-02-08 04:18:28 -0800 | [diff] [blame] | 747 | if (S_ISLNK(de->mode)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | kfree(de->data); | 
|  | 749 | kfree(de); | 
|  | 750 | } | 
|  | 751 |  | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 752 | void pde_put(struct proc_dir_entry *pde) | 
|  | 753 | { | 
|  | 754 | if (atomic_dec_and_test(&pde->count)) | 
|  | 755 | free_proc_entry(pde); | 
|  | 756 | } | 
|  | 757 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | /* | 
|  | 759 | * Remove a /proc entry and free it if it's not currently in use. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | */ | 
|  | 761 | void remove_proc_entry(const char *name, struct proc_dir_entry *parent) | 
|  | 762 | { | 
|  | 763 | struct proc_dir_entry **p; | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 764 | struct proc_dir_entry *de = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | const char *fn = name; | 
| Alexey Dobriyan | 312ec7e | 2011-03-23 16:42:52 -0700 | [diff] [blame] | 766 | unsigned int len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 |  | 
| Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 768 | spin_lock(&proc_subdir_lock); | 
|  | 769 | if (__xlate_proc_name(name, &parent, &fn) != 0) { | 
|  | 770 | spin_unlock(&proc_subdir_lock); | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 771 | return; | 
| Alexey Dobriyan | e17a576 | 2010-03-05 13:43:59 -0800 | [diff] [blame] | 772 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | len = strlen(fn); | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 774 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | for (p = &parent->subdir; *p; p=&(*p)->next ) { | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 776 | if (proc_match(len, fn, *p)) { | 
|  | 777 | de = *p; | 
|  | 778 | *p = de->next; | 
|  | 779 | de->next = NULL; | 
|  | 780 | break; | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 781 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } | 
| Steven Rostedt | 64a07bd | 2006-03-26 01:36:55 -0800 | [diff] [blame] | 783 | spin_unlock(&proc_subdir_lock); | 
| Alexey Dobriyan | 12bac0d | 2010-03-05 13:44:00 -0800 | [diff] [blame] | 784 | if (!de) { | 
|  | 785 | WARN(1, "name '%s'\n", name); | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 786 | return; | 
| Alexey Dobriyan | 12bac0d | 2010-03-05 13:44:00 -0800 | [diff] [blame] | 787 | } | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 788 |  | 
|  | 789 | spin_lock(&de->pde_unload_lock); | 
|  | 790 | /* | 
|  | 791 | * Stop accepting new callers into module. If you're | 
|  | 792 | * dynamically allocating ->proc_fops, save a pointer somewhere. | 
|  | 793 | */ | 
|  | 794 | de->proc_fops = NULL; | 
|  | 795 | /* Wait until all existing callers into module are done. */ | 
|  | 796 | if (de->pde_users > 0) { | 
|  | 797 | DECLARE_COMPLETION_ONSTACK(c); | 
|  | 798 |  | 
|  | 799 | if (!de->pde_unload_completion) | 
|  | 800 | de->pde_unload_completion = &c; | 
|  | 801 |  | 
|  | 802 | spin_unlock(&de->pde_unload_lock); | 
|  | 803 |  | 
|  | 804 | wait_for_completion(de->pde_unload_completion); | 
|  | 805 |  | 
| Alexey Dobriyan | 3740a20 | 2011-01-12 17:00:35 -0800 | [diff] [blame] | 806 | spin_lock(&de->pde_unload_lock); | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 807 | } | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 808 |  | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 809 | while (!list_empty(&de->pde_openers)) { | 
|  | 810 | struct pde_opener *pdeo; | 
|  | 811 |  | 
|  | 812 | pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh); | 
|  | 813 | list_del(&pdeo->lh); | 
|  | 814 | spin_unlock(&de->pde_unload_lock); | 
|  | 815 | pdeo->release(pdeo->inode, pdeo->file); | 
|  | 816 | kfree(pdeo); | 
|  | 817 | spin_lock(&de->pde_unload_lock); | 
|  | 818 | } | 
|  | 819 | spin_unlock(&de->pde_unload_lock); | 
|  | 820 |  | 
| Alexey Dobriyan | f649d6d | 2008-04-29 01:01:39 -0700 | [diff] [blame] | 821 | if (S_ISDIR(de->mode)) | 
|  | 822 | parent->nlink--; | 
|  | 823 | de->nlink = 0; | 
| Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 824 | WARN(de->subdir, "%s: removing non-empty directory " | 
|  | 825 | "'%s/%s', leaking at least '%s'\n", __func__, | 
|  | 826 | de->parent->name, de->name, de->subdir->name); | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 827 | pde_put(de); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } | 
| Helight.Xu | 587d4a1 | 2009-12-30 13:24:41 +0800 | [diff] [blame] | 829 | EXPORT_SYMBOL(remove_proc_entry); |