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