Whamcloud - gitweb
ba87e171d2bae16dbf2ff94c77052fb60d113dc3
[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         long val;
148         int mult;
149
150         spin_lock(&cli->cl_loi_list_lock);
151         val = cli->cl_dirty_max_pages;
152         spin_unlock(&cli->cl_loi_list_lock);
153
154         mult = 1 << (20 - PAGE_SHIFT);
155         return lprocfs_read_frac_helper(buf, PAGE_SIZE, val, mult);
156 }
157
158 static ssize_t max_dirty_mb_store(struct kobject *kobj,
159                                   struct attribute *attr,
160                                   const char *buffer,
161                                   size_t count)
162 {
163         struct obd_device *dev = container_of(kobj, struct obd_device,
164                                               obd_kset.kobj);
165         struct client_obd *cli = &dev->u.cli;
166         unsigned long pages_number;
167         int rc;
168
169         rc = kstrtoul(buffer, 10, &pages_number);
170         if (rc)
171                 return rc;
172
173         pages_number *= 1 << (20 - PAGE_SHIFT); /* MB -> pages */
174
175         if (pages_number <= 0 ||
176             pages_number >= OSC_MAX_DIRTY_MB_MAX << (20 - PAGE_SHIFT) ||
177             pages_number > totalram_pages / 4) /* 1/4 of RAM */
178                 return -ERANGE;
179
180         spin_lock(&cli->cl_loi_list_lock);
181         cli->cl_dirty_max_pages = pages_number;
182         osc_wake_cache_waiters(cli);
183         spin_unlock(&cli->cl_loi_list_lock);
184
185         return count;
186 }
187 LUSTRE_RW_ATTR(max_dirty_mb);
188
189 LUSTRE_RO_ATTR(conn_uuid);
190
191 LUSTRE_WO_ATTR(ping);
192
193 static int osc_cached_mb_seq_show(struct seq_file *m, void *v)
194 {
195         struct obd_device *dev = m->private;
196         struct client_obd *cli = &dev->u.cli;
197         int shift = 20 - PAGE_SHIFT;
198
199         seq_printf(m, "used_mb: %ld\n"
200                    "busy_cnt: %ld\n"
201                    "reclaim: %llu\n",
202                    (atomic_long_read(&cli->cl_lru_in_list) +
203                     atomic_long_read(&cli->cl_lru_busy)) >> shift,
204                     atomic_long_read(&cli->cl_lru_busy),
205                    cli->cl_lru_reclaim);
206
207         return 0;
208 }
209
210 /* shrink the number of caching pages to a specific number */
211 static ssize_t osc_cached_mb_seq_write(struct file *file,
212                                        const char __user *buffer,
213                                        size_t count, loff_t *off)
214 {
215         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
216         struct client_obd *cli = &dev->u.cli;
217         __s64 pages_number;
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         buffer += lprocfs_find_named_value(kernbuf, "used_mb:", &count) -
229                   kernbuf;
230         rc = lprocfs_str_with_units_to_s64(buffer, count, &pages_number, 'M');
231         if (rc)
232                 return rc;
233
234         pages_number >>= PAGE_SHIFT;
235
236         if (pages_number < 0)
237                 return -ERANGE;
238
239         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
240         if (rc > 0) {
241                 struct lu_env *env;
242                 __u16 refcheck;
243
244                 env = cl_env_get(&refcheck);
245                 if (!IS_ERR(env)) {
246                         (void)osc_lru_shrink(env, cli, rc, true);
247                         cl_env_put(env, &refcheck);
248                 }
249         }
250
251         return count;
252 }
253
254 LPROC_SEQ_FOPS(osc_cached_mb);
255
256 static ssize_t cur_dirty_bytes_show(struct kobject *kobj,
257                                     struct attribute *attr,
258                                     char *buf)
259 {
260         struct obd_device *dev = container_of(kobj, struct obd_device,
261                                               obd_kset.kobj);
262         struct client_obd *cli = &dev->u.cli;
263         ssize_t len;
264
265         spin_lock(&cli->cl_loi_list_lock);
266         len = sprintf(buf, "%lu\n", cli->cl_dirty_pages << PAGE_SHIFT);
267         spin_unlock(&cli->cl_loi_list_lock);
268
269         return len;
270 }
271 LUSTRE_RO_ATTR(cur_dirty_bytes);
272
273 static int osc_cur_grant_bytes_seq_show(struct seq_file *m, void *v)
274 {
275         struct obd_device *dev = m->private;
276         struct client_obd *cli = &dev->u.cli;
277
278         spin_lock(&cli->cl_loi_list_lock);
279         seq_printf(m, "%lu\n", cli->cl_avail_grant);
280         spin_unlock(&cli->cl_loi_list_lock);
281         return 0;
282 }
283
284 static ssize_t osc_cur_grant_bytes_seq_write(struct file *file,
285                                              const char __user *buffer,
286                                              size_t count, loff_t *off)
287 {
288         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
289         struct client_obd *cli = &obd->u.cli;
290         s64 val;
291         int rc;
292
293         if (obd == NULL)
294                 return 0;
295
296         rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
297         if (rc)
298                 return rc;
299         if (val < 0)
300                 return val;
301
302         /* this is only for shrinking grant */
303         spin_lock(&cli->cl_loi_list_lock);
304         if (val >= cli->cl_avail_grant) {
305                 spin_unlock(&cli->cl_loi_list_lock);
306                 return 0;
307         }
308
309         spin_unlock(&cli->cl_loi_list_lock);
310
311         LPROCFS_CLIMP_CHECK(obd);
312         if (cli->cl_import->imp_state == LUSTRE_IMP_FULL)
313                 rc = osc_shrink_grant_to_target(cli, val);
314         LPROCFS_CLIMP_EXIT(obd);
315
316         return rc ? rc : count;
317 }
318 LPROC_SEQ_FOPS(osc_cur_grant_bytes);
319
320 static ssize_t cur_lost_grant_bytes_show(struct kobject *kobj,
321                                          struct attribute *attr,
322                                          char *buf)
323 {
324         struct obd_device *dev = container_of(kobj, struct obd_device,
325                                               obd_kset.kobj);
326         struct client_obd *cli = &dev->u.cli;
327         ssize_t len;
328
329         spin_lock(&cli->cl_loi_list_lock);
330         len = sprintf(buf, "%lu\n", cli->cl_lost_grant);
331         spin_unlock(&cli->cl_loi_list_lock);
332         return len;
333 }
334 LUSTRE_RO_ATTR(cur_lost_grant_bytes);
335
336 static ssize_t grant_shrink_interval_show(struct kobject *kobj,
337                                           struct attribute *attr,
338                                           char *buf)
339 {
340         struct obd_device *obd = container_of(kobj, struct obd_device,
341                                               obd_kset.kobj);
342
343         return sprintf(buf, "%lld\n", obd->u.cli.cl_grant_shrink_interval);
344 }
345
346 static ssize_t grant_shrink_interval_store(struct kobject *kobj,
347                                            struct attribute *attr,
348                                            const char *buffer,
349                                            size_t count)
350 {
351         struct obd_device *obd = container_of(kobj, struct obd_device,
352                                               obd_kset.kobj);
353         unsigned int val;
354         int rc;
355
356         rc = kstrtouint(buffer, 0, &val);
357         if (rc)
358                 return rc;
359
360         if (val == 0)
361                 return -ERANGE;
362
363         obd->u.cli.cl_grant_shrink_interval = val;
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
429         if (obd == NULL)
430                 return 0;
431
432         if (count > sizeof(kernbuf) - 1)
433                 return -EINVAL;
434         if (copy_from_user(kernbuf, buffer, count))
435                 return -EFAULT;
436         if (count > 0 && kernbuf[count - 1] == '\n')
437                 kernbuf[count - 1] = '\0';
438         else
439                 kernbuf[count] = '\0';
440
441         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
442                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
443                         continue;
444                 if (!strcmp(kernbuf, cksum_name[i])) {
445                        obd->u.cli.cl_cksum_type = 1 << i;
446                        return count;
447                 }
448         }
449         return -EINVAL;
450 }
451 LPROC_SEQ_FOPS(osc_checksum_type);
452
453 static ssize_t resend_count_show(struct kobject *kobj,
454                                  struct attribute *attr,
455                                  char *buf)
456 {
457         struct obd_device *obd = container_of(kobj, struct obd_device,
458                                               obd_kset.kobj);
459
460         return sprintf(buf, "%u\n", atomic_read(&obd->u.cli.cl_resends));
461 }
462
463 static ssize_t resend_count_store(struct kobject *kobj,
464                                   struct attribute *attr,
465                                   const char *buffer,
466                                   size_t count)
467 {
468         struct obd_device *obd = container_of(kobj, struct obd_device,
469                                               obd_kset.kobj);
470         unsigned int val;
471         int rc;
472
473         rc = kstrtouint(buffer, 10, &val);
474         if (rc)
475                 return rc;
476
477         atomic_set(&obd->u.cli.cl_resends, val);
478
479         return count;
480 }
481 LUSTRE_RW_ATTR(resend_count);
482
483 static ssize_t checksum_dump_show(struct kobject *kobj,
484                                   struct attribute *attr,
485                                   char *buf)
486 {
487         struct obd_device *obd = container_of(kobj, struct obd_device,
488                                               obd_kset.kobj);
489
490         return sprintf(buf, "%d\n", obd->u.cli.cl_checksum_dump ? 1 : 0);
491 }
492
493 static ssize_t checksum_dump_store(struct kobject *kobj,
494                                    struct attribute *attr,
495                                    const char *buffer,
496                                    size_t count)
497 {
498         struct obd_device *obd = container_of(kobj, struct obd_device,
499                                               obd_kset.kobj);
500         bool val;
501         int rc;
502
503         rc = kstrtobool(buffer, &val);
504         if (rc)
505                 return rc;
506
507         obd->u.cli.cl_checksum_dump = val;
508
509         return count;
510 }
511 LUSTRE_RW_ATTR(checksum_dump);
512
513 static ssize_t contention_seconds_show(struct kobject *kobj,
514                                        struct attribute *attr,
515                                        char *buf)
516 {
517         struct obd_device *obd = container_of(kobj, struct obd_device,
518                                               obd_kset.kobj);
519         struct osc_device *od = obd2osc_dev(obd);
520
521         return sprintf(buf, "%lld\n", od->od_contention_time);
522 }
523
524 static ssize_t contention_seconds_store(struct kobject *kobj,
525                                         struct attribute *attr,
526                                         const char *buffer,
527                                         size_t count)
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         unsigned int val;
533         int rc;
534
535         rc = kstrtouint(buffer, 0, &val);
536         if (rc)
537                 return rc;
538
539         od->od_contention_time = val;
540
541         return count;
542 }
543 LUSTRE_RW_ATTR(contention_seconds);
544
545 static ssize_t lockless_truncate_show(struct kobject *kobj,
546                                       struct attribute *attr,
547                                       char *buf)
548 {
549         struct obd_device *obd = container_of(kobj, struct obd_device,
550                                               obd_kset.kobj);
551         struct osc_device *od = obd2osc_dev(obd);
552
553         return sprintf(buf, "%u\n", od->od_lockless_truncate);
554 }
555
556 static ssize_t lockless_truncate_store(struct kobject *kobj,
557                                        struct attribute *attr,
558                                        const char *buffer,
559                                        size_t count)
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         bool val;
565         int rc;
566
567         rc = kstrtobool(buffer, &val);
568         if (rc)
569                 return rc;
570
571         od->od_lockless_truncate = val;
572
573         return count;
574 }
575 LUSTRE_RW_ATTR(lockless_truncate);
576
577 static ssize_t destroys_in_flight_show(struct kobject *kobj,
578                                        struct attribute *attr,
579                                        char *buf)
580 {
581         struct obd_device *obd = container_of(kobj, struct obd_device,
582                                               obd_kset.kobj);
583
584         return sprintf(buf, "%u\n",
585                        atomic_read(&obd->u.cli.cl_destroy_in_flight));
586 }
587 LUSTRE_RO_ATTR(destroys_in_flight);
588
589 LPROC_SEQ_FOPS_RW_TYPE(osc, obd_max_pages_per_rpc);
590
591 LUSTRE_RW_ATTR(short_io_bytes);
592
593 #ifdef CONFIG_PROC_FS
594 static int osc_unstable_stats_seq_show(struct seq_file *m, void *v)
595 {
596         struct obd_device *dev = m->private;
597         struct client_obd *cli = &dev->u.cli;
598         long pages;
599         int mb;
600
601         pages = atomic_long_read(&cli->cl_unstable_count);
602         mb    = (pages * PAGE_SIZE) >> 20;
603
604         seq_printf(m, "unstable_pages: %20ld\n"
605                    "unstable_mb:              %10d\n",
606                    pages, mb);
607         return 0;
608 }
609 LPROC_SEQ_FOPS_RO(osc_unstable_stats);
610
611 static ssize_t idle_timeout_show(struct kobject *kobj, struct attribute *attr,
612                                  char *buf)
613 {
614         struct obd_device *obd = container_of(kobj, struct obd_device,
615                                               obd_kset.kobj);
616         struct client_obd *cli = &obd->u.cli;
617         int ret;
618
619         LPROCFS_CLIMP_CHECK(obd);
620         ret = sprintf(buf, "%u\n", cli->cl_import->imp_idle_timeout);
621         LPROCFS_CLIMP_EXIT(obd);
622
623         return ret;
624 }
625
626 static ssize_t idle_timeout_store(struct kobject *kobj, struct attribute *attr,
627                                   const char *buffer, size_t count)
628 {
629         struct obd_device *dev = container_of(kobj, struct obd_device,
630                                               obd_kset.kobj);
631         struct client_obd *cli = &dev->u.cli;
632         struct ptlrpc_request *req;
633         unsigned int val;
634         int rc;
635
636         rc = kstrtouint(buffer, 10, &val);
637         if (rc)
638                 return rc;
639
640         if (val > CONNECTION_SWITCH_MAX)
641                 return -ERANGE;
642
643         LPROCFS_CLIMP_CHECK(dev);
644         cli->cl_import->imp_idle_timeout = val;
645
646         /* to initiate the connection if it's in IDLE state */
647         if (!val) {
648                 req = ptlrpc_request_alloc(cli->cl_import, &RQF_OST_STATFS);
649                 if (req != NULL)
650                         ptlrpc_req_finished(req);
651         }
652         LPROCFS_CLIMP_EXIT(dev);
653
654         return count;
655 }
656 LUSTRE_RW_ATTR(idle_timeout);
657
658 static ssize_t idle_connect_store(struct kobject *kobj, struct attribute *attr,
659                                   const char *buffer, size_t count)
660 {
661         struct obd_device *dev = container_of(kobj, struct obd_device,
662                                               obd_kset.kobj);
663         struct client_obd *cli = &dev->u.cli;
664         struct ptlrpc_request *req;
665
666         LPROCFS_CLIMP_CHECK(dev);
667         /* to initiate the connection if it's in IDLE state */
668         req = ptlrpc_request_alloc(cli->cl_import, &RQF_OST_STATFS);
669         if (req)
670                 ptlrpc_req_finished(req);
671         ptlrpc_pinger_force(cli->cl_import);
672         LPROCFS_CLIMP_EXIT(dev);
673
674         return count;
675 }
676 LUSTRE_WO_ATTR(idle_connect);
677
678 LPROC_SEQ_FOPS_RO_TYPE(osc, connect_flags);
679 LPROC_SEQ_FOPS_RO_TYPE(osc, server_uuid);
680 LPROC_SEQ_FOPS_RO_TYPE(osc, timeouts);
681 LPROC_SEQ_FOPS_RO_TYPE(osc, state);
682
683 LPROC_SEQ_FOPS_RW_TYPE(osc, import);
684 LPROC_SEQ_FOPS_RW_TYPE(osc, pinger_recov);
685
686 struct lprocfs_vars lprocfs_osc_obd_vars[] = {
687         { .name =       "connect_flags",
688           .fops =       &osc_connect_flags_fops         },
689         { .name =       "ost_server_uuid",
690           .fops =       &osc_server_uuid_fops           },
691         { .name =       "max_pages_per_rpc",
692           .fops =       &osc_obd_max_pages_per_rpc_fops },
693         { .name =       "osc_cached_mb",
694           .fops =       &osc_cached_mb_fops             },
695         { .name =       "cur_grant_bytes",
696           .fops =       &osc_cur_grant_bytes_fops       },
697         { .name =       "checksum_type",
698           .fops =       &osc_checksum_type_fops         },
699         { .name =       "timeouts",
700           .fops =       &osc_timeouts_fops              },
701         { .name =       "import",
702           .fops =       &osc_import_fops                },
703         { .name =       "state",
704           .fops =       &osc_state_fops                 },
705         { .name =       "pinger_recov",
706           .fops =       &osc_pinger_recov_fops          },
707         { .name =       "unstable_stats",
708           .fops =       &osc_unstable_stats_fops        },
709         { NULL }
710 };
711
712 #define pct(a,b) (b ? a * 100 / b : 0)
713
714 static int osc_rpc_stats_seq_show(struct seq_file *seq, void *v)
715 {
716         struct timespec64 now;
717         struct obd_device *dev = seq->private;
718         struct client_obd *cli = &dev->u.cli;
719         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
720         int i;
721
722         ktime_get_real_ts64(&now);
723
724         spin_lock(&cli->cl_loi_list_lock);
725
726         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
727                    (s64)now.tv_sec, now.tv_nsec);
728         seq_printf(seq, "read RPCs in flight:  %d\n",
729                    cli->cl_r_in_flight);
730         seq_printf(seq, "write RPCs in flight: %d\n",
731                    cli->cl_w_in_flight);
732         seq_printf(seq, "pending write pages:  %d\n",
733                    atomic_read(&cli->cl_pending_w_pages));
734         seq_printf(seq, "pending read pages:   %d\n",
735                    atomic_read(&cli->cl_pending_r_pages));
736
737         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
738         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
739         seq_printf(seq, "       rpcs   %% cum %%\n");
740
741         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
742         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
743
744         read_cum = 0;
745         write_cum = 0;
746         for (i = 0; i < OBD_HIST_MAX; i++) {
747                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
748                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
749
750                 read_cum += r;
751                 write_cum += w;
752                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
753                            1 << i, r, pct(r, read_tot),
754                            pct(read_cum, read_tot), w,
755                            pct(w, write_tot),
756                            pct(write_cum, write_tot));
757                 if (read_cum == read_tot && write_cum == write_tot)
758                         break;
759         }
760
761         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
762         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
763         seq_printf(seq, "       rpcs   %% cum %%\n");
764
765         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
766         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
767
768         read_cum = 0;
769         write_cum = 0;
770         for (i = 0; i < OBD_HIST_MAX; i++) {
771                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
772                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
773                 read_cum += r;
774                 write_cum += w;
775                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
776                            i, r, pct(r, read_tot),
777                            pct(read_cum, read_tot), w,
778                            pct(w, write_tot),
779                            pct(write_cum, write_tot));
780                 if (read_cum == read_tot && write_cum == write_tot)
781                         break;
782         }
783
784         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
785         seq_printf(seq, "offset                rpcs   %% cum %% |");
786         seq_printf(seq, "       rpcs   %% cum %%\n");
787
788         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
789         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
790
791         read_cum = 0;
792         write_cum = 0;
793         for (i = 0; i < OBD_HIST_MAX; i++) {
794                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
795                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
796                 read_cum += r;
797                 write_cum += w;
798                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
799                            (i == 0) ? 0 : 1 << (i - 1),
800                            r, pct(r, read_tot), pct(read_cum, read_tot),
801                            w, pct(w, write_tot), pct(write_cum, write_tot));
802                 if (read_cum == read_tot && write_cum == write_tot)
803                         break;
804         }
805
806         spin_unlock(&cli->cl_loi_list_lock);
807
808         return 0;
809 }
810 #undef pct
811
812 static ssize_t osc_rpc_stats_seq_write(struct file *file,
813                                        const char __user *buf,
814                                        size_t len, loff_t *off)
815 {
816         struct seq_file *seq = file->private_data;
817         struct obd_device *dev = seq->private;
818         struct client_obd *cli = &dev->u.cli;
819
820         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
821         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
822         lprocfs_oh_clear(&cli->cl_read_page_hist);
823         lprocfs_oh_clear(&cli->cl_write_page_hist);
824         lprocfs_oh_clear(&cli->cl_read_offset_hist);
825         lprocfs_oh_clear(&cli->cl_write_offset_hist);
826
827         return len;
828 }
829 LPROC_SEQ_FOPS(osc_rpc_stats);
830
831 static int osc_stats_seq_show(struct seq_file *seq, void *v)
832 {
833         struct timespec64 now;
834         struct obd_device *dev = seq->private;
835         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
836
837         ktime_get_real_ts64(&now);
838
839         seq_printf(seq, "snapshot_time:         %lld.%09lu (secs.nsecs)\n",
840                    (s64)now.tv_sec, now.tv_nsec);
841         seq_printf(seq, "lockless_write_bytes\t\t%llu\n",
842                    stats->os_lockless_writes);
843         seq_printf(seq, "lockless_read_bytes\t\t%llu\n",
844                    stats->os_lockless_reads);
845         seq_printf(seq, "lockless_truncate\t\t%llu\n",
846                    stats->os_lockless_truncates);
847         return 0;
848 }
849
850 static ssize_t osc_stats_seq_write(struct file *file,
851                                    const char __user *buf,
852                                    size_t len, loff_t *off)
853 {
854         struct seq_file *seq = file->private_data;
855         struct obd_device *dev = seq->private;
856         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
857
858         memset(stats, 0, sizeof(*stats));
859         return len;
860 }
861
862 LPROC_SEQ_FOPS(osc_stats);
863
864 int lprocfs_osc_attach_seqstat(struct obd_device *dev)
865 {
866         int rc;
867
868         rc = lprocfs_seq_create(dev->obd_proc_entry, "osc_stats", 0644,
869                                 &osc_stats_fops, dev);
870         if (rc == 0)
871                 rc = lprocfs_obd_seq_create(dev, "rpc_stats", 0644,
872                                             &osc_rpc_stats_fops, dev);
873
874         return rc;
875 }
876 #endif /* CONFIG_PROC_FS */
877
878 static struct attribute *osc_attrs[] = {
879         &lustre_attr_active.attr,
880         &lustre_attr_checksums.attr,
881         &lustre_attr_checksum_dump.attr,
882         &lustre_attr_contention_seconds.attr,
883         &lustre_attr_cur_dirty_bytes.attr,
884         &lustre_attr_cur_lost_grant_bytes.attr,
885         &lustre_attr_destroys_in_flight.attr,
886         &lustre_attr_grant_shrink_interval.attr,
887         &lustre_attr_lockless_truncate.attr,
888         &lustre_attr_max_dirty_mb.attr,
889         &lustre_attr_max_rpcs_in_flight.attr,
890         &lustre_attr_short_io_bytes.attr,
891         &lustre_attr_resend_count.attr,
892         &lustre_attr_conn_uuid.attr,
893         &lustre_attr_ping.attr,
894         &lustre_attr_idle_timeout.attr,
895         &lustre_attr_idle_connect.attr,
896         NULL,
897 };
898
899 int osc_tunables_init(struct obd_device *obd)
900 {
901 #if defined(CONFIG_PROC_FS) && defined(HAVE_SERVER_SUPPORT)
902         struct obd_type *type;
903 #endif
904         int rc;
905
906         obd->obd_vars = lprocfs_osc_obd_vars;
907 #if defined(CONFIG_PROC_FS) && defined(HAVE_SERVER_SUPPORT)
908         /* If this is true then both client (osc) and server (osp) are on the
909          * same node. The osp layer if loaded first will register the osc proc
910          * directory. In that case this obd_device will be attached its proc
911          * tree to type->typ_procsym instead of obd->obd_type->typ_procroot.
912          */
913         type = class_search_type(LUSTRE_OSP_NAME);
914         if (type && type->typ_procsym) {
915                 obd->obd_proc_entry = lprocfs_register(obd->obd_name,
916                                                        type->typ_procsym,
917                                                        obd->obd_vars, obd);
918                 if (IS_ERR(obd->obd_proc_entry)) {
919                         rc = PTR_ERR(obd->obd_proc_entry);
920                         CERROR("error %d setting up lprocfs for %s\n", rc,
921                                obd->obd_name);
922                         obd->obd_proc_entry = NULL;
923                 }
924         }
925 #endif
926         obd->obd_ktype.default_attrs = osc_attrs;
927         rc = lprocfs_obd_setup(obd, false);
928         if (rc)
929                 return rc;
930 #ifdef CONFIG_PROC_FS
931         /* If the basic OSC proc tree construction succeeded then
932          * lets do the rest.
933          */
934         rc = lprocfs_osc_attach_seqstat(obd);
935         if (rc)
936                 goto obd_cleanup;
937
938 #endif /* CONFIG_PROC_FS */
939         rc = sptlrpc_lprocfs_cliobd_attach(obd);
940         if (rc)
941                 goto obd_cleanup;
942
943         ptlrpc_lprocfs_register_obd(obd);
944 obd_cleanup:
945         if (rc)
946                 lprocfs_obd_cleanup(obd);
947         return rc;
948 }