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 31 truncate_inode_pages(&inode->i_data, 0); 32 end_writeback(inode); 33 34 /* Stop tracking associated processes */ 35 put_pid(PROC_I(inode)->pid); 36 37 /* Let go of any associated proc directory entry */ 38 de = PROC_I(inode)->pde; 39 if (de) 40 pde_put(de); 41 if (PROC_I(inode)->sysctl) 42 sysctl_head_put(PROC_I(inode)->sysctl); 43 } 44 45 struct vfsmount *proc_mnt; 46 47 static struct kmem_cache * proc_inode_cachep; 48 49 static struct inode *proc_alloc_inode(struct super_block *sb) 50 { 51 struct proc_inode *ei; 52 struct inode *inode; 53 54 ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL); 55 if (!ei) 56 return NULL; 57 ei->pid = NULL; 58 ei->fd = 0; 59 ei->op.proc_get_link = NULL; 60 ei->pde = NULL; 61 ei->sysctl = NULL; 62 ei->sysctl_entry = NULL; 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 73 static void init_once(void *foo) 74 { 75 struct proc_inode *ei = (struct proc_inode *) foo; 76 77 inode_init_once(&ei->vfs_inode); 78 } 79 80 void __init proc_init_inodecache(void) 81 { 82 proc_inode_cachep = kmem_cache_create("proc_inode_cache", 83 sizeof(struct proc_inode), 84 0, (SLAB_RECLAIM_ACCOUNT| 85 SLAB_MEM_SPREAD|SLAB_PANIC), 86 init_once); 87 } 88 89 static const struct super_operations proc_sops = { 90 .alloc_inode = proc_alloc_inode, 91 .destroy_inode = proc_destroy_inode, 92 .drop_inode = generic_delete_inode, 93 .evict_inode = proc_evict_inode, 94 .statfs = simple_statfs, 95 }; 96 97 static void __pde_users_dec(struct proc_dir_entry *pde) 98 { 99 pde->pde_users--; 100 if (pde->pde_unload_completion && pde->pde_users == 0) 101 complete(pde->pde_unload_completion); 102 } 103 104 void pde_users_dec(struct proc_dir_entry *pde) 105 { 106 spin_lock(&pde->pde_unload_lock); 107 __pde_users_dec(pde); 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); 193 unsigned int rv = DEFAULT_POLLMASK; 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; 216 long (*ioctl)(struct file *, unsigned int, unsigned long); 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++; 224 ioctl = pde->proc_fops->unlocked_ioctl; 225 spin_unlock(&pde->pde_unload_lock); 226 227 if (ioctl) 228 rv = ioctl(file, cmd, arg); 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 *); 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; 301 302 spin_lock(&pde->pde_unload_lock); 303 if (!pde->proc_fops) { 304 spin_unlock(&pde->pde_unload_lock); 305 kfree(pdeo); 306 return -EINVAL; 307 } 308 pde->pde_users++; 309 open = pde->proc_fops->open; 310 release = pde->proc_fops->release; 311 spin_unlock(&pde->pde_unload_lock); 312 313 if (open) 314 rv = open(inode, file); 315 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); 328 return rv; 329 } 330 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 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 *); 348 struct pde_opener *pdeo; 349 350 spin_lock(&pde->pde_unload_lock); 351 pdeo = find_pde_opener(pde, inode, file); 352 if (!pde->proc_fops) { 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); 368 return rv; 369 } 370 pde->pde_users++; 371 release = pde->proc_fops->release; 372 if (pdeo) { 373 list_del(&pdeo->lh); 374 kfree(pdeo); 375 } 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 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 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 417 inode = iget_locked(sb, ino); 418 if (!inode) 419 return NULL; 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; 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)) { 438 #ifdef CONFIG_COMPAT 439 if (!de->proc_fops->compat_ioctl) 440 inode->i_fop = 441 &proc_reg_file_ops_no_compat; 442 else 443 #endif 444 inode->i_fop = &proc_reg_file_ops; 445 } else { 446 inode->i_fop = de->proc_fops; 447 } 448 } 449 unlock_new_inode(inode); 450 } else 451 pde_put(de); 452 return inode; 453 } 454 455 int proc_fill_super(struct super_block *s) 456 { 457 struct inode * root_inode; 458 459 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC; 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 466 pde_get(&proc_root); 467 root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root); 468 if (!root_inode) 469 goto out_no_root; 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); 480 pde_put(&proc_root); 481 return -ENOMEM; 482 } 483