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