Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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
28 #include "llite_internal.h"
29
30 /* /proc/lustre/llite mount point registration */
31 struct proc_dir_entry *proc_lustre_fs_root;
32
33 #ifndef LPROCFS
34 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
35                                 struct super_block *sb, char *osc, char *mdc)
36 {
37         return 0;
38 }
39 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi){}
40 #else
41
42 long long mnt_instance;
43
44 static int ll_rd_blksize(char *page, char **start, off_t off, int count,
45                          int *eof, void *data)
46 {
47         struct super_block *sb = (struct super_block *)data;
48         struct obd_statfs osfs;
49         int rc;
50
51         LASSERT(sb != NULL);
52         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
53         if (!rc) {
54               *eof = 1;
55               rc = snprintf(page, count, "%u\n", osfs.os_bsize);
56         }
57
58         return rc;
59 }
60
61 static int ll_rd_kbytestotal(char *page, char **start, off_t off, int count,
62                              int *eof, void *data)
63 {
64         struct super_block *sb = (struct super_block *)data;
65         struct obd_statfs osfs;
66         int rc;
67
68         LASSERT(sb != NULL);
69         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
70         if (!rc) {
71                 __u32 blk_size = osfs.os_bsize >> 10;
72                 __u64 result = osfs.os_blocks;
73
74                 while (blk_size >>= 1)
75                         result <<= 1;
76
77                 *eof = 1;
78                 rc = snprintf(page, count, LPU64"\n", result);
79         }
80         return rc;
81
82 }
83
84 static int ll_rd_kbytesfree(char *page, char **start, off_t off, int count,
85                             int *eof, void *data)
86 {
87         struct super_block *sb = (struct super_block *)data;
88         struct obd_statfs osfs;
89         int rc;
90
91         LASSERT(sb != NULL);
92         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
93         if (!rc) {
94                 __u32 blk_size = osfs.os_bsize >> 10;
95                 __u64 result = osfs.os_bfree;
96
97                 while (blk_size >>= 1)
98                         result <<= 1;
99
100                 *eof = 1;
101                 rc = snprintf(page, count, LPU64"\n", result);
102         }
103         return rc;
104 }
105
106 static int ll_rd_filestotal(char *page, char **start, off_t off, int count,
107                             int *eof, void *data)
108 {
109         struct super_block *sb = (struct super_block *)data;
110         struct obd_statfs osfs;
111         int rc;
112
113         LASSERT(sb != NULL);
114         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
115         if (!rc) {
116                  *eof = 1;
117                  rc = snprintf(page, count, LPU64"\n", osfs.os_files);
118         }
119         return rc;
120 }
121
122 static int ll_rd_filesfree(char *page, char **start, off_t off, int count,
123                            int *eof, void *data)
124 {
125         struct super_block *sb = (struct super_block *)data;
126         struct obd_statfs osfs;
127         int rc;
128
129         LASSERT(sb != NULL);
130         rc = ll_statfs_internal(sb, &osfs, jiffies - HZ);
131         if (!rc) {
132                  *eof = 1;
133                  rc = snprintf(page, count, LPU64"\n", osfs.os_ffree);
134         }
135         return rc;
136
137 }
138
139 #if 0
140 static int ll_rd_path(char *page, char **start, off_t off, int count, int *eof,
141                       void *data)
142 {
143         return 0;
144 }
145 #endif
146
147 static int ll_rd_fstype(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
152         LASSERT(sb != NULL);
153         *eof = 1;
154         return snprintf(page, count, "%s\n", sb->s_type->name);
155 }
156
157 static int ll_rd_sb_uuid(char *page, char **start, off_t off, int count,
158                          int *eof, void *data)
159 {
160         struct super_block *sb = (struct super_block *)data;
161
162         LASSERT(sb != NULL);
163         *eof = 1;
164         return snprintf(page, count, "%s\n", ll_s2sbi(sb)->ll_sb_uuid.uuid);
165 }
166
167 static int ll_rd_read_ahead(char *page, char **start, off_t off, int count,
168                             int *eof, void *data)
169 {
170         struct super_block *sb = (struct super_block*)data;
171         struct ll_sb_info *sbi = ll_s2sbi(sb);
172         int val, rc;
173         ENTRY;
174
175         *eof = 1;
176         val = (sbi->ll_flags & LL_SBI_READAHEAD) ? 1 : 0;
177         rc = snprintf(page, count, "%d\n", val);
178         RETURN(rc);
179 }
180
181 static int ll_wr_read_ahead(struct file *file, const char *buffer,
182                             unsigned long count, void *data)
183 {
184         struct super_block *sb = (struct super_block*)data;
185         struct ll_sb_info *sbi = ll_s2sbi(sb);
186         int readahead;
187         ENTRY;
188
189         if (1 != sscanf(buffer, "%d", &readahead))
190                 RETURN(-EINVAL);        
191
192         if (readahead)
193                 sbi->ll_flags |= LL_SBI_READAHEAD;
194         else
195                 sbi->ll_flags &= ~LL_SBI_READAHEAD;
196
197         RETURN(count);
198 }
199
200 static struct lprocfs_vars lprocfs_obd_vars[] = {
201         { "uuid",         ll_rd_sb_uuid,          0, 0 },
202         //{ "mntpt_path",   ll_rd_path,             0, 0 },
203         { "fstype",       ll_rd_fstype,           0, 0 },
204         { "blocksize",    ll_rd_blksize,          0, 0 },
205         { "kbytestotal",  ll_rd_kbytestotal,      0, 0 },
206         { "kbytesfree",   ll_rd_kbytesfree,       0, 0 },
207         { "filestotal",   ll_rd_filestotal,       0, 0 },
208         { "filesfree",    ll_rd_filesfree,        0, 0 },
209         //{ "filegroups",   lprocfs_rd_filegroups,  0, 0 },
210         { "read_ahead",   ll_rd_read_ahead, ll_wr_read_ahead, 0 },
211         { 0 }
212 };
213
214 #define MAX_STRING_SIZE 128
215
216 struct llite_file_opcode {
217         __u32       opcode;
218         __u32       type;
219         const char *opname;
220 } llite_opcode_table[LPROC_LL_FILE_OPCODES] = {
221         /* file operation */
222         { LPROC_LL_DIRTY_HITS,     LPROCFS_TYPE_REGS, "dirty_pages_hits" },
223         { LPROC_LL_DIRTY_MISSES,   LPROCFS_TYPE_REGS, "dirty_pages_misses" },
224         { LPROC_LL_WB_WRITEPAGE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
225                                    "writeback_from_writepage" },
226         { LPROC_LL_WB_PRESSURE,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
227                                    "writeback_from_pressure" },
228         { LPROC_LL_WB_OK,          LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
229                                    "writeback_ok_pages" },
230         { LPROC_LL_WB_FAIL,        LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
231                                    "writeback_failed_pages" },
232         { LPROC_LL_READ_BYTES,     LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
233                                    "read_bytes" },
234         { LPROC_LL_WRITE_BYTES,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_BYTES,
235                                    "write_bytes" },
236         { LPROC_LL_BRW_READ,       LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
237                                    "brw_read" },
238         { LPROC_LL_BRW_WRITE,      LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
239                                    "brw_write" },
240
241         { LPROC_LL_IOCTL,          LPROCFS_TYPE_REGS, "ioctl" },
242         { LPROC_LL_OPEN,           LPROCFS_TYPE_REGS, "open" },
243         { LPROC_LL_RELEASE,        LPROCFS_TYPE_REGS, "close" },
244         { LPROC_LL_MAP,            LPROCFS_TYPE_REGS, "mmap" },
245         { LPROC_LL_LLSEEK,         LPROCFS_TYPE_REGS, "seek" },
246         { LPROC_LL_FSYNC,          LPROCFS_TYPE_REGS, "fsync" },
247         /* inode operation */
248         { LPROC_LL_SETATTR,        LPROCFS_TYPE_REGS, "setattr" },
249         { LPROC_LL_TRUNC,          LPROCFS_TYPE_REGS, "punch" },
250 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
251         { LPROC_LL_GETATTR,        LPROCFS_TYPE_REGS, "getattr" },
252 #else
253         { LPROC_LL_REVALIDATE,     LPROCFS_TYPE_REGS, "getattr" },
254 #endif
255         /* special inode operation */
256         { LPROC_LL_STAFS,          LPROCFS_TYPE_REGS, "statfs" },
257         { LPROC_LL_ALLOC_INODE,    LPROCFS_TYPE_REGS, "alloc_inode" },
258         { LPROC_LL_DIRECT_READ,    LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
259                                    "direct_read" },
260         { LPROC_LL_DIRECT_WRITE,   LPROCFS_CNTR_AVGMINMAX|LPROCFS_TYPE_PAGES,
261                                    "direct_write" },
262
263 };
264
265 int lprocfs_register_mountpoint(struct proc_dir_entry *parent,
266                                 struct super_block *sb, char *osc, char *mdc)
267 {
268         struct lprocfs_vars lvars[2];
269         struct ll_sb_info *sbi = ll_s2sbi(sb);
270         struct obd_device *obd;
271         char name[MAX_STRING_SIZE + 1];
272         int err, id;
273         struct lprocfs_stats *svc_stats = NULL;
274         ENTRY;
275
276         memset(lvars, 0, sizeof(lvars));
277
278         name[MAX_STRING_SIZE] = '\0';
279         lvars[0].name = name;
280
281         LASSERT(sbi != NULL);
282         LASSERT(mdc != NULL);
283         LASSERT(osc != NULL);
284
285         /* Mount info */
286         snprintf(name, MAX_STRING_SIZE, "fs%llu", mnt_instance);
287
288         mnt_instance++;
289         sbi->ll_proc_root = lprocfs_register(name, parent, NULL, NULL);
290         if (IS_ERR(sbi->ll_proc_root)) {
291                 err = PTR_ERR(sbi->ll_proc_root);
292                 sbi->ll_proc_root = NULL;
293                 RETURN(err);
294         }
295
296         svc_stats = lprocfs_alloc_stats(LPROC_LL_FILE_OPCODES);
297         if (svc_stats == NULL) {
298                 err = -ENOMEM;
299                 goto out;
300         }
301         /* do counter init */
302         for (id = 0; id < LPROC_LL_FILE_OPCODES; id++) {
303                 __u32 type = llite_opcode_table[id].type;
304                 void *ptr = NULL;
305                 if (type & LPROCFS_TYPE_REGS)
306                         ptr = "regs";
307                 else {
308                         if (type & LPROCFS_TYPE_BYTES)
309                                 ptr = "bytes";
310                         else {
311                                 if (type & LPROCFS_TYPE_PAGES)
312                                         ptr = "pages";
313                         }
314                 }
315                 lprocfs_counter_init(svc_stats, llite_opcode_table[id].opcode,
316                                      (type & LPROCFS_CNTR_AVGMINMAX),
317                                      llite_opcode_table[id].opname, ptr);
318         }
319         err = lprocfs_register_stats(sbi->ll_proc_root, "stats", svc_stats);
320         if (err)
321                 goto out;
322         else
323                 sbi->ll_stats = svc_stats;
324         /* need place to keep svc_stats */
325
326         /* Static configuration info */
327         err = lprocfs_add_vars(sbi->ll_proc_root, lprocfs_obd_vars, sb);
328         if (err)
329                 goto out;
330
331         /* MDC info */
332         obd = class_name2obd(mdc);
333
334         LASSERT(obd != NULL);
335         LASSERT(obd->obd_type != NULL);
336         LASSERT(obd->obd_type->typ_name != NULL);
337
338         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
339                  obd->obd_type->typ_name);
340         lvars[0].read_fptr = lprocfs_rd_name;
341         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
342         if (err)
343                 goto out;
344
345         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
346         lvars[0].read_fptr = lprocfs_rd_uuid;
347         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
348         if (err)
349                 goto out;
350
351         /* OSC */
352         obd = class_name2obd(osc);
353
354         LASSERT(obd != NULL);
355         LASSERT(obd->obd_type != NULL);
356         LASSERT(obd->obd_type->typ_name != NULL);
357
358         snprintf(name, MAX_STRING_SIZE, "%s/common_name",
359                  obd->obd_type->typ_name);
360         lvars[0].read_fptr = lprocfs_rd_name;
361         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
362         if (err)
363                 goto out;
364
365         snprintf(name, MAX_STRING_SIZE, "%s/uuid", obd->obd_type->typ_name);
366         lvars[0].read_fptr = lprocfs_rd_uuid;
367         err = lprocfs_add_vars(sbi->ll_proc_root, lvars, obd);
368 out:
369         if (err) {
370                 if (svc_stats)
371                         lprocfs_free_stats(svc_stats);
372                 if (sbi->ll_proc_root)
373                         lprocfs_remove(sbi->ll_proc_root);
374         }
375         RETURN(err);
376 }
377
378 void lprocfs_unregister_mountpoint(struct ll_sb_info *sbi)
379 {
380         if (sbi->ll_proc_root) {
381                 struct proc_dir_entry *file_stats =
382                         lprocfs_srch(sbi->ll_proc_root, "stats");
383
384                 if (file_stats) {
385                         lprocfs_free_stats(sbi->ll_stats);
386                         lprocfs_remove(file_stats);
387                 }
388         }
389 }
390 #undef MAX_STRING_SIZE
391 #endif /* LPROCFS */