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