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