Whamcloud - gitweb
LU-12567 ptlrpc: handle reply and resend reorder
[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  *
31  * lustre/ptlrpc/sec_lproc.c
32  *
33  * Author: Eric Mei <ericm@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_SEC
37
38 #include <libcfs/libcfs.h>
39 #include <linux/crypto.h>
40
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <obd_support.h>
44 #include <lustre_net.h>
45 #include <lustre_import.h>
46 #include <lustre_dlm.h>
47 #include <lustre_sec.h>
48
49 #include "ptlrpc_internal.h"
50
51 static char *sec_flags2str(unsigned long flags, char *buf, int bufsize)
52 {
53         buf[0] = '\0';
54
55         if (flags & PTLRPC_SEC_FL_REVERSE)
56                 strlcat(buf, "reverse,", bufsize);
57         if (flags & PTLRPC_SEC_FL_ROOTONLY)
58                 strlcat(buf, "rootonly,", bufsize);
59         if (flags & PTLRPC_SEC_FL_UDESC)
60                 strlcat(buf, "udesc,", bufsize);
61         if (flags & PTLRPC_SEC_FL_BULK)
62                 strlcat(buf, "bulk,", bufsize);
63         if (buf[0] == '\0')
64                 strlcat(buf, "-,", bufsize);
65
66         return buf;
67 }
68
69 static int sptlrpc_info_lprocfs_seq_show(struct seq_file *seq, void *v)
70 {
71         struct obd_device *obd = seq->private;
72         struct client_obd *cli = &obd->u.cli;
73         struct ptlrpc_sec *sec = NULL;
74         char               str[32];
75
76         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
77                 strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
78                 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) == 0 ||
79                 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0 ||
80                 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0);
81
82         if (cli->cl_import)
83                 sec = sptlrpc_import_sec_ref(cli->cl_import);
84         if (sec == NULL)
85                 goto out;
86
87         sec_flags2str(sec->ps_flvr.sf_flags, str, sizeof(str));
88
89         seq_printf(seq, "rpc flavor:    %s\n",
90                    sptlrpc_flavor2name_base(sec->ps_flvr.sf_rpc));
91         seq_printf(seq, "bulk flavor:   %s\n",
92                    sptlrpc_flavor2name_bulk(&sec->ps_flvr, str, sizeof(str)));
93         seq_printf(seq, "flags:         %s\n",
94                    sec_flags2str(sec->ps_flvr.sf_flags, str, sizeof(str)));
95         seq_printf(seq, "id:            %d\n", sec->ps_id);
96         seq_printf(seq, "refcount:      %d\n",
97                    atomic_read(&sec->ps_refcount));
98         seq_printf(seq, "nctx:  %d\n", atomic_read(&sec->ps_nctx));
99         seq_printf(seq, "gc internal    %lld\n", sec->ps_gc_interval);
100         seq_printf(seq, "gc next        %lld\n",
101                    sec->ps_gc_interval ?
102                    (s64)(sec->ps_gc_next - ktime_get_real_seconds()) : 0ll);
103
104         sptlrpc_sec_put(sec);
105 out:
106         return 0;
107 }
108
109 LDEBUGFS_SEQ_FOPS_RO(sptlrpc_info_lprocfs);
110
111 static int sptlrpc_ctxs_lprocfs_seq_show(struct seq_file *seq, void *v)
112 {
113         struct obd_device *obd = seq->private;
114         struct client_obd *cli = &obd->u.cli;
115         struct ptlrpc_sec *sec = NULL;
116
117         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
118                 strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
119                 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) == 0 ||
120                 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0 ||
121                 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0);
122
123         if (cli->cl_import)
124                 sec = sptlrpc_import_sec_ref(cli->cl_import);
125         if (sec == NULL)
126                 goto out;
127
128         if (sec->ps_policy->sp_cops->display)
129                 sec->ps_policy->sp_cops->display(sec, seq);
130
131         sptlrpc_sec_put(sec);
132 out:
133         return 0;
134 }
135
136 LDEBUGFS_SEQ_FOPS_RO(sptlrpc_ctxs_lprocfs);
137
138 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
139 static ssize_t sepol_seq_write_old(struct obd_device *obd,
140                                    const char __user *buffer,
141                                    size_t count)
142 {
143         struct client_obd *cli = &obd->u.cli;
144         struct obd_import *imp = cli->cl_import;
145         struct sepol_downcall_data_old *param;
146         int size = sizeof(*param);
147         __u16 len;
148         int rc = 0;
149
150         if (count < size) {
151                 rc = -EINVAL;
152                 CERROR("%s: invalid data count = %lu, size = %d: rc = %d\n",
153                        obd->obd_name, (unsigned long) count, size, rc);
154                 return rc;
155         }
156
157         OBD_ALLOC(param, size);
158         if (param == NULL)
159                 return -ENOMEM;
160
161         if (copy_from_user(param, buffer, size)) {
162                 rc = -EFAULT;
163                 CERROR("%s: bad sepol data: rc = %d\n", obd->obd_name, rc);
164                 GOTO(out, rc);
165         }
166
167         if (param->sdd_magic != SEPOL_DOWNCALL_MAGIC_OLD) {
168                 rc = -EINVAL;
169                 CERROR("%s: sepol downcall bad params: rc = %d\n",
170                        obd->obd_name, rc);
171                 GOTO(out, rc);
172         }
173
174         if (param->sdd_sepol_len == 0 ||
175             param->sdd_sepol_len >= sizeof(imp->imp_sec->ps_sepol)) {
176                 rc = -EINVAL;
177                 CERROR("%s: invalid sepol data returned: rc = %d\n",
178                        obd->obd_name, rc);
179                 GOTO(out, rc);
180         }
181         len = param->sdd_sepol_len; /* save sdd_sepol_len */
182         OBD_FREE(param, size);
183         size = offsetof(struct sepol_downcall_data_old,
184                         sdd_sepol[len]);
185
186         if (count < size) {
187                 rc = -EINVAL;
188                 CERROR("%s: invalid sepol count = %lu, size = %d: rc = %d\n",
189                        obd->obd_name, (unsigned long) count, size, rc);
190                 return rc;
191         }
192
193         /* alloc again with real size */
194         OBD_ALLOC(param, size);
195         if (param == NULL)
196                 return -ENOMEM;
197
198         if (copy_from_user(param, buffer, size)) {
199                 rc = -EFAULT;
200                 CERROR("%s: cannot copy sepol data: rc = %d\n",
201                        obd->obd_name, rc);
202                 GOTO(out, rc);
203         }
204
205         spin_lock(&imp->imp_sec->ps_lock);
206         snprintf(imp->imp_sec->ps_sepol, param->sdd_sepol_len + 1, "%s",
207                  param->sdd_sepol);
208         imp->imp_sec->ps_sepol_mtime = ktime_set(param->sdd_sepol_mtime, 0);
209         spin_unlock(&imp->imp_sec->ps_lock);
210
211 out:
212         if (param != NULL)
213                 OBD_FREE(param, size);
214
215         return rc ? rc : count;
216 }
217 #endif
218
219 static ssize_t
220 ldebugfs_sptlrpc_sepol_seq_write(struct file *file, const char __user *buffer,
221                                  size_t count, void *data)
222 {
223         struct seq_file *seq = file->private_data;
224         struct obd_device *obd = seq->private;
225         struct client_obd *cli = &obd->u.cli;
226         struct obd_import *imp = cli->cl_import;
227         struct sepol_downcall_data *param;
228         __u32 magic;
229         int size = sizeof(magic);
230         __u16 len;
231         int rc = 0;
232
233         if (count < size) {
234                 rc = -EINVAL;
235                 CERROR("%s: invalid buffer count = %lu, size = %d: rc = %d\n",
236                        obd->obd_name, (unsigned long) count, size, rc);
237                 return rc;
238         }
239
240         if (copy_from_user(&magic, buffer, size)) {
241                 rc = -EFAULT;
242                 CERROR("%s: bad sepol magic: rc = %d\n", obd->obd_name, rc);
243                 return rc;
244         }
245
246         if (magic != SEPOL_DOWNCALL_MAGIC) {
247 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
248                 if (magic == SEPOL_DOWNCALL_MAGIC_OLD) {
249                         return sepol_seq_write_old(obd, buffer, count);
250                 }
251 #endif
252                 rc = -EINVAL;
253                 CERROR("%s: sepol downcall bad magic '%#08x': rc = %d\n",
254                        obd->obd_name, magic, rc);
255                 return rc;
256         }
257
258         size = sizeof(*param);
259         if (count < size) {
260                 rc = -EINVAL;
261                 CERROR("%s: invalid data count = %lu, size = %d: rc = %d\n",
262                        obd->obd_name, (unsigned long) count, size, rc);
263                 return rc;
264         }
265
266         OBD_ALLOC(param, size);
267         if (param == NULL)
268                 return -ENOMEM;
269
270         if (copy_from_user(param, buffer, size)) {
271                 rc = -EFAULT;
272                 CERROR("%s: bad sepol data: rc = %d\n", obd->obd_name, rc);
273                 GOTO(out, rc);
274         }
275
276         if (param->sdd_sepol_len == 0 ||
277             param->sdd_sepol_len >= sizeof(imp->imp_sec->ps_sepol)) {
278                 rc = -EINVAL;
279                 CERROR("%s: invalid sepol data returned: rc = %d\n",
280                        obd->obd_name, rc);
281                 GOTO(out, rc);
282         }
283         len = param->sdd_sepol_len; /* save sdd_sepol_len */
284         OBD_FREE(param, size);
285         size = offsetof(struct sepol_downcall_data,
286                         sdd_sepol[len]);
287
288         /* alloc again with real size */
289         OBD_ALLOC(param, size);
290         if (param == NULL)
291                 return -ENOMEM;
292
293         if (copy_from_user(param, buffer, size)) {
294                 rc = -EFAULT;
295                 CERROR("%s: cannot copy sepol data: rc = %d\n",
296                        obd->obd_name, rc);
297                 GOTO(out, rc);
298         }
299
300         spin_lock(&imp->imp_sec->ps_lock);
301         snprintf(imp->imp_sec->ps_sepol, param->sdd_sepol_len + 1, "%s",
302                  param->sdd_sepol);
303         imp->imp_sec->ps_sepol_mtime = ktime_set(param->sdd_sepol_mtime, 0);
304         spin_unlock(&imp->imp_sec->ps_lock);
305
306 out:
307         if (param != NULL)
308                 OBD_FREE(param, size);
309
310         return rc ? rc : count;
311 }
312 LDEBUGFS_FOPS_WR_ONLY(srpc, sptlrpc_sepol);
313
314 int sptlrpc_lprocfs_cliobd_attach(struct obd_device *obd)
315 {
316         if (strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) != 0 &&
317             strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) != 0 &&
318             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) != 0 &&
319             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) != 0 &&
320             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) != 0) {
321                 CERROR("can't register lproc for obd type %s\n",
322                        obd->obd_type->typ_name);
323                 return -EINVAL;
324         }
325
326         debugfs_create_file("srpc_info", 0444, obd->obd_debugfs_entry, obd,
327                             &sptlrpc_info_lprocfs_fops);
328
329         debugfs_create_file("srpc_contexts", 0444, obd->obd_debugfs_entry, obd,
330                             &sptlrpc_ctxs_lprocfs_fops);
331
332         debugfs_create_file("srpc_sepol", 0200, obd->obd_debugfs_entry, obd,
333                             &srpc_sptlrpc_sepol_fops);
334
335         return 0;
336 }
337 EXPORT_SYMBOL(sptlrpc_lprocfs_cliobd_attach);
338
339 LDEBUGFS_SEQ_FOPS_RO(sptlrpc_proc_enc_pool);
340
341 static struct ldebugfs_vars sptlrpc_lprocfs_vars[] = {
342         { .name =       "encrypt_page_pools",
343           .fops =       &sptlrpc_proc_enc_pool_fops     },
344         { NULL }
345 };
346
347 struct dentry *sptlrpc_debugfs_dir;
348 EXPORT_SYMBOL(sptlrpc_debugfs_dir);
349
350 struct proc_dir_entry *sptlrpc_lprocfs_dir;
351 EXPORT_SYMBOL(sptlrpc_lprocfs_dir);
352
353 int sptlrpc_lproc_init(void)
354 {
355         int rc;
356
357         LASSERT(sptlrpc_debugfs_dir == NULL);
358
359         sptlrpc_debugfs_dir = debugfs_create_dir("sptlrpc",
360                                                  debugfs_lustre_root);
361         ldebugfs_add_vars(sptlrpc_debugfs_dir, sptlrpc_lprocfs_vars, NULL);
362
363         sptlrpc_lprocfs_dir = lprocfs_register("sptlrpc", proc_lustre_root,
364                                                NULL, NULL);
365         if (IS_ERR_OR_NULL(sptlrpc_lprocfs_dir)) {
366                 rc = PTR_ERR(sptlrpc_lprocfs_dir);
367                 rc = sptlrpc_lprocfs_dir ? PTR_ERR(sptlrpc_lprocfs_dir)
368                         : -ENOMEM;
369                 sptlrpc_lprocfs_dir = NULL;
370         }
371         return 0;
372 }
373
374 void sptlrpc_lproc_fini(void)
375 {
376         debugfs_remove_recursive(sptlrpc_debugfs_dir);
377         sptlrpc_debugfs_dir = NULL;
378
379         if (!IS_ERR_OR_NULL(sptlrpc_lprocfs_dir))
380                 lprocfs_remove(&sptlrpc_lprocfs_dir);
381 }