Whamcloud - gitweb
LU-13799 llite: Adjust dio refcounting
[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  */
31 #define DEBUG_SUBSYSTEM S_LLITE
32
33 #include <linux/version.h>
34 #include <linux/user_namespace.h>
35 #include <linux/uidgid.h>
36
37 #include <uapi/linux/lustre/lustre_param.h>
38 #include <lprocfs_status.h>
39 #include <obd_support.h>
40
41 #include "llite_internal.h"
42 #include "vvp_internal.h"
43
44 static struct kobject *llite_kobj;
45 static struct dentry *llite_root;
46
47 static void llite_kobj_release(struct kobject *kobj)
48 {
49         if (!IS_ERR_OR_NULL(llite_root)) {
50                 debugfs_remove(llite_root);
51                 llite_root = NULL;
52         }
53
54         kfree(kobj);
55 }
56
57 static struct kobj_type llite_kobj_ktype = {
58         .release        = llite_kobj_release,
59         .sysfs_ops      = &lustre_sysfs_ops,
60 };
61
62 int llite_tunables_register(void)
63 {
64         int rc;
65
66         llite_kobj = kzalloc(sizeof(*llite_kobj), GFP_KERNEL);
67         if (!llite_kobj)
68                 return -ENOMEM;
69
70         llite_kobj->kset = lustre_kset;
71         rc = kobject_init_and_add(llite_kobj, &llite_kobj_ktype,
72                                   &lustre_kset->kobj, "%s", "llite");
73         if (rc)
74                 goto free_kobj;
75
76         llite_root = debugfs_create_dir("llite", debugfs_lustre_root);
77         return 0;
78
79 free_kobj:
80         kobject_put(llite_kobj);
81         llite_kobj = NULL;
82
83         return rc;
84 }
85
86 void llite_tunables_unregister(void)
87 {
88         kobject_put(llite_kobj);
89         llite_kobj = NULL;
90 }
91
92 /* <debugfs>/lustre/llite mount point registration */
93 static const struct file_operations ll_rw_extents_stats_fops;
94 static const struct file_operations ll_rw_extents_stats_pp_fops;
95 static const struct file_operations ll_rw_offset_stats_fops;
96
97 /**
98  * ll_stats_pid_write() - Determine if stats collection should be enabled
99  * @buf: Buffer containing the data written
100  * @len: Number of bytes in the buffer
101  *
102  * Several proc files begin collecting stats when a value is written, and stop
103  * collecting when either '0' or 'disable' is written. This function checks the
104  * written value to see if collection should be enabled or disabled.
105  *
106  * Return: If '0' or 'disable' is provided, 0 is returned. If the text
107  * equivalent of a number is written, that number is returned. Otherwise,
108  * 1 is returned. Non-zero return values indicate collection should be enabled.
109  */
110 static s64 ll_stats_pid_write(const char __user *buf, size_t len)
111 {
112         unsigned long long value = 1;
113         char kernbuf[16];
114         int rc;
115
116         rc = kstrtoull_from_user(buf, len, 0, &value);
117         if (rc < 0 && len < sizeof(kernbuf)) {
118                 if (copy_from_user(kernbuf, buf, len))
119                         return -EFAULT;
120                 kernbuf[len] = 0;
121
122                 if (kernbuf[len - 1] == '\n')
123                         kernbuf[len - 1] = 0;
124
125                 if (strncasecmp(kernbuf, "disable", 7) == 0)
126                         value = 0;
127         }
128
129         return value;
130 }
131
132 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
133                               char *buf)
134 {
135         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
136                                               ll_kset.kobj);
137         struct obd_statfs osfs;
138         int rc;
139
140         rc = ll_statfs_internal(sbi, &osfs, OBD_STATFS_NODELAY);
141         if (rc)
142                 return rc;
143
144         return sprintf(buf, "%u\n", osfs.os_bsize);
145 }
146 LUSTRE_RO_ATTR(blocksize);
147
148 static ssize_t stat_blocksize_show(struct kobject *kobj, struct attribute *attr,
149                                    char *buf)
150 {
151         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
152                                               ll_kset.kobj);
153
154         return sprintf(buf, "%u\n", sbi->ll_stat_blksize);
155 }
156
157 static ssize_t stat_blocksize_store(struct kobject *kobj,
158                                     struct attribute *attr,
159                                     const char *buffer,
160                                     size_t count)
161 {
162         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
163                                               ll_kset.kobj);
164         unsigned int val;
165         int rc;
166
167         rc = kstrtouint(buffer, 10, &val);
168         if (rc)
169                 return rc;
170
171         if (val != 0 && (val < PAGE_SIZE || (val & (val - 1))) != 0)
172                 return -ERANGE;
173
174         sbi->ll_stat_blksize = val;
175
176         return count;
177 }
178 LUSTRE_RW_ATTR(stat_blocksize);
179
180 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
181                                 char *buf)
182 {
183         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
184                                               ll_kset.kobj);
185         struct obd_statfs osfs;
186         u32 blk_size;
187         u64 result;
188         int rc;
189
190         rc = ll_statfs_internal(sbi, &osfs, OBD_STATFS_NODELAY);
191         if (rc)
192                 return rc;
193
194         blk_size = osfs.os_bsize >> 10;
195         result = osfs.os_blocks;
196
197         while (blk_size >>= 1)
198                 result <<= 1;
199
200         return sprintf(buf, "%llu\n", result);
201 }
202 LUSTRE_RO_ATTR(kbytestotal);
203
204 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
205                                char *buf)
206 {
207         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
208                                               ll_kset.kobj);
209         struct obd_statfs osfs;
210         u32 blk_size;
211         u64 result;
212         int rc;
213
214         rc = ll_statfs_internal(sbi, &osfs, OBD_STATFS_NODELAY);
215         if (rc)
216                 return rc;
217
218         blk_size = osfs.os_bsize >> 10;
219         result = osfs.os_bfree;
220
221         while (blk_size >>= 1)
222                 result <<= 1;
223
224         return sprintf(buf, "%llu\n", result);
225 }
226 LUSTRE_RO_ATTR(kbytesfree);
227
228 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
229                                 char *buf)
230 {
231         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
232                                               ll_kset.kobj);
233         struct obd_statfs osfs;
234         u32 blk_size;
235         u64 result;
236         int rc;
237
238         rc = ll_statfs_internal(sbi, &osfs, OBD_STATFS_NODELAY);
239         if (rc)
240                 return rc;
241
242         blk_size = osfs.os_bsize >> 10;
243         result = osfs.os_bavail;
244
245         while (blk_size >>= 1)
246                 result <<= 1;
247
248         return sprintf(buf, "%llu\n", result);
249 }
250 LUSTRE_RO_ATTR(kbytesavail);
251
252 static ssize_t filestotal_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         struct obd_statfs osfs;
258         int rc;
259
260         rc = ll_statfs_internal(sbi, &osfs, OBD_STATFS_NODELAY);
261         if (rc)
262                 return rc;
263
264         return sprintf(buf, "%llu\n", osfs.os_files);
265 }
266 LUSTRE_RO_ATTR(filestotal);
267
268 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
269                               char *buf)
270 {
271         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
272                                               ll_kset.kobj);
273         struct obd_statfs osfs;
274         int rc;
275
276         rc = ll_statfs_internal(sbi, &osfs, OBD_STATFS_NODELAY);
277         if (rc)
278                 return rc;
279
280         return sprintf(buf, "%llu\n", osfs.os_ffree);
281 }
282 LUSTRE_RO_ATTR(filesfree);
283
284 static ssize_t client_type_show(struct kobject *kobj, struct attribute *attr,
285                                 char *buf)
286 {
287         return sprintf(buf, "local client\n");
288 }
289 LUSTRE_RO_ATTR(client_type);
290
291 LUSTRE_RW_ATTR(foreign_symlink_enable);
292
293 LUSTRE_RW_ATTR(foreign_symlink_prefix);
294
295 LUSTRE_RW_ATTR(foreign_symlink_upcall);
296
297 LUSTRE_WO_ATTR(foreign_symlink_upcall_info);
298
299 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
300                            char *buf)
301 {
302         return sprintf(buf, "lustre\n");
303 }
304 LUSTRE_RO_ATTR(fstype);
305
306 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
307                          char *buf)
308 {
309         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
310                                               ll_kset.kobj);
311
312         return sprintf(buf, "%s\n", sbi->ll_sb_uuid.uuid);
313 }
314 LUSTRE_RO_ATTR(uuid);
315
316 static int ll_site_stats_seq_show(struct seq_file *m, void *v)
317 {
318         struct super_block *sb = m->private;
319
320         /*
321          * See description of statistical counters in struct cl_site, and
322          * struct lu_site.
323          */
324         return cl_site_stats_print(lu2cl_site(ll_s2sbi(sb)->ll_site), m);
325 }
326
327 LDEBUGFS_SEQ_FOPS_RO(ll_site_stats);
328
329 static ssize_t max_read_ahead_mb_show(struct kobject *kobj,
330                                       struct attribute *attr, char *buf)
331 {
332         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
333                                               ll_kset.kobj);
334
335         return scnprintf(buf, PAGE_SIZE, "%lu\n",
336                         PAGES_TO_MiB(sbi->ll_ra_info.ra_max_pages));
337 }
338
339 static ssize_t max_read_ahead_mb_store(struct kobject *kobj,
340                                        struct attribute *attr,
341                                        const char *buffer, size_t count)
342 {
343         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
344                                               ll_kset.kobj);
345         u64 ra_max_mb, pages_number;
346         int rc;
347
348         rc = sysfs_memparse(buffer, count, &ra_max_mb, "MiB");
349         if (rc)
350                 return rc;
351
352         pages_number = round_up(ra_max_mb, 1024 * 1024) >> PAGE_SHIFT;
353         CDEBUG(D_INFO, "%s: set max_read_ahead_mb=%llu (%llu pages)\n",
354                sbi->ll_fsname, PAGES_TO_MiB(pages_number), pages_number);
355         if (pages_number > cfs_totalram_pages() / 2) {
356                 /* 1/2 of RAM */
357                 CERROR("%s: cannot set max_read_ahead_mb=%llu > totalram/2=%luMB\n",
358                        sbi->ll_fsname, PAGES_TO_MiB(pages_number),
359                        PAGES_TO_MiB(cfs_totalram_pages() / 2));
360                 return -ERANGE;
361         }
362
363         spin_lock(&sbi->ll_lock);
364         sbi->ll_ra_info.ra_max_pages = pages_number;
365         spin_unlock(&sbi->ll_lock);
366
367         return count;
368 }
369 LUSTRE_RW_ATTR(max_read_ahead_mb);
370
371 static ssize_t max_read_ahead_per_file_mb_show(struct kobject *kobj,
372                                                struct attribute *attr,
373                                                char *buf)
374 {
375         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
376                                               ll_kset.kobj);
377
378         return scnprintf(buf, PAGE_SIZE, "%lu\n",
379                          PAGES_TO_MiB(sbi->ll_ra_info.ra_max_pages_per_file));
380 }
381
382 static ssize_t max_read_ahead_per_file_mb_store(struct kobject *kobj,
383                                                 struct attribute *attr,
384                                                 const char *buffer,
385                                                 size_t count)
386 {
387         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
388                                               ll_kset.kobj);
389         u64 ra_max_file_mb, pages_number;
390         int rc;
391
392         rc = sysfs_memparse(buffer, count, &ra_max_file_mb, "MiB");
393         if (rc)
394                 return rc;
395
396         pages_number = round_up(ra_max_file_mb, 1024 * 1024) >> PAGE_SHIFT;
397         if (pages_number > sbi->ll_ra_info.ra_max_pages) {
398                 CERROR("%s: cannot set max_read_ahead_per_file_mb=%llu > max_read_ahead_mb=%lu\n",
399                        sbi->ll_fsname, PAGES_TO_MiB(pages_number),
400                        PAGES_TO_MiB(sbi->ll_ra_info.ra_max_pages));
401                 return -ERANGE;
402         }
403
404         spin_lock(&sbi->ll_lock);
405         sbi->ll_ra_info.ra_max_pages_per_file = pages_number;
406         spin_unlock(&sbi->ll_lock);
407
408         return count;
409 }
410 LUSTRE_RW_ATTR(max_read_ahead_per_file_mb);
411
412 static ssize_t max_read_ahead_whole_mb_show(struct kobject *kobj,
413                                             struct attribute *attr, char *buf)
414 {
415         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
416                                               ll_kset.kobj);
417
418         return scnprintf(buf, PAGE_SIZE, "%lu\n",
419                          PAGES_TO_MiB(sbi->ll_ra_info.ra_max_read_ahead_whole_pages));
420 }
421
422 static ssize_t max_read_ahead_whole_mb_store(struct kobject *kobj,
423                                              struct attribute *attr,
424                                              const char *buffer, size_t count)
425 {
426         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
427                                               ll_kset.kobj);
428         u64 ra_max_whole_mb, pages_number;
429         int rc;
430
431         rc = sysfs_memparse(buffer, count, &ra_max_whole_mb, "MiB");
432         if (rc)
433                 return rc;
434
435         pages_number = round_up(ra_max_whole_mb, 1024 * 1024) >> PAGE_SHIFT;
436         /* Cap this at the current max readahead window size, the readahead
437          * algorithm does this anyway so it's pointless to set it larger.
438          */
439         if (pages_number > sbi->ll_ra_info.ra_max_pages_per_file) {
440                 CERROR("%s: cannot set max_read_ahead_whole_mb=%llu > max_read_ahead_per_file_mb=%lu\n",
441                        sbi->ll_fsname, PAGES_TO_MiB(pages_number),
442                        PAGES_TO_MiB(sbi->ll_ra_info.ra_max_pages_per_file));
443
444                 return -ERANGE;
445         }
446
447         spin_lock(&sbi->ll_lock);
448         sbi->ll_ra_info.ra_max_read_ahead_whole_pages = pages_number;
449         spin_unlock(&sbi->ll_lock);
450
451         return count;
452 }
453 LUSTRE_RW_ATTR(max_read_ahead_whole_mb);
454
455 static int ll_max_cached_mb_seq_show(struct seq_file *m, void *v)
456 {
457         struct super_block     *sb    = m->private;
458         struct ll_sb_info      *sbi   = ll_s2sbi(sb);
459         struct cl_client_cache *cache = sbi->ll_cache;
460         struct ll_ra_info *ra = &sbi->ll_ra_info;
461         long max_cached_mb;
462         long unused_mb;
463
464         mutex_lock(&cache->ccc_max_cache_mb_lock);
465         max_cached_mb = PAGES_TO_MiB(cache->ccc_lru_max);
466         unused_mb = PAGES_TO_MiB(atomic_long_read(&cache->ccc_lru_left));
467         mutex_unlock(&cache->ccc_max_cache_mb_lock);
468
469         seq_printf(m, "users: %d\n"
470                       "max_cached_mb: %ld\n"
471                       "used_mb: %ld\n"
472                       "unused_mb: %ld\n"
473                       "reclaim_count: %u\n"
474                       "max_read_ahead_mb: %lu\n"
475                       "used_read_ahead_mb: %d\n",
476                    atomic_read(&cache->ccc_users),
477                    max_cached_mb,
478                    max_cached_mb - unused_mb,
479                    unused_mb,
480                    cache->ccc_lru_shrinkers,
481                    PAGES_TO_MiB(ra->ra_max_pages),
482                    PAGES_TO_MiB(atomic_read(&ra->ra_cur_pages)));
483         return 0;
484 }
485
486 static ssize_t ll_max_cached_mb_seq_write(struct file *file,
487                                           const char __user *buffer,
488                                           size_t count, loff_t *off)
489 {
490         struct seq_file *m = file->private_data;
491         struct super_block *sb = m->private;
492         struct ll_sb_info *sbi = ll_s2sbi(sb);
493         struct cl_client_cache *cache = sbi->ll_cache;
494         struct lu_env *env;
495         long diff = 0;
496         long nrpages = 0;
497         __u16 refcheck;
498         u64 pages_number;
499         int rc;
500         char kernbuf[128], *ptr;
501
502         ENTRY;
503         if (count >= sizeof(kernbuf))
504                 RETURN(-EINVAL);
505
506         if (copy_from_user(kernbuf, buffer, count))
507                 RETURN(-EFAULT);
508         kernbuf[count] = '\0';
509
510         ptr = lprocfs_find_named_value(kernbuf, "max_cached_mb:", &count);
511         rc = sysfs_memparse(ptr, count, &pages_number, "MiB");
512         if (rc)
513                 RETURN(rc);
514
515         pages_number >>= PAGE_SHIFT;
516
517         if (pages_number < 0 || pages_number > cfs_totalram_pages()) {
518                 CERROR("%s: can't set max cache more than %lu MB\n",
519                        sbi->ll_fsname,
520                        PAGES_TO_MiB(cfs_totalram_pages()));
521                 RETURN(-ERANGE);
522         }
523         /* Allow enough cache so clients can make well-formed RPCs */
524         pages_number = max_t(long, pages_number, PTLRPC_MAX_BRW_PAGES);
525
526         mutex_lock(&cache->ccc_max_cache_mb_lock);
527         diff = pages_number - cache->ccc_lru_max;
528
529         /* easy - add more LRU slots. */
530         if (diff >= 0) {
531                 atomic_long_add(diff, &cache->ccc_lru_left);
532                 GOTO(out, rc = 0);
533         }
534
535         env = cl_env_get(&refcheck);
536         if (IS_ERR(env))
537                 GOTO(out_unlock, rc = PTR_ERR(env));
538
539         diff = -diff;
540         while (diff > 0) {
541                 long tmp;
542
543                 /* reduce LRU budget from free slots. */
544                 do {
545                         long lru_left_old, lru_left_new, lru_left_ret;
546
547                         lru_left_old = atomic_long_read(&cache->ccc_lru_left);
548                         if (lru_left_old == 0)
549                                 break;
550
551                         lru_left_new = lru_left_old > diff ?
552                                         lru_left_old - diff : 0;
553                         lru_left_ret =
554                                 atomic_long_cmpxchg(&cache->ccc_lru_left,
555                                                     lru_left_old,
556                                                     lru_left_new);
557                         if (likely(lru_left_old == lru_left_ret)) {
558                                 diff -= lru_left_old - lru_left_new;
559                                 nrpages += lru_left_old - lru_left_new;
560                                 break;
561                         }
562                 } while (1);
563
564                 if (diff <= 0)
565                         break;
566
567                 if (sbi->ll_dt_exp == NULL) { /* being initialized */
568                         rc = -ENODEV;
569                         break;
570                 }
571
572                 /* Request extra free slots to avoid them all being used
573                  * by other processes before this can continue shrinking.
574                  */
575                 tmp = diff + min_t(long, diff, MiB_TO_PAGES(1024));
576                 /* difficult - have to ask OSCs to drop LRU slots. */
577                 rc = obd_set_info_async(env, sbi->ll_dt_exp,
578                                 sizeof(KEY_CACHE_LRU_SHRINK),
579                                 KEY_CACHE_LRU_SHRINK,
580                                 sizeof(tmp), &tmp, NULL);
581                 if (rc < 0)
582                         break;
583         }
584         cl_env_put(env, &refcheck);
585
586 out:
587         if (rc >= 0) {
588                 cache->ccc_lru_max = pages_number;
589                 rc = count;
590         } else {
591                 atomic_long_add(nrpages, &cache->ccc_lru_left);
592         }
593 out_unlock:
594         mutex_unlock(&cache->ccc_max_cache_mb_lock);
595         return rc;
596 }
597 LDEBUGFS_SEQ_FOPS(ll_max_cached_mb);
598
599 static ssize_t checksums_show(struct kobject *kobj, struct attribute *attr,
600                               char *buf)
601 {
602         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
603                                               ll_kset.kobj);
604
605         return sprintf(buf, "%u\n", (sbi->ll_flags & LL_SBI_CHECKSUM) ? 1 : 0);
606 }
607
608 static ssize_t checksums_store(struct kobject *kobj, struct attribute *attr,
609                                const char *buffer, size_t count)
610 {
611         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
612                                               ll_kset.kobj);
613         bool val;
614         int tmp;
615         int rc;
616
617         if (!sbi->ll_dt_exp)
618                 /* Not set up yet */
619                 return -EAGAIN;
620
621         rc = kstrtobool(buffer, &val);
622         if (rc)
623                 return rc;
624         if (val)
625                 sbi->ll_flags |= LL_SBI_CHECKSUM;
626         else
627                 sbi->ll_flags &= ~LL_SBI_CHECKSUM;
628         tmp = val;
629
630         rc = obd_set_info_async(NULL, sbi->ll_dt_exp, sizeof(KEY_CHECKSUM),
631                                 KEY_CHECKSUM, sizeof(tmp), &tmp, NULL);
632         if (rc)
633                 CWARN("Failed to set OSC checksum flags: %d\n", rc);
634
635         return count;
636 }
637 LUSTRE_RW_ATTR(checksums);
638
639 LUSTRE_ATTR(checksum_pages, 0644, checksums_show, checksums_store);
640
641 static ssize_t ll_rd_track_id(struct kobject *kobj, char *buf,
642                               enum stats_track_type type)
643 {
644         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
645                                               ll_kset.kobj);
646
647         if (sbi->ll_stats_track_type == type)
648                 return sprintf(buf, "%d\n", sbi->ll_stats_track_id);
649         else if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
650                 return sprintf(buf, "0 (all)\n");
651
652         return sprintf(buf, "untracked\n");
653 }
654
655 static ssize_t ll_wr_track_id(struct kobject *kobj, const char *buffer,
656                               size_t count, enum stats_track_type type)
657 {
658         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
659                                               ll_kset.kobj);
660         unsigned long pid;
661         int rc;
662
663         rc = kstrtoul(buffer, 10, &pid);
664         if (rc)
665                 return rc;
666
667         sbi->ll_stats_track_id = pid;
668         if (pid == 0)
669                 sbi->ll_stats_track_type = STATS_TRACK_ALL;
670         else
671                 sbi->ll_stats_track_type = type;
672         lprocfs_clear_stats(sbi->ll_stats);
673         return count;
674 }
675
676 static ssize_t stats_track_pid_show(struct kobject *kobj,
677                                     struct attribute *attr,
678                                     char *buf)
679 {
680         return ll_rd_track_id(kobj, buf, STATS_TRACK_PID);
681 }
682
683 static ssize_t stats_track_pid_store(struct kobject *kobj,
684                                      struct attribute *attr,
685                                      const char *buffer,
686                                      size_t count)
687 {
688         return ll_wr_track_id(kobj, buffer, count, STATS_TRACK_PID);
689 }
690 LUSTRE_RW_ATTR(stats_track_pid);
691
692 static ssize_t stats_track_ppid_show(struct kobject *kobj,
693                                      struct attribute *attr,
694                                      char *buf)
695 {
696         return ll_rd_track_id(kobj, buf, STATS_TRACK_PPID);
697 }
698
699 static ssize_t stats_track_ppid_store(struct kobject *kobj,
700                                       struct attribute *attr,
701                                       const char *buffer,
702                                       size_t count)
703 {
704         return ll_wr_track_id(kobj, buffer, count, STATS_TRACK_PPID);
705 }
706 LUSTRE_RW_ATTR(stats_track_ppid);
707
708 static ssize_t stats_track_gid_show(struct kobject *kobj,
709                                     struct attribute *attr,
710                                     char *buf)
711 {
712         return ll_rd_track_id(kobj, buf, STATS_TRACK_GID);
713 }
714
715 static ssize_t stats_track_gid_store(struct kobject *kobj,
716                                      struct attribute *attr,
717                                      const char *buffer,
718                                      size_t count)
719 {
720         return ll_wr_track_id(kobj, buffer, count, STATS_TRACK_GID);
721 }
722 LUSTRE_RW_ATTR(stats_track_gid);
723
724 static ssize_t statahead_running_max_show(struct kobject *kobj,
725                                           struct attribute *attr,
726                                           char *buf)
727 {
728         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
729                                               ll_kset.kobj);
730
731         return scnprintf(buf, PAGE_SIZE, "%u\n", sbi->ll_sa_running_max);
732 }
733
734 static ssize_t statahead_running_max_store(struct kobject *kobj,
735                                            struct attribute *attr,
736                                            const char *buffer,
737                                            size_t count)
738 {
739         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
740                                               ll_kset.kobj);
741         unsigned long val;
742         int rc;
743
744         rc = kstrtoul(buffer, 0, &val);
745         if (rc)
746                 return rc;
747
748         if (val <= LL_SA_RUNNING_MAX) {
749                 sbi->ll_sa_running_max = val;
750                 return count;
751         }
752
753         CERROR("Bad statahead_running_max value %lu. Valid values "
754                "are in the range [0, %d]\n", val, LL_SA_RUNNING_MAX);
755
756         return -ERANGE;
757 }
758 LUSTRE_RW_ATTR(statahead_running_max);
759
760 static ssize_t statahead_max_show(struct kobject *kobj,
761                                   struct attribute *attr,
762                                   char *buf)
763 {
764         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
765                                               ll_kset.kobj);
766
767         return sprintf(buf, "%u\n", sbi->ll_sa_max);
768 }
769
770 static ssize_t statahead_max_store(struct kobject *kobj,
771                                    struct attribute *attr,
772                                    const char *buffer,
773                                    size_t count)
774 {
775         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
776                                               ll_kset.kobj);
777         unsigned long val;
778         int rc;
779
780         rc = kstrtoul(buffer, 0, &val);
781         if (rc)
782                 return rc;
783
784         if (val <= LL_SA_RPC_MAX)
785                 sbi->ll_sa_max = val;
786         else
787                 CERROR("Bad statahead_max value %lu. Valid values are in the range [0, %d]\n",
788                        val, LL_SA_RPC_MAX);
789
790         return count;
791 }
792 LUSTRE_RW_ATTR(statahead_max);
793
794 static ssize_t statahead_agl_show(struct kobject *kobj,
795                                   struct attribute *attr,
796                                   char *buf)
797 {
798         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
799                                               ll_kset.kobj);
800
801         return sprintf(buf, "%u\n", sbi->ll_flags & LL_SBI_AGL_ENABLED ? 1 : 0);
802 }
803
804 static ssize_t statahead_agl_store(struct kobject *kobj,
805                                    struct attribute *attr,
806                                    const char *buffer,
807                                    size_t count)
808 {
809         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
810                                               ll_kset.kobj);
811         bool val;
812         int rc;
813
814         rc = kstrtobool(buffer, &val);
815         if (rc)
816                 return rc;
817
818         if (val)
819                 sbi->ll_flags |= LL_SBI_AGL_ENABLED;
820         else
821                 sbi->ll_flags &= ~LL_SBI_AGL_ENABLED;
822
823         return count;
824 }
825 LUSTRE_RW_ATTR(statahead_agl);
826
827 static int ll_statahead_stats_seq_show(struct seq_file *m, void *v)
828 {
829         struct super_block *sb = m->private;
830         struct ll_sb_info *sbi = ll_s2sbi(sb);
831
832         seq_printf(m, "statahead total: %u\n"
833                       "statahead wrong: %u\n"
834                       "agl total: %u\n",
835                    atomic_read(&sbi->ll_sa_total),
836                    atomic_read(&sbi->ll_sa_wrong),
837                    atomic_read(&sbi->ll_agl_total));
838         return 0;
839 }
840
841 LDEBUGFS_SEQ_FOPS_RO(ll_statahead_stats);
842
843 static ssize_t lazystatfs_show(struct kobject *kobj,
844                                struct attribute *attr,
845                                char *buf)
846 {
847         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
848                                               ll_kset.kobj);
849
850         return sprintf(buf, "%u\n", (sbi->ll_flags & LL_SBI_LAZYSTATFS) ? 1 : 0);
851 }
852
853 static ssize_t lazystatfs_store(struct kobject *kobj,
854                                 struct attribute *attr,
855                                 const char *buffer,
856                                 size_t count)
857 {
858         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
859                                               ll_kset.kobj);
860         bool val;
861         int rc;
862
863         rc = kstrtobool(buffer, &val);
864         if (rc)
865                 return rc;
866
867         if (val)
868                 sbi->ll_flags |= LL_SBI_LAZYSTATFS;
869         else
870                 sbi->ll_flags &= ~LL_SBI_LAZYSTATFS;
871
872         return count;
873 }
874 LUSTRE_RW_ATTR(lazystatfs);
875
876 static ssize_t statfs_max_age_show(struct kobject *kobj, struct attribute *attr,
877                                    char *buf)
878 {
879         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
880                                               ll_kset.kobj);
881
882         return scnprintf(buf, PAGE_SIZE, "%u\n", sbi->ll_statfs_max_age);
883 }
884
885 static ssize_t statfs_max_age_store(struct kobject *kobj,
886                                     struct attribute *attr, const char *buffer,
887                                     size_t count)
888 {
889         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
890                                               ll_kset.kobj);
891         unsigned int val;
892         int rc;
893
894         rc = kstrtouint(buffer, 10, &val);
895         if (rc)
896                 return rc;
897         if (val > OBD_STATFS_CACHE_MAX_AGE)
898                 return -EINVAL;
899
900         sbi->ll_statfs_max_age = val;
901
902         return count;
903 }
904 LUSTRE_RW_ATTR(statfs_max_age);
905
906 static ssize_t max_easize_show(struct kobject *kobj,
907                                struct attribute *attr,
908                                char *buf)
909 {
910         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
911                                               ll_kset.kobj);
912         unsigned int ealen;
913         int rc;
914
915         rc = ll_get_max_mdsize(sbi, &ealen);
916         if (rc)
917                 return rc;
918
919         /* Limit xattr size returned to userspace based on kernel maximum */
920         return scnprintf(buf, PAGE_SIZE, "%u\n",
921                          ealen > XATTR_SIZE_MAX ? XATTR_SIZE_MAX : ealen);
922 }
923 LUSTRE_RO_ATTR(max_easize);
924
925 /**
926  * Get default_easize.
927  *
928  * \see client_obd::cl_default_mds_easize
929  *
930  * \param[in] m         seq_file handle
931  * \param[in] v         unused for single entry
932  *
933  * \retval 0            on success
934  * \retval negative     negated errno on failure
935  */
936 static ssize_t default_easize_show(struct kobject *kobj,
937                                    struct attribute *attr,
938                                    char *buf)
939 {
940         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
941                                               ll_kset.kobj);
942         unsigned int ealen;
943         int rc;
944
945         rc = ll_get_default_mdsize(sbi, &ealen);
946         if (rc)
947                 return rc;
948
949         /* Limit xattr size returned to userspace based on kernel maximum */
950         return scnprintf(buf, PAGE_SIZE, "%u\n",
951                          ealen > XATTR_SIZE_MAX ? XATTR_SIZE_MAX : ealen);
952 }
953
954 /**
955  * Set default_easize.
956  *
957  * Range checking on the passed value is handled by
958  * ll_set_default_mdsize().
959  *
960  * \see client_obd::cl_default_mds_easize
961  *
962  * \param[in] file      proc file
963  * \param[in] buffer    string passed from user space
964  * \param[in] count     \a buffer length
965  * \param[in] off       unused for single entry
966  *
967  * \retval positive     \a count on success
968  * \retval negative     negated errno on failure
969  */
970 static ssize_t default_easize_store(struct kobject *kobj,
971                                     struct attribute *attr,
972                                     const char *buffer,
973                                     size_t count)
974 {
975         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
976                                               ll_kset.kobj);
977         unsigned int val;
978         int rc;
979
980         if (count == 0)
981                 return 0;
982
983         rc = kstrtouint(buffer, 10, &val);
984         if (rc)
985                 return rc;
986
987         rc = ll_set_default_mdsize(sbi, val);
988         if (rc)
989                 return rc;
990
991         return count;
992 }
993 LUSTRE_RW_ATTR(default_easize);
994
995 static int ll_sbi_flags_seq_show(struct seq_file *m, void *v)
996 {
997         const char *const str[] = LL_SBI_FLAGS;
998         struct super_block *sb = m->private;
999         int flags = ll_s2sbi(sb)->ll_flags;
1000         int i = 0;
1001
1002         while (flags != 0) {
1003                 if (ARRAY_SIZE(str) <= i) {
1004                         CERROR("%s: Revise array LL_SBI_FLAGS to match sbi "
1005                                 "flags please.\n", ll_s2sbi(sb)->ll_fsname);
1006                         return -EINVAL;
1007                 }
1008
1009                 if (flags & 0x1)
1010                         seq_printf(m, "%s ", str[i]);
1011                 flags >>= 1;
1012                 ++i;
1013         }
1014         seq_printf(m, "\b\n");
1015         return 0;
1016 }
1017
1018 LDEBUGFS_SEQ_FOPS_RO(ll_sbi_flags);
1019
1020 static ssize_t xattr_cache_show(struct kobject *kobj,
1021                                 struct attribute *attr,
1022                                 char *buf)
1023 {
1024         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1025                                               ll_kset.kobj);
1026
1027         return sprintf(buf, "%u\n", sbi->ll_xattr_cache_enabled);
1028 }
1029
1030 static ssize_t xattr_cache_store(struct kobject *kobj,
1031                                  struct attribute *attr,
1032                                  const char *buffer,
1033                                  size_t count)
1034 {
1035         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1036                                               ll_kset.kobj);
1037         bool val;
1038         int rc;
1039
1040         rc = kstrtobool(buffer, &val);
1041         if (rc)
1042                 return rc;
1043
1044         if (val && !(sbi->ll_flags & LL_SBI_XATTR_CACHE))
1045                 return -ENOTSUPP;
1046
1047         sbi->ll_xattr_cache_enabled = val;
1048         sbi->ll_xattr_cache_set = 1;
1049
1050         return count;
1051 }
1052 LUSTRE_RW_ATTR(xattr_cache);
1053
1054 static ssize_t tiny_write_show(struct kobject *kobj,
1055                                struct attribute *attr,
1056                                char *buf)
1057 {
1058         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1059                                               ll_kset.kobj);
1060
1061         return sprintf(buf, "%u\n", !!(sbi->ll_flags & LL_SBI_TINY_WRITE));
1062 }
1063
1064 static ssize_t tiny_write_store(struct kobject *kobj,
1065                                 struct attribute *attr,
1066                                 const char *buffer,
1067                                 size_t count)
1068 {
1069         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1070                                               ll_kset.kobj);
1071         bool val;
1072         int rc;
1073
1074         rc = kstrtobool(buffer, &val);
1075         if (rc)
1076                 return rc;
1077
1078         spin_lock(&sbi->ll_lock);
1079         if (val)
1080                 sbi->ll_flags |= LL_SBI_TINY_WRITE;
1081         else
1082                 sbi->ll_flags &= ~LL_SBI_TINY_WRITE;
1083         spin_unlock(&sbi->ll_lock);
1084
1085         return count;
1086 }
1087 LUSTRE_RW_ATTR(tiny_write);
1088
1089 static ssize_t parallel_dio_show(struct kobject *kobj,
1090                                  struct attribute *attr,
1091                                  char *buf)
1092 {
1093         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1094                                               ll_kset.kobj);
1095
1096         return snprintf(buf, PAGE_SIZE, "%u\n",
1097                        !!(sbi->ll_flags & LL_SBI_PARALLEL_DIO));
1098 }
1099
1100 static ssize_t parallel_dio_store(struct kobject *kobj,
1101                                   struct attribute *attr,
1102                                   const char *buffer,
1103                                   size_t count)
1104 {
1105         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1106                                               ll_kset.kobj);
1107         bool val;
1108         int rc;
1109
1110         rc = kstrtobool(buffer, &val);
1111         if (rc)
1112                 return rc;
1113
1114         spin_lock(&sbi->ll_lock);
1115         if (val)
1116                 sbi->ll_flags |= LL_SBI_PARALLEL_DIO;
1117         else
1118                 sbi->ll_flags &= ~LL_SBI_PARALLEL_DIO;
1119         spin_unlock(&sbi->ll_lock);
1120
1121         return count;
1122 }
1123 LUSTRE_RW_ATTR(parallel_dio);
1124
1125 static ssize_t max_read_ahead_async_active_show(struct kobject *kobj,
1126                                                struct attribute *attr,
1127                                                char *buf)
1128 {
1129         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1130                                               ll_kset.kobj);
1131
1132         return scnprintf(buf, PAGE_SIZE, "%u\n",
1133                          sbi->ll_ra_info.ra_async_max_active);
1134 }
1135
1136 static ssize_t max_read_ahead_async_active_store(struct kobject *kobj,
1137                                                  struct attribute *attr,
1138                                                  const char *buffer,
1139                                                  size_t count)
1140 {
1141         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1142                                               ll_kset.kobj);
1143         unsigned int val;
1144         int rc;
1145
1146         rc = kstrtouint(buffer, 10, &val);
1147         if (rc)
1148                 return rc;
1149
1150         /**
1151          * It doesn't make any sense to make it exceed what
1152          * workqueue could acutally support. This can easily
1153          * over subscripe the cores but Lustre internally
1154          * throttles to avoid those impacts.
1155          */
1156         if (val > WQ_UNBOUND_MAX_ACTIVE) {
1157                 CERROR("%s: cannot set max_read_ahead_async_active=%u larger than %u\n",
1158                        sbi->ll_fsname, val, WQ_UNBOUND_MAX_ACTIVE);
1159                 return -ERANGE;
1160         }
1161
1162         spin_lock(&sbi->ll_lock);
1163         sbi->ll_ra_info.ra_async_max_active = val;
1164         spin_unlock(&sbi->ll_lock);
1165
1166         return count;
1167 }
1168 LUSTRE_RW_ATTR(max_read_ahead_async_active);
1169
1170 static ssize_t read_ahead_async_file_threshold_mb_show(struct kobject *kobj,
1171                                                        struct attribute *attr,
1172                                                        char *buf)
1173 {
1174         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1175                                               ll_kset.kobj);
1176
1177         return scnprintf(buf, PAGE_SIZE, "%lu\n", PAGES_TO_MiB(
1178                          sbi->ll_ra_info.ra_async_pages_per_file_threshold));
1179 }
1180
1181 static ssize_t
1182 read_ahead_async_file_threshold_mb_store(struct kobject *kobj,
1183                                          struct attribute *attr,
1184                                          const char *buffer, size_t count)
1185 {
1186         unsigned long pages_number;
1187         unsigned long max_ra_per_file;
1188         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1189                                               ll_kset.kobj);
1190         int rc;
1191
1192         rc = kstrtoul(buffer, 10, &pages_number);
1193         if (rc)
1194                 return rc;
1195
1196         pages_number = MiB_TO_PAGES(pages_number);
1197         max_ra_per_file = sbi->ll_ra_info.ra_max_pages_per_file;
1198         if (pages_number < 0 || pages_number > max_ra_per_file) {
1199                 CERROR("%s: can't set read_ahead_async_file_threshold_mb=%lu > "
1200                        "max_read_readahead_per_file_mb=%lu\n", sbi->ll_fsname,
1201                        PAGES_TO_MiB(pages_number),
1202                        PAGES_TO_MiB(max_ra_per_file));
1203                 return -ERANGE;
1204         }
1205         sbi->ll_ra_info.ra_async_pages_per_file_threshold = pages_number;
1206
1207         return count;
1208 }
1209 LUSTRE_RW_ATTR(read_ahead_async_file_threshold_mb);
1210
1211 static ssize_t read_ahead_range_kb_show(struct kobject *kobj,
1212                                         struct attribute *attr,char *buf)
1213 {
1214         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1215                                               ll_kset.kobj);
1216
1217         return snprintf(buf, PAGE_SIZE, "%lu\n",
1218                         sbi->ll_ra_info.ra_range_pages << (PAGE_SHIFT - 10));
1219 }
1220
1221 static ssize_t
1222 read_ahead_range_kb_store(struct kobject *kobj,
1223                                struct attribute *attr,
1224                                const char *buffer, size_t count)
1225 {
1226         unsigned long pages_number;
1227         unsigned long max_ra_per_file;
1228         u64 val;
1229         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1230                                               ll_kset.kobj);
1231         int rc;
1232
1233         rc = sysfs_memparse(buffer, count, &val, "KiB");
1234         if (rc < 0)
1235                 return rc;
1236
1237         pages_number = val >> PAGE_SHIFT;
1238         /* Disable mmap range read */
1239         if (pages_number == 0)
1240                 goto out;
1241
1242         max_ra_per_file = sbi->ll_ra_info.ra_max_pages_per_file;
1243         if (pages_number > max_ra_per_file ||
1244             pages_number < RA_MIN_MMAP_RANGE_PAGES)
1245                 return -ERANGE;
1246
1247 out:
1248         spin_lock(&sbi->ll_lock);
1249         sbi->ll_ra_info.ra_range_pages = pages_number;
1250         spin_unlock(&sbi->ll_lock);
1251
1252         return count;
1253 }
1254 LUSTRE_RW_ATTR(read_ahead_range_kb);
1255
1256 static ssize_t fast_read_show(struct kobject *kobj,
1257                               struct attribute *attr,
1258                               char *buf)
1259 {
1260         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1261                                               ll_kset.kobj);
1262
1263         return sprintf(buf, "%u\n", !!(sbi->ll_flags & LL_SBI_FAST_READ));
1264 }
1265
1266 static ssize_t fast_read_store(struct kobject *kobj,
1267                                struct attribute *attr,
1268                                const char *buffer,
1269                                size_t count)
1270 {
1271         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1272                                               ll_kset.kobj);
1273         bool val;
1274         int rc;
1275
1276         rc = kstrtobool(buffer, &val);
1277         if (rc)
1278                 return rc;
1279
1280         spin_lock(&sbi->ll_lock);
1281         if (val)
1282                 sbi->ll_flags |= LL_SBI_FAST_READ;
1283         else
1284                 sbi->ll_flags &= ~LL_SBI_FAST_READ;
1285         spin_unlock(&sbi->ll_lock);
1286
1287         return count;
1288 }
1289 LUSTRE_RW_ATTR(fast_read);
1290
1291 static ssize_t file_heat_show(struct kobject *kobj,
1292                               struct attribute *attr,
1293                               char *buf)
1294 {
1295         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1296                                               ll_kset.kobj);
1297
1298         return scnprintf(buf, PAGE_SIZE, "%u\n",
1299                          !!(sbi->ll_flags & LL_SBI_FILE_HEAT));
1300 }
1301
1302 static ssize_t file_heat_store(struct kobject *kobj,
1303                                struct attribute *attr,
1304                                const char *buffer,
1305                                size_t count)
1306 {
1307         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1308                                               ll_kset.kobj);
1309         bool val;
1310         int rc;
1311
1312         rc = kstrtobool(buffer, &val);
1313         if (rc)
1314                 return rc;
1315
1316         spin_lock(&sbi->ll_lock);
1317         if (val)
1318                 sbi->ll_flags |= LL_SBI_FILE_HEAT;
1319         else
1320                 sbi->ll_flags &= ~LL_SBI_FILE_HEAT;
1321         spin_unlock(&sbi->ll_lock);
1322
1323         return count;
1324 }
1325 LUSTRE_RW_ATTR(file_heat);
1326
1327 static ssize_t heat_decay_percentage_show(struct kobject *kobj,
1328                                           struct attribute *attr,
1329                                           char *buf)
1330 {
1331         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1332                                               ll_kset.kobj);
1333
1334         return scnprintf(buf, PAGE_SIZE, "%u\n",
1335                          (sbi->ll_heat_decay_weight * 100 + 128) / 256);
1336 }
1337
1338 static ssize_t heat_decay_percentage_store(struct kobject *kobj,
1339                                            struct attribute *attr,
1340                                            const char *buffer,
1341                                            size_t count)
1342 {
1343         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1344                                               ll_kset.kobj);
1345         unsigned long val;
1346         int rc;
1347
1348         rc = kstrtoul(buffer, 10, &val);
1349         if (rc)
1350                 return rc;
1351
1352         if (val < 0 || val > 100)
1353                 return -ERANGE;
1354
1355         sbi->ll_heat_decay_weight = (val * 256 + 50) / 100;
1356
1357         return count;
1358 }
1359 LUSTRE_RW_ATTR(heat_decay_percentage);
1360
1361 static ssize_t heat_period_second_show(struct kobject *kobj,
1362                                        struct attribute *attr,
1363                                        char *buf)
1364 {
1365         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1366                                               ll_kset.kobj);
1367
1368         return scnprintf(buf, PAGE_SIZE, "%u\n", sbi->ll_heat_period_second);
1369 }
1370
1371 static ssize_t heat_period_second_store(struct kobject *kobj,
1372                                         struct attribute *attr,
1373                                         const char *buffer,
1374                                         size_t count)
1375 {
1376         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1377                                               ll_kset.kobj);
1378         unsigned long val;
1379         int rc;
1380
1381         rc = kstrtoul(buffer, 10, &val);
1382         if (rc)
1383                 return rc;
1384
1385         if (val <= 0)
1386                 return -ERANGE;
1387
1388         sbi->ll_heat_period_second = val;
1389
1390         return count;
1391 }
1392 LUSTRE_RW_ATTR(heat_period_second);
1393
1394 static ssize_t opencache_threshold_count_show(struct kobject *kobj,
1395                                               struct attribute *attr,
1396                                               char *buf)
1397 {
1398         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1399                                               ll_kset.kobj);
1400
1401         if (sbi->ll_oc_thrsh_count)
1402                 return snprintf(buf, PAGE_SIZE, "%u\n",
1403                                 sbi->ll_oc_thrsh_count);
1404         else
1405                 return snprintf(buf, PAGE_SIZE, "off\n");
1406 }
1407
1408 static ssize_t opencache_threshold_count_store(struct kobject *kobj,
1409                                                struct attribute *attr,
1410                                                const char *buffer,
1411                                                size_t count)
1412 {
1413         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1414                                               ll_kset.kobj);
1415         unsigned int val;
1416         int rc;
1417
1418         rc = kstrtouint(buffer, 10, &val);
1419         if (rc) {
1420                 bool enable;
1421                 /* also accept "off" to disable and "on" to always cache */
1422                 rc = kstrtobool(buffer, &enable);
1423                 if (rc)
1424                         return rc;
1425                 val = enable;
1426         }
1427         sbi->ll_oc_thrsh_count = val;
1428
1429         return count;
1430 }
1431 LUSTRE_RW_ATTR(opencache_threshold_count);
1432
1433 static ssize_t opencache_threshold_ms_show(struct kobject *kobj,
1434                                            struct attribute *attr,
1435                                            char *buf)
1436 {
1437         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1438                                               ll_kset.kobj);
1439
1440         return snprintf(buf, PAGE_SIZE, "%u\n", sbi->ll_oc_thrsh_ms);
1441 }
1442
1443 static ssize_t opencache_threshold_ms_store(struct kobject *kobj,
1444                                             struct attribute *attr,
1445                                             const char *buffer,
1446                                             size_t count)
1447 {
1448         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1449                                               ll_kset.kobj);
1450         unsigned int val;
1451         int rc;
1452
1453         rc = kstrtouint(buffer, 10, &val);
1454         if (rc)
1455                 return rc;
1456
1457         sbi->ll_oc_thrsh_ms = val;
1458
1459         return count;
1460 }
1461 LUSTRE_RW_ATTR(opencache_threshold_ms);
1462
1463 static ssize_t opencache_max_ms_show(struct kobject *kobj,
1464                                      struct attribute *attr,
1465                                      char *buf)
1466 {
1467         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1468                                               ll_kset.kobj);
1469
1470         return snprintf(buf, PAGE_SIZE, "%u\n", sbi->ll_oc_max_ms);
1471 }
1472
1473 static ssize_t opencache_max_ms_store(struct kobject *kobj,
1474                                       struct attribute *attr,
1475                                       const char *buffer,
1476                                       size_t count)
1477 {
1478         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1479                                               ll_kset.kobj);
1480         unsigned int val;
1481         int rc;
1482
1483         rc = kstrtouint(buffer, 10, &val);
1484         if (rc)
1485                 return rc;
1486
1487         sbi->ll_oc_max_ms = val;
1488
1489         return count;
1490 }
1491 LUSTRE_RW_ATTR(opencache_max_ms);
1492
1493 static int ll_unstable_stats_seq_show(struct seq_file *m, void *v)
1494 {
1495         struct super_block      *sb    = m->private;
1496         struct ll_sb_info       *sbi   = ll_s2sbi(sb);
1497         struct cl_client_cache  *cache = sbi->ll_cache;
1498         long pages;
1499         int mb;
1500
1501         pages = atomic_long_read(&cache->ccc_unstable_nr);
1502         mb    = (pages * PAGE_SIZE) >> 20;
1503
1504         seq_printf(m, "unstable_check:     %8d\n"
1505                       "unstable_pages: %12ld\n"
1506                       "unstable_mb:        %8d\n",
1507                    cache->ccc_unstable_check, pages, mb);
1508         return 0;
1509 }
1510
1511 static ssize_t ll_unstable_stats_seq_write(struct file *file,
1512                                            const char __user *buffer,
1513                                            size_t count, loff_t *unused)
1514 {
1515         struct seq_file *seq = file->private_data;
1516         struct ll_sb_info *sbi = ll_s2sbi((struct super_block *)seq->private);
1517         char kernbuf[128];
1518         bool val;
1519         int rc;
1520
1521         if (count == 0)
1522                 return 0;
1523         if (count >= sizeof(kernbuf))
1524                 return -EINVAL;
1525
1526         if (copy_from_user(kernbuf, buffer, count))
1527                 return -EFAULT;
1528         kernbuf[count] = 0;
1529
1530         buffer += lprocfs_find_named_value(kernbuf, "unstable_check:", &count) -
1531                   kernbuf;
1532         rc = kstrtobool_from_user(buffer, count, &val);
1533         if (rc < 0)
1534                 return rc;
1535
1536         /* borrow lru lock to set the value */
1537         spin_lock(&sbi->ll_cache->ccc_lru_lock);
1538         sbi->ll_cache->ccc_unstable_check = val;
1539         spin_unlock(&sbi->ll_cache->ccc_lru_lock);
1540
1541         return count;
1542 }
1543
1544 LDEBUGFS_SEQ_FOPS(ll_unstable_stats);
1545
1546 static int ll_root_squash_seq_show(struct seq_file *m, void *v)
1547 {
1548         struct super_block *sb = m->private;
1549         struct ll_sb_info *sbi = ll_s2sbi(sb);
1550         struct root_squash_info *squash = &sbi->ll_squash;
1551
1552         seq_printf(m, "%u:%u\n", squash->rsi_uid, squash->rsi_gid);
1553         return 0;
1554 }
1555
1556 static ssize_t ll_root_squash_seq_write(struct file *file,
1557                                         const char __user *buffer,
1558                                         size_t count, loff_t *off)
1559 {
1560         struct seq_file *m = file->private_data;
1561         struct super_block *sb = m->private;
1562         struct ll_sb_info *sbi = ll_s2sbi(sb);
1563         struct root_squash_info *squash = &sbi->ll_squash;
1564
1565         return lprocfs_wr_root_squash(buffer, count, squash, sbi->ll_fsname);
1566 }
1567
1568 LDEBUGFS_SEQ_FOPS(ll_root_squash);
1569
1570 static int ll_nosquash_nids_seq_show(struct seq_file *m, void *v)
1571 {
1572         struct super_block *sb = m->private;
1573         struct ll_sb_info *sbi = ll_s2sbi(sb);
1574         struct root_squash_info *squash = &sbi->ll_squash;
1575         int len;
1576
1577         spin_lock(&squash->rsi_lock);
1578         if (!list_empty(&squash->rsi_nosquash_nids)) {
1579                 len = cfs_print_nidlist(m->buf + m->count, m->size - m->count,
1580                                         &squash->rsi_nosquash_nids);
1581                 m->count += len;
1582                 seq_putc(m, '\n');
1583         } else {
1584                 seq_puts(m, "NONE\n");
1585         }
1586         spin_unlock(&squash->rsi_lock);
1587
1588         return 0;
1589 }
1590
1591 static ssize_t ll_nosquash_nids_seq_write(struct file *file,
1592                                           const char __user *buffer,
1593                                           size_t count, loff_t *off)
1594 {
1595         struct seq_file *m = file->private_data;
1596         struct super_block *sb = m->private;
1597         struct ll_sb_info *sbi = ll_s2sbi(sb);
1598         struct root_squash_info *squash = &sbi->ll_squash;
1599         int rc;
1600
1601         rc = lprocfs_wr_nosquash_nids(buffer, count, squash, sbi->ll_fsname);
1602         if (rc < 0)
1603                 return rc;
1604
1605         ll_compute_rootsquash_state(sbi);
1606
1607         return rc;
1608 }
1609
1610 LDEBUGFS_SEQ_FOPS(ll_nosquash_nids);
1611
1612 static int ll_pcc_seq_show(struct seq_file *m, void *v)
1613 {
1614         struct super_block *sb = m->private;
1615         struct ll_sb_info *sbi = ll_s2sbi(sb);
1616
1617         return pcc_super_dump(&sbi->ll_pcc_super, m);
1618 }
1619
1620 static ssize_t ll_pcc_seq_write(struct file *file, const char __user *buffer,
1621                                 size_t count, loff_t *off)
1622 {
1623         struct seq_file *m = file->private_data;
1624         struct super_block *sb = m->private;
1625         struct ll_sb_info *sbi = ll_s2sbi(sb);
1626         int rc;
1627         char *kernbuf;
1628
1629         if (count >= LPROCFS_WR_PCC_MAX_CMD)
1630                 return -EINVAL;
1631
1632         if (!(exp_connect_flags2(sbi->ll_md_exp) & OBD_CONNECT2_PCC))
1633                 return -EOPNOTSUPP;
1634
1635         OBD_ALLOC(kernbuf, count + 1);
1636         if (kernbuf == NULL)
1637                 return -ENOMEM;
1638
1639         if (copy_from_user(kernbuf, buffer, count))
1640                 GOTO(out_free_kernbuff, rc = -EFAULT);
1641
1642         rc = pcc_cmd_handle(kernbuf, count, &sbi->ll_pcc_super);
1643 out_free_kernbuff:
1644         OBD_FREE(kernbuf, count + 1);
1645         return rc ? rc : count;
1646 }
1647 LDEBUGFS_SEQ_FOPS(ll_pcc);
1648
1649 struct ldebugfs_vars lprocfs_llite_obd_vars[] = {
1650         { .name =       "site",
1651           .fops =       &ll_site_stats_fops                     },
1652         { .name =       "max_cached_mb",
1653           .fops =       &ll_max_cached_mb_fops                  },
1654         { .name =       "statahead_stats",
1655           .fops =       &ll_statahead_stats_fops                },
1656         { .name =       "unstable_stats",
1657           .fops =       &ll_unstable_stats_fops                 },
1658         { .name =       "sbi_flags",
1659           .fops =       &ll_sbi_flags_fops                      },
1660         { .name =       "root_squash",
1661           .fops =       &ll_root_squash_fops                    },
1662         { .name =       "nosquash_nids",
1663           .fops =       &ll_nosquash_nids_fops                  },
1664         { .name =       "pcc",
1665           .fops =       &ll_pcc_fops,                           },
1666         { NULL }
1667 };
1668
1669 #define MAX_STRING_SIZE 128
1670
1671 static struct attribute *llite_attrs[] = {
1672         &lustre_attr_blocksize.attr,
1673         &lustre_attr_stat_blocksize.attr,
1674         &lustre_attr_kbytestotal.attr,
1675         &lustre_attr_kbytesfree.attr,
1676         &lustre_attr_kbytesavail.attr,
1677         &lustre_attr_filestotal.attr,
1678         &lustre_attr_filesfree.attr,
1679         &lustre_attr_client_type.attr,
1680         &lustre_attr_foreign_symlink_enable.attr,
1681         &lustre_attr_foreign_symlink_prefix.attr,
1682         &lustre_attr_foreign_symlink_upcall.attr,
1683         &lustre_attr_foreign_symlink_upcall_info.attr,
1684         &lustre_attr_fstype.attr,
1685         &lustre_attr_uuid.attr,
1686         &lustre_attr_checksums.attr,
1687         &lustre_attr_checksum_pages.attr,
1688         &lustre_attr_max_read_ahead_mb.attr,
1689         &lustre_attr_max_read_ahead_per_file_mb.attr,
1690         &lustre_attr_max_read_ahead_whole_mb.attr,
1691         &lustre_attr_max_read_ahead_async_active.attr,
1692         &lustre_attr_read_ahead_async_file_threshold_mb.attr,
1693         &lustre_attr_read_ahead_range_kb.attr,
1694         &lustre_attr_stats_track_pid.attr,
1695         &lustre_attr_stats_track_ppid.attr,
1696         &lustre_attr_stats_track_gid.attr,
1697         &lustre_attr_statahead_running_max.attr,
1698         &lustre_attr_statahead_max.attr,
1699         &lustre_attr_statahead_agl.attr,
1700         &lustre_attr_lazystatfs.attr,
1701         &lustre_attr_statfs_max_age.attr,
1702         &lustre_attr_max_easize.attr,
1703         &lustre_attr_default_easize.attr,
1704         &lustre_attr_xattr_cache.attr,
1705         &lustre_attr_fast_read.attr,
1706         &lustre_attr_tiny_write.attr,
1707         &lustre_attr_parallel_dio.attr,
1708         &lustre_attr_file_heat.attr,
1709         &lustre_attr_heat_decay_percentage.attr,
1710         &lustre_attr_heat_period_second.attr,
1711         &lustre_attr_opencache_threshold_count.attr,
1712         &lustre_attr_opencache_threshold_ms.attr,
1713         &lustre_attr_opencache_max_ms.attr,
1714         NULL,
1715 };
1716
1717 static void sbi_kobj_release(struct kobject *kobj)
1718 {
1719         struct ll_sb_info *sbi = container_of(kobj, struct ll_sb_info,
1720                                               ll_kset.kobj);
1721         complete(&sbi->ll_kobj_unregister);
1722 }
1723
1724 static struct kobj_type sbi_ktype = {
1725         .default_attrs  = llite_attrs,
1726         .sysfs_ops      = &lustre_sysfs_ops,
1727         .release        = sbi_kobj_release,
1728 };
1729
1730 static const struct llite_file_opcode {
1731         __u32           opcode;
1732         __u32           type;
1733         const char      *opname;
1734 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
1735         /* file operation */
1736         { LPROC_LL_READ_BYTES,  LPROCFS_TYPE_BYTES_FULL, "read_bytes" },
1737         { LPROC_LL_WRITE_BYTES, LPROCFS_TYPE_BYTES_FULL, "write_bytes" },
1738         { LPROC_LL_READ,        LPROCFS_TYPE_LATENCY,   "read" },
1739         { LPROC_LL_WRITE,       LPROCFS_TYPE_LATENCY,   "write" },
1740         { LPROC_LL_IOCTL,       LPROCFS_TYPE_REQS,      "ioctl" },
1741         { LPROC_LL_OPEN,        LPROCFS_TYPE_LATENCY,   "open" },
1742         { LPROC_LL_RELEASE,     LPROCFS_TYPE_LATENCY,   "close" },
1743         { LPROC_LL_MMAP,        LPROCFS_TYPE_LATENCY,   "mmap" },
1744         { LPROC_LL_FAULT,       LPROCFS_TYPE_LATENCY,   "page_fault" },
1745         { LPROC_LL_MKWRITE,     LPROCFS_TYPE_LATENCY,   "page_mkwrite" },
1746         { LPROC_LL_LLSEEK,      LPROCFS_TYPE_LATENCY,   "seek" },
1747         { LPROC_LL_FSYNC,       LPROCFS_TYPE_LATENCY,   "fsync" },
1748         { LPROC_LL_READDIR,     LPROCFS_TYPE_LATENCY,   "readdir" },
1749         { LPROC_LL_INODE_OCOUNT,LPROCFS_TYPE_REQS |
1750                                 LPROCFS_CNTR_AVGMINMAX |
1751                                 LPROCFS_CNTR_STDDEV,    "opencount" },
1752         { LPROC_LL_INODE_OPCLTM,LPROCFS_TYPE_LATENCY,   "openclosetime" },
1753         /* inode operation */
1754         { LPROC_LL_SETATTR,     LPROCFS_TYPE_LATENCY,   "setattr" },
1755         { LPROC_LL_TRUNC,       LPROCFS_TYPE_LATENCY,   "truncate" },
1756         { LPROC_LL_FLOCK,       LPROCFS_TYPE_LATENCY,   "flock" },
1757         { LPROC_LL_GETATTR,     LPROCFS_TYPE_LATENCY,   "getattr" },
1758         { LPROC_LL_FALLOCATE,   LPROCFS_TYPE_LATENCY, "fallocate"},
1759         /* dir inode operation */
1760         { LPROC_LL_CREATE,      LPROCFS_TYPE_LATENCY,   "create" },
1761         { LPROC_LL_LINK,        LPROCFS_TYPE_LATENCY,   "link" },
1762         { LPROC_LL_UNLINK,      LPROCFS_TYPE_LATENCY,   "unlink" },
1763         { LPROC_LL_SYMLINK,     LPROCFS_TYPE_LATENCY,   "symlink" },
1764         { LPROC_LL_MKDIR,       LPROCFS_TYPE_LATENCY,   "mkdir" },
1765         { LPROC_LL_RMDIR,       LPROCFS_TYPE_LATENCY,   "rmdir" },
1766         { LPROC_LL_MKNOD,       LPROCFS_TYPE_LATENCY,   "mknod" },
1767         { LPROC_LL_RENAME,      LPROCFS_TYPE_LATENCY,   "rename" },
1768         /* special inode operation */
1769         { LPROC_LL_STATFS,      LPROCFS_TYPE_LATENCY,   "statfs" },
1770         { LPROC_LL_SETXATTR,    LPROCFS_TYPE_LATENCY,   "setxattr" },
1771         { LPROC_LL_GETXATTR,    LPROCFS_TYPE_LATENCY,   "getxattr" },
1772         { LPROC_LL_GETXATTR_HITS, LPROCFS_TYPE_REQS,    "getxattr_hits" },
1773         { LPROC_LL_LISTXATTR,   LPROCFS_TYPE_LATENCY,   "listxattr" },
1774         { LPROC_LL_REMOVEXATTR, LPROCFS_TYPE_LATENCY,   "removexattr" },
1775         { LPROC_LL_INODE_PERM,  LPROCFS_TYPE_LATENCY,   "inode_permission" },
1776 };
1777
1778 void ll_stats_ops_tally(struct ll_sb_info *sbi, int op, long count)
1779 {
1780         if (!sbi->ll_stats)
1781                 return;
1782
1783         if (sbi->ll_stats_track_type == STATS_TRACK_ALL)
1784                 lprocfs_counter_add(sbi->ll_stats, op, count);
1785         else if (sbi->ll_stats_track_type == STATS_TRACK_PID &&
1786                  sbi->ll_stats_track_id == current->pid)
1787                 lprocfs_counter_add(sbi->ll_stats, op, count);
1788         else if (sbi->ll_stats_track_type == STATS_TRACK_PPID &&
1789                  sbi->ll_stats_track_id == current->parent->pid)
1790                 lprocfs_counter_add(sbi->ll_stats, op, count);
1791         else if (sbi->ll_stats_track_type == STATS_TRACK_GID &&
1792                  sbi->ll_stats_track_id ==
1793                         from_kgid(&init_user_ns, current_gid()))
1794                 lprocfs_counter_add(sbi->ll_stats, op, count);
1795 }
1796 EXPORT_SYMBOL(ll_stats_ops_tally);
1797
1798 static const char *const ra_stat_string[] = {
1799         [RA_STAT_HIT]                   = "hits",
1800         [RA_STAT_MISS]                  = "misses",
1801         [RA_STAT_DISTANT_READPAGE]      = "readpage_not_consecutive",
1802         [RA_STAT_MISS_IN_WINDOW]        = "miss_inside_window",
1803         [RA_STAT_FAILED_GRAB_PAGE]      = "failed_grab_cache_page",
1804         [RA_STAT_FAILED_MATCH]          = "failed_lock_match",
1805         [RA_STAT_DISCARDED]             = "read_but_discarded",
1806         [RA_STAT_ZERO_LEN]              = "zero_length_file",
1807         [RA_STAT_ZERO_WINDOW]           = "zero_size_window",
1808         [RA_STAT_EOF]                   = "readahead_to_eof",
1809         [RA_STAT_MAX_IN_FLIGHT]         = "hit_max_readahead_issue",
1810         [RA_STAT_WRONG_GRAB_PAGE]       = "wrong_page_from_grab_cache_page",
1811         [RA_STAT_FAILED_REACH_END]      = "failed_to_reach_end",
1812         [RA_STAT_ASYNC]                 = "async_readahead",
1813         [RA_STAT_FAILED_FAST_READ]      = "failed_to_fast_read",
1814         [RA_STAT_MMAP_RANGE_READ]       = "mmap_range_read",
1815 };
1816
1817 int ll_debugfs_register_super(struct super_block *sb, const char *name)
1818 {
1819         struct lustre_sb_info *lsi = s2lsi(sb);
1820         struct ll_sb_info *sbi = ll_s2sbi(sb);
1821         int err, id;
1822
1823         ENTRY;
1824         LASSERT(sbi);
1825
1826         if (IS_ERR_OR_NULL(llite_root))
1827                 goto out_ll_kset;
1828
1829         sbi->ll_debugfs_entry = debugfs_create_dir(name, llite_root);
1830         ldebugfs_add_vars(sbi->ll_debugfs_entry, lprocfs_llite_obd_vars, sb);
1831
1832         debugfs_create_file("dump_page_cache", 0444, sbi->ll_debugfs_entry, sbi,
1833                             &vvp_dump_pgcache_file_ops);
1834
1835         debugfs_create_file("extents_stats", 0644, sbi->ll_debugfs_entry, sbi,
1836                                  &ll_rw_extents_stats_fops);
1837
1838         debugfs_create_file("extents_stats_per_process", 0644,
1839                             sbi->ll_debugfs_entry, sbi,
1840                             &ll_rw_extents_stats_pp_fops);
1841
1842         debugfs_create_file("offset_stats", 0644, sbi->ll_debugfs_entry, sbi,
1843                             &ll_rw_offset_stats_fops);
1844
1845         /* File operations stats */
1846         sbi->ll_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES,
1847                                             LPROCFS_STATS_FLAG_NONE);
1848         if (sbi->ll_stats == NULL)
1849                 GOTO(out_debugfs, err = -ENOMEM);
1850
1851         /* do counter init */
1852         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
1853                 u32 type = llite_opcode_table[id].type;
1854                 void *ptr = "unknown";
1855
1856                 if (type & LPROCFS_TYPE_REQS)
1857                         ptr = "reqs";
1858                 else if (type & LPROCFS_TYPE_BYTES)
1859                         ptr = "bytes";
1860                 else if (type & LPROCFS_TYPE_USEC)
1861                         ptr = "usec";
1862                 lprocfs_counter_init(sbi->ll_stats,
1863                                      llite_opcode_table[id].opcode, type,
1864                                      llite_opcode_table[id].opname, ptr);
1865         }
1866
1867         debugfs_create_file("stats", 0644, sbi->ll_debugfs_entry,
1868                             sbi->ll_stats, &ldebugfs_stats_seq_fops);
1869
1870         sbi->ll_ra_stats = lprocfs_alloc_stats(ARRAY_SIZE(ra_stat_string),
1871                                                LPROCFS_STATS_FLAG_NONE);
1872         if (sbi->ll_ra_stats == NULL)
1873                 GOTO(out_stats, err = -ENOMEM);
1874
1875         for (id = 0; id < ARRAY_SIZE(ra_stat_string); id++)
1876                 lprocfs_counter_init(sbi->ll_ra_stats, id, 0,
1877                                      ra_stat_string[id], "pages");
1878
1879         debugfs_create_file("read_ahead_stats", 0644, sbi->ll_debugfs_entry,
1880                             sbi->ll_ra_stats, &ldebugfs_stats_seq_fops);
1881
1882 out_ll_kset:
1883         /* Yes we also register sysfs mount kset here as well */
1884         sbi->ll_kset.kobj.parent = llite_kobj;
1885         sbi->ll_kset.kobj.ktype = &sbi_ktype;
1886         init_completion(&sbi->ll_kobj_unregister);
1887         err = kobject_set_name(&sbi->ll_kset.kobj, "%s", name);
1888         if (err)
1889                 GOTO(out_ra_stats, err);
1890
1891         err = kset_register(&sbi->ll_kset);
1892         if (err)
1893                 GOTO(out_ra_stats, err);
1894
1895         lsi->lsi_kobj = kobject_get(&sbi->ll_kset.kobj);
1896
1897         RETURN(0);
1898 out_ra_stats:
1899         lprocfs_free_stats(&sbi->ll_ra_stats);
1900 out_stats:
1901         lprocfs_free_stats(&sbi->ll_stats);
1902 out_debugfs:
1903         debugfs_remove_recursive(sbi->ll_debugfs_entry);
1904
1905         RETURN(err);
1906 }
1907
1908 void ll_debugfs_unregister_super(struct super_block *sb)
1909 {
1910         struct lustre_sb_info *lsi = s2lsi(sb);
1911         struct ll_sb_info *sbi = ll_s2sbi(sb);
1912
1913         debugfs_remove_recursive(sbi->ll_debugfs_entry);
1914
1915         if (sbi->ll_dt_obd)
1916                 sysfs_remove_link(&sbi->ll_kset.kobj,
1917                                   sbi->ll_dt_obd->obd_type->typ_name);
1918
1919         if (sbi->ll_md_obd)
1920                 sysfs_remove_link(&sbi->ll_kset.kobj,
1921                                   sbi->ll_md_obd->obd_type->typ_name);
1922
1923         kobject_put(lsi->lsi_kobj);
1924
1925         kset_unregister(&sbi->ll_kset);
1926         wait_for_completion(&sbi->ll_kobj_unregister);
1927
1928         lprocfs_free_stats(&sbi->ll_ra_stats);
1929         lprocfs_free_stats(&sbi->ll_stats);
1930 }
1931 #undef MAX_STRING_SIZE
1932
1933 static void ll_display_extents_info(struct ll_rw_extents_info *io_extents,
1934                                    struct seq_file *seq, int which)
1935 {
1936         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
1937         unsigned long start, end, r, w;
1938         char *unitp = "KMGTPEZY";
1939         int i, units = 10;
1940         struct per_process_info *pp_info = &io_extents->pp_extents[which];
1941
1942         read_cum = 0;
1943         write_cum = 0;
1944         start = 0;
1945
1946         for(i = 0; i < LL_HIST_MAX; i++) {
1947                 read_tot += pp_info->pp_r_hist.oh_buckets[i];
1948                 write_tot += pp_info->pp_w_hist.oh_buckets[i];
1949         }
1950
1951         for(i = 0; i < LL_HIST_MAX; i++) {
1952                 r = pp_info->pp_r_hist.oh_buckets[i];
1953                 w = pp_info->pp_w_hist.oh_buckets[i];
1954                 read_cum += r;
1955                 write_cum += w;
1956                 end = 1 << (i + LL_HIST_START - units);
1957                 seq_printf(seq, "%4lu%c - %4lu%c%c: %14lu %4u %4u  | "
1958                            "%14lu %4u %4u\n", start, *unitp, end, *unitp,
1959                            (i == LL_HIST_MAX - 1) ? '+' : ' ',
1960                            r, pct(r, read_tot), pct(read_cum, read_tot),
1961                            w, pct(w, write_tot), pct(write_cum, write_tot));
1962                 start = end;
1963                 if (start == (1 << 10)) {
1964                         start = 1;
1965                         units += 10;
1966                         unitp++;
1967                 }
1968                 if (read_cum == read_tot && write_cum == write_tot)
1969                         break;
1970         }
1971 }
1972
1973 static int ll_rw_extents_stats_pp_seq_show(struct seq_file *seq, void *v)
1974 {
1975         struct timespec64 now;
1976         struct ll_sb_info *sbi = seq->private;
1977         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
1978         int k;
1979
1980         ktime_get_real_ts64(&now);
1981
1982         if (!sbi->ll_rw_stats_on) {
1983                 seq_puts(seq, "disabled\n write anything to this file to activate, then '0' or 'disable' to deactivate\n");
1984                 return 0;
1985         }
1986         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
1987                    (s64)now.tv_sec, now.tv_nsec);
1988         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
1989         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
1990                    "extents", "calls", "%", "cum%",
1991                    "calls", "%", "cum%");
1992         spin_lock(&sbi->ll_pp_extent_lock);
1993         for (k = 0; k < LL_PROCESS_HIST_MAX; k++) {
1994                 if (io_extents->pp_extents[k].pid != 0) {
1995                         seq_printf(seq, "\nPID: %d\n",
1996                                    io_extents->pp_extents[k].pid);
1997                         ll_display_extents_info(io_extents, seq, k);
1998                 }
1999         }
2000         spin_unlock(&sbi->ll_pp_extent_lock);
2001         return 0;
2002 }
2003
2004 static ssize_t ll_rw_extents_stats_pp_seq_write(struct file *file,
2005                                                 const char __user *buf,
2006                                                 size_t len,
2007                                                 loff_t *off)
2008 {
2009         struct seq_file *seq = file->private_data;
2010         struct ll_sb_info *sbi = seq->private;
2011         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
2012         int i;
2013         __s64 value;
2014
2015         if (len == 0)
2016                 return -EINVAL;
2017
2018         value = ll_stats_pid_write(buf, len);
2019
2020         if (value == 0)
2021                 sbi->ll_rw_stats_on = 0;
2022         else
2023                 sbi->ll_rw_stats_on = 1;
2024
2025         spin_lock(&sbi->ll_pp_extent_lock);
2026         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
2027                 io_extents->pp_extents[i].pid = 0;
2028                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
2029                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
2030         }
2031         spin_unlock(&sbi->ll_pp_extent_lock);
2032         return len;
2033 }
2034
2035 LDEBUGFS_SEQ_FOPS(ll_rw_extents_stats_pp);
2036
2037 static int ll_rw_extents_stats_seq_show(struct seq_file *seq, void *v)
2038 {
2039         struct timespec64 now;
2040         struct ll_sb_info *sbi = seq->private;
2041         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
2042
2043         ktime_get_real_ts64(&now);
2044
2045         if (!sbi->ll_rw_stats_on) {
2046                 seq_puts(seq, "disabled\n write anything to this file to activate, then '0' or 'disable' to deactivate\n");
2047                 return 0;
2048         }
2049         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
2050                    (s64)now.tv_sec, now.tv_nsec);
2051
2052         seq_printf(seq, "%15s %19s       | %20s\n", " ", "read", "write");
2053         seq_printf(seq, "%13s   %14s %4s %4s  | %14s %4s %4s\n",
2054                    "extents", "calls", "%", "cum%",
2055                    "calls", "%", "cum%");
2056         spin_lock(&sbi->ll_lock);
2057         ll_display_extents_info(io_extents, seq, LL_PROCESS_HIST_MAX);
2058         spin_unlock(&sbi->ll_lock);
2059
2060         return 0;
2061 }
2062
2063 static ssize_t ll_rw_extents_stats_seq_write(struct file *file,
2064                                              const char __user *buf,
2065                                              size_t len, loff_t *off)
2066 {
2067         struct seq_file *seq = file->private_data;
2068         struct ll_sb_info *sbi = seq->private;
2069         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
2070         int i;
2071         __s64 value;
2072
2073         if (len == 0)
2074                 return -EINVAL;
2075
2076         value = ll_stats_pid_write(buf, len);
2077
2078         if (value == 0)
2079                 sbi->ll_rw_stats_on = 0;
2080         else
2081                 sbi->ll_rw_stats_on = 1;
2082
2083         spin_lock(&sbi->ll_pp_extent_lock);
2084         for (i = 0; i <= LL_PROCESS_HIST_MAX; i++) {
2085                 io_extents->pp_extents[i].pid = 0;
2086                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_r_hist);
2087                 lprocfs_oh_clear(&io_extents->pp_extents[i].pp_w_hist);
2088         }
2089         spin_unlock(&sbi->ll_pp_extent_lock);
2090
2091         return len;
2092 }
2093
2094 LDEBUGFS_SEQ_FOPS(ll_rw_extents_stats);
2095
2096 void ll_rw_stats_tally(struct ll_sb_info *sbi, pid_t pid,
2097                        struct ll_file_data *file, loff_t pos,
2098                        size_t count, int rw)
2099 {
2100         int i, cur = -1;
2101         struct ll_rw_process_info *process;
2102         struct ll_rw_process_info *offset;
2103         int *off_count = &sbi->ll_rw_offset_entry_count;
2104         int *process_count = &sbi->ll_offset_process_count;
2105         struct ll_rw_extents_info *io_extents = &sbi->ll_rw_extents_info;
2106
2107         if(!sbi->ll_rw_stats_on)
2108                 return;
2109         process = sbi->ll_rw_process_info;
2110         offset = sbi->ll_rw_offset_info;
2111
2112         spin_lock(&sbi->ll_pp_extent_lock);
2113         /* Extent statistics */
2114         for(i = 0; i < LL_PROCESS_HIST_MAX; i++) {
2115                 if(io_extents->pp_extents[i].pid == pid) {
2116                         cur = i;
2117                         break;
2118                 }
2119         }
2120
2121         if (cur == -1) {
2122                 /* new process */
2123                 sbi->ll_extent_process_count =
2124                         (sbi->ll_extent_process_count + 1) % LL_PROCESS_HIST_MAX;
2125                 cur = sbi->ll_extent_process_count;
2126                 io_extents->pp_extents[cur].pid = pid;
2127                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_r_hist);
2128                 lprocfs_oh_clear(&io_extents->pp_extents[cur].pp_w_hist);
2129         }
2130
2131         for (i = 0; (count >= 1 << (LL_HIST_START + i)) &&
2132              (i < (LL_HIST_MAX - 1)); i++);
2133         if (rw == 0) {
2134                 io_extents->pp_extents[cur].pp_r_hist.oh_buckets[i]++;
2135                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_r_hist.oh_buckets[i]++;
2136         } else {
2137                 io_extents->pp_extents[cur].pp_w_hist.oh_buckets[i]++;
2138                 io_extents->pp_extents[LL_PROCESS_HIST_MAX].pp_w_hist.oh_buckets[i]++;
2139         }
2140         spin_unlock(&sbi->ll_pp_extent_lock);
2141
2142         spin_lock(&sbi->ll_process_lock);
2143         /* Offset statistics */
2144         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
2145                 if (process[i].rw_pid == pid) {
2146                         if (process[i].rw_last_file != file) {
2147                                 process[i].rw_range_start = pos;
2148                                 process[i].rw_last_file_pos = pos + count;
2149                                 process[i].rw_smallest_extent = count;
2150                                 process[i].rw_largest_extent = count;
2151                                 process[i].rw_offset = 0;
2152                                 process[i].rw_last_file = file;
2153                                 spin_unlock(&sbi->ll_process_lock);
2154                                 return;
2155                         }
2156                         if (process[i].rw_last_file_pos != pos) {
2157                                 *off_count =
2158                                     (*off_count + 1) % LL_OFFSET_HIST_MAX;
2159                                 offset[*off_count].rw_op = process[i].rw_op;
2160                                 offset[*off_count].rw_pid = pid;
2161                                 offset[*off_count].rw_range_start =
2162                                         process[i].rw_range_start;
2163                                 offset[*off_count].rw_range_end =
2164                                         process[i].rw_last_file_pos;
2165                                 offset[*off_count].rw_smallest_extent =
2166                                         process[i].rw_smallest_extent;
2167                                 offset[*off_count].rw_largest_extent =
2168                                         process[i].rw_largest_extent;
2169                                 offset[*off_count].rw_offset =
2170                                         process[i].rw_offset;
2171                                 process[i].rw_op = rw;
2172                                 process[i].rw_range_start = pos;
2173                                 process[i].rw_smallest_extent = count;
2174                                 process[i].rw_largest_extent = count;
2175                                 process[i].rw_offset = pos -
2176                                         process[i].rw_last_file_pos;
2177                         }
2178                         if(process[i].rw_smallest_extent > count)
2179                                 process[i].rw_smallest_extent = count;
2180                         if(process[i].rw_largest_extent < count)
2181                                 process[i].rw_largest_extent = count;
2182                         process[i].rw_last_file_pos = pos + count;
2183                         spin_unlock(&sbi->ll_process_lock);
2184                         return;
2185                 }
2186         }
2187         *process_count = (*process_count + 1) % LL_PROCESS_HIST_MAX;
2188         process[*process_count].rw_pid = pid;
2189         process[*process_count].rw_op = rw;
2190         process[*process_count].rw_range_start = pos;
2191         process[*process_count].rw_last_file_pos = pos + count;
2192         process[*process_count].rw_smallest_extent = count;
2193         process[*process_count].rw_largest_extent = count;
2194         process[*process_count].rw_offset = 0;
2195         process[*process_count].rw_last_file = file;
2196         spin_unlock(&sbi->ll_process_lock);
2197 }
2198
2199 static int ll_rw_offset_stats_seq_show(struct seq_file *seq, void *v)
2200 {
2201         struct timespec64 now;
2202         struct ll_sb_info *sbi = seq->private;
2203         struct ll_rw_process_info *offset = sbi->ll_rw_offset_info;
2204         struct ll_rw_process_info *process = sbi->ll_rw_process_info;
2205         int i;
2206
2207         ktime_get_real_ts64(&now);
2208
2209         if (!sbi->ll_rw_stats_on) {
2210                 seq_puts(seq, "disabled\n write anything to this file to activate, then '0' or 'disable' to deactivate\n");
2211                 return 0;
2212         }
2213         spin_lock(&sbi->ll_process_lock);
2214
2215         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
2216                    (s64)now.tv_sec, now.tv_nsec);
2217         seq_printf(seq, "%3s %10s %14s %14s %17s %17s %14s\n",
2218                    "R/W", "PID", "RANGE START", "RANGE END",
2219                    "SMALLEST EXTENT", "LARGEST EXTENT", "OFFSET");
2220
2221         /* We stored the discontiguous offsets here; print them first */
2222         for (i = 0; i < LL_OFFSET_HIST_MAX; i++) {
2223                 if (offset[i].rw_pid != 0)
2224                         seq_printf(seq,
2225                                   "%3c %10d %14llu %14llu %17lu %17lu %14lld\n",
2226                                    offset[i].rw_op == READ ? 'R' : 'W',
2227                                    offset[i].rw_pid,
2228                                    offset[i].rw_range_start,
2229                                    offset[i].rw_range_end,
2230                                    (unsigned long)offset[i].rw_smallest_extent,
2231                                    (unsigned long)offset[i].rw_largest_extent,
2232                                    offset[i].rw_offset);
2233         }
2234
2235         /* Then print the current offsets for each process */
2236         for (i = 0; i < LL_PROCESS_HIST_MAX; i++) {
2237                 if (process[i].rw_pid != 0)
2238                         seq_printf(seq,
2239                                   "%3c %10d %14llu %14llu %17lu %17lu %14lld\n",
2240                                    process[i].rw_op == READ ? 'R' : 'W',
2241                                    process[i].rw_pid,
2242                                    process[i].rw_range_start,
2243                                    process[i].rw_last_file_pos,
2244                                    (unsigned long)process[i].rw_smallest_extent,
2245                                    (unsigned long)process[i].rw_largest_extent,
2246                                    process[i].rw_offset);
2247         }
2248         spin_unlock(&sbi->ll_process_lock);
2249
2250         return 0;
2251 }
2252
2253 static ssize_t ll_rw_offset_stats_seq_write(struct file *file,
2254                                             const char __user *buf,
2255                                             size_t len, loff_t *off)
2256 {
2257         struct seq_file *seq = file->private_data;
2258         struct ll_sb_info *sbi = seq->private;
2259         struct ll_rw_process_info *process_info = sbi->ll_rw_process_info;
2260         struct ll_rw_process_info *offset_info = sbi->ll_rw_offset_info;
2261         __s64 value;
2262
2263         if (len == 0)
2264                 return -EINVAL;
2265
2266         value = ll_stats_pid_write(buf, len);
2267
2268         if (value == 0)
2269                 sbi->ll_rw_stats_on = 0;
2270         else
2271                 sbi->ll_rw_stats_on = 1;
2272
2273         spin_lock(&sbi->ll_process_lock);
2274         sbi->ll_offset_process_count = 0;
2275         sbi->ll_rw_offset_entry_count = 0;
2276         memset(process_info, 0, sizeof(struct ll_rw_process_info) *
2277                LL_PROCESS_HIST_MAX);
2278         memset(offset_info, 0, sizeof(struct ll_rw_process_info) *
2279                LL_OFFSET_HIST_MAX);
2280         spin_unlock(&sbi->ll_process_lock);
2281
2282         return len;
2283 }
2284
2285 LDEBUGFS_SEQ_FOPS(ll_rw_offset_stats);