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