Merge tag 'gpio-v4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux...
[cascardo/linux.git] / drivers / staging / lustre / lustre / ptlrpc / sec_null.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) 2011, 2012, 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_null.c
33  *
34  * Author: Eric Mei <ericm@clusterfs.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_SEC
38
39 #include "../include/obd_support.h"
40 #include "../include/obd_cksum.h"
41 #include "../include/obd_class.h"
42 #include "../include/lustre_net.h"
43 #include "../include/lustre_sec.h"
44
45 #include "ptlrpc_internal.h"
46
47 static struct ptlrpc_sec_policy null_policy;
48 static struct ptlrpc_sec        null_sec;
49 static struct ptlrpc_cli_ctx    null_cli_ctx;
50 static struct ptlrpc_svc_ctx    null_svc_ctx;
51
52 /*
53  * we can temporarily use the topmost 8-bits of lm_secflvr to identify
54  * the source sec part.
55  */
56 static inline
57 void null_encode_sec_part(struct lustre_msg *msg, enum lustre_sec_part sp)
58 {
59         msg->lm_secflvr |= (((__u32)sp) & 0xFF) << 24;
60 }
61
62 static inline
63 enum lustre_sec_part null_decode_sec_part(struct lustre_msg *msg)
64 {
65         return (msg->lm_secflvr >> 24) & 0xFF;
66 }
67
68 static int null_ctx_refresh(struct ptlrpc_cli_ctx *ctx)
69 {
70         /* should never reach here */
71         LBUG();
72         return 0;
73 }
74
75 static
76 int null_ctx_sign(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
77 {
78         req->rq_reqbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
79
80         if (!req->rq_import->imp_dlm_fake) {
81                 struct obd_device *obd = req->rq_import->imp_obd;
82
83                 null_encode_sec_part(req->rq_reqbuf,
84                                      obd->u.cli.cl_sp_me);
85         }
86         req->rq_reqdata_len = req->rq_reqlen;
87         return 0;
88 }
89
90 static
91 int null_ctx_verify(struct ptlrpc_cli_ctx *ctx, struct ptlrpc_request *req)
92 {
93         __u32 cksums, cksumc;
94
95         LASSERT(req->rq_repdata);
96
97         req->rq_repmsg = req->rq_repdata;
98         req->rq_replen = req->rq_repdata_len;
99
100         if (req->rq_early) {
101                 cksums = lustre_msg_get_cksum(req->rq_repdata);
102                 cksumc = lustre_msg_calc_cksum(req->rq_repmsg);
103                 if (cksumc != cksums) {
104                         CDEBUG(D_SEC,
105                                "early reply checksum mismatch: %08x != %08x\n",
106                                cksumc, cksums);
107                         return -EINVAL;
108                 }
109         }
110
111         return 0;
112 }
113
114 static
115 struct ptlrpc_sec *null_create_sec(struct obd_import *imp,
116                                    struct ptlrpc_svc_ctx *svc_ctx,
117                                    struct sptlrpc_flavor *sf)
118 {
119         LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_NULL);
120
121         /* general layer has take a module reference for us, because we never
122          * really destroy the sec, simply release the reference here.
123          */
124         sptlrpc_policy_put(&null_policy);
125         return &null_sec;
126 }
127
128 static
129 void null_destroy_sec(struct ptlrpc_sec *sec)
130 {
131         LASSERT(sec == &null_sec);
132 }
133
134 static
135 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
136                                        struct vfs_cred *vcred,
137                                        int create, int remove_dead)
138 {
139         atomic_inc(&null_cli_ctx.cc_refcount);
140         return &null_cli_ctx;
141 }
142
143 static
144 int null_flush_ctx_cache(struct ptlrpc_sec *sec,
145                          uid_t uid,
146                          int grace, int force)
147 {
148         return 0;
149 }
150
151 static
152 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
153                       struct ptlrpc_request *req,
154                       int msgsize)
155 {
156         if (!req->rq_reqbuf) {
157                 int alloc_size = size_roundup_power2(msgsize);
158
159                 LASSERT(!req->rq_pool);
160                 req->rq_reqbuf = libcfs_kvzalloc(alloc_size, GFP_NOFS);
161                 if (!req->rq_reqbuf)
162                         return -ENOMEM;
163
164                 req->rq_reqbuf_len = alloc_size;
165         } else {
166                 LASSERT(req->rq_pool);
167                 LASSERT(req->rq_reqbuf_len >= msgsize);
168                 memset(req->rq_reqbuf, 0, msgsize);
169         }
170
171         req->rq_reqmsg = req->rq_reqbuf;
172         return 0;
173 }
174
175 static
176 void null_free_reqbuf(struct ptlrpc_sec *sec,
177                       struct ptlrpc_request *req)
178 {
179         if (!req->rq_pool) {
180                 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
181                          "req %p: reqmsg %p is not reqbuf %p in null sec\n",
182                          req, req->rq_reqmsg, req->rq_reqbuf);
183                 LASSERTF(req->rq_reqbuf_len >= req->rq_reqlen,
184                          "req %p: reqlen %d should smaller than buflen %d\n",
185                          req, req->rq_reqlen, req->rq_reqbuf_len);
186
187                 kvfree(req->rq_reqbuf);
188                 req->rq_reqbuf = NULL;
189                 req->rq_reqbuf_len = 0;
190         }
191 }
192
193 static
194 int null_alloc_repbuf(struct ptlrpc_sec *sec,
195                       struct ptlrpc_request *req,
196                       int msgsize)
197 {
198         /* add space for early replied */
199         msgsize += lustre_msg_early_size();
200
201         msgsize = size_roundup_power2(msgsize);
202
203         req->rq_repbuf = libcfs_kvzalloc(msgsize, GFP_NOFS);
204         if (!req->rq_repbuf)
205                 return -ENOMEM;
206
207         req->rq_repbuf_len = msgsize;
208         return 0;
209 }
210
211 static
212 void null_free_repbuf(struct ptlrpc_sec *sec,
213                       struct ptlrpc_request *req)
214 {
215         LASSERT(req->rq_repbuf);
216
217         kvfree(req->rq_repbuf);
218         req->rq_repbuf = NULL;
219         req->rq_repbuf_len = 0;
220 }
221
222 static
223 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
224                         struct ptlrpc_request *req,
225                         int segment, int newsize)
226 {
227         struct lustre_msg *newbuf;
228         struct lustre_msg *oldbuf = req->rq_reqmsg;
229         int oldsize, newmsg_size, alloc_size;
230
231         LASSERT(req->rq_reqbuf);
232         LASSERT(req->rq_reqbuf == req->rq_reqmsg);
233         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
234         LASSERT(req->rq_reqlen == lustre_packed_msg_size(oldbuf));
235
236         /* compute new message size */
237         oldsize = req->rq_reqbuf->lm_buflens[segment];
238         req->rq_reqbuf->lm_buflens[segment] = newsize;
239         newmsg_size = lustre_packed_msg_size(oldbuf);
240         req->rq_reqbuf->lm_buflens[segment] = oldsize;
241
242         /* request from pool should always have enough buffer */
243         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
244
245         if (req->rq_reqbuf_len < newmsg_size) {
246                 alloc_size = size_roundup_power2(newmsg_size);
247
248                 newbuf = libcfs_kvzalloc(alloc_size, GFP_NOFS);
249                 if (!newbuf)
250                         return -ENOMEM;
251
252                 /* Must lock this, so that otherwise unprotected change of
253                  * rq_reqmsg is not racing with parallel processing of
254                  * imp_replay_list traversing threads. See LU-3333
255                  * This is a bandaid at best, we really need to deal with this
256                  * in request enlarging code before unpacking that's already
257                  * there
258                  */
259                 if (req->rq_import)
260                         spin_lock(&req->rq_import->imp_lock);
261                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
262
263                 kvfree(req->rq_reqbuf);
264                 req->rq_reqbuf = newbuf;
265                 req->rq_reqmsg = newbuf;
266                 req->rq_reqbuf_len = alloc_size;
267
268                 if (req->rq_import)
269                         spin_unlock(&req->rq_import->imp_lock);
270         }
271
272         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
273         req->rq_reqlen = newmsg_size;
274
275         return 0;
276 }
277
278 static struct ptlrpc_svc_ctx null_svc_ctx = {
279         .sc_refcount    = ATOMIC_INIT(1),
280         .sc_policy      = &null_policy,
281 };
282
283 static
284 int null_accept(struct ptlrpc_request *req)
285 {
286         LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
287                 SPTLRPC_POLICY_NULL);
288
289         if (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL) {
290                 CERROR("Invalid rpc flavor 0x%x\n", req->rq_flvr.sf_rpc);
291                 return SECSVC_DROP;
292         }
293
294         req->rq_sp_from = null_decode_sec_part(req->rq_reqbuf);
295
296         req->rq_reqmsg = req->rq_reqbuf;
297         req->rq_reqlen = req->rq_reqdata_len;
298
299         req->rq_svc_ctx = &null_svc_ctx;
300         atomic_inc(&req->rq_svc_ctx->sc_refcount);
301
302         return SECSVC_OK;
303 }
304
305 static
306 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
307 {
308         struct ptlrpc_reply_state *rs;
309         int rs_size = sizeof(*rs) + msgsize;
310
311         LASSERT(msgsize % 8 == 0);
312
313         rs = req->rq_reply_state;
314
315         if (rs) {
316                 /* pre-allocated */
317                 LASSERT(rs->rs_size >= rs_size);
318         } else {
319                 rs = libcfs_kvzalloc(rs_size, GFP_NOFS);
320                 if (!rs)
321                         return -ENOMEM;
322
323                 rs->rs_size = rs_size;
324         }
325
326         rs->rs_svc_ctx = req->rq_svc_ctx;
327         atomic_inc(&req->rq_svc_ctx->sc_refcount);
328
329         rs->rs_repbuf = (struct lustre_msg *)(rs + 1);
330         rs->rs_repbuf_len = rs_size - sizeof(*rs);
331         rs->rs_msg = rs->rs_repbuf;
332
333         req->rq_reply_state = rs;
334         return 0;
335 }
336
337 static
338 void null_free_rs(struct ptlrpc_reply_state *rs)
339 {
340         LASSERT_ATOMIC_GT(&rs->rs_svc_ctx->sc_refcount, 1);
341         atomic_dec(&rs->rs_svc_ctx->sc_refcount);
342
343         if (!rs->rs_prealloc)
344                 kvfree(rs);
345 }
346
347 static
348 int null_authorize(struct ptlrpc_request *req)
349 {
350         struct ptlrpc_reply_state *rs = req->rq_reply_state;
351
352         LASSERT(rs);
353
354         rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
355         rs->rs_repdata_len = req->rq_replen;
356
357         if (likely(req->rq_packed_final)) {
358                 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
359                         req->rq_reply_off = lustre_msg_early_size();
360                 else
361                         req->rq_reply_off = 0;
362         } else {
363                 __u32 cksum;
364
365                 cksum = lustre_msg_calc_cksum(rs->rs_repbuf);
366                 lustre_msg_set_cksum(rs->rs_repbuf, cksum);
367                 req->rq_reply_off = 0;
368         }
369
370         return 0;
371 }
372
373 static struct ptlrpc_ctx_ops null_ctx_ops = {
374         .refresh                = null_ctx_refresh,
375         .sign              = null_ctx_sign,
376         .verify          = null_ctx_verify,
377 };
378
379 static struct ptlrpc_sec_cops null_sec_cops = {
380         .create_sec          = null_create_sec,
381         .destroy_sec        = null_destroy_sec,
382         .lookup_ctx          = null_lookup_ctx,
383         .flush_ctx_cache        = null_flush_ctx_cache,
384         .alloc_reqbuf      = null_alloc_reqbuf,
385         .alloc_repbuf      = null_alloc_repbuf,
386         .free_reqbuf        = null_free_reqbuf,
387         .free_repbuf        = null_free_repbuf,
388         .enlarge_reqbuf  = null_enlarge_reqbuf,
389 };
390
391 static struct ptlrpc_sec_sops null_sec_sops = {
392         .accept          = null_accept,
393         .alloc_rs              = null_alloc_rs,
394         .authorize            = null_authorize,
395         .free_rs                = null_free_rs,
396 };
397
398 static struct ptlrpc_sec_policy null_policy = {
399         .sp_owner              = THIS_MODULE,
400         .sp_name                = "sec.null",
401         .sp_policy            = SPTLRPC_POLICY_NULL,
402         .sp_cops                = &null_sec_cops,
403         .sp_sops                = &null_sec_sops,
404 };
405
406 static void null_init_internal(void)
407 {
408         static HLIST_HEAD(__list);
409
410         null_sec.ps_policy = &null_policy;
411         atomic_set(&null_sec.ps_refcount, 1);     /* always busy */
412         null_sec.ps_id = -1;
413         null_sec.ps_import = NULL;
414         null_sec.ps_flvr.sf_rpc = SPTLRPC_FLVR_NULL;
415         null_sec.ps_flvr.sf_flags = 0;
416         null_sec.ps_part = LUSTRE_SP_ANY;
417         null_sec.ps_dying = 0;
418         spin_lock_init(&null_sec.ps_lock);
419         atomic_set(&null_sec.ps_nctx, 1);        /* for "null_cli_ctx" */
420         INIT_LIST_HEAD(&null_sec.ps_gc_list);
421         null_sec.ps_gc_interval = 0;
422         null_sec.ps_gc_next = 0;
423
424         hlist_add_head(&null_cli_ctx.cc_cache, &__list);
425         atomic_set(&null_cli_ctx.cc_refcount, 1);    /* for hash */
426         null_cli_ctx.cc_sec = &null_sec;
427         null_cli_ctx.cc_ops = &null_ctx_ops;
428         null_cli_ctx.cc_expire = 0;
429         null_cli_ctx.cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_ETERNAL |
430                                 PTLRPC_CTX_UPTODATE;
431         null_cli_ctx.cc_vcred.vc_uid = 0;
432         spin_lock_init(&null_cli_ctx.cc_lock);
433         INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
434         INIT_LIST_HEAD(&null_cli_ctx.cc_gc_chain);
435 }
436
437 int sptlrpc_null_init(void)
438 {
439         int rc;
440
441         null_init_internal();
442
443         rc = sptlrpc_register_policy(&null_policy);
444         if (rc)
445                 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
446
447         return rc;
448 }
449
450 void sptlrpc_null_fini(void)
451 {
452         int rc;
453
454         rc = sptlrpc_unregister_policy(&null_policy);
455         if (rc)
456                 CERROR("failed to unregister %s: %d\n",
457                        null_policy.sp_name, rc);
458 }