Whamcloud - gitweb
LU-12616 obclass: fix MDS start/stop race
[fs/lustre-release.git] / lustre / osc / lproc_osc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <linux/version.h>
35 #include <asm/statfs.h>
36 #include <obd_cksum.h>
37 #include <obd_class.h>
38 #include <lprocfs_status.h>
39 #include <linux/seq_file.h>
40 #include <lustre_osc.h>
41
42 #include "osc_internal.h"
43
44 static ssize_t active_show(struct kobject *kobj, struct attribute *attr,
45                            char *buf)
46 {
47         struct obd_device *dev = container_of(kobj, struct obd_device,
48                                               obd_kset.kobj);
49         int rc;
50
51         LPROCFS_CLIMP_CHECK(dev);
52         rc = sprintf(buf, "%d\n", !dev->u.cli.cl_import->imp_deactive);
53         LPROCFS_CLIMP_EXIT(dev);
54         return rc;
55 }
56
57 static ssize_t active_store(struct kobject *kobj, struct attribute *attr,
58                             const char *buffer, size_t count)
59 {
60         struct obd_device *dev = container_of(kobj, struct obd_device,
61                                               obd_kset.kobj);
62         bool val;
63         int rc;
64
65         rc = kstrtobool(buffer, &val);
66         if (rc)
67                 return rc;
68
69         /* opposite senses */
70         if (dev->u.cli.cl_import->imp_deactive == val)
71                 rc = ptlrpc_set_import_active(dev->u.cli.cl_import, val);
72         else
73                 CDEBUG(D_CONFIG, "activate %u: ignoring repeat request\n",
74                        (unsigned int)val);
75
76         return count;
77 }
78 LUSTRE_RW_ATTR(active);
79
80 static ssize_t max_rpcs_in_flight_show(struct kobject *kobj,
81                                        struct attribute *attr,
82                                        char *buf)
83 {
84         struct obd_device *dev = container_of(kobj, struct obd_device,
85                                               obd_kset.kobj);
86         struct client_obd *cli = &dev->u.cli;
87         ssize_t len;
88
89         spin_lock(&cli->cl_loi_list_lock);
90         len = sprintf(buf, "%u\n", cli->cl_max_rpcs_in_flight);
91         spin_unlock(&cli->cl_loi_list_lock);
92         return len;
93 }
94
95 static ssize_t max_rpcs_in_flight_store(struct kobject *kobj,
96                                         struct attribute *attr,
97                                         const char *buffer,
98                                         size_t count)
99 {
100         struct obd_device *dev = container_of(kobj, struct obd_device,
101                                               obd_kset.kobj);
102         struct client_obd *cli = &dev->u.cli;
103         int adding, added, req_count;
104         unsigned int val;
105         int rc;
106
107         rc = kstrtouint(buffer, 0, &val);
108         if (rc)
109                 return rc;
110
111         if (val == 0 || val > OSC_MAX_RIF_MAX)
112                 return -ERANGE;
113
114         LPROCFS_CLIMP_CHECK(dev);
115
116         adding = (int)val - cli->cl_max_rpcs_in_flight;
117         req_count = atomic_read(&osc_pool_req_count);
118         if (adding > 0 && req_count < osc_reqpool_maxreqcount) {
119                 /*
120                  * There might be some race which will cause over-limit
121                  * allocation, but it is fine.
122                  */
123                 if (req_count + adding > osc_reqpool_maxreqcount)
124                         adding = osc_reqpool_maxreqcount - req_count;
125
126                 added = osc_rq_pool->prp_populate(osc_rq_pool, adding);
127                 atomic_add(added, &osc_pool_req_count);
128         }
129
130         spin_lock(&cli->cl_loi_list_lock);
131         cli->cl_max_rpcs_in_flight = val;
132         client_adjust_max_dirty(cli);
133         spin_unlock(&cli->cl_loi_list_lock);
134
135         LPROCFS_CLIMP_EXIT(dev);
136         return count;
137 }
138 LUSTRE_RW_ATTR(max_rpcs_in_flight);
139
140 static ssize_t max_dirty_mb_show(struct kobject *kobj,
141                                  struct attribute *attr,
142                                  char *buf)
143 {
144         struct obd_device *dev = container_of(kobj, struct obd_device,
145                                               obd_kset.kobj);
146         struct client_obd *cli = &dev->u.cli;
147         unsigned long val;
148
149         spin_lock(&cli->cl_loi_list_lock);
150         val = PAGES_TO_MiB(cli->cl_dirty_max_pages);
151         spin_unlock(&cli->cl_loi_list_lock);
152
153         return sprintf(buf, "%lu\n", val);
154 }
155
156 static ssize_t max_dirty_mb_store(struct kobject *kobj,
157                                   struct attribute *attr,
158                                   const char *buffer,
159                                   size_t count)
160 {
161         struct obd_device *dev = container_of(kobj, struct obd_device,
162                                               obd_kset.kobj);
163         struct client_obd *cli = &dev->u.cli;
164         unsigned long pages_number, max_dirty_mb;
165         int rc;
166
167         rc = kstrtoul(buffer, 10, &max_dirty_mb);
168         if (rc)
169                 return rc;
170
171         pages_number = MiB_TO_PAGES(max_dirty_mb);
172
173         if (pages_number >= MiB_TO_PAGES(OSC_MAX_DIRTY_MB_MAX) ||
174             pages_number > cfs_totalram_pages() / 4) /* 1/4 of RAM */
175                 return -ERANGE;
176
177         spin_lock(&cli->cl_loi_list_lock);
178         cli->cl_dirty_max_pages = pages_number;
179         osc_wake_cache_waiters(cli);
180         spin_unlock(&cli->cl_loi_list_lock);
181
182         return count;
183 }
184 LUSTRE_RW_ATTR(max_dirty_mb);
185
186 LUSTRE_ATTR(ost_conn_uuid, 0444, conn_uuid_show, NULL);
187 LUSTRE_RO_ATTR(conn_uuid);
188
189 LUSTRE_RW_ATTR(ping);
190
191 static int osc_cached_mb_seq_show(struct seq_file *m, void *v)
192 {
193         struct obd_device *dev = m->private;
194         struct client_obd *cli = &dev->u.cli;
195         int shift = 20 - PAGE_SHIFT;
196
197         seq_printf(m, "used_mb: %ld\n"
198                    "busy_cnt: %ld\n"
199                    "reclaim: %llu\n",
200                    (atomic_long_read(&cli->cl_lru_in_list) +
201                     atomic_long_read(&cli->cl_lru_busy)) >> shift,
202                     atomic_long_read(&cli->cl_lru_busy),
203                    cli->cl_lru_reclaim);
204
205         return 0;
206 }
207
208 /* shrink the number of caching pages to a specific number */
209 static ssize_t osc_cached_mb_seq_write(struct file *file,
210                                        const char __user *buffer,
211                                        size_t count, loff_t *off)
212 {
213         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
214         struct client_obd *cli = &dev->u.cli;
215         __s64 pages_number;
216         long rc;
217         char kernbuf[128];
218
219         if (count >= sizeof(kernbuf))
220                 return -EINVAL;
221
222         if (copy_from_user(kernbuf, buffer, count))
223                 return -EFAULT;
224         kernbuf[count] = 0;
225
226         buffer += lprocfs_find_named_value(kernbuf, "used_mb:", &count) -
227                   kernbuf;
228         rc = lprocfs_str_with_units_to_s64(buffer, count, &pages_number, 'M');
229         if (rc)
230                 return rc;
231
232         pages_number >>= PAGE_SHIFT;
233
234         if (pages_number < 0)
235                 return -ERANGE;
236
237         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
238         if (rc > 0) {
239                 struct lu_env *env;
240                 __u16 refcheck;
241
242                 env = cl_env_get(&refcheck);
243                 if (!IS_ERR(env)) {
244                         (void)osc_lru_shrink(env, cli, rc, true);
245                         cl_env_put(env, &refcheck);
246                 }
247         }
248
249         return count;
250 }
251
252 LPROC_SEQ_FOPS(osc_cached_mb);
253
254 static ssize_t cur_dirty_bytes_show(struct kobject *kobj,
255                                     struct attribute *attr,
256                                     char *buf)
257 {
258         struct obd_device *dev = container_of(kobj, struct obd_device,
259                                               obd_kset.kobj);
260         struct client_obd *cli = &dev->u.cli;
261         ssize_t len;
262
263         spin_lock(&cli->cl_loi_list_lock);
264         len = sprintf(buf, "%lu\n", cli->cl_dirty_pages << PAGE_SHIFT);
265         spin_unlock(&cli->cl_loi_list_lock);
266
267         return len;
268 }
269 LUSTRE_RO_ATTR(cur_dirty_bytes);
270
271 static int osc_cur_grant_bytes_seq_show(struct seq_file *m, void *v)
272 {
273         struct obd_device *dev = m->private;
274         struct client_obd *cli = &dev->u.cli;
275
276         spin_lock(&cli->cl_loi_list_lock);
277         seq_printf(m, "%lu\n", cli->cl_avail_grant);
278         spin_unlock(&cli->cl_loi_list_lock);
279         return 0;
280 }
281
282 static ssize_t osc_cur_grant_bytes_seq_write(struct file *file,
283                                              const char __user *buffer,
284                                              size_t count, loff_t *off)
285 {
286         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
287         struct client_obd *cli = &obd->u.cli;
288         s64 val;
289         int rc;
290
291         if (obd == NULL)
292                 return 0;
293
294         rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
295         if (rc)
296                 return rc;
297         if (val < 0)
298                 return val;
299
300         /* this is only for shrinking grant */
301         spin_lock(&cli->cl_loi_list_lock);
302         if (val >= cli->cl_avail_grant) {
303                 spin_unlock(&cli->cl_loi_list_lock);
304                 return 0;
305         }
306
307         spin_unlock(&cli->cl_loi_list_lock);
308
309         LPROCFS_CLIMP_CHECK(obd);
310         if (cli->cl_import->imp_state == LUSTRE_IMP_FULL)
311                 rc = osc_shrink_grant_to_target(cli, val);
312         LPROCFS_CLIMP_EXIT(obd);
313
314         return rc ? rc : count;
315 }
316 LPROC_SEQ_FOPS(osc_cur_grant_bytes);
317
318 static ssize_t cur_lost_grant_bytes_show(struct kobject *kobj,
319                                          struct attribute *attr,
320                                          char *buf)
321 {
322         struct obd_device *dev = container_of(kobj, struct obd_device,
323                                               obd_kset.kobj);
324         struct client_obd *cli = &dev->u.cli;
325         ssize_t len;
326
327         spin_lock(&cli->cl_loi_list_lock);
328         len = sprintf(buf, "%lu\n", cli->cl_lost_grant);
329         spin_unlock(&cli->cl_loi_list_lock);
330         return len;
331 }
332 LUSTRE_RO_ATTR(cur_lost_grant_bytes);
333
334 static ssize_t grant_shrink_interval_show(struct kobject *kobj,
335                                           struct attribute *attr,
336                                           char *buf)
337 {
338         struct obd_device *obd = container_of(kobj, struct obd_device,
339                                               obd_kset.kobj);
340
341         return sprintf(buf, "%lld\n", obd->u.cli.cl_grant_shrink_interval);
342 }
343
344 static ssize_t grant_shrink_interval_store(struct kobject *kobj,
345                                            struct attribute *attr,
346                                            const char *buffer,
347                                            size_t count)
348 {
349         struct obd_device *obd = container_of(kobj, struct obd_device,
350                                               obd_kset.kobj);
351         unsigned int val;
352         int rc;
353
354         rc = kstrtouint(buffer, 0, &val);
355         if (rc)
356                 return rc;
357
358         if (val == 0)
359                 return -ERANGE;
360
361         obd->u.cli.cl_grant_shrink_interval = val;
362         osc_update_next_shrink(&obd->u.cli);
363         osc_schedule_grant_work();
364
365         return count;
366 }
367 LUSTRE_RW_ATTR(grant_shrink_interval);
368
369 static ssize_t checksums_show(struct kobject *kobj,
370                               struct attribute *attr,
371                               char *buf)
372 {
373         struct obd_device *obd = container_of(kobj, struct obd_device,
374                                               obd_kset.kobj);
375
376         return sprintf(buf, "%d\n", obd->u.cli.cl_checksum ? 1 : 0);
377 }
378
379 static ssize_t checksums_store(struct kobject *kobj,
380                                struct attribute *attr,
381                                const char *buffer,
382                                size_t count)
383 {
384         struct obd_device *obd = container_of(kobj, struct obd_device,
385                                               obd_kset.kobj);
386         bool val;
387         int rc;
388
389         rc = kstrtobool(buffer, &val);
390         if (rc)
391                 return rc;
392
393         obd->u.cli.cl_checksum = val;
394
395         return count;
396 }
397 LUSTRE_RW_ATTR(checksums);
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         DECLARE_CKSUM_NAME;
404
405         if (obd == NULL)
406                 return 0;
407
408         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
409                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
410                         continue;
411                 if (obd->u.cli.cl_cksum_type == (1 << i))
412                         seq_printf(m, "[%s] ", cksum_name[i]);
413                 else
414                         seq_printf(m, "%s ", cksum_name[i]);
415         }
416         seq_printf(m, "\n");
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 obd_device *obd = ((struct seq_file *)file->private_data)->private;
425         int i;
426         DECLARE_CKSUM_NAME;
427         char kernbuf[10];
428         int rc = -EINVAL;
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 sprintf(buf, "%d\n", obd->u.cli.cl_checksum_dump ? 1 : 0);
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 *dev = m->private;
603         struct client_obd *cli = &dev->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 client_obd *cli = &obd->u.cli;
623         int ret;
624
625         LPROCFS_CLIMP_CHECK(obd);
626         ret = sprintf(buf, "%u\n", cli->cl_import->imp_idle_timeout);
627         LPROCFS_CLIMP_EXIT(obd);
628
629         return ret;
630 }
631
632 static ssize_t idle_timeout_store(struct kobject *kobj, struct attribute *attr,
633                                   const char *buffer, size_t count)
634 {
635         struct obd_device *dev = container_of(kobj, struct obd_device,
636                                               obd_kset.kobj);
637         struct client_obd *cli = &dev->u.cli;
638         struct ptlrpc_request *req;
639         unsigned int idle_debug = 0;
640         unsigned int val;
641         int rc;
642
643         if (strncmp(buffer, "debug", 5) == 0) {
644                 idle_debug = D_CONSOLE;
645         } else if (strncmp(buffer, "nodebug", 6) == 0) {
646                 idle_debug = D_HA;
647         } else {
648                 rc = kstrtouint(buffer, 10, &val);
649                 if (rc)
650                         return rc;
651
652                 if (val > CONNECTION_SWITCH_MAX)
653                         return -ERANGE;
654         }
655
656         LPROCFS_CLIMP_CHECK(dev);
657         if (idle_debug) {
658                 cli->cl_import->imp_idle_debug = idle_debug;
659         } else {
660                 if (!val) {
661                         /* initiate the connection if it's in IDLE state */
662                         req = ptlrpc_request_alloc(cli->cl_import,
663                                                    &RQF_OST_STATFS);
664                         if (req != NULL)
665                                 ptlrpc_req_finished(req);
666                 }
667                 cli->cl_import->imp_idle_timeout = val;
668         }
669         LPROCFS_CLIMP_EXIT(dev);
670
671         return count;
672 }
673 LUSTRE_RW_ATTR(idle_timeout);
674
675 static ssize_t idle_connect_store(struct kobject *kobj, struct attribute *attr,
676                                   const char *buffer, size_t count)
677 {
678         struct obd_device *dev = container_of(kobj, struct obd_device,
679                                               obd_kset.kobj);
680         struct client_obd *cli = &dev->u.cli;
681         struct ptlrpc_request *req;
682
683         LPROCFS_CLIMP_CHECK(dev);
684         /* to initiate the connection if it's in IDLE state */
685         req = ptlrpc_request_alloc(cli->cl_import, &RQF_OST_STATFS);
686         if (req)
687                 ptlrpc_req_finished(req);
688         ptlrpc_pinger_force(cli->cl_import);
689         LPROCFS_CLIMP_EXIT(dev);
690
691         return 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 client_obd *cli = &obd->u.cli;
701         struct obd_connect_data *ocd;
702         ssize_t len;
703
704         LPROCFS_CLIMP_CHECK(obd);
705         ocd = &cli->cl_import->imp_connect_data;
706
707         len = snprintf(buf, PAGE_SIZE, "%d\n",
708                        !!OCD_HAS_FLAG(ocd, GRANT_SHRINK));
709         LPROCFS_CLIMP_EXIT(obd);
710
711         return len;
712 }
713
714 static ssize_t grant_shrink_store(struct kobject *kobj, struct attribute *attr,
715                                   const char *buffer, size_t count)
716 {
717         struct obd_device *dev = container_of(kobj, struct obd_device,
718                                               obd_kset.kobj);
719         struct client_obd *cli = &dev->u.cli;
720         struct obd_connect_data *ocd;
721         bool val;
722         int rc;
723
724         if (dev == NULL)
725                 return 0;
726
727         rc = kstrtobool(buffer, &val);
728         if (rc)
729                 return rc;
730
731         LPROCFS_CLIMP_CHECK(dev);
732         ocd = &cli->cl_import->imp_connect_data;
733
734         if (!val) {
735                 if (OCD_HAS_FLAG(ocd, GRANT_SHRINK))
736                         ocd->ocd_connect_flags &= ~OBD_CONNECT_GRANT_SHRINK;
737         } else {
738                 /**
739                  * server replied obd_connect_data is always bigger, so
740                  * client's imp_connect_flags_orig are always supported
741                  * by the server
742                  */
743                 if (!OCD_HAS_FLAG(ocd, GRANT_SHRINK) &&
744                     cli->cl_import->imp_connect_flags_orig &
745                     OBD_CONNECT_GRANT_SHRINK)
746                         ocd->ocd_connect_flags |= OBD_CONNECT_GRANT_SHRINK;
747         }
748
749         LPROCFS_CLIMP_EXIT(dev);
750
751         return count;
752 }
753 LUSTRE_RW_ATTR(grant_shrink);
754
755 LPROC_SEQ_FOPS_RO_TYPE(osc, connect_flags);
756 LPROC_SEQ_FOPS_RO_TYPE(osc, server_uuid);
757 LPROC_SEQ_FOPS_RO_TYPE(osc, timeouts);
758 LPROC_SEQ_FOPS_RO_TYPE(osc, state);
759
760 LPROC_SEQ_FOPS_RW_TYPE(osc, import);
761 LPROC_SEQ_FOPS_RW_TYPE(osc, pinger_recov);
762
763 struct lprocfs_vars lprocfs_osc_obd_vars[] = {
764         { .name =       "connect_flags",
765           .fops =       &osc_connect_flags_fops         },
766         { .name =       "ost_server_uuid",
767           .fops =       &osc_server_uuid_fops           },
768         { .name =       "max_pages_per_rpc",
769           .fops =       &osc_obd_max_pages_per_rpc_fops },
770         { .name =       "osc_cached_mb",
771           .fops =       &osc_cached_mb_fops             },
772         { .name =       "cur_grant_bytes",
773           .fops =       &osc_cur_grant_bytes_fops       },
774         { .name =       "checksum_type",
775           .fops =       &osc_checksum_type_fops         },
776         { .name =       "timeouts",
777           .fops =       &osc_timeouts_fops              },
778         { .name =       "import",
779           .fops =       &osc_import_fops                },
780         { .name =       "state",
781           .fops =       &osc_state_fops                 },
782         { .name =       "pinger_recov",
783           .fops =       &osc_pinger_recov_fops          },
784         { .name =       "unstable_stats",
785           .fops =       &osc_unstable_stats_fops        },
786         { NULL }
787 };
788
789 static int osc_rpc_stats_seq_show(struct seq_file *seq, void *v)
790 {
791         struct timespec64 now;
792         struct obd_device *dev = seq->private;
793         struct client_obd *cli = &dev->u.cli;
794         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
795         int i;
796
797         ktime_get_real_ts64(&now);
798
799         spin_lock(&cli->cl_loi_list_lock);
800
801         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
802                    (s64)now.tv_sec, now.tv_nsec);
803         seq_printf(seq, "read RPCs in flight:  %d\n",
804                    cli->cl_r_in_flight);
805         seq_printf(seq, "write RPCs in flight: %d\n",
806                    cli->cl_w_in_flight);
807         seq_printf(seq, "pending write pages:  %d\n",
808                    atomic_read(&cli->cl_pending_w_pages));
809         seq_printf(seq, "pending read pages:   %d\n",
810                    atomic_read(&cli->cl_pending_r_pages));
811
812         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
813         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
814         seq_printf(seq, "       rpcs   %% cum %%\n");
815
816         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
817         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
818
819         read_cum = 0;
820         write_cum = 0;
821         for (i = 0; i < OBD_HIST_MAX; i++) {
822                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
823                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
824
825                 read_cum += r;
826                 write_cum += w;
827                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
828                            1 << i, r, pct(r, read_tot),
829                            pct(read_cum, read_tot), w,
830                            pct(w, write_tot),
831                            pct(write_cum, write_tot));
832                 if (read_cum == read_tot && write_cum == write_tot)
833                         break;
834         }
835
836         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
837         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
838         seq_printf(seq, "       rpcs   %% cum %%\n");
839
840         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
841         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
842
843         read_cum = 0;
844         write_cum = 0;
845         for (i = 0; i < OBD_HIST_MAX; i++) {
846                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
847                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
848                 read_cum += r;
849                 write_cum += w;
850                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
851                            i, r, pct(r, read_tot),
852                            pct(read_cum, read_tot), w,
853                            pct(w, write_tot),
854                            pct(write_cum, write_tot));
855                 if (read_cum == read_tot && write_cum == write_tot)
856                         break;
857         }
858
859         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
860         seq_printf(seq, "offset                rpcs   %% cum %% |");
861         seq_printf(seq, "       rpcs   %% cum %%\n");
862
863         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
864         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
865
866         read_cum = 0;
867         write_cum = 0;
868         for (i = 0; i < OBD_HIST_MAX; i++) {
869                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
870                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
871                 read_cum += r;
872                 write_cum += w;
873                 seq_printf(seq, "%d:\t\t%10lu %3u %3u   | %10lu %3u %3u\n",
874                            (i == 0) ? 0 : 1 << (i - 1),
875                            r, pct(r, read_tot), pct(read_cum, read_tot),
876                            w, pct(w, write_tot), pct(write_cum, write_tot));
877                 if (read_cum == read_tot && write_cum == write_tot)
878                         break;
879         }
880
881         spin_unlock(&cli->cl_loi_list_lock);
882
883         return 0;
884 }
885
886 static ssize_t osc_rpc_stats_seq_write(struct file *file,
887                                        const char __user *buf,
888                                        size_t len, loff_t *off)
889 {
890         struct seq_file *seq = file->private_data;
891         struct obd_device *dev = seq->private;
892         struct client_obd *cli = &dev->u.cli;
893
894         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
895         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
896         lprocfs_oh_clear(&cli->cl_read_page_hist);
897         lprocfs_oh_clear(&cli->cl_write_page_hist);
898         lprocfs_oh_clear(&cli->cl_read_offset_hist);
899         lprocfs_oh_clear(&cli->cl_write_offset_hist);
900
901         return len;
902 }
903 LPROC_SEQ_FOPS(osc_rpc_stats);
904
905 static int osc_stats_seq_show(struct seq_file *seq, void *v)
906 {
907         struct timespec64 now;
908         struct obd_device *dev = seq->private;
909         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
910
911         ktime_get_real_ts64(&now);
912
913         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
914                    (s64)now.tv_sec, now.tv_nsec);
915         seq_printf(seq, "lockless_write_bytes\t\t%llu\n",
916                    stats->os_lockless_writes);
917         seq_printf(seq, "lockless_read_bytes\t\t%llu\n",
918                    stats->os_lockless_reads);
919         seq_printf(seq, "lockless_truncate\t\t%llu\n",
920                    stats->os_lockless_truncates);
921         return 0;
922 }
923
924 static ssize_t osc_stats_seq_write(struct file *file,
925                                    const char __user *buf,
926                                    size_t len, loff_t *off)
927 {
928         struct seq_file *seq = file->private_data;
929         struct obd_device *dev = seq->private;
930         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
931
932         memset(stats, 0, sizeof(*stats));
933         return len;
934 }
935
936 LPROC_SEQ_FOPS(osc_stats);
937
938 int lprocfs_osc_attach_seqstat(struct obd_device *dev)
939 {
940         int rc;
941
942         rc = lprocfs_seq_create(dev->obd_proc_entry, "osc_stats", 0644,
943                                 &osc_stats_fops, dev);
944         if (rc == 0)
945                 rc = lprocfs_obd_seq_create(dev, "rpc_stats", 0644,
946                                             &osc_rpc_stats_fops, dev);
947
948         return rc;
949 }
950 #endif /* CONFIG_PROC_FS */
951
952 static struct attribute *osc_attrs[] = {
953         &lustre_attr_active.attr,
954         &lustre_attr_checksums.attr,
955         &lustre_attr_checksum_dump.attr,
956         &lustre_attr_contention_seconds.attr,
957         &lustre_attr_cur_dirty_bytes.attr,
958         &lustre_attr_cur_lost_grant_bytes.attr,
959         &lustre_attr_destroys_in_flight.attr,
960         &lustre_attr_grant_shrink_interval.attr,
961         &lustre_attr_lockless_truncate.attr,
962         &lustre_attr_max_dirty_mb.attr,
963         &lustre_attr_max_rpcs_in_flight.attr,
964         &lustre_attr_short_io_bytes.attr,
965         &lustre_attr_resend_count.attr,
966         &lustre_attr_ost_conn_uuid.attr,
967         &lustre_attr_conn_uuid.attr,
968         &lustre_attr_ping.attr,
969         &lustre_attr_idle_timeout.attr,
970         &lustre_attr_idle_connect.attr,
971         &lustre_attr_grant_shrink.attr,
972         NULL,
973 };
974
975 int osc_tunables_init(struct obd_device *obd)
976 {
977         int rc;
978
979         obd->obd_vars = lprocfs_osc_obd_vars;
980         obd->obd_ktype.default_attrs = osc_attrs;
981         rc = lprocfs_obd_setup(obd, false);
982         if (rc)
983                 return rc;
984 #ifdef CONFIG_PROC_FS
985         /* If the basic OSC proc tree construction succeeded then
986          * lets do the rest.
987          */
988         rc = lprocfs_osc_attach_seqstat(obd);
989         if (rc)
990                 goto obd_cleanup;
991
992 #endif /* CONFIG_PROC_FS */
993         rc = sptlrpc_lprocfs_cliobd_attach(obd);
994         if (rc)
995                 goto obd_cleanup;
996
997         ptlrpc_lprocfs_register_obd(obd);
998 obd_cleanup:
999         if (rc)
1000                 lprocfs_obd_cleanup(obd);
1001         return rc;
1002 }