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