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