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