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