orangefs-debugfs.c 25 KB

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