inode.c 9.5 KB

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