Whamcloud - gitweb
6b2eecf17ec4b93191c7d677f5f7093ffcdbf16e
[fs/lustre-release.git] / lustre / mdc / lproc_mdc.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31 #define DEBUG_SUBSYSTEM S_CLASS
32
33 #include <linux/vfs.h>
34 #include <obd_class.h>
35 #include <obd_cksum.h>
36 #include <lprocfs_status.h>
37 #include <lustre_osc.h>
38 #include <cl_object.h>
39 #include "mdc_internal.h"
40
41 static ssize_t active_show(struct kobject *kobj, struct attribute *attr,
42                            char *buf)
43 {
44         struct obd_device *obd = container_of(kobj, struct obd_device,
45                                               obd_kset.kobj);
46         struct obd_import *imp;
47         ssize_t len;
48
49         with_imp_locked(obd, imp, len)
50                 len = sprintf(buf, "%d\n", !imp->imp_deactive);
51         return len;
52 }
53
54 static ssize_t active_store(struct kobject *kobj, struct attribute *attr,
55                             const char *buffer, size_t count)
56 {
57         struct obd_device *obd = container_of(kobj, struct obd_device,
58                                               obd_kset.kobj);
59         struct obd_import *imp, *imp0;
60         bool val;
61         int rc;
62
63         rc = kstrtobool(buffer, &val);
64         if (rc)
65                 return rc;
66
67         with_imp_locked(obd, imp0, rc)
68                 imp = class_import_get(imp0);
69         if (rc)
70                 return rc;
71         /* opposite senses */
72         if (imp->imp_deactive == val)
73                 rc = ptlrpc_set_import_active(imp, val);
74         else
75                 CDEBUG(D_CONFIG, "activate %u: ignoring repeat request\n",
76                        val);
77         class_import_put(imp);
78         return rc ?: count;
79 }
80 LUSTRE_RW_ATTR(active);
81
82 static ssize_t max_rpcs_in_flight_show(struct kobject *kobj,
83                                        struct attribute *attr,
84                                        char *buf)
85 {
86         struct obd_device *obd = container_of(kobj, struct obd_device,
87                                               obd_kset.kobj);
88         ssize_t len;
89         u32 max;
90
91         max = obd_get_max_rpcs_in_flight(&obd->u.cli);
92         len = sprintf(buf, "%u\n", max);
93
94         return len;
95 }
96
97 static ssize_t max_rpcs_in_flight_store(struct kobject *kobj,
98                                         struct attribute *attr,
99                                         const char *buffer,
100                                         size_t count)
101 {
102         struct obd_device *obd = container_of(kobj, struct obd_device,
103                                               obd_kset.kobj);
104         struct obd_import *imp;
105         unsigned int val;
106         int rc;
107
108         rc = kstrtouint(buffer, 10, &val);
109         if (rc)
110                 return rc;
111
112         with_imp_locked(obd, imp, rc)
113                 rc = obd_set_max_rpcs_in_flight(&obd->u.cli, val);
114
115         return rc ? rc : count;
116 }
117 LUSTRE_RW_ATTR(max_rpcs_in_flight);
118
119 static ssize_t max_mod_rpcs_in_flight_show(struct kobject *kobj,
120                                            struct attribute *attr,
121                                            char *buf)
122 {
123         struct obd_device *obd = container_of(kobj, struct obd_device,
124                                               obd_kset.kobj);
125         u16 max;
126
127         max = obd_get_max_mod_rpcs_in_flight(&obd->u.cli);
128         return sprintf(buf, "%hu\n", max);
129 }
130
131 static ssize_t max_mod_rpcs_in_flight_store(struct kobject *kobj,
132                                             struct attribute *attr,
133                                             const char *buffer,
134                                             size_t count)
135 {
136         struct obd_device *obd = container_of(kobj, struct obd_device,
137                                               obd_kset.kobj);
138         struct obd_import *imp;
139         u16 val;
140         int rc;
141
142         rc = kstrtou16(buffer, 10, &val);
143         if (rc)
144                 return rc;
145
146         with_imp_locked(obd, imp, rc)
147                 rc = obd_set_max_mod_rpcs_in_flight(&obd->u.cli, val);
148
149         return rc ? rc : count;
150 }
151 LUSTRE_RW_ATTR(max_mod_rpcs_in_flight);
152
153 static int mdc_max_dirty_mb_seq_show(struct seq_file *m, void *v)
154 {
155         struct obd_device *obd = m->private;
156         struct client_obd *cli = &obd->u.cli;
157
158         seq_printf(m, "%lu\n", PAGES_TO_MiB(cli->cl_dirty_max_pages));
159         return 0;
160 }
161
162 static ssize_t mdc_max_dirty_mb_seq_write(struct file *file,
163                                           const char __user *buffer,
164                                           size_t count, loff_t *off)
165 {
166         struct seq_file *sfl = file->private_data;
167         struct obd_device *obd = sfl->private;
168         struct client_obd *cli = &obd->u.cli;
169         char kernbuf[22] = "";
170         u64 pages_number;
171         int rc;
172
173         if (count >= sizeof(kernbuf))
174                 return -EINVAL;
175
176         if (copy_from_user(kernbuf, buffer, count))
177                 return -EFAULT;
178         kernbuf[count] = 0;
179
180         rc = sysfs_memparse(kernbuf, count, &pages_number, "MiB");
181         if (rc < 0)
182                 return rc;
183
184         /* MB -> pages */
185         pages_number = round_up(pages_number, 1024 * 1024) >> PAGE_SHIFT;
186         if (pages_number <= 0 ||
187             pages_number >= MiB_TO_PAGES(OSC_MAX_DIRTY_MB_MAX) ||
188             pages_number > cfs_totalram_pages() / 4) /* 1/4 of RAM */
189                 return -ERANGE;
190
191         spin_lock(&cli->cl_loi_list_lock);
192         cli->cl_dirty_max_pages = pages_number;
193         osc_wake_cache_waiters(cli);
194         spin_unlock(&cli->cl_loi_list_lock);
195
196         return count;
197 }
198 LPROC_SEQ_FOPS(mdc_max_dirty_mb);
199
200 DECLARE_CKSUM_NAME;
201
202 static int mdc_checksum_type_seq_show(struct seq_file *m, void *v)
203 {
204         struct obd_device *obd = m->private;
205         int i;
206
207         if (obd == NULL)
208                 return 0;
209
210         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
211                 if ((BIT(i) & obd->u.cli.cl_supp_cksum_types) == 0)
212                         continue;
213                 if (obd->u.cli.cl_cksum_type == BIT(i))
214                         seq_printf(m, "[%s] ", cksum_name[i]);
215                 else
216                         seq_printf(m, "%s ", cksum_name[i]);
217         }
218         seq_puts(m, "\n");
219
220         return 0;
221 }
222
223 static ssize_t mdc_checksum_type_seq_write(struct file *file,
224                                            const char __user *buffer,
225                                            size_t count, loff_t *off)
226 {
227         struct seq_file *m = file->private_data;
228         struct obd_device *obd = m->private;
229         char kernbuf[10];
230         int rc = -EINVAL;
231         int i;
232
233         if (obd == NULL)
234                 return 0;
235
236         if (count > sizeof(kernbuf) - 1)
237                 return -EINVAL;
238         if (copy_from_user(kernbuf, buffer, count))
239                 return -EFAULT;
240
241         if (count > 0 && kernbuf[count - 1] == '\n')
242                 kernbuf[count - 1] = '\0';
243         else
244                 kernbuf[count] = '\0';
245
246         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
247                 if (strcasecmp(kernbuf, cksum_name[i]) == 0) {
248                         obd->u.cli.cl_preferred_cksum_type = BIT(i);
249                         if (obd->u.cli.cl_supp_cksum_types & BIT(i)) {
250                                 obd->u.cli.cl_cksum_type = BIT(i);
251                                 rc = count;
252                         } else {
253                                 rc = -ENOTSUPP;
254                         }
255                         break;
256                 }
257         }
258
259         return rc;
260 }
261 LPROC_SEQ_FOPS(mdc_checksum_type);
262
263 static ssize_t checksums_show(struct kobject *kobj,
264                               struct attribute *attr, char *buf)
265 {
266         struct obd_device *obd = container_of(kobj, struct obd_device,
267                                               obd_kset.kobj);
268
269         return scnprintf(buf, PAGE_SIZE, "%d\n", !!obd->u.cli.cl_checksum);
270 }
271
272 static ssize_t checksums_store(struct kobject *kobj,
273                                struct attribute *attr,
274                                const char *buffer,
275                                size_t count)
276 {
277         struct obd_device *obd = container_of(kobj, struct obd_device,
278                                               obd_kset.kobj);
279         bool val;
280         int rc;
281
282         rc = kstrtobool(buffer, &val);
283         if (rc)
284                 return rc;
285
286         obd->u.cli.cl_checksum = val;
287
288         return count;
289 }
290 LUSTRE_RW_ATTR(checksums);
291
292 static ssize_t checksum_dump_show(struct kobject *kobj,
293                                   struct attribute *attr, char *buf)
294 {
295         struct obd_device *obd = container_of(kobj, struct obd_device,
296                                               obd_kset.kobj);
297
298         return scnprintf(buf, PAGE_SIZE, "%d\n", !!obd->u.cli.cl_checksum_dump);
299 }
300
301 static ssize_t checksum_dump_store(struct kobject *kobj,
302                                    struct attribute *attr,
303                                    const char *buffer,
304                                    size_t count)
305 {
306         struct obd_device *obd = container_of(kobj, struct obd_device,
307                                               obd_kset.kobj);
308         bool val;
309         int rc;
310
311         rc = kstrtobool(buffer, &val);
312         if (rc)
313                 return rc;
314
315         obd->u.cli.cl_checksum_dump = val;
316
317         return count;
318 }
319 LUSTRE_RW_ATTR(checksum_dump);
320
321 LUSTRE_ATTR(mds_conn_uuid, 0444, conn_uuid_show, NULL);
322 LUSTRE_RO_ATTR(conn_uuid);
323
324 LUSTRE_RW_ATTR(ping);
325
326 static int mdc_cached_mb_seq_show(struct seq_file *m, void *v)
327 {
328         struct obd_device *obd = m->private;
329         struct client_obd *cli = &obd->u.cli;
330         int shift = 20 - PAGE_SHIFT;
331
332         seq_printf(m, "used_mb: %ld\n"
333                    "busy_cnt: %ld\n"
334                    "reclaim: %llu\n",
335                    (atomic_long_read(&cli->cl_lru_in_list) +
336                     atomic_long_read(&cli->cl_lru_busy)) >> shift,
337                     atomic_long_read(&cli->cl_lru_busy),
338                    cli->cl_lru_reclaim);
339
340         return 0;
341 }
342
343 /* shrink the number of caching pages to a specific number */
344 static ssize_t
345 mdc_cached_mb_seq_write(struct file *file, const char __user *buffer,
346                         size_t count, loff_t *off)
347 {
348         struct seq_file *sfl = file->private_data;
349         struct obd_device *obd = sfl->private;
350         struct client_obd *cli = &obd->u.cli;
351         u64 pages_number;
352         const char *tmp;
353         long rc;
354         char kernbuf[128];
355
356         if (count >= sizeof(kernbuf))
357                 return -EINVAL;
358
359         if (copy_from_user(kernbuf, buffer, count))
360                 return -EFAULT;
361         kernbuf[count] = 0;
362
363         tmp = lprocfs_find_named_value(kernbuf, "used_mb:", &count);
364         rc = sysfs_memparse(tmp, count, &pages_number, "MiB");
365         if (rc < 0)
366                 return rc;
367
368         pages_number >>= PAGE_SHIFT;
369
370         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
371         if (rc > 0) {
372                 struct lu_env *env;
373                 __u16 refcheck;
374
375                 env = cl_env_get(&refcheck);
376                 if (!IS_ERR(env)) {
377                         (void)osc_lru_shrink(env, cli, rc, true);
378                         cl_env_put(env, &refcheck);
379                 }
380         }
381
382         return count;
383 }
384 LPROC_SEQ_FOPS(mdc_cached_mb);
385
386 static int mdc_unstable_stats_seq_show(struct seq_file *m, void *v)
387 {
388         struct obd_device *obd = m->private;
389         struct client_obd *cli = &obd->u.cli;
390         long pages;
391         int mb;
392
393         pages = atomic_long_read(&cli->cl_unstable_count);
394         mb    = (pages * PAGE_SIZE) >> 20;
395
396         seq_printf(m, "unstable_pages: %20ld\n"
397                    "unstable_mb:              %10d\n", pages, mb);
398         return 0;
399 }
400 LPROC_SEQ_FOPS_RO(mdc_unstable_stats);
401
402 static ssize_t mdc_rpc_stats_seq_write(struct file *file,
403                                        const char __user *buf,
404                                        size_t len, loff_t *off)
405 {
406         struct seq_file *seq = file->private_data;
407         struct obd_device *obd = seq->private;
408         struct client_obd *cli = &obd->u.cli;
409
410         lprocfs_oh_clear(&cli->cl_mod_rpcs_hist);
411
412         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
413         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
414         lprocfs_oh_clear(&cli->cl_read_page_hist);
415         lprocfs_oh_clear(&cli->cl_write_page_hist);
416         lprocfs_oh_clear(&cli->cl_read_offset_hist);
417         lprocfs_oh_clear(&cli->cl_write_offset_hist);
418
419         return len;
420 }
421
422 static int mdc_rpc_stats_seq_show(struct seq_file *seq, void *v)
423 {
424         struct obd_device *obd = seq->private;
425         struct client_obd *cli = &obd->u.cli;
426         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
427         int i;
428
429         obd_mod_rpc_stats_seq_show(cli, seq);
430
431         spin_lock(&cli->cl_loi_list_lock);
432
433         seq_printf(seq, "\nread RPCs in flight:  %d\n",
434                    cli->cl_r_in_flight);
435         seq_printf(seq, "write RPCs in flight: %d\n",
436                    cli->cl_w_in_flight);
437         seq_printf(seq, "pending write pages:  %d\n",
438                    atomic_read(&cli->cl_pending_w_pages));
439         seq_printf(seq, "pending read pages:   %d\n",
440                    atomic_read(&cli->cl_pending_r_pages));
441
442         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
443         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
444         seq_printf(seq, "       rpcs   %% cum %%\n");
445
446         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
447         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
448
449         read_cum = 0;
450         write_cum = 0;
451         for (i = 0; i < OBD_HIST_MAX; i++) {
452                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
453                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
454
455                 read_cum += r;
456                 write_cum += w;
457                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
458                            1 << i, r, pct(r, read_tot),
459                            pct(read_cum, read_tot), w,
460                            pct(w, write_tot),
461                            pct(write_cum, write_tot));
462                 if (read_cum == read_tot && write_cum == write_tot)
463                         break;
464         }
465
466         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
467         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
468         seq_printf(seq, "       rpcs   %% cum %%\n");
469
470         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
471         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
472
473         read_cum = 0;
474         write_cum = 0;
475         for (i = 1; i < OBD_HIST_MAX; i++) {
476                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
477                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
478
479                 read_cum += r;
480                 write_cum += w;
481                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
482                            i, r, pct(r, read_tot), pct(read_cum, read_tot), w,
483                            pct(w, write_tot), pct(write_cum, write_tot));
484                 if (read_cum == read_tot && write_cum == write_tot)
485                         break;
486         }
487
488         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
489         seq_printf(seq, "offset                rpcs   %% cum %% |");
490         seq_printf(seq, "       rpcs   %% cum %%\n");
491
492         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
493         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
494
495         read_cum = 0;
496         write_cum = 0;
497         for (i = 0; i < OBD_HIST_MAX; i++) {
498                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
499                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
500
501                 read_cum += r;
502                 write_cum += w;
503                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
504                            (i == 0) ? 0 : 1 << (i - 1),
505                            r, pct(r, read_tot), pct(read_cum, read_tot),
506                            w, pct(w, write_tot), pct(write_cum, write_tot));
507                 if (read_cum == read_tot && write_cum == write_tot)
508                         break;
509         }
510         spin_unlock(&cli->cl_loi_list_lock);
511
512         return 0;
513 }
514 LPROC_SEQ_FOPS(mdc_rpc_stats);
515
516 static int mdc_stats_seq_show(struct seq_file *seq, void *v)
517 {
518         struct obd_device *obd = seq->private;
519         struct osc_stats *stats = &obd2osc_dev(obd)->od_stats;
520
521         lprocfs_stats_header(seq, ktime_get(), stats->os_init, 25, ":", true,
522                              "");
523         seq_printf(seq, "lockless_write_bytes\t\t%llu\n",
524                    stats->os_lockless_writes);
525         seq_printf(seq, "lockless_read_bytes\t\t%llu\n",
526                    stats->os_lockless_reads);
527         return 0;
528 }
529
530 static ssize_t mdc_stats_seq_write(struct file *file,
531                                    const char __user *buf,
532                                    size_t len, loff_t *off)
533 {
534         struct seq_file *seq = file->private_data;
535         struct obd_device *obd = seq->private;
536         struct osc_stats *stats = &obd2osc_dev(obd)->od_stats;
537
538         memset(stats, 0, sizeof(*stats));
539         stats->os_init = ktime_get();
540
541         return len;
542 }
543 LPROC_SEQ_FOPS(mdc_stats);
544
545 static int mdc_dom_min_repsize_seq_show(struct seq_file *m, void *v)
546 {
547         struct obd_device *obd = m->private;
548
549         seq_printf(m, "%u\n", obd->u.cli.cl_dom_min_inline_repsize);
550
551         return 0;
552 }
553
554 static ssize_t mdc_dom_min_repsize_seq_write(struct file *file,
555                                              const char __user *buffer,
556                                              size_t count, loff_t *off)
557 {
558         struct seq_file *m = file->private_data;
559         struct obd_device *obd = m->private;
560         unsigned int val;
561         int rc;
562
563         rc = kstrtouint_from_user(buffer, count, 0, &val);
564         if (rc)
565                 return rc;
566
567         if (val > MDC_DOM_MAX_INLINE_REPSIZE)
568                 return -ERANGE;
569
570         obd->u.cli.cl_dom_min_inline_repsize = val;
571         return count;
572 }
573 LPROC_SEQ_FOPS(mdc_dom_min_repsize);
574
575 static int mdc_lsom_seq_show(struct seq_file *m, void *v)
576 {
577         struct obd_device *dev = m->private;
578
579         seq_printf(m, "%s\n", dev->u.cli.cl_lsom_update ? "On" : "Off");
580
581         return 0;
582 }
583
584 static ssize_t mdc_lsom_seq_write(struct file *file,
585                                   const char __user *buffer,
586                                   size_t count, loff_t *off)
587 {
588         struct obd_device *dev;
589         bool val;
590         int rc;
591
592         dev =  ((struct seq_file *)file->private_data)->private;
593         rc = kstrtobool_from_user(buffer, count, &val);
594         if (rc)
595                 return rc;
596
597         dev->u.cli.cl_lsom_update = val;
598         return count;
599 }
600 LPROC_SEQ_FOPS(mdc_lsom);
601
602
603 LPROC_SEQ_FOPS_RO_TYPE(mdc, connect_flags);
604 LPROC_SEQ_FOPS_RO_TYPE(mdc, server_uuid);
605 LPROC_SEQ_FOPS_RO_TYPE(mdc, timeouts);
606 LPROC_SEQ_FOPS_RO_TYPE(mdc, state);
607 LPROC_SEQ_FOPS_RW_TYPE(mdc, obd_max_pages_per_rpc);
608 LPROC_SEQ_FOPS_RW_TYPE(mdc, import);
609 LPROC_SEQ_FOPS_RW_TYPE(mdc, pinger_recov);
610
611 struct lprocfs_vars lprocfs_mdc_obd_vars[] = {
612         { .name =       "connect_flags",
613           .fops =       &mdc_connect_flags_fops },
614         { .name =       "mds_server_uuid",
615           .fops =       &mdc_server_uuid_fops   },
616         { .name =       "max_pages_per_rpc",
617           .fops =       &mdc_obd_max_pages_per_rpc_fops },
618         { .name =       "max_dirty_mb",
619           .fops =       &mdc_max_dirty_mb_fops          },
620         { .name =       "mdc_cached_mb",
621           .fops =       &mdc_cached_mb_fops             },
622         { .name =       "checksum_type",
623           .fops =       &mdc_checksum_type_fops         },
624         { .name =       "timeouts",
625           .fops =       &mdc_timeouts_fops              },
626         { .name =       "import",
627           .fops =       &mdc_import_fops                },
628         { .name =       "state",
629           .fops =       &mdc_state_fops                 },
630         { .name =       "pinger_recov",
631           .fops =       &mdc_pinger_recov_fops          },
632         { .name =       "rpc_stats",
633           .fops =       &mdc_rpc_stats_fops             },
634         { .name =       "unstable_stats",
635           .fops =       &mdc_unstable_stats_fops        },
636         { .name =       "mdc_stats",
637           .fops =       &mdc_stats_fops                 },
638         { .name =       "mdc_dom_min_repsize",
639           .fops =       &mdc_dom_min_repsize_fops       },
640         { .name =       "mdc_lsom",
641           .fops =       &mdc_lsom_fops                  },
642         { NULL }
643 };
644
645 static ssize_t cur_lost_grant_bytes_show(struct kobject *kobj,
646                                          struct attribute *attr,
647                                          char *buf)
648 {
649         struct obd_device *obd = container_of(kobj, struct obd_device,
650                                               obd_kset.kobj);
651         struct client_obd *cli = &obd->u.cli;
652
653         return scnprintf(buf, PAGE_SIZE, "%lu\n", cli->cl_lost_grant);
654 }
655 LUSTRE_RO_ATTR(cur_lost_grant_bytes);
656
657 static ssize_t cur_dirty_grant_bytes_show(struct kobject *kobj,
658                                           struct attribute *attr,
659                                           char *buf)
660 {
661         struct obd_device *obd = container_of(kobj, struct obd_device,
662                                               obd_kset.kobj);
663         struct client_obd *cli = &obd->u.cli;
664
665         return scnprintf(buf, PAGE_SIZE, "%lu\n", cli->cl_dirty_grant);
666 }
667 LUSTRE_RO_ATTR(cur_dirty_grant_bytes);
668
669 static ssize_t grant_shrink_show(struct kobject *kobj, struct attribute *attr,
670                                  char *buf)
671 {
672         struct obd_device *obd = container_of(kobj, struct obd_device,
673                                               obd_kset.kobj);
674         struct obd_import *imp;
675         ssize_t len;
676
677         with_imp_locked(obd, imp, len)
678                 len = scnprintf(buf, PAGE_SIZE, "%d\n",
679                                 !imp->imp_grant_shrink_disabled &&
680                                 OCD_HAS_FLAG(&imp->imp_connect_data,
681                                              GRANT_SHRINK));
682
683         return len;
684 }
685
686 static ssize_t grant_shrink_store(struct kobject *kobj, struct attribute *attr,
687                                   const char *buffer, size_t count)
688 {
689         struct obd_device *obd = container_of(kobj, struct obd_device,
690                                               obd_kset.kobj);
691         struct obd_import *imp;
692         bool val;
693         int rc;
694
695         if (obd == NULL)
696                 return 0;
697
698         rc = kstrtobool(buffer, &val);
699         if (rc)
700                 return rc;
701
702         with_imp_locked(obd, imp, rc) {
703                 spin_lock(&imp->imp_lock);
704                 imp->imp_grant_shrink_disabled = !val;
705                 spin_unlock(&imp->imp_lock);
706         }
707
708         return rc ?: count;
709 }
710 LUSTRE_RW_ATTR(grant_shrink);
711
712 static ssize_t grant_shrink_interval_show(struct kobject *kobj,
713                                           struct attribute *attr,
714                                           char *buf)
715 {
716         struct obd_device *obd = container_of(kobj, struct obd_device,
717                                               obd_kset.kobj);
718
719         return sprintf(buf, "%lld\n", obd->u.cli.cl_grant_shrink_interval);
720 }
721
722 static ssize_t grant_shrink_interval_store(struct kobject *kobj,
723                                            struct attribute *attr,
724                                            const char *buffer,
725                                            size_t count)
726 {
727         struct obd_device *obd = container_of(kobj, struct obd_device,
728                                               obd_kset.kobj);
729         unsigned int val;
730         int rc;
731
732         rc = kstrtouint(buffer, 0, &val);
733         if (rc)
734                 return rc;
735
736         if (val == 0)
737                 return -ERANGE;
738
739         obd->u.cli.cl_grant_shrink_interval = val;
740         osc_update_next_shrink(&obd->u.cli);
741         osc_schedule_grant_work();
742
743         return count;
744 }
745 LUSTRE_RW_ATTR(grant_shrink_interval);
746
747 static struct attribute *mdc_attrs[] = {
748         &lustre_attr_active.attr,
749         &lustre_attr_checksums.attr,
750         &lustre_attr_checksum_dump.attr,
751         &lustre_attr_max_rpcs_in_flight.attr,
752         &lustre_attr_max_mod_rpcs_in_flight.attr,
753         &lustre_attr_mds_conn_uuid.attr,
754         &lustre_attr_conn_uuid.attr,
755         &lustre_attr_ping.attr,
756         &lustre_attr_grant_shrink.attr,
757         &lustre_attr_grant_shrink_interval.attr,
758         &lustre_attr_cur_lost_grant_bytes.attr,
759         &lustre_attr_cur_dirty_grant_bytes.attr,
760         NULL,
761 };
762
763 int mdc_tunables_init(struct obd_device *obd)
764 {
765         int rc;
766
767         obd->obd_ktype.default_attrs = mdc_attrs;
768         obd->obd_vars = lprocfs_mdc_obd_vars;
769
770         rc = lprocfs_obd_setup(obd, false);
771         if (rc)
772                 goto out_failed;
773 #ifdef CONFIG_PROC_FS
774         rc = lprocfs_alloc_md_stats(obd, 0);
775         if (rc) {
776                 lprocfs_obd_cleanup(obd);
777                 goto out_failed;
778         }
779 #endif
780         rc = sptlrpc_lprocfs_cliobd_attach(obd);
781         if (rc) {
782 #ifdef CONFIG_PROC_FS
783                 lprocfs_free_md_stats(obd);
784 #endif
785                 lprocfs_obd_cleanup(obd);
786                 goto out_failed;
787         }
788         ptlrpc_lprocfs_register_obd(obd);
789
790 out_failed:
791         return rc;
792 }