* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / fs / proc / openpromfs.c
blobc9553ea244e85353786ec3607b195ba42104abf1
1 /* $Id: openpromfs.c,v 1.36 1999/08/31 07:01:03 davem Exp $
2 * openpromfs.c: /proc/openprom handling routines
4 * Copyright (C) 1996-1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
6 */
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/fs.h>
12 #include <linux/proc_fs.h>
13 #include <linux/init.h>
15 #include <asm/openprom.h>
16 #include <asm/oplib.h>
17 #include <asm/uaccess.h>
19 #define ALIASES_NNODES 64
21 typedef struct {
22 u16 parent;
23 u16 next;
24 u16 child;
25 u16 first_prop;
26 u32 node;
27 } openpromfs_node;
29 typedef struct {
30 #define OPP_STRING 0x10
31 #define OPP_STRINGLIST 0x20
32 #define OPP_BINARY 0x40
33 #define OPP_HEXSTRING 0x80
34 #define OPP_DIRTY 0x01
35 #define OPP_QUOTED 0x02
36 #define OPP_NOTQUOTED 0x04
37 #define OPP_ASCIIZ 0x08
38 u32 flag;
39 u32 alloclen;
40 u32 len;
41 char *value;
42 char name[8];
43 } openprom_property;
45 static openpromfs_node *nodes = NULL;
46 static int alloced = 0;
47 static u16 last_node = 0;
48 static u16 first_prop = 0;
49 static u16 options = 0xffff;
50 static u16 aliases = 0xffff;
51 static int aliases_nodes = 0;
52 static char *alias_names [ALIASES_NNODES];
53 static struct inode_operations *proc_openprom_iops = 0;
54 static struct openpromfs_dev **devices;
56 #define NODE(ino) nodes[ino - PROC_OPENPROM_FIRST]
57 #define NODE2INO(node) (node + PROC_OPENPROM_FIRST)
58 #define NODEP2INO(no) (no + PROC_OPENPROM_FIRST + last_node)
60 static int openpromfs_create (struct inode *, struct dentry *, int);
61 static int openpromfs_readdir(struct file *, void *, filldir_t);
62 static struct dentry *openpromfs_lookup(struct inode *, struct dentry *dentry);
63 static int openpromfs_unlink (struct inode *, struct dentry *dentry);
65 static ssize_t nodenum_read(struct file *file, char *buf,
66 size_t count, loff_t *ppos)
68 struct inode *inode = file->f_dentry->d_inode;
69 char buffer[10];
71 if (count < 0 || !inode->u.generic_ip)
72 return -EINVAL;
73 sprintf (buffer, "%8.8x\n", (u32)(long)(inode->u.generic_ip));
74 if (file->f_pos >= 9)
75 return 0;
76 if (count > 9 - file->f_pos)
77 count = 9 - file->f_pos;
78 copy_to_user(buf, buffer + file->f_pos, count);
79 file->f_pos += count;
80 return count;
83 static ssize_t property_read(struct file *filp, char *buf,
84 size_t count, loff_t *ppos)
86 struct inode *inode = filp->f_dentry->d_inode;
87 int i, j, k;
88 u32 node;
89 char *p, *s;
90 u32 *q;
91 openprom_property *op;
92 char buffer[64];
94 if (filp->f_pos >= 0xffffff)
95 return -EINVAL;
96 if (!filp->private_data) {
97 node = nodes[(u16)((long)inode->u.generic_ip)].node;
98 i = ((u32)(long)inode->u.generic_ip) >> 16;
99 if ((u16)((long)inode->u.generic_ip) == aliases) {
100 if (i >= aliases_nodes)
101 p = 0;
102 else
103 p = alias_names [i];
104 } else
105 for (p = prom_firstprop (node, buffer);
106 i && p && *p;
107 p = prom_nextprop (node, p, buffer), i--)
108 /* nothing */ ;
109 if (!p || !*p)
110 return -EIO;
111 i = prom_getproplen (node, p);
112 if (i < 0) {
113 if ((u16)((long)inode->u.generic_ip) == aliases)
114 i = 0;
115 else
116 return -EIO;
118 k = i;
119 if (i < 64) i = 64;
120 filp->private_data = kmalloc (sizeof (openprom_property)
121 + (j = strlen (p)) + 2 * i,
122 GFP_KERNEL);
123 if (!filp->private_data)
124 return -ENOMEM;
125 op = (openprom_property *)filp->private_data;
126 op->flag = 0;
127 op->alloclen = 2 * i;
128 strcpy (op->name, p);
129 op->value = (char *)(((unsigned long)(op->name + j + 4)) & ~3);
130 op->len = k;
131 if (k && prom_getproperty (node, p, op->value, i) < 0)
132 return -EIO;
133 op->value [k] = 0;
134 if (k) {
135 for (s = 0, p = op->value; p < op->value + k; p++) {
136 if ((*p >= ' ' && *p <= '~') || *p == '\n') {
137 op->flag |= OPP_STRING;
138 s = p;
139 continue;
141 if (p > op->value && !*p && s == p - 1) {
142 if (p < op->value + k - 1)
143 op->flag |= OPP_STRINGLIST;
144 else
145 op->flag |= OPP_ASCIIZ;
146 continue;
148 if (k == 1 && !*p) {
149 op->flag |= (OPP_STRING|OPP_ASCIIZ);
150 break;
152 op->flag &= ~(OPP_STRING|OPP_STRINGLIST);
153 if (k & 3)
154 op->flag |= OPP_HEXSTRING;
155 else
156 op->flag |= OPP_BINARY;
157 break;
159 if (op->flag & OPP_STRINGLIST)
160 op->flag &= ~(OPP_STRING);
161 if (op->flag & OPP_ASCIIZ)
162 op->len--;
164 } else
165 op = (openprom_property *)filp->private_data;
166 if (!count || !(op->len || (op->flag & OPP_ASCIIZ)))
167 return 0;
168 if (op->flag & OPP_STRINGLIST) {
169 for (k = 0, p = op->value; p < op->value + op->len; p++)
170 if (!*p)
171 k++;
172 i = op->len + 4 * k + 3;
173 } else if (op->flag & OPP_STRING) {
174 i = op->len + 3;
175 } else if (op->flag & OPP_BINARY) {
176 i = (op->len * 9) >> 2;
177 } else {
178 i = (op->len << 1) + 1;
180 k = filp->f_pos;
181 if (k >= i) return 0;
182 if (count > i - k) count = i - k;
183 if (op->flag & OPP_STRING) {
184 if (!k) {
185 __put_user('\'', buf);
186 k++;
187 count--;
190 if (k + count >= i - 2)
191 j = i - 2 - k;
192 else
193 j = count;
195 if (j >= 0) {
196 copy_to_user(buf + k - filp->f_pos,
197 op->value + k - 1, j);
198 count -= j;
199 k += j;
202 if (count)
203 __put_user('\'', &buf [k++ - filp->f_pos]);
204 if (count > 1)
205 __put_user('\n', &buf [k++ - filp->f_pos]);
207 } else if (op->flag & OPP_STRINGLIST) {
208 char *tmp;
210 tmp = kmalloc (i, GFP_KERNEL);
211 if (!tmp)
212 return -ENOMEM;
214 s = tmp;
215 *s++ = '\'';
216 for (p = op->value; p < op->value + op->len; p++) {
217 if (!*p) {
218 strcpy(s, "' + '");
219 s += 5;
220 continue;
222 *s++ = *p;
224 strcpy(s, "'\n");
226 copy_to_user(buf, tmp + k, count);
228 kfree(tmp);
229 k += count;
231 } else if (op->flag & OPP_BINARY) {
232 char buffer[10];
233 u32 *first, *last;
234 int first_off, last_cnt;
236 first = ((u32 *)op->value) + k / 9;
237 first_off = k % 9;
238 last = ((u32 *)op->value) + (k + count - 1) / 9;
239 last_cnt = (k + count) % 9;
240 if (!last_cnt) last_cnt = 9;
242 if (first == last) {
243 sprintf (buffer, "%08x.", *first);
244 copy_to_user (buf, buffer + first_off, last_cnt - first_off);
245 buf += last_cnt - first_off;
246 } else {
247 for (q = first; q <= last; q++) {
248 sprintf (buffer, "%08x.", *q);
249 if (q == first) {
250 copy_to_user (buf, buffer + first_off,
251 9 - first_off);
252 buf += 9 - first_off;
253 } else if (q == last) {
254 copy_to_user (buf, buffer, last_cnt);
255 buf += last_cnt;
256 } else {
257 copy_to_user (buf, buffer, 9);
258 buf += 9;
263 if (last == (u32 *)(op->value + op->len - 4) && last_cnt == 9)
264 __put_user('\n', (buf - 1));
266 k += count;
268 } else if (op->flag & OPP_HEXSTRING) {
269 char buffer[2];
271 if ((k < i - 1) && (k & 1)) {
272 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
273 __put_user(buffer[1], &buf[k++ - filp->f_pos]);
274 count--;
277 for (; (count > 1) && (k < i - 1); k += 2) {
278 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
279 copy_to_user (buf + k - filp->f_pos, buffer, 2);
280 count -= 2;
283 if (count && (k < i - 1)) {
284 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
285 __put_user(buffer[0], &buf[k++ - filp->f_pos]);
286 count--;
289 if (count)
290 __put_user('\n', &buf [k++ - filp->f_pos]);
292 count = k - filp->f_pos;
293 filp->f_pos = k;
294 return count;
297 static ssize_t property_write(struct file *filp, const char *buf,
298 size_t count, loff_t *ppos)
300 int i, j, k;
301 char *p;
302 u32 *q;
303 void *b;
304 openprom_property *op;
306 if (filp->f_pos >= 0xffffff)
307 return -EINVAL;
308 if (!filp->private_data) {
309 i = property_read (filp, NULL, 0, 0);
310 if (i)
311 return i;
313 k = filp->f_pos;
314 op = (openprom_property *)filp->private_data;
315 if (!(op->flag & OPP_STRING)) {
316 u32 *first, *last;
317 int first_off, last_cnt;
318 u32 mask, mask2;
319 char tmp [9];
320 int forcelen = 0;
322 j = k % 9;
323 for (i = 0; i < count; i++, j++) {
324 if (j == 9) j = 0;
325 if (!j) {
326 char ctmp;
327 __get_user(ctmp, &buf[i]);
328 if (ctmp != '.') {
329 if (ctmp != '\n') {
330 if (op->flag & OPP_BINARY)
331 return -EINVAL;
332 else
333 goto write_try_string;
334 } else {
335 count = i + 1;
336 forcelen = 1;
337 break;
340 } else {
341 char ctmp;
342 __get_user(ctmp, &buf[i]);
343 if (ctmp < '0' ||
344 (ctmp > '9' && ctmp < 'A') ||
345 (ctmp > 'F' && ctmp < 'a') ||
346 ctmp > 'f') {
347 if (op->flag & OPP_BINARY)
348 return -EINVAL;
349 else
350 goto write_try_string;
354 op->flag |= OPP_BINARY;
355 tmp [8] = 0;
356 i = ((count + k + 8) / 9) << 2;
357 if (op->alloclen <= i) {
358 b = kmalloc (sizeof (openprom_property) + 2 * i,
359 GFP_KERNEL);
360 if (!b)
361 return -ENOMEM;
362 memcpy (b, filp->private_data,
363 sizeof (openprom_property)
364 + strlen (op->name) + op->alloclen);
365 memset (((char *)b) + sizeof (openprom_property)
366 + strlen (op->name) + op->alloclen,
367 0, 2 * i - op->alloclen);
368 op = (openprom_property *)b;
369 op->alloclen = 2*i;
370 b = filp->private_data;
371 filp->private_data = (void *)op;
372 kfree (b);
374 first = ((u32 *)op->value) + (k / 9);
375 first_off = k % 9;
376 last = (u32 *)(op->value + i);
377 last_cnt = (k + count) % 9;
378 if (first + 1 == last) {
379 memset (tmp, '0', 8);
380 copy_from_user (tmp + first_off, buf,
381 (count + first_off > 8) ? 8 - first_off : count);
382 mask = 0xffffffff;
383 mask2 = 0xffffffff;
384 for (j = 0; j < first_off; j++)
385 mask >>= 1;
386 for (j = 8 - count - first_off; j > 0; j--)
387 mask2 <<= 1;
388 mask &= mask2;
389 if (mask) {
390 *first &= ~mask;
391 *first |= simple_strtoul (tmp, 0, 16);
392 op->flag |= OPP_DIRTY;
394 } else {
395 op->flag |= OPP_DIRTY;
396 for (q = first; q < last; q++) {
397 if (q == first) {
398 if (first_off < 8) {
399 memset (tmp, '0', 8);
400 copy_from_user (tmp + first_off, buf,
401 8 - first_off);
402 mask = 0xffffffff;
403 for (j = 0; j < first_off; j++)
404 mask >>= 1;
405 *q &= ~mask;
406 *q |= simple_strtoul (tmp,0,16);
408 buf += 9;
409 } else if ((q == last - 1) && last_cnt
410 && (last_cnt < 8)) {
411 memset (tmp, '0', 8);
412 copy_from_user (tmp, buf, last_cnt);
413 mask = 0xffffffff;
414 for (j = 0; j < 8 - last_cnt; j++)
415 mask <<= 1;
416 *q &= ~mask;
417 *q |= simple_strtoul (tmp, 0, 16);
418 buf += last_cnt;
419 } else {
420 char tchars[17]; /* XXX yuck... */
422 copy_from_user(tchars, buf, 16);
423 *q = simple_strtoul (tchars, 0, 16);
424 buf += 9;
428 if (!forcelen) {
429 if (op->len < i)
430 op->len = i;
431 } else
432 op->len = i;
433 filp->f_pos += count;
435 write_try_string:
436 if (!(op->flag & OPP_BINARY)) {
437 if (!(op->flag & (OPP_QUOTED | OPP_NOTQUOTED))) {
438 char ctmp;
440 /* No way, if somebody starts writing from the middle,
441 * we don't know whether he uses quotes around or not
443 if (k > 0)
444 return -EINVAL;
445 __get_user(ctmp, buf);
446 if (ctmp == '\'') {
447 op->flag |= OPP_QUOTED;
448 buf++;
449 count--;
450 filp->f_pos++;
451 if (!count) {
452 op->flag |= OPP_STRING;
453 return 1;
455 } else
456 op->flag |= OPP_NOTQUOTED;
458 op->flag |= OPP_STRING;
459 if (op->alloclen <= count + filp->f_pos) {
460 b = kmalloc (sizeof (openprom_property)
461 + 2 * (count + filp->f_pos), GFP_KERNEL);
462 if (!b)
463 return -ENOMEM;
464 memcpy (b, filp->private_data,
465 sizeof (openprom_property)
466 + strlen (op->name) + op->alloclen);
467 memset (((char *)b) + sizeof (openprom_property)
468 + strlen (op->name) + op->alloclen,
469 0, 2*(count - filp->f_pos) - op->alloclen);
470 op = (openprom_property *)b;
471 op->alloclen = 2*(count + filp->f_pos);
472 b = filp->private_data;
473 filp->private_data = (void *)op;
474 kfree (b);
476 p = op->value + filp->f_pos - ((op->flag & OPP_QUOTED) ? 1 : 0);
477 copy_from_user (p, buf, count);
478 op->flag |= OPP_DIRTY;
479 for (i = 0; i < count; i++, p++)
480 if (*p == '\n') {
481 *p = 0;
482 break;
484 if (i < count) {
485 op->len = p - op->value;
486 filp->f_pos += i + 1;
487 if ((p > op->value) && (op->flag & OPP_QUOTED)
488 && (*(p - 1) == '\''))
489 op->len--;
490 } else {
491 if (p - op->value > op->len)
492 op->len = p - op->value;
493 filp->f_pos += count;
496 return filp->f_pos - k;
499 int property_release (struct inode *inode, struct file *filp)
501 openprom_property *op = (openprom_property *)filp->private_data;
502 unsigned long flags;
503 int error;
504 u32 node;
506 if (!op)
507 return 0;
508 node = nodes[(u16)((long)inode->u.generic_ip)].node;
509 if ((u16)((long)inode->u.generic_ip) == aliases) {
510 if ((op->flag & OPP_DIRTY) && (op->flag & OPP_STRING)) {
511 char *p = op->name;
512 int i = (op->value - op->name) - strlen (op->name) - 1;
513 op->value [op->len] = 0;
514 *(op->value - 1) = ' ';
515 if (i) {
516 for (p = op->value - i - 2; p >= op->name; p--)
517 p[i] = *p;
518 p = op->name + i;
520 memcpy (p - 8, "nvalias ", 8);
521 prom_feval (p - 8);
523 } else if (op->flag & OPP_DIRTY) {
524 if (op->flag & OPP_STRING) {
525 op->value [op->len] = 0;
526 save_and_cli (flags);
527 error = prom_setprop (node, op->name,
528 op->value, op->len + 1);
529 restore_flags (flags);
530 if (error <= 0)
531 printk (KERN_WARNING "/proc/openprom: "
532 "Couldn't write property %s\n",
533 op->name);
534 } else if ((op->flag & OPP_BINARY) || !op->len) {
535 save_and_cli (flags);
536 error = prom_setprop (node, op->name,
537 op->value, op->len);
538 restore_flags (flags);
539 if (error <= 0)
540 printk (KERN_WARNING "/proc/openprom: "
541 "Couldn't write property %s\n",
542 op->name);
543 } else {
544 printk (KERN_WARNING "/proc/openprom: "
545 "Unknown property type of %s\n",
546 op->name);
549 kfree (filp->private_data);
550 return 0;
553 static struct file_operations openpromfs_prop_ops = {
554 NULL, /* lseek - default */
555 property_read, /* read */
556 property_write, /* write - bad */
557 NULL, /* readdir */
558 NULL, /* poll - default */
559 NULL, /* ioctl - default */
560 NULL, /* mmap */
561 NULL, /* no special open code */
562 NULL, /* flush */
563 property_release, /* no special release code */
564 NULL /* can't fsync */
567 static struct inode_operations openpromfs_prop_inode_ops = {
568 &openpromfs_prop_ops, /* default property file-ops */
569 NULL, /* create */
570 NULL, /* lookup */
571 NULL, /* link */
572 NULL, /* unlink */
573 NULL, /* symlink */
574 NULL, /* mkdir */
575 NULL, /* rmdir */
576 NULL, /* mknod */
577 NULL, /* rename */
578 NULL, /* readlink */
579 NULL, /* follow_link */
580 NULL, /* get_block */
581 NULL, /* readpage */
582 NULL, /* writepage */
583 NULL, /* flushpage */
584 NULL, /* truncate */
585 NULL, /* permission */
586 NULL, /* smap */
587 NULL /* revalidate */
590 static struct file_operations openpromfs_nodenum_ops = {
591 NULL, /* lseek - default */
592 nodenum_read, /* read */
593 NULL, /* write - bad */
594 NULL, /* readdir */
595 NULL, /* poll - default */
596 NULL, /* ioctl - default */
597 NULL, /* mmap */
598 NULL, /* no special open code */
599 NULL, /* flush */
600 NULL, /* no special release code */
601 NULL /* can't fsync */
604 static struct inode_operations openpromfs_nodenum_inode_ops = {
605 &openpromfs_nodenum_ops,/* default .node file-ops */
606 NULL, /* create */
607 NULL, /* lookup */
608 NULL, /* link */
609 NULL, /* unlink */
610 NULL, /* symlink */
611 NULL, /* mkdir */
612 NULL, /* rmdir */
613 NULL, /* mknod */
614 NULL, /* rename */
615 NULL, /* readlink */
616 NULL, /* follow_link */
617 NULL, /* get_block */
618 NULL, /* readpage */
619 NULL, /* writepage */
620 NULL, /* flushpage */
621 NULL, /* truncate */
622 NULL, /* permission */
623 NULL, /* smap */
624 NULL /* revalidate */
627 static struct file_operations openprom_alias_operations = {
628 NULL, /* lseek - default */
629 NULL, /* read - bad */
630 NULL, /* write - bad */
631 openpromfs_readdir, /* readdir */
632 NULL, /* poll - default */
633 NULL, /* ioctl - default */
634 NULL, /* mmap */
635 NULL, /* no special open code */
636 NULL, /* flush */
637 NULL, /* no special release code */
638 NULL /* can't fsync */
641 static struct inode_operations openprom_alias_inode_operations = {
642 &openprom_alias_operations,/* default aliases directory file-ops */
643 openpromfs_create, /* create */
644 openpromfs_lookup, /* lookup */
645 NULL, /* link */
646 openpromfs_unlink, /* unlink */
647 NULL, /* symlink */
648 NULL, /* mkdir */
649 NULL, /* rmdir */
650 NULL, /* mknod */
651 NULL, /* rename */
652 NULL, /* readlink */
653 NULL, /* follow_link */
654 NULL, /* get_block */
655 NULL, /* readpage */
656 NULL, /* writepage */
657 NULL, /* flushpage */
658 NULL, /* truncate */
659 NULL, /* permission */
660 NULL, /* smap */
661 NULL /* revalidate */
664 static int lookup_children(u16 n, const char * name, int len)
666 int ret;
667 u16 node;
668 for (; n != 0xffff; n = nodes[n].next) {
669 node = nodes[n].child;
670 if (node != 0xffff) {
671 char buffer[128];
672 int i;
673 char *p;
675 while (node != 0xffff) {
676 if (prom_getname (nodes[node].node,
677 buffer, 128) >= 0) {
678 i = strlen (buffer);
679 if ((len == i)
680 && !strncmp (buffer, name, len))
681 return NODE2INO(node);
682 p = strchr (buffer, '@');
683 if (p && (len == p - buffer)
684 && !strncmp (buffer, name, len))
685 return NODE2INO(node);
687 node = nodes[node].next;
689 } else
690 continue;
691 ret = lookup_children (nodes[n].child, name, len);
692 if (ret) return ret;
694 return 0;
697 static struct dentry *openpromfs_lookup(struct inode * dir, struct dentry *dentry)
699 int ino = 0;
700 #define OPFSL_DIR 0
701 #define OPFSL_PROPERTY 1
702 #define OPFSL_NODENUM 2
703 #define OPFSL_DEVICE 3
704 int type = 0;
705 char buffer[128];
706 char *p;
707 const char *name;
708 u32 n;
709 u16 dirnode;
710 unsigned int len;
711 int i;
712 struct inode *inode;
713 struct openpromfs_dev *d = NULL;
714 char buffer2[64];
716 inode = NULL;
717 name = dentry->d_name.name;
718 len = dentry->d_name.len;
719 if (name [0] == '.' && len == 5 && !strncmp (name + 1, "node", 4)) {
720 ino = NODEP2INO(NODE(dir->i_ino).first_prop);
721 type = OPFSL_NODENUM;
723 if (!ino) {
724 u16 node = NODE(dir->i_ino).child;
725 while (node != 0xffff) {
726 if (prom_getname (nodes[node].node, buffer, 128) >= 0) {
727 i = strlen (buffer);
728 if (len == i && !strncmp (buffer, name, len)) {
729 ino = NODE2INO(node);
730 type = OPFSL_DIR;
731 break;
733 p = strchr (buffer, '@');
734 if (p && (len == p - buffer)
735 && !strncmp (buffer, name, len)) {
736 ino = NODE2INO(node);
737 type = OPFSL_DIR;
738 break;
741 node = nodes[node].next;
744 n = NODE(dir->i_ino).node;
745 dirnode = dir->i_ino - PROC_OPENPROM_FIRST;
746 if (!ino) {
747 int j = NODEP2INO(NODE(dir->i_ino).first_prop);
748 if (dirnode != aliases) {
749 for (p = prom_firstprop (n, buffer2);
750 p && *p;
751 p = prom_nextprop (n, p, buffer2)) {
752 j++;
753 if ((len == strlen (p))
754 && !strncmp (p, name, len)) {
755 ino = j;
756 type = OPFSL_PROPERTY;
757 break;
760 } else {
761 int k;
762 for (k = 0; k < aliases_nodes; k++) {
763 j++;
764 if (alias_names [k]
765 && (len == strlen (alias_names [k]))
766 && !strncmp (alias_names [k], name, len)) {
767 ino = j;
768 type = OPFSL_PROPERTY;
769 break;
774 if (!ino) {
775 for (d = *devices; d; d = d->next)
776 if ((d->node == n) && (strlen (d->name) == len)
777 && !strncmp (d->name, name, len)) {
778 ino = d->inode;
779 type = OPFSL_DEVICE;
780 break;
783 if (!ino) {
784 ino = lookup_children (NODE(dir->i_ino).child, name, len);
785 if (ino)
786 type = OPFSL_DIR;
787 else
788 return ERR_PTR(-ENOENT);
790 inode = proc_get_inode (dir->i_sb, ino, 0);
791 if (!inode)
792 return ERR_PTR(-EINVAL);
793 switch (type) {
794 case OPFSL_DIR:
795 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
796 if (ino == PROC_OPENPROM_FIRST + aliases) {
797 inode->i_mode |= S_IWUSR;
798 inode->i_op = &openprom_alias_inode_operations;
799 } else
800 inode->i_op = proc_openprom_iops;
801 inode->i_nlink = 2;
802 break;
803 case OPFSL_NODENUM:
804 inode->i_mode = S_IFREG | S_IRUGO;
805 inode->i_op = &openpromfs_nodenum_inode_ops;
806 inode->i_nlink = 1;
807 inode->u.generic_ip = (void *)(long)(n);
808 break;
809 case OPFSL_PROPERTY:
810 if ((dirnode == options) && (len == 17)
811 && !strncmp (name, "security-password", 17))
812 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
813 else {
814 inode->i_mode = S_IFREG | S_IRUGO;
815 if (dirnode == options || dirnode == aliases) {
816 if (len != 4 || strncmp (name, "name", 4))
817 inode->i_mode |= S_IWUSR;
820 inode->i_op = &openpromfs_prop_inode_ops;
821 inode->i_nlink = 1;
822 if (inode->i_size < 0)
823 inode->i_size = 0;
824 inode->u.generic_ip = (void *)(long)(((u16)dirnode) |
825 (((u16)(ino - NODEP2INO(NODE(dir->i_ino).first_prop) - 1)) << 16));
826 break;
827 case OPFSL_DEVICE:
828 inode->i_mode = d->mode;
829 inode->i_op = &chrdev_inode_operations;
830 inode->i_nlink = 1;
831 inode->i_rdev = d->rdev;
832 break;
835 inode->i_gid = 0;
836 inode->i_uid = 0;
838 d_add(dentry, inode);
839 return NULL;
842 static int openpromfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
844 struct inode *inode = filp->f_dentry->d_inode;
845 unsigned int ino;
846 u32 n;
847 int i, j;
848 char buffer[128];
849 u16 node;
850 char *p;
851 struct openpromfs_dev *d;
852 char buffer2[64];
854 ino = inode->i_ino;
855 i = filp->f_pos;
856 switch (i) {
857 case 0:
858 if (filldir(dirent, ".", 1, i, ino) < 0) return 0;
859 i++;
860 filp->f_pos++;
861 /* fall thru */
862 case 1:
863 if (filldir(dirent, "..", 2, i,
864 (NODE(ino).parent == 0xffff) ?
865 PROC_ROOT_INO : NODE2INO(NODE(ino).parent)) < 0)
866 return 0;
867 i++;
868 filp->f_pos++;
869 /* fall thru */
870 default:
871 i -= 2;
872 node = NODE(ino).child;
873 while (i && node != 0xffff) {
874 node = nodes[node].next;
875 i--;
877 while (node != 0xffff) {
878 if (prom_getname (nodes[node].node, buffer, 128) < 0)
879 return 0;
880 if (filldir(dirent, buffer, strlen(buffer),
881 filp->f_pos, NODE2INO(node)) < 0)
882 return 0;
883 filp->f_pos++;
884 node = nodes[node].next;
886 j = NODEP2INO(NODE(ino).first_prop);
887 if (!i) {
888 if (filldir(dirent, ".node", 5, filp->f_pos, j) < 0)
889 return 0;
890 filp->f_pos++;
891 } else
892 i--;
893 n = NODE(ino).node;
894 if (ino == PROC_OPENPROM_FIRST + aliases) {
895 for (j++; i < aliases_nodes; i++, j++) {
896 if (alias_names [i]) {
897 if (filldir (dirent, alias_names [i],
898 strlen (alias_names [i]),
899 filp->f_pos, j) < 0) return 0;
900 filp->f_pos++;
903 } else {
904 for (p = prom_firstprop (n, buffer2);
905 p && *p;
906 p = prom_nextprop (n, p, buffer2)) {
907 j++;
908 if (i) i--;
909 else {
910 if (filldir(dirent, p, strlen(p),
911 filp->f_pos, j) < 0)
912 return 0;
913 filp->f_pos++;
917 for (d = *devices; d; d = d->next) {
918 if (d->node == n) {
919 if (i) i--;
920 else {
921 if (filldir(dirent, d->name,
922 strlen(d->name),
923 filp->f_pos, d->inode) < 0)
924 return 0;
925 filp->f_pos++;
930 return 0;
933 static int openpromfs_create (struct inode *dir, struct dentry *dentry, int mode)
935 char *p;
936 struct inode *inode;
938 if (!dir)
939 return -ENOENT;
940 if (dentry->d_name.len > 256)
941 return -EINVAL;
942 if (aliases_nodes == ALIASES_NNODES)
943 return -EIO;
944 p = kmalloc (dentry->d_name.len + 1, GFP_KERNEL);
945 if (!p)
946 return -ENOMEM;
947 strncpy (p, dentry->d_name.name, dentry->d_name.len);
948 p [dentry->d_name.len] = 0;
949 alias_names [aliases_nodes++] = p;
950 inode = proc_get_inode (dir->i_sb,
951 NODEP2INO(NODE(dir->i_ino).first_prop)
952 + aliases_nodes, 0);
953 if (!inode)
954 return -EINVAL;
955 inode->i_mode = S_IFREG | S_IRUGO | S_IWUSR;
956 inode->i_op = &openpromfs_prop_inode_ops;
957 inode->i_nlink = 1;
958 if (inode->i_size < 0) inode->i_size = 0;
959 inode->u.generic_ip = (void *)(long)(((u16)aliases) |
960 (((u16)(aliases_nodes - 1)) << 16));
961 d_instantiate(dentry, inode);
962 return 0;
965 static int openpromfs_unlink (struct inode *dir, struct dentry *dentry)
967 unsigned int len;
968 char *p;
969 const char *name;
970 int i;
972 name = dentry->d_name.name;
973 len = dentry->d_name.len;
974 for (i = 0; i < aliases_nodes; i++)
975 if ((strlen (alias_names [i]) == len)
976 && !strncmp (name, alias_names[i], len)) {
977 char buffer[512];
979 p = alias_names [i];
980 alias_names [i] = NULL;
981 kfree (p);
982 strcpy (buffer, "nvunalias ");
983 memcpy (buffer + 10, name, len);
984 buffer [10 + len] = 0;
985 prom_feval (buffer);
987 d_delete(dentry);
988 return 0;
991 /* {{{ init section */
992 #ifndef MODULE
993 static int __init check_space (u16 n)
994 #else
995 static int check_space (u16 n)
996 #endif
998 unsigned long pages;
1000 if ((1 << alloced) * PAGE_SIZE < (n + 2) * sizeof(openpromfs_node)) {
1001 pages = __get_free_pages (GFP_KERNEL, alloced + 1);
1002 if (!pages)
1003 return -1;
1005 if (nodes) {
1006 memcpy ((char *)pages, (char *)nodes,
1007 (1 << alloced) * PAGE_SIZE);
1008 free_pages ((unsigned long)nodes, alloced);
1010 alloced++;
1011 nodes = (openpromfs_node *)pages;
1013 return 0;
1016 #ifndef MODULE
1017 static u16 __init get_nodes (u16 parent, u32 node)
1018 #else
1019 static u16 get_nodes (u16 parent, u32 node)
1020 #endif
1022 char *p;
1023 u16 n = last_node++, i;
1024 char buffer[64];
1026 if (check_space (n) < 0)
1027 return 0xffff;
1028 nodes[n].parent = parent;
1029 nodes[n].node = node;
1030 nodes[n].next = 0xffff;
1031 nodes[n].child = 0xffff;
1032 nodes[n].first_prop = first_prop++;
1033 if (!parent) {
1034 char buffer[8];
1035 int j;
1037 if ((j = prom_getproperty (node, "name", buffer, 8)) >= 0) {
1038 buffer[j] = 0;
1039 if (!strcmp (buffer, "options"))
1040 options = n;
1041 else if (!strcmp (buffer, "aliases"))
1042 aliases = n;
1045 if (n != aliases)
1046 for (p = prom_firstprop (node, buffer);
1047 p && p != (char *)-1 && *p;
1048 p = prom_nextprop (node, p, buffer))
1049 first_prop++;
1050 else {
1051 char *q;
1052 for (p = prom_firstprop (node, buffer);
1053 p && p != (char *)-1 && *p;
1054 p = prom_nextprop (node, p, buffer)) {
1055 if (aliases_nodes == ALIASES_NNODES)
1056 break;
1057 for (i = 0; i < aliases_nodes; i++)
1058 if (!strcmp (p, alias_names [i]))
1059 break;
1060 if (i < aliases_nodes)
1061 continue;
1062 q = kmalloc (strlen (p) + 1, GFP_KERNEL);
1063 if (!q)
1064 return 0xffff;
1065 strcpy (q, p);
1066 alias_names [aliases_nodes++] = q;
1068 first_prop += ALIASES_NNODES;
1070 node = prom_getchild (node);
1071 if (node) {
1072 parent = get_nodes (n, node);
1073 if (parent == 0xffff)
1074 return 0xffff;
1075 nodes[n].child = parent;
1076 while ((node = prom_getsibling (node)) != 0) {
1077 i = get_nodes (n, node);
1078 if (i == 0xffff)
1079 return 0xffff;
1080 nodes[parent].next = i;
1081 parent = i;
1084 return n;
1088 #ifdef MODULE
1089 void openpromfs_use (struct inode *inode, int inc)
1091 static int root_fresh = 1;
1092 static int dec_first = 1;
1093 #ifdef OPENPROM_DEBUGGING
1094 static int usec = 0;
1096 if (inc) {
1097 if (inode->i_count == 1)
1098 usec++;
1099 else if (root_fresh && inode->i_ino == PROC_OPENPROM_FIRST) {
1100 root_fresh = 0;
1101 usec++;
1103 } else {
1104 if (inode->i_ino == PROC_OPENPROM_FIRST)
1105 root_fresh = 0;
1106 if (!dec_first)
1107 usec--;
1109 printk ("openpromfs_use: %d %d %d %d\n",
1110 inode->i_ino, inc, usec, inode->i_count);
1111 #else
1112 if (inc) {
1113 if (inode->i_count == 1)
1114 MOD_INC_USE_COUNT;
1115 else if (root_fresh && inode->i_ino == PROC_OPENPROM_FIRST) {
1116 root_fresh = 0;
1117 MOD_INC_USE_COUNT;
1119 } else {
1120 if (inode->i_ino == PROC_OPENPROM_FIRST)
1121 root_fresh = 0;
1122 if (!dec_first)
1123 MOD_DEC_USE_COUNT;
1125 #endif
1126 dec_first = 0;
1129 #else
1130 #define openpromfs_use 0
1131 #endif
1133 #ifndef MODULE
1134 #define RET(x)
1135 void __init openpromfs_init (void)
1136 #else
1138 EXPORT_NO_SYMBOLS;
1140 #define RET(x) -x
1141 int init_module (void)
1142 #endif
1144 nodes = (openpromfs_node *)__get_free_pages(GFP_KERNEL, 0);
1145 if (!nodes) {
1146 printk (KERN_WARNING "/proc/openprom: can't get free page\n");
1147 return RET(EIO);
1149 if (get_nodes (0xffff, prom_root_node) == 0xffff) {
1150 printk (KERN_WARNING "/proc/openprom: couldn't setup tree\n");
1151 return RET(EIO);
1153 nodes[last_node].first_prop = first_prop;
1154 proc_openprom_iops = proc_openprom_register (openpromfs_readdir,
1155 openpromfs_lookup,
1156 openpromfs_use,
1157 &devices);
1158 return RET(0);
1161 #ifdef MODULE
1162 void cleanup_module (void)
1164 int i;
1165 proc_openprom_deregister ();
1166 free_pages ((unsigned long)nodes, alloced);
1167 for (i = 0; i < aliases_nodes; i++)
1168 if (alias_names [i])
1169 kfree (alias_names [i]);
1170 nodes = NULL;
1172 #endif