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