Whamcloud - gitweb
LU-9086 gss: handle specific mgc binding
[fs/lustre-release.git] / lustre / ptlrpc / sec_config.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_SEC
34
35 #include <libcfs/libcfs.h>
36 #include <linux/crypto.h>
37 #include <linux/key.h>
38
39 #include <obd.h>
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <lustre_net.h>
43 #include <lustre_import.h>
44 #include <lustre_log.h>
45 #include <lustre_disk.h>
46 #include <lustre_dlm.h>
47 #include <lustre_param.h>
48 #include <lustre_sec.h>
49
50 #include "ptlrpc_internal.h"
51
52 const char *sptlrpc_part2name(enum lustre_sec_part part)
53 {
54         switch (part) {
55         case LUSTRE_SP_CLI:
56                 return "cli";
57         case LUSTRE_SP_MDT:
58                 return "mdt";
59         case LUSTRE_SP_OST:
60                 return "ost";
61         case LUSTRE_SP_MGC:
62                 return "mgc";
63         case LUSTRE_SP_MGS:
64                 return "mgs";
65         case LUSTRE_SP_ANY:
66                 return "any";
67         default:
68                 return "err";
69         }
70 }
71 EXPORT_SYMBOL(sptlrpc_part2name);
72
73 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
74 {
75         const char *type = obd->obd_type->typ_name;
76
77         if (!strcmp(type, LUSTRE_MDT_NAME))
78                 return LUSTRE_SP_MDT;
79         if (!strcmp(type, LUSTRE_OST_NAME))
80                 return LUSTRE_SP_OST;
81         if (!strcmp(type, LUSTRE_MGS_NAME))
82                 return LUSTRE_SP_MGS;
83
84         CERROR("unknown target %p(%s)\n", obd, type);
85         return LUSTRE_SP_ANY;
86 }
87
88 /****************************************
89  * user supplied flavor string parsing  *
90  ****************************************/
91
92 /*
93  * format: <base_flavor>[-<bulk_type:alg_spec>]
94  */
95 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
96 {
97         char            buf[32];
98         char           *bulk, *alg;
99
100         memset(flvr, 0, sizeof(*flvr));
101
102         if (str == NULL || str[0] == '\0') {
103                 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
104                 return 0;
105         }
106
107         strlcpy(buf, str, sizeof(buf));
108
109         bulk = strchr(buf, '-');
110         if (bulk)
111                 *bulk++ = '\0';
112
113         flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
114         if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
115                 goto err_out;
116
117         /*
118          * currently only base flavor "plain" can have bulk specification.
119          */
120         if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
121                 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
122                 if (bulk) {
123                         /*
124                          * format: plain-hash:<hash_alg>
125                          */
126                         alg = strchr(bulk, ':');
127                         if (alg == NULL)
128                                 goto err_out;
129                         *alg++ = '\0';
130
131                         if (strcmp(bulk, "hash"))
132                                 goto err_out;
133
134                         flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
135                         if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
136                                 goto err_out;
137                 }
138
139                 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
140                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
141                 else
142                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
143         } else {
144                 if (bulk)
145                         goto err_out;
146         }
147
148         flvr->sf_flags = 0;
149         return 0;
150
151 err_out:
152         CERROR("invalid flavor string: %s\n", str);
153         return -EINVAL;
154 }
155 EXPORT_SYMBOL(sptlrpc_parse_flavor);
156
157 /****************************************
158  * configure rules                      *
159  ****************************************/
160
161 static void get_default_flavor(struct sptlrpc_flavor *sf)
162 {
163         memset(sf, 0, sizeof(*sf));
164
165         sf->sf_rpc = SPTLRPC_FLVR_NULL;
166         sf->sf_flags = 0;
167 }
168
169 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
170 {
171         rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
172         rule->sr_from = LUSTRE_SP_ANY;
173         rule->sr_to = LUSTRE_SP_ANY;
174         rule->sr_padding = 0;
175
176         get_default_flavor(&rule->sr_flvr);
177 }
178
179 /*
180  * format: network[.direction]=flavor
181  */
182 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
183 {
184         char           *flavor, *dir;
185         int             rc;
186
187         sptlrpc_rule_init(rule);
188
189         flavor = strchr(param, '=');
190         if (flavor == NULL) {
191                 CERROR("invalid param, no '='\n");
192                 RETURN(-EINVAL);
193         }
194         *flavor++ = '\0';
195
196         dir = strchr(param, '.');
197         if (dir)
198                 *dir++ = '\0';
199
200         /* 1.1 network */
201         if (strcmp(param, "default")) {
202                 rule->sr_netid = libcfs_str2net(param);
203                 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
204                         CERROR("invalid network name: %s\n", param);
205                         RETURN(-EINVAL);
206                 }
207         }
208
209         /* 1.2 direction */
210         if (dir) {
211                 if (!strcmp(dir, "mdt2ost")) {
212                         rule->sr_from = LUSTRE_SP_MDT;
213                         rule->sr_to = LUSTRE_SP_OST;
214                 } else if (!strcmp(dir, "mdt2mdt")) {
215                         rule->sr_from = LUSTRE_SP_MDT;
216                         rule->sr_to = LUSTRE_SP_MDT;
217                 } else if (!strcmp(dir, "cli2ost")) {
218                         rule->sr_from = LUSTRE_SP_CLI;
219                         rule->sr_to = LUSTRE_SP_OST;
220                 } else if (!strcmp(dir, "cli2mdt")) {
221                         rule->sr_from = LUSTRE_SP_CLI;
222                         rule->sr_to = LUSTRE_SP_MDT;
223                 } else {
224                         CERROR("invalid rule dir segment: %s\n", dir);
225                         RETURN(-EINVAL);
226                 }
227         }
228
229         /* 2.1 flavor */
230         rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
231         if (rc)
232                 RETURN(-EINVAL);
233
234         RETURN(0);
235 }
236 EXPORT_SYMBOL(sptlrpc_parse_rule);
237
238 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
239 {
240         LASSERT(rset->srs_nslot ||
241                 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
242
243         if (rset->srs_nslot) {
244                 OBD_FREE(rset->srs_rules,
245                          rset->srs_nslot * sizeof(*rset->srs_rules));
246                 sptlrpc_rule_set_init(rset);
247         }
248 }
249 EXPORT_SYMBOL(sptlrpc_rule_set_free);
250
251 /*
252  * return 0 if the rule set could accomodate one more rule.
253  */
254 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
255 {
256         struct sptlrpc_rule *rules;
257         int nslot;
258
259         might_sleep();
260
261         if (rset->srs_nrule < rset->srs_nslot)
262                 return 0;
263
264         nslot = rset->srs_nslot + 8;
265
266         /* better use realloc() if available */
267         OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
268         if (rules == NULL)
269                 return -ENOMEM;
270
271         if (rset->srs_nrule) {
272                 LASSERT(rset->srs_nslot && rset->srs_rules);
273                 memcpy(rules, rset->srs_rules,
274                        rset->srs_nrule * sizeof(*rset->srs_rules));
275
276                 OBD_FREE(rset->srs_rules,
277                          rset->srs_nslot * sizeof(*rset->srs_rules));
278         }
279
280         rset->srs_rules = rules;
281         rset->srs_nslot = nslot;
282         return 0;
283 }
284
285 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
286 {
287         return (rule->sr_from != LUSTRE_SP_ANY ||
288                 rule->sr_to != LUSTRE_SP_ANY);
289 }
290 static inline int rule_spec_net(struct sptlrpc_rule *rule)
291 {
292         return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
293 }
294 static inline int rule_match_dir(struct sptlrpc_rule *r1,
295                                  struct sptlrpc_rule *r2)
296 {
297         return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
298 }
299 static inline int rule_match_net(struct sptlrpc_rule *r1,
300                                  struct sptlrpc_rule *r2)
301 {
302         return (r1->sr_netid == r2->sr_netid);
303 }
304
305 /*
306  * merge @rule into @rset.
307  * the @rset slots might be expanded.
308  */
309 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset, 
310                            struct sptlrpc_rule *rule)
311 {
312         struct sptlrpc_rule      *p = rset->srs_rules;
313         int                       spec_dir, spec_net;
314         int                       rc, n, match = 0;
315
316         might_sleep();
317
318         spec_net = rule_spec_net(rule);
319         spec_dir = rule_spec_dir(rule);
320
321         for (n = 0; n < rset->srs_nrule; n++) {
322                 p = &rset->srs_rules[n]; 
323
324                 /* test network match, if failed:
325                  * - spec rule: skip rules which is also spec rule match, until
326                  *   we hit a wild rule, which means no more chance
327                  * - wild rule: skip until reach the one which is also wild
328                  *   and matches
329                  */
330                 if (!rule_match_net(p, rule)) {
331                         if (spec_net) {
332                                 if (rule_spec_net(p))
333                                         continue;
334                                 else
335                                         break;
336                         } else {
337                                 continue;
338                         }
339                 }
340
341                 /* test dir match, same logic as net matching */
342                 if (!rule_match_dir(p, rule)) {
343                         if (spec_dir) {
344                                 if (rule_spec_dir(p))
345                                         continue;
346                                 else
347                                         break;
348                         } else {
349                                 continue;
350                         }
351                 }
352
353                 /* find a match */
354                 match = 1;
355                 break;
356         }
357
358         if (match) {
359                 LASSERT(n >= 0 && n < rset->srs_nrule);
360
361                 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
362                         /* remove this rule */
363                         if (n < rset->srs_nrule - 1)
364                                 memmove(&rset->srs_rules[n],
365                                         &rset->srs_rules[n + 1],
366                                         (rset->srs_nrule - n - 1) *
367                                         sizeof(*rule));
368                         rset->srs_nrule--;
369                 } else {
370                         /* override the rule */
371                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
372                 }
373         } else {
374                 LASSERT(n >= 0 && n <= rset->srs_nrule);
375
376                 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
377                         rc = sptlrpc_rule_set_expand(rset);
378                         if (rc)
379                                 return rc;
380
381                         if (n < rset->srs_nrule)
382                                 memmove(&rset->srs_rules[n + 1],
383                                         &rset->srs_rules[n],
384                                         (rset->srs_nrule - n) * sizeof(*rule));
385                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
386                         rset->srs_nrule++;
387                 } else {
388                         CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
389                 }
390         }
391
392         return 0;
393 }
394 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
395
396 /**
397  * given from/to/nid, determine a matching flavor in ruleset.
398  * return 1 if a match found, otherwise return 0.
399  */
400 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
401                             enum lustre_sec_part from,
402                             enum lustre_sec_part to,
403                             lnet_nid_t nid,
404                             struct sptlrpc_flavor *sf)
405 {
406         struct sptlrpc_rule    *r;
407         int                     n;
408
409         for (n = 0; n < rset->srs_nrule; n++) {
410                 r = &rset->srs_rules[n];
411
412                 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
413                     r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
414                     LNET_NIDNET(nid) != r->sr_netid)
415                         continue;
416
417                 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
418                     from != r->sr_from)
419                         continue;
420
421                 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
422                     to != r->sr_to)
423                         continue;
424
425                 *sf = r->sr_flvr;
426                 return 1;
427         }
428
429         return 0;
430 }
431
432 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
433 {
434         struct sptlrpc_rule *r;
435         int     n;
436
437         for (n = 0; n < rset->srs_nrule; n++) {
438                 r = &rset->srs_rules[n];
439                 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
440                        r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
441         }
442 }
443
444 static int sptlrpc_rule_set_extract(struct sptlrpc_rule_set *gen,
445                                     struct sptlrpc_rule_set *tgt,
446                                     enum lustre_sec_part from,
447                                     enum lustre_sec_part to,
448                                     struct sptlrpc_rule_set *rset)
449 {
450         struct sptlrpc_rule_set *src[2] = { gen, tgt };
451         struct sptlrpc_rule     *rule;
452         int                      i, n, rc;
453
454         might_sleep();
455
456         /* merge general rules firstly, then target-specific rules */
457         for (i = 0; i < 2; i++) {
458                 if (src[i] == NULL)
459                         continue;
460
461                 for (n = 0; n < src[i]->srs_nrule; n++) {
462                         rule = &src[i]->srs_rules[n];
463
464                         if (from != LUSTRE_SP_ANY &&
465                             rule->sr_from != LUSTRE_SP_ANY &&
466                             rule->sr_from != from)
467                                 continue;
468                         if (to != LUSTRE_SP_ANY &&
469                             rule->sr_to != LUSTRE_SP_ANY &&
470                             rule->sr_to != to)
471                                 continue;
472
473                         rc = sptlrpc_rule_set_merge(rset, rule);
474                         if (rc) {
475                                 CERROR("can't merge: %d\n", rc);
476                                 return rc;
477                         }
478                 }
479         }
480
481         return 0;
482 }
483
484 /**********************************
485  * sptlrpc configuration support  *
486  **********************************/
487
488 struct sptlrpc_conf_tgt {
489         struct list_head              sct_list;
490         char                    sct_name[MAX_OBD_NAME];
491         struct sptlrpc_rule_set sct_rset;
492 };
493
494 struct sptlrpc_conf {
495         struct list_head        sc_list;
496         char                    sc_fsname[MTI_NAME_MAXLEN];
497         unsigned int            sc_modified;    /* modified during updating */
498         unsigned int            sc_updated:1,   /* updated copy from MGS */
499                                 sc_local:1;     /* local copy from target */
500         struct sptlrpc_rule_set sc_rset;        /* fs general rules */
501         struct list_head        sc_tgts;        /* target-specific rules */
502 };
503
504 static struct mutex sptlrpc_conf_lock;
505 static struct list_head sptlrpc_confs;
506
507 /*
508  * The goal of this function is to extract the file system name
509  * from the obd name. This can come in two flavors. One is
510  * fsname-MDTXXXX or fsname-XXXXXXX were X is a hexadecimal
511  * number. In both cases we should be returned fsname. If it is
512  * not a valid obd name it is assumed to be the file system
513  * name itself.
514  */
515 static void obdname2fsname(const char *tgt, char *fsname, size_t buflen)
516 {
517         const char *ptr;
518         size_t len;
519
520         ptr = strrchr(tgt, '-');
521         if (ptr) {
522                 if ((!strncmp(ptr, "-MDT", 4) ||
523                      !strncmp(ptr, "-OST", 4)) &&
524                      (isxdigit(ptr[4]) && isxdigit(ptr[5]) &&
525                       isxdigit(ptr[6]) && isxdigit(ptr[7])))
526                         goto valid_obd_name;
527
528                 /* The length of the obdname can vary on different platforms */
529                 len = strlen(ptr);
530                 while (--len) {
531                         if (!isxdigit(ptr[len])) {
532                                 ptr = NULL;
533                                 break;
534                         }
535                 }
536         }
537
538 valid_obd_name:
539         /* if we didn't find the pattern, treat the whole string as fsname */
540         if (ptr == NULL)
541                 len = strlen(tgt);
542         else
543                 len = ptr - tgt;
544
545         len = min(len, buflen - 1);
546         memcpy(fsname, tgt, len);
547         fsname[len] = '\0';
548 }
549
550 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
551 {
552         struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
553
554         sptlrpc_rule_set_free(&conf->sc_rset);
555
556         list_for_each_entry_safe(conf_tgt, conf_tgt_next,
557                                  &conf->sc_tgts, sct_list) {
558                 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
559                 list_del(&conf_tgt->sct_list);
560                 OBD_FREE_PTR(conf_tgt);
561         }
562         LASSERT(list_empty(&conf->sc_tgts));
563
564         conf->sc_updated = 0;
565         conf->sc_local = 0;
566 }
567
568 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
569 {
570         CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
571
572         sptlrpc_conf_free_rsets(conf);
573         list_del(&conf->sc_list);
574         OBD_FREE_PTR(conf);
575 }
576
577 static
578 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
579                                               const char *name,
580                                               int create)
581 {
582         struct sptlrpc_conf_tgt *conf_tgt;
583
584         list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
585                 if (strcmp(conf_tgt->sct_name, name) == 0)
586                         return conf_tgt;
587         }
588
589         if (!create)
590                 return NULL;
591
592         OBD_ALLOC_PTR(conf_tgt);
593         if (conf_tgt) {
594                 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
595                 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
596                 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
597         }
598
599         return conf_tgt;
600 }
601
602 static
603 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
604                                       int create)
605 {
606         struct sptlrpc_conf *conf;
607
608         list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
609                 if (strcmp(conf->sc_fsname, fsname) == 0)
610                         return conf;
611         }
612
613         if (!create)
614                 return NULL;
615
616         OBD_ALLOC_PTR(conf);
617         if (conf == NULL)
618                 return NULL;
619
620         if (strlcpy(conf->sc_fsname, fsname, sizeof(conf->sc_fsname)) >=
621             sizeof(conf->sc_fsname)) {
622                 OBD_FREE_PTR(conf);
623                 return NULL;
624         }
625         sptlrpc_rule_set_init(&conf->sc_rset);
626         INIT_LIST_HEAD(&conf->sc_tgts);
627         list_add(&conf->sc_list, &sptlrpc_confs);
628
629         CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
630         return conf;
631 }
632
633 /**
634  * caller must hold conf_lock already.
635  */
636 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
637                                    const char *target,
638                                    struct sptlrpc_rule *rule)
639 {
640         struct sptlrpc_conf_tgt  *conf_tgt;
641         struct sptlrpc_rule_set  *rule_set;
642
643         /* fsname == target means general rules for the whole fs */
644         if (strcmp(conf->sc_fsname, target) == 0) {
645                 rule_set = &conf->sc_rset;
646         } else {
647                 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
648                 if (conf_tgt) {
649                         rule_set = &conf_tgt->sct_rset;
650                 } else {
651                         CERROR("out of memory, can't merge rule!\n");
652                         return -ENOMEM;
653                 }
654         }
655
656         return sptlrpc_rule_set_merge(rule_set, rule);
657 }
658
659 /**
660  * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
661  * find one through the target name in the record inside conf_lock;
662  * otherwise means caller already hold conf_lock.
663  */
664 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
665                                     struct sptlrpc_conf *conf)
666 {
667         char                   *target, *param;
668         char                    fsname[MTI_NAME_MAXLEN];
669         struct sptlrpc_rule     rule;
670         int                     rc;
671         ENTRY;
672
673         target = lustre_cfg_string(lcfg, 1);
674         if (target == NULL) {
675                 CERROR("missing target name\n");
676                 RETURN(-EINVAL);
677         }
678
679         param = lustre_cfg_string(lcfg, 2);
680         if (param == NULL) {
681                 CERROR("missing parameter\n");
682                 RETURN(-EINVAL);
683         }
684
685         CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
686
687         /* parse rule to make sure the format is correct */
688         if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
689                 CERROR("Invalid sptlrpc parameter: %s\n", param);
690                 RETURN(-EINVAL);
691         }
692         param += sizeof(PARAM_SRPC_FLVR) - 1;
693
694         rc = sptlrpc_parse_rule(param, &rule);
695         if (rc)
696                 RETURN(-EINVAL);
697
698         if (conf == NULL) {
699                 obdname2fsname(target, fsname, sizeof(fsname));
700
701                 mutex_lock(&sptlrpc_conf_lock);
702                 conf = sptlrpc_conf_get(fsname, 0);
703                 if (conf == NULL) {
704                         CERROR("can't find conf\n");
705                         rc = -ENOMEM;
706                 } else {
707                         rc = sptlrpc_conf_merge_rule(conf, target, &rule);
708                 }
709                 mutex_unlock(&sptlrpc_conf_lock);
710         } else {
711                 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
712                 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
713         }
714
715         if (rc == 0)
716                 conf->sc_modified++;
717
718         RETURN(rc);
719 }
720
721 int sptlrpc_process_config(struct lustre_cfg *lcfg)
722 {
723         return __sptlrpc_process_config(lcfg, NULL);
724 }
725 EXPORT_SYMBOL(sptlrpc_process_config);
726
727 static int logname2fsname(const char *logname, char *buf, int buflen)
728 {
729         char   *ptr;
730         int     len;
731
732         ptr = strrchr(logname, '-');
733         if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
734                 CERROR("%s is not a sptlrpc config log\n", logname);
735                 return -EINVAL;
736         }
737
738         len = min((int) (ptr - logname), buflen - 1);
739
740         memcpy(buf, logname, len);
741         buf[len] = '\0';
742         return 0;
743 }
744
745 void sptlrpc_conf_log_update_begin(const char *logname)
746 {
747         struct sptlrpc_conf *conf;
748         char                 fsname[16];
749
750         if (logname2fsname(logname, fsname, sizeof(fsname)))
751                 return;
752
753         mutex_lock(&sptlrpc_conf_lock);
754
755         conf = sptlrpc_conf_get(fsname, 0);
756         if (conf) {
757                 if (conf->sc_local) {
758                         LASSERT(conf->sc_updated == 0);
759                         sptlrpc_conf_free_rsets(conf);
760                 }
761                 conf->sc_modified = 0;
762         }
763
764         mutex_unlock(&sptlrpc_conf_lock);
765 }
766 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
767
768 /**
769  * mark a config log has been updated
770  */
771 void sptlrpc_conf_log_update_end(const char *logname)
772 {
773         struct sptlrpc_conf *conf;
774         char                 fsname[16];
775
776         if (logname2fsname(logname, fsname, sizeof(fsname)))
777                 return;
778
779         mutex_lock(&sptlrpc_conf_lock);
780
781         conf = sptlrpc_conf_get(fsname, 0);
782         if (conf) {
783                 /*
784                  * if original state is not updated, make sure the
785                  * modified counter > 0 to enforce updating local copy.
786                  */
787                 if (conf->sc_updated == 0)
788                         conf->sc_modified++;
789
790                 conf->sc_updated = 1;
791         }
792
793         mutex_unlock(&sptlrpc_conf_lock);
794 }
795 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
796
797 void sptlrpc_conf_log_start(const char *logname)
798 {
799         char                 fsname[16];
800
801         if (logname2fsname(logname, fsname, sizeof(fsname)))
802                 return;
803
804         mutex_lock(&sptlrpc_conf_lock);
805         sptlrpc_conf_get(fsname, 1);
806         mutex_unlock(&sptlrpc_conf_lock);
807 }
808 EXPORT_SYMBOL(sptlrpc_conf_log_start);
809
810 void sptlrpc_conf_log_stop(const char *logname)
811 {
812         struct sptlrpc_conf *conf;
813         char                 fsname[16];
814
815         if (logname2fsname(logname, fsname, sizeof(fsname)))
816                 return;
817
818         mutex_lock(&sptlrpc_conf_lock);
819         conf = sptlrpc_conf_get(fsname, 0);
820         if (conf)
821                 sptlrpc_conf_free(conf);
822         mutex_unlock(&sptlrpc_conf_lock);
823 }
824 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
825
826 static void inline flavor_set_flags(struct sptlrpc_flavor *sf,
827                                     enum lustre_sec_part from,
828                                     enum lustre_sec_part to,
829                                     unsigned int fl_udesc)
830 {
831         /*
832          * null flavor doesn't need to set any flavor, and in fact
833          * we'd better not do that because everybody share a single sec.
834          */
835         if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
836                 return;
837
838         if (from == LUSTRE_SP_MDT) {
839                 /* MDT->MDT; MDT->OST */
840                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
841         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
842                 /* CLI->OST */
843                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
844         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
845                 /* CLI->MDT */
846                 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
847                         sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
848         }
849
850         /* Some flavors use a single uid (0) context */
851         if (flvr_is_rootonly(sf->sf_rpc))
852                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
853
854         /* User descriptor might need to be cleared */
855         if (flvr_allows_user_desc(sf->sf_rpc) == 0)
856                 sf->sf_flags &= ~PTLRPC_SEC_FL_UDESC;
857 }
858
859 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
860                                 enum lustre_sec_part to,
861                                 struct obd_uuid *target,
862                                 lnet_nid_t nid,
863                                 struct sptlrpc_flavor *sf)
864 {
865         struct sptlrpc_conf     *conf;
866         struct sptlrpc_conf_tgt *conf_tgt;
867         char                     name[MTI_NAME_MAXLEN];
868         int                      len, rc = 0;
869
870         obd_uuid2fsname(name, target->uuid, sizeof(name));
871
872         mutex_lock(&sptlrpc_conf_lock);
873
874         conf = sptlrpc_conf_get(name, 0);
875         if (conf == NULL)
876                 goto out;
877
878         /* convert uuid name (supposed end with _UUID) to target name */
879         len = strlen(target->uuid);
880         LASSERT(len > 5);
881         memcpy(name, target->uuid, len - 5);
882         name[len - 5] = '\0';
883
884         conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
885         if (conf_tgt) {
886                 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
887                                              from, to, nid, sf);
888                 if (rc)
889                         goto out;
890         }
891
892         rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
893 out:
894         mutex_unlock(&sptlrpc_conf_lock);
895
896         if (rc == 0)
897                 get_default_flavor(sf);
898
899         flavor_set_flags(sf, from, to, 1);
900 }
901
902 /**
903  * called by target devices, determine the expected flavor from
904  * certain peer (from, nid).
905  */
906 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
907                                   enum lustre_sec_part from,
908                                   lnet_nid_t nid,
909                                   struct sptlrpc_flavor *sf)
910 {
911         if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
912                 get_default_flavor(sf);
913 }
914
915 #define SEC_ADAPT_DELAY         (10)
916
917 /**
918  * called by client devices, notify the sptlrpc config has changed and
919  * do import_sec_adapt later.
920  */
921 void sptlrpc_conf_client_adapt(struct obd_device *obd)
922 {
923         struct obd_import  *imp;
924         ENTRY;
925
926         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
927                 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0 ||
928                 strcmp(obd->obd_type->typ_name, LUSTRE_OSP_NAME) == 0 ||
929                 strcmp(obd->obd_type->typ_name, LUSTRE_LWP_NAME) == 0);
930         CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
931
932         /* serialize with connect/disconnect import */
933         down_read_nested(&obd->u.cli.cl_sem, OBD_CLI_SEM_MDCOSC);
934
935         imp = obd->u.cli.cl_import;
936         if (imp) {
937                 spin_lock(&imp->imp_lock);
938                 if (imp->imp_sec)
939                         imp->imp_sec_expire = ktime_get_real_seconds() +
940                                 SEC_ADAPT_DELAY;
941                 spin_unlock(&imp->imp_lock);
942         }
943
944         up_read(&obd->u.cli.cl_sem);
945         EXIT;
946 }
947 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
948
949 /**
950  * called by target devices, extract sptlrpc rules which applies to
951  * this target, to be used for future rpc flavor checking.
952  */
953 int sptlrpc_conf_target_get_rules(struct obd_device *obd,
954                                   struct sptlrpc_rule_set *rset)
955 {
956         struct sptlrpc_conf *conf;
957         struct sptlrpc_conf_tgt *conf_tgt;
958         enum lustre_sec_part sp_dst;
959         char fsname[MTI_NAME_MAXLEN];
960         int rc = 0;
961         ENTRY;
962
963         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
964                 sp_dst = LUSTRE_SP_MDT;
965         } else if (strcmp(obd->obd_type->typ_name, LUSTRE_OST_NAME) == 0) {
966                 sp_dst = LUSTRE_SP_OST;
967         } else {
968                 CERROR("unexpected obd type %s\n", obd->obd_type->typ_name);
969                 RETURN(-EINVAL);
970         }
971
972         obd_uuid2fsname(fsname, obd->obd_uuid.uuid, sizeof(fsname));
973
974         mutex_lock(&sptlrpc_conf_lock);
975         conf = sptlrpc_conf_get(fsname, 0);
976         if (conf == NULL) {
977                 CERROR("missing sptlrpc config log\n");
978                 rc = -EFAULT;
979         } else {
980                 /* extract rule set for this target */
981                 conf_tgt = sptlrpc_conf_get_tgt(conf, obd->obd_name, 0);
982
983                 rc = sptlrpc_rule_set_extract(&conf->sc_rset,
984                                       conf_tgt ? &conf_tgt->sct_rset : NULL,
985                                       LUSTRE_SP_ANY, sp_dst, rset);
986         }
987         mutex_unlock(&sptlrpc_conf_lock);
988
989         RETURN(rc);
990 }
991
992 int  sptlrpc_conf_init(void)
993 {
994         INIT_LIST_HEAD(&sptlrpc_confs);
995         mutex_init(&sptlrpc_conf_lock);
996         return 0;
997 }
998
999 void sptlrpc_conf_fini(void)
1000 {
1001         struct sptlrpc_conf  *conf, *conf_next;
1002
1003         mutex_lock(&sptlrpc_conf_lock);
1004         list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list)
1005                 sptlrpc_conf_free(conf);
1006         LASSERT(list_empty(&sptlrpc_confs));
1007         mutex_unlock(&sptlrpc_conf_lock);
1008 }