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