]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/ptlrpc/sec_config.c
0d29b8734a531314b8fcfb88bfb89f07780f2a7c
[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 enum lustre_sec_part sptlrpc_target_sec_part(struct obd_device *obd)
52 {
53         const char *type = obd->obd_type->typ_name;
54
55         if (!strcmp(type, LUSTRE_MDT_NAME))
56                 return LUSTRE_SP_MDT;
57         if (!strcmp(type, LUSTRE_OST_NAME))
58                 return LUSTRE_SP_OST;
59         if (!strcmp(type, LUSTRE_MGS_NAME))
60                 return LUSTRE_SP_MGS;
61
62         CERROR("unknown target %p(%s)\n", obd, type);
63         return LUSTRE_SP_ANY;
64 }
65 EXPORT_SYMBOL(sptlrpc_target_sec_part);
66
67 /****************************************
68  * user supplied flavor string parsing  *
69  ****************************************/
70
71 /*
72  * format: <base_flavor>[-<bulk_type:alg_spec>]
73  */
74 int sptlrpc_parse_flavor(const char *str, struct sptlrpc_flavor *flvr)
75 {
76         char buf[32];
77         char *bulk, *alg;
78
79         memset(flvr, 0, sizeof(*flvr));
80
81         if (str == NULL || str[0] == '\0') {
82                 flvr->sf_rpc = SPTLRPC_FLVR_INVALID;
83                 return 0;
84         }
85
86         strncpy(buf, str, sizeof(buf));
87         buf[sizeof(buf) - 1] = '\0';
88
89         bulk = strchr(buf, '-');
90         if (bulk)
91                 *bulk++ = '\0';
92
93         flvr->sf_rpc = sptlrpc_name2flavor_base(buf);
94         if (flvr->sf_rpc == SPTLRPC_FLVR_INVALID)
95                 goto err_out;
96
97         /*
98          * currently only base flavor "plain" can have bulk specification.
99          */
100         if (flvr->sf_rpc == SPTLRPC_FLVR_PLAIN) {
101                 flvr->u_bulk.hash.hash_alg = BULK_HASH_ALG_ADLER32;
102                 if (bulk) {
103                         /*
104                          * format: plain-hash:<hash_alg>
105                          */
106                         alg = strchr(bulk, ':');
107                         if (alg == NULL)
108                                 goto err_out;
109                         *alg++ = '\0';
110
111                         if (strcmp(bulk, "hash"))
112                                 goto err_out;
113
114                         flvr->u_bulk.hash.hash_alg = sptlrpc_get_hash_alg(alg);
115                         if (flvr->u_bulk.hash.hash_alg >= BULK_HASH_ALG_MAX)
116                                 goto err_out;
117                 }
118
119                 if (flvr->u_bulk.hash.hash_alg == BULK_HASH_ALG_NULL)
120                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_NULL);
121                 else
122                         flvr_set_bulk_svc(&flvr->sf_rpc, SPTLRPC_BULK_SVC_INTG);
123         } else {
124                 if (bulk)
125                         goto err_out;
126         }
127
128         flvr->sf_flags = 0;
129         return 0;
130
131 err_out:
132         CERROR("invalid flavor string: %s\n", str);
133         return -EINVAL;
134 }
135 EXPORT_SYMBOL(sptlrpc_parse_flavor);
136
137 /****************************************
138  * configure rules                    *
139  ****************************************/
140
141 static void get_default_flavor(struct sptlrpc_flavor *sf)
142 {
143         memset(sf, 0, sizeof(*sf));
144
145         sf->sf_rpc = SPTLRPC_FLVR_NULL;
146         sf->sf_flags = 0;
147 }
148
149 static void sptlrpc_rule_init(struct sptlrpc_rule *rule)
150 {
151         rule->sr_netid = LNET_NIDNET(LNET_NID_ANY);
152         rule->sr_from = LUSTRE_SP_ANY;
153         rule->sr_to = LUSTRE_SP_ANY;
154         rule->sr_padding = 0;
155
156         get_default_flavor(&rule->sr_flvr);
157 }
158
159 /*
160  * format: network[.direction]=flavor
161  */
162 int sptlrpc_parse_rule(char *param, struct sptlrpc_rule *rule)
163 {
164         char *flavor, *dir;
165         int rc;
166
167         sptlrpc_rule_init(rule);
168
169         flavor = strchr(param, '=');
170         if (flavor == NULL) {
171                 CERROR("invalid param, no '='\n");
172                 return -EINVAL;
173         }
174         *flavor++ = '\0';
175
176         dir = strchr(param, '.');
177         if (dir)
178                 *dir++ = '\0';
179
180         /* 1.1 network */
181         if (strcmp(param, "default")) {
182                 rule->sr_netid = libcfs_str2net(param);
183                 if (rule->sr_netid == LNET_NIDNET(LNET_NID_ANY)) {
184                         CERROR("invalid network name: %s\n", param);
185                         return -EINVAL;
186                 }
187         }
188
189         /* 1.2 direction */
190         if (dir) {
191                 if (!strcmp(dir, "mdt2ost")) {
192                         rule->sr_from = LUSTRE_SP_MDT;
193                         rule->sr_to = LUSTRE_SP_OST;
194                 } else if (!strcmp(dir, "mdt2mdt")) {
195                         rule->sr_from = LUSTRE_SP_MDT;
196                         rule->sr_to = LUSTRE_SP_MDT;
197                 } else if (!strcmp(dir, "cli2ost")) {
198                         rule->sr_from = LUSTRE_SP_CLI;
199                         rule->sr_to = LUSTRE_SP_OST;
200                 } else if (!strcmp(dir, "cli2mdt")) {
201                         rule->sr_from = LUSTRE_SP_CLI;
202                         rule->sr_to = LUSTRE_SP_MDT;
203                 } else {
204                         CERROR("invalid rule dir segment: %s\n", dir);
205                         return -EINVAL;
206                 }
207         }
208
209         /* 2.1 flavor */
210         rc = sptlrpc_parse_flavor(flavor, &rule->sr_flvr);
211         if (rc)
212                 return -EINVAL;
213
214         return 0;
215 }
216 EXPORT_SYMBOL(sptlrpc_parse_rule);
217
218 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
219 {
220         LASSERT(rset->srs_nslot ||
221                 (rset->srs_nrule == 0 && rset->srs_rules == NULL));
222
223         if (rset->srs_nslot) {
224                 kfree(rset->srs_rules);
225                 sptlrpc_rule_set_init(rset);
226         }
227 }
228 EXPORT_SYMBOL(sptlrpc_rule_set_free);
229
230 /*
231  * return 0 if the rule set could accommodate one more rule.
232  */
233 int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
234 {
235         struct sptlrpc_rule *rules;
236         int nslot;
237
238         might_sleep();
239
240         if (rset->srs_nrule < rset->srs_nslot)
241                 return 0;
242
243         nslot = rset->srs_nslot + 8;
244
245         /* better use realloc() if available */
246         rules = kcalloc(nslot, sizeof(*rset->srs_rules), GFP_NOFS);
247         if (rules == NULL)
248                 return -ENOMEM;
249
250         if (rset->srs_nrule) {
251                 LASSERT(rset->srs_nslot && rset->srs_rules);
252                 memcpy(rules, rset->srs_rules,
253                        rset->srs_nrule * sizeof(*rset->srs_rules));
254
255                 kfree(rset->srs_rules);
256         }
257
258         rset->srs_rules = rules;
259         rset->srs_nslot = nslot;
260         return 0;
261 }
262 EXPORT_SYMBOL(sptlrpc_rule_set_expand);
263
264 static inline int rule_spec_dir(struct sptlrpc_rule *rule)
265 {
266         return (rule->sr_from != LUSTRE_SP_ANY ||
267                 rule->sr_to != LUSTRE_SP_ANY);
268 }
269 static inline int rule_spec_net(struct sptlrpc_rule *rule)
270 {
271         return (rule->sr_netid != LNET_NIDNET(LNET_NID_ANY));
272 }
273 static inline int rule_match_dir(struct sptlrpc_rule *r1,
274                                  struct sptlrpc_rule *r2)
275 {
276         return (r1->sr_from == r2->sr_from && r1->sr_to == r2->sr_to);
277 }
278 static inline int rule_match_net(struct sptlrpc_rule *r1,
279                                  struct sptlrpc_rule *r2)
280 {
281         return (r1->sr_netid == r2->sr_netid);
282 }
283
284 /*
285  * merge @rule into @rset.
286  * the @rset slots might be expanded.
287  */
288 int sptlrpc_rule_set_merge(struct sptlrpc_rule_set *rset,
289                            struct sptlrpc_rule *rule)
290 {
291         struct sptlrpc_rule *p = rset->srs_rules;
292         int spec_dir, spec_net;
293         int rc, n, match = 0;
294
295         might_sleep();
296
297         spec_net = rule_spec_net(rule);
298         spec_dir = rule_spec_dir(rule);
299
300         for (n = 0; n < rset->srs_nrule; n++) {
301                 p = &rset->srs_rules[n];
302
303                 /* test network match, if failed:
304                  * - spec rule: skip rules which is also spec rule match, until
305                  *   we hit a wild rule, which means no more chance
306                  * - wild rule: skip until reach the one which is also wild
307                  *   and matches
308                  */
309                 if (!rule_match_net(p, rule)) {
310                         if (spec_net) {
311                                 if (rule_spec_net(p))
312                                         continue;
313                                 else
314                                         break;
315                         } else {
316                                 continue;
317                         }
318                 }
319
320                 /* test dir match, same logic as net matching */
321                 if (!rule_match_dir(p, rule)) {
322                         if (spec_dir) {
323                                 if (rule_spec_dir(p))
324                                         continue;
325                                 else
326                                         break;
327                         } else {
328                                 continue;
329                         }
330                 }
331
332                 /* find a match */
333                 match = 1;
334                 break;
335         }
336
337         if (match) {
338                 LASSERT(n >= 0 && n < rset->srs_nrule);
339
340                 if (rule->sr_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
341                         /* remove this rule */
342                         if (n < rset->srs_nrule - 1)
343                                 memmove(&rset->srs_rules[n],
344                                         &rset->srs_rules[n + 1],
345                                         (rset->srs_nrule - n - 1) *
346                                         sizeof(*rule));
347                         rset->srs_nrule--;
348                 } else {
349                         /* override the rule */
350                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
351                 }
352         } else {
353                 LASSERT(n >= 0 && n <= rset->srs_nrule);
354
355                 if (rule->sr_flvr.sf_rpc != SPTLRPC_FLVR_INVALID) {
356                         rc = sptlrpc_rule_set_expand(rset);
357                         if (rc)
358                                 return rc;
359
360                         if (n < rset->srs_nrule)
361                                 memmove(&rset->srs_rules[n + 1],
362                                         &rset->srs_rules[n],
363                                         (rset->srs_nrule - n) * sizeof(*rule));
364                         memcpy(&rset->srs_rules[n], rule, sizeof(*rule));
365                         rset->srs_nrule++;
366                 } else {
367                         CDEBUG(D_CONFIG, "ignore the unmatched deletion\n");
368                 }
369         }
370
371         return 0;
372 }
373 EXPORT_SYMBOL(sptlrpc_rule_set_merge);
374
375 /**
376  * given from/to/nid, determine a matching flavor in ruleset.
377  * return 1 if a match found, otherwise return 0.
378  */
379 int sptlrpc_rule_set_choose(struct sptlrpc_rule_set *rset,
380                             enum lustre_sec_part from,
381                             enum lustre_sec_part to,
382                             lnet_nid_t nid,
383                             struct sptlrpc_flavor *sf)
384 {
385         struct sptlrpc_rule *r;
386         int n;
387
388         for (n = 0; n < rset->srs_nrule; n++) {
389                 r = &rset->srs_rules[n];
390
391                 if (LNET_NIDNET(nid) != LNET_NIDNET(LNET_NID_ANY) &&
392                     r->sr_netid != LNET_NIDNET(LNET_NID_ANY) &&
393                     LNET_NIDNET(nid) != r->sr_netid)
394                         continue;
395
396                 if (from != LUSTRE_SP_ANY && r->sr_from != LUSTRE_SP_ANY &&
397                     from != r->sr_from)
398                         continue;
399
400                 if (to != LUSTRE_SP_ANY && r->sr_to != LUSTRE_SP_ANY &&
401                     to != r->sr_to)
402                         continue;
403
404                 *sf = r->sr_flvr;
405                 return 1;
406         }
407
408         return 0;
409 }
410 EXPORT_SYMBOL(sptlrpc_rule_set_choose);
411
412 /**********************************
413  * sptlrpc configuration support  *
414  **********************************/
415
416 struct sptlrpc_conf_tgt {
417         struct list_head              sct_list;
418         char                sct_name[MAX_OBD_NAME];
419         struct sptlrpc_rule_set sct_rset;
420 };
421
422 struct sptlrpc_conf {
423         struct list_head              sc_list;
424         char                sc_fsname[MTI_NAME_MAXLEN];
425         unsigned int        sc_modified;  /* modified during updating */
426         unsigned int        sc_updated:1, /* updated copy from MGS */
427                                 sc_local:1;   /* local copy from target */
428         struct sptlrpc_rule_set sc_rset;      /* fs general rules */
429         struct list_head              sc_tgts;      /* target-specific rules */
430 };
431
432 static struct mutex sptlrpc_conf_lock;
433 static LIST_HEAD(sptlrpc_confs);
434
435 static inline int is_hex(char c)
436 {
437         return ((c >= '0' && c <= '9') ||
438                 (c >= 'a' && c <= 'f'));
439 }
440
441 static void target2fsname(const char *tgt, char *fsname, int buflen)
442 {
443         const char *ptr;
444         int len;
445
446         ptr = strrchr(tgt, '-');
447         if (ptr) {
448                 if ((strncmp(ptr, "-MDT", 4) != 0 &&
449                      strncmp(ptr, "-OST", 4) != 0) ||
450                     !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
451                     !is_hex(ptr[6]) || !is_hex(ptr[7]))
452                         ptr = NULL;
453         }
454
455         /* if we didn't find the pattern, treat the whole string as fsname */
456         if (ptr == NULL)
457                 len = strlen(tgt);
458         else
459                 len = ptr - tgt;
460
461         len = min(len, buflen - 1);
462         memcpy(fsname, tgt, len);
463         fsname[len] = '\0';
464 }
465
466 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
467 {
468         struct sptlrpc_conf_tgt *conf_tgt, *conf_tgt_next;
469
470         sptlrpc_rule_set_free(&conf->sc_rset);
471
472         list_for_each_entry_safe(conf_tgt, conf_tgt_next,
473                                      &conf->sc_tgts, sct_list) {
474                 sptlrpc_rule_set_free(&conf_tgt->sct_rset);
475                 list_del(&conf_tgt->sct_list);
476                 kfree(conf_tgt);
477         }
478         LASSERT(list_empty(&conf->sc_tgts));
479
480         conf->sc_updated = 0;
481         conf->sc_local = 0;
482 }
483
484 static void sptlrpc_conf_free(struct sptlrpc_conf *conf)
485 {
486         CDEBUG(D_SEC, "free sptlrpc conf %s\n", conf->sc_fsname);
487
488         sptlrpc_conf_free_rsets(conf);
489         list_del(&conf->sc_list);
490         kfree(conf);
491 }
492
493 static
494 struct sptlrpc_conf_tgt *sptlrpc_conf_get_tgt(struct sptlrpc_conf *conf,
495                                               const char *name,
496                                               int create)
497 {
498         struct sptlrpc_conf_tgt *conf_tgt;
499
500         list_for_each_entry(conf_tgt, &conf->sc_tgts, sct_list) {
501                 if (strcmp(conf_tgt->sct_name, name) == 0)
502                         return conf_tgt;
503         }
504
505         if (!create)
506                 return NULL;
507
508         conf_tgt = kzalloc(sizeof(*conf_tgt), GFP_NOFS);
509         if (conf_tgt) {
510                 strlcpy(conf_tgt->sct_name, name, sizeof(conf_tgt->sct_name));
511                 sptlrpc_rule_set_init(&conf_tgt->sct_rset);
512                 list_add(&conf_tgt->sct_list, &conf->sc_tgts);
513         }
514
515         return conf_tgt;
516 }
517
518 static
519 struct sptlrpc_conf *sptlrpc_conf_get(const char *fsname,
520                                       int create)
521 {
522         struct sptlrpc_conf *conf;
523
524         list_for_each_entry(conf, &sptlrpc_confs, sc_list) {
525                 if (strcmp(conf->sc_fsname, fsname) == 0)
526                         return conf;
527         }
528
529         if (!create)
530                 return NULL;
531
532         conf = kzalloc(sizeof(*conf), GFP_NOFS);
533         if (!conf)
534                 return NULL;
535
536         strcpy(conf->sc_fsname, fsname);
537         sptlrpc_rule_set_init(&conf->sc_rset);
538         INIT_LIST_HEAD(&conf->sc_tgts);
539         list_add(&conf->sc_list, &sptlrpc_confs);
540
541         CDEBUG(D_SEC, "create sptlrpc conf %s\n", conf->sc_fsname);
542         return conf;
543 }
544
545 /**
546  * caller must hold conf_lock already.
547  */
548 static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
549                                    const char *target,
550                                    struct sptlrpc_rule *rule)
551 {
552         struct sptlrpc_conf_tgt *conf_tgt;
553         struct sptlrpc_rule_set *rule_set;
554
555         /* fsname == target means general rules for the whole fs */
556         if (strcmp(conf->sc_fsname, target) == 0) {
557                 rule_set = &conf->sc_rset;
558         } else {
559                 conf_tgt = sptlrpc_conf_get_tgt(conf, target, 1);
560                 if (conf_tgt) {
561                         rule_set = &conf_tgt->sct_rset;
562                 } else {
563                         CERROR("out of memory, can't merge rule!\n");
564                         return -ENOMEM;
565                 }
566         }
567
568         return sptlrpc_rule_set_merge(rule_set, rule);
569 }
570
571 /**
572  * process one LCFG_SPTLRPC_CONF record. if \a conf is NULL, we
573  * find one through the target name in the record inside conf_lock;
574  * otherwise means caller already hold conf_lock.
575  */
576 static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
577                                     struct sptlrpc_conf *conf)
578 {
579         char *target, *param;
580         char fsname[MTI_NAME_MAXLEN];
581         struct sptlrpc_rule rule;
582         int rc;
583
584         target = lustre_cfg_string(lcfg, 1);
585         if (target == NULL) {
586                 CERROR("missing target name\n");
587                 return -EINVAL;
588         }
589
590         param = lustre_cfg_string(lcfg, 2);
591         if (param == NULL) {
592                 CERROR("missing parameter\n");
593                 return -EINVAL;
594         }
595
596         CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
597
598         /* parse rule to make sure the format is correct */
599         if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
600                 CERROR("Invalid sptlrpc parameter: %s\n", param);
601                 return -EINVAL;
602         }
603         param += sizeof(PARAM_SRPC_FLVR) - 1;
604
605         rc = sptlrpc_parse_rule(param, &rule);
606         if (rc)
607                 return -EINVAL;
608
609         if (conf == NULL) {
610                 target2fsname(target, fsname, sizeof(fsname));
611
612                 mutex_lock(&sptlrpc_conf_lock);
613                 conf = sptlrpc_conf_get(fsname, 0);
614                 if (conf == NULL) {
615                         CERROR("can't find conf\n");
616                         rc = -ENOMEM;
617                 } else {
618                         rc = sptlrpc_conf_merge_rule(conf, target, &rule);
619                 }
620                 mutex_unlock(&sptlrpc_conf_lock);
621         } else {
622                 LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
623                 rc = sptlrpc_conf_merge_rule(conf, target, &rule);
624         }
625
626         if (rc == 0)
627                 conf->sc_modified++;
628
629         return rc;
630 }
631
632 int sptlrpc_process_config(struct lustre_cfg *lcfg)
633 {
634         return __sptlrpc_process_config(lcfg, NULL);
635 }
636 EXPORT_SYMBOL(sptlrpc_process_config);
637
638 static int logname2fsname(const char *logname, char *buf, int buflen)
639 {
640         char *ptr;
641         int len;
642
643         ptr = strrchr(logname, '-');
644         if (ptr == NULL || strcmp(ptr, "-sptlrpc")) {
645                 CERROR("%s is not a sptlrpc config log\n", logname);
646                 return -EINVAL;
647         }
648
649         len = min((int) (ptr - logname), buflen - 1);
650
651         memcpy(buf, logname, len);
652         buf[len] = '\0';
653         return 0;
654 }
655
656 void sptlrpc_conf_log_update_begin(const char *logname)
657 {
658         struct sptlrpc_conf *conf;
659         char fsname[16];
660
661         if (logname2fsname(logname, fsname, sizeof(fsname)))
662                 return;
663
664         mutex_lock(&sptlrpc_conf_lock);
665
666         conf = sptlrpc_conf_get(fsname, 0);
667         if (conf) {
668                 if (conf->sc_local) {
669                         LASSERT(conf->sc_updated == 0);
670                         sptlrpc_conf_free_rsets(conf);
671                 }
672                 conf->sc_modified = 0;
673         }
674
675         mutex_unlock(&sptlrpc_conf_lock);
676 }
677 EXPORT_SYMBOL(sptlrpc_conf_log_update_begin);
678
679 /**
680  * mark a config log has been updated
681  */
682 void sptlrpc_conf_log_update_end(const char *logname)
683 {
684         struct sptlrpc_conf *conf;
685         char fsname[16];
686
687         if (logname2fsname(logname, fsname, sizeof(fsname)))
688                 return;
689
690         mutex_lock(&sptlrpc_conf_lock);
691
692         conf = sptlrpc_conf_get(fsname, 0);
693         if (conf) {
694                 /*
695                  * if original state is not updated, make sure the
696                  * modified counter > 0 to enforce updating local copy.
697                  */
698                 if (conf->sc_updated == 0)
699                         conf->sc_modified++;
700
701                 conf->sc_updated = 1;
702         }
703
704         mutex_unlock(&sptlrpc_conf_lock);
705 }
706 EXPORT_SYMBOL(sptlrpc_conf_log_update_end);
707
708 void sptlrpc_conf_log_start(const char *logname)
709 {
710         char fsname[16];
711
712         if (logname2fsname(logname, fsname, sizeof(fsname)))
713                 return;
714
715         mutex_lock(&sptlrpc_conf_lock);
716         sptlrpc_conf_get(fsname, 1);
717         mutex_unlock(&sptlrpc_conf_lock);
718 }
719 EXPORT_SYMBOL(sptlrpc_conf_log_start);
720
721 void sptlrpc_conf_log_stop(const char *logname)
722 {
723         struct sptlrpc_conf *conf;
724         char fsname[16];
725
726         if (logname2fsname(logname, fsname, sizeof(fsname)))
727                 return;
728
729         mutex_lock(&sptlrpc_conf_lock);
730         conf = sptlrpc_conf_get(fsname, 0);
731         if (conf)
732                 sptlrpc_conf_free(conf);
733         mutex_unlock(&sptlrpc_conf_lock);
734 }
735 EXPORT_SYMBOL(sptlrpc_conf_log_stop);
736
737 static inline void flavor_set_flags(struct sptlrpc_flavor *sf,
738                                     enum lustre_sec_part from,
739                                     enum lustre_sec_part to,
740                                     unsigned int fl_udesc)
741 {
742         /*
743          * null flavor doesn't need to set any flavor, and in fact
744          * we'd better not do that because everybody share a single sec.
745          */
746         if (sf->sf_rpc == SPTLRPC_FLVR_NULL)
747                 return;
748
749         if (from == LUSTRE_SP_MDT) {
750                 /* MDT->MDT; MDT->OST */
751                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY;
752         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_OST) {
753                 /* CLI->OST */
754                 sf->sf_flags |= PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_BULK;
755         } else if (from == LUSTRE_SP_CLI && to == LUSTRE_SP_MDT) {
756                 /* CLI->MDT */
757                 if (fl_udesc && sf->sf_rpc != SPTLRPC_FLVR_NULL)
758                         sf->sf_flags |= PTLRPC_SEC_FL_UDESC;
759         }
760 }
761
762 void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
763                                 enum lustre_sec_part to,
764                                 struct obd_uuid *target,
765                                 lnet_nid_t nid,
766                                 struct sptlrpc_flavor *sf)
767 {
768         struct sptlrpc_conf *conf;
769         struct sptlrpc_conf_tgt *conf_tgt;
770         char name[MTI_NAME_MAXLEN];
771         int len, rc = 0;
772
773         target2fsname(target->uuid, name, sizeof(name));
774
775         mutex_lock(&sptlrpc_conf_lock);
776
777         conf = sptlrpc_conf_get(name, 0);
778         if (conf == NULL)
779                 goto out;
780
781         /* convert uuid name (supposed end with _UUID) to target name */
782         len = strlen(target->uuid);
783         LASSERT(len > 5);
784         memcpy(name, target->uuid, len - 5);
785         name[len - 5] = '\0';
786
787         conf_tgt = sptlrpc_conf_get_tgt(conf, name, 0);
788         if (conf_tgt) {
789                 rc = sptlrpc_rule_set_choose(&conf_tgt->sct_rset,
790                                              from, to, nid, sf);
791                 if (rc)
792                         goto out;
793         }
794
795         rc = sptlrpc_rule_set_choose(&conf->sc_rset, from, to, nid, sf);
796 out:
797         mutex_unlock(&sptlrpc_conf_lock);
798
799         if (rc == 0)
800                 get_default_flavor(sf);
801
802         flavor_set_flags(sf, from, to, 1);
803 }
804
805 #define SEC_ADAPT_DELAY  (10)
806
807 /**
808  * called by client devices, notify the sptlrpc config has changed and
809  * do import_sec_adapt later.
810  */
811 void sptlrpc_conf_client_adapt(struct obd_device *obd)
812 {
813         struct obd_import *imp;
814
815         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0 ||
816                 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0);
817         CDEBUG(D_SEC, "obd %s\n", obd->u.cli.cl_target_uuid.uuid);
818
819         /* serialize with connect/disconnect import */
820         down_read(&obd->u.cli.cl_sem);
821
822         imp = obd->u.cli.cl_import;
823         if (imp) {
824                 spin_lock(&imp->imp_lock);
825                 if (imp->imp_sec)
826                         imp->imp_sec_expire = ktime_get_real_seconds() +
827                                 SEC_ADAPT_DELAY;
828                 spin_unlock(&imp->imp_lock);
829         }
830
831         up_read(&obd->u.cli.cl_sem);
832 }
833 EXPORT_SYMBOL(sptlrpc_conf_client_adapt);
834
835 int sptlrpc_conf_init(void)
836 {
837         mutex_init(&sptlrpc_conf_lock);
838         return 0;
839 }
840
841 void sptlrpc_conf_fini(void)
842 {
843         struct sptlrpc_conf *conf, *conf_next;
844
845         mutex_lock(&sptlrpc_conf_lock);
846         list_for_each_entry_safe(conf, conf_next, &sptlrpc_confs, sc_list) {
847                 sptlrpc_conf_free(conf);
848         }
849         LASSERT(list_empty(&sptlrpc_confs));
850         mutex_unlock(&sptlrpc_conf_lock);
851 }