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