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