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