orangefs-debugfs.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
  253. return NULL;
  254. }
  255. static void help_stop(struct seq_file *m, void *p)
  256. {
  257. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_stop: start\n");
  258. mutex_unlock(&orangefs_help_file_lock);
  259. }
  260. static int help_show(struct seq_file *m, void *v)
  261. {
  262. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_show: start\n");
  263. seq_puts(m, v);
  264. return 0;
  265. }
  266. /*
  267. * initialize the client-debug file.
  268. */
  269. static int orangefs_client_debug_init(void)
  270. {
  271. int rc = -ENOMEM;
  272. char *c_buffer = NULL;
  273. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
  274. c_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  275. if (!c_buffer)
  276. goto out;
  277. if (strlen(client_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
  278. strcpy(c_buffer, client_debug_string);
  279. strcat(c_buffer, "\n");
  280. } else {
  281. strcpy(c_buffer, "none\n");
  282. pr_info("%s: overflow! 2\n", __func__);
  283. }
  284. client_debug_dentry = debugfs_create_file(ORANGEFS_CLIENT_DEBUG_FILE,
  285. 0444,
  286. debug_dir,
  287. c_buffer,
  288. &kernel_debug_fops);
  289. if (!client_debug_dentry) {
  290. pr_info("%s: failed to create updated %s.\n",
  291. __func__,
  292. ORANGEFS_CLIENT_DEBUG_FILE);
  293. goto out;
  294. }
  295. rc = 0;
  296. out:
  297. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  298. return rc;
  299. }
  300. /* open ORANGEFS_KMOD_DEBUG_FILE or ORANGEFS_CLIENT_DEBUG_FILE.*/
  301. static int orangefs_debug_open(struct inode *inode, struct file *file)
  302. {
  303. int rc = -ENODEV;
  304. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  305. "%s: orangefs_debug_disabled: %d\n",
  306. __func__,
  307. orangefs_debug_disabled);
  308. if (orangefs_debug_disabled)
  309. goto out;
  310. rc = 0;
  311. mutex_lock(&orangefs_debug_lock);
  312. file->private_data = inode->i_private;
  313. mutex_unlock(&orangefs_debug_lock);
  314. out:
  315. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  316. "orangefs_debug_open: rc: %d\n",
  317. rc);
  318. return rc;
  319. }
  320. static ssize_t orangefs_debug_read(struct file *file,
  321. char __user *ubuf,
  322. size_t count,
  323. loff_t *ppos)
  324. {
  325. char *buf;
  326. int sprintf_ret;
  327. ssize_t read_ret = -ENOMEM;
  328. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "orangefs_debug_read: start\n");
  329. buf = kmalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  330. if (!buf)
  331. goto out;
  332. mutex_lock(&orangefs_debug_lock);
  333. sprintf_ret = sprintf(buf, "%s", (char *)file->private_data);
  334. mutex_unlock(&orangefs_debug_lock);
  335. read_ret = simple_read_from_buffer(ubuf, count, ppos, buf, sprintf_ret);
  336. kfree(buf);
  337. out:
  338. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  339. "orangefs_debug_read: ret: %zu\n",
  340. read_ret);
  341. return read_ret;
  342. }
  343. static ssize_t orangefs_debug_write(struct file *file,
  344. const char __user *ubuf,
  345. size_t count,
  346. loff_t *ppos)
  347. {
  348. char *buf;
  349. int rc = -EFAULT;
  350. size_t silly = 0;
  351. char *debug_string;
  352. struct orangefs_kernel_op_s *new_op = NULL;
  353. struct client_debug_mask c_mask = { NULL, 0, 0 };
  354. char *s;
  355. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  356. "orangefs_debug_write: %pD\n",
  357. file);
  358. if (count == 0)
  359. return 0;
  360. /*
  361. * Thwart users who try to jamb a ridiculous number
  362. * of bytes into the debug file...
  363. */
  364. if (count > ORANGEFS_MAX_DEBUG_STRING_LEN + 1) {
  365. silly = count;
  366. count = ORANGEFS_MAX_DEBUG_STRING_LEN + 1;
  367. }
  368. buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  369. if (!buf)
  370. goto out;
  371. if (copy_from_user(buf, ubuf, count - 1)) {
  372. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  373. "%s: copy_from_user failed!\n",
  374. __func__);
  375. goto out;
  376. }
  377. /*
  378. * Map the keyword string from userspace into a valid debug mask.
  379. * The mapping process involves mapping the human-inputted string
  380. * into a valid mask, and then rebuilding the string from the
  381. * verified valid mask.
  382. *
  383. * A service operation is required to set a new client-side
  384. * debug mask.
  385. */
  386. if (!strcmp(file->f_path.dentry->d_name.name,
  387. ORANGEFS_KMOD_DEBUG_FILE)) {
  388. debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
  389. debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
  390. debug_string = kernel_debug_string;
  391. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  392. "New kernel debug string is %s\n",
  393. kernel_debug_string);
  394. } else {
  395. /* Can't reset client debug mask if client is not running. */
  396. if (is_daemon_in_service()) {
  397. pr_info("%s: Client not running :%d:\n",
  398. __func__,
  399. is_daemon_in_service());
  400. goto out;
  401. }
  402. debug_string_to_mask(buf, &c_mask, 1);
  403. debug_mask_to_string(&c_mask, 1);
  404. debug_string = client_debug_string;
  405. new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
  406. if (!new_op) {
  407. pr_info("%s: op_alloc failed!\n", __func__);
  408. goto out;
  409. }
  410. new_op->upcall.req.param.op =
  411. ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
  412. new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
  413. memset(new_op->upcall.req.param.s_value,
  414. 0,
  415. ORANGEFS_MAX_DEBUG_STRING_LEN);
  416. sprintf(new_op->upcall.req.param.s_value,
  417. "%llx %llx\n",
  418. c_mask.mask1,
  419. c_mask.mask2);
  420. /* service_operation returns 0 on success... */
  421. rc = service_operation(new_op,
  422. "orangefs_param",
  423. ORANGEFS_OP_INTERRUPTIBLE);
  424. if (rc)
  425. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  426. "%s: service_operation failed! rc:%d:\n",
  427. __func__,
  428. rc);
  429. op_release(new_op);
  430. }
  431. mutex_lock(&orangefs_debug_lock);
  432. s = file_inode(file)->i_private;
  433. memset(s, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  434. sprintf(s, "%s\n", debug_string);
  435. mutex_unlock(&orangefs_debug_lock);
  436. *ppos += count;
  437. if (silly)
  438. rc = silly;
  439. else
  440. rc = count;
  441. out:
  442. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  443. "orangefs_debug_write: rc: %d\n",
  444. rc);
  445. kfree(buf);
  446. return rc;
  447. }
  448. /*
  449. * After obtaining a string representation of the client's debug
  450. * keywords and their associated masks, this function is called to build an
  451. * array of these values.
  452. */
  453. static int orangefs_prepare_cdm_array(char *debug_array_string)
  454. {
  455. int i;
  456. int rc = -EINVAL;
  457. char *cds_head = NULL;
  458. char *cds_delimiter = NULL;
  459. int keyword_len = 0;
  460. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  461. /*
  462. * figure out how many elements the cdm_array needs.
  463. */
  464. for (i = 0; i < strlen(debug_array_string); i++)
  465. if (debug_array_string[i] == '\n')
  466. cdm_element_count++;
  467. if (!cdm_element_count) {
  468. pr_info("No elements in client debug array string!\n");
  469. goto out;
  470. }
  471. cdm_array = kcalloc(cdm_element_count, sizeof(*cdm_array), GFP_KERNEL);
  472. if (!cdm_array) {
  473. rc = -ENOMEM;
  474. goto out;
  475. }
  476. cds_head = debug_array_string;
  477. for (i = 0; i < cdm_element_count; i++) {
  478. cds_delimiter = strchr(cds_head, '\n');
  479. *cds_delimiter = '\0';
  480. keyword_len = strcspn(cds_head, " ");
  481. cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
  482. if (!cdm_array[i].keyword) {
  483. rc = -ENOMEM;
  484. goto out;
  485. }
  486. sscanf(cds_head,
  487. "%s %llx %llx",
  488. cdm_array[i].keyword,
  489. (unsigned long long *)&(cdm_array[i].mask1),
  490. (unsigned long long *)&(cdm_array[i].mask2));
  491. if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
  492. client_verbose_index = i;
  493. if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
  494. client_all_index = i;
  495. cds_head = cds_delimiter + 1;
  496. }
  497. rc = cdm_element_count;
  498. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  499. out:
  500. return rc;
  501. }
  502. /*
  503. * /sys/kernel/debug/orangefs/debug-help can be catted to
  504. * see all the available kernel and client debug keywords.
  505. *
  506. * When orangefs.ko initializes, we have no idea what keywords the
  507. * client supports, nor their associated masks.
  508. *
  509. * We pass through this function once at module-load and stamp a
  510. * boilerplate "we don't know" message for the client in the
  511. * debug-help file. We pass through here again when the client
  512. * starts and then we can fill out the debug-help file fully.
  513. *
  514. * The client might be restarted any number of times between
  515. * module reloads, we only build the debug-help file the first time.
  516. */
  517. int orangefs_prepare_debugfs_help_string(int at_boot)
  518. {
  519. char *client_title = "Client Debug Keywords:\n";
  520. char *kernel_title = "Kernel Debug Keywords:\n";
  521. size_t string_size = DEBUG_HELP_STRING_SIZE;
  522. size_t result_size;
  523. size_t i;
  524. char *new;
  525. int rc = -EINVAL;
  526. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  527. if (at_boot)
  528. client_title = HELP_STRING_UNINITIALIZED;
  529. /* build a new debug_help_string. */
  530. new = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
  531. if (!new) {
  532. rc = -ENOMEM;
  533. goto out;
  534. }
  535. /*
  536. * strlcat(dst, src, size) will append at most
  537. * "size - strlen(dst) - 1" bytes of src onto dst,
  538. * null terminating the result, and return the total
  539. * length of the string it tried to create.
  540. *
  541. * We'll just plow through here building our new debug
  542. * help string and let strlcat take care of assuring that
  543. * dst doesn't overflow.
  544. */
  545. strlcat(new, client_title, string_size);
  546. if (!at_boot) {
  547. /*
  548. * fill the client keyword/mask array and remember
  549. * how many elements there were.
  550. */
  551. cdm_element_count =
  552. orangefs_prepare_cdm_array(client_debug_array_string);
  553. if (cdm_element_count <= 0) {
  554. kfree(new);
  555. goto out;
  556. }
  557. for (i = 0; i < cdm_element_count; i++) {
  558. strlcat(new, "\t", string_size);
  559. strlcat(new, cdm_array[i].keyword, string_size);
  560. strlcat(new, "\n", string_size);
  561. }
  562. }
  563. strlcat(new, "\n", string_size);
  564. strlcat(new, kernel_title, string_size);
  565. for (i = 0; i < num_kmod_keyword_mask_map; i++) {
  566. strlcat(new, "\t", string_size);
  567. strlcat(new, s_kmod_keyword_mask_map[i].keyword, string_size);
  568. result_size = strlcat(new, "\n", string_size);
  569. }
  570. /* See if we tried to put too many bytes into "new"... */
  571. if (result_size >= string_size) {
  572. kfree(new);
  573. goto out;
  574. }
  575. if (at_boot) {
  576. debug_help_string = new;
  577. } else {
  578. mutex_lock(&orangefs_help_file_lock);
  579. memset(debug_help_string, 0, DEBUG_HELP_STRING_SIZE);
  580. strlcat(debug_help_string, new, string_size);
  581. mutex_unlock(&orangefs_help_file_lock);
  582. }
  583. rc = 0;
  584. out: return rc;
  585. }
  586. /*
  587. * kernel = type 0
  588. * client = type 1
  589. */
  590. static void debug_mask_to_string(void *mask, int type)
  591. {
  592. int i;
  593. int len = 0;
  594. char *debug_string;
  595. int element_count = 0;
  596. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  597. if (type) {
  598. debug_string = client_debug_string;
  599. element_count = cdm_element_count;
  600. } else {
  601. debug_string = kernel_debug_string;
  602. element_count = num_kmod_keyword_mask_map;
  603. }
  604. memset(debug_string, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  605. /*
  606. * Some keywords, like "all" or "verbose", are amalgams of
  607. * numerous other keywords. Make a special check for those
  608. * before grinding through the whole mask only to find out
  609. * later...
  610. */
  611. if (check_amalgam_keyword(mask, type))
  612. goto out;
  613. /* Build the debug string. */
  614. for (i = 0; i < element_count; i++)
  615. if (type)
  616. do_c_string(mask, i);
  617. else
  618. do_k_string(mask, i);
  619. len = strlen(debug_string);
  620. if ((len) && (type))
  621. client_debug_string[len - 1] = '\0';
  622. else if (len)
  623. kernel_debug_string[len - 1] = '\0';
  624. else if (type)
  625. strcpy(client_debug_string, "none");
  626. else
  627. strcpy(kernel_debug_string, "none");
  628. out:
  629. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
  630. return;
  631. }
  632. static void do_k_string(void *k_mask, int index)
  633. {
  634. __u64 *mask = (__u64 *) k_mask;
  635. if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
  636. goto out;
  637. if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
  638. if ((strlen(kernel_debug_string) +
  639. strlen(s_kmod_keyword_mask_map[index].keyword))
  640. < ORANGEFS_MAX_DEBUG_STRING_LEN - 1) {
  641. strcat(kernel_debug_string,
  642. s_kmod_keyword_mask_map[index].keyword);
  643. strcat(kernel_debug_string, ",");
  644. } else {
  645. gossip_err("%s: overflow!\n", __func__);
  646. strcpy(kernel_debug_string, ORANGEFS_ALL);
  647. goto out;
  648. }
  649. }
  650. out:
  651. return;
  652. }
  653. static void do_c_string(void *c_mask, int index)
  654. {
  655. struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
  656. if (keyword_is_amalgam(cdm_array[index].keyword))
  657. goto out;
  658. if ((mask->mask1 & cdm_array[index].mask1) ||
  659. (mask->mask2 & cdm_array[index].mask2)) {
  660. if ((strlen(client_debug_string) +
  661. strlen(cdm_array[index].keyword) + 1)
  662. < ORANGEFS_MAX_DEBUG_STRING_LEN - 2) {
  663. strcat(client_debug_string,
  664. cdm_array[index].keyword);
  665. strcat(client_debug_string, ",");
  666. } else {
  667. gossip_err("%s: overflow!\n", __func__);
  668. strcpy(client_debug_string, ORANGEFS_ALL);
  669. goto out;
  670. }
  671. }
  672. out:
  673. return;
  674. }
  675. static int keyword_is_amalgam(char *keyword)
  676. {
  677. int rc = 0;
  678. if ((!strcmp(keyword, ORANGEFS_ALL)) || (!strcmp(keyword, ORANGEFS_VERBOSE)))
  679. rc = 1;
  680. return rc;
  681. }
  682. /*
  683. * kernel = type 0
  684. * client = type 1
  685. *
  686. * return 1 if we found an amalgam.
  687. */
  688. static int check_amalgam_keyword(void *mask, int type)
  689. {
  690. __u64 *k_mask;
  691. struct client_debug_mask *c_mask;
  692. int k_all_index = num_kmod_keyword_mask_map - 1;
  693. int rc = 0;
  694. if (type) {
  695. c_mask = (struct client_debug_mask *) mask;
  696. if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
  697. (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
  698. strcpy(client_debug_string, ORANGEFS_ALL);
  699. rc = 1;
  700. goto out;
  701. }
  702. if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
  703. (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
  704. strcpy(client_debug_string, ORANGEFS_VERBOSE);
  705. rc = 1;
  706. goto out;
  707. }
  708. } else {
  709. k_mask = (__u64 *) mask;
  710. if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
  711. strcpy(kernel_debug_string, ORANGEFS_ALL);
  712. rc = 1;
  713. goto out;
  714. }
  715. }
  716. out:
  717. return rc;
  718. }
  719. /*
  720. * kernel = type 0
  721. * client = type 1
  722. */
  723. static void debug_string_to_mask(char *debug_string, void *mask, int type)
  724. {
  725. char *unchecked_keyword;
  726. int i;
  727. char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
  728. char *original_pointer;
  729. int element_count = 0;
  730. struct client_debug_mask *c_mask = NULL;
  731. __u64 *k_mask = NULL;
  732. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  733. if (type) {
  734. c_mask = (struct client_debug_mask *)mask;
  735. element_count = cdm_element_count;
  736. } else {
  737. k_mask = (__u64 *)mask;
  738. *k_mask = 0;
  739. element_count = num_kmod_keyword_mask_map;
  740. }
  741. original_pointer = strsep_fodder;
  742. while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
  743. if (strlen(unchecked_keyword)) {
  744. for (i = 0; i < element_count; i++)
  745. if (type)
  746. do_c_mask(i,
  747. unchecked_keyword,
  748. &c_mask);
  749. else
  750. do_k_mask(i,
  751. unchecked_keyword,
  752. &k_mask);
  753. }
  754. kfree(original_pointer);
  755. }
  756. static void do_c_mask(int i, char *unchecked_keyword,
  757. struct client_debug_mask **sane_mask)
  758. {
  759. if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
  760. (**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
  761. (**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
  762. }
  763. }
  764. static void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
  765. {
  766. if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
  767. **sane_mask = (**sane_mask) |
  768. s_kmod_keyword_mask_map[i].mask_val;
  769. }
  770. int orangefs_debugfs_new_client_mask(void __user *arg)
  771. {
  772. struct dev_mask2_info_s mask2_info = {0};
  773. int ret;
  774. ret = copy_from_user(&mask2_info,
  775. (void __user *)arg,
  776. sizeof(struct dev_mask2_info_s));
  777. if (ret != 0)
  778. return -EIO;
  779. client_debug_mask.mask1 = mask2_info.mask1_value;
  780. client_debug_mask.mask2 = mask2_info.mask2_value;
  781. pr_info("%s: client debug mask has been been received "
  782. ":%llx: :%llx:\n",
  783. __func__,
  784. (unsigned long long)client_debug_mask.mask1,
  785. (unsigned long long)client_debug_mask.mask2);
  786. return ret;
  787. }
  788. int orangefs_debugfs_new_client_string(void __user *arg)
  789. {
  790. int ret;
  791. ret = copy_from_user(&client_debug_array_string,
  792. (void __user *)arg,
  793. ORANGEFS_MAX_DEBUG_STRING_LEN);
  794. if (ret != 0) {
  795. pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
  796. __func__);
  797. return -EFAULT;
  798. }
  799. /*
  800. * The real client-core makes an effort to ensure
  801. * that actual strings that aren't too long to fit in
  802. * this buffer is what we get here. We're going to use
  803. * string functions on the stuff we got, so we'll make
  804. * this extra effort to try and keep from
  805. * flowing out of this buffer when we use the string
  806. * functions, even if somehow the stuff we end up
  807. * with here is garbage.
  808. */
  809. client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN - 1] =
  810. '\0';
  811. pr_info("%s: client debug array string has been received.\n",
  812. __func__);
  813. if (!help_string_initialized) {
  814. /* Build a proper debug help string. */
  815. ret = orangefs_prepare_debugfs_help_string(0);
  816. if (ret) {
  817. gossip_err("%s: no debug help string \n",
  818. __func__);
  819. return ret;
  820. }
  821. }
  822. debug_mask_to_string(&client_debug_mask, 1);
  823. debugfs_remove(client_debug_dentry);
  824. orangefs_client_debug_init();
  825. help_string_initialized++;
  826. return 0;
  827. }
  828. int orangefs_debugfs_new_debug(void __user *arg)
  829. {
  830. struct dev_mask_info_s mask_info = {0};
  831. int ret;
  832. ret = copy_from_user(&mask_info,
  833. (void __user *)arg,
  834. sizeof(mask_info));
  835. if (ret != 0)
  836. return -EIO;
  837. if (mask_info.mask_type == KERNEL_MASK) {
  838. if ((mask_info.mask_value == 0)
  839. && (kernel_mask_set_mod_init)) {
  840. /*
  841. * the kernel debug mask was set when the
  842. * kernel module was loaded; don't override
  843. * it if the client-core was started without
  844. * a value for ORANGEFS_KMODMASK.
  845. */
  846. return 0;
  847. }
  848. debug_mask_to_string(&mask_info.mask_value,
  849. mask_info.mask_type);
  850. orangefs_gossip_debug_mask = mask_info.mask_value;
  851. pr_info("%s: kernel debug mask has been modified to "
  852. ":%s: :%llx:\n",
  853. __func__,
  854. kernel_debug_string,
  855. (unsigned long long)orangefs_gossip_debug_mask);
  856. } else if (mask_info.mask_type == CLIENT_MASK) {
  857. debug_mask_to_string(&mask_info.mask_value,
  858. mask_info.mask_type);
  859. pr_info("%s: client debug mask has been modified to"
  860. ":%s: :%llx:\n",
  861. __func__,
  862. client_debug_string,
  863. llu(mask_info.mask_value));
  864. } else {
  865. gossip_err("Invalid mask type....\n");
  866. return -EINVAL;
  867. }
  868. return ret;
  869. }