1 /* $Id: inode.c,v 1.15 2001/11/12 09:43:39 davem 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;
75 if (count < 0 || !inode->u.generic_ip)
77 sprintf (buffer, "%8.8x\n", (u32)(long)(inode->u.generic_ip));
78 if (pos != (unsigned)pos || pos >= 9)
82 copy_to_user(buf, buffer + pos, count);
87 static ssize_t property_read(struct file *filp, char *buf,
88 size_t count, loff_t *ppos)
90 struct inode *inode = filp->f_dentry->d_inode;
96 openprom_property *op;
99 if (pos < 0 || pos >= 0xffffff)
101 if (!filp->private_data) {
102 node = nodes[(u16)((long)inode->u.generic_ip)].node;
103 i = ((u32)(long)inode->u.generic_ip) >> 16;
104 if ((u16)((long)inode->u.generic_ip) == aliases) {
105 if (i >= aliases_nodes)
110 for (p = prom_firstprop (node, buffer);
112 p = prom_nextprop (node, p, buffer), i--)
116 i = prom_getproplen (node, p);
118 if ((u16)((long)inode->u.generic_ip) == aliases)
125 filp->private_data = kmalloc (sizeof (openprom_property)
126 + (j = strlen (p)) + 2 * i,
128 if (!filp->private_data)
130 op = (openprom_property *)filp->private_data;
132 op->alloclen = 2 * i;
133 strcpy (op->name, p);
134 op->value = (char *)(((unsigned long)(op->name + j + 4)) & ~3);
136 if (k && prom_getproperty (node, p, op->value, i) < 0)
140 for (s = 0, p = op->value; p < op->value + k; p++) {
141 if ((*p >= ' ' && *p <= '~') || *p == '\n') {
142 op->flag |= OPP_STRING;
146 if (p > op->value && !*p && s == p - 1) {
147 if (p < op->value + k - 1)
148 op->flag |= OPP_STRINGLIST;
150 op->flag |= OPP_ASCIIZ;
154 op->flag |= (OPP_STRING|OPP_ASCIIZ);
157 op->flag &= ~(OPP_STRING|OPP_STRINGLIST);
159 op->flag |= OPP_HEXSTRING;
161 op->flag |= OPP_BINARY;
164 if (op->flag & OPP_STRINGLIST)
165 op->flag &= ~(OPP_STRING);
166 if (op->flag & OPP_ASCIIZ)
170 op = (openprom_property *)filp->private_data;
171 if (!count || !(op->len || (op->flag & OPP_ASCIIZ)))
173 if (op->flag & OPP_STRINGLIST) {
174 for (k = 0, p = op->value; p < op->value + op->len; p++)
177 i = op->len + 4 * k + 3;
178 } else if (op->flag & OPP_STRING) {
180 } else if (op->flag & OPP_BINARY) {
181 i = (op->len * 9) >> 2;
183 i = (op->len << 1) + 1;
186 if (k >= i) return 0;
187 if (count > i - k) count = i - k;
188 if (op->flag & OPP_STRING) {
190 __put_user('\'', buf);
195 if (k + count >= i - 2)
201 copy_to_user(buf + k - pos,
202 op->value + k - 1, j);
208 __put_user('\'', &buf [k++ - pos]);
210 __put_user('\n', &buf [k++ - pos]);
212 } else if (op->flag & OPP_STRINGLIST) {
215 tmp = kmalloc (i, GFP_KERNEL);
221 for (p = op->value; p < op->value + op->len; p++) {
231 copy_to_user(buf, tmp + k, count);
236 } else if (op->flag & OPP_BINARY) {
239 int first_off, last_cnt;
241 first = ((u32 *)op->value) + k / 9;
243 last = ((u32 *)op->value) + (k + count - 1) / 9;
244 last_cnt = (k + count) % 9;
245 if (!last_cnt) last_cnt = 9;
248 sprintf (buffer, "%08x.", *first);
249 copy_to_user (buf, buffer + first_off, last_cnt - first_off);
250 buf += last_cnt - first_off;
252 for (q = first; q <= last; q++) {
253 sprintf (buffer, "%08x.", *q);
255 copy_to_user (buf, buffer + first_off,
257 buf += 9 - first_off;
258 } else if (q == last) {
259 copy_to_user (buf, buffer, last_cnt);
262 copy_to_user (buf, buffer, 9);
268 if (last == (u32 *)(op->value + op->len - 4) && last_cnt == 9)
269 __put_user('\n', (buf - 1));
273 } else if (op->flag & OPP_HEXSTRING) {
276 if ((k < i - 1) && (k & 1)) {
277 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
278 __put_user(buffer[1], &buf[k++ - pos]);
282 for (; (count > 1) && (k < i - 1); k += 2) {
283 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
284 copy_to_user (buf + k - pos, buffer, 2);
288 if (count && (k < i - 1)) {
289 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
290 __put_user(buffer[0], &buf[k++ - pos]);
295 __put_user('\n', &buf [k++ - pos]);
302 static ssize_t property_write(struct file *filp, const char *buf,
303 size_t count, loff_t *ppos)
310 openprom_property *op;
312 if (pos < 0 || pos >= 0xffffff)
314 if (!filp->private_data) {
315 i = property_read (filp, NULL, 0, 0);
320 op = (openprom_property *)filp->private_data;
321 if (!(op->flag & OPP_STRING)) {
323 int first_off, last_cnt;
329 for (i = 0; i < count; i++, j++) {
333 __get_user(ctmp, &buf[i]);
336 if (op->flag & OPP_BINARY)
339 goto write_try_string;
348 __get_user(ctmp, &buf[i]);
350 (ctmp > '9' && ctmp < 'A') ||
351 (ctmp > 'F' && ctmp < 'a') ||
353 if (op->flag & OPP_BINARY)
356 goto write_try_string;
360 op->flag |= OPP_BINARY;
362 i = ((count + k + 8) / 9) << 2;
363 if (op->alloclen <= i) {
364 b = kmalloc (sizeof (openprom_property) + 2 * i,
368 memcpy (b, filp->private_data,
369 sizeof (openprom_property)
370 + strlen (op->name) + op->alloclen);
371 memset (((char *)b) + sizeof (openprom_property)
372 + strlen (op->name) + op->alloclen,
373 0, 2 * i - op->alloclen);
374 op = (openprom_property *)b;
376 b = filp->private_data;
377 filp->private_data = (void *)op;
380 first = ((u32 *)op->value) + (k / 9);
382 last = (u32 *)(op->value + i);
383 last_cnt = (k + count) % 9;
384 if (first + 1 == last) {
385 memset (tmp, '0', 8);
386 copy_from_user (tmp + first_off, buf,
387 (count + first_off > 8) ? 8 - first_off : count);
390 for (j = 0; j < first_off; j++)
392 for (j = 8 - count - first_off; j > 0; j--)
397 *first |= simple_strtoul (tmp, 0, 16);
398 op->flag |= OPP_DIRTY;
401 op->flag |= OPP_DIRTY;
402 for (q = first; q < last; q++) {
405 memset (tmp, '0', 8);
406 copy_from_user (tmp + first_off, buf,
409 for (j = 0; j < first_off; j++)
412 *q |= simple_strtoul (tmp,0,16);
415 } else if ((q == last - 1) && last_cnt
417 memset (tmp, '0', 8);
418 copy_from_user (tmp, buf, last_cnt);
420 for (j = 0; j < 8 - last_cnt; j++)
423 *q |= simple_strtoul (tmp, 0, 16);
426 char tchars[17]; /* XXX yuck... */
428 copy_from_user(tchars, buf, 16);
429 *q = simple_strtoul (tchars, 0, 16);
443 if (!(op->flag & OPP_BINARY)) {
444 if (!(op->flag & (OPP_QUOTED | OPP_NOTQUOTED))) {
447 /* No way, if somebody starts writing from the middle,
448 * we don't know whether he uses quotes around or not
452 __get_user(ctmp, buf);
454 op->flag |= OPP_QUOTED;
460 op->flag |= OPP_STRING;
464 op->flag |= OPP_NOTQUOTED;
466 op->flag |= OPP_STRING;
467 if (op->alloclen <= count + pos) {
468 b = kmalloc (sizeof (openprom_property)
469 + 2 * (count + pos), GFP_KERNEL);
472 memcpy (b, filp->private_data,
473 sizeof (openprom_property)
474 + strlen (op->name) + op->alloclen);
475 memset (((char *)b) + sizeof (openprom_property)
476 + strlen (op->name) + op->alloclen,
477 0, 2*(count - pos) - op->alloclen);
478 op = (openprom_property *)b;
479 op->alloclen = 2*(count + pos);
480 b = filp->private_data;
481 filp->private_data = (void *)op;
484 p = op->value + pos - ((op->flag & OPP_QUOTED) ? 1 : 0);
485 copy_from_user (p, buf, count);
486 op->flag |= OPP_DIRTY;
487 for (i = 0; i < count; i++, p++)
493 op->len = p - op->value;
496 if ((p > op->value) && (op->flag & OPP_QUOTED)
497 && (*(p - 1) == '\''))
500 if (p - op->value > op->len)
501 op->len = p - op->value;
509 int property_release (struct inode *inode, struct file *filp)
511 openprom_property *op = (openprom_property *)filp->private_data;
519 node = nodes[(u16)((long)inode->u.generic_ip)].node;
520 if ((u16)((long)inode->u.generic_ip) == aliases) {
521 if ((op->flag & OPP_DIRTY) && (op->flag & OPP_STRING)) {
523 int i = (op->value - op->name) - strlen (op->name) - 1;
524 op->value [op->len] = 0;
525 *(op->value - 1) = ' ';
527 for (p = op->value - i - 2; p >= op->name; p--)
531 memcpy (p - 8, "nvalias ", 8);
534 } else if (op->flag & OPP_DIRTY) {
535 if (op->flag & OPP_STRING) {
536 op->value [op->len] = 0;
537 save_and_cli (flags);
538 error = prom_setprop (node, op->name,
539 op->value, op->len + 1);
540 restore_flags (flags);
542 printk (KERN_WARNING "openpromfs: "
543 "Couldn't write property %s\n",
545 } else if ((op->flag & OPP_BINARY) || !op->len) {
546 save_and_cli (flags);
547 error = prom_setprop (node, op->name,
549 restore_flags (flags);
551 printk (KERN_WARNING "openpromfs: "
552 "Couldn't write property %s\n",
555 printk (KERN_WARNING "openpromfs: "
556 "Unknown property type of %s\n",
561 kfree (filp->private_data);
565 static struct file_operations openpromfs_prop_ops = {
567 write: property_write,
568 release: property_release,
571 static struct file_operations openpromfs_nodenum_ops = {
575 static struct file_operations openprom_operations = {
576 read: generic_read_dir,
577 readdir: openpromfs_readdir,
580 static struct inode_operations openprom_alias_inode_operations = {
581 create: openpromfs_create,
582 lookup: openpromfs_lookup,
583 unlink: openpromfs_unlink,
586 static struct inode_operations openprom_inode_operations = {
587 lookup: openpromfs_lookup,
590 static int lookup_children(u16 n, const char * name, int len)
594 for (; n != 0xffff; n = nodes[n].next) {
595 node = nodes[n].child;
596 if (node != 0xffff) {
601 while (node != 0xffff) {
602 if (prom_getname (nodes[node].node,
606 && !strncmp (buffer, name, len))
607 return NODE2INO(node);
608 p = strchr (buffer, '@');
609 if (p && (len == p - buffer)
610 && !strncmp (buffer, name, len))
611 return NODE2INO(node);
613 node = nodes[node].next;
617 ret = lookup_children (nodes[n].child, name, len);
623 static struct dentry *openpromfs_lookup(struct inode * dir, struct dentry *dentry)
627 #define OPFSL_PROPERTY 1
628 #define OPFSL_NODENUM 2
641 name = dentry->d_name.name;
642 len = dentry->d_name.len;
643 if (name [0] == '.' && len == 5 && !strncmp (name + 1, "node", 4)) {
644 ino = NODEP2INO(NODE(dir->i_ino).first_prop);
645 type = OPFSL_NODENUM;
648 u16 node = NODE(dir->i_ino).child;
649 while (node != 0xffff) {
650 if (prom_getname (nodes[node].node, buffer, 128) >= 0) {
652 if (len == i && !strncmp (buffer, name, len)) {
653 ino = NODE2INO(node);
657 p = strchr (buffer, '@');
658 if (p && (len == p - buffer)
659 && !strncmp (buffer, name, len)) {
660 ino = NODE2INO(node);
665 node = nodes[node].next;
668 n = NODE(dir->i_ino).node;
669 dirnode = dir->i_ino - OPENPROM_FIRST_INO;
671 int j = NODEP2INO(NODE(dir->i_ino).first_prop);
672 if (dirnode != aliases) {
673 for (p = prom_firstprop (n, buffer2);
675 p = prom_nextprop (n, p, buffer2)) {
677 if ((len == strlen (p))
678 && !strncmp (p, name, len)) {
680 type = OPFSL_PROPERTY;
686 for (k = 0; k < aliases_nodes; k++) {
689 && (len == strlen (alias_names [k]))
690 && !strncmp (alias_names [k], name, len)) {
692 type = OPFSL_PROPERTY;
699 ino = lookup_children (NODE(dir->i_ino).child, name, len);
703 return ERR_PTR(-ENOENT);
705 inode = iget (dir->i_sb, ino);
707 return ERR_PTR(-EINVAL);
710 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
711 if (ino == OPENPROM_FIRST_INO + aliases) {
712 inode->i_mode |= S_IWUSR;
713 inode->i_op = &openprom_alias_inode_operations;
715 inode->i_op = &openprom_inode_operations;
716 inode->i_fop = &openprom_operations;
720 inode->i_mode = S_IFREG | S_IRUGO;
721 inode->i_fop = &openpromfs_nodenum_ops;
723 inode->u.generic_ip = (void *)(long)(n);
726 if ((dirnode == options) && (len == 17)
727 && !strncmp (name, "security-password", 17))
728 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
730 inode->i_mode = S_IFREG | S_IRUGO;
731 if (dirnode == options || dirnode == aliases) {
732 if (len != 4 || strncmp (name, "name", 4))
733 inode->i_mode |= S_IWUSR;
736 inode->i_fop = &openpromfs_prop_ops;
738 if (inode->i_size < 0)
740 inode->u.generic_ip = (void *)(long)(((u16)dirnode) |
741 (((u16)(ino - NODEP2INO(NODE(dir->i_ino).first_prop) - 1)) << 16));
748 d_add(dentry, inode);
752 static int openpromfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
754 struct inode *inode = filp->f_dentry->d_inode;
767 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) return 0;
772 if (filldir(dirent, "..", 2, i,
773 (NODE(ino).parent == 0xffff) ?
774 OPENPROM_ROOT_INO : NODE2INO(NODE(ino).parent), DT_DIR) < 0)
781 node = NODE(ino).child;
782 while (i && node != 0xffff) {
783 node = nodes[node].next;
786 while (node != 0xffff) {
787 if (prom_getname (nodes[node].node, buffer, 128) < 0)
789 if (filldir(dirent, buffer, strlen(buffer),
790 filp->f_pos, NODE2INO(node), DT_DIR) < 0)
793 node = nodes[node].next;
795 j = NODEP2INO(NODE(ino).first_prop);
797 if (filldir(dirent, ".node", 5, filp->f_pos, j, DT_REG) < 0)
803 if (ino == OPENPROM_FIRST_INO + aliases) {
804 for (j++; i < aliases_nodes; i++, j++) {
805 if (alias_names [i]) {
806 if (filldir (dirent, alias_names [i],
807 strlen (alias_names [i]),
808 filp->f_pos, j, DT_REG) < 0) return 0;
813 for (p = prom_firstprop (n, buffer2);
815 p = prom_nextprop (n, p, buffer2)) {
819 if (filldir(dirent, p, strlen(p),
820 filp->f_pos, j, DT_REG) < 0)
830 static int openpromfs_create (struct inode *dir, struct dentry *dentry, int mode)
837 if (dentry->d_name.len > 256)
839 if (aliases_nodes == ALIASES_NNODES)
841 p = kmalloc (dentry->d_name.len + 1, GFP_KERNEL);
844 strncpy (p, dentry->d_name.name, dentry->d_name.len);
845 p [dentry->d_name.len] = 0;
846 alias_names [aliases_nodes++] = p;
847 inode = iget (dir->i_sb,
848 NODEP2INO(NODE(dir->i_ino).first_prop) + aliases_nodes);
851 inode->i_mode = S_IFREG | S_IRUGO | S_IWUSR;
852 inode->i_fop = &openpromfs_prop_ops;
854 if (inode->i_size < 0) inode->i_size = 0;
855 inode->u.generic_ip = (void *)(long)(((u16)aliases) |
856 (((u16)(aliases_nodes - 1)) << 16));
857 d_instantiate(dentry, inode);
861 static int openpromfs_unlink (struct inode *dir, struct dentry *dentry)
868 name = dentry->d_name.name;
869 len = dentry->d_name.len;
870 for (i = 0; i < aliases_nodes; i++)
871 if ((strlen (alias_names [i]) == len)
872 && !strncmp (name, alias_names[i], len)) {
876 alias_names [i] = NULL;
878 strcpy (buffer, "nvunalias ");
879 memcpy (buffer + 10, name, len);
880 buffer [10 + len] = 0;
886 /* {{{ init section */
888 static int __init check_space (u16 n)
890 static int check_space (u16 n)
895 if ((1 << alloced) * PAGE_SIZE < (n + 2) * sizeof(openpromfs_node)) {
896 pages = __get_free_pages (GFP_KERNEL, alloced + 1);
901 memcpy ((char *)pages, (char *)nodes,
902 (1 << alloced) * PAGE_SIZE);
903 free_pages ((unsigned long)nodes, alloced);
906 nodes = (openpromfs_node *)pages;
912 static u16 __init get_nodes (u16 parent, u32 node)
914 static u16 get_nodes (u16 parent, u32 node)
918 u16 n = last_node++, i;
921 if (check_space (n) < 0)
923 nodes[n].parent = parent;
924 nodes[n].node = node;
925 nodes[n].next = 0xffff;
926 nodes[n].child = 0xffff;
927 nodes[n].first_prop = first_prop++;
932 if ((j = prom_getproperty (node, "name", buffer, 8)) >= 0) {
934 if (!strcmp (buffer, "options"))
936 else if (!strcmp (buffer, "aliases"))
941 for (p = prom_firstprop (node, buffer);
942 p && p != (char *)-1 && *p;
943 p = prom_nextprop (node, p, buffer))
947 for (p = prom_firstprop (node, buffer);
948 p && p != (char *)-1 && *p;
949 p = prom_nextprop (node, p, buffer)) {
950 if (aliases_nodes == ALIASES_NNODES)
952 for (i = 0; i < aliases_nodes; i++)
953 if (!strcmp (p, alias_names [i]))
955 if (i < aliases_nodes)
957 q = kmalloc (strlen (p) + 1, GFP_KERNEL);
961 alias_names [aliases_nodes++] = q;
963 first_prop += ALIASES_NNODES;
965 node = prom_getchild (node);
967 parent = get_nodes (n, node);
968 if (parent == 0xffff)
970 nodes[n].child = parent;
971 while ((node = prom_getsibling (node)) != 0) {
972 i = get_nodes (n, node);
975 nodes[parent].next = i;
982 static void openprom_read_inode(struct inode * inode)
984 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
985 if (inode->i_ino == OPENPROM_ROOT_INO) {
986 inode->i_op = &openprom_inode_operations;
987 inode->i_fop = &openprom_operations;
988 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
992 static int openprom_statfs(struct super_block *sb, struct statfs *buf)
994 buf->f_type = OPENPROM_SUPER_MAGIC;
995 buf->f_bsize = PAGE_SIZE/sizeof(long); /* ??? */
999 buf->f_namelen = NAME_MAX;
1003 static struct super_operations openprom_sops = {
1004 read_inode: openprom_read_inode,
1005 statfs: openprom_statfs,
1008 struct super_block *openprom_read_super(struct super_block *s,void *data,
1011 struct inode * root_inode;
1013 s->s_blocksize = 1024;
1014 s->s_blocksize_bits = 10;
1015 s->s_magic = OPENPROM_SUPER_MAGIC;
1016 s->s_op = &openprom_sops;
1017 root_inode = iget(s, OPENPROM_ROOT_INO);
1020 s->s_root = d_alloc_root(root_inode);
1026 printk("openprom_read_super: get root inode failed\n");
1031 static DECLARE_FSTYPE(openprom_fs_type, "openpromfs", openprom_read_super, 0);
1033 static int __init init_openprom_fs(void)
1035 nodes = (openpromfs_node *)__get_free_pages(GFP_KERNEL, 0);
1037 printk (KERN_WARNING "openpromfs: can't get free page\n");
1040 if (get_nodes (0xffff, prom_root_node) == 0xffff) {
1041 printk (KERN_WARNING "openpromfs: couldn't setup tree\n");
1044 nodes[last_node].first_prop = first_prop;
1045 return register_filesystem(&openprom_fs_type);
1048 static void __exit exit_openprom_fs(void)
1051 unregister_filesystem(&openprom_fs_type);
1052 free_pages ((unsigned long)nodes, alloced);
1053 for (i = 0; i < aliases_nodes; i++)
1054 if (alias_names [i])
1055 kfree (alias_names [i]);
1061 module_init(init_openprom_fs)
1062 module_exit(exit_openprom_fs)
1063 MODULE_LICENSE("GPL");