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