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