1 /*
2  * proc/fs/generic.c --- generic routines for the proc-fs
3  *
4  * This file contains generic proc-fs routines for handling
5  * directories and files.
6  *
7  * Copyright (C) 1991, 1992 Linus Torvalds.
8  * Copyright (C) 1997 Theodore Ts'o
9  */
10 
11 #include <asm/uaccess.h>
12 
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/proc_fs.h>
16 #include <linux/stat.h>
17 #define __NO_VERSION__
18 #include <linux/module.h>
19 #include <asm/bitops.h>
20 
21 static ssize_t proc_file_read(struct file * file, char * buf,
22 			      size_t nbytes, loff_t *ppos);
23 static ssize_t proc_file_write(struct file * file, const char * buffer,
24 			       size_t count, loff_t *ppos);
25 static loff_t proc_file_lseek(struct file *, loff_t, int);
26 
proc_match(int len,const char * name,struct proc_dir_entry * de)27 int proc_match(int len, const char *name,struct proc_dir_entry * de)
28 {
29 	if (!de || !de->low_ino)
30 		return 0;
31 	if (de->namelen != len)
32 		return 0;
33 	return !memcmp(name, de->name, len);
34 }
35 
36 static struct file_operations proc_file_operations = {
37 	llseek:		proc_file_lseek,
38 	read:		proc_file_read,
39 	write:		proc_file_write,
40 };
41 
42 #ifndef MIN
43 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
44 #endif
45 
46 /* buffer size is one page but our output routines use some slack for overruns */
47 #define PROC_BLOCK_SIZE	(PAGE_SIZE - 1024)
48 
49 static ssize_t
proc_file_read(struct file * file,char * buf,size_t nbytes,loff_t * ppos)50 proc_file_read(struct file * file, char * buf, size_t nbytes, loff_t *ppos)
51 {
52 	struct inode * inode = file->f_dentry->d_inode;
53 	char 	*page;
54 	ssize_t	retval=0;
55 	int	eof=0;
56 	ssize_t	n, count;
57 	char	*start;
58 	struct proc_dir_entry * dp;
59 	loff_t pos = *ppos;
60 
61 	dp = (struct proc_dir_entry *) inode->u.generic_ip;
62 	if (!(page = (char*) __get_free_page(GFP_KERNEL)))
63 		return -ENOMEM;
64 
65 	while ((nbytes > 0) && !eof)
66 	{
67 		count = MIN(PROC_BLOCK_SIZE, nbytes);
68 		if ((unsigned)pos > INT_MAX)
69 			break;
70 
71 		start = NULL;
72 		if (dp->get_info) {
73 			/*
74 			 * Handle backwards compatibility with the old net
75 			 * routines.
76 			 */
77 			n = dp->get_info(page, &start, pos, count);
78 			if (n < count)
79 				eof = 1;
80 		} else if (dp->read_proc) {
81 			n = dp->read_proc(page, &start, pos,
82 					  count, &eof, dp->data);
83 		} else
84 			break;
85 
86 		if (!start) {
87 			/*
88 			 * For proc files that are less than 4k
89 			 */
90 			start = page + pos;
91 			n -= pos;
92 			if (n <= 0)
93 				break;
94 			if (n > count)
95 				n = count;
96 		}
97 		if (n == 0)
98 			break;	/* End of file */
99 		if (n < 0) {
100 			if (retval == 0)
101 				retval = n;
102 			break;
103 		}
104 
105 		/* This is a hack to allow mangling of file pos independent
106  		 * of actual bytes read.  Simply place the data at page,
107  		 * return the bytes, and set `start' to the desired offset
108  		 * as an unsigned int. - Paul.Russell@rustcorp.com.au
109 		 */
110  		n -= copy_to_user(buf, start < page ? page : start, n);
111 		if (n == 0) {
112 			if (retval == 0)
113 				retval = -EFAULT;
114 			break;
115 		}
116 
117 		pos += start < page ? (long)start : n; /* Move down the file */
118 		nbytes -= n;
119 		buf += n;
120 		retval += n;
121 	}
122 	free_page((unsigned long) page);
123 	*ppos = pos;
124 	return retval;
125 }
126 
127 static ssize_t
proc_file_write(struct file * file,const char * buffer,size_t count,loff_t * ppos)128 proc_file_write(struct file * file, const char * buffer,
129 		size_t count, loff_t *ppos)
130 {
131 	struct inode *inode = file->f_dentry->d_inode;
132 	struct proc_dir_entry * dp;
133 
134 	dp = (struct proc_dir_entry *) inode->u.generic_ip;
135 
136 	if (!dp->write_proc)
137 		return -EIO;
138 
139 	/* FIXME: does this routine need ppos?  probably... */
140 	return dp->write_proc(file, buffer, count, dp->data);
141 }
142 
143 
144 static loff_t
proc_file_lseek(struct file * file,loff_t offset,int origin)145 proc_file_lseek(struct file * file, loff_t offset, int origin)
146 {
147 	long long retval;
148 
149 	switch (origin) {
150 		case 2:
151 			offset += file->f_dentry->d_inode->i_size;
152 			break;
153 		case 1:
154 			offset += file->f_pos;
155 	}
156 	retval = -EINVAL;
157 	if (offset>=0 && (unsigned long long)offset<=file->f_dentry->d_inode->i_sb->s_maxbytes) {
158 		if (offset != file->f_pos) {
159 			file->f_pos = offset;
160 			file->f_reada = 0;
161 		}
162 		retval = offset;
163 	}
164 	/* RED-PEN user can fake an error here by setting offset to >=-4095 && <0  */
165 	return retval;
166 }
167 
168 /*
169  * This function parses a name such as "tty/driver/serial", and
170  * returns the struct proc_dir_entry for "/proc/tty/driver", and
171  * returns "serial" in residual.
172  */
xlate_proc_name(const char * name,struct proc_dir_entry ** ret,const char ** residual)173 static int xlate_proc_name(const char *name,
174 			   struct proc_dir_entry **ret, const char **residual)
175 {
176 	const char     		*cp = name, *next;
177 	struct proc_dir_entry	*de;
178 	int			len;
179 
180 	de = &proc_root;
181 	while (1) {
182 		next = strchr(cp, '/');
183 		if (!next)
184 			break;
185 
186 		len = next - cp;
187 		for (de = de->subdir; de ; de = de->next) {
188 			if (proc_match(len, cp, de))
189 				break;
190 		}
191 		if (!de)
192 			return -ENOENT;
193 		cp += len + 1;
194 	}
195 	*residual = cp;
196 	*ret = de;
197 	return 0;
198 }
199 
200 static unsigned long proc_alloc_map[(PROC_NDYNAMIC + BITS_PER_LONG - 1) / BITS_PER_LONG];
201 
202 spinlock_t proc_alloc_map_lock = SPIN_LOCK_UNLOCKED;
203 
make_inode_number(void)204 static int make_inode_number(void)
205 {
206 	int i;
207 	spin_lock(&proc_alloc_map_lock);
208 	i = find_first_zero_bit(proc_alloc_map, PROC_NDYNAMIC);
209 	if (i < 0 || i >= PROC_NDYNAMIC) {
210 		i = -1;
211 		goto out;
212 	}
213 	set_bit(i, proc_alloc_map);
214 	i += PROC_DYNAMIC_FIRST;
215 out:
216 	spin_unlock(&proc_alloc_map_lock);
217 	return i;
218 }
219 
proc_readlink(struct dentry * dentry,char * buffer,int buflen)220 static int proc_readlink(struct dentry *dentry, char *buffer, int buflen)
221 {
222 	char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
223 	return vfs_readlink(dentry, buffer, buflen, s);
224 }
225 
proc_follow_link(struct dentry * dentry,struct nameidata * nd)226 static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
227 {
228 	char *s=((struct proc_dir_entry *)dentry->d_inode->u.generic_ip)->data;
229 	return vfs_follow_link(nd, s);
230 }
231 
232 static struct inode_operations proc_link_inode_operations = {
233 	readlink:	proc_readlink,
234 	follow_link:	proc_follow_link,
235 };
236 
237 /*
238  * As some entries in /proc are volatile, we want to
239  * get rid of unused dentries.  This could be made
240  * smarter: we could keep a "volatile" flag in the
241  * inode to indicate which ones to keep.
242  */
proc_delete_dentry(struct dentry * dentry)243 static int proc_delete_dentry(struct dentry * dentry)
244 {
245 	return 1;
246 }
247 
248 static struct dentry_operations proc_dentry_operations =
249 {
250 	d_delete:	proc_delete_dentry,
251 };
252 
253 /*
254  * Don't create negative dentries here, return -ENOENT by hand
255  * instead.
256  */
proc_lookup(struct inode * dir,struct dentry * dentry)257 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry)
258 {
259 	struct inode *inode;
260 	struct proc_dir_entry * de;
261 	int error;
262 
263 	error = -ENOENT;
264 	inode = NULL;
265 	de = (struct proc_dir_entry *) dir->u.generic_ip;
266 	if (de) {
267 		for (de = de->subdir; de ; de = de->next) {
268 			if (!de || !de->low_ino)
269 				continue;
270 			if (de->namelen != dentry->d_name.len)
271 				continue;
272 			if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
273 				int ino = de->low_ino;
274 				error = -EINVAL;
275 				inode = proc_get_inode(dir->i_sb, ino, de);
276 				break;
277 			}
278 		}
279 	}
280 
281 	if (inode) {
282 		dentry->d_op = &proc_dentry_operations;
283 		d_add(dentry, inode);
284 		return NULL;
285 	}
286 	return ERR_PTR(error);
287 }
288 
289 /*
290  * This returns non-zero if at EOF, so that the /proc
291  * root directory can use this and check if it should
292  * continue with the <pid> entries..
293  *
294  * Note that the VFS-layer doesn't care about the return
295  * value of the readdir() call, as long as it's non-negative
296  * for success..
297  */
proc_readdir(struct file * filp,void * dirent,filldir_t filldir)298 int proc_readdir(struct file * filp,
299 	void * dirent, filldir_t filldir)
300 {
301 	struct proc_dir_entry * de;
302 	unsigned int ino;
303 	int i;
304 	struct inode *inode = filp->f_dentry->d_inode;
305 
306 	ino = inode->i_ino;
307 	de = (struct proc_dir_entry *) inode->u.generic_ip;
308 	if (!de)
309 		return -EINVAL;
310 	i = filp->f_pos;
311 	switch (i) {
312 		case 0:
313 			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
314 				return 0;
315 			i++;
316 			filp->f_pos++;
317 			/* fall through */
318 		case 1:
319 			if (filldir(dirent, "..", 2, i,
320 				    filp->f_dentry->d_parent->d_inode->i_ino,
321 				    DT_DIR) < 0)
322 				return 0;
323 			i++;
324 			filp->f_pos++;
325 			/* fall through */
326 		default:
327 			de = de->subdir;
328 			i -= 2;
329 			for (;;) {
330 				if (!de)
331 					return 1;
332 				if (!i)
333 					break;
334 				de = de->next;
335 				i--;
336 			}
337 
338 			do {
339 				if (filldir(dirent, de->name, de->namelen, filp->f_pos,
340 					    de->low_ino, de->mode >> 12) < 0)
341 					return 0;
342 				filp->f_pos++;
343 				de = de->next;
344 			} while (de);
345 	}
346 	return 1;
347 }
348 
349 /*
350  * These are the generic /proc directory operations. They
351  * use the in-memory "struct proc_dir_entry" tree to parse
352  * the /proc directory.
353  */
354 static struct file_operations proc_dir_operations = {
355 	read:			generic_read_dir,
356 	readdir:		proc_readdir,
357 };
358 
359 /*
360  * proc directories can do almost nothing..
361  */
362 static struct inode_operations proc_dir_inode_operations = {
363 	lookup:		proc_lookup,
364 };
365 
proc_register(struct proc_dir_entry * dir,struct proc_dir_entry * dp)366 static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
367 {
368 	int	i;
369 
370 	i = make_inode_number();
371 	if (i < 0)
372 		return -EAGAIN;
373 	dp->low_ino = i;
374 	dp->next = dir->subdir;
375 	dp->parent = dir;
376 	dir->subdir = dp;
377 	if (S_ISDIR(dp->mode)) {
378 		if (dp->proc_iops == NULL) {
379 			dp->proc_fops = &proc_dir_operations;
380 			dp->proc_iops = &proc_dir_inode_operations;
381 		}
382 		dir->nlink++;
383 	} else if (S_ISLNK(dp->mode)) {
384 		if (dp->proc_iops == NULL)
385 			dp->proc_iops = &proc_link_inode_operations;
386 	} else if (S_ISREG(dp->mode)) {
387 		if (dp->proc_fops == NULL)
388 			dp->proc_fops = &proc_file_operations;
389 	}
390 	return 0;
391 }
392 
393 /*
394  * Kill an inode that got unregistered..
395  */
proc_kill_inodes(struct proc_dir_entry * de)396 static void proc_kill_inodes(struct proc_dir_entry *de)
397 {
398 	struct list_head *p;
399 	struct super_block *sb = proc_mnt->mnt_sb;
400 
401 	/*
402 	 * Actually it's a partial revoke().
403 	 */
404 	file_list_lock();
405 	for (p = sb->s_files.next; p != &sb->s_files; p = p->next) {
406 		struct file * filp = list_entry(p, struct file, f_list);
407 		struct dentry * dentry = filp->f_dentry;
408 		struct inode * inode;
409 		struct file_operations *fops;
410 
411 		if (dentry->d_op != &proc_dentry_operations)
412 			continue;
413 		inode = dentry->d_inode;
414 		if (inode->u.generic_ip != de)
415 			continue;
416 		fops = filp->f_op;
417 		filp->f_op = NULL;
418 		fops_put(fops);
419 	}
420 	file_list_unlock();
421 }
422 
proc_create(struct proc_dir_entry ** parent,const char * name,mode_t mode,nlink_t nlink)423 static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
424 					  const char *name,
425 					  mode_t mode,
426 					  nlink_t nlink)
427 {
428 	struct proc_dir_entry *ent = NULL;
429 	const char *fn = name;
430 	int len;
431 
432 	/* make sure name is valid */
433 	if (!name || !strlen(name)) goto out;
434 
435 	if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
436 		goto out;
437 	len = strlen(fn);
438 
439 	ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
440 	if (!ent) goto out;
441 
442 	memset(ent, 0, sizeof(struct proc_dir_entry));
443 	memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
444 	ent->name = ((char *) ent) + sizeof(*ent);
445 	ent->namelen = len;
446 	ent->mode = mode;
447 	ent->nlink = nlink;
448  out:
449 	return ent;
450 }
451 
proc_symlink(const char * name,struct proc_dir_entry * parent,const char * dest)452 struct proc_dir_entry *proc_symlink(const char *name,
453 		struct proc_dir_entry *parent, const char *dest)
454 {
455 	struct proc_dir_entry *ent;
456 
457 	ent = proc_create(&parent,name,
458 			  (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
459 
460 	if (ent) {
461 		ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
462 		if (ent->data) {
463 			strcpy((char*)ent->data,dest);
464 			if (proc_register(parent, ent) < 0) {
465 				kfree(ent->data);
466 				kfree(ent);
467 				ent = NULL;
468 			}
469 		} else {
470 			kfree(ent);
471 			ent = NULL;
472 		}
473 	}
474 	return ent;
475 }
476 
proc_mknod(const char * name,mode_t mode,struct proc_dir_entry * parent,kdev_t rdev)477 struct proc_dir_entry *proc_mknod(const char *name, mode_t mode,
478 		struct proc_dir_entry *parent, kdev_t rdev)
479 {
480 	struct proc_dir_entry *ent;
481 
482 	ent = proc_create(&parent,name,mode,1);
483 	if (ent) {
484 		ent->rdev = rdev;
485 		if (proc_register(parent, ent) < 0) {
486 			kfree(ent);
487 			ent = NULL;
488 		}
489 	}
490 	return ent;
491 }
492 
proc_mkdir_mode(const char * name,mode_t mode,struct proc_dir_entry * parent)493 struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
494 		struct proc_dir_entry *parent)
495 {
496 	struct proc_dir_entry *ent;
497 
498 	ent = proc_create(&parent, name, S_IFDIR | mode, 2);
499 	if (ent) {
500 		ent->proc_fops = &proc_dir_operations;
501 		ent->proc_iops = &proc_dir_inode_operations;
502 
503 		if (proc_register(parent, ent) < 0) {
504 			kfree(ent);
505 			ent = NULL;
506 		}
507 	}
508 	return ent;
509 }
510 
proc_mkdir(const char * name,struct proc_dir_entry * parent)511 struct proc_dir_entry *proc_mkdir(const char *name,
512 		struct proc_dir_entry *parent)
513 {
514 	return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
515 }
516 
create_proc_entry(const char * name,mode_t mode,struct proc_dir_entry * parent)517 struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
518 					 struct proc_dir_entry *parent)
519 {
520 	struct proc_dir_entry *ent;
521 	nlink_t nlink;
522 
523 	if (S_ISDIR(mode)) {
524 		if ((mode & S_IALLUGO) == 0)
525 			mode |= S_IRUGO | S_IXUGO;
526 		nlink = 2;
527 	} else {
528 		if ((mode & S_IFMT) == 0)
529 			mode |= S_IFREG;
530 		if ((mode & S_IALLUGO) == 0)
531 			mode |= S_IRUGO;
532 		nlink = 1;
533 	}
534 
535 	ent = proc_create(&parent,name,mode,nlink);
536 	if (ent) {
537 		if (S_ISDIR(mode)) {
538 			ent->proc_fops = &proc_dir_operations;
539 			ent->proc_iops = &proc_dir_inode_operations;
540 		}
541 		if (proc_register(parent, ent) < 0) {
542 			kfree(ent);
543 			ent = NULL;
544 		}
545 	}
546 	return ent;
547 }
548 
free_proc_entry(struct proc_dir_entry * de)549 void free_proc_entry(struct proc_dir_entry *de)
550 {
551 	int ino = de->low_ino;
552 
553 	if (ino < PROC_DYNAMIC_FIRST ||
554 	    ino >= PROC_DYNAMIC_FIRST+PROC_NDYNAMIC)
555 		return;
556 	if (S_ISLNK(de->mode) && de->data)
557 		kfree(de->data);
558 	kfree(de);
559 }
560 
561 /*
562  * Remove a /proc entry and free it if it's not currently in use.
563  * If it is in use, we set the 'deleted' flag.
564  */
remove_proc_entry(const char * name,struct proc_dir_entry * parent)565 void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
566 {
567 	struct proc_dir_entry **p;
568 	struct proc_dir_entry *de;
569 	const char *fn = name;
570 	int len;
571 
572 	if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
573 		goto out;
574 	len = strlen(fn);
575 	for (p = &parent->subdir; *p; p=&(*p)->next ) {
576 		if (!proc_match(len, fn, *p))
577 			continue;
578 		de = *p;
579 		*p = de->next;
580 		de->next = NULL;
581 		if (S_ISDIR(de->mode))
582 			parent->nlink--;
583 		clear_bit(de->low_ino - PROC_DYNAMIC_FIRST,
584 			  proc_alloc_map);
585 		proc_kill_inodes(de);
586 		de->nlink = 0;
587 		if (!atomic_read(&de->count))
588 			free_proc_entry(de);
589 		else {
590 			de->deleted = 1;
591 			printk("remove_proc_entry: %s/%s busy, count=%d\n",
592 				parent->name, de->name, atomic_read(&de->count));
593 		}
594 		break;
595 	}
596 out:
597 	return;
598 }
599