Whamcloud - gitweb
LU-8066 osd: migrate from proc to sysfs
[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  * 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 <uapi/linux/lustre/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_buf[LNET_NIDSTR_SIZE];
90         const char *net;
91         int i;
92
93         for (i = 0; i < rset->srs_nrule; i++) {
94                 r = &rset->srs_rules[i];
95
96                 if (r->sr_netid == LNET_NIDNET(LNET_NID_ANY))
97                         net = "default";
98                 else
99                         net = libcfs_net2str_r(r->sr_netid, net_buf,
100                                                sizeof(net_buf));
101
102                 if (r->sr_from == LUSTRE_SP_ANY && r->sr_to == LUSTRE_SP_ANY)
103                         dirbuf[0] = '\0';
104                 else
105                         snprintf(dirbuf, sizeof(dirbuf), ".%s2%s",
106                                  sptlrpc_part2name(r->sr_from),
107                                  sptlrpc_part2name(r->sr_to));
108
109                 sptlrpc_flavor2name(&r->sr_flvr, flvrbuf, sizeof(flvrbuf));
110                 seq_printf(seq, "%s.srpc.flavor.%s%s=%s\n", tgtname,
111                            net, dirbuf, flvrbuf);
112         }
113 }
114
115 static int mgsself_srpc_seq_show(struct seq_file *seq, void *v)
116 {
117         struct obd_device       *obd = seq->private;
118         struct mgs_device       *mgs = lu2mgs_dev(obd->obd_lu_dev);
119         struct lu_target        *tgt = &mgs->mgs_lut;
120
121         read_lock(&tgt->lut_sptlrpc_lock);
122         seq_show_srpc_rules(seq, MGSSELF_NAME, &tgt->lut_sptlrpc_rset);
123         read_unlock(&tgt->lut_sptlrpc_lock);
124
125         return 0;
126 }
127 LPROC_SEQ_FOPS_RO(mgsself_srpc);
128
129 static int mgs_live_seq_show(struct seq_file *seq, void *v)
130 {
131         struct fs_db             *fsdb = seq->private;
132         struct mgs_tgt_srpc_conf *srpc_tgt;
133         int i;
134
135         mutex_lock(&fsdb->fsdb_mutex);
136
137         seq_printf(seq, "fsname: %s\n", fsdb->fsdb_name);
138         seq_printf(seq, "flags: %#lx     gen: %d\n",
139                    fsdb->fsdb_flags, fsdb->fsdb_gen);
140         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
141                 if (test_bit(i, fsdb->fsdb_mdt_index_map))
142                          seq_printf(seq, "%s-MDT%04x\n", fsdb->fsdb_name, i);
143         for (i = 0; i < INDEX_MAP_SIZE * 8; i++)
144                 if (test_bit(i, fsdb->fsdb_ost_index_map))
145                          seq_printf(seq, "%s-OST%04x\n", fsdb->fsdb_name, i);
146
147         seq_printf(seq, "\nSecure RPC Config Rules:\n");
148 #if 0
149         seq_printf(seq, "%s.%s=%s\n", fsdb->fsdb_name,
150                    PARAM_SRPC_UDESC, fsdb->fsdb_srpc_fl_udesc ? "yes" : "no");
151 #endif
152         for (srpc_tgt = fsdb->fsdb_srpc_tgt; srpc_tgt;
153              srpc_tgt = srpc_tgt->mtsc_next) {
154                 seq_show_srpc_rules(seq, srpc_tgt->mtsc_tgt,
155                                     &srpc_tgt->mtsc_rset);
156         }
157         seq_show_srpc_rules(seq, fsdb->fsdb_name, &fsdb->fsdb_srpc_gen);
158
159         lprocfs_rd_ir_state(seq, fsdb);
160
161         mutex_unlock(&fsdb->fsdb_mutex);
162         return 0;
163 }
164
165 static ssize_t mgs_live_seq_write(struct file *file, const char __user *buf,
166                                   size_t len, loff_t *off)
167 {
168         struct seq_file *seq  = file->private_data;
169         struct fs_db    *fsdb = seq->private;
170         ssize_t rc;
171
172         rc = lprocfs_wr_ir_state(file, buf, len, fsdb);
173         if (rc >= 0)
174                 rc = len;
175         return rc;
176 }
177 LPROC_SEQ_FOPS(mgs_live);
178
179 int lproc_mgs_add_live(struct mgs_device *mgs, struct fs_db *fsdb)
180 {
181         int rc;
182
183         if (!mgs->mgs_proc_live || fsdb->fsdb_has_lproc_entry)
184                 return 0;
185
186         rc = lprocfs_seq_create(mgs->mgs_proc_live, fsdb->fsdb_name, 0644,
187                                 &mgs_live_fops, fsdb);
188         if (!rc)
189                 fsdb->fsdb_has_lproc_entry = 1;
190
191         return rc;
192 }
193
194 int lproc_mgs_del_live(struct mgs_device *mgs, struct fs_db *fsdb)
195 {
196         if (!mgs->mgs_proc_live || !fsdb->fsdb_has_lproc_entry)
197                 return 0;
198
199         /* didn't create the proc file for MGSSELF_NAME */
200         if (!test_bit(FSDB_MGS_SELF, &fsdb->fsdb_flags))
201                 lprocfs_remove_proc_entry(fsdb->fsdb_name, mgs->mgs_proc_live);
202         return 0;
203 }
204
205 LPROC_SEQ_FOPS_RO_TYPE(mgs, num_exports);
206 LPROC_SEQ_FOPS_RO_TYPE(mgs, hash);
207 LPROC_SEQ_FOPS_WR_ONLY(mgs, evict_client);
208 LPROC_SEQ_FOPS_RW_TYPE(mgs, ir_timeout);
209
210 static struct lprocfs_vars lprocfs_mgs_obd_vars[] = {
211         { .name =       "num_exports",
212           .fops =       &mgs_num_exports_fops   },
213         { .name =       "hash_stats",
214           .fops =       &mgs_hash_fops          },
215         { .name =       "evict_client",
216           .fops =       &mgs_evict_client_fops  },
217         { .name =       "ir_timeout",
218           .fops =       &mgs_ir_timeout_fops    },
219         { NULL }
220 };
221
222 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
223                            char *buf)
224 {
225         struct obd_device *obd = container_of(kobj, struct obd_device,
226                                               obd_kset.kobj);
227         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
228         struct kobject *osd_kobj;
229
230         if (!mgs || !mgs->mgs_fstype || !mgs->mgs_bottom)
231                 return -ENODEV;
232
233         osd_kobj = &mgs->mgs_bottom->dd_kobj;
234         return lustre_attr_show(osd_kobj, mgs->mgs_fstype, buf);
235 }
236 LUSTRE_RO_ATTR(fstype);
237
238 static ssize_t mntdev_show(struct kobject *kobj, struct attribute *attr,
239                            char *buf)
240 {
241         struct obd_device *obd = container_of(kobj, struct obd_device,
242                                               obd_kset.kobj);
243         struct mgs_device *mgs = lu2mgs_dev(obd->obd_lu_dev);
244         struct kobject *osd_kobj;
245
246         if (!mgs || !mgs->mgs_mntdev || !mgs->mgs_bottom)
247                 return -ENODEV;
248
249         osd_kobj = &mgs->mgs_bottom->dd_kobj;
250         return lustre_attr_show(osd_kobj, mgs->mgs_mntdev, buf);
251 }
252 LUSTRE_RO_ATTR(mntdev);
253
254 static struct attribute *mgs_attrs[] = {
255         &lustre_attr_fstype.attr,
256         &lustre_attr_mntdev.attr,
257         NULL,
258 };
259
260 int lproc_mgs_setup(struct mgs_device *mgs, const char *osd_name)
261 {
262         int osd_len = strlen(osd_name) - strlen("-osd");
263         struct obd_device *obd = mgs->mgs_obd;
264         struct kobj_type *bottom_type;
265         struct obd_device *osd_obd;
266         int rc;
267         int i;
268
269         obd->obd_vars = lprocfs_mgs_obd_vars;
270         obd->obd_ktype.default_attrs = mgs_attrs;
271         rc = lprocfs_obd_setup(obd, true);
272         if (rc != 0)
273                 GOTO(out, rc);
274
275         rc = ldebugfs_seq_create(obd->obd_debugfs_entry, "filesystems", 0444,
276                                  &mgs_fs_fops, obd);
277         if (rc != 0)
278                 GOTO(out, rc);
279
280         rc = ldebugfs_seq_create(obd->obd_debugfs_entry, "srpc_rules", 0400,
281                                  &mgsself_srpc_fops, obd);
282         if (rc != 0)
283                 GOTO(out, rc);
284
285         mgs->mgs_proc_live = lprocfs_register("live", obd->obd_proc_entry,
286                                               NULL, NULL);
287         if (IS_ERR(mgs->mgs_proc_live)) {
288                 rc = PTR_ERR(mgs->mgs_proc_live);
289                 mgs->mgs_proc_live = NULL;
290                 GOTO(out, rc);
291         }
292
293         obd->obd_proc_exports_entry = lprocfs_register("exports",
294                                                        obd->obd_proc_entry,
295                                                        NULL, NULL);
296         if (IS_ERR(obd->obd_proc_exports_entry)) {
297                 rc = PTR_ERR(obd->obd_proc_exports_entry);
298                 obd->obd_proc_exports_entry = NULL;
299                 GOTO(out, rc);
300         }
301
302         rc = sysfs_create_link(&obd->obd_kset.kobj, &mgs->mgs_bottom->dd_kobj,
303                                "osd");
304         if (rc) {
305                 CWARN("%s: failed to create symlink osd -> %s, rc = %d\n",
306                       kobject_name(&obd->obd_kset.kobj),
307                       kobject_name(&mgs->mgs_bottom->dd_kobj), rc);
308                 rc = 0;
309         }
310
311         bottom_type = get_ktype(&mgs->mgs_bottom->dd_kobj);
312
313         for (i = 0; bottom_type->default_attrs[i]; i++) {
314                 if (strcmp(bottom_type->default_attrs[i]->name, "fstype") == 0) {
315                         mgs->mgs_fstype = bottom_type->default_attrs[i];
316                         break;
317                 }
318         }
319
320         for (i = 0; bottom_type->default_attrs[i]; i++) {
321                 if (strcmp(bottom_type->default_attrs[i]->name, "mntdev") == 0) {
322                         mgs->mgs_mntdev = bottom_type->default_attrs[i];
323                         break;
324                 }
325         }
326
327         osd_obd = mgs->mgs_bottom->dd_lu_dev.ld_obd;
328         mgs->mgs_proc_osd = lprocfs_add_symlink("osd",
329                                                 obd->obd_proc_entry,
330                                                 "../../%s/%.*s",
331                                                 osd_obd->obd_type->typ_name,
332                                                 osd_len, /* Strip "-osd". */
333                                                 osd_name);
334         if (mgs->mgs_proc_osd == NULL)
335                 rc = -ENOMEM;
336 out:
337         if (rc != 0)
338                 lproc_mgs_cleanup(mgs);
339
340         return rc;
341 }
342
343 void lproc_mgs_cleanup(struct mgs_device *mgs)
344 {
345         struct obd_device *obd = mgs->mgs_obd;
346
347         if (obd == NULL)
348                 return;
349
350         if (mgs->mgs_proc_osd != NULL)
351                 lprocfs_remove(&mgs->mgs_proc_osd);
352
353         sysfs_remove_link(&obd->obd_kset.kobj, "osd");
354
355         if (mgs->mgs_proc_live != NULL) {
356                 /* Should be no live entries */
357                 lprocfs_remove(&mgs->mgs_proc_live);
358                 mgs->mgs_proc_live = NULL;
359         }
360
361         lprocfs_free_per_client_stats(obd);
362         lprocfs_obd_cleanup(obd);
363         lprocfs_free_obd_stats(obd);
364         lprocfs_free_md_stats(obd);
365 }
366
367 void mgs_counter_incr(struct obd_export *exp, int opcode)
368 {
369         lprocfs_counter_incr(exp->exp_obd->obd_stats, opcode);
370         if (exp->exp_nid_stats && exp->exp_nid_stats->nid_stats != NULL)
371                 lprocfs_counter_incr(exp->exp_nid_stats->nid_stats, opcode);
372 }
373
374 void mgs_stats_counter_init(struct lprocfs_stats *stats)
375 {
376         lprocfs_counter_init(stats, LPROC_MGS_CONNECT, 0, "connect", "reqs");
377         lprocfs_counter_init(stats, LPROC_MGS_DISCONNECT, 0, "disconnect",
378                              "reqs");
379         lprocfs_counter_init(stats, LPROC_MGS_EXCEPTION, 0, "exception",
380                              "reqs");
381         lprocfs_counter_init(stats, LPROC_MGS_TARGET_REG, 0, "tgtreg", "reqs");
382         lprocfs_counter_init(stats, LPROC_MGS_TARGET_DEL, 0, "tgtdel", "reqs");
383 }
384 #endif