Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
786d7e16 | 13 | #include <linux/completion.h> |
dd23aae4 | 14 | #include <linux/poll.h> |
1da177e4 LT |
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> | |
20 | ||
21 | #include <asm/system.h> | |
22 | #include <asm/uaccess.h> | |
23 | ||
fee781e6 | 24 | #include "internal.h" |
1da177e4 | 25 | |
7695650a | 26 | struct proc_dir_entry *de_get(struct proc_dir_entry *de) |
1da177e4 LT |
27 | { |
28 | if (de) | |
29 | atomic_inc(&de->count); | |
30 | return de; | |
31 | } | |
32 | ||
33 | /* | |
34 | * Decrements the use count and checks for deferred deletion. | |
35 | */ | |
7695650a | 36 | void de_put(struct proc_dir_entry *de) |
1da177e4 LT |
37 | { |
38 | if (de) { | |
39 | lock_kernel(); | |
40 | if (!atomic_read(&de->count)) { | |
41 | printk("de_put: entry %s already free!\n", de->name); | |
42 | unlock_kernel(); | |
43 | return; | |
44 | } | |
45 | ||
46 | if (atomic_dec_and_test(&de->count)) { | |
47 | if (de->deleted) { | |
48 | printk("de_put: deferred delete of %s\n", | |
49 | de->name); | |
50 | free_proc_entry(de); | |
51 | } | |
52 | } | |
53 | unlock_kernel(); | |
54 | } | |
55 | } | |
56 | ||
57 | /* | |
58 | * Decrement the use count of the proc_dir_entry. | |
59 | */ | |
60 | static void proc_delete_inode(struct inode *inode) | |
61 | { | |
62 | struct proc_dir_entry *de; | |
1da177e4 | 63 | |
fef26658 MF |
64 | truncate_inode_pages(&inode->i_data, 0); |
65 | ||
99f89551 | 66 | /* Stop tracking associated processes */ |
13b41b09 | 67 | put_pid(PROC_I(inode)->pid); |
1da177e4 LT |
68 | |
69 | /* Let go of any associated proc directory entry */ | |
70 | de = PROC_I(inode)->pde; | |
71 | if (de) { | |
72 | if (de->owner) | |
73 | module_put(de->owner); | |
74 | de_put(de); | |
75 | } | |
76 | clear_inode(inode); | |
77 | } | |
78 | ||
79 | struct vfsmount *proc_mnt; | |
80 | ||
81 | static void proc_read_inode(struct inode * inode) | |
82 | { | |
83 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
84 | } | |
85 | ||
e18b890b | 86 | static struct kmem_cache * proc_inode_cachep; |
1da177e4 LT |
87 | |
88 | static struct inode *proc_alloc_inode(struct super_block *sb) | |
89 | { | |
90 | struct proc_inode *ei; | |
91 | struct inode *inode; | |
92 | ||
e94b1766 | 93 | ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
94 | if (!ei) |
95 | return NULL; | |
13b41b09 | 96 | ei->pid = NULL; |
aed7a6c4 | 97 | ei->fd = 0; |
1da177e4 LT |
98 | ei->op.proc_get_link = NULL; |
99 | ei->pde = NULL; | |
100 | inode = &ei->vfs_inode; | |
101 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
102 | return inode; | |
103 | } | |
104 | ||
105 | static void proc_destroy_inode(struct inode *inode) | |
106 | { | |
107 | kmem_cache_free(proc_inode_cachep, PROC_I(inode)); | |
108 | } | |
109 | ||
4ba9b9d0 | 110 | static void init_once(struct kmem_cache * cachep, void *foo) |
1da177e4 LT |
111 | { |
112 | struct proc_inode *ei = (struct proc_inode *) foo; | |
113 | ||
a35afb83 | 114 | inode_init_once(&ei->vfs_inode); |
1da177e4 | 115 | } |
20c2df83 | 116 | |
1da177e4 LT |
117 | int __init proc_init_inodecache(void) |
118 | { | |
119 | proc_inode_cachep = kmem_cache_create("proc_inode_cache", | |
120 | sizeof(struct proc_inode), | |
fffb60f9 PJ |
121 | 0, (SLAB_RECLAIM_ACCOUNT| |
122 | SLAB_MEM_SPREAD), | |
20c2df83 | 123 | init_once); |
1da177e4 LT |
124 | if (proc_inode_cachep == NULL) |
125 | return -ENOMEM; | |
126 | return 0; | |
127 | } | |
128 | ||
129 | static int proc_remount(struct super_block *sb, int *flags, char *data) | |
130 | { | |
131 | *flags |= MS_NODIRATIME; | |
132 | return 0; | |
133 | } | |
134 | ||
ee9b6d61 | 135 | static const struct super_operations proc_sops = { |
1da177e4 LT |
136 | .alloc_inode = proc_alloc_inode, |
137 | .destroy_inode = proc_destroy_inode, | |
138 | .read_inode = proc_read_inode, | |
139 | .drop_inode = generic_delete_inode, | |
140 | .delete_inode = proc_delete_inode, | |
141 | .statfs = simple_statfs, | |
142 | .remount_fs = proc_remount, | |
143 | }; | |
144 | ||
786d7e16 AD |
145 | static void pde_users_dec(struct proc_dir_entry *pde) |
146 | { | |
147 | spin_lock(&pde->pde_unload_lock); | |
148 | pde->pde_users--; | |
149 | if (pde->pde_unload_completion && pde->pde_users == 0) | |
150 | complete(pde->pde_unload_completion); | |
151 | spin_unlock(&pde->pde_unload_lock); | |
152 | } | |
153 | ||
154 | static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence) | |
155 | { | |
156 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
157 | loff_t rv = -EINVAL; | |
158 | loff_t (*llseek)(struct file *, loff_t, int); | |
159 | ||
160 | spin_lock(&pde->pde_unload_lock); | |
161 | /* | |
162 | * remove_proc_entry() is going to delete PDE (as part of module | |
163 | * cleanup sequence). No new callers into module allowed. | |
164 | */ | |
165 | if (!pde->proc_fops) { | |
166 | spin_unlock(&pde->pde_unload_lock); | |
167 | return rv; | |
168 | } | |
169 | /* | |
170 | * Bump refcount so that remove_proc_entry will wail for ->llseek to | |
171 | * complete. | |
172 | */ | |
173 | pde->pde_users++; | |
174 | /* | |
175 | * Save function pointer under lock, to protect against ->proc_fops | |
176 | * NULL'ifying right after ->pde_unload_lock is dropped. | |
177 | */ | |
178 | llseek = pde->proc_fops->llseek; | |
179 | spin_unlock(&pde->pde_unload_lock); | |
180 | ||
181 | if (!llseek) | |
182 | llseek = default_llseek; | |
183 | rv = llseek(file, offset, whence); | |
184 | ||
185 | pde_users_dec(pde); | |
186 | return rv; | |
187 | } | |
188 | ||
189 | static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) | |
190 | { | |
191 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
192 | ssize_t rv = -EIO; | |
193 | ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); | |
194 | ||
195 | spin_lock(&pde->pde_unload_lock); | |
196 | if (!pde->proc_fops) { | |
197 | spin_unlock(&pde->pde_unload_lock); | |
198 | return rv; | |
199 | } | |
200 | pde->pde_users++; | |
201 | read = pde->proc_fops->read; | |
202 | spin_unlock(&pde->pde_unload_lock); | |
203 | ||
204 | if (read) | |
205 | rv = read(file, buf, count, ppos); | |
206 | ||
207 | pde_users_dec(pde); | |
208 | return rv; | |
209 | } | |
210 | ||
211 | static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) | |
212 | { | |
213 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
214 | ssize_t rv = -EIO; | |
215 | ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); | |
216 | ||
217 | spin_lock(&pde->pde_unload_lock); | |
218 | if (!pde->proc_fops) { | |
219 | spin_unlock(&pde->pde_unload_lock); | |
220 | return rv; | |
221 | } | |
222 | pde->pde_users++; | |
223 | write = pde->proc_fops->write; | |
224 | spin_unlock(&pde->pde_unload_lock); | |
225 | ||
226 | if (write) | |
227 | rv = write(file, buf, count, ppos); | |
228 | ||
229 | pde_users_dec(pde); | |
230 | return rv; | |
231 | } | |
232 | ||
233 | static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts) | |
234 | { | |
235 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
dd23aae4 | 236 | unsigned int rv = DEFAULT_POLLMASK; |
786d7e16 AD |
237 | unsigned int (*poll)(struct file *, struct poll_table_struct *); |
238 | ||
239 | spin_lock(&pde->pde_unload_lock); | |
240 | if (!pde->proc_fops) { | |
241 | spin_unlock(&pde->pde_unload_lock); | |
242 | return rv; | |
243 | } | |
244 | pde->pde_users++; | |
245 | poll = pde->proc_fops->poll; | |
246 | spin_unlock(&pde->pde_unload_lock); | |
247 | ||
248 | if (poll) | |
249 | rv = poll(file, pts); | |
250 | ||
251 | pde_users_dec(pde); | |
252 | return rv; | |
253 | } | |
254 | ||
255 | static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
256 | { | |
257 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
258 | long rv = -ENOTTY; | |
259 | long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long); | |
260 | int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long); | |
261 | ||
262 | spin_lock(&pde->pde_unload_lock); | |
263 | if (!pde->proc_fops) { | |
264 | spin_unlock(&pde->pde_unload_lock); | |
265 | return rv; | |
266 | } | |
267 | pde->pde_users++; | |
268 | unlocked_ioctl = pde->proc_fops->unlocked_ioctl; | |
269 | ioctl = pde->proc_fops->ioctl; | |
270 | spin_unlock(&pde->pde_unload_lock); | |
271 | ||
272 | if (unlocked_ioctl) { | |
273 | rv = unlocked_ioctl(file, cmd, arg); | |
274 | if (rv == -ENOIOCTLCMD) | |
275 | rv = -EINVAL; | |
276 | } else if (ioctl) { | |
277 | lock_kernel(); | |
278 | rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg); | |
279 | unlock_kernel(); | |
280 | } | |
281 | ||
282 | pde_users_dec(pde); | |
283 | return rv; | |
284 | } | |
285 | ||
286 | #ifdef CONFIG_COMPAT | |
287 | static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
288 | { | |
289 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
290 | long rv = -ENOTTY; | |
291 | long (*compat_ioctl)(struct file *, unsigned int, unsigned long); | |
292 | ||
293 | spin_lock(&pde->pde_unload_lock); | |
294 | if (!pde->proc_fops) { | |
295 | spin_unlock(&pde->pde_unload_lock); | |
296 | return rv; | |
297 | } | |
298 | pde->pde_users++; | |
299 | compat_ioctl = pde->proc_fops->compat_ioctl; | |
300 | spin_unlock(&pde->pde_unload_lock); | |
301 | ||
302 | if (compat_ioctl) | |
303 | rv = compat_ioctl(file, cmd, arg); | |
304 | ||
305 | pde_users_dec(pde); | |
306 | return rv; | |
307 | } | |
308 | #endif | |
309 | ||
310 | static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma) | |
311 | { | |
312 | struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode); | |
313 | int rv = -EIO; | |
314 | int (*mmap)(struct file *, struct vm_area_struct *); | |
315 | ||
316 | spin_lock(&pde->pde_unload_lock); | |
317 | if (!pde->proc_fops) { | |
318 | spin_unlock(&pde->pde_unload_lock); | |
319 | return rv; | |
320 | } | |
321 | pde->pde_users++; | |
322 | mmap = pde->proc_fops->mmap; | |
323 | spin_unlock(&pde->pde_unload_lock); | |
324 | ||
325 | if (mmap) | |
326 | rv = mmap(file, vma); | |
327 | ||
328 | pde_users_dec(pde); | |
329 | return rv; | |
330 | } | |
331 | ||
332 | static int proc_reg_open(struct inode *inode, struct file *file) | |
333 | { | |
334 | struct proc_dir_entry *pde = PDE(inode); | |
335 | int rv = 0; | |
336 | int (*open)(struct inode *, struct file *); | |
337 | ||
338 | spin_lock(&pde->pde_unload_lock); | |
339 | if (!pde->proc_fops) { | |
340 | spin_unlock(&pde->pde_unload_lock); | |
341 | return rv; | |
342 | } | |
343 | pde->pde_users++; | |
344 | open = pde->proc_fops->open; | |
345 | spin_unlock(&pde->pde_unload_lock); | |
346 | ||
347 | if (open) | |
348 | rv = open(inode, file); | |
349 | ||
350 | pde_users_dec(pde); | |
351 | return rv; | |
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 | ||
360 | spin_lock(&pde->pde_unload_lock); | |
361 | if (!pde->proc_fops) { | |
362 | spin_unlock(&pde->pde_unload_lock); | |
363 | return rv; | |
364 | } | |
365 | pde->pde_users++; | |
366 | release = pde->proc_fops->release; | |
367 | spin_unlock(&pde->pde_unload_lock); | |
368 | ||
369 | if (release) | |
370 | rv = release(inode, file); | |
371 | ||
372 | pde_users_dec(pde); | |
373 | return rv; | |
374 | } | |
375 | ||
376 | static const struct file_operations proc_reg_file_ops = { | |
377 | .llseek = proc_reg_llseek, | |
378 | .read = proc_reg_read, | |
379 | .write = proc_reg_write, | |
380 | .poll = proc_reg_poll, | |
381 | .unlocked_ioctl = proc_reg_unlocked_ioctl, | |
382 | #ifdef CONFIG_COMPAT | |
383 | .compat_ioctl = proc_reg_compat_ioctl, | |
384 | #endif | |
385 | .mmap = proc_reg_mmap, | |
386 | .open = proc_reg_open, | |
387 | .release = proc_reg_release, | |
388 | }; | |
389 | ||
778f3dd5 DM |
390 | #ifdef CONFIG_COMPAT |
391 | static const struct file_operations proc_reg_file_ops_no_compat = { | |
392 | .llseek = proc_reg_llseek, | |
393 | .read = proc_reg_read, | |
394 | .write = proc_reg_write, | |
395 | .poll = proc_reg_poll, | |
396 | .unlocked_ioctl = proc_reg_unlocked_ioctl, | |
397 | .mmap = proc_reg_mmap, | |
398 | .open = proc_reg_open, | |
399 | .release = proc_reg_release, | |
400 | }; | |
401 | #endif | |
402 | ||
1da177e4 LT |
403 | struct inode *proc_get_inode(struct super_block *sb, unsigned int ino, |
404 | struct proc_dir_entry *de) | |
405 | { | |
406 | struct inode * inode; | |
407 | ||
e9543659 KK |
408 | if (de != NULL && !try_module_get(de->owner)) |
409 | goto out_mod; | |
410 | ||
1da177e4 LT |
411 | inode = iget(sb, ino); |
412 | if (!inode) | |
e9543659 KK |
413 | goto out_ino; |
414 | ||
77b14db5 | 415 | PROC_I(inode)->fd = 0; |
1da177e4 LT |
416 | PROC_I(inode)->pde = de; |
417 | if (de) { | |
418 | if (de->mode) { | |
419 | inode->i_mode = de->mode; | |
420 | inode->i_uid = de->uid; | |
421 | inode->i_gid = de->gid; | |
422 | } | |
423 | if (de->size) | |
424 | inode->i_size = de->size; | |
425 | if (de->nlink) | |
426 | inode->i_nlink = de->nlink; | |
1da177e4 LT |
427 | if (de->proc_iops) |
428 | inode->i_op = de->proc_iops; | |
786d7e16 | 429 | if (de->proc_fops) { |
778f3dd5 DM |
430 | if (S_ISREG(inode->i_mode)) { |
431 | #ifdef CONFIG_COMPAT | |
432 | if (!de->proc_fops->compat_ioctl) | |
433 | inode->i_fop = | |
434 | &proc_reg_file_ops_no_compat; | |
435 | else | |
436 | #endif | |
437 | inode->i_fop = &proc_reg_file_ops; | |
438 | } | |
786d7e16 AD |
439 | else |
440 | inode->i_fop = de->proc_fops; | |
441 | } | |
1da177e4 LT |
442 | } |
443 | ||
1da177e4 LT |
444 | return inode; |
445 | ||
e9543659 KK |
446 | out_ino: |
447 | if (de != NULL) | |
448 | module_put(de->owner); | |
449 | out_mod: | |
e9543659 | 450 | return NULL; |
1da177e4 LT |
451 | } |
452 | ||
453 | int proc_fill_super(struct super_block *s, void *data, int silent) | |
454 | { | |
455 | struct inode * root_inode; | |
456 | ||
92d03285 | 457 | s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC; |
1da177e4 LT |
458 | s->s_blocksize = 1024; |
459 | s->s_blocksize_bits = 10; | |
460 | s->s_magic = PROC_SUPER_MAGIC; | |
461 | s->s_op = &proc_sops; | |
462 | s->s_time_gran = 1; | |
463 | ||
7695650a | 464 | de_get(&proc_root); |
1da177e4 LT |
465 | root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root); |
466 | if (!root_inode) | |
467 | goto out_no_root; | |
1da177e4 LT |
468 | root_inode->i_uid = 0; |
469 | root_inode->i_gid = 0; | |
470 | s->s_root = d_alloc_root(root_inode); | |
471 | if (!s->s_root) | |
472 | goto out_no_root; | |
473 | return 0; | |
474 | ||
475 | out_no_root: | |
476 | printk("proc_read_super: get root inode failed\n"); | |
477 | iput(root_inode); | |
7695650a | 478 | de_put(&proc_root); |
1da177e4 LT |
479 | return -ENOMEM; |
480 | } | |
481 | MODULE_LICENSE("GPL"); |