Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/proc/inode.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | */ | |
7 | ||
efb1a57d | 8 | #include <linux/cache.h> |
1da177e4 LT |
9 | #include <linux/time.h> |
10 | #include <linux/proc_fs.h> | |
11 | #include <linux/kernel.h> | |
97412950 | 12 | #include <linux/pid_namespace.h> |
1da177e4 LT |
13 | #include <linux/mm.h> |
14 | #include <linux/string.h> | |
15 | #include <linux/stat.h> | |
786d7e16 | 16 | #include <linux/completion.h> |
dd23aae4 | 17 | #include <linux/poll.h> |
87ebdc00 | 18 | #include <linux/printk.h> |
1da177e4 LT |
19 | #include <linux/file.h> |
20 | #include <linux/limits.h> | |
21 | #include <linux/init.h> | |
22 | #include <linux/module.h> | |
9043476f | 23 | #include <linux/sysctl.h> |
97412950 | 24 | #include <linux/seq_file.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
97412950 | 26 | #include <linux/mount.h> |
1da177e4 | 27 | |
7c0f6ba6 | 28 | #include <linux/uaccess.h> |
1da177e4 | 29 | |
fee781e6 | 30 | #include "internal.h" |
1da177e4 | 31 | |
8267952b | 32 | static void proc_evict_inode(struct inode *inode) |
1da177e4 LT |
33 | { |
34 | struct proc_dir_entry *de; | |
dfef6dcd | 35 | struct ctl_table_header *head; |
71448011 | 36 | struct proc_inode *ei = PROC_I(inode); |
1da177e4 | 37 | |
91b0abe3 | 38 | truncate_inode_pages_final(&inode->i_data); |
dbd5768f | 39 | clear_inode(inode); |
fef26658 | 40 | |
99f89551 | 41 | /* Stop tracking associated processes */ |
71448011 | 42 | if (ei->pid) { |
7bc3e6e5 | 43 | proc_pid_evict_inode(ei); |
71448011 EB |
44 | ei->pid = NULL; |
45 | } | |
1da177e4 LT |
46 | |
47 | /* Let go of any associated proc directory entry */ | |
71448011 EB |
48 | de = ei->pde; |
49 | if (de) { | |
135d5655 | 50 | pde_put(de); |
71448011 EB |
51 | ei->pde = NULL; |
52 | } | |
d6cffbbe | 53 | |
71448011 | 54 | head = ei->sysctl; |
dfef6dcd | 55 | if (head) { |
71448011 | 56 | RCU_INIT_POINTER(ei->sysctl, NULL); |
d6cffbbe | 57 | proc_sys_evict_inode(inode, head); |
dfef6dcd | 58 | } |
1da177e4 LT |
59 | } |
60 | ||
efb1a57d | 61 | static struct kmem_cache *proc_inode_cachep __ro_after_init; |
195b8cf0 | 62 | static struct kmem_cache *pde_opener_cache __ro_after_init; |
1da177e4 LT |
63 | |
64 | static struct inode *proc_alloc_inode(struct super_block *sb) | |
65 | { | |
66 | struct proc_inode *ei; | |
1da177e4 | 67 | |
f245e1c1 | 68 | ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
69 | if (!ei) |
70 | return NULL; | |
13b41b09 | 71 | ei->pid = NULL; |
aed7a6c4 | 72 | ei->fd = 0; |
1da177e4 LT |
73 | ei->op.proc_get_link = NULL; |
74 | ei->pde = NULL; | |
9043476f AV |
75 | ei->sysctl = NULL; |
76 | ei->sysctl_entry = NULL; | |
0afa5ca8 | 77 | INIT_HLIST_NODE(&ei->sibling_inodes); |
3d3d35b1 | 78 | ei->ns_ops = NULL; |
230f72e9 | 79 | return &ei->vfs_inode; |
1da177e4 LT |
80 | } |
81 | ||
4aa6b55c | 82 | static void proc_free_inode(struct inode *inode) |
1da177e4 LT |
83 | { |
84 | kmem_cache_free(proc_inode_cachep, PROC_I(inode)); | |
85 | } | |
86 | ||
51cc5068 | 87 | static void init_once(void *foo) |
1da177e4 LT |
88 | { |
89 | struct proc_inode *ei = (struct proc_inode *) foo; | |
90 | ||
a35afb83 | 91 | inode_init_once(&ei->vfs_inode); |
1da177e4 | 92 | } |
20c2df83 | 93 | |
195b8cf0 | 94 | void __init proc_init_kmemcache(void) |
1da177e4 LT |
95 | { |
96 | proc_inode_cachep = kmem_cache_create("proc_inode_cache", | |
97 | sizeof(struct proc_inode), | |
fffb60f9 | 98 | 0, (SLAB_RECLAIM_ACCOUNT| |
5d097056 VD |
99 | SLAB_MEM_SPREAD|SLAB_ACCOUNT| |
100 | SLAB_PANIC), | |
20c2df83 | 101 | init_once); |
195b8cf0 AD |
102 | pde_opener_cache = |
103 | kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0, | |
2acddbe8 | 104 | SLAB_ACCOUNT|SLAB_PANIC, NULL); |
b4884f23 | 105 | proc_dir_entry_cache = kmem_cache_create_usercopy( |
2d6e4e82 AD |
106 | "proc_dir_entry", SIZEOF_PDE, 0, SLAB_PANIC, |
107 | offsetof(struct proc_dir_entry, inline_name), | |
108 | SIZEOF_PDE_INLINE_NAME, NULL); | |
109 | BUILD_BUG_ON(sizeof(struct proc_dir_entry) >= SIZEOF_PDE); | |
1da177e4 LT |
110 | } |
111 | ||
f90f3caf | 112 | void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock) |
26dbc60f EB |
113 | { |
114 | struct inode *inode; | |
115 | struct proc_inode *ei; | |
116 | struct hlist_node *node; | |
080f6276 | 117 | struct super_block *old_sb = NULL; |
26dbc60f EB |
118 | |
119 | rcu_read_lock(); | |
120 | for (;;) { | |
080f6276 | 121 | struct super_block *sb; |
26dbc60f EB |
122 | node = hlist_first_rcu(inodes); |
123 | if (!node) | |
124 | break; | |
125 | ei = hlist_entry(node, struct proc_inode, sibling_inodes); | |
126 | spin_lock(lock); | |
127 | hlist_del_init_rcu(&ei->sibling_inodes); | |
128 | spin_unlock(lock); | |
129 | ||
130 | inode = &ei->vfs_inode; | |
131 | sb = inode->i_sb; | |
080f6276 | 132 | if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active)) |
26dbc60f EB |
133 | continue; |
134 | inode = igrab(inode); | |
135 | rcu_read_unlock(); | |
080f6276 EB |
136 | if (sb != old_sb) { |
137 | if (old_sb) | |
138 | deactivate_super(old_sb); | |
139 | old_sb = sb; | |
140 | } | |
26dbc60f | 141 | if (unlikely(!inode)) { |
26dbc60f EB |
142 | rcu_read_lock(); |
143 | continue; | |
144 | } | |
145 | ||
f90f3caf EB |
146 | if (S_ISDIR(inode->i_mode)) { |
147 | struct dentry *dir = d_find_any_alias(inode); | |
148 | if (dir) { | |
149 | d_invalidate(dir); | |
150 | dput(dir); | |
151 | } | |
152 | } else { | |
153 | struct dentry *dentry; | |
154 | while ((dentry = d_find_alias(inode))) { | |
155 | d_invalidate(dentry); | |
156 | dput(dentry); | |
157 | } | |
158 | } | |
26dbc60f | 159 | iput(inode); |
26dbc60f EB |
160 | |
161 | rcu_read_lock(); | |
162 | } | |
163 | rcu_read_unlock(); | |
080f6276 EB |
164 | if (old_sb) |
165 | deactivate_super(old_sb); | |
26dbc60f EB |
166 | } |
167 | ||
97412950 VK |
168 | static int proc_show_options(struct seq_file *seq, struct dentry *root) |
169 | { | |
fa10fed3 | 170 | struct proc_fs_info *fs_info = proc_sb_info(root->d_sb); |
0499680a | 171 | |
fa10fed3 AG |
172 | if (!gid_eq(fs_info->pid_gid, GLOBAL_ROOT_GID)) |
173 | seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, fs_info->pid_gid)); | |
174 | if (fs_info->hide_pid != HIDEPID_OFF) | |
175 | seq_printf(seq, ",hidepid=%u", fs_info->hide_pid); | |
6814ef2d AG |
176 | if (fs_info->pidonly != PROC_PIDONLY_OFF) |
177 | seq_printf(seq, ",subset=pid"); | |
0499680a | 178 | |
97412950 VK |
179 | return 0; |
180 | } | |
181 | ||
60a3c3a5 | 182 | const struct super_operations proc_sops = { |
1da177e4 | 183 | .alloc_inode = proc_alloc_inode, |
4aa6b55c | 184 | .free_inode = proc_free_inode, |
1da177e4 | 185 | .drop_inode = generic_delete_inode, |
8267952b | 186 | .evict_inode = proc_evict_inode, |
1da177e4 | 187 | .statfs = simple_statfs, |
97412950 | 188 | .show_options = proc_show_options, |
1da177e4 LT |
189 | }; |
190 | ||
866ad9a7 AV |
191 | enum {BIAS = -1U<<31}; |
192 | ||
193 | static inline int use_pde(struct proc_dir_entry *pde) | |
194 | { | |
15b158b4 | 195 | return likely(atomic_inc_unless_negative(&pde->in_use)); |
881adb85 AD |
196 | } |
197 | ||
866ad9a7 | 198 | static void unuse_pde(struct proc_dir_entry *pde) |
881adb85 | 199 | { |
15b158b4 | 200 | if (unlikely(atomic_dec_return(&pde->in_use) == BIAS)) |
05c0ae21 | 201 | complete(pde->pde_unload_completion); |
786d7e16 AD |
202 | } |
203 | ||
2f897424 | 204 | /* pde is locked on entry, unlocked on exit */ |
ca469f35 | 205 | static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo) |
904f394e | 206 | __releases(&pde->pde_unload_lock) |
ca469f35 | 207 | { |
492b2da6 AD |
208 | /* |
209 | * close() (proc_reg_release()) can't delete an entry and proceed: | |
210 | * ->release hook needs to be available at the right moment. | |
211 | * | |
212 | * rmmod (remove_proc_entry() et al) can't delete an entry and proceed: | |
213 | * "struct file" needs to be available at the right moment. | |
214 | * | |
215 | * Therefore, first process to enter this function does ->release() and | |
216 | * signals its completion to the other process which does nothing. | |
217 | */ | |
05c0ae21 | 218 | if (pdeo->closing) { |
ca469f35 | 219 | /* somebody else is doing that, just wait */ |
05c0ae21 AV |
220 | DECLARE_COMPLETION_ONSTACK(c); |
221 | pdeo->c = &c; | |
ca469f35 | 222 | spin_unlock(&pde->pde_unload_lock); |
05c0ae21 | 223 | wait_for_completion(&c); |
ca469f35 AV |
224 | } else { |
225 | struct file *file; | |
2f897424 AD |
226 | struct completion *c; |
227 | ||
f5887c71 | 228 | pdeo->closing = true; |
ca469f35 AV |
229 | spin_unlock(&pde->pde_unload_lock); |
230 | file = pdeo->file; | |
d56c0d45 | 231 | pde->proc_ops->proc_release(file_inode(file), file); |
ca469f35 | 232 | spin_lock(&pde->pde_unload_lock); |
492b2da6 | 233 | /* After ->release. */ |
06a0c417 | 234 | list_del(&pdeo->lh); |
2f897424 AD |
235 | c = pdeo->c; |
236 | spin_unlock(&pde->pde_unload_lock); | |
237 | if (unlikely(c)) | |
238 | complete(c); | |
195b8cf0 | 239 | kmem_cache_free(pde_opener_cache, pdeo); |
05c0ae21 | 240 | } |
ca469f35 AV |
241 | } |
242 | ||
866ad9a7 | 243 | void proc_entry_rundown(struct proc_dir_entry *de) |
786d7e16 | 244 | { |
05c0ae21 | 245 | DECLARE_COMPLETION_ONSTACK(c); |
866ad9a7 | 246 | /* Wait until all existing callers into module are done. */ |
05c0ae21 AV |
247 | de->pde_unload_completion = &c; |
248 | if (atomic_add_return(BIAS, &de->in_use) != BIAS) | |
249 | wait_for_completion(&c); | |
786d7e16 | 250 | |
492b2da6 AD |
251 | /* ->pde_openers list can't grow from now on. */ |
252 | ||
05c0ae21 | 253 | spin_lock(&de->pde_unload_lock); |
866ad9a7 AV |
254 | while (!list_empty(&de->pde_openers)) { |
255 | struct pde_opener *pdeo; | |
866ad9a7 | 256 | pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh); |
ca469f35 | 257 | close_pdeo(de, pdeo); |
2f897424 | 258 | spin_lock(&de->pde_unload_lock); |
866ad9a7 AV |
259 | } |
260 | spin_unlock(&de->pde_unload_lock); | |
786d7e16 AD |
261 | } |
262 | ||
d919b33d AD |
263 | static loff_t pde_lseek(struct proc_dir_entry *pde, struct file *file, loff_t offset, int whence) |
264 | { | |
265 | typeof_member(struct proc_ops, proc_lseek) lseek; | |
266 | ||
267 | lseek = pde->proc_ops->proc_lseek; | |
268 | if (!lseek) | |
269 | lseek = default_llseek; | |
270 | return lseek(file, offset, whence); | |
271 | } | |
272 | ||
866ad9a7 AV |
273 | static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence) |
274 | { | |
275 | struct proc_dir_entry *pde = PDE(file_inode(file)); | |
276 | loff_t rv = -EINVAL; | |
9af27b28 | 277 | |
d919b33d AD |
278 | if (pde_is_permanent(pde)) { |
279 | return pde_lseek(pde, file, offset, whence); | |
280 | } else if (use_pde(pde)) { | |
281 | rv = pde_lseek(pde, file, offset, whence); | |
866ad9a7 AV |
282 | unuse_pde(pde); |
283 | } | |
786d7e16 AD |
284 | return rv; |
285 | } | |
286 | ||
d919b33d AD |
287 | static ssize_t pde_read(struct proc_dir_entry *pde, struct file *file, char __user *buf, size_t count, loff_t *ppos) |
288 | { | |
289 | typeof_member(struct proc_ops, proc_read) read; | |
290 | ||
291 | read = pde->proc_ops->proc_read; | |
292 | if (read) | |
293 | return read(file, buf, count, ppos); | |
294 | return -EIO; | |
295 | } | |
296 | ||
866ad9a7 | 297 | static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
786d7e16 | 298 | { |
496ad9aa | 299 | struct proc_dir_entry *pde = PDE(file_inode(file)); |
786d7e16 | 300 | ssize_t rv = -EIO; |
9af27b28 | 301 | |
d919b33d AD |
302 | if (pde_is_permanent(pde)) { |
303 | return pde_read(pde, file, buf, count, ppos); | |
304 | } else if (use_pde(pde)) { | |
305 | rv = pde_read(pde, file, buf, count, ppos); | |
866ad9a7 | 306 | unuse_pde(pde); |
786d7e16 | 307 | } |
866ad9a7 AV |
308 | return rv; |
309 | } | |
786d7e16 | 310 | |
d919b33d AD |
311 | static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
312 | { | |
313 | typeof_member(struct proc_ops, proc_write) write; | |
314 | ||
315 | write = pde->proc_ops->proc_write; | |
316 | if (write) | |
317 | return write(file, buf, count, ppos); | |
318 | return -EIO; | |
319 | } | |
320 | ||
866ad9a7 AV |
321 | static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
322 | { | |
866ad9a7 AV |
323 | struct proc_dir_entry *pde = PDE(file_inode(file)); |
324 | ssize_t rv = -EIO; | |
9af27b28 | 325 | |
d919b33d AD |
326 | if (pde_is_permanent(pde)) { |
327 | return pde_write(pde, file, buf, count, ppos); | |
328 | } else if (use_pde(pde)) { | |
329 | rv = pde_write(pde, file, buf, count, ppos); | |
866ad9a7 AV |
330 | unuse_pde(pde); |
331 | } | |
786d7e16 AD |
332 | return rv; |
333 | } | |
334 | ||
d919b33d AD |
335 | static __poll_t pde_poll(struct proc_dir_entry *pde, struct file *file, struct poll_table_struct *pts) |
336 | { | |
337 | typeof_member(struct proc_ops, proc_poll) poll; | |
338 | ||
339 | poll = pde->proc_ops->proc_poll; | |
340 | if (poll) | |
341 | return poll(file, pts); | |
342 | return DEFAULT_POLLMASK; | |
343 | } | |
344 | ||
076ccb76 | 345 | static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts) |
786d7e16 | 346 | { |
496ad9aa | 347 | struct proc_dir_entry *pde = PDE(file_inode(file)); |
e6c8adca | 348 | __poll_t rv = DEFAULT_POLLMASK; |
9af27b28 | 349 | |
d919b33d AD |
350 | if (pde_is_permanent(pde)) { |
351 | return pde_poll(pde, file, pts); | |
352 | } else if (use_pde(pde)) { | |
353 | rv = pde_poll(pde, file, pts); | |
866ad9a7 | 354 | unuse_pde(pde); |
786d7e16 | 355 | } |
786d7e16 AD |
356 | return rv; |
357 | } | |
358 | ||
d919b33d AD |
359 | static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg) |
360 | { | |
361 | typeof_member(struct proc_ops, proc_ioctl) ioctl; | |
362 | ||
363 | ioctl = pde->proc_ops->proc_ioctl; | |
364 | if (ioctl) | |
365 | return ioctl(file, cmd, arg); | |
366 | return -ENOTTY; | |
367 | } | |
368 | ||
786d7e16 AD |
369 | static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
370 | { | |
496ad9aa | 371 | struct proc_dir_entry *pde = PDE(file_inode(file)); |
786d7e16 | 372 | long rv = -ENOTTY; |
9af27b28 | 373 | |
d919b33d AD |
374 | if (pde_is_permanent(pde)) { |
375 | return pde_ioctl(pde, file, cmd, arg); | |
376 | } else if (use_pde(pde)) { | |
377 | rv = pde_ioctl(pde, file, cmd, arg); | |
866ad9a7 | 378 | unuse_pde(pde); |
786d7e16 | 379 | } |
786d7e16 AD |
380 | return rv; |
381 | } | |
382 | ||
383 | #ifdef CONFIG_COMPAT | |
d919b33d AD |
384 | static long pde_compat_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg) |
385 | { | |
386 | typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl; | |
387 | ||
388 | compat_ioctl = pde->proc_ops->proc_compat_ioctl; | |
389 | if (compat_ioctl) | |
390 | return compat_ioctl(file, cmd, arg); | |
391 | return -ENOTTY; | |
392 | } | |
393 | ||
786d7e16 AD |
394 | static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
395 | { | |
496ad9aa | 396 | struct proc_dir_entry *pde = PDE(file_inode(file)); |
786d7e16 | 397 | long rv = -ENOTTY; |
d919b33d AD |
398 | if (pde_is_permanent(pde)) { |
399 | return pde_compat_ioctl(pde, file, cmd, arg); | |
400 | } else if (use_pde(pde)) { | |
401 | rv = pde_compat_ioctl(pde, file, cmd, arg); | |
866ad9a7 | 402 | unuse_pde(pde); |
786d7e16 | 403 | } |
786d7e16 AD |
404 | return rv; |
405 | } | |
406 | #endif | |
407 | ||
d919b33d AD |
408 | static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma) |
409 | { | |
410 | typeof_member(struct proc_ops, proc_mmap) mmap; | |
411 | ||
412 | mmap = pde->proc_ops->proc_mmap; | |
413 | if (mmap) | |
414 | return mmap(file, vma); | |
415 | return -EIO; | |
416 | } | |
417 | ||
786d7e16 AD |
418 | static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma) |
419 | { | |
496ad9aa | 420 | struct proc_dir_entry *pde = PDE(file_inode(file)); |
786d7e16 | 421 | int rv = -EIO; |
9af27b28 | 422 | |
d919b33d AD |
423 | if (pde_is_permanent(pde)) { |
424 | return pde_mmap(pde, file, vma); | |
425 | } else if (use_pde(pde)) { | |
426 | rv = pde_mmap(pde, file, vma); | |
866ad9a7 | 427 | unuse_pde(pde); |
786d7e16 | 428 | } |
786d7e16 AD |
429 | return rv; |
430 | } | |
431 | ||
5721cf84 | 432 | static unsigned long |
d919b33d | 433 | pde_get_unmapped_area(struct proc_dir_entry *pde, struct file *file, unsigned long orig_addr, |
5721cf84 HD |
434 | unsigned long len, unsigned long pgoff, |
435 | unsigned long flags) | |
c4fe2448 | 436 | { |
d919b33d | 437 | typeof_member(struct proc_ops, proc_get_unmapped_area) get_area; |
ae5758a1 | 438 | |
d919b33d | 439 | get_area = pde->proc_ops->proc_get_unmapped_area; |
fad1a86e | 440 | #ifdef CONFIG_MMU |
d919b33d AD |
441 | if (!get_area) |
442 | get_area = current->mm->get_unmapped_area; | |
fad1a86e | 443 | #endif |
d919b33d AD |
444 | if (get_area) |
445 | return get_area(file, orig_addr, len, pgoff, flags); | |
446 | return orig_addr; | |
447 | } | |
448 | ||
449 | static unsigned long | |
450 | proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr, | |
451 | unsigned long len, unsigned long pgoff, | |
452 | unsigned long flags) | |
453 | { | |
454 | struct proc_dir_entry *pde = PDE(file_inode(file)); | |
455 | unsigned long rv = -EIO; | |
ae5758a1 | 456 | |
d919b33d AD |
457 | if (pde_is_permanent(pde)) { |
458 | return pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags); | |
459 | } else if (use_pde(pde)) { | |
460 | rv = pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags); | |
c4fe2448 AD |
461 | unuse_pde(pde); |
462 | } | |
463 | return rv; | |
464 | } | |
465 | ||
786d7e16 AD |
466 | static int proc_reg_open(struct inode *inode, struct file *file) |
467 | { | |
6814ef2d | 468 | struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb); |
786d7e16 AD |
469 | struct proc_dir_entry *pde = PDE(inode); |
470 | int rv = 0; | |
d56c0d45 AD |
471 | typeof_member(struct proc_ops, proc_open) open; |
472 | typeof_member(struct proc_ops, proc_release) release; | |
881adb85 AD |
473 | struct pde_opener *pdeo; |
474 | ||
d919b33d AD |
475 | if (pde_is_permanent(pde)) { |
476 | open = pde->proc_ops->proc_open; | |
477 | if (open) | |
478 | rv = open(inode, file); | |
479 | return rv; | |
480 | } | |
481 | ||
6814ef2d AG |
482 | if (fs_info->pidonly == PROC_PIDONLY_ON) |
483 | return -ENOENT; | |
484 | ||
881adb85 | 485 | /* |
492b2da6 AD |
486 | * Ensure that |
487 | * 1) PDE's ->release hook will be called no matter what | |
488 | * either normally by close()/->release, or forcefully by | |
489 | * rmmod/remove_proc_entry. | |
490 | * | |
491 | * 2) rmmod isn't blocked by opening file in /proc and sitting on | |
492 | * the descriptor (including "rmmod foo </proc/foo" scenario). | |
881adb85 | 493 | * |
492b2da6 | 494 | * Save every "struct file" with custom ->release hook. |
881adb85 | 495 | */ |
e7a6e291 | 496 | if (!use_pde(pde)) |
d2857e79 | 497 | return -ENOENT; |
e7a6e291 | 498 | |
d56c0d45 | 499 | release = pde->proc_ops->proc_release; |
e7a6e291 | 500 | if (release) { |
195b8cf0 | 501 | pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL); |
e7a6e291 AD |
502 | if (!pdeo) { |
503 | rv = -ENOMEM; | |
504 | goto out_unuse; | |
505 | } | |
506 | } | |
786d7e16 | 507 | |
d56c0d45 | 508 | open = pde->proc_ops->proc_open; |
786d7e16 AD |
509 | if (open) |
510 | rv = open(inode, file); | |
511 | ||
e7a6e291 AD |
512 | if (release) { |
513 | if (rv == 0) { | |
514 | /* To know what to release. */ | |
515 | pdeo->file = file; | |
516 | pdeo->closing = false; | |
517 | pdeo->c = NULL; | |
518 | spin_lock(&pde->pde_unload_lock); | |
519 | list_add(&pdeo->lh, &pde->pde_openers); | |
520 | spin_unlock(&pde->pde_unload_lock); | |
521 | } else | |
195b8cf0 | 522 | kmem_cache_free(pde_opener_cache, pdeo); |
e7a6e291 | 523 | } |
05c0ae21 | 524 | |
e7a6e291 | 525 | out_unuse: |
05c0ae21 | 526 | unuse_pde(pde); |
786d7e16 AD |
527 | return rv; |
528 | } | |
529 | ||
530 | static int proc_reg_release(struct inode *inode, struct file *file) | |
531 | { | |
532 | struct proc_dir_entry *pde = PDE(inode); | |
881adb85 | 533 | struct pde_opener *pdeo; |
d919b33d AD |
534 | |
535 | if (pde_is_permanent(pde)) { | |
536 | typeof_member(struct proc_ops, proc_release) release; | |
537 | ||
538 | release = pde->proc_ops->proc_release; | |
539 | if (release) { | |
540 | return release(inode, file); | |
541 | } | |
542 | return 0; | |
543 | } | |
544 | ||
786d7e16 | 545 | spin_lock(&pde->pde_unload_lock); |
ca469f35 AV |
546 | list_for_each_entry(pdeo, &pde->pde_openers, lh) { |
547 | if (pdeo->file == file) { | |
548 | close_pdeo(pde, pdeo); | |
2f897424 | 549 | return 0; |
ca469f35 | 550 | } |
881adb85 | 551 | } |
786d7e16 | 552 | spin_unlock(&pde->pde_unload_lock); |
ca469f35 | 553 | return 0; |
786d7e16 AD |
554 | } |
555 | ||
556 | static const struct file_operations proc_reg_file_ops = { | |
557 | .llseek = proc_reg_llseek, | |
558 | .read = proc_reg_read, | |
559 | .write = proc_reg_write, | |
560 | .poll = proc_reg_poll, | |
561 | .unlocked_ioctl = proc_reg_unlocked_ioctl, | |
562 | #ifdef CONFIG_COMPAT | |
563 | .compat_ioctl = proc_reg_compat_ioctl, | |
564 | #endif | |
565 | .mmap = proc_reg_mmap, | |
c4fe2448 | 566 | .get_unmapped_area = proc_reg_get_unmapped_area, |
786d7e16 AD |
567 | .open = proc_reg_open, |
568 | .release = proc_reg_release, | |
569 | }; | |
570 | ||
778f3dd5 DM |
571 | #ifdef CONFIG_COMPAT |
572 | static const struct file_operations proc_reg_file_ops_no_compat = { | |
573 | .llseek = proc_reg_llseek, | |
574 | .read = proc_reg_read, | |
575 | .write = proc_reg_write, | |
576 | .poll = proc_reg_poll, | |
577 | .unlocked_ioctl = proc_reg_unlocked_ioctl, | |
578 | .mmap = proc_reg_mmap, | |
c4fe2448 | 579 | .get_unmapped_area = proc_reg_get_unmapped_area, |
778f3dd5 DM |
580 | .open = proc_reg_open, |
581 | .release = proc_reg_release, | |
582 | }; | |
583 | #endif | |
584 | ||
fceef393 AV |
585 | static void proc_put_link(void *p) |
586 | { | |
587 | unuse_pde(p); | |
588 | } | |
589 | ||
6b255391 | 590 | static const char *proc_get_link(struct dentry *dentry, |
fceef393 AV |
591 | struct inode *inode, |
592 | struct delayed_call *done) | |
7e0e953b | 593 | { |
6b255391 | 594 | struct proc_dir_entry *pde = PDE(inode); |
15b158b4 | 595 | if (!use_pde(pde)) |
7e0e953b | 596 | return ERR_PTR(-EINVAL); |
fceef393 | 597 | set_delayed_call(done, proc_put_link, pde); |
680baacb | 598 | return pde->data; |
7e0e953b AV |
599 | } |
600 | ||
7e0e953b | 601 | const struct inode_operations proc_link_inode_operations = { |
6b255391 | 602 | .get_link = proc_get_link, |
7e0e953b AV |
603 | }; |
604 | ||
6d1b6e4e | 605 | struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de) |
1da177e4 | 606 | { |
51f0885e | 607 | struct inode *inode = new_inode_pseudo(sb); |
1da177e4 | 608 | |
51f0885e LT |
609 | if (inode) { |
610 | inode->i_ino = de->low_ino; | |
078cd827 | 611 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
a1d4aebb | 612 | PROC_I(inode)->pde = de; |
5e971dce | 613 | |
eb6d38d5 EB |
614 | if (is_empty_pde(de)) { |
615 | make_empty_dir_inode(inode); | |
616 | return inode; | |
617 | } | |
5e971dce AD |
618 | if (de->mode) { |
619 | inode->i_mode = de->mode; | |
620 | inode->i_uid = de->uid; | |
621 | inode->i_gid = de->gid; | |
622 | } | |
623 | if (de->size) | |
624 | inode->i_size = de->size; | |
625 | if (de->nlink) | |
bfe86848 | 626 | set_nlink(inode, de->nlink); |
d56c0d45 AD |
627 | |
628 | if (S_ISREG(inode->i_mode)) { | |
629 | inode->i_op = de->proc_iops; | |
630 | inode->i_fop = &proc_reg_file_ops; | |
778f3dd5 | 631 | #ifdef CONFIG_COMPAT |
d56c0d45 AD |
632 | if (!de->proc_ops->proc_compat_ioctl) { |
633 | inode->i_fop = &proc_reg_file_ops_no_compat; | |
778f3dd5 | 634 | } |
d56c0d45 AD |
635 | #endif |
636 | } else if (S_ISDIR(inode->i_mode)) { | |
637 | inode->i_op = de->proc_iops; | |
638 | inode->i_fop = de->proc_dir_ops; | |
639 | } else if (S_ISLNK(inode->i_mode)) { | |
640 | inode->i_op = de->proc_iops; | |
641 | inode->i_fop = NULL; | |
642 | } else | |
643 | BUG(); | |
99b76233 | 644 | } else |
135d5655 | 645 | pde_put(de); |
1da177e4 | 646 | return inode; |
d3d009cb | 647 | } |