1 // SPDX-License-Identifier: GPL-2.0-only
2 /* inode.c: /proc/openprom handling routines
4 * Copyright (C) 1996-1999 Jakub Jelinek (jakub@redhat.com)
5 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/seq_file.h>
15 #include <linux/magic.h>
17 #include <asm/openprom.h>
18 #include <asm/oplib.h>
20 #include <linux/uaccess.h>
22 static DEFINE_MUTEX(op_mutex);
24 #define OPENPROM_ROOT_INO 0
32 struct device_node *node;
33 struct property *prop;
36 struct op_inode_info {
37 struct inode vfs_inode;
38 enum op_inode_type type;
39 union op_inode_data u;
42 static struct inode *openprom_iget(struct super_block *sb, ino_t ino);
44 static inline struct op_inode_info *OP_I(struct inode *inode)
46 return container_of(inode, struct op_inode_info, vfs_inode);
49 static int is_string(unsigned char *p, int len)
53 for (i = 0; i < len; i++) {
54 unsigned char val = p[i];
57 (val >= ' ' && val <= '~'))
66 static int property_show(struct seq_file *f, void *v)
68 struct property *prop = f->private;
75 if (is_string(pval, len)) {
79 seq_printf(f, "%s", (char *) pval);
81 /* Skip over the NULL byte too. */
93 seq_printf(f, "%02x.",
94 *(unsigned char *) pval);
97 *(unsigned char *) pval);
105 seq_printf(f, "%08x.",
106 *(unsigned int *) pval);
108 seq_printf(f, "%08x",
109 *(unsigned int *) pval);
119 static void *property_start(struct seq_file *f, loff_t *pos)
126 static void *property_next(struct seq_file *f, void *v, loff_t *pos)
132 static void property_stop(struct seq_file *f, void *v)
137 static const struct seq_operations property_op = {
138 .start = property_start,
139 .next = property_next,
140 .stop = property_stop,
141 .show = property_show
144 static int property_open(struct inode *inode, struct file *file)
146 struct op_inode_info *oi = OP_I(inode);
149 BUG_ON(oi->type != op_inode_prop);
151 ret = seq_open(file, &property_op);
153 struct seq_file *m = file->private_data;
154 m->private = oi->u.prop;
159 static const struct file_operations openpromfs_prop_ops = {
160 .open = property_open,
163 .release = seq_release,
166 static int openpromfs_readdir(struct file *, struct dir_context *);
168 static const struct file_operations openprom_operations = {
169 .read = generic_read_dir,
170 .iterate_shared = openpromfs_readdir,
171 .llseek = generic_file_llseek,
174 static struct dentry *openpromfs_lookup(struct inode *, struct dentry *, unsigned int);
176 static const struct inode_operations openprom_inode_operations = {
177 .lookup = openpromfs_lookup,
180 static struct dentry *openpromfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
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;
192 BUG_ON(oi->type != op_inode_node);
196 name = dentry->d_name.name;
197 len = dentry->d_name.len;
199 mutex_lock(&op_mutex);
203 const char *node_name = kbasename(child->full_name);
204 int n = strlen(node_name);
207 !strncmp(node_name, name, len)) {
208 ent_type = op_inode_node;
209 ent_data.node = child;
210 ino = child->unique_id;
213 child = child->sibling;
216 prop = dp->properties;
218 int n = strlen(prop->name);
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;
230 mutex_unlock(&op_mutex);
231 return ERR_PTR(-ENOENT);
234 inode = openprom_iget(dir->i_sb, ino);
235 mutex_unlock(&op_mutex);
237 return ERR_CAST(inode);
238 ent_oi = OP_I(inode);
239 ent_oi->type = ent_type;
240 ent_oi->u = ent_data;
244 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
245 inode->i_op = &openprom_inode_operations;
246 inode->i_fop = &openprom_operations;
250 if (of_node_name_eq(dp, "options") && (len == 17) &&
251 !strncmp (name, "security-password", 17))
252 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
254 inode->i_mode = S_IFREG | S_IRUGO;
255 inode->i_fop = &openpromfs_prop_ops;
257 inode->i_size = ent_oi->u.prop->length;
261 return d_splice_alias(inode, dentry);
264 static int openpromfs_readdir(struct file *file, struct dir_context *ctx)
266 struct inode *inode = file_inode(file);
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;
273 mutex_lock(&op_mutex);
276 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
281 if (!dir_emit(ctx, "..", 2,
282 (dp->parent == NULL ?
284 dp->parent->unique_id), DT_DIR))
290 /* First, the children nodes as directories. */
293 child = child->sibling;
298 kbasename(child->full_name),
299 strlen(kbasename(child->full_name)),
300 child->unique_id, DT_DIR))
304 child = child->sibling;
307 /* Next, the properties as files. */
308 prop = dp->properties;
314 if (!dir_emit(ctx, prop->name, strlen(prop->name),
315 prop->unique_id, DT_REG))
323 mutex_unlock(&op_mutex);
327 static struct kmem_cache *op_inode_cachep;
329 static struct inode *openprom_alloc_inode(struct super_block *sb)
331 struct op_inode_info *oi;
333 oi = kmem_cache_alloc(op_inode_cachep, GFP_KERNEL);
337 return &oi->vfs_inode;
340 static void openprom_free_inode(struct inode *inode)
342 kmem_cache_free(op_inode_cachep, OP_I(inode));
345 static struct inode *openprom_iget(struct super_block *sb, ino_t ino)
349 inode = iget_locked(sb, ino);
351 return ERR_PTR(-ENOMEM);
352 if (inode->i_state & I_NEW) {
353 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
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;
359 unlock_new_inode(inode);
364 static int openprom_remount(struct super_block *sb, int *flags, char *data)
367 *flags |= SB_NOATIME;
371 static const struct super_operations openprom_sops = {
372 .alloc_inode = openprom_alloc_inode,
373 .free_inode = openprom_free_inode,
374 .statfs = simple_statfs,
375 .remount_fs = openprom_remount,
378 static int openprom_fill_super(struct super_block *s, void *data, int silent)
380 struct inode *root_inode;
381 struct op_inode_info *oi;
384 s->s_flags |= SB_NOATIME;
385 s->s_blocksize = 1024;
386 s->s_blocksize_bits = 10;
387 s->s_magic = OPENPROM_SUPER_MAGIC;
388 s->s_op = &openprom_sops;
390 root_inode = openprom_iget(s, OPENPROM_ROOT_INO);
391 if (IS_ERR(root_inode)) {
392 ret = PTR_ERR(root_inode);
396 oi = OP_I(root_inode);
397 oi->type = op_inode_node;
398 oi->u.node = of_find_node_by_path("/");
400 s->s_root = d_make_root(root_inode);
402 goto out_no_root_dentry;
408 printk("openprom_fill_super: get root inode failed\n");
412 static struct dentry *openprom_mount(struct file_system_type *fs_type,
413 int flags, const char *dev_name, void *data)
415 return mount_single(fs_type, flags, data, openprom_fill_super);
418 static struct file_system_type openprom_fs_type = {
419 .owner = THIS_MODULE,
420 .name = "openpromfs",
421 .mount = openprom_mount,
422 .kill_sb = kill_anon_super,
424 MODULE_ALIAS_FS("openpromfs");
426 static void op_inode_init_once(void *data)
428 struct op_inode_info *oi = (struct op_inode_info *) data;
430 inode_init_once(&oi->vfs_inode);
433 static int __init init_openprom_fs(void)
437 op_inode_cachep = kmem_cache_create("op_inode_cache",
438 sizeof(struct op_inode_info),
440 (SLAB_RECLAIM_ACCOUNT |
441 SLAB_MEM_SPREAD | SLAB_ACCOUNT),
443 if (!op_inode_cachep)
446 err = register_filesystem(&openprom_fs_type);
448 kmem_cache_destroy(op_inode_cachep);
453 static void __exit exit_openprom_fs(void)
455 unregister_filesystem(&openprom_fs_type);
457 * Make sure all delayed rcu free inodes are flushed before we
461 kmem_cache_destroy(op_inode_cachep);
464 module_init(init_openprom_fs)
465 module_exit(exit_openprom_fs)
466 MODULE_LICENSE("GPL");