Whamcloud - gitweb
LU-2049 grant: add support for OBD_CONNECT_GRANT_PARAM
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #define DEBUG_SUBSYSTEM S_CLASS
37
38 #include <linux/version.h>
39 #include <asm/statfs.h>
40 #include <obd_cksum.h>
41 #include <obd_class.h>
42 #include <lprocfs_status.h>
43 #include <linux/seq_file.h>
44 #include "osc_internal.h"
45
46 #ifdef CONFIG_PROC_FS
47 static int osc_active_seq_show(struct seq_file *m, void *v)
48 {
49         struct obd_device *dev = m->private;
50         int rc;
51
52         LPROCFS_CLIMP_CHECK(dev);
53         rc = seq_printf(m, "%d\n", !dev->u.cli.cl_import->imp_deactive);
54         LPROCFS_CLIMP_EXIT(dev);
55         return rc;
56 }
57
58 static ssize_t osc_active_seq_write(struct file *file,
59                                     const char __user *buffer,
60                                     size_t count, loff_t *off)
61 {
62         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
63         int val, rc;
64
65         rc = lprocfs_write_helper(buffer, count, &val);
66         if (rc)
67                 return rc;
68         if (val < 0 || val > 1)
69                 return -ERANGE;
70
71         /* opposite senses */
72         if (dev->u.cli.cl_import->imp_deactive == val)
73                 rc = ptlrpc_set_import_active(dev->u.cli.cl_import, val);
74         else
75                 CDEBUG(D_CONFIG, "activate %d: ignoring repeat request\n", val);
76
77         return count;
78 }
79 LPROC_SEQ_FOPS(osc_active);
80
81 static int osc_max_rpcs_in_flight_seq_show(struct seq_file *m, void *v)
82 {
83         struct obd_device *dev = m->private;
84         struct client_obd *cli = &dev->u.cli;
85         int rc;
86
87         spin_lock(&cli->cl_loi_list_lock);
88         rc = seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
89         spin_unlock(&cli->cl_loi_list_lock);
90         return rc;
91 }
92
93 static ssize_t osc_max_rpcs_in_flight_seq_write(struct file *file,
94                                                 const char __user *buffer,
95                                                 size_t count, loff_t *off)
96 {
97         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
98         struct client_obd *cli = &dev->u.cli;
99         int val, rc;
100         int adding, added, req_count;
101
102         rc = lprocfs_write_helper(buffer, count, &val);
103         if (rc)
104                 return rc;
105
106         if (val < 1 || val > OSC_MAX_RIF_MAX)
107                 return -ERANGE;
108
109         LPROCFS_CLIMP_CHECK(dev);
110
111         adding = 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         LPROCFS_CLIMP_EXIT(dev);
131         return count;
132 }
133 LPROC_SEQ_FOPS(osc_max_rpcs_in_flight);
134
135 static int osc_max_dirty_mb_seq_show(struct seq_file *m, void *v)
136 {
137         struct obd_device *dev = m->private;
138         struct client_obd *cli = &dev->u.cli;
139         long val;
140         int mult;
141
142         spin_lock(&cli->cl_loi_list_lock);
143         val = cli->cl_dirty_max_pages;
144         spin_unlock(&cli->cl_loi_list_lock);
145
146         mult = 1 << (20 - PAGE_CACHE_SHIFT);
147         return lprocfs_seq_read_frac_helper(m, val, mult);
148 }
149
150 static ssize_t osc_max_dirty_mb_seq_write(struct file *file,
151                                           const char __user *buffer,
152                                           size_t count, loff_t *off)
153 {
154         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
155         struct client_obd *cli = &dev->u.cli;
156         int pages_number, mult, rc;
157
158         mult = 1 << (20 - PAGE_CACHE_SHIFT);
159         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
160         if (rc)
161                 return rc;
162
163         if (pages_number <= 0 ||
164             pages_number >= OSC_MAX_DIRTY_MB_MAX << (20 - PAGE_CACHE_SHIFT) ||
165             pages_number > totalram_pages / 4) /* 1/4 of RAM */
166                 return -ERANGE;
167
168         spin_lock(&cli->cl_loi_list_lock);
169         cli->cl_dirty_max_pages = pages_number;
170         osc_wake_cache_waiters(cli);
171         spin_unlock(&cli->cl_loi_list_lock);
172
173         return count;
174 }
175 LPROC_SEQ_FOPS(osc_max_dirty_mb);
176
177 static int osc_cached_mb_seq_show(struct seq_file *m, void *v)
178 {
179         struct obd_device *dev = m->private;
180         struct client_obd *cli = &dev->u.cli;
181         int shift = 20 - PAGE_CACHE_SHIFT;
182         int rc;
183
184         rc = seq_printf(m,
185                       "used_mb: %ld\n"
186                       "busy_cnt: %ld\n"
187                       "reclaim: "LPU64"\n",
188                       (atomic_long_read(&cli->cl_lru_in_list) +
189                        atomic_long_read(&cli->cl_lru_busy)) >> shift,
190                       atomic_long_read(&cli->cl_lru_busy),
191                       cli->cl_lru_reclaim);
192
193         return rc;
194 }
195
196 /* shrink the number of caching pages to a specific number */
197 static ssize_t
198 osc_cached_mb_seq_write(struct file *file, const char __user *buffer,
199                         size_t count, loff_t *off)
200 {
201         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
202         struct client_obd *cli = &dev->u.cli;
203         __u64 val;
204         long pages_number;
205         long rc;
206         int mult;
207         char kernbuf[128];
208
209         if (count >= sizeof(kernbuf))
210                 return -EINVAL;
211
212         if (copy_from_user(kernbuf, buffer, count))
213                 return -EFAULT;
214         kernbuf[count] = 0;
215
216         mult = 1 << (20 - PAGE_CACHE_SHIFT);
217         buffer += lprocfs_find_named_value(kernbuf, "used_mb:", &count) -
218                   kernbuf;
219         rc = lprocfs_write_frac_u64_helper(buffer, count, &val, mult);
220
221         if (rc)
222                 return rc;
223
224         if (val > LONG_MAX)
225                 return -ERANGE;
226         pages_number = (long)val;
227
228         if (pages_number < 0)
229                 return -ERANGE;
230
231         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
232         if (rc > 0) {
233                 struct lu_env *env;
234                 __u16 refcheck;
235
236                 env = cl_env_get(&refcheck);
237                 if (!IS_ERR(env)) {
238                         (void)osc_lru_shrink(env, cli, rc, true);
239                         cl_env_put(env, &refcheck);
240                 }
241         }
242
243         return count;
244 }
245 LPROC_SEQ_FOPS(osc_cached_mb);
246
247 static int osc_cur_dirty_bytes_seq_show(struct seq_file *m, void *v)
248 {
249         struct obd_device *dev = m->private;
250         struct client_obd *cli = &dev->u.cli;
251         int rc;
252
253         spin_lock(&cli->cl_loi_list_lock);
254         rc = seq_printf(m, "%lu\n", cli->cl_dirty_pages << PAGE_CACHE_SHIFT);
255         spin_unlock(&cli->cl_loi_list_lock);
256         return rc;
257 }
258 LPROC_SEQ_FOPS_RO(osc_cur_dirty_bytes);
259
260 static int osc_cur_grant_bytes_seq_show(struct seq_file *m, void *v)
261 {
262         struct obd_device *dev = m->private;
263         struct client_obd *cli = &dev->u.cli;
264         int rc;
265
266         spin_lock(&cli->cl_loi_list_lock);
267         rc = seq_printf(m, "%lu\n", cli->cl_avail_grant);
268         spin_unlock(&cli->cl_loi_list_lock);
269         return rc;
270 }
271
272 static ssize_t osc_cur_grant_bytes_seq_write(struct file *file,
273                                              const char __user *buffer,
274                                              size_t count, loff_t *off)
275 {
276         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
277         struct client_obd *cli = &obd->u.cli;
278         int                rc;
279         __u64              val;
280
281         if (obd == NULL)
282                 return 0;
283
284         rc = lprocfs_write_u64_helper(buffer, count, &val);
285         if (rc)
286                 return rc;
287
288         /* this is only for shrinking grant */
289         spin_lock(&cli->cl_loi_list_lock);
290         if (val >= cli->cl_avail_grant) {
291                 spin_unlock(&cli->cl_loi_list_lock);
292                 return 0;
293         }
294
295         spin_unlock(&cli->cl_loi_list_lock);
296
297         LPROCFS_CLIMP_CHECK(obd);
298         if (cli->cl_import->imp_state == LUSTRE_IMP_FULL)
299                 rc = osc_shrink_grant_to_target(cli, val);
300         LPROCFS_CLIMP_EXIT(obd);
301         if (rc)
302                 return rc;
303         return count;
304 }
305 LPROC_SEQ_FOPS(osc_cur_grant_bytes);
306
307 static int osc_cur_lost_grant_bytes_seq_show(struct seq_file *m, void *v)
308 {
309         struct obd_device *dev = m->private;
310         struct client_obd *cli = &dev->u.cli;
311         int rc;
312
313         spin_lock(&cli->cl_loi_list_lock);
314         rc = seq_printf(m, "%lu\n", cli->cl_lost_grant);
315         spin_unlock(&cli->cl_loi_list_lock);
316         return rc;
317 }
318 LPROC_SEQ_FOPS_RO(osc_cur_lost_grant_bytes);
319
320 static int osc_cur_dirty_grant_bytes_seq_show(struct seq_file *m, void *v)
321 {
322         struct obd_device *dev = m->private;
323         struct client_obd *cli = &dev->u.cli;
324         int rc;
325
326         spin_lock(&cli->cl_loi_list_lock);
327         rc = seq_printf(m, "%lu\n", cli->cl_dirty_grant);
328         spin_unlock(&cli->cl_loi_list_lock);
329         return rc;
330 }
331 LPROC_SEQ_FOPS_RO(osc_cur_dirty_grant_bytes);
332
333 static int osc_grant_shrink_interval_seq_show(struct seq_file *m, void *v)
334 {
335         struct obd_device *obd = m->private;
336
337         if (obd == NULL)
338                 return 0;
339         return seq_printf(m, "%d\n",
340                           obd->u.cli.cl_grant_shrink_interval);
341 }
342
343 static ssize_t osc_grant_shrink_interval_seq_write(struct file *file,
344                                                    const char __user *buffer,
345                                                    size_t count, loff_t *off)
346 {
347         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
348         int val, rc;
349
350         if (obd == NULL)
351                 return 0;
352
353         rc = lprocfs_write_helper(buffer, count, &val);
354         if (rc)
355                 return rc;
356
357         if (val <= 0)
358                 return -ERANGE;
359
360         obd->u.cli.cl_grant_shrink_interval = val;
361
362         return count;
363 }
364 LPROC_SEQ_FOPS(osc_grant_shrink_interval);
365
366 static int osc_checksum_seq_show(struct seq_file *m, void *v)
367 {
368         struct obd_device *obd = m->private;
369
370         if (obd == NULL)
371                 return 0;
372
373         return seq_printf(m, "%d\n",
374                           obd->u.cli.cl_checksum ? 1 : 0);
375 }
376
377 static ssize_t osc_checksum_seq_write(struct file *file,
378                                       const char __user *buffer,
379                                       size_t count, loff_t *off)
380 {
381         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
382         int val, rc;
383
384         if (obd == NULL)
385                 return 0;
386
387         rc = lprocfs_write_helper(buffer, count, &val);
388         if (rc)
389                 return rc;
390
391         obd->u.cli.cl_checksum = (val ? 1 : 0);
392
393         return count;
394 }
395 LPROC_SEQ_FOPS(osc_checksum);
396
397 static int osc_checksum_type_seq_show(struct seq_file *m, void *v)
398 {
399         struct obd_device *obd = m->private;
400         int i;
401         DECLARE_CKSUM_NAME;
402
403         if (obd == NULL)
404                 return 0;
405
406         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
407                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
408                         continue;
409                 if (obd->u.cli.cl_cksum_type == (1 << i))
410                         seq_printf(m, "[%s] ", cksum_name[i]);
411                 else
412                         seq_printf(m, "%s ", cksum_name[i]);
413         }
414         seq_printf(m, "\n");
415         return 0;
416 }
417
418 static ssize_t osc_checksum_type_seq_write(struct file *file,
419                                            const char __user *buffer,
420                                            size_t count, loff_t *off)
421 {
422         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
423         int i;
424         DECLARE_CKSUM_NAME;
425         char kernbuf[10];
426
427         if (obd == NULL)
428                 return 0;
429
430         if (count > sizeof(kernbuf) - 1)
431                 return -EINVAL;
432         if (copy_from_user(kernbuf, buffer, count))
433                 return -EFAULT;
434         if (count > 0 && kernbuf[count - 1] == '\n')
435                 kernbuf[count - 1] = '\0';
436         else
437                 kernbuf[count] = '\0';
438
439         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
440                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
441                         continue;
442                 if (!strcmp(kernbuf, cksum_name[i])) {
443                        obd->u.cli.cl_cksum_type = 1 << i;
444                        return count;
445                 }
446         }
447         return -EINVAL;
448 }
449 LPROC_SEQ_FOPS(osc_checksum_type);
450
451 static int osc_resend_count_seq_show(struct seq_file *m, void *v)
452 {
453         struct obd_device *obd = m->private;
454
455         return seq_printf(m, "%u\n", atomic_read(&obd->u.cli.cl_resends));
456 }
457
458 static ssize_t osc_resend_count_seq_write(struct file *file,
459                                           const char __user *buffer,
460                                           size_t count, loff_t *off)
461 {
462         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
463         int val, rc;
464
465         rc = lprocfs_write_helper(buffer, count, &val);
466         if (rc)
467                 return rc;
468
469         if (val < 0)
470                 return -EINVAL;
471
472         atomic_set(&obd->u.cli.cl_resends, val);
473
474         return count;
475 }
476 LPROC_SEQ_FOPS(osc_resend_count);
477
478 static int osc_contention_seconds_seq_show(struct seq_file *m, void *v)
479 {
480         struct obd_device *obd = m->private;
481         struct osc_device *od  = obd2osc_dev(obd);
482
483         return seq_printf(m, "%u\n", od->od_contention_time);
484 }
485
486 static ssize_t osc_contention_seconds_seq_write(struct file *file,
487                                                 const char __user *buffer,
488                                                 size_t count, loff_t *off)
489 {
490         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
491         struct osc_device *od  = obd2osc_dev(obd);
492
493         return lprocfs_write_helper(buffer, count, &od->od_contention_time) ?: count;
494 }
495 LPROC_SEQ_FOPS(osc_contention_seconds);
496
497 static int osc_lockless_truncate_seq_show(struct seq_file *m, void *v)
498 {
499         struct obd_device *obd = m->private;
500         struct osc_device *od  = obd2osc_dev(obd);
501
502         return seq_printf(m, "%u\n", od->od_lockless_truncate);
503 }
504
505 static ssize_t osc_lockless_truncate_seq_write(struct file *file,
506                                                const char __user *buffer,
507                                     size_t count, loff_t *off)
508 {
509         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
510         struct osc_device *od  = obd2osc_dev(obd);
511
512         return lprocfs_write_helper(buffer, count, &od->od_lockless_truncate) ?:
513                 count;
514 }
515 LPROC_SEQ_FOPS(osc_lockless_truncate);
516
517 static int osc_destroys_in_flight_seq_show(struct seq_file *m, void *v)
518 {
519         struct obd_device *obd = m->private;
520         return seq_printf(m, "%u\n",
521                           atomic_read(&obd->u.cli.cl_destroy_in_flight));
522 }
523 LPROC_SEQ_FOPS_RO(osc_destroys_in_flight);
524
525 static int osc_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *v)
526 {
527         return lprocfs_obd_max_pages_per_rpc_seq_show(m, m->private);
528 }
529
530 static ssize_t osc_obd_max_pages_per_rpc_seq_write(struct file *file,
531                                                    const char __user *buffer,
532                                                    size_t count, loff_t *off)
533 {
534         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
535         struct client_obd *cli = &dev->u.cli;
536         struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
537         int chunk_mask, rc;
538         __u64 val;
539
540         rc = lprocfs_write_u64_helper(buffer, count, &val);
541         if (rc)
542                 return rc;
543
544         /* if the max_pages is specified in bytes, convert to pages */
545         if (val >= ONE_MB_BRW_SIZE)
546                 val >>= PAGE_CACHE_SHIFT;
547
548         LPROCFS_CLIMP_CHECK(dev);
549
550         chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_CACHE_SHIFT)) - 1);
551         /* max_pages_per_rpc must be chunk aligned */
552         val = (val + ~chunk_mask) & chunk_mask;
553         if (val == 0 || (ocd->ocd_brw_size != 0 &&
554                          val > ocd->ocd_brw_size >> PAGE_CACHE_SHIFT)) {
555                 LPROCFS_CLIMP_EXIT(dev);
556                 return -ERANGE;
557         }
558         spin_lock(&cli->cl_loi_list_lock);
559         cli->cl_max_pages_per_rpc = val;
560         client_adjust_max_dirty(cli);
561         spin_unlock(&cli->cl_loi_list_lock);
562
563         LPROCFS_CLIMP_EXIT(dev);
564         return count;
565 }
566 LPROC_SEQ_FOPS(osc_obd_max_pages_per_rpc);
567
568 static int osc_unstable_stats_seq_show(struct seq_file *m, void *v)
569 {
570         struct obd_device *dev = m->private;
571         struct client_obd *cli = &dev->u.cli;
572         long pages;
573         int mb;
574
575         pages = atomic_long_read(&cli->cl_unstable_count);
576         mb    = (pages * PAGE_CACHE_SIZE) >> 20;
577
578         return seq_printf(m, "unstable_pages: %20ld\n"
579                           "unstable_mb:              %10d\n",
580                         pages, mb);
581 }
582 LPROC_SEQ_FOPS_RO(osc_unstable_stats);
583
584 LPROC_SEQ_FOPS_RO_TYPE(osc, uuid);
585 LPROC_SEQ_FOPS_RO_TYPE(osc, connect_flags);
586 LPROC_SEQ_FOPS_RO_TYPE(osc, blksize);
587 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytestotal);
588 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytesfree);
589 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytesavail);
590 LPROC_SEQ_FOPS_RO_TYPE(osc, filestotal);
591 LPROC_SEQ_FOPS_RO_TYPE(osc, filesfree);
592 LPROC_SEQ_FOPS_RO_TYPE(osc, server_uuid);
593 LPROC_SEQ_FOPS_RO_TYPE(osc, conn_uuid);
594 LPROC_SEQ_FOPS_RO_TYPE(osc, timeouts);
595 LPROC_SEQ_FOPS_RO_TYPE(osc, state);
596
597 LPROC_SEQ_FOPS_WO_TYPE(osc, ping);
598
599 LPROC_SEQ_FOPS_RW_TYPE(osc, import);
600 LPROC_SEQ_FOPS_RW_TYPE(osc, pinger_recov);
601
602 struct lprocfs_vars lprocfs_osc_obd_vars[] = {
603         { .name =       "uuid",
604           .fops =       &osc_uuid_fops                  },
605         { .name =       "ping",
606           .fops =       &osc_ping_fops,
607           .proc_mode =  0222                            },
608         { .name =       "connect_flags",
609           .fops =       &osc_connect_flags_fops         },
610         { .name =       "blocksize",
611           .fops =       &osc_blksize_fops               },
612         { .name =       "kbytestotal",
613           .fops =       &osc_kbytestotal_fops           },
614         { .name =       "kbytesfree",
615           .fops =       &osc_kbytesfree_fops            },
616         { .name =       "kbytesavail",
617           .fops =       &osc_kbytesavail_fops           },
618         { .name =       "filestotal",
619           .fops =       &osc_filestotal_fops            },
620         { .name =       "filesfree",
621           .fops =       &osc_filesfree_fops             },
622         { .name =       "ost_server_uuid",
623           .fops =       &osc_server_uuid_fops           },
624         { .name =       "ost_conn_uuid",
625           .fops =       &osc_conn_uuid_fops             },
626         { .name =       "active",
627           .fops =       &osc_active_fops                },
628         { .name =       "max_pages_per_rpc",
629           .fops =       &osc_obd_max_pages_per_rpc_fops },
630         { .name =       "max_rpcs_in_flight",
631           .fops =       &osc_max_rpcs_in_flight_fops    },
632         { .name =       "destroys_in_flight",
633           .fops =       &osc_destroys_in_flight_fops    },
634         { .name =       "max_dirty_mb",
635           .fops =       &osc_max_dirty_mb_fops          },
636         { .name =       "osc_cached_mb",
637           .fops =       &osc_cached_mb_fops             },
638         { .name =       "cur_dirty_bytes",
639           .fops =       &osc_cur_dirty_bytes_fops       },
640         { .name =       "cur_grant_bytes",
641           .fops =       &osc_cur_grant_bytes_fops       },
642         { .name =       "cur_lost_grant_bytes",
643           .fops =       &osc_cur_lost_grant_bytes_fops  },
644         { .name =       "cur_dirty_grant_bytes",
645           .fops =       &osc_cur_dirty_grant_bytes_fops },
646         { .name =       "grant_shrink_interval",
647           .fops =       &osc_grant_shrink_interval_fops },
648         { .name =       "checksums",
649           .fops =       &osc_checksum_fops              },
650         { .name =       "checksum_type",
651           .fops =       &osc_checksum_type_fops         },
652         { .name =       "resend_count",
653           .fops =       &osc_resend_count_fops          },
654         { .name =       "timeouts",
655           .fops =       &osc_timeouts_fops              },
656         { .name =       "contention_seconds",
657           .fops =       &osc_contention_seconds_fops    },
658         { .name =       "lockless_truncate",
659           .fops =       &osc_lockless_truncate_fops     },
660         { .name =       "import",
661           .fops =       &osc_import_fops                },
662         { .name =       "state",
663           .fops =       &osc_state_fops                 },
664         { .name =       "pinger_recov",
665           .fops =       &osc_pinger_recov_fops          },
666         { .name =       "unstable_stats",
667           .fops =       &osc_unstable_stats_fops        },
668         { NULL }
669 };
670
671 #define pct(a,b) (b ? a * 100 / b : 0)
672
673 static int osc_rpc_stats_seq_show(struct seq_file *seq, void *v)
674 {
675         struct timeval now;
676         struct obd_device *dev = seq->private;
677         struct client_obd *cli = &dev->u.cli;
678         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
679         int i;
680
681         do_gettimeofday(&now);
682
683         spin_lock(&cli->cl_loi_list_lock);
684
685         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
686                    now.tv_sec, now.tv_usec);
687         seq_printf(seq, "read RPCs in flight:  %d\n",
688                    cli->cl_r_in_flight);
689         seq_printf(seq, "write RPCs in flight: %d\n",
690                    cli->cl_w_in_flight);
691         seq_printf(seq, "pending write pages:  %d\n",
692                    atomic_read(&cli->cl_pending_w_pages));
693         seq_printf(seq, "pending read pages:   %d\n",
694                    atomic_read(&cli->cl_pending_r_pages));
695
696         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
697         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
698         seq_printf(seq, "       rpcs   %% cum %%\n");
699
700         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
701         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
702
703         read_cum = 0;
704         write_cum = 0;
705         for (i = 0; i < OBD_HIST_MAX; i++) {
706                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
707                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
708
709                 read_cum += r;
710                 write_cum += w;
711                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
712                            1 << i, r, pct(r, read_tot),
713                            pct(read_cum, read_tot), w,
714                            pct(w, write_tot),
715                            pct(write_cum, write_tot));
716                 if (read_cum == read_tot && write_cum == write_tot)
717                         break;
718         }
719
720         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
721         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
722         seq_printf(seq, "       rpcs   %% cum %%\n");
723
724         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
725         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
726
727         read_cum = 0;
728         write_cum = 0;
729         for (i = 0; i < OBD_HIST_MAX; i++) {
730                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
731                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
732                 read_cum += r;
733                 write_cum += w;
734                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
735                                  i, r, pct(r, read_tot),
736                                  pct(read_cum, read_tot), w,
737                                  pct(w, write_tot),
738                                  pct(write_cum, write_tot));
739                 if (read_cum == read_tot && write_cum == write_tot)
740                         break;
741         }
742
743         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
744         seq_printf(seq, "offset                rpcs   %% cum %% |");
745         seq_printf(seq, "       rpcs   %% cum %%\n");
746
747         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
748         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
749
750         read_cum = 0;
751         write_cum = 0;
752         for (i = 0; i < OBD_HIST_MAX; i++) {
753                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
754                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
755                 read_cum += r;
756                 write_cum += w;
757                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
758                            (i == 0) ? 0 : 1 << (i - 1),
759                            r, pct(r, read_tot), pct(read_cum, read_tot),
760                            w, pct(w, write_tot), pct(write_cum, write_tot));
761                 if (read_cum == read_tot && write_cum == write_tot)
762                         break;
763         }
764
765         spin_unlock(&cli->cl_loi_list_lock);
766
767         return 0;
768 }
769 #undef pct
770
771 static ssize_t osc_rpc_stats_seq_write(struct file *file,
772                                        const char __user *buf,
773                                        size_t len, loff_t *off)
774 {
775         struct seq_file *seq = file->private_data;
776         struct obd_device *dev = seq->private;
777         struct client_obd *cli = &dev->u.cli;
778
779         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
780         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
781         lprocfs_oh_clear(&cli->cl_read_page_hist);
782         lprocfs_oh_clear(&cli->cl_write_page_hist);
783         lprocfs_oh_clear(&cli->cl_read_offset_hist);
784         lprocfs_oh_clear(&cli->cl_write_offset_hist);
785
786         return len;
787 }
788 LPROC_SEQ_FOPS(osc_rpc_stats);
789
790 static int osc_stats_seq_show(struct seq_file *seq, void *v)
791 {
792         struct timeval now;
793         struct obd_device *dev = seq->private;
794         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
795
796         do_gettimeofday(&now);
797
798         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
799                    now.tv_sec, now.tv_usec);
800         seq_printf(seq, "lockless_write_bytes\t\t"LPU64"\n",
801                    stats->os_lockless_writes);
802         seq_printf(seq, "lockless_read_bytes\t\t"LPU64"\n",
803                    stats->os_lockless_reads);
804         seq_printf(seq, "lockless_truncate\t\t"LPU64"\n",
805                    stats->os_lockless_truncates);
806         return 0;
807 }
808
809 static ssize_t osc_stats_seq_write(struct file *file,
810                                    const char __user *buf,
811                                    size_t len, loff_t *off)
812 {
813         struct seq_file *seq = file->private_data;
814         struct obd_device *dev = seq->private;
815         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
816
817         memset(stats, 0, sizeof(*stats));
818         return len;
819 }
820
821 LPROC_SEQ_FOPS(osc_stats);
822
823 int lproc_osc_attach_seqstat(struct obd_device *dev)
824 {
825         int rc;
826
827         rc = lprocfs_seq_create(dev->obd_proc_entry, "osc_stats", 0644,
828                                 &osc_stats_fops, dev);
829         if (rc == 0)
830                 rc = lprocfs_obd_seq_create(dev, "rpc_stats", 0644,
831                                             &osc_rpc_stats_fops, dev);
832
833         return rc;
834 }
835 #endif /* CONFIG_PROC_FS */