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