Whamcloud - gitweb
b=22660 Return kernel's locking return code to when lustre reports success
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #define DEBUG_SUBSYSTEM S_LLITE
37
38 #include <linux/version.h>
39 #include <lustre_lite.h>
40 #include <lprocfs_status.h>
41 #include <linux/seq_file.h>
42 #include <obd_support.h>
43
44 #include "llite_internal.h"
45
46 struct proc_dir_entry *proc_lustre_fs_root;
47
48 #ifdef LPROCFS
49 /* /proc/lustre/llite mount point registration */
50 struct file_operations llite_dump_pgcache_fops;
51 struct file_operations ll_rw_extents_stats_fops;
52 struct file_operations ll_rw_extents_stats_pp_fops;
53 struct file_operations ll_rw_offset_stats_fops;
54
55 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
56                          int *eof, void *data)
57 {
58         struct super_block *sb = (struct super_block *)data;
59         struct obd_statfs osfs;
60         int rc;
61
62         LASSERT(sb != NULL);
63         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
64                                 OBD_STATFS_NODELAY);
65         if (!rc) {
66               *eof = 1;
67               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
68         }
69
70         return rc;
71 }
72
73 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
74                              int *eof, void *data)
75 {
76         struct super_block *sb = (struct super_block *)data;
77         struct obd_statfs osfs;
78         int rc;
79
80         LASSERT(sb != NULL);
81         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
82                                 OBD_STATFS_NODELAY);
83         if (!rc) {
84                 __u32 blk_size = osfs.os_bsize >> 10;
85                 __u64 result = osfs.os_blocks;
86
87                 while (blk_size >>= 1)
88                         result <<= 1;
89
90                 *eof = 1;
91                 rc = snprintf(page, count, LPU64"\n", result);
92         }
93         return rc;
94
95 }
96
97 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
98                             int *eof, void *data)
99 {
100         struct super_block *sb = (struct super_block *)data;
101         struct obd_statfs osfs;
102         int rc;
103
104         LASSERT(sb != NULL);
105         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
106                                 OBD_STATFS_NODELAY);
107         if (!rc) {
108                 __u32 blk_size = osfs.os_bsize >> 10;
109                 __u64 result = osfs.os_bfree;
110
111                 while (blk_size >>= 1)
112                         result <<= 1;
113
114                 *eof = 1;
115                 rc = snprintf(page, count, LPU64"\n", result);
116         }
117         return rc;
118 }
119
120 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
121                              int *eof, void *data)
122 {
123         struct super_block *sb = (struct super_block *)data;
124         struct obd_statfs osfs;
125         int rc;
126
127         LASSERT(sb != NULL);
128         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
129                                 OBD_STATFS_NODELAY);
130         if (!rc) {
131                 __u32 blk_size = osfs.os_bsize >> 10;
132                 __u64 result = osfs.os_bavail;
133
134                 while (blk_size >>= 1)
135                         result <<= 1;
136
137                 *eof = 1;
138                 rc = snprintf(page, count, LPU64"\n", result);
139         }
140         return rc;
141 }
142
143 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
144                             int *eof, void *data)
145 {
146         struct super_block *sb = (struct super_block *)data;
147         struct obd_statfs osfs;
148         int rc;
149
150         LASSERT(sb != NULL);
151         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
152                                 OBD_STATFS_NODELAY);
153         if (!rc) {
154                  *eof = 1;
155                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
156         }
157         return rc;
158 }
159
160 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
161                            int *eof, void *data)
162 {
163         struct super_block *sb = (struct super_block *)data;
164         struct obd_statfs osfs;
165         int rc;
166
167         LASSERT(sb != NULL);
168         rc = ll_statfs_internal(sb, &osfs, cfs_time_current_64() - HZ,
169                                 OBD_STATFS_NODELAY);
170         if (!rc) {
171                  *eof = 1;
172                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
173         }
174         return rc;
175
176 }
177
178 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
179                         int *eof, void *data)
180 {
181         struct super_block *sb = (struct super_block*)data;
182
183         LASSERT(sb != NULL);
184         *eof = 1;
185         return snprintf(page, count, "%s\n", sb->s_type->name);
186 }
187
188 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
189                          int *eof, void *data)
190 {
191         struct super_block *sb = (struct super_block *)data;
192
193         LASSERT(sb != NULL);
194         *eof = 1;
195         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
196 }
197
198 static int ll_rd_max_readahead_mb(char *page, char **start, off_t off,
199                                    int count, int *eof, void *data)
200 {
201         struct super_block *sb = data;
202         struct ll_sb_info *sbi = ll_s2sbi(sb);
203         long pages_number;
204         int mult;
205
206         spin_lock(&sbi->ll_lock);
207         pages_number = sbi->ll_ra_info.ra_max_pages;
208         spin_unlock(&sbi->ll_lock);
209
210         mult = 1 << (20 - CFS_PAGE_SHIFT);
211         return lprocfs_read_frac_helper(page, count, pages_number, mult);
212 }
213
214 static int ll_wr_max_readahead_mb(struct file *file, const char *buffer,
215                                    unsigned long count, void *data)
216 {
217         struct super_block *sb = data;
218         struct ll_sb_info *sbi = ll_s2sbi(sb);
219         int mult, rc, pages_number;
220
221         mult = 1 << (20 - CFS_PAGE_SHIFT);
222         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
223         if (rc)
224                 return rc;
225
226         if (pages_number < 0 || pages_number > num_physpages / 2) {
227                 CERROR("can't set file readahead more than %lu MB\n",
228                         num_physpages >> (20 - CFS_PAGE_SHIFT + 1)); /*1/2 of RAM*/
229                 return -ERANGE;
230         }
231
232         spin_lock(&sbi->ll_lock);
233         sbi->ll_ra_info.ra_max_pages = pages_number;
234         spin_unlock(&sbi->ll_lock);
235
236         return count;
237 }
238
239 static int ll_rd_max_readahead_per_file_mb(char *page, char **start, off_t off,
240                                           int count, int *eof, void *data)
241 {
242         struct super_block *sb = data;
243         struct ll_sb_info *sbi = ll_s2sbi(sb);
244         long pages_number;
245         int mult;
246
247         spin_lock(&sbi->ll_lock);
248         pages_number = sbi->ll_ra_info.ra_max_pages_per_file;
249         spin_unlock(&sbi->ll_lock);
250
251         mult = 1 << (20 - CFS_PAGE_SHIFT);
252         return lprocfs_read_frac_helper(page, count, pages_number, mult);
253 }
254
255 static int ll_wr_max_readahead_per_file_mb(struct file *file, const char *buffer,
256                                           unsigned long count, void *data)
257 {
258         struct super_block *sb = data;
259         struct ll_sb_info *sbi = ll_s2sbi(sb);
260         int mult, rc, pages_number;
261
262         mult = 1 << (20 - CFS_PAGE_SHIFT);
263         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
264         if (rc)
265                 return rc;
266
267         if (pages_number < 0 ||
268                 pages_number > sbi->ll_ra_info.ra_max_pages) {
269                 CERROR("can't set file readahead more than"
270                        "max_read_ahead_mb %lu MB\n", sbi->ll_ra_info.ra_max_pages);
271                 return -ERANGE;
272         }
273
274         spin_lock(&sbi->ll_lock);
275         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
276         spin_unlock(&sbi->ll_lock);
277
278         return count;
279 }
280
281 static int ll_rd_max_read_ahead_whole_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_ra_info.ra_max_read_ahead_whole_pages;
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_read_ahead_whole_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         /* Cap this at the current max readahead window size, the readahead
310          * algorithm does this anyway so it's pointless to set it larger. */
311         if (pages_number < 0 ||
312             pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
313                 CERROR("can't set max_read_ahead_whole_mb more than "
314                        "max_read_ahead_per_file_mb: %lu\n",
315                         sbi->ll_ra_info.ra_max_pages_per_file >> (20 - CFS_PAGE_SHIFT));
316                 return -ERANGE;
317         }
318
319         spin_lock(&sbi->ll_lock);
320         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
321         spin_unlock(&sbi->ll_lock);
322
323         return count;
324 }
325
326 static int ll_rd_max_cached_mb(char *page, char **start, off_t off,
327                                int count, int *eof, void *data)
328 {
329         struct super_block *sb = data;
330         struct ll_sb_info *sbi = ll_s2sbi(sb);
331         long pages_number;
332         int mult;
333
334         spin_lock(&sbi->ll_lock);
335         pages_number = sbi->ll_async_page_max;
336         spin_unlock(&sbi->ll_lock);
337
338         mult = 1 << (20 - CFS_PAGE_SHIFT);
339         return lprocfs_read_frac_helper(page, count, pages_number, mult);;
340 }
341
342 static int ll_wr_max_cached_mb(struct file *file, const char *buffer,
343                                   unsigned long count, void *data)
344 {
345         struct super_block *sb = data;
346         struct ll_sb_info *sbi = ll_s2sbi(sb);
347         unsigned long budget;
348         int mult, rc, pages_number, cpu;
349
350         mult = 1 << (20 - CFS_PAGE_SHIFT);
351         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
352         if (rc)
353                 return rc;
354
355         if (pages_number < 0 || pages_number > num_physpages) {
356                 CERROR("can't set max cache more than %lu MB\n",
357                         num_physpages >> (20 - CFS_PAGE_SHIFT));
358                 return -ERANGE;
359         }
360
361         spin_lock(&sbi->ll_lock);
362         sbi->ll_async_page_max = pages_number ;
363         spin_unlock(&sbi->ll_lock);
364
365         if (!sbi->ll_osc_exp)
366                 /* Not set up yet, don't call llap_shrink_cache */
367                 return count;
368
369         spin_lock(&sbi->ll_async_page_reblnc_lock);
370         budget = sbi->ll_async_page_max / num_online_cpus();
371         for_each_online_cpu(cpu)
372                 LL_PGLIST_DATA_CPU(sbi, cpu)->llpd_budget = budget;
373         spin_unlock(&sbi->ll_async_page_reblnc_lock);
374
375         if (lcounter_read_positive(&sbi->ll_async_page_count) >=
376             sbi->ll_async_page_max)
377                 llap_shrink_cache(sbi, -1);
378
379         return count;
380 }
381
382 static int ll_rd_pgcache_balance(char *page, char **start, off_t off,
383                                  int count, int *eof, void *data)
384 {
385         struct super_block *sb = data;
386         struct ll_sb_info *sbi = ll_s2sbi(sb);
387         struct ll_pglist_data *pd;
388         unsigned long total_budget = 0;
389         int n = 0, cpu;
390
391         n += snprintf(page +n, count - n, "cpu\tpage count\tbudget"
392                       "\t\treblnc count\tgen\thit\tmiss\tcross\n");
393         for_each_online_cpu(cpu) {
394                 pd = LL_PGLIST_DATA_CPU(sbi, cpu);
395                 n += snprintf(page + n, count - n,
396                               "%d\t%-8lu\t%-8lu\t%-8lu\t%lu\t%lu\t%lu\t%lu\n",
397                               cpu, pd->llpd_count, pd->llpd_budget,
398                               pd->llpd_reblnc_count, pd->llpd_gen,
399                               pd->llpd_hit, pd->llpd_miss, pd->llpd_cross);
400                 total_budget += pd->llpd_budget;
401         }
402         n += snprintf(page + n, count - n,
403                       "Total budget: %lu, page max: %lu, rebalance cnt: %lu\n",
404                       total_budget, sbi->ll_async_page_max,
405                       sbi->ll_async_page_reblnc_count);
406         *eof = 1;
407         return n;
408 }
409
410 static int ll_rd_checksum(char *page, char **start, off_t off,
411                           int count, int *eof, void *data)
412 {
413         struct super_block *sb = data;
414         struct ll_sb_info *sbi = ll_s2sbi(sb);
415
416         return snprintf(page, count, "%u\n",
417                         (sbi->ll_flags & LL_SBI_LLITE_CHECKSUM) ? 1 : 0);
418 }
419
420 static int ll_wr_checksum(struct file *file, const char *buffer,
421                           unsigned long count, void *data)
422 {
423         struct super_block *sb = data;
424         struct ll_sb_info *sbi = ll_s2sbi(sb);
425         int val, rc;
426
427         if (!sbi->ll_osc_exp)
428                 /* Not set up yet */
429                 return -EAGAIN;
430
431         rc = lprocfs_write_helper(buffer, count, &val);
432         if (rc)
433                 return rc;
434         if (val)
435                 sbi->ll_flags |=  (LL_SBI_LLITE_CHECKSUM|LL_SBI_DATA_CHECKSUM);
436         else
437                 sbi->ll_flags &= ~(LL_SBI_LLITE_CHECKSUM|LL_SBI_DATA_CHECKSUM);
438
439         rc = obd_set_info_async(sbi->ll_osc_exp, sizeof(KEY_CHECKSUM), KEY_CHECKSUM,
440                                 sizeof(val), &val, NULL);
441         if (rc)
442                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
443
444         return count;
445 }
446
447 static int ll_rd_max_rw_chunk(char *page, char **start, off_t off,
448                           int count, int *eof, void *data)
449 {
450         struct super_block *sb = data;
451
452         return snprintf(page, count, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
453 }
454
455 static int ll_wr_max_rw_chunk(struct file *file, const char *buffer,
456                           unsigned long count, void *data)
457 {
458         struct super_block *sb = data;
459         int rc, val;
460
461         rc = lprocfs_write_helper(buffer, count, &val);
462         if (rc)
463                 return rc;
464         ll_s2sbi(sb)->ll_max_rw_chunk = val;
465         return count;
466 }
467
468 static int ll_rd_track_id(char *page, int count, void *data,
469                           enum stats_track_type type)
470 {
471         struct super_block *sb = data;
472
473         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
474                 return snprintf(page, count, "%d\n",
475                                 ll_s2sbi(sb)->ll_stats_track_id);
476
477         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
478                 return snprintf(page, count, "0 (all)\n");
479         } else {
480                 return snprintf(page, count, "untracked\n");
481         }
482 }
483
484 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
485                           enum stats_track_type type)
486 {
487         struct super_block *sb = data;
488         int rc, pid;
489
490         rc = lprocfs_write_helper(buffer, count, &pid);
491         if (rc)
492                 return rc;
493         ll_s2sbi(sb)->ll_stats_track_id = pid;
494         if (pid == 0)
495                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
496         else
497                 ll_s2sbi(sb)->ll_stats_track_type = type;
498         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
499         return count;
500 }
501
502 static int ll_rd_track_pid(char *page, char **start, off_t off,
503                           int count, int *eof, void *data)
504 {
505         return (ll_rd_track_id(page, count, data, STATS_TRACK_PID));
506 }
507
508 static int ll_wr_track_pid(struct file *file, const char *buffer,
509                           unsigned long count, void *data)
510 {
511         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PID));
512 }
513
514 static int ll_rd_track_ppid(char *page, char **start, off_t off,
515                           int count, int *eof, void *data)
516 {
517         return (ll_rd_track_id(page, count, data, STATS_TRACK_PPID));
518 }
519
520 static int ll_wr_track_ppid(struct file *file, const char *buffer,
521                           unsigned long count, void *data)
522 {
523         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_PPID));
524 }
525
526 static int ll_rd_track_gid(char *page, char **start, off_t off,
527                           int count, int *eof, void *data)
528 {
529         return (ll_rd_track_id(page, count, data, STATS_TRACK_GID));
530 }
531
532 static int ll_wr_track_gid(struct file *file, const char *buffer,
533                           unsigned long count, void *data)
534 {
535         return (ll_wr_track_id(buffer, count, data, STATS_TRACK_GID));
536 }
537
538 static int ll_rd_contention_time(char *page, char **start, off_t off,
539                                  int count, int *eof, void *data)
540 {
541         struct super_block *sb = data;
542
543         *eof = 1;
544         return snprintf(page, count, "%u\n", ll_s2sbi(sb)->ll_contention_time);
545
546 }
547
548 static int ll_wr_contention_time(struct file *file, const char *buffer,
549                                  unsigned long count, void *data)
550 {
551         struct super_block *sb = data;
552         struct ll_sb_info *sbi = ll_s2sbi(sb);
553
554         return lprocfs_write_helper(buffer, count,&sbi->ll_contention_time) ?:
555                 count;
556 }
557
558 static int ll_rd_lockless_truncate(char *page, char **start, off_t off,
559                                    int count, int *eof, void *data)
560 {
561         struct super_block *sb = data;
562
563         *eof = 1;
564         return snprintf(page, count, "%u\n",
565                         ll_s2sbi(sb)->ll_lockless_truncate_enable);
566 }
567
568 static int ll_wr_lockless_truncate(struct file *file, const char *buffer,
569                                    unsigned long count, void *data)
570 {
571         struct super_block *sb = data;
572         struct ll_sb_info *sbi = ll_s2sbi(sb);
573
574         return lprocfs_write_helper(buffer, count,
575                                     &sbi->ll_lockless_truncate_enable)
576                 ?: count;
577 }
578
579 static int ll_rd_direct_io_default(char *page, char **start, off_t off,
580                                      int count, int *eof, void *data)
581 {
582         struct super_block *sb = data;
583
584         *eof = 1;
585         return snprintf(page, count, "%u\n",
586                         ll_s2sbi(sb)->ll_direct_io_default);
587 }
588
589 static int ll_wr_direct_io_default(struct file *file, const char *buffer,
590                                      unsigned long count, void *data)
591 {
592         struct super_block *sb = data;
593         struct ll_sb_info *sbi = ll_s2sbi(sb);
594
595         return lprocfs_write_helper(buffer, count,
596                                     &sbi->ll_direct_io_default)
597                 ?: count;
598 }
599
600 static int ll_rd_lockless_direct_io(char *page, char **start, off_t off,
601                                     int count, int *eof, void *data)
602 {
603         struct super_block *sb = data;
604
605         *eof = 1;
606         return snprintf(page, count, "%u\n",
607                         ll_s2sbi(sb)->ll_lockless_direct_io);
608 }
609
610 static int ll_wr_lockless_direct_io(struct file *file, const char *buffer,
611                                     unsigned long count, void *data)
612 {
613         struct super_block *sb = data;
614         struct ll_sb_info *sbi = ll_s2sbi(sb);
615
616         return lprocfs_write_helper(buffer, count,
617                                     &sbi->ll_lockless_direct_io)
618                 ?: count;
619 }
620
621 static int ll_rd_statahead_max(char *page, char **start, off_t off,
622                                int count, int *eof, void *data)
623 {
624         struct super_block *sb = data;
625         struct ll_sb_info *sbi = ll_s2sbi(sb);
626
627         return snprintf(page, count, "%u\n", sbi->ll_sa_max);
628 }
629
630 static int ll_wr_statahead_max(struct file *file, const char *buffer,
631                                unsigned long count, void *data)
632 {
633         struct super_block *sb = data;
634         struct ll_sb_info *sbi = ll_s2sbi(sb);
635         int val, rc;
636
637         rc = lprocfs_write_helper(buffer, count, &val);
638         if (rc)
639                 return rc;
640
641         if (val >= 0 && val <= LL_SA_RPC_MAX)
642                 sbi->ll_sa_max = val;
643         else
644                 CERROR("Bad statahead_max value %d. Valid values are in the "
645                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
646
647         return count;
648 }
649
650 static int ll_rd_statahead_stats(char *page, char **start, off_t off,
651                                  int count, int *eof, void *data)
652 {
653         struct super_block *sb = data;
654         struct ll_sb_info *sbi = ll_s2sbi(sb);
655
656         return snprintf(page, count,
657                         "statahead wrong: %u\n"
658                         "statahead total: %u\n"
659                         "ls blocked:      %llu\n"
660                         "ls cached:       %llu\n"
661                         "hit count:       %llu\n"
662                         "miss count:      %llu\n",
663                         sbi->ll_sa_wrong,
664                         sbi->ll_sa_total,
665                         sbi->ll_sa_blocked,
666                         sbi->ll_sa_cached,
667                         sbi->ll_sa_hit,
668                         sbi->ll_sa_miss);
669 }
670
671 static int ll_rd_lazystatfs(char *page, char **start, off_t off,
672                             int count, int *eof, void *data)
673 {
674         struct super_block *sb = data;
675         struct ll_sb_info *sbi = ll_s2sbi(sb);
676
677         return snprintf(page, count, "%u\n",
678                         (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
679 }
680
681 static int ll_wr_lazystatfs(struct file *file, const char *buffer,
682                             unsigned long count, void *data)
683 {
684         struct super_block *sb = data;
685         struct ll_sb_info *sbi = ll_s2sbi(sb);
686         int val, rc;
687
688         rc = lprocfs_write_helper(buffer, count, &val);
689         if (rc)
690                 return rc;
691
692         if (val)
693                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
694         else
695                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
696
697         return count;
698 }
699
700 static struct lprocfs_vars lprocfs_llite_obd_vars[] = {
701         { "uuid",         ll_rd_sb_uuid,          0, 0 },
702         //{ "mntpt_path",   ll_rd_path,             0, 0 },
703         { "fstype",       ll_rd_fstype,           0, 0 },
704         { "blocksize",    ll_rd_blksize,          0, 0 },
705         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
706         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
707         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
708         { "filestotal",   ll_rd_filestotal,       0, 0 },
709         { "filesfree",    ll_rd_filesfree,        0, 0 },
710         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
711         { "max_read_ahead_mb", ll_rd_max_readahead_mb,
712                                ll_wr_max_readahead_mb, 0 },
713         { "max_read_ahead_per_file_mb", ll_rd_max_readahead_per_file_mb,
714                                         ll_wr_max_readahead_per_file_mb, 0 },
715         { "max_read_ahead_whole_mb", ll_rd_max_read_ahead_whole_mb,
716                                      ll_wr_max_read_ahead_whole_mb, 0 },
717         { "max_cached_mb",  ll_rd_max_cached_mb, ll_wr_max_cached_mb, 0 },
718         { "pgcache_balance",ll_rd_pgcache_balance, 0, 0 },
719         { "checksum_pages", ll_rd_checksum, ll_wr_checksum, 0 },
720         { "max_rw_chunk",   ll_rd_max_rw_chunk, ll_wr_max_rw_chunk, 0 },
721         { "stats_track_pid",  ll_rd_track_pid, ll_wr_track_pid, 0 },
722         { "stats_track_ppid", ll_rd_track_ppid, ll_wr_track_ppid, 0 },
723         { "stats_track_gid",  ll_rd_track_gid, ll_wr_track_gid, 0 },
724         { "contention_seconds", ll_rd_contention_time,
725                                 ll_wr_contention_time, 0},
726         { "lockless_truncate", ll_rd_lockless_truncate,
727                                ll_wr_lockless_truncate, 0},
728         { "lockless_direct_io", ll_rd_lockless_direct_io,
729                                ll_wr_lockless_direct_io, 0},
730         { "direct_io_default", ll_rd_direct_io_default,
731                                ll_wr_direct_io_default, 0},
732         { "statahead_max",      ll_rd_statahead_max, ll_wr_statahead_max, 0 },
733         { "statahead_stats",    ll_rd_statahead_stats, 0, 0 },
734         { "lazystatfs",         ll_rd_lazystatfs, ll_wr_lazystatfs, 0 },
735         { 0 }
736 };
737
738 #define MAX_STRING_SIZE 128
739
740 struct llite_file_opcode {
741         __u32       opcode;
742         __u32       type;
743         const char *opname;
744 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
745         /* file operation */
746         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
747         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
748         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
749                                    "writeback_from_writepage" },
750         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
751                                    "writeback_from_pressure" },
752         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
753                                    "writeback_ok_pages" },
754         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
755                                    "writeback_failed_pages" },
756         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
757                                    "read_bytes" },
758         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
759                                    "write_bytes" },
760         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
761                                    "brw_read" },
762         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
763                                    "brw_write" },
764
765         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
766         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
767         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
768         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
769         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
770         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
771         /* inode operation */
772         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
773         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
774         { LPROC_LL_LOCKLESS_TRUNC, LPROCFS_TYPE_REGS, "lockless_truncate" },
775         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
776         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
777         /* special inode operation */
778         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
779         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
780         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
781         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
782         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
783         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
784         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
785         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
786                                    "direct_read" },
787         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
788                                    "direct_write" },
789         { LPROC_LL_LOCKLESS_READ,  LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
790                                    "lockless_read_bytes" },
791         { LPROC_LL_LOCKLESS_WRITE, LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
792                                    "lockless_write_bytes" },
793
794 };
795
796 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
797 {
798         if (!sbi->ll_stats)
799                 return;
800         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
801                 lprocfs_counter_add(sbi->ll_stats, op, count);
802         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
803                  sbi->ll_stats_track_id == current->pid)
804                 lprocfs_counter_add(sbi->ll_stats, op, count);
805         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
806                  sbi->ll_stats_track_id == current->parent->pid)
807                 lprocfs_counter_add(sbi->ll_stats, op, count);
808         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
809                  sbi->ll_stats_track_id == cfs_curproc_gid())
810                 lprocfs_counter_add(sbi->ll_stats, op, count);
811 }
812 EXPORT_SYMBOL(ll_stats_ops_tally);
813
814 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
815                                 struct super_block *sb, char *osc, char *mdc)
816 {
817         struct lprocfs_vars lvars[2];
818         struct lustre_sb_info *lsi = s2lsi(sb);
819         struct ll_sb_info *sbi = ll_s2sbi(sb);
820         struct obd_device *obd;
821         char name[MAX_STRING_SIZE + 1], *ptr;
822         int err, id, len;
823         struct proc_dir_entry *entry;
824         static const char *ra_stats_string[] = LL_RA_STAT_STRINGS;
825         ENTRY;
826
827         memset(lvars, 0, sizeof(lvars));
828
829         name[MAX_STRING_SIZE] = '\0';
830         lvars[0].name = name;
831
832         LASSERT(sbi != NULL);
833         LASSERT(mdc != NULL);
834         LASSERT(osc != NULL);
835
836         /* Get fsname */
837         len = strlen(lsi->lsi_lmd->lmd_profile);
838         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
839         if (ptr && (strcmp(ptr, "-client") == 0))
840                 len -= 7;
841
842         /* Mount info */
843         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
844                  lsi->lsi_lmd->lmd_profile, sb);
845
846         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
847         if (IS_ERR(sbi->ll_proc_root)) {
848                 err = PTR_ERR(sbi->ll_proc_root);
849                 sbi->ll_proc_root = NULL;
850                 RETURN(err);
851         }
852
853         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
854         if (entry == NULL)
855                 GOTO(out, err = -ENOMEM);
856         entry->proc_fops = &llite_dump_pgcache_fops;
857         entry->data = sbi;
858
859         sbi->ll_ra_stats = lprocfs_alloc_stats(LL_RA_STAT,
860                                                LPROCFS_STATS_FLAG_NONE);
861         for (id = 0; id < LL_RA_STAT; id++)
862                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
863                         ra_stats_string[id], "pages");
864         lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
865                 sbi->ll_ra_stats);
866
867         entry = create_proc_entry("extents_stats", 0644, sbi->ll_proc_root);
868         if (entry == NULL)
869                  GOTO(out, err = -ENOMEM);
870         entry->proc_fops = &ll_rw_extents_stats_fops;
871         entry->data = sbi;
872
873         entry = create_proc_entry("extents_stats_per_process", 0644,
874                                   sbi->ll_proc_root);
875         if (entry == NULL)
876                  GOTO(out, err = -ENOMEM);
877         entry->proc_fops = &ll_rw_extents_stats_pp_fops;
878         entry->data = sbi;
879
880         entry = create_proc_entry("offset_stats", 0644, sbi->ll_proc_root);
881         if (entry == NULL)
882                 GOTO(out, err = -ENOMEM);
883         entry->proc_fops = &ll_rw_offset_stats_fops;
884         entry->data = sbi;
885
886         /* File operations stats */
887         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
888                                             LPROCFS_STATS_FLAG_NONE);
889         if (sbi->ll_stats == NULL)
890                 GOTO(out, err = -ENOMEM);
891         /* do counter init */
892         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
893                 __u32 type = llite_opcode_table[id].type;
894                 void *ptr = NULL;
895                 if (type & LPROCFS_TYPE_REGS)
896                         ptr = "regs";
897                 else if (type & LPROCFS_TYPE_BYTES)
898                         ptr = "bytes";
899                 else if (type & LPROCFS_TYPE_PAGES)
900                         ptr = "pages";
901                 lprocfs_counter_init(sbi->ll_stats,
902                                      llite_opcode_table[id].opcode,
903                                      (type & LPROCFS_CNTR_AVGMINMAX),
904                                      llite_opcode_table[id].opname, ptr);
905         }
906         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
907         if (err)
908                 GOTO(out, err);
909
910         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
911         if (err)
912                 GOTO(out, err);
913
914         /* MDC info */
915         obd = class_name2obd(mdc);
916
917         LASSERT(obd != NULL);
918         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
919         LASSERT(obd->obd_type->typ_name != NULL);
920
921         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
922                  obd->obd_type->typ_name);
923         lvars[0].read_fptr = lprocfs_rd_name;
924         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
925         if (err)
926                 GOTO(out, err);
927
928         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
929         lvars[0].read_fptr = lprocfs_rd_uuid;
930         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
931         if (err)
932                 GOTO(out, err);
933
934         /* OSC */
935         obd = class_name2obd(osc);
936
937         LASSERT(obd != NULL);
938         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
939         LASSERT(obd->obd_type->typ_name != NULL);
940
941         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
942                  obd->obd_type->typ_name);
943         lvars[0].read_fptr = lprocfs_rd_name;
944         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
945         if (err)
946                 GOTO(out, err);
947
948         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
949         lvars[0].read_fptr = lprocfs_rd_uuid;
950         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
951 out:
952         if (err) {
953                 lprocfs_remove(&sbi->ll_proc_root);
954                 lprocfs_free_stats(&sbi->ll_stats);
955         }
956         RETURN(err);
957 }
958
959 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
960 {
961         if (sbi->ll_proc_root) {
962                 lprocfs_remove(&sbi->ll_proc_root);
963                 lprocfs_free_stats(&sbi->ll_ra_stats);
964                 lprocfs_free_stats(&sbi->ll_stats);
965         }
966 }
967 #undef MAX_STRING_SIZE
968
969 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
970                 if (test_bit(PG_##flag, &(page)->flags)) {              \
971                         if (!has_flags)                                 \
972                                 has_flags = 1;                          \
973                         else                                            \
974                                 seq_putc(seq, '|');                     \
975                         seq_puts(seq, #flag);                           \
976                 }                                                       \
977         } while(0)
978
979 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
980 {
981         struct ll_async_page *dummy_llap = seq->private;
982
983         if (dummy_llap->llap_magic == 2)
984                 return NULL;
985
986         return (void *)1;
987 }
988
989 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
990 {
991         struct ll_async_page *llap, *dummy_llap = seq->private;
992         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
993         struct ll_pglist_data *pd;
994         int cpu = dummy_llap->llap_pglist_cpu;
995
996         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
997          * it in our own state */
998         if (dummy_llap->llap_magic == 0) {
999                 seq_printf(seq, "gener |  llap  cookie  origin wq du wb | page "
1000                                 "inode index count [ page flags ]\n");
1001                 return 0;
1002         }
1003
1004         pd = ll_pglist_cpu_lock(sbi, cpu);
1005         llap = llite_pglist_next_llap(&pd->llpd_list,
1006                                       &dummy_llap->llap_pglist_item);
1007         if (llap != NULL)  {
1008                 int has_flags = 0, i;
1009                 struct page *page = llap->llap_page;
1010                 unsigned long gen = 0UL;
1011
1012                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
1013                          llap->llap_origin);
1014
1015                 for_each_online_cpu(i)
1016                          gen += LL_PGLIST_DATA_CPU(sbi, i)->llpd_gen;
1017
1018                 seq_printf(seq," %5lu | %p %p %s %s %s %s | %p %lu/%u(%p) "
1019                            "%lu %u [",
1020                            gen,
1021                            llap, llap->llap_cookie,
1022                            llap_origins[llap->llap_origin],
1023                            llap->llap_write_queued ? "wq" : "- ",
1024                            llap->llap_defer_uptodate ? "du" : "- ",
1025                            PageWriteback(page) ? "wb" : "-",
1026                            page, page->mapping->host->i_ino,
1027                            page->mapping->host->i_generation,
1028                            page->mapping->host, page->index,
1029                            page_count(page));
1030                 seq_page_flag(seq, page, locked, has_flags);
1031                 seq_page_flag(seq, page, error, has_flags);
1032                 seq_page_flag(seq, page, referenced, has_flags);
1033                 seq_page_flag(seq, page, uptodate, has_flags);
1034                 seq_page_flag(seq, page, dirty, has_flags);
1035 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,12))
1036                 seq_page_flag(seq, page, highmem, has_flags);
1037 #endif
1038                 seq_page_flag(seq, page, writeback, has_flags);
1039                 if (!has_flags)
1040                         seq_puts(seq, "-]\n");
1041                 else
1042                         seq_puts(seq, "]\n");
1043         }
1044         ll_pglist_cpu_unlock(sbi, cpu);
1045
1046         return 0;
1047 }
1048
1049 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v,
1050                                          loff_t *pos)
1051 {
1052         struct ll_async_page *llap, *dummy_llap = seq->private;
1053         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
1054         struct ll_pglist_data *pd, *next;
1055         int cpu = dummy_llap->llap_pglist_cpu;
1056
1057         /* bail if we just displayed the banner */
1058         if (dummy_llap->llap_magic == 0) {
1059                 dummy_llap->llap_magic = 1;
1060                 return dummy_llap;
1061         }
1062
1063         /* we've just displayed the llap that is after us in the list.
1064          * we advance to a position beyond it, returning null if there
1065          * isn't another llap in the list beyond that new position. */
1066         pd = ll_pglist_cpu_lock(sbi, cpu);
1067         llap = llite_pglist_next_llap(&pd->llpd_list,
1068                         &dummy_llap->llap_pglist_item);
1069         list_del_init(&dummy_llap->llap_pglist_item);
1070         if (llap) {
1071                 list_add(&dummy_llap->llap_pglist_item,&llap->llap_pglist_item);
1072                 llap = llite_pglist_next_llap(&pd->llpd_list,
1073                                 &dummy_llap->llap_pglist_item);
1074         }
1075         if (llap == NULL) {
1076                 int i = cpu + 1;
1077                 for (next = NULL; i < num_possible_cpus(); i++, next = NULL) {
1078                         next = ll_pglist_cpu_lock(sbi, i);
1079                         if (!list_empty(&next->llpd_list))
1080                                 break;
1081                         ll_pglist_cpu_unlock(sbi, i);
1082                 }
1083                 if (next != NULL) {
1084                         list_move(&dummy_llap->llap_pglist_item,
1085                                   &next->llpd_list);
1086                         dummy_llap->llap_pglist_cpu = i;
1087                         ll_pglist_cpu_unlock(sbi, cpu);
1088                         llap = llite_pglist_next_llap(&next->llpd_list,
1089                                         &dummy_llap->llap_pglist_item);
1090                         LASSERT(llap);
1091                         cpu = i;
1092                 }
1093         }
1094         ll_pglist_cpu_unlock(sbi, cpu);
1095
1096         ++*pos;
1097         if (llap == NULL) {
1098                 dummy_llap->llap_magic = 2;
1099                 return NULL;
1100         }
1101         return dummy_llap;
1102 }
1103
1104 static void null_stop(struct seq_file *seq, void *v)
1105 {
1106 }
1107
1108 struct seq_operations llite_dump_pgcache_seq_sops = {
1109         .start = llite_dump_pgcache_seq_start,
1110         .stop = null_stop,
1111         .next = llite_dump_pgcache_seq_next,
1112         .show = llite_dump_pgcache_seq_show,
1113 };
1114
1115 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
1116  * while we walk the entire list, and we don't want to have to seek into
1117  * the right position in the list as an app advances with many syscalls.  we
1118  * allocate a dummy llap and hang it off file->private.  its position in
1119  * the list records where the app is currently displaying.  this way our
1120  * seq .start and .stop don't actually do anything.  .next returns null
1121  * when the dummy hits the end of the list which eventually leads to .release
1122  * where we tear down.  this kind of displaying is super-racey, so we put
1123  * a generation counter on the list so the output shows when the list
1124  * changes between reads.
1125  */
1126 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
1127 {
1128         struct proc_dir_entry *dp = PDE(inode);
1129         struct ll_async_page *dummy_llap;
1130         struct seq_file *seq;
1131         struct ll_sb_info *sbi = dp->data;
1132         struct ll_pglist_data *pd;
1133         int rc = -ENOMEM;
1134
1135         LPROCFS_ENTRY_AND_CHECK(dp);
1136
1137         OBD_ALLOC_PTR_WAIT(dummy_llap);
1138         if (dummy_llap == NULL)
1139                 GOTO(out, rc);
1140
1141         dummy_llap->llap_page = NULL;
1142         dummy_llap->llap_cookie = sbi;
1143         dummy_llap->llap_magic = 0;
1144         dummy_llap->llap_pglist_cpu = 0;
1145
1146         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
1147         if (rc) {
1148                 OBD_FREE(dummy_llap, sizeof(*dummy_llap));
1149                 GOTO(out, rc);
1150         }
1151         seq = file->private_data;
1152         seq->private = dummy_llap;
1153
1154         pd = ll_pglist_cpu_lock(sbi, 0);
1155         list_add(&dummy_llap->llap_pglist_item, &pd->llpd_list);
1156         ll_pglist_cpu_unlock(sbi, 0);
1157
1158 out:
1159         if (rc)
1160                 LPROCFS_EXIT();
1161         return rc;
1162 }
1163
1164 static int llite_dump_pgcache_seq_release(struct inode *inode,
1165                                           struct file *file)
1166 {
1167         struct seq_file *seq = file->private_data;
1168         struct ll_async_page *dummy_llap = seq->private;
1169         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
1170         int cpu = dummy_llap->llap_pglist_cpu;
1171
1172         ll_pglist_cpu_lock(sbi, cpu);
1173         if (!list_empty(&dummy_llap->llap_pglist_item))
1174                 list_del_init(&dummy_llap->llap_pglist_item);
1175         ll_pglist_cpu_unlock(sbi, cpu);
1176         OBD_FREE(dummy_llap, sizeof(*dummy_llap));
1177
1178         return lprocfs_seq_release(inode, file);
1179 }
1180
1181 struct file_operations llite_dump_pgcache_fops = {
1182         .owner   = THIS_MODULE,
1183         .open    = llite_dump_pgcache_seq_open,
1184         .read    = seq_read,
1185         .release = llite_dump_pgcache_seq_release,
1186 };
1187
1188 #define pct(a,b) (b ? a * 100 / b : 0)
1189
1190 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1191                                    struct seq_file *seq, int which)
1192 {
1193         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1194         unsigned long start, end, r, w;
1195         char *unitp = "KMGTPEZY";
1196         int i, units = 10;
1197         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1198
1199         read_cum = 0;
1200         write_cum = 0;
1201         start = 0;
1202
1203         for(i = 0; i < LL_HIST_MAX; i++) {
1204                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1205                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1206         }
1207
1208         for(i = 0; i < LL_HIST_MAX; i++) {
1209                 r = pp_info->pp_r_hist.oh_buckets[i];
1210                 w = pp_info->pp_w_hist.oh_buckets[i];
1211                 read_cum += r;
1212                 write_cum += w;
1213                 end = 1 << (i + LL_HIST_START - units);
1214                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1215                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1216                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1217                            r, pct(r, read_tot), pct(read_cum, read_tot),
1218                            w, pct(w, write_tot), pct(write_cum, write_tot));
1219                 start = end;
1220                 if (start == 1<<10) {
1221                         start = 1;
1222                         units += 10;
1223                         unitp++;
1224                 }
1225                 if (read_cum == read_tot && write_cum == write_tot)
1226                         break;
1227         }
1228 }
1229
1230 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1231 {
1232         struct timeval now;
1233         struct ll_sb_info *sbi = seq->private;
1234         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1235         int k;
1236
1237         do_gettimeofday(&now);
1238
1239         if (!sbi->ll_rw_stats_on) {
1240                 seq_printf(seq, "disabled\n"
1241                                 "write anything in this file to activate, "
1242                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1243                 return 0;
1244         }
1245         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1246                    now.tv_sec, now.tv_usec);
1247         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1248         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1249                    "extents", "calls", "%", "cum%",
1250                    "calls", "%", "cum%");
1251         spin_lock(&sbi->ll_pp_extent_lock);
1252         for(k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1253                 if(io_extents->pp_extents[k].pid != 0) {
1254                         seq_printf(seq, "\nPID: %d\n",
1255                                    io_extents->pp_extents[k].pid);
1256                         ll_display_extents_info(io_extents, seq, k);
1257                 }
1258         }
1259         spin_unlock(&sbi->ll_pp_extent_lock);
1260         return 0;
1261 }
1262
1263 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1264                                                 const char *buf, size_t len,
1265                                                 loff_t *off)
1266 {
1267         struct seq_file *seq = file->private_data;
1268         struct ll_sb_info *sbi = seq->private;
1269         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1270         int i;
1271         int value = 1, rc = 0;
1272
1273         rc = lprocfs_write_helper(buf, len, &value);
1274         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1275                        strcmp(buf, "Disabled") == 0))
1276                 value = 0;
1277
1278         if (value == 0)
1279                 sbi->ll_rw_stats_on = 0;
1280         else
1281                 sbi->ll_rw_stats_on = 1;
1282
1283         spin_lock(&sbi->ll_pp_extent_lock);
1284         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1285                 io_extents->pp_extents[i].pid = 0;
1286                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1287                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1288         }
1289         spin_unlock(&sbi->ll_pp_extent_lock);
1290         return len;
1291 }
1292
1293 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1294
1295 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1296 {
1297         struct timeval now;
1298         struct ll_sb_info *sbi = seq->private;
1299         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1300
1301         do_gettimeofday(&now);
1302
1303         if (!sbi->ll_rw_stats_on) {
1304                 seq_printf(seq, "disabled\n"
1305                                 "write anything in this file to activate, "
1306                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1307                 return 0;
1308         }
1309         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1310                    now.tv_sec, now.tv_usec);
1311
1312         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1313         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1314                    "extents", "calls", "%", "cum%",
1315                    "calls", "%", "cum%");
1316         spin_lock(&sbi->ll_lock);
1317         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1318         spin_unlock(&sbi->ll_lock);
1319
1320         return 0;
1321 }
1322
1323 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1324                                         size_t len, loff_t *off)
1325 {
1326         struct seq_file *seq = file->private_data;
1327         struct ll_sb_info *sbi = seq->private;
1328         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1329         int i;
1330         int value = 1, rc = 0;
1331
1332         rc = lprocfs_write_helper(buf, len, &value);
1333         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1334                        strcmp(buf, "Disabled") == 0))
1335                 value = 0;
1336
1337         if (value == 0)
1338                 sbi->ll_rw_stats_on = 0;
1339         else
1340                 sbi->ll_rw_stats_on = 1;
1341         spin_lock(&sbi->ll_pp_extent_lock);
1342         for(i = 0; i <= LL_PROCESS_HIST_MAX; i++)
1343         {
1344                 io_extents->pp_extents[i].pid = 0;
1345                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1346                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1347         }
1348         spin_unlock(&sbi->ll_pp_extent_lock);
1349
1350         return len;
1351 }
1352
1353 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1354
1355 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid, struct file
1356                                *file, size_t count, int rw)
1357 {
1358         int i, cur = -1;
1359         struct ll_rw_process_info *process;
1360         struct ll_rw_process_info *offset;
1361         int *off_count = &sbi->ll_rw_offset_entry_count;
1362         int *process_count = &sbi->ll_offset_process_count;
1363         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1364
1365         if(!sbi->ll_rw_stats_on)
1366                 return;
1367         process = sbi->ll_rw_process_info;
1368         offset = sbi->ll_rw_offset_info;
1369
1370         spin_lock(&sbi->ll_pp_extent_lock);
1371         /* Extent statistics */
1372         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1373                 if(io_extents->pp_extents[i].pid == pid) {
1374                         cur = i;
1375                         break;
1376                 }
1377         }
1378
1379         if (cur == -1) {
1380                 /* new process */
1381                 sbi->ll_extent_process_count =
1382                         (sbi->ll_extent_process_count+1) % LL_PROCESS_HIST_MAX;
1383                 cur = sbi->ll_extent_process_count;
1384                 io_extents->pp_extents[cur].pid = pid;
1385                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1386                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1387         }
1388
1389         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1390              (i < (LL_HIST_MAX - 1)); i++);
1391         if (rw == 0) {
1392                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1393                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1394         } else {
1395                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1396                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1397         }
1398         spin_unlock(&sbi->ll_pp_extent_lock);
1399
1400         spin_lock(&sbi->ll_process_lock);
1401         /* Offset statistics */
1402         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1403                 if (process[i].rw_pid == pid) {
1404                         if (process[i].rw_last_file != file) {
1405                                 process[i].rw_range_start = file->f_pos;
1406                                 process[i].rw_last_file_pos =
1407                                                         file->f_pos + count;
1408                                 process[i].rw_smallest_extent = count;
1409                                 process[i].rw_largest_extent = count;
1410                                 process[i].rw_offset = 0;
1411                                 process[i].rw_last_file = file;
1412                                 spin_unlock(&sbi->ll_process_lock);
1413                                 return;
1414                         }
1415                         if (process[i].rw_last_file_pos != file->f_pos) {
1416                                 *off_count =
1417                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1418                                 offset[*off_count].rw_op = process[i].rw_op;
1419                                 offset[*off_count].rw_pid = pid;
1420                                 offset[*off_count].rw_range_start =
1421                                         process[i].rw_range_start;
1422                                 offset[*off_count].rw_range_end =
1423                                         process[i].rw_last_file_pos;
1424                                 offset[*off_count].rw_smallest_extent =
1425                                         process[i].rw_smallest_extent;
1426                                 offset[*off_count].rw_largest_extent =
1427                                         process[i].rw_largest_extent;
1428                                 offset[*off_count].rw_offset =
1429                                         process[i].rw_offset;
1430                                 process[i].rw_op = rw;
1431                                 process[i].rw_range_start = file->f_pos;
1432                                 process[i].rw_smallest_extent = count;
1433                                 process[i].rw_largest_extent = count;
1434                                 process[i].rw_offset = file->f_pos -
1435                                         process[i].rw_last_file_pos;
1436                         }
1437                         if(process[i].rw_smallest_extent > count)
1438                                 process[i].rw_smallest_extent = count;
1439                         if(process[i].rw_largest_extent < count)
1440                                 process[i].rw_largest_extent = count;
1441                         process[i].rw_last_file_pos = file->f_pos + count;
1442                         spin_unlock(&sbi->ll_process_lock);
1443                         return;
1444                 }
1445         }
1446         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1447         process[*process_count].rw_pid = pid;
1448         process[*process_count].rw_op = rw;
1449         process[*process_count].rw_range_start = file->f_pos;
1450         process[*process_count].rw_last_file_pos = file->f_pos + count;
1451         process[*process_count].rw_smallest_extent = count;
1452         process[*process_count].rw_largest_extent = count;
1453         process[*process_count].rw_offset = 0;
1454         process[*process_count].rw_last_file = file;
1455         spin_unlock(&sbi->ll_process_lock);
1456 }
1457
1458 char lpszt[] = LPSZ;
1459
1460 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1461 {
1462         struct timeval now;
1463         struct ll_sb_info *sbi = seq->private;
1464         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1465         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1466         char format[50];
1467         int i;
1468
1469         do_gettimeofday(&now);
1470
1471         if (!sbi->ll_rw_stats_on) {
1472                 seq_printf(seq, "disabled\n"
1473                                 "write anything in this file to activate, "
1474                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1475                 return 0;
1476         }
1477         spin_lock(&sbi->ll_process_lock);
1478
1479         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1480                    now.tv_sec, now.tv_usec);
1481         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1482                    "R/W", "PID", "RANGE START", "RANGE END",
1483                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1484         sprintf(format, "%s%s%s%s%s\n",
1485                 "%3c %10d %14Lu %14Lu %17", lpszt+1, " %17", lpszt+1, " %14Ld");
1486         /* We stored the discontiguous offsets here; print them first */
1487         for(i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1488                 if (offset[i].rw_pid != 0)
1489                         /* Is there a way to snip the '%' off of LPSZ? */
1490                         seq_printf(seq, format,
1491                                    offset[i].rw_op ? 'W' : 'R',
1492                                    offset[i].rw_pid,
1493                                    offset[i].rw_range_start,
1494                                    offset[i].rw_range_end,
1495                                    offset[i].rw_smallest_extent,
1496                                    offset[i].rw_largest_extent,
1497                                    offset[i].rw_offset);
1498         }
1499         /* Then print the current offsets for each process */
1500         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1501                 if (process[i].rw_pid != 0)
1502                         seq_printf(seq, format,
1503                                    process[i].rw_op ? 'W' : 'R',
1504                                    process[i].rw_pid,
1505                                    process[i].rw_range_start,
1506                                    process[i].rw_last_file_pos,
1507                                    process[i].rw_smallest_extent,
1508                                    process[i].rw_largest_extent,
1509                                    process[i].rw_offset);
1510         }
1511         spin_unlock(&sbi->ll_process_lock);
1512
1513         return 0;
1514 }
1515
1516 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1517                                        size_t len, loff_t *off)
1518 {
1519         struct seq_file *seq = file->private_data;
1520         struct ll_sb_info *sbi = seq->private;
1521         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1522         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1523         int value = 1, rc = 0;
1524
1525         rc = lprocfs_write_helper(buf, len, &value);
1526
1527         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1528                            strcmp(buf, "Disabled") == 0))
1529                 value = 0;
1530
1531         if (value == 0)
1532                 sbi->ll_rw_stats_on = 0;
1533         else
1534                 sbi->ll_rw_stats_on = 1;
1535
1536         spin_lock(&sbi->ll_process_lock);
1537         sbi->ll_offset_process_count = 0;
1538         sbi->ll_rw_offset_entry_count = 0;
1539         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1540                LL_PROCESS_HIST_MAX);
1541         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1542                LL_OFFSET_HIST_MAX);
1543         spin_unlock(&sbi->ll_process_lock);
1544
1545         return len;
1546 }
1547
1548 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1549
1550 void lprocfs_llite_init_vars(struct lprocfs_static_vars *lvars)
1551 {
1552     lvars->module_vars  = NULL;
1553     lvars->obd_vars     = lprocfs_llite_obd_vars;
1554 }
1555 #endif /* LPROCFS */