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