inode.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 *, void *, filldir_t);
  135. static const struct file_operations openprom_operations = {
  136. .read = generic_read_dir,
  137. .readdir = openpromfs_readdir,
  138. .llseek = generic_file_llseek,
  139. };
  140. static struct dentry *openpromfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  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, struct nameidata *nd)
  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. inode->i_nlink = 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. inode->i_nlink = 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 * filp, void * dirent, filldir_t filldir)
  215. {
  216. struct inode *inode = filp->f_path.dentry->d_inode;
  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. unsigned int ino;
  222. int i;
  223. mutex_lock(&op_mutex);
  224. ino = inode->i_ino;
  225. i = filp->f_pos;
  226. switch (i) {
  227. case 0:
  228. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  229. goto out;
  230. i++;
  231. filp->f_pos++;
  232. /* fall thru */
  233. case 1:
  234. if (filldir(dirent, "..", 2, i,
  235. (dp->parent == NULL ?
  236. OPENPROM_ROOT_INO :
  237. dp->parent->unique_id), DT_DIR) < 0)
  238. goto out;
  239. i++;
  240. filp->f_pos++;
  241. /* fall thru */
  242. default:
  243. i -= 2;
  244. /* First, the children nodes as directories. */
  245. child = dp->child;
  246. while (i && child) {
  247. child = child->sibling;
  248. i--;
  249. }
  250. while (child) {
  251. if (filldir(dirent,
  252. child->path_component_name,
  253. strlen(child->path_component_name),
  254. filp->f_pos, child->unique_id, DT_DIR) < 0)
  255. goto out;
  256. filp->f_pos++;
  257. child = child->sibling;
  258. }
  259. /* Next, the properties as files. */
  260. prop = dp->properties;
  261. while (i && prop) {
  262. prop = prop->next;
  263. i--;
  264. }
  265. while (prop) {
  266. if (filldir(dirent, prop->name, strlen(prop->name),
  267. filp->f_pos, prop->unique_id, DT_REG) < 0)
  268. goto out;
  269. filp->f_pos++;
  270. prop = prop->next;
  271. }
  272. }
  273. out:
  274. mutex_unlock(&op_mutex);
  275. return 0;
  276. }
  277. static struct kmem_cache *op_inode_cachep;
  278. static struct inode *openprom_alloc_inode(struct super_block *sb)
  279. {
  280. struct op_inode_info *oi;
  281. oi = kmem_cache_alloc(op_inode_cachep, GFP_KERNEL);
  282. if (!oi)
  283. return NULL;
  284. return &oi->vfs_inode;
  285. }
  286. static void openprom_i_callback(struct rcu_head *head)
  287. {
  288. struct inode *inode = container_of(head, struct inode, i_rcu);
  289. INIT_LIST_HEAD(&inode->i_dentry);
  290. kmem_cache_free(op_inode_cachep, OP_I(inode));
  291. }
  292. static void openprom_destroy_inode(struct inode *inode)
  293. {
  294. call_rcu(&inode->i_rcu, openprom_i_callback);
  295. }
  296. static struct inode *openprom_iget(struct super_block *sb, ino_t ino)
  297. {
  298. struct inode *inode;
  299. inode = iget_locked(sb, ino);
  300. if (!inode)
  301. return ERR_PTR(-ENOMEM);
  302. if (inode->i_state & I_NEW) {
  303. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  304. if (inode->i_ino == OPENPROM_ROOT_INO) {
  305. inode->i_op = &openprom_inode_operations;
  306. inode->i_fop = &openprom_operations;
  307. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  308. }
  309. unlock_new_inode(inode);
  310. }
  311. return inode;
  312. }
  313. static int openprom_remount(struct super_block *sb, int *flags, char *data)
  314. {
  315. *flags |= MS_NOATIME;
  316. return 0;
  317. }
  318. static const struct super_operations openprom_sops = {
  319. .alloc_inode = openprom_alloc_inode,
  320. .destroy_inode = openprom_destroy_inode,
  321. .statfs = simple_statfs,
  322. .remount_fs = openprom_remount,
  323. };
  324. static int openprom_fill_super(struct super_block *s, void *data, int silent)
  325. {
  326. struct inode *root_inode;
  327. struct op_inode_info *oi;
  328. int ret;
  329. s->s_flags |= MS_NOATIME;
  330. s->s_blocksize = 1024;
  331. s->s_blocksize_bits = 10;
  332. s->s_magic = OPENPROM_SUPER_MAGIC;
  333. s->s_op = &openprom_sops;
  334. s->s_time_gran = 1;
  335. root_inode = openprom_iget(s, OPENPROM_ROOT_INO);
  336. if (IS_ERR(root_inode)) {
  337. ret = PTR_ERR(root_inode);
  338. goto out_no_root;
  339. }
  340. oi = OP_I(root_inode);
  341. oi->type = op_inode_node;
  342. oi->u.node = of_find_node_by_path("/");
  343. s->s_root = d_alloc_root(root_inode);
  344. if (!s->s_root)
  345. goto out_no_root_dentry;
  346. return 0;
  347. out_no_root_dentry:
  348. iput(root_inode);
  349. ret = -ENOMEM;
  350. out_no_root:
  351. printk("openprom_fill_super: get root inode failed\n");
  352. return ret;
  353. }
  354. static struct dentry *openprom_mount(struct file_system_type *fs_type,
  355. int flags, const char *dev_name, void *data)
  356. {
  357. return mount_single(fs_type, flags, data, openprom_fill_super);
  358. }
  359. static struct file_system_type openprom_fs_type = {
  360. .owner = THIS_MODULE,
  361. .name = "openpromfs",
  362. .mount = openprom_mount,
  363. .kill_sb = kill_anon_super,
  364. };
  365. static void op_inode_init_once(void *data)
  366. {
  367. struct op_inode_info *oi = (struct op_inode_info *) data;
  368. inode_init_once(&oi->vfs_inode);
  369. }
  370. static int __init init_openprom_fs(void)
  371. {
  372. int err;
  373. op_inode_cachep = kmem_cache_create("op_inode_cache",
  374. sizeof(struct op_inode_info),
  375. 0,
  376. (SLAB_RECLAIM_ACCOUNT |
  377. SLAB_MEM_SPREAD),
  378. op_inode_init_once);
  379. if (!op_inode_cachep)
  380. return -ENOMEM;
  381. err = register_filesystem(&openprom_fs_type);
  382. if (err)
  383. kmem_cache_destroy(op_inode_cachep);
  384. return err;
  385. }
  386. static void __exit exit_openprom_fs(void)
  387. {
  388. unregister_filesystem(&openprom_fs_type);
  389. kmem_cache_destroy(op_inode_cachep);
  390. }
  391. module_init(init_openprom_fs)
  392. module_exit(exit_openprom_fs)
  393. MODULE_LICENSE("GPL");