ipath_fs.c 9.0 KB

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