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