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. umode_t 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_ino = get_next_ino();
  54. inode->i_mode = mode;
  55. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  56. inode->i_private = data;
  57. if (S_ISDIR(mode)) {
  58. inode->i_op = &simple_dir_inode_operations;
  59. inc_nlink(inode);
  60. inc_nlink(dir);
  61. }
  62. inode->i_fop = fops;
  63. d_instantiate(dentry, inode);
  64. error = 0;
  65. bail:
  66. return error;
  67. }
  68. static int create_file(const char *name, umode_t mode,
  69. struct dentry *parent, struct dentry **dentry,
  70. const struct file_operations *fops, void *data)
  71. {
  72. int error;
  73. mutex_lock(&d_inode(parent)->i_mutex);
  74. *dentry = lookup_one_len(name, parent, strlen(name));
  75. if (!IS_ERR(*dentry))
  76. error = ipathfs_mknod(d_inode(parent), *dentry,
  77. mode, fops, data);
  78. else
  79. error = PTR_ERR(*dentry);
  80. mutex_unlock(&d_inode(parent)->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. .llseek = default_llseek,
  92. };
  93. static ssize_t atomic_counters_read(struct file *file, char __user *buf,
  94. size_t count, loff_t *ppos)
  95. {
  96. struct infinipath_counters counters;
  97. struct ipath_devdata *dd;
  98. dd = file_inode(file)->i_private;
  99. dd->ipath_f_read_counters(dd, &counters);
  100. return simple_read_from_buffer(buf, count, ppos, &counters,
  101. sizeof counters);
  102. }
  103. static const struct file_operations atomic_counters_ops = {
  104. .read = atomic_counters_read,
  105. .llseek = default_llseek,
  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_inode(file)->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_inode(file)->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. .llseek = default_llseek,
  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(&tmp->d_lock);
  230. if (!d_unhashed(tmp) && d_really_is_positive(tmp)) {
  231. dget_dlock(tmp);
  232. __d_drop(tmp);
  233. spin_unlock(&tmp->d_lock);
  234. simple_unlink(d_inode(parent), tmp);
  235. } else
  236. spin_unlock(&tmp->d_lock);
  237. ret = 0;
  238. bail:
  239. /*
  240. * We don't expect clients to care about the return value, but
  241. * it's there if they need it.
  242. */
  243. return ret;
  244. }
  245. static int remove_device_files(struct super_block *sb,
  246. struct ipath_devdata *dd)
  247. {
  248. struct dentry *dir, *root;
  249. char unit[10];
  250. int ret;
  251. root = dget(sb->s_root);
  252. mutex_lock(&d_inode(root)->i_mutex);
  253. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  254. dir = lookup_one_len(unit, root, strlen(unit));
  255. if (IS_ERR(dir)) {
  256. ret = PTR_ERR(dir);
  257. printk(KERN_ERR "Lookup of %s failed\n", unit);
  258. goto bail;
  259. }
  260. remove_file(dir, "flash");
  261. remove_file(dir, "atomic_counters");
  262. d_delete(dir);
  263. ret = simple_rmdir(d_inode(root), dir);
  264. bail:
  265. mutex_unlock(&d_inode(root)->i_mutex);
  266. dput(root);
  267. return ret;
  268. }
  269. static int ipathfs_fill_super(struct super_block *sb, void *data,
  270. int silent)
  271. {
  272. struct ipath_devdata *dd, *tmp;
  273. unsigned long flags;
  274. int ret;
  275. static struct tree_descr files[] = {
  276. [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
  277. {""},
  278. };
  279. ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
  280. if (ret) {
  281. printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
  282. goto bail;
  283. }
  284. spin_lock_irqsave(&ipath_devs_lock, flags);
  285. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  286. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  287. ret = create_device_files(sb, dd);
  288. if (ret)
  289. goto bail;
  290. spin_lock_irqsave(&ipath_devs_lock, flags);
  291. }
  292. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  293. bail:
  294. return ret;
  295. }
  296. static struct dentry *ipathfs_mount(struct file_system_type *fs_type,
  297. int flags, const char *dev_name, void *data)
  298. {
  299. struct dentry *ret;
  300. ret = mount_single(fs_type, flags, data, ipathfs_fill_super);
  301. if (!IS_ERR(ret))
  302. ipath_super = ret->d_sb;
  303. return ret;
  304. }
  305. static void ipathfs_kill_super(struct super_block *s)
  306. {
  307. kill_litter_super(s);
  308. ipath_super = NULL;
  309. }
  310. int ipathfs_add_device(struct ipath_devdata *dd)
  311. {
  312. int ret;
  313. if (ipath_super == NULL) {
  314. ret = 0;
  315. goto bail;
  316. }
  317. ret = create_device_files(ipath_super, dd);
  318. bail:
  319. return ret;
  320. }
  321. int ipathfs_remove_device(struct ipath_devdata *dd)
  322. {
  323. int ret;
  324. if (ipath_super == NULL) {
  325. ret = 0;
  326. goto bail;
  327. }
  328. ret = remove_device_files(ipath_super, dd);
  329. bail:
  330. return ret;
  331. }
  332. static struct file_system_type ipathfs_fs_type = {
  333. .owner = THIS_MODULE,
  334. .name = "ipathfs",
  335. .mount = ipathfs_mount,
  336. .kill_sb = ipathfs_kill_super,
  337. };
  338. MODULE_ALIAS_FS("ipathfs");
  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. }