orangefs-debugfs.c 25 KB

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