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