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