Whamcloud - gitweb
LU-9091 sysfs: use string helper like functions for sysfs
[fs/lustre-release.git] / lustre / osc / lproc_osc.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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <linux/version.h>
35 #include <asm/statfs.h>
36 #include <obd_cksum.h>
37 #include <obd_class.h>
38 #include <lprocfs_status.h>
39 #include <linux/seq_file.h>
40 #include <lustre_osc.h>
41
42 #include "osc_internal.h"
43
44 static ssize_t active_show(struct kobject *kobj, struct attribute *attr,
45                            char *buf)
46 {
47         struct obd_device *dev = container_of(kobj, struct obd_device,
48                                               obd_kset.kobj);
49         int rc;
50
51         LPROCFS_CLIMP_CHECK(dev);
52         rc = sprintf(buf, "%d\n", !dev->u.cli.cl_import->imp_deactive);
53         LPROCFS_CLIMP_EXIT(dev);
54         return rc;
55 }
56
57 static ssize_t active_store(struct kobject *kobj, struct attribute *attr,
58                             const char *buffer, size_t count)
59 {
60         struct obd_device *dev = container_of(kobj, struct obd_device,
61                                               obd_kset.kobj);
62         bool val;
63         int rc;
64
65         rc = kstrtobool(buffer, &val);
66         if (rc)
67                 return rc;
68
69         /* opposite senses */
70         if (dev->u.cli.cl_import->imp_deactive == val)
71                 rc = ptlrpc_set_import_active(dev->u.cli.cl_import, val);
72         else
73                 CDEBUG(D_CONFIG, "activate %u: ignoring repeat request\n",
74                        (unsigned int)val);
75
76         return count;
77 }
78 LUSTRE_RW_ATTR(active);
79
80 static ssize_t max_rpcs_in_flight_show(struct kobject *kobj,
81                                        struct attribute *attr,
82                                        char *buf)
83 {
84         struct obd_device *dev = container_of(kobj, struct obd_device,
85                                               obd_kset.kobj);
86         struct client_obd *cli = &dev->u.cli;
87         ssize_t len;
88
89         spin_lock(&cli->cl_loi_list_lock);
90         len = sprintf(buf, "%u\n", cli->cl_max_rpcs_in_flight);
91         spin_unlock(&cli->cl_loi_list_lock);
92         return len;
93 }
94
95 static ssize_t max_rpcs_in_flight_store(struct kobject *kobj,
96                                         struct attribute *attr,
97                                         const char *buffer,
98                                         size_t count)
99 {
100         struct obd_device *dev = container_of(kobj, struct obd_device,
101                                               obd_kset.kobj);
102         struct client_obd *cli = &dev->u.cli;
103         int adding, added, req_count;
104         unsigned int val;
105         int rc;
106
107         rc = kstrtouint(buffer, 0, &val);
108         if (rc)
109                 return rc;
110
111         if (val == 0 || val > OSC_MAX_RIF_MAX)
112                 return -ERANGE;
113
114         LPROCFS_CLIMP_CHECK(dev);
115
116         adding = (int)val - cli->cl_max_rpcs_in_flight;
117         req_count = atomic_read(&osc_pool_req_count);
118         if (adding > 0 && req_count < osc_reqpool_maxreqcount) {
119                 /*
120                  * There might be some race which will cause over-limit
121                  * allocation, but it is fine.
122                  */
123                 if (req_count + adding > osc_reqpool_maxreqcount)
124                         adding = osc_reqpool_maxreqcount - req_count;
125
126                 added = osc_rq_pool->prp_populate(osc_rq_pool, adding);
127                 atomic_add(added, &osc_pool_req_count);
128         }
129
130         spin_lock(&cli->cl_loi_list_lock);
131         cli->cl_max_rpcs_in_flight = val;
132         client_adjust_max_dirty(cli);
133         spin_unlock(&cli->cl_loi_list_lock);
134
135         LPROCFS_CLIMP_EXIT(dev);
136         return count;
137 }
138 LUSTRE_RW_ATTR(max_rpcs_in_flight);
139
140 static ssize_t max_dirty_mb_show(struct kobject *kobj,
141                                  struct attribute *attr,
142                                  char *buf)
143 {
144         struct obd_device *dev = container_of(kobj, struct obd_device,
145                                               obd_kset.kobj);
146         struct client_obd *cli = &dev->u.cli;
147         unsigned long val;
148
149         spin_lock(&cli->cl_loi_list_lock);
150         val = PAGES_TO_MiB(cli->cl_dirty_max_pages);
151         spin_unlock(&cli->cl_loi_list_lock);
152
153         return sprintf(buf, "%lu\n", val);
154 }
155
156 static ssize_t max_dirty_mb_store(struct kobject *kobj,
157                                   struct attribute *attr,
158                                   const char *buffer,
159                                   size_t count)
160 {
161         struct obd_device *dev = container_of(kobj, struct obd_device,
162                                               obd_kset.kobj);
163         struct client_obd *cli = &dev->u.cli;
164         unsigned long pages_number, max_dirty_mb;
165         int rc;
166
167         rc = kstrtoul(buffer, 10, &max_dirty_mb);
168         if (rc)
169                 return rc;
170
171         pages_number = MiB_TO_PAGES(max_dirty_mb);
172
173         if (pages_number >= MiB_TO_PAGES(OSC_MAX_DIRTY_MB_MAX) ||
174             pages_number > cfs_totalram_pages() / 4) /* 1/4 of RAM */
175                 return -ERANGE;
176
177         spin_lock(&cli->cl_loi_list_lock);
178         cli->cl_dirty_max_pages = pages_number;
179         osc_wake_cache_waiters(cli);
180         spin_unlock(&cli->cl_loi_list_lock);
181
182         return count;
183 }
184 LUSTRE_RW_ATTR(max_dirty_mb);
185
186 LUSTRE_ATTR(ost_conn_uuid, 0444, conn_uuid_show, NULL);
187 LUSTRE_RO_ATTR(conn_uuid);
188
189 LUSTRE_RW_ATTR(ping);
190
191 static int osc_cached_mb_seq_show(struct seq_file *m, void *v)
192 {
193         struct obd_device *dev = m->private;
194         struct client_obd *cli = &dev->u.cli;
195         int shift = 20 - PAGE_SHIFT;
196
197         seq_printf(m, "used_mb: %ld\n"
198                    "busy_cnt: %ld\n"
199                    "reclaim: %llu\n",
200                    (atomic_long_read(&cli->cl_lru_in_list) +
201                     atomic_long_read(&cli->cl_lru_busy)) >> shift,
202                     atomic_long_read(&cli->cl_lru_busy),
203                    cli->cl_lru_reclaim);
204
205         return 0;
206 }
207
208 /* shrink the number of caching pages to a specific number */
209 static ssize_t osc_cached_mb_seq_write(struct file *file,
210                                        const char __user *buffer,
211                                        size_t count, loff_t *off)
212 {
213         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
214         struct client_obd *cli = &dev->u.cli;
215         u64 pages_number;
216         const char *tmp;
217         long rc;
218         char kernbuf[128];
219
220         if (count >= sizeof(kernbuf))
221                 return -EINVAL;
222
223         if (copy_from_user(kernbuf, buffer, count))
224                 return -EFAULT;
225         kernbuf[count] = 0;
226
227         tmp = lprocfs_find_named_value(kernbuf, "used_mb:", &count);
228         rc = sysfs_memparse(tmp, count, &pages_number, "MiB");
229         if (rc < 0)
230                 return rc;
231
232         pages_number >>= PAGE_SHIFT;
233
234         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
235         if (rc > 0) {
236                 struct lu_env *env;
237                 __u16 refcheck;
238
239                 env = cl_env_get(&refcheck);
240                 if (!IS_ERR(env)) {
241                         (void)osc_lru_shrink(env, cli, rc, true);
242                         cl_env_put(env, &refcheck);
243                 }
244         }
245
246         return count;
247 }
248
249 LPROC_SEQ_FOPS(osc_cached_mb);
250
251 static ssize_t cur_dirty_bytes_show(struct kobject *kobj,
252                                     struct attribute *attr,
253                                     char *buf)
254 {
255         struct obd_device *dev = container_of(kobj, struct obd_device,
256                                               obd_kset.kobj);
257         struct client_obd *cli = &dev->u.cli;
258         ssize_t len;
259
260         spin_lock(&cli->cl_loi_list_lock);
261         len = sprintf(buf, "%lu\n", cli->cl_dirty_pages << PAGE_SHIFT);
262         spin_unlock(&cli->cl_loi_list_lock);
263
264         return len;
265 }
266 LUSTRE_RO_ATTR(cur_dirty_bytes);
267
268 static int osc_cur_grant_bytes_seq_show(struct seq_file *m, void *v)
269 {
270         struct obd_device *dev = m->private;
271         struct client_obd *cli = &dev->u.cli;
272
273         spin_lock(&cli->cl_loi_list_lock);
274         seq_printf(m, "%lu\n", cli->cl_avail_grant);
275         spin_unlock(&cli->cl_loi_list_lock);
276         return 0;
277 }
278
279 static ssize_t osc_cur_grant_bytes_seq_write(struct file *file,
280                                              const char __user *buffer,
281                                              size_t count, loff_t *off)
282 {
283         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
284         struct client_obd *cli = &obd->u.cli;
285         char kernbuf[22] = "";
286         u64 val;
287         int rc;
288
289         if (obd == NULL)
290                 return 0;
291
292         if (count >= sizeof(kernbuf))
293                 return -EINVAL;
294
295         if (copy_from_user(kernbuf, buffer, count))
296                 return -EFAULT;
297         kernbuf[count] = 0;
298
299         rc = sysfs_memparse(kernbuf, count, &val, "MiB");
300         if (rc < 0)
301                 return rc;
302
303         /* this is only for shrinking grant */
304         spin_lock(&cli->cl_loi_list_lock);
305         if (val >= cli->cl_avail_grant) {
306                 spin_unlock(&cli->cl_loi_list_lock);
307                 return 0;
308         }
309
310         spin_unlock(&cli->cl_loi_list_lock);
311
312         LPROCFS_CLIMP_CHECK(obd);
313         if (cli->cl_import->imp_state == LUSTRE_IMP_FULL)
314                 rc = osc_shrink_grant_to_target(cli, val);
315         LPROCFS_CLIMP_EXIT(obd);
316
317         return rc ? rc : count;
318 }
319 LPROC_SEQ_FOPS(osc_cur_grant_bytes);
320
321 static ssize_t cur_lost_grant_bytes_show(struct kobject *kobj,
322                                          struct attribute *attr,
323                                          char *buf)
324 {
325         struct obd_device *dev = container_of(kobj, struct obd_device,
326                                               obd_kset.kobj);
327         struct client_obd *cli = &dev->u.cli;
328         ssize_t len;
329
330         spin_lock(&cli->cl_loi_list_lock);
331         len = sprintf(buf, "%lu\n", cli->cl_lost_grant);
332         spin_unlock(&cli->cl_loi_list_lock);
333         return len;
334 }
335 LUSTRE_RO_ATTR(cur_lost_grant_bytes);
336
337 static ssize_t grant_shrink_interval_show(struct kobject *kobj,
338                                           struct attribute *attr,
339                                           char *buf)
340 {
341         struct obd_device *obd = container_of(kobj, struct obd_device,
342                                               obd_kset.kobj);
343
344         return sprintf(buf, "%lld\n", obd->u.cli.cl_grant_shrink_interval);
345 }
346
347 static ssize_t grant_shrink_interval_store(struct kobject *kobj,
348                                            struct attribute *attr,
349                                            const char *buffer,
350                                            size_t count)
351 {
352         struct obd_device *obd = container_of(kobj, struct obd_device,
353                                               obd_kset.kobj);
354         unsigned int val;
355         int rc;
356
357         rc = kstrtouint(buffer, 0, &val);
358         if (rc)
359                 return rc;
360
361         if (val == 0)
362                 return -ERANGE;
363
364         obd->u.cli.cl_grant_shrink_interval = val;
365         osc_update_next_shrink(&obd->u.cli);
366         osc_schedule_grant_work();
367
368         return count;
369 }
370 LUSTRE_RW_ATTR(grant_shrink_interval);
371
372 static ssize_t checksums_show(struct kobject *kobj,
373                               struct attribute *attr,
374                               char *buf)
375 {
376         struct obd_device *obd = container_of(kobj, struct obd_device,
377                                               obd_kset.kobj);
378
379         return sprintf(buf, "%d\n", obd->u.cli.cl_checksum ? 1 : 0);
380 }
381
382 static ssize_t checksums_store(struct kobject *kobj,
383                                struct attribute *attr,
384                                const char *buffer,
385                                size_t count)
386 {
387         struct obd_device *obd = container_of(kobj, struct obd_device,
388                                               obd_kset.kobj);
389         bool val;
390         int rc;
391
392         rc = kstrtobool(buffer, &val);
393         if (rc)
394                 return rc;
395
396         obd->u.cli.cl_checksum = val;
397
398         return count;
399 }
400 LUSTRE_RW_ATTR(checksums);
401
402 static int osc_checksum_type_seq_show(struct seq_file *m, void *v)
403 {
404         struct obd_device *obd = m->private;
405         int i;
406         DECLARE_CKSUM_NAME;
407
408         if (obd == NULL)
409                 return 0;
410
411         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
412                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
413                         continue;
414                 if (obd->u.cli.cl_cksum_type == (1 << i))
415                         seq_printf(m, "[%s] ", cksum_name[i]);
416                 else
417                         seq_printf(m, "%s ", cksum_name[i]);
418         }
419         seq_printf(m, "\n");
420         return 0;
421 }
422
423 static ssize_t osc_checksum_type_seq_write(struct file *file,
424                                            const char __user *buffer,
425                                            size_t count, loff_t *off)
426 {
427         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
428         int i;
429         DECLARE_CKSUM_NAME;
430         char kernbuf[10];
431         int rc = -EINVAL;
432
433         if (obd == NULL)
434                 return 0;
435
436         if (count > sizeof(kernbuf) - 1)
437                 return -EINVAL;
438         if (copy_from_user(kernbuf, buffer, count))
439                 return -EFAULT;
440
441         if (count > 0 && kernbuf[count - 1] == '\n')
442                 kernbuf[count - 1] = '\0';
443         else
444                 kernbuf[count] = '\0';
445
446         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
447                 if (strcmp(kernbuf, cksum_name[i]) == 0) {
448                         obd->u.cli.cl_preferred_cksum_type = BIT(i);
449                         if (obd->u.cli.cl_supp_cksum_types & BIT(i)) {
450                                 obd->u.cli.cl_cksum_type = BIT(i);
451                                 rc = count;
452                         } else {
453                                 rc = -ENOTSUPP;
454                         }
455                         break;
456                 }
457         }
458         return rc;
459 }
460 LPROC_SEQ_FOPS(osc_checksum_type);
461
462 static ssize_t resend_count_show(struct kobject *kobj,
463                                  struct attribute *attr,
464                                  char *buf)
465 {
466         struct obd_device *obd = container_of(kobj, struct obd_device,
467                                               obd_kset.kobj);
468
469         return sprintf(buf, "%u\n", atomic_read(&obd->u.cli.cl_resends));
470 }
471
472 static ssize_t resend_count_store(struct kobject *kobj,
473                                   struct attribute *attr,
474                                   const char *buffer,
475                                   size_t count)
476 {
477         struct obd_device *obd = container_of(kobj, struct obd_device,
478                                               obd_kset.kobj);
479         unsigned int val;
480         int rc;
481
482         rc = kstrtouint(buffer, 10, &val);
483         if (rc)
484                 return rc;
485
486         atomic_set(&obd->u.cli.cl_resends, val);
487
488         return count;
489 }
490 LUSTRE_RW_ATTR(resend_count);
491
492 static ssize_t checksum_dump_show(struct kobject *kobj,
493                                   struct attribute *attr,
494                                   char *buf)
495 {
496         struct obd_device *obd = container_of(kobj, struct obd_device,
497                                               obd_kset.kobj);
498
499         return sprintf(buf, "%d\n", obd->u.cli.cl_checksum_dump ? 1 : 0);
500 }
501
502 static ssize_t checksum_dump_store(struct kobject *kobj,
503                                    struct attribute *attr,
504                                    const char *buffer,
505                                    size_t count)
506 {
507         struct obd_device *obd = container_of(kobj, struct obd_device,
508                                               obd_kset.kobj);
509         bool val;
510         int rc;
511
512         rc = kstrtobool(buffer, &val);
513         if (rc)
514                 return rc;
515
516         obd->u.cli.cl_checksum_dump = val;
517
518         return count;
519 }
520 LUSTRE_RW_ATTR(checksum_dump);
521
522 static ssize_t contention_seconds_show(struct kobject *kobj,
523                                        struct attribute *attr,
524                                        char *buf)
525 {
526         struct obd_device *obd = container_of(kobj, struct obd_device,
527                                               obd_kset.kobj);
528         struct osc_device *od = obd2osc_dev(obd);
529
530         return sprintf(buf, "%lld\n", od->od_contention_time);
531 }
532
533 static ssize_t contention_seconds_store(struct kobject *kobj,
534                                         struct attribute *attr,
535                                         const char *buffer,
536                                         size_t count)
537 {
538         struct obd_device *obd = container_of(kobj, struct obd_device,
539                                               obd_kset.kobj);
540         struct osc_device *od = obd2osc_dev(obd);
541         unsigned int val;
542         int rc;
543
544         rc = kstrtouint(buffer, 0, &val);
545         if (rc)
546                 return rc;
547
548         od->od_contention_time = val;
549
550         return count;
551 }
552 LUSTRE_RW_ATTR(contention_seconds);
553
554 static ssize_t lockless_truncate_show(struct kobject *kobj,
555                                       struct attribute *attr,
556                                       char *buf)
557 {
558         struct obd_device *obd = container_of(kobj, struct obd_device,
559                                               obd_kset.kobj);
560         struct osc_device *od = obd2osc_dev(obd);
561
562         return sprintf(buf, "%u\n", od->od_lockless_truncate);
563 }
564
565 static ssize_t lockless_truncate_store(struct kobject *kobj,
566                                        struct attribute *attr,
567                                        const char *buffer,
568                                        size_t count)
569 {
570         struct obd_device *obd = container_of(kobj, struct obd_device,
571                                               obd_kset.kobj);
572         struct osc_device *od = obd2osc_dev(obd);
573         bool val;
574         int rc;
575
576         rc = kstrtobool(buffer, &val);
577         if (rc)
578                 return rc;
579
580         od->od_lockless_truncate = val;
581
582         return count;
583 }
584 LUSTRE_RW_ATTR(lockless_truncate);
585
586 static ssize_t destroys_in_flight_show(struct kobject *kobj,
587                                        struct attribute *attr,
588                                        char *buf)
589 {
590         struct obd_device *obd = container_of(kobj, struct obd_device,
591                                               obd_kset.kobj);
592
593         return sprintf(buf, "%u\n",
594                        atomic_read(&obd->u.cli.cl_destroy_in_flight));
595 }
596 LUSTRE_RO_ATTR(destroys_in_flight);
597
598 LPROC_SEQ_FOPS_RW_TYPE(osc, obd_max_pages_per_rpc);
599
600 LUSTRE_RW_ATTR(short_io_bytes);
601
602 #ifdef CONFIG_PROC_FS
603 static int osc_unstable_stats_seq_show(struct seq_file *m, void *v)
604 {
605         struct obd_device *dev = m->private;
606         struct client_obd *cli = &dev->u.cli;
607         long pages;
608         int mb;
609
610         pages = atomic_long_read(&cli->cl_unstable_count);
611         mb    = (pages * PAGE_SIZE) >> 20;
612
613         seq_printf(m, "unstable_pages: %20ld\n"
614                    "unstable_mb:              %10d\n",
615                    pages, mb);
616         return 0;
617 }
618 LPROC_SEQ_FOPS_RO(osc_unstable_stats);
619
620 static ssize_t idle_timeout_show(struct kobject *kobj, struct attribute *attr,
621                                  char *buf)
622 {
623         struct obd_device *obd = container_of(kobj, struct obd_device,
624                                               obd_kset.kobj);
625         struct client_obd *cli = &obd->u.cli;
626         int ret;
627
628         LPROCFS_CLIMP_CHECK(obd);
629         ret = sprintf(buf, "%u\n", cli->cl_import->imp_idle_timeout);
630         LPROCFS_CLIMP_EXIT(obd);
631
632         return ret;
633 }
634
635 static ssize_t idle_timeout_store(struct kobject *kobj, struct attribute *attr,
636                                   const char *buffer, size_t count)
637 {
638         struct obd_device *dev = container_of(kobj, struct obd_device,
639                                               obd_kset.kobj);
640         struct client_obd *cli = &dev->u.cli;
641         struct ptlrpc_request *req;
642         unsigned int idle_debug = 0;
643         unsigned int val;
644         int rc;
645
646         if (strncmp(buffer, "debug", 5) == 0) {
647                 idle_debug = D_CONSOLE;
648         } else if (strncmp(buffer, "nodebug", 6) == 0) {
649                 idle_debug = D_HA;
650         } else {
651                 rc = kstrtouint(buffer, 10, &val);
652                 if (rc)
653                         return rc;
654
655                 if (val > CONNECTION_SWITCH_MAX)
656                         return -ERANGE;
657         }
658
659         LPROCFS_CLIMP_CHECK(dev);
660         if (idle_debug) {
661                 cli->cl_import->imp_idle_debug = idle_debug;
662         } else {
663                 if (!val) {
664                         /* initiate the connection if it's in IDLE state */
665                         req = ptlrpc_request_alloc(cli->cl_import,
666                                                    &RQF_OST_STATFS);
667                         if (req != NULL)
668                                 ptlrpc_req_finished(req);
669                 }
670                 cli->cl_import->imp_idle_timeout = val;
671         }
672         LPROCFS_CLIMP_EXIT(dev);
673
674         return count;
675 }
676 LUSTRE_RW_ATTR(idle_timeout);
677
678 static ssize_t idle_connect_store(struct kobject *kobj, struct attribute *attr,
679                                   const char *buffer, size_t count)
680 {
681         struct obd_device *dev = container_of(kobj, struct obd_device,
682                                               obd_kset.kobj);
683         struct client_obd *cli = &dev->u.cli;
684         struct ptlrpc_request *req;
685
686         LPROCFS_CLIMP_CHECK(dev);
687         /* to initiate the connection if it's in IDLE state */
688         req = ptlrpc_request_alloc(cli->cl_import, &RQF_OST_STATFS);
689         if (req)
690                 ptlrpc_req_finished(req);
691         ptlrpc_pinger_force(cli->cl_import);
692         LPROCFS_CLIMP_EXIT(dev);
693
694         return count;
695 }
696 LUSTRE_WO_ATTR(idle_connect);
697
698 static ssize_t grant_shrink_show(struct kobject *kobj, struct attribute *attr,
699                                  char *buf)
700 {
701         struct obd_device *obd = container_of(kobj, struct obd_device,
702                                               obd_kset.kobj);
703         struct obd_import *imp;
704         ssize_t len;
705
706         LPROCFS_CLIMP_CHECK(obd);
707         imp = obd->u.cli.cl_import;
708         len = scnprintf(buf, PAGE_SIZE, "%d\n",
709                         !imp->imp_grant_shrink_disabled &&
710                         OCD_HAS_FLAG(&imp->imp_connect_data, GRANT_SHRINK));
711         LPROCFS_CLIMP_EXIT(obd);
712
713         return len;
714 }
715
716 static ssize_t grant_shrink_store(struct kobject *kobj, struct attribute *attr,
717                                   const char *buffer, size_t count)
718 {
719         struct obd_device *dev = container_of(kobj, struct obd_device,
720                                               obd_kset.kobj);
721         struct obd_import *imp;
722         bool val;
723         int rc;
724
725         if (dev == NULL)
726                 return 0;
727
728         rc = kstrtobool(buffer, &val);
729         if (rc)
730                 return rc;
731
732         LPROCFS_CLIMP_CHECK(dev);
733
734         imp = dev->u.cli.cl_import;
735         spin_lock(&imp->imp_lock);
736         imp->imp_grant_shrink_disabled = !val;
737         spin_unlock(&imp->imp_lock);
738
739         LPROCFS_CLIMP_EXIT(dev);
740
741         return count;
742 }
743 LUSTRE_RW_ATTR(grant_shrink);
744
745 LPROC_SEQ_FOPS_RO_TYPE(osc, connect_flags);
746 LPROC_SEQ_FOPS_RO_TYPE(osc, server_uuid);
747 LPROC_SEQ_FOPS_RO_TYPE(osc, timeouts);
748 LPROC_SEQ_FOPS_RO_TYPE(osc, state);
749
750 LPROC_SEQ_FOPS_RW_TYPE(osc, import);
751 LPROC_SEQ_FOPS_RW_TYPE(osc, pinger_recov);
752
753 struct lprocfs_vars lprocfs_osc_obd_vars[] = {
754         { .name =       "connect_flags",
755           .fops =       &osc_connect_flags_fops         },
756         { .name =       "ost_server_uuid",
757           .fops =       &osc_server_uuid_fops           },
758         { .name =       "max_pages_per_rpc",
759           .fops =       &osc_obd_max_pages_per_rpc_fops },
760         { .name =       "osc_cached_mb",
761           .fops =       &osc_cached_mb_fops             },
762         { .name =       "cur_grant_bytes",
763           .fops =       &osc_cur_grant_bytes_fops       },
764         { .name =       "checksum_type",
765           .fops =       &osc_checksum_type_fops         },
766         { .name =       "timeouts",
767           .fops =       &osc_timeouts_fops              },
768         { .name =       "import",
769           .fops =       &osc_import_fops                },
770         { .name =       "state",
771           .fops =       &osc_state_fops                 },
772         { .name =       "pinger_recov",
773           .fops =       &osc_pinger_recov_fops          },
774         { .name =       "unstable_stats",
775           .fops =       &osc_unstable_stats_fops        },
776         { NULL }
777 };
778
779 static int osc_rpc_stats_seq_show(struct seq_file *seq, void *v)
780 {
781         struct timespec64 now;
782         struct obd_device *dev = seq->private;
783         struct client_obd *cli = &dev->u.cli;
784         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
785         int i;
786
787         ktime_get_real_ts64(&now);
788
789         spin_lock(&cli->cl_loi_list_lock);
790
791         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
792                    (s64)now.tv_sec, now.tv_nsec);
793         seq_printf(seq, "read RPCs in flight:  %d\n",
794                    cli->cl_r_in_flight);
795         seq_printf(seq, "write RPCs in flight: %d\n",
796                    cli->cl_w_in_flight);
797         seq_printf(seq, "pending write pages:  %d\n",
798                    atomic_read(&cli->cl_pending_w_pages));
799         seq_printf(seq, "pending read pages:   %d\n",
800                    atomic_read(&cli->cl_pending_r_pages));
801
802         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
803         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
804         seq_printf(seq, "       rpcs   %% cum %%\n");
805
806         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
807         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
808
809         read_cum = 0;
810         write_cum = 0;
811         for (i = 0; i < OBD_HIST_MAX; i++) {
812                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
813                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
814
815                 read_cum += r;
816                 write_cum += w;
817                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
818                            1 << i, r, pct(r, read_tot),
819                            pct(read_cum, read_tot), w,
820                            pct(w, write_tot),
821                            pct(write_cum, write_tot));
822                 if (read_cum == read_tot && write_cum == write_tot)
823                         break;
824         }
825
826         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
827         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
828         seq_printf(seq, "       rpcs   %% cum %%\n");
829
830         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
831         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
832
833         read_cum = 0;
834         write_cum = 0;
835         for (i = 0; i < OBD_HIST_MAX; i++) {
836                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
837                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
838                 read_cum += r;
839                 write_cum += w;
840                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
841                            i, r, pct(r, read_tot),
842                            pct(read_cum, read_tot), w,
843                            pct(w, write_tot),
844                            pct(write_cum, write_tot));
845                 if (read_cum == read_tot && write_cum == write_tot)
846                         break;
847         }
848
849         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
850         seq_printf(seq, "offset                rpcs   %% cum %% |");
851         seq_printf(seq, "       rpcs   %% cum %%\n");
852
853         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
854         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
855
856         read_cum = 0;
857         write_cum = 0;
858         for (i = 0; i < OBD_HIST_MAX; i++) {
859                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
860                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
861                 read_cum += r;
862                 write_cum += w;
863                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
864                            (i == 0) ? 0 : 1 << (i - 1),
865                            r, pct(r, read_tot), pct(read_cum, read_tot),
866                            w, pct(w, write_tot), pct(write_cum, write_tot));
867                 if (read_cum == read_tot && write_cum == write_tot)
868                         break;
869         }
870
871         spin_unlock(&cli->cl_loi_list_lock);
872
873         return 0;
874 }
875
876 static ssize_t osc_rpc_stats_seq_write(struct file *file,
877                                        const char __user *buf,
878                                        size_t len, loff_t *off)
879 {
880         struct seq_file *seq = file->private_data;
881         struct obd_device *dev = seq->private;
882         struct client_obd *cli = &dev->u.cli;
883
884         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
885         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
886         lprocfs_oh_clear(&cli->cl_read_page_hist);
887         lprocfs_oh_clear(&cli->cl_write_page_hist);
888         lprocfs_oh_clear(&cli->cl_read_offset_hist);
889         lprocfs_oh_clear(&cli->cl_write_offset_hist);
890
891         return len;
892 }
893 LPROC_SEQ_FOPS(osc_rpc_stats);
894
895 static int osc_stats_seq_show(struct seq_file *seq, void *v)
896 {
897         struct timespec64 now;
898         struct obd_device *dev = seq->private;
899         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
900
901         ktime_get_real_ts64(&now);
902
903         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
904                    (s64)now.tv_sec, now.tv_nsec);
905         seq_printf(seq, "lockless_write_bytes\t\t%llu\n",
906                    stats->os_lockless_writes);
907         seq_printf(seq, "lockless_read_bytes\t\t%llu\n",
908                    stats->os_lockless_reads);
909         seq_printf(seq, "lockless_truncate\t\t%llu\n",
910                    stats->os_lockless_truncates);
911         return 0;
912 }
913
914 static ssize_t osc_stats_seq_write(struct file *file,
915                                    const char __user *buf,
916                                    size_t len, loff_t *off)
917 {
918         struct seq_file *seq = file->private_data;
919         struct obd_device *dev = seq->private;
920         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
921
922         memset(stats, 0, sizeof(*stats));
923         return len;
924 }
925
926 LPROC_SEQ_FOPS(osc_stats);
927
928 int lprocfs_osc_attach_seqstat(struct obd_device *dev)
929 {
930         int rc;
931
932         rc = lprocfs_seq_create(dev->obd_proc_entry, "osc_stats", 0644,
933                                 &osc_stats_fops, dev);
934         if (rc == 0)
935                 rc = lprocfs_obd_seq_create(dev, "rpc_stats", 0644,
936                                             &osc_rpc_stats_fops, dev);
937
938         return rc;
939 }
940 #endif /* CONFIG_PROC_FS */
941
942 static struct attribute *osc_attrs[] = {
943         &lustre_attr_active.attr,
944         &lustre_attr_checksums.attr,
945         &lustre_attr_checksum_dump.attr,
946         &lustre_attr_contention_seconds.attr,
947         &lustre_attr_cur_dirty_bytes.attr,
948         &lustre_attr_cur_lost_grant_bytes.attr,
949         &lustre_attr_destroys_in_flight.attr,
950         &lustre_attr_grant_shrink_interval.attr,
951         &lustre_attr_lockless_truncate.attr,
952         &lustre_attr_max_dirty_mb.attr,
953         &lustre_attr_max_rpcs_in_flight.attr,
954         &lustre_attr_short_io_bytes.attr,
955         &lustre_attr_resend_count.attr,
956         &lustre_attr_ost_conn_uuid.attr,
957         &lustre_attr_conn_uuid.attr,
958         &lustre_attr_ping.attr,
959         &lustre_attr_idle_timeout.attr,
960         &lustre_attr_idle_connect.attr,
961         &lustre_attr_grant_shrink.attr,
962         NULL,
963 };
964
965 int osc_tunables_init(struct obd_device *obd)
966 {
967         int rc;
968
969         obd->obd_vars = lprocfs_osc_obd_vars;
970         obd->obd_ktype.default_attrs = osc_attrs;
971         rc = lprocfs_obd_setup(obd, false);
972         if (rc)
973                 return rc;
974 #ifdef CONFIG_PROC_FS
975         /* If the basic OSC proc tree construction succeeded then
976          * lets do the rest.
977          */
978         rc = lprocfs_osc_attach_seqstat(obd);
979         if (rc)
980                 goto obd_cleanup;
981
982 #endif /* CONFIG_PROC_FS */
983         rc = sptlrpc_lprocfs_cliobd_attach(obd);
984         if (rc)
985                 goto obd_cleanup;
986
987         ptlrpc_lprocfs_register_obd(obd);
988 obd_cleanup:
989         if (rc)
990                 lprocfs_obd_cleanup(obd);
991         return rc;
992 }