Whamcloud - gitweb
LU-1301 mgs: env and mgs_device to pass around
[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         LASSERT(mnt_get_devname(obd->u.mgs.mgs_vfsmnt));
55         *eof = 1;
56
57         return snprintf(page, count, "%s\n",
58                         mnt_get_devname(obd->u.mgs.mgs_vfsmnt));
59 }
60
61 static int mgs_fs_seq_show(struct seq_file *seq, void *v)
62 {
63         struct obd_device *obd = seq->private;
64         struct mgs_device *mgs = &obd->u.mgs;
65         cfs_list_t dentry_list;
66         struct l_linux_dirent *dirent, *n;
67         struct lu_env env;
68         int rc, len;
69         ENTRY;
70
71         LASSERT(obd != NULL);
72
73         rc = lu_env_init(&env, LCT_MG_THREAD);
74         if (rc)
75                 RETURN(rc);
76
77         rc = class_dentry_readdir(&env, mgs, &dentry_list);
78         if (rc) {
79                 CERROR("Can't read config dir\n");
80                 GOTO(out, rc);
81         }
82         cfs_list_for_each_entry_safe(dirent, n, &dentry_list, lld_list) {
83                 cfs_list_del(&dirent->lld_list);
84                 len = strlen(dirent->lld_name);
85                 if ((len > 7) && (strncmp(dirent->lld_name + len - 7, "-client",
86                                           len) == 0)) {
87                         seq_printf(seq, "%.*s\n", len - 7, dirent->lld_name);
88                 }
89                 OBD_FREE(dirent, sizeof(*dirent));
90         }
91
92 out:
93         lu_env_fini(&env);
94         RETURN(0);
95 }
96
97 LPROC_SEQ_FOPS_RO(mgs_fs);
98
99 static void seq_show_srpc_rules(struct seq_file *seq, const char *tgtname,
100                                 struct sptlrpc_rule_set *rset)
101 {
102         struct sptlrpc_rule    *r;
103         char                    dirbuf[10];
104         char                    flvrbuf[40];
105         char                   *net;
106         int                     i;
107
108         for (i = 0; i < rset->srs_nrule; i++) {
109                 r = &rset->srs_rules[i];
110
111                 if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
112                         net = "default";
113                 else
114                         net = libcfs_net2str(r->sr_netid);
115
116                 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
117                         dirbuf[0] = '\0';
118                 else
119                         snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
120                                  sptlrpc_part2name(r->sr_from),
121                                  sptlrpc_part2name(r->sr_to));
122
123                 sptlrpc_flavor2name(&r->sr_flvr, flvrbuf, sizeof(flvrbuf));
124                 seq_printf(seq, "%s.srpc.flavor.%s%s=%s\n", tgtname,
125                            net, dirbuf, flvrbuf);
126         }
127 }
128
129 static int mgsself_srpc_seq_show(struct seq_file *seq, void *v)
130 {
131         struct obd_device *obd = seq->private;
132         struct mgs_device *mgs = &obd->u.mgs;
133         struct fs_db      *fsdb;
134         struct lu_env      env;
135         int                rc;
136
137         rc = lu_env_init(&env, LCT_MG_THREAD);
138         if (rc)
139                 return rc;
140
141         rc = mgs_find_or_make_fsdb(&env, mgs, MGSSELF_NAME, &fsdb);
142         if (rc)
143                 goto out;
144
145         cfs_mutex_lock(&fsdb->fsdb_mutex);
146         seq_show_srpc_rules(seq, fsdb->fsdb_name, &fsdb->fsdb_srpc_gen);
147         cfs_mutex_unlock(&fsdb->fsdb_mutex);
148
149 out:
150         lu_env_fini(&env);
151         return 0;
152 }
153
154 LPROC_SEQ_FOPS_RO(mgsself_srpc);
155
156 int lproc_mgs_setup(struct mgs_device *mgs)
157 {
158         struct obd_device *obd = mgs->mgs_obd;
159         int rc;
160
161         rc = lprocfs_obd_seq_create(obd, "filesystems", 0444,
162                                     &mgs_fs_fops, obd);
163         rc = lprocfs_obd_seq_create(obd, "srpc_rules", 0600,
164                                     &mgsself_srpc_fops, obd);
165
166         mgs->mgs_proc_live = lprocfs_register("live", obd->obd_proc_entry,
167                                               NULL, NULL);
168         if (IS_ERR(mgs->mgs_proc_live)) {
169                 rc = PTR_ERR(mgs->mgs_proc_live);
170                 CERROR("error %d setting up lprocfs for %s\n", rc, "live");
171                 mgs->mgs_proc_live = NULL;
172         }
173
174         obd->obd_proc_exports_entry = lprocfs_register("exports",
175                                                        obd->obd_proc_entry,
176                                                        NULL, NULL);
177         if (IS_ERR(obd->obd_proc_exports_entry)) {
178                 rc = PTR_ERR(obd->obd_proc_exports_entry);
179                 CERROR("error %d setting up lprocfs for %s\n", rc, "exports");
180                 obd->obd_proc_exports_entry = NULL;
181         }
182
183         return rc;
184 }
185
186 int lproc_mgs_cleanup(struct mgs_device *mgs)
187 {
188         struct obd_device *obd = mgs->mgs_obd;
189
190         if (!obd)
191                 return -EINVAL;
192
193         if (mgs->mgs_proc_live) {
194                 /* Should be no live entries */
195                 LASSERT(mgs->mgs_proc_live->subdir == NULL);
196                 lprocfs_remove(&mgs->mgs_proc_live);
197                 mgs->mgs_proc_live = NULL;
198         }
199         lprocfs_free_per_client_stats(obd);
200         lprocfs_free_obd_stats(obd);
201         lprocfs_free_md_stats(obd);
202
203         return lprocfs_obd_cleanup(obd);
204 }
205
206 static int mgs_live_seq_show(struct seq_file *seq, void *v)
207 {
208         struct fs_db             *fsdb = seq->private;
209         struct mgs_tgt_srpc_conf *srpc_tgt;
210         int i;
211
212         cfs_mutex_lock(&fsdb->fsdb_mutex);
213
214         seq_printf(seq, "fsname: %s\n", fsdb->fsdb_name);
215         seq_printf(seq, "flags: %#lx     gen: %d\n",
216                    fsdb->fsdb_flags, fsdb->fsdb_gen);
217         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
218                  if (cfs_test_bit(i, fsdb->fsdb_mdt_index_map))
219                          seq_printf(seq, "%s-MDT%04x\n", fsdb->fsdb_name, i);
220         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
221                  if (cfs_test_bit(i, fsdb->fsdb_ost_index_map))
222                          seq_printf(seq, "%s-OST%04x\n", fsdb->fsdb_name, i);
223
224         seq_printf(seq, "\nSecure RPC Config Rules:\n");
225 #if 0
226         seq_printf(seq, "%s.%s=%s\n", fsdb->fsdb_name,
227                    PARAM_SRPC_UDESC, fsdb->fsdb_srpc_fl_udesc ? "yes" : "no");
228 #endif
229         for (srpc_tgt = fsdb->fsdb_srpc_tgt; srpc_tgt;
230              srpc_tgt = srpc_tgt->mtsc_next) {
231                 seq_show_srpc_rules(seq, srpc_tgt->mtsc_tgt,
232                                     &srpc_tgt->mtsc_rset);
233         }
234         seq_show_srpc_rules(seq, fsdb->fsdb_name, &fsdb->fsdb_srpc_gen);
235
236         lprocfs_rd_ir_state(seq, fsdb);
237
238         cfs_mutex_unlock(&fsdb->fsdb_mutex);
239         return 0;
240 }
241
242 static ssize_t mgs_live_seq_write(struct file *file, const char *buf,
243                                   size_t len, loff_t *off)
244 {
245         struct seq_file *seq  = file->private_data;
246         struct fs_db    *fsdb = seq->private;
247         ssize_t rc;
248
249         rc = lprocfs_wr_ir_state(file, buf, len, fsdb);
250         if (rc >= 0)
251                 rc = len;
252         return rc;
253 }
254 LPROC_SEQ_FOPS(mgs_live);
255
256 int lproc_mgs_add_live(struct mgs_device *mgs, struct fs_db *fsdb)
257 {
258         int rc;
259
260         if (!mgs->mgs_proc_live)
261                 return 0;
262         rc = lprocfs_seq_create(mgs->mgs_proc_live, fsdb->fsdb_name, 0444,
263                                 &mgs_live_fops, fsdb);
264
265         return 0;
266 }
267
268 int lproc_mgs_del_live(struct mgs_device *mgs, struct fs_db *fsdb)
269 {
270         if (!mgs->mgs_proc_live)
271                 return 0;
272
273         lprocfs_remove_proc_entry(fsdb->fsdb_name, mgs->mgs_proc_live);
274         return 0;
275 }
276
277 struct lprocfs_vars lprocfs_mgs_obd_vars[] = {
278         { "uuid",            lprocfs_rd_uuid,        0, 0 },
279         { "fstype",          lprocfs_rd_fstype,      0, 0 },
280         { "mntdev",          lprocfs_mgs_rd_mntdev,  0, 0 },
281         { "num_exports",     lprocfs_rd_num_exports, 0, 0 },
282         { "hash_stats",      lprocfs_obd_rd_hash,    0, 0 },
283         { "evict_client",    0, lprocfs_wr_evict_client, 0 },
284         { "ir_timeout",      lprocfs_rd_ir_timeout, lprocfs_wr_ir_timeout, 0 },
285         { 0 }
286 };
287
288 struct lprocfs_vars lprocfs_mgs_module_vars[] = {
289         { 0 }
290 };
291
292 void mgs_counter_incr(struct obd_export *exp, int opcode)
293 {
294         lprocfs_counter_incr(exp->exp_obd->obd_stats, opcode);
295         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
296                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
297 }
298
299 void mgs_stats_counter_init(struct lprocfs_stats *stats)
300 {
301         lprocfs_counter_init(stats, LPROC_MGS_CONNECT, 0, "connect", "reqs");
302         lprocfs_counter_init(stats, LPROC_MGS_DISCONNECT, 0, "disconnect",
303                              "reqs");
304         lprocfs_counter_init(stats, LPROC_MGS_EXCEPTION, 0, "exception",
305                              "reqs");
306         lprocfs_counter_init(stats, LPROC_MGS_TARGET_REG, 0, "tgtreg", "reqs");
307         lprocfs_counter_init(stats, LPROC_MGS_TARGET_DEL, 0, "tgtdel", "reqs");
308 }
309
310 void lprocfs_mgs_init_vars(struct lprocfs_static_vars *lvars)
311 {
312     lvars->module_vars  = lprocfs_mgs_module_vars;
313     lvars->obd_vars     = lprocfs_mgs_obd_vars;
314 }
315 #endif