Whamcloud - gitweb
LU-14927 scrub: create shared scrub_needs_check() function.
[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 #ifdef CONFIG_PROC_FS
47
48 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
49                               struct obd_histogram *read,
50                               struct obd_histogram *write, int scale)
51 {
52         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
53         int i;
54
55         seq_printf(seq, "\n%26s read      |     write\n", " ");
56         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
57                    name, units, units);
58
59         read_tot = lprocfs_oh_sum(read);
60         write_tot = lprocfs_oh_sum(write);
61         for (i = 0; i < OBD_HIST_MAX; i++) {
62                 r = read->oh_buckets[i];
63                 w = write->oh_buckets[i];
64                 read_cum += r;
65                 write_cum += w;
66                 if (read_cum == 0 && write_cum == 0)
67                         continue;
68
69                 if (!scale)
70                         seq_printf(seq, "%u", i);
71                 else if (i < 10)
72                         seq_printf(seq, "%u", scale << i);
73                 else if (i < 20)
74                         seq_printf(seq, "%uK", scale << (i-10));
75                 else
76                         seq_printf(seq, "%uM", scale << (i-20));
77
78                 seq_printf(seq, ":\t\t%10lu %3u %3u   | %4lu %3u %3u\n",
79                            r, pct(r, read_tot), pct(read_cum, read_tot),
80                            w, pct(w, write_tot), pct(write_cum, write_tot));
81
82                 if (read_cum == read_tot && write_cum == write_tot)
83                         break;
84         }
85 }
86
87 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
88 {
89         struct timespec64 now;
90
91         /* this sampling races with updates */
92         ktime_get_real_ts64(&now);
93         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
94                    (s64)now.tv_sec, now.tv_nsec);
95
96         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
97                           &brw_stats->hist[BRW_R_PAGES],
98                           &brw_stats->hist[BRW_W_PAGES], 1);
99         display_brw_stats(seq, "discontiguous pages", "rpcs",
100                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
101                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
102 #if 0
103         display_brw_stats(seq, "discontiguous blocks", "rpcs",
104                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
105                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
106
107         display_brw_stats(seq, "disk fragmented I/Os", "ios",
108                           &brw_stats->hist[BRW_R_DIO_FRAGS],
109                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
110 #endif
111         display_brw_stats(seq, "disk I/Os in flight", "ios",
112                           &brw_stats->hist[BRW_R_RPC_HIST],
113                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
114
115         display_brw_stats(seq, "I/O time (1/1000s)", "ios",
116                           &brw_stats->hist[BRW_R_IO_TIME],
117                           &brw_stats->hist[BRW_W_IO_TIME], 1);
118
119         display_brw_stats(seq, "disk I/O size", "ios",
120                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
121                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
122 }
123
124 static int osd_brw_stats_seq_show(struct seq_file *seq, void *v)
125 {
126         struct osd_device *osd = seq->private;
127
128         brw_stats_show(seq, &osd->od_brw_stats);
129
130         return 0;
131 }
132
133 static ssize_t osd_brw_stats_seq_write(struct file *file,
134                                        const char __user *buf,
135                                        size_t len, loff_t *off)
136 {
137         struct seq_file *seq = file->private_data;
138         struct osd_device *osd = seq->private;
139         int i;
140
141         for (i = 0; i < BRW_LAST; i++)
142                 lprocfs_oh_clear(&osd->od_brw_stats.hist[i]);
143
144         return len;
145 }
146
147 LPROC_SEQ_FOPS(osd_brw_stats);
148
149 static int osd_stats_init(struct osd_device *osd)
150 {
151         int result, i;
152         ENTRY;
153
154         for (i = 0; i < BRW_LAST; i++)
155                 spin_lock_init(&osd->od_brw_stats.hist[i].oh_lock);
156
157         osd->od_stats = lprocfs_alloc_stats(LPROC_OSD_LAST, 0);
158         if (osd->od_stats != NULL) {
159                 result = lprocfs_register_stats(osd->od_proc_entry, "stats",
160                                 osd->od_stats);
161                 if (result)
162                         GOTO(out, result);
163
164                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
165                                 LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
166                                 "get_page", "usec");
167                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
168                                 LPROCFS_CNTR_AVGMINMAX,
169                                 "get_page_failures", "num");
170                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
171                                 LPROCFS_CNTR_AVGMINMAX,
172                                 "cache_access", "pages");
173                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
174                                 LPROCFS_CNTR_AVGMINMAX,
175                                 "cache_hit", "pages");
176                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
177                                 LPROCFS_CNTR_AVGMINMAX,
178                                 "cache_miss", "pages");
179                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_COPY_IO,
180                                 LPROCFS_CNTR_AVGMINMAX,
181                                 "copy", "pages");
182                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_ZEROCOPY_IO,
183                                 LPROCFS_CNTR_AVGMINMAX,
184                                 "zerocopy", "pages");
185                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_TAIL_IO,
186                                 LPROCFS_CNTR_AVGMINMAX,
187                                 "tail", "pages");
188 #ifdef OSD_THANDLE_STATS
189                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
190                                 LPROCFS_CNTR_AVGMINMAX,
191                                 "thandle_starting", "usec");
192                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
193                                 LPROCFS_CNTR_AVGMINMAX,
194                                 "thandle_open", "usec");
195                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
196                                 LPROCFS_CNTR_AVGMINMAX,
197                                 "thandle_closing", "usec");
198 #endif
199                 result = lprocfs_seq_create(osd->od_proc_entry, "brw_stats",
200                                             0644, &osd_brw_stats_fops, osd);
201         } else {
202                 result = -ENOMEM;
203         }
204
205 out:
206         RETURN(result);
207 }
208
209 static int zfs_osd_oi_scrub_seq_show(struct seq_file *m, void *data)
210 {
211         struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
212
213         LASSERT(dev != NULL);
214         if (!dev->od_os)
215                 return -EINPROGRESS;
216
217         scrub_dump(m, &dev->od_scrub);
218         return 0;
219 }
220 LDEBUGFS_SEQ_FOPS_RO(zfs_osd_oi_scrub);
221
222 static ssize_t auto_scrub_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 *dev = osd_dt_dev(dt);
228
229         LASSERT(dev);
230         if (!dev->od_os)
231                 return -EINPROGRESS;
232
233         return scnprintf(buf, PAGE_SIZE, "%lld\n",
234                          dev->od_scrub.os_auto_scrub_interval);
235 }
236
237 static ssize_t auto_scrub_store(struct kobject *kobj, struct attribute *attr,
238                                 const char *buffer, size_t count)
239 {
240         struct dt_device *dt = container_of(kobj, struct dt_device,
241                                             dd_kobj);
242         struct osd_device *dev = osd_dt_dev(dt);
243         s64 val;
244         int rc;
245
246         LASSERT(dev);
247         if (!dev->od_os)
248                 return -EINPROGRESS;
249
250         rc = kstrtoull(buffer, 0, &val);
251         if (rc)
252                 return rc;
253
254         dev->od_scrub.os_auto_scrub_interval = val;
255         return count;
256 }
257 LUSTRE_RW_ATTR(auto_scrub);
258
259 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
260                           char *buf)
261 {
262         return sprintf(buf, "zfs\n");
263 }
264 LUSTRE_RO_ATTR(fstype);
265
266 static ssize_t mntdev_show(struct kobject *kobj, struct attribute *attr,
267                            char *buf)
268 {
269         struct dt_device *dt = container_of(kobj, struct dt_device,
270                                             dd_kobj);
271         struct osd_device *osd = osd_dt_dev(dt);
272
273         LASSERT(osd);
274
275         return sprintf(buf, "%s\n", osd->od_mntdev);
276 }
277 LUSTRE_RO_ATTR(mntdev);
278
279 ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
280                          const char *buffer, size_t count)
281 {
282         struct dt_device *dt = container_of(kobj, struct dt_device,
283                                             dd_kobj);
284         struct lu_env env;
285         int rc;
286
287         rc = lu_env_init(&env, LCT_LOCAL);
288         if (rc)
289                 return rc;
290
291         rc = dt_sync(&env, dt);
292         lu_env_fini(&env);
293
294         return rc == 0 ? count : rc;
295 }
296 LUSTRE_WO_ATTR(force_sync);
297
298 static ssize_t sync_on_lseek_show(struct kobject *kobj, struct attribute *attr,
299                                   char *buf)
300 {
301         struct dt_device *dt = container_of(kobj, struct dt_device, dd_kobj);
302         struct osd_device *osd = osd_dt_dev(dt);
303
304         if (!osd->od_os)
305                 return -EINPROGRESS;
306
307         return sprintf(buf, "%u\n", osd->od_sync_on_lseek);
308 }
309
310 ssize_t sync_on_lseek_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, dd_kobj);
314         struct osd_device *osd = osd_dt_dev(dt);
315         bool val;
316         int rc;
317
318         if (!osd->od_os)
319                 return -EINPROGRESS;
320
321         rc = kstrtobool(buffer, &val);
322         if (rc)
323                 return rc;
324
325         osd->od_sync_on_lseek = !!val;
326
327         return count;
328 }
329 LUSTRE_RW_ATTR(sync_on_lseek);
330
331 static ssize_t nonrotational_show(struct kobject *kobj, struct attribute *attr,
332                                   char *buf)
333 {
334         struct dt_device *dt = container_of(kobj, struct dt_device,
335                                             dd_kobj);
336         struct osd_device *osd = osd_dt_dev(dt);
337
338         LASSERT(osd);
339         if (!osd->od_os)
340                 return -EINPROGRESS;
341
342         return sprintf(buf, "%u\n", osd->od_nonrotational);
343 }
344
345 static ssize_t nonrotational_store(struct kobject *kobj,
346                                    struct attribute *attr, const char *buffer,
347                                    size_t count)
348 {
349         struct dt_device *dt = container_of(kobj, struct dt_device,
350                                             dd_kobj);
351         struct osd_device *osd = osd_dt_dev(dt);
352         bool val;
353         int rc;
354
355         LASSERT(osd);
356         if (!osd->od_os)
357                 return -EINPROGRESS;
358
359         rc = kstrtobool(buffer, &val);
360         if (rc)
361                 return rc;
362
363         osd->od_nonrotational = val;
364         return count;
365 }
366 LUSTRE_RW_ATTR(nonrotational);
367
368 static ssize_t index_backup_show(struct kobject *kobj, struct attribute *attr,
369                                  char *buf)
370 {
371         struct dt_device *dt = container_of(kobj, struct dt_device,
372                                             dd_kobj);
373         struct osd_device *dev = osd_dt_dev(dt);
374
375         LASSERT(dev);
376         if (!dev->od_os)
377                 return -EINPROGRESS;
378
379         return sprintf(buf, "%d\n", dev->od_index_backup_policy);
380 }
381
382 ssize_t index_backup_store(struct kobject *kobj, struct attribute *attr,
383                            const char *buffer, size_t count)
384 {
385         struct dt_device *dt = container_of(kobj, struct dt_device,
386                                             dd_kobj);
387         struct osd_device *dev = osd_dt_dev(dt);
388         int val;
389         int rc;
390
391         LASSERT(dev);
392         if (!dev->od_os)
393                 return -EINPROGRESS;
394
395         rc = kstrtoint(buffer, 0, &val);
396         if (rc)
397                 return rc;
398
399         dev->od_index_backup_policy = val;
400         return count;
401 }
402 LUSTRE_RW_ATTR(index_backup);
403
404 static int zfs_osd_readcache_seq_show(struct seq_file *m, void *data)
405 {
406         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
407
408         LASSERT(osd != NULL);
409         if (unlikely(osd->od_os == NULL))
410                 return -EINPROGRESS;
411
412         seq_printf(m, "%llu\n", osd->od_readcache_max_filesize);
413         return 0;
414 }
415
416 static ssize_t
417 zfs_osd_readcache_seq_write(struct file *file, const char __user *buffer,
418                             size_t count, loff_t *off)
419 {
420         struct seq_file *m = file->private_data;
421         struct dt_device *dt = m->private;
422         struct osd_device *osd = osd_dt_dev(dt);
423         char kernbuf[22] = "";
424         u64 val;
425         int rc;
426
427         LASSERT(osd != NULL);
428         if (unlikely(osd->od_os == NULL))
429                 return -EINPROGRESS;
430
431         if (count >= sizeof(kernbuf))
432                 return -EINVAL;
433
434         if (copy_from_user(kernbuf, buffer, count))
435                 return -EFAULT;
436         kernbuf[count] = 0;
437
438         rc = sysfs_memparse(kernbuf, count, &val, "B");
439         if (rc < 0)
440                 return rc;
441
442         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
443                                          OSD_MAX_CACHE_SIZE : val;
444         return count;
445 }
446 LDEBUGFS_SEQ_FOPS(zfs_osd_readcache);
447
448 static struct attribute *zfs_attrs[] = {
449         &lustre_attr_fstype.attr,
450         &lustre_attr_mntdev.attr,
451         &lustre_attr_force_sync.attr,
452         &lustre_attr_nonrotational.attr,
453         &lustre_attr_index_backup.attr,
454         &lustre_attr_auto_scrub.attr,
455         &lustre_attr_sync_on_lseek.attr,
456         NULL,
457 };
458
459 struct ldebugfs_vars ldebugfs_osd_obd_vars[] = {
460         { .name =       "oi_scrub",
461           .fops =       &zfs_osd_oi_scrub_fops          },
462         { .name =       "readcache_max_filesize",
463           .fops =       &zfs_osd_readcache_fops         },
464         { 0 }
465 };
466
467 int osd_procfs_init(struct osd_device *osd, const char *name)
468 {
469         struct obd_type *type;
470         int rc;
471
472         ENTRY;
473
474         /* at the moment there is no linkage between lu_type
475          * and obd_type, so we lookup obd_type this way
476          */
477         type = class_search_type(LUSTRE_OSD_ZFS_NAME);
478
479         LASSERT(type);
480         LASSERT(name);
481
482         /* put reference taken by class_search_type */
483         kobject_put(&type->typ_kobj);
484
485         osd->od_dt_dev.dd_ktype.default_attrs = zfs_attrs;
486         rc = dt_tunables_init(&osd->od_dt_dev, type, name,
487                               ldebugfs_osd_obd_vars);
488         if (rc) {
489                 CERROR("%s: cannot setup sysfs / debugfs entry: %d\n",
490                        name, rc);
491                 GOTO(out, rc);
492         }
493
494         if (osd->od_proc_entry)
495                 RETURN(0);
496
497         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
498                                               NULL, &osd->od_dt_dev);
499         if (IS_ERR(osd->od_proc_entry)) {
500                 rc = PTR_ERR(osd->od_proc_entry);
501                 CERROR("Error %d setting up lprocfs for %s\n", rc, name);
502                 osd->od_proc_entry = NULL;
503                 GOTO(out, rc);
504         }
505
506         rc = osd_stats_init(osd);
507
508         GOTO(out, rc);
509 out:
510         if (rc)
511                 osd_procfs_fini(osd);
512         return rc;
513 }
514
515 int osd_procfs_fini(struct osd_device *osd)
516 {
517         ENTRY;
518
519         if (osd->od_stats)
520                 lprocfs_free_stats(&osd->od_stats);
521
522         if (osd->od_proc_entry) {
523                 lprocfs_remove(&osd->od_proc_entry);
524                 osd->od_proc_entry = NULL;
525         }
526
527         return dt_tunables_fini(&osd->od_dt_dev);
528 }
529
530 #endif