Whamcloud - gitweb
- added comment in mds_open() and GNS mount points.
[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         /* checking for setting "." and ".." as object name */
316         if (buffer[0] == '.') switch (count) {
317                 case 2:
318                         /* this is "." case with terminating zero */
319                         return -EINVAL;
320                 case 3:
321                         /* this is ".." case with terminating zero */
322                         if (buffer[1] == '.')
323                                 return -EINVAL;
324         }
325         
326         down(&sbi->ll_gns_sem);
327         snprintf(sbi->ll_gns_oname, count, "%s", buffer);
328         up(&sbi->ll_gns_sem);
329
330         return count;
331 }
332
333 static int ll_rd_gns_timeout(char *page, char **start, off_t off,
334                              int count, int *eof, void *data)
335 {
336         struct super_block *sb = (struct super_block *)data;
337         struct ll_sb_info *sbi = ll_s2sbi(sb);
338         int len;
339
340         down(&sbi->ll_gns_sem);
341         len = snprintf(page, count, "%lu\n",
342                        (unsigned long)sbi->ll_gns_timeout);
343         up(&sbi->ll_gns_sem);
344
345         return len;
346 }
347
348 static int ll_wr_gns_timeout(struct file *file, const char *buffer,
349                              unsigned long count, void *data)
350 {
351         struct super_block *sb = (struct super_block *)data;
352         struct ll_sb_info *sbi = ll_s2sbi(sb);
353         int val, rc;
354
355         rc = lprocfs_write_helper(buffer, count, &val);
356         if (rc)
357                 return rc;
358
359         down(&sbi->ll_gns_sem);
360         sbi->ll_gns_timeout = val;
361         up(&sbi->ll_gns_sem);
362
363         return count;
364 }
365
366 static int ll_rd_gns_tick(char *page, char **start, off_t off,
367                           int count, int *eof, void *data)
368 {
369         struct super_block *sb = (struct super_block *)data;
370         struct ll_sb_info *sbi = ll_s2sbi(sb);
371         int len;
372
373         down(&sbi->ll_gns_sem);
374         len = snprintf(page, count, "%lu\n",
375                        (unsigned long)sbi->ll_gns_tick);
376         up(&sbi->ll_gns_sem);
377
378         return len;
379 }
380
381 static int ll_wr_gns_tick(struct file *file, const char *buffer,
382                           unsigned long count, void *data)
383 {
384         struct super_block *sb = (struct super_block *)data;
385         struct ll_sb_info *sbi = ll_s2sbi(sb);
386         int val, rc;
387
388         rc = lprocfs_write_helper(buffer, count, &val);
389         if (rc)
390                 return rc;
391
392         down(&sbi->ll_gns_sem);
393         if (sbi->ll_gns_tick < sbi->ll_gns_timeout)
394                 sbi->ll_gns_tick = val;
395         up(&sbi->ll_gns_sem);
396
397         return count;
398 }
399 static struct lprocfs_vars lprocfs_obd_vars[] = {
400         { "uuid",         ll_rd_sb_uuid,          0, 0 },
401         //{ "mntpt_path",   ll_rd_path,             0, 0 },
402         { "fstype",       ll_rd_fstype,           0, 0 },
403         { "blocksize",    ll_rd_blksize,          0, 0 },
404         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
405         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
406         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
407         { "filestotal",   ll_rd_filestotal,       0, 0 },
408         { "filesfree",    ll_rd_filesfree,        0, 0 },
409         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
410         { "read_ahead",   ll_rd_read_ahead, ll_wr_read_ahead, 0 },
411         { "config_update", 0, ll_wr_config_update, 0 },
412         { "max_read_ahead_mb", ll_rd_max_read_ahead_mb,
413                                ll_wr_max_read_ahead_mb, 0 },
414
415         { "gns_upcall", ll_rd_gns_upcall,
416           ll_wr_gns_upcall, 0 },
417         
418         { "gns_timeout", ll_rd_gns_timeout,
419           ll_wr_gns_timeout, 0 },
420         
421         { "gns_tick", ll_rd_gns_tick,
422           ll_wr_gns_tick, 0 },
423         
424         { "gns_object_name", ll_rd_gns_object_name,
425           ll_wr_gns_object_name, 0 },
426         
427         { 0 }
428 };
429
430 #define MAX_STRING_SIZE 128
431
432 struct llite_file_opcode {
433         __u32       opcode;
434         __u32       type;
435         const char *opname;
436 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
437         /* file operation */
438         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
439         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
440         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
441                                    "writeback_from_writepage" },
442         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
443                                    "writeback_from_pressure" },
444         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
445                                    "writeback_ok_pages" },
446         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
447                                    "writeback_failed_pages" },
448         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
449                                    "read_bytes" },
450         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
451                                    "write_bytes" },
452         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
453                                    "brw_read" },
454         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
455                                    "brw_write" },
456
457         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
458         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
459         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
460         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
461         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
462         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
463         /* inode operation */
464         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
465         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "punch" },
466 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
467         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
468 #else
469         { LPROC_LL_REVALIDATE,     LPROCFS_TYPE_REGS, "getattr" },
470 #endif
471         /* special inode operation */
472         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
473         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
474         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
475                                    "direct_read" },
476         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
477                                    "direct_write" },
478         { LPROC_LL_SETXATTR,       LPROCFS_TYPE_REGS, "setxattr" },
479         { LPROC_LL_GETXATTR,       LPROCFS_TYPE_REGS, "getxattr" },
480 };
481
482 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
483                                 struct super_block *sb, char *osc, char *mdc)
484 {
485         struct lprocfs_vars lvars[2];
486         struct ll_sb_info *sbi = ll_s2sbi(sb);
487         struct obd_device *obd;
488         char name[MAX_STRING_SIZE + 1];
489         int err, id;
490         struct lprocfs_stats *svc_stats = NULL;
491         struct proc_dir_entry *mdc_symlink, *osc_symlink;
492         struct proc_dir_entry *entry;
493         ENTRY;
494
495         memset(lvars, 0, sizeof(lvars));
496
497         name[MAX_STRING_SIZE] = '\0';
498         lvars[0].name = name;
499
500         LASSERT(sbi != NULL);
501         LASSERT(mdc != NULL);
502         LASSERT(osc != NULL);
503
504         /* Mount info */
505         snprintf(name, MAX_STRING_SIZE, "fs%llu", mnt_instance);
506
507         mnt_instance++;
508         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
509         if (IS_ERR(sbi->ll_proc_root)) {
510                 err = PTR_ERR(sbi->ll_proc_root);
511                 sbi->ll_proc_root = NULL;
512                 RETURN(err);
513         }
514
515         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
516         if (entry == NULL)
517                 GOTO(out, err = -ENOMEM);
518         entry->proc_fops = &llite_dump_pgcache_fops;
519         entry->data = sbi;
520
521         entry = create_proc_entry("wait_times", 0444, sbi->ll_proc_root);
522         if (entry == NULL)
523                 GOTO(out, err = -ENOMEM);
524         entry->proc_fops = &llite_wait_times_fops;
525         entry->data = sbi;
526
527         entry = create_proc_entry("read_ahead_stats", 0444, sbi->ll_proc_root);
528         if (entry == NULL)
529                 GOTO(out, err = -ENOMEM);
530         entry->proc_fops = &ll_ra_stats_fops;
531         entry->data = sbi;
532
533         svc_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES);
534         if (svc_stats == NULL) {
535                 err = -ENOMEM;
536                 goto out;
537         }
538         /* do counter init */
539         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
540                 __u32 type = llite_opcode_table[id].type;
541                 void *ptr = NULL;
542                 if (type & LPROCFS_TYPE_REGS)
543                         ptr = "regs";
544                 else {
545                         if (type & LPROCFS_TYPE_BYTES)
546                                 ptr = "bytes";
547                         else {
548                                 if (type & LPROCFS_TYPE_PAGES)
549                                         ptr = "pages";
550                         }
551                 }
552                 lprocfs_counter_init(svc_stats, llite_opcode_table[id].opcode,
553                                      (type & LPROCFS_CNTR_AVGMINMAX),
554                                      llite_opcode_table[id].opname, ptr);
555         }
556         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", svc_stats);
557         if (err)
558                 goto out;
559         else
560                 sbi->ll_stats = svc_stats;
561         /* need place to keep svc_stats */
562
563         /* Static configuration info */
564         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_obd_vars, sb);
565         if (err)
566                 goto out;
567
568         /* MDC info */
569         obd = class_name2obd(mdc);
570
571         LASSERT(obd != NULL);
572         LASSERT(obd->obd_type != NULL);
573         LASSERT(obd->obd_type->typ_name != NULL);
574
575         snprintf(name, MAX_STRING_SIZE, "../../%s/%s",
576                  obd->obd_type->typ_name, obd->obd_name);
577         mdc_symlink = proc_symlink(obd->obd_type->typ_name, sbi->ll_proc_root,
578                                    name);
579         if (mdc_symlink == NULL) {
580                 err = -ENOMEM;
581                 goto out;
582         }
583
584         /* OSC */
585         obd = class_name2obd(osc);
586
587         LASSERT(obd != NULL);
588         LASSERT(obd->obd_type != NULL);
589         LASSERT(obd->obd_type->typ_name != NULL);
590
591        snprintf(name, MAX_STRING_SIZE, "../../%s/%s",
592                 obd->obd_type->typ_name, obd->obd_name);
593        osc_symlink = proc_symlink(obd->obd_type->typ_name, sbi->ll_proc_root,
594                                   name);
595        if (osc_symlink == NULL)
596                err = -ENOMEM;
597
598
599 out:
600         if (err) {
601                 if (svc_stats)
602                         lprocfs_free_stats(svc_stats);
603                 if (sbi->ll_proc_root)
604                         lprocfs_remove(sbi->ll_proc_root);
605         }
606         RETURN(err);
607 }
608
609 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
610 {
611         if (sbi->ll_proc_root) {
612                 struct proc_dir_entry *file_stats =
613                         lprocfs_srch(sbi->ll_proc_root, "stats");
614
615                 if (file_stats) {
616                         lprocfs_free_stats(sbi->ll_stats);
617                         lprocfs_remove(file_stats);
618                 }
619         }
620 }
621 #undef MAX_STRING_SIZE
622
623 static struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
624                                                     struct list_head *list)
625 {
626         struct ll_async_page *llap;
627         struct list_head *pos;
628
629         list_for_each(pos, list) {
630                 if (pos == &sbi->ll_pglist)
631                         return NULL;
632                 llap = list_entry(pos, struct ll_async_page, llap_proc_item);
633                 if (llap->llap_page == NULL)
634                         continue;
635                 return llap;
636         }
637         LBUG();
638         return NULL;
639 }
640
641 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
642                 if (test_bit(PG_##flag, &(page)->flags)) {              \
643                         if (!has_flags)                                 \
644                                 has_flags = 1;                          \
645                         else                                            \
646                                 seq_putc(seq, '|');                     \
647                         seq_puts(seq, #flag);                           \
648                 }                                                       \
649         } while(0);
650
651 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
652 {
653         struct ll_async_page *llap, *dummy_llap = seq->private;
654         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
655
656         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
657          * it in our own state */
658         if (dummy_llap->llap_magic == 0) {
659                 seq_printf(seq, "generation | llap cookie origin | page ");
660                 seq_printf(seq, "inode index count [ page flags ]\n");
661                 return 0;
662         }
663
664         spin_lock(&sbi->ll_lock);
665
666         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
667         if (llap != NULL)  {
668                 int has_flags = 0;
669                 struct page *page = llap->llap_page;
670                 static char *origins[] = {
671                         [LLAP_ORIGIN_UNKNOWN] = "--",
672                         [LLAP_ORIGIN_READPAGE] = "rp",
673                         [LLAP_ORIGIN_READAHEAD] = "ra",
674                         [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
675                         [LLAP_ORIGIN_WRITEPAGE] = "wp",
676                 };
677
678                 LASSERTF(llap->llap_origin < LLAP__ORIGIN_MAX, "%u\n",
679                          llap->llap_origin);
680
681                 seq_printf(seq, "%lu | %p %p %s | %p %p %lu %u [",
682                                 sbi->ll_pglist_gen,
683                                 llap, llap->llap_cookie,
684                                 origins[llap->llap_origin],
685                                 page, page->mapping->host, page->index,
686                                 page_count(page));
687                 seq_page_flag(seq, page, locked, has_flags);
688                 seq_page_flag(seq, page, error, has_flags);
689                 seq_page_flag(seq, page, referenced, has_flags);
690                 seq_page_flag(seq, page, uptodate, has_flags);
691                 seq_page_flag(seq, page, dirty, has_flags);
692                 seq_page_flag(seq, page, highmem, has_flags);
693                 if (!has_flags)
694                         seq_puts(seq, "-]\n");
695                 else
696                         seq_puts(seq, "]\n");
697         }
698
699         spin_unlock(&sbi->ll_lock);
700
701         return 0;
702 }
703
704 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
705 {
706         struct ll_async_page *llap = seq->private;
707
708         if (llap->llap_magic == 2)
709                 return NULL;
710
711         return (void *)1;
712 }
713
714 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v,
715                                          loff_t *pos)
716 {
717         struct ll_async_page *llap, *dummy_llap = seq->private;
718         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
719
720         /* bail if we just displayed the banner */
721         if (dummy_llap->llap_magic == 0) {
722                 dummy_llap->llap_magic = 1;
723                 return dummy_llap;
724         }
725
726         /* we've just displayed the llap that is after us in the list.
727          * we advance to a position beyond it, returning null if there
728          * isn't another llap in the list beyond that new position. */
729         spin_lock(&sbi->ll_lock);
730         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
731         list_del_init(&dummy_llap->llap_proc_item);
732         if (llap) {
733                 list_add(&dummy_llap->llap_proc_item, &llap->llap_proc_item);
734                 llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
735         }
736         spin_unlock(&sbi->ll_lock);
737
738         ++*pos;
739         if (llap == NULL) {
740                 dummy_llap->llap_magic = 2;
741                 return NULL;
742         }
743         return dummy_llap;
744 }
745
746 static void llite_dump_pgcache_seq_stop(struct seq_file *seq, void *v)
747 {
748 }
749
750 struct seq_operations llite_dump_pgcache_seq_sops = {
751         .start = llite_dump_pgcache_seq_start,
752         .stop = llite_dump_pgcache_seq_stop,
753         .next = llite_dump_pgcache_seq_next,
754         .show = llite_dump_pgcache_seq_show,
755 };
756
757 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
758  * while we walk the entire list, and we don't want to have to seek into
759  * the right position in the list as an app advances with many syscalls.  we
760  * allocate a dummy llap and hang it off file->private.  its position in
761  * the list records where the app is currently displaying.  this way our
762  * seq .start and .stop don't actually do anything.  .next returns null
763  * when the dummy hits the end of the list which eventually leads to .release
764  * where we tear down.  this kind of displaying is super-racey, so we put
765  * a generation counter on the list so the output shows when the list
766  * changes between reads.
767  */
768 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
769 {
770         struct proc_dir_entry *dp = PDE(inode);
771         struct ll_async_page *llap;
772         struct seq_file *seq;
773         struct ll_sb_info *sbi = dp->data;
774         int rc;
775
776         OBD_ALLOC_GFP(llap, sizeof(*llap), GFP_KERNEL);
777         if (llap == NULL)
778                 return -ENOMEM;
779         llap->llap_page = NULL;
780         llap->llap_cookie = sbi;
781         llap->llap_magic = 0;
782
783         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
784         if (rc) {
785                 OBD_FREE(llap, sizeof(*llap));
786                 return rc;
787         }
788         seq = file->private_data;
789         seq->private = llap;
790
791         spin_lock(&sbi->ll_lock);
792         list_add(&llap->llap_proc_item, &sbi->ll_pglist);
793         spin_unlock(&sbi->ll_lock);
794
795         return 0;
796 }
797
798 static int llite_dump_pgcache_seq_release(struct inode *inode,
799                                           struct file *file)
800 {
801         struct seq_file *seq = file->private_data;
802         struct ll_async_page *llap = seq->private;
803         struct ll_sb_info *sbi = llap->llap_cookie;
804
805         spin_lock(&sbi->ll_lock);
806         if (!list_empty(&llap->llap_proc_item))
807                 list_del_init(&llap->llap_proc_item);
808         spin_unlock(&sbi->ll_lock);
809         OBD_FREE(llap, sizeof(*llap));
810
811         return seq_release(inode, file);
812 }
813
814 struct file_operations llite_dump_pgcache_fops = {
815         .owner   = THIS_MODULE,
816         .open    = llite_dump_pgcache_seq_open,
817         .read    = seq_read,
818         .release = llite_dump_pgcache_seq_release,
819 };
820 static int ll_ra_stats_seq_show(struct seq_file *seq, void *v)
821 {
822         struct timeval now;
823         struct ll_sb_info *sbi = seq->private;
824         struct ll_ra_info *ra = &sbi->ll_ra_info;
825         int i;
826         static char *ra_stat_strings[] = {
827                 [RA_STAT_HIT] = "hits",
828                 [RA_STAT_MISS] = "misses",
829                 [RA_STAT_DISTANT_READPAGE] = "readpage not consecutive",
830                 [RA_STAT_MISS_IN_WINDOW] = "miss inside window",
831                 [RA_STAT_FAILED_MATCH] = "failed lock match",
832                 [RA_STAT_DISCARDED] = "read but discarded",
833                 [RA_STAT_ZERO_LEN] = "zero length file",
834                 [RA_STAT_ZERO_WINDOW] = "zero size window",
835                 [RA_STAT_EOF] = "read-ahead to EOF",
836                 [RA_STAT_MAX_IN_FLIGHT] = "hit max r-a issue",
837         };
838
839         do_gettimeofday(&now);
840
841         spin_lock(&sbi->ll_lock);
842
843         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
844                    now.tv_sec, now.tv_usec);
845         seq_printf(seq, "pending issued pages:           %lu\n",
846                    ra->ra_cur_pages);
847
848         for(i = 0; i < _NR_RA_STAT; i++)
849                 seq_printf(seq, "%-25s %lu\n", ra_stat_strings[i],
850                            ra->ra_stats[i]);
851
852         spin_unlock(&sbi->ll_lock);
853
854         return 0;
855 }
856
857 static void *ll_ra_stats_seq_start(struct seq_file *p, loff_t *pos)
858 {
859         if (*pos == 0)
860                 return (void *)1;
861         return NULL;
862 }
863 static void *ll_ra_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
864 {
865         ++*pos;
866         return NULL;
867 }
868 static void ll_ra_stats_seq_stop(struct seq_file *p, void *v)
869 {
870 }
871 struct seq_operations ll_ra_stats_seq_sops = {
872         .start = ll_ra_stats_seq_start,
873         .stop = ll_ra_stats_seq_stop,
874         .next = ll_ra_stats_seq_next,
875         .show = ll_ra_stats_seq_show,
876 };
877
878 static int ll_ra_stats_seq_open(struct inode *inode, struct file *file)
879 {
880         struct proc_dir_entry *dp = PDE(inode);
881         struct seq_file *seq;
882         int rc;
883
884         rc = seq_open(file, &ll_ra_stats_seq_sops);
885         if (rc)
886                 return rc;
887         seq = file->private_data;
888         seq->private = dp->data;
889         return 0;
890 }
891
892 static ssize_t ll_ra_stats_seq_write(struct file *file, const char *buf,
893                                        size_t len, loff_t *off)
894 {
895         struct seq_file *seq = file->private_data;
896         struct ll_sb_info *sbi = seq->private;
897         struct ll_ra_info *ra = &sbi->ll_ra_info;
898
899         spin_lock(&sbi->ll_lock);
900         memset(ra->ra_stats, 0, sizeof(ra->ra_stats));
901         spin_unlock(&sbi->ll_lock);
902
903         return len;
904 }
905
906 struct file_operations ll_ra_stats_fops = {
907         .owner   = THIS_MODULE,
908         .open    = ll_ra_stats_seq_open,
909         .read    = seq_read,
910         .write   = ll_ra_stats_seq_write,
911         .llseek  = seq_lseek,
912         .release = seq_release,
913 };
914
915 #define PRINTF_STIME(stime) (unsigned long)(stime)->st_num,     \
916         lprocfs_stime_avg_ms(stime), lprocfs_stime_avg_us(stime)
917
918 static int llite_wait_times_seq_show(struct seq_file *seq, void *v)
919 {
920         struct ll_sb_info *sbi = seq->private;
921         struct timeval now;
922
923         do_gettimeofday(&now);
924
925         spin_lock(&sbi->ll_lock);
926
927         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n\n",
928                    now.tv_sec, now.tv_usec);
929
930         seq_printf(seq, "lock wait times: (num, average ms)\n");
931         seq_printf(seq, "\tread\t%lu\t%lu.%04lu\n",
932                         PRINTF_STIME(&sbi->ll_read_stime));
933         seq_printf(seq, "\twrite\t%lu\t%lu.%04lu\n",
934                         PRINTF_STIME(&sbi->ll_write_stime));
935         seq_printf(seq, "\tgroup\t%lu\t%lu.%04lu\n",
936                         PRINTF_STIME(&sbi->ll_grouplock_stime));
937         seq_printf(seq, "\tseek\t%lu\t%lu.%04lu\n",
938                         PRINTF_STIME(&sbi->ll_seek_stime));
939         seq_printf(seq, "\tsetattr\t%lu\t%lu.%04lu\n\n",
940                         PRINTF_STIME(&sbi->ll_setattr_stime));
941
942         seq_printf(seq, "io path wait times: (num, average ms)\n");
943         seq_printf(seq, "\tll_brw\t%lu\t%lu.%04lu\n",
944                         PRINTF_STIME(&sbi->ll_brw_stime));
945 #if 0
946         seq_printf(seq, "\tdone\t%lu\t%lu.%04lu\n",
947                         PRINTF_STIME(&sbi->ll_done_stime));
948 #endif
949
950         spin_unlock(&sbi->ll_lock);
951
952         return 0;
953 }
954 #undef pct
955
956 static void *llite_wait_times_seq_start(struct seq_file *p, loff_t *pos)
957 {
958         if (*pos == 0)
959                 return (void *)1;
960         return NULL;
961 }
962 static void *llite_wait_times_seq_next(struct seq_file *p, void *v, loff_t *pos)
963 {
964         ++*pos;
965         return NULL;
966 }
967 static void llite_wait_times_seq_stop(struct seq_file *p, void *v)
968 {
969 }
970 struct seq_operations llite_wait_times_seq_sops = {
971         .start = llite_wait_times_seq_start,
972         .stop = llite_wait_times_seq_stop,
973         .next = llite_wait_times_seq_next,
974         .show = llite_wait_times_seq_show,
975 };
976
977 static int llite_wait_times_seq_open(struct inode *inode, struct file *file)
978 {
979         struct proc_dir_entry *dp = PDE(inode);
980         struct seq_file *seq;
981         int rc;
982
983         rc = seq_open(file, &llite_wait_times_seq_sops);
984         if (rc)
985                 return rc;
986         seq = file->private_data;
987         seq->private = dp->data;
988         return 0;
989 }
990
991 static ssize_t llite_wait_times_seq_write(struct file *file, const char *buf,
992                                        size_t len, loff_t *off)
993 {
994         struct seq_file *seq = file->private_data;
995         struct ll_sb_info *sbi = seq->private;
996
997         spin_lock(&sbi->ll_lock);
998         memset(&sbi->ll_read_stime, 0, sizeof(sbi->ll_read_stime));
999         memset(&sbi->ll_write_stime, 0, sizeof(sbi->ll_write_stime));
1000         memset(&sbi->ll_grouplock_stime, 0, sizeof(sbi->ll_grouplock_stime));
1001         memset(&sbi->ll_seek_stime, 0, sizeof(sbi->ll_seek_stime));
1002         memset(&sbi->ll_setattr_stime, 0, sizeof(sbi->ll_setattr_stime));
1003         memset(&sbi->ll_brw_stime, 0, sizeof(sbi->ll_brw_stime));
1004 //        memset(&sbi->ll_done_stime, 0, sizeof(sbi->ll_done_stime));
1005         spin_unlock(&sbi->ll_lock);
1006
1007         return len;
1008 }
1009
1010 struct file_operations llite_wait_times_fops = {
1011         .owner   = THIS_MODULE,
1012         .open    = llite_wait_times_seq_open,
1013         .read    = seq_read,
1014         .write   = llite_wait_times_seq_write,
1015         .llseek  = seq_lseek,
1016         .release = seq_release,
1017 };
1018
1019 #endif /* LPROCFS */