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