orangefs-debugfs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. /*
  358. * Thwart users who try to jamb a ridiculous number
  359. * of bytes into the debug file...
  360. */
  361. if (count > ORANGEFS_MAX_DEBUG_STRING_LEN + 1) {
  362. silly = count;
  363. count = ORANGEFS_MAX_DEBUG_STRING_LEN + 1;
  364. }
  365. buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  366. if (!buf)
  367. goto out;
  368. if (copy_from_user(buf, ubuf, count - 1)) {
  369. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  370. "%s: copy_from_user failed!\n",
  371. __func__);
  372. goto out;
  373. }
  374. /*
  375. * Map the keyword string from userspace into a valid debug mask.
  376. * The mapping process involves mapping the human-inputted string
  377. * into a valid mask, and then rebuilding the string from the
  378. * verified valid mask.
  379. *
  380. * A service operation is required to set a new client-side
  381. * debug mask.
  382. */
  383. if (!strcmp(file->f_path.dentry->d_name.name,
  384. ORANGEFS_KMOD_DEBUG_FILE)) {
  385. debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
  386. debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
  387. debug_string = kernel_debug_string;
  388. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  389. "New kernel debug string is %s\n",
  390. kernel_debug_string);
  391. } else {
  392. /* Can't reset client debug mask if client is not running. */
  393. if (is_daemon_in_service()) {
  394. pr_info("%s: Client not running :%d:\n",
  395. __func__,
  396. is_daemon_in_service());
  397. goto out;
  398. }
  399. debug_string_to_mask(buf, &c_mask, 1);
  400. debug_mask_to_string(&c_mask, 1);
  401. debug_string = client_debug_string;
  402. new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
  403. if (!new_op) {
  404. pr_info("%s: op_alloc failed!\n", __func__);
  405. goto out;
  406. }
  407. new_op->upcall.req.param.op =
  408. ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
  409. new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
  410. memset(new_op->upcall.req.param.s_value,
  411. 0,
  412. ORANGEFS_MAX_DEBUG_STRING_LEN);
  413. sprintf(new_op->upcall.req.param.s_value,
  414. "%llx %llx\n",
  415. c_mask.mask1,
  416. c_mask.mask2);
  417. /* service_operation returns 0 on success... */
  418. rc = service_operation(new_op,
  419. "orangefs_param",
  420. ORANGEFS_OP_INTERRUPTIBLE);
  421. if (rc)
  422. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  423. "%s: service_operation failed! rc:%d:\n",
  424. __func__,
  425. rc);
  426. op_release(new_op);
  427. }
  428. mutex_lock(&orangefs_debug_lock);
  429. s = file_inode(file)->i_private;
  430. memset(s, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  431. sprintf(s, "%s\n", debug_string);
  432. mutex_unlock(&orangefs_debug_lock);
  433. *ppos += count;
  434. if (silly)
  435. rc = silly;
  436. else
  437. rc = count;
  438. out:
  439. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  440. "orangefs_debug_write: rc: %d\n",
  441. rc);
  442. kfree(buf);
  443. return rc;
  444. }
  445. /*
  446. * After obtaining a string representation of the client's debug
  447. * keywords and their associated masks, this function is called to build an
  448. * array of these values.
  449. */
  450. static int orangefs_prepare_cdm_array(char *debug_array_string)
  451. {
  452. int i;
  453. int rc = -EINVAL;
  454. char *cds_head = NULL;
  455. char *cds_delimiter = NULL;
  456. int keyword_len = 0;
  457. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  458. /*
  459. * figure out how many elements the cdm_array needs.
  460. */
  461. for (i = 0; i < strlen(debug_array_string); i++)
  462. if (debug_array_string[i] == '\n')
  463. cdm_element_count++;
  464. if (!cdm_element_count) {
  465. pr_info("No elements in client debug array string!\n");
  466. goto out;
  467. }
  468. cdm_array =
  469. kzalloc(cdm_element_count * sizeof(struct client_debug_mask),
  470. GFP_KERNEL);
  471. if (!cdm_array) {
  472. pr_info("malloc failed for cdm_array!\n");
  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 -EIO;
  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. if (orangefs_prepare_debugfs_help_string(0)) {
  816. gossip_err("%s: no debug help string \n",
  817. __func__);
  818. return -EIO;
  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 ret;
  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. }