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