Whamcloud - gitweb
LU-1301 mgs: lu device for mgs
[fs/lustre-release.git] / lustre / mgs / lproc_mgs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 #define DEBUG_SUBSYSTEM S_CLASS
37
38 #include <linux/version.h>
39 #include <asm/statfs.h>
40 #include <obd.h>
41 #include <obd_class.h>
42 #include <lprocfs_status.h>
43 #include <lustre_param.h>
44 #include "mgs_internal.h"
45
46 #ifdef LPROCFS
47
48 static int lprocfs_mgs_rd_mntdev(char *page, char **start, off_t off, int count,
49                                  int *eof, void *data)
50 {
51         struct obd_device *obd = (struct obd_device *)data;
52
53         LASSERT(obd != NULL);
54 #if 0
55         /* will be fixed in the subsequent patch */
56         LASSERT(mnt_get_devname(obd->u.mgs.mgs_vfsmnt));
57         *eof = 1;
58
59         return snprintf(page, count, "%s\n",
60                         mnt_get_devname(obd->u.mgs.mgs_vfsmnt));
61 #else
62         return 0;
63 #endif
64 }
65
66 static int mgs_fs_seq_show(struct seq_file *seq, void *v)
67 {
68         struct obd_device *obd = seq->private;
69         struct mgs_device *mgs;
70         cfs_list_t dentry_list;
71         struct l_linux_dirent *dirent, *n;
72         struct lu_env env;
73         int rc, len;
74         ENTRY;
75
76         LASSERT(obd != NULL);
77         LASSERT(obd->obd_lu_dev != NULL);
78         mgs = lu2mgs_dev(obd->obd_lu_dev);
79
80         rc = lu_env_init(&env, LCT_MG_THREAD);
81         if (rc)
82                 RETURN(rc);
83
84         rc = class_dentry_readdir(&env, mgs, &dentry_list);
85         if (rc) {
86                 CERROR("Can't read config dir\n");
87                 GOTO(out, rc);
88         }
89         cfs_list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
90                 cfs_list_del(&dirent->lld_list);
91                 len = strlen(dirent->lld_name);
92                 if ((len > 7) && (strncmp(dirent->lld_name + len - 7, "-client",
93                                           len) == 0)) {
94                         seq_printf(seq, "%.*s\n", len - 7, dirent->lld_name);
95                 }
96                 OBD_FREE(dirent, sizeof(*dirent));
97         }
98
99 out:
100         lu_env_fini(&env);
101         RETURN(0);
102 }
103
104 LPROC_SEQ_FOPS_RO(mgs_fs);
105
106 static void seq_show_srpc_rules(struct seq_file *seq, const char *tgtname,
107                                 struct sptlrpc_rule_set *rset)
108 {
109         struct sptlrpc_rule    *r;
110         char                    dirbuf[10];
111         char                    flvrbuf[40];
112         char                   *net;
113         int                     i;
114
115         for (i = 0; i < rset->srs_nrule; i++) {
116                 r = &rset->srs_rules[i];
117
118                 if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
119                         net = "default";
120                 else
121                         net = libcfs_net2str(r->sr_netid);
122
123                 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
124                         dirbuf[0] = '\0';
125                 else
126                         snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
127                                  sptlrpc_part2name(r->sr_from),
128                                  sptlrpc_part2name(r->sr_to));
129
130                 sptlrpc_flavor2name(&r->sr_flvr, flvrbuf, sizeof(flvrbuf));
131                 seq_printf(seq, "%s.srpc.flavor.%s%s=%s\n", tgtname,
132                            net, dirbuf, flvrbuf);
133         }
134 }
135
136 static int mgsself_srpc_seq_show(struct seq_file *seq, void *v)
137 {
138         struct obd_device *obd = seq->private;
139         struct mgs_device *mgs;
140         struct fs_db      *fsdb;
141         struct lu_env      env;
142         int                rc;
143
144         LASSERT(obd != NULL);
145         LASSERT(obd->obd_lu_dev != NULL);
146         mgs = lu2mgs_dev(obd->obd_lu_dev);
147
148         rc = lu_env_init(&env, LCT_MG_THREAD);
149         if (rc)
150                 return rc;
151
152         rc = mgs_find_or_make_fsdb(&env, mgs, MGSSELF_NAME, &fsdb);
153         if (rc)
154                 goto out;
155
156         cfs_mutex_lock(&fsdb->fsdb_mutex);
157         seq_show_srpc_rules(seq, fsdb->fsdb_name, &fsdb->fsdb_srpc_gen);
158         cfs_mutex_unlock(&fsdb->fsdb_mutex);
159
160 out:
161         lu_env_fini(&env);
162         return 0;
163 }
164
165 LPROC_SEQ_FOPS_RO(mgsself_srpc);
166
167 int lproc_mgs_setup(struct mgs_device *mgs)
168 {
169         struct obd_device *obd = mgs->mgs_obd;
170         int rc;
171
172         rc = lprocfs_obd_seq_create(obd, "filesystems", 0444,
173                                     &mgs_fs_fops, obd);
174         rc = lprocfs_obd_seq_create(obd, "srpc_rules", 0600,
175                                     &mgsself_srpc_fops, obd);
176
177         mgs->mgs_proc_live = lprocfs_register("live", obd->obd_proc_entry,
178                                               NULL, NULL);
179         if (IS_ERR(mgs->mgs_proc_live)) {
180                 rc = PTR_ERR(mgs->mgs_proc_live);
181                 CERROR("error %d setting up lprocfs for %s\n", rc, "live");
182                 mgs->mgs_proc_live = NULL;
183         }
184
185         obd->obd_proc_exports_entry = lprocfs_register("exports",
186                                                        obd->obd_proc_entry,
187                                                        NULL, NULL);
188         if (IS_ERR(obd->obd_proc_exports_entry)) {
189                 rc = PTR_ERR(obd->obd_proc_exports_entry);
190                 CERROR("error %d setting up lprocfs for %s\n", rc, "exports");
191                 obd->obd_proc_exports_entry = NULL;
192         }
193
194         return rc;
195 }
196
197 int lproc_mgs_cleanup(struct mgs_device *mgs)
198 {
199         struct obd_device *obd = mgs->mgs_obd;
200
201         if (!obd)
202                 return -EINVAL;
203
204         if (mgs->mgs_proc_live) {
205                 /* Should be no live entries */
206                 LASSERT(mgs->mgs_proc_live->subdir == NULL);
207                 lprocfs_remove(&mgs->mgs_proc_live);
208                 mgs->mgs_proc_live = NULL;
209         }
210         lprocfs_free_per_client_stats(obd);
211         lprocfs_free_obd_stats(obd);
212         lprocfs_free_md_stats(obd);
213
214         return lprocfs_obd_cleanup(obd);
215 }
216
217 static int mgs_live_seq_show(struct seq_file *seq, void *v)
218 {
219         struct fs_db             *fsdb = seq->private;
220         struct mgs_tgt_srpc_conf *srpc_tgt;
221         int i;
222
223         cfs_mutex_lock(&fsdb->fsdb_mutex);
224
225         seq_printf(seq, "fsname: %s\n", fsdb->fsdb_name);
226         seq_printf(seq, "flags: %#lx     gen: %d\n",
227                    fsdb->fsdb_flags, fsdb->fsdb_gen);
228         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
229                  if (cfs_test_bit(i, fsdb->fsdb_mdt_index_map))
230                          seq_printf(seq, "%s-MDT%04x\n", fsdb->fsdb_name, i);
231         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
232                  if (cfs_test_bit(i, fsdb->fsdb_ost_index_map))
233                          seq_printf(seq, "%s-OST%04x\n", fsdb->fsdb_name, i);
234
235         seq_printf(seq, "\nSecure RPC Config Rules:\n");
236 #if 0
237         seq_printf(seq, "%s.%s=%s\n", fsdb->fsdb_name,
238                    PARAM_SRPC_UDESC, fsdb->fsdb_srpc_fl_udesc ? "yes" : "no");
239 #endif
240         for (srpc_tgt = fsdb->fsdb_srpc_tgt; srpc_tgt;
241              srpc_tgt = srpc_tgt->mtsc_next) {
242                 seq_show_srpc_rules(seq, srpc_tgt->mtsc_tgt,
243                                     &srpc_tgt->mtsc_rset);
244         }
245         seq_show_srpc_rules(seq, fsdb->fsdb_name, &fsdb->fsdb_srpc_gen);
246
247         lprocfs_rd_ir_state(seq, fsdb);
248
249         cfs_mutex_unlock(&fsdb->fsdb_mutex);
250         return 0;
251 }
252
253 static ssize_t mgs_live_seq_write(struct file *file, const char *buf,
254                                   size_t len, loff_t *off)
255 {
256         struct seq_file *seq  = file->private_data;
257         struct fs_db    *fsdb = seq->private;
258         ssize_t rc;
259
260         rc = lprocfs_wr_ir_state(file, buf, len, fsdb);
261         if (rc >= 0)
262                 rc = len;
263         return rc;
264 }
265 LPROC_SEQ_FOPS(mgs_live);
266
267 int lproc_mgs_add_live(struct mgs_device *mgs, struct fs_db *fsdb)
268 {
269         int rc;
270
271         if (!mgs->mgs_proc_live)
272                 return 0;
273         rc = lprocfs_seq_create(mgs->mgs_proc_live, fsdb->fsdb_name, 0444,
274                                 &mgs_live_fops, fsdb);
275
276         return 0;
277 }
278
279 int lproc_mgs_del_live(struct mgs_device *mgs, struct fs_db *fsdb)
280 {
281         if (!mgs->mgs_proc_live)
282                 return 0;
283
284         lprocfs_remove_proc_entry(fsdb->fsdb_name, mgs->mgs_proc_live);
285         return 0;
286 }
287
288 struct lprocfs_vars lprocfs_mgs_obd_vars[] = {
289         { "uuid",            lprocfs_rd_uuid,        0, 0 },
290         { "fstype",          lprocfs_rd_fstype,      0, 0 },
291         { "mntdev",          lprocfs_mgs_rd_mntdev,  0, 0 },
292         { "num_exports",     lprocfs_rd_num_exports, 0, 0 },
293         { "hash_stats",      lprocfs_obd_rd_hash,    0, 0 },
294         { "evict_client",    0, lprocfs_wr_evict_client, 0 },
295         { "ir_timeout",      lprocfs_rd_ir_timeout, lprocfs_wr_ir_timeout, 0 },
296         { 0 }
297 };
298
299 struct lprocfs_vars lprocfs_mgs_module_vars[] = {
300         { 0 }
301 };
302
303 void mgs_counter_incr(struct obd_export *exp, int opcode)
304 {
305         lprocfs_counter_incr(exp->exp_obd->obd_stats, opcode);
306         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
307                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
308 }
309
310 void mgs_stats_counter_init(struct lprocfs_stats *stats)
311 {
312         lprocfs_counter_init(stats, LPROC_MGS_CONNECT, 0, "connect", "reqs");
313         lprocfs_counter_init(stats, LPROC_MGS_DISCONNECT, 0, "disconnect",
314                              "reqs");
315         lprocfs_counter_init(stats, LPROC_MGS_EXCEPTION, 0, "exception",
316                              "reqs");
317         lprocfs_counter_init(stats, LPROC_MGS_TARGET_REG, 0, "tgtreg", "reqs");
318         lprocfs_counter_init(stats, LPROC_MGS_TARGET_DEL, 0, "tgtdel", "reqs");
319 }
320
321 void lprocfs_mgs_init_vars(struct lprocfs_static_vars *lvars)
322 {
323     lvars->module_vars  = lprocfs_mgs_module_vars;
324     lvars->obd_vars     = lprocfs_mgs_obd_vars;
325 }
326 #endif