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