| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/fs/proc/inode.c | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 1991, 1992  Linus Torvalds | 
|  | 5 | */ | 
|  | 6 |  | 
|  | 7 | #include <linux/time.h> | 
|  | 8 | #include <linux/proc_fs.h> | 
|  | 9 | #include <linux/kernel.h> | 
|  | 10 | #include <linux/mm.h> | 
|  | 11 | #include <linux/string.h> | 
|  | 12 | #include <linux/stat.h> | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 13 | #include <linux/completion.h> | 
| Alexey Dobriyan | dd23aae | 2007-09-11 15:23:55 -0700 | [diff] [blame] | 14 | #include <linux/poll.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/file.h> | 
|  | 16 | #include <linux/limits.h> | 
|  | 17 | #include <linux/init.h> | 
|  | 18 | #include <linux/module.h> | 
|  | 19 | #include <linux/smp_lock.h> | 
| Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 20 | #include <linux/sysctl.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <asm/system.h> | 
|  | 24 | #include <asm/uaccess.h> | 
|  | 25 |  | 
| Adrian Bunk | fee781e | 2006-01-08 01:04:16 -0800 | [diff] [blame] | 26 | #include "internal.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  | 
| Al Viro | 8267952 | 2010-06-04 22:17:56 -0400 | [diff] [blame] | 28 | static void proc_evict_inode(struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | { | 
|  | 30 | struct proc_dir_entry *de; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
| Mark Fasheh | fef2665 | 2005-09-09 13:01:31 -0700 | [diff] [blame] | 32 | truncate_inode_pages(&inode->i_data, 0); | 
| Al Viro | 8267952 | 2010-06-04 22:17:56 -0400 | [diff] [blame] | 33 | end_writeback(inode); | 
| Mark Fasheh | fef2665 | 2005-09-09 13:01:31 -0700 | [diff] [blame] | 34 |  | 
| Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 35 | /* Stop tracking associated processes */ | 
| Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 36 | put_pid(PROC_I(inode)->pid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | /* Let go of any associated proc directory entry */ | 
|  | 39 | de = PROC_I(inode)->pde; | 
| Alexey Dobriyan | 99b7623 | 2009-03-25 22:48:06 +0300 | [diff] [blame] | 40 | if (de) | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 41 | pde_put(de); | 
| Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 42 | if (PROC_I(inode)->sysctl) | 
|  | 43 | sysctl_head_put(PROC_I(inode)->sysctl); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
|  | 46 | struct vfsmount *proc_mnt; | 
|  | 47 |  | 
| Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 48 | static struct kmem_cache * proc_inode_cachep; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 |  | 
|  | 50 | static struct inode *proc_alloc_inode(struct super_block *sb) | 
|  | 51 | { | 
|  | 52 | struct proc_inode *ei; | 
|  | 53 | struct inode *inode; | 
|  | 54 |  | 
| Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 55 | ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | if (!ei) | 
|  | 57 | return NULL; | 
| Eric W. Biederman | 13b41b0 | 2006-06-26 00:25:56 -0700 | [diff] [blame] | 58 | ei->pid = NULL; | 
| Eric W. Biederman | aed7a6c | 2006-06-26 00:25:44 -0700 | [diff] [blame] | 59 | ei->fd = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | ei->op.proc_get_link = NULL; | 
|  | 61 | ei->pde = NULL; | 
| Al Viro | 9043476f | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 62 | ei->sysctl = NULL; | 
|  | 63 | ei->sysctl_entry = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | inode = &ei->vfs_inode; | 
|  | 65 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | 
|  | 66 | return inode; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | static void proc_destroy_inode(struct inode *inode) | 
|  | 70 | { | 
|  | 71 | kmem_cache_free(proc_inode_cachep, PROC_I(inode)); | 
|  | 72 | } | 
|  | 73 |  | 
| Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 74 | static void init_once(void *foo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { | 
|  | 76 | struct proc_inode *ei = (struct proc_inode *) foo; | 
|  | 77 |  | 
| Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 78 | inode_init_once(&ei->vfs_inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 80 |  | 
| Alexey Dobriyan | 5bcd7ff | 2008-10-17 03:43:55 +0400 | [diff] [blame] | 81 | void __init proc_init_inodecache(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { | 
|  | 83 | proc_inode_cachep = kmem_cache_create("proc_inode_cache", | 
|  | 84 | sizeof(struct proc_inode), | 
| Paul Jackson | fffb60f | 2006-03-24 03:16:06 -0800 | [diff] [blame] | 85 | 0, (SLAB_RECLAIM_ACCOUNT| | 
| Alexey Dobriyan | 040b5c6 | 2007-10-16 23:26:10 -0700 | [diff] [blame] | 86 | SLAB_MEM_SPREAD|SLAB_PANIC), | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 87 | init_once); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 90 | static const struct super_operations proc_sops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | .alloc_inode	= proc_alloc_inode, | 
|  | 92 | .destroy_inode	= proc_destroy_inode, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .drop_inode	= generic_delete_inode, | 
| Al Viro | 8267952 | 2010-06-04 22:17:56 -0400 | [diff] [blame] | 94 | .evict_inode	= proc_evict_inode, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | .statfs		= simple_statfs, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; | 
|  | 97 |  | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 98 | static void __pde_users_dec(struct proc_dir_entry *pde) | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 99 | { | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 100 | pde->pde_users--; | 
|  | 101 | if (pde->pde_unload_completion && pde->pde_users == 0) | 
|  | 102 | complete(pde->pde_unload_completion); | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
| Alexey Dobriyan | 3dec7f5 | 2009-02-20 17:04:33 +0300 | [diff] [blame] | 105 | void pde_users_dec(struct proc_dir_entry *pde) | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 106 | { | 
|  | 107 | spin_lock(&pde->pde_unload_lock); | 
|  | 108 | __pde_users_dec(pde); | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 109 | spin_unlock(&pde->pde_unload_lock); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence) | 
|  | 113 | { | 
|  | 114 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | 
|  | 115 | loff_t rv = -EINVAL; | 
|  | 116 | loff_t (*llseek)(struct file *, loff_t, int); | 
|  | 117 |  | 
|  | 118 | spin_lock(&pde->pde_unload_lock); | 
|  | 119 | /* | 
|  | 120 | * remove_proc_entry() is going to delete PDE (as part of module | 
|  | 121 | * cleanup sequence). No new callers into module allowed. | 
|  | 122 | */ | 
|  | 123 | if (!pde->proc_fops) { | 
|  | 124 | spin_unlock(&pde->pde_unload_lock); | 
|  | 125 | return rv; | 
|  | 126 | } | 
|  | 127 | /* | 
|  | 128 | * Bump refcount so that remove_proc_entry will wail for ->llseek to | 
|  | 129 | * complete. | 
|  | 130 | */ | 
|  | 131 | pde->pde_users++; | 
|  | 132 | /* | 
|  | 133 | * Save function pointer under lock, to protect against ->proc_fops | 
|  | 134 | * NULL'ifying right after ->pde_unload_lock is dropped. | 
|  | 135 | */ | 
|  | 136 | llseek = pde->proc_fops->llseek; | 
|  | 137 | spin_unlock(&pde->pde_unload_lock); | 
|  | 138 |  | 
|  | 139 | if (!llseek) | 
|  | 140 | llseek = default_llseek; | 
|  | 141 | rv = llseek(file, offset, whence); | 
|  | 142 |  | 
|  | 143 | pde_users_dec(pde); | 
|  | 144 | return rv; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | 
|  | 148 | { | 
|  | 149 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | 
|  | 150 | ssize_t rv = -EIO; | 
|  | 151 | ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); | 
|  | 152 |  | 
|  | 153 | spin_lock(&pde->pde_unload_lock); | 
|  | 154 | if (!pde->proc_fops) { | 
|  | 155 | spin_unlock(&pde->pde_unload_lock); | 
|  | 156 | return rv; | 
|  | 157 | } | 
|  | 158 | pde->pde_users++; | 
|  | 159 | read = pde->proc_fops->read; | 
|  | 160 | spin_unlock(&pde->pde_unload_lock); | 
|  | 161 |  | 
|  | 162 | if (read) | 
|  | 163 | rv = read(file, buf, count, ppos); | 
|  | 164 |  | 
|  | 165 | pde_users_dec(pde); | 
|  | 166 | return rv; | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) | 
|  | 170 | { | 
|  | 171 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | 
|  | 172 | ssize_t rv = -EIO; | 
|  | 173 | ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); | 
|  | 174 |  | 
|  | 175 | spin_lock(&pde->pde_unload_lock); | 
|  | 176 | if (!pde->proc_fops) { | 
|  | 177 | spin_unlock(&pde->pde_unload_lock); | 
|  | 178 | return rv; | 
|  | 179 | } | 
|  | 180 | pde->pde_users++; | 
|  | 181 | write = pde->proc_fops->write; | 
|  | 182 | spin_unlock(&pde->pde_unload_lock); | 
|  | 183 |  | 
|  | 184 | if (write) | 
|  | 185 | rv = write(file, buf, count, ppos); | 
|  | 186 |  | 
|  | 187 | pde_users_dec(pde); | 
|  | 188 | return rv; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts) | 
|  | 192 | { | 
|  | 193 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | 
| Alexey Dobriyan | dd23aae | 2007-09-11 15:23:55 -0700 | [diff] [blame] | 194 | unsigned int rv = DEFAULT_POLLMASK; | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 195 | unsigned int (*poll)(struct file *, struct poll_table_struct *); | 
|  | 196 |  | 
|  | 197 | spin_lock(&pde->pde_unload_lock); | 
|  | 198 | if (!pde->proc_fops) { | 
|  | 199 | spin_unlock(&pde->pde_unload_lock); | 
|  | 200 | return rv; | 
|  | 201 | } | 
|  | 202 | pde->pde_users++; | 
|  | 203 | poll = pde->proc_fops->poll; | 
|  | 204 | spin_unlock(&pde->pde_unload_lock); | 
|  | 205 |  | 
|  | 206 | if (poll) | 
|  | 207 | rv = poll(file, pts); | 
|  | 208 |  | 
|  | 209 | pde_users_dec(pde); | 
|  | 210 | return rv; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | 
|  | 214 | { | 
|  | 215 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | 
|  | 216 | long rv = -ENOTTY; | 
| Arnd Bergmann | b19dd42 | 2010-07-04 00:15:10 +0200 | [diff] [blame] | 217 | long (*ioctl)(struct file *, unsigned int, unsigned long); | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 218 |  | 
|  | 219 | spin_lock(&pde->pde_unload_lock); | 
|  | 220 | if (!pde->proc_fops) { | 
|  | 221 | spin_unlock(&pde->pde_unload_lock); | 
|  | 222 | return rv; | 
|  | 223 | } | 
|  | 224 | pde->pde_users++; | 
| Arnd Bergmann | b19dd42 | 2010-07-04 00:15:10 +0200 | [diff] [blame] | 225 | ioctl = pde->proc_fops->unlocked_ioctl; | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 226 | spin_unlock(&pde->pde_unload_lock); | 
|  | 227 |  | 
| Arnd Bergmann | b19dd42 | 2010-07-04 00:15:10 +0200 | [diff] [blame] | 228 | if (ioctl) | 
|  | 229 | rv = ioctl(file, cmd, arg); | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | pde_users_dec(pde); | 
|  | 232 | return rv; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | #ifdef CONFIG_COMPAT | 
|  | 236 | static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | 
|  | 237 | { | 
|  | 238 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | 
|  | 239 | long rv = -ENOTTY; | 
|  | 240 | long (*compat_ioctl)(struct file *, unsigned int, unsigned long); | 
|  | 241 |  | 
|  | 242 | spin_lock(&pde->pde_unload_lock); | 
|  | 243 | if (!pde->proc_fops) { | 
|  | 244 | spin_unlock(&pde->pde_unload_lock); | 
|  | 245 | return rv; | 
|  | 246 | } | 
|  | 247 | pde->pde_users++; | 
|  | 248 | compat_ioctl = pde->proc_fops->compat_ioctl; | 
|  | 249 | spin_unlock(&pde->pde_unload_lock); | 
|  | 250 |  | 
|  | 251 | if (compat_ioctl) | 
|  | 252 | rv = compat_ioctl(file, cmd, arg); | 
|  | 253 |  | 
|  | 254 | pde_users_dec(pde); | 
|  | 255 | return rv; | 
|  | 256 | } | 
|  | 257 | #endif | 
|  | 258 |  | 
|  | 259 | static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma) | 
|  | 260 | { | 
|  | 261 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | 
|  | 262 | int rv = -EIO; | 
|  | 263 | int (*mmap)(struct file *, struct vm_area_struct *); | 
|  | 264 |  | 
|  | 265 | spin_lock(&pde->pde_unload_lock); | 
|  | 266 | if (!pde->proc_fops) { | 
|  | 267 | spin_unlock(&pde->pde_unload_lock); | 
|  | 268 | return rv; | 
|  | 269 | } | 
|  | 270 | pde->pde_users++; | 
|  | 271 | mmap = pde->proc_fops->mmap; | 
|  | 272 | spin_unlock(&pde->pde_unload_lock); | 
|  | 273 |  | 
|  | 274 | if (mmap) | 
|  | 275 | rv = mmap(file, vma); | 
|  | 276 |  | 
|  | 277 | pde_users_dec(pde); | 
|  | 278 | return rv; | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | static int proc_reg_open(struct inode *inode, struct file *file) | 
|  | 282 | { | 
|  | 283 | struct proc_dir_entry *pde = PDE(inode); | 
|  | 284 | int rv = 0; | 
|  | 285 | int (*open)(struct inode *, struct file *); | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 286 | int (*release)(struct inode *, struct file *); | 
|  | 287 | struct pde_opener *pdeo; | 
|  | 288 |  | 
|  | 289 | /* | 
|  | 290 | * What for, you ask? Well, we can have open, rmmod, remove_proc_entry | 
|  | 291 | * sequence. ->release won't be called because ->proc_fops will be | 
|  | 292 | * cleared. Depending on complexity of ->release, consequences vary. | 
|  | 293 | * | 
|  | 294 | * We can't wait for mercy when close will be done for real, it's | 
|  | 295 | * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release | 
|  | 296 | * by hand in remove_proc_entry(). For this, save opener's credentials | 
|  | 297 | * for later. | 
|  | 298 | */ | 
|  | 299 | pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL); | 
|  | 300 | if (!pdeo) | 
|  | 301 | return -ENOMEM; | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 302 |  | 
|  | 303 | spin_lock(&pde->pde_unload_lock); | 
|  | 304 | if (!pde->proc_fops) { | 
|  | 305 | spin_unlock(&pde->pde_unload_lock); | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 306 | kfree(pdeo); | 
| Alexey Dobriyan | 300b994 | 2008-10-03 00:18:52 +0400 | [diff] [blame] | 307 | return -EINVAL; | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 308 | } | 
|  | 309 | pde->pde_users++; | 
|  | 310 | open = pde->proc_fops->open; | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 311 | release = pde->proc_fops->release; | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 312 | spin_unlock(&pde->pde_unload_lock); | 
|  | 313 |  | 
|  | 314 | if (open) | 
|  | 315 | rv = open(inode, file); | 
|  | 316 |  | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 317 | spin_lock(&pde->pde_unload_lock); | 
|  | 318 | if (rv == 0 && release) { | 
|  | 319 | /* To know what to release. */ | 
|  | 320 | pdeo->inode = inode; | 
|  | 321 | pdeo->file = file; | 
|  | 322 | /* Strictly for "too late" ->release in proc_reg_release(). */ | 
|  | 323 | pdeo->release = release; | 
|  | 324 | list_add(&pdeo->lh, &pde->pde_openers); | 
|  | 325 | } else | 
|  | 326 | kfree(pdeo); | 
|  | 327 | __pde_users_dec(pde); | 
|  | 328 | spin_unlock(&pde->pde_unload_lock); | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 329 | return rv; | 
|  | 330 | } | 
|  | 331 |  | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 332 | static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde, | 
|  | 333 | struct inode *inode, struct file *file) | 
|  | 334 | { | 
|  | 335 | struct pde_opener *pdeo; | 
|  | 336 |  | 
|  | 337 | list_for_each_entry(pdeo, &pde->pde_openers, lh) { | 
|  | 338 | if (pdeo->inode == inode && pdeo->file == file) | 
|  | 339 | return pdeo; | 
|  | 340 | } | 
|  | 341 | return NULL; | 
|  | 342 | } | 
|  | 343 |  | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 344 | static int proc_reg_release(struct inode *inode, struct file *file) | 
|  | 345 | { | 
|  | 346 | struct proc_dir_entry *pde = PDE(inode); | 
|  | 347 | int rv = 0; | 
|  | 348 | int (*release)(struct inode *, struct file *); | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 349 | struct pde_opener *pdeo; | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 350 |  | 
|  | 351 | spin_lock(&pde->pde_unload_lock); | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 352 | pdeo = find_pde_opener(pde, inode, file); | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 353 | if (!pde->proc_fops) { | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 354 | /* | 
|  | 355 | * Can't simply exit, __fput() will think that everything is OK, | 
|  | 356 | * and move on to freeing struct file. remove_proc_entry() will | 
|  | 357 | * find slacker in opener's list and will try to do non-trivial | 
|  | 358 | * things with struct file. Therefore, remove opener from list. | 
|  | 359 | * | 
|  | 360 | * But if opener is removed from list, who will ->release it? | 
|  | 361 | */ | 
|  | 362 | if (pdeo) { | 
|  | 363 | list_del(&pdeo->lh); | 
|  | 364 | spin_unlock(&pde->pde_unload_lock); | 
|  | 365 | rv = pdeo->release(inode, file); | 
|  | 366 | kfree(pdeo); | 
|  | 367 | } else | 
|  | 368 | spin_unlock(&pde->pde_unload_lock); | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 369 | return rv; | 
|  | 370 | } | 
|  | 371 | pde->pde_users++; | 
|  | 372 | release = pde->proc_fops->release; | 
| Alexey Dobriyan | 881adb8 | 2008-07-25 01:48:29 -0700 | [diff] [blame] | 373 | if (pdeo) { | 
|  | 374 | list_del(&pdeo->lh); | 
|  | 375 | kfree(pdeo); | 
|  | 376 | } | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 377 | spin_unlock(&pde->pde_unload_lock); | 
|  | 378 |  | 
|  | 379 | if (release) | 
|  | 380 | rv = release(inode, file); | 
|  | 381 |  | 
|  | 382 | pde_users_dec(pde); | 
|  | 383 | return rv; | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | static const struct file_operations proc_reg_file_ops = { | 
|  | 387 | .llseek		= proc_reg_llseek, | 
|  | 388 | .read		= proc_reg_read, | 
|  | 389 | .write		= proc_reg_write, | 
|  | 390 | .poll		= proc_reg_poll, | 
|  | 391 | .unlocked_ioctl	= proc_reg_unlocked_ioctl, | 
|  | 392 | #ifdef CONFIG_COMPAT | 
|  | 393 | .compat_ioctl	= proc_reg_compat_ioctl, | 
|  | 394 | #endif | 
|  | 395 | .mmap		= proc_reg_mmap, | 
|  | 396 | .open		= proc_reg_open, | 
|  | 397 | .release	= proc_reg_release, | 
|  | 398 | }; | 
|  | 399 |  | 
| David Miller | 778f3dd | 2007-07-27 22:58:37 -0700 | [diff] [blame] | 400 | #ifdef CONFIG_COMPAT | 
|  | 401 | static const struct file_operations proc_reg_file_ops_no_compat = { | 
|  | 402 | .llseek		= proc_reg_llseek, | 
|  | 403 | .read		= proc_reg_read, | 
|  | 404 | .write		= proc_reg_write, | 
|  | 405 | .poll		= proc_reg_poll, | 
|  | 406 | .unlocked_ioctl	= proc_reg_unlocked_ioctl, | 
|  | 407 | .mmap		= proc_reg_mmap, | 
|  | 408 | .open		= proc_reg_open, | 
|  | 409 | .release	= proc_reg_release, | 
|  | 410 | }; | 
|  | 411 | #endif | 
|  | 412 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | struct inode *proc_get_inode(struct super_block *sb, unsigned int ino, | 
|  | 414 | struct proc_dir_entry *de) | 
|  | 415 | { | 
|  | 416 | struct inode * inode; | 
|  | 417 |  | 
| David Howells | a1d4aeb | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 418 | inode = iget_locked(sb, ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (!inode) | 
| Alexey Dobriyan | 99b7623 | 2009-03-25 22:48:06 +0300 | [diff] [blame] | 420 | return NULL; | 
| David Howells | a1d4aeb | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 421 | if (inode->i_state & I_NEW) { | 
|  | 422 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | 
|  | 423 | PROC_I(inode)->fd = 0; | 
|  | 424 | PROC_I(inode)->pde = de; | 
| Alexey Dobriyan | 5e971dc | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 425 |  | 
|  | 426 | if (de->mode) { | 
|  | 427 | inode->i_mode = de->mode; | 
|  | 428 | inode->i_uid = de->uid; | 
|  | 429 | inode->i_gid = de->gid; | 
|  | 430 | } | 
|  | 431 | if (de->size) | 
|  | 432 | inode->i_size = de->size; | 
|  | 433 | if (de->nlink) | 
|  | 434 | inode->i_nlink = de->nlink; | 
|  | 435 | if (de->proc_iops) | 
|  | 436 | inode->i_op = de->proc_iops; | 
|  | 437 | if (de->proc_fops) { | 
|  | 438 | if (S_ISREG(inode->i_mode)) { | 
| David Howells | a1d4aeb | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 439 | #ifdef CONFIG_COMPAT | 
| Alexey Dobriyan | 5e971dc | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 440 | if (!de->proc_fops->compat_ioctl) | 
|  | 441 | inode->i_fop = | 
|  | 442 | &proc_reg_file_ops_no_compat; | 
|  | 443 | else | 
| David Howells | a1d4aeb | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 444 | #endif | 
| Alexey Dobriyan | 5e971dc | 2008-04-29 01:01:41 -0700 | [diff] [blame] | 445 | inode->i_fop = &proc_reg_file_ops; | 
|  | 446 | } else { | 
|  | 447 | inode->i_fop = de->proc_fops; | 
| David Howells | a1d4aeb | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 448 | } | 
| Alexey Dobriyan | 786d7e1 | 2007-07-15 23:39:00 -0700 | [diff] [blame] | 449 | } | 
| David Howells | a1d4aeb | 2008-02-07 00:15:45 -0800 | [diff] [blame] | 450 | unlock_new_inode(inode); | 
| Alexey Dobriyan | 99b7623 | 2009-03-25 22:48:06 +0300 | [diff] [blame] | 451 | } else | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 452 | pde_put(de); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | return inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
| Pavel Emelyanov | 07543f5 | 2007-10-18 23:40:08 -0700 | [diff] [blame] | 456 | int proc_fill_super(struct super_block *s) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { | 
|  | 458 | struct inode * root_inode; | 
|  | 459 |  | 
| Linus Torvalds | 92d0328 | 2006-07-15 12:20:05 -0700 | [diff] [blame] | 460 | s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | s->s_blocksize = 1024; | 
|  | 462 | s->s_blocksize_bits = 10; | 
|  | 463 | s->s_magic = PROC_SUPER_MAGIC; | 
|  | 464 | s->s_op = &proc_sops; | 
|  | 465 | s->s_time_gran = 1; | 
|  | 466 |  | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 467 | pde_get(&proc_root); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root); | 
|  | 469 | if (!root_inode) | 
|  | 470 | goto out_no_root; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | root_inode->i_uid = 0; | 
|  | 472 | root_inode->i_gid = 0; | 
|  | 473 | s->s_root = d_alloc_root(root_inode); | 
|  | 474 | if (!s->s_root) | 
|  | 475 | goto out_no_root; | 
|  | 476 | return 0; | 
|  | 477 |  | 
|  | 478 | out_no_root: | 
|  | 479 | printk("proc_read_super: get root inode failed\n"); | 
|  | 480 | iput(root_inode); | 
| Alexey Dobriyan | 135d565 | 2009-12-15 16:45:39 -0800 | [diff] [blame] | 481 | pde_put(&proc_root); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | return -ENOMEM; | 
|  | 483 | } |