GNU Linux-libre 4.9.337-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         kfree(debug_help_string);
257         debug_help_string = NULL;
258 }
259
260 /* open ORANGEFS_KMOD_DEBUG_HELP_FILE */
261 static int orangefs_debug_help_open(struct inode *inode, struct file *file)
262 {
263         int rc = -ENODEV;
264         int ret;
265
266         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
267                      "orangefs_debug_help_open: start\n");
268
269         if (orangefs_debug_disabled)
270                 goto out;
271
272         ret = seq_open(file, &help_debug_ops);
273         if (ret)
274                 goto out;
275
276         ((struct seq_file *)(file->private_data))->private = inode->i_private;
277
278         rc = 0;
279
280 out:
281         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
282                      "orangefs_debug_help_open: rc:%d:\n",
283                      rc);
284         return rc;
285 }
286
287 /*
288  * I think start always gets called again after stop. Start
289  * needs to return NULL when it is done. The whole "payload"
290  * in this case is a single (long) string, so by the second
291  * time we get to start (pos = 1), we're done.
292  */
293 static void *help_start(struct seq_file *m, loff_t *pos)
294 {
295         void *payload = NULL;
296
297         gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_start: start\n");
298
299         mutex_lock(&orangefs_help_file_lock);
300
301         if (*pos == 0)
302                 payload = m->private;
303
304         return payload;
305 }
306
307 static void *help_next(struct seq_file *m, void *v, loff_t *pos)
308 {
309         (*pos)++;
310         gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
311
312         return NULL;
313 }
314
315 static void help_stop(struct seq_file *m, void *p)
316 {
317         gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_stop: start\n");
318         mutex_unlock(&orangefs_help_file_lock);
319 }
320
321 static int help_show(struct seq_file *m, void *v)
322 {
323         gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_show: start\n");
324
325         seq_puts(m, v);
326
327         return 0;
328 }
329
330 /*
331  * initialize the client-debug file.
332  */
333 int orangefs_client_debug_init(void)
334 {
335
336         int rc = -ENOMEM;
337         char *c_buffer = NULL;
338
339         gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
340
341         c_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
342         if (!c_buffer)
343                 goto out;
344
345         if (strlen(client_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
346                 strcpy(c_buffer, client_debug_string);
347                 strcat(c_buffer, "\n");
348         } else {
349                 strcpy(c_buffer, "none\n");
350                 pr_info("%s: overflow! 2\n", __func__);
351         }
352
353         client_debug_dentry = debugfs_create_file(ORANGEFS_CLIENT_DEBUG_FILE,
354                                                   0444,
355                                                   debug_dir,
356                                                   c_buffer,
357                                                   &kernel_debug_fops);
358         if (!client_debug_dentry) {
359                 pr_info("%s: failed to create updated %s.\n",
360                         __func__,
361                         ORANGEFS_CLIENT_DEBUG_FILE);
362                 goto out;
363         }
364
365         rc = 0;
366
367 out:
368
369         gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
370         return rc;
371 }
372
373 /* open ORANGEFS_KMOD_DEBUG_FILE or ORANGEFS_CLIENT_DEBUG_FILE.*/
374 static int orangefs_debug_open(struct inode *inode, struct file *file)
375 {
376         int rc = -ENODEV;
377
378         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
379                      "%s: orangefs_debug_disabled: %d\n",
380                      __func__,
381                      orangefs_debug_disabled);
382
383         if (orangefs_debug_disabled)
384                 goto out;
385
386         rc = 0;
387         mutex_lock(&orangefs_debug_lock);
388         file->private_data = inode->i_private;
389         mutex_unlock(&orangefs_debug_lock);
390
391 out:
392         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
393                      "orangefs_debug_open: rc: %d\n",
394                      rc);
395         return rc;
396 }
397
398 static ssize_t orangefs_debug_read(struct file *file,
399                                  char __user *ubuf,
400                                  size_t count,
401                                  loff_t *ppos)
402 {
403         char *buf;
404         int sprintf_ret;
405         ssize_t read_ret = -ENOMEM;
406
407         gossip_debug(GOSSIP_DEBUGFS_DEBUG, "orangefs_debug_read: start\n");
408
409         buf = kmalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
410         if (!buf)
411                 goto out;
412
413         mutex_lock(&orangefs_debug_lock);
414         sprintf_ret = sprintf(buf, "%s", (char *)file->private_data);
415         mutex_unlock(&orangefs_debug_lock);
416
417         read_ret = simple_read_from_buffer(ubuf, count, ppos, buf, sprintf_ret);
418
419         kfree(buf);
420
421 out:
422         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
423                      "orangefs_debug_read: ret: %zu\n",
424                      read_ret);
425
426         return read_ret;
427 }
428
429 static ssize_t orangefs_debug_write(struct file *file,
430                                   const char __user *ubuf,
431                                   size_t count,
432                                   loff_t *ppos)
433 {
434         char *buf;
435         int rc = -EFAULT;
436         size_t silly = 0;
437         char *debug_string;
438         struct orangefs_kernel_op_s *new_op = NULL;
439         struct client_debug_mask c_mask = { NULL, 0, 0 };
440
441         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
442                 "orangefs_debug_write: %pD\n",
443                 file);
444
445         /*
446          * Thwart users who try to jamb a ridiculous number
447          * of bytes into the debug file...
448          */
449         if (count > ORANGEFS_MAX_DEBUG_STRING_LEN + 1) {
450                 silly = count;
451                 count = ORANGEFS_MAX_DEBUG_STRING_LEN + 1;
452         }
453
454         buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
455         if (!buf)
456                 goto out;
457
458         if (copy_from_user(buf, ubuf, count - 1)) {
459                 gossip_debug(GOSSIP_DEBUGFS_DEBUG,
460                              "%s: copy_from_user failed!\n",
461                              __func__);
462                 goto out;
463         }
464
465         /*
466          * Map the keyword string from userspace into a valid debug mask.
467          * The mapping process involves mapping the human-inputted string
468          * into a valid mask, and then rebuilding the string from the
469          * verified valid mask.
470          *
471          * A service operation is required to set a new client-side
472          * debug mask.
473          */
474         if (!strcmp(file->f_path.dentry->d_name.name,
475                     ORANGEFS_KMOD_DEBUG_FILE)) {
476                 debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
477                 debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
478                 debug_string = kernel_debug_string;
479                 gossip_debug(GOSSIP_DEBUGFS_DEBUG,
480                              "New kernel debug string is %s\n",
481                              kernel_debug_string);
482         } else {
483                 /* Can't reset client debug mask if client is not running. */
484                 if (is_daemon_in_service()) {
485                         pr_info("%s: Client not running :%d:\n",
486                                 __func__,
487                                 is_daemon_in_service());
488                         goto out;
489                 }
490
491                 debug_string_to_mask(buf, &c_mask, 1);
492                 debug_mask_to_string(&c_mask, 1);
493                 debug_string = client_debug_string;
494
495                 new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
496                 if (!new_op) {
497                         pr_info("%s: op_alloc failed!\n", __func__);
498                         goto out;
499                 }
500
501                 new_op->upcall.req.param.op =
502                         ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
503                 new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
504                 memset(new_op->upcall.req.param.s_value,
505                        0,
506                        ORANGEFS_MAX_DEBUG_STRING_LEN);
507                 sprintf(new_op->upcall.req.param.s_value,
508                         "%llx %llx\n",
509                         c_mask.mask1,
510                         c_mask.mask2);
511
512                 /* service_operation returns 0 on success... */
513                 rc = service_operation(new_op,
514                                        "orangefs_param",
515                                         ORANGEFS_OP_INTERRUPTIBLE);
516
517                 if (rc)
518                         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
519                                      "%s: service_operation failed! rc:%d:\n",
520                                      __func__,
521                                      rc);
522
523                 op_release(new_op);
524         }
525
526         mutex_lock(&orangefs_debug_lock);
527         memset(file->f_inode->i_private, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
528         sprintf((char *)file->f_inode->i_private, "%s\n", debug_string);
529         mutex_unlock(&orangefs_debug_lock);
530
531         *ppos += count;
532         if (silly)
533                 rc = silly;
534         else
535                 rc = count;
536
537 out:
538         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
539                      "orangefs_debug_write: rc: %d\n",
540                      rc);
541         kfree(buf);
542         return rc;
543 }
544
545 /*
546  * After obtaining a string representation of the client's debug
547  * keywords and their associated masks, this function is called to build an
548  * array of these values.
549  */
550 static int orangefs_prepare_cdm_array(char *debug_array_string)
551 {
552         int i;
553         int rc = -EINVAL;
554         char *cds_head = NULL;
555         char *cds_delimiter = NULL;
556         int keyword_len = 0;
557
558         gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
559
560         /*
561          * figure out how many elements the cdm_array needs.
562          */
563         for (i = 0; i < strlen(debug_array_string); i++)
564                 if (debug_array_string[i] == '\n')
565                         cdm_element_count++;
566
567         if (!cdm_element_count) {
568                 pr_info("No elements in client debug array string!\n");
569                 goto out;
570         }
571
572         cdm_array =
573                 kzalloc(cdm_element_count * sizeof(struct client_debug_mask),
574                         GFP_KERNEL);
575         if (!cdm_array) {
576                 pr_info("malloc failed for cdm_array!\n");
577                 rc = -ENOMEM;
578                 goto out;
579         }
580
581         cds_head = debug_array_string;
582
583         for (i = 0; i < cdm_element_count; i++) {
584                 cds_delimiter = strchr(cds_head, '\n');
585                 *cds_delimiter = '\0';
586
587                 keyword_len = strcspn(cds_head, " ");
588
589                 cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
590                 if (!cdm_array[i].keyword) {
591                         rc = -ENOMEM;
592                         goto out;
593                 }
594
595                 sscanf(cds_head,
596                        "%s %llx %llx",
597                        cdm_array[i].keyword,
598                        (unsigned long long *)&(cdm_array[i].mask1),
599                        (unsigned long long *)&(cdm_array[i].mask2));
600
601                 if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
602                         client_verbose_index = i;
603
604                 if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
605                         client_all_index = i;
606
607                 cds_head = cds_delimiter + 1;
608         }
609
610         rc = cdm_element_count;
611
612         gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
613
614 out:
615
616         return rc;
617
618 }
619
620 /*
621  * /sys/kernel/debug/orangefs/debug-help can be catted to
622  * see all the available kernel and client debug keywords.
623  *
624  * When orangefs.ko initializes, we have no idea what keywords the
625  * client supports, nor their associated masks.
626  *
627  * We pass through this function once at module-load and stamp a
628  * boilerplate "we don't know" message for the client in the
629  * debug-help file. We pass through here again when the client
630  * starts and then we can fill out the debug-help file fully.
631  *
632  * The client might be restarted any number of times between
633  * module reloads, we only build the debug-help file the first time.
634  */
635 int orangefs_prepare_debugfs_help_string(int at_boot)
636 {
637         char *client_title = "Client Debug Keywords:\n";
638         char *kernel_title = "Kernel Debug Keywords:\n";
639         size_t string_size =  DEBUG_HELP_STRING_SIZE;
640         size_t result_size;
641         size_t i;
642         char *new;
643         int rc = -EINVAL;
644
645         gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
646
647         if (at_boot)
648                 client_title = HELP_STRING_UNINITIALIZED;
649
650         /* build a new debug_help_string. */
651         new = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
652         if (!new) {
653                 rc = -ENOMEM;
654                 goto out;
655         }
656
657         /*
658          * strlcat(dst, src, size) will append at most
659          * "size - strlen(dst) - 1" bytes of src onto dst,
660          * null terminating the result, and return the total
661          * length of the string it tried to create.
662          *
663          * We'll just plow through here building our new debug
664          * help string and let strlcat take care of assuring that
665          * dst doesn't overflow.
666          */
667         strlcat(new, client_title, string_size);
668
669         if (!at_boot) {
670
671                 /*
672                  * fill the client keyword/mask array and remember
673                  * how many elements there were.
674                  */
675                 cdm_element_count =
676                         orangefs_prepare_cdm_array(client_debug_array_string);
677                 if (cdm_element_count <= 0) {
678                         kfree(new);
679                         goto out;
680                 }
681
682                 for (i = 0; i < cdm_element_count; i++) {
683                         strlcat(new, "\t", string_size);
684                         strlcat(new, cdm_array[i].keyword, string_size);
685                         strlcat(new, "\n", string_size);
686                 }
687         }
688
689         strlcat(new, "\n", string_size);
690         strlcat(new, kernel_title, string_size);
691
692         for (i = 0; i < num_kmod_keyword_mask_map; i++) {
693                 strlcat(new, "\t", string_size);
694                 strlcat(new, s_kmod_keyword_mask_map[i].keyword, string_size);
695                 result_size = strlcat(new, "\n", string_size);
696         }
697
698         /* See if we tried to put too many bytes into "new"... */
699         if (result_size >= string_size) {
700                 kfree(new);
701                 goto out;
702         }
703
704         if (at_boot) {
705                 debug_help_string = new;
706         } else {
707                 mutex_lock(&orangefs_help_file_lock);
708                 memset(debug_help_string, 0, DEBUG_HELP_STRING_SIZE);
709                 strlcat(debug_help_string, new, string_size);
710                 mutex_unlock(&orangefs_help_file_lock);
711                 kfree(new);
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_lerr("Invalid mask type....\n");
1061                 return -EINVAL;
1062         }
1063
1064         return ret;
1065 }