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