Whamcloud - gitweb
LU-8066 llite: replace ll_process_config with class_modify_config
[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 lustre_sb_info *lsi = s2lsi(sb);
1347         struct ll_sb_info *sbi = ll_s2sbi(sb);
1348         struct lprocfs_vars lvars[2];
1349         int err, id, rc;
1350
1351         ENTRY;
1352         memset(lvars, 0, sizeof(lvars));
1353         lvars[0].name = name;
1354
1355         LASSERT(sbi != NULL);
1356
1357         sbi->ll_proc_root = lprocfs_register(name, proc_lustre_fs_root,
1358                                              NULL, NULL);
1359         if (IS_ERR(sbi->ll_proc_root)) {
1360                 err = PTR_ERR(sbi->ll_proc_root);
1361                 sbi->ll_proc_root = NULL;
1362                 RETURN(err);
1363         }
1364
1365         rc = lprocfs_seq_create(sbi->ll_proc_root, "dump_page_cache", 0444,
1366                                 &vvp_dump_pgcache_file_ops, sbi);
1367         if (rc)
1368                 CWARN("Error adding the dump_page_cache file\n");
1369
1370         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats", 0644,
1371                                 &ll_rw_extents_stats_fops, sbi);
1372         if (rc)
1373                 CWARN("Error adding the extent_stats file\n");
1374
1375         rc = lprocfs_seq_create(sbi->ll_proc_root, "extents_stats_per_process",
1376                                 0644, &ll_rw_extents_stats_pp_fops, sbi);
1377         if (rc)
1378                 CWARN("Error adding the extents_stats_per_process file\n");
1379
1380         rc = lprocfs_seq_create(sbi->ll_proc_root, "offset_stats", 0644,
1381                                 &ll_rw_offset_stats_fops, sbi);
1382         if (rc)
1383                 CWARN("Error adding the offset_stats file\n");
1384
1385         /* File operations stats */
1386         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
1387                                             LPROCFS_STATS_FLAG_NONE);
1388         if (sbi->ll_stats == NULL)
1389                 GOTO(out_proc, err = -ENOMEM);
1390
1391         /* do counter init */
1392         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
1393                 __u32 type = llite_opcode_table[id].type;
1394                 void *ptr = NULL;
1395                 if (type & LPROCFS_TYPE_REGS)
1396                         ptr = "regs";
1397                 else if (type & LPROCFS_TYPE_BYTES)
1398                         ptr = "bytes";
1399                 else if (type & LPROCFS_TYPE_PAGES)
1400                         ptr = "pages";
1401                 lprocfs_counter_init(sbi->ll_stats,
1402                                      llite_opcode_table[id].opcode,
1403                                      (type & LPROCFS_CNTR_AVGMINMAX),
1404                                      llite_opcode_table[id].opname, ptr);
1405         }
1406
1407         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", sbi->ll_stats);
1408         if (err)
1409                 GOTO(out_stats, err);
1410
1411         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
1412                                                LPROCFS_STATS_FLAG_NONE);
1413         if (sbi->ll_ra_stats == NULL)
1414                 GOTO(out_stats, err = -ENOMEM);
1415
1416         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
1417                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
1418                                      ra_stat_string[id], "pages");
1419         err = lprocfs_register_stats(sbi->ll_proc_root, "read_ahead_stats",
1420                                      sbi->ll_ra_stats);
1421         if (err)
1422                 GOTO(out_ra_stats, err);
1423
1424         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_llite_obd_vars, sb);
1425         if (err)
1426                 GOTO(out_ra_stats, err);
1427
1428         /* Yes we also register sysfs mount kset here as well */
1429         sbi->ll_kset.kobj.parent = llite_kobj;
1430         sbi->ll_kset.kobj.ktype = &llite_ktype;
1431         init_completion(&sbi->ll_kobj_unregister);
1432         err = kobject_set_name(&sbi->ll_kset.kobj, "%s", name);
1433         if (err)
1434                 GOTO(out_ra_stats, err);
1435
1436         err = kset_register(&sbi->ll_kset);
1437         if (err)
1438                 GOTO(out_ra_stats, err);
1439
1440         lsi->lsi_kobj = kobject_get(&sbi->ll_kset.kobj);
1441
1442         RETURN(0);
1443 out_ra_stats:
1444         lprocfs_free_stats(&sbi->ll_ra_stats);
1445 out_stats:
1446         lprocfs_free_stats(&sbi->ll_stats);
1447 out_proc:
1448         lprocfs_remove(&sbi->ll_proc_root);
1449
1450         RETURN(err);
1451 }
1452
1453 int lprocfs_ll_register_obd(struct super_block *sb, const char *obdname)
1454 {
1455         struct lprocfs_vars lvars[2];
1456         struct ll_sb_info *sbi = ll_s2sbi(sb);
1457         struct obd_device *obd;
1458         struct proc_dir_entry *dir;
1459         char name[MAX_STRING_SIZE + 1];
1460         int err;
1461         ENTRY;
1462
1463         memset(lvars, 0, sizeof(lvars));
1464
1465         name[MAX_STRING_SIZE] = '\0';
1466         lvars[0].name = name;
1467
1468         LASSERT(sbi != NULL);
1469         LASSERT(obdname != NULL);
1470
1471         obd = class_name2obd(obdname);
1472
1473         LASSERT(obd != NULL);
1474         LASSERT(obd->obd_magic == OBD_DEVICE_MAGIC);
1475         LASSERT(obd->obd_type->typ_name != NULL);
1476
1477         dir = proc_mkdir(obd->obd_type->typ_name, sbi->ll_proc_root);
1478         if (dir == NULL)
1479                 GOTO(out, err = -ENOMEM);
1480
1481         snprintf(name, MAX_STRING_SIZE, "common_name");
1482         lvars[0].fops = &llite_name_fops;
1483         err = lprocfs_add_vars(dir, lvars, obd);
1484         if (err)
1485                 GOTO(out, err);
1486
1487         snprintf(name, MAX_STRING_SIZE, "uuid");
1488         lvars[0].fops = &llite_uuid_fops;
1489         err = lprocfs_add_vars(dir, lvars, obd);
1490         if (err)
1491                 GOTO(out, err);
1492
1493 out:
1494         if (err) {
1495                 lprocfs_remove(&sbi->ll_proc_root);
1496                 lprocfs_free_stats(&sbi->ll_ra_stats);
1497                 lprocfs_free_stats(&sbi->ll_stats);
1498         }
1499         RETURN(err);
1500 }
1501
1502 void ll_debugfs_unregister_super(struct super_block *sb)
1503 {
1504         struct lustre_sb_info *lsi = s2lsi(sb);
1505         struct ll_sb_info *sbi = ll_s2sbi(sb);
1506
1507         kobject_put(lsi->lsi_kobj);
1508
1509         kset_unregister(&sbi->ll_kset);
1510         wait_for_completion(&sbi->ll_kobj_unregister);
1511
1512         if (sbi->ll_proc_root) {
1513                 lprocfs_remove(&sbi->ll_proc_root);
1514                 lprocfs_free_stats(&sbi->ll_ra_stats);
1515                 lprocfs_free_stats(&sbi->ll_stats);
1516         }
1517 }
1518 #undef MAX_STRING_SIZE
1519
1520 #define pct(a,b) (b ? a * 100 / b : 0)
1521
1522 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1523                                    struct seq_file *seq, int which)
1524 {
1525         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1526         unsigned long start, end, r, w;
1527         char *unitp = "KMGTPEZY";
1528         int i, units = 10;
1529         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1530
1531         read_cum = 0;
1532         write_cum = 0;
1533         start = 0;
1534
1535         for(i = 0; i < LL_HIST_MAX; i++) {
1536                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1537                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1538         }
1539
1540         for(i = 0; i < LL_HIST_MAX; i++) {
1541                 r = pp_info->pp_r_hist.oh_buckets[i];
1542                 w = pp_info->pp_w_hist.oh_buckets[i];
1543                 read_cum += r;
1544                 write_cum += w;
1545                 end = 1 << (i + LL_HIST_START - units);
1546                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4lu %4lu  | "
1547                            "%14lu %4lu %4lu\n", start, *unitp, end, *unitp,
1548                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1549                            r, pct(r, read_tot), pct(read_cum, read_tot),
1550                            w, pct(w, write_tot), pct(write_cum, write_tot));
1551                 start = end;
1552                 if (start == 1<<10) {
1553                         start = 1;
1554                         units += 10;
1555                         unitp++;
1556                 }
1557                 if (read_cum == read_tot && write_cum == write_tot)
1558                         break;
1559         }
1560 }
1561
1562 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1563 {
1564         struct timespec64 now;
1565         struct ll_sb_info *sbi = seq->private;
1566         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1567         int k;
1568
1569         ktime_get_real_ts64(&now);
1570
1571         if (!sbi->ll_rw_stats_on) {
1572                 seq_puts(seq, "disabled\n write anything to this file to activate, then '0' or 'disable' to deactivate\n");
1573                 return 0;
1574         }
1575         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
1576                    (s64)now.tv_sec, now.tv_nsec);
1577         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1578         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1579                    "extents", "calls", "%", "cum%",
1580                    "calls", "%", "cum%");
1581         spin_lock(&sbi->ll_pp_extent_lock);
1582         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1583                 if (io_extents->pp_extents[k].pid != 0) {
1584                         seq_printf(seq, "\nPID: %d\n",
1585                                    io_extents->pp_extents[k].pid);
1586                         ll_display_extents_info(io_extents, seq, k);
1587                 }
1588         }
1589         spin_unlock(&sbi->ll_pp_extent_lock);
1590         return 0;
1591 }
1592
1593 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
1594                                                 const char __user *buf,
1595                                                 size_t len,
1596                                                 loff_t *off)
1597 {
1598         struct seq_file *seq = file->private_data;
1599         struct ll_sb_info *sbi = seq->private;
1600         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1601         int i;
1602         __s64 value;
1603
1604         if (len == 0)
1605                 return -EINVAL;
1606
1607         value = ll_stats_pid_write(buf, len);
1608
1609         if (value == 0)
1610                 sbi->ll_rw_stats_on = 0;
1611         else
1612                 sbi->ll_rw_stats_on = 1;
1613
1614         spin_lock(&sbi->ll_pp_extent_lock);
1615         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1616                 io_extents->pp_extents[i].pid = 0;
1617                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1618                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1619         }
1620         spin_unlock(&sbi->ll_pp_extent_lock);
1621         return len;
1622 }
1623
1624 LPROC_SEQ_FOPS(ll_rw_extents_stats_pp);
1625
1626 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
1627 {
1628         struct timespec64 now;
1629         struct ll_sb_info *sbi = seq->private;
1630         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1631
1632         ktime_get_real_ts64(&now);
1633
1634         if (!sbi->ll_rw_stats_on) {
1635                 seq_puts(seq, "disabled\n write anything to this file to activate, then '0' or 'disable' to deactivate\n");
1636                 return 0;
1637         }
1638         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
1639                    (s64)now.tv_sec, now.tv_nsec);
1640
1641         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1642         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1643                    "extents", "calls", "%", "cum%",
1644                    "calls", "%", "cum%");
1645         spin_lock(&sbi->ll_lock);
1646         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
1647         spin_unlock(&sbi->ll_lock);
1648
1649         return 0;
1650 }
1651
1652 static ssize_t ll_rw_extents_stats_seq_write(struct file *file,
1653                                              const char __user *buf,
1654                                              size_t len, loff_t *off)
1655 {
1656         struct seq_file *seq = file->private_data;
1657         struct ll_sb_info *sbi = seq->private;
1658         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1659         int i;
1660         __s64 value;
1661
1662         if (len == 0)
1663                 return -EINVAL;
1664
1665         value = ll_stats_pid_write(buf, len);
1666
1667         if (value == 0)
1668                 sbi->ll_rw_stats_on = 0;
1669         else
1670                 sbi->ll_rw_stats_on = 1;
1671
1672         spin_lock(&sbi->ll_pp_extent_lock);
1673         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
1674                 io_extents->pp_extents[i].pid = 0;
1675                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
1676                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
1677         }
1678         spin_unlock(&sbi->ll_pp_extent_lock);
1679
1680         return len;
1681 }
1682 LPROC_SEQ_FOPS(ll_rw_extents_stats);
1683
1684 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
1685                        struct ll_file_data *file, loff_t pos,
1686                        size_t count, int rw)
1687 {
1688         int i, cur = -1;
1689         struct ll_rw_process_info *process;
1690         struct ll_rw_process_info *offset;
1691         int *off_count = &sbi->ll_rw_offset_entry_count;
1692         int *process_count = &sbi->ll_offset_process_count;
1693         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1694
1695         if(!sbi->ll_rw_stats_on)
1696                 return;
1697         process = sbi->ll_rw_process_info;
1698         offset = sbi->ll_rw_offset_info;
1699
1700         spin_lock(&sbi->ll_pp_extent_lock);
1701         /* Extent statistics */
1702         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1703                 if(io_extents->pp_extents[i].pid == pid) {
1704                         cur = i;
1705                         break;
1706                 }
1707         }
1708
1709         if (cur == -1) {
1710                 /* new process */
1711                 sbi->ll_extent_process_count =
1712                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
1713                 cur = sbi->ll_extent_process_count;
1714                 io_extents->pp_extents[cur].pid = pid;
1715                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
1716                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
1717         }
1718
1719         for(i = 0; (count >= (1 << LL_HIST_START << i)) &&
1720              (i < (LL_HIST_MAX - 1)); i++);
1721         if (rw == 0) {
1722                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
1723                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
1724         } else {
1725                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
1726                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
1727         }
1728         spin_unlock(&sbi->ll_pp_extent_lock);
1729
1730         spin_lock(&sbi->ll_process_lock);
1731         /* Offset statistics */
1732         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1733                 if (process[i].rw_pid == pid) {
1734                         if (process[i].rw_last_file != file) {
1735                                 process[i].rw_range_start = pos;
1736                                 process[i].rw_last_file_pos = pos + count;
1737                                 process[i].rw_smallest_extent = count;
1738                                 process[i].rw_largest_extent = count;
1739                                 process[i].rw_offset = 0;
1740                                 process[i].rw_last_file = file;
1741                                 spin_unlock(&sbi->ll_process_lock);
1742                                 return;
1743                         }
1744                         if (process[i].rw_last_file_pos != pos) {
1745                                 *off_count =
1746                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
1747                                 offset[*off_count].rw_op = process[i].rw_op;
1748                                 offset[*off_count].rw_pid = pid;
1749                                 offset[*off_count].rw_range_start =
1750                                         process[i].rw_range_start;
1751                                 offset[*off_count].rw_range_end =
1752                                         process[i].rw_last_file_pos;
1753                                 offset[*off_count].rw_smallest_extent =
1754                                         process[i].rw_smallest_extent;
1755                                 offset[*off_count].rw_largest_extent =
1756                                         process[i].rw_largest_extent;
1757                                 offset[*off_count].rw_offset =
1758                                         process[i].rw_offset;
1759                                 process[i].rw_op = rw;
1760                                 process[i].rw_range_start = pos;
1761                                 process[i].rw_smallest_extent = count;
1762                                 process[i].rw_largest_extent = count;
1763                                 process[i].rw_offset = pos -
1764                                         process[i].rw_last_file_pos;
1765                         }
1766                         if(process[i].rw_smallest_extent > count)
1767                                 process[i].rw_smallest_extent = count;
1768                         if(process[i].rw_largest_extent < count)
1769                                 process[i].rw_largest_extent = count;
1770                         process[i].rw_last_file_pos = pos + count;
1771                         spin_unlock(&sbi->ll_process_lock);
1772                         return;
1773                 }
1774         }
1775         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
1776         process[*process_count].rw_pid = pid;
1777         process[*process_count].rw_op = rw;
1778         process[*process_count].rw_range_start = pos;
1779         process[*process_count].rw_last_file_pos = pos + count;
1780         process[*process_count].rw_smallest_extent = count;
1781         process[*process_count].rw_largest_extent = count;
1782         process[*process_count].rw_offset = 0;
1783         process[*process_count].rw_last_file = file;
1784         spin_unlock(&sbi->ll_process_lock);
1785 }
1786
1787 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
1788 {
1789         struct timespec64 now;
1790         struct ll_sb_info *sbi = seq->private;
1791         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
1792         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
1793         int i;
1794
1795         ktime_get_real_ts64(&now);
1796
1797         if (!sbi->ll_rw_stats_on) {
1798                 seq_puts(seq, "disabled\n write anything to this file to activate, then '0' or 'disable' to deactivate\n");
1799                 return 0;
1800         }
1801         spin_lock(&sbi->ll_process_lock);
1802
1803         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
1804                    (s64)now.tv_sec, now.tv_nsec);
1805         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
1806                    "R/W", "PID", "RANGE START", "RANGE END",
1807                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
1808
1809         /* We stored the discontiguous offsets here; print them first */
1810         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
1811                 if (offset[i].rw_pid != 0)
1812                         seq_printf(seq,
1813                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1814                                    offset[i].rw_op == READ ? 'R' : 'W',
1815                                    offset[i].rw_pid,
1816                                    offset[i].rw_range_start,
1817                                    offset[i].rw_range_end,
1818                                    (unsigned long)offset[i].rw_smallest_extent,
1819                                    (unsigned long)offset[i].rw_largest_extent,
1820                                    offset[i].rw_offset);
1821         }
1822
1823         /* Then print the current offsets for each process */
1824         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
1825                 if (process[i].rw_pid != 0)
1826                         seq_printf(seq,
1827                                    "%3c %10d %14Lu %14Lu %17lu %17lu %14Lu",
1828                                    process[i].rw_op == READ ? 'R' : 'W',
1829                                    process[i].rw_pid,
1830                                    process[i].rw_range_start,
1831                                    process[i].rw_last_file_pos,
1832                                    (unsigned long)process[i].rw_smallest_extent,
1833                                    (unsigned long)process[i].rw_largest_extent,
1834                                    process[i].rw_offset);
1835         }
1836         spin_unlock(&sbi->ll_process_lock);
1837
1838         return 0;
1839 }
1840
1841 static ssize_t ll_rw_offset_stats_seq_write(struct file *file,
1842                                             const char __user *buf,
1843                                             size_t len, loff_t *off)
1844 {
1845         struct seq_file *seq = file->private_data;
1846         struct ll_sb_info *sbi = seq->private;
1847         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
1848         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
1849         __s64 value;
1850
1851         if (len == 0)
1852                 return -EINVAL;
1853
1854         value = ll_stats_pid_write(buf, len);
1855
1856         if (value == 0)
1857                 sbi->ll_rw_stats_on = 0;
1858         else
1859                 sbi->ll_rw_stats_on = 1;
1860
1861         spin_lock(&sbi->ll_process_lock);
1862         sbi->ll_offset_process_count = 0;
1863         sbi->ll_rw_offset_entry_count = 0;
1864         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
1865                LL_PROCESS_HIST_MAX);
1866         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
1867                LL_OFFSET_HIST_MAX);
1868         spin_unlock(&sbi->ll_process_lock);
1869
1870         return len;
1871 }
1872
1873 /**
1874  * ll_stats_pid_write() - Determine if stats collection should be enabled
1875  * @buf: Buffer containing the data written
1876  * @len: Number of bytes in the buffer
1877  *
1878  * Several proc files begin collecting stats when a value is written, and stop
1879  * collecting when either '0' or 'disable' is written. This function checks the
1880  * written value to see if collection should be enabled or disabled.
1881  *
1882  * Return: If '0' or 'disable' is provided, 0 is returned. If the text
1883  * equivalent of a number is written, that number is returned. Otherwise,
1884  * 1 is returned. Non-zero return values indicate collection should be enabled.
1885  */
1886 static __s64 ll_stats_pid_write(const char __user *buf, size_t len)
1887 {
1888         unsigned long long value = 1;
1889         int rc;
1890         char kernbuf[16];
1891
1892         rc = kstrtoull_from_user(buf, len, 0, &value);
1893         if (rc < 0 && len < sizeof(kernbuf)) {
1894
1895                 if (copy_from_user(kernbuf, buf, len))
1896                         return -EFAULT;
1897                 kernbuf[len] = 0;
1898
1899                 if (kernbuf[len - 1] == '\n')
1900                         kernbuf[len - 1] = 0;
1901
1902                 if (strncasecmp(kernbuf, "disable", 7) == 0)
1903                         value = 0;
1904         }
1905
1906         return value;
1907 }
1908
1909 LPROC_SEQ_FOPS(ll_rw_offset_stats);
1910 #endif /* CONFIG_PROC_FS */