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