Linux 2.2.0
[davej-history.git] / fs / proc / root.c
blobe28338ab4336054b45097272a0bb139dceb52d69
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, /* 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, /* readpage */
75 NULL, /* writepage */
76 NULL, /* bmap */
77 NULL, /* truncate */
78 NULL /* permission */
82 * /proc dynamic directories now support unlinking
84 struct inode_operations proc_dyna_dir_inode_operations = {
85 &proc_dir_operations, /* default proc dir ops */
86 NULL, /* create */
87 proc_lookup, /* lookup */
88 NULL, /* link */
89 proc_unlink, /* unlink(struct inode *, struct dentry *) */
90 NULL, /* symlink */
91 NULL, /* mkdir */
92 NULL, /* rmdir */
93 NULL, /* mknod */
94 NULL, /* rename */
95 NULL, /* readlink */
96 NULL, /* follow_link */
97 NULL, /* readpage */
98 NULL, /* writepage */
99 NULL, /* bmap */
100 NULL, /* truncate */
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 */
116 NULL, /* mmap */
117 NULL, /* no special open code */
118 NULL, /* flush */
119 NULL, /* no special release code */
120 NULL /* no fsync */
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 */
128 NULL, /* create */
129 proc_root_lookup, /* lookup */
130 NULL, /* link */
131 NULL, /* unlink */
132 NULL, /* symlink */
133 NULL, /* mkdir */
134 NULL, /* rmdir */
135 NULL, /* mknod */
136 NULL, /* rename */
137 NULL, /* readlink */
138 NULL, /* follow_link */
139 NULL, /* readpage */
140 NULL, /* writepage */
141 NULL, /* bmap */
142 NULL, /* truncate */
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,
153 NULL, NULL,
154 NULL,
155 &proc_root, NULL
158 struct proc_dir_entry *proc_net, *proc_scsi, *proc_bus;
160 #ifdef CONFIG_MCA
161 struct proc_dir_entry proc_mca = {
162 PROC_MCA, 3, "mca",
163 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
164 0, &proc_dir_inode_operations,
165 NULL, NULL,
166 NULL, &proc_root, NULL
168 #endif
170 #ifdef CONFIG_SYSCTL
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 */
176 NULL, /* next */
177 NULL, NULL /* parent, subdir */
179 #endif
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 int (*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 int (*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)
207 return -1;
208 d->next = proc_openprom_devices;
209 d->inode = proc_openpromdev_ino++;
210 proc_openprom_devices = d;
211 return 0;
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)
219 return -1;
220 else {
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;
225 p->next = d->next;
227 return 0;
230 #ifdef CONFIG_SUN_OPENPROMFS_MODULE
231 void
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;
238 #endif
240 #if defined(CONFIG_SUN_OPENPROMFS_MODULE) && defined(CONFIG_KMOD)
241 static int
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);
249 return -EINVAL;
251 #define OPENPROM_DEFREADDIR proc_openprom_defreaddir
253 static int
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
260 (dir, dentry);
261 return -ENOENT;
263 #define OPENPROM_DEFLOOKUP proc_openprom_deflookup
264 #else
265 #define OPENPROM_DEFREADDIR NULL
266 #define OPENPROM_DEFLOOKUP NULL
267 #endif
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 */
276 NULL, /* mmap */
277 NULL, /* no special open code */
278 NULL, /* flush */
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;
352 * Kill an inode that got unregistered..
354 static void proc_kill_inodes(int ino)
356 struct file *filp;
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;
364 if (!dentry)
365 continue;
366 if (dentry->d_op != &proc_dentry_operations)
367 continue;
368 inode = dentry->d_inode;
369 if (!inode)
370 continue;
371 if (inode->i_ino != ino)
372 continue;
373 filp->f_op = NULL;
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) {
383 *p = dp->next;
384 dp->next = NULL;
385 if (S_ISDIR(dp->mode))
386 dir->nlink--;
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);
392 return 0;
394 p = &dp->next;
396 return -EINVAL;
400 * /proc/self:
402 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
404 int len;
405 char tmp[30];
407 len = sprintf(tmp, "%d", current->pid);
408 if (buflen < len)
409 len = buflen;
410 copy_to_user(buffer, tmp, len);
411 return len;
414 static struct dentry * proc_self_follow_link(struct dentry *dentry,
415 struct dentry *base,
416 unsigned int follow)
418 char tmp[30];
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;
428 char *page;
429 int len = 0;
431 de = (struct proc_dir_entry *) inode->u.generic_ip;
432 if (!de)
433 return -ENOENT;
434 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
435 return -ENOMEM;
437 if (de->readlink_proc)
438 len = de->readlink_proc(de, page);
440 if (len > buflen)
441 len = buflen;
443 copy_to_user(buffer, page, len);
444 free_page((unsigned long) page);
445 return len;
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;
452 char *page;
453 struct dentry *d;
454 int len = 0;
456 de = (struct proc_dir_entry *) inode->u.generic_ip;
457 if (!(page = (char*) __get_free_page(GFP_KERNEL)))
458 return NULL;
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);
465 return d;
468 static struct inode_operations proc_self_inode_operations = {
469 NULL, /* no file-ops */
470 NULL, /* create */
471 NULL, /* lookup */
472 NULL, /* link */
473 NULL, /* unlink */
474 NULL, /* symlink */
475 NULL, /* mkdir */
476 NULL, /* rmdir */
477 NULL, /* mknod */
478 NULL, /* rename */
479 proc_self_readlink, /* readlink */
480 proc_self_follow_link, /* follow_link */
481 NULL, /* readpage */
482 NULL, /* writepage */
483 NULL, /* bmap */
484 NULL, /* truncate */
485 NULL /* permission */
488 static struct inode_operations proc_link_inode_operations = {
489 NULL, /* no file-ops */
490 NULL, /* create */
491 NULL, /* lookup */
492 NULL, /* link */
493 NULL, /* unlink */
494 NULL, /* symlink */
495 NULL, /* mkdir */
496 NULL, /* rmdir */
497 NULL, /* mknod */
498 NULL, /* rename */
499 proc_readlink, /* readlink */
500 proc_follow_link, /* follow_link */
501 NULL, /* readpage */
502 NULL, /* writepage */
503 NULL, /* bmap */
504 NULL, /* truncate */
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
544 #endif
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
551 #endif
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
563 #endif
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
580 #endif
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 = {
607 PROC_FS, 2, "fs",
608 S_IFDIR | S_IRUGO | S_IXUGO, 2, 0, 0,
609 0, &proc_dir_inode_operations,
610 NULL, NULL,
611 NULL,
612 NULL, NULL
614 static struct proc_dir_entry proc_root_dma = {
615 PROC_DMA, 3, "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_cmdline = {
625 PROC_CMDLINE, 7, "cmdline",
626 S_IFREG | S_IRUGO, 1, 0, 0,
627 0, &proc_array_inode_operations
629 #ifdef CONFIG_RTC
630 static struct proc_dir_entry proc_root_rtc = {
631 PROC_RTC, 3, "rtc",
632 S_IFREG | S_IRUGO, 1, 0, 0,
633 0, &proc_array_inode_operations
635 #endif
636 static struct proc_dir_entry proc_root_locks = {
637 PROC_LOCKS, 5, "locks",
638 S_IFREG | S_IRUGO, 1, 0, 0,
639 0, &proc_array_inode_operations
641 static struct proc_dir_entry proc_root_mounts = {
642 PROC_MTAB, 6, "mounts",
643 S_IFREG | S_IRUGO, 1, 0, 0,
644 0, &proc_array_inode_operations
646 static struct proc_dir_entry proc_root_swaps = {
647 PROC_SWAP, 5, "swaps",
648 S_IFREG | S_IRUGO, 1, 0, 0,
649 0, &proc_array_inode_operations
651 static struct proc_dir_entry proc_root_profile = {
652 PROC_PROFILE, 7, "profile",
653 S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0,
654 0, &proc_profile_inode_operations
656 static struct proc_dir_entry proc_root_slab = {
657 PROC_SLABINFO, 8, "slabinfo",
658 S_IFREG | S_IRUGO, 1, 0, 0,
659 0, &proc_array_inode_operations
661 #ifdef __powerpc__
662 static struct proc_dir_entry proc_root_ppc_htab = {
663 PROC_PPC_HTAB, 8, "ppc_htab",
664 S_IFREG | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 1, 0, 0,
665 0, &proc_ppc_htab_inode_operations,
666 NULL, NULL, /* get_info, fill_inode */
667 NULL, /* next */
668 NULL, NULL /* parent, subdir */
670 #endif
672 __initfunc(void proc_root_init(void))
674 proc_base_init();
675 proc_register(&proc_root, &proc_root_loadavg);
676 proc_register(&proc_root, &proc_root_uptime);
677 proc_register(&proc_root, &proc_root_meminfo);
678 proc_register(&proc_root, &proc_root_kmsg);
679 proc_register(&proc_root, &proc_root_version);
680 proc_register(&proc_root, &proc_root_cpuinfo);
681 proc_register(&proc_root, &proc_root_self);
682 proc_net = create_proc_entry("net", S_IFDIR, 0);
683 proc_scsi = create_proc_entry("scsi", S_IFDIR, 0);
684 #ifdef CONFIG_SYSCTL
685 proc_register(&proc_root, &proc_sys_root);
686 #endif
687 #ifdef CONFIG_MCA
688 proc_register(&proc_root, &proc_mca);
689 #endif
691 #ifdef CONFIG_DEBUG_MALLOC
692 proc_register(&proc_root, &proc_root_malloc);
693 #endif
694 proc_register(&proc_root, &proc_root_kcore);
695 proc_root_kcore.size = (MAP_NR(high_memory) << PAGE_SHIFT) + PAGE_SIZE;
697 #ifdef CONFIG_MODULES
698 proc_register(&proc_root, &proc_root_modules);
699 proc_register(&proc_root, &proc_root_ksyms);
700 #endif
701 proc_register(&proc_root, &proc_root_stat);
702 proc_register(&proc_root, &proc_root_devices);
703 proc_register(&proc_root, &proc_root_partitions);
704 proc_register(&proc_root, &proc_root_interrupts);
705 proc_register(&proc_root, &proc_root_filesystems);
706 proc_register(&proc_root, &proc_root_fs);
707 proc_register(&proc_root, &proc_root_dma);
708 proc_register(&proc_root, &proc_root_ioports);
709 proc_register(&proc_root, &proc_root_cmdline);
710 #ifdef CONFIG_RTC
711 proc_register(&proc_root, &proc_root_rtc);
712 #endif
713 proc_register(&proc_root, &proc_root_locks);
715 proc_register(&proc_root, &proc_root_mounts);
716 proc_register(&proc_root, &proc_root_swaps);
718 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
719 #ifdef CONFIG_SUN_OPENPROMFS
720 openpromfs_init ();
721 #endif
722 proc_register(&proc_root, &proc_openprom);
723 #endif
724 #ifdef CONFIG_PROC_HARDWARE
725 proc_register(&proc_root, &proc_root_hardware);
726 #endif
727 #ifdef CONFIG_STRAM_PROC
728 proc_register(&proc_root, &proc_root_stram);
729 #endif
730 proc_register(&proc_root, &proc_root_slab);
732 if (prof_shift) {
733 proc_register(&proc_root, &proc_root_profile);
734 proc_root_profile.size = (1+prof_len) * sizeof(unsigned int);
737 proc_tty_init();
738 #ifdef __powerpc__
739 proc_register(&proc_root, &proc_root_ppc_htab);
740 #endif
741 #ifdef CONFIG_PROC_DEVICETREE
742 proc_device_tree_init();
743 #endif
745 proc_bus = create_proc_entry("bus", S_IFDIR, 0);
749 * As some entries in /proc are volatile, we want to
750 * get rid of unused dentries. This could be made
751 * smarter: we could keep a "volatile" flag in the
752 * inode to indicate which ones to keep.
754 static void
755 proc_delete_dentry(struct dentry * dentry)
757 d_drop(dentry);
760 struct dentry_operations proc_dentry_operations =
762 NULL, /* revalidate */
763 NULL, /* d_hash */
764 NULL, /* d_compare */
765 proc_delete_dentry /* d_delete(struct dentry *) */
769 * Don't create negative dentries here, return -ENOENT by hand
770 * instead.
772 int proc_lookup(struct inode * dir, struct dentry *dentry)
774 struct inode *inode;
775 struct proc_dir_entry * de;
776 int error;
778 error = -ENOTDIR;
779 if (!dir || !S_ISDIR(dir->i_mode))
780 goto out;
782 error = -ENOENT;
783 inode = NULL;
784 de = (struct proc_dir_entry *) dir->u.generic_ip;
785 if (de) {
786 for (de = de->subdir; de ; de = de->next) {
787 if (!de || !de->low_ino)
788 continue;
789 if (de->namelen != dentry->d_name.len)
790 continue;
791 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
792 int ino = de->low_ino | (dir->i_ino & ~(0xffff));
793 error = -EINVAL;
794 inode = proc_get_inode(dir->i_sb, ino, de);
795 break;
800 if (inode) {
801 dentry->d_op = &proc_dentry_operations;
802 d_add(dentry, inode);
803 error = 0;
805 out:
806 return error;
809 static int proc_root_lookup(struct inode * dir, struct dentry * dentry)
811 unsigned int pid, c;
812 struct task_struct *p;
813 const char *name;
814 struct inode *inode;
815 int len;
817 if (dir->i_ino == PROC_ROOT_INO) { /* check for safety... */
818 dir->i_nlink = proc_root.nlink;
820 read_lock(&tasklist_lock);
821 for_each_task(p) {
822 if (p->pid)
823 dir->i_nlink++;
825 read_unlock(&tasklist_lock);
828 if (!proc_lookup(dir, dentry))
829 return 0;
831 pid = 0;
832 name = dentry->d_name.name;
833 len = dentry->d_name.len;
834 while (len-- > 0) {
835 c = *name - '0';
836 name++;
837 if (c > 9) {
838 pid = 0;
839 break;
841 pid *= 10;
842 pid += c;
843 if (pid & 0xffff0000) {
844 pid = 0;
845 break;
848 read_lock(&tasklist_lock);
849 p = find_task_by_pid(pid);
850 read_unlock(&tasklist_lock);
851 inode = NULL;
852 if (pid && p) {
853 unsigned long ino = (pid << 16) + PROC_PID_INO;
854 inode = proc_get_inode(dir->i_sb, ino, &proc_pid);
855 if (!inode)
856 return -EINVAL;
857 inode->i_flags|=S_IMMUTABLE;
860 dentry->d_op = &proc_dentry_operations;
861 d_add(dentry, inode);
862 return 0;
866 * This returns non-zero if at EOF, so that the /proc
867 * root directory can use this and check if it should
868 * continue with the <pid> entries..
870 * Note that the VFS-layer doesn't care about the return
871 * value of the readdir() call, as long as it's non-negative
872 * for success..
874 int proc_readdir(struct file * filp,
875 void * dirent, filldir_t filldir)
877 struct proc_dir_entry * de;
878 unsigned int ino;
879 int i;
880 struct inode *inode = filp->f_dentry->d_inode;
882 if (!inode || !S_ISDIR(inode->i_mode))
883 return -ENOTDIR;
884 ino = inode->i_ino;
885 de = (struct proc_dir_entry *) inode->u.generic_ip;
886 if (!de)
887 return -EINVAL;
888 i = filp->f_pos;
889 switch (i) {
890 case 0:
891 if (filldir(dirent, ".", 1, i, ino) < 0)
892 return 0;
893 i++;
894 filp->f_pos++;
895 /* fall through */
896 case 1:
897 if (filldir(dirent, "..", 2, i, de->parent->low_ino) < 0)
898 return 0;
899 i++;
900 filp->f_pos++;
901 /* fall through */
902 default:
903 ino &= ~0xffff;
904 de = de->subdir;
905 i -= 2;
906 for (;;) {
907 if (!de)
908 return 1;
909 if (!i)
910 break;
911 de = de->next;
912 i--;
915 do {
916 if (filldir(dirent, de->name, de->namelen, filp->f_pos, ino | de->low_ino) < 0)
917 return 0;
918 filp->f_pos++;
919 de = de->next;
920 } while (de);
922 return 1;
925 #define PROC_NUMBUF 10
926 #define PROC_MAXPIDS 20
929 * Get a few pid's to return for filldir - we need to hold the
930 * tasklist lock while doing this, and we must release it before
931 * we actually do the filldir itself, so we use a temp buffer..
933 static int get_pid_list(int index, unsigned int *pids)
935 struct task_struct *p;
936 int nr_pids = 0;
938 index -= FIRST_PROCESS_ENTRY;
939 read_lock(&tasklist_lock);
940 for_each_task(p) {
941 int pid = p->pid;
942 if (!pid)
943 continue;
944 if (--index >= 0)
945 continue;
946 pids[nr_pids] = pid;
947 nr_pids++;
948 if (nr_pids >= PROC_MAXPIDS)
949 break;
951 read_unlock(&tasklist_lock);
952 return nr_pids;
955 static int proc_root_readdir(struct file * filp,
956 void * dirent, filldir_t filldir)
958 unsigned int pid_array[PROC_MAXPIDS];
959 char buf[PROC_NUMBUF];
960 unsigned int nr = filp->f_pos;
961 unsigned int nr_pids, i;
963 if (nr < FIRST_PROCESS_ENTRY) {
964 int error = proc_readdir(filp, dirent, filldir);
965 if (error <= 0)
966 return error;
967 filp->f_pos = nr = FIRST_PROCESS_ENTRY;
970 nr_pids = get_pid_list(nr, pid_array);
972 for (i = 0; i < nr_pids; i++) {
973 int pid = pid_array[i];
974 ino_t ino = (pid << 16) + PROC_PID_INO;
975 unsigned long j = PROC_NUMBUF;
977 do {
978 j--;
979 buf[j] = '0' + (pid % 10);
980 pid /= 10;
981 } while (pid);
983 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino) < 0)
984 break;
985 filp->f_pos++;
987 return 0;
990 static int proc_unlink(struct inode *dir, struct dentry *dentry)
992 struct proc_dir_entry * dp = dir->u.generic_ip;
994 printk("proc_file_unlink: deleting %s/%s\n", dp->name, dentry->d_name.name);
996 remove_proc_entry(dentry->d_name.name, dp);
997 dentry->d_inode->i_nlink = 0;
998 d_delete(dentry);
999 return 0;