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