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