Whamcloud - gitweb
Landing b_bug974 onto HEAD (20040213_1538).
[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
29 #include "llite_internal.h"
30
31 /* /proc/lustre/llite mount point registration */
32 struct proc_dir_entry *proc_lustre_fs_root;
33 struct file_operations llite_dump_pgcache_fops;
34
35 #ifndef LPROCFS
36 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
37                                 struct super_block *sb, char *osc, char *mdc)
38 {
39         return 0;
40 }
41 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi){}
42 #else
43
44 long long mnt_instance;
45
46 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
47                          int *eof, void *data)
48 {
49         struct super_block *sb = (struct super_block *)data;
50         struct obd_statfs osfs;
51         int rc;
52
53         LASSERT(sb != NULL);
54         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
55         if (!rc) {
56               *eof = 1;
57               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
58         }
59
60         return rc;
61 }
62
63 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
64                              int *eof, void *data)
65 {
66         struct super_block *sb = (struct super_block *)data;
67         struct obd_statfs osfs;
68         int rc;
69
70         LASSERT(sb != NULL);
71         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
72         if (!rc) {
73                 __u32 blk_size = osfs.os_bsize >> 10;
74                 __u64 result = osfs.os_blocks;
75
76                 while (blk_size >>= 1)
77                         result <<= 1;
78
79                 *eof = 1;
80                 rc = snprintf(page, count, LPU64"\n", result);
81         }
82         return rc;
83
84 }
85
86 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
87                             int *eof, void *data)
88 {
89         struct super_block *sb = (struct super_block *)data;
90         struct obd_statfs osfs;
91         int rc;
92
93         LASSERT(sb != NULL);
94         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
95         if (!rc) {
96                 __u32 blk_size = osfs.os_bsize >> 10;
97                 __u64 result = osfs.os_bfree;
98
99                 while (blk_size >>= 1)
100                         result <<= 1;
101
102                 *eof = 1;
103                 rc = snprintf(page, count, LPU64"\n", result);
104         }
105         return rc;
106 }
107
108 static int ll_rd_kbytesavail(char *page, char **start, off_t off, int count,
109                              int *eof, void *data)
110 {
111         struct super_block *sb = (struct super_block *)data;
112         struct obd_statfs osfs;
113         int rc;
114
115         LASSERT(sb != NULL);
116         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
117         if (!rc) {
118                 __u32 blk_size = osfs.os_bsize >> 10;
119                 __u64 result = osfs.os_bavail;
120
121                 while (blk_size >>= 1)
122                         result <<= 1;
123
124                 *eof = 1;
125                 rc = snprintf(page, count, LPU64"\n", result);
126         }
127         return rc;
128 }
129
130 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
131                             int *eof, void *data)
132 {
133         struct super_block *sb = (struct super_block *)data;
134         struct obd_statfs osfs;
135         int rc;
136
137         LASSERT(sb != NULL);
138         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
139         if (!rc) {
140                  *eof = 1;
141                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
142         }
143         return rc;
144 }
145
146 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
147                            int *eof, void *data)
148 {
149         struct super_block *sb = (struct super_block *)data;
150         struct obd_statfs osfs;
151         int rc;
152
153         LASSERT(sb != NULL);
154         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
155         if (!rc) {
156                  *eof = 1;
157                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
158         }
159         return rc;
160
161 }
162
163 #if 0
164 static int ll_rd_path(char *page, char **start, off_t off, int count, int *eof,
165                       void *data)
166 {
167         return 0;
168 }
169 #endif
170
171 static int ll_rd_fstype(char *page, char **start, off_t off, int count,
172                         int *eof, void *data)
173 {
174         struct super_block *sb = (struct super_block*)data;
175
176         LASSERT(sb != NULL);
177         *eof = 1;
178         return snprintf(page, count, "%s\n", sb->s_type->name);
179 }
180
181 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
182                          int *eof, void *data)
183 {
184         struct super_block *sb = (struct super_block *)data;
185
186         LASSERT(sb != NULL);
187         *eof = 1;
188         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
189 }
190
191 static int ll_rd_read_ahead(char *page, char **start, off_t off, int count,
192                             int *eof, void *data)
193 {
194         struct super_block *sb = (struct super_block*)data;
195         struct ll_sb_info *sbi = ll_s2sbi(sb);
196         int val, rc;
197         ENTRY;
198
199         *eof = 1;
200         val = (sbi->ll_flags & LL_SBI_READAHEAD) ? 1 : 0;
201         rc = snprintf(page, count, "%d\n", val);
202         RETURN(rc);
203 }
204
205 static int ll_wr_read_ahead(struct file *file, const char *buffer,
206                             unsigned long count, void *data)
207 {
208         struct super_block *sb = (struct super_block*)data;
209         struct ll_sb_info *sbi = ll_s2sbi(sb);
210         int readahead;
211         ENTRY;
212
213         if (1 != sscanf(buffer, "%d", &readahead))
214                 RETURN(-EINVAL);        
215
216         if (readahead)
217                 sbi->ll_flags |= LL_SBI_READAHEAD;
218         else
219                 sbi->ll_flags &= ~LL_SBI_READAHEAD;
220
221         RETURN(count);
222 }
223
224 static struct lprocfs_vars lprocfs_obd_vars[] = {
225         { "uuid",         ll_rd_sb_uuid,          0, 0 },
226         //{ "mntpt_path",   ll_rd_path,             0, 0 },
227         { "fstype",       ll_rd_fstype,           0, 0 },
228         { "blocksize",    ll_rd_blksize,          0, 0 },
229         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
230         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
231         { "kbytesavail",  ll_rd_kbytesavail,      0, 0 },
232         { "filestotal",   ll_rd_filestotal,       0, 0 },
233         { "filesfree",    ll_rd_filesfree,        0, 0 },
234         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
235         { "read_ahead",   ll_rd_read_ahead, ll_wr_read_ahead, 0 },
236         { 0 }
237 };
238
239 #define MAX_STRING_SIZE 128
240
241 struct llite_file_opcode {
242         __u32       opcode;
243         __u32       type;
244         const char *opname;
245 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
246         /* file operation */
247         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
248         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
249         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
250                                    "writeback_from_writepage" },
251         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
252                                    "writeback_from_pressure" },
253         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
254                                    "writeback_ok_pages" },
255         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
256                                    "writeback_failed_pages" },
257         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
258                                    "read_bytes" },
259         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
260                                    "write_bytes" },
261         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
262                                    "brw_read" },
263         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
264                                    "brw_write" },
265
266         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
267         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
268         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
269         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
270         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
271         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
272         /* inode operation */
273         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
274         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "punch" },
275 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
276         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
277 #else
278         { LPROC_LL_REVALIDATE,     LPROCFS_TYPE_REGS, "getattr" },
279 #endif
280         /* special inode operation */
281         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
282         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
283         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
284                                    "direct_read" },
285         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
286                                    "direct_write" },
287
288 };
289
290 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
291                                 struct super_block *sb, char *osc, char *mdc)
292 {
293         struct lprocfs_vars lvars[2];
294         struct ll_sb_info *sbi = ll_s2sbi(sb);
295         struct obd_device *obd;
296         char name[MAX_STRING_SIZE + 1];
297         int err, id;
298         struct lprocfs_stats *svc_stats = NULL;
299         struct proc_dir_entry *entry;
300         ENTRY;
301
302         memset(lvars, 0, sizeof(lvars));
303
304         name[MAX_STRING_SIZE] = '\0';
305         lvars[0].name = name;
306
307         LASSERT(sbi != NULL);
308         LASSERT(mdc != NULL);
309         LASSERT(osc != NULL);
310
311         /* Mount info */
312         snprintf(name, MAX_STRING_SIZE, "fs%llu", mnt_instance);
313
314         mnt_instance++;
315         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
316         if (IS_ERR(sbi->ll_proc_root)) {
317                 err = PTR_ERR(sbi->ll_proc_root);
318                 sbi->ll_proc_root = NULL;
319                 RETURN(err);
320         }
321
322         entry = create_proc_entry("dump_page_cache", 0444, sbi->ll_proc_root);
323         if (entry == NULL)
324                 GOTO(out, err = -ENOMEM);
325         entry->proc_fops = &llite_dump_pgcache_fops;
326         entry->data = sbi;
327
328         svc_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES);
329         if (svc_stats == NULL) {
330                 err = -ENOMEM;
331                 goto out;
332         }
333         /* do counter init */
334         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
335                 __u32 type = llite_opcode_table[id].type;
336                 void *ptr = NULL;
337                 if (type & LPROCFS_TYPE_REGS)
338                         ptr = "regs";
339                 else {
340                         if (type & LPROCFS_TYPE_BYTES)
341                                 ptr = "bytes";
342                         else {
343                                 if (type & LPROCFS_TYPE_PAGES)
344                                         ptr = "pages";
345                         }
346                 }
347                 lprocfs_counter_init(svc_stats, llite_opcode_table[id].opcode,
348                                      (type & LPROCFS_CNTR_AVGMINMAX),
349                                      llite_opcode_table[id].opname, ptr);
350         }
351         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", svc_stats);
352         if (err)
353                 goto out;
354         else
355                 sbi->ll_stats = svc_stats;
356         /* need place to keep svc_stats */
357
358         /* Static configuration info */
359         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_obd_vars, sb);
360         if (err)
361                 goto out;
362
363         /* MDC info */
364         obd = class_name2obd(mdc);
365
366         LASSERT(obd != NULL);
367         LASSERT(obd->obd_type != NULL);
368         LASSERT(obd->obd_type->typ_name != NULL);
369
370         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
371                  obd->obd_type->typ_name);
372         lvars[0].read_fptr = lprocfs_rd_name;
373         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
374         if (err)
375                 goto out;
376
377         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
378         lvars[0].read_fptr = lprocfs_rd_uuid;
379         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
380         if (err)
381                 goto out;
382
383         /* OSC */
384         obd = class_name2obd(osc);
385
386         LASSERT(obd != NULL);
387         LASSERT(obd->obd_type != NULL);
388         LASSERT(obd->obd_type->typ_name != NULL);
389
390         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
391                  obd->obd_type->typ_name);
392         lvars[0].read_fptr = lprocfs_rd_name;
393         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
394         if (err)
395                 goto out;
396
397         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
398         lvars[0].read_fptr = lprocfs_rd_uuid;
399         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
400 out:
401         if (err) {
402                 if (svc_stats)
403                         lprocfs_free_stats(svc_stats);
404                 if (sbi->ll_proc_root)
405                         lprocfs_remove(sbi->ll_proc_root);
406         }
407         RETURN(err);
408 }
409
410 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
411 {
412         if (sbi->ll_proc_root) {
413                 struct proc_dir_entry *file_stats =
414                         lprocfs_srch(sbi->ll_proc_root, "stats");
415
416                 if (file_stats) {
417                         lprocfs_free_stats(sbi->ll_stats);
418                         lprocfs_remove(file_stats);
419                 }
420         }
421 }
422 #undef MAX_STRING_SIZE
423
424 static struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
425                                                     struct list_head *list)
426 {
427         struct ll_async_page *llap;
428         struct list_head *pos;
429
430         list_for_each(pos, list) {
431                 if (pos == &sbi->ll_pglist)
432                         return NULL;
433                 llap = list_entry(pos, struct ll_async_page, llap_proc_item);
434                 if (llap->llap_page == NULL)
435                         continue;
436                 return llap;
437         }
438         LBUG();
439         return NULL;
440 }
441
442 #define seq_page_flag(seq, page, flag, has_flags) do {                  \
443                 if (test_bit(PG_##flag, &(page)->flags)) {              \
444                         if (!has_flags)                                 \
445                                 has_flags = 1;                          \
446                         else                                            \
447                                 seq_putc(seq, '|');                     \
448                         seq_puts(seq, #flag);                           \
449                 }                                                       \
450         } while(0);
451
452 static int llite_dump_pgcache_seq_show(struct seq_file *seq, void *v)
453 {
454         struct ll_async_page *llap, *dummy_llap = seq->private;
455         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
456
457         /* 2.4 doesn't seem to have SEQ_START_TOKEN, so we implement
458          * it in our own state */
459         if (dummy_llap->llap_magic == 0) {
460                 seq_printf(seq, "generation | llap .cookie | page ");
461                 seq_printf(seq, "inode .index [ page flags ]\n");
462                 return 0;
463         }
464
465         spin_lock(&sbi->ll_pglist_lock);
466
467         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
468         if (llap != NULL)  {
469                 int has_flags = 0;
470                 struct page *page = llap->llap_page;
471
472                 seq_printf(seq, "%lu | %p %p | %p %p %lu [", 
473                                 sbi->ll_pglist_gen, 
474                                 llap, llap->llap_cookie,
475                                 page, page->mapping->host, page->index);
476                 seq_page_flag(seq, page, locked, has_flags);
477                 seq_page_flag(seq, page, error, has_flags);
478                 seq_page_flag(seq, page, referenced, has_flags);
479                 seq_page_flag(seq, page, uptodate, has_flags);
480                 seq_page_flag(seq, page, dirty, has_flags);
481                 seq_page_flag(seq, page, highmem, has_flags);
482                 if (!has_flags)
483                         seq_puts(seq, "-]\n");
484                 else 
485                         seq_puts(seq, "]\n");
486         }
487
488         spin_unlock(&sbi->ll_pglist_lock);
489
490         return 0;
491 }
492
493 static void *llite_dump_pgcache_seq_start(struct seq_file *seq, loff_t *pos)
494 {
495         struct ll_async_page *llap = seq->private;
496
497         if (llap->llap_magic == 2)
498                 return NULL;
499
500         return (void *)1;
501 }
502
503 static void *llite_dump_pgcache_seq_next(struct seq_file *seq, void *v, 
504                                          loff_t *pos)
505 {
506         struct ll_async_page *llap, *dummy_llap = seq->private;
507         struct ll_sb_info *sbi = dummy_llap->llap_cookie;
508
509         /* bail if we just displayed the banner */
510         if (dummy_llap->llap_magic == 0) {
511                 dummy_llap->llap_magic = 1;
512                 return dummy_llap;
513         }
514
515         /* we've just displayed the llap that is after us in the list.
516          * we advance to a position beyond it, returning null if there
517          * isn't another llap in the list beyond that new position. */
518         spin_lock(&sbi->ll_pglist_lock);
519         llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
520         list_del_init(&dummy_llap->llap_proc_item);
521         if (llap) {
522                 list_add(&dummy_llap->llap_proc_item, &llap->llap_proc_item);
523                 llap = llite_pglist_next_llap(sbi, &dummy_llap->llap_proc_item);
524         }
525         spin_unlock(&sbi->ll_pglist_lock);
526
527         ++*pos;
528         if (llap == NULL) {
529                 dummy_llap->llap_magic = 2;
530                 return NULL;
531         }
532         return dummy_llap;
533 }
534
535 static void llite_dump_pgcache_seq_stop(struct seq_file *seq, void *v)
536 {
537 }
538
539 struct seq_operations llite_dump_pgcache_seq_sops = {
540         .start = llite_dump_pgcache_seq_start,
541         .stop = llite_dump_pgcache_seq_stop,
542         .next = llite_dump_pgcache_seq_next,
543         .show = llite_dump_pgcache_seq_show,
544 };
545
546 /* we're displaying llaps in a list_head list.  we don't want to hold a lock
547  * while we walk the entire list, and we don't want to have to seek into
548  * the right position in the list as an app advances with many syscalls.  we
549  * allocate a dummy llap and hang it off file->private.  its position in
550  * the list records where the app is currently displaying.  this way our
551  * seq .start and .stop don't actually do anything.  .next returns null
552  * when the dummy hits the end of the list which eventually leads to .release
553  * where we tear down.  this kind of displaying is super-racey, so we put
554  * a generation counter on the list so the output shows when the list
555  * changes between reads.
556  */
557 static int llite_dump_pgcache_seq_open(struct inode *inode, struct file *file)
558 {
559         struct proc_dir_entry *dp = PDE(inode);
560         struct ll_async_page *llap;
561         struct seq_file *seq;
562         struct ll_sb_info *sbi = dp->data;
563         int rc;
564
565         llap = kmalloc(sizeof(*llap), GFP_KERNEL);
566         if (llap == NULL)
567                 return -ENOMEM;
568         llap->llap_page = NULL;
569         llap->llap_cookie = sbi;
570         llap->llap_magic = 0;
571  
572         rc = seq_open(file, &llite_dump_pgcache_seq_sops);
573         if (rc) {
574                 kfree(llap);
575                 return rc;
576         }
577         seq = file->private_data;
578         seq->private = llap;
579
580         spin_lock(&sbi->ll_pglist_lock);
581         list_add(&llap->llap_proc_item, &sbi->ll_pglist);
582         spin_unlock(&sbi->ll_pglist_lock);
583
584         return 0;
585 }
586
587 static int llite_dump_pgcache_seq_release(struct inode *inode, 
588                                           struct file *file)
589 {
590         struct seq_file *seq = file->private_data;
591         struct ll_async_page *llap = seq->private;
592         struct ll_sb_info *sbi = llap->llap_cookie;
593
594         spin_lock(&sbi->ll_pglist_lock);
595         if (!list_empty(&llap->llap_proc_item))
596                 list_del_init(&llap->llap_proc_item);
597         spin_unlock(&sbi->ll_pglist_lock);
598         kfree(llap);
599
600         return seq_release(inode, file);
601 }
602
603 struct file_operations llite_dump_pgcache_fops = {
604         .open    = llite_dump_pgcache_seq_open,
605         .read    = seq_read,
606         .release    = llite_dump_pgcache_seq_release,
607 };
608
609 #endif /* LPROCFS */