inode.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 inline struct op_inode_info *OP_I(struct inode *inode)
  34. {
  35. return container_of(inode, struct op_inode_info, vfs_inode);
  36. }
  37. static int is_string(unsigned char *p, int len)
  38. {
  39. int i;
  40. for (i = 0; i < len; i++) {
  41. unsigned char val = p[i];
  42. if ((i && !val) ||
  43. (val >= ' ' && val <= '~'))
  44. continue;
  45. return 0;
  46. }
  47. return 1;
  48. }
  49. static int property_show(struct seq_file *f, void *v)
  50. {
  51. struct property *prop = f->private;
  52. void *pval;
  53. int len;
  54. len = prop->length;
  55. pval = prop->value;
  56. if (is_string(pval, len)) {
  57. while (len > 0) {
  58. int n = strlen(pval);
  59. seq_printf(f, "%s", (char *) pval);
  60. /* Skip over the NULL byte too. */
  61. pval += n + 1;
  62. len -= n + 1;
  63. if (len > 0)
  64. seq_printf(f, " + ");
  65. }
  66. } else {
  67. if (len & 3) {
  68. while (len) {
  69. len--;
  70. if (len)
  71. seq_printf(f, "%02x.",
  72. *(unsigned char *) pval);
  73. else
  74. seq_printf(f, "%02x",
  75. *(unsigned char *) pval);
  76. pval++;
  77. }
  78. } else {
  79. while (len >= 4) {
  80. len -= 4;
  81. if (len)
  82. seq_printf(f, "%08x.",
  83. *(unsigned int *) pval);
  84. else
  85. seq_printf(f, "%08x",
  86. *(unsigned int *) pval);
  87. pval += 4;
  88. }
  89. }
  90. }
  91. seq_printf(f, "\n");
  92. return 0;
  93. }
  94. static void *property_start(struct seq_file *f, loff_t *pos)
  95. {
  96. if (*pos == 0)
  97. return pos;
  98. return NULL;
  99. }
  100. static void *property_next(struct seq_file *f, void *v, loff_t *pos)
  101. {
  102. (*pos)++;
  103. return NULL;
  104. }
  105. static void property_stop(struct seq_file *f, void *v)
  106. {
  107. /* Nothing to do */
  108. }
  109. static struct seq_operations property_op = {
  110. .start = property_start,
  111. .next = property_next,
  112. .stop = property_stop,
  113. .show = property_show
  114. };
  115. static int property_open(struct inode *inode, struct file *file)
  116. {
  117. struct op_inode_info *oi = OP_I(inode);
  118. int ret;
  119. BUG_ON(oi->type != op_inode_prop);
  120. ret = seq_open(file, &property_op);
  121. if (!ret) {
  122. struct seq_file *m = file->private_data;
  123. m->private = oi->u.prop;
  124. }
  125. return ret;
  126. }
  127. static const struct file_operations openpromfs_prop_ops = {
  128. .open = property_open,
  129. .read = seq_read,
  130. .llseek = seq_lseek,
  131. .release = seq_release,
  132. };
  133. static int openpromfs_readdir(struct file *, void *, filldir_t);
  134. static const struct file_operations openprom_operations = {
  135. .read = generic_read_dir,
  136. .readdir = openpromfs_readdir,
  137. };
  138. static struct dentry *openpromfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  139. static const struct inode_operations openprom_inode_operations = {
  140. .lookup = openpromfs_lookup,
  141. };
  142. static struct dentry *openpromfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  143. {
  144. struct op_inode_info *ent_oi, *oi = OP_I(dir);
  145. struct device_node *dp, *child;
  146. struct property *prop;
  147. enum op_inode_type ent_type;
  148. union op_inode_data ent_data;
  149. const char *name;
  150. struct inode *inode;
  151. unsigned int ino;
  152. int len;
  153. BUG_ON(oi->type != op_inode_node);
  154. dp = oi->u.node;
  155. name = dentry->d_name.name;
  156. len = dentry->d_name.len;
  157. mutex_lock(&op_mutex);
  158. child = dp->child;
  159. while (child) {
  160. int n = strlen(child->path_component_name);
  161. if (len == n &&
  162. !strncmp(child->path_component_name, name, len)) {
  163. ent_type = op_inode_node;
  164. ent_data.node = child;
  165. ino = child->unique_id;
  166. goto found;
  167. }
  168. child = child->sibling;
  169. }
  170. prop = dp->properties;
  171. while (prop) {
  172. int n = strlen(prop->name);
  173. if (len == n && !strncmp(prop->name, name, len)) {
  174. ent_type = op_inode_prop;
  175. ent_data.prop = prop;
  176. ino = prop->unique_id;
  177. goto found;
  178. }
  179. prop = prop->next;
  180. }
  181. mutex_unlock(&op_mutex);
  182. return ERR_PTR(-ENOENT);
  183. found:
  184. inode = iget(dir->i_sb, ino);
  185. mutex_unlock(&op_mutex);
  186. if (!inode)
  187. return ERR_PTR(-EINVAL);
  188. ent_oi = OP_I(inode);
  189. ent_oi->type = ent_type;
  190. ent_oi->u = ent_data;
  191. switch (ent_type) {
  192. case op_inode_node:
  193. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  194. inode->i_op = &openprom_inode_operations;
  195. inode->i_fop = &openprom_operations;
  196. inode->i_nlink = 2;
  197. break;
  198. case op_inode_prop:
  199. if (!strcmp(dp->name, "options") && (len == 17) &&
  200. !strncmp (name, "security-password", 17))
  201. inode->i_mode = S_IFREG | S_IRUSR | S_IWUSR;
  202. else
  203. inode->i_mode = S_IFREG | S_IRUGO;
  204. inode->i_fop = &openpromfs_prop_ops;
  205. inode->i_nlink = 1;
  206. inode->i_size = ent_oi->u.prop->length;
  207. break;
  208. }
  209. inode->i_gid = 0;
  210. inode->i_uid = 0;
  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 void openprom_read_inode(struct inode * inode)
  291. {
  292. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  293. if (inode->i_ino == OPENPROM_ROOT_INO) {
  294. inode->i_op = &openprom_inode_operations;
  295. inode->i_fop = &openprom_operations;
  296. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  297. }
  298. }
  299. static int openprom_remount(struct super_block *sb, int *flags, char *data)
  300. {
  301. *flags |= MS_NOATIME;
  302. return 0;
  303. }
  304. static const struct super_operations openprom_sops = {
  305. .alloc_inode = openprom_alloc_inode,
  306. .destroy_inode = openprom_destroy_inode,
  307. .read_inode = openprom_read_inode,
  308. .statfs = simple_statfs,
  309. .remount_fs = openprom_remount,
  310. };
  311. static int openprom_fill_super(struct super_block *s, void *data, int silent)
  312. {
  313. struct inode *root_inode;
  314. struct op_inode_info *oi;
  315. s->s_flags |= MS_NOATIME;
  316. s->s_blocksize = 1024;
  317. s->s_blocksize_bits = 10;
  318. s->s_magic = OPENPROM_SUPER_MAGIC;
  319. s->s_op = &openprom_sops;
  320. s->s_time_gran = 1;
  321. root_inode = iget(s, OPENPROM_ROOT_INO);
  322. if (!root_inode)
  323. goto out_no_root;
  324. oi = OP_I(root_inode);
  325. oi->type = op_inode_node;
  326. oi->u.node = of_find_node_by_path("/");
  327. s->s_root = d_alloc_root(root_inode);
  328. if (!s->s_root)
  329. goto out_no_root;
  330. return 0;
  331. out_no_root:
  332. printk("openprom_fill_super: get root inode failed\n");
  333. iput(root_inode);
  334. return -ENOMEM;
  335. }
  336. static int openprom_get_sb(struct file_system_type *fs_type,
  337. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  338. {
  339. return get_sb_single(fs_type, flags, data, openprom_fill_super, mnt);
  340. }
  341. static struct file_system_type openprom_fs_type = {
  342. .owner = THIS_MODULE,
  343. .name = "openpromfs",
  344. .get_sb = openprom_get_sb,
  345. .kill_sb = kill_anon_super,
  346. };
  347. static void op_inode_init_once(void *data, struct kmem_cache * cachep, unsigned long flags)
  348. {
  349. struct op_inode_info *oi = (struct op_inode_info *) data;
  350. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  351. SLAB_CTOR_CONSTRUCTOR)
  352. inode_init_once(&oi->vfs_inode);
  353. }
  354. static int __init init_openprom_fs(void)
  355. {
  356. int err;
  357. op_inode_cachep = kmem_cache_create("op_inode_cache",
  358. sizeof(struct op_inode_info),
  359. 0,
  360. (SLAB_RECLAIM_ACCOUNT |
  361. SLAB_MEM_SPREAD),
  362. op_inode_init_once, NULL);
  363. if (!op_inode_cachep)
  364. return -ENOMEM;
  365. err = register_filesystem(&openprom_fs_type);
  366. if (err)
  367. kmem_cache_destroy(op_inode_cachep);
  368. return err;
  369. }
  370. static void __exit exit_openprom_fs(void)
  371. {
  372. unregister_filesystem(&openprom_fs_type);
  373. kmem_cache_destroy(op_inode_cachep);
  374. }
  375. module_init(init_openprom_fs)
  376. module_exit(exit_openprom_fs)
  377. MODULE_LICENSE("GPL");