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 */
82 * /proc dynamic directories now support unlinking
84 struct inode_operations proc_dyna_dir_inode_operations
= {
85 &proc_dir_operations
, /* default proc dir ops */
87 proc_lookup
, /* lookup */
89 proc_unlink
, /* unlink(struct inode *, struct dentry *) */
96 NULL
, /* follow_link */
101 NULL
/* permission */
105 * The root /proc directory is special, as it has the
106 * <pid> directories. Thus we don't use the generic
107 * directory handling functions for that..
109 static struct file_operations proc_root_operations
= {
110 NULL
, /* lseek - default */
111 NULL
, /* read - bad */
112 NULL
, /* write - bad */
113 proc_root_readdir
, /* readdir */
114 NULL
, /* poll - default */
115 NULL
, /* ioctl - default */
117 NULL
, /* no special open code */
119 NULL
, /* no special release code */
124 * proc root can do almost nothing..
126 static struct inode_operations proc_root_inode_operations
= {
127 &proc_root_operations
, /* default base directory file-ops */
129 proc_root_lookup
, /* lookup */
138 NULL
, /* follow_link */
140 NULL
, /* writepage */
143 NULL
/* permission */
147 * This is the root "inode" in the /proc tree..
149 struct proc_dir_entry proc_root
= {
150 PROC_ROOT_INO
, 5, "/proc",
151 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
152 0, &proc_root_inode_operations
,
158 struct proc_dir_entry
*proc_net
, *proc_scsi
, *proc_bus
, *proc_sysvipc
;
161 struct proc_dir_entry proc_mca
= {
163 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
164 0, &proc_dir_inode_operations
,
166 NULL
, &proc_root
, NULL
171 struct proc_dir_entry proc_sys_root
= {
172 PROC_SYS
, 3, "sys", /* inode, name */
173 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0, /* mode, nlink, uid, gid */
174 0, &proc_dir_inode_operations
, /* size, ops */
175 NULL
, NULL
, /* get_info, fill_inode */
177 NULL
, NULL
/* parent, subdir */
181 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
183 static int (*proc_openprom_defreaddir_ptr
)(struct file
*, void *, filldir_t
);
184 static struct dentry
* (*proc_openprom_deflookup_ptr
)(struct inode
*, struct dentry
*);
185 void (*proc_openprom_use
)(struct inode
*, int) = 0;
186 static struct openpromfs_dev
*proc_openprom_devices
= NULL
;
187 static ino_t proc_openpromdev_ino
= PROC_OPENPROMD_FIRST
;
189 struct inode_operations
*
190 proc_openprom_register(int (*readdir
)(struct file
*, void *, filldir_t
),
191 struct dentry
* (*lookup
)(struct inode
*, struct dentry
*),
192 void (*use
)(struct inode
*, int),
193 struct openpromfs_dev
***devices
)
195 proc_openprom_defreaddir_ptr
= (proc_openprom_inode_operations
.default_file_ops
)->readdir
;
196 proc_openprom_deflookup_ptr
= proc_openprom_inode_operations
.lookup
;
197 (proc_openprom_inode_operations
.default_file_ops
)->readdir
= readdir
;
198 proc_openprom_inode_operations
.lookup
= lookup
;
199 proc_openprom_use
= use
;
200 *devices
= &proc_openprom_devices
;
201 return &proc_openprom_inode_operations
;
204 int proc_openprom_regdev(struct openpromfs_dev
*d
)
206 if (proc_openpromdev_ino
== PROC_OPENPROMD_FIRST
+ PROC_NOPENPROMD
)
208 d
->next
= proc_openprom_devices
;
209 d
->inode
= proc_openpromdev_ino
++;
210 proc_openprom_devices
= d
;
214 int proc_openprom_unregdev(struct openpromfs_dev
*d
)
216 if (d
== proc_openprom_devices
) {
217 proc_openprom_devices
= d
->next
;
218 } else if (!proc_openprom_devices
)
221 struct openpromfs_dev
*p
;
223 for (p
= proc_openprom_devices
; p
->next
!= d
&& p
->next
; p
= p
->next
);
224 if (!p
->next
) return -1;
230 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
232 proc_openprom_deregister(void)
234 (proc_openprom_inode_operations
.default_file_ops
)->readdir
= proc_openprom_defreaddir_ptr
;
235 proc_openprom_inode_operations
.lookup
= proc_openprom_deflookup_ptr
;
236 proc_openprom_use
= 0;
240 #if defined(CONFIG_SUN_OPENPROMFS_MODULE) && defined(CONFIG_KMOD)
242 proc_openprom_defreaddir(struct file
* filp
, void * dirent
, filldir_t filldir
)
244 request_module("openpromfs");
245 if ((proc_openprom_inode_operations
.default_file_ops
)->readdir
!=
246 proc_openprom_defreaddir
)
247 return (proc_openprom_inode_operations
.default_file_ops
)->readdir
248 (filp
, dirent
, filldir
);
251 #define OPENPROM_DEFREADDIR proc_openprom_defreaddir
253 static struct dentry
*
254 proc_openprom_deflookup(struct inode
* dir
, struct dentry
*dentry
)
256 request_module("openpromfs");
257 if (proc_openprom_inode_operations
.lookup
!=
258 proc_openprom_deflookup
)
259 return proc_openprom_inode_operations
.lookup
261 return ERR_PTR(-ENOENT
);
263 #define OPENPROM_DEFLOOKUP proc_openprom_deflookup
265 #define OPENPROM_DEFREADDIR NULL
266 #define OPENPROM_DEFLOOKUP NULL
269 static struct file_operations proc_openprom_operations
= {
270 NULL
, /* lseek - default */
271 NULL
, /* read - bad */
272 NULL
, /* write - bad */
273 OPENPROM_DEFREADDIR
, /* readdir */
274 NULL
, /* poll - default */
275 NULL
, /* ioctl - default */
277 NULL
, /* no special open code */
279 NULL
, /* no special release code */
280 NULL
/* can't fsync */
283 struct inode_operations proc_openprom_inode_operations
= {
284 &proc_openprom_operations
,/* default net directory file-ops */
286 OPENPROM_DEFLOOKUP
, /* lookup */
295 NULL
, /* follow_link */
297 NULL
, /* writepage */
300 NULL
/* permission */
303 struct proc_dir_entry proc_openprom
= {
304 PROC_OPENPROM
, 8, "openprom",
305 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
306 0, &proc_openprom_inode_operations
,
312 extern void openpromfs_init (void);
313 #endif /* CONFIG_SUN_OPENPROMFS */
315 static int make_inode_number(void)
317 int i
= find_first_zero_bit((void *) proc_alloc_map
, PROC_NDYNAMIC
);
318 if (i
<0 || i
>=PROC_NDYNAMIC
)
320 set_bit(i
, (void *) proc_alloc_map
);
321 return PROC_DYNAMIC_FIRST
+ i
;
324 int proc_register(struct proc_dir_entry
* dir
, struct proc_dir_entry
* dp
)
328 if (dp
->low_ino
== 0) {
329 i
= make_inode_number();
334 dp
->next
= dir
->subdir
;
337 if (S_ISDIR(dp
->mode
)) {
339 dp
->ops
= &proc_dir_inode_operations
;
341 } else if (S_ISLNK(dp
->mode
)) {
343 dp
->ops
= &proc_link_inode_operations
;
346 dp
->ops
= &proc_file_inode_operations
;
352 * Kill an inode that got unregistered..
354 static void proc_kill_inodes(int ino
)
358 /* inuse_filps is protected by the single kernel lock */
359 for (filp
= inuse_filps
; filp
; filp
= filp
->f_next
) {
360 struct dentry
* dentry
;
361 struct inode
* inode
;
363 dentry
= filp
->f_dentry
;
366 if (dentry
->d_op
!= &proc_dentry_operations
)
368 inode
= dentry
->d_inode
;
371 if (inode
->i_ino
!= ino
)
377 int proc_unregister(struct proc_dir_entry
* dir
, int ino
)
379 struct proc_dir_entry
**p
= &dir
->subdir
, *dp
;
381 while ((dp
= *p
) != NULL
) {
382 if (dp
->low_ino
== ino
) {
385 if (S_ISDIR(dp
->mode
))
387 if (ino
>= PROC_DYNAMIC_FIRST
&&
388 ino
< PROC_DYNAMIC_FIRST
+PROC_NDYNAMIC
)
389 clear_bit(ino
-PROC_DYNAMIC_FIRST
,
390 (void *) proc_alloc_map
);
391 proc_kill_inodes(ino
);
402 static int proc_self_readlink(struct dentry
*dentry
, char *buffer
, int buflen
)
407 len
= sprintf(tmp
, "%d", current
->pid
);
410 copy_to_user(buffer
, tmp
, len
);
414 static struct dentry
* proc_self_follow_link(struct dentry
*dentry
,
420 sprintf(tmp
, "%d", current
->pid
);
421 return lookup_dentry(tmp
, base
, follow
);
424 int proc_readlink(struct dentry
* dentry
, char * buffer
, int buflen
)
426 struct inode
*inode
= dentry
->d_inode
;
427 struct proc_dir_entry
* de
;
431 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
434 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
437 if (de
->readlink_proc
)
438 len
= de
->readlink_proc(de
, page
);
443 copy_to_user(buffer
, page
, len
);
444 free_page((unsigned long) page
);
448 struct dentry
* proc_follow_link(struct dentry
* dentry
, struct dentry
*base
, unsigned int follow
)
450 struct inode
*inode
= dentry
->d_inode
;
451 struct proc_dir_entry
* de
;
456 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
457 if (!(page
= (char*) __get_free_page(GFP_KERNEL
)))
460 if (de
->readlink_proc
)
461 len
= de
->readlink_proc(de
, page
);
463 d
= lookup_dentry(page
, base
, follow
);
464 free_page((unsigned long) page
);
468 static struct inode_operations proc_self_inode_operations
= {
469 NULL
, /* no file-ops */
479 proc_self_readlink
, /* readlink */
480 proc_self_follow_link
, /* follow_link */
482 NULL
, /* writepage */
485 NULL
/* permission */
488 static struct inode_operations proc_link_inode_operations
= {
489 NULL
, /* no file-ops */
499 proc_readlink
, /* readlink */
500 proc_follow_link
, /* follow_link */
502 NULL
, /* writepage */
505 NULL
/* permission */
508 static struct proc_dir_entry proc_root_loadavg
= {
509 PROC_LOADAVG
, 7, "loadavg",
510 S_IFREG
| S_IRUGO
, 1, 0, 0,
511 0, &proc_array_inode_operations
513 static struct proc_dir_entry proc_root_uptime
= {
514 PROC_UPTIME
, 6, "uptime",
515 S_IFREG
| S_IRUGO
, 1, 0, 0,
516 0, &proc_array_inode_operations
518 static struct proc_dir_entry proc_root_meminfo
= {
519 PROC_MEMINFO
, 7, "meminfo",
520 S_IFREG
| S_IRUGO
, 1, 0, 0,
521 0, &proc_array_inode_operations
523 static struct proc_dir_entry proc_root_kmsg
= {
524 PROC_KMSG
, 4, "kmsg",
525 S_IFREG
| S_IRUSR
, 1, 0, 0,
526 0, &proc_kmsg_inode_operations
528 static struct proc_dir_entry proc_root_version
= {
529 PROC_VERSION
, 7, "version",
530 S_IFREG
| S_IRUGO
, 1, 0, 0,
531 0, &proc_array_inode_operations
533 static struct proc_dir_entry proc_root_cpuinfo
= {
534 PROC_CPUINFO
, 7, "cpuinfo",
535 S_IFREG
| S_IRUGO
, 1, 0, 0,
536 0, &proc_array_inode_operations
538 #if defined (CONFIG_PROC_HARDWARE)
539 static struct proc_dir_entry proc_root_hardware
= {
540 PROC_HARDWARE
, 8, "hardware",
541 S_IFREG
| S_IRUGO
, 1, 0, 0,
542 0, &proc_array_inode_operations
545 #ifdef CONFIG_STRAM_PROC
546 static struct proc_dir_entry proc_root_stram
= {
547 PROC_STRAM
, 5, "stram",
548 S_IFREG
| S_IRUGO
, 1, 0, 0,
549 0, &proc_array_inode_operations
552 static struct proc_dir_entry proc_root_self
= {
553 PROC_SELF
, 4, "self",
554 S_IFLNK
| S_IRUGO
| S_IWUGO
| S_IXUGO
, 1, 0, 0,
555 64, &proc_self_inode_operations
,
557 #ifdef CONFIG_DEBUG_MALLOC
558 static struct proc_dir_entry proc_root_malloc
= {
559 PROC_MALLOC
, 6, "malloc",
560 S_IFREG
| S_IRUGO
, 1, 0, 0,
561 0, &proc_array_inode_operations
564 static struct proc_dir_entry proc_root_kcore
= {
565 PROC_KCORE
, 5, "kcore",
566 S_IFREG
| S_IRUSR
, 1, 0, 0,
567 0, &proc_kcore_inode_operations
569 #ifdef CONFIG_MODULES
570 static struct proc_dir_entry proc_root_modules
= {
571 PROC_MODULES
, 7, "modules",
572 S_IFREG
| S_IRUGO
, 1, 0, 0,
573 0, &proc_array_inode_operations
575 static struct proc_dir_entry proc_root_ksyms
= {
576 PROC_KSYMS
, 5, "ksyms",
577 S_IFREG
| S_IRUGO
, 1, 0, 0,
578 0, &proc_array_inode_operations
581 static struct proc_dir_entry proc_root_stat
= {
582 PROC_STAT
, 4, "stat",
583 S_IFREG
| S_IRUGO
, 1, 0, 0,
584 0, &proc_array_inode_operations
586 static struct proc_dir_entry proc_root_devices
= {
587 PROC_DEVICES
, 7, "devices",
588 S_IFREG
| S_IRUGO
, 1, 0, 0,
589 0, &proc_array_inode_operations
591 static struct proc_dir_entry proc_root_partitions
= {
592 PROC_PARTITIONS
, 10, "partitions",
593 S_IFREG
| S_IRUGO
, 1, 0, 0,
594 0, &proc_array_inode_operations
596 static struct proc_dir_entry proc_root_interrupts
= {
597 PROC_INTERRUPTS
, 10,"interrupts",
598 S_IFREG
| S_IRUGO
, 1, 0, 0,
599 0, &proc_array_inode_operations
601 static struct proc_dir_entry proc_root_filesystems
= {
602 PROC_FILESYSTEMS
, 11,"filesystems",
603 S_IFREG
| S_IRUGO
, 1, 0, 0,
604 0, &proc_array_inode_operations
606 struct proc_dir_entry proc_root_fs
= {
608 S_IFDIR
| S_IRUGO
| S_IXUGO
, 2, 0, 0,
609 0, &proc_dir_inode_operations
,
614 static struct proc_dir_entry proc_root_dma
= {
616 S_IFREG
| S_IRUGO
, 1, 0, 0,
617 0, &proc_array_inode_operations
619 static struct proc_dir_entry proc_root_ioports
= {
620 PROC_IOPORTS
, 7, "ioports",
621 S_IFREG
| S_IRUGO
, 1, 0, 0,
622 0, &proc_array_inode_operations
624 static struct proc_dir_entry proc_root_memory
= {
625 PROC_MEMORY
, 6, "memory",
626 S_IFREG
| S_IRUGO
, 1, 0, 0,
627 0, &proc_array_inode_operations
629 static struct proc_dir_entry proc_root_cmdline
= {
630 PROC_CMDLINE
, 7, "cmdline",
631 S_IFREG
| S_IRUGO
, 1, 0, 0,
632 0, &proc_array_inode_operations
635 static struct proc_dir_entry proc_root_rtc
= {
637 S_IFREG
| S_IRUGO
, 1, 0, 0,
638 0, &proc_array_inode_operations
641 static struct proc_dir_entry proc_root_locks
= {
642 PROC_LOCKS
, 5, "locks",
643 S_IFREG
| S_IRUGO
, 1, 0, 0,
644 0, &proc_array_inode_operations
646 static struct proc_dir_entry proc_root_mounts
= {
647 PROC_MTAB
, 6, "mounts",
648 S_IFREG
| S_IRUGO
, 1, 0, 0,
649 0, &proc_array_inode_operations
651 static struct proc_dir_entry proc_root_swaps
= {
652 PROC_SWAP
, 5, "swaps",
653 S_IFREG
| S_IRUGO
, 1, 0, 0,
654 0, &proc_array_inode_operations
656 static struct proc_dir_entry proc_root_profile
= {
657 PROC_PROFILE
, 7, "profile",
658 S_IFREG
| S_IRUGO
| S_IWUSR
, 1, 0, 0,
659 0, &proc_profile_inode_operations
661 static struct proc_dir_entry proc_root_slab
= {
662 PROC_SLABINFO
, 8, "slabinfo",
663 S_IFREG
| S_IRUGO
, 1, 0, 0,
664 0, &proc_array_inode_operations
667 static struct proc_dir_entry proc_root_ppc_htab
= {
668 PROC_PPC_HTAB
, 8, "ppc_htab",
669 S_IFREG
| S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, 1, 0, 0,
670 0, &proc_ppc_htab_inode_operations
,
671 NULL
, NULL
, /* get_info, fill_inode */
673 NULL
, NULL
/* parent, subdir */
677 __initfunc(void proc_root_init(void))
680 proc_register(&proc_root
, &proc_root_loadavg
);
681 proc_register(&proc_root
, &proc_root_uptime
);
682 proc_register(&proc_root
, &proc_root_meminfo
);
683 proc_register(&proc_root
, &proc_root_kmsg
);
684 proc_register(&proc_root
, &proc_root_version
);
685 proc_register(&proc_root
, &proc_root_cpuinfo
);
686 proc_register(&proc_root
, &proc_root_self
);
687 proc_net
= create_proc_entry("net", S_IFDIR
, 0);
688 proc_scsi
= create_proc_entry("scsi", S_IFDIR
, 0);
689 #ifdef CONFIG_SYSVIPC
690 proc_sysvipc
= create_proc_entry("sysvipc", S_IFDIR
, 0);
693 proc_register(&proc_root
, &proc_sys_root
);
696 proc_register(&proc_root
, &proc_mca
);
699 #ifdef CONFIG_DEBUG_MALLOC
700 proc_register(&proc_root
, &proc_root_malloc
);
702 proc_register(&proc_root
, &proc_root_kcore
);
703 proc_root_kcore
.size
= (MAP_NR(high_memory
) << PAGE_SHIFT
) + PAGE_SIZE
;
705 #ifdef CONFIG_MODULES
706 proc_register(&proc_root
, &proc_root_modules
);
707 proc_register(&proc_root
, &proc_root_ksyms
);
709 proc_register(&proc_root
, &proc_root_stat
);
710 proc_register(&proc_root
, &proc_root_devices
);
711 proc_register(&proc_root
, &proc_root_partitions
);
712 proc_register(&proc_root
, &proc_root_interrupts
);
713 proc_register(&proc_root
, &proc_root_filesystems
);
714 proc_register(&proc_root
, &proc_root_fs
);
715 proc_register(&proc_root
, &proc_root_dma
);
716 proc_register(&proc_root
, &proc_root_ioports
);
717 proc_register(&proc_root
, &proc_root_memory
);
718 proc_register(&proc_root
, &proc_root_cmdline
);
720 proc_register(&proc_root
, &proc_root_rtc
);
722 proc_register(&proc_root
, &proc_root_locks
);
724 proc_register(&proc_root
, &proc_root_mounts
);
725 proc_register(&proc_root
, &proc_root_swaps
);
727 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
728 #ifdef CONFIG_SUN_OPENPROMFS
731 proc_register(&proc_root
, &proc_openprom
);
733 #ifdef CONFIG_PROC_HARDWARE
734 proc_register(&proc_root
, &proc_root_hardware
);
736 #ifdef CONFIG_STRAM_PROC
737 proc_register(&proc_root
, &proc_root_stram
);
739 proc_register(&proc_root
, &proc_root_slab
);
742 proc_register(&proc_root
, &proc_root_profile
);
743 proc_root_profile
.size
= (1+prof_len
) * sizeof(unsigned int);
748 proc_register(&proc_root
, &proc_root_ppc_htab
);
750 #ifdef CONFIG_PROC_DEVICETREE
751 proc_device_tree_init();
754 proc_bus
= create_proc_entry("bus", S_IFDIR
, 0);
758 * As some entries in /proc are volatile, we want to
759 * get rid of unused dentries. This could be made
760 * smarter: we could keep a "volatile" flag in the
761 * inode to indicate which ones to keep.
764 proc_delete_dentry(struct dentry
* dentry
)
769 struct dentry_operations proc_dentry_operations
=
771 NULL
, /* revalidate */
773 NULL
, /* d_compare */
774 proc_delete_dentry
/* d_delete(struct dentry *) */
778 * Don't create negative dentries here, return -ENOENT by hand
781 struct dentry
*proc_lookup(struct inode
* dir
, struct dentry
*dentry
)
784 struct proc_dir_entry
* de
;
789 de
= (struct proc_dir_entry
*) dir
->u
.generic_ip
;
791 for (de
= de
->subdir
; de
; de
= de
->next
) {
792 if (!de
|| !de
->low_ino
)
794 if (de
->namelen
!= dentry
->d_name
.len
)
796 if (!memcmp(dentry
->d_name
.name
, de
->name
, de
->namelen
)) {
797 int ino
= de
->low_ino
| (dir
->i_ino
& ~(0xffff));
799 inode
= proc_get_inode(dir
->i_sb
, ino
, de
);
806 dentry
->d_op
= &proc_dentry_operations
;
807 d_add(dentry
, inode
);
810 return ERR_PTR(error
);
813 static struct dentry
*proc_root_lookup(struct inode
* dir
, struct dentry
* dentry
)
816 struct task_struct
*p
;
821 if (dir
->i_ino
== PROC_ROOT_INO
) { /* check for safety... */
822 dir
->i_nlink
= proc_root
.nlink
;
824 read_lock(&tasklist_lock
);
829 read_unlock(&tasklist_lock
);
832 if (!proc_lookup(dir
, dentry
))
836 name
= dentry
->d_name
.name
;
837 len
= dentry
->d_name
.len
;
847 if (pid
& 0xffff0000) {
852 read_lock(&tasklist_lock
);
853 p
= find_task_by_pid(pid
);
854 read_unlock(&tasklist_lock
);
857 unsigned long ino
= (pid
<< 16) + PROC_PID_INO
;
858 inode
= proc_get_inode(dir
->i_sb
, ino
, &proc_pid
);
860 return ERR_PTR(-EINVAL
);
861 inode
->i_flags
|=S_IMMUTABLE
;
864 dentry
->d_op
= &proc_dentry_operations
;
865 d_add(dentry
, inode
);
870 * This returns non-zero if at EOF, so that the /proc
871 * root directory can use this and check if it should
872 * continue with the <pid> entries..
874 * Note that the VFS-layer doesn't care about the return
875 * value of the readdir() call, as long as it's non-negative
878 int proc_readdir(struct file
* filp
,
879 void * dirent
, filldir_t filldir
)
881 struct proc_dir_entry
* de
;
884 struct inode
*inode
= filp
->f_dentry
->d_inode
;
887 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
893 if (filldir(dirent
, ".", 1, i
, ino
) < 0)
899 if (filldir(dirent
, "..", 2, i
, de
->parent
->low_ino
) < 0)
918 if (filldir(dirent
, de
->name
, de
->namelen
, filp
->f_pos
, ino
| de
->low_ino
) < 0)
927 #define PROC_NUMBUF 10
928 #define PROC_MAXPIDS 20
931 * Get a few pid's to return for filldir - we need to hold the
932 * tasklist lock while doing this, and we must release it before
933 * we actually do the filldir itself, so we use a temp buffer..
935 static int get_pid_list(int index
, unsigned int *pids
)
937 struct task_struct
*p
;
940 index
-= FIRST_PROCESS_ENTRY
;
941 read_lock(&tasklist_lock
);
950 if (nr_pids
>= PROC_MAXPIDS
)
953 read_unlock(&tasklist_lock
);
957 static int proc_root_readdir(struct file
* filp
,
958 void * dirent
, filldir_t filldir
)
960 unsigned int pid_array
[PROC_MAXPIDS
];
961 char buf
[PROC_NUMBUF
];
962 unsigned int nr
= filp
->f_pos
;
963 unsigned int nr_pids
, i
;
965 if (nr
< FIRST_PROCESS_ENTRY
) {
966 int error
= proc_readdir(filp
, dirent
, filldir
);
969 filp
->f_pos
= nr
= FIRST_PROCESS_ENTRY
;
972 nr_pids
= get_pid_list(nr
, pid_array
);
974 for (i
= 0; i
< nr_pids
; i
++) {
975 int pid
= pid_array
[i
];
976 ino_t ino
= (pid
<< 16) + PROC_PID_INO
;
977 unsigned long j
= PROC_NUMBUF
;
981 buf
[j
] = '0' + (pid
% 10);
985 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
) < 0)
992 static int proc_unlink(struct inode
*dir
, struct dentry
*dentry
)
994 struct proc_dir_entry
* dp
= dir
->u
.generic_ip
;
996 printk("proc_file_unlink: deleting %s/%s\n", dp
->name
, dentry
->d_name
.name
);
998 remove_proc_entry(dentry
->d_name
.name
, dp
);
999 dentry
->d_inode
->i_nlink
= 0;