Whamcloud - gitweb
LU-14487 modules: remove references to Sun Trademark.
[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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31 #define DEBUG_SUBSYSTEM S_CLASS
32
33 #include <linux/version.h>
34 #include <asm/statfs.h>
35 #include <obd.h>
36 #include <obd_class.h>
37 #include <lprocfs_status.h>
38 #include <uapi/linux/lustre/lustre_param.h>
39 #include "mgs_internal.h"
40
41 #ifdef CONFIG_PROC_FS
42
43 static int mgs_fs_seq_show(struct seq_file *seq, void *v)
44 {
45         struct obd_device       *obd = seq->private;
46         struct mgs_device       *mgs;
47         struct list_head         list;
48         struct mgs_direntry     *dirent, *n;
49         struct lu_env            env;
50         int rc, len;
51
52         ENTRY;
53
54         LASSERT(obd != NULL);
55         LASSERT(obd->obd_lu_dev != NULL);
56         mgs = lu2mgs_dev(obd->obd_lu_dev);
57
58         rc = lu_env_init(&env, LCT_MG_THREAD);
59         if (rc)
60                 RETURN(rc);
61
62         rc = class_dentry_readdir(&env, mgs, &list);
63         if (rc)
64                 GOTO(out, rc);
65
66         list_for_each_entry_safe(dirent, n, &list, mde_list) {
67                 list_del_init(&dirent->mde_list);
68                 len = strlen(dirent->mde_name);
69                 if (len > 7 &&
70                     strncmp(dirent->mde_name + len - 7, "-client", len) == 0)
71                         seq_printf(seq, "%.*s\n", len - 7, dirent->mde_name);
72                 mgs_direntry_free(dirent);
73         }
74
75 out:
76         lu_env_fini(&env);
77         RETURN(0);
78 }
79
80 LDEBUGFS_SEQ_FOPS_RO(mgs_fs);
81
82 static void seq_show_srpc_rules(struct seq_file *seq, const char *tgtname,
83                                 struct sptlrpc_rule_set *rset)
84 {
85         struct sptlrpc_rule *r;
86         char dirbuf[10];
87         char flvrbuf[40];
88         char net_buf[LNET_NIDSTR_SIZE];
89         const char *net;
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_NET_ANY)
96                         net = "default";
97                 else
98                         net = libcfs_net2str_r(r->sr_netid, net_buf,
99                                                sizeof(net_buf));
100
101                 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
102                         dirbuf[0] = '\0';
103                 else
104                         snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
105                                  sptlrpc_part2name(r->sr_from),
106                                  sptlrpc_part2name(r->sr_to));
107
108                 sptlrpc_flavor2name(&r->sr_flvr, flvrbuf, sizeof(flvrbuf));
109                 seq_printf(seq, "%s.srpc.flavor.%s%s=%s\n", tgtname,
110                            net, dirbuf, flvrbuf);
111         }
112 }
113
114 static int mgsself_srpc_seq_show(struct seq_file *seq, void *v)
115 {
116         struct obd_device       *obd = seq->private;
117         struct mgs_device       *mgs = lu2mgs_dev(obd->obd_lu_dev);
118         struct lu_target        *tgt = &mgs->mgs_lut;
119
120         read_lock(&tgt->lut_sptlrpc_lock);
121         seq_show_srpc_rules(seq, MGSSELF_NAME, &tgt->lut_sptlrpc_rset);
122         read_unlock(&tgt->lut_sptlrpc_lock);
123
124         return 0;
125 }
126 LDEBUGFS_SEQ_FOPS_RO(mgsself_srpc);
127
128 static int mgs_live_seq_show(struct seq_file *seq, void *v)
129 {
130         struct fs_db             *fsdb = seq->private;
131         struct mgs_tgt_srpc_conf *srpc_tgt;
132         int i;
133
134         mutex_lock(&fsdb->fsdb_mutex);
135
136         seq_printf(seq, "fsname: %s\n", fsdb->fsdb_name);
137         seq_printf(seq, "flags: %#lx     gen: %d\n",
138                    fsdb->fsdb_flags, fsdb->fsdb_gen);
139         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
140                 if (test_bit(i, fsdb->fsdb_mdt_index_map))
141                          seq_printf(seq, "%s-MDT%04x\n", fsdb->fsdb_name, i);
142         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
143                 if (test_bit(i, fsdb->fsdb_ost_index_map))
144                          seq_printf(seq, "%s-OST%04x\n", fsdb->fsdb_name, i);
145
146         seq_printf(seq, "\nSecure RPC Config Rules:\n");
147 #if 0
148         seq_printf(seq, "%s.%s=%s\n", fsdb->fsdb_name,
149                    PARAM_SRPC_UDESC, fsdb->fsdb_srpc_fl_udesc ? "yes" : "no");
150 #endif
151         for (srpc_tgt = fsdb->fsdb_srpc_tgt; srpc_tgt;
152              srpc_tgt = srpc_tgt->mtsc_next) {
153                 seq_show_srpc_rules(seq, srpc_tgt->mtsc_tgt,
154                                     &srpc_tgt->mtsc_rset);
155         }
156         seq_show_srpc_rules(seq, fsdb->fsdb_name, &fsdb->fsdb_srpc_gen);
157
158         lprocfs_rd_ir_state(seq, fsdb);
159
160         mutex_unlock(&fsdb->fsdb_mutex);
161         return 0;
162 }
163
164 static ssize_t mgs_live_seq_write(struct file *file, const char __user *buf,
165                                   size_t len, loff_t *off)
166 {
167         struct seq_file *seq  = file->private_data;
168         struct fs_db    *fsdb = seq->private;
169         ssize_t rc;
170
171         rc = lprocfs_wr_ir_state(file, buf, len, fsdb);
172         if (rc >= 0)
173                 rc = len;
174         return rc;
175 }
176 LPROC_SEQ_FOPS(mgs_live);
177
178 int lproc_mgs_add_live(struct mgs_device *mgs, struct fs_db *fsdb)
179 {
180         int rc;
181
182         if (!mgs->mgs_proc_live || fsdb->fsdb_has_lproc_entry)
183                 return 0;
184
185         rc = lprocfs_seq_create(mgs->mgs_proc_live, fsdb->fsdb_name, 0644,
186                                 &mgs_live_fops, fsdb);
187         if (!rc)
188                 fsdb->fsdb_has_lproc_entry = 1;
189
190         return rc;
191 }
192
193 int lproc_mgs_del_live(struct mgs_device *mgs, struct fs_db *fsdb)
194 {
195         if (!mgs->mgs_proc_live || !fsdb->fsdb_has_lproc_entry)
196                 return 0;
197
198         /* didn't create the proc file for MGSSELF_NAME */
199         if (!test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags))
200                 lprocfs_remove_proc_entry(fsdb->fsdb_name, mgs->mgs_proc_live);
201         return 0;
202 }
203
204 LPROC_SEQ_FOPS_RO_TYPE(mgs, hash);
205 LPROC_SEQ_FOPS_WR_ONLY(mgs, evict_client);
206 LPROC_SEQ_FOPS_RW_TYPE(mgs, ir_timeout);
207
208 static struct lprocfs_vars lprocfs_mgs_obd_vars[] = {
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 LUSTRE_RO_ATTR(num_exports);
219
220 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
221                            char *buf)
222 {
223         struct obd_device *obd = container_of(kobj, struct obd_device,
224                                               obd_kset.kobj);
225         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
226         struct kobject *osd_kobj;
227
228         if (!mgs || !mgs->mgs_fstype || !mgs->mgs_bottom)
229                 return -ENODEV;
230
231         osd_kobj = &mgs->mgs_bottom->dd_kobj;
232         return lustre_attr_show(osd_kobj, mgs->mgs_fstype, buf);
233 }
234 LUSTRE_RO_ATTR(fstype);
235
236 static ssize_t mntdev_show(struct kobject *kobj, struct attribute *attr,
237                            char *buf)
238 {
239         struct obd_device *obd = container_of(kobj, struct obd_device,
240                                               obd_kset.kobj);
241         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
242         struct kobject *osd_kobj;
243
244         if (!mgs || !mgs->mgs_mntdev || !mgs->mgs_bottom)
245                 return -ENODEV;
246
247         osd_kobj = &mgs->mgs_bottom->dd_kobj;
248         return lustre_attr_show(osd_kobj, mgs->mgs_mntdev, buf);
249 }
250 LUSTRE_RO_ATTR(mntdev);
251
252 static struct attribute *mgs_attrs[] = {
253         &lustre_attr_fstype.attr,
254         &lustre_attr_mntdev.attr,
255         &lustre_attr_num_exports.attr,
256         NULL,
257 };
258
259 int lproc_mgs_setup(struct mgs_device *mgs, const char *osd_name)
260 {
261         int osd_len = strlen(osd_name) - strlen("-osd");
262         struct obd_device *obd = mgs->mgs_obd;
263         struct kobj_type *bottom_type;
264         struct obd_device *osd_obd;
265         int rc;
266         int i;
267
268         obd->obd_vars = lprocfs_mgs_obd_vars;
269         obd->obd_ktype.default_attrs = mgs_attrs;
270         rc = lprocfs_obd_setup(obd, true);
271         if (rc != 0)
272                 GOTO(out, rc);
273
274         debugfs_create_file("filesystems", 0444, obd->obd_debugfs_entry, obd,
275                             &mgs_fs_fops);
276
277         debugfs_create_file("srpc_rules", 0400, obd->obd_debugfs_entry, obd,
278                             &mgsself_srpc_fops);
279
280         mgs->mgs_proc_live = lprocfs_register("live", obd->obd_proc_entry,
281                                               NULL, NULL);
282         if (IS_ERR(mgs->mgs_proc_live)) {
283                 rc = PTR_ERR(mgs->mgs_proc_live);
284                 mgs->mgs_proc_live = NULL;
285                 GOTO(out, rc);
286         }
287
288         obd->obd_proc_exports_entry = lprocfs_register("exports",
289                                                        obd->obd_proc_entry,
290                                                        NULL, NULL);
291         if (IS_ERR(obd->obd_proc_exports_entry)) {
292                 rc = PTR_ERR(obd->obd_proc_exports_entry);
293                 obd->obd_proc_exports_entry = NULL;
294                 GOTO(out, rc);
295         }
296
297         rc = sysfs_create_link(&obd->obd_kset.kobj, &mgs->mgs_bottom->dd_kobj,
298                                "osd");
299         if (rc) {
300                 CWARN("%s: failed to create symlink osd -> %s, rc = %d\n",
301                       kobject_name(&obd->obd_kset.kobj),
302                       kobject_name(&mgs->mgs_bottom->dd_kobj), rc);
303                 rc = 0;
304         }
305
306         bottom_type = get_ktype(&mgs->mgs_bottom->dd_kobj);
307
308         for (i = 0; bottom_type->default_attrs[i]; i++) {
309                 if (strcmp(bottom_type->default_attrs[i]->name, "fstype") == 0) {
310                         mgs->mgs_fstype = bottom_type->default_attrs[i];
311                         break;
312                 }
313         }
314
315         for (i = 0; bottom_type->default_attrs[i]; i++) {
316                 if (strcmp(bottom_type->default_attrs[i]->name, "mntdev") == 0) {
317                         mgs->mgs_mntdev = bottom_type->default_attrs[i];
318                         break;
319                 }
320         }
321
322         osd_obd = mgs->mgs_bottom->dd_lu_dev.ld_obd;
323         mgs->mgs_proc_osd = lprocfs_add_symlink("osd",
324                                                 obd->obd_proc_entry,
325                                                 "../../%s/%.*s",
326                                                 osd_obd->obd_type->typ_name,
327                                                 osd_len, /* Strip "-osd". */
328                                                 osd_name);
329         if (mgs->mgs_proc_osd == NULL)
330                 rc = -ENOMEM;
331 out:
332         if (rc != 0)
333                 lproc_mgs_cleanup(mgs);
334
335         return rc;
336 }
337
338 void lproc_mgs_cleanup(struct mgs_device *mgs)
339 {
340         struct obd_device *obd = mgs->mgs_obd;
341
342         if (obd == NULL)
343                 return;
344
345         if (mgs->mgs_proc_osd != NULL)
346                 lprocfs_remove(&mgs->mgs_proc_osd);
347
348         sysfs_remove_link(&obd->obd_kset.kobj, "osd");
349
350         if (mgs->mgs_proc_live != NULL) {
351                 /* Should be no live entries */
352                 lprocfs_remove(&mgs->mgs_proc_live);
353                 mgs->mgs_proc_live = NULL;
354         }
355
356         lprocfs_free_per_client_stats(obd);
357         lprocfs_obd_cleanup(obd);
358         lprocfs_free_obd_stats(obd);
359         lprocfs_free_md_stats(obd);
360 }
361
362 void mgs_counter_incr(struct obd_export *exp, int opcode)
363 {
364         lprocfs_counter_incr(exp->exp_obd->obd_stats, opcode);
365         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
366                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
367 }
368
369 void mgs_stats_counter_init(struct lprocfs_stats *stats)
370 {
371         lprocfs_counter_init(stats, LPROC_MGS_CONNECT, 0, "connect", "reqs");
372         lprocfs_counter_init(stats, LPROC_MGS_DISCONNECT, 0, "disconnect",
373                              "reqs");
374         lprocfs_counter_init(stats, LPROC_MGS_EXCEPTION, 0, "exception",
375                              "reqs");
376         lprocfs_counter_init(stats, LPROC_MGS_TARGET_REG, 0, "tgtreg", "reqs");
377         lprocfs_counter_init(stats, LPROC_MGS_TARGET_DEL, 0, "tgtdel", "reqs");
378 }
379 #endif