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