Whamcloud - gitweb
LU-5458: libcfs: protect kkuc_groups from write access
[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, 2013, 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 LPROCFS
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         client_obd_list_lock(&cli->cl_loi_list_lock);
88         rc = seq_printf(m, "%u\n", cli->cl_max_rpcs_in_flight);
89         client_obd_list_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         struct ptlrpc_request_pool *pool = cli->cl_import->imp_rq_pool;
100         int val, rc;
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         if (pool && val > cli->cl_max_rpcs_in_flight)
111                 pool->prp_populate(pool, val-cli->cl_max_rpcs_in_flight);
112
113         client_obd_list_lock(&cli->cl_loi_list_lock);
114         cli->cl_max_rpcs_in_flight = val;
115         client_adjust_max_dirty(cli);
116         client_obd_list_unlock(&cli->cl_loi_list_lock);
117
118         LPROCFS_CLIMP_EXIT(dev);
119         return count;
120 }
121 LPROC_SEQ_FOPS(osc_max_rpcs_in_flight);
122
123 static int osc_max_dirty_mb_seq_show(struct seq_file *m, void *v)
124 {
125         struct obd_device *dev = m->private;
126         struct client_obd *cli = &dev->u.cli;
127         long val;
128         int mult;
129
130         client_obd_list_lock(&cli->cl_loi_list_lock);
131         val = cli->cl_dirty_max_pages;
132         client_obd_list_unlock(&cli->cl_loi_list_lock);
133
134         mult = 1 << (20 - PAGE_CACHE_SHIFT);
135         return lprocfs_seq_read_frac_helper(m, val, mult);
136 }
137
138 static ssize_t osc_max_dirty_mb_seq_write(struct file *file,
139                                           const char __user *buffer,
140                                           size_t count, loff_t *off)
141 {
142         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
143         struct client_obd *cli = &dev->u.cli;
144         int pages_number, mult, rc;
145
146         mult = 1 << (20 - PAGE_CACHE_SHIFT);
147         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
148         if (rc)
149                 return rc;
150
151         if (pages_number <= 0 ||
152             pages_number > OSC_MAX_DIRTY_MB_MAX << (20 - PAGE_CACHE_SHIFT) ||
153             pages_number > totalram_pages / 4) /* 1/4 of RAM */
154                 return -ERANGE;
155
156         client_obd_list_lock(&cli->cl_loi_list_lock);
157         cli->cl_dirty_max_pages = pages_number;
158         osc_wake_cache_waiters(cli);
159         client_obd_list_unlock(&cli->cl_loi_list_lock);
160
161         return count;
162 }
163 LPROC_SEQ_FOPS(osc_max_dirty_mb);
164
165 static int osc_cached_mb_seq_show(struct seq_file *m, void *v)
166 {
167         struct obd_device *dev = m->private;
168         struct client_obd *cli = &dev->u.cli;
169         int shift = 20 - PAGE_CACHE_SHIFT;
170         int rc;
171
172         rc = seq_printf(m,
173                       "used_mb: %ld\n"
174                       "busy_cnt: %ld\n",
175                       (atomic_long_read(&cli->cl_lru_in_list) +
176                         atomic_long_read(&cli->cl_lru_busy)) >> shift,
177                       atomic_long_read(&cli->cl_lru_busy));
178
179         return rc;
180 }
181
182 /* shrink the number of caching pages to a specific number */
183 static ssize_t
184 osc_cached_mb_seq_write(struct file *file, const char __user *buffer,
185                         size_t count, loff_t *off)
186 {
187         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
188         struct client_obd *cli = &dev->u.cli;
189         __u64 val;
190         long pages_number;
191         long rc;
192         int mult;
193         char kernbuf[128];
194
195         if (count >= sizeof(kernbuf))
196                 return -EINVAL;
197
198         if (copy_from_user(kernbuf, buffer, count))
199                 return -EFAULT;
200         kernbuf[count] = 0;
201
202         mult = 1 << (20 - PAGE_CACHE_SHIFT);
203         buffer += lprocfs_find_named_value(kernbuf, "used_mb:", &count) -
204                   kernbuf;
205         rc = lprocfs_write_frac_u64_helper(buffer, count, &val, mult);
206
207         if (rc)
208                 return rc;
209
210         if (val > LONG_MAX)
211                 return -ERANGE;
212         pages_number = (long)val;
213
214         if (pages_number < 0)
215                 return -ERANGE;
216
217         rc = atomic_long_read(&cli->cl_lru_in_list) - pages_number;
218         if (rc > 0) {
219                 struct lu_env *env;
220                 int refcheck;
221
222                 env = cl_env_get(&refcheck);
223                 if (!IS_ERR(env)) {
224                         (void)osc_lru_shrink(env, cli, rc, true);
225                         cl_env_put(env, &refcheck);
226                 }
227         }
228
229         return count;
230 }
231 LPROC_SEQ_FOPS(osc_cached_mb);
232
233 static int osc_cur_dirty_bytes_seq_show(struct seq_file *m, void *v)
234 {
235         struct obd_device *dev = m->private;
236         struct client_obd *cli = &dev->u.cli;
237         int rc;
238
239         client_obd_list_lock(&cli->cl_loi_list_lock);
240         rc = seq_printf(m, "%lu\n", cli->cl_dirty_pages << PAGE_CACHE_SHIFT);
241         client_obd_list_unlock(&cli->cl_loi_list_lock);
242         return rc;
243 }
244 LPROC_SEQ_FOPS_RO(osc_cur_dirty_bytes);
245
246 static int osc_cur_grant_bytes_seq_show(struct seq_file *m, void *v)
247 {
248         struct obd_device *dev = m->private;
249         struct client_obd *cli = &dev->u.cli;
250         int rc;
251
252         client_obd_list_lock(&cli->cl_loi_list_lock);
253         rc = seq_printf(m, "%lu\n", cli->cl_avail_grant);
254         client_obd_list_unlock(&cli->cl_loi_list_lock);
255         return rc;
256 }
257
258 static ssize_t osc_cur_grant_bytes_seq_write(struct file *file,
259                                              const char __user *buffer,
260                                              size_t count, loff_t *off)
261 {
262         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
263         struct client_obd *cli = &obd->u.cli;
264         int                rc;
265         __u64              val;
266
267         if (obd == NULL)
268                 return 0;
269
270         rc = lprocfs_write_u64_helper(buffer, count, &val);
271         if (rc)
272                 return rc;
273
274         /* this is only for shrinking grant */
275         client_obd_list_lock(&cli->cl_loi_list_lock);
276         if (val >= cli->cl_avail_grant) {
277                 client_obd_list_unlock(&cli->cl_loi_list_lock);
278                 return 0;
279         }
280         client_obd_list_unlock(&cli->cl_loi_list_lock);
281
282         LPROCFS_CLIMP_CHECK(obd);
283         if (cli->cl_import->imp_state == LUSTRE_IMP_FULL)
284                 rc = osc_shrink_grant_to_target(cli, val);
285         LPROCFS_CLIMP_EXIT(obd);
286         if (rc)
287                 return rc;
288         return count;
289 }
290 LPROC_SEQ_FOPS(osc_cur_grant_bytes);
291
292 static int osc_cur_lost_grant_bytes_seq_show(struct seq_file *m, void *v)
293 {
294         struct obd_device *dev = m->private;
295         struct client_obd *cli = &dev->u.cli;
296         int rc;
297
298         client_obd_list_lock(&cli->cl_loi_list_lock);
299         rc = seq_printf(m, "%lu\n", cli->cl_lost_grant);
300         client_obd_list_unlock(&cli->cl_loi_list_lock);
301         return rc;
302 }
303 LPROC_SEQ_FOPS_RO(osc_cur_lost_grant_bytes);
304
305 static int osc_grant_shrink_interval_seq_show(struct seq_file *m, void *v)
306 {
307         struct obd_device *obd = m->private;
308
309         if (obd == NULL)
310                 return 0;
311         return seq_printf(m, "%d\n",
312                           obd->u.cli.cl_grant_shrink_interval);
313 }
314
315 static ssize_t osc_grant_shrink_interval_seq_write(struct file *file,
316                                                    const char __user *buffer,
317                                                    size_t count, loff_t *off)
318 {
319         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
320         int val, rc;
321
322         if (obd == NULL)
323                 return 0;
324
325         rc = lprocfs_write_helper(buffer, count, &val);
326         if (rc)
327                 return rc;
328
329         if (val <= 0)
330                 return -ERANGE;
331
332         obd->u.cli.cl_grant_shrink_interval = val;
333
334         return count;
335 }
336 LPROC_SEQ_FOPS(osc_grant_shrink_interval);
337
338 static int osc_checksum_seq_show(struct seq_file *m, void *v)
339 {
340         struct obd_device *obd = m->private;
341
342         if (obd == NULL)
343                 return 0;
344
345         return seq_printf(m, "%d\n",
346                           obd->u.cli.cl_checksum ? 1 : 0);
347 }
348
349 static ssize_t osc_checksum_seq_write(struct file *file,
350                                       const char __user *buffer,
351                                       size_t count, loff_t *off)
352 {
353         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
354         int val, rc;
355
356         if (obd == NULL)
357                 return 0;
358
359         rc = lprocfs_write_helper(buffer, count, &val);
360         if (rc)
361                 return rc;
362
363         obd->u.cli.cl_checksum = (val ? 1 : 0);
364
365         return count;
366 }
367 LPROC_SEQ_FOPS(osc_checksum);
368
369 static int osc_checksum_type_seq_show(struct seq_file *m, void *v)
370 {
371         struct obd_device *obd = m->private;
372         int i;
373         DECLARE_CKSUM_NAME;
374
375         if (obd == NULL)
376                 return 0;
377
378         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
379                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
380                         continue;
381                 if (obd->u.cli.cl_cksum_type == (1 << i))
382                         seq_printf(m, "[%s] ", cksum_name[i]);
383                 else
384                         seq_printf(m, "%s ", cksum_name[i]);
385         }
386         seq_printf(m, "\n");
387         return 0;
388 }
389
390 static ssize_t osc_checksum_type_seq_write(struct file *file,
391                                            const char __user *buffer,
392                                            size_t count, loff_t *off)
393 {
394         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
395         int i;
396         DECLARE_CKSUM_NAME;
397         char kernbuf[10];
398
399         if (obd == NULL)
400                 return 0;
401
402         if (count > sizeof(kernbuf) - 1)
403                 return -EINVAL;
404         if (copy_from_user(kernbuf, buffer, count))
405                 return -EFAULT;
406         if (count > 0 && kernbuf[count - 1] == '\n')
407                 kernbuf[count - 1] = '\0';
408         else
409                 kernbuf[count] = '\0';
410
411         for (i = 0; i < ARRAY_SIZE(cksum_name); i++) {
412                 if (((1 << i) & obd->u.cli.cl_supp_cksum_types) == 0)
413                         continue;
414                 if (!strcmp(kernbuf, cksum_name[i])) {
415                        obd->u.cli.cl_cksum_type = 1 << i;
416                        return count;
417                 }
418         }
419         return -EINVAL;
420 }
421 LPROC_SEQ_FOPS(osc_checksum_type);
422
423 static int osc_resend_count_seq_show(struct seq_file *m, void *v)
424 {
425         struct obd_device *obd = m->private;
426
427         return seq_printf(m, "%u\n", atomic_read(&obd->u.cli.cl_resends));
428 }
429
430 static ssize_t osc_resend_count_seq_write(struct file *file,
431                                           const char __user *buffer,
432                                           size_t count, loff_t *off)
433 {
434         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
435         int val, rc;
436
437         rc = lprocfs_write_helper(buffer, count, &val);
438         if (rc)
439                 return rc;
440
441         if (val < 0)
442                 return -EINVAL;
443
444         atomic_set(&obd->u.cli.cl_resends, val);
445
446         return count;
447 }
448 LPROC_SEQ_FOPS(osc_resend_count);
449
450 static int osc_contention_seconds_seq_show(struct seq_file *m, void *v)
451 {
452         struct obd_device *obd = m->private;
453         struct osc_device *od  = obd2osc_dev(obd);
454
455         return seq_printf(m, "%u\n", od->od_contention_time);
456 }
457
458 static ssize_t osc_contention_seconds_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         struct osc_device *od  = obd2osc_dev(obd);
464
465         return lprocfs_write_helper(buffer, count, &od->od_contention_time) ?: count;
466 }
467 LPROC_SEQ_FOPS(osc_contention_seconds);
468
469 static int osc_lockless_truncate_seq_show(struct seq_file *m, void *v)
470 {
471         struct obd_device *obd = m->private;
472         struct osc_device *od  = obd2osc_dev(obd);
473
474         return seq_printf(m, "%u\n", od->od_lockless_truncate);
475 }
476
477 static ssize_t osc_lockless_truncate_seq_write(struct file *file,
478                                                const char __user *buffer,
479                                     size_t count, loff_t *off)
480 {
481         struct obd_device *obd = ((struct seq_file *)file->private_data)->private;
482         struct osc_device *od  = obd2osc_dev(obd);
483
484         return lprocfs_write_helper(buffer, count, &od->od_lockless_truncate) ?:
485                 count;
486 }
487 LPROC_SEQ_FOPS(osc_lockless_truncate);
488
489 static int osc_destroys_in_flight_seq_show(struct seq_file *m, void *v)
490 {
491         struct obd_device *obd = m->private;
492         return seq_printf(m, "%u\n",
493                           atomic_read(&obd->u.cli.cl_destroy_in_flight));
494 }
495 LPROC_SEQ_FOPS_RO(osc_destroys_in_flight);
496
497 static int osc_obd_max_pages_per_rpc_seq_show(struct seq_file *m, void *v)
498 {
499         return lprocfs_obd_max_pages_per_rpc_seq_show(m, m->private);
500 }
501
502 static ssize_t osc_obd_max_pages_per_rpc_seq_write(struct file *file,
503                                                    const char __user *buffer,
504                                                    size_t count, loff_t *off)
505 {
506         struct obd_device *dev = ((struct seq_file *)file->private_data)->private;
507         struct client_obd *cli = &dev->u.cli;
508         struct obd_connect_data *ocd = &cli->cl_import->imp_connect_data;
509         int chunk_mask, rc;
510         __u64 val;
511
512         rc = lprocfs_write_u64_helper(buffer, count, &val);
513         if (rc)
514                 return rc;
515
516         /* if the max_pages is specified in bytes, convert to pages */
517         if (val >= ONE_MB_BRW_SIZE)
518                 val >>= PAGE_CACHE_SHIFT;
519
520         LPROCFS_CLIMP_CHECK(dev);
521
522         chunk_mask = ~((1 << (cli->cl_chunkbits - PAGE_CACHE_SHIFT)) - 1);
523         /* max_pages_per_rpc must be chunk aligned */
524         val = (val + ~chunk_mask) & chunk_mask;
525         if (val == 0 || val > ocd->ocd_brw_size >> PAGE_CACHE_SHIFT) {
526                 LPROCFS_CLIMP_EXIT(dev);
527                 return -ERANGE;
528         }
529         client_obd_list_lock(&cli->cl_loi_list_lock);
530         cli->cl_max_pages_per_rpc = val;
531         client_adjust_max_dirty(cli);
532         client_obd_list_unlock(&cli->cl_loi_list_lock);
533
534         LPROCFS_CLIMP_EXIT(dev);
535         return count;
536 }
537 LPROC_SEQ_FOPS(osc_obd_max_pages_per_rpc);
538
539 static int osc_unstable_stats_seq_show(struct seq_file *m, void *v)
540 {
541         struct obd_device *dev = m->private;
542         struct client_obd *cli = &dev->u.cli;
543         long pages;
544         int mb;
545
546         pages = atomic_long_read(&cli->cl_unstable_count);
547         mb    = (pages * PAGE_CACHE_SIZE) >> 20;
548
549         return seq_printf(m, "unstable_pages: %20ld\n"
550                           "unstable_mb:              %10d\n",
551                         pages, mb);
552 }
553 LPROC_SEQ_FOPS_RO(osc_unstable_stats);
554
555 LPROC_SEQ_FOPS_RO_TYPE(osc, uuid);
556 LPROC_SEQ_FOPS_RO_TYPE(osc, connect_flags);
557 LPROC_SEQ_FOPS_RO_TYPE(osc, blksize);
558 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytestotal);
559 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytesfree);
560 LPROC_SEQ_FOPS_RO_TYPE(osc, kbytesavail);
561 LPROC_SEQ_FOPS_RO_TYPE(osc, filestotal);
562 LPROC_SEQ_FOPS_RO_TYPE(osc, filesfree);
563 LPROC_SEQ_FOPS_RO_TYPE(osc, server_uuid);
564 LPROC_SEQ_FOPS_RO_TYPE(osc, conn_uuid);
565 LPROC_SEQ_FOPS_RO_TYPE(osc, timeouts);
566 LPROC_SEQ_FOPS_RO_TYPE(osc, state);
567
568 LPROC_SEQ_FOPS_WO_TYPE(osc, ping);
569
570 LPROC_SEQ_FOPS_RW_TYPE(osc, import);
571 LPROC_SEQ_FOPS_RW_TYPE(osc, pinger_recov);
572
573 struct lprocfs_seq_vars lprocfs_osc_obd_vars[] = {
574         { .name =       "uuid",
575           .fops =       &osc_uuid_fops                  },
576         { .name =       "ping",
577           .fops =       &osc_ping_fops,
578           .proc_mode =  0222                            },
579         { .name =       "connect_flags",
580           .fops =       &osc_connect_flags_fops         },
581         { .name =       "blocksize",
582           .fops =       &osc_blksize_fops               },
583         { .name =       "kbytestotal",
584           .fops =       &osc_kbytestotal_fops           },
585         { .name =       "kbytesfree",
586           .fops =       &osc_kbytesfree_fops            },
587         { .name =       "kbytesavail",
588           .fops =       &osc_kbytesavail_fops           },
589         { .name =       "filestotal",
590           .fops =       &osc_filestotal_fops            },
591         { .name =       "filesfree",
592           .fops =       &osc_filesfree_fops             },
593         { .name =       "ost_server_uuid",
594           .fops =       &osc_server_uuid_fops           },
595         { .name =       "ost_conn_uuid",
596           .fops =       &osc_conn_uuid_fops             },
597         { .name =       "active",
598           .fops =       &osc_active_fops                },
599         { .name =       "max_pages_per_rpc",
600           .fops =       &osc_obd_max_pages_per_rpc_fops },
601         { .name =       "max_rpcs_in_flight",
602           .fops =       &osc_max_rpcs_in_flight_fops    },
603         { .name =       "destroys_in_flight",
604           .fops =       &osc_destroys_in_flight_fops    },
605         { .name =       "max_dirty_mb",
606           .fops =       &osc_max_dirty_mb_fops          },
607         { .name =       "osc_cached_mb",
608           .fops =       &osc_cached_mb_fops             },
609         { .name =       "cur_dirty_bytes",
610           .fops =       &osc_cur_dirty_bytes_fops       },
611         { .name =       "cur_grant_bytes",
612           .fops =       &osc_cur_grant_bytes_fops       },
613         { .name =       "cur_lost_grant_bytes",
614           .fops =       &osc_cur_lost_grant_bytes_fops  },
615         { .name =       "grant_shrink_interval",
616           .fops =       &osc_grant_shrink_interval_fops },
617         { .name =       "checksums",
618           .fops =       &osc_checksum_fops              },
619         { .name =       "checksum_type",
620           .fops =       &osc_checksum_type_fops         },
621         { .name =       "resend_count",
622           .fops =       &osc_resend_count_fops          },
623         { .name =       "timeouts",
624           .fops =       &osc_timeouts_fops              },
625         { .name =       "contention_seconds",
626           .fops =       &osc_contention_seconds_fops    },
627         { .name =       "lockless_truncate",
628           .fops =       &osc_lockless_truncate_fops     },
629         { .name =       "import",
630           .fops =       &osc_import_fops                },
631         { .name =       "state",
632           .fops =       &osc_state_fops                 },
633         { .name =       "pinger_recov",
634           .fops =       &osc_pinger_recov_fops          },
635         { .name =       "unstable_stats",
636           .fops =       &osc_unstable_stats_fops        },
637         { 0 }
638 };
639
640 #define pct(a,b) (b ? a * 100 / b : 0)
641
642 static int osc_rpc_stats_seq_show(struct seq_file *seq, void *v)
643 {
644         struct timeval now;
645         struct obd_device *dev = seq->private;
646         struct client_obd *cli = &dev->u.cli;
647         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
648         int i;
649
650         do_gettimeofday(&now);
651
652         client_obd_list_lock(&cli->cl_loi_list_lock);
653
654         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
655                    now.tv_sec, now.tv_usec);
656         seq_printf(seq, "read RPCs in flight:  %d\n",
657                    cli->cl_r_in_flight);
658         seq_printf(seq, "write RPCs in flight: %d\n",
659                    cli->cl_w_in_flight);
660         seq_printf(seq, "pending write pages:  %d\n",
661                    atomic_read(&cli->cl_pending_w_pages));
662         seq_printf(seq, "pending read pages:   %d\n",
663                    atomic_read(&cli->cl_pending_r_pages));
664
665         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
666         seq_printf(seq, "pages per rpc         rpcs   %% cum %% |");
667         seq_printf(seq, "       rpcs   %% cum %%\n");
668
669         read_tot = lprocfs_oh_sum(&cli->cl_read_page_hist);
670         write_tot = lprocfs_oh_sum(&cli->cl_write_page_hist);
671
672         read_cum = 0;
673         write_cum = 0;
674         for (i = 0; i < OBD_HIST_MAX; i++) {
675                 unsigned long r = cli->cl_read_page_hist.oh_buckets[i];
676                 unsigned long w = cli->cl_write_page_hist.oh_buckets[i];
677
678                 read_cum += r;
679                 write_cum += w;
680                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
681                            1 << i, r, pct(r, read_tot),
682                            pct(read_cum, read_tot), w,
683                            pct(w, write_tot),
684                            pct(write_cum, write_tot));
685                 if (read_cum == read_tot && write_cum == write_tot)
686                         break;
687         }
688
689         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
690         seq_printf(seq, "rpcs in flight        rpcs   %% cum %% |");
691         seq_printf(seq, "       rpcs   %% cum %%\n");
692
693         read_tot = lprocfs_oh_sum(&cli->cl_read_rpc_hist);
694         write_tot = lprocfs_oh_sum(&cli->cl_write_rpc_hist);
695
696         read_cum = 0;
697         write_cum = 0;
698         for (i = 0; i < OBD_HIST_MAX; i++) {
699                 unsigned long r = cli->cl_read_rpc_hist.oh_buckets[i];
700                 unsigned long w = cli->cl_write_rpc_hist.oh_buckets[i];
701                 read_cum += r;
702                 write_cum += w;
703                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
704                                  i, r, pct(r, read_tot),
705                                  pct(read_cum, read_tot), w,
706                                  pct(w, write_tot),
707                                  pct(write_cum, write_tot));
708                 if (read_cum == read_tot && write_cum == write_tot)
709                         break;
710         }
711
712         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
713         seq_printf(seq, "offset                rpcs   %% cum %% |");
714         seq_printf(seq, "       rpcs   %% cum %%\n");
715
716         read_tot = lprocfs_oh_sum(&cli->cl_read_offset_hist);
717         write_tot = lprocfs_oh_sum(&cli->cl_write_offset_hist);
718
719         read_cum = 0;
720         write_cum = 0;
721         for (i = 0; i < OBD_HIST_MAX; i++) {
722                 unsigned long r = cli->cl_read_offset_hist.oh_buckets[i];
723                 unsigned long w = cli->cl_write_offset_hist.oh_buckets[i];
724                 read_cum += r;
725                 write_cum += w;
726                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
727                            (i == 0) ? 0 : 1 << (i - 1),
728                            r, pct(r, read_tot), pct(read_cum, read_tot),
729                            w, pct(w, write_tot), pct(write_cum, write_tot));
730                 if (read_cum == read_tot && write_cum == write_tot)
731                         break;
732         }
733
734         client_obd_list_unlock(&cli->cl_loi_list_lock);
735
736         return 0;
737 }
738 #undef pct
739
740 static ssize_t osc_rpc_stats_seq_write(struct file *file,
741                                        const char __user *buf,
742                                        size_t len, loff_t *off)
743 {
744         struct seq_file *seq = file->private_data;
745         struct obd_device *dev = seq->private;
746         struct client_obd *cli = &dev->u.cli;
747
748         lprocfs_oh_clear(&cli->cl_read_rpc_hist);
749         lprocfs_oh_clear(&cli->cl_write_rpc_hist);
750         lprocfs_oh_clear(&cli->cl_read_page_hist);
751         lprocfs_oh_clear(&cli->cl_write_page_hist);
752         lprocfs_oh_clear(&cli->cl_read_offset_hist);
753         lprocfs_oh_clear(&cli->cl_write_offset_hist);
754
755         return len;
756 }
757 LPROC_SEQ_FOPS(osc_rpc_stats);
758
759 static int osc_stats_seq_show(struct seq_file *seq, void *v)
760 {
761         struct timeval now;
762         struct obd_device *dev = seq->private;
763         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
764
765         do_gettimeofday(&now);
766
767         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
768                    now.tv_sec, now.tv_usec);
769         seq_printf(seq, "lockless_write_bytes\t\t"LPU64"\n",
770                    stats->os_lockless_writes);
771         seq_printf(seq, "lockless_read_bytes\t\t"LPU64"\n",
772                    stats->os_lockless_reads);
773         seq_printf(seq, "lockless_truncate\t\t"LPU64"\n",
774                    stats->os_lockless_truncates);
775         return 0;
776 }
777
778 static ssize_t osc_stats_seq_write(struct file *file,
779                                    const char __user *buf,
780                                    size_t len, loff_t *off)
781 {
782         struct seq_file *seq = file->private_data;
783         struct obd_device *dev = seq->private;
784         struct osc_stats *stats = &obd2osc_dev(dev)->od_stats;
785
786         memset(stats, 0, sizeof(*stats));
787         return len;
788 }
789
790 LPROC_SEQ_FOPS(osc_stats);
791
792 int lproc_osc_attach_seqstat(struct obd_device *dev)
793 {
794         int rc;
795
796         rc = lprocfs_seq_create(dev->obd_proc_entry, "osc_stats", 0644,
797                                 &osc_stats_fops, dev);
798         if (rc == 0)
799                 rc = lprocfs_obd_seq_create(dev, "rpc_stats", 0644,
800                                             &osc_rpc_stats_fops, dev);
801
802         return rc;
803 }
804 #endif /* LPROCFS */