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