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