ipath_fs.c 9.2 KB

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