orangefs-debugfs.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * What: /sys/kernel/debug/orangefs/debug-help
  4. * Date: June 2015
  5. * Contact: Mike Marshall <hubcap@omnibond.com>
  6. * Description:
  7. * List of client and kernel debug keywords.
  8. *
  9. *
  10. * What: /sys/kernel/debug/orangefs/client-debug
  11. * Date: June 2015
  12. * Contact: Mike Marshall <hubcap@omnibond.com>
  13. * Description:
  14. * Debug setting for "the client", the userspace
  15. * helper for the kernel module.
  16. *
  17. *
  18. * What: /sys/kernel/debug/orangefs/kernel-debug
  19. * Date: June 2015
  20. * Contact: Mike Marshall <hubcap@omnibond.com>
  21. * Description:
  22. * Debug setting for the orangefs kernel module.
  23. *
  24. * Any of the keywords, or comma-separated lists
  25. * of keywords, from debug-help can be catted to
  26. * client-debug or kernel-debug.
  27. *
  28. * "none", "all" and "verbose" are special keywords
  29. * for client-debug. Setting client-debug to "all"
  30. * is kind of like trying to drink water from a
  31. * fire hose, "verbose" triggers most of the same
  32. * output except for the constant flow of output
  33. * from the main wait loop.
  34. *
  35. * "none" and "all" are similar settings for kernel-debug
  36. * no need for a "verbose".
  37. */
  38. #include <linux/debugfs.h>
  39. #include <linux/slab.h>
  40. #include <linux/uaccess.h>
  41. #include "orangefs-debugfs.h"
  42. #include "protocol.h"
  43. #include "orangefs-kernel.h"
  44. #define DEBUG_HELP_STRING_SIZE 4096
  45. #define HELP_STRING_UNINITIALIZED \
  46. "Client Debug Keywords are unknown until the first time\n" \
  47. "the client is started after boot.\n"
  48. #define ORANGEFS_KMOD_DEBUG_HELP_FILE "debug-help"
  49. #define ORANGEFS_KMOD_DEBUG_FILE "kernel-debug"
  50. #define ORANGEFS_CLIENT_DEBUG_FILE "client-debug"
  51. #define ORANGEFS_VERBOSE "verbose"
  52. #define ORANGEFS_ALL "all"
  53. /*
  54. * An array of client_debug_mask will be built to hold debug keyword/mask
  55. * values fetched from userspace.
  56. */
  57. struct client_debug_mask {
  58. char *keyword;
  59. __u64 mask1;
  60. __u64 mask2;
  61. };
  62. static int orangefs_kernel_debug_init(void);
  63. static int orangefs_debug_help_open(struct inode *, struct file *);
  64. static void *help_start(struct seq_file *, loff_t *);
  65. static void *help_next(struct seq_file *, void *, loff_t *);
  66. static void help_stop(struct seq_file *, void *);
  67. static int help_show(struct seq_file *, void *);
  68. static int orangefs_debug_open(struct inode *, struct file *);
  69. static ssize_t orangefs_debug_read(struct file *,
  70. char __user *,
  71. size_t,
  72. loff_t *);
  73. static ssize_t orangefs_debug_write(struct file *,
  74. const char __user *,
  75. size_t,
  76. loff_t *);
  77. static int orangefs_prepare_cdm_array(char *);
  78. static void debug_mask_to_string(void *, int);
  79. static void do_k_string(void *, int);
  80. static void do_c_string(void *, int);
  81. static int keyword_is_amalgam(char *);
  82. static int check_amalgam_keyword(void *, int);
  83. static void debug_string_to_mask(char *, void *, int);
  84. static void do_c_mask(int, char *, struct client_debug_mask **);
  85. static void do_k_mask(int, char *, __u64 **);
  86. static char kernel_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN] = "none";
  87. static char *debug_help_string;
  88. static char client_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
  89. static char client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
  90. static struct dentry *help_file_dentry;
  91. static struct dentry *client_debug_dentry;
  92. static struct dentry *debug_dir;
  93. static unsigned int kernel_mask_set_mod_init;
  94. static int orangefs_debug_disabled = 1;
  95. static int help_string_initialized;
  96. static const struct seq_operations help_debug_ops = {
  97. .start = help_start,
  98. .next = help_next,
  99. .stop = help_stop,
  100. .show = help_show,
  101. };
  102. static const struct file_operations debug_help_fops = {
  103. .owner = THIS_MODULE,
  104. .open = orangefs_debug_help_open,
  105. .read = seq_read,
  106. .release = seq_release,
  107. .llseek = seq_lseek,
  108. };
  109. static const struct file_operations kernel_debug_fops = {
  110. .owner = THIS_MODULE,
  111. .open = orangefs_debug_open,
  112. .read = orangefs_debug_read,
  113. .write = orangefs_debug_write,
  114. .llseek = generic_file_llseek,
  115. };
  116. static int client_all_index;
  117. static int client_verbose_index;
  118. static struct client_debug_mask *cdm_array;
  119. static int cdm_element_count;
  120. static struct client_debug_mask client_debug_mask;
  121. /*
  122. * Used to protect data in ORANGEFS_KMOD_DEBUG_FILE and
  123. * ORANGEFS_KMOD_DEBUG_FILE.
  124. */
  125. static DEFINE_MUTEX(orangefs_debug_lock);
  126. /* Used to protect data in ORANGEFS_KMOD_DEBUG_HELP_FILE */
  127. static DEFINE_MUTEX(orangefs_help_file_lock);
  128. /*
  129. * initialize kmod debug operations, create orangefs debugfs dir and
  130. * ORANGEFS_KMOD_DEBUG_HELP_FILE.
  131. */
  132. int orangefs_debugfs_init(int debug_mask)
  133. {
  134. int rc = -ENOMEM;
  135. /* convert input debug mask to a 64-bit unsigned integer */
  136. orangefs_gossip_debug_mask = (unsigned long long)debug_mask;
  137. /*
  138. * set the kernel's gossip debug string; invalid mask values will
  139. * be ignored.
  140. */
  141. debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
  142. /* remove any invalid values from the mask */
  143. debug_string_to_mask(kernel_debug_string, &orangefs_gossip_debug_mask,
  144. 0);
  145. /*
  146. * if the mask has a non-zero value, then indicate that the mask
  147. * was set when the kernel module was loaded. The orangefs dev ioctl
  148. * command will look at this boolean to determine if the kernel's
  149. * debug mask should be overwritten when the client-core is started.
  150. */
  151. if (orangefs_gossip_debug_mask != 0)
  152. kernel_mask_set_mod_init = true;
  153. pr_info("%s: called with debug mask: :%s: :%llx:\n",
  154. __func__,
  155. kernel_debug_string,
  156. (unsigned long long)orangefs_gossip_debug_mask);
  157. debug_dir = debugfs_create_dir("orangefs", NULL);
  158. if (!debug_dir) {
  159. pr_info("%s: debugfs_create_dir failed.\n", __func__);
  160. goto out;
  161. }
  162. help_file_dentry = debugfs_create_file(ORANGEFS_KMOD_DEBUG_HELP_FILE,
  163. 0444,
  164. debug_dir,
  165. debug_help_string,
  166. &debug_help_fops);
  167. if (!help_file_dentry) {
  168. pr_info("%s: debugfs_create_file failed.\n", __func__);
  169. goto out;
  170. }
  171. orangefs_debug_disabled = 0;
  172. rc = orangefs_kernel_debug_init();
  173. out:
  174. return rc;
  175. }
  176. /*
  177. * initialize the kernel-debug file.
  178. */
  179. static int orangefs_kernel_debug_init(void)
  180. {
  181. int rc = -ENOMEM;
  182. struct dentry *ret;
  183. char *k_buffer = NULL;
  184. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
  185. k_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  186. if (!k_buffer)
  187. goto out;
  188. if (strlen(kernel_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
  189. strcpy(k_buffer, kernel_debug_string);
  190. strcat(k_buffer, "\n");
  191. } else {
  192. strcpy(k_buffer, "none\n");
  193. pr_info("%s: overflow 1!\n", __func__);
  194. }
  195. ret = debugfs_create_file(ORANGEFS_KMOD_DEBUG_FILE,
  196. 0444,
  197. debug_dir,
  198. k_buffer,
  199. &kernel_debug_fops);
  200. if (!ret) {
  201. pr_info("%s: failed to create %s.\n",
  202. __func__,
  203. ORANGEFS_KMOD_DEBUG_FILE);
  204. goto out;
  205. }
  206. rc = 0;
  207. out:
  208. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  209. return rc;
  210. }
  211. void orangefs_debugfs_cleanup(void)
  212. {
  213. debugfs_remove_recursive(debug_dir);
  214. }
  215. /* open ORANGEFS_KMOD_DEBUG_HELP_FILE */
  216. static int orangefs_debug_help_open(struct inode *inode, struct file *file)
  217. {
  218. int rc = -ENODEV;
  219. int ret;
  220. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  221. "orangefs_debug_help_open: start\n");
  222. if (orangefs_debug_disabled)
  223. goto out;
  224. ret = seq_open(file, &help_debug_ops);
  225. if (ret)
  226. goto out;
  227. ((struct seq_file *)(file->private_data))->private = inode->i_private;
  228. rc = 0;
  229. out:
  230. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  231. "orangefs_debug_help_open: rc:%d:\n",
  232. rc);
  233. return rc;
  234. }
  235. /*
  236. * I think start always gets called again after stop. Start
  237. * needs to return NULL when it is done. The whole "payload"
  238. * in this case is a single (long) string, so by the second
  239. * time we get to start (pos = 1), we're done.
  240. */
  241. static void *help_start(struct seq_file *m, loff_t *pos)
  242. {
  243. void *payload = NULL;
  244. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_start: start\n");
  245. mutex_lock(&orangefs_help_file_lock);
  246. if (*pos == 0)
  247. payload = m->private;
  248. return payload;
  249. }
  250. static void *help_next(struct seq_file *m, void *v, loff_t *pos)
  251. {
  252. (*pos)++;
  253. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
  254. return NULL;
  255. }
  256. static void help_stop(struct seq_file *m, void *p)
  257. {
  258. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_stop: start\n");
  259. mutex_unlock(&orangefs_help_file_lock);
  260. }
  261. static int help_show(struct seq_file *m, void *v)
  262. {
  263. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_show: start\n");
  264. seq_puts(m, v);
  265. return 0;
  266. }
  267. /*
  268. * initialize the client-debug file.
  269. */
  270. static int orangefs_client_debug_init(void)
  271. {
  272. int rc = -ENOMEM;
  273. char *c_buffer = NULL;
  274. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
  275. c_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  276. if (!c_buffer)
  277. goto out;
  278. if (strlen(client_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
  279. strcpy(c_buffer, client_debug_string);
  280. strcat(c_buffer, "\n");
  281. } else {
  282. strcpy(c_buffer, "none\n");
  283. pr_info("%s: overflow! 2\n", __func__);
  284. }
  285. client_debug_dentry = debugfs_create_file(ORANGEFS_CLIENT_DEBUG_FILE,
  286. 0444,
  287. debug_dir,
  288. c_buffer,
  289. &kernel_debug_fops);
  290. if (!client_debug_dentry) {
  291. pr_info("%s: failed to create updated %s.\n",
  292. __func__,
  293. ORANGEFS_CLIENT_DEBUG_FILE);
  294. goto out;
  295. }
  296. rc = 0;
  297. out:
  298. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  299. return rc;
  300. }
  301. /* open ORANGEFS_KMOD_DEBUG_FILE or ORANGEFS_CLIENT_DEBUG_FILE.*/
  302. static int orangefs_debug_open(struct inode *inode, struct file *file)
  303. {
  304. int rc = -ENODEV;
  305. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  306. "%s: orangefs_debug_disabled: %d\n",
  307. __func__,
  308. orangefs_debug_disabled);
  309. if (orangefs_debug_disabled)
  310. goto out;
  311. rc = 0;
  312. mutex_lock(&orangefs_debug_lock);
  313. file->private_data = inode->i_private;
  314. mutex_unlock(&orangefs_debug_lock);
  315. out:
  316. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  317. "orangefs_debug_open: rc: %d\n",
  318. rc);
  319. return rc;
  320. }
  321. static ssize_t orangefs_debug_read(struct file *file,
  322. char __user *ubuf,
  323. size_t count,
  324. loff_t *ppos)
  325. {
  326. char *buf;
  327. int sprintf_ret;
  328. ssize_t read_ret = -ENOMEM;
  329. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "orangefs_debug_read: start\n");
  330. buf = kmalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  331. if (!buf)
  332. goto out;
  333. mutex_lock(&orangefs_debug_lock);
  334. sprintf_ret = sprintf(buf, "%s", (char *)file->private_data);
  335. mutex_unlock(&orangefs_debug_lock);
  336. read_ret = simple_read_from_buffer(ubuf, count, ppos, buf, sprintf_ret);
  337. kfree(buf);
  338. out:
  339. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  340. "orangefs_debug_read: ret: %zu\n",
  341. read_ret);
  342. return read_ret;
  343. }
  344. static ssize_t orangefs_debug_write(struct file *file,
  345. const char __user *ubuf,
  346. size_t count,
  347. loff_t *ppos)
  348. {
  349. char *buf;
  350. int rc = -EFAULT;
  351. size_t silly = 0;
  352. char *debug_string;
  353. struct orangefs_kernel_op_s *new_op = NULL;
  354. struct client_debug_mask c_mask = { NULL, 0, 0 };
  355. char *s;
  356. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  357. "orangefs_debug_write: %pD\n",
  358. file);
  359. if (count == 0)
  360. return 0;
  361. /*
  362. * Thwart users who try to jamb a ridiculous number
  363. * of bytes into the debug file...
  364. */
  365. if (count > ORANGEFS_MAX_DEBUG_STRING_LEN + 1) {
  366. silly = count;
  367. count = ORANGEFS_MAX_DEBUG_STRING_LEN + 1;
  368. }
  369. buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  370. if (!buf)
  371. goto out;
  372. if (copy_from_user(buf, ubuf, count - 1)) {
  373. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  374. "%s: copy_from_user failed!\n",
  375. __func__);
  376. goto out;
  377. }
  378. /*
  379. * Map the keyword string from userspace into a valid debug mask.
  380. * The mapping process involves mapping the human-inputted string
  381. * into a valid mask, and then rebuilding the string from the
  382. * verified valid mask.
  383. *
  384. * A service operation is required to set a new client-side
  385. * debug mask.
  386. */
  387. if (!strcmp(file->f_path.dentry->d_name.name,
  388. ORANGEFS_KMOD_DEBUG_FILE)) {
  389. debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
  390. debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
  391. debug_string = kernel_debug_string;
  392. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  393. "New kernel debug string is %s\n",
  394. kernel_debug_string);
  395. } else {
  396. /* Can't reset client debug mask if client is not running. */
  397. if (is_daemon_in_service()) {
  398. pr_info("%s: Client not running :%d:\n",
  399. __func__,
  400. is_daemon_in_service());
  401. goto out;
  402. }
  403. debug_string_to_mask(buf, &c_mask, 1);
  404. debug_mask_to_string(&c_mask, 1);
  405. debug_string = client_debug_string;
  406. new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
  407. if (!new_op) {
  408. pr_info("%s: op_alloc failed!\n", __func__);
  409. goto out;
  410. }
  411. new_op->upcall.req.param.op =
  412. ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
  413. new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
  414. memset(new_op->upcall.req.param.s_value,
  415. 0,
  416. ORANGEFS_MAX_DEBUG_STRING_LEN);
  417. sprintf(new_op->upcall.req.param.s_value,
  418. "%llx %llx\n",
  419. c_mask.mask1,
  420. c_mask.mask2);
  421. /* service_operation returns 0 on success... */
  422. rc = service_operation(new_op,
  423. "orangefs_param",
  424. ORANGEFS_OP_INTERRUPTIBLE);
  425. if (rc)
  426. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  427. "%s: service_operation failed! rc:%d:\n",
  428. __func__,
  429. rc);
  430. op_release(new_op);
  431. }
  432. mutex_lock(&orangefs_debug_lock);
  433. s = file_inode(file)->i_private;
  434. memset(s, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  435. sprintf(s, "%s\n", debug_string);
  436. mutex_unlock(&orangefs_debug_lock);
  437. *ppos += count;
  438. if (silly)
  439. rc = silly;
  440. else
  441. rc = count;
  442. out:
  443. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  444. "orangefs_debug_write: rc: %d\n",
  445. rc);
  446. kfree(buf);
  447. return rc;
  448. }
  449. /*
  450. * After obtaining a string representation of the client's debug
  451. * keywords and their associated masks, this function is called to build an
  452. * array of these values.
  453. */
  454. static int orangefs_prepare_cdm_array(char *debug_array_string)
  455. {
  456. int i;
  457. int rc = -EINVAL;
  458. char *cds_head = NULL;
  459. char *cds_delimiter = NULL;
  460. int keyword_len = 0;
  461. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  462. /*
  463. * figure out how many elements the cdm_array needs.
  464. */
  465. for (i = 0; i < strlen(debug_array_string); i++)
  466. if (debug_array_string[i] == '\n')
  467. cdm_element_count++;
  468. if (!cdm_element_count) {
  469. pr_info("No elements in client debug array string!\n");
  470. goto out;
  471. }
  472. cdm_array = kcalloc(cdm_element_count, sizeof(*cdm_array), GFP_KERNEL);
  473. if (!cdm_array) {
  474. rc = -ENOMEM;
  475. goto out;
  476. }
  477. cds_head = debug_array_string;
  478. for (i = 0; i < cdm_element_count; i++) {
  479. cds_delimiter = strchr(cds_head, '\n');
  480. *cds_delimiter = '\0';
  481. keyword_len = strcspn(cds_head, " ");
  482. cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
  483. if (!cdm_array[i].keyword) {
  484. rc = -ENOMEM;
  485. goto out;
  486. }
  487. sscanf(cds_head,
  488. "%s %llx %llx",
  489. cdm_array[i].keyword,
  490. (unsigned long long *)&(cdm_array[i].mask1),
  491. (unsigned long long *)&(cdm_array[i].mask2));
  492. if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
  493. client_verbose_index = i;
  494. if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
  495. client_all_index = i;
  496. cds_head = cds_delimiter + 1;
  497. }
  498. rc = cdm_element_count;
  499. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  500. out:
  501. return rc;
  502. }
  503. /*
  504. * /sys/kernel/debug/orangefs/debug-help can be catted to
  505. * see all the available kernel and client debug keywords.
  506. *
  507. * When orangefs.ko initializes, we have no idea what keywords the
  508. * client supports, nor their associated masks.
  509. *
  510. * We pass through this function once at module-load and stamp a
  511. * boilerplate "we don't know" message for the client in the
  512. * debug-help file. We pass through here again when the client
  513. * starts and then we can fill out the debug-help file fully.
  514. *
  515. * The client might be restarted any number of times between
  516. * module reloads, we only build the debug-help file the first time.
  517. */
  518. int orangefs_prepare_debugfs_help_string(int at_boot)
  519. {
  520. char *client_title = "Client Debug Keywords:\n";
  521. char *kernel_title = "Kernel Debug Keywords:\n";
  522. size_t string_size = DEBUG_HELP_STRING_SIZE;
  523. size_t result_size;
  524. size_t i;
  525. char *new;
  526. int rc = -EINVAL;
  527. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  528. if (at_boot)
  529. client_title = HELP_STRING_UNINITIALIZED;
  530. /* build a new debug_help_string. */
  531. new = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
  532. if (!new) {
  533. rc = -ENOMEM;
  534. goto out;
  535. }
  536. /*
  537. * strlcat(dst, src, size) will append at most
  538. * "size - strlen(dst) - 1" bytes of src onto dst,
  539. * null terminating the result, and return the total
  540. * length of the string it tried to create.
  541. *
  542. * We'll just plow through here building our new debug
  543. * help string and let strlcat take care of assuring that
  544. * dst doesn't overflow.
  545. */
  546. strlcat(new, client_title, string_size);
  547. if (!at_boot) {
  548. /*
  549. * fill the client keyword/mask array and remember
  550. * how many elements there were.
  551. */
  552. cdm_element_count =
  553. orangefs_prepare_cdm_array(client_debug_array_string);
  554. if (cdm_element_count <= 0) {
  555. kfree(new);
  556. goto out;
  557. }
  558. for (i = 0; i < cdm_element_count; i++) {
  559. strlcat(new, "\t", string_size);
  560. strlcat(new, cdm_array[i].keyword, string_size);
  561. strlcat(new, "\n", string_size);
  562. }
  563. }
  564. strlcat(new, "\n", string_size);
  565. strlcat(new, kernel_title, string_size);
  566. for (i = 0; i < num_kmod_keyword_mask_map; i++) {
  567. strlcat(new, "\t", string_size);
  568. strlcat(new, s_kmod_keyword_mask_map[i].keyword, string_size);
  569. result_size = strlcat(new, "\n", string_size);
  570. }
  571. /* See if we tried to put too many bytes into "new"... */
  572. if (result_size >= string_size) {
  573. kfree(new);
  574. goto out;
  575. }
  576. if (at_boot) {
  577. debug_help_string = new;
  578. } else {
  579. mutex_lock(&orangefs_help_file_lock);
  580. memset(debug_help_string, 0, DEBUG_HELP_STRING_SIZE);
  581. strlcat(debug_help_string, new, string_size);
  582. mutex_unlock(&orangefs_help_file_lock);
  583. }
  584. rc = 0;
  585. out: return rc;
  586. }
  587. /*
  588. * kernel = type 0
  589. * client = type 1
  590. */
  591. static void debug_mask_to_string(void *mask, int type)
  592. {
  593. int i;
  594. int len = 0;
  595. char *debug_string;
  596. int element_count = 0;
  597. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  598. if (type) {
  599. debug_string = client_debug_string;
  600. element_count = cdm_element_count;
  601. } else {
  602. debug_string = kernel_debug_string;
  603. element_count = num_kmod_keyword_mask_map;
  604. }
  605. memset(debug_string, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  606. /*
  607. * Some keywords, like "all" or "verbose", are amalgams of
  608. * numerous other keywords. Make a special check for those
  609. * before grinding through the whole mask only to find out
  610. * later...
  611. */
  612. if (check_amalgam_keyword(mask, type))
  613. goto out;
  614. /* Build the debug string. */
  615. for (i = 0; i < element_count; i++)
  616. if (type)
  617. do_c_string(mask, i);
  618. else
  619. do_k_string(mask, i);
  620. len = strlen(debug_string);
  621. if ((len) && (type))
  622. client_debug_string[len - 1] = '\0';
  623. else if (len)
  624. kernel_debug_string[len - 1] = '\0';
  625. else if (type)
  626. strcpy(client_debug_string, "none");
  627. else
  628. strcpy(kernel_debug_string, "none");
  629. out:
  630. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
  631. return;
  632. }
  633. static void do_k_string(void *k_mask, int index)
  634. {
  635. __u64 *mask = (__u64 *) k_mask;
  636. if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
  637. goto out;
  638. if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
  639. if ((strlen(kernel_debug_string) +
  640. strlen(s_kmod_keyword_mask_map[index].keyword))
  641. < ORANGEFS_MAX_DEBUG_STRING_LEN - 1) {
  642. strcat(kernel_debug_string,
  643. s_kmod_keyword_mask_map[index].keyword);
  644. strcat(kernel_debug_string, ",");
  645. } else {
  646. gossip_err("%s: overflow!\n", __func__);
  647. strcpy(kernel_debug_string, ORANGEFS_ALL);
  648. goto out;
  649. }
  650. }
  651. out:
  652. return;
  653. }
  654. static void do_c_string(void *c_mask, int index)
  655. {
  656. struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
  657. if (keyword_is_amalgam(cdm_array[index].keyword))
  658. goto out;
  659. if ((mask->mask1 & cdm_array[index].mask1) ||
  660. (mask->mask2 & cdm_array[index].mask2)) {
  661. if ((strlen(client_debug_string) +
  662. strlen(cdm_array[index].keyword) + 1)
  663. < ORANGEFS_MAX_DEBUG_STRING_LEN - 2) {
  664. strcat(client_debug_string,
  665. cdm_array[index].keyword);
  666. strcat(client_debug_string, ",");
  667. } else {
  668. gossip_err("%s: overflow!\n", __func__);
  669. strcpy(client_debug_string, ORANGEFS_ALL);
  670. goto out;
  671. }
  672. }
  673. out:
  674. return;
  675. }
  676. static int keyword_is_amalgam(char *keyword)
  677. {
  678. int rc = 0;
  679. if ((!strcmp(keyword, ORANGEFS_ALL)) || (!strcmp(keyword, ORANGEFS_VERBOSE)))
  680. rc = 1;
  681. return rc;
  682. }
  683. /*
  684. * kernel = type 0
  685. * client = type 1
  686. *
  687. * return 1 if we found an amalgam.
  688. */
  689. static int check_amalgam_keyword(void *mask, int type)
  690. {
  691. __u64 *k_mask;
  692. struct client_debug_mask *c_mask;
  693. int k_all_index = num_kmod_keyword_mask_map - 1;
  694. int rc = 0;
  695. if (type) {
  696. c_mask = (struct client_debug_mask *) mask;
  697. if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
  698. (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
  699. strcpy(client_debug_string, ORANGEFS_ALL);
  700. rc = 1;
  701. goto out;
  702. }
  703. if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
  704. (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
  705. strcpy(client_debug_string, ORANGEFS_VERBOSE);
  706. rc = 1;
  707. goto out;
  708. }
  709. } else {
  710. k_mask = (__u64 *) mask;
  711. if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
  712. strcpy(kernel_debug_string, ORANGEFS_ALL);
  713. rc = 1;
  714. goto out;
  715. }
  716. }
  717. out:
  718. return rc;
  719. }
  720. /*
  721. * kernel = type 0
  722. * client = type 1
  723. */
  724. static void debug_string_to_mask(char *debug_string, void *mask, int type)
  725. {
  726. char *unchecked_keyword;
  727. int i;
  728. char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
  729. char *original_pointer;
  730. int element_count = 0;
  731. struct client_debug_mask *c_mask = NULL;
  732. __u64 *k_mask = NULL;
  733. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  734. if (type) {
  735. c_mask = (struct client_debug_mask *)mask;
  736. element_count = cdm_element_count;
  737. } else {
  738. k_mask = (__u64 *)mask;
  739. *k_mask = 0;
  740. element_count = num_kmod_keyword_mask_map;
  741. }
  742. original_pointer = strsep_fodder;
  743. while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
  744. if (strlen(unchecked_keyword)) {
  745. for (i = 0; i < element_count; i++)
  746. if (type)
  747. do_c_mask(i,
  748. unchecked_keyword,
  749. &c_mask);
  750. else
  751. do_k_mask(i,
  752. unchecked_keyword,
  753. &k_mask);
  754. }
  755. kfree(original_pointer);
  756. }
  757. static void do_c_mask(int i, char *unchecked_keyword,
  758. struct client_debug_mask **sane_mask)
  759. {
  760. if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
  761. (**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
  762. (**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
  763. }
  764. }
  765. static void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
  766. {
  767. if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
  768. **sane_mask = (**sane_mask) |
  769. s_kmod_keyword_mask_map[i].mask_val;
  770. }
  771. int orangefs_debugfs_new_client_mask(void __user *arg)
  772. {
  773. struct dev_mask2_info_s mask2_info = {0};
  774. int ret;
  775. ret = copy_from_user(&mask2_info,
  776. (void __user *)arg,
  777. sizeof(struct dev_mask2_info_s));
  778. if (ret != 0)
  779. return -EIO;
  780. client_debug_mask.mask1 = mask2_info.mask1_value;
  781. client_debug_mask.mask2 = mask2_info.mask2_value;
  782. pr_info("%s: client debug mask has been been received "
  783. ":%llx: :%llx:\n",
  784. __func__,
  785. (unsigned long long)client_debug_mask.mask1,
  786. (unsigned long long)client_debug_mask.mask2);
  787. return ret;
  788. }
  789. int orangefs_debugfs_new_client_string(void __user *arg)
  790. {
  791. int ret;
  792. ret = copy_from_user(&client_debug_array_string,
  793. (void __user *)arg,
  794. ORANGEFS_MAX_DEBUG_STRING_LEN);
  795. if (ret != 0) {
  796. pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
  797. __func__);
  798. return -EFAULT;
  799. }
  800. /*
  801. * The real client-core makes an effort to ensure
  802. * that actual strings that aren't too long to fit in
  803. * this buffer is what we get here. We're going to use
  804. * string functions on the stuff we got, so we'll make
  805. * this extra effort to try and keep from
  806. * flowing out of this buffer when we use the string
  807. * functions, even if somehow the stuff we end up
  808. * with here is garbage.
  809. */
  810. client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN - 1] =
  811. '\0';
  812. pr_info("%s: client debug array string has been received.\n",
  813. __func__);
  814. if (!help_string_initialized) {
  815. /* Build a proper debug help string. */
  816. ret = orangefs_prepare_debugfs_help_string(0);
  817. if (ret) {
  818. gossip_err("%s: no debug help string \n",
  819. __func__);
  820. return ret;
  821. }
  822. }
  823. debug_mask_to_string(&client_debug_mask, 1);
  824. debugfs_remove(client_debug_dentry);
  825. orangefs_client_debug_init();
  826. help_string_initialized++;
  827. return 0;
  828. }
  829. int orangefs_debugfs_new_debug(void __user *arg)
  830. {
  831. struct dev_mask_info_s mask_info = {0};
  832. int ret;
  833. ret = copy_from_user(&mask_info,
  834. (void __user *)arg,
  835. sizeof(mask_info));
  836. if (ret != 0)
  837. return -EIO;
  838. if (mask_info.mask_type == KERNEL_MASK) {
  839. if ((mask_info.mask_value == 0)
  840. && (kernel_mask_set_mod_init)) {
  841. /*
  842. * the kernel debug mask was set when the
  843. * kernel module was loaded; don't override
  844. * it if the client-core was started without
  845. * a value for ORANGEFS_KMODMASK.
  846. */
  847. return 0;
  848. }
  849. debug_mask_to_string(&mask_info.mask_value,
  850. mask_info.mask_type);
  851. orangefs_gossip_debug_mask = mask_info.mask_value;
  852. pr_info("%s: kernel debug mask has been modified to "
  853. ":%s: :%llx:\n",
  854. __func__,
  855. kernel_debug_string,
  856. (unsigned long long)orangefs_gossip_debug_mask);
  857. } else if (mask_info.mask_type == CLIENT_MASK) {
  858. debug_mask_to_string(&mask_info.mask_value,
  859. mask_info.mask_type);
  860. pr_info("%s: client debug mask has been modified to"
  861. ":%s: :%llx:\n",
  862. __func__,
  863. client_debug_string,
  864. llu(mask_info.mask_value));
  865. } else {
  866. gossip_err("Invalid mask type....\n");
  867. return -EINVAL;
  868. }
  869. return ret;
  870. }