fnic_debugfs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright 2012 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/debugfs.h>
  20. #include "fnic.h"
  21. static struct dentry *fnic_trace_debugfs_root;
  22. static struct dentry *fnic_trace_debugfs_file;
  23. static struct dentry *fnic_trace_enable;
  24. /*
  25. * fnic_trace_ctrl_open - Open the trace_enable file
  26. * @inode: The inode pointer.
  27. * @file: The file pointer to attach the trace enable/disable flag.
  28. *
  29. * Description:
  30. * This routine opens a debugsfs file trace_enable.
  31. *
  32. * Returns:
  33. * This function returns zero if successful.
  34. */
  35. static int fnic_trace_ctrl_open(struct inode *inode, struct file *filp)
  36. {
  37. filp->private_data = inode->i_private;
  38. return 0;
  39. }
  40. /*
  41. * fnic_trace_ctrl_read - Read a trace_enable debugfs file
  42. * @filp: The file pointer to read from.
  43. * @ubuf: The buffer to copy the data to.
  44. * @cnt: The number of bytes to read.
  45. * @ppos: The position in the file to start reading from.
  46. *
  47. * Description:
  48. * This routine reads value of variable fnic_tracing_enabled
  49. * and stores into local @buf. It will start reading file at @ppos and
  50. * copy up to @cnt of data to @ubuf from @buf.
  51. *
  52. * Returns:
  53. * This function returns the amount of data that was read.
  54. */
  55. static ssize_t fnic_trace_ctrl_read(struct file *filp,
  56. char __user *ubuf,
  57. size_t cnt, loff_t *ppos)
  58. {
  59. char buf[64];
  60. int len;
  61. len = sprintf(buf, "%u\n", fnic_tracing_enabled);
  62. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  63. }
  64. /*
  65. * fnic_trace_ctrl_write - Write to trace_enable debugfs file
  66. * @filp: The file pointer to write from.
  67. * @ubuf: The buffer to copy the data from.
  68. * @cnt: The number of bytes to write.
  69. * @ppos: The position in the file to start writing to.
  70. *
  71. * Description:
  72. * This routine writes data from user buffer @ubuf to buffer @buf and
  73. * sets fnic_tracing_enabled value as per user input.
  74. *
  75. * Returns:
  76. * This function returns the amount of data that was written.
  77. */
  78. static ssize_t fnic_trace_ctrl_write(struct file *filp,
  79. const char __user *ubuf,
  80. size_t cnt, loff_t *ppos)
  81. {
  82. char buf[64];
  83. unsigned long val;
  84. int ret;
  85. if (cnt >= sizeof(buf))
  86. return -EINVAL;
  87. if (copy_from_user(&buf, ubuf, cnt))
  88. return -EFAULT;
  89. buf[cnt] = 0;
  90. ret = kstrtoul(buf, 10, &val);
  91. if (ret < 0)
  92. return ret;
  93. fnic_tracing_enabled = val;
  94. (*ppos)++;
  95. return cnt;
  96. }
  97. /*
  98. * fnic_trace_debugfs_open - Open the fnic trace log
  99. * @inode: The inode pointer
  100. * @file: The file pointer to attach the log output
  101. *
  102. * Description:
  103. * This routine is the entry point for the debugfs open file operation.
  104. * It allocates the necessary buffer for the log, fills the buffer from
  105. * the in-memory log and then returns a pointer to that log in
  106. * the private_data field in @file.
  107. *
  108. * Returns:
  109. * This function returns zero if successful. On error it will return
  110. * a negative error value.
  111. */
  112. static int fnic_trace_debugfs_open(struct inode *inode,
  113. struct file *file)
  114. {
  115. fnic_dbgfs_t *fnic_dbg_prt;
  116. fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
  117. if (!fnic_dbg_prt)
  118. return -ENOMEM;
  119. fnic_dbg_prt->buffer = vmalloc((3*(trace_max_pages * PAGE_SIZE)));
  120. if (!fnic_dbg_prt->buffer) {
  121. kfree(fnic_dbg_prt);
  122. return -ENOMEM;
  123. }
  124. memset((void *)fnic_dbg_prt->buffer, 0,
  125. (3*(trace_max_pages * PAGE_SIZE)));
  126. fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
  127. file->private_data = fnic_dbg_prt;
  128. return 0;
  129. }
  130. /*
  131. * fnic_trace_debugfs_lseek - Seek through a debugfs file
  132. * @file: The file pointer to seek through.
  133. * @offset: The offset to seek to or the amount to seek by.
  134. * @howto: Indicates how to seek.
  135. *
  136. * Description:
  137. * This routine is the entry point for the debugfs lseek file operation.
  138. * The @howto parameter indicates whether @offset is the offset to directly
  139. * seek to, or if it is a value to seek forward or reverse by. This function
  140. * figures out what the new offset of the debugfs file will be and assigns
  141. * that value to the f_pos field of @file.
  142. *
  143. * Returns:
  144. * This function returns the new offset if successful and returns a negative
  145. * error if unable to process the seek.
  146. */
  147. static loff_t fnic_trace_debugfs_lseek(struct file *file,
  148. loff_t offset,
  149. int howto)
  150. {
  151. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  152. return fixed_size_llseek(file, offset, howto,
  153. fnic_dbg_prt->buffer_len);
  154. }
  155. /*
  156. * fnic_trace_debugfs_read - Read a debugfs file
  157. * @file: The file pointer to read from.
  158. * @ubuf: The buffer to copy the data to.
  159. * @nbytes: The number of bytes to read.
  160. * @pos: The position in the file to start reading from.
  161. *
  162. * Description:
  163. * This routine reads data from the buffer indicated in the private_data
  164. * field of @file. It will start reading at @pos and copy up to @nbytes of
  165. * data to @ubuf.
  166. *
  167. * Returns:
  168. * This function returns the amount of data that was read (this could be
  169. * less than @nbytes if the end of the file was reached).
  170. */
  171. static ssize_t fnic_trace_debugfs_read(struct file *file,
  172. char __user *ubuf,
  173. size_t nbytes,
  174. loff_t *pos)
  175. {
  176. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  177. int rc = 0;
  178. rc = simple_read_from_buffer(ubuf, nbytes, pos,
  179. fnic_dbg_prt->buffer,
  180. fnic_dbg_prt->buffer_len);
  181. return rc;
  182. }
  183. /*
  184. * fnic_trace_debugfs_release - Release the buffer used to store
  185. * debugfs file data
  186. * @inode: The inode pointer
  187. * @file: The file pointer that contains the buffer to release
  188. *
  189. * Description:
  190. * This routine frees the buffer that was allocated when the debugfs
  191. * file was opened.
  192. *
  193. * Returns:
  194. * This function returns zero.
  195. */
  196. static int fnic_trace_debugfs_release(struct inode *inode,
  197. struct file *file)
  198. {
  199. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  200. vfree(fnic_dbg_prt->buffer);
  201. kfree(fnic_dbg_prt);
  202. return 0;
  203. }
  204. static const struct file_operations fnic_trace_ctrl_fops = {
  205. .owner = THIS_MODULE,
  206. .open = fnic_trace_ctrl_open,
  207. .read = fnic_trace_ctrl_read,
  208. .write = fnic_trace_ctrl_write,
  209. };
  210. static const struct file_operations fnic_trace_debugfs_fops = {
  211. .owner = THIS_MODULE,
  212. .open = fnic_trace_debugfs_open,
  213. .llseek = fnic_trace_debugfs_lseek,
  214. .read = fnic_trace_debugfs_read,
  215. .release = fnic_trace_debugfs_release,
  216. };
  217. /*
  218. * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
  219. *
  220. * Description:
  221. * When Debugfs is configured this routine sets up the fnic debugfs
  222. * file system. If not already created, this routine will create the
  223. * fnic directory. It will create file trace to log fnic trace buffer
  224. * output into debugfs and it will also create file trace_enable to
  225. * control enable/disable of trace logging into trace buffer.
  226. */
  227. int fnic_trace_debugfs_init(void)
  228. {
  229. int rc = -1;
  230. fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
  231. if (!fnic_trace_debugfs_root) {
  232. printk(KERN_DEBUG "Cannot create debugfs root\n");
  233. return rc;
  234. }
  235. fnic_trace_enable = debugfs_create_file("tracing_enable",
  236. S_IFREG|S_IRUGO|S_IWUSR,
  237. fnic_trace_debugfs_root,
  238. NULL, &fnic_trace_ctrl_fops);
  239. if (!fnic_trace_enable) {
  240. printk(KERN_DEBUG "Cannot create trace_enable file"
  241. " under debugfs");
  242. return rc;
  243. }
  244. fnic_trace_debugfs_file = debugfs_create_file("trace",
  245. S_IFREG|S_IRUGO|S_IWUSR,
  246. fnic_trace_debugfs_root,
  247. NULL,
  248. &fnic_trace_debugfs_fops);
  249. if (!fnic_trace_debugfs_file) {
  250. printk(KERN_DEBUG "Cannot create trace file under debugfs");
  251. return rc;
  252. }
  253. rc = 0;
  254. return rc;
  255. }
  256. /*
  257. * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
  258. *
  259. * Description:
  260. * When Debugfs is configured this routine removes debugfs file system
  261. * elements that are specific to fnic trace logging.
  262. */
  263. void fnic_trace_debugfs_terminate(void)
  264. {
  265. if (fnic_trace_debugfs_file) {
  266. debugfs_remove(fnic_trace_debugfs_file);
  267. fnic_trace_debugfs_file = NULL;
  268. }
  269. if (fnic_trace_enable) {
  270. debugfs_remove(fnic_trace_enable);
  271. fnic_trace_enable = NULL;
  272. }
  273. if (fnic_trace_debugfs_root) {
  274. debugfs_remove(fnic_trace_debugfs_root);
  275. fnic_trace_debugfs_root = NULL;
  276. }
  277. }