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