1 /* $Id: inode.c,v 1.13 2000/08/12 13:25:46 davem Exp $
2 * openpromfs.c: /proc/openprom handling routines
3 *
4 * Copyright (C) 1996-1999 Jakub Jelinek (jakub@redhat.com)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
6 */
7
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/fs.h>
12 #include <linux/openprom_fs.h>
13 #include <linux/locks.h>
14 #include <linux/init.h>
15 #include <linux/malloc.h>
16 #include <linux/smp_lock.h>
17
18 #include <asm/openprom.h>
19 #include <asm/oplib.h>
20 #include <asm/uaccess.h>
21
22 #define ALIASES_NNODES 64
23
24 typedef struct {
25 u16 parent;
26 u16 next;
27 u16 child;
28 u16 first_prop;
29 u32 node;
30 } openpromfs_node;
31
32 typedef struct {
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
41 u32 flag;
42 u32 alloclen;
43 u32 len;
44 char *value;
45 char name[8];
46 } openprom_property;
47
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];
56
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)
62
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);
67
68 static ssize_t nodenum_read(struct file *file, char *buf,
69 size_t count, loff_t *ppos)
70 {
71 struct inode *inode = file->f_dentry->d_inode;
72 char buffer[10];
73
74 if (count < 0 || !inode->u.generic_ip)
75 return -EINVAL;
76 sprintf (buffer, "%8.8x\n", (u32)(long)(inode->u.generic_ip));
77 if (file->f_pos >= 9)
78 return 0;
79 if (count > 9 - file->f_pos)
80 count = 9 - file->f_pos;
81 copy_to_user(buf, buffer + file->f_pos, count);
82 file->f_pos += count;
83 return count;
84 }
85
86 static ssize_t property_read(struct file *filp, char *buf,
87 size_t count, loff_t *ppos)
88 {
89 struct inode *inode = filp->f_dentry->d_inode;
90 int i, j, k;
91 u32 node;
92 char *p, *s;
93 u32 *q;
94 openprom_property *op;
95 char buffer[64];
96
97 if (filp->f_pos >= 0xffffff)
98 return -EINVAL;
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)
104 p = 0;
105 else
106 p = alias_names [i];
107 } else
108 for (p = prom_firstprop (node, buffer);
109 i && p && *p;
110 p = prom_nextprop (node, p, buffer), i--)
111 /* nothing */ ;
112 if (!p || !*p)
113 return -EIO;
114 i = prom_getproplen (node, p);
115 if (i < 0) {
116 if ((u16)((long)inode->u.generic_ip) == aliases)
117 i = 0;
118 else
119 return -EIO;
120 }
121 k = i;
122 if (i < 64) i = 64;
123 filp->private_data = kmalloc (sizeof (openprom_property)
124 + (j = strlen (p)) + 2 * i,
125 GFP_KERNEL);
126 if (!filp->private_data)
127 return -ENOMEM;
128 op = (openprom_property *)filp->private_data;
129 op->flag = 0;
130 op->alloclen = 2 * i;
131 strcpy (op->name, p);
132 op->value = (char *)(((unsigned long)(op->name + j + 4)) & ~3);
133 op->len = k;
134 if (k && prom_getproperty (node, p, op->value, i) < 0)
135 return -EIO;
136 op->value [k] = 0;
137 if (k) {
138 for (s = 0, p = op->value; p < op->value + k; p++) {
139 if ((*p >= ' ' && *p <= '~') || *p == '\n') {
140 op->flag |= OPP_STRING;
141 s = p;
142 continue;
143 }
144 if (p > op->value && !*p && s == p - 1) {
145 if (p < op->value + k - 1)
146 op->flag |= OPP_STRINGLIST;
147 else
148 op->flag |= OPP_ASCIIZ;
149 continue;
150 }
151 if (k == 1 && !*p) {
152 op->flag |= (OPP_STRING|OPP_ASCIIZ);
153 break;
154 }
155 op->flag &= ~(OPP_STRING|OPP_STRINGLIST);
156 if (k & 3)
157 op->flag |= OPP_HEXSTRING;
158 else
159 op->flag |= OPP_BINARY;
160 break;
161 }
162 if (op->flag & OPP_STRINGLIST)
163 op->flag &= ~(OPP_STRING);
164 if (op->flag & OPP_ASCIIZ)
165 op->len--;
166 }
167 } else
168 op = (openprom_property *)filp->private_data;
169 if (!count || !(op->len || (op->flag & OPP_ASCIIZ)))
170 return 0;
171 if (op->flag & OPP_STRINGLIST) {
172 for (k = 0, p = op->value; p < op->value + op->len; p++)
173 if (!*p)
174 k++;
175 i = op->len + 4 * k + 3;
176 } else if (op->flag & OPP_STRING) {
177 i = op->len + 3;
178 } else if (op->flag & OPP_BINARY) {
179 i = (op->len * 9) >> 2;
180 } else {
181 i = (op->len << 1) + 1;
182 }
183 k = filp->f_pos;
184 if (k >= i) return 0;
185 if (count > i - k) count = i - k;
186 if (op->flag & OPP_STRING) {
187 if (!k) {
188 __put_user('\'', buf);
189 k++;
190 count--;
191 }
192
193 if (k + count >= i - 2)
194 j = i - 2 - k;
195 else
196 j = count;
197
198 if (j >= 0) {
199 copy_to_user(buf + k - filp->f_pos,
200 op->value + k - 1, j);
201 count -= j;
202 k += j;
203 }
204
205 if (count)
206 __put_user('\'', &buf [k++ - filp->f_pos]);
207 if (count > 1)
208 __put_user('\n', &buf [k++ - filp->f_pos]);
209
210 } else if (op->flag & OPP_STRINGLIST) {
211 char *tmp;
212
213 tmp = kmalloc (i, GFP_KERNEL);
214 if (!tmp)
215 return -ENOMEM;
216
217 s = tmp;
218 *s++ = '\'';
219 for (p = op->value; p < op->value + op->len; p++) {
220 if (!*p) {
221 strcpy(s, "' + '");
222 s += 5;
223 continue;
224 }
225 *s++ = *p;
226 }
227 strcpy(s, "'\n");
228
229 copy_to_user(buf, tmp + k, count);
230
231 kfree(tmp);
232 k += count;
233
234 } else if (op->flag & OPP_BINARY) {
235 char buffer[10];
236 u32 *first, *last;
237 int first_off, last_cnt;
238
239 first = ((u32 *)op->value) + k / 9;
240 first_off = k % 9;
241 last = ((u32 *)op->value) + (k + count - 1) / 9;
242 last_cnt = (k + count) % 9;
243 if (!last_cnt) last_cnt = 9;
244
245 if (first == last) {
246 sprintf (buffer, "%08x.", *first);
247 copy_to_user (buf, buffer + first_off, last_cnt - first_off);
248 buf += last_cnt - first_off;
249 } else {
250 for (q = first; q <= last; q++) {
251 sprintf (buffer, "%08x.", *q);
252 if (q == first) {
253 copy_to_user (buf, buffer + first_off,
254 9 - first_off);
255 buf += 9 - first_off;
256 } else if (q == last) {
257 copy_to_user (buf, buffer, last_cnt);
258 buf += last_cnt;
259 } else {
260 copy_to_user (buf, buffer, 9);
261 buf += 9;
262 }
263 }
264 }
265
266 if (last == (u32 *)(op->value + op->len - 4) && last_cnt == 9)
267 __put_user('\n', (buf - 1));
268
269 k += count;
270
271 } else if (op->flag & OPP_HEXSTRING) {
272 char buffer[2];
273
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]);
277 count--;
278 }
279
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);
283 count -= 2;
284 }
285
286 if (count && (k < i - 1)) {
287 sprintf (buffer, "%02x", *(op->value + (k >> 1)));
288 __put_user(buffer[0], &buf[k++ - filp->f_pos]);
289 count--;
290 }
291
292 if (count)
293 __put_user('\n', &buf [k++ - filp->f_pos]);
294 }
295 count = k - filp->f_pos;
296 filp->f_pos = k;
297 return count;
298 }
299
300 static ssize_t property_write(struct file *filp, const char *buf,
301 size_t count, loff_t *ppos)
302 {
303 int i, j, k;
304 char *p;
305 u32 *q;
306 void *b;
307 openprom_property *op;
308
309 if (filp->f_pos >= 0xffffff)
310 return -EINVAL;
311 if (!filp->private_data) {
312 i = property_read (filp, NULL, 0, 0);
313 if (i)
314 return i;
315 }
316 k = filp->f_pos;
317 op = (openprom_property *)filp->private_data;
318 if (!(op->flag & OPP_STRING)) {
319 u32 *first, *last;
320 int first_off, last_cnt;
321 u32 mask, mask2;
322 char tmp [9];
323 int forcelen = 0;
324
325 j = k % 9;
326 for (i = 0; i < count; i++, j++) {
327 if (j == 9) j = 0;
328 if (!j) {
329 char ctmp;
330 __get_user(ctmp, &buf[i]);
331 if (ctmp != '.') {
332 if (ctmp != '\n') {
333 if (op->flag & OPP_BINARY)
334 return -EINVAL;
335 else
336 goto write_try_string;
337 } else {
338 count = i + 1;
339 forcelen = 1;
340 break;
341 }
342 }
343 } else {
344 char ctmp;
345 __get_user(ctmp, &buf[i]);
346 if (ctmp < '' ||
347 (ctmp > '9' && ctmp < 'A') ||
348 (ctmp > 'F' && ctmp < 'a') ||
349 ctmp > 'f') {
350 if (op->flag & OPP_BINARY)
351 return -EINVAL;
352 else
353 goto write_try_string;
354 }
355 }
356 }
357 op->flag |= OPP_BINARY;
358 tmp [8] = 0;
359 i = ((count + k + 8) / 9) << 2;
360 if (op->alloclen <= i) {
361 b = kmalloc (sizeof (openprom_property) + 2 * i,
362 GFP_KERNEL);
363 if (!b)
364 return -ENOMEM;
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;
372 op->alloclen = 2*i;
373 b = filp->private_data;
374 filp->private_data = (void *)op;
375 kfree (b);
376 }
377 first = ((u32 *)op->value) + (k / 9);
378 first_off = k % 9;
379 last = (u32 *)(op->value + i);
380 last_cnt = (k + count) % 9;
381 if (first + 1 == last) {
382 memset (tmp, '', 8);
383 copy_from_user (tmp + first_off, buf,
384 (count + first_off > 8) ? 8 - first_off : count);
385 mask = 0xffffffff;
386 mask2 = 0xffffffff;
387 for (j = 0; j < first_off; j++)
388 mask >>= 1;
389 for (j = 8 - count - first_off; j > 0; j--)
390 mask2 <<= 1;
391 mask &= mask2;
392 if (mask) {
393 *first &= ~mask;
394 *first |= simple_strtoul (tmp, 0, 16);
395 op->flag |= OPP_DIRTY;
396 }
397 } else {
398 op->flag |= OPP_DIRTY;
399 for (q = first; q < last; q++) {
400 if (q == first) {
401 if (first_off < 8) {
402 memset (tmp, '', 8);
403 copy_from_user (tmp + first_off, buf,
404 8 - first_off);
405 mask = 0xffffffff;
406 for (j = 0; j < first_off; j++)
407 mask >>= 1;
408 *q &= ~mask;
409 *q |= simple_strtoul (tmp,0,16);
410 }
411 buf += 9;
412 } else if ((q == last - 1) && last_cnt
413 && (last_cnt < 8)) {
414 memset (tmp, '', 8);
415 copy_from_user (tmp, buf, last_cnt);
416 mask = 0xffffffff;
417 for (j = 0; j < 8 - last_cnt; j++)
418 mask <<= 1;
419 *q &= ~mask;
420 *q |= simple_strtoul (tmp, 0, 16);
421 buf += last_cnt;
422 } else {
423 char tchars[17]; /* XXX yuck... */
424
425 copy_from_user(tchars, buf, 16);
426 *q = simple_strtoul (tchars, 0, 16);
427 buf += 9;
428 }
429 }
430 }
431 if (!forcelen) {
432 if (op->len < i)
433 op->len = i;
434 } else
435 op->len = i;
436 filp->f_pos += count;
437 }
438 write_try_string:
439 if (!(op->flag & OPP_BINARY)) {
440 if (!(op->flag & (OPP_QUOTED | OPP_NOTQUOTED))) {
441 char ctmp;
442
443 /* No way, if somebody starts writing from the middle,
444 * we don't know whether he uses quotes around or not
445 */
446 if (k > 0)
447 return -EINVAL;
448 __get_user(ctmp, buf);
449 if (ctmp == '\'') {
450 op->flag |= OPP_QUOTED;
451 buf++;
452 count--;
453 filp->f_pos++;
454 if (!count) {
455 op->flag |= OPP_STRING;
456 return 1;
457 }
458 } else
459 op->flag |= OPP_NOTQUOTED;
460 }
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);
465 if (!b)
466 return -ENOMEM;
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;
477 kfree (b);
478 }
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++)
483 if (*p == '\n') {
484 *p = 0;
485 break;
486 }
487 if (i < count) {
488 op->len = p - op->value;
489 filp->f_pos += i + 1;
490 if ((p > op->value) && (op->flag & OPP_QUOTED)
491 && (*(p - 1) == '\''))
492 op->len--;
493 } else {
494 if (p - op->value > op->len)
495 op->len = p - op->value;
496 filp->f_pos += count;
497 }
498 }
499 return filp->f_pos - k;
500 }
501
502 int property_release (struct inode *inode, struct file *filp)
503 {
504 openprom_property *op = (openprom_property *)filp->private_data;
505 unsigned long flags;
506 int error;
507 u32 node;
508
509 if (!op)
510 return 0;
511 lock_kernel();
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)) {
515 char *p = op->name;
516 int i = (op->value - op->name) - strlen (op->name) - 1;
517 op->value [op->len] = 0;
518 *(op->value - 1) = ' ';
519 if (i) {
520 for (p = op->value - i - 2; p >= op->name; p--)
521 p[i] = *p;
522 p = op->name + i;
523 }
524 memcpy (p - 8, "nvalias ", 8);
525 prom_feval (p - 8);
526 }
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);
534 if (error <= 0)
535 printk (KERN_WARNING "openpromfs: "
536 "Couldn't write property %s\n",
537 op->name);
538 } else if ((op->flag & OPP_BINARY) || !op->len) {
539 save_and_cli (flags);
540 error = prom_setprop (node, op->name,
541 op->value, op->len);
542 restore_flags (flags);
543 if (error <= 0)
544 printk (KERN_WARNING "openpromfs: "
545 "Couldn't write property %s\n",
546 op->name);
547 } else {
548 printk (KERN_WARNING "openpromfs: "
549 "Unknown property type of %s\n",
550 op->name);
551 }
552 }
553 unlock_kernel();
554 kfree (filp->private_data);
555 return 0;
556 }
557
558 static struct file_operations openpromfs_prop_ops = {
559 read: property_read,
560 write: property_write,
561 release: property_release,
562 };
563
564 static struct file_operations openpromfs_nodenum_ops = {
565 read: nodenum_read,
566 };
567
568 static struct file_operations openprom_operations = {
569 read: generic_read_dir,
570 readdir: openpromfs_readdir,
571 };
572
573 static struct inode_operations openprom_alias_inode_operations = {
574 create: openpromfs_create,
575 lookup: openpromfs_lookup,
576 unlink: openpromfs_unlink,
577 };
578
579 static struct inode_operations openprom_inode_operations = {
580 lookup: openpromfs_lookup,
581 };
582
583 static int lookup_children(u16 n, const char * name, int len)
584 {
585 int ret;
586 u16 node;
587 for (; n != 0xffff; n = nodes[n].next) {
588 node = nodes[n].child;
589 if (node != 0xffff) {
590 char buffer[128];
591 int i;
592 char *p;
593
594 while (node != 0xffff) {
595 if (prom_getname (nodes[node].node,
596 buffer, 128) >= 0) {
597 i = strlen (buffer);
598 if ((len == i)
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);
605 }
606 node = nodes[node].next;
607 }
608 } else
609 continue;
610 ret = lookup_children (nodes[n].child, name, len);
611 if (ret) return ret;
612 }
613 return 0;
614 }
615
616 static struct dentry *openpromfs_lookup(struct inode * dir, struct dentry *dentry)
617 {
618 int ino = 0;
619 #define OPFSL_DIR 0
620 #define OPFSL_PROPERTY 1
621 #define OPFSL_NODENUM 2
622 int type = 0;
623 char buffer[128];
624 char *p;
625 const char *name;
626 u32 n;
627 u16 dirnode;
628 unsigned int len;
629 int i;
630 struct inode *inode;
631 char buffer2[64];
632
633 inode = NULL;
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;
639 }
640 if (!ino) {
641 u16 node = NODE(dir->i_ino).child;
642 while (node != 0xffff) {
643 if (prom_getname (nodes[node].node, buffer, 128) >= 0) {
644 i = strlen (buffer);
645 if (len == i && !strncmp (buffer, name, len)) {
646 ino = NODE2INO(node);
647 type = OPFSL_DIR;
648 break;
649 }
650 p = strchr (buffer, '@');
651 if (p && (len == p - buffer)
652 && !strncmp (buffer, name, len)) {
653 ino = NODE2INO(node);
654 type = OPFSL_DIR;
655 break;
656 }
657 }
658 node = nodes[node].next;
659 }
660 }
661 n = NODE(dir->i_ino).node;
662 dirnode = dir->i_ino - OPENPROM_FIRST_INO;
663 if (!ino) {
664 int j = NODEP2INO(NODE(dir->i_ino).first_prop);
665 if (dirnode != aliases) {
666 for (p = prom_firstprop (n, buffer2);
667 p && *p;
668 p = prom_nextprop (n, p, buffer2)) {
669 j++;
670 if ((len == strlen (p))
671 && !strncmp (p, name, len)) {
672 ino = j;
673 type = OPFSL_PROPERTY;
674 break;
675 }
676 }
677 } else {
678 int k;
679 for (k = 0; k < aliases_nodes; k++) {
680 j++;
681 if (alias_names [k]
682 && (len == strlen (alias_names [k]))
683 && !strncmp (alias_names [k], name, len)) {
684 ino = j;
685 type = OPFSL_PROPERTY;
686 break;
687 }
688 }
689 }
690 }
691 if (!ino) {
692 ino = lookup_children (NODE(dir->i_ino).child, name, len);
693 if (ino)
694 type = OPFSL_DIR;
695 else
696 return ERR_PTR(-ENOENT);
697 }
698 inode = iget (dir->i_sb, ino);
699 if (!inode)
700 return ERR_PTR(-EINVAL);
701 switch (type) {
702 case OPFSL_DIR:
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;
707 } else
708 inode->i_op = &openprom_inode_operations;
709 inode->i_fop = &openprom_operations;
710 inode->i_nlink = 2;
711 break;
712 case OPFSL_NODENUM:
713 inode->i_mode = S_IFREG | S_IRUGO;
714 inode->i_fop = &openpromfs_nodenum_ops;
715 inode->i_nlink = 1;
716 inode->u.generic_ip = (void *)(long)(n);
717 break;
718 case OPFSL_PROPERTY:
719 if ((dirnode == options) && (len == 17)
720 && !strncmp (name, "security-password", 17))
721 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
722 else {
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;
727 }
728 }
729 inode->i_fop = &openpromfs_prop_ops;
730 inode->i_nlink = 1;
731 if (inode->i_size < 0)
732 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));
735 break;
736 }
737
738 inode->i_gid = 0;
739 inode->i_uid = 0;
740
741 d_add(dentry, inode);
742 return NULL;
743 }
744
745 static int openpromfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
746 {
747 struct inode *inode = filp->f_dentry->d_inode;
748 unsigned int ino;
749 u32 n;
750 int i, j;
751 char buffer[128];
752 u16 node;
753 char *p;
754 char buffer2[64];
755
756 ino = inode->i_ino;
757 i = filp->f_pos;
758 switch (i) {
759 case 0:
760 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) return 0;
761 i++;
762 filp->f_pos++;
763 /* fall thru */
764 case 1:
765 if (filldir(dirent, "..", 2, i,
766 (NODE(ino).parent == 0xffff) ?
767 OPENPROM_ROOT_INO : NODE2INO(NODE(ino).parent), DT_DIR) < 0)
768 return 0;
769 i++;
770 filp->f_pos++;
771 /* fall thru */
772 default:
773 i -= 2;
774 node = NODE(ino).child;
775 while (i && node != 0xffff) {
776 node = nodes[node].next;
777 i--;
778 }
779 while (node != 0xffff) {
780 if (prom_getname (nodes[node].node, buffer, 128) < 0)
781 return 0;
782 if (filldir(dirent, buffer, strlen(buffer),
783 filp->f_pos, NODE2INO(node), DT_DIR) < 0)
784 return 0;
785 filp->f_pos++;
786 node = nodes[node].next;
787 }
788 j = NODEP2INO(NODE(ino).first_prop);
789 if (!i) {
790 if (filldir(dirent, ".node", 5, filp->f_pos, j, DT_REG) < 0)
791 return 0;
792 filp->f_pos++;
793 } else
794 i--;
795 n = NODE(ino).node;
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;
802 filp->f_pos++;
803 }
804 }
805 } else {
806 for (p = prom_firstprop (n, buffer2);
807 p && *p;
808 p = prom_nextprop (n, p, buffer2)) {
809 j++;
810 if (i) i--;
811 else {
812 if (filldir(dirent, p, strlen(p),
813 filp->f_pos, j, DT_REG) < 0)
814 return 0;
815 filp->f_pos++;
816 }
817 }
818 }
819 }
820 return 0;
821 }
822
823 static int openpromfs_create (struct inode *dir, struct dentry *dentry, int mode)
824 {
825 char *p;
826 struct inode *inode;
827
828 if (!dir)
829 return -ENOENT;
830 if (dentry->d_name.len > 256)
831 return -EINVAL;
832 if (aliases_nodes == ALIASES_NNODES)
833 return -EIO;
834 p = kmalloc (dentry->d_name.len + 1, GFP_KERNEL);
835 if (!p)
836 return -ENOMEM;
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);
842 if (!inode)
843 return -EINVAL;
844 inode->i_mode = S_IFREG | S_IRUGO | S_IWUSR;
845 inode->i_fop = &openpromfs_prop_ops;
846 inode->i_nlink = 1;
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);
851 return 0;
852 }
853
854 static int openpromfs_unlink (struct inode *dir, struct dentry *dentry)
855 {
856 unsigned int len;
857 char *p;
858 const char *name;
859 int i;
860
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)) {
866 char buffer[512];
867
868 p = alias_names [i];
869 alias_names [i] = NULL;
870 kfree (p);
871 strcpy (buffer, "nvunalias ");
872 memcpy (buffer + 10, name, len);
873 buffer [10 + len] = 0;
874 prom_feval (buffer);
875 }
876 return 0;
877 }
878
879 /* {{{ init section */
880 #ifndef MODULE
881 static int __init check_space (u16 n)
882 #else
883 static int check_space (u16 n)
884 #endif
885 {
886 unsigned long pages;
887
888 if ((1 << alloced) * PAGE_SIZE < (n + 2) * sizeof(openpromfs_node)) {
889 pages = __get_free_pages (GFP_KERNEL, alloced + 1);
890 if (!pages)
891 return -1;
892
893 if (nodes) {
894 memcpy ((char *)pages, (char *)nodes,
895 (1 << alloced) * PAGE_SIZE);
896 free_pages ((unsigned long)nodes, alloced);
897 }
898 alloced++;
899 nodes = (openpromfs_node *)pages;
900 }
901 return 0;
902 }
903
904 #ifndef MODULE
905 static u16 __init get_nodes (u16 parent, u32 node)
906 #else
907 static u16 get_nodes (u16 parent, u32 node)
908 #endif
909 {
910 char *p;
911 u16 n = last_node++, i;
912 char buffer[64];
913
914 if (check_space (n) < 0)
915 return 0xffff;
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++;
921 if (!parent) {
922 char buffer[8];
923 int j;
924
925 if ((j = prom_getproperty (node, "name", buffer, 8)) >= 0) {
926 buffer[j] = 0;
927 if (!strcmp (buffer, "options"))
928 options = n;
929 else if (!strcmp (buffer, "aliases"))
930 aliases = n;
931 }
932 }
933 if (n != aliases)
934 for (p = prom_firstprop (node, buffer);
935 p && p != (char *)-1 && *p;
936 p = prom_nextprop (node, p, buffer))
937 first_prop++;
938 else {
939 char *q;
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)
944 break;
945 for (i = 0; i < aliases_nodes; i++)
946 if (!strcmp (p, alias_names [i]))
947 break;
948 if (i < aliases_nodes)
949 continue;
950 q = kmalloc (strlen (p) + 1, GFP_KERNEL);
951 if (!q)
952 return 0xffff;
953 strcpy (q, p);
954 alias_names [aliases_nodes++] = q;
955 }
956 first_prop += ALIASES_NNODES;
957 }
958 node = prom_getchild (node);
959 if (node) {
960 parent = get_nodes (n, node);
961 if (parent == 0xffff)
962 return 0xffff;
963 nodes[n].child = parent;
964 while ((node = prom_getsibling (node)) != 0) {
965 i = get_nodes (n, node);
966 if (i == 0xffff)
967 return 0xffff;
968 nodes[parent].next = i;
969 parent = i;
970 }
971 }
972 return n;
973 }
974
975 static void openprom_read_inode(struct inode * inode)
976 {
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;
982 }
983 }
984
985 static int openprom_statfs(struct super_block *sb, struct statfs *buf)
986 {
987 buf->f_type = OPENPROM_SUPER_MAGIC;
988 buf->f_bsize = PAGE_SIZE/sizeof(long); /* ??? */
989 buf->f_bfree = 0;
990 buf->f_bavail = 0;
991 buf->f_ffree = 0;
992 buf->f_namelen = NAME_MAX;
993 return 0;
994 }
995
996 static struct super_operations openprom_sops = {
997 read_inode: openprom_read_inode,
998 statfs: openprom_statfs,
999 };
1000
1001 struct super_block *openprom_read_super(struct super_block *s,void *data,
1002 int silent)
1003 {
1004 struct inode * root_inode;
1005
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);
1011 if (!root_inode)
1012 goto out_no_root;
1013 s->s_root = d_alloc_root(root_inode);
1014 if (!s->s_root)
1015 goto out_no_root;
1016 return s;
1017
1018 out_no_root:
1019 printk("openprom_read_super: get root inode failed\n");
1020 iput(root_inode);
1021 return NULL;
1022 }
1023
1024 static DECLARE_FSTYPE(openprom_fs_type, "openpromfs", openprom_read_super, 0);
1025
1026 static int __init init_openprom_fs(void)
1027 {
1028 nodes = (openpromfs_node *)__get_free_pages(GFP_KERNEL, 0);
1029 if (!nodes) {
1030 printk (KERN_WARNING "openpromfs: can't get free page\n");
1031 return -EIO;
1032 }
1033 if (get_nodes (0xffff, prom_root_node) == 0xffff) {
1034 printk (KERN_WARNING "openpromfs: couldn't setup tree\n");
1035 return -EIO;
1036 }
1037 nodes[last_node].first_prop = first_prop;
1038 return register_filesystem(&openprom_fs_type);
1039 }
1040
1041 static void __exit exit_openprom_fs(void)
1042 {
1043 int i;
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]);
1049 nodes = NULL;
1050 }
1051
1052 EXPORT_NO_SYMBOLS;
1053
1054 module_init(init_openprom_fs)
1055 module_exit(exit_openprom_fs)
1056
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.