Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
3d824a46 | 2 | /* inode.c: /proc/openprom handling routines |
1da177e4 LT |
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> | |
7ab2fa76 | 12 | #include <linux/fs_context.h> |
1da177e4 LT |
13 | #include <linux/init.h> |
14 | #include <linux/slab.h> | |
3d824a46 | 15 | #include <linux/seq_file.h> |
e18fa700 | 16 | #include <linux/magic.h> |
1da177e4 LT |
17 | |
18 | #include <asm/openprom.h> | |
19 | #include <asm/oplib.h> | |
3d824a46 | 20 | #include <asm/prom.h> |
7c0f6ba6 | 21 | #include <linux/uaccess.h> |
1da177e4 | 22 | |
3d824a46 | 23 | static DEFINE_MUTEX(op_mutex); |
1da177e4 | 24 | |
3d824a46 | 25 | #define OPENPROM_ROOT_INO 0 |
a04ee146 | 26 | |
3d824a46 DM |
27 | enum op_inode_type { |
28 | op_inode_node, | |
29 | op_inode_prop, | |
30 | }; | |
31 | ||
32 | union op_inode_data { | |
33 | struct device_node *node; | |
34 | struct property *prop; | |
35 | }; | |
36 | ||
37 | struct op_inode_info { | |
38 | struct inode vfs_inode; | |
39 | enum op_inode_type type; | |
40 | union op_inode_data u; | |
41 | }; | |
42 | ||
b88a27ed DH |
43 | static struct inode *openprom_iget(struct super_block *sb, ino_t ino); |
44 | ||
3d824a46 | 45 | static inline struct op_inode_info *OP_I(struct inode *inode) |
1da177e4 | 46 | { |
3d824a46 | 47 | return container_of(inode, struct op_inode_info, vfs_inode); |
1da177e4 LT |
48 | } |
49 | ||
3d824a46 | 50 | static int is_string(unsigned char *p, int len) |
1da177e4 | 51 | { |
3d824a46 | 52 | int i; |
1da177e4 | 53 | |
3d824a46 DM |
54 | for (i = 0; i < len; i++) { |
55 | unsigned char val = p[i]; | |
1da177e4 | 56 | |
3d824a46 DM |
57 | if ((i && !val) || |
58 | (val >= ' ' && val <= '~')) | |
59 | continue; | |
1da177e4 | 60 | |
3d824a46 DM |
61 | return 0; |
62 | } | |
1da177e4 | 63 | |
3d824a46 DM |
64 | return 1; |
65 | } | |
1da177e4 | 66 | |
3d824a46 DM |
67 | static int property_show(struct seq_file *f, void *v) |
68 | { | |
69 | struct property *prop = f->private; | |
70 | void *pval; | |
71 | int len; | |
1da177e4 | 72 | |
3d824a46 DM |
73 | len = prop->length; |
74 | pval = prop->value; | |
1da177e4 | 75 | |
3d824a46 DM |
76 | if (is_string(pval, len)) { |
77 | while (len > 0) { | |
78 | int n = strlen(pval); | |
1da177e4 | 79 | |
3d824a46 | 80 | seq_printf(f, "%s", (char *) pval); |
1da177e4 | 81 | |
3d824a46 DM |
82 | /* Skip over the NULL byte too. */ |
83 | pval += n + 1; | |
84 | len -= n + 1; | |
1da177e4 | 85 | |
3d824a46 DM |
86 | if (len > 0) |
87 | seq_printf(f, " + "); | |
1da177e4 | 88 | } |
3d824a46 DM |
89 | } else { |
90 | if (len & 3) { | |
91 | while (len) { | |
92 | len--; | |
93 | if (len) | |
94 | seq_printf(f, "%02x.", | |
95 | *(unsigned char *) pval); | |
96 | else | |
97 | seq_printf(f, "%02x", | |
98 | *(unsigned char *) pval); | |
99 | pval++; | |
1da177e4 LT |
100 | } |
101 | } else { | |
3d824a46 DM |
102 | while (len >= 4) { |
103 | len -= 4; | |
104 | ||
105 | if (len) | |
106 | seq_printf(f, "%08x.", | |
107 | *(unsigned int *) pval); | |
108 | else | |
109 | seq_printf(f, "%08x", | |
110 | *(unsigned int *) pval); | |
111 | pval += 4; | |
1da177e4 | 112 | } |
1da177e4 LT |
113 | } |
114 | } | |
3d824a46 DM |
115 | seq_printf(f, "\n"); |
116 | ||
117 | return 0; | |
1da177e4 LT |
118 | } |
119 | ||
3d824a46 | 120 | static void *property_start(struct seq_file *f, loff_t *pos) |
1da177e4 | 121 | { |
3d824a46 DM |
122 | if (*pos == 0) |
123 | return pos; | |
124 | return NULL; | |
125 | } | |
126 | ||
127 | static void *property_next(struct seq_file *f, void *v, loff_t *pos) | |
128 | { | |
129 | (*pos)++; | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | static void property_stop(struct seq_file *f, void *v) | |
134 | { | |
135 | /* Nothing to do */ | |
136 | } | |
137 | ||
872e2be7 | 138 | static const struct seq_operations property_op = { |
3d824a46 DM |
139 | .start = property_start, |
140 | .next = property_next, | |
141 | .stop = property_stop, | |
142 | .show = property_show | |
143 | }; | |
144 | ||
145 | static int property_open(struct inode *inode, struct file *file) | |
146 | { | |
147 | struct op_inode_info *oi = OP_I(inode); | |
148 | int ret; | |
149 | ||
150 | BUG_ON(oi->type != op_inode_prop); | |
151 | ||
152 | ret = seq_open(file, &property_op); | |
153 | if (!ret) { | |
154 | struct seq_file *m = file->private_data; | |
155 | m->private = oi->u.prop; | |
1da177e4 | 156 | } |
3d824a46 | 157 | return ret; |
1da177e4 LT |
158 | } |
159 | ||
4b6f5d20 | 160 | static const struct file_operations openpromfs_prop_ops = { |
3d824a46 DM |
161 | .open = property_open, |
162 | .read = seq_read, | |
163 | .llseek = seq_lseek, | |
164 | .release = seq_release, | |
1da177e4 LT |
165 | }; |
166 | ||
68c61471 | 167 | static int openpromfs_readdir(struct file *, struct dir_context *); |
1da177e4 | 168 | |
4b6f5d20 | 169 | static const struct file_operations openprom_operations = { |
1da177e4 | 170 | .read = generic_read_dir, |
c51da20c | 171 | .iterate_shared = openpromfs_readdir, |
3222a3e5 | 172 | .llseek = generic_file_llseek, |
1da177e4 LT |
173 | }; |
174 | ||
00cd8dd3 | 175 | static struct dentry *openpromfs_lookup(struct inode *, struct dentry *, unsigned int); |
1da177e4 | 176 | |
92e1d5be | 177 | static const struct inode_operations openprom_inode_operations = { |
1da177e4 LT |
178 | .lookup = openpromfs_lookup, |
179 | }; | |
180 | ||
00cd8dd3 | 181 | static struct dentry *openpromfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
1da177e4 | 182 | { |
3d824a46 DM |
183 | struct op_inode_info *ent_oi, *oi = OP_I(dir); |
184 | struct device_node *dp, *child; | |
185 | struct property *prop; | |
186 | enum op_inode_type ent_type; | |
187 | union op_inode_data ent_data; | |
1da177e4 | 188 | const char *name; |
1da177e4 | 189 | struct inode *inode; |
3d824a46 DM |
190 | unsigned int ino; |
191 | int len; | |
1da177e4 | 192 | |
3d824a46 DM |
193 | BUG_ON(oi->type != op_inode_node); |
194 | ||
195 | dp = oi->u.node; | |
196 | ||
1da177e4 LT |
197 | name = dentry->d_name.name; |
198 | len = dentry->d_name.len; | |
3d824a46 DM |
199 | |
200 | mutex_lock(&op_mutex); | |
201 | ||
202 | child = dp->child; | |
203 | while (child) { | |
105e996a RH |
204 | const char *node_name = kbasename(child->full_name); |
205 | int n = strlen(node_name); | |
3d824a46 DM |
206 | |
207 | if (len == n && | |
105e996a | 208 | !strncmp(node_name, name, len)) { |
3d824a46 DM |
209 | ent_type = op_inode_node; |
210 | ent_data.node = child; | |
211 | ino = child->unique_id; | |
212 | goto found; | |
1da177e4 | 213 | } |
3d824a46 | 214 | child = child->sibling; |
1da177e4 | 215 | } |
3d824a46 DM |
216 | |
217 | prop = dp->properties; | |
218 | while (prop) { | |
219 | int n = strlen(prop->name); | |
220 | ||
221 | if (len == n && !strncmp(prop->name, name, len)) { | |
222 | ent_type = op_inode_prop; | |
223 | ent_data.prop = prop; | |
224 | ino = prop->unique_id; | |
225 | goto found; | |
1da177e4 | 226 | } |
3d824a46 DM |
227 | |
228 | prop = prop->next; | |
1da177e4 | 229 | } |
3d824a46 DM |
230 | |
231 | mutex_unlock(&op_mutex); | |
232 | return ERR_PTR(-ENOENT); | |
233 | ||
234 | found: | |
b88a27ed | 235 | inode = openprom_iget(dir->i_sb, ino); |
3d824a46 | 236 | mutex_unlock(&op_mutex); |
b88a27ed DH |
237 | if (IS_ERR(inode)) |
238 | return ERR_CAST(inode); | |
e34d657f | 239 | if (inode->i_state & I_NEW) { |
76daf9b1 | 240 | simple_inode_init_ts(inode); |
e34d657f AV |
241 | ent_oi = OP_I(inode); |
242 | ent_oi->type = ent_type; | |
243 | ent_oi->u = ent_data; | |
244 | ||
245 | switch (ent_type) { | |
246 | case op_inode_node: | |
247 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; | |
248 | inode->i_op = &openprom_inode_operations; | |
249 | inode->i_fop = &openprom_operations; | |
250 | set_nlink(inode, 2); | |
251 | break; | |
252 | case op_inode_prop: | |
253 | if (of_node_name_eq(dp, "options") && (len == 17) && | |
254 | !strncmp (name, "security-password", 17)) | |
255 | inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR; | |
256 | else | |
257 | inode->i_mode = S_IFREG | S_IRUGO; | |
258 | inode->i_fop = &openpromfs_prop_ops; | |
259 | set_nlink(inode, 1); | |
260 | inode->i_size = ent_oi->u.prop->length; | |
261 | break; | |
262 | } | |
263 | unlock_new_inode(inode); | |
1da177e4 LT |
264 | } |
265 | ||
0ed883fd | 266 | return d_splice_alias(inode, dentry); |
1da177e4 LT |
267 | } |
268 | ||
68c61471 | 269 | static int openpromfs_readdir(struct file *file, struct dir_context *ctx) |
1da177e4 | 270 | { |
68c61471 | 271 | struct inode *inode = file_inode(file); |
3d824a46 DM |
272 | struct op_inode_info *oi = OP_I(inode); |
273 | struct device_node *dp = oi->u.node; | |
274 | struct device_node *child; | |
275 | struct property *prop; | |
3d824a46 DM |
276 | int i; |
277 | ||
278 | mutex_lock(&op_mutex); | |
1da177e4 | 279 | |
68c61471 AV |
280 | if (ctx->pos == 0) { |
281 | if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR)) | |
3d824a46 | 282 | goto out; |
68c61471 AV |
283 | ctx->pos = 1; |
284 | } | |
285 | if (ctx->pos == 1) { | |
286 | if (!dir_emit(ctx, "..", 2, | |
3d824a46 DM |
287 | (dp->parent == NULL ? |
288 | OPENPROM_ROOT_INO : | |
68c61471 | 289 | dp->parent->unique_id), DT_DIR)) |
1da177e4 | 290 | goto out; |
68c61471 AV |
291 | ctx->pos = 2; |
292 | } | |
293 | i = ctx->pos - 2; | |
3d824a46 | 294 | |
68c61471 AV |
295 | /* First, the children nodes as directories. */ |
296 | child = dp->child; | |
297 | while (i && child) { | |
298 | child = child->sibling; | |
299 | i--; | |
300 | } | |
301 | while (child) { | |
302 | if (!dir_emit(ctx, | |
105e996a RH |
303 | kbasename(child->full_name), |
304 | strlen(kbasename(child->full_name)), | |
68c61471 AV |
305 | child->unique_id, DT_DIR)) |
306 | goto out; | |
3d824a46 | 307 | |
68c61471 AV |
308 | ctx->pos++; |
309 | child = child->sibling; | |
310 | } | |
311 | ||
312 | /* Next, the properties as files. */ | |
313 | prop = dp->properties; | |
314 | while (i && prop) { | |
315 | prop = prop->next; | |
316 | i--; | |
1da177e4 | 317 | } |
68c61471 AV |
318 | while (prop) { |
319 | if (!dir_emit(ctx, prop->name, strlen(prop->name), | |
320 | prop->unique_id, DT_REG)) | |
321 | goto out; | |
322 | ||
323 | ctx->pos++; | |
324 | prop = prop->next; | |
325 | } | |
326 | ||
1da177e4 | 327 | out: |
3d824a46 | 328 | mutex_unlock(&op_mutex); |
1da177e4 LT |
329 | return 0; |
330 | } | |
331 | ||
e18b890b | 332 | static struct kmem_cache *op_inode_cachep; |
1da177e4 | 333 | |
3d824a46 | 334 | static struct inode *openprom_alloc_inode(struct super_block *sb) |
1da177e4 | 335 | { |
3d824a46 | 336 | struct op_inode_info *oi; |
1da177e4 | 337 | |
fd60b288 | 338 | oi = alloc_inode_sb(sb, op_inode_cachep, GFP_KERNEL); |
3d824a46 DM |
339 | if (!oi) |
340 | return NULL; | |
1da177e4 | 341 | |
3d824a46 | 342 | return &oi->vfs_inode; |
1da177e4 LT |
343 | } |
344 | ||
363db959 | 345 | static void openprom_free_inode(struct inode *inode) |
1da177e4 | 346 | { |
3d824a46 | 347 | kmem_cache_free(op_inode_cachep, OP_I(inode)); |
1da177e4 LT |
348 | } |
349 | ||
b88a27ed | 350 | static struct inode *openprom_iget(struct super_block *sb, ino_t ino) |
1da177e4 | 351 | { |
e34d657f | 352 | struct inode *inode = iget_locked(sb, ino); |
b88a27ed | 353 | if (!inode) |
e34d657f | 354 | inode = ERR_PTR(-ENOMEM); |
b88a27ed | 355 | return inode; |
1da177e4 LT |
356 | } |
357 | ||
358 | static int openprom_remount(struct super_block *sb, int *flags, char *data) | |
359 | { | |
02b9984d | 360 | sync_filesystem(sb); |
1751e8a6 | 361 | *flags |= SB_NOATIME; |
1da177e4 LT |
362 | return 0; |
363 | } | |
364 | ||
ee9b6d61 | 365 | static const struct super_operations openprom_sops = { |
3d824a46 | 366 | .alloc_inode = openprom_alloc_inode, |
363db959 | 367 | .free_inode = openprom_free_inode, |
1da177e4 LT |
368 | .statfs = simple_statfs, |
369 | .remount_fs = openprom_remount, | |
370 | }; | |
371 | ||
7ab2fa76 | 372 | static int openprom_fill_super(struct super_block *s, struct fs_context *fc) |
1da177e4 | 373 | { |
3d824a46 DM |
374 | struct inode *root_inode; |
375 | struct op_inode_info *oi; | |
b88a27ed | 376 | int ret; |
1da177e4 | 377 | |
1751e8a6 | 378 | s->s_flags |= SB_NOATIME; |
1da177e4 LT |
379 | s->s_blocksize = 1024; |
380 | s->s_blocksize_bits = 10; | |
381 | s->s_magic = OPENPROM_SUPER_MAGIC; | |
382 | s->s_op = &openprom_sops; | |
383 | s->s_time_gran = 1; | |
b88a27ed DH |
384 | root_inode = openprom_iget(s, OPENPROM_ROOT_INO); |
385 | if (IS_ERR(root_inode)) { | |
386 | ret = PTR_ERR(root_inode); | |
1da177e4 | 387 | goto out_no_root; |
b88a27ed | 388 | } |
3d824a46 | 389 | |
76daf9b1 | 390 | simple_inode_init_ts(root_inode); |
e34d657f AV |
391 | root_inode->i_op = &openprom_inode_operations; |
392 | root_inode->i_fop = &openprom_operations; | |
393 | root_inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; | |
3d824a46 DM |
394 | oi = OP_I(root_inode); |
395 | oi->type = op_inode_node; | |
396 | oi->u.node = of_find_node_by_path("/"); | |
e34d657f | 397 | unlock_new_inode(root_inode); |
3d824a46 | 398 | |
48fde701 | 399 | s->s_root = d_make_root(root_inode); |
1da177e4 | 400 | if (!s->s_root) |
b88a27ed | 401 | goto out_no_root_dentry; |
1da177e4 LT |
402 | return 0; |
403 | ||
b88a27ed | 404 | out_no_root_dentry: |
b88a27ed | 405 | ret = -ENOMEM; |
1da177e4 LT |
406 | out_no_root: |
407 | printk("openprom_fill_super: get root inode failed\n"); | |
b88a27ed | 408 | return ret; |
1da177e4 LT |
409 | } |
410 | ||
7ab2fa76 | 411 | static int openpromfs_get_tree(struct fs_context *fc) |
1da177e4 | 412 | { |
7ab2fa76 DH |
413 | return get_tree_single(fc, openprom_fill_super); |
414 | } | |
415 | ||
416 | static const struct fs_context_operations openpromfs_context_ops = { | |
417 | .get_tree = openpromfs_get_tree, | |
418 | }; | |
419 | ||
420 | static int openpromfs_init_fs_context(struct fs_context *fc) | |
421 | { | |
422 | fc->ops = &openpromfs_context_ops; | |
423 | return 0; | |
1da177e4 LT |
424 | } |
425 | ||
426 | static struct file_system_type openprom_fs_type = { | |
427 | .owner = THIS_MODULE, | |
428 | .name = "openpromfs", | |
7ab2fa76 | 429 | .init_fs_context = openpromfs_init_fs_context, |
1da177e4 LT |
430 | .kill_sb = kill_anon_super, |
431 | }; | |
7f78e035 | 432 | MODULE_ALIAS_FS("openpromfs"); |
1da177e4 | 433 | |
51cc5068 | 434 | static void op_inode_init_once(void *data) |
3d824a46 DM |
435 | { |
436 | struct op_inode_info *oi = (struct op_inode_info *) data; | |
437 | ||
a35afb83 | 438 | inode_init_once(&oi->vfs_inode); |
3d824a46 DM |
439 | } |
440 | ||
1da177e4 LT |
441 | static int __init init_openprom_fs(void) |
442 | { | |
3d824a46 DM |
443 | int err; |
444 | ||
445 | op_inode_cachep = kmem_cache_create("op_inode_cache", | |
446 | sizeof(struct op_inode_info), | |
447 | 0, | |
448 | (SLAB_RECLAIM_ACCOUNT | | |
45f29e0e | 449 | SLAB_ACCOUNT), |
20c2df83 | 450 | op_inode_init_once); |
3d824a46 DM |
451 | if (!op_inode_cachep) |
452 | return -ENOMEM; | |
453 | ||
454 | err = register_filesystem(&openprom_fs_type); | |
455 | if (err) | |
456 | kmem_cache_destroy(op_inode_cachep); | |
457 | ||
458 | return err; | |
1da177e4 LT |
459 | } |
460 | ||
461 | static void __exit exit_openprom_fs(void) | |
462 | { | |
1da177e4 | 463 | unregister_filesystem(&openprom_fs_type); |
8c0a8537 KS |
464 | /* |
465 | * Make sure all delayed rcu free inodes are flushed before we | |
466 | * destroy cache. | |
467 | */ | |
468 | rcu_barrier(); | |
3d824a46 | 469 | kmem_cache_destroy(op_inode_cachep); |
1da177e4 LT |
470 | } |
471 | ||
472 | module_init(init_openprom_fs) | |
473 | module_exit(exit_openprom_fs) | |
474 | MODULE_LICENSE("GPL"); |