]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/ptlrpc/sec_config.c
16dbf3fcfc8487481ee5fe3b2f5f905c5d2e7126
[karo-tx-linux.git] / drivers / staging / lustre / 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.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) 2008, 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
37 #define DEBUG_SUBSYSTEM S_SEC
38
39 #include "../../include/linux/libcfs/libcfs.h"
40 #include <linux/crypto.h>
41 #include <linux/key.h>
42
43 #include "../include/obd.h"
44 #include "../include/obd_support.h"
45 #include "../include/lustre_import.h"
46 #include "../include/lustre_param.h"
47 #include "../include/lustre_sec.h"
48
49 #include "ptlrpc_internal.h"
50
51 const char *sptlrpc_part2name(enum lustre_sec_part part)
52 {
53         switch (part) {
54         case LUSTRE_SP_CLI:
55                 return "cli";
56         case LUSTRE_SP_MDT:
57                 return "mdt";
58         case LUSTRE_SP_OST:
59                 return "ost";
60         case LUSTRE_SP_MGC:
61                 return "mgc";
62         case LUSTRE_SP_MGS:
63                 return "mgs";
64         case LUSTRE_SP_ANY:
65                 return "any";
66         default:
67                 return "err";
68         }
69 }
70 EXPORT_SYMBOL(sptlrpc_part2name);
71
72 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
73 {
74         const char *type = obd->obd_type->typ_name;
75
76         if (!strcmp(type, LUSTRE_MDT_NAME))
77                 return LUSTRE_SP_MDT;
78         if (!strcmp(type, LUSTRE_OST_NAME))
79                 return LUSTRE_SP_OST;
80         if (!strcmp(type, LUSTRE_MGS_NAME))
81                 return LUSTRE_SP_MGS;
82
83         CERROR("unknown target %p(%s)\n", obd, type);
84         return LUSTRE_SP_ANY;
85 }
86 EXPORT_SYMBOL(sptlrpc_target_sec_part);
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         strncpy(buf, str, sizeof(buf));
108         buf[sizeof(buf) - 1] = '\0';
109
110         bulk = strchr(buf, '-');
111         if (bulk)
112                 *bulk++ = '\0';
113
114         flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
115         if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
116                 goto err_out;
117
118         /*
119          * currently only base flavor "plain" can have bulk specification.
120          */
121         if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
122                 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
123                 if (bulk) {
124                         /*
125                          * format: plain-hash:<hash_alg>
126                          */
127                         alg = strchr(bulk, ':');
128                         if (alg == NULL)
129                                 goto err_out;
130                         *alg++ = '\0';
131
132                         if (strcmp(bulk, "hash"))
133                                 goto err_out;
134
135                         flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
136                         if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
137                                 goto err_out;
138                 }
139
140                 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
141                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
142                 else
143                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
144         } else {
145                 if (bulk)
146                         goto err_out;
147         }
148
149         flvr->sf_flags = 0;
150         return 0;
151
152 err_out:
153         CERROR("invalid flavor string: %s\n", str);
154         return -EINVAL;
155 }
156 EXPORT_SYMBOL(sptlrpc_parse_flavor);
157
158 /****************************************
159  * configure rules                    *
160  ****************************************/
161
162 static void get_default_flavor(struct sptlrpc_flavor *sf)
163 {
164         memset(sf, 0, sizeof(*sf));
165
166         sf->sf_rpc = SPTLRPC_FLVR_NULL;
167         sf->sf_flags = 0;
168 }
169
170 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
171 {
172         rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
173         rule->sr_from = LUSTRE_SP_ANY;
174         rule->sr_to = LUSTRE_SP_ANY;
175         rule->sr_padding = 0;
176
177         get_default_flavor(&rule->sr_flvr);
178 }
179
180 /*
181  * format: network[.direction]=flavor
182  */
183 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
184 {
185         char       *flavor, *dir;
186         int          rc;
187
188         sptlrpc_rule_init(rule);
189
190         flavor = strchr(param, '=');
191         if (flavor == NULL) {
192                 CERROR("invalid param, no '='\n");
193                 return -EINVAL;
194         }
195         *flavor++ = '\0';
196
197         dir = strchr(param, '.');
198         if (dir)
199                 *dir++ = '\0';
200
201         /* 1.1 network */
202         if (strcmp(param, "default")) {
203                 rule->sr_netid = libcfs_str2net(param);
204                 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
205                         CERROR("invalid network name: %s\n", param);
206                         return -EINVAL;
207                 }
208         }
209
210         /* 1.2 direction */
211         if (dir) {
212                 if (!strcmp(dir, "mdt2ost")) {
213                         rule->sr_from = LUSTRE_SP_MDT;
214                         rule->sr_to = LUSTRE_SP_OST;
215                 } else if (!strcmp(dir, "mdt2mdt")) {
216                         rule->sr_from = LUSTRE_SP_MDT;
217                         rule->sr_to = LUSTRE_SP_MDT;
218                 } else if (!strcmp(dir, "cli2ost")) {
219                         rule->sr_from = LUSTRE_SP_CLI;
220                         rule->sr_to = LUSTRE_SP_OST;
221                 } else if (!strcmp(dir, "cli2mdt")) {
222                         rule->sr_from = LUSTRE_SP_CLI;
223                         rule->sr_to = LUSTRE_SP_MDT;
224                 } else {
225                         CERROR("invalid rule dir segment: %s\n", dir);
226                         return -EINVAL;
227                 }
228         }
229
230         /* 2.1 flavor */
231         rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
232         if (rc)
233                 return -EINVAL;
234
235         return 0;
236 }
237 EXPORT_SYMBOL(sptlrpc_parse_rule);
238
239 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
240 {
241         LASSERT(rset->srs_nslot ||
242                 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
243
244         if (rset->srs_nslot) {
245                 kfree(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 accommodate 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         rules = kcalloc(nslot, sizeof(*rset->srs_rules), GFP_NOFS);
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                 kfree(rset->srs_rules);
277         }
278
279         rset->srs_rules = rules;
280         rset->srs_nslot = nslot;
281         return 0;
282 }
283 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
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 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
432
433 void sptlrpc_rule_set_dump(struct sptlrpc_rule_set *rset)
434 {
435         struct sptlrpc_rule *r;
436         int     n;
437
438         for (n = 0; n < rset->srs_nrule; n++) {
439                 r = &rset->srs_rules[n];
440                 CDEBUG(D_SEC, "<%02d> from %x to %x, net %x, rpc %x\n", n,
441                        r->sr_from, r->sr_to, r->sr_netid, r->sr_flvr.sf_rpc);
442         }
443 }
444 EXPORT_SYMBOL(sptlrpc_rule_set_dump);
445
446 /**********************************
447  * sptlrpc configuration support  *
448  **********************************/
449
450 struct sptlrpc_conf_tgt {
451         struct list_head              sct_list;
452         char                sct_name[MAX_OBD_NAME];
453         struct sptlrpc_rule_set sct_rset;
454 };
455
456 struct sptlrpc_conf {
457         struct list_head              sc_list;
458         char                sc_fsname[MTI_NAME_MAXLEN];
459         unsigned int        sc_modified;  /* modified during updating */
460         unsigned int        sc_updated:1, /* updated copy from MGS */
461                                 sc_local:1;   /* local copy from target */
462         struct sptlrpc_rule_set sc_rset;      /* fs general rules */
463         struct list_head              sc_tgts;      /* target-specific rules */
464 };
465
466 static struct mutex sptlrpc_conf_lock;
467 static LIST_HEAD(sptlrpc_confs);
468
469 static inline int is_hex(char c)
470 {
471         return ((c >= '0' && c <= '9') ||
472                 (c >= 'a' && c <= 'f'));
473 }
474
475 static void target2fsname(const char *tgt, char *fsname, int buflen)
476 {
477         const char     *ptr;
478         int          len;
479
480         ptr = strrchr(tgt, '-');
481         if (ptr) {
482                 if ((strncmp(ptr, "-MDT", 4) != 0 &&
483                      strncmp(ptr, "-OST", 4) != 0) ||
484                     !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
485                     !is_hex(ptr[6]) || !is_hex(ptr[7]))
486                         ptr = NULL;
487         }
488
489         /* if we didn't find the pattern, treat the whole string as fsname */
490         if (ptr == NULL)
491                 len = strlen(tgt);
492         else
493                 len = ptr - tgt;
494
495         len = min(len, buflen - 1);
496         memcpy(fsname, tgt, len);
497         fsname[len] = '\0';
498 }
499
500 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
501 {
502         struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
503
504         sptlrpc_rule_set_free(&conf->sc_rset);
505
506         list_for_each_entry_safe(conf_tgt, conf_tgt_next,
507                                      &conf->sc_tgts, sct_list) {
508                 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
509                 list_del(&conf_tgt->sct_list);
510                 kfree(conf_tgt);
511         }
512         LASSERT(list_empty(&conf->sc_tgts));
513
514         conf->sc_updated = 0;
515         conf->sc_local = 0;
516 }
517
518 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
519 {
520         CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
521
522         sptlrpc_conf_free_rsets(conf);
523         list_del(&conf->sc_list);
524         kfree(conf);
525 }
526
527 static
528 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
529                                               const char *name,
530                                               int create)
531 {
532         struct sptlrpc_conf_tgt *conf_tgt;
533
534         list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
535                 if (strcmp(conf_tgt->sct_name, name) == 0)
536                         return conf_tgt;
537         }
538
539         if (!create)
540                 return NULL;
541
542         conf_tgt = kzalloc(sizeof(*conf_tgt), GFP_NOFS);
543         if (conf_tgt) {
544                 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
545                 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
546                 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
547         }
548
549         return conf_tgt;
550 }
551
552 static
553 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
554                                       int create)
555 {
556         struct sptlrpc_conf *conf;
557
558         list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
559                 if (strcmp(conf->sc_fsname, fsname) == 0)
560                         return conf;
561         }
562
563         if (!create)
564                 return NULL;
565
566         conf = kzalloc(sizeof(*conf), GFP_NOFS);
567         if (conf == NULL)
568                 return NULL;
569
570         strcpy(conf->sc_fsname, fsname);
571         sptlrpc_rule_set_init(&conf->sc_rset);
572         INIT_LIST_HEAD(&conf->sc_tgts);
573         list_add(&conf->sc_list, &sptlrpc_confs);
574
575         CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
576         return conf;
577 }
578
579 /**
580  * caller must hold conf_lock already.
581  */
582 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
583                                    const char *target,
584                                    struct sptlrpc_rule *rule)
585 {
586         struct sptlrpc_conf_tgt  *conf_tgt;
587         struct sptlrpc_rule_set  *rule_set;
588
589         /* fsname == target means general rules for the whole fs */
590         if (strcmp(conf->sc_fsname, target) == 0) {
591                 rule_set = &conf->sc_rset;
592         } else {
593                 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
594                 if (conf_tgt) {
595                         rule_set = &conf_tgt->sct_rset;
596                 } else {
597                         CERROR("out of memory, can't merge rule!\n");
598                         return -ENOMEM;
599                 }
600         }
601
602         return sptlrpc_rule_set_merge(rule_set, rule);
603 }
604
605 /**
606  * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
607  * find one through the target name in the record inside conf_lock;
608  * otherwise means caller already hold conf_lock.
609  */
610 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
611                                     struct sptlrpc_conf *conf)
612 {
613         char               *target, *param;
614         char                fsname[MTI_NAME_MAXLEN];
615         struct sptlrpc_rule     rule;
616         int                  rc;
617
618         target = lustre_cfg_string(lcfg, 1);
619         if (target == NULL) {
620                 CERROR("missing target name\n");
621                 return -EINVAL;
622         }
623
624         param = lustre_cfg_string(lcfg, 2);
625         if (param == NULL) {
626                 CERROR("missing parameter\n");
627                 return -EINVAL;
628         }
629
630         CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
631
632         /* parse rule to make sure the format is correct */
633         if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
634                 CERROR("Invalid sptlrpc parameter: %s\n", param);
635                 return -EINVAL;
636         }
637         param += sizeof(PARAM_SRPC_FLVR) - 1;
638
639         rc = sptlrpc_parse_rule(param, &rule);
640         if (rc)
641                 return -EINVAL;
642
643         if (conf == NULL) {
644                 target2fsname(target, fsname, sizeof(fsname));
645
646                 mutex_lock(&sptlrpc_conf_lock);
647                 conf = sptlrpc_conf_get(fsname, 0);
648                 if (conf == NULL) {
649                         CERROR("can't find conf\n");
650                         rc = -ENOMEM;
651                 } else {
652                         rc = sptlrpc_conf_merge_rule(conf, target, &rule);
653                 }
654                 mutex_unlock(&sptlrpc_conf_lock);
655         } else {
656                 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
657                 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
658         }
659
660         if (rc == 0)
661                 conf->sc_modified++;
662
663         return rc;
664 }
665
666 int sptlrpc_process_config(struct lustre_cfg *lcfg)
667 {
668         return __sptlrpc_process_config(lcfg, NULL);
669 }
670 EXPORT_SYMBOL(sptlrpc_process_config);
671
672 static int logname2fsname(const char *logname, char *buf, int buflen)
673 {
674         char   *ptr;
675         int     len;
676
677         ptr = strrchr(logname, '-');
678         if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
679                 CERROR("%s is not a sptlrpc config log\n", logname);
680                 return -EINVAL;
681         }
682
683         len = min((int) (ptr - logname), buflen - 1);
684
685         memcpy(buf, logname, len);
686         buf[len] = '\0';
687         return 0;
688 }
689
690 void sptlrpc_conf_log_update_begin(const char *logname)
691 {
692         struct sptlrpc_conf *conf;
693         char             fsname[16];
694
695         if (logname2fsname(logname, fsname, sizeof(fsname)))
696                 return;
697
698         mutex_lock(&sptlrpc_conf_lock);
699
700         conf = sptlrpc_conf_get(fsname, 0);
701         if (conf) {
702                 if (conf->sc_local) {
703                         LASSERT(conf->sc_updated == 0);
704                         sptlrpc_conf_free_rsets(conf);
705                 }
706                 conf->sc_modified = 0;
707         }
708
709         mutex_unlock(&sptlrpc_conf_lock);
710 }
711 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
712
713 /**
714  * mark a config log has been updated
715  */
716 void sptlrpc_conf_log_update_end(const char *logname)
717 {
718         struct sptlrpc_conf *conf;
719         char             fsname[16];
720
721         if (logname2fsname(logname, fsname, sizeof(fsname)))
722                 return;
723
724         mutex_lock(&sptlrpc_conf_lock);
725
726         conf = sptlrpc_conf_get(fsname, 0);
727         if (conf) {
728                 /*
729                  * if original state is not updated, make sure the
730                  * modified counter > 0 to enforce updating local copy.
731                  */
732                 if (conf->sc_updated == 0)
733                         conf->sc_modified++;
734
735                 conf->sc_updated = 1;
736         }
737
738         mutex_unlock(&sptlrpc_conf_lock);
739 }
740 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
741
742 void sptlrpc_conf_log_start(const char *logname)
743 {
744         char             fsname[16];
745
746         if (logname2fsname(logname, fsname, sizeof(fsname)))
747                 return;
748
749         mutex_lock(&sptlrpc_conf_lock);
750         sptlrpc_conf_get(fsname, 1);
751         mutex_unlock(&sptlrpc_conf_lock);
752 }
753 EXPORT_SYMBOL(sptlrpc_conf_log_start);
754
755 void sptlrpc_conf_log_stop(const char *logname)
756 {
757         struct sptlrpc_conf *conf;
758         char             fsname[16];
759
760         if (logname2fsname(logname, fsname, sizeof(fsname)))
761                 return;
762
763         mutex_lock(&sptlrpc_conf_lock);
764         conf = sptlrpc_conf_get(fsname, 0);
765         if (conf)
766                 sptlrpc_conf_free(conf);
767         mutex_unlock(&sptlrpc_conf_lock);
768 }
769 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
770
771 static inline void flavor_set_flags(struct sptlrpc_flavor *sf,
772                                     enum lustre_sec_part from,
773                                     enum lustre_sec_part to,
774                                     unsigned int fl_udesc)
775 {
776         /*
777          * null flavor doesn't need to set any flavor, and in fact
778          * we'd better not do that because everybody share a single sec.
779          */
780         if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
781                 return;
782
783         if (from == LUSTRE_SP_MDT) {
784                 /* MDT->MDT; MDT->OST */
785                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
786         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
787                 /* CLI->OST */
788                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
789         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
790                 /* CLI->MDT */
791                 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
792                         sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
793         }
794 }
795
796 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
797                                 enum lustre_sec_part to,
798                                 struct obd_uuid *target,
799                                 lnet_nid_t nid,
800                                 struct sptlrpc_flavor *sf)
801 {
802         struct sptlrpc_conf     *conf;
803         struct sptlrpc_conf_tgt *conf_tgt;
804         char                 name[MTI_NAME_MAXLEN];
805         int                   len, rc = 0;
806
807         target2fsname(target->uuid, name, sizeof(name));
808
809         mutex_lock(&sptlrpc_conf_lock);
810
811         conf = sptlrpc_conf_get(name, 0);
812         if (conf == NULL)
813                 goto out;
814
815         /* convert uuid name (supposed end with _UUID) to target name */
816         len = strlen(target->uuid);
817         LASSERT(len > 5);
818         memcpy(name, target->uuid, len - 5);
819         name[len - 5] = '\0';
820
821         conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
822         if (conf_tgt) {
823                 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
824                                              from, to, nid, sf);
825                 if (rc)
826                         goto out;
827         }
828
829         rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
830 out:
831         mutex_unlock(&sptlrpc_conf_lock);
832
833         if (rc == 0)
834                 get_default_flavor(sf);
835
836         flavor_set_flags(sf, from, to, 1);
837 }
838
839 /**
840  * called by target devices, determine the expected flavor from
841  * certain peer (from, nid).
842  */
843 void sptlrpc_target_choose_flavor(struct sptlrpc_rule_set *rset,
844                                   enum lustre_sec_part from,
845                                   lnet_nid_t nid,
846                                   struct sptlrpc_flavor *sf)
847 {
848         if (sptlrpc_rule_set_choose(rset, from, LUSTRE_SP_ANY, nid, sf) == 0)
849                 get_default_flavor(sf);
850 }
851 EXPORT_SYMBOL(sptlrpc_target_choose_flavor);
852
853 #define SEC_ADAPT_DELAY  (10)
854
855 /**
856  * called by client devices, notify the sptlrpc config has changed and
857  * do import_sec_adapt later.
858  */
859 void sptlrpc_conf_client_adapt(struct obd_device *obd)
860 {
861         struct obd_import  *imp;
862
863         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
864                 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0);
865         CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
866
867         /* serialize with connect/disconnect import */
868         down_read(&obd->u.cli.cl_sem);
869
870         imp = obd->u.cli.cl_import;
871         if (imp) {
872                 spin_lock(&imp->imp_lock);
873                 if (imp->imp_sec)
874                         imp->imp_sec_expire = get_seconds() +
875                                 SEC_ADAPT_DELAY;
876                 spin_unlock(&imp->imp_lock);
877         }
878
879         up_read(&obd->u.cli.cl_sem);
880 }
881 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
882
883 int  sptlrpc_conf_init(void)
884 {
885         mutex_init(&sptlrpc_conf_lock);
886         return 0;
887 }
888
889 void sptlrpc_conf_fini(void)
890 {
891         struct sptlrpc_conf  *conf, *conf_next;
892
893         mutex_lock(&sptlrpc_conf_lock);
894         list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
895                 sptlrpc_conf_free(conf);
896         }
897         LASSERT(list_empty(&sptlrpc_confs));
898         mutex_unlock(&sptlrpc_conf_lock);
899 }