Whamcloud - gitweb
506f06f28da4a17d89bcc71078c2a7b1b07ec209
[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         unsigned int val;
105         int rc;
106
107         rc = kstrtouint(buffer, 10, &val);
108         if (rc)
109                 return rc;
110
111         rc = obd_set_max_rpcs_in_flight(&obd->u.cli, val);
112         if (rc)
113                 count = rc;
114
115         return 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         u16 val;
139         int rc;
140
141         rc = kstrtou16(buffer, 10, &val);
142         if (rc)
143                 return rc;
144
145         rc = obd_set_max_mod_rpcs_in_flight(&obd->u.cli, val);
146         if (rc)
147                 count = rc;
148
149         return 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         seq_printf(seq, "lockless_write_bytes\t\t%llu\n",
523                    stats->os_lockless_writes);
524         seq_printf(seq, "lockless_read_bytes\t\t%llu\n",
525                    stats->os_lockless_reads);
526         return 0;
527 }
528
529 static ssize_t mdc_stats_seq_write(struct file *file,
530                                    const char __user *buf,
531                                    size_t len, loff_t *off)
532 {
533         struct seq_file *seq = file->private_data;
534         struct obd_device *obd = seq->private;
535         struct osc_stats *stats = &obd2osc_dev(obd)->od_stats;
536
537         memset(stats, 0, sizeof(*stats));
538         stats->os_init = ktime_get();
539
540         return len;
541 }
542 LPROC_SEQ_FOPS(mdc_stats);
543
544 static int mdc_dom_min_repsize_seq_show(struct seq_file *m, void *v)
545 {
546         struct obd_device *obd = m->private;
547
548         seq_printf(m, "%u\n", obd->u.cli.cl_dom_min_inline_repsize);
549
550         return 0;
551 }
552
553 static ssize_t mdc_dom_min_repsize_seq_write(struct file *file,
554                                              const char __user *buffer,
555                                              size_t count, loff_t *off)
556 {
557         struct seq_file *m = file->private_data;
558         struct obd_device *obd = m->private;
559         unsigned int val;
560         int rc;
561
562         rc = kstrtouint_from_user(buffer, count, 0, &val);
563         if (rc)
564                 return rc;
565
566         if (val > MDC_DOM_MAX_INLINE_REPSIZE)
567                 return -ERANGE;
568
569         obd->u.cli.cl_dom_min_inline_repsize = val;
570         return count;
571 }
572 LPROC_SEQ_FOPS(mdc_dom_min_repsize);
573
574 static int mdc_lsom_seq_show(struct seq_file *m, void *v)
575 {
576         struct obd_device *dev = m->private;
577
578         seq_printf(m, "%s\n", dev->u.cli.cl_lsom_update ? "On" : "Off");
579
580         return 0;
581 }
582
583 static ssize_t mdc_lsom_seq_write(struct file *file,
584                                   const char __user *buffer,
585                                   size_t count, loff_t *off)
586 {
587         struct obd_device *dev;
588         bool val;
589         int rc;
590
591         dev =  ((struct seq_file *)file->private_data)->private;
592         rc = kstrtobool_from_user(buffer, count, &val);
593         if (rc)
594                 return rc;
595
596         dev->u.cli.cl_lsom_update = val;
597         return count;
598 }
599 LPROC_SEQ_FOPS(mdc_lsom);
600
601
602 LPROC_SEQ_FOPS_RO_TYPE(mdc, connect_flags);
603 LPROC_SEQ_FOPS_RO_TYPE(mdc, server_uuid);
604 LPROC_SEQ_FOPS_RO_TYPE(mdc, timeouts);
605 LPROC_SEQ_FOPS_RO_TYPE(mdc, state);
606 LPROC_SEQ_FOPS_RW_TYPE(mdc, obd_max_pages_per_rpc);
607 LPROC_SEQ_FOPS_RW_TYPE(mdc, import);
608 LPROC_SEQ_FOPS_RW_TYPE(mdc, pinger_recov);
609
610 struct lprocfs_vars lprocfs_mdc_obd_vars[] = {
611         { .name =       "connect_flags",
612           .fops =       &mdc_connect_flags_fops },
613         { .name =       "mds_server_uuid",
614           .fops =       &mdc_server_uuid_fops   },
615         { .name =       "max_pages_per_rpc",
616           .fops =       &mdc_obd_max_pages_per_rpc_fops },
617         { .name =       "max_dirty_mb",
618           .fops =       &mdc_max_dirty_mb_fops          },
619         { .name =       "mdc_cached_mb",
620           .fops =       &mdc_cached_mb_fops             },
621         { .name =       "checksum_type",
622           .fops =       &mdc_checksum_type_fops         },
623         { .name =       "timeouts",
624           .fops =       &mdc_timeouts_fops              },
625         { .name =       "import",
626           .fops =       &mdc_import_fops                },
627         { .name =       "state",
628           .fops =       &mdc_state_fops                 },
629         { .name =       "pinger_recov",
630           .fops =       &mdc_pinger_recov_fops          },
631         { .name =       "rpc_stats",
632           .fops =       &mdc_rpc_stats_fops             },
633         { .name =       "unstable_stats",
634           .fops =       &mdc_unstable_stats_fops        },
635         { .name =       "mdc_stats",
636           .fops =       &mdc_stats_fops                 },
637         { .name =       "mdc_dom_min_repsize",
638           .fops =       &mdc_dom_min_repsize_fops       },
639         { .name =       "mdc_lsom",
640           .fops =       &mdc_lsom_fops                  },
641         { NULL }
642 };
643
644 static ssize_t cur_lost_grant_bytes_show(struct kobject *kobj,
645                                          struct attribute *attr,
646                                          char *buf)
647 {
648         struct obd_device *obd = container_of(kobj, struct obd_device,
649                                               obd_kset.kobj);
650         struct client_obd *cli = &obd->u.cli;
651
652         return scnprintf(buf, PAGE_SIZE, "%lu\n", cli->cl_lost_grant);
653 }
654 LUSTRE_RO_ATTR(cur_lost_grant_bytes);
655
656 static ssize_t cur_dirty_grant_bytes_show(struct kobject *kobj,
657                                           struct attribute *attr,
658                                           char *buf)
659 {
660         struct obd_device *obd = container_of(kobj, struct obd_device,
661                                               obd_kset.kobj);
662         struct client_obd *cli = &obd->u.cli;
663
664         return scnprintf(buf, PAGE_SIZE, "%lu\n", cli->cl_dirty_grant);
665 }
666 LUSTRE_RO_ATTR(cur_dirty_grant_bytes);
667
668 static ssize_t grant_shrink_show(struct kobject *kobj, struct attribute *attr,
669                                  char *buf)
670 {
671         struct obd_device *obd = container_of(kobj, struct obd_device,
672                                               obd_kset.kobj);
673         struct obd_import *imp;
674         ssize_t len;
675
676         with_imp_locked(obd, imp, len)
677                 len = scnprintf(buf, PAGE_SIZE, "%d\n",
678                                 !imp->imp_grant_shrink_disabled &&
679                                 OCD_HAS_FLAG(&imp->imp_connect_data,
680                                              GRANT_SHRINK));
681
682         return len;
683 }
684
685 static ssize_t grant_shrink_store(struct kobject *kobj, struct attribute *attr,
686                                   const char *buffer, size_t count)
687 {
688         struct obd_device *obd = container_of(kobj, struct obd_device,
689                                               obd_kset.kobj);
690         struct obd_import *imp;
691         bool val;
692         int rc;
693
694         if (obd == NULL)
695                 return 0;
696
697         rc = kstrtobool(buffer, &val);
698         if (rc)
699                 return rc;
700
701         with_imp_locked(obd, imp, rc) {
702                 spin_lock(&imp->imp_lock);
703                 imp->imp_grant_shrink_disabled = !val;
704                 spin_unlock(&imp->imp_lock);
705         }
706
707         return rc ?: count;
708 }
709 LUSTRE_RW_ATTR(grant_shrink);
710
711 static ssize_t grant_shrink_interval_show(struct kobject *kobj,
712                                           struct attribute *attr,
713                                           char *buf)
714 {
715         struct obd_device *obd = container_of(kobj, struct obd_device,
716                                               obd_kset.kobj);
717
718         return sprintf(buf, "%lld\n", obd->u.cli.cl_grant_shrink_interval);
719 }
720
721 static ssize_t grant_shrink_interval_store(struct kobject *kobj,
722                                            struct attribute *attr,
723                                            const char *buffer,
724                                            size_t count)
725 {
726         struct obd_device *obd = container_of(kobj, struct obd_device,
727                                               obd_kset.kobj);
728         unsigned int val;
729         int rc;
730
731         rc = kstrtouint(buffer, 0, &val);
732         if (rc)
733                 return rc;
734
735         if (val == 0)
736                 return -ERANGE;
737
738         obd->u.cli.cl_grant_shrink_interval = val;
739         osc_update_next_shrink(&obd->u.cli);
740         osc_schedule_grant_work();
741
742         return count;
743 }
744 LUSTRE_RW_ATTR(grant_shrink_interval);
745
746 static struct attribute *mdc_attrs[] = {
747         &lustre_attr_active.attr,
748         &lustre_attr_checksums.attr,
749         &lustre_attr_checksum_dump.attr,
750         &lustre_attr_max_rpcs_in_flight.attr,
751         &lustre_attr_max_mod_rpcs_in_flight.attr,
752         &lustre_attr_mds_conn_uuid.attr,
753         &lustre_attr_conn_uuid.attr,
754         &lustre_attr_ping.attr,
755         &lustre_attr_grant_shrink.attr,
756         &lustre_attr_grant_shrink_interval.attr,
757         &lustre_attr_cur_lost_grant_bytes.attr,
758         &lustre_attr_cur_dirty_grant_bytes.attr,
759         NULL,
760 };
761
762 int mdc_tunables_init(struct obd_device *obd)
763 {
764         int rc;
765
766         obd->obd_ktype.default_attrs = mdc_attrs;
767         obd->obd_vars = lprocfs_mdc_obd_vars;
768
769         rc = lprocfs_obd_setup(obd, false);
770         if (rc)
771                 goto out_failed;
772 #ifdef CONFIG_PROC_FS
773         rc = lprocfs_alloc_md_stats(obd, 0);
774         if (rc) {
775                 lprocfs_obd_cleanup(obd);
776                 goto out_failed;
777         }
778 #endif
779         rc = sptlrpc_lprocfs_cliobd_attach(obd);
780         if (rc) {
781 #ifdef CONFIG_PROC_FS
782                 lprocfs_free_md_stats(obd);
783 #endif
784                 lprocfs_obd_cleanup(obd);
785                 goto out_failed;
786         }
787         ptlrpc_lprocfs_register_obd(obd);
788
789 out_failed:
790         return rc;
791 }