Whamcloud - gitweb
b=10968
[fs/lustre-release.git] / lustre / llite / lproc_llite.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #define DEBUG_SUBSYSTEM S_LLITE
23
24 #include <linux/version.h>
25 #include <lustre_lite.h>
26 #include <lprocfs_status.h>
27 #include <linux/seq_file.h>
28 #include <obd_support.h>
29
30 #include "llite_internal.h"
31
32 struct proc_dir_entry *proc_lustre_fs_root;
33
34 #ifdef LPROCFS
35 /* /proc/lustre/llite mount point registration */
36 struct file_operations llite_dump_pgcache_fops;
37 struct file_operations ll_ra_stats_fops;
38 struct file_operations ll_rw_extents_stats_fops;
39 struct file_operations ll_rw_extents_stats_pp_fops;
40 struct file_operations ll_rw_offset_stats_fops;
41
42 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
43                          int *eof, void *data)
44 {
45         struct super_block *sb = (struct super_block *)data;
46         struct obd_statfs osfs;
47         int rc;
48
49         LASSERT(sb != NULL);
50         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ);
51         if (!rc) {
52               *eof = 1;
53               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
54         }
55
56         return rc;
57 }
58
59 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
60                              int *eof, void *data)
61 {
62         struct super_block *sb = (struct super_block *)data;
63         struct obd_statfs osfs;
64         int rc;
65
66         LASSERT(sb != NULL);
67         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ);
68         if (!rc) {
69                 __u32 blk_size = osfs.os_bsize >> 10;
70                 __u64 result = osfs.os_blocks;
71
72                 while (blk_size >>= 1)
73                         result <<= 1;
74
75                 *eof = 1;
76                 rc = snprintf(page, count, LPU64"\n", result);
77         }
78         return rc;
79
80 }
81
82 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
83                             int *eof, void *data)
84 {
85         struct super_block *sb = (struct super_block *)data;
86         struct obd_statfs osfs;
87         int rc;
88
89         LASSERT(sb != NULL);
90         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ);
91         if (!rc) {
92                 __u32 blk_size = osfs.os_bsize >> 10;
93                 __u64 result = osfs.os_bfree;
94
95                 while (blk_size >>= 1)
96                         result <<= 1;
97
98                 *eof = 1;
99                 rc = snprintf(page, count, LPU64"\n", result);
100         }
101         return rc;
102 }
103
104 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
105                              int *eof, void *data)
106 {
107         struct super_block *sb = (struct super_block *)data;
108         struct obd_statfs osfs;
109         int rc;
110
111         LASSERT(sb != NULL);
112         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ);
113         if (!rc) {
114                 __u32 blk_size = osfs.os_bsize >> 10;
115                 __u64 result = osfs.os_bavail;
116
117                 while (blk_size >>= 1)
118                         result <<= 1;
119
120                 *eof = 1;
121                 rc = snprintf(page, count, LPU64"\n", result);
122         }
123         return rc;
124 }
125
126 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
127                             int *eof, void *data)
128 {
129         struct super_block *sb = (struct super_block *)data;
130         struct obd_statfs osfs;
131         int rc;
132
133         LASSERT(sb != NULL);
134         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ);
135         if (!rc) {
136                  *eof = 1;
137                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
138         }
139         return rc;
140 }
141
142 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
143                            int *eof, void *data)
144 {
145         struct super_block *sb = (struct super_block *)data;
146         struct obd_statfs osfs;
147         int rc;
148
149         LASSERT(sb != NULL);
150         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ);
151         if (!rc) {
152                  *eof = 1;
153                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
154         }
155         return rc;
156
157 }
158
159 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
160                         int *eof, void *data)
161 {
162         struct super_block *sb = (struct super_block*)data;
163
164         LASSERT(sb != NULL);
165         *eof = 1;
166         return snprintf(page, count, "%s\n", sb->s_type->name);
167 }
168
169 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
170                          int *eof, void *data)
171 {
172         struct super_block *sb = (struct super_block *)data;
173
174         LASSERT(sb != NULL);
175         *eof = 1;
176         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
177 }
178
179 static int ll_rd_max_readahead_mb(char *page, char **start, off_t off,
180                                    int count, int *eof, void *data)
181 {
182         struct super_block *sb = data;
183         struct ll_sb_info *sbi = ll_s2sbi(sb);
184         long pages_number;
185         int mult;
186
187         spin_lock(&sbi->ll_lock);
188         pages_number = sbi->ll_ra_info.ra_max_pages;
189         spin_unlock(&sbi->ll_lock);
190
191         mult = 1 << (20 - CFS_PAGE_SHIFT);
192         return lprocfs_read_frac_helper(page, count, pages_number, mult);
193 }
194
195 static int ll_wr_max_readahead_mb(struct file *file, const char *buffer,
196                                    unsigned long count, void *data)
197 {
198         struct super_block *sb = data;
199         struct ll_sb_info *sbi = ll_s2sbi(sb);
200         int mult, rc, pages_number;
201
202         mult = 1 << (20 - CFS_PAGE_SHIFT);
203         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
204         if (rc)
205                 return rc;
206
207         if (pages_number < 0 || pages_number > num_physpages / 2) {
208                 CERROR("can't set file readahead more than %lu MB\n",
209                         num_physpages >> (20 - CFS_PAGE_SHIFT + 1)); /*1/2 of RAM*/
210                 return -ERANGE;
211         }
212
213         spin_lock(&sbi->ll_lock);
214         sbi->ll_ra_info.ra_max_pages = pages_number;
215         spin_unlock(&sbi->ll_lock);
216
217         return count;
218 }
219
220 static int ll_rd_max_read_ahead_whole_mb(char *page, char **start, off_t off,
221                                        int count, int *eof, void *data)
222 {
223         struct super_block *sb = data;
224         struct ll_sb_info *sbi = ll_s2sbi(sb);
225         long pages_number;
226         int mult;
227
228         spin_lock(&sbi->ll_lock);
229         pages_number = sbi->ll_ra_info.ra_max_read_ahead_whole_pages;
230         spin_unlock(&sbi->ll_lock);
231
232         mult = 1 << (20 - CFS_PAGE_SHIFT);
233         return lprocfs_read_frac_helper(page, count, pages_number, mult);
234 }
235
236 static int ll_wr_max_read_ahead_whole_mb(struct file *file, const char *buffer,
237                                        unsigned long count, void *data)
238 {
239         struct super_block *sb = data;
240         struct ll_sb_info *sbi = ll_s2sbi(sb);
241         int mult, rc, pages_number;
242
243         mult = 1 << (20 - CFS_PAGE_SHIFT);
244         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
245         if (rc)
246                 return rc;
247
248         /* Cap this at the current max readahead window size, the readahead
249          * algorithm does this anyway so it's pointless to set it larger. */
250         if (pages_number < 0 || pages_number > sbi->ll_ra_info.ra_max_pages) {
251                 CERROR("can't set max_read_ahead_whole_mb more than "
252                        "max_read_ahead_mb: %lu\n",
253                        sbi->ll_ra_info.ra_max_pages >> (20 - CFS_PAGE_SHIFT));
254                 return -ERANGE;
255         }
256
257         spin_lock(&sbi->ll_lock);
258         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
259         spin_unlock(&sbi->ll_lock);
260
261         return count;
262 }
263
264 static int ll_rd_max_cached_mb(char *page, char **start, off_t off,
265                                int count, int *eof, void *data)
266 {
267         struct super_block *sb = data;
268         struct ll_sb_info *sbi = ll_s2sbi(sb);
269         long pages_number;
270         int mult;
271
272         spin_lock(&sbi->ll_lock);
273         pages_number = sbi->ll_async_page_max;
274         spin_unlock(&sbi->ll_lock);
275
276         mult = 1 << (20 - CFS_PAGE_SHIFT);
277         return lprocfs_read_frac_helper(page, count, pages_number, mult);;
278 }
279
280 static int ll_wr_max_cached_mb(struct file *file, const char *buffer,
281                                   unsigned long count, void *data)
282 {
283         struct super_block *sb = data;
284         struct ll_sb_info *sbi = ll_s2sbi(sb);
285         int mult, rc, pages_number;
286
287         mult = 1 << (20 - CFS_PAGE_SHIFT);
288         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
289         if (rc)
290                 return rc;
291
292         if (pages_number < 0 || pages_number > num_physpages) {
293                 CERROR("can't set max cache more than %lu MB\n",
294                         num_physpages >> (20 - CFS_PAGE_SHIFT));
295                 return -ERANGE;
296         }
297
298         spin_lock(&sbi->ll_lock);
299         sbi->ll_async_page_max = pages_number ;
300         spin_unlock(&sbi->ll_lock);
301         
302         if (!sbi->ll_osc_exp)
303                 /* Not set up yet, don't call llap_shrink_cache */
304                 return count;
305
306         if (sbi->ll_async_page_count >= sbi->ll_async_page_max)
307                 llap_shrink_cache(sbi, 0);
308
309         return count;
310 }
311
312 static int ll_rd_checksum(char *page, char **start, off_t off,
313                           int count, int *eof, void *data)
314 {
315         struct super_block *sb = data;
316         struct ll_sb_info *sbi = ll_s2sbi(sb);
317
318         return snprintf(page, count, "%u\n",
319                         (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
320 }
321
322 static int ll_wr_checksum(struct file *file, const char *buffer,
323                           unsigned long count, void *data)
324 {
325         struct super_block *sb = data;
326         struct ll_sb_info *sbi = ll_s2sbi(sb);
327         int val, rc;
328
329         if (!sbi->ll_osc_exp)
330                 /* Not set up yet */
331                 return -EAGAIN;
332
333         rc = lprocfs_write_helper(buffer, count, &val);
334         if (rc)
335                 return rc;
336         if (val)
337                 sbi->ll_flags |= LL_SBI_CHECKSUM;
338         else
339                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
340
341         rc = obd_set_info_async(sbi->ll_osc_exp, strlen("checksum"), "checksum",
342                                 sizeof(val), &val, NULL);
343         if (rc)
344                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
345
346         return count;
347 }
348
349 static int ll_rd_max_rw_chunk(char *page, char **start, off_t off,
350                           int count, int *eof, void *data)
351 {
352         struct super_block *sb = data;
353
354         return snprintf(page, count, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
355 }
356
357 static int ll_wr_max_rw_chunk(struct file *file, const char *buffer,
358                           unsigned long count, void *data)
359 {
360         struct super_block *sb = data;
361         int rc, val;
362
363         rc = lprocfs_write_helper(buffer, count, &val);
364         if (rc)
365                 return rc;
366         ll_s2sbi(sb)->ll_max_rw_chunk = val;
367         return count;
368 }
369
370 static int ll_rd_track_id(char *page, int count, void *data, 
371                           enum vfs_track_type type)
372 {
373         struct super_block *sb = data;
374
375         if (ll_s2sbi(sb)->ll_vfs_track_type == type) {
376                 return snprintf(page, count, "%d\n",
377                                 ll_s2sbi(sb)->ll_vfs_track_id);
378         
379         } else if (ll_s2sbi(sb)->ll_vfs_track_type == VFS_TRACK_ALL) {
380                 return snprintf(page, count, "0 (all)\n");
381         } else {
382                 return snprintf(page, count, "untracked\n");
383         }
384 }
385
386 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
387                           enum vfs_track_type type)
388 {
389         struct super_block *sb = data;
390         int rc, pid;
391
392         rc = lprocfs_write_helper(buffer, count, &pid);
393         if (rc)
394                 return rc;
395         ll_s2sbi(sb)->ll_vfs_track_id = pid;
396         if (pid == 0)
397                 ll_s2sbi(sb)->ll_vfs_track_type = VFS_TRACK_ALL;
398         else
399                 ll_s2sbi(sb)->ll_vfs_track_type = type;
400         lprocfs_clear_stats(ll_s2sbi(sb)->ll_vfs_ops_stats);
401         return count;
402 }
403
404 static int ll_rd_track_pid(char *page, char **start, off_t off,
405                           int count, int *eof, void *data)
406 {
407         return (ll_rd_track_id(page, count, data, VFS_TRACK_PID));
408 }
409
410 static int ll_wr_track_pid(struct file *file, const char *buffer,
411                           unsigned long count, void *data)
412 {
413         return (ll_wr_track_id(buffer, count, data, VFS_TRACK_PID)); 
414 }
415
416 static int ll_rd_track_ppid(char *page, char **start, off_t off,
417                           int count, int *eof, void *data)
418 {
419         return (ll_rd_track_id(page, count, data, VFS_TRACK_PPID));
420 }
421
422 static int ll_wr_track_ppid(struct file *file, const char *buffer,
423                           unsigned long count, void *data)
424 {
425         return (ll_wr_track_id(buffer, count, data, VFS_TRACK_PPID)); 
426 }
427
428 static int ll_rd_track_gid(char *page, char **start, off_t off,
429                           int count, int *eof, void *data)
430 {
431         return (ll_rd_track_id(page, count, data, VFS_TRACK_GID));
432 }
433
434 static int ll_wr_track_gid(struct file *file, const char *buffer,
435                           unsigned long count, void *data)
436 {                                                                 
437         return (ll_wr_track_id(buffer, count, data, VFS_TRACK_GID)); 
438 }
439
440 static struct lprocfs_vars lprocfs_obd_vars[] = {
441         { "uuid",         ll_rd_sb_uuid,          0, 0 },
442         //{ "mntpt_path",   ll_rd_path,             0, 0 },
443         { "fstype",       ll_rd_fstype,           0, 0 },
444         { "blocksize",    ll_rd_blksize,          0, 0 },
445         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
446         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
447         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
448         { "filestotal",   ll_rd_filestotal,       0, 0 },
449         { "filesfree",    ll_rd_filesfree,        0, 0 },
450         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
451         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
452                                ll_wr_max_readahead_mb, 0 },
453         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
454                                      ll_wr_max_read_ahead_whole_mb, 0 },
455         { "max_cached_mb",  ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
456         { "checksum_pages", ll_rd_checksum, ll_wr_checksum, 0 },
457         { "max_rw_chunk",   ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
458         { "vfs_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
459         { "vfs_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
460         { "vfs_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
461         { 0 }
462 };
463
464 #define MAX_STRING_SIZE 128
465
466 struct llite_file_opcode {
467         __u32       opcode;
468         __u32       type;
469         const char *opname;
470 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
471         /* file operation */
472         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
473         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
474         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
475                                    "writeback_from_writepage" },
476         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
477                                    "writeback_from_pressure" },
478         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
479                                    "writeback_ok_pages" },
480         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
481                                    "writeback_failed_pages" },
482         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
483                                    "read_bytes" },
484         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
485                                    "write_bytes" },
486         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
487                                    "brw_read" },
488         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
489                                    "brw_write" },
490
491         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
492         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
493         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
494         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
495         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
496         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
497         /* inode operation */
498         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
499         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "punch" },
500 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
501         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
502 #else
503         { LPROC_LL_REVALIDATE,     LPROCFS_TYPE_REGS, "getattr" },
504 #endif
505         /* special inode operation */
506         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
507         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
508         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
509         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
510         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
511                                    "direct_read" },
512         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
513                                    "direct_write" },
514
515 };
516
517 void ll_vfs_ops_tally(struct ll_sb_info *sbi, int op)
518 {
519         if (sbi->ll_vfs_ops_stats && sbi->ll_vfs_track_type == VFS_TRACK_ALL)
520                 lprocfs_counter_incr(sbi->ll_vfs_ops_stats, op);
521         else if (sbi->ll_vfs_track_type == VFS_TRACK_PID &&
522                  sbi->ll_vfs_track_id == current->pid)
523                 lprocfs_counter_incr(sbi->ll_vfs_ops_stats, op);
524         else if (sbi->ll_vfs_track_type == VFS_TRACK_PPID &&
525                  sbi->ll_vfs_track_id == current->p_pptr->pid)
526                 lprocfs_counter_incr(sbi->ll_vfs_ops_stats, op);
527         else if (sbi->ll_vfs_track_type == VFS_TRACK_GID &&
528                  sbi->ll_vfs_track_id == current->gid)
529                 lprocfs_counter_incr(sbi->ll_vfs_ops_stats, op);
530 }
531
532 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
533                                 struct super_block *sb, char *osc, char *mdc)
534 {
535         struct lprocfs_vars lvars[2];
536         struct lustre_sb_info *lsi = s2lsi(sb);
537         struct ll_sb_info *sbi = ll_s2sbi(sb);
538         struct obd_device *obd;
539         char name[MAX_STRING_SIZE + 1], *ptr;
540         int err, id, len;
541         struct lprocfs_stats *vfs_ops_stats = NULL;
542         struct proc_dir_entry *entry;
543         ENTRY;
544
545         memset(lvars, 0, sizeof(lvars));
546
547         name[MAX_STRING_SIZE] = '\0';
548         lvars[0].name = name;
549
550         LASSERT(sbi != NULL);
551         LASSERT(mdc != NULL);
552         LASSERT(osc != NULL);
553
554         /* Get fsname */
555         len = strlen(lsi->lsi_lmd->lmd_profile);
556         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
557         if (ptr && (strcmp(ptr, "-client") == 0))
558                 len -= 7; 
559         
560         /* Mount info */
561         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
562                  lsi->lsi_lmd->lmd_profile, sb);
563         
564         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
565         if (IS_ERR(sbi->ll_proc_root)) {
566                 err = PTR_ERR(sbi->ll_proc_root);
567                 sbi->ll_proc_root = NULL;
568                 RETURN(err);
569         }
570
571         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
572         if (entry == NULL)
573                 GOTO(out, err = -ENOMEM);
574         entry->proc_fops = &llite_dump_pgcache_fops;
575         entry->data = sbi;
576
577         entry = create_proc_entry("read_ahead_stats", 0644, sbi->ll_proc_root);
578         if (entry == NULL)
579                 GOTO(out, err = -ENOMEM);
580         entry->proc_fops = &ll_ra_stats_fops;
581         entry->data = sbi;
582
583         entry = create_proc_entry("extents_stats", 0644, sbi->ll_proc_root);
584         if (entry == NULL)
585                  GOTO(out, err = -ENOMEM);
586         entry->proc_fops = &ll_rw_extents_stats_fops;
587         entry->data = sbi;
588
589         entry = create_proc_entry("extents_stats_per_process", 0644,
590                                   sbi->ll_proc_root);
591         if (entry == NULL)
592                  GOTO(out, err = -ENOMEM);
593         entry->proc_fops = &ll_rw_extents_stats_pp_fops;
594         entry->data = sbi;
595
596         entry = create_proc_entry("offset_stats", 0644, sbi->ll_proc_root);
597         if (entry == NULL)
598                 GOTO(out, err = -ENOMEM);
599         entry->proc_fops = &ll_rw_offset_stats_fops;
600         entry->data = sbi;
601
602         /* File operations stats */
603         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES);
604         if (sbi->ll_stats == NULL)
605                 GOTO(out, err = -ENOMEM);
606         /* do counter init */
607         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
608                 __u32 type = llite_opcode_table[id].type;
609                 void *ptr = NULL;
610                 if (type & LPROCFS_TYPE_REGS)
611                         ptr = "regs";
612                 else {
613                         if (type & LPROCFS_TYPE_BYTES)
614                                 ptr = "bytes";
615                         else {
616                                 if (type & LPROCFS_TYPE_PAGES)
617                                         ptr = "pages";
618                         }
619                 }
620                 lprocfs_counter_init(sbi->ll_stats, 
621                                      llite_opcode_table[id].opcode,
622                                      (type & LPROCFS_CNTR_AVGMINMAX),
623                                      llite_opcode_table[id].opname, ptr);
624         }
625         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
626         if (err)
627                 GOTO(out, err);
628
629         /* VFS operations stats */
630         vfs_ops_stats = sbi->ll_vfs_ops_stats =
631                 lprocfs_alloc_stats(VFS_OPS_LAST);
632         if (vfs_ops_stats == NULL)
633                 GOTO(out, err = -ENOMEM);
634         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_READ, 0, "read", "reqs");
635         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_WRITE, 0, "write", "reqs");
636         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_IOCTL, 0, "ioctl", "reqs");
637         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_OPEN, 0, "open", "reqs");
638         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_RELEASE, 0, "release",
639                              "reqs");
640         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_MMAP, 0, "mmap", "reqs");
641         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_SEEK, 0, "seek", "reqs");
642         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_FSYNC, 0, "fsync", "reqs");
643         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_FLOCK, 0, "flock", "reqs");
644         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_SETATTR, 0, "setattr",
645                              "reqs");
646         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_GETATTR, 0, "getattr",
647                              "reqs");
648         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_SETXATTR, 0, "setxattr",
649                              "reqs");
650         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_GETXATTR, 0, "getxattr",
651                              "reqs");
652         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_LISTXATTR, 0, "listxattr",
653                              "reqs");
654         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_REMOVEXATTR, 0,
655                              "removexattr", "reqs");
656         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_TRUNCATE, 0, "truncate",
657                              "reqs");
658         lprocfs_counter_init(vfs_ops_stats, VFS_OPS_INODE_PERMISSION, 0,
659                              "inode_permission", "reqs");
660
661         err = lprocfs_register_stats(sbi->ll_proc_root, "vfs_ops_stats",
662                                      vfs_ops_stats);
663         if (err)
664                 GOTO(out, err);
665
666         /* Static configuration info */
667         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_obd_vars, sb);
668         if (err)
669                 GOTO(out, err);
670
671         /* MDC info */
672         obd = class_name2obd(mdc);
673
674         LASSERT(obd != NULL);
675         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
676         LASSERT(obd->obd_type->typ_name != NULL);
677
678         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
679                  obd->obd_type->typ_name);
680         lvars[0].read_fptr = lprocfs_rd_name;
681         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
682         if (err)
683                 GOTO(out, err);
684
685         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
686         lvars[0].read_fptr = lprocfs_rd_uuid;
687         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
688         if (err)
689                 GOTO(out, err);
690
691         /* OSC */
692         obd = class_name2obd(osc);
693
694         LASSERT(obd != NULL);
695         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
696         LASSERT(obd->obd_type->typ_name != NULL);
697
698         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
699                  obd->obd_type->typ_name);
700         lvars[0].read_fptr = lprocfs_rd_name;
701         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
702         if (err)
703                 GOTO(out, err);
704
705         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
706         lvars[0].read_fptr = lprocfs_rd_uuid;
707         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
708 out:
709         if (err) {
710                 lprocfs_remove(&sbi->ll_proc_root);
711                 lprocfs_free_stats(&sbi->ll_stats);
712                 lprocfs_free_stats(&sbi->ll_vfs_ops_stats);
713         }
714         RETURN(err);
715 }
716
717 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
718 {
719         if (sbi->ll_proc_root) {
720                 lprocfs_remove(&sbi->ll_proc_root);
721                 lprocfs_free_stats(&sbi->ll_stats);
722                 lprocfs_free_stats(&sbi->ll_vfs_ops_stats);
723         }
724 }
725 #undef MAX_STRING_SIZE
726
727 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
728                 if (test_bit(PG_##flag, &(page)->flags)) {              \
729                         if (!has_flags)                                 \
730                                 has_flags = 1;                          \
731                         else                                            \
732                                 seq_putc(seq, '|');                     \
733                         seq_puts(seq, #flag);                           \
734                 }                                                       \
735         } while(0);
736
737 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
738 {
739         struct ll_async_page *dummy_llap = seq->private;
740
741         if (dummy_llap->llap_magic == 2)
742                 return NULL;
743
744         return (void *)1;
745 }
746
747 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
748 {
749         struct ll_async_page *llap, *dummy_llap = seq->private;
750         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
751
752         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
753          * it in our own state */
754         if (dummy_llap->llap_magic == 0) {
755                 seq_printf(seq, "gener |  llap  cookie  origin wq du | page "
756                                 "inode index count [ page flags ]\n");
757                 return 0;
758         }
759
760         spin_lock(&sbi->ll_lock);
761
762         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_pglist_item);
763         if (llap != NULL)  {
764                 int has_flags = 0;
765                 struct page *page = llap->llap_page;
766
767                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
768                          llap->llap_origin);
769
770                 seq_printf(seq, "%5lu | %p %p %s %s %s | %p %p %lu %u [",
771                            sbi->ll_pglist_gen,
772                            llap, llap->llap_cookie,
773                            llap_origins[llap->llap_origin],
774                            llap->llap_write_queued ? "wq" : "- ",
775                            llap->llap_defer_uptodate ? "du" : "- ",
776                            page, page->mapping->host, page->index,
777                            page_count(page));
778                 seq_page_flag(seq, page, locked, has_flags);
779                 seq_page_flag(seq, page, error, has_flags);
780                 seq_page_flag(seq, page, referenced, has_flags);
781                 seq_page_flag(seq, page, uptodate, has_flags);
782                 seq_page_flag(seq, page, dirty, has_flags);
783 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,12))
784                 seq_page_flag(seq, page, highmem, has_flags);
785 #endif
786                 if (!has_flags)
787                         seq_puts(seq, "-]\n");
788                 else 
789                         seq_puts(seq, "]\n");
790         }
791
792         spin_unlock(&sbi->ll_lock);
793
794         return 0;
795 }
796
797 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v, 
798                                          loff_t *pos)
799 {
800         struct ll_async_page *llap, *dummy_llap = seq->private;
801         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
802
803         /* bail if we just displayed the banner */
804         if (dummy_llap->llap_magic == 0) {
805                 dummy_llap->llap_magic = 1;
806                 return dummy_llap;
807         }
808
809         /* we've just displayed the llap that is after us in the list.
810          * we advance to a position beyond it, returning null if there
811          * isn't another llap in the list beyond that new position. */
812         spin_lock(&sbi->ll_lock);
813         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_pglist_item);
814         list_del_init(&dummy_llap->llap_pglist_item);
815         if (llap) {
816                 list_add(&dummy_llap->llap_pglist_item,&llap->llap_pglist_item);
817                 llap =llite_pglist_next_llap(sbi,&dummy_llap->llap_pglist_item);
818         }
819         spin_unlock(&sbi->ll_lock);
820
821         ++*pos;
822         if (llap == NULL) {
823                 dummy_llap->llap_magic = 2;
824                 return NULL;
825         }
826         return dummy_llap;
827 }
828
829 static void null_stop(struct seq_file *seq, void *v)
830 {
831 }
832
833 struct seq_operations llite_dump_pgcache_seq_sops = {
834         .start = llite_dump_pgcache_seq_start,
835         .stop = null_stop,
836         .next = llite_dump_pgcache_seq_next,
837         .show = llite_dump_pgcache_seq_show,
838 };
839
840 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
841  * while we walk the entire list, and we don't want to have to seek into
842  * the right position in the list as an app advances with many syscalls.  we
843  * allocate a dummy llap and hang it off file->private.  its position in
844  * the list records where the app is currently displaying.  this way our
845  * seq .start and .stop don't actually do anything.  .next returns null
846  * when the dummy hits the end of the list which eventually leads to .release
847  * where we tear down.  this kind of displaying is super-racey, so we put
848  * a generation counter on the list so the output shows when the list
849  * changes between reads.
850  */
851 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
852 {
853         struct proc_dir_entry *dp = PDE(inode);
854         struct ll_async_page *dummy_llap;
855         struct seq_file *seq;
856         struct ll_sb_info *sbi = dp->data;
857         int rc = -ENOMEM;
858
859         LPROCFS_ENTRY_AND_CHECK(dp);
860
861         OBD_ALLOC_GFP(dummy_llap, sizeof(*dummy_llap), GFP_KERNEL);
862         if (dummy_llap == NULL)
863                 GOTO(out, rc);
864
865         dummy_llap->llap_page = NULL;
866         dummy_llap->llap_cookie = sbi;
867         dummy_llap->llap_magic = 0;
868
869         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
870         if (rc) {
871                 OBD_FREE(dummy_llap, sizeof(*dummy_llap));
872                 GOTO(out, rc);
873         }
874         seq = file->private_data;
875         seq->private = dummy_llap;
876
877         spin_lock(&sbi->ll_lock);
878         list_add(&dummy_llap->llap_pglist_item, &sbi->ll_pglist);
879         spin_unlock(&sbi->ll_lock);
880
881 out:
882         if (rc)
883                 LPROCFS_EXIT();
884         return rc;
885 }
886
887 static int llite_dump_pgcache_seq_release(struct inode *inode,
888                                           struct file *file)
889 {
890         struct seq_file *seq = file->private_data;
891         struct ll_async_page *dummy_llap = seq->private;
892         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
893
894         spin_lock(&sbi->ll_lock);
895         if (!list_empty(&dummy_llap->llap_pglist_item))
896                 list_del_init(&dummy_llap->llap_pglist_item);
897         spin_unlock(&sbi->ll_lock);
898         OBD_FREE(dummy_llap, sizeof(*dummy_llap));
899
900         return lprocfs_seq_release(inode, file);
901 }
902
903 struct file_operations llite_dump_pgcache_fops = {
904         .owner   = THIS_MODULE,
905         .open    = llite_dump_pgcache_seq_open,
906         .read    = seq_read,
907         .release = llite_dump_pgcache_seq_release,
908 };
909
910 static int ll_ra_stats_seq_show(struct seq_file *seq, void *v)
911 {
912         struct timeval now;
913         struct ll_sb_info *sbi = seq->private;
914         struct ll_ra_info *ra = &sbi->ll_ra_info;
915         int i;
916         static char *ra_stat_strings[] = {
917                 [RA_STAT_HIT] = "hits",
918                 [RA_STAT_MISS] = "misses",
919                 [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
920                 [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
921                 [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
922                 [RA_STAT_FAILED_MATCH] = "failed lock match",
923                 [RA_STAT_DISCARDED] = "read but discarded",
924                 [RA_STAT_ZERO_LEN] = "zero length file",
925                 [RA_STAT_ZERO_WINDOW] = "zero size window",
926                 [RA_STAT_EOF] = "read-ahead to EOF",
927                 [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
928                 [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
929         };
930
931         do_gettimeofday(&now);
932
933         spin_lock(&sbi->ll_lock);
934
935         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
936                    now.tv_sec, now.tv_usec);
937         seq_printf(seq, "pending issued pages:           %lu\n",
938                    ra->ra_cur_pages);
939
940         for(i = 0; i < _NR_RA_STAT; i++)
941                 seq_printf(seq, "%-25s %lu\n", ra_stat_strings[i], 
942                            ra->ra_stats[i]);
943
944         spin_unlock(&sbi->ll_lock);
945
946         return 0;
947 }
948
949 static ssize_t ll_ra_stats_seq_write(struct file *file, const char *buf,
950                                        size_t len, loff_t *off)
951 {
952         struct seq_file *seq = file->private_data;
953         struct ll_sb_info *sbi = seq->private;
954         struct ll_ra_info *ra = &sbi->ll_ra_info;
955
956         spin_lock(&sbi->ll_lock);
957         memset(ra->ra_stats, 0, sizeof(ra->ra_stats));
958         spin_unlock(&sbi->ll_lock);
959
960         return len;
961 }
962
963 LPROC_SEQ_FOPS(ll_ra_stats);
964
965 #define pct(a,b) (b ? a * 100 / b : 0)
966
967 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
968                                    struct seq_file *seq, int which)
969 {
970         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
971         unsigned long start, end, r, w;
972         char *unitp = "KMGTPEZY";
973         int i, units = 10;
974         struct per_process_info *pp_info = &io_extents->pp_extents[which];
975
976         read_cum = 0;
977         write_cum = 0;
978         start = 0;
979
980         for(i = 0; i < LL_HIST_MAX; i++) {
981                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
982                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
983         }
984
985         for(i = 0; i < LL_HIST_MAX; i++) {
986                 r = pp_info->pp_r_hist.oh_buckets[i];
987                 w = pp_info->pp_w_hist.oh_buckets[i];
988                 read_cum += r;
989                 write_cum += w;
990                 end = 1 << (i + LL_HIST_START - units);
991                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
992                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
993                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
994                            r, pct(r, read_tot), pct(read_cum, read_tot),
995                            w, pct(w, write_tot), pct(write_cum, write_tot));
996                 start = end;
997                 if (start == 1<<10) {
998                         start = 1;
999                         units += 10;
1000                         unitp++;
1001                 }
1002                 if (read_cum == read_tot && write_cum == write_tot)
1003                         break;
1004         }
1005 }
1006
1007 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1008 {
1009         struct timeval now;
1010         struct ll_sb_info *sbi = seq->private;
1011         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1012         int k;
1013
1014         do_gettimeofday(&now);
1015
1016         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1017                    now.tv_sec, now.tv_usec);
1018         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1019         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n", 
1020                    "extents", "calls", "%", "cum%",
1021                    "calls", "%", "cum%");
1022         
1023         spin_lock(&sbi->ll_lock);
1024         for(k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1025                 if(io_extents->pp_extents[k].pid != 0) {
1026                         seq_printf(seq, "\nPID: %d\n",
1027                                    io_extents->pp_extents[k].pid);
1028                         ll_display_extents_info(io_extents, seq, k);
1029                 }
1030         }
1031         spin_unlock(&sbi->ll_lock);
1032         
1033         return 0;
1034 }
1035
1036 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1037                                                 const char *buf, size_t len,
1038                                                 loff_t *off)
1039 {
1040         struct seq_file *seq = file->private_data;
1041         struct ll_sb_info *sbi = seq->private;
1042         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1043         int i;
1044
1045         spin_lock(&sbi->ll_lock);
1046         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1047                 io_extents->pp_extents[i].pid = 0;
1048                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1049                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1050         }
1051         spin_unlock(&sbi->ll_lock);
1052         return len;
1053 }
1054
1055 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1056
1057 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1058 {
1059         struct timeval now;
1060         struct ll_sb_info *sbi = seq->private;
1061         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1062
1063         do_gettimeofday(&now);
1064
1065         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1066                    now.tv_sec, now.tv_usec);
1067
1068         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1069         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n", 
1070                    "extents", "calls", "%", "cum%",
1071                    "calls", "%", "cum%");
1072
1073         spin_lock(&sbi->ll_lock);
1074         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1075         spin_unlock(&sbi->ll_lock);
1076
1077         return 0;
1078 }
1079
1080 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1081                                         size_t len, loff_t *off)
1082 {
1083         struct seq_file *seq = file->private_data;
1084         struct ll_sb_info *sbi = seq->private;
1085         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1086
1087         lprocfs_oh_clear(&io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist);
1088         lprocfs_oh_clear(&io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist);
1089
1090         return len;
1091 }
1092
1093 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1094
1095 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid, struct file
1096                                *file, size_t count, int rw)
1097 {
1098         int i, cur = -1;
1099         struct ll_rw_process_info *process;
1100         struct ll_rw_process_info *offset;
1101         int *off_count = &sbi->ll_rw_offset_entry_count;
1102         int *process_count = &sbi->ll_offset_process_count;
1103         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1104
1105         process = sbi->ll_rw_process_info;
1106         offset = sbi->ll_rw_offset_info;
1107
1108         spin_lock(&sbi->ll_lock);
1109         /* Extent statistics */
1110         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1111                 if(io_extents->pp_extents[i].pid == pid) {
1112                         cur = i;
1113                         break;
1114                 }
1115         }
1116
1117         if (cur == -1) {
1118                 /* new process */
1119                 sbi->ll_extent_process_count = 
1120                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1121                 cur = sbi->ll_extent_process_count;
1122                 io_extents->pp_extents[cur].pid = pid;
1123                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1124                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1125         }
1126
1127         for(i = 0; (count >= (1 << LL_HIST_START << i)) && 
1128              (i < (LL_HIST_MAX - 1)); i++);
1129         if (rw == 0) {
1130                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1131                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1132         } else {
1133                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1134                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1135         }
1136
1137         /* Offset statistics */
1138         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1139                 if (process[i].rw_pid == pid) {
1140                         if (process[i].rw_last_file != file) {
1141                                 process[i].rw_range_start = file->f_pos;
1142                                 process[i].rw_last_file_pos =
1143                                                         file->f_pos + count;
1144                                 process[i].rw_smallest_extent = count;
1145                                 process[i].rw_largest_extent = count;
1146                                 process[i].rw_offset = 0;
1147                                 process[i].rw_last_file = file;
1148                                 spin_unlock(&sbi->ll_lock);
1149                                 return;
1150                         }
1151                         if (process[i].rw_last_file_pos != file->f_pos) {
1152                                 *off_count =
1153                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1154                                 offset[*off_count].rw_op = process[i].rw_op;
1155                                 offset[*off_count].rw_pid = pid;
1156                                 offset[*off_count].rw_range_start =
1157                                         process[i].rw_range_start;
1158                                 offset[*off_count].rw_range_end =
1159                                         process[i].rw_last_file_pos;
1160                                 offset[*off_count].rw_smallest_extent =
1161                                         process[i].rw_smallest_extent;
1162                                 offset[*off_count].rw_largest_extent =
1163                                         process[i].rw_largest_extent;
1164                                 offset[*off_count].rw_offset =
1165                                         process[i].rw_offset;
1166                                 process[i].rw_op = rw;
1167                                 process[i].rw_range_start = file->f_pos;
1168                                 process[i].rw_smallest_extent = count;
1169                                 process[i].rw_largest_extent = count;
1170                                 process[i].rw_offset = file->f_pos -
1171                                         process[i].rw_last_file_pos;
1172                         }
1173                         if(process[i].rw_smallest_extent > count)
1174                                 process[i].rw_smallest_extent = count;
1175                         if(process[i].rw_largest_extent < count)
1176                                 process[i].rw_largest_extent = count;
1177                         process[i].rw_last_file_pos = file->f_pos + count;
1178                         spin_unlock(&sbi->ll_lock);
1179                         return;
1180                 }
1181         }
1182         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1183         process[*process_count].rw_pid = pid;
1184         process[*process_count].rw_op = rw;
1185         process[*process_count].rw_range_start = file->f_pos;
1186         process[*process_count].rw_last_file_pos = file->f_pos + count;
1187         process[*process_count].rw_smallest_extent = count;
1188         process[*process_count].rw_largest_extent = count;
1189         process[*process_count].rw_offset = 0;
1190         process[*process_count].rw_last_file = file;
1191         spin_unlock(&sbi->ll_lock);
1192 }
1193
1194 char lpszt[] = LPSZ;
1195
1196 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1197 {
1198         struct timeval now;
1199         struct ll_sb_info *sbi = seq->private;
1200         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1201         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1202         char format[50];
1203         int i;
1204
1205         do_gettimeofday(&now);
1206
1207         spin_lock(&sbi->ll_lock);
1208
1209         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1210                    now.tv_sec, now.tv_usec);
1211         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1212                    "R/W", "PID", "RANGE START", "RANGE END",
1213                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1214         sprintf(format, "%s%s%s%s%s\n", 
1215                 "%3c %10d %14Lu %14Lu %17", lpszt+1, " %17", lpszt+1, " %14Ld");
1216         /* We stored the discontiguous offsets here; print them first */
1217         for(i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1218                 if (offset[i].rw_pid != 0)
1219                         /* Is there a way to snip the '%' off of LPSZ? */
1220                         seq_printf(seq, format,
1221                                    offset[i].rw_op ? 'W' : 'R',
1222                                    offset[i].rw_pid,
1223                                    offset[i].rw_range_start,
1224                                    offset[i].rw_range_end,
1225                                    offset[i].rw_smallest_extent,
1226                                    offset[i].rw_largest_extent,
1227                                    offset[i].rw_offset);
1228         }
1229         /* Then print the current offsets for each process */
1230         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1231                 if (process[i].rw_pid != 0)
1232                         seq_printf(seq, format,
1233                                    process[i].rw_op ? 'W' : 'R',
1234                                    process[i].rw_pid,
1235                                    process[i].rw_range_start,
1236                                    process[i].rw_last_file_pos,
1237                                    process[i].rw_smallest_extent,
1238                                    process[i].rw_largest_extent,
1239                                    process[i].rw_offset);
1240         }
1241         spin_unlock(&sbi->ll_lock);
1242
1243         return 0;
1244 }
1245
1246 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1247                                        size_t len, loff_t *off)
1248 {
1249         struct seq_file *seq = file->private_data;
1250         struct ll_sb_info *sbi = seq->private;
1251         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1252         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1253
1254         spin_lock(&sbi->ll_lock);
1255         sbi->ll_offset_process_count = 0;
1256         sbi->ll_rw_offset_entry_count = 0;
1257         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1258                LL_PROCESS_HIST_MAX);
1259         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1260                LL_OFFSET_HIST_MAX);
1261         spin_unlock(&sbi->ll_lock);
1262
1263         return len;
1264 }
1265
1266 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1267
1268 LPROCFS_INIT_VARS(llite, NULL, lprocfs_obd_vars)
1269 #endif /* LPROCFS */