Whamcloud - gitweb
LU-14286 osd-ldiskfs: fallocate with unwritten extents
[fs/lustre-release.git] / lustre / osd-ldiskfs / 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) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd/osd_lproc.c
33  *
34  * Author: Mikhail Pershin <tappro@sun.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_OSD
38
39 #include <lprocfs_status.h>
40
41 #include "osd_internal.h"
42
43 #ifdef CONFIG_PROC_FS
44
45 void osd_brw_stats_update(struct osd_device *osd, struct osd_iobuf *iobuf)
46 {
47         struct brw_stats *s = &osd->od_brw_stats;
48         sector_t         *last_block = NULL;
49         struct page     **pages = iobuf->dr_pages;
50         struct page      *last_page = NULL;
51         unsigned long     discont_pages = 0;
52         unsigned long     discont_blocks = 0;
53         sector_t         *blocks = iobuf->dr_blocks;
54         int               i, nr_pages = iobuf->dr_npages;
55         int               blocks_per_page;
56         int               rw = iobuf->dr_rw;
57
58         if (unlikely(nr_pages == 0))
59                 return;
60
61         blocks_per_page = PAGE_SIZE >> osd_sb(osd)->s_blocksize_bits;
62
63         lprocfs_oh_tally_log2(&s->hist[BRW_R_PAGES+rw], nr_pages);
64
65         while (nr_pages-- > 0) {
66                 if (last_page && (*pages)->index != (last_page->index + 1))
67                         discont_pages++;
68                 last_page = *pages;
69                 pages++;
70                 for (i = 0; i < blocks_per_page; i++) {
71                         if (last_block && *blocks != (*last_block + 1))
72                                 discont_blocks++;
73                         last_block = blocks++;
74                 }
75         }
76
77         lprocfs_oh_tally(&s->hist[BRW_R_DISCONT_PAGES+rw], discont_pages);
78         lprocfs_oh_tally(&s->hist[BRW_R_DISCONT_BLOCKS+rw], discont_blocks);
79 }
80
81 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
82         struct obd_histogram *read, struct obd_histogram *write, int scale)
83 {
84         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
85         int i;
86
87         seq_printf(seq, "\n%26s read      |     write\n", " ");
88         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
89                    name, units, units);
90
91         read_tot = lprocfs_oh_sum(read);
92         write_tot = lprocfs_oh_sum(write);
93         for (i = 0; i < OBD_HIST_MAX; i++) {
94                 r = read->oh_buckets[i];
95                 w = write->oh_buckets[i];
96                 read_cum += r;
97                 write_cum += w;
98                 if (read_cum == 0 && write_cum == 0)
99                         continue;
100
101                 if (!scale)
102                         seq_printf(seq, "%u", i);
103                 else if (i < 10)
104                         seq_printf(seq, "%u", scale << i);
105                 else if (i < 20)
106                         seq_printf(seq, "%uK", scale << (i-10));
107                 else
108                         seq_printf(seq, "%uM", scale << (i-20));
109
110                 seq_printf(seq, ":\t\t%10lu %3u %3u   | %4lu %3u %3u\n",
111                            r, pct(r, read_tot), pct(read_cum, read_tot),
112                            w, pct(w, write_tot), pct(write_cum, write_tot));
113
114                 if (read_cum == read_tot && write_cum == write_tot)
115                         break;
116         }
117 }
118
119 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
120 {
121         struct timespec64 now;
122
123         /* this sampling races with updates */
124         ktime_get_real_ts64(&now);
125
126         seq_printf(seq, "snapshot_time:         %lld.%09ld (secs.nsecs)\n",
127                    (s64)now.tv_sec, now.tv_nsec);
128
129         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
130                           &brw_stats->hist[BRW_R_PAGES],
131                           &brw_stats->hist[BRW_W_PAGES], 1);
132
133         display_brw_stats(seq, "discontiguous pages", "rpcs",
134                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
135                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
136
137         display_brw_stats(seq, "discontiguous blocks", "rpcs",
138                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
139                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
140
141         display_brw_stats(seq, "disk fragmented I/Os", "ios",
142                           &brw_stats->hist[BRW_R_DIO_FRAGS],
143                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
144
145         display_brw_stats(seq, "disk I/Os in flight", "ios",
146                           &brw_stats->hist[BRW_R_RPC_HIST],
147                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
148
149         display_brw_stats(seq, "I/O time (1/1000s)", "ios",
150                           &brw_stats->hist[BRW_R_IO_TIME],
151                           &brw_stats->hist[BRW_W_IO_TIME], 1);
152
153         display_brw_stats(seq, "disk I/O size", "ios",
154                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
155                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
156 }
157
158 static int osd_brw_stats_seq_show(struct seq_file *seq, void *v)
159 {
160         struct osd_device *osd = seq->private;
161
162         brw_stats_show(seq, &osd->od_brw_stats);
163
164         return 0;
165 }
166
167 static ssize_t osd_brw_stats_seq_write(struct file *file,
168                                        const char __user *buf,
169                                        size_t len, loff_t *off)
170 {
171         struct seq_file *seq = file->private_data;
172         struct osd_device *osd = seq->private;
173         int i;
174
175         for (i = 0; i < BRW_LAST; i++)
176                 lprocfs_oh_clear(&osd->od_brw_stats.hist[i]);
177
178         return len;
179 }
180
181 LPROC_SEQ_FOPS(osd_brw_stats);
182
183 static int osd_stats_init(struct osd_device *osd)
184 {
185         int i, result;
186         ENTRY;
187
188         for (i = 0; i < BRW_LAST; i++)
189                 spin_lock_init(&osd->od_brw_stats.hist[i].oh_lock);
190
191         osd->od_stats = lprocfs_alloc_stats(LPROC_OSD_LAST, 0);
192         if (osd->od_stats != NULL) {
193                 result = lprocfs_register_stats(osd->od_proc_entry, "stats",
194                                                 osd->od_stats);
195                 if (result)
196                         GOTO(out, result);
197
198                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
199                                      LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
200                                      "get_page", "usec");
201                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
202                                      LPROCFS_CNTR_AVGMINMAX,
203                                      "get_page_failures", "num");
204                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
205                                      LPROCFS_CNTR_AVGMINMAX,
206                                      "cache_access", "pages");
207                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
208                                      LPROCFS_CNTR_AVGMINMAX,
209                                      "cache_hit", "pages");
210                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
211                                      LPROCFS_CNTR_AVGMINMAX,
212                                      "cache_miss", "pages");
213 #if OSD_THANDLE_STATS
214                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
215                                      LPROCFS_CNTR_AVGMINMAX,
216                                      "thandle starting", "usec");
217                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
218                                      LPROCFS_CNTR_AVGMINMAX,
219                                      "thandle open", "usec");
220                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
221                                      LPROCFS_CNTR_AVGMINMAX,
222                                      "thandle closing", "usec");
223 #endif
224                 result = lprocfs_seq_create(osd->od_proc_entry, "brw_stats",
225                                             0644, &osd_brw_stats_fops, osd);
226         } else
227                 result = -ENOMEM;
228
229 out:
230         RETURN(result);
231 }
232
233 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
234                            char *buf)
235 {
236         return sprintf(buf, "ldiskfs\n");
237 }
238 LUSTRE_RO_ATTR(fstype);
239
240 static ssize_t mntdev_show(struct kobject *kobj, struct attribute *attr,
241                            char *buf)
242 {
243         struct dt_device *dt = container_of(kobj, struct dt_device,
244                                             dd_kobj);
245         struct osd_device *osd = osd_dt_dev(dt);
246
247         LASSERT(osd);
248         if (unlikely(!osd->od_mnt))
249                 return -EINPROGRESS;
250
251         return sprintf(buf, "%s\n", osd->od_mntdev);
252 }
253 LUSTRE_RO_ATTR(mntdev);
254
255 static ssize_t read_cache_enable_show(struct kobject *kobj,
256                                       struct attribute *attr,
257                                       char *buf)
258 {
259         struct dt_device *dt = container_of(kobj, struct dt_device,
260                                             dd_kobj);
261         struct osd_device *osd = osd_dt_dev(dt);
262
263         LASSERT(osd);
264         if (unlikely(!osd->od_mnt))
265                 return -EINPROGRESS;
266
267         return sprintf(buf, "%u\n", osd->od_read_cache);
268 }
269
270 static ssize_t read_cache_enable_store(struct kobject *kobj,
271                                        struct attribute *attr,
272                                        const char *buffer, size_t count)
273 {
274         struct dt_device *dt = container_of(kobj, struct dt_device,
275                                             dd_kobj);
276         struct osd_device *osd = osd_dt_dev(dt);
277         bool val;
278         int rc;
279
280         LASSERT(osd);
281         if (unlikely(!osd->od_mnt))
282                 return -EINPROGRESS;
283
284         rc = kstrtobool(buffer, &val);
285         if (rc)
286                 return rc;
287
288         osd->od_read_cache = !!val;
289         return count;
290 }
291 LUSTRE_RW_ATTR(read_cache_enable);
292
293 static ssize_t writethrough_cache_enable_show(struct kobject *kobj,
294                                               struct attribute *attr,
295                                               char *buf)
296 {
297         struct dt_device *dt = container_of(kobj, struct dt_device,
298                                             dd_kobj);
299         struct osd_device *osd = osd_dt_dev(dt);
300
301         LASSERT(osd);
302         if (unlikely(!osd->od_mnt))
303                 return -EINPROGRESS;
304
305         return sprintf(buf, "%u\n", osd->od_writethrough_cache);
306 }
307
308 static ssize_t writethrough_cache_enable_store(struct kobject *kobj,
309                                                struct attribute *attr,
310                                                const char *buffer,
311                                                size_t count)
312 {
313         struct dt_device *dt = container_of(kobj, struct dt_device,
314                                             dd_kobj);
315         struct osd_device *osd = osd_dt_dev(dt);
316         bool val;
317         int rc;
318
319         LASSERT(osd);
320         if (unlikely(!osd->od_mnt))
321                 return -EINPROGRESS;
322
323         rc = kstrtobool(buffer, &val);
324         if (rc)
325                 return rc;
326
327         osd->od_writethrough_cache = !!val;
328         return count;
329 }
330 LUSTRE_RW_ATTR(writethrough_cache_enable);
331
332 static ssize_t fallocate_zero_blocks_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);
341         if (unlikely(!osd->od_mnt))
342                 return -EINPROGRESS;
343
344         return scnprintf(buf, PAGE_SIZE, "%d\n", osd->od_fallocate_zero_blocks);
345 }
346
347 /*
348  * Set how fallocate() interacts with the backing filesystem:
349  * -1: fallocate is disabled and returns -EOPNOTSUPP
350  *  0: fallocate allocates unwritten extents (like ext4)
351  *  1: fallocate zeroes allocated extents on disk
352  */
353 static ssize_t fallocate_zero_blocks_store(struct kobject *kobj,
354                                            struct attribute *attr,
355                                            const char *buffer, size_t count)
356 {
357         struct dt_device *dt = container_of(kobj, struct dt_device,
358                                             dd_kobj);
359         struct osd_device *osd = osd_dt_dev(dt);
360         long val;
361         int rc;
362
363         LASSERT(osd);
364         if (unlikely(!osd->od_mnt))
365                 return -EINPROGRESS;
366
367         rc = kstrtol(buffer, 0, &val);
368         if (rc)
369                 return rc;
370
371         if (val < -1 || val > 1)
372                 return -EINVAL;
373
374         osd->od_fallocate_zero_blocks = val;
375         return count;
376 }
377 LUSTRE_RW_ATTR(fallocate_zero_blocks);
378
379 ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
380                          const char *buffer, size_t count)
381 {
382         struct dt_device *dt = container_of(kobj, struct dt_device,
383                                             dd_kobj);
384         struct osd_device *osd = osd_dt_dev(dt);
385         struct lu_env env;
386         int rc;
387
388         LASSERT(osd);
389         if (unlikely(!osd->od_mnt))
390                 return -EINPROGRESS;
391
392         rc = lu_env_init(&env, LCT_LOCAL);
393         if (rc)
394                 return rc;
395
396         rc = dt_sync(&env, dt);
397         lu_env_fini(&env);
398
399         return rc == 0 ? count : rc;
400 }
401 LUSTRE_WO_ATTR(force_sync);
402
403 static ssize_t nonrotational_show(struct kobject *kobj, struct attribute *attr,
404                                   char *buf)
405 {
406         struct dt_device *dt = container_of(kobj, struct dt_device,
407                                             dd_kobj);
408         struct osd_device *osd = osd_dt_dev(dt);
409
410         LASSERT(osd);
411         if (unlikely(!osd->od_mnt))
412                 return -EINPROGRESS;
413
414         return sprintf(buf, "%u\n", osd->od_nonrotational);
415 }
416
417 static ssize_t nonrotational_store(struct kobject *kobj,
418                                    struct attribute *attr, const char *buffer,
419                                    size_t count)
420 {
421         struct dt_device *dt = container_of(kobj, struct dt_device,
422                                             dd_kobj);
423         struct osd_device *osd = osd_dt_dev(dt);
424         bool val;
425         int rc;
426
427         LASSERT(osd);
428         if (unlikely(!osd->od_mnt))
429                 return -EINPROGRESS;
430
431         rc = kstrtobool(buffer, &val);
432         if (rc)
433                 return rc;
434
435         osd->od_nonrotational = val;
436         return count;
437 }
438 LUSTRE_RW_ATTR(nonrotational);
439
440 static ssize_t pdo_show(struct kobject *kobj, struct attribute *attr,
441                         char *buf)
442 {
443         return sprintf(buf, "%s\n", ldiskfs_pdo ? "ON" : "OFF");
444 }
445
446 static ssize_t pdo_store(struct kobject *kobj, struct attribute *attr,
447                          const char *buffer, size_t count)
448 {
449         bool pdo;
450         int rc;
451
452         rc = kstrtobool(buffer, &pdo);
453         if (rc != 0)
454                 return rc;
455
456         ldiskfs_pdo = pdo;
457
458         return count;
459 }
460 LUSTRE_RW_ATTR(pdo);
461
462 static ssize_t auto_scrub_show(struct kobject *kobj, struct attribute *attr,
463                                char *buf)
464 {
465         struct dt_device *dt = container_of(kobj, struct dt_device,
466                                             dd_kobj);
467         struct osd_device *dev = osd_dt_dev(dt);
468
469         LASSERT(dev);
470         if (unlikely(!dev->od_mnt))
471                 return -EINPROGRESS;
472
473         return sprintf(buf, "%lld\n", dev->od_auto_scrub_interval);
474 }
475
476 static ssize_t auto_scrub_store(struct kobject *kobj, struct attribute *attr,
477                                 const char *buffer, size_t count)
478 {
479         struct dt_device *dt = container_of(kobj, struct dt_device,
480                                             dd_kobj);
481         struct osd_device *dev = osd_dt_dev(dt);
482         s64 val;
483         int rc;
484
485         LASSERT(dev);
486         if (unlikely(!dev->od_mnt))
487                 return -EINPROGRESS;
488
489         rc = kstrtoll(buffer, 0, &val);
490         if (rc)
491                 return rc;
492
493         dev->od_auto_scrub_interval = val;
494         return count;
495 }
496 LUSTRE_RW_ATTR(auto_scrub);
497
498 static ssize_t full_scrub_ratio_show(struct kobject *kobj,
499                                      struct attribute *attr,
500                                      char *buf)
501 {
502         struct dt_device *dt = container_of(kobj, struct dt_device,
503                                             dd_kobj);
504         struct osd_device *dev = osd_dt_dev(dt);
505
506         LASSERT(dev);
507         if (unlikely(!dev->od_mnt))
508                 return -EINPROGRESS;
509
510         return sprintf(buf, "%llu\n", dev->od_full_scrub_ratio);
511 }
512
513 static ssize_t full_scrub_ratio_store(struct kobject *kobj,
514                                       struct attribute *attr,
515                                       const char *buffer, size_t count)
516 {
517         struct dt_device *dt = container_of(kobj, struct dt_device,
518                                             dd_kobj);
519         struct osd_device *dev = osd_dt_dev(dt);
520         s64 val;
521         int rc;
522
523         LASSERT(dev);
524         if (unlikely(!dev->od_mnt))
525                 return -EINPROGRESS;
526
527         rc = kstrtoll(buffer, 0, &val);
528         if (rc)
529                 return rc;
530
531         if (val < 0)
532                 return -EINVAL;
533
534         dev->od_full_scrub_ratio = val;
535         return count;
536 }
537 LUSTRE_RW_ATTR(full_scrub_ratio);
538
539 static ssize_t full_scrub_threshold_rate_show(struct kobject *kobj,
540                                               struct attribute *attr,
541                                               char *buf)
542 {
543         struct dt_device *dt = container_of(kobj, struct dt_device,
544                                             dd_kobj);
545         struct osd_device *dev = osd_dt_dev(dt);
546
547         LASSERT(dev);
548         if (unlikely(!dev->od_mnt))
549                 return -EINPROGRESS;
550
551         return sprintf(buf, "%llu (bad OI mappings/minute)\n",
552                        dev->od_full_scrub_threshold_rate);
553 }
554
555 static ssize_t full_scrub_threshold_rate_store(struct kobject *kobj,
556                                                struct attribute *attr,
557                                                const char *buffer, size_t count)
558 {
559         struct dt_device *dt = container_of(kobj, struct dt_device,
560                                             dd_kobj);
561         struct osd_device *dev = osd_dt_dev(dt);
562         u64 val;
563         int rc;
564
565         LASSERT(dev);
566         if (unlikely(!dev->od_mnt))
567                 return -EINPROGRESS;
568
569         rc = kstrtoull(buffer, 0, &val);
570         if (rc != 0)
571                 return rc;
572
573         dev->od_full_scrub_threshold_rate = val;
574         return count;
575 }
576 LUSTRE_RW_ATTR(full_scrub_threshold_rate);
577
578 static int ldiskfs_osd_oi_scrub_seq_show(struct seq_file *m, void *data)
579 {
580         struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
581
582         LASSERT(dev != NULL);
583         if (unlikely(dev->od_mnt == NULL))
584                 return -EINPROGRESS;
585
586         osd_scrub_dump(m, dev);
587         return 0;
588 }
589
590 LDEBUGFS_SEQ_FOPS_RO(ldiskfs_osd_oi_scrub);
591
592 static int ldiskfs_osd_readcache_seq_show(struct seq_file *m, void *data)
593 {
594         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
595
596         LASSERT(osd != NULL);
597         if (unlikely(osd->od_mnt == NULL))
598                 return -EINPROGRESS;
599
600         seq_printf(m, "%llu\n", osd->od_readcache_max_filesize);
601         return 0;
602 }
603
604 static ssize_t
605 ldiskfs_osd_readcache_seq_write(struct file *file, const char __user *buffer,
606                                 size_t count, loff_t *off)
607 {
608         struct seq_file *m = file->private_data;
609         struct dt_device *dt = m->private;
610         struct osd_device *osd = osd_dt_dev(dt);
611         char kernbuf[22] = "";
612         u64 val;
613         int rc;
614
615         LASSERT(osd != NULL);
616         if (unlikely(osd->od_mnt == NULL))
617                 return -EINPROGRESS;
618
619         if (count >= sizeof(kernbuf))
620                 return -EINVAL;
621
622         if (copy_from_user(kernbuf, buffer, count))
623                 return -EFAULT;
624         kernbuf[count] = 0;
625
626         rc = sysfs_memparse(kernbuf, count, &val, "B");
627         if (rc < 0)
628                 return rc;
629
630         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
631                                          OSD_MAX_CACHE_SIZE : val;
632         return count;
633 }
634
635 LDEBUGFS_SEQ_FOPS(ldiskfs_osd_readcache);
636
637 static int ldiskfs_osd_readcache_max_io_seq_show(struct seq_file *m, void *data)
638 {
639         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
640
641         LASSERT(osd != NULL);
642         if (unlikely(osd->od_mnt == NULL))
643                 return -EINPROGRESS;
644
645         seq_printf(m, "%lu\n", osd->od_readcache_max_iosize >> 20);
646         return 0;
647 }
648
649 static ssize_t
650 ldiskfs_osd_readcache_max_io_seq_write(struct file *file,
651                                        const char __user *buffer,
652                                        size_t count, loff_t *off)
653 {
654         struct seq_file *m = file->private_data;
655         struct dt_device *dt = m->private;
656         struct osd_device *osd = osd_dt_dev(dt);
657         char kernbuf[22] = "";
658         u64 val;
659         int rc;
660
661         LASSERT(osd != NULL);
662         if (unlikely(osd->od_mnt == NULL))
663                 return -EINPROGRESS;
664
665         if (count >= sizeof(kernbuf))
666                 return -EINVAL;
667
668         if (copy_from_user(kernbuf, buffer, count))
669                 return -EFAULT;
670         kernbuf[count] = 0;
671
672         rc = sysfs_memparse(kernbuf, count, &val, "MiB");
673         if (rc < 0)
674                 return rc;
675
676         if (val > PTLRPC_MAX_BRW_SIZE)
677                 return -ERANGE;
678         osd->od_readcache_max_iosize = val;
679         return count;
680 }
681
682 LDEBUGFS_SEQ_FOPS(ldiskfs_osd_readcache_max_io);
683
684 static int ldiskfs_osd_writethrough_max_io_seq_show(struct seq_file *m,
685                                                     void *data)
686 {
687         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
688
689         LASSERT(osd != NULL);
690         if (unlikely(osd->od_mnt == NULL))
691                 return -EINPROGRESS;
692
693         seq_printf(m, "%lu\n", osd->od_writethrough_max_iosize >> 20);
694         return 0;
695 }
696
697 static ssize_t
698 ldiskfs_osd_writethrough_max_io_seq_write(struct file *file,
699                                        const char __user *buffer,
700                                        size_t count, loff_t *off)
701 {
702         struct seq_file *m = file->private_data;
703         struct dt_device *dt = m->private;
704         struct osd_device *osd = osd_dt_dev(dt);
705         char kernbuf[22] = "";
706         u64 val;
707         int rc;
708
709         LASSERT(osd != NULL);
710         if (unlikely(osd->od_mnt == NULL))
711                 return -EINPROGRESS;
712
713         if (count >= sizeof(kernbuf))
714                 return -EINVAL;
715
716         if (copy_from_user(kernbuf, buffer, count))
717                 return -EFAULT;
718         kernbuf[count] = 0;
719
720         rc = sysfs_memparse(kernbuf, count, &val, "MiB");
721         if (rc < 0)
722                 return rc;
723
724         if (val > PTLRPC_MAX_BRW_SIZE)
725                 return -ERANGE;
726         osd->od_writethrough_max_iosize = val;
727         return count;
728 }
729
730 LDEBUGFS_SEQ_FOPS(ldiskfs_osd_writethrough_max_io);
731
732 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 52, 0)
733 static ssize_t index_in_idif_show(struct kobject *kobj, struct attribute *attr,
734                                   char *buf)
735 {
736         struct dt_device *dt = container_of(kobj, struct dt_device,
737                                             dd_kobj);
738         struct osd_device *dev = osd_dt_dev(dt);
739
740         LASSERT(dev);
741         if (unlikely(!dev->od_mnt))
742                 return -EINPROGRESS;
743
744         return sprintf(buf, "%d\n", (int)(dev->od_index_in_idif));
745 }
746
747 static ssize_t index_in_idif_store(struct kobject *kobj,
748                                    struct attribute *attr,
749                                    const char *buffer, size_t count)
750 {
751         struct dt_device *dt = container_of(kobj, struct dt_device,
752                                             dd_kobj);
753         struct osd_device *dev = osd_dt_dev(dt);
754         struct lu_target *tgt;
755         struct lu_env env;
756         bool val;
757         int rc;
758
759         LASSERT(dev);
760         if (unlikely(!dev->od_mnt))
761                 return -EINPROGRESS;
762
763         rc = kstrtobool(buffer, &val);
764         if (rc)
765                 return rc;
766
767         if (dev->od_index_in_idif) {
768                 if (val)
769                         return count;
770
771                 LCONSOLE_WARN("%s: OST-index in IDIF has been enabled, "
772                               "it cannot be reverted back.\n", osd_name(dev));
773                 return -EPERM;
774         }
775
776         if (!val)
777                 return count;
778
779         rc = lu_env_init(&env, LCT_DT_THREAD);
780         if (rc)
781                 return rc;
782
783         tgt = dev->od_dt_dev.dd_lu_dev.ld_site->ls_tgt;
784         tgt->lut_lsd.lsd_feature_rocompat |= OBD_ROCOMPAT_IDX_IN_IDIF;
785         rc = tgt_server_data_update(&env, tgt, 1);
786         lu_env_fini(&env);
787         if (rc < 0)
788                 return rc;
789
790         LCONSOLE_INFO("%s: enable OST-index in IDIF successfully, "
791                       "it cannot be reverted back.\n", osd_name(dev));
792
793         dev->od_index_in_idif = 1;
794         return count;
795 }
796 LUSTRE_RW_ATTR(index_in_idif);
797
798 int osd_register_proc_index_in_idif(struct osd_device *osd)
799 {
800         struct dt_device *dt = &osd->od_dt_dev;
801
802         return sysfs_create_file(&dt->dd_kobj, &lustre_attr_index_in_idif.attr);
803 }
804 #endif
805
806 static ssize_t index_backup_show(struct kobject *kobj, struct attribute *attr,
807                                  char *buf)
808 {
809         struct dt_device *dt = container_of(kobj, struct dt_device,
810                                             dd_kobj);
811         struct osd_device *dev = osd_dt_dev(dt);
812
813         LASSERT(dev);
814         if (unlikely(!dev->od_mnt))
815                 return -EINPROGRESS;
816
817         return sprintf(buf, "%d\n", dev->od_index_backup_policy);
818 }
819
820 ssize_t index_backup_store(struct kobject *kobj, struct attribute *attr,
821                            const char *buffer, size_t count)
822 {
823         struct dt_device *dt = container_of(kobj, struct dt_device,
824                                            dd_kobj);
825         struct osd_device *dev = osd_dt_dev(dt);
826         int val;
827         int rc;
828
829         LASSERT(dev);
830         if (unlikely(!dev->od_mnt))
831                 return -EINPROGRESS;
832
833         rc = kstrtoint(buffer, 0, &val);
834         if (rc)
835                 return rc;
836
837         dev->od_index_backup_policy = val;
838         return count;
839 }
840 LUSTRE_RW_ATTR(index_backup);
841
842 struct ldebugfs_vars ldebugfs_osd_obd_vars[] = {
843         { .name =       "oi_scrub",
844           .fops =       &ldiskfs_osd_oi_scrub_fops      },
845         { .name =       "readcache_max_filesize",
846           .fops =       &ldiskfs_osd_readcache_fops     },
847         { .name =       "readcache_max_io_mb",
848           .fops =       &ldiskfs_osd_readcache_max_io_fops      },
849         { .name =       "writethrough_max_io_mb",
850           .fops =       &ldiskfs_osd_writethrough_max_io_fops   },
851         { NULL }
852 };
853
854 static struct attribute *ldiskfs_attrs[] = {
855         &lustre_attr_read_cache_enable.attr,
856         &lustre_attr_writethrough_cache_enable.attr,
857         &lustre_attr_fstype.attr,
858         &lustre_attr_mntdev.attr,
859         &lustre_attr_fallocate_zero_blocks.attr,
860         &lustre_attr_force_sync.attr,
861         &lustre_attr_nonrotational.attr,
862         &lustre_attr_index_backup.attr,
863         &lustre_attr_auto_scrub.attr,
864         &lustre_attr_pdo.attr,
865         &lustre_attr_full_scrub_ratio.attr,
866         &lustre_attr_full_scrub_threshold_rate.attr,
867         NULL,
868 };
869
870 int osd_procfs_init(struct osd_device *osd, const char *name)
871 {
872         struct obd_type *type;
873         int rc;
874
875         ENTRY;
876
877         /* at the moment there is no linkage between lu_type
878          * and obd_type, so we lookup obd_type this way
879          */
880         type = class_search_type(LUSTRE_OSD_LDISKFS_NAME);
881
882         LASSERT(name);
883         LASSERT(type);
884
885         CDEBUG(D_CONFIG, "%s: register osd-ldiskfs tunable parameters\n", name);
886
887         /* put reference taken by class_search_type */
888         kobject_put(&type->typ_kobj);
889
890         osd->od_dt_dev.dd_ktype.default_attrs = ldiskfs_attrs;
891         rc = dt_tunables_init(&osd->od_dt_dev, type, name,
892                               ldebugfs_osd_obd_vars);
893         if (rc) {
894                 CERROR("%s: cannot setup sysfs / debugfs entry: %d\n",
895                        name, rc);
896                 GOTO(out, rc);
897         }
898
899         if (osd->od_proc_entry)
900                 RETURN(0);
901
902         /* Find the type procroot and add the proc entry for this device */
903         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
904                                               NULL, &osd->od_dt_dev);
905         if (IS_ERR(osd->od_proc_entry)) {
906                 rc = PTR_ERR(osd->od_proc_entry);
907                 CERROR("Error %d setting up lprocfs for %s\n",
908                        rc, name);
909                 osd->od_proc_entry = NULL;
910                 GOTO(out, rc);
911         }
912
913         rc = osd_stats_init(osd);
914
915         EXIT;
916 out:
917         if (rc)
918                 osd_procfs_fini(osd);
919         return rc;
920 }
921
922 int osd_procfs_fini(struct osd_device *osd)
923 {
924         if (osd->od_stats)
925                 lprocfs_free_stats(&osd->od_stats);
926
927         if (osd->od_proc_entry)
928                 lprocfs_remove(&osd->od_proc_entry);
929
930         return dt_tunables_fini(&osd->od_dt_dev);
931 }
932 #endif