blob: 8c0ceb8dd1f709a6b3b6427eabc28718f23f9a50 [file] [log] [blame]
David S. Miller3d824a42006-06-25 23:19:14 -07001/* inode.c: /proc/openprom handling routines
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1996-1999 Jakub Jelinek (jakub@redhat.com)
4 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
5 */
6
7#include <linux/module.h>
8#include <linux/types.h>
9#include <linux/string.h>
10#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/slab.h>
David S. Miller3d824a42006-06-25 23:19:14 -070013#include <linux/seq_file.h>
Jeff Garzike18fa702006-09-24 11:13:19 -040014#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/openprom.h>
17#include <asm/oplib.h>
David S. Miller3d824a42006-06-25 23:19:14 -070018#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
20
David S. Miller3d824a42006-06-25 23:19:14 -070021static DEFINE_MUTEX(op_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
David S. Miller3d824a42006-06-25 23:19:14 -070023#define OPENPROM_ROOT_INO 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David S. Miller3d824a42006-06-25 23:19:14 -070025enum op_inode_type {
26 op_inode_node,
27 op_inode_prop,
28};
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David S. Miller3d824a42006-06-25 23:19:14 -070030union op_inode_data {
31 struct device_node *node;
32 struct property *prop;
33};
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David S. Miller3d824a42006-06-25 23:19:14 -070035struct op_inode_info {
36 struct inode vfs_inode;
37 enum op_inode_type type;
38 union op_inode_data u;
39};
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
David Howellsb88a27e2008-02-07 00:15:49 -080041static struct inode *openprom_iget(struct super_block *sb, ino_t ino);
42
David S. Miller3d824a42006-06-25 23:19:14 -070043static inline struct op_inode_info *OP_I(struct inode *inode)
Jan Engelhardta04ee142006-06-25 05:47:36 -070044{
David S. Miller3d824a42006-06-25 23:19:14 -070045 return container_of(inode, struct op_inode_info, vfs_inode);
Jan Engelhardta04ee142006-06-25 05:47:36 -070046}
47
David S. Miller3d824a42006-06-25 23:19:14 -070048static int is_string(unsigned char *p, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
David S. Miller3d824a42006-06-25 23:19:14 -070050 int i;
51
52 for (i = 0; i < len; i++) {
53 unsigned char val = p[i];
54
55 if ((i && !val) ||
56 (val >= ' ' && val <= '~'))
57 continue;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 0;
David S. Miller3d824a42006-06-25 23:19:14 -070060 }
61
62 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
David S. Miller3d824a42006-06-25 23:19:14 -070065static int property_show(struct seq_file *f, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
David S. Miller3d824a42006-06-25 23:19:14 -070067 struct property *prop = f->private;
68 void *pval;
69 int len;
70
71 len = prop->length;
72 pval = prop->value;
73
74 if (is_string(pval, len)) {
75 while (len > 0) {
76 int n = strlen(pval);
77
78 seq_printf(f, "%s", (char *) pval);
79
80 /* Skip over the NULL byte too. */
81 pval += n + 1;
82 len -= n + 1;
83
84 if (len > 0)
85 seq_printf(f, " + ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 } else {
David S. Miller3d824a42006-06-25 23:19:14 -070088 if (len & 3) {
89 while (len) {
90 len--;
91 if (len)
92 seq_printf(f, "%02x.",
93 *(unsigned char *) pval);
94 else
95 seq_printf(f, "%02x",
96 *(unsigned char *) pval);
97 pval++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 } else {
David S. Miller3d824a42006-06-25 23:19:14 -0700100 while (len >= 4) {
101 len -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
David S. Miller3d824a42006-06-25 23:19:14 -0700103 if (len)
104 seq_printf(f, "%08x.",
105 *(unsigned int *) pval);
106 else
107 seq_printf(f, "%08x",
108 *(unsigned int *) pval);
109 pval += 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
David S. Miller3d824a42006-06-25 23:19:14 -0700113 seq_printf(f, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
David S. Miller3d824a42006-06-25 23:19:14 -0700118static void *property_start(struct seq_file *f, loff_t *pos)
119{
120 if (*pos == 0)
121 return pos;
122 return NULL;
123}
124
125static void *property_next(struct seq_file *f, void *v, loff_t *pos)
126{
127 (*pos)++;
128 return NULL;
129}
130
131static void property_stop(struct seq_file *f, void *v)
132{
133 /* Nothing to do */
134}
135
Jan Engelhardt872e2be2008-01-22 18:29:20 -0800136static const struct seq_operations property_op = {
David S. Miller3d824a42006-06-25 23:19:14 -0700137 .start = property_start,
138 .next = property_next,
139 .stop = property_stop,
140 .show = property_show
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
David S. Miller3d824a42006-06-25 23:19:14 -0700143static int property_open(struct inode *inode, struct file *file)
144{
145 struct op_inode_info *oi = OP_I(inode);
146 int ret;
147
148 BUG_ON(oi->type != op_inode_prop);
149
150 ret = seq_open(file, &property_op);
151 if (!ret) {
152 struct seq_file *m = file->private_data;
153 m->private = oi->u.prop;
154 }
155 return ret;
156}
157
158static const struct file_operations openpromfs_prop_ops = {
159 .open = property_open,
160 .read = seq_read,
161 .llseek = seq_lseek,
162 .release = seq_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163};
164
Al Viro68c61472013-05-16 01:52:12 -0400165static int openpromfs_readdir(struct file *, struct dir_context *);
David S. Miller3d824a42006-06-25 23:19:14 -0700166
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800167static const struct file_operations openprom_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .read = generic_read_dir,
Al Viro68c61472013-05-16 01:52:12 -0400169 .iterate = openpromfs_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200170 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
Al Viro00cd8dd2012-06-10 17:13:09 -0400173static struct dentry *openpromfs_lookup(struct inode *, struct dentry *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800175static const struct inode_operations openprom_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .lookup = openpromfs_lookup,
177};
178
Al Viro00cd8dd2012-06-10 17:13:09 -0400179static struct dentry *openpromfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
David S. Miller3d824a42006-06-25 23:19:14 -0700181 struct op_inode_info *ent_oi, *oi = OP_I(dir);
182 struct device_node *dp, *child;
183 struct property *prop;
184 enum op_inode_type ent_type;
185 union op_inode_data ent_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct inode *inode;
David S. Miller3d824a42006-06-25 23:19:14 -0700188 unsigned int ino;
189 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David S. Miller3d824a42006-06-25 23:19:14 -0700191 BUG_ON(oi->type != op_inode_node);
192
193 dp = oi->u.node;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 name = dentry->d_name.name;
196 len = dentry->d_name.len;
David S. Miller3d824a42006-06-25 23:19:14 -0700197
198 mutex_lock(&op_mutex);
199
200 child = dp->child;
201 while (child) {
202 int n = strlen(child->path_component_name);
203
204 if (len == n &&
205 !strncmp(child->path_component_name, name, len)) {
206 ent_type = op_inode_node;
207 ent_data.node = child;
208 ino = child->unique_id;
209 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
David S. Miller3d824a42006-06-25 23:19:14 -0700211 child = child->sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
David S. Miller3d824a42006-06-25 23:19:14 -0700213
214 prop = dp->properties;
215 while (prop) {
216 int n = strlen(prop->name);
217
218 if (len == n && !strncmp(prop->name, name, len)) {
219 ent_type = op_inode_prop;
220 ent_data.prop = prop;
221 ino = prop->unique_id;
222 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
David S. Miller3d824a42006-06-25 23:19:14 -0700224
225 prop = prop->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
David S. Miller3d824a42006-06-25 23:19:14 -0700227
228 mutex_unlock(&op_mutex);
229 return ERR_PTR(-ENOENT);
230
231found:
David Howellsb88a27e2008-02-07 00:15:49 -0800232 inode = openprom_iget(dir->i_sb, ino);
David S. Miller3d824a42006-06-25 23:19:14 -0700233 mutex_unlock(&op_mutex);
David Howellsb88a27e2008-02-07 00:15:49 -0800234 if (IS_ERR(inode))
235 return ERR_CAST(inode);
David S. Miller3d824a42006-06-25 23:19:14 -0700236 ent_oi = OP_I(inode);
237 ent_oi->type = ent_type;
238 ent_oi->u = ent_data;
239
240 switch (ent_type) {
241 case op_inode_node:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
David S. Miller3d824a42006-06-25 23:19:14 -0700243 inode->i_op = &openprom_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 inode->i_fop = &openprom_operations;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200245 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
David S. Miller3d824a42006-06-25 23:19:14 -0700247 case op_inode_prop:
248 if (!strcmp(dp->name, "options") && (len == 17) &&
249 !strncmp (name, "security-password", 17))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
David S. Miller3d824a42006-06-25 23:19:14 -0700251 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 inode->i_mode = S_IFREG | S_IRUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 inode->i_fop = &openpromfs_prop_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200254 set_nlink(inode, 1);
David S. Miller3d824a42006-06-25 23:19:14 -0700255 inode->i_size = ent_oi->u.prop->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 }
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 d_add(dentry, inode);
260 return NULL;
261}
262
Al Viro68c61472013-05-16 01:52:12 -0400263static int openpromfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Al Viro68c61472013-05-16 01:52:12 -0400265 struct inode *inode = file_inode(file);
David S. Miller3d824a42006-06-25 23:19:14 -0700266 struct op_inode_info *oi = OP_I(inode);
267 struct device_node *dp = oi->u.node;
268 struct device_node *child;
269 struct property *prop;
David S. Miller3d824a42006-06-25 23:19:14 -0700270 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David S. Miller3d824a42006-06-25 23:19:14 -0700272 mutex_lock(&op_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Al Viro68c61472013-05-16 01:52:12 -0400274 if (ctx->pos == 0) {
275 if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
David S. Miller3d824a42006-06-25 23:19:14 -0700276 goto out;
Al Viro68c61472013-05-16 01:52:12 -0400277 ctx->pos = 1;
278 }
279 if (ctx->pos == 1) {
280 if (!dir_emit(ctx, "..", 2,
David S. Miller3d824a42006-06-25 23:19:14 -0700281 (dp->parent == NULL ?
282 OPENPROM_ROOT_INO :
Al Viro68c61472013-05-16 01:52:12 -0400283 dp->parent->unique_id), DT_DIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto out;
Al Viro68c61472013-05-16 01:52:12 -0400285 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Al Viro68c61472013-05-16 01:52:12 -0400287 i = ctx->pos - 2;
288
289 /* First, the children nodes as directories. */
290 child = dp->child;
291 while (i && child) {
292 child = child->sibling;
293 i--;
294 }
295 while (child) {
296 if (!dir_emit(ctx,
297 child->path_component_name,
298 strlen(child->path_component_name),
299 child->unique_id, DT_DIR))
300 goto out;
301
302 ctx->pos++;
303 child = child->sibling;
304 }
305
306 /* Next, the properties as files. */
307 prop = dp->properties;
308 while (i && prop) {
309 prop = prop->next;
310 i--;
311 }
312 while (prop) {
313 if (!dir_emit(ctx, prop->name, strlen(prop->name),
314 prop->unique_id, DT_REG))
315 goto out;
316
317 ctx->pos++;
318 prop = prop->next;
319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321out:
David S. Miller3d824a42006-06-25 23:19:14 -0700322 mutex_unlock(&op_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return 0;
324}
325
Christoph Lametere18b8902006-12-06 20:33:20 -0800326static struct kmem_cache *op_inode_cachep;
David S. Miller3d824a42006-06-25 23:19:14 -0700327
328static struct inode *openprom_alloc_inode(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
David S. Miller3d824a42006-06-25 23:19:14 -0700330 struct op_inode_info *oi;
331
Christoph Lametere94b1762006-12-06 20:33:17 -0800332 oi = kmem_cache_alloc(op_inode_cachep, GFP_KERNEL);
David S. Miller3d824a42006-06-25 23:19:14 -0700333 if (!oi)
334 return NULL;
335
336 return &oi->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100339static void openprom_i_callback(struct rcu_head *head)
340{
341 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100342 kmem_cache_free(op_inode_cachep, OP_I(inode));
343}
344
David S. Miller3d824a42006-06-25 23:19:14 -0700345static void openprom_destroy_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100347 call_rcu(&inode->i_rcu, openprom_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
David Howellsb88a27e2008-02-07 00:15:49 -0800350static struct inode *openprom_iget(struct super_block *sb, ino_t ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
David Howellsb88a27e2008-02-07 00:15:49 -0800352 struct inode *inode;
353
354 inode = iget_locked(sb, ino);
355 if (!inode)
356 return ERR_PTR(-ENOMEM);
357 if (inode->i_state & I_NEW) {
358 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
359 if (inode->i_ino == OPENPROM_ROOT_INO) {
360 inode->i_op = &openprom_inode_operations;
361 inode->i_fop = &openprom_operations;
362 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
363 }
364 unlock_new_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
David Howellsb88a27e2008-02-07 00:15:49 -0800366 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369static int openprom_remount(struct super_block *sb, int *flags, char *data)
370{
371 *flags |= MS_NOATIME;
372 return 0;
373}
374
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800375static const struct super_operations openprom_sops = {
David S. Miller3d824a42006-06-25 23:19:14 -0700376 .alloc_inode = openprom_alloc_inode,
377 .destroy_inode = openprom_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 .statfs = simple_statfs,
379 .remount_fs = openprom_remount,
380};
381
382static int openprom_fill_super(struct super_block *s, void *data, int silent)
383{
David S. Miller3d824a42006-06-25 23:19:14 -0700384 struct inode *root_inode;
385 struct op_inode_info *oi;
David Howellsb88a27e2008-02-07 00:15:49 -0800386 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 s->s_flags |= MS_NOATIME;
389 s->s_blocksize = 1024;
390 s->s_blocksize_bits = 10;
391 s->s_magic = OPENPROM_SUPER_MAGIC;
392 s->s_op = &openprom_sops;
393 s->s_time_gran = 1;
David Howellsb88a27e2008-02-07 00:15:49 -0800394 root_inode = openprom_iget(s, OPENPROM_ROOT_INO);
395 if (IS_ERR(root_inode)) {
396 ret = PTR_ERR(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto out_no_root;
David Howellsb88a27e2008-02-07 00:15:49 -0800398 }
David S. Miller3d824a42006-06-25 23:19:14 -0700399
400 oi = OP_I(root_inode);
401 oi->type = op_inode_node;
402 oi->u.node = of_find_node_by_path("/");
403
Al Viro48fde702012-01-08 22:15:13 -0500404 s->s_root = d_make_root(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!s->s_root)
David Howellsb88a27e2008-02-07 00:15:49 -0800406 goto out_no_root_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408
David Howellsb88a27e2008-02-07 00:15:49 -0800409out_no_root_dentry:
David Howellsb88a27e2008-02-07 00:15:49 -0800410 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411out_no_root:
412 printk("openprom_fill_super: get root inode failed\n");
David Howellsb88a27e2008-02-07 00:15:49 -0800413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Al Virofc14f2f2010-07-25 01:48:30 +0400416static struct dentry *openprom_mount(struct file_system_type *fs_type,
417 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Meelis Roos0e154822010-11-08 13:38:14 -0800419 return mount_single(fs_type, flags, data, openprom_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422static struct file_system_type openprom_fs_type = {
423 .owner = THIS_MODULE,
424 .name = "openpromfs",
Al Virofc14f2f2010-07-25 01:48:30 +0400425 .mount = openprom_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 .kill_sb = kill_anon_super,
427};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800428MODULE_ALIAS_FS("openpromfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700430static void op_inode_init_once(void *data)
David S. Miller3d824a42006-06-25 23:19:14 -0700431{
432 struct op_inode_info *oi = (struct op_inode_info *) data;
433
Christoph Lametera35afb82007-05-16 22:10:57 -0700434 inode_init_once(&oi->vfs_inode);
David S. Miller3d824a42006-06-25 23:19:14 -0700435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static int __init init_openprom_fs(void)
438{
David S. Miller3d824a42006-06-25 23:19:14 -0700439 int err;
440
441 op_inode_cachep = kmem_cache_create("op_inode_cache",
442 sizeof(struct op_inode_info),
443 0,
444 (SLAB_RECLAIM_ACCOUNT |
445 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900446 op_inode_init_once);
David S. Miller3d824a42006-06-25 23:19:14 -0700447 if (!op_inode_cachep)
448 return -ENOMEM;
449
450 err = register_filesystem(&openprom_fs_type);
451 if (err)
452 kmem_cache_destroy(op_inode_cachep);
453
454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static void __exit exit_openprom_fs(void)
458{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 unregister_filesystem(&openprom_fs_type);
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000460 /*
461 * Make sure all delayed rcu free inodes are flushed before we
462 * destroy cache.
463 */
464 rcu_barrier();
David S. Miller3d824a42006-06-25 23:19:14 -0700465 kmem_cache_destroy(op_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
468module_init(init_openprom_fs)
469module_exit(exit_openprom_fs)
470MODULE_LICENSE("GPL");