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