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