PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/openpromfs/inode.c

https://bitbucket.org/cyanogenmod/cm-kernel
C | 466 lines | 376 code | 79 blank | 11 comment | 52 complexity | 24497dcea0a287a0f3f47f8af08bc2c0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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_destroy_inode(struct inode *inode)
  287. {
  288. kmem_cache_free(op_inode_cachep, OP_I(inode));
  289. }
  290. static struct inode *openprom_iget(struct super_block *sb, ino_t ino)
  291. {
  292. struct inode *inode;
  293. inode = iget_locked(sb, ino);
  294. if (!inode)
  295. return ERR_PTR(-ENOMEM);
  296. if (inode->i_state & I_NEW) {
  297. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  298. if (inode->i_ino == OPENPROM_ROOT_INO) {
  299. inode->i_op = &openprom_inode_operations;
  300. inode->i_fop = &openprom_operations;
  301. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  302. }
  303. unlock_new_inode(inode);
  304. }
  305. return inode;
  306. }
  307. static int openprom_remount(struct super_block *sb, int *flags, char *data)
  308. {
  309. *flags |= MS_NOATIME;
  310. return 0;
  311. }
  312. static const struct super_operations openprom_sops = {
  313. .alloc_inode = openprom_alloc_inode,
  314. .destroy_inode = openprom_destroy_inode,
  315. .statfs = simple_statfs,
  316. .remount_fs = openprom_remount,
  317. };
  318. static int openprom_fill_super(struct super_block *s, void *data, int silent)
  319. {
  320. struct inode *root_inode;
  321. struct op_inode_info *oi;
  322. int ret;
  323. s->s_flags |= MS_NOATIME;
  324. s->s_blocksize = 1024;
  325. s->s_blocksize_bits = 10;
  326. s->s_magic = OPENPROM_SUPER_MAGIC;
  327. s->s_op = &openprom_sops;
  328. s->s_time_gran = 1;
  329. root_inode = openprom_iget(s, OPENPROM_ROOT_INO);
  330. if (IS_ERR(root_inode)) {
  331. ret = PTR_ERR(root_inode);
  332. goto out_no_root;
  333. }
  334. oi = OP_I(root_inode);
  335. oi->type = op_inode_node;
  336. oi->u.node = of_find_node_by_path("/");
  337. s->s_root = d_alloc_root(root_inode);
  338. if (!s->s_root)
  339. goto out_no_root_dentry;
  340. return 0;
  341. out_no_root_dentry:
  342. iput(root_inode);
  343. ret = -ENOMEM;
  344. out_no_root:
  345. printk("openprom_fill_super: get root inode failed\n");
  346. return ret;
  347. }
  348. static struct dentry *openprom_mount(struct file_system_type *fs_type,
  349. int flags, const char *dev_name, void *data)
  350. {
  351. return mount_single(fs_type, flags, data, openprom_fill_super);
  352. }
  353. static struct file_system_type openprom_fs_type = {
  354. .owner = THIS_MODULE,
  355. .name = "openpromfs",
  356. .mount = openprom_mount,
  357. .kill_sb = kill_anon_super,
  358. };
  359. static void op_inode_init_once(void *data)
  360. {
  361. struct op_inode_info *oi = (struct op_inode_info *) data;
  362. inode_init_once(&oi->vfs_inode);
  363. }
  364. static int __init init_openprom_fs(void)
  365. {
  366. int err;
  367. op_inode_cachep = kmem_cache_create("op_inode_cache",
  368. sizeof(struct op_inode_info),
  369. 0,
  370. (SLAB_RECLAIM_ACCOUNT |
  371. SLAB_MEM_SPREAD),
  372. op_inode_init_once);
  373. if (!op_inode_cachep)
  374. return -ENOMEM;
  375. err = register_filesystem(&openprom_fs_type);
  376. if (err)
  377. kmem_cache_destroy(op_inode_cachep);
  378. return err;
  379. }
  380. static void __exit exit_openprom_fs(void)
  381. {
  382. unregister_filesystem(&openprom_fs_type);
  383. kmem_cache_destroy(op_inode_cachep);
  384. }
  385. module_init(init_openprom_fs)
  386. module_exit(exit_openprom_fs)
  387. MODULE_LICENSE("GPL");