Whamcloud - gitweb
LU-16002 ptlrpc: adds configurable ping interval
[fs/lustre-release.git] / lustre / obdclass / obd_sysfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/obdclass/obd_sysfs.c
32  *
33  * Object Devices Class Driver
34  * These are the only exported functions, they provide some generic
35  * infrastructure for managing object devices
36  */
37
38 #define DEBUG_SUBSYSTEM S_CLASS
39
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/lp.h>
45 #include <linux/slab.h>
46 #include <linux/ioport.h>
47 #include <linux/fcntl.h>
48 #include <linux/delay.h>
49 #include <linux/skbuff.h>
50 #include <linux/proc_fs.h>
51 #include <linux/fs.h>
52 #include <linux/poll.h>
53 #include <linux/init.h>
54 #include <linux/list.h>
55 #include <linux/highmem.h>
56 #include <asm/io.h>
57 #include <asm/ioctls.h>
58 #include <asm/poll.h>
59 #include <asm/uaccess.h>
60 #include <linux/miscdevice.h>
61 #include <linux/seq_file.h>
62 #include <linux/kobject.h>
63
64 #include <libcfs/libcfs.h>
65 #include <libcfs/libcfs_crypto.h>
66 #include <obd_support.h>
67 #include <obd_class.h>
68 #include <lprocfs_status.h>
69 #include <uapi/linux/lnet/lnetctl.h>
70 #include <uapi/linux/lustre/lustre_ioctl.h>
71 #include <uapi/linux/lustre/lustre_ver.h>
72
73 struct static_lustre_uintvalue_attr {
74         struct {
75                 struct attribute attr;
76                 ssize_t (*show)(struct kobject *kobj, struct attribute *attr,
77                                 char *buf);
78                 ssize_t (*store)(struct kobject *kobj, struct attribute *attr,
79                                  const char *buf, size_t len);
80         } u;
81         int *value;
82 };
83
84 static ssize_t static_uintvalue_show(struct kobject *kobj,
85                                      struct attribute *attr,
86                                      char *buf)
87 {
88         struct static_lustre_uintvalue_attr *lattr = (void *)attr;
89
90         return sprintf(buf, "%d\n", *lattr->value);
91 }
92
93 static ssize_t static_uintvalue_store(struct kobject *kobj,
94                                       struct attribute *attr,
95                                       const char *buffer, size_t count)
96 {
97         struct static_lustre_uintvalue_attr *lattr = (void *)attr;
98         unsigned int val;
99         int rc;
100
101         rc = kstrtouint(buffer, 10, &val);
102         if (rc)
103                 return rc;
104
105         *lattr->value = val;
106
107         return count;
108 }
109
110 #define LUSTRE_STATIC_UINT_ATTR(name, value)                            \
111 static struct static_lustre_uintvalue_attr lustre_sattr_##name =        \
112         { __ATTR(name, 0644, static_uintvalue_show,                     \
113                  static_uintvalue_store), value }
114
115 LUSTRE_STATIC_UINT_ATTR(debug_peer_on_timeout, &obd_debug_peer_on_timeout);
116 LUSTRE_STATIC_UINT_ATTR(dump_on_timeout, &obd_dump_on_timeout);
117 LUSTRE_STATIC_UINT_ATTR(dump_on_eviction, &obd_dump_on_eviction);
118 LUSTRE_STATIC_UINT_ATTR(at_min, &at_min);
119 LUSTRE_STATIC_UINT_ATTR(at_max, &at_max);
120 LUSTRE_STATIC_UINT_ATTR(at_extra, &at_extra);
121 LUSTRE_STATIC_UINT_ATTR(at_early_margin, &at_early_margin);
122 LUSTRE_STATIC_UINT_ATTR(at_history, &at_history);
123 LUSTRE_STATIC_UINT_ATTR(lbug_on_eviction, &obd_lbug_on_eviction);
124 LUSTRE_STATIC_UINT_ATTR(ping_interval, &ping_interval);
125 LUSTRE_STATIC_UINT_ATTR(evict_multiplier, &ping_evict_timeout_multiplier);
126
127 #ifdef HAVE_SERVER_SUPPORT
128 LUSTRE_STATIC_UINT_ATTR(ldlm_timeout, &ldlm_timeout);
129 LUSTRE_STATIC_UINT_ATTR(bulk_timeout, &bulk_timeout);
130 #endif
131
132 static ssize_t memused_show(struct kobject *kobj, struct attribute *attr,
133                             char *buf)
134 {
135         return sprintf(buf, "%llu\n", obd_memory_sum());
136 }
137 LUSTRE_RO_ATTR(memused);
138
139 static ssize_t memused_max_show(struct kobject *kobj, struct attribute *attr,
140                                 char *buf)
141 {
142         return sprintf(buf, "%llu\n", obd_memory_max());
143 }
144 LUSTRE_RO_ATTR(memused_max);
145
146 static ssize_t max_dirty_mb_show(struct kobject *kobj, struct attribute *attr,
147                                  char *buf)
148 {
149         return sprintf(buf, "%lu\n",
150                        obd_max_dirty_pages / (1 << (20 - PAGE_SHIFT)));
151 }
152
153 static ssize_t max_dirty_mb_store(struct kobject *kobj, struct attribute *attr,
154                                   const char *buffer, size_t count)
155 {
156         unsigned long val;
157         int rc;
158
159         rc = kstrtoul(buffer, 10, &val);
160         if (rc)
161                 return rc;
162
163         val *= 1 << (20 - PAGE_SHIFT); /* convert to pages */
164
165         if (val > ((cfs_totalram_pages() / 10) * 9)) {
166                 /* Somebody wants to assign too much memory to dirty pages */
167                 return -EINVAL;
168         }
169
170         if (val < 4 << (20 - PAGE_SHIFT)) {
171                 /* Less than 4 Mb for dirty cache is also bad */
172                 return -EINVAL;
173         }
174
175         obd_max_dirty_pages = val;
176
177         return count;
178 }
179 LUSTRE_RW_ATTR(max_dirty_mb);
180
181 #ifdef HAVE_SERVER_SUPPORT
182 static ssize_t no_transno_store(struct kobject *kobj,
183                                 struct attribute *attr,
184                                 const char *buffer, size_t count)
185 {
186         struct obd_device *obd;
187         unsigned int idx;
188         int rc;
189
190         rc = kstrtouint(buffer, 10, &idx);
191         if (rc)
192                 return rc;
193
194         obd = class_num2obd(idx);
195         if (!obd || !obd->obd_attached) {
196                 if (obd)
197                         CERROR("%s: not attached\n", obd->obd_name);
198                 return -ENODEV;
199         }
200
201         spin_lock(&obd->obd_dev_lock);
202         obd->obd_no_transno = 1;
203         spin_unlock(&obd->obd_dev_lock);
204         return count;
205 }
206 LUSTRE_WO_ATTR(no_transno);
207 #endif /* HAVE_SERVER_SUPPORT */
208
209 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
210                             char *buf)
211 {
212         return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
213 }
214
215 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
216                            char *buf)
217 {
218 #ifdef CONFIG_LUSTRE_FS_PINGER
219         const char *state = "on";
220 #else
221         const char *state = "off";
222 #endif
223         return sprintf(buf, "%s\n", state);
224 }
225
226 /**
227  * Check all obd devices health
228  *
229  * \param kobj
230  * \param buf [in]
231  *
232  * \retval number of characters printed if healthy
233  */
234 static ssize_t
235 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
236 {
237         bool healthy = true;
238         size_t len = 0;
239         int i;
240
241         if (libcfs_catastrophe)
242                 return sprintf(buf, "LBUG\n");
243
244         read_lock(&obd_dev_lock);
245         for (i = 0; i < class_devno_max(); i++) {
246                 struct obd_device *obd;
247
248                 obd = class_num2obd(i);
249                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
250                         continue;
251
252                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
253                 if (obd->obd_stopping)
254                         continue;
255
256                 if (obd->obd_read_only)
257                         continue;
258
259                 class_incref(obd, __func__, current);
260                 read_unlock(&obd_dev_lock);
261
262                 if (obd_health_check(NULL, obd))
263                         healthy = false;
264
265                 class_decref(obd, __func__, current);
266                 read_lock(&obd_dev_lock);
267
268                 if (!healthy)
269                         break;
270         }
271         read_unlock(&obd_dev_lock);
272
273         if (healthy)
274                 len = sprintf(buf, "healthy\n");
275         else
276                 len = sprintf(buf, "NOT HEALTHY\n");
277
278         return len;
279 }
280
281 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
282                               char *buf)
283 {
284         int rc = 0;
285
286         if (strlen(obd_jobid_var))
287                 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
288         return rc;
289 }
290
291 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
292                                const char *buffer, size_t count)
293 {
294         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
295                 return -EINVAL;
296
297         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
298
299         memcpy(obd_jobid_var, buffer, count);
300
301         /* Trim the trailing '\n' if any */
302         if (obd_jobid_var[count - 1] == '\n')
303                 obd_jobid_var[count - 1] = 0;
304
305         return count;
306 }
307
308 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
309                                char *buf)
310 {
311         int rc = 0;
312
313         if (strlen(obd_jobid_name))
314                 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
315         return rc;
316 }
317
318 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
319                                 const char *buffer, size_t count)
320 {
321         if (!count || count > LUSTRE_JOBID_SIZE)
322                 return -EINVAL;
323
324         if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
325             !strchr(buffer, '%')) {
326                 lustre_jobid_clear(buffer);
327                 return count;
328         }
329
330         /* clear previous value */
331         memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
332
333         memcpy(obd_jobid_name, buffer, count);
334
335         /* Trim the trailing '\n' if any */
336         if (obd_jobid_name[count - 1] == '\n') {
337                 /* Don't echo just a newline */
338                 if (count == 1)
339                         return -EINVAL;
340                 obd_jobid_name[count - 1] = 0;
341         }
342
343         return count;
344 }
345
346 static ssize_t jobid_this_session_show(struct kobject *kobj,
347                                        struct attribute *attr,
348                                        char *buf)
349 {
350         char *jid;
351         int ret = -ENOENT;
352
353         rcu_read_lock();
354         jid = jobid_current();
355         if (jid)
356                 ret = scnprintf(buf, PAGE_SIZE, "%s\n", jid);
357         rcu_read_unlock();
358         return ret;
359 }
360
361 static ssize_t jobid_this_session_store(struct kobject *kobj,
362                                         struct attribute *attr,
363                                         const char *buffer,
364                                         size_t count)
365 {
366         char *jobid;
367         int len;
368         int ret;
369
370         if (!count || count > LUSTRE_JOBID_SIZE)
371                 return -EINVAL;
372
373         jobid = kstrndup(buffer, count, GFP_KERNEL);
374         if (!jobid)
375                 return -ENOMEM;
376         len = strcspn(jobid, "\n ");
377         jobid[len] = '\0';
378         ret = jobid_set_current(jobid);
379         kfree(jobid);
380
381         return ret ?: count;
382 }
383
384 static ssize_t timeout_show(struct kobject *kobj,
385                             struct attribute *attr,
386                             char *buf)
387 {
388         return sprintf(buf, "%u\n", obd_timeout);
389 }
390
391 static ssize_t timeout_store(struct kobject *kobj,
392                              struct attribute *attr,
393                              const char *buffer,
394                              size_t count)
395 {
396         unsigned int val;
397         int rc;
398
399         rc = kstrtouint(buffer, 10, &val);
400         if (rc)
401                 return rc;
402         obd_timeout = val ?: 1U;
403         ping_interval = max(obd_timeout / 4, 1U);
404
405         return count;
406 }
407
408 /* Root for /sys/kernel/debug/lustre */
409 struct dentry *debugfs_lustre_root;
410 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
411
412 #ifdef CONFIG_PROC_FS
413 /* Root for /proc/fs/lustre */
414 struct proc_dir_entry *proc_lustre_root;
415 EXPORT_SYMBOL(proc_lustre_root);
416 #else
417 #define lprocfs_base NULL
418 #endif /* CONFIG_PROC_FS */
419
420 LUSTRE_RO_ATTR(version);
421 LUSTRE_RO_ATTR(pinger);
422 LUSTRE_RO_ATTR(health_check);
423 LUSTRE_RW_ATTR(jobid_var);
424 LUSTRE_RW_ATTR(jobid_name);
425 LUSTRE_RW_ATTR(jobid_this_session);
426 LUSTRE_RW_ATTR(timeout);
427
428 static struct attribute *lustre_attrs[] = {
429         &lustre_attr_version.attr,
430         &lustre_attr_pinger.attr,
431         &lustre_attr_health_check.attr,
432         &lustre_attr_jobid_name.attr,
433         &lustre_attr_jobid_var.attr,
434         &lustre_attr_jobid_this_session.attr,
435         &lustre_attr_timeout.attr,
436         &lustre_attr_max_dirty_mb.attr,
437         &lustre_sattr_debug_peer_on_timeout.u.attr,
438         &lustre_sattr_dump_on_timeout.u.attr,
439         &lustre_sattr_dump_on_eviction.u.attr,
440         &lustre_sattr_at_min.u.attr,
441         &lustre_sattr_at_max.u.attr,
442         &lustre_sattr_at_extra.u.attr,
443         &lustre_sattr_at_early_margin.u.attr,
444         &lustre_sattr_at_history.u.attr,
445         &lustre_attr_memused_max.attr,
446         &lustre_attr_memused.attr,
447 #ifdef HAVE_SERVER_SUPPORT
448         &lustre_sattr_ldlm_timeout.u.attr,
449         &lustre_sattr_bulk_timeout.u.attr,
450         &lustre_attr_no_transno.attr,
451 #endif
452         &lustre_sattr_lbug_on_eviction.u.attr,
453         &lustre_sattr_ping_interval.u.attr,
454         &lustre_sattr_evict_multiplier.u.attr,
455         NULL,
456 };
457
458 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
459 {
460         if (*pos >= class_devno_max())
461                 return NULL;
462
463         return pos;
464 }
465
466 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
467 {
468 }
469
470 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
471 {
472         ++*pos;
473         if (*pos >= class_devno_max())
474                 return NULL;
475
476         return pos;
477 }
478
479 static int obd_device_list_seq_show(struct seq_file *p, void *v)
480 {
481         loff_t index = *(loff_t *)v;
482         struct obd_device *obd = class_num2obd((int)index);
483         char *status;
484
485         if (obd == NULL)
486                 return 0;
487
488         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
489         if (obd->obd_stopping)
490                 status = "ST";
491         else if (obd->obd_inactive)
492                 status = "IN";
493         else if (obd->obd_set_up)
494                 status = "UP";
495         else if (obd->obd_attached)
496                 status = "AT";
497         else
498                 status = "--";
499
500         seq_printf(p, "%3d %s %s %s %s %d\n",
501                    (int)index, status, obd->obd_type->typ_name,
502                    obd->obd_name, obd->obd_uuid.uuid,
503                    atomic_read(&obd->obd_refcount));
504         return 0;
505 }
506
507 static const struct seq_operations obd_device_list_sops = {
508         .start = obd_device_list_seq_start,
509         .stop = obd_device_list_seq_stop,
510         .next = obd_device_list_seq_next,
511         .show = obd_device_list_seq_show,
512 };
513
514 static int obd_device_list_open(struct inode *inode, struct file *file)
515 {
516         struct seq_file *seq;
517         int rc = seq_open(file, &obd_device_list_sops);
518
519         if (rc)
520                 return rc;
521
522         seq = file->private_data;
523         seq->private = inode->i_private;
524         return 0;
525 }
526
527 static const struct file_operations obd_device_list_fops = {
528         .owner   = THIS_MODULE,
529         .open    = obd_device_list_open,
530         .read    = seq_read,
531         .llseek  = seq_lseek,
532         .release = seq_release,
533 };
534
535 /* checksum_speed */
536 static void *checksum_speed_start(struct seq_file *p, loff_t *pos)
537 {
538         return pos;
539 }
540
541 static void checksum_speed_stop(struct seq_file *p, void *v)
542 {
543 }
544
545 static void *checksum_speed_next(struct seq_file *p, void *v, loff_t *pos)
546 {
547         ++(*pos);
548         if (*pos >= CFS_HASH_ALG_SPEED_MAX - 1)
549                 return NULL;
550
551         return pos;
552 }
553
554 static int checksum_speed_show(struct seq_file *p, void *v)
555 {
556         loff_t index = *(loff_t *)v;
557
558         if (!index || index > CFS_HASH_ALG_SPEED_MAX - 1)
559                 return 0;
560
561         seq_printf(p, "%s: %d\n", cfs_crypto_hash_name(index),
562                    cfs_crypto_hash_speeds[index]);
563
564         return 0;
565 }
566
567 static const struct seq_operations checksum_speed_sops = {
568         .start = checksum_speed_start,
569         .stop = checksum_speed_stop,
570         .next = checksum_speed_next,
571         .show = checksum_speed_show,
572 };
573
574 static int checksum_speed_open(struct inode *inode, struct file *file)
575 {
576         int rc = seq_open(file, &checksum_speed_sops);
577
578         if (rc)
579                 return rc;
580
581         return 0;
582 }
583
584 static const struct file_operations checksum_speed_fops = {
585         .owner   = THIS_MODULE,
586         .open    = checksum_speed_open,
587         .read    = seq_read,
588         .llseek  = seq_lseek,
589         .release = seq_release,
590 };
591
592 static int
593 health_check_seq_show(struct seq_file *m, void *unused)
594 {
595         int i;
596
597         read_lock(&obd_dev_lock);
598         for (i = 0; i < class_devno_max(); i++) {
599                 struct obd_device *obd;
600
601                 obd = class_num2obd(i);
602                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
603                         continue;
604
605                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
606                 if (obd->obd_stopping)
607                         continue;
608
609                 class_incref(obd, __func__, current);
610                 read_unlock(&obd_dev_lock);
611
612                 if (obd_health_check(NULL, obd)) {
613                         seq_printf(m, "device %s reported unhealthy\n",
614                                    obd->obd_name);
615                 }
616                 class_decref(obd, __func__, current);
617                 read_lock(&obd_dev_lock);
618         }
619         read_unlock(&obd_dev_lock);
620
621         return 0;
622 }
623
624 LDEBUGFS_SEQ_FOPS_RO(health_check);
625
626 struct kset *lustre_kset;
627 EXPORT_SYMBOL_GPL(lustre_kset);
628
629 static struct attribute_group lustre_attr_group = {
630         .attrs = lustre_attrs,
631 };
632
633 ssize_t class_set_global(const char *param)
634 {
635         const char *value = strchr(param, '=') + 1;
636         size_t off = value - param - 1;
637         ssize_t count = -ENOENT;
638         int i;
639
640         for (i = 0; lustre_attrs[i]; i++) {
641                 if (!strncmp(lustre_attrs[i]->name, param, off)) {
642                         count = lustre_attr_store(&lustre_kset->kobj,
643                                                   lustre_attrs[i], value,
644                                                   strlen(value));
645                         break;
646                 }
647         }
648         return count;
649 }
650
651 int class_procfs_init(void)
652 {
653         struct proc_dir_entry *entry;
654         struct dentry *file;
655         int rc = -ENOMEM;
656
657         ENTRY;
658
659         lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
660         if (!lustre_kset)
661                 goto out;
662
663         /* Create the files associated with this kobject */
664         rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
665         if (rc) {
666                 kset_unregister(lustre_kset);
667                 goto out;
668         }
669
670         rc = jobid_cache_init();
671         if (rc) {
672                 kset_unregister(lustre_kset);
673                 goto out;
674         }
675
676         debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
677
678         file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
679                                    &obd_device_list_fops);
680
681         file = debugfs_create_file("health_check", 0444, debugfs_lustre_root,
682                                    NULL, &health_check_fops);
683
684         file = debugfs_create_file("checksum_speed", 0444, debugfs_lustre_root,
685                                    NULL, &checksum_speed_fops);
686
687         entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
688         if (IS_ERR(entry)) {
689                 rc = PTR_ERR(entry);
690                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
691                 debugfs_remove_recursive(debugfs_lustre_root);
692                 kset_unregister(lustre_kset);
693                 goto out;
694         }
695
696         proc_lustre_root = entry;
697 out:
698         RETURN(rc);
699 }
700
701 int class_procfs_clean(void)
702 {
703         ENTRY;
704
705         debugfs_remove_recursive(debugfs_lustre_root);
706
707         debugfs_lustre_root = NULL;
708         jobid_cache_fini();
709
710         if (proc_lustre_root)
711                 lprocfs_remove(&proc_lustre_root);
712
713         sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
714
715         kset_unregister(lustre_kset);
716
717         RETURN(0);
718 }