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