Whamcloud - gitweb
6f1bbacd800460de0eb1b6220252f653e9a927b6
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_lproc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd/osd_lproc.c
37  *
38  * Author: Mikhail Pershin <tappro@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include <lprocfs_status.h>
44 #include <lu_time.h>
45
46 #include <lustre/lustre_idl.h>
47
48 #include "osd_internal.h"
49
50 #ifdef LPROCFS
51
52 static const char *osd_counter_names[LPROC_OSD_NR] = {
53 #if OSD_THANDLE_STATS
54         [LPROC_OSD_THANDLE_STARTING] = "thandle starting",
55         [LPROC_OSD_THANDLE_OPEN]     = "thandle open",
56         [LPROC_OSD_THANDLE_CLOSING]  = "thandle closing"
57 #endif
58 };
59
60 int osd_procfs_init(struct osd_device *osd, const char *name)
61 {
62         struct lprocfs_static_vars lvars;
63         struct lu_device    *ld = &osd->od_dt_dev.dd_lu_dev;
64         struct obd_type     *type;
65         int                  rc;
66         ENTRY;
67
68         type = ld->ld_type->ldt_obd_type;
69
70         LASSERT(name != NULL);
71         LASSERT(type != NULL);
72
73         /* Find the type procroot and add the proc entry for this device */
74         lprocfs_osd_init_vars(&lvars);
75         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
76                                               lvars.obd_vars, osd);
77         if (IS_ERR(osd->od_proc_entry)) {
78                 rc = PTR_ERR(osd->od_proc_entry);
79                 CERROR("Error %d setting up lprocfs for %s\n",
80                        rc, name);
81                 osd->od_proc_entry = NULL;
82                 GOTO(out, rc);
83         }
84
85         rc = lu_time_init(&osd->od_stats,
86                           osd->od_proc_entry,
87                           osd_counter_names, ARRAY_SIZE(osd_counter_names));
88         EXIT;
89 out:
90         if (rc)
91                osd_procfs_fini(osd);
92         return rc;
93 }
94
95 int osd_procfs_fini(struct osd_device *osd)
96 {
97         if (osd->od_stats)
98                 lu_time_fini(&osd->od_stats);
99
100         if (osd->od_proc_entry) {
101                  lprocfs_remove(&osd->od_proc_entry);
102                  osd->od_proc_entry = NULL;
103         }
104         RETURN(0);
105 }
106
107 void osd_lprocfs_time_start(const struct lu_env *env)
108 {
109         lu_lprocfs_time_start(env);
110 }
111
112 void osd_lprocfs_time_end(const struct lu_env *env, struct osd_device *osd,
113                           int idx)
114 {
115         lu_lprocfs_time_end(env, osd->od_stats, idx);
116 }
117
118
119
120 int lprocfs_osd_rd_blksize(char *page, char **start, off_t off, int count,
121                            int *eof, void *data)
122 {
123         struct osd_device *osd = data;
124         int rc;
125
126         if (unlikely(osd->od_mount == NULL))
127                 return -EINPROGRESS;
128
129         rc = osd_statfs(NULL, &osd->od_dt_dev, &osd->od_kstatfs);
130         if (!rc) {
131                 *eof = 1;
132                 rc = snprintf(page, count, "%ld\n", osd->od_kstatfs.f_bsize);
133         }
134         return rc;
135 }
136
137 int lprocfs_osd_rd_kbytestotal(char *page, char **start, off_t off, int count,
138                                int *eof, void *data)
139 {
140         struct osd_device *osd = data;
141         int rc;
142
143         if (unlikely(osd->od_mount == NULL))
144                 return -EINPROGRESS;
145
146         rc = osd_statfs(NULL, &osd->od_dt_dev, &osd->od_kstatfs);
147         if (!rc) {
148                 __u32 blk_size = osd->od_kstatfs.f_bsize >> 10;
149                 __u64 result = osd->od_kstatfs.f_blocks;
150
151                 while (blk_size >>= 1)
152                         result <<= 1;
153
154                 *eof = 1;
155                 rc = snprintf(page, count, LPU64"\n", result);
156         }
157         return rc;
158 }
159
160 int lprocfs_osd_rd_kbytesfree(char *page, char **start, off_t off, int count,
161                               int *eof, void *data)
162 {
163         struct osd_device *osd = data;
164         int rc;
165
166         if (unlikely(osd->od_mount == NULL))
167                 return -EINPROGRESS;
168
169         rc = osd_statfs(NULL, &osd->od_dt_dev, &osd->od_kstatfs);
170         if (!rc) {
171                 __u32 blk_size = osd->od_kstatfs.f_bsize >> 10;
172                 __u64 result = osd->od_kstatfs.f_bfree;
173
174                 while (blk_size >>= 1)
175                         result <<= 1;
176
177                 *eof = 1;
178                 rc = snprintf(page, count, LPU64"\n", result);
179         }
180         return rc;
181 }
182
183 int lprocfs_osd_rd_kbytesavail(char *page, char **start, off_t off, int count,
184                                int *eof, void *data)
185 {
186         struct osd_device *osd = data;
187         int rc;
188
189         if (unlikely(osd->od_mount == NULL))
190                 return -EINPROGRESS;
191
192         rc = osd_statfs(NULL, &osd->od_dt_dev, &osd->od_kstatfs);
193         if (!rc) {
194                 __u32 blk_size = osd->od_kstatfs.f_bsize >> 10;
195                 __u64 result = osd->od_kstatfs.f_bavail;
196
197                 while (blk_size >>= 1)
198                         result <<= 1;
199
200                 *eof = 1;
201                 rc = snprintf(page, count, LPU64"\n", result);
202         }
203         return rc;
204 }
205
206 int lprocfs_osd_rd_filestotal(char *page, char **start, off_t off, int count,
207                               int *eof, void *data)
208 {
209         struct osd_device *osd = data;
210         int rc;
211
212         if (unlikely(osd->od_mount == NULL))
213                 return -EINPROGRESS;
214
215         rc = osd_statfs(NULL, &osd->od_dt_dev, &osd->od_kstatfs);
216         if (!rc) {
217                 *eof = 1;
218                 rc = snprintf(page, count, LPU64"\n", osd->od_kstatfs.f_files);
219         }
220
221         return rc;
222 }
223
224 int lprocfs_osd_rd_filesfree(char *page, char **start, off_t off, int count,
225                              int *eof, void *data)
226 {
227         struct osd_device *osd = data;
228         int rc;
229
230         if (unlikely(osd->od_mount == NULL))
231                 return -EINPROGRESS;
232
233         rc = osd_statfs(NULL, &osd->od_dt_dev, &osd->od_kstatfs);
234         if (!rc) {
235                 *eof = 1;
236                 rc = snprintf(page, count, LPU64"\n", osd->od_kstatfs.f_ffree);
237         }
238         return rc;
239 }
240
241 int lprocfs_osd_rd_fstype(char *page, char **start, off_t off, int count,
242                           int *eof, void *data)
243 {
244         struct obd_device *osd = data;
245
246         LASSERT(osd != NULL);
247         return snprintf(page, count, "ldiskfs\n");
248 }
249
250 static int lprocfs_osd_rd_mntdev(char *page, char **start, off_t off, int count,
251                                  int *eof, void *data)
252 {
253         struct osd_device *osd = data;
254
255         LASSERT(osd != NULL);
256         if (unlikely(osd->od_mount == NULL))
257                 return -EINPROGRESS;
258
259         LASSERT(osd->od_mount->lmi_mnt->mnt_devname);
260         *eof = 1;
261
262         return snprintf(page, count, "%s\n",
263                         osd->od_mount->lmi_mnt->mnt_devname);
264 }
265
266 #ifdef HAVE_LDISKFS_PDO
267 static int lprocfs_osd_rd_pdo(char *page, char **start, off_t off, int count,
268                               int *eof, void *data)
269 {
270         *eof = 1;
271
272         return snprintf(page, count, "%s\n", ldiskfs_pdo ? "ON" : "OFF");
273 }
274
275 static int lprocfs_osd_wr_pdo(struct file *file, const char *buffer,
276                               unsigned long count, void *data)
277 {
278         int     pdo;
279         int     rc;
280
281         rc = lprocfs_write_helper(buffer, count, &pdo);
282         if (rc != 0)
283                 return rc;
284
285         ldiskfs_pdo = !!pdo;
286
287         return count;
288 }
289 #endif
290
291 struct lprocfs_vars lprocfs_osd_obd_vars[] = {
292         { "blocksize",       lprocfs_osd_rd_blksize,     0, 0 },
293         { "kbytestotal",     lprocfs_osd_rd_kbytestotal, 0, 0 },
294         { "kbytesfree",      lprocfs_osd_rd_kbytesfree,  0, 0 },
295         { "kbytesavail",     lprocfs_osd_rd_kbytesavail, 0, 0 },
296         { "filestotal",      lprocfs_osd_rd_filestotal,  0, 0 },
297         { "filesfree",       lprocfs_osd_rd_filesfree,   0, 0 },
298         { "fstype",          lprocfs_osd_rd_fstype,      0, 0 },
299         { "mntdev",          lprocfs_osd_rd_mntdev,      0, 0 },
300 #ifdef HAVE_LDISKFS_PDO
301         { "pdo",             lprocfs_osd_rd_pdo, lprocfs_osd_wr_pdo, 0 },
302 #endif
303         { 0 }
304 };
305
306 struct lprocfs_vars lprocfs_osd_module_vars[] = {
307         { "num_refs",        lprocfs_rd_numrefs,     0, 0 },
308         { 0 }
309 };
310
311 void lprocfs_osd_init_vars(struct lprocfs_static_vars *lvars)
312 {
313         lvars->module_vars = lprocfs_osd_module_vars;
314         lvars->obd_vars = lprocfs_osd_obd_vars;
315 }
316 #endif