Whamcloud - gitweb
LU-10922 osd-zfs: return -ENOENT if object destoryed
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/obd_sysfs.c
33  *
34  * Object Devices Class Driver
35  * These are the only exported functions, they provide some generic
36  * infrastructure for managing object devices
37  */
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/lp.h>
46 #include <linux/slab.h>
47 #include <linux/ioport.h>
48 #include <linux/fcntl.h>
49 #include <linux/delay.h>
50 #include <linux/skbuff.h>
51 #include <linux/proc_fs.h>
52 #include <linux/fs.h>
53 #include <linux/poll.h>
54 #include <linux/init.h>
55 #include <linux/list.h>
56 #include <linux/highmem.h>
57 #include <asm/io.h>
58 #include <asm/ioctls.h>
59 #include <asm/poll.h>
60 #include <asm/uaccess.h>
61 #include <linux/miscdevice.h>
62 #include <linux/seq_file.h>
63 #include <linux/kobject.h>
64
65 #include <libcfs/libcfs.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
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 > ((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 static ssize_t version_show(struct kobject *kobj, struct attribute *attr,
180                             char *buf)
181 {
182         return sprintf(buf, "%s\n", LUSTRE_VERSION_STRING);
183 }
184
185 static ssize_t pinger_show(struct kobject *kobj, struct attribute *attr,
186                            char *buf)
187 {
188 #ifdef ENABLE_PINGER
189         const char *state = "on";
190 #else
191         const char *state = "off";
192 #endif
193         return sprintf(buf, "%s\n", state);
194 }
195
196 /**
197  * Check all obd devices health
198  *
199  * \param kobj
200  * \param buf [in]
201  *
202  * \retval number of characters printed if healthy
203  */
204 static ssize_t
205 health_check_show(struct kobject *kobj, struct attribute *attr, char *buf)
206 {
207         bool healthy = true;
208         size_t len = 0;
209         int i;
210
211         if (libcfs_catastrophe) {
212                 len = sprintf(buf, "LBUG\n");
213                 healthy = false;
214         }
215
216         read_lock(&obd_dev_lock);
217         for (i = 0; i < class_devno_max(); i++) {
218                 struct obd_device *obd;
219
220                 obd = class_num2obd(i);
221                 if (obd == NULL || !obd->obd_attached || !obd->obd_set_up)
222                         continue;
223
224                 LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
225                 if (obd->obd_stopping)
226                         continue;
227
228                 class_incref(obd, __FUNCTION__, current);
229                 read_unlock(&obd_dev_lock);
230
231                 if (obd_health_check(NULL, obd)) {
232                         len = sprintf(buf, "device %s reported unhealthy\n",
233                                       obd->obd_name);
234                         healthy = false;
235                 }
236                 class_decref(obd, __FUNCTION__, current);
237                 read_lock(&obd_dev_lock);
238         }
239         read_unlock(&obd_dev_lock);
240
241         if (healthy)
242                 len = sprintf(buf, "healthy\n");
243         else
244                 len = sprintf(buf, "NOT HEALTHY\n");
245
246         return len;
247 }
248
249 static ssize_t jobid_var_show(struct kobject *kobj, struct attribute *attr,
250                               char *buf)
251 {
252         int rc = 0;
253
254         if (strlen(obd_jobid_var))
255                 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_var);
256         return rc;
257 }
258
259 static ssize_t jobid_var_store(struct kobject *kobj, struct attribute *attr,
260                                const char *buffer, size_t count)
261 {
262         if (!count || count > JOBSTATS_JOBID_VAR_MAX_LEN)
263                 return -EINVAL;
264
265         memset(obd_jobid_var, 0, JOBSTATS_JOBID_VAR_MAX_LEN + 1);
266
267         memcpy(obd_jobid_var, buffer, count);
268
269         /* Trim the trailing '\n' if any */
270         if (obd_jobid_var[count - 1] == '\n')
271                 obd_jobid_var[count - 1] = 0;
272
273         return count;
274 }
275
276 static ssize_t jobid_name_show(struct kobject *kobj, struct attribute *attr,
277                                char *buf)
278 {
279         int rc = 0;
280
281         if (strlen(obd_jobid_name))
282                 rc = snprintf(buf, PAGE_SIZE, "%s\n", obd_jobid_name);
283         return rc;
284 }
285
286 static ssize_t jobid_name_store(struct kobject *kobj, struct attribute *attr,
287                                 const char *buffer, size_t count)
288 {
289         if (!count || count > LUSTRE_JOBID_SIZE)
290                 return -EINVAL;
291
292         if (strcmp(obd_jobid_var, JOBSTATS_NODELOCAL) != 0 &&
293             !strchr(buffer, '%')) {
294                 lustre_jobid_clear(buffer);
295                 return count;
296         }
297
298         /* clear previous value */
299         memset(obd_jobid_name, 0, LUSTRE_JOBID_SIZE);
300
301         memcpy(obd_jobid_name, buffer, count);
302
303         /* Trim the trailing '\n' if any */
304         if (obd_jobid_name[count - 1] == '\n') {
305                 /* Don't echo just a newline */
306                 if (count == 1)
307                         return -EINVAL;
308                 obd_jobid_name[count - 1] = 0;
309         }
310
311         return count;
312 }
313
314 /* Root for /sys/kernel/debug/lustre */
315 struct dentry *debugfs_lustre_root;
316 EXPORT_SYMBOL_GPL(debugfs_lustre_root);
317
318 #ifdef CONFIG_PROC_FS
319 /* Root for /proc/fs/lustre */
320 struct proc_dir_entry *proc_lustre_root = NULL;
321 EXPORT_SYMBOL(proc_lustre_root);
322 #else
323 #define lprocfs_base NULL
324 #endif /* CONFIG_PROC_FS */
325
326 LUSTRE_RO_ATTR(version);
327 LUSTRE_RO_ATTR(pinger);
328 LUSTRE_RO_ATTR(health_check);
329 LUSTRE_RW_ATTR(jobid_var);
330 LUSTRE_RW_ATTR(jobid_name);
331
332 static struct attribute *lustre_attrs[] = {
333         &lustre_attr_version.attr,
334         &lustre_attr_pinger.attr,
335         &lustre_attr_health_check.attr,
336         &lustre_attr_jobid_name.attr,
337         &lustre_attr_jobid_var.attr,
338         &lustre_sattr_timeout.u.attr,
339         &lustre_attr_max_dirty_mb.attr,
340         &lustre_sattr_debug_peer_on_timeout.u.attr,
341         &lustre_sattr_dump_on_timeout.u.attr,
342         &lustre_sattr_dump_on_eviction.u.attr,
343         &lustre_sattr_at_min.u.attr,
344         &lustre_sattr_at_max.u.attr,
345         &lustre_sattr_at_extra.u.attr,
346         &lustre_sattr_at_early_margin.u.attr,
347         &lustre_sattr_at_history.u.attr,
348         &lustre_attr_memused_max.attr,
349         &lustre_attr_memused.attr,
350 #ifdef HAVE_SERVER_SUPPORT
351         &lustre_sattr_ldlm_timeout.u.attr,
352         &lustre_sattr_bulk_timeout.u.attr,
353 #endif
354         NULL,
355 };
356
357 static void *obd_device_list_seq_start(struct seq_file *p, loff_t *pos)
358 {
359         if (*pos >= class_devno_max())
360                 return NULL;
361
362         return pos;
363 }
364
365 static void obd_device_list_seq_stop(struct seq_file *p, void *v)
366 {
367 }
368
369 static void *obd_device_list_seq_next(struct seq_file *p, void *v, loff_t *pos)
370 {
371         ++*pos;
372         if (*pos >= class_devno_max())
373                 return NULL;
374
375         return pos;
376 }
377
378 static int obd_device_list_seq_show(struct seq_file *p, void *v)
379 {
380         loff_t index = *(loff_t *)v;
381         struct obd_device *obd = class_num2obd((int)index);
382         char *status;
383
384         if (obd == NULL)
385                 return 0;
386
387         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
388         if (obd->obd_stopping)
389                 status = "ST";
390         else if (obd->obd_inactive)
391                 status = "IN";
392         else if (obd->obd_set_up)
393                 status = "UP";
394         else if (obd->obd_attached)
395                 status = "AT";
396         else
397                 status = "--";
398
399         seq_printf(p, "%3d %s %s %s %s %d\n",
400                    (int)index, status, obd->obd_type->typ_name,
401                    obd->obd_name, obd->obd_uuid.uuid,
402                    atomic_read(&obd->obd_refcount));
403         return 0;
404 }
405
406 static const struct seq_operations obd_device_list_sops = {
407         .start = obd_device_list_seq_start,
408         .stop = obd_device_list_seq_stop,
409         .next = obd_device_list_seq_next,
410         .show = obd_device_list_seq_show,
411 };
412
413 static int obd_device_list_open(struct inode *inode, struct file *file)
414 {
415         struct seq_file *seq;
416         int rc = seq_open(file, &obd_device_list_sops);
417
418         if (rc)
419                 return rc;
420
421         seq = file->private_data;
422         seq->private = inode->i_private;
423         return 0;
424 }
425
426 static const struct file_operations obd_device_list_fops = {
427         .owner   = THIS_MODULE,
428         .open    = obd_device_list_open,
429         .read    = seq_read,
430         .llseek  = seq_lseek,
431         .release = seq_release,
432 };
433
434 struct kset *lustre_kset;
435 EXPORT_SYMBOL_GPL(lustre_kset);
436
437 static struct attribute_group lustre_attr_group = {
438         .attrs = lustre_attrs,
439 };
440
441 ssize_t class_set_global(const char *param)
442 {
443         const char *value = strchr(param, '=') + 1;
444         size_t off = value - param - 1;
445         ssize_t count = -ENOENT;
446         int i;
447
448         for (i = 0; lustre_attrs[i]; i++) {
449                 if (!strncmp(lustre_attrs[i]->name, param, off)) {
450                         count = lustre_attr_store(&lustre_kset->kobj,
451                                                   lustre_attrs[i], value,
452                                                   strlen(value));
453                         break;
454                 }
455         }
456         return count;
457 }
458
459 int class_procfs_init(void)
460 {
461         struct proc_dir_entry *entry;
462         struct dentry *file;
463         int rc = -ENOMEM;
464         ENTRY;
465
466         lustre_kset = kset_create_and_add("lustre", NULL, fs_kobj);
467         if (!lustre_kset)
468                 goto out;
469
470         /* Create the files associated with this kobject */
471         rc = sysfs_create_group(&lustre_kset->kobj, &lustre_attr_group);
472         if (rc) {
473                 kset_unregister(lustre_kset);
474                 goto out;
475         }
476
477         rc = jobid_cache_init();
478         if (rc) {
479                 kset_unregister(lustre_kset);
480                 goto out;
481         }
482
483         debugfs_lustre_root = debugfs_create_dir("lustre", NULL);
484         if (IS_ERR_OR_NULL(debugfs_lustre_root)) {
485                 rc = debugfs_lustre_root ? PTR_ERR(debugfs_lustre_root)
486                                          : -ENOMEM;
487                 debugfs_lustre_root = NULL;
488                 kset_unregister(lustre_kset);
489                 goto out;
490         }
491
492         file = debugfs_create_file("devices", 0444, debugfs_lustre_root, NULL,
493                                    &obd_device_list_fops);
494         if (IS_ERR_OR_NULL(file)) {
495                 rc = file ? PTR_ERR(file) : -ENOMEM;
496                 debugfs_remove(debugfs_lustre_root);
497                 kset_unregister(lustre_kset);
498                 goto out;
499         }
500
501         entry = lprocfs_register("fs/lustre", NULL, NULL, NULL);
502         if (IS_ERR(entry)) {
503                 rc = PTR_ERR(entry);
504                 CERROR("cannot create '/proc/fs/lustre': rc = %d\n", rc);
505                 debugfs_remove_recursive(debugfs_lustre_root);
506                 kset_unregister(lustre_kset);
507                 goto out;
508         }
509
510         proc_lustre_root = entry;
511 out:
512         RETURN(rc);
513 }
514
515 int class_procfs_clean(void)
516 {
517         ENTRY;
518
519         debugfs_remove_recursive(debugfs_lustre_root);
520
521         debugfs_lustre_root = NULL;
522         jobid_cache_fini();
523
524         if (proc_lustre_root)
525                 lprocfs_remove(&proc_lustre_root);
526
527         sysfs_remove_group(&lustre_kset->kobj, &lustre_attr_group);
528
529         kset_unregister(lustre_kset);
530
531         RETURN(0);
532 }