Whamcloud - gitweb
LU-8955 sec: create new function sptlrpc_get_sepol()
[fs/lustre-release.git] / lustre / ptlrpc / sec_lproc.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) 2014, 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  * lustre/ptlrpc/sec_lproc.c
33  *
34  * Author: Eric Mei <ericm@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_SEC
38
39 #include <libcfs/libcfs.h>
40 #include <linux/crypto.h>
41
42 #include <obd.h>
43 #include <obd_class.h>
44 #include <obd_support.h>
45 #include <lustre_net.h>
46 #include <lustre_import.h>
47 #include <lustre_dlm.h>
48 #include <lustre_sec.h>
49
50 #include "ptlrpc_internal.h"
51
52
53 struct proc_dir_entry *sptlrpc_proc_root = NULL;
54 EXPORT_SYMBOL(sptlrpc_proc_root);
55
56 static char *sec_flags2str(unsigned long flags, char *buf, int bufsize)
57 {
58         buf[0] = '\0';
59
60         if (flags & PTLRPC_SEC_FL_REVERSE)
61                 strlcat(buf, "reverse,", bufsize);
62         if (flags & PTLRPC_SEC_FL_ROOTONLY)
63                 strlcat(buf, "rootonly,", bufsize);
64         if (flags & PTLRPC_SEC_FL_UDESC)
65                 strlcat(buf, "udesc,", bufsize);
66         if (flags & PTLRPC_SEC_FL_BULK)
67                 strlcat(buf, "bulk,", bufsize);
68         if (buf[0] == '\0')
69                 strlcat(buf, "-,", bufsize);
70
71         return buf;
72 }
73
74 static int sptlrpc_info_lprocfs_seq_show(struct seq_file *seq, void *v)
75 {
76         struct obd_device *dev = seq->private;
77         struct client_obd *cli = &dev->u.cli;
78         struct ptlrpc_sec *sec = NULL;
79         char               str[32];
80
81         LASSERT(strcmp(dev->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
82                 strcmp(dev->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
83                 strcmp(dev->obd_type->typ_name, LUSTRE_MGC_NAME) == 0 ||
84                 strcmp(dev->obd_type->typ_name, LUSTRE_LWP_NAME) == 0 ||
85                 strcmp(dev->obd_type->typ_name, LUSTRE_OSP_NAME) == 0);
86
87         if (cli->cl_import)
88                 sec = sptlrpc_import_sec_ref(cli->cl_import);
89         if (sec == NULL)
90                 goto out;
91
92         sec_flags2str(sec->ps_flvr.sf_flags, str, sizeof(str));
93
94         seq_printf(seq, "rpc flavor:    %s\n",
95                    sptlrpc_flavor2name_base(sec->ps_flvr.sf_rpc));
96         seq_printf(seq, "bulk flavor:   %s\n",
97                    sptlrpc_flavor2name_bulk(&sec->ps_flvr, str, sizeof(str)));
98         seq_printf(seq, "flags:         %s\n",
99                    sec_flags2str(sec->ps_flvr.sf_flags, str, sizeof(str)));
100         seq_printf(seq, "id:            %d\n", sec->ps_id);
101         seq_printf(seq, "refcount:      %d\n",
102                    atomic_read(&sec->ps_refcount));
103         seq_printf(seq, "nctx:  %d\n", atomic_read(&sec->ps_nctx));
104         seq_printf(seq, "gc internal    %lld\n", sec->ps_gc_interval);
105         seq_printf(seq, "gc next        %lld\n",
106                    sec->ps_gc_interval ?
107                    (s64)(sec->ps_gc_next - ktime_get_real_seconds()) : 0ll);
108
109         sptlrpc_sec_put(sec);
110 out:
111         return 0;
112 }
113
114 LPROC_SEQ_FOPS_RO(sptlrpc_info_lprocfs);
115
116 static int sptlrpc_ctxs_lprocfs_seq_show(struct seq_file *seq, void *v)
117 {
118         struct obd_device *dev = seq->private;
119         struct client_obd *cli = &dev->u.cli;
120         struct ptlrpc_sec *sec = NULL;
121
122         LASSERT(strcmp(dev->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
123                 strcmp(dev->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
124                 strcmp(dev->obd_type->typ_name, LUSTRE_MGC_NAME) == 0 ||
125                 strcmp(dev->obd_type->typ_name, LUSTRE_LWP_NAME) == 0 ||
126                 strcmp(dev->obd_type->typ_name, LUSTRE_OSP_NAME) == 0);
127
128         if (cli->cl_import)
129                 sec = sptlrpc_import_sec_ref(cli->cl_import);
130         if (sec == NULL)
131                 goto out;
132
133         if (sec->ps_policy->sp_cops->display)
134                 sec->ps_policy->sp_cops->display(sec, seq);
135
136         sptlrpc_sec_put(sec);
137 out:
138         return 0;
139 }
140
141 LPROC_SEQ_FOPS_RO(sptlrpc_ctxs_lprocfs);
142
143 static ssize_t
144 lprocfs_sptlrpc_sepol_seq_write(struct file *file, const char __user *buffer,
145                                 size_t count, void *data)
146 {
147         struct seq_file *seq = file->private_data;
148         struct obd_device *dev = seq->private;
149         struct client_obd *cli = &dev->u.cli;
150         struct obd_import *imp = cli->cl_import;
151         struct sepol_downcall_data *param;
152         int size = sizeof(*param);
153         int rc = 0;
154
155         if (count < size) {
156                 CERROR("%s: invalid data count = %lu, size = %d\n",
157                        dev->obd_name, (unsigned long) count, size);
158                 return -EINVAL;
159         }
160
161         OBD_ALLOC(param, size);
162         if (param == NULL)
163                 return -ENOMEM;
164
165         if (copy_from_user(param, buffer, size)) {
166                 CERROR("%s: bad sepol data\n", dev->obd_name);
167                 GOTO(out, rc = -EFAULT);
168         }
169
170         if (param->sdd_magic != SEPOL_DOWNCALL_MAGIC) {
171                 CERROR("%s: sepol downcall bad params\n",
172                        dev->obd_name);
173                 GOTO(out, rc = -EINVAL);
174         }
175
176         if (param->sdd_sepol_len == 0 ||
177             param->sdd_sepol_len >= sizeof(imp->imp_sec->ps_sepol)) {
178                 CERROR("%s: invalid sepol data returned\n",
179                        dev->obd_name);
180                 GOTO(out, rc = -EINVAL);
181         }
182         rc = param->sdd_sepol_len; /* save sdd_sepol_len */
183         OBD_FREE(param, size);
184         size = offsetof(struct sepol_downcall_data,
185                         sdd_sepol[rc]);
186
187         /* alloc again with real size */
188         rc = 0;
189         OBD_ALLOC(param, size);
190         if (param == NULL)
191                 return -ENOMEM;
192
193         if (copy_from_user(param, buffer, size)) {
194                 CERROR("%s: bad sepol data\n", dev->obd_name);
195                 GOTO(out, rc = -EFAULT);
196         }
197
198         spin_lock(&imp->imp_sec->ps_lock);
199         snprintf(imp->imp_sec->ps_sepol, param->sdd_sepol_len + 1, "%s",
200                  param->sdd_sepol);
201         imp->imp_sec->ps_sepol_mtime = param->sdd_sepol_mtime;
202         spin_unlock(&imp->imp_sec->ps_lock);
203
204 out:
205         if (param != NULL)
206                 OBD_FREE(param, size);
207
208         return rc ? rc : count;
209 }
210 LPROC_SEQ_FOPS_WR_ONLY(srpc, sptlrpc_sepol);
211
212 int sptlrpc_lprocfs_cliobd_attach(struct obd_device *dev)
213 {
214         int     rc;
215
216         if (strcmp(dev->obd_type->typ_name, LUSTRE_OSC_NAME) != 0 &&
217             strcmp(dev->obd_type->typ_name, LUSTRE_MDC_NAME) != 0 &&
218             strcmp(dev->obd_type->typ_name, LUSTRE_MGC_NAME) != 0 &&
219             strcmp(dev->obd_type->typ_name, LUSTRE_LWP_NAME) != 0 &&
220             strcmp(dev->obd_type->typ_name, LUSTRE_OSP_NAME) != 0) {
221                 CERROR("can't register lproc for obd type %s\n",
222                        dev->obd_type->typ_name);
223                 return -EINVAL;
224         }
225
226         rc = ldebugfs_seq_create(dev->obd_debugfs_entry, "srpc_info", 0444,
227                                  &sptlrpc_info_lprocfs_fops, dev);
228         if (rc) {
229                 CERROR("create proc entry srpc_info for %s: %d\n",
230                        dev->obd_name, rc);
231                 return rc;
232         }
233
234         rc = ldebugfs_seq_create(dev->obd_debugfs_entry, "srpc_contexts",
235                                  0444, &sptlrpc_ctxs_lprocfs_fops, dev);
236         if (rc) {
237                 CERROR("create proc entry srpc_contexts for %s: %d\n",
238                        dev->obd_name, rc);
239                 return rc;
240         }
241
242         rc = ldebugfs_seq_create(dev->obd_debugfs_entry, "srpc_sepol",
243                                  0200, &srpc_sptlrpc_sepol_fops, dev);
244         if (rc) {
245                 CERROR("create proc entry srpc_sepol for %s: %d\n",
246                        dev->obd_name, rc);
247                 return rc;
248         }
249
250         return 0;
251 }
252 EXPORT_SYMBOL(sptlrpc_lprocfs_cliobd_attach);
253
254 LPROC_SEQ_FOPS_RO(sptlrpc_proc_enc_pool);
255 static struct lprocfs_vars sptlrpc_lprocfs_vars[] = {
256         { .name =       "encrypt_page_pools",
257           .fops =       &sptlrpc_proc_enc_pool_fops     },
258         { NULL }
259 };
260
261 int sptlrpc_lproc_init(void)
262 {
263         int rc;
264
265         LASSERT(sptlrpc_proc_root == NULL);
266
267         sptlrpc_proc_root = lprocfs_register("sptlrpc", proc_lustre_root,
268                                              sptlrpc_lprocfs_vars, NULL);
269         if (IS_ERR(sptlrpc_proc_root)) {
270                 rc = PTR_ERR(sptlrpc_proc_root);
271                 sptlrpc_proc_root = NULL;
272                 return rc;
273         }
274         return 0;
275 }
276
277 void sptlrpc_lproc_fini(void)
278 {
279         if (sptlrpc_proc_root) {
280                 lprocfs_remove(&sptlrpc_proc_root);
281                 sptlrpc_proc_root = NULL;
282         }
283 }