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