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