Whamcloud - gitweb
- landed b_hd_cray_merge3
[fs/lustre-release.git] / lustre / llite / lproc_llite.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22 #define DEBUG_SUBSYSTEM S_LLITE
23
24 #include <linux/version.h>
25 #include <linux/lustre_lite.h>
26 #include <linux/lprocfs_status.h>
27 #include <linux/seq_file.h>
28 #include <linux/obd_support.h>
29 #ifdef HAVE_MM_INLINE
30 #include <linux/mm_inline.h>
31 #endif
32
33 #include "llite_internal.h"
34
35 /* /proc/lustre/llite mount point registration */
36 struct proc_dir_entry *proc_lustre_fs_root;
37 struct file_operations llite_dump_pgcache_fops;
38 struct file_operations ll_ra_stats_fops;
39 struct file_operations llite_wait_times_fops;
40
41 #ifndef LPROCFS
42 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
43                                 struct super_block *sb, char *osc, char *mdc)
44 {
45         return 0;
46 }
47 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi){}
48 #else
49
50 long long mnt_instance;
51
52 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
53                          int *eof, void *data)
54 {
55         struct super_block *sb = (struct super_block *)data;
56         struct obd_statfs osfs;
57         int rc;
58
59         LASSERT(sb != NULL);
60         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
61         if (!rc) {
62               *eof = 1;
63               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
64         }
65
66         return rc;
67 }
68
69 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
70                              int *eof, void *data)
71 {
72         struct super_block *sb = (struct super_block *)data;
73         struct obd_statfs osfs;
74         int rc;
75
76         LASSERT(sb != NULL);
77         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
78         if (!rc) {
79                 __u32 blk_size = osfs.os_bsize >> 10;
80                 __u64 result = osfs.os_blocks;
81
82                 while (blk_size >>= 1)
83                         result <<= 1;
84
85                 *eof = 1;
86                 rc = snprintf(page, count, LPU64"\n", result);
87         }
88         return rc;
89
90 }
91
92 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
93                             int *eof, void *data)
94 {
95         struct super_block *sb = (struct super_block *)data;
96         struct obd_statfs osfs;
97         int rc;
98
99         LASSERT(sb != NULL);
100         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
101         if (!rc) {
102                 __u32 blk_size = osfs.os_bsize >> 10;
103                 __u64 result = osfs.os_bfree;
104
105                 while (blk_size >>= 1)
106                         result <<= 1;
107
108                 *eof = 1;
109                 rc = snprintf(page, count, LPU64"\n", result);
110         }
111         return rc;
112 }
113
114 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
115                              int *eof, void *data)
116 {
117         struct super_block *sb = (struct super_block *)data;
118         struct obd_statfs osfs;
119         int rc;
120
121         LASSERT(sb != NULL);
122         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
123         if (!rc) {
124                 __u32 blk_size = osfs.os_bsize >> 10;
125                 __u64 result = osfs.os_bavail;
126
127                 while (blk_size >>= 1)
128                         result <<= 1;
129
130                 *eof = 1;
131                 rc = snprintf(page, count, LPU64"\n", result);
132         }
133         return rc;
134 }
135
136 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
137                             int *eof, void *data)
138 {
139         struct super_block *sb = (struct super_block *)data;
140         struct obd_statfs osfs;
141         int rc;
142
143         LASSERT(sb != NULL);
144         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
145         if (!rc) {
146                  *eof = 1;
147                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
148         }
149         return rc;
150 }
151
152 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
153                            int *eof, void *data)
154 {
155         struct super_block *sb = (struct super_block *)data;
156         struct obd_statfs osfs;
157         int rc;
158
159         LASSERT(sb != NULL);
160         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
161         if (!rc) {
162                  *eof = 1;
163                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
164         }
165         return rc;
166
167 }
168
169 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
170                         int *eof, void *data)
171 {
172         struct super_block *sb = (struct super_block*)data;
173
174         LASSERT(sb != NULL);
175         *eof = 1;
176         return snprintf(page, count, "%s\n", sb->s_type->name);
177 }
178
179 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
180                          int *eof, void *data)
181 {
182         struct super_block *sb = (struct super_block *)data;
183
184         LASSERT(sb != NULL);
185         *eof = 1;
186         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
187 }
188
189 static int ll_rd_read_ahead(char *page, char **start, off_t off, int count,
190                             int *eof, void *data)
191 {
192         struct super_block *sb = (struct super_block*)data;
193         struct ll_sb_info *sbi = ll_s2sbi(sb);
194         int val, rc;
195         ENTRY;
196
197         *eof = 1;
198         val = (sbi->ll_flags & LL_SBI_READAHEAD) ? 1 : 0;
199         rc = snprintf(page, count, "%d\n", val);
200         RETURN(rc);
201 }
202
203 static int ll_wr_read_ahead(struct file *file, const char *buffer,
204                             unsigned long count, void *data)
205 {
206         struct super_block *sb = (struct super_block*)data;
207         struct ll_sb_info *sbi = ll_s2sbi(sb);
208         int readahead;
209         ENTRY;
210         
211         if (sscanf(buffer, "%d", &readahead) != 1)
212                 RETURN(-EINVAL);
213
214         if (readahead)
215                 sbi->ll_flags |= LL_SBI_READAHEAD;
216         else
217                 sbi->ll_flags &= ~LL_SBI_READAHEAD;
218
219         RETURN(count);
220 }
221
222 static int ll_wr_config_update(struct file *file, const char *buffer,
223                                unsigned long count, void *data)
224 {
225         struct super_block *sb = (struct super_block*)data;
226         struct ll_sb_info *sbi = ll_s2sbi(sb);
227         ENTRY;
228
229         CWARN("Starting a LOV/OST update !\n");
230         RETURN(ll_process_config_update(sbi, 0));
231 }
232
233 static int ll_rd_max_read_ahead_mb(char *page, char **start, off_t off,
234                                    int count, int *eof, void *data)
235 {
236         struct super_block *sb = data;
237         struct ll_sb_info *sbi = ll_s2sbi(sb);
238         unsigned val;
239
240         spin_lock(&sbi->ll_lock);
241         val = (sbi->ll_ra_info.ra_max_pages << PAGE_CACHE_SHIFT) >> 20;
242         spin_unlock(&sbi->ll_lock);
243
244         return snprintf(page, count, "%u\n", val);
245 }
246
247 static int ll_wr_max_read_ahead_mb(struct file *file, const char *buffer,
248                                    unsigned long count, void *data)
249 {
250         struct super_block *sb = data;
251         struct ll_sb_info *sbi = ll_s2sbi(sb);
252         int val, rc;
253
254         rc = lprocfs_write_helper(buffer, count, &val);
255         if (rc)
256                 return rc;
257
258         if (val < 0 || val > (num_physpages << PAGE_SHIFT) >> 20)
259                 return -ERANGE;
260
261         spin_lock(&sbi->ll_lock);
262         sbi->ll_ra_info.ra_max_pages = (val << 20) >> PAGE_CACHE_SHIFT;
263         spin_unlock(&sbi->ll_lock);
264
265         return count;
266 }
267
268 static int ll_rd_gns_upcall(char *page, char **start, off_t off,
269                             int count, int *eof, void *data)
270 {
271         struct super_block *sb = (struct super_block *)data;
272         struct ll_sb_info *sbi = ll_s2sbi(sb);
273         int len;
274
275         down(&sbi->ll_gns_sem);
276         len = snprintf(page, count, "%s\n", sbi->ll_gns_upcall);
277         up(&sbi->ll_gns_sem);
278
279         return len;
280 }
281
282 static int ll_wr_gns_upcall(struct file *file, const char *buffer,
283                             unsigned long count, void *data)
284 {
285         struct super_block *sb = (struct super_block *)data;
286         struct ll_sb_info *sbi = ll_s2sbi(sb);
287
288         down(&sbi->ll_gns_sem);
289         snprintf(sbi->ll_gns_upcall, count, "%s", buffer);
290         up(&sbi->ll_gns_sem);
291
292         return count;
293 }
294
295 static int ll_rd_gns_object_name(char *page, char **start, off_t off,
296                                  int count, int *eof, void *data)
297 {
298         struct super_block *sb = (struct super_block *)data;
299         struct ll_sb_info *sbi = ll_s2sbi(sb);
300         int len;
301
302         down(&sbi->ll_gns_sem);
303         len = snprintf(page, count, "%s\n", sbi->ll_gns_oname);
304         up(&sbi->ll_gns_sem);
305
306         return len;
307 }
308
309 static int ll_wr_gns_object_name(struct file *file, const char *buffer,
310                                  unsigned long count, void *data)
311 {
312         struct super_block *sb = (struct super_block *)data;
313         struct ll_sb_info *sbi = ll_s2sbi(sb);
314
315         down(&sbi->ll_gns_sem);
316         snprintf(sbi->ll_gns_oname, count, "%s", buffer);
317         up(&sbi->ll_gns_sem);
318
319         return count;
320 }
321
322 static int ll_rd_gns_timeout(char *page, char **start, off_t off,
323                              int count, int *eof, void *data)
324 {
325         struct super_block *sb = (struct super_block *)data;
326         struct ll_sb_info *sbi = ll_s2sbi(sb);
327         int len;
328
329         down(&sbi->ll_gns_sem);
330         len = snprintf(page, count, "%lu\n",
331                        (unsigned long)sbi->ll_gns_timeout);
332         up(&sbi->ll_gns_sem);
333
334         return len;
335 }
336
337 static int ll_wr_gns_timeout(struct file *file, const char *buffer,
338                              unsigned long count, void *data)
339 {
340         struct super_block *sb = (struct super_block *)data;
341         struct ll_sb_info *sbi = ll_s2sbi(sb);
342         int val, rc;
343
344         rc = lprocfs_write_helper(buffer, count, &val);
345         if (rc)
346                 return rc;
347
348         down(&sbi->ll_gns_sem);
349         sbi->ll_gns_timeout = val;
350         up(&sbi->ll_gns_sem);
351
352         return count;
353 }
354
355 static int ll_rd_gns_tick(char *page, char **start, off_t off,
356                           int count, int *eof, void *data)
357 {
358         struct super_block *sb = (struct super_block *)data;
359         struct ll_sb_info *sbi = ll_s2sbi(sb);
360         int len;
361
362         down(&sbi->ll_gns_sem);
363         len = snprintf(page, count, "%lu\n",
364                        (unsigned long)sbi->ll_gns_tick);
365         up(&sbi->ll_gns_sem);
366
367         return len;
368 }
369
370 static int ll_wr_gns_tick(struct file *file, const char *buffer,
371                           unsigned long count, void *data)
372 {
373         struct super_block *sb = (struct super_block *)data;
374         struct ll_sb_info *sbi = ll_s2sbi(sb);
375         int val, rc;
376
377         rc = lprocfs_write_helper(buffer, count, &val);
378         if (rc)
379                 return rc;
380
381         down(&sbi->ll_gns_sem);
382         if (sbi->ll_gns_tick < sbi->ll_gns_timeout)
383                 sbi->ll_gns_tick = val;
384         up(&sbi->ll_gns_sem);
385
386         return count;
387 }
388 static struct lprocfs_vars lprocfs_obd_vars[] = {
389         { "uuid",         ll_rd_sb_uuid,          0, 0 },
390         //{ "mntpt_path",   ll_rd_path,             0, 0 },
391         { "fstype",       ll_rd_fstype,           0, 0 },
392         { "blocksize",    ll_rd_blksize,          0, 0 },
393         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
394         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
395         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
396         { "filestotal",   ll_rd_filestotal,       0, 0 },
397         { "filesfree",    ll_rd_filesfree,        0, 0 },
398         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
399         { "read_ahead",   ll_rd_read_ahead, ll_wr_read_ahead, 0 },
400         { "config_update", 0, ll_wr_config_update, 0 },
401         { "max_read_ahead_mb", ll_rd_max_read_ahead_mb,
402                                ll_wr_max_read_ahead_mb, 0 },
403
404         { "gns_upcall", ll_rd_gns_upcall,
405           ll_wr_gns_upcall, 0 },
406         
407         { "gns_timeout", ll_rd_gns_timeout,
408           ll_wr_gns_timeout, 0 },
409         
410         { "gns_tick", ll_rd_gns_tick,
411           ll_wr_gns_tick, 0 },
412         
413         { "gns_object_name", ll_rd_gns_object_name,
414           ll_wr_gns_object_name, 0 },
415         
416         { 0 }
417 };
418
419 #define MAX_STRING_SIZE 128
420
421 struct llite_file_opcode {
422         __u32       opcode;
423         __u32       type;
424         const char *opname;
425 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
426         /* file operation */
427         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
428         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
429         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
430                                    "writeback_from_writepage" },
431         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
432                                    "writeback_from_pressure" },
433         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
434                                    "writeback_ok_pages" },
435         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
436                                    "writeback_failed_pages" },
437         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
438                                    "read_bytes" },
439         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
440                                    "write_bytes" },
441         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
442                                    "brw_read" },
443         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
444                                    "brw_write" },
445
446         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
447         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
448         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
449         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
450         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
451         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
452         /* inode operation */
453         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
454         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "punch" },
455 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
456         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
457 #else
458         { LPROC_LL_REVALIDATE,     LPROCFS_TYPE_REGS, "getattr" },
459 #endif
460         /* special inode operation */
461         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
462         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
463         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
464                                    "direct_read" },
465         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
466                                    "direct_write" },
467         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
468         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
469 };
470
471 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
472                                 struct super_block *sb, char *osc, char *mdc)
473 {
474         struct lprocfs_vars lvars[2];
475         struct ll_sb_info *sbi = ll_s2sbi(sb);
476         struct obd_device *obd;
477         char name[MAX_STRING_SIZE + 1];
478         int err, id;
479         struct lprocfs_stats *svc_stats = NULL;
480         struct proc_dir_entry *mdc_symlink, *osc_symlink;
481         struct proc_dir_entry *entry;
482         ENTRY;
483
484         memset(lvars, 0, sizeof(lvars));
485
486         name[MAX_STRING_SIZE] = '\0';
487         lvars[0].name = name;
488
489         LASSERT(sbi != NULL);
490         LASSERT(mdc != NULL);
491         LASSERT(osc != NULL);
492
493         /* Mount info */
494         snprintf(name, MAX_STRING_SIZE, "fs%llu", mnt_instance);
495
496         mnt_instance++;
497         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
498         if (IS_ERR(sbi->ll_proc_root)) {
499                 err = PTR_ERR(sbi->ll_proc_root);
500                 sbi->ll_proc_root = NULL;
501                 RETURN(err);
502         }
503
504         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
505         if (entry == NULL)
506                 GOTO(out, err = -ENOMEM);
507         entry->proc_fops = &llite_dump_pgcache_fops;
508         entry->data = sbi;
509
510         entry = create_proc_entry("wait_times", 0444, sbi->ll_proc_root);
511         if (entry == NULL)
512                 GOTO(out, err = -ENOMEM);
513         entry->proc_fops = &llite_wait_times_fops;
514         entry->data = sbi;
515
516         entry = create_proc_entry("read_ahead_stats", 0444, sbi->ll_proc_root);
517         if (entry == NULL)
518                 GOTO(out, err = -ENOMEM);
519         entry->proc_fops = &ll_ra_stats_fops;
520         entry->data = sbi;
521
522         svc_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES);
523         if (svc_stats == NULL) {
524                 err = -ENOMEM;
525                 goto out;
526         }
527         /* do counter init */
528         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
529                 __u32 type = llite_opcode_table[id].type;
530                 void *ptr = NULL;
531                 if (type & LPROCFS_TYPE_REGS)
532                         ptr = "regs";
533                 else {
534                         if (type & LPROCFS_TYPE_BYTES)
535                                 ptr = "bytes";
536                         else {
537                                 if (type & LPROCFS_TYPE_PAGES)
538                                         ptr = "pages";
539                         }
540                 }
541                 lprocfs_counter_init(svc_stats, llite_opcode_table[id].opcode,
542                                      (type & LPROCFS_CNTR_AVGMINMAX),
543                                      llite_opcode_table[id].opname, ptr);
544         }
545         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", svc_stats);
546         if (err)
547                 goto out;
548         else
549                 sbi->ll_stats = svc_stats;
550         /* need place to keep svc_stats */
551
552         /* Static configuration info */
553         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_obd_vars, sb);
554         if (err)
555                 goto out;
556
557         /* MDC info */
558         obd = class_name2obd(mdc);
559
560         LASSERT(obd != NULL);
561         LASSERT(obd->obd_type != NULL);
562         LASSERT(obd->obd_type->typ_name != NULL);
563
564         snprintf(name, MAX_STRING_SIZE, "../../%s/%s",
565                  obd->obd_type->typ_name, obd->obd_name);
566         mdc_symlink = proc_symlink(obd->obd_type->typ_name, sbi->ll_proc_root,
567                                    name);
568         if (mdc_symlink == NULL) {
569                 err = -ENOMEM;
570                 goto out;
571         }
572
573         /* OSC */
574         obd = class_name2obd(osc);
575
576         LASSERT(obd != NULL);
577         LASSERT(obd->obd_type != NULL);
578         LASSERT(obd->obd_type->typ_name != NULL);
579
580        snprintf(name, MAX_STRING_SIZE, "../../%s/%s",
581                 obd->obd_type->typ_name, obd->obd_name);
582        osc_symlink = proc_symlink(obd->obd_type->typ_name, sbi->ll_proc_root,
583                                   name);
584        if (osc_symlink == NULL)
585                err = -ENOMEM;
586
587
588 out:
589         if (err) {
590                 if (svc_stats)
591                         lprocfs_free_stats(svc_stats);
592                 if (sbi->ll_proc_root)
593                         lprocfs_remove(sbi->ll_proc_root);
594         }
595         RETURN(err);
596 }
597
598 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
599 {
600         if (sbi->ll_proc_root) {
601                 struct proc_dir_entry *file_stats =
602                         lprocfs_srch(sbi->ll_proc_root, "stats");
603
604                 if (file_stats) {
605                         lprocfs_free_stats(sbi->ll_stats);
606                         lprocfs_remove(file_stats);
607                 }
608         }
609 }
610 #undef MAX_STRING_SIZE
611
612 static struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
613                                                     struct list_head *list)
614 {
615         struct ll_async_page *llap;
616         struct list_head *pos;
617
618         list_for_each(pos, list) {
619                 if (pos == &sbi->ll_pglist)
620                         return NULL;
621                 llap = list_entry(pos, struct ll_async_page, llap_proc_item);
622                 if (llap->llap_page == NULL)
623                         continue;
624                 return llap;
625         }
626         LBUG();
627         return NULL;
628 }
629
630 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
631                 if (test_bit(PG_##flag, &(page)->flags)) {              \
632                         if (!has_flags)                                 \
633                                 has_flags = 1;                          \
634                         else                                            \
635                                 seq_putc(seq, '|');                     \
636                         seq_puts(seq, #flag);                           \
637                 }                                                       \
638         } while(0);
639
640 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
641 {
642         struct ll_async_page *llap, *dummy_llap = seq->private;
643         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
644
645         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
646          * it in our own state */
647         if (dummy_llap->llap_magic == 0) {
648                 seq_printf(seq, "generation | llap cookie origin | page ");
649                 seq_printf(seq, "inode index count [ page flags ]\n");
650                 return 0;
651         }
652
653         spin_lock(&sbi->ll_lock);
654
655         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
656         if (llap != NULL)  {
657                 int has_flags = 0;
658                 struct page *page = llap->llap_page;
659                 static char *origins[] = {
660                         [LLAP_ORIGIN_UNKNOWN] = "--",
661                         [LLAP_ORIGIN_READPAGE] = "rp",
662                         [LLAP_ORIGIN_READAHEAD] = "ra",
663                         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
664                         [LLAP_ORIGIN_WRITEPAGE] = "wp",
665                 };
666
667                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
668                          llap->llap_origin);
669
670                 seq_printf(seq, "%lu | %p %p %s | %p %p %lu %u [",
671                                 sbi->ll_pglist_gen,
672                                 llap, llap->llap_cookie,
673                                 origins[llap->llap_origin],
674                                 page, page->mapping->host, page->index,
675                                 page_count(page));
676                 seq_page_flag(seq, page, locked, has_flags);
677                 seq_page_flag(seq, page, error, has_flags);
678                 seq_page_flag(seq, page, referenced, has_flags);
679                 seq_page_flag(seq, page, uptodate, has_flags);
680                 seq_page_flag(seq, page, dirty, has_flags);
681                 seq_page_flag(seq, page, highmem, has_flags);
682                 if (!has_flags)
683                         seq_puts(seq, "-]\n");
684                 else
685                         seq_puts(seq, "]\n");
686         }
687
688         spin_unlock(&sbi->ll_lock);
689
690         return 0;
691 }
692
693 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
694 {
695         struct ll_async_page *llap = seq->private;
696
697         if (llap->llap_magic == 2)
698                 return NULL;
699
700         return (void *)1;
701 }
702
703 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v,
704                                          loff_t *pos)
705 {
706         struct ll_async_page *llap, *dummy_llap = seq->private;
707         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
708
709         /* bail if we just displayed the banner */
710         if (dummy_llap->llap_magic == 0) {
711                 dummy_llap->llap_magic = 1;
712                 return dummy_llap;
713         }
714
715         /* we've just displayed the llap that is after us in the list.
716          * we advance to a position beyond it, returning null if there
717          * isn't another llap in the list beyond that new position. */
718         spin_lock(&sbi->ll_lock);
719         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
720         list_del_init(&dummy_llap->llap_proc_item);
721         if (llap) {
722                 list_add(&dummy_llap->llap_proc_item, &llap->llap_proc_item);
723                 llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
724         }
725         spin_unlock(&sbi->ll_lock);
726
727         ++*pos;
728         if (llap == NULL) {
729                 dummy_llap->llap_magic = 2;
730                 return NULL;
731         }
732         return dummy_llap;
733 }
734
735 static void llite_dump_pgcache_seq_stop(struct seq_file *seq, void *v)
736 {
737 }
738
739 struct seq_operations llite_dump_pgcache_seq_sops = {
740         .start = llite_dump_pgcache_seq_start,
741         .stop = llite_dump_pgcache_seq_stop,
742         .next = llite_dump_pgcache_seq_next,
743         .show = llite_dump_pgcache_seq_show,
744 };
745
746 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
747  * while we walk the entire list, and we don't want to have to seek into
748  * the right position in the list as an app advances with many syscalls.  we
749  * allocate a dummy llap and hang it off file->private.  its position in
750  * the list records where the app is currently displaying.  this way our
751  * seq .start and .stop don't actually do anything.  .next returns null
752  * when the dummy hits the end of the list which eventually leads to .release
753  * where we tear down.  this kind of displaying is super-racey, so we put
754  * a generation counter on the list so the output shows when the list
755  * changes between reads.
756  */
757 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
758 {
759         struct proc_dir_entry *dp = PDE(inode);
760         struct ll_async_page *llap;
761         struct seq_file *seq;
762         struct ll_sb_info *sbi = dp->data;
763         int rc;
764
765         OBD_ALLOC_GFP(llap, sizeof(*llap), GFP_KERNEL);
766         if (llap == NULL)
767                 return -ENOMEM;
768         llap->llap_page = NULL;
769         llap->llap_cookie = sbi;
770         llap->llap_magic = 0;
771
772         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
773         if (rc) {
774                 OBD_FREE(llap, sizeof(*llap));
775                 return rc;
776         }
777         seq = file->private_data;
778         seq->private = llap;
779
780         spin_lock(&sbi->ll_lock);
781         list_add(&llap->llap_proc_item, &sbi->ll_pglist);
782         spin_unlock(&sbi->ll_lock);
783
784         return 0;
785 }
786
787 static int llite_dump_pgcache_seq_release(struct inode *inode,
788                                           struct file *file)
789 {
790         struct seq_file *seq = file->private_data;
791         struct ll_async_page *llap = seq->private;
792         struct ll_sb_info *sbi = llap->llap_cookie;
793
794         spin_lock(&sbi->ll_lock);
795         if (!list_empty(&llap->llap_proc_item))
796                 list_del_init(&llap->llap_proc_item);
797         spin_unlock(&sbi->ll_lock);
798         OBD_FREE(llap, sizeof(*llap));
799
800         return seq_release(inode, file);
801 }
802
803 struct file_operations llite_dump_pgcache_fops = {
804         .owner   = THIS_MODULE,
805         .open    = llite_dump_pgcache_seq_open,
806         .read    = seq_read,
807         .release = llite_dump_pgcache_seq_release,
808 };
809 static int ll_ra_stats_seq_show(struct seq_file *seq, void *v)
810 {
811         struct timeval now;
812         struct ll_sb_info *sbi = seq->private;
813         struct ll_ra_info *ra = &sbi->ll_ra_info;
814         int i;
815         static char *ra_stat_strings[] = {
816                 [RA_STAT_HIT] = "hits",
817                 [RA_STAT_MISS] = "misses",
818                 [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
819                 [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
820                 [RA_STAT_FAILED_MATCH] = "failed lock match",
821                 [RA_STAT_DISCARDED] = "read but discarded",
822                 [RA_STAT_ZERO_LEN] = "zero length file",
823                 [RA_STAT_ZERO_WINDOW] = "zero size window",
824                 [RA_STAT_EOF] = "read-ahead to EOF",
825                 [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
826         };
827
828         do_gettimeofday(&now);
829
830         spin_lock(&sbi->ll_lock);
831
832         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
833                    now.tv_sec, now.tv_usec);
834         seq_printf(seq, "pending issued pages:           %lu\n",
835                    ra->ra_cur_pages);
836
837         for(i = 0; i < _NR_RA_STAT; i++)
838                 seq_printf(seq, "%-25s %lu\n", ra_stat_strings[i],
839                            ra->ra_stats[i]);
840
841         spin_unlock(&sbi->ll_lock);
842
843         return 0;
844 }
845
846 static void *ll_ra_stats_seq_start(struct seq_file *p, loff_t *pos)
847 {
848         if (*pos == 0)
849                 return (void *)1;
850         return NULL;
851 }
852 static void *ll_ra_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
853 {
854         ++*pos;
855         return NULL;
856 }
857 static void ll_ra_stats_seq_stop(struct seq_file *p, void *v)
858 {
859 }
860 struct seq_operations ll_ra_stats_seq_sops = {
861         .start = ll_ra_stats_seq_start,
862         .stop = ll_ra_stats_seq_stop,
863         .next = ll_ra_stats_seq_next,
864         .show = ll_ra_stats_seq_show,
865 };
866
867 static int ll_ra_stats_seq_open(struct inode *inode, struct file *file)
868 {
869         struct proc_dir_entry *dp = PDE(inode);
870         struct seq_file *seq;
871         int rc;
872
873         rc = seq_open(file, &ll_ra_stats_seq_sops);
874         if (rc)
875                 return rc;
876         seq = file->private_data;
877         seq->private = dp->data;
878         return 0;
879 }
880
881 static ssize_t ll_ra_stats_seq_write(struct file *file, const char *buf,
882                                        size_t len, loff_t *off)
883 {
884         struct seq_file *seq = file->private_data;
885         struct ll_sb_info *sbi = seq->private;
886         struct ll_ra_info *ra = &sbi->ll_ra_info;
887
888         spin_lock(&sbi->ll_lock);
889         memset(ra->ra_stats, 0, sizeof(ra->ra_stats));
890         spin_unlock(&sbi->ll_lock);
891
892         return len;
893 }
894
895 struct file_operations ll_ra_stats_fops = {
896         .owner   = THIS_MODULE,
897         .open    = ll_ra_stats_seq_open,
898         .read    = seq_read,
899         .write   = ll_ra_stats_seq_write,
900         .llseek  = seq_lseek,
901         .release = seq_release,
902 };
903
904 #define PRINTF_STIME(stime) (unsigned long)(stime)->st_num,     \
905         lprocfs_stime_avg_ms(stime), lprocfs_stime_avg_us(stime)
906
907 static int llite_wait_times_seq_show(struct seq_file *seq, void *v)
908 {
909         struct ll_sb_info *sbi = seq->private;
910         struct timeval now;
911
912         do_gettimeofday(&now);
913
914         spin_lock(&sbi->ll_lock);
915
916         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n\n",
917                    now.tv_sec, now.tv_usec);
918
919         seq_printf(seq, "lock wait times: (num, average ms)\n");
920         seq_printf(seq, "\tread\t%lu\t%lu.%04lu\n",
921                         PRINTF_STIME(&sbi->ll_read_stime));
922         seq_printf(seq, "\twrite\t%lu\t%lu.%04lu\n",
923                         PRINTF_STIME(&sbi->ll_write_stime));
924         seq_printf(seq, "\tgroup\t%lu\t%lu.%04lu\n",
925                         PRINTF_STIME(&sbi->ll_grouplock_stime));
926         seq_printf(seq, "\tseek\t%lu\t%lu.%04lu\n",
927                         PRINTF_STIME(&sbi->ll_seek_stime));
928         seq_printf(seq, "\tsetattr\t%lu\t%lu.%04lu\n\n",
929                         PRINTF_STIME(&sbi->ll_setattr_stime));
930
931         seq_printf(seq, "io path wait times: (num, average ms)\n");
932         seq_printf(seq, "\tll_brw\t%lu\t%lu.%04lu\n",
933                         PRINTF_STIME(&sbi->ll_brw_stime));
934 #if 0
935         seq_printf(seq, "\tdone\t%lu\t%lu.%04lu\n",
936                         PRINTF_STIME(&sbi->ll_done_stime));
937 #endif
938
939         spin_unlock(&sbi->ll_lock);
940
941         return 0;
942 }
943 #undef pct
944
945 static void *llite_wait_times_seq_start(struct seq_file *p, loff_t *pos)
946 {
947         if (*pos == 0)
948                 return (void *)1;
949         return NULL;
950 }
951 static void *llite_wait_times_seq_next(struct seq_file *p, void *v, loff_t *pos)
952 {
953         ++*pos;
954         return NULL;
955 }
956 static void llite_wait_times_seq_stop(struct seq_file *p, void *v)
957 {
958 }
959 struct seq_operations llite_wait_times_seq_sops = {
960         .start = llite_wait_times_seq_start,
961         .stop = llite_wait_times_seq_stop,
962         .next = llite_wait_times_seq_next,
963         .show = llite_wait_times_seq_show,
964 };
965
966 static int llite_wait_times_seq_open(struct inode *inode, struct file *file)
967 {
968         struct proc_dir_entry *dp = PDE(inode);
969         struct seq_file *seq;
970         int rc;
971
972         rc = seq_open(file, &llite_wait_times_seq_sops);
973         if (rc)
974                 return rc;
975         seq = file->private_data;
976         seq->private = dp->data;
977         return 0;
978 }
979
980 static ssize_t llite_wait_times_seq_write(struct file *file, const char *buf,
981                                        size_t len, loff_t *off)
982 {
983         struct seq_file *seq = file->private_data;
984         struct ll_sb_info *sbi = seq->private;
985
986         spin_lock(&sbi->ll_lock);
987         memset(&sbi->ll_read_stime, 0, sizeof(sbi->ll_read_stime));
988         memset(&sbi->ll_write_stime, 0, sizeof(sbi->ll_write_stime));
989         memset(&sbi->ll_grouplock_stime, 0, sizeof(sbi->ll_grouplock_stime));
990         memset(&sbi->ll_seek_stime, 0, sizeof(sbi->ll_seek_stime));
991         memset(&sbi->ll_setattr_stime, 0, sizeof(sbi->ll_setattr_stime));
992         memset(&sbi->ll_brw_stime, 0, sizeof(sbi->ll_brw_stime));
993 //        memset(&sbi->ll_done_stime, 0, sizeof(sbi->ll_done_stime));
994         spin_unlock(&sbi->ll_lock);
995
996         return len;
997 }
998
999 struct file_operations llite_wait_times_fops = {
1000         .owner   = THIS_MODULE,
1001         .open    = llite_wait_times_seq_open,
1002         .read    = seq_read,
1003         .write   = llite_wait_times_seq_write,
1004         .llseek  = seq_lseek,
1005         .release = seq_release,
1006 };
1007
1008 #endif /* LPROCFS */