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