Whamcloud - gitweb
b293e065f02ecc93db19a8f9cd74a033eec0c5c3
[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(timeout, &obd_timeout);
116 LUSTRE_STATIC_UINT_ATTR(debug_peer_on_timeout, &obd_debug_peer_on_timeout);
117 LUSTRE_STATIC_UINT_ATTR(dump_on_timeout, &obd_dump_on_timeout);
118 LUSTRE_STATIC_UINT_ATTR(dump_on_eviction, &obd_dump_on_eviction);
119 LUSTRE_STATIC_UINT_ATTR(at_min, &at_min);
120 LUSTRE_STATIC_UINT_ATTR(at_max, &at_max);
121 LUSTRE_STATIC_UINT_ATTR(at_extra, &at_extra);
122 LUSTRE_STATIC_UINT_ATTR(at_early_margin, &at_early_margin);
123 LUSTRE_STATIC_UINT_ATTR(at_history, &at_history);
124 LUSTRE_STATIC_UINT_ATTR(lbug_on_eviction, &obd_lbug_on_eviction);
125
126 #ifdef HAVE_SERVER_SUPPORT
127 LUSTRE_STATIC_UINT_ATTR(ldlm_timeout, &ldlm_timeout);
128 LUSTRE_STATIC_UINT_ATTR(bulk_timeout, &bulk_timeout);
129 #endif
130
131 static ssize_t memused_show(struct kobject *kobj, struct attribute *attr,
132                             char *buf)
133 {
134         return sprintf(buf, "%llu\n", obd_memory_sum());
135 }
136 LUSTRE_RO_ATTR(memused);
137
138 static ssize_t memused_max_show(struct kobject *kobj, struct attribute *attr,
139                                 char *buf)
140 {
141         return sprintf(buf, "%llu\n", obd_memory_max());
142 }
143 LUSTRE_RO_ATTR(memused_max);
144
145 static ssize_t max_dirty_mb_show(struct kobject *kobj, struct attribute *attr,
146                                  char *buf)
147 {
148         return sprintf(buf, "%lu\n",
149                        obd_max_dirty_pages / (1 << (20 - PAGE_SHIFT)));
150 }
151
152 static ssize_t max_dirty_mb_store(struct kobject *kobj, struct attribute *attr,
153                                   const char *buffer, size_t count)
154 {
155         unsigned long val;
156         int rc;
157
158         rc = kstrtoul(buffer, 10, &val);
159         if (rc)
160                 return rc;
161
162         val *= 1 << (20 - PAGE_SHIFT); /* convert to pages */
163
164         if (val > ((cfs_totalram_pages() / 10) * 9)) {
165                 /* Somebody wants to assign too much memory to dirty pages */
166                 return -EINVAL;
167         }
168
169         if (val < 4 << (20 - PAGE_SHIFT)) {
170                 /* Less than 4 Mb for dirty cache is also bad */
171                 return -EINVAL;
172         }
173
174         obd_max_dirty_pages = val;
175
176         return count;
177 }
178 LUSTRE_RW_ATTR(max_dirty_mb);
179
180 #ifdef HAVE_SERVER_SUPPORT
181 static ssize_t no_transno_store(struct kobject *kobj,
182                                 struct attribute *attr,
183                                 const char *buffer, size_t count)
184 {
185         struct obd_device *obd;
186         unsigned int idx;
187         int rc;
188
189         rc = kstrtouint(buffer, 10, &idx);
190         if (rc)
191                 return rc;
192
193         obd = class_num2obd(idx);
194         if (!obd || !obd->obd_attached) {
195                 if (obd)
196                         CERROR("%s: not attached\n", obd->obd_name);
197                 return -ENODEV;
198         }
199
200         spin_lock(&obd->obd_dev_lock);
201         obd->obd_no_transno = 1;
202         spin_unlock(&obd->obd_dev_lock);
203         return count;
204 }
205 LUSTRE_WO_ATTR(no_transno);
206 #endif /* HAVE_SERVER_SUPPORT */
207
208 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
209                             char *buf)
210 {
211         return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
212 }
213
214 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
215                            char *buf)
216 {
217 #ifdef CONFIG_LUSTRE_FS_PINGER
218         const char *state = "on";
219 #else
220         const char *state = "off";
221 #endif
222         return sprintf(buf, "%s\n", state);
223 }
224
225 /**
226  * Check all obd devices health
227  *
228  * \param kobj
229  * \param buf [in]
230  *
231  * \retval number of characters printed if healthy
232  */
233 static ssize_t
234 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
235 {
236         bool healthy = true;
237         size_t len = 0;
238         int i;
239
240         if (libcfs_catastrophe)
241                 return sprintf(buf, "LBUG\n");
242
243         read_lock(&obd_dev_lock);
244         for (i = 0; i < class_devno_max(); i++) {
245                 struct obd_device *obd;
246
247                 obd = class_num2obd(i);
248                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
249                         continue;
250
251                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
252                 if (obd->obd_stopping)
253                         continue;
254
255                 if (obd->obd_read_only)
256                         continue;
257
258                 class_incref(obd, __func__, current);
259                 read_unlock(&obd_dev_lock);
260
261                 if (obd_health_check(NULL, obd))
262                         healthy = false;
263
264                 class_decref(obd, __func__, current);
265                 read_lock(&obd_dev_lock);
266
267                 if (!healthy)
268                         break;
269         }
270         read_unlock(&obd_dev_lock);
271
272         if (healthy)
273                 len = sprintf(buf, "healthy\n");
274         else
275                 len = sprintf(buf, "NOT HEALTHY\n");
276
277         return len;
278 }
279
280 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
281                               char *buf)
282 {
283         int rc = 0;
284
285         if (strlen(obd_jobid_var))
286                 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
287         return rc;
288 }
289
290 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
291                                const char *buffer, size_t count)
292 {
293         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
294                 return -EINVAL;
295
296         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
297
298         memcpy(obd_jobid_var, buffer, count);
299
300         /* Trim the trailing '\n' if any */
301         if (obd_jobid_var[count - 1] == '\n')
302                 obd_jobid_var[count - 1] = 0;
303
304         return count;
305 }
306
307 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
308                                char *buf)
309 {
310         int rc = 0;
311
312         if (strlen(obd_jobid_name))
313                 rc = scnprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
314         return rc;
315 }
316
317 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
318                                 const char *buffer, size_t count)
319 {
320         if (!count || count > LUSTRE_JOBID_SIZE)
321                 return -EINVAL;
322
323         if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
324             !strchr(buffer, '%')) {
325                 lustre_jobid_clear(buffer);
326                 return count;
327         }
328
329         /* clear previous value */
330         memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
331
332         memcpy(obd_jobid_name, buffer, count);
333
334         /* Trim the trailing '\n' if any */
335         if (obd_jobid_name[count - 1] == '\n') {
336                 /* Don't echo just a newline */
337                 if (count == 1)
338                         return -EINVAL;
339                 obd_jobid_name[count - 1] = 0;
340         }
341
342         return count;
343 }
344
345 static ssize_t jobid_this_session_show(struct kobject *kobj,
346                                        struct attribute *attr,
347                                        char *buf)
348 {
349         char *jid;
350         int ret = -ENOENT;
351
352         rcu_read_lock();
353         jid = jobid_current();
354         if (jid)
355                 ret = scnprintf(buf, PAGE_SIZE, "%s\n", jid);
356         rcu_read_unlock();
357         return ret;
358 }
359
360 static ssize_t jobid_this_session_store(struct kobject *kobj,
361                                         struct attribute *attr,
362                                         const char *buffer,
363                                         size_t count)
364 {
365         char *jobid;
366         int len;
367         int ret;
368
369         if (!count || count > LUSTRE_JOBID_SIZE)
370                 return -EINVAL;
371
372         jobid = kstrndup(buffer, count, GFP_KERNEL);
373         if (!jobid)
374                 return -ENOMEM;
375         len = strcspn(jobid, "\n ");
376         jobid[len] = '\0';
377         ret = jobid_set_current(jobid);
378         kfree(jobid);
379
380         return ret ?: count;
381 }
382
383 /* Root for /sys/kernel/debug/lustre */
384 struct dentry *debugfs_lustre_root;
385 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
386
387 #ifdef CONFIG_PROC_FS
388 /* Root for /proc/fs/lustre */
389 struct proc_dir_entry *proc_lustre_root;
390 EXPORT_SYMBOL(proc_lustre_root);
391 #else
392 #define lprocfs_base NULL
393 #endif /* CONFIG_PROC_FS */
394
395 LUSTRE_RO_ATTR(version);
396 LUSTRE_RO_ATTR(pinger);
397 LUSTRE_RO_ATTR(health_check);
398 LUSTRE_RW_ATTR(jobid_var);
399 LUSTRE_RW_ATTR(jobid_name);
400 LUSTRE_RW_ATTR(jobid_this_session);
401
402 static struct attribute *lustre_attrs[] = {
403         &lustre_attr_version.attr,
404         &lustre_attr_pinger.attr,
405         &lustre_attr_health_check.attr,
406         &lustre_attr_jobid_name.attr,
407         &lustre_attr_jobid_var.attr,
408         &lustre_attr_jobid_this_session.attr,
409         &lustre_sattr_timeout.u.attr,
410         &lustre_attr_max_dirty_mb.attr,
411         &lustre_sattr_debug_peer_on_timeout.u.attr,
412         &lustre_sattr_dump_on_timeout.u.attr,
413         &lustre_sattr_dump_on_eviction.u.attr,
414         &lustre_sattr_at_min.u.attr,
415         &lustre_sattr_at_max.u.attr,
416         &lustre_sattr_at_extra.u.attr,
417         &lustre_sattr_at_early_margin.u.attr,
418         &lustre_sattr_at_history.u.attr,
419         &lustre_attr_memused_max.attr,
420         &lustre_attr_memused.attr,
421 #ifdef HAVE_SERVER_SUPPORT
422         &lustre_sattr_ldlm_timeout.u.attr,
423         &lustre_sattr_bulk_timeout.u.attr,
424         &lustre_attr_no_transno.attr,
425 #endif
426         &lustre_sattr_lbug_on_eviction.u.attr,
427         NULL,
428 };
429
430 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
431 {
432         if (*pos >= class_devno_max())
433                 return NULL;
434
435         return pos;
436 }
437
438 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
439 {
440 }
441
442 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
443 {
444         ++*pos;
445         if (*pos >= class_devno_max())
446                 return NULL;
447
448         return pos;
449 }
450
451 static int obd_device_list_seq_show(struct seq_file *p, void *v)
452 {
453         loff_t index = *(loff_t *)v;
454         struct obd_device *obd = class_num2obd((int)index);
455         char *status;
456
457         if (obd == NULL)
458                 return 0;
459
460         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
461         if (obd->obd_stopping)
462                 status = "ST";
463         else if (obd->obd_inactive)
464                 status = "IN";
465         else if (obd->obd_set_up)
466                 status = "UP";
467         else if (obd->obd_attached)
468                 status = "AT";
469         else
470                 status = "--";
471
472         seq_printf(p, "%3d %s %s %s %s %d\n",
473                    (int)index, status, obd->obd_type->typ_name,
474                    obd->obd_name, obd->obd_uuid.uuid,
475                    atomic_read(&obd->obd_refcount));
476         return 0;
477 }
478
479 static const struct seq_operations obd_device_list_sops = {
480         .start = obd_device_list_seq_start,
481         .stop = obd_device_list_seq_stop,
482         .next = obd_device_list_seq_next,
483         .show = obd_device_list_seq_show,
484 };
485
486 static int obd_device_list_open(struct inode *inode, struct file *file)
487 {
488         struct seq_file *seq;
489         int rc = seq_open(file, &obd_device_list_sops);
490
491         if (rc)
492                 return rc;
493
494         seq = file->private_data;
495         seq->private = inode->i_private;
496         return 0;
497 }
498
499 static const struct file_operations obd_device_list_fops = {
500         .owner   = THIS_MODULE,
501         .open    = obd_device_list_open,
502         .read    = seq_read,
503         .llseek  = seq_lseek,
504         .release = seq_release,
505 };
506
507 /* checksum_speed */
508 static void *checksum_speed_start(struct seq_file *p, loff_t *pos)
509 {
510         return pos;
511 }
512
513 static void checksum_speed_stop(struct seq_file *p, void *v)
514 {
515 }
516
517 static void *checksum_speed_next(struct seq_file *p, void *v, loff_t *pos)
518 {
519         ++(*pos);
520         if (*pos >= CFS_HASH_ALG_SPEED_MAX - 1)
521                 return NULL;
522
523         return pos;
524 }
525
526 static int checksum_speed_show(struct seq_file *p, void *v)
527 {
528         loff_t index = *(loff_t *)v;
529
530         if (!index || index > CFS_HASH_ALG_SPEED_MAX - 1)
531                 return 0;
532
533         seq_printf(p, "%s: %d\n", cfs_crypto_hash_name(index),
534                    cfs_crypto_hash_speeds[index]);
535
536         return 0;
537 }
538
539 static const struct seq_operations checksum_speed_sops = {
540         .start = checksum_speed_start,
541         .stop = checksum_speed_stop,
542         .next = checksum_speed_next,
543         .show = checksum_speed_show,
544 };
545
546 static int checksum_speed_open(struct inode *inode, struct file *file)
547 {
548         int rc = seq_open(file, &checksum_speed_sops);
549
550         if (rc)
551                 return rc;
552
553         return 0;
554 }
555
556 static const struct file_operations checksum_speed_fops = {
557         .owner   = THIS_MODULE,
558         .open    = checksum_speed_open,
559         .read    = seq_read,
560         .llseek  = seq_lseek,
561         .release = seq_release,
562 };
563
564 static int
565 health_check_seq_show(struct seq_file *m, void *unused)
566 {
567         int i;
568
569         read_lock(&obd_dev_lock);
570         for (i = 0; i < class_devno_max(); i++) {
571                 struct obd_device *obd;
572
573                 obd = class_num2obd(i);
574                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
575                         continue;
576
577                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
578                 if (obd->obd_stopping)
579                         continue;
580
581                 class_incref(obd, __func__, current);
582                 read_unlock(&obd_dev_lock);
583
584                 if (obd_health_check(NULL, obd)) {
585                         seq_printf(m, "device %s reported unhealthy\n",
586                                    obd->obd_name);
587                 }
588                 class_decref(obd, __func__, current);
589                 read_lock(&obd_dev_lock);
590         }
591         read_unlock(&obd_dev_lock);
592
593         return 0;
594 }
595
596 LDEBUGFS_SEQ_FOPS_RO(health_check);
597
598 struct kset *lustre_kset;
599 EXPORT_SYMBOL_GPL(lustre_kset);
600
601 static struct attribute_group lustre_attr_group = {
602         .attrs = lustre_attrs,
603 };
604
605 ssize_t class_set_global(const char *param)
606 {
607         const char *value = strchr(param, '=') + 1;
608         size_t off = value - param - 1;
609         ssize_t count = -ENOENT;
610         int i;
611
612         for (i = 0; lustre_attrs[i]; i++) {
613                 if (!strncmp(lustre_attrs[i]->name, param, off)) {
614                         count = lustre_attr_store(&lustre_kset->kobj,
615                                                   lustre_attrs[i], value,
616                                                   strlen(value));
617                         break;
618                 }
619         }
620         return count;
621 }
622
623 int class_procfs_init(void)
624 {
625         struct proc_dir_entry *entry;
626         struct dentry *file;
627         int rc = -ENOMEM;
628
629         ENTRY;
630
631         lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
632         if (!lustre_kset)
633                 goto out;
634
635         /* Create the files associated with this kobject */
636         rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
637         if (rc) {
638                 kset_unregister(lustre_kset);
639                 goto out;
640         }
641
642         rc = jobid_cache_init();
643         if (rc) {
644                 kset_unregister(lustre_kset);
645                 goto out;
646         }
647
648         debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
649
650         file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
651                                    &obd_device_list_fops);
652
653         file = debugfs_create_file("health_check", 0444, debugfs_lustre_root,
654                                    NULL, &health_check_fops);
655
656         file = debugfs_create_file("checksum_speed", 0444, debugfs_lustre_root,
657                                    NULL, &checksum_speed_fops);
658
659         entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
660         if (IS_ERR(entry)) {
661                 rc = PTR_ERR(entry);
662                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
663                 debugfs_remove_recursive(debugfs_lustre_root);
664                 kset_unregister(lustre_kset);
665                 goto out;
666         }
667
668         proc_lustre_root = entry;
669 out:
670         RETURN(rc);
671 }
672
673 int class_procfs_clean(void)
674 {
675         ENTRY;
676
677         debugfs_remove_recursive(debugfs_lustre_root);
678
679         debugfs_lustre_root = NULL;
680         jobid_cache_fini();
681
682         if (proc_lustre_root)
683                 lprocfs_remove(&proc_lustre_root);
684
685         sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
686
687         kset_unregister(lustre_kset);
688
689         RETURN(0);
690 }