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