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