Whamcloud - gitweb
LU-6635 lfsck: block replacing the OST-object for test
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 #define DEBUG_SUBSYSTEM S_CLASS
33
34 #include <linux/version.h>
35 #include <asm/statfs.h>
36 #include <obd.h>
37 #include <obd_class.h>
38 #include <lprocfs_status.h>
39 #include <lustre_param.h>
40 #include "mgs_internal.h"
41
42 #ifdef CONFIG_PROC_FS
43
44 static int mgs_fs_seq_show(struct seq_file *seq, void *v)
45 {
46         struct obd_device       *obd = seq->private;
47         struct mgs_device       *mgs;
48         struct list_head         list;
49         struct mgs_direntry     *dirent, *n;
50         struct lu_env            env;
51         int rc, len;
52
53         ENTRY;
54
55         LASSERT(obd != NULL);
56         LASSERT(obd->obd_lu_dev != NULL);
57         mgs = lu2mgs_dev(obd->obd_lu_dev);
58
59         rc = lu_env_init(&env, LCT_MG_THREAD);
60         if (rc)
61                 RETURN(rc);
62
63         rc = class_dentry_readdir(&env, mgs, &list);
64         if (rc)
65                 GOTO(out, rc);
66
67         list_for_each_entry_safe(dirent, n, &list, mde_list) {
68                 list_del_init(&dirent->mde_list);
69                 len = strlen(dirent->mde_name);
70                 if (len > 7 &&
71                     strncmp(dirent->mde_name + len - 7, "-client", len) == 0)
72                         seq_printf(seq, "%.*s\n", len - 7, dirent->mde_name);
73                 mgs_direntry_free(dirent);
74         }
75
76 out:
77         lu_env_fini(&env);
78         RETURN(0);
79 }
80
81 LPROC_SEQ_FOPS_RO(mgs_fs);
82
83 static void seq_show_srpc_rules(struct seq_file *seq, const char *tgtname,
84                                 struct sptlrpc_rule_set *rset)
85 {
86         struct sptlrpc_rule    *r;
87         char                    dirbuf[10];
88         char                    flvrbuf[40];
89         char                    net[LNET_NIDSTR_SIZE] = "default";
90         int                     i;
91
92         for (i = 0; i < rset->srs_nrule; i++) {
93                 r = &rset->srs_rules[i];
94
95                 if (r->sr_netid != LNET_NIDNET(LNET_NID_ANY))
96                         libcfs_net2str_r(r->sr_netid, net, sizeof(net));
97
98                 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
99                         dirbuf[0] = '\0';
100                 else
101                         snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
102                                  sptlrpc_part2name(r->sr_from),
103                                  sptlrpc_part2name(r->sr_to));
104
105                 sptlrpc_flavor2name(&r->sr_flvr, flvrbuf, sizeof(flvrbuf));
106                 seq_printf(seq, "%s.srpc.flavor.%s%s=%s\n", tgtname,
107                            net, dirbuf, flvrbuf);
108         }
109 }
110
111 static int mgsself_srpc_seq_show(struct seq_file *seq, void *v)
112 {
113         struct obd_device       *obd = seq->private;
114         struct mgs_device       *mgs = lu2mgs_dev(obd->obd_lu_dev);
115         struct lu_target        *tgt = &mgs->mgs_lut;
116
117         read_lock(&tgt->lut_sptlrpc_lock);
118         seq_show_srpc_rules(seq, MGSSELF_NAME, &tgt->lut_sptlrpc_rset);
119         read_unlock(&tgt->lut_sptlrpc_lock);
120
121         return 0;
122 }
123 LPROC_SEQ_FOPS_RO(mgsself_srpc);
124
125 static int mgs_live_seq_show(struct seq_file *seq, void *v)
126 {
127         struct fs_db             *fsdb = seq->private;
128         struct mgs_tgt_srpc_conf *srpc_tgt;
129         int i;
130
131         mutex_lock(&fsdb->fsdb_mutex);
132
133         seq_printf(seq, "fsname: %s\n", fsdb->fsdb_name);
134         seq_printf(seq, "flags: %#lx     gen: %d\n",
135                    fsdb->fsdb_flags, fsdb->fsdb_gen);
136         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
137                 if (test_bit(i, fsdb->fsdb_mdt_index_map))
138                          seq_printf(seq, "%s-MDT%04x\n", fsdb->fsdb_name, i);
139         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
140                 if (test_bit(i, fsdb->fsdb_ost_index_map))
141                          seq_printf(seq, "%s-OST%04x\n", fsdb->fsdb_name, i);
142
143         seq_printf(seq, "\nSecure RPC Config Rules:\n");
144 #if 0
145         seq_printf(seq, "%s.%s=%s\n", fsdb->fsdb_name,
146                    PARAM_SRPC_UDESC, fsdb->fsdb_srpc_fl_udesc ? "yes" : "no");
147 #endif
148         for (srpc_tgt = fsdb->fsdb_srpc_tgt; srpc_tgt;
149              srpc_tgt = srpc_tgt->mtsc_next) {
150                 seq_show_srpc_rules(seq, srpc_tgt->mtsc_tgt,
151                                     &srpc_tgt->mtsc_rset);
152         }
153         seq_show_srpc_rules(seq, fsdb->fsdb_name, &fsdb->fsdb_srpc_gen);
154
155         lprocfs_rd_ir_state(seq, fsdb);
156
157         mutex_unlock(&fsdb->fsdb_mutex);
158         return 0;
159 }
160
161 static ssize_t mgs_live_seq_write(struct file *file, const char __user *buf,
162                                   size_t len, loff_t *off)
163 {
164         struct seq_file *seq  = file->private_data;
165         struct fs_db    *fsdb = seq->private;
166         ssize_t rc;
167
168         rc = lprocfs_wr_ir_state(file, buf, len, fsdb);
169         if (rc >= 0)
170                 rc = len;
171         return rc;
172 }
173 LPROC_SEQ_FOPS(mgs_live);
174
175 int lproc_mgs_add_live(struct mgs_device *mgs, struct fs_db *fsdb)
176 {
177         int rc;
178
179         if (!mgs->mgs_proc_live)
180                 return 0;
181         rc = lprocfs_seq_create(mgs->mgs_proc_live, fsdb->fsdb_name, 0644,
182                                 &mgs_live_fops, fsdb);
183
184         return 0;
185 }
186
187 int lproc_mgs_del_live(struct mgs_device *mgs, struct fs_db *fsdb)
188 {
189         if (!mgs->mgs_proc_live)
190                 return 0;
191
192         /* didn't create the proc file for MGSSELF_NAME */
193         if (!test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags))
194                 lprocfs_remove_proc_entry(fsdb->fsdb_name, mgs->mgs_proc_live);
195         return 0;
196 }
197
198 LPROC_SEQ_FOPS_RO_TYPE(mgs, uuid);
199 LPROC_SEQ_FOPS_RO_TYPE(mgs, num_exports);
200 LPROC_SEQ_FOPS_RO_TYPE(mgs, hash);
201 LPROC_SEQ_FOPS_WO_TYPE(mgs, evict_client);
202 LPROC_SEQ_FOPS_RW_TYPE(mgs, ir_timeout);
203
204 static struct lprocfs_vars lprocfs_mgs_obd_vars[] = {
205         { .name =       "uuid",
206           .fops =       &mgs_uuid_fops          },
207         { .name =       "num_exports",
208           .fops =       &mgs_num_exports_fops   },
209         { .name =       "hash_stats",
210           .fops =       &mgs_hash_fops          },
211         { .name =       "evict_client",
212           .fops =       &mgs_evict_client_fops  },
213         { .name =       "ir_timeout",
214           .fops =       &mgs_ir_timeout_fops    },
215         { NULL }
216 };
217
218 int lproc_mgs_setup(struct mgs_device *mgs, const char *osd_name)
219 {
220         struct obd_device *obd = mgs->mgs_obd;
221         struct obd_device *osd_obd = mgs->mgs_bottom->dd_lu_dev.ld_obd;
222         int                osd_len = strlen(osd_name) - strlen("-osd");
223         int                rc;
224
225         obd->obd_vars = lprocfs_mgs_obd_vars;
226         rc = lprocfs_obd_setup(obd);
227         if (rc != 0)
228                 GOTO(out, rc);
229
230         rc = lprocfs_obd_seq_create(obd, "filesystems", 0444,
231                                     &mgs_fs_fops, obd);
232         if (rc != 0)
233                 GOTO(out, rc);
234
235         rc = lprocfs_obd_seq_create(obd, "srpc_rules", 0400,
236                                     &mgsself_srpc_fops, obd);
237         if (rc != 0)
238                 GOTO(out, rc);
239
240         mgs->mgs_proc_live = lprocfs_register("live", obd->obd_proc_entry,
241                                               NULL, NULL);
242         if (IS_ERR(mgs->mgs_proc_live)) {
243                 rc = PTR_ERR(mgs->mgs_proc_live);
244                 mgs->mgs_proc_live = NULL;
245                 GOTO(out, rc);
246         }
247
248         obd->obd_proc_exports_entry = lprocfs_register("exports",
249                                                        obd->obd_proc_entry,
250                                                        NULL, NULL);
251         if (IS_ERR(obd->obd_proc_exports_entry)) {
252                 rc = PTR_ERR(obd->obd_proc_exports_entry);
253                 obd->obd_proc_exports_entry = NULL;
254                 GOTO(out, rc);
255         }
256
257         mgs->mgs_proc_osd = lprocfs_add_symlink("osd",
258                                                 obd->obd_proc_entry,
259                                                 "../../%s/%.*s",
260                                                 osd_obd->obd_type->typ_name,
261                                                 osd_len, /* Strip "-osd". */
262                                                 osd_name);
263         if (mgs->mgs_proc_osd == NULL)
264                 GOTO(out, rc = -ENOMEM);
265
266         mgs->mgs_proc_mntdev = lprocfs_add_symlink("mntdev",
267                                                    obd->obd_proc_entry,
268                                                    "osd/mntdev");
269         if (mgs->mgs_proc_mntdev == NULL)
270                 GOTO(out, rc = -ENOMEM);
271
272         mgs->mgs_proc_fstype = lprocfs_add_symlink("fstype",
273                                                    obd->obd_proc_entry,
274                                                    "osd/fstype");
275         if (mgs->mgs_proc_fstype == NULL)
276                 GOTO(out, rc = -ENOMEM);
277
278 out:
279         if (rc != 0)
280                 lproc_mgs_cleanup(mgs);
281
282         return rc;
283 }
284
285 void lproc_mgs_cleanup(struct mgs_device *mgs)
286 {
287         struct obd_device *obd = mgs->mgs_obd;
288
289         if (obd == NULL)
290                 return;
291
292         if (mgs->mgs_proc_osd != NULL)
293                 lprocfs_remove(&mgs->mgs_proc_osd);
294
295         if (mgs->mgs_proc_fstype != NULL)
296                 lprocfs_remove(&mgs->mgs_proc_fstype);
297
298         if (mgs->mgs_proc_mntdev != NULL)
299                 lprocfs_remove(&mgs->mgs_proc_mntdev);
300
301         if (mgs->mgs_proc_live != NULL) {
302                 /* Should be no live entries */
303                 lprocfs_remove(&mgs->mgs_proc_live);
304                 mgs->mgs_proc_live = NULL;
305         }
306
307         lprocfs_free_per_client_stats(obd);
308         lprocfs_obd_cleanup(obd);
309         lprocfs_free_obd_stats(obd);
310         lprocfs_free_md_stats(obd);
311 }
312
313 void mgs_counter_incr(struct obd_export *exp, int opcode)
314 {
315         lprocfs_counter_incr(exp->exp_obd->obd_stats, opcode);
316         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
317                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
318 }
319
320 void mgs_stats_counter_init(struct lprocfs_stats *stats)
321 {
322         lprocfs_counter_init(stats, LPROC_MGS_CONNECT, 0, "connect", "reqs");
323         lprocfs_counter_init(stats, LPROC_MGS_DISCONNECT, 0, "disconnect",
324                              "reqs");
325         lprocfs_counter_init(stats, LPROC_MGS_EXCEPTION, 0, "exception",
326                              "reqs");
327         lprocfs_counter_init(stats, LPROC_MGS_TARGET_REG, 0, "tgtreg", "reqs");
328         lprocfs_counter_init(stats, LPROC_MGS_TARGET_DEL, 0, "tgtdel", "reqs");
329 }
330 #endif