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