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