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