Import 2.1.116pre2
[davej-history.git] / fs / proc / root.c
blobe74e91366622a2061fca6880ca3fd26bc9f3df85
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 int 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, /* no special release code */
54 NULL /* can't fsync */
57 int proc_readlink(struct dentry * dentry, char * buffer, int buflen);
58 struct dentry * proc_follow_link(struct dentry *dentry, struct dentry *base);
61 * proc directories can do almost nothing..
63 struct inode_operations proc_dir_inode_operations = {
64 &proc_dir_operations, /* default net directory file-ops */
65 NULL, /* create */
66 proc_lookup, /* lookup */
67 NULL, /* link */
68 NULL, /* unlink */
69 NULL, /* symlink */
70 NULL, /* mkdir */
71 NULL, /* rmdir */
72 NULL, /* mknod */
73 NULL, /* rename */
74 NULL, /* readlink */
75 NULL, /* follow_link */
76 NULL, /* readpage */
77 NULL, /* writepage */
78 NULL, /* bmap */
79 NULL, /* truncate */
80 NULL /* permission */
84 * /proc dynamic directories now support unlinking
86 struct inode_operations proc_dyna_dir_inode_operations = {
87 &proc_dir_operations, /* default proc dir ops */
88 NULL, /* create */
89 proc_lookup, /* lookup */
90 NULL, /* link */
91 proc_unlink, /* unlink(struct inode *, struct dentry *) */
92 NULL, /* symlink */
93 NULL, /* mkdir */
94 NULL, /* rmdir */
95 NULL, /* mknod */
96 NULL, /* rename */
97 NULL, /* readlink */
98 NULL, /* follow_link */
99 NULL, /* readpage */
100 NULL, /* writepage */
101 NULL, /* bmap */
102 NULL, /* truncate */
103 NULL /* permission */
107 * The root /proc directory is special, as it has the
108 * <pid> directories. Thus we don't use the generic
109 * directory handling functions for that..
111 static struct file_operations proc_root_operations = {
112 NULL, /* lseek - default */
113 NULL, /* read - bad */
114 NULL, /* write - bad */
115 proc_root_readdir, /* readdir */
116 NULL, /* poll - default */
117 NULL, /* ioctl - default */
118 NULL, /* mmap */
119 NULL, /* no special open code */
120 NULL, /* no special release code */
121 NULL /* no fsync */
125 * proc root can do almost nothing..
127 static struct inode_operations proc_root_inode_operations = {
128 &proc_root_operations, /* default base directory file-ops */
129 NULL, /* create */
130 proc_root_lookup, /* lookup */
131 NULL, /* link */
132 NULL, /* unlink */
133 NULL, /* symlink */
134 NULL, /* mkdir */
135 NULL, /* rmdir */
136 NULL, /* mknod */
137 NULL, /* rename */
138 NULL, /* readlink */
139 NULL, /* follow_link */
140 NULL, /* readpage */
141 NULL, /* writepage */
142 NULL, /* bmap */
143 NULL, /* truncate */
144 NULL /* permission */
148 * This is the root "inode" in the /proc tree..
150 struct proc_dir_entry proc_root = {
151 PROC_ROOT_INO, 5, "/proc",
152 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
153 0, &proc_root_inode_operations,
154 NULL, NULL,
155 NULL,
156 &proc_root, NULL
159 struct proc_dir_entry *proc_net, *proc_scsi, *proc_bus;
161 #ifdef CONFIG_MCA
162 struct proc_dir_entry proc_mca = {
163 PROC_MCA, 3, "mca",
164 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
165 0, &proc_dir_inode_operations,
166 NULL, NULL,
167 NULL, &proc_root, NULL
169 #endif
171 #ifdef CONFIG_SYSCTL
172 struct proc_dir_entry proc_sys_root = {
173 PROC_SYS, 3, "sys", /* inode, name */
174 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0, /* mode, nlink, uid, gid */
175 0, &proc_dir_inode_operations, /* size, ops */
176 NULL, NULL, /* get_info, fill_inode */
177 NULL, /* next */
178 NULL, NULL /* parent, subdir */
180 #endif
182 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
184 static int (*proc_openprom_defreaddir_ptr)(struct file *, void *, filldir_t);
185 static int (*proc_openprom_deflookup_ptr)(struct inode *, struct dentry *);
186 void (*proc_openprom_use)(struct inode *, int) = 0;
187 static struct openpromfs_dev *proc_openprom_devices = NULL;
188 static ino_t proc_openpromdev_ino = PROC_OPENPROMD_FIRST;
190 struct inode_operations *
191 proc_openprom_register(int (*readdir)(struct file *, void *, filldir_t),
192 int (*lookup)(struct inode *, struct dentry *),
193 void (*use)(struct inode *, int),
194 struct openpromfs_dev ***devices)
196 proc_openprom_defreaddir_ptr = (proc_openprom_inode_operations.default_file_ops)->readdir;
197 proc_openprom_deflookup_ptr = proc_openprom_inode_operations.lookup;
198 (proc_openprom_inode_operations.default_file_ops)->readdir = readdir;
199 proc_openprom_inode_operations.lookup = lookup;
200 proc_openprom_use = use;
201 *devices = &proc_openprom_devices;
202 return &proc_openprom_inode_operations;
205 int proc_openprom_regdev(struct openpromfs_dev *d)
207 if (proc_openpromdev_ino == PROC_OPENPROMD_FIRST + PROC_NOPENPROMD)
208 return -1;
209 d->next = proc_openprom_devices;
210 d->inode = proc_openpromdev_ino++;
211 proc_openprom_devices = d;
212 return 0;
215 int proc_openprom_unregdev(struct openpromfs_dev *d)
217 if (d == proc_openprom_devices) {
218 proc_openprom_devices = d->next;
219 } else if (!proc_openprom_devices)
220 return -1;
221 else {
222 struct openpromfs_dev *p;
224 for (p = proc_openprom_devices; p->next != d && p->next; p = p->next);
225 if (!p->next) return -1;
226 p->next = d->next;
228 return 0;
231 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
232 void
233 proc_openprom_deregister(void)
235 (proc_openprom_inode_operations.default_file_ops)->readdir = proc_openprom_defreaddir_ptr;
236 proc_openprom_inode_operations.lookup = proc_openprom_deflookup_ptr;
237 proc_openprom_use = 0;
239 #endif
241 #if defined(CONFIG_SUN_OPENPROMFS_MODULE) && defined(CONFIG_KMOD)
242 static int
243 proc_openprom_defreaddir(struct file * filp, void * dirent, filldir_t filldir)
245 request_module("openpromfs");
246 if ((proc_openprom_inode_operations.default_file_ops)->readdir !=
247 proc_openprom_defreaddir)
248 return (proc_openprom_inode_operations.default_file_ops)->readdir
249 (filp, dirent, filldir);
250 return -EINVAL;
252 #define OPENPROM_DEFREADDIR proc_openprom_defreaddir
254 static int
255 proc_openprom_deflookup(struct inode * dir, struct dentry *dentry)
257 request_module("openpromfs");
258 if (proc_openprom_inode_operations.lookup !=
259 proc_openprom_deflookup)
260 return proc_openprom_inode_operations.lookup
261 (dir, dentry);
262 return -ENOENT;
264 #define OPENPROM_DEFLOOKUP proc_openprom_deflookup
265 #else
266 #define OPENPROM_DEFREADDIR NULL
267 #define OPENPROM_DEFLOOKUP NULL
268 #endif
270 static struct file_operations proc_openprom_operations = {
271 NULL, /* lseek - default */
272 NULL, /* read - bad */
273 NULL, /* write - bad */
274 OPENPROM_DEFREADDIR, /* readdir */
275 NULL, /* poll - default */
276 NULL, /* ioctl - default */
277 NULL, /* mmap */
278 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 */
285 NULL, /* create */
286 OPENPROM_DEFLOOKUP, /* lookup */
287 NULL, /* link */
288 NULL, /* unlink */
289 NULL, /* symlink */
290 NULL, /* mkdir */
291 NULL, /* rmdir */
292 NULL, /* mknod */
293 NULL, /* rename */
294 NULL, /* readlink */
295 NULL, /* follow_link */
296 NULL, /* readpage */
297 NULL, /* writepage */
298 NULL, /* bmap */
299 NULL, /* truncate */
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,
307 NULL, NULL,
308 NULL,
309 &proc_root, NULL
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)
319 return -1;
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)
326 int i;
328 if (dp->low_ino == 0) {
329 i = make_inode_number();
330 if (i < 0)
331 return -EAGAIN;
332 dp->low_ino = i;
334 dp->next = dir->subdir;
335 dp->parent = dir;
336 dir->subdir = dp;
337 if (S_ISDIR(dp->mode)) {
338 if (dp->ops == NULL)
339 dp->ops = &proc_dir_inode_operations;
340 dir->nlink++;
341 } else if (S_ISLNK(dp->mode)) {
342 if (dp->ops == NULL)
343 dp->ops = &proc_link_inode_operations;
344 } else {
345 if (dp->ops == NULL)
346 dp->ops = &proc_file_inode_operations;
348 return 0;
351 int proc_unregister(struct proc_dir_entry * dir, int ino)
353 struct proc_dir_entry **p = &dir->subdir, *dp;
355 while ((dp = *p) != NULL) {
356 if (dp->low_ino == ino) {
357 *p = dp->next;
358 dp->next = NULL;
359 if (S_ISDIR(dp->mode))
360 dir->nlink--;
361 if (ino >= PROC_DYNAMIC_FIRST &&
362 ino < PROC_DYNAMIC_FIRST+PROC_NDYNAMIC)
363 clear_bit(ino-PROC_DYNAMIC_FIRST,
364 (void *) proc_alloc_map);
365 return 0;
367 p = &dp->next;
369 return -EINVAL;
373 * /proc/self:
375 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
377 int len;
378 char tmp[30];
380 len = sprintf(tmp, "%d", current->pid);
381 if (buflen < len)
382 len = buflen;
383 copy_to_user(buffer, tmp, len);
384 return len;
387 static struct dentry * proc_self_follow_link(struct dentry *dentry,
388 struct dentry *base)
390 char tmp[30];
392 sprintf(tmp, "%d", current->pid);
393 return lookup_dentry(tmp, base, 1);
396 int proc_readlink(struct dentry * dentry, char * buffer, int buflen)
398 struct inode *inode = dentry->d_inode;
399 struct proc_dir_entry * de;
400 char *page;
401 int len = 0;
403 de = (struct proc_dir_entry *) inode->u.generic_ip;
404 if (!de)
405 return -ENOENT;
406 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
407 return -ENOMEM;
409 if (de->readlink_proc)
410 len = de->readlink_proc(de, page);
412 if (len > buflen)
413 len = buflen;
415 copy_to_user(buffer, page, len);
416 free_page((unsigned long) page);
417 return len;
420 struct dentry * proc_follow_link(struct dentry * dentry, struct dentry *base)
422 struct inode *inode = dentry->d_inode;
423 struct proc_dir_entry * de;
424 char *page;
425 struct dentry *d;
426 int len = 0;
428 de = (struct proc_dir_entry *) inode->u.generic_ip;
429 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
430 return NULL;
432 if (de->readlink_proc)
433 len = de->readlink_proc(de, page);
435 d = lookup_dentry(page, base, 1);
436 free_page((unsigned long) page);
437 return d;
440 static struct inode_operations proc_self_inode_operations = {
441 NULL, /* no file-ops */
442 NULL, /* create */
443 NULL, /* lookup */
444 NULL, /* link */
445 NULL, /* unlink */
446 NULL, /* symlink */
447 NULL, /* mkdir */
448 NULL, /* rmdir */
449 NULL, /* mknod */
450 NULL, /* rename */
451 proc_self_readlink, /* readlink */
452 proc_self_follow_link, /* follow_link */
453 NULL, /* readpage */
454 NULL, /* writepage */
455 NULL, /* bmap */
456 NULL, /* truncate */
457 NULL /* permission */
460 static struct inode_operations proc_link_inode_operations = {
461 NULL, /* no file-ops */
462 NULL, /* create */
463 NULL, /* lookup */
464 NULL, /* link */
465 NULL, /* unlink */
466 NULL, /* symlink */
467 NULL, /* mkdir */
468 NULL, /* rmdir */
469 NULL, /* mknod */
470 NULL, /* rename */
471 proc_readlink, /* readlink */
472 proc_follow_link, /* follow_link */
473 NULL, /* readpage */
474 NULL, /* writepage */
475 NULL, /* bmap */
476 NULL, /* truncate */
477 NULL /* permission */
480 static struct proc_dir_entry proc_root_loadavg = {
481 PROC_LOADAVG, 7, "loadavg",
482 S_IFREG | S_IRUGO, 1, 0, 0,
483 0, &proc_array_inode_operations
485 static struct proc_dir_entry proc_root_uptime = {
486 PROC_UPTIME, 6, "uptime",
487 S_IFREG | S_IRUGO, 1, 0, 0,
488 0, &proc_array_inode_operations
490 static struct proc_dir_entry proc_root_meminfo = {
491 PROC_MEMINFO, 7, "meminfo",
492 S_IFREG | S_IRUGO, 1, 0, 0,
493 0, &proc_array_inode_operations
495 static struct proc_dir_entry proc_root_kmsg = {
496 PROC_KMSG, 4, "kmsg",
497 S_IFREG | S_IRUSR, 1, 0, 0,
498 0, &proc_kmsg_inode_operations
500 static struct proc_dir_entry proc_root_version = {
501 PROC_VERSION, 7, "version",
502 S_IFREG | S_IRUGO, 1, 0, 0,
503 0, &proc_array_inode_operations
505 static struct proc_dir_entry proc_root_cpuinfo = {
506 PROC_CPUINFO, 7, "cpuinfo",
507 S_IFREG | S_IRUGO, 1, 0, 0,
508 0, &proc_array_inode_operations
510 #if defined (CONFIG_PROC_HARDWARE)
511 static struct proc_dir_entry proc_root_hardware = {
512 PROC_HARDWARE, 8, "hardware",
513 S_IFREG | S_IRUGO, 1, 0, 0,
514 0, &proc_array_inode_operations
516 #endif
517 #ifdef CONFIG_STRAM_PROC
518 static struct proc_dir_entry proc_root_stram = {
519 PROC_STRAM, 5, "stram",
520 S_IFREG | S_IRUGO, 1, 0, 0,
521 0, &proc_array_inode_operations
523 #endif
524 static struct proc_dir_entry proc_root_self = {
525 PROC_SELF, 4, "self",
526 S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO, 1, 0, 0,
527 64, &proc_self_inode_operations,
529 #ifdef CONFIG_DEBUG_MALLOC
530 static struct proc_dir_entry proc_root_malloc = {
531 PROC_MALLOC, 6, "malloc",
532 S_IFREG | S_IRUGO, 1, 0, 0,
533 0, &proc_array_inode_operations
535 #endif
536 static struct proc_dir_entry proc_root_kcore = {
537 PROC_KCORE, 5, "kcore",
538 S_IFREG | S_IRUSR, 1, 0, 0,
539 0, &proc_kcore_inode_operations
541 #ifdef CONFIG_MODULES
542 static struct proc_dir_entry proc_root_modules = {
543 PROC_MODULES, 7, "modules",
544 S_IFREG | S_IRUGO, 1, 0, 0,
545 0, &proc_array_inode_operations
547 static struct proc_dir_entry proc_root_ksyms = {
548 PROC_KSYMS, 5, "ksyms",
549 S_IFREG | S_IRUGO, 1, 0, 0,
550 0, &proc_array_inode_operations
552 #endif
553 static struct proc_dir_entry proc_root_stat = {
554 PROC_STAT, 4, "stat",
555 S_IFREG | S_IRUGO, 1, 0, 0,
556 0, &proc_array_inode_operations
558 static struct proc_dir_entry proc_root_devices = {
559 PROC_DEVICES, 7, "devices",
560 S_IFREG | S_IRUGO, 1, 0, 0,
561 0, &proc_array_inode_operations
563 static struct proc_dir_entry proc_root_partitions = {
564 PROC_PARTITIONS, 10, "partitions",
565 S_IFREG | S_IRUGO, 1, 0, 0,
566 0, &proc_array_inode_operations
568 static struct proc_dir_entry proc_root_interrupts = {
569 PROC_INTERRUPTS, 10,"interrupts",
570 S_IFREG | S_IRUGO, 1, 0, 0,
571 0, &proc_array_inode_operations
573 static struct proc_dir_entry proc_root_filesystems = {
574 PROC_FILESYSTEMS, 11,"filesystems",
575 S_IFREG | S_IRUGO, 1, 0, 0,
576 0, &proc_array_inode_operations
578 struct proc_dir_entry proc_root_fs = {
579 PROC_FS, 2, "fs",
580 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
581 0, &proc_dir_inode_operations,
582 NULL, NULL,
583 NULL,
584 NULL, NULL
586 static struct proc_dir_entry proc_root_dma = {
587 PROC_DMA, 3, "dma",
588 S_IFREG | S_IRUGO, 1, 0, 0,
589 0, &proc_array_inode_operations
591 static struct proc_dir_entry proc_root_ioports = {
592 PROC_IOPORTS, 7, "ioports",
593 S_IFREG | S_IRUGO, 1, 0, 0,
594 0, &proc_array_inode_operations
596 static struct proc_dir_entry proc_root_cmdline = {
597 PROC_CMDLINE, 7, "cmdline",
598 S_IFREG | S_IRUGO, 1, 0, 0,
599 0, &proc_array_inode_operations
601 #ifdef CONFIG_RTC
602 static struct proc_dir_entry proc_root_rtc = {
603 PROC_RTC, 3, "rtc",
604 S_IFREG | S_IRUGO, 1, 0, 0,
605 0, &proc_array_inode_operations
607 #endif
608 static struct proc_dir_entry proc_root_locks = {
609 PROC_LOCKS, 5, "locks",
610 S_IFREG | S_IRUGO, 1, 0, 0,
611 0, &proc_array_inode_operations
613 static struct proc_dir_entry proc_root_mounts = {
614 PROC_MTAB, 6, "mounts",
615 S_IFREG | S_IRUGO, 1, 0, 0,
616 0, &proc_array_inode_operations
618 static struct proc_dir_entry proc_root_swaps = {
619 PROC_SWAP, 5, "swaps",
620 S_IFREG | S_IRUGO, 1, 0, 0,
621 0, &proc_array_inode_operations
623 static struct proc_dir_entry proc_root_profile = {
624 PROC_PROFILE, 7, "profile",
625 S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0,
626 0, &proc_profile_inode_operations
628 static struct proc_dir_entry proc_root_slab = {
629 PROC_SLABINFO, 8, "slabinfo",
630 S_IFREG | S_IRUGO, 1, 0, 0,
631 0, &proc_array_inode_operations
633 #ifdef __powerpc__
634 static struct proc_dir_entry proc_root_ppc_htab = {
635 PROC_PPC_HTAB, 8, "ppc_htab",
636 S_IFREG | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 1, 0, 0,
637 0, &proc_ppc_htab_inode_operations,
638 NULL, NULL, /* get_info, fill_inode */
639 NULL, /* next */
640 NULL, NULL /* parent, subdir */
642 #endif
644 __initfunc(void proc_root_init(void))
646 proc_base_init();
647 proc_register(&proc_root, &proc_root_loadavg);
648 proc_register(&proc_root, &proc_root_uptime);
649 proc_register(&proc_root, &proc_root_meminfo);
650 proc_register(&proc_root, &proc_root_kmsg);
651 proc_register(&proc_root, &proc_root_version);
652 proc_register(&proc_root, &proc_root_cpuinfo);
653 proc_register(&proc_root, &proc_root_self);
654 proc_net = create_proc_entry("net", S_IFDIR, 0);
655 proc_scsi = create_proc_entry("scsi", S_IFDIR, 0);
656 #ifdef CONFIG_SYSCTL
657 proc_register(&proc_root, &proc_sys_root);
658 #endif
659 #ifdef CONFIG_MCA
660 proc_register(&proc_root, &proc_mca);
661 #endif
663 #ifdef CONFIG_DEBUG_MALLOC
664 proc_register(&proc_root, &proc_root_malloc);
665 #endif
666 proc_register(&proc_root, &proc_root_kcore);
667 proc_root_kcore.size = (MAP_NR(high_memory) << PAGE_SHIFT) + PAGE_SIZE;
669 #ifdef CONFIG_MODULES
670 proc_register(&proc_root, &proc_root_modules);
671 proc_register(&proc_root, &proc_root_ksyms);
672 #endif
673 proc_register(&proc_root, &proc_root_stat);
674 proc_register(&proc_root, &proc_root_devices);
675 proc_register(&proc_root, &proc_root_partitions);
676 proc_register(&proc_root, &proc_root_interrupts);
677 proc_register(&proc_root, &proc_root_filesystems);
678 proc_register(&proc_root, &proc_root_fs);
679 proc_register(&proc_root, &proc_root_dma);
680 proc_register(&proc_root, &proc_root_ioports);
681 proc_register(&proc_root, &proc_root_cmdline);
682 #ifdef CONFIG_RTC
683 proc_register(&proc_root, &proc_root_rtc);
684 #endif
685 proc_register(&proc_root, &proc_root_locks);
687 proc_register(&proc_root, &proc_root_mounts);
688 proc_register(&proc_root, &proc_root_swaps);
690 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
691 #ifdef CONFIG_SUN_OPENPROMFS
692 openpromfs_init ();
693 #endif
694 proc_register(&proc_root, &proc_openprom);
695 #endif
696 #ifdef CONFIG_PROC_HARDWARE
697 proc_register(&proc_root, &proc_root_hardware);
698 #endif
699 #ifdef CONFIG_STRAM_PROC
700 proc_register(&proc_root, &proc_root_stram);
701 #endif
702 proc_register(&proc_root, &proc_root_slab);
704 if (prof_shift) {
705 proc_register(&proc_root, &proc_root_profile);
706 proc_root_profile.size = (1+prof_len) * sizeof(unsigned int);
709 proc_tty_init();
710 #ifdef __powerpc__
711 proc_register(&proc_root, &proc_root_ppc_htab);
712 #endif
713 #ifdef CONFIG_PROC_DEVICETREE
714 proc_device_tree_init();
715 #endif
717 proc_bus = create_proc_entry("bus", S_IFDIR, 0);
721 * As some entries in /proc are volatile, we want to
722 * get rid of unused dentries. This could be made
723 * smarter: we could keep a "volatile" flag in the
724 * inode to indicate which ones to keep.
726 static void
727 proc_delete_dentry(struct dentry * dentry)
729 d_drop(dentry);
732 static struct dentry_operations proc_dentry_operations =
734 NULL, /* revalidate */
735 NULL, /* d_hash */
736 NULL, /* d_compare */
737 proc_delete_dentry /* d_delete(struct dentry *) */
741 * Don't create negative dentries here, return -ENOENT by hand
742 * instead.
744 int proc_lookup(struct inode * dir, struct dentry *dentry)
746 struct inode *inode;
747 struct proc_dir_entry * de;
748 int error;
750 error = -ENOTDIR;
751 if (!dir || !S_ISDIR(dir->i_mode))
752 goto out;
754 error = -ENOENT;
755 inode = NULL;
756 de = (struct proc_dir_entry *) dir->u.generic_ip;
757 if (de) {
758 for (de = de->subdir; de ; de = de->next) {
759 if (!de || !de->low_ino)
760 continue;
761 if (de->namelen != dentry->d_name.len)
762 continue;
763 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
764 int ino = de->low_ino | (dir->i_ino & ~(0xffff));
765 error = -EINVAL;
766 inode = proc_get_inode(dir->i_sb, ino, de);
767 break;
772 if (inode) {
773 dentry->d_op = &proc_dentry_operations;
774 d_add(dentry, inode);
775 error = 0;
777 out:
778 return error;
781 static int proc_root_lookup(struct inode * dir, struct dentry * dentry)
783 unsigned int pid, c;
784 struct task_struct *p;
785 const char *name;
786 struct inode *inode;
787 int len;
789 if (dir->i_ino == PROC_ROOT_INO) { /* check for safety... */
790 dir->i_nlink = proc_root.nlink;
792 read_lock(&tasklist_lock);
793 for_each_task(p) {
794 if (p->pid)
795 dir->i_nlink++;
797 read_unlock(&tasklist_lock);
800 if (!proc_lookup(dir, dentry))
801 return 0;
803 pid = 0;
804 name = dentry->d_name.name;
805 len = dentry->d_name.len;
806 while (len-- > 0) {
807 c = *name - '0';
808 name++;
809 if (c > 9) {
810 pid = 0;
811 break;
813 pid *= 10;
814 pid += c;
815 if (pid & 0xffff0000) {
816 pid = 0;
817 break;
820 read_lock(&tasklist_lock);
821 p = find_task_by_pid(pid);
822 read_unlock(&tasklist_lock);
823 inode = NULL;
824 if (pid && p) {
825 unsigned long ino = (pid << 16) + PROC_PID_INO;
826 inode = proc_get_inode(dir->i_sb, ino, &proc_pid);
827 if (!inode)
828 return -EINVAL;
831 dentry->d_op = &proc_dentry_operations;
832 d_add(dentry, inode);
833 return 0;
837 * This returns non-zero if at EOF, so that the /proc
838 * root directory can use this and check if it should
839 * continue with the <pid> entries..
841 * Note that the VFS-layer doesn't care about the return
842 * value of the readdir() call, as long as it's non-negative
843 * for success..
845 int proc_readdir(struct file * filp,
846 void * dirent, filldir_t filldir)
848 struct proc_dir_entry * de;
849 unsigned int ino;
850 int i;
851 struct inode *inode = filp->f_dentry->d_inode;
853 if (!inode || !S_ISDIR(inode->i_mode))
854 return -ENOTDIR;
855 ino = inode->i_ino;
856 de = (struct proc_dir_entry *) inode->u.generic_ip;
857 if (!de)
858 return -EINVAL;
859 i = filp->f_pos;
860 switch (i) {
861 case 0:
862 if (filldir(dirent, ".", 1, i, ino) < 0)
863 return 0;
864 i++;
865 filp->f_pos++;
866 /* fall through */
867 case 1:
868 if (filldir(dirent, "..", 2, i, de->parent->low_ino) < 0)
869 return 0;
870 i++;
871 filp->f_pos++;
872 /* fall through */
873 default:
874 ino &= ~0xffff;
875 de = de->subdir;
876 i -= 2;
877 for (;;) {
878 if (!de)
879 return 1;
880 if (!i)
881 break;
882 de = de->next;
883 i--;
886 do {
887 if (filldir(dirent, de->name, de->namelen, filp->f_pos, ino | de->low_ino) < 0)
888 return 0;
889 filp->f_pos++;
890 de = de->next;
891 } while (de);
893 return 1;
896 #define PROC_NUMBUF 10
897 #define PROC_MAXPIDS 20
900 * Get a few pid's to return for filldir - we need to hold the
901 * tasklist lock while doing this, and we must release it before
902 * we actually do the filldir itself, so we use a temp buffer..
904 static int get_pid_list(unsigned int index, unsigned int *pids)
906 struct task_struct *p;
907 int nr = FIRST_PROCESS_ENTRY;
908 int nr_pids = 0;
910 read_lock(&tasklist_lock);
911 for_each_task(p) {
912 int pid;
913 if (nr++ < index)
914 continue;
915 pid = p->pid;
916 if (!pid)
917 continue;
918 pids[nr_pids] = pid;
919 nr_pids++;
920 if (nr_pids >= PROC_MAXPIDS)
921 break;
923 read_unlock(&tasklist_lock);
924 return nr_pids;
927 static int proc_root_readdir(struct file * filp,
928 void * dirent, filldir_t filldir)
930 unsigned int pid_array[PROC_MAXPIDS];
931 char buf[PROC_NUMBUF];
932 unsigned int nr = filp->f_pos;
933 unsigned int nr_pids, i;
935 if (nr < FIRST_PROCESS_ENTRY) {
936 int error = proc_readdir(filp, dirent, filldir);
937 if (error <= 0)
938 return error;
939 filp->f_pos = nr = FIRST_PROCESS_ENTRY;
942 nr_pids = get_pid_list(nr, pid_array);
944 for (i = 0; i < nr_pids; i++) {
945 int pid = pid_array[i];
946 unsigned long j = PROC_NUMBUF;
948 do {
949 j--;
950 buf[j] = '0' + (pid % 10);
951 pid /= 10;
952 } while (pid);
954 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, (pid << 16) + PROC_PID_INO) < 0)
955 break;
956 filp->f_pos++;
958 return 0;
961 static int proc_unlink(struct inode *dir, struct dentry *dentry)
963 struct proc_dir_entry * dp = dir->u.generic_ip;
965 printk("proc_file_unlink: deleting %s/%s\n", dp->name, dentry->d_name.name);
967 remove_proc_entry(dentry->d_name.name, dp);
968 dentry->d_inode->i_nlink = 0;
969 d_delete(dentry);
970 return 0;