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