ipath_fs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/module.h>
  34. #include <linux/fs.h>
  35. #include <linux/mount.h>
  36. #include <linux/pagemap.h>
  37. #include <linux/init.h>
  38. #include <linux/namei.h>
  39. #include <linux/slab.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_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  55. inode->i_private = data;
  56. if ((mode & S_IFMT) == S_IFDIR) {
  57. inode->i_op = &simple_dir_inode_operations;
  58. inc_nlink(inode);
  59. inc_nlink(dir);
  60. }
  61. inode->i_fop = fops;
  62. d_instantiate(dentry, inode);
  63. error = 0;
  64. bail:
  65. return error;
  66. }
  67. static int create_file(const char *name, mode_t mode,
  68. struct dentry *parent, struct dentry **dentry,
  69. const struct file_operations *fops, void *data)
  70. {
  71. int error;
  72. *dentry = NULL;
  73. mutex_lock(&parent->d_inode->i_mutex);
  74. *dentry = lookup_one_len(name, parent, strlen(name));
  75. if (!IS_ERR(*dentry))
  76. error = ipathfs_mknod(parent->d_inode, *dentry,
  77. mode, fops, data);
  78. else
  79. error = PTR_ERR(dentry);
  80. mutex_unlock(&parent->d_inode->i_mutex);
  81. return error;
  82. }
  83. static ssize_t atomic_stats_read(struct file *file, char __user *buf,
  84. size_t count, loff_t *ppos)
  85. {
  86. return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
  87. sizeof ipath_stats);
  88. }
  89. static const struct file_operations atomic_stats_ops = {
  90. .read = atomic_stats_read,
  91. };
  92. static ssize_t atomic_counters_read(struct file *file, char __user *buf,
  93. size_t count, loff_t *ppos)
  94. {
  95. struct infinipath_counters counters;
  96. struct ipath_devdata *dd;
  97. dd = file->f_path.dentry->d_inode->i_private;
  98. dd->ipath_f_read_counters(dd, &counters);
  99. return simple_read_from_buffer(buf, count, ppos, &counters,
  100. sizeof counters);
  101. }
  102. static const struct file_operations atomic_counters_ops = {
  103. .read = atomic_counters_read,
  104. };
  105. static ssize_t flash_read(struct file *file, char __user *buf,
  106. size_t count, loff_t *ppos)
  107. {
  108. struct ipath_devdata *dd;
  109. ssize_t ret;
  110. loff_t pos;
  111. char *tmp;
  112. pos = *ppos;
  113. if ( pos < 0) {
  114. ret = -EINVAL;
  115. goto bail;
  116. }
  117. if (pos >= sizeof(struct ipath_flash)) {
  118. ret = 0;
  119. goto bail;
  120. }
  121. if (count > sizeof(struct ipath_flash) - pos)
  122. count = sizeof(struct ipath_flash) - pos;
  123. tmp = kmalloc(count, GFP_KERNEL);
  124. if (!tmp) {
  125. ret = -ENOMEM;
  126. goto bail;
  127. }
  128. dd = file->f_path.dentry->d_inode->i_private;
  129. if (ipath_eeprom_read(dd, pos, tmp, count)) {
  130. ipath_dev_err(dd, "failed to read from flash\n");
  131. ret = -ENXIO;
  132. goto bail_tmp;
  133. }
  134. if (copy_to_user(buf, tmp, count)) {
  135. ret = -EFAULT;
  136. goto bail_tmp;
  137. }
  138. *ppos = pos + count;
  139. ret = count;
  140. bail_tmp:
  141. kfree(tmp);
  142. bail:
  143. return ret;
  144. }
  145. static ssize_t flash_write(struct file *file, const char __user *buf,
  146. size_t count, loff_t *ppos)
  147. {
  148. struct ipath_devdata *dd;
  149. ssize_t ret;
  150. loff_t pos;
  151. char *tmp;
  152. pos = *ppos;
  153. if (pos != 0) {
  154. ret = -EINVAL;
  155. goto bail;
  156. }
  157. if (count != sizeof(struct ipath_flash)) {
  158. ret = -EINVAL;
  159. goto bail;
  160. }
  161. tmp = kmalloc(count, GFP_KERNEL);
  162. if (!tmp) {
  163. ret = -ENOMEM;
  164. goto bail;
  165. }
  166. if (copy_from_user(tmp, buf, count)) {
  167. ret = -EFAULT;
  168. goto bail_tmp;
  169. }
  170. dd = file->f_path.dentry->d_inode->i_private;
  171. if (ipath_eeprom_write(dd, pos, tmp, count)) {
  172. ret = -ENXIO;
  173. ipath_dev_err(dd, "failed to write to flash\n");
  174. goto bail_tmp;
  175. }
  176. *ppos = pos + count;
  177. ret = count;
  178. bail_tmp:
  179. kfree(tmp);
  180. bail:
  181. return ret;
  182. }
  183. static const struct file_operations flash_ops = {
  184. .read = flash_read,
  185. .write = flash_write,
  186. };
  187. static int create_device_files(struct super_block *sb,
  188. struct ipath_devdata *dd)
  189. {
  190. struct dentry *dir, *tmp;
  191. char unit[10];
  192. int ret;
  193. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  194. ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
  195. &simple_dir_operations, dd);
  196. if (ret) {
  197. printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
  198. goto bail;
  199. }
  200. ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
  201. &atomic_counters_ops, dd);
  202. if (ret) {
  203. printk(KERN_ERR "create_file(%s/atomic_counters) "
  204. "failed: %d\n", unit, ret);
  205. goto bail;
  206. }
  207. ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
  208. &flash_ops, dd);
  209. if (ret) {
  210. printk(KERN_ERR "create_file(%s/flash) "
  211. "failed: %d\n", unit, ret);
  212. goto bail;
  213. }
  214. bail:
  215. return ret;
  216. }
  217. static int remove_file(struct dentry *parent, char *name)
  218. {
  219. struct dentry *tmp;
  220. int ret;
  221. tmp = lookup_one_len(name, parent, strlen(name));
  222. if (IS_ERR(tmp)) {
  223. ret = PTR_ERR(tmp);
  224. goto bail;
  225. }
  226. spin_lock(&dcache_lock);
  227. spin_lock(&tmp->d_lock);
  228. if (!(d_unhashed(tmp) && tmp->d_inode)) {
  229. dget_locked(tmp);
  230. __d_drop(tmp);
  231. spin_unlock(&tmp->d_lock);
  232. spin_unlock(&dcache_lock);
  233. simple_unlink(parent->d_inode, tmp);
  234. } else {
  235. spin_unlock(&tmp->d_lock);
  236. spin_unlock(&dcache_lock);
  237. }
  238. ret = 0;
  239. bail:
  240. /*
  241. * We don't expect clients to care about the return value, but
  242. * it's there if they need it.
  243. */
  244. return ret;
  245. }
  246. static int remove_device_files(struct super_block *sb,
  247. struct ipath_devdata *dd)
  248. {
  249. struct dentry *dir, *root;
  250. char unit[10];
  251. int ret;
  252. root = dget(sb->s_root);
  253. mutex_lock(&root->d_inode->i_mutex);
  254. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  255. dir = lookup_one_len(unit, root, strlen(unit));
  256. if (IS_ERR(dir)) {
  257. ret = PTR_ERR(dir);
  258. printk(KERN_ERR "Lookup of %s failed\n", unit);
  259. goto bail;
  260. }
  261. remove_file(dir, "flash");
  262. remove_file(dir, "atomic_counters");
  263. d_delete(dir);
  264. ret = simple_rmdir(root->d_inode, dir);
  265. bail:
  266. mutex_unlock(&root->d_inode->i_mutex);
  267. dput(root);
  268. return ret;
  269. }
  270. static int ipathfs_fill_super(struct super_block *sb, void *data,
  271. int silent)
  272. {
  273. struct ipath_devdata *dd, *tmp;
  274. unsigned long flags;
  275. int ret;
  276. static struct tree_descr files[] = {
  277. [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
  278. {""},
  279. };
  280. ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
  281. if (ret) {
  282. printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
  283. goto bail;
  284. }
  285. spin_lock_irqsave(&ipath_devs_lock, flags);
  286. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  287. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  288. ret = create_device_files(sb, dd);
  289. if (ret)
  290. goto bail;
  291. spin_lock_irqsave(&ipath_devs_lock, flags);
  292. }
  293. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  294. bail:
  295. return ret;
  296. }
  297. static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
  298. const char *dev_name, void *data, struct vfsmount *mnt)
  299. {
  300. int ret = get_sb_single(fs_type, flags, data,
  301. ipathfs_fill_super, mnt);
  302. if (ret >= 0)
  303. ipath_super = mnt->mnt_sb;
  304. return ret;
  305. }
  306. static void ipathfs_kill_super(struct super_block *s)
  307. {
  308. kill_litter_super(s);
  309. ipath_super = NULL;
  310. }
  311. int ipathfs_add_device(struct ipath_devdata *dd)
  312. {
  313. int ret;
  314. if (ipath_super == NULL) {
  315. ret = 0;
  316. goto bail;
  317. }
  318. ret = create_device_files(ipath_super, dd);
  319. bail:
  320. return ret;
  321. }
  322. int ipathfs_remove_device(struct ipath_devdata *dd)
  323. {
  324. int ret;
  325. if (ipath_super == NULL) {
  326. ret = 0;
  327. goto bail;
  328. }
  329. ret = remove_device_files(ipath_super, dd);
  330. bail:
  331. return ret;
  332. }
  333. static struct file_system_type ipathfs_fs_type = {
  334. .owner = THIS_MODULE,
  335. .name = "ipathfs",
  336. .get_sb = ipathfs_get_sb,
  337. .kill_sb = ipathfs_kill_super,
  338. };
  339. int __init ipath_init_ipathfs(void)
  340. {
  341. return register_filesystem(&ipathfs_fs_type);
  342. }
  343. void __exit ipath_exit_ipathfs(void)
  344. {
  345. unregister_filesystem(&ipathfs_fs_type);
  346. }