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