Whamcloud - gitweb
b=23728 Doxygen style comment - sptlrpc api.
[fs/lustre-release.git] / lustre / ptlrpc / sec_null.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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                         CWARN("early reply checksum mismatch: %08x != %08x\n",
122                               cksumc, cksums);
123                         return -EINVAL;
124                 }
125         }
126
127         return 0;
128 }
129
130 static
131 struct ptlrpc_sec *null_create_sec(struct obd_import *imp,
132                                    struct ptlrpc_svc_ctx *svc_ctx,
133                                    struct sptlrpc_flavor *sf)
134 {
135         LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_NULL);
136
137         /* general layer has take a module reference for us, because we never
138          * really destroy the sec, simply release the reference here.
139          */
140         sptlrpc_policy_put(&null_policy);
141         return &null_sec;
142 }
143
144 static
145 void null_destroy_sec(struct ptlrpc_sec *sec)
146 {
147         LASSERT(sec == &null_sec);
148 }
149
150 static
151 struct ptlrpc_cli_ctx *null_lookup_ctx(struct ptlrpc_sec *sec,
152                                        struct vfs_cred *vcred,
153                                        int create, int remove_dead)
154 {
155         cfs_atomic_inc(&null_cli_ctx.cc_refcount);
156         return &null_cli_ctx;
157 }
158
159 static
160 int null_flush_ctx_cache(struct ptlrpc_sec *sec,
161                          uid_t uid,
162                          int grace, int force)
163 {
164         return 0;
165 }
166
167 static
168 int null_alloc_reqbuf(struct ptlrpc_sec *sec,
169                       struct ptlrpc_request *req,
170                       int msgsize)
171 {
172         if (!req->rq_reqbuf) {
173                 int alloc_size = size_roundup_power2(msgsize);
174
175                 LASSERT(!req->rq_pool);
176                 OBD_ALLOC(req->rq_reqbuf, alloc_size);
177                 if (!req->rq_reqbuf)
178                         return -ENOMEM;
179
180                 req->rq_reqbuf_len = alloc_size;
181         } else {
182                 LASSERT(req->rq_pool);
183                 LASSERT(req->rq_reqbuf_len >= msgsize);
184                 memset(req->rq_reqbuf, 0, msgsize);
185         }
186
187         req->rq_reqmsg = req->rq_reqbuf;
188         return 0;
189 }
190
191 static
192 void null_free_reqbuf(struct ptlrpc_sec *sec,
193                       struct ptlrpc_request *req)
194 {
195         if (!req->rq_pool) {
196                 LASSERTF(req->rq_reqmsg == req->rq_reqbuf,
197                          "req %p: reqmsg %p is not reqbuf %p in null sec\n",
198                          req, req->rq_reqmsg, req->rq_reqbuf);
199                 LASSERTF(req->rq_reqbuf_len >= req->rq_reqlen,
200                          "req %p: reqlen %d should smaller than buflen %d\n",
201                          req, req->rq_reqlen, req->rq_reqbuf_len);
202
203                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
204                 req->rq_reqbuf = NULL;
205                 req->rq_reqbuf_len = 0;
206         }
207 }
208
209 static
210 int null_alloc_repbuf(struct ptlrpc_sec *sec,
211                       struct ptlrpc_request *req,
212                       int msgsize)
213 {
214         /* add space for early replied */
215         msgsize += lustre_msg_early_size();
216
217         msgsize = size_roundup_power2(msgsize);
218
219         OBD_ALLOC(req->rq_repbuf, msgsize);
220         if (!req->rq_repbuf)
221                 return -ENOMEM;
222
223         req->rq_repbuf_len = msgsize;
224         return 0;
225 }
226
227 static
228 void null_free_repbuf(struct ptlrpc_sec *sec,
229                       struct ptlrpc_request *req)
230 {
231         LASSERT(req->rq_repbuf);
232
233         OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
234         req->rq_repbuf = NULL;
235         req->rq_repbuf_len = 0;
236 }
237
238 static
239 int null_enlarge_reqbuf(struct ptlrpc_sec *sec,
240                         struct ptlrpc_request *req,
241                         int segment, int newsize)
242 {
243         struct lustre_msg      *newbuf;
244         struct lustre_msg      *oldbuf = req->rq_reqmsg;
245         int                     oldsize, newmsg_size, alloc_size;
246
247         LASSERT(req->rq_reqbuf);
248         LASSERT(req->rq_reqbuf == req->rq_reqmsg);
249         LASSERT(req->rq_reqbuf_len >= req->rq_reqlen);
250         LASSERT(req->rq_reqlen == lustre_packed_msg_size(oldbuf));
251
252         /* compute new message size */
253         oldsize = req->rq_reqbuf->lm_buflens[segment];
254         req->rq_reqbuf->lm_buflens[segment] = newsize;
255         newmsg_size = lustre_packed_msg_size(oldbuf);
256         req->rq_reqbuf->lm_buflens[segment] = oldsize;
257
258         /* request from pool should always have enough buffer */
259         LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newmsg_size);
260
261         if (req->rq_reqbuf_len < newmsg_size) {
262                 alloc_size = size_roundup_power2(newmsg_size);
263
264                 OBD_ALLOC(newbuf, alloc_size);
265                 if (newbuf == NULL)
266                         return -ENOMEM;
267
268                 memcpy(newbuf, req->rq_reqbuf, req->rq_reqlen);
269
270                 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
271                 req->rq_reqbuf = req->rq_reqmsg = newbuf;
272                 req->rq_reqbuf_len = alloc_size;
273         }
274
275         _sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
276         req->rq_reqlen = newmsg_size;
277
278         return 0;
279 }
280
281 static struct ptlrpc_svc_ctx null_svc_ctx = {
282         .sc_refcount    = CFS_ATOMIC_INIT(1),
283         .sc_policy      = &null_policy,
284 };
285
286 static
287 int null_accept(struct ptlrpc_request *req)
288 {
289         LASSERT(SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) ==
290                 SPTLRPC_POLICY_NULL);
291
292         if (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL) {
293                 CERROR("Invalid rpc flavor 0x%x\n", req->rq_flvr.sf_rpc);
294                 return SECSVC_DROP;
295         }
296
297         req->rq_sp_from = null_decode_sec_part(req->rq_reqbuf);
298
299         req->rq_reqmsg = req->rq_reqbuf;
300         req->rq_reqlen = req->rq_reqdata_len;
301
302         req->rq_svc_ctx = &null_svc_ctx;
303         cfs_atomic_inc(&req->rq_svc_ctx->sc_refcount);
304
305         return SECSVC_OK;
306 }
307
308 static
309 int null_alloc_rs(struct ptlrpc_request *req, int msgsize)
310 {
311         struct ptlrpc_reply_state *rs;
312         int rs_size = sizeof(*rs) + msgsize;
313
314         LASSERT(msgsize % 8 == 0);
315
316         rs = req->rq_reply_state;
317
318         if (rs) {
319                 /* pre-allocated */
320                 LASSERT(rs->rs_size >= rs_size);
321         } else {
322                 OBD_ALLOC(rs, rs_size);
323                 if (rs == NULL)
324                         return -ENOMEM;
325
326                 rs->rs_size = rs_size;
327         }
328
329         rs->rs_svc_ctx = req->rq_svc_ctx;
330         cfs_atomic_inc(&req->rq_svc_ctx->sc_refcount);
331
332         rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
333         rs->rs_repbuf_len = rs_size - sizeof(*rs);
334         rs->rs_msg = rs->rs_repbuf;
335
336         req->rq_reply_state = rs;
337         return 0;
338 }
339
340 static
341 void null_free_rs(struct ptlrpc_reply_state *rs)
342 {
343         LASSERT(cfs_atomic_read(&rs->rs_svc_ctx->sc_refcount) > 1);
344         cfs_atomic_dec(&rs->rs_svc_ctx->sc_refcount);
345
346         if (!rs->rs_prealloc)
347                 OBD_FREE(rs, rs->rs_size);
348 }
349
350 static
351 int null_authorize(struct ptlrpc_request *req)
352 {
353         struct ptlrpc_reply_state *rs = req->rq_reply_state;
354
355         LASSERT(rs);
356
357         rs->rs_repbuf->lm_secflvr = SPTLRPC_FLVR_NULL;
358         rs->rs_repdata_len = req->rq_replen;
359
360         if (likely(req->rq_packed_final)) {
361                 if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
362                         req->rq_reply_off = lustre_msg_early_size();
363                 else
364                         req->rq_reply_off = 0;
365         } else {
366                 __u32 cksum;
367
368 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 9, 0, 0)
369                 if (lustre_msghdr_get_flags(req->rq_reqmsg) &
370                     MSGHDR_CKSUM_INCOMPAT18)
371                         cksum = lustre_msg_calc_cksum(rs->rs_repbuf, 0);
372                 else
373                         cksum = lustre_msg_calc_cksum(rs->rs_repbuf, 1);
374 #else
375 # warning "remove checksum compatibility support for b1_8"
376                 cksum = lustre_msg_calc_cksum(rs->rs_repbuf);
377 #endif
378                 lustre_msg_set_cksum(rs->rs_repbuf, cksum);
379                 req->rq_reply_off = 0;
380         }
381
382         return 0;
383 }
384
385 static struct ptlrpc_ctx_ops null_ctx_ops = {
386         .refresh                = null_ctx_refresh,
387         .sign                   = null_ctx_sign,
388         .verify                 = null_ctx_verify,
389 };
390
391 static struct ptlrpc_sec_cops null_sec_cops = {
392         .create_sec             = null_create_sec,
393         .destroy_sec            = null_destroy_sec,
394         .lookup_ctx             = null_lookup_ctx,
395         .flush_ctx_cache        = null_flush_ctx_cache,
396         .alloc_reqbuf           = null_alloc_reqbuf,
397         .alloc_repbuf           = null_alloc_repbuf,
398         .free_reqbuf            = null_free_reqbuf,
399         .free_repbuf            = null_free_repbuf,
400         .enlarge_reqbuf         = null_enlarge_reqbuf,
401 };
402
403 static struct ptlrpc_sec_sops null_sec_sops = {
404         .accept                 = null_accept,
405         .alloc_rs               = null_alloc_rs,
406         .authorize              = null_authorize,
407         .free_rs                = null_free_rs,
408 };
409
410 static struct ptlrpc_sec_policy null_policy = {
411         .sp_owner               = THIS_MODULE,
412         .sp_name                = "sec.null",
413         .sp_policy              = SPTLRPC_POLICY_NULL,
414         .sp_cops                = &null_sec_cops,
415         .sp_sops                = &null_sec_sops,
416 };
417
418 static void null_init_internal(void)
419 {
420         static CFS_HLIST_HEAD(__list);
421
422         null_sec.ps_policy = &null_policy;
423         cfs_atomic_set(&null_sec.ps_refcount, 1);     /* always busy */
424         null_sec.ps_id = -1;
425         null_sec.ps_import = NULL;
426         null_sec.ps_flvr.sf_rpc = SPTLRPC_FLVR_NULL;
427         null_sec.ps_flvr.sf_flags = 0;
428         null_sec.ps_part = LUSTRE_SP_ANY;
429         null_sec.ps_dying = 0;
430         cfs_spin_lock_init(&null_sec.ps_lock);
431         cfs_atomic_set(&null_sec.ps_nctx, 1);         /* for "null_cli_ctx" */
432         CFS_INIT_LIST_HEAD(&null_sec.ps_gc_list);
433         null_sec.ps_gc_interval = 0;
434         null_sec.ps_gc_next = 0;
435
436         cfs_hlist_add_head(&null_cli_ctx.cc_cache, &__list);
437         cfs_atomic_set(&null_cli_ctx.cc_refcount, 1);    /* for hash */
438         null_cli_ctx.cc_sec = &null_sec;
439         null_cli_ctx.cc_ops = &null_ctx_ops;
440         null_cli_ctx.cc_expire = 0;
441         null_cli_ctx.cc_flags = PTLRPC_CTX_CACHED | PTLRPC_CTX_ETERNAL |
442                                 PTLRPC_CTX_UPTODATE;
443         null_cli_ctx.cc_vcred.vc_uid = 0;
444         cfs_spin_lock_init(&null_cli_ctx.cc_lock);
445         CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_req_list);
446         CFS_INIT_LIST_HEAD(&null_cli_ctx.cc_gc_chain);
447 }
448
449 int sptlrpc_null_init(void)
450 {
451         int rc;
452
453         null_init_internal();
454
455         rc = sptlrpc_register_policy(&null_policy);
456         if (rc)
457                 CERROR("failed to register %s: %d\n", null_policy.sp_name, rc);
458
459         return rc;
460 }
461
462 void sptlrpc_null_fini(void)
463 {
464         int rc;
465
466         rc = sptlrpc_unregister_policy(&null_policy);
467         if (rc)
468                 CERROR("failed to unregister %s: %d\n", null_policy.sp_name,rc);
469 }