Whamcloud - gitweb
b=12203
[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 stats_track_type type)
372 {
373         struct super_block *sb = data;
374
375         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
376                 return snprintf(page, count, "%d\n",
377                                 ll_s2sbi(sb)->ll_stats_track_id);
378         
379         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_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 stats_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_stats_track_id = pid;
396         if (pid == 0)
397                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
398         else
399                 ll_s2sbi(sb)->ll_stats_track_type = type;
400         lprocfs_clear_stats(ll_s2sbi(sb)->ll_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, STATS_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, STATS_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, STATS_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, STATS_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, STATS_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, STATS_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         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
459         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
460         { "stats_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, "truncate" },
500         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
501 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
502         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
503 #else
504         { LPROC_LL_REVALIDATE,     LPROCFS_TYPE_REGS, "getattr" },
505 #endif
506         /* special inode operation */
507         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
508         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
509         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
510         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
511         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
512         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
513         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
514         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
515                                    "direct_read" },
516         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
517                                    "direct_write" },
518
519 };
520
521 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
522 {
523         if (!sbi->ll_stats)
524                 return;
525         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
526                 lprocfs_counter_add(sbi->ll_stats, op, count);
527         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
528                  sbi->ll_stats_track_id == current->pid)
529                 lprocfs_counter_add(sbi->ll_stats, op, count);
530         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
531                  sbi->ll_stats_track_id == current->p_pptr->pid)
532                 lprocfs_counter_add(sbi->ll_stats, op, count);
533         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
534                  sbi->ll_stats_track_id == current->gid)
535                 lprocfs_counter_add(sbi->ll_stats, op, count);
536 }
537
538 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
539                                 struct super_block *sb, char *osc, char *mdc)
540 {
541         struct lprocfs_vars lvars[2];
542         struct lustre_sb_info *lsi = s2lsi(sb);
543         struct ll_sb_info *sbi = ll_s2sbi(sb);
544         struct obd_device *obd;
545         char name[MAX_STRING_SIZE + 1], *ptr;
546         int err, id, len;
547         struct proc_dir_entry *entry;
548         ENTRY;
549
550         memset(lvars, 0, sizeof(lvars));
551
552         name[MAX_STRING_SIZE] = '\0';
553         lvars[0].name = name;
554
555         LASSERT(sbi != NULL);
556         LASSERT(mdc != NULL);
557         LASSERT(osc != NULL);
558
559         /* Get fsname */
560         len = strlen(lsi->lsi_lmd->lmd_profile);
561         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
562         if (ptr && (strcmp(ptr, "-client") == 0))
563                 len -= 7; 
564         
565         /* Mount info */
566         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
567                  lsi->lsi_lmd->lmd_profile, sb);
568         
569         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
570         if (IS_ERR(sbi->ll_proc_root)) {
571                 err = PTR_ERR(sbi->ll_proc_root);
572                 sbi->ll_proc_root = NULL;
573                 RETURN(err);
574         }
575
576         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
577         if (entry == NULL)
578                 GOTO(out, err = -ENOMEM);
579         entry->proc_fops = &llite_dump_pgcache_fops;
580         entry->data = sbi;
581
582         entry = create_proc_entry("read_ahead_stats", 0644, sbi->ll_proc_root);
583         if (entry == NULL)
584                 GOTO(out, err = -ENOMEM);
585         entry->proc_fops = &ll_ra_stats_fops;
586         entry->data = sbi;
587
588         entry = create_proc_entry("extents_stats", 0644, sbi->ll_proc_root);
589         if (entry == NULL)
590                  GOTO(out, err = -ENOMEM);
591         entry->proc_fops = &ll_rw_extents_stats_fops;
592         entry->data = sbi;
593
594         entry = create_proc_entry("extents_stats_per_process", 0644,
595                                   sbi->ll_proc_root);
596         if (entry == NULL)
597                  GOTO(out, err = -ENOMEM);
598         entry->proc_fops = &ll_rw_extents_stats_pp_fops;
599         entry->data = sbi;
600
601         entry = create_proc_entry("offset_stats", 0644, sbi->ll_proc_root);
602         if (entry == NULL)
603                 GOTO(out, err = -ENOMEM);
604         entry->proc_fops = &ll_rw_offset_stats_fops;
605         entry->data = sbi;
606
607         /* File operations stats */
608         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES);
609         if (sbi->ll_stats == NULL)
610                 GOTO(out, err = -ENOMEM);
611         /* do counter init */
612         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
613                 __u32 type = llite_opcode_table[id].type;
614                 void *ptr = NULL;
615                 if (type & LPROCFS_TYPE_REGS)
616                         ptr = "regs";
617                 else if (type & LPROCFS_TYPE_BYTES)
618                         ptr = "bytes";
619                 else if (type & LPROCFS_TYPE_PAGES)
620                         ptr = "pages";
621                 lprocfs_counter_init(sbi->ll_stats,
622                                      llite_opcode_table[id].opcode,
623                                      (type & LPROCFS_CNTR_AVGMINMAX),
624                                      llite_opcode_table[id].opname, ptr);
625         }
626         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
627         if (err)
628                 GOTO(out, err);
629
630         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_obd_vars, sb);
631         if (err)
632                 GOTO(out, err);
633
634         /* MDC info */
635         obd = class_name2obd(mdc);
636
637         LASSERT(obd != NULL);
638         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
639         LASSERT(obd->obd_type->typ_name != NULL);
640
641         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
642                  obd->obd_type->typ_name);
643         lvars[0].read_fptr = lprocfs_rd_name;
644         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
645         if (err)
646                 GOTO(out, err);
647
648         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
649         lvars[0].read_fptr = lprocfs_rd_uuid;
650         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
651         if (err)
652                 GOTO(out, err);
653
654         /* OSC */
655         obd = class_name2obd(osc);
656
657         LASSERT(obd != NULL);
658         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
659         LASSERT(obd->obd_type->typ_name != NULL);
660
661         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
662                  obd->obd_type->typ_name);
663         lvars[0].read_fptr = lprocfs_rd_name;
664         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
665         if (err)
666                 GOTO(out, err);
667
668         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
669         lvars[0].read_fptr = lprocfs_rd_uuid;
670         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
671 out:
672         if (err) {
673                 lprocfs_remove(&sbi->ll_proc_root);
674                 lprocfs_free_stats(&sbi->ll_stats);
675         }
676         RETURN(err);
677 }
678
679 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
680 {
681         if (sbi->ll_proc_root) {
682                 lprocfs_remove(&sbi->ll_proc_root);
683                 lprocfs_free_stats(&sbi->ll_stats);
684         }
685 }
686 #undef MAX_STRING_SIZE
687
688 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
689                 if (test_bit(PG_##flag, &(page)->flags)) {              \
690                         if (!has_flags)                                 \
691                                 has_flags = 1;                          \
692                         else                                            \
693                                 seq_putc(seq, '|');                     \
694                         seq_puts(seq, #flag);                           \
695                 }                                                       \
696         } while(0);
697
698 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
699 {
700         struct ll_async_page *dummy_llap = seq->private;
701
702         if (dummy_llap->llap_magic == 2)
703                 return NULL;
704
705         return (void *)1;
706 }
707
708 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
709 {
710         struct ll_async_page *llap, *dummy_llap = seq->private;
711         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
712
713         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
714          * it in our own state */
715         if (dummy_llap->llap_magic == 0) {
716                 seq_printf(seq, "gener |  llap  cookie  origin wq du wb | page "
717                                 "inode index count [ page flags ]\n");
718                 return 0;
719         }
720
721         spin_lock(&sbi->ll_lock);
722
723         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_pglist_item);
724         if (llap != NULL)  {
725                 int has_flags = 0;
726                 struct page *page = llap->llap_page;
727
728                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
729                          llap->llap_origin);
730
731                 seq_printf(seq," %5lu | %p %p %s %s %s %s | %p %lu/%u(%p) "
732                            "%lu %u [",
733                            sbi->ll_pglist_gen,
734                            llap, llap->llap_cookie,
735                            llap_origins[llap->llap_origin],
736                            llap->llap_write_queued ? "wq" : "- ",
737                            llap->llap_defer_uptodate ? "du" : "- ",
738                            PageWriteback(page) ? "wb" : "-",
739                            page, page->mapping->host->i_ino,
740                            page->mapping->host->i_generation,
741                            page->mapping->host, page->index,
742                            page_count(page));
743                 seq_page_flag(seq, page, locked, has_flags);
744                 seq_page_flag(seq, page, error, has_flags);
745                 seq_page_flag(seq, page, referenced, has_flags);
746                 seq_page_flag(seq, page, uptodate, has_flags);
747                 seq_page_flag(seq, page, dirty, has_flags);
748 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,12))
749                 seq_page_flag(seq, page, highmem, has_flags);
750 #endif
751                 seq_page_flag(seq, page, writeback, has_flags);
752                 if (!has_flags)
753                         seq_puts(seq, "-]\n");
754                 else
755                         seq_puts(seq, "]\n");
756         }
757
758         spin_unlock(&sbi->ll_lock);
759
760         return 0;
761 }
762
763 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v, 
764                                          loff_t *pos)
765 {
766         struct ll_async_page *llap, *dummy_llap = seq->private;
767         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
768
769         /* bail if we just displayed the banner */
770         if (dummy_llap->llap_magic == 0) {
771                 dummy_llap->llap_magic = 1;
772                 return dummy_llap;
773         }
774
775         /* we've just displayed the llap that is after us in the list.
776          * we advance to a position beyond it, returning null if there
777          * isn't another llap in the list beyond that new position. */
778         spin_lock(&sbi->ll_lock);
779         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_pglist_item);
780         list_del_init(&dummy_llap->llap_pglist_item);
781         if (llap) {
782                 list_add(&dummy_llap->llap_pglist_item,&llap->llap_pglist_item);
783                 llap =llite_pglist_next_llap(sbi,&dummy_llap->llap_pglist_item);
784         }
785         spin_unlock(&sbi->ll_lock);
786
787         ++*pos;
788         if (llap == NULL) {
789                 dummy_llap->llap_magic = 2;
790                 return NULL;
791         }
792         return dummy_llap;
793 }
794
795 static void null_stop(struct seq_file *seq, void *v)
796 {
797 }
798
799 struct seq_operations llite_dump_pgcache_seq_sops = {
800         .start = llite_dump_pgcache_seq_start,
801         .stop = null_stop,
802         .next = llite_dump_pgcache_seq_next,
803         .show = llite_dump_pgcache_seq_show,
804 };
805
806 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
807  * while we walk the entire list, and we don't want to have to seek into
808  * the right position in the list as an app advances with many syscalls.  we
809  * allocate a dummy llap and hang it off file->private.  its position in
810  * the list records where the app is currently displaying.  this way our
811  * seq .start and .stop don't actually do anything.  .next returns null
812  * when the dummy hits the end of the list which eventually leads to .release
813  * where we tear down.  this kind of displaying is super-racey, so we put
814  * a generation counter on the list so the output shows when the list
815  * changes between reads.
816  */
817 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
818 {
819         struct proc_dir_entry *dp = PDE(inode);
820         struct ll_async_page *dummy_llap;
821         struct seq_file *seq;
822         struct ll_sb_info *sbi = dp->data;
823         int rc = -ENOMEM;
824
825         LPROCFS_ENTRY_AND_CHECK(dp);
826
827         OBD_ALLOC_GFP(dummy_llap, sizeof(*dummy_llap), GFP_KERNEL);
828         if (dummy_llap == NULL)
829                 GOTO(out, rc);
830
831         dummy_llap->llap_page = NULL;
832         dummy_llap->llap_cookie = sbi;
833         dummy_llap->llap_magic = 0;
834
835         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
836         if (rc) {
837                 OBD_FREE(dummy_llap, sizeof(*dummy_llap));
838                 GOTO(out, rc);
839         }
840         seq = file->private_data;
841         seq->private = dummy_llap;
842
843         spin_lock(&sbi->ll_lock);
844         list_add(&dummy_llap->llap_pglist_item, &sbi->ll_pglist);
845         spin_unlock(&sbi->ll_lock);
846
847 out:
848         if (rc)
849                 LPROCFS_EXIT();
850         return rc;
851 }
852
853 static int llite_dump_pgcache_seq_release(struct inode *inode,
854                                           struct file *file)
855 {
856         struct seq_file *seq = file->private_data;
857         struct ll_async_page *dummy_llap = seq->private;
858         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
859
860         spin_lock(&sbi->ll_lock);
861         if (!list_empty(&dummy_llap->llap_pglist_item))
862                 list_del_init(&dummy_llap->llap_pglist_item);
863         spin_unlock(&sbi->ll_lock);
864         OBD_FREE(dummy_llap, sizeof(*dummy_llap));
865
866         return lprocfs_seq_release(inode, file);
867 }
868
869 struct file_operations llite_dump_pgcache_fops = {
870         .owner   = THIS_MODULE,
871         .open    = llite_dump_pgcache_seq_open,
872         .read    = seq_read,
873         .release = llite_dump_pgcache_seq_release,
874 };
875
876 static int ll_ra_stats_seq_show(struct seq_file *seq, void *v)
877 {
878         struct timeval now;
879         struct ll_sb_info *sbi = seq->private;
880         struct ll_ra_info *ra = &sbi->ll_ra_info;
881         int i;
882         static char *ra_stat_strings[] = {
883                 [RA_STAT_HIT] = "hits",
884                 [RA_STAT_MISS] = "misses",
885                 [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
886                 [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
887                 [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
888                 [RA_STAT_FAILED_MATCH] = "failed lock match",
889                 [RA_STAT_DISCARDED] = "read but discarded",
890                 [RA_STAT_ZERO_LEN] = "zero length file",
891                 [RA_STAT_ZERO_WINDOW] = "zero size window",
892                 [RA_STAT_EOF] = "read-ahead to EOF",
893                 [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
894                 [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
895         };
896
897         do_gettimeofday(&now);
898
899         spin_lock(&sbi->ll_lock);
900
901         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
902                    now.tv_sec, now.tv_usec);
903         seq_printf(seq, "pending issued pages:           %lu\n",
904                    ra->ra_cur_pages);
905
906         for(i = 0; i < _NR_RA_STAT; i++)
907                 seq_printf(seq, "%-25s %lu\n", ra_stat_strings[i], 
908                            ra->ra_stats[i]);
909
910         spin_unlock(&sbi->ll_lock);
911
912         return 0;
913 }
914
915 static ssize_t ll_ra_stats_seq_write(struct file *file, const char *buf,
916                                        size_t len, loff_t *off)
917 {
918         struct seq_file *seq = file->private_data;
919         struct ll_sb_info *sbi = seq->private;
920         struct ll_ra_info *ra = &sbi->ll_ra_info;
921
922         spin_lock(&sbi->ll_lock);
923         memset(ra->ra_stats, 0, sizeof(ra->ra_stats));
924         spin_unlock(&sbi->ll_lock);
925
926         return len;
927 }
928
929 LPROC_SEQ_FOPS(ll_ra_stats);
930
931 #define pct(a,b) (b ? a * 100 / b : 0)
932
933 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
934                                    struct seq_file *seq, int which)
935 {
936         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
937         unsigned long start, end, r, w;
938         char *unitp = "KMGTPEZY";
939         int i, units = 10;
940         struct per_process_info *pp_info = &io_extents->pp_extents[which];
941
942         read_cum = 0;
943         write_cum = 0;
944         start = 0;
945
946         for(i = 0; i < LL_HIST_MAX; i++) {
947                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
948                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
949         }
950
951         for(i = 0; i < LL_HIST_MAX; i++) {
952                 r = pp_info->pp_r_hist.oh_buckets[i];
953                 w = pp_info->pp_w_hist.oh_buckets[i];
954                 read_cum += r;
955                 write_cum += w;
956                 end = 1 << (i + LL_HIST_START - units);
957                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
958                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
959                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
960                            r, pct(r, read_tot), pct(read_cum, read_tot),
961                            w, pct(w, write_tot), pct(write_cum, write_tot));
962                 start = end;
963                 if (start == 1<<10) {
964                         start = 1;
965                         units += 10;
966                         unitp++;
967                 }
968                 if (read_cum == read_tot && write_cum == write_tot)
969                         break;
970         }
971 }
972
973 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
974 {
975         struct timeval now;
976         struct ll_sb_info *sbi = seq->private;
977         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
978         int k;
979
980         do_gettimeofday(&now);
981
982         if (!sbi->ll_rw_stats_on) {
983                 seq_printf(seq, "Disabled\n"
984                                 "Write anything in this file to activate\n");
985                 return 0;
986         }
987         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
988                    now.tv_sec, now.tv_usec);
989         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
990         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n", 
991                    "extents", "calls", "%", "cum%",
992                    "calls", "%", "cum%");
993         spin_lock(&sbi->ll_pp_extent_lock);
994         for(k = 0; k < LL_PROCESS_HIST_MAX; k++) {
995                 if(io_extents->pp_extents[k].pid != 0) {
996                         seq_printf(seq, "\nPID: %d\n",
997                                    io_extents->pp_extents[k].pid);
998                         ll_display_extents_info(io_extents, seq, k);
999                 }
1000         }
1001         spin_unlock(&sbi->ll_pp_extent_lock);
1002         return 0;
1003 }
1004
1005 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1006                                                 const char *buf, size_t len,
1007                                                 loff_t *off)
1008 {
1009         struct seq_file *seq = file->private_data;
1010         struct ll_sb_info *sbi = seq->private;
1011         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1012         int i;
1013
1014         sbi->ll_rw_stats_on = 1;
1015         spin_lock(&sbi->ll_pp_extent_lock);
1016         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1017                 io_extents->pp_extents[i].pid = 0;
1018                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1019                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1020         }
1021         spin_unlock(&sbi->ll_pp_extent_lock);
1022         return len;
1023 }
1024
1025 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1026
1027 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1028 {
1029         struct timeval now;
1030         struct ll_sb_info *sbi = seq->private;
1031         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1032
1033         do_gettimeofday(&now);
1034
1035         if (!sbi->ll_rw_stats_on) {
1036                 seq_printf(seq, "Disabled\n"
1037                                 "Write anything in this file to activate\n");
1038                 return 0;
1039         }
1040         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1041                    now.tv_sec, now.tv_usec);
1042
1043         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1044         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n", 
1045                    "extents", "calls", "%", "cum%",
1046                    "calls", "%", "cum%");
1047         spin_lock(&sbi->ll_lock);
1048         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1049         spin_unlock(&sbi->ll_lock);
1050
1051         return 0;
1052 }
1053
1054 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1055                                         size_t len, loff_t *off)
1056 {
1057         struct seq_file *seq = file->private_data;
1058         struct ll_sb_info *sbi = seq->private;
1059         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1060         int i;
1061
1062         sbi->ll_rw_stats_on = 1;
1063         spin_lock(&sbi->ll_pp_extent_lock);
1064         for(i = 0; i <= LL_PROCESS_HIST_MAX; i++)
1065         {
1066                 io_extents->pp_extents[i].pid = 0;
1067                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1068                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1069         }
1070         spin_unlock(&sbi->ll_pp_extent_lock);
1071
1072         return len;
1073 }
1074
1075 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1076
1077 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid, struct file
1078                                *file, size_t count, int rw)
1079 {
1080         int i, cur = -1;
1081         struct ll_rw_process_info *process;
1082         struct ll_rw_process_info *offset;
1083         int *off_count = &sbi->ll_rw_offset_entry_count;
1084         int *process_count = &sbi->ll_offset_process_count;
1085         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1086
1087         if(!sbi->ll_rw_stats_on)
1088                 return;
1089         process = sbi->ll_rw_process_info;
1090         offset = sbi->ll_rw_offset_info;
1091
1092         spin_lock(&sbi->ll_pp_extent_lock);
1093         /* Extent statistics */
1094         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1095                 if(io_extents->pp_extents[i].pid == pid) {
1096                         cur = i;
1097                         break;
1098                 }
1099         }
1100
1101         if (cur == -1) {
1102                 /* new process */
1103                 sbi->ll_extent_process_count = 
1104                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1105                 cur = sbi->ll_extent_process_count;
1106                 io_extents->pp_extents[cur].pid = pid;
1107                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1108                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1109         }
1110
1111         for(i = 0; (count >= (1 << LL_HIST_START << i)) && 
1112              (i < (LL_HIST_MAX - 1)); i++);
1113         if (rw == 0) {
1114                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1115                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1116         } else {
1117                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1118                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1119         }
1120         spin_unlock(&sbi->ll_pp_extent_lock);
1121
1122         spin_lock(&sbi->ll_process_lock);
1123         /* Offset statistics */
1124         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1125                 if (process[i].rw_pid == pid) {
1126                         if (process[i].rw_last_file != file) {
1127                                 process[i].rw_range_start = file->f_pos;
1128                                 process[i].rw_last_file_pos =
1129                                                         file->f_pos + count;
1130                                 process[i].rw_smallest_extent = count;
1131                                 process[i].rw_largest_extent = count;
1132                                 process[i].rw_offset = 0;
1133                                 process[i].rw_last_file = file;
1134                                 spin_unlock(&sbi->ll_process_lock);
1135                                 return;
1136                         }
1137                         if (process[i].rw_last_file_pos != file->f_pos) {
1138                                 *off_count =
1139                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1140                                 offset[*off_count].rw_op = process[i].rw_op;
1141                                 offset[*off_count].rw_pid = pid;
1142                                 offset[*off_count].rw_range_start =
1143                                         process[i].rw_range_start;
1144                                 offset[*off_count].rw_range_end =
1145                                         process[i].rw_last_file_pos;
1146                                 offset[*off_count].rw_smallest_extent =
1147                                         process[i].rw_smallest_extent;
1148                                 offset[*off_count].rw_largest_extent =
1149                                         process[i].rw_largest_extent;
1150                                 offset[*off_count].rw_offset =
1151                                         process[i].rw_offset;
1152                                 process[i].rw_op = rw;
1153                                 process[i].rw_range_start = file->f_pos;
1154                                 process[i].rw_smallest_extent = count;
1155                                 process[i].rw_largest_extent = count;
1156                                 process[i].rw_offset = file->f_pos -
1157                                         process[i].rw_last_file_pos;
1158                         }
1159                         if(process[i].rw_smallest_extent > count)
1160                                 process[i].rw_smallest_extent = count;
1161                         if(process[i].rw_largest_extent < count)
1162                                 process[i].rw_largest_extent = count;
1163                         process[i].rw_last_file_pos = file->f_pos + count;
1164                         spin_unlock(&sbi->ll_process_lock);
1165                         return;
1166                 }
1167         }
1168         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1169         process[*process_count].rw_pid = pid;
1170         process[*process_count].rw_op = rw;
1171         process[*process_count].rw_range_start = file->f_pos;
1172         process[*process_count].rw_last_file_pos = file->f_pos + count;
1173         process[*process_count].rw_smallest_extent = count;
1174         process[*process_count].rw_largest_extent = count;
1175         process[*process_count].rw_offset = 0;
1176         process[*process_count].rw_last_file = file;
1177         spin_unlock(&sbi->ll_process_lock);
1178 }
1179
1180 char lpszt[] = LPSZ;
1181
1182 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1183 {
1184         struct timeval now;
1185         struct ll_sb_info *sbi = seq->private;
1186         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1187         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1188         char format[50];
1189         int i;
1190
1191         do_gettimeofday(&now);
1192
1193         if (!sbi->ll_rw_stats_on) {
1194                 seq_printf(seq, "Disabled\n"
1195                                 "Write anything in this file to activate\n");
1196                 return 0;
1197         }
1198         spin_lock(&sbi->ll_process_lock);
1199
1200         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1201                    now.tv_sec, now.tv_usec);
1202         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1203                    "R/W", "PID", "RANGE START", "RANGE END",
1204                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1205         sprintf(format, "%s%s%s%s%s\n",
1206                 "%3c %10d %14Lu %14Lu %17", lpszt+1, " %17", lpszt+1, " %14Ld");
1207         /* We stored the discontiguous offsets here; print them first */
1208         for(i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1209                 if (offset[i].rw_pid != 0)
1210                         /* Is there a way to snip the '%' off of LPSZ? */
1211                         seq_printf(seq, format,
1212                                    offset[i].rw_op ? 'W' : 'R',
1213                                    offset[i].rw_pid,
1214                                    offset[i].rw_range_start,
1215                                    offset[i].rw_range_end,
1216                                    offset[i].rw_smallest_extent,
1217                                    offset[i].rw_largest_extent,
1218                                    offset[i].rw_offset);
1219         }
1220         /* Then print the current offsets for each process */
1221         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1222                 if (process[i].rw_pid != 0)
1223                         seq_printf(seq, format,
1224                                    process[i].rw_op ? 'W' : 'R',
1225                                    process[i].rw_pid,
1226                                    process[i].rw_range_start,
1227                                    process[i].rw_last_file_pos,
1228                                    process[i].rw_smallest_extent,
1229                                    process[i].rw_largest_extent,
1230                                    process[i].rw_offset);
1231         }
1232         spin_unlock(&sbi->ll_process_lock);
1233
1234         return 0;
1235 }
1236
1237 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1238                                        size_t len, loff_t *off)
1239 {
1240         struct seq_file *seq = file->private_data;
1241         struct ll_sb_info *sbi = seq->private;
1242         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1243         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1244
1245         sbi->ll_rw_stats_on = 1;
1246
1247         spin_lock(&sbi->ll_process_lock);
1248         sbi->ll_offset_process_count = 0;
1249         sbi->ll_rw_offset_entry_count = 0;
1250         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1251                LL_PROCESS_HIST_MAX);
1252         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1253                LL_OFFSET_HIST_MAX);
1254         spin_unlock(&sbi->ll_process_lock);
1255
1256         return len;
1257 }
1258
1259 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1260
1261 LPROCFS_INIT_VARS(llite, NULL, lprocfs_obd_vars)
1262 #endif /* LPROCFS */