File: /usr/src/linux/fs/proc/generic.c

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