/drivers/infiniband/ulp/ipoib/ipoib_fs.c

https://bitbucket.org/evzijst/gittest · C · 287 lines · 195 code · 58 blank · 34 comment · 16 complexity · 7f87ee338f78a9458dc451aea0ccaa74 MD5 · raw file

  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. * $Id: ipoib_fs.c 1389 2004-12-27 22:56:47Z roland $
  33. */
  34. #include <linux/pagemap.h>
  35. #include <linux/seq_file.h>
  36. #include "ipoib.h"
  37. enum {
  38. IPOIB_MAGIC = 0x49504942 /* "IPIB" */
  39. };
  40. static DECLARE_MUTEX(ipoib_fs_mutex);
  41. static struct dentry *ipoib_root;
  42. static struct super_block *ipoib_sb;
  43. static LIST_HEAD(ipoib_device_list);
  44. static void *ipoib_mcg_seq_start(struct seq_file *file, loff_t *pos)
  45. {
  46. struct ipoib_mcast_iter *iter;
  47. loff_t n = *pos;
  48. iter = ipoib_mcast_iter_init(file->private);
  49. if (!iter)
  50. return NULL;
  51. while (n--) {
  52. if (ipoib_mcast_iter_next(iter)) {
  53. ipoib_mcast_iter_free(iter);
  54. return NULL;
  55. }
  56. }
  57. return iter;
  58. }
  59. static void *ipoib_mcg_seq_next(struct seq_file *file, void *iter_ptr,
  60. loff_t *pos)
  61. {
  62. struct ipoib_mcast_iter *iter = iter_ptr;
  63. (*pos)++;
  64. if (ipoib_mcast_iter_next(iter)) {
  65. ipoib_mcast_iter_free(iter);
  66. return NULL;
  67. }
  68. return iter;
  69. }
  70. static void ipoib_mcg_seq_stop(struct seq_file *file, void *iter_ptr)
  71. {
  72. /* nothing for now */
  73. }
  74. static int ipoib_mcg_seq_show(struct seq_file *file, void *iter_ptr)
  75. {
  76. struct ipoib_mcast_iter *iter = iter_ptr;
  77. char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
  78. union ib_gid mgid;
  79. int i, n;
  80. unsigned long created;
  81. unsigned int queuelen, complete, send_only;
  82. if (iter) {
  83. ipoib_mcast_iter_read(iter, &mgid, &created, &queuelen,
  84. &complete, &send_only);
  85. for (n = 0, i = 0; i < sizeof mgid / 2; ++i) {
  86. n += sprintf(gid_buf + n, "%x",
  87. be16_to_cpu(((u16 *)mgid.raw)[i]));
  88. if (i < sizeof mgid / 2 - 1)
  89. gid_buf[n++] = ':';
  90. }
  91. }
  92. seq_printf(file, "GID: %*s", -(1 + (int) sizeof gid_buf), gid_buf);
  93. seq_printf(file,
  94. " created: %10ld queuelen: %4d complete: %d send_only: %d\n",
  95. created, queuelen, complete, send_only);
  96. return 0;
  97. }
  98. static struct seq_operations ipoib_seq_ops = {
  99. .start = ipoib_mcg_seq_start,
  100. .next = ipoib_mcg_seq_next,
  101. .stop = ipoib_mcg_seq_stop,
  102. .show = ipoib_mcg_seq_show,
  103. };
  104. static int ipoib_mcg_open(struct inode *inode, struct file *file)
  105. {
  106. struct seq_file *seq;
  107. int ret;
  108. ret = seq_open(file, &ipoib_seq_ops);
  109. if (ret)
  110. return ret;
  111. seq = file->private_data;
  112. seq->private = inode->u.generic_ip;
  113. return 0;
  114. }
  115. static struct file_operations ipoib_fops = {
  116. .owner = THIS_MODULE,
  117. .open = ipoib_mcg_open,
  118. .read = seq_read,
  119. .llseek = seq_lseek,
  120. .release = seq_release
  121. };
  122. static struct inode *ipoib_get_inode(void)
  123. {
  124. struct inode *inode = new_inode(ipoib_sb);
  125. if (inode) {
  126. inode->i_mode = S_IFREG | S_IRUGO;
  127. inode->i_uid = 0;
  128. inode->i_gid = 0;
  129. inode->i_blksize = PAGE_CACHE_SIZE;
  130. inode->i_blocks = 0;
  131. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  132. inode->i_fop = &ipoib_fops;
  133. }
  134. return inode;
  135. }
  136. static int __ipoib_create_debug_file(struct net_device *dev)
  137. {
  138. struct ipoib_dev_priv *priv = netdev_priv(dev);
  139. struct dentry *dentry;
  140. struct inode *inode;
  141. char name[IFNAMSIZ + sizeof "_mcg"];
  142. snprintf(name, sizeof name, "%s_mcg", dev->name);
  143. dentry = d_alloc_name(ipoib_root, name);
  144. if (!dentry)
  145. return -ENOMEM;
  146. inode = ipoib_get_inode();
  147. if (!inode) {
  148. dput(dentry);
  149. return -ENOMEM;
  150. }
  151. inode->u.generic_ip = dev;
  152. priv->mcg_dentry = dentry;
  153. d_add(dentry, inode);
  154. return 0;
  155. }
  156. int ipoib_create_debug_file(struct net_device *dev)
  157. {
  158. struct ipoib_dev_priv *priv = netdev_priv(dev);
  159. down(&ipoib_fs_mutex);
  160. list_add_tail(&priv->fs_list, &ipoib_device_list);
  161. if (!ipoib_sb) {
  162. up(&ipoib_fs_mutex);
  163. return 0;
  164. }
  165. up(&ipoib_fs_mutex);
  166. return __ipoib_create_debug_file(dev);
  167. }
  168. void ipoib_delete_debug_file(struct net_device *dev)
  169. {
  170. struct ipoib_dev_priv *priv = netdev_priv(dev);
  171. down(&ipoib_fs_mutex);
  172. list_del(&priv->fs_list);
  173. if (!ipoib_sb) {
  174. up(&ipoib_fs_mutex);
  175. return;
  176. }
  177. up(&ipoib_fs_mutex);
  178. if (priv->mcg_dentry) {
  179. d_drop(priv->mcg_dentry);
  180. simple_unlink(ipoib_root->d_inode, priv->mcg_dentry);
  181. }
  182. }
  183. static int ipoib_fill_super(struct super_block *sb, void *data, int silent)
  184. {
  185. static struct tree_descr ipoib_files[] = {
  186. { "" }
  187. };
  188. struct ipoib_dev_priv *priv;
  189. int ret;
  190. ret = simple_fill_super(sb, IPOIB_MAGIC, ipoib_files);
  191. if (ret)
  192. return ret;
  193. ipoib_root = sb->s_root;
  194. down(&ipoib_fs_mutex);
  195. ipoib_sb = sb;
  196. list_for_each_entry(priv, &ipoib_device_list, fs_list) {
  197. ret = __ipoib_create_debug_file(priv->dev);
  198. if (ret)
  199. break;
  200. }
  201. up(&ipoib_fs_mutex);
  202. return ret;
  203. }
  204. static struct super_block *ipoib_get_sb(struct file_system_type *fs_type,
  205. int flags, const char *dev_name, void *data)
  206. {
  207. return get_sb_single(fs_type, flags, data, ipoib_fill_super);
  208. }
  209. static void ipoib_kill_sb(struct super_block *sb)
  210. {
  211. down(&ipoib_fs_mutex);
  212. ipoib_sb = NULL;
  213. up(&ipoib_fs_mutex);
  214. kill_litter_super(sb);
  215. }
  216. static struct file_system_type ipoib_fs_type = {
  217. .owner = THIS_MODULE,
  218. .name = "ipoib_debugfs",
  219. .get_sb = ipoib_get_sb,
  220. .kill_sb = ipoib_kill_sb,
  221. };
  222. int ipoib_register_debugfs(void)
  223. {
  224. return register_filesystem(&ipoib_fs_type);
  225. }
  226. void ipoib_unregister_debugfs(void)
  227. {
  228. unregister_filesystem(&ipoib_fs_type);
  229. }