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