Whamcloud - gitweb
LU-7334 lprocfs: Refactored string to value helpers
[fs/lustre-release.git] / lustre / ptlrpc / gss / lproc_gss.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_SEC
38 #include <linux/init.h>
39 #include <linux/module.h>
40 #include <linux/slab.h>
41 #include <linux/dcache.h>
42 #include <linux/fs.h>
43 #include <linux/mutex.h>
44
45 #include <obd.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre/lustre_idl.h>
49 #include <lustre_net.h>
50 #include <lustre_import.h>
51 #include <lprocfs_status.h>
52 #include <lustre_sec.h>
53
54 #include "gss_err.h"
55 #include "gss_internal.h"
56 #include "gss_api.h"
57
58 static struct proc_dir_entry *gss_proc_root = NULL;
59 static struct proc_dir_entry *gss_proc_lk = NULL;
60
61 /*
62  * statistic of "out-of-sequence-window"
63  */
64 static struct {
65         spinlock_t      oos_lock;
66         atomic_t        oos_cli_count;          /* client occurrence */
67         int             oos_cli_behind;         /* client max seqs behind */
68         atomic_t        oos_svc_replay[3];      /* server replay detected */
69         atomic_t        oos_svc_pass[3];        /* server verified ok */
70 } gss_stat_oos = {
71         .oos_cli_count  = ATOMIC_INIT(0),
72         .oos_cli_behind = 0,
73         .oos_svc_replay = { ATOMIC_INIT(0), },
74         .oos_svc_pass   = { ATOMIC_INIT(0), },
75 };
76
77 void gss_stat_oos_record_cli(int behind)
78 {
79         atomic_inc(&gss_stat_oos.oos_cli_count);
80
81         spin_lock(&gss_stat_oos.oos_lock);
82         if (behind > gss_stat_oos.oos_cli_behind)
83                 gss_stat_oos.oos_cli_behind = behind;
84         spin_unlock(&gss_stat_oos.oos_lock);
85 }
86
87 void gss_stat_oos_record_svc(int phase, int replay)
88 {
89         LASSERT(phase >= 0 && phase <= 2);
90
91         if (replay)
92                 atomic_inc(&gss_stat_oos.oos_svc_replay[phase]);
93         else
94                 atomic_inc(&gss_stat_oos.oos_svc_pass[phase]);
95 }
96
97 static int gss_proc_oos_seq_show(struct seq_file *m, void *v)
98 {
99         seq_printf(m, "seqwin:             %u\n"
100                    "backwin:            %u\n"
101                    "client fall behind seqwin\n"
102                    "  occurrence:       %d\n"
103                    "  max seq behind:   %d\n"
104                    "server replay detected:\n"
105                    "  phase 0:          %d\n"
106                    "  phase 1:          %d\n"
107                    "  phase 2:          %d\n"
108                    "server verify ok:\n"
109                    "  phase 2:          %d\n",
110                    GSS_SEQ_WIN_MAIN,
111                    GSS_SEQ_WIN_BACK,
112                    atomic_read(&gss_stat_oos.oos_cli_count),
113                    gss_stat_oos.oos_cli_behind,
114                    atomic_read(&gss_stat_oos.oos_svc_replay[0]),
115                    atomic_read(&gss_stat_oos.oos_svc_replay[1]),
116                    atomic_read(&gss_stat_oos.oos_svc_replay[2]),
117                    atomic_read(&gss_stat_oos.oos_svc_pass[2]));
118         return 0;
119 }
120 LPROC_SEQ_FOPS_RO(gss_proc_oos);
121
122 static ssize_t
123 gss_proc_write_secinit(struct file *file, const char *buffer,
124                                   size_t count, loff_t *off)
125 {
126         int rc;
127
128         rc = gss_do_ctx_init_rpc((char *) buffer, count);
129         if (rc) {
130                 LASSERT(rc < 0);
131                 return rc;
132         }
133         return count;
134 }
135
136 static const struct file_operations gss_proc_secinit = {
137         .write = gss_proc_write_secinit,
138 };
139
140 static struct lprocfs_vars gss_lprocfs_vars[] = {
141         { .name =       "replays",
142           .fops =       &gss_proc_oos_fops      },
143         { .name =       "init_channel",
144           .fops =       &gss_proc_secinit,
145           .proc_mode =  0222                    },
146         { NULL }
147 };
148
149 /*
150  * for userspace helper lgss_keyring.
151  *
152  * debug_level: [0, 4], defined in utils/gss/lgss_utils.h
153  */
154 static int gss_lk_debug_level = 1;
155
156 static int gss_lk_proc_dl_seq_show(struct seq_file *m, void *v)
157 {
158         seq_printf(m, "%u\n", gss_lk_debug_level);
159         return 0;
160 }
161
162 static ssize_t
163 gss_lk_proc_dl_seq_write(struct file *file, const char __user *buffer,
164                                 size_t count, loff_t *off)
165 {
166         int rc;
167         __s64 val;
168
169         rc = lprocfs_str_to_s64(buffer, count, &val);
170         if (rc < 0)
171                 return rc;
172
173         if (val < 0 || val > 4)
174                 return -ERANGE;
175
176         gss_lk_debug_level = val;
177
178         return count;
179 }
180 LPROC_SEQ_FOPS(gss_lk_proc_dl);
181
182 static struct lprocfs_vars gss_lk_lprocfs_vars[] = {
183         { .name =       "debug_level",
184           .fops =       &gss_lk_proc_dl_fops    },
185         { NULL }
186 };
187
188 void gss_exit_lproc(void)
189 {
190         if (gss_proc_lk) {
191                 lprocfs_remove(&gss_proc_lk);
192                 gss_proc_lk = NULL;
193         }
194
195         if (gss_proc_root) {
196                 lprocfs_remove(&gss_proc_root);
197                 gss_proc_root = NULL;
198         }
199 }
200
201 int gss_init_lproc(void)
202 {
203         int     rc;
204
205         spin_lock_init(&gss_stat_oos.oos_lock);
206
207         gss_proc_root = lprocfs_register("gss", sptlrpc_proc_root,
208                                          gss_lprocfs_vars, NULL);
209         if (IS_ERR(gss_proc_root)) {
210                 rc = PTR_ERR(gss_proc_root);
211                 gss_proc_root = NULL;
212                 GOTO(out, rc);
213         }
214
215         gss_proc_lk = lprocfs_register("lgss_keyring", gss_proc_root,
216                                        gss_lk_lprocfs_vars, NULL);
217         if (IS_ERR(gss_proc_lk)) {
218                 rc = PTR_ERR(gss_proc_lk);
219                 gss_proc_lk = NULL;
220                 GOTO(out, rc);
221         }
222
223         return 0;
224
225 out:
226         CERROR("failed to initialize gss lproc entries: %d\n", rc);
227         gss_exit_lproc();
228
229         return rc;
230 }