ipath_fs.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/version.h>
  34. #include <linux/module.h>
  35. #include <linux/fs.h>
  36. #include <linux/mount.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/init.h>
  39. #include <linux/namei.h>
  40. #include "ipath_kernel.h"
  41. #define IPATHFS_MAGIC 0x726a77
  42. static struct super_block *ipath_super;
  43. static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
  44. int mode, const struct file_operations *fops,
  45. void *data)
  46. {
  47. int error;
  48. struct inode *inode = new_inode(dir->i_sb);
  49. if (!inode) {
  50. error = -EPERM;
  51. goto bail;
  52. }
  53. inode->i_mode = mode;
  54. inode->i_uid = 0;
  55. inode->i_gid = 0;
  56. inode->i_blocks = 0;
  57. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  58. inode->i_private = data;
  59. if ((mode & S_IFMT) == S_IFDIR) {
  60. inode->i_op = &simple_dir_inode_operations;
  61. inc_nlink(inode);
  62. inc_nlink(dir);
  63. }
  64. inode->i_fop = fops;
  65. d_instantiate(dentry, inode);
  66. error = 0;
  67. bail:
  68. return error;
  69. }
  70. static int create_file(const char *name, mode_t mode,
  71. struct dentry *parent, struct dentry **dentry,
  72. const struct file_operations *fops, void *data)
  73. {
  74. int error;
  75. *dentry = NULL;
  76. mutex_lock(&parent->d_inode->i_mutex);
  77. *dentry = lookup_one_len(name, parent, strlen(name));
  78. if (!IS_ERR(dentry))
  79. error = ipathfs_mknod(parent->d_inode, *dentry,
  80. mode, fops, data);
  81. else
  82. error = PTR_ERR(dentry);
  83. mutex_unlock(&parent->d_inode->i_mutex);
  84. return error;
  85. }
  86. static ssize_t atomic_stats_read(struct file *file, char __user *buf,
  87. size_t count, loff_t *ppos)
  88. {
  89. return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
  90. sizeof ipath_stats);
  91. }
  92. static const struct file_operations atomic_stats_ops = {
  93. .read = atomic_stats_read,
  94. };
  95. static ssize_t atomic_counters_read(struct file *file, char __user *buf,
  96. size_t count, loff_t *ppos)
  97. {
  98. struct infinipath_counters counters;
  99. struct ipath_devdata *dd;
  100. dd = file->f_path.dentry->d_inode->i_private;
  101. dd->ipath_f_read_counters(dd, &counters);
  102. return simple_read_from_buffer(buf, count, ppos, &counters,
  103. sizeof counters);
  104. }
  105. static const struct file_operations atomic_counters_ops = {
  106. .read = atomic_counters_read,
  107. };
  108. static ssize_t flash_read(struct file *file, char __user *buf,
  109. size_t count, loff_t *ppos)
  110. {
  111. struct ipath_devdata *dd;
  112. ssize_t ret;
  113. loff_t pos;
  114. char *tmp;
  115. pos = *ppos;
  116. if ( pos < 0) {
  117. ret = -EINVAL;
  118. goto bail;
  119. }
  120. if (pos >= sizeof(struct ipath_flash)) {
  121. ret = 0;
  122. goto bail;
  123. }
  124. if (count > sizeof(struct ipath_flash) - pos)
  125. count = sizeof(struct ipath_flash) - pos;
  126. tmp = kmalloc(count, GFP_KERNEL);
  127. if (!tmp) {
  128. ret = -ENOMEM;
  129. goto bail;
  130. }
  131. dd = file->f_path.dentry->d_inode->i_private;
  132. if (ipath_eeprom_read(dd, pos, tmp, count)) {
  133. ipath_dev_err(dd, "failed to read from flash\n");
  134. ret = -ENXIO;
  135. goto bail_tmp;
  136. }
  137. if (copy_to_user(buf, tmp, count)) {
  138. ret = -EFAULT;
  139. goto bail_tmp;
  140. }
  141. *ppos = pos + count;
  142. ret = count;
  143. bail_tmp:
  144. kfree(tmp);
  145. bail:
  146. return ret;
  147. }
  148. static ssize_t flash_write(struct file *file, const char __user *buf,
  149. size_t count, loff_t *ppos)
  150. {
  151. struct ipath_devdata *dd;
  152. ssize_t ret;
  153. loff_t pos;
  154. char *tmp;
  155. pos = *ppos;
  156. if (pos != 0) {
  157. ret = -EINVAL;
  158. goto bail;
  159. }
  160. if (count != sizeof(struct ipath_flash)) {
  161. ret = -EINVAL;
  162. goto bail;
  163. }
  164. tmp = kmalloc(count, GFP_KERNEL);
  165. if (!tmp) {
  166. ret = -ENOMEM;
  167. goto bail;
  168. }
  169. if (copy_from_user(tmp, buf, count)) {
  170. ret = -EFAULT;
  171. goto bail_tmp;
  172. }
  173. dd = file->f_path.dentry->d_inode->i_private;
  174. if (ipath_eeprom_write(dd, pos, tmp, count)) {
  175. ret = -ENXIO;
  176. ipath_dev_err(dd, "failed to write to flash\n");
  177. goto bail_tmp;
  178. }
  179. *ppos = pos + count;
  180. ret = count;
  181. bail_tmp:
  182. kfree(tmp);
  183. bail:
  184. return ret;
  185. }
  186. static const struct file_operations flash_ops = {
  187. .read = flash_read,
  188. .write = flash_write,
  189. };
  190. static int create_device_files(struct super_block *sb,
  191. struct ipath_devdata *dd)
  192. {
  193. struct dentry *dir, *tmp;
  194. char unit[10];
  195. int ret;
  196. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  197. ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
  198. &simple_dir_operations, dd);
  199. if (ret) {
  200. printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
  201. goto bail;
  202. }
  203. ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
  204. &atomic_counters_ops, dd);
  205. if (ret) {
  206. printk(KERN_ERR "create_file(%s/atomic_counters) "
  207. "failed: %d\n", unit, ret);
  208. goto bail;
  209. }
  210. ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
  211. &flash_ops, dd);
  212. if (ret) {
  213. printk(KERN_ERR "create_file(%s/flash) "
  214. "failed: %d\n", unit, ret);
  215. goto bail;
  216. }
  217. bail:
  218. return ret;
  219. }
  220. static int remove_file(struct dentry *parent, char *name)
  221. {
  222. struct dentry *tmp;
  223. int ret;
  224. tmp = lookup_one_len(name, parent, strlen(name));
  225. if (IS_ERR(tmp)) {
  226. ret = PTR_ERR(tmp);
  227. goto bail;
  228. }
  229. spin_lock(&dcache_lock);
  230. spin_lock(&tmp->d_lock);
  231. if (!(d_unhashed(tmp) && tmp->d_inode)) {
  232. dget_locked(tmp);
  233. __d_drop(tmp);
  234. spin_unlock(&tmp->d_lock);
  235. spin_unlock(&dcache_lock);
  236. simple_unlink(parent->d_inode, tmp);
  237. } else {
  238. spin_unlock(&tmp->d_lock);
  239. spin_unlock(&dcache_lock);
  240. }
  241. ret = 0;
  242. bail:
  243. /*
  244. * We don't expect clients to care about the return value, but
  245. * it's there if they need it.
  246. */
  247. return ret;
  248. }
  249. static int remove_device_files(struct super_block *sb,
  250. struct ipath_devdata *dd)
  251. {
  252. struct dentry *dir, *root;
  253. char unit[10];
  254. int ret;
  255. root = dget(sb->s_root);
  256. mutex_lock(&root->d_inode->i_mutex);
  257. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  258. dir = lookup_one_len(unit, root, strlen(unit));
  259. if (IS_ERR(dir)) {
  260. ret = PTR_ERR(dir);
  261. printk(KERN_ERR "Lookup of %s failed\n", unit);
  262. goto bail;
  263. }
  264. remove_file(dir, "flash");
  265. remove_file(dir, "atomic_counters");
  266. d_delete(dir);
  267. ret = simple_rmdir(root->d_inode, dir);
  268. bail:
  269. mutex_unlock(&root->d_inode->i_mutex);
  270. dput(root);
  271. return ret;
  272. }
  273. static int ipathfs_fill_super(struct super_block *sb, void *data,
  274. int silent)
  275. {
  276. struct ipath_devdata *dd, *tmp;
  277. unsigned long flags;
  278. int ret;
  279. static struct tree_descr files[] = {
  280. [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
  281. {""},
  282. };
  283. ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
  284. if (ret) {
  285. printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
  286. goto bail;
  287. }
  288. spin_lock_irqsave(&ipath_devs_lock, flags);
  289. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  290. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  291. ret = create_device_files(sb, dd);
  292. if (ret) {
  293. deactivate_super(sb);
  294. goto bail;
  295. }
  296. spin_lock_irqsave(&ipath_devs_lock, flags);
  297. }
  298. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  299. bail:
  300. return ret;
  301. }
  302. static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
  303. const char *dev_name, void *data, struct vfsmount *mnt)
  304. {
  305. int ret = get_sb_single(fs_type, flags, data,
  306. ipathfs_fill_super, mnt);
  307. if (ret >= 0)
  308. ipath_super = mnt->mnt_sb;
  309. return ret;
  310. }
  311. static void ipathfs_kill_super(struct super_block *s)
  312. {
  313. kill_litter_super(s);
  314. ipath_super = NULL;
  315. }
  316. int ipathfs_add_device(struct ipath_devdata *dd)
  317. {
  318. int ret;
  319. if (ipath_super == NULL) {
  320. ret = 0;
  321. goto bail;
  322. }
  323. ret = create_device_files(ipath_super, dd);
  324. bail:
  325. return ret;
  326. }
  327. int ipathfs_remove_device(struct ipath_devdata *dd)
  328. {
  329. int ret;
  330. if (ipath_super == NULL) {
  331. ret = 0;
  332. goto bail;
  333. }
  334. ret = remove_device_files(ipath_super, dd);
  335. bail:
  336. return ret;
  337. }
  338. static struct file_system_type ipathfs_fs_type = {
  339. .owner = THIS_MODULE,
  340. .name = "ipathfs",
  341. .get_sb = ipathfs_get_sb,
  342. .kill_sb = ipathfs_kill_super,
  343. };
  344. int __init ipath_init_ipathfs(void)
  345. {
  346. return register_filesystem(&ipathfs_fs_type);
  347. }
  348. void __exit ipath_exit_ipathfs(void)
  349. {
  350. unregister_filesystem(&ipathfs_fs_type);
  351. }