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