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