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