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