Whamcloud - gitweb
LU-19098 hsm: don't print progname twice with lhsmtool
[fs/lustre-release.git] / lustre / osd-zfs / osd_lproc.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2012, 2017, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  *
13  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
14  * Author: Mike Pershin <tappro@whamcloud.com>
15  */
16
17 #define DEBUG_SUBSYSTEM S_OSD
18
19 #include <obd.h>
20 #include <obd_class.h>
21 #include <lprocfs_status.h>
22 #include <lustre_scrub.h>
23
24 #include "osd_internal.h"
25
26 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
27 static int symlink_brw_stats;
28 module_param(symlink_brw_stats, int, 0644);
29 MODULE_PARM_DESC(symlink_brw_stats, "create /proc brw_stats symlink");
30
31 static void osd_symlink_brw_stats(struct osd_device *osd)
32 {
33         size_t len_root;
34         size_t len_path;
35         char *root;
36         char *s;
37         char *p;
38         char *path;
39
40         if (!symlink_brw_stats)
41                 return;
42
43         OBD_ALLOC(path, PATH_MAX);
44         if (path == NULL)
45                 return;
46
47         p = dentry_path_raw(osd->od_dt_dev.dd_debugfs_entry, path, PATH_MAX);
48         if (IS_ERR(p))
49                 goto out;
50
51         root = osd->od_dt_dev.dd_debugfs_entry->d_sb->s_fs_info;
52         len_root = strlen(root);
53         len_path = strlen(p);
54         if (len_root > (p - path) || len_root + len_path + 16 > PATH_MAX)
55                 goto out;
56
57         strscpy(path, root, len_root);
58         if (p > path + len_root) {
59                 s = path + len_root;
60                 while ((*s++ = *p++) != '\0')
61                         ;
62         }
63
64         *(path + len_root + len_path) = '\0';
65         strcat(path, "/brw_stats");
66         lprocfs_add_symlink("brw_stats", osd->od_proc_entry,
67                             "/sys/kernel/debug/%s", path);
68
69 out:
70         OBD_FREE(path, PATH_MAX);
71 }
72 #endif
73
74 static int osd_stats_init(struct osd_device *osd)
75 {
76         char param[MAX_OBD_NAME * 4];
77         int result = -ENOMEM;
78
79         ENTRY;
80         scnprintf(param, sizeof(param), "osd-zfs.%s.stats", osd_name(osd));
81         osd->od_stats = ldebugfs_stats_alloc(LPROC_OSD_LAST, param,
82                                              osd->od_dt_dev.dd_debugfs_entry,
83                                              0);
84         if (osd->od_stats) {
85                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
86                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV |
87                                 LPROCFS_TYPE_USECS, "get_page");
88                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
89                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_REQS,
90                                 "get_page_failures");
91                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
92                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_PAGES,
93                                 "cache_access");
94                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
95                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_PAGES,
96                                 "cache_hit");
97                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
98                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_PAGES,
99                                 "cache_miss");
100                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_COPY_IO,
101                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_PAGES,
102                                 "copy");
103                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_ZEROCOPY_IO,
104                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_PAGES,
105                                 "zerocopy");
106                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_TAIL_IO,
107                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_PAGES,
108                                 "tail");
109 #ifdef OSD_THANDLE_STATS
110                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
111                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_USECS,
112                                 "thandle_starting");
113                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
114                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_USECS,
115                                 "thandle_open");
116                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
117                                 LPROCFS_CNTR_AVGMINMAX | LPROCFS_TYPE_USECS,
118                                 "thandle_closing");
119 #endif
120                 result = 0;
121         }
122
123         ldebugfs_register_brw_stats(osd->od_dt_dev.dd_debugfs_entry,
124                                     &osd->od_brw_stats);
125
126 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
127         osd_symlink_brw_stats(osd);
128 #endif
129
130         /* These fields are not supported for ZFS */
131         osd->od_brw_stats.bs_props[BRW_R_DISCONT_BLOCKS / 2].bsp_name = NULL;
132         osd->od_brw_stats.bs_props[BRW_R_DIO_FRAGS / 2].bsp_name = NULL;
133
134         RETURN(result);
135 }
136
137 static int zfs_osd_oi_scrub_seq_show(struct seq_file *m, void *data)
138 {
139         struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
140
141         LASSERT(dev != NULL);
142         if (!dev->od_os)
143                 return -EINPROGRESS;
144
145         scrub_dump(m, &dev->od_scrub);
146         return 0;
147 }
148 LDEBUGFS_SEQ_FOPS_RO(zfs_osd_oi_scrub);
149
150 static ssize_t auto_scrub_show(struct kobject *kobj, struct attribute *attr,
151                               char *buf)
152 {
153         struct dt_device *dt = container_of(kobj, struct dt_device,
154                                             dd_kobj);
155         struct osd_device *dev = osd_dt_dev(dt);
156
157         LASSERT(dev);
158         if (!dev->od_os)
159                 return -EINPROGRESS;
160
161         return scnprintf(buf, PAGE_SIZE, "%lld\n",
162                          dev->od_scrub.os_auto_scrub_interval);
163 }
164
165 static ssize_t auto_scrub_store(struct kobject *kobj, struct attribute *attr,
166                                 const char *buffer, size_t count)
167 {
168         struct dt_device *dt = container_of(kobj, struct dt_device,
169                                             dd_kobj);
170         struct osd_device *dev = osd_dt_dev(dt);
171         s64 val;
172         int rc;
173
174         LASSERT(dev);
175         if (!dev->od_os)
176                 return -EINPROGRESS;
177
178         rc = kstrtoull(buffer, 0, &val);
179         if (rc)
180                 return rc;
181
182         dev->od_scrub.os_auto_scrub_interval = val;
183         return count;
184 }
185 LUSTRE_RW_ATTR(auto_scrub);
186
187 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
188                           char *buf)
189 {
190         return sprintf(buf, "zfs\n");
191 }
192 LUSTRE_RO_ATTR(fstype);
193
194 static ssize_t mntdev_show(struct kobject *kobj, struct attribute *attr,
195                            char *buf)
196 {
197         struct dt_device *dt = container_of(kobj, struct dt_device,
198                                             dd_kobj);
199         struct osd_device *osd = osd_dt_dev(dt);
200
201         LASSERT(osd);
202
203         return sprintf(buf, "%s\n", osd->od_mntdev);
204 }
205 LUSTRE_RO_ATTR(mntdev);
206
207 static ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
208                                 const char *buffer, size_t count)
209 {
210         struct dt_device *dt = container_of(kobj, struct dt_device,
211                                             dd_kobj);
212         struct lu_env env;
213         int rc;
214
215         rc = lu_env_init(&env, LCT_LOCAL);
216         if (rc)
217                 return rc;
218
219         rc = dt_sync(&env, dt);
220         lu_env_fini(&env);
221
222         return rc == 0 ? count : rc;
223 }
224 LUSTRE_WO_ATTR(force_sync);
225
226 static ssize_t sync_on_lseek_show(struct kobject *kobj, struct attribute *attr,
227                                   char *buf)
228 {
229         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
230         struct osd_device *osd = osd_dt_dev(dt);
231
232         if (!osd->od_os)
233                 return -EINPROGRESS;
234
235         return sprintf(buf, "%u\n", osd->od_sync_on_lseek);
236 }
237
238 static ssize_t sync_on_lseek_store(struct kobject *kobj, struct attribute *attr,
239                                    const char *buffer, size_t count)
240 {
241         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
242         struct osd_device *osd = osd_dt_dev(dt);
243         bool val;
244         int rc;
245
246         if (!osd->od_os)
247                 return -EINPROGRESS;
248
249         rc = kstrtobool(buffer, &val);
250         if (rc)
251                 return rc;
252
253         osd->od_sync_on_lseek = !!val;
254
255         return count;
256 }
257 LUSTRE_RW_ATTR(sync_on_lseek);
258
259 static ssize_t nonrotational_show(struct kobject *kobj, struct attribute *attr,
260                                   char *buf)
261 {
262         struct dt_device *dt = container_of(kobj, struct dt_device,
263                                             dd_kobj);
264         struct osd_device *osd = osd_dt_dev(dt);
265
266         LASSERT(osd);
267         if (!osd->od_os)
268                 return -EINPROGRESS;
269
270         return sprintf(buf, "%u\n", osd->od_nonrotational);
271 }
272
273 static ssize_t nonrotational_store(struct kobject *kobj,
274                                    struct attribute *attr, const char *buffer,
275                                    size_t count)
276 {
277         struct dt_device *dt = container_of(kobj, struct dt_device,
278                                             dd_kobj);
279         struct osd_device *osd = osd_dt_dev(dt);
280         bool val;
281         int rc;
282
283         LASSERT(osd);
284         if (!osd->od_os)
285                 return -EINPROGRESS;
286
287         rc = kstrtobool(buffer, &val);
288         if (rc)
289                 return rc;
290
291         osd->od_nonrotational = val;
292         return count;
293 }
294 LUSTRE_RW_ATTR(nonrotational);
295
296 static ssize_t index_backup_show(struct kobject *kobj, struct attribute *attr,
297                                  char *buf)
298 {
299         struct dt_device *dt = container_of(kobj, struct dt_device,
300                                             dd_kobj);
301         struct osd_device *dev = osd_dt_dev(dt);
302
303         LASSERT(dev);
304         if (!dev->od_os)
305                 return -EINPROGRESS;
306
307         return sprintf(buf, "%d\n", dev->od_index_backup_policy);
308 }
309
310 static ssize_t index_backup_store(struct kobject *kobj, struct attribute *attr,
311                                   const char *buffer, size_t count)
312 {
313         struct dt_device *dt = container_of(kobj, struct dt_device,
314                                             dd_kobj);
315         struct osd_device *dev = osd_dt_dev(dt);
316         int val;
317         int rc;
318
319         LASSERT(dev);
320         if (!dev->od_os)
321                 return -EINPROGRESS;
322
323         rc = kstrtoint(buffer, 0, &val);
324         if (rc)
325                 return rc;
326
327         dev->od_index_backup_policy = val;
328         return count;
329 }
330 LUSTRE_RW_ATTR(index_backup);
331
332 static ssize_t readcache_max_filesize_show(struct kobject *kobj,
333                                            struct attribute *attr,
334                                            char *buf)
335 {
336         struct dt_device *dt = container_of(kobj, struct dt_device,
337                                             dd_kobj);
338         struct osd_device *osd = osd_dt_dev(dt);
339
340         LASSERT(osd != NULL);
341         if (unlikely(osd->od_os == NULL))
342                 return -EINPROGRESS;
343
344         return scnprintf(buf, PAGE_SIZE, "%llu\n",
345                          osd->od_readcache_max_filesize);
346 }
347
348 static ssize_t readcache_max_filesize_store(struct kobject *kobj,
349                                             struct attribute *attr,
350                                             const char *buffer, size_t count)
351 {
352         struct dt_device *dt = container_of(kobj, struct dt_device,
353                                             dd_kobj);
354         struct osd_device *osd = osd_dt_dev(dt);
355         u64 val;
356         int rc;
357
358         LASSERT(osd != NULL);
359         if (unlikely(osd->od_os == NULL))
360                 return -EINPROGRESS;
361
362         rc = sysfs_memparse(buffer, count, &val, "B");
363         if (rc < 0)
364                 return rc;
365
366         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
367                                          OSD_MAX_CACHE_SIZE : val;
368         return count;
369 }
370 LUSTRE_RW_ATTR(readcache_max_filesize);
371
372 static struct attribute *zfs_attrs[] = {
373         &lustre_attr_fstype.attr,
374         &lustre_attr_mntdev.attr,
375         &lustre_attr_force_sync.attr,
376         &lustre_attr_nonrotational.attr,
377         &lustre_attr_index_backup.attr,
378         &lustre_attr_auto_scrub.attr,
379         &lustre_attr_sync_on_lseek.attr,
380         &lustre_attr_readcache_max_filesize.attr,
381         NULL,
382 };
383
384 static struct ldebugfs_vars ldebugfs_osd_obd_vars[] = {
385         { .name =       "oi_scrub",
386           .fops =       &zfs_osd_oi_scrub_fops          },
387         { 0 }
388 };
389
390 KOBJ_ATTRIBUTE_GROUPS(zfs); /* creates zfs_groups from zfs_attrs */
391
392 int osd_procfs_init(struct osd_device *osd, const char *name)
393 {
394         struct obd_type *type;
395         int rc;
396
397         ENTRY;
398
399         /* at the moment there is no linkage between lu_type
400          * and obd_type, so we lookup obd_type this way
401          */
402         type = class_search_type(LUSTRE_OSD_ZFS_NAME);
403
404         LASSERT(type);
405         LASSERT(name);
406
407         /* put reference taken by class_search_type */
408         kobject_put(&type->typ_kobj);
409
410         osd->od_dt_dev.dd_ktype.default_groups = KOBJ_ATTR_GROUPS(zfs);
411         rc = dt_tunables_init(&osd->od_dt_dev, type, name,
412                               ldebugfs_osd_obd_vars);
413         if (rc) {
414                 CERROR("%s: cannot setup sysfs / debugfs entry: %d\n",
415                        name, rc);
416                 GOTO(out, rc);
417         }
418
419         if (osd->od_proc_entry)
420                 RETURN(0);
421
422         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
423                                               NULL, &osd->od_dt_dev);
424         if (IS_ERR(osd->od_proc_entry)) {
425                 rc = PTR_ERR(osd->od_proc_entry);
426                 CERROR("%s: error setting up lprocfs: rc = %d\n", name, rc);
427                 osd->od_proc_entry = NULL;
428                 GOTO(out, rc);
429         }
430
431         rc = osd_stats_init(osd);
432
433         GOTO(out, rc);
434 out:
435         if (rc)
436                 osd_procfs_fini(osd);
437         return rc;
438 }
439
440 void osd_procfs_fini(struct osd_device *osd)
441 {
442         ENTRY;
443
444         lprocfs_fini_brw_stats(&osd->od_brw_stats);
445
446         if (osd->od_stats)
447                 lprocfs_stats_free(&osd->od_stats);
448
449         if (osd->od_proc_entry) {
450                 lprocfs_remove(&osd->od_proc_entry);
451                 osd->od_proc_entry = NULL;
452         }
453
454         dt_tunables_fini(&osd->od_dt_dev);
455 }