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