4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc root directory handling functions
9 #include <asm/uaccess.h>
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/proc_fs.h>
14 #include <linux/stat.h>
15 #include <linux/config.h>
16 #include <linux/init.h>
17 #include <asm/bitops.h>
19 #include <linux/kmod.h>
22 #include <linux/zorro.h>
26 * Offset of the first process in the /proc root directory..
28 #define FIRST_PROCESS_ENTRY 256
30 static int proc_root_readdir(struct file
*, void *, filldir_t
);
31 static struct dentry
*proc_root_lookup(struct inode
*,struct dentry
*);
32 static int proc_unlink(struct inode
*, struct dentry
*);
34 static unsigned char proc_alloc_map
[PROC_NDYNAMIC
/ 8] = {0};
37 * These are the generic /proc directory operations. They
38 * use the in-memory "struct proc_dir_entry" tree to parse
39 * the /proc directory.
41 * NOTE! The /proc/scsi directory currently does not correctly
42 * build up the proc_dir_entry tree, and will show up empty.
44 static struct file_operations proc_dir_operations
= {
45 NULL
, /* lseek - default */
46 NULL
, /* read - bad */
47 NULL
, /* write - bad */
48 proc_readdir
, /* readdir */
49 NULL
, /* poll - default */
50 NULL
, /* ioctl - default */
52 NULL
, /* no special open code */
54 NULL
, /* no special release code */
55 NULL
/* can't fsync */
59 * proc directories can do almost nothing..
61 struct inode_operations proc_dir_inode_operations
= {
62 &proc_dir_operations
, /* default net directory file-ops */
64 proc_lookup
, /* lookup */
73 NULL
, /* follow_link */
79 NULL
, /* permission */
85 * /proc dynamic directories now support unlinking
87 struct inode_operations proc_dyna_dir_inode_operations
= {
88 &proc_dir_operations
, /* default proc dir ops */
90 proc_lookup
, /* lookup */
92 proc_unlink
, /* unlink(struct inode *, struct dentry *) */
99 NULL
, /* follow_link */
100 NULL
, /* get_block */
102 NULL
, /* writepage */
103 NULL
, /* flushpage */
105 NULL
, /* permission */
107 NULL
/* revalidate */
111 * The root /proc directory is special, as it has the
112 * <pid> directories. Thus we don't use the generic
113 * directory handling functions for that..
115 static struct file_operations proc_root_operations
= {
116 NULL
, /* lseek - default */
117 NULL
, /* read - bad */
118 NULL
, /* write - bad */
119 proc_root_readdir
, /* readdir */
120 NULL
, /* poll - default */
121 NULL
, /* ioctl - default */
123 NULL
, /* no special open code */
125 NULL
, /* no special release code */
130 * proc root can do almost nothing..
132 static struct inode_operations proc_root_inode_operations
= {
133 &proc_root_operations
, /* default base directory file-ops */
135 proc_root_lookup
, /* lookup */
144 NULL
, /* follow_link */
145 NULL
, /* get_block */
147 NULL
, /* writepage */
148 NULL
, /* flushpage */
150 NULL
, /* permission */
152 NULL
/* revalidate */
156 * This is the root "inode" in the /proc tree..
158 struct proc_dir_entry proc_root
= {
159 PROC_ROOT_INO
, 5, "/proc",
160 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
161 0, &proc_root_inode_operations
,
167 struct proc_dir_entry
*proc_net
, *proc_scsi
, *proc_bus
, *proc_sysvipc
;
170 struct proc_dir_entry proc_mca
= {
172 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
173 0, &proc_dir_inode_operations
,
175 NULL
, &proc_root
, NULL
180 struct proc_dir_entry proc_sys_root
= {
181 PROC_SYS
, 3, "sys", /* inode, name */
182 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0, /* mode, nlink, uid, gid */
183 0, &proc_dir_inode_operations
, /* size, ops */
184 NULL
, NULL
, /* get_info, fill_inode */
186 NULL
, NULL
/* parent, subdir */
190 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
192 static int (*proc_openprom_defreaddir_ptr
)(struct file
*, void *, filldir_t
);
193 static struct dentry
* (*proc_openprom_deflookup_ptr
)(struct inode
*, struct dentry
*);
194 void (*proc_openprom_use
)(struct inode
*, int) = 0;
195 static struct openpromfs_dev
*proc_openprom_devices
= NULL
;
196 static ino_t proc_openpromdev_ino
= PROC_OPENPROMD_FIRST
;
198 struct inode_operations
*
199 proc_openprom_register(int (*readdir
)(struct file
*, void *, filldir_t
),
200 struct dentry
* (*lookup
)(struct inode
*, struct dentry
*),
201 void (*use
)(struct inode
*, int),
202 struct openpromfs_dev
***devices
)
204 proc_openprom_defreaddir_ptr
= (proc_openprom_inode_operations
.default_file_ops
)->readdir
;
205 proc_openprom_deflookup_ptr
= proc_openprom_inode_operations
.lookup
;
206 (proc_openprom_inode_operations
.default_file_ops
)->readdir
= readdir
;
207 proc_openprom_inode_operations
.lookup
= lookup
;
208 proc_openprom_use
= use
;
209 *devices
= &proc_openprom_devices
;
210 return &proc_openprom_inode_operations
;
213 int proc_openprom_regdev(struct openpromfs_dev
*d
)
215 if (proc_openpromdev_ino
== PROC_OPENPROMD_FIRST
+ PROC_NOPENPROMD
)
217 d
->next
= proc_openprom_devices
;
218 d
->inode
= proc_openpromdev_ino
++;
219 proc_openprom_devices
= d
;
223 int proc_openprom_unregdev(struct openpromfs_dev
*d
)
225 if (d
== proc_openprom_devices
) {
226 proc_openprom_devices
= d
->next
;
227 } else if (!proc_openprom_devices
)
230 struct openpromfs_dev
*p
;
232 for (p
= proc_openprom_devices
; p
->next
!= d
&& p
->next
; p
= p
->next
);
233 if (!p
->next
) return -1;
239 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
241 proc_openprom_deregister(void)
243 (proc_openprom_inode_operations
.default_file_ops
)->readdir
= proc_openprom_defreaddir_ptr
;
244 proc_openprom_inode_operations
.lookup
= proc_openprom_deflookup_ptr
;
245 proc_openprom_use
= 0;
249 #if defined(CONFIG_SUN_OPENPROMFS_MODULE) && defined(CONFIG_KMOD)
251 proc_openprom_defreaddir(struct file
* filp
, void * dirent
, filldir_t filldir
)
253 request_module("openpromfs");
254 if ((proc_openprom_inode_operations
.default_file_ops
)->readdir
!=
255 proc_openprom_defreaddir
)
256 return (proc_openprom_inode_operations
.default_file_ops
)->readdir
257 (filp
, dirent
, filldir
);
260 #define OPENPROM_DEFREADDIR proc_openprom_defreaddir
262 static struct dentry
*
263 proc_openprom_deflookup(struct inode
* dir
, struct dentry
*dentry
)
265 request_module("openpromfs");
266 if (proc_openprom_inode_operations
.lookup
!=
267 proc_openprom_deflookup
)
268 return proc_openprom_inode_operations
.lookup
270 return ERR_PTR(-ENOENT
);
272 #define OPENPROM_DEFLOOKUP proc_openprom_deflookup
274 #define OPENPROM_DEFREADDIR NULL
275 #define OPENPROM_DEFLOOKUP NULL
278 static struct file_operations proc_openprom_operations
= {
279 NULL
, /* lseek - default */
280 NULL
, /* read - bad */
281 NULL
, /* write - bad */
282 OPENPROM_DEFREADDIR
, /* readdir */
283 NULL
, /* poll - default */
284 NULL
, /* ioctl - default */
286 NULL
, /* no special open code */
288 NULL
, /* no special release code */
289 NULL
/* can't fsync */
292 struct inode_operations proc_openprom_inode_operations
= {
293 &proc_openprom_operations
,/* default net directory file-ops */
295 OPENPROM_DEFLOOKUP
, /* lookup */
304 NULL
, /* follow_link */
305 NULL
, /* get_block */
307 NULL
, /* writepage */
308 NULL
, /* flushpage */
310 NULL
, /* permission */
312 NULL
/* revalidate */
315 struct proc_dir_entry proc_openprom
= {
316 PROC_OPENPROM
, 8, "openprom",
317 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
318 0, &proc_openprom_inode_operations
,
324 extern void openpromfs_init (void);
325 #endif /* CONFIG_SUN_OPENPROMFS */
327 static int make_inode_number(void)
329 int i
= find_first_zero_bit((void *) proc_alloc_map
, PROC_NDYNAMIC
);
330 if (i
<0 || i
>=PROC_NDYNAMIC
)
332 set_bit(i
, (void *) proc_alloc_map
);
333 return PROC_DYNAMIC_FIRST
+ i
;
336 int proc_register(struct proc_dir_entry
* dir
, struct proc_dir_entry
* dp
)
340 if (dp
->low_ino
== 0) {
341 i
= make_inode_number();
346 dp
->next
= dir
->subdir
;
349 if (S_ISDIR(dp
->mode
)) {
351 dp
->ops
= &proc_dir_inode_operations
;
353 } else if (S_ISLNK(dp
->mode
)) {
355 dp
->ops
= &proc_link_inode_operations
;
358 dp
->ops
= &proc_file_inode_operations
;
364 * Kill an inode that got unregistered..
366 static void proc_kill_inodes(int ino
)
370 /* inuse_filps is protected by the single kernel lock */
371 for (filp
= inuse_filps
; filp
; filp
= filp
->f_next
) {
372 struct dentry
* dentry
;
373 struct inode
* inode
;
375 dentry
= filp
->f_dentry
;
378 if (dentry
->d_op
!= &proc_dentry_operations
)
380 inode
= dentry
->d_inode
;
383 if (inode
->i_ino
!= ino
)
389 int proc_unregister(struct proc_dir_entry
* dir
, int ino
)
391 struct proc_dir_entry
**p
= &dir
->subdir
, *dp
;
393 while ((dp
= *p
) != NULL
) {
394 if (dp
->low_ino
== ino
) {
397 if (S_ISDIR(dp
->mode
))
399 if (ino
>= PROC_DYNAMIC_FIRST
&&
400 ino
< PROC_DYNAMIC_FIRST
+PROC_NDYNAMIC
)
401 clear_bit(ino
-PROC_DYNAMIC_FIRST
,
402 (void *) proc_alloc_map
);
403 proc_kill_inodes(ino
);
414 static int proc_self_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
419 len
= sprintf(tmp
, "%d", current
->pid
);
422 copy_to_user(buffer
, tmp
, len
);
426 static struct dentry
* proc_self_follow_link(struct dentry
*dentry
,
432 sprintf(tmp
, "%d", current
->pid
);
433 return lookup_dentry(tmp
, base
, follow
);
436 int proc_readlink(struct dentry
* dentry
, char * buffer
, int buflen
)
438 struct inode
*inode
= dentry
->d_inode
;
439 struct proc_dir_entry
* de
;
443 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
446 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
449 if (de
->readlink_proc
)
450 len
= de
->readlink_proc(de
, page
);
455 copy_to_user(buffer
, page
, len
);
456 free_page((unsigned long) page
);
460 struct dentry
* proc_follow_link(struct dentry
* dentry
, struct dentry
*base
, unsigned int follow
)
462 struct inode
*inode
= dentry
->d_inode
;
463 struct proc_dir_entry
* de
;
468 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
469 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
472 if (de
->readlink_proc
)
473 len
= de
->readlink_proc(de
, page
);
475 d
= lookup_dentry(page
, base
, follow
);
476 free_page((unsigned long) page
);
480 static struct inode_operations proc_self_inode_operations
= {
481 NULL
, /* no file-ops */
491 proc_self_readlink
, /* readlink */
492 proc_self_follow_link
, /* follow_link */
493 NULL
, /* get_block */
495 NULL
, /* writepage */
496 NULL
, /* flushpage */
498 NULL
, /* permission */
500 NULL
/* revalidate */
503 static struct inode_operations proc_link_inode_operations
= {
504 NULL
, /* no file-ops */
514 proc_readlink
, /* readlink */
515 proc_follow_link
, /* follow_link */
516 NULL
, /* get_block */
518 NULL
, /* writepage */
519 NULL
, /* flushpage */
521 NULL
, /* permission */
523 NULL
/* revalidate */
526 static struct proc_dir_entry proc_root_loadavg
= {
527 PROC_LOADAVG
, 7, "loadavg",
528 S_IFREG
| S_IRUGO
, 1, 0, 0,
529 0, &proc_array_inode_operations
531 static struct proc_dir_entry proc_root_uptime
= {
532 PROC_UPTIME
, 6, "uptime",
533 S_IFREG
| S_IRUGO
, 1, 0, 0,
534 0, &proc_array_inode_operations
536 static struct proc_dir_entry proc_root_meminfo
= {
537 PROC_MEMINFO
, 7, "meminfo",
538 S_IFREG
| S_IRUGO
, 1, 0, 0,
539 0, &proc_array_inode_operations
541 static struct proc_dir_entry proc_root_kmsg
= {
542 PROC_KMSG
, 4, "kmsg",
543 S_IFREG
| S_IRUSR
, 1, 0, 0,
544 0, &proc_kmsg_inode_operations
546 static struct proc_dir_entry proc_root_version
= {
547 PROC_VERSION
, 7, "version",
548 S_IFREG
| S_IRUGO
, 1, 0, 0,
549 0, &proc_array_inode_operations
551 static struct proc_dir_entry proc_root_cpuinfo
= {
552 PROC_CPUINFO
, 7, "cpuinfo",
553 S_IFREG
| S_IRUGO
, 1, 0, 0,
554 0, &proc_array_inode_operations
556 #if defined (CONFIG_PROC_HARDWARE)
557 static struct proc_dir_entry proc_root_hardware
= {
558 PROC_HARDWARE
, 8, "hardware",
559 S_IFREG
| S_IRUGO
, 1, 0, 0,
560 0, &proc_array_inode_operations
563 #ifdef CONFIG_STRAM_PROC
564 static struct proc_dir_entry proc_root_stram
= {
565 PROC_STRAM
, 5, "stram",
566 S_IFREG
| S_IRUGO
, 1, 0, 0,
567 0, &proc_array_inode_operations
570 static struct proc_dir_entry proc_root_self
= {
571 PROC_SELF
, 4, "self",
572 S_IFLNK
| S_IRUGO
| S_IWUGO
| S_IXUGO
, 1, 0, 0,
573 64, &proc_self_inode_operations
,
575 #ifdef CONFIG_DEBUG_MALLOC
576 static struct proc_dir_entry proc_root_malloc
= {
577 PROC_MALLOC
, 6, "malloc",
578 S_IFREG
| S_IRUGO
, 1, 0, 0,
579 0, &proc_array_inode_operations
582 static struct proc_dir_entry proc_root_kcore
= {
583 PROC_KCORE
, 5, "kcore",
584 S_IFREG
| S_IRUSR
, 1, 0, 0,
585 0, &proc_kcore_inode_operations
587 #ifdef CONFIG_MODULES
588 static struct proc_dir_entry proc_root_modules
= {
589 PROC_MODULES
, 7, "modules",
590 S_IFREG
| S_IRUGO
, 1, 0, 0,
591 0, &proc_array_inode_operations
593 static struct proc_dir_entry proc_root_ksyms
= {
594 PROC_KSYMS
, 5, "ksyms",
595 S_IFREG
| S_IRUGO
, 1, 0, 0,
596 0, &proc_array_inode_operations
599 static struct proc_dir_entry proc_root_stat
= {
600 PROC_STAT
, 4, "stat",
601 S_IFREG
| S_IRUGO
, 1, 0, 0,
602 0, &proc_array_inode_operations
604 static struct proc_dir_entry proc_root_devices
= {
605 PROC_DEVICES
, 7, "devices",
606 S_IFREG
| S_IRUGO
, 1, 0, 0,
607 0, &proc_array_inode_operations
609 static struct proc_dir_entry proc_root_partitions
= {
610 PROC_PARTITIONS
, 10, "partitions",
611 S_IFREG
| S_IRUGO
, 1, 0, 0,
612 0, &proc_array_inode_operations
614 static struct proc_dir_entry proc_root_interrupts
= {
615 PROC_INTERRUPTS
, 10,"interrupts",
616 S_IFREG
| S_IRUGO
, 1, 0, 0,
617 0, &proc_array_inode_operations
619 static struct proc_dir_entry proc_root_filesystems
= {
620 PROC_FILESYSTEMS
, 11,"filesystems",
621 S_IFREG
| S_IRUGO
, 1, 0, 0,
622 0, &proc_array_inode_operations
624 struct proc_dir_entry proc_root_fs
= {
626 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
627 0, &proc_dir_inode_operations
,
632 static struct proc_dir_entry proc_root_dma
= {
634 S_IFREG
| S_IRUGO
, 1, 0, 0,
635 0, &proc_array_inode_operations
637 static struct proc_dir_entry proc_root_ioports
= {
638 PROC_IOPORTS
, 7, "ioports",
639 S_IFREG
| S_IRUGO
, 1, 0, 0,
640 0, &proc_array_inode_operations
642 static struct proc_dir_entry proc_root_memory
= {
643 PROC_MEMORY
, 6, "memory",
644 S_IFREG
| S_IRUGO
, 1, 0, 0,
645 0, &proc_array_inode_operations
647 static struct proc_dir_entry proc_root_cmdline
= {
648 PROC_CMDLINE
, 7, "cmdline",
649 S_IFREG
| S_IRUGO
, 1, 0, 0,
650 0, &proc_array_inode_operations
653 static struct proc_dir_entry proc_root_rtc
= {
655 S_IFREG
| S_IRUGO
, 1, 0, 0,
656 0, &proc_array_inode_operations
659 static struct proc_dir_entry proc_root_locks
= {
660 PROC_LOCKS
, 5, "locks",
661 S_IFREG
| S_IRUGO
, 1, 0, 0,
662 0, &proc_array_inode_operations
664 static struct proc_dir_entry proc_root_mounts
= {
665 PROC_MTAB
, 6, "mounts",
666 S_IFREG
| S_IRUGO
, 1, 0, 0,
667 0, &proc_array_inode_operations
669 static struct proc_dir_entry proc_root_swaps
= {
670 PROC_SWAP
, 5, "swaps",
671 S_IFREG
| S_IRUGO
, 1, 0, 0,
672 0, &proc_array_inode_operations
674 static struct proc_dir_entry proc_root_profile
= {
675 PROC_PROFILE
, 7, "profile",
676 S_IFREG
| S_IRUGO
| S_IWUSR
, 1, 0, 0,
677 0, &proc_profile_inode_operations
679 static struct proc_dir_entry proc_root_slab
= {
680 PROC_SLABINFO
, 8, "slabinfo",
681 S_IFREG
| S_IRUGO
, 1, 0, 0,
682 0, &proc_array_inode_operations
685 static struct proc_dir_entry proc_root_ppc_htab
= {
686 PROC_PPC_HTAB
, 8, "ppc_htab",
687 S_IFREG
| S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, 1, 0, 0,
688 0, &proc_ppc_htab_inode_operations
,
689 NULL
, NULL
, /* get_info, fill_inode */
691 NULL
, NULL
/* parent, subdir */
695 __initfunc(void proc_root_init(void))
698 proc_register(&proc_root
, &proc_root_loadavg
);
699 proc_register(&proc_root
, &proc_root_uptime
);
700 proc_register(&proc_root
, &proc_root_meminfo
);
701 proc_register(&proc_root
, &proc_root_kmsg
);
702 proc_register(&proc_root
, &proc_root_version
);
703 proc_register(&proc_root
, &proc_root_cpuinfo
);
704 proc_register(&proc_root
, &proc_root_self
);
705 proc_net
= create_proc_entry("net", S_IFDIR
, 0);
706 proc_scsi
= create_proc_entry("scsi", S_IFDIR
, 0);
707 #ifdef CONFIG_SYSVIPC
708 proc_sysvipc
= create_proc_entry("sysvipc", S_IFDIR
, 0);
711 proc_register(&proc_root
, &proc_sys_root
);
714 proc_register(&proc_root
, &proc_mca
);
717 #ifdef CONFIG_DEBUG_MALLOC
718 proc_register(&proc_root
, &proc_root_malloc
);
720 proc_register(&proc_root
, &proc_root_kcore
);
721 proc_root_kcore
.size
= (MAP_NR(high_memory
) << PAGE_SHIFT
) + PAGE_SIZE
;
723 #ifdef CONFIG_MODULES
724 proc_register(&proc_root
, &proc_root_modules
);
725 proc_register(&proc_root
, &proc_root_ksyms
);
727 proc_register(&proc_root
, &proc_root_stat
);
728 proc_register(&proc_root
, &proc_root_devices
);
729 proc_register(&proc_root
, &proc_root_partitions
);
730 proc_register(&proc_root
, &proc_root_interrupts
);
731 proc_register(&proc_root
, &proc_root_filesystems
);
732 proc_register(&proc_root
, &proc_root_fs
);
733 proc_register(&proc_root
, &proc_root_dma
);
734 proc_register(&proc_root
, &proc_root_ioports
);
735 proc_register(&proc_root
, &proc_root_memory
);
736 proc_register(&proc_root
, &proc_root_cmdline
);
738 proc_register(&proc_root
, &proc_root_rtc
);
740 proc_register(&proc_root
, &proc_root_locks
);
742 proc_register(&proc_root
, &proc_root_mounts
);
743 proc_register(&proc_root
, &proc_root_swaps
);
745 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
746 #ifdef CONFIG_SUN_OPENPROMFS
749 proc_register(&proc_root
, &proc_openprom
);
751 #ifdef CONFIG_PROC_HARDWARE
752 proc_register(&proc_root
, &proc_root_hardware
);
754 #ifdef CONFIG_STRAM_PROC
755 proc_register(&proc_root
, &proc_root_stram
);
757 proc_register(&proc_root
, &proc_root_slab
);
760 proc_register(&proc_root
, &proc_root_profile
);
761 proc_root_profile
.size
= (1+prof_len
) * sizeof(unsigned int);
766 proc_register(&proc_root
, &proc_root_ppc_htab
);
768 #ifdef CONFIG_PROC_DEVICETREE
769 proc_device_tree_init();
772 proc_bus
= create_proc_entry("bus", S_IFDIR
, 0);
776 * As some entries in /proc are volatile, we want to
777 * get rid of unused dentries. This could be made
778 * smarter: we could keep a "volatile" flag in the
779 * inode to indicate which ones to keep.
782 proc_delete_dentry(struct dentry
* dentry
)
787 struct dentry_operations proc_dentry_operations
=
789 NULL
, /* revalidate */
791 NULL
, /* d_compare */
792 proc_delete_dentry
/* d_delete(struct dentry *) */
796 * Don't create negative dentries here, return -ENOENT by hand
799 struct dentry
*proc_lookup(struct inode
* dir
, struct dentry
*dentry
)
802 struct proc_dir_entry
* de
;
807 de
= (struct proc_dir_entry
*) dir
->u
.generic_ip
;
809 for (de
= de
->subdir
; de
; de
= de
->next
) {
810 if (!de
|| !de
->low_ino
)
812 if (de
->namelen
!= dentry
->d_name
.len
)
814 if (!memcmp(dentry
->d_name
.name
, de
->name
, de
->namelen
)) {
815 int ino
= de
->low_ino
| (dir
->i_ino
& ~(0xffff));
817 inode
= proc_get_inode(dir
->i_sb
, ino
, de
);
824 dentry
->d_op
= &proc_dentry_operations
;
825 d_add(dentry
, inode
);
828 return ERR_PTR(error
);
831 static struct dentry
*proc_root_lookup(struct inode
* dir
, struct dentry
* dentry
)
834 struct task_struct
*p
;
839 if (dir
->i_ino
== PROC_ROOT_INO
) { /* check for safety... */
840 dir
->i_nlink
= proc_root
.nlink
;
842 read_lock(&tasklist_lock
);
847 read_unlock(&tasklist_lock
);
850 if (!proc_lookup(dir
, dentry
))
854 name
= dentry
->d_name
.name
;
855 len
= dentry
->d_name
.len
;
865 if (pid
& 0xffff0000) {
870 read_lock(&tasklist_lock
);
871 p
= find_task_by_pid(pid
);
872 read_unlock(&tasklist_lock
);
875 unsigned long ino
= (pid
<< 16) + PROC_PID_INO
;
876 inode
= proc_get_inode(dir
->i_sb
, ino
, &proc_pid
);
878 return ERR_PTR(-EINVAL
);
879 inode
->i_flags
|=S_IMMUTABLE
;
882 dentry
->d_op
= &proc_dentry_operations
;
883 d_add(dentry
, inode
);
888 * This returns non-zero if at EOF, so that the /proc
889 * root directory can use this and check if it should
890 * continue with the <pid> entries..
892 * Note that the VFS-layer doesn't care about the return
893 * value of the readdir() call, as long as it's non-negative
896 int proc_readdir(struct file
* filp
,
897 void * dirent
, filldir_t filldir
)
899 struct proc_dir_entry
* de
;
902 struct inode
*inode
= filp
->f_dentry
->d_inode
;
905 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
911 if (filldir(dirent
, ".", 1, i
, ino
) < 0)
917 if (filldir(dirent
, "..", 2, i
, de
->parent
->low_ino
) < 0)
936 if (filldir(dirent
, de
->name
, de
->namelen
, filp
->f_pos
, ino
| de
->low_ino
) < 0)
945 #define PROC_NUMBUF 10
946 #define PROC_MAXPIDS 20
949 * Get a few pid's to return for filldir - we need to hold the
950 * tasklist lock while doing this, and we must release it before
951 * we actually do the filldir itself, so we use a temp buffer..
953 static int get_pid_list(int index
, unsigned int *pids
)
955 struct task_struct
*p
;
958 index
-= FIRST_PROCESS_ENTRY
;
959 read_lock(&tasklist_lock
);
968 if (nr_pids
>= PROC_MAXPIDS
)
971 read_unlock(&tasklist_lock
);
975 static int proc_root_readdir(struct file
* filp
,
976 void * dirent
, filldir_t filldir
)
978 unsigned int pid_array
[PROC_MAXPIDS
];
979 char buf
[PROC_NUMBUF
];
980 unsigned int nr
= filp
->f_pos
;
981 unsigned int nr_pids
, i
;
983 if (nr
< FIRST_PROCESS_ENTRY
) {
984 int error
= proc_readdir(filp
, dirent
, filldir
);
987 filp
->f_pos
= nr
= FIRST_PROCESS_ENTRY
;
990 nr_pids
= get_pid_list(nr
, pid_array
);
992 for (i
= 0; i
< nr_pids
; i
++) {
993 int pid
= pid_array
[i
];
994 ino_t ino
= (pid
<< 16) + PROC_PID_INO
;
995 unsigned long j
= PROC_NUMBUF
;
999 buf
[j
] = '0' + (pid
% 10);
1003 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
) < 0)
1010 static int proc_unlink(struct inode
*dir
, struct dentry
*dentry
)
1012 struct proc_dir_entry
* dp
= dir
->u
.generic_ip
;
1014 printk("proc_file_unlink: deleting %s/%s\n", dp
->name
, dentry
->d_name
.name
);
1016 remove_proc_entry(dentry
->d_name
.name
, dp
);
1017 dentry
->d_inode
->i_nlink
= 0;