Whamcloud - gitweb
LU-1346 libcfs: replace cfs_ memory wrappers
[fs/lustre-release.git] / lustre / llite / lproc_llite.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) 2012, 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_LLITE
37
38 #include <linux/version.h>
39 #include <lustre_lite.h>
40 #include <lprocfs_status.h>
41 #include <linux/seq_file.h>
42 #include <obd_support.h>
43
44 #include "llite_internal.h"
45
46 struct proc_dir_entry *proc_lustre_fs_root;
47
48 #ifdef LPROCFS
49 /* /proc/lustre/llite mount point registration */
50 extern struct file_operations vvp_dump_pgcache_file_ops;
51 struct file_operations ll_rw_extents_stats_fops;
52 struct file_operations ll_rw_extents_stats_pp_fops;
53 struct file_operations ll_rw_offset_stats_fops;
54
55 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
56                          int *eof, void *data)
57 {
58         struct super_block *sb = (struct super_block *)data;
59         struct obd_statfs osfs;
60         int rc;
61
62         LASSERT(sb != NULL);
63         rc = ll_statfs_internal(sb, &osfs,
64                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
65                                 OBD_STATFS_NODELAY);
66         if (!rc) {
67               *eof = 1;
68               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
69         }
70
71         return rc;
72 }
73
74 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
75                              int *eof, void *data)
76 {
77         struct super_block *sb = (struct super_block *)data;
78         struct obd_statfs osfs;
79         int rc;
80
81         LASSERT(sb != NULL);
82         rc = ll_statfs_internal(sb, &osfs,
83                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
84                                 OBD_STATFS_NODELAY);
85         if (!rc) {
86                 __u32 blk_size = osfs.os_bsize >> 10;
87                 __u64 result = osfs.os_blocks;
88
89                 while (blk_size >>= 1)
90                         result <<= 1;
91
92                 *eof = 1;
93                 rc = snprintf(page, count, LPU64"\n", result);
94         }
95         return rc;
96
97 }
98
99 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
100                             int *eof, void *data)
101 {
102         struct super_block *sb = (struct super_block *)data;
103         struct obd_statfs osfs;
104         int rc;
105
106         LASSERT(sb != NULL);
107         rc = ll_statfs_internal(sb, &osfs,
108                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
109                                 OBD_STATFS_NODELAY);
110         if (!rc) {
111                 __u32 blk_size = osfs.os_bsize >> 10;
112                 __u64 result = osfs.os_bfree;
113
114                 while (blk_size >>= 1)
115                         result <<= 1;
116
117                 *eof = 1;
118                 rc = snprintf(page, count, LPU64"\n", result);
119         }
120         return rc;
121 }
122
123 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
124                              int *eof, void *data)
125 {
126         struct super_block *sb = (struct super_block *)data;
127         struct obd_statfs osfs;
128         int rc;
129
130         LASSERT(sb != NULL);
131         rc = ll_statfs_internal(sb, &osfs,
132                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
133                                 OBD_STATFS_NODELAY);
134         if (!rc) {
135                 __u32 blk_size = osfs.os_bsize >> 10;
136                 __u64 result = osfs.os_bavail;
137
138                 while (blk_size >>= 1)
139                         result <<= 1;
140
141                 *eof = 1;
142                 rc = snprintf(page, count, LPU64"\n", result);
143         }
144         return rc;
145 }
146
147 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
148                             int *eof, void *data)
149 {
150         struct super_block *sb = (struct super_block *)data;
151         struct obd_statfs osfs;
152         int rc;
153
154         LASSERT(sb != NULL);
155         rc = ll_statfs_internal(sb, &osfs,
156                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
157                                 OBD_STATFS_NODELAY);
158         if (!rc) {
159                  *eof = 1;
160                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
161         }
162         return rc;
163 }
164
165 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
166                            int *eof, void *data)
167 {
168         struct super_block *sb = (struct super_block *)data;
169         struct obd_statfs osfs;
170         int rc;
171
172         LASSERT(sb != NULL);
173         rc = ll_statfs_internal(sb, &osfs,
174                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
175                                 OBD_STATFS_NODELAY);
176         if (!rc) {
177                  *eof = 1;
178                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
179         }
180         return rc;
181
182 }
183
184 static int ll_rd_client_type(char *page, char **start, off_t off, int count,
185                             int *eof, void *data)
186 {
187         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)data);
188         int rc;
189
190         LASSERT(sbi != NULL);
191
192         *eof = 1;
193         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
194                 rc = snprintf(page, count, "remote client\n");
195         else
196                 rc = snprintf(page, count, "local client\n");
197
198         return rc;
199 }
200
201 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
202                         int *eof, void *data)
203 {
204         struct super_block *sb = (struct super_block*)data;
205
206         LASSERT(sb != NULL);
207         *eof = 1;
208         return snprintf(page, count, "%s\n", sb->s_type->name);
209 }
210
211 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
212                          int *eof, void *data)
213 {
214         struct super_block *sb = (struct super_block *)data;
215
216         LASSERT(sb != NULL);
217         *eof = 1;
218         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
219 }
220
221 static int ll_rd_site_stats(char *page, char **start, off_t off,
222                             int count, int *eof, void *data)
223 {
224         struct super_block *sb = data;
225
226         /*
227          * See description of statistical counters in struct cl_site, and
228          * struct lu_site.
229          */
230         return cl_site_stats_print(lu2cl_site(ll_s2sbi(sb)->ll_site),
231                                    page, count);
232 }
233
234 static int ll_rd_max_readahead_mb(char *page, char **start, off_t off,
235                                    int count, int *eof, void *data)
236 {
237         struct super_block *sb = data;
238         struct ll_sb_info *sbi = ll_s2sbi(sb);
239         long pages_number;
240         int mult;
241
242         spin_lock(&sbi->ll_lock);
243         pages_number = sbi->ll_ra_info.ra_max_pages;
244         spin_unlock(&sbi->ll_lock);
245
246         mult = 1 << (20 - PAGE_CACHE_SHIFT);
247         return lprocfs_read_frac_helper(page, count, pages_number, mult);
248 }
249
250 static int ll_wr_max_readahead_mb(struct file *file, const char *buffer,
251                                   unsigned long count, void *data)
252 {
253         struct super_block *sb = data;
254         struct ll_sb_info *sbi = ll_s2sbi(sb);
255         int mult, rc, pages_number;
256
257         mult = 1 << (20 - PAGE_CACHE_SHIFT);
258         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
259         if (rc)
260                 return rc;
261
262         if (pages_number < 0 || pages_number > num_physpages / 2) {
263                 /* 1/2 of RAM */
264                 CERROR("can't set file readahead more than %lu MB\n",
265                        num_physpages >> (20 - PAGE_CACHE_SHIFT + 1));
266                 return -ERANGE;
267         }
268
269         spin_lock(&sbi->ll_lock);
270         sbi->ll_ra_info.ra_max_pages = pages_number;
271         spin_unlock(&sbi->ll_lock);
272
273         return count;
274 }
275
276 static int ll_rd_max_readahead_per_file_mb(char *page, char **start, off_t off,
277                                            int count, int *eof, void *data)
278 {
279         struct super_block *sb = data;
280         struct ll_sb_info *sbi = ll_s2sbi(sb);
281         long pages_number;
282         int mult;
283
284         spin_lock(&sbi->ll_lock);
285         pages_number = sbi->ll_ra_info.ra_max_pages_per_file;
286         spin_unlock(&sbi->ll_lock);
287
288         mult = 1 << (20 - PAGE_CACHE_SHIFT);
289         return lprocfs_read_frac_helper(page, count, pages_number, mult);
290 }
291
292 static int ll_wr_max_readahead_per_file_mb(struct file *file, const char *buffer,
293                                           unsigned long count, void *data)
294 {
295         struct super_block *sb = data;
296         struct ll_sb_info *sbi = ll_s2sbi(sb);
297         int mult, rc, pages_number;
298
299         mult = 1 << (20 - PAGE_CACHE_SHIFT);
300         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
301         if (rc)
302                 return rc;
303
304         if (pages_number < 0 ||
305                 pages_number > sbi->ll_ra_info.ra_max_pages) {
306                 CERROR("can't set file readahead more than"
307                        "max_read_ahead_mb %lu MB\n",
308                        sbi->ll_ra_info.ra_max_pages);
309                 return -ERANGE;
310         }
311
312         spin_lock(&sbi->ll_lock);
313         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
314         spin_unlock(&sbi->ll_lock);
315
316         return count;
317 }
318
319 static int ll_rd_max_read_ahead_whole_mb(char *page, char **start, off_t off,
320                                          int count, int *eof, void *data)
321 {
322         struct super_block *sb = data;
323         struct ll_sb_info *sbi = ll_s2sbi(sb);
324         long pages_number;
325         int mult;
326
327         spin_lock(&sbi->ll_lock);
328         pages_number = sbi->ll_ra_info.ra_max_read_ahead_whole_pages;
329         spin_unlock(&sbi->ll_lock);
330
331         mult = 1 << (20 - PAGE_CACHE_SHIFT);
332         return lprocfs_read_frac_helper(page, count, pages_number, mult);
333 }
334
335 static int ll_wr_max_read_ahead_whole_mb(struct file *file, const char *buffer,
336                                          unsigned long count, void *data)
337 {
338         struct super_block *sb = data;
339         struct ll_sb_info *sbi = ll_s2sbi(sb);
340         int mult, rc, pages_number;
341
342         mult = 1 << (20 - PAGE_CACHE_SHIFT);
343         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
344         if (rc)
345                 return rc;
346
347         /* Cap this at the current max readahead window size, the readahead
348          * algorithm does this anyway so it's pointless to set it larger. */
349         if (pages_number < 0 ||
350             pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
351                 CERROR("can't set max_read_ahead_whole_mb more than "
352                        "max_read_ahead_per_file_mb: %lu\n",
353                         sbi->ll_ra_info.ra_max_pages_per_file >>
354                         (20 - PAGE_CACHE_SHIFT));
355                 return -ERANGE;
356         }
357
358         spin_lock(&sbi->ll_lock);
359         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
360         spin_unlock(&sbi->ll_lock);
361
362         return count;
363 }
364
365 static int ll_rd_max_cached_mb(char *page, char **start, off_t off,
366                                int count, int *eof, void *data)
367 {
368         struct super_block     *sb    = data;
369         struct ll_sb_info      *sbi   = ll_s2sbi(sb);
370         struct cl_client_cache *cache = &sbi->ll_cache;
371         int shift = 20 - PAGE_CACHE_SHIFT;
372         int max_cached_mb;
373         int unused_mb;
374
375         *eof = 1;
376         max_cached_mb = cache->ccc_lru_max >> shift;
377         unused_mb = cfs_atomic_read(&cache->ccc_lru_left) >> shift;
378         return snprintf(page, count,
379                         "users: %d\n"
380                         "max_cached_mb: %d\n"
381                         "used_mb: %d\n"
382                         "unused_mb: %d\n"
383                         "reclaim_count: %u\n",
384                         cfs_atomic_read(&cache->ccc_users),
385                         max_cached_mb,
386                         max_cached_mb - unused_mb,
387                         unused_mb,
388                         cache->ccc_lru_shrinkers);
389 }
390
391 static int ll_wr_max_cached_mb(struct file *file, const char *buffer,
392                                unsigned long count, void *data)
393 {
394         struct super_block *sb = data;
395         struct ll_sb_info *sbi = ll_s2sbi(sb);
396         struct cl_client_cache *cache = &sbi->ll_cache;
397         int mult, rc, pages_number;
398         int diff = 0;
399         int nrpages = 0;
400         ENTRY;
401
402         mult = 1 << (20 - PAGE_CACHE_SHIFT);
403         buffer = lprocfs_find_named_value(buffer, "max_cached_mb:", &count);
404         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
405         if (rc)
406                 RETURN(rc);
407
408         if (pages_number < 0 || pages_number > num_physpages) {
409                 CERROR("%s: can't set max cache more than %lu MB\n",
410                        ll_get_fsname(sb, NULL, 0),
411                        num_physpages >> (20 - PAGE_CACHE_SHIFT));
412                 RETURN(-ERANGE);
413         }
414
415         if (sbi->ll_dt_exp == NULL)
416                 RETURN(-ENODEV);
417
418         spin_lock(&sbi->ll_lock);
419         diff = pages_number - cache->ccc_lru_max;
420         spin_unlock(&sbi->ll_lock);
421
422         /* easy - add more LRU slots. */
423         if (diff >= 0) {
424                 cfs_atomic_add(diff, &cache->ccc_lru_left);
425                 GOTO(out, rc = 0);
426         }
427
428         diff = -diff;
429         while (diff > 0) {
430                 int tmp;
431
432                 /* reduce LRU budget from free slots. */
433                 do {
434                         int ov, nv;
435
436                         ov = cfs_atomic_read(&cache->ccc_lru_left);
437                         if (ov == 0)
438                                 break;
439
440                         nv = ov > diff ? ov - diff : 0;
441                         rc = cfs_atomic_cmpxchg(&cache->ccc_lru_left, ov, nv);
442                         if (likely(ov == rc)) {
443                                 diff -= ov - nv;
444                                 nrpages += ov - nv;
445                                 break;
446                         }
447                 } while (1);
448
449                 if (diff <= 0)
450                         break;
451
452                 /* difficult - have to ask OSCs to drop LRU slots. */
453                 tmp = diff << 1;
454                 rc = obd_set_info_async(NULL, sbi->ll_dt_exp,
455                                 sizeof(KEY_CACHE_LRU_SHRINK),
456                                 KEY_CACHE_LRU_SHRINK,
457                                 sizeof(tmp), &tmp, NULL);
458                 if (rc < 0)
459                         break;
460         }
461
462 out:
463         if (rc >= 0) {
464                 spin_lock(&sbi->ll_lock);
465                 cache->ccc_lru_max = pages_number;
466                 spin_unlock(&sbi->ll_lock);
467                 rc = count;
468         } else {
469                 cfs_atomic_add(nrpages, &cache->ccc_lru_left);
470         }
471         return rc;
472 }
473
474 static int ll_rd_checksum(char *page, char **start, off_t off,
475                           int count, int *eof, void *data)
476 {
477         struct super_block *sb = data;
478         struct ll_sb_info *sbi = ll_s2sbi(sb);
479
480         return snprintf(page, count, "%u\n",
481                         (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
482 }
483
484 static int ll_wr_checksum(struct file *file, const char *buffer,
485                           unsigned long count, void *data)
486 {
487         struct super_block *sb = data;
488         struct ll_sb_info *sbi = ll_s2sbi(sb);
489         int val, rc;
490
491         if (!sbi->ll_dt_exp)
492                 /* Not set up yet */
493                 return -EAGAIN;
494
495         rc = lprocfs_write_helper(buffer, count, &val);
496         if (rc)
497                 return rc;
498         if (val)
499                 sbi->ll_flags |= LL_SBI_CHECKSUM;
500         else
501                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
502
503         rc = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
504                                 KEY_CHECKSUM, sizeof(val), &val, NULL);
505         if (rc)
506                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
507
508         return count;
509 }
510
511 static int ll_rd_max_rw_chunk(char *page, char **start, off_t off,
512                           int count, int *eof, void *data)
513 {
514         struct super_block *sb = data;
515
516         return snprintf(page, count, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
517 }
518
519 static int ll_wr_max_rw_chunk(struct file *file, const char *buffer,
520                           unsigned long count, void *data)
521 {
522         struct super_block *sb = data;
523         int rc, val;
524
525         rc = lprocfs_write_helper(buffer, count, &val);
526         if (rc)
527                 return rc;
528         ll_s2sbi(sb)->ll_max_rw_chunk = val;
529         return count;
530 }
531
532 static int ll_rd_track_id(char *page, int count, void *data,
533                           enum stats_track_type type)
534 {
535         struct super_block *sb = data;
536
537         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
538                 return snprintf(page, count, "%d\n",
539                                 ll_s2sbi(sb)->ll_stats_track_id);
540
541         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
542                 return snprintf(page, count, "0 (all)\n");
543         } else {
544                 return snprintf(page, count, "untracked\n");
545         }
546 }
547
548 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
549                           enum stats_track_type type)
550 {
551         struct super_block *sb = data;
552         int rc, pid;
553
554         rc = lprocfs_write_helper(buffer, count, &pid);
555         if (rc)
556                 return rc;
557         ll_s2sbi(sb)->ll_stats_track_id = pid;
558         if (pid == 0)
559                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
560         else
561                 ll_s2sbi(sb)->ll_stats_track_type = type;
562         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
563         return count;
564 }
565
566 static int ll_rd_track_pid(char *page, char **start, off_t off,
567                           int count, int *eof, void *data)
568 {
569         return (ll_rd_track_id(page, count, data, STATS_TRACK_PID));
570 }
571
572 static int ll_wr_track_pid(struct file *file, const char *buffer,
573                           unsigned long count, void *data)
574 {
575         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PID));
576 }
577
578 static int ll_rd_track_ppid(char *page, char **start, off_t off,
579                           int count, int *eof, void *data)
580 {
581         return (ll_rd_track_id(page, count, data, STATS_TRACK_PPID));
582 }
583
584 static int ll_wr_track_ppid(struct file *file, const char *buffer,
585                           unsigned long count, void *data)
586 {
587         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PPID));
588 }
589
590 static int ll_rd_track_gid(char *page, char **start, off_t off,
591                           int count, int *eof, void *data)
592 {
593         return (ll_rd_track_id(page, count, data, STATS_TRACK_GID));
594 }
595
596 static int ll_wr_track_gid(struct file *file, const char *buffer,
597                           unsigned long count, void *data)
598 {
599         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_GID));
600 }
601
602 static int ll_rd_statahead_max(char *page, char **start, off_t off,
603                                int count, int *eof, void *data)
604 {
605         struct super_block *sb = data;
606         struct ll_sb_info *sbi = ll_s2sbi(sb);
607
608         return snprintf(page, count, "%u\n", sbi->ll_sa_max);
609 }
610
611 static int ll_wr_statahead_max(struct file *file, const char *buffer,
612                                unsigned long count, void *data)
613 {
614         struct super_block *sb = data;
615         struct ll_sb_info *sbi = ll_s2sbi(sb);
616         int val, rc;
617
618         rc = lprocfs_write_helper(buffer, count, &val);
619         if (rc)
620                 return rc;
621
622         if (val >= 0 && val <= LL_SA_RPC_MAX)
623                 sbi->ll_sa_max = val;
624         else
625                 CERROR("Bad statahead_max value %d. Valid values are in the "
626                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
627
628         return count;
629 }
630
631 static int ll_rd_statahead_agl(char *page, char **start, off_t off,
632                                int count, int *eof, void *data)
633 {
634         struct super_block *sb = data;
635         struct ll_sb_info *sbi = ll_s2sbi(sb);
636
637         return snprintf(page, count, "%u\n",
638                         sbi->ll_flags & LL_SBI_AGL_ENABLED ? 1 : 0);
639 }
640
641 static int ll_wr_statahead_agl(struct file *file, const char *buffer,
642                                unsigned long count, void *data)
643 {
644         struct super_block *sb = data;
645         struct ll_sb_info *sbi = ll_s2sbi(sb);
646         int val, rc;
647
648         rc = lprocfs_write_helper(buffer, count, &val);
649         if (rc)
650                 return rc;
651
652         if (val)
653                 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
654         else
655                 sbi->ll_flags &= ~LL_SBI_AGL_ENABLED;
656
657         return count;
658 }
659
660 static int ll_rd_statahead_stats(char *page, char **start, off_t off,
661                                  int count, int *eof, void *data)
662 {
663         struct super_block *sb = data;
664         struct ll_sb_info *sbi = ll_s2sbi(sb);
665
666         return snprintf(page, count,
667                         "statahead total: %u\n"
668                         "statahead wrong: %u\n"
669                         "agl total: %u\n",
670                         atomic_read(&sbi->ll_sa_total),
671                         atomic_read(&sbi->ll_sa_wrong),
672                         atomic_read(&sbi->ll_agl_total));
673 }
674
675 static int ll_rd_lazystatfs(char *page, char **start, off_t off,
676                             int count, int *eof, void *data)
677 {
678         struct super_block *sb = data;
679         struct ll_sb_info *sbi = ll_s2sbi(sb);
680
681         return snprintf(page, count, "%u\n",
682                         (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
683 }
684
685 static int ll_wr_lazystatfs(struct file *file, const char *buffer,
686                             unsigned long count, void *data)
687 {
688         struct super_block *sb = data;
689         struct ll_sb_info *sbi = ll_s2sbi(sb);
690         int val, rc;
691
692         rc = lprocfs_write_helper(buffer, count, &val);
693         if (rc)
694                 return rc;
695
696         if (val)
697                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
698         else
699                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
700
701         return count;
702 }
703
704 static int ll_rd_maxea_size(char *page, char **start, off_t off,
705                             int count, int *eof, void *data)
706 {
707         struct super_block *sb = data;
708         struct ll_sb_info *sbi = ll_s2sbi(sb);
709         unsigned int ealen;
710         int rc;
711
712         rc = ll_get_max_mdsize(sbi, &ealen);
713         if (rc)
714                 return rc;
715
716         return snprintf(page, count, "%u\n", ealen);
717 }
718
719 static int ll_rd_sbi_flags(char *page, char **start, off_t off,
720                                 int count, int *eof, void *data)
721 {
722         const char *str[] = LL_SBI_FLAGS;
723         struct super_block *sb = data;
724         int flags = ll_s2sbi(sb)->ll_flags;
725         int i = 0;
726         int rc = 0;
727
728         while (flags != 0) {
729                 if (ARRAY_SIZE(str) <= i) {
730                         CERROR("%s: Revise array LL_SBI_FLAGS to match sbi "
731                                 "flags please.\n", ll_get_fsname(sb, NULL, 0));
732                         return -EINVAL;
733                 }
734
735                 if (flags & 0x1)
736                         rc += snprintf(page + rc, count - rc, "%s ", str[i]);
737                 flags >>= 1;
738                 ++i;
739         }
740         if (rc > 0)
741                 rc += snprintf(page + rc, count - rc, "\b\n");
742         return rc;
743 }
744
745 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
746         { "uuid",         ll_rd_sb_uuid,          0, 0 },
747         //{ "mntpt_path",   ll_rd_path,             0, 0 },
748         { "fstype",       ll_rd_fstype,           0, 0 },
749         { "site",         ll_rd_site_stats,       0, 0 },
750         { "blocksize",    ll_rd_blksize,          0, 0 },
751         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
752         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
753         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
754         { "filestotal",   ll_rd_filestotal,       0, 0 },
755         { "filesfree",    ll_rd_filesfree,        0, 0 },
756         { "client_type",  ll_rd_client_type,      0, 0 },
757         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
758         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
759                                ll_wr_max_readahead_mb, 0 },
760         { "max_read_ahead_per_file_mb", ll_rd_max_readahead_per_file_mb,
761                                         ll_wr_max_readahead_per_file_mb, 0 },
762         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
763                                      ll_wr_max_read_ahead_whole_mb, 0 },
764         { "max_cached_mb",    ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
765         { "checksum_pages",   ll_rd_checksum, ll_wr_checksum, 0 },
766         { "max_rw_chunk",     ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
767         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
768         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
769         { "stats_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
770         { "statahead_max",    ll_rd_statahead_max, ll_wr_statahead_max, 0 },
771         { "statahead_agl",    ll_rd_statahead_agl, ll_wr_statahead_agl, 0 },
772         { "statahead_stats",  ll_rd_statahead_stats, 0, 0 },
773         { "lazystatfs",       ll_rd_lazystatfs, ll_wr_lazystatfs, 0 },
774         { "max_easize",       ll_rd_maxea_size, 0, 0 },
775         { "sbi_flags",        ll_rd_sbi_flags, 0, 0 },
776         { 0 }
777 };
778
779 #define MAX_STRING_SIZE 128
780
781 struct llite_file_opcode {
782         __u32       opcode;
783         __u32       type;
784         const char *opname;
785 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
786         /* file operation */
787         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
788         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
789         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
790                                    "read_bytes" },
791         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
792                                    "write_bytes" },
793         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
794                                    "brw_read" },
795         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
796                                    "brw_write" },
797         { LPROC_LL_OSC_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
798                                    "osc_read" },
799         { LPROC_LL_OSC_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
800                                    "osc_write" },
801         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
802         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
803         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
804         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
805         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
806         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
807         { LPROC_LL_READDIR,        LPROCFS_TYPE_REGS, "readdir" },
808         /* inode operation */
809         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
810         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
811         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
812         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
813         /* dir inode operation */
814         { LPROC_LL_CREATE,         LPROCFS_TYPE_REGS, "create" },
815         { LPROC_LL_LINK,           LPROCFS_TYPE_REGS, "link" },
816         { LPROC_LL_UNLINK,         LPROCFS_TYPE_REGS, "unlink" },
817         { LPROC_LL_SYMLINK,        LPROCFS_TYPE_REGS, "symlink" },
818         { LPROC_LL_MKDIR,          LPROCFS_TYPE_REGS, "mkdir" },
819         { LPROC_LL_RMDIR,          LPROCFS_TYPE_REGS, "rmdir" },
820         { LPROC_LL_MKNOD,          LPROCFS_TYPE_REGS, "mknod" },
821         { LPROC_LL_RENAME,         LPROCFS_TYPE_REGS, "rename" },
822         /* special inode operation */
823         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
824         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
825         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
826         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
827         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
828         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
829         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
830 };
831
832 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
833 {
834         if (!sbi->ll_stats)
835                 return;
836         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
837                 lprocfs_counter_add(sbi->ll_stats, op, count);
838         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
839                  sbi->ll_stats_track_id == current->pid)
840                 lprocfs_counter_add(sbi->ll_stats, op, count);
841         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
842                  sbi->ll_stats_track_id == current->parent->pid)
843                 lprocfs_counter_add(sbi->ll_stats, op, count);
844         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
845                  sbi->ll_stats_track_id == cfs_curproc_gid())
846                 lprocfs_counter_add(sbi->ll_stats, op, count);
847 }
848 EXPORT_SYMBOL(ll_stats_ops_tally);
849
850 static const char *ra_stat_string[] = {
851         [RA_STAT_HIT] = "hits",
852         [RA_STAT_MISS] = "misses",
853         [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
854         [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
855         [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
856         [RA_STAT_FAILED_MATCH] = "failed lock match",
857         [RA_STAT_DISCARDED] = "read but discarded",
858         [RA_STAT_ZERO_LEN] = "zero length file",
859         [RA_STAT_ZERO_WINDOW] = "zero size window",
860         [RA_STAT_EOF] = "read-ahead to EOF",
861         [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
862         [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
863 };
864
865
866 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
867                                 struct super_block *sb, char *osc, char *mdc)
868 {
869         struct lprocfs_vars lvars[2];
870         struct lustre_sb_info *lsi = s2lsi(sb);
871         struct ll_sb_info *sbi = ll_s2sbi(sb);
872         struct obd_device *obd;
873         char name[MAX_STRING_SIZE + 1], *ptr;
874         int err, id, len, rc;
875         ENTRY;
876
877         memset(lvars, 0, sizeof(lvars));
878
879         name[MAX_STRING_SIZE] = '\0';
880         lvars[0].name = name;
881
882         LASSERT(sbi != NULL);
883         LASSERT(mdc != NULL);
884         LASSERT(osc != NULL);
885
886         /* Get fsname */
887         len = strlen(lsi->lsi_lmd->lmd_profile);
888         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
889         if (ptr && (strcmp(ptr, "-client") == 0))
890                 len -= 7;
891
892         /* Mount info */
893         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
894                  lsi->lsi_lmd->lmd_profile, sb);
895
896         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
897         if (IS_ERR(sbi->ll_proc_root)) {
898                 err = PTR_ERR(sbi->ll_proc_root);
899                 sbi->ll_proc_root = NULL;
900                 RETURN(err);
901         }
902
903         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
904                                 &vvp_dump_pgcache_file_ops, sbi);
905         if (rc)
906                 CWARN("Error adding the dump_page_cache file\n");
907
908         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
909                                 &ll_rw_extents_stats_fops, sbi);
910         if (rc)
911                 CWARN("Error adding the extent_stats file\n");
912
913         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
914                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
915         if (rc)
916                 CWARN("Error adding the extents_stats_per_process file\n");
917
918         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
919                                 &ll_rw_offset_stats_fops, sbi);
920         if (rc)
921                 CWARN("Error adding the offset_stats file\n");
922
923         /* File operations stats */
924         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
925                                             LPROCFS_STATS_FLAG_NONE);
926         if (sbi->ll_stats == NULL)
927                 GOTO(out, err = -ENOMEM);
928         /* do counter init */
929         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
930                 __u32 type = llite_opcode_table[id].type;
931                 void *ptr = NULL;
932                 if (type & LPROCFS_TYPE_REGS)
933                         ptr = "regs";
934                 else if (type & LPROCFS_TYPE_BYTES)
935                         ptr = "bytes";
936                 else if (type & LPROCFS_TYPE_PAGES)
937                         ptr = "pages";
938                 lprocfs_counter_init(sbi->ll_stats,
939                                      llite_opcode_table[id].opcode,
940                                      (type & LPROCFS_CNTR_AVGMINMAX),
941                                      llite_opcode_table[id].opname, ptr);
942         }
943         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
944         if (err)
945                 GOTO(out, err);
946
947         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
948                                                LPROCFS_STATS_FLAG_NONE);
949         if (sbi->ll_ra_stats == NULL)
950                 GOTO(out, err = -ENOMEM);
951
952         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
953                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
954                                      ra_stat_string[id], "pages");
955         err = lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
956                                      sbi->ll_ra_stats);
957         if (err)
958                 GOTO(out, err);
959
960
961         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
962         if (err)
963                 GOTO(out, err);
964
965         /* MDC info */
966         obd = class_name2obd(mdc);
967
968         LASSERT(obd != NULL);
969         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
970         LASSERT(obd->obd_type->typ_name != NULL);
971
972         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
973                  obd->obd_type->typ_name);
974         lvars[0].read_fptr = lprocfs_rd_name;
975         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
976         if (err)
977                 GOTO(out, err);
978
979         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
980         lvars[0].read_fptr = lprocfs_rd_uuid;
981         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
982         if (err)
983                 GOTO(out, err);
984
985         /* OSC */
986         obd = class_name2obd(osc);
987
988         LASSERT(obd != NULL);
989         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
990         LASSERT(obd->obd_type->typ_name != NULL);
991
992         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
993                  obd->obd_type->typ_name);
994         lvars[0].read_fptr = lprocfs_rd_name;
995         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
996         if (err)
997                 GOTO(out, err);
998
999         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
1000         lvars[0].read_fptr = lprocfs_rd_uuid;
1001         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
1002 out:
1003         if (err) {
1004                 lprocfs_remove(&sbi->ll_proc_root);
1005                 lprocfs_free_stats(&sbi->ll_ra_stats);
1006                 lprocfs_free_stats(&sbi->ll_stats);
1007         }
1008         RETURN(err);
1009 }
1010
1011 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
1012 {
1013         if (sbi->ll_proc_root) {
1014                 lprocfs_remove(&sbi->ll_proc_root);
1015                 lprocfs_free_stats(&sbi->ll_ra_stats);
1016                 lprocfs_free_stats(&sbi->ll_stats);
1017         }
1018 }
1019 #undef MAX_STRING_SIZE
1020
1021 #define pct(a,b) (b ? a * 100 / b : 0)
1022
1023 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1024                                    struct seq_file *seq, int which)
1025 {
1026         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1027         unsigned long start, end, r, w;
1028         char *unitp = "KMGTPEZY";
1029         int i, units = 10;
1030         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1031
1032         read_cum = 0;
1033         write_cum = 0;
1034         start = 0;
1035
1036         for(i = 0; i < LL_HIST_MAX; i++) {
1037                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1038                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1039         }
1040
1041         for(i = 0; i < LL_HIST_MAX; i++) {
1042                 r = pp_info->pp_r_hist.oh_buckets[i];
1043                 w = pp_info->pp_w_hist.oh_buckets[i];
1044                 read_cum += r;
1045                 write_cum += w;
1046                 end = 1 << (i + LL_HIST_START - units);
1047                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1048                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1049                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1050                            r, pct(r, read_tot), pct(read_cum, read_tot),
1051                            w, pct(w, write_tot), pct(write_cum, write_tot));
1052                 start = end;
1053                 if (start == 1<<10) {
1054                         start = 1;
1055                         units += 10;
1056                         unitp++;
1057                 }
1058                 if (read_cum == read_tot && write_cum == write_tot)
1059                         break;
1060         }
1061 }
1062
1063 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1064 {
1065         struct timeval now;
1066         struct ll_sb_info *sbi = seq->private;
1067         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1068         int k;
1069
1070         cfs_gettimeofday(&now);
1071
1072         if (!sbi->ll_rw_stats_on) {
1073                 seq_printf(seq, "disabled\n"
1074                                 "write anything in this file to activate, "
1075                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1076                 return 0;
1077         }
1078         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1079                    now.tv_sec, now.tv_usec);
1080         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1081         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1082                    "extents", "calls", "%", "cum%",
1083                    "calls", "%", "cum%");
1084         spin_lock(&sbi->ll_pp_extent_lock);
1085         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1086                 if (io_extents->pp_extents[k].pid != 0) {
1087                         seq_printf(seq, "\nPID: %d\n",
1088                                    io_extents->pp_extents[k].pid);
1089                         ll_display_extents_info(io_extents, seq, k);
1090                 }
1091         }
1092         spin_unlock(&sbi->ll_pp_extent_lock);
1093         return 0;
1094 }
1095
1096 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1097                                                 const char *buf, size_t len,
1098                                                 loff_t *off)
1099 {
1100         struct seq_file *seq = file->private_data;
1101         struct ll_sb_info *sbi = seq->private;
1102         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1103         int i;
1104         int value = 1, rc = 0;
1105
1106         rc = lprocfs_write_helper(buf, len, &value);
1107         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1108                        strcmp(buf, "Disabled") == 0))
1109                 value = 0;
1110
1111         if (value == 0)
1112                 sbi->ll_rw_stats_on = 0;
1113         else
1114                 sbi->ll_rw_stats_on = 1;
1115
1116         spin_lock(&sbi->ll_pp_extent_lock);
1117         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1118                 io_extents->pp_extents[i].pid = 0;
1119                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1120                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1121         }
1122         spin_unlock(&sbi->ll_pp_extent_lock);
1123         return len;
1124 }
1125
1126 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1127
1128 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1129 {
1130         struct timeval now;
1131         struct ll_sb_info *sbi = seq->private;
1132         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1133
1134         cfs_gettimeofday(&now);
1135
1136         if (!sbi->ll_rw_stats_on) {
1137                 seq_printf(seq, "disabled\n"
1138                                 "write anything in this file to activate, "
1139                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1140                 return 0;
1141         }
1142         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1143                    now.tv_sec, now.tv_usec);
1144
1145         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1146         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1147                    "extents", "calls", "%", "cum%",
1148                    "calls", "%", "cum%");
1149         spin_lock(&sbi->ll_lock);
1150         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1151         spin_unlock(&sbi->ll_lock);
1152
1153         return 0;
1154 }
1155
1156 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1157                                         size_t len, loff_t *off)
1158 {
1159         struct seq_file *seq = file->private_data;
1160         struct ll_sb_info *sbi = seq->private;
1161         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1162         int i;
1163         int value = 1, rc = 0;
1164
1165         rc = lprocfs_write_helper(buf, len, &value);
1166         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1167                        strcmp(buf, "Disabled") == 0))
1168                 value = 0;
1169
1170         if (value == 0)
1171                 sbi->ll_rw_stats_on = 0;
1172         else
1173                 sbi->ll_rw_stats_on = 1;
1174         spin_lock(&sbi->ll_pp_extent_lock);
1175         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
1176                 io_extents->pp_extents[i].pid = 0;
1177                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1178                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1179         }
1180         spin_unlock(&sbi->ll_pp_extent_lock);
1181
1182         return len;
1183 }
1184
1185 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1186
1187 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
1188                        struct ll_file_data *file, loff_t pos,
1189                        size_t count, int rw)
1190 {
1191         int i, cur = -1;
1192         struct ll_rw_process_info *process;
1193         struct ll_rw_process_info *offset;
1194         int *off_count = &sbi->ll_rw_offset_entry_count;
1195         int *process_count = &sbi->ll_offset_process_count;
1196         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1197
1198         if(!sbi->ll_rw_stats_on)
1199                 return;
1200         process = sbi->ll_rw_process_info;
1201         offset = sbi->ll_rw_offset_info;
1202
1203         spin_lock(&sbi->ll_pp_extent_lock);
1204         /* Extent statistics */
1205         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1206                 if(io_extents->pp_extents[i].pid == pid) {
1207                         cur = i;
1208                         break;
1209                 }
1210         }
1211
1212         if (cur == -1) {
1213                 /* new process */
1214                 sbi->ll_extent_process_count =
1215                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1216                 cur = sbi->ll_extent_process_count;
1217                 io_extents->pp_extents[cur].pid = pid;
1218                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1219                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1220         }
1221
1222         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1223              (i < (LL_HIST_MAX - 1)); i++);
1224         if (rw == 0) {
1225                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1226                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1227         } else {
1228                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1229                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1230         }
1231         spin_unlock(&sbi->ll_pp_extent_lock);
1232
1233         spin_lock(&sbi->ll_process_lock);
1234         /* Offset statistics */
1235         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1236                 if (process[i].rw_pid == pid) {
1237                         if (process[i].rw_last_file != file) {
1238                                 process[i].rw_range_start = pos;
1239                                 process[i].rw_last_file_pos = pos + count;
1240                                 process[i].rw_smallest_extent = count;
1241                                 process[i].rw_largest_extent = count;
1242                                 process[i].rw_offset = 0;
1243                                 process[i].rw_last_file = file;
1244                                 spin_unlock(&sbi->ll_process_lock);
1245                                 return;
1246                         }
1247                         if (process[i].rw_last_file_pos != pos) {
1248                                 *off_count =
1249                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1250                                 offset[*off_count].rw_op = process[i].rw_op;
1251                                 offset[*off_count].rw_pid = pid;
1252                                 offset[*off_count].rw_range_start =
1253                                         process[i].rw_range_start;
1254                                 offset[*off_count].rw_range_end =
1255                                         process[i].rw_last_file_pos;
1256                                 offset[*off_count].rw_smallest_extent =
1257                                         process[i].rw_smallest_extent;
1258                                 offset[*off_count].rw_largest_extent =
1259                                         process[i].rw_largest_extent;
1260                                 offset[*off_count].rw_offset =
1261                                         process[i].rw_offset;
1262                                 process[i].rw_op = rw;
1263                                 process[i].rw_range_start = pos;
1264                                 process[i].rw_smallest_extent = count;
1265                                 process[i].rw_largest_extent = count;
1266                                 process[i].rw_offset = pos -
1267                                         process[i].rw_last_file_pos;
1268                         }
1269                         if(process[i].rw_smallest_extent > count)
1270                                 process[i].rw_smallest_extent = count;
1271                         if(process[i].rw_largest_extent < count)
1272                                 process[i].rw_largest_extent = count;
1273                         process[i].rw_last_file_pos = pos + count;
1274                         spin_unlock(&sbi->ll_process_lock);
1275                         return;
1276                 }
1277         }
1278         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1279         process[*process_count].rw_pid = pid;
1280         process[*process_count].rw_op = rw;
1281         process[*process_count].rw_range_start = pos;
1282         process[*process_count].rw_last_file_pos = pos + count;
1283         process[*process_count].rw_smallest_extent = count;
1284         process[*process_count].rw_largest_extent = count;
1285         process[*process_count].rw_offset = 0;
1286         process[*process_count].rw_last_file = file;
1287         spin_unlock(&sbi->ll_process_lock);
1288 }
1289
1290 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1291 {
1292         struct timeval now;
1293         struct ll_sb_info *sbi = seq->private;
1294         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1295         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1296         int i;
1297
1298         cfs_gettimeofday(&now);
1299
1300         if (!sbi->ll_rw_stats_on) {
1301                 seq_printf(seq, "disabled\n"
1302                                 "write anything in this file to activate, "
1303                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1304                 return 0;
1305         }
1306         spin_lock(&sbi->ll_process_lock);
1307
1308         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1309                    now.tv_sec, now.tv_usec);
1310         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1311                    "R/W", "PID", "RANGE START", "RANGE END",
1312                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1313
1314         /* We stored the discontiguous offsets here; print them first */
1315         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1316                 if (offset[i].rw_pid != 0)
1317                         seq_printf(seq,
1318                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1319                                    offset[i].rw_op == READ ? 'R' : 'W',
1320                                    offset[i].rw_pid,
1321                                    offset[i].rw_range_start,
1322                                    offset[i].rw_range_end,
1323                                    (unsigned long)offset[i].rw_smallest_extent,
1324                                    (unsigned long)offset[i].rw_largest_extent,
1325                                    offset[i].rw_offset);
1326         }
1327
1328         /* Then print the current offsets for each process */
1329         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1330                 if (process[i].rw_pid != 0)
1331                         seq_printf(seq,
1332                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1333                                    process[i].rw_op == READ ? 'R' : 'W',
1334                                    process[i].rw_pid,
1335                                    process[i].rw_range_start,
1336                                    process[i].rw_last_file_pos,
1337                                    (unsigned long)process[i].rw_smallest_extent,
1338                                    (unsigned long)process[i].rw_largest_extent,
1339                                    process[i].rw_offset);
1340         }
1341         spin_unlock(&sbi->ll_process_lock);
1342
1343         return 0;
1344 }
1345
1346 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1347                                        size_t len, loff_t *off)
1348 {
1349         struct seq_file *seq = file->private_data;
1350         struct ll_sb_info *sbi = seq->private;
1351         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1352         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1353         int value = 1, rc = 0;
1354
1355         rc = lprocfs_write_helper(buf, len, &value);
1356
1357         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1358                            strcmp(buf, "Disabled") == 0))
1359                 value = 0;
1360
1361         if (value == 0)
1362                 sbi->ll_rw_stats_on = 0;
1363         else
1364                 sbi->ll_rw_stats_on = 1;
1365
1366         spin_lock(&sbi->ll_process_lock);
1367         sbi->ll_offset_process_count = 0;
1368         sbi->ll_rw_offset_entry_count = 0;
1369         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1370                LL_PROCESS_HIST_MAX);
1371         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1372                LL_OFFSET_HIST_MAX);
1373         spin_unlock(&sbi->ll_process_lock);
1374
1375         return len;
1376 }
1377
1378 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1379
1380 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1381 {
1382     lvars->module_vars  = NULL;
1383     lvars->obd_vars     = lprocfs_llite_obd_vars;
1384 }
1385 #endif /* LPROCFS */