Import 2.3.7pre9
[davej-history.git] / fs / proc / root.c
blob79622b022e2c1abe67d63a213037957d45fb4672
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, /* 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, *proc_sysvipc;
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 struct dentry * (*proc_openprom_deflookup_ptr)(struct inode *, struct dentry *);
185 void (*proc_openprom_use)(struct inode *, int) = 0;
186 static struct openpromfs_dev *proc_openprom_devices = NULL;
187 static ino_t proc_openpromdev_ino = PROC_OPENPROMD_FIRST;
189 struct inode_operations *
190 proc_openprom_register(int (*readdir)(struct file *, void *, filldir_t),
191 struct dentry * (*lookup)(struct inode *, struct dentry *),
192 void (*use)(struct inode *, int),
193 struct openpromfs_dev ***devices)
195 proc_openprom_defreaddir_ptr = (proc_openprom_inode_operations.default_file_ops)->readdir;
196 proc_openprom_deflookup_ptr = proc_openprom_inode_operations.lookup;
197 (proc_openprom_inode_operations.default_file_ops)->readdir = readdir;
198 proc_openprom_inode_operations.lookup = lookup;
199 proc_openprom_use = use;
200 *devices = &proc_openprom_devices;
201 return &proc_openprom_inode_operations;
204 int proc_openprom_regdev(struct openpromfs_dev *d)
206 if (proc_openpromdev_ino == PROC_OPENPROMD_FIRST + PROC_NOPENPROMD)
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 struct dentry *
254 proc_openprom_deflookup(struct inode * dir, struct dentry *dentry)
256 request_module("openpromfs");
257 if (proc_openprom_inode_operations.lookup !=
258 proc_openprom_deflookup)
259 return proc_openprom_inode_operations.lookup
260 (dir, dentry);
261 return ERR_PTR(-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_memory = {
625 PROC_MEMORY, 6, "memory",
626 S_IFREG | S_IRUGO, 1, 0, 0,
627 0, &proc_array_inode_operations
629 static struct proc_dir_entry proc_root_cmdline = {
630 PROC_CMDLINE, 7, "cmdline",
631 S_IFREG | S_IRUGO, 1, 0, 0,
632 0, &proc_array_inode_operations
634 #ifdef CONFIG_RTC
635 static struct proc_dir_entry proc_root_rtc = {
636 PROC_RTC, 3, "rtc",
637 S_IFREG | S_IRUGO, 1, 0, 0,
638 0, &proc_array_inode_operations
640 #endif
641 static struct proc_dir_entry proc_root_locks = {
642 PROC_LOCKS, 5, "locks",
643 S_IFREG | S_IRUGO, 1, 0, 0,
644 0, &proc_array_inode_operations
646 static struct proc_dir_entry proc_root_mounts = {
647 PROC_MTAB, 6, "mounts",
648 S_IFREG | S_IRUGO, 1, 0, 0,
649 0, &proc_array_inode_operations
651 static struct proc_dir_entry proc_root_swaps = {
652 PROC_SWAP, 5, "swaps",
653 S_IFREG | S_IRUGO, 1, 0, 0,
654 0, &proc_array_inode_operations
656 static struct proc_dir_entry proc_root_profile = {
657 PROC_PROFILE, 7, "profile",
658 S_IFREG | S_IRUGO | S_IWUSR, 1, 0, 0,
659 0, &proc_profile_inode_operations
661 static struct proc_dir_entry proc_root_slab = {
662 PROC_SLABINFO, 8, "slabinfo",
663 S_IFREG | S_IRUGO, 1, 0, 0,
664 0, &proc_array_inode_operations
666 #ifdef __powerpc__
667 static struct proc_dir_entry proc_root_ppc_htab = {
668 PROC_PPC_HTAB, 8, "ppc_htab",
669 S_IFREG | S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, 1, 0, 0,
670 0, &proc_ppc_htab_inode_operations,
671 NULL, NULL, /* get_info, fill_inode */
672 NULL, /* next */
673 NULL, NULL /* parent, subdir */
675 #endif
677 __initfunc(void proc_root_init(void))
679 proc_base_init();
680 proc_register(&proc_root, &proc_root_loadavg);
681 proc_register(&proc_root, &proc_root_uptime);
682 proc_register(&proc_root, &proc_root_meminfo);
683 proc_register(&proc_root, &proc_root_kmsg);
684 proc_register(&proc_root, &proc_root_version);
685 proc_register(&proc_root, &proc_root_cpuinfo);
686 proc_register(&proc_root, &proc_root_self);
687 proc_net = create_proc_entry("net", S_IFDIR, 0);
688 proc_scsi = create_proc_entry("scsi", S_IFDIR, 0);
689 #ifdef CONFIG_SYSVIPC
690 proc_sysvipc = create_proc_entry("sysvipc", S_IFDIR, 0);
691 #endif
692 #ifdef CONFIG_SYSCTL
693 proc_register(&proc_root, &proc_sys_root);
694 #endif
695 #ifdef CONFIG_MCA
696 proc_register(&proc_root, &proc_mca);
697 #endif
699 #ifdef CONFIG_DEBUG_MALLOC
700 proc_register(&proc_root, &proc_root_malloc);
701 #endif
702 proc_register(&proc_root, &proc_root_kcore);
703 proc_root_kcore.size = (MAP_NR(high_memory) << PAGE_SHIFT) + PAGE_SIZE;
705 #ifdef CONFIG_MODULES
706 proc_register(&proc_root, &proc_root_modules);
707 proc_register(&proc_root, &proc_root_ksyms);
708 #endif
709 proc_register(&proc_root, &proc_root_stat);
710 proc_register(&proc_root, &proc_root_devices);
711 proc_register(&proc_root, &proc_root_partitions);
712 proc_register(&proc_root, &proc_root_interrupts);
713 proc_register(&proc_root, &proc_root_filesystems);
714 proc_register(&proc_root, &proc_root_fs);
715 proc_register(&proc_root, &proc_root_dma);
716 proc_register(&proc_root, &proc_root_ioports);
717 proc_register(&proc_root, &proc_root_memory);
718 proc_register(&proc_root, &proc_root_cmdline);
719 #ifdef CONFIG_RTC
720 proc_register(&proc_root, &proc_root_rtc);
721 #endif
722 proc_register(&proc_root, &proc_root_locks);
724 proc_register(&proc_root, &proc_root_mounts);
725 proc_register(&proc_root, &proc_root_swaps);
727 #if defined(CONFIG_SUN_OPENPROMFS) || defined(CONFIG_SUN_OPENPROMFS_MODULE)
728 #ifdef CONFIG_SUN_OPENPROMFS
729 openpromfs_init ();
730 #endif
731 proc_register(&proc_root, &proc_openprom);
732 #endif
733 #ifdef CONFIG_PROC_HARDWARE
734 proc_register(&proc_root, &proc_root_hardware);
735 #endif
736 #ifdef CONFIG_STRAM_PROC
737 proc_register(&proc_root, &proc_root_stram);
738 #endif
739 proc_register(&proc_root, &proc_root_slab);
741 if (prof_shift) {
742 proc_register(&proc_root, &proc_root_profile);
743 proc_root_profile.size = (1+prof_len) * sizeof(unsigned int);
746 proc_tty_init();
747 #ifdef __powerpc__
748 proc_register(&proc_root, &proc_root_ppc_htab);
749 #endif
750 #ifdef CONFIG_PROC_DEVICETREE
751 proc_device_tree_init();
752 #endif
754 proc_bus = create_proc_entry("bus", S_IFDIR, 0);
758 * As some entries in /proc are volatile, we want to
759 * get rid of unused dentries. This could be made
760 * smarter: we could keep a "volatile" flag in the
761 * inode to indicate which ones to keep.
763 static void
764 proc_delete_dentry(struct dentry * dentry)
766 d_drop(dentry);
769 struct dentry_operations proc_dentry_operations =
771 NULL, /* revalidate */
772 NULL, /* d_hash */
773 NULL, /* d_compare */
774 proc_delete_dentry /* d_delete(struct dentry *) */
778 * Don't create negative dentries here, return -ENOENT by hand
779 * instead.
781 struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry)
783 struct inode *inode;
784 struct proc_dir_entry * de;
785 int error;
787 error = -ENOENT;
788 inode = NULL;
789 de = (struct proc_dir_entry *) dir->u.generic_ip;
790 if (de) {
791 for (de = de->subdir; de ; de = de->next) {
792 if (!de || !de->low_ino)
793 continue;
794 if (de->namelen != dentry->d_name.len)
795 continue;
796 if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
797 int ino = de->low_ino | (dir->i_ino & ~(0xffff));
798 error = -EINVAL;
799 inode = proc_get_inode(dir->i_sb, ino, de);
800 break;
805 if (inode) {
806 dentry->d_op = &proc_dentry_operations;
807 d_add(dentry, inode);
808 return NULL;
810 return ERR_PTR(error);
813 static struct dentry *proc_root_lookup(struct inode * dir, struct dentry * dentry)
815 unsigned int pid, c;
816 struct task_struct *p;
817 const char *name;
818 struct inode *inode;
819 int len;
821 if (dir->i_ino == PROC_ROOT_INO) { /* check for safety... */
822 dir->i_nlink = proc_root.nlink;
824 read_lock(&tasklist_lock);
825 for_each_task(p) {
826 if (p->pid)
827 dir->i_nlink++;
829 read_unlock(&tasklist_lock);
832 if (!proc_lookup(dir, dentry))
833 return NULL;
835 pid = 0;
836 name = dentry->d_name.name;
837 len = dentry->d_name.len;
838 while (len-- > 0) {
839 c = *name - '0';
840 name++;
841 if (c > 9) {
842 pid = 0;
843 break;
845 pid *= 10;
846 pid += c;
847 if (pid & 0xffff0000) {
848 pid = 0;
849 break;
852 read_lock(&tasklist_lock);
853 p = find_task_by_pid(pid);
854 read_unlock(&tasklist_lock);
855 inode = NULL;
856 if (pid && p) {
857 unsigned long ino = (pid << 16) + PROC_PID_INO;
858 inode = proc_get_inode(dir->i_sb, ino, &proc_pid);
859 if (!inode)
860 return ERR_PTR(-EINVAL);
861 inode->i_flags|=S_IMMUTABLE;
864 dentry->d_op = &proc_dentry_operations;
865 d_add(dentry, inode);
866 return NULL;
870 * This returns non-zero if at EOF, so that the /proc
871 * root directory can use this and check if it should
872 * continue with the <pid> entries..
874 * Note that the VFS-layer doesn't care about the return
875 * value of the readdir() call, as long as it's non-negative
876 * for success..
878 int proc_readdir(struct file * filp,
879 void * dirent, filldir_t filldir)
881 struct proc_dir_entry * de;
882 unsigned int ino;
883 int i;
884 struct inode *inode = filp->f_dentry->d_inode;
886 ino = inode->i_ino;
887 de = (struct proc_dir_entry *) inode->u.generic_ip;
888 if (!de)
889 return -EINVAL;
890 i = filp->f_pos;
891 switch (i) {
892 case 0:
893 if (filldir(dirent, ".", 1, i, ino) < 0)
894 return 0;
895 i++;
896 filp->f_pos++;
897 /* fall through */
898 case 1:
899 if (filldir(dirent, "..", 2, i, de->parent->low_ino) < 0)
900 return 0;
901 i++;
902 filp->f_pos++;
903 /* fall through */
904 default:
905 ino &= ~0xffff;
906 de = de->subdir;
907 i -= 2;
908 for (;;) {
909 if (!de)
910 return 1;
911 if (!i)
912 break;
913 de = de->next;
914 i--;
917 do {
918 if (filldir(dirent, de->name, de->namelen, filp->f_pos, ino | de->low_ino) < 0)
919 return 0;
920 filp->f_pos++;
921 de = de->next;
922 } while (de);
924 return 1;
927 #define PROC_NUMBUF 10
928 #define PROC_MAXPIDS 20
931 * Get a few pid's to return for filldir - we need to hold the
932 * tasklist lock while doing this, and we must release it before
933 * we actually do the filldir itself, so we use a temp buffer..
935 static int get_pid_list(int index, unsigned int *pids)
937 struct task_struct *p;
938 int nr_pids = 0;
940 index -= FIRST_PROCESS_ENTRY;
941 read_lock(&tasklist_lock);
942 for_each_task(p) {
943 int pid = p->pid;
944 if (!pid)
945 continue;
946 if (--index >= 0)
947 continue;
948 pids[nr_pids] = pid;
949 nr_pids++;
950 if (nr_pids >= PROC_MAXPIDS)
951 break;
953 read_unlock(&tasklist_lock);
954 return nr_pids;
957 static int proc_root_readdir(struct file * filp,
958 void * dirent, filldir_t filldir)
960 unsigned int pid_array[PROC_MAXPIDS];
961 char buf[PROC_NUMBUF];
962 unsigned int nr = filp->f_pos;
963 unsigned int nr_pids, i;
965 if (nr < FIRST_PROCESS_ENTRY) {
966 int error = proc_readdir(filp, dirent, filldir);
967 if (error <= 0)
968 return error;
969 filp->f_pos = nr = FIRST_PROCESS_ENTRY;
972 nr_pids = get_pid_list(nr, pid_array);
974 for (i = 0; i < nr_pids; i++) {
975 int pid = pid_array[i];
976 ino_t ino = (pid << 16) + PROC_PID_INO;
977 unsigned long j = PROC_NUMBUF;
979 do {
980 j--;
981 buf[j] = '0' + (pid % 10);
982 pid /= 10;
983 } while (pid);
985 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino) < 0)
986 break;
987 filp->f_pos++;
989 return 0;
992 static int proc_unlink(struct inode *dir, struct dentry *dentry)
994 struct proc_dir_entry * dp = dir->u.generic_ip;
996 printk("proc_file_unlink: deleting %s/%s\n", dp->name, dentry->d_name.name);
998 remove_proc_entry(dentry->d_name.name, dp);
999 dentry->d_inode->i_nlink = 0;
1000 d_delete(dentry);
1001 return 0;