xref: /openbmc/linux/fs/orangefs/orangefs-debugfs.c (revision fd5e9fccbd504c5179ab57ff695c610bca8809d6)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2575e9461SMike Marshall /*
3575e9461SMike Marshall  * What:		/sys/kernel/debug/orangefs/debug-help
4575e9461SMike Marshall  * Date:		June 2015
5575e9461SMike Marshall  * Contact:		Mike Marshall <hubcap@omnibond.com>
6575e9461SMike Marshall  * Description:
7575e9461SMike Marshall  * 			List of client and kernel debug keywords.
8575e9461SMike Marshall  *
9575e9461SMike Marshall  *
10575e9461SMike Marshall  * What:		/sys/kernel/debug/orangefs/client-debug
11575e9461SMike Marshall  * Date:		June 2015
12575e9461SMike Marshall  * Contact:		Mike Marshall <hubcap@omnibond.com>
13575e9461SMike Marshall  * Description:
14575e9461SMike Marshall  * 			Debug setting for "the client", the userspace
15575e9461SMike Marshall  * 			helper for the kernel module.
16575e9461SMike Marshall  *
17575e9461SMike Marshall  *
18575e9461SMike Marshall  * What:		/sys/kernel/debug/orangefs/kernel-debug
19575e9461SMike Marshall  * Date:		June 2015
20575e9461SMike Marshall  * Contact:		Mike Marshall <hubcap@omnibond.com>
21575e9461SMike Marshall  * Description:
22575e9461SMike Marshall  * 			Debug setting for the orangefs kernel module.
23575e9461SMike Marshall  *
24575e9461SMike Marshall  * 			Any of the keywords, or comma-separated lists
25575e9461SMike Marshall  * 			of keywords, from debug-help can be catted to
26575e9461SMike Marshall  * 			client-debug or kernel-debug.
27575e9461SMike Marshall  *
28575e9461SMike Marshall  * 			"none", "all" and "verbose" are special keywords
29575e9461SMike Marshall  * 			for client-debug. Setting client-debug to "all"
30575e9461SMike Marshall  * 			is kind of like trying to drink water from a
31575e9461SMike Marshall  * 			fire hose, "verbose" triggers most of the same
32575e9461SMike Marshall  * 			output except for the constant flow of output
33575e9461SMike Marshall  * 			from the main wait loop.
34575e9461SMike Marshall  *
35575e9461SMike Marshall  * 			"none" and "all" are similar settings for kernel-debug
36575e9461SMike Marshall  * 			no need for a "verbose".
37575e9461SMike Marshall  */
38575e9461SMike Marshall #include <linux/debugfs.h>
39575e9461SMike Marshall #include <linux/slab.h>
40575e9461SMike Marshall 
41575e9461SMike Marshall #include <linux/uaccess.h>
42575e9461SMike Marshall 
43575e9461SMike Marshall #include "orangefs-debugfs.h"
44575e9461SMike Marshall #include "protocol.h"
45575e9461SMike Marshall #include "orangefs-kernel.h"
46575e9461SMike Marshall 
4744f46410SMartin Brandenburg #define DEBUG_HELP_STRING_SIZE 4096
4844f46410SMartin Brandenburg #define HELP_STRING_UNINITIALIZED \
4944f46410SMartin Brandenburg 	"Client Debug Keywords are unknown until the first time\n" \
5044f46410SMartin Brandenburg 	"the client is started after boot.\n"
5144f46410SMartin Brandenburg #define ORANGEFS_KMOD_DEBUG_HELP_FILE "debug-help"
5244f46410SMartin Brandenburg #define ORANGEFS_KMOD_DEBUG_FILE "kernel-debug"
5344f46410SMartin Brandenburg #define ORANGEFS_CLIENT_DEBUG_FILE "client-debug"
5444f46410SMartin Brandenburg #define ORANGEFS_VERBOSE "verbose"
5544f46410SMartin Brandenburg #define ORANGEFS_ALL "all"
56575e9461SMike Marshall 
5744f46410SMartin Brandenburg /*
5844f46410SMartin Brandenburg  * An array of client_debug_mask will be built to hold debug keyword/mask
5944f46410SMartin Brandenburg  * values fetched from userspace.
6044f46410SMartin Brandenburg  */
6144f46410SMartin Brandenburg struct client_debug_mask {
6244f46410SMartin Brandenburg 	char *keyword;
6344f46410SMartin Brandenburg 	__u64 mask1;
6444f46410SMartin Brandenburg 	__u64 mask2;
65575e9461SMike Marshall };
66575e9461SMike Marshall 
670979cf95SGreg Kroah-Hartman static void orangefs_kernel_debug_init(void);
6844f46410SMartin Brandenburg 
6944f46410SMartin Brandenburg static int orangefs_debug_help_open(struct inode *, struct file *);
70575e9461SMike Marshall static void *help_start(struct seq_file *, loff_t *);
71575e9461SMike Marshall static void *help_next(struct seq_file *, void *, loff_t *);
72575e9461SMike Marshall static void help_stop(struct seq_file *, void *);
73575e9461SMike Marshall static int help_show(struct seq_file *, void *);
74575e9461SMike Marshall 
7544f46410SMartin Brandenburg static int orangefs_debug_open(struct inode *, struct file *);
76575e9461SMike Marshall 
77575e9461SMike Marshall static ssize_t orangefs_debug_read(struct file *,
78575e9461SMike Marshall 				 char __user *,
79575e9461SMike Marshall 				 size_t,
80575e9461SMike Marshall 				 loff_t *);
81575e9461SMike Marshall 
82575e9461SMike Marshall static ssize_t orangefs_debug_write(struct file *,
83575e9461SMike Marshall 				  const char __user *,
84575e9461SMike Marshall 				  size_t,
85575e9461SMike Marshall 				  loff_t *);
86575e9461SMike Marshall 
8744f46410SMartin Brandenburg static int orangefs_prepare_cdm_array(char *);
8844f46410SMartin Brandenburg static void debug_mask_to_string(void *, int);
8944f46410SMartin Brandenburg static void do_k_string(void *, int);
9044f46410SMartin Brandenburg static void do_c_string(void *, int);
9144f46410SMartin Brandenburg static int keyword_is_amalgam(char *);
9244f46410SMartin Brandenburg static int check_amalgam_keyword(void *, int);
9344f46410SMartin Brandenburg static void debug_string_to_mask(char *, void *, int);
9444f46410SMartin Brandenburg static void do_c_mask(int, char *, struct client_debug_mask **);
9544f46410SMartin Brandenburg static void do_k_mask(int, char *, __u64 **);
9644f46410SMartin Brandenburg 
9744f46410SMartin Brandenburg static char kernel_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN] = "none";
9844f46410SMartin Brandenburg static char *debug_help_string;
9944f46410SMartin Brandenburg static char client_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
10044f46410SMartin Brandenburg static char client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
10144f46410SMartin Brandenburg 
10244f46410SMartin Brandenburg static struct dentry *client_debug_dentry;
10344f46410SMartin Brandenburg static struct dentry *debug_dir;
10444f46410SMartin Brandenburg 
10544f46410SMartin Brandenburg static unsigned int kernel_mask_set_mod_init;
10644f46410SMartin Brandenburg static int orangefs_debug_disabled = 1;
10744f46410SMartin Brandenburg static int help_string_initialized;
10844f46410SMartin Brandenburg 
10944f46410SMartin Brandenburg static const struct seq_operations help_debug_ops = {
11044f46410SMartin Brandenburg 	.start	= help_start,
11144f46410SMartin Brandenburg 	.next	= help_next,
11244f46410SMartin Brandenburg 	.stop	= help_stop,
11344f46410SMartin Brandenburg 	.show	= help_show,
11444f46410SMartin Brandenburg };
11544f46410SMartin Brandenburg 
1163cf796afSMike Marshall static const struct file_operations debug_help_fops = {
11719ff7fccSMike Marshall 	.owner		= THIS_MODULE,
11844f46410SMartin Brandenburg 	.open           = orangefs_debug_help_open,
11944f46410SMartin Brandenburg 	.read           = seq_read,
12044f46410SMartin Brandenburg 	.release        = seq_release,
12144f46410SMartin Brandenburg 	.llseek         = seq_lseek,
12244f46410SMartin Brandenburg };
12344f46410SMartin Brandenburg 
124575e9461SMike Marshall static const struct file_operations kernel_debug_fops = {
12519ff7fccSMike Marshall 	.owner		= THIS_MODULE,
126575e9461SMike Marshall 	.open           = orangefs_debug_open,
127575e9461SMike Marshall 	.read           = orangefs_debug_read,
128575e9461SMike Marshall 	.write		= orangefs_debug_write,
129575e9461SMike Marshall 	.llseek         = generic_file_llseek,
130575e9461SMike Marshall };
131575e9461SMike Marshall 
13244f46410SMartin Brandenburg static int client_all_index;
13344f46410SMartin Brandenburg static int client_verbose_index;
13444f46410SMartin Brandenburg 
13544f46410SMartin Brandenburg static struct client_debug_mask *cdm_array;
13644f46410SMartin Brandenburg static int cdm_element_count;
13744f46410SMartin Brandenburg 
13844f46410SMartin Brandenburg static struct client_debug_mask client_debug_mask;
13944f46410SMartin Brandenburg 
14044f46410SMartin Brandenburg /*
14144f46410SMartin Brandenburg  * Used to protect data in ORANGEFS_KMOD_DEBUG_FILE and
14244f46410SMartin Brandenburg  * ORANGEFS_KMOD_DEBUG_FILE.
14344f46410SMartin Brandenburg  */
14444f46410SMartin Brandenburg static DEFINE_MUTEX(orangefs_debug_lock);
14544f46410SMartin Brandenburg 
146dc033621SMike Marshall /* Used to protect data in ORANGEFS_KMOD_DEBUG_HELP_FILE */
147dc033621SMike Marshall static DEFINE_MUTEX(orangefs_help_file_lock);
148dc033621SMike Marshall 
149575e9461SMike Marshall /*
150575e9461SMike Marshall  * initialize kmod debug operations, create orangefs debugfs dir and
151575e9461SMike Marshall  * ORANGEFS_KMOD_DEBUG_HELP_FILE.
152575e9461SMike Marshall  */
orangefs_debugfs_init(int debug_mask)1530979cf95SGreg Kroah-Hartman void orangefs_debugfs_init(int debug_mask)
154575e9461SMike Marshall {
15544f46410SMartin Brandenburg 	/* convert input debug mask to a 64-bit unsigned integer */
15644f46410SMartin Brandenburg         orangefs_gossip_debug_mask = (unsigned long long)debug_mask;
15744f46410SMartin Brandenburg 
15844f46410SMartin Brandenburg 	/*
15944f46410SMartin Brandenburg 	 * set the kernel's gossip debug string; invalid mask values will
16044f46410SMartin Brandenburg 	 * be ignored.
16144f46410SMartin Brandenburg 	 */
16244f46410SMartin Brandenburg 	debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
16344f46410SMartin Brandenburg 
16444f46410SMartin Brandenburg 	/* remove any invalid values from the mask */
16544f46410SMartin Brandenburg 	debug_string_to_mask(kernel_debug_string, &orangefs_gossip_debug_mask,
16644f46410SMartin Brandenburg 	    0);
16744f46410SMartin Brandenburg 
16844f46410SMartin Brandenburg 	/*
16944f46410SMartin Brandenburg 	 * if the mask has a non-zero value, then indicate that the mask
17044f46410SMartin Brandenburg 	 * was set when the kernel module was loaded.  The orangefs dev ioctl
17144f46410SMartin Brandenburg 	 * command will look at this boolean to determine if the kernel's
17244f46410SMartin Brandenburg 	 * debug mask should be overwritten when the client-core is started.
17344f46410SMartin Brandenburg 	 */
17444f46410SMartin Brandenburg 	if (orangefs_gossip_debug_mask != 0)
17544f46410SMartin Brandenburg 		kernel_mask_set_mod_init = true;
17644f46410SMartin Brandenburg 
17744f46410SMartin Brandenburg 	pr_info("%s: called with debug mask: :%s: :%llx:\n",
17844f46410SMartin Brandenburg 		__func__,
17944f46410SMartin Brandenburg 		kernel_debug_string,
18044f46410SMartin Brandenburg 		(unsigned long long)orangefs_gossip_debug_mask);
18144f46410SMartin Brandenburg 
182575e9461SMike Marshall 	debug_dir = debugfs_create_dir("orangefs", NULL);
183575e9461SMike Marshall 
184f095adbaSGreg Kroah-Hartman 	debugfs_create_file(ORANGEFS_KMOD_DEBUG_HELP_FILE, 0444, debug_dir,
185f095adbaSGreg Kroah-Hartman 			    debug_help_string, &debug_help_fops);
186575e9461SMike Marshall 
187575e9461SMike Marshall 	orangefs_debug_disabled = 0;
18844f46410SMartin Brandenburg 
1890979cf95SGreg Kroah-Hartman 	orangefs_kernel_debug_init();
190575e9461SMike Marshall }
191575e9461SMike Marshall 
19244f46410SMartin Brandenburg /*
19344f46410SMartin Brandenburg  * initialize the kernel-debug file.
19444f46410SMartin Brandenburg  */
orangefs_kernel_debug_init(void)1950979cf95SGreg Kroah-Hartman static void orangefs_kernel_debug_init(void)
19644f46410SMartin Brandenburg {
19731720a2bSZhang Xiaoxu 	static char k_buffer[ORANGEFS_MAX_DEBUG_STRING_LEN] = { };
19844f46410SMartin Brandenburg 
19944f46410SMartin Brandenburg 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
20044f46410SMartin Brandenburg 
20144f46410SMartin Brandenburg 	if (strlen(kernel_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
20244f46410SMartin Brandenburg 		strcpy(k_buffer, kernel_debug_string);
20344f46410SMartin Brandenburg 		strcat(k_buffer, "\n");
20444f46410SMartin Brandenburg 	} else {
20544f46410SMartin Brandenburg 		strcpy(k_buffer, "none\n");
20644f46410SMartin Brandenburg 		pr_info("%s: overflow 1!\n", __func__);
20744f46410SMartin Brandenburg 	}
20844f46410SMartin Brandenburg 
209f095adbaSGreg Kroah-Hartman 	debugfs_create_file(ORANGEFS_KMOD_DEBUG_FILE, 0444, debug_dir, k_buffer,
21044f46410SMartin Brandenburg 			    &kernel_debug_fops);
21144f46410SMartin Brandenburg }
21244f46410SMartin Brandenburg 
21344f46410SMartin Brandenburg 
orangefs_debugfs_cleanup(void)214575e9461SMike Marshall void orangefs_debugfs_cleanup(void)
215575e9461SMike Marshall {
216575e9461SMike Marshall 	debugfs_remove_recursive(debug_dir);
217d23417a5SZhang Xiaoxu 	kfree(debug_help_string);
218d23417a5SZhang Xiaoxu 	debug_help_string = NULL;
219575e9461SMike Marshall }
220575e9461SMike Marshall 
221575e9461SMike Marshall /* open ORANGEFS_KMOD_DEBUG_HELP_FILE */
orangefs_debug_help_open(struct inode * inode,struct file * file)222575e9461SMike Marshall static int orangefs_debug_help_open(struct inode *inode, struct file *file)
223575e9461SMike Marshall {
224575e9461SMike Marshall 	int rc = -ENODEV;
225575e9461SMike Marshall 	int ret;
226575e9461SMike Marshall 
227575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
228575e9461SMike Marshall 		     "orangefs_debug_help_open: start\n");
229575e9461SMike Marshall 
230575e9461SMike Marshall 	if (orangefs_debug_disabled)
231575e9461SMike Marshall 		goto out;
232575e9461SMike Marshall 
233575e9461SMike Marshall 	ret = seq_open(file, &help_debug_ops);
234575e9461SMike Marshall 	if (ret)
235575e9461SMike Marshall 		goto out;
236575e9461SMike Marshall 
237575e9461SMike Marshall 	((struct seq_file *)(file->private_data))->private = inode->i_private;
238575e9461SMike Marshall 
239575e9461SMike Marshall 	rc = 0;
240575e9461SMike Marshall 
241575e9461SMike Marshall out:
242575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
243575e9461SMike Marshall 		     "orangefs_debug_help_open: rc:%d:\n",
244575e9461SMike Marshall 		     rc);
245575e9461SMike Marshall 	return rc;
246575e9461SMike Marshall }
247575e9461SMike Marshall 
248575e9461SMike Marshall /*
249575e9461SMike Marshall  * I think start always gets called again after stop. Start
250575e9461SMike Marshall  * needs to return NULL when it is done. The whole "payload"
251575e9461SMike Marshall  * in this case is a single (long) string, so by the second
252575e9461SMike Marshall  * time we get to start (pos = 1), we're done.
253575e9461SMike Marshall  */
help_start(struct seq_file * m,loff_t * pos)254575e9461SMike Marshall static void *help_start(struct seq_file *m, loff_t *pos)
255575e9461SMike Marshall {
256575e9461SMike Marshall 	void *payload = NULL;
257575e9461SMike Marshall 
258575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_start: start\n");
259575e9461SMike Marshall 
260dc033621SMike Marshall 	mutex_lock(&orangefs_help_file_lock);
261dc033621SMike Marshall 
262575e9461SMike Marshall 	if (*pos == 0)
263575e9461SMike Marshall 		payload = m->private;
264575e9461SMike Marshall 
265575e9461SMike Marshall 	return payload;
266575e9461SMike Marshall }
267575e9461SMike Marshall 
help_next(struct seq_file * m,void * v,loff_t * pos)268575e9461SMike Marshall static void *help_next(struct seq_file *m, void *v, loff_t *pos)
269575e9461SMike Marshall {
2709f198a2aSVasily Averin 	(*pos)++;
271575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
272575e9461SMike Marshall 
273575e9461SMike Marshall 	return NULL;
274575e9461SMike Marshall }
275575e9461SMike Marshall 
help_stop(struct seq_file * m,void * p)276575e9461SMike Marshall static void help_stop(struct seq_file *m, void *p)
277575e9461SMike Marshall {
278575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_stop: start\n");
279dc033621SMike Marshall 	mutex_unlock(&orangefs_help_file_lock);
280575e9461SMike Marshall }
281575e9461SMike Marshall 
help_show(struct seq_file * m,void * v)282575e9461SMike Marshall static int help_show(struct seq_file *m, void *v)
283575e9461SMike Marshall {
284575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_show: start\n");
285575e9461SMike Marshall 
286575e9461SMike Marshall 	seq_puts(m, v);
287575e9461SMike Marshall 
288575e9461SMike Marshall 	return 0;
289575e9461SMike Marshall }
290575e9461SMike Marshall 
291575e9461SMike Marshall /*
292575e9461SMike Marshall  * initialize the client-debug file.
293575e9461SMike Marshall  */
orangefs_client_debug_init(void)29431720a2bSZhang Xiaoxu static void orangefs_client_debug_init(void)
295575e9461SMike Marshall {
296575e9461SMike Marshall 
29731720a2bSZhang Xiaoxu 	static char c_buffer[ORANGEFS_MAX_DEBUG_STRING_LEN] = { };
298575e9461SMike Marshall 
299575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
300575e9461SMike Marshall 
301575e9461SMike Marshall 	if (strlen(client_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
302575e9461SMike Marshall 		strcpy(c_buffer, client_debug_string);
303575e9461SMike Marshall 		strcat(c_buffer, "\n");
304575e9461SMike Marshall 	} else {
305575e9461SMike Marshall 		strcpy(c_buffer, "none\n");
306575e9461SMike Marshall 		pr_info("%s: overflow! 2\n", __func__);
307575e9461SMike Marshall 	}
308575e9461SMike Marshall 
309575e9461SMike Marshall 	client_debug_dentry = debugfs_create_file(ORANGEFS_CLIENT_DEBUG_FILE,
310575e9461SMike Marshall 						  0444,
311575e9461SMike Marshall 						  debug_dir,
312575e9461SMike Marshall 						  c_buffer,
313575e9461SMike Marshall 						  &kernel_debug_fops);
314575e9461SMike Marshall }
315575e9461SMike Marshall 
316575e9461SMike Marshall /* open ORANGEFS_KMOD_DEBUG_FILE or ORANGEFS_CLIENT_DEBUG_FILE.*/
orangefs_debug_open(struct inode * inode,struct file * file)31744f46410SMartin Brandenburg static int orangefs_debug_open(struct inode *inode, struct file *file)
318575e9461SMike Marshall {
319575e9461SMike Marshall 	int rc = -ENODEV;
320575e9461SMike Marshall 
321575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
322575e9461SMike Marshall 		     "%s: orangefs_debug_disabled: %d\n",
323575e9461SMike Marshall 		     __func__,
324575e9461SMike Marshall 		     orangefs_debug_disabled);
325575e9461SMike Marshall 
326575e9461SMike Marshall 	if (orangefs_debug_disabled)
327575e9461SMike Marshall 		goto out;
328575e9461SMike Marshall 
329575e9461SMike Marshall 	rc = 0;
330575e9461SMike Marshall 	mutex_lock(&orangefs_debug_lock);
331575e9461SMike Marshall 	file->private_data = inode->i_private;
332575e9461SMike Marshall 	mutex_unlock(&orangefs_debug_lock);
333575e9461SMike Marshall 
334575e9461SMike Marshall out:
335575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
336575e9461SMike Marshall 		     "orangefs_debug_open: rc: %d\n",
337575e9461SMike Marshall 		     rc);
338575e9461SMike Marshall 	return rc;
339575e9461SMike Marshall }
340575e9461SMike Marshall 
orangefs_debug_read(struct file * file,char __user * ubuf,size_t count,loff_t * ppos)341575e9461SMike Marshall static ssize_t orangefs_debug_read(struct file *file,
342575e9461SMike Marshall 				 char __user *ubuf,
343575e9461SMike Marshall 				 size_t count,
344575e9461SMike Marshall 				 loff_t *ppos)
345575e9461SMike Marshall {
346575e9461SMike Marshall 	char *buf;
347575e9461SMike Marshall 	int sprintf_ret;
348575e9461SMike Marshall 	ssize_t read_ret = -ENOMEM;
349575e9461SMike Marshall 
350575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG, "orangefs_debug_read: start\n");
351575e9461SMike Marshall 
352575e9461SMike Marshall 	buf = kmalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
353575e9461SMike Marshall 	if (!buf)
354575e9461SMike Marshall 		goto out;
355575e9461SMike Marshall 
356575e9461SMike Marshall 	mutex_lock(&orangefs_debug_lock);
357575e9461SMike Marshall 	sprintf_ret = sprintf(buf, "%s", (char *)file->private_data);
358575e9461SMike Marshall 	mutex_unlock(&orangefs_debug_lock);
359575e9461SMike Marshall 
360575e9461SMike Marshall 	read_ret = simple_read_from_buffer(ubuf, count, ppos, buf, sprintf_ret);
361575e9461SMike Marshall 
362575e9461SMike Marshall 	kfree(buf);
363575e9461SMike Marshall 
364575e9461SMike Marshall out:
365575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
366575e9461SMike Marshall 		     "orangefs_debug_read: ret: %zu\n",
367575e9461SMike Marshall 		     read_ret);
368575e9461SMike Marshall 
369575e9461SMike Marshall 	return read_ret;
370575e9461SMike Marshall }
371575e9461SMike Marshall 
orangefs_debug_write(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)372575e9461SMike Marshall static ssize_t orangefs_debug_write(struct file *file,
373575e9461SMike Marshall 				  const char __user *ubuf,
374575e9461SMike Marshall 				  size_t count,
375575e9461SMike Marshall 				  loff_t *ppos)
376575e9461SMike Marshall {
377575e9461SMike Marshall 	char *buf;
378575e9461SMike Marshall 	int rc = -EFAULT;
379575e9461SMike Marshall 	size_t silly = 0;
380575e9461SMike Marshall 	char *debug_string;
381575e9461SMike Marshall 	struct orangefs_kernel_op_s *new_op = NULL;
382575e9461SMike Marshall 	struct client_debug_mask c_mask = { NULL, 0, 0 };
38345063097SAl Viro 	char *s;
384575e9461SMike Marshall 
385575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
386f66debf1SAl Viro 		"orangefs_debug_write: %pD\n",
387f66debf1SAl Viro 		file);
388575e9461SMike Marshall 
389907bfcd8SDan Carpenter 	if (count == 0)
390907bfcd8SDan Carpenter 		return 0;
391907bfcd8SDan Carpenter 
392575e9461SMike Marshall 	/*
393575e9461SMike Marshall 	 * Thwart users who try to jamb a ridiculous number
394575e9461SMike Marshall 	 * of bytes into the debug file...
395575e9461SMike Marshall 	 */
396*2b84a231SMike Marshall 	if (count > ORANGEFS_MAX_DEBUG_STRING_LEN) {
397575e9461SMike Marshall 		silly = count;
398*2b84a231SMike Marshall 		count = ORANGEFS_MAX_DEBUG_STRING_LEN;
399575e9461SMike Marshall 	}
400575e9461SMike Marshall 
401dde58ca4SNicholas Mc Guire 	buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
402575e9461SMike Marshall 	if (!buf)
403575e9461SMike Marshall 		goto out;
404575e9461SMike Marshall 
405575e9461SMike Marshall 	if (copy_from_user(buf, ubuf, count - 1)) {
406575e9461SMike Marshall 		gossip_debug(GOSSIP_DEBUGFS_DEBUG,
407575e9461SMike Marshall 			     "%s: copy_from_user failed!\n",
408575e9461SMike Marshall 			     __func__);
409575e9461SMike Marshall 		goto out;
410575e9461SMike Marshall 	}
411575e9461SMike Marshall 
412575e9461SMike Marshall 	/*
413575e9461SMike Marshall 	 * Map the keyword string from userspace into a valid debug mask.
414575e9461SMike Marshall 	 * The mapping process involves mapping the human-inputted string
415575e9461SMike Marshall 	 * into a valid mask, and then rebuilding the string from the
416575e9461SMike Marshall 	 * verified valid mask.
417575e9461SMike Marshall 	 *
418575e9461SMike Marshall 	 * A service operation is required to set a new client-side
419575e9461SMike Marshall 	 * debug mask.
420575e9461SMike Marshall 	 */
421575e9461SMike Marshall 	if (!strcmp(file->f_path.dentry->d_name.name,
422575e9461SMike Marshall 		    ORANGEFS_KMOD_DEBUG_FILE)) {
42344f46410SMartin Brandenburg 		debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
42444f46410SMartin Brandenburg 		debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
425575e9461SMike Marshall 		debug_string = kernel_debug_string;
426575e9461SMike Marshall 		gossip_debug(GOSSIP_DEBUGFS_DEBUG,
427575e9461SMike Marshall 			     "New kernel debug string is %s\n",
428575e9461SMike Marshall 			     kernel_debug_string);
429575e9461SMike Marshall 	} else {
430575e9461SMike Marshall 		/* Can't reset client debug mask if client is not running. */
431575e9461SMike Marshall 		if (is_daemon_in_service()) {
432575e9461SMike Marshall 			pr_info("%s: Client not running :%d:\n",
433575e9461SMike Marshall 				__func__,
434575e9461SMike Marshall 				is_daemon_in_service());
435575e9461SMike Marshall 			goto out;
436575e9461SMike Marshall 		}
437575e9461SMike Marshall 
438575e9461SMike Marshall 		debug_string_to_mask(buf, &c_mask, 1);
439575e9461SMike Marshall 		debug_mask_to_string(&c_mask, 1);
440575e9461SMike Marshall 		debug_string = client_debug_string;
441575e9461SMike Marshall 
442575e9461SMike Marshall 		new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
443575e9461SMike Marshall 		if (!new_op) {
444575e9461SMike Marshall 			pr_info("%s: op_alloc failed!\n", __func__);
445575e9461SMike Marshall 			goto out;
446575e9461SMike Marshall 		}
447575e9461SMike Marshall 
448575e9461SMike Marshall 		new_op->upcall.req.param.op =
449575e9461SMike Marshall 			ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
450575e9461SMike Marshall 		new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
451575e9461SMike Marshall 		memset(new_op->upcall.req.param.s_value,
452575e9461SMike Marshall 		       0,
453575e9461SMike Marshall 		       ORANGEFS_MAX_DEBUG_STRING_LEN);
454575e9461SMike Marshall 		sprintf(new_op->upcall.req.param.s_value,
455575e9461SMike Marshall 			"%llx %llx\n",
456575e9461SMike Marshall 			c_mask.mask1,
457575e9461SMike Marshall 			c_mask.mask2);
458575e9461SMike Marshall 
459575e9461SMike Marshall 		/* service_operation returns 0 on success... */
460575e9461SMike Marshall 		rc = service_operation(new_op,
461575e9461SMike Marshall 				       "orangefs_param",
462575e9461SMike Marshall 					ORANGEFS_OP_INTERRUPTIBLE);
463575e9461SMike Marshall 
464575e9461SMike Marshall 		if (rc)
465575e9461SMike Marshall 			gossip_debug(GOSSIP_DEBUGFS_DEBUG,
466575e9461SMike Marshall 				     "%s: service_operation failed! rc:%d:\n",
467575e9461SMike Marshall 				     __func__,
468575e9461SMike Marshall 				     rc);
469575e9461SMike Marshall 
470575e9461SMike Marshall 		op_release(new_op);
471575e9461SMike Marshall 	}
472575e9461SMike Marshall 
473575e9461SMike Marshall 	mutex_lock(&orangefs_debug_lock);
47445063097SAl Viro 	s = file_inode(file)->i_private;
47545063097SAl Viro 	memset(s, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
47645063097SAl Viro 	sprintf(s, "%s\n", debug_string);
477575e9461SMike Marshall 	mutex_unlock(&orangefs_debug_lock);
478575e9461SMike Marshall 
479575e9461SMike Marshall 	*ppos += count;
480575e9461SMike Marshall 	if (silly)
481575e9461SMike Marshall 		rc = silly;
482575e9461SMike Marshall 	else
483575e9461SMike Marshall 		rc = count;
484575e9461SMike Marshall 
485575e9461SMike Marshall out:
486575e9461SMike Marshall 	gossip_debug(GOSSIP_DEBUGFS_DEBUG,
487575e9461SMike Marshall 		     "orangefs_debug_write: rc: %d\n",
488575e9461SMike Marshall 		     rc);
489575e9461SMike Marshall 	kfree(buf);
490575e9461SMike Marshall 	return rc;
491575e9461SMike Marshall }
49244f46410SMartin Brandenburg 
49344f46410SMartin Brandenburg /*
49444f46410SMartin Brandenburg  * After obtaining a string representation of the client's debug
49544f46410SMartin Brandenburg  * keywords and their associated masks, this function is called to build an
49644f46410SMartin Brandenburg  * array of these values.
49744f46410SMartin Brandenburg  */
orangefs_prepare_cdm_array(char * debug_array_string)49844f46410SMartin Brandenburg static int orangefs_prepare_cdm_array(char *debug_array_string)
49944f46410SMartin Brandenburg {
50044f46410SMartin Brandenburg 	int i;
50144f46410SMartin Brandenburg 	int rc = -EINVAL;
50244f46410SMartin Brandenburg 	char *cds_head = NULL;
50344f46410SMartin Brandenburg 	char *cds_delimiter = NULL;
50444f46410SMartin Brandenburg 	int keyword_len = 0;
50544f46410SMartin Brandenburg 
50644f46410SMartin Brandenburg 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
50744f46410SMartin Brandenburg 
50844f46410SMartin Brandenburg 	/*
50944f46410SMartin Brandenburg 	 * figure out how many elements the cdm_array needs.
51044f46410SMartin Brandenburg 	 */
51144f46410SMartin Brandenburg 	for (i = 0; i < strlen(debug_array_string); i++)
51244f46410SMartin Brandenburg 		if (debug_array_string[i] == '\n')
51344f46410SMartin Brandenburg 			cdm_element_count++;
51444f46410SMartin Brandenburg 
51544f46410SMartin Brandenburg 	if (!cdm_element_count) {
51644f46410SMartin Brandenburg 		pr_info("No elements in client debug array string!\n");
51744f46410SMartin Brandenburg 		goto out;
51844f46410SMartin Brandenburg 	}
51944f46410SMartin Brandenburg 
5205e273a0eSMarkus Elfring 	cdm_array = kcalloc(cdm_element_count, sizeof(*cdm_array), GFP_KERNEL);
52144f46410SMartin Brandenburg 	if (!cdm_array) {
52244f46410SMartin Brandenburg 		rc = -ENOMEM;
52344f46410SMartin Brandenburg 		goto out;
52444f46410SMartin Brandenburg 	}
52544f46410SMartin Brandenburg 
52644f46410SMartin Brandenburg 	cds_head = debug_array_string;
52744f46410SMartin Brandenburg 
52844f46410SMartin Brandenburg 	for (i = 0; i < cdm_element_count; i++) {
52944f46410SMartin Brandenburg 		cds_delimiter = strchr(cds_head, '\n');
53044f46410SMartin Brandenburg 		*cds_delimiter = '\0';
53144f46410SMartin Brandenburg 
53244f46410SMartin Brandenburg 		keyword_len = strcspn(cds_head, " ");
53344f46410SMartin Brandenburg 
53444f46410SMartin Brandenburg 		cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
53544f46410SMartin Brandenburg 		if (!cdm_array[i].keyword) {
53644f46410SMartin Brandenburg 			rc = -ENOMEM;
53744f46410SMartin Brandenburg 			goto out;
53844f46410SMartin Brandenburg 		}
53944f46410SMartin Brandenburg 
54044f46410SMartin Brandenburg 		sscanf(cds_head,
54144f46410SMartin Brandenburg 		       "%s %llx %llx",
54244f46410SMartin Brandenburg 		       cdm_array[i].keyword,
54344f46410SMartin Brandenburg 		       (unsigned long long *)&(cdm_array[i].mask1),
54444f46410SMartin Brandenburg 		       (unsigned long long *)&(cdm_array[i].mask2));
54544f46410SMartin Brandenburg 
54644f46410SMartin Brandenburg 		if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
54744f46410SMartin Brandenburg 			client_verbose_index = i;
54844f46410SMartin Brandenburg 
54944f46410SMartin Brandenburg 		if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
55044f46410SMartin Brandenburg 			client_all_index = i;
55144f46410SMartin Brandenburg 
55244f46410SMartin Brandenburg 		cds_head = cds_delimiter + 1;
55344f46410SMartin Brandenburg 	}
55444f46410SMartin Brandenburg 
55544f46410SMartin Brandenburg 	rc = cdm_element_count;
55644f46410SMartin Brandenburg 
55744f46410SMartin Brandenburg 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
55844f46410SMartin Brandenburg 
55944f46410SMartin Brandenburg out:
56044f46410SMartin Brandenburg 
56144f46410SMartin Brandenburg 	return rc;
56244f46410SMartin Brandenburg 
56344f46410SMartin Brandenburg }
56444f46410SMartin Brandenburg 
56544f46410SMartin Brandenburg /*
56644f46410SMartin Brandenburg  * /sys/kernel/debug/orangefs/debug-help can be catted to
56744f46410SMartin Brandenburg  * see all the available kernel and client debug keywords.
56844f46410SMartin Brandenburg  *
569dc033621SMike Marshall  * When orangefs.ko initializes, we have no idea what keywords the
57044f46410SMartin Brandenburg  * client supports, nor their associated masks.
57144f46410SMartin Brandenburg  *
572dc033621SMike Marshall  * We pass through this function once at module-load and stamp a
57344f46410SMartin Brandenburg  * boilerplate "we don't know" message for the client in the
57444f46410SMartin Brandenburg  * debug-help file. We pass through here again when the client
57544f46410SMartin Brandenburg  * starts and then we can fill out the debug-help file fully.
57644f46410SMartin Brandenburg  *
57744f46410SMartin Brandenburg  * The client might be restarted any number of times between
578dc033621SMike Marshall  * module reloads, we only build the debug-help file the first time.
57944f46410SMartin Brandenburg  */
orangefs_prepare_debugfs_help_string(int at_boot)58044f46410SMartin Brandenburg int orangefs_prepare_debugfs_help_string(int at_boot)
58144f46410SMartin Brandenburg {
58244f46410SMartin Brandenburg 	char *client_title = "Client Debug Keywords:\n";
58344f46410SMartin Brandenburg 	char *kernel_title = "Kernel Debug Keywords:\n";
584dc033621SMike Marshall 	size_t string_size =  DEBUG_HELP_STRING_SIZE;
585dc033621SMike Marshall 	size_t result_size;
586dc033621SMike Marshall 	size_t i;
587dc033621SMike Marshall 	char *new;
588dc033621SMike Marshall 	int rc = -EINVAL;
58944f46410SMartin Brandenburg 
59044f46410SMartin Brandenburg 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
59144f46410SMartin Brandenburg 
592dc033621SMike Marshall 	if (at_boot)
59344f46410SMartin Brandenburg 		client_title = HELP_STRING_UNINITIALIZED;
594dc033621SMike Marshall 
595dc033621SMike Marshall 	/* build a new debug_help_string. */
596dc033621SMike Marshall 	new = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
597dc033621SMike Marshall 	if (!new) {
598dc033621SMike Marshall 		rc = -ENOMEM;
599dc033621SMike Marshall 		goto out;
600dc033621SMike Marshall 	}
601dc033621SMike Marshall 
602dc033621SMike Marshall 	/*
603dc033621SMike Marshall 	 * strlcat(dst, src, size) will append at most
604dc033621SMike Marshall 	 * "size - strlen(dst) - 1" bytes of src onto dst,
605dc033621SMike Marshall 	 * null terminating the result, and return the total
606dc033621SMike Marshall 	 * length of the string it tried to create.
607dc033621SMike Marshall 	 *
608dc033621SMike Marshall 	 * We'll just plow through here building our new debug
609dc033621SMike Marshall 	 * help string and let strlcat take care of assuring that
610dc033621SMike Marshall 	 * dst doesn't overflow.
611dc033621SMike Marshall 	 */
612dc033621SMike Marshall 	strlcat(new, client_title, string_size);
613dc033621SMike Marshall 
614dc033621SMike Marshall 	if (!at_boot) {
615dc033621SMike Marshall 
61644f46410SMartin Brandenburg                 /*
61744f46410SMartin Brandenburg 		 * fill the client keyword/mask array and remember
61844f46410SMartin Brandenburg 		 * how many elements there were.
61944f46410SMartin Brandenburg 		 */
62044f46410SMartin Brandenburg 		cdm_element_count =
62144f46410SMartin Brandenburg 			orangefs_prepare_cdm_array(client_debug_array_string);
6224defb5f9SColin Ian King 		if (cdm_element_count <= 0) {
6234defb5f9SColin Ian King 			kfree(new);
62444f46410SMartin Brandenburg 			goto out;
6254defb5f9SColin Ian King 		}
62644f46410SMartin Brandenburg 
62744f46410SMartin Brandenburg 		for (i = 0; i < cdm_element_count; i++) {
628dc033621SMike Marshall 			strlcat(new, "\t", string_size);
629dc033621SMike Marshall 			strlcat(new, cdm_array[i].keyword, string_size);
630dc033621SMike Marshall 			strlcat(new, "\n", string_size);
63144f46410SMartin Brandenburg 		}
63244f46410SMartin Brandenburg 	}
63344f46410SMartin Brandenburg 
634dc033621SMike Marshall 	strlcat(new, "\n", string_size);
635dc033621SMike Marshall 	strlcat(new, kernel_title, string_size);
63644f46410SMartin Brandenburg 
63744f46410SMartin Brandenburg 	for (i = 0; i < num_kmod_keyword_mask_map; i++) {
638dc033621SMike Marshall 		strlcat(new, "\t", string_size);
639dc033621SMike Marshall 		strlcat(new, s_kmod_keyword_mask_map[i].keyword, string_size);
640dc033621SMike Marshall 		result_size = strlcat(new, "\n", string_size);
641dc033621SMike Marshall 	}
642dc033621SMike Marshall 
643dc033621SMike Marshall 	/* See if we tried to put too many bytes into "new"... */
644dc033621SMike Marshall 	if (result_size >= string_size) {
645dc033621SMike Marshall 		kfree(new);
646dc033621SMike Marshall 		goto out;
647dc033621SMike Marshall 	}
648dc033621SMike Marshall 
649dc033621SMike Marshall 	if (at_boot) {
650dc033621SMike Marshall 		debug_help_string = new;
651dc033621SMike Marshall 	} else {
652dc033621SMike Marshall 		mutex_lock(&orangefs_help_file_lock);
653dc033621SMike Marshall 		memset(debug_help_string, 0, DEBUG_HELP_STRING_SIZE);
654dc033621SMike Marshall 		strlcat(debug_help_string, new, string_size);
655dc033621SMike Marshall 		mutex_unlock(&orangefs_help_file_lock);
656d23417a5SZhang Xiaoxu 		kfree(new);
65744f46410SMartin Brandenburg 	}
65844f46410SMartin Brandenburg 
65944f46410SMartin Brandenburg 	rc = 0;
66044f46410SMartin Brandenburg 
661dc033621SMike Marshall out:	return rc;
66244f46410SMartin Brandenburg 
66344f46410SMartin Brandenburg }
66444f46410SMartin Brandenburg 
66544f46410SMartin Brandenburg /*
66644f46410SMartin Brandenburg  * kernel = type 0
66744f46410SMartin Brandenburg  * client = type 1
66844f46410SMartin Brandenburg  */
debug_mask_to_string(void * mask,int type)66944f46410SMartin Brandenburg static void debug_mask_to_string(void *mask, int type)
67044f46410SMartin Brandenburg {
67144f46410SMartin Brandenburg 	int i;
67244f46410SMartin Brandenburg 	int len = 0;
67344f46410SMartin Brandenburg 	char *debug_string;
67444f46410SMartin Brandenburg 	int element_count = 0;
67544f46410SMartin Brandenburg 
67644f46410SMartin Brandenburg 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
67744f46410SMartin Brandenburg 
67844f46410SMartin Brandenburg 	if (type) {
67944f46410SMartin Brandenburg 		debug_string = client_debug_string;
68044f46410SMartin Brandenburg 		element_count = cdm_element_count;
68144f46410SMartin Brandenburg 	} else {
68244f46410SMartin Brandenburg 		debug_string = kernel_debug_string;
68344f46410SMartin Brandenburg 		element_count = num_kmod_keyword_mask_map;
68444f46410SMartin Brandenburg 	}
68544f46410SMartin Brandenburg 
68644f46410SMartin Brandenburg 	memset(debug_string, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
68744f46410SMartin Brandenburg 
68844f46410SMartin Brandenburg 	/*
68944f46410SMartin Brandenburg 	 * Some keywords, like "all" or "verbose", are amalgams of
69044f46410SMartin Brandenburg 	 * numerous other keywords. Make a special check for those
69144f46410SMartin Brandenburg 	 * before grinding through the whole mask only to find out
69244f46410SMartin Brandenburg 	 * later...
69344f46410SMartin Brandenburg 	 */
69444f46410SMartin Brandenburg 	if (check_amalgam_keyword(mask, type))
69544f46410SMartin Brandenburg 		goto out;
69644f46410SMartin Brandenburg 
69744f46410SMartin Brandenburg 	/* Build the debug string. */
69844f46410SMartin Brandenburg 	for (i = 0; i < element_count; i++)
69944f46410SMartin Brandenburg 		if (type)
70044f46410SMartin Brandenburg 			do_c_string(mask, i);
70144f46410SMartin Brandenburg 		else
70244f46410SMartin Brandenburg 			do_k_string(mask, i);
70344f46410SMartin Brandenburg 
70444f46410SMartin Brandenburg 	len = strlen(debug_string);
70544f46410SMartin Brandenburg 
70644f46410SMartin Brandenburg 	if ((len) && (type))
70744f46410SMartin Brandenburg 		client_debug_string[len - 1] = '\0';
70844f46410SMartin Brandenburg 	else if (len)
70944f46410SMartin Brandenburg 		kernel_debug_string[len - 1] = '\0';
71044f46410SMartin Brandenburg 	else if (type)
71144f46410SMartin Brandenburg 		strcpy(client_debug_string, "none");
71244f46410SMartin Brandenburg 	else
71344f46410SMartin Brandenburg 		strcpy(kernel_debug_string, "none");
71444f46410SMartin Brandenburg 
71544f46410SMartin Brandenburg out:
71644f46410SMartin Brandenburg gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
71744f46410SMartin Brandenburg 
71844f46410SMartin Brandenburg 	return;
71944f46410SMartin Brandenburg 
72044f46410SMartin Brandenburg }
72144f46410SMartin Brandenburg 
do_k_string(void * k_mask,int index)72244f46410SMartin Brandenburg static void do_k_string(void *k_mask, int index)
72344f46410SMartin Brandenburg {
72444f46410SMartin Brandenburg 	__u64 *mask = (__u64 *) k_mask;
72544f46410SMartin Brandenburg 
72644f46410SMartin Brandenburg 	if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
72744f46410SMartin Brandenburg 		goto out;
72844f46410SMartin Brandenburg 
72944f46410SMartin Brandenburg 	if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
73044f46410SMartin Brandenburg 		if ((strlen(kernel_debug_string) +
73144f46410SMartin Brandenburg 		     strlen(s_kmod_keyword_mask_map[index].keyword))
73244f46410SMartin Brandenburg 			< ORANGEFS_MAX_DEBUG_STRING_LEN - 1) {
73344f46410SMartin Brandenburg 				strcat(kernel_debug_string,
73444f46410SMartin Brandenburg 				       s_kmod_keyword_mask_map[index].keyword);
73544f46410SMartin Brandenburg 				strcat(kernel_debug_string, ",");
73644f46410SMartin Brandenburg 			} else {
73744f46410SMartin Brandenburg 				gossip_err("%s: overflow!\n", __func__);
73844f46410SMartin Brandenburg 				strcpy(kernel_debug_string, ORANGEFS_ALL);
73944f46410SMartin Brandenburg 				goto out;
74044f46410SMartin Brandenburg 			}
74144f46410SMartin Brandenburg 	}
74244f46410SMartin Brandenburg 
74344f46410SMartin Brandenburg out:
74444f46410SMartin Brandenburg 
74544f46410SMartin Brandenburg 	return;
74644f46410SMartin Brandenburg }
74744f46410SMartin Brandenburg 
do_c_string(void * c_mask,int index)74844f46410SMartin Brandenburg static void do_c_string(void *c_mask, int index)
74944f46410SMartin Brandenburg {
75044f46410SMartin Brandenburg 	struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
75144f46410SMartin Brandenburg 
75244f46410SMartin Brandenburg 	if (keyword_is_amalgam(cdm_array[index].keyword))
75344f46410SMartin Brandenburg 		goto out;
75444f46410SMartin Brandenburg 
75544f46410SMartin Brandenburg 	if ((mask->mask1 & cdm_array[index].mask1) ||
75644f46410SMartin Brandenburg 	    (mask->mask2 & cdm_array[index].mask2)) {
75744f46410SMartin Brandenburg 		if ((strlen(client_debug_string) +
75844f46410SMartin Brandenburg 		     strlen(cdm_array[index].keyword) + 1)
75944f46410SMartin Brandenburg 			< ORANGEFS_MAX_DEBUG_STRING_LEN - 2) {
76044f46410SMartin Brandenburg 				strcat(client_debug_string,
76144f46410SMartin Brandenburg 				       cdm_array[index].keyword);
76244f46410SMartin Brandenburg 				strcat(client_debug_string, ",");
76344f46410SMartin Brandenburg 			} else {
76444f46410SMartin Brandenburg 				gossip_err("%s: overflow!\n", __func__);
76544f46410SMartin Brandenburg 				strcpy(client_debug_string, ORANGEFS_ALL);
76644f46410SMartin Brandenburg 				goto out;
76744f46410SMartin Brandenburg 			}
76844f46410SMartin Brandenburg 	}
76944f46410SMartin Brandenburg out:
77044f46410SMartin Brandenburg 	return;
77144f46410SMartin Brandenburg }
77244f46410SMartin Brandenburg 
keyword_is_amalgam(char * keyword)77344f46410SMartin Brandenburg static int keyword_is_amalgam(char *keyword)
77444f46410SMartin Brandenburg {
77544f46410SMartin Brandenburg 	int rc = 0;
77644f46410SMartin Brandenburg 
77744f46410SMartin Brandenburg 	if ((!strcmp(keyword, ORANGEFS_ALL)) || (!strcmp(keyword, ORANGEFS_VERBOSE)))
77844f46410SMartin Brandenburg 		rc = 1;
77944f46410SMartin Brandenburg 
78044f46410SMartin Brandenburg 	return rc;
78144f46410SMartin Brandenburg }
78244f46410SMartin Brandenburg 
78344f46410SMartin Brandenburg /*
78444f46410SMartin Brandenburg  * kernel = type 0
78544f46410SMartin Brandenburg  * client = type 1
78644f46410SMartin Brandenburg  *
78744f46410SMartin Brandenburg  * return 1 if we found an amalgam.
78844f46410SMartin Brandenburg  */
check_amalgam_keyword(void * mask,int type)78944f46410SMartin Brandenburg static int check_amalgam_keyword(void *mask, int type)
79044f46410SMartin Brandenburg {
79144f46410SMartin Brandenburg 	__u64 *k_mask;
79244f46410SMartin Brandenburg 	struct client_debug_mask *c_mask;
79344f46410SMartin Brandenburg 	int k_all_index = num_kmod_keyword_mask_map - 1;
79444f46410SMartin Brandenburg 	int rc = 0;
79544f46410SMartin Brandenburg 
79644f46410SMartin Brandenburg 	if (type) {
79744f46410SMartin Brandenburg 		c_mask = (struct client_debug_mask *) mask;
79844f46410SMartin Brandenburg 
79944f46410SMartin Brandenburg 		if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
80044f46410SMartin Brandenburg 		    (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
80144f46410SMartin Brandenburg 			strcpy(client_debug_string, ORANGEFS_ALL);
80244f46410SMartin Brandenburg 			rc = 1;
80344f46410SMartin Brandenburg 			goto out;
80444f46410SMartin Brandenburg 		}
80544f46410SMartin Brandenburg 
80644f46410SMartin Brandenburg 		if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
80744f46410SMartin Brandenburg 		    (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
80844f46410SMartin Brandenburg 			strcpy(client_debug_string, ORANGEFS_VERBOSE);
80944f46410SMartin Brandenburg 			rc = 1;
81044f46410SMartin Brandenburg 			goto out;
81144f46410SMartin Brandenburg 		}
81244f46410SMartin Brandenburg 
81344f46410SMartin Brandenburg 	} else {
81444f46410SMartin Brandenburg 		k_mask = (__u64 *) mask;
81544f46410SMartin Brandenburg 
81644f46410SMartin Brandenburg 		if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
81744f46410SMartin Brandenburg 			strcpy(kernel_debug_string, ORANGEFS_ALL);
81844f46410SMartin Brandenburg 			rc = 1;
81944f46410SMartin Brandenburg 			goto out;
82044f46410SMartin Brandenburg 		}
82144f46410SMartin Brandenburg 	}
82244f46410SMartin Brandenburg 
82344f46410SMartin Brandenburg out:
82444f46410SMartin Brandenburg 
82544f46410SMartin Brandenburg 	return rc;
82644f46410SMartin Brandenburg }
82744f46410SMartin Brandenburg 
82844f46410SMartin Brandenburg /*
82944f46410SMartin Brandenburg  * kernel = type 0
83044f46410SMartin Brandenburg  * client = type 1
83144f46410SMartin Brandenburg  */
debug_string_to_mask(char * debug_string,void * mask,int type)83244f46410SMartin Brandenburg static void debug_string_to_mask(char *debug_string, void *mask, int type)
83344f46410SMartin Brandenburg {
83444f46410SMartin Brandenburg 	char *unchecked_keyword;
83544f46410SMartin Brandenburg 	int i;
83644f46410SMartin Brandenburg 	char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
83744f46410SMartin Brandenburg 	char *original_pointer;
83844f46410SMartin Brandenburg 	int element_count = 0;
83944f46410SMartin Brandenburg 	struct client_debug_mask *c_mask = NULL;
84044f46410SMartin Brandenburg 	__u64 *k_mask = NULL;
84144f46410SMartin Brandenburg 
84244f46410SMartin Brandenburg 	gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
84344f46410SMartin Brandenburg 
84444f46410SMartin Brandenburg 	if (type) {
84544f46410SMartin Brandenburg 		c_mask = (struct client_debug_mask *)mask;
84644f46410SMartin Brandenburg 		element_count = cdm_element_count;
84744f46410SMartin Brandenburg 	} else {
84844f46410SMartin Brandenburg 		k_mask = (__u64 *)mask;
84944f46410SMartin Brandenburg 		*k_mask = 0;
85044f46410SMartin Brandenburg 		element_count = num_kmod_keyword_mask_map;
85144f46410SMartin Brandenburg 	}
85244f46410SMartin Brandenburg 
85344f46410SMartin Brandenburg 	original_pointer = strsep_fodder;
85444f46410SMartin Brandenburg 	while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
85544f46410SMartin Brandenburg 		if (strlen(unchecked_keyword)) {
85644f46410SMartin Brandenburg 			for (i = 0; i < element_count; i++)
85744f46410SMartin Brandenburg 				if (type)
85844f46410SMartin Brandenburg 					do_c_mask(i,
85944f46410SMartin Brandenburg 						  unchecked_keyword,
86044f46410SMartin Brandenburg 						  &c_mask);
86144f46410SMartin Brandenburg 				else
86244f46410SMartin Brandenburg 					do_k_mask(i,
86344f46410SMartin Brandenburg 						  unchecked_keyword,
86444f46410SMartin Brandenburg 						  &k_mask);
86544f46410SMartin Brandenburg 		}
86644f46410SMartin Brandenburg 
86744f46410SMartin Brandenburg 	kfree(original_pointer);
86844f46410SMartin Brandenburg }
86944f46410SMartin Brandenburg 
do_c_mask(int i,char * unchecked_keyword,struct client_debug_mask ** sane_mask)87044f46410SMartin Brandenburg static void do_c_mask(int i, char *unchecked_keyword,
87144f46410SMartin Brandenburg     struct client_debug_mask **sane_mask)
87244f46410SMartin Brandenburg {
87344f46410SMartin Brandenburg 
87444f46410SMartin Brandenburg 	if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
87544f46410SMartin Brandenburg 		(**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
87644f46410SMartin Brandenburg 		(**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
87744f46410SMartin Brandenburg 	}
87844f46410SMartin Brandenburg }
87944f46410SMartin Brandenburg 
do_k_mask(int i,char * unchecked_keyword,__u64 ** sane_mask)88044f46410SMartin Brandenburg static void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
88144f46410SMartin Brandenburg {
88244f46410SMartin Brandenburg 
88344f46410SMartin Brandenburg 	if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
88444f46410SMartin Brandenburg 		**sane_mask = (**sane_mask) |
88544f46410SMartin Brandenburg 				s_kmod_keyword_mask_map[i].mask_val;
88644f46410SMartin Brandenburg }
88744f46410SMartin Brandenburg 
orangefs_debugfs_new_client_mask(void __user * arg)88844f46410SMartin Brandenburg int orangefs_debugfs_new_client_mask(void __user *arg)
88944f46410SMartin Brandenburg {
89044f46410SMartin Brandenburg 	struct dev_mask2_info_s mask2_info = {0};
89144f46410SMartin Brandenburg 	int ret;
89244f46410SMartin Brandenburg 
89344f46410SMartin Brandenburg 	ret = copy_from_user(&mask2_info,
89444f46410SMartin Brandenburg 			     (void __user *)arg,
89544f46410SMartin Brandenburg 			     sizeof(struct dev_mask2_info_s));
89644f46410SMartin Brandenburg 
89744f46410SMartin Brandenburg 	if (ret != 0)
89844f46410SMartin Brandenburg 		return -EIO;
89944f46410SMartin Brandenburg 
90044f46410SMartin Brandenburg 	client_debug_mask.mask1 = mask2_info.mask1_value;
90144f46410SMartin Brandenburg 	client_debug_mask.mask2 = mask2_info.mask2_value;
90244f46410SMartin Brandenburg 
90344f46410SMartin Brandenburg 	pr_info("%s: client debug mask has been been received "
90444f46410SMartin Brandenburg 		":%llx: :%llx:\n",
90544f46410SMartin Brandenburg 		__func__,
90644f46410SMartin Brandenburg 		(unsigned long long)client_debug_mask.mask1,
90744f46410SMartin Brandenburg 		(unsigned long long)client_debug_mask.mask2);
90844f46410SMartin Brandenburg 
90944f46410SMartin Brandenburg 	return ret;
91044f46410SMartin Brandenburg }
91144f46410SMartin Brandenburg 
orangefs_debugfs_new_client_string(void __user * arg)91244f46410SMartin Brandenburg int orangefs_debugfs_new_client_string(void __user *arg)
91344f46410SMartin Brandenburg {
91444f46410SMartin Brandenburg 	int ret;
91544f46410SMartin Brandenburg 
91644f46410SMartin Brandenburg 	ret = copy_from_user(&client_debug_array_string,
91744f46410SMartin Brandenburg 			     (void __user *)arg,
91844f46410SMartin Brandenburg 			     ORANGEFS_MAX_DEBUG_STRING_LEN);
919dc033621SMike Marshall 
920dc033621SMike Marshall 	if (ret != 0) {
921dc033621SMike Marshall 		pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
922dc033621SMike Marshall 			__func__);
92305973c2eSMike Marshall 		return -EFAULT;
924dc033621SMike Marshall 	}
92544f46410SMartin Brandenburg 
92644f46410SMartin Brandenburg 	/*
92744f46410SMartin Brandenburg 	 * The real client-core makes an effort to ensure
92844f46410SMartin Brandenburg 	 * that actual strings that aren't too long to fit in
92944f46410SMartin Brandenburg 	 * this buffer is what we get here. We're going to use
93044f46410SMartin Brandenburg 	 * string functions on the stuff we got, so we'll make
93144f46410SMartin Brandenburg 	 * this extra effort to try and keep from
93244f46410SMartin Brandenburg 	 * flowing out of this buffer when we use the string
93344f46410SMartin Brandenburg 	 * functions, even if somehow the stuff we end up
93444f46410SMartin Brandenburg 	 * with here is garbage.
93544f46410SMartin Brandenburg 	 */
93644f46410SMartin Brandenburg 	client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN - 1] =
93744f46410SMartin Brandenburg 		'\0';
93844f46410SMartin Brandenburg 
93944f46410SMartin Brandenburg 	pr_info("%s: client debug array string has been received.\n",
94044f46410SMartin Brandenburg 		__func__);
94144f46410SMartin Brandenburg 
94244f46410SMartin Brandenburg 	if (!help_string_initialized) {
94344f46410SMartin Brandenburg 
944dc033621SMike Marshall 		/* Build a proper debug help string. */
94505973c2eSMike Marshall 		ret = orangefs_prepare_debugfs_help_string(0);
94605973c2eSMike Marshall 		if (ret) {
94744f46410SMartin Brandenburg 			gossip_err("%s: no debug help string \n",
94844f46410SMartin Brandenburg 				   __func__);
94905973c2eSMike Marshall 			return ret;
95044f46410SMartin Brandenburg 		}
95144f46410SMartin Brandenburg 
95244f46410SMartin Brandenburg 	}
95344f46410SMartin Brandenburg 
95444f46410SMartin Brandenburg 	debug_mask_to_string(&client_debug_mask, 1);
95544f46410SMartin Brandenburg 
95644f46410SMartin Brandenburg 	debugfs_remove(client_debug_dentry);
95744f46410SMartin Brandenburg 
95844f46410SMartin Brandenburg 	orangefs_client_debug_init();
95944f46410SMartin Brandenburg 
96044f46410SMartin Brandenburg 	help_string_initialized++;
96144f46410SMartin Brandenburg 
96205973c2eSMike Marshall 	return 0;
96344f46410SMartin Brandenburg }
96444f46410SMartin Brandenburg 
orangefs_debugfs_new_debug(void __user * arg)96544f46410SMartin Brandenburg int orangefs_debugfs_new_debug(void __user *arg)
96644f46410SMartin Brandenburg {
96744f46410SMartin Brandenburg 	struct dev_mask_info_s mask_info = {0};
96844f46410SMartin Brandenburg 	int ret;
96944f46410SMartin Brandenburg 
97044f46410SMartin Brandenburg 	ret = copy_from_user(&mask_info,
97144f46410SMartin Brandenburg 			     (void __user *)arg,
97244f46410SMartin Brandenburg 			     sizeof(mask_info));
97344f46410SMartin Brandenburg 
97444f46410SMartin Brandenburg 	if (ret != 0)
97544f46410SMartin Brandenburg 		return -EIO;
97644f46410SMartin Brandenburg 
97744f46410SMartin Brandenburg 	if (mask_info.mask_type == KERNEL_MASK) {
97844f46410SMartin Brandenburg 		if ((mask_info.mask_value == 0)
97944f46410SMartin Brandenburg 		    && (kernel_mask_set_mod_init)) {
98044f46410SMartin Brandenburg 			/*
98144f46410SMartin Brandenburg 			 * the kernel debug mask was set when the
98244f46410SMartin Brandenburg 			 * kernel module was loaded; don't override
98344f46410SMartin Brandenburg 			 * it if the client-core was started without
98444f46410SMartin Brandenburg 			 * a value for ORANGEFS_KMODMASK.
98544f46410SMartin Brandenburg 			 */
98644f46410SMartin Brandenburg 			return 0;
98744f46410SMartin Brandenburg 		}
98844f46410SMartin Brandenburg 		debug_mask_to_string(&mask_info.mask_value,
98944f46410SMartin Brandenburg 				     mask_info.mask_type);
99044f46410SMartin Brandenburg 		orangefs_gossip_debug_mask = mask_info.mask_value;
99144f46410SMartin Brandenburg 		pr_info("%s: kernel debug mask has been modified to "
99244f46410SMartin Brandenburg 			":%s: :%llx:\n",
99344f46410SMartin Brandenburg 			__func__,
99444f46410SMartin Brandenburg 			kernel_debug_string,
99544f46410SMartin Brandenburg 			(unsigned long long)orangefs_gossip_debug_mask);
99644f46410SMartin Brandenburg 	} else if (mask_info.mask_type == CLIENT_MASK) {
99744f46410SMartin Brandenburg 		debug_mask_to_string(&mask_info.mask_value,
99844f46410SMartin Brandenburg 				     mask_info.mask_type);
99944f46410SMartin Brandenburg 		pr_info("%s: client debug mask has been modified to"
100044f46410SMartin Brandenburg 			":%s: :%llx:\n",
100144f46410SMartin Brandenburg 			__func__,
100244f46410SMartin Brandenburg 			client_debug_string,
100344f46410SMartin Brandenburg 			llu(mask_info.mask_value));
100444f46410SMartin Brandenburg 	} else {
100579d7cd61SMartin Brandenburg 		gossip_err("Invalid mask type....\n");
100644f46410SMartin Brandenburg 		return -EINVAL;
100744f46410SMartin Brandenburg 	}
100844f46410SMartin Brandenburg 
100944f46410SMartin Brandenburg 	return ret;
101044f46410SMartin Brandenburg }
1011