blob: 3bd2eeccb2ad3c7bdeb28db9e7e31c634a577a34 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: inode.c,v 1.15 2001/11/12 09:43:39 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/init.h>
14#include <linux/slab.h>
15#include <linux/smp_lock.h>
16
17#include <asm/openprom.h>
18#include <asm/oplib.h>
19#include <asm/uaccess.h>
20
21#define ALIASES_NNODES 64
22
23typedef struct {
24 u16 parent;
25 u16 next;
26 u16 child;
27 u16 first_prop;
28 u32 node;
29} openpromfs_node;
30
31typedef struct {
32#define OPP_STRING 0x10
33#define OPP_STRINGLIST 0x20
34#define OPP_BINARY 0x40
35#define OPP_HEXSTRING 0x80
36#define OPP_DIRTY 0x01
37#define OPP_QUOTED 0x02
38#define OPP_NOTQUOTED 0x04
39#define OPP_ASCIIZ 0x08
40 u32 flag;
41 u32 alloclen;
42 u32 len;
43 char *value;
44 char name[8];
45} openprom_property;
46
47static openpromfs_node *nodes;
48static int alloced;
49static u16 last_node;
50static u16 first_prop;
51static u16 options = 0xffff;
52static u16 aliases = 0xffff;
53static int aliases_nodes;
54static char *alias_names [ALIASES_NNODES];
55
56#define OPENPROM_ROOT_INO 16
57#define OPENPROM_FIRST_INO OPENPROM_ROOT_INO
58#define NODE(ino) nodes[ino - OPENPROM_FIRST_INO]
59#define NODE2INO(node) (node + OPENPROM_FIRST_INO)
60#define NODEP2INO(no) (no + OPENPROM_FIRST_INO + last_node)
61
62static int openpromfs_create (struct inode *, struct dentry *, int, struct nameidata *);
63static int openpromfs_readdir(struct file *, void *, filldir_t);
64static struct dentry *openpromfs_lookup(struct inode *, struct dentry *dentry, struct nameidata *nd);
65static int openpromfs_unlink (struct inode *, struct dentry *dentry);
66
67static ssize_t nodenum_read(struct file *file, char __user *buf,
68 size_t count, loff_t *ppos)
69{
70 struct inode *inode = file->f_dentry->d_inode;
71 char buffer[10];
72
73 if (count < 0 || !inode->u.generic_ip)
74 return -EINVAL;
Jan Engelhardt515decd2006-06-25 05:47:35 -070075 sprintf (buffer, "%8.8lx\n", (long)inode->u.generic_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (file->f_pos >= 9)
77 return 0;
78 if (count > 9 - file->f_pos)
79 count = 9 - file->f_pos;
80 if (copy_to_user(buf, buffer + file->f_pos, count))
81 return -EFAULT;
82 *ppos += count;
83 return count;
84}
85
86static ssize_t property_read(struct file *filp, char __user *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->private_data) {
98 node = nodes[(u16)((long)inode->u.generic_ip)].node;
99 i = ((u32)(long)inode->u.generic_ip) >> 16;
100 if ((u16)((long)inode->u.generic_ip) == aliases) {
101 if (i >= aliases_nodes)
102 p = NULL;
103 else
104 p = alias_names [i];
105 } else
106 for (p = prom_firstprop (node, buffer);
107 i && p && *p;
108 p = prom_nextprop (node, p, buffer), i--)
109 /* nothing */ ;
110 if (!p || !*p)
111 return -EIO;
112 i = prom_getproplen (node, p);
113 if (i < 0) {
114 if ((u16)((long)inode->u.generic_ip) == aliases)
115 i = 0;
116 else
117 return -EIO;
118 }
119 k = i;
120 if (i < 64) i = 64;
121 filp->private_data = kmalloc (sizeof (openprom_property)
122 + (j = strlen (p)) + 2 * i,
123 GFP_KERNEL);
124 if (!filp->private_data)
125 return -ENOMEM;
Jan Engelhardt515decd2006-06-25 05:47:35 -0700126 op = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 op->flag = 0;
128 op->alloclen = 2 * i;
129 strcpy (op->name, p);
130 op->value = (char *)(((unsigned long)(op->name + j + 4)) & ~3);
131 op->len = k;
132 if (k && prom_getproperty (node, p, op->value, i) < 0)
133 return -EIO;
134 op->value [k] = 0;
135 if (k) {
136 for (s = NULL, p = op->value; p < op->value + k; p++) {
137 if ((*p >= ' ' && *p <= '~') || *p == '\n') {
138 op->flag |= OPP_STRING;
139 s = p;
140 continue;
141 }
142 if (p > op->value && !*p && s == p - 1) {
143 if (p < op->value + k - 1)
144 op->flag |= OPP_STRINGLIST;
145 else
146 op->flag |= OPP_ASCIIZ;
147 continue;
148 }
149 if (k == 1 && !*p) {
150 op->flag |= (OPP_STRING|OPP_ASCIIZ);
151 break;
152 }
153 op->flag &= ~(OPP_STRING|OPP_STRINGLIST);
154 if (k & 3)
155 op->flag |= OPP_HEXSTRING;
156 else
157 op->flag |= OPP_BINARY;
158 break;
159 }
160 if (op->flag & OPP_STRINGLIST)
161 op->flag &= ~(OPP_STRING);
162 if (op->flag & OPP_ASCIIZ)
163 op->len--;
164 }
165 } else
Jan Engelhardt515decd2006-06-25 05:47:35 -0700166 op = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!count || !(op->len || (op->flag & OPP_ASCIIZ)))
168 return 0;
169 if (*ppos >= 0xffffff || count >= 0xffffff)
170 return -EINVAL;
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 = *ppos;
184 if (k >= i) return 0;
185 if (count > i - k) count = i - k;
186 if (op->flag & OPP_STRING) {
187 if (!k) {
188 if (put_user('\'', buf))
189 return -EFAULT;
190 k++;
191 count--;
192 }
193
194 if (k + count >= i - 2)
195 j = i - 2 - k;
196 else
197 j = count;
198
199 if (j >= 0) {
200 if (copy_to_user(buf + k - *ppos,
201 op->value + k - 1, j))
202 return -EFAULT;
203 count -= j;
204 k += j;
205 }
206
207 if (count) {
208 if (put_user('\'', &buf [k++ - *ppos]))
209 return -EFAULT;
210 }
211 if (count > 1) {
212 if (put_user('\n', &buf [k++ - *ppos]))
213 return -EFAULT;
214 }
215 } else if (op->flag & OPP_STRINGLIST) {
216 char *tmp;
217
218 tmp = kmalloc (i, GFP_KERNEL);
219 if (!tmp)
220 return -ENOMEM;
221
222 s = tmp;
223 *s++ = '\'';
224 for (p = op->value; p < op->value + op->len; p++) {
225 if (!*p) {
226 strcpy(s, "' + '");
227 s += 5;
228 continue;
229 }
230 *s++ = *p;
231 }
232 strcpy(s, "'\n");
233
234 if (copy_to_user(buf, tmp + k, count))
235 return -EFAULT;
236
237 kfree(tmp);
238 k += count;
239
240 } else if (op->flag & OPP_BINARY) {
241 char buffer[10];
242 u32 *first, *last;
243 int first_off, last_cnt;
244
245 first = ((u32 *)op->value) + k / 9;
246 first_off = k % 9;
247 last = ((u32 *)op->value) + (k + count - 1) / 9;
248 last_cnt = (k + count) % 9;
249 if (!last_cnt) last_cnt = 9;
250
251 if (first == last) {
252 sprintf (buffer, "%08x.", *first);
253 if (copy_to_user(buf, buffer + first_off,
254 last_cnt - first_off))
255 return -EFAULT;
256 buf += last_cnt - first_off;
257 } else {
258 for (q = first; q <= last; q++) {
259 sprintf (buffer, "%08x.", *q);
260 if (q == first) {
261 if (copy_to_user(buf, buffer + first_off,
262 9 - first_off))
263 return -EFAULT;
264 buf += 9 - first_off;
265 } else if (q == last) {
266 if (copy_to_user(buf, buffer, last_cnt))
267 return -EFAULT;
268 buf += last_cnt;
269 } else {
270 if (copy_to_user(buf, buffer, 9))
271 return -EFAULT;
272 buf += 9;
273 }
274 }
275 }
276
277 if (last == (u32 *)(op->value + op->len - 4) && last_cnt == 9) {
278 if (put_user('\n', (buf - 1)))
279 return -EFAULT;
280 }
281
282 k += count;
283
284 } else if (op->flag & OPP_HEXSTRING) {
285 char buffer[3];
286
287 if ((k < i - 1) && (k & 1)) {
288 sprintf (buffer, "%02x",
289 (unsigned char) *(op->value + (k >> 1)) & 0xff);
290 if (put_user(buffer[1], &buf[k++ - *ppos]))
291 return -EFAULT;
292 count--;
293 }
294
295 for (; (count > 1) && (k < i - 1); k += 2) {
296 sprintf (buffer, "%02x",
297 (unsigned char) *(op->value + (k >> 1)) & 0xff);
298 if (copy_to_user(buf + k - *ppos, buffer, 2))
299 return -EFAULT;
300 count -= 2;
301 }
302
303 if (count && (k < i - 1)) {
304 sprintf (buffer, "%02x",
305 (unsigned char) *(op->value + (k >> 1)) & 0xff);
306 if (put_user(buffer[0], &buf[k++ - *ppos]))
307 return -EFAULT;
308 count--;
309 }
310
311 if (count) {
312 if (put_user('\n', &buf [k++ - *ppos]))
313 return -EFAULT;
314 }
315 }
316 count = k - *ppos;
317 *ppos = k;
318 return count;
319}
320
321static ssize_t property_write(struct file *filp, const char __user *buf,
322 size_t count, loff_t *ppos)
323{
324 int i, j, k;
325 char *p;
326 u32 *q;
327 void *b;
328 openprom_property *op;
329
330 if (*ppos >= 0xffffff || count >= 0xffffff)
331 return -EINVAL;
332 if (!filp->private_data) {
333 i = property_read (filp, NULL, 0, NULL);
334 if (i)
335 return i;
336 }
337 k = *ppos;
Jan Engelhardt515decd2006-06-25 05:47:35 -0700338 op = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!(op->flag & OPP_STRING)) {
340 u32 *first, *last;
341 int first_off, last_cnt;
342 u32 mask, mask2;
343 char tmp [9];
344 int forcelen = 0;
345
346 j = k % 9;
347 for (i = 0; i < count; i++, j++) {
348 if (j == 9) j = 0;
349 if (!j) {
350 char ctmp;
351 if (get_user(ctmp, &buf[i]))
352 return -EFAULT;
353 if (ctmp != '.') {
354 if (ctmp != '\n') {
355 if (op->flag & OPP_BINARY)
356 return -EINVAL;
357 else
358 goto write_try_string;
359 } else {
360 count = i + 1;
361 forcelen = 1;
362 break;
363 }
364 }
365 } else {
366 char ctmp;
367 if (get_user(ctmp, &buf[i]))
368 return -EFAULT;
369 if (ctmp < '0' ||
370 (ctmp > '9' && ctmp < 'A') ||
371 (ctmp > 'F' && ctmp < 'a') ||
372 ctmp > 'f') {
373 if (op->flag & OPP_BINARY)
374 return -EINVAL;
375 else
376 goto write_try_string;
377 }
378 }
379 }
380 op->flag |= OPP_BINARY;
381 tmp [8] = 0;
382 i = ((count + k + 8) / 9) << 2;
383 if (op->alloclen <= i) {
384 b = kmalloc (sizeof (openprom_property) + 2 * i,
385 GFP_KERNEL);
386 if (!b)
387 return -ENOMEM;
388 memcpy (b, filp->private_data,
389 sizeof (openprom_property)
390 + strlen (op->name) + op->alloclen);
Jan Engelhardt515decd2006-06-25 05:47:35 -0700391 memset (b + sizeof (openprom_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 + strlen (op->name) + op->alloclen,
393 0, 2 * i - op->alloclen);
Jan Engelhardt515decd2006-06-25 05:47:35 -0700394 op = b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 op->alloclen = 2*i;
396 b = filp->private_data;
Jan Engelhardt515decd2006-06-25 05:47:35 -0700397 filp->private_data = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 kfree (b);
399 }
400 first = ((u32 *)op->value) + (k / 9);
401 first_off = k % 9;
402 last = (u32 *)(op->value + i);
403 last_cnt = (k + count) % 9;
404 if (first + 1 == last) {
405 memset (tmp, '0', 8);
406 if (copy_from_user(tmp + first_off, buf,
407 (count + first_off > 8) ?
408 8 - first_off : count))
409 return -EFAULT;
410 mask = 0xffffffff;
411 mask2 = 0xffffffff;
412 for (j = 0; j < first_off; j++)
413 mask >>= 1;
414 for (j = 8 - count - first_off; j > 0; j--)
415 mask2 <<= 1;
416 mask &= mask2;
417 if (mask) {
418 *first &= ~mask;
419 *first |= simple_strtoul (tmp, NULL, 16);
420 op->flag |= OPP_DIRTY;
421 }
422 } else {
423 op->flag |= OPP_DIRTY;
424 for (q = first; q < last; q++) {
425 if (q == first) {
426 if (first_off < 8) {
427 memset (tmp, '0', 8);
428 if (copy_from_user(tmp + first_off,
429 buf,
430 8 - first_off))
431 return -EFAULT;
432 mask = 0xffffffff;
433 for (j = 0; j < first_off; j++)
434 mask >>= 1;
435 *q &= ~mask;
436 *q |= simple_strtoul (tmp,NULL,16);
437 }
438 buf += 9;
439 } else if ((q == last - 1) && last_cnt
440 && (last_cnt < 8)) {
441 memset (tmp, '0', 8);
442 if (copy_from_user(tmp, buf, last_cnt))
443 return -EFAULT;
444 mask = 0xffffffff;
445 for (j = 0; j < 8 - last_cnt; j++)
446 mask <<= 1;
447 *q &= ~mask;
448 *q |= simple_strtoul (tmp, NULL, 16);
449 buf += last_cnt;
450 } else {
Jan Engelhardt0928d682006-06-25 05:47:35 -0700451 char tchars[2 * sizeof(long) + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Jan Engelhardt0928d682006-06-25 05:47:35 -0700453 if (copy_from_user(tchars, buf, sizeof(tchars) - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -EFAULT;
Jan Engelhardt0928d682006-06-25 05:47:35 -0700455 tchars[sizeof(tchars) - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *q = simple_strtoul (tchars, NULL, 16);
457 buf += 9;
458 }
459 }
460 }
461 if (!forcelen) {
462 if (op->len < i)
463 op->len = i;
464 } else
465 op->len = i;
466 *ppos += count;
467 }
468write_try_string:
469 if (!(op->flag & OPP_BINARY)) {
470 if (!(op->flag & (OPP_QUOTED | OPP_NOTQUOTED))) {
471 char ctmp;
472
473 /* No way, if somebody starts writing from the middle,
474 * we don't know whether he uses quotes around or not
475 */
476 if (k > 0)
477 return -EINVAL;
478 if (get_user(ctmp, buf))
479 return -EFAULT;
480 if (ctmp == '\'') {
481 op->flag |= OPP_QUOTED;
482 buf++;
483 count--;
484 (*ppos)++;
485 if (!count) {
486 op->flag |= OPP_STRING;
487 return 1;
488 }
489 } else
490 op->flag |= OPP_NOTQUOTED;
491 }
492 op->flag |= OPP_STRING;
493 if (op->alloclen <= count + *ppos) {
494 b = kmalloc (sizeof (openprom_property)
495 + 2 * (count + *ppos), GFP_KERNEL);
496 if (!b)
497 return -ENOMEM;
498 memcpy (b, filp->private_data,
499 sizeof (openprom_property)
500 + strlen (op->name) + op->alloclen);
Jan Engelhardt515decd2006-06-25 05:47:35 -0700501 memset (b + sizeof (openprom_property)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 + strlen (op->name) + op->alloclen,
503 0, 2*(count - *ppos) - op->alloclen);
Jan Engelhardt515decd2006-06-25 05:47:35 -0700504 op = b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 op->alloclen = 2*(count + *ppos);
506 b = filp->private_data;
Jan Engelhardt515decd2006-06-25 05:47:35 -0700507 filp->private_data = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 kfree (b);
509 }
510 p = op->value + *ppos - ((op->flag & OPP_QUOTED) ? 1 : 0);
511 if (copy_from_user(p, buf, count))
512 return -EFAULT;
513 op->flag |= OPP_DIRTY;
514 for (i = 0; i < count; i++, p++)
515 if (*p == '\n') {
516 *p = 0;
517 break;
518 }
519 if (i < count) {
520 op->len = p - op->value;
521 *ppos += i + 1;
522 if ((p > op->value) && (op->flag & OPP_QUOTED)
523 && (*(p - 1) == '\''))
524 op->len--;
525 } else {
526 if (p - op->value > op->len)
527 op->len = p - op->value;
528 *ppos += count;
529 }
530 }
531 return *ppos - k;
532}
533
534int property_release (struct inode *inode, struct file *filp)
535{
Jan Engelhardt515decd2006-06-25 05:47:35 -0700536 openprom_property *op = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int error;
538 u32 node;
539
540 if (!op)
541 return 0;
542 lock_kernel();
543 node = nodes[(u16)((long)inode->u.generic_ip)].node;
544 if ((u16)((long)inode->u.generic_ip) == aliases) {
545 if ((op->flag & OPP_DIRTY) && (op->flag & OPP_STRING)) {
546 char *p = op->name;
547 int i = (op->value - op->name) - strlen (op->name) - 1;
548 op->value [op->len] = 0;
549 *(op->value - 1) = ' ';
550 if (i) {
551 for (p = op->value - i - 2; p >= op->name; p--)
552 p[i] = *p;
553 p = op->name + i;
554 }
555 memcpy (p - 8, "nvalias ", 8);
556 prom_feval (p - 8);
557 }
558 } else if (op->flag & OPP_DIRTY) {
559 if (op->flag & OPP_STRING) {
560 op->value [op->len] = 0;
561 error = prom_setprop (node, op->name,
562 op->value, op->len + 1);
563 if (error <= 0)
564 printk (KERN_WARNING "openpromfs: "
565 "Couldn't write property %s\n",
566 op->name);
567 } else if ((op->flag & OPP_BINARY) || !op->len) {
568 error = prom_setprop (node, op->name,
569 op->value, op->len);
570 if (error <= 0)
571 printk (KERN_WARNING "openpromfs: "
572 "Couldn't write property %s\n",
573 op->name);
574 } else {
575 printk (KERN_WARNING "openpromfs: "
576 "Unknown property type of %s\n",
577 op->name);
578 }
579 }
580 unlock_kernel();
581 kfree (filp->private_data);
582 return 0;
583}
584
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800585static const struct file_operations openpromfs_prop_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 .read = property_read,
587 .write = property_write,
588 .release = property_release,
589};
590
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800591static const struct file_operations openpromfs_nodenum_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 .read = nodenum_read,
593};
594
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800595static const struct file_operations openprom_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 .read = generic_read_dir,
597 .readdir = openpromfs_readdir,
598};
599
600static struct inode_operations openprom_alias_inode_operations = {
601 .create = openpromfs_create,
602 .lookup = openpromfs_lookup,
603 .unlink = openpromfs_unlink,
604};
605
606static struct inode_operations openprom_inode_operations = {
607 .lookup = openpromfs_lookup,
608};
609
610static int lookup_children(u16 n, const char * name, int len)
611{
612 int ret;
613 u16 node;
614 for (; n != 0xffff; n = nodes[n].next) {
615 node = nodes[n].child;
616 if (node != 0xffff) {
617 char buffer[128];
618 int i;
619 char *p;
620
621 while (node != 0xffff) {
622 if (prom_getname (nodes[node].node,
623 buffer, 128) >= 0) {
624 i = strlen (buffer);
625 if ((len == i)
626 && !strncmp (buffer, name, len))
627 return NODE2INO(node);
628 p = strchr (buffer, '@');
629 if (p && (len == p - buffer)
630 && !strncmp (buffer, name, len))
631 return NODE2INO(node);
632 }
633 node = nodes[node].next;
634 }
635 } else
636 continue;
637 ret = lookup_children (nodes[n].child, name, len);
638 if (ret) return ret;
639 }
640 return 0;
641}
642
643static struct dentry *openpromfs_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
644{
645 int ino = 0;
646#define OPFSL_DIR 0
647#define OPFSL_PROPERTY 1
648#define OPFSL_NODENUM 2
649 int type = 0;
650 char buffer[128];
651 char *p;
652 const char *name;
653 u32 n;
654 u16 dirnode;
655 unsigned int len;
656 int i;
657 struct inode *inode;
658 char buffer2[64];
659
660 inode = NULL;
661 name = dentry->d_name.name;
662 len = dentry->d_name.len;
663 lock_kernel();
664 if (name [0] == '.' && len == 5 && !strncmp (name + 1, "node", 4)) {
665 ino = NODEP2INO(NODE(dir->i_ino).first_prop);
666 type = OPFSL_NODENUM;
667 }
668 if (!ino) {
669 u16 node = NODE(dir->i_ino).child;
670 while (node != 0xffff) {
671 if (prom_getname (nodes[node].node, buffer, 128) >= 0) {
672 i = strlen (buffer);
673 if (len == i && !strncmp (buffer, name, len)) {
674 ino = NODE2INO(node);
675 type = OPFSL_DIR;
676 break;
677 }
678 p = strchr (buffer, '@');
679 if (p && (len == p - buffer)
680 && !strncmp (buffer, name, len)) {
681 ino = NODE2INO(node);
682 type = OPFSL_DIR;
683 break;
684 }
685 }
686 node = nodes[node].next;
687 }
688 }
689 n = NODE(dir->i_ino).node;
690 dirnode = dir->i_ino - OPENPROM_FIRST_INO;
691 if (!ino) {
692 int j = NODEP2INO(NODE(dir->i_ino).first_prop);
693 if (dirnode != aliases) {
694 for (p = prom_firstprop (n, buffer2);
695 p && *p;
696 p = prom_nextprop (n, p, buffer2)) {
697 j++;
698 if ((len == strlen (p))
699 && !strncmp (p, name, len)) {
700 ino = j;
701 type = OPFSL_PROPERTY;
702 break;
703 }
704 }
705 } else {
706 int k;
707 for (k = 0; k < aliases_nodes; k++) {
708 j++;
709 if (alias_names [k]
710 && (len == strlen (alias_names [k]))
711 && !strncmp (alias_names [k], name, len)) {
712 ino = j;
713 type = OPFSL_PROPERTY;
714 break;
715 }
716 }
717 }
718 }
719 if (!ino) {
720 ino = lookup_children (NODE(dir->i_ino).child, name, len);
721 if (ino)
722 type = OPFSL_DIR;
723 else {
724 unlock_kernel();
725 return ERR_PTR(-ENOENT);
726 }
727 }
728 inode = iget (dir->i_sb, ino);
729 unlock_kernel();
730 if (!inode)
731 return ERR_PTR(-EINVAL);
732 switch (type) {
733 case OPFSL_DIR:
734 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
735 if (ino == OPENPROM_FIRST_INO + aliases) {
736 inode->i_mode |= S_IWUSR;
737 inode->i_op = &openprom_alias_inode_operations;
738 } else
739 inode->i_op = &openprom_inode_operations;
740 inode->i_fop = &openprom_operations;
741 inode->i_nlink = 2;
742 break;
743 case OPFSL_NODENUM:
744 inode->i_mode = S_IFREG | S_IRUGO;
745 inode->i_fop = &openpromfs_nodenum_ops;
746 inode->i_nlink = 1;
747 inode->u.generic_ip = (void *)(long)(n);
748 break;
749 case OPFSL_PROPERTY:
750 if ((dirnode == options) && (len == 17)
751 && !strncmp (name, "security-password", 17))
752 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
753 else {
754 inode->i_mode = S_IFREG | S_IRUGO;
755 if (dirnode == options || dirnode == aliases) {
756 if (len != 4 || strncmp (name, "name", 4))
757 inode->i_mode |= S_IWUSR;
758 }
759 }
760 inode->i_fop = &openpromfs_prop_ops;
761 inode->i_nlink = 1;
762 if (inode->i_size < 0)
763 inode->i_size = 0;
764 inode->u.generic_ip = (void *)(long)(((u16)dirnode) |
765 (((u16)(ino - NODEP2INO(NODE(dir->i_ino).first_prop) - 1)) << 16));
766 break;
767 }
768
769 inode->i_gid = 0;
770 inode->i_uid = 0;
771
772 d_add(dentry, inode);
773 return NULL;
774}
775
776static int openpromfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
777{
778 struct inode *inode = filp->f_dentry->d_inode;
779 unsigned int ino;
780 u32 n;
781 int i, j;
782 char buffer[128];
783 u16 node;
784 char *p;
785 char buffer2[64];
786
787 lock_kernel();
788
789 ino = inode->i_ino;
790 i = filp->f_pos;
791 switch (i) {
792 case 0:
793 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) goto out;
794 i++;
795 filp->f_pos++;
796 /* fall thru */
797 case 1:
798 if (filldir(dirent, "..", 2, i,
799 (NODE(ino).parent == 0xffff) ?
800 OPENPROM_ROOT_INO : NODE2INO(NODE(ino).parent), DT_DIR) < 0)
801 goto out;
802 i++;
803 filp->f_pos++;
804 /* fall thru */
805 default:
806 i -= 2;
807 node = NODE(ino).child;
808 while (i && node != 0xffff) {
809 node = nodes[node].next;
810 i--;
811 }
812 while (node != 0xffff) {
813 if (prom_getname (nodes[node].node, buffer, 128) < 0)
814 goto out;
815 if (filldir(dirent, buffer, strlen(buffer),
816 filp->f_pos, NODE2INO(node), DT_DIR) < 0)
817 goto out;
818 filp->f_pos++;
819 node = nodes[node].next;
820 }
821 j = NODEP2INO(NODE(ino).first_prop);
822 if (!i) {
823 if (filldir(dirent, ".node", 5, filp->f_pos, j, DT_REG) < 0)
824 goto out;
825 filp->f_pos++;
826 } else
827 i--;
828 n = NODE(ino).node;
829 if (ino == OPENPROM_FIRST_INO + aliases) {
830 for (j++; i < aliases_nodes; i++, j++) {
831 if (alias_names [i]) {
832 if (filldir (dirent, alias_names [i],
833 strlen (alias_names [i]),
834 filp->f_pos, j, DT_REG) < 0) goto out;
835 filp->f_pos++;
836 }
837 }
838 } else {
839 for (p = prom_firstprop (n, buffer2);
840 p && *p;
841 p = prom_nextprop (n, p, buffer2)) {
842 j++;
843 if (i) i--;
844 else {
845 if (filldir(dirent, p, strlen(p),
846 filp->f_pos, j, DT_REG) < 0)
847 goto out;
848 filp->f_pos++;
849 }
850 }
851 }
852 }
853out:
854 unlock_kernel();
855 return 0;
856}
857
858static int openpromfs_create (struct inode *dir, struct dentry *dentry, int mode,
859 struct nameidata *nd)
860{
861 char *p;
862 struct inode *inode;
863
864 if (!dir)
865 return -ENOENT;
866 if (dentry->d_name.len > 256)
867 return -EINVAL;
868 p = kmalloc (dentry->d_name.len + 1, GFP_KERNEL);
869 if (!p)
870 return -ENOMEM;
871 strncpy (p, dentry->d_name.name, dentry->d_name.len);
872 p [dentry->d_name.len] = 0;
873 lock_kernel();
874 if (aliases_nodes == ALIASES_NNODES) {
875 kfree(p);
876 unlock_kernel();
877 return -EIO;
878 }
879 alias_names [aliases_nodes++] = p;
880 inode = iget (dir->i_sb,
881 NODEP2INO(NODE(dir->i_ino).first_prop) + aliases_nodes);
882 if (!inode) {
883 unlock_kernel();
884 return -EINVAL;
885 }
886 inode->i_mode = S_IFREG | S_IRUGO | S_IWUSR;
887 inode->i_fop = &openpromfs_prop_ops;
888 inode->i_nlink = 1;
889 if (inode->i_size < 0) inode->i_size = 0;
890 inode->u.generic_ip = (void *)(long)(((u16)aliases) |
891 (((u16)(aliases_nodes - 1)) << 16));
892 unlock_kernel();
893 d_instantiate(dentry, inode);
894 return 0;
895}
896
897static int openpromfs_unlink (struct inode *dir, struct dentry *dentry)
898{
899 unsigned int len;
900 char *p;
901 const char *name;
902 int i;
903
904 name = dentry->d_name.name;
905 len = dentry->d_name.len;
906 lock_kernel();
907 for (i = 0; i < aliases_nodes; i++)
908 if ((strlen (alias_names [i]) == len)
909 && !strncmp (name, alias_names[i], len)) {
910 char buffer[512];
911
912 p = alias_names [i];
913 alias_names [i] = NULL;
914 kfree (p);
915 strcpy (buffer, "nvunalias ");
916 memcpy (buffer + 10, name, len);
917 buffer [10 + len] = 0;
918 prom_feval (buffer);
919 }
920 unlock_kernel();
921 return 0;
922}
923
924/* {{{ init section */
925static int __init check_space (u16 n)
926{
927 unsigned long pages;
928
929 if ((1 << alloced) * PAGE_SIZE < (n + 2) * sizeof(openpromfs_node)) {
930 pages = __get_free_pages (GFP_KERNEL, alloced + 1);
931 if (!pages)
932 return -1;
933
934 if (nodes) {
Jan Engelhardt515decd2006-06-25 05:47:35 -0700935 memcpy ((char *)pages, nodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 (1 << alloced) * PAGE_SIZE);
937 free_pages ((unsigned long)nodes, alloced);
938 }
939 alloced++;
940 nodes = (openpromfs_node *)pages;
941 }
942 return 0;
943}
944
945static u16 __init get_nodes (u16 parent, u32 node)
946{
947 char *p;
948 u16 n = last_node++, i;
949 char buffer[64];
950
951 if (check_space (n) < 0)
952 return 0xffff;
953 nodes[n].parent = parent;
954 nodes[n].node = node;
955 nodes[n].next = 0xffff;
956 nodes[n].child = 0xffff;
957 nodes[n].first_prop = first_prop++;
958 if (!parent) {
959 char buffer[8];
960 int j;
961
962 if ((j = prom_getproperty (node, "name", buffer, 8)) >= 0) {
963 buffer[j] = 0;
964 if (!strcmp (buffer, "options"))
965 options = n;
966 else if (!strcmp (buffer, "aliases"))
967 aliases = n;
968 }
969 }
970 if (n != aliases)
971 for (p = prom_firstprop (node, buffer);
972 p && p != (char *)-1 && *p;
973 p = prom_nextprop (node, p, buffer))
974 first_prop++;
975 else {
976 char *q;
977 for (p = prom_firstprop (node, buffer);
978 p && p != (char *)-1 && *p;
979 p = prom_nextprop (node, p, buffer)) {
980 if (aliases_nodes == ALIASES_NNODES)
981 break;
982 for (i = 0; i < aliases_nodes; i++)
983 if (!strcmp (p, alias_names [i]))
984 break;
985 if (i < aliases_nodes)
986 continue;
987 q = kmalloc (strlen (p) + 1, GFP_KERNEL);
988 if (!q)
989 return 0xffff;
990 strcpy (q, p);
991 alias_names [aliases_nodes++] = q;
992 }
993 first_prop += ALIASES_NNODES;
994 }
995 node = prom_getchild (node);
996 if (node) {
997 parent = get_nodes (n, node);
998 if (parent == 0xffff)
999 return 0xffff;
1000 nodes[n].child = parent;
1001 while ((node = prom_getsibling (node)) != 0) {
1002 i = get_nodes (n, node);
1003 if (i == 0xffff)
1004 return 0xffff;
1005 nodes[parent].next = i;
1006 parent = i;
1007 }
1008 }
1009 return n;
1010}
1011
1012static void openprom_read_inode(struct inode * inode)
1013{
1014 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1015 if (inode->i_ino == OPENPROM_ROOT_INO) {
1016 inode->i_op = &openprom_inode_operations;
1017 inode->i_fop = &openprom_operations;
1018 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1019 }
1020}
1021
1022static int openprom_remount(struct super_block *sb, int *flags, char *data)
1023{
1024 *flags |= MS_NOATIME;
1025 return 0;
1026}
1027
1028static struct super_operations openprom_sops = {
1029 .read_inode = openprom_read_inode,
1030 .statfs = simple_statfs,
1031 .remount_fs = openprom_remount,
1032};
1033
1034static int openprom_fill_super(struct super_block *s, void *data, int silent)
1035{
1036 struct inode * root_inode;
1037
1038 s->s_flags |= MS_NOATIME;
1039 s->s_blocksize = 1024;
1040 s->s_blocksize_bits = 10;
1041 s->s_magic = OPENPROM_SUPER_MAGIC;
1042 s->s_op = &openprom_sops;
1043 s->s_time_gran = 1;
1044 root_inode = iget(s, OPENPROM_ROOT_INO);
1045 if (!root_inode)
1046 goto out_no_root;
1047 s->s_root = d_alloc_root(root_inode);
1048 if (!s->s_root)
1049 goto out_no_root;
1050 return 0;
1051
1052out_no_root:
1053 printk("openprom_fill_super: get root inode failed\n");
1054 iput(root_inode);
1055 return -ENOMEM;
1056}
1057
David Howells454e2392006-06-23 02:02:57 -07001058static int openprom_get_sb(struct file_system_type *fs_type,
1059 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
David Howells454e2392006-06-23 02:02:57 -07001061 return get_sb_single(fs_type, flags, data, openprom_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064static struct file_system_type openprom_fs_type = {
1065 .owner = THIS_MODULE,
1066 .name = "openpromfs",
1067 .get_sb = openprom_get_sb,
1068 .kill_sb = kill_anon_super,
1069};
1070
1071static int __init init_openprom_fs(void)
1072{
1073 nodes = (openpromfs_node *)__get_free_pages(GFP_KERNEL, 0);
1074 if (!nodes) {
1075 printk (KERN_WARNING "openpromfs: can't get free page\n");
1076 return -EIO;
1077 }
1078 if (get_nodes (0xffff, prom_root_node) == 0xffff) {
1079 printk (KERN_WARNING "openpromfs: couldn't setup tree\n");
1080 return -EIO;
1081 }
1082 nodes[last_node].first_prop = first_prop;
1083 return register_filesystem(&openprom_fs_type);
1084}
1085
1086static void __exit exit_openprom_fs(void)
1087{
1088 int i;
1089 unregister_filesystem(&openprom_fs_type);
1090 free_pages ((unsigned long)nodes, alloced);
1091 for (i = 0; i < aliases_nodes; i++)
Jesper Juhlf99d49a2005-11-07 01:01:34 -08001092 kfree (alias_names [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nodes = NULL;
1094}
1095
1096module_init(init_openprom_fs)
1097module_exit(exit_openprom_fs)
1098MODULE_LICENSE("GPL");