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