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