inode.c 9.6 KB

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