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