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