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