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