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