Whamcloud - gitweb
LU-3676 llite: to configure max_cached_mb correctly
[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 #include "vvp_internal.h"
46
47 struct proc_dir_entry *proc_lustre_fs_root;
48
49 #ifdef LPROCFS
50 /* /proc/lustre/llite mount point registration */
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 = 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                         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 __user *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         char kernbuf[128];
416         ENTRY;
417
418         if (count >= sizeof(kernbuf))
419                 RETURN(-EINVAL);
420
421         if (copy_from_user(kernbuf, buffer, count))
422                 RETURN(-EFAULT);
423         kernbuf[count] = 0;
424
425         mult = 1 << (20 - PAGE_CACHE_SHIFT);
426         buffer += lprocfs_find_named_value(kernbuf, "max_cached_mb:", &count) -
427                   kernbuf;
428         rc = lprocfs_write_frac_helper(buffer, count, &pages_number, mult);
429         if (rc)
430                 RETURN(rc);
431
432         if (pages_number < 0 || pages_number > totalram_pages) {
433                 CERROR("%s: can't set max cache more than %lu MB\n",
434                        ll_get_fsname(sb, NULL, 0),
435                        totalram_pages >> (20 - PAGE_CACHE_SHIFT));
436                 RETURN(-ERANGE);
437         }
438
439         spin_lock(&sbi->ll_lock);
440         diff = pages_number - cache->ccc_lru_max;
441         spin_unlock(&sbi->ll_lock);
442
443         /* easy - add more LRU slots. */
444         if (diff >= 0) {
445                 atomic_add(diff, &cache->ccc_lru_left);
446                 GOTO(out, rc = 0);
447         }
448
449         env = cl_env_get(&refcheck);
450         if (IS_ERR(env))
451                 RETURN(rc);
452
453         diff = -diff;
454         while (diff > 0) {
455                 int tmp;
456
457                 /* reduce LRU budget from free slots. */
458                 do {
459                         int ov, nv;
460
461                         ov = atomic_read(&cache->ccc_lru_left);
462                         if (ov == 0)
463                                 break;
464
465                         nv = ov > diff ? ov - diff : 0;
466                         rc = atomic_cmpxchg(&cache->ccc_lru_left, ov, nv);
467                         if (likely(ov == rc)) {
468                                 diff -= ov - nv;
469                                 nrpages += ov - nv;
470                                 break;
471                         }
472                 } while (1);
473
474                 if (diff <= 0)
475                         break;
476
477                 if (sbi->ll_dt_exp == NULL) { /* being initialized */
478                         rc = -ENODEV;
479                         break;
480                 }
481
482                 /* difficult - have to ask OSCs to drop LRU slots. */
483                 tmp = diff << 1;
484                 rc = obd_set_info_async(env, sbi->ll_dt_exp,
485                                 sizeof(KEY_CACHE_LRU_SHRINK),
486                                 KEY_CACHE_LRU_SHRINK,
487                                 sizeof(tmp), &tmp, NULL);
488                 if (rc < 0)
489                         break;
490         }
491         cl_env_put(env, &refcheck);
492
493 out:
494         if (rc >= 0) {
495                 spin_lock(&sbi->ll_lock);
496                 cache->ccc_lru_max = pages_number;
497                 spin_unlock(&sbi->ll_lock);
498                 rc = count;
499         } else {
500                 atomic_add(nrpages, &cache->ccc_lru_left);
501         }
502         return rc;
503 }
504 LPROC_SEQ_FOPS(ll_max_cached_mb);
505
506 static int ll_checksum_seq_show(struct seq_file *m, void *v)
507 {
508         struct super_block *sb = m->private;
509         struct ll_sb_info *sbi = ll_s2sbi(sb);
510
511         return seq_printf(m, "%u\n", (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
512 }
513
514 static ssize_t ll_checksum_seq_write(struct file *file, const char *buffer,
515                                      size_t count, loff_t *off)
516 {
517         struct seq_file *m = file->private_data;
518         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
519         int val, rc;
520
521         if (!sbi->ll_dt_exp)
522                 /* Not set up yet */
523                 return -EAGAIN;
524
525         rc = lprocfs_write_helper(buffer, count, &val);
526         if (rc)
527                 return rc;
528         if (val)
529                 sbi->ll_flags |= LL_SBI_CHECKSUM;
530         else
531                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
532
533         rc = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
534                                 KEY_CHECKSUM, sizeof(val), &val, NULL);
535         if (rc)
536                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
537
538         return count;
539 }
540 LPROC_SEQ_FOPS(ll_checksum);
541
542 static int ll_max_rw_chunk_seq_show(struct seq_file *m, void *v)
543 {
544         struct super_block *sb = m->private;
545
546         return seq_printf(m, "%lu\n", ll_s2sbi(sb)->ll_max_rw_chunk);
547 }
548
549 static ssize_t ll_max_rw_chunk_seq_write(struct file *file, const char *buffer,
550                                          size_t count, loff_t *off)
551 {
552         struct seq_file *m = file->private_data;
553         struct super_block *sb = m->private;
554         int rc, val;
555
556         rc = lprocfs_write_helper(buffer, count, &val);
557         if (rc)
558                 return rc;
559         ll_s2sbi(sb)->ll_max_rw_chunk = val;
560         return count;
561 }
562 LPROC_SEQ_FOPS(ll_max_rw_chunk);
563
564 static int ll_rd_track_id(struct seq_file *m, enum stats_track_type type)
565 {
566         struct super_block *sb = m->private;
567
568         if (ll_s2sbi(sb)->ll_stats_track_type == type) {
569                 return seq_printf(m, "%d\n",
570                                   ll_s2sbi(sb)->ll_stats_track_id);
571         } else if (ll_s2sbi(sb)->ll_stats_track_type == STATS_TRACK_ALL) {
572                 return seq_printf(m, "0 (all)\n");
573         } else {
574                 return seq_printf(m, "untracked\n");
575         }
576 }
577
578 static int ll_wr_track_id(const char *buffer, unsigned long count, void *data,
579                           enum stats_track_type type)
580 {
581         struct super_block *sb = data;
582         int rc, pid;
583
584         rc = lprocfs_write_helper(buffer, count, &pid);
585         if (rc)
586                 return rc;
587         ll_s2sbi(sb)->ll_stats_track_id = pid;
588         if (pid == 0)
589                 ll_s2sbi(sb)->ll_stats_track_type = STATS_TRACK_ALL;
590         else
591                 ll_s2sbi(sb)->ll_stats_track_type = type;
592         lprocfs_clear_stats(ll_s2sbi(sb)->ll_stats);
593         return count;
594 }
595
596 static int ll_track_pid_seq_show(struct seq_file *m, void *v)
597 {
598         return ll_rd_track_id(m, STATS_TRACK_PID);
599 }
600
601 static ssize_t ll_track_pid_seq_write(struct file *file, const char *buffer,
602                                       size_t count, loff_t *off)
603 {
604         struct seq_file *seq = file->private_data;
605         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_PID);
606 }
607 LPROC_SEQ_FOPS(ll_track_pid);
608
609 static int ll_track_ppid_seq_show(struct seq_file *m, void *v)
610 {
611         return ll_rd_track_id(m, STATS_TRACK_PPID);
612 }
613
614 static ssize_t ll_track_ppid_seq_write(struct file *file, const char *buffer,
615                                        size_t count, loff_t *off)
616 {
617         struct seq_file *seq = file->private_data;
618         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_PPID);
619 }
620 LPROC_SEQ_FOPS(ll_track_ppid);
621
622 static int ll_track_gid_seq_show(struct seq_file *m, void *v)
623 {
624         return ll_rd_track_id(m, STATS_TRACK_GID);
625 }
626
627 static ssize_t ll_track_gid_seq_write(struct file *file, const char *buffer,
628                                       size_t count, loff_t *off)
629 {
630         struct seq_file *seq = file->private_data;
631         return ll_wr_track_id(buffer, count, seq->private, STATS_TRACK_GID);
632 }
633 LPROC_SEQ_FOPS(ll_track_gid);
634
635 static int ll_statahead_max_seq_show(struct seq_file *m, void *v)
636 {
637         struct super_block *sb = m->private;
638         struct ll_sb_info *sbi = ll_s2sbi(sb);
639
640         return seq_printf(m, "%u\n", sbi->ll_sa_max);
641 }
642
643 static ssize_t ll_statahead_max_seq_write(struct file *file, const char *buffer,
644                                           size_t count, loff_t *off)
645 {
646         struct seq_file *m = file->private_data;
647         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
648         int val, rc;
649
650         rc = lprocfs_write_helper(buffer, count, &val);
651         if (rc)
652                 return rc;
653
654         if (val >= 0 && val <= LL_SA_RPC_MAX)
655                 sbi->ll_sa_max = val;
656         else
657                 CERROR("Bad statahead_max value %d. Valid values are in the "
658                        "range [0, %d]\n", val, LL_SA_RPC_MAX);
659
660         return count;
661 }
662 LPROC_SEQ_FOPS(ll_statahead_max);
663
664 static int ll_statahead_agl_seq_show(struct seq_file *m, void *v)
665 {
666         struct super_block *sb = m->private;
667         struct ll_sb_info *sbi = ll_s2sbi(sb);
668
669         return seq_printf(m, "%u\n",
670                           sbi->ll_flags & LL_SBI_AGL_ENABLED ? 1 : 0);
671 }
672
673 static ssize_t ll_statahead_agl_seq_write(struct file *file, const char *buffer,
674                                           size_t count, loff_t *off)
675 {
676         struct seq_file *m = file->private_data;
677         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
678         int val, rc;
679
680         rc = lprocfs_write_helper(buffer, count, &val);
681         if (rc)
682                 return rc;
683
684         if (val)
685                 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
686         else
687                 sbi->ll_flags &= ~LL_SBI_AGL_ENABLED;
688
689         return count;
690 }
691 LPROC_SEQ_FOPS(ll_statahead_agl);
692
693 static int ll_statahead_stats_seq_show(struct seq_file *m, void *v)
694 {
695         struct super_block *sb = m->private;
696         struct ll_sb_info *sbi = ll_s2sbi(sb);
697
698         return seq_printf(m,
699                         "statahead total: %u\n"
700                         "statahead wrong: %u\n"
701                         "agl total: %u\n",
702                         atomic_read(&sbi->ll_sa_total),
703                         atomic_read(&sbi->ll_sa_wrong),
704                         atomic_read(&sbi->ll_agl_total));
705 }
706 LPROC_SEQ_FOPS_RO(ll_statahead_stats);
707
708 static int ll_lazystatfs_seq_show(struct seq_file *m, void *v)
709 {
710         struct super_block *sb = m->private;
711         struct ll_sb_info *sbi = ll_s2sbi(sb);
712
713         return seq_printf(m, "%u\n",
714                           (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
715 }
716
717 static ssize_t ll_lazystatfs_seq_write(struct file *file, const char *buffer,
718                                         size_t count, loff_t *off)
719 {
720         struct seq_file *m = file->private_data;
721         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)m->private);
722         int val, rc;
723
724         rc = lprocfs_write_helper(buffer, count, &val);
725         if (rc)
726                 return rc;
727
728         if (val)
729                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
730         else
731                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
732
733         return count;
734 }
735 LPROC_SEQ_FOPS(ll_lazystatfs);
736
737 static int ll_max_easize_seq_show(struct seq_file *m, void *v)
738 {
739         struct super_block *sb = m->private;
740         struct ll_sb_info *sbi = ll_s2sbi(sb);
741         unsigned int ealen;
742         int rc;
743
744         rc = ll_get_max_mdsize(sbi, &ealen);
745         if (rc)
746                 return rc;
747
748         return seq_printf(m, "%u\n", ealen);
749 }
750 LPROC_SEQ_FOPS_RO(ll_max_easize);
751
752 static int ll_defult_easize_seq_show(struct seq_file *m, void *v)
753 {
754         struct super_block *sb = m->private;
755         struct ll_sb_info *sbi = ll_s2sbi(sb);
756         unsigned int ealen;
757         int rc;
758
759         rc = ll_get_default_mdsize(sbi, &ealen);
760         if (rc)
761                 return rc;
762
763         return seq_printf(m, "%u\n", ealen);
764 }
765 LPROC_SEQ_FOPS_RO(ll_defult_easize);
766
767 static int ll_max_cookiesize_seq_show(struct seq_file *m, void *v)
768 {
769         struct super_block *sb = m->private;
770         struct ll_sb_info *sbi = ll_s2sbi(sb);
771         unsigned int cookielen;
772         int rc;
773
774         rc = ll_get_max_cookiesize(sbi, &cookielen);
775         if (rc)
776                 return rc;
777
778         return seq_printf(m, "%u\n", cookielen);
779 }
780 LPROC_SEQ_FOPS_RO(ll_max_cookiesize);
781
782 static int ll_defult_cookiesize_seq_show(struct seq_file *m, void *v)
783 {
784         struct super_block *sb = m->private;
785         struct ll_sb_info *sbi = ll_s2sbi(sb);
786         unsigned int cookielen;
787         int rc;
788
789         rc = ll_get_default_cookiesize(sbi, &cookielen);
790         if (rc)
791                 return rc;
792
793         return seq_printf(m, "%u\n", cookielen);
794 }
795 LPROC_SEQ_FOPS_RO(ll_defult_cookiesize);
796
797 static int ll_sbi_flags_seq_show(struct seq_file *m, void *v)
798 {
799         const char *str[] = LL_SBI_FLAGS;
800         struct super_block *sb = m->private;
801         int flags = ll_s2sbi(sb)->ll_flags;
802         int i = 0;
803
804         while (flags != 0) {
805                 if (ARRAY_SIZE(str) <= i) {
806                         CERROR("%s: Revise array LL_SBI_FLAGS to match sbi "
807                                 "flags please.\n", ll_get_fsname(sb, NULL, 0));
808                         return -EINVAL;
809                 }
810
811                 if (flags & 0x1)
812                         seq_printf(m, "%s ", str[i]);
813                 flags >>= 1;
814                 ++i;
815         }
816         seq_printf(m, "\b\n");
817         return 0;
818 }
819 LPROC_SEQ_FOPS_RO(ll_sbi_flags);
820
821 static int ll_unstable_stats_seq_show(struct seq_file *m, void *v)
822 {
823         struct super_block      *sb    = m->private;
824         struct ll_sb_info       *sbi   = ll_s2sbi(sb);
825         struct cl_client_cache  *cache = &sbi->ll_cache;
826         int pages, mb;
827
828         pages = atomic_read(&cache->ccc_unstable_nr);
829         mb    = (pages * PAGE_CACHE_SIZE) >> 20;
830
831         return seq_printf(m, "unstable_check: %8d\n"
832                              "unstable_pages: %8d\n"
833                              "unstable_mb:    %8d\n",
834                           cache->ccc_unstable_check, pages, mb);
835 }
836
837 static ssize_t ll_unstable_stats_seq_write(struct file *file,
838                                            const char __user *buffer,
839                                            size_t count, loff_t *unused)
840 {
841         struct seq_file *seq = file->private_data;
842         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)seq->private);
843         char kernbuf[128];
844         int val, rc;
845
846         if (count == 0)
847                 return 0;
848         if (count >= sizeof(kernbuf))
849                 return -EINVAL;
850
851         if (copy_from_user(kernbuf, buffer, count))
852                 return -EFAULT;
853         kernbuf[count] = 0;
854
855         buffer += lprocfs_find_named_value(kernbuf, "unstable_check:", &count) -
856                   kernbuf;
857         rc = lprocfs_write_helper(buffer, count, &val);
858         if (rc < 0)
859                 return rc;
860
861         /* borrow lru lock to set the value */
862         spin_lock(&sbi->ll_cache.ccc_lru_lock);
863         sbi->ll_cache.ccc_unstable_check = !!val;
864         spin_unlock(&sbi->ll_cache.ccc_lru_lock);
865
866         return count;
867 }
868 LPROC_SEQ_FOPS(ll_unstable_stats);
869
870 static int ll_root_squash_seq_show(struct seq_file *m, void *v)
871 {
872         struct super_block *sb = m->private;
873         struct ll_sb_info *sbi = ll_s2sbi(sb);
874         struct root_squash_info *squash = &sbi->ll_squash;
875
876         return seq_printf(m, "%u:%u\n", squash->rsi_uid, squash->rsi_gid);
877 }
878
879 static ssize_t ll_root_squash_seq_write(struct file *file,
880                                         const char __user *buffer,
881                                         size_t count, loff_t *off)
882 {
883         struct seq_file *m = file->private_data;
884         struct super_block *sb = m->private;
885         struct ll_sb_info *sbi = ll_s2sbi(sb);
886         struct root_squash_info *squash = &sbi->ll_squash;
887
888         return lprocfs_wr_root_squash(buffer, count, squash,
889                                       ll_get_fsname(sb, NULL, 0));
890 }
891 LPROC_SEQ_FOPS(ll_root_squash);
892
893 static int ll_nosquash_nids_seq_show(struct seq_file *m, void *v)
894 {
895         struct super_block *sb = m->private;
896         struct ll_sb_info *sbi = ll_s2sbi(sb);
897         struct root_squash_info *squash = &sbi->ll_squash;
898         int len, rc;
899
900         down_read(&squash->rsi_sem);
901         if (!list_empty(&squash->rsi_nosquash_nids)) {
902                 len = cfs_print_nidlist(m->buf + m->count, m->size - m->count,
903                                         &squash->rsi_nosquash_nids);
904                 m->count += len;
905                 rc = seq_printf(m, "\n");
906         } else {
907                 rc = seq_printf(m, "NONE\n");
908         }
909         up_read(&squash->rsi_sem);
910
911         return rc;
912 }
913
914 static ssize_t ll_nosquash_nids_seq_write(struct file *file,
915                                           const char __user *buffer,
916                                           size_t count, loff_t *off)
917 {
918         struct seq_file *m = file->private_data;
919         struct super_block *sb = m->private;
920         struct ll_sb_info *sbi = ll_s2sbi(sb);
921         struct root_squash_info *squash = &sbi->ll_squash;
922         int rc;
923
924         rc = lprocfs_wr_nosquash_nids(buffer, count, squash,
925                                       ll_get_fsname(sb, NULL, 0));
926         if (rc < 0)
927                 return rc;
928
929         ll_compute_rootsquash_state(sbi);
930
931         return rc;
932 }
933 LPROC_SEQ_FOPS(ll_nosquash_nids);
934
935 struct lprocfs_seq_vars lprocfs_llite_obd_vars[] = {
936         { .name =       "uuid",
937           .fops =       &ll_sb_uuid_fops                        },
938         { .name =       "fstype",
939           .fops =       &ll_fstype_fops                         },
940         { .name =       "site",
941           .fops =       &ll_site_stats_fops                     },
942         { .name =       "blocksize",
943           .fops =       &ll_blksize_fops                        },
944         { .name =       "kbytestotal",
945           .fops =       &ll_kbytestotal_fops                    },
946         { .name =       "kbytesfree",
947           .fops =       &ll_kbytesfree_fops                     },
948         { .name =       "kbytesavail",
949           .fops =       &ll_kbytesavail_fops                    },
950         { .name =       "filestotal",
951           .fops =       &ll_filestotal_fops                     },
952         { .name =       "filesfree",
953           .fops =       &ll_filesfree_fops                      },
954         { .name =       "client_type",
955           .fops =       &ll_client_type_fops                    },
956         { .name =       "max_read_ahead_mb",
957           .fops =       &ll_max_readahead_mb_fops               },
958         { .name =       "max_read_ahead_per_file_mb",
959           .fops =       &ll_max_readahead_per_file_mb_fops      },
960         { .name =       "max_read_ahead_whole_mb",
961           .fops =       &ll_max_read_ahead_whole_mb_fops        },
962         { .name =       "max_cached_mb",
963           .fops =       &ll_max_cached_mb_fops                  },
964         { .name =       "checksum_pages",
965           .fops =       &ll_checksum_fops                       },
966         { .name =       "max_rw_chunk",
967           .fops =       &ll_max_rw_chunk_fops                   },
968         { .name =       "stats_track_pid",
969           .fops =       &ll_track_pid_fops                      },
970         { .name =       "stats_track_ppid",
971           .fops =       &ll_track_ppid_fops                     },
972         { .name =       "stats_track_gid",
973           .fops =       &ll_track_gid_fops                      },
974         { .name =       "statahead_max",
975           .fops =       &ll_statahead_max_fops                  },
976         { .name =       "statahead_agl",
977           .fops =       &ll_statahead_agl_fops                  },
978         { .name =       "statahead_stats",
979           .fops =       &ll_statahead_stats_fops                },
980         { .name =       "lazystatfs",
981           .fops =       &ll_lazystatfs_fops                     },
982         { .name =       "max_easize",
983           .fops =       &ll_max_easize_fops                     },
984         { .name =       "default_easize",
985           .fops =       &ll_defult_easize_fops                  },
986         { .name =       "max_cookiesize",
987           .fops =       &ll_max_cookiesize_fops                 },
988         { .name =       "default_cookiesize",
989           .fops =       &ll_defult_cookiesize_fops              },
990         { .name =       "sbi_flags",
991           .fops =       &ll_sbi_flags_fops                      },
992         { .name =       "xattr_cache",
993           .fops =       &ll_xattr_cache_fops                    },
994         { .name =       "unstable_stats",
995           .fops =       &ll_unstable_stats_fops                 },
996         { .name =       "root_squash",
997           .fops =       &ll_root_squash_fops                    },
998         { .name =       "nosquash_nids",
999           .fops =       &ll_nosquash_nids_fops                  },
1000         { 0 }
1001 };
1002
1003 #define MAX_STRING_SIZE 128
1004
1005 static const struct llite_file_opcode {
1006         __u32       opcode;
1007         __u32       type;
1008         const char *opname;
1009 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
1010         /* file operation */
1011         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
1012         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
1013         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
1014                                    "read_bytes" },
1015         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
1016                                    "write_bytes" },
1017         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
1018                                    "brw_read" },
1019         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
1020                                    "brw_write" },
1021         { LPROC_LL_OSC_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
1022                                    "osc_read" },
1023         { LPROC_LL_OSC_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
1024                                    "osc_write" },
1025         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
1026         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
1027         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
1028         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
1029         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
1030         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
1031         { LPROC_LL_READDIR,        LPROCFS_TYPE_REGS, "readdir" },
1032         /* inode operation */
1033         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
1034         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "truncate" },
1035         { LPROC_LL_FLOCK,          LPROCFS_TYPE_REGS, "flock" },
1036         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
1037         /* dir inode operation */
1038         { LPROC_LL_CREATE,         LPROCFS_TYPE_REGS, "create" },
1039         { LPROC_LL_LINK,           LPROCFS_TYPE_REGS, "link" },
1040         { LPROC_LL_UNLINK,         LPROCFS_TYPE_REGS, "unlink" },
1041         { LPROC_LL_SYMLINK,        LPROCFS_TYPE_REGS, "symlink" },
1042         { LPROC_LL_MKDIR,          LPROCFS_TYPE_REGS, "mkdir" },
1043         { LPROC_LL_RMDIR,          LPROCFS_TYPE_REGS, "rmdir" },
1044         { LPROC_LL_MKNOD,          LPROCFS_TYPE_REGS, "mknod" },
1045         { LPROC_LL_RENAME,         LPROCFS_TYPE_REGS, "rename" },
1046         /* special inode operation */
1047         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
1048         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
1049         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
1050         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
1051         { LPROC_LL_GETXATTR_HITS,  LPROCFS_TYPE_REGS, "getxattr_hits" },
1052         { LPROC_LL_LISTXATTR,      LPROCFS_TYPE_REGS, "listxattr" },
1053         { LPROC_LL_REMOVEXATTR,    LPROCFS_TYPE_REGS, "removexattr" },
1054         { LPROC_LL_INODE_PERM,     LPROCFS_TYPE_REGS, "inode_permission" },
1055 };
1056
1057 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, int count)
1058 {
1059         if (!sbi->ll_stats)
1060                 return;
1061         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
1062                 lprocfs_counter_add(sbi->ll_stats, op, count);
1063         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
1064                  sbi->ll_stats_track_id == current->pid)
1065                 lprocfs_counter_add(sbi->ll_stats, op, count);
1066         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
1067                  sbi->ll_stats_track_id == current->parent->pid)
1068                 lprocfs_counter_add(sbi->ll_stats, op, count);
1069         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
1070                  sbi->ll_stats_track_id ==
1071                         from_kgid(&init_user_ns, current_gid()))
1072                 lprocfs_counter_add(sbi->ll_stats, op, count);
1073 }
1074 EXPORT_SYMBOL(ll_stats_ops_tally);
1075
1076 static const char *ra_stat_string[] = {
1077         [RA_STAT_HIT] = "hits",
1078         [RA_STAT_MISS] = "misses",
1079         [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
1080         [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
1081         [RA_STAT_FAILED_GRAB_PAGE] = "failed grab_cache_page",
1082         [RA_STAT_FAILED_MATCH] = "failed lock match",
1083         [RA_STAT_DISCARDED] = "read but discarded",
1084         [RA_STAT_ZERO_LEN] = "zero length file",
1085         [RA_STAT_ZERO_WINDOW] = "zero size window",
1086         [RA_STAT_EOF] = "read-ahead to EOF",
1087         [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
1088         [RA_STAT_WRONG_GRAB_PAGE] = "wrong page from grab_cache_page",
1089         [RA_STAT_FAILED_REACH_END] = "failed to reach end"
1090 };
1091
1092 LPROC_SEQ_FOPS_RO_TYPE(llite, name);
1093 LPROC_SEQ_FOPS_RO_TYPE(llite, uuid);
1094
1095 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
1096                                 struct super_block *sb, char *osc, char *mdc)
1097 {
1098         struct lprocfs_seq_vars lvars[2];
1099         struct lustre_sb_info *lsi = s2lsi(sb);
1100         struct ll_sb_info *sbi = ll_s2sbi(sb);
1101         struct obd_device *obd;
1102         struct proc_dir_entry *dir;
1103         char name[MAX_STRING_SIZE + 1], *ptr;
1104         int err, id, len, rc;
1105         ENTRY;
1106
1107         memset(lvars, 0, sizeof(lvars));
1108
1109         name[MAX_STRING_SIZE] = '\0';
1110         lvars[0].name = name;
1111
1112         LASSERT(sbi != NULL);
1113         LASSERT(mdc != NULL);
1114         LASSERT(osc != NULL);
1115
1116         /* Get fsname */
1117         len = strlen(lsi->lsi_lmd->lmd_profile);
1118         ptr = strrchr(lsi->lsi_lmd->lmd_profile, '-');
1119         if (ptr && (strcmp(ptr, "-client") == 0))
1120                 len -= 7;
1121
1122         /* Mount info */
1123         snprintf(name, MAX_STRING_SIZE, "%.*s-%p", len,
1124                  lsi->lsi_lmd->lmd_profile, sb);
1125
1126         sbi->ll_proc_root = lprocfs_seq_register(name, parent, NULL, NULL);
1127         if (IS_ERR(sbi->ll_proc_root)) {
1128                 err = PTR_ERR(sbi->ll_proc_root);
1129                 sbi->ll_proc_root = NULL;
1130                 RETURN(err);
1131         }
1132
1133         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
1134                                 &vvp_dump_pgcache_file_ops, sbi);
1135         if (rc)
1136                 CWARN("Error adding the dump_page_cache file\n");
1137
1138         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
1139                                 &ll_rw_extents_stats_fops, sbi);
1140         if (rc)
1141                 CWARN("Error adding the extent_stats file\n");
1142
1143         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
1144                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
1145         if (rc)
1146                 CWARN("Error adding the extents_stats_per_process file\n");
1147
1148         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
1149                                 &ll_rw_offset_stats_fops, sbi);
1150         if (rc)
1151                 CWARN("Error adding the offset_stats file\n");
1152
1153         /* File operations stats */
1154         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
1155                                             LPROCFS_STATS_FLAG_NONE);
1156         if (sbi->ll_stats == NULL)
1157                 GOTO(out, err = -ENOMEM);
1158         /* do counter init */
1159         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
1160                 __u32 type = llite_opcode_table[id].type;
1161                 void *ptr = NULL;
1162                 if (type & LPROCFS_TYPE_REGS)
1163                         ptr = "regs";
1164                 else if (type & LPROCFS_TYPE_BYTES)
1165                         ptr = "bytes";
1166                 else if (type & LPROCFS_TYPE_PAGES)
1167                         ptr = "pages";
1168                 lprocfs_counter_init(sbi->ll_stats,
1169                                      llite_opcode_table[id].opcode,
1170                                      (type & LPROCFS_CNTR_AVGMINMAX),
1171                                      llite_opcode_table[id].opname, ptr);
1172         }
1173         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
1174         if (err)
1175                 GOTO(out, err);
1176
1177         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
1178                                                LPROCFS_STATS_FLAG_NONE);
1179         if (sbi->ll_ra_stats == NULL)
1180                 GOTO(out, err = -ENOMEM);
1181
1182         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
1183                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
1184                                      ra_stat_string[id], "pages");
1185         err = lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
1186                                      sbi->ll_ra_stats);
1187         if (err)
1188                 GOTO(out, err);
1189
1190
1191         err = lprocfs_seq_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
1192         if (err)
1193                 GOTO(out, err);
1194
1195         /* MDC info */
1196         obd = class_name2obd(mdc);
1197
1198         LASSERT(obd != NULL);
1199         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1200         LASSERT(obd->obd_type->typ_name != NULL);
1201
1202         dir = proc_mkdir(obd->obd_type->typ_name, sbi->ll_proc_root);
1203         if (dir == NULL)
1204                 GOTO(out, err = -ENOMEM);
1205
1206         snprintf(name, MAX_STRING_SIZE, "common_name");
1207         lvars[0].fops = &llite_name_fops;
1208         err = lprocfs_seq_add_vars(dir, lvars, obd);
1209         if (err)
1210                 GOTO(out, err);
1211
1212         snprintf(name, MAX_STRING_SIZE, "uuid");
1213         lvars[0].fops = &llite_uuid_fops;
1214         err = lprocfs_seq_add_vars(dir, lvars, obd);
1215         if (err)
1216                 GOTO(out, err);
1217
1218         /* OSC */
1219         obd = class_name2obd(osc);
1220
1221         LASSERT(obd != NULL);
1222         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1223         LASSERT(obd->obd_type->typ_name != NULL);
1224
1225         dir = proc_mkdir(obd->obd_type->typ_name, sbi->ll_proc_root);
1226         if (dir == NULL)
1227                 GOTO(out, err = -ENOMEM);
1228
1229         snprintf(name, MAX_STRING_SIZE, "common_name");
1230         lvars[0].fops = &llite_name_fops;
1231         err = lprocfs_seq_add_vars(dir, lvars, obd);
1232         if (err)
1233                 GOTO(out, err);
1234
1235         snprintf(name, MAX_STRING_SIZE, "uuid");
1236         lvars[0].fops = &llite_uuid_fops;
1237         err = lprocfs_seq_add_vars(dir, lvars, obd);
1238 out:
1239         if (err) {
1240                 lprocfs_remove(&sbi->ll_proc_root);
1241                 lprocfs_free_stats(&sbi->ll_ra_stats);
1242                 lprocfs_free_stats(&sbi->ll_stats);
1243         }
1244         RETURN(err);
1245 }
1246
1247 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
1248 {
1249         if (sbi->ll_proc_root) {
1250                 lprocfs_remove(&sbi->ll_proc_root);
1251                 lprocfs_free_stats(&sbi->ll_ra_stats);
1252                 lprocfs_free_stats(&sbi->ll_stats);
1253         }
1254 }
1255 #undef MAX_STRING_SIZE
1256
1257 #define pct(a,b) (b ? a * 100 / b : 0)
1258
1259 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1260                                    struct seq_file *seq, int which)
1261 {
1262         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1263         unsigned long start, end, r, w;
1264         char *unitp = "KMGTPEZY";
1265         int i, units = 10;
1266         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1267
1268         read_cum = 0;
1269         write_cum = 0;
1270         start = 0;
1271
1272         for(i = 0; i < LL_HIST_MAX; i++) {
1273                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1274                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1275         }
1276
1277         for(i = 0; i < LL_HIST_MAX; i++) {
1278                 r = pp_info->pp_r_hist.oh_buckets[i];
1279                 w = pp_info->pp_w_hist.oh_buckets[i];
1280                 read_cum += r;
1281                 write_cum += w;
1282                 end = 1 << (i + LL_HIST_START - units);
1283                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1284                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1285                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1286                            r, pct(r, read_tot), pct(read_cum, read_tot),
1287                            w, pct(w, write_tot), pct(write_cum, write_tot));
1288                 start = end;
1289                 if (start == 1<<10) {
1290                         start = 1;
1291                         units += 10;
1292                         unitp++;
1293                 }
1294                 if (read_cum == read_tot && write_cum == write_tot)
1295                         break;
1296         }
1297 }
1298
1299 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1300 {
1301         struct timeval now;
1302         struct ll_sb_info *sbi = seq->private;
1303         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1304         int k;
1305
1306         do_gettimeofday(&now);
1307
1308         if (!sbi->ll_rw_stats_on) {
1309                 seq_printf(seq, "disabled\n"
1310                                 "write anything in this file to activate, "
1311                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1312                 return 0;
1313         }
1314         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1315                    now.tv_sec, now.tv_usec);
1316         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1317         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1318                    "extents", "calls", "%", "cum%",
1319                    "calls", "%", "cum%");
1320         spin_lock(&sbi->ll_pp_extent_lock);
1321         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1322                 if (io_extents->pp_extents[k].pid != 0) {
1323                         seq_printf(seq, "\nPID: %d\n",
1324                                    io_extents->pp_extents[k].pid);
1325                         ll_display_extents_info(io_extents, seq, k);
1326                 }
1327         }
1328         spin_unlock(&sbi->ll_pp_extent_lock);
1329         return 0;
1330 }
1331
1332 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1333                                                 const char __user *buf,
1334                                                 size_t len,
1335                                                 loff_t *off)
1336 {
1337         struct seq_file *seq = file->private_data;
1338         struct ll_sb_info *sbi = seq->private;
1339         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1340         int i;
1341         int value = 1, rc = 0;
1342
1343         if (len == 0)
1344                 return -EINVAL;
1345
1346         rc = lprocfs_write_helper(buf, len, &value);
1347         if (rc < 0 && len < 16) {
1348                 char kernbuf[16];
1349
1350                 if (copy_from_user(kernbuf, buf, len))
1351                         return -EFAULT;
1352                 kernbuf[len] = 0;
1353
1354                 if (kernbuf[len - 1] == '\n')
1355                         kernbuf[len - 1] = 0;
1356
1357                 if (strcmp(kernbuf, "disabled") == 0 ||
1358                     strcmp(kernbuf, "Disabled") == 0)
1359                         value = 0;
1360         }
1361
1362         if (value == 0)
1363                 sbi->ll_rw_stats_on = 0;
1364         else
1365                 sbi->ll_rw_stats_on = 1;
1366
1367         spin_lock(&sbi->ll_pp_extent_lock);
1368         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1369                 io_extents->pp_extents[i].pid = 0;
1370                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1371                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1372         }
1373         spin_unlock(&sbi->ll_pp_extent_lock);
1374         return len;
1375 }
1376
1377 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1378
1379 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1380 {
1381         struct timeval now;
1382         struct ll_sb_info *sbi = seq->private;
1383         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1384
1385         do_gettimeofday(&now);
1386
1387         if (!sbi->ll_rw_stats_on) {
1388                 seq_printf(seq, "disabled\n"
1389                                 "write anything in this file to activate, "
1390                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1391                 return 0;
1392         }
1393         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1394                    now.tv_sec, now.tv_usec);
1395
1396         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1397         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1398                    "extents", "calls", "%", "cum%",
1399                    "calls", "%", "cum%");
1400         spin_lock(&sbi->ll_lock);
1401         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1402         spin_unlock(&sbi->ll_lock);
1403
1404         return 0;
1405 }
1406
1407 static ssize_t ll_rw_extents_stats_seq_write(struct file *file,
1408                                              const char __user *buf,
1409                                              size_t len, loff_t *off)
1410 {
1411         struct seq_file *seq = file->private_data;
1412         struct ll_sb_info *sbi = seq->private;
1413         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1414         int i;
1415         int value = 1, rc = 0;
1416
1417         if (len == 0)
1418                 return -EINVAL;
1419
1420         rc = lprocfs_write_helper(buf, len, &value);
1421         if (rc < 0 && len < 16) {
1422                 char kernbuf[16];
1423
1424                 if (copy_from_user(kernbuf, buf, len))
1425                         return -EFAULT;
1426                 kernbuf[len] = 0;
1427
1428                 if (kernbuf[len - 1] == '\n')
1429                         kernbuf[len - 1] = 0;
1430
1431                 if (strcmp(kernbuf, "disabled") == 0 ||
1432                     strcmp(kernbuf, "Disabled") == 0)
1433                         value = 0;
1434         }
1435
1436         if (value == 0)
1437                 sbi->ll_rw_stats_on = 0;
1438         else
1439                 sbi->ll_rw_stats_on = 1;
1440
1441         spin_lock(&sbi->ll_pp_extent_lock);
1442         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
1443                 io_extents->pp_extents[i].pid = 0;
1444                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1445                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1446         }
1447         spin_unlock(&sbi->ll_pp_extent_lock);
1448
1449         return len;
1450 }
1451 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1452
1453 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
1454                        struct ll_file_data *file, loff_t pos,
1455                        size_t count, int rw)
1456 {
1457         int i, cur = -1;
1458         struct ll_rw_process_info *process;
1459         struct ll_rw_process_info *offset;
1460         int *off_count = &sbi->ll_rw_offset_entry_count;
1461         int *process_count = &sbi->ll_offset_process_count;
1462         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1463
1464         if(!sbi->ll_rw_stats_on)
1465                 return;
1466         process = sbi->ll_rw_process_info;
1467         offset = sbi->ll_rw_offset_info;
1468
1469         spin_lock(&sbi->ll_pp_extent_lock);
1470         /* Extent statistics */
1471         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1472                 if(io_extents->pp_extents[i].pid == pid) {
1473                         cur = i;
1474                         break;
1475                 }
1476         }
1477
1478         if (cur == -1) {
1479                 /* new process */
1480                 sbi->ll_extent_process_count =
1481                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1482                 cur = sbi->ll_extent_process_count;
1483                 io_extents->pp_extents[cur].pid = pid;
1484                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1485                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1486         }
1487
1488         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1489              (i < (LL_HIST_MAX - 1)); i++);
1490         if (rw == 0) {
1491                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1492                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1493         } else {
1494                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1495                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1496         }
1497         spin_unlock(&sbi->ll_pp_extent_lock);
1498
1499         spin_lock(&sbi->ll_process_lock);
1500         /* Offset statistics */
1501         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1502                 if (process[i].rw_pid == pid) {
1503                         if (process[i].rw_last_file != file) {
1504                                 process[i].rw_range_start = pos;
1505                                 process[i].rw_last_file_pos = pos + count;
1506                                 process[i].rw_smallest_extent = count;
1507                                 process[i].rw_largest_extent = count;
1508                                 process[i].rw_offset = 0;
1509                                 process[i].rw_last_file = file;
1510                                 spin_unlock(&sbi->ll_process_lock);
1511                                 return;
1512                         }
1513                         if (process[i].rw_last_file_pos != pos) {
1514                                 *off_count =
1515                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1516                                 offset[*off_count].rw_op = process[i].rw_op;
1517                                 offset[*off_count].rw_pid = pid;
1518                                 offset[*off_count].rw_range_start =
1519                                         process[i].rw_range_start;
1520                                 offset[*off_count].rw_range_end =
1521                                         process[i].rw_last_file_pos;
1522                                 offset[*off_count].rw_smallest_extent =
1523                                         process[i].rw_smallest_extent;
1524                                 offset[*off_count].rw_largest_extent =
1525                                         process[i].rw_largest_extent;
1526                                 offset[*off_count].rw_offset =
1527                                         process[i].rw_offset;
1528                                 process[i].rw_op = rw;
1529                                 process[i].rw_range_start = pos;
1530                                 process[i].rw_smallest_extent = count;
1531                                 process[i].rw_largest_extent = count;
1532                                 process[i].rw_offset = pos -
1533                                         process[i].rw_last_file_pos;
1534                         }
1535                         if(process[i].rw_smallest_extent > count)
1536                                 process[i].rw_smallest_extent = count;
1537                         if(process[i].rw_largest_extent < count)
1538                                 process[i].rw_largest_extent = count;
1539                         process[i].rw_last_file_pos = pos + count;
1540                         spin_unlock(&sbi->ll_process_lock);
1541                         return;
1542                 }
1543         }
1544         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1545         process[*process_count].rw_pid = pid;
1546         process[*process_count].rw_op = rw;
1547         process[*process_count].rw_range_start = pos;
1548         process[*process_count].rw_last_file_pos = pos + count;
1549         process[*process_count].rw_smallest_extent = count;
1550         process[*process_count].rw_largest_extent = count;
1551         process[*process_count].rw_offset = 0;
1552         process[*process_count].rw_last_file = file;
1553         spin_unlock(&sbi->ll_process_lock);
1554 }
1555
1556 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1557 {
1558         struct timeval now;
1559         struct ll_sb_info *sbi = seq->private;
1560         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1561         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1562         int i;
1563
1564         do_gettimeofday(&now);
1565
1566         if (!sbi->ll_rw_stats_on) {
1567                 seq_printf(seq, "disabled\n"
1568                                 "write anything in this file to activate, "
1569                                 "then 0 or \"[D/d]isabled\" to deactivate\n");
1570                 return 0;
1571         }
1572         spin_lock(&sbi->ll_process_lock);
1573
1574         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
1575                    now.tv_sec, now.tv_usec);
1576         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1577                    "R/W", "PID", "RANGE START", "RANGE END",
1578                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1579
1580         /* We stored the discontiguous offsets here; print them first */
1581         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1582                 if (offset[i].rw_pid != 0)
1583                         seq_printf(seq,
1584                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1585                                    offset[i].rw_op == READ ? 'R' : 'W',
1586                                    offset[i].rw_pid,
1587                                    offset[i].rw_range_start,
1588                                    offset[i].rw_range_end,
1589                                    (unsigned long)offset[i].rw_smallest_extent,
1590                                    (unsigned long)offset[i].rw_largest_extent,
1591                                    offset[i].rw_offset);
1592         }
1593
1594         /* Then print the current offsets for each process */
1595         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1596                 if (process[i].rw_pid != 0)
1597                         seq_printf(seq,
1598                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1599                                    process[i].rw_op == READ ? 'R' : 'W',
1600                                    process[i].rw_pid,
1601                                    process[i].rw_range_start,
1602                                    process[i].rw_last_file_pos,
1603                                    (unsigned long)process[i].rw_smallest_extent,
1604                                    (unsigned long)process[i].rw_largest_extent,
1605                                    process[i].rw_offset);
1606         }
1607         spin_unlock(&sbi->ll_process_lock);
1608
1609         return 0;
1610 }
1611
1612 static ssize_t ll_rw_offset_stats_seq_write(struct file *file,
1613                                             const char __user *buf,
1614                                             size_t len, loff_t *off)
1615 {
1616         struct seq_file *seq = file->private_data;
1617         struct ll_sb_info *sbi = seq->private;
1618         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1619         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1620         int value = 1, rc = 0;
1621
1622         if (len == 0)
1623                 return -EINVAL;
1624
1625         rc = lprocfs_write_helper(buf, len, &value);
1626
1627         if (rc < 0 && len < 16) {
1628                 char kernbuf[16];
1629
1630                 if (copy_from_user(kernbuf, buf, len))
1631                         return -EFAULT;
1632                 kernbuf[len] = 0;
1633
1634                 if (kernbuf[len - 1] == '\n')
1635                         kernbuf[len - 1] = 0;
1636
1637                 if (strcmp(kernbuf, "disabled") == 0 ||
1638                     strcmp(kernbuf, "Disabled") == 0)
1639                         value = 0;
1640         }
1641
1642         if (value == 0)
1643                 sbi->ll_rw_stats_on = 0;
1644         else
1645                 sbi->ll_rw_stats_on = 1;
1646
1647         spin_lock(&sbi->ll_process_lock);
1648         sbi->ll_offset_process_count = 0;
1649         sbi->ll_rw_offset_entry_count = 0;
1650         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1651                LL_PROCESS_HIST_MAX);
1652         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1653                LL_OFFSET_HIST_MAX);
1654         spin_unlock(&sbi->ll_process_lock);
1655
1656         return len;
1657 }
1658
1659 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1660 #endif /* LPROCFS */