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. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  354. "orangefs_debug_write: %pD\n",
  355. file);
  356. /*
  357. * Thwart users who try to jamb a ridiculous number
  358. * of bytes into the debug file...
  359. */
  360. if (count > ORANGEFS_MAX_DEBUG_STRING_LEN + 1) {
  361. silly = count;
  362. count = ORANGEFS_MAX_DEBUG_STRING_LEN + 1;
  363. }
  364. buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
  365. if (!buf)
  366. goto out;
  367. if (copy_from_user(buf, ubuf, count - 1)) {
  368. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  369. "%s: copy_from_user failed!\n",
  370. __func__);
  371. goto out;
  372. }
  373. /*
  374. * Map the keyword string from userspace into a valid debug mask.
  375. * The mapping process involves mapping the human-inputted string
  376. * into a valid mask, and then rebuilding the string from the
  377. * verified valid mask.
  378. *
  379. * A service operation is required to set a new client-side
  380. * debug mask.
  381. */
  382. if (!strcmp(file->f_path.dentry->d_name.name,
  383. ORANGEFS_KMOD_DEBUG_FILE)) {
  384. debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
  385. debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
  386. debug_string = kernel_debug_string;
  387. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  388. "New kernel debug string is %s\n",
  389. kernel_debug_string);
  390. } else {
  391. /* Can't reset client debug mask if client is not running. */
  392. if (is_daemon_in_service()) {
  393. pr_info("%s: Client not running :%d:\n",
  394. __func__,
  395. is_daemon_in_service());
  396. goto out;
  397. }
  398. debug_string_to_mask(buf, &c_mask, 1);
  399. debug_mask_to_string(&c_mask, 1);
  400. debug_string = client_debug_string;
  401. new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
  402. if (!new_op) {
  403. pr_info("%s: op_alloc failed!\n", __func__);
  404. goto out;
  405. }
  406. new_op->upcall.req.param.op =
  407. ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
  408. new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
  409. memset(new_op->upcall.req.param.s_value,
  410. 0,
  411. ORANGEFS_MAX_DEBUG_STRING_LEN);
  412. sprintf(new_op->upcall.req.param.s_value,
  413. "%llx %llx\n",
  414. c_mask.mask1,
  415. c_mask.mask2);
  416. /* service_operation returns 0 on success... */
  417. rc = service_operation(new_op,
  418. "orangefs_param",
  419. ORANGEFS_OP_INTERRUPTIBLE);
  420. if (rc)
  421. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  422. "%s: service_operation failed! rc:%d:\n",
  423. __func__,
  424. rc);
  425. op_release(new_op);
  426. }
  427. mutex_lock(&orangefs_debug_lock);
  428. memset(file->f_inode->i_private, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  429. sprintf((char *)file->f_inode->i_private, "%s\n", debug_string);
  430. mutex_unlock(&orangefs_debug_lock);
  431. *ppos += count;
  432. if (silly)
  433. rc = silly;
  434. else
  435. rc = count;
  436. out:
  437. gossip_debug(GOSSIP_DEBUGFS_DEBUG,
  438. "orangefs_debug_write: rc: %d\n",
  439. rc);
  440. kfree(buf);
  441. return rc;
  442. }
  443. /*
  444. * After obtaining a string representation of the client's debug
  445. * keywords and their associated masks, this function is called to build an
  446. * array of these values.
  447. */
  448. static int orangefs_prepare_cdm_array(char *debug_array_string)
  449. {
  450. int i;
  451. int rc = -EINVAL;
  452. char *cds_head = NULL;
  453. char *cds_delimiter = NULL;
  454. int keyword_len = 0;
  455. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  456. /*
  457. * figure out how many elements the cdm_array needs.
  458. */
  459. for (i = 0; i < strlen(debug_array_string); i++)
  460. if (debug_array_string[i] == '\n')
  461. cdm_element_count++;
  462. if (!cdm_element_count) {
  463. pr_info("No elements in client debug array string!\n");
  464. goto out;
  465. }
  466. cdm_array =
  467. kzalloc(cdm_element_count * sizeof(struct client_debug_mask),
  468. GFP_KERNEL);
  469. if (!cdm_array) {
  470. pr_info("malloc failed for cdm_array!\n");
  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. }
  581. rc = 0;
  582. out: return rc;
  583. }
  584. /*
  585. * kernel = type 0
  586. * client = type 1
  587. */
  588. static void debug_mask_to_string(void *mask, int type)
  589. {
  590. int i;
  591. int len = 0;
  592. char *debug_string;
  593. int element_count = 0;
  594. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  595. if (type) {
  596. debug_string = client_debug_string;
  597. element_count = cdm_element_count;
  598. } else {
  599. debug_string = kernel_debug_string;
  600. element_count = num_kmod_keyword_mask_map;
  601. }
  602. memset(debug_string, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  603. /*
  604. * Some keywords, like "all" or "verbose", are amalgams of
  605. * numerous other keywords. Make a special check for those
  606. * before grinding through the whole mask only to find out
  607. * later...
  608. */
  609. if (check_amalgam_keyword(mask, type))
  610. goto out;
  611. /* Build the debug string. */
  612. for (i = 0; i < element_count; i++)
  613. if (type)
  614. do_c_string(mask, i);
  615. else
  616. do_k_string(mask, i);
  617. len = strlen(debug_string);
  618. if ((len) && (type))
  619. client_debug_string[len - 1] = '\0';
  620. else if (len)
  621. kernel_debug_string[len - 1] = '\0';
  622. else if (type)
  623. strcpy(client_debug_string, "none");
  624. else
  625. strcpy(kernel_debug_string, "none");
  626. out:
  627. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
  628. return;
  629. }
  630. static void do_k_string(void *k_mask, int index)
  631. {
  632. __u64 *mask = (__u64 *) k_mask;
  633. if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
  634. goto out;
  635. if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
  636. if ((strlen(kernel_debug_string) +
  637. strlen(s_kmod_keyword_mask_map[index].keyword))
  638. < ORANGEFS_MAX_DEBUG_STRING_LEN - 1) {
  639. strcat(kernel_debug_string,
  640. s_kmod_keyword_mask_map[index].keyword);
  641. strcat(kernel_debug_string, ",");
  642. } else {
  643. gossip_err("%s: overflow!\n", __func__);
  644. strcpy(kernel_debug_string, ORANGEFS_ALL);
  645. goto out;
  646. }
  647. }
  648. out:
  649. return;
  650. }
  651. static void do_c_string(void *c_mask, int index)
  652. {
  653. struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
  654. if (keyword_is_amalgam(cdm_array[index].keyword))
  655. goto out;
  656. if ((mask->mask1 & cdm_array[index].mask1) ||
  657. (mask->mask2 & cdm_array[index].mask2)) {
  658. if ((strlen(client_debug_string) +
  659. strlen(cdm_array[index].keyword) + 1)
  660. < ORANGEFS_MAX_DEBUG_STRING_LEN - 2) {
  661. strcat(client_debug_string,
  662. cdm_array[index].keyword);
  663. strcat(client_debug_string, ",");
  664. } else {
  665. gossip_err("%s: overflow!\n", __func__);
  666. strcpy(client_debug_string, ORANGEFS_ALL);
  667. goto out;
  668. }
  669. }
  670. out:
  671. return;
  672. }
  673. static int keyword_is_amalgam(char *keyword)
  674. {
  675. int rc = 0;
  676. if ((!strcmp(keyword, ORANGEFS_ALL)) || (!strcmp(keyword, ORANGEFS_VERBOSE)))
  677. rc = 1;
  678. return rc;
  679. }
  680. /*
  681. * kernel = type 0
  682. * client = type 1
  683. *
  684. * return 1 if we found an amalgam.
  685. */
  686. static int check_amalgam_keyword(void *mask, int type)
  687. {
  688. __u64 *k_mask;
  689. struct client_debug_mask *c_mask;
  690. int k_all_index = num_kmod_keyword_mask_map - 1;
  691. int rc = 0;
  692. if (type) {
  693. c_mask = (struct client_debug_mask *) mask;
  694. if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
  695. (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
  696. strcpy(client_debug_string, ORANGEFS_ALL);
  697. rc = 1;
  698. goto out;
  699. }
  700. if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
  701. (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
  702. strcpy(client_debug_string, ORANGEFS_VERBOSE);
  703. rc = 1;
  704. goto out;
  705. }
  706. } else {
  707. k_mask = (__u64 *) mask;
  708. if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
  709. strcpy(kernel_debug_string, ORANGEFS_ALL);
  710. rc = 1;
  711. goto out;
  712. }
  713. }
  714. out:
  715. return rc;
  716. }
  717. /*
  718. * kernel = type 0
  719. * client = type 1
  720. */
  721. static void debug_string_to_mask(char *debug_string, void *mask, int type)
  722. {
  723. char *unchecked_keyword;
  724. int i;
  725. char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
  726. char *original_pointer;
  727. int element_count = 0;
  728. struct client_debug_mask *c_mask = NULL;
  729. __u64 *k_mask = NULL;
  730. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  731. if (type) {
  732. c_mask = (struct client_debug_mask *)mask;
  733. element_count = cdm_element_count;
  734. } else {
  735. k_mask = (__u64 *)mask;
  736. *k_mask = 0;
  737. element_count = num_kmod_keyword_mask_map;
  738. }
  739. original_pointer = strsep_fodder;
  740. while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
  741. if (strlen(unchecked_keyword)) {
  742. for (i = 0; i < element_count; i++)
  743. if (type)
  744. do_c_mask(i,
  745. unchecked_keyword,
  746. &c_mask);
  747. else
  748. do_k_mask(i,
  749. unchecked_keyword,
  750. &k_mask);
  751. }
  752. kfree(original_pointer);
  753. }
  754. static void do_c_mask(int i, char *unchecked_keyword,
  755. struct client_debug_mask **sane_mask)
  756. {
  757. if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
  758. (**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
  759. (**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
  760. }
  761. }
  762. static void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
  763. {
  764. if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
  765. **sane_mask = (**sane_mask) |
  766. s_kmod_keyword_mask_map[i].mask_val;
  767. }
  768. int orangefs_debugfs_new_client_mask(void __user *arg)
  769. {
  770. struct dev_mask2_info_s mask2_info = {0};
  771. int ret;
  772. ret = copy_from_user(&mask2_info,
  773. (void __user *)arg,
  774. sizeof(struct dev_mask2_info_s));
  775. if (ret != 0)
  776. return -EIO;
  777. client_debug_mask.mask1 = mask2_info.mask1_value;
  778. client_debug_mask.mask2 = mask2_info.mask2_value;
  779. pr_info("%s: client debug mask has been been received "
  780. ":%llx: :%llx:\n",
  781. __func__,
  782. (unsigned long long)client_debug_mask.mask1,
  783. (unsigned long long)client_debug_mask.mask2);
  784. return ret;
  785. }
  786. int orangefs_debugfs_new_client_string(void __user *arg)
  787. {
  788. int ret;
  789. ret = copy_from_user(&client_debug_array_string,
  790. (void __user *)arg,
  791. ORANGEFS_MAX_DEBUG_STRING_LEN);
  792. if (ret != 0) {
  793. pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
  794. __func__);
  795. return -EFAULT;
  796. }
  797. /*
  798. * The real client-core makes an effort to ensure
  799. * that actual strings that aren't too long to fit in
  800. * this buffer is what we get here. We're going to use
  801. * string functions on the stuff we got, so we'll make
  802. * this extra effort to try and keep from
  803. * flowing out of this buffer when we use the string
  804. * functions, even if somehow the stuff we end up
  805. * with here is garbage.
  806. */
  807. client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN - 1] =
  808. '\0';
  809. pr_info("%s: client debug array string has been received.\n",
  810. __func__);
  811. if (!help_string_initialized) {
  812. /* Build a proper debug help string. */
  813. ret = orangefs_prepare_debugfs_help_string(0);
  814. if (ret) {
  815. gossip_err("%s: no debug help string \n",
  816. __func__);
  817. return ret;
  818. }
  819. }
  820. debug_mask_to_string(&client_debug_mask, 1);
  821. debugfs_remove(client_debug_dentry);
  822. orangefs_client_debug_init();
  823. help_string_initialized++;
  824. return 0;
  825. }
  826. int orangefs_debugfs_new_debug(void __user *arg)
  827. {
  828. struct dev_mask_info_s mask_info = {0};
  829. int ret;
  830. ret = copy_from_user(&mask_info,
  831. (void __user *)arg,
  832. sizeof(mask_info));
  833. if (ret != 0)
  834. return -EIO;
  835. if (mask_info.mask_type == KERNEL_MASK) {
  836. if ((mask_info.mask_value == 0)
  837. && (kernel_mask_set_mod_init)) {
  838. /*
  839. * the kernel debug mask was set when the
  840. * kernel module was loaded; don't override
  841. * it if the client-core was started without
  842. * a value for ORANGEFS_KMODMASK.
  843. */
  844. return 0;
  845. }
  846. debug_mask_to_string(&mask_info.mask_value,
  847. mask_info.mask_type);
  848. orangefs_gossip_debug_mask = mask_info.mask_value;
  849. pr_info("%s: kernel debug mask has been modified to "
  850. ":%s: :%llx:\n",
  851. __func__,
  852. kernel_debug_string,
  853. (unsigned long long)orangefs_gossip_debug_mask);
  854. } else if (mask_info.mask_type == CLIENT_MASK) {
  855. debug_mask_to_string(&mask_info.mask_value,
  856. mask_info.mask_type);
  857. pr_info("%s: client debug mask has been modified to"
  858. ":%s: :%llx:\n",
  859. __func__,
  860. client_debug_string,
  861. llu(mask_info.mask_value));
  862. } else {
  863. gossip_lerr("Invalid mask type....\n");
  864. return -EINVAL;
  865. }
  866. return ret;
  867. }