Whamcloud - gitweb
LU-3321 clio: optimize read ahead code
[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, 2013, Intel Corporation.
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 <lustre_param.h>
41 #include <lprocfs_status.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_blksize_seq_show(struct seq_file *m, void *v)
56 {
57         struct super_block *sb = m->private;
58         struct obd_statfs osfs;
59         int rc;
60
61         LASSERT(sb != NULL);
62         rc = ll_statfs_internal(sb, &osfs,
63                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
64                                 OBD_STATFS_NODELAY);
65         if (!rc)
66                 rc = seq_printf(m, "%u\n", osfs.os_bsize);
67         return rc;
68 }
69 LPROC_SEQ_FOPS_RO(ll_blksize);
70
71 static int ll_kbytestotal_seq_show(struct seq_file *m, void *v)
72 {
73         struct super_block *sb = m->private;
74         struct obd_statfs osfs;
75         int rc;
76
77         LASSERT(sb != NULL);
78         rc = ll_statfs_internal(sb, &osfs,
79                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
80                                 OBD_STATFS_NODELAY);
81         if (!rc) {
82                 __u32 blk_size = osfs.os_bsize >> 10;
83                 __u64 result = osfs.os_blocks;
84
85                 while (blk_size >>= 1)
86                         result <<= 1;
87
88                 rc = seq_printf(m, LPU64"\n", result);
89         }
90         return rc;
91 }
92 LPROC_SEQ_FOPS_RO(ll_kbytestotal);
93
94 static int ll_kbytesfree_seq_show(struct seq_file *m, void *v)
95 {
96         struct super_block *sb = m->private;
97         struct obd_statfs osfs;
98         int rc;
99
100         LASSERT(sb != NULL);
101         rc = ll_statfs_internal(sb, &osfs,
102                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
103                                 OBD_STATFS_NODELAY);
104         if (!rc) {
105                 __u32 blk_size = osfs.os_bsize >> 10;
106                 __u64 result = osfs.os_bfree;
107
108                 while (blk_size >>= 1)
109                         result <<= 1;
110
111                 rc = seq_printf(m, LPU64"\n", result);
112         }
113         return rc;
114 }
115 LPROC_SEQ_FOPS_RO(ll_kbytesfree);
116
117 static int ll_kbytesavail_seq_show(struct seq_file *m, void *v)
118 {
119         struct super_block *sb = m->private;
120         struct obd_statfs osfs;
121         int rc;
122
123         LASSERT(sb != NULL);
124         rc = ll_statfs_internal(sb, &osfs,
125                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
126                                 OBD_STATFS_NODELAY);
127         if (!rc) {
128                 __u32 blk_size = osfs.os_bsize >> 10;
129                 __u64 result = osfs.os_bavail;
130
131                 while (blk_size >>= 1)
132                         result <<= 1;
133
134                 rc = seq_printf(m, LPU64"\n", result);
135         }
136         return rc;
137 }
138 LPROC_SEQ_FOPS_RO(ll_kbytesavail);
139
140 static int ll_filestotal_seq_show(struct seq_file *m, void *v)
141 {
142         struct super_block *sb = m->private;
143         struct obd_statfs osfs;
144         int rc;
145
146         LASSERT(sb != NULL);
147         rc = ll_statfs_internal(sb, &osfs,
148                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
149                                 OBD_STATFS_NODELAY);
150         if (!rc)
151                 rc = seq_printf(m, LPU64"\n", osfs.os_files);
152         return rc;
153 }
154 LPROC_SEQ_FOPS_RO(ll_filestotal);
155
156 static int ll_filesfree_seq_show(struct seq_file *m, void *v)
157 {
158         struct super_block *sb = m->private;
159         struct obd_statfs osfs;
160         int rc;
161
162         LASSERT(sb != NULL);
163         rc = ll_statfs_internal(sb, &osfs,
164                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
165                                 OBD_STATFS_NODELAY);
166         if (!rc)
167                 rc = seq_printf(m, LPU64"\n", osfs.os_ffree);
168         return rc;
169 }
170 LPROC_SEQ_FOPS_RO(ll_filesfree);
171
172 static int ll_client_type_seq_show(struct seq_file *m, void *v)
173 {
174         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
175         int rc;
176
177         LASSERT(sbi != NULL);
178
179         if (sbi->ll_flags & LL_SBI_RMT_CLIENT)
180                 rc = seq_printf(m, "remote client\n");
181         else
182                 rc = seq_printf(m, "local client\n");
183         return rc;
184 }
185 LPROC_SEQ_FOPS_RO(ll_client_type);
186
187 static int ll_fstype_seq_show(struct seq_file *m, void *v)
188 {
189         struct super_block *sb = m->private;
190
191         LASSERT(sb != NULL);
192         return seq_printf(m, "%s\n", sb->s_type->name);
193 }
194 LPROC_SEQ_FOPS_RO(ll_fstype);
195
196 static int ll_sb_uuid_seq_show(struct seq_file *m, void *v)
197 {
198         struct super_block *sb = m->private;
199
200         LASSERT(sb != NULL);
201         return seq_printf(m, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
202 }
203 LPROC_SEQ_FOPS_RO(ll_sb_uuid);
204
205 static int ll_xattr_cache_seq_show(struct seq_file *m, void *v)
206 {
207         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
208
209         return seq_printf(m, "%u\n", sbi->ll_xattr_cache_enabled);
210 }
211
212 static ssize_t ll_xattr_cache_seq_write(struct file *file, const char *buffer,
213                                         size_t count, loff_t *off)
214 {
215         struct seq_file *m = file->private_data;
216         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
217         int val, rc;
218
219         rc = lprocfs_write_helper(buffer, count, &val);
220         if (rc)
221                 return rc;
222
223         if (val != 0 && val != 1)
224                 return -ERANGE;
225
226         if (val == 1 && !(sbi->ll_flags & LL_SBI_XATTR_CACHE))
227                 return -ENOTSUPP;
228
229         sbi->ll_xattr_cache_enabled = val;
230
231         return count;
232 }
233 LPROC_SEQ_FOPS(ll_xattr_cache);
234
235 static int ll_site_stats_seq_show(struct seq_file *m, void *v)
236 {
237         struct super_block *sb = m->private;
238
239         /*
240          * See description of statistical counters in struct cl_site, and
241          * struct lu_site.
242          */
243         return cl_site_stats_print(lu2cl_site(ll_s2sbi(sb)->ll_site), m);
244 }
245 LPROC_SEQ_FOPS_RO(ll_site_stats);
246
247 static int ll_max_readahead_mb_seq_show(struct seq_file *m, void *v)
248 {
249         struct super_block *sb = m->private;
250         struct ll_sb_info *sbi = ll_s2sbi(sb);
251         long pages_number;
252         int mult;
253
254         spin_lock(&sbi->ll_lock);
255         pages_number = sbi->ll_ra_info.ra_max_pages;
256         spin_unlock(&sbi->ll_lock);
257
258         mult = 1 << (20 - PAGE_CACHE_SHIFT);
259         return lprocfs_seq_read_frac_helper(m, pages_number, mult);
260 }
261
262 static ssize_t
263 ll_max_readahead_mb_seq_write(struct file *file, const char *buffer,
264                               size_t count, loff_t *off)
265 {
266         struct seq_file *m = file->private_data;
267         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
268         int mult, rc, pages_number;
269
270         mult = 1 << (20 - PAGE_CACHE_SHIFT);
271         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
272         if (rc)
273                 return rc;
274
275         if (pages_number < 0 || pages_number > totalram_pages / 2) {
276                 /* 1/2 of RAM */
277                 CERROR("can't set file readahead more than %lu MB\n",
278                        totalram_pages >> (20 - PAGE_CACHE_SHIFT + 1));
279                 return -ERANGE;
280         }
281
282         spin_lock(&sbi->ll_lock);
283         sbi->ll_ra_info.ra_max_pages = pages_number;
284         spin_unlock(&sbi->ll_lock);
285         return count;
286 }
287 LPROC_SEQ_FOPS(ll_max_readahead_mb);
288
289 static int ll_max_readahead_per_file_mb_seq_show(struct seq_file *m, void *v)
290 {
291         struct super_block *sb = m->private;
292         struct ll_sb_info *sbi = ll_s2sbi(sb);
293         long pages_number;
294         int mult;
295
296         spin_lock(&sbi->ll_lock);
297         pages_number = sbi->ll_ra_info.ra_max_pages_per_file;
298         spin_unlock(&sbi->ll_lock);
299
300         mult = 1 << (20 - PAGE_CACHE_SHIFT);
301         return lprocfs_seq_read_frac_helper(m, pages_number, mult);
302 }
303
304 static ssize_t
305 ll_max_readahead_per_file_mb_seq_write(struct file *file, const char *buffer,
306                                        size_t count, loff_t *off)
307 {
308         struct seq_file *m = file->private_data;
309         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
310         int mult, rc, pages_number;
311
312         mult = 1 << (20 - PAGE_CACHE_SHIFT);
313         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
314         if (rc)
315                 return rc;
316
317         if (pages_number < 0 ||
318             pages_number > sbi->ll_ra_info.ra_max_pages) {
319                 CERROR("can't set file readahead more than"
320                        "max_read_ahead_mb %lu MB\n",
321                        sbi->ll_ra_info.ra_max_pages);
322                 return -ERANGE;
323         }
324
325         spin_lock(&sbi->ll_lock);
326         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
327         spin_unlock(&sbi->ll_lock);
328         return count;
329 }
330 LPROC_SEQ_FOPS(ll_max_readahead_per_file_mb);
331
332 static int ll_max_read_ahead_whole_mb_seq_show(struct seq_file *m, void *v)
333 {
334         struct super_block *sb = m->private;
335         struct ll_sb_info *sbi = ll_s2sbi(sb);
336         long pages_number;
337         int mult;
338
339         spin_lock(&sbi->ll_lock);
340         pages_number = sbi->ll_ra_info.ra_max_read_ahead_whole_pages;
341         spin_unlock(&sbi->ll_lock);
342
343         mult = 1 << (20 - PAGE_CACHE_SHIFT);
344         return lprocfs_seq_read_frac_helper(m, pages_number, mult);
345 }
346
347 static ssize_t
348 ll_max_read_ahead_whole_mb_seq_write(struct file *file, const char *buffer,
349                                      size_t count, loff_t *off)
350 {
351         struct seq_file *m = file->private_data;
352         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
353         int mult, rc, pages_number;
354
355         mult = 1 << (20 - PAGE_CACHE_SHIFT);
356         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
357         if (rc)
358                 return rc;
359
360         /* Cap this at the current max readahead window size, the readahead
361          * algorithm does this anyway so it's pointless to set it larger. */
362         if (pages_number < 0 ||
363             pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
364                 CERROR("can't set max_read_ahead_whole_mb more than "
365                        "max_read_ahead_per_file_mb: %lu\n",
366                         sbi->ll_ra_info.ra_max_pages_per_file >>
367                         (20 - PAGE_CACHE_SHIFT));
368                 return -ERANGE;
369         }
370
371         spin_lock(&sbi->ll_lock);
372         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
373         spin_unlock(&sbi->ll_lock);
374         return count;
375 }
376 LPROC_SEQ_FOPS(ll_max_read_ahead_whole_mb);
377
378 static int ll_max_cached_mb_seq_show(struct seq_file *m, void *v)
379 {
380         struct super_block     *sb    = m->private;
381         struct ll_sb_info      *sbi   = ll_s2sbi(sb);
382         struct cl_client_cache *cache = &sbi->ll_cache;
383         int shift = 20 - PAGE_CACHE_SHIFT;
384         int max_cached_mb;
385         int unused_mb;
386
387         max_cached_mb = cache->ccc_lru_max >> shift;
388         unused_mb = cfs_atomic_read(&cache->ccc_lru_left) >> shift;
389         return seq_printf(m,
390                         "users: %d\n"
391                         "max_cached_mb: %d\n"
392                         "used_mb: %d\n"
393                         "unused_mb: %d\n"
394                         "reclaim_count: %u\n",
395                         cfs_atomic_read(&cache->ccc_users),
396                         max_cached_mb,
397                         max_cached_mb - unused_mb,
398                         unused_mb,
399                         cache->ccc_lru_shrinkers);
400 }
401
402 static ssize_t
403 ll_max_cached_mb_seq_write(struct file *file, const char *buffer,
404                            size_t count, loff_t *off)
405 {
406         struct seq_file *m = file->private_data;
407         struct super_block *sb = m->private;
408         struct ll_sb_info *sbi = ll_s2sbi(sb);
409         struct cl_client_cache *cache = &sbi->ll_cache;
410         struct lu_env *env;
411         int refcheck;
412         int mult, rc, pages_number;
413         int diff = 0;
414         int nrpages = 0;
415         ENTRY;
416
417         mult = 1 << (20 - PAGE_CACHE_SHIFT);
418         buffer = lprocfs_find_named_value(buffer, "max_cached_mb:", &count);
419         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
420         if (rc)
421                 RETURN(rc);
422
423         if (pages_number < 0 || pages_number > totalram_pages) {
424                 CERROR("%s: can't set max cache more than %lu MB\n",
425                        ll_get_fsname(sb, NULL, 0),
426                        totalram_pages >> (20 - PAGE_CACHE_SHIFT));
427                 RETURN(-ERANGE);
428         }
429
430         if (sbi->ll_dt_exp == NULL) /* being initialized */
431                 GOTO(out, rc = 0);
432
433         spin_lock(&sbi->ll_lock);
434         diff = pages_number - cache->ccc_lru_max;
435         spin_unlock(&sbi->ll_lock);
436
437         /* easy - add more LRU slots. */
438         if (diff >= 0) {
439                 cfs_atomic_add(diff, &cache->ccc_lru_left);
440                 GOTO(out, rc = 0);
441         }
442
443         env = cl_env_get(&refcheck);
444         if (IS_ERR(env))
445                 RETURN(rc);
446
447         diff = -diff;
448         while (diff > 0) {
449                 int tmp;
450
451                 /* reduce LRU budget from free slots. */
452                 do {
453                         int ov, nv;
454
455                         ov = cfs_atomic_read(&cache->ccc_lru_left);
456                         if (ov == 0)
457                                 break;
458
459                         nv = ov > diff ? ov - diff : 0;
460                         rc = cfs_atomic_cmpxchg(&cache->ccc_lru_left, ov, nv);
461                         if (likely(ov == rc)) {
462                                 diff -= ov - nv;
463                                 nrpages += ov - nv;
464                                 break;
465                         }
466                 } while (1);
467
468                 if (diff <= 0)
469                         break;
470
471                 /* difficult - have to ask OSCs to drop LRU slots. */
472                 tmp = diff << 1;
473                 rc = obd_set_info_async(env, sbi->ll_dt_exp,
474                                 sizeof(KEY_CACHE_LRU_SHRINK),
475                                 KEY_CACHE_LRU_SHRINK,
476                                 sizeof(tmp), &tmp, NULL);
477                 if (rc < 0)
478                         break;
479         }
480         cl_env_put(env, &refcheck);
481
482 out:
483         if (rc >= 0) {
484                 spin_lock(&sbi->ll_lock);
485                 cache->ccc_lru_max = pages_number;
486                 spin_unlock(&sbi->ll_lock);
487                 rc = count;
488         } else {
489                 cfs_atomic_add(nrpages, &cache->ccc_lru_left);
490         }
491         return rc;
492 }
493 LPROC_SEQ_FOPS(ll_max_cached_mb);
494
495 static int ll_checksum_seq_show(struct seq_file *m, void *v)
496 {
497         struct super_block *sb = m->private;
498         struct ll_sb_info *sbi = ll_s2sbi(sb);
499
500         return seq_printf(m, "%u\n", (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
501 }
502
503 static ssize_t ll_checksum_seq_write(struct file *file, const char *buffer,
504                                      size_t count, loff_t *off)
505 {
506         struct seq_file *m = file->private_data;
507         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
508         int val, rc;
509
510         if (!sbi->ll_dt_exp)
511                 /* Not set up yet */
512                 return -EAGAIN;
513
514         rc = lprocfs_write_helper(buffer, count, &val);
515         if (rc)
516                 return rc;
517         if (val)
518                 sbi->ll_flags |= LL_SBI_CHECKSUM;
519         else
520                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
521
522         rc = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
523                                 KEY_CHECKSUM, sizeof(val), &val, NULL);
524         if (rc)
525                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
526
527         return count;
528 }
529 LPROC_SEQ_FOPS(ll_checksum);
530
531 static int ll_max_rw_chunk_seq_show(struct seq_file *m, void *v)
532 {
533         struct super_block *sb = m->private;
534
535         return seq_printf(m, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
536 }
537
538 static ssize_t ll_max_rw_chunk_seq_write(struct file *file, const char *buffer,
539                                          size_t count, loff_t *off)
540 {
541         struct seq_file *m = file->private_data;
542         struct super_block *sb = m->private;
543         int rc, val;
544
545         rc = lprocfs_write_helper(buffer, count, &val);
546         if (rc)
547                 return rc;
548         ll_s2sbi(sb)->ll_max_rw_chunk = val;
549         return count;
550 }
551 LPROC_SEQ_FOPS(ll_max_rw_chunk);
552
553 static int ll_rd_track_id(struct seq_file *m, enum stats_track_type type)
554 {
555         struct super_block *sb = m->private;
556
557         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
558                 return seq_printf(m, "%d\n",
559                                   ll_s2sbi(sb)->ll_stats_track_id);
560         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
561                 return seq_printf(m, "0 (all)\n");
562         } else {
563                 return seq_printf(m, "untracked\n");
564         }
565 }
566
567 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
568                           enum stats_track_type type)
569 {
570         struct super_block *sb = data;
571         int rc, pid;
572
573         rc = lprocfs_write_helper(buffer, count, &pid);
574         if (rc)
575                 return rc;
576         ll_s2sbi(sb)->ll_stats_track_id = pid;
577         if (pid == 0)
578                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
579         else
580                 ll_s2sbi(sb)->ll_stats_track_type = type;
581         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
582         return count;
583 }
584
585 static int ll_track_pid_seq_show(struct seq_file *m, void *v)
586 {
587         return ll_rd_track_id(m, STATS_TRACK_PID);
588 }
589
590 static ssize_t ll_track_pid_seq_write(struct file *file, const char *buffer,
591                                       size_t count, loff_t *off)
592 {
593         struct seq_file *seq = file->private_data;
594         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_PID);
595 }
596 LPROC_SEQ_FOPS(ll_track_pid);
597
598 static int ll_track_ppid_seq_show(struct seq_file *m, void *v)
599 {
600         return ll_rd_track_id(m, STATS_TRACK_PPID);
601 }
602
603 static ssize_t ll_track_ppid_seq_write(struct file *file, const char *buffer,
604                                        size_t count, loff_t *off)
605 {
606         struct seq_file *seq = file->private_data;
607         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_PPID);
608 }
609 LPROC_SEQ_FOPS(ll_track_ppid);
610
611 static int ll_track_gid_seq_show(struct seq_file *m, void *v)
612 {
613         return ll_rd_track_id(m, STATS_TRACK_GID);
614 }
615
616 static ssize_t ll_track_gid_seq_write(struct file *file, const char *buffer,
617                                       size_t count, loff_t *off)
618 {
619         struct seq_file *seq = file->private_data;
620         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_GID);
621 }
622 LPROC_SEQ_FOPS(ll_track_gid);
623
624 static int ll_statahead_max_seq_show(struct seq_file *m, void *v)
625 {
626         struct super_block *sb = m->private;
627         struct ll_sb_info *sbi = ll_s2sbi(sb);
628
629         return seq_printf(m, "%u\n", sbi->ll_sa_max);
630 }
631
632 static ssize_t ll_statahead_max_seq_write(struct file *file, const char *buffer,
633                                           size_t count, loff_t *off)
634 {
635         struct seq_file *m = file->private_data;
636         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
637         int val, rc;
638
639         rc = lprocfs_write_helper(buffer, count, &val);
640         if (rc)
641                 return rc;
642
643         if (val >= 0 && val <= LL_SA_RPC_MAX)
644                 sbi->ll_sa_max = val;
645         else
646                 CERROR("Bad statahead_max value %d. Valid values are in the "
647                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
648
649         return count;
650 }
651 LPROC_SEQ_FOPS(ll_statahead_max);
652
653 static int ll_statahead_agl_seq_show(struct seq_file *m, void *v)
654 {
655         struct super_block *sb = m->private;
656         struct ll_sb_info *sbi = ll_s2sbi(sb);
657
658         return seq_printf(m, "%u\n",
659                           sbi->ll_flags & LL_SBI_AGL_ENABLED ? 1 : 0);
660 }
661
662 static ssize_t ll_statahead_agl_seq_write(struct file *file, const char *buffer,
663                                           size_t count, loff_t *off)
664 {
665         struct seq_file *m = file->private_data;
666         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
667         int val, rc;
668
669         rc = lprocfs_write_helper(buffer, count, &val);
670         if (rc)
671                 return rc;
672
673         if (val)
674                 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
675         else
676                 sbi->ll_flags &= ~LL_SBI_AGL_ENABLED;
677
678         return count;
679 }
680 LPROC_SEQ_FOPS(ll_statahead_agl);
681
682 static int ll_statahead_stats_seq_show(struct seq_file *m, void *v)
683 {
684         struct super_block *sb = m->private;
685         struct ll_sb_info *sbi = ll_s2sbi(sb);
686
687         return seq_printf(m,
688                         "statahead total: %u\n"
689                         "statahead wrong: %u\n"
690                         "agl total: %u\n",
691                         atomic_read(&sbi->ll_sa_total),
692                         atomic_read(&sbi->ll_sa_wrong),
693                         atomic_read(&sbi->ll_agl_total));
694 }
695 LPROC_SEQ_FOPS_RO(ll_statahead_stats);
696
697 static int ll_lazystatfs_seq_show(struct seq_file *m, void *v)
698 {
699         struct super_block *sb = m->private;
700         struct ll_sb_info *sbi = ll_s2sbi(sb);
701
702         return seq_printf(m, "%u\n",
703                           (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
704 }
705
706 static ssize_t ll_lazystatfs_seq_write(struct file *file, const char *buffer,
707                                         size_t count, loff_t *off)
708 {
709         struct seq_file *m = file->private_data;
710         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
711         int val, rc;
712
713         rc = lprocfs_write_helper(buffer, count, &val);
714         if (rc)
715                 return rc;
716
717         if (val)
718                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
719         else
720                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
721
722         return count;
723 }
724 LPROC_SEQ_FOPS(ll_lazystatfs);
725
726 static int ll_maxea_size_seq_show(struct seq_file *m, void *v)
727 {
728         struct super_block *sb = m->private;
729         struct ll_sb_info *sbi = ll_s2sbi(sb);
730         unsigned int ealen;
731         int rc;
732
733         rc = ll_get_max_mdsize(sbi, &ealen);
734         if (rc)
735                 return rc;
736
737         return seq_printf(m, "%u\n", ealen);
738 }
739 LPROC_SEQ_FOPS_RO(ll_maxea_size);
740
741 static int ll_sbi_flags_seq_show(struct seq_file *m, void *v)
742 {
743         const char *str[] = LL_SBI_FLAGS;
744         struct super_block *sb = m->private;
745         int flags = ll_s2sbi(sb)->ll_flags;
746         int i = 0;
747
748         while (flags != 0) {
749                 if (ARRAY_SIZE(str) <= i) {
750                         CERROR("%s: Revise array LL_SBI_FLAGS to match sbi "
751                                 "flags please.\n", ll_get_fsname(sb, NULL, 0));
752                         return -EINVAL;
753                 }
754
755                 if (flags & 0x1)
756                         seq_printf(m, "%s ", str[i]);
757                 flags >>= 1;
758                 ++i;
759         }
760         seq_printf(m, "\b\n");
761         return 0;
762 }
763 LPROC_SEQ_FOPS_RO(ll_sbi_flags);
764
765 static int ll_unstable_stats_seq_show(struct seq_file *m, void *v)
766 {
767         struct super_block      *sb    = m->private;
768         struct ll_sb_info       *sbi   = ll_s2sbi(sb);
769         struct cl_client_cache  *cache = &sbi->ll_cache;
770         int pages, mb;
771
772         pages = cfs_atomic_read(&cache->ccc_unstable_nr);
773         mb    = (pages * PAGE_CACHE_SIZE) >> 20;
774
775         return seq_printf(m, "unstable_pages: %8d\n"
776                                 "unstable_mb:    %8d\n", pages, mb);
777 }
778 LPROC_SEQ_FOPS_RO(ll_unstable_stats);
779
780 struct lprocfs_seq_vars lprocfs_llite_obd_vars[] = {
781         { .name =       "uuid",
782           .fops =       &ll_sb_uuid_fops                        },
783         { .name =       "fstype",
784           .fops =       &ll_fstype_fops                         },
785         { .name =       "site",
786           .fops =       &ll_site_stats_fops                     },
787         { .name =       "blocksize",
788           .fops =       &ll_blksize_fops                        },
789         { .name =       "kbytestotal",
790           .fops =       &ll_kbytestotal_fops                    },
791         { .name =       "kbytesfree",
792           .fops =       &ll_kbytesfree_fops                     },
793         { .name =       "kbytesavail",
794           .fops =       &ll_kbytesavail_fops                    },
795         { .name =       "filestotal",
796           .fops =       &ll_filestotal_fops                     },
797         { .name =       "filesfree",
798           .fops =       &ll_filesfree_fops                      },
799         { .name =       "client_type",
800           .fops =       &ll_client_type_fops                    },
801         { .name =       "max_read_ahead_mb",
802           .fops =       &ll_max_readahead_mb_fops               },
803         { .name =       "max_read_ahead_per_file_mb",
804           .fops =       &ll_max_readahead_per_file_mb_fops      },
805         { .name =       "max_read_ahead_whole_mb",
806           .fops =       &ll_max_read_ahead_whole_mb_fops        },
807         { .name =       "max_cached_mb",
808           .fops =       &ll_max_cached_mb_fops                  },
809         { .name =       "checksum_pages",
810           .fops =       &ll_checksum_fops                       },
811         { .name =       "max_rw_chunk",
812           .fops =       &ll_max_rw_chunk_fops                   },
813         { .name =       "stats_track_pid",
814           .fops =       &ll_track_pid_fops                      },
815         { .name =       "stats_track_ppid",
816           .fops =       &ll_track_ppid_fops                     },
817         { .name =       "stats_track_gid",
818           .fops =       &ll_track_gid_fops                      },
819         { .name =       "statahead_max",
820           .fops =       &ll_statahead_max_fops                  },
821         { .name =       "statahead_agl",
822           .fops =       &ll_statahead_agl_fops                  },
823         { .name =       "statahead_stats",
824           .fops =       &ll_statahead_stats_fops                },
825         { .name =       "lazystatfs",
826           .fops =       &ll_lazystatfs_fops                     },
827         { .name =       "max_easize",
828           .fops =       &ll_maxea_size_fops                     },
829         { .name =       "sbi_flags",
830           .fops =       &ll_sbi_flags_fops                      },
831         { .name =       "xattr_cache",
832           .fops =       &ll_xattr_cache_fops                    },
833         { .name =       "unstable_stats",
834           .fops =       &ll_unstable_stats_fops                 },
835         { 0 }
836 };
837
838 #define MAX_STRING_SIZE 128
839
840 struct llite_file_opcode {
841         __u32       opcode;
842         __u32       type;
843         const char *opname;
844 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
845         /* file operation */
846         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
847         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
848         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
849                                    "read_bytes" },
850         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
851                                    "write_bytes" },
852         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
853                                    "brw_read" },
854         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
855                                    "brw_write" },
856         { LPROC_LL_OSC_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
857                                    "osc_read" },
858         { LPROC_LL_OSC_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
859                                    "osc_write" },
860         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
861         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
862         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
863         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
864         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
865         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
866         { LPROC_LL_READDIR,        LPROCFS_TYPE_REGS, "readdir" },
867         /* inode operation */
868         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
869         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
870         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
871         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
872         /* dir inode operation */
873         { LPROC_LL_CREATE,         LPROCFS_TYPE_REGS, "create" },
874         { LPROC_LL_LINK,           LPROCFS_TYPE_REGS, "link" },
875         { LPROC_LL_UNLINK,         LPROCFS_TYPE_REGS, "unlink" },
876         { LPROC_LL_SYMLINK,        LPROCFS_TYPE_REGS, "symlink" },
877         { LPROC_LL_MKDIR,          LPROCFS_TYPE_REGS, "mkdir" },
878         { LPROC_LL_RMDIR,          LPROCFS_TYPE_REGS, "rmdir" },
879         { LPROC_LL_MKNOD,          LPROCFS_TYPE_REGS, "mknod" },
880         { LPROC_LL_RENAME,         LPROCFS_TYPE_REGS, "rename" },
881         /* special inode operation */
882         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
883         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
884         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
885         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
886         { LPROC_LL_GETXATTR_HITS,  LPROCFS_TYPE_REGS, "getxattr_hits" },
887         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
888         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
889         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
890 };
891
892 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
893 {
894         if (!sbi->ll_stats)
895                 return;
896         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
897                 lprocfs_counter_add(sbi->ll_stats, op, count);
898         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
899                  sbi->ll_stats_track_id == current->pid)
900                 lprocfs_counter_add(sbi->ll_stats, op, count);
901         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
902                  sbi->ll_stats_track_id == current->parent->pid)
903                 lprocfs_counter_add(sbi->ll_stats, op, count);
904         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
905                  sbi->ll_stats_track_id == current_gid())
906                 lprocfs_counter_add(sbi->ll_stats, op, count);
907 }
908 EXPORT_SYMBOL(ll_stats_ops_tally);
909
910 static const char *ra_stat_string[] = {
911         [RA_STAT_HIT] = "hits",
912         [RA_STAT_MISS] = "misses",
913         [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
914         [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
915         [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
916         [RA_STAT_FAILED_MATCH] = "failed lock match",
917         [RA_STAT_DISCARDED] = "read but discarded",
918         [RA_STAT_ZERO_LEN] = "zero length file",
919         [RA_STAT_ZERO_WINDOW] = "zero size window",
920         [RA_STAT_EOF] = "read-ahead to EOF",
921         [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
922         [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
923         [RA_STAT_FAILED_REACH_END] = "failed to reach end"
924 };
925
926 LPROC_SEQ_FOPS_RO_TYPE(llite, name);
927 LPROC_SEQ_FOPS_RO_TYPE(llite, uuid);
928
929 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
930                                 struct super_block *sb, char *osc, char *mdc)
931 {
932         struct lprocfs_seq_vars lvars[2];
933         struct lustre_sb_info *lsi = s2lsi(sb);
934         struct ll_sb_info *sbi = ll_s2sbi(sb);
935         struct obd_device *obd;
936         struct proc_dir_entry *dir;
937         char name[MAX_STRING_SIZE + 1], *ptr;
938         int err, id, len, rc;
939         ENTRY;
940
941         memset(lvars, 0, sizeof(lvars));
942
943         name[MAX_STRING_SIZE] = '\0';
944         lvars[0].name = name;
945
946         LASSERT(sbi != NULL);
947         LASSERT(mdc != NULL);
948         LASSERT(osc != NULL);
949
950         /* Get fsname */
951         len = strlen(lsi->lsi_lmd->lmd_profile);
952         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
953         if (ptr && (strcmp(ptr, "-client") == 0))
954                 len -= 7;
955
956         /* Mount info */
957         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
958                  lsi->lsi_lmd->lmd_profile, sb);
959
960         sbi->ll_proc_root = lprocfs_seq_register(name, parent, NULL, NULL);
961         if (IS_ERR(sbi->ll_proc_root)) {
962                 err = PTR_ERR(sbi->ll_proc_root);
963                 sbi->ll_proc_root = NULL;
964                 RETURN(err);
965         }
966
967         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
968                                 &vvp_dump_pgcache_file_ops, sbi);
969         if (rc)
970                 CWARN("Error adding the dump_page_cache file\n");
971
972         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
973                                 &ll_rw_extents_stats_fops, sbi);
974         if (rc)
975                 CWARN("Error adding the extent_stats file\n");
976
977         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
978                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
979         if (rc)
980                 CWARN("Error adding the extents_stats_per_process file\n");
981
982         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
983                                 &ll_rw_offset_stats_fops, sbi);
984         if (rc)
985                 CWARN("Error adding the offset_stats file\n");
986
987         /* File operations stats */
988         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
989                                             LPROCFS_STATS_FLAG_NONE);
990         if (sbi->ll_stats == NULL)
991                 GOTO(out, err = -ENOMEM);
992         /* do counter init */
993         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
994                 __u32 type = llite_opcode_table[id].type;
995                 void *ptr = NULL;
996                 if (type & LPROCFS_TYPE_REGS)
997                         ptr = "regs";
998                 else if (type & LPROCFS_TYPE_BYTES)
999                         ptr = "bytes";
1000                 else if (type & LPROCFS_TYPE_PAGES)
1001                         ptr = "pages";
1002                 lprocfs_counter_init(sbi->ll_stats,
1003                                      llite_opcode_table[id].opcode,
1004                                      (type & LPROCFS_CNTR_AVGMINMAX),
1005                                      llite_opcode_table[id].opname, ptr);
1006         }
1007         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
1008         if (err)
1009                 GOTO(out, err);
1010
1011         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
1012                                                LPROCFS_STATS_FLAG_NONE);
1013         if (sbi->ll_ra_stats == NULL)
1014                 GOTO(out, err = -ENOMEM);
1015
1016         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
1017                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
1018                                      ra_stat_string[id], "pages");
1019         err = lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
1020                                      sbi->ll_ra_stats);
1021         if (err)
1022                 GOTO(out, err);
1023
1024
1025         err = lprocfs_seq_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
1026         if (err)
1027                 GOTO(out, err);
1028
1029         /* MDC info */
1030         obd = class_name2obd(mdc);
1031
1032         LASSERT(obd != NULL);
1033         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1034         LASSERT(obd->obd_type->typ_name != NULL);
1035
1036         dir = proc_mkdir(obd->obd_type->typ_name, sbi->ll_proc_root);
1037         if (dir == NULL)
1038                 GOTO(out, err = -ENOMEM);
1039
1040         snprintf(name, MAX_STRING_SIZE, "common_name");
1041         lvars[0].fops = &llite_name_fops;
1042         err = lprocfs_seq_add_vars(dir, lvars, obd);
1043         if (err)
1044                 GOTO(out, err);
1045
1046         snprintf(name, MAX_STRING_SIZE, "uuid");
1047         lvars[0].fops = &llite_uuid_fops;
1048         err = lprocfs_seq_add_vars(dir, lvars, obd);
1049         if (err)
1050                 GOTO(out, err);
1051
1052         /* OSC */
1053         obd = class_name2obd(osc);
1054
1055         LASSERT(obd != NULL);
1056         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1057         LASSERT(obd->obd_type->typ_name != NULL);
1058
1059         dir = proc_mkdir(obd->obd_type->typ_name, sbi->ll_proc_root);
1060         if (dir == NULL)
1061                 GOTO(out, err = -ENOMEM);
1062
1063         snprintf(name, MAX_STRING_SIZE, "common_name");
1064         lvars[0].fops = &llite_name_fops;
1065         err = lprocfs_seq_add_vars(dir, lvars, obd);
1066         if (err)
1067                 GOTO(out, err);
1068
1069         snprintf(name, MAX_STRING_SIZE, "uuid");
1070         lvars[0].fops = &llite_uuid_fops;
1071         err = lprocfs_seq_add_vars(dir, lvars, obd);
1072 out:
1073         if (err) {
1074                 lprocfs_remove(&sbi->ll_proc_root);
1075                 lprocfs_free_stats(&sbi->ll_ra_stats);
1076                 lprocfs_free_stats(&sbi->ll_stats);
1077         }
1078         RETURN(err);
1079 }
1080
1081 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
1082 {
1083         if (sbi->ll_proc_root) {
1084                 lprocfs_remove(&sbi->ll_proc_root);
1085                 lprocfs_free_stats(&sbi->ll_ra_stats);
1086                 lprocfs_free_stats(&sbi->ll_stats);
1087         }
1088 }
1089 #undef MAX_STRING_SIZE
1090
1091 #define pct(a,b) (b ? a * 100 / b : 0)
1092
1093 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1094                                    struct seq_file *seq, int which)
1095 {
1096         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1097         unsigned long start, end, r, w;
1098         char *unitp = "KMGTPEZY";
1099         int i, units = 10;
1100         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1101
1102         read_cum = 0;
1103         write_cum = 0;
1104         start = 0;
1105
1106         for(i = 0; i < LL_HIST_MAX; i++) {
1107                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1108                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1109         }
1110
1111         for(i = 0; i < LL_HIST_MAX; i++) {
1112                 r = pp_info->pp_r_hist.oh_buckets[i];
1113                 w = pp_info->pp_w_hist.oh_buckets[i];
1114                 read_cum += r;
1115                 write_cum += w;
1116                 end = 1 << (i + LL_HIST_START - units);
1117                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1118                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1119                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1120                            r, pct(r, read_tot), pct(read_cum, read_tot),
1121                            w, pct(w, write_tot), pct(write_cum, write_tot));
1122                 start = end;
1123                 if (start == 1<<10) {
1124                         start = 1;
1125                         units += 10;
1126                         unitp++;
1127                 }
1128                 if (read_cum == read_tot && write_cum == write_tot)
1129                         break;
1130         }
1131 }
1132
1133 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1134 {
1135         struct timeval now;
1136         struct ll_sb_info *sbi = seq->private;
1137         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1138         int k;
1139
1140         do_gettimeofday(&now);
1141
1142         if (!sbi->ll_rw_stats_on) {
1143                 seq_printf(seq, "disabled\n"
1144                                 "write anything in this file to activate, "
1145                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1146                 return 0;
1147         }
1148         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1149                    now.tv_sec, now.tv_usec);
1150         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1151         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1152                    "extents", "calls", "%", "cum%",
1153                    "calls", "%", "cum%");
1154         spin_lock(&sbi->ll_pp_extent_lock);
1155         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1156                 if (io_extents->pp_extents[k].pid != 0) {
1157                         seq_printf(seq, "\nPID: %d\n",
1158                                    io_extents->pp_extents[k].pid);
1159                         ll_display_extents_info(io_extents, seq, k);
1160                 }
1161         }
1162         spin_unlock(&sbi->ll_pp_extent_lock);
1163         return 0;
1164 }
1165
1166 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1167                                                 const char *buf, size_t len,
1168                                                 loff_t *off)
1169 {
1170         struct seq_file *seq = file->private_data;
1171         struct ll_sb_info *sbi = seq->private;
1172         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1173         int i;
1174         int value = 1, rc = 0;
1175
1176         rc = lprocfs_write_helper(buf, len, &value);
1177         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1178                        strcmp(buf, "Disabled") == 0))
1179                 value = 0;
1180
1181         if (value == 0)
1182                 sbi->ll_rw_stats_on = 0;
1183         else
1184                 sbi->ll_rw_stats_on = 1;
1185
1186         spin_lock(&sbi->ll_pp_extent_lock);
1187         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1188                 io_extents->pp_extents[i].pid = 0;
1189                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1190                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1191         }
1192         spin_unlock(&sbi->ll_pp_extent_lock);
1193         return len;
1194 }
1195
1196 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1197
1198 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1199 {
1200         struct timeval now;
1201         struct ll_sb_info *sbi = seq->private;
1202         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1203
1204         do_gettimeofday(&now);
1205
1206         if (!sbi->ll_rw_stats_on) {
1207                 seq_printf(seq, "disabled\n"
1208                                 "write anything in this file to activate, "
1209                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1210                 return 0;
1211         }
1212         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1213                    now.tv_sec, now.tv_usec);
1214
1215         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1216         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1217                    "extents", "calls", "%", "cum%",
1218                    "calls", "%", "cum%");
1219         spin_lock(&sbi->ll_lock);
1220         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1221         spin_unlock(&sbi->ll_lock);
1222
1223         return 0;
1224 }
1225
1226 static ssize_t ll_rw_extents_stats_seq_write(struct file *file, const char *buf,
1227                                         size_t len, loff_t *off)
1228 {
1229         struct seq_file *seq = file->private_data;
1230         struct ll_sb_info *sbi = seq->private;
1231         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1232         int i;
1233         int value = 1, rc = 0;
1234
1235         rc = lprocfs_write_helper(buf, len, &value);
1236         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1237                        strcmp(buf, "Disabled") == 0))
1238                 value = 0;
1239
1240         if (value == 0)
1241                 sbi->ll_rw_stats_on = 0;
1242         else
1243                 sbi->ll_rw_stats_on = 1;
1244         spin_lock(&sbi->ll_pp_extent_lock);
1245         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
1246                 io_extents->pp_extents[i].pid = 0;
1247                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1248                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1249         }
1250         spin_unlock(&sbi->ll_pp_extent_lock);
1251
1252         return len;
1253 }
1254
1255 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1256
1257 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
1258                        struct ll_file_data *file, loff_t pos,
1259                        size_t count, int rw)
1260 {
1261         int i, cur = -1;
1262         struct ll_rw_process_info *process;
1263         struct ll_rw_process_info *offset;
1264         int *off_count = &sbi->ll_rw_offset_entry_count;
1265         int *process_count = &sbi->ll_offset_process_count;
1266         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1267
1268         if(!sbi->ll_rw_stats_on)
1269                 return;
1270         process = sbi->ll_rw_process_info;
1271         offset = sbi->ll_rw_offset_info;
1272
1273         spin_lock(&sbi->ll_pp_extent_lock);
1274         /* Extent statistics */
1275         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1276                 if(io_extents->pp_extents[i].pid == pid) {
1277                         cur = i;
1278                         break;
1279                 }
1280         }
1281
1282         if (cur == -1) {
1283                 /* new process */
1284                 sbi->ll_extent_process_count =
1285                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1286                 cur = sbi->ll_extent_process_count;
1287                 io_extents->pp_extents[cur].pid = pid;
1288                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1289                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1290         }
1291
1292         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1293              (i < (LL_HIST_MAX - 1)); i++);
1294         if (rw == 0) {
1295                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1296                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1297         } else {
1298                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1299                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1300         }
1301         spin_unlock(&sbi->ll_pp_extent_lock);
1302
1303         spin_lock(&sbi->ll_process_lock);
1304         /* Offset statistics */
1305         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1306                 if (process[i].rw_pid == pid) {
1307                         if (process[i].rw_last_file != file) {
1308                                 process[i].rw_range_start = pos;
1309                                 process[i].rw_last_file_pos = pos + count;
1310                                 process[i].rw_smallest_extent = count;
1311                                 process[i].rw_largest_extent = count;
1312                                 process[i].rw_offset = 0;
1313                                 process[i].rw_last_file = file;
1314                                 spin_unlock(&sbi->ll_process_lock);
1315                                 return;
1316                         }
1317                         if (process[i].rw_last_file_pos != pos) {
1318                                 *off_count =
1319                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1320                                 offset[*off_count].rw_op = process[i].rw_op;
1321                                 offset[*off_count].rw_pid = pid;
1322                                 offset[*off_count].rw_range_start =
1323                                         process[i].rw_range_start;
1324                                 offset[*off_count].rw_range_end =
1325                                         process[i].rw_last_file_pos;
1326                                 offset[*off_count].rw_smallest_extent =
1327                                         process[i].rw_smallest_extent;
1328                                 offset[*off_count].rw_largest_extent =
1329                                         process[i].rw_largest_extent;
1330                                 offset[*off_count].rw_offset =
1331                                         process[i].rw_offset;
1332                                 process[i].rw_op = rw;
1333                                 process[i].rw_range_start = pos;
1334                                 process[i].rw_smallest_extent = count;
1335                                 process[i].rw_largest_extent = count;
1336                                 process[i].rw_offset = pos -
1337                                         process[i].rw_last_file_pos;
1338                         }
1339                         if(process[i].rw_smallest_extent > count)
1340                                 process[i].rw_smallest_extent = count;
1341                         if(process[i].rw_largest_extent < count)
1342                                 process[i].rw_largest_extent = count;
1343                         process[i].rw_last_file_pos = pos + count;
1344                         spin_unlock(&sbi->ll_process_lock);
1345                         return;
1346                 }
1347         }
1348         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1349         process[*process_count].rw_pid = pid;
1350         process[*process_count].rw_op = rw;
1351         process[*process_count].rw_range_start = pos;
1352         process[*process_count].rw_last_file_pos = pos + count;
1353         process[*process_count].rw_smallest_extent = count;
1354         process[*process_count].rw_largest_extent = count;
1355         process[*process_count].rw_offset = 0;
1356         process[*process_count].rw_last_file = file;
1357         spin_unlock(&sbi->ll_process_lock);
1358 }
1359
1360 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1361 {
1362         struct timeval now;
1363         struct ll_sb_info *sbi = seq->private;
1364         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1365         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1366         int i;
1367
1368         do_gettimeofday(&now);
1369
1370         if (!sbi->ll_rw_stats_on) {
1371                 seq_printf(seq, "disabled\n"
1372                                 "write anything in this file to activate, "
1373                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1374                 return 0;
1375         }
1376         spin_lock(&sbi->ll_process_lock);
1377
1378         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1379                    now.tv_sec, now.tv_usec);
1380         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1381                    "R/W", "PID", "RANGE START", "RANGE END",
1382                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1383
1384         /* We stored the discontiguous offsets here; print them first */
1385         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1386                 if (offset[i].rw_pid != 0)
1387                         seq_printf(seq,
1388                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1389                                    offset[i].rw_op == READ ? 'R' : 'W',
1390                                    offset[i].rw_pid,
1391                                    offset[i].rw_range_start,
1392                                    offset[i].rw_range_end,
1393                                    (unsigned long)offset[i].rw_smallest_extent,
1394                                    (unsigned long)offset[i].rw_largest_extent,
1395                                    offset[i].rw_offset);
1396         }
1397
1398         /* Then print the current offsets for each process */
1399         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1400                 if (process[i].rw_pid != 0)
1401                         seq_printf(seq,
1402                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1403                                    process[i].rw_op == READ ? 'R' : 'W',
1404                                    process[i].rw_pid,
1405                                    process[i].rw_range_start,
1406                                    process[i].rw_last_file_pos,
1407                                    (unsigned long)process[i].rw_smallest_extent,
1408                                    (unsigned long)process[i].rw_largest_extent,
1409                                    process[i].rw_offset);
1410         }
1411         spin_unlock(&sbi->ll_process_lock);
1412
1413         return 0;
1414 }
1415
1416 static ssize_t ll_rw_offset_stats_seq_write(struct file *file, const char *buf,
1417                                        size_t len, loff_t *off)
1418 {
1419         struct seq_file *seq = file->private_data;
1420         struct ll_sb_info *sbi = seq->private;
1421         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1422         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1423         int value = 1, rc = 0;
1424
1425         rc = lprocfs_write_helper(buf, len, &value);
1426
1427         if (rc < 0 && (strcmp(buf, "disabled") == 0 ||
1428                            strcmp(buf, "Disabled") == 0))
1429                 value = 0;
1430
1431         if (value == 0)
1432                 sbi->ll_rw_stats_on = 0;
1433         else
1434                 sbi->ll_rw_stats_on = 1;
1435
1436         spin_lock(&sbi->ll_process_lock);
1437         sbi->ll_offset_process_count = 0;
1438         sbi->ll_rw_offset_entry_count = 0;
1439         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1440                LL_PROCESS_HIST_MAX);
1441         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1442                LL_OFFSET_HIST_MAX);
1443         spin_unlock(&sbi->ll_process_lock);
1444
1445         return len;
1446 }
1447
1448 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1449 #endif /* LPROCFS */