Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * What:        /sys/kernel/debug/orangefs/debug-help
0004  * Date:        June 2015
0005  * Contact:     Mike Marshall <hubcap@omnibond.com>
0006  * Description:
0007  *          List of client and kernel debug keywords.
0008  *
0009  *
0010  * What:        /sys/kernel/debug/orangefs/client-debug
0011  * Date:        June 2015
0012  * Contact:     Mike Marshall <hubcap@omnibond.com>
0013  * Description:
0014  *          Debug setting for "the client", the userspace
0015  *          helper for the kernel module.
0016  *
0017  *
0018  * What:        /sys/kernel/debug/orangefs/kernel-debug
0019  * Date:        June 2015
0020  * Contact:     Mike Marshall <hubcap@omnibond.com>
0021  * Description:
0022  *          Debug setting for the orangefs kernel module.
0023  *
0024  *          Any of the keywords, or comma-separated lists
0025  *          of keywords, from debug-help can be catted to
0026  *          client-debug or kernel-debug.
0027  *
0028  *          "none", "all" and "verbose" are special keywords
0029  *          for client-debug. Setting client-debug to "all"
0030  *          is kind of like trying to drink water from a
0031  *          fire hose, "verbose" triggers most of the same
0032  *          output except for the constant flow of output
0033  *          from the main wait loop.
0034  *
0035  *          "none" and "all" are similar settings for kernel-debug
0036  *          no need for a "verbose".
0037  */
0038 #include <linux/debugfs.h>
0039 #include <linux/slab.h>
0040 
0041 #include <linux/uaccess.h>
0042 
0043 #include "orangefs-debugfs.h"
0044 #include "protocol.h"
0045 #include "orangefs-kernel.h"
0046 
0047 #define DEBUG_HELP_STRING_SIZE 4096
0048 #define HELP_STRING_UNINITIALIZED \
0049     "Client Debug Keywords are unknown until the first time\n" \
0050     "the client is started after boot.\n"
0051 #define ORANGEFS_KMOD_DEBUG_HELP_FILE "debug-help"
0052 #define ORANGEFS_KMOD_DEBUG_FILE "kernel-debug"
0053 #define ORANGEFS_CLIENT_DEBUG_FILE "client-debug"
0054 #define ORANGEFS_VERBOSE "verbose"
0055 #define ORANGEFS_ALL "all"
0056 
0057 /*
0058  * An array of client_debug_mask will be built to hold debug keyword/mask
0059  * values fetched from userspace.
0060  */
0061 struct client_debug_mask {
0062     char *keyword;
0063     __u64 mask1;
0064     __u64 mask2;
0065 };
0066 
0067 static void orangefs_kernel_debug_init(void);
0068 
0069 static int orangefs_debug_help_open(struct inode *, struct file *);
0070 static void *help_start(struct seq_file *, loff_t *);
0071 static void *help_next(struct seq_file *, void *, loff_t *);
0072 static void help_stop(struct seq_file *, void *);
0073 static int help_show(struct seq_file *, void *);
0074 
0075 static int orangefs_debug_open(struct inode *, struct file *);
0076 
0077 static ssize_t orangefs_debug_read(struct file *,
0078                  char __user *,
0079                  size_t,
0080                  loff_t *);
0081 
0082 static ssize_t orangefs_debug_write(struct file *,
0083                   const char __user *,
0084                   size_t,
0085                   loff_t *);
0086 
0087 static int orangefs_prepare_cdm_array(char *);
0088 static void debug_mask_to_string(void *, int);
0089 static void do_k_string(void *, int);
0090 static void do_c_string(void *, int);
0091 static int keyword_is_amalgam(char *);
0092 static int check_amalgam_keyword(void *, int);
0093 static void debug_string_to_mask(char *, void *, int);
0094 static void do_c_mask(int, char *, struct client_debug_mask **);
0095 static void do_k_mask(int, char *, __u64 **);
0096 
0097 static char kernel_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN] = "none";
0098 static char *debug_help_string;
0099 static char client_debug_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
0100 static char client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN];
0101 
0102 static struct dentry *client_debug_dentry;
0103 static struct dentry *debug_dir;
0104 
0105 static unsigned int kernel_mask_set_mod_init;
0106 static int orangefs_debug_disabled = 1;
0107 static int help_string_initialized;
0108 
0109 static const struct seq_operations help_debug_ops = {
0110     .start  = help_start,
0111     .next   = help_next,
0112     .stop   = help_stop,
0113     .show   = help_show,
0114 };
0115 
0116 static const struct file_operations debug_help_fops = {
0117     .owner      = THIS_MODULE,
0118     .open           = orangefs_debug_help_open,
0119     .read           = seq_read,
0120     .release        = seq_release,
0121     .llseek         = seq_lseek,
0122 };
0123 
0124 static const struct file_operations kernel_debug_fops = {
0125     .owner      = THIS_MODULE,
0126     .open           = orangefs_debug_open,
0127     .read           = orangefs_debug_read,
0128     .write      = orangefs_debug_write,
0129     .llseek         = generic_file_llseek,
0130 };
0131 
0132 static int client_all_index;
0133 static int client_verbose_index;
0134 
0135 static struct client_debug_mask *cdm_array;
0136 static int cdm_element_count;
0137 
0138 static struct client_debug_mask client_debug_mask;
0139 
0140 /*
0141  * Used to protect data in ORANGEFS_KMOD_DEBUG_FILE and
0142  * ORANGEFS_KMOD_DEBUG_FILE.
0143  */
0144 static DEFINE_MUTEX(orangefs_debug_lock);
0145 
0146 /* Used to protect data in ORANGEFS_KMOD_DEBUG_HELP_FILE */
0147 static DEFINE_MUTEX(orangefs_help_file_lock);
0148 
0149 /*
0150  * initialize kmod debug operations, create orangefs debugfs dir and
0151  * ORANGEFS_KMOD_DEBUG_HELP_FILE.
0152  */
0153 void orangefs_debugfs_init(int debug_mask)
0154 {
0155     /* convert input debug mask to a 64-bit unsigned integer */
0156         orangefs_gossip_debug_mask = (unsigned long long)debug_mask;
0157 
0158     /*
0159      * set the kernel's gossip debug string; invalid mask values will
0160      * be ignored.
0161      */
0162     debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
0163 
0164     /* remove any invalid values from the mask */
0165     debug_string_to_mask(kernel_debug_string, &orangefs_gossip_debug_mask,
0166         0);
0167 
0168     /*
0169      * if the mask has a non-zero value, then indicate that the mask
0170      * was set when the kernel module was loaded.  The orangefs dev ioctl
0171      * command will look at this boolean to determine if the kernel's
0172      * debug mask should be overwritten when the client-core is started.
0173      */
0174     if (orangefs_gossip_debug_mask != 0)
0175         kernel_mask_set_mod_init = true;
0176 
0177     pr_info("%s: called with debug mask: :%s: :%llx:\n",
0178         __func__,
0179         kernel_debug_string,
0180         (unsigned long long)orangefs_gossip_debug_mask);
0181 
0182     debug_dir = debugfs_create_dir("orangefs", NULL);
0183 
0184     debugfs_create_file(ORANGEFS_KMOD_DEBUG_HELP_FILE, 0444, debug_dir,
0185                 debug_help_string, &debug_help_fops);
0186 
0187     orangefs_debug_disabled = 0;
0188 
0189     orangefs_kernel_debug_init();
0190 }
0191 
0192 /*
0193  * initialize the kernel-debug file.
0194  */
0195 static void orangefs_kernel_debug_init(void)
0196 {
0197     int rc = -ENOMEM;
0198     char *k_buffer = NULL;
0199 
0200     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
0201 
0202     k_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
0203     if (!k_buffer)
0204         goto out;
0205 
0206     if (strlen(kernel_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
0207         strcpy(k_buffer, kernel_debug_string);
0208         strcat(k_buffer, "\n");
0209     } else {
0210         strcpy(k_buffer, "none\n");
0211         pr_info("%s: overflow 1!\n", __func__);
0212     }
0213 
0214     debugfs_create_file(ORANGEFS_KMOD_DEBUG_FILE, 0444, debug_dir, k_buffer,
0215                 &kernel_debug_fops);
0216 
0217 out:
0218     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
0219 }
0220 
0221 
0222 void orangefs_debugfs_cleanup(void)
0223 {
0224     debugfs_remove_recursive(debug_dir);
0225 }
0226 
0227 /* open ORANGEFS_KMOD_DEBUG_HELP_FILE */
0228 static int orangefs_debug_help_open(struct inode *inode, struct file *file)
0229 {
0230     int rc = -ENODEV;
0231     int ret;
0232 
0233     gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0234              "orangefs_debug_help_open: start\n");
0235 
0236     if (orangefs_debug_disabled)
0237         goto out;
0238 
0239     ret = seq_open(file, &help_debug_ops);
0240     if (ret)
0241         goto out;
0242 
0243     ((struct seq_file *)(file->private_data))->private = inode->i_private;
0244 
0245     rc = 0;
0246 
0247 out:
0248     gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0249              "orangefs_debug_help_open: rc:%d:\n",
0250              rc);
0251     return rc;
0252 }
0253 
0254 /*
0255  * I think start always gets called again after stop. Start
0256  * needs to return NULL when it is done. The whole "payload"
0257  * in this case is a single (long) string, so by the second
0258  * time we get to start (pos = 1), we're done.
0259  */
0260 static void *help_start(struct seq_file *m, loff_t *pos)
0261 {
0262     void *payload = NULL;
0263 
0264     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_start: start\n");
0265 
0266     mutex_lock(&orangefs_help_file_lock);
0267 
0268     if (*pos == 0)
0269         payload = m->private;
0270 
0271     return payload;
0272 }
0273 
0274 static void *help_next(struct seq_file *m, void *v, loff_t *pos)
0275 {
0276     (*pos)++;
0277     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
0278 
0279     return NULL;
0280 }
0281 
0282 static void help_stop(struct seq_file *m, void *p)
0283 {
0284     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_stop: start\n");
0285     mutex_unlock(&orangefs_help_file_lock);
0286 }
0287 
0288 static int help_show(struct seq_file *m, void *v)
0289 {
0290     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_show: start\n");
0291 
0292     seq_puts(m, v);
0293 
0294     return 0;
0295 }
0296 
0297 /*
0298  * initialize the client-debug file.
0299  */
0300 static int orangefs_client_debug_init(void)
0301 {
0302 
0303     int rc = -ENOMEM;
0304     char *c_buffer = NULL;
0305 
0306     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: start\n", __func__);
0307 
0308     c_buffer = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
0309     if (!c_buffer)
0310         goto out;
0311 
0312     if (strlen(client_debug_string) + 1 < ORANGEFS_MAX_DEBUG_STRING_LEN) {
0313         strcpy(c_buffer, client_debug_string);
0314         strcat(c_buffer, "\n");
0315     } else {
0316         strcpy(c_buffer, "none\n");
0317         pr_info("%s: overflow! 2\n", __func__);
0318     }
0319 
0320     client_debug_dentry = debugfs_create_file(ORANGEFS_CLIENT_DEBUG_FILE,
0321                           0444,
0322                           debug_dir,
0323                           c_buffer,
0324                           &kernel_debug_fops);
0325 
0326     rc = 0;
0327 
0328 out:
0329 
0330     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "%s: rc:%d:\n", __func__, rc);
0331     return rc;
0332 }
0333 
0334 /* open ORANGEFS_KMOD_DEBUG_FILE or ORANGEFS_CLIENT_DEBUG_FILE.*/
0335 static int orangefs_debug_open(struct inode *inode, struct file *file)
0336 {
0337     int rc = -ENODEV;
0338 
0339     gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0340              "%s: orangefs_debug_disabled: %d\n",
0341              __func__,
0342              orangefs_debug_disabled);
0343 
0344     if (orangefs_debug_disabled)
0345         goto out;
0346 
0347     rc = 0;
0348     mutex_lock(&orangefs_debug_lock);
0349     file->private_data = inode->i_private;
0350     mutex_unlock(&orangefs_debug_lock);
0351 
0352 out:
0353     gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0354              "orangefs_debug_open: rc: %d\n",
0355              rc);
0356     return rc;
0357 }
0358 
0359 static ssize_t orangefs_debug_read(struct file *file,
0360                  char __user *ubuf,
0361                  size_t count,
0362                  loff_t *ppos)
0363 {
0364     char *buf;
0365     int sprintf_ret;
0366     ssize_t read_ret = -ENOMEM;
0367 
0368     gossip_debug(GOSSIP_DEBUGFS_DEBUG, "orangefs_debug_read: start\n");
0369 
0370     buf = kmalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
0371     if (!buf)
0372         goto out;
0373 
0374     mutex_lock(&orangefs_debug_lock);
0375     sprintf_ret = sprintf(buf, "%s", (char *)file->private_data);
0376     mutex_unlock(&orangefs_debug_lock);
0377 
0378     read_ret = simple_read_from_buffer(ubuf, count, ppos, buf, sprintf_ret);
0379 
0380     kfree(buf);
0381 
0382 out:
0383     gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0384              "orangefs_debug_read: ret: %zu\n",
0385              read_ret);
0386 
0387     return read_ret;
0388 }
0389 
0390 static ssize_t orangefs_debug_write(struct file *file,
0391                   const char __user *ubuf,
0392                   size_t count,
0393                   loff_t *ppos)
0394 {
0395     char *buf;
0396     int rc = -EFAULT;
0397     size_t silly = 0;
0398     char *debug_string;
0399     struct orangefs_kernel_op_s *new_op = NULL;
0400     struct client_debug_mask c_mask = { NULL, 0, 0 };
0401     char *s;
0402 
0403     gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0404         "orangefs_debug_write: %pD\n",
0405         file);
0406 
0407     if (count == 0)
0408         return 0;
0409 
0410     /*
0411      * Thwart users who try to jamb a ridiculous number
0412      * of bytes into the debug file...
0413      */
0414     if (count > ORANGEFS_MAX_DEBUG_STRING_LEN + 1) {
0415         silly = count;
0416         count = ORANGEFS_MAX_DEBUG_STRING_LEN + 1;
0417     }
0418 
0419     buf = kzalloc(ORANGEFS_MAX_DEBUG_STRING_LEN, GFP_KERNEL);
0420     if (!buf)
0421         goto out;
0422 
0423     if (copy_from_user(buf, ubuf, count - 1)) {
0424         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0425                  "%s: copy_from_user failed!\n",
0426                  __func__);
0427         goto out;
0428     }
0429 
0430     /*
0431      * Map the keyword string from userspace into a valid debug mask.
0432      * The mapping process involves mapping the human-inputted string
0433      * into a valid mask, and then rebuilding the string from the
0434      * verified valid mask.
0435      *
0436      * A service operation is required to set a new client-side
0437      * debug mask.
0438      */
0439     if (!strcmp(file->f_path.dentry->d_name.name,
0440             ORANGEFS_KMOD_DEBUG_FILE)) {
0441         debug_string_to_mask(buf, &orangefs_gossip_debug_mask, 0);
0442         debug_mask_to_string(&orangefs_gossip_debug_mask, 0);
0443         debug_string = kernel_debug_string;
0444         gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0445                  "New kernel debug string is %s\n",
0446                  kernel_debug_string);
0447     } else {
0448         /* Can't reset client debug mask if client is not running. */
0449         if (is_daemon_in_service()) {
0450             pr_info("%s: Client not running :%d:\n",
0451                 __func__,
0452                 is_daemon_in_service());
0453             goto out;
0454         }
0455 
0456         debug_string_to_mask(buf, &c_mask, 1);
0457         debug_mask_to_string(&c_mask, 1);
0458         debug_string = client_debug_string;
0459 
0460         new_op = op_alloc(ORANGEFS_VFS_OP_PARAM);
0461         if (!new_op) {
0462             pr_info("%s: op_alloc failed!\n", __func__);
0463             goto out;
0464         }
0465 
0466         new_op->upcall.req.param.op =
0467             ORANGEFS_PARAM_REQUEST_OP_TWO_MASK_VALUES;
0468         new_op->upcall.req.param.type = ORANGEFS_PARAM_REQUEST_SET;
0469         memset(new_op->upcall.req.param.s_value,
0470                0,
0471                ORANGEFS_MAX_DEBUG_STRING_LEN);
0472         sprintf(new_op->upcall.req.param.s_value,
0473             "%llx %llx\n",
0474             c_mask.mask1,
0475             c_mask.mask2);
0476 
0477         /* service_operation returns 0 on success... */
0478         rc = service_operation(new_op,
0479                        "orangefs_param",
0480                     ORANGEFS_OP_INTERRUPTIBLE);
0481 
0482         if (rc)
0483             gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0484                      "%s: service_operation failed! rc:%d:\n",
0485                      __func__,
0486                      rc);
0487 
0488         op_release(new_op);
0489     }
0490 
0491     mutex_lock(&orangefs_debug_lock);
0492     s = file_inode(file)->i_private;
0493     memset(s, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
0494     sprintf(s, "%s\n", debug_string);
0495     mutex_unlock(&orangefs_debug_lock);
0496 
0497     *ppos += count;
0498     if (silly)
0499         rc = silly;
0500     else
0501         rc = count;
0502 
0503 out:
0504     gossip_debug(GOSSIP_DEBUGFS_DEBUG,
0505              "orangefs_debug_write: rc: %d\n",
0506              rc);
0507     kfree(buf);
0508     return rc;
0509 }
0510 
0511 /*
0512  * After obtaining a string representation of the client's debug
0513  * keywords and their associated masks, this function is called to build an
0514  * array of these values.
0515  */
0516 static int orangefs_prepare_cdm_array(char *debug_array_string)
0517 {
0518     int i;
0519     int rc = -EINVAL;
0520     char *cds_head = NULL;
0521     char *cds_delimiter = NULL;
0522     int keyword_len = 0;
0523 
0524     gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
0525 
0526     /*
0527      * figure out how many elements the cdm_array needs.
0528      */
0529     for (i = 0; i < strlen(debug_array_string); i++)
0530         if (debug_array_string[i] == '\n')
0531             cdm_element_count++;
0532 
0533     if (!cdm_element_count) {
0534         pr_info("No elements in client debug array string!\n");
0535         goto out;
0536     }
0537 
0538     cdm_array = kcalloc(cdm_element_count, sizeof(*cdm_array), GFP_KERNEL);
0539     if (!cdm_array) {
0540         rc = -ENOMEM;
0541         goto out;
0542     }
0543 
0544     cds_head = debug_array_string;
0545 
0546     for (i = 0; i < cdm_element_count; i++) {
0547         cds_delimiter = strchr(cds_head, '\n');
0548         *cds_delimiter = '\0';
0549 
0550         keyword_len = strcspn(cds_head, " ");
0551 
0552         cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
0553         if (!cdm_array[i].keyword) {
0554             rc = -ENOMEM;
0555             goto out;
0556         }
0557 
0558         sscanf(cds_head,
0559                "%s %llx %llx",
0560                cdm_array[i].keyword,
0561                (unsigned long long *)&(cdm_array[i].mask1),
0562                (unsigned long long *)&(cdm_array[i].mask2));
0563 
0564         if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
0565             client_verbose_index = i;
0566 
0567         if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
0568             client_all_index = i;
0569 
0570         cds_head = cds_delimiter + 1;
0571     }
0572 
0573     rc = cdm_element_count;
0574 
0575     gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
0576 
0577 out:
0578 
0579     return rc;
0580 
0581 }
0582 
0583 /*
0584  * /sys/kernel/debug/orangefs/debug-help can be catted to
0585  * see all the available kernel and client debug keywords.
0586  *
0587  * When orangefs.ko initializes, we have no idea what keywords the
0588  * client supports, nor their associated masks.
0589  *
0590  * We pass through this function once at module-load and stamp a
0591  * boilerplate "we don't know" message for the client in the
0592  * debug-help file. We pass through here again when the client
0593  * starts and then we can fill out the debug-help file fully.
0594  *
0595  * The client might be restarted any number of times between
0596  * module reloads, we only build the debug-help file the first time.
0597  */
0598 int orangefs_prepare_debugfs_help_string(int at_boot)
0599 {
0600     char *client_title = "Client Debug Keywords:\n";
0601     char *kernel_title = "Kernel Debug Keywords:\n";
0602     size_t string_size =  DEBUG_HELP_STRING_SIZE;
0603     size_t result_size;
0604     size_t i;
0605     char *new;
0606     int rc = -EINVAL;
0607 
0608     gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
0609 
0610     if (at_boot)
0611         client_title = HELP_STRING_UNINITIALIZED;
0612 
0613     /* build a new debug_help_string. */
0614     new = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
0615     if (!new) {
0616         rc = -ENOMEM;
0617         goto out;
0618     }
0619 
0620     /*
0621      * strlcat(dst, src, size) will append at most
0622      * "size - strlen(dst) - 1" bytes of src onto dst,
0623      * null terminating the result, and return the total
0624      * length of the string it tried to create.
0625      *
0626      * We'll just plow through here building our new debug
0627      * help string and let strlcat take care of assuring that
0628      * dst doesn't overflow.
0629      */
0630     strlcat(new, client_title, string_size);
0631 
0632     if (!at_boot) {
0633 
0634                 /*
0635          * fill the client keyword/mask array and remember
0636          * how many elements there were.
0637          */
0638         cdm_element_count =
0639             orangefs_prepare_cdm_array(client_debug_array_string);
0640         if (cdm_element_count <= 0) {
0641             kfree(new);
0642             goto out;
0643         }
0644 
0645         for (i = 0; i < cdm_element_count; i++) {
0646             strlcat(new, "\t", string_size);
0647             strlcat(new, cdm_array[i].keyword, string_size);
0648             strlcat(new, "\n", string_size);
0649         }
0650     }
0651 
0652     strlcat(new, "\n", string_size);
0653     strlcat(new, kernel_title, string_size);
0654 
0655     for (i = 0; i < num_kmod_keyword_mask_map; i++) {
0656         strlcat(new, "\t", string_size);
0657         strlcat(new, s_kmod_keyword_mask_map[i].keyword, string_size);
0658         result_size = strlcat(new, "\n", string_size);
0659     }
0660 
0661     /* See if we tried to put too many bytes into "new"... */
0662     if (result_size >= string_size) {
0663         kfree(new);
0664         goto out;
0665     }
0666 
0667     if (at_boot) {
0668         debug_help_string = new;
0669     } else {
0670         mutex_lock(&orangefs_help_file_lock);
0671         memset(debug_help_string, 0, DEBUG_HELP_STRING_SIZE);
0672         strlcat(debug_help_string, new, string_size);
0673         mutex_unlock(&orangefs_help_file_lock);
0674     }
0675 
0676     rc = 0;
0677 
0678 out:    return rc;
0679 
0680 }
0681 
0682 /*
0683  * kernel = type 0
0684  * client = type 1
0685  */
0686 static void debug_mask_to_string(void *mask, int type)
0687 {
0688     int i;
0689     int len = 0;
0690     char *debug_string;
0691     int element_count = 0;
0692 
0693     gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
0694 
0695     if (type) {
0696         debug_string = client_debug_string;
0697         element_count = cdm_element_count;
0698     } else {
0699         debug_string = kernel_debug_string;
0700         element_count = num_kmod_keyword_mask_map;
0701     }
0702 
0703     memset(debug_string, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
0704 
0705     /*
0706      * Some keywords, like "all" or "verbose", are amalgams of
0707      * numerous other keywords. Make a special check for those
0708      * before grinding through the whole mask only to find out
0709      * later...
0710      */
0711     if (check_amalgam_keyword(mask, type))
0712         goto out;
0713 
0714     /* Build the debug string. */
0715     for (i = 0; i < element_count; i++)
0716         if (type)
0717             do_c_string(mask, i);
0718         else
0719             do_k_string(mask, i);
0720 
0721     len = strlen(debug_string);
0722 
0723     if ((len) && (type))
0724         client_debug_string[len - 1] = '\0';
0725     else if (len)
0726         kernel_debug_string[len - 1] = '\0';
0727     else if (type)
0728         strcpy(client_debug_string, "none");
0729     else
0730         strcpy(kernel_debug_string, "none");
0731 
0732 out:
0733 gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
0734 
0735     return;
0736 
0737 }
0738 
0739 static void do_k_string(void *k_mask, int index)
0740 {
0741     __u64 *mask = (__u64 *) k_mask;
0742 
0743     if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
0744         goto out;
0745 
0746     if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
0747         if ((strlen(kernel_debug_string) +
0748              strlen(s_kmod_keyword_mask_map[index].keyword))
0749             < ORANGEFS_MAX_DEBUG_STRING_LEN - 1) {
0750                 strcat(kernel_debug_string,
0751                        s_kmod_keyword_mask_map[index].keyword);
0752                 strcat(kernel_debug_string, ",");
0753             } else {
0754                 gossip_err("%s: overflow!\n", __func__);
0755                 strcpy(kernel_debug_string, ORANGEFS_ALL);
0756                 goto out;
0757             }
0758     }
0759 
0760 out:
0761 
0762     return;
0763 }
0764 
0765 static void do_c_string(void *c_mask, int index)
0766 {
0767     struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
0768 
0769     if (keyword_is_amalgam(cdm_array[index].keyword))
0770         goto out;
0771 
0772     if ((mask->mask1 & cdm_array[index].mask1) ||
0773         (mask->mask2 & cdm_array[index].mask2)) {
0774         if ((strlen(client_debug_string) +
0775              strlen(cdm_array[index].keyword) + 1)
0776             < ORANGEFS_MAX_DEBUG_STRING_LEN - 2) {
0777                 strcat(client_debug_string,
0778                        cdm_array[index].keyword);
0779                 strcat(client_debug_string, ",");
0780             } else {
0781                 gossip_err("%s: overflow!\n", __func__);
0782                 strcpy(client_debug_string, ORANGEFS_ALL);
0783                 goto out;
0784             }
0785     }
0786 out:
0787     return;
0788 }
0789 
0790 static int keyword_is_amalgam(char *keyword)
0791 {
0792     int rc = 0;
0793 
0794     if ((!strcmp(keyword, ORANGEFS_ALL)) || (!strcmp(keyword, ORANGEFS_VERBOSE)))
0795         rc = 1;
0796 
0797     return rc;
0798 }
0799 
0800 /*
0801  * kernel = type 0
0802  * client = type 1
0803  *
0804  * return 1 if we found an amalgam.
0805  */
0806 static int check_amalgam_keyword(void *mask, int type)
0807 {
0808     __u64 *k_mask;
0809     struct client_debug_mask *c_mask;
0810     int k_all_index = num_kmod_keyword_mask_map - 1;
0811     int rc = 0;
0812 
0813     if (type) {
0814         c_mask = (struct client_debug_mask *) mask;
0815 
0816         if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
0817             (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
0818             strcpy(client_debug_string, ORANGEFS_ALL);
0819             rc = 1;
0820             goto out;
0821         }
0822 
0823         if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
0824             (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
0825             strcpy(client_debug_string, ORANGEFS_VERBOSE);
0826             rc = 1;
0827             goto out;
0828         }
0829 
0830     } else {
0831         k_mask = (__u64 *) mask;
0832 
0833         if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
0834             strcpy(kernel_debug_string, ORANGEFS_ALL);
0835             rc = 1;
0836             goto out;
0837         }
0838     }
0839 
0840 out:
0841 
0842     return rc;
0843 }
0844 
0845 /*
0846  * kernel = type 0
0847  * client = type 1
0848  */
0849 static void debug_string_to_mask(char *debug_string, void *mask, int type)
0850 {
0851     char *unchecked_keyword;
0852     int i;
0853     char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
0854     char *original_pointer;
0855     int element_count = 0;
0856     struct client_debug_mask *c_mask = NULL;
0857     __u64 *k_mask = NULL;
0858 
0859     gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
0860 
0861     if (type) {
0862         c_mask = (struct client_debug_mask *)mask;
0863         element_count = cdm_element_count;
0864     } else {
0865         k_mask = (__u64 *)mask;
0866         *k_mask = 0;
0867         element_count = num_kmod_keyword_mask_map;
0868     }
0869 
0870     original_pointer = strsep_fodder;
0871     while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
0872         if (strlen(unchecked_keyword)) {
0873             for (i = 0; i < element_count; i++)
0874                 if (type)
0875                     do_c_mask(i,
0876                           unchecked_keyword,
0877                           &c_mask);
0878                 else
0879                     do_k_mask(i,
0880                           unchecked_keyword,
0881                           &k_mask);
0882         }
0883 
0884     kfree(original_pointer);
0885 }
0886 
0887 static void do_c_mask(int i, char *unchecked_keyword,
0888     struct client_debug_mask **sane_mask)
0889 {
0890 
0891     if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
0892         (**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
0893         (**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
0894     }
0895 }
0896 
0897 static void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
0898 {
0899 
0900     if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
0901         **sane_mask = (**sane_mask) |
0902                 s_kmod_keyword_mask_map[i].mask_val;
0903 }
0904 
0905 int orangefs_debugfs_new_client_mask(void __user *arg)
0906 {
0907     struct dev_mask2_info_s mask2_info = {0};
0908     int ret;
0909 
0910     ret = copy_from_user(&mask2_info,
0911                  (void __user *)arg,
0912                  sizeof(struct dev_mask2_info_s));
0913 
0914     if (ret != 0)
0915         return -EIO;
0916 
0917     client_debug_mask.mask1 = mask2_info.mask1_value;
0918     client_debug_mask.mask2 = mask2_info.mask2_value;
0919 
0920     pr_info("%s: client debug mask has been been received "
0921         ":%llx: :%llx:\n",
0922         __func__,
0923         (unsigned long long)client_debug_mask.mask1,
0924         (unsigned long long)client_debug_mask.mask2);
0925 
0926     return ret;
0927 }
0928 
0929 int orangefs_debugfs_new_client_string(void __user *arg)
0930 {
0931     int ret;
0932 
0933     ret = copy_from_user(&client_debug_array_string,
0934                  (void __user *)arg,
0935                  ORANGEFS_MAX_DEBUG_STRING_LEN);
0936 
0937     if (ret != 0) {
0938         pr_info("%s: CLIENT_STRING: copy_from_user failed\n",
0939             __func__);
0940         return -EFAULT;
0941     }
0942 
0943     /*
0944      * The real client-core makes an effort to ensure
0945      * that actual strings that aren't too long to fit in
0946      * this buffer is what we get here. We're going to use
0947      * string functions on the stuff we got, so we'll make
0948      * this extra effort to try and keep from
0949      * flowing out of this buffer when we use the string
0950      * functions, even if somehow the stuff we end up
0951      * with here is garbage.
0952      */
0953     client_debug_array_string[ORANGEFS_MAX_DEBUG_STRING_LEN - 1] =
0954         '\0';
0955 
0956     pr_info("%s: client debug array string has been received.\n",
0957         __func__);
0958 
0959     if (!help_string_initialized) {
0960 
0961         /* Build a proper debug help string. */
0962         ret = orangefs_prepare_debugfs_help_string(0);
0963         if (ret) {
0964             gossip_err("%s: no debug help string \n",
0965                    __func__);
0966             return ret;
0967         }
0968 
0969     }
0970 
0971     debug_mask_to_string(&client_debug_mask, 1);
0972 
0973     debugfs_remove(client_debug_dentry);
0974 
0975     orangefs_client_debug_init();
0976 
0977     help_string_initialized++;
0978 
0979     return 0;
0980 }
0981 
0982 int orangefs_debugfs_new_debug(void __user *arg)
0983 {
0984     struct dev_mask_info_s mask_info = {0};
0985     int ret;
0986 
0987     ret = copy_from_user(&mask_info,
0988                  (void __user *)arg,
0989                  sizeof(mask_info));
0990 
0991     if (ret != 0)
0992         return -EIO;
0993 
0994     if (mask_info.mask_type == KERNEL_MASK) {
0995         if ((mask_info.mask_value == 0)
0996             && (kernel_mask_set_mod_init)) {
0997             /*
0998              * the kernel debug mask was set when the
0999              * kernel module was loaded; don't override
1000              * it if the client-core was started without
1001              * a value for ORANGEFS_KMODMASK.
1002              */
1003             return 0;
1004         }
1005         debug_mask_to_string(&mask_info.mask_value,
1006                      mask_info.mask_type);
1007         orangefs_gossip_debug_mask = mask_info.mask_value;
1008         pr_info("%s: kernel debug mask has been modified to "
1009             ":%s: :%llx:\n",
1010             __func__,
1011             kernel_debug_string,
1012             (unsigned long long)orangefs_gossip_debug_mask);
1013     } else if (mask_info.mask_type == CLIENT_MASK) {
1014         debug_mask_to_string(&mask_info.mask_value,
1015                      mask_info.mask_type);
1016         pr_info("%s: client debug mask has been modified to"
1017             ":%s: :%llx:\n",
1018             __func__,
1019             client_debug_string,
1020             llu(mask_info.mask_value));
1021     } else {
1022         gossip_err("Invalid mask type....\n");
1023         return -EINVAL;
1024     }
1025 
1026     return ret;
1027 }