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 
40 #include <linux/uaccess.h>
41 
42 #include "orangefs-debugfs.h"
43 #include "protocol.h"
44 #include "orangefs-kernel.h"
45 
46 #define DEBUG_HELP_STRING_SIZE 4096
47 #define HELP_STRING_UNINITIALIZED \
48 	"Client Debug Keywords are unknown until the first time\n" \
49 	"the client is started after boot.\n"
50 #define ORANGEFS_KMOD_DEBUG_HELP_FILE "debug-help"
51 #define ORANGEFS_KMOD_DEBUG_FILE "kernel-debug"
52 #define ORANGEFS_CLIENT_DEBUG_FILE "client-debug"
53 #define ORANGEFS_VERBOSE "verbose"
54 #define ORANGEFS_ALL "all"
55 
56 /*
57  * An array of client_debug_mask will be built to hold debug keyword/mask
58  * values fetched from userspace.
59  */
60 struct client_debug_mask {
61 	char *keyword;
62 	__u64 mask1;
63 	__u64 mask2;
64 };
65 
66 static int orangefs_kernel_debug_init(void);
67 
68 static int orangefs_debug_help_open(struct inode *, struct file *);
69 static void *help_start(struct seq_file *, loff_t *);
70 static void *help_next(struct seq_file *, void *, loff_t *);
71 static void help_stop(struct seq_file *, void *);
72 static int help_show(struct seq_file *, void *);
73 
74 static int orangefs_debug_open(struct inode *, struct file *);
75 
76 static ssize_t orangefs_debug_read(struct file *,
77 				 char __user *,
78 				 size_t,
79 				 loff_t *);
80 
81 static ssize_t orangefs_debug_write(struct file *,
82 				  const char __user *,
83 				  size_t,
84 				  loff_t *);
85 
86 static int orangefs_prepare_cdm_array(char *);
87 static void debug_mask_to_string(void *, int);
88 static void do_k_string(void *, int);
89 static void do_c_string(void *, int);
90 static int keyword_is_amalgam(char *);
91 static int check_amalgam_keyword(void *, int);
92 static void debug_string_to_mask(char *, void *, int);
93 static void do_c_mask(int, char *, struct client_debug_mask **);
94 static void do_k_mask(int, char *, __u64 **);
95 
96 static char kernel_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN] = "none";
97 static char *debug_help_string;
98 static char client_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
99 static char client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
100 
101 static struct dentry *help_file_dentry;
102 static struct dentry *client_debug_dentry;
103 static struct dentry *debug_dir;
104 
105 static unsigned int kernel_mask_set_mod_init;
106 static int orangefs_debug_disabled = 1;
107 static int help_string_initialized;
108 
109 static const struct seq_operations help_debug_ops = {
110 	.start	= help_start,
111 	.next	= help_next,
112 	.stop	= help_stop,
113 	.show	= help_show,
114 };
115 
116 const struct file_operations debug_help_fops = {
117 	.owner		= THIS_MODULE,
118 	.open           = orangefs_debug_help_open,
119 	.read           = seq_read,
120 	.release        = seq_release,
121 	.llseek         = seq_lseek,
122 };
123 
124 static const struct file_operations kernel_debug_fops = {
125 	.owner		= THIS_MODULE,
126 	.open           = orangefs_debug_open,
127 	.read           = orangefs_debug_read,
128 	.write		= orangefs_debug_write,
129 	.llseek         = generic_file_llseek,
130 };
131 
132 static int client_all_index;
133 static int client_verbose_index;
134 
135 static struct client_debug_mask *cdm_array;
136 static int cdm_element_count;
137 
138 static struct client_debug_mask client_debug_mask;
139 
140 /*
141  * Used to protect data in ORANGEFS_KMOD_DEBUG_FILE and
142  * ORANGEFS_KMOD_DEBUG_FILE.
143  */
144 static DEFINE_MUTEX(orangefs_debug_lock);
145 
146 /* Used to protect data in ORANGEFS_KMOD_DEBUG_HELP_FILE */
147 static DEFINE_MUTEX(orangefs_help_file_lock);
148 
149 /*
150  * initialize kmod debug operations, create orangefs debugfs dir and
151  * ORANGEFS_KMOD_DEBUG_HELP_FILE.
152  */
153 int orangefs_debugfs_init(int debug_mask)
154 {
155 	int rc = -ENOMEM;
156 
157 	/* convert input debug mask to a 64-bit unsigned integer */
158         orangefs_gossip_debug_mask = (unsigned long long)debug_mask;
159 
160 	/*
161 	 * set the kernel's gossip debug string; invalid mask values will
162 	 * be ignored.
163 	 */
164 	debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
165 
166 	/* remove any invalid values from the mask */
167 	debug_string_to_mask(kernel_debug_string, &orangefs_gossip_debug_mask,
168 	    0);
169 
170 	/*
171 	 * if the mask has a non-zero value, then indicate that the mask
172 	 * was set when the kernel module was loaded.  The orangefs dev ioctl
173 	 * command will look at this boolean to determine if the kernel's
174 	 * debug mask should be overwritten when the client-core is started.
175 	 */
176 	if (orangefs_gossip_debug_mask != 0)
177 		kernel_mask_set_mod_init = true;
178 
179 	pr_info("%s: called with debug mask: :%s: :%llx:\n",
180 		__func__,
181 		kernel_debug_string,
182 		(unsigned long long)orangefs_gossip_debug_mask);
183 
184 	debug_dir = debugfs_create_dir("orangefs", NULL);
185 	if (!debug_dir) {
186 		pr_info("%s: debugfs_create_dir failed.\n", __func__);
187 		goto out;
188 	}
189 
190 	help_file_dentry = debugfs_create_file(ORANGEFS_KMOD_DEBUG_HELP_FILE,
191 				  0444,
192 				  debug_dir,
193 				  debug_help_string,
194 				  &debug_help_fops);
195 	if (!help_file_dentry) {
196 		pr_info("%s: debugfs_create_file failed.\n", __func__);
197 		goto out;
198 	}
199 
200 	orangefs_debug_disabled = 0;
201 
202 	rc = orangefs_kernel_debug_init();
203 
204 out:
205 
206 	return rc;
207 }
208 
209 /*
210  * initialize the kernel-debug file.
211  */
212 static int orangefs_kernel_debug_init(void)
213 {
214 	int rc = -ENOMEM;
215 	struct dentry *ret;
216 	char *k_buffer = NULL;
217 
218 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
219 
220 	k_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
221 	if (!k_buffer)
222 		goto out;
223 
224 	if (strlen(kernel_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
225 		strcpy(k_buffer, kernel_debug_string);
226 		strcat(k_buffer, "\n");
227 	} else {
228 		strcpy(k_buffer, "none\n");
229 		pr_info("%s: overflow 1!\n", __func__);
230 	}
231 
232 	ret = debugfs_create_file(ORANGEFS_KMOD_DEBUG_FILE,
233 				  0444,
234 				  debug_dir,
235 				  k_buffer,
236 				  &kernel_debug_fops);
237 	if (!ret) {
238 		pr_info("%s: failed to create %s.\n",
239 			__func__,
240 			ORANGEFS_KMOD_DEBUG_FILE);
241 		goto out;
242 	}
243 
244 	rc = 0;
245 
246 out:
247 
248 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
249 	return rc;
250 }
251 
252 
253 void orangefs_debugfs_cleanup(void)
254 {
255 	debugfs_remove_recursive(debug_dir);
256 }
257 
258 /* open ORANGEFS_KMOD_DEBUG_HELP_FILE */
259 static int orangefs_debug_help_open(struct inode *inode, struct file *file)
260 {
261 	int rc = -ENODEV;
262 	int ret;
263 
264 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
265 		     "orangefs_debug_help_open: start\n");
266 
267 	if (orangefs_debug_disabled)
268 		goto out;
269 
270 	ret = seq_open(file, &help_debug_ops);
271 	if (ret)
272 		goto out;
273 
274 	((struct seq_file *)(file->private_data))->private = inode->i_private;
275 
276 	rc = 0;
277 
278 out:
279 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
280 		     "orangefs_debug_help_open: rc:%d:\n",
281 		     rc);
282 	return rc;
283 }
284 
285 /*
286  * I think start always gets called again after stop. Start
287  * needs to return NULL when it is done. The whole "payload"
288  * in this case is a single (long) string, so by the second
289  * time we get to start (pos = 1), we're done.
290  */
291 static void *help_start(struct seq_file *m, loff_t *pos)
292 {
293 	void *payload = NULL;
294 
295 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_start: start\n");
296 
297 	mutex_lock(&orangefs_help_file_lock);
298 
299 	if (*pos == 0)
300 		payload = m->private;
301 
302 	return payload;
303 }
304 
305 static void *help_next(struct seq_file *m, void *v, loff_t *pos)
306 {
307 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
308 
309 	return NULL;
310 }
311 
312 static void help_stop(struct seq_file *m, void *p)
313 {
314 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_stop: start\n");
315 	mutex_unlock(&orangefs_help_file_lock);
316 }
317 
318 static int help_show(struct seq_file *m, void *v)
319 {
320 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_show: start\n");
321 
322 	seq_puts(m, v);
323 
324 	return 0;
325 }
326 
327 /*
328  * initialize the client-debug file.
329  */
330 int orangefs_client_debug_init(void)
331 {
332 
333 	int rc = -ENOMEM;
334 	char *c_buffer = NULL;
335 
336 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
337 
338 	c_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
339 	if (!c_buffer)
340 		goto out;
341 
342 	if (strlen(client_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
343 		strcpy(c_buffer, client_debug_string);
344 		strcat(c_buffer, "\n");
345 	} else {
346 		strcpy(c_buffer, "none\n");
347 		pr_info("%s: overflow! 2\n", __func__);
348 	}
349 
350 	client_debug_dentry = debugfs_create_file(ORANGEFS_CLIENT_DEBUG_FILE,
351 						  0444,
352 						  debug_dir,
353 						  c_buffer,
354 						  &kernel_debug_fops);
355 	if (!client_debug_dentry) {
356 		pr_info("%s: failed to create updated %s.\n",
357 			__func__,
358 			ORANGEFS_CLIENT_DEBUG_FILE);
359 		goto out;
360 	}
361 
362 	rc = 0;
363 
364 out:
365 
366 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
367 	return rc;
368 }
369 
370 /* open ORANGEFS_KMOD_DEBUG_FILE or ORANGEFS_CLIENT_DEBUG_FILE.*/
371 static int orangefs_debug_open(struct inode *inode, struct file *file)
372 {
373 	int rc = -ENODEV;
374 
375 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
376 		     "%s: orangefs_debug_disabled: %d\n",
377 		     __func__,
378 		     orangefs_debug_disabled);
379 
380 	if (orangefs_debug_disabled)
381 		goto out;
382 
383 	rc = 0;
384 	mutex_lock(&orangefs_debug_lock);
385 	file->private_data = inode->i_private;
386 	mutex_unlock(&orangefs_debug_lock);
387 
388 out:
389 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
390 		     "orangefs_debug_open: rc: %d\n",
391 		     rc);
392 	return rc;
393 }
394 
395 static ssize_t orangefs_debug_read(struct file *file,
396 				 char __user *ubuf,
397 				 size_t count,
398 				 loff_t *ppos)
399 {
400 	char *buf;
401 	int sprintf_ret;
402 	ssize_t read_ret = -ENOMEM;
403 
404 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "orangefs_debug_read: start\n");
405 
406 	buf = kmalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
407 	if (!buf)
408 		goto out;
409 
410 	mutex_lock(&orangefs_debug_lock);
411 	sprintf_ret = sprintf(buf, "%s", (char *)file->private_data);
412 	mutex_unlock(&orangefs_debug_lock);
413 
414 	read_ret = simple_read_from_buffer(ubuf, count, ppos, buf, sprintf_ret);
415 
416 	kfree(buf);
417 
418 out:
419 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
420 		     "orangefs_debug_read: ret: %zu\n",
421 		     read_ret);
422 
423 	return read_ret;
424 }
425 
426 static ssize_t orangefs_debug_write(struct file *file,
427 				  const char __user *ubuf,
428 				  size_t count,
429 				  loff_t *ppos)
430 {
431 	char *buf;
432 	int rc = -EFAULT;
433 	size_t silly = 0;
434 	char *debug_string;
435 	struct orangefs_kernel_op_s *new_op = NULL;
436 	struct client_debug_mask c_mask = { NULL, 0, 0 };
437 	char *s;
438 
439 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
440 		"orangefs_debug_write: %pD\n",
441 		file);
442 
443 	if (count == 0)
444 		return 0;
445 
446 	/*
447 	 * Thwart users who try to jamb a ridiculous number
448 	 * of bytes into the debug file...
449 	 */
450 	if (count > ORANGEFS_MAX_DEBUG_STRING_LEN + 1) {
451 		silly = count;
452 		count = ORANGEFS_MAX_DEBUG_STRING_LEN + 1;
453 	}
454 
455 	buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
456 	if (!buf)
457 		goto out;
458 
459 	if (copy_from_user(buf, ubuf, count - 1)) {
460 		gossip_debug(GOSSIP_DEBUGFS_DEBUG,
461 			     "%s: copy_from_user failed!\n",
462 			     __func__);
463 		goto out;
464 	}
465 
466 	/*
467 	 * Map the keyword string from userspace into a valid debug mask.
468 	 * The mapping process involves mapping the human-inputted string
469 	 * into a valid mask, and then rebuilding the string from the
470 	 * verified valid mask.
471 	 *
472 	 * A service operation is required to set a new client-side
473 	 * debug mask.
474 	 */
475 	if (!strcmp(file->f_path.dentry->d_name.name,
476 		    ORANGEFS_KMOD_DEBUG_FILE)) {
477 		debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
478 		debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
479 		debug_string = kernel_debug_string;
480 		gossip_debug(GOSSIP_DEBUGFS_DEBUG,
481 			     "New kernel debug string is %s\n",
482 			     kernel_debug_string);
483 	} else {
484 		/* Can't reset client debug mask if client is not running. */
485 		if (is_daemon_in_service()) {
486 			pr_info("%s: Client not running :%d:\n",
487 				__func__,
488 				is_daemon_in_service());
489 			goto out;
490 		}
491 
492 		debug_string_to_mask(buf, &c_mask, 1);
493 		debug_mask_to_string(&c_mask, 1);
494 		debug_string = client_debug_string;
495 
496 		new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
497 		if (!new_op) {
498 			pr_info("%s: op_alloc failed!\n", __func__);
499 			goto out;
500 		}
501 
502 		new_op->upcall.req.param.op =
503 			ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
504 		new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
505 		memset(new_op->upcall.req.param.s_value,
506 		       0,
507 		       ORANGEFS_MAX_DEBUG_STRING_LEN);
508 		sprintf(new_op->upcall.req.param.s_value,
509 			"%llx %llx\n",
510 			c_mask.mask1,
511 			c_mask.mask2);
512 
513 		/* service_operation returns 0 on success... */
514 		rc = service_operation(new_op,
515 				       "orangefs_param",
516 					ORANGEFS_OP_INTERRUPTIBLE);
517 
518 		if (rc)
519 			gossip_debug(GOSSIP_DEBUGFS_DEBUG,
520 				     "%s: service_operation failed! rc:%d:\n",
521 				     __func__,
522 				     rc);
523 
524 		op_release(new_op);
525 	}
526 
527 	mutex_lock(&orangefs_debug_lock);
528 	s = file_inode(file)->i_private;
529 	memset(s, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
530 	sprintf(s, "%s\n", debug_string);
531 	mutex_unlock(&orangefs_debug_lock);
532 
533 	*ppos += count;
534 	if (silly)
535 		rc = silly;
536 	else
537 		rc = count;
538 
539 out:
540 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
541 		     "orangefs_debug_write: rc: %d\n",
542 		     rc);
543 	kfree(buf);
544 	return rc;
545 }
546 
547 /*
548  * After obtaining a string representation of the client's debug
549  * keywords and their associated masks, this function is called to build an
550  * array of these values.
551  */
552 static int orangefs_prepare_cdm_array(char *debug_array_string)
553 {
554 	int i;
555 	int rc = -EINVAL;
556 	char *cds_head = NULL;
557 	char *cds_delimiter = NULL;
558 	int keyword_len = 0;
559 
560 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
561 
562 	/*
563 	 * figure out how many elements the cdm_array needs.
564 	 */
565 	for (i = 0; i < strlen(debug_array_string); i++)
566 		if (debug_array_string[i] == '\n')
567 			cdm_element_count++;
568 
569 	if (!cdm_element_count) {
570 		pr_info("No elements in client debug array string!\n");
571 		goto out;
572 	}
573 
574 	cdm_array =
575 		kzalloc(cdm_element_count * sizeof(struct client_debug_mask),
576 			GFP_KERNEL);
577 	if (!cdm_array) {
578 		pr_info("malloc failed for cdm_array!\n");
579 		rc = -ENOMEM;
580 		goto out;
581 	}
582 
583 	cds_head = debug_array_string;
584 
585 	for (i = 0; i < cdm_element_count; i++) {
586 		cds_delimiter = strchr(cds_head, '\n');
587 		*cds_delimiter = '\0';
588 
589 		keyword_len = strcspn(cds_head, " ");
590 
591 		cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
592 		if (!cdm_array[i].keyword) {
593 			rc = -ENOMEM;
594 			goto out;
595 		}
596 
597 		sscanf(cds_head,
598 		       "%s %llx %llx",
599 		       cdm_array[i].keyword,
600 		       (unsigned long long *)&(cdm_array[i].mask1),
601 		       (unsigned long long *)&(cdm_array[i].mask2));
602 
603 		if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
604 			client_verbose_index = i;
605 
606 		if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
607 			client_all_index = i;
608 
609 		cds_head = cds_delimiter + 1;
610 	}
611 
612 	rc = cdm_element_count;
613 
614 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
615 
616 out:
617 
618 	return rc;
619 
620 }
621 
622 /*
623  * /sys/kernel/debug/orangefs/debug-help can be catted to
624  * see all the available kernel and client debug keywords.
625  *
626  * When orangefs.ko initializes, we have no idea what keywords the
627  * client supports, nor their associated masks.
628  *
629  * We pass through this function once at module-load and stamp a
630  * boilerplate "we don't know" message for the client in the
631  * debug-help file. We pass through here again when the client
632  * starts and then we can fill out the debug-help file fully.
633  *
634  * The client might be restarted any number of times between
635  * module reloads, we only build the debug-help file the first time.
636  */
637 int orangefs_prepare_debugfs_help_string(int at_boot)
638 {
639 	char *client_title = "Client Debug Keywords:\n";
640 	char *kernel_title = "Kernel Debug Keywords:\n";
641 	size_t string_size =  DEBUG_HELP_STRING_SIZE;
642 	size_t result_size;
643 	size_t i;
644 	char *new;
645 	int rc = -EINVAL;
646 
647 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
648 
649 	if (at_boot)
650 		client_title = HELP_STRING_UNINITIALIZED;
651 
652 	/* build a new debug_help_string. */
653 	new = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
654 	if (!new) {
655 		rc = -ENOMEM;
656 		goto out;
657 	}
658 
659 	/*
660 	 * strlcat(dst, src, size) will append at most
661 	 * "size - strlen(dst) - 1" bytes of src onto dst,
662 	 * null terminating the result, and return the total
663 	 * length of the string it tried to create.
664 	 *
665 	 * We'll just plow through here building our new debug
666 	 * help string and let strlcat take care of assuring that
667 	 * dst doesn't overflow.
668 	 */
669 	strlcat(new, client_title, string_size);
670 
671 	if (!at_boot) {
672 
673                 /*
674 		 * fill the client keyword/mask array and remember
675 		 * how many elements there were.
676 		 */
677 		cdm_element_count =
678 			orangefs_prepare_cdm_array(client_debug_array_string);
679 		if (cdm_element_count <= 0) {
680 			kfree(new);
681 			goto out;
682 		}
683 
684 		for (i = 0; i < cdm_element_count; i++) {
685 			strlcat(new, "\t", string_size);
686 			strlcat(new, cdm_array[i].keyword, string_size);
687 			strlcat(new, "\n", string_size);
688 		}
689 	}
690 
691 	strlcat(new, "\n", string_size);
692 	strlcat(new, kernel_title, string_size);
693 
694 	for (i = 0; i < num_kmod_keyword_mask_map; i++) {
695 		strlcat(new, "\t", string_size);
696 		strlcat(new, s_kmod_keyword_mask_map[i].keyword, string_size);
697 		result_size = strlcat(new, "\n", string_size);
698 	}
699 
700 	/* See if we tried to put too many bytes into "new"... */
701 	if (result_size >= string_size) {
702 		kfree(new);
703 		goto out;
704 	}
705 
706 	if (at_boot) {
707 		debug_help_string = new;
708 	} else {
709 		mutex_lock(&orangefs_help_file_lock);
710 		memset(debug_help_string, 0, DEBUG_HELP_STRING_SIZE);
711 		strlcat(debug_help_string, new, string_size);
712 		mutex_unlock(&orangefs_help_file_lock);
713 	}
714 
715 	rc = 0;
716 
717 out:	return rc;
718 
719 }
720 
721 /*
722  * kernel = type 0
723  * client = type 1
724  */
725 static void debug_mask_to_string(void *mask, int type)
726 {
727 	int i;
728 	int len = 0;
729 	char *debug_string;
730 	int element_count = 0;
731 
732 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
733 
734 	if (type) {
735 		debug_string = client_debug_string;
736 		element_count = cdm_element_count;
737 	} else {
738 		debug_string = kernel_debug_string;
739 		element_count = num_kmod_keyword_mask_map;
740 	}
741 
742 	memset(debug_string, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
743 
744 	/*
745 	 * Some keywords, like "all" or "verbose", are amalgams of
746 	 * numerous other keywords. Make a special check for those
747 	 * before grinding through the whole mask only to find out
748 	 * later...
749 	 */
750 	if (check_amalgam_keyword(mask, type))
751 		goto out;
752 
753 	/* Build the debug string. */
754 	for (i = 0; i < element_count; i++)
755 		if (type)
756 			do_c_string(mask, i);
757 		else
758 			do_k_string(mask, i);
759 
760 	len = strlen(debug_string);
761 
762 	if ((len) && (type))
763 		client_debug_string[len - 1] = '\0';
764 	else if (len)
765 		kernel_debug_string[len - 1] = '\0';
766 	else if (type)
767 		strcpy(client_debug_string, "none");
768 	else
769 		strcpy(kernel_debug_string, "none");
770 
771 out:
772 gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
773 
774 	return;
775 
776 }
777 
778 static void do_k_string(void *k_mask, int index)
779 {
780 	__u64 *mask = (__u64 *) k_mask;
781 
782 	if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
783 		goto out;
784 
785 	if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
786 		if ((strlen(kernel_debug_string) +
787 		     strlen(s_kmod_keyword_mask_map[index].keyword))
788 			< ORANGEFS_MAX_DEBUG_STRING_LEN - 1) {
789 				strcat(kernel_debug_string,
790 				       s_kmod_keyword_mask_map[index].keyword);
791 				strcat(kernel_debug_string, ",");
792 			} else {
793 				gossip_err("%s: overflow!\n", __func__);
794 				strcpy(kernel_debug_string, ORANGEFS_ALL);
795 				goto out;
796 			}
797 	}
798 
799 out:
800 
801 	return;
802 }
803 
804 static void do_c_string(void *c_mask, int index)
805 {
806 	struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
807 
808 	if (keyword_is_amalgam(cdm_array[index].keyword))
809 		goto out;
810 
811 	if ((mask->mask1 & cdm_array[index].mask1) ||
812 	    (mask->mask2 & cdm_array[index].mask2)) {
813 		if ((strlen(client_debug_string) +
814 		     strlen(cdm_array[index].keyword) + 1)
815 			< ORANGEFS_MAX_DEBUG_STRING_LEN - 2) {
816 				strcat(client_debug_string,
817 				       cdm_array[index].keyword);
818 				strcat(client_debug_string, ",");
819 			} else {
820 				gossip_err("%s: overflow!\n", __func__);
821 				strcpy(client_debug_string, ORANGEFS_ALL);
822 				goto out;
823 			}
824 	}
825 out:
826 	return;
827 }
828 
829 static int keyword_is_amalgam(char *keyword)
830 {
831 	int rc = 0;
832 
833 	if ((!strcmp(keyword, ORANGEFS_ALL)) || (!strcmp(keyword, ORANGEFS_VERBOSE)))
834 		rc = 1;
835 
836 	return rc;
837 }
838 
839 /*
840  * kernel = type 0
841  * client = type 1
842  *
843  * return 1 if we found an amalgam.
844  */
845 static int check_amalgam_keyword(void *mask, int type)
846 {
847 	__u64 *k_mask;
848 	struct client_debug_mask *c_mask;
849 	int k_all_index = num_kmod_keyword_mask_map - 1;
850 	int rc = 0;
851 
852 	if (type) {
853 		c_mask = (struct client_debug_mask *) mask;
854 
855 		if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
856 		    (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
857 			strcpy(client_debug_string, ORANGEFS_ALL);
858 			rc = 1;
859 			goto out;
860 		}
861 
862 		if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
863 		    (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
864 			strcpy(client_debug_string, ORANGEFS_VERBOSE);
865 			rc = 1;
866 			goto out;
867 		}
868 
869 	} else {
870 		k_mask = (__u64 *) mask;
871 
872 		if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
873 			strcpy(kernel_debug_string, ORANGEFS_ALL);
874 			rc = 1;
875 			goto out;
876 		}
877 	}
878 
879 out:
880 
881 	return rc;
882 }
883 
884 /*
885  * kernel = type 0
886  * client = type 1
887  */
888 static void debug_string_to_mask(char *debug_string, void *mask, int type)
889 {
890 	char *unchecked_keyword;
891 	int i;
892 	char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
893 	char *original_pointer;
894 	int element_count = 0;
895 	struct client_debug_mask *c_mask = NULL;
896 	__u64 *k_mask = NULL;
897 
898 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
899 
900 	if (type) {
901 		c_mask = (struct client_debug_mask *)mask;
902 		element_count = cdm_element_count;
903 	} else {
904 		k_mask = (__u64 *)mask;
905 		*k_mask = 0;
906 		element_count = num_kmod_keyword_mask_map;
907 	}
908 
909 	original_pointer = strsep_fodder;
910 	while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
911 		if (strlen(unchecked_keyword)) {
912 			for (i = 0; i < element_count; i++)
913 				if (type)
914 					do_c_mask(i,
915 						  unchecked_keyword,
916 						  &c_mask);
917 				else
918 					do_k_mask(i,
919 						  unchecked_keyword,
920 						  &k_mask);
921 		}
922 
923 	kfree(original_pointer);
924 }
925 
926 static void do_c_mask(int i, char *unchecked_keyword,
927     struct client_debug_mask **sane_mask)
928 {
929 
930 	if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
931 		(**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
932 		(**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
933 	}
934 }
935 
936 static void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
937 {
938 
939 	if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
940 		**sane_mask = (**sane_mask) |
941 				s_kmod_keyword_mask_map[i].mask_val;
942 }
943 
944 int orangefs_debugfs_new_client_mask(void __user *arg)
945 {
946 	struct dev_mask2_info_s mask2_info = {0};
947 	int ret;
948 
949 	ret = copy_from_user(&mask2_info,
950 			     (void __user *)arg,
951 			     sizeof(struct dev_mask2_info_s));
952 
953 	if (ret != 0)
954 		return -EIO;
955 
956 	client_debug_mask.mask1 = mask2_info.mask1_value;
957 	client_debug_mask.mask2 = mask2_info.mask2_value;
958 
959 	pr_info("%s: client debug mask has been been received "
960 		":%llx: :%llx:\n",
961 		__func__,
962 		(unsigned long long)client_debug_mask.mask1,
963 		(unsigned long long)client_debug_mask.mask2);
964 
965 	return ret;
966 }
967 
968 int orangefs_debugfs_new_client_string(void __user *arg)
969 {
970 	int ret;
971 
972 	ret = copy_from_user(&client_debug_array_string,
973 			     (void __user *)arg,
974 			     ORANGEFS_MAX_DEBUG_STRING_LEN);
975 
976 	if (ret != 0) {
977 		pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
978 			__func__);
979 		return -EFAULT;
980 	}
981 
982 	/*
983 	 * The real client-core makes an effort to ensure
984 	 * that actual strings that aren't too long to fit in
985 	 * this buffer is what we get here. We're going to use
986 	 * string functions on the stuff we got, so we'll make
987 	 * this extra effort to try and keep from
988 	 * flowing out of this buffer when we use the string
989 	 * functions, even if somehow the stuff we end up
990 	 * with here is garbage.
991 	 */
992 	client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN - 1] =
993 		'\0';
994 
995 	pr_info("%s: client debug array string has been received.\n",
996 		__func__);
997 
998 	if (!help_string_initialized) {
999 
1000 		/* Build a proper debug help string. */
1001 		ret = orangefs_prepare_debugfs_help_string(0);
1002 		if (ret) {
1003 			gossip_err("%s: no debug help string \n",
1004 				   __func__);
1005 			return ret;
1006 		}
1007 
1008 	}
1009 
1010 	debug_mask_to_string(&client_debug_mask, 1);
1011 
1012 	debugfs_remove(client_debug_dentry);
1013 
1014 	orangefs_client_debug_init();
1015 
1016 	help_string_initialized++;
1017 
1018 	return 0;
1019 }
1020 
1021 int orangefs_debugfs_new_debug(void __user *arg)
1022 {
1023 	struct dev_mask_info_s mask_info = {0};
1024 	int ret;
1025 
1026 	ret = copy_from_user(&mask_info,
1027 			     (void __user *)arg,
1028 			     sizeof(mask_info));
1029 
1030 	if (ret != 0)
1031 		return -EIO;
1032 
1033 	if (mask_info.mask_type == KERNEL_MASK) {
1034 		if ((mask_info.mask_value == 0)
1035 		    && (kernel_mask_set_mod_init)) {
1036 			/*
1037 			 * the kernel debug mask was set when the
1038 			 * kernel module was loaded; don't override
1039 			 * it if the client-core was started without
1040 			 * a value for ORANGEFS_KMODMASK.
1041 			 */
1042 			return 0;
1043 		}
1044 		debug_mask_to_string(&mask_info.mask_value,
1045 				     mask_info.mask_type);
1046 		orangefs_gossip_debug_mask = mask_info.mask_value;
1047 		pr_info("%s: kernel debug mask has been modified to "
1048 			":%s: :%llx:\n",
1049 			__func__,
1050 			kernel_debug_string,
1051 			(unsigned long long)orangefs_gossip_debug_mask);
1052 	} else if (mask_info.mask_type == CLIENT_MASK) {
1053 		debug_mask_to_string(&mask_info.mask_value,
1054 				     mask_info.mask_type);
1055 		pr_info("%s: client debug mask has been modified to"
1056 			":%s: :%llx:\n",
1057 			__func__,
1058 			client_debug_string,
1059 			llu(mask_info.mask_value));
1060 	} else {
1061 		gossip_lerr("Invalid mask type....\n");
1062 		return -EINVAL;
1063 	}
1064 
1065 	return ret;
1066 }
1067