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