• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/fs/proc/
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 <linux/errno.h>
12#include <linux/time.h>
13#include <linux/proc_fs.h>
14#include <linux/stat.h>
15#include <linux/mm.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/mount.h>
19#include <linux/init.h>
20#include <linux/idr.h>
21#include <linux/namei.h>
22#include <linux/bitops.h>
23#include <linux/spinlock.h>
24#include <linux/completion.h>
25#include <asm/uaccess.h>
26
27#include "internal.h"
28
29DEFINE_SPINLOCK(proc_subdir_lock);
30
31static int proc_match(int len, const char *name, struct proc_dir_entry *de)
32{
33	if (de->namelen != len)
34		return 0;
35	return !memcmp(name, de->name, len);
36}
37
38/* buffer size is one page but our output routines use some slack for overruns */
39#define PROC_BLOCK_SIZE	(PAGE_SIZE - 1024)
40
41static ssize_t
42__proc_file_read(struct file *file, char __user *buf, size_t nbytes,
43	       loff_t *ppos)
44{
45	struct inode * inode = file->f_path.dentry->d_inode;
46	char 	*page;
47	ssize_t	retval=0;
48	int	eof=0;
49	ssize_t	n, count;
50	char	*start;
51	struct proc_dir_entry * dp;
52	unsigned long long pos;
53
54	/*
55	 * Gaah, please just use "seq_file" instead. The legacy /proc
56	 * interfaces cut loff_t down to off_t for reads, and ignore
57	 * the offset entirely for writes..
58	 */
59	pos = *ppos;
60	if (pos > MAX_NON_LFS)
61		return 0;
62	if (nbytes > MAX_NON_LFS - pos)
63		nbytes = MAX_NON_LFS - pos;
64
65	dp = PDE(inode);
66	if (!(page = (char*) __get_free_page(GFP_TEMPORARY)))
67		return -ENOMEM;
68
69	while ((nbytes > 0) && !eof) {
70		count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
71
72		start = NULL;
73		if (dp->read_proc) {
74			/*
75			 * How to be a proc read function
76			 * ------------------------------
77			 * Prototype:
78			 *    int f(char *buffer, char **start, off_t offset,
79			 *          int count, int *peof, void *dat)
80			 *
81			 * Assume that the buffer is "count" bytes in size.
82			 *
83			 * If you know you have supplied all the data you
84			 * have, set *peof.
85			 *
86			 * You have three ways to return data:
87			 * 0) Leave *start = NULL.  (This is the default.)
88			 *    Put the data of the requested offset at that
89			 *    offset within the buffer.  Return the number (n)
90			 *    of bytes there are from the beginning of the
91			 *    buffer up to the last byte of data.  If the
92			 *    number of supplied bytes (= n - offset) is
93			 *    greater than zero and you didn't signal eof
94			 *    and the reader is prepared to take more data
95			 *    you will be called again with the requested
96			 *    offset advanced by the number of bytes
97			 *    absorbed.  This interface is useful for files
98			 *    no larger than the buffer.
99			 * 1) Set *start = an unsigned long value less than
100			 *    the buffer address but greater than zero.
101			 *    Put the data of the requested offset at the
102			 *    beginning of the buffer.  Return the number of
103			 *    bytes of data placed there.  If this number is
104			 *    greater than zero and you didn't signal eof
105			 *    and the reader is prepared to take more data
106			 *    you will be called again with the requested
107			 *    offset advanced by *start.  This interface is
108			 *    useful when you have a large file consisting
109			 *    of a series of blocks which you want to count
110			 *    and return as wholes.
111			 *    (Hack by Paul.Russell@rustcorp.com.au)
112			 * 2) Set *start = an address within the buffer.
113			 *    Put the data of the requested offset at *start.
114			 *    Return the number of bytes of data placed there.
115			 *    If this number is greater than zero and you
116			 *    didn't signal eof and the reader is prepared to
117			 *    take more data you will be called again with the
118			 *    requested offset advanced by the number of bytes
119			 *    absorbed.
120			 */
121			n = dp->read_proc(page, &start, *ppos,
122					  count, &eof, dp->data);
123		} else
124			break;
125
126		if (n == 0)   /* end of file */
127			break;
128		if (n < 0) {  /* error */
129			if (retval == 0)
130				retval = n;
131			break;
132		}
133
134		if (start == NULL) {
135			if (n > PAGE_SIZE) {
136				printk(KERN_ERR
137				       "proc_file_read: Apparent buffer overflow!\n");
138				n = PAGE_SIZE;
139			}
140			n -= *ppos;
141			if (n <= 0)
142				break;
143			if (n > count)
144				n = count;
145			start = page + *ppos;
146		} else if (start < page) {
147			if (n > PAGE_SIZE) {
148				printk(KERN_ERR
149				       "proc_file_read: Apparent buffer overflow!\n");
150				n = PAGE_SIZE;
151			}
152			if (n > count) {
153				/*
154				 * Don't reduce n because doing so might
155				 * cut off part of a data block.
156				 */
157				printk(KERN_WARNING
158				       "proc_file_read: Read count exceeded\n");
159			}
160		} else /* start >= page */ {
161			unsigned long startoff = (unsigned long)(start - page);
162			if (n > (PAGE_SIZE - startoff)) {
163				printk(KERN_ERR
164				       "proc_file_read: Apparent buffer overflow!\n");
165				n = PAGE_SIZE - startoff;
166			}
167			if (n > count)
168				n = count;
169		}
170
171 		n -= copy_to_user(buf, start < page ? page : start, n);
172		if (n == 0) {
173			if (retval == 0)
174				retval = -EFAULT;
175			break;
176		}
177
178		*ppos += start < page ? (unsigned long)start : n;
179		nbytes -= n;
180		buf += n;
181		retval += n;
182	}
183	free_page((unsigned long) page);
184	return retval;
185}
186
187static ssize_t
188proc_file_read(struct file *file, char __user *buf, size_t nbytes,
189	       loff_t *ppos)
190{
191	struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
192	ssize_t rv = -EIO;
193
194	spin_lock(&pde->pde_unload_lock);
195	if (!pde->proc_fops) {
196		spin_unlock(&pde->pde_unload_lock);
197		return rv;
198	}
199	pde->pde_users++;
200	spin_unlock(&pde->pde_unload_lock);
201
202	rv = __proc_file_read(file, buf, nbytes, ppos);
203
204	pde_users_dec(pde);
205	return rv;
206}
207
208static ssize_t
209proc_file_write(struct file *file, const char __user *buffer,
210		size_t count, loff_t *ppos)
211{
212	struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
213	ssize_t rv = -EIO;
214
215	if (pde->write_proc) {
216		spin_lock(&pde->pde_unload_lock);
217		if (!pde->proc_fops) {
218			spin_unlock(&pde->pde_unload_lock);
219			return rv;
220		}
221		pde->pde_users++;
222		spin_unlock(&pde->pde_unload_lock);
223
224		rv = pde->write_proc(file, buffer, count, pde->data);
225		pde_users_dec(pde);
226	}
227	return rv;
228}
229
230
231static loff_t
232proc_file_lseek(struct file *file, loff_t offset, int orig)
233{
234	loff_t retval = -EINVAL;
235	switch (orig) {
236	case 1:
237		offset += file->f_pos;
238	/* fallthrough */
239	case 0:
240		if (offset < 0 || offset > MAX_NON_LFS)
241			break;
242		file->f_pos = retval = offset;
243	}
244	return retval;
245}
246
247static const struct file_operations proc_file_operations = {
248	.llseek		= proc_file_lseek,
249	.read		= proc_file_read,
250	.write		= proc_file_write,
251};
252
253static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
254{
255	struct inode *inode = dentry->d_inode;
256	struct proc_dir_entry *de = PDE(inode);
257	int error;
258
259	error = inode_change_ok(inode, iattr);
260	if (error)
261		return error;
262
263	if ((iattr->ia_valid & ATTR_SIZE) &&
264	    iattr->ia_size != i_size_read(inode)) {
265		error = vmtruncate(inode, iattr->ia_size);
266		if (error)
267			return error;
268	}
269
270	setattr_copy(inode, iattr);
271	mark_inode_dirty(inode);
272
273	de->uid = inode->i_uid;
274	de->gid = inode->i_gid;
275	de->mode = inode->i_mode;
276	return 0;
277}
278
279static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
280			struct kstat *stat)
281{
282	struct inode *inode = dentry->d_inode;
283	struct proc_dir_entry *de = PROC_I(inode)->pde;
284	if (de && de->nlink)
285		inode->i_nlink = de->nlink;
286
287	generic_fillattr(inode, stat);
288	return 0;
289}
290
291static const struct inode_operations proc_file_inode_operations = {
292	.setattr	= proc_notify_change,
293};
294
295/*
296 * This function parses a name such as "tty/driver/serial", and
297 * returns the struct proc_dir_entry for "/proc/tty/driver", and
298 * returns "serial" in residual.
299 */
300static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
301			     const char **residual)
302{
303	const char     		*cp = name, *next;
304	struct proc_dir_entry	*de;
305	int			len;
306
307	de = *ret;
308	if (!de)
309		de = &proc_root;
310
311	while (1) {
312		next = strchr(cp, '/');
313		if (!next)
314			break;
315
316		len = next - cp;
317		for (de = de->subdir; de ; de = de->next) {
318			if (proc_match(len, cp, de))
319				break;
320		}
321		if (!de) {
322			WARN(1, "name '%s'\n", name);
323			return -ENOENT;
324		}
325		cp += len + 1;
326	}
327	*residual = cp;
328	*ret = de;
329	return 0;
330}
331
332static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
333			   const char **residual)
334{
335	int rv;
336
337	spin_lock(&proc_subdir_lock);
338	rv = __xlate_proc_name(name, ret, residual);
339	spin_unlock(&proc_subdir_lock);
340	return rv;
341}
342
343static DEFINE_IDA(proc_inum_ida);
344static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
345
346#define PROC_DYNAMIC_FIRST 0xF0000000U
347
348/*
349 * Return an inode number between PROC_DYNAMIC_FIRST and
350 * 0xffffffff, or zero on failure.
351 */
352static unsigned int get_inode_number(void)
353{
354	unsigned int i;
355	int error;
356
357retry:
358	if (ida_pre_get(&proc_inum_ida, GFP_KERNEL) == 0)
359		return 0;
360
361	spin_lock(&proc_inum_lock);
362	error = ida_get_new(&proc_inum_ida, &i);
363	spin_unlock(&proc_inum_lock);
364	if (error == -EAGAIN)
365		goto retry;
366	else if (error)
367		return 0;
368
369	if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
370		spin_lock(&proc_inum_lock);
371		ida_remove(&proc_inum_ida, i);
372		spin_unlock(&proc_inum_lock);
373		return 0;
374	}
375	return PROC_DYNAMIC_FIRST + i;
376}
377
378static void release_inode_number(unsigned int inum)
379{
380	spin_lock(&proc_inum_lock);
381	ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
382	spin_unlock(&proc_inum_lock);
383}
384
385static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
386{
387	nd_set_link(nd, PDE(dentry->d_inode)->data);
388	return NULL;
389}
390
391static const struct inode_operations proc_link_inode_operations = {
392	.readlink	= generic_readlink,
393	.follow_link	= proc_follow_link,
394};
395
396/*
397 * As some entries in /proc are volatile, we want to
398 * get rid of unused dentries.  This could be made
399 * smarter: we could keep a "volatile" flag in the
400 * inode to indicate which ones to keep.
401 */
402static int proc_delete_dentry(struct dentry * dentry)
403{
404	return 1;
405}
406
407static const struct dentry_operations proc_dentry_operations =
408{
409	.d_delete	= proc_delete_dentry,
410};
411
412/*
413 * Don't create negative dentries here, return -ENOENT by hand
414 * instead.
415 */
416struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
417		struct dentry *dentry)
418{
419	struct inode *inode = NULL;
420	int error = -ENOENT;
421
422	spin_lock(&proc_subdir_lock);
423	for (de = de->subdir; de ; de = de->next) {
424		if (de->namelen != dentry->d_name.len)
425			continue;
426		if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
427			unsigned int ino;
428
429			ino = de->low_ino;
430			pde_get(de);
431			spin_unlock(&proc_subdir_lock);
432			error = -EINVAL;
433			inode = proc_get_inode(dir->i_sb, ino, de);
434			goto out_unlock;
435		}
436	}
437	spin_unlock(&proc_subdir_lock);
438out_unlock:
439
440	if (inode) {
441		dentry->d_op = &proc_dentry_operations;
442		d_add(dentry, inode);
443		return NULL;
444	}
445	if (de)
446		pde_put(de);
447	return ERR_PTR(error);
448}
449
450struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
451		struct nameidata *nd)
452{
453	return proc_lookup_de(PDE(dir), dir, dentry);
454}
455
456/*
457 * This returns non-zero if at EOF, so that the /proc
458 * root directory can use this and check if it should
459 * continue with the <pid> entries..
460 *
461 * Note that the VFS-layer doesn't care about the return
462 * value of the readdir() call, as long as it's non-negative
463 * for success..
464 */
465int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
466		filldir_t filldir)
467{
468	unsigned int ino;
469	int i;
470	struct inode *inode = filp->f_path.dentry->d_inode;
471	int ret = 0;
472
473	ino = inode->i_ino;
474	i = filp->f_pos;
475	switch (i) {
476		case 0:
477			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
478				goto out;
479			i++;
480			filp->f_pos++;
481			/* fall through */
482		case 1:
483			if (filldir(dirent, "..", 2, i,
484				    parent_ino(filp->f_path.dentry),
485				    DT_DIR) < 0)
486				goto out;
487			i++;
488			filp->f_pos++;
489			/* fall through */
490		default:
491			spin_lock(&proc_subdir_lock);
492			de = de->subdir;
493			i -= 2;
494			for (;;) {
495				if (!de) {
496					ret = 1;
497					spin_unlock(&proc_subdir_lock);
498					goto out;
499				}
500				if (!i)
501					break;
502				de = de->next;
503				i--;
504			}
505
506			do {
507				struct proc_dir_entry *next;
508
509				/* filldir passes info to user space */
510				pde_get(de);
511				spin_unlock(&proc_subdir_lock);
512				if (filldir(dirent, de->name, de->namelen, filp->f_pos,
513					    de->low_ino, de->mode >> 12) < 0) {
514					pde_put(de);
515					goto out;
516				}
517				spin_lock(&proc_subdir_lock);
518				filp->f_pos++;
519				next = de->next;
520				pde_put(de);
521				de = next;
522			} while (de);
523			spin_unlock(&proc_subdir_lock);
524	}
525	ret = 1;
526out:
527	return ret;
528}
529
530int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
531{
532	struct inode *inode = filp->f_path.dentry->d_inode;
533
534	return proc_readdir_de(PDE(inode), filp, dirent, filldir);
535}
536
537/*
538 * These are the generic /proc directory operations. They
539 * use the in-memory "struct proc_dir_entry" tree to parse
540 * the /proc directory.
541 */
542static const struct file_operations proc_dir_operations = {
543	.llseek			= generic_file_llseek,
544	.read			= generic_read_dir,
545	.readdir		= proc_readdir,
546};
547
548/*
549 * proc directories can do almost nothing..
550 */
551static const struct inode_operations proc_dir_inode_operations = {
552	.lookup		= proc_lookup,
553	.getattr	= proc_getattr,
554	.setattr	= proc_notify_change,
555};
556
557static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
558{
559	unsigned int i;
560	struct proc_dir_entry *tmp;
561
562	i = get_inode_number();
563	if (i == 0)
564		return -EAGAIN;
565	dp->low_ino = i;
566
567	if (S_ISDIR(dp->mode)) {
568		if (dp->proc_iops == NULL) {
569			dp->proc_fops = &proc_dir_operations;
570			dp->proc_iops = &proc_dir_inode_operations;
571		}
572		dir->nlink++;
573	} else if (S_ISLNK(dp->mode)) {
574		if (dp->proc_iops == NULL)
575			dp->proc_iops = &proc_link_inode_operations;
576	} else if (S_ISREG(dp->mode)) {
577		if (dp->proc_fops == NULL)
578			dp->proc_fops = &proc_file_operations;
579		if (dp->proc_iops == NULL)
580			dp->proc_iops = &proc_file_inode_operations;
581	}
582
583	spin_lock(&proc_subdir_lock);
584
585	for (tmp = dir->subdir; tmp; tmp = tmp->next)
586		if (strcmp(tmp->name, dp->name) == 0) {
587			WARN(1, KERN_WARNING "proc_dir_entry '%s/%s' already registered\n",
588				dir->name, dp->name);
589			break;
590		}
591
592	dp->next = dir->subdir;
593	dp->parent = dir;
594	dir->subdir = dp;
595	spin_unlock(&proc_subdir_lock);
596
597	return 0;
598}
599
600static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
601					  const char *name,
602					  mode_t mode,
603					  nlink_t nlink)
604{
605	struct proc_dir_entry *ent = NULL;
606	const char *fn = name;
607	int len;
608
609	/* make sure name is valid */
610	if (!name || !strlen(name)) goto out;
611
612	if (xlate_proc_name(name, parent, &fn) != 0)
613		goto out;
614
615	/* At this point there must not be any '/' characters beyond *fn */
616	if (strchr(fn, '/'))
617		goto out;
618
619	len = strlen(fn);
620
621	ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
622	if (!ent) goto out;
623
624	memset(ent, 0, sizeof(struct proc_dir_entry));
625	memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
626	ent->name = ((char *) ent) + sizeof(*ent);
627	ent->namelen = len;
628	ent->mode = mode;
629	ent->nlink = nlink;
630	atomic_set(&ent->count, 1);
631	ent->pde_users = 0;
632	spin_lock_init(&ent->pde_unload_lock);
633	ent->pde_unload_completion = NULL;
634	INIT_LIST_HEAD(&ent->pde_openers);
635 out:
636	return ent;
637}
638
639struct proc_dir_entry *proc_symlink(const char *name,
640		struct proc_dir_entry *parent, const char *dest)
641{
642	struct proc_dir_entry *ent;
643
644	ent = __proc_create(&parent, name,
645			  (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
646
647	if (ent) {
648		ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
649		if (ent->data) {
650			strcpy((char*)ent->data,dest);
651			if (proc_register(parent, ent) < 0) {
652				kfree(ent->data);
653				kfree(ent);
654				ent = NULL;
655			}
656		} else {
657			kfree(ent);
658			ent = NULL;
659		}
660	}
661	return ent;
662}
663EXPORT_SYMBOL(proc_symlink);
664
665struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
666		struct proc_dir_entry *parent)
667{
668	struct proc_dir_entry *ent;
669
670	ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
671	if (ent) {
672		if (proc_register(parent, ent) < 0) {
673			kfree(ent);
674			ent = NULL;
675		}
676	}
677	return ent;
678}
679
680struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
681		struct proc_dir_entry *parent)
682{
683	struct proc_dir_entry *ent;
684
685	ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
686	if (ent) {
687		ent->data = net;
688		if (proc_register(parent, ent) < 0) {
689			kfree(ent);
690			ent = NULL;
691		}
692	}
693	return ent;
694}
695EXPORT_SYMBOL_GPL(proc_net_mkdir);
696
697struct proc_dir_entry *proc_mkdir(const char *name,
698		struct proc_dir_entry *parent)
699{
700	return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
701}
702EXPORT_SYMBOL(proc_mkdir);
703
704struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
705					 struct proc_dir_entry *parent)
706{
707	struct proc_dir_entry *ent;
708	nlink_t nlink;
709
710	if (S_ISDIR(mode)) {
711		if ((mode & S_IALLUGO) == 0)
712			mode |= S_IRUGO | S_IXUGO;
713		nlink = 2;
714	} else {
715		if ((mode & S_IFMT) == 0)
716			mode |= S_IFREG;
717		if ((mode & S_IALLUGO) == 0)
718			mode |= S_IRUGO;
719		nlink = 1;
720	}
721
722	ent = __proc_create(&parent, name, mode, nlink);
723	if (ent) {
724		if (proc_register(parent, ent) < 0) {
725			kfree(ent);
726			ent = NULL;
727		}
728	}
729	return ent;
730}
731EXPORT_SYMBOL(create_proc_entry);
732
733struct proc_dir_entry *proc_create_data(const char *name, mode_t mode,
734					struct proc_dir_entry *parent,
735					const struct file_operations *proc_fops,
736					void *data)
737{
738	struct proc_dir_entry *pde;
739	nlink_t nlink;
740
741	if (S_ISDIR(mode)) {
742		if ((mode & S_IALLUGO) == 0)
743			mode |= S_IRUGO | S_IXUGO;
744		nlink = 2;
745	} else {
746		if ((mode & S_IFMT) == 0)
747			mode |= S_IFREG;
748		if ((mode & S_IALLUGO) == 0)
749			mode |= S_IRUGO;
750		nlink = 1;
751	}
752
753	pde = __proc_create(&parent, name, mode, nlink);
754	if (!pde)
755		goto out;
756	pde->proc_fops = proc_fops;
757	pde->data = data;
758	if (proc_register(parent, pde) < 0)
759		goto out_free;
760	return pde;
761out_free:
762	kfree(pde);
763out:
764	return NULL;
765}
766EXPORT_SYMBOL(proc_create_data);
767
768static void free_proc_entry(struct proc_dir_entry *de)
769{
770	unsigned int ino = de->low_ino;
771
772	if (ino < PROC_DYNAMIC_FIRST)
773		return;
774
775	release_inode_number(ino);
776
777	if (S_ISLNK(de->mode))
778		kfree(de->data);
779	kfree(de);
780}
781
782void pde_put(struct proc_dir_entry *pde)
783{
784	if (atomic_dec_and_test(&pde->count))
785		free_proc_entry(pde);
786}
787
788/*
789 * Remove a /proc entry and free it if it's not currently in use.
790 */
791void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
792{
793	struct proc_dir_entry **p;
794	struct proc_dir_entry *de = NULL;
795	const char *fn = name;
796	int len;
797
798	spin_lock(&proc_subdir_lock);
799	if (__xlate_proc_name(name, &parent, &fn) != 0) {
800		spin_unlock(&proc_subdir_lock);
801		return;
802	}
803	len = strlen(fn);
804
805	for (p = &parent->subdir; *p; p=&(*p)->next ) {
806		if (proc_match(len, fn, *p)) {
807			de = *p;
808			*p = de->next;
809			de->next = NULL;
810			break;
811		}
812	}
813	spin_unlock(&proc_subdir_lock);
814	if (!de) {
815		WARN(1, "name '%s'\n", name);
816		return;
817	}
818
819	spin_lock(&de->pde_unload_lock);
820	/*
821	 * Stop accepting new callers into module. If you're
822	 * dynamically allocating ->proc_fops, save a pointer somewhere.
823	 */
824	de->proc_fops = NULL;
825	/* Wait until all existing callers into module are done. */
826	if (de->pde_users > 0) {
827		DECLARE_COMPLETION_ONSTACK(c);
828
829		if (!de->pde_unload_completion)
830			de->pde_unload_completion = &c;
831
832		spin_unlock(&de->pde_unload_lock);
833
834		wait_for_completion(de->pde_unload_completion);
835
836		goto continue_removing;
837	}
838	spin_unlock(&de->pde_unload_lock);
839
840continue_removing:
841	spin_lock(&de->pde_unload_lock);
842	while (!list_empty(&de->pde_openers)) {
843		struct pde_opener *pdeo;
844
845		pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
846		list_del(&pdeo->lh);
847		spin_unlock(&de->pde_unload_lock);
848		pdeo->release(pdeo->inode, pdeo->file);
849		kfree(pdeo);
850		spin_lock(&de->pde_unload_lock);
851	}
852	spin_unlock(&de->pde_unload_lock);
853
854	if (S_ISDIR(de->mode))
855		parent->nlink--;
856	de->nlink = 0;
857	WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory "
858			"'%s/%s', leaking at least '%s'\n", __func__,
859			de->parent->name, de->name, de->subdir->name);
860	pde_put(de);
861}
862EXPORT_SYMBOL(remove_proc_entry);
863