1 /* $Id: inode.c,v 1.1.1.1 2005/04/11 02:50:49 jack Exp $
2 * openpromfs.c: /proc/openprom handling routines
4 * Copyright (C) 1996-1999 Jakub Jelinek (jakub@redhat.com)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
12 #include <linux/openprom_fs.h>
13 #include <linux/locks.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/smp_lock.h>
18 #include <asm/openprom.h>
19 #include <asm/oplib.h>
20 #include <asm/uaccess.h>
22 #define ALIASES_NNODES 64
33 #define OPP_STRING 0x10
34 #define OPP_STRINGLIST 0x20
35 #define OPP_BINARY 0x40
36 #define OPP_HEXSTRING 0x80
37 #define OPP_DIRTY 0x01
38 #define OPP_QUOTED 0x02
39 #define OPP_NOTQUOTED 0x04
40 #define OPP_ASCIIZ 0x08
48 static openpromfs_node *nodes = NULL;
49 static int alloced = 0;
50 static u16 last_node = 0;
51 static u16 first_prop = 0;
52 static u16 options = 0xffff;
53 static u16 aliases = 0xffff;
54 static int aliases_nodes = 0;
55 static char *alias_names [ALIASES_NNODES];
57 #define OPENPROM_ROOT_INO 16
58 #define OPENPROM_FIRST_INO OPENPROM_ROOT_INO
59 #define NODE(ino) nodes[ino - OPENPROM_FIRST_INO]
60 #define NODE2INO(node) (node + OPENPROM_FIRST_INO)
61 #define NODEP2INO(no) (no + OPENPROM_FIRST_INO + last_node)
63 static int openpromfs_create (struct inode *, struct dentry *, int);
64 static int openpromfs_readdir(struct file *, void *, filldir_t);
65 static struct dentry *openpromfs_lookup(struct inode *, struct dentry *dentry);
66 static int openpromfs_unlink (struct inode *, struct dentry *dentry);
68 static ssize_t nodenum_read(struct file *file, char *buf,
69 size_t count, loff_t *ppos)
71 struct inode *inode = file->f_dentry->d_inode;
74 if (count < 0 || !inode->u.generic_ip)
76 sprintf (buffer, "%8.8x\n", (u32)(long)(inode->u.generic_ip));
79 if (count > 9 - file->f_pos)
80 count = 9 - file->f_pos;
81 copy_to_user(buf, buffer + file->f_pos, count);
86 static ssize_t property_read(struct file *filp, char *buf,
87 size_t count, loff_t *ppos)
89 struct inode *inode = filp->f_dentry->d_inode;
94 openprom_property *op;
97 if (filp->f_pos >= 0xffffff)
99 if (!filp->private_data) {
100 node = nodes[(u16)((long)inode->u.generic_ip)].node;
101 i = ((u32)(long)inode->u.generic_ip) >> 16;
102 if ((u16)((long)inode->u.generic_ip) == aliases) {
103 if (i >= aliases_nodes)
108 for (p = prom_firstprop (node, buffer);
110 p = prom_nextprop (node, p, buffer), i--)
114 i = prom_getproplen (node, p);
116 if ((u16)((long)inode->u.generic_ip) == aliases)
123 filp->private_data = kmalloc (sizeof (openprom_property)
124 + (j = strlen (p)) + 2 * i,
126 if (!filp->private_data)
128 op = (openprom_property *)filp->private_data;
130 op->alloclen = 2 * i;
131 strcpy (op->name, p);
132 op->value = (char *)(((unsigned long)(op->name + j + 4)) & ~3);
134 if (k && prom_getproperty (node, p, op->value, i) < 0)
138 for (s = 0, p = op->value; p < op->value + k; p++) {
139 if ((*p >= ' ' && *p <= '~') || *p == '\n') {
140 op->flag |= OPP_STRING;
144 if (p > op->value && !*p && s == p - 1) {
145 if (p < op->value + k - 1)
146 op->flag |= OPP_STRINGLIST;
148 op->flag |= OPP_ASCIIZ;
152 op->flag |= (OPP_STRING|OPP_ASCIIZ);
155 op->flag &= ~(OPP_STRING|OPP_STRINGLIST);
157 op->flag |= OPP_HEXSTRING;
159 op->flag |= OPP_BINARY;
162 if (op->flag & OPP_STRINGLIST)
163 op->flag &= ~(OPP_STRING);
164 if (op->flag & OPP_ASCIIZ)
168 op = (openprom_property *)filp->private_data;
169 if (!count || !(op->len || (op->flag & OPP_ASCIIZ)))
171 if (op->flag & OPP_STRINGLIST) {
172 for (k = 0, p = op->value; p < op->value + op->len; p++)
175 i = op->len + 4 * k + 3;
176 } else if (op->flag & OPP_STRING) {
178 } else if (op->flag & OPP_BINARY) {
179 i = (op->len * 9) >> 2;
181 i = (op->len << 1) + 1;
184 if (k >= i) return 0;
185 if (count > i - k) count = i - k;
186 if (op->flag & OPP_STRING) {
188 __put_user('\'', buf);
193 if (k + count >= i - 2)
199 copy_to_user(buf + k - filp->f_pos,
200 op->value + k - 1, j);
206 __put_user('\'', &buf [k++ - filp->f_pos]);
208 __put_user('\n', &buf [k++ - filp->f_pos]);
210 } else if (op->flag & OPP_STRINGLIST) {
213 tmp = kmalloc (i, GFP_KERNEL);
219 for (p = op->value; p < op->value + op->len; p++) {
229 copy_to_user(buf, tmp + k, count);
234 } else if (op->flag & OPP_BINARY) {
237 int first_off, last_cnt;
239 first = ((u32 *)op->value) + k / 9;
241 last = ((u32 *)op->value) + (k + count - 1) / 9;
242 last_cnt = (k + count) % 9;
243 if (!last_cnt) last_cnt = 9;
246 sprintf (buffer, "%08x.", *first);
247 copy_to_user (buf, buffer + first_off, last_cnt - first_off);
248 buf += last_cnt - first_off;
250 for (q = first; q <= last; q++) {
251 sprintf (buffer, "%08x.", *q);
253 copy_to_user (buf, buffer + first_off,
255 buf += 9 - first_off;
256 } else if (q == last) {
257 copy_to_user (buf, buffer, last_cnt);
260 copy_to_user (buf, buffer, 9);
266 if (last == (u32 *)(op->value + op->len - 4) && last_cnt == 9)
267 __put_user('\n', (buf - 1));
271 } else if (op->flag & OPP_HEXSTRING) {
274 if ((k < i - 1) && (k & 1)) {
275 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
276 __put_user(buffer[1], &buf[k++ - filp->f_pos]);
280 for (; (count > 1) && (k < i - 1); k += 2) {
281 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
282 copy_to_user (buf + k - filp->f_pos, buffer, 2);
286 if (count && (k < i - 1)) {
287 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
288 __put_user(buffer[0], &buf[k++ - filp->f_pos]);
293 __put_user('\n', &buf [k++ - filp->f_pos]);
295 count = k - filp->f_pos;
300 static ssize_t property_write(struct file *filp, const char *buf,
301 size_t count, loff_t *ppos)
307 openprom_property *op;
309 if (filp->f_pos >= 0xffffff)
311 if (!filp->private_data) {
312 i = property_read (filp, NULL, 0, 0);
317 op = (openprom_property *)filp->private_data;
318 if (!(op->flag & OPP_STRING)) {
320 int first_off, last_cnt;
326 for (i = 0; i < count; i++, j++) {
330 __get_user(ctmp, &buf[i]);
333 if (op->flag & OPP_BINARY)
336 goto write_try_string;
345 __get_user(ctmp, &buf[i]);
347 (ctmp > '9' && ctmp < 'A') ||
348 (ctmp > 'F' && ctmp < 'a') ||
350 if (op->flag & OPP_BINARY)
353 goto write_try_string;
357 op->flag |= OPP_BINARY;
359 i = ((count + k + 8) / 9) << 2;
360 if (op->alloclen <= i) {
361 b = kmalloc (sizeof (openprom_property) + 2 * i,
365 memcpy (b, filp->private_data,
366 sizeof (openprom_property)
367 + strlen (op->name) + op->alloclen);
368 memset (((char *)b) + sizeof (openprom_property)
369 + strlen (op->name) + op->alloclen,
370 0, 2 * i - op->alloclen);
371 op = (openprom_property *)b;
373 b = filp->private_data;
374 filp->private_data = (void *)op;
377 first = ((u32 *)op->value) + (k / 9);
379 last = (u32 *)(op->value + i);
380 last_cnt = (k + count) % 9;
381 if (first + 1 == last) {
382 memset (tmp, '0', 8);
383 copy_from_user (tmp + first_off, buf,
384 (count + first_off > 8) ? 8 - first_off : count);
387 for (j = 0; j < first_off; j++)
389 for (j = 8 - count - first_off; j > 0; j--)
394 *first |= simple_strtoul (tmp, 0, 16);
395 op->flag |= OPP_DIRTY;
398 op->flag |= OPP_DIRTY;
399 for (q = first; q < last; q++) {
402 memset (tmp, '0', 8);
403 copy_from_user (tmp + first_off, buf,
406 for (j = 0; j < first_off; j++)
409 *q |= simple_strtoul (tmp,0,16);
412 } else if ((q == last - 1) && last_cnt
414 memset (tmp, '0', 8);
415 copy_from_user (tmp, buf, last_cnt);
417 for (j = 0; j < 8 - last_cnt; j++)
420 *q |= simple_strtoul (tmp, 0, 16);
423 char tchars[17]; /* XXX yuck... */
425 copy_from_user(tchars, buf, 16);
426 *q = simple_strtoul (tchars, 0, 16);
436 filp->f_pos += count;
439 if (!(op->flag & OPP_BINARY)) {
440 if (!(op->flag & (OPP_QUOTED | OPP_NOTQUOTED))) {
443 /* No way, if somebody starts writing from the middle,
444 * we don't know whether he uses quotes around or not
448 __get_user(ctmp, buf);
450 op->flag |= OPP_QUOTED;
455 op->flag |= OPP_STRING;
459 op->flag |= OPP_NOTQUOTED;
461 op->flag |= OPP_STRING;
462 if (op->alloclen <= count + filp->f_pos) {
463 b = kmalloc (sizeof (openprom_property)
464 + 2 * (count + filp->f_pos), GFP_KERNEL);
467 memcpy (b, filp->private_data,
468 sizeof (openprom_property)
469 + strlen (op->name) + op->alloclen);
470 memset (((char *)b) + sizeof (openprom_property)
471 + strlen (op->name) + op->alloclen,
472 0, 2*(count - filp->f_pos) - op->alloclen);
473 op = (openprom_property *)b;
474 op->alloclen = 2*(count + filp->f_pos);
475 b = filp->private_data;
476 filp->private_data = (void *)op;
479 p = op->value + filp->f_pos - ((op->flag & OPP_QUOTED) ? 1 : 0);
480 copy_from_user (p, buf, count);
481 op->flag |= OPP_DIRTY;
482 for (i = 0; i < count; i++, p++)
488 op->len = p - op->value;
489 filp->f_pos += i + 1;
490 if ((p > op->value) && (op->flag & OPP_QUOTED)
491 && (*(p - 1) == '\''))
494 if (p - op->value > op->len)
495 op->len = p - op->value;
496 filp->f_pos += count;
499 return filp->f_pos - k;
502 int property_release (struct inode *inode, struct file *filp)
504 openprom_property *op = (openprom_property *)filp->private_data;
512 node = nodes[(u16)((long)inode->u.generic_ip)].node;
513 if ((u16)((long)inode->u.generic_ip) == aliases) {
514 if ((op->flag & OPP_DIRTY) && (op->flag & OPP_STRING)) {
516 int i = (op->value - op->name) - strlen (op->name) - 1;
517 op->value [op->len] = 0;
518 *(op->value - 1) = ' ';
520 for (p = op->value - i - 2; p >= op->name; p--)
524 memcpy (p - 8, "nvalias ", 8);
527 } else if (op->flag & OPP_DIRTY) {
528 if (op->flag & OPP_STRING) {
529 op->value [op->len] = 0;
530 save_and_cli (flags);
531 error = prom_setprop (node, op->name,
532 op->value, op->len + 1);
533 restore_flags (flags);
535 printk (KERN_WARNING "openpromfs: "
536 "Couldn't write property %s\n",
538 } else if ((op->flag & OPP_BINARY) || !op->len) {
539 save_and_cli (flags);
540 error = prom_setprop (node, op->name,
542 restore_flags (flags);
544 printk (KERN_WARNING "openpromfs: "
545 "Couldn't write property %s\n",
548 printk (KERN_WARNING "openpromfs: "
549 "Unknown property type of %s\n",
554 kfree (filp->private_data);
558 static struct file_operations openpromfs_prop_ops = {
560 write: property_write,
561 release: property_release,
564 static struct file_operations openpromfs_nodenum_ops = {
568 static struct file_operations openprom_operations = {
569 read: generic_read_dir,
570 readdir: openpromfs_readdir,
573 static struct inode_operations openprom_alias_inode_operations = {
574 create: openpromfs_create,
575 lookup: openpromfs_lookup,
576 unlink: openpromfs_unlink,
579 static struct inode_operations openprom_inode_operations = {
580 lookup: openpromfs_lookup,
583 static int lookup_children(u16 n, const char * name, int len)
587 for (; n != 0xffff; n = nodes[n].next) {
588 node = nodes[n].child;
589 if (node != 0xffff) {
594 while (node != 0xffff) {
595 if (prom_getname (nodes[node].node,
599 && !strncmp (buffer, name, len))
600 return NODE2INO(node);
601 p = strchr (buffer, '@');
602 if (p && (len == p - buffer)
603 && !strncmp (buffer, name, len))
604 return NODE2INO(node);
606 node = nodes[node].next;
610 ret = lookup_children (nodes[n].child, name, len);
616 static struct dentry *openpromfs_lookup(struct inode * dir, struct dentry *dentry)
620 #define OPFSL_PROPERTY 1
621 #define OPFSL_NODENUM 2
634 name = dentry->d_name.name;
635 len = dentry->d_name.len;
636 if (name [0] == '.' && len == 5 && !strncmp (name + 1, "node", 4)) {
637 ino = NODEP2INO(NODE(dir->i_ino).first_prop);
638 type = OPFSL_NODENUM;
641 u16 node = NODE(dir->i_ino).child;
642 while (node != 0xffff) {
643 if (prom_getname (nodes[node].node, buffer, 128) >= 0) {
645 if (len == i && !strncmp (buffer, name, len)) {
646 ino = NODE2INO(node);
650 p = strchr (buffer, '@');
651 if (p && (len == p - buffer)
652 && !strncmp (buffer, name, len)) {
653 ino = NODE2INO(node);
658 node = nodes[node].next;
661 n = NODE(dir->i_ino).node;
662 dirnode = dir->i_ino - OPENPROM_FIRST_INO;
664 int j = NODEP2INO(NODE(dir->i_ino).first_prop);
665 if (dirnode != aliases) {
666 for (p = prom_firstprop (n, buffer2);
668 p = prom_nextprop (n, p, buffer2)) {
670 if ((len == strlen (p))
671 && !strncmp (p, name, len)) {
673 type = OPFSL_PROPERTY;
679 for (k = 0; k < aliases_nodes; k++) {
682 && (len == strlen (alias_names [k]))
683 && !strncmp (alias_names [k], name, len)) {
685 type = OPFSL_PROPERTY;
692 ino = lookup_children (NODE(dir->i_ino).child, name, len);
696 return ERR_PTR(-ENOENT);
698 inode = iget (dir->i_sb, ino);
700 return ERR_PTR(-EINVAL);
703 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
704 if (ino == OPENPROM_FIRST_INO + aliases) {
705 inode->i_mode |= S_IWUSR;
706 inode->i_op = &openprom_alias_inode_operations;
708 inode->i_op = &openprom_inode_operations;
709 inode->i_fop = &openprom_operations;
713 inode->i_mode = S_IFREG | S_IRUGO;
714 inode->i_fop = &openpromfs_nodenum_ops;
716 inode->u.generic_ip = (void *)(long)(n);
719 if ((dirnode == options) && (len == 17)
720 && !strncmp (name, "security-password", 17))
721 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
723 inode->i_mode = S_IFREG | S_IRUGO;
724 if (dirnode == options || dirnode == aliases) {
725 if (len != 4 || strncmp (name, "name", 4))
726 inode->i_mode |= S_IWUSR;
729 inode->i_fop = &openpromfs_prop_ops;
731 if (inode->i_size < 0)
733 inode->u.generic_ip = (void *)(long)(((u16)dirnode) |
734 (((u16)(ino - NODEP2INO(NODE(dir->i_ino).first_prop) - 1)) << 16));
741 d_add(dentry, inode);
745 static int openpromfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
747 struct inode *inode = filp->f_dentry->d_inode;
760 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) return 0;
765 if (filldir(dirent, "..", 2, i,
766 (NODE(ino).parent == 0xffff) ?
767 OPENPROM_ROOT_INO : NODE2INO(NODE(ino).parent), DT_DIR) < 0)
774 node = NODE(ino).child;
775 while (i && node != 0xffff) {
776 node = nodes[node].next;
779 while (node != 0xffff) {
780 if (prom_getname (nodes[node].node, buffer, 128) < 0)
782 if (filldir(dirent, buffer, strlen(buffer),
783 filp->f_pos, NODE2INO(node), DT_DIR) < 0)
786 node = nodes[node].next;
788 j = NODEP2INO(NODE(ino).first_prop);
790 if (filldir(dirent, ".node", 5, filp->f_pos, j, DT_REG) < 0)
796 if (ino == OPENPROM_FIRST_INO + aliases) {
797 for (j++; i < aliases_nodes; i++, j++) {
798 if (alias_names [i]) {
799 if (filldir (dirent, alias_names [i],
800 strlen (alias_names [i]),
801 filp->f_pos, j, DT_REG) < 0) return 0;
806 for (p = prom_firstprop (n, buffer2);
808 p = prom_nextprop (n, p, buffer2)) {
812 if (filldir(dirent, p, strlen(p),
813 filp->f_pos, j, DT_REG) < 0)
823 static int openpromfs_create (struct inode *dir, struct dentry *dentry, int mode)
830 if (dentry->d_name.len > 256)
832 if (aliases_nodes == ALIASES_NNODES)
834 p = kmalloc (dentry->d_name.len + 1, GFP_KERNEL);
837 strncpy (p, dentry->d_name.name, dentry->d_name.len);
838 p [dentry->d_name.len] = 0;
839 alias_names [aliases_nodes++] = p;
840 inode = iget (dir->i_sb,
841 NODEP2INO(NODE(dir->i_ino).first_prop) + aliases_nodes);
844 inode->i_mode = S_IFREG | S_IRUGO | S_IWUSR;
845 inode->i_fop = &openpromfs_prop_ops;
847 if (inode->i_size < 0) inode->i_size = 0;
848 inode->u.generic_ip = (void *)(long)(((u16)aliases) |
849 (((u16)(aliases_nodes - 1)) << 16));
850 d_instantiate(dentry, inode);
854 static int openpromfs_unlink (struct inode *dir, struct dentry *dentry)
861 name = dentry->d_name.name;
862 len = dentry->d_name.len;
863 for (i = 0; i < aliases_nodes; i++)
864 if ((strlen (alias_names [i]) == len)
865 && !strncmp (name, alias_names[i], len)) {
869 alias_names [i] = NULL;
871 strcpy (buffer, "nvunalias ");
872 memcpy (buffer + 10, name, len);
873 buffer [10 + len] = 0;
879 /* {{{ init section */
881 static int __init check_space (u16 n)
883 static int check_space (u16 n)
888 if ((1 << alloced) * PAGE_SIZE < (n + 2) * sizeof(openpromfs_node)) {
889 pages = __get_free_pages (GFP_KERNEL, alloced + 1);
894 memcpy ((char *)pages, (char *)nodes,
895 (1 << alloced) * PAGE_SIZE);
896 free_pages ((unsigned long)nodes, alloced);
899 nodes = (openpromfs_node *)pages;
905 static u16 __init get_nodes (u16 parent, u32 node)
907 static u16 get_nodes (u16 parent, u32 node)
911 u16 n = last_node++, i;
914 if (check_space (n) < 0)
916 nodes[n].parent = parent;
917 nodes[n].node = node;
918 nodes[n].next = 0xffff;
919 nodes[n].child = 0xffff;
920 nodes[n].first_prop = first_prop++;
925 if ((j = prom_getproperty (node, "name", buffer, 8)) >= 0) {
927 if (!strcmp (buffer, "options"))
929 else if (!strcmp (buffer, "aliases"))
934 for (p = prom_firstprop (node, buffer);
935 p && p != (char *)-1 && *p;
936 p = prom_nextprop (node, p, buffer))
940 for (p = prom_firstprop (node, buffer);
941 p && p != (char *)-1 && *p;
942 p = prom_nextprop (node, p, buffer)) {
943 if (aliases_nodes == ALIASES_NNODES)
945 for (i = 0; i < aliases_nodes; i++)
946 if (!strcmp (p, alias_names [i]))
948 if (i < aliases_nodes)
950 q = kmalloc (strlen (p) + 1, GFP_KERNEL);
954 alias_names [aliases_nodes++] = q;
956 first_prop += ALIASES_NNODES;
958 node = prom_getchild (node);
960 parent = get_nodes (n, node);
961 if (parent == 0xffff)
963 nodes[n].child = parent;
964 while ((node = prom_getsibling (node)) != 0) {
965 i = get_nodes (n, node);
968 nodes[parent].next = i;
975 static void openprom_read_inode(struct inode * inode)
977 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
978 if (inode->i_ino == OPENPROM_ROOT_INO) {
979 inode->i_op = &openprom_inode_operations;
980 inode->i_fop = &openprom_operations;
981 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
985 static int openprom_statfs(struct super_block *sb, struct statfs *buf)
987 buf->f_type = OPENPROM_SUPER_MAGIC;
988 buf->f_bsize = PAGE_SIZE/sizeof(long); /* ??? */
992 buf->f_namelen = NAME_MAX;
996 static struct super_operations openprom_sops = {
997 read_inode: openprom_read_inode,
998 statfs: openprom_statfs,
1001 struct super_block *openprom_read_super(struct super_block *s,void *data,
1004 struct inode * root_inode;
1006 s->s_blocksize = 1024;
1007 s->s_blocksize_bits = 10;
1008 s->s_magic = OPENPROM_SUPER_MAGIC;
1009 s->s_op = &openprom_sops;
1010 root_inode = iget(s, OPENPROM_ROOT_INO);
1013 s->s_root = d_alloc_root(root_inode);
1019 printk("openprom_read_super: get root inode failed\n");
1024 static DECLARE_FSTYPE(openprom_fs_type, "openpromfs", openprom_read_super, 0);
1026 static int __init init_openprom_fs(void)
1028 nodes = (openpromfs_node *)__get_free_pages(GFP_KERNEL, 0);
1030 printk (KERN_WARNING "openpromfs: can't get free page\n");
1033 if (get_nodes (0xffff, prom_root_node) == 0xffff) {
1034 printk (KERN_WARNING "openpromfs: couldn't setup tree\n");
1037 nodes[last_node].first_prop = first_prop;
1038 return register_filesystem(&openprom_fs_type);
1041 static void __exit exit_openprom_fs(void)
1044 unregister_filesystem(&openprom_fs_type);
1045 free_pages ((unsigned long)nodes, alloced);
1046 for (i = 0; i < aliases_nodes; i++)
1047 if (alias_names [i])
1048 kfree (alias_names [i]);
1054 module_init(init_openprom_fs)
1055 module_exit(exit_openprom_fs)
1056 MODULE_LICENSE("GPL");