Whamcloud - gitweb
LU-1084 ptlrpc: Change CWARNs to CDEBUGs
[fs/lustre-release.git] / lustre / ptlrpc / sec.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.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 #include <libcfs/libcfs.h>
47 #ifndef __KERNEL__
48 #include <liblustre.h>
49 #include <libcfs/list.h>
50 #else
51 #include <linux/crypto.h>
52 #include <linux/key.h>
53 #endif
54
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <obd_support.h>
58 #include <lustre_net.h>
59 #include <lustre_import.h>
60 #include <lustre_dlm.h>
61 #include <lustre_sec.h>
62
63 #include "ptlrpc_internal.h"
64
65 /***********************************************
66  * policy registers                            *
67  ***********************************************/
68
69 static cfs_rwlock_t policy_lock;
70 static struct ptlrpc_sec_policy *policies[SPTLRPC_POLICY_MAX] = {
71         NULL,
72 };
73
74 int sptlrpc_register_policy(struct ptlrpc_sec_policy *policy)
75 {
76         __u16 number = policy->sp_policy;
77
78         LASSERT(policy->sp_name);
79         LASSERT(policy->sp_cops);
80         LASSERT(policy->sp_sops);
81
82         if (number >= SPTLRPC_POLICY_MAX)
83                 return -EINVAL;
84
85         cfs_write_lock(&policy_lock);
86         if (unlikely(policies[number])) {
87                 cfs_write_unlock(&policy_lock);
88                 return -EALREADY;
89         }
90         policies[number] = policy;
91         cfs_write_unlock(&policy_lock);
92
93         CDEBUG(D_SEC, "%s: registered\n", policy->sp_name);
94         return 0;
95 }
96 EXPORT_SYMBOL(sptlrpc_register_policy);
97
98 int sptlrpc_unregister_policy(struct ptlrpc_sec_policy *policy)
99 {
100         __u16 number = policy->sp_policy;
101
102         LASSERT(number < SPTLRPC_POLICY_MAX);
103
104         cfs_write_lock(&policy_lock);
105         if (unlikely(policies[number] == NULL)) {
106                 cfs_write_unlock(&policy_lock);
107                 CERROR("%s: already unregistered\n", policy->sp_name);
108                 return -EINVAL;
109         }
110
111         LASSERT(policies[number] == policy);
112         policies[number] = NULL;
113         cfs_write_unlock(&policy_lock);
114
115         CDEBUG(D_SEC, "%s: unregistered\n", policy->sp_name);
116         return 0;
117 }
118 EXPORT_SYMBOL(sptlrpc_unregister_policy);
119
120 static
121 struct ptlrpc_sec_policy * sptlrpc_wireflavor2policy(__u32 flavor)
122 {
123         static CFS_DECLARE_MUTEX(load_mutex);
124         static cfs_atomic_t       loaded = CFS_ATOMIC_INIT(0);
125         struct ptlrpc_sec_policy *policy;
126         __u16                     number = SPTLRPC_FLVR_POLICY(flavor);
127         __u16                     flag = 0;
128
129         if (number >= SPTLRPC_POLICY_MAX)
130                 return NULL;
131
132         while (1) {
133                 cfs_read_lock(&policy_lock);
134                 policy = policies[number];
135                 if (policy && !cfs_try_module_get(policy->sp_owner))
136                         policy = NULL;
137                 if (policy == NULL)
138                         flag = cfs_atomic_read(&loaded);
139                 cfs_read_unlock(&policy_lock);
140
141                 if (policy != NULL || flag != 0 ||
142                     number != SPTLRPC_POLICY_GSS)
143                         break;
144
145                 /* try to load gss module, once */
146                 cfs_mutex_down(&load_mutex);
147                 if (cfs_atomic_read(&loaded) == 0) {
148                         if (cfs_request_module("ptlrpc_gss") == 0)
149                                 CDEBUG(D_SEC,
150                                        "module ptlrpc_gss loaded on demand\n");
151                         else
152                                 CERROR("Unable to load module ptlrpc_gss\n");
153
154                         cfs_atomic_set(&loaded, 1);
155                 }
156                 cfs_mutex_up(&load_mutex);
157         }
158
159         return policy;
160 }
161
162 __u32 sptlrpc_name2flavor_base(const char *name)
163 {
164         if (!strcmp(name, "null"))
165                 return SPTLRPC_FLVR_NULL;
166         if (!strcmp(name, "plain"))
167                 return SPTLRPC_FLVR_PLAIN;
168         if (!strcmp(name, "krb5n"))
169                 return SPTLRPC_FLVR_KRB5N;
170         if (!strcmp(name, "krb5a"))
171                 return SPTLRPC_FLVR_KRB5A;
172         if (!strcmp(name, "krb5i"))
173                 return SPTLRPC_FLVR_KRB5I;
174         if (!strcmp(name, "krb5p"))
175                 return SPTLRPC_FLVR_KRB5P;
176
177         return SPTLRPC_FLVR_INVALID;
178 }
179 EXPORT_SYMBOL(sptlrpc_name2flavor_base);
180
181 const char *sptlrpc_flavor2name_base(__u32 flvr)
182 {
183         __u32   base = SPTLRPC_FLVR_BASE(flvr);
184
185         if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL))
186                 return "null";
187         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_PLAIN))
188                 return "plain";
189         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5N))
190                 return "krb5n";
191         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5A))
192                 return "krb5a";
193         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5I))
194                 return "krb5i";
195         else if (base == SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_KRB5P))
196                 return "krb5p";
197
198         CERROR("invalid wire flavor 0x%x\n", flvr);
199         return "invalid";
200 }
201 EXPORT_SYMBOL(sptlrpc_flavor2name_base);
202
203 char *sptlrpc_flavor2name_bulk(struct sptlrpc_flavor *sf,
204                                char *buf, int bufsize)
205 {
206         if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN)
207                 snprintf(buf, bufsize, "hash:%s",
208                          sptlrpc_get_hash_name(sf->u_bulk.hash.hash_alg));
209         else
210                 snprintf(buf, bufsize, "%s",
211                          sptlrpc_flavor2name_base(sf->sf_rpc));
212
213         buf[bufsize - 1] = '\0';
214         return buf;
215 }
216 EXPORT_SYMBOL(sptlrpc_flavor2name_bulk);
217
218 char *sptlrpc_flavor2name(struct sptlrpc_flavor *sf, char *buf, int bufsize)
219 {
220         snprintf(buf, bufsize, "%s", sptlrpc_flavor2name_base(sf->sf_rpc));
221
222         /*
223          * currently we don't support customized bulk specification for
224          * flavors other than plain
225          */
226         if (SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_PLAIN) {
227                 char bspec[16];
228
229                 bspec[0] = '-';
230                 sptlrpc_flavor2name_bulk(sf, &bspec[1], sizeof(bspec) - 1);
231                 strncat(buf, bspec, bufsize);
232         }
233
234         buf[bufsize - 1] = '\0';
235         return buf;
236 }
237 EXPORT_SYMBOL(sptlrpc_flavor2name);
238
239 char *sptlrpc_secflags2str(__u32 flags, char *buf, int bufsize)
240 {
241         buf[0] = '\0';
242
243         if (flags & PTLRPC_SEC_FL_REVERSE)
244                 strncat(buf, "reverse,", bufsize);
245         if (flags & PTLRPC_SEC_FL_ROOTONLY)
246                 strncat(buf, "rootonly,", bufsize);
247         if (flags & PTLRPC_SEC_FL_UDESC)
248                 strncat(buf, "udesc,", bufsize);
249         if (flags & PTLRPC_SEC_FL_BULK)
250                 strncat(buf, "bulk,", bufsize);
251         if (buf[0] == '\0')
252                 strncat(buf, "-,", bufsize);
253
254         buf[bufsize - 1] = '\0';
255         return buf;
256 }
257 EXPORT_SYMBOL(sptlrpc_secflags2str);
258
259 /**************************************************
260  * client context APIs                            *
261  **************************************************/
262
263 static
264 struct ptlrpc_cli_ctx *get_my_ctx(struct ptlrpc_sec *sec)
265 {
266         struct vfs_cred vcred;
267         int create = 1, remove_dead = 1;
268
269         LASSERT(sec);
270         LASSERT(sec->ps_policy->sp_cops->lookup_ctx);
271
272         if (sec->ps_flvr.sf_flags & (PTLRPC_SEC_FL_REVERSE |
273                                      PTLRPC_SEC_FL_ROOTONLY)) {
274                 vcred.vc_uid = 0;
275                 vcred.vc_gid = 0;
276                 if (sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_REVERSE) {
277                         create = 0;
278                         remove_dead = 0;
279                 }
280         } else {
281                 vcred.vc_uid = cfs_curproc_uid();
282                 vcred.vc_gid = cfs_curproc_gid();
283         }
284
285         return sec->ps_policy->sp_cops->lookup_ctx(sec, &vcred,
286                                                    create, remove_dead);
287 }
288
289 struct ptlrpc_cli_ctx *sptlrpc_cli_ctx_get(struct ptlrpc_cli_ctx *ctx)
290 {
291         cfs_atomic_inc(&ctx->cc_refcount);
292         return ctx;
293 }
294 EXPORT_SYMBOL(sptlrpc_cli_ctx_get);
295
296 void sptlrpc_cli_ctx_put(struct ptlrpc_cli_ctx *ctx, int sync)
297 {
298         struct ptlrpc_sec *sec = ctx->cc_sec;
299
300         LASSERT(sec);
301         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
302
303         if (!cfs_atomic_dec_and_test(&ctx->cc_refcount))
304                 return;
305
306         sec->ps_policy->sp_cops->release_ctx(sec, ctx, sync);
307 }
308 EXPORT_SYMBOL(sptlrpc_cli_ctx_put);
309
310 /**
311  * Expire the client context immediately.
312  *
313  * \pre Caller must hold at least 1 reference on the \a ctx.
314  */
315 void sptlrpc_cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
316 {
317         LASSERT(ctx->cc_ops->die);
318         ctx->cc_ops->die(ctx, 0);
319 }
320 EXPORT_SYMBOL(sptlrpc_cli_ctx_expire);
321
322 /**
323  * To wake up the threads who are waiting for this client context. Called
324  * after some status change happened on \a ctx.
325  */
326 void sptlrpc_cli_ctx_wakeup(struct ptlrpc_cli_ctx *ctx)
327 {
328         struct ptlrpc_request *req, *next;
329
330         cfs_spin_lock(&ctx->cc_lock);
331         cfs_list_for_each_entry_safe(req, next, &ctx->cc_req_list,
332                                      rq_ctx_chain) {
333                 cfs_list_del_init(&req->rq_ctx_chain);
334                 ptlrpc_client_wake_req(req);
335         }
336         cfs_spin_unlock(&ctx->cc_lock);
337 }
338 EXPORT_SYMBOL(sptlrpc_cli_ctx_wakeup);
339
340 int sptlrpc_cli_ctx_display(struct ptlrpc_cli_ctx *ctx, char *buf, int bufsize)
341 {
342         LASSERT(ctx->cc_ops);
343
344         if (ctx->cc_ops->display == NULL)
345                 return 0;
346
347         return ctx->cc_ops->display(ctx, buf, bufsize);
348 }
349
350 static int import_sec_check_expire(struct obd_import *imp)
351 {
352         int     adapt = 0;
353
354         cfs_spin_lock(&imp->imp_lock);
355         if (imp->imp_sec_expire &&
356             imp->imp_sec_expire < cfs_time_current_sec()) {
357                 adapt = 1;
358                 imp->imp_sec_expire = 0;
359         }
360         cfs_spin_unlock(&imp->imp_lock);
361
362         if (!adapt)
363                 return 0;
364
365         CDEBUG(D_SEC, "found delayed sec adapt expired, do it now\n");
366         return sptlrpc_import_sec_adapt(imp, NULL, 0);
367 }
368
369 static int import_sec_validate_get(struct obd_import *imp,
370                                    struct ptlrpc_sec **sec)
371 {
372         int     rc;
373
374         if (unlikely(imp->imp_sec_expire)) {
375                 rc = import_sec_check_expire(imp);
376                 if (rc)
377                         return rc;
378         }
379
380         *sec = sptlrpc_import_sec_ref(imp);
381         if (*sec == NULL) {
382                 CERROR("import %p (%s) with no sec\n",
383                        imp, ptlrpc_import_state_name(imp->imp_state));
384                 return -EACCES;
385         }
386
387         if (unlikely((*sec)->ps_dying)) {
388                 CERROR("attempt to use dying sec %p\n", sec);
389                 sptlrpc_sec_put(*sec);
390                 return -EACCES;
391         }
392
393         return 0;
394 }
395
396 /**
397  * Given a \a req, find or allocate a appropriate context for it.
398  * \pre req->rq_cli_ctx == NULL.
399  *
400  * \retval 0 succeed, and req->rq_cli_ctx is set.
401  * \retval -ev error number, and req->rq_cli_ctx == NULL.
402  */
403 int sptlrpc_req_get_ctx(struct ptlrpc_request *req)
404 {
405         struct obd_import *imp = req->rq_import;
406         struct ptlrpc_sec *sec;
407         int                rc;
408         ENTRY;
409
410         LASSERT(!req->rq_cli_ctx);
411         LASSERT(imp);
412
413         rc = import_sec_validate_get(imp, &sec);
414         if (rc)
415                 RETURN(rc);
416
417         req->rq_cli_ctx = get_my_ctx(sec);
418
419         sptlrpc_sec_put(sec);
420
421         if (!req->rq_cli_ctx) {
422                 CERROR("req %p: fail to get context\n", req);
423                 RETURN(-ENOMEM);
424         }
425
426         RETURN(0);
427 }
428
429 /**
430  * Drop the context for \a req.
431  * \pre req->rq_cli_ctx != NULL.
432  * \post req->rq_cli_ctx == NULL.
433  *
434  * If \a sync == 0, this function should return quickly without sleep;
435  * otherwise it might trigger and wait for the whole process of sending
436  * an context-destroying rpc to server.
437  */
438 void sptlrpc_req_put_ctx(struct ptlrpc_request *req, int sync)
439 {
440         ENTRY;
441
442         LASSERT(req);
443         LASSERT(req->rq_cli_ctx);
444
445         /* request might be asked to release earlier while still
446          * in the context waiting list.
447          */
448         if (!cfs_list_empty(&req->rq_ctx_chain)) {
449                 cfs_spin_lock(&req->rq_cli_ctx->cc_lock);
450                 cfs_list_del_init(&req->rq_ctx_chain);
451                 cfs_spin_unlock(&req->rq_cli_ctx->cc_lock);
452         }
453
454         sptlrpc_cli_ctx_put(req->rq_cli_ctx, sync);
455         req->rq_cli_ctx = NULL;
456         EXIT;
457 }
458
459 static
460 int sptlrpc_req_ctx_switch(struct ptlrpc_request *req,
461                            struct ptlrpc_cli_ctx *oldctx,
462                            struct ptlrpc_cli_ctx *newctx)
463 {
464         struct sptlrpc_flavor   old_flvr;
465         char                   *reqmsg = NULL; /* to workaround old gcc */
466         int                     reqmsg_size;
467         int                     rc = 0;
468
469         LASSERT(req->rq_reqmsg);
470         LASSERT(req->rq_reqlen);
471         LASSERT(req->rq_replen);
472
473         CDEBUG(D_SEC, "req %p: switch ctx %p(%u->%s) -> %p(%u->%s), "
474                "switch sec %p(%s) -> %p(%s)\n", req,
475                oldctx, oldctx->cc_vcred.vc_uid, sec2target_str(oldctx->cc_sec),
476                newctx, newctx->cc_vcred.vc_uid, sec2target_str(newctx->cc_sec),
477                oldctx->cc_sec, oldctx->cc_sec->ps_policy->sp_name,
478                newctx->cc_sec, newctx->cc_sec->ps_policy->sp_name);
479
480         /* save flavor */
481         old_flvr = req->rq_flvr;
482
483         /* save request message */
484         reqmsg_size = req->rq_reqlen;
485         if (reqmsg_size != 0) {
486                 OBD_ALLOC_LARGE(reqmsg, reqmsg_size);
487                 if (reqmsg == NULL)
488                         return -ENOMEM;
489                 memcpy(reqmsg, req->rq_reqmsg, reqmsg_size);
490         }
491
492         /* release old req/rep buf */
493         req->rq_cli_ctx = oldctx;
494         sptlrpc_cli_free_reqbuf(req);
495         sptlrpc_cli_free_repbuf(req);
496         req->rq_cli_ctx = newctx;
497
498         /* recalculate the flavor */
499         sptlrpc_req_set_flavor(req, 0);
500
501         /* alloc new request buffer
502          * we don't need to alloc reply buffer here, leave it to the
503          * rest procedure of ptlrpc */
504         if (reqmsg_size != 0) {
505                 rc = sptlrpc_cli_alloc_reqbuf(req, reqmsg_size);
506                 if (!rc) {
507                         LASSERT(req->rq_reqmsg);
508                         memcpy(req->rq_reqmsg, reqmsg, reqmsg_size);
509                 } else {
510                         CWARN("failed to alloc reqbuf: %d\n", rc);
511                         req->rq_flvr = old_flvr;
512                 }
513
514                 OBD_FREE_LARGE(reqmsg, reqmsg_size);
515         }
516         return rc;
517 }
518
519 /**
520  * If current context of \a req is dead somehow, e.g. we just switched flavor
521  * thus marked original contexts dead, we'll find a new context for it. if
522  * no switch is needed, \a req will end up with the same context.
523  *
524  * \note a request must have a context, to keep other parts of code happy.
525  * In any case of failure during the switching, we must restore the old one.
526  */
527 int sptlrpc_req_replace_dead_ctx(struct ptlrpc_request *req)
528 {
529         struct ptlrpc_cli_ctx *oldctx = req->rq_cli_ctx;
530         struct ptlrpc_cli_ctx *newctx;
531         int                    rc;
532         ENTRY;
533
534         LASSERT(oldctx);
535
536         sptlrpc_cli_ctx_get(oldctx);
537         sptlrpc_req_put_ctx(req, 0);
538
539         rc = sptlrpc_req_get_ctx(req);
540         if (unlikely(rc)) {
541                 LASSERT(!req->rq_cli_ctx);
542
543                 /* restore old ctx */
544                 req->rq_cli_ctx = oldctx;
545                 RETURN(rc);
546         }
547
548         newctx = req->rq_cli_ctx;
549         LASSERT(newctx);
550
551         if (unlikely(newctx == oldctx && 
552                      cfs_test_bit(PTLRPC_CTX_DEAD_BIT, &oldctx->cc_flags))) {
553                 /*
554                  * still get the old dead ctx, usually means system too busy
555                  */
556                 CDEBUG(D_SEC,
557                        "ctx (%p, fl %lx) doesn't switch, relax a little bit\n",
558                        newctx, newctx->cc_flags);
559
560                 cfs_schedule_timeout_and_set_state(CFS_TASK_INTERRUPTIBLE,
561                                                    CFS_HZ);
562         } else {
563                 /*
564                  * it's possible newctx == oldctx if we're switching
565                  * subflavor with the same sec.
566                  */
567                 rc = sptlrpc_req_ctx_switch(req, oldctx, newctx);
568                 if (rc) {
569                         /* restore old ctx */
570                         sptlrpc_req_put_ctx(req, 0);
571                         req->rq_cli_ctx = oldctx;
572                         RETURN(rc);
573                 }
574
575                 LASSERT(req->rq_cli_ctx == newctx);
576         }
577
578         sptlrpc_cli_ctx_put(oldctx, 1);
579         RETURN(0);
580 }
581 EXPORT_SYMBOL(sptlrpc_req_replace_dead_ctx);
582
583 static
584 int ctx_check_refresh(struct ptlrpc_cli_ctx *ctx)
585 {
586         if (cli_ctx_is_refreshed(ctx))
587                 return 1;
588         return 0;
589 }
590
591 static
592 int ctx_refresh_timeout(void *data)
593 {
594         struct ptlrpc_request *req = data;
595         int rc;
596
597         /* conn_cnt is needed in expire_one_request */
598         lustre_msg_set_conn_cnt(req->rq_reqmsg, req->rq_import->imp_conn_cnt);
599
600         rc = ptlrpc_expire_one_request(req, 1);
601         /* if we started recovery, we should mark this ctx dead; otherwise
602          * in case of lgssd died nobody would retire this ctx, following
603          * connecting will still find the same ctx thus cause deadlock.
604          * there's an assumption that expire time of the request should be
605          * later than the context refresh expire time.
606          */
607         if (rc == 0)
608                 req->rq_cli_ctx->cc_ops->die(req->rq_cli_ctx, 0);
609         return rc;
610 }
611
612 static
613 void ctx_refresh_interrupt(void *data)
614 {
615         struct ptlrpc_request *req = data;
616
617         cfs_spin_lock(&req->rq_lock);
618         req->rq_intr = 1;
619         cfs_spin_unlock(&req->rq_lock);
620 }
621
622 static
623 void req_off_ctx_list(struct ptlrpc_request *req, struct ptlrpc_cli_ctx *ctx)
624 {
625         cfs_spin_lock(&ctx->cc_lock);
626         if (!cfs_list_empty(&req->rq_ctx_chain))
627                 cfs_list_del_init(&req->rq_ctx_chain);
628         cfs_spin_unlock(&ctx->cc_lock);
629 }
630
631 /**
632  * To refresh the context of \req, if it's not up-to-date.
633  * \param timeout
634  * - < 0: don't wait
635  * - = 0: wait until success or fatal error occur
636  * - > 0: timeout value (in seconds)
637  *
638  * The status of the context could be subject to be changed by other threads
639  * at any time. We allow this race, but once we return with 0, the caller will
640  * suppose it's uptodated and keep using it until the owning rpc is done.
641  *
642  * \retval 0 only if the context is uptodated.
643  * \retval -ev error number.
644  */
645 int sptlrpc_req_refresh_ctx(struct ptlrpc_request *req, long timeout)
646 {
647         struct ptlrpc_cli_ctx  *ctx = req->rq_cli_ctx;
648         struct ptlrpc_sec      *sec;
649         struct l_wait_info      lwi;
650         int                     rc;
651         ENTRY;
652
653         LASSERT(ctx);
654
655         if (req->rq_ctx_init || req->rq_ctx_fini)
656                 RETURN(0);
657
658         /*
659          * during the process a request's context might change type even
660          * (e.g. from gss ctx to null ctx), so each loop we need to re-check
661          * everything
662          */
663 again:
664         rc = import_sec_validate_get(req->rq_import, &sec);
665         if (rc)
666                 RETURN(rc);
667
668         if (sec->ps_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
669                 CDEBUG(D_SEC, "req %p: flavor has changed %x -> %x\n",
670                       req, req->rq_flvr.sf_rpc, sec->ps_flvr.sf_rpc);
671                 req_off_ctx_list(req, ctx);
672                 sptlrpc_req_replace_dead_ctx(req);
673                 ctx = req->rq_cli_ctx;
674         }
675         sptlrpc_sec_put(sec);
676
677         if (cli_ctx_is_eternal(ctx))
678                 RETURN(0);
679
680         if (unlikely(cfs_test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags))) {
681                 LASSERT(ctx->cc_ops->refresh);
682                 ctx->cc_ops->refresh(ctx);
683         }
684         LASSERT(cfs_test_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags) == 0);
685
686         LASSERT(ctx->cc_ops->validate);
687         if (ctx->cc_ops->validate(ctx) == 0) {
688                 req_off_ctx_list(req, ctx);
689                 RETURN(0);
690         }
691
692         if (unlikely(cfs_test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags))) {
693                 cfs_spin_lock(&req->rq_lock);
694                 req->rq_err = 1;
695                 cfs_spin_unlock(&req->rq_lock);
696                 req_off_ctx_list(req, ctx);
697                 RETURN(-EPERM);
698         }
699
700         /*
701          * There's a subtle issue for resending RPCs, suppose following
702          * situation:
703          *  1. the request was sent to server.
704          *  2. recovery was kicked start, after finished the request was
705          *     marked as resent.
706          *  3. resend the request.
707          *  4. old reply from server received, we accept and verify the reply.
708          *     this has to be success, otherwise the error will be aware
709          *     by application.
710          *  5. new reply from server received, dropped by LNet.
711          *
712          * Note the xid of old & new request is the same. We can't simply
713          * change xid for the resent request because the server replies on
714          * it for reply reconstruction.
715          *
716          * Commonly the original context should be uptodate because we
717          * have a expiry nice time; server will keep its context because
718          * we at least hold a ref of old context which prevent context
719          * destroying RPC being sent. So server still can accept the request
720          * and finish the RPC. But if that's not the case:
721          *  1. If server side context has been trimmed, a NO_CONTEXT will
722          *     be returned, gss_cli_ctx_verify/unseal will switch to new
723          *     context by force.
724          *  2. Current context never be refreshed, then we are fine: we
725          *     never really send request with old context before.
726          */
727         if (cfs_test_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags) &&
728             unlikely(req->rq_reqmsg) &&
729             lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) {
730                 req_off_ctx_list(req, ctx);
731                 RETURN(0);
732         }
733
734         if (unlikely(cfs_test_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags))) {
735                 req_off_ctx_list(req, ctx);
736                 /*
737                  * don't switch ctx if import was deactivated
738                  */
739                 if (req->rq_import->imp_deactive) {
740                         cfs_spin_lock(&req->rq_lock);
741                         req->rq_err = 1;
742                         cfs_spin_unlock(&req->rq_lock);
743                         RETURN(-EINTR);
744                 }
745
746                 rc = sptlrpc_req_replace_dead_ctx(req);
747                 if (rc) {
748                         LASSERT(ctx == req->rq_cli_ctx);
749                         CERROR("req %p: failed to replace dead ctx %p: %d\n",
750                                 req, ctx, rc);
751                         cfs_spin_lock(&req->rq_lock);
752                         req->rq_err = 1;
753                         cfs_spin_unlock(&req->rq_lock);
754                         RETURN(rc);
755                 }
756
757                 ctx = req->rq_cli_ctx;
758                 goto again;
759         }
760
761         /*
762          * Now we're sure this context is during upcall, add myself into
763          * waiting list
764          */
765         cfs_spin_lock(&ctx->cc_lock);
766         if (cfs_list_empty(&req->rq_ctx_chain))
767                 cfs_list_add(&req->rq_ctx_chain, &ctx->cc_req_list);
768         cfs_spin_unlock(&ctx->cc_lock);
769
770         if (timeout < 0)
771                 RETURN(-EWOULDBLOCK);
772
773         /* Clear any flags that may be present from previous sends */
774         LASSERT(req->rq_receiving_reply == 0);
775         cfs_spin_lock(&req->rq_lock);
776         req->rq_err = 0;
777         req->rq_timedout = 0;
778         req->rq_resend = 0;
779         req->rq_restart = 0;
780         cfs_spin_unlock(&req->rq_lock);
781
782         lwi = LWI_TIMEOUT_INTR(timeout * CFS_HZ, ctx_refresh_timeout,
783                                ctx_refresh_interrupt, req);
784         rc = l_wait_event(req->rq_reply_waitq, ctx_check_refresh(ctx), &lwi);
785
786         /*
787          * following cases could lead us here:
788          * - successfully refreshed;
789          * - interrupted;
790          * - timedout, and we don't want recover from the failure;
791          * - timedout, and waked up upon recovery finished;
792          * - someone else mark this ctx dead by force;
793          * - someone invalidate the req and call ptlrpc_client_wake_req(),
794          *   e.g. ptlrpc_abort_inflight();
795          */
796         if (!cli_ctx_is_refreshed(ctx)) {
797                 /* timed out or interruptted */
798                 req_off_ctx_list(req, ctx);
799
800                 LASSERT(rc != 0);
801                 RETURN(rc);
802         }
803
804         goto again;
805 }
806
807 /**
808  * Initialize flavor settings for \a req, according to \a opcode.
809  *
810  * \note this could be called in two situations:
811  * - new request from ptlrpc_pre_req(), with proper @opcode
812  * - old request which changed ctx in the middle, with @opcode == 0
813  */
814 void sptlrpc_req_set_flavor(struct ptlrpc_request *req, int opcode)
815 {
816         struct ptlrpc_sec *sec;
817
818         LASSERT(req->rq_import);
819         LASSERT(req->rq_cli_ctx);
820         LASSERT(req->rq_cli_ctx->cc_sec);
821         LASSERT(req->rq_bulk_read == 0 || req->rq_bulk_write == 0);
822
823         /* special security flags accoding to opcode */
824         switch (opcode) {
825         case OST_READ:
826         case MDS_READPAGE:
827         case MGS_CONFIG_READ:
828                 req->rq_bulk_read = 1;
829                 break;
830         case OST_WRITE:
831         case MDS_WRITEPAGE:
832                 req->rq_bulk_write = 1;
833                 break;
834         case SEC_CTX_INIT:
835                 req->rq_ctx_init = 1;
836                 break;
837         case SEC_CTX_FINI:
838                 req->rq_ctx_fini = 1;
839                 break;
840         case 0:
841                 /* init/fini rpc won't be resend, so can't be here */
842                 LASSERT(req->rq_ctx_init == 0);
843                 LASSERT(req->rq_ctx_fini == 0);
844
845                 /* cleanup flags, which should be recalculated */
846                 req->rq_pack_udesc = 0;
847                 req->rq_pack_bulk = 0;
848                 break;
849         }
850
851         sec = req->rq_cli_ctx->cc_sec;
852
853         cfs_spin_lock(&sec->ps_lock);
854         req->rq_flvr = sec->ps_flvr;
855         cfs_spin_unlock(&sec->ps_lock);
856
857         /* force SVC_NULL for context initiation rpc, SVC_INTG for context
858          * destruction rpc */
859         if (unlikely(req->rq_ctx_init))
860                 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_NULL);
861         else if (unlikely(req->rq_ctx_fini))
862                 flvr_set_svc(&req->rq_flvr.sf_rpc, SPTLRPC_SVC_INTG);
863
864         /* user descriptor flag, null security can't do it anyway */
865         if ((sec->ps_flvr.sf_flags & PTLRPC_SEC_FL_UDESC) &&
866             (req->rq_flvr.sf_rpc != SPTLRPC_FLVR_NULL))
867                 req->rq_pack_udesc = 1;
868
869         /* bulk security flag */
870         if ((req->rq_bulk_read || req->rq_bulk_write) &&
871             sptlrpc_flavor_has_bulk(&req->rq_flvr))
872                 req->rq_pack_bulk = 1;
873 }
874
875 void sptlrpc_request_out_callback(struct ptlrpc_request *req)
876 {
877         if (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) != SPTLRPC_SVC_PRIV)
878                 return;
879
880         LASSERT(req->rq_clrbuf);
881         if (req->rq_pool || !req->rq_reqbuf)
882                 return;
883
884         OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
885         req->rq_reqbuf = NULL;
886         req->rq_reqbuf_len = 0;
887 }
888
889 /**
890  * Given an import \a imp, check whether current user has a valid context
891  * or not. We may create a new context and try to refresh it, and try
892  * repeatedly try in case of non-fatal errors. Return 0 means success.
893  */
894 int sptlrpc_import_check_ctx(struct obd_import *imp)
895 {
896         struct ptlrpc_sec     *sec;
897         struct ptlrpc_cli_ctx *ctx;
898         struct ptlrpc_request *req = NULL;
899         int rc;
900         ENTRY;
901
902         cfs_might_sleep();
903
904         sec = sptlrpc_import_sec_ref(imp);
905         ctx = get_my_ctx(sec);
906         sptlrpc_sec_put(sec);
907
908         if (!ctx)
909                 RETURN(-ENOMEM);
910
911         if (cli_ctx_is_eternal(ctx) ||
912             ctx->cc_ops->validate(ctx) == 0) {
913                 sptlrpc_cli_ctx_put(ctx, 1);
914                 RETURN(0);
915         }
916
917         if (cli_ctx_is_error(ctx)) {
918                 sptlrpc_cli_ctx_put(ctx, 1);
919                 RETURN(-EACCES);
920         }
921
922         OBD_ALLOC_PTR(req);
923         if (!req)
924                 RETURN(-ENOMEM);
925
926         cfs_spin_lock_init(&req->rq_lock);
927         cfs_atomic_set(&req->rq_refcount, 10000);
928         CFS_INIT_LIST_HEAD(&req->rq_ctx_chain);
929         cfs_waitq_init(&req->rq_reply_waitq);
930         cfs_waitq_init(&req->rq_set_waitq);
931         req->rq_import = imp;
932         req->rq_flvr = sec->ps_flvr;
933         req->rq_cli_ctx = ctx;
934
935         rc = sptlrpc_req_refresh_ctx(req, 0);
936         LASSERT(cfs_list_empty(&req->rq_ctx_chain));
937         sptlrpc_cli_ctx_put(req->rq_cli_ctx, 1);
938         OBD_FREE_PTR(req);
939
940         RETURN(rc);
941 }
942
943 /**
944  * Used by ptlrpc client, to perform the pre-defined security transformation
945  * upon the request message of \a req. After this function called,
946  * req->rq_reqmsg is still accessible as clear text.
947  */
948 int sptlrpc_cli_wrap_request(struct ptlrpc_request *req)
949 {
950         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
951         int rc = 0;
952         ENTRY;
953
954         LASSERT(ctx);
955         LASSERT(ctx->cc_sec);
956         LASSERT(req->rq_reqbuf || req->rq_clrbuf);
957
958         /* we wrap bulk request here because now we can be sure
959          * the context is uptodate.
960          */
961         if (req->rq_bulk) {
962                 rc = sptlrpc_cli_wrap_bulk(req, req->rq_bulk);
963                 if (rc)
964                         RETURN(rc);
965         }
966
967         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
968         case SPTLRPC_SVC_NULL:
969         case SPTLRPC_SVC_AUTH:
970         case SPTLRPC_SVC_INTG:
971                 LASSERT(ctx->cc_ops->sign);
972                 rc = ctx->cc_ops->sign(ctx, req);
973                 break;
974         case SPTLRPC_SVC_PRIV:
975                 LASSERT(ctx->cc_ops->seal);
976                 rc = ctx->cc_ops->seal(ctx, req);
977                 break;
978         default:
979                 LBUG();
980         }
981
982         if (rc == 0) {
983                 LASSERT(req->rq_reqdata_len);
984                 LASSERT(req->rq_reqdata_len % 8 == 0);
985                 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
986         }
987
988         RETURN(rc);
989 }
990
991 static int do_cli_unwrap_reply(struct ptlrpc_request *req)
992 {
993         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
994         int                    rc;
995         ENTRY;
996
997         LASSERT(ctx);
998         LASSERT(ctx->cc_sec);
999         LASSERT(req->rq_repbuf);
1000         LASSERT(req->rq_repdata);
1001         LASSERT(req->rq_repmsg == NULL);
1002
1003         req->rq_rep_swab_mask = 0;
1004
1005         rc = __lustre_unpack_msg(req->rq_repdata, req->rq_repdata_len);
1006         switch (rc) {
1007         case 1:
1008                 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
1009         case 0:
1010                 break;
1011         default:
1012                 CERROR("failed unpack reply: x"LPU64"\n", req->rq_xid);
1013                 RETURN(-EPROTO);
1014         }
1015
1016         if (req->rq_repdata_len < sizeof(struct lustre_msg)) {
1017                 CERROR("replied data length %d too small\n",
1018                        req->rq_repdata_len);
1019                 RETURN(-EPROTO);
1020         }
1021
1022         if (SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr) !=
1023             SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc)) {
1024                 CERROR("reply policy %u doesn't match request policy %u\n",
1025                        SPTLRPC_FLVR_POLICY(req->rq_repdata->lm_secflvr),
1026                        SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc));
1027                 RETURN(-EPROTO);
1028         }
1029
1030         switch (SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc)) {
1031         case SPTLRPC_SVC_NULL:
1032         case SPTLRPC_SVC_AUTH:
1033         case SPTLRPC_SVC_INTG:
1034                 LASSERT(ctx->cc_ops->verify);
1035                 rc = ctx->cc_ops->verify(ctx, req);
1036                 break;
1037         case SPTLRPC_SVC_PRIV:
1038                 LASSERT(ctx->cc_ops->unseal);
1039                 rc = ctx->cc_ops->unseal(ctx, req);
1040                 break;
1041         default:
1042                 LBUG();
1043         }
1044         LASSERT(rc || req->rq_repmsg || req->rq_resend);
1045
1046         if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL &&
1047             !req->rq_ctx_init)
1048                 req->rq_rep_swab_mask = 0;
1049         RETURN(rc);
1050 }
1051
1052 /**
1053  * Used by ptlrpc client, to perform security transformation upon the reply
1054  * message of \a req. After return successfully, req->rq_repmsg points to
1055  * the reply message in clear text.
1056  *
1057  * \pre the reply buffer should have been un-posted from LNet, so nothing is
1058  * going to change.
1059  */
1060 int sptlrpc_cli_unwrap_reply(struct ptlrpc_request *req)
1061 {
1062         LASSERT(req->rq_repbuf);
1063         LASSERT(req->rq_repdata == NULL);
1064         LASSERT(req->rq_repmsg == NULL);
1065         LASSERT(req->rq_reply_off + req->rq_nob_received <= req->rq_repbuf_len);
1066
1067         if (req->rq_reply_off == 0 &&
1068             (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1069                 CERROR("real reply with offset 0\n");
1070                 return -EPROTO;
1071         }
1072
1073         if (req->rq_reply_off % 8 != 0) {
1074                 CERROR("reply at odd offset %u\n", req->rq_reply_off);
1075                 return -EPROTO;
1076         }
1077
1078         req->rq_repdata = (struct lustre_msg *)
1079                                 (req->rq_repbuf + req->rq_reply_off);
1080         req->rq_repdata_len = req->rq_nob_received;
1081
1082         return do_cli_unwrap_reply(req);
1083 }
1084
1085 /**
1086  * Used by ptlrpc client, to perform security transformation upon the early
1087  * reply message of \a req. We expect the rq_reply_off is 0, and
1088  * rq_nob_received is the early reply size.
1089  * 
1090  * Because the receive buffer might be still posted, the reply data might be
1091  * changed at any time, no matter we're holding rq_lock or not. For this reason
1092  * we allocate a separate ptlrpc_request and reply buffer for early reply
1093  * processing.
1094  * 
1095  * \retval 0 success, \a req_ret is filled with a duplicated ptlrpc_request.
1096  * Later the caller must call sptlrpc_cli_finish_early_reply() on the returned
1097  * \a *req_ret to release it.
1098  * \retval -ev error number, and \a req_ret will not be set.
1099  */
1100 int sptlrpc_cli_unwrap_early_reply(struct ptlrpc_request *req,
1101                                    struct ptlrpc_request **req_ret)
1102 {
1103         struct ptlrpc_request  *early_req;
1104         char                   *early_buf;
1105         int                     early_bufsz, early_size;
1106         int                     rc;
1107         ENTRY;
1108
1109         OBD_ALLOC_PTR(early_req);
1110         if (early_req == NULL)
1111                 RETURN(-ENOMEM);
1112
1113         early_size = req->rq_nob_received;
1114         early_bufsz = size_roundup_power2(early_size);
1115         OBD_ALLOC_LARGE(early_buf, early_bufsz);
1116         if (early_buf == NULL)
1117                 GOTO(err_req, rc = -ENOMEM);
1118
1119         /* sanity checkings and copy data out, do it inside spinlock */
1120         cfs_spin_lock(&req->rq_lock);
1121
1122         if (req->rq_replied) {
1123                 cfs_spin_unlock(&req->rq_lock);
1124                 GOTO(err_buf, rc = -EALREADY);
1125         }
1126
1127         LASSERT(req->rq_repbuf);
1128         LASSERT(req->rq_repdata == NULL);
1129         LASSERT(req->rq_repmsg == NULL);
1130
1131         if (req->rq_reply_off != 0) {
1132                 CERROR("early reply with offset %u\n", req->rq_reply_off);
1133                 cfs_spin_unlock(&req->rq_lock);
1134                 GOTO(err_buf, rc = -EPROTO);
1135         }
1136
1137         if (req->rq_nob_received != early_size) {
1138                 /* even another early arrived the size should be the same */
1139                 CERROR("data size has changed from %u to %u\n",
1140                        early_size, req->rq_nob_received);
1141                 cfs_spin_unlock(&req->rq_lock);
1142                 GOTO(err_buf, rc = -EINVAL);
1143         }
1144
1145         if (req->rq_nob_received < sizeof(struct lustre_msg)) {
1146                 CERROR("early reply length %d too small\n",
1147                        req->rq_nob_received);
1148                 cfs_spin_unlock(&req->rq_lock);
1149                 GOTO(err_buf, rc = -EALREADY);
1150         }
1151
1152         memcpy(early_buf, req->rq_repbuf, early_size);
1153         cfs_spin_unlock(&req->rq_lock);
1154
1155         cfs_spin_lock_init(&early_req->rq_lock);
1156         early_req->rq_cli_ctx = sptlrpc_cli_ctx_get(req->rq_cli_ctx);
1157         early_req->rq_flvr = req->rq_flvr;
1158         early_req->rq_repbuf = early_buf;
1159         early_req->rq_repbuf_len = early_bufsz;
1160         early_req->rq_repdata = (struct lustre_msg *) early_buf;
1161         early_req->rq_repdata_len = early_size;
1162         early_req->rq_early = 1;
1163         early_req->rq_reqmsg = req->rq_reqmsg;
1164
1165         rc = do_cli_unwrap_reply(early_req);
1166         if (rc) {
1167                 DEBUG_REQ(D_ADAPTTO, early_req,
1168                           "error %d unwrap early reply", rc);
1169                 GOTO(err_ctx, rc);
1170         }
1171
1172         LASSERT(early_req->rq_repmsg);
1173         *req_ret = early_req;
1174         RETURN(0);
1175
1176 err_ctx:
1177         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1178 err_buf:
1179         OBD_FREE_LARGE(early_buf, early_bufsz);
1180 err_req:
1181         OBD_FREE_PTR(early_req);
1182         RETURN(rc);
1183 }
1184
1185 /**
1186  * Used by ptlrpc client, to release a processed early reply \a early_req.
1187  *
1188  * \pre \a early_req was obtained from calling sptlrpc_cli_unwrap_early_reply().
1189  */
1190 void sptlrpc_cli_finish_early_reply(struct ptlrpc_request *early_req)
1191 {
1192         LASSERT(early_req->rq_repbuf);
1193         LASSERT(early_req->rq_repdata);
1194         LASSERT(early_req->rq_repmsg);
1195
1196         sptlrpc_cli_ctx_put(early_req->rq_cli_ctx, 1);
1197         OBD_FREE_LARGE(early_req->rq_repbuf, early_req->rq_repbuf_len);
1198         OBD_FREE_PTR(early_req);
1199 }
1200
1201 /**************************************************
1202  * sec ID                                         *
1203  **************************************************/
1204
1205 /*
1206  * "fixed" sec (e.g. null) use sec_id < 0
1207  */
1208 static cfs_atomic_t sptlrpc_sec_id = CFS_ATOMIC_INIT(1);
1209
1210 int sptlrpc_get_next_secid(void)
1211 {
1212         return cfs_atomic_inc_return(&sptlrpc_sec_id);
1213 }
1214 EXPORT_SYMBOL(sptlrpc_get_next_secid);
1215
1216 /**************************************************
1217  * client side high-level security APIs           *
1218  **************************************************/
1219
1220 static int sec_cop_flush_ctx_cache(struct ptlrpc_sec *sec, uid_t uid,
1221                                    int grace, int force)
1222 {
1223         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1224
1225         LASSERT(policy->sp_cops);
1226         LASSERT(policy->sp_cops->flush_ctx_cache);
1227
1228         return policy->sp_cops->flush_ctx_cache(sec, uid, grace, force);
1229 }
1230
1231 static void sec_cop_destroy_sec(struct ptlrpc_sec *sec)
1232 {
1233         struct ptlrpc_sec_policy *policy = sec->ps_policy;
1234
1235         LASSERT_ATOMIC_ZERO(&sec->ps_refcount);
1236         LASSERT_ATOMIC_ZERO(&sec->ps_nctx);
1237         LASSERT(policy->sp_cops->destroy_sec);
1238
1239         CDEBUG(D_SEC, "%s@%p: being destroied\n", sec->ps_policy->sp_name, sec);
1240
1241         policy->sp_cops->destroy_sec(sec);
1242         sptlrpc_policy_put(policy);
1243 }
1244
1245 void sptlrpc_sec_destroy(struct ptlrpc_sec *sec)
1246 {
1247         sec_cop_destroy_sec(sec);
1248 }
1249 EXPORT_SYMBOL(sptlrpc_sec_destroy);
1250
1251 static void sptlrpc_sec_kill(struct ptlrpc_sec *sec)
1252 {
1253         LASSERT_ATOMIC_POS(&sec->ps_refcount);
1254
1255         if (sec->ps_policy->sp_cops->kill_sec) {
1256                 sec->ps_policy->sp_cops->kill_sec(sec);
1257
1258                 sec_cop_flush_ctx_cache(sec, -1, 1, 1);
1259         }
1260 }
1261
1262 struct ptlrpc_sec *sptlrpc_sec_get(struct ptlrpc_sec *sec)
1263 {
1264         if (sec)
1265                 cfs_atomic_inc(&sec->ps_refcount);
1266
1267         return sec;
1268 }
1269 EXPORT_SYMBOL(sptlrpc_sec_get);
1270
1271 void sptlrpc_sec_put(struct ptlrpc_sec *sec)
1272 {
1273         if (sec) {
1274                 LASSERT_ATOMIC_POS(&sec->ps_refcount);
1275
1276                 if (cfs_atomic_dec_and_test(&sec->ps_refcount)) {
1277                         sptlrpc_gc_del_sec(sec);
1278                         sec_cop_destroy_sec(sec);
1279                 }
1280         }
1281 }
1282 EXPORT_SYMBOL(sptlrpc_sec_put);
1283
1284 /*
1285  * policy module is responsible for taking refrence of import
1286  */
1287 static
1288 struct ptlrpc_sec * sptlrpc_sec_create(struct obd_import *imp,
1289                                        struct ptlrpc_svc_ctx *svc_ctx,
1290                                        struct sptlrpc_flavor *sf,
1291                                        enum lustre_sec_part sp)
1292 {
1293         struct ptlrpc_sec_policy *policy;
1294         struct ptlrpc_sec        *sec;
1295         char                      str[32];
1296         ENTRY;
1297
1298         if (svc_ctx) {
1299                 LASSERT(imp->imp_dlm_fake == 1);
1300
1301                 CDEBUG(D_SEC, "%s %s: reverse sec using flavor %s\n",
1302                        imp->imp_obd->obd_type->typ_name,
1303                        imp->imp_obd->obd_name,
1304                        sptlrpc_flavor2name(sf, str, sizeof(str)));
1305
1306                 policy = sptlrpc_policy_get(svc_ctx->sc_policy);
1307                 sf->sf_flags |= PTLRPC_SEC_FL_REVERSE | PTLRPC_SEC_FL_ROOTONLY;
1308         } else {
1309                 LASSERT(imp->imp_dlm_fake == 0);
1310
1311                 CDEBUG(D_SEC, "%s %s: select security flavor %s\n",
1312                        imp->imp_obd->obd_type->typ_name,
1313                        imp->imp_obd->obd_name,
1314                        sptlrpc_flavor2name(sf, str, sizeof(str)));
1315
1316                 policy = sptlrpc_wireflavor2policy(sf->sf_rpc);
1317                 if (!policy) {
1318                         CERROR("invalid flavor 0x%x\n", sf->sf_rpc);
1319                         RETURN(NULL);
1320                 }
1321         }
1322
1323         sec = policy->sp_cops->create_sec(imp, svc_ctx, sf);
1324         if (sec) {
1325                 cfs_atomic_inc(&sec->ps_refcount);
1326
1327                 sec->ps_part = sp;
1328
1329                 if (sec->ps_gc_interval && policy->sp_cops->gc_ctx)
1330                         sptlrpc_gc_add_sec(sec);
1331         } else {
1332                 sptlrpc_policy_put(policy);
1333         }
1334
1335         RETURN(sec);
1336 }
1337
1338 struct ptlrpc_sec *sptlrpc_import_sec_ref(struct obd_import *imp)
1339 {
1340         struct ptlrpc_sec *sec;
1341
1342         cfs_spin_lock(&imp->imp_lock);
1343         sec = sptlrpc_sec_get(imp->imp_sec);
1344         cfs_spin_unlock(&imp->imp_lock);
1345
1346         return sec;
1347 }
1348 EXPORT_SYMBOL(sptlrpc_import_sec_ref);
1349
1350 static void sptlrpc_import_sec_install(struct obd_import *imp,
1351                                        struct ptlrpc_sec *sec)
1352 {
1353         struct ptlrpc_sec *old_sec;
1354
1355         LASSERT_ATOMIC_POS(&sec->ps_refcount);
1356
1357         cfs_spin_lock(&imp->imp_lock);
1358         old_sec = imp->imp_sec;
1359         imp->imp_sec = sec;
1360         cfs_spin_unlock(&imp->imp_lock);
1361
1362         if (old_sec) {
1363                 sptlrpc_sec_kill(old_sec);
1364
1365                 /* balance the ref taken by this import */
1366                 sptlrpc_sec_put(old_sec);
1367         }
1368 }
1369
1370 static inline
1371 int flavor_equal(struct sptlrpc_flavor *sf1, struct sptlrpc_flavor *sf2)
1372 {
1373         return (memcmp(sf1, sf2, sizeof(*sf1)) == 0);
1374 }
1375
1376 static inline
1377 void flavor_copy(struct sptlrpc_flavor *dst, struct sptlrpc_flavor *src)
1378 {
1379         *dst = *src;
1380 }
1381
1382 static void sptlrpc_import_sec_adapt_inplace(struct obd_import *imp,
1383                                              struct ptlrpc_sec *sec,
1384                                              struct sptlrpc_flavor *sf)
1385 {
1386         char    str1[32], str2[32];
1387
1388         if (sec->ps_flvr.sf_flags != sf->sf_flags)
1389                 CDEBUG(D_SEC, "changing sec flags: %s -> %s\n",
1390                        sptlrpc_secflags2str(sec->ps_flvr.sf_flags,
1391                                             str1, sizeof(str1)),
1392                        sptlrpc_secflags2str(sf->sf_flags,
1393                                             str2, sizeof(str2)));
1394
1395         cfs_spin_lock(&sec->ps_lock);
1396         flavor_copy(&sec->ps_flvr, sf);
1397         cfs_spin_unlock(&sec->ps_lock);
1398 }
1399
1400 /**
1401  * To get an appropriate ptlrpc_sec for the \a imp, according to the current
1402  * configuration. Upon called, imp->imp_sec may or may not be NULL.
1403  *
1404  *  - regular import: \a svc_ctx should be NULL and \a flvr is ignored;
1405  *  - reverse import: \a svc_ctx and \a flvr are obtained from incoming request.
1406  */
1407 int sptlrpc_import_sec_adapt(struct obd_import *imp,
1408                              struct ptlrpc_svc_ctx *svc_ctx,
1409                              struct sptlrpc_flavor *flvr)
1410 {
1411         struct ptlrpc_connection   *conn;
1412         struct sptlrpc_flavor       sf;
1413         struct ptlrpc_sec          *sec, *newsec;
1414         enum lustre_sec_part        sp;
1415         char                        str[24];
1416         int                         rc = 0;
1417         ENTRY;
1418
1419         cfs_might_sleep();
1420
1421         if (imp == NULL)
1422                 RETURN(0);
1423
1424         conn = imp->imp_connection;
1425
1426         if (svc_ctx == NULL) {
1427                 struct client_obd *cliobd = &imp->imp_obd->u.cli;
1428                 /*
1429                  * normal import, determine flavor from rule set, except
1430                  * for mgc the flavor is predetermined.
1431                  */
1432                 if (cliobd->cl_sp_me == LUSTRE_SP_MGC)
1433                         sf = cliobd->cl_flvr_mgc;
1434                 else 
1435                         sptlrpc_conf_choose_flavor(cliobd->cl_sp_me,
1436                                                    cliobd->cl_sp_to,
1437                                                    &cliobd->cl_target_uuid,
1438                                                    conn->c_self, &sf);
1439
1440                 sp = imp->imp_obd->u.cli.cl_sp_me;
1441         } else {
1442                 /* reverse import, determine flavor from incoming reqeust */
1443                 sf = *flvr;
1444
1445                 if (sf.sf_rpc != SPTLRPC_FLVR_NULL)
1446                         sf.sf_flags = PTLRPC_SEC_FL_REVERSE |
1447                                       PTLRPC_SEC_FL_ROOTONLY;
1448
1449                 sp = sptlrpc_target_sec_part(imp->imp_obd);
1450         }
1451
1452         sec = sptlrpc_import_sec_ref(imp);
1453         if (sec) {
1454                 char    str2[24];
1455
1456                 if (flavor_equal(&sf, &sec->ps_flvr))
1457                         GOTO(out, rc);
1458
1459                 CDEBUG(D_SEC, "import %s->%s: changing flavor %s -> %s\n",
1460                        imp->imp_obd->obd_name,
1461                        obd_uuid2str(&conn->c_remote_uuid),
1462                        sptlrpc_flavor2name(&sec->ps_flvr, str, sizeof(str)),
1463                        sptlrpc_flavor2name(&sf, str2, sizeof(str2)));
1464
1465                 if (SPTLRPC_FLVR_POLICY(sf.sf_rpc) ==
1466                     SPTLRPC_FLVR_POLICY(sec->ps_flvr.sf_rpc) &&
1467                     SPTLRPC_FLVR_MECH(sf.sf_rpc) ==
1468                     SPTLRPC_FLVR_MECH(sec->ps_flvr.sf_rpc)) {
1469                         sptlrpc_import_sec_adapt_inplace(imp, sec, &sf);
1470                         GOTO(out, rc);
1471                 }
1472         } else if (SPTLRPC_FLVR_BASE(sf.sf_rpc) !=
1473                    SPTLRPC_FLVR_BASE(SPTLRPC_FLVR_NULL)) {
1474                 CDEBUG(D_SEC, "import %s->%s netid %x: select flavor %s\n",
1475                        imp->imp_obd->obd_name,
1476                        obd_uuid2str(&conn->c_remote_uuid),
1477                        LNET_NIDNET(conn->c_self),
1478                        sptlrpc_flavor2name(&sf, str, sizeof(str)));
1479         }
1480
1481         cfs_mutex_down(&imp->imp_sec_mutex);
1482
1483         newsec = sptlrpc_sec_create(imp, svc_ctx, &sf, sp);
1484         if (newsec) {
1485                 sptlrpc_import_sec_install(imp, newsec);
1486         } else {
1487                 CERROR("import %s->%s: failed to create new sec\n",
1488                        imp->imp_obd->obd_name,
1489                        obd_uuid2str(&conn->c_remote_uuid));
1490                 rc = -EPERM;
1491         }
1492
1493         cfs_mutex_up(&imp->imp_sec_mutex);
1494 out:
1495         sptlrpc_sec_put(sec);
1496         RETURN(rc);
1497 }
1498
1499 void sptlrpc_import_sec_put(struct obd_import *imp)
1500 {
1501         if (imp->imp_sec) {
1502                 sptlrpc_sec_kill(imp->imp_sec);
1503
1504                 sptlrpc_sec_put(imp->imp_sec);
1505                 imp->imp_sec = NULL;
1506         }
1507 }
1508
1509 static void import_flush_ctx_common(struct obd_import *imp,
1510                                     uid_t uid, int grace, int force)
1511 {
1512         struct ptlrpc_sec *sec;
1513
1514         if (imp == NULL)
1515                 return;
1516
1517         sec = sptlrpc_import_sec_ref(imp);
1518         if (sec == NULL)
1519                 return;
1520
1521         sec_cop_flush_ctx_cache(sec, uid, grace, force);
1522         sptlrpc_sec_put(sec);
1523 }
1524
1525 void sptlrpc_import_flush_root_ctx(struct obd_import *imp)
1526 {
1527         /* it's important to use grace mode, see explain in
1528          * sptlrpc_req_refresh_ctx() */
1529         import_flush_ctx_common(imp, 0, 1, 1);
1530 }
1531
1532 void sptlrpc_import_flush_my_ctx(struct obd_import *imp)
1533 {
1534         import_flush_ctx_common(imp, cfs_curproc_uid(), 1, 1);
1535 }
1536 EXPORT_SYMBOL(sptlrpc_import_flush_my_ctx);
1537
1538 void sptlrpc_import_flush_all_ctx(struct obd_import *imp)
1539 {
1540         import_flush_ctx_common(imp, -1, 1, 1);
1541 }
1542 EXPORT_SYMBOL(sptlrpc_import_flush_all_ctx);
1543
1544 /**
1545  * Used by ptlrpc client to allocate request buffer of \a req. Upon return
1546  * successfully, req->rq_reqmsg points to a buffer with size \a msgsize.
1547  */
1548 int sptlrpc_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
1549 {
1550         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1551         struct ptlrpc_sec_policy *policy;
1552         int rc;
1553
1554         LASSERT(ctx);
1555         LASSERT(ctx->cc_sec);
1556         LASSERT(ctx->cc_sec->ps_policy);
1557         LASSERT(req->rq_reqmsg == NULL);
1558         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1559
1560         policy = ctx->cc_sec->ps_policy;
1561         rc = policy->sp_cops->alloc_reqbuf(ctx->cc_sec, req, msgsize);
1562         if (!rc) {
1563                 LASSERT(req->rq_reqmsg);
1564                 LASSERT(req->rq_reqbuf || req->rq_clrbuf);
1565
1566                 /* zeroing preallocated buffer */
1567                 if (req->rq_pool)
1568                         memset(req->rq_reqmsg, 0, msgsize);
1569         }
1570
1571         return rc;
1572 }
1573
1574 /**
1575  * Used by ptlrpc client to free request buffer of \a req. After this
1576  * req->rq_reqmsg is set to NULL and should not be accessed anymore.
1577  */
1578 void sptlrpc_cli_free_reqbuf(struct ptlrpc_request *req)
1579 {
1580         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1581         struct ptlrpc_sec_policy *policy;
1582
1583         LASSERT(ctx);
1584         LASSERT(ctx->cc_sec);
1585         LASSERT(ctx->cc_sec->ps_policy);
1586         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1587
1588         if (req->rq_reqbuf == NULL && req->rq_clrbuf == NULL)
1589                 return;
1590
1591         policy = ctx->cc_sec->ps_policy;
1592         policy->sp_cops->free_reqbuf(ctx->cc_sec, req);
1593         req->rq_reqmsg = NULL;
1594 }
1595
1596 /*
1597  * NOTE caller must guarantee the buffer size is enough for the enlargement
1598  */
1599 void _sptlrpc_enlarge_msg_inplace(struct lustre_msg *msg,
1600                                   int segment, int newsize)
1601 {
1602         void   *src, *dst;
1603         int     oldsize, oldmsg_size, movesize;
1604
1605         LASSERT(segment < msg->lm_bufcount);
1606         LASSERT(msg->lm_buflens[segment] <= newsize);
1607
1608         if (msg->lm_buflens[segment] == newsize)
1609                 return;
1610
1611         /* nothing to do if we are enlarging the last segment */
1612         if (segment == msg->lm_bufcount - 1) {
1613                 msg->lm_buflens[segment] = newsize;
1614                 return;
1615         }
1616
1617         oldsize = msg->lm_buflens[segment];
1618
1619         src = lustre_msg_buf(msg, segment + 1, 0);
1620         msg->lm_buflens[segment] = newsize;
1621         dst = lustre_msg_buf(msg, segment + 1, 0);
1622         msg->lm_buflens[segment] = oldsize;
1623
1624         /* move from segment + 1 to end segment */
1625         LASSERT(msg->lm_magic == LUSTRE_MSG_MAGIC_V2);
1626         oldmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
1627         movesize = oldmsg_size - ((unsigned long) src - (unsigned long) msg);
1628         LASSERT(movesize >= 0);
1629
1630         if (movesize)
1631                 memmove(dst, src, movesize);
1632
1633         /* note we don't clear the ares where old data live, not secret */
1634
1635         /* finally set new segment size */
1636         msg->lm_buflens[segment] = newsize;
1637 }
1638 EXPORT_SYMBOL(_sptlrpc_enlarge_msg_inplace);
1639
1640 /**
1641  * Used by ptlrpc client to enlarge the \a segment of request message pointed
1642  * by req->rq_reqmsg to size \a newsize, all previously filled-in data will be
1643  * preserved after the enlargement. this must be called after original request
1644  * buffer being allocated.
1645  *
1646  * \note after this be called, rq_reqmsg and rq_reqlen might have been changed,
1647  * so caller should refresh its local pointers if needed.
1648  */
1649 int sptlrpc_cli_enlarge_reqbuf(struct ptlrpc_request *req,
1650                                int segment, int newsize)
1651 {
1652         struct ptlrpc_cli_ctx    *ctx = req->rq_cli_ctx;
1653         struct ptlrpc_sec_cops   *cops;
1654         struct lustre_msg        *msg = req->rq_reqmsg;
1655
1656         LASSERT(ctx);
1657         LASSERT(msg);
1658         LASSERT(msg->lm_bufcount > segment);
1659         LASSERT(msg->lm_buflens[segment] <= newsize);
1660
1661         if (msg->lm_buflens[segment] == newsize)
1662                 return 0;
1663
1664         cops = ctx->cc_sec->ps_policy->sp_cops;
1665         LASSERT(cops->enlarge_reqbuf);
1666         return cops->enlarge_reqbuf(ctx->cc_sec, req, segment, newsize);
1667 }
1668 EXPORT_SYMBOL(sptlrpc_cli_enlarge_reqbuf);
1669
1670 /**
1671  * Used by ptlrpc client to allocate reply buffer of \a req.
1672  *
1673  * \note After this, req->rq_repmsg is still not accessible.
1674  */
1675 int sptlrpc_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
1676 {
1677         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1678         struct ptlrpc_sec_policy *policy;
1679         ENTRY;
1680
1681         LASSERT(ctx);
1682         LASSERT(ctx->cc_sec);
1683         LASSERT(ctx->cc_sec->ps_policy);
1684
1685         if (req->rq_repbuf)
1686                 RETURN(0);
1687
1688         policy = ctx->cc_sec->ps_policy;
1689         RETURN(policy->sp_cops->alloc_repbuf(ctx->cc_sec, req, msgsize));
1690 }
1691
1692 /**
1693  * Used by ptlrpc client to free reply buffer of \a req. After this
1694  * req->rq_repmsg is set to NULL and should not be accessed anymore.
1695  */
1696 void sptlrpc_cli_free_repbuf(struct ptlrpc_request *req)
1697 {
1698         struct ptlrpc_cli_ctx *ctx = req->rq_cli_ctx;
1699         struct ptlrpc_sec_policy *policy;
1700         ENTRY;
1701
1702         LASSERT(ctx);
1703         LASSERT(ctx->cc_sec);
1704         LASSERT(ctx->cc_sec->ps_policy);
1705         LASSERT_ATOMIC_POS(&ctx->cc_refcount);
1706
1707         if (req->rq_repbuf == NULL)
1708                 return;
1709         LASSERT(req->rq_repbuf_len);
1710
1711         policy = ctx->cc_sec->ps_policy;
1712         policy->sp_cops->free_repbuf(ctx->cc_sec, req);
1713         req->rq_repmsg = NULL;
1714         EXIT;
1715 }
1716
1717 int sptlrpc_cli_install_rvs_ctx(struct obd_import *imp,
1718                                 struct ptlrpc_cli_ctx *ctx)
1719 {
1720         struct ptlrpc_sec_policy *policy = ctx->cc_sec->ps_policy;
1721
1722         if (!policy->sp_cops->install_rctx)
1723                 return 0;
1724         return policy->sp_cops->install_rctx(imp, ctx->cc_sec, ctx);
1725 }
1726
1727 int sptlrpc_svc_install_rvs_ctx(struct obd_import *imp,
1728                                 struct ptlrpc_svc_ctx *ctx)
1729 {
1730         struct ptlrpc_sec_policy *policy = ctx->sc_policy;
1731
1732         if (!policy->sp_sops->install_rctx)
1733                 return 0;
1734         return policy->sp_sops->install_rctx(imp, ctx);
1735 }
1736
1737 /****************************************
1738  * server side security                 *
1739  ****************************************/
1740
1741 static int flavor_allowed(struct sptlrpc_flavor *exp,
1742                           struct ptlrpc_request *req)
1743 {
1744         struct sptlrpc_flavor *flvr = &req->rq_flvr;
1745
1746         if (exp->sf_rpc == SPTLRPC_FLVR_ANY || exp->sf_rpc == flvr->sf_rpc)
1747                 return 1;
1748
1749         if ((req->rq_ctx_init || req->rq_ctx_fini) &&
1750             SPTLRPC_FLVR_POLICY(exp->sf_rpc) ==
1751             SPTLRPC_FLVR_POLICY(flvr->sf_rpc) &&
1752             SPTLRPC_FLVR_MECH(exp->sf_rpc) == SPTLRPC_FLVR_MECH(flvr->sf_rpc))
1753                 return 1;
1754
1755         return 0;
1756 }
1757
1758 #define EXP_FLVR_UPDATE_EXPIRE      (OBD_TIMEOUT_DEFAULT + 10)
1759
1760 /**
1761  * Given an export \a exp, check whether the flavor of incoming \a req
1762  * is allowed by the export \a exp. Main logic is about taking care of
1763  * changing configurations. Return 0 means success.
1764  */
1765 int sptlrpc_target_export_check(struct obd_export *exp,
1766                                 struct ptlrpc_request *req)
1767 {
1768         struct sptlrpc_flavor   flavor;
1769
1770         if (exp == NULL)
1771                 return 0;
1772
1773         /* client side export has no imp_reverse, skip
1774          * FIXME maybe we should check flavor this as well??? */
1775         if (exp->exp_imp_reverse == NULL)
1776                 return 0;
1777
1778         /* don't care about ctx fini rpc */
1779         if (req->rq_ctx_fini)
1780                 return 0;
1781
1782         cfs_spin_lock(&exp->exp_lock);
1783
1784         /* if flavor just changed (exp->exp_flvr_changed != 0), we wait for
1785          * the first req with the new flavor, then treat it as current flavor,
1786          * adapt reverse sec according to it.
1787          * note the first rpc with new flavor might not be with root ctx, in
1788          * which case delay the sec_adapt by leaving exp_flvr_adapt == 1. */
1789         if (unlikely(exp->exp_flvr_changed) &&
1790             flavor_allowed(&exp->exp_flvr_old[1], req)) {
1791                 /* make the new flavor as "current", and old ones as
1792                  * about-to-expire */
1793                 CDEBUG(D_SEC, "exp %p: just changed: %x->%x\n", exp,
1794                        exp->exp_flvr.sf_rpc, exp->exp_flvr_old[1].sf_rpc);
1795                 flavor = exp->exp_flvr_old[1];
1796                 exp->exp_flvr_old[1] = exp->exp_flvr_old[0];
1797                 exp->exp_flvr_expire[1] = exp->exp_flvr_expire[0];
1798                 exp->exp_flvr_old[0] = exp->exp_flvr;
1799                 exp->exp_flvr_expire[0] = cfs_time_current_sec() +
1800                                           EXP_FLVR_UPDATE_EXPIRE;
1801                 exp->exp_flvr = flavor;
1802
1803                 /* flavor change finished */
1804                 exp->exp_flvr_changed = 0;
1805                 LASSERT(exp->exp_flvr_adapt == 1);
1806
1807                 /* if it's gss, we only interested in root ctx init */
1808                 if (req->rq_auth_gss &&
1809                     !(req->rq_ctx_init &&
1810                       (req->rq_auth_usr_root || req->rq_auth_usr_mdt ||
1811                        req->rq_auth_usr_ost))) {
1812                         cfs_spin_unlock(&exp->exp_lock);
1813                         CDEBUG(D_SEC, "is good but not root(%d:%d:%d:%d:%d)\n",
1814                                req->rq_auth_gss, req->rq_ctx_init,
1815                                req->rq_auth_usr_root, req->rq_auth_usr_mdt,
1816                                req->rq_auth_usr_ost);
1817                         return 0;
1818                 }
1819
1820                 exp->exp_flvr_adapt = 0;
1821                 cfs_spin_unlock(&exp->exp_lock);
1822
1823                 return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1824                                                 req->rq_svc_ctx, &flavor);
1825         }
1826
1827         /* if it equals to the current flavor, we accept it, but need to
1828          * dealing with reverse sec/ctx */
1829         if (likely(flavor_allowed(&exp->exp_flvr, req))) {
1830                 /* most cases should return here, we only interested in
1831                  * gss root ctx init */
1832                 if (!req->rq_auth_gss || !req->rq_ctx_init ||
1833                     (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
1834                      !req->rq_auth_usr_ost)) {
1835                         cfs_spin_unlock(&exp->exp_lock);
1836                         return 0;
1837                 }
1838
1839                 /* if flavor just changed, we should not proceed, just leave
1840                  * it and current flavor will be discovered and replaced
1841                  * shortly, and let _this_ rpc pass through */
1842                 if (exp->exp_flvr_changed) {
1843                         LASSERT(exp->exp_flvr_adapt);
1844                         cfs_spin_unlock(&exp->exp_lock);
1845                         return 0;
1846                 }
1847
1848                 if (exp->exp_flvr_adapt) {
1849                         exp->exp_flvr_adapt = 0;
1850                         CDEBUG(D_SEC, "exp %p (%x|%x|%x): do delayed adapt\n",
1851                                exp, exp->exp_flvr.sf_rpc,
1852                                exp->exp_flvr_old[0].sf_rpc,
1853                                exp->exp_flvr_old[1].sf_rpc);
1854                         flavor = exp->exp_flvr;
1855                         cfs_spin_unlock(&exp->exp_lock);
1856
1857                         return sptlrpc_import_sec_adapt(exp->exp_imp_reverse,
1858                                                         req->rq_svc_ctx,
1859                                                         &flavor);
1860                 } else {
1861                         CDEBUG(D_SEC, "exp %p (%x|%x|%x): is current flavor, "
1862                                "install rvs ctx\n", exp, exp->exp_flvr.sf_rpc,
1863                                exp->exp_flvr_old[0].sf_rpc,
1864                                exp->exp_flvr_old[1].sf_rpc);
1865                         cfs_spin_unlock(&exp->exp_lock);
1866
1867                         return sptlrpc_svc_install_rvs_ctx(exp->exp_imp_reverse,
1868                                                            req->rq_svc_ctx);
1869                 }
1870         }
1871
1872         if (exp->exp_flvr_expire[0]) {
1873                 if (exp->exp_flvr_expire[0] >= cfs_time_current_sec()) {
1874                         if (flavor_allowed(&exp->exp_flvr_old[0], req)) {
1875                                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1876                                        "middle one ("CFS_DURATION_T")\n", exp,
1877                                        exp->exp_flvr.sf_rpc,
1878                                        exp->exp_flvr_old[0].sf_rpc,
1879                                        exp->exp_flvr_old[1].sf_rpc,
1880                                        exp->exp_flvr_expire[0] -
1881                                                 cfs_time_current_sec());
1882                                 cfs_spin_unlock(&exp->exp_lock);
1883                                 return 0;
1884                         }
1885                 } else {
1886                         CDEBUG(D_SEC, "mark middle expired\n");
1887                         exp->exp_flvr_expire[0] = 0;
1888                 }
1889                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match middle\n", exp,
1890                        exp->exp_flvr.sf_rpc,
1891                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1892                        req->rq_flvr.sf_rpc);
1893         }
1894
1895         /* now it doesn't match the current flavor, the only chance we can
1896          * accept it is match the old flavors which is not expired. */
1897         if (exp->exp_flvr_changed == 0 && exp->exp_flvr_expire[1]) {
1898                 if (exp->exp_flvr_expire[1] >= cfs_time_current_sec()) {
1899                         if (flavor_allowed(&exp->exp_flvr_old[1], req)) {
1900                                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): match the "
1901                                        "oldest one ("CFS_DURATION_T")\n", exp,
1902                                        exp->exp_flvr.sf_rpc,
1903                                        exp->exp_flvr_old[0].sf_rpc,
1904                                        exp->exp_flvr_old[1].sf_rpc,
1905                                        exp->exp_flvr_expire[1] -
1906                                                 cfs_time_current_sec());
1907                                 cfs_spin_unlock(&exp->exp_lock);
1908                                 return 0;
1909                         }
1910                 } else {
1911                         CDEBUG(D_SEC, "mark oldest expired\n");
1912                         exp->exp_flvr_expire[1] = 0;
1913                 }
1914                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): %x not match found\n",
1915                        exp, exp->exp_flvr.sf_rpc,
1916                        exp->exp_flvr_old[0].sf_rpc, exp->exp_flvr_old[1].sf_rpc,
1917                        req->rq_flvr.sf_rpc);
1918         } else {
1919                 CDEBUG(D_SEC, "exp %p (%x|%x|%x): skip the last one\n",
1920                        exp, exp->exp_flvr.sf_rpc, exp->exp_flvr_old[0].sf_rpc,
1921                        exp->exp_flvr_old[1].sf_rpc);
1922         }
1923
1924         cfs_spin_unlock(&exp->exp_lock);
1925
1926         CWARN("exp %p(%s): req %p (%u|%u|%u|%u|%u|%u) with "
1927               "unauthorized flavor %x, expect %x|%x(%+ld)|%x(%+ld)\n",
1928               exp, exp->exp_obd->obd_name,
1929               req, req->rq_auth_gss, req->rq_ctx_init, req->rq_ctx_fini,
1930               req->rq_auth_usr_root, req->rq_auth_usr_mdt, req->rq_auth_usr_ost,
1931               req->rq_flvr.sf_rpc,
1932               exp->exp_flvr.sf_rpc,
1933               exp->exp_flvr_old[0].sf_rpc,
1934               exp->exp_flvr_expire[0] ?
1935               (unsigned long) (exp->exp_flvr_expire[0] -
1936                                cfs_time_current_sec()) : 0,
1937               exp->exp_flvr_old[1].sf_rpc,
1938               exp->exp_flvr_expire[1] ?
1939               (unsigned long) (exp->exp_flvr_expire[1] -
1940                                cfs_time_current_sec()) : 0);
1941         return -EACCES;
1942 }
1943 EXPORT_SYMBOL(sptlrpc_target_export_check);
1944
1945 void sptlrpc_target_update_exp_flavor(struct obd_device *obd,
1946                                       struct sptlrpc_rule_set *rset)
1947 {
1948         struct obd_export       *exp;
1949         struct sptlrpc_flavor    new_flvr;
1950
1951         LASSERT(obd);
1952
1953         cfs_spin_lock(&obd->obd_dev_lock);
1954
1955         cfs_list_for_each_entry(exp, &obd->obd_exports, exp_obd_chain) {
1956                 if (exp->exp_connection == NULL)
1957                         continue;
1958
1959                 /* note if this export had just been updated flavor
1960                  * (exp_flvr_changed == 1), this will override the
1961                  * previous one. */
1962                 cfs_spin_lock(&exp->exp_lock);
1963                 sptlrpc_target_choose_flavor(rset, exp->exp_sp_peer,
1964                                              exp->exp_connection->c_peer.nid,
1965                                              &new_flvr);
1966                 if (exp->exp_flvr_changed ||
1967                     !flavor_equal(&new_flvr, &exp->exp_flvr)) {
1968                         exp->exp_flvr_old[1] = new_flvr;
1969                         exp->exp_flvr_expire[1] = 0;
1970                         exp->exp_flvr_changed = 1;
1971                         exp->exp_flvr_adapt = 1;
1972
1973                         CDEBUG(D_SEC, "exp %p (%s): updated flavor %x->%x\n",
1974                                exp, sptlrpc_part2name(exp->exp_sp_peer),
1975                                exp->exp_flvr.sf_rpc,
1976                                exp->exp_flvr_old[1].sf_rpc);
1977                 }
1978                 cfs_spin_unlock(&exp->exp_lock);
1979         }
1980
1981         cfs_spin_unlock(&obd->obd_dev_lock);
1982 }
1983 EXPORT_SYMBOL(sptlrpc_target_update_exp_flavor);
1984
1985 static int sptlrpc_svc_check_from(struct ptlrpc_request *req, int svc_rc)
1986 {
1987         /* peer's claim is unreliable unless gss is being used */
1988         if (!req->rq_auth_gss || svc_rc == SECSVC_DROP)
1989                 return svc_rc;
1990
1991         switch (req->rq_sp_from) {
1992         case LUSTRE_SP_CLI:
1993                 if (req->rq_auth_usr_mdt || req->rq_auth_usr_ost) {
1994                         DEBUG_REQ(D_ERROR, req, "faked source CLI");
1995                         svc_rc = SECSVC_DROP;
1996                 }
1997                 break;
1998         case LUSTRE_SP_MDT:
1999                 if (!req->rq_auth_usr_mdt) {
2000                         DEBUG_REQ(D_ERROR, req, "faked source MDT");
2001                         svc_rc = SECSVC_DROP;
2002                 }
2003                 break;
2004         case LUSTRE_SP_OST:
2005                 if (!req->rq_auth_usr_ost) {
2006                         DEBUG_REQ(D_ERROR, req, "faked source OST");
2007                         svc_rc = SECSVC_DROP;
2008                 }
2009                 break;
2010         case LUSTRE_SP_MGS:
2011         case LUSTRE_SP_MGC:
2012                 if (!req->rq_auth_usr_root && !req->rq_auth_usr_mdt &&
2013                     !req->rq_auth_usr_ost) {
2014                         DEBUG_REQ(D_ERROR, req, "faked source MGC/MGS");
2015                         svc_rc = SECSVC_DROP;
2016                 }
2017                 break;
2018         case LUSTRE_SP_ANY:
2019         default:
2020                 DEBUG_REQ(D_ERROR, req, "invalid source %u", req->rq_sp_from);
2021                 svc_rc = SECSVC_DROP;
2022         }
2023
2024         return svc_rc;
2025 }
2026
2027 /**
2028  * Used by ptlrpc server, to perform transformation upon request message of
2029  * incoming \a req. This must be the first thing to do with a incoming
2030  * request in ptlrpc layer.
2031  *
2032  * \retval SECSVC_OK success, and req->rq_reqmsg point to request message in
2033  * clear text, size is req->rq_reqlen; also req->rq_svc_ctx is set.
2034  * \retval SECSVC_COMPLETE success, the request has been fully processed, and
2035  * reply message has been prepared.
2036  * \retval SECSVC_DROP failed, this request should be dropped.
2037  */
2038 int sptlrpc_svc_unwrap_request(struct ptlrpc_request *req)
2039 {
2040         struct ptlrpc_sec_policy *policy;
2041         struct lustre_msg        *msg = req->rq_reqbuf;
2042         int                       rc;
2043         ENTRY;
2044
2045         LASSERT(msg);
2046         LASSERT(req->rq_reqmsg == NULL);
2047         LASSERT(req->rq_repmsg == NULL);
2048         LASSERT(req->rq_svc_ctx == NULL);
2049
2050         req->rq_req_swab_mask = 0;
2051
2052         rc = __lustre_unpack_msg(msg, req->rq_reqdata_len);
2053         switch (rc) {
2054         case 1:
2055                 lustre_set_req_swabbed(req, MSG_PTLRPC_HEADER_OFF);
2056         case 0:
2057                 break;
2058         default:
2059                 CERROR("error unpacking request from %s x"LPU64"\n",
2060                        libcfs_id2str(req->rq_peer), req->rq_xid);
2061                 RETURN(SECSVC_DROP);
2062         }
2063
2064         req->rq_flvr.sf_rpc = WIRE_FLVR(msg->lm_secflvr);
2065         req->rq_sp_from = LUSTRE_SP_ANY;
2066         req->rq_auth_uid = INVALID_UID;
2067         req->rq_auth_mapped_uid = INVALID_UID;
2068
2069         policy = sptlrpc_wireflavor2policy(req->rq_flvr.sf_rpc);
2070         if (!policy) {
2071                 CERROR("unsupported rpc flavor %x\n", req->rq_flvr.sf_rpc);
2072                 RETURN(SECSVC_DROP);
2073         }
2074
2075         LASSERT(policy->sp_sops->accept);
2076         rc = policy->sp_sops->accept(req);
2077         sptlrpc_policy_put(policy);
2078         LASSERT(req->rq_reqmsg || rc != SECSVC_OK);
2079         LASSERT(req->rq_svc_ctx || rc == SECSVC_DROP);
2080
2081         /*
2082          * if it's not null flavor (which means embedded packing msg),
2083          * reset the swab mask for the comming inner msg unpacking.
2084          */
2085         if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL)
2086                 req->rq_req_swab_mask = 0;
2087
2088         /* sanity check for the request source */
2089         rc = sptlrpc_svc_check_from(req, rc);
2090         RETURN(rc);
2091 }
2092
2093 /**
2094  * Used by ptlrpc server, to allocate reply buffer for \a req. If succeed,
2095  * req->rq_reply_state is set, and req->rq_reply_state->rs_msg point to
2096  * a buffer of \a msglen size.
2097  */
2098 int sptlrpc_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
2099 {
2100         struct ptlrpc_sec_policy *policy;
2101         struct ptlrpc_reply_state *rs;
2102         int rc;
2103         ENTRY;
2104
2105         LASSERT(req->rq_svc_ctx);
2106         LASSERT(req->rq_svc_ctx->sc_policy);
2107
2108         policy = req->rq_svc_ctx->sc_policy;
2109         LASSERT(policy->sp_sops->alloc_rs);
2110
2111         rc = policy->sp_sops->alloc_rs(req, msglen);
2112         if (unlikely(rc == -ENOMEM)) {
2113                 /* failed alloc, try emergency pool */
2114                 rs = lustre_get_emerg_rs(req->rq_rqbd->rqbd_service);
2115                 if (rs == NULL)
2116                         RETURN(-ENOMEM);
2117
2118                 req->rq_reply_state = rs;
2119                 rc = policy->sp_sops->alloc_rs(req, msglen);
2120                 if (rc) {
2121                         lustre_put_emerg_rs(rs);
2122                         req->rq_reply_state = NULL;
2123                 }
2124         }
2125
2126         LASSERT(rc != 0 ||
2127                 (req->rq_reply_state && req->rq_reply_state->rs_msg));
2128
2129         RETURN(rc);
2130 }
2131
2132 /**
2133  * Used by ptlrpc server, to perform transformation upon reply message.
2134  *
2135  * \post req->rq_reply_off is set to approriate server-controlled reply offset.
2136  * \post req->rq_repmsg and req->rq_reply_state->rs_msg becomes inaccessible.
2137  */
2138 int sptlrpc_svc_wrap_reply(struct ptlrpc_request *req)
2139 {
2140         struct ptlrpc_sec_policy *policy;
2141         int rc;
2142         ENTRY;
2143
2144         LASSERT(req->rq_svc_ctx);
2145         LASSERT(req->rq_svc_ctx->sc_policy);
2146
2147         policy = req->rq_svc_ctx->sc_policy;
2148         LASSERT(policy->sp_sops->authorize);
2149
2150         rc = policy->sp_sops->authorize(req);
2151         LASSERT(rc || req->rq_reply_state->rs_repdata_len);
2152
2153         RETURN(rc);
2154 }
2155
2156 /**
2157  * Used by ptlrpc server, to free reply_state.
2158  */
2159 void sptlrpc_svc_free_rs(struct ptlrpc_reply_state *rs)
2160 {
2161         struct ptlrpc_sec_policy *policy;
2162         unsigned int prealloc;
2163         ENTRY;
2164
2165         LASSERT(rs->rs_svc_ctx);
2166         LASSERT(rs->rs_svc_ctx->sc_policy);
2167
2168         policy = rs->rs_svc_ctx->sc_policy;
2169         LASSERT(policy->sp_sops->free_rs);
2170
2171         prealloc = rs->rs_prealloc;
2172         policy->sp_sops->free_rs(rs);
2173
2174         if (prealloc)
2175                 lustre_put_emerg_rs(rs);
2176         EXIT;
2177 }
2178
2179 void sptlrpc_svc_ctx_addref(struct ptlrpc_request *req)
2180 {
2181         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2182
2183         if (ctx != NULL)
2184                 cfs_atomic_inc(&ctx->sc_refcount);
2185 }
2186
2187 void sptlrpc_svc_ctx_decref(struct ptlrpc_request *req)
2188 {
2189         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2190
2191         if (ctx == NULL)
2192                 return;
2193
2194         LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2195         if (cfs_atomic_dec_and_test(&ctx->sc_refcount)) {
2196                 if (ctx->sc_policy->sp_sops->free_ctx)
2197                         ctx->sc_policy->sp_sops->free_ctx(ctx);
2198         }
2199         req->rq_svc_ctx = NULL;
2200 }
2201
2202 void sptlrpc_svc_ctx_invalidate(struct ptlrpc_request *req)
2203 {
2204         struct ptlrpc_svc_ctx *ctx = req->rq_svc_ctx;
2205
2206         if (ctx == NULL)
2207                 return;
2208
2209         LASSERT_ATOMIC_POS(&ctx->sc_refcount);
2210         if (ctx->sc_policy->sp_sops->invalidate_ctx)
2211                 ctx->sc_policy->sp_sops->invalidate_ctx(ctx);
2212 }
2213 EXPORT_SYMBOL(sptlrpc_svc_ctx_invalidate);
2214
2215 /****************************************
2216  * bulk security                        *
2217  ****************************************/
2218
2219 /**
2220  * Perform transformation upon bulk data pointed by \a desc. This is called
2221  * before transforming the request message.
2222  */
2223 int sptlrpc_cli_wrap_bulk(struct ptlrpc_request *req,
2224                           struct ptlrpc_bulk_desc *desc)
2225 {
2226         struct ptlrpc_cli_ctx *ctx;
2227
2228         LASSERT(req->rq_bulk_read || req->rq_bulk_write);
2229
2230         if (!req->rq_pack_bulk)
2231                 return 0;
2232
2233         ctx = req->rq_cli_ctx;
2234         if (ctx->cc_ops->wrap_bulk)
2235                 return ctx->cc_ops->wrap_bulk(ctx, req, desc);
2236         return 0;
2237 }
2238 EXPORT_SYMBOL(sptlrpc_cli_wrap_bulk);
2239
2240 /**
2241  * This is called after unwrap the reply message.
2242  * return nob of actual plain text size received, or error code.
2243  */
2244 int sptlrpc_cli_unwrap_bulk_read(struct ptlrpc_request *req,
2245                                  struct ptlrpc_bulk_desc *desc,
2246                                  int nob)
2247 {
2248         struct ptlrpc_cli_ctx  *ctx;
2249         int                     rc;
2250
2251         LASSERT(req->rq_bulk_read && !req->rq_bulk_write);
2252
2253         if (!req->rq_pack_bulk)
2254                 return desc->bd_nob_transferred;
2255
2256         ctx = req->rq_cli_ctx;
2257         if (ctx->cc_ops->unwrap_bulk) {
2258                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2259                 if (rc < 0)
2260                         return rc;
2261         }
2262         return desc->bd_nob_transferred;
2263 }
2264 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_read);
2265
2266 /**
2267  * This is called after unwrap the reply message.
2268  * return 0 for success or error code.
2269  */
2270 int sptlrpc_cli_unwrap_bulk_write(struct ptlrpc_request *req,
2271                                   struct ptlrpc_bulk_desc *desc)
2272 {
2273         struct ptlrpc_cli_ctx  *ctx;
2274         int                     rc;
2275
2276         LASSERT(!req->rq_bulk_read && req->rq_bulk_write);
2277
2278         if (!req->rq_pack_bulk)
2279                 return 0;
2280
2281         ctx = req->rq_cli_ctx;
2282         if (ctx->cc_ops->unwrap_bulk) {
2283                 rc = ctx->cc_ops->unwrap_bulk(ctx, req, desc);
2284                 if (rc < 0)
2285                         return rc;
2286         }
2287
2288         /*
2289          * if everything is going right, nob should equals to nob_transferred.
2290          * in case of privacy mode, nob_transferred needs to be adjusted.
2291          */
2292         if (desc->bd_nob != desc->bd_nob_transferred) {
2293                 CERROR("nob %d doesn't match transferred nob %d",
2294                        desc->bd_nob, desc->bd_nob_transferred);
2295                 return -EPROTO;
2296         }
2297
2298         return 0;
2299 }
2300 EXPORT_SYMBOL(sptlrpc_cli_unwrap_bulk_write);
2301
2302 /**
2303  * Performe transformation upon outgoing bulk read.
2304  */
2305 int sptlrpc_svc_wrap_bulk(struct ptlrpc_request *req,
2306                           struct ptlrpc_bulk_desc *desc)
2307 {
2308         struct ptlrpc_svc_ctx *ctx;
2309
2310         LASSERT(req->rq_bulk_read);
2311
2312         if (!req->rq_pack_bulk)
2313                 return 0;
2314
2315         ctx = req->rq_svc_ctx;
2316         if (ctx->sc_policy->sp_sops->wrap_bulk)
2317                 return ctx->sc_policy->sp_sops->wrap_bulk(req, desc);
2318
2319         return 0;
2320 }
2321 EXPORT_SYMBOL(sptlrpc_svc_wrap_bulk);
2322
2323 /**
2324  * Performe transformation upon incoming bulk write.
2325  */
2326 int sptlrpc_svc_unwrap_bulk(struct ptlrpc_request *req,
2327                             struct ptlrpc_bulk_desc *desc)
2328 {
2329         struct ptlrpc_svc_ctx *ctx;
2330         int                    rc;
2331
2332         LASSERT(req->rq_bulk_write);
2333
2334         /*
2335          * if it's in privacy mode, transferred should >= expected; otherwise
2336          * transferred should == expected.
2337          */
2338         if (desc->bd_nob_transferred < desc->bd_nob ||
2339             (desc->bd_nob_transferred > desc->bd_nob &&
2340              SPTLRPC_FLVR_BULK_SVC(req->rq_flvr.sf_rpc) !=
2341              SPTLRPC_BULK_SVC_PRIV)) {
2342                 DEBUG_REQ(D_ERROR, req, "truncated bulk GET %d(%d)",
2343                           desc->bd_nob_transferred, desc->bd_nob);
2344                 return -ETIMEDOUT;
2345         }
2346
2347         if (!req->rq_pack_bulk)
2348                 return 0;
2349
2350         ctx = req->rq_svc_ctx;
2351         if (ctx->sc_policy->sp_sops->unwrap_bulk) {
2352                 rc = ctx->sc_policy->sp_sops->unwrap_bulk(req, desc);
2353                 if (rc)
2354                         CERROR("error unwrap bulk: %d\n", rc);
2355         }
2356
2357         /* return 0 to allow reply be sent */
2358         return 0;
2359 }
2360 EXPORT_SYMBOL(sptlrpc_svc_unwrap_bulk);
2361
2362 /**
2363  * Prepare buffers for incoming bulk write.
2364  */
2365 int sptlrpc_svc_prep_bulk(struct ptlrpc_request *req,
2366                           struct ptlrpc_bulk_desc *desc)
2367 {
2368         struct ptlrpc_svc_ctx *ctx;
2369
2370         LASSERT(req->rq_bulk_write);
2371
2372         if (!req->rq_pack_bulk)
2373                 return 0;
2374
2375         ctx = req->rq_svc_ctx;
2376         if (ctx->sc_policy->sp_sops->prep_bulk)
2377                 return ctx->sc_policy->sp_sops->prep_bulk(req, desc);
2378
2379         return 0;
2380 }
2381 EXPORT_SYMBOL(sptlrpc_svc_prep_bulk);
2382
2383 /****************************************
2384  * user descriptor helpers              *
2385  ****************************************/
2386
2387 int sptlrpc_current_user_desc_size(void)
2388 {
2389         int ngroups;
2390
2391 #ifdef __KERNEL__
2392         ngroups = current_ngroups;
2393
2394         if (ngroups > LUSTRE_MAX_GROUPS)
2395                 ngroups = LUSTRE_MAX_GROUPS;
2396 #else
2397         ngroups = 0;
2398 #endif
2399         return sptlrpc_user_desc_size(ngroups);
2400 }
2401 EXPORT_SYMBOL(sptlrpc_current_user_desc_size);
2402
2403 int sptlrpc_pack_user_desc(struct lustre_msg *msg, int offset)
2404 {
2405         struct ptlrpc_user_desc *pud;
2406
2407         pud = lustre_msg_buf(msg, offset, 0);
2408
2409         pud->pud_uid = cfs_curproc_uid();
2410         pud->pud_gid = cfs_curproc_gid();
2411         pud->pud_fsuid = cfs_curproc_fsuid();
2412         pud->pud_fsgid = cfs_curproc_fsgid();
2413         pud->pud_cap = cfs_curproc_cap_pack();
2414         pud->pud_ngroups = (msg->lm_buflens[offset] - sizeof(*pud)) / 4;
2415
2416 #ifdef __KERNEL__
2417         task_lock(current);
2418         if (pud->pud_ngroups > current_ngroups)
2419                 pud->pud_ngroups = current_ngroups;
2420         memcpy(pud->pud_groups, current_cred()->group_info->blocks[0],
2421                pud->pud_ngroups * sizeof(__u32));
2422         task_unlock(current);
2423 #endif
2424
2425         return 0;
2426 }
2427 EXPORT_SYMBOL(sptlrpc_pack_user_desc);
2428
2429 int sptlrpc_unpack_user_desc(struct lustre_msg *msg, int offset, int swabbed)
2430 {
2431         struct ptlrpc_user_desc *pud;
2432         int                      i;
2433
2434         pud = lustre_msg_buf(msg, offset, sizeof(*pud));
2435         if (!pud)
2436                 return -EINVAL;
2437
2438         if (swabbed) {
2439                 __swab32s(&pud->pud_uid);
2440                 __swab32s(&pud->pud_gid);
2441                 __swab32s(&pud->pud_fsuid);
2442                 __swab32s(&pud->pud_fsgid);
2443                 __swab32s(&pud->pud_cap);
2444                 __swab32s(&pud->pud_ngroups);
2445         }
2446
2447         if (pud->pud_ngroups > LUSTRE_MAX_GROUPS) {
2448                 CERROR("%u groups is too large\n", pud->pud_ngroups);
2449                 return -EINVAL;
2450         }
2451
2452         if (sizeof(*pud) + pud->pud_ngroups * sizeof(__u32) >
2453             msg->lm_buflens[offset]) {
2454                 CERROR("%u groups are claimed but bufsize only %u\n",
2455                        pud->pud_ngroups, msg->lm_buflens[offset]);
2456                 return -EINVAL;
2457         }
2458
2459         if (swabbed) {
2460                 for (i = 0; i < pud->pud_ngroups; i++)
2461                         __swab32s(&pud->pud_groups[i]);
2462         }
2463
2464         return 0;
2465 }
2466 EXPORT_SYMBOL(sptlrpc_unpack_user_desc);
2467
2468 /****************************************
2469  * misc helpers                         *
2470  ****************************************/
2471
2472 const char * sec2target_str(struct ptlrpc_sec *sec)
2473 {
2474         if (!sec || !sec->ps_import || !sec->ps_import->imp_obd)
2475                 return "*";
2476         if (sec_is_reverse(sec))
2477                 return "c";
2478         return obd_uuid2str(&sec->ps_import->imp_obd->u.cli.cl_target_uuid);
2479 }
2480 EXPORT_SYMBOL(sec2target_str);
2481
2482 /*
2483  * return true if the bulk data is protected
2484  */
2485 int sptlrpc_flavor_has_bulk(struct sptlrpc_flavor *flvr)
2486 {
2487         switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
2488         case SPTLRPC_BULK_SVC_INTG:
2489         case SPTLRPC_BULK_SVC_PRIV:
2490                 return 1;
2491         default:
2492                 return 0;
2493         }
2494 }
2495 EXPORT_SYMBOL(sptlrpc_flavor_has_bulk);
2496
2497 /****************************************
2498  * crypto API helper/alloc blkciper     *
2499  ****************************************/
2500
2501 /****************************************
2502  * initialize/finalize                  *
2503  ****************************************/
2504
2505 int sptlrpc_init(void)
2506 {
2507         int rc;
2508
2509         cfs_rwlock_init(&policy_lock);
2510
2511         rc = sptlrpc_gc_init();
2512         if (rc)
2513                 goto out;
2514
2515         rc = sptlrpc_conf_init();
2516         if (rc)
2517                 goto out_gc;
2518
2519         rc = sptlrpc_enc_pool_init();
2520         if (rc)
2521                 goto out_conf;
2522
2523         rc = sptlrpc_null_init();
2524         if (rc)
2525                 goto out_pool;
2526
2527         rc = sptlrpc_plain_init();
2528         if (rc)
2529                 goto out_null;
2530
2531         rc = sptlrpc_lproc_init();
2532         if (rc)
2533                 goto out_plain;
2534
2535         return 0;
2536
2537 out_plain:
2538         sptlrpc_plain_fini();
2539 out_null:
2540         sptlrpc_null_fini();
2541 out_pool:
2542         sptlrpc_enc_pool_fini();
2543 out_conf:
2544         sptlrpc_conf_fini();
2545 out_gc:
2546         sptlrpc_gc_fini();
2547 out:
2548         return rc;
2549 }
2550
2551 void sptlrpc_fini(void)
2552 {
2553         sptlrpc_lproc_fini();
2554         sptlrpc_plain_fini();
2555         sptlrpc_null_fini();
2556         sptlrpc_enc_pool_fini();
2557         sptlrpc_conf_fini();
2558         sptlrpc_gc_fini();
2559 }