fnic_debugfs.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. loff_t pos = -1;
  153. switch (howto) {
  154. case 0:
  155. pos = offset;
  156. break;
  157. case 1:
  158. pos = file->f_pos + offset;
  159. break;
  160. case 2:
  161. pos = fnic_dbg_prt->buffer_len - offset;
  162. }
  163. return (pos < 0 || pos > fnic_dbg_prt->buffer_len) ?
  164. -EINVAL : (file->f_pos = pos);
  165. }
  166. /*
  167. * fnic_trace_debugfs_read - Read a debugfs file
  168. * @file: The file pointer to read from.
  169. * @ubuf: The buffer to copy the data to.
  170. * @nbytes: The number of bytes to read.
  171. * @pos: The position in the file to start reading from.
  172. *
  173. * Description:
  174. * This routine reads data from the buffer indicated in the private_data
  175. * field of @file. It will start reading at @pos and copy up to @nbytes of
  176. * data to @ubuf.
  177. *
  178. * Returns:
  179. * This function returns the amount of data that was read (this could be
  180. * less than @nbytes if the end of the file was reached).
  181. */
  182. static ssize_t fnic_trace_debugfs_read(struct file *file,
  183. char __user *ubuf,
  184. size_t nbytes,
  185. loff_t *pos)
  186. {
  187. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  188. int rc = 0;
  189. rc = simple_read_from_buffer(ubuf, nbytes, pos,
  190. fnic_dbg_prt->buffer,
  191. fnic_dbg_prt->buffer_len);
  192. return rc;
  193. }
  194. /*
  195. * fnic_trace_debugfs_release - Release the buffer used to store
  196. * debugfs file data
  197. * @inode: The inode pointer
  198. * @file: The file pointer that contains the buffer to release
  199. *
  200. * Description:
  201. * This routine frees the buffer that was allocated when the debugfs
  202. * file was opened.
  203. *
  204. * Returns:
  205. * This function returns zero.
  206. */
  207. static int fnic_trace_debugfs_release(struct inode *inode,
  208. struct file *file)
  209. {
  210. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  211. vfree(fnic_dbg_prt->buffer);
  212. kfree(fnic_dbg_prt);
  213. return 0;
  214. }
  215. static const struct file_operations fnic_trace_ctrl_fops = {
  216. .owner = THIS_MODULE,
  217. .open = fnic_trace_ctrl_open,
  218. .read = fnic_trace_ctrl_read,
  219. .write = fnic_trace_ctrl_write,
  220. };
  221. static const struct file_operations fnic_trace_debugfs_fops = {
  222. .owner = THIS_MODULE,
  223. .open = fnic_trace_debugfs_open,
  224. .llseek = fnic_trace_debugfs_lseek,
  225. .read = fnic_trace_debugfs_read,
  226. .release = fnic_trace_debugfs_release,
  227. };
  228. /*
  229. * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
  230. *
  231. * Description:
  232. * When Debugfs is configured this routine sets up the fnic debugfs
  233. * file system. If not already created, this routine will create the
  234. * fnic directory. It will create file trace to log fnic trace buffer
  235. * output into debugfs and it will also create file trace_enable to
  236. * control enable/disable of trace logging into trace buffer.
  237. */
  238. int fnic_trace_debugfs_init(void)
  239. {
  240. int rc = -1;
  241. fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
  242. if (!fnic_trace_debugfs_root) {
  243. printk(KERN_DEBUG "Cannot create debugfs root\n");
  244. return rc;
  245. }
  246. fnic_trace_enable = debugfs_create_file("tracing_enable",
  247. S_IFREG|S_IRUGO|S_IWUSR,
  248. fnic_trace_debugfs_root,
  249. NULL, &fnic_trace_ctrl_fops);
  250. if (!fnic_trace_enable) {
  251. printk(KERN_DEBUG "Cannot create trace_enable file"
  252. " under debugfs");
  253. return rc;
  254. }
  255. fnic_trace_debugfs_file = debugfs_create_file("trace",
  256. S_IFREG|S_IRUGO|S_IWUSR,
  257. fnic_trace_debugfs_root,
  258. NULL,
  259. &fnic_trace_debugfs_fops);
  260. if (!fnic_trace_debugfs_file) {
  261. printk(KERN_DEBUG "Cannot create trace file under debugfs");
  262. return rc;
  263. }
  264. rc = 0;
  265. return rc;
  266. }
  267. /*
  268. * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
  269. *
  270. * Description:
  271. * When Debugfs is configured this routine removes debugfs file system
  272. * elements that are specific to fnic trace logging.
  273. */
  274. void fnic_trace_debugfs_terminate(void)
  275. {
  276. if (fnic_trace_debugfs_file) {
  277. debugfs_remove(fnic_trace_debugfs_file);
  278. fnic_trace_debugfs_file = NULL;
  279. }
  280. if (fnic_trace_enable) {
  281. debugfs_remove(fnic_trace_enable);
  282. fnic_trace_enable = NULL;
  283. }
  284. if (fnic_trace_debugfs_root) {
  285. debugfs_remove(fnic_trace_debugfs_root);
  286. fnic_trace_debugfs_root = NULL;
  287. }
  288. }