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