orangefs-debugfs.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. /* a private internal type */
  45. struct __keyword_mask_s {
  46. const char *keyword;
  47. __u64 mask_val;
  48. };
  49. /*
  50. * Map all kmod keywords to kmod debug masks here. Keep this
  51. * structure "packed":
  52. *
  53. * "all" is always last...
  54. *
  55. * keyword mask_val index
  56. * foo 1 0
  57. * bar 2 1
  58. * baz 4 2
  59. * qux 8 3
  60. * . . .
  61. */
  62. static struct __keyword_mask_s s_kmod_keyword_mask_map[] = {
  63. {"super", GOSSIP_SUPER_DEBUG},
  64. {"inode", GOSSIP_INODE_DEBUG},
  65. {"file", GOSSIP_FILE_DEBUG},
  66. {"dir", GOSSIP_DIR_DEBUG},
  67. {"utils", GOSSIP_UTILS_DEBUG},
  68. {"wait", GOSSIP_WAIT_DEBUG},
  69. {"acl", GOSSIP_ACL_DEBUG},
  70. {"dcache", GOSSIP_DCACHE_DEBUG},
  71. {"dev", GOSSIP_DEV_DEBUG},
  72. {"name", GOSSIP_NAME_DEBUG},
  73. {"bufmap", GOSSIP_BUFMAP_DEBUG},
  74. {"cache", GOSSIP_CACHE_DEBUG},
  75. {"debugfs", GOSSIP_DEBUGFS_DEBUG},
  76. {"xattr", GOSSIP_XATTR_DEBUG},
  77. {"init", GOSSIP_INIT_DEBUG},
  78. {"sysfs", GOSSIP_SYSFS_DEBUG},
  79. {"none", GOSSIP_NO_DEBUG},
  80. {"all", GOSSIP_MAX_DEBUG}
  81. };
  82. static const int num_kmod_keyword_mask_map = (int)
  83. (ARRAY_SIZE(s_kmod_keyword_mask_map));
  84. #define DEBUG_HELP_STRING_SIZE 4096
  85. #define HELP_STRING_UNINITIALIZED \
  86. "Client Debug Keywords are unknown until the first time\n" \
  87. "the client is started after boot.\n"
  88. #define ORANGEFS_KMOD_DEBUG_HELP_FILE "debug-help"
  89. #define ORANGEFS_KMOD_DEBUG_FILE "kernel-debug"
  90. #define ORANGEFS_CLIENT_DEBUG_FILE "client-debug"
  91. #define ORANGEFS_VERBOSE "verbose"
  92. #define ORANGEFS_ALL "all"
  93. /*
  94. * An array of client_debug_mask will be built to hold debug keyword/mask
  95. * values fetched from userspace.
  96. */
  97. struct client_debug_mask {
  98. char *keyword;
  99. __u64 mask1;
  100. __u64 mask2;
  101. };
  102. static void orangefs_kernel_debug_init(void);
  103. static int orangefs_debug_help_open(struct inode *, struct file *);
  104. static void *help_start(struct seq_file *, loff_t *);
  105. static void *help_next(struct seq_file *, void *, loff_t *);
  106. static void help_stop(struct seq_file *, void *);
  107. static int help_show(struct seq_file *, void *);
  108. static int orangefs_debug_open(struct inode *, struct file *);
  109. static ssize_t orangefs_debug_read(struct file *,
  110. char __user *,
  111. size_t,
  112. loff_t *);
  113. static ssize_t orangefs_debug_write(struct file *,
  114. const char __user *,
  115. size_t,
  116. loff_t *);
  117. static int orangefs_prepare_cdm_array(char *);
  118. static void debug_mask_to_string(void *, int);
  119. static void do_k_string(void *, int);
  120. static void do_c_string(void *, int);
  121. static int keyword_is_amalgam(char *);
  122. static int check_amalgam_keyword(void *, int);
  123. static void debug_string_to_mask(char *, void *, int);
  124. static void do_c_mask(int, char *, struct client_debug_mask **);
  125. static void do_k_mask(int, char *, __u64 **);
  126. static char kernel_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN] = "none";
  127. static char *debug_help_string;
  128. static char client_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
  129. static char client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
  130. static struct dentry *client_debug_dentry;
  131. static struct dentry *debug_dir;
  132. static unsigned int kernel_mask_set_mod_init;
  133. static int orangefs_debug_disabled = 1;
  134. static int help_string_initialized;
  135. static const struct seq_operations help_debug_ops = {
  136. .start = help_start,
  137. .next = help_next,
  138. .stop = help_stop,
  139. .show = help_show,
  140. };
  141. static const struct file_operations debug_help_fops = {
  142. .owner = THIS_MODULE,
  143. .open = orangefs_debug_help_open,
  144. .read = seq_read,
  145. .release = seq_release,
  146. .llseek = seq_lseek,
  147. };
  148. static const struct file_operations kernel_debug_fops = {
  149. .owner = THIS_MODULE,
  150. .open = orangefs_debug_open,
  151. .read = orangefs_debug_read,
  152. .write = orangefs_debug_write,
  153. .llseek = generic_file_llseek,
  154. };
  155. static int client_all_index;
  156. static int client_verbose_index;
  157. static struct client_debug_mask *cdm_array;
  158. static int cdm_element_count;
  159. static struct client_debug_mask client_debug_mask;
  160. /*
  161. * Used to protect data in ORANGEFS_KMOD_DEBUG_FILE and
  162. * ORANGEFS_KMOD_DEBUG_FILE.
  163. */
  164. static DEFINE_MUTEX(orangefs_debug_lock);
  165. /* Used to protect data in ORANGEFS_KMOD_DEBUG_HELP_FILE */
  166. static DEFINE_MUTEX(orangefs_help_file_lock);
  167. /*
  168. * initialize kmod debug operations, create orangefs debugfs dir and
  169. * ORANGEFS_KMOD_DEBUG_HELP_FILE.
  170. */
  171. void orangefs_debugfs_init(int debug_mask)
  172. {
  173. /* convert input debug mask to a 64-bit unsigned integer */
  174. orangefs_gossip_debug_mask = (unsigned long long)debug_mask;
  175. /*
  176. * set the kernel's gossip debug string; invalid mask values will
  177. * be ignored.
  178. */
  179. debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
  180. /* remove any invalid values from the mask */
  181. debug_string_to_mask(kernel_debug_string, &orangefs_gossip_debug_mask,
  182. 0);
  183. /*
  184. * if the mask has a non-zero value, then indicate that the mask
  185. * was set when the kernel module was loaded. The orangefs dev ioctl
  186. * command will look at this boolean to determine if the kernel's
  187. * debug mask should be overwritten when the client-core is started.
  188. */
  189. if (orangefs_gossip_debug_mask != 0)
  190. kernel_mask_set_mod_init = true;
  191. pr_info("%s: called with debug mask: :%s: :%llx:\n",
  192. __func__,
  193. kernel_debug_string,
  194. (unsigned long long)orangefs_gossip_debug_mask);
  195. debug_dir = debugfs_create_dir("orangefs", NULL);
  196. debugfs_create_file(ORANGEFS_KMOD_DEBUG_HELP_FILE, 0444, debug_dir,
  197. debug_help_string, &debug_help_fops);
  198. orangefs_debug_disabled = 0;
  199. orangefs_kernel_debug_init();
  200. }
  201. /*
  202. * initialize the kernel-debug file.
  203. */
  204. static void orangefs_kernel_debug_init(void)
  205. {
  206. static char k_buffer[ORANGEFS_MAX_DEBUG_STRING_LEN] = { };
  207. size_t len = strlen(kernel_debug_string);
  208. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
  209. if (len + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
  210. memcpy(k_buffer, kernel_debug_string, len);
  211. k_buffer[len] = '\n';
  212. k_buffer[len + 1] = '\0';
  213. } else {
  214. strscpy(k_buffer, "none\n");
  215. pr_info("%s: overflow 1!\n", __func__);
  216. }
  217. debugfs_create_file_aux_num(ORANGEFS_KMOD_DEBUG_FILE, 0444, debug_dir, k_buffer,
  218. 0, &kernel_debug_fops);
  219. }
  220. void orangefs_debugfs_cleanup(void)
  221. {
  222. debugfs_remove_recursive(debug_dir);
  223. kfree(debug_help_string);
  224. debug_help_string = NULL;
  225. }
  226. /* open ORANGEFS_KMOD_DEBUG_HELP_FILE */
  227. static int orangefs_debug_help_open(struct inode *inode, struct file *file)
  228. {
  229. int rc = -ENODEV;
  230. int ret;
  231. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  232. "orangefs_debug_help_open: start\n");
  233. if (orangefs_debug_disabled)
  234. goto out;
  235. ret = seq_open(file, &help_debug_ops);
  236. if (ret)
  237. goto out;
  238. ((struct seq_file *)(file->private_data))->private = inode->i_private;
  239. rc = 0;
  240. out:
  241. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  242. "orangefs_debug_help_open: rc:%d:\n",
  243. rc);
  244. return rc;
  245. }
  246. /*
  247. * I think start always gets called again after stop. Start
  248. * needs to return NULL when it is done. The whole "payload"
  249. * in this case is a single (long) string, so by the second
  250. * time we get to start (pos = 1), we're done.
  251. */
  252. static void *help_start(struct seq_file *m, loff_t *pos)
  253. {
  254. void *payload = NULL;
  255. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_start: start\n");
  256. mutex_lock(&orangefs_help_file_lock);
  257. if (*pos == 0)
  258. payload = m->private;
  259. return payload;
  260. }
  261. static void *help_next(struct seq_file *m, void *v, loff_t *pos)
  262. {
  263. (*pos)++;
  264. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
  265. return NULL;
  266. }
  267. static void help_stop(struct seq_file *m, void *p)
  268. {
  269. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_stop: start\n");
  270. mutex_unlock(&orangefs_help_file_lock);
  271. }
  272. static int help_show(struct seq_file *m, void *v)
  273. {
  274. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_show: start\n");
  275. seq_puts(m, v);
  276. return 0;
  277. }
  278. /*
  279. * initialize the client-debug file.
  280. */
  281. static void orangefs_client_debug_init(void)
  282. {
  283. static char c_buffer[ORANGEFS_MAX_DEBUG_STRING_LEN] = { };
  284. size_t len = strlen(client_debug_string);
  285. gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
  286. if (len + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
  287. memcpy(c_buffer, client_debug_string, len);
  288. c_buffer[len] = '\n';
  289. c_buffer[len + 1] = '\0';
  290. } else {
  291. strscpy(c_buffer, "none\n");
  292. pr_info("%s: overflow! 2\n", __func__);
  293. }
  294. client_debug_dentry = debugfs_create_file_aux_num(
  295. ORANGEFS_CLIENT_DEBUG_FILE,
  296. 0444, debug_dir, c_buffer, 1,
  297. &kernel_debug_fops);
  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 = scnprintf(buf, ORANGEFS_MAX_DEBUG_STRING_LEN, "%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) {
  364. silly = count;
  365. count = ORANGEFS_MAX_DEBUG_STRING_LEN;
  366. }
  367. buf = memdup_user_nul(ubuf, count - 1);
  368. if (IS_ERR(buf)) {
  369. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  370. "%s: memdup_user_nul failed!\n",
  371. __func__);
  372. rc = PTR_ERR(buf);
  373. buf = NULL;
  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 (!debugfs_get_aux_num(file)) { // kernel-debug
  386. debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
  387. debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
  388. debug_string = kernel_debug_string;
  389. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  390. "New kernel debug string is %s\n",
  391. kernel_debug_string);
  392. } else {
  393. /* Can't reset client debug mask if client is not running. */
  394. if (is_daemon_in_service()) {
  395. pr_info("%s: Client not running :%d:\n",
  396. __func__,
  397. is_daemon_in_service());
  398. goto out;
  399. }
  400. debug_string_to_mask(buf, &c_mask, 1);
  401. debug_mask_to_string(&c_mask, 1);
  402. debug_string = client_debug_string;
  403. new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
  404. if (!new_op) {
  405. pr_info("%s: op_alloc failed!\n", __func__);
  406. goto out;
  407. }
  408. new_op->upcall.req.param.op =
  409. ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
  410. new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
  411. memset(new_op->upcall.req.param.s_value,
  412. 0,
  413. ORANGEFS_MAX_DEBUG_STRING_LEN);
  414. sprintf(new_op->upcall.req.param.s_value,
  415. "%llx %llx\n",
  416. c_mask.mask1,
  417. c_mask.mask2);
  418. /* service_operation returns 0 on success... */
  419. rc = service_operation(new_op,
  420. "orangefs_param",
  421. ORANGEFS_OP_INTERRUPTIBLE);
  422. if (rc)
  423. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  424. "%s: service_operation failed! rc:%d:\n",
  425. __func__,
  426. rc);
  427. op_release(new_op);
  428. }
  429. mutex_lock(&orangefs_debug_lock);
  430. s = file_inode(file)->i_private;
  431. memset(s, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  432. sprintf(s, "%s\n", debug_string);
  433. mutex_unlock(&orangefs_debug_lock);
  434. *ppos += count;
  435. if (silly)
  436. rc = silly;
  437. else
  438. rc = count;
  439. out:
  440. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  441. "orangefs_debug_write: rc: %d\n",
  442. rc);
  443. kfree(buf);
  444. return rc;
  445. }
  446. /*
  447. * After obtaining a string representation of the client's debug
  448. * keywords and their associated masks, this function is called to build an
  449. * array of these values.
  450. */
  451. static int orangefs_prepare_cdm_array(char *debug_array_string)
  452. {
  453. int i;
  454. int rc = -EINVAL;
  455. char *cds_head = NULL;
  456. char *cds_delimiter = NULL;
  457. int keyword_len = 0;
  458. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  459. /*
  460. * figure out how many elements the cdm_array needs.
  461. */
  462. for (i = 0; i < strlen(debug_array_string); i++)
  463. if (debug_array_string[i] == '\n')
  464. cdm_element_count++;
  465. if (!cdm_element_count) {
  466. pr_info("No elements in client debug array string!\n");
  467. goto out;
  468. }
  469. cdm_array = kzalloc_objs(*cdm_array, cdm_element_count);
  470. if (!cdm_array) {
  471. rc = -ENOMEM;
  472. goto out;
  473. }
  474. cds_head = debug_array_string;
  475. for (i = 0; i < cdm_element_count; i++) {
  476. cds_delimiter = strchr(cds_head, '\n');
  477. *cds_delimiter = '\0';
  478. keyword_len = strcspn(cds_head, " ");
  479. cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
  480. if (!cdm_array[i].keyword) {
  481. rc = -ENOMEM;
  482. goto out;
  483. }
  484. sscanf(cds_head,
  485. "%s %llx %llx",
  486. cdm_array[i].keyword,
  487. (unsigned long long *)&(cdm_array[i].mask1),
  488. (unsigned long long *)&(cdm_array[i].mask2));
  489. if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
  490. client_verbose_index = i;
  491. if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
  492. client_all_index = i;
  493. cds_head = cds_delimiter + 1;
  494. }
  495. rc = cdm_element_count;
  496. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  497. out:
  498. return rc;
  499. }
  500. /*
  501. * /sys/kernel/debug/orangefs/debug-help can be catted to
  502. * see all the available kernel and client debug keywords.
  503. *
  504. * When orangefs.ko initializes, we have no idea what keywords the
  505. * client supports, nor their associated masks.
  506. *
  507. * We pass through this function once at module-load and stamp a
  508. * boilerplate "we don't know" message for the client in the
  509. * debug-help file. We pass through here again when the client
  510. * starts and then we can fill out the debug-help file fully.
  511. *
  512. * The client might be restarted any number of times between
  513. * module reloads, we only build the debug-help file the first time.
  514. */
  515. int orangefs_prepare_debugfs_help_string(int at_boot)
  516. {
  517. char *client_title = "Client Debug Keywords:\n";
  518. char *kernel_title = "Kernel Debug Keywords:\n";
  519. size_t string_size = DEBUG_HELP_STRING_SIZE;
  520. size_t result_size;
  521. size_t i;
  522. char *new;
  523. int rc = -EINVAL;
  524. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  525. if (at_boot)
  526. client_title = HELP_STRING_UNINITIALIZED;
  527. /* build a new debug_help_string. */
  528. new = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
  529. if (!new) {
  530. rc = -ENOMEM;
  531. goto out;
  532. }
  533. /*
  534. * strlcat(dst, src, size) will append at most
  535. * "size - strlen(dst) - 1" bytes of src onto dst,
  536. * null terminating the result, and return the total
  537. * length of the string it tried to create.
  538. *
  539. * We'll just plow through here building our new debug
  540. * help string and let strlcat take care of assuring that
  541. * dst doesn't overflow.
  542. */
  543. strlcat(new, client_title, string_size);
  544. if (!at_boot) {
  545. /*
  546. * fill the client keyword/mask array and remember
  547. * how many elements there were.
  548. */
  549. cdm_element_count =
  550. orangefs_prepare_cdm_array(client_debug_array_string);
  551. if (cdm_element_count <= 0) {
  552. kfree(new);
  553. goto out;
  554. }
  555. for (i = 0; i < cdm_element_count; i++) {
  556. strlcat(new, "\t", string_size);
  557. strlcat(new, cdm_array[i].keyword, string_size);
  558. strlcat(new, "\n", string_size);
  559. }
  560. }
  561. strlcat(new, "\n", string_size);
  562. strlcat(new, kernel_title, string_size);
  563. for (i = 0; i < num_kmod_keyword_mask_map; i++) {
  564. strlcat(new, "\t", string_size);
  565. strlcat(new, s_kmod_keyword_mask_map[i].keyword, string_size);
  566. result_size = strlcat(new, "\n", string_size);
  567. }
  568. /* See if we tried to put too many bytes into "new"... */
  569. if (result_size >= string_size) {
  570. kfree(new);
  571. goto out;
  572. }
  573. if (at_boot) {
  574. debug_help_string = new;
  575. } else {
  576. mutex_lock(&orangefs_help_file_lock);
  577. memset(debug_help_string, 0, DEBUG_HELP_STRING_SIZE);
  578. strlcat(debug_help_string, new, string_size);
  579. mutex_unlock(&orangefs_help_file_lock);
  580. kfree(new);
  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. strscpy(client_debug_string, "none");
  625. else
  626. strscpy(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) + 1)
  639. < ORANGEFS_MAX_DEBUG_STRING_LEN) {
  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. strscpy(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) {
  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. strscpy(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. strscpy(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. strscpy(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. strscpy(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_err("Invalid mask type....\n");
  865. return -EINVAL;
  866. }
  867. return ret;
  868. }