Import 2.3.9pre5
[davej-history.git] / fs / proc / root.c
blob31b89ca824ba01e51c848e510dc90281bfd6924b
1 /*
2 * linux/fs/proc/root.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc root directory handling functions
7 */
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>
18 #ifdef CONFIG_KMOD
19 #include <linux/kmod.h>
20 #endif
21 #ifdef CONFIG_ZORRO
22 #include <linux/zorro.h>
23 #endif
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 */
51 NULL, /* mmap */
52 NULL, /* no special open code */
53 NULL, /* flush */
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 */
63 NULL, /* create */
64 proc_lookup, /* lookup */
65 NULL, /* link */
66 NULL, /* unlink */
67 NULL, /* symlink */
68 NULL, /* mkdir */
69 NULL, /* rmdir */
70 NULL, /* mknod */
71 NULL, /* rename */
72 NULL, /* readlink */
73 NULL, /* follow_link */
74 NULL, /* get_block */
75 NULL, /* readpage */
76 NULL, /* writepage */
77 NULL, /* flushpage */
78 NULL, /* truncate */
79 NULL, /* permission */
80 NULL, /* smap */
81 NULL /* revalidate */
85 * /proc dynamic directories now support unlinking
87 struct inode_operations proc_dyna_dir_inode_operations = {
88 &proc_dir_operations, /* default proc dir ops */
89 NULL, /* create */
90 proc_lookup, /* lookup */
91 NULL, /* link */
92 proc_unlink, /* unlink(struct inode *, struct dentry *) */
93 NULL, /* symlink */
94 NULL, /* mkdir */
95 NULL, /* rmdir */
96 NULL, /* mknod */
97 NULL, /* rename */
98 NULL, /* readlink */
99 NULL, /* follow_link */
100 NULL, /* get_block */
101 NULL, /* readpage */
102 NULL, /* writepage */
103 NULL, /* flushpage */
104 NULL, /* truncate */
105 NULL, /* permission */
106 NULL, /* smap */
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 */
122 NULL, /* mmap */
123 NULL, /* no special open code */
124 NULL, /* flush */
125 NULL, /* no special release code */
126 NULL /* no fsync */
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 */
134 NULL, /* create */
135 proc_root_lookup, /* lookup */
136 NULL, /* link */
137 NULL, /* unlink */
138 NULL, /* symlink */
139 NULL, /* mkdir */
140 NULL, /* rmdir */
141 NULL, /* mknod */
142 NULL, /* rename */
143 NULL, /* readlink */
144 NULL, /* follow_link */
145 NULL, /* get_block */
146 NULL, /* readpage */
147 NULL, /* writepage */
148 NULL, /* flushpage */
149 NULL, /* truncate */
150 NULL, /* permission */
151 NULL, /* smap */
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,
162 NULL, NULL,
163 NULL,
164 &proc_root, NULL
167 struct proc_dir_entry *proc_net, *proc_scsi, *proc_bus, *proc_sysvipc;
169 #ifdef CONFIG_MCA
170 struct proc_dir_entry proc_mca = {
171 PROC_MCA, 3, "mca",
172 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
173 0, &proc_dir_inode_operations,
174 NULL, NULL,
175 NULL, &proc_root, NULL
177 #endif
179 #ifdef CONFIG_SYSCTL
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 */
185 NULL, /* next */
186 NULL, NULL /* parent, subdir */
188 #endif
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)
216 return -1;
217 d->next = proc_openprom_devices;
218 d->inode = proc_openpromdev_ino++;
219 proc_openprom_devices = d;
220 return 0;
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)
228 return -1;
229 else {
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;
234 p->next = d->next;
236 return 0;
239 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
240 void
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;
247 #endif
249 #if defined(CONFIG_SUN_OPENPROMFS_MODULE) && defined(CONFIG_KMOD)
250 static int
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);
258 return -EINVAL;
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
269 (dir, dentry);
270 return ERR_PTR(-ENOENT);
272 #define OPENPROM_DEFLOOKUP proc_openprom_deflookup
273 #else
274 #define OPENPROM_DEFREADDIR NULL
275 #define OPENPROM_DEFLOOKUP NULL
276 #endif
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 */
285 NULL, /* mmap */
286 NULL, /* no special open code */
287 NULL, /* flush */
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 */
294 NULL, /* create */
295 OPENPROM_DEFLOOKUP, /* lookup */
296 NULL, /* link */
297 NULL, /* unlink */
298 NULL, /* symlink */
299 NULL, /* mkdir */
300 NULL, /* rmdir */
301 NULL, /* mknod */
302 NULL, /* rename */
303 NULL, /* readlink */
304 NULL, /* follow_link */
305 NULL, /* get_block */
306 NULL, /* readpage */
307 NULL, /* writepage */
308 NULL, /* flushpage */
309 NULL, /* truncate */
310 NULL, /* permission */
311 NULL, /* smap */
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,
319 NULL, NULL,
320 NULL,
321 &proc_root, NULL
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)
331 return -1;
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)
338 int i;
340 if (dp->low_ino == 0) {
341 i = make_inode_number();
342 if (i < 0)
343 return -EAGAIN;
344 dp->low_ino = i;
346 dp->next = dir->subdir;
347 dp->parent = dir;
348 dir->subdir = dp;
349 if (S_ISDIR(dp->mode)) {
350 if (dp->ops == NULL)
351 dp->ops = &proc_dir_inode_operations;
352 dir->nlink++;
353 } else if (S_ISLNK(dp->mode)) {
354 if (dp->ops == NULL)
355 dp->ops = &proc_link_inode_operations;
356 } else {
357 if (dp->ops == NULL)
358 dp->ops = &proc_file_inode_operations;
360 return 0;
364 * Kill an inode that got unregistered..
366 static void proc_kill_inodes(int ino)
368 struct file *filp;
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;
376 if (!dentry)
377 continue;
378 if (dentry->d_op != &proc_dentry_operations)
379 continue;
380 inode = dentry->d_inode;
381 if (!inode)
382 continue;
383 if (inode->i_ino != ino)
384 continue;
385 filp->f_op = NULL;
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) {
395 *p = dp->next;
396 dp->next = NULL;
397 if (S_ISDIR(dp->mode))
398 dir->nlink--;
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);
404 return 0;
406 p = &dp->next;
408 return -EINVAL;
412 * /proc/self:
414 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
416 int len;
417 char tmp[30];
419 len = sprintf(tmp, "%d", current->pid);
420 if (buflen < len)
421 len = buflen;
422 copy_to_user(buffer, tmp, len);
423 return len;
426 static struct dentry * proc_self_follow_link(struct dentry *dentry,
427 struct dentry *base,
428 unsigned int follow)
430 char tmp[30];
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;
440 char *page;
441 int len = 0;
443 de = (struct proc_dir_entry *) inode->u.generic_ip;
444 if (!de)
445 return -ENOENT;
446 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
447 return -ENOMEM;
449 if (de->readlink_proc)
450 len = de->readlink_proc(de, page);
452 if (len > buflen)
453 len = buflen;
455 copy_to_user(buffer, page, len);
456 free_page((unsigned long) page);
457 return len;
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;
464 char *page;
465 struct dentry *d;
466 int len = 0;
468 de = (struct proc_dir_entry *) inode->u.generic_ip;
469 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
470 return NULL;
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);
477 return d;
480 static struct inode_operations proc_self_inode_operations = {
481 NULL, /* no file-ops */
482 NULL, /* create */
483 NULL, /* lookup */
484 NULL, /* link */
485 NULL, /* unlink */
486 NULL, /* symlink */
487 NULL, /* mkdir */
488 NULL, /* rmdir */
489 NULL, /* mknod */
490 NULL, /* rename */
491 proc_self_readlink, /* readlink */
492 proc_self_follow_link, /* follow_link */
493 NULL, /* get_block */
494 NULL, /* readpage */
495 NULL, /* writepage */
496 NULL, /* flushpage */
497 NULL, /* truncate */
498 NULL, /* permission */
499 NULL, /* smap */
500 NULL /* revalidate */
503 static struct inode_operations proc_link_inode_operations = {
504 NULL, /* no file-ops */
505 NULL, /* create */
506 NULL, /* lookup */
507 NULL, /* link */
508 NULL, /* unlink */
509 NULL, /* symlink */
510 NULL, /* mkdir */
511 NULL, /* rmdir */
512 NULL, /* mknod */
513 NULL, /* rename */
514 proc_readlink, /* readlink */
515 proc_follow_link, /* follow_link */
516 NULL, /* get_block */
517 NULL, /* readpage */
518 NULL, /* writepage */
519 NULL, /* flushpage */
520 NULL, /* truncate */
521 NULL, /* permission */
522 NULL, /* smap */
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
562 #endif
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
569 #endif
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
581 #endif
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
598 #endif
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 = {
625 PROC_FS, 2, "fs",
626 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
627 0, &proc_dir_inode_operations,
628 NULL, NULL,
629 NULL,
630 NULL, NULL
632 static struct proc_dir_entry proc_root_dma = {
633 PROC_DMA, 3, "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
652 #ifdef CONFIG_RTC
653 static struct proc_dir_entry proc_root_rtc = {
654 PROC_RTC, 3, "rtc",
655 S_IFREG | S_IRUGO, 1, 0, 0,
656 0, &proc_array_inode_operations
658 #endif
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
684 #ifdef __powerpc__
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 */
690 NULL, /* next */
691 NULL, NULL /* parent, subdir */
693 #endif
695 __initfunc(void proc_root_init(void))
697 proc_base_init();
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);
709 #endif
710 #ifdef CONFIG_SYSCTL
711 proc_register(&proc_root, &proc_sys_root);
712 #endif
713 #ifdef CONFIG_MCA
714 proc_register(&proc_root, &proc_mca);
715 #endif
717 #ifdef CONFIG_DEBUG_MALLOC
718 proc_register(&proc_root, &proc_root_malloc);
719 #endif
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);
726 #endif
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);
737 #ifdef CONFIG_RTC
738 proc_register(&proc_root, &proc_root_rtc);
739 #endif
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
747 openpromfs_init ();
748 #endif
749 proc_register(&proc_root, &proc_openprom);
750 #endif
751 #ifdef CONFIG_PROC_HARDWARE
752 proc_register(&proc_root, &proc_root_hardware);
753 #endif
754 #ifdef CONFIG_STRAM_PROC
755 proc_register(&proc_root, &proc_root_stram);
756 #endif
757 proc_register(&proc_root, &proc_root_slab);
759 if (prof_shift) {
760 proc_register(&proc_root, &proc_root_profile);
761 proc_root_profile.size = (1+prof_len) * sizeof(unsigned int);
764 proc_tty_init();
765 #ifdef __powerpc__
766 proc_register(&proc_root, &proc_root_ppc_htab);
767 #endif
768 #ifdef CONFIG_PROC_DEVICETREE
769 proc_device_tree_init();
770 #endif
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.
781 static void
782 proc_delete_dentry(struct dentry * dentry)
784 d_drop(dentry);
787 struct dentry_operations proc_dentry_operations =
789 NULL, /* revalidate */
790 NULL, /* d_hash */
791 NULL, /* d_compare */
792 proc_delete_dentry /* d_delete(struct dentry *) */
796 * Don't create negative dentries here, return -ENOENT by hand
797 * instead.
799 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry)
801 struct inode *inode;
802 struct proc_dir_entry * de;
803 int error;
805 error = -ENOENT;
806 inode = NULL;
807 de = (struct proc_dir_entry *) dir->u.generic_ip;
808 if (de) {
809 for (de = de->subdir; de ; de = de->next) {
810 if (!de || !de->low_ino)
811 continue;
812 if (de->namelen != dentry->d_name.len)
813 continue;
814 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
815 int ino = de->low_ino | (dir->i_ino & ~(0xffff));
816 error = -EINVAL;
817 inode = proc_get_inode(dir->i_sb, ino, de);
818 break;
823 if (inode) {
824 dentry->d_op = &proc_dentry_operations;
825 d_add(dentry, inode);
826 return NULL;
828 return ERR_PTR(error);
831 static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry)
833 unsigned int pid, c;
834 struct task_struct *p;
835 const char *name;
836 struct inode *inode;
837 int len;
839 if (dir->i_ino == PROC_ROOT_INO) { /* check for safety... */
840 dir->i_nlink = proc_root.nlink;
842 read_lock(&tasklist_lock);
843 for_each_task(p) {
844 if (p->pid)
845 dir->i_nlink++;
847 read_unlock(&tasklist_lock);
850 if (!proc_lookup(dir, dentry))
851 return NULL;
853 pid = 0;
854 name = dentry->d_name.name;
855 len = dentry->d_name.len;
856 while (len-- > 0) {
857 c = *name - '0';
858 name++;
859 if (c > 9) {
860 pid = 0;
861 break;
863 pid *= 10;
864 pid += c;
865 if (pid & 0xffff0000) {
866 pid = 0;
867 break;
870 read_lock(&tasklist_lock);
871 p = find_task_by_pid(pid);
872 read_unlock(&tasklist_lock);
873 inode = NULL;
874 if (pid && p) {
875 unsigned long ino = (pid << 16) + PROC_PID_INO;
876 inode = proc_get_inode(dir->i_sb, ino, &proc_pid);
877 if (!inode)
878 return ERR_PTR(-EINVAL);
879 inode->i_flags|=S_IMMUTABLE;
882 dentry->d_op = &proc_dentry_operations;
883 d_add(dentry, inode);
884 return NULL;
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
894 * for success..
896 int proc_readdir(struct file * filp,
897 void * dirent, filldir_t filldir)
899 struct proc_dir_entry * de;
900 unsigned int ino;
901 int i;
902 struct inode *inode = filp->f_dentry->d_inode;
904 ino = inode->i_ino;
905 de = (struct proc_dir_entry *) inode->u.generic_ip;
906 if (!de)
907 return -EINVAL;
908 i = filp->f_pos;
909 switch (i) {
910 case 0:
911 if (filldir(dirent, ".", 1, i, ino) < 0)
912 return 0;
913 i++;
914 filp->f_pos++;
915 /* fall through */
916 case 1:
917 if (filldir(dirent, "..", 2, i, de->parent->low_ino) < 0)
918 return 0;
919 i++;
920 filp->f_pos++;
921 /* fall through */
922 default:
923 ino &= ~0xffff;
924 de = de->subdir;
925 i -= 2;
926 for (;;) {
927 if (!de)
928 return 1;
929 if (!i)
930 break;
931 de = de->next;
932 i--;
935 do {
936 if (filldir(dirent, de->name, de->namelen, filp->f_pos, ino | de->low_ino) < 0)
937 return 0;
938 filp->f_pos++;
939 de = de->next;
940 } while (de);
942 return 1;
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;
956 int nr_pids = 0;
958 index -= FIRST_PROCESS_ENTRY;
959 read_lock(&tasklist_lock);
960 for_each_task(p) {
961 int pid = p->pid;
962 if (!pid)
963 continue;
964 if (--index >= 0)
965 continue;
966 pids[nr_pids] = pid;
967 nr_pids++;
968 if (nr_pids >= PROC_MAXPIDS)
969 break;
971 read_unlock(&tasklist_lock);
972 return nr_pids;
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);
985 if (error <= 0)
986 return error;
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;
997 do {
998 j--;
999 buf[j] = '0' + (pid % 10);
1000 pid /= 10;
1001 } while (pid);
1003 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino) < 0)
1004 break;
1005 filp->f_pos++;
1007 return 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;
1018 d_delete(dentry);
1019 return 0;