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  *
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 interval    %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 static inline
139 bool sptlrpc_sepol_update_needed(struct ptlrpc_sec *imp_sec,
140                                  ktime_t mtime, char *pol, size_t pol_len)
141 {
142         struct sptlrpc_sepol *old;
143         bool rc;
144
145         rcu_read_lock();
146         old = rcu_dereference(imp_sec->ps_sepol);
147         if (!old)
148                 rc = true;
149         else if (!kref_read(&old->ssp_ref))
150                 rc = false;
151         else if (ktime_compare(old->ssp_mtime, mtime) != 0)
152                 rc = true;
153         else
154                 rc = false;
155         rcu_read_unlock();
156
157         return rc;
158 }
159 static int sptlrpc_sepol_update(struct obd_import *imp,
160                                 ktime_t mtime, char *pol, size_t pol_len)
161 {
162         struct sptlrpc_sepol *old;
163         struct sptlrpc_sepol *new;
164         struct ptlrpc_sec *imp_sec;
165         int rc = 0;
166
167         imp_sec = sptlrpc_import_sec_ref(imp);
168         if (!imp_sec)
169                 RETURN(-ENODEV);
170
171         if (!sptlrpc_sepol_update_needed(imp_sec, mtime, pol, pol_len))
172                 GOTO(out, rc);
173
174         new = kmalloc(sizeof(typeof(*new)) + pol_len + 1, GFP_KERNEL);
175         if (!new)
176                 GOTO(out, rc = -ENOMEM);
177
178         kref_init(&new->ssp_ref);
179         new->ssp_sepol_size = pol_len + 1;
180         new->ssp_mtime = mtime;
181         strscpy(new->ssp_sepol, pol, new->ssp_sepol_size);
182
183         spin_lock(&imp_sec->ps_lock);
184         old = rcu_dereference_protected(imp_sec->ps_sepol, 1);
185         rcu_assign_pointer(imp_sec->ps_sepol, new);
186         spin_unlock(&imp_sec->ps_lock);
187         sptlrpc_sepol_put(old);
188 out:
189         sptlrpc_sec_put(imp_sec);
190
191         return rc;
192 }
193
194 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
195 static ssize_t sepol_seq_write_old(struct obd_device *obd,
196                                    const char __user *buffer, size_t count)
197 {
198         struct client_obd *cli = &obd->u.cli;
199         struct obd_import *imp = cli->cl_import;
200         struct sepol_downcall_data_old *param;
201         size_t maxlen = LUSTRE_NODEMAP_SEPOL_LENGTH + 1;
202         size_t size = sizeof(*param);
203         size_t maxparam = sizeof(*param) + maxlen;
204         int len;
205         int rc = 0;
206
207         if (count <= size) {
208                 rc = -EINVAL;
209                 CERROR("%s: invalid data count %zu <= size %zu: rc = %d\n",
210                        obd->obd_name, count, size, rc);
211                 return rc;
212         }
213
214         OBD_ALLOC(param, maxparam);
215         if (!param)
216                 return -ENOMEM;
217
218         if (copy_from_user(param, buffer, min(count, maxparam))) {
219                 rc = -EFAULT;
220                 CERROR("%s: bad sepol data: rc = %d\n", obd->obd_name, rc);
221                 GOTO(out, rc);
222         }
223
224         if (param->sdd_magic != SEPOL_DOWNCALL_MAGIC_OLD) {
225                 rc = -EINVAL;
226                 CERROR("%s: sepol downcall bad magic %#08x != %#08x: rc = %d\n",
227                        obd->obd_name, param->sdd_magic,
228                        SEPOL_DOWNCALL_MAGIC_OLD, rc);
229                 GOTO(out, rc);
230         }
231
232         len = param->sdd_sepol_len;
233         if (len == 0 || len >= maxlen) {
234                 rc = -EINVAL;
235                 CERROR("%s: bad sepol len %u >= maxlen %zu: rc = %d\n",
236                        obd->obd_name, len, maxlen, rc);
237                 GOTO(out, rc);
238         }
239         size = offsetof(typeof(*param), sdd_sepol[len]);
240
241         if (count < size) {
242                 rc = -EINVAL;
243                 CERROR("%s: bad sepol count %zu < total size %zu: rc = %d\n",
244                        obd->obd_name, count, size, rc);
245                 GOTO(out, rc);
246         }
247
248         rc = sptlrpc_sepol_update(imp, ktime_set(param->sdd_sepol_mtime, 0),
249                                   param->sdd_sepol, len);
250 out:
251         OBD_FREE(param, maxparam);
252
253         return rc ?: count;
254 }
255 #endif
256
257 static ssize_t
258 ldebugfs_sptlrpc_sepol_seq_write(struct file *file, const char __user *buffer,
259                                  size_t count, void *data)
260 {
261         struct seq_file *seq = file->private_data;
262         struct obd_device *obd = seq->private;
263         struct client_obd *cli = &obd->u.cli;
264         struct obd_import *imp = cli->cl_import;
265         struct sepol_downcall_data *param;
266         size_t maxlen = LUSTRE_NODEMAP_SEPOL_LENGTH + 1;
267         size_t size = sizeof(*param);
268         size_t maxparam = size + maxlen;
269         int len;
270         int rc = 0;
271
272         if (count <= size) {
273                 rc = -EINVAL;
274                 CERROR("%s: invalid data count %zu <= size %zu: rc = %d\n",
275                        obd->obd_name, count, size, rc);
276                 return rc;
277         }
278
279 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
280         {
281                 __u32 magic;
282
283                 if (copy_from_user(&magic, buffer, sizeof(magic))) {
284                         rc = -EFAULT;
285                         CERROR("%s: bad sepol magic data: rc = %d\n",
286                                obd->obd_name, rc);
287                         return rc;
288                 }
289
290                 if (unlikely(magic == SEPOL_DOWNCALL_MAGIC_OLD))
291                         return sepol_seq_write_old(obd, buffer, count);
292         }
293 #endif
294
295         OBD_ALLOC(param, maxparam);
296         if (!param)
297                 return -ENOMEM;
298
299         if (copy_from_user(param, buffer, min(count, maxparam))) {
300                 rc = -EFAULT;
301                 CERROR("%s: bad sepol data: rc = %d\n", obd->obd_name, rc);
302                 GOTO(out, rc);
303         }
304
305         if (param->sdd_magic != SEPOL_DOWNCALL_MAGIC) {
306                 rc = -EINVAL;
307                 CERROR("%s: sepol downcall bad magic %#08x != %#08x: rc = %d\n",
308                        obd->obd_name, param->sdd_magic,
309                        SEPOL_DOWNCALL_MAGIC, rc);
310                 GOTO(out, rc);
311         }
312
313         len = param->sdd_sepol_len;
314         if (len == 0 || len >= maxlen) {
315                 rc = -EINVAL;
316                 CERROR("%s: bad sepol len %u >= maxlen %zu: rc = %d\n",
317                        obd->obd_name, len, maxlen, rc);
318                 GOTO(out, rc);
319         }
320         size = offsetof(typeof(*param), sdd_sepol[len]);
321
322         if (count < size) {
323                 rc = -EINVAL;
324                 CERROR("%s: bad sepol count %zu < total size %zu: rc = %d\n",
325                        obd->obd_name, count, size, rc);
326                 GOTO(out, rc);
327         }
328
329         rc = sptlrpc_sepol_update(imp, ktime_set(param->sdd_sepol_mtime, 0),
330                                   param->sdd_sepol, len);
331 out:
332         OBD_FREE(param, maxparam);
333
334         return rc ?: count;
335 }
336
337 static int lprocfs_sptlrpc_sepol_seq_show(struct seq_file *seq, void *v)
338 {
339         struct obd_device *obd = seq->private;
340         struct client_obd *cli = &obd->u.cli;
341         struct obd_import *imp = cli->cl_import;
342         struct ptlrpc_sec *imp_sec;
343         struct sptlrpc_sepol *sepol;
344         struct timespec64 ts;
345         int rc = 0;
346
347         imp_sec = sptlrpc_import_sec_ref(imp);
348         if (!imp_sec)
349                 RETURN(-ENODEV);
350
351         rcu_read_lock();
352         sepol = rcu_dereference(imp->imp_sec->ps_sepol);
353         if (sepol) {
354                 ts = ktime_to_timespec64(sepol->ssp_mtime);
355                 seq_printf(seq, "mtime: %lld\n", (long long int) ts.tv_sec);
356                 seq_printf(seq, "sepol: %.*s\n",
357                            sepol->ssp_sepol_size, sepol->ssp_sepol);
358         } else {
359                 seq_puts(seq, "uninitialized\n");
360         }
361         rcu_read_unlock();
362         sptlrpc_sec_put(imp_sec);
363
364         return rc;
365 }
366 LDEBUGFS_SEQ_FOPS_RW_TYPE(srpc, sptlrpc_sepol);
367
368 int sptlrpc_lprocfs_cliobd_attach(struct obd_device *obd)
369 {
370         if (strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) != 0 &&
371             strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) != 0 &&
372             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME) != 0 &&
373             strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) != 0 &&
374             strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) != 0) {
375                 CERROR("can't register lproc for obd type %s\n",
376                        obd->obd_type->typ_name);
377                 return -EINVAL;
378         }
379
380         debugfs_create_file("srpc_info", 0444, obd->obd_debugfs_entry, obd,
381                             &sptlrpc_info_lprocfs_fops);
382
383         debugfs_create_file("srpc_contexts", 0444, obd->obd_debugfs_entry, obd,
384                             &sptlrpc_ctxs_lprocfs_fops);
385
386         debugfs_create_file("srpc_sepol", 0200, obd->obd_debugfs_entry, obd,
387                             &srpc_sptlrpc_sepol_fops);
388
389         return 0;
390 }
391 EXPORT_SYMBOL(sptlrpc_lprocfs_cliobd_attach);
392
393 LDEBUGFS_SEQ_FOPS_RO(encrypt_page_pools);
394 LDEBUGFS_SEQ_FOPS_RO(page_pools);
395
396 static struct ldebugfs_vars sptlrpc_lprocfs_vars[] = {
397         { .name =       "encrypt_page_pools",
398           .fops =       &encrypt_page_pools_fops        },
399         { .name =       "page_pools",
400           .fops =       &page_pools_fops        },
401
402         { NULL }
403 };
404
405 struct dentry *sptlrpc_debugfs_dir;
406 EXPORT_SYMBOL(sptlrpc_debugfs_dir);
407
408 struct proc_dir_entry *sptlrpc_lprocfs_dir;
409 EXPORT_SYMBOL(sptlrpc_lprocfs_dir);
410
411 int sptlrpc_lproc_init(void)
412 {
413         int rc = 0;
414
415         LASSERT(sptlrpc_debugfs_dir == NULL);
416
417         sptlrpc_debugfs_dir = debugfs_create_dir("sptlrpc",
418                                                  debugfs_lustre_root);
419         ldebugfs_add_vars(sptlrpc_debugfs_dir, sptlrpc_lprocfs_vars, NULL);
420
421         sptlrpc_lprocfs_dir = lprocfs_register("sptlrpc", proc_lustre_root,
422                                                NULL, NULL);
423         if (IS_ERR_OR_NULL(sptlrpc_lprocfs_dir)) {
424                 rc = PTR_ERR(sptlrpc_lprocfs_dir);
425                 rc = sptlrpc_lprocfs_dir ? PTR_ERR(sptlrpc_lprocfs_dir)
426                         : -ENOMEM;
427                 sptlrpc_lprocfs_dir = NULL;
428         }
429         return rc;
430 }
431
432 void sptlrpc_lproc_fini(void)
433 {
434         debugfs_remove_recursive(sptlrpc_debugfs_dir);
435         sptlrpc_debugfs_dir = NULL;
436
437         if (!IS_ERR_OR_NULL(sptlrpc_lprocfs_dir))
438                 lprocfs_remove(&sptlrpc_lprocfs_dir);
439 }