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
,
168 *proc_root_fs
, *proc_root_driver
;
171 struct proc_dir_entry
*proc_mca
;
175 struct proc_dir_entry
*proc_sys_root
;
178 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
180 static int (*proc_openprom_defreaddir_ptr
)(struct file
*, void *, filldir_t
);
181 static struct dentry
* (*proc_openprom_deflookup_ptr
)(struct inode
*, struct dentry
*);
182 void (*proc_openprom_use
)(struct inode
*, int) = 0;
183 static struct openpromfs_dev
*proc_openprom_devices
= NULL
;
184 static ino_t proc_openpromdev_ino
= PROC_OPENPROMD_FIRST
;
186 struct inode_operations
*
187 proc_openprom_register(int (*readdir
)(struct file
*, void *, filldir_t
),
188 struct dentry
* (*lookup
)(struct inode
*, struct dentry
*),
189 void (*use
)(struct inode
*, int),
190 struct openpromfs_dev
***devices
)
192 proc_openprom_defreaddir_ptr
= (proc_openprom_inode_operations
.default_file_ops
)->readdir
;
193 proc_openprom_deflookup_ptr
= proc_openprom_inode_operations
.lookup
;
194 (proc_openprom_inode_operations
.default_file_ops
)->readdir
= readdir
;
195 proc_openprom_inode_operations
.lookup
= lookup
;
196 proc_openprom_use
= use
;
197 *devices
= &proc_openprom_devices
;
198 return &proc_openprom_inode_operations
;
201 int proc_openprom_regdev(struct openpromfs_dev
*d
)
203 if (proc_openpromdev_ino
== PROC_OPENPROMD_FIRST
+ PROC_NOPENPROMD
)
205 d
->next
= proc_openprom_devices
;
206 d
->inode
= proc_openpromdev_ino
++;
207 proc_openprom_devices
= d
;
211 int proc_openprom_unregdev(struct openpromfs_dev
*d
)
213 if (d
== proc_openprom_devices
) {
214 proc_openprom_devices
= d
->next
;
215 } else if (!proc_openprom_devices
)
218 struct openpromfs_dev
*p
;
220 for (p
= proc_openprom_devices
; p
->next
!= d
&& p
->next
; p
= p
->next
);
221 if (!p
->next
) return -1;
227 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
229 proc_openprom_deregister(void)
231 (proc_openprom_inode_operations
.default_file_ops
)->readdir
= proc_openprom_defreaddir_ptr
;
232 proc_openprom_inode_operations
.lookup
= proc_openprom_deflookup_ptr
;
233 proc_openprom_use
= 0;
237 #if defined(CONFIG_SUN_OPENPROMFS_MODULE) && defined(CONFIG_KMOD)
239 proc_openprom_defreaddir(struct file
* filp
, void * dirent
, filldir_t filldir
)
241 request_module("openpromfs");
242 if ((proc_openprom_inode_operations
.default_file_ops
)->readdir
!=
243 proc_openprom_defreaddir
)
244 return (proc_openprom_inode_operations
.default_file_ops
)->readdir
245 (filp
, dirent
, filldir
);
248 #define OPENPROM_DEFREADDIR proc_openprom_defreaddir
250 static struct dentry
*
251 proc_openprom_deflookup(struct inode
* dir
, struct dentry
*dentry
)
253 request_module("openpromfs");
254 if (proc_openprom_inode_operations
.lookup
!=
255 proc_openprom_deflookup
)
256 return proc_openprom_inode_operations
.lookup
258 return ERR_PTR(-ENOENT
);
260 #define OPENPROM_DEFLOOKUP proc_openprom_deflookup
262 #define OPENPROM_DEFREADDIR NULL
263 #define OPENPROM_DEFLOOKUP NULL
266 static struct file_operations proc_openprom_operations
= {
267 NULL
, /* lseek - default */
268 NULL
, /* read - bad */
269 NULL
, /* write - bad */
270 OPENPROM_DEFREADDIR
, /* readdir */
271 NULL
, /* poll - default */
272 NULL
, /* ioctl - default */
274 NULL
, /* no special open code */
276 NULL
, /* no special release code */
277 NULL
/* can't fsync */
280 struct inode_operations proc_openprom_inode_operations
= {
281 &proc_openprom_operations
,/* default net directory file-ops */
283 OPENPROM_DEFLOOKUP
, /* lookup */
292 NULL
, /* follow_link */
293 NULL
, /* get_block */
295 NULL
, /* writepage */
296 NULL
, /* flushpage */
298 NULL
, /* permission */
300 NULL
/* revalidate */
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
)
357 struct super_block
*sb
;
360 * Actually it's a partial revoke(). We have to go through all
361 * copies of procfs. proc_super_blocks is protected by the big
362 * lock for the time being.
364 for (sb
= proc_super_blocks
;
366 sb
= (struct super_block
*)sb
->u
.generic_sbp
) {
368 for (p
= sb
->s_files
.next
; p
!= &sb
->s_files
; p
= p
->next
) {
369 struct file
* filp
= list_entry(p
, struct file
, f_list
);
370 struct dentry
* dentry
;
371 struct inode
* inode
;
373 dentry
= filp
->f_dentry
;
376 if (dentry
->d_op
!= &proc_dentry_operations
)
378 inode
= dentry
->d_inode
;
381 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_self
= {
528 S_IFLNK
| S_IRUGO
| S_IWUGO
| S_IXUGO
, 1, 0, 0,
529 64, &proc_self_inode_operations
,
532 static struct proc_dir_entry proc_root_ppc_htab
= {
534 S_IFREG
| S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, 1, 0, 0,
535 0, &proc_ppc_htab_inode_operations
,
536 NULL
, NULL
, /* get_info, fill_inode */
538 NULL
, NULL
/* parent, subdir */
542 void __init
proc_root_init(void)
546 proc_register(&proc_root
, &proc_root_self
);
547 proc_net
= create_proc_entry("net", S_IFDIR
, 0);
548 proc_scsi
= create_proc_entry("scsi", S_IFDIR
, 0);
549 #ifdef CONFIG_SYSVIPC
550 proc_sysvipc
= create_proc_entry("sysvipc", S_IFDIR
, 0);
553 proc_sys_root
= create_proc_entry("sys", S_IFDIR
, 0);
556 proc_mca
= create_proc_entry("mca", S_IFDIR
, 0);
558 proc_root_fs
= create_proc_entry("fs", S_IFDIR
, 0);
559 proc_root_driver
= create_proc_entry("driver", S_IFDIR
, 0);
560 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
561 #ifdef CONFIG_SUN_OPENPROMFS
564 proc_register(&proc_root
, &proc_openprom
);
568 proc_register(&proc_root
, &proc_root_ppc_htab
);
570 #ifdef CONFIG_PROC_DEVICETREE
571 proc_device_tree_init();
573 proc_bus
= create_proc_entry("bus", S_IFDIR
, 0);
577 * As some entries in /proc are volatile, we want to
578 * get rid of unused dentries. This could be made
579 * smarter: we could keep a "volatile" flag in the
580 * inode to indicate which ones to keep.
583 proc_delete_dentry(struct dentry
* dentry
)
588 struct dentry_operations proc_dentry_operations
=
590 NULL
, /* revalidate */
592 NULL
, /* d_compare */
593 proc_delete_dentry
/* d_delete(struct dentry *) */
597 * Don't create negative dentries here, return -ENOENT by hand
600 struct dentry
*proc_lookup(struct inode
* dir
, struct dentry
*dentry
)
603 struct proc_dir_entry
* de
;
608 de
= (struct proc_dir_entry
*) dir
->u
.generic_ip
;
610 for (de
= de
->subdir
; de
; de
= de
->next
) {
611 if (!de
|| !de
->low_ino
)
613 if (de
->namelen
!= dentry
->d_name
.len
)
615 if (!memcmp(dentry
->d_name
.name
, de
->name
, de
->namelen
)) {
616 int ino
= de
->low_ino
| (dir
->i_ino
& ~(0xffff));
618 inode
= proc_get_inode(dir
->i_sb
, ino
, de
);
625 dentry
->d_op
= &proc_dentry_operations
;
626 d_add(dentry
, inode
);
629 return ERR_PTR(error
);
632 static struct dentry
*proc_root_lookup(struct inode
* dir
, struct dentry
* dentry
)
635 struct task_struct
*p
;
640 if (dir
->i_ino
== PROC_ROOT_INO
) { /* check for safety... */
641 extern unsigned long total_forks
;
642 static int last_timestamp
= 0;
645 * this one can be a serious 'ps' performance problem if
646 * there are many threads running - thus we do 'lazy'
647 * link-recalculation - we change it only if the number
648 * of threads has increased.
650 if (total_forks
!= last_timestamp
) {
651 int nlink
= proc_root
.nlink
;
653 read_lock(&tasklist_lock
);
654 last_timestamp
= total_forks
;
657 read_unlock(&tasklist_lock
);
659 * subtract the # of idle threads which
660 * do not show up in /proc:
662 dir
->i_nlink
= nlink
- smp_num_cpus
;
666 if (!proc_lookup(dir
, dentry
))
670 name
= dentry
->d_name
.name
;
671 len
= dentry
->d_name
.len
;
683 if (pid
& 0xffff0000) {
688 read_lock(&tasklist_lock
);
689 p
= find_task_by_pid(pid
);
690 read_unlock(&tasklist_lock
);
693 unsigned long ino
= (pid
<< 16) + PROC_PID_INO
;
694 inode
= proc_get_inode(dir
->i_sb
, ino
, &proc_pid
);
696 return ERR_PTR(-EINVAL
);
697 inode
->i_flags
|=S_IMMUTABLE
;
700 dentry
->d_op
= &proc_dentry_operations
;
701 d_add(dentry
, inode
);
706 * This returns non-zero if at EOF, so that the /proc
707 * root directory can use this and check if it should
708 * continue with the <pid> entries..
710 * Note that the VFS-layer doesn't care about the return
711 * value of the readdir() call, as long as it's non-negative
714 int proc_readdir(struct file
* filp
,
715 void * dirent
, filldir_t filldir
)
717 struct proc_dir_entry
* de
;
720 struct inode
*inode
= filp
->f_dentry
->d_inode
;
723 de
= (struct proc_dir_entry
*) inode
->u
.generic_ip
;
729 if (filldir(dirent
, ".", 1, i
, ino
) < 0)
735 if (filldir(dirent
, "..", 2, i
, de
->parent
->low_ino
) < 0)
754 if (filldir(dirent
, de
->name
, de
->namelen
, filp
->f_pos
, ino
| de
->low_ino
) < 0)
763 #define PROC_NUMBUF 10
764 #define PROC_MAXPIDS 20
767 * Get a few pid's to return for filldir - we need to hold the
768 * tasklist lock while doing this, and we must release it before
769 * we actually do the filldir itself, so we use a temp buffer..
771 static int get_pid_list(int index
, unsigned int *pids
)
773 struct task_struct
*p
;
776 index
-= FIRST_PROCESS_ENTRY
;
777 read_lock(&tasklist_lock
);
786 if (nr_pids
>= PROC_MAXPIDS
)
789 read_unlock(&tasklist_lock
);
793 static int proc_root_readdir(struct file
* filp
,
794 void * dirent
, filldir_t filldir
)
796 unsigned int pid_array
[PROC_MAXPIDS
];
797 char buf
[PROC_NUMBUF
];
798 unsigned int nr
= filp
->f_pos
;
799 unsigned int nr_pids
, i
;
801 if (nr
< FIRST_PROCESS_ENTRY
) {
802 int error
= proc_readdir(filp
, dirent
, filldir
);
805 filp
->f_pos
= nr
= FIRST_PROCESS_ENTRY
;
808 nr_pids
= get_pid_list(nr
, pid_array
);
810 for (i
= 0; i
< nr_pids
; i
++) {
811 int pid
= pid_array
[i
];
812 ino_t ino
= (pid
<< 16) + PROC_PID_INO
;
813 unsigned long j
= PROC_NUMBUF
;
817 buf
[j
] = '0' + (pid
% 10);
821 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
) < 0)
828 static int proc_unlink(struct inode
*dir
, struct dentry
*dentry
)
830 struct proc_dir_entry
* dp
= dir
->u
.generic_ip
;
832 printk("proc_file_unlink: deleting %s/%s\n", dp
->name
, dentry
->d_name
.name
);
834 remove_proc_entry(dentry
->d_name
.name
, dp
);
835 dentry
->d_inode
->i_nlink
= 0;